Hex Artifact Content
Not logged in

Artifact 6a71c71db87ffeeb93828591c7c1cc668af67096:

File src/sqlite3.c part of check-in [7a2c37063a] - merge trunk into creole branch by bob on 2009-09-22 07:49:39. Also file src/sqlite3.c part of check-in [129edda98e] - Update to the latest SQLite. Fix a bug in the "diff" and "gdiff" commands. Fix a bug that prevented "fossil config pull" from working. by drh on 2009-09-11 15:06:34.

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 31 38 2e 20 20  ersion 3.6.18.  
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 2a 0a 2a 2a 20 54 68 69 73  rary..**.** This
04e0: 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20 77 61   amalgamation wa
04f0: 73 20 67 65 6e 65 72 61 74 65 64 20 6f 6e 20 32  s generated on 2
0500: 30 30 39 2d 30 39 2d 31 31 20 31 34 3a 31 36 3a  009-09-11 14:16:
0510: 31 33 20 55 54 43 2e 0a 2a 2f 0a 23 64 65 66 69  13 UTC..*/.#defi
0520: 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 31  ne SQLITE_CORE 1
0530: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
0540: 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 20 31 0a 23  AMALGAMATION 1.#
0550: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 50 52  ifndef SQLITE_PR
0560: 49 56 41 54 45 0a 23 20 64 65 66 69 6e 65 20 53  IVATE.# define S
0570: 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 74  QLITE_PRIVATE st
0580: 61 74 69 63 0a 23 65 6e 64 69 66 0a 23 69 66 6e  atic.#endif.#ifn
0590: 64 65 66 20 53 51 4c 49 54 45 5f 41 50 49 0a 23  def SQLITE_API.#
05a0: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41   define SQLITE_A
05b0: 50 49 0a 23 65 6e 64 69 66 0a 2f 2a 2a 2a 2a 2a  PI.#endif./*****
05c0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
05d0: 66 69 6c 65 20 73 71 6c 69 74 65 49 6e 74 2e 68  file sqliteInt.h
05e0: 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
05f0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0600: 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
0610: 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
0620: 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
0630: 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
0640: 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
0650: 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
0660: 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
0670: 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
0680: 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
0690: 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
06a0: 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
06b0: 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
06c0: 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
06d0: 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
06e0: 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
06f0: 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
0700: 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
0710: 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
0720: 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
0730: 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
0740: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0750: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0760: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0770: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0780: 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 69 6e 74  .** Internal int
0790: 65 72 66 61 63 65 20 64 65 66 69 6e 69 74 69 6f  erface definitio
07a0: 6e 73 20 66 6f 72 20 53 51 4c 69 74 65 2e 0a 2a  ns for SQLite..*
07b0: 2a 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51  *.*/.#ifndef _SQ
07c0: 4c 49 54 45 49 4e 54 5f 48 5f 0a 23 64 65 66 69  LITEINT_H_.#defi
07d0: 6e 65 20 5f 53 51 4c 49 54 45 49 4e 54 5f 48 5f  ne _SQLITEINT_H_
07e0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 23 64  ../*.** These #d
07f0: 65 66 69 6e 65 73 20 73 68 6f 75 6c 64 20 65 6e  efines should en
0800: 61 62 6c 65 20 3e 32 47 42 20 66 69 6c 65 20 73  able >2GB file s
0810: 75 70 70 6f 72 74 20 6f 6e 20 50 4f 53 49 58 20  upport on POSIX 
0820: 69 66 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c  if the.** underl
0830: 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73  ying operating s
0840: 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20 69  ystem supports i
0850: 74 2e 20 20 49 66 20 74 68 65 20 4f 53 20 6c 61  t.  If the OS la
0860: 63 6b 73 0a 2a 2a 20 6c 61 72 67 65 20 66 69 6c  cks.** large fil
0870: 65 20 73 75 70 70 6f 72 74 2c 20 6f 72 20 69 66  e support, or if
0880: 20 74 68 65 20 4f 53 20 69 73 20 77 69 6e 64 6f   the OS is windo
0890: 77 73 2c 20 74 68 65 73 65 20 73 68 6f 75 6c 64  ws, these should
08a0: 20 62 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a   be no-ops..**.*
08b0: 2a 20 54 69 63 6b 65 74 20 23 32 37 33 39 3a 20  * Ticket #2739: 
08c0: 20 54 68 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f   The _LARGEFILE_
08d0: 53 4f 55 52 43 45 20 6d 61 63 72 6f 20 6d 75 73  SOURCE macro mus
08e0: 74 20 61 70 70 65 61 72 20 62 65 66 6f 72 65 20  t appear before 
08f0: 61 6e 79 0a 2a 2a 20 73 79 73 74 65 6d 20 23 69  any.** system #i
0900: 6e 63 6c 75 64 65 73 2e 20 20 48 65 6e 63 65 2c  ncludes.  Hence,
0910: 20 74 68 69 73 20 62 6c 6f 63 6b 20 6f 66 20 63   this block of c
0920: 6f 64 65 20 6d 75 73 74 20 62 65 20 74 68 65 20  ode must be the 
0930: 76 65 72 79 20 66 69 72 73 74 0a 2a 2a 20 63 6f  very first.** co
0940: 64 65 20 69 6e 20 61 6c 6c 20 73 6f 75 72 63 65  de in all source
0950: 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61   files..**.** La
0960: 72 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74  rge file support
0970: 20 63 61 6e 20 62 65 20 64 69 73 61 62 6c 65 64   can be disabled
0980: 20 75 73 69 6e 67 20 74 68 65 20 2d 44 53 51 4c   using the -DSQL
0990: 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20  ITE_DISABLE_LFS 
09a0: 73 77 69 74 63 68 0a 2a 2a 20 6f 6e 20 74 68 65  switch.** on the
09b0: 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e   compiler comman
09c0: 64 20 6c 69 6e 65 2e 20 20 54 68 69 73 20 69 73  d line.  This is
09d0: 20 6e 65 63 65 73 73 61 72 79 20 69 66 20 79 6f   necessary if yo
09e0: 75 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 0a  u are compiling.
09f0: 2a 2a 20 6f 6e 20 61 20 72 65 63 65 6e 74 20 6d  ** on a recent m
0a00: 61 63 68 69 6e 65 20 28 65 78 3a 20 52 65 64 20  achine (ex: Red 
0a10: 48 61 74 20 37 2e 32 29 20 62 75 74 20 79 6f 75  Hat 7.2) but you
0a20: 20 77 61 6e 74 20 79 6f 75 72 20 63 6f 64 65 20   want your code 
0a30: 74 6f 20 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61 6e  to work.** on an
0a40: 20 6f 6c 64 65 72 20 6d 61 63 68 69 6e 65 20 28   older machine (
0a50: 65 78 3a 20 52 65 64 20 48 61 74 20 36 2e 30 29  ex: Red Hat 6.0)
0a60: 2e 20 20 49 66 20 79 6f 75 20 63 6f 6d 70 69 6c  .  If you compil
0a70: 65 20 6f 6e 20 52 65 64 20 48 61 74 20 37 2e 32  e on Red Hat 7.2
0a80: 0a 2a 2a 20 77 69 74 68 6f 75 74 20 74 68 69 73  .** without this
0a90: 20 6f 70 74 69 6f 6e 2c 20 4c 46 53 20 69 73 20   option, LFS is 
0aa0: 65 6e 61 62 6c 65 2e 20 20 42 75 74 20 4c 46 53  enable.  But LFS
0ab0: 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20   does not exist 
0ac0: 69 6e 20 74 68 65 20 6b 65 72 6e 65 6c 0a 2a 2a  in the kernel.**
0ad0: 20 69 6e 20 52 65 64 20 48 61 74 20 36 2e 30 2c   in Red Hat 6.0,
0ae0: 20 73 6f 20 74 68 65 20 63 6f 64 65 20 77 6f 6e   so the code won
0af0: 27 74 20 77 6f 72 6b 2e 20 20 48 65 6e 63 65 2c  't work.  Hence,
0b00: 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20 62 69 6e   for maximum bin
0b10: 61 72 79 0a 2a 2a 20 70 6f 72 74 61 62 69 6c 69  ary.** portabili
0b20: 74 79 20 79 6f 75 20 73 68 6f 75 6c 64 20 6f 6d  ty you should om
0b30: 69 74 20 4c 46 53 2e 0a 2a 2a 0a 2a 2a 20 53 69  it LFS..**.** Si
0b40: 6d 69 6c 61 72 20 69 73 20 74 72 75 65 20 66 6f  milar is true fo
0b50: 72 20 4d 61 63 20 4f 53 20 58 2e 20 20 4c 46 53  r Mac OS X.  LFS
0b60: 20 69 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74   is only support
0b70: 65 64 20 6f 6e 20 4d 61 63 20 4f 53 20 58 20 39  ed on Mac OS X 9
0b80: 20 61 6e 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a 23   and later..*/.#
0b90: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 49  ifndef SQLITE_DI
0ba0: 53 41 42 4c 45 5f 4c 46 53 0a 23 20 64 65 66 69  SABLE_LFS.# defi
0bb0: 6e 65 20 5f 4c 41 52 47 45 5f 46 49 4c 45 20 20  ne _LARGE_FILE  
0bc0: 20 20 20 20 20 31 0a 23 20 69 66 6e 64 65 66 20       1.# ifndef 
0bd0: 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54  _FILE_OFFSET_BIT
0be0: 53 0a 23 20 20 20 64 65 66 69 6e 65 20 5f 46 49  S.#   define _FI
0bf0: 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54 53 20 36  LE_OFFSET_BITS 6
0c00: 34 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69  4.# endif.# defi
0c10: 6e 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f 53 4f  ne _LARGEFILE_SO
0c20: 55 52 43 45 20 31 0a 23 65 6e 64 69 66 0a 0a 2f  URCE 1.#endif../
0c30: 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 74 68 65  *.** Include the
0c40: 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 68   configuration h
0c50: 65 61 64 65 72 20 6f 75 74 70 75 74 20 62 79 20  eader output by 
0c60: 27 63 6f 6e 66 69 67 75 72 65 27 20 69 66 20 77  'configure' if w
0c70: 65 27 72 65 20 75 73 69 6e 67 20 74 68 65 0a 2a  e're using the.*
0c80: 2a 20 61 75 74 6f 63 6f 6e 66 2d 62 61 73 65 64  * autoconf-based
0c90: 20 62 75 69 6c 64 0a 2a 2f 0a 23 69 66 64 65 66   build.*/.#ifdef
0ca0: 20 5f 48 41 56 45 5f 53 51 4c 49 54 45 5f 43 4f   _HAVE_SQLITE_CO
0cb0: 4e 46 49 47 5f 48 0a 23 69 6e 63 6c 75 64 65 20  NFIG_H.#include 
0cc0: 22 63 6f 6e 66 69 67 2e 68 22 0a 23 65 6e 64 69  "config.h".#endi
0cd0: 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f../************
0ce0: 2a 2a 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 74  ** Include sqlit
0cf0: 65 4c 69 6d 69 74 2e 68 20 69 6e 20 74 68 65 20  eLimit.h in the 
0d00: 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65  middle of sqlite
0d10: 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
0d20: 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
0d30: 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73 71  ** Begin file sq
0d40: 6c 69 74 65 4c 69 6d 69 74 2e 68 20 2a 2a 2a 2a  liteLimit.h ****
0d50: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0d60: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0d70: 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4d 61  */./*.** 2007 Ma
0d80: 79 20 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75  y 7.**.** The au
0d90: 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
0da0: 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
0db0: 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
0dc0: 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
0dd0: 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
0de0: 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
0df0: 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
0e00: 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
0e10: 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
0e20: 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
0e30: 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
0e40: 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
0e50: 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
0e60: 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
0e70: 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
0e80: 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
0e90: 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
0ea0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0eb0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0ec0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0ed0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0ee0: 2a 2a 0a 2a 2a 20 0a 2a 2a 20 54 68 69 73 20 66  **.** .** This f
0ef0: 69 6c 65 20 64 65 66 69 6e 65 73 20 76 61 72 69  ile defines vari
0f00: 6f 75 73 20 6c 69 6d 69 74 73 20 6f 66 20 77 68  ous limits of wh
0f10: 61 74 20 53 51 4c 69 74 65 20 63 61 6e 20 70 72  at SQLite can pr
0f20: 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 40 28 23  ocess..**.** @(#
0f30: 29 20 24 49 64 3a 20 73 71 6c 69 74 65 4c 69 6d  ) $Id: sqliteLim
0f40: 69 74 2e 68 2c 76 20 31 2e 31 30 20 32 30 30 39  it.h,v 1.10 2009
0f50: 2f 30 31 2f 31 30 20 31 36 3a 31 35 3a 30 39 20  /01/10 16:15:09 
0f60: 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20  danielk1977 Exp 
0f70: 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  $.*/../*.** The 
0f80: 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f  maximum length o
0f90: 66 20 61 20 54 45 58 54 20 6f 72 20 42 4c 4f 42  f a TEXT or BLOB
0fa0: 20 69 6e 20 62 79 74 65 73 2e 20 20 20 54 68 69   in bytes.   Thi
0fb0: 73 20 61 6c 73 6f 0a 2a 2a 20 6c 69 6d 69 74 73  s also.** limits
0fc0: 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 20 72   the size of a r
0fd0: 6f 77 20 69 6e 20 61 20 74 61 62 6c 65 20 6f 72  ow in a table or
0fe0: 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68   index..**.** Th
0ff0: 65 20 68 61 72 64 20 6c 69 6d 69 74 20 69 73 20  e hard limit is 
1000: 74 68 65 20 61 62 69 6c 69 74 79 20 6f 66 20 61  the ability of a
1010: 20 33 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69   32-bit signed i
1020: 6e 74 65 67 65 72 0a 2a 2a 20 74 6f 20 63 6f 75  nteger.** to cou
1030: 6e 74 20 74 68 65 20 73 69 7a 65 3a 20 32 5e 33  nt the size: 2^3
1040: 31 2d 31 20 6f 72 20 32 31 34 37 34 38 33 36 34  1-1 or 214748364
1050: 37 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  7..*/.#ifndef SQ
1060: 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 0a  LITE_MAX_LENGTH.
1070: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
1080: 4d 41 58 5f 4c 45 4e 47 54 48 20 31 30 30 30 30  MAX_LENGTH 10000
1090: 30 30 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a  00000.#endif../*
10a0: 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20  .** This is the 
10b0: 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f  maximum number o
10c0: 66 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 43 6f 6c  f.**.**    * Col
10d0: 75 6d 6e 73 20 69 6e 20 61 20 74 61 62 6c 65 0a  umns in a table.
10e0: 2a 2a 20 20 20 20 2a 20 43 6f 6c 75 6d 6e 73 20  **    * Columns 
10f0: 69 6e 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 20  in an index.**  
1100: 20 20 2a 20 43 6f 6c 75 6d 6e 73 20 69 6e 20 61    * Columns in a
1110: 20 76 69 65 77 0a 2a 2a 20 20 20 20 2a 20 54 65   view.**    * Te
1120: 72 6d 73 20 69 6e 20 74 68 65 20 53 45 54 20 63  rms in the SET c
1130: 6c 61 75 73 65 20 6f 66 20 61 6e 20 55 50 44 41  lause of an UPDA
1140: 54 45 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20  TE statement.** 
1150: 20 20 20 2a 20 54 65 72 6d 73 20 69 6e 20 74 68     * Terms in th
1160: 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20  e result set of 
1170: 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  a SELECT stateme
1180: 6e 74 0a 2a 2a 20 20 20 20 2a 20 54 65 72 6d 73  nt.**    * Terms
1190: 20 69 6e 20 74 68 65 20 47 52 4f 55 50 20 42 59   in the GROUP BY
11a0: 20 6f 72 20 4f 52 44 45 52 20 42 59 20 63 6c 61   or ORDER BY cla
11b0: 75 73 65 73 20 6f 66 20 61 20 53 45 4c 45 43 54  uses of a SELECT
11c0: 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 20   statement..**  
11d0: 20 20 2a 20 54 65 72 6d 73 20 69 6e 20 74 68 65    * Terms in the
11e0: 20 56 41 4c 55 45 53 20 63 6c 61 75 73 65 20 6f   VALUES clause o
11f0: 66 20 61 6e 20 49 4e 53 45 52 54 20 73 74 61 74  f an INSERT stat
1200: 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ement.**.** The 
1210: 68 61 72 64 20 75 70 70 65 72 20 6c 69 6d 69 74  hard upper limit
1220: 20 68 65 72 65 20 69 73 20 33 32 36 37 36 2e 20   here is 32676. 
1230: 20 4d 6f 73 74 20 64 61 74 61 62 61 73 65 20 70   Most database p
1240: 65 6f 70 6c 65 20 77 69 6c 6c 0a 2a 2a 20 74 65  eople will.** te
1250: 6c 6c 20 79 6f 75 20 74 68 61 74 20 69 6e 20 61  ll you that in a
1260: 20 77 65 6c 6c 2d 6e 6f 72 6d 61 6c 69 7a 65 64   well-normalized
1270: 20 64 61 74 61 62 61 73 65 2c 20 79 6f 75 20 75   database, you u
1280: 73 75 61 6c 6c 79 20 73 68 6f 75 6c 64 0a 2a 2a  sually should.**
1290: 20 6e 6f 74 20 68 61 76 65 20 6d 6f 72 65 20 74   not have more t
12a0: 68 61 6e 20 61 20 64 6f 7a 65 6e 20 6f 72 20 73  han a dozen or s
12b0: 6f 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 6e 79  o columns in any
12c0: 20 74 61 62 6c 65 2e 20 20 41 6e 64 20 69 66 0a   table.  And if.
12d0: 2a 2a 20 74 68 61 74 20 69 73 20 74 68 65 20 63  ** that is the c
12e0: 61 73 65 2c 20 74 68 65 72 65 20 69 73 20 6e 6f  ase, there is no
12f0: 20 70 6f 69 6e 74 20 69 6e 20 68 61 76 69 6e 67   point in having
1300: 20 6d 6f 72 65 20 74 68 61 6e 20 61 20 66 65 77   more than a few
1310: 0a 2a 2a 20 64 6f 7a 65 6e 20 76 61 6c 75 65 73  .** dozen values
1320: 20 69 6e 20 61 6e 79 20 6f 66 20 74 68 65 20 6f   in any of the o
1330: 74 68 65 72 20 73 69 74 75 61 74 69 6f 6e 73 20  ther situations 
1340: 64 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 2e  described above.
1350: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
1360: 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 23 20  TE_MAX_COLUMN.# 
1370: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41  define SQLITE_MA
1380: 58 5f 43 4f 4c 55 4d 4e 20 32 30 30 30 0a 23 65  X_COLUMN 2000.#e
1390: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
13a0: 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f  maximum length o
13b0: 66 20 61 20 73 69 6e 67 6c 65 20 53 51 4c 20 73  f a single SQL s
13c0: 74 61 74 65 6d 65 6e 74 20 69 6e 20 62 79 74 65  tatement in byte
13d0: 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 75 73 65 64  s..**.** It used
13e0: 20 74 6f 20 62 65 20 74 68 65 20 63 61 73 65 20   to be the case 
13f0: 74 68 61 74 20 73 65 74 74 69 6e 67 20 74 68 69  that setting thi
1400: 73 20 76 61 6c 75 65 20 74 6f 20 7a 65 72 6f 20  s value to zero 
1410: 77 6f 75 6c 64 0a 2a 2a 20 74 75 72 6e 20 74 68  would.** turn th
1420: 65 20 6c 69 6d 69 74 20 6f 66 66 2e 20 20 54 68  e limit off.  Th
1430: 61 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20  at is no longer 
1440: 74 72 75 65 2e 20 20 49 74 20 69 73 20 6e 6f 74  true.  It is not
1450: 20 70 6f 73 73 69 62 6c 65 0a 2a 2a 20 74 6f 20   possible.** to 
1460: 74 75 72 6e 20 74 68 69 73 20 6c 69 6d 69 74 20  turn this limit 
1470: 6f 66 66 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  off..*/.#ifndef 
1480: 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c  SQLITE_MAX_SQL_L
1490: 45 4e 47 54 48 0a 23 20 64 65 66 69 6e 65 20 53  ENGTH.# define S
14a0: 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45  QLITE_MAX_SQL_LE
14b0: 4e 47 54 48 20 31 30 30 30 30 30 30 30 30 30 0a  NGTH 1000000000.
14c0: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
14d0: 65 20 6d 61 78 69 6d 75 6d 20 64 65 70 74 68 20  e maximum depth 
14e0: 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  of an expression
14f0: 20 74 72 65 65 2e 20 54 68 69 73 20 69 73 20 6c   tree. This is l
1500: 69 6d 69 74 65 64 20 74 6f 20 0a 2a 2a 20 73 6f  imited to .** so
1510: 6d 65 20 65 78 74 65 6e 74 20 62 79 20 53 51 4c  me extent by SQL
1520: 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47  ITE_MAX_SQL_LENG
1530: 54 48 2e 20 42 75 74 20 73 6f 6d 65 74 69 6d 65  TH. But sometime
1540: 20 79 6f 75 20 6d 69 67 68 74 20 0a 2a 2a 20 77   you might .** w
1550: 61 6e 74 20 74 6f 20 70 6c 61 63 65 20 6d 6f 72  ant to place mor
1560: 65 20 73 65 76 65 72 65 20 6c 69 6d 69 74 73 20  e severe limits 
1570: 6f 6e 20 74 68 65 20 63 6f 6d 70 6c 65 78 69 74  on the complexit
1580: 79 20 6f 66 20 61 6e 20 0a 2a 2a 20 65 78 70 72  y of an .** expr
1590: 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20  ession..**.** A 
15a0: 76 61 6c 75 65 20 6f 66 20 30 20 75 73 65 64 20  value of 0 used 
15b0: 74 6f 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65  to mean that the
15c0: 20 6c 69 6d 69 74 20 77 61 73 20 6e 6f 74 20 65   limit was not e
15d0: 6e 66 6f 72 63 65 64 2e 0a 2a 2a 20 42 75 74 20  nforced..** But 
15e0: 74 68 61 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65  that is no longe
15f0: 72 20 74 72 75 65 2e 20 20 54 68 65 20 6c 69 6d  r true.  The lim
1600: 69 74 20 69 73 20 6e 6f 77 20 73 74 72 69 63 74  it is now strict
1610: 6c 79 20 65 6e 66 6f 72 63 65 64 0a 2a 2a 20 61  ly enforced.** a
1620: 74 20 61 6c 6c 20 74 69 6d 65 73 2e 0a 2a 2f 0a  t all times..*/.
1630: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d  #ifndef SQLITE_M
1640: 41 58 5f 45 58 50 52 5f 44 45 50 54 48 0a 23 20  AX_EXPR_DEPTH.# 
1650: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41  define SQLITE_MA
1660: 58 5f 45 58 50 52 5f 44 45 50 54 48 20 31 30 30  X_EXPR_DEPTH 100
1670: 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  0.#endif../*.** 
1680: 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62  The maximum numb
1690: 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 61  er of terms in a
16a0: 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54   compound SELECT
16b0: 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 54   statement..** T
16c0: 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f  he code generato
16d0: 72 20 66 6f 72 20 63 6f 6d 70 6f 75 6e 64 20 53  r for compound S
16e0: 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73  ELECT statements
16f0: 20 64 6f 65 73 20 6f 6e 65 0a 2a 2a 20 6c 65 76   does one.** lev
1700: 65 6c 20 6f 66 20 72 65 63 75 72 73 69 6f 6e 20  el of recursion 
1710: 66 6f 72 20 65 61 63 68 20 74 65 72 6d 2e 20 20  for each term.  
1720: 41 20 73 74 61 63 6b 20 6f 76 65 72 66 6c 6f 77  A stack overflow
1730: 20 63 61 6e 20 72 65 73 75 6c 74 0a 2a 2a 20 69   can result.** i
1740: 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  f the number of 
1750: 74 65 72 6d 73 20 69 73 20 74 6f 6f 20 6c 61 72  terms is too lar
1760: 67 65 2e 20 20 49 6e 20 70 72 61 63 74 69 63 65  ge.  In practice
1770: 2c 20 6d 6f 73 74 20 53 51 4c 0a 2a 2a 20 6e 65  , most SQL.** ne
1780: 76 65 72 20 68 61 73 20 6d 6f 72 65 20 74 68 61  ver has more tha
1790: 6e 20 33 20 6f 72 20 34 20 74 65 72 6d 73 2e 20  n 3 or 4 terms. 
17a0: 20 55 73 65 20 61 20 76 61 6c 75 65 20 6f 66 20   Use a value of 
17b0: 30 20 74 6f 20 64 69 73 61 62 6c 65 0a 2a 2a 20  0 to disable.** 
17c0: 61 6e 79 20 6c 69 6d 69 74 20 6f 6e 20 74 68 65  any limit on the
17d0: 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73   number of terms
17e0: 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 74 20 53   in a compount S
17f0: 45 4c 45 43 54 2e 0a 2a 2f 0a 23 69 66 6e 64 65  ELECT..*/.#ifnde
1800: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d  f SQLITE_MAX_COM
1810: 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a 23 20 64  POUND_SELECT.# d
1820: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58  efine SQLITE_MAX
1830: 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54  _COMPOUND_SELECT
1840: 20 35 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a   500.#endif../*.
1850: 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e  ** The maximum n
1860: 75 6d 62 65 72 20 6f 66 20 6f 70 63 6f 64 65 73  umber of opcodes
1870: 20 69 6e 20 61 20 56 44 42 45 20 70 72 6f 67 72   in a VDBE progr
1880: 61 6d 2e 0a 2a 2a 20 4e 6f 74 20 63 75 72 72 65  am..** Not curre
1890: 6e 74 6c 79 20 65 6e 66 6f 72 63 65 64 2e 0a 2a  ntly enforced..*
18a0: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
18b0: 5f 4d 41 58 5f 56 44 42 45 5f 4f 50 0a 23 20 64  _MAX_VDBE_OP.# d
18c0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58  efine SQLITE_MAX
18d0: 5f 56 44 42 45 5f 4f 50 20 32 35 30 30 30 0a 23  _VDBE_OP 25000.#
18e0: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
18f0: 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   maximum number 
1900: 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  of arguments to 
1910: 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e  an SQL function.
1920: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
1930: 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f  TE_MAX_FUNCTION_
1940: 41 52 47 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  ARG.# define SQL
1950: 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e  ITE_MAX_FUNCTION
1960: 5f 41 52 47 20 31 32 37 0a 23 65 6e 64 69 66 0a  _ARG 127.#endif.
1970: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d  ./*.** The maxim
1980: 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 69 6e 2d  um number of in-
1990: 6d 65 6d 6f 72 79 20 70 61 67 65 73 20 74 6f 20  memory pages to 
19a0: 75 73 65 20 66 6f 72 20 74 68 65 20 6d 61 69 6e  use for the main
19b0: 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 74 61 62   database.** tab
19c0: 6c 65 20 61 6e 64 20 66 6f 72 20 74 65 6d 70 6f  le and for tempo
19d0: 72 61 72 79 20 74 61 62 6c 65 73 2e 20 20 54 68  rary tables.  Th
19e0: 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  e SQLITE_DEFAULT
19f0: 5f 43 41 43 48 45 5f 53 49 5a 45 0a 2a 2f 0a 23  _CACHE_SIZE.*/.#
1a00: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45  ifndef SQLITE_DE
1a10: 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45  FAULT_CACHE_SIZE
1a20: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
1a30: 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53  _DEFAULT_CACHE_S
1a40: 49 5a 45 20 20 32 30 30 30 0a 23 65 6e 64 69 66  IZE  2000.#endif
1a50: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1a60: 44 45 46 41 55 4c 54 5f 54 45 4d 50 5f 43 41 43  DEFAULT_TEMP_CAC
1a70: 48 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65  HE_SIZE.# define
1a80: 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
1a90: 54 45 4d 50 5f 43 41 43 48 45 5f 53 49 5a 45 20  TEMP_CACHE_SIZE 
1aa0: 20 35 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a   500.#endif../*.
1ab0: 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e  ** The maximum n
1ac0: 75 6d 62 65 72 20 6f 66 20 61 74 74 61 63 68 65  umber of attache
1ad0: 64 20 64 61 74 61 62 61 73 65 73 2e 20 20 54 68  d databases.  Th
1ae0: 69 73 20 6d 75 73 74 20 62 65 20 62 65 74 77 65  is must be betwe
1af0: 65 6e 20 30 0a 2a 2a 20 61 6e 64 20 33 30 2e 20  en 0.** and 30. 
1b00: 20 54 68 65 20 75 70 70 65 72 20 62 6f 75 6e 64   The upper bound
1b10: 20 6f 6e 20 33 30 20 69 73 20 62 65 63 61 75 73   on 30 is becaus
1b20: 65 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67  e a 32-bit integ
1b30: 65 72 20 62 69 74 6d 61 70 0a 2a 2a 20 69 73 20  er bitmap.** is 
1b40: 75 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20  used internally 
1b50: 74 6f 20 74 72 61 63 6b 20 61 74 74 61 63 68 65  to track attache
1b60: 64 20 64 61 74 61 62 61 73 65 73 2e 0a 2a 2f 0a  d databases..*/.
1b70: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d  #ifndef SQLITE_M
1b80: 41 58 5f 41 54 54 41 43 48 45 44 0a 23 20 64 65  AX_ATTACHED.# de
1b90: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f  fine SQLITE_MAX_
1ba0: 41 54 54 41 43 48 45 44 20 31 30 0a 23 65 6e 64  ATTACHED 10.#end
1bb0: 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d  if.../*.** The m
1bc0: 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20  aximum value of 
1bd0: 61 20 3f 6e 6e 6e 20 77 69 6c 64 63 61 72 64 20  a ?nnn wildcard 
1be0: 74 68 61 74 20 74 68 65 20 70 61 72 73 65 72 20  that the parser 
1bf0: 77 69 6c 6c 20 61 63 63 65 70 74 2e 0a 2a 2f 0a  will accept..*/.
1c00: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d  #ifndef SQLITE_M
1c10: 41 58 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42  AX_VARIABLE_NUMB
1c20: 45 52 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  ER.# define SQLI
1c30: 54 45 5f 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f  TE_MAX_VARIABLE_
1c40: 4e 55 4d 42 45 52 20 39 39 39 0a 23 65 6e 64 69  NUMBER 999.#endi
1c50: 66 0a 0a 2f 2a 20 4d 61 78 69 6d 75 6d 20 70 61  f../* Maximum pa
1c60: 67 65 20 73 69 7a 65 2e 20 20 54 68 65 20 75 70  ge size.  The up
1c70: 70 65 72 20 62 6f 75 6e 64 20 6f 6e 20 74 68 69  per bound on thi
1c80: 73 20 76 61 6c 75 65 20 69 73 20 33 32 37 36 38  s value is 32768
1c90: 2e 20 20 54 68 69 73 20 61 20 6c 69 6d 69 74 0a  .  This a limit.
1ca0: 2a 2a 20 69 6d 70 6f 73 65 64 20 62 79 20 74 68  ** imposed by th
1cb0: 65 20 6e 65 63 65 73 73 69 74 79 20 6f 66 20 73  e necessity of s
1cc0: 74 6f 72 69 6e 67 20 74 68 65 20 76 61 6c 75 65  toring the value
1cd0: 20 69 6e 20 61 20 32 2d 62 79 74 65 20 75 6e 73   in a 2-byte uns
1ce0: 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a  igned integer.**
1cf0: 20 61 6e 64 20 74 68 65 20 66 61 63 74 20 74 68   and the fact th
1d00: 61 74 20 74 68 65 20 70 61 67 65 20 73 69 7a 65  at the page size
1d10: 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65 72   must be a power
1d20: 20 6f 66 20 32 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   of 2..**.** If 
1d30: 74 68 69 73 20 6c 69 6d 69 74 20 69 73 20 63 68  this limit is ch
1d40: 61 6e 67 65 64 2c 20 74 68 65 6e 20 74 68 65 20  anged, then the 
1d50: 63 6f 6d 70 69 6c 65 64 20 6c 69 62 72 61 72 79  compiled library
1d60: 20 69 73 20 74 65 63 68 6e 69 63 61 6c 6c 79 0a   is technically.
1d70: 2a 2a 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20  ** incompatible 
1d80: 77 69 74 68 20 61 6e 20 53 51 4c 69 74 65 20 6c  with an SQLite l
1d90: 69 62 72 61 72 79 20 63 6f 6d 70 69 6c 65 64 20  ibrary compiled 
1da0: 77 69 74 68 20 61 20 64 69 66 66 65 72 65 6e 74  with a different
1db0: 20 6c 69 6d 69 74 2e 20 49 66 0a 2a 2a 20 61 20   limit. If.** a 
1dc0: 70 72 6f 63 65 73 73 20 6f 70 65 72 61 74 69 6e  process operatin
1dd0: 67 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20  g on a database 
1de0: 77 69 74 68 20 61 20 70 61 67 65 2d 73 69 7a 65  with a page-size
1df0: 20 6f 66 20 36 35 35 33 36 20 62 79 74 65 73 20   of 65536 bytes 
1e00: 0a 2a 2a 20 63 72 61 73 68 65 73 2c 20 74 68 65  .** crashes, the
1e10: 6e 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  n an instance of
1e20: 20 53 51 4c 69 74 65 20 63 6f 6d 70 69 6c 65 64   SQLite compiled
1e30: 20 77 69 74 68 20 74 68 65 20 64 65 66 61 75 6c   with the defaul
1e40: 74 20 70 61 67 65 2d 73 69 7a 65 20 0a 2a 2a 20  t page-size .** 
1e50: 6c 69 6d 69 74 20 77 69 6c 6c 20 6e 6f 74 20 62  limit will not b
1e60: 65 20 61 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61  e able to rollba
1e70: 63 6b 20 74 68 65 20 61 62 6f 72 74 65 64 20 74  ck the aborted t
1e80: 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 69 73  ransaction. This
1e90: 20 63 6f 75 6c 64 0a 2a 2a 20 6c 65 61 64 20 74   could.** lead t
1ea0: 6f 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75  o database corru
1eb0: 70 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65  ption..*/.#ifnde
1ec0: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47  f SQLITE_MAX_PAG
1ed0: 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20  E_SIZE.# define 
1ee0: 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f  SQLITE_MAX_PAGE_
1ef0: 53 49 5a 45 20 33 32 37 36 38 0a 23 65 6e 64 69  SIZE 32768.#endi
1f00: 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 64 65  f.../*.** The de
1f10: 66 61 75 6c 74 20 73 69 7a 65 20 6f 66 20 61 20  fault size of a 
1f20: 64 61 74 61 62 61 73 65 20 70 61 67 65 2e 0a 2a  database page..*
1f30: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
1f40: 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49  _DEFAULT_PAGE_SI
1f50: 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  ZE.# define SQLI
1f60: 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f  TE_DEFAULT_PAGE_
1f70: 53 49 5a 45 20 31 30 32 34 0a 23 65 6e 64 69 66  SIZE 1024.#endif
1f80: 0a 23 69 66 20 53 51 4c 49 54 45 5f 44 45 46 41  .#if SQLITE_DEFA
1f90: 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3e 53 51  ULT_PAGE_SIZE>SQ
1fa0: 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49  LITE_MAX_PAGE_SI
1fb0: 5a 45 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54  ZE.# undef SQLIT
1fc0: 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53  E_DEFAULT_PAGE_S
1fd0: 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  IZE.# define SQL
1fe0: 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45  ITE_DEFAULT_PAGE
1ff0: 5f 53 49 5a 45 20 53 51 4c 49 54 45 5f 4d 41 58  _SIZE SQLITE_MAX
2000: 5f 50 41 47 45 5f 53 49 5a 45 0a 23 65 6e 64 69  _PAGE_SIZE.#endi
2010: 66 0a 0a 2f 2a 0a 2a 2a 20 4f 72 64 69 6e 61 72  f../*.** Ordinar
2020: 69 6c 79 2c 20 69 66 20 6e 6f 20 76 61 6c 75 65  ily, if no value
2030: 20 69 73 20 65 78 70 6c 69 63 69 74 6c 79 20 70   is explicitly p
2040: 72 6f 76 69 64 65 64 2c 20 53 51 4c 69 74 65 20  rovided, SQLite 
2050: 63 72 65 61 74 65 73 20 64 61 74 61 62 61 73 65  creates database
2060: 73 0a 2a 2a 20 77 69 74 68 20 70 61 67 65 20 73  s.** with page s
2070: 69 7a 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55  ize SQLITE_DEFAU
2080: 4c 54 5f 50 41 47 45 5f 53 49 5a 45 2e 20 48 6f  LT_PAGE_SIZE. Ho
2090: 77 65 76 65 72 2c 20 62 61 73 65 64 20 6f 6e 20  wever, based on 
20a0: 63 65 72 74 61 69 6e 0a 2a 2a 20 64 65 76 69 63  certain.** devic
20b0: 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63  e characteristic
20c0: 73 20 28 73 65 63 74 6f 72 2d 73 69 7a 65 20 61  s (sector-size a
20d0: 6e 64 20 61 74 6f 6d 69 63 20 77 72 69 74 65 28  nd atomic write(
20e0: 29 20 73 75 70 70 6f 72 74 29 2c 0a 2a 2a 20 53  ) support),.** S
20f0: 51 4c 69 74 65 20 6d 61 79 20 63 68 6f 6f 73 65  QLite may choose
2100: 20 61 20 6c 61 72 67 65 72 20 76 61 6c 75 65 2e   a larger value.
2110: 20 54 68 69 73 20 63 6f 6e 73 74 61 6e 74 20 69   This constant i
2120: 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 61  s the maximum va
2130: 6c 75 65 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69  lue.** SQLite wi
2140: 6c 6c 20 63 68 6f 6f 73 65 20 6f 6e 20 69 74 73  ll choose on its
2150: 20 6f 77 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66   own..*/.#ifndef
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 38 31 39 32 0a 23 65 6e 64 69 66 0a  IZE 8192.#endif.
21b0: 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44  #if SQLITE_MAX_D
21c0: 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45  EFAULT_PAGE_SIZE
21d0: 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45  >SQLITE_MAX_PAGE
21e0: 5f 53 49 5a 45 0a 23 20 75 6e 64 65 66 20 53 51  _SIZE.# undef SQ
21f0: 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54  LITE_MAX_DEFAULT
2200: 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20 64 65 66  _PAGE_SIZE.# def
2210: 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44  ine SQLITE_MAX_D
2220: 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45  EFAULT_PAGE_SIZE
2230: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45   SQLITE_MAX_PAGE
2240: 5f 53 49 5a 45 0a 23 65 6e 64 69 66 0a 0a 0a 2f  _SIZE.#endif.../
2250: 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d  *.** Maximum num
2260: 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
2270: 6f 6e 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  one database fil
2280: 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73  e..**.** This is
2290: 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 74 68 65   really just the
22a0: 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66   default value f
22b0: 6f 72 20 74 68 65 20 6d 61 78 5f 70 61 67 65 5f  or the max_page_
22c0: 63 6f 75 6e 74 20 70 72 61 67 6d 61 2e 0a 2a 2a  count pragma..**
22d0: 20 54 68 69 73 20 76 61 6c 75 65 20 63 61 6e 20   This value can 
22e0: 62 65 20 6c 6f 77 65 72 65 64 20 28 6f 72 20 72  be lowered (or r
22f0: 61 69 73 65 64 29 20 61 74 20 72 75 6e 2d 74 69  aised) at run-ti
2300: 6d 65 20 75 73 69 6e 67 20 74 68 61 74 20 74 68  me using that th
2310: 65 0a 2a 2a 20 6d 61 78 5f 70 61 67 65 5f 63 6f  e.** max_page_co
2320: 75 6e 74 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69  unt macro..*/.#i
2330: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58  fndef SQLITE_MAX
2340: 5f 50 41 47 45 5f 43 4f 55 4e 54 0a 23 20 64 65  _PAGE_COUNT.# de
2350: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f  fine SQLITE_MAX_
2360: 50 41 47 45 5f 43 4f 55 4e 54 20 31 30 37 33 37  PAGE_COUNT 10737
2370: 34 31 38 32 33 0a 23 65 6e 64 69 66 0a 0a 2f 2a  41823.#endif../*
2380: 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67  .** Maximum leng
2390: 74 68 20 28 69 6e 20 62 79 74 65 73 29 20 6f 66  th (in bytes) of
23a0: 20 74 68 65 20 70 61 74 74 65 72 6e 20 69 6e 20   the pattern in 
23b0: 61 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 0a 2a  a LIKE or GLOB.*
23c0: 2a 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 23  * operator..*/.#
23d0: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41  ifndef SQLITE_MA
23e0: 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c  X_LIKE_PATTERN_L
23f0: 45 4e 47 54 48 0a 23 20 64 65 66 69 6e 65 20 53  ENGTH.# define S
2400: 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50  QLITE_MAX_LIKE_P
2410: 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 20 35 30  ATTERN_LENGTH 50
2420: 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  000.#endif../*.*
2430: 2a 20 4d 61 78 69 6d 75 6d 20 64 65 70 74 68 20  * Maximum depth 
2440: 6f 66 20 72 65 63 75 72 73 69 6f 6e 20 66 6f 72  of recursion for
2450: 20 74 72 69 67 67 65 72 73 2e 0a 2a 2f 0a 23 69   triggers..*/.#i
2460: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58  fndef SQLITE_MAX
2470: 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48 0a 23  _TRIGGER_DEPTH.#
2480: 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
2490: 45 5f 53 4d 41 4c 4c 5f 53 54 41 43 4b 29 0a 23  E_SMALL_STACK).#
24a0: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d   define SQLITE_M
24b0: 41 58 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48  AX_TRIGGER_DEPTH
24c0: 20 31 30 0a 23 65 6c 73 65 0a 23 20 64 65 66 69   10.#else.# defi
24d0: 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 54 52  ne SQLITE_MAX_TR
24e0: 49 47 47 45 52 5f 44 45 50 54 48 20 31 30 30 30  IGGER_DEPTH 1000
24f0: 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a  .#endif.#endif..
2500: 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2510: 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 4c 69 6d  End of sqliteLim
2520: 69 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  it.h ***********
2530: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2540: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2550: 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2560: 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
2570: 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
2580: 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
2590: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
25a0: 0a 2f 2a 20 44 69 73 61 62 6c 65 20 6e 75 69 73  ./* Disable nuis
25b0: 61 6e 63 65 20 77 61 72 6e 69 6e 67 73 20 6f 6e  ance warnings on
25c0: 20 42 6f 72 6c 61 6e 64 20 63 6f 6d 70 69 6c 65   Borland compile
25d0: 72 73 20 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65  rs */.#if define
25e0: 64 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a  d(__BORLANDC__).
25f0: 23 70 72 61 67 6d 61 20 77 61 72 6e 20 2d 72 63  #pragma warn -rc
2600: 68 20 2f 2a 20 75 6e 72 65 61 63 68 61 62 6c 65  h /* unreachable
2610: 20 63 6f 64 65 20 2a 2f 0a 23 70 72 61 67 6d 61   code */.#pragma
2620: 20 77 61 72 6e 20 2d 63 63 63 20 2f 2a 20 43 6f   warn -ccc /* Co
2630: 6e 64 69 74 69 6f 6e 20 69 73 20 61 6c 77 61 79  ndition is alway
2640: 73 20 74 72 75 65 20 6f 72 20 66 61 6c 73 65 20  s true or false 
2650: 2a 2f 0a 23 70 72 61 67 6d 61 20 77 61 72 6e 20  */.#pragma warn 
2660: 2d 61 75 73 20 2f 2a 20 41 73 73 69 67 6e 65 64  -aus /* Assigned
2670: 20 76 61 6c 75 65 20 69 73 20 6e 65 76 65 72 20   value is never 
2680: 75 73 65 64 20 2a 2f 0a 23 70 72 61 67 6d 61 20  used */.#pragma 
2690: 77 61 72 6e 20 2d 63 73 75 20 2f 2a 20 43 6f 6d  warn -csu /* Com
26a0: 70 61 72 69 6e 67 20 73 69 67 6e 65 64 20 61 6e  paring signed an
26b0: 64 20 75 6e 73 69 67 6e 65 64 20 2a 2f 0a 23 70  d unsigned */.#p
26c0: 72 61 67 6d 61 20 77 61 72 6e 20 2d 73 70 61 20  ragma warn -spa 
26d0: 2f 2a 20 53 75 73 70 69 63 69 6f 75 73 20 70 6f  /* Suspicious po
26e0: 69 6e 74 65 72 20 61 72 69 74 68 6d 65 74 69 63  inter arithmetic
26f0: 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 4e   */.#endif../* N
2700: 65 65 64 65 64 20 66 6f 72 20 76 61 72 69 6f 75  eeded for variou
2710: 73 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e 2e 2e  s definitions...
2720: 20 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 47 4e 55   */.#ifndef _GNU
2730: 5f 53 4f 55 52 43 45 0a 23 20 64 65 66 69 6e 65  _SOURCE.# define
2740: 20 5f 47 4e 55 5f 53 4f 55 52 43 45 0a 23 65 6e   _GNU_SOURCE.#en
2750: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75  dif../*.** Inclu
2760: 64 65 20 73 74 61 6e 64 61 72 64 20 68 65 61 64  de standard head
2770: 65 72 20 66 69 6c 65 73 20 61 73 20 6e 65 63 65  er files as nece
2780: 73 73 61 72 79 0a 2a 2f 0a 23 69 66 64 65 66 20  ssary.*/.#ifdef 
2790: 48 41 56 45 5f 53 54 44 49 4e 54 5f 48 0a 23 69  HAVE_STDINT_H.#i
27a0: 6e 63 6c 75 64 65 20 3c 73 74 64 69 6e 74 2e 68  nclude <stdint.h
27b0: 3e 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  >.#endif.#ifdef 
27c0: 48 41 56 45 5f 49 4e 54 54 59 50 45 53 5f 48 0a  HAVE_INTTYPES_H.
27d0: 23 69 6e 63 6c 75 64 65 20 3c 69 6e 74 74 79 70  #include <inttyp
27e0: 65 73 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 23 64  es.h>.#endif..#d
27f0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44  efine SQLITE_IND
2800: 45 58 5f 53 41 4d 50 4c 45 53 20 31 30 0a 0a 2f  EX_SAMPLES 10../
2810: 2a 0a 2a 2a 20 54 68 69 73 20 6d 61 63 72 6f 20  *.** This macro 
2820: 69 73 20 75 73 65 64 20 74 6f 20 22 68 69 64 65  is used to "hide
2830: 22 20 73 6f 6d 65 20 75 67 6c 69 6e 65 73 73 20  " some ugliness 
2840: 69 6e 20 63 61 73 74 69 6e 67 20 61 6e 20 69 6e  in casting an in
2850: 74 0a 2a 2a 20 76 61 6c 75 65 20 74 6f 20 61 20  t.** value to a 
2860: 70 74 72 20 76 61 6c 75 65 20 75 6e 64 65 72 20  ptr value under 
2870: 74 68 65 20 4d 53 56 43 20 36 34 2d 62 69 74 20  the MSVC 64-bit 
2880: 63 6f 6d 70 69 6c 65 72 2e 20 20 20 43 61 73 74  compiler.   Cast
2890: 69 6e 67 0a 2a 2a 20 6e 6f 6e 20 36 34 2d 62 69  ing.** non 64-bi
28a0: 74 20 76 61 6c 75 65 73 20 74 6f 20 70 74 72 20  t values to ptr 
28b0: 74 79 70 65 73 20 72 65 73 75 6c 74 73 20 69 6e  types results in
28c0: 20 61 20 22 68 61 72 64 22 20 65 72 72 6f 72 20   a "hard" error 
28d0: 77 69 74 68 20 0a 2a 2a 20 74 68 65 20 4d 53 56  with .** the MSV
28e0: 43 20 36 34 2d 62 69 74 20 63 6f 6d 70 69 6c 65  C 64-bit compile
28f0: 72 20 77 68 69 63 68 20 74 68 69 73 20 61 74 74  r which this att
2900: 65 6d 70 74 73 20 74 6f 20 61 76 6f 69 64 2e 20  empts to avoid. 
2910: 20 0a 2a 2a 0a 2a 2a 20 41 20 73 69 6d 70 6c 65   .**.** A simple
2920: 20 63 6f 6d 70 69 6c 65 72 20 70 72 61 67 6d 61   compiler pragma
2930: 20 6f 72 20 63 61 73 74 69 6e 67 20 73 65 71 75   or casting sequ
2940: 65 6e 63 65 20 63 6f 75 6c 64 20 6e 6f 74 20 62  ence could not b
2950: 65 20 66 6f 75 6e 64 0a 2a 2a 20 74 6f 20 63 6f  e found.** to co
2960: 72 72 65 63 74 20 74 68 69 73 20 69 6e 20 61 6c  rrect this in al
2970: 6c 20 73 69 74 75 61 74 69 6f 6e 73 2c 20 73 6f  l situations, so
2980: 20 74 68 69 73 20 6d 61 63 72 6f 20 77 61 73 20   this macro was 
2990: 69 6e 74 72 6f 64 75 63 65 64 2e 0a 2a 2a 0a 2a  introduced..**.*
29a0: 2a 20 49 74 20 63 6f 75 6c 64 20 62 65 20 61 72  * It could be ar
29b0: 67 75 65 64 20 74 68 61 74 20 74 68 65 20 69 6e  gued that the in
29c0: 74 70 74 72 5f 74 20 74 79 70 65 20 63 6f 75 6c  tptr_t type coul
29d0: 64 20 62 65 20 75 73 65 64 20 69 6e 20 74 68 69  d be used in thi
29e0: 73 0a 2a 2a 20 63 61 73 65 2c 20 62 75 74 20 74  s.** case, but t
29f0: 68 61 74 20 74 79 70 65 20 69 73 20 6e 6f 74 20  hat type is not 
2a00: 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 61 6c 6c  available on all
2a10: 20 63 6f 6d 70 69 6c 65 72 73 2c 20 6f 72 20 0a   compilers, or .
2a20: 2a 2a 20 72 65 71 75 69 72 65 73 20 74 68 65 20  ** requires the 
2a30: 23 69 6e 63 6c 75 64 65 20 6f 66 20 73 70 65 63  #include of spec
2a40: 69 66 69 63 20 68 65 61 64 65 72 73 20 77 68 69  ific headers whi
2a50: 63 68 20 64 69 66 66 65 72 73 20 62 65 74 77 65  ch differs betwe
2a60: 65 6e 0a 2a 2a 20 70 6c 61 74 66 6f 72 6d 73 2e  en.** platforms.
2a70: 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74 20 23 33  .**.** Ticket #3
2a80: 38 36 30 3a 20 20 54 68 65 20 6c 6c 76 6d 2d 67  860:  The llvm-g
2a90: 63 63 2d 34 2e 32 20 63 6f 6d 70 69 6c 65 72 20  cc-4.2 compiler 
2aa0: 66 72 6f 6d 20 41 70 70 6c 65 20 63 68 6f 6b 65  from Apple choke
2ab0: 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 28 28 76 6f  s on.** the ((vo
2ac0: 69 64 2a 29 26 28 28 63 68 61 72 2a 29 30 29 5b  id*)&((char*)0)[
2ad0: 58 5d 29 20 63 6f 6e 73 74 72 75 63 74 2e 20 20  X]) construct.  
2ae0: 42 75 74 20 4d 53 56 43 20 63 68 6f 6b 65 73 20  But MSVC chokes 
2af0: 6f 6e 20 28 28 76 6f 69 64 2a 29 28 58 29 29 2e  on ((void*)(X)).
2b00: 0a 2a 2a 20 53 6f 20 77 65 20 68 61 76 65 20 74  .** So we have t
2b10: 6f 20 64 65 66 69 6e 65 20 74 68 65 20 6d 61 63  o define the mac
2b20: 72 6f 73 20 69 6e 20 64 69 66 66 65 72 65 6e 74  ros in different
2b30: 20 77 61 79 73 20 64 65 70 65 6e 64 69 6e 67 20   ways depending 
2b40: 6f 6e 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c  on the.** compil
2b50: 65 72 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e  er..*/.#if defin
2b60: 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 23 20 69  ed(__GNUC__).# i
2b70: 66 20 64 65 66 69 6e 65 64 28 48 41 56 45 5f 53  f defined(HAVE_S
2b80: 54 44 49 4e 54 5f 48 29 0a 23 20 20 20 64 65 66  TDINT_H).#   def
2b90: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54  ine SQLITE_INT_T
2ba0: 4f 5f 50 54 52 28 58 29 20 20 28 28 76 6f 69 64  O_PTR(X)  ((void
2bb0: 2a 29 28 69 6e 74 70 74 72 5f 74 29 28 58 29 29  *)(intptr_t)(X))
2bc0: 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49  .#   define SQLI
2bd0: 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 58 29  TE_PTR_TO_INT(X)
2be0: 20 20 28 28 69 6e 74 29 28 69 6e 74 70 74 72 5f    ((int)(intptr_
2bf0: 74 29 28 58 29 29 0a 23 20 65 6c 73 65 0a 23 20  t)(X)).# else.# 
2c00: 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
2c10: 49 4e 54 5f 54 4f 5f 50 54 52 28 58 29 20 20 28  INT_TO_PTR(X)  (
2c20: 28 76 6f 69 64 2a 29 28 58 29 29 0a 23 20 20 20  (void*)(X)).#   
2c30: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 54  define SQLITE_PT
2c40: 52 5f 54 4f 5f 49 4e 54 28 58 29 20 20 28 28 69  R_TO_INT(X)  ((i
2c50: 6e 74 29 28 58 29 29 0a 23 20 65 6e 64 69 66 0a  nt)(X)).# endif.
2c60: 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53  #else.# define S
2c70: 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52  QLITE_INT_TO_PTR
2c80: 28 58 29 20 20 20 28 28 76 6f 69 64 2a 29 26 28  (X)   ((void*)&(
2c90: 28 63 68 61 72 2a 29 30 29 5b 58 5d 29 0a 23 20  (char*)0)[X]).# 
2ca0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 54  define SQLITE_PT
2cb0: 52 5f 54 4f 5f 49 4e 54 28 58 29 20 20 20 28 28  R_TO_INT(X)   ((
2cc0: 69 6e 74 29 28 28 28 63 68 61 72 2a 29 58 29 2d  int)(((char*)X)-
2cd0: 28 63 68 61 72 2a 29 30 29 29 0a 23 65 6e 64 69  (char*)0)).#endi
2ce0: 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51  f.../*.** The SQ
2cf0: 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20  LITE_THREADSAFE 
2d00: 6d 61 63 72 6f 20 6d 75 73 74 20 62 65 20 64 65  macro must be de
2d10: 66 69 6e 65 64 20 61 73 20 65 69 74 68 65 72 20  fined as either 
2d20: 30 20 6f 72 20 31 2e 0a 2a 2a 20 4f 6c 64 65 72  0 or 1..** Older
2d30: 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c   versions of SQL
2d40: 69 74 65 20 75 73 65 64 20 61 6e 20 6f 70 74 69  ite used an opti
2d50: 6f 6e 61 6c 20 54 48 52 45 41 44 53 41 46 45 20  onal THREADSAFE 
2d60: 6d 61 63 72 6f 2e 0a 2a 2a 20 57 65 20 73 75 70  macro..** We sup
2d70: 70 6f 72 74 20 74 68 61 74 20 66 6f 72 20 6c 65  port that for le
2d80: 67 61 63 79 0a 2a 2f 0a 23 69 66 20 21 64 65 66  gacy.*/.#if !def
2d90: 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 48 52 45  ined(SQLITE_THRE
2da0: 41 44 53 41 46 45 29 0a 23 69 66 20 64 65 66 69  ADSAFE).#if defi
2db0: 6e 65 64 28 54 48 52 45 41 44 53 41 46 45 29 0a  ned(THREADSAFE).
2dc0: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
2dd0: 54 48 52 45 41 44 53 41 46 45 20 54 48 52 45 41  THREADSAFE THREA
2de0: 44 53 41 46 45 0a 23 65 6c 73 65 0a 23 20 64 65  DSAFE.#else.# de
2df0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 48 52 45  fine SQLITE_THRE
2e00: 41 44 53 41 46 45 20 31 0a 23 65 6e 64 69 66 0a  ADSAFE 1.#endif.
2e10: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
2e20: 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  e SQLITE_DEFAULT
2e30: 5f 4d 45 4d 53 54 41 54 55 53 20 6d 61 63 72 6f  _MEMSTATUS macro
2e40: 20 6d 75 73 74 20 62 65 20 64 65 66 69 6e 65 64   must be defined
2e50: 20 61 73 20 65 69 74 68 65 72 20 30 20 6f 72 20   as either 0 or 
2e60: 31 2e 0a 2a 2a 20 49 74 20 64 65 74 65 72 6d 69  1..** It determi
2e70: 6e 65 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e  nes whether or n
2e80: 6f 74 20 74 68 65 20 66 65 61 74 75 72 65 73 20  ot the features 
2e90: 72 65 6c 61 74 65 64 20 74 6f 20 0a 2a 2a 20 53  related to .** S
2ea0: 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d  QLITE_CONFIG_MEM
2eb0: 53 54 41 54 55 53 20 61 72 65 20 61 76 61 69 6c  STATUS are avail
2ec0: 61 62 6c 65 20 62 79 20 64 65 66 61 75 6c 74 20  able by default 
2ed0: 6f 72 20 6e 6f 74 2e 20 54 68 69 73 20 76 61 6c  or not. This val
2ee0: 75 65 20 63 61 6e 0a 2a 2a 20 62 65 20 6f 76 65  ue can.** be ove
2ef0: 72 72 69 64 64 65 6e 20 61 74 20 72 75 6e 74 69  rridden at runti
2f00: 6d 65 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c  me using the sql
2f10: 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 41 50  ite3_config() AP
2f20: 49 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e  I..*/.#if !defin
2f30: 65 64 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  ed(SQLITE_DEFAUL
2f40: 54 5f 4d 45 4d 53 54 41 54 55 53 29 0a 23 20 64  T_MEMSTATUS).# d
2f50: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46  efine SQLITE_DEF
2f60: 41 55 4c 54 5f 4d 45 4d 53 54 41 54 55 53 20 31  AULT_MEMSTATUS 1
2f70: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45  .#endif../*.** E
2f80: 78 61 63 74 6c 79 20 6f 6e 65 20 6f 66 20 74 68  xactly one of th
2f90: 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72  e following macr
2fa0: 6f 73 20 6d 75 73 74 20 62 65 20 64 65 66 69 6e  os must be defin
2fb0: 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a  ed in order to.*
2fc0: 2a 20 73 70 65 63 69 66 79 20 77 68 69 63 68 20  * specify which 
2fd0: 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
2fe0: 6e 20 73 75 62 73 79 73 74 65 6d 20 74 6f 20 75  n subsystem to u
2ff0: 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 51  se..**.**     SQ
3000: 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c  LITE_SYSTEM_MALL
3010: 4f 43 20 20 20 20 20 20 20 20 20 20 2f 2f 20 55  OC          // U
3020: 73 65 20 6e 6f 72 6d 61 6c 20 73 79 73 74 65 6d  se normal system
3030: 20 6d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 20 20 20   malloc().**    
3040: 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47   SQLITE_MEMDEBUG
3050: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3060: 2f 20 44 65 62 75 67 67 69 6e 67 20 76 65 72 73  / Debugging vers
3070: 69 6f 6e 20 6f 66 20 73 79 73 74 65 6d 20 6d 61  ion of system ma
3080: 6c 6c 6f 63 28 29 0a 2a 2a 20 20 20 20 20 53 51  lloc().**     SQ
3090: 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45  LITE_MEMORY_SIZE
30a0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2f 20 69              // i
30b0: 6e 74 65 72 6e 61 6c 20 61 6c 6c 6f 63 61 74 6f  nternal allocato
30c0: 72 20 23 31 0a 2a 2a 20 20 20 20 20 53 51 4c 49  r #1.**     SQLI
30d0: 54 45 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a  TE_MMAP_HEAP_SIZ
30e0: 45 20 20 20 20 20 20 20 20 20 2f 2f 20 69 6e 74  E         // int
30f0: 65 72 6e 61 6c 20 6d 6d 61 70 28 29 20 61 6c 6c  ernal mmap() all
3100: 6f 63 61 74 6f 72 0a 2a 2a 20 20 20 20 20 53 51  ocator.**     SQ
3110: 4c 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59  LITE_POW2_MEMORY
3120: 5f 53 49 5a 45 20 20 20 20 20 20 20 2f 2f 20 69  _SIZE       // i
3130: 6e 74 65 72 6e 61 6c 20 70 6f 77 65 72 2d 6f 66  nternal power-of
3140: 2d 74 77 6f 20 61 6c 6c 6f 63 61 74 6f 72 0a 2a  -two allocator.*
3150: 2a 0a 2a 2a 20 49 66 20 6e 6f 6e 65 20 6f 66 20  *.** If none of 
3160: 74 68 65 20 61 62 6f 76 65 20 61 72 65 20 64 65  the above are de
3170: 66 69 6e 65 64 2c 20 74 68 65 6e 20 73 65 74 20  fined, then set 
3180: 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41  SQLITE_SYSTEM_MA
3190: 4c 4c 4f 43 20 61 73 0a 2a 2a 20 74 68 65 20 64  LLOC as.** the d
31a0: 65 66 61 75 6c 74 2e 0a 2a 2f 0a 23 69 66 20 64  efault..*/.#if d
31b0: 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 53 59  efined(SQLITE_SY
31c0: 53 54 45 4d 5f 4d 41 4c 4c 4f 43 29 2b 64 65 66  STEM_MALLOC)+def
31d0: 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 45 4d 44  ined(SQLITE_MEMD
31e0: 45 42 55 47 29 2b 5c 0a 20 20 20 20 64 65 66 69  EBUG)+\.    defi
31f0: 6e 65 64 28 53 51 4c 49 54 45 5f 4d 45 4d 4f 52  ned(SQLITE_MEMOR
3200: 59 5f 53 49 5a 45 29 2b 64 65 66 69 6e 65 64 28  Y_SIZE)+defined(
3210: 53 51 4c 49 54 45 5f 4d 4d 41 50 5f 48 45 41 50  SQLITE_MMAP_HEAP
3220: 5f 53 49 5a 45 29 2b 5c 0a 20 20 20 20 64 65 66  _SIZE)+\.    def
3230: 69 6e 65 64 28 53 51 4c 49 54 45 5f 50 4f 57 32  ined(SQLITE_POW2
3240: 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 29 3e 31 0a  _MEMORY_SIZE)>1.
3250: 23 20 65 72 72 6f 72 20 22 41 74 20 6d 6f 73 74  # error "At most
3260: 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c   one of the foll
3270: 6f 77 69 6e 67 20 63 6f 6d 70 69 6c 65 2d 74 69  owing compile-ti
3280: 6d 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e  me configuration
3290: 20 6f 70 74 69 6f 6e 73 5c 0a 20 69 73 20 61 6c   options\. is al
32a0: 6c 6f 77 73 3a 20 53 51 4c 49 54 45 5f 53 59 53  lows: SQLITE_SYS
32b0: 54 45 4d 5f 4d 41 4c 4c 4f 43 2c 20 53 51 4c 49  TEM_MALLOC, SQLI
32c0: 54 45 5f 4d 45 4d 44 45 42 55 47 2c 20 53 51 4c  TE_MEMDEBUG, SQL
32d0: 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 2c  ITE_MEMORY_SIZE,
32e0: 5c 0a 20 53 51 4c 49 54 45 5f 4d 4d 41 50 5f 48  \. SQLITE_MMAP_H
32f0: 45 41 50 5f 53 49 5a 45 2c 20 53 51 4c 49 54 45  EAP_SIZE, SQLITE
3300: 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59 5f 53 49 5a  _POW2_MEMORY_SIZ
3310: 45 22 0a 23 65 6e 64 69 66 0a 23 69 66 20 64 65  E".#endif.#if de
3320: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 53 59 53  fined(SQLITE_SYS
3330: 54 45 4d 5f 4d 41 4c 4c 4f 43 29 2b 64 65 66 69  TEM_MALLOC)+defi
3340: 6e 65 64 28 53 51 4c 49 54 45 5f 4d 45 4d 44 45  ned(SQLITE_MEMDE
3350: 42 55 47 29 2b 5c 0a 20 20 20 20 64 65 66 69 6e  BUG)+\.    defin
3360: 65 64 28 53 51 4c 49 54 45 5f 4d 45 4d 4f 52 59  ed(SQLITE_MEMORY
3370: 5f 53 49 5a 45 29 2b 64 65 66 69 6e 65 64 28 53  _SIZE)+defined(S
3380: 51 4c 49 54 45 5f 4d 4d 41 50 5f 48 45 41 50 5f  QLITE_MMAP_HEAP_
3390: 53 49 5a 45 29 2b 5c 0a 20 20 20 20 64 65 66 69  SIZE)+\.    defi
33a0: 6e 65 64 28 53 51 4c 49 54 45 5f 50 4f 57 32 5f  ned(SQLITE_POW2_
33b0: 4d 45 4d 4f 52 59 5f 53 49 5a 45 29 3d 3d 30 0a  MEMORY_SIZE)==0.
33c0: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
33d0: 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 31 0a  SYSTEM_MALLOC 1.
33e0: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66  #endif../*.** If
33f0: 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53   SQLITE_MALLOC_S
3400: 4f 46 54 5f 4c 49 4d 49 54 20 69 73 20 6e 6f 74  OFT_LIMIT is not
3410: 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 72 79 20   zero, then try 
3420: 74 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73  to keep the.** s
3430: 69 7a 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 61  izes of memory a
3440: 6c 6c 6f 63 61 74 69 6f 6e 73 20 62 65 6c 6f 77  llocations below
3450: 20 74 68 69 73 20 76 61 6c 75 65 20 77 68 65 72   this value wher
3460: 65 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2f 0a 23  e possible..*/.#
3470: 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
3480: 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c  TE_MALLOC_SOFT_L
3490: 49 4d 49 54 29 0a 23 20 64 65 66 69 6e 65 20 53  IMIT).# define S
34a0: 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46  QLITE_MALLOC_SOF
34b0: 54 5f 4c 49 4d 49 54 20 31 30 32 34 0a 23 65 6e  T_LIMIT 1024.#en
34c0: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 65 20 6e 65  dif../*.** We ne
34d0: 65 64 20 74 6f 20 64 65 66 69 6e 65 20 5f 58 4f  ed to define _XO
34e0: 50 45 4e 5f 53 4f 55 52 43 45 20 61 73 20 66 6f  PEN_SOURCE as fo
34f0: 6c 6c 6f 77 73 20 69 6e 20 6f 72 64 65 72 20 74  llows in order t
3500: 6f 20 65 6e 61 62 6c 65 0a 2a 2a 20 72 65 63 75  o enable.** recu
3510: 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 6f 6e  rsive mutexes on
3520: 20 6d 6f 73 74 20 55 6e 69 78 20 73 79 73 74 65   most Unix syste
3530: 6d 73 2e 20 20 42 75 74 20 4d 61 63 20 4f 53 20  ms.  But Mac OS 
3540: 58 20 69 73 20 64 69 66 66 65 72 65 6e 74 2e 0a  X is different..
3550: 2a 2a 20 54 68 65 20 5f 58 4f 50 45 4e 5f 53 4f  ** The _XOPEN_SO
3560: 55 52 43 45 20 64 65 66 69 6e 65 20 63 61 75 73  URCE define caus
3570: 65 73 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20  es problems for 
3580: 4d 61 63 20 4f 53 20 58 20 77 65 20 61 72 65 20  Mac OS X we are 
3590: 74 6f 6c 64 2c 0a 2a 2a 20 73 6f 20 69 74 20 69  told,.** so it i
35a0: 73 20 6f 6d 69 74 74 65 64 20 74 68 65 72 65 2e  s omitted there.
35b0: 20 20 53 65 65 20 74 69 63 6b 65 74 20 23 32 36    See ticket #26
35c0: 37 33 2e 0a 2a 2a 0a 2a 2a 20 4c 61 74 65 72 20  73..**.** Later 
35d0: 77 65 20 6c 65 61 72 6e 20 74 68 61 74 20 5f 58  we learn that _X
35e0: 4f 50 45 4e 5f 53 4f 55 52 43 45 20 69 73 20 70  OPEN_SOURCE is p
35f0: 6f 6f 72 6c 79 20 6f 72 20 69 6e 63 6f 72 72 65  oorly or incorre
3600: 63 74 6c 79 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  ctly.** implemen
3610: 74 65 64 20 6f 6e 20 73 6f 6d 65 20 73 79 73 74  ted on some syst
3620: 65 6d 73 2e 20 20 53 6f 20 77 65 20 61 76 6f 69  ems.  So we avoi
3630: 64 20 64 65 66 69 6e 69 6e 67 20 69 74 20 61 74  d defining it at
3640: 20 61 6c 6c 0a 2a 2a 20 69 66 20 69 74 20 69 73   all.** if it is
3650: 20 61 6c 72 65 61 64 79 20 64 65 66 69 6e 65 64   already defined
3660: 20 6f 72 20 69 66 20 69 74 20 69 73 20 75 6e 6e   or if it is unn
3670: 65 65 64 65 64 20 62 65 63 61 75 73 65 20 77 65  eeded because we
3680: 20 61 72 65 0a 2a 2a 20 6e 6f 74 20 64 6f 69 6e   are.** not doin
3690: 67 20 61 20 74 68 72 65 61 64 73 61 66 65 20 62  g a threadsafe b
36a0: 75 69 6c 64 2e 20 20 54 69 63 6b 65 74 20 23 32  uild.  Ticket #2
36b0: 36 38 31 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  681..**.** See a
36c0: 6c 73 6f 20 74 69 63 6b 65 74 20 23 32 37 34 31  lso ticket #2741
36d0: 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65  ..*/.#if !define
36e0: 64 28 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 29  d(_XOPEN_SOURCE)
36f0: 20 26 26 20 21 64 65 66 69 6e 65 64 28 5f 5f 44   && !defined(__D
3700: 41 52 57 49 4e 5f 5f 29 20 26 26 20 21 64 65 66  ARWIN__) && !def
3710: 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20  ined(__APPLE__) 
3720: 26 26 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  && SQLITE_THREAD
3730: 53 41 46 45 0a 23 20 20 64 65 66 69 6e 65 20 5f  SAFE.#  define _
3740: 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 35 30 30  XOPEN_SOURCE 500
3750: 20 20 2f 2a 20 4e 65 65 64 65 64 20 74 6f 20 65    /* Needed to e
3760: 6e 61 62 6c 65 20 70 74 68 72 65 61 64 20 72 65  nable pthread re
3770: 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 20  cursive mutexes 
3780: 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  */.#endif../*.**
3790: 20 54 68 65 20 54 43 4c 20 68 65 61 64 65 72 73   The TCL headers
37a0: 20 61 72 65 20 6f 6e 6c 79 20 6e 65 65 64 65 64   are only needed
37b0: 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20   when compiling 
37c0: 74 68 65 20 54 43 4c 20 62 69 6e 64 69 6e 67 73  the TCL bindings
37d0: 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64  ..*/.#if defined
37e0: 28 53 51 4c 49 54 45 5f 54 43 4c 29 20 7c 7c 20  (SQLITE_TCL) || 
37f0: 64 65 66 69 6e 65 64 28 54 43 4c 53 48 29 0a 23  defined(TCLSH).#
3800: 20 69 6e 63 6c 75 64 65 20 3c 74 63 6c 2e 68 3e   include <tcl.h>
3810: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d  .#endif../*.** M
3820: 61 6e 79 20 70 65 6f 70 6c 65 20 61 72 65 20 66  any people are f
3830: 61 69 6c 69 6e 67 20 74 6f 20 73 65 74 20 2d 44  ailing to set -D
3840: 4e 44 45 42 55 47 3d 31 20 77 68 65 6e 20 63 6f  NDEBUG=1 when co
3850: 6d 70 69 6c 69 6e 67 20 53 51 4c 69 74 65 2e 0a  mpiling SQLite..
3860: 2a 2a 20 53 65 74 74 69 6e 67 20 4e 44 45 42 55  ** Setting NDEBU
3870: 47 20 6d 61 6b 65 73 20 74 68 65 20 63 6f 64 65  G makes the code
3880: 20 73 6d 61 6c 6c 65 72 20 61 6e 64 20 72 75 6e   smaller and run
3890: 20 66 61 73 74 65 72 2e 20 20 53 6f 20 74 68 65   faster.  So the
38a0: 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 6c 69   following.** li
38b0: 6e 65 73 20 61 72 65 20 61 64 64 65 64 20 74 6f  nes are added to
38c0: 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 73   automatically s
38d0: 65 74 20 4e 44 45 42 55 47 20 75 6e 6c 65 73 73  et NDEBUG unless
38e0: 20 74 68 65 20 2d 44 53 51 4c 49 54 45 5f 44 45   the -DSQLITE_DE
38f0: 42 55 47 3d 31 0a 2a 2a 20 6f 70 74 69 6f 6e 20  BUG=1.** option 
3900: 69 73 20 73 65 74 2e 20 20 54 68 75 73 20 4e 44  is set.  Thus ND
3910: 45 42 55 47 20 62 65 63 6f 6d 65 73 20 61 6e 20  EBUG becomes an 
3920: 6f 70 74 2d 69 6e 20 72 61 74 68 65 72 20 74 68  opt-in rather th
3930: 61 6e 20 61 6e 20 6f 70 74 2d 6f 75 74 0a 2a 2a  an an opt-out.**
3940: 20 66 65 61 74 75 72 65 2e 0a 2a 2f 0a 23 69 66   feature..*/.#if
3950: 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47   !defined(NDEBUG
3960: 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
3970: 4c 49 54 45 5f 44 45 42 55 47 29 20 0a 23 20 64  LITE_DEBUG) .# d
3980: 65 66 69 6e 65 20 4e 44 45 42 55 47 20 31 0a 23  efine NDEBUG 1.#
3990: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
39a0: 20 74 65 73 74 63 61 73 65 28 29 20 6d 61 63 72   testcase() macr
39b0: 6f 20 69 73 20 75 73 65 64 20 74 6f 20 61 69 64  o is used to aid
39c0: 20 69 6e 20 63 6f 76 65 72 61 67 65 20 74 65 73   in coverage tes
39d0: 74 69 6e 67 2e 20 20 57 68 65 6e 20 0a 2a 2a 20  ting.  When .** 
39e0: 64 6f 69 6e 67 20 63 6f 76 65 72 61 67 65 20 74  doing coverage t
39f0: 65 73 74 69 6e 67 2c 20 74 68 65 20 63 6f 6e 64  esting, the cond
3a00: 69 74 69 6f 6e 20 69 6e 73 69 64 65 20 74 68 65  ition inside the
3a10: 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20   argument to.** 
3a20: 74 65 73 74 63 61 73 65 28 29 20 6d 75 73 74 20  testcase() must 
3a30: 62 65 20 65 76 61 6c 75 61 74 65 64 20 62 6f 74  be evaluated bot
3a40: 68 20 74 72 75 65 20 61 6e 64 20 66 61 6c 73 65  h true and false
3a50: 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20   in order to.** 
3a60: 67 65 74 20 66 75 6c 6c 20 62 72 61 6e 63 68 20  get full branch 
3a70: 63 6f 76 65 72 61 67 65 2e 20 20 54 68 65 20 74  coverage.  The t
3a80: 65 73 74 63 61 73 65 28 29 20 6d 61 63 72 6f 20  estcase() macro 
3a90: 69 73 20 69 6e 73 65 72 74 65 64 0a 2a 2a 20 74  is inserted.** t
3aa0: 6f 20 68 65 6c 70 20 65 6e 73 75 72 65 20 61 64  o help ensure ad
3ab0: 65 71 75 61 74 65 20 74 65 73 74 20 63 6f 76 65  equate test cove
3ac0: 72 61 67 65 20 69 6e 20 70 6c 61 63 65 73 20 77  rage in places w
3ad0: 68 65 72 65 20 73 69 6d 70 6c 65 0a 2a 2a 20 63  here simple.** c
3ae0: 6f 6e 64 69 74 69 6f 6e 2f 64 65 63 69 73 69 6f  ondition/decisio
3af0: 6e 20 63 6f 76 65 72 61 67 65 20 69 73 20 69 6e  n coverage is in
3b00: 61 64 65 71 75 61 74 65 2e 20 20 46 6f 72 20 65  adequate.  For e
3b10: 78 61 6d 70 6c 65 2c 20 74 65 73 74 63 61 73 65  xample, testcase
3b20: 28 29 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65  ().** can be use
3b30: 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 62  d to make sure b
3b40: 6f 75 6e 64 61 72 79 20 76 61 6c 75 65 73 20 61  oundary values a
3b50: 72 65 20 74 65 73 74 65 64 2e 20 20 46 6f 72 0a  re tested.  For.
3b60: 2a 2a 20 62 69 74 6d 61 73 6b 20 74 65 73 74 73  ** bitmask tests
3b70: 2c 20 74 65 73 74 63 61 73 65 28 29 20 63 61 6e  , testcase() can
3b80: 20 62 65 20 75 73 65 64 20 74 6f 20 6d 61 6b 65   be used to make
3b90: 20 73 75 72 65 20 65 61 63 68 20 62 69 74 0a 2a   sure each bit.*
3ba0: 2a 20 69 73 20 73 69 67 6e 69 66 69 63 61 6e 74  * is significant
3bb0: 20 61 6e 64 20 75 73 65 64 20 61 74 20 6c 65 61   and used at lea
3bc0: 73 74 20 6f 6e 63 65 2e 20 20 4f 6e 20 73 77 69  st once.  On swi
3bd0: 74 63 68 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a  tch statements.*
3be0: 2a 20 77 68 65 72 65 20 6d 75 6c 74 69 70 6c 65  * where multiple
3bf0: 20 63 61 73 65 73 20 67 6f 20 74 6f 20 74 68 65   cases go to the
3c00: 20 73 61 6d 65 20 62 6c 6f 63 6b 20 6f 66 20 63   same block of c
3c10: 6f 64 65 2c 20 74 65 73 74 63 61 73 65 28 29 0a  ode, testcase().
3c20: 2a 2a 20 63 61 6e 20 69 6e 73 75 72 65 20 74 68  ** can insure th
3c30: 61 74 20 61 6c 6c 20 63 61 73 65 73 20 61 72 65  at all cases are
3c40: 20 65 76 61 6c 75 61 74 65 64 2e 0a 2a 2a 0a 2a   evaluated..**.*
3c50: 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
3c60: 43 4f 56 45 52 41 47 45 5f 54 45 53 54 0a 53 51  COVERAGE_TEST.SQ
3c70: 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
3c80: 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 76 65 72  oid sqlite3Cover
3c90: 61 67 65 28 69 6e 74 29 3b 0a 23 20 64 65 66 69  age(int);.# defi
3ca0: 6e 65 20 74 65 73 74 63 61 73 65 28 58 29 20 20  ne testcase(X)  
3cb0: 69 66 28 20 58 20 29 7b 20 73 71 6c 69 74 65 33  if( X ){ sqlite3
3cc0: 43 6f 76 65 72 61 67 65 28 5f 5f 4c 49 4e 45 5f  Coverage(__LINE_
3cd0: 5f 29 3b 20 7d 0a 23 65 6c 73 65 0a 23 20 64 65  _); }.#else.# de
3ce0: 66 69 6e 65 20 74 65 73 74 63 61 73 65 28 58 29  fine testcase(X)
3cf0: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
3d00: 68 65 20 54 45 53 54 4f 4e 4c 59 20 6d 61 63 72  he TESTONLY macr
3d10: 6f 20 69 73 20 75 73 65 64 20 74 6f 20 65 6e 63  o is used to enc
3d20: 6c 6f 73 65 20 76 61 72 69 61 62 6c 65 20 64 65  lose variable de
3d30: 63 6c 61 72 61 74 69 6f 6e 73 20 6f 72 0a 2a 2a  clarations or.**
3d40: 20 6f 74 68 65 72 20 62 69 74 73 20 6f 66 20 63   other bits of c
3d50: 6f 64 65 20 74 68 61 74 20 61 72 65 20 6e 65 65  ode that are nee
3d60: 64 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20 74  ded to support t
3d70: 68 65 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20  he arguments.** 
3d80: 77 69 74 68 69 6e 20 74 65 73 74 63 61 73 65 28  within testcase(
3d90: 29 20 61 6e 64 20 61 73 73 65 72 74 28 29 20 6d  ) and assert() m
3da0: 61 63 72 6f 73 2e 0a 2a 2f 0a 23 69 66 20 21 64  acros..*/.#if !d
3db0: 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c  efined(NDEBUG) |
3dc0: 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
3dd0: 5f 43 4f 56 45 52 41 47 45 5f 54 45 53 54 29 0a  _COVERAGE_TEST).
3de0: 23 20 64 65 66 69 6e 65 20 54 45 53 54 4f 4e 4c  # define TESTONL
3df0: 59 28 58 29 20 20 58 0a 23 65 6c 73 65 0a 23 20  Y(X)  X.#else.# 
3e00: 64 65 66 69 6e 65 20 54 45 53 54 4f 4e 4c 59 28  define TESTONLY(
3e10: 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  X).#endif../*.**
3e20: 20 53 6f 6d 65 74 69 6d 65 73 20 77 65 20 6e 65   Sometimes we ne
3e30: 65 64 20 61 20 73 6d 61 6c 6c 20 61 6d 6f 75 6e  ed a small amoun
3e40: 74 20 6f 66 20 63 6f 64 65 20 73 75 63 68 20 61  t of code such a
3e50: 73 20 61 20 76 61 72 69 61 62 6c 65 20 69 6e 69  s a variable ini
3e60: 74 69 61 6c 69 7a 61 74 69 6f 6e 0a 2a 2a 20 74  tialization.** t
3e70: 6f 20 73 65 74 75 70 20 66 6f 72 20 61 20 6c 61  o setup for a la
3e80: 74 65 72 20 61 73 73 65 72 74 28 29 20 73 74 61  ter assert() sta
3e90: 74 65 6d 65 6e 74 2e 20 20 57 65 20 64 6f 20 6e  tement.  We do n
3ea0: 6f 74 20 77 61 6e 74 20 74 68 69 73 20 63 6f 64  ot want this cod
3eb0: 65 20 74 6f 0a 2a 2a 20 61 70 70 65 61 72 20 77  e to.** appear w
3ec0: 68 65 6e 20 61 73 73 65 72 74 28 29 20 69 73 20  hen assert() is 
3ed0: 64 69 73 61 62 6c 65 64 2e 20 20 54 68 65 20 66  disabled.  The f
3ee0: 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 20 69  ollowing macro i
3ef0: 73 20 74 68 65 72 65 66 6f 72 65 0a 2a 2a 20 75  s therefore.** u
3f00: 73 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74  sed to contain t
3f10: 68 61 74 20 73 65 74 75 70 20 63 6f 64 65 2e 20  hat setup code. 
3f20: 20 54 68 65 20 22 56 56 41 22 20 61 63 72 6f 6e   The "VVA" acron
3f30: 79 6d 20 73 74 61 6e 64 73 20 66 6f 72 0a 2a 2a  ym stands for.**
3f40: 20 22 56 65 72 69 66 69 63 61 74 69 6f 6e 2c 20   "Verification, 
3f50: 56 61 6c 69 64 61 74 69 6f 6e 2c 20 61 6e 64 20  Validation, and 
3f60: 41 63 63 72 65 64 69 74 61 74 69 6f 6e 22 2e 20  Accreditation". 
3f70: 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
3f80: 20 74 68 65 0a 2a 2a 20 63 6f 64 65 20 77 69 74   the.** code wit
3f90: 68 69 6e 20 56 56 41 5f 4f 4e 4c 59 28 29 20 77  hin VVA_ONLY() w
3fa0: 69 6c 6c 20 6f 6e 6c 79 20 72 75 6e 20 64 75 72  ill only run dur
3fb0: 69 6e 67 20 76 65 72 69 66 69 63 61 74 69 6f 6e  ing verification
3fc0: 20 70 72 6f 63 65 73 73 65 73 2e 0a 2a 2f 0a 23   processes..*/.#
3fd0: 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 23 20  ifndef NDEBUG.# 
3fe0: 64 65 66 69 6e 65 20 56 56 41 5f 4f 4e 4c 59 28  define VVA_ONLY(
3ff0: 58 29 20 20 58 0a 23 65 6c 73 65 0a 23 20 64 65  X)  X.#else.# de
4000: 66 69 6e 65 20 56 56 41 5f 4f 4e 4c 59 28 58 29  fine VVA_ONLY(X)
4010: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
4020: 68 65 20 41 4c 57 41 59 53 20 61 6e 64 20 4e 45  he ALWAYS and NE
4030: 56 45 52 20 6d 61 63 72 6f 73 20 73 75 72 72 6f  VER macros surro
4040: 75 6e 64 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72  und boolean expr
4050: 65 73 73 69 6f 6e 73 20 77 68 69 63 68 20 0a 2a  essions which .*
4060: 2a 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 74  * are intended t
4070: 6f 20 61 6c 77 61 79 73 20 62 65 20 74 72 75 65  o always be true
4080: 20 6f 72 20 66 61 6c 73 65 2c 20 72 65 73 70 65   or false, respe
4090: 63 74 69 76 65 6c 79 2e 20 20 53 75 63 68 0a 2a  ctively.  Such.*
40a0: 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 63 6f  * expressions co
40b0: 75 6c 64 20 62 65 20 6f 6d 69 74 74 65 64 20 66  uld be omitted f
40c0: 72 6f 6d 20 74 68 65 20 63 6f 64 65 20 63 6f 6d  rom the code com
40d0: 70 6c 65 74 65 6c 79 2e 20 20 42 75 74 20 74 68  pletely.  But th
40e0: 65 79 0a 2a 2a 20 61 72 65 20 69 6e 63 6c 75 64  ey.** are includ
40f0: 65 64 20 69 6e 20 61 20 66 65 77 20 63 61 73 65  ed in a few case
4100: 73 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 65 6e  s in order to en
4110: 68 61 6e 63 65 20 74 68 65 20 72 65 73 69 6c 69  hance the resili
4120: 65 6e 63 65 0a 2a 2a 20 6f 66 20 53 51 4c 69 74  ence.** of SQLit
4130: 65 20 74 6f 20 75 6e 65 78 70 65 63 74 65 64 20  e to unexpected 
4140: 62 65 68 61 76 69 6f 72 20 2d 20 74 6f 20 6d 61  behavior - to ma
4150: 6b 65 20 74 68 65 20 63 6f 64 65 20 22 73 65 6c  ke the code "sel
4160: 66 2d 68 65 61 6c 69 6e 67 22 0a 2a 2a 20 6f 72  f-healing".** or
4170: 20 22 64 75 63 74 69 6c 65 22 20 72 61 74 68 65   "ductile" rathe
4180: 72 20 74 68 61 6e 20 62 65 69 6e 67 20 22 62 72  r than being "br
4190: 69 74 74 6c 65 22 20 61 6e 64 20 63 72 61 73 68  ittle" and crash
41a0: 69 6e 67 20 61 74 20 74 68 65 20 66 69 72 73 74  ing at the first
41b0: 0a 2a 2a 20 68 69 6e 74 20 6f 66 20 75 6e 70 6c  .** hint of unpl
41c0: 61 6e 6e 65 64 20 62 65 68 61 76 69 6f 72 2e 0a  anned behavior..
41d0: 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77  **.** In other w
41e0: 6f 72 64 73 2c 20 41 4c 57 41 59 53 20 61 6e 64  ords, ALWAYS and
41f0: 20 4e 45 56 45 52 20 61 72 65 20 61 64 64 65 64   NEVER are added
4200: 20 66 6f 72 20 64 65 66 65 6e 73 69 76 65 20 63   for defensive c
4210: 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  ode..**.** When 
4220: 64 6f 69 6e 67 20 63 6f 76 65 72 61 67 65 20 74  doing coverage t
4230: 65 73 74 69 6e 67 20 41 4c 57 41 59 53 20 61 6e  esting ALWAYS an
4240: 64 20 4e 45 56 45 52 20 61 72 65 20 68 61 72 64  d NEVER are hard
4250: 2d 63 6f 64 65 64 20 74 6f 0a 2a 2a 20 62 65 20  -coded to.** be 
4260: 74 72 75 65 20 61 6e 64 20 66 61 6c 73 65 20 73  true and false s
4270: 6f 20 74 68 61 74 20 74 68 65 20 75 6e 72 65 61  o that the unrea
4280: 63 68 61 62 6c 65 20 63 6f 64 65 20 74 68 65 6e  chable code then
4290: 20 73 70 65 63 69 66 79 20 77 69 6c 6c 0a 2a 2a   specify will.**
42a0: 20 6e 6f 74 20 62 65 20 63 6f 75 6e 74 65 64 20   not be counted 
42b0: 61 73 20 75 6e 74 65 73 74 65 64 20 63 6f 64 65  as untested code
42c0: 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64  ..*/.#if defined
42d0: 28 53 51 4c 49 54 45 5f 43 4f 56 45 52 41 47 45  (SQLITE_COVERAGE
42e0: 5f 54 45 53 54 29 0a 23 20 64 65 66 69 6e 65 20  _TEST).# define 
42f0: 41 4c 57 41 59 53 28 58 29 20 20 20 20 20 20 28  ALWAYS(X)      (
4300: 31 29 0a 23 20 64 65 66 69 6e 65 20 4e 45 56 45  1).# define NEVE
4310: 52 28 58 29 20 20 20 20 20 20 20 28 30 29 0a 23  R(X)       (0).#
4320: 65 6c 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44  elif !defined(ND
4330: 45 42 55 47 29 0a 23 20 64 65 66 69 6e 65 20 41  EBUG).# define A
4340: 4c 57 41 59 53 28 58 29 20 20 20 20 20 20 28 28  LWAYS(X)      ((
4350: 58 29 3f 31 3a 28 61 73 73 65 72 74 28 30 29 2c  X)?1:(assert(0),
4360: 30 29 29 0a 23 20 64 65 66 69 6e 65 20 4e 45 56  0)).# define NEV
4370: 45 52 28 58 29 20 20 20 20 20 20 20 28 28 58 29  ER(X)       ((X)
4380: 3f 28 61 73 73 65 72 74 28 30 29 2c 31 29 3a 30  ?(assert(0),1):0
4390: 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  ).#else.# define
43a0: 20 41 4c 57 41 59 53 28 58 29 20 20 20 20 20 20   ALWAYS(X)      
43b0: 28 58 29 0a 23 20 64 65 66 69 6e 65 20 4e 45 56  (X).# define NEV
43c0: 45 52 28 58 29 20 20 20 20 20 20 20 28 58 29 0a  ER(X)       (X).
43d0: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
43e0: 65 20 6d 61 63 72 6f 20 75 6e 6c 69 6b 65 6c 79  e macro unlikely
43f0: 28 29 20 69 73 20 61 20 68 69 6e 74 20 74 68 61  () is a hint tha
4400: 74 20 73 75 72 72 6f 75 6e 64 73 20 61 20 62 6f  t surrounds a bo
4410: 6f 6c 65 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73  olean.** express
4420: 69 6f 6e 20 74 68 61 74 20 69 73 20 75 73 75 61  ion that is usua
4430: 6c 6c 79 20 66 61 6c 73 65 2e 20 20 4d 61 63 72  lly false.  Macr
4440: 6f 20 6c 69 6b 65 6c 79 28 29 20 73 75 72 72 6f  o likely() surro
4450: 75 6e 64 73 0a 2a 2a 20 61 20 62 6f 6f 6c 65 61  unds.** a boolea
4460: 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61  n expression tha
4470: 74 20 69 73 20 75 73 75 61 6c 6c 79 20 74 72 75  t is usually tru
4480: 65 2e 20 20 47 43 43 20 69 73 20 61 62 6c 65 20  e.  GCC is able 
4490: 74 6f 0a 2a 2a 20 75 73 65 20 74 68 65 73 65 20  to.** use these 
44a0: 68 69 6e 74 73 20 74 6f 20 67 65 6e 65 72 61 74  hints to generat
44b0: 65 20 62 65 74 74 65 72 20 63 6f 64 65 2c 20 73  e better code, s
44c0: 6f 6d 65 74 69 6d 65 73 2e 0a 2a 2f 0a 23 69 66  ometimes..*/.#if
44d0: 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f   defined(__GNUC_
44e0: 5f 29 20 26 26 20 30 0a 23 20 64 65 66 69 6e 65  _) && 0.# define
44f0: 20 6c 69 6b 65 6c 79 28 58 29 20 20 20 20 5f 5f   likely(X)    __
4500: 62 75 69 6c 74 69 6e 5f 65 78 70 65 63 74 28 28  builtin_expect((
4510: 58 29 2c 31 29 0a 23 20 64 65 66 69 6e 65 20 75  X),1).# define u
4520: 6e 6c 69 6b 65 6c 79 28 58 29 20 20 5f 5f 62 75  nlikely(X)  __bu
4530: 69 6c 74 69 6e 5f 65 78 70 65 63 74 28 28 58 29  iltin_expect((X)
4540: 2c 30 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  ,0).#else.# defi
4550: 6e 65 20 6c 69 6b 65 6c 79 28 58 29 20 20 20 20  ne likely(X)    
4560: 21 21 28 58 29 0a 23 20 64 65 66 69 6e 65 20 75  !!(X).# define u
4570: 6e 6c 69 6b 65 6c 79 28 58 29 20 20 21 21 28 58  nlikely(X)  !!(X
4580: 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a  ).#endif../*****
4590: 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64  ********* Includ
45a0: 65 20 73 71 6c 69 74 65 33 2e 68 20 69 6e 20 74  e sqlite3.h in t
45b0: 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c  he middle of sql
45c0: 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  iteInt.h *******
45d0: 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
45e0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
45f0: 66 69 6c 65 20 73 71 6c 69 74 65 33 2e 68 20 2a  file sqlite3.h *
4600: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4610: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4620: 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
4630: 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
4640: 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
4650: 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
4660: 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
4670: 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
4680: 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
4690: 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
46a0: 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
46b0: 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
46c0: 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
46d0: 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
46e0: 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
46f0: 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
4700: 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
4710: 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
4720: 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
4730: 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
4740: 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
4750: 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
4760: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4770: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4780: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4790: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
47a0: 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20  .** This header 
47b0: 66 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65  file defines the
47c0: 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20   interface that 
47d0: 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61  the SQLite libra
47e0: 72 79 0a 2a 2a 20 70 72 65 73 65 6e 74 73 20 74  ry.** presents t
47f0: 6f 20 63 6c 69 65 6e 74 20 70 72 6f 67 72 61 6d  o client program
4800: 73 2e 20 20 49 66 20 61 20 43 2d 66 75 6e 63 74  s.  If a C-funct
4810: 69 6f 6e 2c 20 73 74 72 75 63 74 75 72 65 2c 20  ion, structure, 
4820: 64 61 74 61 74 79 70 65 2c 0a 2a 2a 20 6f 72 20  datatype,.** or 
4830: 63 6f 6e 73 74 61 6e 74 20 64 65 66 69 6e 69 74  constant definit
4840: 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 61 70 70  ion does not app
4850: 65 61 72 20 69 6e 20 74 68 69 73 20 66 69 6c 65  ear in this file
4860: 2c 20 74 68 65 6e 20 69 74 20 69 73 0a 2a 2a 20  , then it is.** 
4870: 6e 6f 74 20 61 20 70 75 62 6c 69 73 68 65 64 20  not a published 
4880: 41 50 49 20 6f 66 20 53 51 4c 69 74 65 2c 20 69  API of SQLite, i
4890: 73 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61  s subject to cha
48a0: 6e 67 65 20 77 69 74 68 6f 75 74 0a 2a 2a 20 6e  nge without.** n
48b0: 6f 74 69 63 65 2c 20 61 6e 64 20 73 68 6f 75 6c  otice, and shoul
48c0: 64 20 6e 6f 74 20 62 65 20 72 65 66 65 72 65 6e  d not be referen
48d0: 63 65 64 20 62 79 20 70 72 6f 67 72 61 6d 73 20  ced by programs 
48e0: 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 2e  that use SQLite.
48f0: 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 66 20 74  .**.** Some of t
4900: 68 65 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 74  he definitions t
4910: 68 61 74 20 61 72 65 20 69 6e 20 74 68 69 73 20  hat are in this 
4920: 66 69 6c 65 20 61 72 65 20 6d 61 72 6b 65 64 20  file are marked 
4930: 61 73 0a 2a 2a 20 22 65 78 70 65 72 69 6d 65 6e  as.** "experimen
4940: 74 61 6c 22 2e 20 20 45 78 70 65 72 69 6d 65 6e  tal".  Experimen
4950: 74 61 6c 20 69 6e 74 65 72 66 61 63 65 73 20 61  tal interfaces a
4960: 72 65 20 6e 6f 72 6d 61 6c 6c 79 20 6e 65 77 0a  re normally new.
4970: 2a 2a 20 66 65 61 74 75 72 65 73 20 72 65 63 65  ** features rece
4980: 6e 74 6c 79 20 61 64 64 65 64 20 74 6f 20 53 51  ntly added to SQ
4990: 4c 69 74 65 2e 20 20 57 65 20 64 6f 20 6e 6f 74  Lite.  We do not
49a0: 20 61 6e 74 69 63 69 70 61 74 65 20 63 68 61 6e   anticipate chan
49b0: 67 65 73 0a 2a 2a 20 74 6f 20 65 78 70 65 72 69  ges.** to experi
49c0: 6d 65 6e 74 61 6c 20 69 6e 74 65 72 66 61 63 65  mental interface
49d0: 73 20 62 75 74 20 72 65 73 65 72 76 65 20 74 68  s but reserve th
49e0: 65 20 72 69 67 68 74 20 74 6f 20 6d 61 6b 65 20  e right to make 
49f0: 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 0a 2a 2a  minor changes.**
4a00: 20 69 66 20 65 78 70 65 72 69 65 6e 63 65 20 66   if experience f
4a10: 72 6f 6d 20 75 73 65 20 22 69 6e 20 74 68 65 20  rom use "in the 
4a20: 77 69 6c 64 22 20 73 75 67 67 65 73 74 20 73 75  wild" suggest su
4a30: 63 68 20 63 68 61 6e 67 65 73 20 61 72 65 20 70  ch changes are p
4a40: 72 75 64 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  rudent..**.** Th
4a50: 65 20 6f 66 66 69 63 69 61 6c 20 43 2d 6c 61 6e  e official C-lan
4a60: 67 75 61 67 65 20 41 50 49 20 64 6f 63 75 6d 65  guage API docume
4a70: 6e 74 61 74 69 6f 6e 20 66 6f 72 20 53 51 4c 69  ntation for SQLi
4a80: 74 65 20 69 73 20 64 65 72 69 76 65 64 0a 2a 2a  te is derived.**
4a90: 20 66 72 6f 6d 20 63 6f 6d 6d 65 6e 74 73 20 69   from comments i
4aa0: 6e 20 74 68 69 73 20 66 69 6c 65 2e 20 20 54 68  n this file.  Th
4ab0: 69 73 20 66 69 6c 65 20 69 73 20 74 68 65 20 61  is file is the a
4ac0: 75 74 68 6f 72 69 74 61 74 69 76 65 20 73 6f 75  uthoritative sou
4ad0: 72 63 65 0a 2a 2a 20 6f 6e 20 68 6f 77 20 53 51  rce.** on how SQ
4ae0: 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 73 20  Lite interfaces 
4af0: 61 72 65 20 73 75 70 70 6f 73 65 20 74 6f 20 6f  are suppose to o
4b00: 70 65 72 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  perate..**.** Th
4b10: 65 20 6e 61 6d 65 20 6f 66 20 74 68 69 73 20 66  e name of this f
4b20: 69 6c 65 20 75 6e 64 65 72 20 63 6f 6e 66 69 67  ile under config
4b30: 75 72 61 74 69 6f 6e 20 6d 61 6e 61 67 65 6d 65  uration manageme
4b40: 6e 74 20 69 73 20 22 73 71 6c 69 74 65 2e 68 2e  nt is "sqlite.h.
4b50: 69 6e 22 2e 0a 2a 2a 20 54 68 65 20 6d 61 6b 65  in"..** The make
4b60: 66 69 6c 65 20 6d 61 6b 65 73 20 73 6f 6d 65 20  file makes some 
4b70: 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 20 74 6f  minor changes to
4b80: 20 74 68 69 73 20 66 69 6c 65 20 28 73 75 63 68   this file (such
4b90: 20 61 73 20 69 6e 73 65 72 74 69 6e 67 0a 2a 2a   as inserting.**
4ba0: 20 74 68 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d   the version num
4bb0: 62 65 72 29 20 61 6e 64 20 63 68 61 6e 67 65 73  ber) and changes
4bc0: 20 69 74 73 20 6e 61 6d 65 20 74 6f 20 22 73 71   its name to "sq
4bd0: 6c 69 74 65 33 2e 68 22 20 61 73 0a 2a 2a 20 70  lite3.h" as.** p
4be0: 61 72 74 20 6f 66 20 74 68 65 20 62 75 69 6c 64  art of the build
4bf0: 20 70 72 6f 63 65 73 73 2e 0a 2a 2f 0a 23 69 66   process..*/.#if
4c00: 6e 64 65 66 20 5f 53 51 4c 49 54 45 33 5f 48 5f  ndef _SQLITE3_H_
4c10: 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45  .#define _SQLITE
4c20: 33 5f 48 5f 0a 23 69 6e 63 6c 75 64 65 20 3c 73  3_H_.#include <s
4c30: 74 64 61 72 67 2e 68 3e 20 20 20 20 20 2f 2a 20  tdarg.h>     /* 
4c40: 4e 65 65 64 65 64 20 66 6f 72 20 74 68 65 20 64  Needed for the d
4c50: 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 76 61 5f  efinition of va_
4c60: 6c 69 73 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d  list */../*.** M
4c70: 61 6b 65 20 73 75 72 65 20 77 65 20 63 61 6e 20  ake sure we can 
4c80: 63 61 6c 6c 20 74 68 69 73 20 73 74 75 66 66 20  call this stuff 
4c90: 66 72 6f 6d 20 43 2b 2b 2e 0a 2a 2f 0a 23 69 66  from C++..*/.#if
4ca0: 20 30 0a 65 78 74 65 72 6e 20 22 43 22 20 7b 0a   0.extern "C" {.
4cb0: 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41  #endif.../*.** A
4cc0: 64 64 20 74 68 65 20 61 62 69 6c 69 74 79 20 74  dd the ability t
4cd0: 6f 20 6f 76 65 72 72 69 64 65 20 27 65 78 74 65  o override 'exte
4ce0: 72 6e 27 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  rn'.*/.#ifndef S
4cf0: 51 4c 49 54 45 5f 45 58 54 45 52 4e 0a 23 20 64  QLITE_EXTERN.# d
4d00: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 58 54  efine SQLITE_EXT
4d10: 45 52 4e 20 65 78 74 65 72 6e 0a 23 65 6e 64 69  ERN extern.#endi
4d20: 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  f..#ifndef SQLIT
4d30: 45 5f 41 50 49 0a 23 20 64 65 66 69 6e 65 20 53  E_API.# define S
4d40: 51 4c 49 54 45 5f 41 50 49 0a 23 65 6e 64 69 66  QLITE_API.#endif
4d50: 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 6e  .../*.** These n
4d60: 6f 2d 6f 70 20 6d 61 63 72 6f 73 20 61 72 65 20  o-op macros are 
4d70: 75 73 65 64 20 69 6e 20 66 72 6f 6e 74 20 6f 66  used in front of
4d80: 20 69 6e 74 65 72 66 61 63 65 73 20 74 6f 20 6d   interfaces to m
4d90: 61 72 6b 20 74 68 6f 73 65 0a 2a 2a 20 69 6e 74  ark those.** int
4da0: 65 72 66 61 63 65 73 20 61 73 20 65 69 74 68 65  erfaces as eithe
4db0: 72 20 64 65 70 72 65 63 61 74 65 64 20 6f 72 20  r deprecated or 
4dc0: 65 78 70 65 72 69 6d 65 6e 74 61 6c 2e 20 20 4e  experimental.  N
4dd0: 65 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a  ew applications.
4de0: 2a 2a 20 73 68 6f 75 6c 64 20 6e 6f 74 20 75 73  ** should not us
4df0: 65 20 64 65 70 72 65 63 61 74 65 64 20 69 6e 74  e deprecated int
4e00: 65 72 66 61 63 65 73 20 2d 20 74 68 65 79 20 61  erfaces - they a
4e10: 72 65 20 73 75 70 70 6f 72 74 20 66 6f 72 20 62  re support for b
4e20: 61 63 6b 77 61 72 64 73 0a 2a 2a 20 63 6f 6d 70  ackwards.** comp
4e30: 61 74 69 62 69 6c 69 74 79 20 6f 6e 6c 79 2e 20  atibility only. 
4e40: 20 41 70 70 6c 69 63 61 74 69 6f 6e 20 77 72 69   Application wri
4e50: 74 65 72 73 20 73 68 6f 75 6c 64 20 62 65 20 61  ters should be a
4e60: 77 61 72 65 20 74 68 61 74 0a 2a 2a 20 65 78 70  ware that.** exp
4e70: 65 72 69 6d 65 6e 74 61 6c 20 69 6e 74 65 72 66  erimental interf
4e80: 61 63 65 73 20 61 72 65 20 73 75 62 6a 65 63 74  aces are subject
4e90: 20 74 6f 20 63 68 61 6e 67 65 20 69 6e 20 70 6f   to change in po
4ea0: 69 6e 74 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a  int releases..**
4eb0: 0a 2a 2a 20 54 68 65 73 65 20 6d 61 63 72 6f 73  .** These macros
4ec0: 20 75 73 65 64 20 74 6f 20 72 65 73 6f 6c 76 65   used to resolve
4ed0: 20 74 6f 20 76 61 72 69 6f 75 73 20 6b 69 6e 64   to various kind
4ee0: 73 20 6f 66 20 63 6f 6d 70 69 6c 65 72 20 6d 61  s of compiler ma
4ef0: 67 69 63 20 74 68 61 74 0a 2a 2a 20 77 6f 75 6c  gic that.** woul
4f00: 64 20 67 65 6e 65 72 61 74 65 20 77 61 72 6e 69  d generate warni
4f10: 6e 67 20 6d 65 73 73 61 67 65 73 20 77 68 65 6e  ng messages when
4f20: 20 74 68 65 79 20 77 65 72 65 20 75 73 65 64 2e   they were used.
4f30: 20 20 42 75 74 20 74 68 61 74 0a 2a 2a 20 63 6f    But that.** co
4f40: 6d 70 69 6c 65 72 20 6d 61 67 69 63 20 65 6e 64  mpiler magic end
4f50: 65 64 20 75 70 20 67 65 6e 65 72 61 74 69 6e 67  ed up generating
4f60: 20 73 75 63 68 20 61 20 66 6c 75 72 72 79 20 6f   such a flurry o
4f70: 66 20 62 75 67 20 72 65 70 6f 72 74 73 0a 2a 2a  f bug reports.**
4f80: 20 74 68 61 74 20 77 65 20 68 61 76 65 20 74 61   that we have ta
4f90: 6b 65 6e 20 69 74 20 61 6c 6c 20 6f 75 74 20 61  ken it all out a
4fa0: 6e 64 20 67 6f 6e 65 20 62 61 63 6b 20 74 6f 20  nd gone back to 
4fb0: 75 73 69 6e 67 20 73 69 6d 70 6c 65 0a 2a 2a 20  using simple.** 
4fc0: 6e 6f 6f 70 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a  noop macros..*/.
4fd0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44  #define SQLITE_D
4fe0: 45 50 52 45 43 41 54 45 44 0a 23 64 65 66 69 6e  EPRECATED.#defin
4ff0: 65 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d  e SQLITE_EXPERIM
5000: 45 4e 54 41 4c 0a 0a 2f 2a 0a 2a 2a 20 45 6e 73  ENTAL../*.** Ens
5010: 75 72 65 20 74 68 65 73 65 20 73 79 6d 62 6f 6c  ure these symbol
5020: 73 20 77 65 72 65 20 6e 6f 74 20 64 65 66 69 6e  s were not defin
5030: 65 64 20 62 79 20 73 6f 6d 65 20 70 72 65 76 69  ed by some previ
5040: 6f 75 73 20 68 65 61 64 65 72 20 66 69 6c 65 2e  ous header file.
5050: 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
5060: 45 5f 56 45 52 53 49 4f 4e 0a 23 20 75 6e 64 65  E_VERSION.# unde
5070: 66 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e  f SQLITE_VERSION
5080: 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
5090: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55  QLITE_VERSION_NU
50a0: 4d 42 45 52 0a 23 20 75 6e 64 65 66 20 53 51 4c  MBER.# undef SQL
50b0: 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42  ITE_VERSION_NUMB
50c0: 45 52 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  ER.#endif../*.**
50d0: 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6d 70 69   CAPI3REF: Compi
50e0: 6c 65 2d 54 69 6d 65 20 4c 69 62 72 61 72 79 20  le-Time Library 
50f0: 56 65 72 73 69 6f 6e 20 4e 75 6d 62 65 72 73 20  Version Numbers 
5100: 7b 48 31 30 30 31 30 7d 20 3c 53 36 30 31 30 30  {H10010} <S60100
5110: 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49  >.**.** The SQLI
5120: 54 45 5f 56 45 52 53 49 4f 4e 20 61 6e 64 20 53  TE_VERSION and S
5130: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55  QLITE_VERSION_NU
5140: 4d 42 45 52 20 23 64 65 66 69 6e 65 73 20 69 6e  MBER #defines in
5150: 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 2e  .** the sqlite3.
5160: 68 20 66 69 6c 65 20 73 70 65 63 69 66 79 20 74  h file specify t
5170: 68 65 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51  he version of SQ
5180: 4c 69 74 65 20 77 69 74 68 20 77 68 69 63 68 0a  Lite with which.
5190: 2a 2a 20 74 68 61 74 20 68 65 61 64 65 72 20 66  ** that header f
51a0: 69 6c 65 20 69 73 20 61 73 73 6f 63 69 61 74 65  ile is associate
51b0: 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 76 65  d..**.** The "ve
51c0: 72 73 69 6f 6e 22 20 6f 66 20 53 51 4c 69 74 65  rsion" of SQLite
51d0: 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f 66 20   is a string of 
51e0: 74 68 65 20 66 6f 72 6d 20 22 57 2e 58 2e 59 22  the form "W.X.Y"
51f0: 20 6f 72 20 22 57 2e 58 2e 59 2e 5a 22 2e 0a 2a   or "W.X.Y.Z"..*
5200: 2a 20 54 68 65 20 57 20 76 61 6c 75 65 20 69 73  * The W value is
5210: 20 6d 61 6a 6f 72 20 76 65 72 73 69 6f 6e 20 6e   major version n
5220: 75 6d 62 65 72 20 61 6e 64 20 69 73 20 61 6c 77  umber and is alw
5230: 61 79 73 20 33 20 69 6e 20 53 51 4c 69 74 65 33  ays 3 in SQLite3
5240: 2e 0a 2a 2a 20 54 68 65 20 57 20 76 61 6c 75 65  ..** The W value
5250: 20 6f 6e 6c 79 20 63 68 61 6e 67 65 73 20 77 68   only changes wh
5260: 65 6e 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d  en backwards com
5270: 70 61 74 69 62 69 6c 69 74 79 20 69 73 0a 2a 2a  patibility is.**
5280: 20 62 72 6f 6b 65 6e 20 61 6e 64 20 77 65 20 69   broken and we i
5290: 6e 74 65 6e 64 20 74 6f 20 6e 65 76 65 72 20 62  ntend to never b
52a0: 72 65 61 6b 20 62 61 63 6b 77 61 72 64 73 20 63  reak backwards c
52b0: 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a 2a 2a  ompatibility..**
52c0: 20 54 68 65 20 58 20 76 61 6c 75 65 20 69 73 20   The X value is 
52d0: 74 68 65 20 6d 69 6e 6f 72 20 76 65 72 73 69 6f  the minor versio
52e0: 6e 20 6e 75 6d 62 65 72 20 61 6e 64 20 6f 6e 6c  n number and onl
52f0: 79 20 63 68 61 6e 67 65 73 20 77 68 65 6e 0a 2a  y changes when.*
5300: 2a 20 74 68 65 72 65 20 61 72 65 20 6d 61 6a 6f  * there are majo
5310: 72 20 66 65 61 74 75 72 65 20 65 6e 68 61 6e 63  r feature enhanc
5320: 65 6d 65 6e 74 73 20 74 68 61 74 20 61 72 65 20  ements that are 
5330: 66 6f 72 77 61 72 64 73 20 63 6f 6d 70 61 74 69  forwards compati
5340: 62 6c 65 0a 2a 2a 20 62 75 74 20 6e 6f 74 20 62  ble.** but not b
5350: 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69  ackwards compati
5360: 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 59 20 76 61  ble..** The Y va
5370: 6c 75 65 20 69 73 20 74 68 65 20 72 65 6c 65 61  lue is the relea
5380: 73 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 69 73  se number and is
5390: 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 69 74   incremented wit
53a0: 68 0a 2a 2a 20 65 61 63 68 20 72 65 6c 65 61 73  h.** each releas
53b0: 65 20 62 75 74 20 72 65 73 65 74 73 20 62 61 63  e but resets bac
53c0: 6b 20 74 6f 20 30 20 77 68 65 6e 65 76 65 72 20  k to 0 whenever 
53d0: 58 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64  X is incremented
53e0: 2e 0a 2a 2a 20 54 68 65 20 5a 20 76 61 6c 75 65  ..** The Z value
53f0: 20 6f 6e 6c 79 20 61 70 70 65 61 72 73 20 6f 6e   only appears on
5400: 20 62 72 61 6e 63 68 20 72 65 6c 65 61 73 65 73   branch releases
5410: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49  ..**.** The SQLI
5420: 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45  TE_VERSION_NUMBE
5430: 52 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20  R is an integer 
5440: 74 68 61 74 20 69 73 20 63 6f 6d 70 75 74 65 64  that is computed
5450: 20 61 73 0a 2a 2a 20 66 6f 6c 6c 6f 77 73 3a 0a   as.** follows:.
5460: 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74  **.** <blockquot
5470: 65 3e 3c 70 72 65 3e 0a 2a 2a 20 53 51 4c 49 54  e><pre>.** SQLIT
5480: 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52  E_VERSION_NUMBER
5490: 20 3d 20 57 2a 31 30 30 30 30 30 30 20 2b 20 58   = W*1000000 + X
54a0: 2a 31 30 30 30 20 2b 20 59 0a 2a 2a 20 3c 2f 70  *1000 + Y.** </p
54b0: 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e  re></blockquote>
54c0: 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65 20 76 65 72  .**.** Since ver
54d0: 73 69 6f 6e 20 33 2e 36 2e 31 38 2c 20 53 51 4c  sion 3.6.18, SQL
54e0: 69 74 65 20 73 6f 75 72 63 65 20 63 6f 64 65 20  ite source code 
54f0: 68 61 73 20 62 65 65 6e 20 73 74 6f 72 65 64 20  has been stored 
5500: 69 6e 20 74 68 65 0a 2a 2a 20 3c 61 20 68 72 65  in the.** <a hre
5510: 66 3d 22 68 74 74 70 3a 2f 2f 77 77 77 2e 66 6f  f="http://www.fo
5520: 73 73 69 6c 2d 73 63 6d 2e 6f 72 67 2f 22 3e 66  ssil-scm.org/">f
5530: 6f 73 73 69 6c 20 63 6f 6e 66 69 67 75 72 61 74  ossil configurat
5540: 69 6f 6e 20 6d 61 6e 61 67 65 6d 65 6e 74 0a 2a  ion management.*
5550: 2a 20 73 79 73 74 65 6d 3c 2f 61 3e 2e 20 20 54  * system</a>.  T
5560: 68 65 20 53 51 4c 49 54 45 5f 53 4f 55 52 43 45  he SQLITE_SOURCE
5570: 5f 49 44 0a 2a 2a 20 6d 61 63 72 6f 20 69 73 20  _ID.** macro is 
5580: 61 20 73 74 72 69 6e 67 20 77 68 69 63 68 20 69  a string which i
5590: 64 65 6e 74 69 66 69 65 73 20 61 20 70 61 72 74  dentifies a part
55a0: 69 63 75 6c 61 72 20 63 68 65 63 6b 2d 69 6e 20  icular check-in 
55b0: 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69 74  of SQLite.** wit
55c0: 68 69 6e 20 69 74 73 20 63 6f 6e 66 69 67 75 72  hin its configur
55d0: 61 74 69 6f 6e 20 6d 61 6e 61 67 65 6d 65 6e 74  ation management
55e0: 20 73 79 73 74 65 6d 2e 20 20 54 68 65 20 73 74   system.  The st
55f0: 72 69 6e 67 20 63 6f 6e 74 61 69 6e 73 20 74 68  ring contains th
5600: 65 0a 2a 2a 20 64 61 74 65 20 61 6e 64 20 74 69  e.** date and ti
5610: 6d 65 20 6f 66 20 74 68 65 20 63 68 65 63 6b 2d  me of the check-
5620: 69 6e 20 28 55 54 43 29 20 61 6e 64 20 61 6e 20  in (UTC) and an 
5630: 53 48 41 31 20 68 61 73 68 20 6f 66 20 74 68 65  SHA1 hash of the
5640: 20 65 6e 74 69 72 65 0a 2a 2a 20 73 6f 75 72 63   entire.** sourc
5650: 65 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 53 65  e tree..**.** Se
5660: 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33  e also: [sqlite3
5670: 5f 6c 69 62 76 65 72 73 69 6f 6e 28 29 5d 2c 0a  _libversion()],.
5680: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6c 69 62 76  ** [sqlite3_libv
5690: 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 29 5d  ersion_number()]
56a0: 2c 20 5b 73 71 6c 69 74 65 33 5f 73 6f 75 72 63  , [sqlite3_sourc
56b0: 65 69 64 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69  eid()],.** [sqli
56c0: 74 65 5f 76 65 72 73 69 6f 6e 28 29 5d 20 61 6e  te_version()] an
56d0: 64 20 5b 73 71 6c 69 74 65 5f 73 6f 75 72 63 65  d [sqlite_source
56e0: 5f 69 64 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65  _id()]..**.** Re
56f0: 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30  quirements: [H10
5700: 30 31 31 5d 20 5b 48 31 30 30 31 34 5d 0a 2a 2f  011] [H10014].*/
5710: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
5720: 56 45 52 53 49 4f 4e 20 20 20 20 20 20 20 20 22  VERSION        "
5730: 33 2e 36 2e 31 38 22 0a 23 64 65 66 69 6e 65 20  3.6.18".#define 
5740: 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e  SQLITE_VERSION_N
5750: 55 4d 42 45 52 20 33 30 30 36 30 31 38 0a 23 64  UMBER 3006018.#d
5760: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 4f 55  efine SQLITE_SOU
5770: 52 43 45 5f 49 44 20 20 20 20 20 20 22 32 30 30  RCE_ID      "200
5780: 39 2d 30 39 2d 31 31 20 31 34 3a 30 35 3a 30 37  9-09-11 14:05:07
5790: 20 62 30 38 34 38 32 38 61 37 37 31 65 63 34 30   b084828a771ec40
57a0: 62 65 38 35 66 30 37 63 35 39 30 63 61 39 39 64  be85f07c590ca99d
57b0: 65 34 66 36 63 32 34 65 65 22 0a 0a 2f 2a 0a 2a  e4f6c24ee"../*.*
57c0: 2a 20 43 41 50 49 33 52 45 46 3a 20 52 75 6e 2d  * CAPI3REF: Run-
57d0: 54 69 6d 65 20 4c 69 62 72 61 72 79 20 56 65 72  Time Library Ver
57e0: 73 69 6f 6e 20 4e 75 6d 62 65 72 73 20 7b 48 31  sion Numbers {H1
57f0: 30 30 32 30 7d 20 3c 53 36 30 31 30 30 3e 0a 2a  0020} <S60100>.*
5800: 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69  * KEYWORDS: sqli
5810: 74 65 33 5f 76 65 72 73 69 6f 6e 0a 2a 2a 0a 2a  te3_version.**.*
5820: 2a 20 54 68 65 73 65 20 69 6e 74 65 72 66 61 63  * These interfac
5830: 65 73 20 70 72 6f 76 69 64 65 20 74 68 65 20 73  es provide the s
5840: 61 6d 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ame information 
5850: 61 73 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 56  as the [SQLITE_V
5860: 45 52 53 49 4f 4e 5d 2c 0a 2a 2a 20 5b 53 51 4c  ERSION],.** [SQL
5870: 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42  ITE_VERSION_NUMB
5880: 45 52 5d 2c 20 61 6e 64 20 5b 53 51 4c 49 54 45  ER], and [SQLITE
5890: 5f 53 4f 55 52 43 45 5f 49 44 5d 20 23 64 65 66  _SOURCE_ID] #def
58a0: 69 6e 65 73 20 69 6e 20 74 68 65 20 68 65 61 64  ines in the head
58b0: 65 72 2c 0a 2a 2a 20 62 75 74 20 61 72 65 20 61  er,.** but are a
58c0: 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
58d0: 68 65 20 6c 69 62 72 61 72 79 20 69 6e 73 74 65  he library inste
58e0: 61 64 20 6f 66 20 74 68 65 20 68 65 61 64 65 72  ad of the header
58f0: 20 66 69 6c 65 2e 20 20 43 61 75 74 69 6f 75 73   file.  Cautious
5900: 0a 2a 2a 20 70 72 6f 67 72 61 6d 6d 65 72 73 20  .** programmers 
5910: 6d 69 67 68 74 20 69 6e 63 6c 75 64 65 20 61 73  might include as
5920: 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74  sert() statement
5930: 73 20 69 6e 20 74 68 65 69 72 20 61 70 70 6c 69  s in their appli
5940: 63 61 74 69 6f 6e 20 74 6f 0a 2a 2a 20 76 65 72  cation to.** ver
5950: 69 66 79 20 74 68 61 74 20 76 61 6c 75 65 73 20  ify that values 
5960: 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 73  returned by thes
5970: 65 20 69 6e 74 65 72 66 61 63 65 73 20 6d 61 74  e interfaces mat
5980: 63 68 20 74 68 65 20 6d 61 63 72 6f 73 20 69 6e  ch the macros in
5990: 0a 2a 2a 20 74 68 65 20 68 65 61 64 65 72 2c 20  .** the header, 
59a0: 61 6e 64 20 74 68 75 73 20 69 6e 73 75 72 65 20  and thus insure 
59b0: 74 68 61 74 20 74 68 65 20 61 70 70 6c 69 63 61  that the applica
59c0: 74 69 6f 6e 20 69 73 0a 2a 2a 20 63 6f 6d 70 69  tion is.** compi
59d0: 6c 65 64 20 77 69 74 68 20 6d 61 74 63 68 69 6e  led with matchin
59e0: 67 20 6c 69 62 72 61 72 79 20 61 6e 64 20 68 65  g library and he
59f0: 61 64 65 72 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a  ader files..**.*
5a00: 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70  * <blockquote><p
5a10: 72 65 3e 0a 2a 2a 20 61 73 73 65 72 74 28 20 73  re>.** assert( s
5a20: 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f  qlite3_libversio
5a30: 6e 5f 6e 75 6d 62 65 72 28 29 3d 3d 53 51 4c 49  n_number()==SQLI
5a40: 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45  TE_VERSION_NUMBE
5a50: 52 20 29 3b 0a 2a 2a 20 61 73 73 65 72 74 28 20  R );.** assert( 
5a60: 73 74 72 63 6d 70 28 73 71 6c 69 74 65 33 5f 73  strcmp(sqlite3_s
5a70: 6f 75 72 63 65 69 64 28 29 2c 53 51 4c 49 54 45  ourceid(),SQLITE
5a80: 5f 53 4f 55 52 43 45 5f 49 44 29 3d 3d 30 20 29  _SOURCE_ID)==0 )
5a90: 3b 0a 2a 2a 20 61 73 73 65 72 74 28 20 73 74 72  ;.** assert( str
5aa0: 63 6d 70 28 73 71 6c 69 74 65 33 5f 6c 69 62 76  cmp(sqlite3_libv
5ab0: 65 72 73 69 6f 6e 2c 53 51 4c 49 54 45 5f 56 45  ersion,SQLITE_VE
5ac0: 52 53 49 4f 4e 29 3d 3d 30 20 29 3b 0a 2a 2a 20  RSION)==0 );.** 
5ad0: 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f  </pre></blockquo
5ae0: 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  te>.**.** The sq
5af0: 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e  lite3_libversion
5b00: 28 29 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  () function retu
5b10: 72 6e 73 20 74 68 65 20 73 61 6d 65 20 69 6e 66  rns the same inf
5b20: 6f 72 6d 61 74 69 6f 6e 20 61 73 20 69 73 0a 2a  ormation as is.*
5b30: 2a 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33  * in the sqlite3
5b40: 5f 76 65 72 73 69 6f 6e 5b 5d 20 73 74 72 69 6e  _version[] strin
5b50: 67 20 63 6f 6e 73 74 61 6e 74 2e 20 20 54 68 65  g constant.  The
5b60: 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 70 72 6f   function is pro
5b70: 76 69 64 65 64 0a 2a 2a 20 66 6f 72 20 75 73 65  vided.** for use
5b80: 20 69 6e 20 44 4c 4c 73 20 73 69 6e 63 65 20 44   in DLLs since D
5b90: 4c 4c 20 75 73 65 72 73 20 75 73 75 61 6c 6c 79  LL users usually
5ba0: 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 64 69 72   do not have dir
5bb0: 65 63 74 20 61 63 63 65 73 73 20 74 6f 20 73 74  ect access to st
5bc0: 72 69 6e 67 0a 2a 2a 20 63 6f 6e 73 74 61 6e 74  ring.** constant
5bd0: 73 20 77 69 74 68 69 6e 20 74 68 65 20 44 4c 4c  s within the DLL
5be0: 2e 20 20 53 69 6d 69 6c 61 72 6c 79 2c 20 74 68  .  Similarly, th
5bf0: 65 20 73 71 6c 69 74 65 33 5f 73 6f 75 72 63 65  e sqlite3_source
5c00: 69 64 28 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a  id() function.**
5c10: 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 61 6d   returns the sam
5c20: 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 73  e information as
5c30: 20 69 73 20 69 6e 20 74 68 65 20 5b 53 51 4c 49   is in the [SQLI
5c40: 54 45 5f 53 4f 55 52 43 45 5f 49 44 5d 20 23 64  TE_SOURCE_ID] #d
5c50: 65 66 69 6e 65 20 6f 66 0a 2a 2a 20 74 68 65 20  efine of.** the 
5c60: 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2a 0a  header file..**.
5c70: 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71  ** See also: [sq
5c80: 6c 69 74 65 5f 76 65 72 73 69 6f 6e 28 29 5d 20  lite_version()] 
5c90: 61 6e 64 20 5b 73 71 6c 69 74 65 5f 73 6f 75 72  and [sqlite_sour
5ca0: 63 65 5f 69 64 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  ce_id()]..**.** 
5cb0: 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48  Requirements: [H
5cc0: 31 30 30 32 31 5d 20 5b 48 31 30 30 32 32 5d 20  10021] [H10022] 
5cd0: 5b 48 31 30 30 32 33 5d 0a 2a 2f 0a 53 51 4c 49  [H10023].*/.SQLI
5ce0: 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61  TE_API const cha
5cf0: 72 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f  r sqlite3_versio
5d00: 6e 5b 5d 20 3d 20 53 51 4c 49 54 45 5f 56 45 52  n[] = SQLITE_VER
5d10: 53 49 4f 4e 3b 0a 53 51 4c 49 54 45 5f 41 50 49  SION;.SQLITE_API
5d20: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
5d30: 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 28  ite3_libversion(
5d40: 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50  void);.SQLITE_AP
5d50: 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  I const char *sq
5d60: 6c 69 74 65 33 5f 73 6f 75 72 63 65 69 64 28 76  lite3_sourceid(v
5d70: 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  oid);.SQLITE_API
5d80: 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c 69 62   int sqlite3_lib
5d90: 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 76  version_number(v
5da0: 6f 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  oid);../*.** CAP
5db0: 49 33 52 45 46 3a 20 54 65 73 74 20 54 6f 20 53  I3REF: Test To S
5dc0: 65 65 20 49 66 20 54 68 65 20 4c 69 62 72 61 72  ee If The Librar
5dd0: 79 20 49 73 20 54 68 72 65 61 64 73 61 66 65 20  y Is Threadsafe 
5de0: 7b 48 31 30 31 30 30 7d 20 3c 53 36 30 31 30 30  {H10100} <S60100
5df0: 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63  >.**.** SQLite c
5e00: 61 6e 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 77  an be compiled w
5e10: 69 74 68 20 6f 72 20 77 69 74 68 6f 75 74 20 6d  ith or without m
5e20: 75 74 65 78 65 73 2e 20 20 57 68 65 6e 0a 2a 2a  utexes.  When.**
5e30: 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 54 48 52   the [SQLITE_THR
5e40: 45 41 44 53 41 46 45 5d 20 43 20 70 72 65 70 72  EADSAFE] C prepr
5e50: 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 20 69 73  ocessor macro is
5e60: 20 31 20 6f 72 20 32 2c 20 6d 75 74 65 78 65 73   1 or 2, mutexes
5e70: 0a 2a 2a 20 61 72 65 20 65 6e 61 62 6c 65 64 20  .** are enabled 
5e80: 61 6e 64 20 53 51 4c 69 74 65 20 69 73 20 74 68  and SQLite is th
5e90: 72 65 61 64 73 61 66 65 2e 20 20 57 68 65 6e 20  readsafe.  When 
5ea0: 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 54  the.** [SQLITE_T
5eb0: 48 52 45 41 44 53 41 46 45 5d 20 6d 61 63 72 6f  HREADSAFE] macro
5ec0: 20 69 73 20 30 2c 20 0a 2a 2a 20 74 68 65 20 6d   is 0, .** the m
5ed0: 75 74 65 78 65 73 20 61 72 65 20 6f 6d 69 74 74  utexes are omitt
5ee0: 65 64 2e 20 20 57 69 74 68 6f 75 74 20 74 68 65  ed.  Without the
5ef0: 20 6d 75 74 65 78 65 73 2c 20 69 74 20 69 73 20   mutexes, it is 
5f00: 6e 6f 74 20 73 61 66 65 0a 2a 2a 20 74 6f 20 75  not safe.** to u
5f10: 73 65 20 53 51 4c 69 74 65 20 63 6f 6e 63 75 72  se SQLite concur
5f20: 72 65 6e 74 6c 79 20 66 72 6f 6d 20 6d 6f 72 65  rently from more
5f30: 20 74 68 61 6e 20 6f 6e 65 20 74 68 72 65 61 64   than one thread
5f40: 2e 0a 2a 2a 0a 2a 2a 20 45 6e 61 62 6c 69 6e 67  ..**.** Enabling
5f50: 20 6d 75 74 65 78 65 73 20 69 6e 63 75 72 73 20   mutexes incurs 
5f60: 61 20 6d 65 61 73 75 72 61 62 6c 65 20 70 65 72  a measurable per
5f70: 66 6f 72 6d 61 6e 63 65 20 70 65 6e 61 6c 74 79  formance penalty
5f80: 2e 0a 2a 2a 20 53 6f 20 69 66 20 73 70 65 65 64  ..** So if speed
5f90: 20 69 73 20 6f 66 20 75 74 6d 6f 73 74 20 69 6d   is of utmost im
5fa0: 70 6f 72 74 61 6e 63 65 2c 20 69 74 20 6d 61 6b  portance, it mak
5fb0: 65 73 20 73 65 6e 73 65 20 74 6f 20 64 69 73 61  es sense to disa
5fc0: 62 6c 65 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78  ble.** the mutex
5fd0: 65 73 2e 20 20 42 75 74 20 66 6f 72 20 6d 61 78  es.  But for max
5fe0: 69 6d 75 6d 20 73 61 66 65 74 79 2c 20 6d 75 74  imum safety, mut
5ff0: 65 78 65 73 20 73 68 6f 75 6c 64 20 62 65 20 65  exes should be e
6000: 6e 61 62 6c 65 64 2e 0a 2a 2a 20 54 68 65 20 64  nabled..** The d
6010: 65 66 61 75 6c 74 20 62 65 68 61 76 69 6f 72 20  efault behavior 
6020: 69 73 20 66 6f 72 20 6d 75 74 65 78 65 73 20 74  is for mutexes t
6030: 6f 20 62 65 20 65 6e 61 62 6c 65 64 2e 0a 2a 2a  o be enabled..**
6040: 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61  .** This interfa
6050: 63 65 20 63 61 6e 20 62 65 20 75 73 65 64 20 62  ce can be used b
6060: 79 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e  y an application
6070: 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
6080: 61 74 20 74 68 65 0a 2a 2a 20 76 65 72 73 69 6f  at the.** versio
6090: 6e 20 6f 66 20 53 51 4c 69 74 65 20 74 68 61 74  n of SQLite that
60a0: 20 69 74 20 69 73 20 6c 69 6e 6b 69 6e 67 20 61   it is linking a
60b0: 67 61 69 6e 73 74 20 77 61 73 20 63 6f 6d 70 69  gainst was compi
60c0: 6c 65 64 20 77 69 74 68 0a 2a 2a 20 74 68 65 20  led with.** the 
60d0: 64 65 73 69 72 65 64 20 73 65 74 74 69 6e 67 20  desired setting 
60e0: 6f 66 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 54  of the [SQLITE_T
60f0: 48 52 45 41 44 53 41 46 45 5d 20 6d 61 63 72 6f  HREADSAFE] macro
6100: 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74  ..**.** This int
6110: 65 72 66 61 63 65 20 6f 6e 6c 79 20 72 65 70 6f  erface only repo
6120: 72 74 73 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69  rts on the compi
6130: 6c 65 2d 74 69 6d 65 20 6d 75 74 65 78 20 73 65  le-time mutex se
6140: 74 74 69 6e 67 0a 2a 2a 20 6f 66 20 74 68 65 20  tting.** of the 
6150: 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41  [SQLITE_THREADSA
6160: 46 45 5d 20 66 6c 61 67 2e 20 20 49 66 20 53 51  FE] flag.  If SQ
6170: 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64  Lite is compiled
6180: 20 77 69 74 68 0a 2a 2a 20 53 51 4c 49 54 45 5f   with.** SQLITE_
6190: 54 48 52 45 41 44 53 41 46 45 3d 31 20 74 68 65  THREADSAFE=1 the
61a0: 6e 20 6d 75 74 65 78 65 73 20 61 72 65 20 65 6e  n mutexes are en
61b0: 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74  abled by default
61c0: 20 62 75 74 0a 2a 2a 20 63 61 6e 20 62 65 20 66   but.** can be f
61d0: 75 6c 6c 79 20 6f 72 20 70 61 72 74 69 61 6c 6c  ully or partiall
61e0: 79 20 64 69 73 61 62 6c 65 64 20 75 73 69 6e 67  y disabled using
61f0: 20 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69   a call to [sqli
6200: 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 0a 2a 2a  te3_config()].**
6210: 20 77 69 74 68 20 74 68 65 20 76 65 72 62 73 20   with the verbs 
6220: 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53  [SQLITE_CONFIG_S
6230: 49 4e 47 4c 45 54 48 52 45 41 44 5d 2c 20 5b 53  INGLETHREAD], [S
6240: 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c  QLITE_CONFIG_MUL
6250: 54 49 54 48 52 45 41 44 5d 2c 0a 2a 2a 20 6f 72  TITHREAD],.** or
6260: 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f   [SQLITE_CONFIG_
6270: 4d 55 54 45 58 5d 2e 20 20 54 68 65 20 72 65 74  MUTEX].  The ret
6280: 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 69  urn value of thi
6290: 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 77 73  s function shows
62a0: 0a 2a 2a 20 6f 6e 6c 79 20 74 68 65 20 64 65 66  .** only the def
62b0: 61 75 6c 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  ault compile-tim
62c0: 65 20 73 65 74 74 69 6e 67 2c 20 6e 6f 74 20 61  e setting, not a
62d0: 6e 79 20 72 75 6e 2d 74 69 6d 65 20 63 68 61 6e  ny run-time chan
62e0: 67 65 73 0a 2a 2a 20 74 6f 20 74 68 61 74 20 73  ges.** to that s
62f0: 65 74 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53 65  etting..**.** Se
6300: 65 20 74 68 65 20 5b 74 68 72 65 61 64 69 6e 67  e the [threading
6310: 20 6d 6f 64 65 5d 20 64 6f 63 75 6d 65 6e 74 61   mode] documenta
6320: 74 69 6f 6e 20 66 6f 72 20 61 64 64 69 74 69 6f  tion for additio
6330: 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  nal information.
6340: 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
6350: 6e 74 73 3a 20 5b 48 31 30 31 30 31 5d 20 5b 48  nts: [H10101] [H
6360: 31 30 31 30 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45  10102].*/.SQLITE
6370: 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
6380: 5f 74 68 72 65 61 64 73 61 66 65 28 76 6f 69 64  _threadsafe(void
6390: 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
63a0: 45 46 3a 20 44 61 74 61 62 61 73 65 20 43 6f 6e  EF: Database Con
63b0: 6e 65 63 74 69 6f 6e 20 48 61 6e 64 6c 65 20 7b  nection Handle {
63c0: 48 31 32 30 30 30 7d 20 3c 53 34 30 32 30 30 3e  H12000} <S40200>
63d0: 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 64  .** KEYWORDS: {d
63e0: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
63f0: 6f 6e 7d 20 7b 64 61 74 61 62 61 73 65 20 63 6f  on} {database co
6400: 6e 6e 65 63 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a  nnections}.**.**
6410: 20 45 61 63 68 20 6f 70 65 6e 20 53 51 4c 69 74   Each open SQLit
6420: 65 20 64 61 74 61 62 61 73 65 20 69 73 20 72 65  e database is re
6430: 70 72 65 73 65 6e 74 65 64 20 62 79 20 61 20 70  presented by a p
6440: 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73  ointer to an ins
6450: 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 65 20  tance of.** the 
6460: 6f 70 61 71 75 65 20 73 74 72 75 63 74 75 72 65  opaque structure
6470: 20 6e 61 6d 65 64 20 22 73 71 6c 69 74 65 33 22   named "sqlite3"
6480: 2e 20 20 49 74 20 69 73 20 75 73 65 66 75 6c 20  .  It is useful 
6490: 74 6f 20 74 68 69 6e 6b 20 6f 66 20 61 6e 20 73  to think of an s
64a0: 71 6c 69 74 65 33 0a 2a 2a 20 70 6f 69 6e 74 65  qlite3.** pointe
64b0: 72 20 61 73 20 61 6e 20 6f 62 6a 65 63 74 2e 20  r as an object. 
64c0: 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6f 70   The [sqlite3_op
64d0: 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f  en()], [sqlite3_
64e0: 6f 70 65 6e 31 36 28 29 5d 2c 20 61 6e 64 0a 2a  open16()], and.*
64f0: 2a 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  * [sqlite3_open_
6500: 76 32 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73  v2()] interfaces
6510: 20 61 72 65 20 69 74 73 20 63 6f 6e 73 74 72 75   are its constru
6520: 63 74 6f 72 73 2c 20 61 6e 64 20 5b 73 71 6c 69  ctors, and [sqli
6530: 74 65 33 5f 63 6c 6f 73 65 28 29 5d 0a 2a 2a 20  te3_close()].** 
6540: 69 73 20 69 74 73 20 64 65 73 74 72 75 63 74 6f  is its destructo
6550: 72 2e 20 20 54 68 65 72 65 20 61 72 65 20 6d 61  r.  There are ma
6560: 6e 79 20 6f 74 68 65 72 20 69 6e 74 65 72 66 61  ny other interfa
6570: 63 65 73 20 28 73 75 63 68 20 61 73 0a 2a 2a 20  ces (such as.** 
6580: 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
6590: 5f 76 32 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33  _v2()], [sqlite3
65a0: 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
65b0: 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c  ()], and.** [sql
65c0: 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75  ite3_busy_timeou
65d0: 74 28 29 5d 20 74 6f 20 6e 61 6d 65 20 62 75 74  t()] to name but
65e0: 20 74 68 72 65 65 29 20 74 68 61 74 20 61 72 65   three) that are
65f0: 20 6d 65 74 68 6f 64 73 20 6f 6e 20 61 6e 0a 2a   methods on an.*
6600: 2a 20 73 71 6c 69 74 65 33 20 6f 62 6a 65 63 74  * sqlite3 object
6610: 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
6620: 75 63 74 20 73 71 6c 69 74 65 33 20 73 71 6c 69  uct sqlite3 sqli
6630: 74 65 33 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  te3;../*.** CAPI
6640: 33 52 45 46 3a 20 36 34 2d 42 69 74 20 49 6e 74  3REF: 64-Bit Int
6650: 65 67 65 72 20 54 79 70 65 73 20 7b 48 31 30 32  eger Types {H102
6660: 30 30 7d 20 3c 53 31 30 31 31 30 3e 0a 2a 2a 20  00} <S10110>.** 
6670: 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65  KEYWORDS: sqlite
6680: 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 5f 75 69  _int64 sqlite_ui
6690: 6e 74 36 34 0a 2a 2a 0a 2a 2a 20 42 65 63 61 75  nt64.**.** Becau
66a0: 73 65 20 74 68 65 72 65 20 69 73 20 6e 6f 20 63  se there is no c
66b0: 72 6f 73 73 2d 70 6c 61 74 66 6f 72 6d 20 77 61  ross-platform wa
66c0: 79 20 74 6f 20 73 70 65 63 69 66 79 20 36 34 2d  y to specify 64-
66d0: 62 69 74 20 69 6e 74 65 67 65 72 20 74 79 70 65  bit integer type
66e0: 73 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6e 63 6c  s.** SQLite incl
66f0: 75 64 65 73 20 74 79 70 65 64 65 66 73 20 66 6f  udes typedefs fo
6700: 72 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20  r 64-bit signed 
6710: 61 6e 64 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  and unsigned int
6720: 65 67 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  egers..**.** The
6730: 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 61   sqlite3_int64 a
6740: 6e 64 20 73 71 6c 69 74 65 33 5f 75 69 6e 74 36  nd sqlite3_uint6
6750: 34 20 61 72 65 20 74 68 65 20 70 72 65 66 65 72  4 are the prefer
6760: 72 65 64 20 74 79 70 65 20 64 65 66 69 6e 69 74  red type definit
6770: 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 73 71 6c  ions..** The sql
6780: 69 74 65 5f 69 6e 74 36 34 20 61 6e 64 20 73 71  ite_int64 and sq
6790: 6c 69 74 65 5f 75 69 6e 74 36 34 20 74 79 70 65  lite_uint64 type
67a0: 73 20 61 72 65 20 73 75 70 70 6f 72 74 65 64 20  s are supported 
67b0: 66 6f 72 20 62 61 63 6b 77 61 72 64 73 0a 2a 2a  for backwards.**
67c0: 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 6f   compatibility o
67d0: 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  nly..**.** Requi
67e0: 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30 32 30 31  rements: [H10201
67f0: 5d 20 5b 48 31 30 32 30 32 5d 0a 2a 2f 0a 23 69  ] [H10202].*/.#i
6800: 66 64 65 66 20 53 51 4c 49 54 45 5f 49 4e 54 36  fdef SQLITE_INT6
6810: 34 5f 54 59 50 45 0a 20 20 74 79 70 65 64 65 66  4_TYPE.  typedef
6820: 20 53 51 4c 49 54 45 5f 49 4e 54 36 34 5f 54 59   SQLITE_INT64_TY
6830: 50 45 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 3b  PE sqlite_int64;
6840: 0a 20 20 74 79 70 65 64 65 66 20 75 6e 73 69 67  .  typedef unsig
6850: 6e 65 64 20 53 51 4c 49 54 45 5f 49 4e 54 36 34  ned SQLITE_INT64
6860: 5f 54 59 50 45 20 73 71 6c 69 74 65 5f 75 69 6e  _TYPE sqlite_uin
6870: 74 36 34 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e  t64;.#elif defin
6880: 65 64 28 5f 4d 53 43 5f 56 45 52 29 20 7c 7c 20  ed(_MSC_VER) || 
6890: 64 65 66 69 6e 65 64 28 5f 5f 42 4f 52 4c 41 4e  defined(__BORLAN
68a0: 44 43 5f 5f 29 0a 20 20 74 79 70 65 64 65 66 20  DC__).  typedef 
68b0: 5f 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 5f 69  __int64 sqlite_i
68c0: 6e 74 36 34 3b 0a 20 20 74 79 70 65 64 65 66 20  nt64;.  typedef 
68d0: 75 6e 73 69 67 6e 65 64 20 5f 5f 69 6e 74 36 34  unsigned __int64
68e0: 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 3b 0a   sqlite_uint64;.
68f0: 23 65 6c 73 65 0a 20 20 74 79 70 65 64 65 66 20  #else.  typedef 
6900: 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 20 73 71  long long int sq
6910: 6c 69 74 65 5f 69 6e 74 36 34 3b 0a 20 20 74 79  lite_int64;.  ty
6920: 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64 20 6c  pedef unsigned l
6930: 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 20 73 71 6c  ong long int sql
6940: 69 74 65 5f 75 69 6e 74 36 34 3b 0a 23 65 6e 64  ite_uint64;.#end
6950: 69 66 0a 74 79 70 65 64 65 66 20 73 71 6c 69 74  if.typedef sqlit
6960: 65 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f  e_int64 sqlite3_
6970: 69 6e 74 36 34 3b 0a 74 79 70 65 64 65 66 20 73  int64;.typedef s
6980: 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c  qlite_uint64 sql
6990: 69 74 65 33 5f 75 69 6e 74 36 34 3b 0a 0a 2f 2a  ite3_uint64;../*
69a0: 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 69 6e 67  .** If compiling
69b0: 20 66 6f 72 20 61 20 70 72 6f 63 65 73 73 6f 72   for a processor
69c0: 20 74 68 61 74 20 6c 61 63 6b 73 20 66 6c 6f 61   that lacks floa
69d0: 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 70 70 6f  ting point suppo
69e0: 72 74 2c 0a 2a 2a 20 73 75 62 73 74 69 74 75 74  rt,.** substitut
69f0: 65 20 69 6e 74 65 67 65 72 20 66 6f 72 20 66 6c  e integer for fl
6a00: 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 2e 0a 2a 2f  oating-point..*/
6a10: 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
6a20: 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49  MIT_FLOATING_POI
6a30: 4e 54 0a 23 20 64 65 66 69 6e 65 20 64 6f 75 62  NT.# define doub
6a40: 6c 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  le sqlite3_int64
6a50: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43  .#endif../*.** C
6a60: 41 50 49 33 52 45 46 3a 20 43 6c 6f 73 69 6e 67  API3REF: Closing
6a70: 20 41 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e   A Database Conn
6a80: 65 63 74 69 6f 6e 20 7b 48 31 32 30 31 30 7d 20  ection {H12010} 
6a90: 3c 53 33 30 31 30 30 3e 3c 53 34 30 32 30 30 3e  <S30100><S40200>
6aa0: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
6ab0: 69 6e 65 20 69 73 20 74 68 65 20 64 65 73 74 72  ine is the destr
6ac0: 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 5b 73  uctor for the [s
6ad0: 71 6c 69 74 65 33 5d 20 6f 62 6a 65 63 74 2e 0a  qlite3] object..
6ae0: 2a 2a 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f  **.** Applicatio
6af0: 6e 73 20 73 68 6f 75 6c 64 20 5b 73 71 6c 69 74  ns should [sqlit
6b00: 65 33 5f 66 69 6e 61 6c 69 7a 65 20 7c 20 66 69  e3_finalize | fi
6b10: 6e 61 6c 69 7a 65 5d 20 61 6c 6c 20 5b 70 72 65  nalize] all [pre
6b20: 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73  pared statements
6b30: 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c 69 74 65  ].** and [sqlite
6b40: 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 20 7c 20 63  3_blob_close | c
6b50: 6c 6f 73 65 5d 20 61 6c 6c 20 5b 42 4c 4f 42 20  lose] all [BLOB 
6b60: 68 61 6e 64 6c 65 73 5d 20 61 73 73 6f 63 69 61  handles] associa
6b70: 74 65 64 20 77 69 74 68 0a 2a 2a 20 74 68 65 20  ted with.** the 
6b80: 5b 73 71 6c 69 74 65 33 5d 20 6f 62 6a 65 63 74  [sqlite3] object
6b90: 20 70 72 69 6f 72 20 74 6f 20 61 74 74 65 6d 70   prior to attemp
6ba0: 74 69 6e 67 20 74 6f 20 63 6c 6f 73 65 20 74 68  ting to close th
6bb0: 65 20 6f 62 6a 65 63 74 2e 0a 2a 2a 20 54 68 65  e object..** The
6bc0: 20 5b 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73   [sqlite3_next_s
6bd0: 74 6d 74 28 29 5d 20 69 6e 74 65 72 66 61 63 65  tmt()] interface
6be0: 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20   can be used to 
6bf0: 6c 6f 63 61 74 65 20 61 6c 6c 0a 2a 2a 20 5b 70  locate all.** [p
6c00: 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
6c10: 74 73 5d 20 61 73 73 6f 63 69 61 74 65 64 20 77  ts] associated w
6c20: 69 74 68 20 61 20 5b 64 61 74 61 62 61 73 65 20  ith a [database 
6c30: 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 66 20 64  connection] if d
6c40: 65 73 69 72 65 64 2e 0a 2a 2a 20 54 79 70 69 63  esired..** Typic
6c50: 61 6c 20 63 6f 64 65 20 6d 69 67 68 74 20 6c 6f  al code might lo
6c60: 6f 6b 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a  ok like this:.**
6c70: 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e  .** <blockquote>
6c80: 3c 70 72 65 3e 0a 2a 2a 20 73 71 6c 69 74 65 33  <pre>.** sqlite3
6c90: 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 2a 2a  _stmt *pStmt;.**
6ca0: 20 77 68 69 6c 65 28 20 28 70 53 74 6d 74 20 3d   while( (pStmt =
6cb0: 20 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74   sqlite3_next_st
6cc0: 6d 74 28 64 62 2c 20 30 29 29 21 3d 30 20 29 7b  mt(db, 0))!=0 ){
6cd0: 0a 2a 2a 20 26 6e 62 73 70 3b 20 20 20 73 71 6c  .** &nbsp;   sql
6ce0: 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53  ite3_finalize(pS
6cf0: 74 6d 74 29 3b 0a 2a 2a 20 7d 0a 2a 2a 20 3c 2f  tmt);.** }.** </
6d00: 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65  pre></blockquote
6d10: 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 5b 73 71 6c 69  >.**.** If [sqli
6d20: 74 65 33 5f 63 6c 6f 73 65 28 29 5d 20 69 73 20  te3_close()] is 
6d30: 69 6e 76 6f 6b 65 64 20 77 68 69 6c 65 20 61 20  invoked while a 
6d40: 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f  transaction is o
6d50: 70 65 6e 2c 0a 2a 2a 20 74 68 65 20 74 72 61 6e  pen,.** the tran
6d60: 73 61 63 74 69 6f 6e 20 69 73 20 61 75 74 6f 6d  saction is autom
6d70: 61 74 69 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20  atically rolled 
6d80: 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  back..**.** The 
6d90: 43 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b  C parameter to [
6da0: 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 43 29  sqlite3_close(C)
6db0: 5d 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72  ] must be either
6dc0: 20 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f 69 6e 74   a NULL.** point
6dd0: 65 72 20 6f 72 20 61 6e 20 5b 73 71 6c 69 74 65  er or an [sqlite
6de0: 33 5d 20 6f 62 6a 65 63 74 20 70 6f 69 6e 74 65  3] object pointe
6df0: 72 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72  r obtained.** fr
6e00: 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e  om [sqlite3_open
6e10: 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70  ()], [sqlite3_op
6e20: 65 6e 31 36 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b  en16()], or.** [
6e30: 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28  sqlite3_open_v2(
6e40: 29 5d 2c 20 61 6e 64 20 6e 6f 74 20 70 72 65 76  )], and not prev
6e50: 69 6f 75 73 6c 79 20 63 6c 6f 73 65 64 2e 0a 2a  iously closed..*
6e60: 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
6e70: 73 3a 0a 2a 2a 20 5b 48 31 32 30 31 31 5d 20 5b  s:.** [H12011] [
6e80: 48 31 32 30 31 32 5d 20 5b 48 31 32 30 31 33 5d  H12012] [H12013]
6e90: 20 5b 48 31 32 30 31 34 5d 20 5b 48 31 32 30 31   [H12014] [H1201
6ea0: 35 5d 20 5b 48 31 32 30 31 39 5d 0a 2a 2f 0a 53  5] [H12019].*/.S
6eb0: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
6ec0: 6c 69 74 65 33 5f 63 6c 6f 73 65 28 73 71 6c 69  lite3_close(sqli
6ed0: 74 65 33 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54  te3 *);../*.** T
6ee0: 68 65 20 74 79 70 65 20 66 6f 72 20 61 20 63 61  he type for a ca
6ef0: 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2e  llback function.
6f00: 0a 2a 2a 20 54 68 69 73 20 69 73 20 6c 65 67 61  .** This is lega
6f10: 63 79 20 61 6e 64 20 64 65 70 72 65 63 61 74 65  cy and deprecate
6f20: 64 2e 20 20 49 74 20 69 73 20 69 6e 63 6c 75 64  d.  It is includ
6f30: 65 64 20 66 6f 72 20 68 69 73 74 6f 72 69 63 61  ed for historica
6f40: 6c 0a 2a 2a 20 63 6f 6d 70 61 74 69 62 69 6c 69  l.** compatibili
6f50: 74 79 20 61 6e 64 20 69 73 20 6e 6f 74 20 64 6f  ty and is not do
6f60: 63 75 6d 65 6e 74 65 64 2e 0a 2a 2f 0a 74 79 70  cumented..*/.typ
6f70: 65 64 65 66 20 69 6e 74 20 28 2a 73 71 6c 69 74  edef int (*sqlit
6f80: 65 33 5f 63 61 6c 6c 62 61 63 6b 29 28 76 6f 69  e3_callback)(voi
6f90: 64 2a 2c 69 6e 74 2c 63 68 61 72 2a 2a 2c 20 63  d*,int,char**, c
6fa0: 68 61 72 2a 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  har**);../*.** C
6fb0: 41 50 49 33 52 45 46 3a 20 4f 6e 65 2d 53 74 65  API3REF: One-Ste
6fc0: 70 20 51 75 65 72 79 20 45 78 65 63 75 74 69 6f  p Query Executio
6fd0: 6e 20 49 6e 74 65 72 66 61 63 65 20 7b 48 31 32  n Interface {H12
6fe0: 31 30 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a  100} <S10000>.**
6ff0: 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
7000: 65 78 65 63 28 29 20 69 6e 74 65 72 66 61 63 65  exec() interface
7010: 20 69 73 20 61 20 63 6f 6e 76 65 6e 69 65 6e 74   is a convenient
7020: 20 77 61 79 20 6f 66 20 72 75 6e 6e 69 6e 67 20   way of running 
7030: 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 53  one or more.** S
7040: 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 77 69  QL statements wi
7050: 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20  thout having to 
7060: 77 72 69 74 65 20 61 20 6c 6f 74 20 6f 66 20 43  write a lot of C
7070: 20 63 6f 64 65 2e 20 20 54 68 65 20 55 54 46 2d   code.  The UTF-
7080: 38 20 65 6e 63 6f 64 65 64 0a 2a 2a 20 53 51 4c  8 encoded.** SQL
7090: 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20   statements are 
70a0: 70 61 73 73 65 64 20 69 6e 20 61 73 20 74 68 65  passed in as the
70b0: 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   second paramete
70c0: 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 65 78 65  r to sqlite3_exe
70d0: 63 28 29 2e 0a 2a 2a 20 54 68 65 20 73 74 61 74  c()..** The stat
70e0: 65 6d 65 6e 74 73 20 61 72 65 20 65 76 61 6c 75  ements are evalu
70f0: 61 74 65 64 20 6f 6e 65 20 62 79 20 6f 6e 65 20  ated one by one 
7100: 75 6e 74 69 6c 20 65 69 74 68 65 72 20 61 6e 20  until either an 
7110: 65 72 72 6f 72 20 6f 72 0a 2a 2a 20 61 6e 20 69  error or.** an i
7120: 6e 74 65 72 72 75 70 74 20 69 73 20 65 6e 63 6f  nterrupt is enco
7130: 75 6e 74 65 72 65 64 2c 20 6f 72 20 75 6e 74 69  untered, or unti
7140: 6c 20 74 68 65 79 20 61 72 65 20 61 6c 6c 20 64  l they are all d
7150: 6f 6e 65 2e 20 20 54 68 65 20 33 72 64 20 70 61  one.  The 3rd pa
7160: 72 61 6d 65 74 65 72 0a 2a 2a 20 69 73 20 61 6e  rameter.** is an
7170: 20 6f 70 74 69 6f 6e 61 6c 20 63 61 6c 6c 62 61   optional callba
7180: 63 6b 20 74 68 61 74 20 69 73 20 69 6e 76 6f 6b  ck that is invok
7190: 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68  ed once for each
71a0: 20 72 6f 77 20 6f 66 20 61 6e 79 20 71 75 65 72   row of any quer
71b0: 79 0a 2a 2a 20 72 65 73 75 6c 74 73 20 70 72 6f  y.** results pro
71c0: 64 75 63 65 64 20 62 79 20 74 68 65 20 53 51 4c  duced by the SQL
71d0: 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 54 68   statements.  Th
71e0: 65 20 35 74 68 20 70 61 72 61 6d 65 74 65 72 20  e 5th parameter 
71f0: 74 65 6c 6c 73 20 77 68 65 72 65 0a 2a 2a 20 74  tells where.** t
7200: 6f 20 77 72 69 74 65 20 61 6e 79 20 65 72 72 6f  o write any erro
7210: 72 20 6d 65 73 73 61 67 65 73 2e 0a 2a 2a 0a 2a  r messages..**.*
7220: 2a 20 54 68 65 20 65 72 72 6f 72 20 6d 65 73 73  * The error mess
7230: 61 67 65 20 70 61 73 73 65 64 20 62 61 63 6b 20  age passed back 
7240: 74 68 72 6f 75 67 68 20 74 68 65 20 35 74 68 20  through the 5th 
7250: 70 61 72 61 6d 65 74 65 72 20 69 73 20 68 65 6c  parameter is hel
7260: 64 0a 2a 2a 20 69 6e 20 6d 65 6d 6f 72 79 20 6f  d.** in memory o
7270: 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71  btained from [sq
7280: 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e  lite3_malloc()].
7290: 20 20 54 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d    To avoid a mem
72a0: 6f 72 79 20 6c 65 61 6b 2c 0a 2a 2a 20 74 68 65  ory leak,.** the
72b0: 20 63 61 6c 6c 69 6e 67 20 61 70 70 6c 69 63 61   calling applica
72c0: 74 69 6f 6e 20 73 68 6f 75 6c 64 20 63 61 6c 6c  tion should call
72d0: 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29   [sqlite3_free()
72e0: 5d 20 6f 6e 20 61 6e 79 20 65 72 72 6f 72 0a 2a  ] on any error.*
72f0: 2a 20 6d 65 73 73 61 67 65 20 72 65 74 75 72 6e  * message return
7300: 65 64 20 74 68 72 6f 75 67 68 20 74 68 65 20 35  ed through the 5
7310: 74 68 20 70 61 72 61 6d 65 74 65 72 20 77 68 65  th parameter whe
7320: 6e 20 69 74 20 68 61 73 20 66 69 6e 69 73 68 65  n it has finishe
7330: 64 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 65  d using.** the e
7340: 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2a  rror message..**
7350: 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 20 73  .** If the SQL s
7360: 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 20  tatement in the 
7370: 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 73  2nd parameter is
7380: 20 4e 55 4c 4c 20 6f 72 20 61 6e 20 65 6d 70 74   NULL or an empt
7390: 79 20 73 74 72 69 6e 67 0a 2a 2a 20 6f 72 20 61  y string.** or a
73a0: 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69   string containi
73b0: 6e 67 20 6f 6e 6c 79 20 77 68 69 74 65 73 70 61  ng only whitespa
73c0: 63 65 20 61 6e 64 20 63 6f 6d 6d 65 6e 74 73 2c  ce and comments,
73d0: 20 74 68 65 6e 20 6e 6f 20 53 51 4c 0a 2a 2a 20   then no SQL.** 
73e0: 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 65  statements are e
73f0: 76 61 6c 75 61 74 65 64 20 61 6e 64 20 74 68 65  valuated and the
7400: 20 64 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74   database is not
7410: 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20   changed..**.** 
7420: 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63  The sqlite3_exec
7430: 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20  () interface is 
7440: 69 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e 20 74  implemented in t
7450: 65 72 6d 73 20 6f 66 0a 2a 2a 20 5b 73 71 6c 69  erms of.** [sqli
7460: 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29  te3_prepare_v2()
7470: 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  ], [sqlite3_step
7480: 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65  ()], and [sqlite
7490: 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a  3_finalize()]..*
74a0: 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78  * The sqlite3_ex
74b0: 65 63 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65  ec() routine doe
74c0: 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 65  s nothing to the
74d0: 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20 63   database that c
74e0: 61 6e 6e 6f 74 20 62 65 20 64 6f 6e 65 0a 2a 2a  annot be done.**
74f0: 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 70 72 65   by [sqlite3_pre
7500: 70 61 72 65 5f 76 32 28 29 5d 2c 20 5b 73 71 6c  pare_v2()], [sql
7510: 69 74 65 33 5f 73 74 65 70 28 29 5d 2c 20 61 6e  ite3_step()], an
7520: 64 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  d [sqlite3_final
7530: 69 7a 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68  ize()]..**.** Th
7540: 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  e first paramete
7550: 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78  r to [sqlite3_ex
7560: 65 63 28 29 5d 20 6d 75 73 74 20 62 65 20 61 6e  ec()] must be an
7570: 20 76 61 6c 69 64 20 61 6e 64 20 6f 70 65 6e 0a   valid and open.
7580: 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  ** [database con
7590: 6e 65 63 74 69 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a 20  nection]..**.** 
75a0: 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  The database con
75b0: 6e 65 63 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74  nection must not
75c0: 20 62 65 20 63 6c 6f 73 65 64 20 77 68 69 6c 65   be closed while
75d0: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 78 65  .** [sqlite3_exe
75e0: 63 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67 2e  c()] is running.
75f0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69  .**.** The calli
7600: 6e 67 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75  ng function shou
7610: 6c 64 20 75 73 65 20 5b 73 71 6c 69 74 65 33 5f  ld use [sqlite3_
7620: 66 72 65 65 28 29 5d 20 74 6f 20 66 72 65 65 0a  free()] to free.
7630: 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 74 68  ** the memory th
7640: 61 74 20 2a 65 72 72 6d 73 67 20 69 73 20 6c 65  at *errmsg is le
7650: 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 6f  ft pointing at o
7660: 6e 63 65 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a  nce the error.**
7670: 20 6d 65 73 73 61 67 65 20 69 73 20 6e 6f 20 6c   message is no l
7680: 6f 6e 67 65 72 20 6e 65 65 64 65 64 2e 0a 2a 2a  onger needed..**
7690: 0a 2a 2a 20 54 68 65 20 53 51 4c 20 73 74 61 74  .** The SQL stat
76a0: 65 6d 65 6e 74 20 74 65 78 74 20 69 6e 20 74 68  ement text in th
76b0: 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20  e 2nd parameter 
76c0: 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63  to [sqlite3_exec
76d0: 28 29 5d 0a 2a 2a 20 6d 75 73 74 20 72 65 6d 61  ()].** must rema
76e0: 69 6e 20 75 6e 63 68 61 6e 67 65 64 20 77 68 69  in unchanged whi
76f0: 6c 65 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63  le [sqlite3_exec
7700: 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a  ()] is running..
7710: 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
7720: 74 73 3a 0a 2a 2a 20 5b 48 31 32 31 30 31 5d 20  ts:.** [H12101] 
7730: 5b 48 31 32 31 30 32 5d 20 5b 48 31 32 31 30 34  [H12102] [H12104
7740: 5d 20 5b 48 31 32 31 30 35 5d 20 5b 48 31 32 31  ] [H12105] [H121
7750: 30 37 5d 20 5b 48 31 32 31 31 30 5d 20 5b 48 31  07] [H12110] [H1
7760: 32 31 31 33 5d 20 5b 48 31 32 31 31 36 5d 0a 2a  2113] [H12116].*
7770: 2a 20 5b 48 31 32 31 31 39 5d 20 5b 48 31 32 31  * [H12119] [H121
7780: 32 32 5d 20 5b 48 31 32 31 32 35 5d 20 5b 48 31  22] [H12125] [H1
7790: 32 31 33 31 5d 20 5b 48 31 32 31 33 34 5d 20 5b  2131] [H12134] [
77a0: 48 31 32 31 33 37 5d 20 5b 48 31 32 31 33 38 5d  H12137] [H12138]
77b0: 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
77c0: 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28  nt sqlite3_exec(
77d0: 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 20 20 20  .  sqlite3*,    
77e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
7800: 20 41 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73   An open databas
7810: 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  e */.  const cha
7820: 72 20 2a 73 71 6c 2c 20 20 20 20 20 20 20 20 20  r *sql,         
7830: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7840: 20 20 2f 2a 20 53 51 4c 20 74 6f 20 62 65 20 65    /* SQL to be e
7850: 76 61 6c 75 61 74 65 64 20 2a 2f 0a 20 20 69 6e  valuated */.  in
7860: 74 20 28 2a 63 61 6c 6c 62 61 63 6b 29 28 76 6f  t (*callback)(vo
7870: 69 64 2a 2c 69 6e 74 2c 63 68 61 72 2a 2a 2c 63  id*,int,char**,c
7880: 68 61 72 2a 2a 29 2c 20 20 2f 2a 20 43 61 6c 6c  har**),  /* Call
7890: 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  back function */
78a0: 0a 20 20 76 6f 69 64 20 2a 2c 20 20 20 20 20 20  .  void *,      
78b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
78d0: 20 31 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f   1st argument to
78e0: 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 63   callback */.  c
78f0: 68 61 72 20 2a 2a 65 72 72 6d 73 67 20 20 20 20  har **errmsg    
7900: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7910: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72            /* Err
7920: 6f 72 20 6d 73 67 20 77 72 69 74 74 65 6e 20 68  or msg written h
7930: 65 72 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a  ere */.);../*.**
7940: 20 43 41 50 49 33 52 45 46 3a 20 52 65 73 75 6c   CAPI3REF: Resul
7950: 74 20 43 6f 64 65 73 20 7b 48 31 30 32 31 30 7d  t Codes {H10210}
7960: 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 20 4b 45 59   <S10700>.** KEY
7970: 57 4f 52 44 53 3a 20 53 51 4c 49 54 45 5f 4f 4b  WORDS: SQLITE_OK
7980: 20 7b 65 72 72 6f 72 20 63 6f 64 65 7d 20 7b 65   {error code} {e
7990: 72 72 6f 72 20 63 6f 64 65 73 7d 0a 2a 2a 20 4b  rror codes}.** K
79a0: 45 59 57 4f 52 44 53 3a 20 7b 72 65 73 75 6c 74  EYWORDS: {result
79b0: 20 63 6f 64 65 7d 20 7b 72 65 73 75 6c 74 20 63   code} {result c
79c0: 6f 64 65 73 7d 0a 2a 2a 0a 2a 2a 20 4d 61 6e 79  odes}.**.** Many
79d0: 20 53 51 4c 69 74 65 20 66 75 6e 63 74 69 6f 6e   SQLite function
79e0: 73 20 72 65 74 75 72 6e 20 61 6e 20 69 6e 74 65  s return an inte
79f0: 67 65 72 20 72 65 73 75 6c 74 20 63 6f 64 65 20  ger result code 
7a00: 66 72 6f 6d 20 74 68 65 20 73 65 74 20 73 68 6f  from the set sho
7a10: 77 6e 0a 2a 2a 20 68 65 72 65 20 69 6e 20 6f 72  wn.** here in or
7a20: 64 65 72 20 74 6f 20 69 6e 64 69 63 61 74 65 73  der to indicates
7a30: 20 73 75 63 63 65 73 73 20 6f 72 20 66 61 69 6c   success or fail
7a40: 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 65  ure..**.** New e
7a50: 72 72 6f 72 20 63 6f 64 65 73 20 6d 61 79 20 62  rror codes may b
7a60: 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72  e added in futur
7a70: 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51  e versions of SQ
7a80: 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  Lite..**.** See 
7a90: 61 6c 73 6f 3a 20 5b 53 51 4c 49 54 45 5f 49 4f  also: [SQLITE_IO
7aa0: 45 52 52 5f 52 45 41 44 20 7c 20 65 78 74 65 6e  ERR_READ | exten
7ab0: 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73  ded result codes
7ac0: 5d 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ].*/.#define SQL
7ad0: 49 54 45 5f 4f 4b 20 20 20 20 20 20 20 20 20 20  ITE_OK          
7ae0: 20 30 20 20 20 2f 2a 20 53 75 63 63 65 73 73 66   0   /* Successf
7af0: 75 6c 20 72 65 73 75 6c 74 20 2a 2f 0a 2f 2a 20  ul result */./* 
7b00: 62 65 67 69 6e 6e 69 6e 67 2d 6f 66 2d 65 72 72  beginning-of-err
7b10: 6f 72 2d 63 6f 64 65 73 20 2a 2f 0a 23 64 65 66  or-codes */.#def
7b20: 69 6e 65 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  ine SQLITE_ERROR
7b30: 20 20 20 20 20 20 20 20 31 20 20 20 2f 2a 20 53          1   /* S
7b40: 51 4c 20 65 72 72 6f 72 20 6f 72 20 6d 69 73 73  QL error or miss
7b50: 69 6e 67 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  ing database */.
7b60: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
7b70: 4e 54 45 52 4e 41 4c 20 20 20 20 20 32 20 20 20  NTERNAL     2   
7b80: 2f 2a 20 49 6e 74 65 72 6e 61 6c 20 6c 6f 67 69  /* Internal logi
7b90: 63 20 65 72 72 6f 72 20 69 6e 20 53 51 4c 69 74  c error in SQLit
7ba0: 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  e */.#define SQL
7bb0: 49 54 45 5f 50 45 52 4d 20 20 20 20 20 20 20 20  ITE_PERM        
7bc0: 20 33 20 20 20 2f 2a 20 41 63 63 65 73 73 20 70   3   /* Access p
7bd0: 65 72 6d 69 73 73 69 6f 6e 20 64 65 6e 69 65 64  ermission denied
7be0: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
7bf0: 54 45 5f 41 42 4f 52 54 20 20 20 20 20 20 20 20  TE_ABORT        
7c00: 34 20 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20  4   /* Callback 
7c10: 72 6f 75 74 69 6e 65 20 72 65 71 75 65 73 74 65  routine requeste
7c20: 64 20 61 6e 20 61 62 6f 72 74 20 2a 2f 0a 23 64  d an abort */.#d
7c30: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 42 55 53  efine SQLITE_BUS
7c40: 59 20 20 20 20 20 20 20 20 20 35 20 20 20 2f 2a  Y         5   /*
7c50: 20 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69   The database fi
7c60: 6c 65 20 69 73 20 6c 6f 63 6b 65 64 20 2a 2f 0a  le is locked */.
7c70: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c  #define SQLITE_L
7c80: 4f 43 4b 45 44 20 20 20 20 20 20 20 36 20 20 20  OCKED       6   
7c90: 2f 2a 20 41 20 74 61 62 6c 65 20 69 6e 20 74 68  /* A table in th
7ca0: 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f  e database is lo
7cb0: 63 6b 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20  cked */.#define 
7cc0: 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 20 20 20  SQLITE_NOMEM    
7cd0: 20 20 20 20 37 20 20 20 2f 2a 20 41 20 6d 61 6c      7   /* A mal
7ce0: 6c 6f 63 28 29 20 66 61 69 6c 65 64 20 2a 2f 0a  loc() failed */.
7cf0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52  #define SQLITE_R
7d00: 45 41 44 4f 4e 4c 59 20 20 20 20 20 38 20 20 20  EADONLY     8   
7d10: 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 77 72  /* Attempt to wr
7d20: 69 74 65 20 61 20 72 65 61 64 6f 6e 6c 79 20 64  ite a readonly d
7d30: 61 74 61 62 61 73 65 20 2a 2f 0a 23 64 65 66 69  atabase */.#defi
7d40: 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52  ne SQLITE_INTERR
7d50: 55 50 54 20 20 20 20 39 20 20 20 2f 2a 20 4f 70  UPT    9   /* Op
7d60: 65 72 61 74 69 6f 6e 20 74 65 72 6d 69 6e 61 74  eration terminat
7d70: 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 69 6e  ed by sqlite3_in
7d80: 74 65 72 72 75 70 74 28 29 2a 2f 0a 23 64 65 66  terrupt()*/.#def
7d90: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  ine SQLITE_IOERR
7da0: 20 20 20 20 20 20 20 31 30 20 20 20 2f 2a 20 53         10   /* S
7db0: 6f 6d 65 20 6b 69 6e 64 20 6f 66 20 64 69 73 6b  ome kind of disk
7dc0: 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72   I/O error occur
7dd0: 72 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  red */.#define S
7de0: 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 20 20  QLITE_CORRUPT   
7df0: 20 20 31 31 20 20 20 2f 2a 20 54 68 65 20 64 61    11   /* The da
7e00: 74 61 62 61 73 65 20 64 69 73 6b 20 69 6d 61 67  tabase disk imag
7e10: 65 20 69 73 20 6d 61 6c 66 6f 72 6d 65 64 20 2a  e is malformed *
7e20: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
7e30: 5f 4e 4f 54 46 4f 55 4e 44 20 20 20 20 31 32 20  _NOTFOUND    12 
7e40: 20 20 2f 2a 20 4e 4f 54 20 55 53 45 44 2e 20 54    /* NOT USED. T
7e50: 61 62 6c 65 20 6f 72 20 72 65 63 6f 72 64 20 6e  able or record n
7e60: 6f 74 20 66 6f 75 6e 64 20 2a 2f 0a 23 64 65 66  ot found */.#def
7e70: 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20  ine SQLITE_FULL 
7e80: 20 20 20 20 20 20 20 31 33 20 20 20 2f 2a 20 49         13   /* I
7e90: 6e 73 65 72 74 69 6f 6e 20 66 61 69 6c 65 64 20  nsertion failed 
7ea0: 62 65 63 61 75 73 65 20 64 61 74 61 62 61 73 65  because database
7eb0: 20 69 73 20 66 75 6c 6c 20 2a 2f 0a 23 64 65 66   is full */.#def
7ec0: 69 6e 65 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f  ine SQLITE_CANTO
7ed0: 50 45 4e 20 20 20 20 31 34 20 20 20 2f 2a 20 55  PEN    14   /* U
7ee0: 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 74 68  nable to open th
7ef0: 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
7f00: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
7f10: 45 5f 50 52 4f 54 4f 43 4f 4c 20 20 20 20 31 35  E_PROTOCOL    15
7f20: 20 20 20 2f 2a 20 4e 4f 54 20 55 53 45 44 2e 20     /* NOT USED. 
7f30: 44 61 74 61 62 61 73 65 20 6c 6f 63 6b 20 70 72  Database lock pr
7f40: 6f 74 6f 63 6f 6c 20 65 72 72 6f 72 20 2a 2f 0a  otocol error */.
7f50: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45  #define SQLITE_E
7f60: 4d 50 54 59 20 20 20 20 20 20 20 31 36 20 20 20  MPTY       16   
7f70: 2f 2a 20 44 61 74 61 62 61 73 65 20 69 73 20 65  /* Database is e
7f80: 6d 70 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20  mpty */.#define 
7f90: 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 20 20  SQLITE_SCHEMA   
7fa0: 20 20 20 31 37 20 20 20 2f 2a 20 54 68 65 20 64     17   /* The d
7fb0: 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 63  atabase schema c
7fc0: 68 61 6e 67 65 64 20 2a 2f 0a 23 64 65 66 69 6e  hanged */.#defin
7fd0: 65 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20  e SQLITE_TOOBIG 
7fe0: 20 20 20 20 20 31 38 20 20 20 2f 2a 20 53 74 72       18   /* Str
7ff0: 69 6e 67 20 6f 72 20 42 4c 4f 42 20 65 78 63 65  ing or BLOB exce
8000: 65 64 73 20 73 69 7a 65 20 6c 69 6d 69 74 20 2a  eds size limit *
8010: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
8020: 5f 43 4f 4e 53 54 52 41 49 4e 54 20 20 31 39 20  _CONSTRAINT  19 
8030: 20 20 2f 2a 20 41 62 6f 72 74 20 64 75 65 20 74    /* Abort due t
8040: 6f 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f  o constraint vio
8050: 6c 61 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e  lation */.#defin
8060: 65 20 53 51 4c 49 54 45 5f 4d 49 53 4d 41 54 43  e SQLITE_MISMATC
8070: 48 20 20 20 20 32 30 20 20 20 2f 2a 20 44 61 74  H    20   /* Dat
8080: 61 20 74 79 70 65 20 6d 69 73 6d 61 74 63 68 20  a type mismatch 
8090: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
80a0: 45 5f 4d 49 53 55 53 45 20 20 20 20 20 20 32 31  E_MISUSE      21
80b0: 20 20 20 2f 2a 20 4c 69 62 72 61 72 79 20 75 73     /* Library us
80c0: 65 64 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 2a  ed incorrectly *
80d0: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
80e0: 5f 4e 4f 4c 46 53 20 20 20 20 20 20 20 32 32 20  _NOLFS       22 
80f0: 20 20 2f 2a 20 55 73 65 73 20 4f 53 20 66 65 61    /* Uses OS fea
8100: 74 75 72 65 73 20 6e 6f 74 20 73 75 70 70 6f 72  tures not suppor
8110: 74 65 64 20 6f 6e 20 68 6f 73 74 20 2a 2f 0a 23  ted on host */.#
8120: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 55  define SQLITE_AU
8130: 54 48 20 20 20 20 20 20 20 20 32 33 20 20 20 2f  TH        23   /
8140: 2a 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  * Authorization 
8150: 64 65 6e 69 65 64 20 2a 2f 0a 23 64 65 66 69 6e  denied */.#defin
8160: 65 20 53 51 4c 49 54 45 5f 46 4f 52 4d 41 54 20  e SQLITE_FORMAT 
8170: 20 20 20 20 20 32 34 20 20 20 2f 2a 20 41 75 78       24   /* Aux
8180: 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20  iliary database 
8190: 66 6f 72 6d 61 74 20 65 72 72 6f 72 20 2a 2f 0a  format error */.
81a0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52  #define SQLITE_R
81b0: 41 4e 47 45 20 20 20 20 20 20 20 32 35 20 20 20  ANGE       25   
81c0: 2f 2a 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72  /* 2nd parameter
81d0: 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 69 6e 64   to sqlite3_bind
81e0: 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 2a 2f   out of range */
81f0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
8200: 4e 4f 54 41 44 42 20 20 20 20 20 20 32 36 20 20  NOTADB      26  
8210: 20 2f 2a 20 46 69 6c 65 20 6f 70 65 6e 65 64 20   /* File opened 
8220: 74 68 61 74 20 69 73 20 6e 6f 74 20 61 20 64 61  that is not a da
8230: 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 23  tabase file */.#
8240: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 4f  define SQLITE_RO
8250: 57 20 20 20 20 20 20 20 20 20 31 30 30 20 20 2f  W         100  /
8260: 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  * sqlite3_step()
8270: 20 68 61 73 20 61 6e 6f 74 68 65 72 20 72 6f 77   has another row
8280: 20 72 65 61 64 79 20 2a 2f 0a 23 64 65 66 69 6e   ready */.#defin
8290: 65 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 20 20  e SQLITE_DONE   
82a0: 20 20 20 20 20 31 30 31 20 20 2f 2a 20 73 71 6c       101  /* sql
82b0: 69 74 65 33 5f 73 74 65 70 28 29 20 68 61 73 20  ite3_step() has 
82c0: 66 69 6e 69 73 68 65 64 20 65 78 65 63 75 74 69  finished executi
82d0: 6e 67 20 2a 2f 0a 2f 2a 20 65 6e 64 2d 6f 66 2d  ng */./* end-of-
82e0: 65 72 72 6f 72 2d 63 6f 64 65 73 20 2a 2f 0a 0a  error-codes */..
82f0: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
8300: 45 78 74 65 6e 64 65 64 20 52 65 73 75 6c 74 20  Extended Result 
8310: 43 6f 64 65 73 20 7b 48 31 30 32 32 30 7d 20 3c  Codes {H10220} <
8320: 53 31 30 37 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f  S10700>.** KEYWO
8330: 52 44 53 3a 20 7b 65 78 74 65 6e 64 65 64 20 65  RDS: {extended e
8340: 72 72 6f 72 20 63 6f 64 65 7d 20 7b 65 78 74 65  rror code} {exte
8350: 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 73  nded error codes
8360: 7d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b  }.** KEYWORDS: {
8370: 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20  extended result 
8380: 63 6f 64 65 7d 20 7b 65 78 74 65 6e 64 65 64 20  code} {extended 
8390: 72 65 73 75 6c 74 20 63 6f 64 65 73 7d 0a 2a 2a  result codes}.**
83a0: 0a 2a 2a 20 49 6e 20 69 74 73 20 64 65 66 61 75  .** In its defau
83b0: 6c 74 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e  lt configuration
83c0: 2c 20 53 51 4c 69 74 65 20 41 50 49 20 72 6f 75  , SQLite API rou
83d0: 74 69 6e 65 73 20 72 65 74 75 72 6e 20 6f 6e 65  tines return one
83e0: 20 6f 66 20 32 36 20 69 6e 74 65 67 65 72 0a 2a   of 26 integer.*
83f0: 2a 20 5b 53 51 4c 49 54 45 5f 4f 4b 20 7c 20 72  * [SQLITE_OK | r
8400: 65 73 75 6c 74 20 63 6f 64 65 73 5d 2e 20 20 48  esult codes].  H
8410: 6f 77 65 76 65 72 2c 20 65 78 70 65 72 69 65 6e  owever, experien
8420: 63 65 20 68 61 73 20 73 68 6f 77 6e 20 74 68 61  ce has shown tha
8430: 74 20 6d 61 6e 79 20 6f 66 0a 2a 2a 20 74 68 65  t many of.** the
8440: 73 65 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20  se result codes 
8450: 61 72 65 20 74 6f 6f 20 63 6f 61 72 73 65 2d 67  are too coarse-g
8460: 72 61 69 6e 65 64 2e 20 20 54 68 65 79 20 64 6f  rained.  They do
8470: 20 6e 6f 74 20 70 72 6f 76 69 64 65 20 61 73 0a   not provide as.
8480: 2a 2a 20 6d 75 63 68 20 69 6e 66 6f 72 6d 61 74  ** much informat
8490: 69 6f 6e 20 61 62 6f 75 74 20 70 72 6f 62 6c 65  ion about proble
84a0: 6d 73 20 61 73 20 70 72 6f 67 72 61 6d 6d 65 72  ms as programmer
84b0: 73 20 6d 69 67 68 74 20 6c 69 6b 65 2e 20 20 49  s might like.  I
84c0: 6e 20 61 6e 20 65 66 66 6f 72 74 20 74 6f 0a 2a  n an effort to.*
84d0: 2a 20 61 64 64 72 65 73 73 20 74 68 69 73 2c 20  * address this, 
84e0: 6e 65 77 65 72 20 76 65 72 73 69 6f 6e 73 20 6f  newer versions o
84f0: 66 20 53 51 4c 69 74 65 20 28 76 65 72 73 69 6f  f SQLite (versio
8500: 6e 20 33 2e 33 2e 38 20 61 6e 64 20 6c 61 74 65  n 3.3.8 and late
8510: 72 29 20 69 6e 63 6c 75 64 65 0a 2a 2a 20 73 75  r) include.** su
8520: 70 70 6f 72 74 20 66 6f 72 20 61 64 64 69 74 69  pport for additi
8530: 6f 6e 61 6c 20 72 65 73 75 6c 74 20 63 6f 64 65  onal result code
8540: 73 20 74 68 61 74 20 70 72 6f 76 69 64 65 20 6d  s that provide m
8550: 6f 72 65 20 64 65 74 61 69 6c 65 64 20 69 6e 66  ore detailed inf
8560: 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75  ormation.** abou
8570: 74 20 65 72 72 6f 72 73 2e 20 54 68 65 20 65 78  t errors. The ex
8580: 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f  tended result co
8590: 64 65 73 20 61 72 65 20 65 6e 61 62 6c 65 64 20  des are enabled 
85a0: 6f 72 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20 6f  or disabled.** o
85b0: 6e 20 61 20 70 65 72 20 64 61 74 61 62 61 73 65  n a per database
85c0: 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 62 61 73 69   connection basi
85d0: 73 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b  s using the.** [
85e0: 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64  sqlite3_extended
85f0: 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 29 5d  _result_codes()]
8600: 20 41 50 49 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65   API..**.** Some
8610: 20 6f 66 20 74 68 65 20 61 76 61 69 6c 61 62 6c   of the availabl
8620: 65 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c  e extended resul
8630: 74 20 63 6f 64 65 73 20 61 72 65 20 6c 69 73 74  t codes are list
8640: 65 64 20 68 65 72 65 2e 0a 2a 2a 20 4f 6e 65 20  ed here..** One 
8650: 6d 61 79 20 65 78 70 65 63 74 20 74 68 65 20 6e  may expect the n
8660: 75 6d 62 65 72 20 6f 66 20 65 78 74 65 6e 64 65  umber of extende
8670: 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 77  d result codes w
8680: 69 6c 6c 20 62 65 20 65 78 70 61 6e 64 0a 2a 2a  ill be expand.**
8690: 20 6f 76 65 72 20 74 69 6d 65 2e 20 20 53 6f 66   over time.  Sof
86a0: 74 77 61 72 65 20 74 68 61 74 20 75 73 65 73 20  tware that uses 
86b0: 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20  extended result 
86c0: 63 6f 64 65 73 20 73 68 6f 75 6c 64 20 65 78 70  codes should exp
86d0: 65 63 74 0a 2a 2a 20 74 6f 20 73 65 65 20 6e 65  ect.** to see ne
86e0: 77 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 69  w result codes i
86f0: 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65  n future release
8700: 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a  s of SQLite..**.
8710: 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 4f 4b  ** The SQLITE_OK
8720: 20 72 65 73 75 6c 74 20 63 6f 64 65 20 77 69 6c   result code wil
8730: 6c 20 6e 65 76 65 72 20 62 65 20 65 78 74 65 6e  l never be exten
8740: 64 65 64 2e 20 20 49 74 20 77 69 6c 6c 20 61 6c  ded.  It will al
8750: 77 61 79 73 0a 2a 2a 20 62 65 20 65 78 61 63 74  ways.** be exact
8760: 6c 79 20 7a 65 72 6f 2e 0a 2a 2f 0a 23 64 65 66  ly zero..*/.#def
8770: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  ine SQLITE_IOERR
8780: 5f 52 45 41 44 20 20 20 20 20 20 20 20 20 20 20  _READ           
8790: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52     (SQLITE_IOERR
87a0: 20 7c 20 28 31 3c 3c 38 29 29 0a 23 64 65 66 69   | (1<<8)).#defi
87b0: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  ne SQLITE_IOERR_
87c0: 53 48 4f 52 54 5f 52 45 41 44 20 20 20 20 20 20  SHORT_READ      
87d0: 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20    (SQLITE_IOERR 
87e0: 7c 20 28 32 3c 3c 38 29 29 0a 23 64 65 66 69 6e  | (2<<8)).#defin
87f0: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57  e SQLITE_IOERR_W
8800: 52 49 54 45 20 20 20 20 20 20 20 20 20 20 20 20  RITE            
8810: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c   (SQLITE_IOERR |
8820: 20 28 33 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65   (3<<8)).#define
8830: 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53   SQLITE_IOERR_FS
8840: 59 4e 43 20 20 20 20 20 20 20 20 20 20 20 20 20  YNC             
8850: 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20  (SQLITE_IOERR | 
8860: 28 34 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20  (4<<8)).#define 
8870: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 49 52  SQLITE_IOERR_DIR
8880: 5f 46 53 59 4e 43 20 20 20 20 20 20 20 20 20 28  _FSYNC         (
8890: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28  SQLITE_IOERR | (
88a0: 35 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53  5<<8)).#define S
88b0: 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e  QLITE_IOERR_TRUN
88c0: 43 41 54 45 20 20 20 20 20 20 20 20 20 20 28 53  CATE          (S
88d0: 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 36  QLITE_IOERR | (6
88e0: 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51  <<8)).#define SQ
88f0: 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54  LITE_IOERR_FSTAT
8900: 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51               (SQ
8910: 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 37 3c  LITE_IOERR | (7<
8920: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c  <8)).#define SQL
8930: 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b  ITE_IOERR_UNLOCK
8940: 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c              (SQL
8950: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 38 3c 3c  ITE_IOERR | (8<<
8960: 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  8)).#define SQLI
8970: 54 45 5f 49 4f 45 52 52 5f 52 44 4c 4f 43 4b 20  TE_IOERR_RDLOCK 
8980: 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49             (SQLI
8990: 54 45 5f 49 4f 45 52 52 20 7c 20 28 39 3c 3c 38  TE_IOERR | (9<<8
89a0: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
89b0: 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 20 20  E_IOERR_DELETE  
89c0: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
89d0: 45 5f 49 4f 45 52 52 20 7c 20 28 31 30 3c 3c 38  E_IOERR | (10<<8
89e0: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
89f0: 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20  E_IOERR_BLOCKED 
8a00: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
8a10: 45 5f 49 4f 45 52 52 20 7c 20 28 31 31 3c 3c 38  E_IOERR | (11<<8
8a20: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
8a30: 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 20 20  E_IOERR_NOMEM   
8a40: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
8a50: 45 5f 49 4f 45 52 52 20 7c 20 28 31 32 3c 3c 38  E_IOERR | (12<<8
8a60: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
8a70: 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 20 20  E_IOERR_ACCESS  
8a80: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
8a90: 45 5f 49 4f 45 52 52 20 7c 20 28 31 33 3c 3c 38  E_IOERR | (13<<8
8aa0: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
8ab0: 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53  E_IOERR_CHECKRES
8ac0: 45 52 56 45 44 4c 4f 43 4b 20 28 53 51 4c 49 54  ERVEDLOCK (SQLIT
8ad0: 45 5f 49 4f 45 52 52 20 7c 20 28 31 34 3c 3c 38  E_IOERR | (14<<8
8ae0: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
8af0: 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 20 20 20 20  E_IOERR_LOCK    
8b00: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
8b10: 45 5f 49 4f 45 52 52 20 7c 20 28 31 35 3c 3c 38  E_IOERR | (15<<8
8b20: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
8b30: 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 20 20 20  E_IOERR_CLOSE   
8b40: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
8b50: 45 5f 49 4f 45 52 52 20 7c 20 28 31 36 3c 3c 38  E_IOERR | (16<<8
8b60: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
8b70: 45 5f 49 4f 45 52 52 5f 44 49 52 5f 43 4c 4f 53  E_IOERR_DIR_CLOS
8b80: 45 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54  E         (SQLIT
8b90: 45 5f 49 4f 45 52 52 20 7c 20 28 31 37 3c 3c 38  E_IOERR | (17<<8
8ba0: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
8bb0: 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43  E_LOCKED_SHAREDC
8bc0: 41 43 48 45 20 20 20 20 20 20 28 53 51 4c 49 54  ACHE      (SQLIT
8bd0: 45 5f 4c 4f 43 4b 45 44 20 7c 20 28 31 3c 3c 38  E_LOCKED | (1<<8
8be0: 29 20 29 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  ) )../*.** CAPI3
8bf0: 52 45 46 3a 20 46 6c 61 67 73 20 46 6f 72 20 46  REF: Flags For F
8c00: 69 6c 65 20 4f 70 65 6e 20 4f 70 65 72 61 74 69  ile Open Operati
8c10: 6f 6e 73 20 7b 48 31 30 32 33 30 7d 20 3c 48 31  ons {H10230} <H1
8c20: 31 31 32 30 3e 20 3c 48 31 32 37 30 30 3e 0a 2a  1120> <H12700>.*
8c30: 2a 0a 2a 2a 20 54 68 65 73 65 20 62 69 74 20 76  *.** These bit v
8c40: 61 6c 75 65 73 20 61 72 65 20 69 6e 74 65 6e 64  alues are intend
8c50: 65 64 20 66 6f 72 20 75 73 65 20 69 6e 20 74 68  ed for use in th
8c60: 65 0a 2a 2a 20 33 72 64 20 70 61 72 61 6d 65 74  e.** 3rd paramet
8c70: 65 72 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74  er to the [sqlit
8c80: 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 69 6e  e3_open_v2()] in
8c90: 74 65 72 66 61 63 65 20 61 6e 64 0a 2a 2a 20 69  terface and.** i
8ca0: 6e 20 74 68 65 20 34 74 68 20 70 61 72 61 6d 65  n the 4th parame
8cb0: 74 65 72 20 74 6f 20 74 68 65 20 78 4f 70 65 6e  ter to the xOpen
8cc0: 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 0a 2a   method of the.*
8cd0: 2a 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20  * [sqlite3_vfs] 
8ce0: 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 23 64 65 66 69  object..*/.#defi
8cf0: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52  ne SQLITE_OPEN_R
8d00: 45 41 44 4f 4e 4c 59 20 20 20 20 20 20 20 20 20  EADONLY         
8d10: 30 78 30 30 30 30 30 30 30 31 20 20 2f 2a 20 4f  0x00000001  /* O
8d20: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70  k for sqlite3_op
8d30: 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69  en_v2() */.#defi
8d40: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52  ne SQLITE_OPEN_R
8d50: 45 41 44 57 52 49 54 45 20 20 20 20 20 20 20 20  EADWRITE        
8d60: 30 78 30 30 30 30 30 30 30 32 20 20 2f 2a 20 4f  0x00000002  /* O
8d70: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70  k for sqlite3_op
8d80: 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69  en_v2() */.#defi
8d90: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43  ne SQLITE_OPEN_C
8da0: 52 45 41 54 45 20 20 20 20 20 20 20 20 20 20 20  REATE           
8db0: 30 78 30 30 30 30 30 30 30 34 20 20 2f 2a 20 4f  0x00000004  /* O
8dc0: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70  k for sqlite3_op
8dd0: 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69  en_v2() */.#defi
8de0: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44  ne SQLITE_OPEN_D
8df0: 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 20 20 20  ELETEONCLOSE    
8e00: 30 78 30 30 30 30 30 30 30 38 20 20 2f 2a 20 56  0x00000008  /* V
8e10: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  FS only */.#defi
8e20: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45  ne SQLITE_OPEN_E
8e30: 58 43 4c 55 53 49 56 45 20 20 20 20 20 20 20 20  XCLUSIVE        
8e40: 30 78 30 30 30 30 30 30 31 30 20 20 2f 2a 20 56  0x00000010  /* V
8e50: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  FS only */.#defi
8e60: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  ne SQLITE_OPEN_M
8e70: 41 49 4e 5f 44 42 20 20 20 20 20 20 20 20 20 20  AIN_DB          
8e80: 30 78 30 30 30 30 30 31 30 30 20 20 2f 2a 20 56  0x00000100  /* V
8e90: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  FS only */.#defi
8ea0: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54  ne SQLITE_OPEN_T
8eb0: 45 4d 50 5f 44 42 20 20 20 20 20 20 20 20 20 20  EMP_DB          
8ec0: 30 78 30 30 30 30 30 32 30 30 20 20 2f 2a 20 56  0x00000200  /* V
8ed0: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  FS only */.#defi
8ee0: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54  ne SQLITE_OPEN_T
8ef0: 52 41 4e 53 49 45 4e 54 5f 44 42 20 20 20 20 20  RANSIENT_DB     
8f00: 30 78 30 30 30 30 30 34 30 30 20 20 2f 2a 20 56  0x00000400  /* V
8f10: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  FS only */.#defi
8f20: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  ne SQLITE_OPEN_M
8f30: 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 20 20 20 20  AIN_JOURNAL     
8f40: 30 78 30 30 30 30 30 38 30 30 20 20 2f 2a 20 56  0x00000800  /* V
8f50: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  FS only */.#defi
8f60: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54  ne SQLITE_OPEN_T
8f70: 45 4d 50 5f 4a 4f 55 52 4e 41 4c 20 20 20 20 20  EMP_JOURNAL     
8f80: 30 78 30 30 30 30 31 30 30 30 20 20 2f 2a 20 56  0x00001000  /* V
8f90: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  FS only */.#defi
8fa0: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53  ne SQLITE_OPEN_S
8fb0: 55 42 4a 4f 55 52 4e 41 4c 20 20 20 20 20 20 20  UBJOURNAL       
8fc0: 30 78 30 30 30 30 32 30 30 30 20 20 2f 2a 20 56  0x00002000  /* V
8fd0: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  FS only */.#defi
8fe0: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  ne SQLITE_OPEN_M
8ff0: 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 20 20  ASTER_JOURNAL   
9000: 30 78 30 30 30 30 34 30 30 30 20 20 2f 2a 20 56  0x00004000  /* V
9010: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  FS only */.#defi
9020: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e  ne SQLITE_OPEN_N
9030: 4f 4d 55 54 45 58 20 20 20 20 20 20 20 20 20 20  OMUTEX          
9040: 30 78 30 30 30 30 38 30 30 30 20 20 2f 2a 20 4f  0x00008000  /* O
9050: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70  k for sqlite3_op
9060: 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69  en_v2() */.#defi
9070: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46  ne SQLITE_OPEN_F
9080: 55 4c 4c 4d 55 54 45 58 20 20 20 20 20 20 20 20  ULLMUTEX        
9090: 30 78 30 30 30 31 30 30 30 30 20 20 2f 2a 20 4f  0x00010000  /* O
90a0: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70  k for sqlite3_op
90b0: 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69  en_v2() */.#defi
90c0: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53  ne SQLITE_OPEN_S
90d0: 48 41 52 45 44 43 41 43 48 45 20 20 20 20 20 20  HAREDCACHE      
90e0: 30 78 30 30 30 32 30 30 30 30 20 20 2f 2a 20 4f  0x00020000  /* O
90f0: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70  k for sqlite3_op
9100: 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69  en_v2() */.#defi
9110: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 50  ne SQLITE_OPEN_P
9120: 52 49 56 41 54 45 43 41 43 48 45 20 20 20 20 20  RIVATECACHE     
9130: 30 78 30 30 30 34 30 30 30 30 20 20 2f 2a 20 4f  0x00040000  /* O
9140: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70  k for sqlite3_op
9150: 65 6e 5f 76 32 28 29 20 2a 2f 0a 0a 2f 2a 0a 2a  en_v2() */../*.*
9160: 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 76 69  * CAPI3REF: Devi
9170: 63 65 20 43 68 61 72 61 63 74 65 72 69 73 74 69  ce Characteristi
9180: 63 73 20 7b 48 31 30 32 34 30 7d 20 3c 48 31 31  cs {H10240} <H11
9190: 31 32 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78  120>.**.** The x
91a0: 44 65 76 69 63 65 43 61 70 61 62 69 6c 69 74 69  DeviceCapabiliti
91b0: 65 73 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65  es method of the
91c0: 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74   [sqlite3_io_met
91d0: 68 6f 64 73 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20  hods].** object 
91e0: 72 65 74 75 72 6e 73 20 61 6e 20 69 6e 74 65 67  returns an integ
91f0: 65 72 20 77 68 69 63 68 20 69 73 20 61 20 76 65  er which is a ve
9200: 63 74 6f 72 20 6f 66 20 74 68 65 20 74 68 65 73  ctor of the thes
9210: 65 0a 2a 2a 20 62 69 74 20 76 61 6c 75 65 73 20  e.** bit values 
9220: 65 78 70 72 65 73 73 69 6e 67 20 49 2f 4f 20 63  expressing I/O c
9230: 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20 6f  haracteristics o
9240: 66 20 74 68 65 20 6d 61 73 73 20 73 74 6f 72 61  f the mass stora
9250: 67 65 0a 2a 2a 20 64 65 76 69 63 65 20 74 68 61  ge.** device tha
9260: 74 20 68 6f 6c 64 73 20 74 68 65 20 66 69 6c 65  t holds the file
9270: 20 74 68 61 74 20 74 68 65 20 5b 73 71 6c 69 74   that the [sqlit
9280: 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 0a 2a  e3_io_methods].*
9290: 2a 20 72 65 66 65 72 73 20 74 6f 2e 0a 2a 2a 0a  * refers to..**.
92a0: 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f  ** The SQLITE_IO
92b0: 43 41 50 5f 41 54 4f 4d 49 43 20 70 72 6f 70 65  CAP_ATOMIC prope
92c0: 72 74 79 20 6d 65 61 6e 73 20 74 68 61 74 20 61  rty means that a
92d0: 6c 6c 20 77 72 69 74 65 73 20 6f 66 0a 2a 2a 20  ll writes of.** 
92e0: 61 6e 79 20 73 69 7a 65 20 61 72 65 20 61 74 6f  any size are ato
92f0: 6d 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45  mic.  The SQLITE
9300: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 6e 6e 6e  _IOCAP_ATOMICnnn
9310: 20 76 61 6c 75 65 73 0a 2a 2a 20 6d 65 61 6e 20   values.** mean 
9320: 74 68 61 74 20 77 72 69 74 65 73 20 6f 66 20 62  that writes of b
9330: 6c 6f 63 6b 73 20 74 68 61 74 20 61 72 65 20 6e  locks that are n
9340: 6e 6e 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65  nn bytes in size
9350: 20 61 6e 64 0a 2a 2a 20 61 72 65 20 61 6c 69 67   and.** are alig
9360: 6e 65 64 20 74 6f 20 61 6e 20 61 64 64 72 65 73  ned to an addres
9370: 73 20 77 68 69 63 68 20 69 73 20 61 6e 20 69 6e  s which is an in
9380: 74 65 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f  teger multiple o
9390: 66 0a 2a 2a 20 6e 6e 6e 20 61 72 65 20 61 74 6f  f.** nnn are ato
93a0: 6d 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45  mic.  The SQLITE
93b0: 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45  _IOCAP_SAFE_APPE
93c0: 4e 44 20 76 61 6c 75 65 20 6d 65 61 6e 73 0a 2a  ND value means.*
93d0: 2a 20 74 68 61 74 20 77 68 65 6e 20 64 61 74 61  * that when data
93e0: 20 69 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20   is appended to 
93f0: 61 20 66 69 6c 65 2c 20 74 68 65 20 64 61 74 61  a file, the data
9400: 20 69 73 20 61 70 70 65 6e 64 65 64 0a 2a 2a 20   is appended.** 
9410: 66 69 72 73 74 20 74 68 65 6e 20 74 68 65 20 73  first then the s
9420: 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20  ize of the file 
9430: 69 73 20 65 78 74 65 6e 64 65 64 2c 20 6e 65 76  is extended, nev
9440: 65 72 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20  er the other.** 
9450: 77 61 79 20 61 72 6f 75 6e 64 2e 20 20 54 68 65  way around.  The
9460: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45   SQLITE_IOCAP_SE
9470: 51 55 45 4e 54 49 41 4c 20 70 72 6f 70 65 72 74  QUENTIAL propert
9480: 79 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20  y means that.** 
9490: 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 77  information is w
94a0: 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 20 69  ritten to disk i
94b0: 6e 20 74 68 65 20 73 61 6d 65 20 6f 72 64 65 72  n the same order
94c0: 20 61 73 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20   as calls.** to 
94d0: 78 57 72 69 74 65 28 29 2e 0a 2a 2f 0a 23 64 65  xWrite()..*/.#de
94e0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
94f0: 50 5f 41 54 4f 4d 49 43 20 20 20 20 20 20 20 20  P_ATOMIC        
9500: 20 20 30 78 30 30 30 30 30 30 30 31 0a 23 64 65    0x00000001.#de
9510: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
9520: 50 5f 41 54 4f 4d 49 43 35 31 32 20 20 20 20 20  P_ATOMIC512     
9530: 20 20 30 78 30 30 30 30 30 30 30 32 0a 23 64 65    0x00000002.#de
9540: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
9550: 50 5f 41 54 4f 4d 49 43 31 4b 20 20 20 20 20 20  P_ATOMIC1K      
9560: 20 20 30 78 30 30 30 30 30 30 30 34 0a 23 64 65    0x00000004.#de
9570: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
9580: 50 5f 41 54 4f 4d 49 43 32 4b 20 20 20 20 20 20  P_ATOMIC2K      
9590: 20 20 30 78 30 30 30 30 30 30 30 38 0a 23 64 65    0x00000008.#de
95a0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
95b0: 50 5f 41 54 4f 4d 49 43 34 4b 20 20 20 20 20 20  P_ATOMIC4K      
95c0: 20 20 30 78 30 30 30 30 30 30 31 30 0a 23 64 65    0x00000010.#de
95d0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
95e0: 50 5f 41 54 4f 4d 49 43 38 4b 20 20 20 20 20 20  P_ATOMIC8K      
95f0: 20 20 30 78 30 30 30 30 30 30 32 30 0a 23 64 65    0x00000020.#de
9600: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
9610: 50 5f 41 54 4f 4d 49 43 31 36 4b 20 20 20 20 20  P_ATOMIC16K     
9620: 20 20 30 78 30 30 30 30 30 30 34 30 0a 23 64 65    0x00000040.#de
9630: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
9640: 50 5f 41 54 4f 4d 49 43 33 32 4b 20 20 20 20 20  P_ATOMIC32K     
9650: 20 20 30 78 30 30 30 30 30 30 38 30 0a 23 64 65    0x00000080.#de
9660: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
9670: 50 5f 41 54 4f 4d 49 43 36 34 4b 20 20 20 20 20  P_ATOMIC64K     
9680: 20 20 30 78 30 30 30 30 30 31 30 30 0a 23 64 65    0x00000100.#de
9690: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
96a0: 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 20 20 20  P_SAFE_APPEND   
96b0: 20 20 30 78 30 30 30 30 30 32 30 30 0a 23 64 65    0x00000200.#de
96c0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
96d0: 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 20 20 20  P_SEQUENTIAL    
96e0: 20 20 30 78 30 30 30 30 30 34 30 30 0a 0a 2f 2a    0x00000400../*
96f0: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 69  .** CAPI3REF: Fi
9700: 6c 65 20 4c 6f 63 6b 69 6e 67 20 4c 65 76 65 6c  le Locking Level
9710: 73 20 7b 48 31 30 32 35 30 7d 20 3c 48 31 31 31  s {H10250} <H111
9720: 32 30 3e 20 3c 48 31 31 33 31 30 3e 0a 2a 2a 0a  20> <H11310>.**.
9730: 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 73 20 6f  ** SQLite uses o
9740: 6e 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65  ne of these inte
9750: 67 65 72 20 76 61 6c 75 65 73 20 61 73 20 74 68  ger values as th
9760: 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 61 72 67 75  e second.** argu
9770: 6d 65 6e 74 20 74 6f 20 63 61 6c 6c 73 20 69 74  ment to calls it
9780: 20 6d 61 6b 65 73 20 74 6f 20 74 68 65 20 78 4c   makes to the xL
9790: 6f 63 6b 28 29 20 61 6e 64 20 78 55 6e 6c 6f 63  ock() and xUnloc
97a0: 6b 28 29 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f  k() methods.** o
97b0: 66 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 69 6f  f an [sqlite3_io
97c0: 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74  _methods] object
97d0: 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ..*/.#define SQL
97e0: 49 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45 20 20 20  ITE_LOCK_NONE   
97f0: 20 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65         0.#define
9800: 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 53 48 41   SQLITE_LOCK_SHA
9810: 52 45 44 20 20 20 20 20 20 20 20 31 0a 23 64 65  RED        1.#de
9820: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  fine SQLITE_LOCK
9830: 5f 52 45 53 45 52 56 45 44 20 20 20 20 20 20 32  _RESERVED      2
9840: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
9850: 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47 20 20 20 20  LOCK_PENDING    
9860: 20 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c     3.#define SQL
9870: 49 54 45 5f 4c 4f 43 4b 5f 45 58 43 4c 55 53 49  ITE_LOCK_EXCLUSI
9880: 56 45 20 20 20 20 20 34 0a 0a 2f 2a 0a 2a 2a 20  VE     4../*.** 
9890: 43 41 50 49 33 52 45 46 3a 20 53 79 6e 63 68 72  CAPI3REF: Synchr
98a0: 6f 6e 69 7a 61 74 69 6f 6e 20 54 79 70 65 20 46  onization Type F
98b0: 6c 61 67 73 20 7b 48 31 30 32 36 30 7d 20 3c 48  lags {H10260} <H
98c0: 31 31 31 32 30 3e 0a 2a 2a 0a 2a 2a 20 57 68 65  11120>.**.** Whe
98d0: 6e 20 53 51 4c 69 74 65 20 69 6e 76 6f 6b 65 73  n SQLite invokes
98e0: 20 74 68 65 20 78 53 79 6e 63 28 29 20 6d 65 74   the xSync() met
98f0: 68 6f 64 20 6f 66 20 61 6e 0a 2a 2a 20 5b 73 71  hod of an.** [sq
9900: 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
9910: 5d 20 6f 62 6a 65 63 74 20 69 74 20 75 73 65 73  ] object it uses
9920: 20 61 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f   a combination o
9930: 66 0a 2a 2a 20 74 68 65 73 65 20 69 6e 74 65 67  f.** these integ
9940: 65 72 20 76 61 6c 75 65 73 20 61 73 20 74 68 65  er values as the
9950: 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
9960: 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65  ..**.** When the
9970: 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54   SQLITE_SYNC_DAT
9980: 41 4f 4e 4c 59 20 66 6c 61 67 20 69 73 20 75 73  AONLY flag is us
9990: 65 64 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61  ed, it means tha
99a0: 74 20 74 68 65 0a 2a 2a 20 73 79 6e 63 20 6f 70  t the.** sync op
99b0: 65 72 61 74 69 6f 6e 20 6f 6e 6c 79 20 6e 65 65  eration only nee
99c0: 64 73 20 74 6f 20 66 6c 75 73 68 20 64 61 74 61  ds to flush data
99d0: 20 74 6f 20 6d 61 73 73 20 73 74 6f 72 61 67 65   to mass storage
99e0: 2e 20 20 49 6e 6f 64 65 0a 2a 2a 20 69 6e 66 6f  .  Inode.** info
99f0: 72 6d 61 74 69 6f 6e 20 6e 65 65 64 20 6e 6f 74  rmation need not
9a00: 20 62 65 20 66 6c 75 73 68 65 64 2e 20 49 66 20   be flushed. If 
9a10: 74 68 65 20 6c 6f 77 65 72 20 66 6f 75 72 20 62  the lower four b
9a20: 69 74 73 20 6f 66 20 74 68 65 20 66 6c 61 67 0a  its of the flag.
9a30: 2a 2a 20 65 71 75 61 6c 20 53 51 4c 49 54 45 5f  ** equal SQLITE_
9a40: 53 59 4e 43 5f 4e 4f 52 4d 41 4c 2c 20 74 68 61  SYNC_NORMAL, tha
9a50: 74 20 6d 65 61 6e 73 20 74 6f 20 75 73 65 20 6e  t means to use n
9a60: 6f 72 6d 61 6c 20 66 73 79 6e 63 28 29 20 73 65  ormal fsync() se
9a70: 6d 61 6e 74 69 63 73 2e 0a 2a 2a 20 49 66 20 74  mantics..** If t
9a80: 68 65 20 6c 6f 77 65 72 20 66 6f 75 72 20 62 69  he lower four bi
9a90: 74 73 20 65 71 75 61 6c 20 53 51 4c 49 54 45 5f  ts equal SQLITE_
9aa0: 53 59 4e 43 5f 46 55 4c 4c 2c 20 74 68 61 74 20  SYNC_FULL, that 
9ab0: 6d 65 61 6e 73 0a 2a 2a 20 74 6f 20 75 73 65 20  means.** to use 
9ac0: 4d 61 63 20 4f 53 20 58 20 73 74 79 6c 65 20 66  Mac OS X style f
9ad0: 75 6c 6c 73 79 6e 63 20 69 6e 73 74 65 61 64 20  ullsync instead 
9ae0: 6f 66 20 66 73 79 6e 63 28 29 2e 0a 2a 2f 0a 23  of fsync()..*/.#
9af0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 59  define SQLITE_SY
9b00: 4e 43 5f 4e 4f 52 4d 41 4c 20 20 20 20 20 20 20  NC_NORMAL       
9b10: 20 30 78 30 30 30 30 32 0a 23 64 65 66 69 6e 65   0x00002.#define
9b20: 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c   SQLITE_SYNC_FUL
9b30: 4c 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30  L          0x000
9b40: 30 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  03.#define SQLIT
9b50: 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 20  E_SYNC_DATAONLY 
9b60: 20 20 20 20 20 30 78 30 30 30 31 30 0a 0a 2f 2a       0x00010../*
9b70: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 53  .** CAPI3REF: OS
9b80: 20 49 6e 74 65 72 66 61 63 65 20 4f 70 65 6e 20   Interface Open 
9b90: 46 69 6c 65 20 48 61 6e 64 6c 65 20 7b 48 31 31  File Handle {H11
9ba0: 31 31 30 7d 20 3c 53 32 30 31 31 30 3e 0a 2a 2a  110} <S20110>.**
9bb0: 0a 2a 2a 20 41 6e 20 5b 73 71 6c 69 74 65 33 5f  .** An [sqlite3_
9bc0: 66 69 6c 65 5d 20 6f 62 6a 65 63 74 20 72 65 70  file] object rep
9bd0: 72 65 73 65 6e 74 73 20 61 6e 20 6f 70 65 6e 20  resents an open 
9be0: 66 69 6c 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20  file in the .** 
9bf0: 5b 73 71 6c 69 74 65 33 5f 76 66 73 20 7c 20 4f  [sqlite3_vfs | O
9c00: 53 20 69 6e 74 65 72 66 61 63 65 20 6c 61 79 65  S interface laye
9c10: 72 5d 2e 20 20 49 6e 64 69 76 69 64 75 61 6c 20  r].  Individual 
9c20: 4f 53 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20  OS interface.** 
9c30: 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
9c40: 77 69 6c 6c 0a 2a 2a 20 77 61 6e 74 20 74 6f 20  will.** want to 
9c50: 73 75 62 63 6c 61 73 73 20 74 68 69 73 20 6f 62  subclass this ob
9c60: 6a 65 63 74 20 62 79 20 61 70 70 65 6e 64 69 6e  ject by appendin
9c70: 67 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 69 65  g additional fie
9c80: 6c 64 73 0a 2a 2a 20 66 6f 72 20 74 68 65 69 72  lds.** for their
9c90: 20 6f 77 6e 20 75 73 65 2e 20 20 54 68 65 20 70   own use.  The p
9ca0: 4d 65 74 68 6f 64 73 20 65 6e 74 72 79 20 69 73  Methods entry is
9cb0: 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
9cc0: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f  .** [sqlite3_io_
9cd0: 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20  methods] object 
9ce0: 74 68 61 74 20 64 65 66 69 6e 65 73 20 6d 65 74  that defines met
9cf0: 68 6f 64 73 20 66 6f 72 20 70 65 72 66 6f 72 6d  hods for perform
9d00: 69 6e 67 0a 2a 2a 20 49 2f 4f 20 6f 70 65 72 61  ing.** I/O opera
9d10: 74 69 6f 6e 73 20 6f 6e 20 74 68 65 20 6f 70 65  tions on the ope
9d20: 6e 20 66 69 6c 65 2e 0a 2a 2f 0a 74 79 70 65 64  n file..*/.typed
9d30: 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  ef struct sqlite
9d40: 33 5f 66 69 6c 65 20 73 71 6c 69 74 65 33 5f 66  3_file sqlite3_f
9d50: 69 6c 65 3b 0a 73 74 72 75 63 74 20 73 71 6c 69  ile;.struct sqli
9d60: 74 65 33 5f 66 69 6c 65 20 7b 0a 20 20 63 6f 6e  te3_file {.  con
9d70: 73 74 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  st struct sqlite
9d80: 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d  3_io_methods *pM
9d90: 65 74 68 6f 64 73 3b 20 20 2f 2a 20 4d 65 74 68  ethods;  /* Meth
9da0: 6f 64 73 20 66 6f 72 20 61 6e 20 6f 70 65 6e 20  ods for an open 
9db0: 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  file */.};../*.*
9dc0: 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 53 20 49  * CAPI3REF: OS I
9dd0: 6e 74 65 72 66 61 63 65 20 46 69 6c 65 20 56 69  nterface File Vi
9de0: 72 74 75 61 6c 20 4d 65 74 68 6f 64 73 20 4f 62  rtual Methods Ob
9df0: 6a 65 63 74 20 7b 48 31 31 31 32 30 7d 20 3c 53  ject {H11120} <S
9e00: 32 30 31 31 30 3e 0a 2a 2a 0a 2a 2a 20 45 76 65  20110>.**.** Eve
9e10: 72 79 20 66 69 6c 65 20 6f 70 65 6e 65 64 20 62  ry file opened b
9e20: 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76  y the [sqlite3_v
9e30: 66 73 5d 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64  fs] xOpen method
9e40: 20 70 6f 70 75 6c 61 74 65 73 20 61 6e 0a 2a 2a   populates an.**
9e50: 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20   [sqlite3_file] 
9e60: 6f 62 6a 65 63 74 20 28 6f 72 2c 20 6d 6f 72 65  object (or, more
9e70: 20 63 6f 6d 6d 6f 6e 6c 79 2c 20 61 20 73 75 62   commonly, a sub
9e80: 63 6c 61 73 73 20 6f 66 20 74 68 65 0a 2a 2a 20  class of the.** 
9e90: 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f  [sqlite3_file] o
9ea0: 62 6a 65 63 74 29 20 77 69 74 68 20 61 20 70 6f  bject) with a po
9eb0: 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74  inter to an inst
9ec0: 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a  ance of this obj
9ed0: 65 63 74 2e 0a 2a 2a 20 54 68 69 73 20 6f 62 6a  ect..** This obj
9ee0: 65 63 74 20 64 65 66 69 6e 65 73 20 74 68 65 20  ect defines the 
9ef0: 6d 65 74 68 6f 64 73 20 75 73 65 64 20 74 6f 20  methods used to 
9f00: 70 65 72 66 6f 72 6d 20 76 61 72 69 6f 75 73 20  perform various 
9f10: 6f 70 65 72 61 74 69 6f 6e 73 0a 2a 2a 20 61 67  operations.** ag
9f20: 61 69 6e 73 74 20 74 68 65 20 6f 70 65 6e 20 66  ainst the open f
9f30: 69 6c 65 20 72 65 70 72 65 73 65 6e 74 65 64 20  ile represented 
9f40: 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  by the [sqlite3_
9f50: 66 69 6c 65 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a  file] object..**
9f60: 0a 2a 2a 20 49 66 20 74 68 65 20 78 4f 70 65 6e  .** If the xOpen
9f70: 20 6d 65 74 68 6f 64 20 73 65 74 73 20 74 68 65   method sets the
9f80: 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2e 70 4d   sqlite3_file.pM
9f90: 65 74 68 6f 64 73 20 65 6c 65 6d 65 6e 74 20 0a  ethods element .
9fa0: 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 4e 55 4c 4c  ** to a non-NULL
9fb0: 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 6e 20 74   pointer, then t
9fc0: 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  he sqlite3_io_me
9fd0: 74 68 6f 64 73 2e 78 43 6c 6f 73 65 20 6d 65 74  thods.xClose met
9fe0: 68 6f 64 0a 2a 2a 20 6d 61 79 20 62 65 20 69 6e  hod.** may be in
9ff0: 76 6f 6b 65 64 20 65 76 65 6e 20 69 66 20 74 68  voked even if th
a000: 65 20 78 4f 70 65 6e 20 72 65 70 6f 72 74 65 64  e xOpen reported
a010: 20 74 68 61 74 20 69 74 20 66 61 69 6c 65 64 2e   that it failed.
a020: 20 20 54 68 65 0a 2a 2a 20 6f 6e 6c 79 20 77 61    The.** only wa
a030: 79 20 74 6f 20 70 72 65 76 65 6e 74 20 61 20 63  y to prevent a c
a040: 61 6c 6c 20 74 6f 20 78 43 6c 6f 73 65 20 66 6f  all to xClose fo
a050: 6c 6c 6f 77 69 6e 67 20 61 20 66 61 69 6c 65 64  llowing a failed
a060: 20 78 4f 70 65 6e 0a 2a 2a 20 69 73 20 66 6f 72   xOpen.** is for
a070: 20 74 68 65 20 78 4f 70 65 6e 20 74 6f 20 73 65   the xOpen to se
a080: 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69  t the sqlite3_fi
a090: 6c 65 2e 70 4d 65 74 68 6f 64 73 20 65 6c 65 6d  le.pMethods elem
a0a0: 65 6e 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a  ent to NULL..**.
a0b0: 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 61 72 67  ** The flags arg
a0c0: 75 6d 65 6e 74 20 74 6f 20 78 53 79 6e 63 20 6d  ument to xSync m
a0d0: 61 79 20 62 65 20 6f 6e 65 20 6f 66 20 5b 53 51  ay be one of [SQ
a0e0: 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c  LITE_SYNC_NORMAL
a0f0: 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  ] or.** [SQLITE_
a100: 53 59 4e 43 5f 46 55 4c 4c 5d 2e 20 20 54 68 65  SYNC_FULL].  The
a110: 20 66 69 72 73 74 20 63 68 6f 69 63 65 20 69 73   first choice is
a120: 20 74 68 65 20 6e 6f 72 6d 61 6c 20 66 73 79 6e   the normal fsyn
a130: 63 28 29 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f  c()..** The seco
a140: 6e 64 20 63 68 6f 69 63 65 20 69 73 20 61 20 4d  nd choice is a M
a150: 61 63 20 4f 53 20 58 20 73 74 79 6c 65 20 66 75  ac OS X style fu
a160: 6c 6c 73 79 6e 63 2e 20 20 54 68 65 20 5b 53 51  llsync.  The [SQ
a170: 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e  LITE_SYNC_DATAON
a180: 4c 59 5d 0a 2a 2a 20 66 6c 61 67 20 6d 61 79 20  LY].** flag may 
a190: 62 65 20 4f 52 65 64 20 69 6e 20 74 6f 20 69 6e  be ORed in to in
a1a0: 64 69 63 61 74 65 20 74 68 61 74 20 6f 6e 6c 79  dicate that only
a1b0: 20 74 68 65 20 64 61 74 61 20 6f 66 20 74 68 65   the data of the
a1c0: 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 6e 6f 74   file.** and not
a1d0: 20 69 74 73 20 69 6e 6f 64 65 20 6e 65 65 64 73   its inode needs
a1e0: 20 74 6f 20 62 65 20 73 79 6e 63 65 64 2e 0a 2a   to be synced..*
a1f0: 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 67 65 72  *.** The integer
a200: 20 76 61 6c 75 65 73 20 74 6f 20 78 4c 6f 63 6b   values to xLock
a210: 28 29 20 61 6e 64 20 78 55 6e 6c 6f 63 6b 28 29  () and xUnlock()
a220: 20 61 72 65 20 6f 6e 65 20 6f 66 0a 2a 2a 20 3c   are one of.** <
a230: 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c  ul>.** <li> [SQL
a240: 49 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45 5d 2c 0a  ITE_LOCK_NONE],.
a250: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f  ** <li> [SQLITE_
a260: 4c 4f 43 4b 5f 53 48 41 52 45 44 5d 2c 0a 2a 2a  LOCK_SHARED],.**
a270: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f   <li> [SQLITE_LO
a280: 43 4b 5f 52 45 53 45 52 56 45 44 5d 2c 0a 2a 2a  CK_RESERVED],.**
a290: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f   <li> [SQLITE_LO
a2a0: 43 4b 5f 50 45 4e 44 49 4e 47 5d 2c 20 6f 72 0a  CK_PENDING], or.
a2b0: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f  ** <li> [SQLITE_
a2c0: 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 56 45 5d 2e  LOCK_EXCLUSIVE].
a2d0: 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 20 78 4c 6f  .** </ul>.** xLo
a2e0: 63 6b 28 29 20 69 6e 63 72 65 61 73 65 73 20 74  ck() increases t
a2f0: 68 65 20 6c 6f 63 6b 2e 20 78 55 6e 6c 6f 63 6b  he lock. xUnlock
a300: 28 29 20 64 65 63 72 65 61 73 65 73 20 74 68 65  () decreases the
a310: 20 6c 6f 63 6b 2e 0a 2a 2a 20 54 68 65 20 78 43   lock..** The xC
a320: 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
a330: 28 29 20 6d 65 74 68 6f 64 20 63 68 65 63 6b 73  () method checks
a340: 20 77 68 65 74 68 65 72 20 61 6e 79 20 64 61 74   whether any dat
a350: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
a360: 2c 0a 2a 2a 20 65 69 74 68 65 72 20 69 6e 20 74  ,.** either in t
a370: 68 69 73 20 70 72 6f 63 65 73 73 20 6f 72 20 69  his process or i
a380: 6e 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f  n some other pro
a390: 63 65 73 73 2c 20 69 73 20 68 6f 6c 64 69 6e 67  cess, is holding
a3a0: 20 61 20 52 45 53 45 52 56 45 44 2c 0a 2a 2a 20   a RESERVED,.** 
a3b0: 50 45 4e 44 49 4e 47 2c 20 6f 72 20 45 58 43 4c  PENDING, or EXCL
a3c0: 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68  USIVE lock on th
a3d0: 65 20 66 69 6c 65 2e 20 20 49 74 20 72 65 74 75  e file.  It retu
a3e0: 72 6e 73 20 74 72 75 65 0a 2a 2a 20 69 66 20 73  rns true.** if s
a3f0: 75 63 68 20 61 20 6c 6f 63 6b 20 65 78 69 73 74  uch a lock exist
a400: 73 20 61 6e 64 20 66 61 6c 73 65 20 6f 74 68 65  s and false othe
a410: 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  rwise..**.** The
a420: 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 29 20   xFileControl() 
a430: 6d 65 74 68 6f 64 20 69 73 20 61 20 67 65 6e 65  method is a gene
a440: 72 69 63 20 69 6e 74 65 72 66 61 63 65 20 74 68  ric interface th
a450: 61 74 20 61 6c 6c 6f 77 73 20 63 75 73 74 6f 6d  at allows custom
a460: 0a 2a 2a 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e  .** VFS implemen
a470: 74 61 74 69 6f 6e 73 20 74 6f 20 64 69 72 65 63  tations to direc
a480: 74 6c 79 20 63 6f 6e 74 72 6f 6c 20 61 6e 20 6f  tly control an o
a490: 70 65 6e 20 66 69 6c 65 20 75 73 69 6e 67 20 74  pen file using t
a4a0: 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66  he.** [sqlite3_f
a4b0: 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 69  ile_control()] i
a4c0: 6e 74 65 72 66 61 63 65 2e 20 20 54 68 65 20 73  nterface.  The s
a4d0: 65 63 6f 6e 64 20 22 6f 70 22 20 61 72 67 75 6d  econd "op" argum
a4e0: 65 6e 74 20 69 73 20 61 6e 0a 2a 2a 20 69 6e 74  ent is an.** int
a4f0: 65 67 65 72 20 6f 70 63 6f 64 65 2e 20 20 54 68  eger opcode.  Th
a500: 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74  e third argument
a510: 20 69 73 20 61 20 67 65 6e 65 72 69 63 20 70 6f   is a generic po
a520: 69 6e 74 65 72 20 69 6e 74 65 6e 64 65 64 20 74  inter intended t
a530: 6f 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 61 20  o.** point to a 
a540: 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 6d  structure that m
a550: 61 79 20 63 6f 6e 74 61 69 6e 20 61 72 67 75 6d  ay contain argum
a560: 65 6e 74 73 20 6f 72 20 73 70 61 63 65 20 69 6e  ents or space in
a570: 20 77 68 69 63 68 20 74 6f 0a 2a 2a 20 77 72 69   which to.** wri
a580: 74 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 73  te return values
a590: 2e 20 20 50 6f 74 65 6e 74 69 61 6c 20 75 73 65  .  Potential use
a5a0: 73 20 66 6f 72 20 78 46 69 6c 65 43 6f 6e 74 72  s for xFileContr
a5b0: 6f 6c 28 29 20 6d 69 67 68 74 20 62 65 0a 2a 2a  ol() might be.**
a5c0: 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 65 6e   functions to en
a5d0: 61 62 6c 65 20 62 6c 6f 63 6b 69 6e 67 20 6c 6f  able blocking lo
a5e0: 63 6b 73 20 77 69 74 68 20 74 69 6d 65 6f 75 74  cks with timeout
a5f0: 73 2c 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65  s, to change the
a600: 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61  .** locking stra
a610: 74 65 67 79 20 28 66 6f 72 20 65 78 61 6d 70 6c  tegy (for exampl
a620: 65 20 74 6f 20 75 73 65 20 64 6f 74 2d 66 69 6c  e to use dot-fil
a630: 65 20 6c 6f 63 6b 73 29 2c 20 74 6f 20 69 6e 71  e locks), to inq
a640: 75 69 72 65 0a 2a 2a 20 61 62 6f 75 74 20 74 68  uire.** about th
a650: 65 20 73 74 61 74 75 73 20 6f 66 20 61 20 6c 6f  e status of a lo
a660: 63 6b 2c 20 6f 72 20 74 6f 20 62 72 65 61 6b 20  ck, or to break 
a670: 73 74 61 6c 65 20 6c 6f 63 6b 73 2e 20 20 54 68  stale locks.  Th
a680: 65 20 53 51 4c 69 74 65 0a 2a 2a 20 63 6f 72 65  e SQLite.** core
a690: 20 72 65 73 65 72 76 65 73 20 61 6c 6c 20 6f 70   reserves all op
a6a0: 63 6f 64 65 73 20 6c 65 73 73 20 74 68 61 6e 20  codes less than 
a6b0: 31 30 30 20 66 6f 72 20 69 74 73 20 6f 77 6e 20  100 for its own 
a6c0: 75 73 65 2e 0a 2a 2a 20 41 20 5b 53 51 4c 49 54  use..** A [SQLIT
a6d0: 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54  E_FCNTL_LOCKSTAT
a6e0: 45 20 7c 20 6c 69 73 74 20 6f 66 20 6f 70 63 6f  E | list of opco
a6f0: 64 65 73 5d 20 6c 65 73 73 20 74 68 61 6e 20 31  des] less than 1
a700: 30 30 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e  00 is available.
a710: 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f 6e 73  .** Applications
a720: 20 74 68 61 74 20 64 65 66 69 6e 65 20 61 20 63   that define a c
a730: 75 73 74 6f 6d 20 78 46 69 6c 65 43 6f 6e 74 72  ustom xFileContr
a740: 6f 6c 20 6d 65 74 68 6f 64 20 73 68 6f 75 6c 64  ol method should
a750: 20 75 73 65 20 6f 70 63 6f 64 65 73 0a 2a 2a 20   use opcodes.** 
a760: 67 72 65 61 74 65 72 20 74 68 61 6e 20 31 30 30  greater than 100
a770: 20 74 6f 20 61 76 6f 69 64 20 63 6f 6e 66 6c 69   to avoid confli
a780: 63 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78  cts..**.** The x
a790: 53 65 63 74 6f 72 53 69 7a 65 28 29 20 6d 65 74  SectorSize() met
a7a0: 68 6f 64 20 72 65 74 75 72 6e 73 20 74 68 65 20  hod returns the 
a7b0: 73 65 63 74 6f 72 20 73 69 7a 65 20 6f 66 20 74  sector size of t
a7c0: 68 65 0a 2a 2a 20 64 65 76 69 63 65 20 74 68 61  he.** device tha
a7d0: 74 20 75 6e 64 65 72 6c 69 65 73 20 74 68 65 20  t underlies the 
a7e0: 66 69 6c 65 2e 20 20 54 68 65 20 73 65 63 74 6f  file.  The secto
a7f0: 72 20 73 69 7a 65 20 69 73 20 74 68 65 0a 2a 2a  r size is the.**
a800: 20 6d 69 6e 69 6d 75 6d 20 77 72 69 74 65 20 74   minimum write t
a810: 68 61 74 20 63 61 6e 20 62 65 20 70 65 72 66 6f  hat can be perfo
a820: 72 6d 65 64 20 77 69 74 68 6f 75 74 20 64 69 73  rmed without dis
a830: 74 75 72 62 69 6e 67 0a 2a 2a 20 6f 74 68 65 72  turbing.** other
a840: 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 66 69   bytes in the fi
a850: 6c 65 2e 20 20 54 68 65 20 78 44 65 76 69 63 65  le.  The xDevice
a860: 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28  Characteristics(
a870: 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 72 65 74 75  ).** method retu
a880: 72 6e 73 20 61 20 62 69 74 20 76 65 63 74 6f 72  rns a bit vector
a890: 20 64 65 73 63 72 69 62 69 6e 67 20 62 65 68 61   describing beha
a8a0: 76 69 6f 72 73 20 6f 66 20 74 68 65 0a 2a 2a 20  viors of the.** 
a8b0: 75 6e 64 65 72 6c 79 69 6e 67 20 64 65 76 69 63  underlying devic
a8c0: 65 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a  e:.**.** <ul>.**
a8d0: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f   <li> [SQLITE_IO
a8e0: 43 41 50 5f 41 54 4f 4d 49 43 5d 0a 2a 2a 20 3c  CAP_ATOMIC].** <
a8f0: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41  li> [SQLITE_IOCA
a900: 50 5f 41 54 4f 4d 49 43 35 31 32 5d 0a 2a 2a 20  P_ATOMIC512].** 
a910: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43  <li> [SQLITE_IOC
a920: 41 50 5f 41 54 4f 4d 49 43 31 4b 5d 0a 2a 2a 20  AP_ATOMIC1K].** 
a930: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43  <li> [SQLITE_IOC
a940: 41 50 5f 41 54 4f 4d 49 43 32 4b 5d 0a 2a 2a 20  AP_ATOMIC2K].** 
a950: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43  <li> [SQLITE_IOC
a960: 41 50 5f 41 54 4f 4d 49 43 34 4b 5d 0a 2a 2a 20  AP_ATOMIC4K].** 
a970: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43  <li> [SQLITE_IOC
a980: 41 50 5f 41 54 4f 4d 49 43 38 4b 5d 0a 2a 2a 20  AP_ATOMIC8K].** 
a990: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43  <li> [SQLITE_IOC
a9a0: 41 50 5f 41 54 4f 4d 49 43 31 36 4b 5d 0a 2a 2a  AP_ATOMIC16K].**
a9b0: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f   <li> [SQLITE_IO
a9c0: 43 41 50 5f 41 54 4f 4d 49 43 33 32 4b 5d 0a 2a  CAP_ATOMIC32K].*
a9d0: 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49  * <li> [SQLITE_I
a9e0: 4f 43 41 50 5f 41 54 4f 4d 49 43 36 34 4b 5d 0a  OCAP_ATOMIC64K].
a9f0: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f  ** <li> [SQLITE_
aa00: 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e  IOCAP_SAFE_APPEN
aa10: 44 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49  D].** <li> [SQLI
aa20: 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54  TE_IOCAP_SEQUENT
aa30: 49 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a  IAL].** </ul>.**
aa40: 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 49  .** The SQLITE_I
aa50: 4f 43 41 50 5f 41 54 4f 4d 49 43 20 70 72 6f 70  OCAP_ATOMIC prop
aa60: 65 72 74 79 20 6d 65 61 6e 73 20 74 68 61 74 20  erty means that 
aa70: 61 6c 6c 20 77 72 69 74 65 73 20 6f 66 0a 2a 2a  all writes of.**
aa80: 20 61 6e 79 20 73 69 7a 65 20 61 72 65 20 61 74   any size are at
aa90: 6f 6d 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54  omic.  The SQLIT
aaa0: 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 6e 6e  E_IOCAP_ATOMICnn
aab0: 6e 20 76 61 6c 75 65 73 0a 2a 2a 20 6d 65 61 6e  n values.** mean
aac0: 20 74 68 61 74 20 77 72 69 74 65 73 20 6f 66 20   that writes of 
aad0: 62 6c 6f 63 6b 73 20 74 68 61 74 20 61 72 65 20  blocks that are 
aae0: 6e 6e 6e 20 62 79 74 65 73 20 69 6e 20 73 69 7a  nnn bytes in siz
aaf0: 65 20 61 6e 64 0a 2a 2a 20 61 72 65 20 61 6c 69  e and.** are ali
ab00: 67 6e 65 64 20 74 6f 20 61 6e 20 61 64 64 72 65  gned to an addre
ab10: 73 73 20 77 68 69 63 68 20 69 73 20 61 6e 20 69  ss which is an i
ab20: 6e 74 65 67 65 72 20 6d 75 6c 74 69 70 6c 65 20  nteger multiple 
ab30: 6f 66 0a 2a 2a 20 6e 6e 6e 20 61 72 65 20 61 74  of.** nnn are at
ab40: 6f 6d 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54  omic.  The SQLIT
ab50: 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50  E_IOCAP_SAFE_APP
ab60: 45 4e 44 20 76 61 6c 75 65 20 6d 65 61 6e 73 0a  END value means.
ab70: 2a 2a 20 74 68 61 74 20 77 68 65 6e 20 64 61 74  ** that when dat
ab80: 61 20 69 73 20 61 70 70 65 6e 64 65 64 20 74 6f  a is appended to
ab90: 20 61 20 66 69 6c 65 2c 20 74 68 65 20 64 61 74   a file, the dat
aba0: 61 20 69 73 20 61 70 70 65 6e 64 65 64 0a 2a 2a  a is appended.**
abb0: 20 66 69 72 73 74 20 74 68 65 6e 20 74 68 65 20   first then the 
abc0: 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65  size of the file
abd0: 20 69 73 20 65 78 74 65 6e 64 65 64 2c 20 6e 65   is extended, ne
abe0: 76 65 72 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a  ver the other.**
abf0: 20 77 61 79 20 61 72 6f 75 6e 64 2e 20 20 54 68   way around.  Th
ac00: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53  e SQLITE_IOCAP_S
ac10: 45 51 55 45 4e 54 49 41 4c 20 70 72 6f 70 65 72  EQUENTIAL proper
ac20: 74 79 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a  ty means that.**
ac30: 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20   information is 
ac40: 77 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 20  written to disk 
ac50: 69 6e 20 74 68 65 20 73 61 6d 65 20 6f 72 64 65  in the same orde
ac60: 72 20 61 73 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f  r as calls.** to
ac70: 20 78 57 72 69 74 65 28 29 2e 0a 2a 2a 0a 2a 2a   xWrite()..**.**
ac80: 20 49 66 20 78 52 65 61 64 28 29 20 72 65 74 75   If xRead() retu
ac90: 72 6e 73 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  rns SQLITE_IOERR
aca0: 5f 53 48 4f 52 54 5f 52 45 41 44 20 69 74 20 6d  _SHORT_READ it m
acb0: 75 73 74 20 61 6c 73 6f 20 66 69 6c 6c 0a 2a 2a  ust also fill.**
acc0: 20 69 6e 20 74 68 65 20 75 6e 72 65 61 64 20 70   in the unread p
acd0: 6f 72 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 62  ortions of the b
ace0: 75 66 66 65 72 20 77 69 74 68 20 7a 65 72 6f 73  uffer with zeros
acf0: 2e 20 20 41 20 56 46 53 20 74 68 61 74 0a 2a 2a  .  A VFS that.**
ad00: 20 66 61 69 6c 73 20 74 6f 20 7a 65 72 6f 2d 66   fails to zero-f
ad10: 69 6c 6c 20 73 68 6f 72 74 20 72 65 61 64 73 20  ill short reads 
ad20: 6d 69 67 68 74 20 73 65 65 6d 20 74 6f 20 77 6f  might seem to wo
ad30: 72 6b 2e 20 20 48 6f 77 65 76 65 72 2c 0a 2a 2a  rk.  However,.**
ad40: 20 66 61 69 6c 75 72 65 20 74 6f 20 7a 65 72 6f   failure to zero
ad50: 2d 66 69 6c 6c 20 73 68 6f 72 74 20 72 65 61 64  -fill short read
ad60: 73 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c  s will eventuall
ad70: 79 20 6c 65 61 64 20 74 6f 0a 2a 2a 20 64 61 74  y lead to.** dat
ad80: 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e  abase corruption
ad90: 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
ada0: 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  uct sqlite3_io_m
adb0: 65 74 68 6f 64 73 20 73 71 6c 69 74 65 33 5f 69  ethods sqlite3_i
adc0: 6f 5f 6d 65 74 68 6f 64 73 3b 0a 73 74 72 75 63  o_methods;.struc
add0: 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
ade0: 68 6f 64 73 20 7b 0a 20 20 69 6e 74 20 69 56 65  hods {.  int iVe
adf0: 72 73 69 6f 6e 3b 0a 20 20 69 6e 74 20 28 2a 78  rsion;.  int (*x
ae00: 43 6c 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 66  Close)(sqlite3_f
ae10: 69 6c 65 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78  ile*);.  int (*x
ae20: 52 65 61 64 29 28 73 71 6c 69 74 65 33 5f 66 69  Read)(sqlite3_fi
ae30: 6c 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20  le*, void*, int 
ae40: 69 41 6d 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e  iAmt, sqlite3_in
ae50: 74 36 34 20 69 4f 66 73 74 29 3b 0a 20 20 69 6e  t64 iOfst);.  in
ae60: 74 20 28 2a 78 57 72 69 74 65 29 28 73 71 6c 69  t (*xWrite)(sqli
ae70: 74 65 33 5f 66 69 6c 65 2a 2c 20 63 6f 6e 73 74  te3_file*, const
ae80: 20 76 6f 69 64 2a 2c 20 69 6e 74 20 69 41 6d 74   void*, int iAmt
ae90: 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  , sqlite3_int64 
aea0: 69 4f 66 73 74 29 3b 0a 20 20 69 6e 74 20 28 2a  iOfst);.  int (*
aeb0: 78 54 72 75 6e 63 61 74 65 29 28 73 71 6c 69 74  xTruncate)(sqlit
aec0: 65 33 5f 66 69 6c 65 2a 2c 20 73 71 6c 69 74 65  e3_file*, sqlite
aed0: 33 5f 69 6e 74 36 34 20 73 69 7a 65 29 3b 0a 20  3_int64 size);. 
aee0: 20 69 6e 74 20 28 2a 78 53 79 6e 63 29 28 73 71   int (*xSync)(sq
aef0: 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74  lite3_file*, int
af00: 20 66 6c 61 67 73 29 3b 0a 20 20 69 6e 74 20 28   flags);.  int (
af10: 2a 78 46 69 6c 65 53 69 7a 65 29 28 73 71 6c 69  *xFileSize)(sqli
af20: 74 65 33 5f 66 69 6c 65 2a 2c 20 73 71 6c 69 74  te3_file*, sqlit
af30: 65 33 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 29  e3_int64 *pSize)
af40: 3b 0a 20 20 69 6e 74 20 28 2a 78 4c 6f 63 6b 29  ;.  int (*xLock)
af50: 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20  (sqlite3_file*, 
af60: 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 55  int);.  int (*xU
af70: 6e 6c 6f 63 6b 29 28 73 71 6c 69 74 65 33 5f 66  nlock)(sqlite3_f
af80: 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e  ile*, int);.  in
af90: 74 20 28 2a 78 43 68 65 63 6b 52 65 73 65 72 76  t (*xCheckReserv
afa0: 65 64 4c 6f 63 6b 29 28 73 71 6c 69 74 65 33 5f  edLock)(sqlite3_
afb0: 66 69 6c 65 2a 2c 20 69 6e 74 20 2a 70 52 65 73  file*, int *pRes
afc0: 4f 75 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46  Out);.  int (*xF
afd0: 69 6c 65 43 6f 6e 74 72 6f 6c 29 28 73 71 6c 69  ileControl)(sqli
afe0: 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 6f  te3_file*, int o
aff0: 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 3b 0a  p, void *pArg);.
b000: 20 20 69 6e 74 20 28 2a 78 53 65 63 74 6f 72 53    int (*xSectorS
b010: 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c  ize)(sqlite3_fil
b020: 65 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 44 65  e*);.  int (*xDe
b030: 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74  viceCharacterist
b040: 69 63 73 29 28 73 71 6c 69 74 65 33 5f 66 69 6c  ics)(sqlite3_fil
b050: 65 2a 29 3b 0a 20 20 2f 2a 20 41 64 64 69 74 69  e*);.  /* Additi
b060: 6f 6e 61 6c 20 6d 65 74 68 6f 64 73 20 6d 61 79  onal methods may
b070: 20 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 74   be added in fut
b080: 75 72 65 20 72 65 6c 65 61 73 65 73 20 2a 2f 0a  ure releases */.
b090: 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  };../*.** CAPI3R
b0a0: 45 46 3a 20 53 74 61 6e 64 61 72 64 20 46 69 6c  EF: Standard Fil
b0b0: 65 20 43 6f 6e 74 72 6f 6c 20 4f 70 63 6f 64 65  e Control Opcode
b0c0: 73 20 7b 48 31 31 33 31 30 7d 20 3c 53 33 30 38  s {H11310} <S308
b0d0: 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  00>.**.** These 
b0e0: 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74  integer constant
b0f0: 73 20 61 72 65 20 6f 70 63 6f 64 65 73 20 66 6f  s are opcodes fo
b100: 72 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 72  r the xFileContr
b110: 6f 6c 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20  ol method.** of 
b120: 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f  the [sqlite3_io_
b130: 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20  methods] object 
b140: 61 6e 64 20 66 6f 72 20 74 68 65 20 5b 73 71 6c  and for the [sql
b150: 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f  ite3_file_contro
b160: 6c 28 29 5d 0a 2a 2a 20 69 6e 74 65 72 66 61 63  l()].** interfac
b170: 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 53 51  e..**.** The [SQ
b180: 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53  LITE_FCNTL_LOCKS
b190: 54 41 54 45 5d 20 6f 70 63 6f 64 65 20 69 73 20  TATE] opcode is 
b1a0: 75 73 65 64 20 66 6f 72 20 64 65 62 75 67 67 69  used for debuggi
b1b0: 6e 67 2e 20 20 54 68 69 73 0a 2a 2a 20 6f 70 63  ng.  This.** opc
b1c0: 6f 64 65 20 63 61 75 73 65 73 20 74 68 65 20 78  ode causes the x
b1d0: 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68  FileControl meth
b1e0: 6f 64 20 74 6f 20 77 72 69 74 65 20 74 68 65 20  od to write the 
b1f0: 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66  current state of
b200: 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 20 28 6f 6e  .** the lock (on
b210: 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 4c 4f 43  e of [SQLITE_LOC
b220: 4b 5f 4e 4f 4e 45 5d 2c 20 5b 53 51 4c 49 54 45  K_NONE], [SQLITE
b230: 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 5d 2c 0a 2a  _LOCK_SHARED],.*
b240: 2a 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52  * [SQLITE_LOCK_R
b250: 45 53 45 52 56 45 44 5d 2c 20 5b 53 51 4c 49 54  ESERVED], [SQLIT
b260: 45 5f 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47 5d 2c  E_LOCK_PENDING],
b270: 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b   or [SQLITE_LOCK
b280: 5f 45 58 43 4c 55 53 49 56 45 5d 29 0a 2a 2a 20  _EXCLUSIVE]).** 
b290: 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20  into an integer 
b2a0: 74 68 61 74 20 74 68 65 20 70 41 72 67 20 61 72  that the pArg ar
b2b0: 67 75 6d 65 6e 74 20 70 6f 69 6e 74 73 20 74 6f  gument points to
b2c0: 2e 20 54 68 69 73 20 63 61 70 61 62 69 6c 69 74  . This capabilit
b2d0: 79 0a 2a 2a 20 69 73 20 75 73 65 64 20 64 75 72  y.** is used dur
b2e0: 69 6e 67 20 74 65 73 74 69 6e 67 20 61 6e 64 20  ing testing and 
b2f0: 6f 6e 6c 79 20 6e 65 65 64 73 20 74 6f 20 62 65  only needs to be
b300: 20 73 75 70 70 6f 72 74 65 64 20 77 68 65 6e 20   supported when 
b310: 53 51 4c 49 54 45 5f 54 45 53 54 0a 2a 2a 20 69  SQLITE_TEST.** i
b320: 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 64  s defined..*/.#d
b330: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e  efine SQLITE_FCN
b340: 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 20 20 20 20  TL_LOCKSTATE    
b350: 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51      1.#define SQ
b360: 4c 49 54 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f  LITE_GET_LOCKPRO
b370: 58 59 46 49 4c 45 20 20 20 20 20 20 32 0a 23 64  XYFILE      2.#d
b380: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 45 54  efine SQLITE_SET
b390: 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 20 20  _LOCKPROXYFILE  
b3a0: 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51      3.#define SQ
b3b0: 4c 49 54 45 5f 4c 41 53 54 5f 45 52 52 4e 4f 20  LITE_LAST_ERRNO 
b3c0: 20 20 20 20 20 20 20 20 20 20 20 20 34 0a 0a 2f              4../
b3d0: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d  *.** CAPI3REF: M
b3e0: 75 74 65 78 20 48 61 6e 64 6c 65 20 7b 48 31 37  utex Handle {H17
b3f0: 31 31 30 7d 20 3c 53 32 30 31 33 30 3e 0a 2a 2a  110} <S20130>.**
b400: 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 6d 6f  .** The mutex mo
b410: 64 75 6c 65 20 77 69 74 68 69 6e 20 53 51 4c 69  dule within SQLi
b420: 74 65 20 64 65 66 69 6e 65 73 20 5b 73 71 6c 69  te defines [sqli
b430: 74 65 33 5f 6d 75 74 65 78 5d 20 74 6f 20 62 65  te3_mutex] to be
b440: 20 61 6e 0a 2a 2a 20 61 62 73 74 72 61 63 74 20   an.** abstract 
b450: 74 79 70 65 20 66 6f 72 20 61 20 6d 75 74 65 78  type for a mutex
b460: 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 53 51   object.  The SQ
b470: 4c 69 74 65 20 63 6f 72 65 20 6e 65 76 65 72 20  Lite core never 
b480: 6c 6f 6f 6b 73 0a 2a 2a 20 61 74 20 74 68 65 20  looks.** at the 
b490: 69 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65  internal represe
b4a0: 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 5b 73  ntation of an [s
b4b0: 71 6c 69 74 65 33 5f 6d 75 74 65 78 5d 2e 20 20  qlite3_mutex].  
b4c0: 49 74 20 6f 6e 6c 79 0a 2a 2a 20 64 65 61 6c 73  It only.** deals
b4d0: 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74   with pointers t
b4e0: 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d  o the [sqlite3_m
b4f0: 75 74 65 78 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a  utex] object..**
b500: 0a 2a 2a 20 4d 75 74 65 78 65 73 20 61 72 65 20  .** Mutexes are 
b510: 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 5b 73  created using [s
b520: 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
b530: 6f 63 28 29 5d 2e 0a 2a 2f 0a 74 79 70 65 64 65  oc()]..*/.typede
b540: 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  f struct sqlite3
b550: 5f 6d 75 74 65 78 20 73 71 6c 69 74 65 33 5f 6d  _mutex sqlite3_m
b560: 75 74 65 78 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  utex;../*.** CAP
b570: 49 33 52 45 46 3a 20 4f 53 20 49 6e 74 65 72 66  I3REF: OS Interf
b580: 61 63 65 20 4f 62 6a 65 63 74 20 7b 48 31 31 31  ace Object {H111
b590: 34 30 7d 20 3c 53 32 30 31 30 30 3e 0a 2a 2a 0a  40} <S20100>.**.
b5a0: 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
b5b0: 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 66  f the sqlite3_vf
b5c0: 73 20 6f 62 6a 65 63 74 20 64 65 66 69 6e 65 73  s object defines
b5d0: 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 62   the interface b
b5e0: 65 74 77 65 65 6e 0a 2a 2a 20 74 68 65 20 53 51  etween.** the SQ
b5f0: 4c 69 74 65 20 63 6f 72 65 20 61 6e 64 20 74 68  Lite core and th
b600: 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65  e underlying ope
b610: 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 20  rating system.  
b620: 54 68 65 20 22 76 66 73 22 0a 2a 2a 20 69 6e 20  The "vfs".** in 
b630: 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
b640: 6f 62 6a 65 63 74 20 73 74 61 6e 64 73 20 66 6f  object stands fo
b650: 72 20 22 76 69 72 74 75 61 6c 20 66 69 6c 65 20  r "virtual file 
b660: 73 79 73 74 65 6d 22 2e 0a 2a 2a 0a 2a 2a 20 54  system"..**.** T
b670: 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
b680: 69 56 65 72 73 69 6f 6e 20 66 69 65 6c 64 20 69  iVersion field i
b690: 73 20 69 6e 69 74 69 61 6c 6c 79 20 31 20 62 75  s initially 1 bu
b6a0: 74 20 6d 61 79 20 62 65 20 6c 61 72 67 65 72 20  t may be larger 
b6b0: 69 6e 0a 2a 2a 20 66 75 74 75 72 65 20 76 65 72  in.** future ver
b6c0: 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2e  sions of SQLite.
b6d0: 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 66 69 65    Additional fie
b6e0: 6c 64 73 20 6d 61 79 20 62 65 20 61 70 70 65 6e  lds may be appen
b6f0: 64 65 64 20 74 6f 20 74 68 69 73 0a 2a 2a 20 6f  ded to this.** o
b700: 62 6a 65 63 74 20 77 68 65 6e 20 74 68 65 20 69  bject when the i
b710: 56 65 72 73 69 6f 6e 20 76 61 6c 75 65 20 69 73  Version value is
b720: 20 69 6e 63 72 65 61 73 65 64 2e 20 20 4e 6f 74   increased.  Not
b730: 65 20 74 68 61 74 20 74 68 65 20 73 74 72 75 63  e that the struc
b740: 74 75 72 65 0a 2a 2a 20 6f 66 20 74 68 65 20 73  ture.** of the s
b750: 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63  qlite3_vfs objec
b760: 74 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68 65  t changes in the
b770: 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 74   transaction bet
b780: 77 65 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 76  ween.** SQLite v
b790: 65 72 73 69 6f 6e 20 33 2e 35 2e 39 20 61 6e 64  ersion 3.5.9 and
b7a0: 20 33 2e 36 2e 30 20 61 6e 64 20 79 65 74 20 74   3.6.0 and yet t
b7b0: 68 65 20 69 56 65 72 73 69 6f 6e 20 66 69 65 6c  he iVersion fiel
b7c0: 64 20 77 61 73 20 6e 6f 74 0a 2a 2a 20 6d 6f 64  d was not.** mod
b7d0: 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ified..**.** The
b7e0: 20 73 7a 4f 73 46 69 6c 65 20 66 69 65 6c 64 20   szOsFile field 
b7f0: 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  is the size of t
b800: 68 65 20 73 75 62 63 6c 61 73 73 65 64 20 5b 73  he subclassed [s
b810: 71 6c 69 74 65 33 5f 66 69 6c 65 5d 0a 2a 2a 20  qlite3_file].** 
b820: 73 74 72 75 63 74 75 72 65 20 75 73 65 64 20 62  structure used b
b830: 79 20 74 68 69 73 20 56 46 53 2e 20 20 6d 78 50  y this VFS.  mxP
b840: 61 74 68 6e 61 6d 65 20 69 73 20 74 68 65 20 6d  athname is the m
b850: 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66  aximum length of
b860: 0a 2a 2a 20 61 20 70 61 74 68 6e 61 6d 65 20 69  .** a pathname i
b870: 6e 20 74 68 69 73 20 56 46 53 2e 0a 2a 2a 0a 2a  n this VFS..**.*
b880: 2a 20 52 65 67 69 73 74 65 72 65 64 20 73 71 6c  * Registered sql
b890: 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 73  ite3_vfs objects
b8a0: 20 61 72 65 20 6b 65 70 74 20 6f 6e 20 61 20 6c   are kept on a l
b8b0: 69 6e 6b 65 64 20 6c 69 73 74 20 66 6f 72 6d 65  inked list forme
b8c0: 64 20 62 79 0a 2a 2a 20 74 68 65 20 70 4e 65 78  d by.** the pNex
b8d0: 74 20 70 6f 69 6e 74 65 72 2e 20 20 54 68 65 20  t pointer.  The 
b8e0: 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67  [sqlite3_vfs_reg
b8f0: 69 73 74 65 72 28 29 5d 0a 2a 2a 20 61 6e 64 20  ister()].** and 
b900: 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72  [sqlite3_vfs_unr
b910: 65 67 69 73 74 65 72 28 29 5d 20 69 6e 74 65 72  egister()] inter
b920: 66 61 63 65 73 20 6d 61 6e 61 67 65 20 74 68 69  faces manage thi
b930: 73 20 6c 69 73 74 0a 2a 2a 20 69 6e 20 61 20 74  s list.** in a t
b940: 68 72 65 61 64 2d 73 61 66 65 20 77 61 79 2e 20  hread-safe way. 
b950: 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66   The [sqlite3_vf
b960: 73 5f 66 69 6e 64 28 29 5d 20 69 6e 74 65 72 66  s_find()] interf
b970: 61 63 65 0a 2a 2a 20 73 65 61 72 63 68 65 73 20  ace.** searches 
b980: 74 68 65 20 6c 69 73 74 2e 20 20 4e 65 69 74 68  the list.  Neith
b990: 65 72 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  er the applicati
b9a0: 6f 6e 20 63 6f 64 65 20 6e 6f 72 20 74 68 65 20  on code nor the 
b9b0: 56 46 53 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  VFS.** implement
b9c0: 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 75 73 65  ation should use
b9d0: 20 74 68 65 20 70 4e 65 78 74 20 70 6f 69 6e 74   the pNext point
b9e0: 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 4e  er..**.** The pN
b9f0: 65 78 74 20 66 69 65 6c 64 20 69 73 20 74 68 65  ext field is the
ba00: 20 6f 6e 6c 79 20 66 69 65 6c 64 20 69 6e 20 74   only field in t
ba10: 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 0a 2a  he sqlite3_vfs.*
ba20: 2a 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  * structure that
ba30: 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 65 76 65   SQLite will eve
ba40: 72 20 6d 6f 64 69 66 79 2e 20 20 53 51 4c 69 74  r modify.  SQLit
ba50: 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 61 63 63 65  e will only acce
ba60: 73 73 0a 2a 2a 20 6f 72 20 6d 6f 64 69 66 79 20  ss.** or modify 
ba70: 74 68 69 73 20 66 69 65 6c 64 20 77 68 69 6c 65  this field while
ba80: 20 68 6f 6c 64 69 6e 67 20 61 20 70 61 72 74 69   holding a parti
ba90: 63 75 6c 61 72 20 73 74 61 74 69 63 20 6d 75 74  cular static mut
baa0: 65 78 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69  ex..** The appli
bab0: 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65  cation should ne
bac0: 76 65 72 20 6d 6f 64 69 66 79 20 61 6e 79 74 68  ver modify anyth
bad0: 69 6e 67 20 77 69 74 68 69 6e 20 74 68 65 20 73  ing within the s
bae0: 71 6c 69 74 65 33 5f 76 66 73 0a 2a 2a 20 6f 62  qlite3_vfs.** ob
baf0: 6a 65 63 74 20 6f 6e 63 65 20 74 68 65 20 6f 62  ject once the ob
bb00: 6a 65 63 74 20 68 61 73 20 62 65 65 6e 20 72 65  ject has been re
bb10: 67 69 73 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20  gistered..**.** 
bb20: 54 68 65 20 7a 4e 61 6d 65 20 66 69 65 6c 64 20  The zName field 
bb30: 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65 20 6f  holds the name o
bb40: 66 20 74 68 65 20 56 46 53 20 6d 6f 64 75 6c 65  f the VFS module
bb50: 2e 20 20 54 68 65 20 6e 61 6d 65 20 6d 75 73 74  .  The name must
bb60: 0a 2a 2a 20 62 65 20 75 6e 69 71 75 65 20 61 63  .** be unique ac
bb70: 72 6f 73 73 20 61 6c 6c 20 56 46 53 20 6d 6f 64  ross all VFS mod
bb80: 75 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69  ules..**.** SQLi
bb90: 74 65 20 77 69 6c 6c 20 67 75 61 72 61 6e 74 65  te will guarante
bba0: 65 20 74 68 61 74 20 74 68 65 20 7a 46 69 6c 65  e that the zFile
bbb0: 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20 74  name parameter t
bbc0: 6f 20 78 4f 70 65 6e 0a 2a 2a 20 69 73 20 65 69  o xOpen.** is ei
bbd0: 74 68 65 72 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  ther a NULL poin
bbe0: 74 65 72 20 6f 72 20 73 74 72 69 6e 67 20 6f 62  ter or string ob
bbf0: 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 78  tained.** from x
bc00: 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 29 2e 20  FullPathname(). 
bc10: 20 53 51 4c 69 74 65 20 66 75 72 74 68 65 72 20   SQLite further 
bc20: 67 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 0a  guarantees that.
bc30: 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 77 69  ** the string wi
bc40: 6c 6c 20 62 65 20 76 61 6c 69 64 20 61 6e 64 20  ll be valid and 
bc50: 75 6e 63 68 61 6e 67 65 64 20 75 6e 74 69 6c 20  unchanged until 
bc60: 78 43 6c 6f 73 65 28 29 20 69 73 0a 2a 2a 20 63  xClose() is.** c
bc70: 61 6c 6c 65 64 2e 20 42 65 63 61 75 73 65 20 6f  alled. Because o
bc80: 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 73  f the previous s
bc90: 65 6e 74 65 6e 63 65 2c 0a 2a 2a 20 74 68 65 20  entence,.** the 
bca0: 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 63  [sqlite3_file] c
bcb0: 61 6e 20 73 61 66 65 6c 79 20 73 74 6f 72 65 20  an safely store 
bcc0: 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
bcd0: 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65 20 69 66 20  .** filename if 
bce0: 69 74 20 6e 65 65 64 73 20 74 6f 20 72 65 6d 65  it needs to reme
bcf0: 6d 62 65 72 20 74 68 65 20 66 69 6c 65 6e 61 6d  mber the filenam
bd00: 65 20 66 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f  e for some reaso
bd10: 6e 2e 0a 2a 2a 20 49 66 20 74 68 65 20 7a 46 69  n..** If the zFi
bd20: 6c 65 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72  lename parameter
bd30: 20 69 73 20 78 4f 70 65 6e 20 69 73 20 61 20 4e   is xOpen is a N
bd40: 55 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e  ULL pointer then
bd50: 20 78 4f 70 65 6e 0a 2a 2a 20 6d 75 73 74 20 69   xOpen.** must i
bd60: 6e 76 65 6e 74 20 69 74 73 20 6f 77 6e 20 74 65  nvent its own te
bd70: 6d 70 6f 72 61 72 79 20 6e 61 6d 65 20 66 6f 72  mporary name for
bd80: 20 74 68 65 20 66 69 6c 65 2e 20 20 57 68 65 6e   the file.  When
bd90: 65 76 65 72 20 74 68 65 20 0a 2a 2a 20 78 46 69  ever the .** xFi
bda0: 6c 65 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72  lename parameter
bdb0: 20 69 73 20 4e 55 4c 4c 20 69 74 20 77 69 6c 6c   is NULL it will
bdc0: 20 61 6c 73 6f 20 62 65 20 74 68 65 20 63 61 73   also be the cas
bdd0: 65 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 66 6c  e that the.** fl
bde0: 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 77 69  ags parameter wi
bdf0: 6c 6c 20 69 6e 63 6c 75 64 65 20 5b 53 51 4c 49  ll include [SQLI
be00: 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e  TE_OPEN_DELETEON
be10: 43 4c 4f 53 45 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68  CLOSE]..**.** Th
be20: 65 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74  e flags argument
be30: 20 74 6f 20 78 4f 70 65 6e 28 29 20 69 6e 63 6c   to xOpen() incl
be40: 75 64 65 73 20 61 6c 6c 20 62 69 74 73 20 73 65  udes all bits se
be50: 74 20 69 6e 0a 2a 2a 20 74 68 65 20 66 6c 61 67  t in.** the flag
be60: 73 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 5b 73  s argument to [s
be70: 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29  qlite3_open_v2()
be80: 5d 2e 20 20 4f 72 20 69 66 20 5b 73 71 6c 69 74  ].  Or if [sqlit
be90: 65 33 5f 6f 70 65 6e 28 29 5d 0a 2a 2a 20 6f 72  e3_open()].** or
bea0: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36   [sqlite3_open16
beb0: 28 29 5d 20 69 73 20 75 73 65 64 2c 20 74 68 65  ()] is used, the
bec0: 6e 20 66 6c 61 67 73 20 69 6e 63 6c 75 64 65 73  n flags includes
bed0: 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 5b 53 51   at least.** [SQ
bee0: 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
bef0: 49 54 45 5d 20 7c 20 5b 53 51 4c 49 54 45 5f 4f  ITE] | [SQLITE_O
bf00: 50 45 4e 5f 43 52 45 41 54 45 5d 2e 20 0a 2a 2a  PEN_CREATE]. .**
bf10: 20 49 66 20 78 4f 70 65 6e 28 29 20 6f 70 65 6e   If xOpen() open
bf20: 73 20 61 20 66 69 6c 65 20 72 65 61 64 2d 6f 6e  s a file read-on
bf30: 6c 79 20 74 68 65 6e 20 69 74 20 73 65 74 73 20  ly then it sets 
bf40: 2a 70 4f 75 74 46 6c 61 67 73 20 74 6f 0a 2a 2a  *pOutFlags to.**
bf50: 20 69 6e 63 6c 75 64 65 20 5b 53 51 4c 49 54 45   include [SQLITE
bf60: 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 2e  _OPEN_READONLY].
bf70: 20 20 4f 74 68 65 72 20 62 69 74 73 20 69 6e 20    Other bits in 
bf80: 2a 70 4f 75 74 46 6c 61 67 73 20 6d 61 79 20 62  *pOutFlags may b
bf90: 65 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c  e set..**.** SQL
bfa0: 69 74 65 20 77 69 6c 6c 20 61 6c 73 6f 20 61 64  ite will also ad
bfb0: 64 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c  d one of the fol
bfc0: 6c 6f 77 69 6e 67 20 66 6c 61 67 73 20 74 6f 20  lowing flags to 
bfd0: 74 68 65 20 78 4f 70 65 6e 28 29 0a 2a 2a 20 63  the xOpen().** c
bfe0: 61 6c 6c 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f  all, depending o
bff0: 6e 20 74 68 65 20 6f 62 6a 65 63 74 20 62 65 69  n the object bei
c000: 6e 67 20 6f 70 65 6e 65 64 3a 0a 2a 2a 0a 2a 2a  ng opened:.**.**
c010: 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 5b   <ul>.** <li>  [
c020: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
c030: 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53  _DB].** <li>  [S
c040: 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
c050: 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e  JOURNAL].** <li>
c060: 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54    [SQLITE_OPEN_T
c070: 45 4d 50 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20  EMP_DB].** <li> 
c080: 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45   [SQLITE_OPEN_TE
c090: 4d 50 5f 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c  MP_JOURNAL].** <
c0a0: 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45  li>  [SQLITE_OPE
c0b0: 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 5d 0a  N_TRANSIENT_DB].
c0c0: 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45  ** <li>  [SQLITE
c0d0: 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c  _OPEN_SUBJOURNAL
c0e0: 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49  ].** <li>  [SQLI
c0f0: 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a  TE_OPEN_MASTER_J
c100: 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c 3e  OURNAL].** </ul>
c110: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20  .**.** The file 
c120: 49 2f 4f 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  I/O implementati
c130: 6f 6e 20 63 61 6e 20 75 73 65 20 74 68 65 20 6f  on can use the o
c140: 62 6a 65 63 74 20 74 79 70 65 20 66 6c 61 67 73  bject type flags
c150: 20 74 6f 0a 2a 2a 20 63 68 61 6e 67 65 20 74 68   to.** change th
c160: 65 20 77 61 79 20 69 74 20 64 65 61 6c 73 20 77  e way it deals w
c170: 69 74 68 20 66 69 6c 65 73 2e 20 20 46 6f 72 20  ith files.  For 
c180: 65 78 61 6d 70 6c 65 2c 20 61 6e 20 61 70 70 6c  example, an appl
c190: 69 63 61 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 20  ication.** that 
c1a0: 64 6f 65 73 20 6e 6f 74 20 63 61 72 65 20 61 62  does not care ab
c1b0: 6f 75 74 20 63 72 61 73 68 20 72 65 63 6f 76 65  out crash recove
c1c0: 72 79 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6d  ry or rollback m
c1d0: 69 67 68 74 20 6d 61 6b 65 0a 2a 2a 20 74 68 65  ight make.** the
c1e0: 20 6f 70 65 6e 20 6f 66 20 61 20 6a 6f 75 72 6e   open of a journ
c1f0: 61 6c 20 66 69 6c 65 20 61 20 6e 6f 2d 6f 70 2e  al file a no-op.
c200: 20 20 57 72 69 74 65 73 20 74 6f 20 74 68 69 73    Writes to this
c210: 20 6a 6f 75 72 6e 61 6c 20 77 6f 75 6c 64 0a 2a   journal would.*
c220: 2a 20 61 6c 73 6f 20 62 65 20 6e 6f 2d 6f 70 73  * also be no-ops
c230: 2c 20 61 6e 64 20 61 6e 79 20 61 74 74 65 6d 70  , and any attemp
c240: 74 20 74 6f 20 72 65 61 64 20 74 68 65 20 6a 6f  t to read the jo
c250: 75 72 6e 61 6c 20 77 6f 75 6c 64 20 72 65 74 75  urnal would retu
c260: 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 49 4f 45  rn.** SQLITE_IOE
c270: 52 52 2e 20 20 4f 72 20 74 68 65 20 69 6d 70 6c  RR.  Or the impl
c280: 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 69 67 68 74  ementation might
c290: 20 72 65 63 6f 67 6e 69 7a 65 20 74 68 61 74 20   recognize that 
c2a0: 61 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69  a database.** fi
c2b0: 6c 65 20 77 69 6c 6c 20 62 65 20 64 6f 69 6e 67  le will be doing
c2c0: 20 70 61 67 65 2d 61 6c 69 67 6e 65 64 20 73 65   page-aligned se
c2d0: 63 74 6f 72 20 72 65 61 64 73 20 61 6e 64 20 77  ctor reads and w
c2e0: 72 69 74 65 73 20 69 6e 20 61 20 72 61 6e 64 6f  rites in a rando
c2f0: 6d 0a 2a 2a 20 6f 72 64 65 72 20 61 6e 64 20 73  m.** order and s
c300: 65 74 20 75 70 20 69 74 73 20 49 2f 4f 20 73 75  et up its I/O su
c310: 62 73 79 73 74 65 6d 20 61 63 63 6f 72 64 69 6e  bsystem accordin
c320: 67 6c 79 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  gly..**.** SQLit
c330: 65 20 6d 69 67 68 74 20 61 6c 73 6f 20 61 64 64  e might also add
c340: 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c   one of the foll
c350: 6f 77 69 6e 67 20 66 6c 61 67 73 20 74 6f 20 74  owing flags to t
c360: 68 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 3a  he xOpen method:
c370: 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c  .**.** <ul>.** <
c380: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e  li> [SQLITE_OPEN
c390: 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 0a  _DELETEONCLOSE].
c3a0: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f  ** <li> [SQLITE_
c3b0: 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 5d 0a  OPEN_EXCLUSIVE].
c3c0: 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54  ** </ul>.**.** T
c3d0: 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  he [SQLITE_OPEN_
c3e0: 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 20 66  DELETEONCLOSE] f
c3f0: 6c 61 67 20 6d 65 61 6e 73 20 74 68 65 20 66 69  lag means the fi
c400: 6c 65 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20  le should be.** 
c410: 64 65 6c 65 74 65 64 20 77 68 65 6e 20 69 74 20  deleted when it 
c420: 69 73 20 63 6c 6f 73 65 64 2e 20 20 54 68 65 20  is closed.  The 
c430: 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c  [SQLITE_OPEN_DEL
c440: 45 54 45 4f 4e 43 4c 4f 53 45 5d 0a 2a 2a 20 77  ETEONCLOSE].** w
c450: 69 6c 6c 20 62 65 20 73 65 74 20 66 6f 72 20 54  ill be set for T
c460: 45 4d 50 20 20 64 61 74 61 62 61 73 65 73 2c 20  EMP  databases, 
c470: 6a 6f 75 72 6e 61 6c 73 20 61 6e 64 20 66 6f 72  journals and for
c480: 20 73 75 62 6a 6f 75 72 6e 61 6c 73 2e 0a 2a 2a   subjournals..**
c490: 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f  .** The [SQLITE_
c4a0: 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 5d 20  OPEN_EXCLUSIVE] 
c4b0: 66 6c 61 67 20 69 73 20 61 6c 77 61 79 73 20 75  flag is always u
c4c0: 73 65 64 20 69 6e 20 63 6f 6e 6a 75 6e 63 74 69  sed in conjuncti
c4d0: 6f 6e 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 5b  on.** with the [
c4e0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41  SQLITE_OPEN_CREA
c4f0: 54 45 5d 20 66 6c 61 67 2c 20 77 68 69 63 68 20  TE] flag, which 
c500: 61 72 65 20 62 6f 74 68 20 64 69 72 65 63 74 6c  are both directl
c510: 79 0a 2a 2a 20 61 6e 61 6c 6f 67 6f 75 73 20 74  y.** analogous t
c520: 6f 20 74 68 65 20 4f 5f 45 58 43 4c 20 61 6e 64  o the O_EXCL and
c530: 20 4f 5f 43 52 45 41 54 20 66 6c 61 67 73 20 6f   O_CREAT flags o
c540: 66 20 74 68 65 20 50 4f 53 49 58 20 6f 70 65 6e  f the POSIX open
c550: 28 29 0a 2a 2a 20 41 50 49 2e 20 20 54 68 65 20  ().** API.  The 
c560: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c  SQLITE_OPEN_EXCL
c570: 55 53 49 56 45 20 66 6c 61 67 2c 20 77 68 65 6e  USIVE flag, when
c580: 20 70 61 69 72 65 64 20 77 69 74 68 20 74 68 65   paired with the
c590: 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 50 45 4e   .** SQLITE_OPEN
c5a0: 5f 43 52 45 41 54 45 2c 20 69 73 20 75 73 65 64  _CREATE, is used
c5b0: 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61   to indicate tha
c5c0: 74 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 61 6c  t file should al
c5d0: 77 61 79 73 0a 2a 2a 20 62 65 20 63 72 65 61 74  ways.** be creat
c5e0: 65 64 2c 20 61 6e 64 20 74 68 61 74 20 69 74 20  ed, and that it 
c5f0: 69 73 20 61 6e 20 65 72 72 6f 72 20 69 66 20 69  is an error if i
c600: 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73  t already exists
c610: 2e 0a 2a 2a 20 49 74 20 69 73 20 3c 69 3e 6e 6f  ..** It is <i>no
c620: 74 3c 2f 69 3e 20 75 73 65 64 20 74 6f 20 69 6e  t</i> used to in
c630: 64 69 63 61 74 65 20 74 68 65 20 66 69 6c 65 20  dicate the file 
c640: 73 68 6f 75 6c 64 20 62 65 20 6f 70 65 6e 65 64  should be opened
c650: 20 0a 2a 2a 20 66 6f 72 20 65 78 63 6c 75 73 69   .** for exclusi
c660: 76 65 20 61 63 63 65 73 73 2e 0a 2a 2a 0a 2a 2a  ve access..**.**
c670: 20 41 74 20 6c 65 61 73 74 20 73 7a 4f 73 46 69   At least szOsFi
c680: 6c 65 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f  le bytes of memo
c690: 72 79 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64  ry are allocated
c6a0: 20 62 79 20 53 51 4c 69 74 65 0a 2a 2a 20 74 6f   by SQLite.** to
c6b0: 20 68 6f 6c 64 20 74 68 65 20 20 5b 73 71 6c 69   hold the  [sqli
c6c0: 74 65 33 5f 66 69 6c 65 5d 20 73 74 72 75 63 74  te3_file] struct
c6d0: 75 72 65 20 70 61 73 73 65 64 20 61 73 20 74 68  ure passed as th
c6e0: 65 20 74 68 69 72 64 0a 2a 2a 20 61 72 67 75 6d  e third.** argum
c6f0: 65 6e 74 20 74 6f 20 78 4f 70 65 6e 2e 20 20 54  ent to xOpen.  T
c700: 68 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20  he xOpen method 
c710: 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 74 6f  does not have to
c720: 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 74 68 65  .** allocate the
c730: 20 73 74 72 75 63 74 75 72 65 3b 20 69 74 20 73   structure; it s
c740: 68 6f 75 6c 64 20 6a 75 73 74 20 66 69 6c 6c 20  hould just fill 
c750: 69 74 20 69 6e 2e 20 20 4e 6f 74 65 20 74 68 61  it in.  Note tha
c760: 74 0a 2a 2a 20 74 68 65 20 78 4f 70 65 6e 20 6d  t.** the xOpen m
c770: 65 74 68 6f 64 20 6d 75 73 74 20 73 65 74 20 74  ethod must set t
c780: 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2e  he sqlite3_file.
c790: 70 4d 65 74 68 6f 64 73 20 74 6f 20 65 69 74 68  pMethods to eith
c7a0: 65 72 0a 2a 2a 20 61 20 76 61 6c 69 64 20 5b 73  er.** a valid [s
c7b0: 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
c7c0: 73 5d 20 6f 62 6a 65 63 74 20 6f 72 20 74 6f 20  s] object or to 
c7d0: 4e 55 4c 4c 2e 20 20 78 4f 70 65 6e 20 6d 75 73  NULL.  xOpen mus
c7e0: 74 20 64 6f 0a 2a 2a 20 74 68 69 73 20 65 76 65  t do.** this eve
c7f0: 6e 20 69 66 20 74 68 65 20 6f 70 65 6e 20 66 61  n if the open fa
c800: 69 6c 73 2e 20 20 53 51 4c 69 74 65 20 65 78 70  ils.  SQLite exp
c810: 65 63 74 73 20 74 68 61 74 20 74 68 65 20 73 71  ects that the sq
c820: 6c 69 74 65 33 5f 66 69 6c 65 2e 70 4d 65 74 68  lite3_file.pMeth
c830: 6f 64 73 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 77  ods.** element w
c840: 69 6c 6c 20 62 65 20 76 61 6c 69 64 20 61 66 74  ill be valid aft
c850: 65 72 20 78 4f 70 65 6e 20 72 65 74 75 72 6e 73  er xOpen returns
c860: 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74   regardless of t
c870: 68 65 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72  he success.** or
c880: 20 66 61 69 6c 75 72 65 20 6f 66 20 74 68 65 20   failure of the 
c890: 78 4f 70 65 6e 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a  xOpen call..**.*
c8a0: 2a 20 54 68 65 20 66 6c 61 67 73 20 61 72 67 75  * The flags argu
c8b0: 6d 65 6e 74 20 74 6f 20 78 41 63 63 65 73 73 28  ment to xAccess(
c8c0: 29 20 6d 61 79 20 62 65 20 5b 53 51 4c 49 54 45  ) may be [SQLITE
c8d0: 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 5d 0a  _ACCESS_EXISTS].
c8e0: 2a 2a 20 74 6f 20 74 65 73 74 20 66 6f 72 20 74  ** to test for t
c8f0: 68 65 20 65 78 69 73 74 65 6e 63 65 20 6f 66 20  he existence of 
c900: 61 20 66 69 6c 65 2c 20 6f 72 20 5b 53 51 4c 49  a file, or [SQLI
c910: 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52  TE_ACCESS_READWR
c920: 49 54 45 5d 20 74 6f 0a 2a 2a 20 74 65 73 74 20  ITE] to.** test 
c930: 77 68 65 74 68 65 72 20 61 20 66 69 6c 65 20 69  whether a file i
c940: 73 20 72 65 61 64 61 62 6c 65 20 61 6e 64 20 77  s readable and w
c950: 72 69 74 61 62 6c 65 2c 20 6f 72 20 5b 53 51 4c  ritable, or [SQL
c960: 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 5d  ITE_ACCESS_READ]
c970: 0a 2a 2a 20 74 6f 20 74 65 73 74 20 77 68 65 74  .** to test whet
c980: 68 65 72 20 61 20 66 69 6c 65 20 69 73 20 61 74  her a file is at
c990: 20 6c 65 61 73 74 20 72 65 61 64 61 62 6c 65 2e   least readable.
c9a0: 20 20 20 54 68 65 20 66 69 6c 65 20 63 61 6e 20     The file can 
c9b0: 62 65 20 61 0a 2a 2a 20 64 69 72 65 63 74 6f 72  be a.** director
c9c0: 79 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  y..**.** SQLite 
c9d0: 77 69 6c 6c 20 61 6c 77 61 79 73 20 61 6c 6c 6f  will always allo
c9e0: 63 61 74 65 20 61 74 20 6c 65 61 73 74 20 6d 78  cate at least mx
c9f0: 50 61 74 68 6e 61 6d 65 2b 31 20 62 79 74 65 73  Pathname+1 bytes
ca00: 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6f 75 74 70   for the.** outp
ca10: 75 74 20 62 75 66 66 65 72 20 78 46 75 6c 6c 50  ut buffer xFullP
ca20: 61 74 68 6e 61 6d 65 2e 20 20 54 68 65 20 65 78  athname.  The ex
ca30: 61 63 74 20 73 69 7a 65 20 6f 66 20 74 68 65 20  act size of the 
ca40: 6f 75 74 70 75 74 20 62 75 66 66 65 72 0a 2a 2a  output buffer.**
ca50: 20 69 73 20 61 6c 73 6f 20 70 61 73 73 65 64 20   is also passed 
ca60: 61 73 20 61 20 70 61 72 61 6d 65 74 65 72 20 74  as a parameter t
ca70: 6f 20 62 6f 74 68 20 20 6d 65 74 68 6f 64 73 2e  o both  methods.
ca80: 20 49 66 20 74 68 65 20 6f 75 74 70 75 74 20 62   If the output b
ca90: 75 66 66 65 72 0a 2a 2a 20 69 73 20 6e 6f 74 20  uffer.** is not 
caa0: 6c 61 72 67 65 20 65 6e 6f 75 67 68 2c 20 5b 53  large enough, [S
cab0: 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5d 20  QLITE_CANTOPEN] 
cac0: 73 68 6f 75 6c 64 20 62 65 20 72 65 74 75 72 6e  should be return
cad0: 65 64 2e 20 53 69 6e 63 65 20 74 68 69 73 20 69  ed. Since this i
cae0: 73 0a 2a 2a 20 68 61 6e 64 6c 65 64 20 61 73 20  s.** handled as 
caf0: 61 20 66 61 74 61 6c 20 65 72 72 6f 72 20 62 79  a fatal error by
cb00: 20 53 51 4c 69 74 65 2c 20 76 66 73 20 69 6d 70   SQLite, vfs imp
cb10: 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 73 68 6f  lementations sho
cb20: 75 6c 64 20 65 6e 64 65 61 76 6f 72 0a 2a 2a 20  uld endeavor.** 
cb30: 74 6f 20 70 72 65 76 65 6e 74 20 74 68 69 73 20  to prevent this 
cb40: 62 79 20 73 65 74 74 69 6e 67 20 6d 78 50 61 74  by setting mxPat
cb50: 68 6e 61 6d 65 20 74 6f 20 61 20 73 75 66 66 69  hname to a suffi
cb60: 63 69 65 6e 74 6c 79 20 6c 61 72 67 65 20 76 61  ciently large va
cb70: 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78  lue..**.** The x
cb80: 52 61 6e 64 6f 6d 6e 65 73 73 28 29 2c 20 78 53  Randomness(), xS
cb90: 6c 65 65 70 28 29 2c 20 61 6e 64 20 78 43 75 72  leep(), and xCur
cba0: 72 65 6e 74 54 69 6d 65 28 29 20 69 6e 74 65 72  rentTime() inter
cbb0: 66 61 63 65 73 0a 2a 2a 20 61 72 65 20 6e 6f 74  faces.** are not
cbc0: 20 73 74 72 69 63 74 6c 79 20 61 20 70 61 72 74   strictly a part
cbd0: 20 6f 66 20 74 68 65 20 66 69 6c 65 73 79 73 74   of the filesyst
cbe0: 65 6d 2c 20 62 75 74 20 74 68 65 79 20 61 72 65  em, but they are
cbf0: 0a 2a 2a 20 69 6e 63 6c 75 64 65 64 20 69 6e 20  .** included in 
cc00: 74 68 65 20 56 46 53 20 73 74 72 75 63 74 75 72  the VFS structur
cc10: 65 20 66 6f 72 20 63 6f 6d 70 6c 65 74 65 6e 65  e for completene
cc20: 73 73 2e 0a 2a 2a 20 54 68 65 20 78 52 61 6e 64  ss..** The xRand
cc30: 6f 6d 6e 65 73 73 28 29 20 66 75 6e 63 74 69 6f  omness() functio
cc40: 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65  n attempts to re
cc50: 74 75 72 6e 20 6e 42 79 74 65 73 20 62 79 74 65  turn nBytes byte
cc60: 73 0a 2a 2a 20 6f 66 20 67 6f 6f 64 2d 71 75 61  s.** of good-qua
cc70: 6c 69 74 79 20 72 61 6e 64 6f 6d 6e 65 73 73 20  lity randomness 
cc80: 69 6e 74 6f 20 7a 4f 75 74 2e 20 20 54 68 65 20  into zOut.  The 
cc90: 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 0a  return value is.
cca0: 2a 2a 20 74 68 65 20 61 63 74 75 61 6c 20 6e 75  ** the actual nu
ccb0: 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
ccc0: 20 72 61 6e 64 6f 6d 6e 65 73 73 20 6f 62 74 61   randomness obta
ccd0: 69 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 78 53 6c  ined..** The xSl
cce0: 65 65 70 28 29 20 6d 65 74 68 6f 64 20 63 61 75  eep() method cau
ccf0: 73 65 73 20 74 68 65 20 63 61 6c 6c 69 6e 67 20  ses the calling 
cd00: 74 68 72 65 61 64 20 74 6f 20 73 6c 65 65 70 20  thread to sleep 
cd10: 66 6f 72 20 61 74 0a 2a 2a 20 6c 65 61 73 74 20  for at.** least 
cd20: 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69  the number of mi
cd30: 63 72 6f 73 65 63 6f 6e 64 73 20 67 69 76 65 6e  croseconds given
cd40: 2e 20 20 54 68 65 20 78 43 75 72 72 65 6e 74 54  .  The xCurrentT
cd50: 69 6d 65 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20  ime().** method 
cd60: 72 65 74 75 72 6e 73 20 61 20 4a 75 6c 69 61 6e  returns a Julian
cd70: 20 44 61 79 20 4e 75 6d 62 65 72 20 66 6f 72 20   Day Number for 
cd80: 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 65  the current date
cd90: 20 61 6e 64 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2f   and time..**.*/
cda0: 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
cdb0: 73 71 6c 69 74 65 33 5f 76 66 73 20 73 71 6c 69  sqlite3_vfs sqli
cdc0: 74 65 33 5f 76 66 73 3b 0a 73 74 72 75 63 74 20  te3_vfs;.struct 
cdd0: 73 71 6c 69 74 65 33 5f 76 66 73 20 7b 0a 20 20  sqlite3_vfs {.  
cde0: 69 6e 74 20 69 56 65 72 73 69 6f 6e 3b 20 20 20  int iVersion;   
cdf0: 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 75           /* Stru
ce00: 63 74 75 72 65 20 76 65 72 73 69 6f 6e 20 6e 75  cture version nu
ce10: 6d 62 65 72 20 2a 2f 0a 20 20 69 6e 74 20 73 7a  mber */.  int sz
ce20: 4f 73 46 69 6c 65 3b 20 20 20 20 20 20 20 20 20  OsFile;         
ce30: 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 75     /* Size of su
ce40: 62 63 6c 61 73 73 65 64 20 73 71 6c 69 74 65 33  bclassed sqlite3
ce50: 5f 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6d  _file */.  int m
ce60: 78 50 61 74 68 6e 61 6d 65 3b 20 20 20 20 20 20  xPathname;      
ce70: 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 66      /* Maximum f
ce80: 69 6c 65 20 70 61 74 68 6e 61 6d 65 20 6c 65 6e  ile pathname len
ce90: 67 74 68 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  gth */.  sqlite3
cea0: 5f 76 66 73 20 2a 70 4e 65 78 74 3b 20 20 20 20  _vfs *pNext;    
ceb0: 20 20 2f 2a 20 4e 65 78 74 20 72 65 67 69 73 74    /* Next regist
cec0: 65 72 65 64 20 56 46 53 20 2a 2f 0a 20 20 63 6f  ered VFS */.  co
ced0: 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b  nst char *zName;
cee0: 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
cef0: 66 20 74 68 69 73 20 76 69 72 74 75 61 6c 20 66  f this virtual f
cf00: 69 6c 65 20 73 79 73 74 65 6d 20 2a 2f 0a 20 20  ile system */.  
cf10: 76 6f 69 64 20 2a 70 41 70 70 44 61 74 61 3b 20  void *pAppData; 
cf20: 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
cf30: 74 65 72 20 74 6f 20 61 70 70 6c 69 63 61 74 69  ter to applicati
cf40: 6f 6e 2d 73 70 65 63 69 66 69 63 20 64 61 74 61  on-specific data
cf50: 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 4f 70 65   */.  int (*xOpe
cf60: 6e 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c  n)(sqlite3_vfs*,
cf70: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
cf80: 6d 65 2c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  me, sqlite3_file
cf90: 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  *,.             
cfa0: 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 69 6e 74    int flags, int
cfb0: 20 2a 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20   *pOutFlags);.  
cfc0: 69 6e 74 20 28 2a 78 44 65 6c 65 74 65 29 28 73  int (*xDelete)(s
cfd0: 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e  qlite3_vfs*, con
cfe0: 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  st char *zName, 
cff0: 69 6e 74 20 73 79 6e 63 44 69 72 29 3b 0a 20 20  int syncDir);.  
d000: 69 6e 74 20 28 2a 78 41 63 63 65 73 73 29 28 73  int (*xAccess)(s
d010: 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e  qlite3_vfs*, con
d020: 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  st char *zName, 
d030: 69 6e 74 20 66 6c 61 67 73 2c 20 69 6e 74 20 2a  int flags, int *
d040: 70 52 65 73 4f 75 74 29 3b 0a 20 20 69 6e 74 20  pResOut);.  int 
d050: 28 2a 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29  (*xFullPathname)
d060: 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63  (sqlite3_vfs*, c
d070: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
d080: 2c 20 69 6e 74 20 6e 4f 75 74 2c 20 63 68 61 72  , int nOut, char
d090: 20 2a 7a 4f 75 74 29 3b 0a 20 20 76 6f 69 64 20   *zOut);.  void 
d0a0: 2a 28 2a 78 44 6c 4f 70 65 6e 29 28 73 71 6c 69  *(*xDlOpen)(sqli
d0b0: 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20  te3_vfs*, const 
d0c0: 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29  char *zFilename)
d0d0: 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44 6c 45 72  ;.  void (*xDlEr
d0e0: 72 6f 72 29 28 73 71 6c 69 74 65 33 5f 76 66 73  ror)(sqlite3_vfs
d0f0: 2a 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 68  *, int nByte, ch
d100: 61 72 20 2a 7a 45 72 72 4d 73 67 29 3b 0a 20 20  ar *zErrMsg);.  
d110: 76 6f 69 64 20 28 2a 28 2a 78 44 6c 53 79 6d 29  void (*(*xDlSym)
d120: 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 76 6f  (sqlite3_vfs*,vo
d130: 69 64 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  id*, const char 
d140: 2a 7a 53 79 6d 62 6f 6c 29 29 28 76 6f 69 64 29  *zSymbol))(void)
d150: 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44 6c 43 6c  ;.  void (*xDlCl
d160: 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 76 66 73  ose)(sqlite3_vfs
d170: 2a 2c 20 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74  *, void*);.  int
d180: 20 28 2a 78 52 61 6e 64 6f 6d 6e 65 73 73 29 28   (*xRandomness)(
d190: 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e  sqlite3_vfs*, in
d1a0: 74 20 6e 42 79 74 65 2c 20 63 68 61 72 20 2a 7a  t nByte, char *z
d1b0: 4f 75 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53  Out);.  int (*xS
d1c0: 6c 65 65 70 29 28 73 71 6c 69 74 65 33 5f 76 66  leep)(sqlite3_vf
d1d0: 73 2a 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63  s*, int microsec
d1e0: 6f 6e 64 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78  onds);.  int (*x
d1f0: 43 75 72 72 65 6e 74 54 69 6d 65 29 28 73 71 6c  CurrentTime)(sql
d200: 69 74 65 33 5f 76 66 73 2a 2c 20 64 6f 75 62 6c  ite3_vfs*, doubl
d210: 65 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 47 65  e*);.  int (*xGe
d220: 74 4c 61 73 74 45 72 72 6f 72 29 28 73 71 6c 69  tLastError)(sqli
d230: 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 2c 20 63  te3_vfs*, int, c
d240: 68 61 72 20 2a 29 3b 0a 20 20 2f 2a 20 4e 65 77  har *);.  /* New
d250: 20 66 69 65 6c 64 73 20 6d 61 79 20 62 65 20 61   fields may be a
d260: 70 70 65 6e 64 65 64 20 69 6e 20 66 69 67 75 72  ppended in figur
d270: 65 20 76 65 72 73 69 6f 6e 73 2e 20 20 54 68 65  e versions.  The
d280: 20 69 56 65 72 73 69 6f 6e 0a 20 20 2a 2a 20 76   iVersion.  ** v
d290: 61 6c 75 65 20 77 69 6c 6c 20 69 6e 63 72 65 6d  alue will increm
d2a0: 65 6e 74 20 77 68 65 6e 65 76 65 72 20 74 68 69  ent whenever thi
d2b0: 73 20 68 61 70 70 65 6e 73 2e 20 2a 2f 0a 7d 3b  s happens. */.};
d2c0: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
d2d0: 3a 20 46 6c 61 67 73 20 66 6f 72 20 74 68 65 20  : Flags for the 
d2e0: 78 41 63 63 65 73 73 20 56 46 53 20 6d 65 74 68  xAccess VFS meth
d2f0: 6f 64 20 7b 48 31 31 31 39 30 7d 20 3c 48 31 31  od {H11190} <H11
d300: 31 34 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  140>.**.** These
d310: 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e   integer constan
d320: 74 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 61  ts can be used a
d330: 73 20 74 68 65 20 74 68 69 72 64 20 70 61 72 61  s the third para
d340: 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20  meter to.** the 
d350: 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 20 6f  xAccess method o
d360: 66 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 76 66  f an [sqlite3_vf
d370: 73 5d 20 6f 62 6a 65 63 74 2e 20 7b 45 4e 44 7d  s] object. {END}
d380: 20 20 54 68 65 79 20 64 65 74 65 72 6d 69 6e 65    They determine
d390: 0a 2a 2a 20 77 68 61 74 20 6b 69 6e 64 20 6f 66  .** what kind of
d3a0: 20 70 65 72 6d 69 73 73 69 6f 6e 73 20 74 68 65   permissions the
d3b0: 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 20   xAccess method 
d3c0: 69 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 2e 0a  is looking for..
d3d0: 2a 2a 20 57 69 74 68 20 53 51 4c 49 54 45 5f 41  ** With SQLITE_A
d3e0: 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20 74 68  CCESS_EXISTS, th
d3f0: 65 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64  e xAccess method
d400: 0a 2a 2a 20 73 69 6d 70 6c 79 20 63 68 65 63 6b  .** simply check
d410: 73 20 77 68 65 74 68 65 72 20 74 68 65 20 66 69  s whether the fi
d420: 6c 65 20 65 78 69 73 74 73 2e 0a 2a 2a 20 57 69  le exists..** Wi
d430: 74 68 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53  th SQLITE_ACCESS
d440: 5f 52 45 41 44 57 52 49 54 45 2c 20 74 68 65 20  _READWRITE, the 
d450: 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 0a 2a  xAccess method.*
d460: 2a 20 63 68 65 63 6b 73 20 77 68 65 74 68 65 72  * checks whether
d470: 20 74 68 65 20 66 69 6c 65 20 69 73 20 62 6f 74   the file is bot
d480: 68 20 72 65 61 64 61 62 6c 65 20 61 6e 64 20 77  h readable and w
d490: 72 69 74 61 62 6c 65 2e 0a 2a 2a 20 57 69 74 68  ritable..** With
d4a0: 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52   SQLITE_ACCESS_R
d4b0: 45 41 44 2c 20 74 68 65 20 78 41 63 63 65 73 73  EAD, the xAccess
d4c0: 20 6d 65 74 68 6f 64 0a 2a 2a 20 63 68 65 63 6b   method.** check
d4d0: 73 20 77 68 65 74 68 65 72 20 74 68 65 20 66 69  s whether the fi
d4e0: 6c 65 20 69 73 20 72 65 61 64 61 62 6c 65 2e 0a  le is readable..
d4f0: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
d500: 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 20  E_ACCESS_EXISTS 
d510: 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c     0.#define SQL
d520: 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57  ITE_ACCESS_READW
d530: 52 49 54 45 20 31 0a 23 64 65 66 69 6e 65 20 53  RITE 1.#define S
d540: 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41  QLITE_ACCESS_REA
d550: 44 20 20 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 20  D      2../*.** 
d560: 43 41 50 49 33 52 45 46 3a 20 49 6e 69 74 69 61  CAPI3REF: Initia
d570: 6c 69 7a 65 20 54 68 65 20 53 51 4c 69 74 65 20  lize The SQLite 
d580: 4c 69 62 72 61 72 79 20 7b 48 31 30 31 33 30 7d  Library {H10130}
d590: 20 3c 53 32 30 30 30 30 3e 3c 53 33 30 31 30 30   <S20000><S30100
d5a0: 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  >.**.** The sqli
d5b0: 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
d5c0: 20 72 6f 75 74 69 6e 65 20 69 6e 69 74 69 61 6c   routine initial
d5d0: 69 7a 65 73 20 74 68 65 0a 2a 2a 20 53 51 4c 69  izes the.** SQLi
d5e0: 74 65 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65  te library.  The
d5f0: 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77   sqlite3_shutdow
d600: 6e 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 64  n() routine.** d
d610: 65 61 6c 6c 6f 63 61 74 65 73 20 61 6e 79 20 72  eallocates any r
d620: 65 73 6f 75 72 63 65 73 20 74 68 61 74 20 77 65  esources that we
d630: 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20  re allocated by 
d640: 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
d650: 7a 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 61  ze()..**.** A ca
d660: 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 69 6e  ll to sqlite3_in
d670: 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20 61 6e  itialize() is an
d680: 20 22 65 66 66 65 63 74 69 76 65 22 20 63 61 6c   "effective" cal
d690: 6c 20 69 66 20 69 74 20 69 73 0a 2a 2a 20 74 68  l if it is.** th
d6a0: 65 20 66 69 72 73 74 20 74 69 6d 65 20 73 71 6c  e first time sql
d6b0: 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
d6c0: 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 64 75 72  ) is invoked dur
d6d0: 69 6e 67 20 74 68 65 20 6c 69 66 65 74 69 6d 65  ing the lifetime
d6e0: 20 6f 66 0a 2a 2a 20 74 68 65 20 70 72 6f 63 65   of.** the proce
d6f0: 73 73 2c 20 6f 72 20 69 66 20 69 74 20 69 73 20  ss, or if it is 
d700: 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 73  the first time s
d710: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
d720: 65 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 0a 2a  e() is invoked.*
d730: 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 63 61  * following a ca
d740: 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 68  ll to sqlite3_sh
d750: 75 74 64 6f 77 6e 28 29 2e 20 20 4f 6e 6c 79 20  utdown().  Only 
d760: 61 6e 20 65 66 66 65 63 74 69 76 65 20 63 61 6c  an effective cal
d770: 6c 0a 2a 2a 20 6f 66 20 73 71 6c 69 74 65 33 5f  l.** of sqlite3_
d780: 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 64 6f 65  initialize() doe
d790: 73 20 61 6e 79 20 69 6e 69 74 69 61 6c 69 7a 61  s any initializa
d7a0: 74 69 6f 6e 2e 20 20 41 6c 6c 20 6f 74 68 65 72  tion.  All other
d7b0: 20 63 61 6c 6c 73 0a 2a 2a 20 61 72 65 20 68 61   calls.** are ha
d7c0: 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 73 2e 0a 2a  rmless no-ops..*
d7d0: 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 73  *.** A call to s
d7e0: 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28  qlite3_shutdown(
d7f0: 29 20 69 73 20 61 6e 20 22 65 66 66 65 63 74 69  ) is an "effecti
d800: 76 65 22 20 63 61 6c 6c 20 69 66 20 69 74 20 69  ve" call if it i
d810: 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 63  s the first.** c
d820: 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73  all to sqlite3_s
d830: 68 75 74 64 6f 77 6e 28 29 20 73 69 6e 63 65 20  hutdown() since 
d840: 74 68 65 20 6c 61 73 74 20 73 71 6c 69 74 65 33  the last sqlite3
d850: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 2e 20 20  _initialize().  
d860: 4f 6e 6c 79 0a 2a 2a 20 61 6e 20 65 66 66 65 63  Only.** an effec
d870: 74 69 76 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c  tive call to sql
d880: 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 20  ite3_shutdown() 
d890: 64 6f 65 73 20 61 6e 79 20 64 65 69 6e 69 74 69  does any deiniti
d8a0: 61 6c 69 7a 61 74 69 6f 6e 2e 0a 2a 2a 20 41 6c  alization..** Al
d8b0: 6c 20 6f 74 68 65 72 20 63 61 6c 6c 73 20 74 6f  l other calls to
d8c0: 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77   sqlite3_shutdow
d8d0: 6e 28 29 20 61 72 65 20 68 61 72 6d 6c 65 73 73  n() are harmless
d8e0: 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 41   no-ops..**.** A
d8f0: 6d 6f 6e 67 20 6f 74 68 65 72 20 74 68 69 6e 67  mong other thing
d900: 73 2c 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  s, sqlite3_initi
d910: 61 6c 69 7a 65 28 29 20 73 68 61 6c 6c 20 69 6e  alize() shall in
d920: 76 6f 6b 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  voke.** sqlite3_
d930: 6f 73 5f 69 6e 69 74 28 29 2e 20 20 53 69 6d 69  os_init().  Simi
d940: 6c 61 72 6c 79 2c 20 73 71 6c 69 74 65 33 5f 73  larly, sqlite3_s
d950: 68 75 74 64 6f 77 6e 28 29 0a 2a 2a 20 73 68 61  hutdown().** sha
d960: 6c 6c 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65  ll invoke sqlite
d970: 33 5f 6f 73 5f 65 6e 64 28 29 2e 0a 2a 2a 0a 2a  3_os_end()..**.*
d980: 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e  * The sqlite3_in
d990: 69 74 69 61 6c 69 7a 65 28 29 20 72 6f 75 74 69  itialize() routi
d9a0: 6e 65 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49  ne returns [SQLI
d9b0: 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73  TE_OK] on succes
d9c0: 73 2e 0a 2a 2a 20 49 66 20 66 6f 72 20 73 6f 6d  s..** If for som
d9d0: 65 20 72 65 61 73 6f 6e 2c 20 73 71 6c 69 74 65  e reason, sqlite
d9e0: 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 69  3_initialize() i
d9f0: 73 20 75 6e 61 62 6c 65 20 74 6f 20 69 6e 69 74  s unable to init
da00: 69 61 6c 69 7a 65 0a 2a 2a 20 74 68 65 20 6c 69  ialize.** the li
da10: 62 72 61 72 79 20 28 70 65 72 68 61 70 73 20 69  brary (perhaps i
da20: 74 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 61  t is unable to a
da30: 6c 6c 6f 63 61 74 65 20 61 20 6e 65 65 64 65 64  llocate a needed
da40: 20 72 65 73 6f 75 72 63 65 20 73 75 63 68 0a 2a   resource such.*
da50: 2a 20 61 73 20 61 20 6d 75 74 65 78 29 20 69 74  * as a mutex) it
da60: 20 72 65 74 75 72 6e 73 20 61 6e 20 5b 65 72 72   returns an [err
da70: 6f 72 20 63 6f 64 65 5d 20 6f 74 68 65 72 20 74  or code] other t
da80: 68 61 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e  han [SQLITE_OK].
da90: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
daa0: 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
dab0: 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
dac0: 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 62 79 20  d internally by 
dad0: 6d 61 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 53 51  many other.** SQ
dae0: 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 73 20  Lite interfaces 
daf0: 73 6f 20 74 68 61 74 20 61 6e 20 61 70 70 6c 69  so that an appli
db00: 63 61 74 69 6f 6e 20 75 73 75 61 6c 6c 79 20 64  cation usually d
db10: 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 0a  oes not need to.
db20: 2a 2a 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65  ** invoke sqlite
db30: 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 64  3_initialize() d
db40: 69 72 65 63 74 6c 79 2e 20 20 46 6f 72 20 65 78  irectly.  For ex
db50: 61 6d 70 6c 65 2c 20 5b 73 71 6c 69 74 65 33 5f  ample, [sqlite3_
db60: 6f 70 65 6e 28 29 5d 0a 2a 2a 20 63 61 6c 6c 73  open()].** calls
db70: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
db80: 69 7a 65 28 29 20 73 6f 20 74 68 65 20 53 51 4c  ize() so the SQL
db90: 69 74 65 20 6c 69 62 72 61 72 79 20 77 69 6c 6c  ite library will
dba0: 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c   be automaticall
dbb0: 79 0a 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 64  y.** initialized
dbc0: 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 6f   when [sqlite3_o
dbd0: 70 65 6e 28 29 5d 20 69 73 20 63 61 6c 6c 65 64  pen()] is called
dbe0: 20 69 66 20 69 74 20 68 61 73 20 6e 6f 74 20 62   if it has not b
dbf0: 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 0a 2a 2a  e initialized.**
dc00: 20 61 6c 72 65 61 64 79 2e 20 20 48 6f 77 65 76   already.  Howev
dc10: 65 72 2c 20 69 66 20 53 51 4c 69 74 65 20 69 73  er, if SQLite is
dc20: 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74   compiled with t
dc30: 68 65 20 5b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  he [SQLITE_OMIT_
dc40: 41 55 54 4f 49 4e 49 54 5d 0a 2a 2a 20 63 6f 6d  AUTOINIT].** com
dc50: 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e  pile-time option
dc60: 2c 20 74 68 65 6e 20 74 68 65 20 61 75 74 6f 6d  , then the autom
dc70: 61 74 69 63 20 63 61 6c 6c 73 20 74 6f 20 73 71  atic calls to sq
dc80: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
dc90: 28 29 0a 2a 2a 20 61 72 65 20 6f 6d 69 74 74 65  ().** are omitte
dca0: 64 20 61 6e 64 20 74 68 65 20 61 70 70 6c 69 63  d and the applic
dcb0: 61 74 69 6f 6e 20 6d 75 73 74 20 63 61 6c 6c 20  ation must call 
dcc0: 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
dcd0: 7a 65 28 29 20 64 69 72 65 63 74 6c 79 0a 2a 2a  ze() directly.**
dce0: 20 70 72 69 6f 72 20 74 6f 20 75 73 69 6e 67 20   prior to using 
dcf0: 61 6e 79 20 6f 74 68 65 72 20 53 51 4c 69 74 65  any other SQLite
dd00: 20 69 6e 74 65 72 66 61 63 65 2e 20 20 46 6f 72   interface.  For
dd10: 20 6d 61 78 69 6d 75 6d 20 70 6f 72 74 61 62 69   maximum portabi
dd20: 6c 69 74 79 2c 0a 2a 2a 20 69 74 20 69 73 20 72  lity,.** it is r
dd30: 65 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74 20  ecommended that 
dd40: 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 61 6c 77  applications alw
dd50: 61 79 73 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74  ays invoke sqlit
dd60: 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 0a  e3_initialize().
dd70: 2a 2a 20 64 69 72 65 63 74 6c 79 20 70 72 69 6f  ** directly prio
dd80: 72 20 74 6f 20 75 73 69 6e 67 20 61 6e 79 20 6f  r to using any o
dd90: 74 68 65 72 20 53 51 4c 69 74 65 20 69 6e 74 65  ther SQLite inte
dda0: 72 66 61 63 65 2e 20 20 46 75 74 75 72 65 20 72  rface.  Future r
ddb0: 65 6c 65 61 73 65 73 0a 2a 2a 20 6f 66 20 53 51  eleases.** of SQ
ddc0: 4c 69 74 65 20 6d 61 79 20 72 65 71 75 69 72 65  Lite may require
ddd0: 20 74 68 69 73 2e 20 20 49 6e 20 6f 74 68 65 72   this.  In other
dde0: 20 77 6f 72 64 73 2c 20 74 68 65 20 62 65 68 61   words, the beha
ddf0: 76 69 6f 72 20 65 78 68 69 62 69 74 65 64 0a 2a  vior exhibited.*
de00: 2a 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73  * when SQLite is
de10: 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 5b   compiled with [
de20: 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
de30: 49 4e 49 54 5d 20 6d 69 67 68 74 20 62 65 63 6f  INIT] might beco
de40: 6d 65 20 74 68 65 0a 2a 2a 20 64 65 66 61 75 6c  me the.** defaul
de50: 74 20 62 65 68 61 76 69 6f 72 20 69 6e 20 73 6f  t behavior in so
de60: 6d 65 20 66 75 74 75 72 65 20 72 65 6c 65 61 73  me future releas
de70: 65 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a  e of SQLite..**.
de80: 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6f  ** The sqlite3_o
de90: 73 5f 69 6e 69 74 28 29 20 72 6f 75 74 69 6e 65  s_init() routine
dea0: 20 64 6f 65 73 20 6f 70 65 72 61 74 69 6e 67 2d   does operating-
deb0: 73 79 73 74 65 6d 20 73 70 65 63 69 66 69 63 0a  system specific.
dec0: 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  ** initializatio
ded0: 6e 20 6f 66 20 74 68 65 20 53 51 4c 69 74 65 20  n of the SQLite 
dee0: 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20 73 71  library.  The sq
def0: 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 0a 2a  lite3_os_end().*
df00: 2a 20 72 6f 75 74 69 6e 65 20 75 6e 64 6f 65 73  * routine undoes
df10: 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 73   the effect of s
df20: 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29  qlite3_os_init()
df30: 2e 20 20 54 79 70 69 63 61 6c 20 74 61 73 6b 73  .  Typical tasks
df40: 0a 2a 2a 20 70 65 72 66 6f 72 6d 65 64 20 62 79  .** performed by
df50: 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   these routines 
df60: 69 6e 63 6c 75 64 65 20 61 6c 6c 6f 63 61 74 69  include allocati
df70: 6f 6e 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 69  on or deallocati
df80: 6f 6e 0a 2a 2a 20 6f 66 20 73 74 61 74 69 63 20  on.** of static 
df90: 72 65 73 6f 75 72 63 65 73 2c 20 69 6e 69 74 69  resources, initi
dfa0: 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 67 6c 6f  alization of glo
dfb0: 62 61 6c 20 76 61 72 69 61 62 6c 65 73 2c 0a 2a  bal variables,.*
dfc0: 2a 20 73 65 74 74 69 6e 67 20 75 70 20 61 20 64  * setting up a d
dfd0: 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f  efault [sqlite3_
dfe0: 76 66 73 5d 20 6d 6f 64 75 6c 65 2c 20 6f 72 20  vfs] module, or 
dff0: 73 65 74 74 69 6e 67 20 75 70 0a 2a 2a 20 61 20  setting up.** a 
e000: 64 65 66 61 75 6c 74 20 63 6f 6e 66 69 67 75 72  default configur
e010: 61 74 69 6f 6e 20 75 73 69 6e 67 20 5b 73 71 6c  ation using [sql
e020: 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 2e 0a  ite3_config()]..
e030: 2a 2a 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63  **.** The applic
e040: 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65 76  ation should nev
e050: 65 72 20 69 6e 76 6f 6b 65 20 65 69 74 68 65 72  er invoke either
e060: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74   sqlite3_os_init
e070: 28 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33  ().** or sqlite3
e080: 5f 6f 73 5f 65 6e 64 28 29 20 64 69 72 65 63 74  _os_end() direct
e090: 6c 79 2e 20 20 54 68 65 20 61 70 70 6c 69 63 61  ly.  The applica
e0a0: 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6f 6e 6c 79  tion should only
e0b0: 20 69 6e 76 6f 6b 65 0a 2a 2a 20 73 71 6c 69 74   invoke.** sqlit
e0c0: 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
e0d0: 61 6e 64 20 73 71 6c 69 74 65 33 5f 73 68 75 74  and sqlite3_shut
e0e0: 64 6f 77 6e 28 29 2e 20 20 54 68 65 20 73 71 6c  down().  The sql
e0f0: 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 0a 2a  ite3_os_init().*
e100: 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 63  * interface is c
e110: 61 6c 6c 65 64 20 61 75 74 6f 6d 61 74 69 63 61  alled automatica
e120: 6c 6c 79 20 62 79 20 73 71 6c 69 74 65 33 5f 69  lly by sqlite3_i
e130: 6e 69 74 69 61 6c 69 7a 65 28 29 20 61 6e 64 0a  nitialize() and.
e140: 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e  ** sqlite3_os_en
e150: 64 28 29 20 69 73 20 63 61 6c 6c 65 64 20 62 79  d() is called by
e160: 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77   sqlite3_shutdow
e170: 6e 28 29 2e 20 20 41 70 70 72 6f 70 72 69 61 74  n().  Appropriat
e180: 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74  e.** implementat
e190: 69 6f 6e 73 20 66 6f 72 20 73 71 6c 69 74 65 33  ions for sqlite3
e1a0: 5f 6f 73 5f 69 6e 69 74 28 29 20 61 6e 64 20 73  _os_init() and s
e1b0: 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 0a  qlite3_os_end().
e1c0: 2a 2a 20 61 72 65 20 62 75 69 6c 74 20 69 6e 74  ** are built int
e1d0: 6f 20 53 51 4c 69 74 65 20 77 68 65 6e 20 69 74  o SQLite when it
e1e0: 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 66 6f 72   is compiled for
e1f0: 20 55 6e 69 78 2c 20 57 69 6e 64 6f 77 73 2c 20   Unix, Windows, 
e200: 6f 72 20 4f 53 2f 32 2e 0a 2a 2a 20 57 68 65 6e  or OS/2..** When
e210: 20 5b 63 75 73 74 6f 6d 20 62 75 69 6c 64 73 20   [custom builds 
e220: 7c 20 62 75 69 6c 74 20 66 6f 72 20 6f 74 68 65  | built for othe
e230: 72 20 70 6c 61 74 66 6f 72 6d 73 5d 0a 2a 2a 20  r platforms].** 
e240: 28 75 73 69 6e 67 20 74 68 65 20 5b 53 51 4c 49  (using the [SQLI
e250: 54 45 5f 4f 53 5f 4f 54 48 45 52 3d 31 5d 20 63  TE_OS_OTHER=1] c
e260: 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a 20 6f  ompile-time.** o
e270: 70 74 69 6f 6e 29 20 74 68 65 20 61 70 70 6c 69  ption) the appli
e280: 63 61 74 69 6f 6e 20 6d 75 73 74 20 73 75 70 70  cation must supp
e290: 6c 79 20 61 20 73 75 69 74 61 62 6c 65 20 69 6d  ly a suitable im
e2a0: 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72  plementation for
e2b0: 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69  .** sqlite3_os_i
e2c0: 6e 69 74 28 29 20 61 6e 64 20 73 71 6c 69 74 65  nit() and sqlite
e2d0: 33 5f 6f 73 5f 65 6e 64 28 29 2e 20 20 41 6e 20  3_os_end().  An 
e2e0: 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73 75 70 70  application-supp
e2f0: 6c 69 65 64 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  lied.** implemen
e300: 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65  tation of sqlite
e310: 33 5f 6f 73 5f 69 6e 69 74 28 29 20 6f 72 20 73  3_os_init() or s
e320: 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 0a  qlite3_os_end().
e330: 2a 2a 20 6d 75 73 74 20 72 65 74 75 72 6e 20 5b  ** must return [
e340: 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75  SQLITE_OK] on su
e350: 63 63 65 73 73 20 61 6e 64 20 73 6f 6d 65 20 6f  ccess and some o
e360: 74 68 65 72 20 5b 65 72 72 6f 72 20 63 6f 64 65  ther [error code
e370: 5d 20 75 70 6f 6e 0a 2a 2a 20 66 61 69 6c 75 72  ] upon.** failur
e380: 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  e..*/.SQLITE_API
e390: 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6e 69   int sqlite3_ini
e3a0: 74 69 61 6c 69 7a 65 28 76 6f 69 64 29 3b 0a 53  tialize(void);.S
e3b0: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
e3c0: 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 76  lite3_shutdown(v
e3d0: 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  oid);.SQLITE_API
e3e0: 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f   int sqlite3_os_
e3f0: 69 6e 69 74 28 76 6f 69 64 29 3b 0a 53 51 4c 49  init(void);.SQLI
e400: 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
e410: 65 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 64 29 3b  e3_os_end(void);
e420: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
e430: 3a 20 43 6f 6e 66 69 67 75 72 69 6e 67 20 54 68  : Configuring Th
e440: 65 20 53 51 4c 69 74 65 20 4c 69 62 72 61 72 79  e SQLite Library
e450: 20 7b 48 31 34 31 30 30 7d 20 3c 53 32 30 30 30   {H14100} <S2000
e460: 30 3e 3c 53 33 30 32 30 30 3e 0a 2a 2a 20 45 58  0><S30200>.** EX
e470: 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a  PERIMENTAL.**.**
e480: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e   The sqlite3_con
e490: 66 69 67 28 29 20 69 6e 74 65 72 66 61 63 65 20  fig() interface 
e4a0: 69 73 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20  is used to make 
e4b0: 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72 61  global configura
e4c0: 74 69 6f 6e 0a 2a 2a 20 63 68 61 6e 67 65 73 20  tion.** changes 
e4d0: 74 6f 20 53 51 4c 69 74 65 20 69 6e 20 6f 72 64  to SQLite in ord
e4e0: 65 72 20 74 6f 20 74 75 6e 65 20 53 51 4c 69 74  er to tune SQLit
e4f0: 65 20 74 6f 20 74 68 65 20 73 70 65 63 69 66 69  e to the specifi
e500: 63 20 6e 65 65 64 73 20 6f 66 0a 2a 2a 20 74 68  c needs of.** th
e510: 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20  e application.  
e520: 54 68 65 20 64 65 66 61 75 6c 74 20 63 6f 6e 66  The default conf
e530: 69 67 75 72 61 74 69 6f 6e 20 69 73 20 72 65 63  iguration is rec
e540: 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20 6d 6f 73  ommended for mos
e550: 74 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e  t.** application
e560: 73 20 61 6e 64 20 73 6f 20 74 68 69 73 20 72 6f  s and so this ro
e570: 75 74 69 6e 65 20 69 73 20 75 73 75 61 6c 6c 79  utine is usually
e580: 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 2e 20   not necessary. 
e590: 20 49 74 20 69 73 0a 2a 2a 20 70 72 6f 76 69 64   It is.** provid
e5a0: 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20 72 61  ed to support ra
e5b0: 72 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20  re applications 
e5c0: 77 69 74 68 20 75 6e 75 73 75 61 6c 20 6e 65 65  with unusual nee
e5d0: 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  ds..**.** The sq
e5e0: 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 69  lite3_config() i
e5f0: 6e 74 65 72 66 61 63 65 20 69 73 20 6e 6f 74 20  nterface is not 
e600: 74 68 72 65 61 64 73 61 66 65 2e 20 20 54 68 65  threadsafe.  The
e610: 20 61 70 70 6c 69 63 61 74 69 6f 6e 0a 2a 2a 20   application.** 
e620: 6d 75 73 74 20 69 6e 73 75 72 65 20 74 68 61 74  must insure that
e630: 20 6e 6f 20 6f 74 68 65 72 20 53 51 4c 69 74 65   no other SQLite
e640: 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 20   interfaces are 
e650: 69 6e 76 6f 6b 65 64 20 62 79 20 6f 74 68 65 72  invoked by other
e660: 0a 2a 2a 20 74 68 72 65 61 64 73 20 77 68 69 6c  .** threads whil
e670: 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67  e sqlite3_config
e680: 28 29 20 69 73 20 72 75 6e 6e 69 6e 67 2e 20 20  () is running.  
e690: 46 75 72 74 68 65 72 6d 6f 72 65 2c 20 73 71 6c  Furthermore, sql
e6a0: 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 0a 2a 2a  ite3_config().**
e6b0: 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 69 6e 76   may only be inv
e6c0: 6f 6b 65 64 20 70 72 69 6f 72 20 74 6f 20 6c 69  oked prior to li
e6d0: 62 72 61 72 79 20 69 6e 69 74 69 61 6c 69 7a 61  brary initializa
e6e0: 74 69 6f 6e 20 75 73 69 6e 67 0a 2a 2a 20 5b 73  tion using.** [s
e6f0: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
e700: 65 28 29 5d 20 6f 72 20 61 66 74 65 72 20 73 68  e()] or after sh
e710: 75 74 64 6f 77 6e 20 62 79 20 5b 73 71 6c 69 74  utdown by [sqlit
e720: 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d 2e 0a  e3_shutdown()]..
e730: 2a 2a 20 4e 6f 74 65 2c 20 68 6f 77 65 76 65 72  ** Note, however
e740: 2c 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f 63  , that sqlite3_c
e750: 6f 6e 66 69 67 28 29 20 63 61 6e 20 62 65 20 63  onfig() can be c
e760: 61 6c 6c 65 64 20 61 73 20 70 61 72 74 20 6f 66  alled as part of
e770: 20 74 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e   the.** implemen
e780: 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 61 70 70  tation of an app
e790: 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64  lication-defined
e7a0: 20 5b 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69   [sqlite3_os_ini
e7b0: 74 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  t()]..**.** The 
e7c0: 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74  first argument t
e7d0: 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67  o sqlite3_config
e7e0: 28 29 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72  () is an integer
e7f0: 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46  .** [SQLITE_CONF
e800: 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 20  IG_SINGLETHREAD 
e810: 7c 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  | configuration 
e820: 6f 70 74 69 6f 6e 5d 20 74 68 61 74 20 64 65 74  option] that det
e830: 65 72 6d 69 6e 65 73 0a 2a 2a 20 77 68 61 74 20  ermines.** what 
e840: 70 72 6f 70 65 72 74 79 20 6f 66 20 53 51 4c 69  property of SQLi
e850: 74 65 20 69 73 20 74 6f 20 62 65 20 63 6f 6e 66  te is to be conf
e860: 69 67 75 72 65 64 2e 20 20 53 75 62 73 65 71 75  igured.  Subsequ
e870: 65 6e 74 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a  ent arguments.**
e880: 20 76 61 72 79 20 64 65 70 65 6e 64 69 6e 67 20   vary depending 
e890: 6f 6e 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 43  on the [SQLITE_C
e8a0: 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45  ONFIG_SINGLETHRE
e8b0: 41 44 20 7c 20 63 6f 6e 66 69 67 75 72 61 74 69  AD | configurati
e8c0: 6f 6e 20 6f 70 74 69 6f 6e 5d 0a 2a 2a 20 69 6e  on option].** in
e8d0: 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
e8e0: 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  ent..**.** When 
e8f0: 61 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  a configuration 
e900: 6f 70 74 69 6f 6e 20 69 73 20 73 65 74 2c 20 73  option is set, s
e910: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20  qlite3_config() 
e920: 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f  returns [SQLITE_
e930: 4f 4b 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 6f  OK]..** If the o
e940: 70 74 69 6f 6e 20 69 73 20 75 6e 6b 6e 6f 77 6e  ption is unknown
e950: 20 6f 72 20 53 51 4c 69 74 65 20 69 73 20 75 6e   or SQLite is un
e960: 61 62 6c 65 20 74 6f 20 73 65 74 20 74 68 65 20  able to set the 
e970: 6f 70 74 69 6f 6e 0a 2a 2a 20 74 68 65 6e 20 74  option.** then t
e980: 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
e990: 72 6e 73 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 5b  rns a non-zero [
e9a0: 65 72 72 6f 72 20 63 6f 64 65 5d 2e 0a 2a 2a 0a  error code]..**.
e9b0: 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
e9c0: 0a 2a 2a 20 5b 48 31 34 31 30 33 5d 20 5b 48 31  .** [H14103] [H1
e9d0: 34 31 30 36 5d 20 5b 48 31 34 31 32 30 5d 20 5b  4106] [H14120] [
e9e0: 48 31 34 31 32 33 5d 20 5b 48 31 34 31 32 36 5d  H14123] [H14126]
e9f0: 20 5b 48 31 34 31 32 39 5d 20 5b 48 31 34 31 33   [H14129] [H1413
ea00: 32 5d 20 5b 48 31 34 31 33 35 5d 0a 2a 2a 20 5b  2] [H14135].** [
ea10: 48 31 34 31 33 38 5d 20 5b 48 31 34 31 34 31 5d  H14138] [H14141]
ea20: 20 5b 48 31 34 31 34 34 5d 20 5b 48 31 34 31 34   [H14144] [H1414
ea30: 37 5d 20 5b 48 31 34 31 35 30 5d 20 5b 48 31 34  7] [H14150] [H14
ea40: 31 35 33 5d 20 5b 48 31 34 31 35 36 5d 20 5b 48  153] [H14156] [H
ea50: 31 34 31 35 39 5d 0a 2a 2a 20 5b 48 31 34 31 36  14159].** [H1416
ea60: 32 5d 20 5b 48 31 34 31 36 35 5d 20 5b 48 31 34  2] [H14165] [H14
ea70: 31 36 38 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  168].*/.SQLITE_A
ea80: 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49  PI SQLITE_EXPERI
ea90: 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74  MENTAL int sqlit
eaa0: 65 33 5f 63 6f 6e 66 69 67 28 69 6e 74 2c 20 2e  e3_config(int, .
eab0: 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  ..);../*.** CAPI
eac0: 33 52 45 46 3a 20 43 6f 6e 66 69 67 75 72 65 20  3REF: Configure 
ead0: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
eae0: 69 6f 6e 73 20 20 7b 48 31 34 32 30 30 7d 20 3c  ions  {H14200} <
eaf0: 53 32 30 30 30 30 3e 0a 2a 2a 20 45 58 50 45 52  S20000>.** EXPER
eb00: 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68  IMENTAL.**.** Th
eb10: 65 20 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e  e sqlite3_db_con
eb20: 66 69 67 28 29 20 69 6e 74 65 72 66 61 63 65 20  fig() interface 
eb30: 69 73 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20  is used to make 
eb40: 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 0a 2a 2a  configuration.**
eb50: 20 63 68 61 6e 67 65 73 20 74 6f 20 61 20 5b 64   changes to a [d
eb60: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
eb70: 6f 6e 5d 2e 20 20 54 68 65 20 69 6e 74 65 72 66  on].  The interf
eb80: 61 63 65 20 69 73 20 73 69 6d 69 6c 61 72 20 74  ace is similar t
eb90: 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f  o.** [sqlite3_co
eba0: 6e 66 69 67 28 29 5d 20 65 78 63 65 70 74 20 74  nfig()] except t
ebb0: 68 61 74 20 74 68 65 20 63 68 61 6e 67 65 73 20  hat the changes 
ebc0: 61 70 70 6c 79 20 74 6f 20 61 20 73 69 6e 67 6c  apply to a singl
ebd0: 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63  e.** [database c
ebe0: 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 73 70 65 63  onnection] (spec
ebf0: 69 66 69 65 64 20 69 6e 20 74 68 65 20 66 69 72  ified in the fir
ec00: 73 74 20 61 72 67 75 6d 65 6e 74 29 2e 20 20 54  st argument).  T
ec10: 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 64 62  he.** sqlite3_db
ec20: 5f 63 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66  _config() interf
ec30: 61 63 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20  ace can only be 
ec40: 75 73 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79  used immediately
ec50: 20 61 66 74 65 72 0a 2a 2a 20 74 68 65 20 64 61   after.** the da
ec60: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
ec70: 6e 20 69 73 20 63 72 65 61 74 65 64 20 75 73 69  n is created usi
ec80: 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e  ng [sqlite3_open
ec90: 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  ()],.** [sqlite3
eca0: 5f 6f 70 65 6e 31 36 28 29 5d 2c 20 6f 72 20 5b  _open16()], or [
ecb0: 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28  sqlite3_open_v2(
ecc0: 29 5d 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20  )].  .**.** The 
ecd0: 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
ece0: 74 6f 20 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f  to sqlite3_db_co
ecf0: 6e 66 69 67 28 44 2c 56 2c 2e 2e 2e 29 20 20 69  nfig(D,V,...)  i
ed00: 73 20 74 68 65 0a 2a 2a 20 63 6f 6e 66 69 67 75  s the.** configu
ed10: 72 61 74 69 6f 6e 20 76 65 72 62 20 2d 20 61 6e  ration verb - an
ed20: 20 69 6e 74 65 67 65 72 20 63 6f 64 65 20 74 68   integer code th
ed30: 61 74 20 69 6e 64 69 63 61 74 65 73 20 77 68 61  at indicates wha
ed40: 74 0a 2a 2a 20 61 73 70 65 63 74 20 6f 66 20 74  t.** aspect of t
ed50: 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  he [database con
ed60: 6e 65 63 74 69 6f 6e 5d 20 69 73 20 62 65 69 6e  nection] is bein
ed70: 67 20 63 6f 6e 66 69 67 75 72 65 64 2e 0a 2a 2a  g configured..**
ed80: 20 54 68 65 20 6f 6e 6c 79 20 63 68 6f 69 63 65   The only choice
ed90: 20 66 6f 72 20 74 68 69 73 20 76 61 6c 75 65 20   for this value 
eda0: 69 73 20 5b 53 51 4c 49 54 45 5f 44 42 43 4f 4e  is [SQLITE_DBCON
edb0: 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 5d 2e 0a  FIG_LOOKASIDE]..
edc0: 2a 2a 20 4e 65 77 20 76 65 72 62 73 20 61 72 65  ** New verbs are
edd0: 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 61 64   likely to be ad
ede0: 64 65 64 20 69 6e 20 66 75 74 75 72 65 20 72 65  ded in future re
edf0: 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65  leases of SQLite
ee00: 2e 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20  ..** Additional 
ee10: 61 72 67 75 6d 65 6e 74 73 20 64 65 70 65 6e 64  arguments depend
ee20: 20 6f 6e 20 74 68 65 20 76 65 72 62 2e 0a 2a 2a   on the verb..**
ee30: 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
ee40: 3a 0a 2a 2a 20 5b 48 31 34 32 30 33 5d 20 5b 48  :.** [H14203] [H
ee50: 31 34 32 30 36 5d 20 5b 48 31 34 32 30 39 5d 20  14206] [H14209] 
ee60: 5b 48 31 34 32 31 32 5d 20 5b 48 31 34 32 31 35  [H14212] [H14215
ee70: 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
ee80: 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e  SQLITE_EXPERIMEN
ee90: 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  TAL int sqlite3_
eea0: 64 62 5f 63 6f 6e 66 69 67 28 73 71 6c 69 74 65  db_config(sqlite
eeb0: 33 2a 2c 20 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29  3*, int op, ...)
eec0: 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
eed0: 46 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61  F: Memory Alloca
eee0: 74 69 6f 6e 20 52 6f 75 74 69 6e 65 73 20 7b 48  tion Routines {H
eef0: 31 30 31 35 35 7d 20 3c 53 32 30 31 32 30 3e 0a  10155} <S20120>.
ef00: 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a  ** EXPERIMENTAL.
ef10: 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  **.** An instanc
ef20: 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74  e of this object
ef30: 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74   defines the int
ef40: 65 72 66 61 63 65 20 62 65 74 77 65 65 6e 20 53  erface between S
ef50: 51 4c 69 74 65 0a 2a 2a 20 61 6e 64 20 6c 6f 77  QLite.** and low
ef60: 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c  -level memory al
ef70: 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65  location routine
ef80: 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 62  s..**.** This ob
ef90: 6a 65 63 74 20 69 73 20 75 73 65 64 20 69 6e 20  ject is used in 
efa0: 6f 6e 6c 79 20 6f 6e 65 20 70 6c 61 63 65 20 69  only one place i
efb0: 6e 20 74 68 65 20 53 51 4c 69 74 65 20 69 6e 74  n the SQLite int
efc0: 65 72 66 61 63 65 2e 0a 2a 2a 20 41 20 70 6f 69  erface..** A poi
efd0: 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61  nter to an insta
efe0: 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65  nce of this obje
eff0: 63 74 20 69 73 20 74 68 65 20 61 72 67 75 6d 65  ct is the argume
f000: 6e 74 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65  nt to.** [sqlite
f010: 33 5f 63 6f 6e 66 69 67 28 29 5d 20 77 68 65 6e  3_config()] when
f020: 20 74 68 65 20 63 6f 6e 66 69 67 75 72 61 74 69   the configurati
f030: 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 0a 2a 2a 20  on option is.** 
f040: 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d  [SQLITE_CONFIG_M
f050: 41 4c 4c 4f 43 5d 20 6f 72 20 5b 53 51 4c 49 54  ALLOC] or [SQLIT
f060: 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c  E_CONFIG_GETMALL
f070: 4f 43 5d 2e 20 20 0a 2a 2a 20 42 79 20 63 72 65  OC].  .** By cre
f080: 61 74 69 6e 67 20 61 6e 20 69 6e 73 74 61 6e 63  ating an instanc
f090: 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74  e of this object
f0a0: 0a 2a 2a 20 61 6e 64 20 70 61 73 73 69 6e 67 20  .** and passing 
f0b0: 69 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63  it to [sqlite3_c
f0c0: 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43  onfig]([SQLITE_C
f0d0: 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 5d 29 0a 2a  ONFIG_MALLOC]).*
f0e0: 2a 20 64 75 72 69 6e 67 20 63 6f 6e 66 69 67 75  * during configu
f0f0: 72 61 74 69 6f 6e 2c 20 61 6e 20 61 70 70 6c 69  ration, an appli
f100: 63 61 74 69 6f 6e 20 63 61 6e 20 73 70 65 63 69  cation can speci
f110: 66 79 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76  fy an alternativ
f120: 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  e.** memory allo
f130: 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d  cation subsystem
f140: 20 66 6f 72 20 53 51 4c 69 74 65 20 74 6f 20 75   for SQLite to u
f150: 73 65 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74  se for all of it
f160: 73 0a 2a 2a 20 64 79 6e 61 6d 69 63 20 6d 65 6d  s.** dynamic mem
f170: 6f 72 79 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a  ory needs..**.**
f180: 20 4e 6f 74 65 20 74 68 61 74 20 53 51 4c 69 74   Note that SQLit
f190: 65 20 63 6f 6d 65 73 20 77 69 74 68 20 73 65 76  e comes with sev
f1a0: 65 72 61 6c 20 5b 62 75 69 6c 74 2d 69 6e 20 6d  eral [built-in m
f1b0: 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 73  emory allocators
f1c0: 5d 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 70 65  ].** that are pe
f1d0: 72 66 65 63 74 6c 79 20 61 64 65 71 75 61 74 65  rfectly adequate
f1e0: 20 66 6f 72 20 74 68 65 20 6f 76 65 72 77 68 65   for the overwhe
f1f0: 6c 6d 69 6e 67 20 6d 61 6a 6f 72 69 74 79 20 6f  lming majority o
f200: 66 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a  f applications.*
f210: 2a 20 61 6e 64 20 74 68 61 74 20 74 68 69 73 20  * and that this 
f220: 6f 62 6a 65 63 74 20 69 73 20 6f 6e 6c 79 20 75  object is only u
f230: 73 65 66 75 6c 20 74 6f 20 61 20 74 69 6e 79 20  seful to a tiny 
f240: 6d 69 6e 6f 72 69 74 79 20 6f 66 20 61 70 70 6c  minority of appl
f250: 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 77 69 74 68  ications.** with
f260: 20 73 70 65 63 69 61 6c 69 7a 65 64 20 6d 65 6d   specialized mem
f270: 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72  ory allocation r
f280: 65 71 75 69 72 65 6d 65 6e 74 73 2e 20 20 54 68  equirements.  Th
f290: 69 73 20 6f 62 6a 65 63 74 20 69 73 0a 2a 2a 20  is object is.** 
f2a0: 61 6c 73 6f 20 75 73 65 64 20 64 75 72 69 6e 67  also used during
f2b0: 20 74 65 73 74 69 6e 67 20 6f 66 20 53 51 4c 69   testing of SQLi
f2c0: 74 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73  te in order to s
f2d0: 70 65 63 69 66 79 20 61 6e 20 61 6c 74 65 72 6e  pecify an altern
f2e0: 61 74 69 76 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20  ative.** memory 
f2f0: 61 6c 6c 6f 63 61 74 6f 72 20 74 68 61 74 20 73  allocator that s
f300: 69 6d 75 6c 61 74 65 73 20 6d 65 6d 6f 72 79 20  imulates memory 
f310: 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 6f  out-of-memory co
f320: 6e 64 69 74 69 6f 6e 73 20 69 6e 0a 2a 2a 20 6f  nditions in.** o
f330: 72 64 65 72 20 74 6f 20 76 65 72 69 66 79 20 74  rder to verify t
f340: 68 61 74 20 53 51 4c 69 74 65 20 72 65 63 6f 76  hat SQLite recov
f350: 65 72 73 20 67 72 61 63 65 66 75 6c 6c 79 20 66  ers gracefully f
f360: 72 6f 6d 20 73 75 63 68 0a 2a 2a 20 63 6f 6e 64  rom such.** cond
f370: 69 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  itions..**.** Th
f380: 65 20 78 4d 61 6c 6c 6f 63 20 61 6e 64 20 78 46  e xMalloc and xF
f390: 72 65 65 20 6d 65 74 68 6f 64 73 20 6d 75 73 74  ree methods must
f3a0: 20 77 6f 72 6b 20 6c 69 6b 65 20 74 68 65 0a 2a   work like the.*
f3b0: 2a 20 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 66  * malloc() and f
f3c0: 72 65 65 28 29 20 66 75 6e 63 74 69 6f 6e 73 20  ree() functions 
f3d0: 66 72 6f 6d 20 74 68 65 20 73 74 61 6e 64 61 72  from the standar
f3e0: 64 20 43 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 20  d C library..** 
f3f0: 54 68 65 20 78 52 65 61 6c 6c 6f 63 20 6d 65 74  The xRealloc met
f400: 68 6f 64 20 6d 75 73 74 20 77 6f 72 6b 20 6c 69  hod must work li
f410: 6b 65 20 72 65 61 6c 6c 6f 63 28 29 20 66 72 6f  ke realloc() fro
f420: 6d 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 43  m the standard C
f430: 20 6c 69 62 72 61 72 79 0a 2a 2a 20 77 69 74 68   library.** with
f440: 20 74 68 65 20 65 78 63 65 70 74 69 6f 6e 20 74   the exception t
f450: 68 61 74 20 69 66 20 74 68 65 20 73 65 63 6f 6e  hat if the secon
f460: 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 52  d argument to xR
f470: 65 61 6c 6c 6f 63 20 69 73 20 7a 65 72 6f 2c 0a  ealloc is zero,.
f480: 2a 2a 20 78 52 65 61 6c 6c 6f 63 20 6d 75 73 74  ** xRealloc must
f490: 20 62 65 20 61 20 6e 6f 2d 6f 70 20 2d 20 69 74   be a no-op - it
f4a0: 20 6d 75 73 74 20 6e 6f 74 20 70 65 72 66 6f 72   must not perfor
f4b0: 6d 20 61 6e 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  m any allocation
f4c0: 20 6f 72 0a 2a 2a 20 64 65 61 6c 6c 6f 63 61 74   or.** deallocat
f4d0: 69 6f 6e 2e 20 20 53 51 4c 69 74 65 20 67 75 61  ion.  SQLite gua
f4e0: 72 61 6e 74 65 65 64 73 20 74 68 61 74 20 74 68  ranteeds that th
f4f0: 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
f500: 74 20 74 6f 0a 2a 2a 20 78 52 65 61 6c 6c 6f 63  t to.** xRealloc
f510: 20 69 73 20 61 6c 77 61 79 73 20 61 20 76 61 6c   is always a val
f520: 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 61  ue returned by a
f530: 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 78   prior call to x
f540: 52 6f 75 6e 64 75 70 2e 0a 2a 2a 20 41 6e 64 20  Roundup..** And 
f550: 73 6f 20 69 6e 20 63 61 73 65 73 20 77 68 65 72  so in cases wher
f560: 65 20 78 52 6f 75 6e 64 75 70 20 61 6c 77 61 79  e xRoundup alway
f570: 73 20 72 65 74 75 72 6e 73 20 61 20 70 6f 73 69  s returns a posi
f580: 74 69 76 65 20 6e 75 6d 62 65 72 2c 0a 2a 2a 20  tive number,.** 
f590: 78 52 65 61 6c 6c 6f 63 20 63 61 6e 20 70 65 72  xRealloc can per
f5a0: 66 6f 72 6d 20 65 78 61 63 74 6c 79 20 61 73 20  form exactly as 
f5b0: 74 68 65 20 73 74 61 6e 64 61 72 64 20 6c 69 62  the standard lib
f5c0: 72 61 72 79 20 72 65 61 6c 6c 6f 63 28 29 20 61  rary realloc() a
f5d0: 6e 64 0a 2a 2a 20 73 74 69 6c 6c 20 62 65 20 69  nd.** still be i
f5e0: 6e 20 63 6f 6d 70 6c 69 61 6e 63 65 20 77 69 74  n compliance wit
f5f0: 68 20 74 68 69 73 20 73 70 65 63 69 66 69 63 61  h this specifica
f600: 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 78 53 69 7a  tion..**.** xSiz
f610: 65 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20  e should return 
f620: 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 69  the allocated si
f630: 7a 65 20 6f 66 20 61 20 6d 65 6d 6f 72 79 20 61  ze of a memory a
f640: 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 70 72 65  llocation.** pre
f650: 76 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 64  viously obtained
f660: 20 66 72 6f 6d 20 78 4d 61 6c 6c 6f 63 20 6f 72   from xMalloc or
f670: 20 78 52 65 61 6c 6c 6f 63 2e 20 20 54 68 65 20   xRealloc.  The 
f680: 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 0a 2a  allocated size.*
f690: 2a 20 69 73 20 61 6c 77 61 79 73 20 61 74 20 6c  * is always at l
f6a0: 65 61 73 74 20 61 73 20 62 69 67 20 61 73 20 74  east as big as t
f6b0: 68 65 20 72 65 71 75 65 73 74 65 64 20 73 69 7a  he requested siz
f6c0: 65 20 62 75 74 20 6d 61 79 20 62 65 20 6c 61 72  e but may be lar
f6d0: 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78  ger..**.** The x
f6e0: 52 6f 75 6e 64 75 70 20 6d 65 74 68 6f 64 20 72  Roundup method r
f6f0: 65 74 75 72 6e 73 20 77 68 61 74 20 77 6f 75 6c  eturns what woul
f700: 64 20 62 65 20 74 68 65 20 61 6c 6c 6f 63 61 74  d be the allocat
f710: 65 64 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 61 20  ed size of.** a 
f720: 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
f730: 6e 20 67 69 76 65 6e 20 61 20 70 61 72 74 69 63  n given a partic
f740: 75 6c 61 72 20 72 65 71 75 65 73 74 65 64 20 73  ular requested s
f750: 69 7a 65 2e 20 20 4d 6f 73 74 20 6d 65 6d 6f 72  ize.  Most memor
f760: 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 6f 72 73 20  y.** allocators 
f770: 72 6f 75 6e 64 20 75 70 20 6d 65 6d 6f 72 79 20  round up memory 
f780: 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 74 20 6c  allocations at l
f790: 65 61 73 74 20 74 6f 20 74 68 65 20 6e 65 78 74  east to the next
f7a0: 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 6f 66 20   multiple.** of 
f7b0: 38 2e 20 20 53 6f 6d 65 20 61 6c 6c 6f 63 61 74  8.  Some allocat
f7c0: 6f 72 73 20 72 6f 75 6e 64 20 75 70 20 74 6f 20  ors round up to 
f7d0: 61 20 6c 61 72 67 65 72 20 6d 75 6c 74 69 70 6c  a larger multipl
f7e0: 65 20 6f 72 20 74 6f 20 61 20 70 6f 77 65 72 20  e or to a power 
f7f0: 6f 66 20 32 2e 0a 2a 2a 20 45 76 65 72 79 20 6d  of 2..** Every m
f800: 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
f810: 20 72 65 71 75 65 73 74 20 63 6f 6d 69 6e 67 20   request coming 
f820: 69 6e 20 74 68 72 6f 75 67 68 20 5b 73 71 6c 69  in through [sqli
f830: 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 0a 2a 2a  te3_malloc()].**
f840: 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 61   or [sqlite3_rea
f850: 6c 6c 6f 63 28 29 5d 20 66 69 72 73 74 20 63 61  lloc()] first ca
f860: 6c 6c 73 20 78 52 6f 75 6e 64 75 70 2e 20 20 49  lls xRoundup.  I
f870: 66 20 78 52 6f 75 6e 64 75 70 20 72 65 74 75 72  f xRoundup retur
f880: 6e 73 20 30 2c 20 0a 2a 2a 20 74 68 61 74 20 63  ns 0, .** that c
f890: 61 75 73 65 73 20 74 68 65 20 63 6f 72 72 65 73  auses the corres
f8a0: 70 6f 6e 64 69 6e 67 20 6d 65 6d 6f 72 79 20 61  ponding memory a
f8b0: 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 66 61 69  llocation to fai
f8c0: 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 49 6e  l..**.** The xIn
f8d0: 69 74 20 6d 65 74 68 6f 64 20 69 6e 69 74 69 61  it method initia
f8e0: 6c 69 7a 65 73 20 74 68 65 20 6d 65 6d 6f 72 79  lizes the memory
f8f0: 20 61 6c 6c 6f 63 61 74 6f 72 2e 20 20 28 46 6f   allocator.  (Fo
f900: 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 69 74  r example,.** it
f910: 20 6d 69 67 68 74 20 61 6c 6c 6f 63 61 74 65 20   might allocate 
f920: 61 6e 79 20 72 65 71 75 69 72 65 20 6d 75 74 65  any require mute
f930: 78 65 73 20 6f 72 20 69 6e 69 74 69 61 6c 69 7a  xes or initializ
f940: 65 20 69 6e 74 65 72 6e 61 6c 20 64 61 74 61 0a  e internal data.
f950: 2a 2a 20 73 74 72 75 63 74 75 72 65 73 2e 20 20  ** structures.  
f960: 54 68 65 20 78 53 68 75 74 64 6f 77 6e 20 6d 65  The xShutdown me
f970: 74 68 6f 64 20 69 73 20 69 6e 76 6f 6b 65 64 20  thod is invoked 
f980: 28 69 6e 64 69 72 65 63 74 6c 79 29 20 62 79 0a  (indirectly) by.
f990: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74  ** [sqlite3_shut
f9a0: 64 6f 77 6e 28 29 5d 20 61 6e 64 20 73 68 6f 75  down()] and shou
f9b0: 6c 64 20 64 65 61 6c 6c 6f 63 61 74 65 20 61 6e  ld deallocate an
f9c0: 79 20 72 65 73 6f 75 72 63 65 73 20 61 63 71 75  y resources acqu
f9d0: 69 72 65 64 0a 2a 2a 20 62 79 20 78 49 6e 69 74  ired.** by xInit
f9e0: 2e 20 20 54 68 65 20 70 41 70 70 44 61 74 61 20  .  The pAppData 
f9f0: 70 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64 20  pointer is used 
fa00: 61 73 20 74 68 65 20 6f 6e 6c 79 20 70 61 72 61  as the only para
fa10: 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 78 49 6e 69  meter to.** xIni
fa20: 74 20 61 6e 64 20 78 53 68 75 74 64 6f 77 6e 2e  t and xShutdown.
fa30: 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 68 6f  .**.** SQLite ho
fa40: 6c 64 73 20 74 68 65 20 5b 53 51 4c 49 54 45 5f  lds the [SQLITE_
fa50: 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53  MUTEX_STATIC_MAS
fa60: 54 45 52 5d 20 6d 75 74 65 78 20 77 68 65 6e 20  TER] mutex when 
fa70: 69 74 20 69 6e 76 6f 6b 65 73 0a 2a 2a 20 74 68  it invokes.** th
fa80: 65 20 78 49 6e 69 74 20 6d 65 74 68 6f 64 2c 20  e xInit method, 
fa90: 73 6f 20 74 68 65 20 78 49 6e 69 74 20 6d 65 74  so the xInit met
faa0: 68 6f 64 20 6e 65 65 64 20 6e 6f 74 20 62 65 20  hod need not be 
fab0: 74 68 72 65 61 64 73 61 66 65 2e 20 20 54 68 65  threadsafe.  The
fac0: 0a 2a 2a 20 78 53 68 75 74 64 6f 77 6e 20 6d 65  .** xShutdown me
fad0: 74 68 6f 64 20 69 73 20 6f 6e 6c 79 20 63 61 6c  thod is only cal
fae0: 6c 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65  led from [sqlite
faf0: 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d 20 73 6f  3_shutdown()] so
fb00: 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20   it does.** not 
fb10: 6e 65 65 64 20 74 6f 20 62 65 20 74 68 72 65 61  need to be threa
fb20: 64 73 61 66 65 20 65 69 74 68 65 72 2e 20 20 46  dsafe either.  F
fb30: 6f 72 20 61 6c 6c 20 6f 74 68 65 72 20 6d 65 74  or all other met
fb40: 68 6f 64 73 2c 20 53 51 4c 69 74 65 0a 2a 2a 20  hods, SQLite.** 
fb50: 68 6f 6c 64 73 20 74 68 65 20 5b 53 51 4c 49 54  holds the [SQLIT
fb60: 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
fb70: 45 4d 5d 20 6d 75 74 65 78 20 61 73 20 6c 6f 6e  EM] mutex as lon
fb80: 67 20 61 73 20 74 68 65 0a 2a 2a 20 5b 53 51 4c  g as the.** [SQL
fb90: 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54  ITE_CONFIG_MEMST
fba0: 41 54 55 53 5d 20 63 6f 6e 66 69 67 75 72 61 74  ATUS] configurat
fbb0: 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 20 74 75  ion option is tu
fbc0: 72 6e 65 64 20 6f 6e 20 28 77 68 69 63 68 0a 2a  rned on (which.*
fbd0: 2a 20 69 74 20 69 73 20 62 79 20 64 65 66 61 75  * it is by defau
fbe0: 6c 74 29 20 61 6e 64 20 73 6f 20 74 68 65 20 6d  lt) and so the m
fbf0: 65 74 68 6f 64 73 20 61 72 65 20 61 75 74 6f 6d  ethods are autom
fc00: 61 74 69 63 61 6c 6c 79 20 73 65 72 69 61 6c 69  atically seriali
fc10: 7a 65 64 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c  zed..** However,
fc20: 20 69 66 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46   if [SQLITE_CONF
fc30: 49 47 5f 4d 45 4d 53 54 41 54 55 53 5d 20 69 73  IG_MEMSTATUS] is
fc40: 20 64 69 73 61 62 6c 65 64 2c 20 74 68 65 6e 20   disabled, then 
fc50: 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 6d 65 74  the other.** met
fc60: 68 6f 64 73 20 6d 75 73 74 20 62 65 20 74 68 72  hods must be thr
fc70: 65 61 64 73 61 66 65 20 6f 72 20 65 6c 73 65 20  eadsafe or else 
fc80: 6d 61 6b 65 20 74 68 65 69 72 20 6f 77 6e 20 61  make their own a
fc90: 72 72 61 6e 67 65 6d 65 6e 74 73 20 66 6f 72 0a  rrangements for.
fca0: 2a 2a 20 73 65 72 69 61 6c 69 7a 61 74 69 6f 6e  ** serialization
fcb0: 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77  ..**.** SQLite w
fcc0: 69 6c 6c 20 6e 65 76 65 72 20 69 6e 76 6f 6b 65  ill never invoke
fcd0: 20 78 49 6e 69 74 28 29 20 6d 6f 72 65 20 74 68   xInit() more th
fce0: 61 6e 20 6f 6e 63 65 20 77 69 74 68 6f 75 74 20  an once without 
fcf0: 61 6e 20 69 6e 74 65 72 76 65 6e 69 6e 67 0a 2a  an intervening.*
fd00: 2a 20 63 61 6c 6c 20 74 6f 20 78 53 68 75 74 64  * call to xShutd
fd10: 6f 77 6e 28 29 2e 0a 2a 2f 0a 74 79 70 65 64 65  own()..*/.typede
fd20: 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  f struct sqlite3
fd30: 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 73 71 6c  _mem_methods sql
fd40: 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73  ite3_mem_methods
fd50: 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  ;.struct sqlite3
fd60: 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 7b 0a 20  _mem_methods {. 
fd70: 20 76 6f 69 64 20 2a 28 2a 78 4d 61 6c 6c 6f 63   void *(*xMalloc
fd80: 29 28 69 6e 74 29 3b 20 20 20 20 20 20 20 20 20  )(int);         
fd90: 2f 2a 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  /* Memory alloca
fda0: 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  tion function */
fdb0: 0a 20 20 76 6f 69 64 20 28 2a 78 46 72 65 65 29  .  void (*xFree)
fdc0: 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20 20 20  (void*);        
fdd0: 20 20 2f 2a 20 46 72 65 65 20 61 20 70 72 69 6f    /* Free a prio
fde0: 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a  r allocation */.
fdf0: 20 20 76 6f 69 64 20 2a 28 2a 78 52 65 61 6c 6c    void *(*xReall
fe00: 6f 63 29 28 76 6f 69 64 2a 2c 69 6e 74 29 3b 20  oc)(void*,int); 
fe10: 20 2f 2a 20 52 65 73 69 7a 65 20 61 6e 20 61 6c   /* Resize an al
fe20: 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  location */.  in
fe30: 74 20 28 2a 78 53 69 7a 65 29 28 76 6f 69 64 2a  t (*xSize)(void*
fe40: 29 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  );           /* 
fe50: 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20  Return the size 
fe60: 6f 66 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e  of an allocation
fe70: 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 52 6f 75   */.  int (*xRou
fe80: 6e 64 75 70 29 28 69 6e 74 29 3b 20 20 20 20 20  ndup)(int);     
fe90: 20 20 20 20 20 2f 2a 20 52 6f 75 6e 64 20 75 70       /* Round up
fea0: 20 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f   request size to
feb0: 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65   allocation size
fec0: 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 49 6e 69   */.  int (*xIni
fed0: 74 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20  t)(void*);      
fee0: 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69       /* Initiali
fef0: 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  ze the memory al
ff00: 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20 20 76 6f 69  locator */.  voi
ff10: 64 20 28 2a 78 53 68 75 74 64 6f 77 6e 29 28 76  d (*xShutdown)(v
ff20: 6f 69 64 2a 29 3b 20 20 20 20 20 20 2f 2a 20 44  oid*);      /* D
ff30: 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20  einitialize the 
ff40: 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72  memory allocator
ff50: 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 70 70   */.  void *pApp
ff60: 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20  Data;           
ff70: 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74       /* Argument
ff80: 20 74 6f 20 78 49 6e 69 74 28 29 20 61 6e 64 20   to xInit() and 
ff90: 78 53 68 75 74 64 6f 77 6e 28 29 20 2a 2f 0a 7d  xShutdown() */.}
ffa0: 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
ffb0: 46 3a 20 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e  F: Configuration
ffc0: 20 4f 70 74 69 6f 6e 73 20 7b 48 31 30 31 36 30   Options {H10160
ffd0: 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 20 45 58  } <S20000>.** EX
ffe0: 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a  PERIMENTAL.**.**
fff0: 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73   These constants
10000 20 61 72 65 20 74 68 65 20 61 76 61 69 6c 61 62   are the availab
10010 6c 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 66 69  le integer confi
10020 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73  guration options
10030 20 74 68 61 74 0a 2a 2a 20 63 61 6e 20 62 65 20   that.** can be 
10040 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69  passed as the fi
10050 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
10060 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e  the [sqlite3_con
10070 66 69 67 28 29 5d 20 69 6e 74 65 72 66 61 63 65  fig()] interface
10080 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 63 6f 6e 66  ..**.** New conf
10090 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e  iguration option
100a0 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69  s may be added i
100b0 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65  n future release
100c0 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 20  s of SQLite..** 
100d0 45 78 69 73 74 69 6e 67 20 63 6f 6e 66 69 67 75  Existing configu
100e0 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 6d  ration options m
100f0 69 67 68 74 20 62 65 20 64 69 73 63 6f 6e 74 69  ight be disconti
10100 6e 75 65 64 2e 20 20 41 70 70 6c 69 63 61 74 69  nued.  Applicati
10110 6f 6e 73 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 68  ons.** should ch
10120 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 63  eck the return c
10130 6f 64 65 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65  ode from [sqlite
10140 33 5f 63 6f 6e 66 69 67 28 29 5d 20 74 6f 20 6d  3_config()] to m
10150 61 6b 65 20 73 75 72 65 20 74 68 61 74 0a 2a 2a  ake sure that.**
10160 20 74 68 65 20 63 61 6c 6c 20 77 6f 72 6b 65 64   the call worked
10170 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  .  The [sqlite3_
10180 63 6f 6e 66 69 67 28 29 5d 20 69 6e 74 65 72 66  config()] interf
10190 61 63 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20  ace will return 
101a0 61 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 5b 65  a.** non-zero [e
101b0 72 72 6f 72 20 63 6f 64 65 5d 20 69 66 20 61 20  rror code] if a 
101c0 64 69 73 63 6f 6e 74 69 6e 75 65 64 20 6f 72 20  discontinued or 
101d0 75 6e 73 75 70 70 6f 72 74 65 64 20 63 6f 6e 66  unsupported conf
101e0 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e  iguration option
101f0 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a  .** is invoked..
10200 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64  **.** <dl>.** <d
10210 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  t>SQLITE_CONFIG_
10220 53 49 4e 47 4c 45 54 48 52 45 41 44 3c 2f 64 74  SINGLETHREAD</dt
10230 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 72 65 20 61  >.** <dd>There a
10240 72 65 20 6e 6f 20 61 72 67 75 6d 65 6e 74 73 20  re no arguments 
10250 74 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e 2e 20  to this option. 
10260 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 64 69 73   This option dis
10270 61 62 6c 65 73 0a 2a 2a 20 61 6c 6c 20 6d 75 74  ables.** all mut
10280 65 78 69 6e 67 20 61 6e 64 20 70 75 74 73 20 53  exing and puts S
10290 51 4c 69 74 65 20 69 6e 74 6f 20 61 20 6d 6f 64  QLite into a mod
102a0 65 20 77 68 65 72 65 20 69 74 20 63 61 6e 20 6f  e where it can o
102b0 6e 6c 79 20 62 65 20 75 73 65 64 0a 2a 2a 20 62  nly be used.** b
102c0 79 20 61 20 73 69 6e 67 6c 65 20 74 68 72 65 61  y a single threa
102d0 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64  d.</dd>.**.** <d
102e0 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  t>SQLITE_CONFIG_
102f0 4d 55 4c 54 49 54 48 52 45 41 44 3c 2f 64 74 3e  MULTITHREAD</dt>
10300 0a 2a 2a 20 3c 64 64 3e 54 68 65 72 65 20 61 72  .** <dd>There ar
10310 65 20 6e 6f 20 61 72 67 75 6d 65 6e 74 73 20 74  e no arguments t
10320 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e 2e 20 20  o this option.  
10330 54 68 69 73 20 6f 70 74 69 6f 6e 20 64 69 73 61  This option disa
10340 62 6c 65 73 0a 2a 2a 20 6d 75 74 65 78 69 6e 67  bles.** mutexing
10350 20 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f   on [database co
10360 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e 64 20 5b 70  nnection] and [p
10370 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
10380 74 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 54  t] objects..** T
10390 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 69  he application i
103a0 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f  s responsible fo
103b0 72 20 73 65 72 69 61 6c 69 7a 69 6e 67 20 61 63  r serializing ac
103c0 63 65 73 73 20 74 6f 0a 2a 2a 20 5b 64 61 74 61  cess to.** [data
103d0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
103e0 5d 20 61 6e 64 20 5b 70 72 65 70 61 72 65 64 20  ] and [prepared 
103f0 73 74 61 74 65 6d 65 6e 74 73 5d 2e 20 20 42 75  statements].  Bu
10400 74 20 6f 74 68 65 72 20 6d 75 74 65 78 65 73 0a  t other mutexes.
10410 2a 2a 20 61 72 65 20 65 6e 61 62 6c 65 64 20 73  ** are enabled s
10420 6f 20 74 68 61 74 20 53 51 4c 69 74 65 20 77 69  o that SQLite wi
10430 6c 6c 20 62 65 20 73 61 66 65 20 74 6f 20 75 73  ll be safe to us
10440 65 20 69 6e 20 61 20 6d 75 6c 74 69 2d 74 68 72  e in a multi-thr
10450 65 61 64 65 64 0a 2a 2a 20 65 6e 76 69 72 6f 6e  eaded.** environ
10460 6d 65 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73 20  ment as long as 
10470 6e 6f 20 74 77 6f 20 74 68 72 65 61 64 73 20 61  no two threads a
10480 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20 74 68  ttempt to use th
10490 65 20 73 61 6d 65 0a 2a 2a 20 5b 64 61 74 61 62  e same.** [datab
104a0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
104b0 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65  at the same time
104c0 2e 20 20 53 65 65 20 74 68 65 20 5b 74 68 72 65  .  See the [thre
104d0 61 64 69 6e 67 20 6d 6f 64 65 5d 0a 2a 2a 20 64  ading mode].** d
104e0 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72  ocumentation for
104f0 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f   additional info
10500 72 6d 61 74 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a  rmation.</dd>.**
10510 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43  .** <dt>SQLITE_C
10520 4f 4e 46 49 47 5f 53 45 52 49 41 4c 49 5a 45 44  ONFIG_SERIALIZED
10530 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65  </dt>.** <dd>The
10540 72 65 20 61 72 65 20 6e 6f 20 61 72 67 75 6d 65  re are no argume
10550 6e 74 73 20 74 6f 20 74 68 69 73 20 6f 70 74 69  nts to this opti
10560 6f 6e 2e 20 20 54 68 69 73 20 6f 70 74 69 6f 6e  on.  This option
10570 20 65 6e 61 62 6c 65 73 0a 2a 2a 20 61 6c 6c 20   enables.** all 
10580 6d 75 74 65 78 65 73 20 69 6e 63 6c 75 64 69 6e  mutexes includin
10590 67 20 74 68 65 20 72 65 63 75 72 73 69 76 65 0a  g the recursive.
105a0 2a 2a 20 6d 75 74 65 78 65 73 20 6f 6e 20 5b 64  ** mutexes on [d
105b0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
105c0 6f 6e 5d 20 61 6e 64 20 5b 70 72 65 70 61 72 65  on] and [prepare
105d0 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 6f 62 6a  d statement] obj
105e0 65 63 74 73 2e 0a 2a 2a 20 49 6e 20 74 68 69 73  ects..** In this
105f0 20 6d 6f 64 65 20 28 77 68 69 63 68 20 69 73 20   mode (which is 
10600 74 68 65 20 64 65 66 61 75 6c 74 20 77 68 65 6e  the default when
10610 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69   SQLite is compi
10620 6c 65 64 20 77 69 74 68 0a 2a 2a 20 5b 53 51 4c  led with.** [SQL
10630 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 31  ITE_THREADSAFE=1
10640 5d 29 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69  ]) the SQLite li
10650 62 72 61 72 79 20 77 69 6c 6c 20 69 74 73 65 6c  brary will itsel
10660 66 20 73 65 72 69 61 6c 69 7a 65 20 61 63 63 65  f serialize acce
10670 73 73 0a 2a 2a 20 74 6f 20 5b 64 61 74 61 62 61  ss.** to [databa
10680 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 20  se connections] 
10690 61 6e 64 20 5b 70 72 65 70 61 72 65 64 20 73 74  and [prepared st
106a0 61 74 65 6d 65 6e 74 73 5d 20 73 6f 20 74 68 61  atements] so tha
106b0 74 20 74 68 65 0a 2a 2a 20 61 70 70 6c 69 63 61  t the.** applica
106c0 74 69 6f 6e 20 69 73 20 66 72 65 65 20 74 6f 20  tion is free to 
106d0 75 73 65 20 74 68 65 20 73 61 6d 65 20 5b 64 61  use the same [da
106e0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
106f0 6e 5d 20 6f 72 20 74 68 65 0a 2a 2a 20 73 61 6d  n] or the.** sam
10700 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  e [prepared stat
10710 65 6d 65 6e 74 5d 20 69 6e 20 64 69 66 66 65 72  ement] in differ
10720 65 6e 74 20 74 68 72 65 61 64 73 20 61 74 20 74  ent threads at t
10730 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a 2a 2a  he same time..**
10740 20 53 65 65 20 74 68 65 20 5b 74 68 72 65 61 64   See the [thread
10750 69 6e 67 20 6d 6f 64 65 5d 20 64 6f 63 75 6d 65  ing mode] docume
10760 6e 74 61 74 69 6f 6e 20 66 6f 72 20 61 64 64 69  ntation for addi
10770 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69  tional informati
10780 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  on.</dd>.**.** <
10790 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  dt>SQLITE_CONFIG
107a0 5f 4d 41 4c 4c 4f 43 3c 2f 64 74 3e 0a 2a 2a 20  _MALLOC</dt>.** 
107b0 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20  <dd>This option 
107c0 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61  takes a single a
107d0 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73  rgument which is
107e0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
107f0 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20  .** instance of 
10800 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d  the [sqlite3_mem
10810 5f 6d 65 74 68 6f 64 73 5d 20 73 74 72 75 63 74  _methods] struct
10820 75 72 65 2e 20 20 54 68 65 20 61 72 67 75 6d 65  ure.  The argume
10830 6e 74 20 73 70 65 63 69 66 69 65 73 0a 2a 2a 20  nt specifies.** 
10840 61 6c 74 65 72 6e 61 74 69 76 65 20 6c 6f 77 2d  alternative low-
10850 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c  level memory all
10860 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73  ocation routines
10870 20 74 6f 20 62 65 20 75 73 65 64 20 69 6e 20 70   to be used in p
10880 6c 61 63 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6d  lace of.** the m
10890 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
108a0 20 72 6f 75 74 69 6e 65 73 20 62 75 69 6c 74 20   routines built 
108b0 69 6e 74 6f 20 53 51 4c 69 74 65 2e 3c 2f 64 64  into SQLite.</dd
108c0 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
108d0 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c  TE_CONFIG_GETMAL
108e0 4c 4f 43 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  LOC</dt>.** <dd>
108f0 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65  This option take
10900 73 20 61 20 73 69 6e 67 6c 65 20 61 72 67 75 6d  s a single argum
10910 65 6e 74 20 77 68 69 63 68 20 69 73 20 61 20 70  ent which is a p
10920 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20  ointer to an.** 
10930 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
10940 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74  [sqlite3_mem_met
10950 68 6f 64 73 5d 20 73 74 72 75 63 74 75 72 65 2e  hods] structure.
10960 20 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d    The [sqlite3_m
10970 65 6d 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 73  em_methods].** s
10980 74 72 75 63 74 75 72 65 20 69 73 20 66 69 6c 6c  tructure is fill
10990 65 64 20 77 69 74 68 20 74 68 65 20 63 75 72 72  ed with the curr
109a0 65 6e 74 6c 79 20 64 65 66 69 6e 65 64 20 6d 65  ently defined me
109b0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
109c0 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 20 54 68 69  routines..** Thi
109d0 73 20 6f 70 74 69 6f 6e 20 63 61 6e 20 62 65 20  s option can be 
109e0 75 73 65 64 20 74 6f 20 6f 76 65 72 6c 6f 61 64  used to overload
109f0 20 74 68 65 20 64 65 66 61 75 6c 74 20 6d 65 6d   the default mem
10a00 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a  ory allocation.*
10a10 2a 20 72 6f 75 74 69 6e 65 73 20 77 69 74 68 20  * routines with 
10a20 61 20 77 72 61 70 70 65 72 20 74 68 61 74 20 73  a wrapper that s
10a30 69 6d 75 6c 61 74 69 6f 6e 73 20 6d 65 6d 6f 72  imulations memor
10a40 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69  y allocation fai
10a50 6c 75 72 65 20 6f 72 0a 2a 2a 20 74 72 61 63 6b  lure or.** track
10a60 73 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 2c 20  s memory usage, 
10a70 66 6f 72 20 65 78 61 6d 70 6c 65 2e 3c 2f 64 64  for example.</dd
10a80 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
10a90 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41  TE_CONFIG_MEMSTA
10aa0 54 55 53 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  TUS</dt>.** <dd>
10ab0 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65  This option take
10ac0 73 20 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e  s single argumen
10ad0 74 20 6f 66 20 74 79 70 65 20 69 6e 74 2c 20 69  t of type int, i
10ae0 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 61 20  nterpreted as a 
10af0 0a 2a 2a 20 62 6f 6f 6c 65 61 6e 2c 20 77 68 69  .** boolean, whi
10b00 63 68 20 65 6e 61 62 6c 65 73 20 6f 72 20 64 69  ch enables or di
10b10 73 61 62 6c 65 73 20 74 68 65 20 63 6f 6c 6c 65  sables the colle
10b20 63 74 69 6f 6e 20 6f 66 20 6d 65 6d 6f 72 79 20  ction of memory 
10b30 61 6c 6c 6f 63 61 74 69 6f 6e 20 0a 2a 2a 20 73  allocation .** s
10b40 74 61 74 69 73 74 69 63 73 2e 20 57 68 65 6e 20  tatistics. When 
10b50 64 69 73 61 62 6c 65 64 2c 20 74 68 65 20 66 6f  disabled, the fo
10b60 6c 6c 6f 77 69 6e 67 20 53 51 4c 69 74 65 20 69  llowing SQLite i
10b70 6e 74 65 72 66 61 63 65 73 20 62 65 63 6f 6d 65  nterfaces become
10b80 20 0a 2a 2a 20 6e 6f 6e 2d 6f 70 65 72 61 74 69   .** non-operati
10b90 6f 6e 61 6c 3a 0a 2a 2a 20 20 20 3c 75 6c 3e 0a  onal:.**   <ul>.
10ba0 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 6c 69 74  **   <li> [sqlit
10bb0 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 29  e3_memory_used()
10bc0 5d 0a 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 6c  ].**   <li> [sql
10bd0 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68  ite3_memory_high
10be0 77 61 74 65 72 28 29 5d 0a 2a 2a 20 20 20 3c 6c  water()].**   <l
10bf0 69 3e 20 5b 73 71 6c 69 74 65 33 5f 73 6f 66 74  i> [sqlite3_soft
10c00 5f 68 65 61 70 5f 6c 69 6d 69 74 28 29 5d 0a 2a  _heap_limit()].*
10c10 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 6c 69 74 65  *   <li> [sqlite
10c20 33 5f 73 74 61 74 75 73 28 29 5d 0a 2a 2a 20 20  3_status()].**  
10c30 20 3c 2f 75 6c 3e 0a 2a 2a 20 3c 2f 64 64 3e 0a   </ul>.** </dd>.
10c40 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  **.** <dt>SQLITE
10c50 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 3c  _CONFIG_SCRATCH<
10c60 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73  /dt>.** <dd>This
10c70 20 6f 70 74 69 6f 6e 20 73 70 65 63 69 66 69 65   option specifie
10c80 73 20 61 20 73 74 61 74 69 63 20 6d 65 6d 6f 72  s a static memor
10c90 79 20 62 75 66 66 65 72 20 74 68 61 74 20 53 51  y buffer that SQ
10ca0 4c 69 74 65 20 63 61 6e 20 75 73 65 20 66 6f 72  Lite can use for
10cb0 0a 2a 2a 20 73 63 72 61 74 63 68 20 6d 65 6d 6f  .** scratch memo
10cc0 72 79 2e 20 20 54 68 65 72 65 20 61 72 65 20 74  ry.  There are t
10cd0 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 3a 20  hree arguments: 
10ce0 20 41 20 70 6f 69 6e 74 65 72 20 61 6e 20 38 2d   A pointer an 8-
10cf0 62 79 74 65 0a 2a 2a 20 61 6c 69 67 6e 65 64 20  byte.** aligned 
10d00 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 66 72  memory buffer fr
10d10 6f 6d 20 77 68 69 63 68 20 74 68 65 20 73 63 72  om which the scr
10d20 61 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20  ach allocations 
10d30 77 69 6c 6c 20 62 65 0a 2a 2a 20 64 72 61 77 6e  will be.** drawn
10d40 2c 20 74 68 65 20 73 69 7a 65 20 6f 66 20 65 61  , the size of ea
10d50 63 68 20 73 63 72 61 74 63 68 20 61 6c 6c 6f 63  ch scratch alloc
10d60 61 74 69 6f 6e 20 28 73 7a 29 2c 0a 2a 2a 20 61  ation (sz),.** a
10d70 6e 64 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e  nd the maximum n
10d80 75 6d 62 65 72 20 6f 66 20 73 63 72 61 74 63 68  umber of scratch
10d90 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 28 4e 29   allocations (N)
10da0 2e 20 20 54 68 65 20 73 7a 0a 2a 2a 20 61 72 67  .  The sz.** arg
10db0 75 6d 65 6e 74 20 6d 75 73 74 20 62 65 20 61 20  ument must be a 
10dc0 6d 75 6c 74 69 70 6c 65 20 6f 66 20 31 36 2e 20  multiple of 16. 
10dd0 54 68 65 20 73 7a 20 70 61 72 61 6d 65 74 65 72  The sz parameter
10de0 20 73 68 6f 75 6c 64 20 62 65 20 61 20 66 65 77   should be a few
10df0 20 62 79 74 65 73 0a 2a 2a 20 6c 61 72 67 65 72   bytes.** larger
10e00 20 74 68 61 6e 20 74 68 65 20 61 63 74 75 61 6c   than the actual
10e10 20 73 63 72 61 74 63 68 20 73 70 61 63 65 20 72   scratch space r
10e20 65 71 75 69 72 65 64 20 64 75 65 20 74 6f 20 69  equired due to i
10e30 6e 74 65 72 6e 61 6c 20 6f 76 65 72 68 65 61 64  nternal overhead
10e40 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61  ..** The first a
10e50 72 67 75 6d 65 6e 74 20 73 68 6f 75 6c 64 20 70  rgument should p
10e60 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 38 2d 62  ointer to an 8-b
10e70 79 74 65 20 61 6c 69 67 6e 65 64 20 62 75 66 66  yte aligned buff
10e80 65 72 0a 2a 2a 20 6f 66 20 61 74 20 6c 65 61 73  er.** of at leas
10e90 74 20 73 7a 2a 4e 20 62 79 74 65 73 20 6f 66 20  t sz*N bytes of 
10ea0 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 53 51 4c 69 74  memory..** SQLit
10eb0 65 20 77 69 6c 6c 20 75 73 65 20 6e 6f 20 6d 6f  e will use no mo
10ec0 72 65 20 74 68 61 6e 20 6f 6e 65 20 73 63 72 61  re than one scra
10ed0 74 63 68 20 62 75 66 66 65 72 20 61 74 20 6f 6e  tch buffer at on
10ee0 63 65 20 70 65 72 20 74 68 72 65 61 64 2c 20 73  ce per thread, s
10ef0 6f 0a 2a 2a 20 4e 20 73 68 6f 75 6c 64 20 62 65  o.** N should be
10f00 20 73 65 74 20 74 6f 20 74 68 65 20 65 78 70 65   set to the expe
10f10 63 74 65 64 20 6d 61 78 69 6d 75 6d 20 6e 75 6d  cted maximum num
10f20 62 65 72 20 6f 66 20 74 68 72 65 61 64 73 2e 20  ber of threads. 
10f30 20 54 68 65 20 73 7a 0a 2a 2a 20 70 61 72 61 6d   The sz.** param
10f40 65 74 65 72 20 73 68 6f 75 6c 64 20 62 65 20 36  eter should be 6
10f50 20 74 69 6d 65 73 20 74 68 65 20 73 69 7a 65 20   times the size 
10f60 6f 66 20 74 68 65 20 6c 61 72 67 65 73 74 20 64  of the largest d
10f70 61 74 61 62 61 73 65 20 70 61 67 65 20 73 69 7a  atabase page siz
10f80 65 2e 0a 2a 2a 20 53 63 72 61 74 63 68 20 62 75  e..** Scratch bu
10f90 66 66 65 72 73 20 61 72 65 20 75 73 65 64 20 61  ffers are used a
10fa0 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 62 74  s part of the bt
10fb0 72 65 65 20 62 61 6c 61 6e 63 65 20 6f 70 65 72  ree balance oper
10fc0 61 74 69 6f 6e 2e 20 20 49 66 0a 2a 2a 20 54 68  ation.  If.** Th
10fd0 65 20 62 74 72 65 65 20 62 61 6c 61 6e 63 65 72  e btree balancer
10fe0 20 6e 65 65 64 73 20 61 64 64 69 74 69 6f 6e 61   needs additiona
10ff0 6c 20 6d 65 6d 6f 72 79 20 62 65 79 6f 6e 64 20  l memory beyond 
11000 77 68 61 74 20 69 73 20 70 72 6f 76 69 64 65 64  what is provided
11010 20 62 79 0a 2a 2a 20 73 63 72 61 74 63 68 20 62   by.** scratch b
11020 75 66 66 65 72 73 20 6f 72 20 69 66 20 6e 6f 20  uffers or if no 
11030 73 63 72 61 74 63 68 20 62 75 66 66 65 72 20 73  scratch buffer s
11040 70 61 63 65 20 69 73 20 73 70 65 63 69 66 69 65  pace is specifie
11050 64 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 0a 2a  d, then SQLite.*
11060 2a 20 67 6f 65 73 20 74 6f 20 5b 73 71 6c 69 74  * goes to [sqlit
11070 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 74 6f 20  e3_malloc()] to 
11080 6f 62 74 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72  obtain the memor
11090 79 20 69 74 20 6e 65 65 64 73 2e 3c 2f 64 64 3e  y it needs.</dd>
110a0 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54  .**.** <dt>SQLIT
110b0 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43  E_CONFIG_PAGECAC
110c0 48 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  HE</dt>.** <dd>T
110d0 68 69 73 20 6f 70 74 69 6f 6e 20 73 70 65 63 69  his option speci
110e0 66 69 65 73 20 61 20 73 74 61 74 69 63 20 6d 65  fies a static me
110f0 6d 6f 72 79 20 62 75 66 66 65 72 20 74 68 61 74  mory buffer that
11100 20 53 51 4c 69 74 65 20 63 61 6e 20 75 73 65 20   SQLite can use 
11110 66 6f 72 0a 2a 2a 20 74 68 65 20 64 61 74 61 62  for.** the datab
11120 61 73 65 20 70 61 67 65 20 63 61 63 68 65 20 77  ase page cache w
11130 69 74 68 20 74 68 65 20 64 65 66 61 75 6c 74 20  ith the default 
11140 70 61 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65  page cache imple
11150 6d 65 6e 61 74 69 6f 6e 2e 20 20 0a 2a 2a 20 54  menation.  .** T
11160 68 69 73 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  his configuratio
11170 6e 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20  n should not be 
11180 75 73 65 64 20 69 66 20 61 6e 20 61 70 70 6c 69  used if an appli
11190 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 20 70 61  cation-define pa
111a0 67 65 0a 2a 2a 20 63 61 63 68 65 20 69 6d 70 6c  ge.** cache impl
111b0 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 6c 6f  ementation is lo
111c0 61 64 65 64 20 75 73 69 6e 67 20 74 68 65 20 53  aded using the S
111d0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41  QLITE_CONFIG_PCA
111e0 43 48 45 20 6f 70 74 69 6f 6e 2e 0a 2a 2a 20 54  CHE option..** T
111f0 68 65 72 65 20 61 72 65 20 74 68 72 65 65 20 61  here are three a
11200 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73  rguments to this
11210 20 6f 70 74 69 6f 6e 3a 20 41 20 70 6f 69 6e 74   option: A point
11220 65 72 20 74 6f 20 38 2d 62 79 74 65 20 61 6c 69  er to 8-byte ali
11230 67 6e 65 64 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20  gned.** memory, 
11240 74 68 65 20 73 69 7a 65 20 6f 66 20 65 61 63 68  the size of each
11250 20 70 61 67 65 20 62 75 66 66 65 72 20 28 73 7a   page buffer (sz
11260 29 2c 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65  ), and the numbe
11270 72 20 6f 66 20 70 61 67 65 73 20 28 4e 29 2e 0a  r of pages (N)..
11280 2a 2a 20 54 68 65 20 73 7a 20 61 72 67 75 6d 65  ** The sz argume
11290 6e 74 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65  nt should be the
112a0 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6c 61 72   size of the lar
112b0 67 65 73 74 20 64 61 74 61 62 61 73 65 20 70 61  gest database pa
112c0 67 65 0a 2a 2a 20 28 61 20 70 6f 77 65 72 20 6f  ge.** (a power o
112d0 66 20 74 77 6f 20 62 65 74 77 65 65 6e 20 35 31  f two between 51
112e0 32 20 61 6e 64 20 33 32 37 36 38 29 20 70 6c 75  2 and 32768) plu
112f0 73 20 61 20 6c 69 74 74 6c 65 20 65 78 74 72 61  s a little extra
11300 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 70 61 67   for each.** pag
11310 65 20 68 65 61 64 65 72 2e 20 20 54 68 65 20 70  e header.  The p
11320 61 67 65 20 68 65 61 64 65 72 20 73 69 7a 65 20  age header size 
11330 69 73 20 32 30 20 74 6f 20 34 30 20 62 79 74 65  is 20 to 40 byte
11340 73 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 0a 2a  s depending on.*
11350 2a 20 74 68 65 20 68 6f 73 74 20 61 72 63 68 69  * the host archi
11360 74 65 63 74 75 72 65 2e 20 20 49 74 20 69 73 20  tecture.  It is 
11370 68 61 72 6d 6c 65 73 73 2c 20 61 70 61 72 74 20  harmless, apart 
11380 66 72 6f 6d 20 74 68 65 20 77 61 73 74 65 64 20  from the wasted 
11390 6d 65 6d 6f 72 79 2c 0a 2a 2a 20 74 6f 20 6d 61  memory,.** to ma
113a0 6b 65 20 73 7a 20 61 20 6c 69 74 74 6c 65 20 74  ke sz a little t
113b0 6f 6f 20 6c 61 72 67 65 2e 20 20 54 68 65 20 66  oo large.  The f
113c0 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  irst.** argument
113d0 20 73 68 6f 75 6c 64 20 70 6f 69 6e 74 20 74 6f   should point to
113e0 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f   an allocation o
113f0 66 20 61 74 20 6c 65 61 73 74 20 73 7a 2a 4e 20  f at least sz*N 
11400 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e  bytes of memory.
11410 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  .** SQLite will 
11420 75 73 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 70  use the memory p
11430 72 6f 76 69 64 65 64 20 62 79 20 74 68 65 20 66  rovided by the f
11440 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
11450 20 73 61 74 69 73 66 79 20 69 74 73 0a 2a 2a 20   satisfy its.** 
11460 6d 65 6d 6f 72 79 20 6e 65 65 64 73 20 66 6f 72  memory needs for
11470 20 74 68 65 20 66 69 72 73 74 20 4e 20 70 61 67   the first N pag
11480 65 73 20 74 68 61 74 20 69 74 20 61 64 64 73 20  es that it adds 
11490 74 6f 20 63 61 63 68 65 2e 20 20 49 66 20 61 64  to cache.  If ad
114a0 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 70 61 67 65  ditional.** page
114b0 20 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 69 73   cache memory is
114c0 20 6e 65 65 64 65 64 20 62 65 79 6f 6e 64 20 77   needed beyond w
114d0 68 61 74 20 69 73 20 70 72 6f 76 69 64 65 64 20  hat is provided 
114e0 62 79 20 74 68 69 73 20 6f 70 74 69 6f 6e 2c 20  by this option, 
114f0 74 68 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 67  then.** SQLite g
11500 6f 65 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  oes to [sqlite3_
11510 6d 61 6c 6c 6f 63 28 29 5d 20 66 6f 72 20 74 68  malloc()] for th
11520 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 6f  e additional sto
11530 72 61 67 65 20 73 70 61 63 65 2e 0a 2a 2a 20 54  rage space..** T
11540 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
11550 6e 20 6d 69 67 68 74 20 75 73 65 20 6f 6e 65 20  n might use one 
11560 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 4e  or more of the N
11570 20 62 75 66 66 65 72 73 20 74 6f 20 68 6f 6c 64   buffers to hold
11580 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 63 63 6f   .** memory acco
11590 75 6e 74 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69  unting informati
115a0 6f 6e 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20  on. The pointer 
115b0 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 72 67  in the first arg
115c0 75 6d 65 6e 74 20 6d 75 73 74 0a 2a 2a 20 62 65  ument must.** be
115d0 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 38   aligned to an 8
115e0 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 79 20 6f  -byte boundary o
115f0 72 20 73 75 62 73 65 71 75 65 6e 74 20 62 65 68  r subsequent beh
11600 61 76 69 6f 72 20 6f 66 20 53 51 4c 69 74 65 0a  avior of SQLite.
11610 2a 2a 20 77 69 6c 6c 20 62 65 20 75 6e 64 65 66  ** will be undef
11620 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a  ined.</dd>.**.**
11630 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46   <dt>SQLITE_CONF
11640 49 47 5f 48 45 41 50 3c 2f 64 74 3e 0a 2a 2a 20  IG_HEAP</dt>.** 
11650 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20  <dd>This option 
11660 73 70 65 63 69 66 69 65 73 20 61 20 73 74 61 74  specifies a stat
11670 69 63 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72  ic memory buffer
11680 20 74 68 61 74 20 53 51 4c 69 74 65 20 77 69 6c   that SQLite wil
11690 6c 20 75 73 65 0a 2a 2a 20 66 6f 72 20 61 6c 6c  l use.** for all
116a0 20 6f 66 20 69 74 73 20 64 79 6e 61 6d 69 63 20   of its dynamic 
116b0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
116c0 6e 20 6e 65 65 64 73 20 62 65 79 6f 6e 64 20 74  n needs beyond t
116d0 68 6f 73 65 20 70 72 6f 76 69 64 65 64 0a 2a 2a  hose provided.**
116e0 20 66 6f 72 20 62 79 20 5b 53 51 4c 49 54 45 5f   for by [SQLITE_
116f0 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 20  CONFIG_SCRATCH] 
11700 61 6e 64 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46  and [SQLITE_CONF
11710 49 47 5f 50 41 47 45 43 41 43 48 45 5d 2e 0a 2a  IG_PAGECACHE]..*
11720 2a 20 54 68 65 72 65 20 61 72 65 20 74 68 72 65  * There are thre
11730 65 20 61 72 67 75 6d 65 6e 74 73 3a 20 41 6e 20  e arguments: An 
11740 38 2d 62 79 74 65 20 61 6c 69 67 6e 65 64 20 70  8-byte aligned p
11750 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 65  ointer to the me
11760 6d 6f 72 79 2c 0a 2a 2a 20 74 68 65 20 6e 75 6d  mory,.** the num
11770 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
11780 74 68 65 20 6d 65 6d 6f 72 79 20 62 75 66 66 65  the memory buffe
11790 72 2c 20 61 6e 64 20 74 68 65 20 6d 69 6e 69 6d  r, and the minim
117a0 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69  um allocation si
117b0 7a 65 2e 0a 2a 2a 20 49 66 20 74 68 65 20 66 69  ze..** If the fi
117c0 72 73 74 20 70 6f 69 6e 74 65 72 20 28 74 68 65  rst pointer (the
117d0 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 72 29   memory pointer)
117e0 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 53   is NULL, then S
117f0 51 4c 69 74 65 20 72 65 76 65 72 74 73 0a 2a 2a  QLite reverts.**
11800 20 74 6f 20 75 73 69 6e 67 20 69 74 73 20 64 65   to using its de
11810 66 61 75 6c 74 20 6d 65 6d 6f 72 79 20 61 6c 6c  fault memory all
11820 6f 63 61 74 6f 72 20 28 74 68 65 20 73 79 73 74  ocator (the syst
11830 65 6d 20 6d 61 6c 6c 6f 63 28 29 20 69 6d 70 6c  em malloc() impl
11840 65 6d 65 6e 74 61 74 69 6f 6e 29 2c 0a 2a 2a 20  ementation),.** 
11850 75 6e 64 6f 69 6e 67 20 61 6e 79 20 70 72 69 6f  undoing any prio
11860 72 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20  r invocation of 
11870 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d  [SQLITE_CONFIG_M
11880 41 4c 4c 4f 43 5d 2e 20 20 49 66 20 74 68 65 0a  ALLOC].  If the.
11890 2a 2a 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65  ** memory pointe
118a0 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 61 6e  r is not NULL an
118b0 64 20 65 69 74 68 65 72 20 5b 53 51 4c 49 54 45  d either [SQLITE
118c0 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 5d  _ENABLE_MEMSYS3]
118d0 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45   or.** [SQLITE_E
118e0 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 5d 20 61  NABLE_MEMSYS5] a
118f0 72 65 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e  re defined, then
11900 20 74 68 65 20 61 6c 74 65 72 6e 61 74 69 76 65   the alternative
11910 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63   memory.** alloc
11920 61 74 6f 72 20 69 73 20 65 6e 67 61 67 65 64 20  ator is engaged 
11930 74 6f 20 68 61 6e 64 6c 65 20 61 6c 6c 20 6f 66  to handle all of
11940 20 53 51 4c 69 74 65 73 20 6d 65 6d 6f 72 79 20   SQLites memory 
11950 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73  allocation needs
11960 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70  ..** The first p
11970 6f 69 6e 74 65 72 20 28 74 68 65 20 6d 65 6d 6f  ointer (the memo
11980 72 79 20 70 6f 69 6e 74 65 72 29 20 6d 75 73 74  ry pointer) must
11990 20 62 65 20 61 6c 69 67 6e 65 64 20 74 6f 20 61   be aligned to a
119a0 6e 20 38 2d 62 79 74 65 0a 2a 2a 20 62 6f 75 6e  n 8-byte.** boun
119b0 64 61 72 79 20 6f 72 20 73 75 62 73 65 71 75 65  dary or subseque
119c0 6e 74 20 62 65 68 61 76 69 6f 72 20 6f 66 20 53  nt behavior of S
119d0 51 4c 69 74 65 20 77 69 6c 6c 20 62 65 20 75 6e  QLite will be un
119e0 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a  defined.</dd>.**
119f0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43  .** <dt>SQLITE_C
11a00 4f 4e 46 49 47 5f 4d 55 54 45 58 3c 2f 64 74 3e  ONFIG_MUTEX</dt>
11a10 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74  .** <dd>This opt
11a20 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67  ion takes a sing
11a30 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63  le argument whic
11a40 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  h is a pointer t
11a50 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65  o an.** instance
11a60 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33   of the [sqlite3
11a70 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d 20  _mutex_methods] 
11a80 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20  structure.  The 
11a90 61 72 67 75 6d 65 6e 74 20 73 70 65 63 69 66 69  argument specifi
11aa0 65 73 0a 2a 2a 20 61 6c 74 65 72 6e 61 74 69 76  es.** alternativ
11ab0 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 75 74 65  e low-level mute
11ac0 78 20 72 6f 75 74 69 6e 65 73 20 74 6f 20 62 65  x routines to be
11ad0 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65 0a 2a   used in place.*
11ae0 2a 20 74 68 65 20 6d 75 74 65 78 20 72 6f 75 74  * the mutex rout
11af0 69 6e 65 73 20 62 75 69 6c 74 20 69 6e 74 6f 20  ines built into 
11b00 53 51 4c 69 74 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a  SQLite.</dd>.**.
11b10 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f  ** <dt>SQLITE_CO
11b20 4e 46 49 47 5f 47 45 54 4d 55 54 45 58 3c 2f 64  NFIG_GETMUTEX</d
11b30 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f  t>.** <dd>This o
11b40 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69  ption takes a si
11b50 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68  ngle argument wh
11b60 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ich is a pointer
11b70 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e   to an.** instan
11b80 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74  ce of the [sqlit
11b90 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73  e3_mutex_methods
11ba0 5d 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68  ] structure.  Th
11bb0 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 75  e.** [sqlite3_mu
11bc0 74 65 78 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20  tex_methods].** 
11bd0 73 74 72 75 63 74 75 72 65 20 69 73 20 66 69 6c  structure is fil
11be0 6c 65 64 20 77 69 74 68 20 74 68 65 20 63 75 72  led with the cur
11bf0 72 65 6e 74 6c 79 20 64 65 66 69 6e 65 64 20 6d  rently defined m
11c00 75 74 65 78 20 72 6f 75 74 69 6e 65 73 2e 0a 2a  utex routines..*
11c10 2a 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 63 61  * This option ca
11c20 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6f 76 65  n be used to ove
11c30 72 6c 6f 61 64 20 74 68 65 20 64 65 66 61 75 6c  rload the defaul
11c40 74 20 6d 75 74 65 78 20 61 6c 6c 6f 63 61 74 69  t mutex allocati
11c50 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 77  on.** routines w
11c60 69 74 68 20 61 20 77 72 61 70 70 65 72 20 75 73  ith a wrapper us
11c70 65 64 20 74 6f 20 74 72 61 63 6b 20 6d 75 74 65  ed to track mute
11c80 78 20 75 73 61 67 65 20 66 6f 72 20 70 65 72 66  x usage for perf
11c90 6f 72 6d 61 6e 63 65 0a 2a 2a 20 70 72 6f 66 69  ormance.** profi
11ca0 6c 69 6e 67 20 6f 72 20 74 65 73 74 69 6e 67 2c  ling or testing,
11cb0 20 66 6f 72 20 65 78 61 6d 70 6c 65 2e 3c 2f 64   for example.</d
11cc0 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c  d>.**.** <dt>SQL
11cd0 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41  ITE_CONFIG_LOOKA
11ce0 53 49 44 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  SIDE</dt>.** <dd
11cf0 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b  >This option tak
11d00 65 73 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73  es two arguments
11d10 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 20   that determine 
11d20 74 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 6d  the default.** m
11d30 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
11d40 20 6c 6f 6f 6b 61 73 69 64 65 20 6f 70 74 69 6d   lookaside optim
11d50 69 7a 61 74 69 6f 6e 2e 20 20 54 68 65 20 66 69  ization.  The fi
11d60 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20  rst argument is 
11d70 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 65  the.** size of e
11d80 61 63 68 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75  ach lookaside bu
11d90 66 66 65 72 20 73 6c 6f 74 20 61 6e 64 20 74 68  ffer slot and th
11da0 65 20 73 65 63 6f 6e 64 20 69 73 20 74 68 65 20  e second is the 
11db0 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 73 6c 6f  number of.** slo
11dc0 74 73 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20  ts allocated to 
11dd0 65 61 63 68 20 64 61 74 61 62 61 73 65 20 63 6f  each database co
11de0 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 69 73 20  nnection.  This 
11df0 6f 70 74 69 6f 6e 20 73 65 74 73 20 74 68 65 0a  option sets the.
11e00 2a 2a 20 3c 69 3e 64 65 66 61 75 6c 74 3c 2f 69  ** <i>default</i
11e10 3e 20 6c 6f 6f 6b 61 73 69 64 65 20 73 69 7a 65  > lookaside size
11e20 2e 20 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 44  .  The [SQLITE_D
11e30 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44  BCONFIG_LOOKASID
11e40 45 5d 0a 2a 2a 20 76 65 72 62 20 74 6f 20 5b 73  E].** verb to [s
11e50 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67  qlite3_db_config
11e60 28 29 5d 20 63 61 6e 20 62 65 20 75 73 65 64 20  ()] can be used 
11e70 74 6f 20 63 68 61 6e 67 65 20 74 68 65 20 6c 6f  to change the lo
11e80 6f 6b 61 73 69 64 65 0a 2a 2a 20 63 6f 6e 66 69  okaside.** confi
11e90 67 75 72 61 74 69 6f 6e 20 6f 6e 20 69 6e 64 69  guration on indi
11ea0 76 69 64 75 61 6c 20 63 6f 6e 6e 65 63 74 69 6f  vidual connectio
11eb0 6e 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  ns.</dd>.**.** <
11ec0 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  dt>SQLITE_CONFIG
11ed0 5f 50 43 41 43 48 45 3c 2f 64 74 3e 0a 2a 2a 20  _PCACHE</dt>.** 
11ee0 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20  <dd>This option 
11ef0 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61  takes a single a
11f00 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73  rgument which is
11f10 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a   a pointer to.**
11f20 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 70 63 61   an [sqlite3_pca
11f30 63 68 65 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a  che_methods] obj
11f40 65 63 74 2e 20 20 54 68 69 73 20 6f 62 6a 65 63  ect.  This objec
11f50 74 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20  t specifies the 
11f60 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20  interface.** to 
11f70 61 20 63 75 73 74 6f 6d 20 70 61 67 65 20 63 61  a custom page ca
11f80 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  che implementati
11f90 6f 6e 2e 20 20 53 51 4c 69 74 65 20 6d 61 6b 65  on.  SQLite make
11fa0 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a  s a copy of the.
11fb0 2a 2a 20 6f 62 6a 65 63 74 20 61 6e 64 20 75 73  ** object and us
11fc0 65 73 20 69 74 20 66 6f 72 20 70 61 67 65 20 63  es it for page c
11fd0 61 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  ache memory allo
11fe0 63 61 74 69 6f 6e 73 2e 3c 2f 64 64 3e 0a 2a 2a  cations.</dd>.**
11ff0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43  .** <dt>SQLITE_C
12000 4f 4e 46 49 47 5f 47 45 54 50 43 41 43 48 45 3c  ONFIG_GETPCACHE<
12010 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73  /dt>.** <dd>This
12020 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20   option takes a 
12030 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20  single argument 
12040 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74  which is a point
12050 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 5b 73 71 6c  er to an.** [sql
12060 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68  ite3_pcache_meth
12070 6f 64 73 5d 20 6f 62 6a 65 63 74 2e 20 20 53 51  ods] object.  SQ
12080 4c 69 74 65 20 63 6f 70 69 65 73 20 6f 66 20 74  Lite copies of t
12090 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 70 61  he current.** pa
120a0 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65  ge cache impleme
120b0 6e 74 61 74 69 6f 6e 20 69 6e 74 6f 20 74 68 61  ntation into tha
120c0 74 20 6f 62 6a 65 63 74 2e 3c 2f 64 64 3e 0a 2a  t object.</dd>.*
120d0 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64  *.** </dl>.*/.#d
120e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e  efine SQLITE_CON
120f0 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44  FIG_SINGLETHREAD
12100 20 20 31 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 23    1  /* nil */.#
12110 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f  define SQLITE_CO
12120 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 44  NFIG_MULTITHREAD
12130 20 20 20 32 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a     2  /* nil */.
12140 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
12150 4f 4e 46 49 47 5f 53 45 52 49 41 4c 49 5a 45 44  ONFIG_SERIALIZED
12160 20 20 20 20 33 20 20 2f 2a 20 6e 69 6c 20 2a 2f      3  /* nil */
12170 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
12180 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 20 20 20  CONFIG_MALLOC   
12190 20 20 20 20 20 34 20 20 2f 2a 20 73 71 6c 69 74       4  /* sqlit
121a0 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a 20  e3_mem_methods* 
121b0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
121c0 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c  E_CONFIG_GETMALL
121d0 4f 43 20 20 20 20 20 35 20 20 2f 2a 20 73 71 6c  OC     5  /* sql
121e0 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73  ite3_mem_methods
121f0 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  * */.#define SQL
12200 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54  ITE_CONFIG_SCRAT
12210 43 48 20 20 20 20 20 20 20 36 20 20 2f 2a 20 76  CH       6  /* v
12220 6f 69 64 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e  oid*, int sz, in
12230 74 20 4e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  t N */.#define S
12240 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47  QLITE_CONFIG_PAG
12250 45 43 41 43 48 45 20 20 20 20 20 37 20 20 2f 2a  ECACHE     7  /*
12260 20 76 6f 69 64 2a 2c 20 69 6e 74 20 73 7a 2c 20   void*, int sz, 
12270 69 6e 74 20 4e 20 2a 2f 0a 23 64 65 66 69 6e 65  int N */.#define
12280 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 48   SQLITE_CONFIG_H
12290 45 41 50 20 20 20 20 20 20 20 20 20 20 38 20 20  EAP          8  
122a0 2f 2a 20 76 6f 69 64 2a 2c 20 69 6e 74 20 6e 42  /* void*, int nB
122b0 79 74 65 2c 20 69 6e 74 20 6d 69 6e 20 2a 2f 0a  yte, int min */.
122c0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
122d0 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 20  ONFIG_MEMSTATUS 
122e0 20 20 20 20 39 20 20 2f 2a 20 62 6f 6f 6c 65 61      9  /* boolea
122f0 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  n */.#define SQL
12300 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58  ITE_CONFIG_MUTEX
12310 20 20 20 20 20 20 20 20 31 30 20 20 2f 2a 20 73          10  /* s
12320 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74  qlite3_mutex_met
12330 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65  hods* */.#define
12340 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47   SQLITE_CONFIG_G
12350 45 54 4d 55 54 45 58 20 20 20 20 20 31 31 20 20  ETMUTEX     11  
12360 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  /* sqlite3_mutex
12370 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 2f 2a 20  _methods* */./* 
12380 70 72 65 76 69 6f 75 73 6c 79 20 53 51 4c 49 54  previously SQLIT
12390 45 5f 43 4f 4e 46 49 47 5f 43 48 55 4e 4b 41 4c  E_CONFIG_CHUNKAL
123a0 4c 4f 43 20 31 32 20 77 68 69 63 68 20 69 73 20  LOC 12 which is 
123b0 6e 6f 77 20 75 6e 75 73 65 64 2e 20 2a 2f 20 0a  now unused. */ .
123c0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
123d0 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 20  ONFIG_LOOKASIDE 
123e0 20 20 20 31 33 20 20 2f 2a 20 69 6e 74 20 69 6e     13  /* int in
123f0 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  t */.#define SQL
12400 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48  ITE_CONFIG_PCACH
12410 45 20 20 20 20 20 20 20 31 34 20 20 2f 2a 20 73  E       14  /* s
12420 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65  qlite3_pcache_me
12430 74 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e  thods* */.#defin
12440 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  e SQLITE_CONFIG_
12450 47 45 54 50 43 41 43 48 45 20 20 20 20 31 35 20  GETPCACHE    15 
12460 20 2f 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63   /* sqlite3_pcac
12470 68 65 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 0a  he_methods* */..
12480 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
12490 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 4f 70  Configuration Op
124a0 74 69 6f 6e 73 20 7b 48 31 30 31 37 30 7d 20 3c  tions {H10170} <
124b0 53 32 30 30 30 30 3e 0a 2a 2a 20 45 58 50 45 52  S20000>.** EXPER
124c0 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68  IMENTAL.**.** Th
124d0 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 61 72  ese constants ar
124e0 65 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20  e the available 
124f0 69 6e 74 65 67 65 72 20 63 6f 6e 66 69 67 75 72  integer configur
12500 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 74 68  ation options th
12510 61 74 0a 2a 2a 20 63 61 6e 20 62 65 20 70 61 73  at.** can be pas
12520 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e  sed as the secon
12530 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  d argument to th
12540 65 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f  e [sqlite3_db_co
12550 6e 66 69 67 28 29 5d 20 69 6e 74 65 72 66 61 63  nfig()] interfac
12560 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 63 6f 6e  e..**.** New con
12570 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f  figuration optio
12580 6e 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20  ns may be added 
12590 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73  in future releas
125a0 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a  es of SQLite..**
125b0 20 45 78 69 73 74 69 6e 67 20 63 6f 6e 66 69 67   Existing config
125c0 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20  uration options 
125d0 6d 69 67 68 74 20 62 65 20 64 69 73 63 6f 6e 74  might be discont
125e0 69 6e 75 65 64 2e 20 20 41 70 70 6c 69 63 61 74  inued.  Applicat
125f0 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75 6c 64 20 63  ions.** should c
12600 68 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20  heck the return 
12610 63 6f 64 65 20 66 72 6f 6d 20 5b 73 71 6c 69 74  code from [sqlit
12620 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d 20  e3_db_config()] 
12630 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61  to make sure tha
12640 74 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 20 77 6f  t.** the call wo
12650 72 6b 65 64 2e 20 20 54 68 65 20 5b 73 71 6c 69  rked.  The [sqli
12660 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d  te3_db_config()]
12670 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c 6c 20   interface will 
12680 72 65 74 75 72 6e 20 61 0a 2a 2a 20 6e 6f 6e 2d  return a.** non-
12690 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65  zero [error code
126a0 5d 20 69 66 20 61 20 64 69 73 63 6f 6e 74 69 6e  ] if a discontin
126b0 75 65 64 20 6f 72 20 75 6e 73 75 70 70 6f 72 74  ued or unsupport
126c0 65 64 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e  ed configuration
126d0 20 6f 70 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e   option.** is in
126e0 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c  voked..**.** <dl
126f0 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  >.** <dt>SQLITE_
12700 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49  DBCONFIG_LOOKASI
12710 44 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  DE</dt>.** <dd>T
12720 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73  his option takes
12730 20 74 68 72 65 65 20 61 64 64 69 74 69 6f 6e 61   three additiona
12740 6c 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74  l arguments that
12750 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 0a   determine the .
12760 2a 2a 20 5b 6c 6f 6f 6b 61 73 69 64 65 20 6d 65  ** [lookaside me
12770 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20  mory allocator] 
12780 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 66 6f  configuration fo
12790 72 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20  r the [database 
127a0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 0a 2a 2a 20  connection]..** 
127b0 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  The first argume
127c0 6e 74 20 28 74 68 65 20 74 68 69 72 64 20 70 61  nt (the third pa
127d0 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69  rameter to [sqli
127e0 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d  te3_db_config()]
127f0 20 69 73 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72   is a.** pointer
12800 20 74 6f 20 61 6e 20 6d 65 6d 6f 72 79 20 62 75   to an memory bu
12810 66 66 65 72 20 74 6f 20 75 73 65 20 66 6f 72 20  ffer to use for 
12820 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79  lookaside memory
12830 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61  ..** The first a
12840 72 67 75 6d 65 6e 74 20 6d 61 79 20 62 65 20 4e  rgument may be N
12850 55 4c 4c 20 69 6e 20 77 68 69 63 68 20 63 61 73  ULL in which cas
12860 65 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c  e SQLite will al
12870 6c 6f 63 61 74 65 20 74 68 65 0a 2a 2a 20 6c 6f  locate the.** lo
12880 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 69  okaside buffer i
12890 74 73 65 6c 66 20 75 73 69 6e 67 20 5b 73 71 6c  tself using [sql
128a0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20  ite3_malloc()]. 
128b0 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   The second argu
128c0 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a 2a 20 73  ment is the.** s
128d0 69 7a 65 20 6f 66 20 65 61 63 68 20 6c 6f 6f 6b  ize of each look
128e0 61 73 69 64 65 20 62 75 66 66 65 72 20 73 6c 6f  aside buffer slo
128f0 74 20 61 6e 64 20 74 68 65 20 74 68 69 72 64 20  t and the third 
12900 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20  argument is the 
12910 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 73 6c 6f  number of.** slo
12920 74 73 2e 20 20 54 68 65 20 73 69 7a 65 20 6f 66  ts.  The size of
12930 20 74 68 65 20 62 75 66 66 65 72 20 69 6e 20 74   the buffer in t
12940 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
12950 74 20 6d 75 73 74 20 62 65 20 67 72 65 61 74 65  t must be greate
12960 72 20 74 68 61 6e 0a 2a 2a 20 6f 72 20 65 71 75  r than.** or equ
12970 61 6c 20 74 6f 20 74 68 65 20 70 72 6f 64 75 63  al to the produc
12980 74 20 6f 66 20 74 68 65 20 73 65 63 6f 6e 64 20  t of the second 
12990 61 6e 64 20 74 68 69 72 64 20 61 72 67 75 6d 65  and third argume
129a0 6e 74 73 2e 20 20 54 68 65 20 62 75 66 66 65 72  nts.  The buffer
129b0 0a 2a 2a 20 6d 75 73 74 20 62 65 20 61 6c 69 67  .** must be alig
129c0 6e 65 64 20 74 6f 20 61 6e 20 38 2d 62 79 74 65  ned to an 8-byte
129d0 20 62 6f 75 6e 64 61 72 79 2e 20 20 49 66 20 74   boundary.  If t
129e0 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
129f0 6e 74 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 6d  nt is not.** a m
12a00 75 6c 74 69 70 6c 65 20 6f 66 20 38 2c 20 69 74  ultiple of 8, it
12a10 20 69 73 20 69 6e 74 65 72 6e 61 6c 6c 79 20 72   is internally r
12a20 6f 75 6e 64 65 64 20 64 6f 77 6e 20 74 6f 20 74  ounded down to t
12a30 68 65 20 6e 65 78 74 20 73 6d 61 6c 6c 65 72 0a  he next smaller.
12a40 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 38  ** multiple of 8
12a50 2e 20 20 53 65 65 20 61 6c 73 6f 3a 20 5b 53 51  .  See also: [SQ
12a60 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b  LITE_CONFIG_LOOK
12a70 41 53 49 44 45 5d 3c 2f 64 64 3e 0a 2a 2a 0a 2a  ASIDE]</dd>.**.*
12a80 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69  * </dl>.*/.#defi
12a90 6e 65 20 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46  ne SQLITE_DBCONF
12aa0 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 20 20 20 20  IG_LOOKASIDE    
12ab0 31 30 30 31 20 20 2f 2a 20 76 6f 69 64 2a 20 69  1001  /* void* i
12ac0 6e 74 20 69 6e 74 20 2a 2f 0a 0a 0a 2f 2a 0a 2a  nt int */.../*.*
12ad0 2a 20 43 41 50 49 33 52 45 46 3a 20 45 6e 61 62  * CAPI3REF: Enab
12ae0 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 20 45 78  le Or Disable Ex
12af0 74 65 6e 64 65 64 20 52 65 73 75 6c 74 20 43 6f  tended Result Co
12b00 64 65 73 20 7b 48 31 32 32 30 30 7d 20 3c 53 31  des {H12200} <S1
12b10 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  0700>.**.** The 
12b20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64  sqlite3_extended
12b30 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 29 20  _result_codes() 
12b40 72 6f 75 74 69 6e 65 20 65 6e 61 62 6c 65 73 20  routine enables 
12b50 6f 72 20 64 69 73 61 62 6c 65 73 20 74 68 65 0a  or disables the.
12b60 2a 2a 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73  ** [extended res
12b70 75 6c 74 20 63 6f 64 65 73 5d 20 66 65 61 74 75  ult codes] featu
12b80 72 65 20 6f 66 20 53 51 4c 69 74 65 2e 20 54 68  re of SQLite. Th
12b90 65 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c  e extended resul
12ba0 74 0a 2a 2a 20 63 6f 64 65 73 20 61 72 65 20 64  t.** codes are d
12bb0 69 73 61 62 6c 65 64 20 62 79 20 64 65 66 61 75  isabled by defau
12bc0 6c 74 20 66 6f 72 20 68 69 73 74 6f 72 69 63 61  lt for historica
12bd0 6c 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20  l compatibility 
12be0 63 6f 6e 73 69 64 65 72 61 74 69 6f 6e 73 2e 0a  considerations..
12bf0 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
12c00 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 30 31 5d 20  ts:.** [H12201] 
12c10 5b 48 31 32 32 30 32 5d 0a 2a 2f 0a 53 51 4c 49  [H12202].*/.SQLI
12c20 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
12c30 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75  e3_extended_resu
12c40 6c 74 5f 63 6f 64 65 73 28 73 71 6c 69 74 65 33  lt_codes(sqlite3
12c50 2a 2c 20 69 6e 74 20 6f 6e 6f 66 66 29 3b 0a 0a  *, int onoff);..
12c60 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
12c70 4c 61 73 74 20 49 6e 73 65 72 74 20 52 6f 77 69  Last Insert Rowi
12c80 64 20 7b 48 31 32 32 32 30 7d 20 3c 53 31 30 37  d {H12220} <S107
12c90 30 30 3e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 65  00>.**.** Each e
12ca0 6e 74 72 79 20 69 6e 20 61 6e 20 53 51 4c 69 74  ntry in an SQLit
12cb0 65 20 74 61 62 6c 65 20 68 61 73 20 61 20 75 6e  e table has a un
12cc0 69 71 75 65 20 36 34 2d 62 69 74 20 73 69 67 6e  ique 64-bit sign
12cd0 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6b 65  ed.** integer ke
12ce0 79 20 63 61 6c 6c 65 64 20 74 68 65 20 5b 52 4f  y called the [RO
12cf0 57 49 44 20 7c 20 22 72 6f 77 69 64 22 5d 2e 20  WID | "rowid"]. 
12d00 54 68 65 20 72 6f 77 69 64 20 69 73 20 61 6c 77  The rowid is alw
12d10 61 79 73 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a  ays available.**
12d20 20 61 73 20 61 6e 20 75 6e 64 65 63 6c 61 72 65   as an undeclare
12d30 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 52  d column named R
12d40 4f 57 49 44 2c 20 4f 49 44 2c 20 6f 72 20 5f 52  OWID, OID, or _R
12d50 4f 57 49 44 5f 20 61 73 20 6c 6f 6e 67 20 61 73  OWID_ as long as
12d60 20 74 68 6f 73 65 0a 2a 2a 20 6e 61 6d 65 73 20   those.** names 
12d70 61 72 65 20 6e 6f 74 20 61 6c 73 6f 20 75 73 65  are not also use
12d80 64 20 62 79 20 65 78 70 6c 69 63 69 74 6c 79 20  d by explicitly 
12d90 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d 6e 73  declared columns
12da0 2e 20 49 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c  . If.** the tabl
12db0 65 20 68 61 73 20 61 20 63 6f 6c 75 6d 6e 20 6f  e has a column o
12dc0 66 20 74 79 70 65 20 5b 49 4e 54 45 47 45 52 20  f type [INTEGER 
12dd0 50 52 49 4d 41 52 59 20 4b 45 59 5d 20 74 68 65  PRIMARY KEY] the
12de0 6e 20 74 68 61 74 20 63 6f 6c 75 6d 6e 0a 2a 2a  n that column.**
12df0 20 69 73 20 61 6e 6f 74 68 65 72 20 61 6c 69 61   is another alia
12e00 73 20 66 6f 72 20 74 68 65 20 72 6f 77 69 64 2e  s for the rowid.
12e10 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
12e20 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20  ine returns the 
12e30 5b 72 6f 77 69 64 5d 20 6f 66 20 74 68 65 20 6d  [rowid] of the m
12e40 6f 73 74 20 72 65 63 65 6e 74 0a 2a 2a 20 73 75  ost recent.** su
12e50 63 63 65 73 73 66 75 6c 20 5b 49 4e 53 45 52 54  ccessful [INSERT
12e60 5d 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62  ] into the datab
12e70 61 73 65 20 66 72 6f 6d 20 74 68 65 20 5b 64 61  ase from the [da
12e80 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
12e90 6e 5d 0a 2a 2a 20 69 6e 20 74 68 65 20 66 69 72  n].** in the fir
12ea0 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66  st argument.  If
12eb0 20 6e 6f 20 73 75 63 63 65 73 73 66 75 6c 20 5b   no successful [
12ec0 49 4e 53 45 52 54 5d 73 0a 2a 2a 20 68 61 76 65  INSERT]s.** have
12ed0 20 65 76 65 72 20 6f 63 63 75 72 72 65 64 20 6f   ever occurred o
12ee0 6e 20 74 68 61 74 20 64 61 74 61 62 61 73 65 20  n that database 
12ef0 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 7a 65 72 6f  connection, zero
12f00 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
12f10 0a 2a 2a 20 49 66 20 61 6e 20 5b 49 4e 53 45 52  .** If an [INSER
12f20 54 5d 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e  T] occurs within
12f30 20 61 20 74 72 69 67 67 65 72 2c 20 74 68 65 6e   a trigger, then
12f40 20 74 68 65 20 5b 72 6f 77 69 64 5d 20 6f 66 20   the [rowid] of 
12f50 74 68 65 20 69 6e 73 65 72 74 65 64 0a 2a 2a 20  the inserted.** 
12f60 72 6f 77 20 69 73 20 72 65 74 75 72 6e 65 64 20  row is returned 
12f70 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  by this routine 
12f80 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 74  as long as the t
12f90 72 69 67 67 65 72 20 69 73 20 72 75 6e 6e 69 6e  rigger is runnin
12fa0 67 2e 0a 2a 2a 20 42 75 74 20 6f 6e 63 65 20 74  g..** But once t
12fb0 68 65 20 74 72 69 67 67 65 72 20 74 65 72 6d 69  he trigger termi
12fc0 6e 61 74 65 73 2c 20 74 68 65 20 76 61 6c 75 65  nates, the value
12fd0 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 69   returned by thi
12fe0 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 76  s routine.** rev
12ff0 65 72 74 73 20 74 6f 20 74 68 65 20 6c 61 73 74  erts to the last
13000 20 76 61 6c 75 65 20 69 6e 73 65 72 74 65 64 20   value inserted 
13010 62 65 66 6f 72 65 20 74 68 65 20 74 72 69 67 67  before the trigg
13020 65 72 20 66 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20  er fired..**.** 
13030 41 6e 20 5b 49 4e 53 45 52 54 5d 20 74 68 61 74  An [INSERT] that
13040 20 66 61 69 6c 73 20 64 75 65 20 74 6f 20 61 20   fails due to a 
13050 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61  constraint viola
13060 74 69 6f 6e 20 69 73 20 6e 6f 74 20 61 0a 2a 2a  tion is not a.**
13070 20 73 75 63 63 65 73 73 66 75 6c 20 5b 49 4e 53   successful [INS
13080 45 52 54 5d 20 61 6e 64 20 64 6f 65 73 20 6e 6f  ERT] and does no
13090 74 20 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c  t change the val
130a0 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74  ue returned by t
130b0 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e 20  his.** routine. 
130c0 20 54 68 75 73 20 49 4e 53 45 52 54 20 4f 52 20   Thus INSERT OR 
130d0 46 41 49 4c 2c 20 49 4e 53 45 52 54 20 4f 52 20  FAIL, INSERT OR 
130e0 49 47 4e 4f 52 45 2c 20 49 4e 53 45 52 54 20 4f  IGNORE, INSERT O
130f0 52 20 52 4f 4c 4c 42 41 43 4b 2c 0a 2a 2a 20 61  R ROLLBACK,.** a
13100 6e 64 20 49 4e 53 45 52 54 20 4f 52 20 41 42 4f  nd INSERT OR ABO
13110 52 54 20 6d 61 6b 65 20 6e 6f 20 63 68 61 6e 67  RT make no chang
13120 65 73 20 74 6f 20 74 68 65 20 72 65 74 75 72 6e  es to the return
13130 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 0a 2a   value of this.*
13140 2a 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 74  * routine when t
13150 68 65 69 72 20 69 6e 73 65 72 74 69 6f 6e 20 66  heir insertion f
13160 61 69 6c 73 2e 20 20 57 68 65 6e 20 49 4e 53 45  ails.  When INSE
13170 52 54 20 4f 52 20 52 45 50 4c 41 43 45 0a 2a 2a  RT OR REPLACE.**
13180 20 65 6e 63 6f 75 6e 74 65 72 73 20 61 20 63 6f   encounters a co
13190 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69  nstraint violati
131a0 6f 6e 2c 20 69 74 20 64 6f 65 73 20 6e 6f 74 20  on, it does not 
131b0 66 61 69 6c 2e 20 20 54 68 65 0a 2a 2a 20 49 4e  fail.  The.** IN
131c0 53 45 52 54 20 63 6f 6e 74 69 6e 75 65 73 20 74  SERT continues t
131d0 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 61 66 74  o completion aft
131e0 65 72 20 64 65 6c 65 74 69 6e 67 20 72 6f 77 73  er deleting rows
131f0 20 74 68 61 74 20 63 61 75 73 65 64 0a 2a 2a 20   that caused.** 
13200 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 70  the constraint p
13210 72 6f 62 6c 65 6d 20 73 6f 20 49 4e 53 45 52 54  roblem so INSERT
13220 20 4f 52 20 52 45 50 4c 41 43 45 20 77 69 6c 6c   OR REPLACE will
13230 20 61 6c 77 61 79 73 20 63 68 61 6e 67 65 0a 2a   always change.*
13240 2a 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  * the return val
13250 75 65 20 6f 66 20 74 68 69 73 20 69 6e 74 65 72  ue of this inter
13260 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  face..**.** For 
13270 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20  the purposes of 
13280 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 61 6e  this routine, an
13290 20 5b 49 4e 53 45 52 54 5d 20 69 73 20 63 6f 6e   [INSERT] is con
132a0 73 69 64 65 72 65 64 20 74 6f 0a 2a 2a 20 62 65  sidered to.** be
132b0 20 73 75 63 63 65 73 73 66 75 6c 20 65 76 65 6e   successful even
132c0 20 69 66 20 69 74 20 69 73 20 73 75 62 73 65 71   if it is subseq
132d0 75 65 6e 74 6c 79 20 72 6f 6c 6c 65 64 20 62 61  uently rolled ba
132e0 63 6b 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  ck..**.** Requir
132f0 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32  ements:.** [H122
13300 32 31 5d 20 5b 48 31 32 32 32 33 5d 0a 2a 2a 0a  21] [H12223].**.
13310 2a 2a 20 49 66 20 61 20 73 65 70 61 72 61 74 65  ** If a separate
13320 20 74 68 72 65 61 64 20 70 65 72 66 6f 72 6d 73   thread performs
13330 20 61 20 6e 65 77 20 5b 49 4e 53 45 52 54 5d 20   a new [INSERT] 
13340 6f 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 64  on the same.** d
13350 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
13360 6f 6e 20 77 68 69 6c 65 20 74 68 65 20 5b 73 71  on while the [sq
13370 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72  lite3_last_inser
13380 74 5f 72 6f 77 69 64 28 29 5d 0a 2a 2a 20 66 75  t_rowid()].** fu
13390 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e  nction is runnin
133a0 67 20 61 6e 64 20 74 68 75 73 20 63 68 61 6e 67  g and thus chang
133b0 65 73 20 74 68 65 20 6c 61 73 74 20 69 6e 73 65  es the last inse
133c0 72 74 20 5b 72 6f 77 69 64 5d 2c 0a 2a 2a 20 74  rt [rowid],.** t
133d0 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65  hen the value re
133e0 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74  turned by [sqlit
133f0 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72  e3_last_insert_r
13400 6f 77 69 64 28 29 5d 20 69 73 0a 2a 2a 20 75 6e  owid()] is.** un
13410 70 72 65 64 69 63 74 61 62 6c 65 20 61 6e 64 20  predictable and 
13420 6d 69 67 68 74 20 6e 6f 74 20 65 71 75 61 6c 20  might not equal 
13430 65 69 74 68 65 72 20 74 68 65 20 6f 6c 64 20 6f  either the old o
13440 72 20 74 68 65 20 6e 65 77 0a 2a 2a 20 6c 61 73  r the new.** las
13450 74 20 69 6e 73 65 72 74 20 5b 72 6f 77 69 64 5d  t insert [rowid]
13460 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
13470 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71  sqlite3_int64 sq
13480 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72  lite3_last_inser
13490 74 5f 72 6f 77 69 64 28 73 71 6c 69 74 65 33 2a  t_rowid(sqlite3*
134a0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
134b0 45 46 3a 20 43 6f 75 6e 74 20 54 68 65 20 4e 75  EF: Count The Nu
134c0 6d 62 65 72 20 4f 66 20 52 6f 77 73 20 4d 6f 64  mber Of Rows Mod
134d0 69 66 69 65 64 20 7b 48 31 32 32 34 30 7d 20 3c  ified {H12240} <
134e0 53 31 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S10600>.**.** Th
134f0 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  is function retu
13500 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  rns the number o
13510 66 20 64 61 74 61 62 61 73 65 20 72 6f 77 73 20  f database rows 
13520 74 68 61 74 20 77 65 72 65 20 63 68 61 6e 67 65  that were change
13530 64 0a 2a 2a 20 6f 72 20 69 6e 73 65 72 74 65 64  d.** or inserted
13540 20 6f 72 20 64 65 6c 65 74 65 64 20 62 79 20 74   or deleted by t
13550 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79  he most recently
13560 20 63 6f 6d 70 6c 65 74 65 64 20 53 51 4c 20 73   completed SQL s
13570 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 6f 6e 20 74  tatement.** on t
13580 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  he [database con
13590 6e 65 63 74 69 6f 6e 5d 20 73 70 65 63 69 66 69  nection] specifi
135a0 65 64 20 62 79 20 74 68 65 20 66 69 72 73 74 20  ed by the first 
135b0 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 4f 6e  parameter..** On
135c0 6c 79 20 63 68 61 6e 67 65 73 20 74 68 61 74 20  ly changes that 
135d0 61 72 65 20 64 69 72 65 63 74 6c 79 20 73 70 65  are directly spe
135e0 63 69 66 69 65 64 20 62 79 20 74 68 65 20 5b 49  cified by the [I
135f0 4e 53 45 52 54 5d 2c 20 5b 55 50 44 41 54 45 5d  NSERT], [UPDATE]
13600 2c 0a 2a 2a 20 6f 72 20 5b 44 45 4c 45 54 45 5d  ,.** or [DELETE]
13610 20 73 74 61 74 65 6d 65 6e 74 20 61 72 65 20 63   statement are c
13620 6f 75 6e 74 65 64 2e 20 20 41 75 78 69 6c 69 61  ounted.  Auxilia
13630 72 79 20 63 68 61 6e 67 65 73 20 63 61 75 73 65  ry changes cause
13640 64 20 62 79 0a 2a 2a 20 74 72 69 67 67 65 72 73  d by.** triggers
13650 20 61 72 65 20 6e 6f 74 20 63 6f 75 6e 74 65 64   are not counted
13660 2e 20 55 73 65 20 74 68 65 20 5b 73 71 6c 69 74  . Use the [sqlit
13670 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  e3_total_changes
13680 28 29 5d 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  ()] function.** 
13690 74 6f 20 66 69 6e 64 20 74 68 65 20 74 6f 74 61  to find the tota
136a0 6c 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 6e  l number of chan
136b0 67 65 73 20 69 6e 63 6c 75 64 69 6e 67 20 63 68  ges including ch
136c0 61 6e 67 65 73 20 63 61 75 73 65 64 20 62 79 20  anges caused by 
136d0 74 72 69 67 67 65 72 73 2e 0a 2a 2a 0a 2a 2a 20  triggers..**.** 
136e0 43 68 61 6e 67 65 73 20 74 6f 20 61 20 76 69 65  Changes to a vie
136f0 77 20 74 68 61 74 20 61 72 65 20 73 69 6d 75 6c  w that are simul
13700 61 74 65 64 20 62 79 20 61 6e 20 5b 49 4e 53 54  ated by an [INST
13710 45 41 44 20 4f 46 20 74 72 69 67 67 65 72 5d 0a  EAD OF trigger].
13720 2a 2a 20 61 72 65 20 6e 6f 74 20 63 6f 75 6e 74  ** are not count
13730 65 64 2e 20 20 4f 6e 6c 79 20 72 65 61 6c 20 74  ed.  Only real t
13740 61 62 6c 65 20 63 68 61 6e 67 65 73 20 61 72 65  able changes are
13750 20 63 6f 75 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a 20   counted..**.** 
13760 41 20 22 72 6f 77 20 63 68 61 6e 67 65 22 20 69  A "row change" i
13770 73 20 61 20 63 68 61 6e 67 65 20 74 6f 20 61 20  s a change to a 
13780 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 61 20  single row of a 
13790 73 69 6e 67 6c 65 20 74 61 62 6c 65 0a 2a 2a 20  single table.** 
137a0 63 61 75 73 65 64 20 62 79 20 61 6e 20 49 4e 53  caused by an INS
137b0 45 52 54 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20  ERT, DELETE, or 
137c0 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74  UPDATE statement
137d0 2e 20 20 52 6f 77 73 20 74 68 61 74 0a 2a 2a 20  .  Rows that.** 
137e0 61 72 65 20 63 68 61 6e 67 65 64 20 61 73 20 73  are changed as s
137f0 69 64 65 20 65 66 66 65 63 74 73 20 6f 66 20 5b  ide effects of [
13800 52 45 50 4c 41 43 45 5d 20 63 6f 6e 73 74 72 61  REPLACE] constra
13810 69 6e 74 20 72 65 73 6f 6c 75 74 69 6f 6e 2c 0a  int resolution,.
13820 2a 2a 20 72 6f 6c 6c 62 61 63 6b 2c 20 41 42 4f  ** rollback, ABO
13830 52 54 20 70 72 6f 63 65 73 73 69 6e 67 2c 20 5b  RT processing, [
13840 44 52 4f 50 20 54 41 42 4c 45 5d 2c 20 6f 72 20  DROP TABLE], or 
13850 62 79 20 61 6e 79 20 6f 74 68 65 72 0a 2a 2a 20  by any other.** 
13860 6d 65 63 68 61 6e 69 73 6d 73 20 64 6f 20 6e 6f  mechanisms do no
13870 74 20 63 6f 75 6e 74 20 61 73 20 64 69 72 65 63  t count as direc
13880 74 20 72 6f 77 20 63 68 61 6e 67 65 73 2e 0a 2a  t row changes..*
13890 2a 0a 2a 2a 20 41 20 22 74 72 69 67 67 65 72 20  *.** A "trigger 
138a0 63 6f 6e 74 65 78 74 22 20 69 73 20 61 20 73 63  context" is a sc
138b0 6f 70 65 20 6f 66 20 65 78 65 63 75 74 69 6f 6e  ope of execution
138c0 20 74 68 61 74 20 62 65 67 69 6e 73 20 61 6e 64   that begins and
138d0 0a 2a 2a 20 65 6e 64 73 20 77 69 74 68 20 74 68  .** ends with th
138e0 65 20 73 63 72 69 70 74 20 6f 66 20 61 20 5b 43  e script of a [C
138f0 52 45 41 54 45 20 54 52 49 47 47 45 52 20 7c 20  REATE TRIGGER | 
13900 74 72 69 67 67 65 72 5d 2e 20 0a 2a 2a 20 4d 6f  trigger]. .** Mo
13910 73 74 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  st SQL statement
13920 73 20 61 72 65 0a 2a 2a 20 65 76 61 6c 75 61 74  s are.** evaluat
13930 65 64 20 6f 75 74 73 69 64 65 20 6f 66 20 61 6e  ed outside of an
13940 79 20 74 72 69 67 67 65 72 2e 20 20 54 68 69 73  y trigger.  This
13950 20 69 73 20 74 68 65 20 22 74 6f 70 20 6c 65 76   is the "top lev
13960 65 6c 22 0a 2a 2a 20 74 72 69 67 67 65 72 20 63  el".** trigger c
13970 6f 6e 74 65 78 74 2e 20 20 49 66 20 61 20 74 72  ontext.  If a tr
13980 69 67 67 65 72 20 66 69 72 65 73 20 66 72 6f 6d  igger fires from
13990 20 74 68 65 20 74 6f 70 20 6c 65 76 65 6c 2c 20   the top level, 
139a0 61 0a 2a 2a 20 6e 65 77 20 74 72 69 67 67 65 72  a.** new trigger
139b0 20 63 6f 6e 74 65 78 74 20 69 73 20 65 6e 74 65   context is ente
139c0 72 65 64 20 66 6f 72 20 74 68 65 20 64 75 72 61  red for the dura
139d0 74 69 6f 6e 20 6f 66 20 74 68 61 74 20 6f 6e 65  tion of that one
139e0 0a 2a 2a 20 74 72 69 67 67 65 72 2e 20 20 53 75  .** trigger.  Su
139f0 62 74 72 69 67 67 65 72 73 20 63 72 65 61 74 65  btriggers create
13a00 20 73 75 62 63 6f 6e 74 65 78 74 73 20 66 6f 72   subcontexts for
13a10 20 74 68 65 69 72 20 64 75 72 61 74 69 6f 6e 2e   their duration.
13a20 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 5b  .**.** Calling [
13a30 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20  sqlite3_exec()] 
13a40 6f 72 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  or [sqlite3_step
13a50 28 29 5d 20 72 65 63 75 72 73 69 76 65 6c 79 20  ()] recursively 
13a60 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 72 65 61  does.** not crea
13a70 74 65 20 61 20 6e 65 77 20 74 72 69 67 67 65 72  te a new trigger
13a80 20 63 6f 6e 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20   context..**.** 
13a90 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  This function re
13aa0 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72  turns the number
13ab0 20 6f 66 20 64 69 72 65 63 74 20 72 6f 77 20 63   of direct row c
13ac0 68 61 6e 67 65 73 20 69 6e 20 74 68 65 0a 2a 2a  hanges in the.**
13ad0 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 49 4e 53   most recent INS
13ae0 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20  ERT, UPDATE, or 
13af0 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74  DELETE statement
13b00 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65   within the same
13b10 0a 2a 2a 20 74 72 69 67 67 65 72 20 63 6f 6e 74  .** trigger cont
13b20 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 75 73 2c  ext..**.** Thus,
13b30 20 77 68 65 6e 20 63 61 6c 6c 65 64 20 66 72 6f   when called fro
13b40 6d 20 74 68 65 20 74 6f 70 20 6c 65 76 65 6c 2c  m the top level,
13b50 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   this function r
13b60 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 6e 75  eturns the.** nu
13b70 6d 62 65 72 20 6f 66 20 63 68 61 6e 67 65 73 20  mber of changes 
13b80 69 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  in the most rece
13b90 6e 74 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54  nt INSERT, UPDAT
13ba0 45 2c 20 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20  E, or DELETE.** 
13bb0 74 68 61 74 20 61 6c 73 6f 20 6f 63 63 75 72 72  that also occurr
13bc0 65 64 20 61 74 20 74 68 65 20 74 6f 70 20 6c 65  ed at the top le
13bd0 76 65 6c 2e 20 20 57 69 74 68 69 6e 20 74 68 65  vel.  Within the
13be0 20 62 6f 64 79 20 6f 66 20 61 20 74 72 69 67 67   body of a trigg
13bf0 65 72 2c 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74  er,.** the sqlit
13c00 65 33 5f 63 68 61 6e 67 65 73 28 29 20 69 6e 74  e3_changes() int
13c10 65 72 66 61 63 65 20 63 61 6e 20 62 65 20 63 61  erface can be ca
13c20 6c 6c 65 64 20 74 6f 20 66 69 6e 64 20 74 68 65  lled to find the
13c30 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 68   number of.** ch
13c40 61 6e 67 65 73 20 69 6e 20 74 68 65 20 6d 6f 73  anges in the mos
13c50 74 20 72 65 63 65 6e 74 6c 79 20 63 6f 6d 70 6c  t recently compl
13c60 65 74 65 64 20 49 4e 53 45 52 54 2c 20 55 50 44  eted INSERT, UPD
13c70 41 54 45 2c 20 6f 72 20 44 45 4c 45 54 45 0a 2a  ATE, or DELETE.*
13c80 2a 20 73 74 61 74 65 6d 65 6e 74 20 77 69 74 68  * statement with
13c90 69 6e 20 74 68 65 20 62 6f 64 79 20 6f 66 20 74  in the body of t
13ca0 68 65 20 73 61 6d 65 20 74 72 69 67 67 65 72 2e  he same trigger.
13cb0 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65  .** However, the
13cc0 20 6e 75 6d 62 65 72 20 72 65 74 75 72 6e 65 64   number returned
13cd0 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64   does not includ
13ce0 65 20 63 68 61 6e 67 65 73 0a 2a 2a 20 63 61 75  e changes.** cau
13cf0 73 65 64 20 62 79 20 73 75 62 74 72 69 67 67 65  sed by subtrigge
13d00 72 73 20 73 69 6e 63 65 20 74 68 6f 73 65 20 68  rs since those h
13d10 61 76 65 20 74 68 65 69 72 20 6f 77 6e 20 63 6f  ave their own co
13d20 6e 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  ntext..**.** See
13d30 20 61 6c 73 6f 20 74 68 65 20 5b 73 71 6c 69 74   also the [sqlit
13d40 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  e3_total_changes
13d50 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 61 6e  ()] interface an
13d60 64 20 74 68 65 0a 2a 2a 20 5b 63 6f 75 6e 74 5f  d the.** [count_
13d70 63 68 61 6e 67 65 73 20 70 72 61 67 6d 61 5d 2e  changes pragma].
13d80 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
13d90 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 34 31 5d  nts:.** [H12241]
13da0 20 5b 48 31 32 32 34 33 5d 0a 2a 2a 0a 2a 2a 20   [H12243].**.** 
13db0 49 66 20 61 20 73 65 70 61 72 61 74 65 20 74 68  If a separate th
13dc0 72 65 61 64 20 6d 61 6b 65 73 20 63 68 61 6e 67  read makes chang
13dd0 65 73 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 64  es on the same d
13de0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
13df0 6f 6e 0a 2a 2a 20 77 68 69 6c 65 20 5b 73 71 6c  on.** while [sql
13e00 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 5d 20  ite3_changes()] 
13e10 69 73 20 72 75 6e 6e 69 6e 67 20 74 68 65 6e 20  is running then 
13e20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  the value return
13e30 65 64 0a 2a 2a 20 69 73 20 75 6e 70 72 65 64 69  ed.** is unpredi
13e40 63 74 61 62 6c 65 20 61 6e 64 20 6e 6f 74 20 6d  ctable and not m
13e50 65 61 6e 69 6e 67 66 75 6c 2e 0a 2a 2f 0a 53 51  eaningful..*/.SQ
13e60 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
13e70 69 74 65 33 5f 63 68 61 6e 67 65 73 28 73 71 6c  ite3_changes(sql
13e80 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  ite3*);../*.** C
13e90 41 50 49 33 52 45 46 3a 20 54 6f 74 61 6c 20 4e  API3REF: Total N
13ea0 75 6d 62 65 72 20 4f 66 20 52 6f 77 73 20 4d 6f  umber Of Rows Mo
13eb0 64 69 66 69 65 64 20 7b 48 31 32 32 36 30 7d 20  dified {H12260} 
13ec0 3c 53 31 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S10600>.**.** T
13ed0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  his function ret
13ee0 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20  urns the number 
13ef0 6f 66 20 72 6f 77 20 63 68 61 6e 67 65 73 20 63  of row changes c
13f00 61 75 73 65 64 20 62 79 20 5b 49 4e 53 45 52 54  aused by [INSERT
13f10 5d 2c 0a 2a 2a 20 5b 55 50 44 41 54 45 5d 20 6f  ],.** [UPDATE] o
13f20 72 20 5b 44 45 4c 45 54 45 5d 20 73 74 61 74 65  r [DELETE] state
13f30 6d 65 6e 74 73 20 73 69 6e 63 65 20 74 68 65 20  ments since the 
13f40 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
13f50 74 69 6f 6e 5d 20 77 61 73 20 6f 70 65 6e 65 64  tion] was opened
13f60 2e 0a 2a 2a 20 54 68 65 20 63 6f 75 6e 74 20 69  ..** The count i
13f70 6e 63 6c 75 64 65 73 20 61 6c 6c 20 63 68 61 6e  ncludes all chan
13f80 67 65 73 20 66 72 6f 6d 20 61 6c 6c 20 0a 2a 2a  ges from all .**
13f90 20 5b 43 52 45 41 54 45 20 54 52 49 47 47 45 52   [CREATE TRIGGER
13fa0 20 7c 20 74 72 69 67 67 65 72 5d 20 63 6f 6e 74   | trigger] cont
13fb0 65 78 74 73 2e 20 20 48 6f 77 65 76 65 72 2c 0a  exts.  However,.
13fc0 2a 2a 20 74 68 65 20 63 6f 75 6e 74 20 64 6f 65  ** the count doe
13fd0 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 63 68  s not include ch
13fe0 61 6e 67 65 73 20 75 73 65 64 20 74 6f 20 69 6d  anges used to im
13ff0 70 6c 65 6d 65 6e 74 20 5b 52 45 50 4c 41 43 45  plement [REPLACE
14000 5d 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 0a 2a  ] constraints,.*
14010 2a 20 64 6f 20 72 6f 6c 6c 62 61 63 6b 73 20 6f  * do rollbacks o
14020 72 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69  r ABORT processi
14030 6e 67 2c 20 6f 72 20 5b 44 52 4f 50 20 54 41 42  ng, or [DROP TAB
14040 4c 45 5d 20 70 72 6f 63 65 73 73 69 6e 67 2e 20  LE] processing. 
14050 20 54 68 65 0a 2a 2a 20 63 6f 75 6e 74 20 64 6f   The.** count do
14060 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 72  es not include r
14070 6f 77 73 20 6f 66 20 76 69 65 77 73 20 74 68 61  ows of views tha
14080 74 20 66 69 72 65 20 61 6e 20 5b 49 4e 53 54 45  t fire an [INSTE
14090 41 44 20 4f 46 20 74 72 69 67 67 65 72 5d 2c 0a  AD OF trigger],.
140a0 2a 2a 20 74 68 6f 75 67 68 20 69 66 20 74 68 65  ** though if the
140b0 20 49 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67   INSTEAD OF trig
140c0 67 65 72 20 6d 61 6b 65 73 20 63 68 61 6e 67 65  ger makes change
140d0 73 20 6f 66 20 69 74 73 20 6f 77 6e 2c 20 74 68  s of its own, th
140e0 6f 73 65 20 63 68 61 6e 67 65 73 20 0a 2a 2a 20  ose changes .** 
140f0 61 72 65 20 63 6f 75 6e 74 65 64 2e 0a 2a 2a 20  are counted..** 
14100 54 68 65 20 63 68 61 6e 67 65 73 20 61 72 65 20  The changes are 
14110 63 6f 75 6e 74 65 64 20 61 73 20 73 6f 6f 6e 20  counted as soon 
14120 61 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  as the statement
14130 20 74 68 61 74 20 6d 61 6b 65 73 20 74 68 65 6d   that makes them
14140 20 69 73 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 64   is.** completed
14150 20 28 77 68 65 6e 20 74 68 65 20 73 74 61 74 65   (when the state
14160 6d 65 6e 74 20 68 61 6e 64 6c 65 20 69 73 20 70  ment handle is p
14170 61 73 73 65 64 20 74 6f 20 5b 73 71 6c 69 74 65  assed to [sqlite
14180 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 0a 2a 2a  3_reset()] or.**
14190 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69   [sqlite3_finali
141a0 7a 65 28 29 5d 29 2e 0a 2a 2a 0a 2a 2a 20 53 65  ze()])..**.** Se
141b0 65 20 61 6c 73 6f 20 74 68 65 20 5b 73 71 6c 69  e also the [sqli
141c0 74 65 33 5f 63 68 61 6e 67 65 73 28 29 5d 20 69  te3_changes()] i
141d0 6e 74 65 72 66 61 63 65 20 61 6e 64 20 74 68 65  nterface and the
141e0 0a 2a 2a 20 5b 63 6f 75 6e 74 5f 63 68 61 6e 67  .** [count_chang
141f0 65 73 20 70 72 61 67 6d 61 5d 2e 0a 2a 2a 0a 2a  es pragma]..**.*
14200 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
14210 2a 2a 20 5b 48 31 32 32 36 31 5d 20 5b 48 31 32  ** [H12261] [H12
14220 32 36 33 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20  263].**.** If a 
14230 73 65 70 61 72 61 74 65 20 74 68 72 65 61 64 20  separate thread 
14240 6d 61 6b 65 73 20 63 68 61 6e 67 65 73 20 6f 6e  makes changes on
14250 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61   the same databa
14260 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a  se connection.**
14270 20 77 68 69 6c 65 20 5b 73 71 6c 69 74 65 33 5f   while [sqlite3_
14280 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d  total_changes()]
14290 20 69 73 20 72 75 6e 6e 69 6e 67 20 74 68 65 6e   is running then
142a0 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 72 65   the value.** re
142b0 74 75 72 6e 65 64 20 69 73 20 75 6e 70 72 65 64  turned is unpred
142c0 69 63 74 61 62 6c 65 20 61 6e 64 20 6e 6f 74 20  ictable and not 
142d0 6d 65 61 6e 69 6e 67 66 75 6c 2e 0a 2a 2f 0a 53  meaningful..*/.S
142e0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
142f0 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e  lite3_total_chan
14300 67 65 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a  ges(sqlite3*);..
14310 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
14320 49 6e 74 65 72 72 75 70 74 20 41 20 4c 6f 6e 67  Interrupt A Long
14330 2d 52 75 6e 6e 69 6e 67 20 51 75 65 72 79 20 7b  -Running Query {
14340 48 31 32 32 37 30 7d 20 3c 53 33 30 35 30 30 3e  H12270} <S30500>
14350 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
14360 74 69 6f 6e 20 63 61 75 73 65 73 20 61 6e 79 20  tion causes any 
14370 70 65 6e 64 69 6e 67 20 64 61 74 61 62 61 73 65  pending database
14380 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 61 62   operation to ab
14390 6f 72 74 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72  ort and.** retur
143a0 6e 20 61 74 20 69 74 73 20 65 61 72 6c 69 65 73  n at its earlies
143b0 74 20 6f 70 70 6f 72 74 75 6e 69 74 79 2e 20 54  t opportunity. T
143c0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74  his routine is t
143d0 79 70 69 63 61 6c 6c 79 0a 2a 2a 20 63 61 6c 6c  ypically.** call
143e0 65 64 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74  ed in response t
143f0 6f 20 61 20 75 73 65 72 20 61 63 74 69 6f 6e 20  o a user action 
14400 73 75 63 68 20 61 73 20 70 72 65 73 73 69 6e 67  such as pressing
14410 20 22 43 61 6e 63 65 6c 22 0a 2a 2a 20 6f 72 20   "Cancel".** or 
14420 43 74 72 6c 2d 43 20 77 68 65 72 65 20 74 68 65  Ctrl-C where the
14430 20 75 73 65 72 20 77 61 6e 74 73 20 61 20 6c 6f   user wants a lo
14440 6e 67 20 71 75 65 72 79 20 6f 70 65 72 61 74 69  ng query operati
14450 6f 6e 20 74 6f 20 68 61 6c 74 0a 2a 2a 20 69 6d  on to halt.** im
14460 6d 65 64 69 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a  mediately..**.**
14470 20 49 74 20 69 73 20 73 61 66 65 20 74 6f 20 63   It is safe to c
14480 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  all this routine
14490 20 66 72 6f 6d 20 61 20 74 68 72 65 61 64 20 64   from a thread d
144a0 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 74 68  ifferent from th
144b0 65 0a 2a 2a 20 74 68 72 65 61 64 20 74 68 61 74  e.** thread that
144c0 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 72 75   is currently ru
144d0 6e 6e 69 6e 67 20 74 68 65 20 64 61 74 61 62 61  nning the databa
144e0 73 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42  se operation.  B
144f0 75 74 20 69 74 0a 2a 2a 20 69 73 20 6e 6f 74 20  ut it.** is not 
14500 73 61 66 65 20 74 6f 20 63 61 6c 6c 20 74 68 69  safe to call thi
14510 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20 61  s routine with a
14520 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
14530 63 74 69 6f 6e 5d 20 74 68 61 74 0a 2a 2a 20 69  ction] that.** i
14540 73 20 63 6c 6f 73 65 64 20 6f 72 20 6d 69 67 68  s closed or migh
14550 74 20 63 6c 6f 73 65 20 62 65 66 6f 72 65 20 73  t close before s
14560 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
14570 28 29 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a  () returns..**.*
14580 2a 20 49 66 20 61 6e 20 53 51 4c 20 6f 70 65 72  * If an SQL oper
14590 61 74 69 6f 6e 20 69 73 20 76 65 72 79 20 6e 65  ation is very ne
145a0 61 72 6c 79 20 66 69 6e 69 73 68 65 64 20 61 74  arly finished at
145b0 20 74 68 65 20 74 69 6d 65 20 77 68 65 6e 0a 2a   the time when.*
145c0 2a 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72  * sqlite3_interr
145d0 75 70 74 28 29 20 69 73 20 63 61 6c 6c 65 64 2c  upt() is called,
145e0 20 74 68 65 6e 20 69 74 20 6d 69 67 68 74 20 6e   then it might n
145f0 6f 74 20 68 61 76 65 20 61 6e 20 6f 70 70 6f 72  ot have an oppor
14600 74 75 6e 69 74 79 0a 2a 2a 20 74 6f 20 62 65 20  tunity.** to be 
14610 69 6e 74 65 72 72 75 70 74 65 64 20 61 6e 64 20  interrupted and 
14620 6d 69 67 68 74 20 63 6f 6e 74 69 6e 75 65 20 74  might continue t
14630 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 2e 0a 2a 2a  o completion..**
14640 0a 2a 2a 20 41 6e 20 53 51 4c 20 6f 70 65 72 61  .** An SQL opera
14650 74 69 6f 6e 20 74 68 61 74 20 69 73 20 69 6e 74  tion that is int
14660 65 72 72 75 70 74 65 64 20 77 69 6c 6c 20 72 65  errupted will re
14670 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 49 4e 54  turn [SQLITE_INT
14680 45 52 52 55 50 54 5d 2e 0a 2a 2a 20 49 66 20 74  ERRUPT]..** If t
14690 68 65 20 69 6e 74 65 72 72 75 70 74 65 64 20 53  he interrupted S
146a0 51 4c 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20  QL operation is 
146b0 61 6e 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54  an INSERT, UPDAT
146c0 45 2c 20 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20  E, or DELETE.** 
146d0 74 68 61 74 20 69 73 20 69 6e 73 69 64 65 20 61  that is inside a
146e0 6e 20 65 78 70 6c 69 63 69 74 20 74 72 61 6e 73  n explicit trans
146f0 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65  action, then the
14700 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74   entire transact
14710 69 6f 6e 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 72  ion.** will be r
14720 6f 6c 6c 65 64 20 62 61 63 6b 20 61 75 74 6f 6d  olled back autom
14730 61 74 69 63 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20  atically..**.** 
14740 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65  The sqlite3_inte
14750 72 72 75 70 74 28 44 29 20 63 61 6c 6c 20 69 73  rrupt(D) call is
14760 20 69 6e 20 65 66 66 65 63 74 20 75 6e 74 69 6c   in effect until
14770 20 61 6c 6c 20 63 75 72 72 65 6e 74 6c 79 20 72   all currently r
14780 75 6e 6e 69 6e 67 0a 2a 2a 20 53 51 4c 20 73 74  unning.** SQL st
14790 61 74 65 6d 65 6e 74 73 20 6f 6e 20 5b 64 61 74  atements on [dat
147a0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
147b0 5d 20 44 20 63 6f 6d 70 6c 65 74 65 2e 20 20 41  ] D complete.  A
147c0 6e 79 20 6e 65 77 20 53 51 4c 20 73 74 61 74 65  ny new SQL state
147d0 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 72  ments.** that ar
147e0 65 20 73 74 61 72 74 65 64 20 61 66 74 65 72 20  e started after 
147f0 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65  the sqlite3_inte
14800 72 72 75 70 74 28 29 20 63 61 6c 6c 20 61 6e 64  rrupt() call and
14810 20 62 65 66 6f 72 65 20 74 68 65 20 0a 2a 2a 20   before the .** 
14820 72 75 6e 6e 69 6e 67 20 73 74 61 74 65 6d 65 6e  running statemen
14830 74 73 20 72 65 61 63 68 65 73 20 7a 65 72 6f 20  ts reaches zero 
14840 61 72 65 20 69 6e 74 65 72 72 75 70 74 65 64 20  are interrupted 
14850 61 73 20 69 66 20 74 68 65 79 20 68 61 64 20 62  as if they had b
14860 65 65 6e 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 70  een.** running p
14870 72 69 6f 72 20 74 6f 20 74 68 65 20 73 71 6c 69  rior to the sqli
14880 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20  te3_interrupt() 
14890 63 61 6c 6c 2e 20 20 4e 65 77 20 53 51 4c 20 73  call.  New SQL s
148a0 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61  tatements.** tha
148b0 74 20 61 72 65 20 73 74 61 72 74 65 64 20 61 66  t are started af
148c0 74 65 72 20 74 68 65 20 72 75 6e 6e 69 6e 67 20  ter the running 
148d0 73 74 61 74 65 6d 65 6e 74 20 63 6f 75 6e 74 20  statement count 
148e0 72 65 61 63 68 65 73 20 7a 65 72 6f 20 61 72 65  reaches zero are
148f0 0a 2a 2a 20 6e 6f 74 20 65 66 66 65 63 74 65 64  .** not effected
14900 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f   by the sqlite3_
14910 69 6e 74 65 72 72 75 70 74 28 29 2e 0a 2a 2a 20  interrupt()..** 
14920 41 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  A call to sqlite
14930 33 5f 69 6e 74 65 72 72 75 70 74 28 44 29 20 74  3_interrupt(D) t
14940 68 61 74 20 6f 63 63 75 72 73 20 77 68 65 6e 20  hat occurs when 
14950 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72 75 6e  there are no run
14960 6e 69 6e 67 0a 2a 2a 20 53 51 4c 20 73 74 61 74  ning.** SQL stat
14970 65 6d 65 6e 74 73 20 69 73 20 61 20 6e 6f 2d 6f  ements is a no-o
14980 70 20 61 6e 64 20 68 61 73 20 6e 6f 20 65 66 66  p and has no eff
14990 65 63 74 20 6f 6e 20 53 51 4c 20 73 74 61 74 65  ect on SQL state
149a0 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 72  ments.** that ar
149b0 65 20 73 74 61 72 74 65 64 20 61 66 74 65 72 20  e started after 
149c0 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65  the sqlite3_inte
149d0 72 72 75 70 74 28 29 20 63 61 6c 6c 20 72 65 74  rrupt() call ret
149e0 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  urns..**.** Requ
149f0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
14a00 32 32 37 31 5d 20 5b 48 31 32 32 37 32 5d 0a 2a  2271] [H12272].*
14a10 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61  *.** If the data
14a20 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
14a30 63 6c 6f 73 65 73 20 77 68 69 6c 65 20 5b 73 71  closes while [sq
14a40 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28  lite3_interrupt(
14a50 29 5d 0a 2a 2a 20 69 73 20 72 75 6e 6e 69 6e 67  )].** is running
14a60 20 74 68 65 6e 20 62 61 64 20 74 68 69 6e 67 73   then bad things
14a70 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 68 61 70   will likely hap
14a80 70 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  pen..*/.SQLITE_A
14a90 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
14aa0 69 6e 74 65 72 72 75 70 74 28 73 71 6c 69 74 65  interrupt(sqlite
14ab0 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  3*);../*.** CAPI
14ac0 33 52 45 46 3a 20 44 65 74 65 72 6d 69 6e 65 20  3REF: Determine 
14ad0 49 66 20 41 6e 20 53 51 4c 20 53 74 61 74 65 6d  If An SQL Statem
14ae0 65 6e 74 20 49 73 20 43 6f 6d 70 6c 65 74 65 20  ent Is Complete 
14af0 7b 48 31 30 35 31 30 7d 20 3c 53 37 30 32 30 30  {H10510} <S70200
14b00 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f  >.**.** These ro
14b10 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 66 75  utines are usefu
14b20 6c 20 64 75 72 69 6e 67 20 63 6f 6d 6d 61 6e 64  l during command
14b30 2d 6c 69 6e 65 20 69 6e 70 75 74 20 74 6f 20 64  -line input to d
14b40 65 74 65 72 6d 69 6e 65 20 69 66 20 74 68 65 0a  etermine if the.
14b50 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74  ** currently ent
14b60 65 72 65 64 20 74 65 78 74 20 73 65 65 6d 73 20  ered text seems 
14b70 74 6f 20 66 6f 72 6d 20 61 20 63 6f 6d 70 6c 65  to form a comple
14b80 74 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  te SQL statement
14b90 20 6f 72 0a 2a 2a 20 69 66 20 61 64 64 69 74 69   or.** if additi
14ba0 6f 6e 61 6c 20 69 6e 70 75 74 20 69 73 20 6e 65  onal input is ne
14bb0 65 64 65 64 20 62 65 66 6f 72 65 20 73 65 6e 64  eded before send
14bc0 69 6e 67 20 74 68 65 20 74 65 78 74 20 69 6e 74  ing the text int
14bd0 6f 0a 2a 2a 20 53 51 4c 69 74 65 20 66 6f 72 20  o.** SQLite for 
14be0 70 61 72 73 69 6e 67 2e 20 20 54 68 65 73 65 20  parsing.  These 
14bf0 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20  routines return 
14c00 31 20 69 66 20 74 68 65 20 69 6e 70 75 74 20 73  1 if the input s
14c10 74 72 69 6e 67 0a 2a 2a 20 61 70 70 65 61 72 73  tring.** appears
14c20 20 74 6f 20 62 65 20 61 20 63 6f 6d 70 6c 65 74   to be a complet
14c30 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e  e SQL statement.
14c40 20 20 41 20 73 74 61 74 65 6d 65 6e 74 20 69 73    A statement is
14c50 20 6a 75 64 67 65 64 20 74 6f 20 62 65 0a 2a 2a   judged to be.**
14c60 20 63 6f 6d 70 6c 65 74 65 20 69 66 20 69 74 20   complete if it 
14c70 65 6e 64 73 20 77 69 74 68 20 61 20 73 65 6d 69  ends with a semi
14c80 63 6f 6c 6f 6e 20 74 6f 6b 65 6e 20 61 6e 64 20  colon token and 
14c90 69 73 20 6e 6f 74 20 61 20 70 72 65 66 69 78 20  is not a prefix 
14ca0 6f 66 20 61 0a 2a 2a 20 77 65 6c 6c 2d 66 6f 72  of a.** well-for
14cb0 6d 65 64 20 43 52 45 41 54 45 20 54 52 49 47 47  med CREATE TRIGG
14cc0 45 52 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 53  ER statement.  S
14cd0 65 6d 69 63 6f 6c 6f 6e 73 20 74 68 61 74 20 61  emicolons that a
14ce0 72 65 20 65 6d 62 65 64 64 65 64 20 77 69 74 68  re embedded with
14cf0 69 6e 0a 2a 2a 20 73 74 72 69 6e 67 20 6c 69 74  in.** string lit
14d00 65 72 61 6c 73 20 6f 72 20 71 75 6f 74 65 64 20  erals or quoted 
14d10 69 64 65 6e 74 69 66 69 65 72 20 6e 61 6d 65 73  identifier names
14d20 20 6f 72 20 63 6f 6d 6d 65 6e 74 73 20 61 72 65   or comments are
14d30 20 6e 6f 74 0a 2a 2a 20 69 6e 64 65 70 65 6e 64   not.** independ
14d40 65 6e 74 20 74 6f 6b 65 6e 73 20 28 74 68 65 79  ent tokens (they
14d50 20 61 72 65 20 70 61 72 74 20 6f 66 20 74 68 65   are part of the
14d60 20 74 6f 6b 65 6e 20 69 6e 20 77 68 69 63 68 20   token in which 
14d70 74 68 65 79 20 61 72 65 0a 2a 2a 20 65 6d 62 65  they are.** embe
14d80 64 64 65 64 29 20 61 6e 64 20 74 68 75 73 20 64  dded) and thus d
14d90 6f 20 6e 6f 74 20 63 6f 75 6e 74 20 61 73 20 61  o not count as a
14da0 20 73 74 61 74 65 6d 65 6e 74 20 74 65 72 6d 69   statement termi
14db0 6e 61 74 6f 72 2e 20 20 57 68 69 74 65 73 70 61  nator.  Whitespa
14dc0 63 65 0a 2a 2a 20 61 6e 64 20 63 6f 6d 6d 65 6e  ce.** and commen
14dd0 74 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 74  ts that follow t
14de0 68 65 20 66 69 6e 61 6c 20 73 65 6d 69 63 6f 6c  he final semicol
14df0 6f 6e 20 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a  on are ignored..
14e00 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74  **.** These rout
14e10 69 6e 65 73 20 72 65 74 75 72 6e 20 30 20 69 66  ines return 0 if
14e20 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69   the statement i
14e30 73 20 69 6e 63 6f 6d 70 6c 65 74 65 2e 20 20 49  s incomplete.  I
14e40 66 20 61 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c  f a.** memory al
14e50 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20  location fails, 
14e60 74 68 65 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  then SQLITE_NOME
14e70 4d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  M is returned..*
14e80 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69  *.** These routi
14e90 6e 65 73 20 64 6f 20 6e 6f 74 20 70 61 72 73 65  nes do not parse
14ea0 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65   the SQL stateme
14eb0 6e 74 73 20 74 68 75 73 0a 2a 2a 20 77 69 6c 6c  nts thus.** will
14ec0 20 6e 6f 74 20 64 65 74 65 63 74 20 73 79 6e 74   not detect synt
14ed0 61 63 74 69 63 61 6c 6c 79 20 69 6e 63 6f 72 72  actically incorr
14ee0 65 63 74 20 53 51 4c 2e 0a 2a 2a 0a 2a 2a 20 49  ect SQL..**.** I
14ef0 66 20 53 51 4c 69 74 65 20 68 61 73 20 6e 6f 74  f SQLite has not
14f00 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65   been initialize
14f10 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33  d using [sqlite3
14f20 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d 20 70  _initialize()] p
14f30 72 69 6f 72 20 0a 2a 2a 20 74 6f 20 69 6e 76 6f  rior .** to invo
14f40 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f 63 6f 6d  king sqlite3_com
14f50 70 6c 65 74 65 31 36 28 29 20 74 68 65 6e 20 73  plete16() then s
14f60 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
14f70 65 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 0a 2a  e() is invoked.*
14f80 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  * automatically 
14f90 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c  by sqlite3_compl
14fa0 65 74 65 31 36 28 29 2e 20 20 49 66 20 74 68 61  ete16().  If tha
14fb0 74 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  t initialization
14fc0 20 66 61 69 6c 73 2c 0a 2a 2a 20 74 68 65 6e 20   fails,.** then 
14fd0 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  the return value
14fe0 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 63 6f   from sqlite3_co
14ff0 6d 70 6c 65 74 65 31 36 28 29 20 77 69 6c 6c 20  mplete16() will 
15000 62 65 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 72  be non-zero.** r
15010 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65  egardless of whe
15020 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20  ther or not the 
15030 69 6e 70 75 74 20 53 51 4c 20 69 73 20 63 6f 6d  input SQL is com
15040 70 6c 65 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  plete..**.** Req
15050 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30 35  uirements: [H105
15060 31 31 5d 20 5b 48 31 30 35 31 32 5d 0a 2a 2a 0a  11] [H10512].**.
15070 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 74 6f 20  ** The input to 
15080 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74  [sqlite3_complet
15090 65 28 29 5d 20 6d 75 73 74 20 62 65 20 61 20 7a  e()] must be a z
150a0 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a  ero-terminated.*
150b0 2a 20 55 54 46 2d 38 20 73 74 72 69 6e 67 2e 0a  * UTF-8 string..
150c0 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20  **.** The input 
150d0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 70  to [sqlite3_comp
150e0 6c 65 74 65 31 36 28 29 5d 20 6d 75 73 74 20 62  lete16()] must b
150f0 65 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61  e a zero-termina
15100 74 65 64 0a 2a 2a 20 55 54 46 2d 31 36 20 73 74  ted.** UTF-16 st
15110 72 69 6e 67 20 69 6e 20 6e 61 74 69 76 65 20 62  ring in native b
15120 79 74 65 20 6f 72 64 65 72 2e 0a 2a 2f 0a 53 51  yte order..*/.SQ
15130 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
15140 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 63 6f  ite3_complete(co
15150 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 29 3b 0a  nst char *sql);.
15160 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
15170 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31  qlite3_complete1
15180 36 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  6(const void *sq
15190 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  l);../*.** CAPI3
151a0 52 45 46 3a 20 52 65 67 69 73 74 65 72 20 41 20  REF: Register A 
151b0 43 61 6c 6c 62 61 63 6b 20 54 6f 20 48 61 6e 64  Callback To Hand
151c0 6c 65 20 53 51 4c 49 54 45 5f 42 55 53 59 20 45  le SQLITE_BUSY E
151d0 72 72 6f 72 73 20 7b 48 31 32 33 31 30 7d 20 3c  rrors {H12310} <
151e0 53 34 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S40400>.**.** Th
151f0 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20  is routine sets 
15200 61 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  a callback funct
15210 69 6f 6e 20 74 68 61 74 20 6d 69 67 68 74 20 62  ion that might b
15220 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76  e invoked whenev
15230 65 72 0a 2a 2a 20 61 6e 20 61 74 74 65 6d 70 74  er.** an attempt
15240 20 69 73 20 6d 61 64 65 20 74 6f 20 6f 70 65 6e   is made to open
15250 20 61 20 64 61 74 61 62 61 73 65 20 74 61 62 6c   a database tabl
15260 65 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20 74  e that another t
15270 68 72 65 61 64 0a 2a 2a 20 6f 72 20 70 72 6f 63  hread.** or proc
15280 65 73 73 20 68 61 73 20 6c 6f 63 6b 65 64 2e 0a  ess has locked..
15290 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62 75 73  **.** If the bus
152a0 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 4e 55  y callback is NU
152b0 4c 4c 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54 45  LL, then [SQLITE
152c0 5f 42 55 53 59 5d 20 6f 72 20 5b 53 51 4c 49 54  _BUSY] or [SQLIT
152d0 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d  E_IOERR_BLOCKED]
152e0 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20  .** is returned 
152f0 69 6d 6d 65 64 69 61 74 65 6c 79 20 75 70 6f 6e  immediately upon
15300 20 65 6e 63 6f 75 6e 74 65 72 69 6e 67 20 74 68   encountering th
15310 65 20 6c 6f 63 6b 2e 20 49 66 20 74 68 65 20 62  e lock. If the b
15320 75 73 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20  usy callback.** 
15330 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65  is not NULL, the
15340 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 77  n the callback w
15350 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 20 77  ill be invoked w
15360 69 74 68 20 74 77 6f 20 61 72 67 75 6d 65 6e 74  ith two argument
15370 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72  s..**.** The fir
15380 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  st argument to t
15390 68 65 20 68 61 6e 64 6c 65 72 20 69 73 20 61 20  he handler is a 
153a0 63 6f 70 79 20 6f 66 20 74 68 65 20 76 6f 69 64  copy of the void
153b0 2a 20 70 6f 69 6e 74 65 72 20 77 68 69 63 68 0a  * pointer which.
153c0 2a 2a 20 69 73 20 74 68 65 20 74 68 69 72 64 20  ** is the third 
153d0 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69  argument to sqli
153e0 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72  te3_busy_handler
153f0 28 29 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20  ().  The second 
15400 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 74  argument to.** t
15410 68 65 20 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62  he handler callb
15420 61 63 6b 20 69 73 20 74 68 65 20 6e 75 6d 62 65  ack is the numbe
15430 72 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20  r of times that 
15440 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72  the busy handler
15450 20 68 61 73 0a 2a 2a 20 62 65 65 6e 20 69 6e 76   has.** been inv
15460 6f 6b 65 64 20 66 6f 72 20 74 68 69 73 20 6c 6f  oked for this lo
15470 63 6b 69 6e 67 20 65 76 65 6e 74 2e 20 20 49 66  cking event.  If
15480 20 74 68 65 0a 2a 2a 20 62 75 73 79 20 63 61 6c   the.** busy cal
15490 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 30 2c  lback returns 0,
154a0 20 74 68 65 6e 20 6e 6f 20 61 64 64 69 74 69 6f   then no additio
154b0 6e 61 6c 20 61 74 74 65 6d 70 74 73 20 61 72 65  nal attempts are
154c0 20 6d 61 64 65 20 74 6f 0a 2a 2a 20 61 63 63 65   made to.** acce
154d0 73 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ss the database 
154e0 61 6e 64 20 5b 53 51 4c 49 54 45 5f 42 55 53 59  and [SQLITE_BUSY
154f0 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45  ] or [SQLITE_IOE
15500 52 52 5f 42 4c 4f 43 4b 45 44 5d 20 69 73 20 72  RR_BLOCKED] is r
15510 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 49 66 20 74  eturned..** If t
15520 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75  he callback retu
15530 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68  rns non-zero, th
15540 65 6e 20 61 6e 6f 74 68 65 72 20 61 74 74 65 6d  en another attem
15550 70 74 0a 2a 2a 20 69 73 20 6d 61 64 65 20 74 6f  pt.** is made to
15560 20 6f 70 65 6e 20 74 68 65 20 64 61 74 61 62 61   open the databa
15570 73 65 20 66 6f 72 20 72 65 61 64 69 6e 67 20 61  se for reading a
15580 6e 64 20 74 68 65 20 63 79 63 6c 65 20 72 65 70  nd the cycle rep
15590 65 61 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  eats..**.** The 
155a0 70 72 65 73 65 6e 63 65 20 6f 66 20 61 20 62 75  presence of a bu
155b0 73 79 20 68 61 6e 64 6c 65 72 20 64 6f 65 73 20  sy handler does 
155c0 6e 6f 74 20 67 75 61 72 61 6e 74 65 65 20 74 68  not guarantee th
155d0 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20 69 6e  at it will be in
155e0 76 6f 6b 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68  voked.** when th
155f0 65 72 65 20 69 73 20 6c 6f 63 6b 20 63 6f 6e 74  ere is lock cont
15600 65 6e 74 69 6f 6e 2e 20 49 66 20 53 51 4c 69 74  ention. If SQLit
15610 65 20 64 65 74 65 72 6d 69 6e 65 73 20 74 68 61  e determines tha
15620 74 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 62  t invoking the b
15630 75 73 79 0a 2a 2a 20 68 61 6e 64 6c 65 72 20 63  usy.** handler c
15640 6f 75 6c 64 20 72 65 73 75 6c 74 20 69 6e 20 61  ould result in a
15650 20 64 65 61 64 6c 6f 63 6b 2c 20 69 74 20 77 69   deadlock, it wi
15660 6c 6c 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20  ll go ahead and 
15670 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 42  return [SQLITE_B
15680 55 53 59 5d 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49  USY].** or [SQLI
15690 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44  TE_IOERR_BLOCKED
156a0 5d 20 69 6e 73 74 65 61 64 20 6f 66 20 69 6e 76  ] instead of inv
156b0 6f 6b 69 6e 67 20 74 68 65 20 62 75 73 79 20 68  oking the busy h
156c0 61 6e 64 6c 65 72 2e 0a 2a 2a 20 43 6f 6e 73 69  andler..** Consi
156d0 64 65 72 20 61 20 73 63 65 6e 61 72 69 6f 20 77  der a scenario w
156e0 68 65 72 65 20 6f 6e 65 20 70 72 6f 63 65 73 73  here one process
156f0 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 72 65   is holding a re
15700 61 64 20 6c 6f 63 6b 20 74 68 61 74 0a 2a 2a 20  ad lock that.** 
15710 69 74 20 69 73 20 74 72 79 69 6e 67 20 74 6f 20  it is trying to 
15720 70 72 6f 6d 6f 74 65 20 74 6f 20 61 20 72 65 73  promote to a res
15730 65 72 76 65 64 20 6c 6f 63 6b 20 61 6e 64 0a 2a  erved lock and.*
15740 2a 20 61 20 73 65 63 6f 6e 64 20 70 72 6f 63 65  * a second proce
15750 73 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20  ss is holding a 
15760 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 74 68  reserved lock th
15770 61 74 20 69 74 20 69 73 20 74 72 79 69 6e 67 0a  at it is trying.
15780 2a 2a 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f  ** to promote to
15790 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f   an exclusive lo
157a0 63 6b 2e 20 20 54 68 65 20 66 69 72 73 74 20 70  ck.  The first p
157b0 72 6f 63 65 73 73 20 63 61 6e 6e 6f 74 20 70 72  rocess cannot pr
157c0 6f 63 65 65 64 0a 2a 2a 20 62 65 63 61 75 73 65  oceed.** because
157d0 20 69 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 62   it is blocked b
157e0 79 20 74 68 65 20 73 65 63 6f 6e 64 20 61 6e 64  y the second and
157f0 20 74 68 65 20 73 65 63 6f 6e 64 20 70 72 6f 63   the second proc
15800 65 73 73 20 63 61 6e 6e 6f 74 0a 2a 2a 20 70 72  ess cannot.** pr
15810 6f 63 65 65 64 20 62 65 63 61 75 73 65 20 69 74  oceed because it
15820 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 20 74   is blocked by t
15830 68 65 20 66 69 72 73 74 2e 20 20 49 66 20 62 6f  he first.  If bo
15840 74 68 20 70 72 6f 63 65 73 73 65 73 0a 2a 2a 20  th processes.** 
15850 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20  invoke the busy 
15860 68 61 6e 64 6c 65 72 73 2c 20 6e 65 69 74 68 65  handlers, neithe
15870 72 20 77 69 6c 6c 20 6d 61 6b 65 20 61 6e 79 20  r will make any 
15880 70 72 6f 67 72 65 73 73 2e 20 20 54 68 65 72 65  progress.  There
15890 66 6f 72 65 2c 0a 2a 2a 20 53 51 4c 69 74 65 20  fore,.** SQLite 
158a0 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f  returns [SQLITE_
158b0 42 55 53 59 5d 20 66 6f 72 20 74 68 65 20 66 69  BUSY] for the fi
158c0 72 73 74 20 70 72 6f 63 65 73 73 2c 20 68 6f 70  rst process, hop
158d0 69 6e 67 20 74 68 61 74 20 74 68 69 73 0a 2a 2a  ing that this.**
158e0 20 77 69 6c 6c 20 69 6e 64 75 63 65 20 74 68 65   will induce the
158f0 20 66 69 72 73 74 20 70 72 6f 63 65 73 73 20 74   first process t
15900 6f 20 72 65 6c 65 61 73 65 20 69 74 73 20 72 65  o release its re
15910 61 64 20 6c 6f 63 6b 20 61 6e 64 20 61 6c 6c 6f  ad lock and allo
15920 77 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 20  w.** the second 
15930 70 72 6f 63 65 73 73 20 74 6f 20 70 72 6f 63 65  process to proce
15940 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65  ed..**.** The de
15950 66 61 75 6c 74 20 62 75 73 79 20 63 61 6c 6c 62  fault busy callb
15960 61 63 6b 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a  ack is NULL..**.
15970 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 42  ** The [SQLITE_B
15980 55 53 59 5d 20 65 72 72 6f 72 20 69 73 20 63 6f  USY] error is co
15990 6e 76 65 72 74 65 64 20 74 6f 20 5b 53 51 4c 49  nverted to [SQLI
159a0 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44  TE_IOERR_BLOCKED
159b0 5d 0a 2a 2a 20 77 68 65 6e 20 53 51 4c 69 74 65  ].** when SQLite
159c0 20 69 73 20 69 6e 20 74 68 65 20 6d 69 64 64 6c   is in the middl
159d0 65 20 6f 66 20 61 20 6c 61 72 67 65 20 74 72 61  e of a large tra
159e0 6e 73 61 63 74 69 6f 6e 20 77 68 65 72 65 20 61  nsaction where a
159f0 6c 6c 20 74 68 65 0a 2a 2a 20 63 68 61 6e 67 65  ll the.** change
15a00 73 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 20 69  s will not fit i
15a10 6e 74 6f 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72  nto the in-memor
15a20 79 20 63 61 63 68 65 2e 20 20 53 51 4c 69 74 65  y cache.  SQLite
15a30 20 77 69 6c 6c 0a 2a 2a 20 61 6c 72 65 61 64 79   will.** already
15a40 20 68 6f 6c 64 20 61 20 52 45 53 45 52 56 45 44   hold a RESERVED
15a50 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74   lock on the dat
15a60 61 62 61 73 65 20 66 69 6c 65 2c 20 62 75 74 20  abase file, but 
15a70 69 74 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 70  it needs.** to p
15a80 72 6f 6d 6f 74 65 20 74 68 69 73 20 6c 6f 63 6b  romote this lock
15a90 20 74 6f 20 45 58 43 4c 55 53 49 56 45 20 73 6f   to EXCLUSIVE so
15aa0 20 74 68 61 74 20 69 74 20 63 61 6e 20 73 70 69   that it can spi
15ab0 6c 6c 20 63 61 63 68 65 0a 2a 2a 20 70 61 67 65  ll cache.** page
15ac0 73 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62  s into the datab
15ad0 61 73 65 20 66 69 6c 65 20 77 69 74 68 6f 75 74  ase file without
15ae0 20 68 61 72 6d 20 74 6f 20 63 6f 6e 63 75 72 72   harm to concurr
15af0 65 6e 74 0a 2a 2a 20 72 65 61 64 65 72 73 2e 20  ent.** readers. 
15b00 20 49 66 20 69 74 20 69 73 20 75 6e 61 62 6c 65   If it is unable
15b10 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 68 65 20   to promote the 
15b20 6c 6f 63 6b 2c 20 74 68 65 6e 20 74 68 65 20 69  lock, then the i
15b30 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 63 61 63 68  n-memory.** cach
15b40 65 20 77 69 6c 6c 20 62 65 20 6c 65 66 74 20 69  e will be left i
15b50 6e 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e  n an inconsisten
15b60 74 20 73 74 61 74 65 20 61 6e 64 20 73 6f 20 74  t state and so t
15b70 68 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65  he error.** code
15b80 20 69 73 20 70 72 6f 6d 6f 74 65 64 20 66 72 6f   is promoted fro
15b90 6d 20 74 68 65 20 72 65 6c 61 74 69 76 65 6c 79  m the relatively
15ba0 20 62 65 6e 69 67 6e 20 5b 53 51 4c 49 54 45 5f   benign [SQLITE_
15bb0 42 55 53 59 5d 20 74 6f 0a 2a 2a 20 74 68 65 20  BUSY] to.** the 
15bc0 6d 6f 72 65 20 73 65 76 65 72 65 20 5b 53 51 4c  more severe [SQL
15bd0 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45  ITE_IOERR_BLOCKE
15be0 44 5d 2e 20 20 54 68 69 73 20 65 72 72 6f 72 20  D].  This error 
15bf0 63 6f 64 65 20 70 72 6f 6d 6f 74 69 6f 6e 0a 2a  code promotion.*
15c00 2a 20 66 6f 72 63 65 73 20 61 6e 20 61 75 74 6f  * forces an auto
15c10 6d 61 74 69 63 20 72 6f 6c 6c 62 61 63 6b 20 6f  matic rollback o
15c20 66 20 74 68 65 20 63 68 61 6e 67 65 73 2e 20 20  f the changes.  
15c30 53 65 65 20 74 68 65 0a 2a 2a 20 3c 61 20 68 72  See the.** <a hr
15c40 65 66 3d 22 2f 63 76 73 74 72 61 63 2f 77 69 6b  ef="/cvstrac/wik
15c50 69 3f 70 3d 43 6f 72 72 75 70 74 69 6f 6e 46 6f  i?p=CorruptionFo
15c60 6c 6c 6f 77 69 6e 67 42 75 73 79 45 72 72 6f 72  llowingBusyError
15c70 22 3e 0a 2a 2a 20 43 6f 72 72 75 70 74 69 6f 6e  ">.** Corruption
15c80 46 6f 6c 6c 6f 77 69 6e 67 42 75 73 79 45 72 72  FollowingBusyErr
15c90 6f 72 3c 2f 61 3e 20 77 69 6b 69 20 70 61 67 65  or</a> wiki page
15ca0 20 66 6f 72 20 61 20 64 69 73 63 75 73 73 69 6f   for a discussio
15cb0 6e 20 6f 66 20 77 68 79 0a 2a 2a 20 74 68 69 73  n of why.** this
15cc0 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 2e 0a 2a   is important..*
15cd0 2a 0a 2a 2a 20 54 68 65 72 65 20 63 61 6e 20 6f  *.** There can o
15ce0 6e 6c 79 20 62 65 20 61 20 73 69 6e 67 6c 65 20  nly be a single 
15cf0 62 75 73 79 20 68 61 6e 64 6c 65 72 20 64 65 66  busy handler def
15d00 69 6e 65 64 20 66 6f 72 20 65 61 63 68 0a 2a 2a  ined for each.**
15d10 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
15d20 63 74 69 6f 6e 5d 2e 20 20 53 65 74 74 69 6e 67  ction].  Setting
15d30 20 61 20 6e 65 77 20 62 75 73 79 20 68 61 6e 64   a new busy hand
15d40 6c 65 72 20 63 6c 65 61 72 73 20 61 6e 79 0a 2a  ler clears any.*
15d50 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 73 65 74  * previously set
15d60 20 68 61 6e 64 6c 65 72 2e 20 20 4e 6f 74 65 20   handler.  Note 
15d70 74 68 61 74 20 63 61 6c 6c 69 6e 67 20 5b 73 71  that calling [sq
15d80 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f  lite3_busy_timeo
15d90 75 74 28 29 5d 0a 2a 2a 20 77 69 6c 6c 20 61 6c  ut()].** will al
15da0 73 6f 20 73 65 74 20 6f 72 20 63 6c 65 61 72 20  so set or clear 
15db0 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72  the busy handler
15dc0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 75 73 79  ..**.** The busy
15dd0 20 63 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c 64   callback should
15de0 20 6e 6f 74 20 74 61 6b 65 20 61 6e 79 20 61 63   not take any ac
15df0 74 69 6f 6e 73 20 77 68 69 63 68 20 6d 6f 64 69  tions which modi
15e00 66 79 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61  fy the.** databa
15e10 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68  se connection th
15e20 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 62  at invoked the b
15e30 75 73 79 20 68 61 6e 64 6c 65 72 2e 20 20 41 6e  usy handler.  An
15e40 79 20 73 75 63 68 20 61 63 74 69 6f 6e 73 0a 2a  y such actions.*
15e50 2a 20 72 65 73 75 6c 74 20 69 6e 20 75 6e 64 65  * result in unde
15e60 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72 2e 0a  fined behavior..
15e70 2a 2a 20 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  ** .** Requireme
15e80 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 33 31 31 5d  nts:.** [H12311]
15e90 20 5b 48 31 32 33 31 32 5d 20 5b 48 31 32 33 31   [H12312] [H1231
15ea0 34 5d 20 5b 48 31 32 33 31 36 5d 20 5b 48 31 32  4] [H12316] [H12
15eb0 33 31 38 5d 0a 2a 2a 0a 2a 2a 20 41 20 62 75 73  318].**.** A bus
15ec0 79 20 68 61 6e 64 6c 65 72 20 6d 75 73 74 20 6e  y handler must n
15ed0 6f 74 20 63 6c 6f 73 65 20 74 68 65 20 64 61 74  ot close the dat
15ee0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
15ef0 0a 2a 2a 20 6f 72 20 5b 70 72 65 70 61 72 65 64  .** or [prepared
15f00 20 73 74 61 74 65 6d 65 6e 74 5d 20 74 68 61 74   statement] that
15f10 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 62 75 73   invoked the bus
15f20 79 20 68 61 6e 64 6c 65 72 2e 0a 2a 2f 0a 53 51  y handler..*/.SQ
15f30 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
15f40 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65  ite3_busy_handle
15f50 72 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 28  r(sqlite3*, int(
15f60 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 29 2c 20 76  *)(void*,int), v
15f70 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  oid*);../*.** CA
15f80 50 49 33 52 45 46 3a 20 53 65 74 20 41 20 42 75  PI3REF: Set A Bu
15f90 73 79 20 54 69 6d 65 6f 75 74 20 7b 48 31 32 33  sy Timeout {H123
15fa0 34 30 7d 20 3c 53 34 30 34 31 30 3e 0a 2a 2a 0a  40} <S40410>.**.
15fb0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
15fc0 73 65 74 73 20 61 20 5b 73 71 6c 69 74 65 33 5f  sets a [sqlite3_
15fd0 62 75 73 79 5f 68 61 6e 64 6c 65 72 20 7c 20 62  busy_handler | b
15fe0 75 73 79 20 68 61 6e 64 6c 65 72 5d 20 74 68 61  usy handler] tha
15ff0 74 20 73 6c 65 65 70 73 0a 2a 2a 20 66 6f 72 20  t sleeps.** for 
16000 61 20 73 70 65 63 69 66 69 65 64 20 61 6d 6f 75  a specified amou
16010 6e 74 20 6f 66 20 74 69 6d 65 20 77 68 65 6e 20  nt of time when 
16020 61 20 74 61 62 6c 65 20 69 73 20 6c 6f 63 6b 65  a table is locke
16030 64 2e 20 20 54 68 65 20 68 61 6e 64 6c 65 72 0a  d.  The handler.
16040 2a 2a 20 77 69 6c 6c 20 73 6c 65 65 70 20 6d 75  ** will sleep mu
16050 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 75 6e 74  ltiple times unt
16060 69 6c 20 61 74 20 6c 65 61 73 74 20 22 6d 73 22  il at least "ms"
16070 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66   milliseconds of
16080 20 73 6c 65 65 70 69 6e 67 0a 2a 2a 20 68 61 76   sleeping.** hav
16090 65 20 61 63 63 75 6d 75 6c 61 74 65 64 2e 20 7b  e accumulated. {
160a0 48 31 32 33 34 33 7d 20 41 66 74 65 72 20 22 6d  H12343} After "m
160b0 73 22 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20  s" milliseconds 
160c0 6f 66 20 73 6c 65 65 70 69 6e 67 2c 0a 2a 2a 20  of sleeping,.** 
160d0 74 68 65 20 68 61 6e 64 6c 65 72 20 72 65 74 75  the handler retu
160e0 72 6e 73 20 30 20 77 68 69 63 68 20 63 61 75 73  rns 0 which caus
160f0 65 73 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  es [sqlite3_step
16100 28 29 5d 20 74 6f 20 72 65 74 75 72 6e 0a 2a 2a  ()] to return.**
16110 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f   [SQLITE_BUSY] o
16120 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  r [SQLITE_IOERR_
16130 42 4c 4f 43 4b 45 44 5d 2e 0a 2a 2a 0a 2a 2a 20  BLOCKED]..**.** 
16140 43 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75  Calling this rou
16150 74 69 6e 65 20 77 69 74 68 20 61 6e 20 61 72 67  tine with an arg
16160 75 6d 65 6e 74 20 6c 65 73 73 20 74 68 61 6e 20  ument less than 
16170 6f 72 20 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f  or equal to zero
16180 0a 2a 2a 20 74 75 72 6e 73 20 6f 66 66 20 61 6c  .** turns off al
16190 6c 20 62 75 73 79 20 68 61 6e 64 6c 65 72 73 2e  l busy handlers.
161a0 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 63 61 6e  .**.** There can
161b0 20 6f 6e 6c 79 20 62 65 20 61 20 73 69 6e 67 6c   only be a singl
161c0 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 66  e busy handler f
161d0 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a  or a particular.
161e0 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  ** [database con
161f0 6e 65 63 74 69 6f 6e 5d 20 61 6e 79 20 61 6e 79  nection] any any
16200 20 67 69 76 65 6e 20 6d 6f 6d 65 6e 74 2e 20 20   given moment.  
16210 49 66 20 61 6e 6f 74 68 65 72 20 62 75 73 79 20  If another busy 
16220 68 61 6e 64 6c 65 72 0a 2a 2a 20 77 61 73 20 64  handler.** was d
16230 65 66 69 6e 65 64 20 20 28 75 73 69 6e 67 20 5b  efined  (using [
16240 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e  sqlite3_busy_han
16250 64 6c 65 72 28 29 5d 29 20 70 72 69 6f 72 20 74  dler()]) prior t
16260 6f 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 69  o calling.** thi
16270 73 20 72 6f 75 74 69 6e 65 2c 20 74 68 61 74 20  s routine, that 
16280 6f 74 68 65 72 20 62 75 73 79 20 68 61 6e 64 6c  other busy handl
16290 65 72 20 69 73 20 63 6c 65 61 72 65 64 2e 0a 2a  er is cleared..*
162a0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
162b0 73 3a 0a 2a 2a 20 5b 48 31 32 33 34 31 5d 20 5b  s:.** [H12341] [
162c0 48 31 32 33 34 33 5d 20 5b 48 31 32 33 34 34 5d  H12343] [H12344]
162d0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
162e0 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f  nt sqlite3_busy_
162f0 74 69 6d 65 6f 75 74 28 73 71 6c 69 74 65 33 2a  timeout(sqlite3*
16300 2c 20 69 6e 74 20 6d 73 29 3b 0a 0a 2f 2a 0a 2a  , int ms);../*.*
16310 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 76  * CAPI3REF: Conv
16320 65 6e 69 65 6e 63 65 20 52 6f 75 74 69 6e 65 73  enience Routines
16330 20 46 6f 72 20 52 75 6e 6e 69 6e 67 20 51 75 65   For Running Que
16340 72 69 65 73 20 7b 48 31 32 33 37 30 7d 20 3c 53  ries {H12370} <S
16350 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 44 65 66  10000>.**.** Def
16360 69 6e 69 74 69 6f 6e 3a 20 41 20 3c 62 3e 72 65  inition: A <b>re
16370 73 75 6c 74 20 74 61 62 6c 65 3c 2f 62 3e 20 69  sult table</b> i
16380 73 20 6d 65 6d 6f 72 79 20 64 61 74 61 20 73 74  s memory data st
16390 72 75 63 74 75 72 65 20 63 72 65 61 74 65 64 20  ructure created 
163a0 62 79 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74  by the.** [sqlit
163b0 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d 20  e3_get_table()] 
163c0 69 6e 74 65 72 66 61 63 65 2e 20 20 41 20 72 65  interface.  A re
163d0 73 75 6c 74 20 74 61 62 6c 65 20 72 65 63 6f 72  sult table recor
163e0 64 73 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c 65  ds the.** comple
163f0 74 65 20 71 75 65 72 79 20 72 65 73 75 6c 74 73  te query results
16400 20 66 72 6f 6d 20 6f 6e 65 20 6f 72 20 6d 6f 72   from one or mor
16410 65 20 71 75 65 72 69 65 73 2e 0a 2a 2a 0a 2a 2a  e queries..**.**
16420 20 54 68 65 20 74 61 62 6c 65 20 63 6f 6e 63 65   The table conce
16430 70 74 75 61 6c 6c 79 20 68 61 73 20 61 20 6e 75  ptually has a nu
16440 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 61 6e 64  mber of rows and
16450 20 63 6f 6c 75 6d 6e 73 2e 20 20 42 75 74 0a 2a   columns.  But.*
16460 2a 20 74 68 65 73 65 20 6e 75 6d 62 65 72 73 20  * these numbers 
16470 61 72 65 20 6e 6f 74 20 70 61 72 74 20 6f 66 20  are not part of 
16480 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65  the result table
16490 20 69 74 73 65 6c 66 2e 20 20 54 68 65 73 65 0a   itself.  These.
164a0 2a 2a 20 6e 75 6d 62 65 72 73 20 61 72 65 20 6f  ** numbers are o
164b0 62 74 61 69 6e 65 64 20 73 65 70 61 72 61 74 65  btained separate
164c0 6c 79 2e 20 20 4c 65 74 20 4e 20 62 65 20 74 68  ly.  Let N be th
164d0 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  e number of rows
164e0 0a 2a 2a 20 61 6e 64 20 4d 20 62 65 20 74 68 65  .** and M be the
164f0 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
16500 6e 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 73 75  ns..**.** A resu
16510 6c 74 20 74 61 62 6c 65 20 69 73 20 61 6e 20 61  lt table is an a
16520 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 65 72 73  rray of pointers
16530 20 74 6f 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61   to zero-termina
16540 74 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67  ted UTF-8 string
16550 73 2e 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20  s..** There are 
16560 28 4e 2b 31 29 2a 4d 20 65 6c 65 6d 65 6e 74 73  (N+1)*M elements
16570 20 69 6e 20 74 68 65 20 61 72 72 61 79 2e 20 20   in the array.  
16580 54 68 65 20 66 69 72 73 74 20 4d 20 70 6f 69 6e  The first M poin
16590 74 65 72 73 20 70 6f 69 6e 74 0a 2a 2a 20 74 6f  ters point.** to
165a0 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64   zero-terminated
165b0 20 73 74 72 69 6e 67 73 20 74 68 61 74 20 20 63   strings that  c
165c0 6f 6e 74 61 69 6e 20 74 68 65 20 6e 61 6d 65 73  ontain the names
165d0 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 2e   of the columns.
165e0 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e 69 6e  .** The remainin
165f0 67 20 65 6e 74 72 69 65 73 20 61 6c 6c 20 70 6f  g entries all po
16600 69 6e 74 20 74 6f 20 71 75 65 72 79 20 72 65 73  int to query res
16610 75 6c 74 73 2e 20 20 4e 55 4c 4c 20 76 61 6c 75  ults.  NULL valu
16620 65 73 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20  es result.** in 
16630 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73 2e 20 20  NULL pointers.  
16640 41 6c 6c 20 6f 74 68 65 72 20 76 61 6c 75 65 73  All other values
16650 20 61 72 65 20 69 6e 20 74 68 65 69 72 20 55 54   are in their UT
16660 46 2d 38 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61  F-8 zero-termina
16670 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 72 65  ted.** string re
16680 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 61 73 20  presentation as 
16690 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c  returned by [sql
166a0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
166b0 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 73  ()]..**.** A res
166c0 75 6c 74 20 74 61 62 6c 65 20 6d 69 67 68 74 20  ult table might 
166d0 63 6f 6e 73 69 73 74 20 6f 66 20 6f 6e 65 20 6f  consist of one o
166e0 72 20 6d 6f 72 65 20 6d 65 6d 6f 72 79 20 61 6c  r more memory al
166f0 6c 6f 63 61 74 69 6f 6e 73 2e 0a 2a 2a 20 49 74  locations..** It
16700 20 69 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20   is not safe to 
16710 70 61 73 73 20 61 20 72 65 73 75 6c 74 20 74 61  pass a result ta
16720 62 6c 65 20 64 69 72 65 63 74 6c 79 20 74 6f 20  ble directly to 
16730 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d  [sqlite3_free()]
16740 2e 0a 2a 2a 20 41 20 72 65 73 75 6c 74 20 74 61  ..** A result ta
16750 62 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 64 65  ble should be de
16760 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 20  allocated using 
16770 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61  [sqlite3_free_ta
16780 62 6c 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 41 73  ble()]..**.** As
16790 20 61 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 74   an example of t
167a0 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20  he result table 
167b0 66 6f 72 6d 61 74 2c 20 73 75 70 70 6f 73 65 20  format, suppose 
167c0 61 20 71 75 65 72 79 20 72 65 73 75 6c 74 0a 2a  a query result.*
167d0 2a 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  * is as follows:
167e0 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f  .**.** <blockquo
167f0 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20  te><pre>.**     
16800 20 20 20 4e 61 6d 65 20 20 20 20 20 20 20 20 7c     Name        |
16810 20 41 67 65 0a 2a 2a 20 20 20 20 20 20 20 20 2d   Age.**        -
16820 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
16830 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 20  ------.**       
16840 20 41 6c 69 63 65 20 20 20 20 20 20 20 7c 20 34   Alice       | 4
16850 33 0a 2a 2a 20 20 20 20 20 20 20 20 42 6f 62 20  3.**        Bob 
16860 20 20 20 20 20 20 20 20 7c 20 32 38 0a 2a 2a 20          | 28.** 
16870 20 20 20 20 20 20 20 43 69 6e 64 79 20 20 20 20         Cindy    
16880 20 20 20 7c 20 32 31 0a 2a 2a 20 3c 2f 70 72 65     | 21.** </pre
16890 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a  ></blockquote>.*
168a0 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74  *.** There are t
168b0 77 6f 20 63 6f 6c 75 6d 6e 20 28 4d 3d 3d 32 29  wo column (M==2)
168c0 20 61 6e 64 20 74 68 72 65 65 20 72 6f 77 73 20   and three rows 
168d0 28 4e 3d 3d 33 29 2e 20 20 54 68 75 73 20 74 68  (N==3).  Thus th
168e0 65 0a 2a 2a 20 72 65 73 75 6c 74 20 74 61 62 6c  e.** result tabl
168f0 65 20 68 61 73 20 38 20 65 6e 74 72 69 65 73 2e  e has 8 entries.
16900 20 20 53 75 70 70 6f 73 65 20 74 68 65 20 72 65    Suppose the re
16910 73 75 6c 74 20 74 61 62 6c 65 20 69 73 20 73 74  sult table is st
16920 6f 72 65 64 0a 2a 2a 20 69 6e 20 61 6e 20 61 72  ored.** in an ar
16930 72 61 79 20 6e 61 6d 65 73 20 61 7a 52 65 73 75  ray names azResu
16940 6c 74 2e 20 20 54 68 65 6e 20 61 7a 52 65 73 75  lt.  Then azResu
16950 6c 74 20 68 6f 6c 64 73 20 74 68 69 73 20 63 6f  lt holds this co
16960 6e 74 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c  ntent:.**.** <bl
16970 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a  ockquote><pre>.*
16980 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c  *        azResul
16990 74 26 23 39 31 3b 30 5d 20 3d 20 22 4e 61 6d 65  t&#91;0] = "Name
169a0 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52  ";.**        azR
169b0 65 73 75 6c 74 26 23 39 31 3b 31 5d 20 3d 20 22  esult&#91;1] = "
169c0 41 67 65 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20  Age";.**        
169d0 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 32 5d 20  azResult&#91;2] 
169e0 3d 20 22 41 6c 69 63 65 22 3b 0a 2a 2a 20 20 20  = "Alice";.**   
169f0 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39       azResult&#9
16a00 31 3b 33 5d 20 3d 20 22 34 33 22 3b 0a 2a 2a 20  1;3] = "43";.** 
16a10 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26         azResult&
16a20 23 39 31 3b 34 5d 20 3d 20 22 42 6f 62 22 3b 0a  #91;4] = "Bob";.
16a30 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75  **        azResu
16a40 6c 74 26 23 39 31 3b 35 5d 20 3d 20 22 32 38 22  lt&#91;5] = "28"
16a50 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65  ;.**        azRe
16a60 73 75 6c 74 26 23 39 31 3b 36 5d 20 3d 20 22 43  sult&#91;6] = "C
16a70 69 6e 64 79 22 3b 0a 2a 2a 20 20 20 20 20 20 20  indy";.**       
16a80 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 37 5d   azResult&#91;7]
16a90 20 3d 20 22 32 31 22 3b 0a 2a 2a 20 3c 2f 70 72   = "21";.** </pr
16aa0 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a  e></blockquote>.
16ab0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
16ac0 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 66 75  3_get_table() fu
16ad0 6e 63 74 69 6f 6e 20 65 76 61 6c 75 61 74 65 73  nction evaluates
16ae0 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20   one or more.** 
16af0 73 65 6d 69 63 6f 6c 6f 6e 2d 73 65 70 61 72 61  semicolon-separa
16b00 74 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  ted SQL statemen
16b10 74 73 20 69 6e 20 74 68 65 20 7a 65 72 6f 2d 74  ts in the zero-t
16b20 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 0a  erminated UTF-8.
16b30 2a 2a 20 73 74 72 69 6e 67 20 6f 66 20 69 74 73  ** string of its
16b40 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20   2nd parameter. 
16b50 20 49 74 20 72 65 74 75 72 6e 73 20 61 20 72 65   It returns a re
16b60 73 75 6c 74 20 74 61 62 6c 65 20 74 6f 20 74 68  sult table to th
16b70 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 67 69 76  e.** pointer giv
16b80 65 6e 20 69 6e 20 69 74 73 20 33 72 64 20 70 61  en in its 3rd pa
16b90 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41  rameter..**.** A
16ba0 66 74 65 72 20 74 68 65 20 63 61 6c 6c 69 6e 67  fter the calling
16bb0 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 66 69   function has fi
16bc0 6e 69 73 68 65 64 20 75 73 69 6e 67 20 74 68 65  nished using the
16bd0 20 72 65 73 75 6c 74 2c 20 69 74 20 73 68 6f 75   result, it shou
16be0 6c 64 0a 2a 2a 20 70 61 73 73 20 74 68 65 20 70  ld.** pass the p
16bf0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72 65  ointer to the re
16c00 73 75 6c 74 20 74 61 62 6c 65 20 74 6f 20 73 71  sult table to sq
16c10 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65  lite3_free_table
16c20 28 29 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a  () in order to.*
16c30 2a 20 72 65 6c 65 61 73 65 20 74 68 65 20 6d 65  * release the me
16c40 6d 6f 72 79 20 74 68 61 74 20 77 61 73 20 6d 61  mory that was ma
16c50 6c 6c 6f 63 65 64 2e 20 20 42 65 63 61 75 73 65  lloced.  Because
16c60 20 6f 66 20 74 68 65 20 77 61 79 20 74 68 65 0a   of the way the.
16c70 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  ** [sqlite3_mall
16c80 6f 63 28 29 5d 20 68 61 70 70 65 6e 73 20 77 69  oc()] happens wi
16c90 74 68 69 6e 20 73 71 6c 69 74 65 33 5f 67 65 74  thin sqlite3_get
16ca0 5f 74 61 62 6c 65 28 29 2c 20 74 68 65 20 63 61  _table(), the ca
16cb0 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f  lling.** functio
16cc0 6e 20 6d 75 73 74 20 6e 6f 74 20 74 72 79 20 74  n must not try t
16cd0 6f 20 63 61 6c 6c 20 5b 73 71 6c 69 74 65 33 5f  o call [sqlite3_
16ce0 66 72 65 65 28 29 5d 20 64 69 72 65 63 74 6c 79  free()] directly
16cf0 2e 20 20 4f 6e 6c 79 0a 2a 2a 20 5b 73 71 6c 69  .  Only.** [sqli
16d00 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 29  te3_free_table()
16d10 5d 20 69 73 20 61 62 6c 65 20 74 6f 20 72 65 6c  ] is able to rel
16d20 65 61 73 65 20 74 68 65 20 6d 65 6d 6f 72 79 20  ease the memory 
16d30 70 72 6f 70 65 72 6c 79 20 61 6e 64 20 73 61 66  properly and saf
16d40 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ely..**.** The s
16d50 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65  qlite3_get_table
16d60 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20  () interface is 
16d70 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61  implemented as a
16d80 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 0a   wrapper around.
16d90 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63  ** [sqlite3_exec
16da0 28 29 5d 2e 20 20 54 68 65 20 73 71 6c 69 74 65  ()].  The sqlite
16db0 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 72 6f  3_get_table() ro
16dc0 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20 68  utine does not h
16dd0 61 76 65 20 61 63 63 65 73 73 0a 2a 2a 20 74 6f  ave access.** to
16de0 20 61 6e 79 20 69 6e 74 65 72 6e 61 6c 20 64 61   any internal da
16df0 74 61 20 73 74 72 75 63 74 75 72 65 73 20 6f 66  ta structures of
16e00 20 53 51 4c 69 74 65 2e 20 20 49 74 20 75 73 65   SQLite.  It use
16e10 73 20 6f 6e 6c 79 20 74 68 65 20 70 75 62 6c 69  s only the publi
16e20 63 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 64  c.** interface d
16e30 65 66 69 6e 65 64 20 68 65 72 65 2e 20 20 41 73  efined here.  As
16e40 20 61 20 63 6f 6e 73 65 71 75 65 6e 63 65 2c 20   a consequence, 
16e50 65 72 72 6f 72 73 20 74 68 61 74 20 6f 63 63 75  errors that occu
16e60 72 20 69 6e 20 74 68 65 0a 2a 2a 20 77 72 61 70  r in the.** wrap
16e70 70 65 72 20 6c 61 79 65 72 20 6f 75 74 73 69 64  per layer outsid
16e80 65 20 6f 66 20 74 68 65 20 69 6e 74 65 72 6e 61  e of the interna
16e90 6c 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28  l [sqlite3_exec(
16ea0 29 5d 20 63 61 6c 6c 20 61 72 65 20 6e 6f 74 0a  )] call are not.
16eb0 2a 2a 20 72 65 66 6c 65 63 74 65 64 20 69 6e 20  ** reflected in 
16ec0 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73  subsequent calls
16ed0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72 72   to [sqlite3_err
16ee0 63 6f 64 65 28 29 5d 20 6f 72 20 5b 73 71 6c 69  code()] or [sqli
16ef0 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e 0a 2a  te3_errmsg()]..*
16f00 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
16f10 73 3a 0a 2a 2a 20 5b 48 31 32 33 37 31 5d 20 5b  s:.** [H12371] [
16f20 48 31 32 33 37 33 5d 20 5b 48 31 32 33 37 34 5d  H12373] [H12374]
16f30 20 5b 48 31 32 33 37 36 5d 20 5b 48 31 32 33 37   [H12376] [H1237
16f40 39 5d 20 5b 48 31 32 33 38 32 5d 0a 2a 2f 0a 53  9] [H12382].*/.S
16f50 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
16f60 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28  lite3_get_table(
16f70 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
16f80 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f           /* An o
16f90 70 65 6e 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  pen database */.
16fa0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
16fb0 71 6c 2c 20 20 20 20 20 2f 2a 20 53 51 4c 20 74  ql,     /* SQL t
16fc0 6f 20 62 65 20 65 76 61 6c 75 61 74 65 64 20 2a  o be evaluated *
16fd0 2f 0a 20 20 63 68 61 72 20 2a 2a 2a 70 61 7a 52  /.  char ***pazR
16fe0 65 73 75 6c 74 2c 20 20 20 20 2f 2a 20 52 65 73  esult,    /* Res
16ff0 75 6c 74 73 20 6f 66 20 74 68 65 20 71 75 65 72  ults of the quer
17000 79 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 52 6f  y */.  int *pnRo
17010 77 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  w,           /* 
17020 4e 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74  Number of result
17030 20 72 6f 77 73 20 77 72 69 74 74 65 6e 20 68 65   rows written he
17040 72 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43  re */.  int *pnC
17050 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 2f 2a  olumn,        /*
17060 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c   Number of resul
17070 74 20 63 6f 6c 75 6d 6e 73 20 77 72 69 74 74 65  t columns writte
17080 6e 20 68 65 72 65 20 2a 2f 0a 20 20 63 68 61 72  n here */.  char
17090 20 2a 2a 70 7a 45 72 72 6d 73 67 20 20 20 20 20   **pzErrmsg     
170a0 20 20 2f 2a 20 45 72 72 6f 72 20 6d 73 67 20 77    /* Error msg w
170b0 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 29  ritten here */.)
170c0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  ;.SQLITE_API voi
170d0 64 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74  d sqlite3_free_t
170e0 61 62 6c 65 28 63 68 61 72 20 2a 2a 72 65 73 75  able(char **resu
170f0 6c 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  lt);../*.** CAPI
17100 33 52 45 46 3a 20 46 6f 72 6d 61 74 74 65 64 20  3REF: Formatted 
17110 53 74 72 69 6e 67 20 50 72 69 6e 74 69 6e 67 20  String Printing 
17120 46 75 6e 63 74 69 6f 6e 73 20 7b 48 31 37 34 30  Functions {H1740
17130 30 7d 20 3c 53 37 30 30 30 30 3e 3c 53 32 30 30  0} <S70000><S200
17140 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  00>.**.** These 
17150 72 6f 75 74 69 6e 65 73 20 61 72 65 20 77 6f 72  routines are wor
17160 6b 2d 61 6c 69 6b 65 73 20 6f 66 20 74 68 65 20  k-alikes of the 
17170 22 70 72 69 6e 74 66 28 29 22 20 66 61 6d 69 6c  "printf()" famil
17180 79 20 6f 66 20 66 75 6e 63 74 69 6f 6e 73 0a 2a  y of functions.*
17190 2a 20 66 72 6f 6d 20 74 68 65 20 73 74 61 6e 64  * from the stand
171a0 61 72 64 20 43 20 6c 69 62 72 61 72 79 2e 0a 2a  ard C library..*
171b0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
171c0 5f 6d 70 72 69 6e 74 66 28 29 20 61 6e 64 20 73  _mprintf() and s
171d0 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28  qlite3_vmprintf(
171e0 29 20 72 6f 75 74 69 6e 65 73 20 77 72 69 74 65  ) routines write
171f0 20 74 68 65 69 72 0a 2a 2a 20 72 65 73 75 6c 74   their.** result
17200 73 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62  s into memory ob
17210 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c  tained from [sql
17220 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 0a  ite3_malloc()]..
17230 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 73 20 72  ** The strings r
17240 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 73 65  eturned by these
17250 20 74 77 6f 20 72 6f 75 74 69 6e 65 73 20 73 68   two routines sh
17260 6f 75 6c 64 20 62 65 0a 2a 2a 20 72 65 6c 65 61  ould be.** relea
17270 73 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f  sed by [sqlite3_
17280 66 72 65 65 28 29 5d 2e 20 20 42 6f 74 68 20 72  free()].  Both r
17290 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 61  outines return a
172a0 0a 2a 2a 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  .** NULL pointer
172b0 20 69 66 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c   if [sqlite3_mal
172c0 6c 6f 63 28 29 5d 20 69 73 20 75 6e 61 62 6c 65  loc()] is unable
172d0 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 65 6e 6f   to allocate eno
172e0 75 67 68 0a 2a 2a 20 6d 65 6d 6f 72 79 20 74 6f  ugh.** memory to
172f0 20 68 6f 6c 64 20 74 68 65 20 72 65 73 75 6c 74   hold the result
17300 69 6e 67 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a  ing string..**.*
17310 2a 20 49 6e 20 73 71 6c 69 74 65 33 5f 73 6e 70  * In sqlite3_snp
17320 72 69 6e 74 66 28 29 20 72 6f 75 74 69 6e 65 20  rintf() routine 
17330 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 22 73  is similar to "s
17340 6e 70 72 69 6e 74 66 28 29 22 20 66 72 6f 6d 0a  nprintf()" from.
17350 2a 2a 20 74 68 65 20 73 74 61 6e 64 61 72 64 20  ** the standard 
17360 43 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20  C library.  The 
17370 72 65 73 75 6c 74 20 69 73 20 77 72 69 74 74 65  result is writte
17380 6e 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 62 75  n into the.** bu
17390 66 66 65 72 20 73 75 70 70 6c 69 65 64 20 61 73  ffer supplied as
173a0 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61   the second para
173b0 6d 65 74 65 72 20 77 68 6f 73 65 20 73 69 7a 65  meter whose size
173c0 20 69 73 20 67 69 76 65 6e 20 62 79 0a 2a 2a 20   is given by.** 
173d0 74 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65  the first parame
173e0 74 65 72 2e 20 4e 6f 74 65 20 74 68 61 74 20 74  ter. Note that t
173f0 68 65 20 6f 72 64 65 72 20 6f 66 20 74 68 65 0a  he order of the.
17400 2a 2a 20 66 69 72 73 74 20 74 77 6f 20 70 61 72  ** first two par
17410 61 6d 65 74 65 72 73 20 69 73 20 72 65 76 65 72  ameters is rever
17420 73 65 64 20 66 72 6f 6d 20 73 6e 70 72 69 6e 74  sed from snprint
17430 66 28 29 2e 20 20 54 68 69 73 20 69 73 20 61 6e  f().  This is an
17440 0a 2a 2a 20 68 69 73 74 6f 72 69 63 61 6c 20 61  .** historical a
17450 63 63 69 64 65 6e 74 20 74 68 61 74 20 63 61 6e  ccident that can
17460 6e 6f 74 20 62 65 20 66 69 78 65 64 20 77 69 74  not be fixed wit
17470 68 6f 75 74 20 62 72 65 61 6b 69 6e 67 0a 2a 2a  hout breaking.**
17480 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61   backwards compa
17490 74 69 62 69 6c 69 74 79 2e 20 20 4e 6f 74 65 20  tibility.  Note 
174a0 61 6c 73 6f 20 74 68 61 74 20 73 71 6c 69 74 65  also that sqlite
174b0 33 5f 73 6e 70 72 69 6e 74 66 28 29 0a 2a 2a 20  3_snprintf().** 
174c0 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
174d0 72 20 74 6f 20 69 74 73 20 62 75 66 66 65 72 20  r to its buffer 
174e0 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 6e  instead of the n
174f0 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 68 61 72  umber of.** char
17500 61 63 74 65 72 73 20 61 63 74 75 61 6c 6c 79 20  acters actually 
17510 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65  written into the
17520 20 62 75 66 66 65 72 2e 20 20 57 65 20 61 64 6d   buffer.  We adm
17530 69 74 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 6e  it that.** the n
17540 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74  umber of charact
17550 65 72 73 20 77 72 69 74 74 65 6e 20 77 6f 75 6c  ers written woul
17560 64 20 62 65 20 61 20 6d 6f 72 65 20 75 73 65 66  d be a more usef
17570 75 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c  ul return.** val
17580 75 65 20 62 75 74 20 77 65 20 63 61 6e 6e 6f 74  ue but we cannot
17590 20 63 68 61 6e 67 65 20 74 68 65 20 69 6d 70 6c   change the impl
175a0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71  ementation of sq
175b0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29  lite3_snprintf()
175c0 0a 2a 2a 20 6e 6f 77 20 77 69 74 68 6f 75 74 20  .** now without 
175d0 62 72 65 61 6b 69 6e 67 20 63 6f 6d 70 61 74 69  breaking compati
175e0 62 69 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 41 73  bility..**.** As
175f0 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 62 75 66   long as the buf
17600 66 65 72 20 73 69 7a 65 20 69 73 20 67 72 65 61  fer size is grea
17610 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 73  ter than zero, s
17620 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
17630 29 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 73 20  ).** guarantees 
17640 74 68 61 74 20 74 68 65 20 62 75 66 66 65 72 20  that the buffer 
17650 69 73 20 61 6c 77 61 79 73 20 7a 65 72 6f 2d 74  is always zero-t
17660 65 72 6d 69 6e 61 74 65 64 2e 20 20 54 68 65 20  erminated.  The 
17670 66 69 72 73 74 0a 2a 2a 20 70 61 72 61 6d 65 74  first.** paramet
17680 65 72 20 22 6e 22 20 69 73 20 74 68 65 20 74 6f  er "n" is the to
17690 74 61 6c 20 73 69 7a 65 20 6f 66 20 74 68 65 20  tal size of the 
176a0 62 75 66 66 65 72 2c 20 69 6e 63 6c 75 64 69 6e  buffer, includin
176b0 67 20 73 70 61 63 65 20 66 6f 72 0a 2a 2a 20 74  g space for.** t
176c0 68 65 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74  he zero terminat
176d0 6f 72 2e 20 20 53 6f 20 74 68 65 20 6c 6f 6e 67  or.  So the long
176e0 65 73 74 20 73 74 72 69 6e 67 20 74 68 61 74 20  est string that 
176f0 63 61 6e 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c  can be completel
17700 79 0a 2a 2a 20 77 72 69 74 74 65 6e 20 77 69 6c  y.** written wil
17710 6c 20 62 65 20 6e 2d 31 20 63 68 61 72 61 63 74  l be n-1 charact
17720 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  ers..**.** These
17730 20 72 6f 75 74 69 6e 65 73 20 61 6c 6c 20 69 6d   routines all im
17740 70 6c 65 6d 65 6e 74 20 73 6f 6d 65 20 61 64 64  plement some add
17750 69 74 69 6f 6e 61 6c 20 66 6f 72 6d 61 74 74 69  itional formatti
17760 6e 67 0a 2a 2a 20 6f 70 74 69 6f 6e 73 20 74 68  ng.** options th
17770 61 74 20 61 72 65 20 75 73 65 66 75 6c 20 66 6f  at are useful fo
17780 72 20 63 6f 6e 73 74 72 75 63 74 69 6e 67 20 53  r constructing S
17790 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a  QL statements..*
177a0 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 75 73 75  * All of the usu
177b0 61 6c 20 70 72 69 6e 74 66 28 29 20 66 6f 72 6d  al printf() form
177c0 61 74 74 69 6e 67 20 6f 70 74 69 6f 6e 73 20 61  atting options a
177d0 70 70 6c 79 2e 20 20 49 6e 20 61 64 64 69 74 69  pply.  In additi
177e0 6f 6e 2c 20 74 68 65 72 65 0a 2a 2a 20 69 73 20  on, there.** is 
177f0 61 72 65 20 22 25 71 22 2c 20 22 25 51 22 2c 20  are "%q", "%Q", 
17800 61 6e 64 20 22 25 7a 22 20 6f 70 74 69 6f 6e 73  and "%z" options
17810 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 25 71 20 6f  ..**.** The %q o
17820 70 74 69 6f 6e 20 77 6f 72 6b 73 20 6c 69 6b 65  ption works like
17830 20 25 73 20 69 6e 20 74 68 61 74 20 69 74 20 73   %s in that it s
17840 75 62 73 74 69 74 75 74 65 73 20 61 20 6e 75 6c  ubstitutes a nul
17850 6c 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20  l-terminated.** 
17860 73 74 72 69 6e 67 20 66 72 6f 6d 20 74 68 65 20  string from the 
17870 61 72 67 75 6d 65 6e 74 20 6c 69 73 74 2e 20 20  argument list.  
17880 42 75 74 20 25 71 20 61 6c 73 6f 20 64 6f 75 62  But %q also doub
17890 6c 65 73 20 65 76 65 72 79 20 27 5c 27 27 20 63  les every '\'' c
178a0 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 25 71 20  haracter..** %q 
178b0 69 73 20 64 65 73 69 67 6e 65 64 20 66 6f 72 20  is designed for 
178c0 75 73 65 20 69 6e 73 69 64 65 20 61 20 73 74 72  use inside a str
178d0 69 6e 67 20 6c 69 74 65 72 61 6c 2e 20 20 42 79  ing literal.  By
178e0 20 64 6f 75 62 6c 69 6e 67 20 65 61 63 68 20 27   doubling each '
178f0 5c 27 27 0a 2a 2a 20 63 68 61 72 61 63 74 65 72  \''.** character
17900 20 69 74 20 65 73 63 61 70 65 73 20 74 68 61 74   it escapes that
17910 20 63 68 61 72 61 63 74 65 72 20 61 6e 64 20 61   character and a
17920 6c 6c 6f 77 73 20 69 74 20 74 6f 20 62 65 20 69  llows it to be i
17930 6e 73 65 72 74 65 64 20 69 6e 74 6f 0a 2a 2a 20  nserted into.** 
17940 74 68 65 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a  the string..**.*
17950 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 61  * For example, a
17960 73 73 75 6d 65 20 74 68 65 20 73 74 72 69 6e 67  ssume the string
17970 20 76 61 72 69 61 62 6c 65 20 7a 54 65 78 74 20   variable zText 
17980 63 6f 6e 74 61 69 6e 73 20 74 65 78 74 20 61 73  contains text as
17990 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20   follows:.**.** 
179a0 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65  <blockquote><pre
179b0 3e 0a 2a 2a 20 20 63 68 61 72 20 2a 7a 54 65 78  >.**  char *zTex
179c0 74 20 3d 20 22 49 74 27 73 20 61 20 68 61 70 70  t = "It's a happ
179d0 79 20 64 61 79 21 22 3b 0a 2a 2a 20 3c 2f 70 72  y day!";.** </pr
179e0 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a  e></blockquote>.
179f0 2a 2a 0a 2a 2a 20 4f 6e 65 20 63 61 6e 20 75 73  **.** One can us
17a00 65 20 74 68 69 73 20 74 65 78 74 20 69 6e 20 61  e this text in a
17a10 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  n SQL statement 
17a20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
17a30 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70  * <blockquote><p
17a40 72 65 3e 0a 2a 2a 20 20 63 68 61 72 20 2a 7a 53  re>.**  char *zS
17a50 51 4c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  QL = sqlite3_mpr
17a60 69 6e 74 66 28 22 49 4e 53 45 52 54 20 49 4e 54  intf("INSERT INT
17a70 4f 20 74 61 62 6c 65 20 56 41 4c 55 45 53 28 27  O table VALUES('
17a80 25 71 27 29 22 2c 20 7a 54 65 78 74 29 3b 0a 2a  %q')", zText);.*
17a90 2a 20 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28  *  sqlite3_exec(
17aa0 64 62 2c 20 7a 53 51 4c 2c 20 30 2c 20 30 2c 20  db, zSQL, 0, 0, 
17ab0 30 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f  0);.**  sqlite3_
17ac0 66 72 65 65 28 7a 53 51 4c 29 3b 0a 2a 2a 20 3c  free(zSQL);.** <
17ad0 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74  /pre></blockquot
17ae0 65 3e 0a 2a 2a 0a 2a 2a 20 42 65 63 61 75 73 65  e>.**.** Because
17af0 20 74 68 65 20 25 71 20 66 6f 72 6d 61 74 20 73   the %q format s
17b00 74 72 69 6e 67 20 69 73 20 75 73 65 64 2c 20 74  tring is used, t
17b10 68 65 20 27 5c 27 27 20 63 68 61 72 61 63 74 65  he '\'' characte
17b20 72 20 69 6e 20 7a 54 65 78 74 0a 2a 2a 20 69 73  r in zText.** is
17b30 20 65 73 63 61 70 65 64 20 61 6e 64 20 74 68 65   escaped and the
17b40 20 53 51 4c 20 67 65 6e 65 72 61 74 65 64 20 69   SQL generated i
17b50 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  s as follows:.**
17b60 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e  .** <blockquote>
17b70 3c 70 72 65 3e 0a 2a 2a 20 20 49 4e 53 45 52 54  <pre>.**  INSERT
17b80 20 49 4e 54 4f 20 74 61 62 6c 65 31 20 56 41 4c   INTO table1 VAL
17b90 55 45 53 28 27 49 74 27 27 73 20 61 20 68 61 70  UES('It''s a hap
17ba0 70 79 20 64 61 79 21 27 29 0a 2a 2a 20 3c 2f 70  py day!').** </p
17bb0 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e  re></blockquote>
17bc0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 63  .**.** This is c
17bd0 6f 72 72 65 63 74 2e 20 20 48 61 64 20 77 65 20  orrect.  Had we 
17be0 75 73 65 64 20 25 73 20 69 6e 73 74 65 61 64 20  used %s instead 
17bf0 6f 66 20 25 71 2c 20 74 68 65 20 67 65 6e 65 72  of %q, the gener
17c00 61 74 65 64 20 53 51 4c 0a 2a 2a 20 77 6f 75 6c  ated SQL.** woul
17c10 64 20 68 61 76 65 20 6c 6f 6f 6b 65 64 20 6c 69  d have looked li
17c20 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c  ke this:.**.** <
17c30 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e  blockquote><pre>
17c40 0a 2a 2a 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .**  INSERT INTO
17c50 20 74 61 62 6c 65 31 20 56 41 4c 55 45 53 28 27   table1 VALUES('
17c60 49 74 27 73 20 61 20 68 61 70 70 79 20 64 61 79  It's a happy day
17c70 21 27 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f  !');.** </pre></
17c80 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a  blockquote>.**.*
17c90 2a 20 54 68 69 73 20 73 65 63 6f 6e 64 20 65 78  * This second ex
17ca0 61 6d 70 6c 65 20 69 73 20 61 6e 20 53 51 4c 20  ample is an SQL 
17cb0 73 79 6e 74 61 78 20 65 72 72 6f 72 2e 20 20 41  syntax error.  A
17cc0 73 20 61 20 67 65 6e 65 72 61 6c 20 72 75 6c 65  s a general rule
17cd0 20 79 6f 75 20 73 68 6f 75 6c 64 0a 2a 2a 20 61   you should.** a
17ce0 6c 77 61 79 73 20 75 73 65 20 25 71 20 69 6e 73  lways use %q ins
17cf0 74 65 61 64 20 6f 66 20 25 73 20 77 68 65 6e 20  tead of %s when 
17d00 69 6e 73 65 72 74 69 6e 67 20 74 65 78 74 20 69  inserting text i
17d10 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 6c 69 74  nto a string lit
17d20 65 72 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  eral..**.** The 
17d30 25 51 20 6f 70 74 69 6f 6e 20 77 6f 72 6b 73 20  %Q option works 
17d40 6c 69 6b 65 20 25 71 20 65 78 63 65 70 74 20 69  like %q except i
17d50 74 20 61 6c 73 6f 20 61 64 64 73 20 73 69 6e 67  t also adds sing
17d60 6c 65 20 71 75 6f 74 65 73 20 61 72 6f 75 6e 64  le quotes around
17d70 0a 2a 2a 20 74 68 65 20 6f 75 74 73 69 64 65 20  .** the outside 
17d80 6f 66 20 74 68 65 20 74 6f 74 61 6c 20 73 74 72  of the total str
17d90 69 6e 67 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c  ing.  Additional
17da0 6c 79 2c 20 69 66 20 74 68 65 20 70 61 72 61 6d  ly, if the param
17db0 65 74 65 72 20 69 6e 20 74 68 65 0a 2a 2a 20 61  eter in the.** a
17dc0 72 67 75 6d 65 6e 74 20 6c 69 73 74 20 69 73 20  rgument list is 
17dd0 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20  a NULL pointer, 
17de0 25 51 20 73 75 62 73 74 69 74 75 74 65 73 20 74  %Q substitutes t
17df0 68 65 20 74 65 78 74 20 22 4e 55 4c 4c 22 20 28  he text "NULL" (
17e00 77 69 74 68 6f 75 74 0a 2a 2a 20 73 69 6e 67 6c  without.** singl
17e10 65 20 71 75 6f 74 65 73 29 20 69 6e 20 70 6c 61  e quotes) in pla
17e20 63 65 20 6f 66 20 74 68 65 20 25 51 20 6f 70 74  ce of the %Q opt
17e30 69 6f 6e 2e 20 20 53 6f 2c 20 66 6f 72 20 65 78  ion.  So, for ex
17e40 61 6d 70 6c 65 2c 20 6f 6e 65 20 63 6f 75 6c 64  ample, one could
17e50 20 73 61 79 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f   say:.**.** <blo
17e60 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a  ckquote><pre>.**
17e70 20 20 63 68 61 72 20 2a 7a 53 51 4c 20 3d 20 73    char *zSQL = s
17e80 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
17e90 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c  INSERT INTO tabl
17ea0 65 20 56 41 4c 55 45 53 28 25 51 29 22 2c 20 7a  e VALUES(%Q)", z
17eb0 54 65 78 74 29 3b 0a 2a 2a 20 20 73 71 6c 69 74  Text);.**  sqlit
17ec0 65 33 5f 65 78 65 63 28 64 62 2c 20 7a 53 51 4c  e3_exec(db, zSQL
17ed0 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 2a 2a 20 20  , 0, 0, 0);.**  
17ee0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 51  sqlite3_free(zSQ
17ef0 4c 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62  L);.** </pre></b
17f00 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a  lockquote>.**.**
17f10 20 54 68 65 20 63 6f 64 65 20 61 62 6f 76 65 20   The code above 
17f20 77 69 6c 6c 20 72 65 6e 64 65 72 20 61 20 63 6f  will render a co
17f30 72 72 65 63 74 20 53 51 4c 20 73 74 61 74 65 6d  rrect SQL statem
17f40 65 6e 74 20 69 6e 20 74 68 65 20 7a 53 51 4c 0a  ent in the zSQL.
17f50 2a 2a 20 76 61 72 69 61 62 6c 65 20 65 76 65 6e  ** variable even
17f60 20 69 66 20 74 68 65 20 7a 54 65 78 74 20 76 61   if the zText va
17f70 72 69 61 62 6c 65 20 69 73 20 61 20 4e 55 4c 4c  riable is a NULL
17f80 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20   pointer..**.** 
17f90 54 68 65 20 22 25 7a 22 20 66 6f 72 6d 61 74 74  The "%z" formatt
17fa0 69 6e 67 20 6f 70 74 69 6f 6e 20 77 6f 72 6b 73  ing option works
17fb0 20 65 78 61 63 74 6c 79 20 6c 69 6b 65 20 22 25   exactly like "%
17fc0 73 22 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 61  s" with the.** a
17fd0 64 64 69 74 69 6f 6e 20 74 68 61 74 20 61 66 74  ddition that aft
17fe0 65 72 20 74 68 65 20 73 74 72 69 6e 67 20 68 61  er the string ha
17ff0 73 20 62 65 65 6e 20 72 65 61 64 20 61 6e 64 20  s been read and 
18000 63 6f 70 69 65 64 20 69 6e 74 6f 0a 2a 2a 20 74  copied into.** t
18010 68 65 20 72 65 73 75 6c 74 2c 20 5b 73 71 6c 69  he result, [sqli
18020 74 65 33 5f 66 72 65 65 28 29 5d 20 69 73 20 63  te3_free()] is c
18030 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 69 6e 70  alled on the inp
18040 75 74 20 73 74 72 69 6e 67 2e 20 7b 45 4e 44 7d  ut string. {END}
18050 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
18060 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 34 30 33 5d  nts:.** [H17403]
18070 20 5b 48 31 37 34 30 36 5d 20 5b 48 31 37 34 30   [H17406] [H1740
18080 37 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  7].*/.SQLITE_API
18090 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6d   char *sqlite3_m
180a0 70 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61  printf(const cha
180b0 72 2a 2c 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f  r*,...);.SQLITE_
180c0 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65  API char *sqlite
180d0 33 5f 76 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74  3_vmprintf(const
180e0 20 63 68 61 72 2a 2c 20 76 61 5f 6c 69 73 74 29   char*, va_list)
180f0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61  ;.SQLITE_API cha
18100 72 20 2a 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  r *sqlite3_snpri
18110 6e 74 66 28 69 6e 74 2c 63 68 61 72 2a 2c 63 6f  ntf(int,char*,co
18120 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b  nst char*, ...);
18130 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
18140 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74  : Memory Allocat
18150 69 6f 6e 20 53 75 62 73 79 73 74 65 6d 20 7b 48  ion Subsystem {H
18160 31 37 33 30 30 7d 20 3c 53 32 30 30 30 30 3e 0a  17300} <S20000>.
18170 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 65  **.** The SQLite
18180 20 63 6f 72 65 20 20 75 73 65 73 20 74 68 65 73   core  uses thes
18190 65 20 74 68 72 65 65 20 72 6f 75 74 69 6e 65 73  e three routines
181a0 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 20   for all of its 
181b0 6f 77 6e 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c 20  own.** internal 
181c0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
181d0 6e 20 6e 65 65 64 73 2e 20 22 43 6f 72 65 22 20  n needs. "Core" 
181e0 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  in the previous 
181f0 73 65 6e 74 65 6e 63 65 0a 2a 2a 20 64 6f 65 73  sentence.** does
18200 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 6f 70 65   not include ope
18210 72 61 74 69 6e 67 2d 73 79 73 74 65 6d 20 73 70  rating-system sp
18220 65 63 69 66 69 63 20 56 46 53 20 69 6d 70 6c 65  ecific VFS imple
18230 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68 65 0a  mentation.  The.
18240 2a 2a 20 57 69 6e 64 6f 77 73 20 56 46 53 20 75  ** Windows VFS u
18250 73 65 73 20 6e 61 74 69 76 65 20 6d 61 6c 6c 6f  ses native mallo
18260 63 28 29 20 61 6e 64 20 66 72 65 65 28 29 20 66  c() and free() f
18270 6f 72 20 73 6f 6d 65 20 6f 70 65 72 61 74 69 6f  or some operatio
18280 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  ns..**.** The sq
18290 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 72  lite3_malloc() r
182a0 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61  outine returns a
182b0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 62 6c   pointer to a bl
182c0 6f 63 6b 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72 79  ock.** of memory
182d0 20 61 74 20 6c 65 61 73 74 20 4e 20 62 79 74 65   at least N byte
182e0 73 20 69 6e 20 6c 65 6e 67 74 68 2c 20 77 68 65  s in length, whe
182f0 72 65 20 4e 20 69 73 20 74 68 65 20 70 61 72 61  re N is the para
18300 6d 65 74 65 72 2e 0a 2a 2a 20 49 66 20 73 71 6c  meter..** If sql
18310 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 69 73  ite3_malloc() is
18320 20 75 6e 61 62 6c 65 20 74 6f 20 6f 62 74 61 69   unable to obtai
18330 6e 20 73 75 66 66 69 63 69 65 6e 74 20 66 72 65  n sufficient fre
18340 65 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 69 74 20  e.** memory, it 
18350 72 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70  returns a NULL p
18360 6f 69 6e 74 65 72 2e 20 20 49 66 20 74 68 65 20  ointer.  If the 
18370 70 61 72 61 6d 65 74 65 72 20 4e 20 74 6f 0a 2a  parameter N to.*
18380 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  * sqlite3_malloc
18390 28 29 20 69 73 20 7a 65 72 6f 20 6f 72 20 6e 65  () is zero or ne
183a0 67 61 74 69 76 65 20 74 68 65 6e 20 73 71 6c 69  gative then sqli
183b0 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 72 65 74  te3_malloc() ret
183c0 75 72 6e 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70  urns.** a NULL p
183d0 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 61  ointer..**.** Ca
183e0 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 72  lling sqlite3_fr
183f0 65 65 28 29 20 77 69 74 68 20 61 20 70 6f 69 6e  ee() with a poin
18400 74 65 72 20 70 72 65 76 69 6f 75 73 6c 79 20 72  ter previously r
18410 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79 20 73 71  eturned.** by sq
18420 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 6f  lite3_malloc() o
18430 72 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f  r sqlite3_reallo
18440 63 28 29 20 72 65 6c 65 61 73 65 73 20 74 68 61  c() releases tha
18450 74 20 6d 65 6d 6f 72 79 20 73 6f 0a 2a 2a 20 74  t memory so.** t
18460 68 61 74 20 69 74 20 6d 69 67 68 74 20 62 65 20  hat it might be 
18470 72 65 75 73 65 64 2e 20 20 54 68 65 20 73 71 6c  reused.  The sql
18480 69 74 65 33 5f 66 72 65 65 28 29 20 72 6f 75 74  ite3_free() rout
18490 69 6e 65 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f  ine is.** a no-o
184a0 70 20 69 66 20 69 73 20 63 61 6c 6c 65 64 20 77  p if is called w
184b0 69 74 68 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  ith a NULL point
184c0 65 72 2e 20 20 50 61 73 73 69 6e 67 20 61 20 4e  er.  Passing a N
184d0 55 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74  ULL pointer.** t
184e0 6f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29  o sqlite3_free()
184f0 20 69 73 20 68 61 72 6d 6c 65 73 73 2e 20 20 41   is harmless.  A
18500 66 74 65 72 20 62 65 69 6e 67 20 66 72 65 65 64  fter being freed
18510 2c 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 68 6f 75  , memory.** shou
18520 6c 64 20 6e 65 69 74 68 65 72 20 62 65 20 72 65  ld neither be re
18530 61 64 20 6e 6f 72 20 77 72 69 74 74 65 6e 2e 20  ad nor written. 
18540 20 45 76 65 6e 20 72 65 61 64 69 6e 67 20 70 72   Even reading pr
18550 65 76 69 6f 75 73 6c 79 20 66 72 65 65 64 0a 2a  eviously freed.*
18560 2a 20 6d 65 6d 6f 72 79 20 6d 69 67 68 74 20 72  * memory might r
18570 65 73 75 6c 74 20 69 6e 20 61 20 73 65 67 6d 65  esult in a segme
18580 6e 74 61 74 69 6f 6e 20 66 61 75 6c 74 20 6f 72  ntation fault or
18590 20 6f 74 68 65 72 20 73 65 76 65 72 65 20 65 72   other severe er
185a0 72 6f 72 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 63  ror..** Memory c
185b0 6f 72 72 75 70 74 69 6f 6e 2c 20 61 20 73 65 67  orruption, a seg
185c0 6d 65 6e 74 61 74 69 6f 6e 20 66 61 75 6c 74 2c  mentation fault,
185d0 20 6f 72 20 6f 74 68 65 72 20 73 65 76 65 72 65   or other severe
185e0 20 65 72 72 6f 72 0a 2a 2a 20 6d 69 67 68 74 20   error.** might 
185f0 72 65 73 75 6c 74 20 69 66 20 73 71 6c 69 74 65  result if sqlite
18600 33 5f 66 72 65 65 28 29 20 69 73 20 63 61 6c 6c  3_free() is call
18610 65 64 20 77 69 74 68 20 61 20 6e 6f 6e 2d 4e 55  ed with a non-NU
18620 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 61 74 0a  LL pointer that.
18630 2a 2a 20 77 61 73 20 6e 6f 74 20 6f 62 74 61 69  ** was not obtai
18640 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
18650 5f 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 73 71 6c  _malloc() or sql
18660 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a  ite3_realloc()..
18670 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
18680 33 5f 72 65 61 6c 6c 6f 63 28 29 20 69 6e 74 65  3_realloc() inte
18690 72 66 61 63 65 20 61 74 74 65 6d 70 74 73 20 74  rface attempts t
186a0 6f 20 72 65 73 69 7a 65 20 61 0a 2a 2a 20 70 72  o resize a.** pr
186b0 69 6f 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  ior memory alloc
186c0 61 74 69 6f 6e 20 74 6f 20 62 65 20 61 74 20 6c  ation to be at l
186d0 65 61 73 74 20 4e 20 62 79 74 65 73 2c 20 77 68  east N bytes, wh
186e0 65 72 65 20 4e 20 69 73 20 74 68 65 0a 2a 2a 20  ere N is the.** 
186f0 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72  second parameter
18700 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79 20 61 6c  .  The memory al
18710 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 62 65 20 72  location to be r
18720 65 73 69 7a 65 64 20 69 73 20 74 68 65 20 66 69  esized is the fi
18730 72 73 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  rst.** parameter
18740 2e 20 20 49 66 20 74 68 65 20 66 69 72 73 74 20  .  If the first 
18750 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c  parameter to sql
18760 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 0a 2a  ite3_realloc().*
18770 2a 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  * is a NULL poin
18780 74 65 72 20 74 68 65 6e 20 69 74 73 20 62 65 68  ter then its beh
18790 61 76 69 6f 72 20 69 73 20 69 64 65 6e 74 69 63  avior is identic
187a0 61 6c 20 74 6f 20 63 61 6c 6c 69 6e 67 0a 2a 2a  al to calling.**
187b0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
187c0 4e 29 20 77 68 65 72 65 20 4e 20 69 73 20 74 68  N) where N is th
187d0 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74  e second paramet
187e0 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65  er to sqlite3_re
187f0 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 49 66 20 74  alloc()..** If t
18800 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65  he second parame
18810 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72  ter to sqlite3_r
18820 65 61 6c 6c 6f 63 28 29 20 69 73 20 7a 65 72 6f  ealloc() is zero
18830 20 6f 72 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20   or.** negative 
18840 74 68 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f  then the behavio
18850 72 20 69 73 20 65 78 61 63 74 6c 79 20 74 68 65  r is exactly the
18860 20 73 61 6d 65 20 61 73 20 63 61 6c 6c 69 6e 67   same as calling
18870 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 72 65 65  .** sqlite3_free
18880 28 50 29 20 77 68 65 72 65 20 50 20 69 73 20 74  (P) where P is t
18890 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74  he first paramet
188a0 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65  er to sqlite3_re
188b0 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 73 71 6c 69  alloc()..** sqli
188c0 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 72 65  te3_realloc() re
188d0 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  turns a pointer 
188e0 74 6f 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  to a memory allo
188f0 63 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 61 74 20  cation.** of at 
18900 6c 65 61 73 74 20 4e 20 62 79 74 65 73 20 69 6e  least N bytes in
18910 20 73 69 7a 65 20 6f 72 20 4e 55 4c 4c 20 69 66   size or NULL if
18920 20 73 75 66 66 69 63 69 65 6e 74 20 6d 65 6d 6f   sufficient memo
18930 72 79 20 69 73 20 75 6e 61 76 61 69 6c 61 62 6c  ry is unavailabl
18940 65 2e 0a 2a 2a 20 49 66 20 4d 20 69 73 20 74 68  e..** If M is th
18950 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 70 72  e size of the pr
18960 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20  ior allocation, 
18970 74 68 65 6e 20 6d 69 6e 28 4e 2c 4d 29 20 62 79  then min(N,M) by
18980 74 65 73 0a 2a 2a 20 6f 66 20 74 68 65 20 70 72  tes.** of the pr
18990 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61  ior allocation a
189a0 72 65 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74  re copied into t
189b0 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
189c0 62 75 66 66 65 72 20 72 65 74 75 72 6e 65 64 0a  buffer returned.
189d0 2a 2a 20 62 79 20 73 71 6c 69 74 65 33 5f 72 65  ** by sqlite3_re
189e0 61 6c 6c 6f 63 28 29 20 61 6e 64 20 74 68 65 20  alloc() and the 
189f0 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e  prior allocation
18a00 20 69 73 20 66 72 65 65 64 2e 0a 2a 2a 20 49 66   is freed..** If
18a10 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
18a20 28 29 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2c  () returns NULL,
18a30 20 74 68 65 6e 20 74 68 65 20 70 72 69 6f 72 20   then the prior 
18a40 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 69 73  allocation.** is
18a50 20 6e 6f 74 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a   not freed..**.*
18a60 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20 72 65 74  * The memory ret
18a70 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33  urned by sqlite3
18a80 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 73 71  _malloc() and sq
18a90 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 0a  lite3_realloc().
18aa0 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 61 6c 69  ** is always ali
18ab0 67 6e 65 64 20 74 6f 20 61 74 20 6c 65 61 73 74  gned to at least
18ac0 20 61 6e 20 38 20 62 79 74 65 20 62 6f 75 6e 64   an 8 byte bound
18ad0 61 72 79 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a  ary. {END}.**.**
18ae0 20 54 68 65 20 64 65 66 61 75 6c 74 20 69 6d 70   The default imp
18af0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
18b00 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
18b10 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 75  tion subsystem u
18b20 73 65 73 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c 6f  ses.** the mallo
18b30 63 28 29 2c 20 72 65 61 6c 6c 6f 63 28 29 20 61  c(), realloc() a
18b40 6e 64 20 66 72 65 65 28 29 20 70 72 6f 76 69 64  nd free() provid
18b50 65 64 20 62 79 20 74 68 65 20 73 74 61 6e 64 61  ed by the standa
18b60 72 64 20 43 20 6c 69 62 72 61 72 79 2e 0a 2a 2a  rd C library..**
18b70 20 7b 48 31 37 33 38 32 7d 20 48 6f 77 65 76 65   {H17382} Howeve
18b80 72 2c 20 69 66 20 53 51 4c 69 74 65 20 69 73 20  r, if SQLite is 
18b90 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68  compiled with th
18ba0 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 4f  e.** SQLITE_MEMO
18bb0 52 59 5f 53 49 5a 45 3d 3c 69 3e 4e 4e 4e 3c 2f  RY_SIZE=<i>NNN</
18bc0 69 3e 20 43 20 70 72 65 70 72 6f 63 65 73 73 6f  i> C preprocesso
18bd0 72 20 6d 61 63 72 6f 20 28 77 68 65 72 65 20 3c  r macro (where <
18be0 69 3e 4e 4e 4e 3c 2f 69 3e 0a 2a 2a 20 69 73 20  i>NNN</i>.** is 
18bf0 61 6e 20 69 6e 74 65 67 65 72 29 2c 20 74 68 65  an integer), the
18c00 6e 20 53 51 4c 69 74 65 20 63 72 65 61 74 65 20  n SQLite create 
18c10 61 20 73 74 61 74 69 63 20 61 72 72 61 79 20 6f  a static array o
18c20 66 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 3c 69  f at least.** <i
18c30 3e 4e 4e 4e 3c 2f 69 3e 20 62 79 74 65 73 20 69  >NNN</i> bytes i
18c40 6e 20 73 69 7a 65 20 61 6e 64 20 75 73 65 73 20  n size and uses 
18c50 74 68 61 74 20 61 72 72 61 79 20 66 6f 72 20 61  that array for a
18c60 6c 6c 20 6f 66 20 69 74 73 20 64 79 6e 61 6d 69  ll of its dynami
18c70 63 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  c.** memory allo
18c80 63 61 74 69 6f 6e 20 6e 65 65 64 73 2e 20 7b 45  cation needs. {E
18c90 4e 44 7d 20 20 41 64 64 69 74 69 6f 6e 61 6c 20  ND}  Additional 
18ca0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72  memory allocator
18cb0 20 6f 70 74 69 6f 6e 73 0a 2a 2a 20 6d 61 79 20   options.** may 
18cc0 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 75  be added in futu
18cd0 72 65 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a  re releases..**.
18ce0 2a 2a 20 49 6e 20 53 51 4c 69 74 65 20 76 65 72  ** In SQLite ver
18cf0 73 69 6f 6e 20 33 2e 35 2e 30 20 61 6e 64 20 33  sion 3.5.0 and 3
18d00 2e 35 2e 31 2c 20 69 74 20 77 61 73 20 70 6f 73  .5.1, it was pos
18d10 73 69 62 6c 65 20 74 6f 20 64 65 66 69 6e 65 0a  sible to define.
18d20 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f 4f 4d  ** the SQLITE_OM
18d30 49 54 5f 4d 45 4d 4f 52 59 5f 41 4c 4c 4f 43 41  IT_MEMORY_ALLOCA
18d40 54 49 4f 4e 20 77 68 69 63 68 20 77 6f 75 6c 64  TION which would
18d50 20 63 61 75 73 65 20 74 68 65 20 62 75 69 6c 74   cause the built
18d60 2d 69 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  -in.** implement
18d70 61 74 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 72  ation of these r
18d80 6f 75 74 69 6e 65 73 20 74 6f 20 62 65 20 6f 6d  outines to be om
18d90 69 74 74 65 64 2e 20 20 54 68 61 74 20 63 61 70  itted.  That cap
18da0 61 62 69 6c 69 74 79 0a 2a 2a 20 69 73 20 6e 6f  ability.** is no
18db0 20 6c 6f 6e 67 65 72 20 70 72 6f 76 69 64 65 64   longer provided
18dc0 2e 20 20 4f 6e 6c 79 20 62 75 69 6c 74 2d 69 6e  .  Only built-in
18dd0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f   memory allocato
18de0 72 73 20 63 61 6e 20 62 65 20 75 73 65 64 2e 0a  rs can be used..
18df0 2a 2a 0a 2a 2a 20 54 68 65 20 57 69 6e 64 6f 77  **.** The Window
18e00 73 20 4f 53 20 69 6e 74 65 72 66 61 63 65 20 6c  s OS interface l
18e10 61 79 65 72 20 63 61 6c 6c 73 0a 2a 2a 20 74 68  ayer calls.** th
18e20 65 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28  e system malloc(
18e30 29 20 61 6e 64 20 66 72 65 65 28 29 20 64 69 72  ) and free() dir
18e40 65 63 74 6c 79 20 77 68 65 6e 20 63 6f 6e 76 65  ectly when conve
18e50 72 74 69 6e 67 0a 2a 2a 20 66 69 6c 65 6e 61 6d  rting.** filenam
18e60 65 73 20 62 65 74 77 65 65 6e 20 74 68 65 20 55  es between the U
18e70 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 75 73  TF-8 encoding us
18e80 65 64 20 62 79 20 53 51 4c 69 74 65 0a 2a 2a 20  ed by SQLite.** 
18e90 61 6e 64 20 77 68 61 74 65 76 65 72 20 66 69 6c  and whatever fil
18ea0 65 6e 61 6d 65 20 65 6e 63 6f 64 69 6e 67 20 69  ename encoding i
18eb0 73 20 75 73 65 64 20 62 79 20 74 68 65 20 70 61  s used by the pa
18ec0 72 74 69 63 75 6c 61 72 20 57 69 6e 64 6f 77 73  rticular Windows
18ed0 0a 2a 2a 20 69 6e 73 74 61 6c 6c 61 74 69 6f 6e  .** installation
18ee0 2e 20 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  .  Memory alloca
18ef0 74 69 6f 6e 20 65 72 72 6f 72 73 20 61 72 65 20  tion errors are 
18f00 64 65 74 65 63 74 65 64 2c 20 62 75 74 0a 2a 2a  detected, but.**
18f10 20 74 68 65 79 20 61 72 65 20 72 65 70 6f 72 74   they are report
18f20 65 64 20 62 61 63 6b 20 61 73 20 5b 53 51 4c 49  ed back as [SQLI
18f30 54 45 5f 43 41 4e 54 4f 50 45 4e 5d 20 6f 72 0a  TE_CANTOPEN] or.
18f40 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52  ** [SQLITE_IOERR
18f50 5d 20 72 61 74 68 65 72 20 74 68 61 6e 20 5b 53  ] rather than [S
18f60 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2e 0a 2a 2a  QLITE_NOMEM]..**
18f70 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
18f80 3a 0a 2a 2a 20 5b 48 31 37 33 30 33 5d 20 5b 48  :.** [H17303] [H
18f90 31 37 33 30 34 5d 20 5b 48 31 37 33 30 35 5d 20  17304] [H17305] 
18fa0 5b 48 31 37 33 30 36 5d 20 5b 48 31 37 33 31 30  [H17306] [H17310
18fb0 5d 20 5b 48 31 37 33 31 32 5d 20 5b 48 31 37 33  ] [H17312] [H173
18fc0 31 35 5d 20 5b 48 31 37 33 31 38 5d 0a 2a 2a 20  15] [H17318].** 
18fd0 5b 48 31 37 33 32 31 5d 20 5b 48 31 37 33 32 32  [H17321] [H17322
18fe0 5d 20 5b 48 31 37 33 32 33 5d 0a 2a 2a 0a 2a 2a  ] [H17323].**.**
18ff0 20 54 68 65 20 70 6f 69 6e 74 65 72 20 61 72 67   The pointer arg
19000 75 6d 65 6e 74 73 20 74 6f 20 5b 73 71 6c 69 74  uments to [sqlit
19010 65 33 5f 66 72 65 65 28 29 5d 20 61 6e 64 20 5b  e3_free()] and [
19020 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
19030 29 5d 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69  )].** must be ei
19040 74 68 65 72 20 4e 55 4c 4c 20 6f 72 20 65 6c 73  ther NULL or els
19050 65 20 70 6f 69 6e 74 65 72 73 20 6f 62 74 61 69  e pointers obtai
19060 6e 65 64 20 66 72 6f 6d 20 61 20 70 72 69 6f 72  ned from a prior
19070 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f  .** invocation o
19080 66 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  f [sqlite3_mallo
19090 63 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33  c()] or [sqlite3
190a0 5f 72 65 61 6c 6c 6f 63 28 29 5d 20 74 68 61 74  _realloc()] that
190b0 20 68 61 76 65 0a 2a 2a 20 6e 6f 74 20 79 65 74   have.** not yet
190c0 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64 2e 0a   been released..
190d0 2a 2a 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63  **.** The applic
190e0 61 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 72  ation must not r
190f0 65 61 64 20 6f 72 20 77 72 69 74 65 20 61 6e 79  ead or write any
19100 20 70 61 72 74 20 6f 66 0a 2a 2a 20 61 20 62 6c   part of.** a bl
19110 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 61 66  ock of memory af
19120 74 65 72 20 69 74 20 68 61 73 20 62 65 65 6e 20  ter it has been 
19130 72 65 6c 65 61 73 65 64 20 75 73 69 6e 67 0a 2a  released using.*
19140 2a 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28  * [sqlite3_free(
19150 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72  )] or [sqlite3_r
19160 65 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2f 0a 53 51  ealloc()]..*/.SQ
19170 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73  LITE_API void *s
19180 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69 6e  qlite3_malloc(in
19190 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  t);.SQLITE_API v
191a0 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 65 61  oid *sqlite3_rea
191b0 6c 6c 6f 63 28 76 6f 69 64 2a 2c 20 69 6e 74 29  lloc(void*, int)
191c0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  ;.SQLITE_API voi
191d0 64 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 76  d sqlite3_free(v
191e0 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  oid*);../*.** CA
191f0 50 49 33 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41  PI3REF: Memory A
19200 6c 6c 6f 63 61 74 6f 72 20 53 74 61 74 69 73 74  llocator Statist
19210 69 63 73 20 7b 48 31 37 33 37 30 7d 20 3c 53 33  ics {H17370} <S3
19220 30 32 31 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69  0210>.**.** SQLi
19230 74 65 20 70 72 6f 76 69 64 65 73 20 74 68 65 73  te provides thes
19240 65 20 74 77 6f 20 69 6e 74 65 72 66 61 63 65 73  e two interfaces
19250 20 66 6f 72 20 72 65 70 6f 72 74 69 6e 67 20 6f   for reporting o
19260 6e 20 74 68 65 20 73 74 61 74 75 73 0a 2a 2a 20  n the status.** 
19270 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  of the [sqlite3_
19280 6d 61 6c 6c 6f 63 28 29 5d 2c 20 5b 73 71 6c 69  malloc()], [sqli
19290 74 65 33 5f 66 72 65 65 28 29 5d 2c 20 61 6e 64  te3_free()], and
192a0 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f   [sqlite3_reallo
192b0 63 28 29 5d 0a 2a 2a 20 72 6f 75 74 69 6e 65 73  c()].** routines
192c0 2c 20 77 68 69 63 68 20 66 6f 72 6d 20 74 68 65  , which form the
192d0 20 62 75 69 6c 74 2d 69 6e 20 6d 65 6d 6f 72 79   built-in memory
192e0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73   allocation subs
192f0 79 73 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  ystem..**.** Req
19300 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
19310 31 37 33 37 31 5d 20 5b 48 31 37 33 37 33 5d 20  17371] [H17373] 
19320 5b 48 31 37 33 37 34 5d 20 5b 48 31 37 33 37 35  [H17374] [H17375
19330 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
19340 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71  sqlite3_int64 sq
19350 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65  lite3_memory_use
19360 64 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f  d(void);.SQLITE_
19370 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  API sqlite3_int6
19380 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79  4 sqlite3_memory
19390 5f 68 69 67 68 77 61 74 65 72 28 69 6e 74 20 72  _highwater(int r
193a0 65 73 65 74 46 6c 61 67 29 3b 0a 0a 2f 2a 0a 2a  esetFlag);../*.*
193b0 2a 20 43 41 50 49 33 52 45 46 3a 20 50 73 65 75  * CAPI3REF: Pseu
193c0 64 6f 2d 52 61 6e 64 6f 6d 20 4e 75 6d 62 65 72  do-Random Number
193d0 20 47 65 6e 65 72 61 74 6f 72 20 7b 48 31 37 33   Generator {H173
193e0 39 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a  90} <S20000>.**.
193f0 2a 2a 20 53 51 4c 69 74 65 20 63 6f 6e 74 61 69  ** SQLite contai
19400 6e 73 20 61 20 68 69 67 68 2d 71 75 61 6c 69 74  ns a high-qualit
19410 79 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20  y pseudo-random 
19420 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72  number generator
19430 20 28 50 52 4e 47 29 20 75 73 65 64 20 74 6f 0a   (PRNG) used to.
19440 2a 2a 20 73 65 6c 65 63 74 20 72 61 6e 64 6f 6d  ** select random
19450 20 5b 52 4f 57 49 44 20 7c 20 52 4f 57 49 44 73   [ROWID | ROWIDs
19460 5d 20 77 68 65 6e 20 69 6e 73 65 72 74 69 6e 67  ] when inserting
19470 20 6e 65 77 20 72 65 63 6f 72 64 73 20 69 6e 74   new records int
19480 6f 20 61 20 74 61 62 6c 65 20 74 68 61 74 0a 2a  o a table that.*
19490 2a 20 61 6c 72 65 61 64 79 20 75 73 65 73 20 74  * already uses t
194a0 68 65 20 6c 61 72 67 65 73 74 20 70 6f 73 73 69  he largest possi
194b0 62 6c 65 20 5b 52 4f 57 49 44 5d 2e 20 20 54 68  ble [ROWID].  Th
194c0 65 20 50 52 4e 47 20 69 73 20 61 6c 73 6f 20 75  e PRNG is also u
194d0 73 65 64 20 66 6f 72 0a 2a 2a 20 74 68 65 20 62  sed for.** the b
194e0 75 69 6c 64 2d 69 6e 20 72 61 6e 64 6f 6d 28 29  uild-in random()
194f0 20 61 6e 64 20 72 61 6e 64 6f 6d 62 6c 6f 62 28   and randomblob(
19500 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 2e  ) SQL functions.
19510 20 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65    This interface
19520 20 61 6c 6c 6f 77 73 0a 2a 2a 20 61 70 70 6c 69   allows.** appli
19530 63 61 74 69 6f 6e 73 20 74 6f 20 61 63 63 65 73  cations to acces
19540 73 20 74 68 65 20 73 61 6d 65 20 50 52 4e 47 20  s the same PRNG 
19550 66 6f 72 20 6f 74 68 65 72 20 70 75 72 70 6f 73  for other purpos
19560 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 61 6c 6c  es..**.** A call
19570 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65   to this routine
19580 20 73 74 6f 72 65 73 20 4e 20 62 79 74 65 73 20   stores N bytes 
19590 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e  of randomness in
195a0 74 6f 20 62 75 66 66 65 72 20 50 2e 0a 2a 2a 0a  to buffer P..**.
195b0 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 69 6d  ** The first tim
195c0 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  e this routine i
195d0 73 20 69 6e 76 6f 6b 65 64 20 28 65 69 74 68 65  s invoked (eithe
195e0 72 20 69 6e 74 65 72 6e 61 6c 6c 79 20 6f 72 20  r internally or 
195f0 62 79 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63  by.** the applic
19600 61 74 69 6f 6e 29 20 74 68 65 20 50 52 4e 47 20  ation) the PRNG 
19610 69 73 20 73 65 65 64 65 64 20 75 73 69 6e 67 20  is seeded using 
19620 72 61 6e 64 6f 6d 6e 65 73 73 20 6f 62 74 61 69  randomness obtai
19630 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20  ned.** from the 
19640 78 52 61 6e 64 6f 6d 6e 65 73 73 20 6d 65 74 68  xRandomness meth
19650 6f 64 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c  od of the defaul
19660 74 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20  t [sqlite3_vfs] 
19670 6f 62 6a 65 63 74 2e 0a 2a 2a 20 4f 6e 20 61 6c  object..** On al
19680 6c 20 73 75 62 73 65 71 75 65 6e 74 20 69 6e 76  l subsequent inv
19690 6f 63 61 74 69 6f 6e 73 2c 20 74 68 65 20 70 73  ocations, the ps
196a0 65 75 64 6f 2d 72 61 6e 64 6f 6d 6e 65 73 73 20  eudo-randomness 
196b0 69 73 20 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20  is generated.** 
196c0 69 6e 74 65 72 6e 61 6c 6c 79 20 61 6e 64 20 77  internally and w
196d0 69 74 68 6f 75 74 20 72 65 63 6f 75 72 73 65 20  ithout recourse 
196e0 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  to the [sqlite3_
196f0 76 66 73 5d 20 78 52 61 6e 64 6f 6d 6e 65 73 73  vfs] xRandomness
19700 0a 2a 2a 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a  .** method..**.*
19710 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
19720 2a 2a 20 5b 48 31 37 33 39 32 5d 0a 2a 2f 0a 53  ** [H17392].*/.S
19730 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
19740 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73  qlite3_randomnes
19750 73 28 69 6e 74 20 4e 2c 20 76 6f 69 64 20 2a 50  s(int N, void *P
19760 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
19770 45 46 3a 20 43 6f 6d 70 69 6c 65 2d 54 69 6d 65  EF: Compile-Time
19780 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 43   Authorization C
19790 61 6c 6c 62 61 63 6b 73 20 7b 48 31 32 35 30 30  allbacks {H12500
197a0 7d 20 3c 53 37 30 31 30 30 3e 0a 2a 2a 0a 2a 2a  } <S70100>.**.**
197b0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   This routine re
197c0 67 69 73 74 65 72 73 20 61 20 61 75 74 68 6f 72  gisters a author
197d0 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 77 69  izer callback wi
197e0 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a  th a particular.
197f0 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  ** [database con
19800 6e 65 63 74 69 6f 6e 5d 2c 20 73 75 70 70 6c 69  nection], suppli
19810 65 64 20 69 6e 20 74 68 65 20 66 69 72 73 74 20  ed in the first 
19820 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 20 54 68 65  argument..** The
19830 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c   authorizer call
19840 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20  back is invoked 
19850 61 73 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  as SQL statement
19860 73 20 61 72 65 20 62 65 69 6e 67 20 63 6f 6d 70  s are being comp
19870 69 6c 65 64 0a 2a 2a 20 62 79 20 5b 73 71 6c 69  iled.** by [sqli
19880 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 6f  te3_prepare()] o
19890 72 20 69 74 73 20 76 61 72 69 61 6e 74 73 20 5b  r its variants [
198a0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
198b0 76 32 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74  v2()],.** [sqlit
198c0 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d 20  e3_prepare16()] 
198d0 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  and [sqlite3_pre
198e0 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 20 20 41  pare16_v2()].  A
198f0 74 20 76 61 72 69 6f 75 73 0a 2a 2a 20 70 6f 69  t various.** poi
19900 6e 74 73 20 64 75 72 69 6e 67 20 74 68 65 20 63  nts during the c
19910 6f 6d 70 69 6c 61 74 69 6f 6e 20 70 72 6f 63 65  ompilation proce
19920 73 73 2c 20 61 73 20 6c 6f 67 69 63 20 69 73 20  ss, as logic is 
19930 62 65 69 6e 67 20 63 72 65 61 74 65 64 0a 2a 2a  being created.**
19940 20 74 6f 20 70 65 72 66 6f 72 6d 20 76 61 72 69   to perform vari
19950 6f 75 73 20 61 63 74 69 6f 6e 73 2c 20 74 68 65  ous actions, the
19960 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c   authorizer call
19970 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20  back is invoked 
19980 74 6f 0a 2a 2a 20 73 65 65 20 69 66 20 74 68 6f  to.** see if tho
19990 73 65 20 61 63 74 69 6f 6e 73 20 61 72 65 20 61  se actions are a
199a0 6c 6c 6f 77 65 64 2e 20 20 54 68 65 20 61 75 74  llowed.  The aut
199b0 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b  horizer callback
199c0 20 73 68 6f 75 6c 64 0a 2a 2a 20 72 65 74 75 72   should.** retur
199d0 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 74 6f  n [SQLITE_OK] to
199e0 20 61 6c 6c 6f 77 20 74 68 65 20 61 63 74 69 6f   allow the actio
199f0 6e 2c 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52  n, [SQLITE_IGNOR
19a00 45 5d 20 74 6f 20 64 69 73 61 6c 6c 6f 77 20 74  E] to disallow t
19a10 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 63 20 61  he.** specific a
19a20 63 74 69 6f 6e 20 62 75 74 20 61 6c 6c 6f 77 20  ction but allow 
19a30 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
19a40 74 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 74 6f  t to continue to
19a50 20 62 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 2c   be.** compiled,
19a60 20 6f 72 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59   or [SQLITE_DENY
19a70 5d 20 74 6f 20 63 61 75 73 65 20 74 68 65 20 65  ] to cause the e
19a80 6e 74 69 72 65 20 53 51 4c 20 73 74 61 74 65 6d  ntire SQL statem
19a90 65 6e 74 20 74 6f 20 62 65 0a 2a 2a 20 72 65 6a  ent to be.** rej
19aa0 65 63 74 65 64 20 77 69 74 68 20 61 6e 20 65 72  ected with an er
19ab0 72 6f 72 2e 20 20 49 66 20 74 68 65 20 61 75 74  ror.  If the aut
19ac0 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b  horizer callback
19ad0 20 72 65 74 75 72 6e 73 0a 2a 2a 20 61 6e 79 20   returns.** any 
19ae0 76 61 6c 75 65 20 6f 74 68 65 72 20 74 68 61 6e  value other than
19af0 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d   [SQLITE_IGNORE]
19b00 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2c 20 6f  , [SQLITE_OK], o
19b10 72 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59 5d 0a  r [SQLITE_DENY].
19b20 2a 2a 20 74 68 65 6e 20 74 68 65 20 5b 73 71 6c  ** then the [sql
19b30 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
19b40 29 5d 20 6f 72 20 65 71 75 69 76 61 6c 65 6e 74  )] or equivalent
19b50 20 63 61 6c 6c 20 74 68 61 74 20 74 72 69 67 67   call that trigg
19b60 65 72 65 64 0a 2a 2a 20 74 68 65 20 61 75 74 68  ered.** the auth
19b70 6f 72 69 7a 65 72 20 77 69 6c 6c 20 66 61 69 6c  orizer will fail
19b80 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d   with an error m
19b90 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 57 68  essage..**.** Wh
19ba0 65 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  en the callback 
19bb0 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f  returns [SQLITE_
19bc0 4f 4b 5d 2c 20 74 68 61 74 20 6d 65 61 6e 73 20  OK], that means 
19bd0 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a  the operation.**
19be0 20 72 65 71 75 65 73 74 65 64 20 69 73 20 6f 6b   requested is ok
19bf0 2e 20 20 57 68 65 6e 20 74 68 65 20 63 61 6c 6c  .  When the call
19c00 62 61 63 6b 20 72 65 74 75 72 6e 73 20 5b 53 51  back returns [SQ
19c10 4c 49 54 45 5f 44 45 4e 59 5d 2c 20 74 68 65 0a  LITE_DENY], the.
19c20 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  ** [sqlite3_prep
19c30 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 65 71 75  are_v2()] or equ
19c40 69 76 61 6c 65 6e 74 20 63 61 6c 6c 20 74 68 61  ivalent call tha
19c50 74 20 74 72 69 67 67 65 72 65 64 20 74 68 65 0a  t triggered the.
19c60 2a 2a 20 61 75 74 68 6f 72 69 7a 65 72 20 77 69  ** authorizer wi
19c70 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e 20  ll fail with an 
19c80 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 65 78  error message ex
19c90 70 6c 61 69 6e 69 6e 67 20 74 68 61 74 0a 2a 2a  plaining that.**
19ca0 20 61 63 63 65 73 73 20 69 73 20 64 65 6e 69 65   access is denie
19cb0 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  d. .**.** The fi
19cc0 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f  rst parameter to
19cd0 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20   the authorizer 
19ce0 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 20 63 6f  callback is a co
19cf0 70 79 20 6f 66 20 74 68 65 20 74 68 69 72 64 0a  py of the third.
19d00 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  ** parameter to 
19d10 74 68 65 20 73 71 6c 69 74 65 33 5f 73 65 74 5f  the sqlite3_set_
19d20 61 75 74 68 6f 72 69 7a 65 72 28 29 20 69 6e 74  authorizer() int
19d30 65 72 66 61 63 65 2e 20 54 68 65 20 73 65 63 6f  erface. The seco
19d40 6e 64 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20  nd parameter.** 
19d50 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  to the callback 
19d60 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 5b 53  is an integer [S
19d70 51 4c 49 54 45 5f 43 4f 50 59 20 7c 20 61 63 74  QLITE_COPY | act
19d80 69 6f 6e 20 63 6f 64 65 5d 20 74 68 61 74 20 73  ion code] that s
19d90 70 65 63 69 66 69 65 73 0a 2a 2a 20 74 68 65 20  pecifies.** the 
19da0 70 61 72 74 69 63 75 6c 61 72 20 61 63 74 69 6f  particular actio
19db0 6e 20 74 6f 20 62 65 20 61 75 74 68 6f 72 69 7a  n to be authoriz
19dc0 65 64 2e 20 54 68 65 20 74 68 69 72 64 20 74 68  ed. The third th
19dd0 72 6f 75 67 68 20 73 69 78 74 68 20 70 61 72 61  rough sixth para
19de0 6d 65 74 65 72 73 0a 2a 2a 20 74 6f 20 74 68 65  meters.** to the
19df0 20 63 61 6c 6c 62 61 63 6b 20 61 72 65 20 7a 65   callback are ze
19e00 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74  ro-terminated st
19e10 72 69 6e 67 73 20 74 68 61 74 20 63 6f 6e 74 61  rings that conta
19e20 69 6e 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a  in additional.**
19e30 20 64 65 74 61 69 6c 73 20 61 62 6f 75 74 20 74   details about t
19e40 68 65 20 61 63 74 69 6f 6e 20 74 6f 20 62 65 20  he action to be 
19e50 61 75 74 68 6f 72 69 7a 65 64 2e 0a 2a 2a 0a 2a  authorized..**.*
19e60 2a 20 49 66 20 74 68 65 20 61 63 74 69 6f 6e 20  * If the action 
19e70 63 6f 64 65 20 69 73 20 5b 53 51 4c 49 54 45 5f  code is [SQLITE_
19e80 52 45 41 44 5d 0a 2a 2a 20 61 6e 64 20 74 68 65  READ].** and the
19e90 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e   callback return
19ea0 73 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  s [SQLITE_IGNORE
19eb0 5d 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 5b 70  ] then the.** [p
19ec0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
19ed0 74 5d 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  t] statement is 
19ee0 63 6f 6e 73 74 72 75 63 74 65 64 20 74 6f 20 73  constructed to s
19ef0 75 62 73 74 69 74 75 74 65 0a 2a 2a 20 61 20 4e  ubstitute.** a N
19f00 55 4c 4c 20 76 61 6c 75 65 20 69 6e 20 70 6c 61  ULL value in pla
19f10 63 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  ce of the table 
19f20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 77 6f 75 6c  column that woul
19f30 64 20 68 61 76 65 0a 2a 2a 20 62 65 65 6e 20 72  d have.** been r
19f40 65 61 64 20 69 66 20 5b 53 51 4c 49 54 45 5f 4f  ead if [SQLITE_O
19f50 4b 5d 20 68 61 64 20 62 65 65 6e 20 72 65 74 75  K] had been retu
19f60 72 6e 65 64 2e 20 20 54 68 65 20 5b 53 51 4c 49  rned.  The [SQLI
19f70 54 45 5f 49 47 4e 4f 52 45 5d 0a 2a 2a 20 72 65  TE_IGNORE].** re
19f80 74 75 72 6e 20 63 61 6e 20 62 65 20 75 73 65 64  turn can be used
19f90 20 74 6f 20 64 65 6e 79 20 61 6e 20 75 6e 74 72   to deny an untr
19fa0 75 73 74 65 64 20 75 73 65 72 20 61 63 63 65 73  usted user acces
19fb0 73 20 74 6f 20 69 6e 64 69 76 69 64 75 61 6c 0a  s to individual.
19fc0 2a 2a 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 61 20  ** columns of a 
19fd0 74 61 62 6c 65 2e 0a 2a 2a 20 49 66 20 74 68 65  table..** 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 44 45 4c 45 54 45 5d 20  [SQLITE_DELETE] 
1a000 61 6e 64 20 74 68 65 20 63 61 6c 6c 62 61 63 6b  and the callback
1a010 20 72 65 74 75 72 6e 73 0a 2a 2a 20 5b 53 51 4c   returns.** [SQL
1a020 49 54 45 5f 49 47 4e 4f 52 45 5d 20 74 68 65 6e  ITE_IGNORE] then
1a030 20 74 68 65 20 5b 44 45 4c 45 54 45 5d 20 6f 70   the [DELETE] op
1a040 65 72 61 74 69 6f 6e 20 70 72 6f 63 65 65 64 73  eration proceeds
1a050 20 62 75 74 20 74 68 65 0a 2a 2a 20 5b 74 72 75   but the.** [tru
1a060 6e 63 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 69  ncate optimizati
1a070 6f 6e 5d 20 69 73 20 64 69 73 61 62 6c 65 64 20  on] is disabled 
1a080 61 6e 64 20 61 6c 6c 20 72 6f 77 73 20 61 72 65  and all rows are
1a090 20 64 65 6c 65 74 65 64 20 69 6e 64 69 76 69 64   deleted individ
1a0a0 75 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20  ually..**.** An 
1a0b0 61 75 74 68 6f 72 69 7a 65 72 20 69 73 20 75 73  authorizer is us
1a0c0 65 64 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 33  ed when [sqlite3
1a0d0 5f 70 72 65 70 61 72 65 20 7c 20 70 72 65 70 61  _prepare | prepa
1a0e0 72 69 6e 67 5d 0a 2a 2a 20 53 51 4c 20 73 74 61  ring].** SQL sta
1a0f0 74 65 6d 65 6e 74 73 20 66 72 6f 6d 20 61 6e 20  tements from an 
1a100 75 6e 74 72 75 73 74 65 64 20 73 6f 75 72 63 65  untrusted source
1a110 2c 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74  , to ensure that
1a120 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65   the SQL stateme
1a130 6e 74 73 0a 2a 2a 20 64 6f 20 6e 6f 74 20 74 72  nts.** do not tr
1a140 79 20 74 6f 20 61 63 63 65 73 73 20 64 61 74 61  y to access data
1a150 20 74 68 65 79 20 61 72 65 20 6e 6f 74 20 61 6c   they are not al
1a160 6c 6f 77 65 64 20 74 6f 20 73 65 65 2c 20 6f 72  lowed to see, or
1a170 20 74 68 61 74 20 74 68 65 79 20 64 6f 20 6e 6f   that they do no
1a180 74 0a 2a 2a 20 74 72 79 20 74 6f 20 65 78 65 63  t.** try to exec
1a190 75 74 65 20 6d 61 6c 69 63 69 6f 75 73 20 73 74  ute malicious st
1a1a0 61 74 65 6d 65 6e 74 73 20 74 68 61 74 20 64 61  atements that da
1a1b0 6d 61 67 65 20 74 68 65 20 64 61 74 61 62 61 73  mage the databas
1a1c0 65 2e 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70  e.  For.** examp
1a1d0 6c 65 2c 20 61 6e 20 61 70 70 6c 69 63 61 74 69  le, an applicati
1a1e0 6f 6e 20 6d 61 79 20 61 6c 6c 6f 77 20 61 20 75  on may allow a u
1a1f0 73 65 72 20 74 6f 20 65 6e 74 65 72 20 61 72 62  ser to enter arb
1a200 69 74 72 61 72 79 0a 2a 2a 20 53 51 4c 20 71 75  itrary.** SQL qu
1a210 65 72 69 65 73 20 66 6f 72 20 65 76 61 6c 75 61  eries for evalua
1a220 74 69 6f 6e 20 62 79 20 61 20 64 61 74 61 62 61  tion by a databa
1a230 73 65 2e 20 20 42 75 74 20 74 68 65 20 61 70 70  se.  But the app
1a240 6c 69 63 61 74 69 6f 6e 20 64 6f 65 73 0a 2a 2a  lication does.**
1a250 20 6e 6f 74 20 77 61 6e 74 20 74 68 65 20 75 73   not want the us
1a260 65 72 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f  er to be able to
1a270 20 6d 61 6b 65 20 61 72 62 69 74 72 61 72 79 20   make arbitrary 
1a280 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a  changes to the.*
1a290 2a 20 64 61 74 61 62 61 73 65 2e 20 20 41 6e 20  * database.  An 
1a2a0 61 75 74 68 6f 72 69 7a 65 72 20 63 6f 75 6c 64  authorizer could
1a2b0 20 74 68 65 6e 20 62 65 20 70 75 74 20 69 6e 20   then be put in 
1a2c0 70 6c 61 63 65 20 77 68 69 6c 65 20 74 68 65 0a  place while the.
1a2d0 2a 2a 20 75 73 65 72 2d 65 6e 74 65 72 65 64 20  ** user-entered 
1a2e0 53 51 4c 20 69 73 20 62 65 69 6e 67 20 5b 73 71  SQL is being [sq
1a2f0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 20 7c 20  lite3_prepare | 
1a300 70 72 65 70 61 72 65 64 5d 20 74 68 61 74 0a 2a  prepared] that.*
1a310 2a 20 64 69 73 61 6c 6c 6f 77 73 20 65 76 65 72  * disallows ever
1a320 79 74 68 69 6e 67 20 65 78 63 65 70 74 20 5b 53  ything except [S
1a330 45 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 6e 74  ELECT] statement
1a340 73 2e 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 69 63 61  s..**.** Applica
1a350 74 69 6f 6e 73 20 74 68 61 74 20 6e 65 65 64 20  tions that need 
1a360 74 6f 20 70 72 6f 63 65 73 73 20 53 51 4c 20 66  to process SQL f
1a370 72 6f 6d 20 75 6e 74 72 75 73 74 65 64 20 73 6f  rom untrusted so
1a380 75 72 63 65 73 0a 2a 2a 20 6d 69 67 68 74 20 61  urces.** might a
1a390 6c 73 6f 20 63 6f 6e 73 69 64 65 72 20 6c 6f 77  lso consider low
1a3a0 65 72 69 6e 67 20 72 65 73 6f 75 72 63 65 20 6c  ering resource l
1a3b0 69 6d 69 74 73 20 75 73 69 6e 67 20 5b 73 71 6c  imits using [sql
1a3c0 69 74 65 33 5f 6c 69 6d 69 74 28 29 5d 0a 2a 2a  ite3_limit()].**
1a3d0 20 61 6e 64 20 6c 69 6d 69 74 69 6e 67 20 64 61   and limiting da
1a3e0 74 61 62 61 73 65 20 73 69 7a 65 20 75 73 69 6e  tabase size usin
1a3f0 67 20 74 68 65 20 5b 6d 61 78 5f 70 61 67 65 5f  g the [max_page_
1a400 63 6f 75 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 0a  count] [PRAGMA].
1a410 2a 2a 20 69 6e 20 61 64 64 69 74 69 6f 6e 20 74  ** in addition t
1a420 6f 20 75 73 69 6e 67 20 61 6e 20 61 75 74 68 6f  o using an autho
1a430 72 69 7a 65 72 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c  rizer..**.** Onl
1a440 79 20 61 20 73 69 6e 67 6c 65 20 61 75 74 68 6f  y a single autho
1a450 72 69 7a 65 72 20 63 61 6e 20 62 65 20 69 6e 20  rizer can be in 
1a460 70 6c 61 63 65 20 6f 6e 20 61 20 64 61 74 61 62  place on a datab
1a470 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a  ase connection.*
1a480 2a 20 61 74 20 61 20 74 69 6d 65 2e 20 20 45 61  * at a time.  Ea
1a490 63 68 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  ch call to sqlit
1a4a0 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65  e3_set_authorize
1a4b0 72 20 6f 76 65 72 72 69 64 65 73 20 74 68 65 0a  r overrides the.
1a4c0 2a 2a 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c  ** previous call
1a4d0 2e 20 20 44 69 73 61 62 6c 65 20 74 68 65 20 61  .  Disable the a
1a4e0 75 74 68 6f 72 69 7a 65 72 20 62 79 20 69 6e 73  uthorizer by ins
1a4f0 74 61 6c 6c 69 6e 67 20 61 20 4e 55 4c 4c 20 63  talling a NULL c
1a500 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 54 68 65 20  allback..** The 
1a510 61 75 74 68 6f 72 69 7a 65 72 20 69 73 20 64 69  authorizer is di
1a520 73 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c  sabled by defaul
1a530 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  t..**.** The aut
1a540 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b  horizer callback
1a550 20 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79   must not do any
1a560 74 68 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20  thing that will 
1a570 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 61  modify.** the da
1a580 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1a590 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74  n that invoked t
1a5a0 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61  he authorizer ca
1a5b0 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 4e 6f 74 65 20  llback..** Note 
1a5c0 74 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 72  that [sqlite3_pr
1a5d0 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20  epare_v2()] and 
1a5e0 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
1a5f0 20 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 65   both modify the
1a600 69 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63  ir.** database c
1a610 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 74  onnections for t
1a620 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d  he meaning of "m
1a630 6f 64 69 66 79 22 20 69 6e 20 74 68 69 73 20 70  odify" in this p
1a640 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20  aragraph..**.** 
1a650 57 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72  When [sqlite3_pr
1a660 65 70 61 72 65 5f 76 32 28 29 5d 20 69 73 20 75  epare_v2()] is u
1a670 73 65 64 20 74 6f 20 70 72 65 70 61 72 65 20 61  sed to prepare a
1a680 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 0a   statement, the.
1a690 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 6d 69 67  ** statement mig
1a6a0 68 74 20 62 65 20 72 65 2d 70 72 65 70 61 72 65  ht be re-prepare
1a6b0 64 20 64 75 72 69 6e 67 20 5b 73 71 6c 69 74 65  d during [sqlite
1a6c0 33 5f 73 74 65 70 28 29 5d 20 64 75 65 20 74 6f  3_step()] due to
1a6d0 20 61 20 0a 2a 2a 20 73 63 68 65 6d 61 20 63 68   a .** schema ch
1a6e0 61 6e 67 65 2e 20 20 48 65 6e 63 65 2c 20 74 68  ange.  Hence, th
1a6f0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 68  e application sh
1a700 6f 75 6c 64 20 65 6e 73 75 72 65 20 74 68 61 74  ould ensure that
1a710 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20   the.** correct 
1a720 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62  authorizer callb
1a730 61 63 6b 20 72 65 6d 61 69 6e 73 20 69 6e 20 70  ack remains in p
1a740 6c 61 63 65 20 64 75 72 69 6e 67 20 74 68 65 20  lace during the 
1a750 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
1a760 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ..**.** Note tha
1a770 74 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72  t the authorizer
1a780 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76   callback is inv
1a790 6f 6b 65 64 20 6f 6e 6c 79 20 64 75 72 69 6e 67  oked only during
1a7a0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  .** [sqlite3_pre
1a7b0 70 61 72 65 28 29 5d 20 6f 72 20 69 74 73 20 76  pare()] or its v
1a7c0 61 72 69 61 6e 74 73 2e 20 20 41 75 74 68 6f 72  ariants.  Author
1a7d0 69 7a 61 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 2a  ization is not.*
1a7e0 2a 20 70 65 72 66 6f 72 6d 65 64 20 64 75 72 69  * performed duri
1a7f0 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 65 76 61  ng statement eva
1a800 6c 75 61 74 69 6f 6e 20 69 6e 20 5b 73 71 6c 69  luation in [sqli
1a810 74 65 33 5f 73 74 65 70 28 29 5d 2c 20 75 6e 6c  te3_step()], unl
1a820 65 73 73 0a 2a 2a 20 61 73 20 73 74 61 74 65 64  ess.** as stated
1a830 20 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73   in the previous
1a840 20 70 61 72 61 67 72 61 70 68 2c 20 73 71 6c 69   paragraph, sqli
1a850 74 65 33 5f 73 74 65 70 28 29 20 69 6e 76 6f 6b  te3_step() invok
1a860 65 73 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 70 72  es.** sqlite3_pr
1a870 65 70 61 72 65 5f 76 32 28 29 20 74 6f 20 72 65  epare_v2() to re
1a880 70 72 65 70 61 72 65 20 61 20 73 74 61 74 65 6d  prepare a statem
1a890 65 6e 74 20 61 66 74 65 72 20 61 20 73 63 68 65  ent after a sche
1a8a0 6d 61 20 63 68 61 6e 67 65 2e 0a 2a 2a 0a 2a 2a  ma change..**.**
1a8b0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
1a8c0 2a 20 5b 48 31 32 35 30 31 5d 20 5b 48 31 32 35  * [H12501] [H125
1a8d0 30 32 5d 20 5b 48 31 32 35 30 33 5d 20 5b 48 31  02] [H12503] [H1
1a8e0 32 35 30 34 5d 20 5b 48 31 32 35 30 35 5d 20 5b  2504] [H12505] [
1a8f0 48 31 32 35 30 36 5d 20 5b 48 31 32 35 30 37 5d  H12506] [H12507]
1a900 20 5b 48 31 32 35 31 30 5d 0a 2a 2a 20 5b 48 31   [H12510].** [H1
1a910 32 35 31 31 5d 20 5b 48 31 32 35 31 32 5d 20 5b  2511] [H12512] [
1a920 48 31 32 35 32 30 5d 20 5b 48 31 32 35 32 31 5d  H12520] [H12521]
1a930 20 5b 48 31 32 35 32 32 5d 0a 2a 2f 0a 53 51 4c   [H12522].*/.SQL
1a940 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1a950 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a  te3_set_authoriz
1a960 65 72 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 0a  er(.  sqlite3*,.
1a970 20 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76    int (*xAuth)(v
1a980 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63  oid*,int,const c
1a990 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
1a9a0 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
1a9b0 73 74 20 63 68 61 72 2a 29 2c 0a 20 20 76 6f 69  st char*),.  voi
1a9c0 64 20 2a 70 55 73 65 72 44 61 74 61 0a 29 3b 0a  d *pUserData.);.
1a9d0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
1a9e0 20 41 75 74 68 6f 72 69 7a 65 72 20 52 65 74 75   Authorizer Retu
1a9f0 72 6e 20 43 6f 64 65 73 20 7b 48 31 32 35 39 30  rn Codes {H12590
1aa00 7d 20 3c 48 31 32 35 30 30 3e 0a 2a 2a 0a 2a 2a  } <H12500>.**.**
1aa10 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 65   The [sqlite3_se
1aa20 74 5f 61 75 74 68 6f 72 69 7a 65 72 20 7c 20 61  t_authorizer | a
1aa30 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61  uthorizer callba
1aa40 63 6b 20 66 75 6e 63 74 69 6f 6e 5d 20 6d 75 73  ck function] mus
1aa50 74 0a 2a 2a 20 72 65 74 75 72 6e 20 65 69 74 68  t.** return eith
1aa60 65 72 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f  er [SQLITE_OK] o
1aa70 72 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 74  r one of these t
1aa80 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20 69 6e 20  wo constants in 
1aa90 6f 72 64 65 72 0a 2a 2a 20 74 6f 20 73 69 67 6e  order.** to sign
1aaa0 61 6c 20 53 51 4c 69 74 65 20 77 68 65 74 68 65  al SQLite whethe
1aab0 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 61 63 74  r or not the act
1aac0 69 6f 6e 20 69 73 20 70 65 72 6d 69 74 74 65 64  ion is permitted
1aad0 2e 20 20 53 65 65 20 74 68 65 0a 2a 2a 20 5b 73  .  See the.** [s
1aae0 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f  qlite3_set_autho
1aaf0 72 69 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 7a  rizer | authoriz
1ab00 65 72 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e  er documentation
1ab10 5d 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  ] for additional
1ab20 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  .** information.
1ab30 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
1ab40 54 45 5f 44 45 4e 59 20 20 20 31 20 20 20 2f 2a  TE_DENY   1   /*
1ab50 20 41 62 6f 72 74 20 74 68 65 20 53 51 4c 20 73   Abort the SQL s
1ab60 74 61 74 65 6d 65 6e 74 20 77 69 74 68 20 61 6e  tatement with an
1ab70 20 65 72 72 6f 72 20 2a 2f 0a 23 64 65 66 69 6e   error */.#defin
1ab80 65 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20  e SQLITE_IGNORE 
1ab90 32 20 20 20 2f 2a 20 44 6f 6e 27 74 20 61 6c 6c  2   /* Don't all
1aba0 6f 77 20 61 63 63 65 73 73 2c 20 62 75 74 20 64  ow access, but d
1abb0 6f 6e 27 74 20 67 65 6e 65 72 61 74 65 20 61 6e  on't generate an
1abc0 20 65 72 72 6f 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a   error */../*.**
1abd0 20 43 41 50 49 33 52 45 46 3a 20 41 75 74 68 6f   CAPI3REF: Autho
1abe0 72 69 7a 65 72 20 41 63 74 69 6f 6e 20 43 6f 64  rizer Action Cod
1abf0 65 73 20 7b 48 31 32 35 35 30 7d 20 3c 48 31 32  es {H12550} <H12
1ac00 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b  500>.**.** The [
1ac10 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68  sqlite3_set_auth
1ac20 6f 72 69 7a 65 72 28 29 5d 20 69 6e 74 65 72 66  orizer()] interf
1ac30 61 63 65 20 72 65 67 69 73 74 65 72 73 20 61 20  ace registers a 
1ac40 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f  callback functio
1ac50 6e 0a 2a 2a 20 74 68 61 74 20 69 73 20 69 6e 76  n.** that is inv
1ac60 6f 6b 65 64 20 74 6f 20 61 75 74 68 6f 72 69 7a  oked to authoriz
1ac70 65 20 63 65 72 74 61 69 6e 20 53 51 4c 20 73 74  e certain SQL st
1ac80 61 74 65 6d 65 6e 74 20 61 63 74 69 6f 6e 73 2e  atement actions.
1ac90 20 20 54 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20    The.** second 
1aca0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65  parameter to the
1acb0 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20   callback is an 
1acc0 69 6e 74 65 67 65 72 20 63 6f 64 65 20 74 68 61  integer code tha
1acd0 74 20 73 70 65 63 69 66 69 65 73 0a 2a 2a 20 77  t specifies.** w
1ace0 68 61 74 20 61 63 74 69 6f 6e 20 69 73 20 62 65  hat action is be
1acf0 69 6e 67 20 61 75 74 68 6f 72 69 7a 65 64 2e 20  ing authorized. 
1ad00 20 54 68 65 73 65 20 61 72 65 20 74 68 65 20 69   These are the i
1ad10 6e 74 65 67 65 72 20 61 63 74 69 6f 6e 20 63 6f  nteger action co
1ad20 64 65 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 20  des that.** the 
1ad30 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62  authorizer callb
1ad40 61 63 6b 20 6d 61 79 20 62 65 20 70 61 73 73 65  ack may be passe
1ad50 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 61  d..**.** These a
1ad60 63 74 69 6f 6e 20 63 6f 64 65 20 76 61 6c 75 65  ction code value
1ad70 73 20 73 69 67 6e 69 66 79 20 77 68 61 74 20 6b  s signify what k
1ad80 69 6e 64 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e  ind of operation
1ad90 20 69 73 20 74 6f 20 62 65 0a 2a 2a 20 61 75 74   is to be.** aut
1ada0 68 6f 72 69 7a 65 64 2e 20 20 54 68 65 20 33 72  horized.  The 3r
1adb0 64 20 61 6e 64 20 34 74 68 20 70 61 72 61 6d 65  d and 4th parame
1adc0 74 65 72 73 20 74 6f 20 74 68 65 20 61 75 74 68  ters to the auth
1add0 6f 72 69 7a 61 74 69 6f 6e 0a 2a 2a 20 63 61 6c  orization.** cal
1ade0 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 77  lback function w
1adf0 69 6c 6c 20 62 65 20 70 61 72 61 6d 65 74 65 72  ill be parameter
1ae00 73 20 6f 72 20 4e 55 4c 4c 20 64 65 70 65 6e 64  s or NULL depend
1ae10 69 6e 67 20 6f 6e 20 77 68 69 63 68 20 6f 66 20  ing on which of 
1ae20 74 68 65 73 65 0a 2a 2a 20 63 6f 64 65 73 20 69  these.** codes i
1ae30 73 20 75 73 65 64 20 61 73 20 74 68 65 20 73 65  s used as the se
1ae40 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20  cond parameter. 
1ae50 20 54 68 65 20 35 74 68 20 70 61 72 61 6d 65 74   The 5th paramet
1ae60 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20 61 75 74  er to the.** aut
1ae70 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b  horizer callback
1ae80 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   is the name of 
1ae90 74 68 65 20 64 61 74 61 62 61 73 65 20 28 22 6d  the database ("m
1aea0 61 69 6e 22 2c 20 22 74 65 6d 70 22 2c 0a 2a 2a  ain", "temp",.**
1aeb0 20 65 74 63 2e 29 20 69 66 20 61 70 70 6c 69 63   etc.) if applic
1aec0 61 62 6c 65 2e 20 20 54 68 65 20 36 74 68 20 70  able.  The 6th p
1aed0 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20  arameter to the 
1aee0 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62  authorizer callb
1aef0 61 63 6b 0a 2a 2a 20 69 73 20 74 68 65 20 6e 61  ack.** is the na
1af00 6d 65 20 6f 66 20 74 68 65 20 69 6e 6e 65 72 2d  me of the inner-
1af10 6d 6f 73 74 20 74 72 69 67 67 65 72 20 6f 72 20  most trigger or 
1af20 76 69 65 77 20 74 68 61 74 20 69 73 20 72 65 73  view that is res
1af30 70 6f 6e 73 69 62 6c 65 20 66 6f 72 0a 2a 2a 20  ponsible for.** 
1af40 74 68 65 20 61 63 63 65 73 73 20 61 74 74 65 6d  the access attem
1af50 70 74 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68  pt or NULL if th
1af60 69 73 20 61 63 63 65 73 73 20 61 74 74 65 6d 70  is access attemp
1af70 74 20 69 73 20 64 69 72 65 63 74 6c 79 20 66 72  t is directly fr
1af80 6f 6d 0a 2a 2a 20 74 6f 70 2d 6c 65 76 65 6c 20  om.** top-level 
1af90 53 51 4c 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20  SQL code..**.** 
1afa0 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  Requirements:.**
1afb0 20 5b 48 31 32 35 35 31 5d 20 5b 48 31 32 35 35   [H12551] [H1255
1afc0 32 5d 20 5b 48 31 32 35 35 33 5d 20 5b 48 31 32  2] [H12553] [H12
1afd0 35 35 34 5d 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  554].*/./*******
1afe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1aff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b000 2a 2a 2a 2a 20 33 72 64 20 2a 2a 2a 2a 2a 2a 2a  **** 3rd *******
1b010 2a 2a 2a 2a 2a 20 34 74 68 20 2a 2a 2a 2a 2a 2a  ***** 4th ******
1b020 2a 2a 2a 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  *****/.#define S
1b030 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 49 4e 44  QLITE_CREATE_IND
1b040 45 58 20 20 20 20 20 20 20 20 20 20 31 20 20 20  EX          1   
1b050 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20  /* Index Name   
1b060 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20     Table Name   
1b070 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
1b080 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 41 42 4c  LITE_CREATE_TABL
1b090 45 20 20 20 20 20 20 20 20 20 20 32 20 20 20 2f  E          2   /
1b0a0 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20  * Table Name    
1b0b0 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20    NULL          
1b0c0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
1b0d0 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f  ITE_CREATE_TEMP_
1b0e0 49 4e 44 45 58 20 20 20 20 20 33 20 20 20 2f 2a  INDEX     3   /*
1b0f0 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20   Index Name     
1b100 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20   Table Name     
1b110 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
1b120 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54  TE_CREATE_TEMP_T
1b130 41 42 4c 45 20 20 20 20 20 34 20 20 20 2f 2a 20  ABLE     4   /* 
1b140 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20  Table Name      
1b150 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20  NULL            
1b160 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
1b170 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 52  E_CREATE_TEMP_TR
1b180 49 47 47 45 52 20 20 20 35 20 20 20 2f 2a 20 54  IGGER   5   /* T
1b190 72 69 67 67 65 72 20 4e 61 6d 65 20 20 20 20 54  rigger Name    T
1b1a0 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a  able Name      *
1b1b0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
1b1c0 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 56 49 45  _CREATE_TEMP_VIE
1b1d0 57 20 20 20 20 20 20 36 20 20 20 2f 2a 20 56 69  W      6   /* Vi
1b1e0 65 77 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55  ew Name       NU
1b1f0 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f  LL            */
1b200 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1b210 43 52 45 41 54 45 5f 54 52 49 47 47 45 52 20 20  CREATE_TRIGGER  
1b220 20 20 20 20 20 20 37 20 20 20 2f 2a 20 54 72 69        7   /* Tri
1b230 67 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62  gger Name    Tab
1b240 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a  le Name      */.
1b250 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
1b260 52 45 41 54 45 5f 56 49 45 57 20 20 20 20 20 20  REATE_VIEW      
1b270 20 20 20 20 20 38 20 20 20 2f 2a 20 56 69 65 77       8   /* View
1b280 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c   Name       NULL
1b290 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23              */.#
1b2a0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45  define SQLITE_DE
1b2b0 4c 45 54 45 20 20 20 20 20 20 20 20 20 20 20 20  LETE            
1b2c0 20 20 20 20 39 20 20 20 2f 2a 20 54 61 62 6c 65      9   /* Table
1b2d0 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20   Name      NULL 
1b2e0 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64             */.#d
1b2f0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f  efine SQLITE_DRO
1b300 50 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20 20  P_INDEX         
1b310 20 20 31 30 20 20 20 2f 2a 20 49 6e 64 65 78 20    10   /* Index 
1b320 4e 61 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20  Name      Table 
1b330 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65  Name      */.#de
1b340 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50  fine SQLITE_DROP
1b350 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20 20 20  _TABLE          
1b360 20 31 31 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e   11   /* Table N
1b370 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20  ame      NULL   
1b380 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66           */.#def
1b390 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f  ine SQLITE_DROP_
1b3a0 54 45 4d 50 5f 49 4e 44 45 58 20 20 20 20 20 20  TEMP_INDEX      
1b3b0 31 32 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61  12   /* Index Na
1b3c0 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e 61  me      Table Na
1b3d0 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69  me      */.#defi
1b3e0 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54  ne SQLITE_DROP_T
1b3f0 45 4d 50 5f 54 41 42 4c 45 20 20 20 20 20 20 31  EMP_TABLE      1
1b400 33 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d  3   /* Table Nam
1b410 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20  e      NULL     
1b420 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e         */.#defin
1b430 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45  e SQLITE_DROP_TE
1b440 4d 50 5f 54 52 49 47 47 45 52 20 20 20 20 31 34  MP_TRIGGER    14
1b450 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61     /* Trigger Na
1b460 6d 65 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65  me    Table Name
1b470 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
1b480 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d   SQLITE_DROP_TEM
1b490 50 5f 56 49 45 57 20 20 20 20 20 20 20 31 35 20  P_VIEW       15 
1b4a0 20 20 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20    /* View Name  
1b4b0 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20       NULL       
1b4c0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
1b4d0 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 52 49 47  SQLITE_DROP_TRIG
1b4e0 47 45 52 20 20 20 20 20 20 20 20 20 31 36 20 20  GER         16  
1b4f0 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d 65   /* Trigger Name
1b500 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20      Table Name  
1b510 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53      */.#define S
1b520 51 4c 49 54 45 5f 44 52 4f 50 5f 56 49 45 57 20  QLITE_DROP_VIEW 
1b530 20 20 20 20 20 20 20 20 20 20 20 31 37 20 20 20             17   
1b540 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20 20  /* View Name    
1b550 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20     NULL         
1b560 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
1b570 4c 49 54 45 5f 49 4e 53 45 52 54 20 20 20 20 20  LITE_INSERT     
1b580 20 20 20 20 20 20 20 20 20 20 31 38 20 20 20 2f            18   /
1b590 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20  * Table Name    
1b5a0 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20    NULL          
1b5b0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
1b5c0 49 54 45 5f 50 52 41 47 4d 41 20 20 20 20 20 20  ITE_PRAGMA      
1b5d0 20 20 20 20 20 20 20 20 20 31 39 20 20 20 2f 2a           19   /*
1b5e0 20 50 72 61 67 6d 61 20 4e 61 6d 65 20 20 20 20   Pragma Name    
1b5f0 20 31 73 74 20 61 72 67 20 6f 72 20 4e 55 4c 4c   1st arg or NULL
1b600 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
1b610 54 45 5f 52 45 41 44 20 20 20 20 20 20 20 20 20  TE_READ         
1b620 20 20 20 20 20 20 20 20 32 30 20 20 20 2f 2a 20          20   /* 
1b630 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20  Table Name      
1b640 43 6f 6c 75 6d 6e 20 4e 61 6d 65 20 20 20 20 20  Column Name     
1b650 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
1b660 45 5f 53 45 4c 45 43 54 20 20 20 20 20 20 20 20  E_SELECT        
1b670 20 20 20 20 20 20 20 32 31 20 20 20 2f 2a 20 4e         21   /* N
1b680 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 4e  ULL            N
1b690 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a  ULL            *
1b6a0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
1b6b0 5f 54 52 41 4e 53 41 43 54 49 4f 4e 20 20 20 20  _TRANSACTION    
1b6c0 20 20 20 20 20 20 32 32 20 20 20 2f 2a 20 4f 70        22   /* Op
1b6d0 65 72 61 74 69 6f 6e 20 20 20 20 20 20 20 4e 55  eration       NU
1b6e0 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f  LL            */
1b6f0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1b700 55 50 44 41 54 45 20 20 20 20 20 20 20 20 20 20  UPDATE          
1b710 20 20 20 20 20 32 33 20 20 20 2f 2a 20 54 61 62       23   /* Tab
1b720 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 43 6f 6c  le Name      Col
1b730 75 6d 6e 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a  umn Name     */.
1b740 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41  #define SQLITE_A
1b750 54 54 41 43 48 20 20 20 20 20 20 20 20 20 20 20  TTACH           
1b760 20 20 20 20 32 34 20 20 20 2f 2a 20 46 69 6c 65      24   /* File
1b770 6e 61 6d 65 20 20 20 20 20 20 20 20 4e 55 4c 4c  name        NULL
1b780 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23              */.#
1b790 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45  define SQLITE_DE
1b7a0 54 41 43 48 20 20 20 20 20 20 20 20 20 20 20 20  TACH            
1b7b0 20 20 20 32 35 20 20 20 2f 2a 20 44 61 74 61 62     25   /* Datab
1b7c0 61 73 65 20 4e 61 6d 65 20 20 20 4e 55 4c 4c 20  ase Name   NULL 
1b7d0 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64             */.#d
1b7e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 4c 54  efine SQLITE_ALT
1b7f0 45 52 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20  ER_TABLE        
1b800 20 20 32 36 20 20 20 2f 2a 20 44 61 74 61 62 61    26   /* Databa
1b810 73 65 20 4e 61 6d 65 20 20 20 54 61 62 6c 65 20  se Name   Table 
1b820 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65  Name      */.#de
1b830 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 45 49 4e  fine SQLITE_REIN
1b840 44 45 58 20 20 20 20 20 20 20 20 20 20 20 20 20  DEX             
1b850 20 32 37 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e   27   /* Index N
1b860 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20  ame      NULL   
1b870 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66           */.#def
1b880 69 6e 65 20 53 51 4c 49 54 45 5f 41 4e 41 4c 59  ine SQLITE_ANALY
1b890 5a 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ZE              
1b8a0 32 38 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61  28   /* Table Na
1b8b0 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20  me      NULL    
1b8c0 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69          */.#defi
1b8d0 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45  ne SQLITE_CREATE
1b8e0 5f 56 54 41 42 4c 45 20 20 20 20 20 20 20 20 32  _VTABLE        2
1b8f0 39 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d  9   /* Table Nam
1b900 65 20 20 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61  e      Module Na
1b910 6d 65 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  me     */.#defin
1b920 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56 54  e SQLITE_DROP_VT
1b930 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 33 30  ABLE          30
1b940 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65     /* Table Name
1b950 20 20 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61 6d        Module Nam
1b960 65 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65  e     */.#define
1b970 20 53 51 4c 49 54 45 5f 46 55 4e 43 54 49 4f 4e   SQLITE_FUNCTION
1b980 20 20 20 20 20 20 20 20 20 20 20 20 20 33 31 20               31 
1b990 20 20 2f 2a 20 4e 55 4c 4c 20 20 20 20 20 20 20    /* NULL       
1b9a0 20 20 20 20 20 46 75 6e 63 74 69 6f 6e 20 4e 61       Function Na
1b9b0 6d 65 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20  me   */.#define 
1b9c0 53 51 4c 49 54 45 5f 53 41 56 45 50 4f 49 4e 54  SQLITE_SAVEPOINT
1b9d0 20 20 20 20 20 20 20 20 20 20 20 20 33 32 20 20              32  
1b9e0 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 20 20   /* Operation   
1b9f0 20 20 20 20 53 61 76 65 70 6f 69 6e 74 20 4e 61      Savepoint Na
1ba00 6d 65 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  me  */.#define S
1ba10 51 4c 49 54 45 5f 43 4f 50 59 20 20 20 20 20 20  QLITE_COPY      
1ba20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 20 20              0   
1ba30 2f 2a 20 4e 6f 20 6c 6f 6e 67 65 72 20 75 73 65  /* No longer use
1ba40 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  d */../*.** CAPI
1ba50 33 52 45 46 3a 20 54 72 61 63 69 6e 67 20 41 6e  3REF: Tracing An
1ba60 64 20 50 72 6f 66 69 6c 69 6e 67 20 46 75 6e 63  d Profiling Func
1ba70 74 69 6f 6e 73 20 7b 48 31 32 32 38 30 7d 20 3c  tions {H12280} <
1ba80 53 36 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52  S60400>.** EXPER
1ba90 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68  IMENTAL.**.** Th
1baa0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65 67  ese routines reg
1bab0 69 73 74 65 72 20 63 61 6c 6c 62 61 63 6b 20 66  ister callback f
1bac0 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 63 61  unctions that ca
1bad0 6e 20 62 65 20 75 73 65 64 20 66 6f 72 0a 2a 2a  n be used for.**
1bae0 20 74 72 61 63 69 6e 67 20 61 6e 64 20 70 72 6f   tracing and pro
1baf0 66 69 6c 69 6e 67 20 74 68 65 20 65 78 65 63 75  filing the execu
1bb00 74 69 6f 6e 20 6f 66 20 53 51 4c 20 73 74 61 74  tion of SQL stat
1bb10 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  ements..**.** Th
1bb20 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  e callback funct
1bb30 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 62  ion registered b
1bb40 79 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28  y sqlite3_trace(
1bb50 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 74 0a  ) is invoked at.
1bb60 2a 2a 20 76 61 72 69 6f 75 73 20 74 69 6d 65 73  ** various times
1bb70 20 77 68 65 6e 20 61 6e 20 53 51 4c 20 73 74 61   when an SQL sta
1bb80 74 65 6d 65 6e 74 20 69 73 20 62 65 69 6e 67 20  tement is being 
1bb90 72 75 6e 20 62 79 20 5b 73 71 6c 69 74 65 33 5f  run by [sqlite3_
1bba0 73 74 65 70 28 29 5d 2e 0a 2a 2a 20 54 68 65 20  step()]..** The 
1bbb0 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73  callback returns
1bbc0 20 61 20 55 54 46 2d 38 20 72 65 6e 64 65 72 69   a UTF-8 renderi
1bbd0 6e 67 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74  ng of the SQL st
1bbe0 61 74 65 6d 65 6e 74 20 74 65 78 74 0a 2a 2a 20  atement text.** 
1bbf0 61 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  as the statement
1bc00 20 66 69 72 73 74 20 62 65 67 69 6e 73 20 65 78   first begins ex
1bc10 65 63 75 74 69 6e 67 2e 20 20 41 64 64 69 74 69  ecuting.  Additi
1bc20 6f 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 73 20 6f  onal callbacks o
1bc30 63 63 75 72 0a 2a 2a 20 61 73 20 65 61 63 68 20  ccur.** as each 
1bc40 74 72 69 67 67 65 72 65 64 20 73 75 62 70 72 6f  triggered subpro
1bc50 67 72 61 6d 20 69 73 20 65 6e 74 65 72 65 64 2e  gram is entered.
1bc60 20 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 73 20    The callbacks 
1bc70 66 6f 72 20 74 72 69 67 67 65 72 73 0a 2a 2a 20  for triggers.** 
1bc80 63 6f 6e 74 61 69 6e 20 61 20 55 54 46 2d 38 20  contain a UTF-8 
1bc90 53 51 4c 20 63 6f 6d 6d 65 6e 74 20 74 68 61 74  SQL comment that
1bca0 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20   identifies the 
1bcb0 74 72 69 67 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54  trigger..**.** T
1bcc0 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63  he callback func
1bcd0 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20  tion registered 
1bce0 62 79 20 73 71 6c 69 74 65 33 5f 70 72 6f 66 69  by sqlite3_profi
1bcf0 6c 65 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 0a  le() is invoked.
1bd00 2a 2a 20 61 73 20 65 61 63 68 20 53 51 4c 20 73  ** as each SQL s
1bd10 74 61 74 65 6d 65 6e 74 20 66 69 6e 69 73 68 65  tatement finishe
1bd20 73 2e 20 20 54 68 65 20 70 72 6f 66 69 6c 65 20  s.  The profile 
1bd30 63 61 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e  callback contain
1bd40 73 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e 61  s.** the origina
1bd50 6c 20 73 74 61 74 65 6d 65 6e 74 20 74 65 78 74  l statement text
1bd60 20 61 6e 64 20 61 6e 20 65 73 74 69 6d 61 74 65   and an estimate
1bd70 20 6f 66 20 77 61 6c 6c 2d 63 6c 6f 63 6b 20 74   of wall-clock t
1bd80 69 6d 65 0a 2a 2a 20 6f 66 20 68 6f 77 20 6c 6f  ime.** of how lo
1bd90 6e 67 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e  ng that statemen
1bda0 74 20 74 6f 6f 6b 20 74 6f 20 72 75 6e 2e 0a 2a  t took to run..*
1bdb0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
1bdc0 73 3a 0a 2a 2a 20 5b 48 31 32 32 38 31 5d 20 5b  s:.** [H12281] [
1bdd0 48 31 32 32 38 32 5d 20 5b 48 31 32 32 38 33 5d  H12282] [H12283]
1bde0 20 5b 48 31 32 32 38 34 5d 20 5b 48 31 32 32 38   [H12284] [H1228
1bdf0 35 5d 20 5b 48 31 32 32 38 37 5d 20 5b 48 31 32  5] [H12287] [H12
1be00 32 38 38 5d 20 5b 48 31 32 32 38 39 5d 0a 2a 2a  288] [H12289].**
1be10 20 5b 48 31 32 32 39 30 5d 0a 2a 2f 0a 53 51 4c   [H12290].*/.SQL
1be20 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45  ITE_API SQLITE_E
1be30 58 50 45 52 49 4d 45 4e 54 41 4c 20 76 6f 69 64  XPERIMENTAL void
1be40 20 2a 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28   *sqlite3_trace(
1be50 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 28 2a  sqlite3*, void(*
1be60 78 54 72 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f  xTrace)(void*,co
1be70 6e 73 74 20 63 68 61 72 2a 29 2c 20 76 6f 69 64  nst char*), void
1be80 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53  *);.SQLITE_API S
1be90 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54  QLITE_EXPERIMENT
1bea0 41 4c 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  AL void *sqlite3
1beb0 5f 70 72 6f 66 69 6c 65 28 73 71 6c 69 74 65 33  _profile(sqlite3
1bec0 2a 2c 0a 20 20 20 76 6f 69 64 28 2a 78 50 72 6f  *,.   void(*xPro
1bed0 66 69 6c 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73  file)(void*,cons
1bee0 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 5f  t char*,sqlite3_
1bef0 75 69 6e 74 36 34 29 2c 20 76 6f 69 64 2a 29 3b  uint64), void*);
1bf00 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
1bf10 3a 20 51 75 65 72 79 20 50 72 6f 67 72 65 73 73  : Query Progress
1bf20 20 43 61 6c 6c 62 61 63 6b 73 20 7b 48 31 32 39   Callbacks {H129
1bf30 31 30 7d 20 3c 53 36 30 34 30 30 3e 0a 2a 2a 0a  10} <S60400>.**.
1bf40 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
1bf50 63 6f 6e 66 69 67 75 72 65 73 20 61 20 63 61 6c  configures a cal
1bf60 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2d  lback function -
1bf70 20 74 68 65 0a 2a 2a 20 70 72 6f 67 72 65 73 73   the.** progress
1bf80 20 63 61 6c 6c 62 61 63 6b 20 2d 20 74 68 61 74   callback - that
1bf90 20 69 73 20 69 6e 76 6f 6b 65 64 20 70 65 72 69   is invoked peri
1bfa0 6f 64 69 63 61 6c 6c 79 20 64 75 72 69 6e 67 20  odically during 
1bfb0 6c 6f 6e 67 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20  long.** running 
1bfc0 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65  calls to [sqlite
1bfd0 33 5f 65 78 65 63 28 29 5d 2c 20 5b 73 71 6c 69  3_exec()], [sqli
1bfe0 74 65 33 5f 73 74 65 70 28 29 5d 20 61 6e 64 0a  te3_step()] and.
1bff0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f  ** [sqlite3_get_
1c000 74 61 62 6c 65 28 29 5d 2e 20 20 41 6e 20 65 78  table()].  An ex
1c010 61 6d 70 6c 65 20 75 73 65 20 66 6f 72 20 74 68  ample use for th
1c020 69 73 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20  is.** interface 
1c030 69 73 20 74 6f 20 6b 65 65 70 20 61 20 47 55 49  is to keep a GUI
1c040 20 75 70 64 61 74 65 64 20 64 75 72 69 6e 67 20   updated during 
1c050 61 20 6c 61 72 67 65 20 71 75 65 72 79 2e 0a 2a  a large query..*
1c060 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 72 6f 67  *.** If the prog
1c070 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 72 65  ress callback re
1c080 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  turns non-zero, 
1c090 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 73  the operation is
1c0a0 0a 2a 2a 20 69 6e 74 65 72 72 75 70 74 65 64 2e  .** interrupted.
1c0b0 20 20 54 68 69 73 20 66 65 61 74 75 72 65 20 63    This feature c
1c0c0 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 69 6d  an be used to im
1c0d0 70 6c 65 6d 65 6e 74 20 61 0a 2a 2a 20 22 43 61  plement a.** "Ca
1c0e0 6e 63 65 6c 22 20 62 75 74 74 6f 6e 20 6f 6e 20  ncel" button on 
1c0f0 61 20 47 55 49 20 70 72 6f 67 72 65 73 73 20 64  a GUI progress d
1c100 69 61 6c 6f 67 20 62 6f 78 2e 0a 2a 2a 0a 2a 2a  ialog box..**.**
1c110 20 54 68 65 20 70 72 6f 67 72 65 73 73 20 68 61   The progress ha
1c120 6e 64 6c 65 72 20 6d 75 73 74 20 6e 6f 74 20 64  ndler must not d
1c130 6f 20 61 6e 79 74 68 69 6e 67 20 74 68 61 74 20  o anything that 
1c140 77 69 6c 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 74  will modify.** t
1c150 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
1c160 65 63 74 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f  ection that invo
1c170 6b 65 64 20 74 68 65 20 70 72 6f 67 72 65 73 73  ked the progress
1c180 20 68 61 6e 64 6c 65 72 2e 0a 2a 2a 20 4e 6f 74   handler..** Not
1c190 65 20 74 68 61 74 20 5b 73 71 6c 69 74 65 33 5f  e that [sqlite3_
1c1a0 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e  prepare_v2()] an
1c1b0 64 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  d [sqlite3_step(
1c1c0 29 5d 20 62 6f 74 68 20 6d 6f 64 69 66 79 20 74  )] both modify t
1c1d0 68 65 69 72 0a 2a 2a 20 64 61 74 61 62 61 73 65  heir.** database
1c1e0 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72   connections for
1c1f0 20 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20   the meaning of 
1c200 22 6d 6f 64 69 66 79 22 20 69 6e 20 74 68 69 73  "modify" in this
1c210 20 70 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a   paragraph..**.*
1c220 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
1c230 2a 2a 20 5b 48 31 32 39 31 31 5d 20 5b 48 31 32  ** [H12911] [H12
1c240 39 31 32 5d 20 5b 48 31 32 39 31 33 5d 20 5b 48  912] [H12913] [H
1c250 31 32 39 31 34 5d 20 5b 48 31 32 39 31 35 5d 20  12914] [H12915] 
1c260 5b 48 31 32 39 31 36 5d 20 5b 48 31 32 39 31 37  [H12916] [H12917
1c270 5d 20 5b 48 31 32 39 31 38 5d 0a 2a 2a 0a 2a 2f  ] [H12918].**.*/
1c280 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
1c290 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73   sqlite3_progres
1c2a0 73 5f 68 61 6e 64 6c 65 72 28 73 71 6c 69 74 65  s_handler(sqlite
1c2b0 33 2a 2c 20 69 6e 74 2c 20 69 6e 74 28 2a 29 28  3*, int, int(*)(
1c2c0 76 6f 69 64 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a  void*), void*);.
1c2d0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
1c2e0 20 4f 70 65 6e 69 6e 67 20 41 20 4e 65 77 20 44   Opening A New D
1c2f0 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69  atabase Connecti
1c300 6f 6e 20 7b 48 31 32 37 30 30 7d 20 3c 53 34 30  on {H12700} <S40
1c310 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  200>.**.** These
1c320 20 72 6f 75 74 69 6e 65 73 20 6f 70 65 6e 20 61   routines open a
1c330 6e 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73  n SQLite databas
1c340 65 20 66 69 6c 65 20 77 68 6f 73 65 20 6e 61 6d  e file whose nam
1c350 65 20 69 73 20 67 69 76 65 6e 20 62 79 20 74 68  e is given by th
1c360 65 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65 20 61 72  e.** filename ar
1c370 67 75 6d 65 6e 74 2e 20 54 68 65 20 66 69 6c 65  gument. The file
1c380 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 69 73  name argument is
1c390 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20   interpreted as 
1c3a0 55 54 46 2d 38 20 66 6f 72 0a 2a 2a 20 73 71 6c  UTF-8 for.** sql
1c3b0 69 74 65 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20  ite3_open() and 
1c3c0 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28  sqlite3_open_v2(
1c3d0 29 20 61 6e 64 20 61 73 20 55 54 46 2d 31 36 20  ) and as UTF-16 
1c3e0 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62 79  in the native by
1c3f0 74 65 0a 2a 2a 20 6f 72 64 65 72 20 66 6f 72 20  te.** order for 
1c400 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29  sqlite3_open16()
1c410 2e 20 41 20 5b 64 61 74 61 62 61 73 65 20 63 6f  . A [database co
1c420 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65  nnection] handle
1c430 20 69 73 20 75 73 75 61 6c 6c 79 0a 2a 2a 20 72   is usually.** r
1c440 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 70 44 62  eturned in *ppDb
1c450 2c 20 65 76 65 6e 20 69 66 20 61 6e 20 65 72 72  , even if an err
1c460 6f 72 20 6f 63 63 75 72 73 2e 20 20 54 68 65 20  or occurs.  The 
1c470 6f 6e 6c 79 20 65 78 63 65 70 74 69 6f 6e 20 69  only exception i
1c480 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 53 51 4c  s that.** if SQL
1c490 69 74 65 20 69 73 20 75 6e 61 62 6c 65 20 74 6f  ite is unable to
1c4a0 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79   allocate memory
1c4b0 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 5b 73 71   to hold the [sq
1c4c0 6c 69 74 65 33 5d 20 6f 62 6a 65 63 74 2c 0a 2a  lite3] object,.*
1c4d0 2a 20 61 20 4e 55 4c 4c 20 77 69 6c 6c 20 62 65  * a NULL will be
1c4e0 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70   written into *p
1c4f0 70 44 62 20 69 6e 73 74 65 61 64 20 6f 66 20 61  pDb instead of a
1c500 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
1c510 5b 73 71 6c 69 74 65 33 5d 0a 2a 2a 20 6f 62 6a  [sqlite3].** obj
1c520 65 63 74 2e 20 49 66 20 74 68 65 20 64 61 74 61  ect. If the data
1c530 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 28  base is opened (
1c540 61 6e 64 2f 6f 72 20 63 72 65 61 74 65 64 29 20  and/or created) 
1c550 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 74 68  successfully, th
1c560 65 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 4b  en.** [SQLITE_OK
1c570 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20  ] is returned.  
1c580 4f 74 68 65 72 77 69 73 65 20 61 6e 20 5b 65 72  Otherwise an [er
1c590 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74  ror code] is ret
1c5a0 75 72 6e 65 64 2e 20 20 54 68 65 0a 2a 2a 20 5b  urned.  The.** [
1c5b0 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29  sqlite3_errmsg()
1c5c0 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 65 72  ] or [sqlite3_er
1c5d0 72 6d 73 67 31 36 28 29 5d 20 72 6f 75 74 69 6e  rmsg16()] routin
1c5e0 65 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 74  es can be used t
1c5f0 6f 20 6f 62 74 61 69 6e 0a 2a 2a 20 61 6e 20 45  o obtain.** an E
1c600 6e 67 6c 69 73 68 20 6c 61 6e 67 75 61 67 65 20  nglish language 
1c610 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74  description of t
1c620 68 65 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20  he error..**.** 
1c630 54 68 65 20 64 65 66 61 75 6c 74 20 65 6e 63 6f  The default enco
1c640 64 69 6e 67 20 66 6f 72 20 74 68 65 20 64 61 74  ding for the dat
1c650 61 62 61 73 65 20 77 69 6c 6c 20 62 65 20 55 54  abase will be UT
1c660 46 2d 38 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65  F-8 if.** sqlite
1c670 33 5f 6f 70 65 6e 28 29 20 6f 72 20 73 71 6c 69  3_open() or sqli
1c680 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73  te3_open_v2() is
1c690 20 63 61 6c 6c 65 64 20 61 6e 64 0a 2a 2a 20 55   called and.** U
1c6a0 54 46 2d 31 36 20 69 6e 20 74 68 65 20 6e 61 74  TF-16 in the nat
1c6b0 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 20 69  ive byte order i
1c6c0 66 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36  f sqlite3_open16
1c6d0 28 29 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a  () is used..**.*
1c6e0 2a 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  * Whether or not
1c6f0 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
1c700 20 77 68 65 6e 20 69 74 20 69 73 20 6f 70 65 6e   when it is open
1c710 65 64 2c 20 72 65 73 6f 75 72 63 65 73 0a 2a 2a  ed, resources.**
1c720 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
1c730 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63   the [database c
1c740 6f 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c  onnection] handl
1c750 65 20 73 68 6f 75 6c 64 20 62 65 20 72 65 6c 65  e should be rele
1c760 61 73 65 64 20 62 79 0a 2a 2a 20 70 61 73 73 69  ased by.** passi
1c770 6e 67 20 69 74 20 74 6f 20 5b 73 71 6c 69 74 65  ng it to [sqlite
1c780 33 5f 63 6c 6f 73 65 28 29 5d 20 77 68 65 6e 20  3_close()] when 
1c790 69 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20  it is no longer 
1c7a0 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20  required..**.** 
1c7b0 54 68 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  The sqlite3_open
1c7c0 5f 76 32 28 29 20 69 6e 74 65 72 66 61 63 65 20  _v2() interface 
1c7d0 77 6f 72 6b 73 20 6c 69 6b 65 20 73 71 6c 69 74  works like sqlit
1c7e0 65 33 5f 6f 70 65 6e 28 29 0a 2a 2a 20 65 78 63  e3_open().** exc
1c7f0 65 70 74 20 74 68 61 74 20 69 74 20 61 63 63 65  ept that it acce
1c800 70 74 73 20 74 77 6f 20 61 64 64 69 74 69 6f 6e  pts two addition
1c810 61 6c 20 70 61 72 61 6d 65 74 65 72 73 20 66 6f  al parameters fo
1c820 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6e  r additional con
1c830 74 72 6f 6c 0a 2a 2a 20 6f 76 65 72 20 74 68 65  trol.** over the
1c840 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 63 6f   new database co
1c850 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 66  nnection.  The f
1c860 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 63  lags parameter c
1c870 61 6e 20 74 61 6b 65 20 6f 6e 65 20 6f 66 0a 2a  an take one of.*
1c880 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * the following 
1c890 74 68 72 65 65 20 76 61 6c 75 65 73 2c 20 6f 70  three values, op
1c8a0 74 69 6f 6e 61 6c 6c 79 20 63 6f 6d 62 69 6e 65  tionally combine
1c8b0 64 20 77 69 74 68 20 74 68 65 20 0a 2a 2a 20 5b  d with the .** [
1c8c0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55  SQLITE_OPEN_NOMU
1c8d0 54 45 58 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f 50  TEX], [SQLITE_OP
1c8e0 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 2c 20 5b  EN_FULLMUTEX], [
1c8f0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52  SQLITE_OPEN_SHAR
1c900 45 44 43 41 43 48 45 5d 2c 0a 2a 2a 20 61 6e 64  EDCACHE],.** and
1c910 2f 6f 72 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e  /or [SQLITE_OPEN
1c920 5f 50 52 49 56 41 54 45 43 41 43 48 45 5d 20 66  _PRIVATECACHE] f
1c930 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e  lags:.**.** <dl>
1c940 0a 2a 2a 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f  .** <dt>[SQLITE_
1c950 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 3c 2f  OPEN_READONLY]</
1c960 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64  dt>.** <dd>The d
1c970 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65  atabase is opene
1c980 64 20 69 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d  d in read-only m
1c990 6f 64 65 2e 20 20 49 66 20 74 68 65 20 64 61 74  ode.  If the dat
1c9a0 61 62 61 73 65 20 64 6f 65 73 20 6e 6f 74 0a 2a  abase does not.*
1c9b0 2a 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 2c  * already exist,
1c9c0 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74   an error is ret
1c9d0 75 72 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a  urned.</dd>.**.*
1c9e0 2a 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50  * <dt>[SQLITE_OP
1c9f0 45 4e 5f 52 45 41 44 57 52 49 54 45 5d 3c 2f 64  EN_READWRITE]</d
1ca00 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64 61  t>.** <dd>The da
1ca10 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64  tabase is opened
1ca20 20 66 6f 72 20 72 65 61 64 69 6e 67 20 61 6e 64   for reading and
1ca30 20 77 72 69 74 69 6e 67 20 69 66 20 70 6f 73 73   writing if poss
1ca40 69 62 6c 65 2c 20 6f 72 20 72 65 61 64 69 6e 67  ible, or reading
1ca50 0a 2a 2a 20 6f 6e 6c 79 20 69 66 20 74 68 65 20  .** only if the 
1ca60 66 69 6c 65 20 69 73 20 77 72 69 74 65 20 70 72  file is write pr
1ca70 6f 74 65 63 74 65 64 20 62 79 20 74 68 65 20 6f  otected by the o
1ca80 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e  perating system.
1ca90 20 20 49 6e 20 65 69 74 68 65 72 0a 2a 2a 20 63    In either.** c
1caa0 61 73 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ase the database
1cab0 20 6d 75 73 74 20 61 6c 72 65 61 64 79 20 65 78   must already ex
1cac0 69 73 74 2c 20 6f 74 68 65 72 77 69 73 65 20 61  ist, otherwise a
1cad0 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72  n error is retur
1cae0 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  ned.</dd>.**.** 
1caf0 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 45 4e  <dt>[SQLITE_OPEN
1cb00 5f 52 45 41 44 57 52 49 54 45 5d 20 7c 20 5b 53  _READWRITE] | [S
1cb10 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54  QLITE_OPEN_CREAT
1cb20 45 5d 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  E]</dt>.** <dd>T
1cb30 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6f  he database is o
1cb40 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 69 6e  pened for readin
1cb50 67 20 61 6e 64 20 77 72 69 74 69 6e 67 2c 20 61  g and writing, a
1cb60 6e 64 20 69 73 20 63 72 65 61 74 65 73 20 69 74  nd is creates it
1cb70 20 69 66 0a 2a 2a 20 69 74 20 64 6f 65 73 20 6e   if.** it does n
1cb80 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  ot already exist
1cb90 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 62 65  . This is the be
1cba0 68 61 76 69 6f 72 20 74 68 61 74 20 69 73 20 61  havior that is a
1cbb0 6c 77 61 79 73 20 75 73 65 64 20 66 6f 72 0a 2a  lways used for.*
1cbc0 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29  * sqlite3_open()
1cbd0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 65   and sqlite3_ope
1cbe0 6e 31 36 28 29 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c  n16().</dd>.** <
1cbf0 2f 64 6c 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  /dl>.**.** If th
1cc00 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20  e 3rd parameter 
1cc10 74 6f 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  to sqlite3_open_
1cc20 76 32 28 29 20 69 73 20 6e 6f 74 20 6f 6e 65 20  v2() is not one 
1cc30 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6d 62 69 6e  of the.** combin
1cc40 61 74 69 6f 6e 73 20 73 68 6f 77 6e 20 61 62 6f  ations shown abo
1cc50 76 65 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65  ve or one of the
1cc60 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 73 68   combinations sh
1cc70 6f 77 6e 20 61 62 6f 76 65 20 63 6f 6d 62 69 6e  own above combin
1cc80 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 5b  ed.** with the [
1cc90 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55  SQLITE_OPEN_NOMU
1cca0 54 45 58 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f 50  TEX], [SQLITE_OP
1ccb0 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 2c 0a 2a  EN_FULLMUTEX],.*
1ccc0 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53  * [SQLITE_OPEN_S
1ccd0 48 41 52 45 44 43 41 43 48 45 5d 20 61 6e 64 2f  HAREDCACHE] and/
1cce0 6f 72 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  or [SQLITE_OPEN_
1ccf0 53 48 41 52 45 44 43 41 43 48 45 5d 20 66 6c 61  SHAREDCACHE] fla
1cd00 67 73 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20  gs,.** then the 
1cd10 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65  behavior is unde
1cd20 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  fined..**.** If 
1cd30 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e  the [SQLITE_OPEN
1cd40 5f 4e 4f 4d 55 54 45 58 5d 20 66 6c 61 67 20 69  _NOMUTEX] flag i
1cd50 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20  s set, then the 
1cd60 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1cd70 69 6f 6e 0a 2a 2a 20 6f 70 65 6e 73 20 69 6e 20  ion.** opens in 
1cd80 74 68 65 20 6d 75 6c 74 69 2d 74 68 72 65 61 64  the multi-thread
1cd90 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65   [threading mode
1cda0 5d 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65  ] as long as the
1cdb0 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 0a 2a   single-thread.*
1cdc0 2a 20 6d 6f 64 65 20 68 61 73 20 6e 6f 74 20 62  * mode has not b
1cdd0 65 65 6e 20 73 65 74 20 61 74 20 63 6f 6d 70 69  een set at compi
1cde0 6c 65 2d 74 69 6d 65 20 6f 72 20 73 74 61 72 74  le-time or start
1cdf0 2d 74 69 6d 65 2e 20 20 49 66 20 74 68 65 0a 2a  -time.  If the.*
1ce00 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46  * [SQLITE_OPEN_F
1ce10 55 4c 4c 4d 55 54 45 58 5d 20 66 6c 61 67 20 69  ULLMUTEX] flag i
1ce20 73 20 73 65 74 20 74 68 65 6e 20 74 68 65 20 64  s set then the d
1ce30 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
1ce40 6f 6e 20 6f 70 65 6e 73 0a 2a 2a 20 69 6e 20 74  on opens.** in t
1ce50 68 65 20 73 65 72 69 61 6c 69 7a 65 64 20 5b 74  he serialized [t
1ce60 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 75  hreading mode] u
1ce70 6e 6c 65 73 73 20 73 69 6e 67 6c 65 2d 74 68 72  nless single-thr
1ce80 65 61 64 20 77 61 73 0a 2a 2a 20 70 72 65 76 69  ead was.** previ
1ce90 6f 75 73 6c 79 20 73 65 6c 65 63 74 65 64 20 61  ously selected a
1cea0 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f  t compile-time o
1ceb0 72 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a  r start-time..**
1cec0 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45   The [SQLITE_OPE
1ced0 4e 5f 53 48 41 52 45 44 43 41 43 48 45 5d 20 66  N_SHAREDCACHE] f
1cee0 6c 61 67 20 63 61 75 73 65 73 20 74 68 65 20 64  lag causes the d
1cef0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
1cf00 6f 6e 20 74 6f 20 62 65 0a 2a 2a 20 65 6c 69 67  on to be.** elig
1cf10 69 62 6c 65 20 74 6f 20 75 73 65 20 5b 73 68 61  ible to use [sha
1cf20 72 65 64 20 63 61 63 68 65 20 6d 6f 64 65 5d 2c  red cache mode],
1cf30 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77   regardless of w
1cf40 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 73 68  hether or not sh
1cf50 61 72 65 64 0a 2a 2a 20 63 61 63 68 65 20 69 73  ared.** cache is
1cf60 20 65 6e 61 62 6c 65 64 20 75 73 69 6e 67 20 5b   enabled using [
1cf70 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73  sqlite3_enable_s
1cf80 68 61 72 65 64 5f 63 61 63 68 65 28 29 5d 2e 20  hared_cache()]. 
1cf90 20 54 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f   The.** [SQLITE_
1cfa0 4f 50 45 4e 5f 50 52 49 56 41 54 45 43 41 43 48  OPEN_PRIVATECACH
1cfb0 45 5d 20 66 6c 61 67 20 63 61 75 73 65 73 20 74  E] flag causes t
1cfc0 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
1cfd0 65 63 74 69 6f 6e 20 74 6f 20 6e 6f 74 0a 2a 2a  ection to not.**
1cfe0 20 70 61 72 74 69 63 69 70 61 74 65 20 69 6e 20   participate in 
1cff0 5b 73 68 61 72 65 64 20 63 61 63 68 65 20 6d 6f  [shared cache mo
1d000 64 65 5d 20 65 76 65 6e 20 69 66 20 69 74 20 69  de] even if it i
1d010 73 20 65 6e 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a  s enabled..**.**
1d020 20 49 66 20 74 68 65 20 66 69 6c 65 6e 61 6d 65   If the filename
1d030 20 69 73 20 22 3a 6d 65 6d 6f 72 79 3a 22 2c 20   is ":memory:", 
1d040 74 68 65 6e 20 61 20 70 72 69 76 61 74 65 2c 20  then a private, 
1d050 74 65 6d 70 6f 72 61 72 79 20 69 6e 2d 6d 65 6d  temporary in-mem
1d060 6f 72 79 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  ory database.** 
1d070 69 73 20 63 72 65 61 74 65 64 20 66 6f 72 20 74  is created for t
1d080 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20  he connection.  
1d090 54 68 69 73 20 69 6e 2d 6d 65 6d 6f 72 79 20 64  This in-memory d
1d0a0 61 74 61 62 61 73 65 20 77 69 6c 6c 20 76 61 6e  atabase will van
1d0b0 69 73 68 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20  ish when.** the 
1d0c0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1d0d0 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 20 20  ion is closed.  
1d0e0 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20  Future versions 
1d0f0 6f 66 20 53 51 4c 69 74 65 20 6d 69 67 68 74 0a  of SQLite might.
1d100 2a 2a 20 6d 61 6b 65 20 75 73 65 20 6f 66 20 61  ** make use of a
1d110 64 64 69 74 69 6f 6e 61 6c 20 73 70 65 63 69 61  dditional specia
1d120 6c 20 66 69 6c 65 6e 61 6d 65 73 20 74 68 61 74  l filenames that
1d130 20 62 65 67 69 6e 20 77 69 74 68 20 74 68 65 20   begin with the 
1d140 22 3a 22 20 63 68 61 72 61 63 74 65 72 2e 0a 2a  ":" character..*
1d150 2a 20 49 74 20 69 73 20 72 65 63 6f 6d 6d 65 6e  * It is recommen
1d160 64 65 64 20 74 68 61 74 20 77 68 65 6e 20 61 20  ded that when a 
1d170 64 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d  database filenam
1d180 65 20 61 63 74 75 61 6c 6c 79 20 64 6f 65 73 20  e actually does 
1d190 62 65 67 69 6e 20 77 69 74 68 0a 2a 2a 20 61 20  begin with.** a 
1d1a0 22 3a 22 20 63 68 61 72 61 63 74 65 72 20 79 6f  ":" character yo
1d1b0 75 20 73 68 6f 75 6c 64 20 70 72 65 66 69 78 20  u should prefix 
1d1c0 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 77 69 74  the filename wit
1d1d0 68 20 61 20 70 61 74 68 6e 61 6d 65 20 73 75 63  h a pathname suc
1d1e0 68 20 61 73 0a 2a 2a 20 22 2e 2f 22 20 74 6f 20  h as.** "./" to 
1d1f0 61 76 6f 69 64 20 61 6d 62 69 67 75 69 74 79 2e  avoid ambiguity.
1d200 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69  .**.** If the fi
1d210 6c 65 6e 61 6d 65 20 69 73 20 61 6e 20 65 6d 70  lename is an emp
1d220 74 79 20 73 74 72 69 6e 67 2c 20 74 68 65 6e 20  ty string, then 
1d230 61 20 70 72 69 76 61 74 65 2c 20 74 65 6d 70 6f  a private, tempo
1d240 72 61 72 79 0a 2a 2a 20 6f 6e 2d 64 69 73 6b 20  rary.** on-disk 
1d250 64 61 74 61 62 61 73 65 20 77 69 6c 6c 20 62 65  database will be
1d260 20 63 72 65 61 74 65 64 2e 20 20 54 68 69 73 20   created.  This 
1d270 70 72 69 76 61 74 65 20 64 61 74 61 62 61 73 65  private database
1d280 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 61 75 74 6f   will be.** auto
1d290 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c 65 74 65  matically delete
1d2a0 64 20 61 73 20 73 6f 6f 6e 20 61 73 20 74 68 65  d as soon as the
1d2b0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
1d2c0 74 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0a  tion is closed..
1d2d0 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 75 72 74 68  **.** The fourth
1d2e0 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71   parameter to sq
1d2f0 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20  lite3_open_v2() 
1d300 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  is the name of t
1d310 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76  he.** [sqlite3_v
1d320 66 73 5d 20 6f 62 6a 65 63 74 20 74 68 61 74 20  fs] object that 
1d330 64 65 66 69 6e 65 73 20 74 68 65 20 6f 70 65 72  defines the oper
1d340 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e 74  ating system int
1d350 65 72 66 61 63 65 20 74 68 61 74 0a 2a 2a 20 74  erface that.** t
1d360 68 65 20 6e 65 77 20 64 61 74 61 62 61 73 65 20  he new database 
1d370 63 6f 6e 6e 65 63 74 69 6f 6e 20 73 68 6f 75 6c  connection shoul
1d380 64 20 75 73 65 2e 20 20 49 66 20 74 68 65 20 66  d use.  If the f
1d390 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 20  ourth parameter 
1d3a0 69 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f 69  is.** a NULL poi
1d3b0 6e 74 65 72 20 74 68 65 6e 20 74 68 65 20 64 65  nter then the de
1d3c0 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76  fault [sqlite3_v
1d3d0 66 73 5d 20 6f 62 6a 65 63 74 20 69 73 20 75 73  fs] object is us
1d3e0 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 4e 6f 74  ed..**.** <b>Not
1d3f0 65 20 74 6f 20 57 69 6e 64 6f 77 73 20 75 73 65  e to Windows use
1d400 72 73 3a 3c 2f 62 3e 20 20 54 68 65 20 65 6e 63  rs:</b>  The enc
1d410 6f 64 69 6e 67 20 75 73 65 64 20 66 6f 72 20 74  oding used for t
1d420 68 65 20 66 69 6c 65 6e 61 6d 65 20 61 72 67 75  he filename argu
1d430 6d 65 6e 74 0a 2a 2a 20 6f 66 20 73 71 6c 69 74  ment.** of sqlit
1d440 65 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73 71  e3_open() and sq
1d450 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20  lite3_open_v2() 
1d460 6d 75 73 74 20 62 65 20 55 54 46 2d 38 2c 20 6e  must be UTF-8, n
1d470 6f 74 20 77 68 61 74 65 76 65 72 0a 2a 2a 20 63  ot whatever.** c
1d480 6f 64 65 70 61 67 65 20 69 73 20 63 75 72 72 65  odepage is curre
1d490 6e 74 6c 79 20 64 65 66 69 6e 65 64 2e 20 20 46  ntly defined.  F
1d4a0 69 6c 65 6e 61 6d 65 73 20 63 6f 6e 74 61 69 6e  ilenames contain
1d4b0 69 6e 67 20 69 6e 74 65 72 6e 61 74 69 6f 6e 61  ing internationa
1d4c0 6c 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73 20  l.** characters 
1d4d0 6d 75 73 74 20 62 65 20 63 6f 6e 76 65 72 74 65  must be converte
1d4e0 64 20 74 6f 20 55 54 46 2d 38 20 70 72 69 6f 72  d to UTF-8 prior
1d4f0 20 74 6f 20 70 61 73 73 69 6e 67 20 74 68 65 6d   to passing them
1d500 20 69 6e 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33   into.** sqlite3
1d510 5f 6f 70 65 6e 28 29 20 6f 72 20 73 71 6c 69 74  _open() or sqlit
1d520 65 33 5f 6f 70 65 6e 5f 76 32 28 29 2e 0a 2a 2a  e3_open_v2()..**
1d530 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
1d540 3a 0a 2a 2a 20 5b 48 31 32 37 30 31 5d 20 5b 48  :.** [H12701] [H
1d550 31 32 37 30 32 5d 20 5b 48 31 32 37 30 33 5d 20  12702] [H12703] 
1d560 5b 48 31 32 37 30 34 5d 20 5b 48 31 32 37 30 36  [H12704] [H12706
1d570 5d 20 5b 48 31 32 37 30 37 5d 20 5b 48 31 32 37  ] [H12707] [H127
1d580 30 39 5d 20 5b 48 31 32 37 31 31 5d 0a 2a 2a 20  09] [H12711].** 
1d590 5b 48 31 32 37 31 32 5d 20 5b 48 31 32 37 31 33  [H12712] [H12713
1d5a0 5d 20 5b 48 31 32 37 31 34 5d 20 5b 48 31 32 37  ] [H12714] [H127
1d5b0 31 37 5d 20 5b 48 31 32 37 31 39 5d 20 5b 48 31  17] [H12719] [H1
1d5c0 32 37 32 31 5d 20 5b 48 31 32 37 32 33 5d 0a 2a  2721] [H12723].*
1d5d0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1d5e0 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 0a 20   sqlite3_open(. 
1d5f0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c   const char *fil
1d600 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 44 61 74 61  ename,   /* Data
1d610 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 28 55  base filename (U
1d620 54 46 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74  TF-8) */.  sqlit
1d630 65 33 20 2a 2a 70 70 44 62 20 20 20 20 20 20 20  e3 **ppDb       
1d640 20 20 20 2f 2a 20 4f 55 54 3a 20 53 51 4c 69 74     /* OUT: SQLit
1d650 65 20 64 62 20 68 61 6e 64 6c 65 20 2a 2f 0a 29  e db handle */.)
1d660 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
1d670 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28   sqlite3_open16(
1d680 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 66  .  const void *f
1d690 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 44 61  ilename,   /* Da
1d6a0 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20  tabase filename 
1d6b0 28 55 54 46 2d 31 36 29 20 2a 2f 0a 20 20 73 71  (UTF-16) */.  sq
1d6c0 6c 69 74 65 33 20 2a 2a 70 70 44 62 20 20 20 20  lite3 **ppDb    
1d6d0 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 51        /* OUT: SQ
1d6e0 4c 69 74 65 20 64 62 20 68 61 6e 64 6c 65 20 2a  Lite db handle *
1d6f0 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  /.);.SQLITE_API 
1d700 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  int sqlite3_open
1d710 5f 76 32 28 0a 20 20 63 6f 6e 73 74 20 63 68 61  _v2(.  const cha
1d720 72 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20 20 2f  r *filename,   /
1d730 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 6e  * Database filen
1d740 61 6d 65 20 28 55 54 46 2d 38 29 20 2a 2f 0a 20  ame (UTF-8) */. 
1d750 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 2c   sqlite3 **ppDb,
1d760 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
1d770 20 53 51 4c 69 74 65 20 64 62 20 68 61 6e 64 6c   SQLite db handl
1d780 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73  e */.  int flags
1d790 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
1d7a0 2a 20 46 6c 61 67 73 20 2a 2f 0a 20 20 63 6f 6e  * Flags */.  con
1d7b0 73 74 20 63 68 61 72 20 2a 7a 56 66 73 20 20 20  st char *zVfs   
1d7c0 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
1d7d0 56 46 53 20 6d 6f 64 75 6c 65 20 74 6f 20 75 73  VFS module to us
1d7e0 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  e */.);../*.** C
1d7f0 41 50 49 33 52 45 46 3a 20 45 72 72 6f 72 20 43  API3REF: Error C
1d800 6f 64 65 73 20 41 6e 64 20 4d 65 73 73 61 67 65  odes And Message
1d810 73 20 7b 48 31 32 38 30 30 7d 20 3c 53 36 30 32  s {H12800} <S602
1d820 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  00>.**.** The sq
1d830 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 20  lite3_errcode() 
1d840 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e  interface return
1d850 73 20 74 68 65 20 6e 75 6d 65 72 69 63 20 5b 72  s the numeric [r
1d860 65 73 75 6c 74 20 63 6f 64 65 5d 20 6f 72 0a 2a  esult code] or.*
1d870 2a 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73 75  * [extended resu
1d880 6c 74 20 63 6f 64 65 5d 20 66 6f 72 20 74 68 65  lt code] for the
1d890 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 66 61 69   most recent fai
1d8a0 6c 65 64 20 73 71 6c 69 74 65 33 5f 2a 20 41 50  led sqlite3_* AP
1d8b0 49 20 63 61 6c 6c 0a 2a 2a 20 61 73 73 6f 63 69  I call.** associ
1d8c0 61 74 65 64 20 77 69 74 68 20 61 20 5b 64 61 74  ated with a [dat
1d8d0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1d8e0 5d 2e 20 49 66 20 61 20 70 72 69 6f 72 20 41 50  ]. If a prior AP
1d8f0 49 20 63 61 6c 6c 20 66 61 69 6c 65 64 0a 2a 2a  I call failed.**
1d900 20 62 75 74 20 74 68 65 20 6d 6f 73 74 20 72 65   but the most re
1d910 63 65 6e 74 20 41 50 49 20 63 61 6c 6c 20 73 75  cent API call su
1d920 63 63 65 65 64 65 64 2c 20 74 68 65 20 72 65 74  cceeded, the ret
1d930 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 0a 2a  urn value from.*
1d940 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64  * sqlite3_errcod
1d950 65 28 29 20 69 73 20 75 6e 64 65 66 69 6e 65 64  e() is undefined
1d960 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65  .  The sqlite3_e
1d970 78 74 65 6e 64 65 64 5f 65 72 72 63 6f 64 65 28  xtended_errcode(
1d980 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69  ).** interface i
1d990 73 20 74 68 65 20 73 61 6d 65 20 65 78 63 65 70  s the same excep
1d9a0 74 20 74 68 61 74 20 69 74 20 61 6c 77 61 79 73  t that it always
1d9b0 20 72 65 74 75 72 6e 73 20 74 68 65 20 0a 2a 2a   returns the .**
1d9c0 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c   [extended resul
1d9d0 74 20 63 6f 64 65 5d 20 65 76 65 6e 20 77 68 65  t code] even whe
1d9e0 6e 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c  n extended resul
1d9f0 74 20 63 6f 64 65 73 20 61 72 65 0a 2a 2a 20 64  t codes are.** d
1da00 69 73 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  isabled..**.** T
1da10 68 65 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  he sqlite3_errms
1da20 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  g() and sqlite3_
1da30 65 72 72 6d 73 67 31 36 28 29 20 72 65 74 75 72  errmsg16() retur
1da40 6e 20 45 6e 67 6c 69 73 68 2d 6c 61 6e 67 75 61  n English-langua
1da50 67 65 0a 2a 2a 20 74 65 78 74 20 74 68 61 74 20  ge.** text that 
1da60 64 65 73 63 72 69 62 65 73 20 74 68 65 20 65 72  describes the er
1da70 72 6f 72 2c 20 61 73 20 65 69 74 68 65 72 20 55  ror, as either U
1da80 54 46 2d 38 20 6f 72 20 55 54 46 2d 31 36 20 72  TF-8 or UTF-16 r
1da90 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20  espectively..** 
1daa0 4d 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74  Memory to hold t
1dab0 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  he error message
1dac0 20 73 74 72 69 6e 67 20 69 73 20 6d 61 6e 61 67   string is manag
1dad0 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 2a  ed internally..*
1dae0 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f  * The applicatio
1daf0 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20  n does not need 
1db00 74 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 20 66  to worry about f
1db10 72 65 65 69 6e 67 20 74 68 65 20 72 65 73 75 6c  reeing the resul
1db20 74 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74  t..** However, t
1db30 68 65 20 65 72 72 6f 72 20 73 74 72 69 6e 67 20  he error string 
1db40 6d 69 67 68 74 20 62 65 20 6f 76 65 72 77 72 69  might be overwri
1db50 74 74 65 6e 20 6f 72 20 64 65 61 6c 6c 6f 63 61  tten or dealloca
1db60 74 65 64 20 62 79 0a 2a 2a 20 73 75 62 73 65 71  ted by.** subseq
1db70 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 6f 74  uent calls to ot
1db80 68 65 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72  her SQLite inter
1db90 66 61 63 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a  face functions..
1dba0 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 73  **.** When the s
1dbb0 65 72 69 61 6c 69 7a 65 64 20 5b 74 68 72 65 61  erialized [threa
1dbc0 64 69 6e 67 20 6d 6f 64 65 5d 20 69 73 20 69 6e  ding mode] is in
1dbd0 20 75 73 65 2c 20 69 74 20 6d 69 67 68 74 20 62   use, it might b
1dbe0 65 20 74 68 65 0a 2a 2a 20 63 61 73 65 20 74 68  e the.** case th
1dbf0 61 74 20 61 20 73 65 63 6f 6e 64 20 65 72 72 6f  at a second erro
1dc00 72 20 6f 63 63 75 72 73 20 6f 6e 20 61 20 73 65  r occurs on a se
1dc10 70 61 72 61 74 65 20 74 68 72 65 61 64 20 69 6e  parate thread in
1dc20 20 62 65 74 77 65 65 6e 0a 2a 2a 20 74 68 65 20   between.** the 
1dc30 74 69 6d 65 20 6f 66 20 74 68 65 20 66 69 72 73  time of the firs
1dc40 74 20 65 72 72 6f 72 20 61 6e 64 20 74 68 65 20  t error and the 
1dc50 63 61 6c 6c 20 74 6f 20 74 68 65 73 65 20 69 6e  call to these in
1dc60 74 65 72 66 61 63 65 73 2e 0a 2a 2a 20 57 68 65  terfaces..** Whe
1dc70 6e 20 74 68 61 74 20 68 61 70 70 65 6e 73 2c 20  n that happens, 
1dc80 74 68 65 20 73 65 63 6f 6e 64 20 65 72 72 6f 72  the second error
1dc90 20 77 69 6c 6c 20 62 65 20 72 65 70 6f 72 74 65   will be reporte
1dca0 64 20 73 69 6e 63 65 20 74 68 65 73 65 0a 2a 2a  d since these.**
1dcb0 20 69 6e 74 65 72 66 61 63 65 73 20 61 6c 77 61   interfaces alwa
1dcc0 79 73 20 72 65 70 6f 72 74 20 74 68 65 20 6d 6f  ys report the mo
1dcd0 73 74 20 72 65 63 65 6e 74 20 72 65 73 75 6c 74  st recent result
1dce0 2e 20 20 54 6f 20 61 76 6f 69 64 0a 2a 2a 20 74  .  To avoid.** t
1dcf0 68 69 73 2c 20 65 61 63 68 20 74 68 72 65 61 64  his, each thread
1dd00 20 63 61 6e 20 6f 62 74 61 69 6e 20 65 78 63 6c   can obtain excl
1dd10 75 73 69 76 65 20 75 73 65 20 6f 66 20 74 68 65  usive use of the
1dd20 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
1dd30 63 74 69 6f 6e 5d 20 44 0a 2a 2a 20 62 79 20 69  ction] D.** by i
1dd40 6e 76 6f 6b 69 6e 67 20 5b 73 71 6c 69 74 65 33  nvoking [sqlite3
1dd50 5f 6d 75 74 65 78 5f 65 6e 74 65 72 5d 28 5b 73  _mutex_enter]([s
1dd60 71 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65 78 5d  qlite3_db_mutex]
1dd70 28 44 29 29 20 62 65 66 6f 72 65 20 62 65 67 69  (D)) before begi
1dd80 6e 6e 69 6e 67 0a 2a 2a 20 74 6f 20 75 73 65 20  nning.** to use 
1dd90 44 20 61 6e 64 20 69 6e 76 6f 6b 69 6e 67 20 5b  D and invoking [
1dda0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
1ddb0 61 76 65 5d 28 5b 73 71 6c 69 74 65 33 5f 64 62  ave]([sqlite3_db
1ddc0 5f 6d 75 74 65 78 5d 28 44 29 29 20 61 66 74 65  _mutex](D)) afte
1ddd0 72 0a 2a 2a 20 61 6c 6c 20 63 61 6c 6c 73 20 74  r.** all calls t
1dde0 6f 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 73  o the interfaces
1ddf0 20 6c 69 73 74 65 64 20 68 65 72 65 20 61 72 65   listed here are
1de00 20 63 6f 6d 70 6c 65 74 65 64 2e 0a 2a 2a 0a 2a   completed..**.*
1de10 2a 20 49 66 20 61 6e 20 69 6e 74 65 72 66 61 63  * If an interfac
1de20 65 20 66 61 69 6c 73 20 77 69 74 68 20 53 51 4c  e fails with SQL
1de30 49 54 45 5f 4d 49 53 55 53 45 2c 20 74 68 61 74  ITE_MISUSE, that
1de40 20 6d 65 61 6e 73 20 74 68 65 20 69 6e 74 65 72   means the inter
1de50 66 61 63 65 0a 2a 2a 20 77 61 73 20 69 6e 76 6f  face.** was invo
1de60 6b 65 64 20 69 6e 63 6f 72 72 65 63 74 6c 79 20  ked incorrectly 
1de70 62 79 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  by the applicati
1de80 6f 6e 2e 20 20 49 6e 20 74 68 61 74 20 63 61 73  on.  In that cas
1de90 65 2c 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20  e, the.** error 
1dea0 63 6f 64 65 20 61 6e 64 20 6d 65 73 73 61 67 65  code and message
1deb0 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20   may or may not 
1dec0 62 65 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 52 65  be set..**.** Re
1ded0 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b  quirements:.** [
1dee0 48 31 32 38 30 31 5d 20 5b 48 31 32 38 30 32 5d  H12801] [H12802]
1def0 20 5b 48 31 32 38 30 33 5d 20 5b 48 31 32 38 30   [H12803] [H1280
1df00 37 5d 20 5b 48 31 32 38 30 38 5d 20 5b 48 31 32  7] [H12808] [H12
1df10 38 30 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  809].*/.SQLITE_A
1df20 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65  PI int sqlite3_e
1df30 72 72 63 6f 64 65 28 73 71 6c 69 74 65 33 20 2a  rrcode(sqlite3 *
1df40 64 62 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  db);.SQLITE_API 
1df50 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 74 65  int sqlite3_exte
1df60 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 73 71 6c  nded_errcode(sql
1df70 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54  ite3 *db);.SQLIT
1df80 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72  E_API const char
1df90 20 2a 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67   *sqlite3_errmsg
1dfa0 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49  (sqlite3*);.SQLI
1dfb0 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69  TE_API const voi
1dfc0 64 20 2a 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  d *sqlite3_errms
1dfd0 67 31 36 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a  g16(sqlite3*);..
1dfe0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
1dff0 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 4f 62  SQL Statement Ob
1e000 6a 65 63 74 20 7b 48 31 33 30 30 30 7d 20 3c 48  ject {H13000} <H
1e010 31 33 30 31 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52  13010>.** KEYWOR
1e020 44 53 3a 20 7b 70 72 65 70 61 72 65 64 20 73 74  DS: {prepared st
1e030 61 74 65 6d 65 6e 74 7d 20 7b 70 72 65 70 61 72  atement} {prepar
1e040 65 64 20 73 74 61 74 65 6d 65 6e 74 73 7d 0a 2a  ed statements}.*
1e050 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
1e060 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20   of this object 
1e070 72 65 70 72 65 73 65 6e 74 73 20 61 20 73 69 6e  represents a sin
1e080 67 6c 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  gle SQL statemen
1e090 74 2e 0a 2a 2a 20 54 68 69 73 20 6f 62 6a 65 63  t..** This objec
1e0a0 74 20 69 73 20 76 61 72 69 6f 75 73 6c 79 20 6b  t is variously k
1e0b0 6e 6f 77 6e 20 61 73 20 61 20 22 70 72 65 70 61  nown as a "prepa
1e0c0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 22 20 6f  red statement" o
1e0d0 72 20 61 0a 2a 2a 20 22 63 6f 6d 70 69 6c 65 64  r a.** "compiled
1e0e0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 22 20   SQL statement" 
1e0f0 6f 72 20 73 69 6d 70 6c 79 20 61 73 20 61 20 22  or simply as a "
1e100 73 74 61 74 65 6d 65 6e 74 22 2e 0a 2a 2a 0a 2a  statement"..**.*
1e110 2a 20 54 68 65 20 6c 69 66 65 20 6f 66 20 61 20  * The life of a 
1e120 73 74 61 74 65 6d 65 6e 74 20 6f 62 6a 65 63 74  statement object
1e130 20 67 6f 65 73 20 73 6f 6d 65 74 68 69 6e 67 20   goes something 
1e140 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a  like this:.**.**
1e150 20 3c 6f 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 43 72   <ol>.** <li> Cr
1e160 65 61 74 65 20 74 68 65 20 6f 62 6a 65 63 74 20  eate the object 
1e170 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70  using [sqlite3_p
1e180 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20  repare_v2()] or 
1e190 61 20 72 65 6c 61 74 65 64 0a 2a 2a 20 20 20 20  a related.**    
1e1a0 20 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 3c    function..** <
1e1b0 6c 69 3e 20 42 69 6e 64 20 76 61 6c 75 65 73 20  li> Bind values 
1e1c0 74 6f 20 5b 68 6f 73 74 20 70 61 72 61 6d 65 74  to [host paramet
1e1d0 65 72 73 5d 20 75 73 69 6e 67 20 74 68 65 20 73  ers] using the s
1e1e0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 0a  qlite3_bind_*().
1e1f0 2a 2a 20 20 20 20 20 20 69 6e 74 65 72 66 61 63  **      interfac
1e200 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 75 6e 20  es..** <li> Run 
1e210 74 68 65 20 53 51 4c 20 62 79 20 63 61 6c 6c 69  the SQL by calli
1e220 6e 67 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  ng [sqlite3_step
1e230 28 29 5d 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  ()] one or more 
1e240 74 69 6d 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 52  times..** <li> R
1e250 65 73 65 74 20 74 68 65 20 73 74 61 74 65 6d 65  eset the stateme
1e260 6e 74 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65  nt using [sqlite
1e270 33 5f 72 65 73 65 74 28 29 5d 20 74 68 65 6e 20  3_reset()] then 
1e280 67 6f 20 62 61 63 6b 0a 2a 2a 20 20 20 20 20 20  go back.**      
1e290 74 6f 20 73 74 65 70 20 32 2e 20 20 44 6f 20 74  to step 2.  Do t
1e2a0 68 69 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65  his zero or more
1e2b0 20 74 69 6d 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20   times..** <li> 
1e2c0 44 65 73 74 72 6f 79 20 74 68 65 20 6f 62 6a 65  Destroy the obje
1e2d0 63 74 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65  ct using [sqlite
1e2e0 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a  3_finalize()]..*
1e2f0 2a 20 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 52 65  * </ol>.**.** Re
1e300 66 65 72 20 74 6f 20 64 6f 63 75 6d 65 6e 74 61  fer to documenta
1e310 74 69 6f 6e 20 6f 6e 20 69 6e 64 69 76 69 64 75  tion on individu
1e320 61 6c 20 6d 65 74 68 6f 64 73 20 61 62 6f 76 65  al methods above
1e330 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a   for additional.
1e340 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  ** information..
1e350 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
1e360 74 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 73  t sqlite3_stmt s
1e370 71 6c 69 74 65 33 5f 73 74 6d 74 3b 0a 0a 2f 2a  qlite3_stmt;../*
1e380 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 75  .** CAPI3REF: Ru
1e390 6e 2d 74 69 6d 65 20 4c 69 6d 69 74 73 20 7b 48  n-time Limits {H
1e3a0 31 32 37 36 30 7d 20 3c 53 32 30 36 30 30 3e 0a  12760} <S20600>.
1e3b0 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72  **.** This inter
1e3c0 66 61 63 65 20 61 6c 6c 6f 77 73 20 74 68 65 20  face allows the 
1e3d0 73 69 7a 65 20 6f 66 20 76 61 72 69 6f 75 73 20  size of various 
1e3e0 63 6f 6e 73 74 72 75 63 74 73 20 74 6f 20 62 65  constructs to be
1e3f0 20 6c 69 6d 69 74 65 64 0a 2a 2a 20 6f 6e 20 61   limited.** on a
1e400 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 62 79 20 63   connection by c
1e410 6f 6e 6e 65 63 74 69 6f 6e 20 62 61 73 69 73 2e  onnection basis.
1e420 20 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61    The first para
1e430 6d 65 74 65 72 20 69 73 20 74 68 65 0a 2a 2a 20  meter is the.** 
1e440 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
1e450 74 69 6f 6e 5d 20 77 68 6f 73 65 20 6c 69 6d 69  tion] whose limi
1e460 74 20 69 73 20 74 6f 20 62 65 20 73 65 74 20 6f  t is to be set o
1e470 72 20 71 75 65 72 69 65 64 2e 20 20 54 68 65 0a  r queried.  The.
1e480 2a 2a 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65  ** second parame
1e490 74 65 72 20 69 73 20 6f 6e 65 20 6f 66 20 74 68  ter is one of th
1e4a0 65 20 5b 6c 69 6d 69 74 20 63 61 74 65 67 6f 72  e [limit categor
1e4b0 69 65 73 5d 20 74 68 61 74 20 64 65 66 69 6e 65  ies] that define
1e4c0 20 61 0a 2a 2a 20 63 6c 61 73 73 20 6f 66 20 63   a.** class of c
1e4d0 6f 6e 73 74 72 75 63 74 73 20 74 6f 20 62 65 20  onstructs to be 
1e4e0 73 69 7a 65 20 6c 69 6d 69 74 65 64 2e 20 20 54  size limited.  T
1e4f0 68 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74  he third paramet
1e500 65 72 20 69 73 20 74 68 65 0a 2a 2a 20 6e 65 77  er is the.** new
1e510 20 6c 69 6d 69 74 20 66 6f 72 20 74 68 61 74 20   limit for that 
1e520 63 6f 6e 73 74 72 75 63 74 2e 20 20 54 68 65 20  construct.  The 
1e530 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
1e540 20 74 68 65 20 6f 6c 64 20 6c 69 6d 69 74 2e 0a   the old limit..
1e550 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 65 77  **.** If the new
1e560 20 6c 69 6d 69 74 20 69 73 20 61 20 6e 65 67 61   limit is a nega
1e570 74 69 76 65 20 6e 75 6d 62 65 72 2c 20 74 68 65  tive number, the
1e580 20 6c 69 6d 69 74 20 69 73 20 75 6e 63 68 61 6e   limit is unchan
1e590 67 65 64 2e 0a 2a 2a 20 46 6f 72 20 74 68 65 20  ged..** For the 
1e5a0 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 79 20 6f  limit category o
1e5b0 66 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 58  f SQLITE_LIMIT_X
1e5c0 59 5a 20 74 68 65 72 65 20 69 73 20 61 20 0a 2a  YZ there is a .*
1e5d0 2a 20 5b 6c 69 6d 69 74 73 20 7c 20 68 61 72 64  * [limits | hard
1e5e0 20 75 70 70 65 72 20 62 6f 75 6e 64 5d 0a 2a 2a   upper bound].**
1e5f0 20 73 65 74 20 62 79 20 61 20 63 6f 6d 70 69 6c   set by a compil
1e600 65 2d 74 69 6d 65 20 43 20 70 72 65 70 72 6f 63  e-time C preproc
1e610 65 73 73 6f 72 20 6d 61 63 72 6f 20 6e 61 6d 65  essor macro name
1e620 64 20 0a 2a 2a 20 5b 6c 69 6d 69 74 73 20 7c 20  d .** [limits | 
1e630 53 51 4c 49 54 45 5f 4d 41 58 5f 58 59 5a 5d 2e  SQLITE_MAX_XYZ].
1e640 0a 2a 2a 20 28 54 68 65 20 22 5f 4c 49 4d 49 54  .** (The "_LIMIT
1e650 5f 22 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 69  _" in the name i
1e660 73 20 63 68 61 6e 67 65 64 20 74 6f 20 22 5f 4d  s changed to "_M
1e670 41 58 5f 22 2e 29 0a 2a 2a 20 41 74 74 65 6d 70  AX_".).** Attemp
1e680 74 73 20 74 6f 20 69 6e 63 72 65 61 73 65 20 61  ts to increase a
1e690 20 6c 69 6d 69 74 20 61 62 6f 76 65 20 69 74 73   limit above its
1e6a0 20 68 61 72 64 20 75 70 70 65 72 20 62 6f 75 6e   hard upper boun
1e6b0 64 20 61 72 65 0a 2a 2a 20 73 69 6c 65 6e 74 6c  d are.** silentl
1e6c0 79 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20 74  y truncated to t
1e6d0 68 65 20 68 61 72 64 20 75 70 70 65 72 20 6c 69  he hard upper li
1e6e0 6d 69 74 2e 0a 2a 2a 0a 2a 2a 20 52 75 6e 20 74  mit..**.** Run t
1e6f0 69 6d 65 20 6c 69 6d 69 74 73 20 61 72 65 20 69  ime limits are i
1e700 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20  ntended for use 
1e710 69 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20  in applications 
1e720 74 68 61 74 20 6d 61 6e 61 67 65 0a 2a 2a 20 62  that manage.** b
1e730 6f 74 68 20 74 68 65 69 72 20 6f 77 6e 20 69 6e  oth their own in
1e740 74 65 72 6e 61 6c 20 64 61 74 61 62 61 73 65 20  ternal database 
1e750 61 6e 64 20 61 6c 73 6f 20 64 61 74 61 62 61 73  and also databas
1e760 65 73 20 74 68 61 74 20 61 72 65 20 63 6f 6e 74  es that are cont
1e770 72 6f 6c 6c 65 64 0a 2a 2a 20 62 79 20 75 6e 74  rolled.** by unt
1e780 72 75 73 74 65 64 20 65 78 74 65 72 6e 61 6c 20  rusted external 
1e790 73 6f 75 72 63 65 73 2e 20 20 41 6e 20 65 78 61  sources.  An exa
1e7a0 6d 70 6c 65 20 61 70 70 6c 69 63 61 74 69 6f 6e  mple application
1e7b0 20 6d 69 67 68 74 20 62 65 20 61 0a 2a 2a 20 77   might be a.** w
1e7c0 65 62 20 62 72 6f 77 73 65 72 20 74 68 61 74 20  eb browser that 
1e7d0 68 61 73 20 69 74 73 20 6f 77 6e 20 64 61 74 61  has its own data
1e7e0 62 61 73 65 73 20 66 6f 72 20 73 74 6f 72 69 6e  bases for storin
1e7f0 67 20 68 69 73 74 6f 72 79 20 61 6e 64 0a 2a 2a  g history and.**
1e800 20 73 65 70 61 72 61 74 65 20 64 61 74 61 62 61   separate databa
1e810 73 65 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62  ses controlled b
1e820 79 20 4a 61 76 61 53 63 72 69 70 74 20 61 70 70  y JavaScript app
1e830 6c 69 63 61 74 69 6f 6e 73 20 64 6f 77 6e 6c 6f  lications downlo
1e840 61 64 65 64 0a 2a 2a 20 6f 66 66 20 74 68 65 20  aded.** off the 
1e850 49 6e 74 65 72 6e 65 74 2e 20 20 54 68 65 20 69  Internet.  The i
1e860 6e 74 65 72 6e 61 6c 20 64 61 74 61 62 61 73 65  nternal database
1e870 73 20 63 61 6e 20 62 65 20 67 69 76 65 6e 20 74  s can be given t
1e880 68 65 0a 2a 2a 20 6c 61 72 67 65 2c 20 64 65 66  he.** large, def
1e890 61 75 6c 74 20 6c 69 6d 69 74 73 2e 20 20 44 61  ault limits.  Da
1e8a0 74 61 62 61 73 65 73 20 6d 61 6e 61 67 65 64 20  tabases managed 
1e8b0 62 79 20 65 78 74 65 72 6e 61 6c 20 73 6f 75 72  by external sour
1e8c0 63 65 73 20 63 61 6e 0a 2a 2a 20 62 65 20 67 69  ces can.** be gi
1e8d0 76 65 6e 20 6d 75 63 68 20 73 6d 61 6c 6c 65 72  ven much smaller
1e8e0 20 6c 69 6d 69 74 73 20 64 65 73 69 67 6e 65 64   limits designed
1e8f0 20 74 6f 20 70 72 65 76 65 6e 74 20 61 20 64 65   to prevent a de
1e900 6e 69 61 6c 20 6f 66 20 73 65 72 76 69 63 65 0a  nial of service.
1e910 2a 2a 20 61 74 74 61 63 6b 2e 20 20 44 65 76 65  ** attack.  Deve
1e920 6c 6f 70 65 72 73 20 6d 69 67 68 74 20 61 6c 73  lopers might als
1e930 6f 20 77 61 6e 74 20 74 6f 20 75 73 65 20 74 68  o want to use th
1e940 65 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61  e [sqlite3_set_a
1e950 75 74 68 6f 72 69 7a 65 72 28 29 5d 0a 2a 2a 20  uthorizer()].** 
1e960 69 6e 74 65 72 66 61 63 65 20 74 6f 20 66 75 72  interface to fur
1e970 74 68 65 72 20 63 6f 6e 74 72 6f 6c 20 75 6e 74  ther control unt
1e980 72 75 73 74 65 64 20 53 51 4c 2e 20 20 54 68 65  rusted SQL.  The
1e990 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74   size of the dat
1e9a0 61 62 61 73 65 0a 2a 2a 20 63 72 65 61 74 65 64  abase.** created
1e9b0 20 62 79 20 61 6e 20 75 6e 74 72 75 73 74 65 64   by an untrusted
1e9c0 20 73 63 72 69 70 74 20 63 61 6e 20 62 65 20 63   script can be c
1e9d0 6f 6e 74 61 69 6e 65 64 20 75 73 69 6e 67 20 74  ontained using t
1e9e0 68 65 0a 2a 2a 20 5b 6d 61 78 5f 70 61 67 65 5f  he.** [max_page_
1e9f0 63 6f 75 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 2e  count] [PRAGMA].
1ea00 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 72 75 6e 2d 74  .**.** New run-t
1ea10 69 6d 65 20 6c 69 6d 69 74 20 63 61 74 65 67 6f  ime limit catego
1ea20 72 69 65 73 20 6d 61 79 20 62 65 20 61 64 64 65  ries may be adde
1ea30 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65  d in future rele
1ea40 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  ases..**.** Requ
1ea50 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
1ea60 32 37 36 32 5d 20 5b 48 31 32 37 36 36 5d 20 5b  2762] [H12766] [
1ea70 48 31 32 37 36 39 5d 0a 2a 2f 0a 53 51 4c 49 54  H12769].*/.SQLIT
1ea80 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1ea90 33 5f 6c 69 6d 69 74 28 73 71 6c 69 74 65 33 2a  3_limit(sqlite3*
1eaa0 2c 20 69 6e 74 20 69 64 2c 20 69 6e 74 20 6e 65  , int id, int ne
1eab0 77 56 61 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  wVal);../*.** CA
1eac0 50 49 33 52 45 46 3a 20 52 75 6e 2d 54 69 6d 65  PI3REF: Run-Time
1ead0 20 4c 69 6d 69 74 20 43 61 74 65 67 6f 72 69 65   Limit Categorie
1eae0 73 20 7b 48 31 32 37 39 30 7d 20 3c 48 31 32 37  s {H12790} <H127
1eaf0 36 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a  60>.** KEYWORDS:
1eb00 20 7b 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 79   {limit category
1eb10 7d 20 7b 6c 69 6d 69 74 20 63 61 74 65 67 6f 72  } {limit categor
1eb20 69 65 73 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  ies}.**.** These
1eb30 20 63 6f 6e 73 74 61 6e 74 73 20 64 65 66 69 6e   constants defin
1eb40 65 20 76 61 72 69 6f 75 73 20 70 65 72 66 6f 72  e various perfor
1eb50 6d 61 6e 63 65 20 6c 69 6d 69 74 73 0a 2a 2a 20  mance limits.** 
1eb60 74 68 61 74 20 63 61 6e 20 62 65 20 6c 6f 77 65  that can be lowe
1eb70 72 65 64 20 61 74 20 72 75 6e 2d 74 69 6d 65 20  red at run-time 
1eb80 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6c  using [sqlite3_l
1eb90 69 6d 69 74 28 29 5d 2e 0a 2a 2a 20 54 68 65 20  imit()]..** The 
1eba0 73 79 6e 6f 70 73 69 73 20 6f 66 20 74 68 65 20  synopsis of the 
1ebb0 6d 65 61 6e 69 6e 67 73 20 6f 66 20 74 68 65 20  meanings of the 
1ebc0 76 61 72 69 6f 75 73 20 6c 69 6d 69 74 73 20 69  various limits i
1ebd0 73 20 73 68 6f 77 6e 20 62 65 6c 6f 77 2e 0a 2a  s shown below..*
1ebe0 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66  * Additional inf
1ebf0 6f 72 6d 61 74 69 6f 6e 20 69 73 20 61 76 61 69  ormation is avai
1ec00 6c 61 62 6c 65 20 61 74 20 5b 6c 69 6d 69 74 73  lable at [limits
1ec10 20 7c 20 4c 69 6d 69 74 73 20 69 6e 20 53 51 4c   | Limits in SQL
1ec20 69 74 65 5d 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e  ite]..**.** <dl>
1ec30 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c  .** <dt>SQLITE_L
1ec40 49 4d 49 54 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e  IMIT_LENGTH</dt>
1ec50 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69  .** <dd>The maxi
1ec60 6d 75 6d 20 73 69 7a 65 20 6f 66 20 61 6e 79 20  mum size of any 
1ec70 73 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 6f  string or BLOB o
1ec80 72 20 74 61 62 6c 65 20 72 6f 77 2e 3c 64 64 3e  r table row.<dd>
1ec90 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54  .**.** <dt>SQLIT
1eca0 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47  E_LIMIT_SQL_LENG
1ecb0 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  TH</dt>.** <dd>T
1ecc0 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74  he maximum lengt
1ecd0 68 20 6f 66 20 61 6e 20 53 51 4c 20 73 74 61 74  h of an SQL stat
1ece0 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a  ement.</dd>.**.*
1ecf0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d  * <dt>SQLITE_LIM
1ed00 49 54 5f 43 4f 4c 55 4d 4e 3c 2f 64 74 3e 0a 2a  IT_COLUMN</dt>.*
1ed10 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75  * <dd>The maximu
1ed20 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  m number of colu
1ed30 6d 6e 73 20 69 6e 20 61 20 74 61 62 6c 65 20 64  mns in a table d
1ed40 65 66 69 6e 69 74 69 6f 6e 20 6f 72 20 69 6e 20  efinition or in 
1ed50 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20 73 65  the.** result se
1ed60 74 20 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d 20  t of a [SELECT] 
1ed70 6f 72 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e  or the maximum n
1ed80 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
1ed90 20 69 6e 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20   in an index.** 
1eda0 6f 72 20 69 6e 20 61 6e 20 4f 52 44 45 52 20 42  or in an ORDER B
1edb0 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c  Y or GROUP BY cl
1edc0 61 75 73 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a  ause.</dd>.**.**
1edd0 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49   <dt>SQLITE_LIMI
1ede0 54 5f 45 58 50 52 5f 44 45 50 54 48 3c 2f 64 74  T_EXPR_DEPTH</dt
1edf0 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78  >.** <dd>The max
1ee00 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 74 68  imum depth of th
1ee10 65 20 70 61 72 73 65 20 74 72 65 65 20 6f 6e 20  e parse tree on 
1ee20 61 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 2e 3c  any expression.<
1ee30 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53  /dd>.**.** <dt>S
1ee40 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50  QLITE_LIMIT_COMP
1ee50 4f 55 4e 44 5f 53 45 4c 45 43 54 3c 2f 64 74 3e  OUND_SELECT</dt>
1ee60 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69  .** <dd>The maxi
1ee70 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74 65  mum number of te
1ee80 72 6d 73 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e  rms in a compoun
1ee90 64 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  d SELECT stateme
1eea0 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  nt.</dd>.**.** <
1eeb0 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  dt>SQLITE_LIMIT_
1eec0 56 44 42 45 5f 4f 50 3c 2f 64 74 3e 0a 2a 2a 20  VDBE_OP</dt>.** 
1eed0 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20  <dd>The maximum 
1eee0 6e 75 6d 62 65 72 20 6f 66 20 69 6e 73 74 72 75  number of instru
1eef0 63 74 69 6f 6e 73 20 69 6e 20 61 20 76 69 72 74  ctions in a virt
1ef00 75 61 6c 20 6d 61 63 68 69 6e 65 20 70 72 6f 67  ual machine prog
1ef10 72 61 6d 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69  ram.** used to i
1ef20 6d 70 6c 65 6d 65 6e 74 20 61 6e 20 53 51 4c 20  mplement an SQL 
1ef30 73 74 61 74 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a  statement.</dd>.
1ef40 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  **.** <dt>SQLITE
1ef50 5f 4c 49 4d 49 54 5f 46 55 4e 43 54 49 4f 4e 5f  _LIMIT_FUNCTION_
1ef60 41 52 47 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  ARG</dt>.** <dd>
1ef70 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62  The maximum numb
1ef80 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20  er of arguments 
1ef90 6f 6e 20 61 20 66 75 6e 63 74 69 6f 6e 2e 3c 2f  on a function.</
1efa0 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51  dd>.**.** <dt>SQ
1efb0 4c 49 54 45 5f 4c 49 4d 49 54 5f 41 54 54 41 43  LITE_LIMIT_ATTAC
1efc0 48 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  HED</dt>.** <dd>
1efd0 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62  The maximum numb
1efe0 65 72 20 6f 66 20 5b 41 54 54 41 43 48 20 7c 20  er of [ATTACH | 
1eff0 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
1f000 65 73 5d 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  es].</dd>.**.** 
1f010 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54  <dt>SQLITE_LIMIT
1f020 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45  _LIKE_PATTERN_LE
1f030 4e 47 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  NGTH</dt>.** <dd
1f040 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e  >The maximum len
1f050 67 74 68 20 6f 66 20 74 68 65 20 70 61 74 74 65  gth of the patte
1f060 72 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  rn argument to t
1f070 68 65 20 5b 4c 49 4b 45 5d 20 6f 72 0a 2a 2a 20  he [LIKE] or.** 
1f080 5b 47 4c 4f 42 5d 20 6f 70 65 72 61 74 6f 72 73  [GLOB] operators
1f090 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74  .</dd>.**.** <dt
1f0a0 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41  >SQLITE_LIMIT_VA
1f0b0 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 3c 2f 64  RIABLE_NUMBER</d
1f0c0 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61  t>.** <dd>The ma
1f0d0 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20  ximum number of 
1f0e0 76 61 72 69 61 62 6c 65 73 20 69 6e 20 61 6e 20  variables in an 
1f0f0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68  SQL statement th
1f100 61 74 20 63 61 6e 0a 2a 2a 20 62 65 20 62 6f 75  at can.** be bou
1f110 6e 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  nd.</dd>.**.** <
1f120 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  dt>SQLITE_LIMIT_
1f130 54 52 49 47 47 45 52 5f 44 45 50 54 48 3c 2f 64  TRIGGER_DEPTH</d
1f140 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61  t>.** <dd>The ma
1f150 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 72  ximum depth of r
1f160 65 63 75 72 73 69 6f 6e 20 66 6f 72 20 74 72 69  ecursion for tri
1f170 67 67 65 72 73 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c  ggers.</dd>.** <
1f180 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  /dl>.*/.#define 
1f190 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e  SQLITE_LIMIT_LEN
1f1a0 47 54 48 20 20 20 20 20 20 20 20 20 20 20 20 20  GTH             
1f1b0 20 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65         0.#define
1f1c0 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 53 51   SQLITE_LIMIT_SQ
1f1d0 4c 5f 4c 45 4e 47 54 48 20 20 20 20 20 20 20 20  L_LENGTH        
1f1e0 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e          1.#defin
1f1f0 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43  e SQLITE_LIMIT_C
1f200 4f 4c 55 4d 4e 20 20 20 20 20 20 20 20 20 20 20  OLUMN           
1f210 20 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 69           2.#defi
1f220 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  ne SQLITE_LIMIT_
1f230 45 58 50 52 5f 44 45 50 54 48 20 20 20 20 20 20  EXPR_DEPTH      
1f240 20 20 20 20 20 20 20 20 20 20 33 0a 23 64 65 66            3.#def
1f250 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54  ine SQLITE_LIMIT
1f260 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54  _COMPOUND_SELECT
1f270 20 20 20 20 20 20 20 20 20 20 20 34 0a 23 64 65             4.#de
1f280 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49  fine SQLITE_LIMI
1f290 54 5f 56 44 42 45 5f 4f 50 20 20 20 20 20 20 20  T_VDBE_OP       
1f2a0 20 20 20 20 20 20 20 20 20 20 20 20 35 0a 23 64              5.#d
1f2b0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d  efine SQLITE_LIM
1f2c0 49 54 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 20  IT_FUNCTION_ARG 
1f2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 36 0a 23               6.#
1f2e0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49  define SQLITE_LI
1f2f0 4d 49 54 5f 41 54 54 41 43 48 45 44 20 20 20 20  MIT_ATTACHED    
1f300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 0a                7.
1f310 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c  #define SQLITE_L
1f320 49 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52  IMIT_LIKE_PATTER
1f330 4e 5f 4c 45 4e 47 54 48 20 20 20 20 20 20 20 38  N_LENGTH       8
1f340 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1f350 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e  LIMIT_VARIABLE_N
1f360 55 4d 42 45 52 20 20 20 20 20 20 20 20 20 20 20  UMBER           
1f370 39 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  9.#define SQLITE
1f380 5f 4c 49 4d 49 54 5f 54 52 49 47 47 45 52 5f 44  _LIMIT_TRIGGER_D
1f390 45 50 54 48 20 20 20 20 20 20 20 20 20 20 20 20  EPTH            
1f3a0 31 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  10../*.** CAPI3R
1f3b0 45 46 3a 20 43 6f 6d 70 69 6c 69 6e 67 20 41 6e  EF: Compiling An
1f3c0 20 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 7b   SQL Statement {
1f3d0 48 31 33 30 31 30 7d 20 3c 53 31 30 30 30 30 3e  H13010} <S10000>
1f3e0 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 53  .** KEYWORDS: {S
1f3f0 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d  QL statement com
1f400 70 69 6c 65 72 7d 0a 2a 2a 0a 2a 2a 20 54 6f 20  piler}.**.** To 
1f410 65 78 65 63 75 74 65 20 61 6e 20 53 51 4c 20 71  execute an SQL q
1f420 75 65 72 79 2c 20 69 74 20 6d 75 73 74 20 66 69  uery, it must fi
1f430 72 73 74 20 62 65 20 63 6f 6d 70 69 6c 65 64 20  rst be compiled 
1f440 69 6e 74 6f 20 61 20 62 79 74 65 2d 63 6f 64 65  into a byte-code
1f450 0a 2a 2a 20 70 72 6f 67 72 61 6d 20 75 73 69 6e  .** program usin
1f460 67 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 72  g one of these r
1f470 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54  outines..**.** T
1f480 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
1f490 74 2c 20 22 64 62 22 2c 20 69 73 20 61 20 5b 64  t, "db", is a [d
1f4a0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
1f4b0 6f 6e 5d 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  on] obtained fro
1f4c0 6d 20 61 0a 2a 2a 20 70 72 69 6f 72 20 73 75 63  m a.** prior suc
1f4d0 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20  cessful call to 
1f4e0 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d  [sqlite3_open()]
1f4f0 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  , [sqlite3_open_
1f500 76 32 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c  v2()] or.** [sql
1f510 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 20  ite3_open16()]. 
1f520 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   The database co
1f530 6e 6e 65 63 74 69 6f 6e 20 6d 75 73 74 20 6e 6f  nnection must no
1f540 74 20 68 61 76 65 20 62 65 65 6e 20 63 6c 6f 73  t have been clos
1f550 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65  ed..**.** The se
1f560 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 22  cond argument, "
1f570 7a 53 71 6c 22 2c 20 69 73 20 74 68 65 20 73 74  zSql", is the st
1f580 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 20 63 6f  atement to be co
1f590 6d 70 69 6c 65 64 2c 20 65 6e 63 6f 64 65 64 0a  mpiled, encoded.
1f5a0 2a 2a 20 61 73 20 65 69 74 68 65 72 20 55 54 46  ** as either UTF
1f5b0 2d 38 20 6f 72 20 55 54 46 2d 31 36 2e 20 20 54  -8 or UTF-16.  T
1f5c0 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  he sqlite3_prepa
1f5d0 72 65 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  re() and sqlite3
1f5e0 5f 70 72 65 70 61 72 65 5f 76 32 28 29 0a 2a 2a  _prepare_v2().**
1f5f0 20 69 6e 74 65 72 66 61 63 65 73 20 75 73 65 20   interfaces use 
1f600 55 54 46 2d 38 2c 20 61 6e 64 20 73 71 6c 69 74  UTF-8, and sqlit
1f610 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 20 61  e3_prepare16() a
1f620 6e 64 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  nd sqlite3_prepa
1f630 72 65 31 36 5f 76 32 28 29 0a 2a 2a 20 75 73 65  re16_v2().** use
1f640 20 55 54 46 2d 31 36 2e 0a 2a 2a 0a 2a 2a 20 49   UTF-16..**.** I
1f650 66 20 74 68 65 20 6e 42 79 74 65 20 61 72 67 75  f the nByte argu
1f660 6d 65 6e 74 20 69 73 20 6c 65 73 73 20 74 68 61  ment is less tha
1f670 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 7a 53 71  n zero, then zSq
1f680 6c 20 69 73 20 72 65 61 64 20 75 70 20 74 6f 20  l is read up to 
1f690 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 7a 65 72  the.** first zer
1f6a0 6f 20 74 65 72 6d 69 6e 61 74 6f 72 2e 20 49 66  o terminator. If
1f6b0 20 6e 42 79 74 65 20 69 73 20 6e 6f 6e 2d 6e 65   nByte is non-ne
1f6c0 67 61 74 69 76 65 2c 20 74 68 65 6e 20 69 74 20  gative, then it 
1f6d0 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 2a  is the maximum.*
1f6e0 2a 20 6e 75 6d 62 65 72 20 6f 66 20 20 62 79 74  * number of  byt
1f6f0 65 73 20 72 65 61 64 20 66 72 6f 6d 20 7a 53 71  es read from zSq
1f700 6c 2e 20 20 57 68 65 6e 20 6e 42 79 74 65 20 69  l.  When nByte i
1f710 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20  s non-negative, 
1f720 74 68 65 0a 2a 2a 20 7a 53 71 6c 20 73 74 72 69  the.** zSql stri
1f730 6e 67 20 65 6e 64 73 20 61 74 20 65 69 74 68 65  ng ends at eithe
1f740 72 20 74 68 65 20 66 69 72 73 74 20 27 5c 30 30  r the first '\00
1f750 30 27 20 6f 72 20 27 5c 75 30 30 30 30 27 20 63  0' or '\u0000' c
1f760 68 61 72 61 63 74 65 72 20 6f 72 0a 2a 2a 20 74  haracter or.** t
1f770 68 65 20 6e 42 79 74 65 2d 74 68 20 62 79 74 65  he nByte-th byte
1f780 2c 20 77 68 69 63 68 65 76 65 72 20 63 6f 6d 65  , whichever come
1f790 73 20 66 69 72 73 74 2e 20 49 66 20 74 68 65 20  s first. If the 
1f7a0 63 61 6c 6c 65 72 20 6b 6e 6f 77 73 0a 2a 2a 20  caller knows.** 
1f7b0 74 68 61 74 20 74 68 65 20 73 75 70 70 6c 69 65  that the supplie
1f7c0 64 20 73 74 72 69 6e 67 20 69 73 20 6e 75 6c 2d  d string is nul-
1f7d0 74 65 72 6d 69 6e 61 74 65 64 2c 20 74 68 65 6e  terminated, then
1f7e0 20 74 68 65 72 65 20 69 73 20 61 20 73 6d 61 6c   there is a smal
1f7f0 6c 0a 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65  l.** performance
1f800 20 61 64 76 61 6e 74 61 67 65 20 74 6f 20 62 65   advantage to be
1f810 20 67 61 69 6e 65 64 20 62 79 20 70 61 73 73 69   gained by passi
1f820 6e 67 20 61 6e 20 6e 42 79 74 65 20 70 61 72 61  ng an nByte para
1f830 6d 65 74 65 72 20 74 68 61 74 0a 2a 2a 20 69 73  meter that.** is
1f840 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6e 75   equal to the nu
1f850 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
1f860 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e   the input strin
1f870 67 20 3c 69 3e 69 6e 63 6c 75 64 69 6e 67 3c 2f  g <i>including</
1f880 69 3e 0a 2a 2a 20 74 68 65 20 6e 75 6c 2d 74 65  i>.** the nul-te
1f890 72 6d 69 6e 61 74 6f 72 20 62 79 74 65 73 2e 0a  rminator bytes..
1f8a0 2a 2a 0a 2a 2a 20 49 66 20 70 7a 54 61 69 6c 20  **.** If pzTail 
1f8b0 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e  is not NULL then
1f8c0 20 2a 70 7a 54 61 69 6c 20 69 73 20 6d 61 64 65   *pzTail is made
1f8d0 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65   to point to the
1f8e0 20 66 69 72 73 74 20 62 79 74 65 0a 2a 2a 20 70   first byte.** p
1f8f0 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74  ast the end of t
1f900 68 65 20 66 69 72 73 74 20 53 51 4c 20 73 74 61  he first SQL sta
1f910 74 65 6d 65 6e 74 20 69 6e 20 7a 53 71 6c 2e 20  tement in zSql. 
1f920 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
1f930 6f 6e 6c 79 0a 2a 2a 20 63 6f 6d 70 69 6c 65 20  only.** compile 
1f940 74 68 65 20 66 69 72 73 74 20 73 74 61 74 65 6d  the first statem
1f950 65 6e 74 20 69 6e 20 7a 53 71 6c 2c 20 73 6f 20  ent in zSql, so 
1f960 2a 70 7a 54 61 69 6c 20 69 73 20 6c 65 66 74 20  *pzTail is left 
1f970 70 6f 69 6e 74 69 6e 67 20 74 6f 0a 2a 2a 20 77  pointing to.** w
1f980 68 61 74 20 72 65 6d 61 69 6e 73 20 75 6e 63 6f  hat remains unco
1f990 6d 70 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 2a 70  mpiled..**.** *p
1f9a0 70 53 74 6d 74 20 69 73 20 6c 65 66 74 20 70 6f  pStmt is left po
1f9b0 69 6e 74 69 6e 67 20 74 6f 20 61 20 63 6f 6d 70  inting to a comp
1f9c0 69 6c 65 64 20 5b 70 72 65 70 61 72 65 64 20 73  iled [prepared s
1f9d0 74 61 74 65 6d 65 6e 74 5d 20 74 68 61 74 20 63  tatement] that c
1f9e0 61 6e 20 62 65 0a 2a 2a 20 65 78 65 63 75 74 65  an be.** execute
1f9f0 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33  d using [sqlite3
1fa00 5f 73 74 65 70 28 29 5d 2e 20 20 49 66 20 74 68  _step()].  If th
1fa10 65 72 65 20 69 73 20 61 6e 20 65 72 72 6f 72 2c  ere is an error,
1fa20 20 2a 70 70 53 74 6d 74 20 69 73 20 73 65 74 0a   *ppStmt is set.
1fa30 2a 2a 20 74 6f 20 4e 55 4c 4c 2e 20 20 49 66 20  ** to NULL.  If 
1fa40 74 68 65 20 69 6e 70 75 74 20 74 65 78 74 20 63  the input text c
1fa50 6f 6e 74 61 69 6e 73 20 6e 6f 20 53 51 4c 20 28  ontains no SQL (
1fa60 69 66 20 74 68 65 20 69 6e 70 75 74 20 69 73 20  if the input is 
1fa70 61 6e 20 65 6d 70 74 79 0a 2a 2a 20 73 74 72 69  an empty.** stri
1fa80 6e 67 20 6f 72 20 61 20 63 6f 6d 6d 65 6e 74 29  ng or a comment)
1fa90 20 74 68 65 6e 20 2a 70 70 53 74 6d 74 20 69 73   then *ppStmt is
1faa0 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a   set to NULL..**
1fab0 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f   The calling pro
1fac0 63 65 64 75 72 65 20 69 73 20 72 65 73 70 6f 6e  cedure is respon
1fad0 73 69 62 6c 65 20 66 6f 72 20 64 65 6c 65 74 69  sible for deleti
1fae0 6e 67 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 0a  ng the compiled.
1faf0 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ** SQL statement
1fb00 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f   using [sqlite3_
1fb10 66 69 6e 61 6c 69 7a 65 28 29 5d 20 61 66 74 65  finalize()] afte
1fb20 72 20 69 74 20 68 61 73 20 66 69 6e 69 73 68 65  r it has finishe
1fb30 64 20 77 69 74 68 20 69 74 2e 0a 2a 2a 20 70 70  d with it..** pp
1fb40 53 74 6d 74 20 6d 61 79 20 6e 6f 74 20 62 65 20  Stmt may not be 
1fb50 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73  NULL..**.** On s
1fb60 75 63 63 65 73 73 2c 20 5b 53 51 4c 49 54 45 5f  uccess, [SQLITE_
1fb70 4f 4b 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2c  OK] is returned,
1fb80 20 6f 74 68 65 72 77 69 73 65 20 61 6e 20 5b 65   otherwise an [e
1fb90 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65  rror code] is re
1fba0 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  turned..**.** Th
1fbb0 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  e sqlite3_prepar
1fbc0 65 5f 76 32 28 29 20 61 6e 64 20 73 71 6c 69 74  e_v2() and sqlit
1fbd0 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28  e3_prepare16_v2(
1fbe0 29 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 65  ) interfaces are
1fbf0 0a 2a 2a 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20  .** recommended 
1fc00 66 6f 72 20 61 6c 6c 20 6e 65 77 20 70 72 6f 67  for all new prog
1fc10 72 61 6d 73 2e 20 54 68 65 20 74 77 6f 20 6f 6c  rams. The two ol
1fc20 64 65 72 20 69 6e 74 65 72 66 61 63 65 73 20 61  der interfaces a
1fc30 72 65 20 72 65 74 61 69 6e 65 64 0a 2a 2a 20 66  re retained.** f
1fc40 6f 72 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d  or backwards com
1fc50 70 61 74 69 62 69 6c 69 74 79 2c 20 62 75 74 20  patibility, but 
1fc60 74 68 65 69 72 20 75 73 65 20 69 73 20 64 69 73  their use is dis
1fc70 63 6f 75 72 61 67 65 64 2e 0a 2a 2a 20 49 6e 20  couraged..** In 
1fc80 74 68 65 20 22 76 32 22 20 69 6e 74 65 72 66 61  the "v2" interfa
1fc90 63 65 73 2c 20 74 68 65 20 70 72 65 70 61 72 65  ces, the prepare
1fca0 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74  d statement.** t
1fcb0 68 61 74 20 69 73 20 72 65 74 75 72 6e 65 64 20  hat is returned 
1fcc0 28 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74  (the [sqlite3_st
1fcd0 6d 74 5d 20 6f 62 6a 65 63 74 29 20 63 6f 6e 74  mt] object) cont
1fce0 61 69 6e 73 20 61 20 63 6f 70 79 20 6f 66 20 74  ains a copy of t
1fcf0 68 65 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 53  he.** original S
1fd00 51 4c 20 74 65 78 74 2e 20 54 68 69 73 20 63 61  QL text. This ca
1fd10 75 73 65 73 20 74 68 65 20 5b 73 71 6c 69 74 65  uses the [sqlite
1fd20 33 5f 73 74 65 70 28 29 5d 20 69 6e 74 65 72 66  3_step()] interf
1fd30 61 63 65 20 74 6f 0a 2a 2a 20 62 65 68 61 76 65  ace to.** behave
1fd40 20 61 20 64 69 66 66 65 72 65 6e 74 6c 79 20 69   a differently i
1fd50 6e 20 74 77 6f 20 77 61 79 73 3a 0a 2a 2a 0a 2a  n two ways:.**.*
1fd60 2a 20 3c 6f 6c 3e 0a 2a 2a 20 3c 6c 69 3e 0a 2a  * <ol>.** <li>.*
1fd70 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73  * If the databas
1fd80 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73  e schema changes
1fd90 2c 20 69 6e 73 74 65 61 64 20 6f 66 20 72 65 74  , instead of ret
1fda0 75 72 6e 69 6e 67 20 5b 53 51 4c 49 54 45 5f 53  urning [SQLITE_S
1fdb0 43 48 45 4d 41 5d 20 61 73 20 69 74 0a 2a 2a 20  CHEMA] as it.** 
1fdc0 61 6c 77 61 79 73 20 75 73 65 64 20 74 6f 20 64  always used to d
1fdd0 6f 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  o, [sqlite3_step
1fde0 28 29 5d 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74  ()] will automat
1fdf0 69 63 61 6c 6c 79 20 72 65 63 6f 6d 70 69 6c 65  ically recompile
1fe00 20 74 68 65 20 53 51 4c 0a 2a 2a 20 73 74 61 74   the SQL.** stat
1fe10 65 6d 65 6e 74 20 61 6e 64 20 74 72 79 20 74 6f  ement and try to
1fe20 20 72 75 6e 20 69 74 20 61 67 61 69 6e 2e 20 20   run it again.  
1fe30 49 66 20 74 68 65 20 73 63 68 65 6d 61 20 68 61  If the schema ha
1fe40 73 20 63 68 61 6e 67 65 64 20 69 6e 0a 2a 2a 20  s changed in.** 
1fe50 61 20 77 61 79 20 74 68 61 74 20 6d 61 6b 65 73  a way that makes
1fe60 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6e   the statement n
1fe70 6f 20 6c 6f 6e 67 65 72 20 76 61 6c 69 64 2c 20  o longer valid, 
1fe80 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
1fe90 20 77 69 6c 6c 20 73 74 69 6c 6c 0a 2a 2a 20 72   will still.** r
1fea0 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 53 43  eturn [SQLITE_SC
1feb0 48 45 4d 41 5d 2e 20 20 42 75 74 20 75 6e 6c 69  HEMA].  But unli
1fec0 6b 65 20 74 68 65 20 6c 65 67 61 63 79 20 62 65  ke the legacy be
1fed0 68 61 76 69 6f 72 2c 20 5b 53 51 4c 49 54 45 5f  havior, [SQLITE_
1fee0 53 43 48 45 4d 41 5d 20 69 73 0a 2a 2a 20 6e 6f  SCHEMA] is.** no
1fef0 77 20 61 20 66 61 74 61 6c 20 65 72 72 6f 72 2e  w a fatal error.
1ff00 20 20 43 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74    Calling [sqlit
1ff10 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
1ff20 20 61 67 61 69 6e 20 77 69 6c 6c 20 6e 6f 74 20   again will not 
1ff30 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 65 72 72 6f  make the.** erro
1ff40 72 20 67 6f 20 61 77 61 79 2e 20 20 4e 6f 74 65  r go away.  Note
1ff50 3a 20 75 73 65 20 5b 73 71 6c 69 74 65 33 5f 65  : use [sqlite3_e
1ff60 72 72 6d 73 67 28 29 5d 20 74 6f 20 66 69 6e 64  rrmsg()] to find
1ff70 20 74 68 65 20 74 65 78 74 0a 2a 2a 20 6f 66 20   the text.** of 
1ff80 74 68 65 20 70 61 72 73 69 6e 67 20 65 72 72 6f  the parsing erro
1ff90 72 20 74 68 61 74 20 72 65 73 75 6c 74 73 20 69  r that results i
1ffa0 6e 20 61 6e 20 5b 53 51 4c 49 54 45 5f 53 43 48  n an [SQLITE_SCH
1ffb0 45 4d 41 5d 20 72 65 74 75 72 6e 2e 0a 2a 2a 20  EMA] return..** 
1ffc0 3c 2f 6c 69 3e 0a 2a 2a 0a 2a 2a 20 3c 6c 69 3e  </li>.**.** <li>
1ffd0 0a 2a 2a 20 57 68 65 6e 20 61 6e 20 65 72 72 6f  .** When an erro
1ffe0 72 20 6f 63 63 75 72 73 2c 20 5b 73 71 6c 69 74  r occurs, [sqlit
1fff0 65 33 5f 73 74 65 70 28 29 5d 20 77 69 6c 6c 20  e3_step()] will 
20000 72 65 74 75 72 6e 20 6f 6e 65 20 6f 66 20 74 68  return one of th
20010 65 20 64 65 74 61 69 6c 65 64 0a 2a 2a 20 5b 65  e detailed.** [e
20020 72 72 6f 72 20 63 6f 64 65 73 5d 20 6f 72 20 5b  rror codes] or [
20030 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63  extended error c
20040 6f 64 65 73 5d 2e 20 20 54 68 65 20 6c 65 67 61  odes].  The lega
20050 63 79 20 62 65 68 61 76 69 6f 72 20 77 61 73 20  cy behavior was 
20060 74 68 61 74 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  that.** [sqlite3
20070 5f 73 74 65 70 28 29 5d 20 77 6f 75 6c 64 20 6f  _step()] would o
20080 6e 6c 79 20 72 65 74 75 72 6e 20 61 20 67 65 6e  nly return a gen
20090 65 72 69 63 20 5b 53 51 4c 49 54 45 5f 45 52 52  eric [SQLITE_ERR
200a0 4f 52 5d 20 72 65 73 75 6c 74 20 63 6f 64 65 0a  OR] result code.
200b0 2a 2a 20 61 6e 64 20 79 6f 75 20 77 6f 75 6c 64  ** and you would
200c0 20 68 61 76 65 20 74 6f 20 6d 61 6b 65 20 61 20   have to make a 
200d0 73 65 63 6f 6e 64 20 63 61 6c 6c 20 74 6f 20 5b  second call to [
200e0 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d  sqlite3_reset()]
200f0 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 74 6f 20   in order.** to 
20100 66 69 6e 64 20 74 68 65 20 75 6e 64 65 72 6c 79  find the underly
20110 69 6e 67 20 63 61 75 73 65 20 6f 66 20 74 68 65  ing cause of the
20120 20 70 72 6f 62 6c 65 6d 2e 20 57 69 74 68 20 74   problem. With t
20130 68 65 20 22 76 32 22 20 70 72 65 70 61 72 65 0a  he "v2" prepare.
20140 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 2c 20 74  ** interfaces, t
20150 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 72 65  he underlying re
20160 61 73 6f 6e 20 66 6f 72 20 74 68 65 20 65 72 72  ason for the err
20170 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  or is returned i
20180 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 2a 2a 20 3c  mmediately..** <
20190 2f 6c 69 3e 0a 2a 2a 20 3c 2f 6f 6c 3e 0a 2a 2a  /li>.** </ol>.**
201a0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
201b0 3a 0a 2a 2a 20 5b 48 31 33 30 31 31 5d 20 5b 48  :.** [H13011] [H
201c0 31 33 30 31 32 5d 20 5b 48 31 33 30 31 33 5d 20  13012] [H13013] 
201d0 5b 48 31 33 30 31 34 5d 20 5b 48 31 33 30 31 35  [H13014] [H13015
201e0 5d 20 5b 48 31 33 30 31 36 5d 20 5b 48 31 33 30  ] [H13016] [H130
201f0 31 39 5d 20 5b 48 31 33 30 32 31 5d 0a 2a 2a 0a  19] [H13021].**.
20200 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
20210 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  t sqlite3_prepar
20220 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  e(.  sqlite3 *db
20230 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
20240 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  Database handle 
20250 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
20260 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20  *zSql,       /* 
20270 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 55  SQL statement, U
20280 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a  TF-8 encoded */.
20290 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20    int nByte,    
202a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78            /* Max
202b0 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a  imum length of z
202c0 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f  Sql in bytes. */
202d0 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
202e0 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55  **ppStmt,  /* OU
202f0 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e  T: Statement han
20300 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  dle */.  const c
20310 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20  har **pzTail    
20320 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72   /* OUT: Pointer
20330 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69   to unused porti
20340 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b  on of zSql */.);
20350 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
20360 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
20370 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  v2(.  sqlite3 *d
20380 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  b,            /*
20390 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   Database handle
203a0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
203b0 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a   *zSql,       /*
203c0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20   SQL statement, 
203d0 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f  UTF-8 encoded */
203e0 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20  .  int nByte,   
203f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
20400 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20  ximum length of 
20410 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a  zSql in bytes. *
20420 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  /.  sqlite3_stmt
20430 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f   **ppStmt,  /* O
20440 55 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61  UT: Statement ha
20450 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ndle */.  const 
20460 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20  char **pzTail   
20470 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65    /* OUT: Pointe
20480 72 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 74  r to unused port
20490 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29  ion of zSql */.)
204a0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
204b0 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
204c0 31 36 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  16(.  sqlite3 *d
204d0 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  b,            /*
204e0 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   Database handle
204f0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
20500 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a   *zSql,       /*
20510 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20   SQL statement, 
20520 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 2a  UTF-16 encoded *
20530 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20  /.  int nByte,  
20540 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
20550 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66  aximum length of
20560 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20   zSql in bytes. 
20570 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  */.  sqlite3_stm
20580 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20  t **ppStmt,  /* 
20590 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68  OUT: Statement h
205a0 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  andle */.  const
205b0 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20   void **pzTail  
205c0 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74     /* OUT: Point
205d0 65 72 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72  er to unused por
205e0 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a  tion of zSql */.
205f0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
20600 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  t sqlite3_prepar
20610 65 31 36 5f 76 32 28 0a 20 20 73 71 6c 69 74 65  e16_v2(.  sqlite
20620 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
20630 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61    /* Database ha
20640 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ndle */.  const 
20650 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20 20  void *zSql,     
20660 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65    /* SQL stateme
20670 6e 74 2c 20 55 54 46 2d 31 36 20 65 6e 63 6f 64  nt, UTF-16 encod
20680 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74  ed */.  int nByt
20690 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
206a0 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74  /* Maximum lengt
206b0 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74  h of zSql in byt
206c0 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  es. */.  sqlite3
206d0 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20  _stmt **ppStmt, 
206e0 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65   /* OUT: Stateme
206f0 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63  nt handle */.  c
20700 6f 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61  onst void **pzTa
20710 69 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50  il     /* OUT: P
20720 6f 69 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64  ointer to unused
20730 20 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c   portion of zSql
20740 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41   */.);../*.** CA
20750 50 49 33 52 45 46 3a 20 52 65 74 72 69 65 76 69  PI3REF: Retrievi
20760 6e 67 20 53 74 61 74 65 6d 65 6e 74 20 53 51 4c  ng Statement SQL
20770 20 7b 48 31 33 31 30 30 7d 20 3c 48 31 33 30 30   {H13100} <H1300
20780 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  0>.**.** This in
20790 74 65 72 66 61 63 65 20 63 61 6e 20 62 65 20 75  terface can be u
207a0 73 65 64 20 74 6f 20 72 65 74 72 69 65 76 65 20  sed to retrieve 
207b0 61 20 73 61 76 65 64 20 63 6f 70 79 20 6f 66 20  a saved copy of 
207c0 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20  the original.** 
207d0 53 51 4c 20 74 65 78 74 20 75 73 65 64 20 74 6f  SQL text used to
207e0 20 63 72 65 61 74 65 20 61 20 5b 70 72 65 70 61   create a [prepa
207f0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 69  red statement] i
20800 66 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74  f that statement
20810 20 77 61 73 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64   was.** compiled
20820 20 75 73 69 6e 67 20 65 69 74 68 65 72 20 5b 73   using either [s
20830 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
20840 32 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33  2()] or [sqlite3
20850 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d  _prepare16_v2()]
20860 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
20870 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 31 30 31  ents:.** [H13101
20880 5d 20 5b 48 31 33 31 30 32 5d 20 5b 48 31 33 31  ] [H13102] [H131
20890 30 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  03].*/.SQLITE_AP
208a0 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  I const char *sq
208b0 6c 69 74 65 33 5f 73 71 6c 28 73 71 6c 69 74 65  lite3_sql(sqlite
208c0 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a  3_stmt *pStmt);.
208d0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
208e0 20 44 79 6e 61 6d 69 63 61 6c 6c 79 20 54 79 70   Dynamically Typ
208f0 65 64 20 56 61 6c 75 65 20 4f 62 6a 65 63 74 20  ed Value Object 
20900 7b 48 31 35 30 30 30 7d 20 3c 53 32 30 32 30 30  {H15000} <S20200
20910 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b  >.** KEYWORDS: {
20920 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65  protected sqlite
20930 33 5f 76 61 6c 75 65 7d 20 7b 75 6e 70 72 6f 74  3_value} {unprot
20940 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61  ected sqlite3_va
20950 6c 75 65 7d 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  lue}.**.** SQLit
20960 65 20 75 73 65 73 20 74 68 65 20 73 71 6c 69 74  e uses the sqlit
20970 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 20  e3_value object 
20980 74 6f 20 72 65 70 72 65 73 65 6e 74 20 61 6c 6c  to represent all
20990 20 76 61 6c 75 65 73 0a 2a 2a 20 74 68 61 74 20   values.** that 
209a0 63 61 6e 20 62 65 20 73 74 6f 72 65 64 20 69 6e  can be stored in
209b0 20 61 20 64 61 74 61 62 61 73 65 20 74 61 62 6c   a database tabl
209c0 65 2e 20 53 51 4c 69 74 65 20 75 73 65 73 20 64  e. SQLite uses d
209d0 79 6e 61 6d 69 63 20 74 79 70 69 6e 67 0a 2a 2a  ynamic typing.**
209e0 20 66 6f 72 20 74 68 65 20 76 61 6c 75 65 73 20   for the values 
209f0 69 74 20 73 74 6f 72 65 73 2e 20 56 61 6c 75 65  it stores. Value
20a00 73 20 73 74 6f 72 65 64 20 69 6e 20 73 71 6c 69  s stored in sqli
20a10 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74  te3_value object
20a20 73 0a 2a 2a 20 63 61 6e 20 62 65 20 69 6e 74 65  s.** can be inte
20a30 67 65 72 73 2c 20 66 6c 6f 61 74 69 6e 67 20 70  gers, floating p
20a40 6f 69 6e 74 20 76 61 6c 75 65 73 2c 20 73 74 72  oint values, str
20a50 69 6e 67 73 2c 20 42 4c 4f 42 73 2c 20 6f 72 20  ings, BLOBs, or 
20a60 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 73  NULL..**.** An s
20a70 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a  qlite3_value obj
20a80 65 63 74 20 6d 61 79 20 62 65 20 65 69 74 68 65  ect may be eithe
20a90 72 20 22 70 72 6f 74 65 63 74 65 64 22 20 6f 72  r "protected" or
20aa0 20 22 75 6e 70 72 6f 74 65 63 74 65 64 22 2e 0a   "unprotected"..
20ab0 2a 2a 20 53 6f 6d 65 20 69 6e 74 65 72 66 61 63  ** Some interfac
20ac0 65 73 20 72 65 71 75 69 72 65 20 61 20 70 72 6f  es require a pro
20ad0 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76  tected sqlite3_v
20ae0 61 6c 75 65 2e 20 20 4f 74 68 65 72 20 69 6e 74  alue.  Other int
20af0 65 72 66 61 63 65 73 0a 2a 2a 20 77 69 6c 6c 20  erfaces.** will 
20b00 61 63 63 65 70 74 20 65 69 74 68 65 72 20 61 20  accept either a 
20b10 70 72 6f 74 65 63 74 65 64 20 6f 72 20 61 6e 20  protected or an 
20b20 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69  unprotected sqli
20b30 74 65 33 5f 76 61 6c 75 65 2e 0a 2a 2a 20 45 76  te3_value..** Ev
20b40 65 72 79 20 69 6e 74 65 72 66 61 63 65 20 74 68  ery interface th
20b50 61 74 20 61 63 63 65 70 74 73 20 73 71 6c 69 74  at accepts sqlit
20b60 65 33 5f 76 61 6c 75 65 20 61 72 67 75 6d 65 6e  e3_value argumen
20b70 74 73 20 73 70 65 63 69 66 69 65 73 0a 2a 2a 20  ts specifies.** 
20b80 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 69  whether or not i
20b90 74 20 72 65 71 75 69 72 65 73 20 61 20 70 72 6f  t requires a pro
20ba0 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76  tected sqlite3_v
20bb0 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  alue..**.** The 
20bc0 74 65 72 6d 73 20 22 70 72 6f 74 65 63 74 65 64  terms "protected
20bd0 22 20 61 6e 64 20 22 75 6e 70 72 6f 74 65 63 74  " and "unprotect
20be0 65 64 22 20 72 65 66 65 72 20 74 6f 20 77 68 65  ed" refer to whe
20bf0 74 68 65 72 20 6f 72 20 6e 6f 74 0a 2a 2a 20 61  ther or not.** a
20c00 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 2e 20   mutex is held. 
20c10 20 41 20 69 6e 74 65 72 6e 61 6c 20 6d 75 74 65   A internal mute
20c20 78 20 69 73 20 68 65 6c 64 20 66 6f 72 20 61 20  x is held for a 
20c30 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20 73 71 6c  protected.** sql
20c40 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63  ite3_value objec
20c50 74 20 62 75 74 20 6e 6f 20 6d 75 74 65 78 20 69  t but no mutex i
20c60 73 20 68 65 6c 64 20 66 6f 72 20 61 6e 20 75 6e  s held for an un
20c70 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20 73 71 6c  protected.** sql
20c80 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63  ite3_value objec
20c90 74 2e 20 20 49 66 20 53 51 4c 69 74 65 20 69 73  t.  If SQLite is
20ca0 20 63 6f 6d 70 69 6c 65 64 20 74 6f 20 62 65 20   compiled to be 
20cb0 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 0a  single-threaded.
20cc0 2a 2a 20 28 77 69 74 68 20 5b 53 51 4c 49 54 45  ** (with [SQLITE
20cd0 5f 54 48 52 45 41 44 53 41 46 45 3d 30 5d 20 61  _THREADSAFE=0] a
20ce0 6e 64 20 77 69 74 68 20 5b 73 71 6c 69 74 65 33  nd with [sqlite3
20cf0 5f 74 68 72 65 61 64 73 61 66 65 28 29 5d 20 72  _threadsafe()] r
20d00 65 74 75 72 6e 69 6e 67 20 30 29 0a 2a 2a 20 6f  eturning 0).** o
20d10 72 20 69 66 20 53 51 4c 69 74 65 20 69 73 20 72  r if SQLite is r
20d20 75 6e 20 69 6e 20 6f 6e 65 20 6f 66 20 72 65 64  un in one of red
20d30 75 63 65 64 20 6d 75 74 65 78 20 6d 6f 64 65 73  uced mutex modes
20d40 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e   .** [SQLITE_CON
20d50 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44  FIG_SINGLETHREAD
20d60 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 43 4f 4e  ] or [SQLITE_CON
20d70 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 5d  FIG_MULTITHREAD]
20d80 0a 2a 2a 20 74 68 65 6e 20 74 68 65 72 65 20 69  .** then there i
20d90 73 20 6e 6f 20 64 69 73 74 69 6e 63 74 69 6f 6e  s no distinction
20da0 20 62 65 74 77 65 65 6e 20 70 72 6f 74 65 63 74   between protect
20db0 65 64 20 61 6e 64 20 75 6e 70 72 6f 74 65 63 74  ed and unprotect
20dc0 65 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61  ed.** sqlite3_va
20dd0 6c 75 65 20 6f 62 6a 65 63 74 73 20 61 6e 64 20  lue objects and 
20de0 74 68 65 79 20 63 61 6e 20 62 65 20 75 73 65 64  they can be used
20df0 20 69 6e 74 65 72 63 68 61 6e 67 65 61 62 6c 79   interchangeably
20e00 2e 20 20 48 6f 77 65 76 65 72 2c 0a 2a 2a 20 66  .  However,.** f
20e10 6f 72 20 6d 61 78 69 6d 75 6d 20 63 6f 64 65 20  or maximum code 
20e20 70 6f 72 74 61 62 69 6c 69 74 79 20 69 74 20 69  portability it i
20e30 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 74 68  s recommended th
20e40 61 74 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a  at applications.
20e50 2a 2a 20 73 74 69 6c 6c 20 6d 61 6b 65 20 74 68  ** still make th
20e60 65 20 64 69 73 74 69 6e 63 74 69 6f 6e 20 62 65  e distinction be
20e70 74 77 65 65 6e 20 62 65 74 77 65 65 6e 20 70 72  tween between pr
20e80 6f 74 65 63 74 65 64 20 61 6e 64 20 75 6e 70 72  otected and unpr
20e90 6f 74 65 63 74 65 64 0a 2a 2a 20 73 71 6c 69 74  otected.** sqlit
20ea0 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73  e3_value objects
20eb0 20 65 76 65 6e 20 77 68 65 6e 20 6e 6f 74 20 73   even when not s
20ec0 74 72 69 63 74 6c 79 20 72 65 71 75 69 72 65 64  trictly required
20ed0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
20ee0 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74  te3_value object
20ef0 73 20 74 68 61 74 20 61 72 65 20 70 61 73 73 65  s that are passe
20f00 64 20 61 73 20 70 61 72 61 6d 65 74 65 72 73 20  d as parameters 
20f10 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 69 6d 70 6c  into the.** impl
20f20 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 5b 61  ementation of [a
20f30 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
20f40 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73  ed SQL functions
20f50 5d 20 61 72 65 20 70 72 6f 74 65 63 74 65 64 2e  ] are protected.
20f60 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
20f70 76 61 6c 75 65 20 6f 62 6a 65 63 74 20 72 65 74  value object ret
20f80 75 72 6e 65 64 20 62 79 0a 2a 2a 20 5b 73 71 6c  urned by.** [sql
20f90 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75  ite3_column_valu
20fa0 65 28 29 5d 20 69 73 20 75 6e 70 72 6f 74 65 63  e()] is unprotec
20fb0 74 65 64 2e 0a 2a 2a 20 55 6e 70 72 6f 74 65 63  ted..** Unprotec
20fc0 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ted sqlite3_valu
20fd0 65 20 6f 62 6a 65 63 74 73 20 6d 61 79 20 6f 6e  e objects may on
20fe0 6c 79 20 62 65 20 75 73 65 64 20 77 69 74 68 0a  ly be used with.
20ff0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75  ** [sqlite3_resu
21000 6c 74 5f 76 61 6c 75 65 28 29 5d 20 61 6e 64 20  lt_value()] and 
21010 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61  [sqlite3_bind_va
21020 6c 75 65 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 5b  lue()]..** The [
21030 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
21040 6f 62 20 7c 20 73 71 6c 69 74 65 33 5f 76 61 6c  ob | sqlite3_val
21050 75 65 5f 74 79 70 65 28 29 5d 20 66 61 6d 69 6c  ue_type()] famil
21060 79 20 6f 66 0a 2a 2a 20 69 6e 74 65 72 66 61 63  y of.** interfac
21070 65 73 20 72 65 71 75 69 72 65 20 70 72 6f 74 65  es require prote
21080 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c  cted sqlite3_val
21090 75 65 20 6f 62 6a 65 63 74 73 2e 0a 2a 2f 0a 74  ue objects..*/.t
210a0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 65  ypedef struct Me
210b0 6d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 3b  m sqlite3_value;
210c0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
210d0 3a 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e 20 43  : SQL Function C
210e0 6f 6e 74 65 78 74 20 4f 62 6a 65 63 74 20 7b 48  ontext Object {H
210f0 31 36 30 30 31 7d 20 3c 53 32 30 32 30 30 3e 0a  16001} <S20200>.
21100 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 78  **.** The contex
21110 74 20 69 6e 20 77 68 69 63 68 20 61 6e 20 53 51  t in which an SQ
21120 4c 20 66 75 6e 63 74 69 6f 6e 20 65 78 65 63 75  L function execu
21130 74 65 73 20 69 73 20 73 74 6f 72 65 64 20 69 6e  tes is stored in
21140 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63   an.** sqlite3_c
21150 6f 6e 74 65 78 74 20 6f 62 6a 65 63 74 2e 20 20  ontext object.  
21160 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20  A pointer to an 
21170 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
21180 6f 62 6a 65 63 74 0a 2a 2a 20 69 73 20 61 6c 77  object.** is alw
21190 61 79 73 20 66 69 72 73 74 20 70 61 72 61 6d 65  ays first parame
211a0 74 65 72 20 74 6f 20 5b 61 70 70 6c 69 63 61 74  ter to [applicat
211b0 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20  ion-defined SQL 
211c0 66 75 6e 63 74 69 6f 6e 73 5d 2e 0a 2a 2a 20 54  functions]..** T
211d0 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64  he application-d
211e0 65 66 69 6e 65 64 20 53 51 4c 20 66 75 6e 63 74  efined SQL funct
211f0 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ion implementati
21200 6f 6e 20 77 69 6c 6c 20 70 61 73 73 20 74 68 69  on will pass thi
21210 73 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 68 72  s.** pointer thr
21220 6f 75 67 68 20 69 6e 74 6f 20 63 61 6c 6c 73 20  ough into calls 
21230 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75  to [sqlite3_resu
21240 6c 74 5f 69 6e 74 20 7c 20 73 71 6c 69 74 65 33  lt_int | sqlite3
21250 5f 72 65 73 75 6c 74 28 29 5d 2c 0a 2a 2a 20 5b  _result()],.** [
21260 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
21270 65 5f 63 6f 6e 74 65 78 74 28 29 5d 2c 20 5b 73  e_context()], [s
21280 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61  qlite3_user_data
21290 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  ()],.** [sqlite3
212a0 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
212b0 6c 65 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f  le()], [sqlite3_
212c0 67 65 74 5f 61 75 78 64 61 74 61 28 29 5d 2c 0a  get_auxdata()],.
212d0 2a 2a 20 61 6e 64 2f 6f 72 20 5b 73 71 6c 69 74  ** and/or [sqlit
212e0 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29  e3_set_auxdata()
212f0 5d 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  ]..*/.typedef st
21300 72 75 63 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e  ruct sqlite3_con
21310 74 65 78 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e  text sqlite3_con
21320 74 65 78 74 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  text;../*.** CAP
21330 49 33 52 45 46 3a 20 42 69 6e 64 69 6e 67 20 56  I3REF: Binding V
21340 61 6c 75 65 73 20 54 6f 20 50 72 65 70 61 72 65  alues To Prepare
21350 64 20 53 74 61 74 65 6d 65 6e 74 73 20 7b 48 31  d Statements {H1
21360 33 35 30 30 7d 20 3c 53 37 30 33 30 30 3e 0a 2a  3500} <S70300>.*
21370 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 68 6f 73  * KEYWORDS: {hos
21380 74 20 70 61 72 61 6d 65 74 65 72 7d 20 7b 68 6f  t parameter} {ho
21390 73 74 20 70 61 72 61 6d 65 74 65 72 73 7d 20 7b  st parameters} {
213a0 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 20 6e  host parameter n
213b0 61 6d 65 7d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53  ame}.** KEYWORDS
213c0 3a 20 7b 53 51 4c 20 70 61 72 61 6d 65 74 65 72  : {SQL parameter
213d0 7d 20 7b 53 51 4c 20 70 61 72 61 6d 65 74 65 72  } {SQL parameter
213e0 73 7d 20 7b 70 61 72 61 6d 65 74 65 72 20 62 69  s} {parameter bi
213f0 6e 64 69 6e 67 7d 0a 2a 2a 0a 2a 2a 20 49 6e 20  nding}.**.** In 
21400 74 68 65 20 53 51 4c 20 73 74 72 69 6e 67 73 20  the SQL strings 
21410 69 6e 70 75 74 20 74 6f 20 5b 73 71 6c 69 74 65  input to [sqlite
21420 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20  3_prepare_v2()] 
21430 61 6e 64 20 69 74 73 20 76 61 72 69 61 6e 74 73  and its variants
21440 2c 0a 2a 2a 20 6c 69 74 65 72 61 6c 73 20 6d 61  ,.** literals ma
21450 79 20 62 65 20 72 65 70 6c 61 63 65 64 20 62 79  y be replaced by
21460 20 61 20 5b 70 61 72 61 6d 65 74 65 72 5d 20 74   a [parameter] t
21470 68 61 74 20 6d 61 74 63 68 65 73 20 6f 6e 65 20  hat matches one 
21480 6f 66 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20  of following.** 
21490 74 65 6d 70 6c 61 74 65 73 3a 0a 2a 2a 0a 2a 2a  templates:.**.**
214a0 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 3f   <ul>.** <li>  ?
214b0 0a 2a 2a 20 3c 6c 69 3e 20 20 3f 4e 4e 4e 0a 2a  .** <li>  ?NNN.*
214c0 2a 20 3c 6c 69 3e 20 20 3a 56 56 56 0a 2a 2a 20  * <li>  :VVV.** 
214d0 3c 6c 69 3e 20 20 40 56 56 56 0a 2a 2a 20 3c 6c  <li>  @VVV.** <l
214e0 69 3e 20 20 24 56 56 56 0a 2a 2a 20 3c 2f 75 6c  i>  $VVV.** </ul
214f0 3e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 74  >.**.** In the t
21500 65 6d 70 6c 61 74 65 73 20 61 62 6f 76 65 2c 20  emplates above, 
21510 4e 4e 4e 20 72 65 70 72 65 73 65 6e 74 73 20 61  NNN represents a
21520 6e 20 69 6e 74 65 67 65 72 20 6c 69 74 65 72 61  n integer litera
21530 6c 2c 0a 2a 2a 20 61 6e 64 20 56 56 56 20 72 65  l,.** and VVV re
21540 70 72 65 73 65 6e 74 73 20 61 6e 20 61 6c 70 68  presents an alph
21550 61 6e 75 6d 65 72 69 63 20 69 64 65 6e 74 69 66  anumeric identif
21560 65 72 2e 20 20 54 68 65 20 76 61 6c 75 65 73 20  er.  The values 
21570 6f 66 20 74 68 65 73 65 0a 2a 2a 20 70 61 72 61  of these.** para
21580 6d 65 74 65 72 73 20 28 61 6c 73 6f 20 63 61 6c  meters (also cal
21590 6c 65 64 20 22 68 6f 73 74 20 70 61 72 61 6d 65  led "host parame
215a0 74 65 72 20 6e 61 6d 65 73 22 20 6f 72 20 22 53  ter names" or "S
215b0 51 4c 20 70 61 72 61 6d 65 74 65 72 73 22 29 0a  QL parameters").
215c0 2a 2a 20 63 61 6e 20 62 65 20 73 65 74 20 75 73  ** can be set us
215d0 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f  ing the sqlite3_
215e0 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65  bind_*() routine
215f0 73 20 64 65 66 69 6e 65 64 20 68 65 72 65 2e 0a  s defined here..
21600 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20  **.** The first 
21610 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  argument to the 
21620 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29  sqlite3_bind_*()
21630 20 72 6f 75 74 69 6e 65 73 20 69 73 20 61 6c 77   routines is alw
21640 61 79 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72  ays.** a pointer
21650 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33   to the [sqlite3
21660 5f 73 74 6d 74 5d 20 6f 62 6a 65 63 74 20 72 65  _stmt] object re
21670 74 75 72 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 5b  turned from.** [
21680 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
21690 76 32 28 29 5d 20 6f 72 20 69 74 73 20 76 61 72  v2()] or its var
216a0 69 61 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  iants..**.** The
216b0 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
216c0 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66   is the index of
216d0 20 74 68 65 20 53 51 4c 20 70 61 72 61 6d 65 74   the SQL paramet
216e0 65 72 20 74 6f 20 62 65 20 73 65 74 2e 0a 2a 2a  er to be set..**
216f0 20 54 68 65 20 6c 65 66 74 6d 6f 73 74 20 53 51   The leftmost SQ
21700 4c 20 70 61 72 61 6d 65 74 65 72 20 68 61 73 20  L parameter has 
21710 61 6e 20 69 6e 64 65 78 20 6f 66 20 31 2e 20 20  an index of 1.  
21720 57 68 65 6e 20 74 68 65 20 73 61 6d 65 20 6e 61  When the same na
21730 6d 65 64 0a 2a 2a 20 53 51 4c 20 70 61 72 61 6d  med.** SQL param
21740 65 74 65 72 20 69 73 20 75 73 65 64 20 6d 6f 72  eter is used mor
21750 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 73 65 63  e than once, sec
21760 6f 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 65  ond and subseque
21770 6e 74 0a 2a 2a 20 6f 63 63 75 72 72 65 6e 63 65  nt.** occurrence
21780 73 20 68 61 76 65 20 74 68 65 20 73 61 6d 65 20  s have the same 
21790 69 6e 64 65 78 20 61 73 20 74 68 65 20 66 69 72  index as the fir
217a0 73 74 20 6f 63 63 75 72 72 65 6e 63 65 2e 0a 2a  st occurrence..*
217b0 2a 20 54 68 65 20 69 6e 64 65 78 20 66 6f 72 20  * The index for 
217c0 6e 61 6d 65 64 20 70 61 72 61 6d 65 74 65 72 73  named parameters
217d0 20 63 61 6e 20 62 65 20 6c 6f 6f 6b 65 64 20 75   can be looked u
217e0 70 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b  p using the.** [
217f0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72  sqlite3_bind_par
21800 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 20  ameter_index()] 
21810 41 50 49 20 69 66 20 64 65 73 69 72 65 64 2e 20  API if desired. 
21820 20 54 68 65 20 69 6e 64 65 78 0a 2a 2a 20 66 6f   The index.** fo
21830 72 20 22 3f 4e 4e 4e 22 20 70 61 72 61 6d 65 74  r "?NNN" paramet
21840 65 72 73 20 69 73 20 74 68 65 20 76 61 6c 75 65  ers is the value
21850 20 6f 66 20 4e 4e 4e 2e 0a 2a 2a 20 54 68 65 20   of NNN..** The 
21860 4e 4e 4e 20 76 61 6c 75 65 20 6d 75 73 74 20 62  NNN value must b
21870 65 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20  e between 1 and 
21880 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d  the [sqlite3_lim
21890 69 74 28 29 5d 0a 2a 2a 20 70 61 72 61 6d 65 74  it()].** paramet
218a0 65 72 20 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  er [SQLITE_LIMIT
218b0 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52  _VARIABLE_NUMBER
218c0 5d 20 28 64 65 66 61 75 6c 74 20 76 61 6c 75 65  ] (default value
218d0 3a 20 39 39 39 29 2e 0a 2a 2a 0a 2a 2a 20 54 68  : 999)..**.** Th
218e0 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74  e third argument
218f0 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 74 6f   is the value to
21900 20 62 69 6e 64 20 74 6f 20 74 68 65 20 70 61 72   bind to the par
21910 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 6e  ameter..**.** In
21920 20 74 68 6f 73 65 20 72 6f 75 74 69 6e 65 73 20   those routines 
21930 74 68 61 74 20 68 61 76 65 20 61 20 66 6f 75 72  that have a four
21940 74 68 20 61 72 67 75 6d 65 6e 74 2c 20 69 74 73  th argument, its
21950 20 76 61 6c 75 65 20 69 73 20 74 68 65 0a 2a 2a   value is the.**
21960 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
21970 20 69 6e 20 74 68 65 20 70 61 72 61 6d 65 74 65   in the paramete
21980 72 2e 20 20 54 6f 20 62 65 20 63 6c 65 61 72 3a  r.  To be clear:
21990 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 74 68   the value is th
219a0 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 3c  e.** number of <
219b0 75 3e 62 79 74 65 73 3c 2f 75 3e 20 69 6e 20 74  u>bytes</u> in t
219c0 68 65 20 76 61 6c 75 65 2c 20 6e 6f 74 20 74 68  he value, not th
219d0 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72  e number of char
219e0 61 63 74 65 72 73 2e 0a 2a 2a 20 49 66 20 74 68  acters..** If th
219f0 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74  e fourth paramet
21a00 65 72 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20  er is negative, 
21a10 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68  the length of th
21a20 65 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 74  e string is.** t
21a30 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
21a40 65 73 20 75 70 20 74 6f 20 74 68 65 20 66 69 72  es up to the fir
21a50 73 74 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74  st zero terminat
21a60 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  or..**.** The fi
21a70 66 74 68 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  fth argument to 
21a80 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f  sqlite3_bind_blo
21a90 62 28 29 2c 20 73 71 6c 69 74 65 33 5f 62 69 6e  b(), sqlite3_bin
21aa0 64 5f 74 65 78 74 28 29 2c 20 61 6e 64 0a 2a 2a  d_text(), and.**
21ab0 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65   sqlite3_bind_te
21ac0 78 74 31 36 28 29 20 69 73 20 61 20 64 65 73 74  xt16() is a dest
21ad0 72 75 63 74 6f 72 20 75 73 65 64 20 74 6f 20 64  ructor used to d
21ae0 69 73 70 6f 73 65 20 6f 66 20 74 68 65 20 42 4c  ispose of the BL
21af0 4f 42 20 6f 72 0a 2a 2a 20 73 74 72 69 6e 67 20  OB or.** string 
21b00 61 66 74 65 72 20 53 51 4c 69 74 65 20 68 61 73  after SQLite has
21b10 20 66 69 6e 69 73 68 65 64 20 77 69 74 68 20 69   finished with i
21b20 74 2e 20 49 66 20 74 68 65 20 66 69 66 74 68 20  t. If the fifth 
21b30 61 72 67 75 6d 65 6e 74 20 69 73 0a 2a 2a 20 74  argument is.** t
21b40 68 65 20 73 70 65 63 69 61 6c 20 76 61 6c 75 65  he special value
21b50 20 5b 53 51 4c 49 54 45 5f 53 54 41 54 49 43 5d   [SQLITE_STATIC]
21b60 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 61 73  , then SQLite as
21b70 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 0a 2a  sumes that the.*
21b80 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73  * information is
21b90 20 69 6e 20 73 74 61 74 69 63 2c 20 75 6e 6d 61   in static, unma
21ba0 6e 61 67 65 64 20 73 70 61 63 65 20 61 6e 64 20  naged space and 
21bb0 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f  does not need to
21bc0 20 62 65 20 66 72 65 65 64 2e 0a 2a 2a 20 49 66   be freed..** If
21bd0 20 74 68 65 20 66 69 66 74 68 20 61 72 67 75 6d   the fifth argum
21be0 65 6e 74 20 68 61 73 20 74 68 65 20 76 61 6c 75  ent has the valu
21bf0 65 20 5b 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  e [SQLITE_TRANSI
21c00 45 4e 54 5d 2c 20 74 68 65 6e 0a 2a 2a 20 53 51  ENT], then.** SQ
21c10 4c 69 74 65 20 6d 61 6b 65 73 20 69 74 73 20 6f  Lite makes its o
21c20 77 6e 20 70 72 69 76 61 74 65 20 63 6f 70 79 20  wn private copy 
21c30 6f 66 20 74 68 65 20 64 61 74 61 20 69 6d 6d 65  of the data imme
21c40 64 69 61 74 65 6c 79 2c 20 62 65 66 6f 72 65 0a  diately, before.
21c50 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62  ** the sqlite3_b
21c60 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 20  ind_*() routine 
21c70 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54  returns..**.** T
21c80 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  he sqlite3_bind_
21c90 7a 65 72 6f 62 6c 6f 62 28 29 20 72 6f 75 74 69  zeroblob() routi
21ca0 6e 65 20 62 69 6e 64 73 20 61 20 42 4c 4f 42 20  ne binds a BLOB 
21cb0 6f 66 20 6c 65 6e 67 74 68 20 4e 20 74 68 61 74  of length N that
21cc0 0a 2a 2a 20 69 73 20 66 69 6c 6c 65 64 20 77 69  .** is filled wi
21cd0 74 68 20 7a 65 72 6f 65 73 2e 20 20 41 20 7a 65  th zeroes.  A ze
21ce0 72 6f 62 6c 6f 62 20 75 73 65 73 20 61 20 66 69  roblob uses a fi
21cf0 78 65 64 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65  xed amount of me
21d00 6d 6f 72 79 0a 2a 2a 20 28 6a 75 73 74 20 61 6e  mory.** (just an
21d10 20 69 6e 74 65 67 65 72 20 74 6f 20 68 6f 6c 64   integer to hold
21d20 20 69 74 73 20 73 69 7a 65 29 20 77 68 69 6c 65   its size) while
21d30 20 69 74 20 69 73 20 62 65 69 6e 67 20 70 72 6f   it is being pro
21d40 63 65 73 73 65 64 2e 0a 2a 2a 20 5a 65 72 6f 62  cessed..** Zerob
21d50 6c 6f 62 73 20 61 72 65 20 69 6e 74 65 6e 64 65  lobs are intende
21d60 64 20 74 6f 20 73 65 72 76 65 20 61 73 20 70 6c  d to serve as pl
21d70 61 63 65 68 6f 6c 64 65 72 73 20 66 6f 72 20 42  aceholders for B
21d80 4c 4f 42 73 20 77 68 6f 73 65 0a 2a 2a 20 63 6f  LOBs whose.** co
21d90 6e 74 65 6e 74 20 69 73 20 6c 61 74 65 72 20 77  ntent is later w
21da0 72 69 74 74 65 6e 20 75 73 69 6e 67 0a 2a 2a 20  ritten using.** 
21db0 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70  [sqlite3_blob_op
21dc0 65 6e 20 7c 20 69 6e 63 72 65 6d 65 6e 74 61 6c  en | incremental
21dd0 20 42 4c 4f 42 20 49 2f 4f 5d 20 72 6f 75 74 69   BLOB I/O] routi
21de0 6e 65 73 2e 0a 2a 2a 20 41 20 6e 65 67 61 74 69  nes..** A negati
21df0 76 65 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65  ve value for the
21e00 20 7a 65 72 6f 62 6c 6f 62 20 72 65 73 75 6c 74   zeroblob result
21e10 73 20 69 6e 20 61 20 7a 65 72 6f 2d 6c 65 6e 67  s in a zero-leng
21e20 74 68 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 54  th BLOB..**.** T
21e30 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  he sqlite3_bind_
21e40 2a 28 29 20 72 6f 75 74 69 6e 65 73 20 6d 75 73  *() routines mus
21e50 74 20 62 65 20 63 61 6c 6c 65 64 20 61 66 74 65  t be called afte
21e60 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72  r.** [sqlite3_pr
21e70 65 70 61 72 65 5f 76 32 28 29 5d 20 28 61 6e 64  epare_v2()] (and
21e80 20 69 74 73 20 76 61 72 69 61 6e 74 73 29 20 6f   its variants) o
21e90 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74  r [sqlite3_reset
21ea0 28 29 5d 20 61 6e 64 0a 2a 2a 20 62 65 66 6f 72  ()] and.** befor
21eb0 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  e [sqlite3_step(
21ec0 29 5d 2e 0a 2a 2a 20 42 69 6e 64 69 6e 67 73 20  )]..** Bindings 
21ed0 61 72 65 20 6e 6f 74 20 63 6c 65 61 72 65 64 20  are not cleared 
21ee0 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  by the [sqlite3_
21ef0 72 65 73 65 74 28 29 5d 20 72 6f 75 74 69 6e 65  reset()] routine
21f00 2e 0a 2a 2a 20 55 6e 62 6f 75 6e 64 20 70 61 72  ..** Unbound par
21f10 61 6d 65 74 65 72 73 20 61 72 65 20 69 6e 74 65  ameters are inte
21f20 72 70 72 65 74 65 64 20 61 73 20 4e 55 4c 4c 2e  rpreted as NULL.
21f30 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75  .**.** These rou
21f40 74 69 6e 65 73 20 72 65 74 75 72 6e 20 5b 53 51  tines return [SQ
21f50 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63  LITE_OK] on succ
21f60 65 73 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 20  ess or an error 
21f70 63 6f 64 65 20 69 66 0a 2a 2a 20 61 6e 79 74 68  code if.** anyth
21f80 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 20  ing goes wrong. 
21f90 20 5b 53 51 4c 49 54 45 5f 52 41 4e 47 45 5d 20   [SQLITE_RANGE] 
21fa0 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74  is returned if t
21fb0 68 65 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20  he parameter.** 
21fc0 69 6e 64 65 78 20 69 73 20 6f 75 74 20 6f 66 20  index is out of 
21fd0 72 61 6e 67 65 2e 20 20 5b 53 51 4c 49 54 45 5f  range.  [SQLITE_
21fe0 4e 4f 4d 45 4d 5d 20 69 73 20 72 65 74 75 72 6e  NOMEM] is return
21ff0 65 64 20 69 66 20 6d 61 6c 6c 6f 63 28 29 20 66  ed if malloc() f
22000 61 69 6c 73 2e 0a 2a 2a 20 5b 53 51 4c 49 54 45  ails..** [SQLITE
22010 5f 4d 49 53 55 53 45 5d 20 6d 69 67 68 74 20 62  _MISUSE] might b
22020 65 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68  e returned if th
22030 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65  ese routines are
22040 20 63 61 6c 6c 65 64 20 6f 6e 20 61 0a 2a 2a 20   called on a.** 
22050 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20  virtual machine 
22060 74 68 61 74 20 69 73 20 74 68 65 20 77 72 6f 6e  that is the wron
22070 67 20 73 74 61 74 65 20 6f 72 20 77 68 69 63 68  g state or which
22080 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65   has already bee
22090 6e 20 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a 20  n finalized..** 
220a0 44 65 74 65 63 74 69 6f 6e 20 6f 66 20 6d 69 73  Detection of mis
220b0 75 73 65 20 69 73 20 75 6e 72 65 6c 69 61 62 6c  use is unreliabl
220c0 65 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73  e.  Applications
220d0 20 73 68 6f 75 6c 64 20 6e 6f 74 20 64 65 70 65   should not depe
220e0 6e 64 0a 2a 2a 20 6f 6e 20 53 51 4c 49 54 45 5f  nd.** on SQLITE_
220f0 4d 49 53 55 53 45 20 72 65 74 75 72 6e 73 2e 20  MISUSE returns. 
22100 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 69   SQLITE_MISUSE i
22110 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 69 6e  s intended to in
22120 64 69 63 61 74 65 20 61 0a 2a 2a 20 61 20 6c 6f  dicate a.** a lo
22130 67 69 63 20 65 72 72 6f 72 20 69 6e 20 74 68 65  gic error in the
22140 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20 46   application.  F
22150 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f  uture versions o
22160 66 20 53 51 4c 69 74 65 20 6d 69 67 68 74 0a 2a  f SQLite might.*
22170 2a 20 70 61 6e 69 63 20 72 61 74 68 65 72 20 74  * panic rather t
22180 68 61 6e 20 72 65 74 75 72 6e 20 53 51 4c 49 54  han return SQLIT
22190 45 5f 4d 49 53 55 53 45 2e 0a 2a 2a 0a 2a 2a 20  E_MISUSE..**.** 
221a0 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74  See also: [sqlit
221b0 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65  e3_bind_paramete
221c0 72 5f 63 6f 75 6e 74 28 29 5d 2c 0a 2a 2a 20 5b  r_count()],.** [
221d0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72  sqlite3_bind_par
221e0 61 6d 65 74 65 72 5f 6e 61 6d 65 28 29 5d 2c 20  ameter_name()], 
221f0 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e  and [sqlite3_bin
22200 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65  d_parameter_inde
22210 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  x()]..**.** Requ
22220 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
22230 33 35 30 36 5d 20 5b 48 31 33 35 30 39 5d 20 5b  3506] [H13509] [
22240 48 31 33 35 31 32 5d 20 5b 48 31 33 35 31 35 5d  H13512] [H13515]
22250 20 5b 48 31 33 35 31 38 5d 20 5b 48 31 33 35 32   [H13518] [H1352
22260 31 5d 20 5b 48 31 33 35 32 34 5d 20 5b 48 31 33  1] [H13524] [H13
22270 35 32 37 5d 0a 2a 2a 20 5b 48 31 33 35 33 30 5d  527].** [H13530]
22280 20 5b 48 31 33 35 33 33 5d 20 5b 48 31 33 35 33   [H13533] [H1353
22290 36 5d 20 5b 48 31 33 35 33 39 5d 20 5b 48 31 33  6] [H13539] [H13
222a0 35 34 32 5d 20 5b 48 31 33 35 34 35 5d 20 5b 48  542] [H13545] [H
222b0 31 33 35 34 38 5d 20 5b 48 31 33 35 35 31 5d 0a  13548] [H13551].
222c0 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  **.*/.SQLITE_API
222d0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
222e0 64 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 73  d_blob(sqlite3_s
222f0 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74  tmt*, int, const
22300 20 76 6f 69 64 2a 2c 20 69 6e 74 20 6e 2c 20 76   void*, int n, v
22310 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
22320 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
22330 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64 6f 75 62  qlite3_bind_doub
22340 6c 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  le(sqlite3_stmt*
22350 2c 20 69 6e 74 2c 20 64 6f 75 62 6c 65 29 3b 0a  , int, double);.
22360 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
22370 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28  qlite3_bind_int(
22380 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
22390 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
223a0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
223b0 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 71 6c 69  _bind_int64(sqli
223c0 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20  te3_stmt*, int, 
223d0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a  sqlite3_int64);.
223e0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
223f0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c  qlite3_bind_null
22400 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20  (sqlite3_stmt*, 
22410 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  int);.SQLITE_API
22420 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
22430 64 5f 74 65 78 74 28 73 71 6c 69 74 65 33 5f 73  d_text(sqlite3_s
22440 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74  tmt*, int, const
22450 20 63 68 61 72 2a 2c 20 69 6e 74 20 6e 2c 20 76   char*, int n, v
22460 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
22470 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
22480 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74  qlite3_bind_text
22490 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  16(sqlite3_stmt*
224a0 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69  , int, const voi
224b0 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29  d*, int, void(*)
224c0 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45  (void*));.SQLITE
224d0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
224e0 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73 71 6c 69  _bind_value(sqli
224f0 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20  te3_stmt*, int, 
22500 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 61  const sqlite3_va
22510 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  lue*);.SQLITE_AP
22520 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69  I int sqlite3_bi
22530 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69  nd_zeroblob(sqli
22540 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20  te3_stmt*, int, 
22550 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  int n);../*.** C
22560 41 50 49 33 52 45 46 3a 20 4e 75 6d 62 65 72 20  API3REF: Number 
22570 4f 66 20 53 51 4c 20 50 61 72 61 6d 65 74 65 72  Of SQL Parameter
22580 73 20 7b 48 31 33 36 30 30 7d 20 3c 53 37 30 33  s {H13600} <S703
22590 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  00>.**.** This r
225a0 6f 75 74 69 6e 65 20 63 61 6e 20 62 65 20 75 73  outine can be us
225b0 65 64 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6e  ed to find the n
225c0 75 6d 62 65 72 20 6f 66 20 5b 53 51 4c 20 70 61  umber of [SQL pa
225d0 72 61 6d 65 74 65 72 73 5d 0a 2a 2a 20 69 6e 20  rameters].** in 
225e0 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  a [prepared stat
225f0 65 6d 65 6e 74 5d 2e 20 20 53 51 4c 20 70 61 72  ement].  SQL par
22600 61 6d 65 74 65 72 73 20 61 72 65 20 74 6f 6b 65  ameters are toke
22610 6e 73 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 72  ns of the.** for
22620 6d 20 22 3f 22 2c 20 22 3f 4e 4e 4e 22 2c 20 22  m "?", "?NNN", "
22630 3a 41 41 41 22 2c 20 22 24 41 41 41 22 2c 20 6f  :AAA", "$AAA", o
22640 72 20 22 40 41 41 41 22 20 74 68 61 74 20 73 65  r "@AAA" that se
22650 72 76 65 20 61 73 0a 2a 2a 20 70 6c 61 63 65 68  rve as.** placeh
22660 6f 6c 64 65 72 73 20 66 6f 72 20 76 61 6c 75 65  olders for value
22670 73 20 74 68 61 74 20 61 72 65 20 5b 73 71 6c 69  s that are [sqli
22680 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 7c 20  te3_bind_blob | 
22690 62 6f 75 6e 64 5d 0a 2a 2a 20 74 6f 20 74 68 65  bound].** to the
226a0 20 70 61 72 61 6d 65 74 65 72 73 20 61 74 20 61   parameters at a
226b0 20 6c 61 74 65 72 20 74 69 6d 65 2e 0a 2a 2a 0a   later time..**.
226c0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
226d0 61 63 74 75 61 6c 6c 79 20 72 65 74 75 72 6e 73  actually returns
226e0 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   the index of th
226f0 65 20 6c 61 72 67 65 73 74 20 28 72 69 67 68 74  e largest (right
22700 6d 6f 73 74 29 0a 2a 2a 20 70 61 72 61 6d 65 74  most).** paramet
22710 65 72 2e 20 46 6f 72 20 61 6c 6c 20 66 6f 72 6d  er. For all form
22720 73 20 65 78 63 65 70 74 20 3f 4e 4e 4e 2c 20 74  s except ?NNN, t
22730 68 69 73 20 77 69 6c 6c 20 63 6f 72 72 65 73 70  his will corresp
22740 6f 6e 64 20 74 6f 20 74 68 65 0a 2a 2a 20 6e 75  ond to the.** nu
22750 6d 62 65 72 20 6f 66 20 75 6e 69 71 75 65 20 70  mber of unique p
22760 61 72 61 6d 65 74 65 72 73 2e 20 20 49 66 20 70  arameters.  If p
22770 61 72 61 6d 65 74 65 72 73 20 6f 66 20 74 68 65  arameters of the
22780 20 3f 4e 4e 4e 20 61 72 65 20 75 73 65 64 2c 0a   ?NNN are used,.
22790 2a 2a 20 74 68 65 72 65 20 6d 61 79 20 62 65 20  ** there may be 
227a0 67 61 70 73 20 69 6e 20 74 68 65 20 6c 69 73 74  gaps in the list
227b0 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
227c0 3a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  : [sqlite3_bind_
227d0 62 6c 6f 62 7c 73 71 6c 69 74 65 33 5f 62 69 6e  blob|sqlite3_bin
227e0 64 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65  d()],.** [sqlite
227f0 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72  3_bind_parameter
22800 5f 6e 61 6d 65 28 29 5d 2c 20 61 6e 64 0a 2a 2a  _name()], and.**
22810 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70   [sqlite3_bind_p
22820 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29  arameter_index()
22830 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  ]..**.** Require
22840 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 36 30  ments:.** [H1360
22850 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  1].*/.SQLITE_API
22860 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
22870 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e  d_parameter_coun
22880 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29  t(sqlite3_stmt*)
22890 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
228a0 46 3a 20 4e 61 6d 65 20 4f 66 20 41 20 48 6f 73  F: Name Of A Hos
228b0 74 20 50 61 72 61 6d 65 74 65 72 20 7b 48 31 33  t Parameter {H13
228c0 36 32 30 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 2a  620} <S70300>.**
228d0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
228e0 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
228f0 65 72 20 74 6f 20 74 68 65 20 6e 61 6d 65 20 6f  er to the name o
22900 66 20 74 68 65 20 6e 2d 74 68 0a 2a 2a 20 5b 53  f the n-th.** [S
22910 51 4c 20 70 61 72 61 6d 65 74 65 72 5d 20 69 6e  QL parameter] in
22920 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61   a [prepared sta
22930 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 53 51 4c 20  tement]..** SQL 
22940 70 61 72 61 6d 65 74 65 72 73 20 6f 66 20 74 68  parameters of th
22950 65 20 66 6f 72 6d 20 22 3f 4e 4e 4e 22 20 6f 72  e form "?NNN" or
22960 20 22 3a 41 41 41 22 20 6f 72 20 22 40 41 41 41   ":AAA" or "@AAA
22970 22 20 6f 72 20 22 24 41 41 41 22 0a 2a 2a 20 68  " or "$AAA".** h
22980 61 76 65 20 61 20 6e 61 6d 65 20 77 68 69 63 68  ave a name which
22990 20 69 73 20 74 68 65 20 73 74 72 69 6e 67 20 22   is the string "
229a0 3f 4e 4e 4e 22 20 6f 72 20 22 3a 41 41 41 22 20  ?NNN" or ":AAA" 
229b0 6f 72 20 22 40 41 41 41 22 20 6f 72 20 22 24 41  or "@AAA" or "$A
229c0 41 41 22 0a 2a 2a 20 72 65 73 70 65 63 74 69 76  AA".** respectiv
229d0 65 6c 79 2e 0a 2a 2a 20 49 6e 20 6f 74 68 65 72  ely..** In other
229e0 20 77 6f 72 64 73 2c 20 74 68 65 20 69 6e 69 74   words, the init
229f0 69 61 6c 20 22 3a 22 20 6f 72 20 22 24 22 20 6f  ial ":" or "$" o
22a00 72 20 22 40 22 20 6f 72 20 22 3f 22 0a 2a 2a 20  r "@" or "?".** 
22a10 69 73 20 69 6e 63 6c 75 64 65 64 20 61 73 20 70  is included as p
22a20 61 72 74 20 6f 66 20 74 68 65 20 6e 61 6d 65 2e  art of the name.
22a30 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 73 20 6f  .** Parameters o
22a40 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 22 20 77  f the form "?" w
22a50 69 74 68 6f 75 74 20 61 20 66 6f 6c 6c 6f 77 69  ithout a followi
22a60 6e 67 20 69 6e 74 65 67 65 72 20 68 61 76 65 20  ng integer have 
22a70 6e 6f 20 6e 61 6d 65 0a 2a 2a 20 61 6e 64 20 61  no name.** and a
22a80 72 65 20 61 6c 73 6f 20 72 65 66 65 72 72 65 64  re also referred
22a90 20 74 6f 20 61 73 20 22 61 6e 6f 6e 79 6d 6f 75   to as "anonymou
22aa0 73 20 70 61 72 61 6d 65 74 65 72 73 22 2e 0a 2a  s parameters"..*
22ab0 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 68  *.** The first h
22ac0 6f 73 74 20 70 61 72 61 6d 65 74 65 72 20 68 61  ost parameter ha
22ad0 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 31 2c  s an index of 1,
22ae0 20 6e 6f 74 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66   not 0..**.** If
22af0 20 74 68 65 20 76 61 6c 75 65 20 6e 20 69 73 20   the value n is 
22b00 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 6f 72 20  out of range or 
22b10 69 66 20 74 68 65 20 6e 2d 74 68 20 70 61 72 61  if the n-th para
22b20 6d 65 74 65 72 20 69 73 0a 2a 2a 20 6e 61 6d 65  meter is.** name
22b30 6c 65 73 73 2c 20 74 68 65 6e 20 4e 55 4c 4c 20  less, then NULL 
22b40 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68  is returned.  Th
22b50 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e  e returned strin
22b60 67 20 69 73 0a 2a 2a 20 61 6c 77 61 79 73 20 69  g is.** always i
22b70 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67  n UTF-8 encoding
22b80 20 65 76 65 6e 20 69 66 20 74 68 65 20 6e 61 6d   even if the nam
22b90 65 64 20 70 61 72 61 6d 65 74 65 72 20 77 61 73  ed parameter was
22ba0 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 6c 79 20 73  .** originally s
22bb0 70 65 63 69 66 69 65 64 20 61 73 20 55 54 46 2d  pecified as UTF-
22bc0 31 36 20 69 6e 20 5b 73 71 6c 69 74 65 33 5f 70  16 in [sqlite3_p
22bd0 72 65 70 61 72 65 31 36 28 29 5d 20 6f 72 0a 2a  repare16()] or.*
22be0 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  * [sqlite3_prepa
22bf0 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a  re16_v2()]..**.*
22c00 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c  * See also: [sql
22c10 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73  ite3_bind_blob|s
22c20 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d 2c 0a  qlite3_bind()],.
22c30 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64  ** [sqlite3_bind
22c40 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74  _parameter_count
22c50 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c  ()], and.** [sql
22c60 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
22c70 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a 2a  ter_index()]..**
22c80 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
22c90 3a 0a 2a 2a 20 5b 48 31 33 36 32 31 5d 0a 2a 2f  :.** [H13621].*/
22ca0 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
22cb0 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  t char *sqlite3_
22cc0 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e  bind_parameter_n
22cd0 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ame(sqlite3_stmt
22ce0 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20  *, int);../*.** 
22cf0 43 41 50 49 33 52 45 46 3a 20 49 6e 64 65 78 20  CAPI3REF: Index 
22d00 4f 66 20 41 20 50 61 72 61 6d 65 74 65 72 20 57  Of A Parameter W
22d10 69 74 68 20 41 20 47 69 76 65 6e 20 4e 61 6d 65  ith A Given Name
22d20 20 7b 48 31 33 36 34 30 7d 20 3c 53 37 30 33 30   {H13640} <S7030
22d30 30 3e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  0>.**.** Return 
22d40 74 68 65 20 69 6e 64 65 78 20 6f 66 20 61 6e 20  the index of an 
22d50 53 51 4c 20 70 61 72 61 6d 65 74 65 72 20 67 69  SQL parameter gi
22d60 76 65 6e 20 69 74 73 20 6e 61 6d 65 2e 20 20 54  ven its name.  T
22d70 68 65 0a 2a 2a 20 69 6e 64 65 78 20 76 61 6c 75  he.** index valu
22d80 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 73 75  e returned is su
22d90 69 74 61 62 6c 65 20 66 6f 72 20 75 73 65 20 61  itable for use a
22da0 73 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20  s the second.** 
22db0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71  parameter to [sq
22dc0 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c  lite3_bind_blob|
22dd0 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d 2e  sqlite3_bind()].
22de0 20 20 41 20 7a 65 72 6f 0a 2a 2a 20 69 73 20 72    A zero.** is r
22df0 65 74 75 72 6e 65 64 20 69 66 20 6e 6f 20 6d 61  eturned if no ma
22e00 74 63 68 69 6e 67 20 70 61 72 61 6d 65 74 65 72  tching parameter
22e10 20 69 73 20 66 6f 75 6e 64 2e 20 20 54 68 65 20   is found.  The 
22e20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 6e 61 6d  parameter.** nam
22e30 65 20 6d 75 73 74 20 62 65 20 67 69 76 65 6e 20  e must be given 
22e40 69 6e 20 55 54 46 2d 38 20 65 76 65 6e 20 69 66  in UTF-8 even if
22e50 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 73 74   the original st
22e60 61 74 65 6d 65 6e 74 0a 2a 2a 20 77 61 73 20 70  atement.** was p
22e70 72 65 70 61 72 65 64 20 66 72 6f 6d 20 55 54 46  repared from UTF
22e80 2d 31 36 20 74 65 78 74 20 75 73 69 6e 67 20 5b  -16 text using [
22e90 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31  sqlite3_prepare1
22ea0 36 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53  6_v2()]..**.** S
22eb0 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65  ee also: [sqlite
22ec0 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69  3_bind_blob|sqli
22ed0 74 65 33 5f 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20  te3_bind()],.** 
22ee0 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61  [sqlite3_bind_pa
22ef0 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 29 5d  rameter_count()]
22f00 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65  , and.** [sqlite
22f10 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72  3_bind_parameter
22f20 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a  _index()]..**.**
22f30 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
22f40 2a 20 5b 48 31 33 36 34 31 5d 0a 2a 2f 0a 53 51  * [H13641].*/.SQ
22f50 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
22f60 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
22f70 74 65 72 5f 69 6e 64 65 78 28 73 71 6c 69 74 65  ter_index(sqlite
22f80 33 5f 73 74 6d 74 2a 2c 20 63 6f 6e 73 74 20 63  3_stmt*, const c
22f90 68 61 72 20 2a 7a 4e 61 6d 65 29 3b 0a 0a 2f 2a  har *zName);../*
22fa0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65  .** CAPI3REF: Re
22fb0 73 65 74 20 41 6c 6c 20 42 69 6e 64 69 6e 67 73  set All Bindings
22fc0 20 4f 6e 20 41 20 50 72 65 70 61 72 65 64 20 53   On A Prepared S
22fd0 74 61 74 65 6d 65 6e 74 20 7b 48 31 33 36 36 30  tatement {H13660
22fe0 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a  } <S70300>.**.**
22ff0 20 43 6f 6e 74 72 61 72 79 20 74 6f 20 74 68 65   Contrary to the
23000 20 69 6e 74 75 69 74 69 6f 6e 20 6f 66 20 6d 61   intuition of ma
23010 6e 79 2c 20 5b 73 71 6c 69 74 65 33 5f 72 65 73  ny, [sqlite3_res
23020 65 74 28 29 5d 20 64 6f 65 73 20 6e 6f 74 20 72  et()] does not r
23030 65 73 65 74 0a 2a 2a 20 74 68 65 20 5b 73 71 6c  eset.** the [sql
23040 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 7c  ite3_bind_blob |
23050 20 62 69 6e 64 69 6e 67 73 5d 20 6f 6e 20 61 20   bindings] on a 
23060 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
23070 65 6e 74 5d 2e 0a 2a 2a 20 55 73 65 20 74 68 69  ent]..** Use thi
23080 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 72 65 73  s routine to res
23090 65 74 20 61 6c 6c 20 68 6f 73 74 20 70 61 72 61  et all host para
230a0 6d 65 74 65 72 73 20 74 6f 20 4e 55 4c 4c 2e 0a  meters to NULL..
230b0 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
230c0 74 73 3a 0a 2a 2a 20 5b 48 31 33 36 36 31 5d 0a  ts:.** [H13661].
230d0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
230e0 74 20 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f  t sqlite3_clear_
230f0 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 33  bindings(sqlite3
23100 5f 73 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  _stmt*);../*.** 
23110 43 41 50 49 33 52 45 46 3a 20 4e 75 6d 62 65 72  CAPI3REF: Number
23120 20 4f 66 20 43 6f 6c 75 6d 6e 73 20 49 6e 20 41   Of Columns In A
23130 20 52 65 73 75 6c 74 20 53 65 74 20 7b 48 31 33   Result Set {H13
23140 37 31 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a  710} <S10700>.**
23150 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
23160 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
23170 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
23180 65 74 20 72 65 74 75 72 6e 65 64 20 62 79 20 74  et returned by t
23190 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20  he.** [prepared 
231a0 73 74 61 74 65 6d 65 6e 74 5d 2e 20 54 68 69 73  statement]. This
231b0 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
231c0 20 30 20 69 66 20 70 53 74 6d 74 20 69 73 20 61   0 if pStmt is a
231d0 6e 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65  n SQL.** stateme
231e0 6e 74 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74  nt that does not
231f0 20 72 65 74 75 72 6e 20 64 61 74 61 20 28 66 6f   return data (fo
23200 72 20 65 78 61 6d 70 6c 65 20 61 6e 20 5b 55 50  r example an [UP
23210 44 41 54 45 5d 29 2e 0a 2a 2a 0a 2a 2a 20 52 65  DATE])..**.** Re
23220 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b  quirements:.** [
23230 48 31 33 37 31 31 5d 0a 2a 2f 0a 53 51 4c 49 54  H13711].*/.SQLIT
23240 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
23250 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 73  3_column_count(s
23260 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
23270 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  mt);../*.** CAPI
23280 33 52 45 46 3a 20 43 6f 6c 75 6d 6e 20 4e 61 6d  3REF: Column Nam
23290 65 73 20 49 6e 20 41 20 52 65 73 75 6c 74 20 53  es In A Result S
232a0 65 74 20 7b 48 31 33 37 32 30 7d 20 3c 53 31 30  et {H13720} <S10
232b0 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  700>.**.** These
232c0 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e   routines return
232d0 20 74 68 65 20 6e 61 6d 65 20 61 73 73 69 67 6e   the name assign
232e0 65 64 20 74 6f 20 61 20 70 61 72 74 69 63 75 6c  ed to a particul
232f0 61 72 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 6e 20  ar column.** in 
23300 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f  the result set o
23310 66 20 61 20 5b 53 45 4c 45 43 54 5d 20 73 74 61  f a [SELECT] sta
23320 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 73 71 6c  tement.  The sql
23330 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65  ite3_column_name
23340 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20  ().** interface 
23350 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
23360 72 20 74 6f 20 61 20 7a 65 72 6f 2d 74 65 72 6d  r to a zero-term
23370 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74 72  inated UTF-8 str
23380 69 6e 67 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74  ing.** and sqlit
23390 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36  e3_column_name16
233a0 28 29 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  () returns a poi
233b0 6e 74 65 72 20 74 6f 20 61 20 7a 65 72 6f 2d 74  nter to a zero-t
233c0 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 55 54 46  erminated.** UTF
233d0 2d 31 36 20 73 74 72 69 6e 67 2e 20 20 54 68 65  -16 string.  The
233e0 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72   first parameter
233f0 20 69 73 20 74 68 65 20 5b 70 72 65 70 61 72 65   is the [prepare
23400 64 20 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20  d statement].** 
23410 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20  that implements 
23420 74 68 65 20 5b 53 45 4c 45 43 54 5d 20 73 74 61  the [SELECT] sta
23430 74 65 6d 65 6e 74 2e 20 54 68 65 20 73 65 63 6f  tement. The seco
23440 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  nd parameter is 
23450 74 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 75  the.** column nu
23460 6d 62 65 72 2e 20 20 54 68 65 20 6c 65 66 74 6d  mber.  The leftm
23470 6f 73 74 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 75  ost column is nu
23480 6d 62 65 72 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 68  mber 0..**.** Th
23490 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e  e returned strin
234a0 67 20 70 6f 69 6e 74 65 72 20 69 73 20 76 61 6c  g pointer is val
234b0 69 64 20 75 6e 74 69 6c 20 65 69 74 68 65 72 20  id until either 
234c0 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74  the [prepared st
234d0 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 69 73 20 64  atement].** is d
234e0 65 73 74 72 6f 79 65 64 20 62 79 20 5b 73 71 6c  estroyed by [sql
234f0 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d  ite3_finalize()]
23500 20 6f 72 20 75 6e 74 69 6c 20 74 68 65 20 6e 65   or until the ne
23510 78 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71  xt call to.** sq
23520 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d  lite3_column_nam
23530 65 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 63  e() or sqlite3_c
23540 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 29 20 6f  olumn_name16() o
23550 6e 20 74 68 65 20 73 61 6d 65 20 63 6f 6c 75 6d  n the same colum
23560 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 71 6c 69  n..**.** If sqli
23570 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 66 61 69  te3_malloc() fai
23580 6c 73 20 64 75 72 69 6e 67 20 74 68 65 20 70 72  ls during the pr
23590 6f 63 65 73 73 69 6e 67 20 6f 66 20 65 69 74 68  ocessing of eith
235a0 65 72 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 28 66  er routine.** (f
235b0 6f 72 20 65 78 61 6d 70 6c 65 20 64 75 72 69 6e  or example durin
235c0 67 20 61 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66  g a conversion f
235d0 72 6f 6d 20 55 54 46 2d 38 20 74 6f 20 55 54 46  rom UTF-8 to UTF
235e0 2d 31 36 29 20 74 68 65 6e 20 61 0a 2a 2a 20 4e  -16) then a.** N
235f0 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73 20 72  ULL pointer is r
23600 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  eturned..**.** T
23610 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 72 65 73  he name of a res
23620 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 74 68  ult column is th
23630 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 22  e value of the "
23640 41 53 22 20 63 6c 61 75 73 65 20 66 6f 72 0a 2a  AS" clause for.*
23650 2a 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2c 20 69  * that column, i
23660 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 41 53  f there is an AS
23670 20 63 6c 61 75 73 65 2e 20 20 49 66 20 74 68 65   clause.  If the
23680 72 65 20 69 73 20 6e 6f 20 41 53 20 63 6c 61 75  re is no AS clau
23690 73 65 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 6e  se.** then the n
236a0 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d  ame of the colum
236b0 6e 20 69 73 20 75 6e 73 70 65 63 69 66 69 65 64  n is unspecified
236c0 20 61 6e 64 20 6d 61 79 20 63 68 61 6e 67 65 20   and may change 
236d0 66 72 6f 6d 0a 2a 2a 20 6f 6e 65 20 72 65 6c 65  from.** one rele
236e0 61 73 65 20 6f 66 20 53 51 4c 69 74 65 20 74 6f  ase of SQLite to
236f0 20 74 68 65 20 6e 65 78 74 2e 0a 2a 2a 0a 2a 2a   the next..**.**
23700 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
23710 2a 20 5b 48 31 33 37 32 31 5d 20 5b 48 31 33 37  * [H13721] [H137
23720 32 33 5d 20 5b 48 31 33 37 32 34 5d 20 5b 48 31  23] [H13724] [H1
23730 33 37 32 35 5d 20 5b 48 31 33 37 32 36 5d 20 5b  3725] [H13726] [
23740 48 31 33 37 32 37 5d 0a 2a 2f 0a 53 51 4c 49 54  H13727].*/.SQLIT
23750 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72  E_API const char
23760 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
23770 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74  _name(sqlite3_st
23780 6d 74 2a 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c  mt*, int N);.SQL
23790 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f  ITE_API const vo
237a0 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  id *sqlite3_colu
237b0 6d 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65  mn_name16(sqlite
237c0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 4e 29 3b  3_stmt*, int N);
237d0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
237e0 3a 20 53 6f 75 72 63 65 20 4f 66 20 44 61 74 61  : Source Of Data
237f0 20 49 6e 20 41 20 51 75 65 72 79 20 52 65 73 75   In A Query Resu
23800 6c 74 20 7b 48 31 33 37 34 30 7d 20 3c 53 31 30  lt {H13740} <S10
23810 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  700>.**.** These
23820 20 72 6f 75 74 69 6e 65 73 20 70 72 6f 76 69 64   routines provid
23830 65 20 61 20 6d 65 61 6e 73 20 74 6f 20 64 65 74  e a means to det
23840 65 72 6d 69 6e 65 20 77 68 61 74 20 63 6f 6c 75  ermine what colu
23850 6d 6e 20 6f 66 20 77 68 61 74 0a 2a 2a 20 74 61  mn of what.** ta
23860 62 6c 65 20 69 6e 20 77 68 69 63 68 20 64 61 74  ble in which dat
23870 61 62 61 73 65 20 61 20 72 65 73 75 6c 74 20 6f  abase a result o
23880 66 20 61 20 5b 53 45 4c 45 43 54 5d 20 73 74 61  f a [SELECT] sta
23890 74 65 6d 65 6e 74 20 63 6f 6d 65 73 20 66 72 6f  tement comes fro
238a0 6d 2e 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f  m..** The name o
238b0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6f  f the database o
238c0 72 20 74 61 62 6c 65 20 6f 72 20 63 6f 6c 75 6d  r table or colum
238d0 6e 20 63 61 6e 20 62 65 20 72 65 74 75 72 6e 65  n can be returne
238e0 64 20 61 73 0a 2a 2a 20 65 69 74 68 65 72 20 61  d as.** either a
238f0 20 55 54 46 2d 38 20 6f 72 20 55 54 46 2d 31 36   UTF-8 or UTF-16
23900 20 73 74 72 69 6e 67 2e 20 20 54 68 65 20 5f 64   string.  The _d
23910 61 74 61 62 61 73 65 5f 20 72 6f 75 74 69 6e 65  atabase_ routine
23920 73 20 72 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20  s return.** the 
23930 64 61 74 61 62 61 73 65 20 6e 61 6d 65 2c 20 74  database name, t
23940 68 65 20 5f 74 61 62 6c 65 5f 20 72 6f 75 74 69  he _table_ routi
23950 6e 65 73 20 72 65 74 75 72 6e 20 74 68 65 20 74  nes return the t
23960 61 62 6c 65 20 6e 61 6d 65 2c 20 61 6e 64 0a 2a  able name, and.*
23970 2a 20 74 68 65 20 6f 72 69 67 69 6e 5f 20 72 6f  * the origin_ ro
23980 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 74 68  utines return th
23990 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2e 0a 2a  e column name..*
239a0 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 73  * The returned s
239b0 74 72 69 6e 67 20 69 73 20 76 61 6c 69 64 20 75  tring is valid u
239c0 6e 74 69 6c 20 74 68 65 20 5b 70 72 65 70 61 72  ntil the [prepar
239d0 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 69 73  ed statement] is
239e0 20 64 65 73 74 72 6f 79 65 64 0a 2a 2a 20 75 73   destroyed.** us
239f0 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e  ing [sqlite3_fin
23a00 61 6c 69 7a 65 28 29 5d 20 6f 72 20 75 6e 74 69  alize()] or unti
23a10 6c 20 74 68 65 20 73 61 6d 65 20 69 6e 66 6f 72  l the same infor
23a20 6d 61 74 69 6f 6e 20 69 73 20 72 65 71 75 65 73  mation is reques
23a30 74 65 64 0a 2a 2a 20 61 67 61 69 6e 20 69 6e 20  ted.** again in 
23a40 61 20 64 69 66 66 65 72 65 6e 74 20 65 6e 63 6f  a different enco
23a50 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ding..**.** The 
23a60 6e 61 6d 65 73 20 72 65 74 75 72 6e 65 64 20 61  names returned a
23a70 72 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  re the original 
23a80 75 6e 2d 61 6c 69 61 73 65 64 20 6e 61 6d 65 73  un-aliased names
23a90 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62   of the.** datab
23aa0 61 73 65 2c 20 74 61 62 6c 65 2c 20 61 6e 64 20  ase, table, and 
23ab0 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  column..**.** Th
23ac0 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
23ad0 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   to the followin
23ae0 67 20 63 61 6c 6c 73 20 69 73 20 61 20 5b 70 72  g calls is a [pr
23af0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
23b00 5d 2e 0a 2a 2a 20 54 68 65 73 65 20 66 75 6e 63  ]..** These func
23b10 74 69 6f 6e 73 20 72 65 74 75 72 6e 20 69 6e 66  tions return inf
23b20 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74  ormation about t
23b30 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 72 65  he Nth column re
23b40 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 74 68 65  turned by.** the
23b50 20 73 74 61 74 65 6d 65 6e 74 2c 20 77 68 65 72   statement, wher
23b60 65 20 4e 20 69 73 20 74 68 65 20 73 65 63 6f 6e  e N is the secon
23b70 64 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d  d function argum
23b80 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ent..**.** If th
23b90 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 72 65 74  e Nth column ret
23ba0 75 72 6e 65 64 20 62 79 20 74 68 65 20 73 74 61  urned by the sta
23bb0 74 65 6d 65 6e 74 20 69 73 20 61 6e 20 65 78 70  tement is an exp
23bc0 72 65 73 73 69 6f 6e 20 6f 72 0a 2a 2a 20 73 75  ression or.** su
23bd0 62 71 75 65 72 79 20 61 6e 64 20 69 73 20 6e 6f  bquery and is no
23be0 74 20 61 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65  t a column value
23bf0 2c 20 74 68 65 6e 20 61 6c 6c 20 6f 66 20 74 68  , then all of th
23c00 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 72 65  ese functions re
23c10 74 75 72 6e 0a 2a 2a 20 4e 55 4c 4c 2e 20 20 54  turn.** NULL.  T
23c20 68 65 73 65 20 72 6f 75 74 69 6e 65 20 6d 69 67  hese routine mig
23c30 68 74 20 61 6c 73 6f 20 72 65 74 75 72 6e 20 4e  ht also return N
23c40 55 4c 4c 20 69 66 20 61 20 6d 65 6d 6f 72 79 20  ULL if a memory 
23c50 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72  allocation error
23c60 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 20 4f 74 68  .** occurs.  Oth
23c70 65 72 77 69 73 65 2c 20 74 68 65 79 20 72 65 74  erwise, they ret
23c80 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  urn the name of 
23c90 74 68 65 20 61 74 74 61 63 68 65 64 20 64 61 74  the attached dat
23ca0 61 62 61 73 65 2c 20 74 61 62 6c 65 0a 2a 2a 20  abase, table.** 
23cb0 61 6e 64 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20  and column that 
23cc0 71 75 65 72 79 20 72 65 73 75 6c 74 20 63 6f 6c  query result col
23cd0 75 6d 6e 20 77 61 73 20 65 78 74 72 61 63 74 65  umn was extracte
23ce0 64 20 66 72 6f 6d 2e 0a 2a 2a 0a 2a 2a 20 41 73  d from..**.** As
23cf0 20 77 69 74 68 20 61 6c 6c 20 6f 74 68 65 72 20   with all other 
23d00 53 51 4c 69 74 65 20 41 50 49 73 2c 20 74 68 6f  SQLite APIs, tho
23d10 73 65 20 70 6f 73 74 66 69 78 65 64 20 77 69 74  se postfixed wit
23d20 68 20 22 31 36 22 20 72 65 74 75 72 6e 0a 2a 2a  h "16" return.**
23d30 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20   UTF-16 encoded 
23d40 73 74 72 69 6e 67 73 2c 20 74 68 65 20 6f 74 68  strings, the oth
23d50 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74  er functions ret
23d60 75 72 6e 20 55 54 46 2d 38 2e 20 7b 45 4e 44 7d  urn UTF-8. {END}
23d70 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 41 50 49  .**.** These API
23d80 73 20 61 72 65 20 6f 6e 6c 79 20 61 76 61 69 6c  s are only avail
23d90 61 62 6c 65 20 69 66 20 74 68 65 20 6c 69 62 72  able if the libr
23da0 61 72 79 20 77 61 73 20 63 6f 6d 70 69 6c 65 64  ary was compiled
23db0 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 5b 53 51   with the.** [SQ
23dc0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55  LITE_ENABLE_COLU
23dd0 4d 4e 5f 4d 45 54 41 44 41 54 41 5d 20 43 2d 70  MN_METADATA] C-p
23de0 72 65 70 72 6f 63 65 73 73 6f 72 20 73 79 6d 62  reprocessor symb
23df0 6f 6c 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a  ol defined..**.*
23e00 2a 20 7b 41 31 33 37 35 31 7d 0a 2a 2a 20 49 66  * {A13751}.** If
23e10 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 74 68 72   two or more thr
23e20 65 61 64 73 20 63 61 6c 6c 20 6f 6e 65 20 6f 72  eads call one or
23e30 20 6d 6f 72 65 20 6f 66 20 74 68 65 73 65 20 72   more of these r
23e40 6f 75 74 69 6e 65 73 20 61 67 61 69 6e 73 74 20  outines against 
23e50 74 68 65 20 73 61 6d 65 0a 2a 2a 20 70 72 65 70  the same.** prep
23e60 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 61  ared statement a
23e70 6e 64 20 63 6f 6c 75 6d 6e 20 61 74 20 74 68 65  nd column at the
23e80 20 73 61 6d 65 20 74 69 6d 65 20 74 68 65 6e 20   same time then 
23e90 74 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 0a  the results are.
23ea0 2a 2a 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a  ** undefined..**
23eb0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
23ec0 3a 0a 2a 2a 20 5b 48 31 33 37 34 31 5d 20 5b 48  :.** [H13741] [H
23ed0 31 33 37 34 32 5d 20 5b 48 31 33 37 34 33 5d 20  13742] [H13743] 
23ee0 5b 48 31 33 37 34 34 5d 20 5b 48 31 33 37 34 35  [H13744] [H13745
23ef0 5d 20 5b 48 31 33 37 34 36 5d 20 5b 48 31 33 37  ] [H13746] [H137
23f00 34 38 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 77 6f  48].**.** If two
23f10 20 6f 72 20 6d 6f 72 65 20 74 68 72 65 61 64 73   or more threads
23f20 20 63 61 6c 6c 20 6f 6e 65 20 6f 72 20 6d 6f 72   call one or mor
23f30 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f  e.** [sqlite3_co
23f40 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61  lumn_database_na
23f50 6d 65 20 7c 20 63 6f 6c 75 6d 6e 20 6d 65 74 61  me | column meta
23f60 64 61 74 61 20 69 6e 74 65 72 66 61 63 65 73 5d  data interfaces]
23f70 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 61 6d 65  .** for the same
23f80 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
23f90 6d 65 6e 74 5d 20 61 6e 64 20 72 65 73 75 6c 74  ment] and result
23fa0 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 61 74 20 74 68   column.** at th
23fb0 65 20 73 61 6d 65 20 74 69 6d 65 20 74 68 65 6e  e same time then
23fc0 20 74 68 65 20 72 65 73 75 6c 74 73 20 61 72 65   the results are
23fd0 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53   undefined..*/.S
23fe0 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
23ff0 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f  char *sqlite3_co
24000 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61  lumn_database_na
24010 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  me(sqlite3_stmt*
24020 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50  ,int);.SQLITE_AP
24030 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  I const void *sq
24040 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74  lite3_column_dat
24050 61 62 61 73 65 5f 6e 61 6d 65 31 36 28 73 71 6c  abase_name16(sql
24060 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b  ite3_stmt*,int);
24070 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
24080 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  t char *sqlite3_
24090 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d  column_table_nam
240a0 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  e(sqlite3_stmt*,
240b0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  int);.SQLITE_API
240c0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c   const void *sql
240d0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c  ite3_column_tabl
240e0 65 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33  e_name16(sqlite3
240f0 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c  _stmt*,int);.SQL
24100 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68  ITE_API const ch
24110 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ar *sqlite3_colu
24120 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 28 73  mn_origin_name(s
24130 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
24140 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  );.SQLITE_API co
24150 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
24160 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f  3_column_origin_
24170 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73  name16(sqlite3_s
24180 74 6d 74 2a 2c 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a  tmt*,int);../*.*
24190 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 63 6c  * CAPI3REF: Decl
241a0 61 72 65 64 20 44 61 74 61 74 79 70 65 20 4f 66  ared Datatype Of
241b0 20 41 20 51 75 65 72 79 20 52 65 73 75 6c 74 20   A Query Result 
241c0 7b 48 31 33 37 36 30 7d 20 3c 53 31 30 37 30 30  {H13760} <S10700
241d0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73  >.**.** The firs
241e0 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61  t parameter is a
241f0 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
24200 6d 65 6e 74 5d 2e 0a 2a 2a 20 49 66 20 74 68 69  ment]..** If thi
24210 73 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61  s statement is a
24220 20 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65 6d   [SELECT] statem
24230 65 6e 74 20 61 6e 64 20 74 68 65 20 4e 74 68 20  ent and the Nth 
24240 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 0a 2a 2a  column of the.**
24250 20 72 65 74 75 72 6e 65 64 20 72 65 73 75 6c 74   returned result
24260 20 73 65 74 20 6f 66 20 74 68 61 74 20 5b 53 45   set of that [SE
24270 4c 45 43 54 5d 20 69 73 20 61 20 74 61 62 6c 65  LECT] is a table
24280 20 63 6f 6c 75 6d 6e 20 28 6e 6f 74 20 61 6e 0a   column (not an.
24290 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72  ** expression or
242a0 20 73 75 62 71 75 65 72 79 29 20 74 68 65 6e 20   subquery) then 
242b0 74 68 65 20 64 65 63 6c 61 72 65 64 20 74 79 70  the declared typ
242c0 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 0a 2a  e of the table.*
242d0 2a 20 63 6f 6c 75 6d 6e 20 69 73 20 72 65 74 75  * column is retu
242e0 72 6e 65 64 2e 20 20 49 66 20 74 68 65 20 4e 74  rned.  If the Nt
242f0 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  h column of the 
24300 72 65 73 75 6c 74 20 73 65 74 20 69 73 20 61 6e  result set is an
24310 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6f  .** expression o
24320 72 20 73 75 62 71 75 65 72 79 2c 20 74 68 65 6e  r subquery, then
24330 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20   a NULL pointer 
24340 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20  is returned..** 
24350 54 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72  The returned str
24360 69 6e 67 20 69 73 20 61 6c 77 61 79 73 20 55 54  ing is always UT
24370 46 2d 38 20 65 6e 63 6f 64 65 64 2e 20 7b 45 4e  F-8 encoded. {EN
24380 44 7d 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61  D}.**.** For exa
24390 6d 70 6c 65 2c 20 67 69 76 65 6e 20 74 68 65 20  mple, given the 
243a0 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 3a  database schema:
243b0 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54 45 20 54 41  .**.** CREATE TA
243c0 42 4c 45 20 74 31 28 63 31 20 56 41 52 49 41 4e  BLE t1(c1 VARIAN
243d0 54 29 3b 0a 2a 2a 0a 2a 2a 20 61 6e 64 20 74 68  T);.**.** and th
243e0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74  e following stat
243f0 65 6d 65 6e 74 20 74 6f 20 62 65 20 63 6f 6d 70  ement to be comp
24400 69 6c 65 64 3a 0a 2a 2a 0a 2a 2a 20 53 45 4c 45  iled:.**.** SELE
24410 43 54 20 63 31 20 2b 20 31 2c 20 63 31 20 46 52  CT c1 + 1, c1 FR
24420 4f 4d 20 74 31 3b 0a 2a 2a 0a 2a 2a 20 74 68 69  OM t1;.**.** thi
24430 73 20 72 6f 75 74 69 6e 65 20 77 6f 75 6c 64 20  s routine would 
24440 72 65 74 75 72 6e 20 74 68 65 20 73 74 72 69 6e  return the strin
24450 67 20 22 56 41 52 49 41 4e 54 22 20 66 6f 72 20  g "VARIANT" for 
24460 74 68 65 20 73 65 63 6f 6e 64 20 72 65 73 75 6c  the second resul
24470 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 28 69 3d 3d  t.** column (i==
24480 31 29 2c 20 61 6e 64 20 61 20 4e 55 4c 4c 20 70  1), and a NULL p
24490 6f 69 6e 74 65 72 20 66 6f 72 20 74 68 65 20 66  ointer for the f
244a0 69 72 73 74 20 72 65 73 75 6c 74 20 63 6f 6c 75  irst result colu
244b0 6d 6e 20 28 69 3d 3d 30 29 2e 0a 2a 2a 0a 2a 2a  mn (i==0)..**.**
244c0 20 53 51 4c 69 74 65 20 75 73 65 73 20 64 79 6e   SQLite uses dyn
244d0 61 6d 69 63 20 72 75 6e 2d 74 69 6d 65 20 74 79  amic run-time ty
244e0 70 69 6e 67 2e 20 20 53 6f 20 6a 75 73 74 20 62  ping.  So just b
244f0 65 63 61 75 73 65 20 61 20 63 6f 6c 75 6d 6e 0a  ecause a column.
24500 2a 2a 20 69 73 20 64 65 63 6c 61 72 65 64 20 74  ** is declared t
24510 6f 20 63 6f 6e 74 61 69 6e 20 61 20 70 61 72 74  o contain a part
24520 69 63 75 6c 61 72 20 74 79 70 65 20 64 6f 65 73  icular type does
24530 20 6e 6f 74 20 6d 65 61 6e 20 74 68 61 74 20 74   not mean that t
24540 68 65 0a 2a 2a 20 64 61 74 61 20 73 74 6f 72 65  he.** data store
24550 64 20 69 6e 20 74 68 61 74 20 63 6f 6c 75 6d 6e  d in that column
24560 20 69 73 20 6f 66 20 74 68 65 20 64 65 63 6c 61   is of the decla
24570 72 65 64 20 74 79 70 65 2e 20 20 53 51 4c 69 74  red type.  SQLit
24580 65 20 69 73 0a 2a 2a 20 73 74 72 6f 6e 67 6c 79  e is.** strongly
24590 20 74 79 70 65 64 2c 20 62 75 74 20 74 68 65 20   typed, but the 
245a0 74 79 70 69 6e 67 20 69 73 20 64 79 6e 61 6d 69  typing is dynami
245b0 63 20 6e 6f 74 20 73 74 61 74 69 63 2e 20 20 54  c not static.  T
245c0 79 70 65 0a 2a 2a 20 69 73 20 61 73 73 6f 63 69  ype.** is associ
245d0 61 74 65 64 20 77 69 74 68 20 69 6e 64 69 76 69  ated with indivi
245e0 64 75 61 6c 20 76 61 6c 75 65 73 2c 20 6e 6f 74  dual values, not
245f0 20 77 69 74 68 20 74 68 65 20 63 6f 6e 74 61 69   with the contai
24600 6e 65 72 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20  ners.** used to 
24610 68 6f 6c 64 20 74 68 6f 73 65 20 76 61 6c 75 65  hold those value
24620 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  s..**.** Require
24630 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 37 36  ments:.** [H1376
24640 31 5d 20 5b 48 31 33 37 36 32 5d 20 5b 48 31 33  1] [H13762] [H13
24650 37 36 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  763].*/.SQLITE_A
24660 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  PI const char *s
24670 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65  qlite3_column_de
24680 63 6c 74 79 70 65 28 73 71 6c 69 74 65 33 5f 73  cltype(sqlite3_s
24690 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  tmt*,int);.SQLIT
246a0 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64  E_API const void
246b0 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
246c0 5f 64 65 63 6c 74 79 70 65 31 36 28 73 71 6c 69  _decltype16(sqli
246d0 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a  te3_stmt*,int);.
246e0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
246f0 20 45 76 61 6c 75 61 74 65 20 41 6e 20 53 51 4c   Evaluate An SQL
24700 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 31 33 32   Statement {H132
24710 30 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 0a  00} <S10000>.**.
24720 2a 2a 20 41 66 74 65 72 20 61 20 5b 70 72 65 70  ** After a [prep
24730 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20  ared statement] 
24740 68 61 73 20 62 65 65 6e 20 70 72 65 70 61 72 65  has been prepare
24750 64 20 75 73 69 6e 67 20 65 69 74 68 65 72 0a 2a  d using either.*
24760 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  * [sqlite3_prepa
24770 72 65 5f 76 32 28 29 5d 20 6f 72 20 5b 73 71 6c  re_v2()] or [sql
24780 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76  ite3_prepare16_v
24790 32 28 29 5d 20 6f 72 20 6f 6e 65 20 6f 66 20 74  2()] or one of t
247a0 68 65 20 6c 65 67 61 63 79 0a 2a 2a 20 69 6e 74  he legacy.** int
247b0 65 72 66 61 63 65 73 20 5b 73 71 6c 69 74 65 33  erfaces [sqlite3
247c0 5f 70 72 65 70 61 72 65 28 29 5d 20 6f 72 20 5b  _prepare()] or [
247d0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31  sqlite3_prepare1
247e0 36 28 29 5d 2c 20 74 68 69 73 20 66 75 6e 63 74  6()], this funct
247f0 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 62 65 20 63  ion.** must be c
24800 61 6c 6c 65 64 20 6f 6e 65 20 6f 72 20 6d 6f 72  alled one or mor
24810 65 20 74 69 6d 65 73 20 74 6f 20 65 76 61 6c 75  e times to evalu
24820 61 74 65 20 74 68 65 20 73 74 61 74 65 6d 65 6e  ate the statemen
24830 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 74  t..**.** The det
24840 61 69 6c 73 20 6f 66 20 74 68 65 20 62 65 68 61  ails of the beha
24850 76 69 6f 72 20 6f 66 20 74 68 65 20 73 71 6c 69  vior of the sqli
24860 74 65 33 5f 73 74 65 70 28 29 20 69 6e 74 65 72  te3_step() inter
24870 66 61 63 65 20 64 65 70 65 6e 64 0a 2a 2a 20 6f  face depend.** o
24880 6e 20 77 68 65 74 68 65 72 20 74 68 65 20 73 74  n whether the st
24890 61 74 65 6d 65 6e 74 20 77 61 73 20 70 72 65 70  atement was prep
248a0 61 72 65 64 20 75 73 69 6e 67 20 74 68 65 20 6e  ared using the n
248b0 65 77 65 72 20 22 76 32 22 20 69 6e 74 65 72 66  ewer "v2" interf
248c0 61 63 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  ace.** [sqlite3_
248d0 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e  prepare_v2()] an
248e0 64 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  d [sqlite3_prepa
248f0 72 65 31 36 5f 76 32 28 29 5d 20 6f 72 20 74 68  re16_v2()] or th
24900 65 20 6f 6c 64 65 72 20 6c 65 67 61 63 79 0a 2a  e older legacy.*
24910 2a 20 69 6e 74 65 72 66 61 63 65 20 5b 73 71 6c  * interface [sql
24920 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20  ite3_prepare()] 
24930 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  and [sqlite3_pre
24940 70 61 72 65 31 36 28 29 5d 2e 20 20 54 68 65 20  pare16()].  The 
24950 75 73 65 20 6f 66 20 74 68 65 0a 2a 2a 20 6e 65  use of the.** ne
24960 77 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 65  w "v2" interface
24970 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20   is recommended 
24980 66 6f 72 20 6e 65 77 20 61 70 70 6c 69 63 61 74  for new applicat
24990 69 6f 6e 73 20 62 75 74 20 74 68 65 20 6c 65 67  ions but the leg
249a0 61 63 79 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  acy.** interface
249b0 20 77 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74   will continue t
249c0 6f 20 62 65 20 73 75 70 70 6f 72 74 65 64 2e 0a  o be supported..
249d0 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 6c 65 67  **.** In the leg
249e0 61 63 79 20 69 6e 74 65 72 66 61 63 65 2c 20 74  acy interface, t
249f0 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
24a00 77 69 6c 6c 20 62 65 20 65 69 74 68 65 72 20 5b  will be either [
24a10 53 51 4c 49 54 45 5f 42 55 53 59 5d 2c 0a 2a 2a  SQLITE_BUSY],.**
24a20 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c 20   [SQLITE_DONE], 
24a30 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 2c 20 5b 53  [SQLITE_ROW], [S
24a40 51 4c 49 54 45 5f 45 52 52 4f 52 5d 2c 20 6f 72  QLITE_ERROR], or
24a50 20 5b 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5d   [SQLITE_MISUSE]
24a60 2e 0a 2a 2a 20 57 69 74 68 20 74 68 65 20 22 76  ..** With the "v
24a70 32 22 20 69 6e 74 65 72 66 61 63 65 2c 20 61 6e  2" interface, an
24a80 79 20 6f 66 20 74 68 65 20 6f 74 68 65 72 20 5b  y of the other [
24a90 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 20 6f 72  result codes] or
24aa0 0a 2a 2a 20 5b 65 78 74 65 6e 64 65 64 20 72 65  .** [extended re
24ab0 73 75 6c 74 20 63 6f 64 65 73 5d 20 6d 69 67 68  sult codes] migh
24ac0 74 20 62 65 20 72 65 74 75 72 6e 65 64 20 61 73  t be returned as
24ad0 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51   well..**.** [SQ
24ae0 4c 49 54 45 5f 42 55 53 59 5d 20 6d 65 61 6e 73  LITE_BUSY] means
24af0 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61   that the databa
24b00 73 65 20 65 6e 67 69 6e 65 20 77 61 73 20 75 6e  se engine was un
24b10 61 62 6c 65 20 74 6f 20 61 63 71 75 69 72 65 20  able to acquire 
24b20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  the.** database 
24b30 6c 6f 63 6b 73 20 69 74 20 6e 65 65 64 73 20 74  locks it needs t
24b40 6f 20 64 6f 20 69 74 73 20 6a 6f 62 2e 20 20 49  o do its job.  I
24b50 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  f the statement 
24b60 69 73 20 61 20 5b 43 4f 4d 4d 49 54 5d 0a 2a 2a  is a [COMMIT].**
24b70 20 6f 72 20 6f 63 63 75 72 73 20 6f 75 74 73 69   or occurs outsi
24b80 64 65 20 6f 66 20 61 6e 20 65 78 70 6c 69 63 69  de of an explici
24b90 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74  t transaction, t
24ba0 68 65 6e 20 79 6f 75 20 63 61 6e 20 72 65 74 72  hen you can retr
24bb0 79 20 74 68 65 0a 2a 2a 20 73 74 61 74 65 6d 65  y the.** stateme
24bc0 6e 74 2e 20 20 49 66 20 74 68 65 20 73 74 61 74  nt.  If the stat
24bd0 65 6d 65 6e 74 20 69 73 20 6e 6f 74 20 61 20 5b  ement is not a [
24be0 43 4f 4d 4d 49 54 5d 20 61 6e 64 20 6f 63 63 75  COMMIT] and occu
24bf0 72 73 20 77 69 74 68 69 6e 20 61 0a 2a 2a 20 65  rs within a.** e
24c00 78 70 6c 69 63 69 74 20 74 72 61 6e 73 61 63 74  xplicit transact
24c10 69 6f 6e 20 74 68 65 6e 20 79 6f 75 20 73 68 6f  ion then you sho
24c20 75 6c 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65  uld rollback the
24c30 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 66   transaction bef
24c40 6f 72 65 0a 2a 2a 20 63 6f 6e 74 69 6e 75 69 6e  ore.** continuin
24c50 67 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45  g..**.** [SQLITE
24c60 5f 44 4f 4e 45 5d 20 6d 65 61 6e 73 20 74 68 61  _DONE] means tha
24c70 74 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  t the statement 
24c80 68 61 73 20 66 69 6e 69 73 68 65 64 20 65 78 65  has finished exe
24c90 63 75 74 69 6e 67 0a 2a 2a 20 73 75 63 63 65 73  cuting.** succes
24ca0 73 66 75 6c 6c 79 2e 20 20 73 71 6c 69 74 65 33  sfully.  sqlite3
24cb0 5f 73 74 65 70 28 29 20 73 68 6f 75 6c 64 20 6e  _step() should n
24cc0 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 61 67 61  ot be called aga
24cd0 69 6e 20 6f 6e 20 74 68 69 73 20 76 69 72 74 75  in on this virtu
24ce0 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 20 77 69  al.** machine wi
24cf0 74 68 6f 75 74 20 66 69 72 73 74 20 63 61 6c 6c  thout first call
24d00 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 72 65 73  ing [sqlite3_res
24d10 65 74 28 29 5d 20 74 6f 20 72 65 73 65 74 20 74  et()] to reset t
24d20 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a 20 6d 61  he virtual.** ma
24d30 63 68 69 6e 65 20 62 61 63 6b 20 74 6f 20 69 74  chine back to it
24d40 73 20 69 6e 69 74 69 61 6c 20 73 74 61 74 65 2e  s initial state.
24d50 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51  .**.** If the SQ
24d60 4c 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e  L statement bein
24d70 67 20 65 78 65 63 75 74 65 64 20 72 65 74 75 72  g executed retur
24d80 6e 73 20 61 6e 79 20 64 61 74 61 2c 20 74 68 65  ns any data, the
24d90 6e 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 0a 2a  n [SQLITE_ROW].*
24da0 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 65 61  * is returned ea
24db0 63 68 20 74 69 6d 65 20 61 20 6e 65 77 20 72 6f  ch time a new ro
24dc0 77 20 6f 66 20 64 61 74 61 20 69 73 20 72 65 61  w of data is rea
24dd0 64 79 20 66 6f 72 20 70 72 6f 63 65 73 73 69 6e  dy for processin
24de0 67 20 62 79 20 74 68 65 0a 2a 2a 20 63 61 6c 6c  g by the.** call
24df0 65 72 2e 20 54 68 65 20 76 61 6c 75 65 73 20 6d  er. The values m
24e00 61 79 20 62 65 20 61 63 63 65 73 73 65 64 20 75  ay be accessed u
24e10 73 69 6e 67 20 74 68 65 20 5b 63 6f 6c 75 6d 6e  sing the [column
24e20 20 61 63 63 65 73 73 20 66 75 6e 63 74 69 6f 6e   access function
24e30 73 5d 2e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73  s]..** sqlite3_s
24e40 74 65 70 28 29 20 69 73 20 63 61 6c 6c 65 64 20  tep() is called 
24e50 61 67 61 69 6e 20 74 6f 20 72 65 74 72 69 65 76  again to retriev
24e60 65 20 74 68 65 20 6e 65 78 74 20 72 6f 77 20 6f  e the next row o
24e70 66 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 5b 53  f data..**.** [S
24e80 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 6d 65 61  QLITE_ERROR] mea
24e90 6e 73 20 74 68 61 74 20 61 20 72 75 6e 2d 74 69  ns that a run-ti
24ea0 6d 65 20 65 72 72 6f 72 20 28 73 75 63 68 20 61  me error (such a
24eb0 73 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a  s a constraint.*
24ec0 2a 20 76 69 6f 6c 61 74 69 6f 6e 29 20 68 61 73  * violation) has
24ed0 20 6f 63 63 75 72 72 65 64 2e 20 20 73 71 6c 69   occurred.  sqli
24ee0 74 65 33 5f 73 74 65 70 28 29 20 73 68 6f 75 6c  te3_step() shoul
24ef0 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20  d not be called 
24f00 61 67 61 69 6e 20 6f 6e 0a 2a 2a 20 74 68 65 20  again on.** the 
24f10 56 4d 2e 20 4d 6f 72 65 20 69 6e 66 6f 72 6d 61  VM. More informa
24f20 74 69 6f 6e 20 6d 61 79 20 62 65 20 66 6f 75 6e  tion may be foun
24f30 64 20 62 79 20 63 61 6c 6c 69 6e 67 20 5b 73 71  d by calling [sq
24f40 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e  lite3_errmsg()].
24f50 0a 2a 2a 20 57 69 74 68 20 74 68 65 20 6c 65 67  .** With the leg
24f60 61 63 79 20 69 6e 74 65 72 66 61 63 65 2c 20 61  acy interface, a
24f70 20 6d 6f 72 65 20 73 70 65 63 69 66 69 63 20 65   more specific e
24f80 72 72 6f 72 20 63 6f 64 65 20 28 66 6f 72 20 65  rror code (for e
24f90 78 61 6d 70 6c 65 2c 0a 2a 2a 20 5b 53 51 4c 49  xample,.** [SQLI
24fa0 54 45 5f 49 4e 54 45 52 52 55 50 54 5d 2c 20 5b  TE_INTERRUPT], [
24fb0 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d 2c 20  SQLITE_SCHEMA], 
24fc0 5b 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5d  [SQLITE_CORRUPT]
24fd0 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 29 0a  , and so forth).
24fe0 2a 2a 20 63 61 6e 20 62 65 20 6f 62 74 61 69 6e  ** can be obtain
24ff0 65 64 20 62 79 20 63 61 6c 6c 69 6e 67 20 5b 73  ed by calling [s
25000 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20  qlite3_reset()] 
25010 6f 6e 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61  on the.** [prepa
25020 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20  red statement]. 
25030 20 49 6e 20 74 68 65 20 22 76 32 22 20 69 6e 74   In the "v2" int
25040 65 72 66 61 63 65 2c 0a 2a 2a 20 74 68 65 20 6d  erface,.** the m
25050 6f 72 65 20 73 70 65 63 69 66 69 63 20 65 72 72  ore specific err
25060 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
25070 6e 65 64 20 64 69 72 65 63 74 6c 79 20 62 79 20  ned directly by 
25080 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 0a  sqlite3_step()..
25090 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4d 49  **.** [SQLITE_MI
250a0 53 55 53 45 5d 20 6d 65 61 6e 73 20 74 68 61 74  SUSE] means that
250b0 20 74 68 65 20 74 68 69 73 20 72 6f 75 74 69 6e   the this routin
250c0 65 20 77 61 73 20 63 61 6c 6c 65 64 20 69 6e 61  e was called ina
250d0 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2a  ppropriately..**
250e0 20 50 65 72 68 61 70 73 20 69 74 20 77 61 73 20   Perhaps it was 
250f0 63 61 6c 6c 65 64 20 6f 6e 20 61 20 5b 70 72 65  called on a [pre
25100 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
25110 20 74 68 61 74 20 68 61 73 0a 2a 2a 20 61 6c 72   that has.** alr
25120 65 61 64 79 20 62 65 65 6e 20 5b 73 71 6c 69 74  eady been [sqlit
25130 65 33 5f 66 69 6e 61 6c 69 7a 65 20 7c 20 66 69  e3_finalize | fi
25140 6e 61 6c 69 7a 65 64 5d 20 6f 72 20 6f 6e 20 6f  nalized] or on o
25150 6e 65 20 74 68 61 74 20 68 61 64 0a 2a 2a 20 70  ne that had.** p
25160 72 65 76 69 6f 75 73 6c 79 20 72 65 74 75 72 6e  reviously return
25170 65 64 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52  ed [SQLITE_ERROR
25180 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 44 4f 4e  ] or [SQLITE_DON
25190 45 5d 2e 20 20 4f 72 20 69 74 20 63 6f 75 6c 64  E].  Or it could
251a0 0a 2a 2a 20 62 65 20 74 68 65 20 63 61 73 65 20  .** be the case 
251b0 74 68 61 74 20 74 68 65 20 73 61 6d 65 20 64 61  that the same da
251c0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
251d0 6e 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 20  n is being used 
251e0 62 79 20 74 77 6f 20 6f 72 0a 2a 2a 20 6d 6f 72  by two or.** mor
251f0 65 20 74 68 72 65 61 64 73 20 61 74 20 74 68 65  e threads at the
25200 20 73 61 6d 65 20 6d 6f 6d 65 6e 74 20 69 6e 20   same moment in 
25210 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 47  time..**.** <b>G
25220 6f 6f 66 79 20 49 6e 74 65 72 66 61 63 65 20 41  oofy Interface A
25230 6c 65 72 74 3a 3c 2f 62 3e 20 49 6e 20 74 68 65  lert:</b> In the
25240 20 6c 65 67 61 63 79 20 69 6e 74 65 72 66 61 63   legacy interfac
25250 65 2c 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73  e, the sqlite3_s
25260 74 65 70 28 29 0a 2a 2a 20 41 50 49 20 61 6c 77  tep().** API alw
25270 61 79 73 20 72 65 74 75 72 6e 73 20 61 20 67 65  ays returns a ge
25280 6e 65 72 69 63 20 65 72 72 6f 72 20 63 6f 64 65  neric error code
25290 2c 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d  , [SQLITE_ERROR]
252a0 2c 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 6e 79 0a  , following any.
252b0 2a 2a 20 65 72 72 6f 72 20 6f 74 68 65 72 20 74  ** error other t
252c0 68 61 6e 20 5b 53 51 4c 49 54 45 5f 42 55 53 59  han [SQLITE_BUSY
252d0 5d 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 4d 49  ] and [SQLITE_MI
252e0 53 55 53 45 5d 2e 20 20 59 6f 75 20 6d 75 73 74  SUSE].  You must
252f0 20 63 61 6c 6c 0a 2a 2a 20 5b 73 71 6c 69 74 65   call.** [sqlite
25300 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 20 5b 73  3_reset()] or [s
25310 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
25320 29 5d 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 66  )] in order to f
25330 69 6e 64 20 6f 6e 65 20 6f 66 20 74 68 65 0a 2a  ind one of the.*
25340 2a 20 73 70 65 63 69 66 69 63 20 5b 65 72 72 6f  * specific [erro
25350 72 20 63 6f 64 65 73 5d 20 74 68 61 74 20 62 65  r codes] that be
25360 74 74 65 72 20 64 65 73 63 72 69 62 65 73 20 74  tter describes t
25370 68 65 20 65 72 72 6f 72 2e 0a 2a 2a 20 57 65 20  he error..** We 
25380 61 64 6d 69 74 20 74 68 61 74 20 74 68 69 73 20  admit that this 
25390 69 73 20 61 20 67 6f 6f 66 79 20 64 65 73 69 67  is a goofy desig
253a0 6e 2e 20 20 54 68 65 20 70 72 6f 62 6c 65 6d 20  n.  The problem 
253b0 68 61 73 20 62 65 65 6e 20 66 69 78 65 64 0a 2a  has been fixed.*
253c0 2a 20 77 69 74 68 20 74 68 65 20 22 76 32 22 20  * with the "v2" 
253d0 69 6e 74 65 72 66 61 63 65 2e 20 20 49 66 20 79  interface.  If y
253e0 6f 75 20 70 72 65 70 61 72 65 20 61 6c 6c 20 6f  ou prepare all o
253f0 66 20 79 6f 75 72 20 53 51 4c 20 73 74 61 74 65  f your SQL state
25400 6d 65 6e 74 73 0a 2a 2a 20 75 73 69 6e 67 20 65  ments.** using e
25410 69 74 68 65 72 20 5b 73 71 6c 69 74 65 33 5f 70  ither [sqlite3_p
25420 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20  repare_v2()] or 
25430 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
25440 31 36 5f 76 32 28 29 5d 20 69 6e 73 74 65 61 64  16_v2()] instead
25450 0a 2a 2a 20 6f 66 20 74 68 65 20 6c 65 67 61 63  .** of the legac
25460 79 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  y [sqlite3_prepa
25470 72 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74  re()] and [sqlit
25480 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d 20  e3_prepare16()] 
25490 69 6e 74 65 72 66 61 63 65 73 2c 0a 2a 2a 20 74  interfaces,.** t
254a0 68 65 6e 20 74 68 65 20 6d 6f 72 65 20 73 70 65  hen the more spe
254b0 63 69 66 69 63 20 5b 65 72 72 6f 72 20 63 6f 64  cific [error cod
254c0 65 73 5d 20 61 72 65 20 72 65 74 75 72 6e 65 64  es] are returned
254d0 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20   directly.** by 
254e0 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20  sqlite3_step(). 
254f0 20 54 68 65 20 75 73 65 20 6f 66 20 74 68 65 20   The use of the 
25500 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 20 69  "v2" interface i
25510 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64 2e 0a 2a  s recommended..*
25520 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
25530 73 3a 0a 2a 2a 20 5b 48 31 33 32 30 32 5d 20 5b  s:.** [H13202] [
25540 48 31 35 33 30 34 5d 20 5b 48 31 35 33 30 36 5d  H15304] [H15306]
25550 20 5b 48 31 35 33 30 38 5d 20 5b 48 31 35 33 31   [H15308] [H1531
25560 30 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  0].*/.SQLITE_API
25570 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 65   int sqlite3_ste
25580 70 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29  p(sqlite3_stmt*)
25590 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
255a0 46 3a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  F: Number of col
255b0 75 6d 6e 73 20 69 6e 20 61 20 72 65 73 75 6c 74  umns in a result
255c0 20 73 65 74 20 7b 48 31 33 37 37 30 7d 20 3c 53   set {H13770} <S
255d0 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 52 65 74  10700>.**.** Ret
255e0 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20  urns the number 
255f0 6f 66 20 76 61 6c 75 65 73 20 69 6e 20 74 68 65  of values in the
25600 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20   current row of 
25610 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a  the result set..
25620 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
25630 74 73 3a 0a 2a 2a 20 5b 48 31 33 37 37 31 5d 20  ts:.** [H13771] 
25640 5b 48 31 33 37 37 32 5d 0a 2a 2f 0a 53 51 4c 49  [H13772].*/.SQLI
25650 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
25660 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74 28 73 71  e3_data_count(sq
25670 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
25680 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  t);../*.** CAPI3
25690 52 45 46 3a 20 46 75 6e 64 61 6d 65 6e 74 61 6c  REF: Fundamental
256a0 20 44 61 74 61 74 79 70 65 73 20 7b 48 31 30 32   Datatypes {H102
256b0 36 35 7d 20 3c 53 31 30 31 31 30 3e 3c 53 31 30  65} <S10110><S10
256c0 31 32 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53  120>.** KEYWORDS
256d0 3a 20 53 51 4c 49 54 45 5f 54 45 58 54 0a 2a 2a  : SQLITE_TEXT.**
256e0 0a 2a 2a 20 7b 48 31 30 32 36 36 7d 20 45 76 65  .** {H10266} Eve
256f0 72 79 20 76 61 6c 75 65 20 69 6e 20 53 51 4c 69  ry value in SQLi
25700 74 65 20 68 61 73 20 6f 6e 65 20 6f 66 20 66 69  te has one of fi
25710 76 65 20 66 75 6e 64 61 6d 65 6e 74 61 6c 20 64  ve fundamental d
25720 61 74 61 74 79 70 65 73 3a 0a 2a 2a 0a 2a 2a 20  atatypes:.**.** 
25730 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 36 34 2d  <ul>.** <li> 64-
25740 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67  bit signed integ
25750 65 72 0a 2a 2a 20 3c 6c 69 3e 20 36 34 2d 62 69  er.** <li> 64-bi
25760 74 20 49 45 45 45 20 66 6c 6f 61 74 69 6e 67 20  t IEEE floating 
25770 70 6f 69 6e 74 20 6e 75 6d 62 65 72 0a 2a 2a 20  point number.** 
25780 3c 6c 69 3e 20 73 74 72 69 6e 67 0a 2a 2a 20 3c  <li> string.** <
25790 6c 69 3e 20 42 4c 4f 42 0a 2a 2a 20 3c 6c 69 3e  li> BLOB.** <li>
257a0 20 4e 55 4c 4c 0a 2a 2a 20 3c 2f 75 6c 3e 20 7b   NULL.** </ul> {
257b0 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  END}.**.** These
257c0 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 63   constants are c
257d0 6f 64 65 73 20 66 6f 72 20 65 61 63 68 20 6f 66  odes for each of
257e0 20 74 68 6f 73 65 20 74 79 70 65 73 2e 0a 2a 2a   those types..**
257f0 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68  .** Note that th
25800 65 20 53 51 4c 49 54 45 5f 54 45 58 54 20 63 6f  e SQLITE_TEXT co
25810 6e 73 74 61 6e 74 20 77 61 73 20 61 6c 73 6f 20  nstant was also 
25820 75 73 65 64 20 69 6e 20 53 51 4c 69 74 65 20 76  used in SQLite v
25830 65 72 73 69 6f 6e 20 32 0a 2a 2a 20 66 6f 72 20  ersion 2.** for 
25840 61 20 63 6f 6d 70 6c 65 74 65 6c 79 20 64 69 66  a completely dif
25850 66 65 72 65 6e 74 20 6d 65 61 6e 69 6e 67 2e 20  ferent meaning. 
25860 20 53 6f 66 74 77 61 72 65 20 74 68 61 74 20 6c   Software that l
25870 69 6e 6b 73 20 61 67 61 69 6e 73 74 20 62 6f 74  inks against bot
25880 68 0a 2a 2a 20 53 51 4c 69 74 65 20 76 65 72 73  h.** SQLite vers
25890 69 6f 6e 20 32 20 61 6e 64 20 53 51 4c 69 74 65  ion 2 and SQLite
258a0 20 76 65 72 73 69 6f 6e 20 33 20 73 68 6f 75 6c   version 3 shoul
258b0 64 20 75 73 65 20 53 51 4c 49 54 45 33 5f 54 45  d use SQLITE3_TE
258c0 58 54 2c 20 6e 6f 74 0a 2a 2a 20 53 51 4c 49 54  XT, not.** SQLIT
258d0 45 5f 54 45 58 54 2e 0a 2a 2f 0a 23 64 65 66 69  E_TEXT..*/.#defi
258e0 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45  ne SQLITE_INTEGE
258f0 52 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c  R  1.#define SQL
25900 49 54 45 5f 46 4c 4f 41 54 20 20 20 20 32 0a 23  ITE_FLOAT    2.#
25910 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 42 4c  define SQLITE_BL
25920 4f 42 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65  OB     4.#define
25930 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 20 20 20   SQLITE_NULL    
25940 20 35 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45   5.#ifdef SQLITE
25950 5f 54 45 58 54 0a 23 20 75 6e 64 65 66 20 53 51  _TEXT.# undef SQ
25960 4c 49 54 45 5f 54 45 58 54 0a 23 65 6c 73 65 0a  LITE_TEXT.#else.
25970 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
25980 54 45 58 54 20 20 20 20 20 33 0a 23 65 6e 64 69  TEXT     3.#endi
25990 66 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  f.#define SQLITE
259a0 33 5f 54 45 58 54 20 20 20 20 20 33 0a 0a 2f 2a  3_TEXT     3../*
259b0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65  .** CAPI3REF: Re
259c0 73 75 6c 74 20 56 61 6c 75 65 73 20 46 72 6f 6d  sult Values From
259d0 20 41 20 51 75 65 72 79 20 7b 48 31 33 38 30 30   A Query {H13800
259e0 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 20 4b 45  } <S10700>.** KE
259f0 59 57 4f 52 44 53 3a 20 7b 63 6f 6c 75 6d 6e 20  YWORDS: {column 
25a00 61 63 63 65 73 73 20 66 75 6e 63 74 69 6f 6e 73  access functions
25a10 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f  }.**.** These ro
25a20 75 74 69 6e 65 73 20 66 6f 72 6d 20 74 68 65 20  utines form the 
25a30 22 72 65 73 75 6c 74 20 73 65 74 20 71 75 65 72  "result set quer
25a40 79 22 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a  y" interface..**
25a50 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e  .** These routin
25a60 65 73 20 72 65 74 75 72 6e 20 69 6e 66 6f 72 6d  es return inform
25a70 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 20 73 69  ation about a si
25a80 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 6f 66 20 74  ngle column of t
25a90 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 72 65  he current.** re
25aa0 73 75 6c 74 20 72 6f 77 20 6f 66 20 61 20 71 75  sult row of a qu
25ab0 65 72 79 2e 20 20 49 6e 20 65 76 65 72 79 20 63  ery.  In every c
25ac0 61 73 65 20 74 68 65 20 66 69 72 73 74 20 61 72  ase the first ar
25ad0 67 75 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e  gument is a poin
25ae0 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 5b 70  ter.** to the [p
25af0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
25b00 74 5d 20 74 68 61 74 20 69 73 20 62 65 69 6e 67  t] that is being
25b10 20 65 76 61 6c 75 61 74 65 64 20 28 74 68 65 20   evaluated (the 
25b20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 5d 0a  [sqlite3_stmt*].
25b30 2a 2a 20 74 68 61 74 20 77 61 73 20 72 65 74 75  ** that was retu
25b40 72 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74  rned from [sqlit
25b50 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
25b60 20 6f 72 20 6f 6e 65 20 6f 66 20 69 74 73 20 76   or one of its v
25b70 61 72 69 61 6e 74 73 29 0a 2a 2a 20 61 6e 64 20  ariants).** and 
25b80 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
25b90 65 6e 74 20 69 73 20 74 68 65 20 69 6e 64 65 78  ent is the index
25ba0 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 66   of the column f
25bb0 6f 72 20 77 68 69 63 68 20 69 6e 66 6f 72 6d 61  or which informa
25bc0 74 69 6f 6e 0a 2a 2a 20 73 68 6f 75 6c 64 20 62  tion.** should b
25bd0 65 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 65  e returned.  The
25be0 20 6c 65 66 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e   leftmost column
25bf0 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73   of the result s
25c00 65 74 20 68 61 73 20 74 68 65 20 69 6e 64 65 78  et has the index
25c10 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65   0..**.** If the
25c20 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 64   SQL statement d
25c30 6f 65 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c  oes not currentl
25c40 79 20 70 6f 69 6e 74 20 74 6f 20 61 20 76 61 6c  y point to a val
25c50 69 64 20 72 6f 77 2c 20 6f 72 20 69 66 20 74 68  id row, or if th
25c60 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e 64 65  e.** column inde
25c70 78 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67  x is out of rang
25c80 65 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  e, the result is
25c90 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 54   undefined..** T
25ca0 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d 61  hese routines ma
25cb0 79 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64  y only be called
25cc0 20 77 68 65 6e 20 74 68 65 20 6d 6f 73 74 20 72   when the most r
25cd0 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a  ecent call to.**
25ce0 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   [sqlite3_step()
25cf0 5d 20 68 61 73 20 72 65 74 75 72 6e 65 64 20 5b  ] has returned [
25d00 53 51 4c 49 54 45 5f 52 4f 57 5d 20 61 6e 64 20  SQLITE_ROW] and 
25d10 6e 65 69 74 68 65 72 0a 2a 2a 20 5b 73 71 6c 69  neither.** [sqli
25d20 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6e 6f 72  te3_reset()] nor
25d30 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69   [sqlite3_finali
25d40 7a 65 28 29 5d 20 68 61 76 65 20 62 65 65 6e 20  ze()] have been 
25d50 63 61 6c 6c 65 64 20 73 75 62 73 65 71 75 65 6e  called subsequen
25d60 74 6c 79 2e 0a 2a 2a 20 49 66 20 61 6e 79 20 6f  tly..** If any o
25d70 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  f these routines
25d80 20 61 72 65 20 63 61 6c 6c 65 64 20 61 66 74 65   are called afte
25d90 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74  r [sqlite3_reset
25da0 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74  ()] or.** [sqlit
25db0 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f  e3_finalize()] o
25dc0 72 20 61 66 74 65 72 20 5b 73 71 6c 69 74 65 33  r after [sqlite3
25dd0 5f 73 74 65 70 28 29 5d 20 68 61 73 20 72 65 74  _step()] has ret
25de0 75 72 6e 65 64 0a 2a 2a 20 73 6f 6d 65 74 68 69  urned.** somethi
25df0 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 5b 53  ng other than [S
25e00 51 4c 49 54 45 5f 52 4f 57 5d 2c 20 74 68 65 20  QLITE_ROW], the 
25e10 72 65 73 75 6c 74 73 20 61 72 65 20 75 6e 64 65  results are unde
25e20 66 69 6e 65 64 2e 0a 2a 2a 20 49 66 20 5b 73 71  fined..** If [sq
25e30 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 6f 72  lite3_step()] or
25e40 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28   [sqlite3_reset(
25e50 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66  )] or [sqlite3_f
25e60 69 6e 61 6c 69 7a 65 28 29 5d 0a 2a 2a 20 61 72  inalize()].** ar
25e70 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 61 20  e called from a 
25e80 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61 64  different thread
25e90 20 77 68 69 6c 65 20 61 6e 79 20 6f 66 20 74 68   while any of th
25ea0 65 73 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20  ese routines.** 
25eb0 61 72 65 20 70 65 6e 64 69 6e 67 2c 20 74 68 65  are pending, the
25ec0 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20 61 72  n the results ar
25ed0 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a  e undefined..**.
25ee0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63  ** The sqlite3_c
25ef0 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 20 72 6f 75  olumn_type() rou
25f00 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65  tine returns the
25f10 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45  .** [SQLITE_INTE
25f20 47 45 52 20 7c 20 64 61 74 61 74 79 70 65 20 63  GER | datatype c
25f30 6f 64 65 5d 20 66 6f 72 20 74 68 65 20 69 6e 69  ode] for the ini
25f40 74 69 61 6c 20 64 61 74 61 20 74 79 70 65 0a 2a  tial data type.*
25f50 2a 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20  * of the result 
25f60 63 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 72 65 74  column.  The ret
25f70 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20 6f  urned value is o
25f80 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 49 4e  ne of [SQLITE_IN
25f90 54 45 47 45 52 5d 2c 0a 2a 2a 20 5b 53 51 4c 49  TEGER],.** [SQLI
25fa0 54 45 5f 46 4c 4f 41 54 5d 2c 20 5b 53 51 4c 49  TE_FLOAT], [SQLI
25fb0 54 45 5f 54 45 58 54 5d 2c 20 5b 53 51 4c 49 54  TE_TEXT], [SQLIT
25fc0 45 5f 42 4c 4f 42 5d 2c 20 6f 72 20 5b 53 51 4c  E_BLOB], or [SQL
25fd0 49 54 45 5f 4e 55 4c 4c 5d 2e 20 20 54 68 65 20  ITE_NULL].  The 
25fe0 76 61 6c 75 65 0a 2a 2a 20 72 65 74 75 72 6e 65  value.** returne
25ff0 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c  d by sqlite3_col
26000 75 6d 6e 5f 74 79 70 65 28 29 20 69 73 20 6f 6e  umn_type() is on
26010 6c 79 20 6d 65 61 6e 69 6e 67 66 75 6c 20 69 66  ly meaningful if
26020 20 6e 6f 20 74 79 70 65 0a 2a 2a 20 63 6f 6e 76   no type.** conv
26030 65 72 73 69 6f 6e 73 20 68 61 76 65 20 6f 63 63  ersions have occ
26040 75 72 72 65 64 20 61 73 20 64 65 73 63 72 69 62  urred as describ
26050 65 64 20 62 65 6c 6f 77 2e 20 20 41 66 74 65 72  ed below.  After
26060 20 61 20 74 79 70 65 20 63 6f 6e 76 65 72 73 69   a type conversi
26070 6f 6e 2c 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65  on,.** the value
26080 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c   returned by sql
26090 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65  ite3_column_type
260a0 28 29 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e  () is undefined.
260b0 20 20 46 75 74 75 72 65 0a 2a 2a 20 76 65 72 73    Future.** vers
260c0 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 6d  ions of SQLite m
260d0 61 79 20 63 68 61 6e 67 65 20 74 68 65 20 62 65  ay change the be
260e0 68 61 76 69 6f 72 20 6f 66 20 73 71 6c 69 74 65  havior of sqlite
260f0 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 0a  3_column_type().
26100 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 74  ** following a t
26110 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a  ype conversion..
26120 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73  **.** If the res
26130 75 6c 74 20 69 73 20 61 20 42 4c 4f 42 20 6f 72  ult is a BLOB or
26140 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 74 68   UTF-8 string th
26150 65 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63  en the sqlite3_c
26160 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 0a 2a 2a  olumn_bytes().**
26170 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
26180 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
26190 79 74 65 73 20 69 6e 20 74 68 61 74 20 42 4c 4f  ytes in that BLO
261a0 42 20 6f 72 20 73 74 72 69 6e 67 2e 0a 2a 2a 20  B or string..** 
261b0 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  If the result is
261c0 20 61 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67   a UTF-16 string
261d0 2c 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 63  , then sqlite3_c
261e0 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 20 63 6f  olumn_bytes() co
261f0 6e 76 65 72 74 73 0a 2a 2a 20 74 68 65 20 73 74  nverts.** the st
26200 72 69 6e 67 20 74 6f 20 55 54 46 2d 38 20 61 6e  ring to UTF-8 an
26210 64 20 74 68 65 6e 20 72 65 74 75 72 6e 73 20 74  d then returns t
26220 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
26230 65 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 72 65  es..** If the re
26240 73 75 6c 74 20 69 73 20 61 20 6e 75 6d 65 72 69  sult is a numeri
26250 63 20 76 61 6c 75 65 20 74 68 65 6e 20 73 71 6c  c value then sql
26260 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
26270 73 28 29 20 75 73 65 73 0a 2a 2a 20 5b 73 71 6c  s() uses.** [sql
26280 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 5d  ite3_snprintf()]
26290 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 61 74   to convert that
262a0 20 76 61 6c 75 65 20 74 6f 20 61 20 55 54 46 2d   value to a UTF-
262b0 38 20 73 74 72 69 6e 67 20 61 6e 64 20 72 65 74  8 string and ret
262c0 75 72 6e 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62  urns.** the numb
262d0 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74  er of bytes in t
262e0 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 54  hat string..** T
262f0 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
26300 64 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75  d does not inclu
26310 64 65 20 74 68 65 20 7a 65 72 6f 20 74 65 72 6d  de the zero term
26320 69 6e 61 74 6f 72 20 61 74 20 74 68 65 20 65 6e  inator at the en
26330 64 0a 2a 2a 20 6f 66 20 74 68 65 20 73 74 72 69  d.** of the stri
26340 6e 67 2e 20 20 46 6f 72 20 63 6c 61 72 69 74 79  ng.  For clarity
26350 3a 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75  : the value retu
26360 72 6e 65 64 20 69 73 20 74 68 65 20 6e 75 6d 62  rned is the numb
26370 65 72 20 6f 66 0a 2a 2a 20 62 79 74 65 73 20 69  er of.** bytes i
26380 6e 20 74 68 65 20 73 74 72 69 6e 67 2c 20 6e 6f  n the string, no
26390 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  t the number of 
263a0 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a  characters..**.*
263b0 2a 20 53 74 72 69 6e 67 73 20 72 65 74 75 72 6e  * Strings return
263c0 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f  ed by sqlite3_co
263d0 6c 75 6d 6e 5f 74 65 78 74 28 29 20 61 6e 64 20  lumn_text() and 
263e0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
263f0 65 78 74 31 36 28 29 2c 0a 2a 2a 20 65 76 65 6e  ext16(),.** even
26400 20 65 6d 70 74 79 20 73 74 72 69 6e 67 73 2c 20   empty strings, 
26410 61 72 65 20 61 6c 77 61 79 73 20 7a 65 72 6f 20  are always zero 
26420 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 54 68 65  terminated.  The
26430 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65   return.** value
26440 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 63 6f   from sqlite3_co
26450 6c 75 6d 6e 5f 62 6c 6f 62 28 29 20 66 6f 72 20  lumn_blob() for 
26460 61 20 7a 65 72 6f 2d 6c 65 6e 67 74 68 20 42 4c  a zero-length BL
26470 4f 42 20 69 73 20 61 6e 20 61 72 62 69 74 72 61  OB is an arbitra
26480 72 79 0a 2a 2a 20 70 6f 69 6e 74 65 72 2c 20 70  ry.** pointer, p
26490 6f 73 73 69 62 6c 79 20 65 76 65 6e 20 61 20 4e  ossibly even a N
264a0 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a  ULL pointer..**.
264b0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63  ** The sqlite3_c
264c0 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 20  olumn_bytes16() 
264d0 72 6f 75 74 69 6e 65 20 69 73 20 73 69 6d 69 6c  routine is simil
264e0 61 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f  ar to sqlite3_co
264f0 6c 75 6d 6e 5f 62 79 74 65 73 28 29 0a 2a 2a 20  lumn_bytes().** 
26500 62 75 74 20 6c 65 61 76 65 73 20 74 68 65 20 72  but leaves the r
26510 65 73 75 6c 74 20 69 6e 20 55 54 46 2d 31 36 20  esult in UTF-16 
26520 69 6e 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f  in native byte o
26530 72 64 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20  rder instead of 
26540 55 54 46 2d 38 2e 0a 2a 2a 20 54 68 65 20 7a 65  UTF-8..** The ze
26550 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 69 73  ro terminator is
26560 20 6e 6f 74 20 69 6e 63 6c 75 64 65 64 20 69 6e   not included in
26570 20 74 68 69 73 20 63 6f 75 6e 74 2e 0a 2a 2a 0a   this count..**.
26580 2a 2a 20 54 68 65 20 6f 62 6a 65 63 74 20 72 65  ** The object re
26590 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74  turned by [sqlit
265a0 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28  e3_column_value(
265b0 29 5d 20 69 73 20 61 6e 0a 2a 2a 20 5b 75 6e 70  )] is an.** [unp
265c0 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33  rotected sqlite3
265d0 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 2e 20  _value] object. 
265e0 20 41 6e 20 75 6e 70 72 6f 74 65 63 74 65 64 20   An unprotected 
265f0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62  sqlite3_value ob
26600 6a 65 63 74 0a 2a 2a 20 6d 61 79 20 6f 6e 6c 79  ject.** may only
26610 20 62 65 20 75 73 65 64 20 77 69 74 68 20 5b 73   be used with [s
26620 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75  qlite3_bind_valu
26630 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  e()] and [sqlite
26640 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29  3_result_value()
26650 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 5b 75 6e  ]..** If the [un
26660 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65  protected sqlite
26670 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20  3_value] object 
26680 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 5b  returned by.** [
26690 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76  sqlite3_column_v
266a0 61 6c 75 65 28 29 5d 20 69 73 20 75 73 65 64 20  alue()] is used 
266b0 69 6e 20 61 6e 79 20 6f 74 68 65 72 20 77 61 79  in any other way
266c0 2c 20 69 6e 63 6c 75 64 69 6e 67 20 63 61 6c 6c  , including call
266d0 73 0a 2a 2a 20 74 6f 20 72 6f 75 74 69 6e 65 73  s.** to routines
266e0 20 6c 69 6b 65 20 5b 73 71 6c 69 74 65 33 5f 76   like [sqlite3_v
266f0 61 6c 75 65 5f 69 6e 74 28 29 5d 2c 20 5b 73 71  alue_int()], [sq
26700 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
26710 28 29 5d 2c 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69  ()],.** or [sqli
26720 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
26730 29 5d 2c 20 74 68 65 6e 20 74 68 65 20 62 65 68  )], then the beh
26740 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e  avior is undefin
26750 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  ed..**.** These 
26760 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74  routines attempt
26770 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 65 20   to convert the 
26780 76 61 6c 75 65 20 77 68 65 72 65 20 61 70 70 72  value where appr
26790 6f 70 72 69 61 74 65 2e 20 20 46 6f 72 0a 2a 2a  opriate.  For.**
267a0 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65   example, if the
267b0 20 69 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73   internal repres
267c0 65 6e 74 61 74 69 6f 6e 20 69 73 20 46 4c 4f 41  entation is FLOA
267d0 54 20 61 6e 64 20 61 20 74 65 78 74 20 72 65 73  T and a text res
267e0 75 6c 74 0a 2a 2a 20 69 73 20 72 65 71 75 65 73  ult.** is reques
267f0 74 65 64 2c 20 5b 73 71 6c 69 74 65 33 5f 73 6e  ted, [sqlite3_sn
26800 70 72 69 6e 74 66 28 29 5d 20 69 73 20 75 73 65  printf()] is use
26810 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 74 6f 20  d internally to 
26820 70 65 72 66 6f 72 6d 20 74 68 65 0a 2a 2a 20 63  perform the.** c
26830 6f 6e 76 65 72 73 69 6f 6e 20 61 75 74 6f 6d 61  onversion automa
26840 74 69 63 61 6c 6c 79 2e 20 20 54 68 65 20 66 6f  tically.  The fo
26850 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 64 65  llowing table de
26860 74 61 69 6c 73 20 74 68 65 20 63 6f 6e 76 65 72  tails the conver
26870 73 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 61 72  sions.** that ar
26880 65 20 61 70 70 6c 69 65 64 3a 0a 2a 2a 0a 2a 2a  e applied:.**.**
26890 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a   <blockquote>.**
268a0 20 3c 74 61 62 6c 65 20 62 6f 72 64 65 72 3d 22   <table border="
268b0 31 22 3e 0a 2a 2a 20 3c 74 72 3e 3c 74 68 3e 20  1">.** <tr><th> 
268c0 49 6e 74 65 72 6e 61 6c 3c 62 72 3e 54 79 70 65  Internal<br>Type
268d0 20 3c 74 68 3e 20 52 65 71 75 65 73 74 65 64 3c   <th> Requested<
268e0 62 72 3e 54 79 70 65 20 3c 74 68 3e 20 20 43 6f  br>Type <th>  Co
268f0 6e 76 65 72 73 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c  nversion.**.** <
26900 74 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 20  tr><td>  NULL   
26910 20 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 20   <td> INTEGER   
26920 3c 74 64 3e 20 52 65 73 75 6c 74 20 69 73 20 30  <td> Result is 0
26930 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 55  .** <tr><td>  NU
26940 4c 4c 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f 41  LL    <td>  FLOA
26950 54 20 20 20 20 3c 74 64 3e 20 52 65 73 75 6c 74  T    <td> Result
26960 20 69 73 20 30 2e 30 0a 2a 2a 20 3c 74 72 3e 3c   is 0.0.** <tr><
26970 74 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64  td>  NULL    <td
26980 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e  >   TEXT    <td>
26990 20 52 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 20   Result is NULL 
269a0 70 6f 69 6e 74 65 72 0a 2a 2a 20 3c 74 72 3e 3c  pointer.** <tr><
269b0 74 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64  td>  NULL    <td
269c0 3e 20 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e  >   BLOB    <td>
269d0 20 52 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 20   Result is NULL 
269e0 70 6f 69 6e 74 65 72 0a 2a 2a 20 3c 74 72 3e 3c  pointer.** <tr><
269f0 74 64 3e 20 49 4e 54 45 47 45 52 20 20 3c 74 64  td> INTEGER  <td
26a00 3e 20 20 46 4c 4f 41 54 20 20 20 20 3c 74 64 3e  >  FLOAT    <td>
26a10 20 43 6f 6e 76 65 72 74 20 66 72 6f 6d 20 69 6e   Convert from in
26a20 74 65 67 65 72 20 74 6f 20 66 6c 6f 61 74 0a 2a  teger to float.*
26a30 2a 20 3c 74 72 3e 3c 74 64 3e 20 49 4e 54 45 47  * <tr><td> INTEG
26a40 45 52 20 20 3c 74 64 3e 20 20 20 54 45 58 54 20  ER  <td>   TEXT 
26a50 20 20 20 3c 74 64 3e 20 41 53 43 49 49 20 72 65     <td> ASCII re
26a60 6e 64 65 72 69 6e 67 20 6f 66 20 74 68 65 20 69  ndering of the i
26a70 6e 74 65 67 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74  nteger.** <tr><t
26a80 64 3e 20 49 4e 54 45 47 45 52 20 20 3c 74 64 3e  d> INTEGER  <td>
26a90 20 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20     BLOB    <td> 
26aa0 53 61 6d 65 20 61 73 20 49 4e 54 45 47 45 52 2d  Same as INTEGER-
26ab0 3e 54 45 58 54 0a 2a 2a 20 3c 74 72 3e 3c 74 64  >TEXT.** <tr><td
26ac0 3e 20 20 46 4c 4f 41 54 20 20 20 3c 74 64 3e 20  >  FLOAT   <td> 
26ad0 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e 20 43  INTEGER   <td> C
26ae0 6f 6e 76 65 72 74 20 66 72 6f 6d 20 66 6c 6f 61  onvert from floa
26af0 74 20 74 6f 20 69 6e 74 65 67 65 72 0a 2a 2a 20  t to integer.** 
26b00 3c 74 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54 20  <tr><td>  FLOAT 
26b10 20 20 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20    <td>   TEXT   
26b20 20 3c 74 64 3e 20 41 53 43 49 49 20 72 65 6e 64   <td> ASCII rend
26b30 65 72 69 6e 67 20 6f 66 20 74 68 65 20 66 6c 6f  ering of the flo
26b40 61 74 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20  at.** <tr><td>  
26b50 46 4c 4f 41 54 20 20 20 3c 74 64 3e 20 20 20 42  FLOAT   <td>   B
26b60 4c 4f 42 20 20 20 20 3c 74 64 3e 20 53 61 6d 65  LOB    <td> Same
26b70 20 61 73 20 46 4c 4f 41 54 2d 3e 54 45 58 54 0a   as FLOAT->TEXT.
26b80 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 54 45 58  ** <tr><td>  TEX
26b90 54 20 20 20 20 3c 74 64 3e 20 49 4e 54 45 47 45  T    <td> INTEGE
26ba0 52 20 20 20 3c 74 64 3e 20 55 73 65 20 61 74 6f  R   <td> Use ato
26bb0 69 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20  i().** <tr><td> 
26bc0 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20 20 46   TEXT    <td>  F
26bd0 4c 4f 41 54 20 20 20 20 3c 74 64 3e 20 55 73 65  LOAT    <td> Use
26be0 20 61 74 6f 66 28 29 0a 2a 2a 20 3c 74 72 3e 3c   atof().** <tr><
26bf0 74 64 3e 20 20 54 45 58 54 20 20 20 20 3c 74 64  td>  TEXT    <td
26c00 3e 20 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e  >   BLOB    <td>
26c10 20 4e 6f 20 63 68 61 6e 67 65 0a 2a 2a 20 3c 74   No change.** <t
26c20 72 3e 3c 74 64 3e 20 20 42 4c 4f 42 20 20 20 20  r><td>  BLOB    
26c30 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 20 3c  <td> INTEGER   <
26c40 74 64 3e 20 43 6f 6e 76 65 72 74 20 74 6f 20 54  td> Convert to T
26c50 45 58 54 20 74 68 65 6e 20 75 73 65 20 61 74 6f  EXT then use ato
26c60 69 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20  i().** <tr><td> 
26c70 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 20 46   BLOB    <td>  F
26c80 4c 4f 41 54 20 20 20 20 3c 74 64 3e 20 43 6f 6e  LOAT    <td> Con
26c90 76 65 72 74 20 74 6f 20 54 45 58 54 20 74 68 65  vert to TEXT the
26ca0 6e 20 75 73 65 20 61 74 6f 66 28 29 0a 2a 2a 20  n use atof().** 
26cb0 3c 74 72 3e 3c 74 64 3e 20 20 42 4c 4f 42 20 20  <tr><td>  BLOB  
26cc0 20 20 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20    <td>   TEXT   
26cd0 20 3c 74 64 3e 20 41 64 64 20 61 20 7a 65 72 6f   <td> Add a zero
26ce0 20 74 65 72 6d 69 6e 61 74 6f 72 20 69 66 20 6e   terminator if n
26cf0 65 65 64 65 64 0a 2a 2a 20 3c 2f 74 61 62 6c 65  eeded.** </table
26d00 3e 0a 2a 2a 20 3c 2f 62 6c 6f 63 6b 71 75 6f 74  >.** </blockquot
26d10 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62  e>.**.** The tab
26d20 6c 65 20 61 62 6f 76 65 20 6d 61 6b 65 73 20 72  le above makes r
26d30 65 66 65 72 65 6e 63 65 20 74 6f 20 73 74 61 6e  eference to stan
26d40 64 61 72 64 20 43 20 6c 69 62 72 61 72 79 20 66  dard C library f
26d50 75 6e 63 74 69 6f 6e 73 20 61 74 6f 69 28 29 0a  unctions atoi().
26d60 2a 2a 20 61 6e 64 20 61 74 6f 66 28 29 2e 20 20  ** and atof().  
26d70 53 51 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74 20  SQLite does not 
26d80 72 65 61 6c 6c 79 20 75 73 65 20 74 68 65 73 65  really use these
26d90 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 49 74 20   functions.  It 
26da0 68 61 73 20 69 74 73 0a 2a 2a 20 6f 77 6e 20 65  has its.** own e
26db0 71 75 69 76 61 6c 65 6e 74 20 69 6e 74 65 72 6e  quivalent intern
26dc0 61 6c 20 72 6f 75 74 69 6e 65 73 2e 20 20 54 68  al routines.  Th
26dd0 65 20 61 74 6f 69 28 29 20 61 6e 64 20 61 74 6f  e atoi() and ato
26de0 66 28 29 20 6e 61 6d 65 73 20 61 72 65 0a 2a 2a  f() names are.**
26df0 20 75 73 65 64 20 69 6e 20 74 68 65 20 74 61 62   used in the tab
26e00 6c 65 20 66 6f 72 20 62 72 65 76 69 74 79 20 61  le for brevity a
26e10 6e 64 20 62 65 63 61 75 73 65 20 74 68 65 79 20  nd because they 
26e20 61 72 65 20 66 61 6d 69 6c 69 61 72 20 74 6f 20  are familiar to 
26e30 6d 6f 73 74 0a 2a 2a 20 43 20 70 72 6f 67 72 61  most.** C progra
26e40 6d 6d 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74  mmers..**.** Not
26e50 65 20 74 68 61 74 20 77 68 65 6e 20 74 79 70 65  e that when type
26e60 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 6f 63 63   conversions occ
26e70 75 72 2c 20 70 6f 69 6e 74 65 72 73 20 72 65 74  ur, pointers ret
26e80 75 72 6e 65 64 20 62 79 20 70 72 69 6f 72 0a 2a  urned by prior.*
26e90 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74  * calls to sqlit
26ea0 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29  e3_column_blob()
26eb0 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  , sqlite3_column
26ec0 5f 74 65 78 74 28 29 2c 20 61 6e 64 2f 6f 72 0a  _text(), and/or.
26ed0 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ** sqlite3_colum
26ee0 6e 5f 74 65 78 74 31 36 28 29 20 6d 61 79 20 62  n_text16() may b
26ef0 65 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a  e invalidated..*
26f00 2a 20 54 79 70 65 20 63 6f 6e 76 65 72 73 69 6f  * Type conversio
26f10 6e 73 20 61 6e 64 20 70 6f 69 6e 74 65 72 20 69  ns and pointer i
26f20 6e 76 61 6c 69 64 61 74 69 6f 6e 73 20 6d 69 67  nvalidations mig
26f30 68 74 20 6f 63 63 75 72 0a 2a 2a 20 69 6e 20 74  ht occur.** in t
26f40 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 73  he following cas
26f50 65 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a  es:.**.** <ul>.*
26f60 2a 20 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74 69  * <li> The initi
26f70 61 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 61 20  al content is a 
26f80 42 4c 4f 42 20 61 6e 64 20 73 71 6c 69 74 65 33  BLOB and sqlite3
26f90 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 6f  _column_text() o
26fa0 72 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65  r.**      sqlite
26fb0 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28  3_column_text16(
26fc0 29 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 41 20  ) is called.  A 
26fd0 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 20  zero-terminator 
26fe0 6d 69 67 68 74 0a 2a 2a 20 20 20 20 20 20 6e 65  might.**      ne
26ff0 65 64 20 74 6f 20 62 65 20 61 64 64 65 64 20 74  ed to be added t
27000 6f 20 74 68 65 20 73 74 72 69 6e 67 2e 3c 2f 6c  o the string.</l
27010 69 3e 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 69  i>.** <li> The i
27020 6e 69 74 69 61 6c 20 63 6f 6e 74 65 6e 74 20 69  nitial content i
27030 73 20 55 54 46 2d 38 20 74 65 78 74 20 61 6e 64  s UTF-8 text and
27040 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
27050 62 79 74 65 73 31 36 28 29 20 6f 72 0a 2a 2a 20  bytes16() or.** 
27060 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c       sqlite3_col
27070 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 69 73 20  umn_text16() is 
27080 63 61 6c 6c 65 64 2e 20 20 54 68 65 20 63 6f 6e  called.  The con
27090 74 65 6e 74 20 6d 75 73 74 20 62 65 20 63 6f 6e  tent must be con
270a0 76 65 72 74 65 64 0a 2a 2a 20 20 20 20 20 20 74  verted.**      t
270b0 6f 20 55 54 46 2d 31 36 2e 3c 2f 6c 69 3e 0a 2a  o UTF-16.</li>.*
270c0 2a 20 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74 69  * <li> The initi
270d0 61 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 55 54  al content is UT
270e0 46 2d 31 36 20 74 65 78 74 20 61 6e 64 20 73 71  F-16 text and sq
270f0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
27100 65 73 28 29 20 6f 72 0a 2a 2a 20 20 20 20 20 20  es() or.**      
27110 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
27120 65 78 74 28 29 20 69 73 20 63 61 6c 6c 65 64 2e  ext() is called.
27130 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6d 75    The content mu
27140 73 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 0a  st be converted.
27150 2a 2a 20 20 20 20 20 20 74 6f 20 55 54 46 2d 38  **      to UTF-8
27160 2e 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a  .</li>.** </ul>.
27170 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 73 69 6f 6e  **.** Conversion
27180 73 20 62 65 74 77 65 65 6e 20 55 54 46 2d 31 36  s between UTF-16
27190 62 65 20 61 6e 64 20 55 54 46 2d 31 36 6c 65 20  be and UTF-16le 
271a0 61 72 65 20 61 6c 77 61 79 73 20 64 6f 6e 65 20  are always done 
271b0 69 6e 20 70 6c 61 63 65 20 61 6e 64 20 64 6f 0a  in place and do.
271c0 2a 2a 20 6e 6f 74 20 69 6e 76 61 6c 69 64 61 74  ** not invalidat
271d0 65 20 61 20 70 72 69 6f 72 20 70 6f 69 6e 74 65  e a prior pointe
271e0 72 2c 20 74 68 6f 75 67 68 20 6f 66 20 63 6f 75  r, though of cou
271f0 72 73 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  rse the content 
27200 6f 66 20 74 68 65 20 62 75 66 66 65 72 0a 2a 2a  of the buffer.**
27210 20 74 68 61 74 20 74 68 65 20 70 72 69 6f 72 20   that the prior 
27220 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 74  pointer points t
27230 6f 20 77 69 6c 6c 20 68 61 76 65 20 62 65 65 6e  o will have been
27240 20 6d 6f 64 69 66 69 65 64 2e 20 20 4f 74 68 65   modified.  Othe
27250 72 20 6b 69 6e 64 73 0a 2a 2a 20 6f 66 20 63 6f  r kinds.** of co
27260 6e 76 65 72 73 69 6f 6e 20 61 72 65 20 64 6f 6e  nversion are don
27270 65 20 69 6e 20 70 6c 61 63 65 20 77 68 65 6e 20  e in place when 
27280 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 2c 20  it is possible, 
27290 62 75 74 20 73 6f 6d 65 74 69 6d 65 73 20 74 68  but sometimes th
272a0 65 79 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 70 6f  ey.** are not po
272b0 73 73 69 62 6c 65 20 61 6e 64 20 69 6e 20 74 68  ssible and in th
272c0 6f 73 65 20 63 61 73 65 73 20 70 72 69 6f 72 20  ose cases prior 
272d0 70 6f 69 6e 74 65 72 73 20 61 72 65 20 69 6e 76  pointers are inv
272e0 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  alidated..**.** 
272f0 54 68 65 20 73 61 66 65 73 74 20 61 6e 64 20 65  The safest and e
27300 61 73 69 65 73 74 20 74 6f 20 72 65 6d 65 6d 62  asiest to rememb
27310 65 72 20 70 6f 6c 69 63 79 20 69 73 20 74 6f 20  er policy is to 
27320 69 6e 76 6f 6b 65 20 74 68 65 73 65 20 72 6f 75  invoke these rou
27330 74 69 6e 65 73 0a 2a 2a 20 69 6e 20 6f 6e 65 20  tines.** in one 
27340 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
27350 20 77 61 79 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c   ways:.**.** <ul
27360 3e 0a 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69 74 65  >.**  <li>sqlite
27370 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20  3_column_text() 
27380 66 6f 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c 69  followed by sqli
27390 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  te3_column_bytes
273a0 28 29 3c 2f 6c 69 3e 0a 2a 2a 20 20 3c 6c 69 3e  ()</li>.**  <li>
273b0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
273c0 6c 6f 62 28 29 20 66 6f 6c 6c 6f 77 65 64 20 62  lob() followed b
273d0 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  y sqlite3_column
273e0 5f 62 79 74 65 73 28 29 3c 2f 6c 69 3e 0a 2a 2a  _bytes()</li>.**
273f0 20 20 3c 6c 69 3e 73 71 6c 69 74 65 33 5f 63 6f    <li>sqlite3_co
27400 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 6f  lumn_text16() fo
27410 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c 69 74 65  llowed by sqlite
27420 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36  3_column_bytes16
27430 28 29 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e  ()</li>.** </ul>
27440 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20  .**.** In other 
27450 77 6f 72 64 73 2c 20 79 6f 75 20 73 68 6f 75 6c  words, you shoul
27460 64 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 63  d call sqlite3_c
27470 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 2c 0a 2a 2a  olumn_text(),.**
27480 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
27490 62 6c 6f 62 28 29 2c 20 6f 72 20 73 71 6c 69 74  blob(), or sqlit
274a0 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36  e3_column_text16
274b0 28 29 20 66 69 72 73 74 20 74 6f 20 66 6f 72 63  () first to forc
274c0 65 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20  e the result.** 
274d0 69 6e 74 6f 20 74 68 65 20 64 65 73 69 72 65 64  into the desired
274e0 20 66 6f 72 6d 61 74 2c 20 74 68 65 6e 20 69 6e   format, then in
274f0 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  voke sqlite3_col
27500 75 6d 6e 5f 62 79 74 65 73 28 29 20 6f 72 0a 2a  umn_bytes() or.*
27510 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  * sqlite3_column
27520 5f 62 79 74 65 73 31 36 28 29 20 74 6f 20 66 69  _bytes16() to fi
27530 6e 64 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  nd the size of t
27540 68 65 20 72 65 73 75 6c 74 2e 20 20 44 6f 20 6e  he result.  Do n
27550 6f 74 20 6d 69 78 20 63 61 6c 6c 73 0a 2a 2a 20  ot mix calls.** 
27560 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  to sqlite3_colum
27570 6e 5f 74 65 78 74 28 29 20 6f 72 20 73 71 6c 69  n_text() or sqli
27580 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
27590 29 20 77 69 74 68 20 63 61 6c 6c 73 20 74 6f 0a  ) with calls to.
275a0 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ** sqlite3_colum
275b0 6e 5f 62 79 74 65 73 31 36 28 29 2c 20 61 6e 64  n_bytes16(), and
275c0 20 64 6f 20 6e 6f 74 20 6d 69 78 20 63 61 6c 6c   do not mix call
275d0 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c  s to sqlite3_col
275e0 75 6d 6e 5f 74 65 78 74 31 36 28 29 0a 2a 2a 20  umn_text16().** 
275f0 77 69 74 68 20 63 61 6c 6c 73 20 74 6f 20 73 71  with calls to sq
27600 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
27610 65 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  es()..**.** The 
27620 70 6f 69 6e 74 65 72 73 20 72 65 74 75 72 6e 65  pointers returne
27630 64 20 61 72 65 20 76 61 6c 69 64 20 75 6e 74 69  d are valid unti
27640 6c 20 61 20 74 79 70 65 20 63 6f 6e 76 65 72 73  l a type convers
27650 69 6f 6e 20 6f 63 63 75 72 73 20 61 73 0a 2a 2a  ion occurs as.**
27660 20 64 65 73 63 72 69 62 65 64 20 61 62 6f 76 65   described above
27670 2c 20 6f 72 20 75 6e 74 69 6c 20 5b 73 71 6c 69  , or until [sqli
27680 74 65 33 5f 73 74 65 70 28 29 5d 20 6f 72 20 5b  te3_step()] or [
27690 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d  sqlite3_reset()]
276a0 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   or.** [sqlite3_
276b0 66 69 6e 61 6c 69 7a 65 28 29 5d 20 69 73 20 63  finalize()] is c
276c0 61 6c 6c 65 64 2e 20 20 54 68 65 20 6d 65 6d 6f  alled.  The memo
276d0 72 79 20 73 70 61 63 65 20 75 73 65 64 20 74 6f  ry space used to
276e0 20 68 6f 6c 64 20 73 74 72 69 6e 67 73 0a 2a 2a   hold strings.**
276f0 20 61 6e 64 20 42 4c 4f 42 73 20 69 73 20 66 72   and BLOBs is fr
27700 65 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  eed automaticall
27710 79 2e 20 20 44 6f 20 3c 62 3e 6e 6f 74 3c 2f 62  y.  Do <b>not</b
27720 3e 20 70 61 73 73 20 74 68 65 20 70 6f 69 6e 74  > pass the point
27730 65 72 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20  ers returned.** 
27740 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  [sqlite3_column_
27750 62 6c 6f 62 28 29 5d 2c 20 5b 73 71 6c 69 74 65  blob()], [sqlite
27760 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 5d  3_column_text()]
27770 2c 20 65 74 63 2e 20 69 6e 74 6f 0a 2a 2a 20 5b  , etc. into.** [
27780 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e  sqlite3_free()].
27790 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f  .**.** If a memo
277a0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72  ry allocation er
277b0 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e  ror occurs durin
277c0 67 20 74 68 65 20 65 76 61 6c 75 61 74 69 6f 6e  g the evaluation
277d0 20 6f 66 20 61 6e 79 0a 2a 2a 20 6f 66 20 74 68   of any.** of th
277e0 65 73 65 20 72 6f 75 74 69 6e 65 73 2c 20 61 20  ese routines, a 
277f0 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 69 73  default value is
27800 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 65 20   returned.  The 
27810 64 65 66 61 75 6c 74 20 76 61 6c 75 65 0a 2a 2a  default value.**
27820 20 69 73 20 65 69 74 68 65 72 20 74 68 65 20 69   is either the i
27830 6e 74 65 67 65 72 20 30 2c 20 74 68 65 20 66 6c  nteger 0, the fl
27840 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d  oating point num
27850 62 65 72 20 30 2e 30 2c 20 6f 72 20 61 20 4e 55  ber 0.0, or a NU
27860 4c 4c 0a 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 20  LL.** pointer.  
27870 53 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73  Subsequent calls
27880 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72 72   to [sqlite3_err
27890 63 6f 64 65 28 29 5d 20 77 69 6c 6c 20 72 65 74  code()] will ret
278a0 75 72 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4e  urn.** [SQLITE_N
278b0 4f 4d 45 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  OMEM]..**.** Req
278c0 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
278d0 31 33 38 30 33 5d 20 5b 48 31 33 38 30 36 5d 20  13803] [H13806] 
278e0 5b 48 31 33 38 30 39 5d 20 5b 48 31 33 38 31 32  [H13809] [H13812
278f0 5d 20 5b 48 31 33 38 31 35 5d 20 5b 48 31 33 38  ] [H13815] [H138
27900 31 38 5d 20 5b 48 31 33 38 32 31 5d 20 5b 48 31  18] [H13821] [H1
27910 33 38 32 34 5d 0a 2a 2a 20 5b 48 31 33 38 32 37  3824].** [H13827
27920 5d 20 5b 48 31 33 38 33 30 5d 0a 2a 2f 0a 53 51  ] [H13830].*/.SQ
27930 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76  LITE_API const v
27940 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  oid *sqlite3_col
27950 75 6d 6e 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33  umn_blob(sqlite3
27960 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c  _stmt*, int iCol
27970 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
27980 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  t sqlite3_column
27990 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f 73  _bytes(sqlite3_s
279a0 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b  tmt*, int iCol);
279b0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
279c0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
279d0 79 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f 73  ytes16(sqlite3_s
279e0 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b  tmt*, int iCol);
279f0 0a 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62  .SQLITE_API doub
27a00 6c 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  le sqlite3_colum
27a10 6e 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33  n_double(sqlite3
27a20 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c  _stmt*, int iCol
27a30 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
27a40 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  t sqlite3_column
27a50 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d  _int(sqlite3_stm
27a60 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53  t*, int iCol);.S
27a70 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
27a80 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f  3_int64 sqlite3_
27a90 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71 6c  column_int64(sql
27aa0 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20  ite3_stmt*, int 
27ab0 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50  iCol);.SQLITE_AP
27ac0 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  I const unsigned
27ad0 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63   char *sqlite3_c
27ae0 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69 74  olumn_text(sqlit
27af0 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43  e3_stmt*, int iC
27b00 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  ol);.SQLITE_API 
27b10 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
27b20 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31  te3_column_text1
27b30 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  6(sqlite3_stmt*,
27b40 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49   int iCol);.SQLI
27b50 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
27b60 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73  e3_column_type(s
27b70 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
27b80 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f  t iCol);.SQLITE_
27b90 41 50 49 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  API sqlite3_valu
27ba0 65 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  e *sqlite3_colum
27bb0 6e 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f  n_value(sqlite3_
27bc0 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29  stmt*, int iCol)
27bd0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
27be0 46 3a 20 44 65 73 74 72 6f 79 20 41 20 50 72 65  F: Destroy A Pre
27bf0 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20  pared Statement 
27c00 4f 62 6a 65 63 74 20 7b 48 31 33 33 30 30 7d 20  Object {H13300} 
27c10 3c 53 37 30 33 30 30 3e 3c 53 33 30 31 30 30 3e  <S70300><S30100>
27c20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
27c30 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 20 66 75  e3_finalize() fu
27c40 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
27c50 20 74 6f 20 64 65 6c 65 74 65 20 61 20 5b 70 72   to delete a [pr
27c60 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
27c70 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 61  ]..** If the sta
27c80 74 65 6d 65 6e 74 20 77 61 73 20 65 78 65 63 75  tement was execu
27c90 74 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79  ted successfully
27ca0 20 6f 72 20 6e 6f 74 20 65 78 65 63 75 74 65 64   or not executed
27cb0 20 61 74 20 61 6c 6c 2c 20 74 68 65 6e 0a 2a 2a   at all, then.**
27cc0 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
27cd0 74 75 72 6e 65 64 2e 20 49 66 20 65 78 65 63 75  turned. If execu
27ce0 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 74 61 74  tion of the stat
27cf0 65 6d 65 6e 74 20 66 61 69 6c 65 64 20 74 68 65  ement failed the
27d00 6e 20 61 6e 0a 2a 2a 20 5b 65 72 72 6f 72 20 63  n an.** [error c
27d10 6f 64 65 5d 20 6f 72 20 5b 65 78 74 65 6e 64 65  ode] or [extende
27d20 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73  d error code] is
27d30 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
27d40 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61   This routine ca
27d50 6e 20 62 65 20 63 61 6c 6c 65 64 20 61 74 20 61  n be called at a
27d60 6e 79 20 70 6f 69 6e 74 20 64 75 72 69 6e 67 20  ny point during 
27d70 74 68 65 20 65 78 65 63 75 74 69 6f 6e 20 6f 66  the execution of
27d80 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65   the.** [prepare
27d90 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 49  d statement].  I
27da0 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61  f the virtual ma
27db0 63 68 69 6e 65 20 68 61 73 20 6e 6f 74 0a 2a 2a  chine has not.**
27dc0 20 63 6f 6d 70 6c 65 74 65 64 20 65 78 65 63 75   completed execu
27dd0 74 69 6f 6e 20 77 68 65 6e 20 74 68 69 73 20 72  tion when this r
27de0 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
27df0 2c 20 74 68 61 74 20 69 73 20 6c 69 6b 65 0a 2a  , that is like.*
27e00 2a 20 65 6e 63 6f 75 6e 74 65 72 69 6e 67 20 61  * encountering a
27e10 6e 20 65 72 72 6f 72 20 6f 72 20 61 6e 20 5b 73  n error or an [s
27e20 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
27e30 20 7c 20 69 6e 74 65 72 72 75 70 74 5d 2e 0a 2a   | interrupt]..*
27e40 2a 20 49 6e 63 6f 6d 70 6c 65 74 65 20 75 70 64  * Incomplete upd
27e50 61 74 65 73 20 6d 61 79 20 62 65 20 72 6f 6c 6c  ates may be roll
27e60 65 64 20 62 61 63 6b 20 61 6e 64 20 74 72 61 6e  ed back and tran
27e70 73 61 63 74 69 6f 6e 73 20 63 61 6e 63 65 6c 65  sactions cancele
27e80 64 2c 0a 2a 2a 20 64 65 70 65 6e 64 69 6e 67 20  d,.** depending 
27e90 6f 6e 20 74 68 65 20 63 69 72 63 75 6d 73 74 61  on the circumsta
27ea0 6e 63 65 73 2c 20 61 6e 64 20 74 68 65 0a 2a 2a  nces, and the.**
27eb0 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 72 65   [error code] re
27ec0 74 75 72 6e 65 64 20 77 69 6c 6c 20 62 65 20 5b  turned will be [
27ed0 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a  SQLITE_ABORT]..*
27ee0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
27ef0 73 3a 0a 2a 2a 20 5b 48 31 31 33 30 32 5d 20 5b  s:.** [H11302] [
27f00 48 31 31 33 30 34 5d 0a 2a 2f 0a 53 51 4c 49 54  H11304].*/.SQLIT
27f10 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
27f20 33 5f 66 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74  3_finalize(sqlit
27f30 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b  e3_stmt *pStmt);
27f40 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
27f50 3a 20 52 65 73 65 74 20 41 20 50 72 65 70 61 72  : Reset A Prepar
27f60 65 64 20 53 74 61 74 65 6d 65 6e 74 20 4f 62 6a  ed Statement Obj
27f70 65 63 74 20 7b 48 31 33 33 33 30 7d 20 3c 53 37  ect {H13330} <S7
27f80 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  0300>.**.** The 
27f90 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 20  sqlite3_reset() 
27fa0 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
27fb0 65 64 20 74 6f 20 72 65 73 65 74 20 61 20 5b 70  ed to reset a [p
27fc0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
27fd0 74 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 62 61 63  t].** object bac
27fe0 6b 20 74 6f 20 69 74 73 20 69 6e 69 74 69 61 6c  k to its initial
27ff0 20 73 74 61 74 65 2c 20 72 65 61 64 79 20 74 6f   state, ready to
28000 20 62 65 20 72 65 2d 65 78 65 63 75 74 65 64 2e   be re-executed.
28010 0a 2a 2a 20 41 6e 79 20 53 51 4c 20 73 74 61 74  .** Any SQL stat
28020 65 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 73 20  ement variables 
28030 74 68 61 74 20 68 61 64 20 76 61 6c 75 65 73 20  that had values 
28040 62 6f 75 6e 64 20 74 6f 20 74 68 65 6d 20 75 73  bound to them us
28050 69 6e 67 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69  ing.** the [sqli
28060 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 7c 20  te3_bind_blob | 
28070 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29  sqlite3_bind_*()
28080 20 41 50 49 5d 20 72 65 74 61 69 6e 20 74 68 65   API] retain the
28090 69 72 20 76 61 6c 75 65 73 2e 0a 2a 2a 20 55 73  ir values..** Us
280a0 65 20 5b 73 71 6c 69 74 65 33 5f 63 6c 65 61 72  e [sqlite3_clear
280b0 5f 62 69 6e 64 69 6e 67 73 28 29 5d 20 74 6f 20  _bindings()] to 
280c0 72 65 73 65 74 20 74 68 65 20 62 69 6e 64 69 6e  reset the bindin
280d0 67 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 33  gs..**.** {H1133
280e0 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  2} The [sqlite3_
280f0 72 65 73 65 74 28 53 29 5d 20 69 6e 74 65 72 66  reset(S)] interf
28100 61 63 65 20 72 65 73 65 74 73 20 74 68 65 20 5b  ace resets the [
28110 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
28120 6e 74 5d 20 53 0a 2a 2a 20 20 20 20 20 20 20 20  nt] S.**        
28130 20 20 62 61 63 6b 20 74 6f 20 74 68 65 20 62 65    back to the be
28140 67 69 6e 6e 69 6e 67 20 6f 66 20 69 74 73 20 70  ginning of its p
28150 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 7b 48  rogram..**.** {H
28160 31 31 33 33 34 7d 20 49 66 20 74 68 65 20 6d 6f  11334} If the mo
28170 73 74 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74  st recent call t
28180 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  o [sqlite3_step(
28190 53 29 5d 20 66 6f 72 20 74 68 65 0a 2a 2a 20 20  S)] for the.**  
281a0 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 72 65          [prepare
281b0 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 72  d statement] S r
281c0 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f  eturned [SQLITE_
281d0 52 4f 57 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f  ROW] or [SQLITE_
281e0 44 4f 4e 45 5d 2c 0a 2a 2a 20 20 20 20 20 20 20  DONE],.**       
281f0 20 20 20 6f 72 20 69 66 20 5b 73 71 6c 69 74 65     or if [sqlite
28200 33 5f 73 74 65 70 28 53 29 5d 20 68 61 73 20 6e  3_step(S)] has n
28210 65 76 65 72 20 62 65 66 6f 72 65 20 62 65 65 6e  ever before been
28220 20 63 61 6c 6c 65 64 20 6f 6e 20 53 2c 0a 2a 2a   called on S,.**
28230 20 20 20 20 20 20 20 20 20 20 74 68 65 6e 20 5b            then [
28240 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53 29  sqlite3_reset(S)
28250 5d 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54  ] returns [SQLIT
28260 45 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  E_OK]..**.** {H1
28270 31 33 33 36 7d 20 49 66 20 74 68 65 20 6d 6f 73  1336} If the mos
28280 74 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f  t recent call to
28290 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53   [sqlite3_step(S
282a0 29 5d 20 66 6f 72 20 74 68 65 0a 2a 2a 20 20 20  )] for the.**   
282b0 20 20 20 20 20 20 20 5b 70 72 65 70 61 72 65 64         [prepared
282c0 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 69 6e   statement] S in
282d0 64 69 63 61 74 65 64 20 61 6e 20 65 72 72 6f 72  dicated an error
282e0 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20  , then.**       
282f0 20 20 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65     [sqlite3_rese
28300 74 28 53 29 5d 20 72 65 74 75 72 6e 73 20 61 6e  t(S)] returns an
28310 20 61 70 70 72 6f 70 72 69 61 74 65 20 5b 65 72   appropriate [er
28320 72 6f 72 20 63 6f 64 65 5d 2e 0a 2a 2a 0a 2a 2a  ror code]..**.**
28330 20 7b 48 31 31 33 33 38 7d 20 54 68 65 20 5b 73   {H11338} The [s
28340 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53 29 5d  qlite3_reset(S)]
28350 20 69 6e 74 65 72 66 61 63 65 20 64 6f 65 73 20   interface does 
28360 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65 20 76  not change the v
28370 61 6c 75 65 73 0a 2a 2a 20 20 20 20 20 20 20 20  alues.**        
28380 20 20 6f 66 20 61 6e 79 20 5b 73 71 6c 69 74 65    of any [sqlite
28390 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 62 69 6e 64  3_bind_blob|bind
283a0 69 6e 67 73 5d 20 6f 6e 20 74 68 65 20 5b 70 72  ings] on the [pr
283b0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
283c0 5d 20 53 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ] S..*/.SQLITE_A
283d0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72  PI int sqlite3_r
283e0 65 73 65 74 28 73 71 6c 69 74 65 33 5f 73 74 6d  eset(sqlite3_stm
283f0 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a  t *pStmt);../*.*
28400 2a 20 43 41 50 49 33 52 45 46 3a 20 43 72 65 61  * CAPI3REF: Crea
28410 74 65 20 4f 72 20 52 65 64 65 66 69 6e 65 20 53  te Or Redefine S
28420 51 4c 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48 31  QL Functions {H1
28430 36 31 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a  6100} <S20200>.*
28440 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 66 75 6e  * KEYWORDS: {fun
28450 63 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20 72  ction creation r
28460 6f 75 74 69 6e 65 73 7d 0a 2a 2a 20 4b 45 59 57  outines}.** KEYW
28470 4f 52 44 53 3a 20 7b 61 70 70 6c 69 63 61 74 69  ORDS: {applicati
28480 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66  on-defined SQL f
28490 75 6e 63 74 69 6f 6e 7d 0a 2a 2a 20 4b 45 59 57  unction}.** KEYW
284a0 4f 52 44 53 3a 20 7b 61 70 70 6c 69 63 61 74 69  ORDS: {applicati
284b0 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66  on-defined SQL f
284c0 75 6e 63 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20  unctions}.**.** 
284d0 54 68 65 73 65 20 74 77 6f 20 66 75 6e 63 74 69  These two functi
284e0 6f 6e 73 20 28 63 6f 6c 6c 65 63 74 69 76 65 6c  ons (collectivel
284f0 79 20 6b 6e 6f 77 6e 20 61 73 20 22 66 75 6e 63  y known as "func
28500 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20 72 6f  tion creation ro
28510 75 74 69 6e 65 73 22 29 0a 2a 2a 20 61 72 65 20  utines").** are 
28520 75 73 65 64 20 74 6f 20 61 64 64 20 53 51 4c 20  used to add SQL 
28530 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20 61 67 67  functions or agg
28540 72 65 67 61 74 65 73 20 6f 72 20 74 6f 20 72 65  regates or to re
28550 64 65 66 69 6e 65 20 74 68 65 20 62 65 68 61 76  define the behav
28560 69 6f 72 0a 2a 2a 20 6f 66 20 65 78 69 73 74 69  ior.** of existi
28570 6e 67 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73  ng SQL functions
28580 20 6f 72 20 61 67 67 72 65 67 61 74 65 73 2e 20   or aggregates. 
28590 20 54 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 72   The only differ
285a0 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 65  ence between the
285b0 0a 2a 2a 20 74 77 6f 20 69 73 20 74 68 61 74 20  .** two is that 
285c0 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  the second param
285d0 65 74 65 72 2c 20 74 68 65 20 6e 61 6d 65 20 6f  eter, the name o
285e0 66 20 74 68 65 20 28 73 63 61 6c 61 72 29 20 66  f the (scalar) f
285f0 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67  unction or.** ag
28600 67 72 65 67 61 74 65 2c 20 69 73 20 65 6e 63 6f  gregate, is enco
28610 64 65 64 20 69 6e 20 55 54 46 2d 38 20 66 6f 72  ded in UTF-8 for
28620 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
28630 66 75 6e 63 74 69 6f 6e 28 29 20 61 6e 64 20 55  function() and U
28640 54 46 2d 31 36 0a 2a 2a 20 66 6f 72 20 73 71 6c  TF-16.** for sql
28650 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
28660 74 69 6f 6e 31 36 28 29 2e 0a 2a 2a 0a 2a 2a 20  tion16()..**.** 
28670 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65  The first parame
28680 74 65 72 20 69 73 20 74 68 65 20 5b 64 61 74 61  ter is the [data
28690 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
286a0 20 74 6f 20 77 68 69 63 68 20 74 68 65 20 53 51   to which the SQ
286b0 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73  L.** function is
286c0 20 74 6f 20 62 65 20 61 64 64 65 64 2e 20 20 49   to be added.  I
286d0 66 20 61 20 73 69 6e 67 6c 65 20 70 72 6f 67 72  f a single progr
286e0 61 6d 20 75 73 65 73 20 6d 6f 72 65 20 74 68 61  am uses more tha
286f0 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 0a 2a  n one database.*
28700 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 6e 74  * connection int
28710 65 72 6e 61 6c 6c 79 2c 20 74 68 65 6e 20 53 51  ernally, then SQ
28720 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6d 75 73 74  L functions must
28730 20 62 65 20 61 64 64 65 64 20 69 6e 64 69 76 69   be added indivi
28740 64 75 61 6c 6c 79 20 74 6f 0a 2a 2a 20 65 61 63  dually to.** eac
28750 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  h database conne
28760 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ction..**.** The
28770 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   second paramete
28780 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  r is the name of
28790 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f   the SQL functio
287a0 6e 20 74 6f 20 62 65 20 63 72 65 61 74 65 64 20  n to be created 
287b0 6f 72 0a 2a 2a 20 72 65 64 65 66 69 6e 65 64 2e  or.** redefined.
287c0 20 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 20    The length of 
287d0 74 68 65 20 6e 61 6d 65 20 69 73 20 6c 69 6d 69  the name is limi
287e0 74 65 64 20 74 6f 20 32 35 35 20 62 79 74 65 73  ted to 255 bytes
287f0 2c 20 65 78 63 6c 75 73 69 76 65 20 6f 66 0a 2a  , exclusive of.*
28800 2a 20 74 68 65 20 7a 65 72 6f 2d 74 65 72 6d 69  * the zero-termi
28810 6e 61 74 6f 72 2e 20 20 4e 6f 74 65 20 74 68 61  nator.  Note tha
28820 74 20 74 68 65 20 6e 61 6d 65 20 6c 65 6e 67 74  t the name lengt
28830 68 20 6c 69 6d 69 74 20 69 73 20 69 6e 20 62 79  h limit is in by
28840 74 65 73 2c 20 6e 6f 74 0a 2a 2a 20 63 68 61 72  tes, not.** char
28850 61 63 74 65 72 73 2e 20 20 41 6e 79 20 61 74 74  acters.  Any att
28860 65 6d 70 74 20 74 6f 20 63 72 65 61 74 65 20 61  empt to create a
28870 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 61   function with a
28880 20 6c 6f 6e 67 65 72 20 6e 61 6d 65 0a 2a 2a 20   longer name.** 
28890 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 5b  will result in [
288a0 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 62 65  SQLITE_ERROR] be
288b0 69 6e 67 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a  ing returned..**
288c0 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20 70 61  .** The third pa
288d0 72 61 6d 65 74 65 72 20 28 6e 41 72 67 29 0a 2a  rameter (nArg).*
288e0 2a 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  * is the number 
288f0 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61  of arguments tha
28900 74 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69  t the SQL functi
28910 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61  on or.** aggrega
28920 74 65 20 74 61 6b 65 73 2e 20 49 66 20 74 68 69  te takes. If thi
28930 73 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 2d  s parameter is -
28940 31 2c 20 74 68 65 6e 20 74 68 65 20 53 51 4c 20  1, then the SQL 
28950 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61  function or.** a
28960 67 67 72 65 67 61 74 65 20 6d 61 79 20 74 61 6b  ggregate may tak
28970 65 20 61 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20  e any number of 
28980 61 72 67 75 6d 65 6e 74 73 20 62 65 74 77 65 65  arguments betwee
28990 6e 20 30 20 61 6e 64 20 74 68 65 20 6c 69 6d 69  n 0 and the limi
289a0 74 0a 2a 2a 20 73 65 74 20 62 79 20 5b 73 71 6c  t.** set by [sql
289b0 69 74 65 33 5f 6c 69 6d 69 74 5d 28 5b 53 51 4c  ite3_limit]([SQL
289c0 49 54 45 5f 4c 49 4d 49 54 5f 46 55 4e 43 54 49  ITE_LIMIT_FUNCTI
289d0 4f 4e 5f 41 52 47 5d 29 2e 20 20 49 66 20 74 68  ON_ARG]).  If th
289e0 65 20 74 68 69 72 64 0a 2a 2a 20 70 61 72 61 6d  e third.** param
289f0 65 74 65 72 20 69 73 20 6c 65 73 73 20 74 68 61  eter is less tha
28a00 6e 20 2d 31 20 6f 72 20 67 72 65 61 74 65 72 20  n -1 or greater 
28a10 74 68 61 6e 20 31 32 37 20 74 68 65 6e 20 74 68  than 127 then th
28a20 65 20 62 65 68 61 76 69 6f 72 20 69 73 0a 2a 2a  e behavior is.**
28a30 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a   undefined..**.*
28a40 2a 20 54 68 65 20 66 6f 75 72 74 68 20 70 61 72  * The fourth par
28a50 61 6d 65 74 65 72 2c 20 65 54 65 78 74 52 65 70  ameter, eTextRep
28a60 2c 20 73 70 65 63 69 66 69 65 73 20 77 68 61 74  , specifies what
28a70 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 55 54 46 38  .** [SQLITE_UTF8
28a80 20 7c 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67   | text encoding
28a90 5d 20 74 68 69 73 20 53 51 4c 20 66 75 6e 63 74  ] this SQL funct
28aa0 69 6f 6e 20 70 72 65 66 65 72 73 20 66 6f 72 0a  ion prefers for.
28ab0 2a 2a 20 69 74 73 20 70 61 72 61 6d 65 74 65 72  ** its parameter
28ac0 73 2e 20 20 41 6e 79 20 53 51 4c 20 66 75 6e 63  s.  Any SQL func
28ad0 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74  tion implementat
28ae0 69 6f 6e 20 73 68 6f 75 6c 64 20 62 65 20 61 62  ion should be ab
28af0 6c 65 20 74 6f 20 77 6f 72 6b 0a 2a 2a 20 77 6f  le to work.** wo
28b00 72 6b 20 77 69 74 68 20 55 54 46 2d 38 2c 20 55  rk with UTF-8, U
28b10 54 46 2d 31 36 6c 65 2c 20 6f 72 20 55 54 46 2d  TF-16le, or UTF-
28b20 31 36 62 65 2e 20 20 42 75 74 20 73 6f 6d 65 20  16be.  But some 
28b30 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
28b40 6d 61 79 20 62 65 0a 2a 2a 20 6d 6f 72 65 20 65  may be.** more e
28b50 66 66 69 63 69 65 6e 74 20 77 69 74 68 20 6f 6e  fficient with on
28b60 65 20 65 6e 63 6f 64 69 6e 67 20 74 68 61 6e 20  e encoding than 
28b70 61 6e 6f 74 68 65 72 2e 20 20 41 6e 20 61 70 70  another.  An app
28b80 6c 69 63 61 74 69 6f 6e 20 6d 61 79 0a 2a 2a 20  lication may.** 
28b90 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f 63  invoke sqlite3_c
28ba0 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29  reate_function()
28bb0 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61   or sqlite3_crea
28bc0 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 20  te_function16() 
28bd0 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 74 69 6d 65  multiple.** time
28be0 73 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20  s with the same 
28bf0 66 75 6e 63 74 69 6f 6e 20 62 75 74 20 77 69 74  function but wit
28c00 68 20 64 69 66 66 65 72 65 6e 74 20 76 61 6c 75  h different valu
28c10 65 73 20 6f 66 20 65 54 65 78 74 52 65 70 2e 0a  es of eTextRep..
28c20 2a 2a 20 57 68 65 6e 20 6d 75 6c 74 69 70 6c 65  ** When multiple
28c30 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
28c40 20 6f 66 20 74 68 65 20 73 61 6d 65 20 66 75 6e   of the same fun
28c50 63 74 69 6f 6e 20 61 72 65 20 61 76 61 69 6c 61  ction are availa
28c60 62 6c 65 2c 20 53 51 4c 69 74 65 0a 2a 2a 20 77  ble, SQLite.** w
28c70 69 6c 6c 20 70 69 63 6b 20 74 68 65 20 6f 6e 65  ill pick the one
28c80 20 74 68 61 74 20 69 6e 76 6f 6c 76 65 73 20 74   that involves t
28c90 68 65 20 6c 65 61 73 74 20 61 6d 6f 75 6e 74 20  he least amount 
28ca0 6f 66 20 64 61 74 61 20 63 6f 6e 76 65 72 73 69  of data conversi
28cb0 6f 6e 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20  on..** If there 
28cc0 69 73 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65  is only a single
28cd0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
28ce0 77 68 69 63 68 20 64 6f 65 73 20 6e 6f 74 20 63  which does not c
28cf0 61 72 65 20 77 68 61 74 20 74 65 78 74 0a 2a 2a  are what text.**
28d00 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 75 73 65   encoding is use
28d10 64 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 75 72  d, then the four
28d20 74 68 20 61 72 67 75 6d 65 6e 74 20 73 68 6f 75  th argument shou
28d30 6c 64 20 62 65 20 5b 53 51 4c 49 54 45 5f 41 4e  ld be [SQLITE_AN
28d40 59 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  Y]..**.** The fi
28d50 66 74 68 20 70 61 72 61 6d 65 74 65 72 20 69 73  fth parameter is
28d60 20 61 6e 20 61 72 62 69 74 72 61 72 79 20 70 6f   an arbitrary po
28d70 69 6e 74 65 72 2e 20 20 54 68 65 20 69 6d 70 6c  inter.  The impl
28d80 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
28d90 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 63 61  e.** function ca
28da0 6e 20 67 61 69 6e 20 61 63 63 65 73 73 20 74 6f  n gain access to
28db0 20 74 68 69 73 20 70 6f 69 6e 74 65 72 20 75 73   this pointer us
28dc0 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 75 73 65  ing [sqlite3_use
28dd0 72 5f 64 61 74 61 28 29 5d 2e 0a 2a 2a 0a 2a 2a  r_data()]..**.**
28de0 20 54 68 65 20 73 65 76 65 6e 74 68 2c 20 65 69   The seventh, ei
28df0 67 68 74 68 20 61 6e 64 20 6e 69 6e 74 68 20 70  ghth and ninth p
28e00 61 72 61 6d 65 74 65 72 73 2c 20 78 46 75 6e 63  arameters, xFunc
28e10 2c 20 78 53 74 65 70 20 61 6e 64 20 78 46 69 6e  , xStep and xFin
28e20 61 6c 2c 20 61 72 65 0a 2a 2a 20 70 6f 69 6e 74  al, are.** point
28e30 65 72 73 20 74 6f 20 43 2d 6c 61 6e 67 75 61 67  ers to C-languag
28e40 65 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74  e functions that
28e50 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 53   implement the S
28e60 51 4c 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a  QL function or.*
28e70 2a 20 61 67 67 72 65 67 61 74 65 2e 20 41 20 73  * aggregate. A s
28e80 63 61 6c 61 72 20 53 51 4c 20 66 75 6e 63 74 69  calar SQL functi
28e90 6f 6e 20 72 65 71 75 69 72 65 73 20 61 6e 20 69  on requires an i
28ea0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
28eb0 20 74 68 65 20 78 46 75 6e 63 0a 2a 2a 20 63 61   the xFunc.** ca
28ec0 6c 6c 62 61 63 6b 20 6f 6e 6c 79 2c 20 4e 55 4c  llback only, NUL
28ed0 4c 20 70 6f 69 6e 74 65 72 73 20 73 68 6f 75 6c  L pointers shoul
28ee0 64 20 62 65 20 70 61 73 73 65 64 20 61 73 20 74  d be passed as t
28ef0 68 65 20 78 53 74 65 70 20 61 6e 64 20 78 46 69  he xStep and xFi
28f00 6e 61 6c 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  nal.** parameter
28f10 73 2e 20 41 6e 20 61 67 67 72 65 67 61 74 65 20  s. An aggregate 
28f20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 72 65 71  SQL function req
28f30 75 69 72 65 73 20 61 6e 20 69 6d 70 6c 65 6d 65  uires an impleme
28f40 6e 74 61 74 69 6f 6e 20 6f 66 20 78 53 74 65 70  ntation of xStep
28f50 0a 2a 2a 20 61 6e 64 20 78 46 69 6e 61 6c 20 61  .** and xFinal a
28f60 6e 64 20 4e 55 4c 4c 20 73 68 6f 75 6c 64 20 62  nd NULL should b
28f70 65 20 70 61 73 73 65 64 20 66 6f 72 20 78 46 75  e passed for xFu
28f80 6e 63 2e 20 54 6f 20 64 65 6c 65 74 65 20 61 6e  nc. To delete an
28f90 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 53 51 4c   existing.** SQL
28fa0 20 66 75 6e 63 74 69 6f 6e 20 6f 72 20 61 67 67   function or agg
28fb0 72 65 67 61 74 65 2c 20 70 61 73 73 20 4e 55 4c  regate, pass NUL
28fc0 4c 20 66 6f 72 20 61 6c 6c 20 74 68 72 65 65 20  L for all three 
28fd0 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 62 61 63  function callbac
28fe0 6b 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20  ks..**.** It is 
28ff0 70 65 72 6d 69 74 74 65 64 20 74 6f 20 72 65 67  permitted to reg
29000 69 73 74 65 72 20 6d 75 6c 74 69 70 6c 65 20 69  ister multiple i
29010 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f  mplementations o
29020 66 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 66 75  f the same.** fu
29030 6e 63 74 69 6f 6e 73 20 77 69 74 68 20 74 68 65  nctions with the
29040 20 73 61 6d 65 20 6e 61 6d 65 20 62 75 74 20 77   same name but w
29050 69 74 68 20 65 69 74 68 65 72 20 64 69 66 66 65  ith either diffe
29060 72 69 6e 67 20 6e 75 6d 62 65 72 73 20 6f 66 0a  ring numbers of.
29070 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20 6f 72 20  ** arguments or 
29080 64 69 66 66 65 72 69 6e 67 20 70 72 65 66 65 72  differing prefer
29090 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  red text encodin
290a0 67 73 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c  gs.  SQLite will
290b0 20 75 73 65 0a 2a 2a 20 74 68 65 20 69 6d 70 6c   use.** the impl
290c0 65 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 74 20  ementation that 
290d0 6d 6f 73 74 20 63 6c 6f 73 65 6c 79 20 6d 61 74  most closely mat
290e0 63 68 65 73 20 74 68 65 20 77 61 79 20 69 6e 20  ches the way in 
290f0 77 68 69 63 68 20 74 68 65 0a 2a 2a 20 53 51 4c  which the.** SQL
29100 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
29110 64 2e 20 20 41 20 66 75 6e 63 74 69 6f 6e 20 69  d.  A function i
29120 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69  mplementation wi
29130 74 68 20 61 20 6e 6f 6e 2d 6e 65 67 61 74 69 76  th a non-negativ
29140 65 0a 2a 2a 20 6e 41 72 67 20 70 61 72 61 6d 65  e.** nArg parame
29150 74 65 72 20 69 73 20 61 20 62 65 74 74 65 72 20  ter is a better 
29160 6d 61 74 63 68 20 74 68 61 6e 20 61 20 66 75 6e  match than a fun
29170 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61  ction implementa
29180 74 69 6f 6e 20 77 69 74 68 0a 2a 2a 20 61 20 6e  tion with.** a n
29190 65 67 61 74 69 76 65 20 6e 41 72 67 2e 20 20 41  egative nArg.  A
291a0 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 72 65 20   function where 
291b0 74 68 65 20 70 72 65 66 65 72 72 65 64 20 74 65  the preferred te
291c0 78 74 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 6d  xt encoding.** m
291d0 61 74 63 68 65 73 20 74 68 65 20 64 61 74 61 62  atches the datab
291e0 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 20  ase encoding is 
291f0 61 20 62 65 74 74 65 72 0a 2a 2a 20 6d 61 74 63  a better.** matc
29200 68 20 74 68 61 6e 20 61 20 66 75 6e 63 74 69 6f  h than a functio
29210 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e 63 6f  n where the enco
29220 64 69 6e 67 20 69 73 20 64 69 66 66 65 72 65 6e  ding is differen
29230 74 2e 20 20 0a 2a 2a 20 41 20 66 75 6e 63 74 69  t.  .** A functi
29240 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e 63  on where the enc
29250 6f 64 69 6e 67 20 64 69 66 66 65 72 65 6e 63 65  oding difference
29260 20 69 73 20 62 65 74 77 65 65 6e 20 55 54 46 31   is between UTF1
29270 36 6c 65 20 61 6e 64 20 55 54 46 31 36 62 65 0a  6le and UTF16be.
29280 2a 2a 20 69 73 20 61 20 63 6c 6f 73 65 72 20 6d  ** is a closer m
29290 61 74 63 68 20 74 68 61 6e 20 61 20 66 75 6e 63  atch than a func
292a0 74 69 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65  tion where the e
292b0 6e 63 6f 64 69 6e 67 20 64 69 66 66 65 72 65 6e  ncoding differen
292c0 63 65 20 69 73 0a 2a 2a 20 62 65 74 77 65 65 6e  ce is.** between
292d0 20 55 54 46 38 20 61 6e 64 20 55 54 46 31 36 2e   UTF8 and UTF16.
292e0 0a 2a 2a 0a 2a 2a 20 42 75 69 6c 74 2d 69 6e 20  .**.** Built-in 
292f0 66 75 6e 63 74 69 6f 6e 73 20 6d 61 79 20 62 65  functions may be
29300 20 6f 76 65 72 6c 6f 61 64 65 64 20 62 79 20 6e   overloaded by n
29310 65 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64  ew application-d
29320 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73  efined functions
29330 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61  ..** The first a
29340 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
29350 65 64 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68  ed function with
29360 20 61 20 67 69 76 65 6e 20 6e 61 6d 65 20 6f 76   a given name ov
29370 65 72 72 69 64 65 73 20 61 6c 6c 0a 2a 2a 20 62  errides all.** b
29380 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e  uilt-in function
29390 73 20 69 6e 20 74 68 65 20 73 61 6d 65 20 5b 64  s in the same [d
293a0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
293b0 6f 6e 5d 20 77 69 74 68 20 74 68 65 20 73 61 6d  on] with the sam
293c0 65 20 6e 61 6d 65 2e 0a 2a 2a 20 53 75 62 73 65  e name..** Subse
293d0 71 75 65 6e 74 20 61 70 70 6c 69 63 61 74 69 6f  quent applicatio
293e0 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69  n-defined functi
293f0 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20  ons of the same 
29400 6e 61 6d 65 20 6f 6e 6c 79 20 6f 76 65 72 72 69  name only overri
29410 64 65 20 0a 2a 2a 20 70 72 69 6f 72 20 61 70 70  de .** prior app
29420 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64  lication-defined
29430 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20   functions that 
29440 61 72 65 20 61 6e 20 65 78 61 63 74 20 6d 61 74  are an exact mat
29450 63 68 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6e 75  ch for the.** nu
29460 6d 62 65 72 20 6f 66 20 70 61 72 61 6d 65 74 65  mber of paramete
29470 72 73 20 61 6e 64 20 70 72 65 66 65 72 72 65 64  rs and preferred
29480 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a   encoding..**.**
29490 20 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d   An application-
294a0 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
294b0 20 69 73 20 70 65 72 6d 69 74 74 65 64 20 74 6f   is permitted to
294c0 20 63 61 6c 6c 20 6f 74 68 65 72 0a 2a 2a 20 53   call other.** S
294d0 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 73  QLite interfaces
294e0 2e 20 20 48 6f 77 65 76 65 72 2c 20 73 75 63 68  .  However, such
294f0 20 63 61 6c 6c 73 20 6d 75 73 74 20 6e 6f 74 0a   calls must not.
29500 2a 2a 20 63 6c 6f 73 65 20 74 68 65 20 64 61 74  ** close the dat
29510 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
29520 20 6e 6f 72 20 66 69 6e 61 6c 69 7a 65 20 6f 72   nor finalize or
29530 20 72 65 73 65 74 20 74 68 65 20 70 72 65 70 61   reset the prepa
29540 72 65 64 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  red.** statement
29550 20 69 6e 20 77 68 69 63 68 20 74 68 65 20 66 75   in which the fu
29560 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e  nction is runnin
29570 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  g..**.** Require
29580 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 31 30  ments:.** [H1610
29590 33 5d 20 5b 48 31 36 31 30 36 5d 20 5b 48 31 36  3] [H16106] [H16
295a0 31 30 39 5d 20 5b 48 31 36 31 31 32 5d 20 5b 48  109] [H16112] [H
295b0 31 36 31 31 38 5d 20 5b 48 31 36 31 32 31 5d 20  16118] [H16121] 
295c0 5b 48 31 36 31 32 37 5d 0a 2a 2a 20 5b 48 31 36  [H16127].** [H16
295d0 31 33 30 5d 20 5b 48 31 36 31 33 33 5d 20 5b 48  130] [H16133] [H
295e0 31 36 31 33 36 5d 20 5b 48 31 36 31 33 39 5d 20  16136] [H16139] 
295f0 5b 48 31 36 31 34 32 5d 0a 2a 2f 0a 53 51 4c 49  [H16142].*/.SQLI
29600 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
29610 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
29620 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  on(.  sqlite3 *d
29630 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  b,.  const char 
29640 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a  *zFunctionName,.
29650 20 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e    int nArg,.  in
29660 74 20 65 54 65 78 74 52 65 70 2c 0a 20 20 76 6f  t eTextRep,.  vo
29670 69 64 20 2a 70 41 70 70 2c 0a 20 20 76 6f 69 64  id *pApp,.  void
29680 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65   (*xFunc)(sqlite
29690 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73  3_context*,int,s
296a0 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c  qlite3_value**),
296b0 0a 20 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29  .  void (*xStep)
296c0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
296d0 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61  *,int,sqlite3_va
296e0 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28  lue**),.  void (
296f0 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33  *xFinal)(sqlite3
29700 5f 63 6f 6e 74 65 78 74 2a 29 0a 29 3b 0a 53 51  _context*).);.SQ
29710 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
29720 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
29730 74 69 6f 6e 31 36 28 0a 20 20 73 71 6c 69 74 65  tion16(.  sqlite
29740 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 76  3 *db,.  const v
29750 6f 69 64 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61  oid *zFunctionNa
29760 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a  me,.  int nArg,.
29770 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 0a    int eTextRep,.
29780 20 20 76 6f 69 64 20 2a 70 41 70 70 2c 0a 20 20    void *pApp,.  
29790 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71  void (*xFunc)(sq
297a0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
297b0 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt,sqlite3_value
297c0 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 53  **),.  void (*xS
297d0 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  tep)(sqlite3_con
297e0 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  text*,int,sqlite
297f0 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f  3_value**),.  vo
29800 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c  id (*xFinal)(sql
29810 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0a 29  ite3_context*).)
29820 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
29830 46 3a 20 54 65 78 74 20 45 6e 63 6f 64 69 6e 67  F: Text Encoding
29840 73 20 7b 48 31 30 32 36 37 7d 20 3c 53 35 30 32  s {H10267} <S502
29850 30 30 3e 20 3c 48 31 36 31 30 30 3e 0a 2a 2a 0a  00> <H16100>.**.
29860 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e  ** These constan
29870 74 20 64 65 66 69 6e 65 20 69 6e 74 65 67 65 72  t define integer
29880 20 63 6f 64 65 73 20 74 68 61 74 20 72 65 70 72   codes that repr
29890 65 73 65 6e 74 20 74 68 65 20 76 61 72 69 6f 75  esent the variou
298a0 73 0a 2a 2a 20 74 65 78 74 20 65 6e 63 6f 64 69  s.** text encodi
298b0 6e 67 73 20 73 75 70 70 6f 72 74 65 64 20 62 79  ngs supported by
298c0 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 23 64 65 66   SQLite..*/.#def
298d0 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 38 20  ine SQLITE_UTF8 
298e0 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 65 66            1.#def
298f0 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36  ine SQLITE_UTF16
29900 4c 45 20 20 20 20 20 20 20 20 32 0a 23 64 65 66  LE        2.#def
29910 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36  ine SQLITE_UTF16
29920 42 45 20 20 20 20 20 20 20 20 33 0a 23 64 65 66  BE        3.#def
29930 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36  ine SQLITE_UTF16
29940 20 20 20 20 20 20 20 20 20 20 34 20 20 20 20 2f            4    /
29950 2a 20 55 73 65 20 6e 61 74 69 76 65 20 62 79 74  * Use native byt
29960 65 20 6f 72 64 65 72 20 2a 2f 0a 23 64 65 66 69  e order */.#defi
29970 6e 65 20 53 51 4c 49 54 45 5f 41 4e 59 20 20 20  ne SQLITE_ANY   
29980 20 20 20 20 20 20 20 20 20 35 20 20 20 20 2f 2a           5    /*
29990 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
299a0 66 75 6e 63 74 69 6f 6e 20 6f 6e 6c 79 20 2a 2f  function only */
299b0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
299c0 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 20 20 38  UTF16_ALIGNED  8
299d0 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 63      /* sqlite3_c
299e0 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 20  reate_collation 
299f0 6f 6e 6c 79 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43  only */../*.** C
29a00 41 50 49 33 52 45 46 3a 20 44 65 70 72 65 63 61  API3REF: Depreca
29a10 74 65 64 20 46 75 6e 63 74 69 6f 6e 73 0a 2a 2a  ted Functions.**
29a20 20 44 45 50 52 45 43 41 54 45 44 0a 2a 2a 0a 2a   DEPRECATED.**.*
29a30 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f 6e  * These function
29a40 73 20 61 72 65 20 5b 64 65 70 72 65 63 61 74 65  s are [deprecate
29a50 64 5d 2e 20 20 49 6e 20 6f 72 64 65 72 20 74 6f  d].  In order to
29a60 20 6d 61 69 6e 74 61 69 6e 0a 2a 2a 20 62 61 63   maintain.** bac
29a70 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69  kwards compatibi
29a80 6c 69 74 79 20 77 69 74 68 20 6f 6c 64 65 72 20  lity with older 
29a90 63 6f 64 65 2c 20 74 68 65 73 65 20 66 75 6e 63  code, these func
29aa0 74 69 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20 0a  tions continue .
29ab0 2a 2a 20 74 6f 20 62 65 20 73 75 70 70 6f 72 74  ** to be support
29ac0 65 64 2e 20 20 48 6f 77 65 76 65 72 2c 20 6e 65  ed.  However, ne
29ad0 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 73  w applications s
29ae0 68 6f 75 6c 64 20 61 76 6f 69 64 0a 2a 2a 20 74  hould avoid.** t
29af0 68 65 20 75 73 65 20 6f 66 20 74 68 65 73 65 20  he use of these 
29b00 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 6f 20 68  functions.  To h
29b10 65 6c 70 20 65 6e 63 6f 75 72 61 67 65 20 70 65  elp encourage pe
29b20 6f 70 6c 65 20 74 6f 20 61 76 6f 69 64 0a 2a 2a  ople to avoid.**
29b30 20 75 73 69 6e 67 20 74 68 65 73 65 20 66 75 6e   using these fun
29b40 63 74 69 6f 6e 73 2c 20 77 65 20 61 72 65 20 6e  ctions, we are n
29b50 6f 74 20 67 6f 69 6e 67 20 74 6f 20 74 65 6c 6c  ot going to tell
29b60 20 79 6f 75 20 77 68 61 74 20 74 68 65 79 20 64   you what they d
29b70 6f 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  o..*/.#ifndef SQ
29b80 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43  LITE_OMIT_DEPREC
29b90 41 54 45 44 0a 53 51 4c 49 54 45 5f 41 50 49 20  ATED.SQLITE_API 
29ba0 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45  SQLITE_DEPRECATE
29bb0 44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 67  D int sqlite3_ag
29bc0 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 73 71  gregate_count(sq
29bd0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b  lite3_context*);
29be0 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49  .SQLITE_API SQLI
29bf0 54 45 5f 44 45 50 52 45 43 41 54 45 44 20 69 6e  TE_DEPRECATED in
29c00 74 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72 65  t sqlite3_expire
29c10 64 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29  d(sqlite3_stmt*)
29c20 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c  ;.SQLITE_API SQL
29c30 49 54 45 5f 44 45 50 52 45 43 41 54 45 44 20 69  ITE_DEPRECATED i
29c40 6e 74 20 73 71 6c 69 74 65 33 5f 74 72 61 6e 73  nt sqlite3_trans
29c50 66 65 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c  fer_bindings(sql
29c60 69 74 65 33 5f 73 74 6d 74 2a 2c 20 73 71 6c 69  ite3_stmt*, sqli
29c70 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 53 51 4c 49  te3_stmt*);.SQLI
29c80 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45  TE_API SQLITE_DE
29c90 50 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c  PRECATED int sql
29ca0 69 74 65 33 5f 67 6c 6f 62 61 6c 5f 72 65 63 6f  ite3_global_reco
29cb0 76 65 72 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54  ver(void);.SQLIT
29cc0 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50  E_API SQLITE_DEP
29cd0 52 45 43 41 54 45 44 20 76 6f 69 64 20 73 71 6c  RECATED void sql
29ce0 69 74 65 33 5f 74 68 72 65 61 64 5f 63 6c 65 61  ite3_thread_clea
29cf0 6e 75 70 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54  nup(void);.SQLIT
29d00 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50  E_API SQLITE_DEP
29d10 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69  RECATED int sqli
29d20 74 65 33 5f 6d 65 6d 6f 72 79 5f 61 6c 61 72 6d  te3_memory_alarm
29d30 28 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c 73  (void(*)(void*,s
29d40 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74  qlite3_int64,int
29d50 29 2c 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f  ),void*,sqlite3_
29d60 69 6e 74 36 34 29 3b 0a 23 65 6e 64 69 66 0a 0a  int64);.#endif..
29d70 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
29d80 4f 62 74 61 69 6e 69 6e 67 20 53 51 4c 20 46 75  Obtaining SQL Fu
29d90 6e 63 74 69 6f 6e 20 50 61 72 61 6d 65 74 65 72  nction Parameter
29da0 20 56 61 6c 75 65 73 20 7b 48 31 35 31 30 30 7d   Values {H15100}
29db0 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S20200>.**.** 
29dc0 54 68 65 20 43 2d 6c 61 6e 67 75 61 67 65 20 69  The C-language i
29dd0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
29de0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61   SQL functions a
29df0 6e 64 20 61 67 67 72 65 67 61 74 65 73 20 75 73  nd aggregates us
29e00 65 73 0a 2a 2a 20 74 68 69 73 20 73 65 74 20 6f  es.** this set o
29e10 66 20 69 6e 74 65 72 66 61 63 65 20 72 6f 75 74  f interface rout
29e20 69 6e 65 73 20 74 6f 20 61 63 63 65 73 73 20 74  ines to access t
29e30 68 65 20 70 61 72 61 6d 65 74 65 72 20 76 61 6c  he parameter val
29e40 75 65 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 66 75  ues on.** the fu
29e50 6e 63 74 69 6f 6e 20 6f 72 20 61 67 67 72 65 67  nction or aggreg
29e60 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78  ate..**.** The x
29e70 46 75 6e 63 20 28 66 6f 72 20 73 63 61 6c 61 72  Func (for scalar
29e80 20 66 75 6e 63 74 69 6f 6e 73 29 20 6f 72 20 78   functions) or x
29e90 53 74 65 70 20 28 66 6f 72 20 61 67 67 72 65 67  Step (for aggreg
29ea0 61 74 65 73 29 20 70 61 72 61 6d 65 74 65 72 73  ates) parameters
29eb0 0a 2a 2a 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  .** to [sqlite3_
29ec0 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28  create_function(
29ed0 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  )] and [sqlite3_
29ee0 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31  create_function1
29ef0 36 28 29 5d 0a 2a 2a 20 64 65 66 69 6e 65 20 63  6()].** define c
29f00 61 6c 6c 62 61 63 6b 73 20 74 68 61 74 20 69 6d  allbacks that im
29f10 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 4c 20  plement the SQL 
29f20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 61 67  functions and ag
29f30 67 72 65 67 61 74 65 73 2e 0a 2a 2a 20 54 68 65  gregates..** The
29f40 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74   4th parameter t
29f50 6f 20 74 68 65 73 65 20 63 61 6c 6c 62 61 63 6b  o these callback
29f60 73 20 69 73 20 61 6e 20 61 72 72 61 79 20 6f 66  s is an array of
29f70 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20   pointers to.** 
29f80 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74  [protected sqlit
29f90 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74  e3_value] object
29fa0 73 2e 20 20 54 68 65 72 65 20 69 73 20 6f 6e 65  s.  There is one
29fb0 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d   [sqlite3_value]
29fc0 20 6f 62 6a 65 63 74 20 66 6f 72 0a 2a 2a 20 65   object for.** e
29fd0 61 63 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f  ach parameter to
29fe0 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f   the SQL functio
29ff0 6e 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e  n.  These routin
2a000 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 0a 2a  es are used to.*
2a010 2a 20 65 78 74 72 61 63 74 20 76 61 6c 75 65 73  * extract values
2a020 20 66 72 6f 6d 20 74 68 65 20 5b 73 71 6c 69 74   from the [sqlit
2a030 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74  e3_value] object
2a040 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72  s..**.** These r
2a050 6f 75 74 69 6e 65 73 20 77 6f 72 6b 20 6f 6e 6c  outines work onl
2a060 79 20 77 69 74 68 20 5b 70 72 6f 74 65 63 74 65  y with [protecte
2a070 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d  d sqlite3_value]
2a080 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 41 6e 79   objects..** Any
2a090 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20   attempt to use 
2a0a0 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6f  these routines o
2a0b0 6e 20 61 6e 20 5b 75 6e 70 72 6f 74 65 63 74 65  n an [unprotecte
2a0c0 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d  d sqlite3_value]
2a0d0 0a 2a 2a 20 6f 62 6a 65 63 74 20 72 65 73 75 6c  .** object resul
2a0e0 74 73 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 20  ts in undefined 
2a0f0 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20  behavior..**.** 
2a100 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 77  These routines w
2a110 6f 72 6b 20 6a 75 73 74 20 6c 69 6b 65 20 74 68  ork just like th
2a120 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
2a130 5b 63 6f 6c 75 6d 6e 20 61 63 63 65 73 73 20 66  [column access f
2a140 75 6e 63 74 69 6f 6e 73 5d 0a 2a 2a 20 65 78 63  unctions].** exc
2a150 65 70 74 20 74 68 61 74 20 20 74 68 65 73 65 20  ept that  these 
2a160 72 6f 75 74 69 6e 65 73 20 74 61 6b 65 20 61 20  routines take a 
2a170 73 69 6e 67 6c 65 20 5b 70 72 6f 74 65 63 74 65  single [protecte
2a180 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d  d sqlite3_value]
2a190 20 6f 62 6a 65 63 74 0a 2a 2a 20 70 6f 69 6e 74   object.** point
2a1a0 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20  er instead of a 
2a1b0 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 5d 20  [sqlite3_stmt*] 
2a1c0 70 6f 69 6e 74 65 72 20 61 6e 64 20 61 6e 20 69  pointer and an i
2a1d0 6e 74 65 67 65 72 20 63 6f 6c 75 6d 6e 20 6e 75  nteger column nu
2a1e0 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  mber..**.** The 
2a1f0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
2a200 78 74 31 36 28 29 20 69 6e 74 65 72 66 61 63 65  xt16() interface
2a210 20 65 78 74 72 61 63 74 73 20 61 20 55 54 46 2d   extracts a UTF-
2a220 31 36 20 73 74 72 69 6e 67 0a 2a 2a 20 69 6e 20  16 string.** in 
2a230 74 68 65 20 6e 61 74 69 76 65 20 62 79 74 65 2d  the native byte-
2a240 6f 72 64 65 72 20 6f 66 20 74 68 65 20 68 6f 73  order of the hos
2a250 74 20 6d 61 63 68 69 6e 65 2e 20 20 54 68 65 0a  t machine.  The.
2a260 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ** sqlite3_value
2a270 5f 74 65 78 74 31 36 62 65 28 29 20 61 6e 64 20  _text16be() and 
2a280 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
2a290 78 74 31 36 6c 65 28 29 20 69 6e 74 65 72 66 61  xt16le() interfa
2a2a0 63 65 73 0a 2a 2a 20 65 78 74 72 61 63 74 20 55  ces.** extract U
2a2b0 54 46 2d 31 36 20 73 74 72 69 6e 67 73 20 61 73  TF-16 strings as
2a2c0 20 62 69 67 2d 65 6e 64 69 61 6e 20 61 6e 64 20   big-endian and 
2a2d0 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 72 65  little-endian re
2a2e0 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 0a 2a  spectively..**.*
2a2f0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 76 61  * The sqlite3_va
2a300 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65  lue_numeric_type
2a310 28 29 20 69 6e 74 65 72 66 61 63 65 20 61 74 74  () interface att
2a320 65 6d 70 74 73 20 74 6f 20 61 70 70 6c 79 0a 2a  empts to apply.*
2a330 2a 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e 69  * numeric affini
2a340 74 79 20 74 6f 20 74 68 65 20 76 61 6c 75 65 2e  ty to the value.
2a350 20 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61    This means tha
2a360 74 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 0a  t an attempt is.
2a370 2a 2a 20 6d 61 64 65 20 74 6f 20 63 6f 6e 76 65  ** made to conve
2a380 72 74 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20  rt the value to 
2a390 61 6e 20 69 6e 74 65 67 65 72 20 6f 72 20 66 6c  an integer or fl
2a3a0 6f 61 74 69 6e 67 20 70 6f 69 6e 74 2e 20 20 49  oating point.  I
2a3b0 66 0a 2a 2a 20 73 75 63 68 20 61 20 63 6f 6e 76  f.** such a conv
2a3c0 65 72 73 69 6f 6e 20 69 73 20 70 6f 73 73 69 62  ersion is possib
2a3d0 6c 65 20 77 69 74 68 6f 75 74 20 6c 6f 73 73 20  le without loss 
2a3e0 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 28  of information (
2a3f0 69 6e 20 6f 74 68 65 72 0a 2a 2a 20 77 6f 72 64  in other.** word
2a400 73 2c 20 69 66 20 74 68 65 20 76 61 6c 75 65 20  s, if the value 
2a410 69 73 20 61 20 73 74 72 69 6e 67 20 74 68 61 74  is a string that
2a420 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 6e 75   looks like a nu
2a430 6d 62 65 72 29 0a 2a 2a 20 74 68 65 6e 20 74 68  mber).** then th
2a440 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20  e conversion is 
2a450 70 65 72 66 6f 72 6d 65 64 2e 20 20 4f 74 68 65  performed.  Othe
2a460 72 77 69 73 65 20 6e 6f 20 63 6f 6e 76 65 72 73  rwise no convers
2a470 69 6f 6e 20 6f 63 63 75 72 73 2e 0a 2a 2a 20 54  ion occurs..** T
2a480 68 65 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47  he [SQLITE_INTEG
2a490 45 52 20 7c 20 64 61 74 61 74 79 70 65 5d 20 61  ER | datatype] a
2a4a0 66 74 65 72 20 63 6f 6e 76 65 72 73 69 6f 6e 20  fter conversion 
2a4b0 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
2a4c0 2a 2a 20 50 6c 65 61 73 65 20 70 61 79 20 70 61  ** Please pay pa
2a4d0 72 74 69 63 75 6c 61 72 20 61 74 74 65 6e 74 69  rticular attenti
2a4e0 6f 6e 20 74 6f 20 74 68 65 20 66 61 63 74 20 74  on to the fact t
2a4f0 68 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20  hat the pointer 
2a500 72 65 74 75 72 6e 65 64 0a 2a 2a 20 66 72 6f 6d  returned.** from
2a510 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   [sqlite3_value_
2a520 62 6c 6f 62 28 29 5d 2c 20 5b 73 71 6c 69 74 65  blob()], [sqlite
2a530 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 5d 2c  3_value_text()],
2a540 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   or.** [sqlite3_
2a550 76 61 6c 75 65 5f 74 65 78 74 31 36 28 29 5d 20  value_text16()] 
2a560 63 61 6e 20 62 65 20 69 6e 76 61 6c 69 64 61 74  can be invalidat
2a570 65 64 20 62 79 20 61 20 73 75 62 73 65 71 75 65  ed by a subseque
2a580 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73  nt call to.** [s
2a590 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
2a5a0 65 73 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f  es()], [sqlite3_
2a5b0 76 61 6c 75 65 5f 62 79 74 65 73 31 36 28 29 5d  value_bytes16()]
2a5c0 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  , [sqlite3_value
2a5d0 5f 74 65 78 74 28 29 5d 2c 0a 2a 2a 20 6f 72 20  _text()],.** or 
2a5e0 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  [sqlite3_value_t
2a5f0 65 78 74 31 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  ext16()]..**.** 
2a600 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d  These routines m
2a610 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72  ust be called fr
2a620 6f 6d 20 74 68 65 20 73 61 6d 65 20 74 68 72 65  om the same thre
2a630 61 64 20 61 73 0a 2a 2a 20 74 68 65 20 53 51 4c  ad as.** the SQL
2a640 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 73   function that s
2a650 75 70 70 6c 69 65 64 20 74 68 65 20 5b 73 71 6c  upplied the [sql
2a660 69 74 65 33 5f 76 61 6c 75 65 2a 5d 20 70 61 72  ite3_value*] par
2a670 61 6d 65 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 52  ameters..**.** R
2a680 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
2a690 5b 48 31 35 31 30 33 5d 20 5b 48 31 35 31 30 36  [H15103] [H15106
2a6a0 5d 20 5b 48 31 35 31 30 39 5d 20 5b 48 31 35 31  ] [H15109] [H151
2a6b0 31 32 5d 20 5b 48 31 35 31 31 35 5d 20 5b 48 31  12] [H15115] [H1
2a6c0 35 31 31 38 5d 20 5b 48 31 35 31 32 31 5d 20 5b  5118] [H15121] [
2a6d0 48 31 35 31 32 34 5d 0a 2a 2a 20 5b 48 31 35 31  H15124].** [H151
2a6e0 32 37 5d 20 5b 48 31 35 31 33 30 5d 20 5b 48 31  27] [H15130] [H1
2a6f0 35 31 33 33 5d 20 5b 48 31 35 31 33 36 5d 0a 2a  5133] [H15136].*
2a700 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  /.SQLITE_API con
2a710 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
2a720 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 73 71 6c 69  _value_blob(sqli
2a730 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c  te3_value*);.SQL
2a740 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
2a750 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
2a760 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
2a770 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2a780 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
2a790 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f 76 61  tes16(sqlite3_va
2a7a0 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  lue*);.SQLITE_AP
2a7b0 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 33  I double sqlite3
2a7c0 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 73 71  _value_double(sq
2a7d0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53  lite3_value*);.S
2a7e0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
2a7f0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28  lite3_value_int(
2a800 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
2a810 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
2a820 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65  te3_int64 sqlite
2a830 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 73 71  3_value_int64(sq
2a840 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53  lite3_value*);.S
2a850 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
2a860 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73  unsigned char *s
2a870 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
2a880 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  t(sqlite3_value*
2a890 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  );.SQLITE_API co
2a8a0 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
2a8b0 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 73  3_value_text16(s
2a8c0 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a  qlite3_value*);.
2a8d0 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
2a8e0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76   void *sqlite3_v
2a8f0 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 73 71  alue_text16le(sq
2a900 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53  lite3_value*);.S
2a910 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
2a920 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61  void *sqlite3_va
2a930 6c 75 65 5f 74 65 78 74 31 36 62 65 28 73 71 6c  lue_text16be(sql
2a940 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51  ite3_value*);.SQ
2a950 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
2a960 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
2a970 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
2a980 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2a990 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75  sqlite3_value_nu
2a9a0 6d 65 72 69 63 5f 74 79 70 65 28 73 71 6c 69 74  meric_type(sqlit
2a9b0 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 0a 2f 2a 0a  e3_value*);../*.
2a9c0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 62 74  ** CAPI3REF: Obt
2a9d0 61 69 6e 20 41 67 67 72 65 67 61 74 65 20 46 75  ain Aggregate Fu
2a9e0 6e 63 74 69 6f 6e 20 43 6f 6e 74 65 78 74 20 7b  nction Context {
2a9f0 48 31 36 32 31 30 7d 20 3c 53 32 30 32 30 30 3e  H16210} <S20200>
2aa00 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65  .**.** The imple
2aa10 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 67 67  mentation of agg
2aa20 72 65 67 61 74 65 20 53 51 4c 20 66 75 6e 63 74  regate SQL funct
2aa30 69 6f 6e 73 20 75 73 65 20 74 68 69 73 20 72 6f  ions use this ro
2aa40 75 74 69 6e 65 20 74 6f 20 61 6c 6c 6f 63 61 74  utine to allocat
2aa50 65 0a 2a 2a 20 61 20 73 74 72 75 63 74 75 72 65  e.** a structure
2aa60 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 65   for storing the
2aa70 69 72 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20  ir state..**.** 
2aa80 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74  The first time t
2aa90 68 65 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65  he sqlite3_aggre
2aaa0 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 29 20 72  gate_context() r
2aab0 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
2aac0 20 66 6f 72 20 61 0a 2a 2a 20 70 61 72 74 69 63   for a.** partic
2aad0 75 6c 61 72 20 61 67 67 72 65 67 61 74 65 2c 20  ular aggregate, 
2aae0 53 51 4c 69 74 65 20 61 6c 6c 6f 63 61 74 65 73  SQLite allocates
2aaf0 20 6e 42 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72   nBytes of memor
2ab00 79 2c 20 7a 65 72 6f 65 73 20 6f 75 74 20 74 68  y, zeroes out th
2ab10 61 74 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 61 6e  at.** memory, an
2ab20 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e  d returns a poin
2ab30 74 65 72 20 74 6f 20 69 74 2e 20 4f 6e 20 73 65  ter to it. On se
2ab40 63 6f 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75  cond and subsequ
2ab50 65 6e 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20  ent calls to.** 
2ab60 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
2ab70 65 5f 63 6f 6e 74 65 78 74 28 29 20 66 6f 72 20  e_context() for 
2ab80 74 68 65 20 73 61 6d 65 20 61 67 67 72 65 67 61  the same aggrega
2ab90 74 65 20 66 75 6e 63 74 69 6f 6e 20 69 6e 64 65  te function inde
2aba0 78 2c 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 62  x,.** the same b
2abb0 75 66 66 65 72 20 69 73 20 72 65 74 75 72 6e 65  uffer is returne
2abc0 64 2e 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74  d. The implement
2abd0 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 61 67 67  ation of the agg
2abe0 72 65 67 61 74 65 20 63 61 6e 20 75 73 65 0a 2a  regate can use.*
2abf0 2a 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 62  * the returned b
2ac00 75 66 66 65 72 20 74 6f 20 61 63 63 75 6d 75 6c  uffer to accumul
2ac10 61 74 65 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20  ate data..**.** 
2ac20 53 51 4c 69 74 65 20 61 75 74 6f 6d 61 74 69 63  SQLite automatic
2ac30 61 6c 6c 79 20 66 72 65 65 73 20 74 68 65 20 61  ally frees the a
2ac40 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 20  llocated buffer 
2ac50 77 68 65 6e 20 74 68 65 20 61 67 67 72 65 67 61  when the aggrega
2ac60 74 65 0a 2a 2a 20 71 75 65 72 79 20 63 6f 6e 63  te.** query conc
2ac70 6c 75 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ludes..**.** The
2ac80 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72   first parameter
2ac90 20 73 68 6f 75 6c 64 20 62 65 20 61 20 63 6f 70   should be a cop
2aca0 79 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71 6c  y of the.** [sql
2acb0 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 7c 20 53  ite3_context | S
2acc0 51 4c 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74  QL function cont
2acd0 65 78 74 5d 20 74 68 61 74 20 69 73 20 74 68 65  ext] that is the
2ace0 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72   first parameter
2acf0 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 62  .** to the callb
2ad00 61 63 6b 20 72 6f 75 74 69 6e 65 20 74 68 61 74  ack routine that
2ad10 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20   implements the 
2ad20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
2ad30 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  on..**.** This r
2ad40 6f 75 74 69 6e 65 20 6d 75 73 74 20 62 65 20 63  outine must be c
2ad50 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 73  alled from the s
2ad60 61 6d 65 20 74 68 72 65 61 64 20 69 6e 20 77 68  ame thread in wh
2ad70 69 63 68 0a 2a 2a 20 74 68 65 20 61 67 67 72 65  ich.** the aggre
2ad80 67 61 74 65 20 53 51 4c 20 66 75 6e 63 74 69 6f  gate SQL functio
2ad90 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a  n is running..**
2ada0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
2adb0 3a 0a 2a 2a 20 5b 48 31 36 32 31 31 5d 20 5b 48  :.** [H16211] [H
2adc0 31 36 32 31 33 5d 20 5b 48 31 36 32 31 35 5d 20  16213] [H16215] 
2add0 5b 48 31 36 32 31 37 5d 0a 2a 2f 0a 53 51 4c 49  [H16217].*/.SQLI
2ade0 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
2adf0 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63  ite3_aggregate_c
2ae00 6f 6e 74 65 78 74 28 73 71 6c 69 74 65 33 5f 63  ontext(sqlite3_c
2ae10 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 20 6e 42 79  ontext*, int nBy
2ae20 74 65 73 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  tes);../*.** CAP
2ae30 49 33 52 45 46 3a 20 55 73 65 72 20 44 61 74 61  I3REF: User Data
2ae40 20 46 6f 72 20 46 75 6e 63 74 69 6f 6e 73 20 7b   For Functions {
2ae50 48 31 36 32 34 30 7d 20 3c 53 32 30 32 30 30 3e  H16240} <S20200>
2ae60 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
2ae70 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 20 69  e3_user_data() i
2ae80 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73  nterface returns
2ae90 20 61 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68   a copy of.** th
2aea0 65 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 77  e pointer that w
2aeb0 61 73 20 74 68 65 20 70 55 73 65 72 44 61 74 61  as the pUserData
2aec0 20 70 61 72 61 6d 65 74 65 72 20 28 74 68 65 20   parameter (the 
2aed0 35 74 68 20 70 61 72 61 6d 65 74 65 72 29 0a 2a  5th parameter).*
2aee0 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65  * of the [sqlite
2aef0 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
2af00 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c  n()].** and [sql
2af10 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
2af20 74 69 6f 6e 31 36 28 29 5d 20 72 6f 75 74 69 6e  tion16()] routin
2af30 65 73 20 74 68 61 74 20 6f 72 69 67 69 6e 61 6c  es that original
2af40 6c 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64  ly.** registered
2af50 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
2af60 20 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f   defined functio
2af70 6e 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54  n. {END}.**.** T
2af80 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 74  his routine must
2af90 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20   be called from 
2afa0 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20  the same thread 
2afb0 69 6e 20 77 68 69 63 68 0a 2a 2a 20 74 68 65 20  in which.** the 
2afc0 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69  application-defi
2afd0 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  ned function is 
2afe0 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52  running..**.** R
2aff0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
2b000 5b 48 31 36 32 34 33 5d 0a 2a 2f 0a 53 51 4c 49  [H16243].*/.SQLI
2b010 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
2b020 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 73  ite3_user_data(s
2b030 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29  qlite3_context*)
2b040 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
2b050 46 3a 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e  F: Database Conn
2b060 65 63 74 69 6f 6e 20 46 6f 72 20 46 75 6e 63 74  ection For Funct
2b070 69 6f 6e 73 20 7b 48 31 36 32 35 30 7d 20 3c 53  ions {H16250} <S
2b080 36 30 36 30 30 3e 3c 53 32 30 32 30 30 3e 0a 2a  60600><S20200>.*
2b090 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
2b0a0 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
2b0b0 6c 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 72  le() interface r
2b0c0 65 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66  eturns a copy of
2b0d0 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20  .** the pointer 
2b0e0 74 6f 20 74 68 65 20 5b 64 61 74 61 62 61 73 65  to the [database
2b0f0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 74 68   connection] (th
2b100 65 20 31 73 74 20 70 61 72 61 6d 65 74 65 72 29  e 1st parameter)
2b110 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c 69  .** of the [sqli
2b120 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
2b130 69 6f 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73  ion()].** and [s
2b140 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
2b150 6e 63 74 69 6f 6e 31 36 28 29 5d 20 72 6f 75 74  nction16()] rout
2b160 69 6e 65 73 20 74 68 61 74 20 6f 72 69 67 69 6e  ines that origin
2b170 61 6c 6c 79 0a 2a 2a 20 72 65 67 69 73 74 65 72  ally.** register
2b180 65 64 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  ed the applicati
2b190 6f 6e 20 64 65 66 69 6e 65 64 20 66 75 6e 63 74  on defined funct
2b1a0 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  ion..**.** Requi
2b1b0 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36  rements:.** [H16
2b1c0 32 35 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  253].*/.SQLITE_A
2b1d0 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69  PI sqlite3 *sqli
2b1e0 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68  te3_context_db_h
2b1f0 61 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f  andle(sqlite3_co
2b200 6e 74 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  ntext*);../*.** 
2b210 43 41 50 49 33 52 45 46 3a 20 46 75 6e 63 74 69  CAPI3REF: Functi
2b220 6f 6e 20 41 75 78 69 6c 69 61 72 79 20 44 61 74  on Auxiliary Dat
2b230 61 20 7b 48 31 36 32 37 30 7d 20 3c 53 32 30 32  a {H16270} <S202
2b240 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f  00>.**.** The fo
2b250 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 66 75 6e 63  llowing two func
2b260 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 75 73 65  tions may be use
2b270 64 20 62 79 20 73 63 61 6c 61 72 20 53 51 4c 20  d by scalar SQL 
2b280 66 75 6e 63 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20  functions to.** 
2b290 61 73 73 6f 63 69 61 74 65 20 6d 65 74 61 64 61  associate metada
2b2a0 74 61 20 77 69 74 68 20 61 72 67 75 6d 65 6e 74  ta with argument
2b2b0 20 76 61 6c 75 65 73 2e 20 49 66 20 74 68 65 20   values. If the 
2b2c0 73 61 6d 65 20 76 61 6c 75 65 20 69 73 20 70 61  same value is pa
2b2d0 73 73 65 64 20 74 6f 0a 2a 2a 20 6d 75 6c 74 69  ssed to.** multi
2b2e0 70 6c 65 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20  ple invocations 
2b2f0 6f 66 20 74 68 65 20 73 61 6d 65 20 53 51 4c 20  of the same SQL 
2b300 66 75 6e 63 74 69 6f 6e 20 64 75 72 69 6e 67 20  function during 
2b310 71 75 65 72 79 20 65 78 65 63 75 74 69 6f 6e 2c  query execution,
2b320 20 75 6e 64 65 72 0a 2a 2a 20 73 6f 6d 65 20 63   under.** some c
2b330 69 72 63 75 6d 73 74 61 6e 63 65 73 20 74 68 65  ircumstances the
2b340 20 61 73 73 6f 63 69 61 74 65 64 20 6d 65 74 61   associated meta
2b350 64 61 74 61 20 6d 61 79 20 62 65 20 70 72 65 73  data may be pres
2b360 65 72 76 65 64 2e 20 54 68 69 73 20 6d 61 79 0a  erved. This may.
2b370 2a 2a 20 62 65 20 75 73 65 64 2c 20 66 6f 72 20  ** be used, for 
2b380 65 78 61 6d 70 6c 65 2c 20 74 6f 20 61 64 64 20  example, to add 
2b390 61 20 72 65 67 75 6c 61 72 2d 65 78 70 72 65 73  a regular-expres
2b3a0 73 69 6f 6e 20 6d 61 74 63 68 69 6e 67 20 73 63  sion matching sc
2b3b0 61 6c 61 72 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  alar.** function
2b3c0 2e 20 54 68 65 20 63 6f 6d 70 69 6c 65 64 20 76  . The compiled v
2b3d0 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 72 65  ersion of the re
2b3e0 67 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e  gular expression
2b3f0 20 69 73 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a   is stored as.**
2b400 20 6d 65 74 61 64 61 74 61 20 61 73 73 6f 63 69   metadata associ
2b410 61 74 65 64 20 77 69 74 68 20 74 68 65 20 53 51  ated with the SQ
2b420 4c 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61  L value passed a
2b430 73 20 74 68 65 20 72 65 67 75 6c 61 72 20 65 78  s the regular ex
2b440 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 70 61 74 74  pression.** patt
2b450 65 72 6e 2e 20 20 54 68 65 20 63 6f 6d 70 69 6c  ern.  The compil
2b460 65 64 20 72 65 67 75 6c 61 72 20 65 78 70 72 65  ed regular expre
2b470 73 73 69 6f 6e 20 63 61 6e 20 62 65 20 72 65 75  ssion can be reu
2b480 73 65 64 20 6f 6e 20 6d 75 6c 74 69 70 6c 65 0a  sed on multiple.
2b490 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6f  ** invocations o
2b4a0 66 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74  f the same funct
2b4b0 69 6f 6e 20 73 6f 20 74 68 61 74 20 74 68 65 20  ion so that the 
2b4c0 6f 72 69 67 69 6e 61 6c 20 70 61 74 74 65 72 6e  original pattern
2b4d0 20 73 74 72 69 6e 67 0a 2a 2a 20 64 6f 65 73 20   string.** does 
2b4e0 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 72  not need to be r
2b4f0 65 63 6f 6d 70 69 6c 65 64 20 6f 6e 20 65 61 63  ecompiled on eac
2b500 68 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 0a 2a 2a  h invocation..**
2b510 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
2b520 67 65 74 5f 61 75 78 64 61 74 61 28 29 20 69 6e  get_auxdata() in
2b530 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20  terface returns 
2b540 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
2b550 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 61 73 73   metadata.** ass
2b560 6f 63 69 61 74 65 64 20 62 79 20 74 68 65 20 73  ociated by the s
2b570 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61  qlite3_set_auxda
2b580 74 61 28 29 20 66 75 6e 63 74 69 6f 6e 20 77 69  ta() function wi
2b590 74 68 20 74 68 65 20 4e 74 68 20 61 72 67 75 6d  th the Nth argum
2b5a0 65 6e 74 0a 2a 2a 20 76 61 6c 75 65 20 74 6f 20  ent.** value to 
2b5b0 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d  the application-
2b5c0 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
2b5d0 2e 20 49 66 20 6e 6f 20 6d 65 74 61 64 61 74 61  . If no metadata
2b5e0 20 68 61 73 20 62 65 65 6e 20 65 76 65 72 0a 2a   has been ever.*
2b5f0 2a 20 62 65 65 6e 20 73 65 74 20 66 6f 72 20 74  * been set for t
2b600 68 65 20 4e 74 68 20 61 72 67 75 6d 65 6e 74 20  he Nth argument 
2b610 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2c  of the function,
2b620 20 6f 72 20 69 66 20 74 68 65 20 63 6f 72 72 65   or if the corre
2b630 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 66 75 6e 63  sponding.** func
2b640 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 68  tion parameter h
2b650 61 73 20 63 68 61 6e 67 65 64 20 73 69 6e 63 65  as changed since
2b660 20 74 68 65 20 6d 65 74 61 2d 64 61 74 61 20 77   the meta-data w
2b670 61 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 6e 20  as set,.** then 
2b680 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64  sqlite3_get_auxd
2b690 61 74 61 28 29 20 72 65 74 75 72 6e 73 20 61 20  ata() returns a 
2b6a0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a  NULL pointer..**
2b6b0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
2b6c0 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 69 6e  set_auxdata() in
2b6d0 74 65 72 66 61 63 65 20 73 61 76 65 73 20 74 68  terface saves th
2b6e0 65 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 70 6f  e metadata.** po
2b6f0 69 6e 74 65 64 20 74 6f 20 62 79 20 69 74 73 20  inted to by its 
2b700 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 61 73  3rd parameter as
2b710 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 66 6f   the metadata fo
2b720 72 20 74 68 65 20 4e 2d 74 68 0a 2a 2a 20 61 72  r the N-th.** ar
2b730 67 75 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 70  gument of the ap
2b740 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
2b750 64 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 75 62  d function.  Sub
2b760 73 65 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c 73  sequent.** calls
2b770 20 74 6f 20 73 71 6c 69 74 65 33 5f 67 65 74 5f   to sqlite3_get_
2b780 61 75 78 64 61 74 61 28 29 20 6d 69 67 68 74 20  auxdata() might 
2b790 72 65 74 75 72 6e 20 74 68 69 73 20 64 61 74 61  return this data
2b7a0 2c 20 69 66 20 69 74 20 68 61 73 0a 2a 2a 20 6e  , if it has.** n
2b7b0 6f 74 20 62 65 65 6e 20 64 65 73 74 72 6f 79 65  ot been destroye
2b7c0 64 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 6e  d..** If it is n
2b7d0 6f 74 20 4e 55 4c 4c 2c 20 53 51 4c 69 74 65 20  ot NULL, SQLite 
2b7e0 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20  will invoke the 
2b7f0 64 65 73 74 72 75 63 74 6f 72 0a 2a 2a 20 66 75  destructor.** fu
2b800 6e 63 74 69 6f 6e 20 67 69 76 65 6e 20 62 79 20  nction given by 
2b810 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65  the 4th paramete
2b820 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 65 74  r to sqlite3_set
2b830 5f 61 75 78 64 61 74 61 28 29 20 6f 6e 0a 2a 2a  _auxdata() on.**
2b840 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 77 68   the metadata wh
2b850 65 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  en the correspon
2b860 64 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 70 61  ding function pa
2b870 72 61 6d 65 74 65 72 20 63 68 61 6e 67 65 73 0a  rameter changes.
2b880 2a 2a 20 6f 72 20 77 68 65 6e 20 74 68 65 20 53  ** or when the S
2b890 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d  QL statement com
2b8a0 70 6c 65 74 65 73 2c 20 77 68 69 63 68 65 76 65  pletes, whicheve
2b8b0 72 20 63 6f 6d 65 73 20 66 69 72 73 74 2e 0a 2a  r comes first..*
2b8c0 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69 73 20 66  *.** SQLite is f
2b8d0 72 65 65 20 74 6f 20 63 61 6c 6c 20 74 68 65 20  ree to call the 
2b8e0 64 65 73 74 72 75 63 74 6f 72 20 61 6e 64 20 64  destructor and d
2b8f0 72 6f 70 20 6d 65 74 61 64 61 74 61 20 6f 6e 20  rop metadata on 
2b900 61 6e 79 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  any.** parameter
2b910 20 6f 66 20 61 6e 79 20 66 75 6e 63 74 69 6f 6e   of any function
2b920 20 61 74 20 61 6e 79 20 74 69 6d 65 2e 20 20 54   at any time.  T
2b930 68 65 20 6f 6e 6c 79 20 67 75 61 72 61 6e 74 65  he only guarante
2b940 65 20 69 73 20 74 68 61 74 0a 2a 2a 20 74 68 65  e is that.** the
2b950 20 64 65 73 74 72 75 63 74 6f 72 20 77 69 6c 6c   destructor will
2b960 20 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72   be called befor
2b970 65 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 69  e the metadata i
2b980 73 20 64 72 6f 70 70 65 64 2e 0a 2a 2a 0a 2a 2a  s dropped..**.**
2b990 20 49 6e 20 70 72 61 63 74 69 63 65 2c 20 6d 65   In practice, me
2b9a0 74 61 64 61 74 61 20 69 73 20 70 72 65 73 65 72  tadata is preser
2b9b0 76 65 64 20 62 65 74 77 65 65 6e 20 66 75 6e 63  ved between func
2b9c0 74 69 6f 6e 20 63 61 6c 6c 73 20 66 6f 72 0a 2a  tion calls for.*
2b9d0 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68  * expressions th
2b9e0 61 74 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 20  at are constant 
2b9f0 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2e  at compile time.
2ba00 20 54 68 69 73 20 69 6e 63 6c 75 64 65 73 20 6c   This includes l
2ba10 69 74 65 72 61 6c 0a 2a 2a 20 76 61 6c 75 65 73  iteral.** values
2ba20 20 61 6e 64 20 53 51 4c 20 76 61 72 69 61 62 6c   and SQL variabl
2ba30 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  es..**.** These 
2ba40 72 6f 75 74 69 6e 65 73 20 6d 75 73 74 20 62 65  routines must be
2ba50 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65   called from the
2ba60 20 73 61 6d 65 20 74 68 72 65 61 64 20 69 6e 20   same thread in 
2ba70 77 68 69 63 68 0a 2a 2a 20 74 68 65 20 53 51 4c  which.** the SQL
2ba80 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e   function is run
2ba90 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  ning..**.** Requ
2baa0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
2bab0 36 32 37 32 5d 20 5b 48 31 36 32 37 34 5d 20 5b  6272] [H16274] [
2bac0 48 31 36 32 37 36 5d 20 5b 48 31 36 32 37 37 5d  H16276] [H16277]
2bad0 20 5b 48 31 36 32 37 38 5d 20 5b 48 31 36 32 37   [H16278] [H1627
2bae0 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  9].*/.SQLITE_API
2baf0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 67   void *sqlite3_g
2bb00 65 74 5f 61 75 78 64 61 74 61 28 73 71 6c 69 74  et_auxdata(sqlit
2bb10 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74  e3_context*, int
2bb20 20 4e 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20   N);.SQLITE_API 
2bb30 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 73 65 74  void sqlite3_set
2bb40 5f 61 75 78 64 61 74 61 28 73 71 6c 69 74 65 33  _auxdata(sqlite3
2bb50 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 20 4e  _context*, int N
2bb60 2c 20 76 6f 69 64 2a 2c 20 76 6f 69 64 20 28 2a  , void*, void (*
2bb70 29 28 76 6f 69 64 2a 29 29 3b 0a 0a 0a 2f 2a 0a  )(void*));.../*.
2bb80 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e  ** CAPI3REF: Con
2bb90 73 74 61 6e 74 73 20 44 65 66 69 6e 69 6e 67 20  stants Defining 
2bba0 53 70 65 63 69 61 6c 20 44 65 73 74 72 75 63 74  Special Destruct
2bbb0 6f 72 20 42 65 68 61 76 69 6f 72 20 7b 48 31 30  or Behavior {H10
2bbc0 32 38 30 7d 20 3c 53 33 30 31 30 30 3e 0a 2a 2a  280} <S30100>.**
2bbd0 0a 2a 2a 20 54 68 65 73 65 20 61 72 65 20 73 70  .** These are sp
2bbe0 65 63 69 61 6c 20 76 61 6c 75 65 73 20 66 6f 72  ecial values for
2bbf0 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20   the destructor 
2bc00 74 68 61 74 20 69 73 20 70 61 73 73 65 64 20 69  that is passed i
2bc10 6e 20 61 73 20 74 68 65 0a 2a 2a 20 66 69 6e 61  n as the.** fina
2bc20 6c 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 72 6f  l argument to ro
2bc30 75 74 69 6e 65 73 20 6c 69 6b 65 20 5b 73 71 6c  utines like [sql
2bc40 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62  ite3_result_blob
2bc50 28 29 5d 2e 20 20 49 66 20 74 68 65 20 64 65 73  ()].  If the des
2bc60 74 72 75 63 74 6f 72 0a 2a 2a 20 61 72 67 75 6d  tructor.** argum
2bc70 65 6e 74 20 69 73 20 53 51 4c 49 54 45 5f 53 54  ent is SQLITE_ST
2bc80 41 54 49 43 2c 20 69 74 20 6d 65 61 6e 73 20 74  ATIC, it means t
2bc90 68 61 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  hat the content 
2bca0 70 6f 69 6e 74 65 72 20 69 73 20 63 6f 6e 73 74  pointer is const
2bcb0 61 6e 74 0a 2a 2a 20 61 6e 64 20 77 69 6c 6c 20  ant.** and will 
2bcc0 6e 65 76 65 72 20 63 68 61 6e 67 65 2e 20 20 49  never change.  I
2bcd0 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20  t does not need 
2bce0 74 6f 20 62 65 20 64 65 73 74 72 6f 79 65 64 2e  to be destroyed.
2bcf0 20 20 54 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f    The.** SQLITE_
2bd00 54 52 41 4e 53 49 45 4e 54 20 76 61 6c 75 65 20  TRANSIENT value 
2bd10 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 63  means that the c
2bd20 6f 6e 74 65 6e 74 20 77 69 6c 6c 20 6c 69 6b 65  ontent will like
2bd30 6c 79 20 63 68 61 6e 67 65 20 69 6e 0a 2a 2a 20  ly change in.** 
2bd40 74 68 65 20 6e 65 61 72 20 66 75 74 75 72 65 20  the near future 
2bd50 61 6e 64 20 74 68 61 74 20 53 51 4c 69 74 65 20  and that SQLite 
2bd60 73 68 6f 75 6c 64 20 6d 61 6b 65 20 69 74 73 20  should make its 
2bd70 6f 77 6e 20 70 72 69 76 61 74 65 20 63 6f 70 79  own private copy
2bd80 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65   of.** the conte
2bd90 6e 74 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e  nt before return
2bda0 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74  ing..**.** The t
2bdb0 79 70 65 64 65 66 20 69 73 20 6e 65 63 65 73 73  ypedef is necess
2bdc0 61 72 79 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75  ary to work arou
2bdd0 6e 64 20 70 72 6f 62 6c 65 6d 73 20 69 6e 20 63  nd problems in c
2bde0 65 72 74 61 69 6e 0a 2a 2a 20 43 2b 2b 20 63 6f  ertain.** C++ co
2bdf0 6d 70 69 6c 65 72 73 2e 20 20 53 65 65 20 74 69  mpilers.  See ti
2be00 63 6b 65 74 20 23 32 31 39 31 2e 0a 2a 2f 0a 74  cket #2191..*/.t
2be10 79 70 65 64 65 66 20 76 6f 69 64 20 28 2a 73 71  ypedef void (*sq
2be20 6c 69 74 65 33 5f 64 65 73 74 72 75 63 74 6f 72  lite3_destructor
2be30 5f 74 79 70 65 29 28 76 6f 69 64 2a 29 3b 0a 23  _type)(void*);.#
2be40 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54  define SQLITE_ST
2be50 41 54 49 43 20 20 20 20 20 20 28 28 73 71 6c 69  ATIC      ((sqli
2be60 74 65 33 5f 64 65 73 74 72 75 63 74 6f 72 5f 74  te3_destructor_t
2be70 79 70 65 29 30 29 0a 23 64 65 66 69 6e 65 20 53  ype)0).#define S
2be80 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 20  QLITE_TRANSIENT 
2be90 20 20 28 28 73 71 6c 69 74 65 33 5f 64 65 73 74    ((sqlite3_dest
2bea0 72 75 63 74 6f 72 5f 74 79 70 65 29 2d 31 29 0a  ructor_type)-1).
2beb0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
2bec0 20 53 65 74 74 69 6e 67 20 54 68 65 20 52 65 73   Setting The Res
2bed0 75 6c 74 20 4f 66 20 41 6e 20 53 51 4c 20 46 75  ult Of An SQL Fu
2bee0 6e 63 74 69 6f 6e 20 7b 48 31 36 34 30 30 7d 20  nction {H16400} 
2bef0 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S20200>.**.** T
2bf00 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72  hese routines ar
2bf10 65 20 75 73 65 64 20 62 79 20 74 68 65 20 78 46  e used by the xF
2bf20 75 6e 63 20 6f 72 20 78 46 69 6e 61 6c 20 63 61  unc or xFinal ca
2bf30 6c 6c 62 61 63 6b 73 20 74 68 61 74 0a 2a 2a 20  llbacks that.** 
2bf40 69 6d 70 6c 65 6d 65 6e 74 20 53 51 4c 20 66 75  implement SQL fu
2bf50 6e 63 74 69 6f 6e 73 20 61 6e 64 20 61 67 67 72  nctions and aggr
2bf60 65 67 61 74 65 73 2e 20 20 53 65 65 0a 2a 2a 20  egates.  See.** 
2bf70 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f  [sqlite3_create_
2bf80 66 75 6e 63 74 69 6f 6e 28 29 5d 20 61 6e 64 20  function()] and 
2bf90 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f  [sqlite3_create_
2bfa0 66 75 6e 63 74 69 6f 6e 31 36 28 29 5d 0a 2a 2a  function16()].**
2bfb0 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20   for additional 
2bfc0 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a  information..**.
2bfd0 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f  ** These functio
2bfe0 6e 73 20 77 6f 72 6b 20 76 65 72 79 20 6d 75 63  ns work very muc
2bff0 68 20 6c 69 6b 65 20 74 68 65 20 5b 70 61 72 61  h like the [para
2c000 6d 65 74 65 72 20 62 69 6e 64 69 6e 67 5d 20 66  meter binding] f
2c010 61 6d 69 6c 79 20 6f 66 0a 2a 2a 20 66 75 6e 63  amily of.** func
2c020 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 62 69  tions used to bi
2c030 6e 64 20 76 61 6c 75 65 73 20 74 6f 20 68 6f 73  nd values to hos
2c040 74 20 70 61 72 61 6d 65 74 65 72 73 20 69 6e 20  t parameters in 
2c050 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
2c060 6e 74 73 2e 0a 2a 2a 20 52 65 66 65 72 20 74 6f  nts..** Refer to
2c070 20 74 68 65 20 5b 53 51 4c 20 70 61 72 61 6d 65   the [SQL parame
2c080 74 65 72 5d 20 64 6f 63 75 6d 65 6e 74 61 74 69  ter] documentati
2c090 6f 6e 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61  on for additiona
2c0a0 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a  l information..*
2c0b0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
2c0c0 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 29 20 69  _result_blob() i
2c0d0 6e 74 65 72 66 61 63 65 20 73 65 74 73 20 74 68  nterface sets th
2c0e0 65 20 72 65 73 75 6c 74 20 66 72 6f 6d 0a 2a 2a  e result from.**
2c0f0 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d   an application-
2c100 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
2c110 20 74 6f 20 62 65 20 74 68 65 20 42 4c 4f 42 20   to be the BLOB 
2c120 77 68 6f 73 65 20 63 6f 6e 74 65 6e 74 20 69 73  whose content is
2c130 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62   pointed.** to b
2c140 79 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72  y the second par
2c150 61 6d 65 74 65 72 20 61 6e 64 20 77 68 69 63 68  ameter and which
2c160 20 69 73 20 4e 20 62 79 74 65 73 20 6c 6f 6e 67   is N bytes long
2c170 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 0a   where N is the.
2c180 2a 2a 20 74 68 69 72 64 20 70 61 72 61 6d 65 74  ** third paramet
2c190 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  er..**.** The sq
2c1a0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72  lite3_result_zer
2c1b0 6f 62 6c 6f 62 28 29 20 69 6e 74 65 72 66 61 63  oblob() interfac
2c1c0 65 73 20 73 65 74 20 74 68 65 20 72 65 73 75 6c  es set the resul
2c1d0 74 20 6f 66 0a 2a 2a 20 74 68 65 20 61 70 70 6c  t of.** the appl
2c1e0 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20  ication-defined 
2c1f0 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 61  function to be a
2c200 20 42 4c 4f 42 20 63 6f 6e 74 61 69 6e 69 6e 67   BLOB containing
2c210 20 61 6c 6c 20 7a 65 72 6f 0a 2a 2a 20 62 79 74   all zero.** byt
2c220 65 73 20 61 6e 64 20 4e 20 62 79 74 65 73 20 69  es and N bytes i
2c230 6e 20 73 69 7a 65 2c 20 77 68 65 72 65 20 4e 20  n size, where N 
2c240 69 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  is the value of 
2c250 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65  the 2nd paramete
2c260 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  r..**.** The sql
2c270 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62  ite3_result_doub
2c280 6c 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 73  le() interface s
2c290 65 74 73 20 74 68 65 20 72 65 73 75 6c 74 20 66  ets the result f
2c2a0 72 6f 6d 0a 2a 2a 20 61 6e 20 61 70 70 6c 69 63  rom.** an applic
2c2b0 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75  ation-defined fu
2c2c0 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 61 20 66  nction to be a f
2c2d0 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61  loating point va
2c2e0 6c 75 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a  lue specified.**
2c2f0 20 62 79 20 69 74 73 20 32 6e 64 20 61 72 67 75   by its 2nd argu
2c300 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ment..**.** The 
2c310 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
2c320 72 72 6f 72 28 29 20 61 6e 64 20 73 71 6c 69 74  rror() and sqlit
2c330 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31  e3_result_error1
2c340 36 28 29 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a  6() functions.**
2c350 20 63 61 75 73 65 20 74 68 65 20 69 6d 70 6c 65   cause the imple
2c360 6d 65 6e 74 65 64 20 53 51 4c 20 66 75 6e 63 74  mented SQL funct
2c370 69 6f 6e 20 74 6f 20 74 68 72 6f 77 20 61 6e 20  ion to throw an 
2c380 65 78 63 65 70 74 69 6f 6e 2e 0a 2a 2a 20 53 51  exception..** SQ
2c390 4c 69 74 65 20 75 73 65 73 20 74 68 65 20 73 74  Lite uses the st
2c3a0 72 69 6e 67 20 70 6f 69 6e 74 65 64 20 74 6f 20  ring pointed to 
2c3b0 62 79 20 74 68 65 0a 2a 2a 20 32 6e 64 20 70 61  by the.** 2nd pa
2c3c0 72 61 6d 65 74 65 72 20 6f 66 20 73 71 6c 69 74  rameter of sqlit
2c3d0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
2c3e0 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73  ) or sqlite3_res
2c3f0 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 0a 2a 2a  ult_error16().**
2c400 20 61 73 20 74 68 65 20 74 65 78 74 20 6f 66 20   as the text of 
2c410 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
2c420 2e 20 20 53 51 4c 69 74 65 20 69 6e 74 65 72 70  .  SQLite interp
2c430 72 65 74 73 20 74 68 65 20 65 72 72 6f 72 0a 2a  rets the error.*
2c440 2a 20 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67  * message string
2c450 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 72 65   from sqlite3_re
2c460 73 75 6c 74 5f 65 72 72 6f 72 28 29 20 61 73 20  sult_error() as 
2c470 55 54 46 2d 38 2e 20 53 51 4c 69 74 65 0a 2a 2a  UTF-8. SQLite.**
2c480 20 69 6e 74 65 72 70 72 65 74 73 20 74 68 65 20   interprets the 
2c490 73 74 72 69 6e 67 20 66 72 6f 6d 20 73 71 6c 69  string from sqli
2c4a0 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
2c4b0 31 36 28 29 20 61 73 20 55 54 46 2d 31 36 20 69  16() as UTF-16 i
2c4c0 6e 20 6e 61 74 69 76 65 0a 2a 2a 20 62 79 74 65  n native.** byte
2c4d0 20 6f 72 64 65 72 2e 20 20 49 66 20 74 68 65 20   order.  If the 
2c4e0 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20  third parameter 
2c4f0 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  to sqlite3_resul
2c500 74 5f 65 72 72 6f 72 28 29 0a 2a 2a 20 6f 72 20  t_error().** or 
2c510 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
2c520 72 72 6f 72 31 36 28 29 20 69 73 20 6e 65 67 61  rror16() is nega
2c530 74 69 76 65 20 74 68 65 6e 20 53 51 4c 69 74 65  tive then SQLite
2c540 20 74 61 6b 65 73 20 61 73 20 74 68 65 20 65 72   takes as the er
2c550 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 61  ror.** message a
2c560 6c 6c 20 74 65 78 74 20 75 70 20 74 68 72 6f 75  ll text up throu
2c570 67 68 20 74 68 65 20 66 69 72 73 74 20 7a 65 72  gh the first zer
2c580 6f 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20  o character..** 
2c590 49 66 20 74 68 65 20 74 68 69 72 64 20 70 61 72  If the third par
2c5a0 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65  ameter to sqlite
2c5b0 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29  3_result_error()
2c5c0 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72   or.** sqlite3_r
2c5d0 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 20  esult_error16() 
2c5e0 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20  is non-negative 
2c5f0 74 68 65 6e 20 53 51 4c 69 74 65 20 74 61 6b 65  then SQLite take
2c600 73 20 74 68 61 74 20 6d 61 6e 79 0a 2a 2a 20 62  s that many.** b
2c610 79 74 65 73 20 28 6e 6f 74 20 63 68 61 72 61 63  ytes (not charac
2c620 74 65 72 73 29 20 66 72 6f 6d 20 74 68 65 20 32  ters) from the 2
2c630 6e 64 20 70 61 72 61 6d 65 74 65 72 20 61 73 20  nd parameter as 
2c640 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67  the error messag
2c650 65 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  e..** The sqlite
2c660 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29  3_result_error()
2c670 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 73   and sqlite3_res
2c680 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 0a 2a 2a  ult_error16().**
2c690 20 72 6f 75 74 69 6e 65 73 20 6d 61 6b 65 20 61   routines make a
2c6a0 20 70 72 69 76 61 74 65 20 63 6f 70 79 20 6f 66   private copy of
2c6b0 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61   the error messa
2c6c0 67 65 20 74 65 78 74 20 62 65 66 6f 72 65 0a 2a  ge text before.*
2c6d0 2a 20 74 68 65 79 20 72 65 74 75 72 6e 2e 20 20  * they return.  
2c6e0 48 65 6e 63 65 2c 20 74 68 65 20 63 61 6c 6c 69  Hence, the calli
2c6f0 6e 67 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 20  ng function can 
2c700 64 65 61 6c 6c 6f 63 61 74 65 20 6f 72 0a 2a 2a  deallocate or.**
2c710 20 6d 6f 64 69 66 79 20 74 68 65 20 74 65 78 74   modify the text
2c720 20 61 66 74 65 72 20 74 68 65 79 20 72 65 74 75   after they retu
2c730 72 6e 20 77 69 74 68 6f 75 74 20 68 61 72 6d 2e  rn without harm.
2c740 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
2c750 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64  result_error_cod
2c760 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 63 68 61  e() function cha
2c770 6e 67 65 73 20 74 68 65 20 65 72 72 6f 72 20 63  nges the error c
2c780 6f 64 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20  ode.** returned 
2c790 62 79 20 53 51 4c 69 74 65 20 61 73 20 61 20 72  by SQLite as a r
2c7a0 65 73 75 6c 74 20 6f 66 20 61 6e 20 65 72 72 6f  esult of an erro
2c7b0 72 20 69 6e 20 61 20 66 75 6e 63 74 69 6f 6e 2e  r in a function.
2c7c0 20 20 42 79 20 64 65 66 61 75 6c 74 2c 0a 2a 2a    By default,.**
2c7d0 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20   the error code 
2c7e0 69 73 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e  is SQLITE_ERROR.
2c7f0 20 20 41 20 73 75 62 73 65 71 75 65 6e 74 20 63    A subsequent c
2c800 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 72  all to sqlite3_r
2c810 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 0a 2a 2a  esult_error().**
2c820 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75   or sqlite3_resu
2c830 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 72 65 73  lt_error16() res
2c840 65 74 73 20 74 68 65 20 65 72 72 6f 72 20 63 6f  ets the error co
2c850 64 65 20 74 6f 20 53 51 4c 49 54 45 5f 45 52 52  de to SQLITE_ERR
2c860 4f 52 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  OR..**.** The sq
2c870 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 6f 6f  lite3_result_too
2c880 62 69 67 28 29 20 69 6e 74 65 72 66 61 63 65 20  big() interface 
2c890 63 61 75 73 65 73 20 53 51 4c 69 74 65 20 74 6f  causes SQLite to
2c8a0 20 74 68 72 6f 77 20 61 6e 20 65 72 72 6f 72 0a   throw an error.
2c8b0 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68  ** indicating th
2c8c0 61 74 20 61 20 73 74 72 69 6e 67 20 6f 72 20 42  at a string or B
2c8d0 4c 4f 42 20 69 73 20 74 6f 20 6c 6f 6e 67 20 74  LOB is to long t
2c8e0 6f 20 72 65 70 72 65 73 65 6e 74 2e 0a 2a 2a 0a  o represent..**.
2c8f0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72  ** The sqlite3_r
2c900 65 73 75 6c 74 5f 6e 6f 6d 65 6d 28 29 20 69 6e  esult_nomem() in
2c910 74 65 72 66 61 63 65 20 63 61 75 73 65 73 20 53  terface causes S
2c920 51 4c 69 74 65 20 74 6f 20 74 68 72 6f 77 20 61  QLite to throw a
2c930 6e 20 65 72 72 6f 72 0a 2a 2a 20 69 6e 64 69 63  n error.** indic
2c940 61 74 69 6e 67 20 74 68 61 74 20 61 20 6d 65 6d  ating that a mem
2c950 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66  ory allocation f
2c960 61 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ailed..**.** The
2c970 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2c980 69 6e 74 28 29 20 69 6e 74 65 72 66 61 63 65 20  int() interface 
2c990 73 65 74 73 20 74 68 65 20 72 65 74 75 72 6e 20  sets the return 
2c9a0 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20  value.** of the 
2c9b0 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69  application-defi
2c9c0 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  ned function to 
2c9d0 62 65 20 74 68 65 20 33 32 2d 62 69 74 20 73 69  be the 32-bit si
2c9e0 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20  gned integer.** 
2c9f0 76 61 6c 75 65 20 67 69 76 65 6e 20 69 6e 20 74  value given in t
2ca00 68 65 20 32 6e 64 20 61 72 67 75 6d 65 6e 74 2e  he 2nd argument.
2ca10 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
2ca20 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 29 20 69  result_int64() i
2ca30 6e 74 65 72 66 61 63 65 20 73 65 74 73 20 74 68  nterface sets th
2ca40 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a  e return value.*
2ca50 2a 20 6f 66 20 74 68 65 20 61 70 70 6c 69 63 61  * of the applica
2ca60 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e  tion-defined fun
2ca70 63 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20  ction to be the 
2ca80 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e  64-bit signed in
2ca90 74 65 67 65 72 0a 2a 2a 20 76 61 6c 75 65 20 67  teger.** value g
2caa0 69 76 65 6e 20 69 6e 20 74 68 65 20 32 6e 64 20  iven in the 2nd 
2cab0 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  argument..**.** 
2cac0 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  The sqlite3_resu
2cad0 6c 74 5f 6e 75 6c 6c 28 29 20 69 6e 74 65 72 66  lt_null() interf
2cae0 61 63 65 20 73 65 74 73 20 74 68 65 20 72 65 74  ace sets the ret
2caf0 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20  urn value.** of 
2cb00 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d  the application-
2cb10 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
2cb20 20 74 6f 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a   to be NULL..**.
2cb30 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72  ** The sqlite3_r
2cb40 65 73 75 6c 74 5f 74 65 78 74 28 29 2c 20 73 71  esult_text(), sq
2cb50 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
2cb60 74 31 36 28 29 2c 0a 2a 2a 20 73 71 6c 69 74 65  t16(),.** sqlite
2cb70 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c  3_result_text16l
2cb80 65 28 29 2c 20 61 6e 64 20 73 71 6c 69 74 65 33  e(), and sqlite3
2cb90 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65  _result_text16be
2cba0 28 29 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a  () interfaces.**
2cbb0 20 73 65 74 20 74 68 65 20 72 65 74 75 72 6e 20   set the return 
2cbc0 76 61 6c 75 65 20 6f 66 20 74 68 65 20 61 70 70  value of the app
2cbd0 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64  lication-defined
2cbe0 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 0a   function to be.
2cbf0 2a 2a 20 61 20 74 65 78 74 20 73 74 72 69 6e 67  ** a text string
2cc00 20 77 68 69 63 68 20 69 73 20 72 65 70 72 65 73   which is repres
2cc10 65 6e 74 65 64 20 61 73 20 55 54 46 2d 38 2c 20  ented as UTF-8, 
2cc20 55 54 46 2d 31 36 20 6e 61 74 69 76 65 20 62 79  UTF-16 native by
2cc30 74 65 20 6f 72 64 65 72 2c 0a 2a 2a 20 55 54 46  te order,.** UTF
2cc40 2d 31 36 20 6c 69 74 74 6c 65 20 65 6e 64 69 61  -16 little endia
2cc50 6e 2c 20 6f 72 20 55 54 46 2d 31 36 20 62 69 67  n, or UTF-16 big
2cc60 20 65 6e 64 69 61 6e 2c 20 72 65 73 70 65 63 74   endian, respect
2cc70 69 76 65 6c 79 2e 0a 2a 2a 20 53 51 4c 69 74 65  ively..** SQLite
2cc80 20 74 61 6b 65 73 20 74 68 65 20 74 65 78 74 20   takes the text 
2cc90 72 65 73 75 6c 74 20 66 72 6f 6d 20 74 68 65 20  result from the 
2cca0 61 70 70 6c 69 63 61 74 69 6f 6e 20 66 72 6f 6d  application from
2ccb0 0a 2a 2a 20 74 68 65 20 32 6e 64 20 70 61 72 61  .** the 2nd para
2ccc0 6d 65 74 65 72 20 6f 66 20 74 68 65 20 73 71 6c  meter of the sql
2ccd0 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
2cce0 2a 20 69 6e 74 65 72 66 61 63 65 73 2e 0a 2a 2a  * interfaces..**
2ccf0 20 49 66 20 74 68 65 20 33 72 64 20 70 61 72 61   If the 3rd para
2cd00 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c  meter to the sql
2cd10 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
2cd20 2a 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20  * interfaces.** 
2cd30 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65  is negative, the
2cd40 6e 20 53 51 4c 69 74 65 20 74 61 6b 65 73 20 72  n SQLite takes r
2cd50 65 73 75 6c 74 20 74 65 78 74 20 66 72 6f 6d 20  esult text from 
2cd60 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65  the 2nd paramete
2cd70 72 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65  r.** through the
2cd80 20 66 69 72 73 74 20 7a 65 72 6f 20 63 68 61 72   first zero char
2cd90 61 63 74 65 72 2e 0a 2a 2a 20 49 66 20 74 68 65  acter..** If the
2cda0 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 74   3rd parameter t
2cdb0 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65  o the sqlite3_re
2cdc0 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72  sult_text* inter
2cdd0 66 61 63 65 73 0a 2a 2a 20 69 73 20 6e 6f 6e 2d  faces.** is non-
2cde0 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e 20 61  negative, then a
2cdf0 73 20 6d 61 6e 79 20 62 79 74 65 73 20 28 6e 6f  s many bytes (no
2ce00 74 20 63 68 61 72 61 63 74 65 72 73 29 20 6f 66  t characters) of
2ce10 20 74 68 65 20 74 65 78 74 0a 2a 2a 20 70 6f 69   the text.** poi
2ce20 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65 20 32  nted to by the 2
2ce30 6e 64 20 70 61 72 61 6d 65 74 65 72 20 61 72 65  nd parameter are
2ce40 20 74 61 6b 65 6e 20 61 73 20 74 68 65 20 61 70   taken as the ap
2ce50 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
2ce60 64 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65  d.** function re
2ce70 73 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20  sult..** If the 
2ce80 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f  4th parameter to
2ce90 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73   the sqlite3_res
2cea0 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66  ult_text* interf
2ceb0 61 63 65 73 0a 2a 2a 20 6f 72 20 73 71 6c 69 74  aces.** or sqlit
2cec0 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 69  e3_result_blob i
2ced0 73 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f 69  s a non-NULL poi
2cee0 6e 74 65 72 2c 20 74 68 65 6e 20 53 51 4c 69 74  nter, then SQLit
2cef0 65 20 63 61 6c 6c 73 20 74 68 61 74 0a 2a 2a 20  e calls that.** 
2cf00 66 75 6e 63 74 69 6f 6e 20 61 73 20 74 68 65 20  function as the 
2cf10 64 65 73 74 72 75 63 74 6f 72 20 6f 6e 20 74 68  destructor on th
2cf20 65 20 74 65 78 74 20 6f 72 20 42 4c 4f 42 20 72  e text or BLOB r
2cf30 65 73 75 6c 74 20 77 68 65 6e 20 69 74 20 68 61  esult when it ha
2cf40 73 0a 2a 2a 20 66 69 6e 69 73 68 65 64 20 75 73  s.** finished us
2cf50 69 6e 67 20 74 68 61 74 20 72 65 73 75 6c 74 2e  ing that result.
2cf60 0a 2a 2a 20 49 66 20 74 68 65 20 34 74 68 20 70  .** If the 4th p
2cf70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20  arameter to the 
2cf80 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
2cf90 65 78 74 2a 20 69 6e 74 65 72 66 61 63 65 73 20  ext* interfaces 
2cfa0 6f 72 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33  or to.** sqlite3
2cfb0 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 69 73 20  _result_blob is 
2cfc0 74 68 65 20 73 70 65 63 69 61 6c 20 63 6f 6e 73  the special cons
2cfd0 74 61 6e 74 20 53 51 4c 49 54 45 5f 53 54 41 54  tant SQLITE_STAT
2cfe0 49 43 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 0a  IC, then SQLite.
2cff0 2a 2a 20 61 73 73 75 6d 65 73 20 74 68 61 74 20  ** assumes that 
2d000 74 68 65 20 74 65 78 74 20 6f 72 20 42 4c 4f 42  the text or BLOB
2d010 20 72 65 73 75 6c 74 20 69 73 20 69 6e 20 63 6f   result is in co
2d020 6e 73 74 61 6e 74 20 73 70 61 63 65 20 61 6e 64  nstant space and
2d030 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 63 6f 70   does not.** cop
2d040 79 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  y the content of
2d050 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 6e   the parameter n
2d060 6f 72 20 63 61 6c 6c 20 61 20 64 65 73 74 72 75  or call a destru
2d070 63 74 6f 72 20 6f 6e 20 74 68 65 20 63 6f 6e 74  ctor on the cont
2d080 65 6e 74 0a 2a 2a 20 77 68 65 6e 20 69 74 20 68  ent.** when it h
2d090 61 73 20 66 69 6e 69 73 68 65 64 20 75 73 69 6e  as finished usin
2d0a0 67 20 74 68 61 74 20 72 65 73 75 6c 74 2e 0a 2a  g that result..*
2d0b0 2a 20 49 66 20 74 68 65 20 34 74 68 20 70 61 72  * If the 4th par
2d0c0 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71  ameter to the sq
2d0d0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
2d0e0 74 2a 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a  t* interfaces.**
2d0f0 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75   or sqlite3_resu
2d100 6c 74 5f 62 6c 6f 62 20 69 73 20 74 68 65 20 73  lt_blob is the s
2d110 70 65 63 69 61 6c 20 63 6f 6e 73 74 61 6e 74 20  pecial constant 
2d120 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
2d130 0a 2a 2a 20 74 68 65 6e 20 53 51 4c 69 74 65 20  .** then SQLite 
2d140 6d 61 6b 65 73 20 61 20 63 6f 70 79 20 6f 66 20  makes a copy of 
2d150 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74 6f 20  the result into 
2d160 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64 20 66  space obtained f
2d170 72 6f 6d 0a 2a 2a 20 66 72 6f 6d 20 5b 73 71 6c  rom.** from [sql
2d180 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 62  ite3_malloc()] b
2d190 65 66 6f 72 65 20 69 74 20 72 65 74 75 72 6e 73  efore it returns
2d1a0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
2d1b0 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65  te3_result_value
2d1c0 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 74  () interface set
2d1d0 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a  s the result of.
2d1e0 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  ** the applicati
2d1f0 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74  on-defined funct
2d200 69 6f 6e 20 74 6f 20 62 65 20 61 20 63 6f 70 79  ion to be a copy
2d210 20 74 68 65 0a 2a 2a 20 5b 75 6e 70 72 6f 74 65   the.** [unprote
2d220 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c  cted sqlite3_val
2d230 75 65 5d 20 6f 62 6a 65 63 74 20 73 70 65 63 69  ue] object speci
2d240 66 69 65 64 20 62 79 20 74 68 65 20 32 6e 64 20  fied by the 2nd 
2d250 70 61 72 61 6d 65 74 65 72 2e 20 20 54 68 65 0a  parameter.  The.
2d260 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ** sqlite3_resul
2d270 74 5f 76 61 6c 75 65 28 29 20 69 6e 74 65 72 66  t_value() interf
2d280 61 63 65 20 6d 61 6b 65 73 20 61 20 63 6f 70 79  ace makes a copy
2d290 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33   of the [sqlite3
2d2a0 5f 76 61 6c 75 65 5d 0a 2a 2a 20 73 6f 20 74 68  _value].** so th
2d2b0 61 74 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  at the [sqlite3_
2d2c0 76 61 6c 75 65 5d 20 73 70 65 63 69 66 69 65 64  value] specified
2d2d0 20 69 6e 20 74 68 65 20 70 61 72 61 6d 65 74 65   in the paramete
2d2e0 72 20 6d 61 79 20 63 68 61 6e 67 65 20 6f 72 0a  r may change or.
2d2f0 2a 2a 20 62 65 20 64 65 61 6c 6c 6f 63 61 74 65  ** be deallocate
2d300 64 20 61 66 74 65 72 20 73 71 6c 69 74 65 33 5f  d after sqlite3_
2d310 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 20 72  result_value() r
2d320 65 74 75 72 6e 73 20 77 69 74 68 6f 75 74 20 68  eturns without h
2d330 61 72 6d 2e 0a 2a 2a 20 41 20 5b 70 72 6f 74 65  arm..** A [prote
2d340 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c  cted sqlite3_val
2d350 75 65 5d 20 6f 62 6a 65 63 74 20 6d 61 79 20 61  ue] object may a
2d360 6c 77 61 79 73 20 62 65 20 75 73 65 64 20 77 68  lways be used wh
2d370 65 72 65 20 61 6e 0a 2a 2a 20 5b 75 6e 70 72 6f  ere an.** [unpro
2d380 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76  tected sqlite3_v
2d390 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 69 73 20  alue] object is 
2d3a0 72 65 71 75 69 72 65 64 2c 20 73 6f 20 65 69 74  required, so eit
2d3b0 68 65 72 0a 2a 2a 20 6b 69 6e 64 20 6f 66 20 5b  her.** kind of [
2d3c0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f  sqlite3_value] o
2d3d0 62 6a 65 63 74 20 63 61 6e 20 62 65 20 75 73 65  bject can be use
2d3e0 64 20 77 69 74 68 20 74 68 69 73 20 69 6e 74 65  d with this inte
2d3f0 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  rface..**.** If 
2d400 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61  these routines a
2d410 72 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77  re called from w
2d420 69 74 68 69 6e 20 74 68 65 20 64 69 66 66 65 72  ithin the differ
2d430 65 6e 74 20 74 68 72 65 61 64 0a 2a 2a 20 74 68  ent thread.** th
2d440 61 6e 20 74 68 65 20 6f 6e 65 20 63 6f 6e 74 61  an the one conta
2d450 69 6e 69 6e 67 20 74 68 65 20 61 70 70 6c 69 63  ining the applic
2d460 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75  ation-defined fu
2d470 6e 63 74 69 6f 6e 20 74 68 61 74 20 72 65 63 65  nction that rece
2d480 69 76 65 64 0a 2a 2a 20 74 68 65 20 5b 73 71 6c  ived.** the [sql
2d490 69 74 65 33 5f 63 6f 6e 74 65 78 74 5d 20 70 6f  ite3_context] po
2d4a0 69 6e 74 65 72 2c 20 74 68 65 20 72 65 73 75 6c  inter, the resul
2d4b0 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64  ts are undefined
2d4c0 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
2d4d0 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 34 30 33  ents:.** [H16403
2d4e0 5d 20 5b 48 31 36 34 30 36 5d 20 5b 48 31 36 34  ] [H16406] [H164
2d4f0 30 39 5d 20 5b 48 31 36 34 31 32 5d 20 5b 48 31  09] [H16412] [H1
2d500 36 34 31 35 5d 20 5b 48 31 36 34 31 38 5d 20 5b  6415] [H16418] [
2d510 48 31 36 34 32 31 5d 20 5b 48 31 36 34 32 34 5d  H16421] [H16424]
2d520 0a 2a 2a 20 5b 48 31 36 34 32 37 5d 20 5b 48 31  .** [H16427] [H1
2d530 36 34 33 30 5d 20 5b 48 31 36 34 33 33 5d 20 5b  6430] [H16433] [
2d540 48 31 36 34 33 36 5d 20 5b 48 31 36 34 33 39 5d  H16436] [H16439]
2d550 20 5b 48 31 36 34 34 32 5d 20 5b 48 31 36 34 34   [H16442] [H1644
2d560 35 5d 20 5b 48 31 36 34 34 38 5d 0a 2a 2a 20 5b  5] [H16448].** [
2d570 48 31 36 34 35 31 5d 20 5b 48 31 36 34 35 34 5d  H16451] [H16454]
2d580 20 5b 48 31 36 34 35 37 5d 20 5b 48 31 36 34 36   [H16457] [H1646
2d590 30 5d 20 5b 48 31 36 34 36 33 5d 0a 2a 2f 0a 53  0] [H16463].*/.S
2d5a0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
2d5b0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c  qlite3_result_bl
2d5c0 6f 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ob(sqlite3_conte
2d5d0 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a  xt*, const void*
2d5e0 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76  , int, void(*)(v
2d5f0 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41  oid*));.SQLITE_A
2d600 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
2d610 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 73 71  result_double(sq
2d620 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20  lite3_context*, 
2d630 64 6f 75 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f  double);.SQLITE_
2d640 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
2d650 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 73 71  _result_error(sq
2d660 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20  lite3_context*, 
2d670 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74  const char*, int
2d680 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  );.SQLITE_API vo
2d690 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
2d6a0 74 5f 65 72 72 6f 72 31 36 28 73 71 6c 69 74 65  t_error16(sqlite
2d6b0 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73  3_context*, cons
2d6c0 74 20 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53  t void*, int);.S
2d6d0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
2d6e0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
2d6f0 72 6f 72 5f 74 6f 6f 62 69 67 28 73 71 6c 69 74  ror_toobig(sqlit
2d700 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51  e3_context*);.SQ
2d710 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
2d720 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
2d730 6f 72 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74 65 33  or_nomem(sqlite3
2d740 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49  _context*);.SQLI
2d750 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
2d760 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
2d770 5f 63 6f 64 65 28 73 71 6c 69 74 65 33 5f 63 6f  _code(sqlite3_co
2d780 6e 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 53 51  ntext*, int);.SQ
2d790 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
2d7a0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
2d7b0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
2d7c0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
2d7d0 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
2d7e0 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 73 71  _result_int64(sq
2d7f0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20  lite3_context*, 
2d800 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a  sqlite3_int64);.
2d810 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
2d820 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
2d830 75 6c 6c 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ull(sqlite3_cont
2d840 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  ext*);.SQLITE_AP
2d850 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
2d860 65 73 75 6c 74 5f 74 65 78 74 28 73 71 6c 69 74  esult_text(sqlit
2d870 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e  e3_context*, con
2d880 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 76  st char*, int, v
2d890 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
2d8a0 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
2d8b0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
2d8c0 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 63 6f  ext16(sqlite3_co
2d8d0 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f  ntext*, const vo
2d8e0 69 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a  id*, int, void(*
2d8f0 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54  )(void*));.SQLIT
2d900 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
2d910 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36  e3_result_text16
2d920 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  le(sqlite3_conte
2d930 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a  xt*, const void*
2d940 2c 20 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f  , int,void(*)(vo
2d950 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50  id*));.SQLITE_AP
2d960 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
2d970 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28 73  esult_text16be(s
2d980 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
2d990 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e   const void*, in
2d9a0 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29  t,void(*)(void*)
2d9b0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  );.SQLITE_API vo
2d9c0 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
2d9d0 74 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f  t_value(sqlite3_
2d9e0 63 6f 6e 74 65 78 74 2a 2c 20 73 71 6c 69 74 65  context*, sqlite
2d9f0 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54  3_value*);.SQLIT
2da00 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
2da10 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c  e3_result_zerobl
2da20 6f 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ob(sqlite3_conte
2da30 78 74 2a 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a  xt*, int n);../*
2da40 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65  .** CAPI3REF: De
2da50 66 69 6e 65 20 4e 65 77 20 43 6f 6c 6c 61 74 69  fine New Collati
2da60 6e 67 20 53 65 71 75 65 6e 63 65 73 20 7b 48 31  ng Sequences {H1
2da70 36 36 30 30 7d 20 3c 53 32 30 33 30 30 3e 0a 2a  6600} <S20300>.*
2da80 2a 0a 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 74  *.** These funct
2da90 69 6f 6e 73 20 61 72 65 20 75 73 65 64 20 74 6f  ions are used to
2daa0 20 61 64 64 20 6e 65 77 20 63 6f 6c 6c 61 74 69   add new collati
2dab0 6f 6e 20 73 65 71 75 65 6e 63 65 73 20 74 6f 20  on sequences to 
2dac0 74 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65  the.** [database
2dad0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65   connection] spe
2dae0 63 69 66 69 65 64 20 61 73 20 74 68 65 20 66 69  cified as the fi
2daf0 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a  rst argument..**
2db00 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20  .** The name of 
2db10 74 68 65 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f  the new collatio
2db20 6e 20 73 65 71 75 65 6e 63 65 20 69 73 20 73 70  n sequence is sp
2db30 65 63 69 66 69 65 64 20 61 73 20 61 20 55 54 46  ecified as a UTF
2db40 2d 38 20 73 74 72 69 6e 67 0a 2a 2a 20 66 6f 72  -8 string.** for
2db50 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
2db60 63 6f 6c 6c 61 74 69 6f 6e 28 29 20 61 6e 64 20  collation() and 
2db70 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63  sqlite3_create_c
2db80 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 0a 2a 2a  ollation_v2().**
2db90 20 61 6e 64 20 61 20 55 54 46 2d 31 36 20 73 74   and a UTF-16 st
2dba0 72 69 6e 67 20 66 6f 72 20 73 71 6c 69 74 65 33  ring for sqlite3
2dbb0 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
2dbc0 6e 31 36 28 29 2e 20 49 6e 20 61 6c 6c 20 63 61  n16(). In all ca
2dbd0 73 65 73 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20  ses.** the name 
2dbe0 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65  is passed as the
2dbf0 20 73 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e   second function
2dc00 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a   argument..**.**
2dc10 20 54 68 65 20 74 68 69 72 64 20 61 72 67 75 6d   The third argum
2dc20 65 6e 74 20 6d 61 79 20 62 65 20 6f 6e 65 20 6f  ent may be one o
2dc30 66 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20  f the constants 
2dc40 5b 53 51 4c 49 54 45 5f 55 54 46 38 5d 2c 0a 2a  [SQLITE_UTF8],.*
2dc50 2a 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  * [SQLITE_UTF16L
2dc60 45 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 55  E], or [SQLITE_U
2dc70 54 46 31 36 42 45 5d 2c 20 69 6e 64 69 63 61 74  TF16BE], indicat
2dc80 69 6e 67 20 74 68 61 74 20 74 68 65 20 75 73 65  ing that the use
2dc90 72 2d 73 75 70 70 6c 69 65 64 0a 2a 2a 20 72 6f  r-supplied.** ro
2dca0 75 74 69 6e 65 20 65 78 70 65 63 74 73 20 74 6f  utine expects to
2dcb0 20 62 65 20 70 61 73 73 65 64 20 70 6f 69 6e 74   be passed point
2dcc0 65 72 73 20 74 6f 20 73 74 72 69 6e 67 73 20 65  ers to strings e
2dcd0 6e 63 6f 64 65 64 20 75 73 69 6e 67 20 55 54 46  ncoded using UTF
2dce0 2d 38 2c 0a 2a 2a 20 55 54 46 2d 31 36 20 6c 69  -8,.** UTF-16 li
2dcf0 74 74 6c 65 2d 65 6e 64 69 61 6e 2c 20 6f 72 20  ttle-endian, or 
2dd00 55 54 46 2d 31 36 20 62 69 67 2d 65 6e 64 69 61  UTF-16 big-endia
2dd10 6e 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e  n, respectively.
2dd20 20 54 68 65 0a 2a 2a 20 74 68 69 72 64 20 61 72   The.** third ar
2dd30 67 75 6d 65 6e 74 20 6d 69 67 68 74 20 61 6c 73  gument might als
2dd40 6f 20 62 65 20 5b 53 51 4c 49 54 45 5f 55 54 46  o be [SQLITE_UTF
2dd50 31 36 5d 20 74 6f 20 69 6e 64 69 63 61 74 65 20  16] to indicate 
2dd60 74 68 61 74 20 74 68 65 20 72 6f 75 74 69 6e 65  that the routine
2dd70 0a 2a 2a 20 65 78 70 65 63 74 73 20 70 6f 69 6e  .** expects poin
2dd80 74 65 72 73 20 74 6f 20 62 65 20 55 54 46 2d 31  ters to be UTF-1
2dd90 36 20 73 74 72 69 6e 67 73 20 69 6e 20 74 68 65  6 strings in the
2dda0 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64   native byte ord
2ddb0 65 72 2c 20 6f 72 20 74 68 65 0a 2a 2a 20 61 72  er, or the.** ar
2ddc0 67 75 6d 65 6e 74 20 63 61 6e 20 62 65 20 5b 53  gument can be [S
2ddd0 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47  QLITE_UTF16_ALIG
2dde0 4e 45 44 5d 20 69 66 20 74 68 65 0a 2a 2a 20 74  NED] if the.** t
2ddf0 68 65 20 72 6f 75 74 69 6e 65 20 65 78 70 65 63  he routine expec
2de00 74 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 31  ts pointers to 1
2de10 36 2d 62 69 74 20 77 6f 72 64 20 61 6c 69 67 6e  6-bit word align
2de20 65 64 20 73 74 72 69 6e 67 73 0a 2a 2a 20 6f 66  ed strings.** of
2de30 20 55 54 46 2d 31 36 20 69 6e 20 74 68 65 20 6e   UTF-16 in the n
2de40 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72  ative byte order
2de50 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65  ..**.** A pointe
2de60 72 20 74 6f 20 74 68 65 20 75 73 65 72 20 73 75  r to the user su
2de70 70 70 6c 69 65 64 20 72 6f 75 74 69 6e 65 20 6d  pplied routine m
2de80 75 73 74 20 62 65 20 70 61 73 73 65 64 20 61 73  ust be passed as
2de90 20 74 68 65 20 66 69 66 74 68 0a 2a 2a 20 61 72   the fifth.** ar
2dea0 67 75 6d 65 6e 74 2e 20 20 49 66 20 69 74 20 69  gument.  If it i
2deb0 73 20 4e 55 4c 4c 2c 20 74 68 69 73 20 69 73 20  s NULL, this is 
2dec0 74 68 65 20 73 61 6d 65 20 61 73 20 64 65 6c 65  the same as dele
2ded0 74 69 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74 69  ting the collati
2dee0 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 28  on.** sequence (
2def0 73 6f 20 74 68 61 74 20 53 51 4c 69 74 65 20 63  so that SQLite c
2df00 61 6e 6e 6f 74 20 63 61 6c 6c 20 69 74 20 61 6e  annot call it an
2df10 79 6d 6f 72 65 29 2e 0a 2a 2a 20 45 61 63 68 20  ymore)..** Each 
2df20 74 69 6d 65 20 74 68 65 20 61 70 70 6c 69 63 61  time the applica
2df30 74 69 6f 6e 20 73 75 70 70 6c 69 65 64 20 66 75  tion supplied fu
2df40 6e 63 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b 65  nction is invoke
2df50 64 2c 20 69 74 20 69 73 20 70 61 73 73 65 64 0a  d, it is passed.
2df60 2a 2a 20 61 73 20 69 74 73 20 66 69 72 73 74 20  ** as its first 
2df70 70 61 72 61 6d 65 74 65 72 20 61 20 63 6f 70 79  parameter a copy
2df80 20 6f 66 20 74 68 65 20 76 6f 69 64 2a 20 70 61   of the void* pa
2df90 73 73 65 64 20 61 73 20 74 68 65 20 66 6f 75 72  ssed as the four
2dfa0 74 68 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74  th argument.** t
2dfb0 6f 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  o sqlite3_create
2dfc0 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 20 6f 72 20  _collation() or 
2dfd0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63  sqlite3_create_c
2dfe0 6f 6c 6c 61 74 69 6f 6e 31 36 28 29 2e 0a 2a 2a  ollation16()..**
2dff0 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e 69 6e  .** The remainin
2e000 67 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74  g arguments to t
2e010 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73  he application-s
2e020 75 70 70 6c 69 65 64 20 72 6f 75 74 69 6e 65 20  upplied routine 
2e030 61 72 65 20 74 77 6f 20 73 74 72 69 6e 67 73 2c  are two strings,
2e040 0a 2a 2a 20 65 61 63 68 20 72 65 70 72 65 73 65  .** each represe
2e050 6e 74 65 64 20 62 79 20 61 20 28 6c 65 6e 67 74  nted by a (lengt
2e060 68 2c 20 64 61 74 61 29 20 70 61 69 72 20 61 6e  h, data) pair an
2e070 64 20 65 6e 63 6f 64 65 64 20 69 6e 20 74 68 65  d encoded in the
2e080 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 74 68 61   encoding.** tha
2e090 74 20 77 61 73 20 70 61 73 73 65 64 20 61 73 20  t was passed as 
2e0a0 74 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65  the third argume
2e0b0 6e 74 20 77 68 65 6e 20 74 68 65 20 63 6f 6c 6c  nt when the coll
2e0c0 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77  ation sequence w
2e0d0 61 73 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64  as.** registered
2e0e0 2e 20 7b 45 4e 44 7d 20 20 54 68 65 20 61 70 70  . {END}  The app
2e0f0 6c 69 63 61 74 69 6f 6e 20 64 65 66 69 6e 65 64  lication defined
2e100 20 63 6f 6c 6c 61 74 69 6f 6e 20 72 6f 75 74 69   collation routi
2e110 6e 65 20 73 68 6f 75 6c 64 0a 2a 2a 20 72 65 74  ne should.** ret
2e120 75 72 6e 20 6e 65 67 61 74 69 76 65 2c 20 7a 65  urn negative, ze
2e130 72 6f 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69  ro or positive i
2e140 66 20 74 68 65 20 66 69 72 73 74 20 73 74 72 69  f the first stri
2e150 6e 67 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c  ng is less than,
2e160 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72  .** equal to, or
2e170 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68   greater than th
2e180 65 20 73 65 63 6f 6e 64 20 73 74 72 69 6e 67 2e  e second string.
2e190 20 69 2e 65 2e 20 28 53 54 52 49 4e 47 31 20 2d   i.e. (STRING1 -
2e1a0 20 53 54 52 49 4e 47 32 29 2e 0a 2a 2a 0a 2a 2a   STRING2)..**.**
2e1b0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 72 65   The sqlite3_cre
2e1c0 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32  ate_collation_v2
2e1d0 28 29 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73 71  () works like sq
2e1e0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
2e1f0 6c 61 74 69 6f 6e 28 29 0a 2a 2a 20 65 78 63 65  lation().** exce
2e200 70 74 20 74 68 61 74 20 69 74 20 74 61 6b 65 73  pt that it takes
2e210 20 61 6e 20 65 78 74 72 61 20 61 72 67 75 6d 65   an extra argume
2e220 6e 74 20 77 68 69 63 68 20 69 73 20 61 20 64 65  nt which is a de
2e230 73 74 72 75 63 74 6f 72 20 66 6f 72 0a 2a 2a 20  structor for.** 
2e240 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 2e 20 20  the collation.  
2e250 54 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 69  The destructor i
2e260 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68  s called when th
2e270 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 69 73 0a 2a  e collation is.*
2e280 2a 20 64 65 73 74 72 6f 79 65 64 20 61 6e 64 20  * destroyed and 
2e290 69 73 20 70 61 73 73 65 64 20 61 20 63 6f 70 79  is passed a copy
2e2a0 20 6f 66 20 74 68 65 20 66 6f 75 72 74 68 20 70   of the fourth p
2e2b0 61 72 61 6d 65 74 65 72 20 76 6f 69 64 2a 20 70  arameter void* p
2e2c0 6f 69 6e 74 65 72 0a 2a 2a 20 6f 66 20 74 68 65  ointer.** of the
2e2d0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
2e2e0 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 2e 0a  collation_v2()..
2e2f0 2a 2a 20 43 6f 6c 6c 61 74 69 6f 6e 73 20 61 72  ** Collations ar
2e300 65 20 64 65 73 74 72 6f 79 65 64 20 77 68 65 6e  e destroyed when
2e310 20 74 68 65 79 20 61 72 65 20 6f 76 65 72 72 69   they are overri
2e320 64 64 65 6e 20 62 79 20 6c 61 74 65 72 20 63 61  dden by later ca
2e330 6c 6c 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63 6f  lls to the.** co
2e340 6c 6c 61 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e  llation creation
2e350 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20 77 68   functions or wh
2e360 65 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73 65  en the [database
2e370 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 73 20   connection] is 
2e380 63 6c 6f 73 65 64 0a 2a 2a 20 75 73 69 6e 67 20  closed.** using 
2e390 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29  [sqlite3_close()
2e3a0 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  ]..**.** See als
2e3b0 6f 3a 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c  o:  [sqlite3_col
2e3c0 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 5d  lation_needed()]
2e3d0 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 6f   and [sqlite3_co
2e3e0 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36  llation_needed16
2e3f0 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  ()]..**.** Requi
2e400 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36  rements:.** [H16
2e410 36 30 33 5d 20 5b 48 31 36 36 30 34 5d 20 5b 48  603] [H16604] [H
2e420 31 36 36 30 36 5d 20 5b 48 31 36 36 30 39 5d 20  16606] [H16609] 
2e430 5b 48 31 36 36 31 32 5d 20 5b 48 31 36 36 31 35  [H16612] [H16615
2e440 5d 20 5b 48 31 36 36 31 38 5d 20 5b 48 31 36 36  ] [H16618] [H166
2e450 32 31 5d 0a 2a 2a 20 5b 48 31 36 36 32 34 5d 20  21].** [H16624] 
2e460 5b 48 31 36 36 32 37 5d 20 5b 48 31 36 36 33 30  [H16627] [H16630
2e470 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
2e480 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61  int sqlite3_crea
2e490 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20  te_collation(.  
2e4a0 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e  sqlite3*, .  con
2e4b0 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  st char *zName, 
2e4c0 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c  .  int eTextRep,
2e4d0 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74   .  void*,.  int
2e4e0 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64  (*xCompare)(void
2e4f0 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64  *,int,const void
2e500 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64  *,int,const void
2e510 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  *).);.SQLITE_API
2e520 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65   int sqlite3_cre
2e530 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32  ate_collation_v2
2e540 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20  (.  sqlite3*, . 
2e550 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
2e560 6d 65 2c 20 0a 20 20 69 6e 74 20 65 54 65 78 74  me, .  int eText
2e570 52 65 70 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a 20  Rep, .  void*,. 
2e580 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28   int(*xCompare)(
2e590 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
2e5a0 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
2e5b0 76 6f 69 64 2a 29 2c 0a 20 20 76 6f 69 64 28 2a  void*),.  void(*
2e5c0 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64 2a 29  xDestroy)(void*)
2e5d0 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .);.SQLITE_API i
2e5e0 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  nt sqlite3_creat
2e5f0 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 0a 20  e_collation16(. 
2e600 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f   sqlite3*, .  co
2e610 6e 73 74 20 76 6f 69 64 20 2a 7a 4e 61 6d 65 2c  nst void *zName,
2e620 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c  .  int eTextRep,
2e630 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74   .  void*,.  int
2e640 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64  (*xCompare)(void
2e650 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64  *,int,const void
2e660 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64  *,int,const void
2e670 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  *).);../*.** CAP
2e680 49 33 52 45 46 3a 20 43 6f 6c 6c 61 74 69 6f 6e  I3REF: Collation
2e690 20 4e 65 65 64 65 64 20 43 61 6c 6c 62 61 63 6b   Needed Callback
2e6a0 73 20 7b 48 31 36 37 30 30 7d 20 3c 53 32 30 33  s {H16700} <S203
2e6b0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f  00>.**.** To avo
2e6c0 69 64 20 68 61 76 69 6e 67 20 74 6f 20 72 65 67  id having to reg
2e6d0 69 73 74 65 72 20 61 6c 6c 20 63 6f 6c 6c 61 74  ister all collat
2e6e0 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 20 62 65  ion sequences be
2e6f0 66 6f 72 65 20 61 20 64 61 74 61 62 61 73 65 0a  fore a database.
2e700 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 2c 20  ** can be used, 
2e710 61 20 73 69 6e 67 6c 65 20 63 61 6c 6c 62 61 63  a single callbac
2e720 6b 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62  k function may b
2e730 65 20 72 65 67 69 73 74 65 72 65 64 20 77 69 74  e registered wit
2e740 68 20 74 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61  h the.** [databa
2e750 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74  se connection] t
2e760 6f 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e  o be called when
2e770 65 76 65 72 20 61 6e 20 75 6e 64 65 66 69 6e 65  ever an undefine
2e780 64 20 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73  d collation.** s
2e790 65 71 75 65 6e 63 65 20 69 73 20 72 65 71 75 69  equence is requi
2e7a0 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  red..**.** If th
2e7b0 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65  e function is re
2e7c0 67 69 73 74 65 72 65 64 20 75 73 69 6e 67 20 74  gistered using t
2e7d0 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61  he sqlite3_colla
2e7e0 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 20 41 50  tion_needed() AP
2e7f0 49 2c 0a 2a 2a 20 74 68 65 6e 20 69 74 20 69 73  I,.** then it is
2e800 20 70 61 73 73 65 64 20 74 68 65 20 6e 61 6d 65   passed the name
2e810 73 20 6f 66 20 75 6e 64 65 66 69 6e 65 64 20 63  s of undefined c
2e820 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
2e830 65 73 20 61 73 20 73 74 72 69 6e 67 73 0a 2a 2a  es as strings.**
2e840 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d   encoded in UTF-
2e850 38 2e 20 7b 48 31 36 37 30 33 7d 20 49 66 20 73  8. {H16703} If s
2e860 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e  qlite3_collation
2e870 5f 6e 65 65 64 65 64 31 36 28 29 20 69 73 20 75  _needed16() is u
2e880 73 65 64 2c 0a 2a 2a 20 74 68 65 20 6e 61 6d 65  sed,.** the name
2e890 73 20 61 72 65 20 70 61 73 73 65 64 20 61 73 20  s are passed as 
2e8a0 55 54 46 2d 31 36 20 69 6e 20 6d 61 63 68 69 6e  UTF-16 in machin
2e8b0 65 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72  e native byte or
2e8c0 64 65 72 2e 0a 2a 2a 20 41 20 63 61 6c 6c 20 74  der..** A call t
2e8d0 6f 20 65 69 74 68 65 72 20 66 75 6e 63 74 69 6f  o either functio
2e8e0 6e 20 72 65 70 6c 61 63 65 73 20 61 6e 79 20 65  n replaces any e
2e8f0 78 69 73 74 69 6e 67 20 63 61 6c 6c 62 61 63 6b  xisting callback
2e900 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65  ..**.** When the
2e910 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76   callback is inv
2e920 6f 6b 65 64 2c 20 74 68 65 20 66 69 72 73 74 20  oked, the first 
2e930 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20  argument passed 
2e940 69 73 20 61 20 63 6f 70 79 0a 2a 2a 20 6f 66 20  is a copy.** of 
2e950 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
2e960 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 63  ent to sqlite3_c
2e970 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28  ollation_needed(
2e980 29 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  ) or.** sqlite3_
2e990 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
2e9a0 31 36 28 29 2e 20 20 54 68 65 20 73 65 63 6f 6e  16().  The secon
2e9b0 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68  d argument is th
2e9c0 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f  e database.** co
2e9d0 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 74  nnection.  The t
2e9e0 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73  hird argument is
2e9f0 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f   one of [SQLITE_
2ea00 55 54 46 38 5d 2c 20 5b 53 51 4c 49 54 45 5f 55  UTF8], [SQLITE_U
2ea10 54 46 31 36 42 45 5d 2c 0a 2a 2a 20 6f 72 20 5b  TF16BE],.** or [
2ea20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d 2c  SQLITE_UTF16LE],
2ea30 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20   indicating the 
2ea40 6d 6f 73 74 20 64 65 73 69 72 61 62 6c 65 20 66  most desirable f
2ea50 6f 72 6d 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61  orm of the colla
2ea60 74 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65  tion.** sequence
2ea70 20 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 72   function requir
2ea80 65 64 2e 20 20 54 68 65 20 66 6f 75 72 74 68 20  ed.  The fourth 
2ea90 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65  parameter is the
2eaa0 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20   name of the.** 
2eab0 72 65 71 75 69 72 65 64 20 63 6f 6c 6c 61 74 69  required collati
2eac0 6f 6e 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a  on sequence..**.
2ead0 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20  ** The callback 
2eae0 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20  function should 
2eaf0 72 65 67 69 73 74 65 72 20 74 68 65 20 64 65 73  register the des
2eb00 69 72 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 75  ired collation u
2eb10 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  sing.** [sqlite3
2eb20 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
2eb30 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63  n()], [sqlite3_c
2eb40 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31  reate_collation1
2eb50 36 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 71 6c  6()], or.** [sql
2eb60 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c  ite3_create_coll
2eb70 61 74 69 6f 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a  ation_v2()]..**.
2eb80 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
2eb90 0a 2a 2a 20 5b 48 31 36 37 30 32 5d 20 5b 48 31  .** [H16702] [H1
2eba0 36 37 30 34 5d 20 5b 48 31 36 37 30 36 5d 0a 2a  6704] [H16706].*
2ebb0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
2ebc0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69   sqlite3_collati
2ebd0 6f 6e 5f 6e 65 65 64 65 64 28 0a 20 20 73 71 6c  on_needed(.  sql
2ebe0 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64 2a 2c  ite3*, .  void*,
2ebf0 20 0a 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64   .  void(*)(void
2ec00 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65  *,sqlite3*,int e
2ec10 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 63 68  TextRep,const ch
2ec20 61 72 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41  ar*).);.SQLITE_A
2ec30 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
2ec40 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31  ollation_needed1
2ec50 36 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a  6(.  sqlite3*, .
2ec60 20 20 76 6f 69 64 2a 2c 0a 20 20 76 6f 69 64 28    void*,.  void(
2ec70 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33  *)(void*,sqlite3
2ec80 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63  *,int eTextRep,c
2ec90 6f 6e 73 74 20 76 6f 69 64 2a 29 0a 29 3b 0a 0a  onst void*).);..
2eca0 2f 2a 0a 2a 2a 20 53 70 65 63 69 66 79 20 74 68  /*.** Specify th
2ecb0 65 20 6b 65 79 20 66 6f 72 20 61 6e 20 65 6e 63  e key for an enc
2ecc0 72 79 70 74 65 64 20 64 61 74 61 62 61 73 65 2e  rypted database.
2ecd0 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73    This routine s
2ece0 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 63 61 6c 6c  hould be.** call
2ecf0 65 64 20 72 69 67 68 74 20 61 66 74 65 72 20 73  ed right after s
2ed00 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 2e 0a 2a  qlite3_open()..*
2ed10 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 74 6f  *.** The code to
2ed20 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 69 73 20   implement this 
2ed30 41 50 49 20 69 73 20 6e 6f 74 20 61 76 61 69 6c  API is not avail
2ed40 61 62 6c 65 20 69 6e 20 74 68 65 20 70 75 62 6c  able in the publ
2ed50 69 63 20 72 65 6c 65 61 73 65 0a 2a 2a 20 6f 66  ic release.** of
2ed60 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51 4c 49   SQLite..*/.SQLI
2ed70 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
2ed80 65 33 5f 6b 65 79 28 0a 20 20 73 71 6c 69 74 65  e3_key(.  sqlite
2ed90 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
2eda0 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
2edb0 62 61 73 65 20 74 6f 20 62 65 20 72 65 6b 65 79  base to be rekey
2edc0 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f  ed */.  const vo
2edd0 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b  id *pKey, int nK
2ede0 65 79 20 20 20 20 20 2f 2a 20 54 68 65 20 6b 65  ey     /* The ke
2edf0 79 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  y */.);../*.** C
2ee00 68 61 6e 67 65 20 74 68 65 20 6b 65 79 20 6f 6e  hange the key on
2ee10 20 61 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73   an open databas
2ee20 65 2e 20 20 49 66 20 74 68 65 20 63 75 72 72 65  e.  If the curre
2ee30 6e 74 20 64 61 74 61 62 61 73 65 20 69 73 20 6e  nt database is n
2ee40 6f 74 0a 2a 2a 20 65 6e 63 72 79 70 74 65 64 2c  ot.** encrypted,
2ee50 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69   this routine wi
2ee60 6c 6c 20 65 6e 63 72 79 70 74 20 69 74 2e 20 20  ll encrypt it.  
2ee70 49 66 20 70 4e 65 77 3d 3d 30 20 6f 72 20 6e 4e  If pNew==0 or nN
2ee80 65 77 3d 3d 30 2c 20 74 68 65 0a 2a 2a 20 64 61  ew==0, the.** da
2ee90 74 61 62 61 73 65 20 69 73 20 64 65 63 72 79 70  tabase is decryp
2eea0 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  ted..**.** The c
2eeb0 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  ode to implement
2eec0 20 74 68 69 73 20 41 50 49 20 69 73 20 6e 6f 74   this API is not
2eed0 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68   available in th
2eee0 65 20 70 75 62 6c 69 63 20 72 65 6c 65 61 73 65  e public release
2eef0 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a  .** of SQLite..*
2ef00 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
2ef10 20 73 71 6c 69 74 65 33 5f 72 65 6b 65 79 28 0a   sqlite3_rekey(.
2ef20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
2ef30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ef40 20 2f 2a 20 44 61 74 61 62 61 73 65 20 74 6f 20   /* Database to 
2ef50 62 65 20 72 65 6b 65 79 65 64 20 2a 2f 0a 20 20  be rekeyed */.  
2ef60 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
2ef70 2c 20 69 6e 74 20 6e 4b 65 79 20 20 20 20 20 2f  , int nKey     /
2ef80 2a 20 54 68 65 20 6e 65 77 20 6b 65 79 20 2a 2f  * The new key */
2ef90 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  .);../*.** CAPI3
2efa0 52 45 46 3a 20 53 75 73 70 65 6e 64 20 45 78 65  REF: Suspend Exe
2efb0 63 75 74 69 6f 6e 20 46 6f 72 20 41 20 53 68 6f  cution For A Sho
2efc0 72 74 20 54 69 6d 65 20 7b 48 31 30 35 33 30 7d  rt Time {H10530}
2efd0 20 3c 53 34 30 34 31 30 3e 0a 2a 2a 0a 2a 2a 20   <S40410>.**.** 
2efe0 54 68 65 20 73 71 6c 69 74 65 33 5f 73 6c 65 65  The sqlite3_slee
2eff0 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 63 61 75  p() function cau
2f000 73 65 73 20 74 68 65 20 63 75 72 72 65 6e 74 20  ses the current 
2f010 74 68 72 65 61 64 20 74 6f 20 73 75 73 70 65 6e  thread to suspen
2f020 64 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 66  d execution.** f
2f030 6f 72 20 61 74 20 6c 65 61 73 74 20 61 20 6e 75  or at least a nu
2f040 6d 62 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65 63  mber of millisec
2f050 6f 6e 64 73 20 73 70 65 63 69 66 69 65 64 20 69  onds specified i
2f060 6e 20 69 74 73 20 70 61 72 61 6d 65 74 65 72 2e  n its parameter.
2f070 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70  .**.** If the op
2f080 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 64  erating system d
2f090 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 20  oes not support 
2f0a0 73 6c 65 65 70 20 72 65 71 75 65 73 74 73 20 77  sleep requests w
2f0b0 69 74 68 0a 2a 2a 20 6d 69 6c 6c 69 73 65 63 6f  ith.** milliseco
2f0c0 6e 64 20 74 69 6d 65 20 72 65 73 6f 6c 75 74 69  nd time resoluti
2f0d0 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 74 69 6d  on, then the tim
2f0e0 65 20 77 69 6c 6c 20 62 65 20 72 6f 75 6e 64 65  e will be rounde
2f0f0 64 20 75 70 20 74 6f 0a 2a 2a 20 74 68 65 20 6e  d up to.** the n
2f100 65 61 72 65 73 74 20 73 65 63 6f 6e 64 2e 20 54  earest second. T
2f110 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6c  he number of mil
2f120 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65  liseconds of sle
2f130 65 70 20 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 72  ep actually.** r
2f140 65 71 75 65 73 74 65 64 20 66 72 6f 6d 20 74 68  equested from th
2f150 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74  e operating syst
2f160 65 6d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  em is returned..
2f170 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6d 70  **.** SQLite imp
2f180 6c 65 6d 65 6e 74 73 20 74 68 69 73 20 69 6e 74  lements this int
2f190 65 72 66 61 63 65 20 62 79 20 63 61 6c 6c 69 6e  erface by callin
2f1a0 67 20 74 68 65 20 78 53 6c 65 65 70 28 29 0a 2a  g the xSleep().*
2f1b0 2a 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20  * method of the 
2f1c0 64 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33  default [sqlite3
2f1d0 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a  _vfs] object..**
2f1e0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
2f1f0 3a 20 5b 48 31 30 35 33 33 5d 20 5b 48 31 30 35  : [H10533] [H105
2f200 33 36 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  36].*/.SQLITE_AP
2f210 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 6c  I int sqlite3_sl
2f220 65 65 70 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a  eep(int);../*.**
2f230 20 43 41 50 49 33 52 45 46 3a 20 4e 61 6d 65 20   CAPI3REF: Name 
2f240 4f 66 20 54 68 65 20 46 6f 6c 64 65 72 20 48 6f  Of The Folder Ho
2f250 6c 64 69 6e 67 20 54 65 6d 70 6f 72 61 72 79 20  lding Temporary 
2f260 46 69 6c 65 73 20 7b 48 31 30 33 31 30 7d 20 3c  Files {H10310} <
2f270 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 49 66  S20000>.**.** If
2f280 20 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 72   this global var
2f290 69 61 62 6c 65 20 69 73 20 6d 61 64 65 20 74 6f  iable is made to
2f2a0 20 70 6f 69 6e 74 20 74 6f 20 61 20 73 74 72 69   point to a stri
2f2b0 6e 67 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 74  ng which is.** t
2f2c0 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 66 6f 6c  he name of a fol
2f2d0 64 65 72 20 28 61 2e 6b 2e 61 2e 20 64 69 72 65  der (a.k.a. dire
2f2e0 63 74 6f 72 79 29 2c 20 74 68 65 6e 20 61 6c 6c  ctory), then all
2f2f0 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73   temporary files
2f300 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 53  .** created by S
2f310 51 4c 69 74 65 20 77 69 6c 6c 20 62 65 20 70 6c  QLite will be pl
2f320 61 63 65 64 20 69 6e 20 74 68 61 74 20 64 69 72  aced in that dir
2f330 65 63 74 6f 72 79 2e 20 20 49 66 20 74 68 69 73  ectory.  If this
2f340 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 73 20   variable.** is 
2f350 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20  a NULL pointer, 
2f360 74 68 65 6e 20 53 51 4c 69 74 65 20 70 65 72 66  then SQLite perf
2f370 6f 72 6d 73 20 61 20 73 65 61 72 63 68 20 66 6f  orms a search fo
2f380 72 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65  r an appropriate
2f390 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 66 69  .** temporary fi
2f3a0 6c 65 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a 2a  le directory..**
2f3b0 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 73 61  .** It is not sa
2f3c0 66 65 20 74 6f 20 72 65 61 64 20 6f 72 20 6d 6f  fe to read or mo
2f3d0 64 69 66 79 20 74 68 69 73 20 76 61 72 69 61 62  dify this variab
2f3e0 6c 65 20 69 6e 20 6d 6f 72 65 20 74 68 61 6e 20  le in more than 
2f3f0 6f 6e 65 0a 2a 2a 20 74 68 72 65 61 64 20 61 74  one.** thread at
2f400 20 61 20 74 69 6d 65 2e 20 20 49 74 20 69 73 20   a time.  It is 
2f410 6e 6f 74 20 73 61 66 65 20 74 6f 20 72 65 61 64  not safe to read
2f420 20 6f 72 20 6d 6f 64 69 66 79 20 74 68 69 73 20   or modify this 
2f430 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 66 20 61  variable.** if a
2f440 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
2f450 63 74 69 6f 6e 5d 20 69 73 20 62 65 69 6e 67 20  ction] is being 
2f460 75 73 65 64 20 61 74 20 74 68 65 20 73 61 6d 65  used at the same
2f470 20 74 69 6d 65 20 69 6e 20 61 20 73 65 70 61 72   time in a separ
2f480 61 74 65 0a 2a 2a 20 74 68 72 65 61 64 2e 0a 2a  ate.** thread..*
2f490 2a 20 49 74 20 69 73 20 69 6e 74 65 6e 64 65 64  * It is intended
2f4a0 20 74 68 61 74 20 74 68 69 73 20 76 61 72 69 61   that this varia
2f4b0 62 6c 65 20 62 65 20 73 65 74 20 6f 6e 63 65 0a  ble be set once.
2f4c0 2a 2a 20 61 73 20 70 61 72 74 20 6f 66 20 70 72  ** as part of pr
2f4d0 6f 63 65 73 73 20 69 6e 69 74 69 61 6c 69 7a 61  ocess initializa
2f4e0 74 69 6f 6e 20 61 6e 64 20 62 65 66 6f 72 65 20  tion and before 
2f4f0 61 6e 79 20 53 51 4c 69 74 65 20 69 6e 74 65 72  any SQLite inter
2f500 66 61 63 65 0a 2a 2a 20 72 6f 75 74 69 6e 65 73  face.** routines
2f510 20 68 61 76 65 20 62 65 65 6e 20 63 61 6c 6c 65   have been calle
2f520 64 20 61 6e 64 20 74 68 61 74 20 74 68 69 73 20  d and that this 
2f530 76 61 72 69 61 62 6c 65 20 72 65 6d 61 69 6e 20  variable remain 
2f540 75 6e 63 68 61 6e 67 65 64 0a 2a 2a 20 74 68 65  unchanged.** the
2f550 72 65 61 66 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54  reafter..**.** T
2f560 68 65 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64  he [temp_store_d
2f570 69 72 65 63 74 6f 72 79 20 70 72 61 67 6d 61 5d  irectory pragma]
2f580 20 6d 61 79 20 6d 6f 64 69 66 79 20 74 68 69 73   may modify this
2f590 20 76 61 72 69 61 62 6c 65 20 61 6e 64 20 63 61   variable and ca
2f5a0 75 73 65 0a 2a 2a 20 69 74 20 74 6f 20 70 6f 69  use.** it to poi
2f5b0 6e 74 20 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74  nt to memory obt
2f5c0 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69  ained from [sqli
2f5d0 74 65 33 5f 6d 61 6c 6c 6f 63 5d 2e 20 20 46 75  te3_malloc].  Fu
2f5e0 72 74 68 65 72 6d 6f 72 65 2c 0a 2a 2a 20 74 68  rthermore,.** th
2f5f0 65 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69  e [temp_store_di
2f600 72 65 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20  rectory pragma] 
2f610 61 6c 77 61 79 73 20 61 73 73 75 6d 65 73 20 74  always assumes t
2f620 68 61 74 20 61 6e 79 20 73 74 72 69 6e 67 0a 2a  hat any string.*
2f630 2a 20 74 68 61 74 20 74 68 69 73 20 76 61 72 69  * that this vari
2f640 61 62 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 69  able points to i
2f650 73 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79  s held in memory
2f660 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 0a   obtained from .
2f670 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  ** [sqlite3_mall
2f680 6f 63 5d 20 61 6e 64 20 74 68 65 20 70 72 61 67  oc] and the prag
2f690 6d 61 20 6d 61 79 20 61 74 74 65 6d 70 74 20 74  ma may attempt t
2f6a0 6f 20 66 72 65 65 20 74 68 61 74 20 6d 65 6d 6f  o free that memo
2f6b0 72 79 0a 2a 2a 20 75 73 69 6e 67 20 5b 73 71 6c  ry.** using [sql
2f6c0 69 74 65 33 5f 66 72 65 65 5d 2e 0a 2a 2a 20 48  ite3_free]..** H
2f6d0 65 6e 63 65 2c 20 69 66 20 74 68 69 73 20 76 61  ence, if this va
2f6e0 72 69 61 62 6c 65 20 69 73 20 6d 6f 64 69 66 69  riable is modifi
2f6f0 65 64 20 64 69 72 65 63 74 6c 79 2c 20 65 69 74  ed directly, eit
2f700 68 65 72 20 69 74 20 73 68 6f 75 6c 64 20 62 65  her it should be
2f710 0a 2a 2a 20 6d 61 64 65 20 4e 55 4c 4c 20 6f 72  .** made NULL or
2f720 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74   made to point t
2f730 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65  o memory obtaine
2f740 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f  d from [sqlite3_
2f750 6d 61 6c 6c 6f 63 5d 0a 2a 2a 20 6f 72 20 65 6c  malloc].** or el
2f760 73 65 20 74 68 65 20 75 73 65 20 6f 66 20 74 68  se the use of th
2f770 65 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69  e [temp_store_di
2f780 72 65 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20  rectory pragma] 
2f790 73 68 6f 75 6c 64 20 62 65 20 61 76 6f 69 64 65  should be avoide
2f7a0 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  d..*/.SQLITE_API
2f7b0 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 74   char *sqlite3_t
2f7c0 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 3b 0a 0a  emp_directory;..
2f7d0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
2f7e0 54 65 73 74 20 46 6f 72 20 41 75 74 6f 2d 43 6f  Test For Auto-Co
2f7f0 6d 6d 69 74 20 4d 6f 64 65 20 7b 48 31 32 39 33  mmit Mode {H1293
2f800 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 4b  0} <S60200>.** K
2f810 45 59 57 4f 52 44 53 3a 20 7b 61 75 74 6f 63 6f  EYWORDS: {autoco
2f820 6d 6d 69 74 20 6d 6f 64 65 7d 0a 2a 2a 0a 2a 2a  mmit mode}.**.**
2f830 20 54 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74   The sqlite3_get
2f840 5f 61 75 74 6f 63 6f 6d 6d 69 74 28 29 20 69 6e  _autocommit() in
2f850 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20  terface returns 
2f860 6e 6f 6e 2d 7a 65 72 6f 20 6f 72 0a 2a 2a 20 7a  non-zero or.** z
2f870 65 72 6f 20 69 66 20 74 68 65 20 67 69 76 65 6e  ero if the given
2f880 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
2f890 74 69 6f 6e 20 69 73 20 6f 72 20 69 73 20 6e 6f  tion is or is no
2f8a0 74 20 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 74 20  t in autocommit 
2f8b0 6d 6f 64 65 2c 0a 2a 2a 20 72 65 73 70 65 63 74  mode,.** respect
2f8c0 69 76 65 6c 79 2e 20 20 41 75 74 6f 63 6f 6d 6d  ively.  Autocomm
2f8d0 69 74 20 6d 6f 64 65 20 69 73 20 6f 6e 20 62 79  it mode is on by
2f8e0 20 64 65 66 61 75 6c 74 2e 0a 2a 2a 20 41 75 74   default..** Aut
2f8f0 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20  ocommit mode is 
2f900 64 69 73 61 62 6c 65 64 20 62 79 20 61 20 5b 42  disabled by a [B
2f910 45 47 49 4e 5d 20 73 74 61 74 65 6d 65 6e 74 2e  EGIN] statement.
2f920 0a 2a 2a 20 41 75 74 6f 63 6f 6d 6d 69 74 20 6d  .** Autocommit m
2f930 6f 64 65 20 69 73 20 72 65 2d 65 6e 61 62 6c 65  ode is re-enable
2f940 64 20 62 79 20 61 20 5b 43 4f 4d 4d 49 54 5d 20  d by a [COMMIT] 
2f950 6f 72 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2e 0a 2a  or [ROLLBACK]..*
2f960 2a 0a 2a 2a 20 49 66 20 63 65 72 74 61 69 6e 20  *.** If certain 
2f970 6b 69 6e 64 73 20 6f 66 20 65 72 72 6f 72 73 20  kinds of errors 
2f980 6f 63 63 75 72 20 6f 6e 20 61 20 73 74 61 74 65  occur on a state
2f990 6d 65 6e 74 20 77 69 74 68 69 6e 20 61 20 6d 75  ment within a mu
2f9a0 6c 74 69 2d 73 74 61 74 65 6d 65 6e 74 0a 2a 2a  lti-statement.**
2f9b0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 28 65 72   transaction (er
2f9c0 72 6f 72 73 20 69 6e 63 6c 75 64 69 6e 67 20 5b  rors including [
2f9d0 53 51 4c 49 54 45 5f 46 55 4c 4c 5d 2c 20 5b 53  SQLITE_FULL], [S
2f9e0 51 4c 49 54 45 5f 49 4f 45 52 52 5d 2c 0a 2a 2a  QLITE_IOERR],.**
2f9f0 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2c   [SQLITE_NOMEM],
2fa00 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 2c 20   [SQLITE_BUSY], 
2fa10 61 6e 64 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45  and [SQLITE_INTE
2fa20 52 52 55 50 54 5d 29 20 74 68 65 6e 20 74 68 65  RRUPT]) then the
2fa30 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  .** transaction 
2fa40 6d 69 67 68 74 20 62 65 20 72 6f 6c 6c 65 64 20  might be rolled 
2fa50 62 61 63 6b 20 61 75 74 6f 6d 61 74 69 63 61 6c  back automatical
2fa60 6c 79 2e 20 20 54 68 65 20 6f 6e 6c 79 20 77 61  ly.  The only wa
2fa70 79 20 74 6f 0a 2a 2a 20 66 69 6e 64 20 6f 75 74  y to.** find out
2fa80 20 77 68 65 74 68 65 72 20 53 51 4c 69 74 65 20   whether SQLite 
2fa90 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 6f  automatically ro
2faa0 6c 6c 65 64 20 62 61 63 6b 20 74 68 65 20 74 72  lled back the tr
2fab0 61 6e 73 61 63 74 69 6f 6e 20 61 66 74 65 72 0a  ansaction after.
2fac0 2a 2a 20 61 6e 20 65 72 72 6f 72 20 69 73 20 74  ** an error is t
2fad0 6f 20 75 73 65 20 74 68 69 73 20 66 75 6e 63 74  o use this funct
2fae0 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  ion..**.** If an
2faf0 6f 74 68 65 72 20 74 68 72 65 61 64 20 63 68 61  other thread cha
2fb00 6e 67 65 73 20 74 68 65 20 61 75 74 6f 63 6f 6d  nges the autocom
2fb10 6d 69 74 20 73 74 61 74 75 73 20 6f 66 20 74 68  mit status of th
2fb20 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f  e database.** co
2fb30 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 74  nnection while t
2fb40 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 72  his routine is r
2fb50 75 6e 6e 69 6e 67 2c 20 74 68 65 6e 20 74 68 65  unning, then the
2fb60 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a   return value.**
2fb70 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a   is undefined..*
2fb80 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
2fb90 73 3a 20 5b 48 31 32 39 33 31 5d 20 5b 48 31 32  s: [H12931] [H12
2fba0 39 33 32 5d 20 5b 48 31 32 39 33 33 5d 20 5b 48  932] [H12933] [H
2fbb0 31 32 39 33 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45  12934].*/.SQLITE
2fbc0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
2fbd0 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 28  _get_autocommit(
2fbe0 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a  sqlite3*);../*.*
2fbf0 2a 20 43 41 50 49 33 52 45 46 3a 20 46 69 6e 64  * CAPI3REF: Find
2fc00 20 54 68 65 20 44 61 74 61 62 61 73 65 20 48 61   The Database Ha
2fc10 6e 64 6c 65 20 4f 66 20 41 20 50 72 65 70 61 72  ndle Of A Prepar
2fc20 65 64 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 31  ed Statement {H1
2fc30 33 31 32 30 7d 20 3c 53 36 30 36 30 30 3e 0a 2a  3120} <S60600>.*
2fc40 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
2fc50 5f 64 62 5f 68 61 6e 64 6c 65 20 69 6e 74 65 72  _db_handle inter
2fc60 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65  face returns the
2fc70 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
2fc80 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 0a 2a 2a  ction] handle.**
2fc90 20 74 6f 20 77 68 69 63 68 20 61 20 5b 70 72 65   to which a [pre
2fca0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
2fcb0 20 62 65 6c 6f 6e 67 73 2e 20 20 54 68 65 20 5b   belongs.  The [
2fcc0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
2fcd0 69 6f 6e 5d 0a 2a 2a 20 72 65 74 75 72 6e 65 64  ion].** returned
2fce0 20 62 79 20 73 71 6c 69 74 65 33 5f 64 62 5f 68   by sqlite3_db_h
2fcf0 61 6e 64 6c 65 20 69 73 20 74 68 65 20 73 61 6d  andle is the sam
2fd00 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  e [database conn
2fd10 65 63 74 69 6f 6e 5d 20 74 68 61 74 20 77 61 73  ection] that was
2fd20 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
2fd30 65 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20 5b 73  ent.** to the [s
2fd40 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
2fd50 32 28 29 5d 20 63 61 6c 6c 20 28 6f 72 20 69 74  2()] call (or it
2fd60 73 20 76 61 72 69 61 6e 74 73 29 20 74 68 61 74  s variants) that
2fd70 20 77 61 73 20 75 73 65 64 20 74 6f 0a 2a 2a 20   was used to.** 
2fd80 63 72 65 61 74 65 20 74 68 65 20 73 74 61 74 65  create the state
2fd90 6d 65 6e 74 20 69 6e 20 74 68 65 20 66 69 72 73  ment in the firs
2fda0 74 20 70 6c 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 52  t place..**.** R
2fdb0 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31  equirements: [H1
2fdc0 33 31 32 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f  3123].*/.SQLITE_
2fdd0 41 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c  API sqlite3 *sql
2fde0 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 28 73  ite3_db_handle(s
2fdf0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a  qlite3_stmt*);..
2fe00 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
2fe10 46 69 6e 64 20 74 68 65 20 6e 65 78 74 20 70 72  Find the next pr
2fe20 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
2fe30 20 7b 48 31 33 31 34 30 7d 20 3c 53 36 30 36 30   {H13140} <S6060
2fe40 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  0>.**.** This in
2fe50 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20  terface returns 
2fe60 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
2fe70 20 6e 65 78 74 20 5b 70 72 65 70 61 72 65 64 20   next [prepared 
2fe80 73 74 61 74 65 6d 65 6e 74 5d 20 61 66 74 65 72  statement] after
2fe90 0a 2a 2a 20 70 53 74 6d 74 20 61 73 73 6f 63 69  .** pStmt associ
2fea0 61 74 65 64 20 77 69 74 68 20 74 68 65 20 5b 64  ated with the [d
2feb0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
2fec0 6f 6e 5d 20 70 44 62 2e 20 20 49 66 20 70 53 74  on] pDb.  If pSt
2fed0 6d 74 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68  mt is NULL.** th
2fee0 65 6e 20 74 68 69 73 20 69 6e 74 65 72 66 61 63  en this interfac
2fef0 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e  e returns a poin
2ff00 74 65 72 20 74 6f 20 74 68 65 20 66 69 72 73 74  ter to the first
2ff10 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
2ff20 65 6e 74 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65  ent.** associate
2ff30 64 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62  d with the datab
2ff40 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70  ase connection p
2ff50 44 62 2e 20 20 49 66 20 6e 6f 20 70 72 65 70 61  Db.  If no prepa
2ff60 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a  red statement.**
2ff70 20 73 61 74 69 73 66 69 65 73 20 74 68 65 20 63   satisfies the c
2ff80 6f 6e 64 69 74 69 6f 6e 73 20 6f 66 20 74 68 69  onditions of thi
2ff90 73 20 72 6f 75 74 69 6e 65 2c 20 69 74 20 72 65  s routine, it re
2ffa0 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a  turns NULL..**.*
2ffb0 2a 20 54 68 65 20 5b 64 61 74 61 62 61 73 65 20  * The [database 
2ffc0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 70 6f 69 6e  connection] poin
2ffd0 74 65 72 20 44 20 69 6e 20 61 20 63 61 6c 6c 20  ter D in a call 
2ffe0 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6e  to.** [sqlite3_n
2fff0 65 78 74 5f 73 74 6d 74 28 44 2c 53 29 5d 20 6d  ext_stmt(D,S)] m
30000 75 73 74 20 72 65 66 65 72 20 74 6f 20 61 6e 20  ust refer to an 
30010 6f 70 65 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a  open database.**
30020 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 6e 64 20   connection and 
30030 69 6e 20 70 61 72 74 69 63 75 6c 61 72 20 6d 75  in particular mu
30040 73 74 20 6e 6f 74 20 62 65 20 61 20 4e 55 4c 4c  st not be a NULL
30050 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20   pointer..**.** 
30060 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48  Requirements: [H
30070 31 33 31 34 33 5d 20 5b 48 31 33 31 34 36 5d 20  13143] [H13146] 
30080 5b 48 31 33 31 34 39 5d 20 5b 48 31 33 31 35 32  [H13149] [H13152
30090 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
300a0 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 71  sqlite3_stmt *sq
300b0 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28  lite3_next_stmt(
300c0 73 71 6c 69 74 65 33 20 2a 70 44 62 2c 20 73 71  sqlite3 *pDb, sq
300d0 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
300e0 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  t);../*.** CAPI3
300f0 52 45 46 3a 20 43 6f 6d 6d 69 74 20 41 6e 64 20  REF: Commit And 
30100 52 6f 6c 6c 62 61 63 6b 20 4e 6f 74 69 66 69 63  Rollback Notific
30110 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 20  ation Callbacks 
30120 7b 48 31 32 39 35 30 7d 20 3c 53 36 30 34 30 30  {H12950} <S60400
30130 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  >.**.** The sqli
30140 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28  te3_commit_hook(
30150 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 69  ) interface regi
30160 73 74 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b  sters a callback
30170 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  .** function to 
30180 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65  be invoked whene
30190 76 65 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f  ver a transactio
301a0 6e 20 69 73 20 5b 43 4f 4d 4d 49 54 20 7c 20 63  n is [COMMIT | c
301b0 6f 6d 6d 69 74 74 65 64 5d 2e 0a 2a 2a 20 41 6e  ommitted]..** An
301c0 79 20 63 61 6c 6c 62 61 63 6b 20 73 65 74 20 62  y callback set b
301d0 79 20 61 20 70 72 65 76 69 6f 75 73 20 63 61 6c  y a previous cal
301e0 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6d  l to sqlite3_com
301f0 6d 69 74 5f 68 6f 6f 6b 28 29 0a 2a 2a 20 66 6f  mit_hook().** fo
30200 72 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62  r the same datab
30210 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69  ase connection i
30220 73 20 6f 76 65 72 72 69 64 64 65 6e 2e 0a 2a 2a  s overridden..**
30230 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 6f 6c   The sqlite3_rol
30240 6c 62 61 63 6b 5f 68 6f 6f 6b 28 29 20 69 6e 74  lback_hook() int
30250 65 72 66 61 63 65 20 72 65 67 69 73 74 65 72 73  erface registers
30260 20 61 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 66   a callback.** f
30270 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 69 6e  unction to be in
30280 76 6f 6b 65 64 20 77 68 65 6e 65 76 65 72 20 61  voked whenever a
30290 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
302a0 5b 52 4f 4c 4c 42 41 43 4b 20 7c 20 72 6f 6c 6c  [ROLLBACK | roll
302b0 65 64 20 62 61 63 6b 5d 2e 0a 2a 2a 20 41 6e 79  ed back]..** Any
302c0 20 63 61 6c 6c 62 61 63 6b 20 73 65 74 20 62 79   callback set by
302d0 20 61 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c   a previous call
302e0 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6d 6d   to sqlite3_comm
302f0 69 74 5f 68 6f 6f 6b 28 29 0a 2a 2a 20 66 6f 72  it_hook().** for
30300 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61   the same databa
30310 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73  se connection is
30320 20 6f 76 65 72 72 69 64 64 65 6e 2e 0a 2a 2a 20   overridden..** 
30330 54 68 65 20 70 41 72 67 20 61 72 67 75 6d 65 6e  The pArg argumen
30340 74 20 69 73 20 70 61 73 73 65 64 20 74 68 72 6f  t is passed thro
30350 75 67 68 20 74 6f 20 74 68 65 20 63 61 6c 6c 62  ugh to the callb
30360 61 63 6b 2e 0a 2a 2a 20 49 66 20 74 68 65 20 63  ack..** If the c
30370 61 6c 6c 62 61 63 6b 20 6f 6e 20 61 20 63 6f 6d  allback on a com
30380 6d 69 74 20 68 6f 6f 6b 20 66 75 6e 63 74 69 6f  mit hook functio
30390 6e 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65  n returns non-ze
303a0 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20  ro,.** then the 
303b0 63 6f 6d 6d 69 74 20 69 73 20 63 6f 6e 76 65 72  commit is conver
303c0 74 65 64 20 69 6e 74 6f 20 61 20 72 6f 6c 6c 62  ted into a rollb
303d0 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  ack..**.** If an
303e0 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 20 77  other function w
303f0 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65  as previously re
30400 67 69 73 74 65 72 65 64 2c 20 69 74 73 0a 2a 2a  gistered, its.**
30410 20 70 41 72 67 20 76 61 6c 75 65 20 69 73 20 72   pArg value is r
30420 65 74 75 72 6e 65 64 2e 20 20 4f 74 68 65 72 77  eturned.  Otherw
30430 69 73 65 20 4e 55 4c 4c 20 69 73 20 72 65 74 75  ise NULL is retu
30440 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  rned..**.** The 
30450 63 61 6c 6c 62 61 63 6b 20 69 6d 70 6c 65 6d 65  callback impleme
30460 6e 74 61 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74  ntation must not
30470 20 64 6f 20 61 6e 79 74 68 69 6e 67 20 74 68 61   do anything tha
30480 74 20 77 69 6c 6c 20 6d 6f 64 69 66 79 0a 2a 2a  t will modify.**
30490 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
304a0 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 69 6e  nnection that in
304b0 76 6f 6b 65 64 20 74 68 65 20 63 61 6c 6c 62 61  voked the callba
304c0 63 6b 2e 20 20 41 6e 79 20 61 63 74 69 6f 6e 73  ck.  Any actions
304d0 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66 79 20 74 68  .** to modify th
304e0 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
304f0 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 64 65  ction must be de
30500 66 65 72 72 65 64 20 75 6e 74 69 6c 20 61 66 74  ferred until aft
30510 65 72 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c 65  er the.** comple
30520 74 69 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c  tion of the [sql
30530 69 74 65 33 5f 73 74 65 70 28 29 5d 20 63 61 6c  ite3_step()] cal
30540 6c 20 74 68 61 74 20 74 72 69 67 67 65 72 65 64  l that triggered
30550 20 74 68 65 20 63 6f 6d 6d 69 74 0a 2a 2a 20 6f   the commit.** o
30560 72 20 72 6f 6c 6c 62 61 63 6b 20 68 6f 6f 6b 20  r rollback hook 
30570 69 6e 20 74 68 65 20 66 69 72 73 74 20 70 6c 61  in the first pla
30580 63 65 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74  ce..** Note that
30590 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
305a0 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b 73 71 6c  e_v2()] and [sql
305b0 69 74 65 33 5f 73 74 65 70 28 29 5d 20 62 6f 74  ite3_step()] bot
305c0 68 20 6d 6f 64 69 66 79 20 74 68 65 69 72 0a 2a  h modify their.*
305d0 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * database conne
305e0 63 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 6d  ctions for the m
305f0 65 61 6e 69 6e 67 20 6f 66 20 22 6d 6f 64 69 66  eaning of "modif
30600 79 22 20 69 6e 20 74 68 69 73 20 70 61 72 61 67  y" in this parag
30610 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 69  raph..**.** Regi
30620 73 74 65 72 69 6e 67 20 61 20 4e 55 4c 4c 20 66  stering a NULL f
30630 75 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 73  unction disables
30640 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a   the callback..*
30650 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63 6f  *.** When the co
30660 6d 6d 69 74 20 68 6f 6f 6b 20 63 61 6c 6c 62 61  mmit hook callba
30670 63 6b 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  ck routine retur
30680 6e 73 20 7a 65 72 6f 2c 20 74 68 65 20 5b 43 4f  ns zero, the [CO
30690 4d 4d 49 54 5d 0a 2a 2a 20 6f 70 65 72 61 74 69  MMIT].** operati
306a0 6f 6e 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f  on is allowed to
306b0 20 63 6f 6e 74 69 6e 75 65 20 6e 6f 72 6d 61 6c   continue normal
306c0 6c 79 2e 20 20 49 66 20 74 68 65 20 63 6f 6d 6d  ly.  If the comm
306d0 69 74 20 68 6f 6f 6b 0a 2a 2a 20 72 65 74 75 72  it hook.** retur
306e0 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  ns non-zero, the
306f0 6e 20 74 68 65 20 5b 43 4f 4d 4d 49 54 5d 20 69  n the [COMMIT] i
30700 73 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f  s converted into
30710 20 61 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2e 0a 2a   a [ROLLBACK]..*
30720 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 68  * The rollback h
30730 6f 6f 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f  ook is invoked o
30740 6e 20 61 20 72 6f 6c 6c 62 61 63 6b 20 74 68 61  n a rollback tha
30750 74 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 20 61  t results from a
30760 20 63 6f 6d 6d 69 74 0a 2a 2a 20 68 6f 6f 6b 20   commit.** hook 
30770 72 65 74 75 72 6e 69 6e 67 20 6e 6f 6e 2d 7a 65  returning non-ze
30780 72 6f 2c 20 6a 75 73 74 20 61 73 20 69 74 20 77  ro, just as it w
30790 6f 75 6c 64 20 62 65 20 77 69 74 68 20 61 6e 79  ould be with any
307a0 20 6f 74 68 65 72 20 72 6f 6c 6c 62 61 63 6b 2e   other rollback.
307b0 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70  .**.** For the p
307c0 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69 73 20  urposes of this 
307d0 41 50 49 2c 20 61 20 74 72 61 6e 73 61 63 74 69  API, a transacti
307e0 6f 6e 20 69 73 20 73 61 69 64 20 74 6f 20 68 61  on is said to ha
307f0 76 65 20 62 65 65 6e 0a 2a 2a 20 72 6f 6c 6c 65  ve been.** rolle
30800 64 20 62 61 63 6b 20 69 66 20 61 6e 20 65 78 70  d back if an exp
30810 6c 69 63 69 74 20 22 52 4f 4c 4c 42 41 43 4b 22  licit "ROLLBACK"
30820 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 65 78   statement is ex
30830 65 63 75 74 65 64 2c 20 6f 72 0a 2a 2a 20 61 6e  ecuted, or.** an
30840 20 65 72 72 6f 72 20 6f 72 20 63 6f 6e 73 74 72   error or constr
30850 61 69 6e 74 20 63 61 75 73 65 73 20 61 6e 20 69  aint causes an i
30860 6d 70 6c 69 63 69 74 20 72 6f 6c 6c 62 61 63 6b  mplicit rollback
30870 20 74 6f 20 6f 63 63 75 72 2e 0a 2a 2a 20 54 68   to occur..** Th
30880 65 20 72 6f 6c 6c 62 61 63 6b 20 63 61 6c 6c 62  e rollback callb
30890 61 63 6b 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b  ack is not invok
308a0 65 64 20 69 66 20 61 20 74 72 61 6e 73 61 63 74  ed if a transact
308b0 69 6f 6e 20 69 73 0a 2a 2a 20 61 75 74 6f 6d 61  ion is.** automa
308c0 74 69 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 62  tically rolled b
308d0 61 63 6b 20 62 65 63 61 75 73 65 20 74 68 65 20  ack because the 
308e0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
308f0 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a  ion is closed..*
30900 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 63  * The rollback c
30910 61 6c 6c 62 61 63 6b 20 69 73 20 6e 6f 74 20 69  allback is not i
30920 6e 76 6f 6b 65 64 20 69 66 20 61 20 74 72 61 6e  nvoked if a tran
30930 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 72 6f  saction is.** ro
30940 6c 6c 65 64 20 62 61 63 6b 20 62 65 63 61 75 73  lled back becaus
30950 65 20 61 20 63 6f 6d 6d 69 74 20 63 61 6c 6c 62  e a commit callb
30960 61 63 6b 20 72 65 74 75 72 6e 65 64 20 6e 6f 6e  ack returned non
30970 2d 7a 65 72 6f 2e 0a 2a 2a 20 3c 74 6f 64 6f 3e  -zero..** <todo>
30980 20 43 68 65 63 6b 20 6f 6e 20 74 68 69 73 20 3c   Check on this <
30990 2f 74 6f 64 6f 3e 0a 2a 2a 0a 2a 2a 20 53 65 65  /todo>.**.** See
309a0 20 61 6c 73 6f 20 74 68 65 20 5b 73 71 6c 69 74   also the [sqlit
309b0 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 29  e3_update_hook()
309c0 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a  ] interface..**.
309d0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
309e0 0a 2a 2a 20 5b 48 31 32 39 35 31 5d 20 5b 48 31  .** [H12951] [H1
309f0 32 39 35 32 5d 20 5b 48 31 32 39 35 33 5d 20 5b  2952] [H12953] [
30a00 48 31 32 39 35 34 5d 20 5b 48 31 32 39 35 35 5d  H12954] [H12955]
30a10 0a 2a 2a 20 5b 48 31 32 39 36 31 5d 20 5b 48 31  .** [H12961] [H1
30a20 32 39 36 32 5d 20 5b 48 31 32 39 36 33 5d 20 5b  2962] [H12963] [
30a30 48 31 32 39 36 34 5d 0a 2a 2f 0a 53 51 4c 49 54  H12964].*/.SQLIT
30a40 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69  E_API void *sqli
30a50 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28  te3_commit_hook(
30a60 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 28 2a 29  sqlite3*, int(*)
30a70 28 76 6f 69 64 2a 29 2c 20 76 6f 69 64 2a 29 3b  (void*), void*);
30a80 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
30a90 20 2a 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62 61   *sqlite3_rollba
30aa0 63 6b 5f 68 6f 6f 6b 28 73 71 6c 69 74 65 33 2a  ck_hook(sqlite3*
30ab0 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 20 2a  , void(*)(void *
30ac0 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a  ), void*);../*.*
30ad0 2a 20 43 41 50 49 33 52 45 46 3a 20 44 61 74 61  * CAPI3REF: Data
30ae0 20 43 68 61 6e 67 65 20 4e 6f 74 69 66 69 63 61   Change Notifica
30af0 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 20 7b  tion Callbacks {
30b00 48 31 32 39 37 30 7d 20 3c 53 36 30 34 30 30 3e  H12970} <S60400>
30b10 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
30b20 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 29  e3_update_hook()
30b30 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73   interface regis
30b40 74 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b 20  ters a callback 
30b50 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 77 69 74 68  function.** with
30b60 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63   the [database c
30b70 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 64 65 6e 74  onnection] ident
30b80 69 66 69 65 64 20 62 79 20 74 68 65 20 66 69 72  ified by the fir
30b90 73 74 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74  st argument.** t
30ba0 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65  o be invoked whe
30bb0 6e 65 76 65 72 20 61 20 72 6f 77 20 69 73 20 75  never a row is u
30bc0 70 64 61 74 65 64 2c 20 69 6e 73 65 72 74 65 64  pdated, inserted
30bd0 20 6f 72 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 20   or deleted..** 
30be0 41 6e 79 20 63 61 6c 6c 62 61 63 6b 20 73 65 74  Any callback set
30bf0 20 62 79 20 61 20 70 72 65 76 69 6f 75 73 20 63   by a previous c
30c00 61 6c 6c 20 74 6f 20 74 68 69 73 20 66 75 6e 63  all to this func
30c10 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 68 65 20  tion.** for the 
30c20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f  same database co
30c30 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72  nnection is over
30c40 72 69 64 64 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  ridden..**.** Th
30c50 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
30c60 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  t is a pointer t
30c70 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74  o the function t
30c80 6f 20 69 6e 76 6f 6b 65 20 77 68 65 6e 20 61 0a  o invoke when a.
30c90 2a 2a 20 72 6f 77 20 69 73 20 75 70 64 61 74 65  ** row is update
30ca0 64 2c 20 69 6e 73 65 72 74 65 64 20 6f 72 20 64  d, inserted or d
30cb0 65 6c 65 74 65 64 2e 0a 2a 2a 20 54 68 65 20 66  eleted..** The f
30cc0 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
30cd0 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73   the callback is
30ce0 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 74   a copy of the t
30cf0 68 69 72 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a  hird argument.**
30d00 20 74 6f 20 73 71 6c 69 74 65 33 5f 75 70 64 61   to sqlite3_upda
30d10 74 65 5f 68 6f 6f 6b 28 29 2e 0a 2a 2a 20 54 68  te_hook()..** Th
30d20 65 20 73 65 63 6f 6e 64 20 63 61 6c 6c 62 61 63  e second callbac
30d30 6b 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f 6e  k argument is on
30d40 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 49 4e 53  e of [SQLITE_INS
30d50 45 52 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 44 45  ERT], [SQLITE_DE
30d60 4c 45 54 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51  LETE],.** or [SQ
30d70 4c 49 54 45 5f 55 50 44 41 54 45 5d 2c 20 64 65  LITE_UPDATE], de
30d80 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 6f  pending on the o
30d90 70 65 72 61 74 69 6f 6e 20 74 68 61 74 20 63 61  peration that ca
30da0 75 73 65 64 20 74 68 65 20 63 61 6c 6c 62 61 63  used the callbac
30db0 6b 0a 2a 2a 20 74 6f 20 62 65 20 69 6e 76 6f 6b  k.** to be invok
30dc0 65 64 2e 0a 2a 2a 20 54 68 65 20 74 68 69 72 64  ed..** The third
30dd0 20 61 6e 64 20 66 6f 75 72 74 68 20 61 72 67 75   and fourth argu
30de0 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 63 61 6c  ments to the cal
30df0 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e 20 70 6f  lback contain po
30e00 69 6e 74 65 72 73 20 74 6f 20 74 68 65 0a 2a 2a  inters to the.**
30e10 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 61   database and ta
30e20 62 6c 65 20 6e 61 6d 65 20 63 6f 6e 74 61 69 6e  ble name contain
30e30 69 6e 67 20 74 68 65 20 61 66 66 65 63 74 65 64  ing the affected
30e40 20 72 6f 77 2e 0a 2a 2a 20 54 68 65 20 66 69 6e   row..** The fin
30e50 61 6c 20 63 61 6c 6c 62 61 63 6b 20 70 61 72 61  al callback para
30e60 6d 65 74 65 72 20 69 73 20 74 68 65 20 5b 72 6f  meter is the [ro
30e70 77 69 64 5d 20 6f 66 20 74 68 65 20 72 6f 77 2e  wid] of the row.
30e80 0a 2a 2a 20 49 6e 20 74 68 65 20 63 61 73 65 20  .** In the case 
30e90 6f 66 20 61 6e 20 75 70 64 61 74 65 2c 20 74 68  of an update, th
30ea0 69 73 20 69 73 20 74 68 65 20 5b 72 6f 77 69 64  is is the [rowid
30eb0 5d 20 61 66 74 65 72 20 74 68 65 20 75 70 64 61  ] after the upda
30ec0 74 65 20 74 61 6b 65 73 20 70 6c 61 63 65 2e 0a  te takes place..
30ed0 2a 2a 0a 2a 2a 20 54 68 65 20 75 70 64 61 74 65  **.** The update
30ee0 20 68 6f 6f 6b 20 69 73 20 6e 6f 74 20 69 6e 76   hook is not inv
30ef0 6f 6b 65 64 20 77 68 65 6e 20 69 6e 74 65 72 6e  oked when intern
30f00 61 6c 20 73 79 73 74 65 6d 20 74 61 62 6c 65 73  al system tables
30f10 20 61 72 65 0a 2a 2a 20 6d 6f 64 69 66 69 65 64   are.** modified
30f20 20 28 69 2e 65 2e 20 73 71 6c 69 74 65 5f 6d 61   (i.e. sqlite_ma
30f30 73 74 65 72 20 61 6e 64 20 73 71 6c 69 74 65 5f  ster and sqlite_
30f40 73 65 71 75 65 6e 63 65 29 2e 0a 2a 2a 0a 2a 2a  sequence)..**.**
30f50 20 49 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20   In the current 
30f60 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20  implementation, 
30f70 74 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 0a  the update hook.
30f80 2a 2a 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65  ** is not invoke
30f90 64 20 77 68 65 6e 20 64 75 70 6c 69 63 61 74 69  d when duplicati
30fa0 6f 6e 20 72 6f 77 73 20 61 72 65 20 64 65 6c 65  on rows are dele
30fb0 74 65 64 20 62 65 63 61 75 73 65 20 6f 66 20 61  ted because of a
30fc0 6e 0a 2a 2a 20 5b 4f 4e 20 43 4f 4e 46 4c 49 43  n.** [ON CONFLIC
30fd0 54 20 7c 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20  T | ON CONFLICT 
30fe0 52 45 50 4c 41 43 45 5d 20 63 6c 61 75 73 65 2e  REPLACE] clause.
30ff0 20 20 4e 6f 72 20 69 73 20 74 68 65 20 75 70 64    Nor is the upd
31000 61 74 65 20 68 6f 6f 6b 0a 2a 2a 20 69 6e 76 6f  ate hook.** invo
31010 6b 65 64 20 77 68 65 6e 20 72 6f 77 73 20 61 72  ked when rows ar
31020 65 20 64 65 6c 65 74 65 64 20 75 73 69 6e 67 20  e deleted using 
31030 74 68 65 20 5b 74 72 75 6e 63 61 74 65 20 6f 70  the [truncate op
31040 74 69 6d 69 7a 61 74 69 6f 6e 5d 2e 0a 2a 2a 20  timization]..** 
31050 54 68 65 20 65 78 63 65 70 74 69 6f 6e 73 20 64  The exceptions d
31060 65 66 69 6e 65 64 20 69 6e 20 74 68 69 73 20 70  efined in this p
31070 61 72 61 67 72 61 70 68 20 6d 69 67 68 74 20 63  aragraph might c
31080 68 61 6e 67 65 20 69 6e 20 61 20 66 75 74 75 72  hange in a futur
31090 65 0a 2a 2a 20 72 65 6c 65 61 73 65 20 6f 66 20  e.** release of 
310a0 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  SQLite..**.** Th
310b0 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 20 69 6d  e update hook im
310c0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 75 73  plementation mus
310d0 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69 6e  t not do anythin
310e0 67 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64 69  g that will modi
310f0 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61  fy.** the databa
31100 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68  se connection th
31110 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 75  at invoked the u
31120 70 64 61 74 65 20 68 6f 6f 6b 2e 20 20 41 6e 79  pdate hook.  Any
31130 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 6d   actions.** to m
31140 6f 64 69 66 79 20 74 68 65 20 64 61 74 61 62 61  odify the databa
31150 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75  se connection mu
31160 73 74 20 62 65 20 64 65 66 65 72 72 65 64 20 75  st be deferred u
31170 6e 74 69 6c 20 61 66 74 65 72 20 74 68 65 0a 2a  ntil after the.*
31180 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 66 20  * completion of 
31190 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65  the [sqlite3_ste
311a0 70 28 29 5d 20 63 61 6c 6c 20 74 68 61 74 20 74  p()] call that t
311b0 72 69 67 67 65 72 65 64 20 74 68 65 20 75 70 64  riggered the upd
311c0 61 74 65 20 68 6f 6f 6b 2e 0a 2a 2a 20 4e 6f 74  ate hook..** Not
311d0 65 20 74 68 61 74 20 5b 73 71 6c 69 74 65 33 5f  e that [sqlite3_
311e0 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e  prepare_v2()] an
311f0 64 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  d [sqlite3_step(
31200 29 5d 20 62 6f 74 68 20 6d 6f 64 69 66 79 20 74  )] both modify t
31210 68 65 69 72 0a 2a 2a 20 64 61 74 61 62 61 73 65  heir.** database
31220 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72   connections for
31230 20 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20   the meaning of 
31240 22 6d 6f 64 69 66 79 22 20 69 6e 20 74 68 69 73  "modify" in this
31250 20 70 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a   paragraph..**.*
31260 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 66 75 6e  * If another fun
31270 63 74 69 6f 6e 20 77 61 73 20 70 72 65 76 69 6f  ction was previo
31280 75 73 6c 79 20 72 65 67 69 73 74 65 72 65 64 2c  usly registered,
31290 20 69 74 73 20 70 41 72 67 20 76 61 6c 75 65 0a   its pArg value.
312a0 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  ** is returned. 
312b0 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 20   Otherwise NULL 
312c0 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
312d0 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 65 20  ** See also the 
312e0 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f  [sqlite3_commit_
312f0 68 6f 6f 6b 28 29 5d 20 61 6e 64 20 5b 73 71 6c  hook()] and [sql
31300 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f  ite3_rollback_ho
31310 6f 6b 28 29 5d 0a 2a 2a 20 69 6e 74 65 72 66 61  ok()].** interfa
31320 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  ces..**.** Requi
31330 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32  rements:.** [H12
31340 39 37 31 5d 20 5b 48 31 32 39 37 33 5d 20 5b 48  971] [H12973] [H
31350 31 32 39 37 35 5d 20 5b 48 31 32 39 37 37 5d 20  12975] [H12977] 
31360 5b 48 31 32 39 37 39 5d 20 5b 48 31 32 39 38 31  [H12979] [H12981
31370 5d 20 5b 48 31 32 39 38 33 5d 20 5b 48 31 32 39  ] [H12983] [H129
31380 38 36 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  86].*/.SQLITE_AP
31390 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  I void *sqlite3_
313a0 75 70 64 61 74 65 5f 68 6f 6f 6b 28 0a 20 20 73  update_hook(.  s
313b0 71 6c 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64  qlite3*, .  void
313c0 28 2a 29 28 76 6f 69 64 20 2a 2c 69 6e 74 20 2c  (*)(void *,int ,
313d0 63 68 61 72 20 63 6f 6e 73 74 20 2a 2c 63 68 61  char const *,cha
313e0 72 20 63 6f 6e 73 74 20 2a 2c 73 71 6c 69 74 65  r const *,sqlite
313f0 33 5f 69 6e 74 36 34 29 2c 0a 20 20 76 6f 69 64  3_int64),.  void
31400 2a 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  *.);../*.** CAPI
31410 33 52 45 46 3a 20 45 6e 61 62 6c 65 20 4f 72 20  3REF: Enable Or 
31420 44 69 73 61 62 6c 65 20 53 68 61 72 65 64 20 50  Disable Shared P
31430 61 67 65 72 20 43 61 63 68 65 20 7b 48 31 30 33  ager Cache {H103
31440 33 30 7d 20 3c 53 33 30 39 30 30 3e 0a 2a 2a 20  30} <S30900>.** 
31450 4b 45 59 57 4f 52 44 53 3a 20 7b 73 68 61 72 65  KEYWORDS: {share
31460 64 20 63 61 63 68 65 7d 0a 2a 2a 0a 2a 2a 20 54  d cache}.**.** T
31470 68 69 73 20 72 6f 75 74 69 6e 65 20 65 6e 61 62  his routine enab
31480 6c 65 73 20 6f 72 20 64 69 73 61 62 6c 65 73 20  les or disables 
31490 74 68 65 20 73 68 61 72 69 6e 67 20 6f 66 20 74  the sharing of t
314a0 68 65 20 64 61 74 61 62 61 73 65 20 63 61 63 68  he database cach
314b0 65 0a 2a 2a 20 61 6e 64 20 73 63 68 65 6d 61 20  e.** and schema 
314c0 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20  data structures 
314d0 62 65 74 77 65 65 6e 20 5b 64 61 74 61 62 61 73  between [databas
314e0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 7c 20 63  e connection | c
314f0 6f 6e 6e 65 63 74 69 6f 6e 73 5d 0a 2a 2a 20 74  onnections].** t
31500 6f 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62  o the same datab
31510 61 73 65 2e 20 53 68 61 72 69 6e 67 20 69 73 20  ase. Sharing is 
31520 65 6e 61 62 6c 65 64 20 69 66 20 74 68 65 20 61  enabled if the a
31530 72 67 75 6d 65 6e 74 20 69 73 20 74 72 75 65 0a  rgument is true.
31540 2a 2a 20 61 6e 64 20 64 69 73 61 62 6c 65 64 20  ** and disabled 
31550 69 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20  if the argument 
31560 69 73 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20  is false..**.** 
31570 43 61 63 68 65 20 73 68 61 72 69 6e 67 20 69 73  Cache sharing is
31580 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 64 69 73   enabled and dis
31590 61 62 6c 65 64 20 66 6f 72 20 61 6e 20 65 6e 74  abled for an ent
315a0 69 72 65 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 20  ire process..** 
315b0 54 68 69 73 20 69 73 20 61 20 63 68 61 6e 67 65  This is a change
315c0 20 61 73 20 6f 66 20 53 51 4c 69 74 65 20 76 65   as of SQLite ve
315d0 72 73 69 6f 6e 20 33 2e 35 2e 30 2e 20 49 6e 20  rsion 3.5.0. In 
315e0 70 72 69 6f 72 20 76 65 72 73 69 6f 6e 73 20 6f  prior versions o
315f0 66 20 53 51 4c 69 74 65 2c 0a 2a 2a 20 73 68 61  f SQLite,.** sha
31600 72 69 6e 67 20 77 61 73 20 65 6e 61 62 6c 65 64  ring was enabled
31610 20 6f 72 20 64 69 73 61 62 6c 65 64 20 66 6f 72   or disabled for
31620 20 65 61 63 68 20 74 68 72 65 61 64 20 73 65 70   each thread sep
31630 61 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54  arately..**.** T
31640 68 65 20 63 61 63 68 65 20 73 68 61 72 69 6e 67  he cache sharing
31650 20 6d 6f 64 65 20 73 65 74 20 62 79 20 74 68 69   mode set by thi
31660 73 20 69 6e 74 65 72 66 61 63 65 20 65 66 66 65  s interface effe
31670 63 74 73 20 61 6c 6c 20 73 75 62 73 65 71 75 65  cts all subseque
31680 6e 74 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 5b  nt.** calls to [
31690 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c  sqlite3_open()],
316a0 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76   [sqlite3_open_v
316b0 32 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74  2()], and [sqlit
316c0 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a 2a 2a  e3_open16()]..**
316d0 20 45 78 69 73 74 69 6e 67 20 64 61 74 61 62 61   Existing databa
316e0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 63  se connections c
316f0 6f 6e 74 69 6e 75 65 20 75 73 65 20 74 68 65 20  ontinue use the 
31700 73 68 61 72 69 6e 67 20 6d 6f 64 65 0a 2a 2a 20  sharing mode.** 
31710 74 68 61 74 20 77 61 73 20 69 6e 20 65 66 66 65  that was in effe
31720 63 74 20 61 74 20 74 68 65 20 74 69 6d 65 20 74  ct at the time t
31730 68 65 79 20 77 65 72 65 20 6f 70 65 6e 65 64 2e  hey were opened.
31740 0a 2a 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20 74  .**.** Virtual t
31750 61 62 6c 65 73 20 63 61 6e 6e 6f 74 20 62 65 20  ables cannot be 
31760 75 73 65 64 20 77 69 74 68 20 61 20 73 68 61 72  used with a shar
31770 65 64 20 63 61 63 68 65 2e 20 20 57 68 65 6e 20  ed cache.  When 
31780 73 68 61 72 65 64 0a 2a 2a 20 63 61 63 68 65 20  shared.** cache 
31790 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 20  is enabled, the 
317a0 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f  [sqlite3_create_
317b0 6d 6f 64 75 6c 65 28 29 5d 20 41 50 49 20 75 73  module()] API us
317c0 65 64 20 74 6f 20 72 65 67 69 73 74 65 72 0a 2a  ed to register.*
317d0 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73  * virtual tables
317e0 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 72 65 74   will always ret
317f0 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a  urn an error..**
31800 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
31810 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45   returns [SQLITE
31820 5f 4f 4b 5d 20 69 66 20 73 68 61 72 65 64 20 63  _OK] if shared c
31830 61 63 68 65 20 77 61 73 20 65 6e 61 62 6c 65 64  ache was enabled
31840 20 6f 72 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20   or disabled.** 
31850 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 20 41  successfully.  A
31860 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69  n [error code] i
31870 73 20 72 65 74 75 72 6e 65 64 20 6f 74 68 65 72  s returned other
31880 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 53 68 61 72  wise..**.** Shar
31890 65 64 20 63 61 63 68 65 20 69 73 20 64 69 73 61  ed cache is disa
318a0 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e  bled by default.
318b0 20 42 75 74 20 74 68 69 73 20 6d 69 67 68 74 20   But this might 
318c0 63 68 61 6e 67 65 20 69 6e 0a 2a 2a 20 66 75 74  change in.** fut
318d0 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20  ure releases of 
318e0 53 51 4c 69 74 65 2e 20 20 41 70 70 6c 69 63 61  SQLite.  Applica
318f0 74 69 6f 6e 73 20 74 68 61 74 20 63 61 72 65 20  tions that care 
31900 61 62 6f 75 74 20 73 68 61 72 65 64 0a 2a 2a 20  about shared.** 
31910 63 61 63 68 65 20 73 65 74 74 69 6e 67 20 73 68  cache setting sh
31920 6f 75 6c 64 20 73 65 74 20 69 74 20 65 78 70 6c  ould set it expl
31930 69 63 69 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 53 65  icitly..**.** Se
31940 65 20 41 6c 73 6f 3a 20 20 5b 53 51 4c 69 74 65  e Also:  [SQLite
31950 20 53 68 61 72 65 64 2d 43 61 63 68 65 20 4d 6f   Shared-Cache Mo
31960 64 65 5d 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  de].**.** Requir
31970 65 6d 65 6e 74 73 3a 20 5b 48 31 30 33 33 31 5d  ements: [H10331]
31980 20 5b 48 31 30 33 33 36 5d 20 5b 48 31 30 33 33   [H10336] [H1033
31990 37 5d 20 5b 48 31 30 33 33 39 5d 0a 2a 2f 0a 53  7] [H10339].*/.S
319a0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
319b0 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61  lite3_enable_sha
319c0 72 65 64 5f 63 61 63 68 65 28 69 6e 74 29 3b 0a  red_cache(int);.
319d0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
319e0 20 41 74 74 65 6d 70 74 20 54 6f 20 46 72 65 65   Attempt To Free
319f0 20 48 65 61 70 20 4d 65 6d 6f 72 79 20 7b 48 31   Heap Memory {H1
31a00 37 33 34 30 7d 20 3c 53 33 30 32 32 30 3e 0a 2a  7340} <S30220>.*
31a10 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
31a20 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28  _release_memory(
31a30 29 20 69 6e 74 65 72 66 61 63 65 20 61 74 74 65  ) interface atte
31a40 6d 70 74 73 20 74 6f 20 66 72 65 65 20 4e 20 62  mpts to free N b
31a50 79 74 65 73 0a 2a 2a 20 6f 66 20 68 65 61 70 20  ytes.** of heap 
31a60 6d 65 6d 6f 72 79 20 62 79 20 64 65 61 6c 6c 6f  memory by deallo
31a70 63 61 74 69 6e 67 20 6e 6f 6e 2d 65 73 73 65 6e  cating non-essen
31a80 74 69 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  tial memory allo
31a90 63 61 74 69 6f 6e 73 0a 2a 2a 20 68 65 6c 64 20  cations.** held 
31aa0 62 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20  by the database 
31ab0 6c 69 62 72 61 72 79 2e 20 7b 45 4e 44 7d 20 20  library. {END}  
31ac0 4d 65 6d 6f 72 79 20 75 73 65 64 20 74 6f 20 63  Memory used to c
31ad0 61 63 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a  ache database.**
31ae0 20 70 61 67 65 73 20 74 6f 20 69 6d 70 72 6f 76   pages to improv
31af0 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 69 73  e performance is
31b00 20 61 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 6e   an example of n
31b10 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d  on-essential mem
31b20 6f 72 79 2e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  ory..** sqlite3_
31b30 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29  release_memory()
31b40 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d   returns the num
31b50 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 74  ber of bytes act
31b60 75 61 6c 6c 79 20 66 72 65 65 64 2c 0a 2a 2a 20  ually freed,.** 
31b70 77 68 69 63 68 20 6d 69 67 68 74 20 62 65 20 6d  which might be m
31b80 6f 72 65 20 6f 72 20 6c 65 73 73 20 74 68 61 6e  ore or less than
31b90 20 74 68 65 20 61 6d 6f 75 6e 74 20 72 65 71 75   the amount requ
31ba0 65 73 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  ested..**.** Req
31bb0 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 37 33  uirements: [H173
31bc0 34 31 5d 20 5b 48 31 37 33 34 32 5d 0a 2a 2f 0a  41] [H17342].*/.
31bd0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
31be0 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d  qlite3_release_m
31bf0 65 6d 6f 72 79 28 69 6e 74 29 3b 0a 0a 2f 2a 0a  emory(int);../*.
31c00 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 49 6d 70  ** CAPI3REF: Imp
31c10 6f 73 65 20 41 20 4c 69 6d 69 74 20 4f 6e 20 48  ose A Limit On H
31c20 65 61 70 20 53 69 7a 65 20 7b 48 31 37 33 35 30  eap Size {H17350
31c30 7d 20 3c 53 33 30 32 32 30 3e 0a 2a 2a 0a 2a 2a  } <S30220>.**.**
31c40 20 54 68 65 20 73 71 6c 69 74 65 33 5f 73 6f 66   The sqlite3_sof
31c50 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 29 20 69  t_heap_limit() i
31c60 6e 74 65 72 66 61 63 65 20 70 6c 61 63 65 73 20  nterface places 
31c70 61 20 22 73 6f 66 74 22 20 6c 69 6d 69 74 0a 2a  a "soft" limit.*
31c80 2a 20 6f 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20  * on the amount 
31c90 6f 66 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 74  of heap memory t
31ca0 68 61 74 20 6d 61 79 20 62 65 20 61 6c 6c 6f 63  hat may be alloc
31cb0 61 74 65 64 20 62 79 20 53 51 4c 69 74 65 2e 0a  ated by SQLite..
31cc0 2a 2a 20 49 66 20 61 6e 20 69 6e 74 65 72 6e 61  ** If an interna
31cd0 6c 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20  l allocation is 
31ce0 72 65 71 75 65 73 74 65 64 20 74 68 61 74 20 77  requested that w
31cf0 6f 75 6c 64 20 65 78 63 65 65 64 20 74 68 65 0a  ould exceed the.
31d00 2a 2a 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d  ** soft heap lim
31d10 69 74 2c 20 5b 73 71 6c 69 74 65 33 5f 72 65 6c  it, [sqlite3_rel
31d20 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d 20 69  ease_memory()] i
31d30 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 65 20 6f 72  s invoked one or
31d40 0a 2a 2a 20 6d 6f 72 65 20 74 69 6d 65 73 20 74  .** more times t
31d50 6f 20 66 72 65 65 20 75 70 20 73 6f 6d 65 20 73  o free up some s
31d60 70 61 63 65 20 62 65 66 6f 72 65 20 74 68 65 20  pace before the 
31d70 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 70 65  allocation is pe
31d80 72 66 6f 72 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  rformed..**.** T
31d90 68 65 20 6c 69 6d 69 74 20 69 73 20 63 61 6c 6c  he limit is call
31da0 65 64 20 22 73 6f 66 74 22 2c 20 62 65 63 61 75  ed "soft", becau
31db0 73 65 20 69 66 20 5b 73 71 6c 69 74 65 33 5f 72  se if [sqlite3_r
31dc0 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d  elease_memory()]
31dd0 0a 2a 2a 20 63 61 6e 6e 6f 74 20 66 72 65 65 20  .** cannot free 
31de0 73 75 66 66 69 63 69 65 6e 74 20 6d 65 6d 6f 72  sufficient memor
31df0 79 20 74 6f 20 70 72 65 76 65 6e 74 20 74 68 65  y to prevent the
31e00 20 6c 69 6d 69 74 20 66 72 6f 6d 20 62 65 69 6e   limit from bein
31e10 67 20 65 78 63 65 65 64 65 64 2c 0a 2a 2a 20 74  g exceeded,.** t
31e20 68 65 20 6d 65 6d 6f 72 79 20 69 73 20 61 6c 6c  he memory is all
31e30 6f 63 61 74 65 64 20 61 6e 79 77 61 79 20 61 6e  ocated anyway an
31e40 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 6f 70  d the current op
31e50 65 72 61 74 69 6f 6e 20 70 72 6f 63 65 65 64 73  eration proceeds
31e60 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 67 61 74 69  ..**.** A negati
31e70 76 65 20 6f 72 20 7a 65 72 6f 20 76 61 6c 75 65  ve or zero value
31e80 20 66 6f 72 20 4e 20 6d 65 61 6e 73 20 74 68 61   for N means tha
31e90 74 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73 6f  t there is no so
31ea0 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20 61 6e  ft heap limit an
31eb0 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 65  d.** [sqlite3_re
31ec0 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d 20  lease_memory()] 
31ed0 77 69 6c 6c 20 6f 6e 6c 79 20 62 65 20 63 61 6c  will only be cal
31ee0 6c 65 64 20 77 68 65 6e 20 6d 65 6d 6f 72 79 20  led when memory 
31ef0 69 73 20 65 78 68 61 75 73 74 65 64 2e 0a 2a 2a  is exhausted..**
31f00 20 54 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c   The default val
31f10 75 65 20 66 6f 72 20 74 68 65 20 73 6f 66 74 20  ue for the soft 
31f20 68 65 61 70 20 6c 69 6d 69 74 20 69 73 20 7a 65  heap limit is ze
31f30 72 6f 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65  ro..**.** SQLite
31f40 20 6d 61 6b 65 73 20 61 20 62 65 73 74 20 65 66   makes a best ef
31f50 66 6f 72 74 20 74 6f 20 68 6f 6e 6f 72 20 74 68  fort to honor th
31f60 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69  e soft heap limi
31f70 74 2e 0a 2a 2a 20 42 75 74 20 69 66 20 74 68 65  t..** But if the
31f80 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74   soft heap limit
31f90 20 63 61 6e 6e 6f 74 20 62 65 20 68 6f 6e 6f 72   cannot be honor
31fa0 65 64 2c 20 65 78 65 63 75 74 69 6f 6e 20 77 69  ed, execution wi
31fb0 6c 6c 0a 2a 2a 20 63 6f 6e 74 69 6e 75 65 20 77  ll.** continue w
31fc0 69 74 68 6f 75 74 20 65 72 72 6f 72 20 6f 72 20  ithout error or 
31fd0 6e 6f 74 69 66 69 63 61 74 69 6f 6e 2e 20 20 54  notification.  T
31fe0 68 69 73 20 69 73 20 77 68 79 20 74 68 65 20 6c  his is why the l
31ff0 69 6d 69 74 20 69 73 0a 2a 2a 20 63 61 6c 6c 65  imit is.** calle
32000 64 20 61 20 22 73 6f 66 74 22 20 6c 69 6d 69 74  d a "soft" limit
32010 2e 20 20 49 74 20 69 73 20 61 64 76 69 73 6f 72  .  It is advisor
32020 79 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 72  y only..**.** Pr
32030 69 6f 72 20 74 6f 20 53 51 4c 69 74 65 20 76 65  ior to SQLite ve
32040 72 73 69 6f 6e 20 33 2e 35 2e 30 2c 20 74 68 69  rsion 3.5.0, thi
32050 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 63  s routine only c
32060 6f 6e 73 74 72 61 69 6e 65 64 20 74 68 65 20 6d  onstrained the m
32070 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74  emory.** allocat
32080 65 64 20 62 79 20 61 20 73 69 6e 67 6c 65 20 74  ed by a single t
32090 68 72 65 61 64 20 2d 20 74 68 65 20 73 61 6d 65  hread - the same
320a0 20 74 68 72 65 61 64 20 69 6e 20 77 68 69 63 68   thread in which
320b0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   this routine.**
320c0 20 72 75 6e 73 2e 20 20 42 65 67 69 6e 6e 69 6e   runs.  Beginnin
320d0 67 20 77 69 74 68 20 53 51 4c 69 74 65 20 76 65  g with SQLite ve
320e0 72 73 69 6f 6e 20 33 2e 35 2e 30 2c 20 74 68 65  rsion 3.5.0, the
320f0 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74   soft heap limit
32100 20 69 73 0a 2a 2a 20 61 70 70 6c 69 65 64 20 74   is.** applied t
32110 6f 20 61 6c 6c 20 74 68 72 65 61 64 73 2e 20 54  o all threads. T
32120 68 65 20 76 61 6c 75 65 20 73 70 65 63 69 66 69  he value specifi
32130 65 64 20 66 6f 72 20 74 68 65 20 73 6f 66 74 20  ed for the soft 
32140 68 65 61 70 20 6c 69 6d 69 74 0a 2a 2a 20 69 73  heap limit.** is
32150 20 61 6e 20 75 70 70 65 72 20 62 6f 75 6e 64 20   an upper bound 
32160 6f 6e 20 74 68 65 20 74 6f 74 61 6c 20 6d 65 6d  on the total mem
32170 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66  ory allocation f
32180 6f 72 20 61 6c 6c 20 74 68 72 65 61 64 73 2e 20  or all threads. 
32190 49 6e 0a 2a 2a 20 76 65 72 73 69 6f 6e 20 33 2e  In.** version 3.
321a0 35 2e 30 20 74 68 65 72 65 20 69 73 20 6e 6f 20  5.0 there is no 
321b0 6d 65 63 68 61 6e 69 73 6d 20 66 6f 72 20 6c 69  mechanism for li
321c0 6d 69 74 69 6e 67 20 74 68 65 20 68 65 61 70 20  miting the heap 
321d0 75 73 61 67 65 20 66 6f 72 0a 2a 2a 20 69 6e 64  usage for.** ind
321e0 69 76 69 64 75 61 6c 20 74 68 72 65 61 64 73 2e  ividual threads.
321f0 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
32200 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 33 35 31 5d  nts:.** [H16351]
32210 20 5b 48 31 36 33 35 32 5d 20 5b 48 31 36 33 35   [H16352] [H1635
32220 33 5d 20 5b 48 31 36 33 35 34 5d 20 5b 48 31 36  3] [H16354] [H16
32230 33 35 35 5d 20 5b 48 31 36 33 35 38 5d 0a 2a 2f  355] [H16358].*/
32240 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
32250 20 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65   sqlite3_soft_he
32260 61 70 5f 6c 69 6d 69 74 28 69 6e 74 29 3b 0a 0a  ap_limit(int);..
32270 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
32280 45 78 74 72 61 63 74 20 4d 65 74 61 64 61 74 61  Extract Metadata
32290 20 41 62 6f 75 74 20 41 20 43 6f 6c 75 6d 6e 20   About A Column 
322a0 4f 66 20 41 20 54 61 62 6c 65 20 7b 48 31 32 38  Of A Table {H128
322b0 35 30 7d 20 3c 53 36 30 33 30 30 3e 0a 2a 2a 0a  50} <S60300>.**.
322c0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
322d0 72 65 74 75 72 6e 73 20 6d 65 74 61 64 61 74 61  returns metadata
322e0 20 61 62 6f 75 74 20 61 20 73 70 65 63 69 66 69   about a specifi
322f0 63 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 73 70  c column of a sp
32300 65 63 69 66 69 63 0a 2a 2a 20 64 61 74 61 62 61  ecific.** databa
32310 73 65 20 74 61 62 6c 65 20 61 63 63 65 73 73 69  se table accessi
32320 62 6c 65 20 75 73 69 6e 67 20 74 68 65 20 5b 64  ble using the [d
32330 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
32340 6f 6e 5d 20 68 61 6e 64 6c 65 0a 2a 2a 20 70 61  on] handle.** pa
32350 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73  ssed as the firs
32360 74 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d  t function argum
32370 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  ent..**.** The c
32380 6f 6c 75 6d 6e 20 69 73 20 69 64 65 6e 74 69 66  olumn is identif
32390 69 65 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e  ied by the secon
323a0 64 2c 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75  d, third and fou
323b0 72 74 68 20 70 61 72 61 6d 65 74 65 72 73 20 74  rth parameters t
323c0 6f 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69  o.** this functi
323d0 6f 6e 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 70  on. The second p
323e0 61 72 61 6d 65 74 65 72 20 69 73 20 65 69 74 68  arameter is eith
323f0 65 72 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  er the name of t
32400 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 28  he database.** (
32410 69 2e 65 2e 20 22 6d 61 69 6e 22 2c 20 22 74 65  i.e. "main", "te
32420 6d 70 22 20 6f 72 20 61 6e 20 61 74 74 61 63 68  mp" or an attach
32430 65 64 20 64 61 74 61 62 61 73 65 29 20 63 6f 6e  ed database) con
32440 74 61 69 6e 69 6e 67 20 74 68 65 20 73 70 65 63  taining the spec
32450 69 66 69 65 64 0a 2a 2a 20 74 61 62 6c 65 20 6f  ified.** table o
32460 72 20 4e 55 4c 4c 2e 20 49 66 20 69 74 20 69 73  r NULL. If it is
32470 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 6c 6c 20   NULL, then all 
32480 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
32490 65 73 20 61 72 65 20 73 65 61 72 63 68 65 64 0a  es are searched.
324a0 2a 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65  ** for the table
324b0 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20   using the same 
324c0 61 6c 67 6f 72 69 74 68 6d 20 75 73 65 64 20 62  algorithm used b
324d0 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20 65  y the database e
324e0 6e 67 69 6e 65 20 74 6f 0a 2a 2a 20 72 65 73 6f  ngine to.** reso
324f0 6c 76 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20  lve unqualified 
32500 74 61 62 6c 65 20 72 65 66 65 72 65 6e 63 65 73  table references
32510 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72  ..**.** The thir
32520 64 20 61 6e 64 20 66 6f 75 72 74 68 20 70 61 72  d and fourth par
32530 61 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73 20  ameters to this 
32540 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 65  function are the
32550 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d   table and colum
32560 6e 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65  n.** name of the
32570 20 64 65 73 69 72 65 64 20 63 6f 6c 75 6d 6e 2c   desired column,
32580 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 4e   respectively. N
32590 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20  either of these 
325a0 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 6d 61  parameters.** ma
325b0 79 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a  y be NULL..**.**
325c0 20 4d 65 74 61 64 61 74 61 20 69 73 20 72 65 74   Metadata is ret
325d0 75 72 6e 65 64 20 62 79 20 77 72 69 74 69 6e 67  urned by writing
325e0 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 6c   to the memory l
325f0 6f 63 61 74 69 6f 6e 73 20 70 61 73 73 65 64 20  ocations passed 
32600 61 73 20 74 68 65 20 35 74 68 0a 2a 2a 20 61 6e  as the 5th.** an
32610 64 20 73 75 62 73 65 71 75 65 6e 74 20 70 61 72  d subsequent par
32620 61 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73 20  ameters to this 
32630 66 75 6e 63 74 69 6f 6e 2e 20 41 6e 79 20 6f 66  function. Any of
32640 20 74 68 65 73 65 20 61 72 67 75 6d 65 6e 74 73   these arguments
32650 20 6d 61 79 20 62 65 0a 2a 2a 20 4e 55 4c 4c 2c   may be.** NULL,
32660 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 74   in which case t
32670 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
32680 20 65 6c 65 6d 65 6e 74 20 6f 66 20 6d 65 74 61   element of meta
32690 64 61 74 61 20 69 73 20 6f 6d 69 74 74 65 64 2e  data is omitted.
326a0 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f  .**.** <blockquo
326b0 74 65 3e 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 6f  te>.** <table bo
326c0 72 64 65 72 3d 22 31 22 3e 0a 2a 2a 20 3c 74 72  rder="1">.** <tr
326d0 3e 3c 74 68 3e 20 50 61 72 61 6d 65 74 65 72 20  ><th> Parameter 
326e0 3c 74 68 3e 20 4f 75 74 70 75 74 3c 62 72 3e 54  <th> Output<br>T
326f0 79 70 65 20 3c 74 68 3e 20 20 44 65 73 63 72 69  ype <th>  Descri
32700 70 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 72 3e  ption.**.** <tr>
32710 3c 74 64 3e 20 35 74 68 20 3c 74 64 3e 20 63 6f  <td> 5th <td> co
32720 6e 73 74 20 63 68 61 72 2a 20 3c 74 64 3e 20 44  nst char* <td> D
32730 61 74 61 20 74 79 70 65 0a 2a 2a 20 3c 74 72 3e  ata type.** <tr>
32740 3c 74 64 3e 20 36 74 68 20 3c 74 64 3e 20 63 6f  <td> 6th <td> co
32750 6e 73 74 20 63 68 61 72 2a 20 3c 74 64 3e 20 4e  nst char* <td> N
32760 61 6d 65 20 6f 66 20 64 65 66 61 75 6c 74 20 63  ame of default c
32770 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
32780 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 37 74  e.** <tr><td> 7t
32790 68 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20  h <td> int      
327a0 20 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20     <td> True if 
327b0 63 6f 6c 75 6d 6e 20 68 61 73 20 61 20 4e 4f 54  column has a NOT
327c0 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74   NULL constraint
327d0 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 38 74 68  .** <tr><td> 8th
327e0 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20   <td> int       
327f0 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63    <td> True if c
32800 6f 6c 75 6d 6e 20 69 73 20 70 61 72 74 20 6f 66  olumn is part of
32810 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59   the PRIMARY KEY
32820 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 39 74 68  .** <tr><td> 9th
32830 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20   <td> int       
32840 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63    <td> True if c
32850 6f 6c 75 6d 6e 20 69 73 20 5b 41 55 54 4f 49 4e  olumn is [AUTOIN
32860 43 52 45 4d 45 4e 54 5d 0a 2a 2a 20 3c 2f 74 61  CREMENT].** </ta
32870 62 6c 65 3e 0a 2a 2a 20 3c 2f 62 6c 6f 63 6b 71  ble>.** </blockq
32880 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  uote>.**.** The 
32890 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 64 20 74  memory pointed t
328a0 6f 20 62 79 20 74 68 65 20 63 68 61 72 61 63 74  o by the charact
328b0 65 72 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75  er pointers retu
328c0 72 6e 65 64 20 66 6f 72 20 74 68 65 0a 2a 2a 20  rned for the.** 
328d0 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65  declaration type
328e0 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73   and collation s
328f0 65 71 75 65 6e 63 65 20 69 73 20 76 61 6c 69 64  equence is valid
32900 20 6f 6e 6c 79 20 75 6e 74 69 6c 20 74 68 65 20   only until the 
32910 6e 65 78 74 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20  next.** call to 
32920 61 6e 79 20 53 51 4c 69 74 65 20 41 50 49 20 66  any SQLite API f
32930 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49  unction..**.** I
32940 66 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  f the specified 
32950 74 61 62 6c 65 20 69 73 20 61 63 74 75 61 6c 6c  table is actuall
32960 79 20 61 20 76 69 65 77 2c 20 61 6e 20 5b 65 72  y a view, an [er
32970 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74  ror code] is ret
32980 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  urned..**.** If 
32990 74 68 65 20 73 70 65 63 69 66 69 65 64 20 63 6f  the specified co
329a0 6c 75 6d 6e 20 69 73 20 22 72 6f 77 69 64 22 2c  lumn is "rowid",
329b0 20 22 6f 69 64 22 20 6f 72 20 22 5f 72 6f 77 69   "oid" or "_rowi
329c0 64 5f 22 20 61 6e 64 20 61 6e 0a 2a 2a 20 5b 49  d_" and an.** [I
329d0 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
329e0 45 59 5d 20 63 6f 6c 75 6d 6e 20 68 61 73 20 62  EY] column has b
329f0 65 65 6e 20 65 78 70 6c 69 63 69 74 6c 79 20 64  een explicitly d
32a00 65 63 6c 61 72 65 64 2c 20 74 68 65 6e 20 74 68  eclared, then th
32a10 65 20 6f 75 74 70 75 74 0a 2a 2a 20 70 61 72 61  e output.** para
32a20 6d 65 74 65 72 73 20 61 72 65 20 73 65 74 20 66  meters are set f
32a30 6f 72 20 74 68 65 20 65 78 70 6c 69 63 69 74 6c  or the explicitl
32a40 79 20 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d  y declared colum
32a50 6e 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e  n. If there is n
32a60 6f 0a 2a 2a 20 65 78 70 6c 69 63 69 74 6c 79 20  o.** explicitly 
32a70 64 65 63 6c 61 72 65 64 20 5b 49 4e 54 45 47 45  declared [INTEGE
32a80 52 20 50 52 49 4d 41 52 59 20 4b 45 59 5d 20 63  R PRIMARY KEY] c
32a90 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 74 68 65 20  olumn, then the 
32aa0 6f 75 74 70 75 74 0a 2a 2a 20 70 61 72 61 6d 65  output.** parame
32ab0 74 65 72 73 20 61 72 65 20 73 65 74 20 61 73 20  ters are set as 
32ac0 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c  follows:.**.** <
32ad0 70 72 65 3e 0a 2a 2a 20 20 20 20 20 64 61 74 61  pre>.**     data
32ae0 20 74 79 70 65 3a 20 22 49 4e 54 45 47 45 52 22   type: "INTEGER"
32af0 0a 2a 2a 20 20 20 20 20 63 6f 6c 6c 61 74 69 6f  .**     collatio
32b00 6e 20 73 65 71 75 65 6e 63 65 3a 20 22 42 49 4e  n sequence: "BIN
32b10 41 52 59 22 0a 2a 2a 20 20 20 20 20 6e 6f 74 20  ARY".**     not 
32b20 6e 75 6c 6c 3a 20 30 0a 2a 2a 20 20 20 20 20 70  null: 0.**     p
32b30 72 69 6d 61 72 79 20 6b 65 79 3a 20 31 0a 2a 2a  rimary key: 1.**
32b40 20 20 20 20 20 61 75 74 6f 20 69 6e 63 72 65 6d       auto increm
32b50 65 6e 74 3a 20 30 0a 2a 2a 20 3c 2f 70 72 65 3e  ent: 0.** </pre>
32b60 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
32b70 74 69 6f 6e 20 6d 61 79 20 6c 6f 61 64 20 6f 6e  tion may load on
32b80 65 20 6f 72 20 6d 6f 72 65 20 73 63 68 65 6d 61  e or more schema
32b90 73 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65 20  s from database 
32ba0 66 69 6c 65 73 2e 20 49 66 20 61 6e 0a 2a 2a 20  files. If an.** 
32bb0 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72  error occurs dur
32bc0 69 6e 67 20 74 68 69 73 20 70 72 6f 63 65 73 73  ing this process
32bd0 2c 20 6f 72 20 69 66 20 74 68 65 20 72 65 71 75  , or if the requ
32be0 65 73 74 65 64 20 74 61 62 6c 65 20 6f 72 20 63  ested table or c
32bf0 6f 6c 75 6d 6e 0a 2a 2a 20 63 61 6e 6e 6f 74 20  olumn.** cannot 
32c00 62 65 20 66 6f 75 6e 64 2c 20 61 6e 20 5b 65 72  be found, an [er
32c10 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74  ror code] is ret
32c20 75 72 6e 65 64 20 61 6e 64 20 61 6e 20 65 72 72  urned and an err
32c30 6f 72 20 6d 65 73 73 61 67 65 20 6c 65 66 74 0a  or message left.
32c40 2a 2a 20 69 6e 20 74 68 65 20 5b 64 61 74 61 62  ** in the [datab
32c50 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
32c60 28 74 6f 20 62 65 20 72 65 74 72 69 65 76 65 64  (to be retrieved
32c70 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 65   using sqlite3_e
32c80 72 72 6d 73 67 28 29 29 2e 0a 2a 2a 0a 2a 2a 20  rrmsg())..**.** 
32c90 54 68 69 73 20 41 50 49 20 69 73 20 6f 6e 6c 79  This API is only
32ca0 20 61 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68   available if th
32cb0 65 20 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f  e library was co
32cc0 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a  mpiled with the.
32cd0 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ** [SQLITE_ENABL
32ce0 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54  E_COLUMN_METADAT
32cf0 41 5d 20 43 2d 70 72 65 70 72 6f 63 65 73 73 6f  A] C-preprocesso
32d00 72 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64  r symbol defined
32d10 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
32d20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 61 62 6c  int sqlite3_tabl
32d30 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74  e_column_metadat
32d40 61 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  a(.  sqlite3 *db
32d50 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
32d60 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 68   /* Connection h
32d70 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  andle */.  const
32d80 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c 20   char *zDbName, 
32d90 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
32da0 73 65 20 6e 61 6d 65 20 6f 72 20 4e 55 4c 4c 20  se name or NULL 
32db0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
32dc0 2a 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 20 20 20  *zTableName,    
32dd0 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20 2a   /* Table name *
32de0 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
32df0 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 2c 20 20 20 20  zColumnName,    
32e00 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a  /* Column name *
32e10 2f 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  /.  char const *
32e20 2a 70 7a 44 61 74 61 54 79 70 65 2c 20 20 20 20  *pzDataType,    
32e30 2f 2a 20 4f 55 54 50 55 54 3a 20 44 65 63 6c 61  /* OUTPUT: Decla
32e40 72 65 64 20 64 61 74 61 20 74 79 70 65 20 2a 2f  red data type */
32e50 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a  .  char const **
32e60 70 7a 43 6f 6c 6c 53 65 71 2c 20 20 20 20 20 2f  pzCollSeq,     /
32e70 2a 20 4f 55 54 50 55 54 3a 20 43 6f 6c 6c 61 74  * OUTPUT: Collat
32e80 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d  ion sequence nam
32e90 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4e 6f 74  e */.  int *pNot
32ea0 4e 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20  Null,           
32eb0 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72     /* OUTPUT: Tr
32ec0 75 65 20 69 66 20 4e 4f 54 20 4e 55 4c 4c 20 63  ue if NOT NULL c
32ed0 6f 6e 73 74 72 61 69 6e 74 20 65 78 69 73 74 73  onstraint exists
32ee0 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 50 72 69 6d   */.  int *pPrim
32ef0 61 72 79 4b 65 79 2c 20 20 20 20 20 20 20 20 20  aryKey,         
32f00 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75    /* OUTPUT: Tru
32f10 65 20 69 66 20 63 6f 6c 75 6d 6e 20 70 61 72 74  e if column part
32f20 20 6f 66 20 50 4b 20 2a 2f 0a 20 20 69 6e 74 20   of PK */.  int 
32f30 2a 70 41 75 74 6f 69 6e 63 20 20 20 20 20 20 20  *pAutoinc       
32f40 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 55          /* OUTPU
32f50 54 3a 20 54 72 75 65 20 69 66 20 63 6f 6c 75 6d  T: True if colum
32f60 6e 20 69 73 20 61 75 74 6f 2d 69 6e 63 72 65 6d  n is auto-increm
32f70 65 6e 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a  ent */.);../*.**
32f80 20 43 41 50 49 33 52 45 46 3a 20 4c 6f 61 64 20   CAPI3REF: Load 
32f90 41 6e 20 45 78 74 65 6e 73 69 6f 6e 20 7b 48 31  An Extension {H1
32fa0 32 36 30 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a  2600} <S20500>.*
32fb0 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66  *.** This interf
32fc0 61 63 65 20 6c 6f 61 64 73 20 61 6e 20 53 51 4c  ace loads an SQL
32fd0 69 74 65 20 65 78 74 65 6e 73 69 6f 6e 20 6c 69  ite extension li
32fe0 62 72 61 72 79 20 66 72 6f 6d 20 74 68 65 20 6e  brary from the n
32ff0 61 6d 65 64 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a  amed file..**.**
33000 20 7b 48 31 32 36 30 31 7d 20 54 68 65 20 73 71   {H12601} The sq
33010 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e  lite3_load_exten
33020 73 69 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65  sion() interface
33030 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6c 6f 61   attempts to loa
33040 64 20 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  d an.**         
33050 20 53 51 4c 69 74 65 20 65 78 74 65 6e 73 69 6f   SQLite extensio
33060 6e 20 6c 69 62 72 61 72 79 20 63 6f 6e 74 61 69  n library contai
33070 6e 65 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20  ned in the file 
33080 7a 46 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  zFile..**.** {H1
33090 32 36 30 32 7d 20 54 68 65 20 65 6e 74 72 79 20  2602} The entry 
330a0 70 6f 69 6e 74 20 69 73 20 7a 50 72 6f 63 2e 0a  point is zProc..
330b0 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 33 7d 20 7a  **.** {H12603} z
330c0 50 72 6f 63 20 6d 61 79 20 62 65 20 30 2c 20 69  Proc may be 0, i
330d0 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65  n which case the
330e0 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 65 6e 74   name of the ent
330f0 72 79 20 70 6f 69 6e 74 0a 2a 2a 20 20 20 20 20  ry point.**     
33100 20 20 20 20 20 64 65 66 61 75 6c 74 73 20 74 6f       defaults to
33110 20 22 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73   "sqlite3_extens
33120 69 6f 6e 5f 69 6e 69 74 22 2e 0a 2a 2a 0a 2a 2a  ion_init"..**.**
33130 20 7b 48 31 32 36 30 34 7d 20 54 68 65 20 73 71   {H12604} The sq
33140 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e  lite3_load_exten
33150 73 69 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65  sion() interface
33160 20 73 68 61 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a   shall return.**
33170 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54            [SQLIT
33180 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73  E_OK] on success
33190 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 45 52 52   and [SQLITE_ERR
331a0 4f 52 5d 20 69 66 20 73 6f 6d 65 74 68 69 6e 67  OR] if something
331b0 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a   goes wrong..**.
331c0 2a 2a 20 7b 48 31 32 36 30 35 7d 20 49 66 20 61  ** {H12605} If a
331d0 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61  n error occurs a
331e0 6e 64 20 70 7a 45 72 72 4d 73 67 20 69 73 20 6e  nd pzErrMsg is n
331f0 6f 74 20 30 2c 20 74 68 65 6e 20 74 68 65 0a 2a  ot 0, then the.*
33200 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69  *          [sqli
33210 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69  te3_load_extensi
33220 6f 6e 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20  on()] interface 
33230 73 68 61 6c 6c 20 61 74 74 65 6d 70 74 20 74 6f  shall attempt to
33240 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 69 6c  .**          fil
33250 6c 20 2a 70 7a 45 72 72 4d 73 67 20 77 69 74 68  l *pzErrMsg with
33260 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74   error message t
33270 65 78 74 20 73 74 6f 72 65 64 20 69 6e 20 6d 65  ext stored in me
33280 6d 6f 72 79 0a 2a 2a 20 20 20 20 20 20 20 20 20  mory.**         
33290 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b   obtained from [
332a0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
332b0 5d 2e 20 7b 45 4e 44 7d 20 20 54 68 65 20 63 61  ]. {END}  The ca
332c0 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a  lling function.*
332d0 2a 20 20 20 20 20 20 20 20 20 20 73 68 6f 75 6c  *          shoul
332e0 64 20 66 72 65 65 20 74 68 69 73 20 6d 65 6d 6f  d free this memo
332f0 72 79 20 62 79 20 63 61 6c 6c 69 6e 67 20 5b 73  ry by calling [s
33300 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 0a  qlite3_free()]..
33310 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 36 7d 20 45  **.** {H12606} E
33320 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67  xtension loading
33330 20 6d 75 73 74 20 62 65 20 65 6e 61 62 6c 65 64   must be enabled
33340 20 75 73 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20   using.**       
33350 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 6e 61 62     [sqlite3_enab
33360 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f  le_load_extensio
33370 6e 28 29 5d 20 70 72 69 6f 72 20 74 6f 20 63 61  n()] prior to ca
33380 6c 6c 69 6e 67 20 74 68 69 73 20 41 50 49 2c 0a  lling this API,.
33390 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 74 68 65  **          othe
333a0 72 77 69 73 65 20 61 6e 20 65 72 72 6f 72 20 77  rwise an error w
333b0 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 2e  ill be returned.
333c0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
333d0 6e 74 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f  nt sqlite3_load_
333e0 65 78 74 65 6e 73 69 6f 6e 28 0a 20 20 73 71 6c  extension(.  sql
333f0 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
33400 20 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65     /* Load the e
33410 78 74 65 6e 73 69 6f 6e 20 69 6e 74 6f 20 74 68  xtension into th
33420 69 73 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  is database conn
33430 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73  ection */.  cons
33440 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 2c 20 20  t char *zFile,  
33450 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
33460 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20   shared library 
33470 63 6f 6e 74 61 69 6e 69 6e 67 20 65 78 74 65 6e  containing exten
33480 73 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  sion */.  const 
33490 63 68 61 72 20 2a 7a 50 72 6f 63 2c 20 20 20 20  char *zProc,    
334a0 2f 2a 20 45 6e 74 72 79 20 70 6f 69 6e 74 2e 20  /* Entry point. 
334b0 20 44 65 72 69 76 65 64 20 66 72 6f 6d 20 7a 46   Derived from zF
334c0 69 6c 65 20 69 66 20 30 20 2a 2f 0a 20 20 63 68  ile if 0 */.  ch
334d0 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20  ar **pzErrMsg   
334e0 20 20 20 20 2f 2a 20 50 75 74 20 65 72 72 6f 72      /* Put error
334f0 20 6d 65 73 73 61 67 65 20 68 65 72 65 20 69 66   message here if
33500 20 6e 6f 74 20 30 20 2a 2f 0a 29 3b 0a 0a 2f 2a   not 0 */.);../*
33510 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 6e  .** CAPI3REF: En
33520 61 62 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 20  able Or Disable 
33530 45 78 74 65 6e 73 69 6f 6e 20 4c 6f 61 64 69 6e  Extension Loadin
33540 67 20 7b 48 31 32 36 32 30 7d 20 3c 53 32 30 35  g {H12620} <S205
33550 30 30 3e 0a 2a 2a 0a 2a 2a 20 53 6f 20 61 73 20  00>.**.** So as 
33560 6e 6f 74 20 74 6f 20 6f 70 65 6e 20 73 65 63 75  not to open secu
33570 72 69 74 79 20 68 6f 6c 65 73 20 69 6e 20 6f 6c  rity holes in ol
33580 64 65 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 73  der applications
33590 20 74 68 61 74 20 61 72 65 0a 2a 2a 20 75 6e 70   that are.** unp
335a0 72 65 70 61 72 65 64 20 74 6f 20 64 65 61 6c 20  repared to deal 
335b0 77 69 74 68 20 65 78 74 65 6e 73 69 6f 6e 20 6c  with extension l
335c0 6f 61 64 69 6e 67 2c 20 61 6e 64 20 61 73 20 61  oading, and as a
335d0 20 6d 65 61 6e 73 20 6f 66 20 64 69 73 61 62 6c   means of disabl
335e0 69 6e 67 0a 2a 2a 20 65 78 74 65 6e 73 69 6f 6e  ing.** extension
335f0 20 6c 6f 61 64 69 6e 67 20 77 68 69 6c 65 20 65   loading while e
33600 76 61 6c 75 61 74 69 6e 67 20 75 73 65 72 2d 65  valuating user-e
33610 6e 74 65 72 65 64 20 53 51 4c 2c 20 74 68 65 20  ntered SQL, the 
33620 66 6f 6c 6c 6f 77 69 6e 67 20 41 50 49 0a 2a 2a  following API.**
33630 20 69 73 20 70 72 6f 76 69 64 65 64 20 74 6f 20   is provided to 
33640 74 75 72 6e 20 74 68 65 20 5b 73 71 6c 69 74 65  turn the [sqlite
33650 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e  3_load_extension
33660 28 29 5d 20 6d 65 63 68 61 6e 69 73 6d 20 6f 6e  ()] mechanism on
33670 20 61 6e 64 20 6f 66 66 2e 0a 2a 2a 0a 2a 2a 20   and off..**.** 
33680 45 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e  Extension loadin
33690 67 20 69 73 20 6f 66 66 20 62 79 20 64 65 66 61  g is off by defa
336a0 75 6c 74 2e 20 53 65 65 20 74 69 63 6b 65 74 20  ult. See ticket 
336b0 23 31 38 36 33 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  #1863..**.** {H1
336c0 32 36 32 31 7d 20 43 61 6c 6c 20 74 68 65 20 73  2621} Call the s
336d0 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f  qlite3_enable_lo
336e0 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 72  ad_extension() r
336f0 6f 75 74 69 6e 65 20 77 69 74 68 20 6f 6e 6f 66  outine with onof
33700 66 3d 3d 31 0a 2a 2a 20 20 20 20 20 20 20 20 20  f==1.**         
33710 20 74 6f 20 74 75 72 6e 20 65 78 74 65 6e 73 69   to turn extensi
33720 6f 6e 20 6c 6f 61 64 69 6e 67 20 6f 6e 20 61 6e  on loading on an
33730 64 20 63 61 6c 6c 20 69 74 20 77 69 74 68 20 6f  d call it with o
33740 6e 6f 66 66 3d 3d 30 20 74 6f 20 74 75 72 6e 0a  noff==0 to turn.
33750 2a 2a 20 20 20 20 20 20 20 20 20 20 69 74 20 62  **          it b
33760 61 63 6b 20 6f 66 66 20 61 67 61 69 6e 2e 0a 2a  ack off again..*
33770 2a 0a 2a 2a 20 7b 48 31 32 36 32 32 7d 20 45 78  *.** {H12622} Ex
33780 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20  tension loading 
33790 69 73 20 6f 66 66 20 62 79 20 64 65 66 61 75 6c  is off by defaul
337a0 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  t..*/.SQLITE_API
337b0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e 61   int sqlite3_ena
337c0 62 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69  ble_load_extensi
337d0 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  on(sqlite3 *db, 
337e0 69 6e 74 20 6f 6e 6f 66 66 29 3b 0a 0a 2f 2a 0a  int onoff);../*.
337f0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 41 75 74  ** CAPI3REF: Aut
33800 6f 6d 61 74 69 63 61 6c 6c 79 20 4c 6f 61 64 20  omatically Load 
33810 41 6e 20 45 78 74 65 6e 73 69 6f 6e 73 20 7b 48  An Extensions {H
33820 31 32 36 34 30 7d 20 3c 53 32 30 35 30 30 3e 0a  12640} <S20500>.
33830 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 63  **.** This API c
33840 61 6e 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 74  an be invoked at
33850 20 70 72 6f 67 72 61 6d 20 73 74 61 72 74 75 70   program startup
33860 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 72 65 67   in order to reg
33870 69 73 74 65 72 0a 2a 2a 20 6f 6e 65 20 6f 72 20  ister.** one or 
33880 6d 6f 72 65 20 73 74 61 74 69 63 61 6c 6c 79 20  more statically 
33890 6c 69 6e 6b 65 64 20 65 78 74 65 6e 73 69 6f 6e  linked extension
338a0 73 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 61  s that will be a
338b0 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 74 6f 20 61  vailable.** to a
338c0 6c 6c 20 6e 65 77 20 5b 64 61 74 61 62 61 73 65  ll new [database
338d0 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 2e 20 7b   connections]. {
338e0 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  END}.**.** This 
338f0 72 6f 75 74 69 6e 65 20 73 74 6f 72 65 73 20 61  routine stores a
33900 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
33910 65 78 74 65 6e 73 69 6f 6e 20 69 6e 20 61 6e 20  extension in an 
33920 61 72 72 61 79 20 74 68 61 74 20 69 73 0a 2a 2a  array that is.**
33930 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b   obtained from [
33940 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
33950 5d 2e 20 20 49 66 20 79 6f 75 20 72 75 6e 20 61  ].  If you run a
33960 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 20 63 68 65   memory leak che
33970 63 6b 65 72 0a 2a 2a 20 6f 6e 20 79 6f 75 72 20  cker.** on your 
33980 70 72 6f 67 72 61 6d 20 61 6e 64 20 69 74 20 72  program and it r
33990 65 70 6f 72 74 73 20 61 20 6c 65 61 6b 20 62 65  eports a leak be
339a0 63 61 75 73 65 20 6f 66 20 74 68 69 73 20 61 72  cause of this ar
339b0 72 61 79 2c 20 69 6e 76 6f 6b 65 0a 2a 2a 20 5b  ray, invoke.** [
339c0 73 71 6c 69 74 65 33 5f 72 65 73 65 74 5f 61 75  sqlite3_reset_au
339d0 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20  to_extension()] 
339e0 70 72 69 6f 72 20 74 6f 20 73 68 75 74 64 6f 77  prior to shutdow
339f0 6e 20 74 6f 20 66 72 65 65 20 74 68 65 20 6d 65  n to free the me
33a00 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  mory..**.** {H12
33a10 36 34 31 7d 20 54 68 69 73 20 66 75 6e 63 74 69  641} This functi
33a20 6f 6e 20 72 65 67 69 73 74 65 72 73 20 61 6e 20  on registers an 
33a30 65 78 74 65 6e 73 69 6f 6e 20 65 6e 74 72 79 20  extension entry 
33a40 70 6f 69 6e 74 20 74 68 61 74 20 69 73 0a 2a 2a  point that is.**
33a50 20 20 20 20 20 20 20 20 20 20 61 75 74 6f 6d 61            automa
33a60 74 69 63 61 6c 6c 79 20 69 6e 76 6f 6b 65 64 20  tically invoked 
33a70 77 68 65 6e 65 76 65 72 20 61 20 6e 65 77 20 5b  whenever a new [
33a80 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
33a90 69 6f 6e 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20  ion].**         
33aa0 20 69 73 20 6f 70 65 6e 65 64 20 75 73 69 6e 67   is opened using
33ab0 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29   [sqlite3_open()
33ac0 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e  ], [sqlite3_open
33ad0 31 36 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20  16()],.**       
33ae0 20 20 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 6f     or [sqlite3_o
33af0 70 65 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a  pen_v2()]..**.**
33b00 20 7b 48 31 32 36 34 32 7d 20 44 75 70 6c 69 63   {H12642} Duplic
33b10 61 74 65 20 65 78 74 65 6e 73 69 6f 6e 73 20 61  ate extensions a
33b20 72 65 20 64 65 74 65 63 74 65 64 20 73 6f 20 63  re detected so c
33b30 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74  alling this rout
33b40 69 6e 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ine.**          
33b50 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 77  multiple times w
33b60 69 74 68 20 74 68 65 20 73 61 6d 65 20 65 78 74  ith the same ext
33b70 65 6e 73 69 6f 6e 20 69 73 20 68 61 72 6d 6c 65  ension is harmle
33b80 73 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34  ss..**.** {H1264
33b90 33 7d 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  3} This routine 
33ba0 73 74 6f 72 65 73 20 61 20 70 6f 69 6e 74 65 72  stores a pointer
33bb0 20 74 6f 20 74 68 65 20 65 78 74 65 6e 73 69 6f   to the extensio
33bc0 6e 20 69 6e 20 61 6e 20 61 72 72 61 79 0a 2a 2a  n in an array.**
33bd0 20 20 20 20 20 20 20 20 20 20 74 68 61 74 20 69            that i
33be0 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  s obtained from 
33bf0 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  [sqlite3_malloc(
33c00 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34  )]..**.** {H1264
33c10 34 7d 20 41 75 74 6f 6d 61 74 69 63 20 65 78 74  4} Automatic ext
33c20 65 6e 73 69 6f 6e 73 20 61 70 70 6c 79 20 61 63  ensions apply ac
33c30 72 6f 73 73 20 61 6c 6c 20 74 68 72 65 61 64 73  ross all threads
33c40 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
33c50 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 75 74 6f  int sqlite3_auto
33c60 5f 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64 20  _extension(void 
33c70 28 2a 78 45 6e 74 72 79 50 6f 69 6e 74 29 28 76  (*xEntryPoint)(v
33c80 6f 69 64 29 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  oid));../*.** CA
33c90 50 49 33 52 45 46 3a 20 52 65 73 65 74 20 41 75  PI3REF: Reset Au
33ca0 74 6f 6d 61 74 69 63 20 45 78 74 65 6e 73 69 6f  tomatic Extensio
33cb0 6e 20 4c 6f 61 64 69 6e 67 20 7b 48 31 32 36 36  n Loading {H1266
33cc0 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a  0} <S20500>.**.*
33cd0 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
33ce0 64 69 73 61 62 6c 65 73 20 61 6c 6c 20 70 72 65  disables all pre
33cf0 76 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72  viously register
33d00 65 64 20 61 75 74 6f 6d 61 74 69 63 0a 2a 2a 20  ed automatic.** 
33d10 65 78 74 65 6e 73 69 6f 6e 73 2e 20 7b 45 4e 44  extensions. {END
33d20 7d 20 20 49 74 20 75 6e 64 6f 65 73 20 74 68 65  }  It undoes the
33d30 20 65 66 66 65 63 74 20 6f 66 20 61 6c 6c 20 70   effect of all p
33d40 72 69 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  rior.** [sqlite3
33d50 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28  _auto_extension(
33d60 29 5d 20 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20  )] calls..**.** 
33d70 7b 48 31 32 36 36 31 7d 20 54 68 69 73 20 66 75  {H12661} This fu
33d80 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 20  nction disables 
33d90 61 6c 6c 20 70 72 65 76 69 6f 75 73 6c 79 20 72  all previously r
33da0 65 67 69 73 74 65 72 65 64 0a 2a 2a 20 20 20 20  egistered.**    
33db0 20 20 20 20 20 20 61 75 74 6f 6d 61 74 69 63 20        automatic 
33dc0 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2a 0a 2a  extensions..**.*
33dd0 2a 20 7b 48 31 32 36 36 32 7d 20 54 68 69 73 20  * {H12662} This 
33de0 66 75 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65  function disable
33df0 73 20 61 75 74 6f 6d 61 74 69 63 20 65 78 74 65  s automatic exte
33e00 6e 73 69 6f 6e 73 20 69 6e 20 61 6c 6c 20 74 68  nsions in all th
33e10 72 65 61 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  reads..*/.SQLITE
33e20 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
33e30 33 5f 72 65 73 65 74 5f 61 75 74 6f 5f 65 78 74  3_reset_auto_ext
33e40 65 6e 73 69 6f 6e 28 76 6f 69 64 29 3b 0a 0a 2f  ension(void);../
33e50 2a 0a 2a 2a 2a 2a 2a 2a 20 45 58 50 45 52 49 4d  *.****** EXPERIM
33e60 45 4e 54 41 4c 20 2d 20 73 75 62 6a 65 63 74 20  ENTAL - subject 
33e70 74 6f 20 63 68 61 6e 67 65 20 77 69 74 68 6f 75  to change withou
33e80 74 20 6e 6f 74 69 63 65 20 2a 2a 2a 2a 2a 2a 2a  t notice *******
33e90 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
33ea0 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74  e interface to t
33eb0 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65  he virtual-table
33ec0 20 6d 65 63 68 61 6e 69 73 6d 20 69 73 20 63 75   mechanism is cu
33ed0 72 72 65 6e 74 6c 79 20 63 6f 6e 73 69 64 65 72  rrently consider
33ee0 65 64 0a 2a 2a 20 74 6f 20 62 65 20 65 78 70 65  ed.** to be expe
33ef0 72 69 6d 65 6e 74 61 6c 2e 20 20 54 68 65 20 69  rimental.  The i
33f00 6e 74 65 72 66 61 63 65 20 6d 69 67 68 74 20 63  nterface might c
33f10 68 61 6e 67 65 20 69 6e 20 69 6e 63 6f 6d 70 61  hange in incompa
33f20 74 69 62 6c 65 20 77 61 79 73 2e 0a 2a 2a 20 49  tible ways..** I
33f30 66 20 74 68 69 73 20 69 73 20 61 20 70 72 6f 62  f this is a prob
33f40 6c 65 6d 20 66 6f 72 20 79 6f 75 2c 20 64 6f 20  lem for you, do 
33f50 6e 6f 74 20 75 73 65 20 74 68 65 20 69 6e 74 65  not use the inte
33f60 72 66 61 63 65 20 61 74 20 74 68 69 73 20 74 69  rface at this ti
33f70 6d 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74  me..**.** When t
33f80 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65  he virtual-table
33f90 20 6d 65 63 68 61 6e 69 73 6d 20 73 74 61 62 69   mechanism stabi
33fa0 6c 69 7a 65 73 2c 20 77 65 20 77 69 6c 6c 20 64  lizes, we will d
33fb0 65 63 6c 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e  eclare the.** in
33fc0 74 65 72 66 61 63 65 20 66 69 78 65 64 2c 20 73  terface fixed, s
33fd0 75 70 70 6f 72 74 20 69 74 20 69 6e 64 65 66 69  upport it indefi
33fe0 6e 69 74 65 6c 79 2c 20 61 6e 64 20 72 65 6d 6f  nitely, and remo
33ff0 76 65 20 74 68 69 73 20 63 6f 6d 6d 65 6e 74 2e  ve this comment.
34000 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63  .*/../*.** Struc
34010 74 75 72 65 73 20 75 73 65 64 20 62 79 20 74 68  tures used by th
34020 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
34030 69 6e 74 65 72 66 61 63 65 0a 2a 2f 0a 74 79 70  interface.*/.typ
34040 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69  edef struct sqli
34050 74 65 33 5f 76 74 61 62 20 73 71 6c 69 74 65 33  te3_vtab sqlite3
34060 5f 76 74 61 62 3b 0a 74 79 70 65 64 65 66 20 73  _vtab;.typedef s
34070 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e  truct sqlite3_in
34080 64 65 78 5f 69 6e 66 6f 20 73 71 6c 69 74 65 33  dex_info sqlite3
34090 5f 69 6e 64 65 78 5f 69 6e 66 6f 3b 0a 74 79 70  _index_info;.typ
340a0 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69  edef struct sqli
340b0 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
340c0 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
340d0 73 6f 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72  sor;.typedef str
340e0 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  uct sqlite3_modu
340f0 6c 65 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  le sqlite3_modul
34100 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  e;../*.** CAPI3R
34110 45 46 3a 20 56 69 72 74 75 61 6c 20 54 61 62 6c  EF: Virtual Tabl
34120 65 20 4f 62 6a 65 63 74 20 7b 48 31 38 30 30 30  e Object {H18000
34130 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b 45  } <S20400>.** KE
34140 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 5f  YWORDS: sqlite3_
34150 6d 6f 64 75 6c 65 20 7b 76 69 72 74 75 61 6c 20  module {virtual 
34160 74 61 62 6c 65 20 6d 6f 64 75 6c 65 7d 0a 2a 2a  table module}.**
34170 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a   EXPERIMENTAL.**
34180 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75  .** This structu
34190 72 65 2c 20 73 6f 6d 65 74 69 6d 65 73 20 63 61  re, sometimes ca
341a0 6c 6c 65 64 20 61 20 61 20 22 76 69 72 74 75 61  lled a a "virtua
341b0 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 22 2c  l table module",
341c0 20 0a 2a 2a 20 64 65 66 69 6e 65 73 20 74 68 65   .** defines the
341d0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
341e0 6f 66 20 61 20 5b 76 69 72 74 75 61 6c 20 74 61  of a [virtual ta
341f0 62 6c 65 73 5d 2e 20 20 0a 2a 2a 20 54 68 69 73  bles].  .** This
34200 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 73 69   structure consi
34210 73 74 73 20 6d 6f 73 74 6c 79 20 6f 66 20 6d 65  sts mostly of me
34220 74 68 6f 64 73 20 66 6f 72 20 74 68 65 20 6d 6f  thods for the mo
34230 64 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 69  dule..**.** A vi
34240 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75  rtual table modu
34250 6c 65 20 69 73 20 63 72 65 61 74 65 64 20 62 79  le is created by
34260 20 66 69 6c 6c 69 6e 67 20 69 6e 20 61 20 70 65   filling in a pe
34270 72 73 69 73 74 65 6e 74 0a 2a 2a 20 69 6e 73 74  rsistent.** inst
34280 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72  ance of this str
34290 75 63 74 75 72 65 20 61 6e 64 20 70 61 73 73 69  ucture and passi
342a0 6e 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ng a pointer to 
342b0 74 68 61 74 20 69 6e 73 74 61 6e 63 65 0a 2a 2a  that instance.**
342c0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 72 65   to [sqlite3_cre
342d0 61 74 65 5f 6d 6f 64 75 6c 65 28 29 5d 20 6f 72  ate_module()] or
342e0 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65   [sqlite3_create
342f0 5f 6d 6f 64 75 6c 65 5f 76 32 28 29 5d 2e 0a 2a  _module_v2()]..*
34300 2a 20 54 68 65 20 72 65 67 69 73 74 72 61 74 69  * The registrati
34310 6f 6e 20 72 65 6d 61 69 6e 73 20 76 61 6c 69 64  on remains valid
34320 20 75 6e 74 69 6c 20 69 74 20 69 73 20 72 65 70   until it is rep
34330 6c 61 63 65 64 20 62 79 20 61 20 64 69 66 66 65  laced by a diffe
34340 72 65 6e 74 0a 2a 2a 20 6d 6f 64 75 6c 65 20 6f  rent.** module o
34350 72 20 75 6e 74 69 6c 20 74 68 65 20 5b 64 61 74  r until the [dat
34360 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
34370 5d 20 63 6c 6f 73 65 73 2e 20 20 54 68 65 20 63  ] closes.  The c
34380 6f 6e 74 65 6e 74 0a 2a 2a 20 6f 66 20 74 68 69  ontent.** of thi
34390 73 20 73 74 72 75 63 74 75 72 65 20 6d 75 73 74  s structure must
343a0 20 6e 6f 74 20 63 68 61 6e 67 65 20 77 68 69 6c   not change whil
343b0 65 20 69 74 20 69 73 20 72 65 67 69 73 74 65 72  e it is register
343c0 65 64 20 77 69 74 68 0a 2a 2a 20 61 6e 79 20 64  ed with.** any d
343d0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
343e0 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71  on..*/.struct sq
343f0 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 7b 0a 20  lite3_module {. 
34400 20 69 6e 74 20 69 56 65 72 73 69 6f 6e 3b 0a 20   int iVersion;. 
34410 20 69 6e 74 20 28 2a 78 43 72 65 61 74 65 29 28   int (*xCreate)(
34420 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 20 2a  sqlite3*, void *
34430 70 41 75 78 2c 0a 20 20 20 20 20 20 20 20 20 20  pAux,.          
34440 20 20 20 20 20 69 6e 74 20 61 72 67 63 2c 20 63       int argc, c
34450 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74  onst char *const
34460 2a 61 72 67 76 2c 0a 20 20 20 20 20 20 20 20 20  *argv,.         
34470 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74        sqlite3_vt
34480 61 62 20 2a 2a 70 70 56 54 61 62 2c 20 63 68 61  ab **ppVTab, cha
34490 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43  r**);.  int (*xC
344a0 6f 6e 6e 65 63 74 29 28 73 71 6c 69 74 65 33 2a  onnect)(sqlite3*
344b0 2c 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20  , void *pAux,.  
344c0 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74               int
344d0 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61   argc, const cha
344e0 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20  r *const*argv,. 
344f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
34500 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56  lite3_vtab **ppV
34510 54 61 62 2c 20 63 68 61 72 2a 2a 29 3b 0a 20 20  Tab, char**);.  
34520 69 6e 74 20 28 2a 78 42 65 73 74 49 6e 64 65 78  int (*xBestIndex
34530 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  )(sqlite3_vtab *
34540 70 56 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 69  pVTab, sqlite3_i
34550 6e 64 65 78 5f 69 6e 66 6f 2a 29 3b 0a 20 20 69  ndex_info*);.  i
34560 6e 74 20 28 2a 78 44 69 73 63 6f 6e 6e 65 63 74  nt (*xDisconnect
34570 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  )(sqlite3_vtab *
34580 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a  pVTab);.  int (*
34590 78 44 65 73 74 72 6f 79 29 28 73 71 6c 69 74 65  xDestroy)(sqlite
345a0 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a  3_vtab *pVTab);.
345b0 20 20 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28 73    int (*xOpen)(s
345c0 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54  qlite3_vtab *pVT
345d0 61 62 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62  ab, sqlite3_vtab
345e0 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73  _cursor **ppCurs
345f0 6f 72 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6c  or);.  int (*xCl
34600 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 76 74 61  ose)(sqlite3_vta
34610 62 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e  b_cursor*);.  in
34620 74 20 28 2a 78 46 69 6c 74 65 72 29 28 73 71 6c  t (*xFilter)(sql
34630 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
34640 2a 2c 20 69 6e 74 20 69 64 78 4e 75 6d 2c 20 63  *, int idxNum, c
34650 6f 6e 73 74 20 63 68 61 72 20 2a 69 64 78 53 74  onst char *idxSt
34660 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r,.             
34670 20 20 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c     int argc, sql
34680 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
34690 76 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4e 65 78  v);.  int (*xNex
346a0 74 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  t)(sqlite3_vtab_
346b0 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20  cursor*);.  int 
346c0 28 2a 78 45 6f 66 29 28 73 71 6c 69 74 65 33 5f  (*xEof)(sqlite3_
346d0 76 74 61 62 5f 63 75 72 73 6f 72 2a 29 3b 0a 20  vtab_cursor*);. 
346e0 20 69 6e 74 20 28 2a 78 43 6f 6c 75 6d 6e 29 28   int (*xColumn)(
346f0 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
34700 73 6f 72 2a 2c 20 73 71 6c 69 74 65 33 5f 63 6f  sor*, sqlite3_co
34710 6e 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 20 20  ntext*, int);.  
34720 69 6e 74 20 28 2a 78 52 6f 77 69 64 29 28 73 71  int (*xRowid)(sq
34730 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
34740 72 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  r*, sqlite3_int6
34750 34 20 2a 70 52 6f 77 69 64 29 3b 0a 20 20 69 6e  4 *pRowid);.  in
34760 74 20 28 2a 78 55 70 64 61 74 65 29 28 73 71 6c  t (*xUpdate)(sql
34770 69 74 65 33 5f 76 74 61 62 20 2a 2c 20 69 6e 74  ite3_vtab *, int
34780 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
34790 2a 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  **, sqlite3_int6
347a0 34 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 42  4 *);.  int (*xB
347b0 65 67 69 6e 29 28 73 71 6c 69 74 65 33 5f 76 74  egin)(sqlite3_vt
347c0 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e  ab *pVTab);.  in
347d0 74 20 28 2a 78 53 79 6e 63 29 28 73 71 6c 69 74  t (*xSync)(sqlit
347e0 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b  e3_vtab *pVTab);
347f0 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6d 6d 69 74  .  int (*xCommit
34800 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  )(sqlite3_vtab *
34810 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a  pVTab);.  int (*
34820 78 52 6f 6c 6c 62 61 63 6b 29 28 73 71 6c 69 74  xRollback)(sqlit
34830 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b  e3_vtab *pVTab);
34840 0a 20 20 69 6e 74 20 28 2a 78 46 69 6e 64 46 75  .  int (*xFindFu
34850 6e 63 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 5f  nction)(sqlite3_
34860 76 74 61 62 20 2a 70 56 74 61 62 2c 20 69 6e 74  vtab *pVtab, int
34870 20 6e 41 72 67 2c 20 63 6f 6e 73 74 20 63 68 61   nArg, const cha
34880 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20  r *zName,.      
34890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
348a0 20 76 6f 69 64 20 28 2a 2a 70 78 46 75 6e 63 29   void (**pxFunc)
348b0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
348c0 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61  *,int,sqlite3_va
348d0 6c 75 65 2a 2a 29 2c 0a 20 20 20 20 20 20 20 20  lue**),.        
348e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 76                 v
348f0 6f 69 64 20 2a 2a 70 70 41 72 67 29 3b 0a 20 20  oid **ppArg);.  
34900 69 6e 74 20 28 2a 78 52 65 6e 61 6d 65 29 28 73  int (*xRename)(s
34910 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
34920 61 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ab, const char *
34930 7a 4e 65 77 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  zNew);.};../*.**
34940 20 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75   CAPI3REF: Virtu
34950 61 6c 20 54 61 62 6c 65 20 49 6e 64 65 78 69 6e  al Table Indexin
34960 67 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 7b 48  g Information {H
34970 31 38 31 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a  18100} <S20400>.
34980 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c  ** KEYWORDS: sql
34990 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 0a  ite3_index_info.
349a0 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a  ** EXPERIMENTAL.
349b0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
349c0 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72  3_index_info str
349d0 75 63 74 75 72 65 20 61 6e 64 20 69 74 73 20 73  ucture and its s
349e0 75 62 73 74 72 75 63 74 75 72 65 73 20 69 73 20  ubstructures is 
349f0 75 73 65 64 20 74 6f 0a 2a 2a 20 70 61 73 73 20  used to.** pass 
34a00 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 74 6f  information into
34a10 20 61 6e 64 20 72 65 63 65 69 76 65 20 74 68 65   and receive the
34a20 20 72 65 70 6c 79 20 66 72 6f 6d 20 74 68 65 20   reply from the 
34a30 5b 78 42 65 73 74 49 6e 64 65 78 5d 0a 2a 2a 20  [xBestIndex].** 
34a40 6d 65 74 68 6f 64 20 6f 66 20 61 20 5b 76 69 72  method of a [vir
34a50 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c  tual table modul
34a60 65 5d 2e 20 20 54 68 65 20 66 69 65 6c 64 73 20  e].  The fields 
34a70 75 6e 64 65 72 20 2a 2a 49 6e 70 75 74 73 2a 2a  under **Inputs**
34a80 20 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e 70 75   are the.** inpu
34a90 74 73 20 74 6f 20 78 42 65 73 74 49 6e 64 65 78  ts to xBestIndex
34aa0 20 61 6e 64 20 61 72 65 20 72 65 61 64 2d 6f 6e   and are read-on
34ab0 6c 79 2e 20 20 78 42 65 73 74 49 6e 64 65 78 20  ly.  xBestIndex 
34ac0 69 6e 73 65 72 74 73 20 69 74 73 0a 2a 2a 20 72  inserts its.** r
34ad0 65 73 75 6c 74 73 20 69 6e 74 6f 20 74 68 65 20  esults into the 
34ae0 2a 2a 4f 75 74 70 75 74 73 2a 2a 20 66 69 65 6c  **Outputs** fiel
34af0 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 43  ds..**.** The aC
34b00 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 61 72 72 61  onstraint[] arra
34b10 79 20 72 65 63 6f 72 64 73 20 57 48 45 52 45 20  y records WHERE 
34b20 63 6c 61 75 73 65 20 63 6f 6e 73 74 72 61 69 6e  clause constrain
34b30 74 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a  ts of the form:.
34b40 2a 2a 0a 2a 2a 20 3c 70 72 65 3e 63 6f 6c 75 6d  **.** <pre>colum
34b50 6e 20 4f 50 20 65 78 70 72 3c 2f 70 72 65 3e 0a  n OP expr</pre>.
34b60 2a 2a 0a 2a 2a 20 77 68 65 72 65 20 4f 50 20 69  **.** where OP i
34b70 73 20 3d 2c 20 26 6c 74 3b 2c 20 26 6c 74 3b 3d  s =, &lt;, &lt;=
34b80 2c 20 26 67 74 3b 2c 20 6f 72 20 26 67 74 3b 3d  , &gt;, or &gt;=
34b90 2e 20 20 54 68 65 20 70 61 72 74 69 63 75 6c 61  .  The particula
34ba0 72 20 6f 70 65 72 61 74 6f 72 20 69 73 0a 2a 2a  r operator is.**
34bb0 20 73 74 6f 72 65 64 20 69 6e 20 61 43 6f 6e 73   stored in aCons
34bc0 74 72 61 69 6e 74 5b 5d 2e 6f 70 2e 20 20 54 68  traint[].op.  Th
34bd0 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 63  e index of the c
34be0 6f 6c 75 6d 6e 20 69 73 20 73 74 6f 72 65 64 20  olumn is stored 
34bf0 69 6e 0a 2a 2a 20 61 43 6f 6e 73 74 72 61 69 6e  in.** aConstrain
34c00 74 5b 5d 2e 69 43 6f 6c 75 6d 6e 2e 20 20 61 43  t[].iColumn.  aC
34c10 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e 75 73 61 62  onstraint[].usab
34c20 6c 65 20 69 73 20 54 52 55 45 20 69 66 20 74 68  le is TRUE if th
34c30 65 0a 2a 2a 20 65 78 70 72 20 6f 6e 20 74 68 65  e.** expr on the
34c40 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65   right-hand side
34c50 20 63 61 6e 20 62 65 20 65 76 61 6c 75 61 74 65   can be evaluate
34c60 64 20 28 61 6e 64 20 74 68 75 73 20 74 68 65 20  d (and thus the 
34c70 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 69 73  constraint.** is
34c80 20 75 73 61 62 6c 65 29 20 61 6e 64 20 66 61 6c   usable) and fal
34c90 73 65 20 69 66 20 69 74 20 63 61 6e 6e 6f 74 2e  se if it cannot.
34ca0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 74 69 6d  .**.** The optim
34cb0 69 7a 65 72 20 61 75 74 6f 6d 61 74 69 63 61 6c  izer automatical
34cc0 6c 79 20 69 6e 76 65 72 74 73 20 74 65 72 6d 73  ly inverts terms
34cd0 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 65 78   of the form "ex
34ce0 70 72 20 4f 50 20 63 6f 6c 75 6d 6e 22 0a 2a 2a  pr OP column".**
34cf0 20 61 6e 64 20 6d 61 6b 65 73 20 6f 74 68 65 72   and makes other
34d00 20 73 69 6d 70 6c 69 66 69 63 61 74 69 6f 6e 73   simplifications
34d10 20 74 6f 20 74 68 65 20 57 48 45 52 45 20 63 6c   to the WHERE cl
34d20 61 75 73 65 20 69 6e 20 61 6e 20 61 74 74 65 6d  ause in an attem
34d30 70 74 20 74 6f 0a 2a 2a 20 67 65 74 20 61 73 20  pt to.** get as 
34d40 6d 61 6e 79 20 57 48 45 52 45 20 63 6c 61 75 73  many WHERE claus
34d50 65 20 74 65 72 6d 73 20 69 6e 74 6f 20 74 68 65  e terms into the
34d60 20 66 6f 72 6d 20 73 68 6f 77 6e 20 61 62 6f 76   form shown abov
34d70 65 20 61 73 20 70 6f 73 73 69 62 6c 65 2e 0a 2a  e as possible..*
34d80 2a 20 54 68 65 20 61 43 6f 6e 73 74 72 61 69 6e  * The aConstrain
34d90 74 5b 5d 20 61 72 72 61 79 20 6f 6e 6c 79 20 72  t[] array only r
34da0 65 70 6f 72 74 73 20 57 48 45 52 45 20 63 6c 61  eports WHERE cla
34db0 75 73 65 20 74 65 72 6d 73 20 69 6e 20 74 68 65  use terms in the
34dc0 20 63 6f 72 72 65 63 74 0a 2a 2a 20 66 6f 72 6d   correct.** form
34dd0 20 74 68 61 74 20 72 65 66 65 72 20 74 6f 20 74   that refer to t
34de0 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 76 69  he particular vi
34df0 72 74 75 61 6c 20 74 61 62 6c 65 20 62 65 69 6e  rtual table bein
34e00 67 20 71 75 65 72 69 65 64 2e 0a 2a 2a 0a 2a 2a  g queried..**.**
34e10 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   Information abo
34e20 75 74 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  ut the ORDER BY 
34e30 63 6c 61 75 73 65 20 69 73 20 73 74 6f 72 65 64  clause is stored
34e40 20 69 6e 20 61 4f 72 64 65 72 42 79 5b 5d 2e 0a   in aOrderBy[]..
34e50 2a 2a 20 45 61 63 68 20 74 65 72 6d 20 6f 66 20  ** Each term of 
34e60 61 4f 72 64 65 72 42 79 20 72 65 63 6f 72 64 73  aOrderBy records
34e70 20 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65   a column of the
34e80 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
34e90 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 78 42 65  ..**.** The [xBe
34ea0 73 74 49 6e 64 65 78 5d 20 6d 65 74 68 6f 64 20  stIndex] method 
34eb0 6d 75 73 74 20 66 69 6c 6c 20 61 43 6f 6e 73 74  must fill aConst
34ec0 72 61 69 6e 74 55 73 61 67 65 5b 5d 20 77 69 74  raintUsage[] wit
34ed0 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a  h information.**
34ee0 20 61 62 6f 75 74 20 77 68 61 74 20 70 61 72 61   about what para
34ef0 6d 65 74 65 72 73 20 74 6f 20 70 61 73 73 20 74  meters to pass t
34f00 6f 20 78 46 69 6c 74 65 72 2e 20 20 49 66 20 61  o xFilter.  If a
34f10 72 67 76 49 6e 64 65 78 3e 30 20 74 68 65 6e 0a  rgvIndex>0 then.
34f20 2a 2a 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e  ** the right-han
34f30 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 63 6f  d side of the co
34f40 72 72 65 73 70 6f 6e 64 69 6e 67 20 61 43 6f 6e  rresponding aCon
34f50 73 74 72 61 69 6e 74 5b 5d 20 69 73 20 65 76 61  straint[] is eva
34f60 6c 75 61 74 65 64 0a 2a 2a 20 61 6e 64 20 62 65  luated.** and be
34f70 63 6f 6d 65 73 20 74 68 65 20 61 72 67 76 49 6e  comes the argvIn
34f80 64 65 78 2d 74 68 20 65 6e 74 72 79 20 69 6e 20  dex-th entry in 
34f90 61 72 67 76 2e 20 20 49 66 20 61 43 6f 6e 73 74  argv.  If aConst
34fa0 72 61 69 6e 74 55 73 61 67 65 5b 5d 2e 6f 6d 69  raintUsage[].omi
34fb0 74 0a 2a 2a 20 69 73 20 74 72 75 65 2c 20 74 68  t.** is true, th
34fc0 65 6e 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  en the constrain
34fd0 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20  t is assumed to 
34fe0 62 65 20 66 75 6c 6c 79 20 68 61 6e 64 6c 65 64  be fully handled
34ff0 20 62 79 20 74 68 65 0a 2a 2a 20 76 69 72 74 75   by the.** virtu
35000 61 6c 20 74 61 62 6c 65 20 61 6e 64 20 69 73 20  al table and is 
35010 6e 6f 74 20 63 68 65 63 6b 65 64 20 61 67 61 69  not checked agai
35020 6e 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a  n by SQLite..**.
35030 2a 2a 20 54 68 65 20 69 64 78 4e 75 6d 20 61 6e  ** The idxNum an
35040 64 20 69 64 78 50 74 72 20 76 61 6c 75 65 73 20  d idxPtr values 
35050 61 72 65 20 72 65 63 6f 72 64 65 64 20 61 6e 64  are recorded and
35060 20 70 61 73 73 65 64 20 69 6e 74 6f 20 74 68 65   passed into the
35070 0a 2a 2a 20 5b 78 46 69 6c 74 65 72 5d 20 6d 65  .** [xFilter] me
35080 74 68 6f 64 2e 0a 2a 2a 20 5b 73 71 6c 69 74 65  thod..** [sqlite
35090 33 5f 66 72 65 65 28 29 5d 20 69 73 20 75 73 65  3_free()] is use
350a0 64 20 74 6f 20 66 72 65 65 20 69 64 78 50 74 72  d to free idxPtr
350b0 20 69 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66 66   if and only iff
350c0 0a 2a 2a 20 6e 65 65 64 54 6f 46 72 65 65 49 64  .** needToFreeId
350d0 78 50 74 72 20 69 73 20 74 72 75 65 2e 0a 2a 2a  xPtr is true..**
350e0 0a 2a 2a 20 54 68 65 20 6f 72 64 65 72 42 79 43  .** The orderByC
350f0 6f 6e 73 75 6d 65 64 20 6d 65 61 6e 73 20 74 68  onsumed means th
35100 61 74 20 6f 75 74 70 75 74 20 66 72 6f 6d 20 5b  at output from [
35110 78 46 69 6c 74 65 72 5d 2f 5b 78 4e 65 78 74 5d  xFilter]/[xNext]
35120 20 77 69 6c 6c 20 6f 63 63 75 72 20 69 6e 0a 2a   will occur in.*
35130 2a 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 72  * the correct or
35140 64 65 72 20 74 6f 20 73 61 74 69 73 66 79 20 74  der to satisfy t
35150 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
35160 73 65 20 73 6f 20 74 68 61 74 20 6e 6f 20 73 65  se so that no se
35170 70 61 72 61 74 65 0a 2a 2a 20 73 6f 72 74 69 6e  parate.** sortin
35180 67 20 73 74 65 70 20 69 73 20 72 65 71 75 69 72  g step is requir
35190 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 73  ed..**.** The es
351a0 74 69 6d 61 74 65 64 43 6f 73 74 20 76 61 6c 75  timatedCost valu
351b0 65 20 69 73 20 61 6e 20 65 73 74 69 6d 61 74 65  e is an estimate
351c0 20 6f 66 20 74 68 65 20 63 6f 73 74 20 6f 66 20   of the cost of 
351d0 64 6f 69 6e 67 20 74 68 65 0a 2a 2a 20 70 61 72  doing the.** par
351e0 74 69 63 75 6c 61 72 20 6c 6f 6f 6b 75 70 2e 20  ticular lookup. 
351f0 20 41 20 66 75 6c 6c 20 73 63 61 6e 20 6f 66 20   A full scan of 
35200 61 20 74 61 62 6c 65 20 77 69 74 68 20 4e 20 65  a table with N e
35210 6e 74 72 69 65 73 20 73 68 6f 75 6c 64 20 68 61  ntries should ha
35220 76 65 0a 2a 2a 20 61 20 63 6f 73 74 20 6f 66 20  ve.** a cost of 
35230 4e 2e 20 20 41 20 62 69 6e 61 72 79 20 73 65 61  N.  A binary sea
35240 72 63 68 20 6f 66 20 61 20 74 61 62 6c 65 20 6f  rch of a table o
35250 66 20 4e 20 65 6e 74 72 69 65 73 20 73 68 6f 75  f N entries shou
35260 6c 64 20 68 61 76 65 20 61 0a 2a 2a 20 63 6f 73  ld have a.** cos
35270 74 20 6f 66 20 61 70 70 72 6f 78 69 6d 61 74 65  t of approximate
35280 6c 79 20 6c 6f 67 28 4e 29 2e 0a 2a 2f 0a 73 74  ly log(N)..*/.st
35290 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64  ruct sqlite3_ind
352a0 65 78 5f 69 6e 66 6f 20 7b 0a 20 20 2f 2a 20 49  ex_info {.  /* I
352b0 6e 70 75 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  nputs */.  int n
352c0 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20  Constraint;     
352d0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
352e0 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 43  of entries in aC
352f0 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 73  onstraint */.  s
35300 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e  truct sqlite3_in
35310 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20 7b  dex_constraint {
35320 0a 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d  .     int iColum
35330 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
35340 2f 2a 20 43 6f 6c 75 6d 6e 20 6f 6e 20 6c 65 66  /* Column on lef
35350 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 63  t-hand side of c
35360 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 20  onstraint */.   
35370 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
35380 6f 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43  op;         /* C
35390 6f 6e 73 74 72 61 69 6e 74 20 6f 70 65 72 61 74  onstraint operat
353a0 6f 72 20 2a 2f 0a 20 20 20 20 20 75 6e 73 69 67  or */.     unsig
353b0 6e 65 64 20 63 68 61 72 20 75 73 61 62 6c 65 3b  ned char usable;
353c0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
353d0 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 20  this constraint 
353e0 69 73 20 75 73 61 62 6c 65 20 2a 2f 0a 20 20 20  is usable */.   
353f0 20 20 69 6e 74 20 69 54 65 72 6d 4f 66 66 73 65    int iTermOffse
35400 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55  t;          /* U
35410 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 2d  sed internally -
35420 20 78 42 65 73 74 49 6e 64 65 78 20 73 68 6f 75   xBestIndex shou
35430 6c 64 20 69 67 6e 6f 72 65 20 2a 2f 0a 20 20 7d  ld ignore */.  }
35440 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20   *aConstraint;  
35450 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62            /* Tab
35460 6c 65 20 6f 66 20 57 48 45 52 45 20 63 6c 61 75  le of WHERE clau
35470 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a  se constraints *
35480 2f 0a 20 20 69 6e 74 20 6e 4f 72 64 65 72 42 79  /.  int nOrderBy
35490 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
354a0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d  * Number of term
354b0 73 20 69 6e 20 74 68 65 20 4f 52 44 45 52 20 42  s in the ORDER B
354c0 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74  Y clause */.  st
354d0 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64  ruct sqlite3_ind
354e0 65 78 5f 6f 72 64 65 72 62 79 20 7b 0a 20 20 20  ex_orderby {.   
354f0 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20    int iColumn;  
35500 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
35510 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a  olumn number */.
35520 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68       unsigned ch
35530 61 72 20 64 65 73 63 3b 20 20 20 20 20 20 20 2f  ar desc;       /
35540 2a 20 54 72 75 65 20 66 6f 72 20 44 45 53 43 2e  * True for DESC.
35550 20 20 46 61 6c 73 65 20 66 6f 72 20 41 53 43 2e    False for ASC.
35560 20 2a 2f 0a 20 20 7d 20 2a 61 4f 72 64 65 72 42   */.  } *aOrderB
35570 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y;              
35580 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59   /* The ORDER BY
35590 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 2f 2a 20   clause */.  /* 
355a0 4f 75 74 70 75 74 73 20 2a 2f 0a 20 20 73 74 72  Outputs */.  str
355b0 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
355c0 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61  x_constraint_usa
355d0 67 65 20 7b 0a 20 20 20 20 69 6e 74 20 61 72 67  ge {.    int arg
355e0 76 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20 20  vIndex;         
355f0 20 20 2f 2a 20 69 66 20 3e 30 2c 20 63 6f 6e 73    /* if >0, cons
35600 74 72 61 69 6e 74 20 69 73 20 70 61 72 74 20 6f  traint is part o
35610 66 20 61 72 67 76 20 74 6f 20 78 46 69 6c 74 65  f argv to xFilte
35620 72 20 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65  r */.    unsigne
35630 64 20 63 68 61 72 20 6f 6d 69 74 3b 20 20 20 20  d char omit;    
35640 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 63 6f 64 65    /* Do not code
35650 20 61 20 74 65 73 74 20 66 6f 72 20 74 68 69 73   a test for this
35660 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20   constraint */. 
35670 20 7d 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 55   } *aConstraintU
35680 73 61 67 65 3b 0a 20 20 69 6e 74 20 69 64 78 4e  sage;.  int idxN
35690 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  um;             
356a0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 75 73 65     /* Number use
356b0 64 20 74 6f 20 69 64 65 6e 74 69 66 79 20 74 68  d to identify th
356c0 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 63 68 61  e index */.  cha
356d0 72 20 2a 69 64 78 53 74 72 3b 20 20 20 20 20 20  r *idxStr;      
356e0 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e          /* Strin
356f0 67 2c 20 70 6f 73 73 69 62 6c 79 20 6f 62 74 61  g, possibly obta
35700 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
35710 33 5f 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 69 6e  3_malloc */.  in
35720 74 20 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53  t needToFreeIdxS
35730 74 72 3b 20 20 20 20 20 20 2f 2a 20 46 72 65 65  tr;      /* Free
35740 20 69 64 78 53 74 72 20 75 73 69 6e 67 20 73 71   idxStr using sq
35750 6c 69 74 65 33 5f 66 72 65 65 28 29 20 69 66 20  lite3_free() if 
35760 74 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 72  true */.  int or
35770 64 65 72 42 79 43 6f 6e 73 75 6d 65 64 3b 20 20  derByConsumed;  
35780 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
35790 6f 75 74 70 75 74 20 69 73 20 61 6c 72 65 61 64  output is alread
357a0 79 20 6f 72 64 65 72 65 64 20 2a 2f 0a 20 20 64  y ordered */.  d
357b0 6f 75 62 6c 65 20 65 73 74 69 6d 61 74 65 64 43  ouble estimatedC
357c0 6f 73 74 3b 20 20 20 20 20 20 2f 2a 20 45 73 74  ost;      /* Est
357d0 69 6d 61 74 65 64 20 63 6f 73 74 20 6f 66 20 75  imated cost of u
357e0 73 69 6e 67 20 74 68 69 73 20 69 6e 64 65 78 20  sing this index 
357f0 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 53 51  */.};.#define SQ
35800 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54  LITE_INDEX_CONST
35810 52 41 49 4e 54 5f 45 51 20 20 20 20 32 0a 23 64  RAINT_EQ    2.#d
35820 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44  efine SQLITE_IND
35830 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 54  EX_CONSTRAINT_GT
35840 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51      4.#define SQ
35850 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54  LITE_INDEX_CONST
35860 52 41 49 4e 54 5f 4c 45 20 20 20 20 38 0a 23 64  RAINT_LE    8.#d
35870 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44  efine SQLITE_IND
35880 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 54  EX_CONSTRAINT_LT
35890 20 20 20 20 31 36 0a 23 64 65 66 69 6e 65 20 53      16.#define S
358a0 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53  QLITE_INDEX_CONS
358b0 54 52 41 49 4e 54 5f 47 45 20 20 20 20 33 32 0a  TRAINT_GE    32.
358c0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
358d0 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f  NDEX_CONSTRAINT_
358e0 4d 41 54 43 48 20 36 34 0a 0a 2f 2a 0a 2a 2a 20  MATCH 64../*.** 
358f0 43 41 50 49 33 52 45 46 3a 20 52 65 67 69 73 74  CAPI3REF: Regist
35900 65 72 20 41 20 56 69 72 74 75 61 6c 20 54 61 62  er A Virtual Tab
35910 6c 65 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  le Implementatio
35920 6e 20 7b 48 31 38 32 30 30 7d 20 3c 53 32 30 34  n {H18200} <S204
35930 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  00>.** EXPERIMEN
35940 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  TAL.**.** This r
35950 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 74  outine is used t
35960 6f 20 72 65 67 69 73 74 65 72 20 61 20 6e 65 77  o register a new
35970 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20   [virtual table 
35980 6d 6f 64 75 6c 65 5d 20 6e 61 6d 65 2e 0a 2a 2a  module] name..**
35990 20 4d 6f 64 75 6c 65 20 6e 61 6d 65 73 20 6d 75   Module names mu
359a0 73 74 20 62 65 20 72 65 67 69 73 74 65 72 65 64  st be registered
359b0 20 62 65 66 6f 72 65 0a 2a 2a 20 63 72 65 61 74   before.** creat
359c0 69 6e 67 20 61 20 6e 65 77 20 5b 76 69 72 74 75  ing a new [virtu
359d0 61 6c 20 74 61 62 6c 65 5d 20 75 73 69 6e 67 20  al table] using 
359e0 74 68 65 20 6d 6f 64 75 6c 65 2c 20 6f 72 20 62  the module, or b
359f0 65 66 6f 72 65 20 75 73 69 6e 67 20 61 0a 2a 2a  efore using a.**
35a00 20 70 72 65 65 78 69 73 74 69 6e 67 20 5b 76 69   preexisting [vi
35a10 72 74 75 61 6c 20 74 61 62 6c 65 5d 20 66 6f 72  rtual table] for
35a20 20 74 68 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a   the module..**.
35a30 2a 2a 20 54 68 65 20 6d 6f 64 75 6c 65 20 6e 61  ** The module na
35a40 6d 65 20 69 73 20 72 65 67 69 73 74 65 72 65 64  me is registered
35a50 20 6f 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73   on the [databas
35a60 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70  e connection] sp
35a70 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 74 68  ecified.** by th
35a80 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  e first paramete
35a90 72 2e 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20  r.  The name of 
35aa0 74 68 65 20 6d 6f 64 75 6c 65 20 69 73 20 67 69  the module is gi
35ab0 76 65 6e 20 62 79 20 74 68 65 20 0a 2a 2a 20 73  ven by the .** s
35ac0 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2e  econd parameter.
35ad0 20 20 54 68 65 20 74 68 69 72 64 20 70 61 72 61    The third para
35ae0 6d 65 74 65 72 20 69 73 20 61 20 70 6f 69 6e 74  meter is a point
35af0 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20 69 6d 70  er to.** the imp
35b00 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
35b10 68 65 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c  he [virtual tabl
35b20 65 20 6d 6f 64 75 6c 65 5d 2e 20 20 20 54 68 65  e module].   The
35b30 20 66 6f 75 72 74 68 0a 2a 2a 20 70 61 72 61 6d   fourth.** param
35b40 65 74 65 72 20 69 73 20 61 6e 20 61 72 62 69 74  eter is an arbit
35b50 72 61 72 79 20 63 6c 69 65 6e 74 20 64 61 74 61  rary client data
35b60 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 69 73   pointer that is
35b70 20 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 0a   passed through.
35b80 2a 2a 20 69 6e 74 6f 20 74 68 65 20 5b 78 43 72  ** into the [xCr
35b90 65 61 74 65 5d 20 61 6e 64 20 5b 78 43 6f 6e 6e  eate] and [xConn
35ba0 65 63 74 5d 20 6d 65 74 68 6f 64 73 20 6f 66 20  ect] methods of 
35bb0 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  the virtual tabl
35bc0 65 20 6d 6f 64 75 6c 65 0a 2a 2a 20 77 68 65 6e  e module.** when
35bd0 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c 20 74   a new virtual t
35be0 61 62 6c 65 20 69 73 20 62 65 20 62 65 69 6e 67  able is be being
35bf0 20 63 72 65 61 74 65 64 20 6f 72 20 72 65 69 6e   created or rein
35c00 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a  itialized..**.**
35c10 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20   This interface 
35c20 68 61 73 20 65 78 61 63 74 6c 79 20 74 68 65 20  has exactly the 
35c30 73 61 6d 65 20 65 66 66 65 63 74 20 61 73 20 63  same effect as c
35c40 61 6c 6c 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74  alling.** [sqlit
35c50 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
35c60 5f 76 32 28 29 5d 20 77 69 74 68 20 61 20 4e 55  _v2()] with a NU
35c70 4c 4c 20 63 6c 69 65 6e 74 20 64 61 74 61 20 64  LL client data d
35c80 65 73 74 72 75 63 74 6f 72 2e 0a 2a 2f 0a 53 51  estructor..*/.SQ
35c90 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f  LITE_API SQLITE_
35ca0 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74  EXPERIMENTAL int
35cb0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
35cc0 6d 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 74 65  module(.  sqlite
35cd0 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
35ce0 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 63       /* SQLite c
35cf0 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65 67  onnection to reg
35d00 69 73 74 65 72 20 6d 6f 64 75 6c 65 20 77 69 74  ister module wit
35d10 68 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  h */.  const cha
35d20 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20  r *zName,       
35d30 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
35d40 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e   module */.  con
35d50 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  st sqlite3_modul
35d60 65 20 2a 70 2c 20 20 20 2f 2a 20 4d 65 74 68 6f  e *p,   /* Metho
35d70 64 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c  ds for the modul
35d80 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6c  e */.  void *pCl
35d90 69 65 6e 74 44 61 74 61 20 20 20 20 20 20 20 20  ientData        
35da0 20 20 2f 2a 20 43 6c 69 65 6e 74 20 64 61 74 61    /* Client data
35db0 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f   for xCreate/xCo
35dc0 6e 6e 65 63 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a  nnect */.);../*.
35dd0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 67  ** CAPI3REF: Reg
35de0 69 73 74 65 72 20 41 20 56 69 72 74 75 61 6c 20  ister A Virtual 
35df0 54 61 62 6c 65 20 49 6d 70 6c 65 6d 65 6e 74 61  Table Implementa
35e00 74 69 6f 6e 20 7b 48 31 38 32 31 30 7d 20 3c 53  tion {H18210} <S
35e10 32 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49  20400>.** EXPERI
35e20 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69  MENTAL.**.** Thi
35e30 73 20 72 6f 75 74 69 6e 65 20 69 73 20 69 64 65  s routine is ide
35e40 6e 74 69 63 61 6c 20 74 6f 20 74 68 65 20 5b 73  ntical to the [s
35e50 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f  qlite3_create_mo
35e60 64 75 6c 65 28 29 5d 20 6d 65 74 68 6f 64 2c 0a  dule()] method,.
35e70 2a 2a 20 65 78 63 65 70 74 20 74 68 61 74 20 69  ** except that i
35e80 74 20 68 61 73 20 61 6e 20 65 78 74 72 61 20 70  t has an extra p
35e90 61 72 61 6d 65 74 65 72 20 74 6f 20 73 70 65 63  arameter to spec
35ea0 69 66 79 20 0a 2a 2a 20 61 20 64 65 73 74 72 75  ify .** a destru
35eb0 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f  ctor function fo
35ec0 72 20 74 68 65 20 63 6c 69 65 6e 74 20 64 61 74  r the client dat
35ed0 61 20 70 6f 69 6e 74 65 72 2e 20 20 53 51 4c 69  a pointer.  SQLi
35ee0 74 65 20 77 69 6c 6c 0a 2a 2a 20 69 6e 76 6f 6b  te will.** invok
35ef0 65 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72  e the destructor
35f00 20 66 75 6e 63 74 69 6f 6e 20 28 69 66 20 69 74   function (if it
35f10 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 29 20 77 68   is not NULL) wh
35f20 65 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 6e 6f 20  en SQLite.** no 
35f30 6c 6f 6e 67 65 72 20 6e 65 65 64 73 20 74 68 65  longer needs the
35f40 20 70 43 6c 69 65 6e 74 44 61 74 61 20 70 6f 69   pClientData poi
35f50 6e 74 65 72 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54  nter.  .*/.SQLIT
35f60 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50  E_API SQLITE_EXP
35f70 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71  ERIMENTAL int sq
35f80 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64  lite3_create_mod
35f90 75 6c 65 5f 76 32 28 0a 20 20 73 71 6c 69 74 65  ule_v2(.  sqlite
35fa0 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
35fb0 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 63       /* SQLite c
35fc0 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65 67  onnection to reg
35fd0 69 73 74 65 72 20 6d 6f 64 75 6c 65 20 77 69 74  ister module wit
35fe0 68 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  h */.  const cha
35ff0 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20  r *zName,       
36000 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
36010 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e   module */.  con
36020 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  st sqlite3_modul
36030 65 20 2a 70 2c 20 20 20 2f 2a 20 4d 65 74 68 6f  e *p,   /* Metho
36040 64 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c  ds for the modul
36050 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6c  e */.  void *pCl
36060 69 65 6e 74 44 61 74 61 2c 20 20 20 20 20 20 20  ientData,       
36070 20 20 2f 2a 20 43 6c 69 65 6e 74 20 64 61 74 61    /* Client data
36080 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f   for xCreate/xCo
36090 6e 6e 65 63 74 20 2a 2f 0a 20 20 76 6f 69 64 28  nnect */.  void(
360a0 2a 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64 2a  *xDestroy)(void*
360b0 29 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20  )     /* Module 
360c0 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74  destructor funct
360d0 69 6f 6e 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a  ion */.);../*.**
360e0 20 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75   CAPI3REF: Virtu
360f0 61 6c 20 54 61 62 6c 65 20 49 6e 73 74 61 6e 63  al Table Instanc
36100 65 20 4f 62 6a 65 63 74 20 7b 48 31 38 30 31 30  e Object {H18010
36110 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b 45  } <S20400>.** KE
36120 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 5f  YWORDS: sqlite3_
36130 76 74 61 62 0a 2a 2a 20 45 58 50 45 52 49 4d 45  vtab.** EXPERIME
36140 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79  NTAL.**.** Every
36150 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20   [virtual table 
36160 6d 6f 64 75 6c 65 5d 20 69 6d 70 6c 65 6d 65 6e  module] implemen
36170 74 61 74 69 6f 6e 20 75 73 65 73 20 61 20 73 75  tation uses a su
36180 62 63 6c 61 73 73 0a 2a 2a 20 6f 66 20 74 68 65  bclass.** of the
36190 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
361a0 74 75 72 65 20 74 6f 20 64 65 73 63 72 69 62 65  ture to describe
361b0 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 69 6e   a particular in
361c0 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65  stance.** of the
361d0 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d   [virtual table]
361e0 2e 20 20 45 61 63 68 20 73 75 62 63 6c 61 73 73  .  Each subclass
361f0 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 74 61 69 6c   will.** be tail
36200 6f 72 65 64 20 74 6f 20 74 68 65 20 73 70 65 63  ored to the spec
36210 69 66 69 63 20 6e 65 65 64 73 20 6f 66 20 74 68  ific needs of th
36220 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65  e module impleme
36230 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20  ntation..** The 
36240 70 75 72 70 6f 73 65 20 6f 66 20 74 68 69 73 20  purpose of this 
36250 73 75 70 65 72 63 6c 61 73 73 20 69 73 20 74 6f  superclass is to
36260 20 64 65 66 69 6e 65 20 63 65 72 74 61 69 6e 20   define certain 
36270 66 69 65 6c 64 73 20 74 68 61 74 20 61 72 65 0a  fields that are.
36280 2a 2a 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c  ** common to all
36290 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e   module implemen
362a0 74 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 56  tations..**.** V
362b0 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6d 65  irtual tables me
362c0 74 68 6f 64 73 20 63 61 6e 20 73 65 74 20 61 6e  thods can set an
362d0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 62   error message b
362e0 79 20 61 73 73 69 67 6e 69 6e 67 20 61 0a 2a 2a  y assigning a.**
362f0 20 73 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64   string obtained
36300 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d   from [sqlite3_m
36310 70 72 69 6e 74 66 28 29 5d 20 74 6f 20 7a 45 72  printf()] to zEr
36320 72 4d 73 67 2e 20 20 54 68 65 20 6d 65 74 68 6f  rMsg.  The metho
36330 64 20 73 68 6f 75 6c 64 0a 2a 2a 20 74 61 6b 65  d should.** take
36340 20 63 61 72 65 20 74 68 61 74 20 61 6e 79 20 70   care that any p
36350 72 69 6f 72 20 73 74 72 69 6e 67 20 69 73 20 66  rior string is f
36360 72 65 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74  reed by a call t
36370 6f 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28  o [sqlite3_free(
36380 29 5d 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 61  )].** prior to a
36390 73 73 69 67 6e 69 6e 67 20 61 20 6e 65 77 20 73  ssigning a new s
363a0 74 72 69 6e 67 20 74 6f 20 7a 45 72 72 4d 73 67  tring to zErrMsg
363b0 2e 20 20 41 66 74 65 72 20 74 68 65 20 65 72 72  .  After the err
363c0 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 73  or message.** is
363d0 20 64 65 6c 69 76 65 72 65 64 20 75 70 20 74 6f   delivered up to
363e0 20 74 68 65 20 63 6c 69 65 6e 74 20 61 70 70 6c   the client appl
363f0 69 63 61 74 69 6f 6e 2c 20 74 68 65 20 73 74 72  ication, the str
36400 69 6e 67 20 77 69 6c 6c 20 62 65 20 61 75 74 6f  ing will be auto
36410 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 66 72 65  matically.** fre
36420 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 66 72  ed by sqlite3_fr
36430 65 65 28 29 20 61 6e 64 20 74 68 65 20 7a 45 72  ee() and the zEr
36440 72 4d 73 67 20 66 69 65 6c 64 20 77 69 6c 6c 20  rMsg field will 
36450 62 65 20 7a 65 72 6f 65 64 2e 0a 2a 2f 0a 73 74  be zeroed..*/.st
36460 72 75 63 74 20 73 71 6c 69 74 65 33 5f 76 74 61  ruct sqlite3_vta
36470 62 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69  b {.  const sqli
36480 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64  te3_module *pMod
36490 75 6c 65 3b 20 20 2f 2a 20 54 68 65 20 6d 6f 64  ule;  /* The mod
364a0 75 6c 65 20 66 6f 72 20 74 68 69 73 20 76 69 72  ule for this vir
364b0 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20  tual table */.  
364c0 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20  int nRef;       
364d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
364e0 2f 2a 20 4e 4f 20 4c 4f 4e 47 45 52 20 55 53 45  /* NO LONGER USE
364f0 44 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72  D */.  char *zEr
36500 72 4d 73 67 3b 20 20 20 20 20 20 20 20 20 20 20  rMsg;           
36510 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20         /* Error 
36520 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 73 71 6c  message from sql
36530 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 20 2a  ite3_mprintf() *
36540 2f 0a 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74  /.  /* Virtual t
36550 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  able implementat
36560 69 6f 6e 73 20 77 69 6c 6c 20 74 79 70 69 63 61  ions will typica
36570 6c 6c 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e  lly add addition
36580 61 6c 20 66 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a  al fields */.};.
36590 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
365a0 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 43   Virtual Table C
365b0 75 72 73 6f 72 20 4f 62 6a 65 63 74 20 20 7b 48  ursor Object  {H
365c0 31 38 30 32 30 7d 20 3c 53 32 30 34 30 30 3e 0a  18020} <S20400>.
365d0 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c  ** KEYWORDS: sql
365e0 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
365f0 20 7b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20   {virtual table 
36600 63 75 72 73 6f 72 7d 0a 2a 2a 20 45 58 50 45 52  cursor}.** EXPER
36610 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 76  IMENTAL.**.** Ev
36620 65 72 79 20 5b 76 69 72 74 75 61 6c 20 74 61 62  ery [virtual tab
36630 6c 65 20 6d 6f 64 75 6c 65 5d 20 69 6d 70 6c 65  le module] imple
36640 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 61  mentation uses a
36650 20 73 75 62 63 6c 61 73 73 20 6f 66 20 74 68 65   subclass of the
36660 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  .** following st
36670 72 75 63 74 75 72 65 20 74 6f 20 64 65 73 63 72  ructure to descr
36680 69 62 65 20 63 75 72 73 6f 72 73 20 74 68 61 74  ibe cursors that
36690 20 70 6f 69 6e 74 20 69 6e 74 6f 20 74 68 65 0a   point into the.
366a0 2a 2a 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c  ** [virtual tabl
366b0 65 5d 20 61 6e 64 20 61 72 65 20 75 73 65 64 0a  e] and are used.
366c0 2a 2a 20 74 6f 20 6c 6f 6f 70 20 74 68 72 6f 75  ** to loop throu
366d0 67 68 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  gh the virtual t
366e0 61 62 6c 65 2e 20 20 43 75 72 73 6f 72 73 20 61  able.  Cursors a
366f0 72 65 20 63 72 65 61 74 65 64 20 75 73 69 6e 67  re created using
36700 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33   the.** [sqlite3
36710 5f 6d 6f 64 75 6c 65 2e 78 4f 70 65 6e 20 7c 20  _module.xOpen | 
36720 78 4f 70 65 6e 5d 20 6d 65 74 68 6f 64 20 6f 66  xOpen] method of
36730 20 74 68 65 20 6d 6f 64 75 6c 65 20 61 6e 64 20   the module and 
36740 61 72 65 20 64 65 73 74 72 6f 79 65 64 0a 2a 2a  are destroyed.**
36750 20 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33   by the [sqlite3
36760 5f 6d 6f 64 75 6c 65 2e 78 43 6c 6f 73 65 20 7c  _module.xClose |
36770 20 78 43 6c 6f 73 65 5d 20 6d 65 74 68 6f 64 2e   xClose] method.
36780 20 20 43 75 73 73 6f 72 73 20 61 72 65 20 75 73    Cussors are us
36790 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 5b 78 46  ed.** by the [xF
367a0 69 6c 74 65 72 5d 2c 20 5b 78 4e 65 78 74 5d 2c  ilter], [xNext],
367b0 20 5b 78 45 6f 66 5d 2c 20 5b 78 43 6f 6c 75 6d   [xEof], [xColum
367c0 6e 5d 2c 20 61 6e 64 20 5b 78 52 6f 77 69 64 5d  n], and [xRowid]
367d0 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 20 74   methods.** of t
367e0 68 65 20 6d 6f 64 75 6c 65 2e 20 20 45 61 63 68  he module.  Each
367f0 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e   module implemen
36800 74 61 74 69 6f 6e 20 77 69 6c 6c 20 64 65 66 69  tation will defi
36810 6e 65 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e  ne.** the conten
36820 74 20 6f 66 20 61 20 63 75 72 73 6f 72 20 73 74  t of a cursor st
36830 72 75 63 74 75 72 65 20 74 6f 20 73 75 69 74 20  ructure to suit 
36840 69 74 73 20 6f 77 6e 20 6e 65 65 64 73 2e 0a 2a  its own needs..*
36850 2a 0a 2a 2a 20 54 68 69 73 20 73 75 70 65 72 63  *.** This superc
36860 6c 61 73 73 20 65 78 69 73 74 73 20 69 6e 20 6f  lass exists in o
36870 72 64 65 72 20 74 6f 20 64 65 66 69 6e 65 20 66  rder to define f
36880 69 65 6c 64 73 20 6f 66 20 74 68 65 20 63 75 72  ields of the cur
36890 73 6f 72 20 74 68 61 74 0a 2a 2a 20 61 72 65 20  sor that.** are 
368a0 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 69 6d  common to all im
368b0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a  plementations..*
368c0 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  /.struct sqlite3
368d0 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 7b 0a 20  _vtab_cursor {. 
368e0 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
368f0 56 74 61 62 3b 20 20 20 20 20 20 2f 2a 20 56 69  Vtab;      /* Vi
36900 72 74 75 61 6c 20 74 61 62 6c 65 20 6f 66 20 74  rtual table of t
36910 68 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20  his cursor */.  
36920 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65  /* Virtual table
36930 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
36940 20 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79 20   will typically 
36950 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 66  add additional f
36960 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  ields */.};../*.
36970 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 63  ** CAPI3REF: Dec
36980 6c 61 72 65 20 54 68 65 20 53 63 68 65 6d 61 20  lare The Schema 
36990 4f 66 20 41 20 56 69 72 74 75 61 6c 20 54 61 62  Of A Virtual Tab
369a0 6c 65 20 7b 48 31 38 32 38 30 7d 20 3c 53 32 30  le {H18280} <S20
369b0 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45  400>.** EXPERIME
369c0 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b  NTAL.**.** The [
369d0 78 43 72 65 61 74 65 5d 20 61 6e 64 20 5b 78 43  xCreate] and [xC
369e0 6f 6e 6e 65 63 74 5d 20 6d 65 74 68 6f 64 73 20  onnect] methods 
369f0 6f 66 20 61 0a 2a 2a 20 5b 76 69 72 74 75 61 6c  of a.** [virtual
36a00 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 20 63   table module] c
36a10 61 6c 6c 20 74 68 69 73 20 69 6e 74 65 72 66 61  all this interfa
36a20 63 65 0a 2a 2a 20 74 6f 20 64 65 63 6c 61 72 65  ce.** to declare
36a30 20 74 68 65 20 66 6f 72 6d 61 74 20 28 74 68 65   the format (the
36a40 20 6e 61 6d 65 73 20 61 6e 64 20 64 61 74 61 74   names and datat
36a50 79 70 65 73 20 6f 66 20 74 68 65 20 63 6f 6c 75  ypes of the colu
36a60 6d 6e 73 29 20 6f 66 0a 2a 2a 20 74 68 65 20 76  mns) of.** the v
36a70 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 74 68  irtual tables th
36a80 65 79 20 69 6d 70 6c 65 6d 65 6e 74 2e 0a 2a 2f  ey implement..*/
36a90 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49  .SQLITE_API SQLI
36aa0 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20  TE_EXPERIMENTAL 
36ab0 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 65 63 6c  int sqlite3_decl
36ac0 61 72 65 5f 76 74 61 62 28 73 71 6c 69 74 65 33  are_vtab(sqlite3
36ad0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  *, const char *z
36ae0 53 51 4c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  SQL);../*.** CAP
36af0 49 33 52 45 46 3a 20 4f 76 65 72 6c 6f 61 64 20  I3REF: Overload 
36b00 41 20 46 75 6e 63 74 69 6f 6e 20 46 6f 72 20 41  A Function For A
36b10 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 7b   Virtual Table {
36b20 48 31 38 33 30 30 7d 20 3c 53 32 30 34 30 30 3e  H18300} <S20400>
36b30 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c  .** EXPERIMENTAL
36b40 0a 2a 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20 74  .**.** Virtual t
36b50 61 62 6c 65 73 20 63 61 6e 20 70 72 6f 76 69 64  ables can provid
36b60 65 20 61 6c 74 65 72 6e 61 74 69 76 65 20 69 6d  e alternative im
36b70 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66  plementations of
36b80 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 75 73   functions.** us
36b90 69 6e 67 20 74 68 65 20 5b 78 46 69 6e 64 46 75  ing the [xFindFu
36ba0 6e 63 74 69 6f 6e 5d 20 6d 65 74 68 6f 64 20 6f  nction] method o
36bb0 66 20 74 68 65 20 5b 76 69 72 74 75 61 6c 20 74  f the [virtual t
36bc0 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 2e 20 20 0a  able module].  .
36bd0 2a 2a 20 42 75 74 20 67 6c 6f 62 61 6c 20 76 65  ** But global ve
36be0 72 73 69 6f 6e 73 20 6f 66 20 74 68 6f 73 65 20  rsions of those 
36bf0 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 6d 75 73  functions.** mus
36c00 74 20 65 78 69 73 74 20 69 6e 20 6f 72 64 65 72  t exist in order
36c10 20 74 6f 20 62 65 20 6f 76 65 72 6c 6f 61 64 65   to be overloade
36c20 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50  d..**.** This AP
36c30 49 20 6d 61 6b 65 73 20 73 75 72 65 20 61 20 67  I makes sure a g
36c40 6c 6f 62 61 6c 20 76 65 72 73 69 6f 6e 20 6f 66  lobal version of
36c50 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68   a function with
36c60 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a   a particular.**
36c70 20 6e 61 6d 65 20 61 6e 64 20 6e 75 6d 62 65 72   name and number
36c80 20 6f 66 20 70 61 72 61 6d 65 74 65 72 73 20 65   of parameters e
36c90 78 69 73 74 73 2e 20 20 49 66 20 6e 6f 20 73 75  xists.  If no su
36ca0 63 68 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 73  ch function exis
36cb0 74 73 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 69  ts.** before thi
36cc0 73 20 41 50 49 20 69 73 20 63 61 6c 6c 65 64 2c  s API is called,
36cd0 20 61 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 20   a new function 
36ce0 69 73 20 63 72 65 61 74 65 64 2e 20 20 54 68 65  is created.  The
36cf0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a   implementation.
36d00 2a 2a 20 6f 66 20 74 68 65 20 6e 65 77 20 66 75  ** of the new fu
36d10 6e 63 74 69 6f 6e 20 61 6c 77 61 79 73 20 63 61  nction always ca
36d20 75 73 65 73 20 61 6e 20 65 78 63 65 70 74 69 6f  uses an exceptio
36d30 6e 20 74 6f 20 62 65 20 74 68 72 6f 77 6e 2e 20  n to be thrown. 
36d40 20 53 6f 0a 2a 2a 20 74 68 65 20 6e 65 77 20 66   So.** the new f
36d50 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 67  unction is not g
36d60 6f 6f 64 20 66 6f 72 20 61 6e 79 74 68 69 6e 67  ood for anything
36d70 20 62 79 20 69 74 73 65 6c 66 2e 20 20 49 74 73   by itself.  Its
36d80 20 6f 6e 6c 79 0a 2a 2a 20 70 75 72 70 6f 73 65   only.** purpose
36d90 20 69 73 20 74 6f 20 62 65 20 61 20 70 6c 61 63   is to be a plac
36da0 65 68 6f 6c 64 65 72 20 66 75 6e 63 74 69 6f 6e  eholder function
36db0 20 74 68 61 74 20 63 61 6e 20 62 65 20 6f 76 65   that can be ove
36dc0 72 6c 6f 61 64 65 64 0a 2a 2a 20 62 79 20 61 20  rloaded.** by a 
36dd0 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d 2e  [virtual table].
36de0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53  .*/.SQLITE_API S
36df0 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54  QLITE_EXPERIMENT
36e00 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  AL int sqlite3_o
36e10 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e  verload_function
36e20 28 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74  (sqlite3*, const
36e30 20 63 68 61 72 20 2a 7a 46 75 6e 63 4e 61 6d 65   char *zFuncName
36e40 2c 20 69 6e 74 20 6e 41 72 67 29 3b 0a 0a 2f 2a  , int nArg);../*
36e50 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63  .** The interfac
36e60 65 20 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c  e to the virtual
36e70 2d 74 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d  -table mechanism
36e80 20 64 65 66 69 6e 65 64 20 61 62 6f 76 65 20 28   defined above (
36e90 62 61 63 6b 20 75 70 0a 2a 2a 20 74 6f 20 61 20  back up.** to a 
36ea0 63 6f 6d 6d 65 6e 74 20 72 65 6d 61 72 6b 61 62  comment remarkab
36eb0 6c 79 20 73 69 6d 69 6c 61 72 20 74 6f 20 74 68  ly similar to th
36ec0 69 73 20 6f 6e 65 29 20 69 73 20 63 75 72 72 65  is one) is curre
36ed0 6e 74 6c 79 20 63 6f 6e 73 69 64 65 72 65 64 0a  ntly considered.
36ee0 2a 2a 20 74 6f 20 62 65 20 65 78 70 65 72 69 6d  ** to be experim
36ef0 65 6e 74 61 6c 2e 20 20 54 68 65 20 69 6e 74 65  ental.  The inte
36f00 72 66 61 63 65 20 6d 69 67 68 74 20 63 68 61 6e  rface might chan
36f10 67 65 20 69 6e 20 69 6e 63 6f 6d 70 61 74 69 62  ge in incompatib
36f20 6c 65 20 77 61 79 73 2e 0a 2a 2a 20 49 66 20 74  le ways..** If t
36f30 68 69 73 20 69 73 20 61 20 70 72 6f 62 6c 65 6d  his is a problem
36f40 20 66 6f 72 20 79 6f 75 2c 20 64 6f 20 6e 6f 74   for you, do not
36f50 20 75 73 65 20 74 68 65 20 69 6e 74 65 72 66 61   use the interfa
36f60 63 65 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e  ce at this time.
36f70 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20  .**.** When the 
36f80 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 65  virtual-table me
36f90 63 68 61 6e 69 73 6d 20 73 74 61 62 69 6c 69 7a  chanism stabiliz
36fa0 65 73 2c 20 77 65 20 77 69 6c 6c 20 64 65 63 6c  es, we will decl
36fb0 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e 74 65 72  are the.** inter
36fc0 66 61 63 65 20 66 69 78 65 64 2c 20 73 75 70 70  face fixed, supp
36fd0 6f 72 74 20 69 74 20 69 6e 64 65 66 69 6e 69 74  ort it indefinit
36fe0 65 6c 79 2c 20 61 6e 64 20 72 65 6d 6f 76 65 20  ely, and remove 
36ff0 74 68 69 73 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2a  this comment..**
37000 0a 2a 2a 2a 2a 2a 2a 20 45 58 50 45 52 49 4d 45  .****** EXPERIME
37010 4e 54 41 4c 20 2d 20 73 75 62 6a 65 63 74 20 74  NTAL - subject t
37020 6f 20 63 68 61 6e 67 65 20 77 69 74 68 6f 75 74  o change without
37030 20 6e 6f 74 69 63 65 20 2a 2a 2a 2a 2a 2a 2a 2a   notice ********
37040 2a 2a 2a 2a 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  ******.*/../*.**
37050 20 43 41 50 49 33 52 45 46 3a 20 41 20 48 61 6e   CAPI3REF: A Han
37060 64 6c 65 20 54 6f 20 41 6e 20 4f 70 65 6e 20 42  dle To An Open B
37070 4c 4f 42 20 7b 48 31 37 38 30 30 7d 20 3c 53 33  LOB {H17800} <S3
37080 30 32 33 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44  0230>.** KEYWORD
37090 53 3a 20 7b 42 4c 4f 42 20 68 61 6e 64 6c 65 7d  S: {BLOB handle}
370a0 20 7b 42 4c 4f 42 20 68 61 6e 64 6c 65 73 7d 0a   {BLOB handles}.
370b0 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  **.** An instanc
370c0 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74  e of this object
370d0 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20 6f   represents an o
370e0 70 65 6e 20 42 4c 4f 42 20 6f 6e 20 77 68 69 63  pen BLOB on whic
370f0 68 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 6c  h.** [sqlite3_bl
37100 6f 62 5f 6f 70 65 6e 20 7c 20 69 6e 63 72 65 6d  ob_open | increm
37110 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 5d 20  ental BLOB I/O] 
37120 63 61 6e 20 62 65 20 70 65 72 66 6f 72 6d 65 64  can be performed
37130 2e 0a 2a 2a 20 4f 62 6a 65 63 74 73 20 6f 66 20  ..** Objects of 
37140 74 68 69 73 20 74 79 70 65 20 61 72 65 20 63 72  this type are cr
37150 65 61 74 65 64 20 62 79 20 5b 73 71 6c 69 74 65  eated by [sqlite
37160 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 0a 2a  3_blob_open()].*
37170 2a 20 61 6e 64 20 64 65 73 74 72 6f 79 65 64 20  * and destroyed 
37180 62 79 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  by [sqlite3_blob
37190 5f 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 20 54 68  _close()]..** Th
371a0 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  e [sqlite3_blob_
371b0 72 65 61 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c  read()] and [sql
371c0 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28  ite3_blob_write(
371d0 29 5d 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a  )] interfaces.**
371e0 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20   can be used to 
371f0 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 73 6d  read or write sm
37200 61 6c 6c 20 73 75 62 73 65 63 74 69 6f 6e 73 20  all subsections 
37210 6f 66 20 74 68 65 20 42 4c 4f 42 2e 0a 2a 2a 20  of the BLOB..** 
37220 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f  The [sqlite3_blo
37230 62 5f 62 79 74 65 73 28 29 5d 20 69 6e 74 65 72  b_bytes()] inter
37240 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65  face returns the
37250 20 73 69 7a 65 20 6f 66 20 74 68 65 20 42 4c 4f   size of the BLO
37260 42 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a 74  B in bytes..*/.t
37270 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71  ypedef struct sq
37280 6c 69 74 65 33 5f 62 6c 6f 62 20 73 71 6c 69 74  lite3_blob sqlit
37290 65 33 5f 62 6c 6f 62 3b 0a 0a 2f 2a 0a 2a 2a 20  e3_blob;../*.** 
372a0 43 41 50 49 33 52 45 46 3a 20 4f 70 65 6e 20 41  CAPI3REF: Open A
372b0 20 42 4c 4f 42 20 46 6f 72 20 49 6e 63 72 65 6d   BLOB For Increm
372c0 65 6e 74 61 6c 20 49 2f 4f 20 7b 48 31 37 38 31  ental I/O {H1781
372d0 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a  0} <S30230>.**.*
372e0 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65  * This interface
372f0 73 20 6f 70 65 6e 73 20 61 20 5b 42 4c 4f 42 20  s opens a [BLOB 
37300 68 61 6e 64 6c 65 20 7c 20 68 61 6e 64 6c 65 5d  handle | handle]
37310 20 74 6f 20 74 68 65 20 42 4c 4f 42 20 6c 6f 63   to the BLOB loc
37320 61 74 65 64 0a 2a 2a 20 69 6e 20 72 6f 77 20 69  ated.** in row i
37330 52 6f 77 2c 20 63 6f 6c 75 6d 6e 20 7a 43 6f 6c  Row, column zCol
37340 75 6d 6e 2c 20 74 61 62 6c 65 20 7a 54 61 62 6c  umn, table zTabl
37350 65 20 69 6e 20 64 61 74 61 62 61 73 65 20 7a 44  e in database zD
37360 62 3b 0a 2a 2a 20 69 6e 20 6f 74 68 65 72 20 77  b;.** in other w
37370 6f 72 64 73 2c 20 74 68 65 20 73 61 6d 65 20 42  ords, the same B
37380 4c 4f 42 20 74 68 61 74 20 77 6f 75 6c 64 20 62  LOB that would b
37390 65 20 73 65 6c 65 63 74 65 64 20 62 79 3a 0a 2a  e selected by:.*
373a0 2a 0a 2a 2a 20 3c 70 72 65 3e 0a 2a 2a 20 20 20  *.** <pre>.**   
373b0 20 20 53 45 4c 45 43 54 20 7a 43 6f 6c 75 6d 6e    SELECT zColumn
373c0 20 46 52 4f 4d 20 7a 44 62 2e 7a 54 61 62 6c 65   FROM zDb.zTable
373d0 20 57 48 45 52 45 20 5b 72 6f 77 69 64 5d 20 3d   WHERE [rowid] =
373e0 20 69 52 6f 77 3b 0a 2a 2a 20 3c 2f 70 72 65 3e   iRow;.** </pre>
373f0 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 49 66 20   {END}.**.** If 
37400 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65  the flags parame
37410 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c  ter is non-zero,
37420 20 74 68 65 6e 20 74 68 65 20 42 4c 4f 42 20 69   then the BLOB i
37430 73 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61  s opened for rea
37440 64 0a 2a 2a 20 61 6e 64 20 77 72 69 74 65 20 61  d.** and write a
37450 63 63 65 73 73 2e 20 49 66 20 69 74 20 69 73 20  ccess. If it is 
37460 7a 65 72 6f 2c 20 74 68 65 20 42 4c 4f 42 20 69  zero, the BLOB i
37470 73 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61  s opened for rea
37480 64 20 61 63 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20  d access..**.** 
37490 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 64 61  Note that the da
374a0 74 61 62 61 73 65 20 6e 61 6d 65 20 69 73 20 6e  tabase name is n
374b0 6f 74 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20  ot the filename 
374c0 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a  that contains.**
374d0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62 75   the database bu
374e0 74 20 72 61 74 68 65 72 20 74 68 65 20 73 79 6d  t rather the sym
374f0 62 6f 6c 69 63 20 6e 61 6d 65 20 6f 66 20 74 68  bolic name of th
37500 65 20 64 61 74 61 62 61 73 65 20 74 68 61 74 0a  e database that.
37510 2a 2a 20 69 73 20 61 73 73 69 67 6e 65 64 20 77  ** is assigned w
37520 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  hen the database
37530 20 69 73 20 63 6f 6e 6e 65 63 74 65 64 20 75 73   is connected us
37540 69 6e 67 20 5b 41 54 54 41 43 48 5d 2e 0a 2a 2a  ing [ATTACH]..**
37550 20 46 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61   For the main da
37560 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65  tabase file, the
37570 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 69   database name i
37580 73 20 22 6d 61 69 6e 22 2e 0a 2a 2a 20 46 6f 72  s "main"..** For
37590 20 54 45 4d 50 20 74 61 62 6c 65 73 2c 20 74 68   TEMP tables, th
375a0 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  e database name 
375b0 69 73 20 22 74 65 6d 70 22 2e 0a 2a 2a 0a 2a 2a  is "temp"..**.**
375c0 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 5b 53 51   On success, [SQ
375d0 4c 49 54 45 5f 4f 4b 5d 20 69 73 20 72 65 74 75  LITE_OK] is retu
375e0 72 6e 65 64 20 61 6e 64 20 74 68 65 20 6e 65 77  rned and the new
375f0 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 69   [BLOB handle] i
37600 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20  s written.** to 
37610 2a 70 70 42 6c 6f 62 2e 20 4f 74 68 65 72 77 69  *ppBlob. Otherwi
37620 73 65 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64  se an [error cod
37630 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  e] is returned a
37640 6e 64 20 2a 70 70 42 6c 6f 62 20 69 73 20 73 65  nd *ppBlob is se
37650 74 0a 2a 2a 20 74 6f 20 62 65 20 61 20 6e 75 6c  t.** to be a nul
37660 6c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 20 54 68  l pointer..** Th
37670 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 73  is function sets
37680 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63   the [database c
37690 6f 6e 6e 65 63 74 69 6f 6e 5d 20 65 72 72 6f 72  onnection] error
376a0 20 63 6f 64 65 20 61 6e 64 20 6d 65 73 73 61 67   code and messag
376b0 65 0a 2a 2a 20 61 63 63 65 73 73 69 62 6c 65 20  e.** accessible 
376c0 76 69 61 20 5b 73 71 6c 69 74 65 33 5f 65 72 72  via [sqlite3_err
376d0 63 6f 64 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c  code()] and [sql
376e0 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 20 61  ite3_errmsg()] a
376f0 6e 64 20 72 65 6c 61 74 65 64 0a 2a 2a 20 66 75  nd related.** fu
37700 6e 63 74 69 6f 6e 73 2e 20 20 4e 6f 74 65 20 74  nctions.  Note t
37710 68 61 74 20 74 68 65 20 2a 70 70 42 6c 6f 62 20  hat the *ppBlob 
37720 76 61 72 69 61 62 6c 65 20 69 73 20 61 6c 77 61  variable is alwa
37730 79 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 69  ys initialized i
37740 6e 20 61 0a 2a 2a 20 77 61 79 20 74 68 61 74 20  n a.** way that 
37750 6d 61 6b 65 73 20 69 74 20 73 61 66 65 20 74 6f  makes it safe to
37760 20 69 6e 76 6f 6b 65 20 5b 73 71 6c 69 74 65 33   invoke [sqlite3
37770 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 5d 20 6f  _blob_close()] o
37780 6e 20 2a 70 70 42 6c 6f 62 0a 2a 2a 20 72 65 67  n *ppBlob.** reg
37790 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 73  ardless of the s
377a0 75 63 63 65 73 73 20 6f 72 20 66 61 69 6c 75 72  uccess or failur
377b0 65 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e  e of this routin
377c0 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
377d0 72 6f 77 20 74 68 61 74 20 61 20 42 4c 4f 42 20  row that a BLOB 
377e0 68 61 6e 64 6c 65 20 70 6f 69 6e 74 73 20 74 6f  handle points to
377f0 20 69 73 20 6d 6f 64 69 66 69 65 64 20 62 79 20   is modified by 
37800 61 6e 0a 2a 2a 20 5b 55 50 44 41 54 45 5d 2c 20  an.** [UPDATE], 
37810 5b 44 45 4c 45 54 45 5d 2c 20 6f 72 20 62 79 20  [DELETE], or by 
37820 5b 4f 4e 20 43 4f 4e 46 4c 49 43 54 5d 20 73 69  [ON CONFLICT] si
37830 64 65 2d 65 66 66 65 63 74 73 0a 2a 2a 20 74 68  de-effects.** th
37840 65 6e 20 74 68 65 20 42 4c 4f 42 20 68 61 6e 64  en the BLOB hand
37850 6c 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20  le is marked as 
37860 22 65 78 70 69 72 65 64 22 2e 0a 2a 2a 20 54 68  "expired"..** Th
37870 69 73 20 69 73 20 74 72 75 65 20 69 66 20 61 6e  is is true if an
37880 79 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  y column of the 
37890 72 6f 77 20 69 73 20 63 68 61 6e 67 65 64 2c 20  row is changed, 
378a0 65 76 65 6e 20 61 20 63 6f 6c 75 6d 6e 0a 2a 2a  even a column.**
378b0 20 6f 74 68 65 72 20 74 68 61 6e 20 74 68 65 20   other than the 
378c0 6f 6e 65 20 74 68 65 20 42 4c 4f 42 20 68 61 6e  one the BLOB han
378d0 64 6c 65 20 69 73 20 6f 70 65 6e 20 6f 6e 2e 0a  dle is open on..
378e0 2a 2a 20 43 61 6c 6c 73 20 74 6f 20 5b 73 71 6c  ** Calls to [sql
378f0 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 29  ite3_blob_read()
37900 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62  ] and [sqlite3_b
37910 6c 6f 62 5f 77 72 69 74 65 28 29 5d 20 66 6f 72  lob_write()] for
37920 0a 2a 2a 20 61 20 65 78 70 69 72 65 64 20 42 4c  .** a expired BL
37930 4f 42 20 68 61 6e 64 6c 65 20 66 61 69 6c 20 77  OB handle fail w
37940 69 74 68 20 61 6e 20 72 65 74 75 72 6e 20 63 6f  ith an return co
37950 64 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 41 42  de of [SQLITE_AB
37960 4f 52 54 5d 2e 0a 2a 2a 20 43 68 61 6e 67 65 73  ORT]..** Changes
37970 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 61 20   written into a 
37980 42 4c 4f 42 20 70 72 69 6f 72 20 74 6f 20 74 68  BLOB prior to th
37990 65 20 42 4c 4f 42 20 65 78 70 69 72 69 6e 67 20  e BLOB expiring 
379a0 61 72 65 20 6e 6f 74 0a 2a 2a 20 72 6f 6c 6c 62  are not.** rollb
379b0 61 63 6b 20 62 79 20 74 68 65 20 65 78 70 69 72  ack by the expir
379c0 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 42 4c 4f  ation of the BLO
379d0 42 2e 20 20 53 75 63 68 20 63 68 61 6e 67 65 73  B.  Such changes
379e0 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79   will eventually
379f0 0a 2a 2a 20 63 6f 6d 6d 69 74 20 69 66 20 74 68  .** commit if th
37a00 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f  e transaction co
37a10 6e 74 69 6e 75 65 73 20 74 6f 20 63 6f 6d 70 6c  ntinues to compl
37a20 65 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 55 73 65  etion..**.** Use
37a30 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c   the [sqlite3_bl
37a40 6f 62 5f 62 79 74 65 73 28 29 5d 20 69 6e 74 65  ob_bytes()] inte
37a50 72 66 61 63 65 20 74 6f 20 64 65 74 65 72 6d 69  rface to determi
37a60 6e 65 20 74 68 65 20 73 69 7a 65 20 6f 66 0a 2a  ne the size of.*
37a70 2a 20 74 68 65 20 6f 70 65 6e 65 64 20 62 6c 6f  * the opened blo
37a80 62 2e 20 20 54 68 65 20 73 69 7a 65 20 6f 66 20  b.  The size of 
37a90 61 20 62 6c 6f 62 20 6d 61 79 20 6e 6f 74 20 62  a blob may not b
37aa0 65 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 69  e changed by thi
37ab0 73 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e 20  s.** interface. 
37ac0 20 55 73 65 20 74 68 65 20 5b 55 50 44 41 54 45   Use the [UPDATE
37ad0 5d 20 53 51 4c 20 63 6f 6d 6d 61 6e 64 20 74 6f  ] SQL command to
37ae0 20 63 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65   change the size
37af0 20 6f 66 20 61 0a 2a 2a 20 62 6c 6f 62 2e 0a 2a   of a.** blob..*
37b00 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65  *.** The [sqlite
37b10 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28  3_bind_zeroblob(
37b20 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  )] and [sqlite3_
37b30 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28  result_zeroblob(
37b40 29 5d 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a  )] interfaces.**
37b50 20 61 6e 64 20 74 68 65 20 62 75 69 6c 74 2d 69   and the built-i
37b60 6e 20 5b 7a 65 72 6f 62 6c 6f 62 5d 20 53 51 4c  n [zeroblob] SQL
37b70 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 20 62 65   function can be
37b80 20 75 73 65 64 2c 20 69 66 20 64 65 73 69 72 65   used, if desire
37b90 64 2c 0a 2a 2a 20 74 6f 20 63 72 65 61 74 65 20  d,.** to create 
37ba0 61 6e 20 65 6d 70 74 79 2c 20 7a 65 72 6f 2d 66  an empty, zero-f
37bb0 69 6c 6c 65 64 20 62 6c 6f 62 20 69 6e 20 77 68  illed blob in wh
37bc0 69 63 68 20 74 6f 20 72 65 61 64 20 6f 72 20 77  ich to read or w
37bd0 72 69 74 65 20 75 73 69 6e 67 0a 2a 2a 20 74 68  rite using.** th
37be0 69 73 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a  is interface..**
37bf0 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 61 20 72  .** To avoid a r
37c00 65 73 6f 75 72 63 65 20 6c 65 61 6b 2c 20 65 76  esource leak, ev
37c10 65 72 79 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 68  ery open [BLOB h
37c20 61 6e 64 6c 65 5d 20 73 68 6f 75 6c 64 20 65 76  andle] should ev
37c30 65 6e 74 75 61 6c 6c 79 0a 2a 2a 20 62 65 20 72  entually.** be r
37c40 65 6c 65 61 73 65 64 20 62 79 20 61 20 63 61 6c  eleased by a cal
37c50 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c  l to [sqlite3_bl
37c60 6f 62 5f 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 0a  ob_close()]..**.
37c70 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
37c80 0a 2a 2a 20 5b 48 31 37 38 31 33 5d 20 5b 48 31  .** [H17813] [H1
37c90 37 38 31 34 5d 20 5b 48 31 37 38 31 36 5d 20 5b  7814] [H17816] [
37ca0 48 31 37 38 31 39 5d 20 5b 48 31 37 38 32 31 5d  H17819] [H17821]
37cb0 20 5b 48 31 37 38 32 34 5d 0a 2a 2f 0a 53 51 4c   [H17824].*/.SQL
37cc0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
37cd0 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 0a 20  te3_blob_open(. 
37ce0 20 73 71 6c 69 74 65 33 2a 2c 0a 20 20 63 6f 6e   sqlite3*,.  con
37cf0 73 74 20 63 68 61 72 20 2a 7a 44 62 2c 0a 20 20  st char *zDb,.  
37d00 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62  const char *zTab
37d10 6c 65 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  le,.  const char
37d20 20 2a 7a 43 6f 6c 75 6d 6e 2c 0a 20 20 73 71 6c   *zColumn,.  sql
37d30 69 74 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 2c  ite3_int64 iRow,
37d40 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 0a 20 20  .  int flags,.  
37d50 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 2a 70  sqlite3_blob **p
37d60 70 42 6c 6f 62 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  pBlob.);../*.** 
37d70 43 41 50 49 33 52 45 46 3a 20 43 6c 6f 73 65 20  CAPI3REF: Close 
37d80 41 20 42 4c 4f 42 20 48 61 6e 64 6c 65 20 7b 48  A BLOB Handle {H
37d90 31 37 38 33 30 7d 20 3c 53 33 30 32 33 30 3e 0a  17830} <S30230>.
37da0 2a 2a 0a 2a 2a 20 43 6c 6f 73 65 73 20 61 6e 20  **.** Closes an 
37db0 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 61 6e 64 6c  open [BLOB handl
37dc0 65 5d 2e 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 6e  e]..**.** Closin
37dd0 67 20 61 20 42 4c 4f 42 20 73 68 61 6c 6c 20 63  g a BLOB shall c
37de0 61 75 73 65 20 74 68 65 20 63 75 72 72 65 6e 74  ause the current
37df0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20   transaction to 
37e00 63 6f 6d 6d 69 74 0a 2a 2a 20 69 66 20 74 68 65  commit.** if the
37e10 72 65 20 61 72 65 20 6e 6f 20 6f 74 68 65 72 20  re are no other 
37e20 42 4c 4f 42 73 2c 20 6e 6f 20 70 65 6e 64 69 6e  BLOBs, no pendin
37e30 67 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  g prepared state
37e40 6d 65 6e 74 73 2c 20 61 6e 64 20 74 68 65 0a 2a  ments, and the.*
37e50 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * database conne
37e60 63 74 69 6f 6e 20 69 73 20 69 6e 20 5b 61 75 74  ction is in [aut
37e70 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 5d 2e 0a 2a  ocommit mode]..*
37e80 2a 20 49 66 20 61 6e 79 20 77 72 69 74 65 73 20  * If any writes 
37e90 77 65 72 65 20 6d 61 64 65 20 74 6f 20 74 68 65  were made to the
37ea0 20 42 4c 4f 42 2c 20 74 68 65 79 20 6d 69 67 68   BLOB, they migh
37eb0 74 20 62 65 20 68 65 6c 64 20 69 6e 20 63 61 63  t be held in cac
37ec0 68 65 0a 2a 2a 20 75 6e 74 69 6c 20 74 68 65 20  he.** until the 
37ed0 63 6c 6f 73 65 20 6f 70 65 72 61 74 69 6f 6e 20  close operation 
37ee0 69 66 20 74 68 65 79 20 77 69 6c 6c 20 66 69 74  if they will fit
37ef0 2e 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20  ..**.** Closing 
37f00 74 68 65 20 42 4c 4f 42 20 6f 66 74 65 6e 20 66  the BLOB often f
37f10 6f 72 63 65 73 20 74 68 65 20 63 68 61 6e 67 65  orces the change
37f20 73 0a 2a 2a 20 6f 75 74 20 74 6f 20 64 69 73 6b  s.** out to disk
37f30 20 61 6e 64 20 73 6f 20 69 66 20 61 6e 79 20 49   and so if any I
37f40 2f 4f 20 65 72 72 6f 72 73 20 6f 63 63 75 72 2c  /O errors occur,
37f50 20 74 68 65 79 20 77 69 6c 6c 20 6c 69 6b 65 6c   they will likel
37f60 79 20 6f 63 63 75 72 0a 2a 2a 20 61 74 20 74 68  y occur.** at th
37f70 65 20 74 69 6d 65 20 77 68 65 6e 20 74 68 65 20  e time when the 
37f80 42 4c 4f 42 20 69 73 20 63 6c 6f 73 65 64 2e 20  BLOB is closed. 
37f90 20 41 6e 79 20 65 72 72 6f 72 73 20 74 68 61 74   Any errors that
37fa0 20 6f 63 63 75 72 20 64 75 72 69 6e 67 0a 2a 2a   occur during.**
37fb0 20 63 6c 6f 73 69 6e 67 20 61 72 65 20 72 65 70   closing are rep
37fc0 6f 72 74 65 64 20 61 73 20 61 20 6e 6f 6e 2d 7a  orted as a non-z
37fd0 65 72 6f 20 72 65 74 75 72 6e 20 76 61 6c 75 65  ero return value
37fe0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 42 4c 4f 42  ..**.** The BLOB
37ff0 20 69 73 20 63 6c 6f 73 65 64 20 75 6e 63 6f 6e   is closed uncon
38000 64 69 74 69 6f 6e 61 6c 6c 79 2e 20 20 45 76 65  ditionally.  Eve
38010 6e 20 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e  n if this routin
38020 65 20 72 65 74 75 72 6e 73 0a 2a 2a 20 61 6e 20  e returns.** an 
38030 65 72 72 6f 72 20 63 6f 64 65 2c 20 74 68 65 20  error code, the 
38040 42 4c 4f 42 20 69 73 20 73 74 69 6c 6c 20 63 6c  BLOB is still cl
38050 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c  osed..**.** Call
38060 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ing this routine
38070 20 77 69 74 68 20 61 20 6e 75 6c 6c 20 70 6f 69   with a null poi
38080 6e 74 65 72 20 28 77 68 69 63 68 20 61 73 20 77  nter (which as w
38090 6f 75 6c 64 20 62 65 20 72 65 74 75 72 6e 65 64  ould be returned
380a0 0a 2a 2a 20 62 79 20 66 61 69 6c 65 64 20 63 61  .** by failed ca
380b0 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62  ll to [sqlite3_b
380c0 6c 6f 62 5f 6f 70 65 6e 28 29 5d 29 20 69 73 20  lob_open()]) is 
380d0 61 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70  a harmless no-op
380e0 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
380f0 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 38 33 33  ents:.** [H17833
38100 5d 20 5b 48 31 37 38 33 36 5d 20 5b 48 31 37 38  ] [H17836] [H178
38110 33 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  39].*/.SQLITE_AP
38120 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c  I int sqlite3_bl
38130 6f 62 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33  ob_close(sqlite3
38140 5f 62 6c 6f 62 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  _blob *);../*.**
38150 20 43 41 50 49 33 52 45 46 3a 20 52 65 74 75 72   CAPI3REF: Retur
38160 6e 20 54 68 65 20 53 69 7a 65 20 4f 66 20 41 6e  n The Size Of An
38170 20 4f 70 65 6e 20 42 4c 4f 42 20 7b 48 31 37 38   Open BLOB {H178
38180 34 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a  40} <S30230>.**.
38190 2a 2a 20 52 65 74 75 72 6e 73 20 74 68 65 20 73  ** Returns the s
381a0 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20  ize in bytes of 
381b0 74 68 65 20 42 4c 4f 42 20 61 63 63 65 73 73 69  the BLOB accessi
381c0 62 6c 65 20 76 69 61 20 74 68 65 20 0a 2a 2a 20  ble via the .** 
381d0 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6f 70 65  successfully ope
381e0 6e 65 64 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65  ned [BLOB handle
381f0 5d 20 69 6e 20 69 74 73 20 6f 6e 6c 79 20 61 72  ] in its only ar
38200 67 75 6d 65 6e 74 2e 20 20 54 68 65 0a 2a 2a 20  gument.  The.** 
38210 69 6e 63 72 65 6d 65 6e 74 61 6c 20 62 6c 6f 62  incremental blob
38220 20 49 2f 4f 20 72 6f 75 74 69 6e 65 73 20 63 61   I/O routines ca
38230 6e 20 6f 6e 6c 79 20 72 65 61 64 20 6f 72 20 6f  n only read or o
38240 76 65 72 77 72 69 74 69 6e 67 20 65 78 69 73 74  verwriting exist
38250 69 6e 67 0a 2a 2a 20 62 6c 6f 62 20 63 6f 6e 74  ing.** blob cont
38260 65 6e 74 3b 20 74 68 65 79 20 63 61 6e 6e 6f 74  ent; they cannot
38270 20 63 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65   change the size
38280 20 6f 66 20 61 20 62 6c 6f 62 2e 0a 2a 2a 0a 2a   of a blob..**.*
38290 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6f  * This routine o
382a0 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 61 20 5b  nly works on a [
382b0 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 77 68 69  BLOB handle] whi
382c0 63 68 20 68 61 73 20 62 65 65 6e 20 63 72 65 61  ch has been crea
382d0 74 65 64 0a 2a 2a 20 62 79 20 61 20 70 72 69 6f  ted.** by a prio
382e0 72 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c  r successful cal
382f0 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c  l to [sqlite3_bl
38300 6f 62 5f 6f 70 65 6e 28 29 5d 20 61 6e 64 20 77  ob_open()] and w
38310 68 69 63 68 20 68 61 73 20 6e 6f 74 0a 2a 2a 20  hich has not.** 
38320 62 65 65 6e 20 63 6c 6f 73 65 64 20 62 79 20 5b  been closed by [
38330 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f  sqlite3_blob_clo
38340 73 65 28 29 5d 2e 20 20 50 61 73 73 69 6e 67 20  se()].  Passing 
38350 61 6e 79 20 6f 74 68 65 72 20 70 6f 69 6e 74 65  any other pointe
38360 72 20 69 6e 0a 2a 2a 20 74 6f 20 74 68 69 73 20  r in.** to this 
38370 72 6f 75 74 69 6e 65 20 72 65 73 75 6c 74 73 20  routine results 
38380 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64  in undefined and
38390 20 70 72 6f 62 61 62 6c 79 20 75 6e 64 65 73 69   probably undesi
383a0 72 61 62 6c 65 20 62 65 68 61 76 69 6f 72 2e 0a  rable behavior..
383b0 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
383c0 74 73 3a 0a 2a 2a 20 5b 48 31 37 38 34 33 5d 0a  ts:.** [H17843].
383d0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
383e0 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62  t sqlite3_blob_b
383f0 79 74 65 73 28 73 71 6c 69 74 65 33 5f 62 6c 6f  ytes(sqlite3_blo
38400 62 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  b *);../*.** CAP
38410 49 33 52 45 46 3a 20 52 65 61 64 20 44 61 74 61  I3REF: Read Data
38420 20 46 72 6f 6d 20 41 20 42 4c 4f 42 20 49 6e 63   From A BLOB Inc
38430 72 65 6d 65 6e 74 61 6c 6c 79 20 7b 48 31 37 38  rementally {H178
38440 35 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a  50} <S30230>.**.
38450 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
38460 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 61 64   is used to read
38470 20 64 61 74 61 20 66 72 6f 6d 20 61 6e 20 6f 70   data from an op
38480 65 6e 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d  en [BLOB handle]
38490 20 69 6e 74 6f 20 61 0a 2a 2a 20 63 61 6c 6c 65   into a.** calle
384a0 72 2d 73 75 70 70 6c 69 65 64 20 62 75 66 66 65  r-supplied buffe
384b0 72 2e 20 4e 20 62 79 74 65 73 20 6f 66 20 64 61  r. N bytes of da
384c0 74 61 20 61 72 65 20 63 6f 70 69 65 64 20 69 6e  ta are copied in
384d0 74 6f 20 62 75 66 66 65 72 20 5a 0a 2a 2a 20 66  to buffer Z.** f
384e0 72 6f 6d 20 74 68 65 20 6f 70 65 6e 20 42 4c 4f  rom the open BLO
384f0 42 2c 20 73 74 61 72 74 69 6e 67 20 61 74 20 6f  B, starting at o
38500 66 66 73 65 74 20 69 4f 66 66 73 65 74 2e 0a 2a  ffset iOffset..*
38510 2a 0a 2a 2a 20 49 66 20 6f 66 66 73 65 74 20 69  *.** If offset i
38520 4f 66 66 73 65 74 20 69 73 20 6c 65 73 73 20 74  Offset is less t
38530 68 61 6e 20 4e 20 62 79 74 65 73 20 66 72 6f 6d  han N bytes from
38540 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
38550 42 4c 4f 42 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45  BLOB,.** [SQLITE
38560 5f 45 52 52 4f 52 5d 20 69 73 20 72 65 74 75 72  _ERROR] is retur
38570 6e 65 64 20 61 6e 64 20 6e 6f 20 64 61 74 61 20  ned and no data 
38580 69 73 20 72 65 61 64 2e 20 20 49 66 20 4e 20 6f  is read.  If N o
38590 72 20 69 4f 66 66 73 65 74 20 69 73 0a 2a 2a 20  r iOffset is.** 
385a0 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20  less than zero, 
385b0 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69  [SQLITE_ERROR] i
385c0 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e  s returned and n
385d0 6f 20 64 61 74 61 20 69 73 20 72 65 61 64 2e 0a  o data is read..
385e0 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74  ** The size of t
385f0 68 65 20 62 6c 6f 62 20 28 61 6e 64 20 68 65 6e  he blob (and hen
38600 63 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 76  ce the maximum v
38610 61 6c 75 65 20 6f 66 20 4e 2b 69 4f 66 66 73 65  alue of N+iOffse
38620 74 29 0a 2a 2a 20 63 61 6e 20 62 65 20 64 65 74  t).** can be det
38630 65 72 6d 69 6e 65 64 20 75 73 69 6e 67 20 74 68  ermined using th
38640 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  e [sqlite3_blob_
38650 62 79 74 65 73 28 29 5d 20 69 6e 74 65 72 66 61  bytes()] interfa
38660 63 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 74 74  ce..**.** An att
38670 65 6d 70 74 20 74 6f 20 72 65 61 64 20 66 72 6f  empt to read fro
38680 6d 20 61 6e 20 65 78 70 69 72 65 64 20 5b 42 4c  m an expired [BL
38690 4f 42 20 68 61 6e 64 6c 65 5d 20 66 61 69 6c 73  OB handle] fails
386a0 20 77 69 74 68 20 61 6e 0a 2a 2a 20 65 72 72 6f   with an.** erro
386b0 72 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54  r code of [SQLIT
386c0 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2a 0a 2a 2a 20  E_ABORT]..**.** 
386d0 4f 6e 20 73 75 63 63 65 73 73 2c 20 53 51 4c 49  On success, SQLI
386e0 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
386f0 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  d..** Otherwise,
38700 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d   an [error code]
38710 20 6f 72 20 61 6e 20 5b 65 78 74 65 6e 64 65 64   or an [extended
38720 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20   error code] is 
38730 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
38740 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c  This routine onl
38750 79 20 77 6f 72 6b 73 20 6f 6e 20 61 20 5b 42 4c  y works on a [BL
38760 4f 42 20 68 61 6e 64 6c 65 5d 20 77 68 69 63 68  OB handle] which
38770 20 68 61 73 20 62 65 65 6e 20 63 72 65 61 74 65   has been create
38780 64 0a 2a 2a 20 62 79 20 61 20 70 72 69 6f 72 20  d.** by a prior 
38790 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20  successful call 
387a0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  to [sqlite3_blob
387b0 5f 6f 70 65 6e 28 29 5d 20 61 6e 64 20 77 68 69  _open()] and whi
387c0 63 68 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 62 65  ch has not.** be
387d0 65 6e 20 63 6c 6f 73 65 64 20 62 79 20 5b 73 71  en closed by [sq
387e0 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65  lite3_blob_close
387f0 28 29 5d 2e 20 20 50 61 73 73 69 6e 67 20 61 6e  ()].  Passing an
38800 79 20 6f 74 68 65 72 20 70 6f 69 6e 74 65 72 20  y other pointer 
38810 69 6e 0a 2a 2a 20 74 6f 20 74 68 69 73 20 72 6f  in.** to this ro
38820 75 74 69 6e 65 20 72 65 73 75 6c 74 73 20 69 6e  utine results in
38830 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 20 70   undefined and p
38840 72 6f 62 61 62 6c 79 20 75 6e 64 65 73 69 72 61  robably undesira
38850 62 6c 65 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a  ble behavior..**
38860 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73  .** See also: [s
38870 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74  qlite3_blob_writ
38880 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  e()]..**.** Requ
38890 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
388a0 37 38 35 33 5d 20 5b 48 31 37 38 35 36 5d 20 5b  7853] [H17856] [
388b0 48 31 37 38 35 39 5d 20 5b 48 31 37 38 36 32 5d  H17859] [H17862]
388c0 20 5b 48 31 37 38 36 33 5d 20 5b 48 31 37 38 36   [H17863] [H1786
388d0 35 5d 20 5b 48 31 37 38 36 38 5d 0a 2a 2f 0a 53  5] [H17868].*/.S
388e0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
388f0 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28  lite3_blob_read(
38900 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 2c 20  sqlite3_blob *, 
38910 76 6f 69 64 20 2a 5a 2c 20 69 6e 74 20 4e 2c 20  void *Z, int N, 
38920 69 6e 74 20 69 4f 66 66 73 65 74 29 3b 0a 0a 2f  int iOffset);../
38930 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 57  *.** CAPI3REF: W
38940 72 69 74 65 20 44 61 74 61 20 49 6e 74 6f 20 41  rite Data Into A
38950 20 42 4c 4f 42 20 49 6e 63 72 65 6d 65 6e 74 61   BLOB Incrementa
38960 6c 6c 79 20 7b 48 31 37 38 37 30 7d 20 3c 53 33  lly {H17870} <S3
38970 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  0230>.**.** This
38980 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
38990 64 20 74 6f 20 77 72 69 74 65 20 64 61 74 61 20  d to write data 
389a0 69 6e 74 6f 20 61 6e 20 6f 70 65 6e 20 5b 42 4c  into an open [BL
389b0 4f 42 20 68 61 6e 64 6c 65 5d 20 66 72 6f 6d 20  OB handle] from 
389c0 61 0a 2a 2a 20 63 61 6c 6c 65 72 2d 73 75 70 70  a.** caller-supp
389d0 6c 69 65 64 20 62 75 66 66 65 72 2e 20 4e 20 62  lied buffer. N b
389e0 79 74 65 73 20 6f 66 20 64 61 74 61 20 61 72 65  ytes of data are
389f0 20 63 6f 70 69 65 64 20 66 72 6f 6d 20 74 68 65   copied from the
38a00 20 62 75 66 66 65 72 20 5a 0a 2a 2a 20 69 6e 74   buffer Z.** int
38a10 6f 20 74 68 65 20 6f 70 65 6e 20 42 4c 4f 42 2c  o the open BLOB,
38a20 20 73 74 61 72 74 69 6e 67 20 61 74 20 6f 66 66   starting at off
38a30 73 65 74 20 69 4f 66 66 73 65 74 2e 0a 2a 2a 0a  set iOffset..**.
38a40 2a 2a 20 49 66 20 74 68 65 20 5b 42 4c 4f 42 20  ** If the [BLOB 
38a50 68 61 6e 64 6c 65 5d 20 70 61 73 73 65 64 20 61  handle] passed a
38a60 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  s the first argu
38a70 6d 65 6e 74 20 77 61 73 20 6e 6f 74 20 6f 70 65  ment was not ope
38a80 6e 65 64 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69  ned for.** writi
38a90 6e 67 20 28 74 68 65 20 66 6c 61 67 73 20 70 61  ng (the flags pa
38aa0 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69  rameter to [sqli
38ab0 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d  te3_blob_open()]
38ac0 20 77 61 73 20 7a 65 72 6f 29 2c 0a 2a 2a 20 74   was zero),.** t
38ad0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  his function ret
38ae0 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 52 45 41  urns [SQLITE_REA
38af0 44 4f 4e 4c 59 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68  DONLY]..**.** Th
38b00 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20  is function may 
38b10 6f 6e 6c 79 20 6d 6f 64 69 66 79 20 74 68 65 20  only modify the 
38b20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
38b30 42 4c 4f 42 3b 20 69 74 20 69 73 0a 2a 2a 20 6e  BLOB; it is.** n
38b40 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 69  ot possible to i
38b50 6e 63 72 65 61 73 65 20 74 68 65 20 73 69 7a 65  ncrease the size
38b60 20 6f 66 20 61 20 42 4c 4f 42 20 75 73 69 6e 67   of a BLOB using
38b70 20 74 68 69 73 20 41 50 49 2e 0a 2a 2a 20 49 66   this API..** If
38b80 20 6f 66 66 73 65 74 20 69 4f 66 66 73 65 74 20   offset iOffset 
38b90 69 73 20 6c 65 73 73 20 74 68 61 6e 20 4e 20 62  is less than N b
38ba0 79 74 65 73 20 66 72 6f 6d 20 74 68 65 20 65 6e  ytes from the en
38bb0 64 20 6f 66 20 74 68 65 20 42 4c 4f 42 2c 0a 2a  d of the BLOB,.*
38bc0 2a 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d  * [SQLITE_ERROR]
38bd0 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64   is returned and
38be0 20 6e 6f 20 64 61 74 61 20 69 73 20 77 72 69 74   no data is writ
38bf0 74 65 6e 2e 20 20 49 66 20 4e 20 69 73 0a 2a 2a  ten.  If N is.**
38c00 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 20   less than zero 
38c10 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69  [SQLITE_ERROR] i
38c20 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e  s returned and n
38c30 6f 20 64 61 74 61 20 69 73 20 77 72 69 74 74 65  o data is writte
38c40 6e 2e 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f  n..** The size o
38c50 66 20 74 68 65 20 42 4c 4f 42 20 28 61 6e 64 20  f the BLOB (and 
38c60 68 65 6e 63 65 20 74 68 65 20 6d 61 78 69 6d 75  hence the maximu
38c70 6d 20 76 61 6c 75 65 20 6f 66 20 4e 2b 69 4f 66  m value of N+iOf
38c80 66 73 65 74 29 0a 2a 2a 20 63 61 6e 20 62 65 20  fset).** can be 
38c90 64 65 74 65 72 6d 69 6e 65 64 20 75 73 69 6e 67  determined using
38ca0 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c   the [sqlite3_bl
38cb0 6f 62 5f 62 79 74 65 73 28 29 5d 20 69 6e 74 65  ob_bytes()] inte
38cc0 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20  rface..**.** An 
38cd0 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65  attempt to write
38ce0 20 74 6f 20 61 6e 20 65 78 70 69 72 65 64 20 5b   to an expired [
38cf0 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 66 61 69  BLOB handle] fai
38d00 6c 73 20 77 69 74 68 20 61 6e 0a 2a 2a 20 65 72  ls with an.** er
38d10 72 6f 72 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c  ror code of [SQL
38d20 49 54 45 5f 41 42 4f 52 54 5d 2e 20 20 57 72 69  ITE_ABORT].  Wri
38d30 74 65 73 20 74 6f 20 74 68 65 20 42 4c 4f 42 20  tes to the BLOB 
38d40 74 68 61 74 20 6f 63 63 75 72 72 65 64 0a 2a 2a  that occurred.**
38d50 20 62 65 66 6f 72 65 20 74 68 65 20 5b 42 4c 4f   before the [BLO
38d60 42 20 68 61 6e 64 6c 65 5d 20 65 78 70 69 72 65  B handle] expire
38d70 64 20 61 72 65 20 6e 6f 74 20 72 6f 6c 6c 65 64  d are not rolled
38d80 20 62 61 63 6b 20 62 79 20 74 68 65 0a 2a 2a 20   back by the.** 
38d90 65 78 70 69 72 61 74 69 6f 6e 20 6f 66 20 74 68  expiration of th
38da0 65 20 68 61 6e 64 6c 65 2c 20 74 68 6f 75 67 68  e handle, though
38db0 20 6f 66 20 63 6f 75 72 73 65 20 74 68 6f 73 65   of course those
38dc0 20 63 68 61 6e 67 65 73 20 6d 69 67 68 74 0a 2a   changes might.*
38dd0 2a 20 68 61 76 65 20 62 65 65 6e 20 6f 76 65 72  * have been over
38de0 77 72 69 74 74 65 6e 20 62 79 20 74 68 65 20 73  written by the s
38df0 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 65 78  tatement that ex
38e00 70 69 72 65 64 20 74 68 65 20 42 4c 4f 42 20 68  pired the BLOB h
38e10 61 6e 64 6c 65 0a 2a 2a 20 6f 72 20 62 79 20 6f  andle.** or by o
38e20 74 68 65 72 20 69 6e 64 65 70 65 6e 64 65 6e 74  ther independent
38e30 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a   statements..**.
38e40 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 53  ** On success, S
38e50 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
38e60 72 6e 65 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 69  rned..** Otherwi
38e70 73 65 2c 20 61 6e 20 20 5b 65 72 72 6f 72 20 63  se, an  [error c
38e80 6f 64 65 5d 20 6f 72 20 61 6e 20 5b 65 78 74 65  ode] or an [exte
38e90 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 5d  nded error code]
38ea0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
38eb0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
38ec0 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 61   only works on a
38ed0 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 77   [BLOB handle] w
38ee0 68 69 63 68 20 68 61 73 20 62 65 65 6e 20 63 72  hich has been cr
38ef0 65 61 74 65 64 0a 2a 2a 20 62 79 20 61 20 70 72  eated.** by a pr
38f00 69 6f 72 20 73 75 63 63 65 73 73 66 75 6c 20 63  ior successful c
38f10 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  all to [sqlite3_
38f20 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 20 61 6e 64  blob_open()] and
38f30 20 77 68 69 63 68 20 68 61 73 20 6e 6f 74 0a 2a   which has not.*
38f40 2a 20 62 65 65 6e 20 63 6c 6f 73 65 64 20 62 79  * been closed by
38f50 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63   [sqlite3_blob_c
38f60 6c 6f 73 65 28 29 5d 2e 20 20 50 61 73 73 69 6e  lose()].  Passin
38f70 67 20 61 6e 79 20 6f 74 68 65 72 20 70 6f 69 6e  g any other poin
38f80 74 65 72 20 69 6e 0a 2a 2a 20 74 6f 20 74 68 69  ter in.** to thi
38f90 73 20 72 6f 75 74 69 6e 65 20 72 65 73 75 6c 74  s routine result
38fa0 73 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 61  s in undefined a
38fb0 6e 64 20 70 72 6f 62 61 62 6c 79 20 75 6e 64 65  nd probably unde
38fc0 73 69 72 61 62 6c 65 20 62 65 68 61 76 69 6f 72  sirable behavior
38fd0 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
38fe0 3a 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  : [sqlite3_blob_
38ff0 72 65 61 64 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52  read()]..**.** R
39000 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
39010 5b 48 31 37 38 37 33 5d 20 5b 48 31 37 38 37 34  [H17873] [H17874
39020 5d 20 5b 48 31 37 38 37 35 5d 20 5b 48 31 37 38  ] [H17875] [H178
39030 37 36 5d 20 5b 48 31 37 38 37 37 5d 20 5b 48 31  76] [H17877] [H1
39040 37 38 37 39 5d 20 5b 48 31 37 38 38 32 5d 20 5b  7879] [H17882] [
39050 48 31 37 38 38 35 5d 0a 2a 2a 20 5b 48 31 37 38  H17885].** [H178
39060 38 38 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  88].*/.SQLITE_AP
39070 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c  I int sqlite3_bl
39080 6f 62 5f 77 72 69 74 65 28 73 71 6c 69 74 65 33  ob_write(sqlite3
39090 5f 62 6c 6f 62 20 2a 2c 20 63 6f 6e 73 74 20 76  _blob *, const v
390a0 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69  oid *z, int n, i
390b0 6e 74 20 69 4f 66 66 73 65 74 29 3b 0a 0a 2f 2a  nt iOffset);../*
390c0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 56 69  .** CAPI3REF: Vi
390d0 72 74 75 61 6c 20 46 69 6c 65 20 53 79 73 74 65  rtual File Syste
390e0 6d 20 4f 62 6a 65 63 74 73 20 7b 48 31 31 32 30  m Objects {H1120
390f0 30 7d 20 3c 53 32 30 31 30 30 3e 0a 2a 2a 0a 2a  0} <S20100>.**.*
39100 2a 20 41 20 76 69 72 74 75 61 6c 20 66 69 6c 65  * A virtual file
39110 73 79 73 74 65 6d 20 28 56 46 53 29 20 69 73 20  system (VFS) is 
39120 61 6e 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d  an [sqlite3_vfs]
39130 20 6f 62 6a 65 63 74 0a 2a 2a 20 74 68 61 74 20   object.** that 
39140 53 51 4c 69 74 65 20 75 73 65 73 20 74 6f 20 69  SQLite uses to i
39150 6e 74 65 72 61 63 74 0a 2a 2a 20 77 69 74 68 20  nteract.** with 
39160 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f  the underlying o
39170 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e  perating system.
39180 20 20 4d 6f 73 74 20 53 51 4c 69 74 65 20 62 75    Most SQLite bu
39190 69 6c 64 73 20 63 6f 6d 65 20 77 69 74 68 20 61  ilds come with a
391a0 0a 2a 2a 20 73 69 6e 67 6c 65 20 64 65 66 61 75  .** single defau
391b0 6c 74 20 56 46 53 20 74 68 61 74 20 69 73 20 61  lt VFS that is a
391c0 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 74  ppropriate for t
391d0 68 65 20 68 6f 73 74 20 63 6f 6d 70 75 74 65 72  he host computer
391e0 2e 0a 2a 2a 20 4e 65 77 20 56 46 53 65 73 20 63  ..** New VFSes c
391f0 61 6e 20 62 65 20 72 65 67 69 73 74 65 72 65 64  an be registered
39200 20 61 6e 64 20 65 78 69 73 74 69 6e 67 20 56 46   and existing VF
39210 53 65 73 20 63 61 6e 20 62 65 20 75 6e 72 65 67  Ses can be unreg
39220 69 73 74 65 72 65 64 2e 0a 2a 2a 20 54 68 65 20  istered..** The 
39230 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 72 66  following interf
39240 61 63 65 73 20 61 72 65 20 70 72 6f 76 69 64 65  aces are provide
39250 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  d..**.** The sql
39260 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 29 20  ite3_vfs_find() 
39270 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e  interface return
39280 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
39290 20 56 46 53 20 67 69 76 65 6e 20 69 74 73 20 6e   VFS given its n
392a0 61 6d 65 2e 0a 2a 2a 20 4e 61 6d 65 73 20 61 72  ame..** Names ar
392b0 65 20 63 61 73 65 20 73 65 6e 73 69 74 69 76 65  e case sensitive
392c0 2e 0a 2a 2a 20 4e 61 6d 65 73 20 61 72 65 20 7a  ..** Names are z
392d0 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55  ero-terminated U
392e0 54 46 2d 38 20 73 74 72 69 6e 67 73 2e 0a 2a 2a  TF-8 strings..**
392f0 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   If there is no 
39300 6d 61 74 63 68 2c 20 61 20 4e 55 4c 4c 20 70 6f  match, a NULL po
39310 69 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65  inter is returne
39320 64 2e 0a 2a 2a 20 49 66 20 7a 56 66 73 4e 61 6d  d..** If zVfsNam
39330 65 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74  e is NULL then t
39340 68 65 20 64 65 66 61 75 6c 74 20 56 46 53 20 69  he default VFS i
39350 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
39360 2a 20 4e 65 77 20 56 46 53 65 73 20 61 72 65 20  * New VFSes are 
39370 72 65 67 69 73 74 65 72 65 64 20 77 69 74 68 20  registered with 
39380 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69  sqlite3_vfs_regi
39390 73 74 65 72 28 29 2e 0a 2a 2a 20 45 61 63 68 20  ster()..** Each 
393a0 6e 65 77 20 56 46 53 20 62 65 63 6f 6d 65 73 20  new VFS becomes 
393b0 74 68 65 20 64 65 66 61 75 6c 74 20 56 46 53 20  the default VFS 
393c0 69 66 20 74 68 65 20 6d 61 6b 65 44 66 6c 74 20  if the makeDflt 
393d0 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20  flag is set..** 
393e0 54 68 65 20 73 61 6d 65 20 56 46 53 20 63 61 6e  The same VFS can
393f0 20 62 65 20 72 65 67 69 73 74 65 72 65 64 20 6d   be registered m
39400 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 77 69  ultiple times wi
39410 74 68 6f 75 74 20 69 6e 6a 75 72 79 2e 0a 2a 2a  thout injury..**
39420 20 54 6f 20 6d 61 6b 65 20 61 6e 20 65 78 69 73   To make an exis
39430 74 69 6e 67 20 56 46 53 20 69 6e 74 6f 20 74 68  ting VFS into th
39440 65 20 64 65 66 61 75 6c 74 20 56 46 53 2c 20 72  e default VFS, r
39450 65 67 69 73 74 65 72 20 69 74 20 61 67 61 69 6e  egister it again
39460 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 6d 61 6b  .** with the mak
39470 65 44 66 6c 74 20 66 6c 61 67 20 73 65 74 2e 20  eDflt flag set. 
39480 20 49 66 20 74 77 6f 20 64 69 66 66 65 72 65 6e   If two differen
39490 74 20 56 46 53 65 73 20 77 69 74 68 20 74 68 65  t VFSes with the
394a0 0a 2a 2a 20 73 61 6d 65 20 6e 61 6d 65 20 61 72  .** same name ar
394b0 65 20 72 65 67 69 73 74 65 72 65 64 2c 20 74 68  e registered, th
394c0 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e  e behavior is un
394d0 64 65 66 69 6e 65 64 2e 20 20 49 66 20 61 0a 2a  defined.  If a.*
394e0 2a 20 56 46 53 20 69 73 20 72 65 67 69 73 74 65  * VFS is registe
394f0 72 65 64 20 77 69 74 68 20 61 20 6e 61 6d 65 20  red with a name 
39500 74 68 61 74 20 69 73 20 4e 55 4c 4c 20 6f 72 20  that is NULL or 
39510 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 2c  an empty string,
39520 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 62 65 68  .** then the beh
39530 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e  avior is undefin
39540 65 64 2e 0a 2a 2a 0a 2a 2a 20 55 6e 72 65 67 69  ed..**.** Unregi
39550 73 74 65 72 20 61 20 56 46 53 20 77 69 74 68 20  ster a VFS with 
39560 74 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 5f  the sqlite3_vfs_
39570 75 6e 72 65 67 69 73 74 65 72 28 29 20 69 6e 74  unregister() int
39580 65 72 66 61 63 65 2e 0a 2a 2a 20 49 66 20 74 68  erface..** If th
39590 65 20 64 65 66 61 75 6c 74 20 56 46 53 20 69 73  e default VFS is
395a0 20 75 6e 72 65 67 69 73 74 65 72 65 64 2c 20 61   unregistered, a
395b0 6e 6f 74 68 65 72 20 56 46 53 20 69 73 20 63 68  nother VFS is ch
395c0 6f 73 65 6e 20 61 73 0a 2a 2a 20 74 68 65 20 64  osen as.** the d
395d0 65 66 61 75 6c 74 2e 20 20 54 68 65 20 63 68 6f  efault.  The cho
395e0 69 63 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20  ice for the new 
395f0 56 46 53 20 69 73 20 61 72 62 69 74 72 61 72 79  VFS is arbitrary
39600 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
39610 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 31 32 30 33  ents:.** [H11203
39620 5d 20 5b 48 31 31 32 30 36 5d 20 5b 48 31 31 32  ] [H11206] [H112
39630 30 39 5d 20 5b 48 31 31 32 31 32 5d 20 5b 48 31  09] [H11212] [H1
39640 31 32 31 35 5d 20 5b 48 31 31 32 31 38 5d 0a 2a  1215] [H11218].*
39650 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c  /.SQLITE_API sql
39660 69 74 65 33 5f 76 66 73 20 2a 73 71 6c 69 74 65  ite3_vfs *sqlite
39670 33 5f 76 66 73 5f 66 69 6e 64 28 63 6f 6e 73 74  3_vfs_find(const
39680 20 63 68 61 72 20 2a 7a 56 66 73 4e 61 6d 65 29   char *zVfsName)
39690 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
396a0 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67   sqlite3_vfs_reg
396b0 69 73 74 65 72 28 73 71 6c 69 74 65 33 5f 76 66  ister(sqlite3_vf
396c0 73 2a 2c 20 69 6e 74 20 6d 61 6b 65 44 66 6c 74  s*, int makeDflt
396d0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
396e0 74 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e  t sqlite3_vfs_un
396f0 72 65 67 69 73 74 65 72 28 73 71 6c 69 74 65 33  register(sqlite3
39700 5f 76 66 73 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  _vfs*);../*.** C
39710 41 50 49 33 52 45 46 3a 20 4d 75 74 65 78 65 73  API3REF: Mutexes
39720 20 7b 48 31 37 30 30 30 7d 20 3c 53 32 30 30 30   {H17000} <S2000
39730 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c  0>.**.** The SQL
39740 69 74 65 20 63 6f 72 65 20 75 73 65 73 20 74 68  ite core uses th
39750 65 73 65 20 72 6f 75 74 69 6e 65 73 20 66 6f 72  ese routines for
39760 20 74 68 72 65 61 64 0a 2a 2a 20 73 79 6e 63 68   thread.** synch
39770 72 6f 6e 69 7a 61 74 69 6f 6e 2e 20 54 68 6f 75  ronization. Thou
39780 67 68 20 74 68 65 79 20 61 72 65 20 69 6e 74 65  gh they are inte
39790 6e 64 65 64 20 66 6f 72 20 69 6e 74 65 72 6e 61  nded for interna
397a0 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69  l.** use by SQLi
397b0 74 65 2c 20 63 6f 64 65 20 74 68 61 74 20 6c 69  te, code that li
397c0 6e 6b 73 20 61 67 61 69 6e 73 74 20 53 51 4c 69  nks against SQLi
397d0 74 65 20 69 73 0a 2a 2a 20 70 65 72 6d 69 74 74  te is.** permitt
397e0 65 64 20 74 6f 20 75 73 65 20 61 6e 79 20 6f 66  ed to use any of
397f0 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 2e   these routines.
39800 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74  .**.** The SQLit
39810 65 20 73 6f 75 72 63 65 20 63 6f 64 65 20 63 6f  e source code co
39820 6e 74 61 69 6e 73 20 6d 75 6c 74 69 70 6c 65 20  ntains multiple 
39830 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a  implementations.
39840 2a 2a 20 6f 66 20 74 68 65 73 65 20 6d 75 74 65  ** of these mute
39850 78 20 72 6f 75 74 69 6e 65 73 2e 20 20 41 6e 20  x routines.  An 
39860 61 70 70 72 6f 70 72 69 61 74 65 20 69 6d 70 6c  appropriate impl
39870 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 69 73  ementation.** is
39880 20 73 65 6c 65 63 74 65 64 20 61 75 74 6f 6d 61   selected automa
39890 74 69 63 61 6c 6c 79 20 61 74 20 63 6f 6d 70 69  tically at compi
398a0 6c 65 2d 74 69 6d 65 2e 20 20 54 68 65 20 66 6f  le-time.  The fo
398b0 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 69 6d 70 6c 65  llowing.** imple
398c0 6d 65 6e 74 61 74 69 6f 6e 73 20 61 72 65 20 61  mentations are a
398d0 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20  vailable in the 
398e0 53 51 4c 69 74 65 20 63 6f 72 65 3a 0a 2a 2a 0a  SQLite core:.**.
398f0 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20  ** <ul>.** <li> 
39900 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f    SQLITE_MUTEX_O
39910 53 32 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 4c  S2.** <li>   SQL
39920 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41  ITE_MUTEX_PTHREA
39930 44 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49  D.** <li>   SQLI
39940 54 45 5f 4d 55 54 45 58 5f 57 33 32 0a 2a 2a 20  TE_MUTEX_W32.** 
39950 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55  <li>   SQLITE_MU
39960 54 45 58 5f 4e 4f 4f 50 0a 2a 2a 20 3c 2f 75 6c  TEX_NOOP.** </ul
39970 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49  >.**.** The SQLI
39980 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 20 69 6d  TE_MUTEX_NOOP im
39990 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20  plementation is 
399a0 61 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65  a set of routine
399b0 73 0a 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 6e  s.** that does n
399c0 6f 20 72 65 61 6c 20 6c 6f 63 6b 69 6e 67 20 61  o real locking a
399d0 6e 64 20 69 73 20 61 70 70 72 6f 70 72 69 61 74  nd is appropriat
399e0 65 20 66 6f 72 20 75 73 65 20 69 6e 0a 2a 2a 20  e for use in.** 
399f0 61 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65  a single-threade
39a00 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20  d application.  
39a10 54 68 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  The SQLITE_MUTEX
39a20 5f 4f 53 32 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f  _OS2,.** SQLITE_
39a30 4d 55 54 45 58 5f 50 54 48 52 45 41 44 2c 20 61  MUTEX_PTHREAD, a
39a40 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  nd SQLITE_MUTEX_
39a50 57 33 32 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  W32 implementati
39a60 6f 6e 73 0a 2a 2a 20 61 72 65 20 61 70 70 72 6f  ons.** are appro
39a70 70 72 69 61 74 65 20 66 6f 72 20 75 73 65 20 6f  priate for use o
39a80 6e 20 4f 53 2f 32 2c 20 55 6e 69 78 2c 20 61 6e  n OS/2, Unix, an
39a90 64 20 57 69 6e 64 6f 77 73 2e 0a 2a 2a 0a 2a 2a  d Windows..**.**
39aa0 20 49 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f   If SQLite is co
39ab0 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 20  mpiled with the 
39ac0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 41 50 50  SQLITE_MUTEX_APP
39ad0 44 45 46 20 70 72 65 70 72 6f 63 65 73 73 6f 72  DEF preprocessor
39ae0 0a 2a 2a 20 6d 61 63 72 6f 20 64 65 66 69 6e 65  .** macro define
39af0 64 20 28 77 69 74 68 20 22 2d 44 53 51 4c 49 54  d (with "-DSQLIT
39b00 45 5f 4d 55 54 45 58 5f 41 50 50 44 45 46 3d 31  E_MUTEX_APPDEF=1
39b10 22 29 2c 20 74 68 65 6e 20 6e 6f 20 6d 75 74 65  "), then no mute
39b20 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74  x.** implementat
39b30 69 6f 6e 20 69 73 20 69 6e 63 6c 75 64 65 64 20  ion is included 
39b40 77 69 74 68 20 74 68 65 20 6c 69 62 72 61 72 79  with the library
39b50 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74  . In this case t
39b60 68 65 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f  he.** applicatio
39b70 6e 20 6d 75 73 74 20 73 75 70 70 6c 79 20 61 20  n must supply a 
39b80 63 75 73 74 6f 6d 20 6d 75 74 65 78 20 69 6d 70  custom mutex imp
39b90 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 69 6e  lementation usin
39ba0 67 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45  g the.** [SQLITE
39bb0 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 5d 20 6f  _CONFIG_MUTEX] o
39bc0 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c  ption of the sql
39bd0 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 66 75  ite3_config() fu
39be0 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 66 6f 72 65  nction.** before
39bf0 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33   calling sqlite3
39c00 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 6f 72  _initialize() or
39c10 20 61 6e 79 20 6f 74 68 65 72 20 70 75 62 6c 69   any other publi
39c20 63 20 73 71 6c 69 74 65 33 5f 0a 2a 2a 20 66 75  c sqlite3_.** fu
39c30 6e 63 74 69 6f 6e 20 74 68 61 74 20 63 61 6c 6c  nction that call
39c40 73 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  s sqlite3_initia
39c50 6c 69 7a 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 7b 48  lize()..**.** {H
39c60 31 37 30 31 31 7d 20 54 68 65 20 73 71 6c 69 74  17011} The sqlit
39c70 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29  e3_mutex_alloc()
39c80 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74   routine allocat
39c90 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65  es a new.** mute
39ca0 78 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20  x and returns a 
39cb0 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 7b  pointer to it. {
39cc0 48 31 37 30 31 32 7d 20 49 66 20 69 74 20 72 65  H17012} If it re
39cd0 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68  turns NULL.** th
39ce0 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20  at means that a 
39cf0 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20  mutex could not 
39d00 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 7b 48  be allocated. {H
39d10 31 37 30 31 33 7d 20 53 51 4c 69 74 65 0a 2a 2a  17013} SQLite.**
39d20 20 77 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73   will unwind its
39d30 20 73 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72   stack and retur
39d40 6e 20 61 6e 20 65 72 72 6f 72 2e 20 7b 48 31 37  n an error. {H17
39d50 30 31 34 7d 20 54 68 65 20 61 72 67 75 6d 65 6e  014} The argumen
39d60 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f  t.** to sqlite3_
39d70 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73  mutex_alloc() is
39d80 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 69 6e   one of these in
39d90 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a  teger constants:
39da0 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c  .**.** <ul>.** <
39db0 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  li>  SQLITE_MUTE
39dc0 58 5f 46 41 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20  X_FAST.** <li>  
39dd0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
39de0 55 52 53 49 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20  URSIVE.** <li>  
39df0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
39e00 54 49 43 5f 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c  TIC_MASTER.** <l
39e10 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  i>  SQLITE_MUTEX
39e20 5f 53 54 41 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c  _STATIC_MEM.** <
39e30 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  li>  SQLITE_MUTE
39e40 58 5f 53 54 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a  X_STATIC_MEM2.**
39e50 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55   <li>  SQLITE_MU
39e60 54 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 0a  TEX_STATIC_PRNG.
39e70 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f  ** <li>  SQLITE_
39e80 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55  MUTEX_STATIC_LRU
39e90 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45  .** <li>  SQLITE
39ea0 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52  _MUTEX_STATIC_LR
39eb0 55 32 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a  U2.** </ul>.**.*
39ec0 2a 20 7b 48 31 37 30 31 35 7d 20 54 68 65 20 66  * {H17015} The f
39ed0 69 72 73 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e  irst two constan
39ee0 74 73 20 63 61 75 73 65 20 73 71 6c 69 74 65 33  ts cause sqlite3
39ef0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74  _mutex_alloc() t
39f00 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65  o create.** a ne
39f10 77 20 6d 75 74 65 78 2e 20 20 54 68 65 20 6e 65  w mutex.  The ne
39f20 77 20 6d 75 74 65 78 20 69 73 20 72 65 63 75 72  w mutex is recur
39f30 73 69 76 65 20 77 68 65 6e 20 53 51 4c 49 54 45  sive when SQLITE
39f40 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
39f50 0a 2a 2a 20 69 73 20 75 73 65 64 20 62 75 74 20  .** is used but 
39f60 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20  not necessarily 
39f70 73 6f 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d  so when SQLITE_M
39f80 55 54 45 58 5f 46 41 53 54 20 69 73 20 75 73 65  UTEX_FAST is use
39f90 64 2e 20 7b 45 4e 44 7d 0a 2a 2a 20 54 68 65 20  d. {END}.** The 
39fa0 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61  mutex implementa
39fb0 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65  tion does not ne
39fc0 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 64 69 73  ed to make a dis
39fd0 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77  tinction.** betw
39fe0 65 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  een SQLITE_MUTEX
39ff0 5f 52 45 43 55 52 53 49 56 45 20 61 6e 64 20 53  _RECURSIVE and S
3a000 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54  QLITE_MUTEX_FAST
3a010 20 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e   if it does.** n
3a020 6f 74 20 77 61 6e 74 20 74 6f 2e 20 20 7b 48 31  ot want to.  {H1
3a030 37 30 31 36 7d 20 42 75 74 20 53 51 4c 69 74 65  7016} But SQLite
3a040 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65   will only reque
3a050 73 74 20 61 20 72 65 63 75 72 73 69 76 65 20 6d  st a recursive m
3a060 75 74 65 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73  utex in.** cases
3a070 20 77 68 65 72 65 20 69 74 20 72 65 61 6c 6c 79   where it really
3a080 20 6e 65 65 64 73 20 6f 6e 65 2e 20 20 7b 45 4e   needs one.  {EN
3a090 44 7d 20 49 66 20 61 20 66 61 73 74 65 72 20 6e  D} If a faster n
3a0a0 6f 6e 2d 72 65 63 75 72 73 69 76 65 20 6d 75 74  on-recursive mut
3a0b0 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61  ex.** implementa
3a0c0 74 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c  tion is availabl
3a0d0 65 20 6f 6e 20 74 68 65 20 68 6f 73 74 20 70 6c  e on the host pl
3a0e0 61 74 66 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65  atform, the mute
3a0f0 78 20 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d  x subsystem.** m
3a100 69 67 68 74 20 72 65 74 75 72 6e 20 73 75 63 68  ight return such
3a110 20 61 20 6d 75 74 65 78 20 69 6e 20 72 65 73 70   a mutex in resp
3a120 6f 6e 73 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d  onse to SQLITE_M
3a130 55 54 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a  UTEX_FAST..**.**
3a140 20 7b 48 31 37 30 31 37 7d 20 54 68 65 20 6f 74   {H17017} The ot
3a150 68 65 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 61  her allowed para
3a160 6d 65 74 65 72 73 20 74 6f 20 73 71 6c 69 74 65  meters to sqlite
3a170 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20  3_mutex_alloc() 
3a180 65 61 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61  each return.** a
3a190 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74   pointer to a st
3a1a0 61 74 69 63 20 70 72 65 65 78 69 73 74 69 6e 67  atic preexisting
3a1b0 20 6d 75 74 65 78 2e 20 7b 45 4e 44 7d 20 20 53   mutex. {END}  S
3a1c0 69 78 20 73 74 61 74 69 63 20 6d 75 74 65 78 65  ix static mutexe
3a1d0 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79  s are.** used by
3a1e0 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 65 72   the current ver
3a1f0 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20  sion of SQLite. 
3a200 20 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73   Future versions
3a210 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61   of SQLite.** ma
3a220 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c  y add additional
3a230 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e   static mutexes.
3a240 20 20 53 74 61 74 69 63 20 6d 75 74 65 78 65 73    Static mutexes
3a250 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61   are for interna
3a260 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69  l.** use by SQLi
3a270 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63  te only.  Applic
3a280 61 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20  ations that use 
3a290 53 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73  SQLite mutexes s
3a2a0 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c  hould.** use onl
3a2b0 79 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75  y the dynamic mu
3a2c0 74 65 78 65 73 20 72 65 74 75 72 6e 65 64 20 62  texes returned b
3a2d0 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  y SQLITE_MUTEX_F
3a2e0 41 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45  AST or.** SQLITE
3a2f0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
3a300 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31 38 7d  ..**.** {H17018}
3a310 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 6f 6e   Note that if on
3a320 65 20 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63  e of the dynamic
3a330 20 6d 75 74 65 78 20 70 61 72 61 6d 65 74 65 72   mutex parameter
3a340 73 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  s (SQLITE_MUTEX_
3a350 46 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54  FAST.** or SQLIT
3a360 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
3a370 45 29 20 69 73 20 75 73 65 64 20 74 68 65 6e 20  E) is used then 
3a380 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
3a390 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73  loc().** returns
3a3a0 20 61 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74   a different mut
3a3b0 65 78 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c  ex on every call
3a3c0 2e 20 20 7b 48 31 37 30 33 34 7d 20 42 75 74 20  .  {H17034} But 
3a3d0 66 6f 72 20 74 68 65 20 73 74 61 74 69 63 0a 2a  for the static.*
3a3e0 2a 20 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74  * mutex types, t
3a3f0 68 65 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73  he same mutex is
3a400 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65   returned on eve
3a410 72 79 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73  ry call that has
3a420 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70  .** the same typ
3a430 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20  e number..**.** 
3a440 7b 48 31 37 30 31 39 7d 20 54 68 65 20 73 71 6c  {H17019} The sql
3a450 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28  ite3_mutex_free(
3a460 29 20 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f  ) routine deallo
3a470 63 61 74 65 73 20 61 20 70 72 65 76 69 6f 75 73  cates a previous
3a480 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20  ly.** allocated 
3a490 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 2e 20 7b  dynamic mutex. {
3a4a0 48 31 37 30 32 30 7d 20 53 51 4c 69 74 65 20 69  H17020} SQLite i
3a4b0 73 20 63 61 72 65 66 75 6c 20 74 6f 20 64 65 61  s careful to dea
3a4c0 6c 6c 6f 63 61 74 65 20 65 76 65 72 79 0a 2a 2a  llocate every.**
3a4d0 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 74   dynamic mutex t
3a4e0 68 61 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73  hat it allocates
3a4f0 2e 20 7b 41 31 37 30 32 31 7d 20 54 68 65 20 64  . {A17021} The d
3a500 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20 6d  ynamic mutexes m
3a510 75 73 74 20 6e 6f 74 20 62 65 20 69 6e 0a 2a 2a  ust not be in.**
3a520 20 75 73 65 20 77 68 65 6e 20 74 68 65 79 20 61   use when they a
3a530 72 65 20 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20  re deallocated. 
3a540 7b 41 31 37 30 32 32 7d 20 41 74 74 65 6d 70 74  {A17022} Attempt
3a550 69 6e 67 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74  ing to deallocat
3a560 65 20 61 20 73 74 61 74 69 63 0a 2a 2a 20 6d 75  e a static.** mu
3a570 74 65 78 20 72 65 73 75 6c 74 73 20 69 6e 20 75  tex results in u
3a580 6e 64 65 66 69 6e 65 64 20 62 65 68 61 76 69 6f  ndefined behavio
3a590 72 2e 20 7b 48 31 37 30 32 33 7d 20 53 51 4c 69  r. {H17023} SQLi
3a5a0 74 65 20 6e 65 76 65 72 20 64 65 61 6c 6c 6f 63  te never dealloc
3a5b0 61 74 65 73 0a 2a 2a 20 61 20 73 74 61 74 69 63  ates.** a static
3a5c0 20 6d 75 74 65 78 2e 20 7b 45 4e 44 7d 0a 2a 2a   mutex. {END}.**
3a5d0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
3a5e0 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e  mutex_enter() an
3a5f0 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  d sqlite3_mutex_
3a600 74 72 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61  try() routines a
3a610 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74  ttempt.** to ent
3a620 65 72 20 61 20 6d 75 74 65 78 2e 20 7b 48 31 37  er a mutex. {H17
3a630 30 32 34 7d 20 49 66 20 61 6e 6f 74 68 65 72 20  024} If another 
3a640 74 68 72 65 61 64 20 69 73 20 61 6c 72 65 61 64  thread is alread
3a650 79 20 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74  y within the mut
3a660 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d  ex,.** sqlite3_m
3a670 75 74 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c  utex_enter() wil
3a680 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69  l block and sqli
3a690 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20  te3_mutex_try() 
3a6a0 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53  will return.** S
3a6b0 51 4c 49 54 45 5f 42 55 53 59 2e 20 7b 48 31 37  QLITE_BUSY. {H17
3a6c0 30 32 35 7d 20 20 54 68 65 20 73 71 6c 69 74 65  025}  The sqlite
3a6d0 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e  3_mutex_try() in
3a6e0 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20  terface returns 
3a6f0 5b 53 51 4c 49 54 45 5f 4f 4b 5d 0a 2a 2a 20 75  [SQLITE_OK].** u
3a700 70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c 20 65  pon successful e
3a710 6e 74 72 79 2e 20 20 7b 48 31 37 30 32 36 7d 20  ntry.  {H17026} 
3a720 4d 75 74 65 78 65 73 20 63 72 65 61 74 65 64 20  Mutexes created 
3a730 75 73 69 6e 67 0a 2a 2a 20 53 51 4c 49 54 45 5f  using.** SQLITE_
3a740 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20  MUTEX_RECURSIVE 
3a750 63 61 6e 20 62 65 20 65 6e 74 65 72 65 64 20 6d  can be entered m
3a760 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79  ultiple times by
3a770 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64   the same thread
3a780 2e 0a 2a 2a 20 7b 48 31 37 30 32 37 7d 20 49 6e  ..** {H17027} In
3a790 20 73 75 63 68 20 63 61 73 65 73 20 74 68 65 2c   such cases the,
3a7a0 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62  .** mutex must b
3a7b0 65 20 65 78 69 74 65 64 20 61 6e 20 65 71 75 61  e exited an equa
3a7c0 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65  l number of time
3a7d0 73 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72  s before another
3a7e0 20 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65   thread.** can e
3a7f0 6e 74 65 72 2e 20 20 7b 41 31 37 30 32 38 7d 20  nter.  {A17028} 
3a800 49 66 20 74 68 65 20 73 61 6d 65 20 74 68 72 65  If the same thre
3a810 61 64 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65  ad tries to ente
3a820 72 20 61 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 6b  r any other.** k
3a830 69 6e 64 20 6f 66 20 6d 75 74 65 78 20 6d 6f 72  ind of mutex mor
3a840 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65  e than once, the
3a850 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64   behavior is und
3a860 65 66 69 6e 65 64 2e 0a 2a 2a 20 7b 48 31 37 30  efined..** {H170
3a870 32 39 7d 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  29} SQLite will 
3a880 6e 65 76 65 72 20 65 78 68 69 62 69 74 0a 2a 2a  never exhibit.**
3a890 20 73 75 63 68 20 62 65 68 61 76 69 6f 72 20 69   such behavior i
3a8a0 6e 20 69 74 73 20 6f 77 6e 20 75 73 65 20 6f 66  n its own use of
3a8b0 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20   mutexes..**.** 
3a8c0 53 6f 6d 65 20 73 79 73 74 65 6d 73 20 28 66 6f  Some systems (fo
3a8d0 72 20 65 78 61 6d 70 6c 65 2c 20 57 69 6e 64 6f  r example, Windo
3a8e0 77 73 20 39 35 29 20 64 6f 20 6e 6f 74 20 73 75  ws 95) do not su
3a8f0 70 70 6f 72 74 20 74 68 65 20 6f 70 65 72 61 74  pport the operat
3a900 69 6f 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  ion.** implement
3a910 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 75  ed by sqlite3_mu
3a920 74 65 78 5f 74 72 79 28 29 2e 20 20 4f 6e 20 74  tex_try().  On t
3a930 68 6f 73 65 20 73 79 73 74 65 6d 73 2c 20 73 71  hose systems, sq
3a940 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
3a950 29 0a 2a 2a 20 77 69 6c 6c 20 61 6c 77 61 79 73  ).** will always
3a960 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42   return SQLITE_B
3a970 55 53 59 2e 20 20 7b 48 31 37 30 33 30 7d 20 54  USY.  {H17030} T
3a980 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 6f  he SQLite core o
3a990 6e 6c 79 20 65 76 65 72 20 75 73 65 73 0a 2a 2a  nly ever uses.**
3a9a0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74   sqlite3_mutex_t
3a9b0 72 79 28 29 20 61 73 20 61 6e 20 6f 70 74 69 6d  ry() as an optim
3a9c0 69 7a 61 74 69 6f 6e 20 73 6f 20 74 68 69 73 20  ization so this 
3a9d0 69 73 20 61 63 63 65 70 74 61 62 6c 65 20 62 65  is acceptable be
3a9e0 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48  havior..**.** {H
3a9f0 31 37 30 33 31 7d 20 54 68 65 20 73 71 6c 69 74  17031} The sqlit
3aa00 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29  e3_mutex_leave()
3aa10 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61   routine exits a
3aa20 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 0a   mutex that was.
3aa30 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e  ** previously en
3aa40 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d  tered by the sam
3aa50 65 20 74 68 72 65 61 64 2e 20 20 7b 41 31 37 30  e thread.  {A170
3aa60 33 32 7d 20 54 68 65 20 62 65 68 61 76 69 6f 72  32} The behavior
3aa70 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64  .** is undefined
3aa80 20 69 66 20 74 68 65 20 6d 75 74 65 78 20 69 73   if the mutex is
3aa90 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65   not currently e
3aaa0 6e 74 65 72 65 64 20 62 79 20 74 68 65 0a 2a 2a  ntered by the.**
3aab0 20 63 61 6c 6c 69 6e 67 20 74 68 72 65 61 64 20   calling thread 
3aac0 6f 72 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e  or is not curren
3aad0 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20  tly allocated.  
3aae0 7b 48 31 37 30 33 33 7d 20 53 51 4c 69 74 65 20  {H17033} SQLite 
3aaf0 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65 72 20 64 6f  will.** never do
3ab00 20 65 69 74 68 65 72 2e 20 7b 45 4e 44 7d 0a 2a   either. {END}.*
3ab10 2a 0a 2a 2a 20 49 66 20 74 68 65 20 61 72 67 75  *.** If the argu
3ab20 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f  ment to sqlite3_
3ab30 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 2c 20 73  mutex_enter(), s
3ab40 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
3ab50 28 29 2c 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65  (), or.** sqlite
3ab60 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20  3_mutex_leave() 
3ab70 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  is a NULL pointe
3ab80 72 2c 20 74 68 65 6e 20 61 6c 6c 20 74 68 72 65  r, then all thre
3ab90 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 62 65  e routines.** be
3aba0 68 61 76 65 20 61 73 20 6e 6f 2d 6f 70 73 2e 0a  have as no-ops..
3abb0 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  **.** See also: 
3abc0 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68  [sqlite3_mutex_h
3abd0 65 6c 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69  eld()] and [sqli
3abe0 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c  te3_mutex_nothel
3abf0 64 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  d()]..*/.SQLITE_
3ac00 41 50 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  API sqlite3_mute
3ac10 78 20 2a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  x *sqlite3_mutex
3ac20 5f 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c  _alloc(int);.SQL
3ac30 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
3ac40 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28  ite3_mutex_free(
3ac50 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b  sqlite3_mutex*);
3ac60 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
3ac70 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
3ac80 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74  nter(sqlite3_mut
3ac90 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  ex*);.SQLITE_API
3aca0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74   int sqlite3_mut
3acb0 65 78 5f 74 72 79 28 73 71 6c 69 74 65 33 5f 6d  ex_try(sqlite3_m
3acc0 75 74 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 41  utex*);.SQLITE_A
3acd0 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
3ace0 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69  mutex_leave(sqli
3acf0 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a  te3_mutex*);../*
3ad00 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75  .** CAPI3REF: Mu
3ad10 74 65 78 20 4d 65 74 68 6f 64 73 20 4f 62 6a 65  tex Methods Obje
3ad20 63 74 20 7b 48 31 37 31 32 30 7d 20 3c 53 32 30  ct {H17120} <S20
3ad30 31 33 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45  130>.** EXPERIME
3ad40 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e  NTAL.**.** An in
3ad50 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73  stance of this s
3ad60 74 72 75 63 74 75 72 65 20 64 65 66 69 6e 65 73  tructure defines
3ad70 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 72   the low-level r
3ad80 6f 75 74 69 6e 65 73 0a 2a 2a 20 75 73 65 64 20  outines.** used 
3ad90 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20  to allocate and 
3ada0 75 73 65 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a  use mutexes..**.
3adb0 2a 2a 20 55 73 75 61 6c 6c 79 2c 20 74 68 65 20  ** Usually, the 
3adc0 64 65 66 61 75 6c 74 20 6d 75 74 65 78 20 69 6d  default mutex im
3add0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 70 72  plementations pr
3ade0 6f 76 69 64 65 64 20 62 79 20 53 51 4c 69 74 65  ovided by SQLite
3adf0 20 61 72 65 0a 2a 2a 20 73 75 66 66 69 63 69 65   are.** sufficie
3ae00 6e 74 2c 20 68 6f 77 65 76 65 72 20 74 68 65 20  nt, however the 
3ae10 75 73 65 72 20 68 61 73 20 74 68 65 20 6f 70 74  user has the opt
3ae20 69 6f 6e 20 6f 66 20 73 75 62 73 74 69 74 75 74  ion of substitut
3ae30 69 6e 67 20 61 20 63 75 73 74 6f 6d 0a 2a 2a 20  ing a custom.** 
3ae40 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66  implementation f
3ae50 6f 72 20 73 70 65 63 69 61 6c 69 7a 65 64 20 64  or specialized d
3ae60 65 70 6c 6f 79 6d 65 6e 74 73 20 6f 72 20 73 79  eployments or sy
3ae70 73 74 65 6d 73 20 66 6f 72 20 77 68 69 63 68 20  stems for which 
3ae80 53 51 4c 69 74 65 0a 2a 2a 20 64 6f 65 73 20 6e  SQLite.** does n
3ae90 6f 74 20 70 72 6f 76 69 64 65 20 61 20 73 75 69  ot provide a sui
3aea0 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61  table implementa
3aeb0 74 69 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63 61  tion. In this ca
3aec0 73 65 2c 20 74 68 65 20 75 73 65 72 0a 2a 2a 20  se, the user.** 
3aed0 63 72 65 61 74 65 73 20 61 6e 64 20 70 6f 70 75  creates and popu
3aee0 6c 61 74 65 73 20 61 6e 20 69 6e 73 74 61 6e 63  lates an instanc
3aef0 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  e of this struct
3af00 75 72 65 20 74 6f 20 70 61 73 73 0a 2a 2a 20 74  ure to pass.** t
3af10 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67  o sqlite3_config
3af20 28 29 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68  () along with th
3af30 65 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  e [SQLITE_CONFIG
3af40 5f 4d 55 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a  _MUTEX] option..
3af50 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c  ** Additionally,
3af60 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
3af70 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 63  this structure c
3af80 61 6e 20 62 65 20 75 73 65 64 20 61 73 20 61 6e  an be used as an
3af90 0a 2a 2a 20 6f 75 74 70 75 74 20 76 61 72 69 61  .** output varia
3afa0 62 6c 65 20 77 68 65 6e 20 71 75 65 72 79 69 6e  ble when queryin
3afb0 67 20 74 68 65 20 73 79 73 74 65 6d 20 66 6f 72  g the system for
3afc0 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 75 74   the current mut
3afd0 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61  ex.** implementa
3afe0 74 69 6f 6e 2c 20 75 73 69 6e 67 20 74 68 65 20  tion, using the 
3aff0 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47  [SQLITE_CONFIG_G
3b000 45 54 4d 55 54 45 58 5d 20 6f 70 74 69 6f 6e 2e  ETMUTEX] option.
3b010 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 74 65  .**.** The xMute
3b020 78 49 6e 69 74 20 6d 65 74 68 6f 64 20 64 65 66  xInit method def
3b030 69 6e 65 64 20 62 79 20 74 68 69 73 20 73 74 72  ined by this str
3b040 75 63 74 75 72 65 20 69 73 20 69 6e 76 6f 6b 65  ucture is invoke
3b050 64 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20  d as.** part of 
3b060 73 79 73 74 65 6d 20 69 6e 69 74 69 61 6c 69 7a  system initializ
3b070 61 74 69 6f 6e 20 62 79 20 74 68 65 20 73 71 6c  ation by the sql
3b080 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
3b090 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 7b  ) function..** {
3b0a0 48 31 37 30 30 31 7d 20 54 68 65 20 78 4d 75 74  H17001} The xMut
3b0b0 65 78 49 6e 69 74 20 72 6f 75 74 69 6e 65 20 73  exInit routine s
3b0c0 68 61 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 62  hall be called b
3b0d0 79 20 53 51 4c 69 74 65 20 6f 6e 63 65 20 66 6f  y SQLite once fo
3b0e0 72 20 65 61 63 68 0a 2a 2a 20 65 66 66 65 63 74  r each.** effect
3b0f0 69 76 65 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c  ive call to [sql
3b100 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
3b110 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d  )]..**.** The xM
3b120 75 74 65 78 45 6e 64 20 6d 65 74 68 6f 64 20 64  utexEnd method d
3b130 65 66 69 6e 65 64 20 62 79 20 74 68 69 73 20 73  efined by this s
3b140 74 72 75 63 74 75 72 65 20 69 73 20 69 6e 76 6f  tructure is invo
3b150 6b 65 64 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f  ked as.** part o
3b160 66 20 73 79 73 74 65 6d 20 73 68 75 74 64 6f 77  f system shutdow
3b170 6e 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33  n by the sqlite3
3b180 5f 73 68 75 74 64 6f 77 6e 28 29 20 66 75 6e 63  _shutdown() func
3b190 74 69 6f 6e 2e 20 54 68 65 0a 2a 2a 20 69 6d 70  tion. The.** imp
3b1a0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
3b1b0 68 69 73 20 6d 65 74 68 6f 64 20 69 73 20 65 78  his method is ex
3b1c0 70 65 63 74 65 64 20 74 6f 20 72 65 6c 65 61 73  pected to releas
3b1d0 65 20 61 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e  e all outstandin
3b1e0 67 0a 2a 2a 20 72 65 73 6f 75 72 63 65 73 20 6f  g.** resources o
3b1f0 62 74 61 69 6e 65 64 20 62 79 20 74 68 65 20 6d  btained by the m
3b200 75 74 65 78 20 6d 65 74 68 6f 64 73 20 69 6d 70  utex methods imp
3b210 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 65 73 70  lementation, esp
3b220 65 63 69 61 6c 6c 79 0a 2a 2a 20 74 68 6f 73 65  ecially.** those
3b230 20 6f 62 74 61 69 6e 65 64 20 62 79 20 74 68 65   obtained by the
3b240 20 78 4d 75 74 65 78 49 6e 69 74 20 6d 65 74 68   xMutexInit meth
3b250 6f 64 2e 20 7b 48 31 37 30 30 33 7d 20 54 68 65  od. {H17003} The
3b260 20 78 4d 75 74 65 78 45 6e 64 28 29 0a 2a 2a 20   xMutexEnd().** 
3b270 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20  interface shall 
3b280 62 65 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20  be invoked once 
3b290 66 6f 72 20 65 61 63 68 20 63 61 6c 6c 20 74 6f  for each call to
3b2a0 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f   [sqlite3_shutdo
3b2b0 77 6e 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  wn()]..**.** The
3b2c0 20 72 65 6d 61 69 6e 69 6e 67 20 73 65 76 65 6e   remaining seven
3b2d0 20 6d 65 74 68 6f 64 73 20 64 65 66 69 6e 65 64   methods defined
3b2e0 20 62 79 20 74 68 69 73 20 73 74 72 75 63 74 75   by this structu
3b2f0 72 65 20 28 78 4d 75 74 65 78 41 6c 6c 6f 63 2c  re (xMutexAlloc,
3b300 0a 2a 2a 20 78 4d 75 74 65 78 46 72 65 65 2c 20  .** xMutexFree, 
3b310 78 4d 75 74 65 78 45 6e 74 65 72 2c 20 78 4d 75  xMutexEnter, xMu
3b320 74 65 78 54 72 79 2c 20 78 4d 75 74 65 78 4c 65  texTry, xMutexLe
3b330 61 76 65 2c 20 78 4d 75 74 65 78 48 65 6c 64 20  ave, xMutexHeld 
3b340 61 6e 64 0a 2a 2a 20 78 4d 75 74 65 78 4e 6f 74  and.** xMutexNot
3b350 68 65 6c 64 29 20 69 6d 70 6c 65 6d 65 6e 74 20  held) implement 
3b360 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e  the following in
3b370 74 65 72 66 61 63 65 73 20 28 72 65 73 70 65 63  terfaces (respec
3b380 74 69 76 65 6c 79 29 3a 0a 2a 2a 0a 2a 2a 20 3c  tively):.**.** <
3b390 75 6c 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b  ul>.**   <li>  [
3b3a0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
3b3b0 6c 6f 63 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20  loc()] </li>.** 
3b3c0 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33    <li>  [sqlite3
3b3d0 5f 6d 75 74 65 78 5f 66 72 65 65 28 29 5d 20 3c  _mutex_free()] <
3b3e0 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20  /li>.**   <li>  
3b3f0 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65  [sqlite3_mutex_e
3b400 6e 74 65 72 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a  nter()] </li>.**
3b410 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65     <li>  [sqlite
3b420 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 5d 20 3c  3_mutex_try()] <
3b430 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20  /li>.**   <li>  
3b440 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c  [sqlite3_mutex_l
3b450 65 61 76 65 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a  eave()] </li>.**
3b460 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65     <li>  [sqlite
3b470 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 5d 20  3_mutex_held()] 
3b480 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20  </li>.**   <li> 
3b490 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f   [sqlite3_mutex_
3b4a0 6e 6f 74 68 65 6c 64 28 29 5d 20 3c 2f 6c 69 3e  notheld()] </li>
3b4b0 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20  .** </ul>.**.** 
3b4c0 54 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 65  The only differe
3b4d0 6e 63 65 20 69 73 20 74 68 61 74 20 74 68 65 20  nce is that the 
3b4e0 70 75 62 6c 69 63 20 73 71 6c 69 74 65 33 5f 58  public sqlite3_X
3b4f0 58 58 20 66 75 6e 63 74 69 6f 6e 73 20 65 6e 75  XX functions enu
3b500 6d 65 72 61 74 65 64 0a 2a 2a 20 61 62 6f 76 65  merated.** above
3b510 20 73 69 6c 65 6e 74 6c 79 20 69 67 6e 6f 72 65   silently ignore
3b520 20 61 6e 79 20 69 6e 76 6f 63 61 74 69 6f 6e 73   any invocations
3b530 20 74 68 61 74 20 70 61 73 73 20 61 20 4e 55 4c   that pass a NUL
3b540 4c 20 70 6f 69 6e 74 65 72 20 69 6e 73 74 65 61  L pointer instea
3b550 64 0a 2a 2a 20 6f 66 20 61 20 76 61 6c 69 64 20  d.** of a valid 
3b560 6d 75 74 65 78 20 68 61 6e 64 6c 65 2e 20 54 68  mutex handle. Th
3b570 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
3b580 73 20 6f 66 20 74 68 65 20 6d 65 74 68 6f 64 73  s of the methods
3b590 20 64 65 66 69 6e 65 64 0a 2a 2a 20 62 79 20 74   defined.** by t
3b5a0 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61 72  his structure ar
3b5b0 65 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74  e not required t
3b5c0 6f 20 68 61 6e 64 6c 65 20 74 68 69 73 20 63 61  o handle this ca
3b5d0 73 65 2c 20 74 68 65 20 72 65 73 75 6c 74 73 0a  se, the results.
3b5e0 2a 2a 20 6f 66 20 70 61 73 73 69 6e 67 20 61 20  ** of passing a 
3b5f0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 6e 73  NULL pointer ins
3b600 74 65 61 64 20 6f 66 20 61 20 76 61 6c 69 64 20  tead of a valid 
3b610 6d 75 74 65 78 20 68 61 6e 64 6c 65 20 61 72 65  mutex handle are
3b620 20 75 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 28 69   undefined.** (i
3b630 2e 65 2e 20 69 74 20 69 73 20 61 63 63 65 70 74  .e. it is accept
3b640 61 62 6c 65 20 74 6f 20 70 72 6f 76 69 64 65 20  able to provide 
3b650 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  an implementatio
3b660 6e 20 74 68 61 74 20 73 65 67 66 61 75 6c 74 73  n that segfaults
3b670 20 69 66 0a 2a 2a 20 69 74 20 69 73 20 70 61 73   if.** it is pas
3b680 73 65 64 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  sed a NULL point
3b690 65 72 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78  er)..**.** The x
3b6a0 4d 75 74 65 78 49 6e 69 74 28 29 20 6d 65 74 68  MutexInit() meth
3b6b0 6f 64 20 6d 75 73 74 20 62 65 20 74 68 72 65 61  od must be threa
3b6c0 64 73 61 66 65 2e 20 20 49 74 20 6d 75 73 74 20  dsafe.  It must 
3b6d0 62 65 20 68 61 72 6d 6c 65 73 73 20 74 6f 0a 2a  be harmless to.*
3b6e0 2a 20 69 6e 76 6f 6b 65 20 78 4d 75 74 65 78 49  * invoke xMutexI
3b6f0 6e 69 74 28 29 20 6d 75 74 69 70 6c 65 20 74 69  nit() mutiple ti
3b700 6d 65 73 20 77 69 74 68 69 6e 20 74 68 65 20 73  mes within the s
3b710 61 6d 65 20 70 72 6f 63 65 73 73 20 61 6e 64 20  ame process and 
3b720 77 69 74 68 6f 75 74 0a 2a 2a 20 69 6e 74 65 72  without.** inter
3b730 76 65 6e 69 6e 67 20 63 61 6c 6c 73 20 74 6f 20  vening calls to 
3b740 78 4d 75 74 65 78 45 6e 64 28 29 2e 20 20 53 65  xMutexEnd().  Se
3b750 63 6f 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75  cond and subsequ
3b760 65 6e 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20  ent calls to.** 
3b770 78 4d 75 74 65 78 49 6e 69 74 28 29 20 6d 75 73  xMutexInit() mus
3b780 74 20 62 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a  t be no-ops..**.
3b790 2a 2a 20 78 4d 75 74 65 78 49 6e 69 74 28 29 20  ** xMutexInit() 
3b7a0 6d 75 73 74 20 6e 6f 74 20 75 73 65 20 53 51 4c  must not use SQL
3b7b0 69 74 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  ite memory alloc
3b7c0 61 74 69 6f 6e 20 28 5b 73 71 6c 69 74 65 33 5f  ation ([sqlite3_
3b7d0 6d 61 6c 6c 6f 63 28 29 5d 0a 2a 2a 20 61 6e 64  malloc()].** and
3b7e0 20 69 74 73 20 61 73 73 6f 63 69 61 74 65 73 29   its associates)
3b7f0 2e 20 20 53 69 6d 69 6c 61 72 6c 79 2c 20 78 4d  .  Similarly, xM
3b800 75 74 65 78 41 6c 6c 6f 63 28 29 20 6d 75 73 74  utexAlloc() must
3b810 20 6e 6f 74 20 75 73 65 20 53 51 4c 69 74 65 20   not use SQLite 
3b820 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61  memory.** alloca
3b830 74 69 6f 6e 20 66 6f 72 20 61 20 73 74 61 74 69  tion for a stati
3b840 63 20 6d 75 74 65 78 2e 20 20 48 6f 77 65 76 65  c mutex.  Howeve
3b850 72 20 78 4d 75 74 65 78 41 6c 6c 6f 63 28 29 20  r xMutexAlloc() 
3b860 6d 61 79 20 75 73 65 20 53 51 4c 69 74 65 0a 2a  may use SQLite.*
3b870 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  * memory allocat
3b880 69 6f 6e 20 66 6f 72 20 61 20 66 61 73 74 20 6f  ion for a fast o
3b890 72 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  r recursive mute
3b8a0 78 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  x..**.** SQLite 
3b8b0 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20  will invoke the 
3b8c0 78 4d 75 74 65 78 45 6e 64 28 29 20 6d 65 74 68  xMutexEnd() meth
3b8d0 6f 64 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 33  od when [sqlite3
3b8e0 5f 73 68 75 74 64 6f 77 6e 28 29 5d 20 69 73 0a  _shutdown()] is.
3b8f0 2a 2a 20 63 61 6c 6c 65 64 2c 20 62 75 74 20 6f  ** called, but o
3b900 6e 6c 79 20 69 66 20 74 68 65 20 70 72 69 6f 72  nly if the prior
3b910 20 63 61 6c 6c 20 74 6f 20 78 4d 75 74 65 78 49   call to xMutexI
3b920 6e 69 74 20 72 65 74 75 72 6e 65 64 20 53 51 4c  nit returned SQL
3b930 49 54 45 5f 4f 4b 2e 0a 2a 2a 20 49 66 20 78 4d  ITE_OK..** If xM
3b940 75 74 65 78 49 6e 69 74 20 66 61 69 6c 73 20 69  utexInit fails i
3b950 6e 20 61 6e 79 20 77 61 79 2c 20 69 74 20 69 73  n any way, it is
3b960 20 65 78 70 65 63 74 65 64 20 74 6f 20 63 6c 65   expected to cle
3b970 61 6e 20 75 70 20 61 66 74 65 72 20 69 74 73 65  an up after itse
3b980 6c 66 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 72  lf.** prior to r
3b990 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 74 79 70  eturning..*/.typ
3b9a0 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69  edef struct sqli
3b9b0 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64  te3_mutex_method
3b9c0 73 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  s sqlite3_mutex_
3b9d0 6d 65 74 68 6f 64 73 3b 0a 73 74 72 75 63 74 20  methods;.struct 
3b9e0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
3b9f0 74 68 6f 64 73 20 7b 0a 20 20 69 6e 74 20 28 2a  thods {.  int (*
3ba00 78 4d 75 74 65 78 49 6e 69 74 29 28 76 6f 69 64  xMutexInit)(void
3ba10 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65  );.  int (*xMute
3ba20 78 45 6e 64 29 28 76 6f 69 64 29 3b 0a 20 20 73  xEnd)(void);.  s
3ba30 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 28 2a  qlite3_mutex *(*
3ba40 78 4d 75 74 65 78 41 6c 6c 6f 63 29 28 69 6e 74  xMutexAlloc)(int
3ba50 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 4d 75 74  );.  void (*xMut
3ba60 65 78 46 72 65 65 29 28 73 71 6c 69 74 65 33 5f  exFree)(sqlite3_
3ba70 6d 75 74 65 78 20 2a 29 3b 0a 20 20 76 6f 69 64  mutex *);.  void
3ba80 20 28 2a 78 4d 75 74 65 78 45 6e 74 65 72 29 28   (*xMutexEnter)(
3ba90 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 29  sqlite3_mutex *)
3baa0 3b 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78  ;.  int (*xMutex
3bab0 54 72 79 29 28 73 71 6c 69 74 65 33 5f 6d 75 74  Try)(sqlite3_mut
3bac0 65 78 20 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a  ex *);.  void (*
3bad0 78 4d 75 74 65 78 4c 65 61 76 65 29 28 73 71 6c  xMutexLeave)(sql
3bae0 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20  ite3_mutex *);. 
3baf0 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 48 65 6c   int (*xMutexHel
3bb00 64 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  d)(sqlite3_mutex
3bb10 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4d 75   *);.  int (*xMu
3bb20 74 65 78 4e 6f 74 68 65 6c 64 29 28 73 71 6c 69  texNotheld)(sqli
3bb30 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 7d 3b  te3_mutex *);.};
3bb40 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
3bb50 3a 20 4d 75 74 65 78 20 56 65 72 69 66 69 63 61  : Mutex Verifica
3bb60 74 69 6f 6e 20 52 6f 75 74 69 6e 65 73 20 7b 48  tion Routines {H
3bb70 31 37 30 38 30 7d 20 3c 53 32 30 31 33 30 3e 20  17080} <S20130> 
3bb80 3c 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S30800>.**.** T
3bb90 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
3bba0 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69  _held() and sqli
3bbb0 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c  te3_mutex_nothel
3bbc0 64 28 29 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20  d() routines.** 
3bbd0 61 72 65 20 69 6e 74 65 6e 64 65 64 20 66 6f 72  are intended for
3bbe0 20 75 73 65 20 69 6e 73 69 64 65 20 61 73 73 65   use inside asse
3bbf0 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e  rt() statements.
3bc00 20 7b 48 31 37 30 38 31 7d 20 54 68 65 20 53 51   {H17081} The SQ
3bc10 4c 69 74 65 20 63 6f 72 65 0a 2a 2a 20 6e 65 76  Lite core.** nev
3bc20 65 72 20 75 73 65 73 20 74 68 65 73 65 20 72 6f  er uses these ro
3bc30 75 74 69 6e 65 73 20 65 78 63 65 70 74 20 69 6e  utines except in
3bc40 73 69 64 65 20 61 6e 20 61 73 73 65 72 74 28 29  side an assert()
3bc50 20 61 6e 64 20 61 70 70 6c 69 63 61 74 69 6f 6e   and application
3bc60 73 0a 2a 2a 20 61 72 65 20 61 64 76 69 73 65 64  s.** are advised
3bc70 20 74 6f 20 66 6f 6c 6c 6f 77 20 74 68 65 20 6c   to follow the l
3bc80 65 61 64 20 6f 66 20 74 68 65 20 63 6f 72 65 2e  ead of the core.
3bc90 20 20 7b 48 31 37 30 38 32 7d 20 54 68 65 20 63    {H17082} The c
3bca0 6f 72 65 20 6f 6e 6c 79 0a 2a 2a 20 70 72 6f 76  ore only.** prov
3bcb0 69 64 65 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74  ides implementat
3bcc0 69 6f 6e 73 20 66 6f 72 20 74 68 65 73 65 20 72  ions for these r
3bcd0 6f 75 74 69 6e 65 73 20 77 68 65 6e 20 69 74 20  outines when it 
3bce0 69 73 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 77  is compiled.** w
3bcf0 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 44  ith the SQLITE_D
3bd00 45 42 55 47 20 66 6c 61 67 2e 20 20 7b 41 31 37  EBUG flag.  {A17
3bd10 30 38 37 7d 20 45 78 74 65 72 6e 61 6c 20 6d 75  087} External mu
3bd20 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  tex implementati
3bd30 6f 6e 73 0a 2a 2a 20 61 72 65 20 6f 6e 6c 79 20  ons.** are only 
3bd40 72 65 71 75 69 72 65 64 20 74 6f 20 70 72 6f 76  required to prov
3bd50 69 64 65 20 74 68 65 73 65 20 72 6f 75 74 69 6e  ide these routin
3bd60 65 73 20 69 66 20 53 51 4c 49 54 45 5f 44 45 42  es if SQLITE_DEB
3bd70 55 47 20 69 73 0a 2a 2a 20 64 65 66 69 6e 65 64  UG is.** defined
3bd80 20 61 6e 64 20 69 66 20 4e 44 45 42 55 47 20 69   and if NDEBUG i
3bd90 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 2e 0a 2a  s not defined..*
3bda0 2a 0a 2a 2a 20 7b 48 31 37 30 38 33 7d 20 54 68  *.** {H17083} Th
3bdb0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 73 68 6f  ese routines sho
3bdc0 75 6c 64 20 72 65 74 75 72 6e 20 74 72 75 65 20  uld return true 
3bdd0 69 66 20 74 68 65 20 6d 75 74 65 78 20 69 6e 20  if the mutex in 
3bde0 74 68 65 69 72 20 61 72 67 75 6d 65 6e 74 0a 2a  their argument.*
3bdf0 2a 20 69 73 20 68 65 6c 64 20 6f 72 20 6e 6f 74  * is held or not
3be00 20 68 65 6c 64 2c 20 72 65 73 70 65 63 74 69 76   held, respectiv
3be10 65 6c 79 2c 20 62 79 20 74 68 65 20 63 61 6c 6c  ely, by the call
3be20 69 6e 67 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a  ing thread..**.*
3be30 2a 20 7b 58 31 37 30 38 34 7d 20 54 68 65 20 69  * {X17084} The i
3be40 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73  mplementation is
3be50 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f   not required to
3be60 20 70 72 6f 76 69 64 65 64 20 76 65 72 73 69 6f   provided versio
3be70 6e 73 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 72  ns of these.** r
3be80 6f 75 74 69 6e 65 73 20 74 68 61 74 20 61 63 74  outines that act
3be90 75 61 6c 6c 79 20 77 6f 72 6b 2e 20 49 66 20 74  ually work. If t
3bea0 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
3beb0 6e 20 64 6f 65 73 20 6e 6f 74 20 70 72 6f 76 69  n does not provi
3bec0 64 65 20 77 6f 72 6b 69 6e 67 0a 2a 2a 20 76 65  de working.** ve
3bed0 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 73 65 20  rsions of these 
3bee0 72 6f 75 74 69 6e 65 73 2c 20 69 74 20 73 68 6f  routines, it sho
3bef0 75 6c 64 20 61 74 20 6c 65 61 73 74 20 70 72 6f  uld at least pro
3bf00 76 69 64 65 20 73 74 75 62 73 20 74 68 61 74 20  vide stubs that 
3bf10 61 6c 77 61 79 73 0a 2a 2a 20 72 65 74 75 72 6e  always.** return
3bf20 20 74 72 75 65 20 73 6f 20 74 68 61 74 20 6f 6e   true so that on
3bf30 65 20 64 6f 65 73 20 6e 6f 74 20 67 65 74 20 73  e does not get s
3bf40 70 75 72 69 6f 75 73 20 61 73 73 65 72 74 69 6f  purious assertio
3bf50 6e 20 66 61 69 6c 75 72 65 73 2e 0a 2a 2a 0a 2a  n failures..**.*
3bf60 2a 20 7b 48 31 37 30 38 35 7d 20 49 66 20 74 68  * {H17085} If th
3bf70 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71  e argument to sq
3bf80 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
3bf90 28 29 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69  () is a NULL poi
3bfa0 6e 74 65 72 20 74 68 65 6e 0a 2a 2a 20 74 68 65  nter then.** the
3bfb0 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20   routine should 
3bfc0 72 65 74 75 72 6e 20 31 2e 20 20 7b 45 4e 44 7d  return 1.  {END}
3bfd0 20 54 68 69 73 20 73 65 65 6d 73 20 63 6f 75 6e   This seems coun
3bfe0 74 65 72 2d 69 6e 74 75 69 74 69 76 65 20 73 69  ter-intuitive si
3bff0 6e 63 65 0a 2a 2a 20 63 6c 65 61 72 6c 79 20 74  nce.** clearly t
3c000 68 65 20 6d 75 74 65 78 20 63 61 6e 6e 6f 74 20  he mutex cannot 
3c010 62 65 20 68 65 6c 64 20 69 66 20 69 74 20 64 6f  be held if it do
3c020 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 20 20 42  es not exist.  B
3c030 75 74 20 74 68 65 0a 2a 2a 20 74 68 65 20 72 65  ut the.** the re
3c040 61 73 6f 6e 20 74 68 65 20 6d 75 74 65 78 20 64  ason the mutex d
3c050 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 69 73  oes not exist is
3c060 20 62 65 63 61 75 73 65 20 74 68 65 20 62 75 69   because the bui
3c070 6c 64 20 69 73 20 6e 6f 74 0a 2a 2a 20 75 73 69  ld is not.** usi
3c080 6e 67 20 6d 75 74 65 78 65 73 2e 20 20 41 6e 64  ng mutexes.  And
3c090 20 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20   we do not want 
3c0a0 74 68 65 20 61 73 73 65 72 74 28 29 20 63 6f 6e  the assert() con
3c0b0 74 61 69 6e 69 6e 67 20 74 68 65 0a 2a 2a 20 63  taining the.** c
3c0c0 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d  all to sqlite3_m
3c0d0 75 74 65 78 5f 68 65 6c 64 28 29 20 74 6f 20 66  utex_held() to f
3c0e0 61 69 6c 2c 20 73 6f 20 61 20 6e 6f 6e 2d 7a 65  ail, so a non-ze
3c0f0 72 6f 20 72 65 74 75 72 6e 20 69 73 0a 2a 2a 20  ro return is.** 
3c100 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  the appropriate 
3c110 74 68 69 6e 67 20 74 6f 20 64 6f 2e 20 20 7b 48  thing to do.  {H
3c120 31 37 30 38 36 7d 20 54 68 65 20 73 71 6c 69 74  17086} The sqlit
3c130 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64  e3_mutex_notheld
3c140 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20  ().** interface 
3c150 73 68 6f 75 6c 64 20 61 6c 73 6f 20 72 65 74 75  should also retu
3c160 72 6e 20 31 20 77 68 65 6e 20 67 69 76 65 6e 20  rn 1 when given 
3c170 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a  a NULL pointer..
3c180 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
3c190 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  t sqlite3_mutex_
3c1a0 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74  held(sqlite3_mut
3c1b0 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  ex*);.SQLITE_API
3c1c0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74   int sqlite3_mut
3c1d0 65 78 5f 6e 6f 74 68 65 6c 64 28 73 71 6c 69 74  ex_notheld(sqlit
3c1e0 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a 0a  e3_mutex*);../*.
3c1f0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74  ** CAPI3REF: Mut
3c200 65 78 20 54 79 70 65 73 20 7b 48 31 37 30 30 31  ex Types {H17001
3c210 7d 20 3c 48 31 37 30 30 30 3e 0a 2a 2a 0a 2a 2a  } <H17000>.**.**
3c220 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75   The [sqlite3_mu
3c230 74 65 78 5f 61 6c 6c 6f 63 28 29 5d 20 69 6e 74  tex_alloc()] int
3c240 65 72 66 61 63 65 20 74 61 6b 65 73 20 61 20 73  erface takes a s
3c250 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 0a 2a  ingle argument.*
3c260 2a 20 77 68 69 63 68 20 69 73 20 6f 6e 65 20 6f  * which is one o
3c270 66 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 20  f these integer 
3c280 63 6f 6e 73 74 61 6e 74 73 2e 0a 2a 2a 0a 2a 2a  constants..**.**
3c290 20 54 68 65 20 73 65 74 20 6f 66 20 73 74 61 74   The set of stat
3c2a0 69 63 20 6d 75 74 65 78 65 73 20 6d 61 79 20 63  ic mutexes may c
3c2b0 68 61 6e 67 65 20 66 72 6f 6d 20 6f 6e 65 20 53  hange from one S
3c2c0 51 4c 69 74 65 20 72 65 6c 65 61 73 65 20 74 6f  QLite release to
3c2d0 20 74 68 65 0a 2a 2a 20 6e 65 78 74 2e 20 20 41   the.** next.  A
3c2e0 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74  pplications that
3c2f0 20 6f 76 65 72 72 69 64 65 20 74 68 65 20 62 75   override the bu
3c300 69 6c 74 2d 69 6e 20 6d 75 74 65 78 20 6c 6f 67  ilt-in mutex log
3c310 69 63 20 6d 75 73 74 20 62 65 0a 2a 2a 20 70 72  ic must be.** pr
3c320 65 70 61 72 65 64 20 74 6f 20 61 63 63 6f 6d 6d  epared to accomm
3c330 6f 64 61 74 65 20 61 64 64 69 74 69 6f 6e 61 6c  odate additional
3c340 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e   static mutexes.
3c350 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
3c360 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 20 20  TE_MUTEX_FAST   
3c370 20 20 20 20 20 20 20 20 20 20 30 0a 23 64 65 66            0.#def
3c380 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ine SQLITE_MUTEX
3c390 5f 52 45 43 55 52 53 49 56 45 20 20 20 20 20 20  _RECURSIVE      
3c3a0 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    1.#define SQLI
3c3b0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
3c3c0 4d 41 53 54 45 52 20 20 20 20 32 0a 23 64 65 66  MASTER    2.#def
3c3d0 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ine SQLITE_MUTEX
3c3e0 5f 53 54 41 54 49 43 5f 4d 45 4d 20 20 20 20 20  _STATIC_MEM     
3c3f0 20 20 33 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f    3  /* sqlite3_
3c400 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 23 64 65 66  malloc() */.#def
3c410 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ine SQLITE_MUTEX
3c420 5f 53 54 41 54 49 43 5f 4d 45 4d 32 20 20 20 20  _STATIC_MEM2    
3c430 20 20 34 20 20 2f 2a 20 4e 4f 54 20 55 53 45 44    4  /* NOT USED
3c440 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
3c450 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
3c460 4f 50 45 4e 20 20 20 20 20 20 34 20 20 2f 2a 20  OPEN      4  /* 
3c470 73 71 6c 69 74 65 33 42 74 72 65 65 4f 70 65 6e  sqlite3BtreeOpen
3c480 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  () */.#define SQ
3c490 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
3c4a0 43 5f 50 52 4e 47 20 20 20 20 20 20 35 20 20 2f  C_PRNG      5  /
3c4b0 2a 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d  * sqlite3_random
3c4c0 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  () */.#define SQ
3c4d0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
3c4e0 43 5f 4c 52 55 20 20 20 20 20 20 20 36 20 20 2f  C_LRU       6  /
3c4f0 2a 20 6c 72 75 20 70 61 67 65 20 6c 69 73 74 20  * lru page list 
3c500 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
3c510 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c  E_MUTEX_STATIC_L
3c520 52 55 32 20 20 20 20 20 20 37 20 20 2f 2a 20 6c  RU2      7  /* l
3c530 72 75 20 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a  ru page list */.
3c540 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
3c550 20 52 65 74 72 69 65 76 65 20 74 68 65 20 6d 75   Retrieve the mu
3c560 74 65 78 20 66 6f 72 20 61 20 64 61 74 61 62 61  tex for a databa
3c570 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 7b 48  se connection {H
3c580 31 37 30 30 32 7d 20 3c 48 31 37 30 30 30 3e 0a  17002} <H17000>.
3c590 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72  **.** This inter
3c5a0 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 70  face returns a p
3c5b0 6f 69 6e 74 65 72 20 74 68 65 20 5b 73 71 6c 69  ointer the [sqli
3c5c0 74 65 33 5f 6d 75 74 65 78 5d 20 6f 62 6a 65 63  te3_mutex] objec
3c5d0 74 20 74 68 61 74 20 0a 2a 2a 20 73 65 72 69 61  t that .** seria
3c5e0 6c 69 7a 65 73 20 61 63 63 65 73 73 20 74 6f 20  lizes access to 
3c5f0 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f  the [database co
3c600 6e 6e 65 63 74 69 6f 6e 5d 20 67 69 76 65 6e 20  nnection] given 
3c610 69 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 0a  in the argument.
3c620 2a 2a 20 77 68 65 6e 20 74 68 65 20 5b 74 68 72  ** when the [thr
3c630 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 69 73 20  eading mode] is 
3c640 53 65 72 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20 49  Serialized..** I
3c650 66 20 74 68 65 20 5b 74 68 72 65 61 64 69 6e 67  f the [threading
3c660 20 6d 6f 64 65 5d 20 69 73 20 53 69 6e 67 6c 65   mode] is Single
3c670 2d 74 68 72 65 61 64 20 6f 72 20 4d 75 6c 74 69  -thread or Multi
3c680 2d 74 68 72 65 61 64 20 74 68 65 6e 20 74 68 69  -thread then thi
3c690 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 72 65 74  s.** routine ret
3c6a0 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  urns a NULL poin
3c6b0 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ter..*/.SQLITE_A
3c6c0 50 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  PI sqlite3_mutex
3c6d0 20 2a 73 71 6c 69 74 65 33 5f 64 62 5f 6d 75 74   *sqlite3_db_mut
3c6e0 65 78 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f  ex(sqlite3*);../
3c6f0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4c  *.** CAPI3REF: L
3c700 6f 77 2d 4c 65 76 65 6c 20 43 6f 6e 74 72 6f 6c  ow-Level Control
3c710 20 4f 66 20 44 61 74 61 62 61 73 65 20 46 69 6c   Of Database Fil
3c720 65 73 20 7b 48 31 31 33 30 30 7d 20 3c 53 33 30  es {H11300} <S30
3c730 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33  800>.**.** {H113
3c740 30 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  01} The [sqlite3
3c750 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d  _file_control()]
3c760 20 69 6e 74 65 72 66 61 63 65 20 6d 61 6b 65 73   interface makes
3c770 20 61 20 64 69 72 65 63 74 20 63 61 6c 6c 20 74   a direct call t
3c780 6f 20 74 68 65 0a 2a 2a 20 78 46 69 6c 65 43 6f  o the.** xFileCo
3c790 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20 66 6f 72  ntrol method for
3c7a0 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f   the [sqlite3_io
3c7b0 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74  _methods] object
3c7c0 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77   associated.** w
3c7d0 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72  ith a particular
3c7e0 20 64 61 74 61 62 61 73 65 20 69 64 65 6e 74 69   database identi
3c7f0 66 69 65 64 20 62 79 20 74 68 65 20 73 65 63 6f  fied by the seco
3c800 6e 64 20 61 72 67 75 6d 65 6e 74 2e 20 7b 48 31  nd argument. {H1
3c810 31 33 30 32 7d 20 54 68 65 0a 2a 2a 20 6e 61 6d  1302} The.** nam
3c820 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
3c830 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 61 73  e is the name as
3c840 73 69 67 6e 65 64 20 74 6f 20 74 68 65 20 64 61  signed to the da
3c850 74 61 62 61 73 65 20 62 79 20 74 68 65 0a 2a 2a  tabase by the.**
3c860 20 3c 61 20 68 72 65 66 3d 22 6c 61 6e 67 5f 61   <a href="lang_a
3c870 74 74 61 63 68 2e 68 74 6d 6c 22 3e 41 54 54 41  ttach.html">ATTA
3c880 43 48 3c 2f 61 3e 20 53 51 4c 20 63 6f 6d 6d 61  CH</a> SQL comma
3c890 6e 64 20 74 68 61 74 20 6f 70 65 6e 65 64 20 74  nd that opened t
3c8a0 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20  he.** database. 
3c8b0 7b 48 31 31 33 30 33 7d 20 54 6f 20 63 6f 6e 74  {H11303} To cont
3c8c0 72 6f 6c 20 74 68 65 20 6d 61 69 6e 20 64 61 74  rol the main dat
3c8d0 61 62 61 73 65 20 66 69 6c 65 2c 20 75 73 65 20  abase file, use 
3c8e0 74 68 65 20 6e 61 6d 65 20 22 6d 61 69 6e 22 0a  the name "main".
3c8f0 2a 2a 20 6f 72 20 61 20 4e 55 4c 4c 20 70 6f 69  ** or a NULL poi
3c900 6e 74 65 72 2e 20 7b 48 31 31 33 30 34 7d 20 54  nter. {H11304} T
3c910 68 65 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75  he third and fou
3c920 72 74 68 20 70 61 72 61 6d 65 74 65 72 73 20 74  rth parameters t
3c930 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a  o this routine.*
3c940 2a 20 61 72 65 20 70 61 73 73 65 64 20 64 69 72  * are passed dir
3c950 65 63 74 6c 79 20 74 68 72 6f 75 67 68 20 74 6f  ectly through to
3c960 20 74 68 65 20 73 65 63 6f 6e 64 20 61 6e 64 20   the second and 
3c970 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 73  third parameters
3c980 20 6f 66 0a 2a 2a 20 74 68 65 20 78 46 69 6c 65   of.** the xFile
3c990 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 2e 20  Control method. 
3c9a0 20 7b 48 31 31 33 30 35 7d 20 54 68 65 20 72 65   {H11305} The re
3c9b0 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68  turn value of th
3c9c0 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 0a 2a  e xFileControl.*
3c9d0 2a 20 6d 65 74 68 6f 64 20 62 65 63 6f 6d 65 73  * method becomes
3c9e0 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
3c9f0 65 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e  e of this routin
3ca00 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30 36  e..**.** {H11306
3ca10 7d 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20  } If the second 
3ca20 70 61 72 61 6d 65 74 65 72 20 28 7a 44 62 4e 61  parameter (zDbNa
3ca30 6d 65 29 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74  me) does not mat
3ca40 63 68 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61  ch the name of a
3ca50 6e 79 0a 2a 2a 20 6f 70 65 6e 20 64 61 74 61 62  ny.** open datab
3ca60 61 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 53  ase file, then S
3ca70 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 73 20 72  QLITE_ERROR is r
3ca80 65 74 75 72 6e 65 64 2e 20 7b 48 31 31 33 30 37  eturned. {H11307
3ca90 7d 20 54 68 69 73 20 65 72 72 6f 72 0a 2a 2a 20  } This error.** 
3caa0 63 6f 64 65 20 69 73 20 6e 6f 74 20 72 65 6d 65  code is not reme
3cab0 6d 62 65 72 65 64 20 61 6e 64 20 77 69 6c 6c 20  mbered and will 
3cac0 6e 6f 74 20 62 65 20 72 65 63 61 6c 6c 65 64 20  not be recalled 
3cad0 62 79 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63  by [sqlite3_errc
3cae0 6f 64 65 28 29 5d 0a 2a 2a 20 6f 72 20 5b 73 71  ode()].** or [sq
3caf0 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e  lite3_errmsg()].
3cb00 20 7b 41 31 31 33 30 38 7d 20 54 68 65 20 75 6e   {A11308} The un
3cb10 64 65 72 6c 79 69 6e 67 20 78 46 69 6c 65 43 6f  derlying xFileCo
3cb20 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20 6d 69 67  ntrol method mig
3cb30 68 74 0a 2a 2a 20 61 6c 73 6f 20 72 65 74 75 72  ht.** also retur
3cb40 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 20  n SQLITE_ERROR. 
3cb50 20 7b 41 31 31 33 30 39 7d 20 54 68 65 72 65 20   {A11309} There 
3cb60 69 73 20 6e 6f 20 77 61 79 20 74 6f 20 64 69 73  is no way to dis
3cb70 74 69 6e 67 75 69 73 68 20 62 65 74 77 65 65 6e  tinguish between
3cb80 0a 2a 2a 20 61 6e 20 69 6e 63 6f 72 72 65 63 74  .** an incorrect
3cb90 20 7a 44 62 4e 61 6d 65 20 61 6e 64 20 61 6e 20   zDbName and an 
3cba0 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 72 65 74  SQLITE_ERROR ret
3cbb0 75 72 6e 20 66 72 6f 6d 20 74 68 65 20 75 6e 64  urn from the und
3cbc0 65 72 6c 79 69 6e 67 0a 2a 2a 20 78 46 69 6c 65  erlying.** xFile
3cbd0 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 2e 20  Control method. 
3cbe0 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 53 65 65 20  {END}.**.** See 
3cbf0 61 6c 73 6f 3a 20 5b 53 51 4c 49 54 45 5f 46 43  also: [SQLITE_FC
3cc00 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 5d 0a 2a  NTL_LOCKSTATE].*
3cc10 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
3cc20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f   sqlite3_file_co
3cc30 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 2a 2c 20  ntrol(sqlite3*, 
3cc40 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 4e  const char *zDbN
3cc50 61 6d 65 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69  ame, int op, voi
3cc60 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  d*);../*.** CAPI
3cc70 33 52 45 46 3a 20 54 65 73 74 69 6e 67 20 49 6e  3REF: Testing In
3cc80 74 65 72 66 61 63 65 20 7b 48 31 31 34 30 30 7d  terface {H11400}
3cc90 20 3c 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S30800>.**.** 
3cca0 54 68 65 20 73 71 6c 69 74 65 33 5f 74 65 73 74  The sqlite3_test
3ccb0 5f 63 6f 6e 74 72 6f 6c 28 29 20 69 6e 74 65 72  _control() inter
3ccc0 66 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20  face is used to 
3ccd0 72 65 61 64 20 6f 75 74 20 69 6e 74 65 72 6e 61  read out interna
3cce0 6c 0a 2a 2a 20 73 74 61 74 65 20 6f 66 20 53 51  l.** state of SQ
3ccf0 4c 69 74 65 20 61 6e 64 20 74 6f 20 69 6e 6a 65  Lite and to inje
3cd00 63 74 20 66 61 75 6c 74 73 20 69 6e 74 6f 20 53  ct faults into S
3cd10 51 4c 69 74 65 20 66 6f 72 20 74 65 73 74 69 6e  QLite for testin
3cd20 67 0a 2a 2a 20 70 75 72 70 6f 73 65 73 2e 20 20  g.** purposes.  
3cd30 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65  The first parame
3cd40 74 65 72 20 69 73 20 61 6e 20 6f 70 65 72 61 74  ter is an operat
3cd50 69 6f 6e 20 63 6f 64 65 20 74 68 61 74 20 64 65  ion code that de
3cd60 74 65 72 6d 69 6e 65 73 0a 2a 2a 20 74 68 65 20  termines.** the 
3cd70 6e 75 6d 62 65 72 2c 20 6d 65 61 6e 69 6e 67 2c  number, meaning,
3cd80 20 61 6e 64 20 6f 70 65 72 61 74 69 6f 6e 20 6f   and operation o
3cd90 66 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74  f all subsequent
3cda0 20 70 61 72 61 6d 65 74 65 72 73 2e 0a 2a 2a 0a   parameters..**.
3cdb0 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63  ** This interfac
3cdc0 65 20 69 73 20 6e 6f 74 20 66 6f 72 20 75 73 65  e is not for use
3cdd0 20 62 79 20 61 70 70 6c 69 63 61 74 69 6f 6e 73   by applications
3cde0 2e 20 20 49 74 20 65 78 69 73 74 73 20 73 6f 6c  .  It exists sol
3cdf0 65 6c 79 0a 2a 2a 20 66 6f 72 20 76 65 72 69 66  ely.** for verif
3ce00 79 69 6e 67 20 74 68 65 20 63 6f 72 72 65 63 74  ying the correct
3ce10 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68   operation of th
3ce20 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79  e SQLite library
3ce30 2e 20 20 44 65 70 65 6e 64 69 6e 67 0a 2a 2a 20  .  Depending.** 
3ce40 6f 6e 20 68 6f 77 20 74 68 65 20 53 51 4c 69 74  on how the SQLit
3ce50 65 20 6c 69 62 72 61 72 79 20 69 73 20 63 6f 6d  e library is com
3ce60 70 69 6c 65 64 2c 20 74 68 69 73 20 69 6e 74 65  piled, this inte
3ce70 72 66 61 63 65 20 6d 69 67 68 74 20 6e 6f 74 20  rface might not 
3ce80 65 78 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  exist..**.** The
3ce90 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68 65 20   details of the 
3cea0 6f 70 65 72 61 74 69 6f 6e 20 63 6f 64 65 73 2c  operation codes,
3ceb0 20 74 68 65 69 72 20 6d 65 61 6e 69 6e 67 73 2c   their meanings,
3cec0 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 73 0a   the parameters.
3ced0 2a 2a 20 74 68 65 79 20 74 61 6b 65 2c 20 61 6e  ** they take, an
3cee0 64 20 77 68 61 74 20 74 68 65 79 20 64 6f 20 61  d what they do a
3cef0 72 65 20 61 6c 6c 20 73 75 62 6a 65 63 74 20 74  re all subject t
3cf00 6f 20 63 68 61 6e 67 65 20 77 69 74 68 6f 75 74  o change without
3cf10 20 6e 6f 74 69 63 65 2e 0a 2a 2a 20 55 6e 6c 69   notice..** Unli
3cf20 6b 65 20 6d 6f 73 74 20 6f 66 20 74 68 65 20 53  ke most of the S
3cf30 51 4c 69 74 65 20 41 50 49 2c 20 74 68 69 73 20  QLite API, this 
3cf40 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20  function is not 
3cf50 67 75 61 72 61 6e 74 65 65 64 20 74 6f 0a 2a 2a  guaranteed to.**
3cf60 20 6f 70 65 72 61 74 65 20 63 6f 6e 73 69 73 74   operate consist
3cf70 65 6e 74 6c 79 20 66 72 6f 6d 20 6f 6e 65 20 72  ently from one r
3cf80 65 6c 65 61 73 65 20 74 6f 20 74 68 65 20 6e 65  elease to the ne
3cf90 78 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  xt..*/.SQLITE_AP
3cfa0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 65  I int sqlite3_te
3cfb0 73 74 5f 63 6f 6e 74 72 6f 6c 28 69 6e 74 20 6f  st_control(int o
3cfc0 70 2c 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20  p, ...);../*.** 
3cfd0 43 41 50 49 33 52 45 46 3a 20 54 65 73 74 69 6e  CAPI3REF: Testin
3cfe0 67 20 49 6e 74 65 72 66 61 63 65 20 4f 70 65 72  g Interface Oper
3cff0 61 74 69 6f 6e 20 43 6f 64 65 73 20 7b 48 31 31  ation Codes {H11
3d000 34 31 30 7d 20 3c 48 31 31 34 30 30 3e 0a 2a 2a  410} <H11400>.**
3d010 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61  .** These consta
3d020 6e 74 73 20 61 72 65 20 74 68 65 20 76 61 6c 69  nts are the vali
3d030 64 20 6f 70 65 72 61 74 69 6f 6e 20 63 6f 64 65  d operation code
3d040 20 70 61 72 61 6d 65 74 65 72 73 20 75 73 65 64   parameters used
3d050 0a 2a 2a 20 61 73 20 74 68 65 20 66 69 72 73 74  .** as the first
3d060 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 5b 73 71   argument to [sq
3d070 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72  lite3_test_contr
3d080 6f 6c 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ol()]..**.** The
3d090 73 65 20 70 61 72 61 6d 65 74 65 72 73 20 61 6e  se parameters an
3d0a0 64 20 74 68 65 69 72 20 6d 65 61 6e 69 6e 67 73  d their meanings
3d0b0 20 61 72 65 20 73 75 62 6a 65 63 74 20 74 6f 20   are subject to 
3d0c0 63 68 61 6e 67 65 0a 2a 2a 20 77 69 74 68 6f 75  change.** withou
3d0d0 74 20 6e 6f 74 69 63 65 2e 20 20 54 68 65 73 65  t notice.  These
3d0e0 20 76 61 6c 75 65 73 20 61 72 65 20 66 6f 72 20   values are for 
3d0f0 74 65 73 74 69 6e 67 20 70 75 72 70 6f 73 65 73  testing purposes
3d100 20 6f 6e 6c 79 2e 0a 2a 2a 20 41 70 70 6c 69 63   only..** Applic
3d110 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6e 6f  ations should no
3d120 74 20 75 73 65 20 61 6e 79 20 6f 66 20 74 68 65  t use any of the
3d130 73 65 20 70 61 72 61 6d 65 74 65 72 73 20 6f 72  se parameters or
3d140 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33   the.** [sqlite3
3d150 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 5d  _test_control()]
3d160 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 23   interface..*/.#
3d170 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45  define SQLITE_TE
3d180 53 54 43 54 52 4c 5f 50 52 4e 47 5f 53 41 56 45  STCTRL_PRNG_SAVE
3d190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3d1a0 35 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  5.#define SQLITE
3d1b0 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f 52  _TESTCTRL_PRNG_R
3d1c0 45 53 54 4f 52 45 20 20 20 20 20 20 20 20 20 20  ESTORE          
3d1d0 20 20 20 36 0a 23 64 65 66 69 6e 65 20 53 51 4c     6.#define SQL
3d1e0 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e  ITE_TESTCTRL_PRN
3d1f0 47 5f 52 45 53 45 54 20 20 20 20 20 20 20 20 20  G_RESET         
3d200 20 20 20 20 20 20 37 0a 23 64 65 66 69 6e 65 20        7.#define 
3d210 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f  SQLITE_TESTCTRL_
3d220 42 49 54 56 45 43 5f 54 45 53 54 20 20 20 20 20  BITVEC_TEST     
3d230 20 20 20 20 20 20 20 20 20 38 0a 23 64 65 66 69           8.#defi
3d240 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54  ne SQLITE_TESTCT
3d250 52 4c 5f 46 41 55 4c 54 5f 49 4e 53 54 41 4c 4c  RL_FAULT_INSTALL
3d260 20 20 20 20 20 20 20 20 20 20 20 20 39 0a 23 64              9.#d
3d270 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53  efine SQLITE_TES
3d280 54 43 54 52 4c 5f 42 45 4e 49 47 4e 5f 4d 41 4c  TCTRL_BENIGN_MAL
3d290 4c 4f 43 5f 48 4f 4f 4b 53 20 20 20 20 20 31 30  LOC_HOOKS     10
3d2a0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
3d2b0 54 45 53 54 43 54 52 4c 5f 50 45 4e 44 49 4e 47  TESTCTRL_PENDING
3d2c0 5f 42 59 54 45 20 20 20 20 20 20 20 20 20 20 20  _BYTE           
3d2d0 20 31 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   11.#define SQLI
3d2e0 54 45 5f 54 45 53 54 43 54 52 4c 5f 41 53 53 45  TE_TESTCTRL_ASSE
3d2f0 52 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  RT              
3d300 20 20 20 20 31 32 0a 23 64 65 66 69 6e 65 20 53      12.#define S
3d310 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 41  QLITE_TESTCTRL_A
3d320 4c 57 41 59 53 20 20 20 20 20 20 20 20 20 20 20  LWAYS           
3d330 20 20 20 20 20 20 20 31 33 0a 23 64 65 66 69 6e         13.#defin
3d340 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52  e SQLITE_TESTCTR
3d350 4c 5f 52 45 53 45 52 56 45 20 20 20 20 20 20 20  L_RESERVE       
3d360 20 20 20 20 20 20 20 20 20 20 31 34 0a 0a 2f 2a            14../*
3d370 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51  .** CAPI3REF: SQ
3d380 4c 69 74 65 20 52 75 6e 74 69 6d 65 20 53 74 61  Lite Runtime Sta
3d390 74 75 73 20 7b 48 31 37 32 30 30 7d 20 3c 53 36  tus {H17200} <S6
3d3a0 30 32 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d  0200>.** EXPERIM
3d3b0 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ENTAL.**.** This
3d3c0 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 75 73   interface is us
3d3d0 65 64 20 74 6f 20 72 65 74 72 69 65 76 65 20 72  ed to retrieve r
3d3e0 75 6e 74 69 6d 65 20 73 74 61 74 75 73 20 69 6e  untime status in
3d3f0 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f  formation.** abo
3d400 75 74 20 74 68 65 20 70 72 65 66 6f 72 6d 61 6e  ut the preforman
3d410 63 65 20 6f 66 20 53 51 4c 69 74 65 2c 20 61 6e  ce of SQLite, an
3d420 64 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 74 6f 20  d optionally to 
3d430 72 65 73 65 74 20 76 61 72 69 6f 75 73 0a 2a 2a  reset various.**
3d440 20 68 69 67 68 77 61 74 65 72 20 6d 61 72 6b 73   highwater marks
3d450 2e 20 20 54 68 65 20 66 69 72 73 74 20 61 72 67  .  The first arg
3d460 75 6d 65 6e 74 20 69 73 20 61 6e 20 69 6e 74 65  ument is an inte
3d470 67 65 72 20 63 6f 64 65 20 66 6f 72 0a 2a 2a 20  ger code for.** 
3d480 74 68 65 20 73 70 65 63 69 66 69 63 20 70 61 72  the specific par
3d490 61 6d 65 74 65 72 20 74 6f 20 6d 65 61 73 75 72  ameter to measur
3d4a0 65 2e 20 20 52 65 63 6f 67 6e 69 7a 65 64 20 69  e.  Recognized i
3d4b0 6e 74 65 67 65 72 20 63 6f 64 65 73 0a 2a 2a 20  nteger codes.** 
3d4c0 61 72 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  are of the form 
3d4d0 5b 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d  [SQLITE_STATUS_M
3d4e0 45 4d 4f 52 59 5f 55 53 45 44 20 7c 20 53 51 4c  EMORY_USED | SQL
3d4f0 49 54 45 5f 53 54 41 54 55 53 5f 2e 2e 2e 5d 2e  ITE_STATUS_...].
3d500 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20  .** The current 
3d510 76 61 6c 75 65 20 6f 66 20 74 68 65 20 70 61 72  value of the par
3d520 61 6d 65 74 65 72 20 69 73 20 72 65 74 75 72 6e  ameter is return
3d530 65 64 20 69 6e 74 6f 20 2a 70 43 75 72 72 65 6e  ed into *pCurren
3d540 74 2e 0a 2a 2a 20 54 68 65 20 68 69 67 68 65 73  t..** The highes
3d550 74 20 72 65 63 6f 72 64 65 64 20 76 61 6c 75 65  t recorded value
3d560 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20   is returned in 
3d570 2a 70 48 69 67 68 77 61 74 65 72 2e 20 20 49 66  *pHighwater.  If
3d580 20 74 68 65 0a 2a 2a 20 72 65 73 65 74 46 6c 61   the.** resetFla
3d590 67 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20  g is true, then 
3d5a0 74 68 65 20 68 69 67 68 65 73 74 20 72 65 63 6f  the highest reco
3d5b0 72 64 20 76 61 6c 75 65 20 69 73 20 72 65 73 65  rd value is rese
3d5c0 74 20 61 66 74 65 72 0a 2a 2a 20 2a 70 48 69 67  t after.** *pHig
3d5d0 68 77 61 74 65 72 20 69 73 20 77 72 69 74 74 65  hwater is writte
3d5e0 6e 2e 20 53 6f 6d 65 20 70 61 72 61 6d 65 74 65  n. Some paramete
3d5f0 72 73 20 64 6f 20 6e 6f 74 20 72 65 63 6f 72 64  rs do not record
3d600 20 74 68 65 20 68 69 67 68 65 73 74 0a 2a 2a 20   the highest.** 
3d610 76 61 6c 75 65 2e 20 20 46 6f 72 20 74 68 6f 73  value.  For thos
3d620 65 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20  e parameters.** 
3d630 6e 6f 74 68 69 6e 67 20 69 73 20 77 72 69 74 74  nothing is writt
3d640 65 6e 20 69 6e 74 6f 20 2a 70 48 69 67 68 77 61  en into *pHighwa
3d650 74 65 72 20 61 6e 64 20 74 68 65 20 72 65 73 65  ter and the rese
3d660 74 46 6c 61 67 20 69 73 20 69 67 6e 6f 72 65 64  tFlag is ignored
3d670 2e 0a 2a 2a 20 4f 74 68 65 72 20 70 61 72 61 6d  ..** Other param
3d680 65 74 65 72 73 20 72 65 63 6f 72 64 20 6f 6e 6c  eters record onl
3d690 79 20 74 68 65 20 68 69 67 68 77 61 74 65 72 20  y the highwater 
3d6a0 6d 61 72 6b 20 61 6e 64 20 6e 6f 74 20 74 68 65  mark and not the
3d6b0 20 63 75 72 72 65 6e 74 0a 2a 2a 20 76 61 6c 75   current.** valu
3d6c0 65 2e 20 20 46 6f 72 20 74 68 65 73 65 20 6c 61  e.  For these la
3d6d0 74 74 65 72 20 70 61 72 61 6d 65 74 65 72 73 20  tter parameters 
3d6e0 6e 6f 74 68 69 6e 67 20 69 73 20 77 72 69 74 74  nothing is writt
3d6f0 65 6e 20 69 6e 74 6f 20 2a 70 43 75 72 72 65 6e  en into *pCurren
3d700 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  t..**.** This ro
3d710 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 53 51  utine returns SQ
3d720 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65  LITE_OK on succe
3d730 73 73 20 61 6e 64 20 61 20 6e 6f 6e 2d 7a 65 72  ss and a non-zer
3d740 6f 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 6f 64 65  o.** [error code
3d750 5d 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2a  ] on failure..**
3d760 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
3d770 20 69 73 20 74 68 72 65 61 64 73 61 66 65 20 62   is threadsafe b
3d780 75 74 20 69 73 20 6e 6f 74 20 61 74 6f 6d 69 63  ut is not atomic
3d790 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
3d7a0 63 61 6e 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64  can be.** called
3d7b0 20 77 68 69 6c 65 20 6f 74 68 65 72 20 74 68 72   while other thr
3d7c0 65 61 64 73 20 61 72 65 20 72 75 6e 6e 69 6e 67  eads are running
3d7d0 20 74 68 65 20 73 61 6d 65 20 6f 72 20 64 69 66   the same or dif
3d7e0 66 65 72 65 6e 74 20 53 51 4c 69 74 65 0a 2a 2a  ferent SQLite.**
3d7f0 20 69 6e 74 65 72 66 61 63 65 73 2e 20 20 48 6f   interfaces.  Ho
3d800 77 65 76 65 72 20 74 68 65 20 76 61 6c 75 65 73  wever the values
3d810 20 72 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 43   returned in *pC
3d820 75 72 72 65 6e 74 20 61 6e 64 0a 2a 2a 20 2a 70  urrent and.** *p
3d830 48 69 67 68 77 61 74 65 72 20 72 65 66 6c 65 63  Highwater reflec
3d840 74 20 74 68 65 20 73 74 61 74 75 73 20 6f 66 20  t the status of 
3d850 53 51 4c 69 74 65 20 61 74 20 64 69 66 66 65 72  SQLite at differ
3d860 65 6e 74 20 70 6f 69 6e 74 73 20 69 6e 20 74 69  ent points in ti
3d870 6d 65 0a 2a 2a 20 61 6e 64 20 69 74 20 69 73 20  me.** and it is 
3d880 70 6f 73 73 69 62 6c 65 20 74 68 61 74 20 61 6e  possible that an
3d890 6f 74 68 65 72 20 74 68 72 65 61 64 20 6d 69 67  other thread mig
3d8a0 68 74 20 63 68 61 6e 67 65 20 74 68 65 20 70 61  ht change the pa
3d8b0 72 61 6d 65 74 65 72 0a 2a 2a 20 69 6e 20 62 65  rameter.** in be
3d8c0 74 77 65 65 6e 20 74 68 65 20 74 69 6d 65 73 20  tween the times 
3d8d0 77 68 65 6e 20 2a 70 43 75 72 72 65 6e 74 20 61  when *pCurrent a
3d8e0 6e 64 20 2a 70 48 69 67 68 77 61 74 65 72 20 61  nd *pHighwater a
3d8f0 72 65 20 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a  re written..**.*
3d900 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c  * See also: [sql
3d910 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 29  ite3_db_status()
3d920 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
3d930 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e  SQLITE_EXPERIMEN
3d940 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  TAL int sqlite3_
3d950 73 74 61 74 75 73 28 69 6e 74 20 6f 70 2c 20 69  status(int op, i
3d960 6e 74 20 2a 70 43 75 72 72 65 6e 74 2c 20 69 6e  nt *pCurrent, in
3d970 74 20 2a 70 48 69 67 68 77 61 74 65 72 2c 20 69  t *pHighwater, i
3d980 6e 74 20 72 65 73 65 74 46 6c 61 67 29 3b 0a 0a  nt resetFlag);..
3d990 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
3d9a0 20 53 74 61 74 75 73 20 50 61 72 61 6d 65 74 65   Status Paramete
3d9b0 72 73 20 7b 48 31 37 32 35 30 7d 20 3c 48 31 37  rs {H17250} <H17
3d9c0 32 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45  200>.** EXPERIME
3d9d0 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  NTAL.**.** These
3d9e0 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e   integer constan
3d9f0 74 73 20 64 65 73 69 67 6e 61 74 65 20 76 61 72  ts designate var
3da00 69 6f 75 73 20 72 75 6e 2d 74 69 6d 65 20 73 74  ious run-time st
3da10 61 74 75 73 20 70 61 72 61 6d 65 74 65 72 73 0a  atus parameters.
3da20 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20 72  ** that can be r
3da30 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69  eturned by [sqli
3da40 74 65 33 5f 73 74 61 74 75 73 28 29 5d 2e 0a 2a  te3_status()]..*
3da50 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74  *.** <dl>.** <dt
3da60 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d  >SQLITE_STATUS_M
3da70 45 4d 4f 52 59 5f 55 53 45 44 3c 2f 64 74 3e 0a  EMORY_USED</dt>.
3da80 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61  ** <dd>This para
3da90 6d 65 74 65 72 20 69 73 20 74 68 65 20 63 75 72  meter is the cur
3daa0 72 65 6e 74 20 61 6d 6f 75 6e 74 20 6f 66 20 6d  rent amount of m
3dab0 65 6d 6f 72 79 20 63 68 65 63 6b 65 64 20 6f 75  emory checked ou
3dac0 74 0a 2a 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69  t.** using [sqli
3dad0 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2c 20 65  te3_malloc()], e
3dae0 69 74 68 65 72 20 64 69 72 65 63 74 6c 79 20 6f  ither directly o
3daf0 72 20 69 6e 64 69 72 65 63 74 6c 79 2e 20 20 54  r indirectly.  T
3db00 68 65 0a 2a 2a 20 66 69 67 75 72 65 20 69 6e 63  he.** figure inc
3db10 6c 75 64 65 73 20 63 61 6c 6c 73 20 6d 61 64 65  ludes calls made
3db20 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c   to [sqlite3_mal
3db30 6c 6f 63 28 29 5d 20 62 79 20 74 68 65 20 61 70  loc()] by the ap
3db40 70 6c 69 63 61 74 69 6f 6e 0a 2a 2a 20 61 6e 64  plication.** and
3db50 20 69 6e 74 65 72 6e 61 6c 20 6d 65 6d 6f 72 79   internal memory
3db60 20 75 73 61 67 65 20 62 79 20 74 68 65 20 53 51   usage by the SQ
3db70 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 20 20 53  Lite library.  S
3db80 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 0a 2a 2a  cratch memory.**
3db90 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 5b   controlled by [
3dba0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43  SQLITE_CONFIG_SC
3dbb0 52 41 54 43 48 5d 20 61 6e 64 20 61 75 78 69 6c  RATCH] and auxil
3dbc0 69 61 72 79 20 70 61 67 65 2d 63 61 63 68 65 0a  iary page-cache.
3dbd0 2a 2a 20 6d 65 6d 6f 72 79 20 63 6f 6e 74 72 6f  ** memory contro
3dbe0 6c 6c 65 64 20 62 79 20 5b 53 51 4c 49 54 45 5f  lled by [SQLITE_
3dbf0 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45  CONFIG_PAGECACHE
3dc00 5d 20 69 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65  ] is not include
3dc10 64 20 69 6e 0a 2a 2a 20 74 68 69 73 20 70 61 72  d in.** this par
3dc20 61 6d 65 74 65 72 2e 20 20 54 68 65 20 61 6d 6f  ameter.  The amo
3dc30 75 6e 74 20 72 65 74 75 72 6e 65 64 20 69 73 20  unt returned is 
3dc40 74 68 65 20 73 75 6d 20 6f 66 20 74 68 65 20 61  the sum of the a
3dc50 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 73 69 7a  llocation.** siz
3dc60 65 73 20 61 73 20 72 65 70 6f 72 74 65 64 20 62  es as reported b
3dc70 79 20 74 68 65 20 78 53 69 7a 65 20 6d 65 74 68  y the xSize meth
3dc80 6f 64 20 69 6e 20 5b 73 71 6c 69 74 65 33 5f 6d  od in [sqlite3_m
3dc90 65 6d 5f 6d 65 74 68 6f 64 73 5d 2e 3c 2f 64 64  em_methods].</dd
3dca0 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
3dcb0 54 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43  TE_STATUS_MALLOC
3dcc0 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  _SIZE</dt>.** <d
3dcd0 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72  d>This parameter
3dce0 20 72 65 63 6f 72 64 73 20 74 68 65 20 6c 61 72   records the lar
3dcf0 67 65 73 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  gest memory allo
3dd00 63 61 74 69 6f 6e 20 72 65 71 75 65 73 74 0a 2a  cation request.*
3dd10 2a 20 68 61 6e 64 65 64 20 74 6f 20 5b 73 71 6c  * handed to [sql
3dd20 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f  ite3_malloc()] o
3dd30 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c  r [sqlite3_reall
3dd40 6f 63 28 29 5d 20 28 6f 72 20 74 68 65 69 72 0a  oc()] (or their.
3dd50 2a 2a 20 69 6e 74 65 72 6e 61 6c 20 65 71 75 69  ** internal equi
3dd60 76 61 6c 65 6e 74 73 29 2e 20 20 4f 6e 6c 79 20  valents).  Only 
3dd70 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  the value return
3dd80 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48  ed in the.** *pH
3dd90 69 67 68 77 61 74 65 72 20 70 61 72 61 6d 65 74  ighwater paramet
3dda0 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73  er to [sqlite3_s
3ddb0 74 61 74 75 73 28 29 5d 20 69 73 20 6f 66 20 69  tatus()] is of i
3ddc0 6e 74 65 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68  nterest.  .** Th
3ddd0 65 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20  e value written 
3dde0 69 6e 74 6f 20 74 68 65 20 2a 70 43 75 72 72 65  into the *pCurre
3ddf0 6e 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  nt parameter is 
3de00 75 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a  undefined.</dd>.
3de10 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  **.** <dt>SQLITE
3de20 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48  _STATUS_PAGECACH
3de30 45 5f 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c  E_USED</dt>.** <
3de40 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65  dd>This paramete
3de50 72 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75  r returns the nu
3de60 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 75 73  mber of pages us
3de70 65 64 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a  ed out of the.**
3de80 20 5b 70 61 67 65 63 61 63 68 65 20 6d 65 6d 6f   [pagecache memo
3de90 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 74 68  ry allocator] th
3dea0 61 74 20 77 61 73 20 63 6f 6e 66 69 67 75 72 65  at was configure
3deb0 64 20 75 73 69 6e 67 20 0a 2a 2a 20 5b 53 51 4c  d using .** [SQL
3dec0 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43  ITE_CONFIG_PAGEC
3ded0 41 43 48 45 5d 2e 20 20 54 68 65 0a 2a 2a 20 76  ACHE].  The.** v
3dee0 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73  alue returned is
3def0 20 69 6e 20 70 61 67 65 73 2c 20 6e 6f 74 20 69   in pages, not i
3df00 6e 20 62 79 74 65 73 2e 3c 2f 64 64 3e 0a 2a 2a  n bytes.</dd>.**
3df10 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53  .** <dt>SQLITE_S
3df20 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f  TATUS_PAGECACHE_
3df30 4f 56 45 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a 2a  OVERFLOW</dt>.**
3df40 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65   <dd>This parame
3df50 74 65 72 20 72 65 74 75 72 6e 73 20 74 68 65 20  ter returns the 
3df60 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
3df70 6f 66 20 70 61 67 65 20 63 61 63 68 65 0a 2a 2a  of page cache.**
3df80 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 68 69 63   allocation whic
3df90 68 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73  h could not be s
3dfa0 74 61 74 69 73 66 69 65 64 20 62 79 20 74 68 65  tatisfied by the
3dfb0 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f   [SQLITE_CONFIG_
3dfc0 50 41 47 45 43 41 43 48 45 5d 0a 2a 2a 20 62 75  PAGECACHE].** bu
3dfd0 66 66 65 72 20 61 6e 64 20 77 68 65 72 65 20 66  ffer and where f
3dfe0 6f 72 63 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f  orced to overflo
3dff0 77 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61  w to [sqlite3_ma
3e000 6c 6c 6f 63 28 29 5d 2e 20 20 54 68 65 0a 2a 2a  lloc()].  The.**
3e010 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20   returned value 
3e020 69 6e 63 6c 75 64 65 73 20 61 6c 6c 6f 63 61 74  includes allocat
3e030 69 6f 6e 73 20 74 68 61 74 20 6f 76 65 72 66 6c  ions that overfl
3e040 6f 77 65 64 20 62 65 63 61 75 73 65 20 74 68 65  owed because the
3e050 79 0a 2a 2a 20 77 68 65 72 65 20 74 6f 6f 20 6c  y.** where too l
3e060 61 72 67 65 20 28 74 68 65 79 20 77 65 72 65 20  arge (they were 
3e070 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20  larger than the 
3e080 22 73 7a 22 20 70 61 72 61 6d 65 74 65 72 20 74  "sz" parameter t
3e090 6f 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e  o.** [SQLITE_CON
3e0a0 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d 29 20  FIG_PAGECACHE]) 
3e0b0 61 6e 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20  and allocations 
3e0c0 74 68 61 74 20 6f 76 65 72 66 6c 6f 77 65 64 20  that overflowed 
3e0d0 62 65 63 61 75 73 65 0a 2a 2a 20 6e 6f 20 73 70  because.** no sp
3e0e0 61 63 65 20 77 61 73 20 6c 65 66 74 20 69 6e 20  ace was left in 
3e0f0 74 68 65 20 70 61 67 65 20 63 61 63 68 65 2e 3c  the page cache.<
3e100 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53  /dd>.**.** <dt>S
3e110 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47  QLITE_STATUS_PAG
3e120 45 43 41 43 48 45 5f 53 49 5a 45 3c 2f 64 74 3e  ECACHE_SIZE</dt>
3e130 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72  .** <dd>This par
3e140 61 6d 65 74 65 72 20 72 65 63 6f 72 64 73 20 74  ameter records t
3e150 68 65 20 6c 61 72 67 65 73 74 20 6d 65 6d 6f 72  he largest memor
3e160 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71  y allocation req
3e170 75 65 73 74 0a 2a 2a 20 68 61 6e 64 65 64 20 74  uest.** handed t
3e180 6f 20 5b 70 61 67 65 63 61 63 68 65 20 6d 65 6d  o [pagecache mem
3e190 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 2e 20  ory allocator]. 
3e1a0 20 4f 6e 6c 79 20 74 68 65 20 76 61 6c 75 65 20   Only the value 
3e1b0 72 65 74 75 72 6e 65 64 20 69 6e 20 74 68 65 0a  returned in the.
3e1c0 2a 2a 20 2a 70 48 69 67 68 77 61 74 65 72 20 70  ** *pHighwater p
3e1d0 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c  arameter to [sql
3e1e0 69 74 65 33 5f 73 74 61 74 75 73 28 29 5d 20 69  ite3_status()] i
3e1f0 73 20 6f 66 20 69 6e 74 65 72 65 73 74 2e 20 20  s of interest.  
3e200 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 77 72  .** The value wr
3e210 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 2a  itten into the *
3e220 70 43 75 72 72 65 6e 74 20 70 61 72 61 6d 65 74  pCurrent paramet
3e230 65 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e  er is undefined.
3e240 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e  </dd>.**.** <dt>
3e250 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43  SQLITE_STATUS_SC
3e260 52 41 54 43 48 5f 55 53 45 44 3c 2f 64 74 3e 0a  RATCH_USED</dt>.
3e270 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61  ** <dd>This para
3e280 6d 65 74 65 72 20 72 65 74 75 72 6e 73 20 74 68  meter returns th
3e290 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f  e number of allo
3e2a0 63 61 74 69 6f 6e 73 20 75 73 65 64 20 6f 75 74  cations used out
3e2b0 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 63 72 61   of the.** [scra
3e2c0 74 63 68 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  tch memory alloc
3e2d0 61 74 6f 72 5d 20 63 6f 6e 66 69 67 75 72 65 64  ator] configured
3e2e0 20 75 73 69 6e 67 0a 2a 2a 20 5b 53 51 4c 49 54   using.** [SQLIT
3e2f0 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48  E_CONFIG_SCRATCH
3e300 5d 2e 20 20 54 68 65 20 76 61 6c 75 65 20 72 65  ].  The value re
3e310 74 75 72 6e 65 64 20 69 73 20 69 6e 20 61 6c 6c  turned is in all
3e320 6f 63 61 74 69 6f 6e 73 2c 20 6e 6f 74 0a 2a 2a  ocations, not.**
3e330 20 69 6e 20 62 79 74 65 73 2e 20 20 53 69 6e 63   in bytes.  Sinc
3e340 65 20 61 20 73 69 6e 67 6c 65 20 74 68 72 65 61  e a single threa
3e350 64 20 6d 61 79 20 6f 6e 6c 79 20 68 61 76 65 20  d may only have 
3e360 6f 6e 65 20 73 63 72 61 74 63 68 20 61 6c 6c 6f  one scratch allo
3e370 63 61 74 69 6f 6e 0a 2a 2a 20 6f 75 74 73 74 61  cation.** outsta
3e380 6e 64 69 6e 67 20 61 74 20 74 69 6d 65 2c 20 74  nding at time, t
3e390 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 61 6c  his parameter al
3e3a0 73 6f 20 72 65 70 6f 72 74 73 20 74 68 65 20 6e  so reports the n
3e3b0 75 6d 62 65 72 20 6f 66 20 74 68 72 65 61 64 73  umber of threads
3e3c0 0a 2a 2a 20 75 73 69 6e 67 20 73 63 72 61 74 63  .** using scratc
3e3d0 68 20 6d 65 6d 6f 72 79 20 61 74 20 74 68 65 20  h memory at the 
3e3e0 73 61 6d 65 20 74 69 6d 65 2e 3c 2f 64 64 3e 0a  same time.</dd>.
3e3f0 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  **.** <dt>SQLITE
3e400 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f  _STATUS_SCRATCH_
3e410 4f 56 45 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a 2a  OVERFLOW</dt>.**
3e420 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65   <dd>This parame
3e430 74 65 72 20 72 65 74 75 72 6e 73 20 74 68 65 20  ter returns the 
3e440 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
3e450 6f 66 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72  of scratch memor
3e460 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  y.** allocation 
3e470 77 68 69 63 68 20 63 6f 75 6c 64 20 6e 6f 74 20  which could not 
3e480 62 65 20 73 74 61 74 69 73 66 69 65 64 20 62 79  be statisfied by
3e490 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 43 4f 4e   the [SQLITE_CON
3e4a0 46 49 47 5f 53 43 52 41 54 43 48 5d 0a 2a 2a 20  FIG_SCRATCH].** 
3e4b0 62 75 66 66 65 72 20 61 6e 64 20 77 68 65 72 65  buffer and where
3e4c0 20 66 6f 72 63 65 64 20 74 6f 20 6f 76 65 72 66   forced to overf
3e4d0 6c 6f 77 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  low to [sqlite3_
3e4e0 6d 61 6c 6c 6f 63 28 29 5d 2e 20 20 54 68 65 20  malloc()].  The 
3e4f0 76 61 6c 75 65 73 0a 2a 2a 20 72 65 74 75 72 6e  values.** return
3e500 65 64 20 69 6e 63 6c 75 64 65 20 6f 76 65 72 66  ed include overf
3e510 6c 6f 77 73 20 62 65 63 61 75 73 65 20 74 68 65  lows because the
3e520 20 72 65 71 75 65 73 74 65 64 20 61 6c 6c 6f 63   requested alloc
3e530 61 74 69 6f 6e 20 77 61 73 20 74 6f 6f 0a 2a 2a  ation was too.**
3e540 20 6c 61 72 67 65 72 20 28 74 68 61 74 20 69 73   larger (that is
3e550 2c 20 62 65 63 61 75 73 65 20 74 68 65 20 72 65  , because the re
3e560 71 75 65 73 74 65 64 20 61 6c 6c 6f 63 61 74 69  quested allocati
3e570 6f 6e 20 77 61 73 20 6c 61 72 67 65 72 20 74 68  on was larger th
3e580 61 6e 20 74 68 65 0a 2a 2a 20 22 73 7a 22 20 70  an the.** "sz" p
3e590 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 53 51 4c  arameter to [SQL
3e5a0 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54  ITE_CONFIG_SCRAT
3e5b0 43 48 5d 29 20 61 6e 64 20 62 65 63 61 75 73 65  CH]) and because
3e5c0 20 6e 6f 20 73 63 72 61 74 63 68 20 62 75 66 66   no scratch buff
3e5d0 65 72 0a 2a 2a 20 73 6c 6f 74 73 20 77 65 72 65  er.** slots were
3e5e0 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20 3c   available..** <
3e5f0 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53  /dd>.**.** <dt>S
3e600 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52  QLITE_STATUS_SCR
3e610 41 54 43 48 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a  ATCH_SIZE</dt>.*
3e620 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d  * <dd>This param
3e630 65 74 65 72 20 72 65 63 6f 72 64 73 20 74 68 65  eter records the
3e640 20 6c 61 72 67 65 73 74 20 6d 65 6d 6f 72 79 20   largest memory 
3e650 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 65  allocation reque
3e660 73 74 0a 2a 2a 20 68 61 6e 64 65 64 20 74 6f 20  st.** handed to 
3e670 5b 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 20  [scratch memory 
3e680 61 6c 6c 6f 63 61 74 6f 72 5d 2e 20 20 4f 6e 6c  allocator].  Onl
3e690 79 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75  y the value retu
3e6a0 72 6e 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 2a  rned in the.** *
3e6b0 70 48 69 67 68 77 61 74 65 72 20 70 61 72 61 6d  pHighwater param
3e6c0 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33  eter to [sqlite3
3e6d0 5f 73 74 61 74 75 73 28 29 5d 20 69 73 20 6f 66  _status()] is of
3e6e0 20 69 6e 74 65 72 65 73 74 2e 20 20 0a 2a 2a 20   interest.  .** 
3e6f0 54 68 65 20 76 61 6c 75 65 20 77 72 69 74 74 65  The value writte
3e700 6e 20 69 6e 74 6f 20 74 68 65 20 2a 70 43 75 72  n into the *pCur
3e710 72 65 6e 74 20 70 61 72 61 6d 65 74 65 72 20 69  rent parameter i
3e720 73 20 75 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 64  s undefined.</dd
3e730 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
3e740 54 45 5f 53 54 41 54 55 53 5f 50 41 52 53 45 52  TE_STATUS_PARSER
3e750 5f 53 54 41 43 4b 3c 2f 64 74 3e 0a 2a 2a 20 3c  _STACK</dt>.** <
3e760 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65  dd>This paramete
3e770 72 20 72 65 63 6f 72 64 73 20 74 68 65 20 64 65  r records the de
3e780 65 70 65 73 74 20 70 61 72 73 65 72 20 73 74 61  epest parser sta
3e790 63 6b 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79 0a  ck.  It is only.
3e7a0 2a 2a 20 6d 65 61 6e 69 6e 67 66 75 6c 20 69 66  ** meaningful if
3e7b0 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69   SQLite is compi
3e7c0 6c 65 64 20 77 69 74 68 20 5b 59 59 54 52 41 43  led with [YYTRAC
3e7d0 4b 4d 41 58 53 54 41 43 4b 44 45 50 54 48 5d 2e  KMAXSTACKDEPTH].
3e7e0 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a  </dd>.** </dl>.*
3e7f0 2a 0a 2a 2a 20 4e 65 77 20 73 74 61 74 75 73 20  *.** New status 
3e800 70 61 72 61 6d 65 74 65 72 73 20 6d 61 79 20 62  parameters may b
3e810 65 20 61 64 64 65 64 20 66 72 6f 6d 20 74 69 6d  e added from tim
3e820 65 20 74 6f 20 74 69 6d 65 2e 0a 2a 2f 0a 23 64  e to time..*/.#d
3e830 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41  efine SQLITE_STA
3e840 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 20  TUS_MEMORY_USED 
3e850 20 20 20 20 20 20 20 20 20 30 0a 23 64 65 66 69           0.#defi
3e860 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53  ne SQLITE_STATUS
3e870 5f 50 41 47 45 43 41 43 48 45 5f 55 53 45 44 20  _PAGECACHE_USED 
3e880 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20        1.#define 
3e890 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41  SQLITE_STATUS_PA
3e8a0 47 45 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57  GECACHE_OVERFLOW
3e8b0 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c     2.#define SQL
3e8c0 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54  ITE_STATUS_SCRAT
3e8d0 43 48 5f 55 53 45 44 20 20 20 20 20 20 20 20 20  CH_USED         
3e8e0 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  3.#define SQLITE
3e8f0 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f  _STATUS_SCRATCH_
3e900 4f 56 45 52 46 4c 4f 57 20 20 20 20 20 34 0a 23  OVERFLOW     4.#
3e910 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54  define SQLITE_ST
3e920 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45  ATUS_MALLOC_SIZE
3e930 20 20 20 20 20 20 20 20 20 20 35 0a 23 64 65 66            5.#def
3e940 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55  ine SQLITE_STATU
3e950 53 5f 50 41 52 53 45 52 5f 53 54 41 43 4b 20 20  S_PARSER_STACK  
3e960 20 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65         6.#define
3e970 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50   SQLITE_STATUS_P
3e980 41 47 45 43 41 43 48 45 5f 53 49 5a 45 20 20 20  AGECACHE_SIZE   
3e990 20 20 20 20 37 0a 23 64 65 66 69 6e 65 20 53 51      7.#define SQ
3e9a0 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41  LITE_STATUS_SCRA
3e9b0 54 43 48 5f 53 49 5a 45 20 20 20 20 20 20 20 20  TCH_SIZE        
3e9c0 20 38 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52   8../*.** CAPI3R
3e9d0 45 46 3a 20 44 61 74 61 62 61 73 65 20 43 6f 6e  EF: Database Con
3e9e0 6e 65 63 74 69 6f 6e 20 53 74 61 74 75 73 20 7b  nection Status {
3e9f0 48 31 37 35 30 30 7d 20 3c 53 36 30 32 30 30 3e  H17500} <S60200>
3ea00 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c  .** EXPERIMENTAL
3ea10 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65  .**.** This inte
3ea20 72 66 61 63 65 20 69 73 20 75 73 65 64 20 74 6f  rface is used to
3ea30 20 72 65 74 72 69 65 76 65 20 72 75 6e 74 69 6d   retrieve runtim
3ea40 65 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61  e status informa
3ea50 74 69 6f 6e 20 0a 2a 2a 20 61 62 6f 75 74 20 61  tion .** about a
3ea60 20 73 69 6e 67 6c 65 20 5b 64 61 74 61 62 61 73   single [databas
3ea70 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 20  e connection].  
3ea80 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  The first argume
3ea90 6e 74 20 69 73 20 74 68 65 0a 2a 2a 20 64 61 74  nt is the.** dat
3eaa0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
3eab0 20 6f 62 6a 65 63 74 20 74 6f 20 62 65 20 69 6e   object to be in
3eac0 74 65 72 72 6f 67 61 74 65 64 2e 20 20 54 68 65  terrogated.  The
3ead0 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
3eae0 0a 2a 2a 20 69 73 20 74 68 65 20 70 61 72 61 6d  .** is the param
3eaf0 65 74 65 72 20 74 6f 20 69 6e 74 65 72 72 6f 67  eter to interrog
3eb00 61 74 65 2e 20 20 43 75 72 72 65 6e 74 6c 79 2c  ate.  Currently,
3eb10 20 74 68 65 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65   the only allowe
3eb20 64 20 76 61 6c 75 65 0a 2a 2a 20 66 6f 72 20 74  d value.** for t
3eb30 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65  he second parame
3eb40 74 65 72 20 69 73 20 5b 53 51 4c 49 54 45 5f 44  ter is [SQLITE_D
3eb50 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44  BSTATUS_LOOKASID
3eb60 45 5f 55 53 45 44 5d 2e 0a 2a 2a 20 41 64 64 69  E_USED]..** Addi
3eb70 74 69 6f 6e 61 6c 20 6f 70 74 69 6f 6e 73 20 77  tional options w
3eb80 69 6c 6c 20 6c 69 6b 65 6c 79 20 61 70 70 65 61  ill likely appea
3eb90 72 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65  r in future rele
3eba0 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a  ases of SQLite..
3ebb0 2a 2a 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 6e  **.** The curren
3ebc0 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72  t value of the r
3ebd0 65 71 75 65 73 74 65 64 20 70 61 72 61 6d 65 74  equested paramet
3ebe0 65 72 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e  er is written in
3ebf0 74 6f 20 2a 70 43 75 72 0a 2a 2a 20 61 6e 64 20  to *pCur.** and 
3ec00 74 68 65 20 68 69 67 68 65 73 74 20 69 6e 73 74  the highest inst
3ec10 61 6e 74 61 6e 65 6f 75 73 20 76 61 6c 75 65 20  antaneous value 
3ec20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20  is written into 
3ec30 2a 70 48 69 77 74 72 2e 20 20 49 66 0a 2a 2a 20  *pHiwtr.  If.** 
3ec40 74 68 65 20 72 65 73 65 74 46 6c 67 20 69 73 20  the resetFlg is 
3ec50 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 68  true, then the h
3ec60 69 67 68 65 73 74 20 69 6e 73 74 61 6e 74 61 6e  ighest instantan
3ec70 65 6f 75 73 20 76 61 6c 75 65 20 69 73 0a 2a 2a  eous value is.**
3ec80 20 72 65 73 65 74 20 62 61 63 6b 20 64 6f 77 6e   reset back down
3ec90 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20   to the current 
3eca0 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  value..**.** See
3ecb0 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f   also: [sqlite3_
3ecc0 73 74 61 74 75 73 28 29 5d 20 61 6e 64 20 5b 73  status()] and [s
3ecd0 71 6c 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74  qlite3_stmt_stat
3ece0 75 73 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45  us()]..*/.SQLITE
3ecf0 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45  _API SQLITE_EXPE
3ed00 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c  RIMENTAL int sql
3ed10 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 73  ite3_db_status(s
3ed20 71 6c 69 74 65 33 2a 2c 20 69 6e 74 20 6f 70 2c  qlite3*, int op,
3ed30 20 69 6e 74 20 2a 70 43 75 72 2c 20 69 6e 74 20   int *pCur, int 
3ed40 2a 70 48 69 77 74 72 2c 20 69 6e 74 20 72 65 73  *pHiwtr, int res
3ed50 65 74 46 6c 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  etFlg);../*.** C
3ed60 41 50 49 33 52 45 46 3a 20 53 74 61 74 75 73 20  API3REF: Status 
3ed70 50 61 72 61 6d 65 74 65 72 73 20 66 6f 72 20 64  Parameters for d
3ed80 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
3ed90 6f 6e 73 20 7b 48 31 37 35 32 30 7d 20 3c 48 31  ons {H17520} <H1
3eda0 37 35 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d  7500>.** EXPERIM
3edb0 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73  ENTAL.**.** Thes
3edc0 65 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20  e constants are 
3edd0 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20 69 6e  the available in
3ede0 74 65 67 65 72 20 22 76 65 72 62 73 22 20 74 68  teger "verbs" th
3edf0 61 74 20 63 61 6e 20 62 65 20 70 61 73 73 65 64  at can be passed
3ee00 20 61 73 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e   as.** the secon
3ee10 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  d argument to th
3ee20 65 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 73 74  e [sqlite3_db_st
3ee30 61 74 75 73 28 29 5d 20 69 6e 74 65 72 66 61 63  atus()] interfac
3ee40 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 76 65 72  e..**.** New ver
3ee50 62 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20  bs may be added 
3ee60 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73  in future releas
3ee70 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 45 78  es of SQLite. Ex
3ee80 69 73 74 69 6e 67 20 76 65 72 62 73 0a 2a 2a 20  isting verbs.** 
3ee90 6d 69 67 68 74 20 62 65 20 64 69 73 63 6f 6e 74  might be discont
3eea0 69 6e 75 65 64 2e 20 41 70 70 6c 69 63 61 74 69  inued. Applicati
3eeb0 6f 6e 73 20 73 68 6f 75 6c 64 20 63 68 65 63 6b  ons should check
3eec0 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65   the return code
3eed0 20 66 72 6f 6d 0a 2a 2a 20 5b 73 71 6c 69 74 65   from.** [sqlite
3eee0 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 20 74  3_db_status()] t
3eef0 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74  o make sure that
3ef00 20 74 68 65 20 63 61 6c 6c 20 77 6f 72 6b 65 64   the call worked
3ef10 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65  ..** The [sqlite
3ef20 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 20 69  3_db_status()] i
3ef30 6e 74 65 72 66 61 63 65 20 77 69 6c 6c 20 72 65  nterface will re
3ef40 74 75 72 6e 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20  turn a non-zero 
3ef50 65 72 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 69 66  error code.** if
3ef60 20 61 20 64 69 73 63 6f 6e 74 69 6e 75 65 64 20   a discontinued 
3ef70 6f 72 20 75 6e 73 75 70 70 6f 72 74 65 64 20 76  or unsupported v
3ef80 65 72 62 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a  erb is invoked..
3ef90 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64  **.** <dl>.** <d
3efa0 74 3e 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55  t>SQLITE_DBSTATU
3efb0 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44  S_LOOKASIDE_USED
3efc0 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69  </dt>.** <dd>Thi
3efd0 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 75  s parameter retu
3efe0 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  rns the number o
3eff0 66 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f  f lookaside memo
3f000 72 79 20 73 6c 6f 74 73 20 63 75 72 72 65 6e 74  ry slots current
3f010 6c 79 0a 2a 2a 20 63 68 65 63 6b 65 64 20 6f 75  ly.** checked ou
3f020 74 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e  t.</dd>.** </dl>
3f030 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
3f040 54 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b  TE_DBSTATUS_LOOK
3f050 41 53 49 44 45 5f 55 53 45 44 20 20 20 20 20 30  ASIDE_USED     0
3f060 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  .../*.** CAPI3RE
3f070 46 3a 20 50 72 65 70 61 72 65 64 20 53 74 61 74  F: Prepared Stat
3f080 65 6d 65 6e 74 20 53 74 61 74 75 73 20 7b 48 31  ement Status {H1
3f090 37 35 35 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a  7550} <S60200>.*
3f0a0 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a  * EXPERIMENTAL.*
3f0b0 2a 0a 2a 2a 20 45 61 63 68 20 70 72 65 70 61 72  *.** Each prepar
3f0c0 65 64 20 73 74 61 74 65 6d 65 6e 74 20 6d 61 69  ed statement mai
3f0d0 6e 74 61 69 6e 73 20 76 61 72 69 6f 75 73 0a 2a  ntains various.*
3f0e0 2a 20 5b 53 51 4c 49 54 45 5f 53 54 4d 54 53 54  * [SQLITE_STMTST
3f0f0 41 54 55 53 5f 53 4f 52 54 20 7c 20 63 6f 75 6e  ATUS_SORT | coun
3f100 74 65 72 73 5d 20 74 68 61 74 20 6d 65 61 73 75  ters] that measu
3f110 72 65 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a  re the number.**
3f120 20 6f 66 20 74 69 6d 65 73 20 69 74 20 68 61 73   of times it has
3f130 20 70 65 72 66 6f 72 6d 65 64 20 73 70 65 63 69   performed speci
3f140 66 69 63 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20  fic operations. 
3f150 20 54 68 65 73 65 20 63 6f 75 6e 74 65 72 73 20   These counters 
3f160 63 61 6e 0a 2a 2a 20 62 65 20 75 73 65 64 20 74  can.** be used t
3f170 6f 20 6d 6f 6e 69 74 6f 72 20 74 68 65 20 70 65  o monitor the pe
3f180 72 66 6f 72 6d 61 6e 63 65 20 63 68 61 72 61 63  rformance charac
3f190 74 65 72 69 73 74 69 63 73 20 6f 66 20 74 68 65  teristics of the
3f1a0 20 70 72 65 70 61 72 65 64 0a 2a 2a 20 73 74 61   prepared.** sta
3f1b0 74 65 6d 65 6e 74 73 2e 20 20 46 6f 72 20 65 78  tements.  For ex
3f1c0 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 6e 75  ample, if the nu
3f1d0 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 20 73 74  mber of table st
3f1e0 65 70 73 20 67 72 65 61 74 6c 79 20 65 78 63 65  eps greatly exce
3f1f0 65 64 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65  eds.** the numbe
3f200 72 20 6f 66 20 74 61 62 6c 65 20 73 65 61 72 63  r of table searc
3f210 68 65 73 20 6f 72 20 72 65 73 75 6c 74 20 72 6f  hes or result ro
3f220 77 73 2c 20 74 68 61 74 20 77 6f 75 6c 64 20 74  ws, that would t
3f230 65 6e 64 20 74 6f 20 69 6e 64 69 63 61 74 65 0a  end to indicate.
3f240 2a 2a 20 74 68 61 74 20 74 68 65 20 70 72 65 70  ** that the prep
3f250 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 69  ared statement i
3f260 73 20 75 73 69 6e 67 20 61 20 66 75 6c 6c 20 74  s using a full t
3f270 61 62 6c 65 20 73 63 61 6e 20 72 61 74 68 65 72  able scan rather
3f280 20 74 68 61 6e 0a 2a 2a 20 61 6e 20 69 6e 64 65   than.** an inde
3f290 78 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  x.  .**.** This 
3f2a0 69 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 65  interface is use
3f2b0 64 20 74 6f 20 72 65 74 72 69 65 76 65 20 61 6e  d to retrieve an
3f2c0 64 20 72 65 73 65 74 20 63 6f 75 6e 74 65 72 20  d reset counter 
3f2d0 76 61 6c 75 65 73 20 66 72 6f 6d 0a 2a 2a 20 61  values from.** a
3f2e0 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
3f2f0 6d 65 6e 74 5d 2e 20 20 54 68 65 20 66 69 72 73  ment].  The firs
3f300 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68  t argument is th
3f310 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  e prepared state
3f320 6d 65 6e 74 0a 2a 2a 20 6f 62 6a 65 63 74 20 74  ment.** object t
3f330 6f 20 62 65 20 69 6e 74 65 72 72 6f 67 61 74 65  o be interrogate
3f340 64 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61  d.  The second a
3f350 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 61 6e  rgument.** is an
3f360 20 69 6e 74 65 67 65 72 20 63 6f 64 65 20 66 6f   integer code fo
3f370 72 20 61 20 73 70 65 63 69 66 69 63 20 5b 53 51  r a specific [SQ
3f380 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f  LITE_STMTSTATUS_
3f390 53 4f 52 54 20 7c 20 63 6f 75 6e 74 65 72 5d 0a  SORT | counter].
3f3a0 2a 2a 20 74 6f 20 62 65 20 69 6e 74 65 72 72 6f  ** to be interro
3f3b0 67 61 74 65 64 2e 20 0a 2a 2a 20 54 68 65 20 63  gated. .** The c
3f3c0 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20  urrent value of 
3f3d0 74 68 65 20 72 65 71 75 65 73 74 65 64 20 63 6f  the requested co
3f3e0 75 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65  unter is returne
3f3f0 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73  d..** If the res
3f400 65 74 46 6c 67 20 69 73 20 74 72 75 65 2c 20 74  etFlg is true, t
3f410 68 65 6e 20 74 68 65 20 63 6f 75 6e 74 65 72 20  hen the counter 
3f420 69 73 20 72 65 73 65 74 20 74 6f 20 7a 65 72 6f  is reset to zero
3f430 20 61 66 74 65 72 20 74 68 69 73 0a 2a 2a 20 69   after this.** i
3f440 6e 74 65 72 66 61 63 65 20 63 61 6c 6c 20 72 65  nterface call re
3f450 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  turns..**.** See
3f460 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f   also: [sqlite3_
3f470 73 74 61 74 75 73 28 29 5d 20 61 6e 64 20 5b 73  status()] and [s
3f480 71 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73  qlite3_db_status
3f490 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ()]..*/.SQLITE_A
3f4a0 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49  PI SQLITE_EXPERI
3f4b0 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74  MENTAL int sqlit
3f4c0 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 28 73  e3_stmt_status(s
3f4d0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
3f4e0 74 20 6f 70 2c 69 6e 74 20 72 65 73 65 74 46 6c  t op,int resetFl
3f4f0 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  g);../*.** CAPI3
3f500 52 45 46 3a 20 53 74 61 74 75 73 20 50 61 72 61  REF: Status Para
3f510 6d 65 74 65 72 73 20 66 6f 72 20 70 72 65 70 61  meters for prepa
3f520 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20 7b  red statements {
3f530 48 31 37 35 37 30 7d 20 3c 48 31 37 35 35 30 3e  H17570} <H17550>
3f540 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c  .** EXPERIMENTAL
3f550 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 70 72 65  .**.** These pre
3f560 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 73  processor macros
3f570 20 64 65 66 69 6e 65 20 69 6e 74 65 67 65 72 20   define integer 
3f580 63 6f 64 65 73 20 74 68 61 74 20 6e 61 6d 65 20  codes that name 
3f590 63 6f 75 6e 74 65 72 0a 2a 2a 20 76 61 6c 75 65  counter.** value
3f5a0 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
3f5b0 68 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73  h the [sqlite3_s
3f5c0 74 6d 74 5f 73 74 61 74 75 73 28 29 5d 20 69 6e  tmt_status()] in
3f5d0 74 65 72 66 61 63 65 2e 0a 2a 2a 20 54 68 65 20  terface..** The 
3f5e0 6d 65 61 6e 69 6e 67 73 20 6f 66 20 74 68 65 20  meanings of the 
3f5f0 76 61 72 69 6f 75 73 20 63 6f 75 6e 74 65 72 73  various counters
3f600 20 61 72 65 20 61 73 20 66 6f 6c 6c 6f 77 73 3a   are as follows:
3f610 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c  .**.** <dl>.** <
3f620 64 74 3e 53 51 4c 49 54 45 5f 53 54 4d 54 53 54  dt>SQLITE_STMTST
3f630 41 54 55 53 5f 46 55 4c 4c 53 43 41 4e 5f 53 54  ATUS_FULLSCAN_ST
3f640 45 50 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  EP</dt>.** <dd>T
3f650 68 69 73 20 69 73 20 74 68 65 20 6e 75 6d 62 65  his is the numbe
3f660 72 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20  r of times that 
3f670 53 51 4c 69 74 65 20 68 61 73 20 73 74 65 70 70  SQLite has stepp
3f680 65 64 20 66 6f 72 77 61 72 64 20 69 6e 0a 2a 2a  ed forward in.**
3f690 20 61 20 74 61 62 6c 65 20 61 73 20 70 61 72 74   a table as part
3f6a0 20 6f 66 20 61 20 66 75 6c 6c 20 74 61 62 6c 65   of a full table
3f6b0 20 73 63 61 6e 2e 20 20 4c 61 72 67 65 20 6e 75   scan.  Large nu
3f6c0 6d 62 65 72 73 20 66 6f 72 20 74 68 69 73 20 63  mbers for this c
3f6d0 6f 75 6e 74 65 72 0a 2a 2a 20 6d 61 79 20 69 6e  ounter.** may in
3f6e0 64 69 63 61 74 65 20 6f 70 70 6f 72 74 75 6e 69  dicate opportuni
3f6f0 74 69 65 73 20 66 6f 72 20 70 65 72 66 6f 72 6d  ties for perform
3f700 61 6e 63 65 20 69 6d 70 72 6f 76 65 6d 65 6e 74  ance improvement
3f710 20 74 68 72 6f 75 67 68 20 0a 2a 2a 20 63 61 72   through .** car
3f720 65 66 75 6c 20 75 73 65 20 6f 66 20 69 6e 64 69  eful use of indi
3f730 63 65 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  ces.</dd>.**.** 
3f740 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 4d 54 53  <dt>SQLITE_STMTS
3f750 54 41 54 55 53 5f 53 4f 52 54 3c 2f 64 74 3e 0a  TATUS_SORT</dt>.
3f760 2a 2a 20 3c 64 64 3e 54 68 69 73 20 69 73 20 74  ** <dd>This is t
3f770 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 6f 72  he number of sor
3f780 74 20 6f 70 65 72 61 74 69 6f 6e 73 20 74 68 61  t operations tha
3f790 74 20 68 61 76 65 20 6f 63 63 75 72 72 65 64 2e  t have occurred.
3f7a0 0a 2a 2a 20 41 20 6e 6f 6e 2d 7a 65 72 6f 20 76  .** A non-zero v
3f7b0 61 6c 75 65 20 69 6e 20 74 68 69 73 20 63 6f 75  alue in this cou
3f7c0 6e 74 65 72 20 6d 61 79 20 69 6e 64 69 63 61 74  nter may indicat
3f7d0 65 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79  e an opportunity
3f7e0 20 74 6f 0a 2a 2a 20 69 6d 70 72 6f 76 65 6d 65   to.** improveme
3f7f0 6e 74 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74  nt performance t
3f800 68 72 6f 75 67 68 20 63 61 72 65 66 75 6c 20 75  hrough careful u
3f810 73 65 20 6f 66 20 69 6e 64 69 63 65 73 2e 3c 2f  se of indices.</
3f820 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a  dd>.**.** </dl>.
3f830 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
3f840 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 46 55 4c  E_STMTSTATUS_FUL
3f850 4c 53 43 41 4e 5f 53 54 45 50 20 20 20 20 20 31  LSCAN_STEP     1
3f860 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
3f870 53 54 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 20  STMTSTATUS_SORT 
3f880 20 20 20 20 20 20 20 20 20 20 20 20 20 32 0a 0a               2..
3f890 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
3f8a0 43 75 73 74 6f 6d 20 50 61 67 65 20 43 61 63 68  Custom Page Cach
3f8b0 65 20 4f 62 6a 65 63 74 0a 2a 2a 20 45 58 50 45  e Object.** EXPE
3f8c0 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54  RIMENTAL.**.** T
3f8d0 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68  he sqlite3_pcach
3f8e0 65 20 74 79 70 65 20 69 73 20 6f 70 61 71 75 65  e type is opaque
3f8f0 2e 20 20 49 74 20 69 73 20 69 6d 70 6c 65 6d 65  .  It is impleme
3f900 6e 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 70  nted by.** the p
3f910 6c 75 67 67 61 62 6c 65 20 6d 6f 64 75 6c 65 2e  luggable module.
3f920 20 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f 72    The SQLite cor
3f930 65 20 68 61 73 20 6e 6f 20 6b 6e 6f 77 6c 65 64  e has no knowled
3f940 67 65 20 6f 66 0a 2a 2a 20 69 74 73 20 73 69 7a  ge of.** its siz
3f950 65 20 6f 72 20 69 6e 74 65 72 6e 61 6c 20 73 74  e or internal st
3f960 72 75 63 74 75 72 65 20 61 6e 64 20 6e 65 76 65  ructure and neve
3f970 72 20 64 65 61 6c 73 20 77 69 74 68 20 74 68 65  r deals with the
3f980 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63  .** sqlite3_pcac
3f990 68 65 20 6f 62 6a 65 63 74 20 65 78 63 65 70 74  he object except
3f9a0 20 62 79 20 68 6f 6c 64 69 6e 67 20 61 6e 64 20   by holding and 
3f9b0 70 61 73 73 69 6e 67 20 70 6f 69 6e 74 65 72 73  passing pointers
3f9c0 0a 2a 2a 20 74 6f 20 74 68 65 20 6f 62 6a 65 63  .** to the objec
3f9d0 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 5b 73 71  t..**.** See [sq
3f9e0 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74  lite3_pcache_met
3f9f0 68 6f 64 73 5d 20 66 6f 72 20 61 64 64 69 74 69  hods] for additi
3fa00 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
3fa10 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
3fa20 75 63 74 20 73 71 6c 69 74 65 33 5f 70 63 61 63  uct sqlite3_pcac
3fa30 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68  he sqlite3_pcach
3fa40 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  e;../*.** CAPI3R
3fa50 45 46 3a 20 41 70 70 6c 69 63 61 74 69 6f 6e 20  EF: Application 
3fa60 44 65 66 69 6e 65 64 20 50 61 67 65 20 43 61 63  Defined Page Cac
3fa70 68 65 2e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a  he..** KEYWORDS:
3fa80 20 7b 70 61 67 65 20 63 61 63 68 65 7d 0a 2a 2a   {page cache}.**
3fa90 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a   EXPERIMENTAL.**
3faa0 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33  .** The [sqlite3
3fab0 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45  _config]([SQLITE
3fac0 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 5d 2c  _CONFIG_PCACHE],
3fad0 20 2e 2e 2e 29 20 69 6e 74 65 72 66 61 63 65 20   ...) interface 
3fae0 63 61 6e 0a 2a 2a 20 72 65 67 69 73 74 65 72 20  can.** register 
3faf0 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 20 70  an alternative p
3fb00 61 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d  age cache implem
3fb10 65 6e 74 61 74 69 6f 6e 20 62 79 20 70 61 73 73  entation by pass
3fb20 69 6e 67 20 69 6e 20 61 6e 20 0a 2a 2a 20 69 6e  ing in an .** in
3fb30 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 73 71  stance of the sq
3fb40 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74  lite3_pcache_met
3fb50 68 6f 64 73 20 73 74 72 75 63 74 75 72 65 2e 20  hods structure. 
3fb60 54 68 65 20 6d 61 6a 6f 72 69 74 79 20 6f 66 20  The majority of 
3fb70 74 68 65 20 0a 2a 2a 20 68 65 61 70 20 6d 65 6d  the .** heap mem
3fb80 6f 72 79 20 75 73 65 64 20 62 79 20 53 51 4c 69  ory used by SQLi
3fb90 74 65 20 69 73 20 75 73 65 64 20 62 79 20 74 68  te is used by th
3fba0 65 20 70 61 67 65 20 63 61 63 68 65 20 74 6f 20  e page cache to 
3fbb0 63 61 63 68 65 20 64 61 74 61 20 72 65 61 64 20  cache data read 
3fbc0 0a 2a 2a 20 66 72 6f 6d 2c 20 6f 72 20 72 65 61  .** from, or rea
3fbd0 64 79 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e  dy to be written
3fbe0 20 74 6f 2c 20 74 68 65 20 64 61 74 61 62 61 73   to, the databas
3fbf0 65 20 66 69 6c 65 2e 20 42 79 20 69 6d 70 6c 65  e file. By imple
3fc00 6d 65 6e 74 69 6e 67 20 61 20 0a 2a 2a 20 63 75  menting a .** cu
3fc10 73 74 6f 6d 20 70 61 67 65 20 63 61 63 68 65 20  stom page cache 
3fc20 75 73 69 6e 67 20 74 68 69 73 20 41 50 49 2c 20  using this API, 
3fc30 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 63  an application c
3fc40 61 6e 20 63 6f 6e 74 72 6f 6c 20 6d 6f 72 65 20  an control more 
3fc50 0a 2a 2a 20 70 72 65 63 69 73 65 6c 79 20 74 68  .** precisely th
3fc60 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f  e amount of memo
3fc70 72 79 20 63 6f 6e 73 75 6d 65 64 20 62 79 20 53  ry consumed by S
3fc80 51 4c 69 74 65 2c 20 74 68 65 20 77 61 79 20 69  QLite, the way i
3fc90 6e 20 77 68 69 63 68 20 0a 2a 2a 20 74 68 61 74  n which .** that
3fca0 20 6d 65 6d 6f 72 79 20 69 73 20 61 6c 6c 6f 63   memory is alloc
3fcb0 61 74 65 64 20 61 6e 64 20 72 65 6c 65 61 73 65  ated and release
3fcc0 64 2c 20 61 6e 64 20 74 68 65 20 70 6f 6c 69 63  d, and the polic
3fcd0 69 65 73 20 75 73 65 64 20 74 6f 20 0a 2a 2a 20  ies used to .** 
3fce0 64 65 74 65 72 6d 69 6e 65 20 65 78 61 63 74 6c  determine exactl
3fcf0 79 20 77 68 69 63 68 20 70 61 72 74 73 20 6f 66  y which parts of
3fd00 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65   a database file
3fd10 20 61 72 65 20 63 61 63 68 65 64 20 61 6e 64 20   are cached and 
3fd20 66 6f 72 20 0a 2a 2a 20 68 6f 77 20 6c 6f 6e 67  for .** how long
3fd30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74  ..**.** The cont
3fd40 65 6e 74 73 20 6f 66 20 74 68 65 20 73 71 6c 69  ents of the sqli
3fd50 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f  te3_pcache_metho
3fd60 64 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65  ds structure are
3fd70 20 63 6f 70 69 65 64 20 74 6f 20 61 6e 0a 2a 2a   copied to an.**
3fd80 20 69 6e 74 65 72 6e 61 6c 20 62 75 66 66 65 72   internal buffer
3fd90 20 62 79 20 53 51 4c 69 74 65 20 77 69 74 68 69   by SQLite withi
3fda0 6e 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 5b 73  n the call to [s
3fdb0 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d 2e 20  qlite3_config]. 
3fdc0 20 48 65 6e 63 65 0a 2a 2a 20 74 68 65 20 61 70   Hence.** the ap
3fdd0 70 6c 69 63 61 74 69 6f 6e 20 6d 61 79 20 64 69  plication may di
3fde0 73 63 61 72 64 20 74 68 65 20 70 61 72 61 6d 65  scard the parame
3fdf0 74 65 72 20 61 66 74 65 72 20 74 68 65 20 63 61  ter after the ca
3fe00 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65  ll to.** [sqlite
3fe10 33 5f 63 6f 6e 66 69 67 28 29 5d 20 72 65 74 75  3_config()] retu
3fe20 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78  rns..**.** The x
3fe30 49 6e 69 74 28 29 20 6d 65 74 68 6f 64 20 69 73  Init() method is
3fe40 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 66 6f 72   called once for
3fe50 20 65 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b 73   each call to [s
3fe60 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
3fe70 65 28 29 5d 0a 2a 2a 20 28 75 73 75 61 6c 6c 79  e()].** (usually
3fe80 20 6f 6e 6c 79 20 6f 6e 63 65 20 64 75 72 69 6e   only once durin
3fe90 67 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f  g the lifetime o
3fea0 66 20 74 68 65 20 70 72 6f 63 65 73 73 29 2e 20  f the process). 
3feb0 49 74 20 69 73 20 70 61 73 73 65 64 0a 2a 2a 20  It is passed.** 
3fec0 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 73 71  a copy of the sq
3fed0 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74  lite3_pcache_met
3fee0 68 6f 64 73 2e 70 41 72 67 20 76 61 6c 75 65 2e  hods.pArg value.
3fef0 20 49 74 20 63 61 6e 20 62 65 20 75 73 65 64 20   It can be used 
3ff00 74 6f 20 73 65 74 0a 2a 2a 20 75 70 20 67 6c 6f  to set.** up glo
3ff10 62 61 6c 20 73 74 72 75 63 74 75 72 65 73 20 61  bal structures a
3ff20 6e 64 20 6d 75 74 65 78 65 73 20 72 65 71 75 69  nd mutexes requi
3ff30 72 65 64 20 62 79 20 74 68 65 20 63 75 73 74 6f  red by the custo
3ff40 6d 20 70 61 67 65 20 63 61 63 68 65 20 0a 2a 2a  m page cache .**
3ff50 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e   implementation.
3ff60 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 53 68 75   .**.** The xShu
3ff70 74 64 6f 77 6e 28 29 20 6d 65 74 68 6f 64 20 69  tdown() method i
3ff80 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69  s called from wi
3ff90 74 68 69 6e 20 5b 73 71 6c 69 74 65 33 5f 73 68  thin [sqlite3_sh
3ffa0 75 74 64 6f 77 6e 28 29 5d 2c 20 0a 2a 2a 20 69  utdown()], .** i
3ffb0 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  f the applicatio
3ffc0 6e 20 69 6e 76 6f 6b 65 73 20 74 68 69 73 20 41  n invokes this A
3ffd0 50 49 2e 20 49 74 20 63 61 6e 20 62 65 20 75 73  PI. It can be us
3ffe0 65 64 20 74 6f 20 63 6c 65 61 6e 20 75 70 20 0a  ed to clean up .
3fff0 2a 2a 20 61 6e 79 20 6f 75 74 73 74 61 6e 64 69  ** any outstandi
40000 6e 67 20 72 65 73 6f 75 72 63 65 73 20 62 65 66  ng resources bef
40010 6f 72 65 20 70 72 6f 63 65 73 73 20 73 68 75 74  ore process shut
40020 64 6f 77 6e 2c 20 69 66 20 72 65 71 75 69 72 65  down, if require
40030 64 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  d..**.** SQLite 
40040 68 6f 6c 64 73 20 61 20 5b 53 51 4c 49 54 45 5f  holds a [SQLITE_
40050 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 5d  MUTEX_RECURSIVE]
40060 20 6d 75 74 65 78 20 77 68 65 6e 20 69 74 20 69   mutex when it i
40070 6e 76 6f 6b 65 73 0a 2a 2a 20 74 68 65 20 78 49  nvokes.** the xI
40080 6e 69 74 20 6d 65 74 68 6f 64 2c 20 73 6f 20 74  nit method, so t
40090 68 65 20 78 49 6e 69 74 20 6d 65 74 68 6f 64 20  he xInit method 
400a0 6e 65 65 64 20 6e 6f 74 20 62 65 20 74 68 72 65  need not be thre
400b0 61 64 73 61 66 65 2e 20 20 54 68 65 0a 2a 2a 20  adsafe.  The.** 
400c0 78 53 68 75 74 64 6f 77 6e 20 6d 65 74 68 6f 64  xShutdown method
400d0 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20   is only called 
400e0 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 73 68  from [sqlite3_sh
400f0 75 74 64 6f 77 6e 28 29 5d 20 73 6f 20 69 74 20  utdown()] so it 
40100 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64  does.** not need
40110 20 74 6f 20 62 65 20 74 68 72 65 61 64 73 61 66   to be threadsaf
40120 65 20 65 69 74 68 65 72 2e 20 20 41 6c 6c 20 6f  e either.  All o
40130 74 68 65 72 20 6d 65 74 68 6f 64 73 20 6d 75 73  ther methods mus
40140 74 20 62 65 20 74 68 72 65 61 64 73 61 66 65 0a  t be threadsafe.
40150 2a 2a 20 69 6e 20 6d 75 6c 74 69 74 68 72 65 61  ** in multithrea
40160 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73  ded applications
40170 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77  ..**.** SQLite w
40180 69 6c 6c 20 6e 65 76 65 72 20 69 6e 76 6f 6b 65  ill never invoke
40190 20 78 49 6e 69 74 28 29 20 6d 6f 72 65 20 74 68   xInit() more th
401a0 61 6e 20 6f 6e 63 65 20 77 69 74 68 6f 75 74 20  an once without 
401b0 61 6e 20 69 6e 74 65 72 76 65 6e 69 6e 67 0a 2a  an intervening.*
401c0 2a 20 63 61 6c 6c 20 74 6f 20 78 53 68 75 74 64  * call to xShutd
401d0 6f 77 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  own()..**.** The
401e0 20 78 43 72 65 61 74 65 28 29 20 6d 65 74 68 6f   xCreate() metho
401f0 64 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6e  d is used to con
40200 73 74 72 75 63 74 20 61 20 6e 65 77 20 63 61 63  struct a new cac
40210 68 65 20 69 6e 73 74 61 6e 63 65 2e 20 20 53 51  he instance.  SQ
40220 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 74 79 70  Lite.** will typ
40230 69 63 61 6c 6c 79 20 63 72 65 61 74 65 20 6f 6e  ically create on
40240 65 20 63 61 63 68 65 20 69 6e 73 74 61 6e 63 65  e cache instance
40250 20 66 6f 72 20 65 61 63 68 20 6f 70 65 6e 20 64   for each open d
40260 61 74 61 62 61 73 65 20 66 69 6c 65 2c 0a 2a 2a  atabase file,.**
40270 20 74 68 6f 75 67 68 20 74 68 69 73 20 69 73 20   though this is 
40280 6e 6f 74 20 67 75 61 72 61 6e 74 65 65 64 2e 20  not guaranteed. 
40290 54 68 65 0a 2a 2a 20 66 69 72 73 74 20 70 61 72  The.** first par
402a0 61 6d 65 74 65 72 2c 20 73 7a 50 61 67 65 2c 20  ameter, szPage, 
402b0 69 73 20 74 68 65 20 73 69 7a 65 20 69 6e 20 62  is the size in b
402c0 79 74 65 73 20 6f 66 20 74 68 65 20 70 61 67 65  ytes of the page
402d0 73 20 74 68 61 74 20 6d 75 73 74 0a 2a 2a 20 62  s that must.** b
402e0 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74  e allocated by t
402f0 68 65 20 63 61 63 68 65 2e 20 20 73 7a 50 61 67  he cache.  szPag
40300 65 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 20  e will not be a 
40310 70 6f 77 65 72 20 6f 66 20 74 77 6f 2e 20 20 73  power of two.  s
40320 7a 50 61 67 65 0a 2a 2a 20 77 69 6c 6c 20 74 68  zPage.** will th
40330 65 20 70 61 67 65 20 73 69 7a 65 20 6f 66 20 74  e page size of t
40340 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
40350 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 20 63   that is to be c
40360 61 63 68 65 64 20 70 6c 75 73 20 61 6e 0a 2a 2a  ached plus an.**
40370 20 69 6e 63 72 65 6d 65 6e 74 20 28 68 65 72 65   increment (here
40380 20 63 61 6c 6c 65 64 20 22 52 22 29 20 6f 66 20   called "R") of 
40390 61 62 6f 75 74 20 31 30 30 20 6f 72 20 32 30 30  about 100 or 200
403a0 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75  .  SQLite will u
403b0 73 65 20 74 68 65 0a 2a 2a 20 65 78 74 72 61 20  se the.** extra 
403c0 52 20 62 79 74 65 73 20 6f 6e 20 65 61 63 68 20  R bytes on each 
403d0 70 61 67 65 20 74 6f 20 73 74 6f 72 65 20 6d 65  page to store me
403e0 74 61 64 61 74 61 20 61 62 6f 75 74 20 74 68 65  tadata about the
403f0 20 75 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 64   underlying.** d
40400 61 74 61 62 61 73 65 20 70 61 67 65 20 6f 6e 20  atabase page on 
40410 64 69 73 6b 2e 20 20 54 68 65 20 76 61 6c 75 65  disk.  The value
40420 20 6f 66 20 52 20 64 65 70 65 6e 64 73 0a 2a 2a   of R depends.**
40430 20 6f 6e 20 74 68 65 20 53 51 4c 69 74 65 20 76   on the SQLite v
40440 65 72 73 69 6f 6e 2c 20 74 68 65 20 74 61 72 67  ersion, the targ
40450 65 74 20 70 6c 61 74 66 6f 72 6d 2c 20 61 6e 64  et platform, and
40460 20 68 6f 77 20 53 51 4c 69 74 65 20 77 61 73 20   how SQLite was 
40470 63 6f 6d 70 69 6c 65 64 2e 0a 2a 2a 20 52 20 69  compiled..** R i
40480 73 20 63 6f 6e 73 74 61 6e 74 20 66 6f 72 20 61  s constant for a
40490 20 70 61 72 74 69 63 75 6c 61 72 20 62 75 69 6c   particular buil
404a0 64 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 54 68  d of SQLite.  Th
404b0 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
404c0 74 20 74 6f 0a 2a 2a 20 78 43 72 65 61 74 65 28  t to.** xCreate(
404d0 29 2c 20 62 50 75 72 67 65 61 62 6c 65 2c 20 69  ), bPurgeable, i
404e0 73 20 74 72 75 65 20 69 66 20 74 68 65 20 63 61  s true if the ca
404f0 63 68 65 20 62 65 69 6e 67 20 63 72 65 61 74 65  che being create
40500 64 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 75 73 65  d will.** be use
40510 64 20 74 6f 20 63 61 63 68 65 20 64 61 74 61 62  d to cache datab
40520 61 73 65 20 70 61 67 65 73 20 6f 66 20 61 20 66  ase pages of a f
40530 69 6c 65 20 73 74 6f 72 65 64 20 6f 6e 20 64 69  ile stored on di
40540 73 6b 2c 20 6f 72 0a 2a 2a 20 66 61 6c 73 65 20  sk, or.** false 
40550 69 66 20 69 74 20 69 73 20 75 73 65 64 20 66 6f  if it is used fo
40560 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64  r an in-memory d
40570 61 74 61 62 61 73 65 2e 20 54 68 65 20 63 61 63  atabase. The cac
40580 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
40590 6e 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 68 61  n.** does not ha
405a0 76 65 20 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e  ve to do anythin
405b0 67 20 73 70 65 63 69 61 6c 20 62 61 73 65 64 20  g special based 
405c0 77 69 74 68 20 74 68 65 20 76 61 6c 75 65 20 6f  with the value o
405d0 66 20 62 50 75 72 67 65 61 62 6c 65 3b 0a 2a 2a  f bPurgeable;.**
405e0 20 69 74 20 69 73 20 70 75 72 65 6c 79 20 61 64   it is purely ad
405f0 76 69 73 6f 72 79 2e 20 20 4f 6e 20 61 20 63 61  visory.  On a ca
40600 63 68 65 20 77 68 65 72 65 20 62 50 75 72 67 65  che where bPurge
40610 61 62 6c 65 20 69 73 20 66 61 6c 73 65 2c 20 53  able is false, S
40620 51 4c 69 74 65 20 77 69 6c 6c 0a 2a 2a 20 6e 65  QLite will.** ne
40630 76 65 72 20 69 6e 76 6f 6b 65 20 78 55 6e 70 69  ver invoke xUnpi
40640 6e 28 29 20 65 78 63 65 70 74 20 74 6f 20 64 65  n() except to de
40650 6c 69 62 65 72 61 74 65 6c 79 20 64 65 6c 65 74  liberately delet
40660 65 20 61 20 70 61 67 65 2e 0a 2a 2a 20 49 6e 20  e a page..** In 
40670 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 61 20 63  other words, a c
40680 61 63 68 65 20 63 72 65 61 74 65 64 20 77 69 74  ache created wit
40690 68 20 62 50 75 72 67 65 61 62 6c 65 20 73 65 74  h bPurgeable set
406a0 20 74 6f 20 66 61 6c 73 65 20 77 69 6c 6c 0a 2a   to false will.*
406b0 2a 20 6e 65 76 65 72 20 63 6f 6e 74 61 69 6e 20  * never contain 
406c0 61 6e 79 20 75 6e 70 69 6e 6e 65 64 20 70 61 67  any unpinned pag
406d0 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 43  es..**.** The xC
406e0 61 63 68 65 73 69 7a 65 28 29 20 6d 65 74 68 6f  achesize() metho
406f0 64 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 20  d may be called 
40700 61 74 20 61 6e 79 20 74 69 6d 65 20 62 79 20 53  at any time by S
40710 51 4c 69 74 65 20 74 6f 20 73 65 74 20 74 68 65  QLite to set the
40720 0a 2a 2a 20 73 75 67 67 65 73 74 65 64 20 6d 61  .** suggested ma
40730 78 69 6d 75 6d 20 63 61 63 68 65 2d 73 69 7a 65  ximum cache-size
40740 20 28 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65   (number of page
40750 73 20 73 74 6f 72 65 64 20 62 79 29 20 74 68 65  s stored by) the
40760 20 63 61 63 68 65 0a 2a 2a 20 69 6e 73 74 61 6e   cache.** instan
40770 63 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65  ce passed as the
40780 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e   first argument.
40790 20 54 68 69 73 20 69 73 20 74 68 65 20 76 61 6c   This is the val
407a0 75 65 20 63 6f 6e 66 69 67 75 72 65 64 20 75 73  ue configured us
407b0 69 6e 67 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74  ing.** the SQLit
407c0 65 20 22 5b 50 52 41 47 4d 41 20 63 61 63 68 65  e "[PRAGMA cache
407d0 5f 73 69 7a 65 5d 22 20 63 6f 6d 6d 61 6e 64 2e  _size]" command.
407e0 20 41 73 20 77 69 74 68 20 74 68 65 20 62 50 75   As with the bPu
407f0 72 67 65 61 62 6c 65 20 70 61 72 61 6d 65 74 65  rgeable paramete
40800 72 2c 0a 2a 2a 20 74 68 65 20 69 6d 70 6c 65 6d  r,.** the implem
40810 65 6e 74 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20  entation is not 
40820 72 65 71 75 69 72 65 64 20 74 6f 20 64 6f 20 61  required to do a
40830 6e 79 74 68 69 6e 67 20 77 69 74 68 20 74 68 69  nything with thi
40840 73 0a 2a 2a 20 76 61 6c 75 65 3b 20 69 74 20 69  s.** value; it i
40850 73 20 61 64 76 69 73 6f 72 79 20 6f 6e 6c 79 2e  s advisory only.
40860 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 50 61 67 65  .**.** The xPage
40870 63 6f 75 6e 74 28 29 20 6d 65 74 68 6f 64 20 73  count() method s
40880 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 68 65  hould return the
40890 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
408a0 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 73 74   currently.** st
408b0 6f 72 65 64 20 69 6e 20 74 68 65 20 63 61 63 68  ored in the cach
408c0 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 78 46  e..** .** The xF
408d0 65 74 63 68 28 29 20 6d 65 74 68 6f 64 20 69 73  etch() method is
408e0 20 75 73 65 64 20 74 6f 20 66 65 74 63 68 20 61   used to fetch a
408f0 20 70 61 67 65 20 61 6e 64 20 72 65 74 75 72 6e   page and return
40900 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74   a pointer to it
40910 2e 20 0a 2a 2a 20 41 20 27 70 61 67 65 27 2c 20  . .** A 'page', 
40920 69 6e 20 74 68 69 73 20 63 6f 6e 74 65 78 74 2c  in this context,
40930 20 69 73 20 61 20 62 75 66 66 65 72 20 6f 66 20   is a buffer of 
40940 73 7a 50 61 67 65 20 62 79 74 65 73 20 61 6c 69  szPage bytes ali
40950 67 6e 65 64 20 61 74 20 61 6e 0a 2a 2a 20 38 2d  gned at an.** 8-
40960 62 79 74 65 20 62 6f 75 6e 64 61 72 79 2e 20 54  byte boundary. T
40970 68 65 20 70 61 67 65 20 74 6f 20 62 65 20 66 65  he page to be fe
40980 74 63 68 65 64 20 69 73 20 64 65 74 65 72 6d 69  tched is determi
40990 6e 65 64 20 62 79 20 74 68 65 20 6b 65 79 2e 20  ned by the key. 
409a0 54 68 65 0a 2a 2a 20 6d 69 6d 69 6d 75 6d 20 6b  The.** mimimum k
409b0 65 79 20 76 61 6c 75 65 20 69 73 20 31 2e 20 41  ey value is 1. A
409c0 66 74 65 72 20 69 74 20 68 61 73 20 62 65 65 6e  fter it has been
409d0 20 72 65 74 72 69 65 76 65 64 20 75 73 69 6e 67   retrieved using
409e0 20 78 46 65 74 63 68 2c 20 74 68 65 20 70 61 67   xFetch, the pag
409f0 65 20 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65  e .** is conside
40a00 72 65 64 20 74 6f 20 62 65 20 22 70 69 6e 6e 65  red to be "pinne
40a10 64 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  d"..**.** If the
40a20 20 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20   requested page 
40a30 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68  is already in th
40a40 65 20 70 61 67 65 20 63 61 63 68 65 2c 20 74 68  e page cache, th
40a50 65 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68  en the page cach
40a60 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74  e.** implementat
40a70 69 6f 6e 20 6d 75 73 74 20 72 65 74 75 72 6e 20  ion must return 
40a80 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
40a90 20 70 61 67 65 20 62 75 66 66 65 72 20 77 69 74   page buffer wit
40aa0 68 20 69 74 73 20 63 6f 6e 74 65 6e 74 0a 2a 2a  h its content.**
40ab0 20 69 6e 74 61 63 74 2e 20 20 49 66 20 74 68 65   intact.  If the
40ac0 20 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20   requested page 
40ad0 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 69  is not already i
40ae0 6e 20 74 68 65 20 63 61 63 68 65 2c 20 74 68 65  n the cache, the
40af0 6e 20 74 68 65 0a 2a 2a 20 62 65 68 61 76 69 6f  n the.** behavio
40b00 72 20 6f 66 20 74 68 65 20 63 61 63 68 65 20 69  r of the cache i
40b10 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73  mplementation is
40b20 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74   determined by t
40b30 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 0a  he value of the.
40b40 2a 2a 20 63 72 65 61 74 65 46 6c 61 67 20 70 61  ** createFlag pa
40b50 72 61 6d 65 74 65 72 20 70 61 73 73 65 64 20 74  rameter passed t
40b60 6f 20 78 46 65 74 63 68 2c 20 61 63 63 6f 72 64  o xFetch, accord
40b70 69 6e 67 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f  ing to the follo
40b80 77 69 6e 67 20 74 61 62 6c 65 3a 0a 2a 2a 0a 2a  wing table:.**.*
40b90 2a 20 3c 74 61 62 6c 65 20 62 6f 72 64 65 72 3d  * <table border=
40ba0 31 20 77 69 64 74 68 3d 38 35 25 20 61 6c 69 67  1 width=85% alig
40bb0 6e 3d 63 65 6e 74 65 72 3e 0a 2a 2a 20 3c 74 72  n=center>.** <tr
40bc0 3e 3c 74 68 3e 20 63 72 65 61 74 65 46 6c 61 67  ><th> createFlag
40bd0 20 3c 74 68 3e 20 42 65 68 61 76 69 6f 75 72 20   <th> Behaviour 
40be0 77 68 65 6e 20 70 61 67 65 20 69 73 20 6e 6f 74  when page is not
40bf0 20 61 6c 72 65 61 64 79 20 69 6e 20 63 61 63 68   already in cach
40c00 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 30 20  e.** <tr><td> 0 
40c10 3c 74 64 3e 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f  <td> Do not allo
40c20 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 2e  cate a new page.
40c30 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a    Return NULL..*
40c40 2a 20 3c 74 72 3e 3c 74 64 3e 20 31 20 3c 74 64  * <tr><td> 1 <td
40c50 3e 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77  > Allocate a new
40c60 20 70 61 67 65 20 69 66 20 69 74 20 65 61 73 79   page if it easy
40c70 20 61 6e 64 20 63 6f 6e 76 65 6e 69 65 6e 74 20   and convenient 
40c80 74 6f 20 64 6f 20 73 6f 2e 0a 2a 2a 20 20 20 20  to do so..**    
40c90 20 20 20 20 20 20 20 20 20 20 20 20 20 4f 74 68               Oth
40ca0 65 72 77 69 73 65 20 72 65 74 75 72 6e 20 4e 55  erwise return NU
40cb0 4c 4c 2e 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20  LL..** <tr><td> 
40cc0 32 20 3c 74 64 3e 20 4d 61 6b 65 20 65 76 65 72  2 <td> Make ever
40cd0 79 20 65 66 66 6f 72 74 20 74 6f 20 61 6c 6c 6f  y effort to allo
40ce0 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 2e  cate a new page.
40cf0 20 20 4f 6e 6c 79 20 72 65 74 75 72 6e 0a 2a 2a    Only return.**
40d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40d10 20 4e 55 4c 4c 20 69 66 20 61 6c 6c 6f 63 61 74   NULL if allocat
40d20 69 6e 67 20 61 20 6e 65 77 20 70 61 67 65 20 69  ing a new page i
40d30 73 20 65 66 66 65 63 74 69 76 65 6c 79 20 69 6d  s effectively im
40d40 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a 20 3c 2f 74  possible..** </t
40d50 61 62 6c 65 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69  able>.**.** SQLi
40d60 74 65 20 77 69 6c 6c 20 6e 6f 72 6d 61 6c 6c 79  te will normally
40d70 20 69 6e 76 6f 6b 65 20 78 46 65 74 63 68 28 29   invoke xFetch()
40d80 20 77 69 74 68 20 61 20 63 72 65 61 74 65 46 6c   with a createFl
40d90 61 67 20 6f 66 20 30 20 6f 72 20 31 2e 20 20 49  ag of 0 or 1.  I
40da0 66 0a 2a 2a 20 61 20 63 61 6c 6c 20 74 6f 20 78  f.** a call to x
40db0 46 65 74 63 68 28 29 20 77 69 74 68 20 63 72 65  Fetch() with cre
40dc0 61 74 65 46 6c 61 67 3d 3d 31 20 72 65 74 75 72  ateFlag==1 retur
40dd0 6e 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 53 51  ns NULL, then SQ
40de0 4c 69 74 65 20 77 69 6c 6c 0a 2a 2a 20 61 74 74  Lite will.** att
40df0 65 6d 70 74 20 74 6f 20 75 6e 70 69 6e 20 6f 6e  empt to unpin on
40e00 65 20 6f 72 20 6d 6f 72 65 20 63 61 63 68 65 20  e or more cache 
40e10 70 61 67 65 73 20 62 79 20 73 70 69 6c 6c 69 6e  pages by spillin
40e20 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  g the content of
40e30 0a 2a 2a 20 70 69 6e 6e 65 64 20 70 61 67 65 73  .** pinned pages
40e40 20 74 6f 20 64 69 73 6b 20 61 6e 64 20 73 79 6e   to disk and syn
40e50 63 68 69 6e 67 20 74 68 65 20 6f 70 65 72 61 74  ching the operat
40e60 69 6e 67 20 73 79 73 74 65 6d 20 64 69 73 6b 20  ing system disk 
40e70 63 61 63 68 65 2e 20 41 66 74 65 72 0a 2a 2a 20  cache. After.** 
40e80 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 75 6e  attempting to un
40e90 70 69 6e 20 70 61 67 65 73 2c 20 74 68 65 20 78  pin pages, the x
40ea0 46 65 74 63 68 28 29 20 6d 65 74 68 6f 64 20 77  Fetch() method w
40eb0 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 20 61  ill be invoked a
40ec0 67 61 69 6e 20 77 69 74 68 0a 2a 2a 20 61 20 63  gain with.** a c
40ed0 72 65 61 74 65 46 6c 61 67 20 6f 66 20 32 2e 0a  reateFlag of 2..
40ee0 2a 2a 0a 2a 2a 20 78 55 6e 70 69 6e 28 29 20 69  **.** xUnpin() i
40ef0 73 20 63 61 6c 6c 65 64 20 62 79 20 53 51 4c 69  s called by SQLi
40f00 74 65 20 77 69 74 68 20 61 20 70 6f 69 6e 74 65  te with a pointe
40f10 72 20 74 6f 20 61 20 63 75 72 72 65 6e 74 6c 79  r to a currently
40f20 20 70 69 6e 6e 65 64 20 70 61 67 65 0a 2a 2a 20   pinned page.** 
40f30 61 73 20 69 74 73 20 73 65 63 6f 6e 64 20 61 72  as its second ar
40f40 67 75 6d 65 6e 74 2e 20 49 66 20 74 68 65 20 74  gument. If the t
40f50 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 2c 20  hird parameter, 
40f60 64 69 73 63 61 72 64 2c 20 69 73 20 6e 6f 6e 2d  discard, is non-
40f70 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 74 68  zero,.** then th
40f80 65 20 70 61 67 65 20 73 68 6f 75 6c 64 20 62 65  e page should be
40f90 20 65 76 69 63 74 65 64 20 66 72 6f 6d 20 74 68   evicted from th
40fa0 65 20 63 61 63 68 65 2e 20 49 6e 20 74 68 69 73  e cache. In this
40fb0 20 63 61 73 65 20 53 51 4c 69 74 65 20 0a 2a 2a   case SQLite .**
40fc0 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68   assumes that th
40fd0 65 20 6e 65 78 74 20 74 69 6d 65 20 74 68 65 20  e next time the 
40fe0 70 61 67 65 20 69 73 20 72 65 74 72 69 65 76 65  page is retrieve
40ff0 64 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65  d from the cache
41000 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 78 46   using.** the xF
41010 65 74 63 68 28 29 20 6d 65 74 68 6f 64 2c 20 69  etch() method, i
41020 74 20 77 69 6c 6c 20 62 65 20 7a 65 72 6f 65 64  t will be zeroed
41030 2e 20 49 66 20 74 68 65 20 64 69 73 63 61 72 64  . If the discard
41040 20 70 61 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a   parameter is.**
41050 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20   zero, then the 
41060 70 61 67 65 20 69 73 20 63 6f 6e 73 69 64 65 72  page is consider
41070 65 64 20 74 6f 20 62 65 20 75 6e 70 69 6e 6e 65  ed to be unpinne
41080 64 2e 20 54 68 65 20 63 61 63 68 65 20 69 6d 70  d. The cache imp
41090 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6d  lementation.** m
410a0 61 79 20 63 68 6f 6f 73 65 20 74 6f 20 65 76 69  ay choose to evi
410b0 63 74 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65  ct unpinned page
410c0 73 20 61 74 20 61 6e 79 20 74 69 6d 65 2e 0a 2a  s at any time..*
410d0 2a 0a 2a 2a 20 54 68 65 20 63 61 63 68 65 20 69  *.** The cache i
410e0 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74  s not required t
410f0 6f 20 70 65 72 66 6f 72 6d 20 61 6e 79 20 72 65  o perform any re
41100 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 69 6e 67  ference counting
41110 2e 20 41 20 73 69 6e 67 6c 65 20 0a 2a 2a 20 63  . A single .** c
41120 61 6c 6c 20 74 6f 20 78 55 6e 70 69 6e 28 29 20  all to xUnpin() 
41130 75 6e 70 69 6e 73 20 74 68 65 20 70 61 67 65 20  unpins the page 
41140 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68  regardless of th
41150 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 72 69 6f  e number of prio
41160 72 20 63 61 6c 6c 73 20 0a 2a 2a 20 74 6f 20 78  r calls .** to x
41170 46 65 74 63 68 28 29 2e 0a 2a 2a 0a 2a 2a 20 54  Fetch()..**.** T
41180 68 65 20 78 52 65 6b 65 79 28 29 20 6d 65 74 68  he xRekey() meth
41190 6f 64 20 69 73 20 75 73 65 64 20 74 6f 20 63 68  od is used to ch
411a0 61 6e 67 65 20 74 68 65 20 6b 65 79 20 76 61 6c  ange the key val
411b0 75 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ue associated wi
411c0 74 68 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 70  th the.** page p
411d0 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63  assed as the sec
411e0 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 66 72 6f  ond argument fro
411f0 6d 20 6f 6c 64 4b 65 79 20 74 6f 20 6e 65 77 4b  m oldKey to newK
41200 65 79 2e 20 49 66 20 74 68 65 20 63 61 63 68 65  ey. If the cache
41210 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 63  .** previously c
41220 6f 6e 74 61 69 6e 73 20 61 6e 20 65 6e 74 72 79  ontains an entry
41230 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
41240 20 6e 65 77 4b 65 79 2c 20 69 74 20 73 68 6f 75   newKey, it shou
41250 6c 64 20 62 65 0a 2a 2a 20 64 69 73 63 61 72 64  ld be.** discard
41260 65 64 2e 20 41 6e 79 20 70 72 69 6f 72 20 63 61  ed. Any prior ca
41270 63 68 65 20 65 6e 74 72 79 20 61 73 73 6f 63 69  che entry associ
41280 61 74 65 64 20 77 69 74 68 20 6e 65 77 4b 65 79  ated with newKey
41290 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 6e   is guaranteed n
412a0 6f 74 0a 2a 2a 20 74 6f 20 62 65 20 70 69 6e 6e  ot.** to be pinn
412b0 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 53  ed..**.** When S
412c0 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 65 20  QLite calls the 
412d0 78 54 72 75 6e 63 61 74 65 28 29 20 6d 65 74 68  xTruncate() meth
412e0 6f 64 2c 20 74 68 65 20 63 61 63 68 65 20 6d 75  od, the cache mu
412f0 73 74 20 64 69 73 63 61 72 64 20 61 6c 6c 0a 2a  st discard all.*
41300 2a 20 65 78 69 73 74 69 6e 67 20 63 61 63 68 65  * existing cache
41310 20 65 6e 74 72 69 65 73 20 77 69 74 68 20 70 61   entries with pa
41320 67 65 20 6e 75 6d 62 65 72 73 20 28 6b 65 79 73  ge numbers (keys
41330 29 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f  ) greater than o
41340 72 20 65 71 75 61 6c 0a 2a 2a 20 74 6f 20 74 68  r equal.** to th
41350 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69  e value of the i
41360 4c 69 6d 69 74 20 70 61 72 61 6d 65 74 65 72 20  Limit parameter 
41370 70 61 73 73 65 64 20 74 6f 20 78 54 72 75 6e 63  passed to xTrunc
41380 61 74 65 28 29 2e 20 49 66 20 61 6e 79 0a 2a 2a  ate(). If any.**
41390 20 6f 66 20 74 68 65 73 65 20 70 61 67 65 73 20   of these pages 
413a0 61 72 65 20 70 69 6e 6e 65 64 2c 20 74 68 65 79  are pinned, they
413b0 20 61 72 65 20 69 6d 70 6c 69 63 69 74 6c 79 20   are implicitly 
413c0 75 6e 70 69 6e 6e 65 64 2c 20 6d 65 61 6e 69 6e  unpinned, meanin
413d0 67 20 74 68 61 74 0a 2a 2a 20 74 68 65 79 20 63  g that.** they c
413e0 61 6e 20 62 65 20 73 61 66 65 6c 79 20 64 69 73  an be safely dis
413f0 63 61 72 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  carded..**.** Th
41400 65 20 78 44 65 73 74 72 6f 79 28 29 20 6d 65 74  e xDestroy() met
41410 68 6f 64 20 69 73 20 75 73 65 64 20 74 6f 20 64  hod is used to d
41420 65 6c 65 74 65 20 61 20 63 61 63 68 65 20 61 6c  elete a cache al
41430 6c 6f 63 61 74 65 64 20 62 79 20 78 43 72 65 61  located by xCrea
41440 74 65 28 29 2e 0a 2a 2a 20 41 6c 6c 20 72 65 73  te()..** All res
41450 6f 75 72 63 65 73 20 61 73 73 6f 63 69 61 74 65  ources associate
41460 64 20 77 69 74 68 20 74 68 65 20 73 70 65 63 69  d with the speci
41470 66 69 65 64 20 63 61 63 68 65 20 73 68 6f 75 6c  fied cache shoul
41480 64 20 62 65 20 66 72 65 65 64 2e 20 41 66 74 65  d be freed. Afte
41490 72 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 74 68 65  r.** calling the
414a0 20 78 44 65 73 74 72 6f 79 28 29 20 6d 65 74 68   xDestroy() meth
414b0 6f 64 2c 20 53 51 4c 69 74 65 20 63 6f 6e 73 69  od, SQLite consi
414c0 64 65 72 73 20 74 68 65 20 5b 73 71 6c 69 74 65  ders the [sqlite
414d0 33 5f 70 63 61 63 68 65 2a 5d 0a 2a 2a 20 68 61  3_pcache*].** ha
414e0 6e 64 6c 65 20 69 6e 76 61 6c 69 64 2c 20 61 6e  ndle invalid, an
414f0 64 20 77 69 6c 6c 20 6e 6f 74 20 75 73 65 20 69  d will not use i
41500 74 20 77 69 74 68 20 61 6e 79 20 6f 74 68 65 72  t with any other
41510 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f   sqlite3_pcache_
41520 6d 65 74 68 6f 64 73 0a 2a 2a 20 66 75 6e 63 74  methods.** funct
41530 69 6f 6e 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  ions..*/.typedef
41540 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
41550 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 73  pcache_methods s
41560 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65  qlite3_pcache_me
41570 74 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 73 71  thods;.struct sq
41580 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74  lite3_pcache_met
41590 68 6f 64 73 20 7b 0a 20 20 76 6f 69 64 20 2a 70  hods {.  void *p
415a0 41 72 67 3b 0a 20 20 69 6e 74 20 28 2a 78 49 6e  Arg;.  int (*xIn
415b0 69 74 29 28 76 6f 69 64 2a 29 3b 0a 20 20 76 6f  it)(void*);.  vo
415c0 69 64 20 28 2a 78 53 68 75 74 64 6f 77 6e 29 28  id (*xShutdown)(
415d0 76 6f 69 64 2a 29 3b 0a 20 20 73 71 6c 69 74 65  void*);.  sqlite
415e0 33 5f 70 63 61 63 68 65 20 2a 28 2a 78 43 72 65  3_pcache *(*xCre
415f0 61 74 65 29 28 69 6e 74 20 73 7a 50 61 67 65 2c  ate)(int szPage,
41600 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 29   int bPurgeable)
41610 3b 0a 20 20 76 6f 69 64 20 28 2a 78 43 61 63 68  ;.  void (*xCach
41620 65 73 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 70  esize)(sqlite3_p
41630 63 61 63 68 65 2a 2c 20 69 6e 74 20 6e 43 61 63  cache*, int nCac
41640 68 65 73 69 7a 65 29 3b 0a 20 20 69 6e 74 20 28  hesize);.  int (
41650 2a 78 50 61 67 65 63 6f 75 6e 74 29 28 73 71 6c  *xPagecount)(sql
41660 69 74 65 33 5f 70 63 61 63 68 65 2a 29 3b 0a 20  ite3_pcache*);. 
41670 20 76 6f 69 64 20 2a 28 2a 78 46 65 74 63 68 29   void *(*xFetch)
41680 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a  (sqlite3_pcache*
41690 2c 20 75 6e 73 69 67 6e 65 64 20 6b 65 79 2c 20  , unsigned key, 
416a0 69 6e 74 20 63 72 65 61 74 65 46 6c 61 67 29 3b  int createFlag);
416b0 0a 20 20 76 6f 69 64 20 28 2a 78 55 6e 70 69 6e  .  void (*xUnpin
416c0 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  )(sqlite3_pcache
416d0 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20 64 69  *, void*, int di
416e0 73 63 61 72 64 29 3b 0a 20 20 76 6f 69 64 20 28  scard);.  void (
416f0 2a 78 52 65 6b 65 79 29 28 73 71 6c 69 74 65 33  *xRekey)(sqlite3
41700 5f 70 63 61 63 68 65 2a 2c 20 76 6f 69 64 2a 2c  _pcache*, void*,
41710 20 75 6e 73 69 67 6e 65 64 20 6f 6c 64 4b 65 79   unsigned oldKey
41720 2c 20 75 6e 73 69 67 6e 65 64 20 6e 65 77 4b 65  , unsigned newKe
41730 79 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 54 72  y);.  void (*xTr
41740 75 6e 63 61 74 65 29 28 73 71 6c 69 74 65 33 5f  uncate)(sqlite3_
41750 70 63 61 63 68 65 2a 2c 20 75 6e 73 69 67 6e 65  pcache*, unsigne
41760 64 20 69 4c 69 6d 69 74 29 3b 0a 20 20 76 6f 69  d iLimit);.  voi
41770 64 20 28 2a 78 44 65 73 74 72 6f 79 29 28 73 71  d (*xDestroy)(sq
41780 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 29 3b 0a  lite3_pcache*);.
41790 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  };../*.** CAPI3R
417a0 45 46 3a 20 4f 6e 6c 69 6e 65 20 42 61 63 6b 75  EF: Online Backu
417b0 70 20 4f 62 6a 65 63 74 0a 2a 2a 20 45 58 50 45  p Object.** EXPE
417c0 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54  RIMENTAL.**.** T
417d0 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  he sqlite3_backu
417e0 70 20 6f 62 6a 65 63 74 20 72 65 63 6f 72 64 73  p object records
417f0 20 73 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69   state informati
41800 6f 6e 20 61 62 6f 75 74 20 61 6e 20 6f 6e 67 6f  on about an ongo
41810 69 6e 67 0a 2a 2a 20 6f 6e 6c 69 6e 65 20 62 61  ing.** online ba
41820 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20  ckup operation. 
41830 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63   The sqlite3_bac
41840 6b 75 70 20 6f 62 6a 65 63 74 20 69 73 20 63 72  kup object is cr
41850 65 61 74 65 64 20 62 79 0a 2a 2a 20 61 20 63 61  eated by.** a ca
41860 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62  ll to [sqlite3_b
41870 61 63 6b 75 70 5f 69 6e 69 74 28 29 5d 20 61 6e  ackup_init()] an
41880 64 20 69 73 20 64 65 73 74 72 6f 79 65 64 20 62  d is destroyed b
41890 79 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b  y a call to.** [
418a0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66  sqlite3_backup_f
418b0 69 6e 69 73 68 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  inish()]..**.** 
418c0 53 65 65 20 41 6c 73 6f 3a 20 5b 55 73 69 6e 67  See Also: [Using
418d0 20 74 68 65 20 53 51 4c 69 74 65 20 4f 6e 6c 69   the SQLite Onli
418e0 6e 65 20 42 61 63 6b 75 70 20 41 50 49 5d 0a 2a  ne Backup API].*
418f0 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
41900 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20   sqlite3_backup 
41910 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 3b 0a  sqlite3_backup;.
41920 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
41930 20 4f 6e 6c 69 6e 65 20 42 61 63 6b 75 70 20 41   Online Backup A
41940 50 49 2e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  PI..** EXPERIMEN
41950 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41  TAL.**.** This A
41960 50 49 20 69 73 20 75 73 65 64 20 74 6f 20 6f 76  PI is used to ov
41970 65 72 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74  erwrite the cont
41980 65 6e 74 73 20 6f 66 20 6f 6e 65 20 64 61 74 61  ents of one data
41990 62 61 73 65 20 77 69 74 68 20 74 68 61 74 0a 2a  base with that.*
419a0 2a 20 6f 66 20 61 6e 6f 74 68 65 72 2e 20 49 74  * of another. It
419b0 20 69 73 20 75 73 65 66 75 6c 20 65 69 74 68 65   is useful eithe
419c0 72 20 66 6f 72 20 63 72 65 61 74 69 6e 67 20 62  r for creating b
419d0 61 63 6b 75 70 73 20 6f 66 20 64 61 74 61 62 61  ackups of databa
419e0 73 65 73 20 6f 72 0a 2a 2a 20 66 6f 72 20 63 6f  ses or.** for co
419f0 70 79 69 6e 67 20 69 6e 2d 6d 65 6d 6f 72 79 20  pying in-memory 
41a00 64 61 74 61 62 61 73 65 73 20 74 6f 20 6f 72 20  databases to or 
41a10 66 72 6f 6d 20 70 65 72 73 69 73 74 65 6e 74 20  from persistent 
41a20 66 69 6c 65 73 2e 20 0a 2a 2a 0a 2a 2a 20 53 65  files. .**.** Se
41a30 65 20 41 6c 73 6f 3a 20 5b 55 73 69 6e 67 20 74  e Also: [Using t
41a40 68 65 20 53 51 4c 69 74 65 20 4f 6e 6c 69 6e 65  he SQLite Online
41a50 20 42 61 63 6b 75 70 20 41 50 49 5d 0a 2a 2a 0a   Backup API].**.
41a60 2a 2a 20 45 78 63 6c 75 73 69 76 65 20 61 63 63  ** Exclusive acc
41a70 65 73 73 20 69 73 20 72 65 71 75 69 72 65 64 20  ess is required 
41a80 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69  to the destinati
41a90 6f 6e 20 64 61 74 61 62 61 73 65 20 66 6f 72 20  on database for 
41aa0 74 68 65 20 0a 2a 2a 20 64 75 72 61 74 69 6f 6e  the .** duration
41ab0 20 6f 66 20 74 68 65 20 6f 70 65 72 61 74 69 6f   of the operatio
41ac0 6e 2e 20 48 6f 77 65 76 65 72 20 74 68 65 20 73  n. However the s
41ad0 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 69  ource database i
41ae0 73 20 6f 6e 6c 79 0a 2a 2a 20 72 65 61 64 2d 6c  s only.** read-l
41af0 6f 63 6b 65 64 20 77 68 69 6c 65 20 69 74 20 69  ocked while it i
41b00 73 20 61 63 74 75 61 6c 6c 79 20 62 65 69 6e 67  s actually being
41b10 20 72 65 61 64 2c 20 69 74 20 69 73 20 6e 6f 74   read, it is not
41b20 20 6c 6f 63 6b 65 64 0a 2a 2a 20 63 6f 6e 74 69   locked.** conti
41b30 6e 75 6f 75 73 6c 79 20 66 6f 72 20 74 68 65 20  nuously for the 
41b40 65 6e 74 69 72 65 20 6f 70 65 72 61 74 69 6f 6e  entire operation
41b50 2e 20 54 68 75 73 2c 20 74 68 65 20 62 61 63 6b  . Thus, the back
41b60 75 70 20 6d 61 79 20 62 65 0a 2a 2a 20 70 65 72  up may be.** per
41b70 66 6f 72 6d 65 64 20 6f 6e 20 61 20 6c 69 76 65  formed on a live
41b80 20 64 61 74 61 62 61 73 65 20 77 69 74 68 6f 75   database withou
41b90 74 20 70 72 65 76 65 6e 74 69 6e 67 20 6f 74 68  t preventing oth
41ba0 65 72 20 75 73 65 72 73 20 66 72 6f 6d 0a 2a 2a  er users from.**
41bb0 20 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20   writing to the 
41bc0 64 61 74 61 62 61 73 65 20 66 6f 72 20 61 6e 20  database for an 
41bd0 65 78 74 65 6e 64 65 64 20 70 65 72 69 6f 64 20  extended period 
41be0 6f 66 20 74 69 6d 65 2e 0a 2a 2a 20 0a 2a 2a 20  of time..** .** 
41bf0 54 6f 20 70 65 72 66 6f 72 6d 20 61 20 62 61 63  To perform a bac
41c00 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 3a 20 0a  kup operation: .
41c10 2a 2a 20 20 20 3c 6f 6c 3e 0a 2a 2a 20 20 20 20  **   <ol>.**    
41c20 20 3c 6c 69 3e 3c 62 3e 73 71 6c 69 74 65 33 5f   <li><b>sqlite3_
41c30 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 3c 2f 62  backup_init()</b
41c40 3e 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65  > is called once
41c50 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 74   to initialize t
41c60 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 62 61  he.**         ba
41c70 63 6b 75 70 2c 20 0a 2a 2a 20 20 20 20 20 3c 6c  ckup, .**     <l
41c80 69 3e 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63  i><b>sqlite3_bac
41c90 6b 75 70 5f 73 74 65 70 28 29 3c 2f 62 3e 20 69  kup_step()</b> i
41ca0 73 20 63 61 6c 6c 65 64 20 6f 6e 65 20 6f 72 20  s called one or 
41cb0 6d 6f 72 65 20 74 69 6d 65 73 20 74 6f 20 74 72  more times to tr
41cc0 61 6e 73 66 65 72 20 0a 2a 2a 20 20 20 20 20 20  ansfer .**      
41cd0 20 20 20 74 68 65 20 64 61 74 61 20 62 65 74 77     the data betw
41ce0 65 65 6e 20 74 68 65 20 74 77 6f 20 64 61 74 61  een the two data
41cf0 62 61 73 65 73 2c 20 61 6e 64 20 66 69 6e 61 6c  bases, and final
41d00 6c 79 0a 2a 2a 20 20 20 20 20 3c 6c 69 3e 3c 62  ly.**     <li><b
41d10 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f  >sqlite3_backup_
41d20 66 69 6e 69 73 68 28 29 3c 2f 62 3e 20 69 73 20  finish()</b> is 
41d30 63 61 6c 6c 65 64 20 74 6f 20 72 65 6c 65 61 73  called to releas
41d40 65 20 61 6c 6c 20 72 65 73 6f 75 72 63 65 73 20  e all resources 
41d50 0a 2a 2a 20 20 20 20 20 20 20 20 20 61 73 73 6f  .**         asso
41d60 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
41d70 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e  backup operation
41d80 2e 20 0a 2a 2a 20 20 20 3c 2f 6f 6c 3e 0a 2a 2a  . .**   </ol>.**
41d90 20 54 68 65 72 65 20 73 68 6f 75 6c 64 20 62 65   There should be
41da0 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 63 61 6c   exactly one cal
41db0 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63  l to sqlite3_bac
41dc0 6b 75 70 5f 66 69 6e 69 73 68 28 29 20 66 6f 72  kup_finish() for
41dd0 20 65 61 63 68 0a 2a 2a 20 73 75 63 63 65 73 73   each.** success
41de0 66 75 6c 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  ful call to sqli
41df0 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28  te3_backup_init(
41e00 29 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 6c 69  )..**.** <b>sqli
41e10 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28  te3_backup_init(
41e20 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  )</b>.**.** The 
41e30 66 69 72 73 74 20 74 77 6f 20 61 72 67 75 6d 65  first two argume
41e40 6e 74 73 20 70 61 73 73 65 64 20 74 6f 20 5b 73  nts passed to [s
41e50 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e  qlite3_backup_in
41e60 69 74 28 29 5d 20 61 72 65 20 74 68 65 20 64 61  it()] are the da
41e70 74 61 62 61 73 65 0a 2a 2a 20 68 61 6e 64 6c 65  tabase.** handle
41e80 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
41e90 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
41ea0 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 68   database and th
41eb0 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  e database name 
41ec0 0a 2a 2a 20 75 73 65 64 20 74 6f 20 61 74 74 61  .** used to atta
41ed0 63 68 20 74 68 65 20 64 65 73 74 69 6e 61 74 69  ch the destinati
41ee0 6f 6e 20 64 61 74 61 62 61 73 65 20 74 6f 20 74  on database to t
41ef0 68 65 20 68 61 6e 64 6c 65 2e 20 54 68 65 20 64  he handle. The d
41f00 61 74 61 62 61 73 65 20 6e 61 6d 65 0a 2a 2a 20  atabase name.** 
41f10 69 73 20 22 6d 61 69 6e 22 20 66 6f 72 20 74 68  is "main" for th
41f20 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2c  e main database,
41f30 20 22 74 65 6d 70 22 20 66 6f 72 20 74 68 65 20   "temp" for the 
41f40 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61  temporary databa
41f50 73 65 2c 20 6f 72 0a 2a 2a 20 74 68 65 20 6e 61  se, or.** the na
41f60 6d 65 20 73 70 65 63 69 66 69 65 64 20 61 73 20  me specified as 
41f70 70 61 72 74 20 6f 66 20 74 68 65 20 5b 41 54 54  part of the [ATT
41f80 41 43 48 5d 20 73 74 61 74 65 6d 65 6e 74 20 69  ACH] statement i
41f90 66 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  f the destinatio
41fa0 6e 20 69 73 0a 2a 2a 20 61 6e 20 61 74 74 61 63  n is.** an attac
41fb0 68 65 64 20 64 61 74 61 62 61 73 65 2e 20 54 68  hed database. Th
41fc0 65 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72  e third and four
41fd0 74 68 20 61 72 67 75 6d 65 6e 74 73 20 70 61 73  th arguments pas
41fe0 73 65 64 20 74 6f 20 0a 2a 2a 20 73 71 6c 69 74  sed to .** sqlit
41ff0 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29  e3_backup_init()
42000 20 69 64 65 6e 74 69 66 79 20 74 68 65 20 5b 64   identify the [d
42010 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
42020 6f 6e 5d 0a 2a 2a 20 61 6e 64 20 64 61 74 61 62  on].** and datab
42030 61 73 65 20 6e 61 6d 65 20 75 73 65 64 0a 2a 2a  ase name used.**
42040 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 73   to access the s
42050 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 2e 20  ource database. 
42060 54 68 65 20 76 61 6c 75 65 73 20 70 61 73 73 65  The values passe
42070 64 20 66 6f 72 20 74 68 65 20 73 6f 75 72 63 65  d for the source
42080 20 61 6e 64 20 0a 2a 2a 20 64 65 73 74 69 6e 61   and .** destina
42090 74 69 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63  tion [database c
420a0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 70 61 72 61 6d  onnection] param
420b0 65 74 65 72 73 20 6d 75 73 74 20 6e 6f 74 20 62  eters must not b
420c0 65 20 74 68 65 20 73 61 6d 65 2e 0a 2a 2a 0a 2a  e the same..**.*
420d0 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  * If an error oc
420e0 63 75 72 73 20 77 69 74 68 69 6e 20 73 71 6c 69  curs within sqli
420f0 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28  te3_backup_init(
42100 29 2c 20 74 68 65 6e 20 4e 55 4c 4c 20 69 73 20  ), then NULL is 
42110 72 65 74 75 72 6e 65 64 0a 2a 2a 20 61 6e 64 20  returned.** and 
42120 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e  an error code an
42130 64 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  d error message 
42140 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65  written into the
42150 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
42160 63 74 69 6f 6e 5d 20 0a 2a 2a 20 70 61 73 73 65  ction] .** passe
42170 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61  d as the first a
42180 72 67 75 6d 65 6e 74 2e 20 54 68 65 79 20 6d 61  rgument. They ma
42190 79 20 62 65 20 72 65 74 72 69 65 76 65 64 20 75  y be retrieved u
421a0 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 73 71 6c  sing the.** [sql
421b0 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 2c  ite3_errcode()],
421c0 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67   [sqlite3_errmsg
421d0 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65  ()], and [sqlite
421e0 33 5f 65 72 72 6d 73 67 31 36 28 29 5d 20 66 75  3_errmsg16()] fu
421f0 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20 4f 74 68 65  nctions..** Othe
42200 72 77 69 73 65 2c 20 69 66 20 73 75 63 63 65 73  rwise, if succes
42210 73 66 75 6c 2c 20 61 20 70 6f 69 6e 74 65 72 20  sful, a pointer 
42220 74 6f 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 62  to an [sqlite3_b
42230 61 63 6b 75 70 5d 20 6f 62 6a 65 63 74 20 69 73  ackup] object is
42240 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 54 68  .** returned. Th
42250 69 73 20 70 6f 69 6e 74 65 72 20 6d 61 79 20 62  is pointer may b
42260 65 20 75 73 65 64 20 77 69 74 68 20 74 68 65 20  e used with the 
42270 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73  sqlite3_backup_s
42280 74 65 70 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c  tep() and.** sql
42290 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69  ite3_backup_fini
422a0 73 68 28 29 20 66 75 6e 63 74 69 6f 6e 73 20 74  sh() functions t
422b0 6f 20 70 65 72 66 6f 72 6d 20 74 68 65 20 73 70  o perform the sp
422c0 65 63 69 66 69 65 64 20 62 61 63 6b 75 70 20 0a  ecified backup .
422d0 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a  ** operation..**
422e0 0a 2a 2a 20 3c 62 3e 73 71 6c 69 74 65 33 5f 62  .** <b>sqlite3_b
422f0 61 63 6b 75 70 5f 73 74 65 70 28 29 3c 2f 62 3e  ackup_step()</b>
42300 0a 2a 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20  .**.** Function 
42310 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f  [sqlite3_backup_
42320 73 74 65 70 28 29 5d 20 69 73 20 75 73 65 64 20  step()] is used 
42330 74 6f 20 63 6f 70 79 20 75 70 20 74 6f 20 6e 50  to copy up to nP
42340 61 67 65 20 70 61 67 65 73 20 62 65 74 77 65 65  age pages betwee
42350 6e 20 0a 2a 2a 20 74 68 65 20 73 6f 75 72 63 65  n .** the source
42360 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69 6f 6e   and destination
42370 20 64 61 74 61 62 61 73 65 73 2c 20 77 68 65 72   databases, wher
42380 65 20 6e 50 61 67 65 20 69 73 20 74 68 65 20 76  e nPage is the v
42390 61 6c 75 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20  alue of the .** 
423a0 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72  second parameter
423b0 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74   passed to sqlit
423c0 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29  e3_backup_step()
423d0 2e 20 49 66 20 6e 50 61 67 65 20 69 73 20 61 20  . If nPage is a 
423e0 6e 65 67 61 74 69 76 65 0a 2a 2a 20 76 61 6c 75  negative.** valu
423f0 65 2c 20 61 6c 6c 20 72 65 6d 61 69 6e 69 6e 67  e, all remaining
42400 20 73 6f 75 72 63 65 20 70 61 67 65 73 20 61 72   source pages ar
42410 65 20 63 6f 70 69 65 64 2e 20 49 66 20 74 68 65  e copied. If the
42420 20 72 65 71 75 69 72 65 64 20 70 61 67 65 73 20   required pages 
42430 61 72 65 20 0a 2a 2a 20 73 75 63 63 65 73 66 75  are .** succesfu
42440 6c 6c 79 20 63 6f 70 69 65 64 2c 20 62 75 74 20  lly copied, but 
42450 74 68 65 72 65 20 61 72 65 20 73 74 69 6c 6c 20  there are still 
42460 6d 6f 72 65 20 70 61 67 65 73 20 74 6f 20 63 6f  more pages to co
42470 70 79 20 62 65 66 6f 72 65 20 74 68 65 20 0a 2a  py before the .*
42480 2a 20 62 61 63 6b 75 70 20 69 73 20 63 6f 6d 70  * backup is comp
42490 6c 65 74 65 2c 20 69 74 20 72 65 74 75 72 6e 73  lete, it returns
424a0 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 20 49 66   [SQLITE_OK]. If
424b0 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 65   no error occure
424c0 64 20 61 6e 64 20 74 68 65 72 65 20 0a 2a 2a 20  d and there .** 
424d0 61 72 65 20 6e 6f 20 6d 6f 72 65 20 70 61 67 65  are no more page
424e0 73 20 74 6f 20 63 6f 70 79 2c 20 74 68 65 6e 20  s to copy, then 
424f0 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 20 69 73  [SQLITE_DONE] is
42500 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e   returned. If an
42510 20 65 72 72 6f 72 20 0a 2a 2a 20 6f 63 63 75 72   error .** occur
42520 73 2c 20 74 68 65 6e 20 61 6e 20 53 51 4c 69 74  s, then an SQLit
42530 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  e error code is 
42540 72 65 74 75 72 6e 65 64 2e 20 41 73 20 77 65 6c  returned. As wel
42550 6c 20 61 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d  l as [SQLITE_OK]
42560 20 61 6e 64 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f   and.** [SQLITE_
42570 44 4f 4e 45 5d 2c 20 61 20 63 61 6c 6c 20 74 6f  DONE], a call to
42580 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
42590 73 74 65 70 28 29 20 6d 61 79 20 72 65 74 75 72  step() may retur
425a0 6e 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e  n [SQLITE_READON
425b0 4c 59 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  LY],.** [SQLITE_
425c0 4e 4f 4d 45 4d 5d 2c 20 5b 53 51 4c 49 54 45 5f  NOMEM], [SQLITE_
425d0 42 55 53 59 5d 2c 20 5b 53 51 4c 49 54 45 5f 4c  BUSY], [SQLITE_L
425e0 4f 43 4b 45 44 5d 2c 20 6f 72 20 61 6e 0a 2a 2a  OCKED], or an.**
425f0 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41   [SQLITE_IOERR_A
42600 43 43 45 53 53 20 7c 20 53 51 4c 49 54 45 5f 49  CCESS | SQLITE_I
42610 4f 45 52 52 5f 58 58 58 5d 20 65 78 74 65 6e 64  OERR_XXX] extend
42620 65 64 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a  ed error code..*
42630 2a 0a 2a 2a 20 41 73 20 77 65 6c 6c 20 61 73 20  *.** As well as 
42640 74 68 65 20 63 61 73 65 20 77 68 65 72 65 20 74  the case where t
42650 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64  he destination d
42660 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 61 73  atabase file was
42670 20 6f 70 65 6e 65 64 20 66 6f 72 0a 2a 2a 20 72   opened for.** r
42680 65 61 64 2d 6f 6e 6c 79 20 61 63 63 65 73 73 2c  ead-only access,
42690 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
426a0 73 74 65 70 28 29 20 6d 61 79 20 72 65 74 75 72  step() may retur
426b0 6e 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e  n [SQLITE_READON
426c0 4c 59 5d 20 69 66 0a 2a 2a 20 74 68 65 20 64 65  LY] if.** the de
426d0 73 74 69 6e 61 74 69 6f 6e 20 69 73 20 61 6e 20  stination is an 
426e0 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61  in-memory databa
426f0 73 65 20 77 69 74 68 20 61 20 64 69 66 66 65 72  se with a differ
42700 65 6e 74 20 70 61 67 65 20 73 69 7a 65 0a 2a 2a  ent page size.**
42710 20 66 72 6f 6d 20 74 68 65 20 73 6f 75 72 63 65   from the source
42720 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a   database..**.**
42730 20 49 66 20 73 71 6c 69 74 65 33 5f 62 61 63 6b   If sqlite3_back
42740 75 70 5f 73 74 65 70 28 29 20 63 61 6e 6e 6f 74  up_step() cannot
42750 20 6f 62 74 61 69 6e 20 61 20 72 65 71 75 69 72   obtain a requir
42760 65 64 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 6c  ed file-system l
42770 6f 63 6b 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65  ock, then.** the
42780 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68   [sqlite3_busy_h
42790 61 6e 64 6c 65 72 20 7c 20 62 75 73 79 2d 68 61  andler | busy-ha
427a0 6e 64 6c 65 72 20 66 75 6e 63 74 69 6f 6e 5d 0a  ndler function].
427b0 2a 2a 20 69 73 20 69 6e 76 6f 6b 65 64 20 28 69  ** is invoked (i
427c0 66 20 6f 6e 65 20 69 73 20 73 70 65 63 69 66 69  f one is specifi
427d0 65 64 29 2e 20 49 66 20 74 68 65 20 0a 2a 2a 20  ed). If the .** 
427e0 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 72 65 74  busy-handler ret
427f0 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 20 62 65  urns non-zero be
42800 66 6f 72 65 20 74 68 65 20 6c 6f 63 6b 20 69 73  fore the lock is
42810 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 65 6e   available, then
42820 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 42 55 53   .** [SQLITE_BUS
42830 59 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 74  Y] is returned t
42840 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 49 6e  o the caller. In
42850 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 63   this case the c
42860 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65  all to.** sqlite
42870 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20  3_backup_step() 
42880 63 61 6e 20 62 65 20 72 65 74 72 69 65 64 20 6c  can be retried l
42890 61 74 65 72 2e 20 49 66 20 74 68 65 20 73 6f 75  ater. If the sou
428a0 72 63 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65  rce.** [database
428b0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20   connection].** 
428c0 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 74 6f  is being used to
428d0 20 77 72 69 74 65 20 74 6f 20 74 68 65 20 73 6f   write to the so
428e0 75 72 63 65 20 64 61 74 61 62 61 73 65 20 77 68  urce database wh
428f0 65 6e 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  en sqlite3_backu
42900 70 5f 73 74 65 70 28 29 0a 2a 2a 20 69 73 20 63  p_step().** is c
42910 61 6c 6c 65 64 2c 20 74 68 65 6e 20 5b 53 51 4c  alled, then [SQL
42920 49 54 45 5f 4c 4f 43 4b 45 44 5d 20 69 73 20 72  ITE_LOCKED] is r
42930 65 74 75 72 6e 65 64 20 69 6d 6d 65 64 69 61 74  eturned immediat
42940 65 6c 79 2e 20 41 67 61 69 6e 2c 20 69 6e 20 74  ely. Again, in t
42950 68 69 73 0a 2a 2a 20 63 61 73 65 20 74 68 65 20  his.** case the 
42960 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f  call to sqlite3_
42970 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 63 61  backup_step() ca
42980 6e 20 62 65 20 72 65 74 72 69 65 64 20 6c 61 74  n be retried lat
42990 65 72 20 6f 6e 2e 20 49 66 0a 2a 2a 20 5b 53 51  er on. If.** [SQ
429a0 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53  LITE_IOERR_ACCES
429b0 53 20 7c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  S | SQLITE_IOERR
429c0 5f 58 58 58 5d 2c 20 5b 53 51 4c 49 54 45 5f 4e  _XXX], [SQLITE_N
429d0 4f 4d 45 4d 5d 2c 20 6f 72 0a 2a 2a 20 5b 53 51  OMEM], or.** [SQ
429e0 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5d 20 69  LITE_READONLY] i
429f0 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65 6e  s returned, then
42a00 20 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f   .** there is no
42a10 20 70 6f 69 6e 74 20 69 6e 20 72 65 74 72 79 69   point in retryi
42a20 6e 67 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73  ng the call to s
42a30 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
42a40 65 70 28 29 2e 20 54 68 65 73 65 20 0a 2a 2a 20  ep(). These .** 
42a50 65 72 72 6f 72 73 20 61 72 65 20 63 6f 6e 73 69  errors are consi
42a60 64 65 72 65 64 20 66 61 74 61 6c 2e 20 41 74 20  dered fatal. At 
42a70 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 20 61  this point the a
42a80 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20  pplication must 
42a90 61 63 63 65 70 74 20 0a 2a 2a 20 74 68 61 74 20  accept .** that 
42aa0 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61  the backup opera
42ab0 74 69 6f 6e 20 68 61 73 20 66 61 69 6c 65 64 20  tion has failed 
42ac0 61 6e 64 20 70 61 73 73 20 74 68 65 20 62 61 63  and pass the bac
42ad0 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 20 68 61  kup operation ha
42ae0 6e 64 6c 65 20 0a 2a 2a 20 74 6f 20 74 68 65 20  ndle .** to the 
42af0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66  sqlite3_backup_f
42b00 69 6e 69 73 68 28 29 20 74 6f 20 72 65 6c 65 61  inish() to relea
42b10 73 65 20 61 73 73 6f 63 69 61 74 65 64 20 72 65  se associated re
42b20 73 6f 75 72 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 46  sources..**.** F
42b30 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 66 69 72  ollowing the fir
42b40 73 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  st call to sqlit
42b50 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29  e3_backup_step()
42b60 2c 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c  , an exclusive l
42b70 6f 63 6b 20 69 73 0a 2a 2a 20 6f 62 74 61 69 6e  ock is.** obtain
42b80 65 64 20 6f 6e 20 74 68 65 20 64 65 73 74 69 6e  ed on the destin
42b90 61 74 69 6f 6e 20 66 69 6c 65 2e 20 49 74 20 69  ation file. It i
42ba0 73 20 6e 6f 74 20 72 65 6c 65 61 73 65 64 20 75  s not released u
42bb0 6e 74 69 6c 20 65 69 74 68 65 72 20 0a 2a 2a 20  ntil either .** 
42bc0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66  sqlite3_backup_f
42bd0 69 6e 69 73 68 28 29 20 69 73 20 63 61 6c 6c 65  inish() is calle
42be0 64 20 6f 72 20 74 68 65 20 62 61 63 6b 75 70 20  d or the backup 
42bf0 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 63 6f 6d  operation is com
42c00 70 6c 65 74 65 20 0a 2a 2a 20 61 6e 64 20 73 71  plete .** and sq
42c10 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
42c20 70 28 29 20 72 65 74 75 72 6e 73 20 5b 53 51 4c  p() returns [SQL
42c30 49 54 45 5f 44 4f 4e 45 5d 2e 20 41 64 64 69 74  ITE_DONE]. Addit
42c40 69 6f 6e 61 6c 6c 79 2c 20 65 61 63 68 20 74 69  ionally, each ti
42c50 6d 65 20 0a 2a 2a 20 61 20 63 61 6c 6c 20 74 6f  me .** a call to
42c60 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
42c70 73 74 65 70 28 29 20 69 73 20 6d 61 64 65 20 61  step() is made a
42c80 20 5b 73 68 61 72 65 64 20 6c 6f 63 6b 5d 20 69   [shared lock] i
42c90 73 20 6f 62 74 61 69 6e 65 64 20 6f 6e 0a 2a 2a  s obtained on.**
42ca0 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61   the source data
42cb0 62 61 73 65 20 66 69 6c 65 2e 20 54 68 69 73 20  base file. This 
42cc0 6c 6f 63 6b 20 69 73 20 72 65 6c 65 61 73 65 64  lock is released
42cd0 20 62 65 66 6f 72 65 20 74 68 65 0a 2a 2a 20 73   before the.** s
42ce0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
42cf0 65 70 28 29 20 63 61 6c 6c 20 72 65 74 75 72 6e  ep() call return
42d00 73 2e 20 42 65 63 61 75 73 65 20 74 68 65 20 73  s. Because the s
42d10 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 69  ource database i
42d20 73 20 6e 6f 74 0a 2a 2a 20 6c 6f 63 6b 65 64 20  s not.** locked 
42d30 62 65 74 77 65 65 6e 20 63 61 6c 6c 73 20 74 6f  between calls to
42d40 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
42d50 73 74 65 70 28 29 2c 20 69 74 20 6d 61 79 20 62  step(), it may b
42d60 65 20 6d 6f 64 69 66 69 65 64 20 6d 69 64 2d 77  e modified mid-w
42d70 61 79 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 68  ay.** through th
42d80 65 20 62 61 63 6b 75 70 20 70 72 6f 63 65 64 75  e backup procedu
42d90 72 65 2e 20 49 66 20 74 68 65 20 73 6f 75 72 63  re. If the sourc
42da0 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6d 6f  e database is mo
42db0 64 69 66 69 65 64 20 62 79 20 61 6e 0a 2a 2a 20  dified by an.** 
42dc0 65 78 74 65 72 6e 61 6c 20 70 72 6f 63 65 73 73  external process
42dd0 20 6f 72 20 76 69 61 20 61 20 64 61 74 61 62 61   or via a databa
42de0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 74  se connection ot
42df0 68 65 72 20 74 68 61 6e 20 74 68 65 20 6f 6e 65  her than the one
42e00 20 62 65 69 6e 67 0a 2a 2a 20 75 73 65 64 20 62   being.** used b
42e10 79 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65  y the backup ope
42e20 72 61 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65  ration, then the
42e30 20 62 61 63 6b 75 70 20 77 69 6c 6c 20 62 65 20   backup will be 
42e40 74 72 61 6e 73 70 61 72 65 6e 74 6c 79 0a 2a 2a  transparently.**
42e50 20 72 65 73 74 61 72 74 65 64 20 62 79 20 74 68   restarted by th
42e60 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20 73  e next call to s
42e70 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
42e80 65 70 28 29 2e 20 49 66 20 74 68 65 20 73 6f 75  ep(). If the sou
42e90 72 63 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65  rce .** database
42ea0 20 69 73 20 6d 6f 64 69 66 69 65 64 20 62 79 20   is modified by 
42eb0 74 68 65 20 75 73 69 6e 67 20 74 68 65 20 73 61  the using the sa
42ec0 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  me database conn
42ed0 65 63 74 69 6f 6e 20 61 73 20 69 73 20 75 73 65  ection as is use
42ee0 64 0a 2a 2a 20 62 79 20 74 68 65 20 62 61 63 6b  d.** by the back
42ef0 75 70 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68  up operation, th
42f00 65 6e 20 74 68 65 20 62 61 63 6b 75 70 20 64 61  en the backup da
42f10 74 61 62 61 73 65 20 69 73 20 74 72 61 6e 73 70  tabase is transp
42f20 61 72 65 6e 74 6c 79 20 0a 2a 2a 20 75 70 64 61  arently .** upda
42f30 74 65 64 20 61 74 20 74 68 65 20 73 61 6d 65 20  ted at the same 
42f40 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73  time..**.** <b>s
42f50 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69  qlite3_backup_fi
42f60 6e 69 73 68 28 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a  nish()</b>.**.**
42f70 20 4f 6e 63 65 20 73 71 6c 69 74 65 33 5f 62 61   Once sqlite3_ba
42f80 63 6b 75 70 5f 73 74 65 70 28 29 20 68 61 73 20  ckup_step() has 
42f90 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45  returned [SQLITE
42fa0 5f 44 4f 4e 45 5d 2c 20 6f 72 20 77 68 65 6e 20  _DONE], or when 
42fb0 74 68 65 20 0a 2a 2a 20 61 70 70 6c 69 63 61 74  the .** applicat
42fc0 69 6f 6e 20 77 69 73 68 65 73 20 74 6f 20 61 62  ion wishes to ab
42fd0 61 6e 64 6f 6e 20 74 68 65 20 62 61 63 6b 75 70  andon the backup
42fe0 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 20   operation, the 
42ff0 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d  [sqlite3_backup]
43000 0a 2a 2a 20 6f 62 6a 65 63 74 20 73 68 6f 75 6c  .** object shoul
43010 64 20 62 65 20 70 61 73 73 65 64 20 74 6f 20 73  d be passed to s
43020 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69  qlite3_backup_fi
43030 6e 69 73 68 28 29 2e 20 54 68 69 73 20 72 65 6c  nish(). This rel
43040 65 61 73 65 73 20 61 6c 6c 0a 2a 2a 20 72 65 73  eases all.** res
43050 6f 75 72 63 65 73 20 61 73 73 6f 63 69 61 74 65  ources associate
43060 64 20 77 69 74 68 20 74 68 65 20 62 61 63 6b 75  d with the backu
43070 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 49 66 20  p operation. If 
43080 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73  sqlite3_backup_s
43090 74 65 70 28 29 0a 2a 2a 20 68 61 73 20 6e 6f 74  tep().** has not
430a0 20 79 65 74 20 72 65 74 75 72 6e 65 64 20 5b 53   yet returned [S
430b0 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c 20 74 68 65  QLITE_DONE], the
430c0 6e 20 61 6e 79 20 61 63 74 69 76 65 20 77 72 69  n any active wri
430d0 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f  te-transaction o
430e0 6e 20 74 68 65 0a 2a 2a 20 64 65 73 74 69 6e 61  n the.** destina
430f0 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 69 73  tion database is
43100 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 54 68   rolled back. Th
43110 65 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  e [sqlite3_backu
43120 70 5d 20 6f 62 6a 65 63 74 20 69 73 20 69 6e 76  p] object is inv
43130 61 6c 69 64 0a 2a 2a 20 61 6e 64 20 6d 61 79 20  alid.** and may 
43140 6e 6f 74 20 62 65 20 75 73 65 64 20 66 6f 6c 6c  not be used foll
43150 6f 77 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20  owing a call to 
43160 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66  sqlite3_backup_f
43170 69 6e 69 73 68 28 29 2e 0a 2a 2a 0a 2a 2a 20 54  inish()..**.** T
43180 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
43190 64 20 62 79 20 73 71 6c 69 74 65 33 5f 62 61 63  d by sqlite3_bac
431a0 6b 75 70 5f 66 69 6e 69 73 68 20 69 73 20 5b 53  kup_finish is [S
431b0 51 4c 49 54 45 5f 4f 4b 5d 20 69 66 20 6e 6f 20  QLITE_OK] if no 
431c0 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 72 65  error.** occurre
431d0 64 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f 72  d, regardless or
431e0 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
431f0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73  sqlite3_backup_s
43200 74 65 70 28 29 20 77 61 73 20 63 61 6c 6c 65 64  tep() was called
43210 0a 2a 2a 20 61 20 73 75 66 66 69 63 69 65 6e 74  .** a sufficient
43220 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73   number of times
43230 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65   to complete the
43240 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f   backup operatio
43250 6e 2e 20 4f 72 2c 20 69 66 0a 2a 2a 20 61 6e 20  n. Or, if.** an 
43260 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 6f  out-of-memory co
43270 6e 64 69 74 69 6f 6e 20 6f 72 20 49 4f 20 65 72  ndition or IO er
43280 72 6f 72 20 6f 63 63 75 72 65 64 20 64 75 72 69  ror occured duri
43290 6e 67 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20  ng a call to.** 
432a0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73  sqlite3_backup_s
432b0 74 65 70 28 29 20 74 68 65 6e 20 5b 53 51 4c 49  tep() then [SQLI
432c0 54 45 5f 4e 4f 4d 45 4d 5d 20 6f 72 20 61 6e 0a  TE_NOMEM] or an.
432d0 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52  ** [SQLITE_IOERR
432e0 5f 41 43 43 45 53 53 20 7c 20 53 51 4c 49 54 45  _ACCESS | SQLITE
432f0 5f 49 4f 45 52 52 5f 58 58 58 5d 20 65 72 72 6f  _IOERR_XXX] erro
43300 72 20 63 6f 64 65 0a 2a 2a 20 69 73 20 72 65 74  r code.** is ret
43310 75 72 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 63  urned. In this c
43320 61 73 65 20 74 68 65 20 65 72 72 6f 72 20 63 6f  ase the error co
43330 64 65 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20  de and an error 
43340 6d 65 73 73 61 67 65 20 61 72 65 0a 2a 2a 20 77  message are.** w
43350 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 64 65  ritten to the de
43360 73 74 69 6e 61 74 69 6f 6e 20 5b 64 61 74 61 62  stination [datab
43370 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e  ase connection].
43380 0a 2a 2a 0a 2a 2a 20 41 20 72 65 74 75 72 6e 20  .**.** A return 
43390 6f 66 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d  of [SQLITE_BUSY]
433a0 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b   or [SQLITE_LOCK
433b0 45 44 5d 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ED] from sqlite3
433c0 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 69  _backup_step() i
433d0 73 0a 2a 2a 20 6e 6f 74 20 61 20 70 65 72 6d 61  s.** not a perma
433e0 6e 65 6e 74 20 65 72 72 6f 72 20 61 6e 64 20 64  nent error and d
433f0 6f 65 73 20 6e 6f 74 20 61 66 66 65 63 74 20 74  oes not affect t
43400 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
43410 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61  of.** sqlite3_ba
43420 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 2e 0a 2a  ckup_finish()..*
43430 2a 0a 2a 2a 20 3c 62 3e 73 71 6c 69 74 65 33 5f  *.** <b>sqlite3_
43440 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67  backup_remaining
43450 28 29 2c 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  (), sqlite3_back
43460 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 3c 2f  up_pagecount()</
43470 62 3e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 63 61  b>.**.** Each ca
43480 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61  ll to sqlite3_ba
43490 63 6b 75 70 5f 73 74 65 70 28 29 20 73 65 74 73  ckup_step() sets
434a0 20 74 77 6f 20 76 61 6c 75 65 73 20 73 74 6f 72   two values stor
434b0 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a  ed internally.**
434c0 20 62 79 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f   by an [sqlite3_
434d0 62 61 63 6b 75 70 5d 20 6f 62 6a 65 63 74 2e 20  backup] object. 
434e0 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  The number of pa
434f0 67 65 73 20 73 74 69 6c 6c 20 74 6f 20 62 65 20  ges still to be 
43500 62 61 63 6b 65 64 0a 2a 2a 20 75 70 2c 20 77 68  backed.** up, wh
43510 69 63 68 20 6d 61 79 20 62 65 20 71 75 65 72 69  ich may be queri
43520 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 62 61  ed by sqlite3_ba
43530 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29  ckup_remaining()
43540 2c 20 61 6e 64 20 74 68 65 20 74 6f 74 61 6c 0a  , and the total.
43550 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  ** number of pag
43560 65 73 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65  es in the source
43570 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20   database file, 
43580 77 68 69 63 68 20 6d 61 79 20 62 65 20 71 75 65  which may be que
43590 72 69 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74  ried by.** sqlit
435a0 65 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 63 6f  e3_backup_pageco
435b0 75 6e 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  unt()..**.** The
435c0 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64   values returned
435d0 20 62 79 20 74 68 65 73 65 20 66 75 6e 63 74 69   by these functi
435e0 6f 6e 73 20 61 72 65 20 6f 6e 6c 79 20 75 70 64  ons are only upd
435f0 61 74 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74  ated by.** sqlit
43600 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29  e3_backup_step()
43610 2e 20 49 66 20 74 68 65 20 73 6f 75 72 63 65 20  . If the source 
43620 64 61 74 61 62 61 73 65 20 69 73 20 6d 6f 64 69  database is modi
43630 66 69 65 64 20 64 75 72 69 6e 67 20 61 20 62 61  fied during a ba
43640 63 6b 75 70 0a 2a 2a 20 6f 70 65 72 61 74 69 6f  ckup.** operatio
43650 6e 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75  n, then the valu
43660 65 73 20 61 72 65 20 6e 6f 74 20 75 70 64 61 74  es are not updat
43670 65 64 20 74 6f 20 61 63 63 6f 75 6e 74 20 66 6f  ed to account fo
43680 72 20 61 6e 79 20 65 78 74 72 61 0a 2a 2a 20 70  r any extra.** p
43690 61 67 65 73 20 74 68 61 74 20 6e 65 65 64 20 74  ages that need t
436a0 6f 20 62 65 20 75 70 64 61 74 65 64 20 6f 72 20  o be updated or 
436b0 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
436c0 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20  source database 
436d0 66 69 6c 65 0a 2a 2a 20 63 68 61 6e 67 69 6e 67  file.** changing
436e0 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 43 6f 6e 63 75  ..**.** <b>Concu
436f0 72 72 65 6e 74 20 55 73 61 67 65 20 6f 66 20 44  rrent Usage of D
43700 61 74 61 62 61 73 65 20 48 61 6e 64 6c 65 73 3c  atabase Handles<
43710 2f 62 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 6f  /b>.**.** The so
43720 75 72 63 65 20 5b 64 61 74 61 62 61 73 65 20 63  urce [database c
43730 6f 6e 6e 65 63 74 69 6f 6e 5d 20 6d 61 79 20 62  onnection] may b
43740 65 20 75 73 65 64 20 62 79 20 74 68 65 20 61 70  e used by the ap
43750 70 6c 69 63 61 74 69 6f 6e 20 66 6f 72 20 6f 74  plication for ot
43760 68 65 72 0a 2a 2a 20 70 75 72 70 6f 73 65 73 20  her.** purposes 
43770 77 68 69 6c 65 20 61 20 62 61 63 6b 75 70 20 6f  while a backup o
43780 70 65 72 61 74 69 6f 6e 20 69 73 20 75 6e 64 65  peration is unde
43790 72 77 61 79 20 6f 72 20 62 65 69 6e 67 20 69 6e  rway or being in
437a0 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20 49 66  itialized..** If
437b0 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69   SQLite is compi
437c0 6c 65 64 20 61 6e 64 20 63 6f 6e 66 69 67 75 72  led and configur
437d0 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20 74 68  ed to support th
437e0 72 65 61 64 73 61 66 65 20 64 61 74 61 62 61 73  readsafe databas
437f0 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  e.** connections
43800 2c 20 74 68 65 6e 20 74 68 65 20 73 6f 75 72 63  , then the sourc
43810 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
43820 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 75 73 65  ction may be use
43830 64 20 63 6f 6e 63 75 72 72 65 6e 74 6c 79 0a 2a  d concurrently.*
43840 2a 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 6f 74  * from within ot
43850 68 65 72 20 74 68 72 65 61 64 73 2e 0a 2a 2a 0a  her threads..**.
43860 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65 20  ** However, the 
43870 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74  application must
43880 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20   guarantee that 
43890 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  the destination 
438a0 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e  database.** conn
438b0 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 69 73  ection handle is
438c0 20 6e 6f 74 20 70 61 73 73 65 64 20 74 6f 20 61   not passed to a
438d0 6e 79 20 6f 74 68 65 72 20 41 50 49 20 28 62 79  ny other API (by
438e0 20 61 6e 79 20 74 68 72 65 61 64 29 20 61 66 74   any thread) aft
438f0 65 72 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62  er .** sqlite3_b
43900 61 63 6b 75 70 5f 69 6e 69 74 28 29 20 69 73 20  ackup_init() is 
43910 63 61 6c 6c 65 64 20 61 6e 64 20 62 65 66 6f 72  called and befor
43920 65 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  e the correspond
43930 69 6e 67 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73  ing call to.** s
43940 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69  qlite3_backup_fi
43950 6e 69 73 68 28 29 2e 20 55 6e 66 6f 72 74 75 6e  nish(). Unfortun
43960 61 74 65 6c 79 20 53 51 4c 69 74 65 20 64 6f 65  ately SQLite doe
43970 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20  s not currently 
43980 63 68 65 63 6b 0a 2a 2a 20 66 6f 72 20 74 68 69  check.** for thi
43990 73 2c 20 69 66 20 74 68 65 20 61 70 70 6c 69 63  s, if the applic
439a0 61 74 69 6f 6e 20 64 6f 65 73 20 75 73 65 20 74  ation does use t
439b0 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 5b  he destination [
439c0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
439d0 69 6f 6e 5d 0a 2a 2a 20 66 6f 72 20 73 6f 6d 65  ion].** for some
439e0 20 6f 74 68 65 72 20 70 75 72 70 6f 73 65 20 64   other purpose d
439f0 75 72 69 6e 67 20 61 20 62 61 63 6b 75 70 20 6f  uring a backup o
43a00 70 65 72 61 74 69 6f 6e 2c 20 74 68 69 6e 67 73  peration, things
43a10 20 6d 61 79 20 61 70 70 65 61 72 20 74 6f 0a 2a   may appear to.*
43a20 2a 20 77 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79  * work correctly
43a30 20 62 75 74 20 69 6e 20 66 61 63 74 20 62 65 20   but in fact be 
43a40 73 75 62 74 6c 79 20 6d 61 6c 66 75 6e 63 74 69  subtly malfuncti
43a50 6f 6e 69 6e 67 2e 20 20 55 73 65 20 6f 66 20 74  oning.  Use of t
43a60 68 65 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f  he.** destinatio
43a70 6e 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  n database conne
43a80 63 74 69 6f 6e 20 77 68 69 6c 65 20 61 20 62 61  ction while a ba
43a90 63 6b 75 70 20 69 73 20 69 6e 20 70 72 6f 67 72  ckup is in progr
43aa0 65 73 73 20 6d 69 67 68 74 0a 2a 2a 20 61 6c 73  ess might.** als
43ab0 6f 20 63 61 75 73 65 20 61 20 6d 75 74 65 78 20  o cause a mutex 
43ac0 64 65 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20  deadlock..**.** 
43ad0 46 75 72 74 68 65 72 6d 6f 72 65 2c 20 69 66 20  Furthermore, if 
43ae0 72 75 6e 6e 69 6e 67 20 69 6e 20 5b 73 68 61 72  running in [shar
43af0 65 64 20 63 61 63 68 65 20 6d 6f 64 65 5d 2c 20  ed cache mode], 
43b00 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  the application 
43b10 6d 75 73 74 0a 2a 2a 20 67 75 61 72 61 6e 74 65  must.** guarante
43b20 65 20 74 68 61 74 20 74 68 65 20 73 68 61 72 65  e that the share
43b30 64 20 63 61 63 68 65 20 75 73 65 64 20 62 79 20  d cache used by 
43b40 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  the destination 
43b50 64 61 74 61 62 61 73 65 0a 2a 2a 20 69 73 20 6e  database.** is n
43b60 6f 74 20 61 63 63 65 73 73 65 64 20 77 68 69 6c  ot accessed whil
43b70 65 20 74 68 65 20 62 61 63 6b 75 70 20 69 73 20  e the backup is 
43b80 72 75 6e 6e 69 6e 67 2e 20 49 6e 20 70 72 61 63  running. In prac
43b90 74 69 63 65 20 74 68 69 73 20 6d 65 61 6e 73 0a  tice this means.
43ba0 2a 2a 20 74 68 61 74 20 74 68 65 20 61 70 70 6c  ** that the appl
43bb0 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 67 75 61  ication must gua
43bc0 72 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 20  rantee that the 
43bd0 66 69 6c 65 2d 73 79 73 74 65 6d 20 66 69 6c 65  file-system file
43be0 20 62 65 69 6e 67 20 0a 2a 2a 20 62 61 63 6b 65   being .** backe
43bf0 64 20 75 70 20 74 6f 20 69 73 20 6e 6f 74 20 61  d up to is not a
43c00 63 63 65 73 73 65 64 20 62 79 20 61 6e 79 20 63  ccessed by any c
43c10 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 74 68 69 6e  onnection within
43c20 20 74 68 65 20 70 72 6f 63 65 73 73 2c 0a 2a 2a   the process,.**
43c30 20 6e 6f 74 20 6a 75 73 74 20 74 68 65 20 73 70   not just the sp
43c40 65 63 69 66 69 63 20 63 6f 6e 6e 65 63 74 69 6f  ecific connectio
43c50 6e 20 74 68 61 74 20 77 61 73 20 70 61 73 73 65  n that was passe
43c60 64 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63  d to sqlite3_bac
43c70 6b 75 70 5f 69 6e 69 74 28 29 2e 0a 2a 2a 0a 2a  kup_init()..**.*
43c80 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62  * The [sqlite3_b
43c90 61 63 6b 75 70 5d 20 6f 62 6a 65 63 74 20 69 74  ackup] object it
43ca0 73 65 6c 66 20 69 73 20 70 61 72 74 69 61 6c 6c  self is partiall
43cb0 79 20 74 68 72 65 61 64 73 61 66 65 2e 20 4d 75  y threadsafe. Mu
43cc0 6c 74 69 70 6c 65 20 0a 2a 2a 20 74 68 72 65 61  ltiple .** threa
43cd0 64 73 20 6d 61 79 20 73 61 66 65 6c 79 20 6d 61  ds may safely ma
43ce0 6b 65 20 6d 75 6c 74 69 70 6c 65 20 63 6f 6e 63  ke multiple conc
43cf0 75 72 72 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20  urrent calls to 
43d00 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73  sqlite3_backup_s
43d10 74 65 70 28 29 2e 0a 2a 2a 20 48 6f 77 65 76 65  tep()..** Howeve
43d20 72 2c 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62  r, the sqlite3_b
43d30 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28  ackup_remaining(
43d40 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 62 61  ) and sqlite3_ba
43d50 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 29  ckup_pagecount()
43d60 0a 2a 2a 20 41 50 49 73 20 61 72 65 20 6e 6f 74  .** APIs are not
43d70 20 73 74 72 69 63 74 6c 79 20 73 70 65 61 6b 69   strictly speaki
43d80 6e 67 20 74 68 72 65 61 64 73 61 66 65 2e 20 49  ng threadsafe. I
43d90 66 20 74 68 65 79 20 61 72 65 20 69 6e 76 6f 6b  f they are invok
43da0 65 64 20 61 74 20 74 68 65 0a 2a 2a 20 73 61 6d  ed at the.** sam
43db0 65 20 74 69 6d 65 20 61 73 20 61 6e 6f 74 68 65  e time as anothe
43dc0 72 20 74 68 72 65 61 64 20 69 73 20 69 6e 76 6f  r thread is invo
43dd0 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f 62 61 63  king sqlite3_bac
43de0 6b 75 70 5f 73 74 65 70 28 29 20 69 74 20 69 73  kup_step() it is
43df0 0a 2a 2a 20 70 6f 73 73 69 62 6c 65 20 74 68 61  .** possible tha
43e00 74 20 74 68 65 79 20 72 65 74 75 72 6e 20 69 6e  t they return in
43e10 76 61 6c 69 64 20 76 61 6c 75 65 73 2e 0a 2a 2f  valid values..*/
43e20 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
43e30 74 65 33 5f 62 61 63 6b 75 70 20 2a 73 71 6c 69  te3_backup *sqli
43e40 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28  te3_backup_init(
43e50 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 44 65 73  .  sqlite3 *pDes
43e60 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
43e70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73            /* Des
43e80 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73  tination databas
43e90 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f  e handle */.  co
43ea0 6e 73 74 20 63 68 61 72 20 2a 7a 44 65 73 74 4e  nst char *zDestN
43eb0 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ame,            
43ec0 20 20 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74       /* Destinat
43ed0 69 6f 6e 20 64 61 74 61 62 61 73 65 20 6e 61 6d  ion database nam
43ee0 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  e */.  sqlite3 *
43ef0 70 53 6f 75 72 63 65 2c 20 20 20 20 20 20 20 20  pSource,        
43f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
43f10 20 53 6f 75 72 63 65 20 64 61 74 61 62 61 73 65   Source database
43f20 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e   handle */.  con
43f30 73 74 20 63 68 61 72 20 2a 7a 53 6f 75 72 63 65  st char *zSource
43f40 4e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20  Name            
43f50 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 64 61      /* Source da
43f60 74 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 29  tabase name */.)
43f70 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
43f80 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
43f90 73 74 65 70 28 73 71 6c 69 74 65 33 5f 62 61 63  step(sqlite3_bac
43fa0 6b 75 70 20 2a 70 2c 20 69 6e 74 20 6e 50 61 67  kup *p, int nPag
43fb0 65 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  e);.SQLITE_API i
43fc0 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  nt sqlite3_backu
43fd0 70 5f 66 69 6e 69 73 68 28 73 71 6c 69 74 65 33  p_finish(sqlite3
43fe0 5f 62 61 63 6b 75 70 20 2a 70 29 3b 0a 53 51 4c  _backup *p);.SQL
43ff0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
44000 74 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69  te3_backup_remai
44010 6e 69 6e 67 28 73 71 6c 69 74 65 33 5f 62 61 63  ning(sqlite3_bac
44020 6b 75 70 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f  kup *p);.SQLITE_
44030 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
44040 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74  backup_pagecount
44050 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20  (sqlite3_backup 
44060 2a 70 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  *p);../*.** CAPI
44070 33 52 45 46 3a 20 55 6e 6c 6f 63 6b 20 4e 6f 74  3REF: Unlock Not
44080 69 66 69 63 61 74 69 6f 6e 0a 2a 2a 20 45 58 50  ification.** EXP
44090 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20  ERIMENTAL.**.** 
440a0 57 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e 20  When running in 
440b0 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f 64  shared-cache mod
440c0 65 2c 20 61 20 64 61 74 61 62 61 73 65 20 6f 70  e, a database op
440d0 65 72 61 74 69 6f 6e 20 6d 61 79 20 66 61 69 6c  eration may fail
440e0 20 77 69 74 68 0a 2a 2a 20 61 6e 20 5b 53 51 4c   with.** an [SQL
440f0 49 54 45 5f 4c 4f 43 4b 45 44 5d 20 65 72 72 6f  ITE_LOCKED] erro
44100 72 20 69 66 20 74 68 65 20 72 65 71 75 69 72 65  r if the require
44110 64 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 73  d locks on the s
44120 68 61 72 65 64 2d 63 61 63 68 65 20 6f 72 0a 2a  hared-cache or.*
44130 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 74 61 62  * individual tab
44140 6c 65 73 20 77 69 74 68 69 6e 20 74 68 65 20 73  les within the s
44150 68 61 72 65 64 2d 63 61 63 68 65 20 63 61 6e 6e  hared-cache cann
44160 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2e 20  ot be obtained. 
44170 53 65 65 0a 2a 2a 20 5b 53 51 4c 69 74 65 20 53  See.** [SQLite S
44180 68 61 72 65 64 2d 43 61 63 68 65 20 4d 6f 64 65  hared-Cache Mode
44190 5d 20 66 6f 72 20 61 20 64 65 73 63 72 69 70 74  ] for a descript
441a0 69 6f 6e 20 6f 66 20 73 68 61 72 65 64 2d 63 61  ion of shared-ca
441b0 63 68 65 20 6c 6f 63 6b 69 6e 67 2e 20 0a 2a 2a  che locking. .**
441c0 20 54 68 69 73 20 41 50 49 20 6d 61 79 20 62 65   This API may be
441d0 20 75 73 65 64 20 74 6f 20 72 65 67 69 73 74 65   used to registe
441e0 72 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 68 61  r a callback tha
441f0 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 69 6e  t SQLite will in
44200 76 6f 6b 65 20 0a 2a 2a 20 77 68 65 6e 20 74 68  voke .** when th
44210 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 75 72  e connection cur
44220 72 65 6e 74 6c 79 20 68 6f 6c 64 69 6e 67 20 74  rently holding t
44230 68 65 20 72 65 71 75 69 72 65 64 20 6c 6f 63 6b  he required lock
44240 20 72 65 6c 69 6e 71 75 69 73 68 65 73 20 69 74   relinquishes it
44250 2e 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 69 73  ..** This API is
44260 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20   only available 
44270 69 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 77  if the library w
44280 61 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68  as compiled with
44290 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f   the.** [SQLITE_
442a0 45 4e 41 42 4c 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f  ENABLE_UNLOCK_NO
442b0 54 49 46 59 5d 20 43 2d 70 72 65 70 72 6f 63 65  TIFY] C-preproce
442c0 73 73 6f 72 20 73 79 6d 62 6f 6c 20 64 65 66 69  ssor symbol defi
442d0 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 41  ned..**.** See A
442e0 6c 73 6f 3a 20 5b 55 73 69 6e 67 20 74 68 65 20  lso: [Using the 
442f0 53 51 4c 69 74 65 20 55 6e 6c 6f 63 6b 20 4e 6f  SQLite Unlock No
44300 74 69 66 69 63 61 74 69 6f 6e 20 46 65 61 74 75  tification Featu
44310 72 65 5d 2e 0a 2a 2a 0a 2a 2a 20 53 68 61 72 65  re]..**.** Share
44320 64 2d 63 61 63 68 65 20 6c 6f 63 6b 73 20 61 72  d-cache locks ar
44330 65 20 72 65 6c 65 61 73 65 64 20 77 68 65 6e 20  e released when 
44340 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  a database conne
44350 63 74 69 6f 6e 20 63 6f 6e 63 6c 75 64 65 73 0a  ction concludes.
44360 2a 2a 20 69 74 73 20 63 75 72 72 65 6e 74 20 74  ** its current t
44370 72 61 6e 73 61 63 74 69 6f 6e 2c 20 65 69 74 68  ransaction, eith
44380 65 72 20 62 79 20 63 6f 6d 6d 69 74 74 69 6e 67  er by committing
44390 20 69 74 20 6f 72 20 72 6f 6c 6c 69 6e 67 20 69   it or rolling i
443a0 74 20 62 61 63 6b 2e 20 0a 2a 2a 0a 2a 2a 20 57  t back. .**.** W
443b0 68 65 6e 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e  hen a connection
443c0 20 28 6b 6e 6f 77 6e 20 61 73 20 74 68 65 20 62   (known as the b
443d0 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f  locked connectio
443e0 6e 29 20 66 61 69 6c 73 20 74 6f 20 6f 62 74 61  n) fails to obta
443f0 69 6e 20 61 0a 2a 2a 20 73 68 61 72 65 64 2d 63  in a.** shared-c
44400 61 63 68 65 20 6c 6f 63 6b 20 61 6e 64 20 53 51  ache lock and SQ
44410 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 73 20 72  LITE_LOCKED is r
44420 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63  eturned to the c
44430 61 6c 6c 65 72 2c 20 74 68 65 0a 2a 2a 20 69 64  aller, the.** id
44440 65 6e 74 69 74 79 20 6f 66 20 74 68 65 20 64 61  entity of the da
44450 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
44460 6e 20 28 74 68 65 20 62 6c 6f 63 6b 69 6e 67 20  n (the blocking 
44470 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 74 68 61 74  connection) that
44480 0a 2a 2a 20 68 61 73 20 6c 6f 63 6b 65 64 20 74  .** has locked t
44490 68 65 20 72 65 71 75 69 72 65 64 20 72 65 73 6f  he required reso
444a0 75 72 63 65 20 69 73 20 73 74 6f 72 65 64 20 69  urce is stored i
444b0 6e 74 65 72 6e 61 6c 6c 79 2e 20 41 66 74 65 72  nternally. After
444c0 20 61 6e 20 0a 2a 2a 20 61 70 70 6c 69 63 61 74   an .** applicat
444d0 69 6f 6e 20 72 65 63 65 69 76 65 73 20 61 6e 20  ion receives an 
444e0 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 65 72  SQLITE_LOCKED er
444f0 72 6f 72 2c 20 69 74 20 6d 61 79 20 63 61 6c 6c  ror, it may call
44500 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f   the.** sqlite3_
44510 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20  unlock_notify() 
44520 6d 65 74 68 6f 64 20 77 69 74 68 20 74 68 65 20  method with the 
44530 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69  blocked connecti
44540 6f 6e 20 68 61 6e 64 6c 65 20 61 73 20 0a 2a 2a  on handle as .**
44550 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
44560 65 6e 74 20 74 6f 20 72 65 67 69 73 74 65 72 20  ent to register 
44570 66 6f 72 20 61 20 63 61 6c 6c 62 61 63 6b 20 74  for a callback t
44580 68 61 74 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f  hat will be invo
44590 6b 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20  ked.** when the 
445a0 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74  blocking connect
445b0 69 6f 6e 73 20 63 75 72 72 65 6e 74 20 74 72 61  ions current tra
445c0 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6e 63  nsaction is conc
445d0 6c 75 64 65 64 2e 20 54 68 65 0a 2a 2a 20 63 61  luded. The.** ca
445e0 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65  llback is invoke
445f0 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68  d from within th
44600 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 5d  e [sqlite3_step]
44610 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f   or [sqlite3_clo
44620 73 65 5d 0a 2a 2a 20 63 61 6c 6c 20 74 68 61 74  se].** call that
44630 20 63 6f 6e 63 6c 75 64 65 73 20 74 68 65 20 62   concludes the b
44640 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69  locking connecti
44650 6f 6e 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  ons transaction.
44660 0a 2a 2a 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65  .**.** If sqlite
44670 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28  3_unlock_notify(
44680 29 20 69 73 20 63 61 6c 6c 65 64 20 69 6e 20 61  ) is called in a
44690 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20   multi-threaded 
446a0 61 70 70 6c 69 63 61 74 69 6f 6e 2c 0a 2a 2a 20  application,.** 
446b0 74 68 65 72 65 20 69 73 20 61 20 63 68 61 6e 63  there is a chanc
446c0 65 20 74 68 61 74 20 74 68 65 20 62 6c 6f 63 6b  e that the block
446d0 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77  ing connection w
446e0 69 6c 6c 20 68 61 76 65 20 61 6c 72 65 61 64 79  ill have already
446f0 0a 2a 2a 20 63 6f 6e 63 6c 75 64 65 64 20 69 74  .** concluded it
44700 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 79  s transaction by
44710 20 74 68 65 20 74 69 6d 65 20 73 71 6c 69 74 65   the time sqlite
44720 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28  3_unlock_notify(
44730 29 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a  ) is invoked..**
44740 20 49 66 20 74 68 69 73 20 68 61 70 70 65 6e 73   If this happens
44750 2c 20 74 68 65 6e 20 74 68 65 20 73 70 65 63 69  , then the speci
44760 66 69 65 64 20 63 61 6c 6c 62 61 63 6b 20 69 73  fied callback is
44770 20 69 6e 76 6f 6b 65 64 20 69 6d 6d 65 64 69 61   invoked immedia
44780 74 65 6c 79 2c 0a 2a 2a 20 66 72 6f 6d 20 77 69  tely,.** from wi
44790 74 68 69 6e 20 74 68 65 20 63 61 6c 6c 20 74 6f  thin the call to
447a0 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f   sqlite3_unlock_
447b0 6e 6f 74 69 66 79 28 29 2e 0a 2a 2a 0a 2a 2a 20  notify()..**.** 
447c0 49 66 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 63  If the blocked c
447d0 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 61 74 74  onnection is att
447e0 65 6d 70 74 69 6e 67 20 74 6f 20 6f 62 74 61 69  empting to obtai
447f0 6e 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f  n a write-lock o
44800 6e 20 61 0a 2a 2a 20 73 68 61 72 65 64 2d 63 61  n a.** shared-ca
44810 63 68 65 20 74 61 62 6c 65 2c 20 61 6e 64 20 6d  che table, and m
44820 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 6f 74 68  ore than one oth
44830 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 75  er connection cu
44840 72 72 65 6e 74 6c 79 20 68 6f 6c 64 73 0a 2a 2a  rrently holds.**
44850 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20   a read-lock on 
44860 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 2c 20  the same table, 
44870 74 68 65 6e 20 53 51 4c 69 74 65 20 61 72 62 69  then SQLite arbi
44880 74 72 61 72 69 6c 79 20 73 65 6c 65 63 74 73 20  trarily selects 
44890 6f 6e 65 20 6f 66 20 0a 2a 2a 20 74 68 65 20 6f  one of .** the o
448a0 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  ther connections
448b0 20 74 6f 20 75 73 65 20 61 73 20 74 68 65 20 62   to use as the b
448c0 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69  locking connecti
448d0 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20  on..**.** There 
448e0 6d 61 79 20 62 65 20 61 74 20 6d 6f 73 74 20 6f  may be at most o
448f0 6e 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79  ne unlock-notify
44900 20 63 61 6c 6c 62 61 63 6b 20 72 65 67 69 73 74   callback regist
44910 65 72 65 64 20 62 79 20 61 20 0a 2a 2a 20 62 6c  ered by a .** bl
44920 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e  ocked connection
44930 2e 20 49 66 20 73 71 6c 69 74 65 33 5f 75 6e 6c  . If sqlite3_unl
44940 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 69 73 20  ock_notify() is 
44950 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 0a  called when the.
44960 2a 2a 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65  ** blocked conne
44970 63 74 69 6f 6e 20 61 6c 72 65 61 64 79 20 68 61  ction already ha
44980 73 20 61 20 72 65 67 69 73 74 65 72 65 64 20 75  s a registered u
44990 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c  nlock-notify cal
449a0 6c 62 61 63 6b 2c 0a 2a 2a 20 74 68 65 6e 20 74  lback,.** then t
449b0 68 65 20 6e 65 77 20 63 61 6c 6c 62 61 63 6b 20  he new callback 
449c0 72 65 70 6c 61 63 65 73 20 74 68 65 20 6f 6c 64  replaces the old
449d0 2e 20 49 66 20 73 71 6c 69 74 65 33 5f 75 6e 6c  . If sqlite3_unl
449e0 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 69 73 0a  ock_notify() is.
449f0 2a 2a 20 63 61 6c 6c 65 64 20 77 69 74 68 20 61  ** called with a
44a00 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 61 73   NULL pointer as
44a10 20 69 74 73 20 73 65 63 6f 6e 64 20 61 72 67 75   its second argu
44a20 6d 65 6e 74 2c 20 74 68 65 6e 20 61 6e 79 20 65  ment, then any e
44a30 78 69 73 74 69 6e 67 0a 2a 2a 20 75 6e 6c 6f 63  xisting.** unloc
44a40 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63  k-notify callbac
44a50 6b 20 69 73 20 63 61 6e 63 65 6c 6c 65 64 2e 20  k is cancelled. 
44a60 54 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e  The blocked conn
44a70 65 63 74 69 6f 6e 73 20 0a 2a 2a 20 75 6e 6c 6f  ections .** unlo
44a80 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61  ck-notify callba
44a90 63 6b 20 6d 61 79 20 61 6c 73 6f 20 62 65 20 63  ck may also be c
44aa0 61 6e 63 65 6c 65 64 20 62 79 20 63 6c 6f 73 69  anceled by closi
44ab0 6e 67 20 74 68 65 20 62 6c 6f 63 6b 65 64 0a 2a  ng the blocked.*
44ac0 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 75 73 69  * connection usi
44ad0 6e 67 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73  ng [sqlite3_clos
44ae0 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  e()]..**.** The 
44af0 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61  unlock-notify ca
44b00 6c 6c 62 61 63 6b 20 69 73 20 6e 6f 74 20 72 65  llback is not re
44b10 65 6e 74 72 61 6e 74 2e 20 49 66 20 61 6e 20 61  entrant. If an a
44b20 70 70 6c 69 63 61 74 69 6f 6e 20 69 6e 76 6f 6b  pplication invok
44b30 65 73 0a 2a 2a 20 61 6e 79 20 73 71 6c 69 74 65  es.** any sqlite
44b40 33 5f 78 78 78 20 41 50 49 20 66 75 6e 63 74 69  3_xxx API functi
44b50 6f 6e 73 20 66 72 6f 6d 20 77 69 74 68 69 6e 20  ons from within 
44b60 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79  an unlock-notify
44b70 20 63 61 6c 6c 62 61 63 6b 2c 20 61 0a 2a 2a 20   callback, a.** 
44b80 63 72 61 73 68 20 6f 72 20 64 65 61 64 6c 6f 63  crash or deadloc
44b90 6b 20 6d 61 79 20 62 65 20 74 68 65 20 72 65 73  k may be the res
44ba0 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 65 73  ult..**.** Unles
44bb0 73 20 64 65 61 64 6c 6f 63 6b 20 69 73 20 64 65  s deadlock is de
44bc0 74 65 63 74 65 64 20 28 73 65 65 20 62 65 6c 6f  tected (see belo
44bd0 77 29 2c 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f  w), sqlite3_unlo
44be0 63 6b 5f 6e 6f 74 69 66 79 28 29 20 61 6c 77 61  ck_notify() alwa
44bf0 79 73 0a 2a 2a 20 72 65 74 75 72 6e 73 20 53 51  ys.** returns SQ
44c00 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 3c  LITE_OK..**.** <
44c10 62 3e 43 61 6c 6c 62 61 63 6b 20 49 6e 76 6f 63  b>Callback Invoc
44c20 61 74 69 6f 6e 20 44 65 74 61 69 6c 73 3c 2f 62  ation Details</b
44c30 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 6e 20  >.**.** When an 
44c40 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61  unlock-notify ca
44c50 6c 6c 62 61 63 6b 20 69 73 20 72 65 67 69 73 74  llback is regist
44c60 65 72 65 64 2c 20 74 68 65 20 61 70 70 6c 69 63  ered, the applic
44c70 61 74 69 6f 6e 20 70 72 6f 76 69 64 65 73 20 61  ation provides a
44c80 20 0a 2a 2a 20 73 69 6e 67 6c 65 20 76 6f 69 64   .** single void
44c90 2a 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 69  * pointer that i
44ca0 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 20  s passed to the 
44cb0 63 61 6c 6c 62 61 63 6b 20 77 68 65 6e 20 69 74  callback when it
44cc0 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 20   is invoked..** 
44cd0 48 6f 77 65 76 65 72 2c 20 74 68 65 20 73 69 67  However, the sig
44ce0 6e 61 74 75 72 65 20 6f 66 20 74 68 65 20 63 61  nature of the ca
44cf0 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20  llback function 
44d00 61 6c 6c 6f 77 73 20 53 51 4c 69 74 65 20 74 6f  allows SQLite to
44d10 20 70 61 73 73 0a 2a 2a 20 69 74 20 61 6e 20 61   pass.** it an a
44d20 72 72 61 79 20 6f 66 20 76 6f 69 64 2a 20 63 6f  rray of void* co
44d30 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 73 2e 20  ntext pointers. 
44d40 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  The first argume
44d50 6e 74 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a 20  nt passed to.** 
44d60 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79  an unlock-notify
44d70 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 20 70   callback is a p
44d80 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 61 72 72  ointer to an arr
44d90 61 79 20 6f 66 20 76 6f 69 64 2a 20 70 6f 69 6e  ay of void* poin
44da0 74 65 72 73 2c 0a 2a 2a 20 61 6e 64 20 74 68 65  ters,.** and the
44db0 20 73 65 63 6f 6e 64 20 69 73 20 74 68 65 20 6e   second is the n
44dc0 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
44dd0 20 69 6e 20 74 68 65 20 61 72 72 61 79 2e 0a 2a   in the array..*
44de0 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 62 6c 6f 63  *.** When a bloc
44df0 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  king connections
44e00 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
44e10 63 6f 6e 63 6c 75 64 65 64 2c 20 74 68 65 72 65  concluded, there
44e20 20 6d 61 79 20 62 65 0a 2a 2a 20 6d 6f 72 65 20   may be.** more 
44e30 74 68 61 6e 20 6f 6e 65 20 62 6c 6f 63 6b 65 64  than one blocked
44e40 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74   connection that
44e50 20 68 61 73 20 72 65 67 69 73 74 65 72 65 64 20   has registered 
44e60 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f  for an unlock-no
44e70 74 69 66 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b  tify.** callback
44e80 2e 20 49 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65  . If two or more
44e90 20 73 75 63 68 20 62 6c 6f 63 6b 65 64 20 63 6f   such blocked co
44ea0 6e 6e 65 63 74 69 6f 6e 73 20 68 61 76 65 20 73  nnections have s
44eb0 70 65 63 69 66 69 65 64 20 74 68 65 0a 2a 2a 20  pecified the.** 
44ec0 73 61 6d 65 20 63 61 6c 6c 62 61 63 6b 20 66 75  same callback fu
44ed0 6e 63 74 69 6f 6e 2c 20 74 68 65 6e 20 69 6e 73  nction, then ins
44ee0 74 65 61 64 20 6f 66 20 69 6e 76 6f 6b 69 6e 67  tead of invoking
44ef0 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75   the callback fu
44f00 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75 6c 74 69 70  nction.** multip
44f10 6c 65 20 74 69 6d 65 73 2c 20 69 74 20 69 73 20  le times, it is 
44f20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 77 69 74  invoked once wit
44f30 68 20 74 68 65 20 73 65 74 20 6f 66 20 76 6f 69  h the set of voi
44f40 64 2a 20 63 6f 6e 74 65 78 74 20 70 6f 69 6e 74  d* context point
44f50 65 72 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 64  ers.** specified
44f60 20 62 79 20 74 68 65 20 62 6c 6f 63 6b 65 64 20   by the blocked 
44f70 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 62 75 6e 64  connections bund
44f80 6c 65 64 20 74 6f 67 65 74 68 65 72 20 69 6e 74  led together int
44f90 6f 20 61 6e 20 61 72 72 61 79 2e 0a 2a 2a 20 54  o an array..** T
44fa0 68 69 73 20 67 69 76 65 73 20 74 68 65 20 61 70  his gives the ap
44fb0 70 6c 69 63 61 74 69 6f 6e 20 61 6e 20 6f 70 70  plication an opp
44fc0 6f 72 74 75 6e 69 74 79 20 74 6f 20 70 72 69 6f  ortunity to prio
44fd0 72 69 74 69 7a 65 20 61 6e 79 20 61 63 74 69 6f  ritize any actio
44fe0 6e 73 20 0a 2a 2a 20 72 65 6c 61 74 65 64 20 74  ns .** related t
44ff0 6f 20 74 68 65 20 73 65 74 20 6f 66 20 75 6e 62  o the set of unb
45000 6c 6f 63 6b 65 64 20 64 61 74 61 62 61 73 65 20  locked database 
45010 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 0a 2a 2a 0a  connections..**.
45020 2a 2a 20 3c 62 3e 44 65 61 64 6c 6f 63 6b 20 44  ** <b>Deadlock D
45030 65 74 65 63 74 69 6f 6e 3c 2f 62 3e 0a 2a 2a 0a  etection</b>.**.
45040 2a 2a 20 41 73 73 75 6d 69 6e 67 20 74 68 61 74  ** Assuming that
45050 20 61 66 74 65 72 20 72 65 67 69 73 74 65 72 69   after registeri
45060 6e 67 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b  ng for an unlock
45070 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b  -notify callback
45080 20 61 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20   a .** database 
45090 77 61 69 74 73 20 66 6f 72 20 74 68 65 20 63 61  waits for the ca
450a0 6c 6c 62 61 63 6b 20 74 6f 20 62 65 20 69 73 73  llback to be iss
450b0 75 65 64 20 62 65 66 6f 72 65 20 74 61 6b 69 6e  ued before takin
450c0 67 20 61 6e 79 20 66 75 72 74 68 65 72 0a 2a 2a  g any further.**
450d0 20 61 63 74 69 6f 6e 20 28 61 20 72 65 61 73 6f   action (a reaso
450e0 6e 61 62 6c 65 20 61 73 73 75 6d 70 74 69 6f 6e  nable assumption
450f0 29 2c 20 74 68 65 6e 20 75 73 69 6e 67 20 74 68  ), then using th
45100 69 73 20 41 50 49 20 6d 61 79 20 63 61 75 73 65  is API may cause
45110 20 74 68 65 0a 2a 2a 20 61 70 70 6c 69 63 61 74   the.** applicat
45120 69 6f 6e 20 74 6f 20 64 65 61 64 6c 6f 63 6b 2e  ion to deadlock.
45130 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66   For example, if
45140 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 58 20 69 73   connection X is
45150 20 77 61 69 74 69 6e 67 20 66 6f 72 0a 2a 2a 20   waiting for.** 
45160 63 6f 6e 6e 65 63 74 69 6f 6e 20 59 27 73 20 74  connection Y's t
45170 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 62 65  ransaction to be
45180 20 63 6f 6e 63 6c 75 64 65 64 2c 20 61 6e 64 20   concluded, and 
45190 73 69 6d 69 6c 61 72 6c 79 20 63 6f 6e 6e 65 63  similarly connec
451a0 74 69 6f 6e 0a 2a 2a 20 59 20 69 73 20 77 61 69  tion.** Y is wai
451b0 74 69 6e 67 20 6f 6e 20 63 6f 6e 6e 65 63 74 69  ting on connecti
451c0 6f 6e 20 58 27 73 20 74 72 61 6e 73 61 63 74 69  on X's transacti
451d0 6f 6e 2c 20 74 68 65 6e 20 6e 65 69 74 68 65 72  on, then neither
451e0 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 77   connection.** w
451f0 69 6c 6c 20 70 72 6f 63 65 65 64 20 61 6e 64 20  ill proceed and 
45200 74 68 65 20 73 79 73 74 65 6d 20 6d 61 79 20 72  the system may r
45210 65 6d 61 69 6e 20 64 65 61 64 6c 6f 63 6b 65 64  emain deadlocked
45220 20 69 6e 64 65 66 69 6e 69 74 65 6c 79 2e 0a 2a   indefinitely..*
45230 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 74 68  *.** To avoid th
45240 69 73 20 73 63 65 6e 61 72 69 6f 2c 20 74 68 65  is scenario, the
45250 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f   sqlite3_unlock_
45260 6e 6f 74 69 66 79 28 29 20 70 65 72 66 6f 72 6d  notify() perform
45270 73 20 64 65 61 64 6c 6f 63 6b 0a 2a 2a 20 64 65  s deadlock.** de
45280 74 65 63 74 69 6f 6e 2e 20 49 66 20 61 20 67 69  tection. If a gi
45290 76 65 6e 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  ven call to sqli
452a0 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66  te3_unlock_notif
452b0 79 28 29 20 77 6f 75 6c 64 20 70 75 74 20 74 68  y() would put th
452c0 65 0a 2a 2a 20 73 79 73 74 65 6d 20 69 6e 20 61  e.** system in a
452d0 20 64 65 61 64 6c 6f 63 6b 65 64 20 73 74 61 74   deadlocked stat
452e0 65 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 4c  e, then SQLITE_L
452f0 4f 43 4b 45 44 20 69 73 20 72 65 74 75 72 6e 65  OCKED is returne
45300 64 20 61 6e 64 20 6e 6f 0a 2a 2a 20 75 6e 6c 6f  d and no.** unlo
45310 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61  ck-notify callba
45320 63 6b 20 69 73 20 72 65 67 69 73 74 65 72 65 64  ck is registered
45330 2e 20 54 68 65 20 73 79 73 74 65 6d 20 69 73 20  . The system is 
45340 73 61 69 64 20 74 6f 20 62 65 20 69 6e 0a 2a 2a  said to be in.**
45350 20 61 20 64 65 61 64 6c 6f 63 6b 65 64 20 73 74   a deadlocked st
45360 61 74 65 20 69 66 20 63 6f 6e 6e 65 63 74 69 6f  ate if connectio
45370 6e 20 41 20 68 61 73 20 72 65 67 69 73 74 65 72  n A has register
45380 65 64 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b  ed for an unlock
45390 2d 6e 6f 74 69 66 79 0a 2a 2a 20 63 61 6c 6c 62  -notify.** callb
453a0 61 63 6b 20 6f 6e 20 74 68 65 20 63 6f 6e 63 6c  ack on the concl
453b0 75 73 69 6f 6e 20 6f 66 20 63 6f 6e 6e 65 63 74  usion of connect
453c0 69 6f 6e 20 42 27 73 20 74 72 61 6e 73 61 63 74  ion B's transact
453d0 69 6f 6e 2c 20 61 6e 64 20 63 6f 6e 6e 65 63 74  ion, and connect
453e0 69 6f 6e 0a 2a 2a 20 42 20 68 61 73 20 69 74 73  ion.** B has its
453f0 65 6c 66 20 72 65 67 69 73 74 65 72 65 64 20 66  elf registered f
45400 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74  or an unlock-not
45410 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 77 68 65  ify callback whe
45420 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20  n connection.** 
45430 41 27 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  A's transaction 
45440 69 73 20 63 6f 6e 63 6c 75 64 65 64 2e 20 49 6e  is concluded. In
45450 64 69 72 65 63 74 20 64 65 61 64 6c 6f 63 6b 20  direct deadlock 
45460 69 73 20 61 6c 73 6f 20 64 65 74 65 63 74 65 64  is also detected
45470 2c 20 73 6f 0a 2a 2a 20 74 68 65 20 73 79 73 74  , so.** the syst
45480 65 6d 20 69 73 20 61 6c 73 6f 20 63 6f 6e 73 69  em is also consi
45490 64 65 72 65 64 20 74 6f 20 62 65 20 64 65 61 64  dered to be dead
454a0 6c 6f 63 6b 65 64 20 69 66 20 63 6f 6e 6e 65 63  locked if connec
454b0 74 69 6f 6e 20 42 20 68 61 73 0a 2a 2a 20 72 65  tion B has.** re
454c0 67 69 73 74 65 72 65 64 20 66 6f 72 20 61 6e 20  gistered for an 
454d0 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61  unlock-notify ca
454e0 6c 6c 62 61 63 6b 20 6f 6e 20 74 68 65 20 63 6f  llback on the co
454f0 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 63 6f 6e 6e  nclusion of conn
45500 65 63 74 69 6f 6e 0a 2a 2a 20 43 27 73 20 74 72  ection.** C's tr
45510 61 6e 73 61 63 74 69 6f 6e 2c 20 77 68 65 72 65  ansaction, where
45520 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 43 20 69 73   connection C is
45530 20 77 61 69 74 69 6e 67 20 6f 6e 20 63 6f 6e 6e   waiting on conn
45540 65 63 74 69 6f 6e 20 41 2e 20 41 6e 79 0a 2a 2a  ection A. Any.**
45550 20 6e 75 6d 62 65 72 20 6f 66 20 6c 65 76 65 6c   number of level
45560 73 20 6f 66 20 69 6e 64 69 72 65 63 74 69 6f 6e  s of indirection
45570 20 61 72 65 20 61 6c 6c 6f 77 65 64 2e 0a 2a 2a   are allowed..**
45580 0a 2a 2a 20 3c 62 3e 54 68 65 20 22 44 52 4f 50  .** <b>The "DROP
45590 20 54 41 42 4c 45 22 20 45 78 63 65 70 74 69 6f   TABLE" Exceptio
455a0 6e 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  n</b>.**.** When
455b0 20 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69   a call to [sqli
455c0 74 65 33 5f 73 74 65 70 28 29 5d 20 72 65 74 75  te3_step()] retu
455d0 72 6e 73 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  rns SQLITE_LOCKE
455e0 44 2c 20 69 74 20 69 73 20 61 6c 6d 6f 73 74 20  D, it is almost 
455f0 0a 2a 2a 20 61 6c 77 61 79 73 20 61 70 70 72 6f  .** always appro
45600 70 72 69 61 74 65 20 74 6f 20 63 61 6c 6c 20 73  priate to call s
45610 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f  qlite3_unlock_no
45620 74 69 66 79 28 29 2e 20 54 68 65 72 65 20 69 73  tify(). There is
45630 20 68 6f 77 65 76 65 72 2c 0a 2a 2a 20 6f 6e 65   however,.** one
45640 20 65 78 63 65 70 74 69 6f 6e 2e 20 57 68 65 6e   exception. When
45650 20 65 78 65 63 75 74 69 6e 67 20 61 20 22 44 52   executing a "DR
45660 4f 50 20 54 41 42 4c 45 22 20 6f 72 20 22 44 52  OP TABLE" or "DR
45670 4f 50 20 49 4e 44 45 58 22 20 73 74 61 74 65 6d  OP INDEX" statem
45680 65 6e 74 2c 0a 2a 2a 20 53 51 4c 69 74 65 20 63  ent,.** SQLite c
45690 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 61  hecks if there a
456a0 72 65 20 61 6e 79 20 63 75 72 72 65 6e 74 6c 79  re any currently
456b0 20 65 78 65 63 75 74 69 6e 67 20 53 45 4c 45 43   executing SELEC
456c0 54 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20  T statements.** 
456d0 74 68 61 74 20 62 65 6c 6f 6e 67 20 74 6f 20 74  that belong to t
456e0 68 65 20 73 61 6d 65 20 63 6f 6e 6e 65 63 74 69  he same connecti
456f0 6f 6e 2e 20 49 66 20 74 68 65 72 65 20 61 72 65  on. If there are
45700 2c 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20  , SQLITE_LOCKED 
45710 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20  is.** returned. 
45720 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65  In this case the
45730 72 65 20 69 73 20 6e 6f 20 22 62 6c 6f 63 6b 69  re is no "blocki
45740 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 22 2c 20  ng connection", 
45750 73 6f 20 69 6e 76 6f 6b 69 6e 67 0a 2a 2a 20 73  so invoking.** s
45760 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f  qlite3_unlock_no
45770 74 69 66 79 28 29 20 72 65 73 75 6c 74 73 20 69  tify() results i
45780 6e 20 74 68 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74  n the unlock-not
45790 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 62 65 69  ify callback bei
457a0 6e 67 0a 2a 2a 20 69 6e 76 6f 6b 65 64 20 69 6d  ng.** invoked im
457b0 6d 65 64 69 61 74 65 6c 79 2e 20 49 66 20 74 68  mediately. If th
457c0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 68  e application th
457d0 65 6e 20 72 65 2d 61 74 74 65 6d 70 74 73 20 74  en re-attempts t
457e0 68 65 20 22 44 52 4f 50 20 54 41 42 4c 45 22 0a  he "DROP TABLE".
457f0 2a 2a 20 6f 72 20 22 44 52 4f 50 20 49 4e 44 45  ** or "DROP INDE
45800 58 22 20 71 75 65 72 79 2c 20 61 6e 20 69 6e 66  X" query, an inf
45810 69 6e 69 74 65 20 6c 6f 6f 70 20 6d 69 67 68 74  inite loop might
45820 20 62 65 20 74 68 65 20 72 65 73 75 6c 74 2e 0a   be the result..
45830 2a 2a 0a 2a 2a 20 4f 6e 65 20 77 61 79 20 61 72  **.** One way ar
45840 6f 75 6e 64 20 74 68 69 73 20 70 72 6f 62 6c 65  ound this proble
45850 6d 20 69 73 20 74 6f 20 63 68 65 63 6b 20 74 68  m is to check th
45860 65 20 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72  e extended error
45870 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 0a 2a   code returned.*
45880 2a 20 62 79 20 61 6e 20 73 71 6c 69 74 65 33 5f  * by an sqlite3_
45890 73 74 65 70 28 29 20 63 61 6c 6c 2e 20 49 66 20  step() call. If 
458a0 74 68 65 72 65 20 69 73 20 61 20 62 6c 6f 63 6b  there is a block
458b0 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20  ing connection, 
458c0 74 68 65 6e 20 74 68 65 0a 2a 2a 20 65 78 74 65  then the.** exte
458d0 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 20  nded error code 
458e0 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45  is set to SQLITE
458f0 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41  _LOCKED_SHAREDCA
45900 43 48 45 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  CHE. Otherwise, 
45910 69 6e 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 61  in.** the specia
45920 6c 20 22 44 52 4f 50 20 54 41 42 4c 45 2f 49 4e  l "DROP TABLE/IN
45930 44 45 58 22 20 63 61 73 65 2c 20 74 68 65 20 65  DEX" case, the e
45940 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f  xtended error co
45950 64 65 20 69 73 20 6a 75 73 74 20 0a 2a 2a 20 53  de is just .** S
45960 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f  QLITE_LOCKED..*/
45970 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
45980 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e  sqlite3_unlock_n
45990 6f 74 69 66 79 28 0a 20 20 73 71 6c 69 74 65 33  otify(.  sqlite3
459a0 20 2a 70 42 6c 6f 63 6b 65 64 2c 20 20 20 20 20   *pBlocked,     
459b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
459c0 20 20 20 20 20 2f 2a 20 57 61 69 74 69 6e 67 20       /* Waiting 
459d0 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
459e0 76 6f 69 64 20 28 2a 78 4e 6f 74 69 66 79 29 28  void (*xNotify)(
459f0 76 6f 69 64 20 2a 2a 61 70 41 72 67 2c 20 69 6e  void **apArg, in
45a00 74 20 6e 41 72 67 29 2c 20 20 20 20 2f 2a 20 43  t nArg),    /* C
45a10 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
45a20 20 74 6f 20 69 6e 76 6f 6b 65 20 2a 2f 0a 20 20   to invoke */.  
45a30 76 6f 69 64 20 2a 70 4e 6f 74 69 66 79 41 72 67  void *pNotifyArg
45a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45a50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
45a60 72 67 75 6d 65 6e 74 20 74 6f 20 70 61 73 73 20  rgument to pass 
45a70 74 6f 20 78 4e 6f 74 69 66 79 20 2a 2f 0a 29 3b  to xNotify */.);
45a80 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  .../*.** CAPI3RE
45a90 46 3a 20 53 74 72 69 6e 67 20 43 6f 6d 70 61 72  F: String Compar
45aa0 69 73 6f 6e 0a 2a 2a 20 45 58 50 45 52 49 4d 45  ison.** EXPERIME
45ab0 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b  NTAL.**.** The [
45ac0 73 71 6c 69 74 65 33 5f 73 74 72 6e 69 63 6d 70  sqlite3_strnicmp
45ad0 28 29 5d 20 41 50 49 20 61 6c 6c 6f 77 73 20 61  ()] API allows a
45ae0 70 70 6c 69 63 61 74 69 6f 6e 73 20 61 6e 64 20  pplications and 
45af0 65 78 74 65 6e 73 69 6f 6e 73 20 74 6f 0a 2a 2a  extensions to.**
45b00 20 63 6f 6d 70 61 72 65 20 74 68 65 20 63 6f 6e   compare the con
45b10 74 65 6e 74 73 20 6f 66 20 74 77 6f 20 62 75 66  tents of two buf
45b20 66 65 72 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20  fers containing 
45b30 55 54 46 2d 38 20 73 74 72 69 6e 67 73 20 69 6e  UTF-8 strings in
45b40 20 61 0a 2a 2a 20 63 61 73 65 2d 69 6e 64 65 6e   a.** case-inden
45b50 64 65 6e 74 20 66 61 73 68 69 6f 6e 2c 20 75 73  dent fashion, us
45b60 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 65 66  ing the same def
45b70 69 6e 69 74 69 6f 6e 20 6f 66 20 63 61 73 65 20  inition of case 
45b80 69 6e 64 65 70 65 6e 64 65 6e 63 65 20 0a 2a 2a  independence .**
45b90 20 74 68 61 74 20 53 51 4c 69 74 65 20 75 73 65   that SQLite use
45ba0 73 20 69 6e 74 65 72 6e 61 6c 6c 79 20 77 68 65  s internally whe
45bb0 6e 20 63 6f 6d 70 61 72 69 6e 67 20 69 64 65 6e  n comparing iden
45bc0 74 69 66 69 65 72 73 2e 0a 2a 2f 0a 53 51 4c 49  tifiers..*/.SQLI
45bd0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
45be0 65 33 5f 73 74 72 6e 69 63 6d 70 28 63 6f 6e 73  e3_strnicmp(cons
45bf0 74 20 63 68 61 72 20 2a 2c 20 63 6f 6e 73 74 20  t char *, const 
45c00 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 0a 2f  char *, int);../
45c10 2a 0a 2a 2a 20 55 6e 64 6f 20 74 68 65 20 68 61  *.** Undo the ha
45c20 63 6b 20 74 68 61 74 20 63 6f 6e 76 65 72 74 73  ck that converts
45c30 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20   floating point 
45c40 74 79 70 65 73 20 74 6f 20 69 6e 74 65 67 65 72  types to integer
45c50 20 66 6f 72 0a 2a 2a 20 62 75 69 6c 64 73 20 6f   for.** builds o
45c60 6e 20 70 72 6f 63 65 73 73 6f 72 73 20 77 69 74  n processors wit
45c70 68 6f 75 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f  hout floating po
45c80 69 6e 74 20 73 75 70 70 6f 72 74 2e 0a 2a 2f 0a  int support..*/.
45c90 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
45ca0 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
45cb0 54 0a 23 20 75 6e 64 65 66 20 64 6f 75 62 6c 65  T.# undef double
45cc0 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 30 0a 7d  .#endif..#if 0.}
45cd0 20 20 2f 2a 20 45 6e 64 20 6f 66 20 74 68 65 20    /* End of the 
45ce0 27 65 78 74 65 72 6e 20 22 43 22 27 20 62 6c 6f  'extern "C"' blo
45cf0 63 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 65 6e  ck */.#endif.#en
45d00 64 69 66 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  dif.../*********
45d10 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c  ***** End of sql
45d20 69 74 65 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  ite3.h *********
45d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45d50 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
45d60 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
45d70 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
45d80 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e  ff in sqliteInt.
45d90 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
45da0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
45db0 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 61  ***** Include ha
45dc0 73 68 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  sh.h in the midd
45dd0 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e  le of sqliteInt.
45de0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
45df0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
45e00 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
45e10 20 68 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a   hash.h ********
45e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45e40 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31  ****/./*.** 2001
45e50 20 53 65 70 74 65 6d 62 65 72 20 32 32 0a 2a 2a   September 22.**
45e60 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
45e70 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
45e80 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
45e90 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
45ea0 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
45eb0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
45ec0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
45ed0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
45ee0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
45ef0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
45f00 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
45f10 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
45f20 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
45f30 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
45f40 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
45f50 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
45f60 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
45f70 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
45f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
45fc0 54 68 69 73 20 69 73 20 74 68 65 20 68 65 61 64  This is the head
45fd0 65 72 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20  er file for the 
45fe0 67 65 6e 65 72 69 63 20 68 61 73 68 2d 74 61 62  generic hash-tab
45ff0 6c 65 20 69 6d 70 6c 65 6d 65 6e 61 74 69 6f 6e  le implemenation
46000 0a 2a 2a 20 75 73 65 64 20 69 6e 20 53 51 4c 69  .** used in SQLi
46010 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68  te..**.** $Id: h
46020 61 73 68 2e 68 2c 76 20 31 2e 31 35 20 32 30 30  ash.h,v 1.15 200
46030 39 2f 30 35 2f 30 32 20 31 33 3a 32 39 3a 33 38  9/05/02 13:29:38
46040 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69   drh Exp $.*/.#i
46050 66 6e 64 65 66 20 5f 53 51 4c 49 54 45 5f 48 41  fndef _SQLITE_HA
46060 53 48 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53  SH_H_.#define _S
46070 51 4c 49 54 45 5f 48 41 53 48 5f 48 5f 0a 0a 2f  QLITE_HASH_H_../
46080 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72  * Forward declar
46090 61 74 69 6f 6e 73 20 6f 66 20 73 74 72 75 63 74  ations of struct
460a0 75 72 65 73 2e 20 2a 2f 0a 74 79 70 65 64 65 66  ures. */.typedef
460b0 20 73 74 72 75 63 74 20 48 61 73 68 20 48 61 73   struct Hash Has
460c0 68 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  h;.typedef struc
460d0 74 20 48 61 73 68 45 6c 65 6d 20 48 61 73 68 45  t HashElem HashE
460e0 6c 65 6d 3b 0a 0a 2f 2a 20 41 20 63 6f 6d 70 6c  lem;../* A compl
460f0 65 74 65 20 68 61 73 68 20 74 61 62 6c 65 20 69  ete hash table i
46100 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  s an instance of
46110 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
46120 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 65  tructure..** The
46130 20 69 6e 74 65 72 6e 61 6c 73 20 6f 66 20 74 68   internals of th
46140 69 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65  is structure are
46150 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20   intended to be 
46160 6f 70 61 71 75 65 20 2d 2d 20 63 6c 69 65 6e 74  opaque -- client
46170 0a 2a 2a 20 63 6f 64 65 20 73 68 6f 75 6c 64 20  .** code should 
46180 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 61  not attempt to a
46190 63 63 65 73 73 20 6f 72 20 6d 6f 64 69 66 79 20  ccess or modify 
461a0 74 68 65 20 66 69 65 6c 64 73 20 6f 66 20 74 68  the fields of th
461b0 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  is structure.** 
461c0 64 69 72 65 63 74 6c 79 2e 20 20 43 68 61 6e 67  directly.  Chang
461d0 65 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  e this structure
461e0 20 6f 6e 6c 79 20 62 79 20 75 73 69 6e 67 20 74   only by using t
461f0 68 65 20 72 6f 75 74 69 6e 65 73 20 62 65 6c 6f  he routines belo
46200 77 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 73  w..** However, s
46210 6f 6d 65 20 6f 66 20 74 68 65 20 22 70 72 6f 63  ome of the "proc
46220 65 64 75 72 65 73 22 20 61 6e 64 20 22 66 75 6e  edures" and "fun
46230 63 74 69 6f 6e 73 22 20 66 6f 72 20 6d 6f 64 69  ctions" for modi
46240 66 79 69 6e 67 20 61 6e 64 0a 2a 2a 20 61 63 63  fying and.** acc
46250 65 73 73 69 6e 67 20 74 68 69 73 20 73 74 72 75  essing this stru
46260 63 74 75 72 65 20 61 72 65 20 72 65 61 6c 6c 79  cture are really
46270 20 6d 61 63 72 6f 73 2c 20 73 6f 20 77 65 20 63   macros, so we c
46280 61 6e 27 74 20 72 65 61 6c 6c 79 20 6d 61 6b 65  an't really make
46290 0a 2a 2a 20 74 68 69 73 20 73 74 72 75 63 74 75  .** this structu
462a0 72 65 20 6f 70 61 71 75 65 2e 0a 2a 2a 0a 2a 2a  re opaque..**.**
462b0 20 41 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66   All elements of
462c0 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
462d0 61 72 65 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20  are on a single 
462e0 64 6f 75 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69  doubly-linked li
462f0 73 74 2e 0a 2a 2a 20 48 61 73 68 2e 66 69 72 73  st..** Hash.firs
46300 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20  t points to the 
46310 68 65 61 64 20 6f 66 20 74 68 69 73 20 6c 69 73  head of this lis
46320 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61  t..**.** There a
46330 72 65 20 48 61 73 68 2e 68 74 73 69 7a 65 20 62  re Hash.htsize b
46340 75 63 6b 65 74 73 2e 20 20 45 61 63 68 20 62 75  uckets.  Each bu
46350 63 6b 65 74 20 70 6f 69 6e 74 73 20 74 6f 20 61  cket points to a
46360 20 73 70 6f 74 20 69 6e 0a 2a 2a 20 74 68 65 20   spot in.** the 
46370 67 6c 6f 62 61 6c 20 64 6f 75 62 6c 79 2d 6c 69  global doubly-li
46380 6e 6b 65 64 20 6c 69 73 74 2e 20 20 54 68 65 20  nked list.  The 
46390 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
463a0 62 75 63 6b 65 74 20 61 72 65 20 74 68 65 0a 2a  bucket are the.*
463b0 2a 20 65 6c 65 6d 65 6e 74 20 70 6f 69 6e 74 65  * element pointe
463c0 64 20 74 6f 20 70 6c 75 73 20 74 68 65 20 6e 65  d to plus the ne
463d0 78 74 20 5f 68 74 2e 63 6f 75 6e 74 2d 31 20 65  xt _ht.count-1 e
463e0 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 6c  lements in the l
463f0 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 48 61 73 68 2e  ist..**.** Hash.
46400 68 74 73 69 7a 65 20 61 6e 64 20 48 61 73 68 2e  htsize and Hash.
46410 68 74 20 6d 61 79 20 62 65 20 7a 65 72 6f 2e 20  ht may be zero. 
46420 20 49 6e 20 74 68 61 74 20 63 61 73 65 20 6c 6f   In that case lo
46430 6f 6b 75 70 20 69 73 20 64 6f 6e 65 0a 2a 2a 20  okup is done.** 
46440 62 79 20 61 20 6c 69 6e 65 61 72 20 73 65 61 72  by a linear sear
46450 63 68 20 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c  ch of the global
46460 20 6c 69 73 74 2e 20 20 46 6f 72 20 73 6d 61 6c   list.  For smal
46470 6c 20 74 61 62 6c 65 73 2c 20 74 68 65 20 0a 2a  l tables, the .*
46480 2a 20 48 61 73 68 2e 68 74 20 74 61 62 6c 65 20  * Hash.ht table 
46490 69 73 20 6e 65 76 65 72 20 61 6c 6c 6f 63 61 74  is never allocat
464a0 65 64 20 62 65 63 61 75 73 65 20 69 66 20 74 68  ed because if th
464b0 65 72 65 20 61 72 65 20 66 65 77 20 65 6c 65 6d  ere are few elem
464c0 65 6e 74 73 0a 2a 2a 20 69 6e 20 74 68 65 20 74  ents.** in the t
464d0 61 62 6c 65 2c 20 69 74 20 69 73 20 66 61 73 74  able, it is fast
464e0 65 72 20 74 6f 20 64 6f 20 61 20 6c 69 6e 65 61  er to do a linea
464f0 72 20 73 65 61 72 63 68 20 74 68 61 6e 20 74 6f  r search than to
46500 20 6d 61 6e 61 67 65 0a 2a 2a 20 74 68 65 20 68   manage.** the h
46510 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74  ash table..*/.st
46520 72 75 63 74 20 48 61 73 68 20 7b 0a 20 20 75 6e  ruct Hash {.  un
46530 73 69 67 6e 65 64 20 69 6e 74 20 68 74 73 69 7a  signed int htsiz
46540 65 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  e;      /* Numbe
46550 72 20 6f 66 20 62 75 63 6b 65 74 73 20 69 6e 20  r of buckets in 
46560 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 2a  the hash table *
46570 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  /.  unsigned int
46580 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20 2f 2a   count;       /*
46590 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
465a0 65 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65  es in this table
465b0 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a   */.  HashElem *
465c0 66 69 72 73 74 3b 20 20 20 20 20 20 20 20 20 20  first;          
465d0 2f 2a 20 54 68 65 20 66 69 72 73 74 20 65 6c 65  /* The first ele
465e0 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 72 72 61  ment of the arra
465f0 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 5f 68  y */.  struct _h
46600 74 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  t {             
46610 20 2f 2a 20 74 68 65 20 68 61 73 68 20 74 61 62   /* the hash tab
46620 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 63 6f  le */.    int co
46630 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  unt;            
46640 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
46650 66 20 65 6e 74 72 69 65 73 20 77 69 74 68 20 74  f entries with t
46660 68 69 73 20 68 61 73 68 20 2a 2f 0a 20 20 20 20  his hash */.    
46670 48 61 73 68 45 6c 65 6d 20 2a 63 68 61 69 6e 3b  HashElem *chain;
46680 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f             /* Po
46690 69 6e 74 65 72 20 74 6f 20 66 69 72 73 74 20 65  inter to first e
466a0 6e 74 72 79 20 77 69 74 68 20 74 68 69 73 20 68  ntry with this h
466b0 61 73 68 20 2a 2f 0a 20 20 7d 20 2a 68 74 3b 0a  ash */.  } *ht;.
466c0 7d 3b 0a 0a 2f 2a 20 45 61 63 68 20 65 6c 65 6d  };../* Each elem
466d0 65 6e 74 20 69 6e 20 74 68 65 20 68 61 73 68 20  ent in the hash 
466e0 74 61 62 6c 65 20 69 73 20 61 6e 20 69 6e 73 74  table is an inst
466f0 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
46700 6f 77 69 6e 67 20 0a 2a 2a 20 73 74 72 75 63 74  owing .** struct
46710 75 72 65 2e 20 20 41 6c 6c 20 65 6c 65 6d 65 6e  ure.  All elemen
46720 74 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e  ts are stored on
46730 20 61 20 73 69 6e 67 6c 65 20 64 6f 75 62 6c 79   a single doubly
46740 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a 2a  -linked list..**
46750 0a 2a 2a 20 41 67 61 69 6e 2c 20 74 68 69 73 20  .** Again, this 
46760 73 74 72 75 63 74 75 72 65 20 69 73 20 69 6e 74  structure is int
46770 65 6e 64 65 64 20 74 6f 20 62 65 20 6f 70 61 71  ended to be opaq
46780 75 65 2c 20 62 75 74 20 69 74 20 63 61 6e 27 74  ue, but it can't
46790 20 72 65 61 6c 6c 79 0a 2a 2a 20 62 65 20 6f 70   really.** be op
467a0 61 71 75 65 20 62 65 63 61 75 73 65 20 69 74 20  aque because it 
467b0 69 73 20 75 73 65 64 20 62 79 20 6d 61 63 72 6f  is used by macro
467c0 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 48 61 73  s..*/.struct Has
467d0 68 45 6c 65 6d 20 7b 0a 20 20 48 61 73 68 45 6c  hElem {.  HashEl
467e0 65 6d 20 2a 6e 65 78 74 2c 20 2a 70 72 65 76 3b  em *next, *prev;
467f0 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 61         /* Next a
46800 6e 64 20 70 72 65 76 69 6f 75 73 20 65 6c 65 6d  nd previous elem
46810 65 6e 74 73 20 69 6e 20 74 68 65 20 74 61 62 6c  ents in the tabl
46820 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 64 61 74  e */.  void *dat
46830 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  a;              
46840 20 20 20 20 2f 2a 20 44 61 74 61 20 61 73 73 6f      /* Data asso
46850 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73  ciated with this
46860 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f   element */.  co
46870 6e 73 74 20 63 68 61 72 20 2a 70 4b 65 79 3b 20  nst char *pKey; 
46880 69 6e 74 20 6e 4b 65 79 3b 20 20 2f 2a 20 4b 65  int nKey;  /* Ke
46890 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  y associated wit
468a0 68 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a  h this element *
468b0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 63 63 65  /.};../*.** Acce
468c0 73 73 20 72 6f 75 74 69 6e 65 73 2e 20 20 54 6f  ss routines.  To
468d0 20 64 65 6c 65 74 65 2c 20 69 6e 73 65 72 74 20   delete, insert 
468e0 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a  a NULL pointer..
468f0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
46900 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61  E void sqlite3Ha
46910 73 68 49 6e 69 74 28 48 61 73 68 2a 29 3b 0a 53  shInit(Hash*);.S
46920 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
46930 69 64 20 2a 73 71 6c 69 74 65 33 48 61 73 68 49  id *sqlite3HashI
46940 6e 73 65 72 74 28 48 61 73 68 2a 2c 20 63 6f 6e  nsert(Hash*, con
46950 73 74 20 63 68 61 72 20 2a 70 4b 65 79 2c 20 69  st char *pKey, i
46960 6e 74 20 6e 4b 65 79 2c 20 76 6f 69 64 20 2a 70  nt nKey, void *p
46970 44 61 74 61 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Data);.SQLITE_PR
46980 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
46990 74 65 33 48 61 73 68 46 69 6e 64 28 63 6f 6e 73  te3HashFind(cons
469a0 74 20 48 61 73 68 2a 2c 20 63 6f 6e 73 74 20 63  t Hash*, const c
469b0 68 61 72 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e  har *pKey, int n
469c0 4b 65 79 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  Key);.SQLITE_PRI
469d0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
469e0 33 48 61 73 68 43 6c 65 61 72 28 48 61 73 68 2a  3HashClear(Hash*
469f0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73  );../*.** Macros
46a00 20 66 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65   for looping ove
46a10 72 20 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f  r all elements o
46a20 66 20 61 20 68 61 73 68 20 74 61 62 6c 65 2e 20  f a hash table. 
46a30 20 54 68 65 20 69 64 69 6f 6d 20 69 73 0a 2a 2a   The idiom is.**
46a40 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a   like this:.**.*
46a50 2a 20 20 20 48 61 73 68 20 68 3b 0a 2a 2a 20 20  *   Hash h;.**  
46a60 20 48 61 73 68 45 6c 65 6d 20 2a 70 3b 0a 2a 2a   HashElem *p;.**
46a70 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 66 6f 72 28     ....**   for(
46a80 70 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73  p=sqliteHashFirs
46a90 74 28 26 68 29 3b 20 70 3b 20 70 3d 73 71 6c 69  t(&h); p; p=sqli
46aa0 74 65 48 61 73 68 4e 65 78 74 28 70 29 29 7b 0a  teHashNext(p)){.
46ab0 2a 2a 20 20 20 20 20 53 6f 6d 65 53 74 72 75 63  **     SomeStruc
46ac0 74 75 72 65 20 2a 70 44 61 74 61 20 3d 20 73 71  ture *pData = sq
46ad0 6c 69 74 65 48 61 73 68 44 61 74 61 28 70 29 3b  liteHashData(p);
46ae0 0a 2a 2a 20 20 20 20 20 2f 2f 20 64 6f 20 73 6f  .**     // do so
46af0 6d 65 74 68 69 6e 67 20 77 69 74 68 20 70 44 61  mething with pDa
46b00 74 61 0a 2a 2a 20 20 20 7d 0a 2a 2f 0a 23 64 65  ta.**   }.*/.#de
46b10 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68 46  fine sqliteHashF
46b20 69 72 73 74 28 48 29 20 20 28 28 48 29 2d 3e 66  irst(H)  ((H)->f
46b30 69 72 73 74 29 0a 23 64 65 66 69 6e 65 20 73 71  irst).#define sq
46b40 6c 69 74 65 48 61 73 68 4e 65 78 74 28 45 29 20  liteHashNext(E) 
46b50 20 20 28 28 45 29 2d 3e 6e 65 78 74 29 0a 23 64    ((E)->next).#d
46b60 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68  efine sqliteHash
46b70 44 61 74 61 28 45 29 20 20 20 28 28 45 29 2d 3e  Data(E)   ((E)->
46b80 64 61 74 61 29 0a 2f 2a 20 23 64 65 66 69 6e 65  data)./* #define
46b90 20 73 71 6c 69 74 65 48 61 73 68 4b 65 79 28 45   sqliteHashKey(E
46ba0 29 20 20 20 20 28 28 45 29 2d 3e 70 4b 65 79 29  )    ((E)->pKey)
46bb0 20 2f 2f 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0a   // NOT USED */.
46bc0 2f 2a 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74  /* #define sqlit
46bd0 65 48 61 73 68 4b 65 79 73 69 7a 65 28 45 29 20  eHashKeysize(E) 
46be0 28 28 45 29 2d 3e 6e 4b 65 79 29 20 20 2f 2f 20  ((E)->nKey)  // 
46bf0 4e 4f 54 20 55 53 45 44 20 2a 2f 0a 0a 2f 2a 0a  NOT USED */../*.
46c00 2a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74  ** Number of ent
46c10 72 69 65 73 20 69 6e 20 61 20 68 61 73 68 20 74  ries in a hash t
46c20 61 62 6c 65 0a 2a 2f 0a 2f 2a 20 23 64 65 66 69  able.*/./* #defi
46c30 6e 65 20 73 71 6c 69 74 65 48 61 73 68 43 6f 75  ne sqliteHashCou
46c40 6e 74 28 48 29 20 20 28 28 48 29 2d 3e 63 6f 75  nt(H)  ((H)->cou
46c50 6e 74 29 20 2f 2f 20 4e 4f 54 20 55 53 45 44 20  nt) // NOT USED 
46c60 2a 2f 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 53  */..#endif /* _S
46c70 51 4c 49 54 45 5f 48 41 53 48 5f 48 5f 20 2a 2f  QLITE_HASH_H_ */
46c80 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
46c90 2a 20 45 6e 64 20 6f 66 20 68 61 73 68 2e 68 20  * End of hash.h 
46ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46cd0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
46ce0 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
46cf0 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
46d00 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a  n sqliteInt.h **
46d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46d20 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
46d30 2a 20 49 6e 63 6c 75 64 65 20 70 61 72 73 65 2e  * Include parse.
46d40 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
46d50 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a  of sqliteInt.h *
46d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46d70 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
46d80 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 61 72  * Begin file par
46d90 73 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  se.h ***********
46da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46dc0 2f 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 45 4d  /.#define TK_SEM
46dd0 49 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  I               
46de0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 0a 23               1.#
46df0 64 65 66 69 6e 65 20 54 4b 5f 45 58 50 4c 41 49  define TK_EXPLAI
46e00 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  N               
46e10 20 20 20 20 20 20 20 20 20 20 32 0a 23 64 65 66            2.#def
46e20 69 6e 65 20 54 4b 5f 51 55 45 52 59 20 20 20 20  ine TK_QUERY    
46e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46e40 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e 65         3.#define
46e50 20 54 4b 5f 50 4c 41 4e 20 20 20 20 20 20 20 20   TK_PLAN        
46e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46e70 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 54 4b      4.#define TK
46e80 5f 42 45 47 49 4e 20 20 20 20 20 20 20 20 20 20  _BEGIN          
46e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46ea0 20 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 52   5.#define TK_TR
46eb0 41 4e 53 41 43 54 49 4f 4e 20 20 20 20 20 20 20  ANSACTION       
46ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 0a                6.
46ed0 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 46 45 52  #define TK_DEFER
46ee0 52 45 44 20 20 20 20 20 20 20 20 20 20 20 20 20  RED             
46ef0 20 20 20 20 20 20 20 20 20 20 20 37 0a 23 64 65             7.#de
46f00 66 69 6e 65 20 54 4b 5f 49 4d 4d 45 44 49 41 54  fine TK_IMMEDIAT
46f10 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
46f20 20 20 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e          8.#defin
46f30 65 20 54 4b 5f 45 58 43 4c 55 53 49 56 45 20 20  e TK_EXCLUSIVE  
46f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46f50 20 20 20 20 20 39 0a 23 64 65 66 69 6e 65 20 54       9.#define T
46f60 4b 5f 43 4f 4d 4d 49 54 20 20 20 20 20 20 20 20  K_COMMIT        
46f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46f80 20 31 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45   10.#define TK_E
46f90 4e 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ND              
46fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31                11
46fb0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 4f 4c 4c  .#define TK_ROLL
46fc0 42 41 43 4b 20 20 20 20 20 20 20 20 20 20 20 20  BACK            
46fd0 20 20 20 20 20 20 20 20 20 20 20 31 32 0a 23 64             12.#d
46fe0 65 66 69 6e 65 20 54 4b 5f 53 41 56 45 50 4f 49  efine TK_SAVEPOI
46ff0 4e 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  NT              
47000 20 20 20 20 20 20 20 20 31 33 0a 23 64 65 66 69          13.#defi
47010 6e 65 20 54 4b 5f 52 45 4c 45 41 53 45 20 20 20  ne TK_RELEASE   
47020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47030 20 20 20 20 20 31 34 0a 23 64 65 66 69 6e 65 20       14.#define 
47040 54 4b 5f 54 4f 20 20 20 20 20 20 20 20 20 20 20  TK_TO           
47050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47060 20 20 31 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f    15.#define TK_
47070 54 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 20  TABLE           
47080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
47090 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 52 45  6.#define TK_CRE
470a0 41 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ATE             
470b0 20 20 20 20 20 20 20 20 20 20 20 20 31 37 0a 23              17.#
470c0 64 65 66 69 6e 65 20 54 4b 5f 49 46 20 20 20 20  define TK_IF    
470d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
470e0 20 20 20 20 20 20 20 20 20 31 38 0a 23 64 65 66           18.#def
470f0 69 6e 65 20 54 4b 5f 4e 4f 54 20 20 20 20 20 20  ine TK_NOT      
47100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47110 20 20 20 20 20 20 31 39 0a 23 64 65 66 69 6e 65        19.#define
47120 20 54 4b 5f 45 58 49 53 54 53 20 20 20 20 20 20   TK_EXISTS      
47130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47140 20 20 20 32 30 0a 23 64 65 66 69 6e 65 20 54 4b     20.#define TK
47150 5f 54 45 4d 50 20 20 20 20 20 20 20 20 20 20 20  _TEMP           
47160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47170 32 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 50  21.#define TK_LP
47180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47190 20 20 20 20 20 20 20 20 20 20 20 20 20 32 32 0a               22.
471a0 23 64 65 66 69 6e 65 20 54 4b 5f 52 50 20 20 20  #define TK_RP   
471b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
471c0 20 20 20 20 20 20 20 20 20 20 32 33 0a 23 64 65            23.#de
471d0 66 69 6e 65 20 54 4b 5f 41 53 20 20 20 20 20 20  fine TK_AS      
471e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
471f0 20 20 20 20 20 20 20 32 34 0a 23 64 65 66 69 6e         24.#defin
47200 65 20 54 4b 5f 43 4f 4d 4d 41 20 20 20 20 20 20  e TK_COMMA      
47210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47220 20 20 20 20 32 35 0a 23 64 65 66 69 6e 65 20 54      25.#define T
47230 4b 5f 49 44 20 20 20 20 20 20 20 20 20 20 20 20  K_ID            
47240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47250 20 32 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49   26.#define TK_I
47260 4e 44 45 58 45 44 20 20 20 20 20 20 20 20 20 20  NDEXED          
47270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 37                27
47280 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 42 4f 52  .#define TK_ABOR
47290 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
472a0 20 20 20 20 20 20 20 20 20 20 20 32 38 0a 23 64             28.#d
472b0 65 66 69 6e 65 20 54 4b 5f 41 46 54 45 52 20 20  efine TK_AFTER  
472c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
472d0 20 20 20 20 20 20 20 20 32 39 0a 23 64 65 66 69          29.#defi
472e0 6e 65 20 54 4b 5f 41 4e 41 4c 59 5a 45 20 20 20  ne TK_ANALYZE   
472f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47300 20 20 20 20 20 33 30 0a 23 64 65 66 69 6e 65 20       30.#define 
47310 54 4b 5f 41 53 43 20 20 20 20 20 20 20 20 20 20  TK_ASC          
47320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47330 20 20 33 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f    31.#define TK_
47340 41 54 54 41 43 48 20 20 20 20 20 20 20 20 20 20  ATTACH          
47350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33                 3
47360 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 45 46  2.#define TK_BEF
47370 4f 52 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ORE             
47380 20 20 20 20 20 20 20 20 20 20 20 20 33 33 0a 23              33.#
47390 64 65 66 69 6e 65 20 54 4b 5f 42 59 20 20 20 20  define TK_BY    
473a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
473b0 20 20 20 20 20 20 20 20 20 33 34 0a 23 64 65 66           34.#def
473c0 69 6e 65 20 54 4b 5f 43 41 53 43 41 44 45 20 20  ine TK_CASCADE  
473d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
473e0 20 20 20 20 20 20 33 35 0a 23 64 65 66 69 6e 65        35.#define
473f0 20 54 4b 5f 43 41 53 54 20 20 20 20 20 20 20 20   TK_CAST        
47400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47410 20 20 20 33 36 0a 23 64 65 66 69 6e 65 20 54 4b     36.#define TK
47420 5f 43 4f 4c 55 4d 4e 4b 57 20 20 20 20 20 20 20  _COLUMNKW       
47430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47440 33 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f  37.#define TK_CO
47450 4e 46 4c 49 43 54 20 20 20 20 20 20 20 20 20 20  NFLICT          
47460 20 20 20 20 20 20 20 20 20 20 20 20 20 33 38 0a               38.
47470 23 64 65 66 69 6e 65 20 54 4b 5f 44 41 54 41 42  #define TK_DATAB
47480 41 53 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ASE             
47490 20 20 20 20 20 20 20 20 20 20 33 39 0a 23 64 65            39.#de
474a0 66 69 6e 65 20 54 4b 5f 44 45 53 43 20 20 20 20  fine TK_DESC    
474b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
474c0 20 20 20 20 20 20 20 34 30 0a 23 64 65 66 69 6e         40.#defin
474d0 65 20 54 4b 5f 44 45 54 41 43 48 20 20 20 20 20  e TK_DETACH     
474e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
474f0 20 20 20 20 34 31 0a 23 64 65 66 69 6e 65 20 54      41.#define T
47500 4b 5f 45 41 43 48 20 20 20 20 20 20 20 20 20 20  K_EACH          
47510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47520 20 34 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46   42.#define TK_F
47530 41 49 4c 20 20 20 20 20 20 20 20 20 20 20 20 20  AIL             
47540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 33                43
47550 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 4f 52 20  .#define TK_FOR 
47560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47570 20 20 20 20 20 20 20 20 20 20 20 34 34 0a 23 64             44.#d
47580 65 66 69 6e 65 20 54 4b 5f 49 47 4e 4f 52 45 20  efine TK_IGNORE 
47590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
475a0 20 20 20 20 20 20 20 20 34 35 0a 23 64 65 66 69          45.#defi
475b0 6e 65 20 54 4b 5f 49 4e 49 54 49 41 4c 4c 59 20  ne TK_INITIALLY 
475c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
475d0 20 20 20 20 20 34 36 0a 23 64 65 66 69 6e 65 20       46.#define 
475e0 54 4b 5f 49 4e 53 54 45 41 44 20 20 20 20 20 20  TK_INSTEAD      
475f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47600 20 20 34 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f    47.#define TK_
47610 4c 49 4b 45 5f 4b 57 20 20 20 20 20 20 20 20 20  LIKE_KW         
47620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34                 4
47630 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4d 41 54  8.#define TK_MAT
47640 43 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20  CH              
47650 20 20 20 20 20 20 20 20 20 20 20 20 34 39 0a 23              49.#
47660 64 65 66 69 6e 65 20 54 4b 5f 4b 45 59 20 20 20  define TK_KEY   
47670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47680 20 20 20 20 20 20 20 20 20 35 30 0a 23 64 65 66           50.#def
47690 69 6e 65 20 54 4b 5f 4f 46 20 20 20 20 20 20 20  ine TK_OF       
476a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
476b0 20 20 20 20 20 20 35 31 0a 23 64 65 66 69 6e 65        51.#define
476c0 20 54 4b 5f 4f 46 46 53 45 54 20 20 20 20 20 20   TK_OFFSET      
476d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
476e0 20 20 20 35 32 0a 23 64 65 66 69 6e 65 20 54 4b     52.#define TK
476f0 5f 50 52 41 47 4d 41 20 20 20 20 20 20 20 20 20  _PRAGMA         
47700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47710 35 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 41  53.#define TK_RA
47720 49 53 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ISE             
47730 20 20 20 20 20 20 20 20 20 20 20 20 20 35 34 0a               54.
47740 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 50 4c 41  #define TK_REPLA
47750 43 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  CE              
47760 20 20 20 20 20 20 20 20 20 20 35 35 0a 23 64 65            55.#de
47770 66 69 6e 65 20 54 4b 5f 52 45 53 54 52 49 43 54  fine TK_RESTRICT
47780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47790 20 20 20 20 20 20 20 35 36 0a 23 64 65 66 69 6e         56.#defin
477a0 65 20 54 4b 5f 52 4f 57 20 20 20 20 20 20 20 20  e TK_ROW        
477b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
477c0 20 20 20 20 35 37 0a 23 64 65 66 69 6e 65 20 54      57.#define T
477d0 4b 5f 54 52 49 47 47 45 52 20 20 20 20 20 20 20  K_TRIGGER       
477e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
477f0 20 35 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56   58.#define TK_V
47800 41 43 55 55 4d 20 20 20 20 20 20 20 20 20 20 20  ACUUM           
47810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 39                59
47820 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 49 45 57  .#define TK_VIEW
47830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47840 20 20 20 20 20 20 20 20 20 20 20 36 30 0a 23 64             60.#d
47850 65 66 69 6e 65 20 54 4b 5f 56 49 52 54 55 41 4c  efine TK_VIRTUAL
47860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47870 20 20 20 20 20 20 20 20 36 31 0a 23 64 65 66 69          61.#defi
47880 6e 65 20 54 4b 5f 52 45 49 4e 44 45 58 20 20 20  ne TK_REINDEX   
47890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
478a0 20 20 20 20 20 36 32 0a 23 64 65 66 69 6e 65 20       62.#define 
478b0 54 4b 5f 52 45 4e 41 4d 45 20 20 20 20 20 20 20  TK_RENAME       
478c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
478d0 20 20 36 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f    63.#define TK_
478e0 43 54 49 4d 45 5f 4b 57 20 20 20 20 20 20 20 20  CTIME_KW        
478f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36                 6
47900 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 4e 59  4.#define TK_ANY
47910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47920 20 20 20 20 20 20 20 20 20 20 20 20 36 35 0a 23              65.#
47930 64 65 66 69 6e 65 20 54 4b 5f 4f 52 20 20 20 20  define TK_OR    
47940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47950 20 20 20 20 20 20 20 20 20 36 36 0a 23 64 65 66           66.#def
47960 69 6e 65 20 54 4b 5f 41 4e 44 20 20 20 20 20 20  ine TK_AND      
47970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47980 20 20 20 20 20 20 36 37 0a 23 64 65 66 69 6e 65        67.#define
47990 20 54 4b 5f 49 53 20 20 20 20 20 20 20 20 20 20   TK_IS          
479a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
479b0 20 20 20 36 38 0a 23 64 65 66 69 6e 65 20 54 4b     68.#define TK
479c0 5f 42 45 54 57 45 45 4e 20 20 20 20 20 20 20 20  _BETWEEN        
479d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
479e0 36 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e  69.#define TK_IN
479f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47a00 20 20 20 20 20 20 20 20 20 20 20 20 20 37 30 0a               70.
47a10 23 64 65 66 69 6e 65 20 54 4b 5f 49 53 4e 55 4c  #define TK_ISNUL
47a20 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  L               
47a30 20 20 20 20 20 20 20 20 20 20 37 31 0a 23 64 65            71.#de
47a40 66 69 6e 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 20  fine TK_NOTNULL 
47a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47a60 20 20 20 20 20 20 20 37 32 0a 23 64 65 66 69 6e         72.#defin
47a70 65 20 54 4b 5f 4e 45 20 20 20 20 20 20 20 20 20  e TK_NE         
47a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47a90 20 20 20 20 37 33 0a 23 64 65 66 69 6e 65 20 54      73.#define T
47aa0 4b 5f 45 51 20 20 20 20 20 20 20 20 20 20 20 20  K_EQ            
47ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ac0 20 37 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 47   74.#define TK_G
47ad0 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
47ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 35                75
47af0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 45 20 20  .#define TK_LE  
47b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47b10 20 20 20 20 20 20 20 20 20 20 20 37 36 0a 23 64             76.#d
47b20 65 66 69 6e 65 20 54 4b 5f 4c 54 20 20 20 20 20  efine TK_LT     
47b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47b40 20 20 20 20 20 20 20 20 37 37 0a 23 64 65 66 69          77.#defi
47b50 6e 65 20 54 4b 5f 47 45 20 20 20 20 20 20 20 20  ne TK_GE        
47b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47b70 20 20 20 20 20 37 38 0a 23 64 65 66 69 6e 65 20       78.#define 
47b80 54 4b 5f 45 53 43 41 50 45 20 20 20 20 20 20 20  TK_ESCAPE       
47b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ba0 20 20 37 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f    79.#define TK_
47bb0 42 49 54 41 4e 44 20 20 20 20 20 20 20 20 20 20  BITAND          
47bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38                 8
47bd0 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 49 54  0.#define TK_BIT
47be0 4f 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20  OR              
47bf0 20 20 20 20 20 20 20 20 20 20 20 20 38 31 0a 23              81.#
47c00 64 65 66 69 6e 65 20 54 4b 5f 4c 53 48 49 46 54  define TK_LSHIFT
47c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47c20 20 20 20 20 20 20 20 20 20 38 32 0a 23 64 65 66           82.#def
47c30 69 6e 65 20 54 4b 5f 52 53 48 49 46 54 20 20 20  ine TK_RSHIFT   
47c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47c50 20 20 20 20 20 20 38 33 0a 23 64 65 66 69 6e 65        83.#define
47c60 20 54 4b 5f 50 4c 55 53 20 20 20 20 20 20 20 20   TK_PLUS        
47c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47c80 20 20 20 38 34 0a 23 64 65 66 69 6e 65 20 54 4b     84.#define TK
47c90 5f 4d 49 4e 55 53 20 20 20 20 20 20 20 20 20 20  _MINUS          
47ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47cb0 38 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 54  85.#define TK_ST
47cc0 41 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20  AR              
47cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 38 36 0a               86.
47ce0 23 64 65 66 69 6e 65 20 54 4b 5f 53 4c 41 53 48  #define TK_SLASH
47cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47d00 20 20 20 20 20 20 20 20 20 20 38 37 0a 23 64 65            87.#de
47d10 66 69 6e 65 20 54 4b 5f 52 45 4d 20 20 20 20 20  fine TK_REM     
47d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47d30 20 20 20 20 20 20 20 38 38 0a 23 64 65 66 69 6e         88.#defin
47d40 65 20 54 4b 5f 43 4f 4e 43 41 54 20 20 20 20 20  e TK_CONCAT     
47d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47d60 20 20 20 20 38 39 0a 23 64 65 66 69 6e 65 20 54      89.#define T
47d70 4b 5f 43 4f 4c 4c 41 54 45 20 20 20 20 20 20 20  K_COLLATE       
47d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47d90 20 39 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55   90.#define TK_U
47da0 4d 49 4e 55 53 20 20 20 20 20 20 20 20 20 20 20  MINUS           
47db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 31                91
47dc0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 50 4c 55  .#define TK_UPLU
47dd0 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  S               
47de0 20 20 20 20 20 20 20 20 20 20 20 39 32 0a 23 64             92.#d
47df0 65 66 69 6e 65 20 54 4b 5f 42 49 54 4e 4f 54 20  efine TK_BITNOT 
47e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e10 20 20 20 20 20 20 20 20 39 33 0a 23 64 65 66 69          93.#defi
47e20 6e 65 20 54 4b 5f 53 54 52 49 4e 47 20 20 20 20  ne TK_STRING    
47e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e40 20 20 20 20 20 39 34 0a 23 64 65 66 69 6e 65 20       94.#define 
47e50 54 4b 5f 4a 4f 49 4e 5f 4b 57 20 20 20 20 20 20  TK_JOIN_KW      
47e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e70 20 20 39 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f    95.#define TK_
47e80 43 4f 4e 53 54 52 41 49 4e 54 20 20 20 20 20 20  CONSTRAINT      
47e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39                 9
47ea0 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 46  6.#define TK_DEF
47eb0 41 55 4c 54 20 20 20 20 20 20 20 20 20 20 20 20  AULT            
47ec0 20 20 20 20 20 20 20 20 20 20 20 20 39 37 0a 23              97.#
47ed0 64 65 66 69 6e 65 20 54 4b 5f 4e 55 4c 4c 20 20  define TK_NULL  
47ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ef0 20 20 20 20 20 20 20 20 20 39 38 0a 23 64 65 66           98.#def
47f00 69 6e 65 20 54 4b 5f 50 52 49 4d 41 52 59 20 20  ine TK_PRIMARY  
47f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47f20 20 20 20 20 20 20 39 39 0a 23 64 65 66 69 6e 65        99.#define
47f30 20 54 4b 5f 55 4e 49 51 55 45 20 20 20 20 20 20   TK_UNIQUE      
47f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47f50 20 20 20 31 30 30 0a 23 64 65 66 69 6e 65 20 54     100.#define T
47f60 4b 5f 43 48 45 43 4b 20 20 20 20 20 20 20 20 20  K_CHECK         
47f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47f80 20 31 30 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f   101.#define TK_
47f90 52 45 46 45 52 45 4e 43 45 53 20 20 20 20 20 20  REFERENCES      
47fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
47fb0 30 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 55  02.#define TK_AU
47fc0 54 4f 49 4e 43 52 20 20 20 20 20 20 20 20 20 20  TOINCR          
47fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 33               103
47fe0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 4e 20 20  .#define TK_ON  
47ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48000 20 20 20 20 20 20 20 20 20 20 20 31 30 34 0a 23             104.#
48010 64 65 66 69 6e 65 20 54 4b 5f 44 45 4c 45 54 45  define TK_DELETE
48020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48030 20 20 20 20 20 20 20 20 20 31 30 35 0a 23 64 65           105.#de
48040 66 69 6e 65 20 54 4b 5f 55 50 44 41 54 45 20 20  fine TK_UPDATE  
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 31 30 36 0a 23 64 65 66 69         106.#defi
48070 6e 65 20 54 4b 5f 49 4e 53 45 52 54 20 20 20 20  ne TK_INSERT    
48080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48090 20 20 20 20 20 31 30 37 0a 23 64 65 66 69 6e 65       107.#define
480a0 20 54 4b 5f 53 45 54 20 20 20 20 20 20 20 20 20   TK_SET         
480b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
480c0 20 20 20 31 30 38 0a 23 64 65 66 69 6e 65 20 54     108.#define T
480d0 4b 5f 44 45 46 45 52 52 41 42 4c 45 20 20 20 20  K_DEFERRABLE    
480e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
480f0 20 31 30 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f   109.#define TK_
48100 46 4f 52 45 49 47 4e 20 20 20 20 20 20 20 20 20  FOREIGN         
48110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
48120 31 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 52  10.#define TK_DR
48130 4f 50 20 20 20 20 20 20 20 20 20 20 20 20 20 20  OP              
48140 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 31               111
48150 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 4e 49 4f  .#define TK_UNIO
48160 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  N               
48170 20 20 20 20 20 20 20 20 20 20 20 31 31 32 0a 23             112.#
48180 64 65 66 69 6e 65 20 54 4b 5f 41 4c 4c 20 20 20  define TK_ALL   
48190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
481a0 20 20 20 20 20 20 20 20 20 31 31 33 0a 23 64 65           113.#de
481b0 66 69 6e 65 20 54 4b 5f 45 58 43 45 50 54 20 20  fine TK_EXCEPT  
481c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
481d0 20 20 20 20 20 20 20 31 31 34 0a 23 64 65 66 69         114.#defi
481e0 6e 65 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 20  ne TK_INTERSECT 
481f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48200 20 20 20 20 20 31 31 35 0a 23 64 65 66 69 6e 65       115.#define
48210 20 54 4b 5f 53 45 4c 45 43 54 20 20 20 20 20 20   TK_SELECT      
48220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48230 20 20 20 31 31 36 0a 23 64 65 66 69 6e 65 20 54     116.#define T
48240 4b 5f 44 49 53 54 49 4e 43 54 20 20 20 20 20 20  K_DISTINCT      
48250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48260 20 31 31 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f   117.#define TK_
48270 44 4f 54 20 20 20 20 20 20 20 20 20 20 20 20 20  DOT             
48280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
48290 31 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 52  18.#define TK_FR
482a0 4f 4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20  OM              
482b0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 39               119
482c0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4a 4f 49 4e  .#define TK_JOIN
482d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
482e0 20 20 20 20 20 20 20 20 20 20 20 31 32 30 0a 23             120.#
482f0 64 65 66 69 6e 65 20 54 4b 5f 55 53 49 4e 47 20  define TK_USING 
48300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48310 20 20 20 20 20 20 20 20 20 31 32 31 0a 23 64 65           121.#de
48320 66 69 6e 65 20 54 4b 5f 4f 52 44 45 52 20 20 20  fine TK_ORDER   
48330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48340 20 20 20 20 20 20 20 31 32 32 0a 23 64 65 66 69         122.#defi
48350 6e 65 20 54 4b 5f 47 52 4f 55 50 20 20 20 20 20  ne TK_GROUP     
48360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48370 20 20 20 20 20 31 32 33 0a 23 64 65 66 69 6e 65       123.#define
48380 20 54 4b 5f 48 41 56 49 4e 47 20 20 20 20 20 20   TK_HAVING      
48390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
483a0 20 20 20 31 32 34 0a 23 64 65 66 69 6e 65 20 54     124.#define T
483b0 4b 5f 4c 49 4d 49 54 20 20 20 20 20 20 20 20 20  K_LIMIT         
483c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
483d0 20 31 32 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f   125.#define TK_
483e0 57 48 45 52 45 20 20 20 20 20 20 20 20 20 20 20  WHERE           
483f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
48400 32 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e  26.#define TK_IN
48410 54 4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  TO              
48420 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 37               127
48430 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 41 4c 55  .#define TK_VALU
48440 45 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ES              
48450 20 20 20 20 20 20 20 20 20 20 20 31 32 38 0a 23             128.#
48460 64 65 66 69 6e 65 20 54 4b 5f 49 4e 54 45 47 45  define TK_INTEGE
48470 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  R               
48480 20 20 20 20 20 20 20 20 20 31 32 39 0a 23 64 65           129.#de
48490 66 69 6e 65 20 54 4b 5f 46 4c 4f 41 54 20 20 20  fine TK_FLOAT   
484a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
484b0 20 20 20 20 20 20 20 31 33 30 0a 23 64 65 66 69         130.#defi
484c0 6e 65 20 54 4b 5f 42 4c 4f 42 20 20 20 20 20 20  ne TK_BLOB      
484d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
484e0 20 20 20 20 20 31 33 31 0a 23 64 65 66 69 6e 65       131.#define
484f0 20 54 4b 5f 52 45 47 49 53 54 45 52 20 20 20 20   TK_REGISTER    
48500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48510 20 20 20 31 33 32 0a 23 64 65 66 69 6e 65 20 54     132.#define T
48520 4b 5f 56 41 52 49 41 42 4c 45 20 20 20 20 20 20  K_VARIABLE      
48530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48540 20 31 33 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f   133.#define TK_
48550 43 41 53 45 20 20 20 20 20 20 20 20 20 20 20 20  CASE            
48560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
48570 33 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 57 48  34.#define TK_WH
48580 45 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  EN              
48590 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 35               135
485a0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 48 45 4e  .#define TK_THEN
485b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
485c0 20 20 20 20 20 20 20 20 20 20 20 31 33 36 0a 23             136.#
485d0 64 65 66 69 6e 65 20 54 4b 5f 45 4c 53 45 20 20  define TK_ELSE  
485e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
485f0 20 20 20 20 20 20 20 20 20 31 33 37 0a 23 64 65           137.#de
48600 66 69 6e 65 20 54 4b 5f 49 4e 44 45 58 20 20 20  fine TK_INDEX   
48610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48620 20 20 20 20 20 20 20 31 33 38 0a 23 64 65 66 69         138.#defi
48630 6e 65 20 54 4b 5f 41 4c 54 45 52 20 20 20 20 20  ne TK_ALTER     
48640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48650 20 20 20 20 20 31 33 39 0a 23 64 65 66 69 6e 65       139.#define
48660 20 54 4b 5f 41 44 44 20 20 20 20 20 20 20 20 20   TK_ADD         
48670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48680 20 20 20 31 34 30 0a 23 64 65 66 69 6e 65 20 54     140.#define T
48690 4b 5f 54 4f 5f 54 45 58 54 20 20 20 20 20 20 20  K_TO_TEXT       
486a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
486b0 20 31 34 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f   141.#define TK_
486c0 54 4f 5f 42 4c 4f 42 20 20 20 20 20 20 20 20 20  TO_BLOB         
486d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
486e0 34 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f  42.#define TK_TO
486f0 5f 4e 55 4d 45 52 49 43 20 20 20 20 20 20 20 20  _NUMERIC        
48700 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 33               143
48710 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 49  .#define TK_TO_I
48720 4e 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  NT              
48730 20 20 20 20 20 20 20 20 20 20 20 31 34 34 0a 23             144.#
48740 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 52 45 41  define TK_TO_REA
48750 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  L               
48760 20 20 20 20 20 20 20 20 20 31 34 35 0a 23 64 65           145.#de
48770 66 69 6e 65 20 54 4b 5f 45 4e 44 5f 4f 46 5f 46  fine TK_END_OF_F
48780 49 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ILE             
48790 20 20 20 20 20 20 20 31 34 36 0a 23 64 65 66 69         146.#defi
487a0 6e 65 20 54 4b 5f 49 4c 4c 45 47 41 4c 20 20 20  ne TK_ILLEGAL   
487b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
487c0 20 20 20 20 20 31 34 37 0a 23 64 65 66 69 6e 65       147.#define
487d0 20 54 4b 5f 53 50 41 43 45 20 20 20 20 20 20 20   TK_SPACE       
487e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
487f0 20 20 20 31 34 38 0a 23 64 65 66 69 6e 65 20 54     148.#define T
48800 4b 5f 55 4e 43 4c 4f 53 45 44 5f 53 54 52 49 4e  K_UNCLOSED_STRIN
48810 47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  G               
48820 20 31 34 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f   149.#define TK_
48830 46 55 4e 43 54 49 4f 4e 20 20 20 20 20 20 20 20  FUNCTION        
48840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
48850 35 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f  50.#define TK_CO
48860 4c 55 4d 4e 20 20 20 20 20 20 20 20 20 20 20 20  LUMN            
48870 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35 31               151
48880 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 47 47 5f  .#define TK_AGG_
48890 46 55 4e 43 54 49 4f 4e 20 20 20 20 20 20 20 20  FUNCTION        
488a0 20 20 20 20 20 20 20 20 20 20 20 31 35 32 0a 23             152.#
488b0 64 65 66 69 6e 65 20 54 4b 5f 41 47 47 5f 43 4f  define TK_AGG_CO
488c0 4c 55 4d 4e 20 20 20 20 20 20 20 20 20 20 20 20  LUMN            
488d0 20 20 20 20 20 20 20 20 20 31 35 33 0a 23 64 65           153.#de
488e0 66 69 6e 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55  fine TK_CONST_FU
488f0 4e 43 20 20 20 20 20 20 20 20 20 20 20 20 20 20  NC              
48900 20 20 20 20 20 20 20 31 35 34 0a 0a 2f 2a 2a 2a         154../***
48910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
48920 6f 66 20 70 61 72 73 65 2e 68 20 2a 2a 2a 2a 2a  of parse.h *****
48930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
48940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
48950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
48960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
48970 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
48980 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69  left off in sqli
48990 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  teInt.h ********
489a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 69 6e 63  **********/.#inc
489b0 6c 75 64 65 20 3c 73 74 64 69 6f 2e 68 3e 0a 23  lude <stdio.h>.#
489c0 69 6e 63 6c 75 64 65 20 3c 73 74 64 6c 69 62 2e  include <stdlib.
489d0 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 72  h>.#include <str
489e0 69 6e 67 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20  ing.h>.#include 
489f0 3c 61 73 73 65 72 74 2e 68 3e 0a 23 69 6e 63 6c  <assert.h>.#incl
48a00 75 64 65 20 3c 73 74 64 64 65 66 2e 68 3e 0a 0a  ude <stddef.h>..
48a10 2f 2a 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 69  /*.** If compili
48a20 6e 67 20 66 6f 72 20 61 20 70 72 6f 63 65 73 73  ng for a process
48a30 6f 72 20 74 68 61 74 20 6c 61 63 6b 73 20 66 6c  or that lacks fl
48a40 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 70  oating point sup
48a50 70 6f 72 74 2c 0a 2a 2a 20 73 75 62 73 74 69 74  port,.** substit
48a60 75 74 65 20 69 6e 74 65 67 65 72 20 66 6f 72 20  ute integer for 
48a70 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 0a 2a  floating-point.*
48a80 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
48a90 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
48aa0 49 4e 54 0a 23 20 64 65 66 69 6e 65 20 64 6f 75  INT.# define dou
48ab0 62 6c 65 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  ble sqlite_int64
48ac0 0a 23 20 64 65 66 69 6e 65 20 4c 4f 4e 47 44 4f  .# define LONGDO
48ad0 55 42 4c 45 5f 54 59 50 45 20 73 71 6c 69 74 65  UBLE_TYPE sqlite
48ae0 5f 69 6e 74 36 34 0a 23 20 69 66 6e 64 65 66 20  _int64.# ifndef 
48af0 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 0a 23  SQLITE_BIG_DBL.#
48b00 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45     define SQLITE
48b10 5f 42 49 47 5f 44 42 4c 20 28 28 28 73 71 6c 69  _BIG_DBL (((sqli
48b20 74 65 33 5f 69 6e 74 36 34 29 31 29 3c 3c 35 30  te3_int64)1)<<50
48b30 29 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69  ).# endif.# defi
48b40 6e 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ne SQLITE_OMIT_D
48b50 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 20 31 0a  ATETIME_FUNCS 1.
48b60 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
48b70 4f 4d 49 54 5f 54 52 41 43 45 20 31 0a 23 20 75  OMIT_TRACE 1.# u
48b80 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 49 58 45  ndef SQLITE_MIXE
48b90 44 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46  D_ENDIAN_64BIT_F
48ba0 4c 4f 41 54 0a 23 20 75 6e 64 65 66 20 53 51 4c  LOAT.# undef SQL
48bb0 49 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e 0a 23  ITE_HAVE_ISNAN.#
48bc0 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51  endif.#ifndef SQ
48bd0 4c 49 54 45 5f 42 49 47 5f 44 42 4c 0a 23 20 64  LITE_BIG_DBL.# d
48be0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 42 49 47  efine SQLITE_BIG
48bf0 5f 44 42 4c 20 28 31 65 39 39 29 0a 23 65 6e 64  _DBL (1e99).#end
48c00 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 4d 49 54 5f 54  if../*.** OMIT_T
48c10 45 4d 50 44 42 20 69 73 20 73 65 74 20 74 6f 20  EMPDB is set to 
48c20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  1 if SQLITE_OMIT
48c30 5f 54 45 4d 50 44 42 20 69 73 20 64 65 66 69 6e  _TEMPDB is defin
48c40 65 64 2c 20 6f 72 20 30 0a 2a 2a 20 61 66 74 65  ed, or 0.** afte
48c50 72 77 61 72 64 2e 20 48 61 76 69 6e 67 20 74 68  rward. Having th
48c60 69 73 20 6d 61 63 72 6f 20 61 6c 6c 6f 77 73 20  is macro allows 
48c70 75 73 20 74 6f 20 63 61 75 73 65 20 74 68 65 20  us to cause the 
48c80 43 20 63 6f 6d 70 69 6c 65 72 20 0a 2a 2a 20 74  C compiler .** t
48c90 6f 20 6f 6d 69 74 20 63 6f 64 65 20 75 73 65 64  o omit code used
48ca0 20 62 79 20 54 45 4d 50 20 74 61 62 6c 65 73 20   by TEMP tables 
48cb0 77 69 74 68 6f 75 74 20 6d 65 73 73 79 20 23 69  without messy #i
48cc0 66 6e 64 65 66 20 73 74 61 74 65 6d 65 6e 74 73  fndef statements
48cd0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
48ce0 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 23  TE_OMIT_TEMPDB.#
48cf0 64 65 66 69 6e 65 20 4f 4d 49 54 5f 54 45 4d 50  define OMIT_TEMP
48d00 44 42 20 31 0a 23 65 6c 73 65 0a 23 64 65 66 69  DB 1.#else.#defi
48d10 6e 65 20 4f 4d 49 54 5f 54 45 4d 50 44 42 20 30  ne OMIT_TEMPDB 0
48d20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49  .#endif../*.** I
48d30 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
48d40 6d 61 63 72 6f 20 69 73 20 73 65 74 20 74 6f 20  macro is set to 
48d50 31 2c 20 74 68 65 6e 20 4e 55 4c 4c 20 76 61 6c  1, then NULL val
48d60 75 65 73 20 61 72 65 20 63 6f 6e 73 69 64 65 72  ues are consider
48d70 65 64 0a 2a 2a 20 64 69 73 74 69 6e 63 74 20 77  ed.** distinct w
48d80 68 65 6e 20 64 65 74 65 72 6d 69 6e 69 6e 67 20  hen determining 
48d90 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  whether or not t
48da0 77 6f 20 65 6e 74 72 69 65 73 20 61 72 65 20 74  wo entries are t
48db0 68 65 20 73 61 6d 65 0a 2a 2a 20 69 6e 20 61 20  he same.** in a 
48dc0 55 4e 49 51 55 45 20 69 6e 64 65 78 2e 20 20 54  UNIQUE index.  T
48dd0 68 69 73 20 69 73 20 74 68 65 20 77 61 79 20 50  his is the way P
48de0 6f 73 74 67 72 65 53 51 4c 2c 20 4f 72 61 63 6c  ostgreSQL, Oracl
48df0 65 2c 20 44 42 32 2c 20 4d 79 53 51 4c 2c 0a 2a  e, DB2, MySQL,.*
48e00 2a 20 4f 43 45 4c 4f 54 2c 20 61 6e 64 20 46 69  * OCELOT, and Fi
48e10 72 65 62 69 72 64 20 61 6c 6c 20 77 6f 72 6b 2e  rebird all work.
48e20 20 20 54 68 65 20 53 51 4c 39 32 20 73 70 65 63    The SQL92 spec
48e30 20 65 78 70 6c 69 63 69 74 6c 79 20 73 61 79 73   explicitly says
48e40 20 74 68 69 73 0a 2a 2a 20 69 73 20 74 68 65 20   this.** is the 
48e50 77 61 79 20 74 68 69 6e 67 73 20 61 72 65 20 73  way things are s
48e60 75 70 70 6f 73 65 20 74 6f 20 77 6f 72 6b 2e 0a  uppose to work..
48e70 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f 6c  **.** If the fol
48e80 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 20 69 73 20  lowing macro is 
48e90 73 65 74 20 74 6f 20 30 2c 20 74 68 65 20 4e 55  set to 0, the NU
48ea0 4c 4c 73 20 61 72 65 20 69 6e 64 69 73 74 69 6e  LLs are indistin
48eb0 63 74 20 66 6f 72 0a 2a 2a 20 61 20 55 4e 49 51  ct for.** a UNIQ
48ec0 55 45 20 69 6e 64 65 78 2e 20 20 49 6e 20 74 68  UE index.  In th
48ed0 69 73 20 6d 6f 64 65 2c 20 79 6f 75 20 63 61 6e  is mode, you can
48ee0 20 6f 6e 6c 79 20 68 61 76 65 20 61 20 73 69 6e   only have a sin
48ef0 67 6c 65 20 4e 55 4c 4c 20 65 6e 74 72 79 0a 2a  gle NULL entry.*
48f00 2a 20 66 6f 72 20 61 20 63 6f 6c 75 6d 6e 20 64  * for a column d
48f10 65 63 6c 61 72 65 64 20 55 4e 49 51 55 45 2e 20  eclared UNIQUE. 
48f20 20 54 68 69 73 20 69 73 20 74 68 65 20 77 61 79   This is the way
48f30 20 49 6e 66 6f 72 6d 69 78 20 61 6e 64 20 53 51   Informix and SQ
48f40 4c 20 53 65 72 76 65 72 0a 2a 2a 20 77 6f 72 6b  L Server.** work
48f50 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 55 4c  ..*/.#define NUL
48f60 4c 5f 44 49 53 54 49 4e 43 54 5f 46 4f 52 5f 55  L_DISTINCT_FOR_U
48f70 4e 49 51 55 45 20 31 0a 0a 2f 2a 0a 2a 2a 20 54  NIQUE 1../*.** T
48f80 68 65 20 22 66 69 6c 65 20 66 6f 72 6d 61 74 22  he "file format"
48f90 20 6e 75 6d 62 65 72 20 69 73 20 61 6e 20 69 6e   number is an in
48fa0 74 65 67 65 72 20 74 68 61 74 20 69 73 20 69 6e  teger that is in
48fb0 63 72 65 6d 65 6e 74 65 64 20 77 68 65 6e 65 76  cremented whenev
48fc0 65 72 0a 2a 2a 20 74 68 65 20 56 44 42 45 2d 6c  er.** the VDBE-l
48fd0 65 76 65 6c 20 66 69 6c 65 20 66 6f 72 6d 61 74  evel file format
48fe0 20 63 68 61 6e 67 65 73 2e 20 20 54 68 65 20 66   changes.  The f
48ff0 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20  ollowing macros 
49000 64 65 66 69 6e 65 20 74 68 65 0a 2a 2a 20 74 68  define the.** th
49010 65 20 64 65 66 61 75 6c 74 20 66 69 6c 65 20 66  e default file f
49020 6f 72 6d 61 74 20 66 6f 72 20 6e 65 77 20 64 61  ormat for new da
49030 74 61 62 61 73 65 73 20 61 6e 64 20 74 68 65 20  tabases and the 
49040 6d 61 78 69 6d 75 6d 20 66 69 6c 65 20 66 6f 72  maximum file for
49050 6d 61 74 0a 2a 2a 20 74 68 61 74 20 74 68 65 20  mat.** that the 
49060 6c 69 62 72 61 72 79 20 63 61 6e 20 72 65 61 64  library can read
49070 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ..*/.#define SQL
49080 49 54 45 5f 4d 41 58 5f 46 49 4c 45 5f 46 4f 52  ITE_MAX_FILE_FOR
49090 4d 41 54 20 34 0a 23 69 66 6e 64 65 66 20 53 51  MAT 4.#ifndef SQ
490a0 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c  LITE_DEFAULT_FIL
490b0 45 5f 46 4f 52 4d 41 54 0a 23 20 64 65 66 69 6e  E_FORMAT.# defin
490c0 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  e SQLITE_DEFAULT
490d0 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 20 31 0a 23  _FILE_FORMAT 1.#
490e0 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53  endif..#ifndef S
490f0 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 52 45  QLITE_DEFAULT_RE
49100 43 55 52 53 49 56 45 5f 54 52 49 47 47 45 52 53  CURSIVE_TRIGGERS
49110 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
49120 5f 44 45 46 41 55 4c 54 5f 52 45 43 55 52 53 49  _DEFAULT_RECURSI
49130 56 45 5f 54 52 49 47 47 45 52 53 20 30 0a 23 65  VE_TRIGGERS 0.#e
49140 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 76  ndif../*.** Prov
49150 69 64 65 20 61 20 64 65 66 61 75 6c 74 20 76 61  ide a default va
49160 6c 75 65 20 66 6f 72 20 53 51 4c 49 54 45 5f 54  lue for SQLITE_T
49170 45 4d 50 5f 53 54 4f 52 45 20 69 6e 20 63 61 73  EMP_STORE in cas
49180 65 20 69 74 20 69 73 20 6e 6f 74 20 73 70 65 63  e it is not spec
49190 69 66 69 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20  ified.** on the 
491a0 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 0a 2a 2f 0a  command-line.*/.
491b0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 54  #ifndef SQLITE_T
491c0 45 4d 50 5f 53 54 4f 52 45 0a 23 20 64 65 66 69  EMP_STORE.# defi
491d0 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53  ne SQLITE_TEMP_S
491e0 54 4f 52 45 20 31 0a 23 65 6e 64 69 66 0a 0a 2f  TORE 1.#endif../
491f0 2a 0a 2a 2a 20 47 43 43 20 64 6f 65 73 20 6e 6f  *.** GCC does no
49200 74 20 64 65 66 69 6e 65 20 74 68 65 20 6f 66 66  t define the off
49210 73 65 74 6f 66 28 29 20 6d 61 63 72 6f 20 73 6f  setof() macro so
49220 20 77 65 27 6c 6c 20 68 61 76 65 20 74 6f 20 64   we'll have to d
49230 6f 20 69 74 0a 2a 2a 20 6f 75 72 73 65 6c 76 65  o it.** ourselve
49240 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 6f 66  s..*/.#ifndef of
49250 66 73 65 74 6f 66 0a 23 64 65 66 69 6e 65 20 6f  fsetof.#define o
49260 66 66 73 65 74 6f 66 28 53 54 52 55 43 54 55 52  ffsetof(STRUCTUR
49270 45 2c 46 49 45 4c 44 29 20 28 28 69 6e 74 29 28  E,FIELD) ((int)(
49280 28 63 68 61 72 2a 29 26 28 28 53 54 52 55 43 54  (char*)&((STRUCT
49290 55 52 45 2a 29 30 29 2d 3e 46 49 45 4c 44 29 29  URE*)0)->FIELD))
492a0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43  .#endif../*.** C
492b0 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74  heck to see if t
492c0 68 69 73 20 6d 61 63 68 69 6e 65 20 75 73 65 73  his machine uses
492d0 20 45 42 43 44 49 43 2e 20 20 28 59 65 73 2c 20   EBCDIC.  (Yes, 
492e0 62 65 6c 69 65 76 65 20 69 74 20 6f 72 0a 2a 2a  believe it or.**
492f0 20 6e 6f 74 2c 20 74 68 65 72 65 20 61 72 65 20   not, there are 
49300 73 74 69 6c 6c 20 6d 61 63 68 69 6e 65 73 20 6f  still machines o
49310 75 74 20 74 68 65 72 65 20 74 68 61 74 20 75 73  ut there that us
49320 65 20 45 42 43 44 49 43 2e 29 0a 2a 2f 0a 23 69  e EBCDIC.).*/.#i
49330 66 20 27 41 27 20 3d 3d 20 27 5c 33 30 31 27 0a  f 'A' == '\301'.
49340 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
49350 45 42 43 44 49 43 20 31 0a 23 65 6c 73 65 0a 23  EBCDIC 1.#else.#
49360 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41   define SQLITE_A
49370 53 43 49 49 20 31 0a 23 65 6e 64 69 66 0a 0a 2f  SCII 1.#endif../
49380 2a 0a 2a 2a 20 49 6e 74 65 67 65 72 73 20 6f 66  *.** Integers of
49390 20 6b 6e 6f 77 6e 20 73 69 7a 65 73 2e 20 20 54   known sizes.  T
493a0 68 65 73 65 20 74 79 70 65 64 65 66 73 20 6d 69  hese typedefs mi
493b0 67 68 74 20 63 68 61 6e 67 65 20 66 6f 72 20 61  ght change for a
493c0 72 63 68 69 74 65 63 74 75 72 65 73 0a 2a 2a 20  rchitectures.** 
493d0 77 68 65 72 65 20 74 68 65 20 73 69 7a 65 73 20  where the sizes 
493e0 76 65 72 79 2e 20 20 50 72 65 70 72 6f 63 65 73  very.  Preproces
493f0 73 6f 72 20 6d 61 63 72 6f 73 20 61 72 65 20 61  sor macros are a
49400 76 61 69 6c 61 62 6c 65 20 73 6f 20 74 68 61 74  vailable so that
49410 20 74 68 65 0a 2a 2a 20 74 79 70 65 73 20 63 61   the.** types ca
49420 6e 20 62 65 20 63 6f 6e 76 65 6e 69 65 6e 74 6c  n be convenientl
49430 79 20 72 65 64 65 66 69 6e 65 64 20 61 74 20 63  y redefined at c
49440 6f 6d 70 69 6c 65 2d 74 79 70 65 2e 20 20 4c 69  ompile-type.  Li
49450 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20  ke this:.**.**  
49460 20 20 20 20 20 20 20 63 63 20 27 2d 44 55 49 4e         cc '-DUIN
49470 54 50 54 52 5f 54 59 50 45 3d 6c 6f 6e 67 20 6c  TPTR_TYPE=long l
49480 6f 6e 67 20 69 6e 74 27 20 2e 2e 2e 0a 2a 2f 0a  ong int' ....*/.
49490 23 69 66 6e 64 65 66 20 55 49 4e 54 33 32 5f 54  #ifndef UINT32_T
494a0 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 45  YPE.# ifdef HAVE
494b0 5f 55 49 4e 54 33 32 5f 54 0a 23 20 20 64 65 66  _UINT32_T.#  def
494c0 69 6e 65 20 55 49 4e 54 33 32 5f 54 59 50 45 20  ine UINT32_TYPE 
494d0 75 69 6e 74 33 32 5f 74 0a 23 20 65 6c 73 65 0a  uint32_t.# else.
494e0 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54 33 32  #  define UINT32
494f0 5f 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20 69  _TYPE unsigned i
49500 6e 74 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69  nt.# endif.#endi
49510 66 0a 23 69 66 6e 64 65 66 20 55 49 4e 54 31 36  f.#ifndef UINT16
49520 5f 54 59 50 45 0a 23 20 69 66 64 65 66 20 48 41  _TYPE.# ifdef HA
49530 56 45 5f 55 49 4e 54 31 36 5f 54 0a 23 20 20 64  VE_UINT16_T.#  d
49540 65 66 69 6e 65 20 55 49 4e 54 31 36 5f 54 59 50  efine UINT16_TYP
49550 45 20 75 69 6e 74 31 36 5f 74 0a 23 20 65 6c 73  E uint16_t.# els
49560 65 0a 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54  e.#  define UINT
49570 31 36 5f 54 59 50 45 20 75 6e 73 69 67 6e 65 64  16_TYPE unsigned
49580 20 73 68 6f 72 74 20 69 6e 74 0a 23 20 65 6e 64   short int.# end
49590 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65  if.#endif.#ifnde
495a0 66 20 49 4e 54 31 36 5f 54 59 50 45 0a 23 20 69  f INT16_TYPE.# i
495b0 66 64 65 66 20 48 41 56 45 5f 49 4e 54 31 36 5f  fdef HAVE_INT16_
495c0 54 0a 23 20 20 64 65 66 69 6e 65 20 49 4e 54 31  T.#  define INT1
495d0 36 5f 54 59 50 45 20 69 6e 74 31 36 5f 74 0a 23  6_TYPE int16_t.#
495e0 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20   else.#  define 
495f0 49 4e 54 31 36 5f 54 59 50 45 20 73 68 6f 72 74  INT16_TYPE short
49600 20 69 6e 74 0a 23 20 65 6e 64 69 66 0a 23 65 6e   int.# endif.#en
49610 64 69 66 0a 23 69 66 6e 64 65 66 20 55 49 4e 54  dif.#ifndef UINT
49620 38 5f 54 59 50 45 0a 23 20 69 66 64 65 66 20 48  8_TYPE.# ifdef H
49630 41 56 45 5f 55 49 4e 54 38 5f 54 0a 23 20 20 64  AVE_UINT8_T.#  d
49640 65 66 69 6e 65 20 55 49 4e 54 38 5f 54 59 50 45  efine UINT8_TYPE
49650 20 75 69 6e 74 38 5f 74 0a 23 20 65 6c 73 65 0a   uint8_t.# else.
49660 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54 38 5f  #  define UINT8_
49670 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20 63 68  TYPE unsigned ch
49680 61 72 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69  ar.# endif.#endi
49690 66 0a 23 69 66 6e 64 65 66 20 49 4e 54 38 5f 54  f.#ifndef INT8_T
496a0 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 45  YPE.# ifdef HAVE
496b0 5f 49 4e 54 38 5f 54 0a 23 20 20 64 65 66 69 6e  _INT8_T.#  defin
496c0 65 20 49 4e 54 38 5f 54 59 50 45 20 69 6e 74 38  e INT8_TYPE int8
496d0 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66  _t.# else.#  def
496e0 69 6e 65 20 49 4e 54 38 5f 54 59 50 45 20 73 69  ine INT8_TYPE si
496f0 67 6e 65 64 20 63 68 61 72 0a 23 20 65 6e 64 69  gned char.# endi
49700 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66  f.#endif.#ifndef
49710 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45   LONGDOUBLE_TYPE
49720 0a 23 20 64 65 66 69 6e 65 20 4c 4f 4e 47 44 4f  .# define LONGDO
49730 55 42 4c 45 5f 54 59 50 45 20 6c 6f 6e 67 20 64  UBLE_TYPE long d
49740 6f 75 62 6c 65 0a 23 65 6e 64 69 66 0a 74 79 70  ouble.#endif.typ
49750 65 64 65 66 20 73 71 6c 69 74 65 5f 69 6e 74 36  edef sqlite_int6
49760 34 20 69 36 34 3b 20 20 20 20 20 20 20 20 20 20  4 i64;          
49770 2f 2a 20 38 2d 62 79 74 65 20 73 69 67 6e 65 64  /* 8-byte signed
49780 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65   integer */.type
49790 64 65 66 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  def sqlite_uint6
497a0 34 20 75 36 34 3b 20 20 20 20 20 20 20 20 20 2f  4 u64;         /
497b0 2a 20 38 2d 62 79 74 65 20 75 6e 73 69 67 6e 65  * 8-byte unsigne
497c0 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70  d integer */.typ
497d0 65 64 65 66 20 55 49 4e 54 33 32 5f 54 59 50 45  edef UINT32_TYPE
497e0 20 75 33 32 3b 20 20 20 20 20 20 20 20 20 20 20   u32;           
497f0 2f 2a 20 34 2d 62 79 74 65 20 75 6e 73 69 67 6e  /* 4-byte unsign
49800 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79  ed integer */.ty
49810 70 65 64 65 66 20 55 49 4e 54 31 36 5f 54 59 50  pedef UINT16_TYP
49820 45 20 75 31 36 3b 20 20 20 20 20 20 20 20 20 20  E u16;          
49830 20 2f 2a 20 32 2d 62 79 74 65 20 75 6e 73 69 67   /* 2-byte unsig
49840 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74  ned integer */.t
49850 79 70 65 64 65 66 20 49 4e 54 31 36 5f 54 59 50  ypedef INT16_TYP
49860 45 20 69 31 36 3b 20 20 20 20 20 20 20 20 20 20  E i16;          
49870 20 20 2f 2a 20 32 2d 62 79 74 65 20 73 69 67 6e    /* 2-byte sign
49880 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79  ed integer */.ty
49890 70 65 64 65 66 20 55 49 4e 54 38 5f 54 59 50 45  pedef UINT8_TYPE
498a0 20 75 38 3b 20 20 20 20 20 20 20 20 20 20 20 20   u8;            
498b0 20 2f 2a 20 31 2d 62 79 74 65 20 75 6e 73 69 67   /* 1-byte unsig
498c0 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74  ned integer */.t
498d0 79 70 65 64 65 66 20 49 4e 54 38 5f 54 59 50 45  ypedef INT8_TYPE
498e0 20 69 38 3b 20 20 20 20 20 20 20 20 20 20 20 20   i8;            
498f0 20 20 2f 2a 20 31 2d 62 79 74 65 20 73 69 67 6e    /* 1-byte sign
49900 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 0a 2f  ed integer */../
49910 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 58 5f  *.** SQLITE_MAX_
49920 55 33 32 20 69 73 20 61 20 75 36 34 20 63 6f 6e  U32 is a u64 con
49930 73 74 61 6e 74 20 74 68 61 74 20 69 73 20 74 68  stant that is th
49940 65 20 6d 61 78 69 6d 75 6d 20 75 36 34 20 76 61  e maximum u64 va
49950 6c 75 65 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20  lue.** that can 
49960 62 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20 75  be stored in a u
49970 33 32 20 77 69 74 68 6f 75 74 20 6c 6f 73 73 20  32 without loss 
49980 6f 66 20 64 61 74 61 2e 20 20 54 68 65 20 76 61  of data.  The va
49990 6c 75 65 0a 2a 2a 20 69 73 20 30 78 30 30 30 30  lue.** is 0x0000
499a0 30 30 30 30 66 66 66 66 66 66 66 66 2e 20 20 42  0000ffffffff.  B
499b0 75 74 20 62 65 63 61 75 73 65 20 6f 66 20 71 75  ut because of qu
499c0 69 72 6b 73 20 6f 66 20 73 6f 6d 65 20 63 6f 6d  irks of some com
499d0 70 69 6c 65 72 73 2c 20 77 65 0a 2a 2a 20 68 61  pilers, we.** ha
499e0 76 65 20 74 6f 20 73 70 65 63 69 66 79 20 74 68  ve to specify th
499f0 65 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 6c  e value in the l
49a00 65 73 73 20 69 6e 74 75 69 74 69 76 65 20 6d 61  ess intuitive ma
49a10 6e 6e 65 72 20 73 68 6f 77 6e 3a 0a 2a 2f 0a 23  nner shown:.*/.#
49a20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41  define SQLITE_MA
49a30 58 5f 55 33 32 20 20 28 28 28 28 75 36 34 29 31  X_U32  ((((u64)1
49a40 29 3c 3c 33 32 29 2d 31 29 0a 0a 2f 2a 0a 2a 2a  )<<32)-1)../*.**
49a50 20 4d 61 63 72 6f 73 20 74 6f 20 64 65 74 65 72   Macros to deter
49a60 6d 69 6e 65 20 77 68 65 74 68 65 72 20 74 68 65  mine whether the
49a70 20 6d 61 63 68 69 6e 65 20 69 73 20 62 69 67 20   machine is big 
49a80 6f 72 20 6c 69 74 74 6c 65 20 65 6e 64 69 61 6e  or little endian
49a90 2c 0a 2a 2a 20 65 76 61 6c 75 61 74 65 64 20 61  ,.** evaluated a
49aa0 74 20 72 75 6e 74 69 6d 65 2e 0a 2a 2f 0a 23 69  t runtime..*/.#i
49ab0 66 64 65 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c  fdef SQLITE_AMAL
49ac0 47 41 4d 41 54 49 4f 4e 0a 53 51 4c 49 54 45 5f  GAMATION.SQLITE_
49ad0 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 69 6e  PRIVATE const in
49ae0 74 20 73 71 6c 69 74 65 33 6f 6e 65 20 3d 20 31  t sqlite3one = 1
49af0 3b 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50  ;.#else.SQLITE_P
49b00 52 49 56 41 54 45 20 63 6f 6e 73 74 20 69 6e 74  RIVATE const int
49b10 20 73 71 6c 69 74 65 33 6f 6e 65 3b 0a 23 65 6e   sqlite3one;.#en
49b20 64 69 66 0a 23 69 66 20 64 65 66 69 6e 65 64 28  dif.#if defined(
49b30 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 64  i386) || defined
49b40 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65  (__i386__) || de
49b50 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 5c 0a  fined(_M_IX86)\.
49b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49b70 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20               || 
49b80 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34  defined(__x86_64
49b90 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 78  ) || defined(__x
49ba0 38 36 5f 36 34 5f 5f 29 0a 23 20 64 65 66 69 6e  86_64__).# defin
49bb0 65 20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 49  e SQLITE_BIGENDI
49bc0 41 4e 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65  AN    0.# define
49bd0 20 53 51 4c 49 54 45 5f 4c 49 54 54 4c 45 45 4e   SQLITE_LITTLEEN
49be0 44 49 41 4e 20 31 0a 23 20 64 65 66 69 6e 65 20  DIAN 1.# define 
49bf0 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49  SQLITE_UTF16NATI
49c00 56 45 20 20 53 51 4c 49 54 45 5f 55 54 46 31 36  VE  SQLITE_UTF16
49c10 4c 45 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  LE.#else.# defin
49c20 65 20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 49  e SQLITE_BIGENDI
49c30 41 4e 20 20 20 20 28 2a 28 63 68 61 72 20 2a 29  AN    (*(char *)
49c40 28 26 73 71 6c 69 74 65 33 6f 6e 65 29 3d 3d 30  (&sqlite3one)==0
49c50 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  ).# define SQLIT
49c60 45 5f 4c 49 54 54 4c 45 45 4e 44 49 41 4e 20 28  E_LITTLEENDIAN (
49c70 2a 28 63 68 61 72 20 2a 29 28 26 73 71 6c 69 74  *(char *)(&sqlit
49c80 65 33 6f 6e 65 29 3d 3d 31 29 0a 23 20 64 65 66  e3one)==1).# def
49c90 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36  ine SQLITE_UTF16
49ca0 4e 41 54 49 56 45 20 28 53 51 4c 49 54 45 5f 42  NATIVE (SQLITE_B
49cb0 49 47 45 4e 44 49 41 4e 3f 53 51 4c 49 54 45 5f  IGENDIAN?SQLITE_
49cc0 55 54 46 31 36 42 45 3a 53 51 4c 49 54 45 5f 55  UTF16BE:SQLITE_U
49cd0 54 46 31 36 4c 45 29 0a 23 65 6e 64 69 66 0a 0a  TF16LE).#endif..
49ce0 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 61 6e 74 73 20  /*.** Constants 
49cf0 66 6f 72 20 74 68 65 20 6c 61 72 67 65 73 74 20  for the largest 
49d00 61 6e 64 20 73 6d 61 6c 6c 65 73 74 20 70 6f 73  and smallest pos
49d10 73 69 62 6c 65 20 36 34 2d 62 69 74 20 73 69 67  sible 64-bit sig
49d20 6e 65 64 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a  ned integers..**
49d30 20 54 68 65 73 65 20 6d 61 63 72 6f 73 20 61 72   These macros ar
49d40 65 20 64 65 73 69 67 6e 65 64 20 74 6f 20 77 6f  e designed to wo
49d50 72 6b 20 63 6f 72 72 65 63 74 6c 79 20 6f 6e 20  rk correctly on 
49d60 62 6f 74 68 20 33 32 2d 62 69 74 20 61 6e 64 20  both 32-bit and 
49d70 36 34 2d 62 69 74 0a 2a 2a 20 63 6f 6d 70 69 6c  64-bit.** compil
49d80 65 72 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ers..*/.#define 
49d90 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 20 20 28  LARGEST_INT64  (
49da0 30 78 66 66 66 66 66 66 66 66 7c 28 28 28 69 36  0xffffffff|(((i6
49db0 34 29 30 78 37 66 66 66 66 66 66 66 29 3c 3c 33  4)0x7fffffff)<<3
49dc0 32 29 29 0a 23 64 65 66 69 6e 65 20 53 4d 41 4c  2)).#define SMAL
49dd0 4c 45 53 54 5f 49 4e 54 36 34 20 28 28 28 69 36  LEST_INT64 (((i6
49de0 34 29 2d 31 29 20 2d 20 4c 41 52 47 45 53 54 5f  4)-1) - LARGEST_
49df0 49 4e 54 36 34 29 0a 0a 2f 2a 20 0a 2a 2a 20 52  INT64)../* .** R
49e00 6f 75 6e 64 20 75 70 20 61 20 6e 75 6d 62 65 72  ound up a number
49e10 20 74 6f 20 74 68 65 20 6e 65 78 74 20 6c 61 72   to the next lar
49e20 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20  ger multiple of 
49e30 38 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64  8.  This is used
49e40 0a 2a 2a 20 74 6f 20 66 6f 72 63 65 20 38 2d 62  .** to force 8-b
49e50 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 6e  yte alignment on
49e60 20 36 34 2d 62 69 74 20 61 72 63 68 69 74 65 63   64-bit architec
49e70 74 75 72 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e  tures..*/.#defin
49e80 65 20 52 4f 55 4e 44 38 28 78 29 20 20 20 20 20  e ROUND8(x)     
49e90 28 28 28 78 29 2b 37 29 26 7e 37 29 0a 0a 2f 2a  (((x)+7)&~7)../*
49ea0 0a 2a 2a 20 52 6f 75 6e 64 20 64 6f 77 6e 20 74  .** Round down t
49eb0 6f 20 74 68 65 20 6e 65 61 72 65 73 74 20 6d 75  o the nearest mu
49ec0 6c 74 69 70 6c 65 20 6f 66 20 38 0a 2a 2f 0a 23  ltiple of 8.*/.#
49ed0 64 65 66 69 6e 65 20 52 4f 55 4e 44 44 4f 57 4e  define ROUNDDOWN
49ee0 38 28 78 29 20 28 28 78 29 26 7e 37 29 0a 0a 2f  8(x) ((x)&~7)../
49ef0 2a 0a 2a 2a 20 41 73 73 65 72 74 20 74 68 61 74  *.** Assert that
49f00 20 74 68 65 20 70 6f 69 6e 74 65 72 20 58 20 69   the pointer X i
49f10 73 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20  s aligned to an 
49f20 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 79 2e  8-byte boundary.
49f30 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 49 47 48  .*/.#define EIGH
49f40 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54  T_BYTE_ALIGNMENT
49f50 28 58 29 20 20 20 28 28 28 28 63 68 61 72 2a 29  (X)   ((((char*)
49f60 28 58 29 20 2d 20 28 63 68 61 72 2a 29 30 29 26  (X) - (char*)0)&
49f70 37 29 3d 3d 30 29 0a 0a 0a 2f 2a 0a 2a 2a 20 41  7)==0).../*.** A
49f80 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
49f90 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
49fa0 63 74 75 72 65 20 69 73 20 75 73 65 64 20 74 6f  cture is used to
49fb0 20 73 74 6f 72 65 20 74 68 65 20 62 75 73 79 2d   store the busy-
49fc0 68 61 6e 64 6c 65 72 0a 2a 2a 20 63 61 6c 6c 62  handler.** callb
49fd0 61 63 6b 20 66 6f 72 20 61 20 67 69 76 65 6e 20  ack for a given 
49fe0 73 71 6c 69 74 65 20 68 61 6e 64 6c 65 2e 20 0a  sqlite handle. .
49ff0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
4a000 2e 62 75 73 79 48 61 6e 64 6c 65 72 20 6d 65 6d  .busyHandler mem
4a010 62 65 72 20 6f 66 20 74 68 65 20 73 71 6c 69 74  ber of the sqlit
4a020 65 20 73 74 72 75 63 74 20 63 6f 6e 74 61 69 6e  e struct contain
4a030 73 20 74 68 65 20 62 75 73 79 0a 2a 2a 20 63 61  s the busy.** ca
4a040 6c 6c 62 61 63 6b 20 66 6f 72 20 74 68 65 20 64  llback for the d
4a050 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20  atabase handle. 
4a060 45 61 63 68 20 70 61 67 65 72 20 6f 70 65 6e 65  Each pager opene
4a070 64 20 76 69 61 20 74 68 65 20 73 71 6c 69 74 65  d via the sqlite
4a080 0a 2a 2a 20 68 61 6e 64 6c 65 20 69 73 20 70 61  .** handle is pa
4a090 73 73 65 64 20 61 20 70 6f 69 6e 74 65 72 20 74  ssed a pointer t
4a0a0 6f 20 73 71 6c 69 74 65 2e 62 75 73 79 48 61 6e  o sqlite.busyHan
4a0b0 64 6c 65 72 2e 20 54 68 65 20 62 75 73 79 2d 68  dler. The busy-h
4a0c0 61 6e 64 6c 65 72 0a 2a 2a 20 63 61 6c 6c 62 61  andler.** callba
4a0d0 63 6b 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  ck is currently 
4a0e0 69 6e 76 6f 6b 65 64 20 6f 6e 6c 79 20 66 72 6f  invoked only fro
4a0f0 6d 20 77 69 74 68 69 6e 20 70 61 67 65 72 2e 63  m within pager.c
4a100 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
4a110 75 63 74 20 42 75 73 79 48 61 6e 64 6c 65 72 20  uct BusyHandler 
4a120 42 75 73 79 48 61 6e 64 6c 65 72 3b 0a 73 74 72  BusyHandler;.str
4a130 75 63 74 20 42 75 73 79 48 61 6e 64 6c 65 72 20  uct BusyHandler 
4a140 7b 0a 20 20 69 6e 74 20 28 2a 78 46 75 6e 63 29  {.  int (*xFunc)
4a150 28 76 6f 69 64 20 2a 2c 69 6e 74 29 3b 20 20 2f  (void *,int);  /
4a160 2a 20 54 68 65 20 62 75 73 79 20 63 61 6c 6c 62  * The busy callb
4a170 61 63 6b 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  ack */.  void *p
4a180 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20  Arg;            
4a190 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67      /* First arg
4a1a0 20 74 6f 20 62 75 73 79 20 63 61 6c 6c 62 61 63   to busy callbac
4a1b0 6b 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 75 73 79  k */.  int nBusy
4a1c0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
4a1d0 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 65 64    /* Incremented
4a1e0 20 77 69 74 68 20 65 61 63 68 20 62 75 73 79 20   with each busy 
4a1f0 63 61 6c 6c 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  call */.};../*.*
4a200 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6d 61  * Name of the ma
4a210 73 74 65 72 20 64 61 74 61 62 61 73 65 20 74 61  ster database ta
4a220 62 6c 65 2e 20 20 54 68 65 20 6d 61 73 74 65 72  ble.  The master
4a230 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 0a   database table.
4a240 2a 2a 20 69 73 20 61 20 73 70 65 63 69 61 6c 20  ** is a special 
4a250 74 61 62 6c 65 20 74 68 61 74 20 68 6f 6c 64 73  table that holds
4a260 20 74 68 65 20 6e 61 6d 65 73 20 61 6e 64 20 61   the names and a
4a270 74 74 72 69 62 75 74 65 73 20 6f 66 20 61 6c 6c  ttributes of all
4a280 0a 2a 2a 20 75 73 65 72 20 74 61 62 6c 65 73 20  .** user tables 
4a290 61 6e 64 20 69 6e 64 69 63 65 73 2e 0a 2a 2f 0a  and indices..*/.
4a2a0 23 64 65 66 69 6e 65 20 4d 41 53 54 45 52 5f 4e  #define MASTER_N
4a2b0 41 4d 45 20 20 20 20 20 20 20 22 73 71 6c 69 74  AME       "sqlit
4a2c0 65 5f 6d 61 73 74 65 72 22 0a 23 64 65 66 69 6e  e_master".#defin
4a2d0 65 20 54 45 4d 50 5f 4d 41 53 54 45 52 5f 4e 41  e TEMP_MASTER_NA
4a2e0 4d 45 20 20 22 73 71 6c 69 74 65 5f 74 65 6d 70  ME  "sqlite_temp
4a2f0 5f 6d 61 73 74 65 72 22 0a 0a 2f 2a 0a 2a 2a 20  _master"../*.** 
4a300 54 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66  The root-page of
4a310 20 74 68 65 20 6d 61 73 74 65 72 20 64 61 74 61   the master data
4a320 62 61 73 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 23  base table..*/.#
4a330 64 65 66 69 6e 65 20 4d 41 53 54 45 52 5f 52 4f  define MASTER_RO
4a340 4f 54 20 20 20 20 20 20 20 31 0a 0a 2f 2a 0a 2a  OT       1../*.*
4a350 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  * The name of th
4a360 65 20 73 63 68 65 6d 61 20 74 61 62 6c 65 2e 0a  e schema table..
4a370 2a 2f 0a 23 64 65 66 69 6e 65 20 53 43 48 45 4d  */.#define SCHEM
4a380 41 5f 54 41 42 4c 45 28 78 29 20 20 28 28 21 4f  A_TABLE(x)  ((!O
4a390 4d 49 54 5f 54 45 4d 50 44 42 29 26 26 28 78 3d  MIT_TEMPDB)&&(x=
4a3a0 3d 31 29 3f 54 45 4d 50 5f 4d 41 53 54 45 52 5f  =1)?TEMP_MASTER_
4a3b0 4e 41 4d 45 3a 4d 41 53 54 45 52 5f 4e 41 4d 45  NAME:MASTER_NAME
4a3c0 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 6f 6e 76 65  )../*.** A conve
4a3d0 6e 69 65 6e 63 65 20 6d 61 63 72 6f 20 74 68 61  nience macro tha
4a3e0 74 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75  t returns the nu
4a3f0 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73  mber of elements
4a400 20 69 6e 0a 2a 2a 20 61 6e 20 61 72 72 61 79 2e   in.** an array.
4a410 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 41 72 72 61  .*/.#define Arra
4a420 79 53 69 7a 65 28 58 29 20 20 20 20 28 28 69 6e  ySize(X)    ((in
4a430 74 29 28 73 69 7a 65 6f 66 28 58 29 2f 73 69 7a  t)(sizeof(X)/siz
4a440 65 6f 66 28 58 5b 30 5d 29 29 29 0a 0a 2f 2a 0a  eof(X[0])))../*.
4a450 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
4a460 20 76 61 6c 75 65 20 61 73 20 61 20 64 65 73 74   value as a dest
4a470 72 75 63 74 6f 72 20 6d 65 61 6e 73 20 74 6f 20  ructor means to 
4a480 75 73 65 20 73 71 6c 69 74 65 33 44 62 46 72 65  use sqlite3DbFre
4a490 65 28 29 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20  e()..** This is 
4a4a0 61 6e 20 69 6e 74 65 72 6e 61 6c 20 65 78 74 65  an internal exte
4a4b0 6e 73 69 6f 6e 20 74 6f 20 53 51 4c 49 54 45 5f  nsion to SQLITE_
4a4c0 53 54 41 54 49 43 20 61 6e 64 20 53 51 4c 49 54  STATIC and SQLIT
4a4d0 45 5f 54 52 41 4e 53 49 45 4e 54 2e 0a 2a 2f 0a  E_TRANSIENT..*/.
4a4e0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44  #define SQLITE_D
4a4f0 59 4e 41 4d 49 43 20 20 20 28 28 73 71 6c 69 74  YNAMIC   ((sqlit
4a500 65 33 5f 64 65 73 74 72 75 63 74 6f 72 5f 74 79  e3_destructor_ty
4a510 70 65 29 73 71 6c 69 74 65 33 44 62 46 72 65 65  pe)sqlite3DbFree
4a520 29 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 53 51  )../*.** When SQ
4a530 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 20 69 73  LITE_OMIT_WSD is
4a540 20 64 65 66 69 6e 65 64 2c 20 69 74 20 6d 65 61   defined, it mea
4a550 6e 73 20 74 68 61 74 20 74 68 65 20 74 61 72 67  ns that the targ
4a560 65 74 20 70 6c 61 74 66 6f 72 6d 20 64 6f 65 73  et platform does
4a570 0a 2a 2a 20 6e 6f 74 20 73 75 70 70 6f 72 74 20  .** not support 
4a580 57 72 69 74 61 62 6c 65 20 53 74 61 74 69 63 20  Writable Static 
4a590 44 61 74 61 20 28 57 53 44 29 20 73 75 63 68 20  Data (WSD) such 
4a5a0 61 73 20 67 6c 6f 62 61 6c 20 61 6e 64 20 73 74  as global and st
4a5b0 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 2e 0a  atic variables..
4a5c0 2a 2a 20 41 6c 6c 20 76 61 72 69 61 62 6c 65 73  ** All variables
4a5d0 20 6d 75 73 74 20 65 69 74 68 65 72 20 62 65 20   must either be 
4a5e0 6f 6e 20 74 68 65 20 73 74 61 63 6b 20 6f 72 20  on the stack or 
4a5f0 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f  dynamically allo
4a600 63 61 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 74 68  cated from.** th
4a610 65 20 68 65 61 70 2e 20 20 57 68 65 6e 20 57 53  e heap.  When WS
4a620 44 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65 64  D is unsupported
4a630 2c 20 74 68 65 20 76 61 72 69 61 62 6c 65 20 64  , the variable d
4a640 65 63 6c 61 72 61 74 69 6f 6e 73 20 73 63 61 74  eclarations scat
4a650 74 65 72 65 64 0a 2a 2a 20 74 68 72 6f 75 67 68  tered.** through
4a660 6f 75 74 20 74 68 65 20 53 51 4c 69 74 65 20 63  out the SQLite c
4a670 6f 64 65 20 6d 75 73 74 20 62 65 63 6f 6d 65 20  ode must become 
4a680 63 6f 6e 73 74 61 6e 74 73 20 69 6e 73 74 65 61  constants instea
4a690 64 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 57  d.  The SQLITE_W
4a6a0 53 44 0a 2a 2a 20 6d 61 63 72 6f 20 69 73 20 75  SD.** macro is u
4a6b0 73 65 64 20 66 6f 72 20 74 68 69 73 20 70 75 72  sed for this pur
4a6c0 70 6f 73 65 2e 20 20 41 6e 64 20 69 6e 73 74 65  pose.  And inste
4a6d0 61 64 20 6f 66 20 72 65 66 65 72 65 6e 63 69 6e  ad of referencin
4a6e0 67 20 74 68 65 20 76 61 72 69 61 62 6c 65 0a 2a  g the variable.*
4a6f0 2a 20 64 69 72 65 63 74 6c 79 2c 20 77 65 20 75  * directly, we u
4a700 73 65 20 69 74 73 20 63 6f 6e 73 74 61 6e 74 20  se its constant 
4a710 61 73 20 61 20 6b 65 79 20 74 6f 20 6c 6f 6f 6b  as a key to look
4a720 75 70 20 74 68 65 20 72 75 6e 2d 74 69 6d 65 20  up the run-time 
4a730 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 62 75 66  allocated.** buf
4a740 66 65 72 20 74 68 61 74 20 68 6f 6c 64 73 20 72  fer that holds r
4a750 65 61 6c 20 76 61 72 69 61 62 6c 65 2e 20 20 54  eal variable.  T
4a760 68 65 20 63 6f 6e 73 74 61 6e 74 20 69 73 20 61  he constant is a
4a770 6c 73 6f 20 74 68 65 20 69 6e 69 74 69 61 6c 69  lso the initiali
4a780 7a 65 72 0a 2a 2a 20 66 6f 72 20 74 68 65 20 72  zer.** for the r
4a790 75 6e 2d 74 69 6d 65 20 61 6c 6c 6f 63 61 74 65  un-time allocate
4a7a0 64 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20  d buffer..**.** 
4a7b0 49 6e 20 74 68 65 20 75 73 75 61 6c 20 63 61 73  In the usual cas
4a7c0 65 20 77 68 65 72 65 20 57 53 44 20 69 73 20 73  e where WSD is s
4a7d0 75 70 70 6f 72 74 65 64 2c 20 74 68 65 20 53 51  upported, the SQ
4a7e0 4c 49 54 45 5f 57 53 44 20 61 6e 64 20 47 4c 4f  LITE_WSD and GLO
4a7f0 42 41 4c 0a 2a 2a 20 6d 61 63 72 6f 73 20 62 65  BAL.** macros be
4a800 63 6f 6d 65 20 6e 6f 2d 6f 70 73 20 61 6e 64 20  come no-ops and 
4a810 68 61 76 65 20 7a 65 72 6f 20 70 65 72 66 6f 72  have zero perfor
4a820 6d 61 6e 63 65 20 69 6d 70 61 63 74 2e 0a 2a 2f  mance impact..*/
4a830 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
4a840 4d 49 54 5f 57 53 44 0a 20 20 23 64 65 66 69 6e  MIT_WSD.  #defin
4a850 65 20 53 51 4c 49 54 45 5f 57 53 44 20 63 6f 6e  e SQLITE_WSD con
4a860 73 74 0a 20 20 23 64 65 66 69 6e 65 20 47 4c 4f  st.  #define GLO
4a870 42 41 4c 28 74 2c 76 29 20 28 2a 28 74 2a 29 73  BAL(t,v) (*(t*)s
4a880 71 6c 69 74 65 33 5f 77 73 64 5f 66 69 6e 64 28  qlite3_wsd_find(
4a890 28 76 6f 69 64 2a 29 26 28 76 29 2c 20 73 69 7a  (void*)&(v), siz
4a8a0 65 6f 66 28 76 29 29 29 0a 20 20 23 64 65 66 69  eof(v))).  #defi
4a8b0 6e 65 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  ne sqlite3Global
4a8c0 43 6f 6e 66 69 67 20 47 4c 4f 42 41 4c 28 73 74  Config GLOBAL(st
4a8d0 72 75 63 74 20 53 71 6c 69 74 65 33 43 6f 6e 66  ruct Sqlite3Conf
4a8e0 69 67 2c 20 73 71 6c 69 74 65 33 43 6f 6e 66 69  ig, sqlite3Confi
4a8f0 67 29 0a 53 51 4c 49 54 45 5f 41 50 49 20 20 20  g).SQLITE_API   
4a900 69 6e 74 20 73 71 6c 69 74 65 33 5f 77 73 64 5f  int sqlite3_wsd_
4a910 69 6e 69 74 28 69 6e 74 20 4e 2c 20 69 6e 74 20  init(int N, int 
4a920 4a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 20  J);.SQLITE_API  
4a930 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 77   void *sqlite3_w
4a940 73 64 5f 66 69 6e 64 28 76 6f 69 64 20 2a 4b 2c  sd_find(void *K,
4a950 20 69 6e 74 20 4c 29 3b 0a 23 65 6c 73 65 0a 20   int L);.#else. 
4a960 20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f   #define SQLITE_
4a970 57 53 44 20 0a 20 20 23 64 65 66 69 6e 65 20 47  WSD .  #define G
4a980 4c 4f 42 41 4c 28 74 2c 76 29 20 76 0a 20 20 23  LOBAL(t,v) v.  #
4a990 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 47 6c  define sqlite3Gl
4a9a0 6f 62 61 6c 43 6f 6e 66 69 67 20 73 71 6c 69 74  obalConfig sqlit
4a9b0 65 33 43 6f 6e 66 69 67 0a 23 65 6e 64 69 66 0a  e3Config.#endif.
4a9c0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
4a9d0 77 69 6e 67 20 6d 61 63 72 6f 73 20 61 72 65 20  wing macros are 
4a9e0 75 73 65 64 20 74 6f 20 73 75 70 70 72 65 73 73  used to suppress
4a9f0 20 63 6f 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e   compiler warnin
4aa00 67 73 20 61 6e 64 20 74 6f 0a 2a 2a 20 6d 61 6b  gs and to.** mak
4aa10 65 20 69 74 20 63 6c 65 61 72 20 74 6f 20 68 75  e it clear to hu
4aa20 6d 61 6e 20 72 65 61 64 65 72 73 20 77 68 65 6e  man readers when
4aa30 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 61 72 61   a function para
4aa40 6d 65 74 65 72 20 69 73 20 64 65 6c 69 62 65 72  meter is deliber
4aa50 61 74 65 6c 79 20 0a 2a 2a 20 6c 65 66 74 20 75  ately .** left u
4aa60 6e 75 73 65 64 20 77 69 74 68 69 6e 20 74 68 65  nused within the
4aa70 20 62 6f 64 79 20 6f 66 20 61 20 66 75 6e 63 74   body of a funct
4aa80 69 6f 6e 2e 20 54 68 69 73 20 75 73 75 61 6c 6c  ion. This usuall
4aa90 79 20 68 61 70 70 65 6e 73 20 77 68 65 6e 0a 2a  y happens when.*
4aaa0 2a 20 61 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  * a function is 
4aab0 63 61 6c 6c 65 64 20 76 69 61 20 61 20 66 75 6e  called via a fun
4aac0 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 2e 20 46  ction pointer. F
4aad0 6f 72 20 65 78 61 6d 70 6c 65 20 74 68 65 20 0a  or example the .
4aae0 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
4aaf0 6e 20 6f 66 20 61 6e 20 53 51 4c 20 61 67 67 72  n of an SQL aggr
4ab00 65 67 61 74 65 20 73 74 65 70 20 63 61 6c 6c 62  egate step callb
4ab10 61 63 6b 20 6d 61 79 20 6e 6f 74 20 75 73 65 20  ack may not use 
4ab20 74 68 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  the.** parameter
4ab30 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20   indicating the 
4ab40 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65  number of argume
4ab50 6e 74 73 20 70 61 73 73 65 64 20 74 6f 20 74 68  nts passed to th
4ab60 65 20 61 67 67 72 65 67 61 74 65 2c 0a 2a 2a 20  e aggregate,.** 
4ab70 69 66 20 69 74 20 6b 6e 6f 77 73 20 74 68 61 74  if it knows that
4ab80 20 74 68 69 73 20 69 73 20 65 6e 66 6f 72 63 65   this is enforce
4ab90 64 20 65 6c 73 65 77 68 65 72 65 2e 0a 2a 2a 0a  d elsewhere..**.
4aba0 2a 2a 20 57 68 65 6e 20 61 20 66 75 6e 63 74 69  ** When a functi
4abb0 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  on parameter is 
4abc0 6e 6f 74 20 75 73 65 64 20 61 74 20 61 6c 6c 20  not used at all 
4abd0 77 69 74 68 69 6e 20 74 68 65 20 62 6f 64 79 20  within the body 
4abe0 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 2c 0a 2a  of a function,.*
4abf0 2a 20 69 74 20 69 73 20 67 65 6e 65 72 61 6c 6c  * it is generall
4ac00 79 20 6e 61 6d 65 64 20 22 4e 6f 74 55 73 65 64  y named "NotUsed
4ac10 22 20 6f 72 20 22 4e 6f 74 55 73 65 64 32 22 20  " or "NotUsed2" 
4ac20 74 6f 20 6d 61 6b 65 20 74 68 69 6e 67 73 20 65  to make things e
4ac30 76 65 6e 20 63 6c 65 61 72 65 72 2e 0a 2a 2a 20  ven clearer..** 
4ac40 48 6f 77 65 76 65 72 2c 20 74 68 65 73 65 20 6d  However, these m
4ac50 61 63 72 6f 73 20 6d 61 79 20 61 6c 73 6f 20 62  acros may also b
4ac60 65 20 75 73 65 64 20 74 6f 20 73 75 70 70 72 65  e used to suppre
4ac70 73 73 20 77 61 72 6e 69 6e 67 73 20 72 65 6c 61  ss warnings rela
4ac80 74 65 64 20 74 6f 0a 2a 2a 20 70 61 72 61 6d 65  ted to.** parame
4ac90 74 65 72 73 20 74 68 61 74 20 6d 61 79 20 6f 72  ters that may or
4aca0 20 6d 61 79 20 6e 6f 74 20 62 65 20 75 73 65 64   may not be used
4acb0 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 63 6f   depending on co
4acc0 6d 70 69 6c 61 74 69 6f 6e 20 6f 70 74 69 6f 6e  mpilation option
4acd0 73 2e 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c  s..** For exampl
4ace0 65 20 74 68 6f 73 65 20 70 61 72 61 6d 65 74 65  e those paramete
4acf0 72 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 6e 20  rs only used in 
4ad00 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65  assert() stateme
4ad10 6e 74 73 2e 20 49 6e 20 74 68 65 73 65 0a 2a 2a  nts. In these.**
4ad20 20 63 61 73 65 73 20 74 68 65 20 70 61 72 61 6d   cases the param
4ad30 65 74 65 72 73 20 61 72 65 20 6e 61 6d 65 64 20  eters are named 
4ad40 61 73 20 70 65 72 20 74 68 65 20 75 73 75 61 6c  as per the usual
4ad50 20 63 6f 6e 76 65 6e 74 69 6f 6e 73 2e 0a 2a 2f   conventions..*/
4ad60 0a 23 64 65 66 69 6e 65 20 55 4e 55 53 45 44 5f  .#define UNUSED_
4ad70 50 41 52 41 4d 45 54 45 52 28 78 29 20 28 76 6f  PARAMETER(x) (vo
4ad80 69 64 29 28 78 29 0a 23 64 65 66 69 6e 65 20 55  id)(x).#define U
4ad90 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32  NUSED_PARAMETER2
4ada0 28 78 2c 79 29 20 55 4e 55 53 45 44 5f 50 41 52  (x,y) UNUSED_PAR
4adb0 41 4d 45 54 45 52 28 78 29 2c 55 4e 55 53 45 44  AMETER(x),UNUSED
4adc0 5f 50 41 52 41 4d 45 54 45 52 28 79 29 0a 0a 2f  _PARAMETER(y)../
4add0 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20 72 65 66  *.** Forward ref
4ade0 65 72 65 6e 63 65 73 20 74 6f 20 73 74 72 75 63  erences to struc
4adf0 74 75 72 65 73 0a 2a 2f 0a 74 79 70 65 64 65 66  tures.*/.typedef
4ae00 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 20   struct AggInfo 
4ae10 41 67 67 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66  AggInfo;.typedef
4ae20 20 73 74 72 75 63 74 20 41 75 74 68 43 6f 6e 74   struct AuthCont
4ae30 65 78 74 20 41 75 74 68 43 6f 6e 74 65 78 74 3b  ext AuthContext;
4ae40 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4ae50 41 75 74 6f 69 6e 63 49 6e 66 6f 20 41 75 74 6f  AutoincInfo Auto
4ae60 69 6e 63 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66  incInfo;.typedef
4ae70 20 73 74 72 75 63 74 20 42 69 74 76 65 63 20 42   struct Bitvec B
4ae80 69 74 76 65 63 3b 0a 74 79 70 65 64 65 66 20 73  itvec;.typedef s
4ae90 74 72 75 63 74 20 52 6f 77 53 65 74 20 52 6f 77  truct RowSet Row
4aea0 53 65 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72  Set;.typedef str
4aeb0 75 63 74 20 43 6f 6c 6c 53 65 71 20 43 6f 6c 6c  uct CollSeq Coll
4aec0 53 65 71 3b 0a 74 79 70 65 64 65 66 20 73 74 72  Seq;.typedef str
4aed0 75 63 74 20 43 6f 6c 75 6d 6e 20 43 6f 6c 75 6d  uct Column Colum
4aee0 6e 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  n;.typedef struc
4aef0 74 20 44 62 20 44 62 3b 0a 74 79 70 65 64 65 66  t Db Db;.typedef
4af00 20 73 74 72 75 63 74 20 53 63 68 65 6d 61 20 53   struct Schema S
4af10 63 68 65 6d 61 3b 0a 74 79 70 65 64 65 66 20 73  chema;.typedef s
4af20 74 72 75 63 74 20 45 78 70 72 20 45 78 70 72 3b  truct Expr Expr;
4af30 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4af40 45 78 70 72 4c 69 73 74 20 45 78 70 72 4c 69 73  ExprList ExprLis
4af50 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  t;.typedef struc
4af60 74 20 45 78 70 72 53 70 61 6e 20 45 78 70 72 53  t ExprSpan ExprS
4af70 70 61 6e 3b 0a 74 79 70 65 64 65 66 20 73 74 72  pan;.typedef str
4af80 75 63 74 20 46 4b 65 79 20 46 4b 65 79 3b 0a 74  uct FKey FKey;.t
4af90 79 70 65 64 65 66 20 73 74 72 75 63 74 20 46 75  ypedef struct Fu
4afa0 6e 63 44 65 66 20 46 75 6e 63 44 65 66 3b 0a 74  ncDef FuncDef;.t
4afb0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 46 75  ypedef struct Fu
4afc0 6e 63 44 65 66 48 61 73 68 20 46 75 6e 63 44 65  ncDefHash FuncDe
4afd0 66 48 61 73 68 3b 0a 74 79 70 65 64 65 66 20 73  fHash;.typedef s
4afe0 74 72 75 63 74 20 49 64 4c 69 73 74 20 49 64 4c  truct IdList IdL
4aff0 69 73 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ist;.typedef str
4b000 75 63 74 20 49 6e 64 65 78 20 49 6e 64 65 78 3b  uct Index Index;
4b010 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4b020 49 6e 64 65 78 53 61 6d 70 6c 65 20 49 6e 64 65  IndexSample Inde
4b030 78 53 61 6d 70 6c 65 3b 0a 74 79 70 65 64 65 66  xSample;.typedef
4b040 20 73 74 72 75 63 74 20 4b 65 79 43 6c 61 73 73   struct KeyClass
4b050 20 4b 65 79 43 6c 61 73 73 3b 0a 74 79 70 65 64   KeyClass;.typed
4b060 65 66 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66  ef struct KeyInf
4b070 6f 20 4b 65 79 49 6e 66 6f 3b 0a 74 79 70 65 64  o KeyInfo;.typed
4b080 65 66 20 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73  ef struct Lookas
4b090 69 64 65 20 4c 6f 6f 6b 61 73 69 64 65 3b 0a 74  ide Lookaside;.t
4b0a0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4c 6f  ypedef struct Lo
4b0b0 6f 6b 61 73 69 64 65 53 6c 6f 74 20 4c 6f 6f 6b  okasideSlot Look
4b0c0 61 73 69 64 65 53 6c 6f 74 3b 0a 74 79 70 65 64  asideSlot;.typed
4b0d0 65 66 20 73 74 72 75 63 74 20 4d 6f 64 75 6c 65  ef struct Module
4b0e0 20 4d 6f 64 75 6c 65 3b 0a 74 79 70 65 64 65 66   Module;.typedef
4b0f0 20 73 74 72 75 63 74 20 4e 61 6d 65 43 6f 6e 74   struct NameCont
4b100 65 78 74 20 4e 61 6d 65 43 6f 6e 74 65 78 74 3b  ext NameContext;
4b110 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4b120 50 61 72 73 65 20 50 61 72 73 65 3b 0a 74 79 70  Parse Parse;.typ
4b130 65 64 65 66 20 73 74 72 75 63 74 20 53 61 76 65  edef struct Save
4b140 70 6f 69 6e 74 20 53 61 76 65 70 6f 69 6e 74 3b  point Savepoint;
4b150 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4b160 53 65 6c 65 63 74 20 53 65 6c 65 63 74 3b 0a 74  Select Select;.t
4b170 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 72  ypedef struct Sr
4b180 63 4c 69 73 74 20 53 72 63 4c 69 73 74 3b 0a 74  cList SrcList;.t
4b190 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 74  ypedef struct St
4b1a0 72 41 63 63 75 6d 20 53 74 72 41 63 63 75 6d 3b  rAccum StrAccum;
4b1b0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4b1c0 54 61 62 6c 65 20 54 61 62 6c 65 3b 0a 74 79 70  Table Table;.typ
4b1d0 65 64 65 66 20 73 74 72 75 63 74 20 54 61 62 6c  edef struct Tabl
4b1e0 65 4c 6f 63 6b 20 54 61 62 6c 65 4c 6f 63 6b 3b  eLock TableLock;
4b1f0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4b200 54 6f 6b 65 6e 20 54 6f 6b 65 6e 3b 0a 74 79 70  Token Token;.typ
4b210 65 64 65 66 20 73 74 72 75 63 74 20 54 72 69 67  edef struct Trig
4b220 67 65 72 50 72 67 20 54 72 69 67 67 65 72 50 72  gerPrg TriggerPr
4b230 67 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  g;.typedef struc
4b240 74 20 54 72 69 67 67 65 72 53 74 65 70 20 54 72  t TriggerStep Tr
4b250 69 67 67 65 72 53 74 65 70 3b 0a 74 79 70 65 64  iggerStep;.typed
4b260 65 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65  ef struct Trigge
4b270 72 20 54 72 69 67 67 65 72 3b 0a 74 79 70 65 64  r Trigger;.typed
4b280 65 66 20 73 74 72 75 63 74 20 55 6e 70 61 63 6b  ef struct Unpack
4b290 65 64 52 65 63 6f 72 64 20 55 6e 70 61 63 6b 65  edRecord Unpacke
4b2a0 64 52 65 63 6f 72 64 3b 0a 74 79 70 65 64 65 66  dRecord;.typedef
4b2b0 20 73 74 72 75 63 74 20 56 54 61 62 6c 65 20 56   struct VTable V
4b2c0 54 61 62 6c 65 3b 0a 74 79 70 65 64 65 66 20 73  Table;.typedef s
4b2d0 74 72 75 63 74 20 57 61 6c 6b 65 72 20 57 61 6c  truct Walker Wal
4b2e0 6b 65 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ker;.typedef str
4b2f0 75 63 74 20 57 68 65 72 65 50 6c 61 6e 20 57 68  uct WherePlan Wh
4b300 65 72 65 50 6c 61 6e 3b 0a 74 79 70 65 64 65 66  erePlan;.typedef
4b310 20 73 74 72 75 63 74 20 57 68 65 72 65 49 6e 66   struct WhereInf
4b320 6f 20 57 68 65 72 65 49 6e 66 6f 3b 0a 74 79 70  o WhereInfo;.typ
4b330 65 64 65 66 20 73 74 72 75 63 74 20 57 68 65 72  edef struct Wher
4b340 65 4c 65 76 65 6c 20 57 68 65 72 65 4c 65 76 65  eLevel WhereLeve
4b350 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 65 72 20  l;../*.** Defer 
4b360 73 6f 75 72 63 69 6e 67 20 76 64 62 65 2e 68 20  sourcing vdbe.h 
4b370 61 6e 64 20 62 74 72 65 65 2e 68 20 75 6e 74 69  and btree.h unti
4b380 6c 20 61 66 74 65 72 20 74 68 65 20 22 75 38 22  l after the "u8"
4b390 20 61 6e 64 20 0a 2a 2a 20 22 42 75 73 79 48 61   and .** "BusyHa
4b3a0 6e 64 6c 65 72 22 20 74 79 70 65 64 65 66 73 2e  ndler" typedefs.
4b3b0 20 76 64 62 65 2e 68 20 61 6c 73 6f 20 72 65 71   vdbe.h also req
4b3c0 75 69 72 65 73 20 61 20 66 65 77 20 6f 66 20 74  uires a few of t
4b3d0 68 65 20 6f 70 61 71 75 65 0a 2a 2a 20 70 6f 69  he opaque.** poi
4b3e0 6e 74 65 72 20 74 79 70 65 73 20 28 69 2e 65 2e  nter types (i.e.
4b3f0 20 46 75 6e 63 44 65 66 29 20 64 65 66 69 6e 65   FuncDef) define
4b400 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 2f 2a 2a 2a  d above..*/./***
4b410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
4b420 75 64 65 20 62 74 72 65 65 2e 68 20 69 6e 20 74  ude btree.h in t
4b430 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c  he middle of sql
4b440 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  iteInt.h *******
4b450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
4b460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
4b470 6e 20 66 69 6c 65 20 62 74 72 65 65 2e 68 20 2a  n file btree.h *
4b480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4b490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4b4a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
4b4b0 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72  * 2001 September
4b4c0 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   15.**.** The au
4b4d0 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
4b4e0 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
4b4f0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
4b500 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
4b510 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
4b520 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
4b530 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
4b540 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
4b550 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
4b560 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
4b570 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
4b580 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
4b590 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
4b5a0 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
4b5b0 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
4b5c0 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
4b5d0 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
4b5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4b5f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4b600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4b610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4b620 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65  **.** This heade
4b630 72 20 66 69 6c 65 20 64 65 66 69 6e 65 73 20 74  r file defines t
4b640 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 68 61  he interface tha
4b650 74 20 74 68 65 20 73 71 6c 69 74 65 20 42 2d 54  t the sqlite B-T
4b660 72 65 65 20 66 69 6c 65 0a 2a 2a 20 73 75 62 73  ree file.** subs
4b670 79 73 74 65 6d 2e 20 20 53 65 65 20 63 6f 6d 6d  ystem.  See comm
4b680 65 6e 74 73 20 69 6e 20 74 68 65 20 73 6f 75 72  ents in the sour
4b690 63 65 20 63 6f 64 65 20 66 6f 72 20 61 20 64 65  ce code for a de
4b6a0 74 61 69 6c 65 64 20 64 65 73 63 72 69 70 74 69  tailed descripti
4b6b0 6f 6e 0a 2a 2a 20 6f 66 20 77 68 61 74 20 65 61  on.** of what ea
4b6c0 63 68 20 69 6e 74 65 72 66 61 63 65 20 72 6f 75  ch interface rou
4b6d0 74 69 6e 65 20 64 6f 65 73 2e 0a 2a 2a 0a 2a 2a  tine does..**.**
4b6e0 20 40 28 23 29 20 24 49 64 3a 20 62 74 72 65 65   @(#) $Id: btree
4b6f0 2e 68 2c 76 20 31 2e 31 32 30 20 32 30 30 39 2f  .h,v 1.120 2009/
4b700 30 37 2f 32 32 20 30 30 3a 33 35 3a 32 34 20 64  07/22 00:35:24 d
4b710 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e  rh Exp $.*/.#ifn
4b720 64 65 66 20 5f 42 54 52 45 45 5f 48 5f 0a 23 64  def _BTREE_H_.#d
4b730 65 66 69 6e 65 20 5f 42 54 52 45 45 5f 48 5f 0a  efine _BTREE_H_.
4b740 0a 2f 2a 20 54 4f 44 4f 3a 20 54 68 69 73 20 64  ./* TODO: This d
4b750 65 66 69 6e 69 74 69 6f 6e 20 69 73 20 6a 75 73  efinition is jus
4b760 74 20 69 6e 63 6c 75 64 65 64 20 73 6f 20 6f 74  t included so ot
4b770 68 65 72 20 6d 6f 64 75 6c 65 73 20 63 6f 6d 70  her modules comp
4b780 69 6c 65 2e 20 49 74 0a 2a 2a 20 6e 65 65 64 73  ile. It.** needs
4b790 20 74 6f 20 62 65 20 72 65 76 69 73 69 74 65 64   to be revisited
4b7a0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ..*/.#define SQL
4b7b0 49 54 45 5f 4e 5f 42 54 52 45 45 5f 4d 45 54 41  ITE_N_BTREE_META
4b7c0 20 31 30 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 64 65   10../*.** If de
4b7d0 66 69 6e 65 64 20 61 73 20 6e 6f 6e 2d 7a 65 72  fined as non-zer
4b7e0 6f 2c 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 69  o, auto-vacuum i
4b7f0 73 20 65 6e 61 62 6c 65 64 20 62 79 20 64 65 66  s enabled by def
4b800 61 75 6c 74 2e 20 4f 74 68 65 72 77 69 73 65 0a  ault. Otherwise.
4b810 2a 2a 20 69 74 20 6d 75 73 74 20 62 65 20 74 75  ** it must be tu
4b820 72 6e 65 64 20 6f 6e 20 66 6f 72 20 65 61 63 68  rned on for each
4b830 20 64 61 74 61 62 61 73 65 20 75 73 69 6e 67 20   database using 
4b840 22 50 52 41 47 4d 41 20 61 75 74 6f 5f 76 61 63  "PRAGMA auto_vac
4b850 75 75 6d 20 3d 20 31 22 2e 0a 2a 2f 0a 23 69 66  uum = 1"..*/.#if
4b860 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41  ndef SQLITE_DEFA
4b870 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  ULT_AUTOVACUUM. 
4b880 20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f   #define SQLITE_
4b890 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55  DEFAULT_AUTOVACU
4b8a0 55 4d 20 30 0a 23 65 6e 64 69 66 0a 0a 23 64 65  UM 0.#endif..#de
4b8b0 66 69 6e 65 20 42 54 52 45 45 5f 41 55 54 4f 56  fine BTREE_AUTOV
4b8c0 41 43 55 55 4d 5f 4e 4f 4e 45 20 30 20 20 20 20  ACUUM_NONE 0    
4b8d0 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 64 6f      /* Do not do
4b8e0 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 2a 2f 0a   auto-vacuum */.
4b8f0 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 41 55  #define BTREE_AU
4b900 54 4f 56 41 43 55 55 4d 5f 46 55 4c 4c 20 31 20  TOVACUUM_FULL 1 
4b910 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 66 75 6c         /* Do ful
4b920 6c 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 2a 2f  l auto-vacuum */
4b930 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 41  .#define BTREE_A
4b940 55 54 4f 56 41 43 55 55 4d 5f 49 4e 43 52 20 32  UTOVACUUM_INCR 2
4b950 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 65          /* Incre
4b960 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 2a 2f  mental vacuum */
4b970 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20  ../*.** Forward 
4b980 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20  declarations of 
4b990 73 74 72 75 63 74 75 72 65 0a 2a 2f 0a 74 79 70  structure.*/.typ
4b9a0 65 64 65 66 20 73 74 72 75 63 74 20 42 74 72 65  edef struct Btre
4b9b0 65 20 42 74 72 65 65 3b 0a 74 79 70 65 64 65 66  e Btree;.typedef
4b9c0 20 73 74 72 75 63 74 20 42 74 43 75 72 73 6f 72   struct BtCursor
4b9d0 20 42 74 43 75 72 73 6f 72 3b 0a 74 79 70 65 64   BtCursor;.typed
4b9e0 65 66 20 73 74 72 75 63 74 20 42 74 53 68 61 72  ef struct BtShar
4b9f0 65 64 20 42 74 53 68 61 72 65 64 3b 0a 74 79 70  ed BtShared;.typ
4ba00 65 64 65 66 20 73 74 72 75 63 74 20 42 74 72 65  edef struct Btre
4ba10 65 4d 75 74 65 78 41 72 72 61 79 20 42 74 72 65  eMutexArray Btre
4ba20 65 4d 75 74 65 78 41 72 72 61 79 3b 0a 0a 2f 2a  eMutexArray;../*
4ba30 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75  .** This structu
4ba40 72 65 20 72 65 63 6f 72 64 73 20 61 6c 6c 20 6f  re records all o
4ba50 66 20 74 68 65 20 42 74 72 65 65 73 20 74 68 61  f the Btrees tha
4ba60 74 20 6e 65 65 64 20 74 6f 20 68 6f 6c 64 0a 2a  t need to hold.*
4ba70 2a 20 61 20 6d 75 74 65 78 20 62 65 66 6f 72 65  * a mutex before
4ba80 20 77 65 20 65 6e 74 65 72 20 73 71 6c 69 74 65   we enter sqlite
4ba90 33 56 64 62 65 45 78 65 63 28 29 2e 20 20 54 68  3VdbeExec().  Th
4baa0 65 20 42 74 72 65 65 73 20 61 72 65 0a 2a 2a 20  e Btrees are.** 
4bab0 61 72 65 20 70 6c 61 63 65 64 20 69 6e 20 61 42  are placed in aB
4bac0 74 72 65 65 5b 5d 20 69 6e 20 6f 72 64 65 72 20  tree[] in order 
4bad0 6f 66 20 61 42 74 72 65 65 5b 5d 2d 3e 70 42 74  of aBtree[]->pBt
4bae0 2e 20 20 54 68 61 74 20 77 61 79 2c 0a 2a 2a 20  .  That way,.** 
4baf0 77 65 20 63 61 6e 20 61 6c 77 61 79 73 20 6c 6f  we can always lo
4bb00 63 6b 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 68  ck and unlock th
4bb10 65 6d 20 61 6c 6c 20 71 75 69 63 6b 6c 79 2e 0a  em all quickly..
4bb20 2a 2f 0a 73 74 72 75 63 74 20 42 74 72 65 65 4d  */.struct BtreeM
4bb30 75 74 65 78 41 72 72 61 79 20 7b 0a 20 20 69 6e  utexArray {.  in
4bb40 74 20 6e 4d 75 74 65 78 3b 0a 20 20 42 74 72 65  t nMutex;.  Btre
4bb50 65 20 2a 61 42 74 72 65 65 5b 53 51 4c 49 54 45  e *aBtree[SQLITE
4bb60 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 2b 31 5d  _MAX_ATTACHED+1]
4bb70 3b 0a 7d 3b 0a 0a 0a 53 51 4c 49 54 45 5f 50 52  ;.};...SQLITE_PR
4bb80 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4bb90 33 42 74 72 65 65 4f 70 65 6e 28 0a 20 20 63 6f  3BtreeOpen(.  co
4bba0 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  nst char *zFilen
4bbb0 61 6d 65 2c 20 20 20 2f 2a 20 4e 61 6d 65 20 6f  ame,   /* Name o
4bbc0 66 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  f database file 
4bbd0 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 73 71 6c  to open */.  sql
4bbe0 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
4bbf0 20 20 20 20 20 20 2f 2a 20 41 73 73 6f 63 69 61        /* Associa
4bc00 74 65 64 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ted database con
4bc10 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 42 74 72  nection */.  Btr
4bc20 65 65 20 2a 2a 70 70 42 74 72 65 65 2c 20 20 20  ee **ppBtree,   
4bc30 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
4bc40 6f 70 65 6e 20 42 74 72 65 65 2a 20 68 65 72 65  open Btree* here
4bc50 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c   */.  int flags,
4bc60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4bc70 2a 20 46 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74  * Flags */.  int
4bc80 20 76 66 73 46 6c 61 67 73 20 20 20 20 20 20 20   vfsFlags       
4bc90 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70        /* Flags p
4bca0 61 73 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f  assed through to
4bcb0 20 56 46 53 20 6f 70 65 6e 20 2a 2f 0a 29 3b 0a   VFS open */.);.
4bcc0 0a 2f 2a 20 54 68 65 20 66 6c 61 67 73 20 70 61  ./* The flags pa
4bcd0 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74  rameter to sqlit
4bce0 65 33 42 74 72 65 65 4f 70 65 6e 20 63 61 6e 20  e3BtreeOpen can 
4bcf0 62 65 20 74 68 65 20 62 69 74 77 69 73 65 20 6f  be the bitwise o
4bd00 72 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c  r of the.** foll
4bd10 6f 77 69 6e 67 20 76 61 6c 75 65 73 2e 0a 2a 2a  owing values..**
4bd20 0a 2a 2a 20 4e 4f 54 45 3a 20 20 54 68 65 73 65  .** NOTE:  These
4bd30 20 76 61 6c 75 65 73 20 6d 75 73 74 20 6d 61 74   values must mat
4bd40 63 68 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  ch the correspon
4bd50 64 69 6e 67 20 50 41 47 45 52 5f 20 76 61 6c 75  ding PAGER_ valu
4bd60 65 73 20 69 6e 0a 2a 2a 20 70 61 67 65 72 2e 68  es in.** pager.h
4bd70 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52  ..*/.#define BTR
4bd80 45 45 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20  EE_OMIT_JOURNAL 
4bd90 20 31 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 75 73   1  /* Do not us
4bda0 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 4e 6f 20 61  e journal.  No a
4bdb0 72 67 75 6d 65 6e 74 20 2a 2f 0a 23 64 65 66 69  rgument */.#defi
4bdc0 6e 65 20 42 54 52 45 45 5f 4e 4f 5f 52 45 41 44  ne BTREE_NO_READ
4bdd0 4c 4f 43 4b 20 20 20 32 20 20 2f 2a 20 4f 6d 69  LOCK   2  /* Omi
4bde0 74 20 72 65 61 64 6c 6f 63 6b 73 20 6f 6e 20 72  t readlocks on r
4bdf0 65 61 64 6f 6e 6c 79 20 66 69 6c 65 73 20 2a 2f  eadonly files */
4be00 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 4d  .#define BTREE_M
4be10 45 4d 4f 52 59 20 20 20 20 20 20 20 20 34 20 20  EMORY        4  
4be20 2f 2a 20 49 6e 2d 6d 65 6d 6f 72 79 20 44 42 2e  /* In-memory DB.
4be30 20 20 4e 6f 20 61 72 67 75 6d 65 6e 74 20 2a 2f    No argument */
4be40 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 52  .#define BTREE_R
4be50 45 41 44 4f 4e 4c 59 20 20 20 20 20 20 38 20 20  EADONLY      8  
4be60 2f 2a 20 4f 70 65 6e 20 74 68 65 20 64 61 74 61  /* Open the data
4be70 62 61 73 65 20 69 6e 20 72 65 61 64 2d 6f 6e 6c  base in read-onl
4be80 79 20 6d 6f 64 65 20 2a 2f 0a 23 64 65 66 69 6e  y mode */.#defin
4be90 65 20 42 54 52 45 45 5f 52 45 41 44 57 52 49 54  e BTREE_READWRIT
4bea0 45 20 20 20 20 31 36 20 20 2f 2a 20 4f 70 65 6e  E    16  /* Open
4beb0 20 66 6f 72 20 62 6f 74 68 20 72 65 61 64 69 6e   for both readin
4bec0 67 20 61 6e 64 20 77 72 69 74 69 6e 67 20 2a 2f  g and writing */
4bed0 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 43  .#define BTREE_C
4bee0 52 45 41 54 45 20 20 20 20 20 20 20 33 32 20 20  REATE       32  
4bef0 2f 2a 20 43 72 65 61 74 65 20 74 68 65 20 64 61  /* Create the da
4bf00 74 61 62 61 73 65 20 69 66 20 69 74 20 64 6f 65  tabase if it doe
4bf10 73 20 6e 6f 74 20 65 78 69 73 74 20 2a 2f 0a 0a  s not exist */..
4bf20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4bf30 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
4bf40 6c 6f 73 65 28 42 74 72 65 65 2a 29 3b 0a 53 51  lose(Btree*);.SQ
4bf50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
4bf60 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74   sqlite3BtreeSet
4bf70 43 61 63 68 65 53 69 7a 65 28 42 74 72 65 65 2a  CacheSize(Btree*
4bf80 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
4bf90 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4bfa0 33 42 74 72 65 65 53 65 74 53 61 66 65 74 79 4c  3BtreeSetSafetyL
4bfb0 65 76 65 6c 28 42 74 72 65 65 2a 2c 69 6e 74 2c  evel(Btree*,int,
4bfc0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
4bfd0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4bfe0 42 74 72 65 65 53 79 6e 63 44 69 73 61 62 6c 65  BtreeSyncDisable
4bff0 64 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54  d(Btree*);.SQLIT
4c000 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4c010 6c 69 74 65 33 42 74 72 65 65 53 65 74 50 61 67  lite3BtreeSetPag
4c020 65 53 69 7a 65 28 42 74 72 65 65 20 2a 70 2c 20  eSize(Btree *p, 
4c030 69 6e 74 20 6e 50 61 67 65 73 69 7a 65 2c 20 69  int nPagesize, i
4c040 6e 74 20 6e 52 65 73 65 72 76 65 2c 20 69 6e 74  nt nReserve, int
4c050 20 65 46 69 78 29 3b 0a 53 51 4c 49 54 45 5f 50   eFix);.SQLITE_P
4c060 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
4c070 65 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69  e3BtreeGetPageSi
4c080 7a 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49  ze(Btree*);.SQLI
4c090 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4c0a0 71 6c 69 74 65 33 42 74 72 65 65 4d 61 78 50 61  qlite3BtreeMaxPa
4c0b0 67 65 43 6f 75 6e 74 28 42 74 72 65 65 2a 2c 69  geCount(Btree*,i
4c0c0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
4c0d0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
4c0e0 74 72 65 65 47 65 74 52 65 73 65 72 76 65 28 42  treeGetReserve(B
4c0f0 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  tree*);.SQLITE_P
4c100 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
4c110 65 33 42 74 72 65 65 53 65 74 41 75 74 6f 56 61  e3BtreeSetAutoVa
4c120 63 75 75 6d 28 42 74 72 65 65 20 2a 2c 20 69 6e  cuum(Btree *, in
4c130 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
4c140 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
4c150 72 65 65 47 65 74 41 75 74 6f 56 61 63 75 75 6d  reeGetAutoVacuum
4c160 28 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54  (Btree *);.SQLIT
4c170 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4c180 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54  lite3BtreeBeginT
4c190 72 61 6e 73 28 42 74 72 65 65 2a 2c 69 6e 74 29  rans(Btree*,int)
4c1a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4c1b0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
4c1c0 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28  eCommitPhaseOne(
4c1d0 42 74 72 65 65 2a 2c 20 63 6f 6e 73 74 20 63 68  Btree*, const ch
4c1e0 61 72 20 2a 7a 4d 61 73 74 65 72 29 3b 0a 53 51  ar *zMaster);.SQ
4c1f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
4c200 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d   sqlite3BtreeCom
4c210 6d 69 74 50 68 61 73 65 54 77 6f 28 42 74 72 65  mitPhaseTwo(Btre
4c220 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
4c230 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
4c240 74 72 65 65 43 6f 6d 6d 69 74 28 42 74 72 65 65  treeCommit(Btree
4c250 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
4c260 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
4c270 72 65 65 52 6f 6c 6c 62 61 63 6b 28 42 74 72 65  reeRollback(Btre
4c280 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
4c290 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
4c2a0 74 72 65 65 42 65 67 69 6e 53 74 6d 74 28 42 74  treeBeginStmt(Bt
4c2b0 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  ree*,int);.SQLIT
4c2c0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4c2d0 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65  lite3BtreeCreate
4c2e0 54 61 62 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e  Table(Btree*, in
4c2f0 74 2a 2c 20 69 6e 74 20 66 6c 61 67 73 29 3b 0a  t*, int flags);.
4c300 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4c310 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49  nt sqlite3BtreeI
4c320 73 49 6e 54 72 61 6e 73 28 42 74 72 65 65 2a 29  sInTrans(Btree*)
4c330 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4c340 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
4c350 65 49 73 49 6e 52 65 61 64 54 72 61 6e 73 28 42  eIsInReadTrans(B
4c360 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  tree*);.SQLITE_P
4c370 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
4c380 65 33 42 74 72 65 65 49 73 49 6e 42 61 63 6b 75  e3BtreeIsInBacku
4c390 70 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54  p(Btree*);.SQLIT
4c3a0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
4c3b0 73 71 6c 69 74 65 33 42 74 72 65 65 53 63 68 65  sqlite3BtreeSche
4c3c0 6d 61 28 42 74 72 65 65 20 2a 2c 20 69 6e 74 2c  ma(Btree *, int,
4c3d0 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 20 2a 29   void(*)(void *)
4c3e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4c3f0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
4c400 65 65 53 63 68 65 6d 61 4c 6f 63 6b 65 64 28 42  eeSchemaLocked(B
4c410 74 72 65 65 20 2a 70 42 74 72 65 65 29 3b 0a 53  tree *pBtree);.S
4c420 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4c430 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 6f  t sqlite3BtreeLo
4c440 63 6b 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70  ckTable(Btree *p
4c450 42 74 72 65 65 2c 20 69 6e 74 20 69 54 61 62 2c  Btree, int iTab,
4c460 20 75 38 20 69 73 57 72 69 74 65 4c 6f 63 6b 29   u8 isWriteLock)
4c470 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4c480 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
4c490 65 53 61 76 65 70 6f 69 6e 74 28 42 74 72 65 65  eSavepoint(Btree
4c4a0 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a   *, int, int);..
4c4b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
4c4c0 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
4c4d0 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61  e3BtreeGetFilena
4c4e0 6d 65 28 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c  me(Btree *);.SQL
4c4f0 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
4c500 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42  t char *sqlite3B
4c510 74 72 65 65 47 65 74 4a 6f 75 72 6e 61 6c 6e 61  treeGetJournalna
4c520 6d 65 28 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c  me(Btree *);.SQL
4c530 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4c540 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 70 79  sqlite3BtreeCopy
4c550 46 69 6c 65 28 42 74 72 65 65 20 2a 2c 20 42 74  File(Btree *, Bt
4c560 72 65 65 20 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f  ree *);..SQLITE_
4c570 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4c580 74 65 33 42 74 72 65 65 49 6e 63 72 56 61 63 75  te3BtreeIncrVacu
4c590 75 6d 28 42 74 72 65 65 20 2a 29 3b 0a 0a 2f 2a  um(Btree *);../*
4c5a0 20 54 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d   The flags param
4c5b0 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 42  eter to sqlite3B
4c5c0 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 20  treeCreateTable 
4c5d0 63 61 6e 20 62 65 20 74 68 65 20 62 69 74 77 69  can be the bitwi
4c5e0 73 65 20 4f 52 0a 2a 2a 20 6f 66 20 74 68 65 20  se OR.** of the 
4c5f0 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 3a  following flags:
4c600 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45  .*/.#define BTRE
4c610 45 5f 49 4e 54 4b 45 59 20 20 20 20 20 31 20 20  E_INTKEY     1  
4c620 20 20 2f 2a 20 54 61 62 6c 65 20 68 61 73 20 6f    /* Table has o
4c630 6e 6c 79 20 36 34 2d 62 69 74 20 73 69 67 6e 65  nly 64-bit signe
4c640 64 20 69 6e 74 65 67 65 72 20 6b 65 79 73 20 2a  d integer keys *
4c650 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f  /.#define BTREE_
4c660 5a 45 52 4f 44 41 54 41 20 20 20 32 20 20 20 20  ZERODATA   2    
4c670 2f 2a 20 54 61 62 6c 65 20 68 61 73 20 6b 65 79  /* Table has key
4c680 73 20 6f 6e 6c 79 20 2d 20 6e 6f 20 64 61 74 61  s only - no data
4c690 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45   */.#define BTRE
4c6a0 45 5f 4c 45 41 46 44 41 54 41 20 20 20 34 20 20  E_LEAFDATA   4  
4c6b0 20 20 2f 2a 20 44 61 74 61 20 73 74 6f 72 65 64    /* Data stored
4c6c0 20 69 6e 20 6c 65 61 76 65 73 20 6f 6e 6c 79 2e   in leaves only.
4c6d0 20 20 49 6d 70 6c 69 65 73 20 49 4e 54 4b 45 59    Implies INTKEY
4c6e0 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56   */..SQLITE_PRIV
4c6f0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
4c700 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28 42 74  treeDropTable(Bt
4c710 72 65 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2a 29  ree*, int, int*)
4c720 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4c730 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
4c740 65 43 6c 65 61 72 54 61 62 6c 65 28 42 74 72 65  eClearTable(Btre
4c750 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a  e*, int, int*);.
4c760 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
4c770 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
4c780 54 72 69 70 41 6c 6c 43 75 72 73 6f 72 73 28 42  TripAllCursors(B
4c790 74 72 65 65 2a 2c 20 69 6e 74 29 3b 0a 0a 53 51  tree*, int);..SQ
4c7a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
4c7b0 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  d sqlite3BtreeGe
4c7c0 74 4d 65 74 61 28 42 74 72 65 65 20 2a 70 42 74  tMeta(Btree *pBt
4c7d0 72 65 65 2c 20 69 6e 74 20 69 64 78 2c 20 75 33  ree, int idx, u3
4c7e0 32 20 2a 70 56 61 6c 75 65 29 3b 0a 53 51 4c 49  2 *pValue);.SQLI
4c7f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4c800 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74  qlite3BtreeUpdat
4c810 65 4d 65 74 61 28 42 74 72 65 65 2a 2c 20 69 6e  eMeta(Btree*, in
4c820 74 20 69 64 78 2c 20 75 33 32 20 76 61 6c 75 65  t idx, u32 value
4c830 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65  );../*.** The se
4c840 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74  cond parameter t
4c850 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  o sqlite3BtreeGe
4c860 74 4d 65 74 61 20 6f 72 20 73 71 6c 69 74 65 33  tMeta or sqlite3
4c870 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 0a  BtreeUpdateMeta.
4c880 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 6f 6e 65  ** should be one
4c890 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
4c8a0 67 20 76 61 6c 75 65 73 2e 20 54 68 65 20 69 6e  g values. The in
4c8b0 74 65 67 65 72 20 76 61 6c 75 65 73 20 61 72 65  teger values are
4c8c0 20 61 73 73 69 67 6e 65 64 20 0a 2a 2a 20 74 6f   assigned .** to
4c8d0 20 63 6f 6e 73 74 61 6e 74 73 20 73 6f 20 74 68   constants so th
4c8e0 61 74 20 74 68 65 20 6f 66 66 73 65 74 20 6f 66  at the offset of
4c8f0 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
4c900 6e 67 20 66 69 65 6c 64 20 69 6e 20 61 6e 0a 2a  ng field in an.*
4c910 2a 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73  * SQLite databas
4c920 65 20 68 65 61 64 65 72 20 6d 61 79 20 62 65 20  e header may be 
4c930 66 6f 75 6e 64 20 75 73 69 6e 67 20 74 68 65 20  found using the 
4c940 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 75 6c  following formul
4c950 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 6f 66 66 73 65  a:.**.**   offse
4c960 74 20 3d 20 33 36 20 2b 20 28 69 64 78 20 2a 20  t = 36 + (idx * 
4c970 34 29 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61  4).**.** For exa
4c980 6d 70 6c 65 2c 20 74 68 65 20 66 72 65 65 2d 70  mple, the free-p
4c990 61 67 65 2d 63 6f 75 6e 74 20 66 69 65 6c 64 20  age-count field 
4c9a0 69 73 20 6c 6f 63 61 74 65 64 20 61 74 20 62 79  is located at by
4c9b0 74 65 20 6f 66 66 73 65 74 20 33 36 20 6f 66 0a  te offset 36 of.
4c9c0 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ** the database 
4c9d0 66 69 6c 65 20 68 65 61 64 65 72 2e 20 54 68 65  file header. The
4c9e0 20 69 6e 63 72 2d 76 61 63 75 75 6d 2d 66 6c 61   incr-vacuum-fla
4c9f0 67 20 66 69 65 6c 64 20 69 73 20 6c 6f 63 61 74  g field is locat
4ca00 65 64 20 61 74 0a 2a 2a 20 62 79 74 65 20 6f 66  ed at.** byte of
4ca10 66 73 65 74 20 36 34 20 28 3d 3d 20 33 36 2b 34  fset 64 (== 36+4
4ca20 2a 37 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  *7)..*/.#define 
4ca30 42 54 52 45 45 5f 46 52 45 45 5f 50 41 47 45 5f  BTREE_FREE_PAGE_
4ca40 43 4f 55 4e 54 20 20 20 20 20 30 0a 23 64 65 66  COUNT     0.#def
4ca50 69 6e 65 20 42 54 52 45 45 5f 53 43 48 45 4d 41  ine BTREE_SCHEMA
4ca60 5f 56 45 52 53 49 4f 4e 20 20 20 20 20 20 31 0a  _VERSION      1.
4ca70 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 46 49  #define BTREE_FI
4ca80 4c 45 5f 46 4f 52 4d 41 54 20 20 20 20 20 20 20  LE_FORMAT       
4ca90 20 20 32 0a 23 64 65 66 69 6e 65 20 42 54 52 45    2.#define BTRE
4caa0 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f  E_DEFAULT_CACHE_
4cab0 53 49 5a 45 20 20 33 0a 23 64 65 66 69 6e 65 20  SIZE  3.#define 
4cac0 42 54 52 45 45 5f 4c 41 52 47 45 53 54 5f 52 4f  BTREE_LARGEST_RO
4cad0 4f 54 5f 50 41 47 45 20 20 20 34 0a 23 64 65 66  OT_PAGE   4.#def
4cae0 69 6e 65 20 42 54 52 45 45 5f 54 45 58 54 5f 45  ine BTREE_TEXT_E
4caf0 4e 43 4f 44 49 4e 47 20 20 20 20 20 20 20 35 0a  NCODING       5.
4cb00 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 55 53  #define BTREE_US
4cb10 45 52 5f 56 45 52 53 49 4f 4e 20 20 20 20 20 20  ER_VERSION      
4cb20 20 20 36 0a 23 64 65 66 69 6e 65 20 42 54 52 45    6.#define BTRE
4cb30 45 5f 49 4e 43 52 5f 56 41 43 55 55 4d 20 20 20  E_INCR_VACUUM   
4cb40 20 20 20 20 20 20 37 0a 0a 53 51 4c 49 54 45 5f        7..SQLITE_
4cb50 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4cb60 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 28 0a  te3BtreeCursor(.
4cb70 20 20 42 74 72 65 65 2a 2c 20 20 20 20 20 20 20    Btree*,       
4cb80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cb90 20 20 20 20 20 20 20 2f 2a 20 42 54 72 65 65 20         /* BTree 
4cba0 63 6f 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65  containing table
4cbb0 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e   to open */.  in
4cbc0 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20  t iTable,       
4cbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cbe0 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 72     /* Index of r
4cbf0 6f 6f 74 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e  oot page */.  in
4cc00 74 20 77 72 46 6c 61 67 2c 20 20 20 20 20 20 20  t wrFlag,       
4cc10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cc20 20 20 20 2f 2a 20 31 20 66 6f 72 20 77 72 69 74     /* 1 for writ
4cc30 69 6e 67 2e 20 20 30 20 66 6f 72 20 72 65 61 64  ing.  0 for read
4cc40 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 75 63  -only */.  struc
4cc50 74 20 4b 65 79 49 6e 66 6f 2a 2c 20 20 20 20 20  t KeyInfo*,     
4cc60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cc70 2f 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e  /* First argumen
4cc80 74 20 74 6f 20 63 6f 6d 70 61 72 65 20 66 75 6e  t to compare fun
4cc90 63 74 69 6f 6e 20 2a 2f 0a 20 20 42 74 43 75 72  ction */.  BtCur
4cca0 73 6f 72 20 2a 70 43 75 72 73 6f 72 20 20 20 20  sor *pCursor    
4ccb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ccc0 2f 2a 20 53 70 61 63 65 20 74 6f 20 77 72 69 74  /* Space to writ
4ccd0 65 20 63 75 72 73 6f 72 20 73 74 72 75 63 74 75  e cursor structu
4cce0 72 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f  re */.);.SQLITE_
4ccf0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4cd00 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 53 69  te3BtreeCursorSi
4cd10 7a 65 28 76 6f 69 64 29 3b 0a 0a 53 51 4c 49 54  ze(void);..SQLIT
4cd20 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4cd30 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43  lite3BtreeCloseC
4cd40 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 2a 29  ursor(BtCursor*)
4cd50 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4cd60 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
4cd70 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28  eMovetoUnpacked(
4cd80 0a 20 20 42 74 43 75 72 73 6f 72 2a 2c 0a 20 20  .  BtCursor*,.  
4cd90 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a  UnpackedRecord *
4cda0 70 55 6e 4b 65 79 2c 0a 20 20 69 36 34 20 69 6e  pUnKey,.  i64 in
4cdb0 74 4b 65 79 2c 0a 20 20 69 6e 74 20 62 69 61 73  tKey,.  int bias
4cdc0 2c 0a 20 20 69 6e 74 20 2a 70 52 65 73 0a 29 3b  ,.  int *pRes.);
4cdd0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4cde0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
4cdf0 43 75 72 73 6f 72 48 61 73 4d 6f 76 65 64 28 42  CursorHasMoved(B
4ce00 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 2a 29 3b  tCursor*, int*);
4ce10 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4ce20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
4ce30 44 65 6c 65 74 65 28 42 74 43 75 72 73 6f 72 2a  Delete(BtCursor*
4ce40 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4ce50 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
4ce60 65 65 49 6e 73 65 72 74 28 42 74 43 75 72 73 6f  eeInsert(BtCurso
4ce70 72 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  r*, const void *
4ce80 70 4b 65 79 2c 20 69 36 34 20 6e 4b 65 79 2c 0a  pKey, i64 nKey,.
4ce90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ceb0 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 44    const void *pD
4cec0 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a  ata, int nData,.
4ced0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4cef0 20 20 69 6e 74 20 6e 5a 65 72 6f 2c 20 69 6e 74    int nZero, int
4cf00 20 62 69 61 73 2c 20 69 6e 74 20 73 65 65 6b 52   bias, int seekR
4cf10 65 73 75 6c 74 29 3b 0a 53 51 4c 49 54 45 5f 50  esult);.SQLITE_P
4cf20 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
4cf30 65 33 42 74 72 65 65 46 69 72 73 74 28 42 74 43  e3BtreeFirst(BtC
4cf40 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 65  ursor*, int *pRe
4cf50 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  s);.SQLITE_PRIVA
4cf60 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
4cf70 72 65 65 4c 61 73 74 28 42 74 43 75 72 73 6f 72  reeLast(BtCursor
4cf80 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53  *, int *pRes);.S
4cf90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4cfa0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65  t sqlite3BtreeNe
4cfb0 78 74 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e  xt(BtCursor*, in
4cfc0 74 20 2a 70 52 65 73 29 3b 0a 53 51 4c 49 54 45  t *pRes);.SQLITE
4cfd0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4cfe0 69 74 65 33 42 74 72 65 65 45 6f 66 28 42 74 43  ite3BtreeEof(BtC
4cff0 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  ursor*);.SQLITE_
4d000 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4d010 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73  te3BtreePrevious
4d020 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20  (BtCursor*, int 
4d030 2a 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50  *pRes);.SQLITE_P
4d040 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
4d050 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 42  e3BtreeKeySize(B
4d060 74 43 75 72 73 6f 72 2a 2c 20 69 36 34 20 2a 70  tCursor*, i64 *p
4d070 53 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Size);.SQLITE_PR
4d080 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4d090 33 42 74 72 65 65 4b 65 79 28 42 74 43 75 72 73  3BtreeKey(BtCurs
4d0a0 6f 72 2a 2c 20 75 33 32 20 6f 66 66 73 65 74 2c  or*, u32 offset,
4d0b0 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 2a 29   u32 amt, void*)
4d0c0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4d0d0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c   const void *sql
4d0e0 69 74 65 33 42 74 72 65 65 4b 65 79 46 65 74 63  ite3BtreeKeyFetc
4d0f0 68 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74  h(BtCursor*, int
4d100 20 2a 70 41 6d 74 29 3b 0a 53 51 4c 49 54 45 5f   *pAmt);.SQLITE_
4d110 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f  PRIVATE const vo
4d120 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65  id *sqlite3Btree
4d130 44 61 74 61 46 65 74 63 68 28 42 74 43 75 72 73  DataFetch(BtCurs
4d140 6f 72 2a 2c 20 69 6e 74 20 2a 70 41 6d 74 29 3b  or*, int *pAmt);
4d150 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4d160 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
4d170 44 61 74 61 53 69 7a 65 28 42 74 43 75 72 73 6f  DataSize(BtCurso
4d180 72 2a 2c 20 75 33 32 20 2a 70 53 69 7a 65 29 3b  r*, u32 *pSize);
4d190 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4d1a0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
4d1b0 44 61 74 61 28 42 74 43 75 72 73 6f 72 2a 2c 20  Data(BtCursor*, 
4d1c0 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20  u32 offset, u32 
4d1d0 61 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c  amt, void*);.SQL
4d1e0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
4d1f0 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74   sqlite3BtreeSet
4d200 43 61 63 68 65 64 52 6f 77 69 64 28 42 74 43 75  CachedRowid(BtCu
4d210 72 73 6f 72 2a 2c 20 73 71 6c 69 74 65 33 5f 69  rsor*, sqlite3_i
4d220 6e 74 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52  nt64);.SQLITE_PR
4d230 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 69 6e  IVATE sqlite3_in
4d240 74 36 34 20 73 71 6c 69 74 65 33 42 74 72 65 65  t64 sqlite3Btree
4d250 47 65 74 43 61 63 68 65 64 52 6f 77 69 64 28 42  GetCachedRowid(B
4d260 74 43 75 72 73 6f 72 2a 29 3b 0a 0a 53 51 4c 49  tCursor*);..SQLI
4d270 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20  TE_PRIVATE char 
4d280 2a 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 74  *sqlite3BtreeInt
4d290 65 67 72 69 74 79 43 68 65 63 6b 28 42 74 72 65  egrityCheck(Btre
4d2a0 65 2a 2c 20 69 6e 74 20 2a 61 52 6f 6f 74 2c 20  e*, int *aRoot, 
4d2b0 69 6e 74 20 6e 52 6f 6f 74 2c 20 69 6e 74 2c 20  int nRoot, int, 
4d2c0 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  int*);.SQLITE_PR
4d2d0 49 56 41 54 45 20 73 74 72 75 63 74 20 50 61 67  IVATE struct Pag
4d2e0 65 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65  er *sqlite3Btree
4d2f0 50 61 67 65 72 28 42 74 72 65 65 2a 29 3b 0a 0a  Pager(Btree*);..
4d300 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4d310 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 50  nt sqlite3BtreeP
4d320 75 74 44 61 74 61 28 42 74 43 75 72 73 6f 72 2a  utData(BtCursor*
4d330 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33  , u32 offset, u3
4d340 32 20 61 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a 53  2 amt, void*);.S
4d350 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
4d360 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  id sqlite3BtreeC
4d370 61 63 68 65 4f 76 65 72 66 6c 6f 77 28 42 74 43  acheOverflow(BtC
4d380 75 72 73 6f 72 20 2a 29 3b 0a 53 51 4c 49 54 45  ursor *);.SQLITE
4d390 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
4d3a0 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 43  lite3BtreeClearC
4d3b0 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a  ursor(BtCursor *
4d3c0 29 3b 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  );..#ifndef NDEB
4d3d0 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  UG.SQLITE_PRIVAT
4d3e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
4d3f0 65 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28  eeCursorIsValid(
4d400 42 74 43 75 72 73 6f 72 2a 29 3b 0a 23 65 6e 64  BtCursor*);.#end
4d410 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
4d420 54 45 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f 55  TE_OMIT_BTREECOU
4d430 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  NT.SQLITE_PRIVAT
4d440 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
4d450 65 65 43 6f 75 6e 74 28 42 74 43 75 72 73 6f 72  eeCount(BtCursor
4d460 20 2a 2c 20 69 36 34 20 2a 29 3b 0a 23 65 6e 64   *, i64 *);.#end
4d470 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
4d480 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52  E_TEST.SQLITE_PR
4d490 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4d4a0 33 42 74 72 65 65 43 75 72 73 6f 72 49 6e 66 6f  3BtreeCursorInfo
4d4b0 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 2a  (BtCursor*, int*
4d4c0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
4d4d0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
4d4e0 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 4c 69  te3BtreeCursorLi
4d4f0 73 74 28 42 74 72 65 65 2a 29 3b 0a 23 65 6e 64  st(Btree*);.#end
4d500 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20  if../*.** If we 
4d510 61 72 65 20 6e 6f 74 20 75 73 69 6e 67 20 73 68  are not using sh
4d520 61 72 65 64 20 63 61 63 68 65 2c 20 74 68 65 6e  ared cache, then
4d530 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65   there is no nee
4d540 64 20 74 6f 0a 2a 2a 20 75 73 65 20 6d 75 74 65  d to.** use mute
4d550 78 65 73 20 74 6f 20 61 63 63 65 73 73 20 74 68  xes to access th
4d560 65 20 42 74 53 68 61 72 65 64 20 73 74 72 75 63  e BtShared struc
4d570 74 75 72 65 73 2e 20 20 53 6f 20 6d 61 6b 65 20  tures.  So make 
4d580 74 68 65 0a 2a 2a 20 45 6e 74 65 72 20 61 6e 64  the.** Enter and
4d590 20 4c 65 61 76 65 20 70 72 6f 63 65 64 75 72 65   Leave procedure
4d5a0 73 20 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 23 69 66  s no-ops..*/.#if
4d5b0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
4d5c0 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 53 51  _SHARED_CACHE.SQ
4d5d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
4d5e0 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
4d5f0 45 6e 74 65 72 28 42 74 72 65 65 2a 29 3b 0a 53  Enter(Btree*);.S
4d600 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
4d610 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
4d620 65 45 6e 74 65 72 41 6c 6c 28 73 71 6c 69 74 65  eEnterAll(sqlite
4d630 33 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66  3*);.#else.# def
4d640 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65  ine sqlite3Btree
4d650 45 6e 74 65 72 28 58 29 20 0a 23 20 64 65 66 69  Enter(X) .# defi
4d660 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  ne sqlite3BtreeE
4d670 6e 74 65 72 41 6c 6c 28 58 29 0a 23 65 6e 64 69  nterAll(X).#endi
4d680 66 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  f..#if !defined(
4d690 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
4d6a0 45 44 5f 43 41 43 48 45 29 20 26 26 20 53 51 4c  ED_CACHE) && SQL
4d6b0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 53  ITE_THREADSAFE.S
4d6c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
4d6d0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
4d6e0 65 4c 65 61 76 65 28 42 74 72 65 65 2a 29 3b 0a  eLeave(Btree*);.
4d6f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
4d700 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
4d710 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28 42 74  eeEnterCursor(Bt
4d720 43 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45  Cursor*);.SQLITE
4d730 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
4d740 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
4d750 65 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72  eCursor(BtCursor
4d760 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
4d770 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
4d780 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 73  3BtreeLeaveAll(s
4d790 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45  qlite3*);.SQLITE
4d7a0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
4d7b0 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65  sqlite3BtreeMute
4d7c0 78 41 72 72 61 79 45 6e 74 65 72 28 42 74 72 65  xArrayEnter(Btre
4d7d0 65 4d 75 74 65 78 41 72 72 61 79 2a 29 3b 0a 53  eMutexArray*);.S
4d7e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
4d7f0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
4d800 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76 65  eMutexArrayLeave
4d810 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79  (BtreeMutexArray
4d820 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
4d830 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
4d840 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79  3BtreeMutexArray
4d850 49 6e 73 65 72 74 28 42 74 72 65 65 4d 75 74 65  Insert(BtreeMute
4d860 78 41 72 72 61 79 2a 2c 20 42 74 72 65 65 2a 29  xArray*, Btree*)
4d870 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ;.#ifndef NDEBUG
4d880 0a 20 20 2f 2a 20 54 68 65 73 65 20 72 6f 75 74  .  /* These rout
4d890 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 69 6e  ines are used in
4d8a0 73 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74  side assert() st
4d8b0 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a  atements only. *
4d8c0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
4d8d0 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74     int sqlite3Bt
4d8e0 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 42 74  reeHoldsMutex(Bt
4d8f0 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ree*);.SQLITE_PR
4d900 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69  IVATE   int sqli
4d910 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c  te3BtreeHoldsAll
4d920 4d 75 74 65 78 65 73 28 73 71 6c 69 74 65 33 2a  Mutexes(sqlite3*
4d930 29 3b 0a 23 65 6e 64 69 66 0a 23 65 6c 73 65 0a  );.#endif.#else.
4d940 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
4d950 33 42 74 72 65 65 4c 65 61 76 65 28 58 29 0a 23  3BtreeLeave(X).#
4d960 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42   define sqlite3B
4d970 74 72 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28  treeEnterCursor(
4d980 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  X).# define sqli
4d990 74 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72  te3BtreeLeaveCur
4d9a0 73 6f 72 28 58 29 0a 23 20 64 65 66 69 6e 65 20  sor(X).# define 
4d9b0 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
4d9c0 65 41 6c 6c 28 58 29 0a 23 20 64 65 66 69 6e 65  eAll(X).# define
4d9d0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74   sqlite3BtreeMut
4d9e0 65 78 41 72 72 61 79 45 6e 74 65 72 28 58 29 0a  exArrayEnter(X).
4d9f0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
4da00 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c  BtreeMutexArrayL
4da10 65 61 76 65 28 58 29 0a 23 20 64 65 66 69 6e 65  eave(X).# define
4da20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74   sqlite3BtreeMut
4da30 65 78 41 72 72 61 79 49 6e 73 65 72 74 28 58 2c  exArrayInsert(X,
4da40 59 29 0a 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  Y)..# define sql
4da50 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75  ite3BtreeHoldsMu
4da60 74 65 78 28 58 29 20 31 0a 23 20 64 65 66 69 6e  tex(X) 1.# defin
4da70 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f  e sqlite3BtreeHo
4da80 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 58 29  ldsAllMutexes(X)
4da90 20 31 0a 23 65 6e 64 69 66 0a 0a 0a 23 65 6e 64   1.#endif...#end
4daa0 69 66 20 2f 2a 20 5f 42 54 52 45 45 5f 48 5f 20  if /* _BTREE_H_ 
4dab0 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
4dac0 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 72 65 65  *** End of btree
4dad0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
4dae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4daf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4db00 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
4db10 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77  *** Continuing w
4db20 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66  here we left off
4db30 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20   in sqliteInt.h 
4db40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4db50 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
4db60 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 76 64 62 65  *** Include vdbe
4db70 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  .h in the middle
4db80 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20   of sqliteInt.h 
4db90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4dba0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
4dbb0 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76  *** Begin file v
4dbc0 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dbe.h **********
4dbd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4dbe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4dbf0 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53  **/./*.** 2001 S
4dc00 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a  eptember 15.**.*
4dc10 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
4dc20 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
4dc30 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
4dc40 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
4dc50 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
4dc60 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
4dc70 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
4dc80 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
4dc90 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
4dca0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
4dcb0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
4dcc0 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
4dcd0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
4dce0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
4dcf0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
4dd00 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
4dd10 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
4dd20 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
4dd30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4dd40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4dd50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4dd60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65  **********.** He
4dd70 61 64 65 72 20 66 69 6c 65 20 66 6f 72 20 74 68  ader file for th
4dd80 65 20 56 69 72 74 75 61 6c 20 44 61 74 61 42 61  e Virtual DataBa
4dd90 73 65 20 45 6e 67 69 6e 65 20 28 56 44 42 45 29  se Engine (VDBE)
4dda0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64  .**.** This head
4ddb0 65 72 20 64 65 66 69 6e 65 73 20 74 68 65 20 69  er defines the i
4ddc0 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68 65 20  nterface to the 
4ddd0 76 69 72 74 75 61 6c 20 64 61 74 61 62 61 73 65  virtual database
4dde0 20 65 6e 67 69 6e 65 0a 2a 2a 20 6f 72 20 56 44   engine.** or VD
4ddf0 42 45 2e 20 20 54 68 65 20 56 44 42 45 20 69 6d  BE.  The VDBE im
4de00 70 6c 65 6d 65 6e 74 73 20 61 6e 20 61 62 73 74  plements an abst
4de10 72 61 63 74 20 6d 61 63 68 69 6e 65 20 74 68 61  ract machine tha
4de20 74 20 72 75 6e 73 20 61 0a 2a 2a 20 73 69 6d 70  t runs a.** simp
4de30 6c 65 20 70 72 6f 67 72 61 6d 20 74 6f 20 61 63  le program to ac
4de40 63 65 73 73 20 61 6e 64 20 6d 6f 64 69 66 79 20  cess and modify 
4de50 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 64  the underlying d
4de60 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 24  atabase..**.** $
4de70 49 64 3a 20 76 64 62 65 2e 68 2c 76 20 31 2e 31  Id: vdbe.h,v 1.1
4de80 34 32 20 32 30 30 39 2f 30 37 2f 32 34 20 31 37  42 2009/07/24 17
4de90 3a 35 38 3a 35 33 20 64 61 6e 69 65 6c 6b 31 39  :58:53 danielk19
4dea0 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e  77 Exp $.*/.#ifn
4deb0 64 65 66 20 5f 53 51 4c 49 54 45 5f 56 44 42 45  def _SQLITE_VDBE
4dec0 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c  _H_.#define _SQL
4ded0 49 54 45 5f 56 44 42 45 5f 48 5f 0a 0a 2f 2a 0a  ITE_VDBE_H_../*.
4dee0 2a 2a 20 41 20 73 69 6e 67 6c 65 20 56 44 42 45  ** A single VDBE
4def0 20 69 73 20 61 6e 20 6f 70 61 71 75 65 20 73 74   is an opaque st
4df00 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 56  ructure named "V
4df10 64 62 65 22 2e 20 20 4f 6e 6c 79 20 72 6f 75 74  dbe".  Only rout
4df20 69 6e 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73  ines.** in the s
4df30 6f 75 72 63 65 20 66 69 6c 65 20 73 71 6c 69 74  ource file sqlit
4df40 65 56 64 62 65 2e 63 20 61 72 65 20 61 6c 6c 6f  eVdbe.c are allo
4df50 77 65 64 20 74 6f 20 73 65 65 20 74 68 65 20 69  wed to see the i
4df60 6e 73 69 64 65 73 0a 2a 2a 20 6f 66 20 74 68 69  nsides.** of thi
4df70 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a  s structure..*/.
4df80 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 56  typedef struct V
4df90 64 62 65 20 56 64 62 65 3b 0a 0a 2f 2a 0a 2a 2a  dbe Vdbe;../*.**
4dfa0 20 54 68 65 20 6e 61 6d 65 73 20 6f 66 20 74 68   The names of th
4dfb0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 79 70 65  e following type
4dfc0 73 20 64 65 63 6c 61 72 65 64 20 69 6e 20 76 64  s declared in vd
4dfd0 62 65 49 6e 74 2e 68 20 61 72 65 20 72 65 71 75  beInt.h are requ
4dfe0 69 72 65 64 0a 2a 2a 20 66 6f 72 20 74 68 65 20  ired.** for the 
4dff0 56 64 62 65 4f 70 20 64 65 66 69 6e 69 74 69 6f  VdbeOp definitio
4e000 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  n..*/.typedef st
4e010 72 75 63 74 20 56 64 62 65 46 75 6e 63 20 56 64  ruct VdbeFunc Vd
4e020 62 65 46 75 6e 63 3b 0a 74 79 70 65 64 65 66 20  beFunc;.typedef 
4e030 73 74 72 75 63 74 20 4d 65 6d 20 4d 65 6d 3b 0a  struct Mem Mem;.
4e040 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53  typedef struct S
4e050 75 62 50 72 6f 67 72 61 6d 20 53 75 62 50 72 6f  ubProgram SubPro
4e060 67 72 61 6d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 73  gram;../*.** A s
4e070 69 6e 67 6c 65 20 69 6e 73 74 72 75 63 74 69 6f  ingle instructio
4e080 6e 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c  n of the virtual
4e090 20 6d 61 63 68 69 6e 65 20 68 61 73 20 61 6e 20   machine has an 
4e0a0 6f 70 63 6f 64 65 0a 2a 2a 20 61 6e 64 20 61 73  opcode.** and as
4e0b0 20 6d 61 6e 79 20 61 73 20 74 68 72 65 65 20 6f   many as three o
4e0c0 70 65 72 61 6e 64 73 2e 20 20 54 68 65 20 69 6e  perands.  The in
4e0d0 73 74 72 75 63 74 69 6f 6e 20 69 73 20 72 65 63  struction is rec
4e0e0 6f 72 64 65 64 0a 2a 2a 20 61 73 20 61 6e 20 69  orded.** as an i
4e0f0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
4e100 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
4e110 72 65 3a 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64  re:.*/.struct Vd
4e120 62 65 4f 70 20 7b 0a 20 20 75 38 20 6f 70 63 6f  beOp {.  u8 opco
4e130 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  de;          /* 
4e140 57 68 61 74 20 6f 70 65 72 61 74 69 6f 6e 20 74  What operation t
4e150 6f 20 70 65 72 66 6f 72 6d 20 2a 2f 0a 20 20 73  o perform */.  s
4e160 69 67 6e 65 64 20 63 68 61 72 20 70 34 74 79 70  igned char p4typ
4e170 65 3b 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65  e; /* One of the
4e180 20 50 34 5f 78 78 78 20 63 6f 6e 73 74 61 6e 74   P4_xxx constant
4e190 73 20 66 6f 72 20 70 34 20 2a 2f 0a 20 20 75 38  s for p4 */.  u8
4e1a0 20 6f 70 66 6c 61 67 73 3b 20 20 20 20 20 20 20   opflags;       
4e1b0 20 20 2f 2a 20 4e 6f 74 20 63 75 72 72 65 6e 74    /* Not current
4e1c0 6c 79 20 75 73 65 64 20 2a 2f 0a 20 20 75 38 20  ly used */.  u8 
4e1d0 70 35 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  p5;             
4e1e0 20 2f 2a 20 46 69 66 74 68 20 70 61 72 61 6d 65   /* Fifth parame
4e1f0 74 65 72 20 69 73 20 61 6e 20 75 6e 73 69 67 6e  ter is an unsign
4e200 65 64 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a  ed character */.
4e210 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20    int p1;       
4e220 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6f        /* First o
4e230 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20  perand */.  int 
4e240 70 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  p2;             
4e250 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 61 6d 65  /* Second parame
4e260 74 65 72 20 28 6f 66 74 65 6e 20 74 68 65 20 6a  ter (often the j
4e270 75 6d 70 20 64 65 73 74 69 6e 61 74 69 6f 6e 29  ump destination)
4e280 20 2a 2f 0a 20 20 69 6e 74 20 70 33 3b 20 20 20   */.  int p3;   
4e290 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
4e2a0 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72   third parameter
4e2b0 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 20 20 20   */.  union {   
4e2c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6f 75            /* fou
4e2d0 72 74 68 20 70 61 72 61 6d 65 74 65 72 20 2a 2f  rth parameter */
4e2e0 0a 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20  .    int i;     
4e2f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
4e300 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69 66 20  nteger value if 
4e310 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32  p4type==P4_INT32
4e320 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 3b   */.    void *p;
4e330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4e340 2a 20 47 65 6e 65 72 69 63 20 70 6f 69 6e 74 65  * Generic pointe
4e350 72 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a  r */.    char *z
4e360 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
4e370 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 61  /* Pointer to da
4e380 74 61 20 66 6f 72 20 73 74 72 69 6e 67 20 28 63  ta for string (c
4e390 68 61 72 20 61 72 72 61 79 29 20 74 79 70 65 73  har array) types
4e3a0 20 2a 2f 0a 20 20 20 20 69 36 34 20 2a 70 49 36   */.    i64 *pI6
4e3b0 34 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  4;             /
4e3c0 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79  * Used when p4ty
4e3d0 70 65 20 69 73 20 50 34 5f 49 4e 54 36 34 20 2a  pe is P4_INT64 *
4e3e0 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20 2a 70 52  /.    double *pR
4e3f0 65 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  eal;         /* 
4e400 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 65  Used when p4type
4e410 20 69 73 20 50 34 5f 52 45 41 4c 20 2a 2f 0a 20   is P4_REAL */. 
4e420 20 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e     FuncDef *pFun
4e430 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65  c;        /* Use
4e440 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73  d when p4type is
4e450 20 50 34 5f 46 55 4e 43 44 45 46 20 2a 2f 0a 20   P4_FUNCDEF */. 
4e460 20 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64     VdbeFunc *pVd
4e470 62 65 46 75 6e 63 3b 20 20 20 2f 2a 20 55 73 65  beFunc;   /* Use
4e480 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73  d when p4type is
4e490 20 50 34 5f 56 44 42 45 46 55 4e 43 20 2a 2f 0a   P4_VDBEFUNC */.
4e4a0 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f      CollSeq *pCo
4e4b0 6c 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73  ll;        /* Us
4e4c0 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69  ed when p4type i
4e4d0 73 20 50 34 5f 43 4f 4c 4c 53 45 51 20 2a 2f 0a  s P4_COLLSEQ */.
4e4e0 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20      Mem *pMem;  
4e4f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73             /* Us
4e500 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69  ed when p4type i
4e510 73 20 50 34 5f 4d 45 4d 20 2a 2f 0a 20 20 20 20  s P4_MEM */.    
4e520 56 54 61 62 6c 65 20 2a 70 56 74 61 62 3b 20 20  VTable *pVtab;  
4e530 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77         /* Used w
4e540 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 34  hen p4type is P4
4e550 5f 56 54 41 42 20 2a 2f 0a 20 20 20 20 4b 65 79  _VTAB */.    Key
4e560 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20  Info *pKeyInfo; 
4e570 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e      /* Used when
4e580 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 4b 45   p4type is P4_KE
4e590 59 49 4e 46 4f 20 2a 2f 0a 20 20 20 20 69 6e 74  YINFO */.    int
4e5a0 20 2a 61 69 3b 20 20 20 20 20 20 20 20 20 20 20   *ai;           
4e5b0 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e      /* Used when
4e5c0 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 49 4e   p4type is P4_IN
4e5d0 54 41 52 52 41 59 20 2a 2f 0a 20 20 20 20 53 75  TARRAY */.    Su
4e5e0 62 50 72 6f 67 72 61 6d 20 2a 70 50 72 6f 67 72  bProgram *pProgr
4e5f0 61 6d 3b 20 20 2f 2a 20 55 73 65 64 20 77 68 65  am;  /* Used whe
4e600 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 53  n p4type is P4_S
4e610 55 42 50 52 4f 47 52 41 4d 20 2a 2f 0a 20 20 7d  UBPROGRAM */.  }
4e620 20 70 34 3b 0a 23 69 66 64 65 66 20 53 51 4c 49   p4;.#ifdef SQLI
4e630 54 45 5f 44 45 42 55 47 0a 20 20 63 68 61 72 20  TE_DEBUG.  char 
4e640 2a 7a 43 6f 6d 6d 65 6e 74 3b 20 20 20 20 20 20  *zComment;      
4e650 20 20 20 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 74      /* Comment t
4e660 6f 20 69 6d 70 72 6f 76 65 20 72 65 61 64 61 62  o improve readab
4e670 69 6c 69 74 79 20 2a 2f 0a 23 65 6e 64 69 66 0a  ility */.#endif.
4e680 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46  #ifdef VDBE_PROF
4e690 49 4c 45 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20  ILE.  int cnt;  
4e6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4e6b0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65  * Number of time
4e6c0 73 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69  s this instructi
4e6d0 6f 6e 20 77 61 73 20 65 78 65 63 75 74 65 64 20  on was executed 
4e6e0 2a 2f 0a 20 20 75 36 34 20 63 79 63 6c 65 73 3b  */.  u64 cycles;
4e6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4e700 20 54 6f 74 61 6c 20 74 69 6d 65 20 73 70 65 6e   Total time spen
4e710 74 20 65 78 65 63 75 74 69 6e 67 20 74 68 69 73  t executing this
4e720 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a   instruction */.
4e730 23 65 6e 64 69 66 0a 7d 3b 0a 74 79 70 65 64 65  #endif.};.typede
4e740 66 20 73 74 72 75 63 74 20 56 64 62 65 4f 70 20  f struct VdbeOp 
4e750 56 64 62 65 4f 70 3b 0a 0a 0a 2f 2a 0a 2a 2a 20  VdbeOp;.../*.** 
4e760 41 20 73 75 62 2d 72 6f 75 74 69 6e 65 20 75 73  A sub-routine us
4e770 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  ed to implement 
4e780 61 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61  a trigger progra
4e790 6d 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 75 62  m..*/.struct Sub
4e7a0 50 72 6f 67 72 61 6d 20 7b 0a 20 20 56 64 62 65  Program {.  Vdbe
4e7b0 4f 70 20 2a 61 4f 70 3b 20 20 20 20 20 20 20 20  Op *aOp;        
4e7c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72            /* Arr
4e7d0 61 79 20 6f 66 20 6f 70 63 6f 64 65 73 20 66 6f  ay of opcodes fo
4e7e0 72 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f  r sub-program */
4e7f0 0a 20 20 69 6e 74 20 6e 4f 70 3b 20 20 20 20 20  .  int nOp;     
4e800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4e810 20 2f 2a 20 45 6c 65 6d 65 6e 74 73 20 69 6e 20   /* Elements in 
4e820 61 4f 70 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e  aOp[] */.  int n
4e830 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  Mem;            
4e840 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
4e850 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c  er of memory cel
4e860 6c 73 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20  ls required */. 
4e870 20 69 6e 74 20 6e 43 73 72 3b 20 20 20 20 20 20   int nCsr;      
4e880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4e890 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 75 72 73  * Number of curs
4e8a0 6f 72 73 20 72 65 71 75 69 72 65 64 20 2a 2f 0a  ors required */.
4e8b0 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20    int nRef;     
4e8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4e8d0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69  /* Number of poi
4e8e0 6e 74 65 72 73 20 74 6f 20 74 68 69 73 20 73 74  nters to this st
4e8f0 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 76 6f 69  ructure */.  voi
4e900 64 20 2a 74 6f 6b 65 6e 3b 20 20 20 20 20 20 20  d *token;       
4e910 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 64             /* id
4e920 20 74 68 61 74 20 6d 61 79 20 62 65 20 75 73 65   that may be use
4e930 64 20 74 6f 20 72 65 63 75 72 73 69 76 65 20 74  d to recursive t
4e940 72 69 67 67 65 72 73 20 2a 2f 0a 7d 3b 0a 0a 2f  riggers */.};../
4e950 2a 0a 2a 2a 20 41 20 73 6d 61 6c 6c 65 72 20 76  *.** A smaller v
4e960 65 72 73 69 6f 6e 20 6f 66 20 56 64 62 65 4f 70  ersion of VdbeOp
4e970 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 56 64   used for the Vd
4e980 62 65 41 64 64 4f 70 4c 69 73 74 28 29 20 66 75  beAddOpList() fu
4e990 6e 63 74 69 6f 6e 20 62 65 63 61 75 73 65 0a 2a  nction because.*
4e9a0 2a 20 69 74 20 74 61 6b 65 73 20 75 70 20 6c 65  * it takes up le
4e9b0 73 73 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 72  ss space..*/.str
4e9c0 75 63 74 20 56 64 62 65 4f 70 4c 69 73 74 20 7b  uct VdbeOpList {
4e9d0 0a 20 20 75 38 20 6f 70 63 6f 64 65 3b 20 20 20  .  u8 opcode;   
4e9e0 20 20 20 20 20 20 20 2f 2a 20 57 68 61 74 20 6f         /* What o
4e9f0 70 65 72 61 74 69 6f 6e 20 74 6f 20 70 65 72 66  peration to perf
4ea00 6f 72 6d 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20  orm */.  signed 
4ea10 63 68 61 72 20 70 31 3b 20 20 20 20 20 2f 2a 20  char p1;     /* 
4ea20 46 69 72 73 74 20 6f 70 65 72 61 6e 64 20 2a 2f  First operand */
4ea30 0a 20 20 73 69 67 6e 65 64 20 63 68 61 72 20 70  .  signed char p
4ea40 32 3b 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64  2;     /* Second
4ea50 20 70 61 72 61 6d 65 74 65 72 20 28 6f 66 74 65   parameter (ofte
4ea60 6e 20 74 68 65 20 6a 75 6d 70 20 64 65 73 74 69  n the jump desti
4ea70 6e 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 73 69 67  nation) */.  sig
4ea80 6e 65 64 20 63 68 61 72 20 70 33 3b 20 20 20 20  ned char p3;    
4ea90 20 2f 2a 20 54 68 69 72 64 20 70 61 72 61 6d 65   /* Third parame
4eaa0 74 65 72 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65  ter */.};.typede
4eab0 66 20 73 74 72 75 63 74 20 56 64 62 65 4f 70 4c  f struct VdbeOpL
4eac0 69 73 74 20 56 64 62 65 4f 70 4c 69 73 74 3b 0a  ist VdbeOpList;.
4ead0 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76  ./*.** Allowed v
4eae0 61 6c 75 65 73 20 6f 66 20 56 64 62 65 4f 70 2e  alues of VdbeOp.
4eaf0 70 34 74 79 70 65 0a 2a 2f 0a 23 64 65 66 69 6e  p4type.*/.#defin
4eb00 65 20 50 34 5f 4e 4f 54 55 53 45 44 20 20 20 20  e P4_NOTUSED    
4eb10 30 20 20 20 2f 2a 20 54 68 65 20 50 34 20 70 61  0   /* The P4 pa
4eb20 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 75  rameter is not u
4eb30 73 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  sed */.#define P
4eb40 34 5f 44 59 4e 41 4d 49 43 20 20 28 2d 31 29 20  4_DYNAMIC  (-1) 
4eb50 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61   /* Pointer to a
4eb60 20 73 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64   string obtained
4eb70 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c   from sqliteMall
4eb80 6f 63 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20  oc() */.#define 
4eb90 50 34 5f 53 54 41 54 49 43 20 20 20 28 2d 32 29  P4_STATIC   (-2)
4eba0 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
4ebb0 61 20 73 74 61 74 69 63 20 73 74 72 69 6e 67 20  a static string 
4ebc0 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 43 4f  */.#define P4_CO
4ebd0 4c 4c 53 45 51 20 20 28 2d 34 29 20 20 2f 2a 20  LLSEQ  (-4)  /* 
4ebe0 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  P4 is a pointer 
4ebf0 74 6f 20 61 20 43 6f 6c 6c 53 65 71 20 73 74 72  to a CollSeq str
4ec00 75 63 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e  ucture */.#defin
4ec10 65 20 50 34 5f 46 55 4e 43 44 45 46 20 20 28 2d  e P4_FUNCDEF  (-
4ec20 35 29 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70  5)  /* P4 is a p
4ec30 6f 69 6e 74 65 72 20 74 6f 20 61 20 46 75 6e 63  ointer to a Func
4ec40 44 65 66 20 73 74 72 75 63 74 75 72 65 20 2a 2f  Def structure */
4ec50 0a 23 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49  .#define P4_KEYI
4ec60 4e 46 4f 20 20 28 2d 36 29 20 20 2f 2a 20 50 34  NFO  (-6)  /* P4
4ec70 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
4ec80 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63   a KeyInfo struc
4ec90 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ture */.#define 
4eca0 50 34 5f 56 44 42 45 46 55 4e 43 20 28 2d 37 29  P4_VDBEFUNC (-7)
4ecb0 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69    /* P4 is a poi
4ecc0 6e 74 65 72 20 74 6f 20 61 20 56 64 62 65 46 75  nter to a VdbeFu
4ecd0 6e 63 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a  nc structure */.
4ece0 23 64 65 66 69 6e 65 20 50 34 5f 4d 45 4d 20 20  #define P4_MEM  
4ecf0 20 20 20 20 28 2d 38 29 20 20 2f 2a 20 50 34 20      (-8)  /* P4 
4ed00 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
4ed10 61 20 4d 65 6d 2a 20 20 20 20 73 74 72 75 63 74  a Mem*    struct
4ed20 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ure */.#define P
4ed30 34 5f 54 52 41 4e 53 49 45 4e 54 20 28 2d 39 29  4_TRANSIENT (-9)
4ed40 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e   /* P4 is a poin
4ed50 74 65 72 20 74 6f 20 61 20 74 72 61 6e 73 69 65  ter to a transie
4ed60 6e 74 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65  nt string */.#de
4ed70 66 69 6e 65 20 50 34 5f 56 54 41 42 20 20 20 20  fine P4_VTAB    
4ed80 20 28 2d 31 30 29 20 2f 2a 20 50 34 20 69 73 20   (-10) /* P4 is 
4ed90 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20  a pointer to an 
4eda0 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72  sqlite3_vtab str
4edb0 75 63 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e  ucture */.#defin
4edc0 65 20 50 34 5f 4d 50 52 49 4e 54 46 20 20 28 2d  e P4_MPRINTF  (-
4edd0 31 31 29 20 2f 2a 20 50 34 20 69 73 20 61 20 73  11) /* P4 is a s
4ede0 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66  tring obtained f
4edf0 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  rom sqlite3_mpri
4ee00 6e 74 66 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65  ntf() */.#define
4ee10 20 50 34 5f 52 45 41 4c 20 20 20 20 20 28 2d 31   P4_REAL     (-1
4ee20 32 29 20 2f 2a 20 50 34 20 69 73 20 61 20 36 34  2) /* P4 is a 64
4ee30 2d 62 69 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f  -bit floating po
4ee40 69 6e 74 20 76 61 6c 75 65 20 2a 2f 0a 23 64 65  int value */.#de
4ee50 66 69 6e 65 20 50 34 5f 49 4e 54 36 34 20 20 20  fine P4_INT64   
4ee60 20 28 2d 31 33 29 20 2f 2a 20 50 34 20 69 73 20   (-13) /* P4 is 
4ee70 61 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20  a 64-bit signed 
4ee80 69 6e 74 65 67 65 72 20 2a 2f 0a 23 64 65 66 69  integer */.#defi
4ee90 6e 65 20 50 34 5f 49 4e 54 33 32 20 20 20 20 28  ne P4_INT32    (
4eea0 2d 31 34 29 20 2f 2a 20 50 34 20 69 73 20 61 20  -14) /* P4 is a 
4eeb0 33 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e  32-bit signed in
4eec0 74 65 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65  teger */.#define
4eed0 20 50 34 5f 49 4e 54 41 52 52 41 59 20 28 2d 31   P4_INTARRAY (-1
4eee0 35 29 20 2f 2a 20 50 34 20 69 73 20 61 20 76 65  5) /* P4 is a ve
4eef0 63 74 6f 72 20 6f 66 20 33 32 2d 62 69 74 20 69  ctor of 32-bit i
4ef00 6e 74 65 67 65 72 73 20 2a 2f 0a 23 64 65 66 69  ntegers */.#defi
4ef10 6e 65 20 50 34 5f 53 55 42 50 52 4f 47 52 41 4d  ne P4_SUBPROGRAM
4ef20 20 20 28 2d 31 38 29 20 2f 2a 20 50 34 20 69 73    (-18) /* P4 is
4ef30 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
4ef40 53 75 62 50 72 6f 67 72 61 6d 20 73 74 72 75 63  SubProgram struc
4ef50 74 75 72 65 20 2a 2f 0a 0a 2f 2a 20 57 68 65 6e  ture */../* When
4ef60 20 61 64 64 69 6e 67 20 61 20 50 34 20 61 72 67   adding a P4 arg
4ef70 75 6d 65 6e 74 20 75 73 69 6e 67 20 50 34 5f 4b  ument using P4_K
4ef80 45 59 49 4e 46 4f 2c 20 61 20 63 6f 70 79 20 6f  EYINFO, a copy o
4ef90 66 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74  f the KeyInfo st
4efa0 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 6d 61  ructure.** is ma
4efb0 64 65 2e 20 20 54 68 61 74 20 63 6f 70 79 20 69  de.  That copy i
4efc0 73 20 66 72 65 65 64 20 77 68 65 6e 20 74 68 65  s freed when the
4efd0 20 56 64 62 65 20 69 73 20 66 69 6e 61 6c 69 7a   Vdbe is finaliz
4efe0 65 64 2e 20 20 42 75 74 20 69 66 20 74 68 65 0a  ed.  But if the.
4eff0 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 73 20 50  ** argument is P
4f000 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46  4_KEYINFO_HANDOF
4f010 46 2c 20 74 68 65 20 70 61 73 73 65 64 20 69 6e  F, the passed in
4f020 20 70 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64   pointer is used
4f030 2e 20 20 49 74 20 73 74 69 6c 6c 0a 2a 2a 20 67  .  It still.** g
4f040 65 74 73 20 66 72 65 65 64 20 77 68 65 6e 20 74  ets freed when t
4f050 68 65 20 56 64 62 65 20 69 73 20 66 69 6e 61 6c  he Vdbe is final
4f060 69 7a 65 64 20 73 6f 20 69 74 20 73 74 69 6c 6c  ized so it still
4f070 20 73 68 6f 75 6c 64 20 62 65 20 6f 62 74 61 69   should be obtai
4f080 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 61 20 73 69  ned.** from a si
4f090 6e 67 6c 65 20 73 71 6c 69 74 65 4d 61 6c 6c 6f  ngle sqliteMallo
4f0a0 63 28 29 2e 20 20 42 75 74 20 6e 6f 20 63 6f 70  c().  But no cop
4f0b0 79 20 69 73 20 6d 61 64 65 20 61 6e 64 20 74 68  y is made and th
4f0c0 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e  e calling.** fun
4f0d0 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 2a 6e 6f  ction should *no
4f0e0 74 2a 20 74 72 79 20 74 6f 20 66 72 65 65 20 74  t* try to free t
4f0f0 68 65 20 4b 65 79 49 6e 66 6f 2e 0a 2a 2f 0a 23  he KeyInfo..*/.#
4f100 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46  define P4_KEYINF
4f110 4f 5f 48 41 4e 44 4f 46 46 20 28 2d 31 36 29 0a  O_HANDOFF (-16).
4f120 23 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e  #define P4_KEYIN
4f130 46 4f 5f 53 54 41 54 49 43 20 20 28 2d 31 37 29  FO_STATIC  (-17)
4f140 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 56 64 62 65  ../*.** The Vdbe
4f150 2e 61 43 6f 6c 4e 61 6d 65 20 61 72 72 61 79 20  .aColName array 
4f160 63 6f 6e 74 61 69 6e 73 20 35 6e 20 4d 65 6d 20  contains 5n Mem 
4f170 73 74 72 75 63 74 75 72 65 73 2c 20 77 68 65 72  structures, wher
4f180 65 20 6e 20 69 73 20 74 68 65 20 0a 2a 2a 20 6e  e n is the .** n
4f190 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
4f1a0 20 6f 66 20 64 61 74 61 20 72 65 74 75 72 6e 65   of data returne
4f1b0 64 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65  d by the stateme
4f1c0 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43  nt..*/.#define C
4f1d0 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 20 20 20 20 20  OLNAME_NAME     
4f1e0 30 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d  0.#define COLNAM
4f1f0 45 5f 44 45 43 4c 54 59 50 45 20 31 0a 23 64 65  E_DECLTYPE 1.#de
4f200 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 44 41 54  fine COLNAME_DAT
4f210 41 42 41 53 45 20 32 0a 23 64 65 66 69 6e 65 20  ABASE 2.#define 
4f220 43 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45 20 20 20  COLNAME_TABLE   
4f230 20 33 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41   3.#define COLNA
4f240 4d 45 5f 43 4f 4c 55 4d 4e 20 20 20 34 0a 23 69  ME_COLUMN   4.#i
4f250 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
4f260 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41  LE_COLUMN_METADA
4f270 54 41 0a 23 20 64 65 66 69 6e 65 20 43 4f 4c 4e  TA.# define COLN
4f280 41 4d 45 5f 4e 20 20 20 20 20 20 20 20 35 20 20  AME_N        5  
4f290 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
4f2a0 20 43 4f 4c 4e 41 4d 45 5f 78 78 78 20 73 79 6d   COLNAME_xxx sym
4f2b0 62 6f 6c 73 20 2a 2f 0a 23 65 6c 73 65 0a 23 20  bols */.#else.# 
4f2c0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
4f2d0 54 5f 44 45 43 4c 54 59 50 45 0a 23 20 20 20 64  T_DECLTYPE.#   d
4f2e0 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20  efine COLNAME_N 
4f2f0 20 20 20 20 20 31 20 20 20 20 20 20 2f 2a 20 53       1      /* S
4f300 74 6f 72 65 20 6f 6e 6c 79 20 74 68 65 20 6e 61  tore only the na
4f310 6d 65 20 2a 2f 0a 23 20 65 6c 73 65 0a 23 20 20  me */.# else.#  
4f320 20 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f   define COLNAME_
4f330 4e 20 20 20 20 20 20 32 20 20 20 20 20 20 2f 2a  N      2      /*
4f340 20 53 74 6f 72 65 20 74 68 65 20 6e 61 6d 65 20   Store the name 
4f350 61 6e 64 20 64 65 63 6c 74 79 70 65 20 2a 2f 0a  and decltype */.
4f360 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a  # endif.#endif..
4f370 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
4f380 69 6e 67 20 6d 61 63 72 6f 20 63 6f 6e 76 65 72  ing macro conver
4f390 74 73 20 61 20 72 65 6c 61 74 69 76 65 20 61 64  ts a relative ad
4f3a0 64 72 65 73 73 20 69 6e 20 74 68 65 20 70 32 20  dress in the p2 
4f3b0 66 69 65 6c 64 0a 2a 2a 20 6f 66 20 61 20 56 64  field.** of a Vd
4f3c0 62 65 4f 70 20 73 74 72 75 63 74 75 72 65 20 69  beOp structure i
4f3d0 6e 74 6f 20 61 20 6e 65 67 61 74 69 76 65 20 6e  nto a negative n
4f3e0 75 6d 62 65 72 20 73 6f 20 74 68 61 74 20 0a 2a  umber so that .*
4f3f0 2a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  * sqlite3VdbeAdd
4f400 4f 70 4c 69 73 74 28 29 20 6b 6e 6f 77 73 20 74  OpList() knows t
4f410 68 61 74 20 74 68 65 20 61 64 64 72 65 73 73 20  hat the address 
4f420 69 73 20 72 65 6c 61 74 69 76 65 2e 20 20 43 61  is relative.  Ca
4f430 6c 6c 69 6e 67 0a 2a 2a 20 74 68 65 20 6d 61 63  lling.** the mac
4f440 72 6f 20 61 67 61 69 6e 20 72 65 73 74 6f 72 65  ro again restore
4f450 73 20 74 68 65 20 61 64 64 72 65 73 73 2e 0a 2a  s the address..*
4f460 2f 0a 23 64 65 66 69 6e 65 20 41 44 44 52 28 58  /.#define ADDR(X
4f470 29 20 20 28 2d 31 2d 28 58 29 29 0a 0a 2f 2a 0a  )  (-1-(X))../*.
4f480 2a 2a 20 54 68 65 20 6d 61 6b 65 66 69 6c 65 20  ** The makefile 
4f490 73 63 61 6e 73 20 74 68 65 20 76 64 62 65 2e 63  scans the vdbe.c
4f4a0 20 73 6f 75 72 63 65 20 66 69 6c 65 20 61 6e 64   source file and
4f4b0 20 63 72 65 61 74 65 73 20 74 68 65 20 22 6f 70   creates the "op
4f4c0 63 6f 64 65 73 2e 68 22 0a 2a 2a 20 68 65 61 64  codes.h".** head
4f4d0 65 72 20 66 69 6c 65 20 74 68 61 74 20 64 65 66  er file that def
4f4e0 69 6e 65 73 20 61 20 6e 75 6d 62 65 72 20 66 6f  ines a number fo
4f4f0 72 20 65 61 63 68 20 6f 70 63 6f 64 65 20 75 73  r each opcode us
4f500 65 64 20 62 79 20 74 68 65 20 56 44 42 45 2e 0a  ed by the VDBE..
4f510 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
4f520 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 70 63 6f 64  ** Include opcod
4f530 65 73 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  es.h in the midd
4f540 6c 65 20 6f 66 20 76 64 62 65 2e 68 20 2a 2a 2a  le of vdbe.h ***
4f550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4f560 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
4f570 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 70  ** Begin file op
4f580 63 6f 64 65 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  codes.h ********
4f590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4f5a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4f5b0 2a 2f 0a 2f 2a 20 41 75 74 6f 6d 61 74 69 63 61  */./* Automatica
4f5c0 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 2e 20 20  lly generated.  
4f5d0 44 6f 20 6e 6f 74 20 65 64 69 74 20 2a 2f 0a 2f  Do not edit */./
4f5e0 2a 20 53 65 65 20 74 68 65 20 6d 6b 6f 70 63 6f  * See the mkopco
4f5f0 64 65 68 2e 61 77 6b 20 73 63 72 69 70 74 20 66  deh.awk script f
4f600 6f 72 20 64 65 74 61 69 6c 73 20 2a 2f 0a 23 64  or details */.#d
4f610 65 66 69 6e 65 20 4f 50 5f 56 4e 65 78 74 20 20  efine OP_VNext  
4f620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 0a                1.
4f640 23 64 65 66 69 6e 65 20 4f 50 5f 41 66 66 69 6e  #define OP_Affin
4f650 69 74 79 20 20 20 20 20 20 20 20 20 20 20 20 20  ity             
4f660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f670 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6c  2.#define OP_Col
4f680 75 6d 6e 20 20 20 20 20 20 20 20 20 20 20 20 20  umn             
4f690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f6a0 20 20 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53    3.#define OP_S
4f6b0 65 74 43 6f 6f 6b 69 65 20 20 20 20 20 20 20 20  etCookie        
4f6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f6d0 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 4f 50      4.#define OP
4f6e0 5f 53 65 65 6b 20 20 20 20 20 20 20 20 20 20 20  _Seek           
4f6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f700 20 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20        5.#define 
4f710 4f 50 5f 52 65 61 6c 20 20 20 20 20 20 20 20 20  OP_Real         
4f720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f730 20 20 20 20 20 20 31 33 30 20 20 20 2f 2a 20 73        130   /* s
4f740 61 6d 65 20 61 73 20 54 4b 5f 46 4c 4f 41 54 20  ame as TK_FLOAT 
4f750 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50     */.#define OP
4f760 5f 53 65 71 75 65 6e 63 65 20 20 20 20 20 20 20  _Sequence       
4f770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f780 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20        6.#define 
4f790 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20 20 20 20  OP_Savepoint    
4f7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f7b0 20 20 20 20 20 20 20 20 37 0a 23 64 65 66 69 6e          7.#defin
4f7c0 65 20 4f 50 5f 47 65 20 20 20 20 20 20 20 20 20  e OP_Ge         
4f7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f7e0 20 20 20 20 20 20 20 20 20 37 38 20 20 20 2f 2a           78   /*
4f7f0 20 73 61 6d 65 20 61 73 20 54 4b 5f 47 45 20 20   same as TK_GE  
4f800 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
4f810 4f 50 5f 52 6f 77 4b 65 79 20 20 20 20 20 20 20  OP_RowKey       
4f820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f830 20 20 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e          8.#defin
4f840 65 20 4f 50 5f 53 43 6f 70 79 20 20 20 20 20 20  e OP_SCopy      
4f850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f860 20 20 20 20 20 20 20 20 20 20 39 0a 23 64 65 66            9.#def
4f870 69 6e 65 20 4f 50 5f 45 71 20 20 20 20 20 20 20  ine OP_Eq       
4f880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f890 20 20 20 20 20 20 20 20 20 20 20 37 34 20 20 20             74   
4f8a0 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 45 51  /* same as TK_EQ
4f8b0 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e         */.#defin
4f8c0 65 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 20  e OP_OpenWrite  
4f8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f8e0 20 20 20 20 20 20 20 20 20 31 30 0a 23 64 65 66           10.#def
4f8f0 69 6e 65 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 20  ine OP_NotNull  
4f900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f910 20 20 20 20 20 20 20 20 20 20 20 37 32 20 20 20             72   
4f920 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f  /* same as TK_NO
4f930 54 4e 55 4c 4c 20 20 2a 2f 0a 23 64 65 66 69 6e  TNULL  */.#defin
4f940 65 20 4f 50 5f 49 66 20 20 20 20 20 20 20 20 20  e OP_If         
4f950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f960 20 20 20 20 20 20 20 20 20 31 31 0a 23 64 65 66           11.#def
4f970 69 6e 65 20 4f 50 5f 54 6f 49 6e 74 20 20 20 20  ine OP_ToInt    
4f980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f990 20 20 20 20 20 20 20 20 20 20 31 34 34 20 20 20            144   
4f9a0 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f  /* same as TK_TO
4f9b0 5f 49 4e 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e  _INT   */.#defin
4f9c0 65 20 4f 50 5f 53 74 72 69 6e 67 38 20 20 20 20  e OP_String8    
4f9d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f9e0 20 20 20 20 20 20 20 20 20 39 34 20 20 20 2f 2a           94   /*
4f9f0 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 54 52 49   same as TK_STRI
4fa00 4e 47 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20  NG   */.#define 
4fa10 4f 50 5f 43 6f 6c 6c 53 65 71 20 20 20 20 20 20  OP_CollSeq      
4fa20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fa30 20 20 20 20 20 20 20 31 32 0a 23 64 65 66 69 6e         12.#defin
4fa40 65 20 4f 50 5f 4f 70 65 6e 52 65 61 64 20 20 20  e OP_OpenRead   
4fa50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fa60 20 20 20 20 20 20 20 20 20 31 33 0a 23 64 65 66           13.#def
4fa70 69 6e 65 20 4f 50 5f 45 78 70 69 72 65 20 20 20  ine OP_Expire   
4fa80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fa90 20 20 20 20 20 20 20 20 20 20 20 31 34 0a 23 64             14.#d
4faa0 65 66 69 6e 65 20 4f 50 5f 41 75 74 6f 43 6f 6d  efine OP_AutoCom
4fab0 6d 69 74 20 20 20 20 20 20 20 20 20 20 20 20 20  mit             
4fac0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35 0a               15.
4fad0 23 64 65 66 69 6e 65 20 4f 50 5f 47 74 20 20 20  #define OP_Gt   
4fae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4faf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37                 7
4fb00 35 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  5   /* same as T
4fb10 4b 5f 47 54 20 20 20 20 20 20 20 2a 2f 0a 23 64  K_GT       */.#d
4fb20 65 66 69 6e 65 20 4f 50 5f 50 61 67 65 63 6f 75  efine OP_Pagecou
4fb30 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  nt              
4fb40 20 20 20 20 20 20 20 20 20 20 20 20 20 31 36 0a               16.
4fb50 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 74 65 67  #define OP_Integ
4fb60 72 69 74 79 43 6b 20 20 20 20 20 20 20 20 20 20  rityCk          
4fb70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
4fb80 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 6f 72  7.#define OP_Sor
4fb90 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
4fba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fbb0 20 31 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43   18.#define OP_C
4fbc0 6f 70 79 20 20 20 20 20 20 20 20 20 20 20 20 20  opy             
4fbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fbe0 20 20 20 32 30 0a 23 64 65 66 69 6e 65 20 4f 50     20.#define OP
4fbf0 5f 54 72 61 63 65 20 20 20 20 20 20 20 20 20 20  _Trace          
4fc00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fc10 20 20 20 20 20 32 31 0a 23 64 65 66 69 6e 65 20       21.#define 
4fc20 4f 50 5f 46 75 6e 63 74 69 6f 6e 20 20 20 20 20  OP_Function     
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 32 32 0a 23 64 65 66 69 6e         22.#defin
4fc50 65 20 4f 50 5f 49 66 4e 65 67 20 20 20 20 20 20  e OP_IfNeg      
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 32 33 0a 23 64 65 66           23.#def
4fc80 69 6e 65 20 4f 50 5f 41 6e 64 20 20 20 20 20 20  ine OP_And      
4fc90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fca0 20 20 20 20 20 20 20 20 20 20 20 36 37 20 20 20             67   
4fcb0 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 41 4e  /* same as TK_AN
4fcc0 44 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  D      */.#defin
4fcd0 65 20 4f 50 5f 53 75 62 74 72 61 63 74 20 20 20  e OP_Subtract   
4fce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fcf0 20 20 20 20 20 20 20 20 20 38 35 20 20 20 2f 2a           85   /*
4fd00 20 73 61 6d 65 20 61 73 20 54 4b 5f 4d 49 4e 55   same as TK_MINU
4fd10 53 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20  S    */.#define 
4fd20 4f 50 5f 4e 6f 6f 70 20 20 20 20 20 20 20 20 20  OP_Noop         
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 32 34 0a 23 64 65 66 69 6e         24.#defin
4fd50 65 20 4f 50 5f 50 72 6f 67 72 61 6d 20 20 20 20  e OP_Program    
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 32 35 0a 23 64 65 66           25.#def
4fd80 69 6e 65 20 4f 50 5f 52 65 74 75 72 6e 20 20 20  ine OP_Return   
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 32 36 0a 23 64             26.#d
4fdb0 65 66 69 6e 65 20 4f 50 5f 52 65 6d 61 69 6e 64  efine OP_Remaind
4fdc0 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20  er              
4fdd0 20 20 20 20 20 20 20 20 20 20 20 20 20 38 38 20               88 
4fde0 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
4fdf0 52 45 4d 20 20 20 20 20 20 2a 2f 0a 23 64 65 66  REM      */.#def
4fe00 69 6e 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20  ine OP_NewRowid 
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 32 37 0a 23 64             27.#d
4fe30 65 66 69 6e 65 20 4f 50 5f 4d 75 6c 74 69 70 6c  efine OP_Multipl
4fe40 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y               
4fe50 20 20 20 20 20 20 20 20 20 20 20 20 20 38 36 20               86 
4fe60 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
4fe70 53 54 41 52 20 20 20 20 20 2a 2f 0a 23 64 65 66  STAR     */.#def
4fe80 69 6e 65 20 4f 50 5f 56 61 72 69 61 62 6c 65 20  ine OP_Variable 
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 32 38 0a 23 64             28.#d
4feb0 65 66 69 6e 65 20 4f 50 5f 53 74 72 69 6e 67 20  efine OP_String 
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 32 39 0a               29.
4fee0 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 61 6c 41  #define OP_RealA
4fef0 66 66 69 6e 69 74 79 20 20 20 20 20 20 20 20 20  ffinity         
4ff00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33                 3
4ff10 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 52 65  0.#define OP_VRe
4ff20 6e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20  name            
4ff30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ff40 20 33 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50   31.#define OP_P
4ff50 61 72 73 65 53 63 68 65 6d 61 20 20 20 20 20 20  arseSchema      
4ff60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ff70 20 20 20 33 32 0a 23 64 65 66 69 6e 65 20 4f 50     32.#define OP
4ff80 5f 56 4f 70 65 6e 20 20 20 20 20 20 20 20 20 20  _VOpen          
4ff90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ffa0 20 20 20 20 20 33 33 0a 23 64 65 66 69 6e 65 20       33.#define 
4ffb0 4f 50 5f 43 6c 6f 73 65 20 20 20 20 20 20 20 20  OP_Close        
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 33 34 0a 23 64 65 66 69 6e         34.#defin
4ffe0 65 20 4f 50 5f 43 72 65 61 74 65 49 6e 64 65 78  e OP_CreateIndex
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 33 35 0a 23 64 65 66           35.#def
50010 69 6e 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 20  ine OP_IsUnique 
50020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50030 20 20 20 20 20 20 20 20 20 20 20 33 36 0a 23 64             36.#d
50040 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 46 6f 75 6e  efine OP_NotFoun
50050 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d               
50060 20 20 20 20 20 20 20 20 20 20 20 20 20 33 37 0a               37.
50070 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 74 36 34  #define OP_Int64
50080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33                 3
500a0 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 75 73  8.#define OP_Mus
500b0 74 42 65 49 6e 74 20 20 20 20 20 20 20 20 20 20  tBeInt          
500c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
500d0 20 33 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 48   39.#define OP_H
500e0 61 6c 74 20 20 20 20 20 20 20 20 20 20 20 20 20  alt             
500f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50100 20 20 20 34 30 0a 23 64 65 66 69 6e 65 20 4f 50     40.#define OP
50110 5f 52 6f 77 69 64 20 20 20 20 20 20 20 20 20 20  _Rowid          
50120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50130 20 20 20 20 20 34 31 0a 23 64 65 66 69 6e 65 20       41.#define 
50140 4f 50 5f 49 64 78 4c 54 20 20 20 20 20 20 20 20  OP_IdxLT        
50150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50160 20 20 20 20 20 20 20 34 32 0a 23 64 65 66 69 6e         42.#defin
50170 65 20 4f 50 5f 41 64 64 49 6d 6d 20 20 20 20 20  e OP_AddImm     
50180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50190 20 20 20 20 20 20 20 20 20 34 33 0a 23 64 65 66           43.#def
501a0 69 6e 65 20 4f 50 5f 52 6f 77 44 61 74 61 20 20  ine OP_RowData  
501b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
501c0 20 20 20 20 20 20 20 20 20 20 20 34 34 0a 23 64             44.#d
501d0 65 66 69 6e 65 20 4f 50 5f 4d 65 6d 4d 61 78 20  efine OP_MemMax 
501e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
501f0 20 20 20 20 20 20 20 20 20 20 20 20 20 34 35 0a               45.
50200 23 64 65 66 69 6e 65 20 4f 50 5f 4f 72 20 20 20  #define OP_Or   
50210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36                 6
50230 36 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  6   /* same as T
50240 4b 5f 4f 52 20 20 20 20 20 20 20 2a 2f 0a 23 64  K_OR       */.#d
50250 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 45 78 69 73  efine OP_NotExis
50260 74 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ts              
50270 20 20 20 20 20 20 20 20 20 20 20 20 20 34 36 0a               46.
50280 23 64 65 66 69 6e 65 20 4f 50 5f 47 6f 73 75 62  #define OP_Gosub
50290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
502a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34                 4
502b0 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 69 76  7.#define OP_Div
502c0 69 64 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ide             
502d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
502e0 20 38 37 20 20 20 2f 2a 20 73 61 6d 65 20 61 73   87   /* same as
502f0 20 54 4b 5f 53 4c 41 53 48 20 20 20 20 2a 2f 0a   TK_SLASH    */.
50300 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 74 65 67  #define OP_Integ
50310 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20  er              
50320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34                 4
50330 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f 4e  8.#define OP_ToN
50340 75 6d 65 72 69 63 20 20 20 20 20 20 20 20 20 20  umeric          
50350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50360 31 34 33 20 20 20 2f 2a 20 73 61 6d 65 20 61 73  143   /* same as
50370 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 2a 2f   TK_TO_NUMERIC*/
50380 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 72 65 76  .#define OP_Prev
50390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
503a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
503b0 34 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f  49.#define OP_Ro
503c0 77 53 65 74 52 65 61 64 20 20 20 20 20 20 20 20  wSetRead        
503d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
503e0 20 20 35 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f    50.#define OP_
503f0 43 6f 6e 63 61 74 20 20 20 20 20 20 20 20 20 20  Concat          
50400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50410 20 20 20 20 38 39 20 20 20 2f 2a 20 73 61 6d 65      89   /* same
50420 20 61 73 20 54 4b 5f 43 4f 4e 43 41 54 20 20 20   as TK_CONCAT   
50430 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f  */.#define OP_Ro
50440 77 53 65 74 41 64 64 20 20 20 20 20 20 20 20 20  wSetAdd         
50450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50460 20 20 35 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f    51.#define OP_
50470 42 69 74 41 6e 64 20 20 20 20 20 20 20 20 20 20  BitAnd          
50480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50490 20 20 20 20 38 30 20 20 20 2f 2a 20 73 61 6d 65      80   /* same
504a0 20 61 73 20 54 4b 5f 42 49 54 41 4e 44 20 20 20   as TK_BITAND   
504b0 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 43  */.#define OP_VC
504c0 6f 6c 75 6d 6e 20 20 20 20 20 20 20 20 20 20 20  olumn           
504d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
504e0 20 20 35 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f    52.#define OP_
504f0 43 72 65 61 74 65 54 61 62 6c 65 20 20 20 20 20  CreateTable     
50500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50510 20 20 20 20 35 33 0a 23 64 65 66 69 6e 65 20 4f      53.#define O
50520 50 5f 4c 61 73 74 20 20 20 20 20 20 20 20 20 20  P_Last          
50530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50540 20 20 20 20 20 20 35 34 0a 23 64 65 66 69 6e 65        54.#define
50550 20 4f 50 5f 53 65 65 6b 4c 65 20 20 20 20 20 20   OP_SeekLe      
50560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50570 20 20 20 20 20 20 20 20 35 35 0a 23 64 65 66 69          55.#defi
50580 6e 65 20 4f 50 5f 49 73 4e 75 6c 6c 20 20 20 20  ne OP_IsNull    
50590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
505a0 20 20 20 20 20 20 20 20 20 20 37 31 20 20 20 2f            71   /
505b0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 49 53 4e  * same as TK_ISN
505c0 55 4c 4c 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65  ULL   */.#define
505d0 20 4f 50 5f 49 6e 63 72 56 61 63 75 75 6d 20 20   OP_IncrVacuum  
505e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
505f0 20 20 20 20 20 20 20 20 35 36 0a 23 64 65 66 69          56.#defi
50600 6e 65 20 4f 50 5f 49 64 78 52 6f 77 69 64 20 20  ne OP_IdxRowid  
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 35 37 0a 23 64 65            57.#de
50630 66 69 6e 65 20 4f 50 5f 53 68 69 66 74 52 69 67  fine OP_ShiftRig
50640 68 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ht              
50650 20 20 20 20 20 20 20 20 20 20 20 20 38 33 20 20              83  
50660 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52   /* same as TK_R
50670 53 48 49 46 54 20 20 20 2a 2f 0a 23 64 65 66 69  SHIFT   */.#defi
50680 6e 65 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74  ne OP_ResetCount
50690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
506a0 20 20 20 20 20 20 20 20 20 20 35 38 0a 23 64 65            58.#de
506b0 66 69 6e 65 20 4f 50 5f 59 69 65 6c 64 20 20 20  fine OP_Yield   
506c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
506d0 20 20 20 20 20 20 20 20 20 20 20 20 35 39 0a 23              59.#
506e0 64 65 66 69 6e 65 20 4f 50 5f 44 72 6f 70 54 72  define OP_DropTr
506f0 69 67 67 65 72 20 20 20 20 20 20 20 20 20 20 20  igger           
50700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 30                60
50710 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 72 6f 70  .#define OP_Drop
50720 49 6e 64 65 78 20 20 20 20 20 20 20 20 20 20 20  Index           
50730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50740 36 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 61  61.#define OP_Pa
50750 72 61 6d 20 20 20 20 20 20 20 20 20 20 20 20 20  ram             
50760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50770 20 20 36 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f    62.#define OP_
50780 49 64 78 47 45 20 20 20 20 20 20 20 20 20 20 20  IdxGE           
50790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
507a0 20 20 20 20 36 33 0a 23 64 65 66 69 6e 65 20 4f      63.#define O
507b0 50 5f 49 64 78 44 65 6c 65 74 65 20 20 20 20 20  P_IdxDelete     
507c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
507d0 20 20 20 20 20 20 36 34 0a 23 64 65 66 69 6e 65        64.#define
507e0 20 4f 50 5f 56 61 63 75 75 6d 20 20 20 20 20 20   OP_Vacuum      
507f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50800 20 20 20 20 20 20 20 20 36 35 0a 23 64 65 66 69          65.#defi
50810 6e 65 20 4f 50 5f 49 66 4e 6f 74 20 20 20 20 20  ne OP_IfNot     
50820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50830 20 20 20 20 20 20 20 20 20 20 36 38 0a 23 64 65            68.#de
50840 66 69 6e 65 20 4f 50 5f 44 72 6f 70 54 61 62 6c  fine OP_DropTabl
50850 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
50860 20 20 20 20 20 20 20 20 20 20 20 20 36 39 0a 23              69.#
50870 64 65 66 69 6e 65 20 4f 50 5f 53 65 65 6b 4c 74  define OP_SeekLt
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 20 20 37 30                70
508a0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 61 6b 65  .#define OP_Make
508b0 52 65 63 6f 72 64 20 20 20 20 20 20 20 20 20 20  Record          
508c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
508d0 37 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f  79.#define OP_To
508e0 42 6c 6f 62 20 20 20 20 20 20 20 20 20 20 20 20  Blob            
508f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50900 20 31 34 32 20 20 20 2f 2a 20 73 61 6d 65 20 61   142   /* same a
50910 73 20 54 4b 5f 54 4f 5f 42 4c 4f 42 20 20 2a 2f  s TK_TO_BLOB  */
50920 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 73 75  .#define OP_Resu
50930 6c 74 52 6f 77 20 20 20 20 20 20 20 20 20 20 20  ltRow           
50940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50950 39 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 65  90.#define OP_De
50960 6c 65 74 65 20 20 20 20 20 20 20 20 20 20 20 20  lete            
50970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50980 20 20 39 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f    91.#define OP_
50990 41 67 67 46 69 6e 61 6c 20 20 20 20 20 20 20 20  AggFinal        
509a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
509b0 20 20 20 20 39 32 0a 23 64 65 66 69 6e 65 20 4f      92.#define O
509c0 50 5f 43 6f 6d 70 61 72 65 20 20 20 20 20 20 20  P_Compare       
509d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
509e0 20 20 20 20 20 20 39 35 0a 23 64 65 66 69 6e 65        95.#define
509f0 20 4f 50 5f 53 68 69 66 74 4c 65 66 74 20 20 20   OP_ShiftLeft   
50a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50a10 20 20 20 20 20 20 20 20 38 32 20 20 20 2f 2a 20          82   /* 
50a20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 53 48 49 46  same as TK_LSHIF
50a30 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  T   */.#define O
50a40 50 5f 47 6f 74 6f 20 20 20 20 20 20 20 20 20 20  P_Goto          
50a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50a60 20 20 20 20 20 20 39 36 0a 23 64 65 66 69 6e 65        96.#define
50a70 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 20 20 20   OP_TableLock   
50a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50a90 20 20 20 20 20 20 20 20 39 37 0a 23 64 65 66 69          97.#defi
50aa0 6e 65 20 4f 50 5f 43 6c 65 61 72 20 20 20 20 20  ne OP_Clear     
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 39 38 0a 23 64 65            98.#de
50ad0 66 69 6e 65 20 4f 50 5f 4c 65 20 20 20 20 20 20  fine OP_Le      
50ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50af0 20 20 20 20 20 20 20 20 20 20 20 20 37 36 20 20              76  
50b00 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c   /* same as TK_L
50b10 45 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69  E       */.#defi
50b20 6e 65 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b  ne OP_VerifyCook
50b30 69 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ie              
50b40 20 20 20 20 20 20 20 20 20 20 39 39 0a 23 64 65            99.#de
50b50 66 69 6e 65 20 4f 50 5f 41 67 67 53 74 65 70 20  fine OP_AggStep 
50b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50b70 20 20 20 20 20 20 20 20 20 20 20 31 30 30 0a 23             100.#
50b80 64 65 66 69 6e 65 20 4f 50 5f 54 6f 54 65 78 74  define OP_ToText
50b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 31               141
50bb0 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
50bc0 5f 54 4f 5f 54 45 58 54 20 20 2a 2f 0a 23 64 65  _TO_TEXT  */.#de
50bd0 66 69 6e 65 20 4f 50 5f 4e 6f 74 20 20 20 20 20  fine OP_Not     
50be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50bf0 20 20 20 20 20 20 20 20 20 20 20 20 31 39 20 20              19  
50c00 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e   /* same as TK_N
50c10 4f 54 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69  OT      */.#defi
50c20 6e 65 20 4f 50 5f 54 6f 52 65 61 6c 20 20 20 20  ne OP_ToReal    
50c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50c40 20 20 20 20 20 20 20 20 20 31 34 35 20 20 20 2f           145   /
50c50 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f  * same as TK_TO_
50c60 52 45 41 4c 20 20 2a 2f 0a 23 64 65 66 69 6e 65  REAL  */.#define
50c70 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 20   OP_Transaction 
50c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50c90 20 20 20 20 20 20 20 31 30 31 0a 23 64 65 66 69         101.#defi
50ca0 6e 65 20 4f 50 5f 56 46 69 6c 74 65 72 20 20 20  ne OP_VFilter   
50cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50cc0 20 20 20 20 20 20 20 20 20 31 30 32 0a 23 64 65           102.#de
50cd0 66 69 6e 65 20 4f 50 5f 4e 65 20 20 20 20 20 20  fine OP_Ne      
50ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50cf0 20 20 20 20 20 20 20 20 20 20 20 20 37 33 20 20              73  
50d00 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e   /* same as TK_N
50d10 45 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69  E       */.#defi
50d20 6e 65 20 4f 50 5f 56 44 65 73 74 72 6f 79 20 20  ne OP_VDestroy  
50d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50d40 20 20 20 20 20 20 20 20 20 31 30 33 0a 23 64 65           103.#de
50d50 66 69 6e 65 20 4f 50 5f 42 69 74 4f 72 20 20 20  fine OP_BitOr   
50d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50d70 20 20 20 20 20 20 20 20 20 20 20 20 38 31 20 20              81  
50d80 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42   /* same as TK_B
50d90 49 54 4f 52 20 20 20 20 2a 2f 0a 23 64 65 66 69  ITOR    */.#defi
50da0 6e 65 20 4f 50 5f 4e 65 78 74 20 20 20 20 20 20  ne OP_Next      
50db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50dc0 20 20 20 20 20 20 20 20 20 31 30 34 0a 23 64 65           104.#de
50dd0 66 69 6e 65 20 4f 50 5f 43 6f 75 6e 74 20 20 20  fine OP_Count   
50de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50df0 20 20 20 20 20 20 20 20 20 20 20 31 30 35 0a 23             105.#
50e00 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 49 6e 73  define OP_IdxIns
50e10 65 72 74 20 20 20 20 20 20 20 20 20 20 20 20 20  ert             
50e20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 36               106
50e30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4c 74 20 20  .#define OP_Lt  
50e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50e60 37 37 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20  77   /* same as 
50e70 54 4b 5f 4c 54 20 20 20 20 20 20 20 2a 2f 0a 23  TK_LT       */.#
50e80 64 65 66 69 6e 65 20 4f 50 5f 53 65 65 6b 47 65  define OP_SeekGe
50e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 37               107
50eb0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 73 65  .#define OP_Inse
50ec0 72 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  rt              
50ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
50ee0 30 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 65  08.#define OP_De
50ef0 73 74 72 6f 79 20 20 20 20 20 20 20 20 20 20 20  stroy           
50f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50f10 20 31 30 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f   109.#define OP_
50f20 52 65 61 64 43 6f 6f 6b 69 65 20 20 20 20 20 20  ReadCookie      
50f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50f40 20 20 20 31 31 30 0a 23 64 65 66 69 6e 65 20 4f     110.#define O
50f50 50 5f 52 6f 77 53 65 74 54 65 73 74 20 20 20 20  P_RowSetTest    
50f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50f70 20 20 20 20 20 31 31 31 0a 23 64 65 66 69 6e 65       111.#define
50f80 20 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 73   OP_LoadAnalysis
50f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50fa0 20 20 20 20 20 20 20 31 31 32 0a 23 64 65 66 69         112.#defi
50fb0 6e 65 20 4f 50 5f 45 78 70 6c 61 69 6e 20 20 20  ne OP_Explain   
50fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50fd0 20 20 20 20 20 20 20 20 20 31 31 33 0a 23 64 65           113.#de
50fe0 66 69 6e 65 20 4f 50 5f 48 61 6c 74 49 66 4e 75  fine OP_HaltIfNu
50ff0 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ll              
51000 20 20 20 20 20 20 20 20 20 20 20 31 31 34 0a 23             114.#
51010 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 50 73  define OP_OpenPs
51020 65 75 64 6f 20 20 20 20 20 20 20 20 20 20 20 20  eudo            
51030 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 35               115
51040 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65 6e  .#define OP_Open
51050 45 70 68 65 6d 65 72 61 6c 20 20 20 20 20 20 20  Ephemeral       
51060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
51070 31 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 75  16.#define OP_Nu
51080 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ll              
51090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
510a0 20 31 31 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f   117.#define OP_
510b0 4d 6f 76 65 20 20 20 20 20 20 20 20 20 20 20 20  Move            
510c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
510d0 20 20 20 31 31 38 0a 23 64 65 66 69 6e 65 20 4f     118.#define O
510e0 50 5f 42 6c 6f 62 20 20 20 20 20 20 20 20 20 20  P_Blob          
510f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51100 20 20 20 20 20 31 31 39 0a 23 64 65 66 69 6e 65       119.#define
51110 20 4f 50 5f 41 64 64 20 20 20 20 20 20 20 20 20   OP_Add         
51120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51130 20 20 20 20 20 20 20 20 38 34 20 20 20 2f 2a 20          84   /* 
51140 73 61 6d 65 20 61 73 20 54 4b 5f 50 4c 55 53 20  same as TK_PLUS 
51150 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f      */.#define O
51160 50 5f 52 65 77 69 6e 64 20 20 20 20 20 20 20 20  P_Rewind        
51170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51180 20 20 20 20 20 31 32 30 0a 23 64 65 66 69 6e 65       120.#define
51190 20 4f 50 5f 53 65 65 6b 47 74 20 20 20 20 20 20   OP_SeekGt      
511a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
511b0 20 20 20 20 20 20 20 31 32 31 0a 23 64 65 66 69         121.#defi
511c0 6e 65 20 4f 50 5f 56 42 65 67 69 6e 20 20 20 20  ne OP_VBegin    
511d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
511e0 20 20 20 20 20 20 20 20 20 31 32 32 0a 23 64 65           122.#de
511f0 66 69 6e 65 20 4f 50 5f 56 55 70 64 61 74 65 20  fine OP_VUpdate 
51200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51210 20 20 20 20 20 20 20 20 20 20 20 31 32 33 0a 23             123.#
51220 64 65 66 69 6e 65 20 4f 50 5f 49 66 5a 65 72 6f  define OP_IfZero
51230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51240 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 34               124
51250 0a 23 64 65 66 69 6e 65 20 4f 50 5f 42 69 74 4e  .#define OP_BitN
51260 6f 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ot              
51270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51280 39 33 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20  93   /* same as 
51290 54 4b 5f 42 49 54 4e 4f 54 20 20 20 2a 2f 0a 23  TK_BITNOT   */.#
512a0 64 65 66 69 6e 65 20 4f 50 5f 56 43 72 65 61 74  define OP_VCreat
512b0 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
512c0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 35               125
512d0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 6f 75 6e  .#define OP_Foun
512e0 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d               
512f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
51300 32 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66  26.#define OP_If
51310 50 6f 73 20 20 20 20 20 20 20 20 20 20 20 20 20  Pos             
51320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51330 20 31 32 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f   127.#define OP_
51340 4e 75 6c 6c 52 6f 77 20 20 20 20 20 20 20 20 20  NullRow         
51350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51360 20 20 20 31 32 38 0a 23 64 65 66 69 6e 65 20 4f     128.#define O
51370 50 5f 4a 75 6d 70 20 20 20 20 20 20 20 20 20 20  P_Jump          
51380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51390 20 20 20 20 20 31 32 39 0a 23 64 65 66 69 6e 65       129.#define
513a0 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e 20   OP_Permutation 
513b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
513c0 20 20 20 20 20 20 20 31 33 31 0a 0a 2f 2a 20 54         131../* T
513d0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 70 63  he following opc
513e0 6f 64 65 20 76 61 6c 75 65 73 20 61 72 65 20 6e  ode values are n
513f0 65 76 65 72 20 75 73 65 64 20 2a 2f 0a 23 64 65  ever used */.#de
51400 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f  fine OP_NotUsed_
51410 31 33 32 20 20 20 20 20 20 20 20 20 20 20 20 20  132             
51420 20 20 20 20 20 20 20 20 20 20 20 31 33 32 0a 23             132.#
51430 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65  define OP_NotUse
51440 64 5f 31 33 33 20 20 20 20 20 20 20 20 20 20 20  d_133           
51450 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 33               133
51460 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55  .#define OP_NotU
51470 73 65 64 5f 31 33 34 20 20 20 20 20 20 20 20 20  sed_134         
51480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
51490 33 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f  34.#define OP_No
514a0 74 55 73 65 64 5f 31 33 35 20 20 20 20 20 20 20  tUsed_135       
514b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
514c0 20 31 33 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f   135.#define OP_
514d0 4e 6f 74 55 73 65 64 5f 31 33 36 20 20 20 20 20  NotUsed_136     
514e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
514f0 20 20 20 31 33 36 0a 23 64 65 66 69 6e 65 20 4f     136.#define O
51500 50 5f 4e 6f 74 55 73 65 64 5f 31 33 37 20 20 20  P_NotUsed_137   
51510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51520 20 20 20 20 20 31 33 37 0a 23 64 65 66 69 6e 65       137.#define
51530 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 38 20   OP_NotUsed_138 
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 31 33 38 0a 23 64 65 66 69         138.#defi
51560 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33  ne OP_NotUsed_13
51570 39 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  9               
51580 20 20 20 20 20 20 20 20 20 31 33 39 0a 23 64 65           139.#de
51590 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f  fine OP_NotUsed_
515a0 31 34 30 20 20 20 20 20 20 20 20 20 20 20 20 20  140             
515b0 20 20 20 20 20 20 20 20 20 20 20 31 34 30 0a 0a             140..
515c0 0a 2f 2a 20 50 72 6f 70 65 72 74 69 65 73 20 73  ./* Properties s
515d0 75 63 68 20 61 73 20 22 6f 75 74 32 22 20 6f 72  uch as "out2" or
515e0 20 22 6a 75 6d 70 22 20 74 68 61 74 20 61 72 65   "jump" that are
515f0 20 73 70 65 63 69 66 69 65 64 20 69 6e 0a 2a 2a   specified in.**
51600 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 6c 6c 6f 77   comments follow
51610 69 6e 67 20 74 68 65 20 22 63 61 73 65 22 20 66  ing the "case" f
51620 6f 72 20 65 61 63 68 20 6f 70 63 6f 64 65 20 69  or each opcode i
51630 6e 20 74 68 65 20 76 64 62 65 2e 63 0a 2a 2a 20  n the vdbe.c.** 
51640 61 72 65 20 65 6e 63 6f 64 65 64 20 69 6e 74 6f  are encoded into
51650 20 62 69 74 76 65 63 74 6f 72 73 20 61 73 20 66   bitvectors as f
51660 6f 6c 6c 6f 77 73 3a 0a 2a 2f 0a 23 64 65 66 69  ollows:.*/.#defi
51670 6e 65 20 4f 50 46 4c 47 5f 4a 55 4d 50 20 20 20  ne OPFLG_JUMP   
51680 20 20 20 20 20 20 20 20 20 30 78 30 30 30 31 20           0x0001 
51690 20 2f 2a 20 6a 75 6d 70 3a 20 20 50 32 20 68 6f   /* jump:  P2 ho
516a0 6c 64 73 20 6a 6d 70 20 74 61 72 67 65 74 20 2a  lds jmp target *
516b0 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f  /.#define OPFLG_
516c0 4f 55 54 32 5f 50 52 45 52 45 4c 45 41 53 45 20  OUT2_PRERELEASE 
516d0 30 78 30 30 30 32 20 20 2f 2a 20 6f 75 74 32 2d  0x0002  /* out2-
516e0 70 72 65 72 65 6c 65 61 73 65 3a 20 2a 2f 0a 23  prerelease: */.#
516f0 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 31  define OPFLG_IN1
51700 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30               0x0
51710 30 30 34 20 20 2f 2a 20 69 6e 31 3a 20 20 20 50  004  /* in1:   P
51720 31 20 69 73 20 61 6e 20 69 6e 70 75 74 20 2a 2f  1 is an input */
51730 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49  .#define OPFLG_I
51740 4e 32 20 20 20 20 20 20 20 20 20 20 20 20 20 30  N2             0
51750 78 30 30 30 38 20 20 2f 2a 20 69 6e 32 3a 20 20  x0008  /* in2:  
51760 20 50 32 20 69 73 20 61 6e 20 69 6e 70 75 74 20   P2 is an input 
51770 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47  */.#define OPFLG
51780 5f 49 4e 33 20 20 20 20 20 20 20 20 20 20 20 20  _IN3            
51790 20 30 78 30 30 31 30 20 20 2f 2a 20 69 6e 33 3a   0x0010  /* in3:
517a0 20 20 20 50 33 20 69 73 20 61 6e 20 69 6e 70 75     P3 is an inpu
517b0 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46  t */.#define OPF
517c0 4c 47 5f 4f 55 54 33 20 20 20 20 20 20 20 20 20  LG_OUT3         
517d0 20 20 20 30 78 30 30 32 30 20 20 2f 2a 20 6f 75     0x0020  /* ou
517e0 74 33 3a 20 20 50 33 20 69 73 20 61 6e 20 6f 75  t3:  P3 is an ou
517f0 74 70 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20  tput */.#define 
51800 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45  OPFLG_INITIALIZE
51810 52 20 7b 5c 0a 2f 2a 20 20 20 30 20 2a 2f 20 30  R {\./*   0 */ 0
51820 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 30 2c  x00, 0x01, 0x00,
51830 20 30 78 30 30 2c 20 30 78 31 30 2c 20 30 78 30   0x00, 0x10, 0x0
51840 38 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 5c 0a  8, 0x02, 0x00,\.
51850 2f 2a 20 20 20 38 20 2a 2f 20 30 78 30 30 2c 20  /*   8 */ 0x00, 
51860 30 78 30 34 2c 20 30 78 30 30 2c 20 30 78 30 35  0x04, 0x00, 0x05
51870 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
51880 30 30 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 31  00, 0x00,\./*  1
51890 36 20 2a 2f 20 30 78 30 32 2c 20 30 78 30 30 2c  6 */ 0x02, 0x00,
518a0 20 30 78 30 31 2c 20 30 78 30 34 2c 20 30 78 30   0x01, 0x04, 0x0
518b0 34 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  4, 0x00, 0x00, 0
518c0 78 30 35 2c 5c 0a 2f 2a 20 20 32 34 20 2a 2f 20  x05,\./*  24 */ 
518d0 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 34  0x00, 0x01, 0x04
518e0 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78  , 0x02, 0x00, 0x
518f0 30 32 2c 20 30 78 30 34 2c 20 30 78 30 30 2c 5c  02, 0x04, 0x00,\
51900 0a 2f 2a 20 20 33 32 20 2a 2f 20 30 78 30 30 2c  ./*  32 */ 0x00,
51910 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
51920 32 2c 20 30 78 31 31 2c 20 30 78 31 31 2c 20 30  2, 0x11, 0x11, 0
51930 78 30 32 2c 20 30 78 30 35 2c 5c 0a 2f 2a 20 20  x02, 0x05,\./*  
51940 34 30 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 32  40 */ 0x00, 0x02
51950 2c 20 30 78 31 31 2c 20 30 78 30 34 2c 20 30 78  , 0x11, 0x04, 0x
51960 30 30 2c 20 30 78 30 38 2c 20 30 78 31 31 2c 20  00, 0x08, 0x11, 
51970 30 78 30 31 2c 5c 0a 2f 2a 20 20 34 38 20 2a 2f  0x01,\./*  48 */
51980 20 30 78 30 32 2c 20 30 78 30 31 2c 20 30 78 32   0x02, 0x01, 0x2
51990 31 2c 20 30 78 30 38 2c 20 30 78 30 30 2c 20 30  1, 0x08, 0x00, 0
519a0 78 30 32 2c 20 30 78 30 31 2c 20 30 78 31 31 2c  x02, 0x01, 0x11,
519b0 5c 0a 2f 2a 20 20 35 36 20 2a 2f 20 30 78 30 31  \./*  56 */ 0x01
519c0 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78  , 0x02, 0x00, 0x
519d0 30 34 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  04, 0x00, 0x00, 
519e0 30 78 30 32 2c 20 30 78 31 31 2c 5c 0a 2f 2a 20  0x02, 0x11,\./* 
519f0 20 36 34 20 2a 2f 20 30 78 30 30 2c 20 30 78 30   64 */ 0x00, 0x0
51a00 30 2c 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30  0, 0x2c, 0x2c, 0
51a10 78 30 35 2c 20 30 78 30 30 2c 20 30 78 31 31 2c  x05, 0x00, 0x11,
51a20 20 30 78 30 35 2c 5c 0a 2f 2a 20 20 37 32 20 2a   0x05,\./*  72 *
51a30 2f 20 30 78 30 35 2c 20 30 78 31 35 2c 20 30 78  / 0x05, 0x15, 0x
51a40 31 35 2c 20 30 78 31 35 2c 20 30 78 31 35 2c 20  15, 0x15, 0x15, 
51a50 30 78 31 35 2c 20 30 78 31 35 2c 20 30 78 30 30  0x15, 0x15, 0x00
51a60 2c 5c 0a 2f 2a 20 20 38 30 20 2a 2f 20 30 78 32  ,\./*  80 */ 0x2
51a70 63 2c 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30  c, 0x2c, 0x2c, 0
51a80 78 32 63 2c 20 30 78 32 63 2c 20 30 78 32 63 2c  x2c, 0x2c, 0x2c,
51a90 20 30 78 32 63 2c 20 30 78 32 63 2c 5c 0a 2f 2a   0x2c, 0x2c,\./*
51aa0 20 20 38 38 20 2a 2f 20 30 78 32 63 2c 20 30 78    88 */ 0x2c, 0x
51ab0 32 63 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  2c, 0x00, 0x00, 
51ac0 30 78 30 30 2c 20 30 78 30 34 2c 20 30 78 30 32  0x00, 0x04, 0x02
51ad0 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 39 36 20  , 0x00,\./*  96 
51ae0 2a 2f 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30  */ 0x01, 0x00, 0
51af0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
51b00 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30   0x00, 0x01, 0x0
51b10 30 2c 5c 0a 2f 2a 20 31 30 34 20 2a 2f 20 30 78  0,\./* 104 */ 0x
51b20 30 31 2c 20 30 78 30 32 2c 20 30 78 30 38 2c 20  01, 0x02, 0x08, 
51b30 30 78 31 31 2c 20 30 78 30 30 2c 20 30 78 30 32  0x11, 0x00, 0x02
51b40 2c 20 30 78 30 32 2c 20 30 78 31 35 2c 5c 0a 2f  , 0x02, 0x15,\./
51b50 2a 20 31 31 32 20 2a 2f 20 30 78 30 30 2c 20 30  * 112 */ 0x00, 0
51b60 78 30 30 2c 20 30 78 31 30 2c 20 30 78 30 30 2c  x00, 0x10, 0x00,
51b70 20 30 78 30 30 2c 20 30 78 30 32 2c 20 30 78 30   0x00, 0x02, 0x0
51b80 30 2c 20 30 78 30 32 2c 5c 0a 2f 2a 20 31 32 30  0, 0x02,\./* 120
51b90 20 2a 2f 20 30 78 30 31 2c 20 30 78 31 31 2c 20   */ 0x01, 0x11, 
51ba0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 35  0x00, 0x00, 0x05
51bb0 2c 20 30 78 30 30 2c 20 30 78 31 31 2c 20 30 78  , 0x00, 0x11, 0x
51bc0 30 35 2c 5c 0a 2f 2a 20 31 32 38 20 2a 2f 20 30  05,\./* 128 */ 0
51bd0 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 32 2c  x00, 0x01, 0x02,
51be0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
51bf0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 5c 0a  0, 0x00, 0x00,\.
51c00 2f 2a 20 31 33 36 20 2a 2f 20 30 78 30 30 2c 20  /* 136 */ 0x00, 
51c10 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
51c20 2c 20 30 78 30 30 2c 20 30 78 30 34 2c 20 30 78  , 0x00, 0x04, 0x
51c30 30 34 2c 20 30 78 30 34 2c 5c 0a 2f 2a 20 31 34  04, 0x04,\./* 14
51c40 34 20 2a 2f 20 30 78 30 34 2c 20 30 78 30 34 2c  4 */ 0x04, 0x04,
51c50 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
51c60 2a 2a 20 45 6e 64 20 6f 66 20 6f 70 63 6f 64 65  ** End of opcode
51c70 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s.h ************
51c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
51c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
51ca0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
51cb0 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68  ** Continuing wh
51cc0 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20  ere we left off 
51cd0 69 6e 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a  in vdbe.h ******
51ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
51cf0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 74 6f 74  */../*.** Protot
51d00 79 70 65 73 20 66 6f 72 20 74 68 65 20 56 44 42  ypes for the VDB
51d10 45 20 69 6e 74 65 72 66 61 63 65 2e 20 20 53 65  E interface.  Se
51d20 65 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 74 68  e comments on th
51d30 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
51d40 0a 2a 2a 20 66 6f 72 20 61 20 64 65 73 63 72 69  .** for a descri
51d50 70 74 69 6f 6e 20 6f 66 20 77 68 61 74 20 65 61  ption of what ea
51d60 63 68 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74  ch of these rout
51d70 69 6e 65 73 20 64 6f 65 73 2e 0a 2a 2f 0a 53 51  ines does..*/.SQ
51d80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 62  LITE_PRIVATE Vdb
51d90 65 20 2a 73 71 6c 69 74 65 33 56 64 62 65 43 72  e *sqlite3VdbeCr
51da0 65 61 74 65 28 73 71 6c 69 74 65 33 2a 29 3b 0a  eate(sqlite3*);.
51db0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
51dc0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  nt sqlite3VdbeAd
51dd0 64 4f 70 30 28 56 64 62 65 2a 2c 69 6e 74 29 3b  dOp0(Vdbe*,int);
51de0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
51df0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41  int sqlite3VdbeA
51e00 64 64 4f 70 31 28 56 64 62 65 2a 2c 69 6e 74 2c  ddOp1(Vdbe*,int,
51e10 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
51e20 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
51e30 56 64 62 65 41 64 64 4f 70 32 28 56 64 62 65 2a  VdbeAddOp2(Vdbe*
51e40 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53  ,int,int,int);.S
51e50 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
51e60 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  t sqlite3VdbeAdd
51e70 4f 70 33 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e  Op3(Vdbe*,int,in
51e80 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49  t,int,int);.SQLI
51e90 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
51ea0 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
51eb0 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69  (Vdbe*,int,int,i
51ec0 6e 74 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61  nt,int,const cha
51ed0 72 20 2a 7a 50 34 2c 69 6e 74 29 3b 0a 53 51 4c  r *zP4,int);.SQL
51ee0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
51ef0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
51f00 4c 69 73 74 28 56 64 62 65 2a 2c 20 69 6e 74 20  List(Vdbe*, int 
51f10 6e 4f 70 2c 20 56 64 62 65 4f 70 4c 69 73 74 20  nOp, VdbeOpList 
51f20 63 6f 6e 73 74 20 2a 61 4f 70 29 3b 0a 53 51 4c  const *aOp);.SQL
51f30 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
51f40 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
51f50 67 65 50 31 28 56 64 62 65 2a 2c 20 69 6e 74 20  geP1(Vdbe*, int 
51f60 61 64 64 72 2c 20 69 6e 74 20 50 31 29 3b 0a 53  addr, int P1);.S
51f70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
51f80 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  id sqlite3VdbeCh
51f90 61 6e 67 65 50 32 28 56 64 62 65 2a 2c 20 69 6e  angeP2(Vdbe*, in
51fa0 74 20 61 64 64 72 2c 20 69 6e 74 20 50 32 29 3b  t addr, int P2);
51fb0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
51fc0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
51fd0 43 68 61 6e 67 65 50 33 28 56 64 62 65 2a 2c 20  ChangeP3(Vdbe*, 
51fe0 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 50 33  int addr, int P3
51ff0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
52000 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
52010 62 65 43 68 61 6e 67 65 50 35 28 56 64 62 65 2a  beChangeP5(Vdbe*
52020 2c 20 75 38 20 50 35 29 3b 0a 53 51 4c 49 54 45  , u8 P5);.SQLITE
52030 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
52040 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
52050 65 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64  e(Vdbe*, int add
52060 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  r);.SQLITE_PRIVA
52070 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
52080 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28  dbeChangeToNoop(
52090 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c  Vdbe*, int addr,
520a0 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45 5f   int N);.SQLITE_
520b0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
520c0 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34  ite3VdbeChangeP4
520d0 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72  (Vdbe*, int addr
520e0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50  , const char *zP
520f0 34 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54  4, int N);.SQLIT
52100 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
52110 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 74  qlite3VdbeUsesBt
52120 72 65 65 28 56 64 62 65 2a 2c 20 69 6e 74 29 3b  ree(Vdbe*, int);
52130 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
52140 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 56  VdbeOp *sqlite3V
52150 64 62 65 47 65 74 4f 70 28 56 64 62 65 2a 2c 20  dbeGetOp(Vdbe*, 
52160 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
52170 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
52180 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 56 64  VdbeMakeLabel(Vd
52190 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  be*);.SQLITE_PRI
521a0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
521b0 33 56 64 62 65 44 65 6c 65 74 65 28 56 64 62 65  3VdbeDelete(Vdbe
521c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
521d0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
521e0 64 62 65 4d 61 6b 65 52 65 61 64 79 28 56 64 62  dbeMakeReady(Vdb
521f0 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69  e*,int,int,int,i
52200 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c  nt,int,int);.SQL
52210 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
52220 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c  sqlite3VdbeFinal
52230 69 7a 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49  ize(Vdbe*);.SQLI
52240 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
52250 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c  sqlite3VdbeResol
52260 76 65 4c 61 62 65 6c 28 56 64 62 65 2a 2c 20 69  veLabel(Vdbe*, i
52270 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
52280 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
52290 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 56  dbeCurrentAddr(V
522a0 64 62 65 2a 29 3b 0a 23 69 66 64 65 66 20 53 51  dbe*);.#ifdef SQ
522b0 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54  LITE_DEBUG.SQLIT
522c0 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20  E_PRIVATE   int 
522d0 73 71 6c 69 74 65 33 56 64 62 65 41 73 73 65 72  sqlite3VdbeAsser
522e0 74 4d 61 79 41 62 6f 72 74 28 56 64 62 65 20 2a  tMayAbort(Vdbe *
522f0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
52300 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
52310 6c 69 74 65 33 56 64 62 65 54 72 61 63 65 28 56  lite3VdbeTrace(V
52320 64 62 65 2a 2c 46 49 4c 45 2a 29 3b 0a 23 65 6e  dbe*,FILE*);.#en
52330 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  dif.SQLITE_PRIVA
52340 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
52350 64 62 65 52 65 73 65 74 53 74 65 70 52 65 73 75  dbeResetStepResu
52360 6c 74 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54  lt(Vdbe*);.SQLIT
52370 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
52380 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 28 56  lite3VdbeReset(V
52390 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  dbe*);.SQLITE_PR
523a0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
523b0 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
523c0 28 56 64 62 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c  (Vdbe*,int);.SQL
523d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
523e0 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
523f0 6c 4e 61 6d 65 28 56 64 62 65 2a 2c 20 69 6e 74  lName(Vdbe*, int
52400 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61  , int, const cha
52410 72 20 2a 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69  r *, void(*)(voi
52420 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  d*));.SQLITE_PRI
52430 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
52440 33 56 64 62 65 43 6f 75 6e 74 43 68 61 6e 67 65  3VdbeCountChange
52450 73 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45  s(Vdbe*);.SQLITE
52460 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33  _PRIVATE sqlite3
52470 20 2a 73 71 6c 69 74 65 33 56 64 62 65 44 62 28   *sqlite3VdbeDb(
52480 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Vdbe*);.SQLITE_P
52490 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
524a0 74 65 33 56 64 62 65 53 65 74 53 71 6c 28 56 64  te3VdbeSetSql(Vd
524b0 62 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  be*, const char 
524c0 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 29 3b  *z, int n, int);
524d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
524e0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
524f0 53 77 61 70 28 56 64 62 65 2a 2c 56 64 62 65 2a  Swap(Vdbe*,Vdbe*
52500 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
52510 45 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65  E VdbeOp *sqlite
52520 33 56 64 62 65 54 61 6b 65 4f 70 41 72 72 61 79  3VdbeTakeOpArray
52530 28 56 64 62 65 2a 2c 20 69 6e 74 2a 2c 20 69 6e  (Vdbe*, int*, in
52540 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
52550 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
52560 56 64 62 65 50 72 6f 67 72 61 6d 44 65 6c 65 74  VdbeProgramDelet
52570 65 28 73 71 6c 69 74 65 33 20 2a 2c 20 53 75 62  e(sqlite3 *, Sub
52580 50 72 6f 67 72 61 6d 20 2a 2c 20 69 6e 74 29 3b  Program *, int);
52590 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
525a0 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41  ENABLE_MEMORY_MA
525b0 4e 41 47 45 4d 45 4e 54 0a 53 51 4c 49 54 45 5f  NAGEMENT.SQLITE_
525c0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
525d0 74 65 33 56 64 62 65 52 65 6c 65 61 73 65 4d 65  te3VdbeReleaseMe
525e0 6d 6f 72 79 28 69 6e 74 29 3b 0a 23 65 6e 64 69  mory(int);.#endi
525f0 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  f.SQLITE_PRIVATE
52600 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20   UnpackedRecord 
52610 2a 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f  *sqlite3VdbeReco
52620 72 64 55 6e 70 61 63 6b 28 4b 65 79 49 6e 66 6f  rdUnpack(KeyInfo
52630 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64  *,int,const void
52640 2a 2c 63 68 61 72 2a 2c 69 6e 74 29 3b 0a 53 51  *,char*,int);.SQ
52650 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
52660 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c  d sqlite3VdbeDel
52670 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  eteUnpackedRecor
52680 64 28 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64  d(UnpackedRecord
52690 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
526a0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
526b0 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28  beRecordCompare(
526c0 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  int,const void*,
526d0 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 2a 29  UnpackedRecord*)
526e0 3b 0a 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  ;...#ifndef NDEB
526f0 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  UG.SQLITE_PRIVAT
52700 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
52710 56 64 62 65 43 6f 6d 6d 65 6e 74 28 56 64 62 65  VdbeComment(Vdbe
52720 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  *, const char*, 
52730 2e 2e 2e 29 3b 0a 23 20 64 65 66 69 6e 65 20 56  ...);.# define V
52740 64 62 65 43 6f 6d 6d 65 6e 74 28 58 29 20 20 73  dbeComment(X)  s
52750 71 6c 69 74 65 33 56 64 62 65 43 6f 6d 6d 65 6e  qlite3VdbeCommen
52760 74 20 58 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t X.SQLITE_PRIVA
52770 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
52780 33 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74  3VdbeNoopComment
52790 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74 20 63 68  (Vdbe*, const ch
527a0 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 23 20 64 65 66  ar*, ...);.# def
527b0 69 6e 65 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d  ine VdbeNoopComm
527c0 65 6e 74 28 58 29 20 20 73 71 6c 69 74 65 33 56  ent(X)  sqlite3V
527d0 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 20 58  dbeNoopComment X
527e0 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
527f0 56 64 62 65 43 6f 6d 6d 65 6e 74 28 58 29 0a 23  VdbeComment(X).#
52800 20 64 65 66 69 6e 65 20 56 64 62 65 4e 6f 6f 70   define VdbeNoop
52810 43 6f 6d 6d 65 6e 74 28 58 29 0a 23 65 6e 64 69  Comment(X).#endi
52820 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a  f..#endif../****
52830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
52840 66 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a  f vdbe.h *******
52850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
52880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
52890 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
528a0 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74  eft off in sqlit
528b0 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eInt.h *********
528c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
528d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
528e0 64 65 20 70 61 67 65 72 2e 68 20 69 6e 20 74 68  de pager.h in th
528f0 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69  e middle of sqli
52900 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  teInt.h ********
52910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
52920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
52930 20 66 69 6c 65 20 70 61 67 65 72 2e 68 20 2a 2a   file pager.h **
52940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
52970 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
52980 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
52990 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
529a0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
529b0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
529c0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
529d0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
529e0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
529f0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
52a00 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
52a10 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
52a20 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
52a30 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
52a40 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
52a50 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
52a60 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
52a70 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
52a80 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
52a90 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
52aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52ae0 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72  *.** This header
52af0 20 66 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68   file defines th
52b00 65 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 74  e interface that
52b10 20 74 68 65 20 73 71 6c 69 74 65 20 70 61 67 65   the sqlite page
52b20 20 63 61 63 68 65 0a 2a 2a 20 73 75 62 73 79 73   cache.** subsys
52b30 74 65 6d 2e 20 20 54 68 65 20 70 61 67 65 20 63  tem.  The page c
52b40 61 63 68 65 20 73 75 62 73 79 73 74 65 6d 20 72  ache subsystem r
52b50 65 61 64 73 20 61 6e 64 20 77 72 69 74 65 73 20  eads and writes 
52b60 61 20 66 69 6c 65 20 61 20 70 61 67 65 0a 2a 2a  a file a page.**
52b70 20 61 74 20 61 20 74 69 6d 65 20 61 6e 64 20 70   at a time and p
52b80 72 6f 76 69 64 65 73 20 61 20 6a 6f 75 72 6e 61  rovides a journa
52b90 6c 20 66 6f 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a  l for rollback..
52ba0 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20  **.** @(#) $Id: 
52bb0 70 61 67 65 72 2e 68 2c 76 20 31 2e 31 30 34 20  pager.h,v 1.104 
52bc0 32 30 30 39 2f 30 37 2f 32 34 20 31 39 3a 30 31  2009/07/24 19:01
52bd0 3a 31 39 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :19 drh Exp $.*/
52be0 0a 0a 23 69 66 6e 64 65 66 20 5f 50 41 47 45 52  ..#ifndef _PAGER
52bf0 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 50 41 47  _H_.#define _PAG
52c00 45 52 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 44 65 66  ER_H_../*.** Def
52c10 61 75 6c 74 20 6d 61 78 69 6d 75 6d 20 73 69 7a  ault maximum siz
52c20 65 20 66 6f 72 20 70 65 72 73 69 73 74 65 6e 74  e for persistent
52c30 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 20   journal files. 
52c40 41 20 6e 65 67 61 74 69 76 65 20 0a 2a 2a 20 76  A negative .** v
52c50 61 6c 75 65 20 6d 65 61 6e 73 20 6e 6f 20 6c 69  alue means no li
52c60 6d 69 74 2e 20 54 68 69 73 20 76 61 6c 75 65 20  mit. This value 
52c70 6d 61 79 20 62 65 20 6f 76 65 72 72 69 64 64 65  may be overridde
52c80 6e 20 75 73 69 6e 67 20 74 68 65 20 0a 2a 2a 20  n using the .** 
52c90 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72  sqlite3PagerJour
52ca0 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 29 20 41  nalSizeLimit() A
52cb0 50 49 2e 20 53 65 65 20 61 6c 73 6f 20 22 50 52  PI. See also "PR
52cc0 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 73 69 7a  AGMA journal_siz
52cd0 65 5f 6c 69 6d 69 74 22 2e 0a 2a 2f 0a 23 69 66  e_limit"..*/.#if
52ce0 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41  ndef SQLITE_DEFA
52cf0 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45  ULT_JOURNAL_SIZE
52d00 5f 4c 49 4d 49 54 0a 20 20 23 64 65 66 69 6e 65  _LIMIT.  #define
52d10 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
52d20 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45 5f 4c 49 4d  JOURNAL_SIZE_LIM
52d30 49 54 20 2d 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a  IT -1.#endif../*
52d40 0a 2a 2a 20 54 68 65 20 74 79 70 65 20 75 73 65  .** The type use
52d50 64 20 74 6f 20 72 65 70 72 65 73 65 6e 74 20 61  d to represent a
52d60 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 20 54   page number.  T
52d70 68 65 20 66 69 72 73 74 20 70 61 67 65 20 69 6e  he first page in
52d80 20 61 20 66 69 6c 65 0a 2a 2a 20 69 73 20 63 61   a file.** is ca
52d90 6c 6c 65 64 20 70 61 67 65 20 31 2e 20 20 30 20  lled page 1.  0 
52da0 69 73 20 75 73 65 64 20 74 6f 20 72 65 70 72 65  is used to repre
52db0 73 65 6e 74 20 22 6e 6f 74 20 61 20 70 61 67 65  sent "not a page
52dc0 22 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75 33  "..*/.typedef u3
52dd0 32 20 50 67 6e 6f 3b 0a 0a 2f 2a 0a 2a 2a 20 45  2 Pgno;../*.** E
52de0 61 63 68 20 6f 70 65 6e 20 66 69 6c 65 20 69 73  ach open file is
52df0 20 6d 61 6e 61 67 65 64 20 62 79 20 61 20 73 65   managed by a se
52e00 70 61 72 61 74 65 20 69 6e 73 74 61 6e 63 65 20  parate instance 
52e10 6f 66 20 74 68 65 20 22 50 61 67 65 72 22 20 73  of the "Pager" s
52e20 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 70  tructure..*/.typ
52e30 65 64 65 66 20 73 74 72 75 63 74 20 50 61 67 65  edef struct Page
52e40 72 20 50 61 67 65 72 3b 0a 0a 2f 2a 0a 2a 2a 20  r Pager;../*.** 
52e50 48 61 6e 64 6c 65 20 74 79 70 65 20 66 6f 72 20  Handle type for 
52e60 70 61 67 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65  pages..*/.typede
52e70 66 20 73 74 72 75 63 74 20 50 67 48 64 72 20 44  f struct PgHdr D
52e80 62 50 61 67 65 3b 0a 0a 2f 2a 0a 2a 2a 20 50 61  bPage;../*.** Pa
52e90 67 65 20 6e 75 6d 62 65 72 20 50 41 47 45 52 5f  ge number PAGER_
52ea0 4d 4a 5f 50 47 4e 4f 20 69 73 20 6e 65 76 65 72  MJ_PGNO is never
52eb0 20 75 73 65 64 20 69 6e 20 61 6e 20 53 51 4c 69   used in an SQLi
52ec0 74 65 20 64 61 74 61 62 61 73 65 20 28 69 74 20  te database (it 
52ed0 69 73 0a 2a 2a 20 72 65 73 65 72 76 65 64 20 66  is.** reserved f
52ee0 6f 72 20 77 6f 72 6b 69 6e 67 20 61 72 6f 75 6e  or working aroun
52ef0 64 20 61 20 77 69 6e 64 6f 77 73 2f 70 6f 73 69  d a windows/posi
52f00 78 20 69 6e 63 6f 6d 70 61 74 69 62 69 6c 69 74  x incompatibilit
52f10 79 29 2e 20 49 74 20 69 73 0a 2a 2a 20 75 73 65  y). It is.** use
52f20 64 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  d in the journal
52f30 20 74 6f 20 73 69 67 6e 69 66 79 20 74 68 61 74   to signify that
52f40 20 74 68 65 20 72 65 6d 61 69 6e 64 65 72 20 6f   the remainder o
52f50 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  f the journal fi
52f60 6c 65 20 0a 2a 2a 20 69 73 20 64 65 76 6f 74 65  le .** is devote
52f70 64 20 74 6f 20 73 74 6f 72 69 6e 67 20 61 20 6d  d to storing a m
52f80 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61  aster journal na
52f90 6d 65 20 2d 20 74 68 65 72 65 20 61 72 65 20 6e  me - there are n
52fa0 6f 20 6d 6f 72 65 20 70 61 67 65 73 20 74 6f 0a  o more pages to.
52fb0 2a 2a 20 72 6f 6c 6c 20 62 61 63 6b 2e 20 53 65  ** roll back. Se
52fc0 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 66  e comments for f
52fd0 75 6e 63 74 69 6f 6e 20 77 72 69 74 65 4d 61 73  unction writeMas
52fe0 74 65 72 4a 6f 75 72 6e 61 6c 28 29 20 69 6e 20  terJournal() in 
52ff0 70 61 67 65 72 2e 63 20 0a 2a 2a 20 66 6f 72 20  pager.c .** for 
53000 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 64 65 66  details..*/.#def
53010 69 6e 65 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e  ine PAGER_MJ_PGN
53020 4f 28 78 29 20 28 28 50 67 6e 6f 29 28 28 50 45  O(x) ((Pgno)((PE
53030 4e 44 49 4e 47 5f 42 59 54 45 2f 28 28 78 29 2d  NDING_BYTE/((x)-
53040 3e 70 61 67 65 53 69 7a 65 29 29 2b 31 29 29 0a  >pageSize))+1)).
53050 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76  ./*.** Allowed v
53060 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 66 6c  alues for the fl
53070 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f  ags parameter to
53080 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65   sqlite3PagerOpe
53090 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a  n()..**.** NOTE:
530a0 20 54 68 65 73 65 20 76 61 6c 75 65 73 20 6d 75   These values mu
530b0 73 74 20 6d 61 74 63 68 20 74 68 65 20 63 6f 72  st match the cor
530c0 72 65 73 70 6f 6e 64 69 6e 67 20 42 54 52 45 45  responding BTREE
530d0 5f 20 76 61 6c 75 65 73 20 69 6e 20 62 74 72 65  _ values in btre
530e0 65 2e 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  e.h..*/.#define 
530f0 50 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e  PAGER_OMIT_JOURN
53100 41 4c 20 20 30 78 30 30 30 31 20 20 20 20 2f 2a  AL  0x0001    /*
53110 20 44 6f 20 6e 6f 74 20 75 73 65 20 61 20 72 6f   Do not use a ro
53120 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 2a  llback journal *
53130 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f  /.#define PAGER_
53140 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 20 20 30 78  NO_READLOCK   0x
53150 30 30 30 32 20 20 20 20 2f 2a 20 4f 6d 69 74 20  0002    /* Omit 
53160 72 65 61 64 6c 6f 63 6b 73 20 6f 6e 20 72 65 61  readlocks on rea
53170 64 6f 6e 6c 79 20 66 69 6c 65 73 20 2a 2f 0a 0a  donly files */..
53180 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 76 61 6c 75  /*.** Valid valu
53190 65 73 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e  es for the secon
531a0 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71  d argument to sq
531b0 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e  lite3PagerLockin
531c0 67 4d 6f 64 65 28 29 2e 0a 2a 2f 0a 23 64 65 66  gMode()..*/.#def
531d0 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e  ine PAGER_LOCKIN
531e0 47 4d 4f 44 45 5f 51 55 45 52 59 20 20 20 20 20  GMODE_QUERY     
531f0 20 2d 31 0a 23 64 65 66 69 6e 65 20 50 41 47 45   -1.#define PAGE
53200 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f  R_LOCKINGMODE_NO
53210 52 4d 41 4c 20 20 20 20 20 20 30 0a 23 64 65 66  RMAL      0.#def
53220 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e  ine PAGER_LOCKIN
53230 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20  GMODE_EXCLUSIVE 
53240 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64    1../*.** Valid
53250 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20   values for the 
53260 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
53270 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a  to sqlite3PagerJ
53280 6f 75 72 6e 61 6c 4d 6f 64 65 28 29 2e 0a 2a 2f  ournalMode()..*/
53290 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4a  .#define PAGER_J
532a0 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59  OURNALMODE_QUERY
532b0 20 20 20 20 20 20 2d 31 0a 23 64 65 66 69 6e 65        -1.#define
532c0 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f   PAGER_JOURNALMO
532d0 44 45 5f 44 45 4c 45 54 45 20 20 20 20 20 20 30  DE_DELETE      0
532e0 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 79 20     /* Commit by 
532f0 64 65 6c 65 74 69 6e 67 20 6a 6f 75 72 6e 61 6c  deleting journal
53300 20 66 69 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65   file */.#define
53310 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f   PAGER_JOURNALMO
53320 44 45 5f 50 45 52 53 49 53 54 20 20 20 20 20 31  DE_PERSIST     1
53330 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 79 20     /* Commit by 
53340 7a 65 72 6f 69 6e 67 20 6a 6f 75 72 6e 61 6c 20  zeroing journal 
53350 68 65 61 64 65 72 20 2a 2f 0a 23 64 65 66 69 6e  header */.#defin
53360 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  e PAGER_JOURNALM
53370 4f 44 45 5f 4f 46 46 20 20 20 20 20 20 20 20 20  ODE_OFF         
53380 32 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20 6f  2   /* Journal o
53390 6d 69 74 74 65 64 2e 20 20 2a 2f 0a 23 64 65 66  mitted.  */.#def
533a0 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41  ine PAGER_JOURNA
533b0 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 20 20  LMODE_TRUNCATE  
533c0 20 20 33 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20    3   /* Commit 
533d0 62 79 20 74 72 75 6e 63 61 74 69 6e 67 20 6a 6f  by truncating jo
533e0 75 72 6e 61 6c 20 2a 2f 0a 23 64 65 66 69 6e 65  urnal */.#define
533f0 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f   PAGER_JOURNALMO
53400 44 45 5f 4d 45 4d 4f 52 59 20 20 20 20 20 20 34  DE_MEMORY      4
53410 20 20 20 2f 2a 20 49 6e 2d 6d 65 6d 6f 72 79 20     /* In-memory 
53420 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a  journal file */.
53430 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69  ./*.** The remai
53440 6e 64 65 72 20 6f 66 20 74 68 69 73 20 66 69 6c  nder of this fil
53450 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 64  e contains the d
53460 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 74  eclarations of t
53470 68 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20  he functions.** 
53480 74 68 61 74 20 6d 61 6b 65 20 75 70 20 74 68 65  that make up the
53490 20 50 61 67 65 72 20 73 75 62 2d 73 79 73 74 65   Pager sub-syste
534a0 6d 20 41 50 49 2e 20 53 65 65 20 73 6f 75 72 63  m API. See sourc
534b0 65 20 63 6f 64 65 20 63 6f 6d 6d 65 6e 74 73 20  e code comments 
534c0 66 6f 72 20 0a 2a 2a 20 61 20 64 65 74 61 69 6c  for .** a detail
534d0 65 64 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f  ed description o
534e0 66 20 65 61 63 68 20 72 6f 75 74 69 6e 65 2e 0a  f each routine..
534f0 2a 2f 0a 0a 2f 2a 20 4f 70 65 6e 20 61 6e 64 20  */../* Open and 
53500 63 6c 6f 73 65 20 61 20 50 61 67 65 72 20 63 6f  close a Pager co
53510 6e 6e 65 63 74 69 6f 6e 2e 20 2a 2f 20 0a 53 51  nnection. */ .SQ
53520 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
53530 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65   sqlite3PagerOpe
53540 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  n(.  sqlite3_vfs
53550 2a 2c 0a 20 20 50 61 67 65 72 20 2a 2a 70 70 50  *,.  Pager **ppP
53560 61 67 65 72 2c 0a 20 20 63 6f 6e 73 74 20 63 68  ager,.  const ch
53570 61 72 2a 2c 0a 20 20 69 6e 74 2c 0a 20 20 69 6e  ar*,.  int,.  in
53580 74 2c 0a 20 20 69 6e 74 2c 0a 20 20 76 6f 69 64  t,.  int,.  void
53590 28 2a 29 28 44 62 50 61 67 65 2a 29 0a 29 3b 0a  (*)(DbPage*).);.
535a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
535b0 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43  nt sqlite3PagerC
535c0 6c 6f 73 65 28 50 61 67 65 72 20 2a 70 50 61 67  lose(Pager *pPag
535d0 65 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  er);.SQLITE_PRIV
535e0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
535f0 61 67 65 72 52 65 61 64 46 69 6c 65 68 65 61 64  agerReadFilehead
53600 65 72 28 50 61 67 65 72 2a 2c 20 69 6e 74 2c 20  er(Pager*, int, 
53610 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 3b  unsigned char*);
53620 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20 75  ../* Functions u
53630 73 65 64 20 74 6f 20 63 6f 6e 66 69 67 75 72 65  sed to configure
53640 20 61 20 50 61 67 65 72 20 6f 62 6a 65 63 74 2e   a Pager object.
53650 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   */.SQLITE_PRIVA
53660 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
53670 61 67 65 72 53 65 74 42 75 73 79 68 61 6e 64 6c  agerSetBusyhandl
53680 65 72 28 50 61 67 65 72 2a 2c 20 69 6e 74 28 2a  er(Pager*, int(*
53690 29 28 76 6f 69 64 20 2a 29 2c 20 76 6f 69 64 20  )(void *), void 
536a0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
536b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
536c0 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 50  gerSetPagesize(P
536d0 61 67 65 72 2a 2c 20 75 31 36 2a 2c 20 69 6e 74  ager*, u16*, int
536e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
536f0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
53700 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 50  erMaxPageCount(P
53710 61 67 65 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  ager*, int);.SQL
53720 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
53730 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74   sqlite3PagerSet
53740 43 61 63 68 65 73 69 7a 65 28 50 61 67 65 72 2a  Cachesize(Pager*
53750 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
53760 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
53770 74 65 33 50 61 67 65 72 53 65 74 53 61 66 65 74  te3PagerSetSafet
53780 79 4c 65 76 65 6c 28 50 61 67 65 72 2a 2c 69 6e  yLevel(Pager*,in
53790 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  t,int);.SQLITE_P
537a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
537b0 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f  e3PagerLockingMo
537c0 64 65 28 50 61 67 65 72 20 2a 2c 20 69 6e 74 29  de(Pager *, int)
537d0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
537e0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
537f0 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50 61 67  rJournalMode(Pag
53800 65 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  er *, int);.SQLI
53810 54 45 5f 50 52 49 56 41 54 45 20 69 36 34 20 73  TE_PRIVATE i64 s
53820 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e  qlite3PagerJourn
53830 61 6c 53 69 7a 65 4c 69 6d 69 74 28 50 61 67 65  alSizeLimit(Page
53840 72 20 2a 2c 20 69 36 34 29 3b 0a 53 51 4c 49 54  r *, i64);.SQLIT
53850 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65  E_PRIVATE sqlite
53860 33 5f 62 61 63 6b 75 70 20 2a 2a 73 71 6c 69 74  3_backup **sqlit
53870 65 33 50 61 67 65 72 42 61 63 6b 75 70 50 74 72  e3PagerBackupPtr
53880 28 50 61 67 65 72 2a 29 3b 0a 0a 2f 2a 20 46 75  (Pager*);../* Fu
53890 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20  nctions used to 
538a0 6f 62 74 61 69 6e 20 61 6e 64 20 72 65 6c 65 61  obtain and relea
538b0 73 65 20 70 61 67 65 20 72 65 66 65 72 65 6e 63  se page referenc
538c0 65 73 2e 20 2a 2f 20 0a 53 51 4c 49 54 45 5f 50  es. */ .SQLITE_P
538d0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
538e0 65 33 50 61 67 65 72 41 63 71 75 69 72 65 28 50  e3PagerAcquire(P
538f0 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67  ager *pPager, Pg
53900 6e 6f 20 70 67 6e 6f 2c 20 44 62 50 61 67 65 20  no pgno, DbPage 
53910 2a 2a 70 70 50 61 67 65 2c 20 69 6e 74 20 63 6c  **ppPage, int cl
53920 72 46 6c 61 67 29 3b 0a 23 64 65 66 69 6e 65 20  rFlag);.#define 
53930 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28  sqlite3PagerGet(
53940 41 2c 42 2c 43 29 20 73 71 6c 69 74 65 33 50 61  A,B,C) sqlite3Pa
53950 67 65 72 41 63 71 75 69 72 65 28 41 2c 42 2c 43  gerAcquire(A,B,C
53960 2c 30 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ,0).SQLITE_PRIVA
53970 54 45 20 44 62 50 61 67 65 20 2a 73 71 6c 69 74  TE DbPage *sqlit
53980 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 50 61  e3PagerLookup(Pa
53990 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e  ger *pPager, Pgn
539a0 6f 20 70 67 6e 6f 29 3b 0a 53 51 4c 49 54 45 5f  o pgno);.SQLITE_
539b0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
539c0 69 74 65 33 50 61 67 65 72 52 65 66 28 44 62 50  ite3PagerRef(DbP
539d0 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  age*);.SQLITE_PR
539e0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
539f0 65 33 50 61 67 65 72 55 6e 72 65 66 28 44 62 50  e3PagerUnref(DbP
53a00 61 67 65 2a 29 3b 0a 0a 2f 2a 20 4f 70 65 72 61  age*);../* Opera
53a10 74 69 6f 6e 73 20 6f 6e 20 70 61 67 65 20 72 65  tions on page re
53a20 66 65 72 65 6e 63 65 73 2e 20 2a 2f 0a 53 51 4c  ferences. */.SQL
53a30 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
53a40 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
53a50 65 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49  e(DbPage*);.SQLI
53a60 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
53a70 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74  sqlite3PagerDont
53a80 57 72 69 74 65 28 44 62 50 61 67 65 2a 29 3b 0a  Write(DbPage*);.
53a90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
53aa0 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d  nt sqlite3PagerM
53ab0 6f 76 65 70 61 67 65 28 50 61 67 65 72 2a 2c 44  ovepage(Pager*,D
53ac0 62 50 61 67 65 2a 2c 50 67 6e 6f 2c 69 6e 74 29  bPage*,Pgno,int)
53ad0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
53ae0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
53af0 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 44 62  rPageRefcount(Db
53b00 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Page*);.SQLITE_P
53b10 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
53b20 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61  ite3PagerGetData
53b30 28 44 62 50 61 67 65 20 2a 29 3b 20 0a 53 51 4c  (DbPage *); .SQL
53b40 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
53b50 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 47 65   *sqlite3PagerGe
53b60 74 45 78 74 72 61 28 44 62 50 61 67 65 20 2a 29  tExtra(DbPage *)
53b70 3b 20 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73  ; ../* Functions
53b80 20 75 73 65 64 20 74 6f 20 6d 61 6e 61 67 65 20   used to manage 
53b90 70 61 67 65 72 20 74 72 61 6e 73 61 63 74 69 6f  pager transactio
53ba0 6e 73 20 61 6e 64 20 73 61 76 65 70 6f 69 6e 74  ns and savepoint
53bb0 73 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s. */.SQLITE_PRI
53bc0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
53bd0 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 50  PagerPagecount(P
53be0 61 67 65 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51  ager*, int*);.SQ
53bf0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
53c00 20 73 71 6c 69 74 65 33 50 61 67 65 72 42 65 67   sqlite3PagerBeg
53c10 69 6e 28 50 61 67 65 72 2a 2c 20 69 6e 74 20 65  in(Pager*, int e
53c20 78 46 6c 61 67 2c 20 69 6e 74 29 3b 0a 53 51 4c  xFlag, int);.SQL
53c30 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
53c40 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d  sqlite3PagerComm
53c50 69 74 50 68 61 73 65 4f 6e 65 28 50 61 67 65 72  itPhaseOne(Pager
53c60 2a 2c 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d  *,const char *zM
53c70 61 73 74 65 72 2c 20 69 6e 74 29 3b 0a 53 51 4c  aster, int);.SQL
53c80 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
53c90 73 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63  sqlite3PagerSync
53ca0 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 3b  (Pager *pPager);
53cb0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
53cc0 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
53cd0 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 50  CommitPhaseTwo(P
53ce0 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ager*);.SQLITE_P
53cf0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
53d00 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28  e3PagerRollback(
53d10 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  Pager*);.SQLITE_
53d20 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
53d30 74 65 33 50 61 67 65 72 4f 70 65 6e 53 61 76 65  te3PagerOpenSave
53d40 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50 61  point(Pager *pPa
53d50 67 65 72 2c 20 69 6e 74 20 6e 29 3b 0a 53 51 4c  ger, int n);.SQL
53d60 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
53d70 73 71 6c 69 74 65 33 50 61 67 65 72 53 61 76 65  sqlite3PagerSave
53d80 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50 61  point(Pager *pPa
53d90 67 65 72 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74  ger, int op, int
53da0 20 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a 53 51   iSavepoint);.SQ
53db0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
53dc0 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 68 61   sqlite3PagerSha
53dd0 72 65 64 4c 6f 63 6b 28 50 61 67 65 72 20 2a 70  redLock(Pager *p
53de0 50 61 67 65 72 29 3b 0a 0a 2f 2a 20 46 75 6e 63  Pager);../* Func
53df0 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 71 75  tions used to qu
53e00 65 72 79 20 70 61 67 65 72 20 73 74 61 74 65 20  ery pager state 
53e10 61 6e 64 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  and configuratio
53e20 6e 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  n. */.SQLITE_PRI
53e30 56 41 54 45 20 75 38 20 73 71 6c 69 74 65 33 50  VATE u8 sqlite3P
53e40 61 67 65 72 49 73 72 65 61 64 6f 6e 6c 79 28 50  agerIsreadonly(P
53e50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ager*);.SQLITE_P
53e60 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
53e70 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28  e3PagerRefcount(
53e80 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  Pager*);.SQLITE_
53e90 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68  PRIVATE const ch
53ea0 61 72 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72  ar *sqlite3Pager
53eb0 46 69 6c 65 6e 61 6d 65 28 50 61 67 65 72 2a 29  Filename(Pager*)
53ec0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
53ed0 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 76   const sqlite3_v
53ee0 66 73 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72  fs *sqlite3Pager
53ef0 56 66 73 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c  Vfs(Pager*);.SQL
53f00 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69  ITE_PRIVATE sqli
53f10 74 65 33 5f 66 69 6c 65 20 2a 73 71 6c 69 74 65  te3_file *sqlite
53f20 33 50 61 67 65 72 46 69 6c 65 28 50 61 67 65 72  3PagerFile(Pager
53f30 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
53f40 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  TE const char *s
53f50 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e  qlite3PagerJourn
53f60 61 6c 6e 61 6d 65 28 50 61 67 65 72 2a 29 3b 0a  alname(Pager*);.
53f70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
53f80 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4e  nt sqlite3PagerN
53f90 6f 73 79 6e 63 28 50 61 67 65 72 2a 29 3b 0a 53  osync(Pager*);.S
53fa0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
53fb0 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72  id *sqlite3Pager
53fc0 54 65 6d 70 53 70 61 63 65 28 50 61 67 65 72 2a  TempSpace(Pager*
53fd0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
53fe0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
53ff0 65 72 49 73 4d 65 6d 64 62 28 50 61 67 65 72 2a  erIsMemdb(Pager*
54000 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73  );../* Functions
54010 20 75 73 65 64 20 74 6f 20 74 72 75 6e 63 61 74   used to truncat
54020 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  e the database f
54030 69 6c 65 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  ile. */.SQLITE_P
54040 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
54050 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 65  te3PagerTruncate
54060 49 6d 61 67 65 28 50 61 67 65 72 2a 2c 50 67 6e  Image(Pager*,Pgn
54070 6f 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e  o);../* Function
54080 73 20 74 6f 20 73 75 70 70 6f 72 74 20 74 65 73  s to support tes
54090 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69  ting and debuggi
540a0 6e 67 2e 20 2a 2f 0a 23 69 66 20 21 64 65 66 69  ng. */.#if !defi
540b0 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64  ned(NDEBUG) || d
540c0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45  efined(SQLITE_TE
540d0 53 54 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ST).SQLITE_PRIVA
540e0 54 45 20 20 20 50 67 6e 6f 20 73 71 6c 69 74 65  TE   Pgno sqlite
540f0 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65 72  3PagerPagenumber
54100 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54  (DbPage*);.SQLIT
54110 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20  E_PRIVATE   int 
54120 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72  sqlite3PagerIswr
54130 69 74 65 61 62 6c 65 28 44 62 50 61 67 65 2a 29  iteable(DbPage*)
54140 3b 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  ;.#endif.#ifdef 
54150 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49  SQLITE_TEST.SQLI
54160 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
54170 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 53 74   *sqlite3PagerSt
54180 61 74 73 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c  ats(Pager*);.SQL
54190 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
541a0 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 52  id sqlite3PagerR
541b0 65 66 64 75 6d 70 28 50 61 67 65 72 2a 29 3b 0a  efdump(Pager*);.
541c0 20 20 76 6f 69 64 20 64 69 73 61 62 6c 65 5f 73    void disable_s
541d0 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f  imulated_io_erro
541e0 72 73 28 76 6f 69 64 29 3b 0a 20 20 76 6f 69 64  rs(void);.  void
541f0 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65   enable_simulate
54200 64 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64  d_io_errors(void
54210 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  );.#else.# defin
54220 65 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61  e disable_simula
54230 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 0a  ted_io_errors().
54240 23 20 64 65 66 69 6e 65 20 65 6e 61 62 6c 65 5f  # define enable_
54250 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72  simulated_io_err
54260 6f 72 73 28 29 0a 23 65 6e 64 69 66 0a 0a 23 65  ors().#endif..#e
54270 6e 64 69 66 20 2f 2a 20 5f 50 41 47 45 52 5f 48  ndif /* _PAGER_H
54280 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  _ */../*********
54290 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 67  ***** End of pag
542a0 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  er.h ***********
542b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
542c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
542d0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
542e0 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
542f0 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
54300 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e  ff in sqliteInt.
54310 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
54320 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
54330 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 70 63  ***** Include pc
54340 61 63 68 65 2e 68 20 69 6e 20 74 68 65 20 6d 69  ache.h in the mi
54350 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e  ddle of sqliteIn
54360 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
54370 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
54380 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
54390 20 70 63 61 63 68 65 2e 68 20 2a 2a 2a 2a 2a 2a   pcache.h ******
543a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
543b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
543c0 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38  ****/./*.** 2008
543d0 20 41 75 67 75 73 74 20 30 35 0a 2a 2a 0a 2a 2a   August 05.**.**
543e0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
543f0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
54400 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
54410 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
54420 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
54430 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
54440 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
54450 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
54460 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
54470 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
54480 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
54490 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
544a0 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
544b0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
544c0 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
544d0 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
544e0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
544f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54530 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
54540 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 65  s header file de
54550 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66  fines the interf
54560 61 63 65 20 74 68 61 74 20 74 68 65 20 73 71 6c  ace that the sql
54570 69 74 65 20 70 61 67 65 20 63 61 63 68 65 0a 2a  ite page cache.*
54580 2a 20 73 75 62 73 79 73 74 65 6d 2e 20 0a 2a 2a  * subsystem. .**
54590 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 70 63  .** @(#) $Id: pc
545a0 61 63 68 65 2e 68 2c 76 20 31 2e 32 30 20 32 30  ache.h,v 1.20 20
545b0 30 39 2f 30 37 2f 32 35 20 31 31 3a 34 36 3a 34  09/07/25 11:46:4
545c0 39 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78  9 danielk1977 Ex
545d0 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  p $.*/..#ifndef 
545e0 5f 50 43 41 43 48 45 5f 48 5f 0a 0a 74 79 70 65  _PCACHE_H_..type
545f0 64 65 66 20 73 74 72 75 63 74 20 50 67 48 64 72  def struct PgHdr
54600 20 50 67 48 64 72 3b 0a 74 79 70 65 64 65 66 20   PgHdr;.typedef 
54610 73 74 72 75 63 74 20 50 43 61 63 68 65 20 50 43  struct PCache PC
54620 61 63 68 65 3b 0a 0a 2f 2a 0a 2a 2a 20 45 76 65  ache;../*.** Eve
54630 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 63  ry page in the c
54640 61 63 68 65 20 69 73 20 63 6f 6e 74 72 6f 6c 6c  ache is controll
54650 65 64 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63  ed by an instanc
54660 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
54670 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e  ng.** structure.
54680 0a 2a 2f 0a 73 74 72 75 63 74 20 50 67 48 64 72  .*/.struct PgHdr
54690 20 7b 0a 20 20 76 6f 69 64 20 2a 70 44 61 74 61   {.  void *pData
546a0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
546b0 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f      /* Content o
546c0 66 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20  f this page */. 
546d0 20 76 6f 69 64 20 2a 70 45 78 74 72 61 3b 20 20   void *pExtra;  
546e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
546f0 2f 2a 20 45 78 74 72 61 20 63 6f 6e 74 65 6e 74  /* Extra content
54700 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 44 69   */.  PgHdr *pDi
54710 72 74 79 3b 20 20 20 20 20 20 20 20 20 20 20 20  rty;            
54720 20 20 20 20 20 2f 2a 20 54 72 61 6e 73 69 65 6e       /* Transien
54730 74 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20  t list of dirty 
54740 70 61 67 65 73 20 2a 2f 0a 20 20 50 67 6e 6f 20  pages */.  Pgno 
54750 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20  pgno;           
54760 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67            /* Pag
54770 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 69  e number for thi
54780 73 20 70 61 67 65 20 2a 2f 0a 20 20 50 61 67 65  s page */.  Page
54790 72 20 2a 70 50 61 67 65 72 3b 20 20 20 20 20 20  r *pPager;      
547a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
547b0 65 20 70 61 67 65 72 20 74 68 69 73 20 70 61 67  e pager this pag
547c0 65 20 69 73 20 70 61 72 74 20 6f 66 20 2a 2f 0a  e is part of */.
547d0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48  #ifdef SQLITE_CH
547e0 45 43 4b 5f 50 41 47 45 53 0a 20 20 75 33 32 20  ECK_PAGES.  u32 
547f0 70 61 67 65 48 61 73 68 3b 20 20 20 20 20 20 20  pageHash;       
54800 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61             /* Ha
54810 73 68 20 6f 66 20 70 61 67 65 20 63 6f 6e 74 65  sh of page conte
54820 6e 74 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 75  nt */.#endif.  u
54830 31 36 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20  16 flags;       
54840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
54850 20 50 47 48 44 52 20 66 6c 61 67 73 20 64 65 66   PGHDR flags def
54860 69 6e 65 64 20 62 65 6c 6f 77 20 2a 2f 0a 0a 20  ined below */.. 
54870 20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   /**************
54880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
548a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
548b0 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2a 20 45 6c  ********.  ** El
548c0 65 6d 65 6e 74 73 20 61 62 6f 76 65 20 61 72 65  ements above are
548d0 20 70 75 62 6c 69 63 2e 20 20 41 6c 6c 20 74 68   public.  All th
548e0 61 74 20 66 6f 6c 6c 6f 77 73 20 69 73 20 70 72  at follows is pr
548f0 69 76 61 74 65 20 74 6f 20 70 63 61 63 68 65 2e  ivate to pcache.
54900 63 0a 20 20 2a 2a 20 61 6e 64 20 73 68 6f 75 6c  c.  ** and shoul
54910 64 20 6e 6f 74 20 62 65 20 61 63 63 65 73 73 65  d not be accesse
54920 64 20 62 79 20 6f 74 68 65 72 20 6d 6f 64 75 6c  d by other modul
54930 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 31 36 20 6e  es..  */.  i16 n
54940 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20  Ref;            
54950 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
54960 62 65 72 20 6f 66 20 75 73 65 72 73 20 6f 66 20  ber of users of 
54970 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 50  this page */.  P
54980 43 61 63 68 65 20 2a 70 43 61 63 68 65 3b 20 20  Cache *pCache;  
54990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
549a0 20 43 61 63 68 65 20 74 68 61 74 20 6f 77 6e 73   Cache that owns
549b0 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 0a 20   this page */.. 
549c0 20 50 67 48 64 72 20 2a 70 44 69 72 74 79 4e 65   PgHdr *pDirtyNe
549d0 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  xt;             
549e0 2f 2a 20 4e 65 78 74 20 65 6c 65 6d 65 6e 74 20  /* Next element 
549f0 69 6e 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79  in list of dirty
54a00 20 70 61 67 65 73 20 2a 2f 0a 20 20 50 67 48 64   pages */.  PgHd
54a10 72 20 2a 70 44 69 72 74 79 50 72 65 76 3b 20 20  r *pDirtyPrev;  
54a20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72             /* Pr
54a30 65 76 69 6f 75 73 20 65 6c 65 6d 65 6e 74 20 69  evious element i
54a40 6e 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20  n list of dirty 
54a50 70 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20  pages */.};../* 
54a60 42 69 74 20 76 61 6c 75 65 73 20 66 6f 72 20 50  Bit values for P
54a70 67 48 64 72 2e 66 6c 61 67 73 20 2a 2f 0a 23 64  gHdr.flags */.#d
54a80 65 66 69 6e 65 20 50 47 48 44 52 5f 44 49 52 54  efine PGHDR_DIRT
54a90 59 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78  Y             0x
54aa0 30 30 32 20 20 2f 2a 20 50 61 67 65 20 68 61 73  002  /* Page has
54ab0 20 63 68 61 6e 67 65 64 20 2a 2f 0a 23 64 65 66   changed */.#def
54ac0 69 6e 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53  ine PGHDR_NEED_S
54ad0 59 4e 43 20 20 20 20 20 20 20 20 20 30 78 30 30  YNC         0x00
54ae0 34 20 20 2f 2a 20 46 73 79 6e 63 20 74 68 65 20  4  /* Fsync the 
54af0 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
54b00 20 62 65 66 6f 72 65 0a 20 20 20 20 20 20 20 20   before.        
54b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
54b30 2a 20 77 72 69 74 69 6e 67 20 74 68 69 73 20 70  * writing this p
54b40 61 67 65 20 74 6f 20 74 68 65 20 64 61 74 61 62  age to the datab
54b50 61 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ase */.#define P
54b60 47 48 44 52 5f 4e 45 45 44 5f 52 45 41 44 20 20  GHDR_NEED_READ  
54b70 20 20 20 20 20 20 20 30 78 30 30 38 20 20 2f 2a         0x008  /*
54b80 20 43 6f 6e 74 65 6e 74 20 69 73 20 75 6e 72 65   Content is unre
54b90 61 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47  ad */.#define PG
54ba0 48 44 52 5f 52 45 55 53 45 5f 55 4e 4c 49 4b 45  HDR_REUSE_UNLIKE
54bb0 4c 59 20 20 20 20 30 78 30 31 30 20 20 2f 2a 20  LY    0x010  /* 
54bc0 41 20 68 69 6e 74 20 74 68 61 74 20 72 65 75 73  A hint that reus
54bd0 65 20 69 73 20 75 6e 6c 69 6b 65 6c 79 20 2a 2f  e is unlikely */
54be0 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f 44  .#define PGHDR_D
54bf0 4f 4e 54 5f 57 52 49 54 45 20 20 20 20 20 20 20  ONT_WRITE       
54c00 20 30 78 30 32 30 20 20 2f 2a 20 44 6f 20 6e 6f   0x020  /* Do no
54c10 74 20 77 72 69 74 65 20 63 6f 6e 74 65 6e 74 20  t write content 
54c20 74 6f 20 64 69 73 6b 20 2a 2f 0a 0a 2f 2a 20 49  to disk */../* I
54c30 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 73 68  nitialize and sh
54c40 75 74 64 6f 77 6e 20 74 68 65 20 70 61 67 65 20  utdown the page 
54c50 63 61 63 68 65 20 73 75 62 73 79 73 74 65 6d 20  cache subsystem 
54c60 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
54c70 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61  E int sqlite3Pca
54c80 63 68 65 49 6e 69 74 69 61 6c 69 7a 65 28 76 6f  cheInitialize(vo
54c90 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  id);.SQLITE_PRIV
54ca0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
54cb0 50 63 61 63 68 65 53 68 75 74 64 6f 77 6e 28 76  PcacheShutdown(v
54cc0 6f 69 64 29 3b 0a 0a 2f 2a 20 50 61 67 65 20 63  oid);../* Page c
54cd0 61 63 68 65 20 62 75 66 66 65 72 20 6d 61 6e 61  ache buffer mana
54ce0 67 65 6d 65 6e 74 3a 0a 2a 2a 20 54 68 65 73 65  gement:.** These
54cf0 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d   routines implem
54d00 65 6e 74 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ent SQLITE_CONFI
54d10 47 5f 50 41 47 45 43 41 43 48 45 2e 0a 2a 2f 0a  G_PAGECACHE..*/.
54d20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
54d30 6f 69 64 20 73 71 6c 69 74 65 33 50 43 61 63 68  oid sqlite3PCach
54d40 65 42 75 66 66 65 72 53 65 74 75 70 28 76 6f 69  eBufferSetup(voi
54d50 64 20 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74  d *, int sz, int
54d60 20 6e 29 3b 0a 0a 2f 2a 20 43 72 65 61 74 65 20   n);../* Create 
54d70 61 20 6e 65 77 20 70 61 67 65 72 20 63 61 63 68  a new pager cach
54d80 65 2e 0a 2a 2a 20 55 6e 64 65 72 20 6d 65 6d 6f  e..** Under memo
54d90 72 79 20 73 74 72 65 73 73 2c 20 69 6e 76 6f 6b  ry stress, invok
54da0 65 20 78 53 74 72 65 73 73 20 74 6f 20 74 72 79  e xStress to try
54db0 20 74 6f 20 6d 61 6b 65 20 70 61 67 65 73 20 63   to make pages c
54dc0 6c 65 61 6e 2e 0a 2a 2a 20 4f 6e 6c 79 20 63 6c  lean..** Only cl
54dd0 65 61 6e 20 61 6e 64 20 75 6e 70 69 6e 6e 65 64  ean and unpinned
54de0 20 70 61 67 65 73 20 63 61 6e 20 62 65 20 72 65   pages can be re
54df0 63 6c 61 69 6d 65 64 2e 0a 2a 2f 0a 53 51 4c 49  claimed..*/.SQLI
54e00 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
54e10 73 71 6c 69 74 65 33 50 63 61 63 68 65 4f 70 65  sqlite3PcacheOpe
54e20 6e 28 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 2c  n(.  int szPage,
54e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54e40 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65      /* Size of e
54e50 76 65 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 69  very page */.  i
54e60 6e 74 20 73 7a 45 78 74 72 61 2c 20 20 20 20 20  nt szExtra,     
54e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
54e80 20 45 78 74 72 61 20 73 70 61 63 65 20 61 73 73   Extra space ass
54e90 6f 63 69 61 74 65 64 20 77 69 74 68 20 65 61 63  ociated with eac
54ea0 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20  h page */.  int 
54eb0 62 50 75 72 67 65 61 62 6c 65 2c 20 20 20 20 20  bPurgeable,     
54ec0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
54ed0 75 65 20 69 66 20 70 61 67 65 73 20 61 72 65 20  ue if pages are 
54ee0 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f 72 65  on backing store
54ef0 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 74 72   */.  int (*xStr
54f00 65 73 73 29 28 76 6f 69 64 2a 2c 20 50 67 48 64  ess)(void*, PgHd
54f10 72 2a 29 2c 20 2f 2a 20 43 61 6c 6c 20 74 6f 20  r*), /* Call to 
54f20 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 67 65  try to make page
54f30 73 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 6f 69  s clean */.  voi
54f40 64 20 2a 70 53 74 72 65 73 73 2c 20 20 20 20 20  d *pStress,     
54f50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
54f60 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65  rgument to xStre
54f70 73 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a  ss */.  PCache *
54f80 70 54 6f 49 6e 69 74 20 20 20 20 20 20 20 20 20  pToInit         
54f90 20 20 20 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c         /* Preall
54fa0 6f 63 61 74 65 64 20 73 70 61 63 65 20 66 6f 72  ocated space for
54fb0 20 74 68 65 20 50 43 61 63 68 65 20 2a 2f 0a 29   the PCache */.)
54fc0 3b 0a 0a 2f 2a 20 4d 6f 64 69 66 79 20 74 68 65  ;../* Modify the
54fd0 20 70 61 67 65 2d 73 69 7a 65 20 61 66 74 65 72   page-size after
54fe0 20 74 68 65 20 63 61 63 68 65 20 68 61 73 20 62   the cache has b
54ff0 65 65 6e 20 63 72 65 61 74 65 64 2e 20 2a 2f 0a  een created. */.
55000 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
55010 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
55020 65 53 65 74 50 61 67 65 53 69 7a 65 28 50 43 61  eSetPageSize(PCa
55030 63 68 65 20 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a  che *, int);../*
55040 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65   Return the size
55050 20 69 6e 20 62 79 74 65 73 20 6f 66 20 61 20 50   in bytes of a P
55060 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 20 55  Cache object.  U
55070 73 65 64 20 74 6f 20 70 72 65 61 6c 6c 6f 63 61  sed to prealloca
55080 74 65 0a 2a 2a 20 73 74 6f 72 61 67 65 20 73 70  te.** storage sp
55090 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ace..*/.SQLITE_P
550a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
550b0 65 33 50 63 61 63 68 65 53 69 7a 65 28 76 6f 69  e3PcacheSize(voi
550c0 64 29 3b 0a 0a 2f 2a 20 4f 6e 65 20 72 65 6c 65  d);../* One rele
550d0 61 73 65 20 70 65 72 20 73 75 63 63 65 73 73 66  ase per successf
550e0 75 6c 20 66 65 74 63 68 2e 20 20 50 61 67 65 20  ul fetch.  Page 
550f0 69 73 20 70 69 6e 6e 65 64 20 75 6e 74 69 6c 20  is pinned until 
55100 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 20 52 65 66  released..** Ref
55110 65 72 65 6e 63 65 20 63 6f 75 6e 74 65 64 2e 20  erence counted. 
55120 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
55130 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63  TE int sqlite3Pc
55140 61 63 68 65 46 65 74 63 68 28 50 43 61 63 68 65  acheFetch(PCache
55150 2a 2c 20 50 67 6e 6f 2c 20 69 6e 74 20 63 72 65  *, Pgno, int cre
55160 61 74 65 46 6c 61 67 2c 20 50 67 48 64 72 2a 2a  ateFlag, PgHdr**
55170 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
55180 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
55190 61 63 68 65 52 65 6c 65 61 73 65 28 50 67 48 64  acheRelease(PgHd
551a0 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49  r*);..SQLITE_PRI
551b0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
551c0 33 50 63 61 63 68 65 44 72 6f 70 28 50 67 48 64  3PcacheDrop(PgHd
551d0 72 2a 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  r*);         /* 
551e0 52 65 6d 6f 76 65 20 70 61 67 65 20 66 72 6f 6d  Remove page from
551f0 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45   cache */.SQLITE
55200 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
55210 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44  lite3PcacheMakeD
55220 69 72 74 79 28 50 67 48 64 72 2a 29 3b 20 20 20  irty(PgHdr*);   
55230 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 70 61   /* Make sure pa
55240 67 65 20 69 73 20 6d 61 72 6b 65 64 20 64 69 72  ge is marked dir
55250 74 79 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  ty */.SQLITE_PRI
55260 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
55270 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e  3PcacheMakeClean
55280 28 50 67 48 64 72 2a 29 3b 20 20 20 20 2f 2a 20  (PgHdr*);    /* 
55290 4d 61 72 6b 20 61 20 73 69 6e 67 6c 65 20 70 61  Mark a single pa
552a0 67 65 20 61 73 20 63 6c 65 61 6e 20 2a 2f 0a 53  ge as clean */.S
552b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
552c0 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
552d0 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68 65 2a  CleanAll(PCache*
552e0 29 3b 20 20 20 20 2f 2a 20 4d 61 72 6b 20 61 6c  );    /* Mark al
552f0 6c 20 64 69 72 74 79 20 6c 69 73 74 20 70 61 67  l dirty list pag
55300 65 73 20 61 73 20 63 6c 65 61 6e 20 2a 2f 0a 0a  es as clean */..
55310 2f 2a 20 43 68 61 6e 67 65 20 61 20 70 61 67 65  /* Change a page
55320 20 6e 75 6d 62 65 72 2e 20 20 55 73 65 64 20 62   number.  Used b
55330 79 20 69 6e 63 72 2d 76 61 63 75 75 6d 2e 20 2a  y incr-vacuum. *
55340 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
55350 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
55360 63 68 65 4d 6f 76 65 28 50 67 48 64 72 2a 2c 20  cheMove(PgHdr*, 
55370 50 67 6e 6f 29 3b 0a 0a 2f 2a 20 52 65 6d 6f 76  Pgno);../* Remov
55380 65 20 61 6c 6c 20 70 61 67 65 73 20 77 69 74 68  e all pages with
55390 20 70 67 6e 6f 3e 78 2e 20 20 52 65 73 65 74 20   pgno>x.  Reset 
553a0 74 68 65 20 63 61 63 68 65 20 69 66 20 78 3d 3d  the cache if x==
553b0 30 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  0 */.SQLITE_PRIV
553c0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
553d0 50 63 61 63 68 65 54 72 75 6e 63 61 74 65 28 50  PcacheTruncate(P
553e0 43 61 63 68 65 2a 2c 20 50 67 6e 6f 20 78 29 3b  Cache*, Pgno x);
553f0 0a 0a 2f 2a 20 47 65 74 20 61 20 6c 69 73 74 20  ../* Get a list 
55400 6f 66 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67  of all dirty pag
55410 65 73 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c  es in the cache,
55420 20 73 6f 72 74 65 64 20 62 79 20 70 61 67 65 20   sorted by page 
55430 6e 75 6d 62 65 72 20 2a 2f 0a 53 51 4c 49 54 45  number */.SQLITE
55440 5f 50 52 49 56 41 54 45 20 50 67 48 64 72 20 2a  _PRIVATE PgHdr *
55450 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 69 72  sqlite3PcacheDir
55460 74 79 4c 69 73 74 28 50 43 61 63 68 65 2a 29 3b  tyList(PCache*);
55470 0a 0a 2f 2a 20 52 65 73 65 74 20 61 6e 64 20 63  ../* Reset and c
55480 6c 6f 73 65 20 74 68 65 20 63 61 63 68 65 20 6f  lose the cache o
55490 62 6a 65 63 74 20 2a 2f 0a 53 51 4c 49 54 45 5f  bject */.SQLITE_
554a0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
554b0 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65 28  ite3PcacheClose(
554c0 50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20 43 6c  PCache*);../* Cl
554d0 65 61 72 20 66 6c 61 67 73 20 66 72 6f 6d 20 70  ear flags from p
554e0 61 67 65 73 20 6f 66 20 74 68 65 20 70 61 67 65  ages of the page
554f0 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45   cache */.SQLITE
55500 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
55510 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 72  lite3PcacheClear
55520 53 79 6e 63 46 6c 61 67 73 28 50 43 61 63 68 65  SyncFlags(PCache
55530 20 2a 29 3b 0a 0a 2f 2a 20 44 69 73 63 61 72 64   *);../* Discard
55540 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
55550 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a 53 51   the cache */.SQ
55560 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
55570 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43  d sqlite3PcacheC
55580 6c 65 61 72 28 50 43 61 63 68 65 2a 29 3b 0a 0a  lear(PCache*);..
55590 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f  /* Return the to
555a0 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 6f 75  tal number of ou
555b0 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 20 72  tstanding page r
555c0 65 66 65 72 65 6e 63 65 73 20 2a 2f 0a 53 51 4c  eferences */.SQL
555d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
555e0 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66  sqlite3PcacheRef
555f0 43 6f 75 6e 74 28 50 43 61 63 68 65 2a 29 3b 0a  Count(PCache*);.
55600 0a 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68  ./* Increment th
55610 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  e reference coun
55620 74 20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67  t of an existing
55630 20 70 61 67 65 20 2a 2f 0a 53 51 4c 49 54 45 5f   page */.SQLITE_
55640 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
55650 69 74 65 33 50 63 61 63 68 65 52 65 66 28 50 67  ite3PcacheRef(Pg
55660 48 64 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50  Hdr*);..SQLITE_P
55670 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
55680 65 33 50 63 61 63 68 65 50 61 67 65 52 65 66 63  e3PcachePageRefc
55690 6f 75 6e 74 28 50 67 48 64 72 2a 29 3b 0a 0a 2f  ount(PgHdr*);../
556a0 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74  * Return the tot
556b0 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  al number of pag
556c0 65 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  es stored in the
556d0 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45   cache */.SQLITE
556e0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
556f0 69 74 65 33 50 63 61 63 68 65 50 61 67 65 63 6f  ite3PcachePageco
55700 75 6e 74 28 50 43 61 63 68 65 2a 29 3b 0a 0a 23  unt(PCache*);..#
55710 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
55720 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 29 20 7c  E_CHECK_PAGES) |
55730 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
55740 5f 44 45 42 55 47 29 0a 2f 2a 20 49 74 65 72 61  _DEBUG)./* Itera
55750 74 65 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 64  te through all d
55760 69 72 74 79 20 70 61 67 65 73 20 63 75 72 72 65  irty pages curre
55770 6e 74 6c 79 20 73 74 6f 72 65 64 20 69 6e 20 74  ntly stored in t
55780 68 65 20 63 61 63 68 65 2e 20 54 68 69 73 0a 2a  he cache. This.*
55790 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 6f  * interface is o
557a0 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 66  nly available if
557b0 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41   SQLITE_CHECK_PA
557c0 47 45 53 20 69 73 20 64 65 66 69 6e 65 64 20 77  GES is defined w
557d0 68 65 6e 20 74 68 65 20 0a 2a 2a 20 6c 69 62 72  hen the .** libr
557e0 61 72 79 20 69 73 20 62 75 69 6c 74 2e 0a 2a 2f  ary is built..*/
557f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
55800 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
55810 68 65 49 74 65 72 61 74 65 44 69 72 74 79 28 50  heIterateDirty(P
55820 43 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 76  Cache *pCache, v
55830 6f 69 64 20 28 2a 78 49 74 65 72 29 28 50 67 48  oid (*xIter)(PgH
55840 64 72 20 2a 29 29 3b 0a 23 65 6e 64 69 66 0a 0a  dr *));.#endif..
55850 2f 2a 20 53 65 74 20 61 6e 64 20 67 65 74 20 74  /* Set and get t
55860 68 65 20 73 75 67 67 65 73 74 65 64 20 63 61 63  he suggested cac
55870 68 65 2d 73 69 7a 65 20 66 6f 72 20 74 68 65 20  he-size for the 
55880 73 70 65 63 69 66 69 65 64 20 70 61 67 65 72 2d  specified pager-
55890 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  cache..**.** If 
558a0 6e 6f 20 67 6c 6f 62 61 6c 20 6d 61 78 69 6d 75  no global maximu
558b0 6d 20 69 73 20 63 6f 6e 66 69 67 75 72 65 64 2c  m is configured,
558c0 20 74 68 65 6e 20 74 68 65 20 73 79 73 74 65 6d   then the system
558d0 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6c 69 6d   attempts to lim
558e0 69 74 0a 2a 2a 20 74 68 65 20 74 6f 74 61 6c 20  it.** the total 
558f0 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
55900 63 61 63 68 65 64 20 62 79 20 70 75 72 67 65 61  cached by purgea
55910 62 6c 65 20 70 61 67 65 72 2d 63 61 63 68 65 73  ble pager-caches
55920 20 74 6f 20 74 68 65 20 73 75 6d 0a 2a 2a 20 6f   to the sum.** o
55930 66 20 74 68 65 20 73 75 67 67 65 73 74 65 64 20  f the suggested 
55940 63 61 63 68 65 2d 73 69 7a 65 73 2e 0a 2a 2f 0a  cache-sizes..*/.
55950 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
55960 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
55970 65 53 65 74 43 61 63 68 65 73 69 7a 65 28 50 43  eSetCachesize(PC
55980 61 63 68 65 20 2a 2c 20 69 6e 74 29 3b 0a 23 69  ache *, int);.#i
55990 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
559a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
559b0 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68  int sqlite3Pcach
559c0 65 47 65 74 43 61 63 68 65 73 69 7a 65 28 50 43  eGetCachesize(PC
559d0 61 63 68 65 20 2a 29 3b 0a 23 65 6e 64 69 66 0a  ache *);.#endif.
559e0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
559f0 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e  NABLE_MEMORY_MAN
55a00 41 47 45 4d 45 4e 54 0a 2f 2a 20 54 72 79 20 74  AGEMENT./* Try t
55a10 6f 20 72 65 74 75 72 6e 20 6d 65 6d 6f 72 79 20  o return memory 
55a20 75 73 65 64 20 62 79 20 74 68 65 20 70 63 61 63  used by the pcac
55a30 68 65 20 6d 6f 64 75 6c 65 20 74 6f 20 74 68 65  he module to the
55a40 20 6d 61 69 6e 20 6d 65 6d 6f 72 79 20 68 65 61   main memory hea
55a50 70 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  p */.SQLITE_PRIV
55a60 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
55a70 63 61 63 68 65 52 65 6c 65 61 73 65 4d 65 6d 6f  cacheReleaseMemo
55a80 72 79 28 69 6e 74 29 3b 0a 23 65 6e 64 69 66 0a  ry(int);.#endif.
55a90 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
55aa0 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  EST.SQLITE_PRIVA
55ab0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
55ac0 63 61 63 68 65 53 74 61 74 73 28 69 6e 74 2a 2c  cacheStats(int*,
55ad0 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29 3b  int*,int*,int*);
55ae0 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f  .#endif..SQLITE_
55af0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
55b00 69 74 65 33 50 43 61 63 68 65 53 65 74 44 65 66  ite3PCacheSetDef
55b10 61 75 6c 74 28 76 6f 69 64 29 3b 0a 0a 23 65 6e  ault(void);..#en
55b20 64 69 66 20 2f 2a 20 5f 50 43 41 43 48 45 5f 48  dif /* _PCACHE_H
55b30 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  _ */../*********
55b40 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 63 61  ***** End of pca
55b50 63 68 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  che.h **********
55b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55b80 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
55b90 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
55ba0 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
55bb0 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e  ff in sqliteInt.
55bc0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
55bd0 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ****/../********
55be0 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f  ****** Include o
55bf0 73 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  s.h in the middl
55c00 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68  e of sqliteInt.h
55c10 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
55c20 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
55c30 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
55c40 65 20 6f 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  e os.h *********
55c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55c70 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
55c80 31 20 53 65 70 74 65 6d 62 65 72 20 31 36 0a 2a  1 September 16.*
55c90 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
55ca0 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
55cb0 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
55cc0 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
55cd0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
55ce0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
55cf0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
55d00 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
55d10 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
55d20 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
55d30 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
55d40 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
55d50 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
55d60 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
55d70 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
55d80 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
55d90 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
55da0 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
55db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55df0 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65  **.**.** This he
55e00 61 64 65 72 20 66 69 6c 65 20 28 74 6f 67 65 74  ader file (toget
55e10 68 65 72 20 77 69 74 68 20 69 73 20 63 6f 6d 70  her with is comp
55e20 61 6e 69 6f 6e 20 43 20 73 6f 75 72 63 65 2d 63  anion C source-c
55e30 6f 64 65 20 66 69 6c 65 0a 2a 2a 20 22 6f 73 2e  ode file.** "os.
55e40 63 22 29 20 61 74 74 65 6d 70 74 20 74 6f 20 61  c") attempt to a
55e50 62 73 74 72 61 63 74 20 74 68 65 20 75 6e 64 65  bstract the unde
55e60 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67  rlying operating
55e70 20 73 79 73 74 65 6d 20 73 6f 20 74 68 61 74 0a   system so that.
55e80 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69  ** the SQLite li
55e90 62 72 61 72 79 20 77 69 6c 6c 20 77 6f 72 6b 20  brary will work 
55ea0 6f 6e 20 62 6f 74 68 20 50 4f 53 49 58 20 61 6e  on both POSIX an
55eb0 64 20 77 69 6e 64 6f 77 73 20 73 79 73 74 65 6d  d windows system
55ec0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65  s..**.** This he
55ed0 61 64 65 72 20 66 69 6c 65 20 69 73 20 23 69 6e  ader file is #in
55ee0 63 6c 75 64 65 2d 65 64 20 62 79 20 73 71 6c 69  clude-ed by sqli
55ef0 74 65 49 6e 74 2e 68 20 61 6e 64 20 74 68 75 73  teInt.h and thus
55f00 20 65 6e 64 73 20 75 70 0a 2a 2a 20 62 65 69 6e   ends up.** bein
55f10 67 20 69 6e 63 6c 75 64 65 64 20 62 79 20 65 76  g included by ev
55f20 65 72 79 20 73 6f 75 72 63 65 20 66 69 6c 65 2e  ery source file.
55f30 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 2e 68  .**.** $Id: os.h
55f40 2c 76 20 31 2e 31 30 38 20 32 30 30 39 2f 30 32  ,v 1.108 2009/02
55f50 2f 30 35 20 31 36 3a 33 31 3a 34 36 20 64 72 68  /05 16:31:46 drh
55f60 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65   Exp $.*/.#ifnde
55f70 66 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 48 5f 0a  f _SQLITE_OS_H_.
55f80 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f  #define _SQLITE_
55f90 4f 53 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 46 69 67  OS_H_../*.** Fig
55fa0 75 72 65 20 6f 75 74 20 69 66 20 77 65 20 61 72  ure out if we ar
55fb0 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 55  e dealing with U
55fc0 6e 69 78 2c 20 57 69 6e 64 6f 77 73 2c 20 6f 72  nix, Windows, or
55fd0 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 6f   some other.** o
55fe0 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e  perating system.
55ff0 20 20 41 66 74 65 72 20 74 68 65 20 66 6f 6c 6c    After the foll
56000 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 6f 66 20 70  owing block of p
56010 72 65 70 72 6f 63 65 73 73 20 6d 61 63 72 6f 73  reprocess macros
56020 2c 0a 2a 2a 20 61 6c 6c 20 6f 66 20 53 51 4c 49  ,.** all of SQLI
56030 54 45 5f 4f 53 5f 55 4e 49 58 2c 20 53 51 4c 49  TE_OS_UNIX, SQLI
56040 54 45 5f 4f 53 5f 57 49 4e 2c 20 53 51 4c 49 54  TE_OS_WIN, SQLIT
56050 45 5f 4f 53 5f 4f 53 32 2c 20 61 6e 64 20 53 51  E_OS_OS2, and SQ
56060 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 20 0a 2a  LITE_OS_OTHER .*
56070 2a 20 77 69 6c 6c 20 64 65 66 69 6e 65 64 20 74  * will defined t
56080 6f 20 65 69 74 68 65 72 20 31 20 6f 72 20 30 2e  o either 1 or 0.
56090 20 20 4f 6e 65 20 6f 66 20 74 68 65 20 66 6f 75    One of the fou
560a0 72 20 77 69 6c 6c 20 62 65 20 31 2e 20 20 54 68  r will be 1.  Th
560b0 65 20 6f 74 68 65 72 20 0a 2a 2a 20 74 68 72 65  e other .** thre
560c0 65 20 77 69 6c 6c 20 62 65 20 30 2e 0a 2a 2f 0a  e will be 0..*/.
560d0 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
560e0 54 45 5f 4f 53 5f 4f 54 48 45 52 29 0a 23 20 69  TE_OS_OTHER).# i
560f0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45  f SQLITE_OS_OTHE
56100 52 3d 3d 31 0a 23 20 20 20 75 6e 64 65 66 20 53  R==1.#   undef S
56110 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 0a 23 20  QLITE_OS_UNIX.# 
56120 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
56130 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 20 75 6e  OS_UNIX 0.#   un
56140 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  def SQLITE_OS_WI
56150 4e 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c  N.#   define SQL
56160 49 54 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 20  ITE_OS_WIN 0.#  
56170 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53   undef SQLITE_OS
56180 5f 4f 53 32 0a 23 20 20 20 64 65 66 69 6e 65 20  _OS2.#   define 
56190 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 30 0a  SQLITE_OS_OS2 0.
561a0 23 20 65 6c 73 65 0a 23 20 20 20 75 6e 64 65 66  # else.#   undef
561b0 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52   SQLITE_OS_OTHER
561c0 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a  .# endif.#endif.
561d0 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
561e0 49 54 45 5f 4f 53 5f 55 4e 49 58 29 20 26 26 20  ITE_OS_UNIX) && 
561f0 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
56200 4f 53 5f 4f 54 48 45 52 29 0a 23 20 64 65 66 69  OS_OTHER).# defi
56210 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48  ne SQLITE_OS_OTH
56220 45 52 20 30 0a 23 20 69 66 6e 64 65 66 20 53 51  ER 0.# ifndef SQ
56230 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 20  LITE_OS_WIN.#   
56240 69 66 20 64 65 66 69 6e 65 64 28 5f 57 49 4e 33  if defined(_WIN3
56250 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 57 49  2) || defined(WI
56260 4e 33 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  N32) || defined(
56270 5f 5f 43 59 47 57 49 4e 5f 5f 29 20 7c 7c 20 64  __CYGWIN__) || d
56280 65 66 69 6e 65 64 28 5f 5f 4d 49 4e 47 57 33 32  efined(__MINGW32
56290 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f  __) || defined(_
562a0 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a 23 20 20  _BORLANDC__).#  
562b0 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45     define SQLITE
562c0 5f 4f 53 5f 57 49 4e 20 31 0a 23 20 20 20 20 20  _OS_WIN 1.#     
562d0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53  define SQLITE_OS
562e0 5f 55 4e 49 58 20 30 0a 23 20 20 20 20 20 64 65  _UNIX 0.#     de
562f0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f  fine SQLITE_OS_O
56300 53 32 20 30 0a 23 20 20 20 65 6c 69 66 20 64 65  S2 0.#   elif de
56310 66 69 6e 65 64 28 5f 5f 45 4d 58 5f 5f 29 20 7c  fined(__EMX__) |
56320 7c 20 64 65 66 69 6e 65 64 28 5f 4f 53 32 29 20  | defined(_OS2) 
56330 7c 7c 20 64 65 66 69 6e 65 64 28 4f 53 32 29 20  || defined(OS2) 
56340 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4f 53 32 5f  || defined(_OS2_
56350 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 4f  ) || defined(__O
56360 53 32 5f 5f 29 0a 23 20 20 20 20 20 64 65 66 69  S2__).#     defi
56370 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  ne SQLITE_OS_WIN
56380 20 30 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20   0.#     define 
56390 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 30  SQLITE_OS_UNIX 0
563a0 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 53 51  .#     define SQ
563b0 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 31 0a 23 20  LITE_OS_OS2 1.# 
563c0 20 20 65 6c 73 65 0a 23 20 20 20 20 20 64 65 66    else.#     def
563d0 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  ine SQLITE_OS_WI
563e0 4e 20 30 0a 23 20 20 20 20 20 64 65 66 69 6e 65  N 0.#     define
563f0 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20   SQLITE_OS_UNIX 
56400 31 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 53  1.#     define S
56410 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 30 0a 23  QLITE_OS_OS2 0.#
56420 20 20 65 6e 64 69 66 0a 23 20 65 6c 73 65 0a 23    endif.# else.#
56430 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
56440 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 64 65 66  OS_UNIX 0.#  def
56450 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53  ine SQLITE_OS_OS
56460 32 20 30 0a 23 20 65 6e 64 69 66 0a 23 65 6c 73  2 0.# endif.#els
56470 65 0a 23 20 69 66 6e 64 65 66 20 53 51 4c 49 54  e.# ifndef SQLIT
56480 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 64 65 66 69  E_OS_WIN.#  defi
56490 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  ne SQLITE_OS_WIN
564a0 20 30 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69   0.# endif.#endi
564b0 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d 69  f../*.** Determi
564c0 6e 65 20 69 66 20 77 65 20 61 72 65 20 64 65 61  ne if we are dea
564d0 6c 69 6e 67 20 77 69 74 68 20 57 69 6e 64 6f 77  ling with Window
564e0 73 43 45 20 2d 20 77 68 69 63 68 20 68 61 73 20  sCE - which has 
564f0 61 20 6d 75 63 68 0a 2a 2a 20 72 65 64 75 63 65  a much.** reduce
56500 64 20 41 50 49 2e 0a 2a 2f 0a 23 69 66 20 64 65  d API..*/.#if de
56510 66 69 6e 65 64 28 5f 57 49 4e 33 32 5f 57 43 45  fined(_WIN32_WCE
56520 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  ).# define SQLIT
56530 45 5f 4f 53 5f 57 49 4e 43 45 20 31 0a 23 65 6c  E_OS_WINCE 1.#el
56540 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  se.# define SQLI
56550 54 45 5f 4f 53 5f 57 49 4e 43 45 20 30 0a 23 65  TE_OS_WINCE 0.#e
56560 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 66  ndif.../*.** Def
56570 69 6e 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20  ine the maximum 
56580 73 69 7a 65 20 6f 66 20 61 20 74 65 6d 70 6f 72  size of a tempor
56590 61 72 79 20 66 69 6c 65 6e 61 6d 65 0a 2a 2f 0a  ary filename.*/.
565a0 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
565b0 4e 0a 23 20 69 6e 63 6c 75 64 65 20 3c 77 69 6e  N.# include <win
565c0 64 6f 77 73 2e 68 3e 0a 23 20 64 65 66 69 6e 65  dows.h>.# define
565d0 20 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45   SQLITE_TEMPNAME
565e0 5f 53 49 5a 45 20 28 4d 41 58 5f 50 41 54 48 2b  _SIZE (MAX_PATH+
565f0 35 30 29 0a 23 65 6c 69 66 20 53 51 4c 49 54 45  50).#elif SQLITE
56600 5f 4f 53 5f 4f 53 32 0a 23 20 69 66 20 28 5f 5f  _OS_OS2.# if (__
56610 47 4e 55 43 5f 5f 20 3e 20 33 20 7c 7c 20 5f 5f  GNUC__ > 3 || __
56620 47 4e 55 43 5f 5f 20 3d 3d 20 33 20 26 26 20 5f  GNUC__ == 3 && _
56630 5f 47 4e 55 43 5f 4d 49 4e 4f 52 5f 5f 20 3e 3d  _GNUC_MINOR__ >=
56640 20 33 29 20 26 26 20 64 65 66 69 6e 65 64 28 4f   3) && defined(O
56650 53 32 5f 48 49 47 48 5f 4d 45 4d 4f 52 59 29 0a  S2_HIGH_MEMORY).
56660 23 20 20 69 6e 63 6c 75 64 65 20 3c 6f 73 32 73  #  include <os2s
56670 61 66 65 2e 68 3e 20 2f 2a 20 68 61 73 20 74 6f  afe.h> /* has to
56680 20 62 65 20 69 6e 63 6c 75 64 65 64 20 62 65 66   be included bef
56690 6f 72 65 20 6f 73 32 2e 68 20 66 6f 72 20 6c 69  ore os2.h for li
566a0 6e 6b 69 6e 67 20 74 6f 20 77 6f 72 6b 20 2a 2f  nking to work */
566b0 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69 6e  .# endif.# defin
566c0 65 20 49 4e 43 4c 5f 44 4f 53 44 41 54 45 54 49  e INCL_DOSDATETI
566d0 4d 45 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c  ME.# define INCL
566e0 5f 44 4f 53 46 49 4c 45 4d 47 52 0a 23 20 64 65  _DOSFILEMGR.# de
566f0 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 45 52 52  fine INCL_DOSERR
56700 4f 52 53 0a 23 20 64 65 66 69 6e 65 20 49 4e 43  ORS.# define INC
56710 4c 5f 44 4f 53 4d 49 53 43 0a 23 20 64 65 66 69  L_DOSMISC.# defi
56720 6e 65 20 49 4e 43 4c 5f 44 4f 53 50 52 4f 43 45  ne INCL_DOSPROCE
56730 53 53 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c  SS.# define INCL
56740 5f 44 4f 53 4d 4f 44 55 4c 45 4d 47 52 0a 23 20  _DOSMODULEMGR.# 
56750 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 53  define INCL_DOSS
56760 45 4d 41 50 48 4f 52 45 53 0a 23 20 69 6e 63 6c  EMAPHORES.# incl
56770 75 64 65 20 3c 6f 73 32 2e 68 3e 0a 23 20 69 6e  ude <os2.h>.# in
56780 63 6c 75 64 65 20 3c 75 63 6f 6e 76 2e 68 3e 0a  clude <uconv.h>.
56790 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
567a0 54 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 20 28 43  TEMPNAME_SIZE (C
567b0 43 48 4d 41 58 50 41 54 48 43 4f 4d 50 29 0a 23  CHMAXPATHCOMP).#
567c0 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51  else.# define SQ
567d0 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45 5f 53 49  LITE_TEMPNAME_SI
567e0 5a 45 20 32 30 30 0a 23 65 6e 64 69 66 0a 0a 2f  ZE 200.#endif../
567f0 2a 20 49 66 20 74 68 65 20 53 45 54 5f 46 55 4c  * If the SET_FUL
56800 4c 53 59 4e 43 20 6d 61 63 72 6f 20 69 73 20 6e  LSYNC macro is n
56810 6f 74 20 64 65 66 69 6e 65 64 20 61 62 6f 76 65  ot defined above
56820 2c 20 74 68 65 6e 20 6d 61 6b 65 20 69 74 0a 2a  , then make it.*
56830 2a 20 61 20 6e 6f 2d 6f 70 0a 2a 2f 0a 23 69 66  * a no-op.*/.#if
56840 6e 64 65 66 20 53 45 54 5f 46 55 4c 4c 53 59 4e  ndef SET_FULLSYN
56850 43 0a 23 20 64 65 66 69 6e 65 20 53 45 54 5f 46  C.# define SET_F
56860 55 4c 4c 53 59 4e 43 28 78 2c 79 29 0a 23 65 6e  ULLSYNC(x,y).#en
56870 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 64  dif../*.** The d
56880 65 66 61 75 6c 74 20 73 69 7a 65 20 6f 66 20 61  efault size of a
56890 20 64 69 73 6b 20 73 65 63 74 6f 72 0a 2a 2f 0a   disk sector.*/.
568a0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44  #ifndef SQLITE_D
568b0 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49  EFAULT_SECTOR_SI
568c0 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  ZE.# define SQLI
568d0 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f  TE_DEFAULT_SECTO
568e0 52 5f 53 49 5a 45 20 35 31 32 0a 23 65 6e 64 69  R_SIZE 512.#endi
568f0 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 6d 70 6f 72 61  f../*.** Tempora
56900 72 79 20 66 69 6c 65 73 20 61 72 65 20 6e 61 6d  ry files are nam
56910 65 64 20 73 74 61 72 74 69 6e 67 20 77 69 74 68  ed starting with
56920 20 74 68 69 73 20 70 72 65 66 69 78 20 66 6f 6c   this prefix fol
56930 6c 6f 77 65 64 20 62 79 20 31 36 20 72 61 6e 64  lowed by 16 rand
56940 6f 6d 0a 2a 2a 20 61 6c 70 68 61 6e 75 6d 65 72  om.** alphanumer
56950 69 63 20 63 68 61 72 61 63 74 65 72 73 2c 20 61  ic characters, a
56960 6e 64 20 6e 6f 20 66 69 6c 65 20 65 78 74 65 6e  nd no file exten
56970 73 69 6f 6e 2e 20 54 68 65 79 20 61 72 65 20 73  sion. They are s
56980 74 6f 72 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20  tored in the.** 
56990 4f 53 27 73 20 73 74 61 6e 64 61 72 64 20 74 65  OS's standard te
569a0 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 64 69 72  mporary file dir
569b0 65 63 74 6f 72 79 2c 20 61 6e 64 20 61 72 65 20  ectory, and are 
569c0 64 65 6c 65 74 65 64 20 70 72 69 6f 72 20 74 6f  deleted prior to
569d0 20 65 78 69 74 2e 0a 2a 2a 20 49 66 20 73 71 6c   exit..** If sql
569e0 69 74 65 20 69 73 20 62 65 69 6e 67 20 65 6d 62  ite is being emb
569f0 65 64 64 65 64 20 69 6e 20 61 6e 6f 74 68 65 72  edded in another
56a00 20 70 72 6f 67 72 61 6d 2c 20 79 6f 75 20 6d 61   program, you ma
56a10 79 20 77 69 73 68 20 74 6f 20 63 68 61 6e 67 65  y wish to change
56a20 20 74 68 65 0a 2a 2a 20 70 72 65 66 69 78 20 74   the.** prefix t
56a30 6f 20 72 65 66 6c 65 63 74 20 79 6f 75 72 20 70  o reflect your p
56a40 72 6f 67 72 61 6d 27 73 20 6e 61 6d 65 2c 20 73  rogram's name, s
56a50 6f 20 74 68 61 74 20 69 66 20 79 6f 75 72 20 70  o that if your p
56a60 72 6f 67 72 61 6d 20 65 78 69 74 73 0a 2a 2a 20  rogram exits.** 
56a70 70 72 65 6d 61 74 75 72 65 6c 79 2c 20 6f 6c 64  prematurely, old
56a80 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73   temporary files
56a90 20 63 61 6e 20 62 65 20 65 61 73 69 6c 79 20 69   can be easily i
56aa0 64 65 6e 74 69 66 69 65 64 2e 20 54 68 69 73 20  dentified. This 
56ab0 63 61 6e 20 62 65 20 64 6f 6e 65 0a 2a 2a 20 75  can be done.** u
56ac0 73 69 6e 67 20 2d 44 53 51 4c 49 54 45 5f 54 45  sing -DSQLITE_TE
56ad0 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 3d 6d  MP_FILE_PREFIX=m
56ae0 79 70 72 65 66 69 78 5f 20 6f 6e 20 74 68 65 20  yprefix_ on the 
56af0 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64  compiler command
56b00 20 6c 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 32 30 30   line..**.** 200
56b10 36 2d 31 30 2d 33 31 3a 20 20 54 68 65 20 64 65  6-10-31:  The de
56b20 66 61 75 6c 74 20 70 72 65 66 69 78 20 75 73 65  fault prefix use
56b30 64 20 74 6f 20 62 65 20 22 73 71 6c 69 74 65 5f  d to be "sqlite_
56b40 22 2e 20 20 42 75 74 20 74 68 65 6e 0a 2a 2a 20  ".  But then.** 
56b50 4d 63 61 66 65 65 20 73 74 61 72 74 65 64 20 75  Mcafee started u
56b60 73 69 6e 67 20 53 51 4c 69 74 65 20 69 6e 20 74  sing SQLite in t
56b70 68 65 69 72 20 61 6e 74 69 2d 76 69 72 75 73 20  heir anti-virus 
56b80 70 72 6f 64 75 63 74 20 61 6e 64 20 69 74 0a 2a  product and it.*
56b90 2a 20 73 74 61 72 74 65 64 20 70 75 74 74 69 6e  * started puttin
56ba0 67 20 66 69 6c 65 73 20 77 69 74 68 20 74 68 65  g files with the
56bb0 20 22 73 71 6c 69 74 65 22 20 6e 61 6d 65 20 69   "sqlite" name i
56bc0 6e 20 74 68 65 20 63 3a 2f 74 65 6d 70 20 66 6f  n the c:/temp fo
56bd0 6c 64 65 72 2e 0a 2a 2a 20 54 68 69 73 20 61 6e  lder..** This an
56be0 6e 6f 79 65 64 20 6d 61 6e 79 20 77 69 6e 64 6f  noyed many windo
56bf0 77 73 20 75 73 65 72 73 2e 20 20 54 68 6f 73 65  ws users.  Those
56c00 20 75 73 65 72 73 20 77 6f 75 6c 64 20 74 68 65   users would the
56c10 6e 20 64 6f 20 61 20 0a 2a 2a 20 47 6f 6f 67 6c  n do a .** Googl
56c20 65 20 73 65 61 72 63 68 20 66 6f 72 20 22 73 71  e search for "sq
56c30 6c 69 74 65 22 2c 20 66 69 6e 64 20 74 68 65 20  lite", find the 
56c40 74 65 6c 65 70 68 6f 6e 65 20 6e 75 6d 62 65 72  telephone number
56c50 73 20 6f 66 20 74 68 65 0a 2a 2a 20 64 65 76 65  s of the.** deve
56c60 6c 6f 70 65 72 73 20 61 6e 64 20 63 61 6c 6c 20  lopers and call 
56c70 74 6f 20 77 61 6b 65 20 74 68 65 6d 20 75 70 20  to wake them up 
56c80 61 74 20 6e 69 67 68 74 20 61 6e 64 20 63 6f 6d  at night and com
56c90 70 6c 61 69 6e 2e 0a 2a 2a 20 46 6f 72 20 74 68  plain..** For th
56ca0 69 73 20 72 65 61 73 6f 6e 2c 20 74 68 65 20 64  is reason, the d
56cb0 65 66 61 75 6c 74 20 6e 61 6d 65 20 70 72 65 66  efault name pref
56cc0 69 78 20 69 73 20 63 68 61 6e 67 65 64 20 74 6f  ix is changed to
56cd0 20 62 65 20 22 73 71 6c 69 74 65 22 20 0a 2a 2a   be "sqlite" .**
56ce0 20 73 70 65 6c 6c 65 64 20 62 61 63 6b 77 61 72   spelled backwar
56cf0 64 73 2e 20 20 53 6f 20 74 68 65 20 74 65 6d 70  ds.  So the temp
56d00 20 66 69 6c 65 73 20 61 72 65 20 73 74 69 6c 6c   files are still
56d10 20 69 64 65 6e 74 69 66 69 65 64 2c 20 62 75 74   identified, but
56d20 0a 2a 2a 20 61 6e 79 62 6f 64 79 20 73 6d 61 72  .** anybody smar
56d30 74 20 65 6e 6f 75 67 68 20 74 6f 20 66 69 67 75  t enough to figu
56d40 72 65 20 6f 75 74 20 74 68 65 20 63 6f 64 65 20  re out the code 
56d50 69 73 20 61 6c 73 6f 20 6c 69 6b 65 6c 79 20 73  is also likely s
56d60 6d 61 72 74 0a 2a 2a 20 65 6e 6f 75 67 68 20 74  mart.** enough t
56d70 6f 20 6b 6e 6f 77 20 74 68 61 74 20 63 61 6c 6c  o know that call
56d80 69 6e 67 20 74 68 65 20 64 65 76 65 6c 6f 70 65  ing the develope
56d90 72 20 77 69 6c 6c 20 6e 6f 74 20 68 65 6c 70 20  r will not help 
56da0 67 65 74 20 72 69 64 0a 2a 2a 20 6f 66 20 74 68  get rid.** of th
56db0 65 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66 6e 64  e file..*/.#ifnd
56dc0 65 66 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46  ef SQLITE_TEMP_F
56dd0 49 4c 45 5f 50 52 45 46 49 58 0a 23 20 64 65 66  ILE_PREFIX.# def
56de0 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f  ine SQLITE_TEMP_
56df0 46 49 4c 45 5f 50 52 45 46 49 58 20 22 65 74 69  FILE_PREFIX "eti
56e00 6c 71 73 5f 22 0a 23 65 6e 64 69 66 0a 0a 2f 2a  lqs_".#endif../*
56e10 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
56e20 67 20 76 61 6c 75 65 73 20 6d 61 79 20 62 65 20  g values may be 
56e30 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65  passed as the se
56e40 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f  cond argument to
56e50 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63  .** sqlite3OsLoc
56e60 6b 28 29 2e 20 54 68 65 20 76 61 72 69 6f 75 73  k(). The various
56e70 20 6c 6f 63 6b 73 20 65 78 68 69 62 69 74 20 74   locks exhibit t
56e80 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 6d  he following sem
56e90 61 6e 74 69 63 73 3a 0a 2a 2a 0a 2a 2a 20 53 48  antics:.**.** SH
56ea0 41 52 45 44 3a 20 20 20 20 41 6e 79 20 6e 75 6d  ARED:    Any num
56eb0 62 65 72 20 6f 66 20 70 72 6f 63 65 73 73 65 73  ber of processes
56ec0 20 6d 61 79 20 68 6f 6c 64 20 61 20 53 48 41 52   may hold a SHAR
56ed0 45 44 20 6c 6f 63 6b 20 73 69 6d 75 6c 74 61 6e  ED lock simultan
56ee0 65 6f 75 73 6c 79 2e 0a 2a 2a 20 52 45 53 45 52  eously..** RESER
56ef0 56 45 44 3a 20 20 41 20 73 69 6e 67 6c 65 20 70  VED:  A single p
56f00 72 6f 63 65 73 73 20 6d 61 79 20 68 6f 6c 64 20  rocess may hold 
56f10 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20  a RESERVED lock 
56f20 6f 6e 20 61 20 66 69 6c 65 20 61 74 0a 2a 2a 20  on a file at.** 
56f30 20 20 20 20 20 20 20 20 20 20 20 61 6e 79 20 74             any t
56f40 69 6d 65 2e 20 4f 74 68 65 72 20 70 72 6f 63 65  ime. Other proce
56f50 73 73 65 73 20 6d 61 79 20 68 6f 6c 64 20 61 6e  sses may hold an
56f60 64 20 6f 62 74 61 69 6e 20 6e 65 77 20 53 48 41  d obtain new SHA
56f70 52 45 44 20 6c 6f 63 6b 73 2e 0a 2a 2a 20 50 45  RED locks..** PE
56f80 4e 44 49 4e 47 3a 20 20 20 41 20 73 69 6e 67 6c  NDING:   A singl
56f90 65 20 70 72 6f 63 65 73 73 20 6d 61 79 20 68 6f  e process may ho
56fa0 6c 64 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63  ld a PENDING loc
56fb0 6b 20 6f 6e 20 61 20 66 69 6c 65 20 61 74 0a 2a  k on a file at.*
56fc0 2a 20 20 20 20 20 20 20 20 20 20 20 20 61 6e 79  *            any
56fd0 20 6f 6e 65 20 74 69 6d 65 2e 20 45 78 69 73 74   one time. Exist
56fe0 69 6e 67 20 53 48 41 52 45 44 20 6c 6f 63 6b 73  ing SHARED locks
56ff0 20 6d 61 79 20 70 65 72 73 69 73 74 2c 20 62 75   may persist, bu
57000 74 20 6e 6f 20 6e 65 77 0a 2a 2a 20 20 20 20 20  t no new.**     
57010 20 20 20 20 20 20 20 53 48 41 52 45 44 20 6c 6f         SHARED lo
57020 63 6b 73 20 6d 61 79 20 62 65 20 6f 62 74 61 69  cks may be obtai
57030 6e 65 64 20 62 79 20 6f 74 68 65 72 20 70 72 6f  ned by other pro
57040 63 65 73 73 65 73 2e 0a 2a 2a 20 45 58 43 4c 55  cesses..** EXCLU
57050 53 49 56 45 3a 20 41 6e 20 45 58 43 4c 55 53 49  SIVE: An EXCLUSI
57060 56 45 20 6c 6f 63 6b 20 70 72 65 63 6c 75 64 65  VE lock preclude
57070 73 20 61 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 6b  s all other lock
57080 73 2e 0a 2a 2a 0a 2a 2a 20 50 45 4e 44 49 4e 47  s..**.** PENDING
57090 5f 4c 4f 43 4b 20 6d 61 79 20 6e 6f 74 20 62 65  _LOCK may not be
570a0 20 70 61 73 73 65 64 20 64 69 72 65 63 74 6c 79   passed directly
570b0 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63   to sqlite3OsLoc
570c0 6b 28 29 2e 20 49 6e 73 74 65 61 64 2c 20 61 0a  k(). Instead, a.
570d0 2a 2a 20 70 72 6f 63 65 73 73 20 74 68 61 74 20  ** process that 
570e0 72 65 71 75 65 73 74 73 20 61 6e 20 45 58 43 4c  requests an EXCL
570f0 55 53 49 56 45 20 6c 6f 63 6b 20 6d 61 79 20 61  USIVE lock may a
57100 63 74 75 61 6c 6c 79 20 6f 62 74 61 69 6e 20 61  ctually obtain a
57110 20 50 45 4e 44 49 4e 47 0a 2a 2a 20 6c 6f 63 6b   PENDING.** lock
57120 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 70  . This can be up
57130 67 72 61 64 65 64 20 74 6f 20 61 6e 20 45 58 43  graded to an EXC
57140 4c 55 53 49 56 45 20 6c 6f 63 6b 20 62 79 20 61  LUSIVE lock by a
57150 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c   subsequent call
57160 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73   to.** sqlite3Os
57170 4c 6f 63 6b 28 29 2e 0a 2a 2f 0a 23 64 65 66 69  Lock()..*/.#defi
57180 6e 65 20 4e 4f 5f 4c 4f 43 4b 20 20 20 20 20 20  ne NO_LOCK      
57190 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 48 41     0.#define SHA
571a0 52 45 44 5f 4c 4f 43 4b 20 20 20 20 20 31 0a 23  RED_LOCK     1.#
571b0 64 65 66 69 6e 65 20 52 45 53 45 52 56 45 44 5f  define RESERVED_
571c0 4c 4f 43 4b 20 20 20 32 0a 23 64 65 66 69 6e 65  LOCK   2.#define
571d0 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 20 20   PENDING_LOCK   
571e0 20 33 0a 23 64 65 66 69 6e 65 20 45 58 43 4c 55   3.#define EXCLU
571f0 53 49 56 45 5f 4c 4f 43 4b 20 20 34 0a 0a 2f 2a  SIVE_LOCK  4../*
57200 0a 2a 2a 20 46 69 6c 65 20 4c 6f 63 6b 69 6e 67  .** File Locking
57210 20 4e 6f 74 65 73 3a 20 20 28 4d 6f 73 74 6c 79   Notes:  (Mostly
57220 20 61 62 6f 75 74 20 77 69 6e 64 6f 77 73 20 62   about windows b
57230 75 74 20 61 6c 73 6f 20 73 6f 6d 65 20 69 6e 66  ut also some inf
57240 6f 20 66 6f 72 20 55 6e 69 78 29 0a 2a 2a 0a 2a  o for Unix).**.*
57250 2a 20 57 65 20 63 61 6e 6e 6f 74 20 75 73 65 20  * We cannot use 
57260 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 6f 72 20  LockFileEx() or 
57270 55 6e 6c 6f 63 6b 46 69 6c 65 45 78 28 29 20 6f  UnlockFileEx() o
57280 6e 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20 62 65  n Win95/98/ME be
57290 63 61 75 73 65 0a 2a 2a 20 74 68 6f 73 65 20 66  cause.** those f
572a0 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f 74  unctions are not
572b0 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 53 6f 20   available.  So 
572c0 77 65 20 75 73 65 20 6f 6e 6c 79 20 4c 6f 63 6b  we use only Lock
572d0 46 69 6c 65 28 29 20 61 6e 64 0a 2a 2a 20 55 6e  File() and.** Un
572e0 6c 6f 63 6b 46 69 6c 65 28 29 2e 0a 2a 2a 0a 2a  lockFile()..**.*
572f0 2a 20 4c 6f 63 6b 46 69 6c 65 28 29 20 70 72 65  * LockFile() pre
57300 76 65 6e 74 73 20 6e 6f 74 20 6a 75 73 74 20 77  vents not just w
57310 72 69 74 69 6e 67 20 62 75 74 20 61 6c 73 6f 20  riting but also 
57320 72 65 61 64 69 6e 67 20 62 79 20 6f 74 68 65 72  reading by other
57330 20 70 72 6f 63 65 73 73 65 73 2e 0a 2a 2a 20 41   processes..** A
57340 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 69 73 20   SHARED_LOCK is 
57350 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f 63 6b  obtained by lock
57360 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 72 61 6e  ing a single ran
57370 64 6f 6d 6c 79 2d 63 68 6f 73 65 6e 20 0a 2a 2a  domly-chosen .**
57380 20 62 79 74 65 20 6f 75 74 20 6f 66 20 61 20 73   byte out of a s
57390 70 65 63 69 66 69 63 20 72 61 6e 67 65 20 6f 66  pecific range of
573a0 20 62 79 74 65 73 2e 20 54 68 65 20 6c 6f 63 6b   bytes. The lock
573b0 20 62 79 74 65 20 69 73 20 6f 62 74 61 69 6e 65   byte is obtaine
573c0 64 20 61 74 20 0a 2a 2a 20 72 61 6e 64 6f 6d 20  d at .** random 
573d0 73 6f 20 74 77 6f 20 73 65 70 61 72 61 74 65 20  so two separate 
573e0 72 65 61 64 65 72 73 20 63 61 6e 20 70 72 6f 62  readers can prob
573f0 61 62 6c 79 20 61 63 63 65 73 73 20 74 68 65 20  ably access the 
57400 66 69 6c 65 20 61 74 20 74 68 65 20 0a 2a 2a 20  file at the .** 
57410 73 61 6d 65 20 74 69 6d 65 2c 20 75 6e 6c 65 73  same time, unles
57420 73 20 74 68 65 79 20 61 72 65 20 75 6e 6c 75 63  s they are unluc
57430 6b 79 20 61 6e 64 20 63 68 6f 6f 73 65 20 74 68  ky and choose th
57440 65 20 73 61 6d 65 20 6c 6f 63 6b 20 62 79 74 65  e same lock byte
57450 2e 0a 2a 2a 20 41 6e 20 45 58 43 4c 55 53 49 56  ..** An EXCLUSIV
57460 45 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 61 69 6e  E_LOCK is obtain
57470 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67 20 61 6c  ed by locking al
57480 6c 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 72  l bytes in the r
57490 61 6e 67 65 2e 0a 2a 2a 20 54 68 65 72 65 20 63  ange..** There c
574a0 61 6e 20 6f 6e 6c 79 20 62 65 20 6f 6e 65 20 77  an only be one w
574b0 72 69 74 65 72 2e 20 20 41 20 52 45 53 45 52 56  riter.  A RESERV
574c0 45 44 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 61 69  ED_LOCK is obtai
574d0 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67 0a 2a  ned by locking.*
574e0 2a 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20  * a single byte 
574f0 6f 66 20 74 68 65 20 66 69 6c 65 20 74 68 61 74  of the file that
57500 20 69 73 20 64 65 73 69 67 6e 61 74 65 64 20 61   is designated a
57510 73 20 74 68 65 20 72 65 73 65 72 76 65 64 20 6c  s the reserved l
57520 6f 63 6b 20 62 79 74 65 2e 0a 2a 2a 20 41 20 50  ock byte..** A P
57530 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 69 73 20 6f  ENDING_LOCK is o
57540 62 74 61 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69  btained by locki
57550 6e 67 20 61 20 64 65 73 69 67 6e 61 74 65 64 20  ng a designated 
57560 62 79 74 65 20 64 69 66 66 65 72 65 6e 74 20 66  byte different f
57570 72 6f 6d 0a 2a 2a 20 74 68 65 20 52 45 53 45 52  rom.** the RESER
57580 56 45 44 5f 4c 4f 43 4b 20 62 79 74 65 2e 0a 2a  VED_LOCK byte..*
57590 2a 0a 2a 2a 20 4f 6e 20 57 69 6e 4e 54 2f 32 4b  *.** On WinNT/2K
575a0 2f 58 50 20 73 79 73 74 65 6d 73 2c 20 4c 6f 63  /XP systems, Loc
575b0 6b 46 69 6c 65 45 78 28 29 20 61 6e 64 20 55 6e  kFileEx() and Un
575c0 6c 6f 63 6b 46 69 6c 65 45 78 28 29 20 61 72 65  lockFileEx() are
575d0 20 61 76 61 69 6c 61 62 6c 65 2c 0a 2a 2a 20 77   available,.** w
575e0 68 69 63 68 20 6d 65 61 6e 73 20 77 65 20 63 61  hich means we ca
575f0 6e 20 75 73 65 20 72 65 61 64 65 72 2f 77 72 69  n use reader/wri
57600 74 65 72 20 6c 6f 63 6b 73 2e 20 20 57 68 65 6e  ter locks.  When
57610 20 72 65 61 64 65 72 2f 77 72 69 74 65 72 20 6c   reader/writer l
57620 6f 63 6b 73 0a 2a 2a 20 61 72 65 20 75 73 65 64  ocks.** are used
57630 2c 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 70 6c  , the lock is pl
57640 61 63 65 64 20 6f 6e 20 74 68 65 20 73 61 6d 65  aced on the same
57650 20 72 61 6e 67 65 20 6f 66 20 62 79 74 65 73 20   range of bytes 
57660 74 68 61 74 20 69 73 20 75 73 65 64 0a 2a 2a 20  that is used.** 
57670 66 6f 72 20 70 72 6f 62 61 62 69 6c 69 73 74 69  for probabilisti
57680 63 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 57 69 6e  c locking in Win
57690 39 35 2f 39 38 2f 4d 45 2e 20 20 48 65 6e 63 65  95/98/ME.  Hence
576a0 2c 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 63  , the locking sc
576b0 68 65 6d 65 0a 2a 2a 20 77 69 6c 6c 20 73 75 70  heme.** will sup
576c0 70 6f 72 74 20 74 77 6f 20 6f 72 20 6d 6f 72 65  port two or more
576d0 20 57 69 6e 39 35 20 72 65 61 64 65 72 73 20 6f   Win95 readers o
576e0 72 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 57 69  r two or more Wi
576f0 6e 4e 54 20 72 65 61 64 65 72 73 2e 0a 2a 2a 20  nNT readers..** 
57700 42 75 74 20 61 20 73 69 6e 67 6c 65 20 57 69 6e  But a single Win
57710 39 35 20 72 65 61 64 65 72 20 77 69 6c 6c 20 6c  95 reader will l
57720 6f 63 6b 20 6f 75 74 20 61 6c 6c 20 57 69 6e 4e  ock out all WinN
57730 54 20 72 65 61 64 65 72 73 20 61 6e 64 20 61 20  T readers and a 
57740 73 69 6e 67 6c 65 0a 2a 2a 20 57 69 6e 4e 54 20  single.** WinNT 
57750 72 65 61 64 65 72 20 77 69 6c 6c 20 6c 6f 63 6b  reader will lock
57760 20 6f 75 74 20 61 6c 6c 20 6f 74 68 65 72 20 57   out all other W
57770 69 6e 39 35 20 72 65 61 64 65 72 73 2e 0a 2a 2a  in95 readers..**
57780 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
57790 67 20 23 64 65 66 69 6e 65 73 20 73 70 65 63 69  g #defines speci
577a0 66 79 20 74 68 65 20 72 61 6e 67 65 20 6f 66 20  fy the range of 
577b0 62 79 74 65 73 20 75 73 65 64 20 66 6f 72 20 6c  bytes used for l
577c0 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 53 48 41 52 45  ocking..** SHARE
577d0 44 5f 53 49 5a 45 20 69 73 20 74 68 65 20 6e 75  D_SIZE is the nu
577e0 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 76  mber of bytes av
577f0 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 70  ailable in the p
57800 6f 6f 6c 20 66 72 6f 6d 20 77 68 69 63 68 0a 2a  ool from which.*
57810 2a 20 61 20 72 61 6e 64 6f 6d 20 62 79 74 65 20  * a random byte 
57820 69 73 20 73 65 6c 65 63 74 65 64 20 66 6f 72 20  is selected for 
57830 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e 20 20  a shared lock.  
57840 54 68 65 20 70 6f 6f 6c 20 6f 66 20 62 79 74 65  The pool of byte
57850 73 20 66 6f 72 0a 2a 2a 20 73 68 61 72 65 64 20  s for.** shared 
57860 6c 6f 63 6b 73 20 62 65 67 69 6e 73 20 61 74 20  locks begins at 
57870 53 48 41 52 45 44 5f 46 49 52 53 54 2e 20 0a 2a  SHARED_FIRST. .*
57880 2a 0a 2a 2a 20 54 68 65 20 73 61 6d 65 20 6c 6f  *.** The same lo
57890 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79 20 61  cking strategy a
578a0 6e 64 0a 2a 2a 20 62 79 74 65 20 72 61 6e 67 65  nd.** byte range
578b0 73 20 61 72 65 20 75 73 65 64 20 66 6f 72 20 55  s are used for U
578c0 6e 69 78 2e 20 20 54 68 69 73 20 6c 65 61 76 65  nix.  This leave
578d0 73 20 6f 70 65 6e 20 74 68 65 20 70 6f 73 73 69  s open the possi
578e0 62 6c 69 74 79 20 6f 66 20 68 61 76 69 6e 67 0a  blity of having.
578f0 2a 2a 20 63 6c 69 65 6e 74 73 20 6f 6e 20 77 69  ** clients on wi
57900 6e 39 35 2c 20 77 69 6e 4e 54 2c 20 61 6e 64 20  n95, winNT, and 
57910 75 6e 69 78 20 61 6c 6c 20 74 61 6c 6b 69 6e 67  unix all talking
57920 20 74 6f 20 74 68 65 20 73 61 6d 65 20 73 68 61   to the same sha
57930 72 65 64 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20  red file.** and 
57940 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 63 6f 72 72  all locking corr
57950 65 63 74 6c 79 2e 20 20 54 6f 20 64 6f 20 73 6f  ectly.  To do so
57960 20 77 6f 75 6c 64 20 72 65 71 75 69 72 65 20 74   would require t
57970 68 61 74 20 73 61 6d 62 61 20 28 6f 72 20 77 68  hat samba (or wh
57980 61 74 65 76 65 72 0a 2a 2a 20 74 6f 6f 6c 20 69  atever.** tool i
57990 73 20 62 65 69 6e 67 20 75 73 65 64 20 66 6f 72  s being used for
579a0 20 66 69 6c 65 20 73 68 61 72 69 6e 67 29 20 69   file sharing) i
579b0 6d 70 6c 65 6d 65 6e 74 73 20 6c 6f 63 6b 73 20  mplements locks 
579c0 63 6f 72 72 65 63 74 6c 79 20 62 65 74 77 65 65  correctly betwee
579d0 6e 0a 2a 2a 20 77 69 6e 64 6f 77 73 20 61 6e 64  n.** windows and
579e0 20 75 6e 69 78 2e 20 20 49 27 6d 20 67 75 65 73   unix.  I'm gues
579f0 73 69 6e 67 20 74 68 61 74 20 69 73 6e 27 74 20  sing that isn't 
57a00 6c 69 6b 65 6c 79 20 74 6f 20 68 61 70 70 65 6e  likely to happen
57a10 2c 20 62 75 74 20 62 79 0a 2a 2a 20 75 73 69 6e  , but by.** usin
57a20 67 20 74 68 65 20 73 61 6d 65 20 6c 6f 63 6b 69  g the same locki
57a30 6e 67 20 72 61 6e 67 65 20 77 65 20 61 72 65 20  ng range we are 
57a40 61 74 20 6c 65 61 73 74 20 6f 70 65 6e 20 74 6f  at least open to
57a50 20 74 68 65 20 70 6f 73 73 69 62 69 6c 69 74 79   the possibility
57a60 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 63 6b 69 6e 67 20  ..**.** Locking 
57a70 69 6e 20 77 69 6e 64 6f 77 73 20 69 73 20 6d 61  in windows is ma
57a80 6e 64 69 74 6f 72 79 2e 20 20 46 6f 72 20 74 68  nditory.  For th
57a90 69 73 20 72 65 61 73 6f 6e 2c 20 77 65 20 63 61  is reason, we ca
57aa0 6e 6e 6f 74 20 73 74 6f 72 65 0a 2a 2a 20 61 63  nnot store.** ac
57ab0 74 75 61 6c 20 64 61 74 61 20 69 6e 20 74 68 65  tual data in the
57ac0 20 62 79 74 65 73 20 75 73 65 64 20 66 6f 72 20   bytes used for 
57ad0 6c 6f 63 6b 69 6e 67 2e 20 20 54 68 65 20 70 61  locking.  The pa
57ae0 67 65 72 20 6e 65 76 65 72 20 61 6c 6c 6f 63 61  ger never alloca
57af0 74 65 73 0a 2a 2a 20 74 68 65 20 70 61 67 65 73  tes.** the pages
57b00 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 6c 6f 63   involved in loc
57b10 6b 69 6e 67 20 74 68 65 72 65 66 6f 72 65 2e 20  king therefore. 
57b20 20 53 48 41 52 45 44 5f 53 49 5a 45 20 69 73 20   SHARED_SIZE is 
57b30 73 65 6c 65 63 74 65 64 20 73 6f 0a 2a 2a 20 74  selected so.** t
57b40 68 61 74 20 61 6c 6c 20 6c 6f 63 6b 73 20 77 69  hat all locks wi
57b50 6c 6c 20 66 69 74 20 6f 6e 20 61 20 73 69 6e 67  ll fit on a sing
57b60 6c 65 20 70 61 67 65 20 65 76 65 6e 20 61 74 20  le page even at 
57b70 74 68 65 20 6d 69 6e 69 6d 75 6d 20 70 61 67 65  the minimum page
57b80 20 73 69 7a 65 2e 0a 2a 2a 20 50 45 4e 44 49 4e   size..** PENDIN
57b90 47 5f 42 59 54 45 20 64 65 66 69 6e 65 73 20 74  G_BYTE defines t
57ba0 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
57bb0 74 68 65 20 6c 6f 63 6b 73 2e 20 20 42 79 20 64  the locks.  By d
57bc0 65 66 61 75 6c 74 20 50 45 4e 44 49 4e 47 5f 42  efault PENDING_B
57bd0 59 54 45 0a 2a 2a 20 69 73 20 73 65 74 20 68 69  YTE.** is set hi
57be0 67 68 20 73 6f 20 74 68 61 74 20 77 65 20 64 6f  gh so that we do
57bf0 6e 27 74 20 68 61 76 65 20 74 6f 20 61 6c 6c 6f  n't have to allo
57c00 63 61 74 65 20 61 6e 20 75 6e 75 73 65 64 20 70  cate an unused p
57c10 61 67 65 20 65 78 63 65 70 74 0a 2a 2a 20 66 6f  age except.** fo
57c20 72 20 76 65 72 79 20 6c 61 72 67 65 20 64 61 74  r very large dat
57c30 61 62 61 73 65 73 2e 20 20 42 75 74 20 6f 6e 65  abases.  But one
57c40 20 73 68 6f 75 6c 64 20 74 65 73 74 20 74 68 65   should test the
57c50 20 70 61 67 65 20 73 6b 69 70 70 69 6e 67 20 6c   page skipping l
57c60 6f 67 69 63 20 0a 2a 2a 20 62 79 20 73 65 74 74  ogic .** by sett
57c70 69 6e 67 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  ing PENDING_BYTE
57c80 20 6c 6f 77 20 61 6e 64 20 72 75 6e 6e 69 6e 67   low and running
57c90 20 74 68 65 20 65 6e 74 69 72 65 20 72 65 67 72   the entire regr
57ca0 65 73 73 69 6f 6e 20 73 75 69 74 65 2e 0a 2a 2a  ession suite..**
57cb0 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 68 65  .** Changing the
57cc0 20 76 61 6c 75 65 20 6f 66 20 50 45 4e 44 49 4e   value of PENDIN
57cd0 47 5f 42 59 54 45 20 72 65 73 75 6c 74 73 20 69  G_BYTE results i
57ce0 6e 20 61 20 73 75 62 74 6c 79 20 69 6e 63 6f 6d  n a subtly incom
57cf0 70 61 74 69 62 6c 65 0a 2a 2a 20 66 69 6c 65 20  patible.** file 
57d00 66 6f 72 6d 61 74 2e 20 20 44 65 70 65 6e 64 69  format.  Dependi
57d10 6e 67 20 6f 6e 20 68 6f 77 20 69 74 20 69 73 20  ng on how it is 
57d20 63 68 61 6e 67 65 64 2c 20 79 6f 75 20 6d 69 67  changed, you mig
57d30 68 74 20 6e 6f 74 20 6e 6f 74 69 63 65 0a 2a 2a  ht not notice.**
57d40 20 74 68 65 20 69 6e 63 6f 6d 70 61 74 69 62 69   the incompatibi
57d50 6c 69 74 79 20 72 69 67 68 74 20 61 77 61 79 2c  lity right away,
57d60 20 65 76 65 6e 20 72 75 6e 6e 69 6e 67 20 61 20   even running a 
57d70 66 75 6c 6c 20 72 65 67 72 65 73 73 69 6f 6e 20  full regression 
57d80 74 65 73 74 2e 0a 2a 2a 20 54 68 65 20 64 65 66  test..** The def
57d90 61 75 6c 74 20 6c 6f 63 61 74 69 6f 6e 20 6f 66  ault location of
57da0 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 69 73   PENDING_BYTE is
57db0 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20   the first byte 
57dc0 70 61 73 74 20 74 68 65 0a 2a 2a 20 31 47 42 20  past the.** 1GB 
57dd0 62 6f 75 6e 64 61 72 79 2e 0a 2a 2a 0a 2a 2f 0a  boundary..**.*/.
57de0 23 64 65 66 69 6e 65 20 50 45 4e 44 49 4e 47 5f  #define PENDING_
57df0 42 59 54 45 20 20 20 20 20 20 73 71 6c 69 74 65  BYTE      sqlite
57e00 33 50 65 6e 64 69 6e 67 42 79 74 65 0a 23 64 65  3PendingByte.#de
57e10 66 69 6e 65 20 52 45 53 45 52 56 45 44 5f 42 59  fine RESERVED_BY
57e20 54 45 20 20 20 20 20 28 50 45 4e 44 49 4e 47 5f  TE     (PENDING_
57e30 42 59 54 45 2b 31 29 0a 23 64 65 66 69 6e 65 20  BYTE+1).#define 
57e40 53 48 41 52 45 44 5f 46 49 52 53 54 20 20 20 20  SHARED_FIRST    
57e50 20 20 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b    (PENDING_BYTE+
57e60 32 29 0a 23 64 65 66 69 6e 65 20 53 48 41 52 45  2).#define SHARE
57e70 44 5f 53 49 5a 45 20 20 20 20 20 20 20 35 31 30  D_SIZE       510
57e80 0a 0a 2f 2a 0a 2a 2a 20 57 72 61 70 70 65 72 20  ../*.** Wrapper 
57e90 61 72 6f 75 6e 64 20 4f 53 20 73 70 65 63 69 66  around OS specif
57ea0 69 63 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e  ic sqlite3_os_in
57eb0 69 74 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  it() function..*
57ec0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
57ed0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 49 6e   int sqlite3OsIn
57ee0 69 74 28 76 6f 69 64 29 3b 0a 0a 2f 2a 20 0a 2a  it(void);../* .*
57ef0 2a 20 46 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20  * Functions for 
57f00 61 63 63 65 73 73 69 6e 67 20 73 71 6c 69 74 65  accessing sqlite
57f10 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 20 0a  3_file methods .
57f20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
57f30 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43  E int sqlite3OsC
57f40 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  lose(sqlite3_fil
57f50 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
57f60 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
57f70 73 52 65 61 64 28 73 71 6c 69 74 65 33 5f 66 69  sRead(sqlite3_fi
57f80 6c 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20  le*, void*, int 
57f90 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 65 74 29  amt, i64 offset)
57fa0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
57fb0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 57 72   int sqlite3OsWr
57fc0 69 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ite(sqlite3_file
57fd0 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20  *, const void*, 
57fe0 69 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 66  int amt, i64 off
57ff0 73 65 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  set);.SQLITE_PRI
58000 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
58010 4f 73 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74  OsTruncate(sqlit
58020 65 33 5f 66 69 6c 65 2a 2c 20 69 36 34 20 73 69  e3_file*, i64 si
58030 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  ze);.SQLITE_PRIV
58040 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
58050 73 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69  sSync(sqlite3_fi
58060 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  le*, int);.SQLIT
58070 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
58080 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28  lite3OsFileSize(
58090 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69  sqlite3_file*, i
580a0 36 34 20 2a 70 53 69 7a 65 29 3b 0a 53 51 4c 49  64 *pSize);.SQLI
580b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
580c0 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 73 71 6c  qlite3OsLock(sql
580d0 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29  ite3_file*, int)
580e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
580f0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 55 6e   int sqlite3OsUn
58100 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  lock(sqlite3_fil
58110 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  e*, int);.SQLITE
58120 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
58130 69 74 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72  ite3OsCheckReser
58140 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  vedLock(sqlite3_
58150 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70  file *id, int *p
58160 52 65 73 4f 75 74 29 3b 0a 53 51 4c 49 54 45 5f  ResOut);.SQLITE_
58170 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
58180 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c  te3OsFileControl
58190 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 69  (sqlite3_file*,i
581a0 6e 74 2c 76 6f 69 64 2a 29 3b 0a 23 64 65 66 69  nt,void*);.#defi
581b0 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f  ne SQLITE_FCNTL_
581c0 44 42 5f 55 4e 43 48 41 4e 47 45 44 20 30 78 63  DB_UNCHANGED 0xc
581d0 61 30 39 33 66 61 30 0a 53 51 4c 49 54 45 5f 50  a093fa0.SQLITE_P
581e0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
581f0 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 73  e3OsSectorSize(s
58200 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29  qlite3_file *id)
58210 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
58220 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 65   int sqlite3OsDe
58230 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74  viceCharacterist
58240 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ics(sqlite3_file
58250 20 2a 69 64 29 3b 0a 0a 2f 2a 20 0a 2a 2a 20 46   *id);../* .** F
58260 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 61 63 63  unctions for acc
58270 65 73 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 76  essing sqlite3_v
58280 66 73 20 6d 65 74 68 6f 64 73 20 0a 2a 2f 0a 53  fs methods .*/.S
58290 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
582a0 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28  t sqlite3OsOpen(
582b0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63  sqlite3_vfs *, c
582c0 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 73 71 6c  onst char *, sql
582d0 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 2c  ite3_file*, int,
582e0 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f   int *);.SQLITE_
582f0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
58300 74 65 33 4f 73 44 65 6c 65 74 65 28 73 71 6c 69  te3OsDelete(sqli
58310 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74  te3_vfs *, const
58320 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 53   char *, int);.S
58330 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
58340 74 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73  t sqlite3OsAcces
58350 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c  s(sqlite3_vfs *,
58360 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69   const char *, i
58370 6e 74 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74  nt, int *pResOut
58380 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
58390 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46  E int sqlite3OsF
583a0 75 6c 6c 50 61 74 68 6e 61 6d 65 28 73 71 6c 69  ullPathname(sqli
583b0 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74  te3_vfs *, const
583c0 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 63 68   char *, int, ch
583d0 61 72 20 2a 29 3b 0a 23 69 66 6e 64 65 66 20 53  ar *);.#ifndef S
583e0 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f  QLITE_OMIT_LOAD_
583f0 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45  EXTENSION.SQLITE
58400 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
58410 71 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28 73  qlite3OsDlOpen(s
58420 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f  qlite3_vfs *, co
58430 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c  nst char *);.SQL
58440 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
58450 20 73 71 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f   sqlite3OsDlErro
58460 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c  r(sqlite3_vfs *,
58470 20 69 6e 74 2c 20 63 68 61 72 20 2a 29 3b 0a 53   int, char *);.S
58480 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
58490 69 64 20 28 2a 73 71 6c 69 74 65 33 4f 73 44 6c  id (*sqlite3OsDl
584a0 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 20  Sym(sqlite3_vfs 
584b0 2a 2c 20 76 6f 69 64 20 2a 2c 20 63 6f 6e 73 74  *, void *, const
584c0 20 63 68 61 72 20 2a 29 29 28 76 6f 69 64 29 3b   char *))(void);
584d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
584e0 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 73 44 6c  void sqlite3OsDl
584f0 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66  Close(sqlite3_vf
58500 73 20 2a 2c 20 76 6f 69 64 20 2a 29 3b 0a 23 65  s *, void *);.#e
58510 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
58520 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49  MIT_LOAD_EXTENSI
58530 4f 4e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  ON */.SQLITE_PRI
58540 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
58550 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c  OsRandomness(sql
58560 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74 2c  ite3_vfs *, int,
58570 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45   char *);.SQLITE
58580 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
58590 69 74 65 33 4f 73 53 6c 65 65 70 28 73 71 6c 69  ite3OsSleep(sqli
585a0 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74 29 3b  te3_vfs *, int);
585b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
585c0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 75 72  int sqlite3OsCur
585d0 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 65 33  rentTime(sqlite3
585e0 5f 76 66 73 20 2a 2c 20 64 6f 75 62 6c 65 2a 29  _vfs *, double*)
585f0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 6e 69  ;../*.** Conveni
58600 65 6e 63 65 20 66 75 6e 63 74 69 6f 6e 73 20 66  ence functions f
58610 6f 72 20 6f 70 65 6e 69 6e 67 20 61 6e 64 20 63  or opening and c
58620 6c 6f 73 69 6e 67 20 66 69 6c 65 73 20 75 73 69  losing files usi
58630 6e 67 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d  ng .** sqlite3_m
58640 61 6c 6c 6f 63 28 29 20 74 6f 20 6f 62 74 61 69  alloc() to obtai
58650 6e 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 20  n space for the 
58660 66 69 6c 65 2d 68 61 6e 64 6c 65 20 73 74 72 75  file-handle stru
58670 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  cture..*/.SQLITE
58680 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
58690 69 74 65 33 4f 73 4f 70 65 6e 4d 61 6c 6c 6f 63  ite3OsOpenMalloc
586a0 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20  (sqlite3_vfs *, 
586b0 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 73 71  const char *, sq
586c0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2a 2c 20 69  lite3_file **, i
586d0 6e 74 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45  nt,int*);.SQLITE
586e0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
586f0 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28  ite3OsCloseFree(
58700 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 3b  sqlite3_file *);
58710 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 53 51 4c  ..#endif /* _SQL
58720 49 54 45 5f 4f 53 5f 48 5f 20 2a 2f 0a 0a 2f 2a  ITE_OS_H_ */../*
58730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
58740 64 20 6f 66 20 6f 73 2e 68 20 2a 2a 2a 2a 2a 2a  d of os.h ******
58750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
58780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f  ************* Co
58790 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77  ntinuing where w
587a0 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71  e left off in sq
587b0 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a  liteInt.h ******
587c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
587d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
587e0 63 6c 75 64 65 20 6d 75 74 65 78 2e 68 20 69 6e  clude mutex.h in
587f0 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73   the middle of s
58800 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a  qliteInt.h *****
58810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
58820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
58830 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 2e 68  gin file mutex.h
58840 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
58850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
58870 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 20  .** 2007 August 
58880 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  28.**.** The aut
58890 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
588a0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
588b0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
588c0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
588d0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
588e0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
588f0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
58900 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
58910 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
58920 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
58930 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
58940 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
58950 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
58960 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
58970 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
58980 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
58990 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
589a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
589b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
589c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
589d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
589e0 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  *.**.** This fil
589f0 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63  e contains the c
58a00 6f 6d 6d 6f 6e 20 68 65 61 64 65 72 20 66 6f 72  ommon header for
58a10 20 61 6c 6c 20 6d 75 74 65 78 20 69 6d 70 6c 65   all mutex imple
58a20 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 20 54  mentations..** T
58a30 68 65 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 68  he sqliteInt.h h
58a40 65 61 64 65 72 20 23 69 6e 63 6c 75 64 65 73 20  eader #includes 
58a50 74 68 69 73 20 66 69 6c 65 20 73 6f 20 74 68 61  this file so tha
58a60 74 20 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c  t it is availabl
58a70 65 0a 2a 2a 20 74 6f 20 61 6c 6c 20 73 6f 75 72  e.** to all sour
58a80 63 65 20 66 69 6c 65 73 2e 20 20 57 65 20 62 72  ce files.  We br
58a90 65 61 6b 20 69 74 20 6f 75 74 20 69 6e 20 61 6e  eak it out in an
58aa0 20 65 66 66 6f 72 74 20 74 6f 20 6b 65 65 70 20   effort to keep 
58ab0 74 68 65 20 63 6f 64 65 0a 2a 2a 20 62 65 74 74  the code.** bett
58ac0 65 72 20 6f 72 67 61 6e 69 7a 65 64 2e 0a 2a 2a  er organized..**
58ad0 0a 2a 2a 20 4e 4f 54 45 3a 20 20 73 6f 75 72 63  .** NOTE:  sourc
58ae0 65 20 66 69 6c 65 73 20 73 68 6f 75 6c 64 20 2a  e files should *
58af0 6e 6f 74 2a 20 23 69 6e 63 6c 75 64 65 20 74 68  not* #include th
58b00 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 64  is header file d
58b10 69 72 65 63 74 6c 79 2e 0a 2a 2a 20 53 6f 75 72  irectly..** Sour
58b20 63 65 20 66 69 6c 65 73 20 73 68 6f 75 6c 64 20  ce files should 
58b30 23 69 6e 63 6c 75 64 65 20 74 68 65 20 73 71 6c  #include the sql
58b40 69 74 65 49 6e 74 2e 68 20 66 69 6c 65 20 61 6e  iteInt.h file an
58b50 64 20 6c 65 74 20 74 68 61 74 20 66 69 6c 65 0a  d let that file.
58b60 2a 2a 20 69 6e 63 6c 75 64 65 20 74 68 69 73 20  ** include this 
58b70 6f 6e 65 20 69 6e 64 69 72 65 63 74 6c 79 2e 0a  one indirectly..
58b80 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78  **.** $Id: mutex
58b90 2e 68 2c 76 20 31 2e 39 20 32 30 30 38 2f 31 30  .h,v 1.9 2008/10
58ba0 2f 30 37 20 31 35 3a 32 35 3a 34 38 20 64 72 68  /07 15:25:48 drh
58bb0 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a   Exp $.*/.../*.*
58bc0 2a 20 46 69 67 75 72 65 20 6f 75 74 20 77 68 61  * Figure out wha
58bd0 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65  t version of the
58be0 20 63 6f 64 65 20 74 6f 20 75 73 65 2e 20 20 54   code to use.  T
58bf0 68 65 20 63 68 6f 69 63 65 73 20 61 72 65 0a 2a  he choices are.*
58c00 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55  *.**   SQLITE_MU
58c10 54 45 58 5f 4f 4d 49 54 20 20 20 20 20 20 20 20  TEX_OMIT        
58c20 20 4e 6f 20 6d 75 74 65 78 20 6c 6f 67 69 63 2e   No mutex logic.
58c30 20 20 4e 6f 74 20 65 76 65 6e 20 73 74 75 62 73    Not even stubs
58c40 2e 20 20 54 68 65 0a 2a 2a 20 20 20 20 20 20 20  .  The.**       
58c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58c60 20 20 20 20 20 20 6d 75 74 65 78 65 73 20 69 6d        mutexes im
58c70 70 6c 65 6d 65 6e 74 69 6f 6e 20 63 61 6e 6e 6f  plemention canno
58c80 74 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e 0a  t be overridden.
58c90 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
58ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61                 a
58cb0 74 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a  t start-time..**
58cc0 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54  .**   SQLITE_MUT
58cd0 45 58 5f 4e 4f 4f 50 20 20 20 20 20 20 20 20 20  EX_NOOP         
58ce0 46 6f 72 20 73 69 6e 67 6c 65 2d 74 68 72 65 61  For single-threa
58cf0 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73  ded applications
58d00 2e 20 20 4e 6f 0a 2a 2a 20 20 20 20 20 20 20 20  .  No.**        
58d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58d20 20 20 20 20 20 6d 75 74 75 61 6c 20 65 78 63 6c       mutual excl
58d30 75 73 69 6f 6e 20 69 73 20 70 72 6f 76 69 64 65  usion is provide
58d40 64 2e 20 20 42 75 74 20 74 68 69 73 0a 2a 2a 20  d.  But this.** 
58d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58d60 20 20 20 20 20 20 20 20 20 20 20 20 69 6d 70 6c              impl
58d70 65 6d 65 6e 74 61 74 69 6f 6e 20 63 61 6e 20 62  ementation can b
58d80 65 20 6f 76 65 72 72 69 64 64 65 6e 20 61 74 0a  e overridden at.
58d90 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
58da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
58db0 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a  tart-time..**.**
58dc0 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f     SQLITE_MUTEX_
58dd0 50 54 48 52 45 41 44 53 20 20 20 20 20 46 6f 72  PTHREADS     For
58de0 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20   multi-threaded 
58df0 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 6f 6e 20  applications on 
58e00 55 6e 69 78 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51  Unix..**.**   SQ
58e10 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 32 20 20  LITE_MUTEX_W32  
58e20 20 20 20 20 20 20 20 20 46 6f 72 20 6d 75 6c 74          For mult
58e30 69 2d 74 68 72 65 61 64 65 64 20 61 70 70 6c 69  i-threaded appli
58e40 63 61 74 69 6f 6e 73 20 6f 6e 20 57 69 6e 33 32  cations on Win32
58e50 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45  ..**.**   SQLITE
58e60 5f 4d 55 54 45 58 5f 4f 53 32 20 20 20 20 20 20  _MUTEX_OS2      
58e70 20 20 20 20 46 6f 72 20 6d 75 6c 74 69 2d 74 68      For multi-th
58e80 72 65 61 64 65 64 20 61 70 70 6c 69 63 61 74 69  readed applicati
58e90 6f 6e 73 20 6f 6e 20 4f 53 2f 32 2e 0a 2a 2f 0a  ons on OS/2..*/.
58ea0 23 69 66 20 21 53 51 4c 49 54 45 5f 54 48 52 45  #if !SQLITE_THRE
58eb0 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e 65 20  ADSAFE.# define 
58ec0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49  SQLITE_MUTEX_OMI
58ed0 54 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c  T.#endif.#if SQL
58ee0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26  ITE_THREADSAFE &
58ef0 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  & !defined(SQLIT
58f00 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 0a 23 20  E_MUTEX_NOOP).# 
58f10 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e   if SQLITE_OS_UN
58f20 49 58 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53  IX.#    define S
58f30 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52  QLITE_MUTEX_PTHR
58f40 45 41 44 53 0a 23 20 20 65 6c 69 66 20 53 51 4c  EADS.#  elif SQL
58f50 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 20 20  ITE_OS_WIN.#    
58f60 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55  define SQLITE_MU
58f70 54 45 58 5f 57 33 32 0a 23 20 20 65 6c 69 66 20  TEX_W32.#  elif 
58f80 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a 23 20  SQLITE_OS_OS2.# 
58f90 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45     define SQLITE
58fa0 5f 4d 55 54 45 58 5f 4f 53 32 0a 23 20 20 65 6c  _MUTEX_OS2.#  el
58fb0 73 65 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53  se.#    define S
58fc0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50  QLITE_MUTEX_NOOP
58fd0 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 66  .#  endif.#endif
58fe0 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
58ff0 4d 55 54 45 58 5f 4f 4d 49 54 0a 2f 2a 0a 2a 2a  MUTEX_OMIT./*.**
59000 20 49 66 20 74 68 69 73 20 69 73 20 61 20 6e 6f   If this is a no
59010 2d 6f 70 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  -op implementati
59020 6f 6e 2c 20 69 6d 70 6c 65 6d 65 6e 74 20 65 76  on, implement ev
59030 65 72 79 74 68 69 6e 67 20 61 73 20 6d 61 63 72  erything as macr
59040 6f 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 73  os..*/.#define s
59050 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
59060 6f 63 28 58 29 20 20 20 20 28 28 73 71 6c 69 74  oc(X)    ((sqlit
59070 65 33 5f 6d 75 74 65 78 2a 29 38 29 0a 23 64 65  e3_mutex*)8).#de
59080 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74  fine sqlite3_mut
59090 65 78 5f 66 72 65 65 28 58 29 0a 23 64 65 66 69  ex_free(X).#defi
590a0 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ne sqlite3_mutex
590b0 5f 65 6e 74 65 72 28 58 29 0a 23 64 65 66 69 6e  _enter(X).#defin
590c0 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
590d0 74 72 79 28 58 29 20 20 20 20 20 20 53 51 4c 49  try(X)      SQLI
590e0 54 45 5f 4f 4b 0a 23 64 65 66 69 6e 65 20 73 71  TE_OK.#define sq
590f0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
59100 65 28 58 29 0a 23 64 65 66 69 6e 65 20 73 71 6c  e(X).#define sql
59110 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
59120 58 29 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65  X)     1.#define
59130 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e   sqlite3_mutex_n
59140 6f 74 68 65 6c 64 28 58 29 20 20 31 0a 23 64 65  otheld(X)  1.#de
59150 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 75 74 65  fine sqlite3Mute
59160 78 41 6c 6c 6f 63 28 58 29 20 20 20 20 20 20 28  xAlloc(X)      (
59170 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29  (sqlite3_mutex*)
59180 38 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  8).#define sqlit
59190 65 33 4d 75 74 65 78 49 6e 69 74 28 29 20 20 20  e3MutexInit()   
591a0 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 23       SQLITE_OK.#
591b0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 75  define sqlite3Mu
591c0 74 65 78 45 6e 64 28 29 0a 23 65 6e 64 69 66 20  texEnd().#endif 
591d0 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  /* defined(SQLIT
591e0 45 5f 4f 4d 49 54 5f 4d 55 54 45 58 29 20 2a 2f  E_OMIT_MUTEX) */
591f0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
59200 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 2e 68  * End of mutex.h
59210 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
59220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59240 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
59250 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
59260 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
59270 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a  n sqliteInt.h **
59280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59290 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 64  /.../*.** Each d
592a0 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20  atabase file to 
592b0 62 65 20 61 63 63 65 73 73 65 64 20 62 79 20 74  be accessed by t
592c0 68 65 20 73 79 73 74 65 6d 20 69 73 20 61 6e 20  he system is an 
592d0 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74  instance.** of t
592e0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
592f0 75 63 74 75 72 65 2e 20 20 54 68 65 72 65 20 61  ucture.  There a
59300 72 65 20 6e 6f 72 6d 61 6c 6c 79 20 74 77 6f 20  re normally two 
59310 6f 66 20 74 68 65 73 65 20 73 74 72 75 63 74 75  of these structu
59320 72 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73 71  res.** in the sq
59330 6c 69 74 65 2e 61 44 62 5b 5d 20 61 72 72 61 79  lite.aDb[] array
59340 2e 20 20 61 44 62 5b 30 5d 20 69 73 20 74 68 65  .  aDb[0] is the
59350 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66   main database f
59360 69 6c 65 20 61 6e 64 0a 2a 2a 20 61 44 62 5b 31  ile and.** aDb[1
59370 5d 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73  ] is the databas
59380 65 20 66 69 6c 65 20 75 73 65 64 20 74 6f 20 68  e file used to h
59390 6f 6c 64 20 74 65 6d 70 6f 72 61 72 79 20 74 61  old temporary ta
593a0 62 6c 65 73 2e 20 20 41 64 64 69 74 69 6f 6e 61  bles.  Additiona
593b0 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65 73 20 6d  l.** databases m
593c0 61 79 20 62 65 20 61 74 74 61 63 68 65 64 2e 0a  ay be attached..
593d0 2a 2f 0a 73 74 72 75 63 74 20 44 62 20 7b 0a 20  */.struct Db {. 
593e0 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20   char *zName;   
593f0 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
59400 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 2a   this database *
59410 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b 20  /.  Btree *pBt; 
59420 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
59430 42 2a 54 72 65 65 20 73 74 72 75 63 74 75 72 65  B*Tree structure
59440 20 66 6f 72 20 74 68 69 73 20 64 61 74 61 62 61   for this databa
59450 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20  se file */.  u8 
59460 69 6e 54 72 61 6e 73 3b 20 20 20 20 20 20 20 20  inTrans;        
59470 20 20 2f 2a 20 30 3a 20 6e 6f 74 20 77 72 69 74    /* 0: not writ
59480 61 62 6c 65 2e 20 20 31 3a 20 54 72 61 6e 73 61  able.  1: Transa
59490 63 74 69 6f 6e 2e 20 20 32 3a 20 43 68 65 63 6b  ction.  2: Check
594a0 70 6f 69 6e 74 20 2a 2f 0a 20 20 75 38 20 73 61  point */.  u8 sa
594b0 66 65 74 79 5f 6c 65 76 65 6c 3b 20 20 20 20 20  fety_level;     
594c0 2f 2a 20 48 6f 77 20 61 67 67 72 65 73 73 69 76  /* How aggressiv
594d0 65 20 61 74 20 73 79 6e 63 69 6e 67 20 64 61 74  e at syncing dat
594e0 61 20 74 6f 20 64 69 73 6b 20 2a 2f 0a 20 20 53  a to disk */.  S
594f0 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20  chema *pSchema; 
59500 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
59510 6f 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  o database schem
59520 61 20 28 70 6f 73 73 69 62 6c 79 20 73 68 61 72  a (possibly shar
59530 65 64 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ed) */.};../*.**
59540 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
59550 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
59560 72 75 63 74 75 72 65 20 73 74 6f 72 65 73 20 61  ructure stores a
59570 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
59580 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65  ..**.** If there
59590 20 61 72 65 20 6e 6f 20 76 69 72 74 75 61 6c 20   are no virtual 
595a0 74 61 62 6c 65 73 20 63 6f 6e 66 69 67 75 72 65  tables configure
595b0 64 20 69 6e 20 74 68 69 73 20 73 63 68 65 6d 61  d in this schema
595c0 2c 20 74 68 65 0a 2a 2a 20 53 63 68 65 6d 61 2e  , the.** Schema.
595d0 64 62 20 76 61 72 69 61 62 6c 65 20 69 73 20 73  db variable is s
595e0 65 74 20 74 6f 20 4e 55 4c 4c 2e 20 41 66 74 65  et to NULL. Afte
595f0 72 20 74 68 65 20 66 69 72 73 74 20 76 69 72 74  r the first virt
59600 75 61 6c 20 74 61 62 6c 65 0a 2a 2a 20 68 61 73  ual table.** has
59610 20 62 65 65 6e 20 61 64 64 65 64 2c 20 69 74 20   been added, it 
59620 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20  is set to point 
59630 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
59640 63 6f 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a 20 75  connection .** u
59650 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 74 68  sed to create th
59660 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 4f 6e  e connection. On
59670 63 65 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  ce a virtual tab
59680 6c 65 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 61  le has been.** a
59690 64 64 65 64 20 74 6f 20 74 68 65 20 53 63 68 65  dded to the Sche
596a0 6d 61 20 73 74 72 75 63 74 75 72 65 20 61 6e 64  ma structure and
596b0 20 74 68 65 20 53 63 68 65 6d 61 2e 64 62 20 76   the Schema.db v
596c0 61 72 69 61 62 6c 65 20 70 6f 70 75 6c 61 74 65  ariable populate
596d0 64 2c 20 0a 2a 2a 20 6f 6e 6c 79 20 74 68 61 74  d, .** only that
596e0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
596f0 74 69 6f 6e 20 6d 61 79 20 75 73 65 20 74 68 65  tion may use the
59700 20 53 63 68 65 6d 61 20 74 6f 20 70 72 65 70 61   Schema to prepa
59710 72 65 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  re .** statement
59720 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 63 68  s..*/.struct Sch
59730 65 6d 61 20 7b 0a 20 20 69 6e 74 20 73 63 68 65  ema {.  int sche
59740 6d 61 5f 63 6f 6f 6b 69 65 3b 20 20 20 2f 2a 20  ma_cookie;   /* 
59750 44 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20  Database schema 
59760 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 66  version number f
59770 6f 72 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a  or this file */.
59780 20 20 48 61 73 68 20 74 62 6c 48 61 73 68 3b 20    Hash tblHash; 
59790 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 74 61         /* All ta
597a0 62 6c 65 73 20 69 6e 64 65 78 65 64 20 62 79 20  bles indexed by 
597b0 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68 20 69  name */.  Hash i
597c0 64 78 48 61 73 68 3b 20 20 20 20 20 20 20 20 2f  dxHash;        /
597d0 2a 20 41 6c 6c 20 28 6e 61 6d 65 64 29 20 69 6e  * All (named) in
597e0 64 69 63 65 73 20 69 6e 64 65 78 65 64 20 62 79  dices indexed by
597f0 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68 20   name */.  Hash 
59800 74 72 69 67 48 61 73 68 3b 20 20 20 20 20 20 20  trigHash;       
59810 2f 2a 20 41 6c 6c 20 74 72 69 67 67 65 72 73 20  /* All triggers 
59820 69 6e 64 65 78 65 64 20 62 79 20 6e 61 6d 65 20  indexed by name 
59830 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 53 65 71  */.  Table *pSeq
59840 54 61 62 3b 20 20 20 20 20 20 2f 2a 20 54 68 65  Tab;      /* The
59850 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65   sqlite_sequence
59860 20 74 61 62 6c 65 20 75 73 65 64 20 62 79 20 41   table used by A
59870 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a  UTOINCREMENT */.
59880 20 20 75 38 20 66 69 6c 65 5f 66 6f 72 6d 61 74    u8 file_format
59890 3b 20 20 20 20 20 20 2f 2a 20 53 63 68 65 6d 61  ;      /* Schema
598a0 20 66 6f 72 6d 61 74 20 76 65 72 73 69 6f 6e 20   format version 
598b0 66 6f 72 20 74 68 69 73 20 66 69 6c 65 20 2a 2f  for this file */
598c0 0a 20 20 75 38 20 65 6e 63 3b 20 20 20 20 20 20  .  u8 enc;      
598d0 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20          /* Text 
598e0 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 62 79  encoding used by
598f0 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 2a   this database *
59900 2f 0a 20 20 75 31 36 20 66 6c 61 67 73 3b 20 20  /.  u16 flags;  
59910 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67           /* Flag
59920 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
59930 68 20 74 68 69 73 20 73 63 68 65 6d 61 20 2a 2f  h this schema */
59940 0a 20 20 69 6e 74 20 63 61 63 68 65 5f 73 69 7a  .  int cache_siz
59950 65 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  e;      /* Numbe
59960 72 20 6f 66 20 70 61 67 65 73 20 74 6f 20 75 73  r of pages to us
59970 65 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 2a  e in the cache *
59980 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
59990 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
599a0 4c 45 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  LE.  sqlite3 *db
599b0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 22 4f 77  ;         /* "Ow
599c0 6e 65 72 22 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  ner" connection.
599d0 20 53 65 65 20 63 6f 6d 6d 65 6e 74 20 61 62 6f   See comment abo
599e0 76 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a  ve */.#endif.};.
599f0 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 6d 61 63  ./*.** These mac
59a00 72 6f 73 20 63 61 6e 20 62 65 20 75 73 65 64 20  ros can be used 
59a10 74 6f 20 74 65 73 74 2c 20 73 65 74 2c 20 6f 72  to test, set, or
59a20 20 63 6c 65 61 72 20 62 69 74 73 20 69 6e 20 74   clear bits in t
59a30 68 65 20 0a 2a 2a 20 44 62 2e 66 6c 61 67 73 20  he .** Db.flags 
59a40 66 69 65 6c 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e  field..*/.#defin
59a50 65 20 44 62 48 61 73 50 72 6f 70 65 72 74 79 28  e DbHasProperty(
59a60 44 2c 49 2c 50 29 20 20 20 20 20 28 28 28 44 29  D,I,P)     (((D)
59a70 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d 61  ->aDb[I].pSchema
59a80 2d 3e 66 6c 61 67 73 26 28 50 29 29 3d 3d 28 50  ->flags&(P))==(P
59a90 29 29 0a 23 64 65 66 69 6e 65 20 44 62 48 61 73  )).#define DbHas
59aa0 41 6e 79 50 72 6f 70 65 72 74 79 28 44 2c 49 2c  AnyProperty(D,I,
59ab0 50 29 20 20 28 28 28 44 29 2d 3e 61 44 62 5b 49  P)  (((D)->aDb[I
59ac0 5d 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73  ].pSchema->flags
59ad0 26 28 50 29 29 21 3d 30 29 0a 23 64 65 66 69 6e  &(P))!=0).#defin
59ae0 65 20 44 62 53 65 74 50 72 6f 70 65 72 74 79 28  e DbSetProperty(
59af0 44 2c 49 2c 50 29 20 20 20 20 20 28 44 29 2d 3e  D,I,P)     (D)->
59b00 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d 61 2d 3e  aDb[I].pSchema->
59b10 66 6c 61 67 73 7c 3d 28 50 29 0a 23 64 65 66 69  flags|=(P).#defi
59b20 6e 65 20 44 62 43 6c 65 61 72 50 72 6f 70 65 72  ne DbClearProper
59b30 74 79 28 44 2c 49 2c 50 29 20 20 20 28 44 29 2d  ty(D,I,P)   (D)-
59b40 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d 61 2d  >aDb[I].pSchema-
59b50 3e 66 6c 61 67 73 26 3d 7e 28 50 29 0a 0a 2f 2a  >flags&=~(P)../*
59b60 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75  .** Allowed valu
59b70 65 73 20 66 6f 72 20 74 68 65 20 44 42 2e 66 6c  es for the DB.fl
59b80 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2a 0a 2a 2a  ags field..**.**
59b90 20 54 68 65 20 44 42 5f 53 63 68 65 6d 61 4c 6f   The DB_SchemaLo
59ba0 61 64 65 64 20 66 6c 61 67 20 69 73 20 73 65 74  aded flag is set
59bb0 20 61 66 74 65 72 20 74 68 65 20 64 61 74 61 62   after the datab
59bc0 61 73 65 20 73 63 68 65 6d 61 20 68 61 73 20 62  ase schema has b
59bd0 65 65 6e 0a 2a 2a 20 72 65 61 64 20 69 6e 74 6f  een.** read into
59be0 20 69 6e 74 65 72 6e 61 6c 20 68 61 73 68 20 74   internal hash t
59bf0 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 44 42 5f  ables..**.** DB_
59c00 55 6e 72 65 73 65 74 56 69 65 77 73 20 6d 65 61  UnresetViews mea
59c10 6e 73 20 74 68 61 74 20 6f 6e 65 20 6f 72 20 6d  ns that one or m
59c20 6f 72 65 20 76 69 65 77 73 20 68 61 76 65 20 63  ore views have c
59c30 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 74 68 61 74  olumn names that
59c40 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 66 69  .** have been fi
59c50 6c 6c 65 64 20 6f 75 74 2e 20 20 49 66 20 74 68  lled out.  If th
59c60 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73  e schema changes
59c70 2c 20 74 68 65 73 65 20 63 6f 6c 75 6d 6e 20 6e  , these column n
59c80 61 6d 65 73 20 6d 69 67 68 74 0a 2a 2a 20 63 68  ames might.** ch
59c90 61 6e 67 65 73 20 61 6e 64 20 73 6f 20 74 68 65  anges and so the
59ca0 20 76 69 65 77 20 77 69 6c 6c 20 6e 65 65 64 20   view will need 
59cb0 74 6f 20 62 65 20 72 65 73 65 74 2e 0a 2a 2f 0a  to be reset..*/.
59cc0 23 64 65 66 69 6e 65 20 44 42 5f 53 63 68 65 6d  #define DB_Schem
59cd0 61 4c 6f 61 64 65 64 20 20 20 20 30 78 30 30 30  aLoaded    0x000
59ce0 31 20 20 2f 2a 20 54 68 65 20 73 63 68 65 6d 61  1  /* The schema
59cf0 20 68 61 73 20 62 65 65 6e 20 6c 6f 61 64 65 64   has been loaded
59d00 20 2a 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f 55   */.#define DB_U
59d10 6e 72 65 73 65 74 56 69 65 77 73 20 20 20 20 30  nresetViews    0
59d20 78 30 30 30 32 20 20 2f 2a 20 53 6f 6d 65 20 76  x0002  /* Some v
59d30 69 65 77 73 20 68 61 76 65 20 64 65 66 69 6e 65  iews have define
59d40 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 2a  d column names *
59d50 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f 45 6d 70  /.#define DB_Emp
59d60 74 79 20 20 20 20 20 20 20 20 20 20 20 30 78 30  ty           0x0
59d70 30 30 34 20 20 2f 2a 20 54 68 65 20 66 69 6c 65  004  /* The file
59d80 20 69 73 20 65 6d 70 74 79 20 28 6c 65 6e 67 74   is empty (lengt
59d90 68 20 30 20 62 79 74 65 73 29 20 2a 2f 0a 0a 2f  h 0 bytes) */../
59da0 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20  *.** The number 
59db0 6f 66 20 64 69 66 66 65 72 65 6e 74 20 6b 69 6e  of different kin
59dc0 64 73 20 6f 66 20 74 68 69 6e 67 73 20 74 68 61  ds of things tha
59dd0 74 20 63 61 6e 20 62 65 20 6c 69 6d 69 74 65 64  t can be limited
59de0 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 73 71  .** using the sq
59df0 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29 20 69 6e  lite3_limit() in
59e00 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 23 64 65 66  terface..*/.#def
59e10 69 6e 65 20 53 51 4c 49 54 45 5f 4e 5f 4c 49 4d  ine SQLITE_N_LIM
59e20 49 54 20 28 53 51 4c 49 54 45 5f 4c 49 4d 49 54  IT (SQLITE_LIMIT
59e30 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48 2b 31  _TRIGGER_DEPTH+1
59e40 29 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 6b 61 73 69  )../*.** Lookasi
59e50 64 65 20 6d 61 6c 6c 6f 63 20 69 73 20 61 20 73  de malloc is a s
59e60 65 74 20 6f 66 20 66 69 78 65 64 2d 73 69 7a 65  et of fixed-size
59e70 20 62 75 66 66 65 72 73 20 74 68 61 74 20 63 61   buffers that ca
59e80 6e 20 62 65 20 75 73 65 64 0a 2a 2a 20 74 6f 20  n be used.** to 
59e90 73 61 74 69 73 66 79 20 73 6d 61 6c 6c 20 74 72  satisfy small tr
59ea0 61 6e 73 69 65 6e 74 20 6d 65 6d 6f 72 79 20 61  ansient memory a
59eb0 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 73  llocation reques
59ec0 74 73 20 66 6f 72 20 6f 62 6a 65 63 74 73 0a 2a  ts for objects.*
59ed0 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  * associated wit
59ee0 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64  h a particular d
59ef0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
59f00 6f 6e 2e 20 20 54 68 65 20 75 73 65 20 6f 66 0a  on.  The use of.
59f10 2a 2a 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 61 6c  ** lookaside mal
59f20 6c 6f 63 20 70 72 6f 76 69 64 65 73 20 61 20 73  loc provides a s
59f30 69 67 6e 69 66 69 63 61 6e 74 20 70 65 72 66 6f  ignificant perfo
59f40 72 6d 61 6e 63 65 20 65 6e 68 61 6e 63 65 6d 65  rmance enhanceme
59f50 6e 74 0a 2a 2a 20 28 61 70 70 72 6f 78 20 31 30  nt.** (approx 10
59f60 25 29 20 62 79 20 61 76 6f 69 64 69 6e 67 20 6e  %) by avoiding n
59f70 75 6d 65 72 6f 75 73 20 6d 61 6c 6c 6f 63 2f 66  umerous malloc/f
59f80 72 65 65 20 72 65 71 75 65 73 74 73 20 77 68 69  ree requests whi
59f90 6c 65 20 70 61 72 73 69 6e 67 0a 2a 2a 20 53 51  le parsing.** SQ
59fa0 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a  L statements..**
59fb0 0a 2a 2a 20 54 68 65 20 4c 6f 6f 6b 61 73 69 64  .** The Lookasid
59fc0 65 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64  e structure hold
59fd0 73 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  s configuration 
59fe0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  information abou
59ff0 74 20 74 68 65 0a 2a 2a 20 6c 6f 6f 6b 61 73 69  t the.** lookasi
5a000 64 65 20 6d 61 6c 6c 6f 63 20 73 75 62 73 79 73  de malloc subsys
5a010 74 65 6d 2e 20 20 45 61 63 68 20 61 76 61 69 6c  tem.  Each avail
5a020 61 62 6c 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  able memory allo
5a030 63 61 74 69 6f 6e 20 69 6e 0a 2a 2a 20 74 68 65  cation in.** the
5a040 20 6c 6f 6f 6b 61 73 69 64 65 20 73 75 62 73 79   lookaside subsy
5a050 73 74 65 6d 20 69 73 20 73 74 6f 72 65 64 20 6f  stem is stored o
5a060 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  n a linked list 
5a070 6f 66 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74  of LookasideSlot
5a080 0a 2a 2a 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 0a  .** objects..**.
5a090 2a 2a 20 4c 6f 6f 6b 61 73 69 64 65 20 61 6c 6c  ** Lookaside all
5a0a0 6f 63 61 74 69 6f 6e 73 20 61 72 65 20 6f 6e 6c  ocations are onl
5a0b0 79 20 61 6c 6c 6f 77 65 64 20 66 6f 72 20 6f 62  y allowed for ob
5a0c0 6a 65 63 74 73 20 74 68 61 74 20 61 72 65 20 61  jects that are a
5a0d0 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74  ssociated.** wit
5a0e0 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64  h a particular d
5a0f0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
5a100 6f 6e 2e 20 20 48 65 6e 63 65 2c 20 73 63 68 65  on.  Hence, sche
5a110 6d 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 63  ma information c
5a120 61 6e 6e 6f 74 0a 2a 2a 20 62 65 20 73 74 6f 72  annot.** be stor
5a130 65 64 20 69 6e 20 6c 6f 6f 6b 61 73 69 64 65 20  ed in lookaside 
5a140 62 65 63 61 75 73 65 20 69 6e 20 73 68 61 72 65  because in share
5a150 64 20 63 61 63 68 65 20 6d 6f 64 65 20 74 68 65  d cache mode the
5a160 20 73 63 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74   schema informat
5a170 69 6f 6e 0a 2a 2a 20 69 73 20 73 68 61 72 65 64  ion.** is shared
5a180 20 62 79 20 6d 75 6c 74 69 70 6c 65 20 64 61 74   by multiple dat
5a190 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
5a1a0 73 2e 20 20 54 68 65 72 65 66 6f 72 65 2c 20 77  s.  Therefore, w
5a1b0 68 69 6c 65 20 70 61 72 73 69 6e 67 0a 2a 2a 20  hile parsing.** 
5a1c0 73 63 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74 69  schema informati
5a1d0 6f 6e 2c 20 74 68 65 20 4c 6f 6f 6b 61 73 69 64  on, the Lookasid
5a1e0 65 2e 62 45 6e 61 62 6c 65 64 20 66 6c 61 67 20  e.bEnabled flag 
5a1f0 69 73 20 63 6c 65 61 72 65 64 20 73 6f 20 74 68  is cleared so th
5a200 61 74 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 64 65 20  at.** lookaside 
5a210 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 72 65 20  allocations are 
5a220 6e 6f 74 20 75 73 65 64 20 74 6f 20 63 6f 6e 73  not used to cons
5a230 74 72 75 63 74 20 74 68 65 20 73 63 68 65 6d 61  truct the schema
5a240 20 6f 62 6a 65 63 74 73 2e 0a 2a 2f 0a 73 74 72   objects..*/.str
5a250 75 63 74 20 4c 6f 6f 6b 61 73 69 64 65 20 7b 0a  uct Lookaside {.
5a260 20 20 75 31 36 20 73 7a 3b 20 20 20 20 20 20 20    u16 sz;       
5a270 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
5a280 65 20 6f 66 20 65 61 63 68 20 62 75 66 66 65 72  e of each buffer
5a290 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 75   in bytes */.  u
5a2a0 38 20 62 45 6e 61 62 6c 65 64 3b 20 20 20 20 20  8 bEnabled;     
5a2b0 20 20 20 20 20 20 20 2f 2a 20 46 61 6c 73 65 20         /* False 
5a2c0 74 6f 20 64 69 73 61 62 6c 65 20 6e 65 77 20 6c  to disable new l
5a2d0 6f 6f 6b 61 73 69 64 65 20 61 6c 6c 6f 63 61 74  ookaside allocat
5a2e0 69 6f 6e 73 20 2a 2f 0a 20 20 75 38 20 62 4d 61  ions */.  u8 bMa
5a2f0 6c 6c 6f 63 65 64 3b 20 20 20 20 20 20 20 20 20  lloced;         
5a300 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 53 74    /* True if pSt
5a310 61 72 74 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  art obtained fro
5a320 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  m sqlite3_malloc
5a330 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 75 74  () */.  int nOut
5a340 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
5a350 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 75 66  /* Number of buf
5a360 66 65 72 73 20 63 75 72 72 65 6e 74 6c 79 20 63  fers currently c
5a370 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a 20 20  hecked out */.  
5a380 69 6e 74 20 6d 78 4f 75 74 3b 20 20 20 20 20 20  int mxOut;      
5a390 20 20 20 20 20 20 20 20 2f 2a 20 48 69 67 68 77          /* Highw
5a3a0 61 74 65 72 20 6d 61 72 6b 20 66 6f 72 20 6e 4f  ater mark for nO
5a3b0 75 74 20 2a 2f 0a 20 20 4c 6f 6f 6b 61 73 69 64  ut */.  Lookasid
5a3c0 65 53 6c 6f 74 20 2a 70 46 72 65 65 3b 20 20 20  eSlot *pFree;   
5a3d0 2f 2a 20 4c 69 73 74 20 6f 66 20 61 76 61 69 6c  /* List of avail
5a3e0 61 62 6c 65 20 62 75 66 66 65 72 73 20 2a 2f 0a  able buffers */.
5a3f0 20 20 76 6f 69 64 20 2a 70 53 74 61 72 74 3b 20    void *pStart; 
5a400 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72            /* Fir
5a410 73 74 20 62 79 74 65 20 6f 66 20 61 76 61 69 6c  st byte of avail
5a420 61 62 6c 65 20 6d 65 6d 6f 72 79 20 73 70 61 63  able memory spac
5a430 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 45 6e  e */.  void *pEn
5a440 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  d;             /
5a450 2a 20 46 69 72 73 74 20 62 79 74 65 20 70 61 73  * First byte pas
5a460 74 20 65 6e 64 20 6f 66 20 61 76 61 69 6c 61 62  t end of availab
5a470 6c 65 20 73 70 61 63 65 20 2a 2f 0a 7d 3b 0a 73  le space */.};.s
5a480 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 64 65 53  truct LookasideS
5a490 6c 6f 74 20 7b 0a 20 20 4c 6f 6f 6b 61 73 69 64  lot {.  Lookasid
5a4a0 65 53 6c 6f 74 20 2a 70 4e 65 78 74 3b 20 20 20  eSlot *pNext;   
5a4b0 20 2f 2a 20 4e 65 78 74 20 62 75 66 66 65 72 20   /* Next buffer 
5a4c0 69 6e 20 74 68 65 20 6c 69 73 74 20 6f 66 20 66  in the list of f
5a4d0 72 65 65 20 62 75 66 66 65 72 73 20 2a 2f 0a 7d  ree buffers */.}
5a4e0 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 68 61 73 68 20  ;../*.** A hash 
5a4f0 74 61 62 6c 65 20 66 6f 72 20 66 75 6e 63 74 69  table for functi
5a500 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e 0a  on definitions..
5a510 2a 2a 0a 2a 2a 20 48 61 73 68 20 65 61 63 68 20  **.** Hash each 
5a520 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 75 72  FuncDef structur
5a530 65 20 69 6e 74 6f 20 6f 6e 65 20 6f 66 20 74 68  e into one of th
5a540 65 20 46 75 6e 63 44 65 66 48 61 73 68 2e 61 5b  e FuncDefHash.a[
5a550 5d 20 73 6c 6f 74 73 2e 0a 2a 2a 20 43 6f 6c 6c  ] slots..** Coll
5a560 69 73 69 6f 6e 73 20 61 72 65 20 6f 6e 20 74 68  isions are on th
5a570 65 20 46 75 6e 63 44 65 66 2e 70 48 61 73 68 20  e FuncDef.pHash 
5a580 63 68 61 69 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74  chain..*/.struct
5a590 20 46 75 6e 63 44 65 66 48 61 73 68 20 7b 0a 20   FuncDefHash {. 
5a5a0 20 46 75 6e 63 44 65 66 20 2a 61 5b 32 33 5d 3b   FuncDef *a[23];
5a5b0 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 74         /* Hash t
5a5c0 61 62 6c 65 20 66 6f 72 20 66 75 6e 63 74 69 6f  able for functio
5a5d0 6e 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  ns */.};../*.** 
5a5e0 45 61 63 68 20 64 61 74 61 62 61 73 65 20 69 73  Each database is
5a5f0 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
5a600 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
5a610 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54  ructure..**.** T
5a620 68 65 20 73 71 6c 69 74 65 2e 6c 61 73 74 52 6f  he sqlite.lastRo
5a630 77 69 64 20 72 65 63 6f 72 64 73 20 74 68 65 20  wid records the 
5a640 6c 61 73 74 20 69 6e 73 65 72 74 20 72 6f 77 69  last insert rowi
5a650 64 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 61  d generated by a
5a660 6e 0a 2a 2a 20 69 6e 73 65 72 74 20 73 74 61 74  n.** insert stat
5a670 65 6d 65 6e 74 2e 20 20 49 6e 73 65 72 74 73 20  ement.  Inserts 
5a680 6f 6e 20 76 69 65 77 73 20 64 6f 20 6e 6f 74 20  on views do not 
5a690 61 66 66 65 63 74 20 69 74 73 20 76 61 6c 75 65  affect its value
5a6a0 2e 20 20 45 61 63 68 0a 2a 2a 20 74 72 69 67 67  .  Each.** trigg
5a6b0 65 72 20 68 61 73 20 69 74 73 20 6f 77 6e 20 63  er has its own c
5a6c0 6f 6e 74 65 78 74 2c 20 73 6f 20 74 68 61 74 20  ontext, so that 
5a6d0 6c 61 73 74 52 6f 77 69 64 20 63 61 6e 20 62 65  lastRowid can be
5a6e0 20 75 70 64 61 74 65 64 20 69 6e 73 69 64 65 0a   updated inside.
5a6f0 2a 2a 20 74 72 69 67 67 65 72 73 20 61 73 20 75  ** triggers as u
5a700 73 75 61 6c 2e 20 20 54 68 65 20 70 72 65 76 69  sual.  The previ
5a710 6f 75 73 20 76 61 6c 75 65 20 77 69 6c 6c 20 62  ous value will b
5a720 65 20 72 65 73 74 6f 72 65 64 20 6f 6e 63 65 20  e restored once 
5a730 74 68 65 20 74 72 69 67 67 65 72 0a 2a 2a 20 65  the trigger.** e
5a740 78 69 74 73 2e 20 20 55 70 6f 6e 20 65 6e 74 65  xits.  Upon ente
5a750 72 69 6e 67 20 61 20 62 65 66 6f 72 65 20 6f 72  ring a before or
5a760 20 69 6e 73 74 65 61 64 20 6f 66 20 74 72 69 67   instead of trig
5a770 67 65 72 2c 20 6c 61 73 74 52 6f 77 69 64 20 69  ger, lastRowid i
5a780 73 20 6e 6f 0a 2a 2a 20 6c 6f 6e 67 65 72 20 28  s no.** longer (
5a790 73 69 6e 63 65 20 61 66 74 65 72 20 76 65 72 73  since after vers
5a7a0 69 6f 6e 20 32 2e 38 2e 31 32 29 20 72 65 73 65  ion 2.8.12) rese
5a7b0 74 20 74 6f 20 2d 31 2e 0a 2a 2a 0a 2a 2a 20 54  t to -1..**.** T
5a7c0 68 65 20 73 71 6c 69 74 65 2e 6e 43 68 61 6e 67  he sqlite.nChang
5a7d0 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f 75 6e 74  e does not count
5a7e0 20 63 68 61 6e 67 65 73 20 77 69 74 68 69 6e 20   changes within 
5a7f0 74 72 69 67 67 65 72 73 20 61 6e 64 20 6b 65 65  triggers and kee
5a800 70 73 20 6e 6f 0a 2a 2a 20 63 6f 6e 74 65 78 74  ps no.** context
5a810 2e 20 20 49 74 20 69 73 20 72 65 73 65 74 20 61  .  It is reset a
5a820 74 20 73 74 61 72 74 20 6f 66 20 73 71 6c 69 74  t start of sqlit
5a830 65 33 5f 65 78 65 63 2e 0a 2a 2a 20 54 68 65 20  e3_exec..** The 
5a840 73 71 6c 69 74 65 2e 6c 73 43 68 61 6e 67 65 20  sqlite.lsChange 
5a850 72 65 70 72 65 73 65 6e 74 73 20 74 68 65 20 6e  represents the n
5a860 75 6d 62 65 72 20 6f 66 20 63 68 61 6e 67 65 73  umber of changes
5a870 20 6d 61 64 65 20 62 79 20 74 68 65 20 6c 61 73   made by the las
5a880 74 0a 2a 2a 20 69 6e 73 65 72 74 2c 20 75 70 64  t.** insert, upd
5a890 61 74 65 2c 20 6f 72 20 64 65 6c 65 74 65 20 73  ate, or delete s
5a8a0 74 61 74 65 6d 65 6e 74 2e 20 20 49 74 20 72 65  tatement.  It re
5a8b0 6d 61 69 6e 73 20 63 6f 6e 73 74 61 6e 74 20 74  mains constant t
5a8c0 68 72 6f 75 67 68 6f 75 74 20 74 68 65 0a 2a 2a  hroughout the.**
5a8d0 20 6c 65 6e 67 74 68 20 6f 66 20 61 20 73 74 61   length of a sta
5a8e0 74 65 6d 65 6e 74 20 61 6e 64 20 69 73 20 74 68  tement and is th
5a8f0 65 6e 20 75 70 64 61 74 65 64 20 62 79 20 4f 50  en updated by OP
5a900 5f 53 65 74 43 6f 75 6e 74 73 2e 20 20 49 74 20  _SetCounts.  It 
5a910 6b 65 65 70 73 20 61 0a 2a 2a 20 63 6f 6e 74 65  keeps a.** conte
5a920 78 74 20 73 74 61 63 6b 20 6a 75 73 74 20 6c 69  xt stack just li
5a930 6b 65 20 6c 61 73 74 52 6f 77 69 64 20 73 6f 20  ke lastRowid so 
5a940 74 68 61 74 20 74 68 65 20 63 6f 75 6e 74 20 6f  that the count o
5a950 66 20 63 68 61 6e 67 65 73 0a 2a 2a 20 77 69 74  f changes.** wit
5a960 68 69 6e 20 61 20 74 72 69 67 67 65 72 20 69 73  hin a trigger is
5a970 20 6e 6f 74 20 73 65 65 6e 20 6f 75 74 73 69 64   not seen outsid
5a980 65 20 74 68 65 20 74 72 69 67 67 65 72 2e 20 20  e the trigger.  
5a990 43 68 61 6e 67 65 73 20 74 6f 20 76 69 65 77 73  Changes to views
5a9a0 20 64 6f 20 6e 6f 74 0a 2a 2a 20 61 66 66 65 63   do not.** affec
5a9b0 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6c  t the value of l
5a9c0 73 43 68 61 6e 67 65 2e 0a 2a 2a 20 54 68 65 20  sChange..** The 
5a9d0 73 71 6c 69 74 65 2e 63 73 43 68 61 6e 67 65 20  sqlite.csChange 
5a9e0 6b 65 65 70 73 20 74 72 61 63 6b 20 6f 66 20 74  keeps track of t
5a9f0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 75 72  he number of cur
5aa00 72 65 6e 74 20 63 68 61 6e 67 65 73 20 28 73 69  rent changes (si
5aa10 6e 63 65 0a 2a 2a 20 74 68 65 20 6c 61 73 74 20  nce.** the last 
5aa20 73 74 61 74 65 6d 65 6e 74 29 20 61 6e 64 20 69  statement) and i
5aa30 73 20 75 73 65 64 20 74 6f 20 75 70 64 61 74 65  s used to update
5aa40 20 73 71 6c 69 74 65 5f 6c 73 43 68 61 6e 67 65   sqlite_lsChange
5aa50 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d 62  ..**.** The memb
5aa60 65 72 20 76 61 72 69 61 62 6c 65 73 20 73 71 6c  er variables sql
5aa70 69 74 65 2e 65 72 72 43 6f 64 65 2c 20 73 71 6c  ite.errCode, sql
5aa80 69 74 65 2e 7a 45 72 72 4d 73 67 20 61 6e 64 20  ite.zErrMsg and 
5aa90 73 71 6c 69 74 65 2e 7a 45 72 72 4d 73 67 31 36  sqlite.zErrMsg16
5aaa0 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 6d 6f  .** store the mo
5aab0 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20  st recent error 
5aac0 63 6f 64 65 20 61 6e 64 2c 20 69 66 20 61 70 70  code and, if app
5aad0 6c 69 63 61 62 6c 65 2c 20 73 74 72 69 6e 67 2e  licable, string.
5aae0 20 54 68 65 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c   The.** internal
5aaf0 20 66 75 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65   function sqlite
5ab00 33 45 72 72 6f 72 28 29 20 69 73 20 75 73 65 64  3Error() is used
5ab10 20 74 6f 20 73 65 74 20 74 68 65 73 65 20 76 61   to set these va
5ab20 72 69 61 62 6c 65 73 0a 2a 2a 20 63 6f 6e 73 69  riables.** consi
5ab30 73 74 65 6e 74 6c 79 2e 0a 2a 2f 0a 73 74 72 75  stently..*/.stru
5ab40 63 74 20 73 71 6c 69 74 65 33 20 7b 0a 20 20 73  ct sqlite3 {.  s
5ab50 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
5ab60 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
5ab70 4f 53 20 49 6e 74 65 72 66 61 63 65 20 2a 2f 0a  OS Interface */.
5ab80 20 20 69 6e 74 20 6e 44 62 3b 20 20 20 20 20 20    int nDb;      
5ab90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5aba0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 61 63  /* Number of bac
5abb0 6b 65 6e 64 73 20 63 75 72 72 65 6e 74 6c 79 20  kends currently 
5abc0 69 6e 20 75 73 65 20 2a 2f 0a 20 20 44 62 20 2a  in use */.  Db *
5abd0 61 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  aDb;            
5abe0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c            /* All
5abf0 20 62 61 63 6b 65 6e 64 73 20 2a 2f 0a 20 20 69   backends */.  i
5ac00 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20  nt flags;       
5ac10 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5ac20 4d 69 73 63 65 6c 6c 61 6e 65 6f 75 73 20 66 6c  Miscellaneous fl
5ac30 61 67 73 2e 20 53 65 65 20 62 65 6c 6f 77 20 2a  ags. See below *
5ac40 2f 0a 20 20 69 6e 74 20 6f 70 65 6e 46 6c 61 67  /.  int openFlag
5ac50 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
5ac60 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 73 65    /* Flags passe
5ac70 64 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 66 73  d to sqlite3_vfs
5ac80 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e  .xOpen() */.  in
5ac90 74 20 65 72 72 43 6f 64 65 3b 20 20 20 20 20 20  t errCode;      
5aca0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
5acb0 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72  ost recent error
5acc0 20 63 6f 64 65 20 28 53 51 4c 49 54 45 5f 2a 29   code (SQLITE_*)
5acd0 20 2a 2f 0a 20 20 69 6e 74 20 65 72 72 4d 61 73   */.  int errMas
5ace0 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k;              
5acf0 20 20 20 20 2f 2a 20 26 20 72 65 73 75 6c 74 20      /* & result 
5ad00 63 6f 64 65 73 20 77 69 74 68 20 74 68 69 73 20  codes with this 
5ad10 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  before returning
5ad20 20 2a 2f 0a 20 20 75 38 20 61 75 74 6f 43 6f 6d   */.  u8 autoCom
5ad30 6d 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  mit;            
5ad40 20 20 20 20 2f 2a 20 54 68 65 20 61 75 74 6f 2d      /* The auto-
5ad50 63 6f 6d 6d 69 74 20 66 6c 61 67 2e 20 2a 2f 0a  commit flag. */.
5ad60 20 20 75 38 20 74 65 6d 70 5f 73 74 6f 72 65 3b    u8 temp_store;
5ad70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ad80 2f 2a 20 31 3a 20 66 69 6c 65 20 32 3a 20 6d 65  /* 1: file 2: me
5ad90 6d 6f 72 79 20 30 3a 20 64 65 66 61 75 6c 74 20  mory 0: default 
5ada0 2a 2f 0a 20 20 75 38 20 6d 61 6c 6c 6f 63 46 61  */.  u8 mallocFa
5adb0 69 6c 65 64 3b 20 20 20 20 20 20 20 20 20 20 20  iled;           
5adc0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 77 65     /* True if we
5add0 20 68 61 76 65 20 73 65 65 6e 20 61 20 6d 61 6c   have seen a mal
5ade0 6c 6f 63 20 66 61 69 6c 75 72 65 20 2a 2f 0a 20  loc failure */. 
5adf0 20 75 38 20 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65   u8 dfltLockMode
5ae00 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
5ae10 2a 20 44 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e  * Default lockin
5ae20 67 2d 6d 6f 64 65 20 66 6f 72 20 61 74 74 61 63  g-mode for attac
5ae30 68 65 64 20 64 62 73 20 2a 2f 0a 20 20 75 38 20  hed dbs */.  u8 
5ae40 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 3b  dfltJournalMode;
5ae50 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65             /* De
5ae60 66 61 75 6c 74 20 6a 6f 75 72 6e 61 6c 20 6d 6f  fault journal mo
5ae70 64 65 20 66 6f 72 20 61 74 74 61 63 68 65 64 20  de for attached 
5ae80 64 62 73 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20  dbs */.  signed 
5ae90 63 68 61 72 20 6e 65 78 74 41 75 74 6f 76 61 63  char nextAutovac
5aea0 3b 20 20 20 20 20 20 2f 2a 20 41 75 74 6f 76 61  ;      /* Autova
5aeb0 63 20 73 65 74 74 69 6e 67 20 61 66 74 65 72 20  c setting after 
5aec0 56 41 43 55 55 4d 20 69 66 20 3e 3d 30 20 2a 2f  VACUUM if >=0 */
5aed0 0a 20 20 69 6e 74 20 6e 65 78 74 50 61 67 65 73  .  int nextPages
5aee0 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ize;            
5aef0 20 2f 2a 20 50 61 67 65 73 69 7a 65 20 61 66 74   /* Pagesize aft
5af00 65 72 20 56 41 43 55 55 4d 20 69 66 20 3e 30 20  er VACUUM if >0 
5af10 2a 2f 0a 20 20 69 6e 74 20 6e 54 61 62 6c 65 3b  */.  int nTable;
5af20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5af30 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
5af40 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 64 61  tables in the da
5af50 74 61 62 61 73 65 20 2a 2f 0a 20 20 43 6f 6c 6c  tabase */.  Coll
5af60 53 65 71 20 2a 70 44 66 6c 74 43 6f 6c 6c 3b 20  Seq *pDfltColl; 
5af70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
5af80 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69   default collati
5af90 6e 67 20 73 65 71 75 65 6e 63 65 20 28 42 49 4e  ng sequence (BIN
5afa0 41 52 59 29 20 2a 2f 0a 20 20 69 36 34 20 6c 61  ARY) */.  i64 la
5afb0 73 74 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20  stRowid;        
5afc0 20 20 20 20 20 20 20 20 2f 2a 20 52 4f 57 49 44          /* ROWID
5afd0 20 6f 66 20 6d 6f 73 74 20 72 65 63 65 6e 74 20   of most recent 
5afe0 69 6e 73 65 72 74 20 28 73 65 65 20 61 62 6f 76  insert (see abov
5aff0 65 29 20 2a 2f 0a 20 20 75 33 32 20 6d 61 67 69  e) */.  u32 magi
5b000 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
5b010 20 20 20 20 20 20 2f 2a 20 4d 61 67 69 63 20 6e        /* Magic n
5b020 75 6d 62 65 72 20 66 6f 72 20 64 65 74 65 63 74  umber for detect
5b030 20 6c 69 62 72 61 72 79 20 6d 69 73 75 73 65 20   library misuse 
5b040 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67 65  */.  int nChange
5b050 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
5b060 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 74 75     /* Value retu
5b070 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  rned by sqlite3_
5b080 63 68 61 6e 67 65 73 28 29 20 2a 2f 0a 20 20 69  changes() */.  i
5b090 6e 74 20 6e 54 6f 74 61 6c 43 68 61 6e 67 65 3b  nt nTotalChange;
5b0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5b0b0 56 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62  Value returned b
5b0c0 79 20 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f  y sqlite3_total_
5b0d0 63 68 61 6e 67 65 73 28 29 20 2a 2f 0a 20 20 73  changes() */.  s
5b0e0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75  qlite3_mutex *mu
5b0f0 74 65 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  tex;         /* 
5b100 43 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 74 65 78  Connection mutex
5b110 20 2a 2f 0a 20 20 69 6e 74 20 61 4c 69 6d 69 74   */.  int aLimit
5b120 5b 53 51 4c 49 54 45 5f 4e 5f 4c 49 4d 49 54 5d  [SQLITE_N_LIMIT]
5b130 3b 20 20 20 2f 2a 20 4c 69 6d 69 74 73 20 2a 2f  ;   /* Limits */
5b140 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  .  struct sqlite
5b150 33 49 6e 69 74 49 6e 66 6f 20 7b 20 20 20 20 20  3InitInfo {     
5b160 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20   /* Information 
5b170 75 73 65 64 20 64 75 72 69 6e 67 20 69 6e 69 74  used during init
5b180 69 61 6c 69 7a 61 74 69 6f 6e 20 2a 2f 0a 20 20  ialization */.  
5b190 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20    int iDb;      
5b1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
5b1b0 20 57 68 65 6e 20 62 61 63 6b 20 69 73 20 62 65   When back is be
5b1c0 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 65 64 20  ing initialized 
5b1d0 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 65 77 54 6e  */.    int newTn
5b1e0 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  um;             
5b1f0 20 20 20 2f 2a 20 52 6f 6f 74 70 61 67 65 20 6f     /* Rootpage o
5b200 66 20 74 61 62 6c 65 20 62 65 69 6e 67 20 69 6e  f table being in
5b210 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 20  itialized */.   
5b220 20 75 38 20 62 75 73 79 3b 20 20 20 20 20 20 20   u8 busy;       
5b230 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5b240 54 52 55 45 20 69 66 20 63 75 72 72 65 6e 74 6c  TRUE if currentl
5b250 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 2a  y initializing *
5b260 2f 0a 20 20 20 20 75 38 20 6f 72 70 68 61 6e 54  /.    u8 orphanT
5b270 72 69 67 67 65 72 3b 20 20 20 20 20 20 20 20 20  rigger;         
5b280 20 20 2f 2a 20 4c 61 73 74 20 73 74 61 74 65 6d    /* Last statem
5b290 65 6e 74 20 69 73 20 6f 72 70 68 61 6e 65 64 20  ent is orphaned 
5b2a0 54 45 4d 50 20 74 72 69 67 67 65 72 20 2a 2f 0a  TEMP trigger */.
5b2b0 20 20 7d 20 69 6e 69 74 3b 0a 20 20 69 6e 74 20    } init;.  int 
5b2c0 6e 45 78 74 65 6e 73 69 6f 6e 3b 20 20 20 20 20  nExtension;     
5b2d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
5b2e0 62 65 72 20 6f 66 20 6c 6f 61 64 65 64 20 65 78  ber of loaded ex
5b2f0 74 65 6e 73 69 6f 6e 73 20 2a 2f 0a 20 20 76 6f  tensions */.  vo
5b300 69 64 20 2a 2a 61 45 78 74 65 6e 73 69 6f 6e 3b  id **aExtension;
5b310 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
5b320 72 72 61 79 20 6f 66 20 73 68 61 72 65 64 20 6c  rray of shared l
5b330 69 62 72 61 72 79 20 68 61 6e 64 6c 65 73 20 2a  ibrary handles *
5b340 2f 0a 20 20 73 74 72 75 63 74 20 56 64 62 65 20  /.  struct Vdbe 
5b350 2a 70 56 64 62 65 3b 20 20 20 20 20 20 20 20 20  *pVdbe;         
5b360 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 63 74    /* List of act
5b370 69 76 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68  ive virtual mach
5b380 69 6e 65 73 20 2a 2f 0a 20 20 69 6e 74 20 61 63  ines */.  int ac
5b390 74 69 76 65 56 64 62 65 43 6e 74 3b 20 20 20 20  tiveVdbeCnt;    
5b3a0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
5b3b0 72 20 6f 66 20 56 44 42 45 73 20 63 75 72 72 65  r of VDBEs curre
5b3c0 6e 74 6c 79 20 65 78 65 63 75 74 69 6e 67 20 2a  ntly executing *
5b3d0 2f 0a 20 20 69 6e 74 20 77 72 69 74 65 56 64 62  /.  int writeVdb
5b3e0 65 43 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  eCnt;           
5b3f0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61    /* Number of a
5b400 63 74 69 76 65 20 56 44 42 45 73 20 74 68 61 74  ctive VDBEs that
5b410 20 61 72 65 20 77 72 69 74 69 6e 67 20 2a 2f 0a   are writing */.
5b420 20 20 76 6f 69 64 20 28 2a 78 54 72 61 63 65 29    void (*xTrace)
5b430 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61  (void*,const cha
5b440 72 2a 29 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  r*);        /* T
5b450 72 61 63 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  race function */
5b460 0a 20 20 76 6f 69 64 20 2a 70 54 72 61 63 65 41  .  void *pTraceA
5b470 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rg;             
5b480 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5b490 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  Argument to the 
5b4a0 74 72 61 63 65 20 66 75 6e 63 74 69 6f 6e 20 2a  trace function *
5b4b0 2f 0a 20 20 76 6f 69 64 20 28 2a 78 50 72 6f 66  /.  void (*xProf
5b4c0 69 6c 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74  ile)(void*,const
5b4d0 20 63 68 61 72 2a 2c 75 36 34 29 3b 20 20 2f 2a   char*,u64);  /*
5b4e0 20 50 72 6f 66 69 6c 69 6e 67 20 66 75 6e 63 74   Profiling funct
5b4f0 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  ion */.  void *p
5b500 50 72 6f 66 69 6c 65 41 72 67 3b 20 20 20 20 20  ProfileArg;     
5b510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b520 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74     /* Argument t
5b530 6f 20 70 72 6f 66 69 6c 65 20 66 75 6e 63 74 69  o profile functi
5b540 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43  on */.  void *pC
5b550 6f 6d 6d 69 74 41 72 67 3b 20 20 20 20 20 20 20  ommitArg;       
5b560 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67            /* Arg
5b570 75 6d 65 6e 74 20 74 6f 20 78 43 6f 6d 6d 69 74  ument to xCommit
5b580 43 61 6c 6c 62 61 63 6b 28 29 20 2a 2f 20 20 20  Callback() */   
5b590 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6d 6d 69 74  .  int (*xCommit
5b5a0 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 29  Callback)(void*)
5b5b0 3b 20 20 20 20 2f 2a 20 49 6e 76 6f 6b 65 64 20  ;    /* Invoked 
5b5c0 61 74 20 65 76 65 72 79 20 63 6f 6d 6d 69 74 2e  at every commit.
5b5d0 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 52 6f 6c   */.  void *pRol
5b5e0 6c 62 61 63 6b 41 72 67 3b 20 20 20 20 20 20 20  lbackArg;       
5b5f0 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d          /* Argum
5b600 65 6e 74 20 74 6f 20 78 52 6f 6c 6c 62 61 63 6b  ent to xRollback
5b610 43 61 6c 6c 62 61 63 6b 28 29 20 2a 2f 20 20 20  Callback() */   
5b620 0a 20 20 76 6f 69 64 20 28 2a 78 52 6f 6c 6c 62  .  void (*xRollb
5b630 61 63 6b 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69  ackCallback)(voi
5b640 64 2a 29 3b 20 2f 2a 20 49 6e 76 6f 6b 65 64 20  d*); /* Invoked 
5b650 61 74 20 65 76 65 72 79 20 63 6f 6d 6d 69 74 2e  at every commit.
5b660 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 55 70 64   */.  void *pUpd
5b670 61 74 65 41 72 67 3b 0a 20 20 76 6f 69 64 20 28  ateArg;.  void (
5b680 2a 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63 6b  *xUpdateCallback
5b690 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 20 63 6f 6e  )(void*,int, con
5b6a0 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63  st char*,const c
5b6b0 68 61 72 2a 2c 73 71 6c 69 74 65 5f 69 6e 74 36  har*,sqlite_int6
5b6c0 34 29 3b 0a 20 20 76 6f 69 64 28 2a 78 43 6f 6c  4);.  void(*xCol
5b6d0 6c 4e 65 65 64 65 64 29 28 76 6f 69 64 2a 2c 73  lNeeded)(void*,s
5b6e0 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54 65 78  qlite3*,int eTex
5b6f0 74 52 65 70 2c 63 6f 6e 73 74 20 63 68 61 72 2a  tRep,const char*
5b700 29 3b 0a 20 20 76 6f 69 64 28 2a 78 43 6f 6c 6c  );.  void(*xColl
5b710 4e 65 65 64 65 64 31 36 29 28 76 6f 69 64 2a 2c  Needed16)(void*,
5b720 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54 65  sqlite3*,int eTe
5b730 78 74 52 65 70 2c 63 6f 6e 73 74 20 76 6f 69 64  xtRep,const void
5b740 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 70 43 6f 6c  *);.  void *pCol
5b750 6c 4e 65 65 64 65 64 41 72 67 3b 0a 20 20 73 71  lNeededArg;.  sq
5b760 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 45 72  lite3_value *pEr
5b770 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  r;          /* M
5b780 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72  ost recent error
5b790 20 6d 65 73 73 61 67 65 20 2a 2f 0a 20 20 63 68   message */.  ch
5b7a0 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20  ar *zErrMsg;    
5b7b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
5b7c0 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72  ost recent error
5b7d0 20 6d 65 73 73 61 67 65 20 28 55 54 46 2d 38 20   message (UTF-8 
5b7e0 65 6e 63 6f 64 65 64 29 20 2a 2f 0a 20 20 63 68  encoded) */.  ch
5b7f0 61 72 20 2a 7a 45 72 72 4d 73 67 31 36 3b 20 20  ar *zErrMsg16;  
5b800 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
5b810 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72  ost recent error
5b820 20 6d 65 73 73 61 67 65 20 28 55 54 46 2d 31 36   message (UTF-16
5b830 20 65 6e 63 6f 64 65 64 29 20 2a 2f 0a 20 20 75   encoded) */.  u
5b840 6e 69 6f 6e 20 7b 0a 20 20 20 20 76 6f 6c 61 74  nion {.    volat
5b850 69 6c 65 20 69 6e 74 20 69 73 49 6e 74 65 72 72  ile int isInterr
5b860 75 70 74 65 64 3b 20 2f 2a 20 54 72 75 65 20 69  upted; /* True i
5b870 66 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72  f sqlite3_interr
5b880 75 70 74 20 68 61 73 20 62 65 65 6e 20 63 61 6c  upt has been cal
5b890 6c 65 64 20 2a 2f 0a 20 20 20 20 64 6f 75 62 6c  led */.    doubl
5b8a0 65 20 6e 6f 74 55 73 65 64 31 3b 20 20 20 20 20  e notUsed1;     
5b8b0 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 72         /* Spacer
5b8c0 20 2a 2f 0a 20 20 7d 20 75 31 3b 0a 20 20 4c 6f   */.  } u1;.  Lo
5b8d0 6f 6b 61 73 69 64 65 20 6c 6f 6f 6b 61 73 69 64  okaside lookasid
5b8e0 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  e;          /* L
5b8f0 6f 6f 6b 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20  ookaside malloc 
5b900 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 2a 2f  configuration */
5b910 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
5b920 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49  OMIT_AUTHORIZATI
5b930 4f 4e 0a 20 20 69 6e 74 20 28 2a 78 41 75 74 68  ON.  int (*xAuth
5b940 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  )(void*,int,cons
5b950 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68  t char*,const ch
5b960 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  ar*,const char*,
5b970 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20 20  const char*);.  
5b980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
5b9a0 20 41 63 63 65 73 73 20 61 75 74 68 6f 72 69 7a   Access authoriz
5b9b0 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a  ation function *
5b9c0 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 74 68 41  /.  void *pAuthA
5b9d0 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rg;             
5b9e0 20 20 2f 2a 20 31 73 74 20 61 72 67 75 6d 65 6e    /* 1st argumen
5b9f0 74 20 74 6f 20 74 68 65 20 61 63 63 65 73 73 20  t to the access 
5ba00 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  auth function */
5ba10 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20  .#endif.#ifndef 
5ba20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47  SQLITE_OMIT_PROG
5ba30 52 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 20 20  RESS_CALLBACK.  
5ba40 69 6e 74 20 28 2a 78 50 72 6f 67 72 65 73 73 29  int (*xProgress)
5ba50 28 76 6f 69 64 20 2a 29 3b 20 20 20 20 20 2f 2a  (void *);     /*
5ba60 20 54 68 65 20 70 72 6f 67 72 65 73 73 20 63 61   The progress ca
5ba70 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 76 6f 69 64  llback */.  void
5ba80 20 2a 70 50 72 6f 67 72 65 73 73 41 72 67 3b 20   *pProgressArg; 
5ba90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67            /* Arg
5baa0 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 70 72 6f  ument to the pro
5bab0 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 2a  gress callback *
5bac0 2f 0a 20 20 69 6e 74 20 6e 50 72 6f 67 72 65 73  /.  int nProgres
5bad0 73 4f 70 73 3b 20 20 20 20 20 20 20 20 20 20 20  sOps;           
5bae0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f    /* Number of o
5baf0 70 63 6f 64 65 73 20 66 6f 72 20 70 72 6f 67 72  pcodes for progr
5bb00 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a  ess callback */.
5bb10 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53  #endif.#ifndef S
5bb20 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
5bb30 41 4c 54 41 42 4c 45 0a 20 20 48 61 73 68 20 61  ALTABLE.  Hash a
5bb40 4d 6f 64 75 6c 65 3b 20 20 20 20 20 20 20 20 20  Module;         
5bb50 20 20 20 20 20 20 20 20 2f 2a 20 70 6f 70 75 6c          /* popul
5bb60 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  ated by sqlite3_
5bb70 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20  create_module() 
5bb80 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 56 54 61  */.  Table *pVTa
5bb90 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
5bba0 20 20 20 2f 2a 20 76 74 61 62 20 77 69 74 68 20     /* vtab with 
5bbb0 61 63 74 69 76 65 20 43 6f 6e 6e 65 63 74 2f 43  active Connect/C
5bbc0 72 65 61 74 65 20 6d 65 74 68 6f 64 20 2a 2f 0a  reate method */.
5bbd0 20 20 56 54 61 62 6c 65 20 2a 2a 61 56 54 72 61    VTable **aVTra
5bbe0 6e 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ns;             
5bbf0 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65  /* Virtual table
5bc00 73 20 77 69 74 68 20 6f 70 65 6e 20 74 72 61 6e  s with open tran
5bc10 73 61 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e  sactions */.  in
5bc20 74 20 6e 56 54 72 61 6e 73 3b 20 20 20 20 20 20  t nVTrans;      
5bc30 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
5bc40 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66  llocated size of
5bc50 20 61 56 54 72 61 6e 73 20 2a 2f 0a 20 20 56 54   aVTrans */.  VT
5bc60 61 62 6c 65 20 2a 70 44 69 73 63 6f 6e 6e 65 63  able *pDisconnec
5bc70 74 3b 20 20 20 20 2f 2a 20 44 69 73 63 6f 6e 6e  t;    /* Disconn
5bc80 65 63 74 20 74 68 65 73 65 20 69 6e 20 6e 65 78  ect these in nex
5bc90 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  t sqlite3_prepar
5bca0 65 28 29 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20  e() */.#endif.  
5bcb0 46 75 6e 63 44 65 66 48 61 73 68 20 61 46 75 6e  FuncDefHash aFun
5bcc0 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  c;            /*
5bcd0 20 48 61 73 68 20 74 61 62 6c 65 20 6f 66 20 63   Hash table of c
5bce0 6f 6e 6e 65 63 74 69 6f 6e 20 66 75 6e 63 74 69  onnection functi
5bcf0 6f 6e 73 20 2a 2f 0a 20 20 48 61 73 68 20 61 43  ons */.  Hash aC
5bd00 6f 6c 6c 53 65 71 3b 20 20 20 20 20 20 20 20 20  ollSeq;         
5bd10 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 63 6f         /* All co
5bd20 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
5bd30 73 20 2a 2f 0a 20 20 42 75 73 79 48 61 6e 64 6c  s */.  BusyHandl
5bd40 65 72 20 62 75 73 79 48 61 6e 64 6c 65 72 3b 20  er busyHandler; 
5bd50 20 20 20 20 20 2f 2a 20 42 75 73 79 20 63 61 6c       /* Busy cal
5bd60 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 62  lback */.  int b
5bd70 75 73 79 54 69 6d 65 6f 75 74 3b 20 20 20 20 20  usyTimeout;     
5bd80 20 20 20 20 20 20 20 20 20 2f 2a 20 42 75 73 79           /* Busy
5bd90 20 68 61 6e 64 6c 65 72 20 74 69 6d 65 6f 75 74   handler timeout
5bda0 2c 20 69 6e 20 6d 73 65 63 20 2a 2f 0a 20 20 44  , in msec */.  D
5bdb0 62 20 61 44 62 53 74 61 74 69 63 5b 32 5d 3b 20  b aDbStatic[2]; 
5bdc0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5bdd0 53 74 61 74 69 63 20 73 70 61 63 65 20 66 6f 72  Static space for
5bde0 20 74 68 65 20 32 20 64 65 66 61 75 6c 74 20 62   the 2 default b
5bdf0 61 63 6b 65 6e 64 73 20 2a 2f 0a 20 20 53 61 76  ackends */.  Sav
5be00 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 70 6f 69  epoint *pSavepoi
5be10 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 69  nt;        /* Li
5be20 73 74 20 6f 66 20 61 63 74 69 76 65 20 73 61 76  st of active sav
5be30 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 20 69 6e 74  epoints */.  int
5be40 20 6e 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 20   nSavepoint;    
5be50 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
5be60 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 74 72 61 6e  mber of non-tran
5be70 73 61 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e  saction savepoin
5be80 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 74 61  ts */.  int nSta
5be90 74 65 6d 65 6e 74 3b 20 20 20 20 20 20 20 20 20  tement;         
5bea0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
5beb0 6f 66 20 6e 65 73 74 65 64 20 73 74 61 74 65 6d  of nested statem
5bec0 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e 73  ent-transactions
5bed0 20 20 2a 2f 0a 20 20 75 38 20 69 73 54 72 61 6e    */.  u8 isTran
5bee0 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74  sactionSavepoint
5bef0 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20  ;    /* True if 
5bf00 74 68 65 20 6f 75 74 65 72 6d 6f 73 74 20 73 61  the outermost sa
5bf10 76 65 70 6f 69 6e 74 20 69 73 20 61 20 54 53 20  vepoint is a TS 
5bf20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  */..#ifdef SQLIT
5bf30 45 5f 45 4e 41 42 4c 45 5f 55 4e 4c 4f 43 4b 5f  E_ENABLE_UNLOCK_
5bf40 4e 4f 54 49 46 59 0a 20 20 2f 2a 20 54 68 65 20  NOTIFY.  /* The 
5bf50 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62  following variab
5bf60 6c 65 73 20 61 72 65 20 61 6c 6c 20 70 72 6f 74  les are all prot
5bf70 65 63 74 65 64 20 62 79 20 74 68 65 20 53 54 41  ected by the STA
5bf80 54 49 43 5f 4d 41 53 54 45 52 20 0a 20 20 2a 2a  TIC_MASTER .  **
5bf90 20 6d 75 74 65 78 2c 20 6e 6f 74 20 62 79 20 73   mutex, not by s
5bfa0 71 6c 69 74 65 33 2e 6d 75 74 65 78 2e 20 54 68  qlite3.mutex. Th
5bfb0 65 79 20 61 72 65 20 75 73 65 64 20 62 79 20 63  ey are used by c
5bfc0 6f 64 65 20 69 6e 20 6e 6f 74 69 66 79 2e 63 2e  ode in notify.c.
5bfd0 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 65 6e   .  **.  ** When
5bfe0 20 58 2e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63   X.pUnlockConnec
5bff0 74 69 6f 6e 3d 3d 59 2c 20 74 68 61 74 20 6d 65  tion==Y, that me
5c000 61 6e 73 20 74 68 61 74 20 58 20 69 73 20 77 61  ans that X is wa
5c010 69 74 69 6e 67 20 66 6f 72 20 59 20 74 6f 0a 20  iting for Y to. 
5c020 20 2a 2a 20 75 6e 6c 6f 63 6b 20 73 6f 20 74 68   ** unlock so th
5c030 61 74 20 69 74 20 63 61 6e 20 70 72 6f 63 65 65  at it can procee
5c040 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 65  d..  **.  ** Whe
5c050 6e 20 58 2e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e  n X.pBlockingCon
5c060 6e 65 63 74 69 6f 6e 3d 3d 59 2c 20 74 68 61 74  nection==Y, that
5c070 20 6d 65 61 6e 73 20 74 68 61 74 20 73 6f 6d 65   means that some
5c080 74 68 69 6e 67 20 74 68 61 74 20 58 20 74 72 69  thing that X tri
5c090 65 64 0a 20 20 2a 2a 20 74 72 69 65 64 20 74 6f  ed.  ** tried to
5c0a0 20 64 6f 20 72 65 63 65 6e 74 6c 79 20 66 61 69   do recently fai
5c0b0 6c 65 64 20 77 69 74 68 20 61 6e 20 53 51 4c 49  led with an SQLI
5c0c0 54 45 5f 4c 4f 43 4b 45 44 20 65 72 72 6f 72 20  TE_LOCKED error 
5c0d0 64 75 65 20 74 6f 20 6c 6f 63 6b 73 0a 20 20 2a  due to locks.  *
5c0e0 2a 20 68 65 6c 64 20 62 79 20 59 2e 0a 20 20 2a  * held by Y..  *
5c0f0 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 42 6c  /.  sqlite3 *pBl
5c100 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e  ockingConnection
5c110 3b 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20  ; /* Connection 
5c120 74 68 61 74 20 63 61 75 73 65 64 20 53 51 4c 49  that caused SQLI
5c130 54 45 5f 4c 4f 43 4b 45 44 20 2a 2f 0a 20 20 73  TE_LOCKED */.  s
5c140 71 6c 69 74 65 33 20 2a 70 55 6e 6c 6f 63 6b 43  qlite3 *pUnlockC
5c150 6f 6e 6e 65 63 74 69 6f 6e 3b 20 20 20 20 20 20  onnection;      
5c160 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69       /* Connecti
5c170 6f 6e 20 74 6f 20 77 61 74 63 68 20 66 6f 72 20  on to watch for 
5c180 75 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 76 6f 69 64  unlock */.  void
5c190 20 2a 70 55 6e 6c 6f 63 6b 41 72 67 3b 20 20 20   *pUnlockArg;   
5c1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c1b0 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f    /* Argument to
5c1c0 20 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20 2a   xUnlockNotify *
5c1d0 2f 0a 20 20 76 6f 69 64 20 28 2a 78 55 6e 6c 6f  /.  void (*xUnlo
5c1e0 63 6b 4e 6f 74 69 66 79 29 28 76 6f 69 64 20 2a  ckNotify)(void *
5c1f0 2a 2c 20 69 6e 74 29 3b 20 20 2f 2a 20 55 6e 6c  *, int);  /* Unl
5c200 6f 63 6b 20 6e 6f 74 69 66 79 20 63 61 6c 6c 62  ock notify callb
5c210 61 63 6b 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ack */.  sqlite3
5c220 20 2a 70 4e 65 78 74 42 6c 6f 63 6b 65 64 3b 20   *pNextBlocked; 
5c230 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 69         /* Next i
5c240 6e 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 62 6c  n list of all bl
5c250 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e  ocked connection
5c260 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a  s */.#endif.};..
5c270 2f 2a 0a 2a 2a 20 41 20 6d 61 63 72 6f 20 74 6f  /*.** A macro to
5c280 20 64 69 73 63 6f 76 65 72 20 74 68 65 20 65 6e   discover the en
5c290 63 6f 64 69 6e 67 20 6f 66 20 61 20 64 61 74 61  coding of a data
5c2a0 62 61 73 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  base..*/.#define
5c2b0 20 45 4e 43 28 64 62 29 20 28 28 64 62 29 2d 3e   ENC(db) ((db)->
5c2c0 61 44 62 5b 30 5d 2e 70 53 63 68 65 6d 61 2d 3e  aDb[0].pSchema->
5c2d0 65 6e 63 29 0a 0a 2f 2a 0a 2a 2a 20 50 6f 73 73  enc)../*.** Poss
5c2e0 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f 72 20  ible values for 
5c2f0 74 68 65 20 73 71 6c 69 74 65 2e 66 6c 61 67 73  the sqlite.flags
5c300 20 61 6e 64 20 6f 72 20 44 62 2e 66 6c 61 67 73   and or Db.flags
5c310 20 66 69 65 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20 4f   fields..**.** O
5c320 6e 20 73 71 6c 69 74 65 2e 66 6c 61 67 73 2c 20  n sqlite.flags, 
5c330 74 68 65 20 53 51 4c 49 54 45 5f 49 6e 54 72 61  the SQLITE_InTra
5c340 6e 73 20 76 61 6c 75 65 20 6d 65 61 6e 73 20 74  ns value means t
5c350 68 61 74 20 77 65 20 68 61 76 65 0a 2a 2a 20 65  hat we have.** e
5c360 78 65 63 75 74 65 64 20 61 20 42 45 47 49 4e 2e  xecuted a BEGIN.
5c370 20 20 4f 6e 20 44 62 2e 66 6c 61 67 73 2c 20 53    On Db.flags, S
5c380 51 4c 49 54 45 5f 49 6e 54 72 61 6e 73 20 6d 65  QLITE_InTrans me
5c390 61 6e 73 20 61 20 73 74 61 74 65 6d 65 6e 74 0a  ans a statement.
5c3a0 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  ** transaction i
5c3b0 73 20 61 63 74 69 76 65 20 6f 6e 20 74 68 61 74  s active on that
5c3c0 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 61   particular data
5c3d0 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a 23 64  base file..*/.#d
5c3e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 56 64 62  efine SQLITE_Vdb
5c3f0 65 54 72 61 63 65 20 20 20 20 20 20 30 78 30 30  eTrace      0x00
5c400 30 30 30 30 30 31 20 20 2f 2a 20 54 72 75 65 20  000001  /* True 
5c410 74 6f 20 74 72 61 63 65 20 56 44 42 45 20 65 78  to trace VDBE ex
5c420 65 63 75 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69  ecution */.#defi
5c430 6e 65 20 53 51 4c 49 54 45 5f 49 6e 54 72 61 6e  ne SQLITE_InTran
5c440 73 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30  s        0x00000
5c450 30 30 38 20 20 2f 2a 20 54 72 75 65 20 69 66 20  008  /* True if 
5c460 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  in a transaction
5c470 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
5c480 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73  TE_InternChanges
5c490 20 20 30 78 30 30 30 30 30 30 31 30 20 20 2f 2a    0x00000010  /*
5c4a0 20 55 6e 63 6f 6d 6d 69 74 74 65 64 20 48 61 73   Uncommitted Has
5c4b0 68 20 74 61 62 6c 65 20 63 68 61 6e 67 65 73 20  h table changes 
5c4c0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
5c4d0 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73 20 20  E_FullColNames  
5c4e0 20 30 78 30 30 30 30 30 30 32 30 20 20 2f 2a 20   0x00000020  /* 
5c4f0 53 68 6f 77 20 66 75 6c 6c 20 63 6f 6c 75 6d 6e  Show full column
5c500 20 6e 61 6d 65 73 20 6f 6e 20 53 45 4c 45 43 54   names on SELECT
5c510 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
5c520 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65 73  TE_ShortColNames
5c530 20 20 30 78 30 30 30 30 30 30 34 30 20 20 2f 2a    0x00000040  /*
5c540 20 53 68 6f 77 20 73 68 6f 72 74 20 63 6f 6c 75   Show short colu
5c550 6d 6e 73 20 6e 61 6d 65 73 20 2a 2f 0a 23 64 65  mns names */.#de
5c560 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 6f 75 6e  fine SQLITE_Coun
5c570 74 52 6f 77 73 20 20 20 20 20 20 30 78 30 30 30  tRows      0x000
5c580 30 30 30 38 30 20 20 2f 2a 20 43 6f 75 6e 74 20  00080  /* Count 
5c590 72 6f 77 73 20 63 68 61 6e 67 65 64 20 62 79 20  rows changed by 
5c5a0 49 4e 53 45 52 54 2c 20 2a 2f 0a 20 20 20 20 20  INSERT, */.     
5c5b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c5c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c5d0 20 20 20 20 20 2f 2a 20 20 20 44 45 4c 45 54 45       /*   DELETE
5c5e0 2c 20 6f 72 20 55 50 44 41 54 45 20 61 6e 64 20  , or UPDATE and 
5c5f0 72 65 74 75 72 6e 20 2a 2f 0a 20 20 20 20 20 20  return */.      
5c600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c620 20 20 20 20 2f 2a 20 20 20 74 68 65 20 63 6f 75      /*   the cou
5c630 6e 74 20 75 73 69 6e 67 20 61 20 63 61 6c 6c 62  nt using a callb
5c640 61 63 6b 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ack. */.#define 
5c650 53 51 4c 49 54 45 5f 4e 75 6c 6c 43 61 6c 6c 62  SQLITE_NullCallb
5c660 61 63 6b 20 20 20 30 78 30 30 30 30 30 31 30 30  ack   0x00000100
5c670 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20    /* Invoke the 
5c680 63 61 6c 6c 62 61 63 6b 20 6f 6e 63 65 20 69 66  callback once if
5c690 20 74 68 65 20 2a 2f 0a 20 20 20 20 20 20 20 20   the */.        
5c6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c6c0 20 20 2f 2a 20 20 20 72 65 73 75 6c 74 20 73 65    /*   result se
5c6d0 74 20 69 73 20 65 6d 70 74 79 20 2a 2f 0a 23 64  t is empty */.#d
5c6e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 71 6c  efine SQLITE_Sql
5c6f0 54 72 61 63 65 20 20 20 20 20 20 20 30 78 30 30  Trace       0x00
5c700 30 30 30 32 30 30 20 20 2f 2a 20 44 65 62 75 67  000200  /* Debug
5c710 20 70 72 69 6e 74 20 53 51 4c 20 61 73 20 69 74   print SQL as it
5c720 20 65 78 65 63 75 74 65 73 20 2a 2f 0a 23 64 65   executes */.#de
5c730 66 69 6e 65 20 53 51 4c 49 54 45 5f 56 64 62 65  fine SQLITE_Vdbe
5c740 4c 69 73 74 69 6e 67 20 20 20 20 30 78 30 30 30  Listing    0x000
5c750 30 30 34 30 30 20 20 2f 2a 20 44 65 62 75 67 20  00400  /* Debug 
5c760 6c 69 73 74 69 6e 67 73 20 6f 66 20 56 44 42 45  listings of VDBE
5c770 20 70 72 6f 67 72 61 6d 73 20 2a 2f 0a 23 64 65   programs */.#de
5c780 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 72 69 74  fine SQLITE_Writ
5c790 65 53 63 68 65 6d 61 20 20 20 20 30 78 30 30 30  eSchema    0x000
5c7a0 30 30 38 30 30 20 20 2f 2a 20 4f 4b 20 74 6f 20  00800  /* OK to 
5c7b0 75 70 64 61 74 65 20 53 51 4c 49 54 45 5f 4d 41  update SQLITE_MA
5c7c0 53 54 45 52 20 2a 2f 0a 23 64 65 66 69 6e 65 20  STER */.#define 
5c7d0 53 51 4c 49 54 45 5f 4e 6f 52 65 61 64 6c 6f 63  SQLITE_NoReadloc
5c7e0 6b 20 20 20 20 20 30 78 30 30 30 30 31 30 30 30  k     0x00001000
5c7f0 20 20 2f 2a 20 52 65 61 64 6c 6f 63 6b 73 20 61    /* Readlocks a
5c800 72 65 20 6f 6d 69 74 74 65 64 20 77 68 65 6e 20  re omitted when 
5c810 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
5c820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c830 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 63             ** ac
5c840 63 65 73 73 69 6e 67 20 72 65 61 64 2d 6f 6e 6c  cessing read-onl
5c850 79 20 64 61 74 61 62 61 73 65 73 20 2a 2f 0a 23  y databases */.#
5c860 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 67  define SQLITE_Ig
5c870 6e 6f 72 65 43 68 65 63 6b 73 20 20 20 30 78 30  noreChecks   0x0
5c880 30 30 30 32 30 30 30 20 20 2f 2a 20 44 6f 20 6e  0002000  /* Do n
5c890 6f 74 20 65 6e 66 6f 72 63 65 20 63 68 65 63 6b  ot enforce check
5c8a0 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a   constraints */.
5c8b0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52  #define SQLITE_R
5c8c0 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20 30  eadUncommitted 0
5c8d0 78 30 30 30 30 34 30 30 30 20 2f 2a 20 46 6f 72  x00004000 /* For
5c8e0 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f   shared-cache mo
5c8f0 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  de */.#define SQ
5c900 4c 49 54 45 5f 4c 65 67 61 63 79 46 69 6c 65 46  LITE_LegacyFileF
5c910 6d 74 20 20 30 78 30 30 30 30 38 30 30 30 20 20  mt  0x00008000  
5c920 2f 2a 20 43 72 65 61 74 65 20 6e 65 77 20 64 61  /* Create new da
5c930 74 61 62 61 73 65 73 20 69 6e 20 66 6f 72 6d 61  tabases in forma
5c940 74 20 31 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  t 1 */.#define S
5c950 51 4c 49 54 45 5f 46 75 6c 6c 46 53 79 6e 63 20  QLITE_FullFSync 
5c960 20 20 20 20 20 30 78 30 30 30 31 30 30 30 30 20       0x00010000 
5c970 20 2f 2a 20 55 73 65 20 66 75 6c 6c 20 66 73 79   /* Use full fsy
5c980 6e 63 20 6f 6e 20 74 68 65 20 62 61 63 6b 65 6e  nc on the backen
5c990 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  d */.#define SQL
5c9a0 49 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f  ITE_LoadExtensio
5c9b0 6e 20 20 30 78 30 30 30 32 30 30 30 30 20 20 2f  n  0x00020000  /
5c9c0 2a 20 45 6e 61 62 6c 65 20 6c 6f 61 64 5f 65 78  * Enable load_ex
5c9d0 74 65 6e 73 69 6f 6e 20 2a 2f 0a 0a 23 64 65 66  tension */..#def
5c9e0 69 6e 65 20 53 51 4c 49 54 45 5f 52 65 63 6f 76  ine SQLITE_Recov
5c9f0 65 72 79 4d 6f 64 65 20 20 20 30 78 30 30 30 34  eryMode   0x0004
5ca00 30 30 30 30 20 20 2f 2a 20 49 67 6e 6f 72 65 20  0000  /* Ignore 
5ca10 73 63 68 65 6d 61 20 65 72 72 6f 72 73 20 2a 2f  schema errors */
5ca20 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
5ca30 52 65 76 65 72 73 65 4f 72 64 65 72 20 20 20 30  ReverseOrder   0
5ca40 78 30 30 31 30 30 30 30 30 20 20 2f 2a 20 52 65  x00100000  /* Re
5ca50 76 65 72 73 65 20 75 6e 6f 72 64 65 72 65 64 20  verse unordered 
5ca60 53 45 4c 45 43 54 73 20 2a 2f 0a 23 64 65 66 69  SELECTs */.#defi
5ca70 6e 65 20 53 51 4c 49 54 45 5f 52 65 63 54 72 69  ne SQLITE_RecTri
5ca80 67 67 65 72 73 20 20 20 20 30 78 30 30 32 30 30  ggers    0x00200
5ca90 30 30 30 20 20 2f 2a 20 45 6e 61 62 6c 65 20 72  000  /* Enable r
5caa0 65 63 75 72 73 69 76 65 20 74 72 69 67 67 65 72  ecursive trigger
5cab0 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 6f 73 73  s */../*.** Poss
5cac0 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f 72 20  ible values for 
5cad0 74 68 65 20 73 71 6c 69 74 65 2e 6d 61 67 69 63  the sqlite.magic
5cae0 20 66 69 65 6c 64 2e 0a 2a 2a 20 54 68 65 20 6e   field..** The n
5caf0 75 6d 62 65 72 73 20 61 72 65 20 6f 62 74 61 69  umbers are obtai
5cb00 6e 65 64 20 61 74 20 72 61 6e 64 6f 6d 20 61 6e  ned at random an
5cb10 64 20 68 61 76 65 20 6e 6f 20 73 70 65 63 69 61  d have no specia
5cb20 6c 20 6d 65 61 6e 69 6e 67 2c 20 6f 74 68 65 72  l meaning, other
5cb30 0a 2a 2a 20 74 68 61 6e 20 62 65 69 6e 67 20 64  .** than being d
5cb40 69 73 74 69 6e 63 74 20 66 72 6f 6d 20 6f 6e 65  istinct from one
5cb50 20 61 6e 6f 74 68 65 72 2e 0a 2a 2f 0a 23 64 65   another..*/.#de
5cb60 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47 49  fine SQLITE_MAGI
5cb70 43 5f 4f 50 45 4e 20 20 20 20 20 30 78 61 30 32  C_OPEN     0xa02
5cb80 39 61 36 39 37 20 20 2f 2a 20 44 61 74 61 62 61  9a697  /* Databa
5cb90 73 65 20 69 73 20 6f 70 65 6e 20 2a 2f 0a 23 64  se is open */.#d
5cba0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47  efine SQLITE_MAG
5cbb0 49 43 5f 43 4c 4f 53 45 44 20 20 20 30 78 39 66  IC_CLOSED   0x9f
5cbc0 33 63 32 64 33 33 20 20 2f 2a 20 44 61 74 61 62  3c2d33  /* Datab
5cbd0 61 73 65 20 69 73 20 63 6c 6f 73 65 64 20 2a 2f  ase is closed */
5cbe0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
5cbf0 4d 41 47 49 43 5f 53 49 43 4b 20 20 20 20 20 30  MAGIC_SICK     0
5cc00 78 34 62 37 37 31 32 39 30 20 20 2f 2a 20 45 72  x4b771290  /* Er
5cc10 72 6f 72 20 61 6e 64 20 61 77 61 69 74 69 6e 67  ror and awaiting
5cc20 20 63 6c 6f 73 65 20 2a 2f 0a 23 64 65 66 69 6e   close */.#defin
5cc30 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42  e SQLITE_MAGIC_B
5cc40 55 53 59 20 20 20 20 20 30 78 66 30 33 62 37 39  USY     0xf03b79
5cc50 30 36 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20  06  /* Database 
5cc60 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65  currently in use
5cc70 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
5cc80 54 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52 20 20  TE_MAGIC_ERROR  
5cc90 20 20 30 78 62 35 33 35 37 39 33 30 20 20 2f 2a    0xb5357930  /*
5cca0 20 41 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53   An SQLITE_MISUS
5ccb0 45 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64  E error occurred
5ccc0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20   */../*.** Each 
5ccd0 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  SQL function is 
5cce0 64 65 66 69 6e 65 64 20 62 79 20 61 6e 20 69 6e  defined by an in
5ccf0 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
5cd00 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63  llowing.** struc
5cd10 74 75 72 65 2e 20 20 41 20 70 6f 69 6e 74 65 72  ture.  A pointer
5cd20 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75   to this structu
5cd30 72 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  re is stored in 
5cd40 74 68 65 20 73 71 6c 69 74 65 2e 61 46 75 6e 63  the sqlite.aFunc
5cd50 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 2e 20  .** hash table. 
5cd60 20 57 68 65 6e 20 6d 75 6c 74 69 70 6c 65 20 66   When multiple f
5cd70 75 6e 63 74 69 6f 6e 73 20 68 61 76 65 20 74 68  unctions have th
5cd80 65 20 73 61 6d 65 20 6e 61 6d 65 2c 20 74 68 65  e same name, the
5cd90 20 68 61 73 68 20 74 61 62 6c 65 0a 2a 2a 20 70   hash table.** p
5cda0 6f 69 6e 74 73 20 74 6f 20 61 20 6c 69 6e 6b 65  oints to a linke
5cdb0 64 20 6c 69 73 74 20 6f 66 20 74 68 65 73 65 20  d list of these 
5cdc0 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a 73  structures..*/.s
5cdd0 74 72 75 63 74 20 46 75 6e 63 44 65 66 20 7b 0a  truct FuncDef {.
5cde0 20 20 69 31 36 20 6e 41 72 67 3b 20 20 20 20 20    i16 nArg;     
5cdf0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
5ce00 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e 20 20   of arguments.  
5ce10 2d 31 20 6d 65 61 6e 73 20 75 6e 6c 69 6d 69 74  -1 means unlimit
5ce20 65 64 20 2a 2f 0a 20 20 75 38 20 69 50 72 65 66  ed */.  u8 iPref
5ce30 45 6e 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Enc;         /* 
5ce40 50 72 65 66 65 72 72 65 64 20 74 65 78 74 20 65  Preferred text e
5ce50 6e 63 6f 64 69 6e 67 20 28 53 51 4c 49 54 45 5f  ncoding (SQLITE_
5ce60 55 54 46 38 2c 20 31 36 4c 45 2c 20 31 36 42 45  UTF8, 16LE, 16BE
5ce70 29 20 2a 2f 0a 20 20 75 38 20 66 6c 61 67 73 3b  ) */.  u8 flags;
5ce80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
5ce90 6f 6d 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20  ome combination 
5cea0 6f 66 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 2a  of SQLITE_FUNC_*
5ceb0 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 55 73 65   */.  void *pUse
5cec0 72 44 61 74 61 3b 20 20 20 20 20 2f 2a 20 55 73  rData;     /* Us
5ced0 65 72 20 64 61 74 61 20 70 61 72 61 6d 65 74 65  er data paramete
5cee0 72 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66 20 2a  r */.  FuncDef *
5cef0 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e  pNext;      /* N
5cf00 65 78 74 20 66 75 6e 63 74 69 6f 6e 20 77 69 74  ext function wit
5cf10 68 20 73 61 6d 65 20 6e 61 6d 65 20 2a 2f 0a 20  h same name */. 
5cf20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73   void (*xFunc)(s
5cf30 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
5cf40 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
5cf50 65 2a 2a 29 3b 20 2f 2a 20 52 65 67 75 6c 61 72  e**); /* Regular
5cf60 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76   function */.  v
5cf70 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c  oid (*xStep)(sql
5cf80 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
5cf90 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  t,sqlite3_value*
5cfa0 2a 29 3b 20 2f 2a 20 41 67 67 72 65 67 61 74 65  *); /* Aggregate
5cfb0 20 73 74 65 70 20 2a 2f 0a 20 20 76 6f 69 64 20   step */.  void 
5cfc0 28 2a 78 46 69 6e 61 6c 69 7a 65 29 28 73 71 6c  (*xFinalize)(sql
5cfd0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 20  ite3_context*); 
5cfe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5cff0 2a 20 41 67 67 72 65 67 61 74 65 20 66 69 6e 61  * Aggregate fina
5d000 6c 69 7a 65 72 20 2a 2f 0a 20 20 63 68 61 72 20  lizer */.  char 
5d010 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20  *zName;         
5d020 2f 2a 20 53 51 4c 20 6e 61 6d 65 20 6f 66 20 74  /* SQL name of t
5d030 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 2a 2f 0a  he function. */.
5d040 20 20 46 75 6e 63 44 65 66 20 2a 70 48 61 73 68    FuncDef *pHash
5d050 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 77  ;      /* Next w
5d060 69 74 68 20 61 20 64 69 66 66 65 72 65 6e 74 20  ith a different 
5d070 6e 61 6d 65 20 62 75 74 20 74 68 65 20 73 61 6d  name but the sam
5d080 65 20 68 61 73 68 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  e hash */.};../*
5d090 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c  .** Possible val
5d0a0 75 65 73 20 66 6f 72 20 46 75 6e 63 44 65 66 2e  ues for FuncDef.
5d0b0 66 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 6e 65  flags.*/.#define
5d0c0 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b   SQLITE_FUNC_LIK
5d0d0 45 20 20 20 20 20 30 78 30 31 20 2f 2a 20 43 61  E     0x01 /* Ca
5d0e0 6e 64 69 64 61 74 65 20 66 6f 72 20 74 68 65 20  ndidate for the 
5d0f0 4c 49 4b 45 20 6f 70 74 69 6d 69 7a 61 74 69 6f  LIKE optimizatio
5d100 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  n */.#define SQL
5d110 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 20 20 20  ITE_FUNC_CASE   
5d120 20 20 30 78 30 32 20 2f 2a 20 43 61 73 65 2d 73    0x02 /* Case-s
5d130 65 6e 73 69 74 69 76 65 20 4c 49 4b 45 2d 74 79  ensitive LIKE-ty
5d140 70 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 23  pe function */.#
5d150 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55  define SQLITE_FU
5d160 4e 43 5f 45 50 48 45 4d 20 20 20 20 30 78 30 34  NC_EPHEM    0x04
5d170 20 2f 2a 20 45 70 68 65 6d 65 72 61 6c 2e 20 20   /* Ephemeral.  
5d180 44 65 6c 65 74 65 20 77 69 74 68 20 56 44 42 45  Delete with VDBE
5d190 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
5d1a0 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c  TE_FUNC_NEEDCOLL
5d1b0 20 30 78 30 38 20 2f 2a 20 73 71 6c 69 74 65 33   0x08 /* sqlite3
5d1c0 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 29  GetFuncCollSeq()
5d1d0 20 6d 69 67 68 74 20 62 65 20 63 61 6c 6c 65 64   might be called
5d1e0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
5d1f0 54 45 5f 46 55 4e 43 5f 50 52 49 56 41 54 45 20  TE_FUNC_PRIVATE 
5d200 20 30 78 31 30 20 2f 2a 20 41 6c 6c 6f 77 65 64   0x10 /* Allowed
5d210 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73   for internal us
5d220 65 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e  e only */.#defin
5d230 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 4f  e SQLITE_FUNC_CO
5d240 55 4e 54 20 20 20 20 30 78 32 30 20 2f 2a 20 42  UNT    0x20 /* B
5d250 75 69 6c 74 2d 69 6e 20 63 6f 75 6e 74 28 2a 29  uilt-in count(*)
5d260 20 61 67 67 72 65 67 61 74 65 20 2a 2f 0a 0a 2f   aggregate */../
5d270 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
5d280 6e 67 20 74 68 72 65 65 20 6d 61 63 72 6f 73 2c  ng three macros,
5d290 20 46 55 4e 43 54 49 4f 4e 28 29 2c 20 4c 49 4b   FUNCTION(), LIK
5d2a0 45 46 55 4e 43 28 29 20 61 6e 64 20 41 47 47 52  EFUNC() and AGGR
5d2b0 45 47 41 54 45 28 29 20 61 72 65 0a 2a 2a 20 75  EGATE() are.** u
5d2c0 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 74 68  sed to create th
5d2d0 65 20 69 6e 69 74 69 61 6c 69 7a 65 72 73 20 66  e initializers f
5d2e0 6f 72 20 74 68 65 20 46 75 6e 63 44 65 66 20 73  or the FuncDef s
5d2f0 74 72 75 63 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a  tructures..**.**
5d300 20 20 20 46 55 4e 43 54 49 4f 4e 28 7a 4e 61 6d     FUNCTION(zNam
5d310 65 2c 20 6e 41 72 67 2c 20 69 41 72 67 2c 20 62  e, nArg, iArg, b
5d320 4e 43 2c 20 78 46 75 6e 63 29 0a 2a 2a 20 20 20  NC, xFunc).**   
5d330 20 20 55 73 65 64 20 74 6f 20 63 72 65 61 74 65    Used to create
5d340 20 61 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69   a scalar functi
5d350 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66  on definition of
5d360 20 61 20 66 75 6e 63 74 69 6f 6e 20 7a 4e 61 6d   a function zNam
5d370 65 20 0a 2a 2a 20 20 20 20 20 69 6d 70 6c 65 6d  e .**     implem
5d380 65 6e 74 65 64 20 62 79 20 43 20 66 75 6e 63 74  ented by C funct
5d390 69 6f 6e 20 78 46 75 6e 63 20 74 68 61 74 20 61  ion xFunc that a
5d3a0 63 63 65 70 74 73 20 6e 41 72 67 20 61 72 67 75  ccepts nArg argu
5d3b0 6d 65 6e 74 73 2e 20 54 68 65 0a 2a 2a 20 20 20  ments. The.**   
5d3c0 20 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61    value passed a
5d3d0 73 20 69 41 72 67 20 69 73 20 63 61 73 74 20 74  s iArg is cast t
5d3e0 6f 20 61 20 28 76 6f 69 64 2a 29 20 61 6e 64 20  o a (void*) and 
5d3f0 6d 61 64 65 20 61 76 61 69 6c 61 62 6c 65 0a 2a  made available.*
5d400 2a 20 20 20 20 20 61 73 20 74 68 65 20 75 73 65  *     as the use
5d410 72 2d 64 61 74 61 20 28 73 71 6c 69 74 65 33 5f  r-data (sqlite3_
5d420 75 73 65 72 5f 64 61 74 61 28 29 29 20 66 6f 72  user_data()) for
5d430 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 49   the function. I
5d440 66 20 0a 2a 2a 20 20 20 20 20 61 72 67 75 6d 65  f .**     argume
5d450 6e 74 20 62 4e 43 20 69 73 20 74 72 75 65 2c 20  nt bNC is true, 
5d460 74 68 65 6e 20 74 68 65 20 53 51 4c 49 54 45 5f  then the SQLITE_
5d470 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 66 6c  FUNC_NEEDCOLL fl
5d480 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 0a 2a 2a  ag is set..**.**
5d490 20 20 20 41 47 47 52 45 47 41 54 45 28 7a 4e 61     AGGREGATE(zNa
5d4a0 6d 65 2c 20 6e 41 72 67 2c 20 69 41 72 67 2c 20  me, nArg, iArg, 
5d4b0 62 4e 43 2c 20 78 53 74 65 70 2c 20 78 46 69 6e  bNC, xStep, xFin
5d4c0 61 6c 29 0a 2a 2a 20 20 20 20 20 55 73 65 64 20  al).**     Used 
5d4d0 74 6f 20 63 72 65 61 74 65 20 61 6e 20 61 67 67  to create an agg
5d4e0 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20  regate function 
5d4f0 64 65 66 69 6e 69 74 69 6f 6e 20 69 6d 70 6c 65  definition imple
5d500 6d 65 6e 74 65 64 20 62 79 0a 2a 2a 20 20 20 20  mented by.**    
5d510 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73   the C functions
5d520 20 78 53 74 65 70 20 61 6e 64 20 78 46 69 6e 61   xStep and xFina
5d530 6c 2e 20 54 68 65 20 66 69 72 73 74 20 66 6f 75  l. The first fou
5d540 72 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20  r parameters.** 
5d550 20 20 20 20 61 72 65 20 69 6e 74 65 72 70 72 65      are interpre
5d560 74 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20  ted in the same 
5d570 77 61 79 20 61 73 20 74 68 65 20 66 69 72 73 74  way as the first
5d580 20 34 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f   4 parameters to
5d590 0a 2a 2a 20 20 20 20 20 46 55 4e 43 54 49 4f 4e  .**     FUNCTION
5d5a0 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 4c 49 4b 45  ()..**.**   LIKE
5d5b0 46 55 4e 43 28 7a 4e 61 6d 65 2c 20 6e 41 72 67  FUNC(zName, nArg
5d5c0 2c 20 70 41 72 67 2c 20 66 6c 61 67 73 29 0a 2a  , pArg, flags).*
5d5d0 2a 20 20 20 20 20 55 73 65 64 20 74 6f 20 63 72  *     Used to cr
5d5e0 65 61 74 65 20 61 20 73 63 61 6c 61 72 20 66 75  eate a scalar fu
5d5f0 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f  nction definitio
5d600 6e 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 20  n of a function 
5d610 7a 4e 61 6d 65 20 0a 2a 2a 20 20 20 20 20 74 68  zName .**     th
5d620 61 74 20 61 63 63 65 70 74 73 20 6e 41 72 67 20  at accepts nArg 
5d630 61 72 67 75 6d 65 6e 74 73 20 61 6e 64 20 69 73  arguments and is
5d640 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20   implemented by 
5d650 61 20 63 61 6c 6c 20 74 6f 20 43 20 0a 2a 2a 20  a call to C .** 
5d660 20 20 20 20 66 75 6e 63 74 69 6f 6e 20 6c 69 6b      function lik
5d670 65 46 75 6e 63 2e 20 41 72 67 75 6d 65 6e 74 20  eFunc. Argument 
5d680 70 41 72 67 20 69 73 20 63 61 73 74 20 74 6f 20  pArg is cast to 
5d690 61 20 28 76 6f 69 64 20 2a 29 20 61 6e 64 20 6d  a (void *) and m
5d6a0 61 64 65 0a 2a 2a 20 20 20 20 20 61 76 61 69 6c  ade.**     avail
5d6b0 61 62 6c 65 20 61 73 20 74 68 65 20 66 75 6e 63  able as the func
5d6c0 74 69 6f 6e 20 75 73 65 72 2d 64 61 74 61 20 28  tion user-data (
5d6d0 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74  sqlite3_user_dat
5d6e0 61 28 29 29 2e 20 54 68 65 0a 2a 2a 20 20 20 20  a()). The.**    
5d6f0 20 46 75 6e 63 44 65 66 2e 66 6c 61 67 73 20 76   FuncDef.flags v
5d700 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 74  ariable is set t
5d710 6f 20 74 68 65 20 76 61 6c 75 65 20 70 61 73 73  o the value pass
5d720 65 64 20 61 73 20 74 68 65 20 66 6c 61 67 73 0a  ed as the flags.
5d730 2a 2a 20 20 20 20 20 70 61 72 61 6d 65 74 65 72  **     parameter
5d740 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 55 4e  ..*/.#define FUN
5d750 43 54 49 4f 4e 28 7a 4e 61 6d 65 2c 20 6e 41 72  CTION(zName, nAr
5d760 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20 78 46  g, iArg, bNC, xF
5d770 75 6e 63 29 20 5c 0a 20 20 7b 6e 41 72 67 2c 20  unc) \.  {nArg, 
5d780 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 62 4e 43  SQLITE_UTF8, bNC
5d790 2a 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45  *SQLITE_FUNC_NEE
5d7a0 44 43 4f 4c 4c 2c 20 5c 0a 20 20 20 53 51 4c 49  DCOLL, \.   SQLI
5d7b0 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 69 41  TE_INT_TO_PTR(iA
5d7c0 72 67 29 2c 20 30 2c 20 78 46 75 6e 63 2c 20 30  rg), 0, xFunc, 0
5d7d0 2c 20 30 2c 20 23 7a 4e 61 6d 65 2c 20 30 7d 0a  , 0, #zName, 0}.
5d7e0 23 64 65 66 69 6e 65 20 53 54 52 5f 46 55 4e 43  #define STR_FUNC
5d7f0 54 49 4f 4e 28 7a 4e 61 6d 65 2c 20 6e 41 72 67  TION(zName, nArg
5d800 2c 20 70 41 72 67 2c 20 62 4e 43 2c 20 78 46 75  , pArg, bNC, xFu
5d810 6e 63 29 20 5c 0a 20 20 7b 6e 41 72 67 2c 20 53  nc) \.  {nArg, S
5d820 51 4c 49 54 45 5f 55 54 46 38 2c 20 62 4e 43 2a  QLITE_UTF8, bNC*
5d830 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44  SQLITE_FUNC_NEED
5d840 43 4f 4c 4c 2c 20 5c 0a 20 20 20 70 41 72 67 2c  COLL, \.   pArg,
5d850 20 30 2c 20 78 46 75 6e 63 2c 20 30 2c 20 30 2c   0, xFunc, 0, 0,
5d860 20 23 7a 4e 61 6d 65 2c 20 30 7d 0a 23 64 65 66   #zName, 0}.#def
5d870 69 6e 65 20 4c 49 4b 45 46 55 4e 43 28 7a 4e 61  ine LIKEFUNC(zNa
5d880 6d 65 2c 20 6e 41 72 67 2c 20 61 72 67 2c 20 66  me, nArg, arg, f
5d890 6c 61 67 73 29 20 5c 0a 20 20 7b 6e 41 72 67 2c  lags) \.  {nArg,
5d8a0 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 66 6c   SQLITE_UTF8, fl
5d8b0 61 67 73 2c 20 28 76 6f 69 64 20 2a 29 61 72 67  ags, (void *)arg
5d8c0 2c 20 30 2c 20 6c 69 6b 65 46 75 6e 63 2c 20 30  , 0, likeFunc, 0
5d8d0 2c 20 30 2c 20 23 7a 4e 61 6d 65 2c 20 30 7d 0a  , 0, #zName, 0}.
5d8e0 23 64 65 66 69 6e 65 20 41 47 47 52 45 47 41 54  #define AGGREGAT
5d8f0 45 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 61  E(zName, nArg, a
5d900 72 67 2c 20 6e 63 2c 20 78 53 74 65 70 2c 20 78  rg, nc, xStep, x
5d910 46 69 6e 61 6c 29 20 5c 0a 20 20 7b 6e 41 72 67  Final) \.  {nArg
5d920 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 6e  , SQLITE_UTF8, n
5d930 63 2a 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45  c*SQLITE_FUNC_NE
5d940 45 44 43 4f 4c 4c 2c 20 5c 0a 20 20 20 53 51 4c  EDCOLL, \.   SQL
5d950 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 61  ITE_INT_TO_PTR(a
5d960 72 67 29 2c 20 30 2c 20 30 2c 20 78 53 74 65 70  rg), 0, 0, xStep
5d970 2c 78 46 69 6e 61 6c 2c 23 7a 4e 61 6d 65 2c 30  ,xFinal,#zName,0
5d980 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 63 75 72  }../*.** All cur
5d990 72 65 6e 74 20 73 61 76 65 70 6f 69 6e 74 73 20  rent savepoints 
5d9a0 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20  are stored in a 
5d9b0 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73 74 61 72  linked list star
5d9c0 74 69 6e 67 20 61 74 0a 2a 2a 20 73 71 6c 69 74  ting at.** sqlit
5d9d0 65 33 2e 70 53 61 76 65 70 6f 69 6e 74 2e 20 54  e3.pSavepoint. T
5d9e0 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74  he first element
5d9f0 20 69 6e 20 74 68 65 20 6c 69 73 74 20 69 73 20   in the list is 
5da00 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c  the most recentl
5da10 79 0a 2a 2a 20 6f 70 65 6e 65 64 20 73 61 76 65  y.** opened save
5da20 70 6f 69 6e 74 2e 20 53 61 76 65 70 6f 69 6e 74  point. Savepoint
5da30 73 20 61 72 65 20 61 64 64 65 64 20 74 6f 20 74  s are added to t
5da40 68 65 20 6c 69 73 74 20 62 79 20 74 68 65 20 76  he list by the v
5da50 64 62 65 0a 2a 2a 20 4f 50 5f 53 61 76 65 70 6f  dbe.** OP_Savepo
5da60 69 6e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  int instruction.
5da70 0a 2a 2f 0a 73 74 72 75 63 74 20 53 61 76 65 70  .*/.struct Savep
5da80 6f 69 6e 74 20 7b 0a 20 20 63 68 61 72 20 2a 7a  oint {.  char *z
5da90 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20  Name;           
5daa0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5dab0 53 61 76 65 70 6f 69 6e 74 20 6e 61 6d 65 20 28  Savepoint name (
5dac0 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 29 20  nul-terminated) 
5dad0 2a 2f 0a 20 20 53 61 76 65 70 6f 69 6e 74 20 2a  */.  Savepoint *
5dae0 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20  pNext;          
5daf0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 65           /* Pare
5db00 6e 74 20 73 61 76 65 70 6f 69 6e 74 20 28 69 66  nt savepoint (if
5db10 20 61 6e 79 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a   any) */.};../*.
5db20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
5db30 20 61 72 65 20 75 73 65 64 20 61 73 20 74 68 65   are used as the
5db40 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   second paramete
5db50 72 20 74 6f 20 73 71 6c 69 74 65 33 53 61 76 65  r to sqlite3Save
5db60 70 6f 69 6e 74 28 29 2c 0a 2a 2a 20 61 6e 64 20  point(),.** and 
5db70 61 73 20 74 68 65 20 50 31 20 61 72 67 75 6d 65  as the P1 argume
5db80 6e 74 20 74 6f 20 74 68 65 20 4f 50 5f 53 61 76  nt to the OP_Sav
5db90 65 70 6f 69 6e 74 20 69 6e 73 74 72 75 63 74 69  epoint instructi
5dba0 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  on..*/.#define S
5dbb0 41 56 45 50 4f 49 4e 54 5f 42 45 47 49 4e 20 20  AVEPOINT_BEGIN  
5dbc0 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 41      0.#define SA
5dbd0 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20  VEPOINT_RELEASE 
5dbe0 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 41 56     1.#define SAV
5dbf0 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20  EPOINT_ROLLBACK 
5dc00 20 20 32 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68    2.../*.** Each
5dc10 20 53 51 4c 69 74 65 20 6d 6f 64 75 6c 65 20 28   SQLite module (
5dc20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 64 65  virtual table de
5dc30 66 69 6e 69 74 69 6f 6e 29 20 69 73 20 64 65 66  finition) is def
5dc40 69 6e 65 64 20 62 79 20 61 6e 0a 2a 2a 20 69 6e  ined by an.** in
5dc50 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
5dc60 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
5dc70 65 2c 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  e, stored in the
5dc80 20 73 71 6c 69 74 65 33 2e 61 4d 6f 64 75 6c 65   sqlite3.aModule
5dc90 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 2e 0a  .** hash table..
5dca0 2a 2f 0a 73 74 72 75 63 74 20 4d 6f 64 75 6c 65  */.struct Module
5dcb0 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74   {.  const sqlit
5dcc0 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75  e3_module *pModu
5dcd0 6c 65 3b 20 20 20 20 20 20 20 2f 2a 20 43 61 6c  le;       /* Cal
5dce0 6c 62 61 63 6b 20 70 6f 69 6e 74 65 72 73 20 2a  lback pointers *
5dcf0 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
5dd00 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20  zName;          
5dd10 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
5dd20 20 70 61 73 73 65 64 20 74 6f 20 63 72 65 61 74   passed to creat
5dd30 65 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a 20 20  e_module() */.  
5dd40 76 6f 69 64 20 2a 70 41 75 78 3b 20 20 20 20 20  void *pAux;     
5dd50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5dd60 20 20 20 20 20 2f 2a 20 70 41 75 78 20 70 61 73       /* pAux pas
5dd70 73 65 64 20 74 6f 20 63 72 65 61 74 65 5f 6d 6f  sed to create_mo
5dd80 64 75 6c 65 28 29 20 2a 2f 0a 20 20 76 6f 69 64  dule() */.  void
5dd90 20 28 2a 78 44 65 73 74 72 6f 79 29 28 76 6f 69   (*xDestroy)(voi
5dda0 64 20 2a 29 3b 20 20 20 20 20 20 20 20 20 20 20  d *);           
5ddb0 20 2f 2a 20 4d 6f 64 75 6c 65 20 64 65 73 74 72   /* Module destr
5ddc0 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a  uctor function *
5ddd0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 69 6e 66 6f  /.};../*.** info
5dde0 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 61  rmation about ea
5ddf0 63 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 6e 20  ch column of an 
5de00 53 51 4c 20 74 61 62 6c 65 20 69 73 20 68 65 6c  SQL table is hel
5de10 64 20 69 6e 20 61 6e 20 69 6e 73 74 61 6e 63 65  d in an instance
5de20 0a 2a 2a 20 6f 66 20 74 68 69 73 20 73 74 72 75  .** of this stru
5de30 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74  cture..*/.struct
5de40 20 43 6f 6c 75 6d 6e 20 7b 0a 20 20 63 68 61 72   Column {.  char
5de50 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 2f 2a 20   *zName;     /* 
5de60 4e 61 6d 65 20 6f 66 20 74 68 69 73 20 63 6f 6c  Name of this col
5de70 75 6d 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  umn */.  Expr *p
5de80 44 66 6c 74 3b 20 20 20 20 20 2f 2a 20 44 65 66  Dflt;     /* Def
5de90 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 74 68  ault value of th
5dea0 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 63  is column */.  c
5deb0 68 61 72 20 2a 7a 44 66 6c 74 3b 20 20 20 20 20  har *zDflt;     
5dec0 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 74 65 78 74  /* Original text
5ded0 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74 20   of the default 
5dee0 76 61 6c 75 65 20 2a 2f 0a 20 20 63 68 61 72 20  value */.  char 
5def0 2a 7a 54 79 70 65 3b 20 20 20 20 20 2f 2a 20 44  *zType;     /* D
5df00 61 74 61 20 74 79 70 65 20 66 6f 72 20 74 68 69  ata type for thi
5df10 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 63 68  s column */.  ch
5df20 61 72 20 2a 7a 43 6f 6c 6c 3b 20 20 20 20 20 2f  ar *zColl;     /
5df30 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  * Collating sequ
5df40 65 6e 63 65 2e 20 20 49 66 20 4e 55 4c 4c 2c 20  ence.  If NULL, 
5df50 75 73 65 20 74 68 65 20 64 65 66 61 75 6c 74 20  use the default 
5df60 2a 2f 0a 20 20 75 38 20 6e 6f 74 4e 75 6c 6c 3b  */.  u8 notNull;
5df70 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
5df80 20 74 68 65 72 65 20 69 73 20 61 20 4e 4f 54 20   there is a NOT 
5df90 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 20  NULL constraint 
5dfa0 2a 2f 0a 20 20 75 38 20 69 73 50 72 69 6d 4b 65  */.  u8 isPrimKe
5dfb0 79 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66  y;    /* True if
5dfc0 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 69 73 20   this column is 
5dfd0 70 61 72 74 20 6f 66 20 74 68 65 20 50 52 49 4d  part of the PRIM
5dfe0 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20 63 68 61  ARY KEY */.  cha
5dff0 72 20 61 66 66 69 6e 69 74 79 3b 20 20 20 2f 2a  r affinity;   /*
5e000 20 4f 6e 65 20 6f 66 20 74 68 65 20 53 51 4c 49   One of the SQLI
5e010 54 45 5f 41 46 46 5f 2e 2e 2e 20 76 61 6c 75 65  TE_AFF_... value
5e020 73 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  s */.#ifndef SQL
5e030 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
5e040 54 41 42 4c 45 0a 20 20 75 38 20 69 73 48 69 64  TABLE.  u8 isHid
5e050 64 65 6e 3b 20 20 20 20 20 2f 2a 20 54 72 75 65  den;     /* True
5e060 20 69 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20   if this column 
5e070 69 73 20 27 68 69 64 64 65 6e 27 20 2a 2f 0a 23  is 'hidden' */.#
5e080 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  endif.};../*.** 
5e090 41 20 22 43 6f 6c 6c 61 74 69 6e 67 20 53 65 71  A "Collating Seq
5e0a0 75 65 6e 63 65 22 20 69 73 20 64 65 66 69 6e 65  uence" is define
5e0b0 64 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65  d by an instance
5e0c0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
5e0d0 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20  g.** structure. 
5e0e0 43 6f 6e 63 65 70 74 75 61 6c 6c 79 2c 20 61 20  Conceptually, a 
5e0f0 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
5e100 63 65 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61  ce consists of a
5e110 20 6e 61 6d 65 20 61 6e 64 0a 2a 2a 20 61 20 63   name and.** a c
5e120 6f 6d 70 61 72 69 73 6f 6e 20 72 6f 75 74 69 6e  omparison routin
5e130 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74  e that defines t
5e140 68 65 20 6f 72 64 65 72 20 6f 66 20 74 68 61 74  he order of that
5e150 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a   sequence..**.**
5e160 20 54 68 65 72 65 20 6d 61 79 20 74 77 6f 20 73   There may two s
5e170 65 70 61 72 61 74 65 20 69 6d 70 6c 65 6d 65 6e  eparate implemen
5e180 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 63  tations of the c
5e190 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f  ollation functio
5e1a0 6e 2c 20 6f 6e 65 0a 2a 2a 20 74 68 61 74 20 70  n, one.** that p
5e1b0 72 6f 63 65 73 73 65 73 20 74 65 78 74 20 69 6e  rocesses text in
5e1c0 20 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20   UTF-8 encoding 
5e1d0 28 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 29 20 61  (CollSeq.xCmp) a
5e1e0 6e 64 20 61 6e 6f 74 68 65 72 20 74 68 61 74 0a  nd another that.
5e1f0 2a 2a 20 70 72 6f 63 65 73 73 65 73 20 74 65 78  ** processes tex
5e200 74 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 46  t encoded in UTF
5e210 2d 31 36 20 28 43 6f 6c 6c 53 65 71 2e 78 43 6d  -16 (CollSeq.xCm
5e220 70 31 36 29 2c 20 75 73 69 6e 67 20 74 68 65 20  p16), using the 
5e230 6d 61 63 68 69 6e 65 0a 2a 2a 20 6e 61 74 69 76  machine.** nativ
5e240 65 20 62 79 74 65 20 6f 72 64 65 72 2e 20 57 68  e byte order. Wh
5e250 65 6e 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  en a collation s
5e260 65 71 75 65 6e 63 65 20 69 73 20 69 6e 76 6f 6b  equence is invok
5e270 65 64 2c 20 53 51 4c 69 74 65 20 73 65 6c 65 63  ed, SQLite selec
5e280 74 73 0a 2a 2a 20 74 68 65 20 76 65 72 73 69 6f  ts.** the versio
5e290 6e 20 74 68 61 74 20 77 69 6c 6c 20 72 65 71 75  n that will requ
5e2a0 69 72 65 20 74 68 65 20 6c 65 61 73 74 20 65 78  ire the least ex
5e2b0 70 65 6e 73 69 76 65 20 65 6e 63 6f 64 69 6e 67  pensive encoding
5e2c0 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 69 6f 6e 73  .** translations
5e2d0 2c 20 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20  , if any..**.** 
5e2e0 54 68 65 20 43 6f 6c 6c 53 65 71 2e 70 55 73 65  The CollSeq.pUse
5e2f0 72 20 6d 65 6d 62 65 72 20 76 61 72 69 61 62 6c  r member variabl
5e300 65 20 69 73 20 61 6e 20 65 78 74 72 61 20 70 61  e is an extra pa
5e310 72 61 6d 65 74 65 72 20 74 68 61 74 20 70 61 73  rameter that pas
5e320 73 65 64 20 69 6e 0a 2a 2a 20 61 73 20 74 68 65  sed in.** as the
5e330 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
5e340 74 6f 20 74 68 65 20 55 54 46 2d 38 20 63 6f 6d  to the UTF-8 com
5e350 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e  parison function
5e360 2c 20 78 43 6d 70 2e 0a 2a 2a 20 43 6f 6c 6c 53  , xCmp..** CollS
5e370 65 71 2e 70 55 73 65 72 31 36 20 69 73 20 74 68  eq.pUser16 is th
5e380 65 20 65 71 75 69 76 61 6c 65 6e 74 20 66 6f 72  e equivalent for
5e390 20 74 68 65 20 55 54 46 2d 31 36 20 63 6f 6d 70   the UTF-16 comp
5e3a0 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 2c  arison function,
5e3b0 0a 2a 2a 20 78 43 6d 70 31 36 2e 0a 2a 2a 0a 2a  .** xCmp16..**.*
5e3c0 2a 20 49 66 20 62 6f 74 68 20 43 6f 6c 6c 53 65  * If both CollSe
5e3d0 71 2e 78 43 6d 70 20 61 6e 64 20 43 6f 6c 6c 53  q.xCmp and CollS
5e3e0 65 71 2e 78 43 6d 70 31 36 20 61 72 65 20 4e 55  eq.xCmp16 are NU
5e3f0 4c 4c 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61  LL, it means tha
5e400 74 20 74 68 65 0a 2a 2a 20 63 6f 6c 6c 61 74 69  t the.** collati
5e410 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 75  ng sequence is u
5e420 6e 64 65 66 69 6e 65 64 2e 20 20 49 6e 64 69 63  ndefined.  Indic
5e430 65 73 20 62 75 69 6c 74 20 6f 6e 20 61 6e 20 75  es built on an u
5e440 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 63 6f 6c 6c  ndefined.** coll
5e450 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6d  ating sequence m
5e460 61 79 20 6e 6f 74 20 62 65 20 72 65 61 64 20 6f  ay not be read o
5e470 72 20 77 72 69 74 74 65 6e 2e 0a 2a 2f 0a 73 74  r written..*/.st
5e480 72 75 63 74 20 43 6f 6c 6c 53 65 71 20 7b 0a 20  ruct CollSeq {. 
5e490 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20   char *zName;   
5e4a0 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
5e4b0 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20  f the collating 
5e4c0 73 65 71 75 65 6e 63 65 2c 20 55 54 46 2d 38 20  sequence, UTF-8 
5e4d0 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 75 38 20  encoded */.  u8 
5e4e0 65 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  enc;            
5e4f0 20 20 20 2f 2a 20 54 65 78 74 20 65 6e 63 6f 64     /* Text encod
5e500 69 6e 67 20 68 61 6e 64 6c 65 64 20 62 79 20 78  ing handled by x
5e510 43 6d 70 28 29 20 2a 2f 0a 20 20 75 38 20 74 79  Cmp() */.  u8 ty
5e520 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  pe;             
5e530 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 53   /* One of the S
5e540 51 4c 49 54 45 5f 43 4f 4c 4c 5f 2e 2e 2e 20 76  QLITE_COLL_... v
5e550 61 6c 75 65 73 20 62 65 6c 6f 77 20 2a 2f 0a 20  alues below */. 
5e560 20 76 6f 69 64 20 2a 70 55 73 65 72 3b 20 20 20   void *pUser;   
5e570 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
5e580 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 43 6d 70  argument to xCmp
5e590 28 29 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43  () */.  int (*xC
5e5a0 6d 70 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 20 63  mp)(void*,int, c
5e5b0 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c  onst void*, int,
5e5c0 20 63 6f 6e 73 74 20 76 6f 69 64 2a 29 3b 0a 20   const void*);. 
5e5d0 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f   void (*xDel)(vo
5e5e0 69 64 2a 29 3b 20 20 2f 2a 20 44 65 73 74 72 75  id*);  /* Destru
5e5f0 63 74 6f 72 20 66 6f 72 20 70 55 73 65 72 20 2a  ctor for pUser *
5e600 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  /.};../*.** Allo
5e610 77 65 64 20 76 61 6c 75 65 73 20 6f 66 20 43 6f  wed values of Co
5e620 6c 6c 53 65 71 2e 74 79 70 65 3a 0a 2a 2f 0a 23  llSeq.type:.*/.#
5e630 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f  define SQLITE_CO
5e640 4c 4c 5f 42 49 4e 41 52 59 20 20 31 20 20 2f 2a  LL_BINARY  1  /*
5e650 20 54 68 65 20 64 65 66 61 75 6c 74 20 6d 65 6d   The default mem
5e660 63 6d 70 28 29 20 63 6f 6c 6c 61 74 69 6e 67 20  cmp() collating 
5e670 73 65 71 75 65 6e 63 65 20 2a 2f 0a 23 64 65 66  sequence */.#def
5e680 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f  ine SQLITE_COLL_
5e690 4e 4f 43 41 53 45 20 20 32 20 20 2f 2a 20 54 68  NOCASE  2  /* Th
5e6a0 65 20 62 75 69 6c 74 2d 69 6e 20 4e 4f 43 41 53  e built-in NOCAS
5e6b0 45 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  E collating sequ
5e6c0 65 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ence */.#define 
5e6d0 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 52 45 56 45  SQLITE_COLL_REVE
5e6e0 52 53 45 20 33 20 20 2f 2a 20 54 68 65 20 62 75  RSE 3  /* The bu
5e6f0 69 6c 74 2d 69 6e 20 52 45 56 45 52 53 45 20 63  ilt-in REVERSE c
5e700 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
5e710 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  e */.#define SQL
5e720 49 54 45 5f 43 4f 4c 4c 5f 55 53 45 52 20 20 20  ITE_COLL_USER   
5e730 20 30 20 20 2f 2a 20 41 6e 79 20 6f 74 68 65 72   0  /* Any other
5e740 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20 63 6f   user-defined co
5e750 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
5e760 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 6f 72   */../*.** A sor
5e770 74 20 6f 72 64 65 72 20 63 61 6e 20 62 65 20 65  t order can be e
5e780 69 74 68 65 72 20 41 53 43 20 6f 72 20 44 45 53  ither ASC or DES
5e790 43 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  C..*/.#define SQ
5e7a0 4c 49 54 45 5f 53 4f 5f 41 53 43 20 20 20 20 20  LITE_SO_ASC     
5e7b0 20 20 30 20 20 2f 2a 20 53 6f 72 74 20 69 6e 20    0  /* Sort in 
5e7c0 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20  ascending order 
5e7d0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
5e7e0 45 5f 53 4f 5f 44 45 53 43 20 20 20 20 20 20 31  E_SO_DESC      1
5e7f0 20 20 2f 2a 20 53 6f 72 74 20 69 6e 20 61 73 63    /* Sort in asc
5e800 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 2a 2f 0a  ending order */.
5e810 0a 2f 2a 0a 2a 2a 20 43 6f 6c 75 6d 6e 20 61 66  ./*.** Column af
5e820 66 69 6e 69 74 79 20 74 79 70 65 73 2e 0a 2a 2a  finity types..**
5e830 0a 2a 2a 20 54 68 65 73 65 20 75 73 65 64 20 74  .** These used t
5e840 6f 20 68 61 76 65 20 6d 6e 65 6d 6f 6e 69 63 20  o have mnemonic 
5e850 6e 61 6d 65 20 6c 69 6b 65 20 27 69 27 20 66 6f  name like 'i' fo
5e860 72 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54  r SQLITE_AFF_INT
5e870 45 47 45 52 20 61 6e 64 0a 2a 2a 20 27 74 27 20  EGER and.** 't' 
5e880 66 6f 72 20 53 51 4c 49 54 45 5f 41 46 46 5f 54  for SQLITE_AFF_T
5e890 45 58 54 2e 20 20 42 75 74 20 77 65 20 63 61 6e  EXT.  But we can
5e8a0 20 73 61 76 65 20 61 20 6c 69 74 74 6c 65 20 73   save a little s
5e8b0 70 61 63 65 20 61 6e 64 20 69 6d 70 72 6f 76 65  pace and improve
5e8c0 0a 2a 2a 20 74 68 65 20 73 70 65 65 64 20 61 20  .** the speed a 
5e8d0 6c 69 74 74 6c 65 20 62 79 20 6e 75 6d 62 65 72  little by number
5e8e0 69 6e 67 20 74 68 65 20 76 61 6c 75 65 73 20 63  ing the values c
5e8f0 6f 6e 73 65 63 75 74 69 76 65 6c 79 2e 20 20 0a  onsecutively.  .
5e900 2a 2a 0a 2a 2a 20 42 75 74 20 72 61 74 68 65 72  **.** But rather
5e910 20 74 68 61 6e 20 73 74 61 72 74 20 77 69 74 68   than start with
5e920 20 30 20 6f 72 20 31 2c 20 77 65 20 62 65 67 69   0 or 1, we begi
5e930 6e 20 77 69 74 68 20 27 61 27 2e 20 20 54 68 61  n with 'a'.  Tha
5e940 74 20 77 61 79 2c 0a 2a 2a 20 77 68 65 6e 20 6d  t way,.** when m
5e950 75 6c 74 69 70 6c 65 20 61 66 66 69 6e 69 74 79  ultiple affinity
5e960 20 74 79 70 65 73 20 61 72 65 20 63 6f 6e 63 61   types are conca
5e970 74 65 6e 61 74 65 64 20 69 6e 74 6f 20 61 20 73  tenated into a s
5e980 74 72 69 6e 67 20 61 6e 64 0a 2a 2a 20 75 73 65  tring and.** use
5e990 64 20 61 73 20 74 68 65 20 50 34 20 6f 70 65 72  d as the P4 oper
5e9a0 61 6e 64 2c 20 74 68 65 79 20 77 69 6c 6c 20 62  and, they will b
5e9b0 65 20 6d 6f 72 65 20 72 65 61 64 61 62 6c 65 2e  e more readable.
5e9c0 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 61 6c 73 6f  .**.** Note also
5e9d0 20 74 68 61 74 20 74 68 65 20 6e 75 6d 65 72 69   that the numeri
5e9e0 63 20 74 79 70 65 73 20 61 72 65 20 67 72 6f 75  c types are grou
5e9f0 70 65 64 20 74 6f 67 65 74 68 65 72 20 73 6f 20  ped together so 
5ea00 74 68 61 74 20 74 65 73 74 69 6e 67 0a 2a 2a 20  that testing.** 
5ea10 66 6f 72 20 61 20 6e 75 6d 65 72 69 63 20 74 79  for a numeric ty
5ea20 70 65 20 69 73 20 61 20 73 69 6e 67 6c 65 20 63  pe is a single c
5ea30 6f 6d 70 61 72 69 73 6f 6e 2e 0a 2a 2f 0a 23 64  omparison..*/.#d
5ea40 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46  efine SQLITE_AFF
5ea50 5f 54 45 58 54 20 20 20 20 20 27 61 27 0a 23 64  _TEXT     'a'.#d
5ea60 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46  efine SQLITE_AFF
5ea70 5f 4e 4f 4e 45 20 20 20 20 20 27 62 27 0a 23 64  _NONE     'b'.#d
5ea80 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46  efine SQLITE_AFF
5ea90 5f 4e 55 4d 45 52 49 43 20 20 27 63 27 0a 23 64  _NUMERIC  'c'.#d
5eaa0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46  efine SQLITE_AFF
5eab0 5f 49 4e 54 45 47 45 52 20 20 27 64 27 0a 23 64  _INTEGER  'd'.#d
5eac0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46  efine SQLITE_AFF
5ead0 5f 52 45 41 4c 20 20 20 20 20 27 65 27 0a 0a 23  _REAL     'e'..#
5eae0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73  define sqlite3Is
5eaf0 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28  NumericAffinity(
5eb00 58 29 20 20 28 28 58 29 3e 3d 53 51 4c 49 54 45  X)  ((X)>=SQLITE
5eb10 5f 41 46 46 5f 4e 55 4d 45 52 49 43 29 0a 0a 2f  _AFF_NUMERIC)../
5eb20 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f  *.** The SQLITE_
5eb30 41 46 46 5f 4d 41 53 4b 20 76 61 6c 75 65 73 20  AFF_MASK values 
5eb40 6d 61 73 6b 73 20 6f 66 66 20 74 68 65 20 73 69  masks off the si
5eb50 67 6e 69 66 69 63 61 6e 74 20 62 69 74 73 20 6f  gnificant bits o
5eb60 66 20 61 6e 0a 2a 2a 20 61 66 66 69 6e 69 74 79  f an.** affinity
5eb70 20 76 61 6c 75 65 2e 20 0a 2a 2f 0a 23 64 65 66   value. .*/.#def
5eb80 69 6e 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d  ine SQLITE_AFF_M
5eb90 41 53 4b 20 20 20 20 20 30 78 36 37 0a 0a 2f 2a  ASK     0x67../*
5eba0 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 62  .** Additional b
5ebb0 69 74 20 76 61 6c 75 65 73 20 74 68 61 74 20 63  it values that c
5ebc0 61 6e 20 62 65 20 4f 52 65 64 20 77 69 74 68 20  an be ORed with 
5ebd0 61 6e 20 61 66 66 69 6e 69 74 79 20 77 69 74 68  an affinity with
5ebe0 6f 75 74 0a 2a 2a 20 63 68 61 6e 67 69 6e 67 20  out.** changing 
5ebf0 74 68 65 20 61 66 66 69 6e 69 74 79 2e 0a 2a 2f  the affinity..*/
5ec00 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
5ec10 4a 55 4d 50 49 46 4e 55 4c 4c 20 20 20 30 78 30  JUMPIFNULL   0x0
5ec20 38 20 20 2f 2a 20 6a 75 6d 70 73 20 69 66 20 65  8  /* jumps if e
5ec30 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73  ither operand is
5ec40 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65   NULL */.#define
5ec50 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 20   SQLITE_STOREP2 
5ec60 20 20 20 20 20 30 78 31 30 20 20 2f 2a 20 53 74       0x10  /* St
5ec70 6f 72 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65  ore result in re
5ec80 67 5b 50 32 5d 20 72 61 74 68 65 72 20 74 68 61  g[P2] rather tha
5ec90 6e 20 6a 75 6d 70 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  n jump */../*.**
5eca0 20 41 6e 20 6f 62 6a 65 63 74 20 6f 66 20 74 68   An object of th
5ecb0 69 73 20 74 79 70 65 20 69 73 20 63 72 65 61 74  is type is creat
5ecc0 65 64 20 66 6f 72 20 65 61 63 68 20 76 69 72 74  ed for each virt
5ecd0 75 61 6c 20 74 61 62 6c 65 20 70 72 65 73 65 6e  ual table presen
5ece0 74 20 69 6e 0a 2a 2a 20 74 68 65 20 64 61 74 61  t in.** the data
5ecf0 62 61 73 65 20 73 63 68 65 6d 61 2e 20 0a 2a 2a  base schema. .**
5ed00 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61 62  .** If the datab
5ed10 61 73 65 20 73 63 68 65 6d 61 20 69 73 20 73 68  ase schema is sh
5ed20 61 72 65 64 2c 20 74 68 65 6e 20 74 68 65 72 65  ared, then there
5ed30 20 69 73 20 6f 6e 65 20 69 6e 73 74 61 6e 63 65   is one instance
5ed40 20 6f 66 20 74 68 69 73 0a 2a 2a 20 73 74 72 75   of this.** stru
5ed50 63 74 75 72 65 20 66 6f 72 20 65 61 63 68 20 64  cture for each d
5ed60 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
5ed70 6f 6e 20 28 73 71 6c 69 74 65 33 2a 29 20 74 68  on (sqlite3*) th
5ed80 61 74 20 75 73 65 73 20 74 68 65 20 73 68 61 72  at uses the shar
5ed90 65 64 0a 2a 2a 20 73 63 68 65 6d 61 2e 20 54 68  ed.** schema. Th
5eda0 69 73 20 69 73 20 62 65 63 61 75 73 65 20 65 61  is is because ea
5edb0 63 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ch database conn
5edc0 65 63 74 69 6f 6e 20 72 65 71 75 69 72 65 73 20  ection requires 
5edd0 69 74 73 20 6f 77 6e 20 75 6e 69 71 75 65 0a 2a  its own unique.*
5ede0 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  * instance of th
5edf0 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62 2a 20  e sqlite3_vtab* 
5ee00 68 61 6e 64 6c 65 20 75 73 65 64 20 74 6f 20 61  handle used to a
5ee10 63 63 65 73 73 20 74 68 65 20 76 69 72 74 75 61  ccess the virtua
5ee20 6c 20 74 61 62 6c 65 20 0a 2a 2a 20 69 6d 70 6c  l table .** impl
5ee30 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 73 71 6c 69  ementation. sqli
5ee40 74 65 33 5f 76 74 61 62 2a 20 68 61 6e 64 6c 65  te3_vtab* handle
5ee50 73 20 63 61 6e 20 6e 6f 74 20 62 65 20 73 68 61  s can not be sha
5ee60 72 65 64 20 62 65 74 77 65 65 6e 20 0a 2a 2a 20  red between .** 
5ee70 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
5ee80 69 6f 6e 73 2c 20 65 76 65 6e 20 77 68 65 6e 20  ions, even when 
5ee90 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 65 20  the rest of the 
5eea0 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61  in-memory databa
5eeb0 73 65 20 0a 2a 2a 20 73 63 68 65 6d 61 20 69 73  se .** schema is
5eec0 20 73 68 61 72 65 64 2c 20 61 73 20 74 68 65 20   shared, as the 
5eed0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
5eee0 66 74 65 6e 20 73 74 6f 72 65 73 20 74 68 65 20  ften stores the 
5eef0 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e  database.** conn
5ef00 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 70 61  ection handle pa
5ef10 73 73 65 64 20 74 6f 20 69 74 20 76 69 61 20 74  ssed to it via t
5ef20 68 65 20 78 43 6f 6e 6e 65 63 74 28 29 20 6f 72  he xConnect() or
5ef30 20 78 43 72 65 61 74 65 28 29 20 6d 65 74 68 6f   xCreate() metho
5ef40 64 0a 2a 2a 20 64 75 72 69 6e 67 20 69 6e 69 74  d.** during init
5ef50 69 61 6c 69 7a 61 74 69 6f 6e 20 69 6e 74 65 72  ialization inter
5ef60 6e 61 6c 6c 79 2e 20 54 68 69 73 20 64 61 74 61  nally. This data
5ef70 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
5ef80 68 61 6e 64 6c 65 20 6d 61 79 0a 2a 2a 20 74 68  handle may.** th
5ef90 65 6e 20 75 73 65 64 20 62 79 20 74 68 65 20 76  en used by the v
5efa0 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6d 70  irtual table imp
5efb0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 6f 20 61  lementation to a
5efc0 63 63 65 73 73 20 72 65 61 6c 20 74 61 62 6c 65  ccess real table
5efd0 73 20 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65  s .** within the
5efe0 20 64 61 74 61 62 61 73 65 2e 20 53 6f 20 74 68   database. So th
5eff0 61 74 20 74 68 65 79 20 61 70 70 65 61 72 20 61  at they appear a
5f000 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 63 61  s part of the ca
5f010 6c 6c 65 72 73 20 0a 2a 2a 20 74 72 61 6e 73 61  llers .** transa
5f020 63 74 69 6f 6e 2c 20 74 68 65 73 65 20 61 63 63  ction, these acc
5f030 65 73 73 65 73 20 6e 65 65 64 20 74 6f 20 62 65  esses need to be
5f040 20 6d 61 64 65 20 76 69 61 20 74 68 65 20 73 61   made via the sa
5f050 6d 65 20 64 61 74 61 62 61 73 65 20 0a 2a 2a 20  me database .** 
5f060 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 73 20 74 68  connection as th
5f070 61 74 20 75 73 65 64 20 74 6f 20 65 78 65 63 75  at used to execu
5f080 74 65 20 53 51 4c 20 6f 70 65 72 61 74 69 6f 6e  te SQL operation
5f090 73 20 6f 6e 20 74 68 65 20 76 69 72 74 75 61 6c  s on the virtual
5f0a0 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 6c   table..**.** Al
5f0b0 6c 20 56 54 61 62 6c 65 20 6f 62 6a 65 63 74 73  l VTable objects
5f0c0 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64   that correspond
5f0d0 20 74 6f 20 61 20 73 69 6e 67 6c 65 20 74 61 62   to a single tab
5f0e0 6c 65 20 69 6e 20 61 20 73 68 61 72 65 64 0a 2a  le in a shared.*
5f0f0 2a 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  * database schem
5f100 61 20 61 72 65 20 69 6e 69 74 69 61 6c 6c 79 20  a are initially 
5f110 73 74 6f 72 65 64 20 69 6e 20 61 20 6c 69 6e 6b  stored in a link
5f120 65 64 2d 6c 69 73 74 20 70 6f 69 6e 74 65 64 20  ed-list pointed 
5f130 74 6f 20 62 79 0a 2a 2a 20 74 68 65 20 54 61 62  to by.** the Tab
5f140 6c 65 2e 70 56 54 61 62 6c 65 20 6d 65 6d 62 65  le.pVTable membe
5f150 72 20 76 61 72 69 61 62 6c 65 20 6f 66 20 74 68  r variable of th
5f160 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
5f170 54 61 62 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a 2a  Table object..**
5f180 20 57 68 65 6e 20 61 6e 20 73 71 6c 69 74 65 33   When an sqlite3
5f190 5f 70 72 65 70 61 72 65 28 29 20 6f 70 65 72 61  _prepare() opera
5f1a0 74 69 6f 6e 20 69 73 20 72 65 71 75 69 72 65 64  tion is required
5f1b0 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 76   to access the v
5f1c0 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 2c  irtual.** table,
5f1d0 20 69 74 20 73 65 61 72 63 68 65 73 20 74 68 65   it searches the
5f1e0 20 6c 69 73 74 20 66 6f 72 20 74 68 65 20 56 54   list for the VT
5f1f0 61 62 6c 65 20 74 68 61 74 20 63 6f 72 72 65 73  able that corres
5f200 70 6f 6e 64 73 20 74 6f 20 74 68 65 0a 2a 2a 20  ponds to the.** 
5f210 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
5f220 69 6f 6e 20 64 6f 69 6e 67 20 74 68 65 20 70 72  ion doing the pr
5f230 65 70 61 72 69 6e 67 20 73 6f 20 61 73 20 74 6f  eparing so as to
5f240 20 75 73 65 20 74 68 65 20 63 6f 72 72 65 63 74   use the correct
5f250 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 74 61 62  .** sqlite3_vtab
5f260 2a 20 68 61 6e 64 6c 65 20 69 6e 20 74 68 65 20  * handle in the 
5f270 63 6f 6d 70 69 6c 65 64 20 71 75 65 72 79 2e 0a  compiled query..
5f280 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 6e 20 69 6e  **.** When an in
5f290 2d 6d 65 6d 6f 72 79 20 54 61 62 6c 65 20 6f 62  -memory Table ob
5f2a0 6a 65 63 74 20 69 73 20 64 65 6c 65 74 65 64 20  ject is deleted 
5f2b0 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 77 68 65  (for example whe
5f2c0 6e 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20  n the.** schema 
5f2d0 69 73 20 62 65 69 6e 67 20 72 65 6c 6f 61 64 65  is being reloade
5f2e0 64 20 66 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f  d for some reaso
5f2f0 6e 29 2c 20 74 68 65 20 56 54 61 62 6c 65 20 6f  n), the VTable o
5f300 62 6a 65 63 74 73 20 61 72 65 20 6e 6f 74 20 0a  bjects are not .
5f310 2a 2a 20 64 65 6c 65 74 65 64 20 61 6e 64 20 74  ** deleted and t
5f320 68 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62 2a  he sqlite3_vtab*
5f330 20 68 61 6e 64 6c 65 73 20 61 72 65 20 6e 6f 74   handles are not
5f340 20 78 44 69 73 63 6f 6e 6e 65 63 74 28 29 65 64   xDisconnect()ed
5f350 20 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79   .** immediately
5f360 2e 20 49 6e 73 74 65 61 64 2c 20 74 68 65 79 20  . Instead, they 
5f370 61 72 65 20 6d 6f 76 65 64 20 66 72 6f 6d 20 74  are moved from t
5f380 68 65 20 54 61 62 6c 65 2e 70 56 54 61 62 6c 65  he Table.pVTable
5f390 20 6c 69 73 74 20 74 6f 0a 2a 2a 20 61 6e 6f 74   list to.** anot
5f3a0 68 65 72 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  her linked list 
5f3b0 68 65 61 64 65 64 20 62 79 20 74 68 65 20 73 71  headed by the sq
5f3c0 6c 69 74 65 33 2e 70 44 69 73 63 6f 6e 6e 65 63  lite3.pDisconnec
5f3d0 74 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 0a  t member of the.
5f3e0 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  ** corresponding
5f3f0 20 73 71 6c 69 74 65 33 20 73 74 72 75 63 74 75   sqlite3 structu
5f400 72 65 2e 20 54 68 65 79 20 61 72 65 20 74 68 65  re. They are the
5f410 6e 20 64 65 6c 65 74 65 64 2f 78 44 69 73 63 6f  n deleted/xDisco
5f420 6e 6e 65 63 74 65 64 20 0a 2a 2a 20 6e 65 78 74  nnected .** next
5f430 20 74 69 6d 65 20 61 20 73 74 61 74 65 6d 65 6e   time a statemen
5f440 74 20 69 73 20 70 72 65 70 61 72 65 64 20 75 73  t is prepared us
5f450 69 6e 67 20 73 61 69 64 20 73 71 6c 69 74 65 33  ing said sqlite3
5f460 2a 2e 20 54 68 69 73 20 69 73 20 64 6f 6e 65 0a  *. This is done.
5f470 2a 2a 20 74 6f 20 61 76 6f 69 64 20 64 65 61 64  ** to avoid dead
5f480 6c 6f 63 6b 20 69 73 73 75 65 73 20 69 6e 76 6f  lock issues invo
5f490 6c 76 69 6e 67 20 6d 75 6c 74 69 70 6c 65 20 73  lving multiple s
5f4a0 71 6c 69 74 65 33 2e 6d 75 74 65 78 20 6d 75 74  qlite3.mutex mut
5f4b0 65 78 65 73 2e 0a 2a 2a 20 52 65 66 65 72 20 74  exes..** Refer t
5f4c0 6f 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65  o comments above
5f4d0 20 66 75 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65   function sqlite
5f4e0 33 56 74 61 62 55 6e 6c 6f 63 6b 4c 69 73 74 28  3VtabUnlockList(
5f4f0 29 20 66 6f 72 20 61 6e 0a 2a 2a 20 65 78 70 6c  ) for an.** expl
5f500 61 6e 61 74 69 6f 6e 20 61 73 20 74 6f 20 77 68  anation as to wh
5f510 79 20 69 74 20 69 73 20 73 61 66 65 20 74 6f 20  y it is safe to 
5f520 61 64 64 20 61 6e 20 65 6e 74 72 79 20 74 6f 20  add an entry to 
5f530 61 6e 20 73 71 6c 69 74 65 33 2e 70 44 69 73 63  an sqlite3.pDisc
5f540 6f 6e 6e 65 63 74 0a 2a 2a 20 6c 69 73 74 20 77  onnect.** list w
5f550 69 74 68 6f 75 74 20 68 6f 6c 64 69 6e 67 20 74  ithout holding t
5f560 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
5f570 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 78 20 6d   sqlite3.mutex m
5f580 75 74 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  utex..**.** The 
5f590 6d 65 6d 6f 72 79 20 66 6f 72 20 6f 62 6a 65 63  memory for objec
5f5a0 74 73 20 6f 66 20 74 68 69 73 20 74 79 70 65 20  ts of this type 
5f5b0 69 73 20 61 6c 77 61 79 73 20 61 6c 6c 6f 63 61  is always alloca
5f5c0 74 65 64 20 62 79 20 0a 2a 2a 20 73 71 6c 69 74  ted by .** sqlit
5f5d0 65 33 44 62 4d 61 6c 6c 6f 63 28 29 2c 20 75 73  e3DbMalloc(), us
5f5e0 69 6e 67 20 74 68 65 20 63 6f 6e 6e 65 63 74 69  ing the connecti
5f5f0 6f 6e 20 68 61 6e 64 6c 65 20 73 74 6f 72 65 64  on handle stored
5f600 20 69 6e 20 56 54 61 62 6c 65 2e 64 62 20 61 73   in VTable.db as
5f610 20 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 61   .** the first a
5f620 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 72 75  rgument..*/.stru
5f630 63 74 20 56 54 61 62 6c 65 20 7b 0a 20 20 73 71  ct VTable {.  sq
5f640 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20  lite3 *db;      
5f650 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
5f660 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61  ase connection a
5f670 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
5f680 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 4d  his table */.  M
5f690 6f 64 75 6c 65 20 2a 70 4d 6f 64 3b 20 20 20 20  odule *pMod;    
5f6a0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
5f6b0 74 65 72 20 74 6f 20 6d 6f 64 75 6c 65 20 69 6d  ter to module im
5f6c0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a  plementation */.
5f6d0 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
5f6e0 70 56 74 61 62 3b 20 20 20 20 20 20 2f 2a 20 50  pVtab;      /* P
5f6f0 6f 69 6e 74 65 72 20 74 6f 20 76 74 61 62 20 69  ointer to vtab i
5f700 6e 73 74 61 6e 63 65 20 2a 2f 0a 20 20 69 6e 74  nstance */.  int
5f710 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20   nRef;          
5f720 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
5f730 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20   of pointers to 
5f740 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a  this structure *
5f750 2f 0a 20 20 56 54 61 62 6c 65 20 2a 70 4e 65 78  /.  VTable *pNex
5f760 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t;            /*
5f770 20 4e 65 78 74 20 69 6e 20 6c 69 6e 6b 65 64 20   Next in linked 
5f780 6c 69 73 74 20 28 73 65 65 20 61 62 6f 76 65 29  list (see above)
5f790 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61   */.};../*.** Ea
5f7a0 63 68 20 53 51 4c 20 74 61 62 6c 65 20 69 73 20  ch SQL table is 
5f7b0 72 65 70 72 65 73 65 6e 74 65 64 20 69 6e 20 6d  represented in m
5f7c0 65 6d 6f 72 79 20 62 79 20 61 6e 20 69 6e 73 74  emory by an inst
5f7d0 61 6e 63 65 20 6f 66 20 74 68 65 0a 2a 2a 20 66  ance of the.** f
5f7e0 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
5f7f0 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e  re..**.** Table.
5f800 7a 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d  zName is the nam
5f810 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20  e of the table. 
5f820 20 54 68 65 20 63 61 73 65 20 6f 66 20 74 68 65   The case of the
5f830 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 43 52 45   original.** CRE
5f840 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d  ATE TABLE statem
5f850 65 6e 74 20 69 73 20 73 74 6f 72 65 64 2c 20 62  ent is stored, b
5f860 75 74 20 63 61 73 65 20 69 73 20 6e 6f 74 20 73  ut case is not s
5f870 69 67 6e 69 66 69 63 61 6e 74 20 66 6f 72 0a 2a  ignificant for.*
5f880 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 73 2e 0a 2a  * comparisons..*
5f890 2a 0a 2a 2a 20 54 61 62 6c 65 2e 6e 43 6f 6c 20  *.** Table.nCol 
5f8a0 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
5f8b0 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 69 73   columns in this
5f8c0 20 74 61 62 6c 65 2e 20 20 54 61 62 6c 65 2e 61   table.  Table.a
5f8d0 43 6f 6c 20 69 73 20 61 0a 2a 2a 20 70 6f 69 6e  Col is a.** poin
5f8e0 74 65 72 20 74 6f 20 61 6e 20 61 72 72 61 79 20  ter to an array 
5f8f0 6f 66 20 43 6f 6c 75 6d 6e 20 73 74 72 75 63 74  of Column struct
5f900 75 72 65 73 2c 20 6f 6e 65 20 66 6f 72 20 65 61  ures, one for ea
5f910 63 68 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a  ch column..**.**
5f920 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 68 61   If the table ha
5f930 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49  s an INTEGER PRI
5f940 4d 41 52 59 20 4b 45 59 2c 20 74 68 65 6e 20 54  MARY KEY, then T
5f950 61 62 6c 65 2e 69 50 4b 65 79 20 69 73 20 74 68  able.iPKey is th
5f960 65 20 69 6e 64 65 78 20 6f 66 0a 2a 2a 20 74 68  e index of.** th
5f970 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73  e column that is
5f980 20 74 68 61 74 20 6b 65 79 2e 20 20 20 4f 74 68   that key.   Oth
5f990 65 72 77 69 73 65 20 54 61 62 6c 65 2e 69 50 4b  erwise Table.iPK
5f9a0 65 79 20 69 73 20 6e 65 67 61 74 69 76 65 2e 20  ey is negative. 
5f9b0 20 4e 6f 74 65 0a 2a 2a 20 74 68 61 74 20 74 68   Note.** that th
5f9c0 65 20 64 61 74 61 74 79 70 65 20 6f 66 20 74 68  e datatype of th
5f9d0 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6d 75  e PRIMARY KEY mu
5f9e0 73 74 20 62 65 20 49 4e 54 45 47 45 52 20 66 6f  st be INTEGER fo
5f9f0 72 20 74 68 69 73 20 66 69 65 6c 64 20 74 6f 0a  r this field to.
5fa00 2a 2a 20 62 65 20 73 65 74 2e 20 20 41 6e 20 49  ** be set.  An I
5fa10 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
5fa20 45 59 20 69 73 20 75 73 65 64 20 61 73 20 74 68  EY is used as th
5fa30 65 20 72 6f 77 69 64 20 66 6f 72 20 65 61 63 68  e rowid for each
5fa40 20 72 6f 77 20 6f 66 0a 2a 2a 20 74 68 65 20 74   row of.** the t
5fa50 61 62 6c 65 2e 20 20 49 66 20 61 20 74 61 62 6c  able.  If a tabl
5fa60 65 20 68 61 73 20 6e 6f 20 49 4e 54 45 47 45 52  e has no INTEGER
5fa70 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 74 68   PRIMARY KEY, th
5fa80 65 6e 20 61 20 72 61 6e 64 6f 6d 20 72 6f 77 69  en a random rowi
5fa90 64 0a 2a 2a 20 69 73 20 67 65 6e 65 72 61 74 65  d.** is generate
5faa0 64 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 6f  d for each row o
5fab0 66 20 74 68 65 20 74 61 62 6c 65 2e 20 20 54 46  f the table.  TF
5fac0 5f 48 61 73 50 72 69 6d 61 72 79 4b 65 79 20 69  _HasPrimaryKey i
5fad0 73 20 73 65 74 20 69 66 0a 2a 2a 20 74 68 65 20  s set if.** the 
5fae0 74 61 62 6c 65 20 68 61 73 20 61 6e 79 20 50 52  table has any PR
5faf0 49 4d 41 52 59 20 4b 45 59 2c 20 49 4e 54 45 47  IMARY KEY, INTEG
5fb00 45 52 20 6f 72 20 6f 74 68 65 72 77 69 73 65 2e  ER or otherwise.
5fb10 0a 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e 74 6e 75  .**.** Table.tnu
5fb20 6d 20 69 73 20 74 68 65 20 70 61 67 65 20 6e 75  m is the page nu
5fb30 6d 62 65 72 20 66 6f 72 20 74 68 65 20 72 6f 6f  mber for the roo
5fb40 74 20 42 54 72 65 65 20 70 61 67 65 20 6f 66 20  t BTree page of 
5fb50 74 68 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65  the table in the
5fb60 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c  .** database fil
5fb70 65 2e 20 20 49 66 20 54 61 62 6c 65 2e 69 44 62  e.  If Table.iDb
5fb80 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66   is the index of
5fb90 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 61   the database ta
5fba0 62 6c 65 20 62 61 63 6b 65 6e 64 0a 2a 2a 20 69  ble backend.** i
5fbb0 6e 20 73 71 6c 69 74 65 2e 61 44 62 5b 5d 2e 20  n sqlite.aDb[]. 
5fbc0 20 30 20 69 73 20 66 6f 72 20 74 68 65 20 6d 61   0 is for the ma
5fbd0 69 6e 20 64 61 74 61 62 61 73 65 20 61 6e 64 20  in database and 
5fbe0 31 20 69 73 20 66 6f 72 20 74 68 65 20 66 69 6c  1 is for the fil
5fbf0 65 20 74 68 61 74 0a 2a 2a 20 68 6f 6c 64 73 20  e that.** holds 
5fc00 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73  temporary tables
5fc10 20 61 6e 64 20 69 6e 64 69 63 65 73 2e 20 20 49   and indices.  I
5fc20 66 20 54 46 5f 45 70 68 65 6d 65 72 61 6c 20 69  f TF_Ephemeral i
5fc30 73 20 73 65 74 0a 2a 2a 20 74 68 65 6e 20 74 68  s set.** then th
5fc40 65 20 74 61 62 6c 65 20 69 73 20 73 74 6f 72 65  e table is store
5fc50 64 20 69 6e 20 61 20 66 69 6c 65 20 74 68 61 74  d in a file that
5fc60 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c   is automaticall
5fc70 79 20 64 65 6c 65 74 65 64 0a 2a 2a 20 77 68 65  y deleted.** whe
5fc80 6e 20 74 68 65 20 56 44 42 45 20 63 75 72 73 6f  n the VDBE curso
5fc90 72 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 69  r to the table i
5fca0 73 20 63 6c 6f 73 65 64 2e 20 20 49 6e 20 74 68  s closed.  In th
5fcb0 69 73 20 63 61 73 65 20 54 61 62 6c 65 2e 74 6e  is case Table.tn
5fcc0 75 6d 20 0a 2a 2a 20 72 65 66 65 72 73 20 56 44  um .** refers VD
5fcd0 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  BE cursor number
5fce0 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20   that holds the 
5fcf0 74 61 62 6c 65 20 6f 70 65 6e 2c 20 6e 6f 74 20  table open, not 
5fd00 74 6f 20 74 68 65 20 72 6f 6f 74 0a 2a 2a 20 70  to the root.** p
5fd10 61 67 65 20 6e 75 6d 62 65 72 2e 20 20 54 72 61  age number.  Tra
5fd20 6e 73 69 65 6e 74 20 74 61 62 6c 65 73 20 61 72  nsient tables ar
5fd30 65 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 74  e used to hold t
5fd40 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 61 0a  he results of a.
5fd50 2a 2a 20 73 75 62 2d 71 75 65 72 79 20 74 68 61  ** sub-query tha
5fd60 74 20 61 70 70 65 61 72 73 20 69 6e 73 74 65 61  t appears instea
5fd70 64 20 6f 66 20 61 20 72 65 61 6c 20 74 61 62 6c  d of a real tabl
5fd80 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 46 52  e name in the FR
5fd90 4f 4d 20 63 6c 61 75 73 65 20 0a 2a 2a 20 6f 66  OM clause .** of
5fda0 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
5fdb0 65 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 54  ent..*/.struct T
5fdc0 61 62 6c 65 20 7b 0a 20 20 73 71 6c 69 74 65 33  able {.  sqlite3
5fdd0 20 2a 64 62 4d 65 6d 3b 20 20 20 20 20 20 2f 2a   *dbMem;      /*
5fde0 20 44 42 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 75   DB connection u
5fdf0 73 65 64 20 66 6f 72 20 6c 6f 6f 6b 61 73 69 64  sed for lookasid
5fe00 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 20 2a  e allocations. *
5fe10 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b  /.  char *zName;
5fe20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
5fe30 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72   of the table or
5fe40 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20 69   view */.  int i
5fe50 50 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20  PKey;           
5fe60 2f 2a 20 49 66 20 6e 6f 74 20 6e 65 67 61 74 69  /* If not negati
5fe70 76 65 2c 20 75 73 65 20 61 43 6f 6c 5b 69 50 4b  ve, use aCol[iPK
5fe80 65 79 5d 20 61 73 20 74 68 65 20 70 72 69 6d 61  ey] as the prima
5fe90 72 79 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20  ry key */.  int 
5fea0 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20  nCol;           
5feb0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f   /* Number of co
5fec0 6c 75 6d 6e 73 20 69 6e 20 74 68 69 73 20 74 61  lumns in this ta
5fed0 62 6c 65 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e 20  ble */.  Column 
5fee0 2a 61 43 6f 6c 3b 20 20 20 20 20 20 20 20 2f 2a  *aCol;        /*
5fef0 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   Information abo
5ff00 75 74 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a  ut each column *
5ff10 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65  /.  Index *pInde
5ff20 78 3b 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74  x;       /* List
5ff30 20 6f 66 20 53 51 4c 20 69 6e 64 65 78 65 73 20   of SQL indexes 
5ff40 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2e 20 2a  on this table. *
5ff50 2f 0a 20 20 69 6e 74 20 74 6e 75 6d 3b 20 20 20  /.  int tnum;   
5ff60 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74           /* Root
5ff70 20 42 54 72 65 65 20 6e 6f 64 65 20 66 6f 72 20   BTree node for 
5ff80 74 68 69 73 20 74 61 62 6c 65 20 28 73 65 65 20  this table (see 
5ff90 6e 6f 74 65 20 61 62 6f 76 65 29 20 2a 2f 0a 20  note above) */. 
5ffa0 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
5ffb0 3b 20 20 20 20 20 2f 2a 20 4e 55 4c 4c 20 66 6f  ;     /* NULL fo
5ffc0 72 20 74 61 62 6c 65 73 2e 20 20 50 6f 69 6e 74  r tables.  Point
5ffd0 73 20 74 6f 20 64 65 66 69 6e 69 74 69 6f 6e 20  s to definition 
5ffe0 69 66 20 61 20 76 69 65 77 2e 20 2a 2f 0a 20 20  if a view. */.  
5fff0 75 31 36 20 6e 52 65 66 3b 20 20 20 20 20 20 20  u16 nRef;       
60000 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
60010 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68  f pointers to th
60020 69 73 20 54 61 62 6c 65 20 2a 2f 0a 20 20 75 38  is Table */.  u8
60030 20 74 61 62 46 6c 61 67 73 3b 20 20 20 20 20 20   tabFlags;      
60040 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 54 46     /* Mask of TF
60050 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75  _* values */.  u
60060 38 20 6b 65 79 43 6f 6e 66 3b 20 20 20 20 20 20  8 keyConf;      
60070 20 20 20 20 2f 2a 20 57 68 61 74 20 74 6f 20 64      /* What to d
60080 6f 20 69 6e 20 63 61 73 65 20 6f 66 20 75 6e 69  o in case of uni
60090 71 75 65 6e 65 73 73 20 63 6f 6e 66 6c 69 63 74  queness conflict
600a0 20 6f 6e 20 69 50 4b 65 79 20 2a 2f 0a 20 20 46   on iPKey */.  F
600b0 4b 65 79 20 2a 70 46 4b 65 79 3b 20 20 20 20 20  Key *pFKey;     
600c0 20 20 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69      /* Linked li
600d0 73 74 20 6f 66 20 61 6c 6c 20 66 6f 72 65 69 67  st of all foreig
600e0 6e 20 6b 65 79 73 20 69 6e 20 74 68 69 73 20 74  n keys in this t
600f0 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a  able */.  char *
60100 7a 43 6f 6c 41 66 66 3b 20 20 20 20 20 20 20 2f  zColAff;       /
60110 2a 20 53 74 72 69 6e 67 20 64 65 66 69 6e 69 6e  * String definin
60120 67 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f  g the affinity o
60130 66 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f  f each column */
60140 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
60150 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 45 78 70  OMIT_CHECK.  Exp
60160 72 20 2a 70 43 68 65 63 6b 3b 20 20 20 20 20 20  r *pCheck;      
60170 20 20 2f 2a 20 54 68 65 20 41 4e 44 20 6f 66 20    /* The AND of 
60180 61 6c 6c 20 43 48 45 43 4b 20 63 6f 6e 73 74 72  all CHECK constr
60190 61 69 6e 74 73 20 2a 2f 0a 23 65 6e 64 69 66 0a  aints */.#endif.
601a0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
601b0 4d 49 54 5f 41 4c 54 45 52 54 41 42 4c 45 0a 20  MIT_ALTERTABLE. 
601c0 20 69 6e 74 20 61 64 64 43 6f 6c 4f 66 66 73 65   int addColOffse
601d0 74 3b 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20  t;    /* Offset 
601e0 69 6e 20 43 52 45 41 54 45 20 54 41 42 4c 45 20  in CREATE TABLE 
601f0 73 74 6d 74 20 74 6f 20 61 64 64 20 61 20 6e 65  stmt to add a ne
60200 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 23 65 6e 64  w column */.#end
60210 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  if.#ifndef SQLIT
60220 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
60230 42 4c 45 0a 20 20 56 54 61 62 6c 65 20 2a 70 56  BLE.  VTable *pV
60240 54 61 62 6c 65 3b 20 20 20 20 20 2f 2a 20 4c 69  Table;     /* Li
60250 73 74 20 6f 66 20 56 54 61 62 6c 65 20 6f 62 6a  st of VTable obj
60260 65 63 74 73 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e  ects. */.  int n
60270 4d 6f 64 75 6c 65 41 72 67 3b 20 20 20 20 20 20  ModuleArg;      
60280 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67  /* Number of arg
60290 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 6d 6f  uments to the mo
602a0 64 75 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a  dule */.  char *
602b0 2a 61 7a 4d 6f 64 75 6c 65 41 72 67 3b 20 20 2f  *azModuleArg;  /
602c0 2a 20 54 65 78 74 20 6f 66 20 61 6c 6c 20 6d 6f  * Text of all mo
602d0 64 75 6c 65 20 61 72 67 73 2e 20 5b 30 5d 20 69  dule args. [0] i
602e0 73 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 2a 2f  s module name */
602f0 0a 23 65 6e 64 69 66 0a 20 20 54 72 69 67 67 65  .#endif.  Trigge
60300 72 20 2a 70 54 72 69 67 67 65 72 3b 20 20 20 2f  r *pTrigger;   /
60310 2a 20 4c 69 73 74 20 6f 66 20 74 72 69 67 67 65  * List of trigge
60320 72 73 20 73 74 6f 72 65 64 20 69 6e 20 70 53 63  rs stored in pSc
60330 68 65 6d 61 20 2a 2f 0a 20 20 53 63 68 65 6d 61  hema */.  Schema
60340 20 2a 70 53 63 68 65 6d 61 3b 20 20 20 20 20 2f   *pSchema;     /
60350 2a 20 53 63 68 65 6d 61 20 74 68 61 74 20 63 6f  * Schema that co
60360 6e 74 61 69 6e 73 20 74 68 69 73 20 74 61 62 6c  ntains this tabl
60370 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 4e  e */.  Table *pN
60380 65 78 74 5a 6f 6d 62 69 65 3b 20 20 2f 2a 20 4e  extZombie;  /* N
60390 65 78 74 20 6f 6e 20 74 68 65 20 50 61 72 73 65  ext on the Parse
603a0 2e 70 5a 6f 6d 62 69 65 54 61 62 20 6c 69 73 74  .pZombieTab list
603b0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c   */.};../*.** Al
603c0 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72  lowed values for
603d0 20 54 61 62 65 2e 74 61 62 46 6c 61 67 73 2e 0a   Tabe.tabFlags..
603e0 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f 52 65  */.#define TF_Re
603f0 61 64 6f 6e 6c 79 20 20 20 20 20 20 20 20 30 78  adonly        0x
60400 30 31 20 20 20 20 2f 2a 20 52 65 61 64 2d 6f 6e  01    /* Read-on
60410 6c 79 20 73 79 73 74 65 6d 20 74 61 62 6c 65 20  ly system table 
60420 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f 45 70  */.#define TF_Ep
60430 68 65 6d 65 72 61 6c 20 20 20 20 20 20 20 30 78  hemeral       0x
60440 30 32 20 20 20 20 2f 2a 20 41 6e 20 65 70 68 65  02    /* An ephe
60450 6d 65 72 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 23  meral table */.#
60460 64 65 66 69 6e 65 20 54 46 5f 48 61 73 50 72 69  define TF_HasPri
60470 6d 61 72 79 4b 65 79 20 20 20 30 78 30 34 20 20  maryKey   0x04  
60480 20 20 2f 2a 20 54 61 62 6c 65 20 68 61 73 20 61    /* Table has a
60490 20 70 72 69 6d 61 72 79 20 6b 65 79 20 2a 2f 0a   primary key */.
604a0 23 64 65 66 69 6e 65 20 54 46 5f 41 75 74 6f 69  #define TF_Autoi
604b0 6e 63 72 65 6d 65 6e 74 20 20 20 30 78 30 38 20  ncrement   0x08 
604c0 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 70 72     /* Integer pr
604d0 69 6d 61 72 79 20 6b 65 79 20 69 73 20 61 75 74  imary key is aut
604e0 6f 69 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a 23 64  oincrement */.#d
604f0 65 66 69 6e 65 20 54 46 5f 56 69 72 74 75 61 6c  efine TF_Virtual
60500 20 20 20 20 20 20 20 20 20 30 78 31 30 20 20 20           0x10   
60510 20 2f 2a 20 49 73 20 61 20 76 69 72 74 75 61 6c   /* Is a virtual
60520 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e   table */.#defin
60530 65 20 54 46 5f 4e 65 65 64 4d 65 74 61 64 61 74  e TF_NeedMetadat
60540 61 20 20 20 20 30 78 32 30 20 20 20 20 2f 2a 20  a    0x20    /* 
60550 61 43 6f 6c 5b 5d 2e 7a 54 79 70 65 20 61 6e 64  aCol[].zType and
60560 20 61 43 6f 6c 5b 5d 2e 70 43 6f 6c 6c 20 6d 69   aCol[].pColl mi
60570 73 73 69 6e 67 20 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a  ssing */..../*.*
60580 2a 20 54 65 73 74 20 74 6f 20 73 65 65 20 77 68  * Test to see wh
60590 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 20 74  ether or not a t
605a0 61 62 6c 65 20 69 73 20 61 20 76 69 72 74 75 61  able is a virtua
605b0 6c 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 69  l table.  This i
605c0 73 0a 2a 2a 20 64 6f 6e 65 20 61 73 20 61 20 6d  s.** done as a m
605d0 61 63 72 6f 20 73 6f 20 74 68 61 74 20 69 74 20  acro so that it 
605e0 77 69 6c 6c 20 62 65 20 6f 70 74 69 6d 69 7a 65  will be optimize
605f0 64 20 6f 75 74 20 77 68 65 6e 20 76 69 72 74 75  d out when virtu
60600 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 73 75 70 70  al.** table supp
60610 6f 72 74 20 69 73 20 6f 6d 69 74 74 65 64 20 66  ort is omitted f
60620 72 6f 6d 20 74 68 65 20 62 75 69 6c 64 2e 0a 2a  rom the build..*
60630 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
60640 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
60650 4c 45 0a 23 20 20 64 65 66 69 6e 65 20 49 73 56  LE.#  define IsV
60660 69 72 74 75 61 6c 28 58 29 20 20 20 20 20 20 28  irtual(X)      (
60670 28 28 58 29 2d 3e 74 61 62 46 6c 61 67 73 20 26  ((X)->tabFlags &
60680 20 54 46 5f 56 69 72 74 75 61 6c 29 21 3d 30 29   TF_Virtual)!=0)
60690 0a 23 20 20 64 65 66 69 6e 65 20 49 73 48 69 64  .#  define IsHid
606a0 64 65 6e 43 6f 6c 75 6d 6e 28 58 29 20 28 28 58  denColumn(X) ((X
606b0 29 2d 3e 69 73 48 69 64 64 65 6e 29 0a 23 65 6c  )->isHidden).#el
606c0 73 65 0a 23 20 20 64 65 66 69 6e 65 20 49 73 56  se.#  define IsV
606d0 69 72 74 75 61 6c 28 58 29 20 20 20 20 20 20 30  irtual(X)      0
606e0 0a 23 20 20 64 65 66 69 6e 65 20 49 73 48 69 64  .#  define IsHid
606f0 64 65 6e 43 6f 6c 75 6d 6e 28 58 29 20 30 0a 23  denColumn(X) 0.#
60700 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 61 63  endif../*.** Eac
60710 68 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f  h foreign key co
60720 6e 73 74 72 61 69 6e 74 20 69 73 20 61 6e 20 69  nstraint is an i
60730 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
60740 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
60750 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 66 6f 72 65  re..**.** A fore
60760 69 67 6e 20 6b 65 79 20 69 73 20 61 73 73 6f 63  ign key is assoc
60770 69 61 74 65 64 20 77 69 74 68 20 74 77 6f 20 74  iated with two t
60780 61 62 6c 65 73 2e 20 20 54 68 65 20 22 66 72 6f  ables.  The "fro
60790 6d 22 20 74 61 62 6c 65 20 69 73 0a 2a 2a 20 74  m" table is.** t
607a0 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 63 6f  he table that co
607b0 6e 74 61 69 6e 73 20 74 68 65 20 52 45 46 45 52  ntains the REFER
607c0 45 4e 43 45 53 20 63 6c 61 75 73 65 20 74 68 61  ENCES clause tha
607d0 74 20 63 72 65 61 74 65 73 20 74 68 65 20 66 6f  t creates the fo
607e0 72 65 69 67 6e 0a 2a 2a 20 6b 65 79 2e 20 20 54  reign.** key.  T
607f0 68 65 20 22 74 6f 22 20 74 61 62 6c 65 20 69 73  he "to" table is
60800 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20   the table that 
60810 69 73 20 6e 61 6d 65 64 20 69 6e 20 74 68 65 20  is named in the 
60820 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61 75 73  REFERENCES claus
60830 65 2e 0a 2a 2a 20 43 6f 6e 73 69 64 65 72 20 74  e..** Consider t
60840 68 69 73 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a  his example:.**.
60850 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 54 41  **     CREATE TA
60860 42 4c 45 20 65 78 31 28 0a 2a 2a 20 20 20 20 20  BLE ex1(.**     
60870 20 20 61 20 49 4e 54 45 47 45 52 20 50 52 49 4d    a INTEGER PRIM
60880 41 52 59 20 4b 45 59 2c 0a 2a 2a 20 20 20 20 20  ARY KEY,.**     
60890 20 20 62 20 49 4e 54 45 47 45 52 20 43 4f 4e 53    b INTEGER CONS
608a0 54 52 41 49 4e 54 20 66 6b 31 20 52 45 46 45 52  TRAINT fk1 REFER
608b0 45 4e 43 45 53 20 65 78 32 28 78 29 0a 2a 2a 20  ENCES ex2(x).** 
608c0 20 20 20 20 29 3b 0a 2a 2a 0a 2a 2a 20 46 6f 72      );.**.** For
608d0 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 22 66 6b   foreign key "fk
608e0 31 22 2c 20 74 68 65 20 66 72 6f 6d 2d 74 61 62  1", the from-tab
608f0 6c 65 20 69 73 20 22 65 78 31 22 20 61 6e 64 20  le is "ex1" and 
60900 74 68 65 20 74 6f 2d 74 61 62 6c 65 20 69 73 20  the to-table is 
60910 22 65 78 32 22 2e 0a 2a 2a 0a 2a 2a 20 45 61 63  "ex2"..**.** Eac
60920 68 20 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61  h REFERENCES cla
60930 75 73 65 20 67 65 6e 65 72 61 74 65 73 20 61 6e  use generates an
60940 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
60950 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
60960 74 75 72 65 0a 2a 2a 20 77 68 69 63 68 20 69 73  ture.** which is
60970 20 61 74 74 61 63 68 65 64 20 74 6f 20 74 68 65   attached to the
60980 20 66 72 6f 6d 2d 74 61 62 6c 65 2e 20 20 54 68   from-table.  Th
60990 65 20 74 6f 2d 74 61 62 6c 65 20 6e 65 65 64 20  e to-table need 
609a0 6e 6f 74 20 65 78 69 73 74 20 77 68 65 6e 0a 2a  not exist when.*
609b0 2a 20 74 68 65 20 66 72 6f 6d 2d 74 61 62 6c 65  * the from-table
609c0 20 69 73 20 63 72 65 61 74 65 64 2e 20 20 54 68   is created.  Th
609d0 65 20 65 78 69 73 74 65 6e 63 65 20 6f 66 20 74  e existence of t
609e0 68 65 20 74 6f 2d 74 61 62 6c 65 20 69 73 20 6e  he to-table is n
609f0 6f 74 20 63 68 65 63 6b 65 64 2e 0a 2a 2f 0a 73  ot checked..*/.s
60a00 74 72 75 63 74 20 46 4b 65 79 20 7b 0a 20 20 54  truct FKey {.  T
60a10 61 62 6c 65 20 2a 70 46 72 6f 6d 3b 20 20 20 20  able *pFrom;    
60a20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 68   /* The table th
60a30 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  at contains the 
60a40 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61 75 73  REFERENCES claus
60a50 65 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e 65  e */.  FKey *pNe
60a60 78 74 46 72 6f 6d 3b 20 20 2f 2a 20 4e 65 78 74  xtFrom;  /* Next
60a70 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 6e 20   foreign key in 
60a80 70 46 72 6f 6d 20 2a 2f 0a 20 20 63 68 61 72 20  pFrom */.  char 
60a90 2a 7a 54 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20  *zTo;        /* 
60aa0 4e 61 6d 65 20 6f 66 20 74 61 62 6c 65 20 74 68  Name of table th
60ab0 61 74 20 74 68 65 20 6b 65 79 20 70 6f 69 6e 74  at the key point
60ac0 73 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  s to */.  int nC
60ad0 6f 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ol;         /* N
60ae0 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
60af0 20 69 6e 20 74 68 69 73 20 6b 65 79 20 2a 2f 0a   in this key */.
60b00 20 20 75 38 20 69 73 44 65 66 65 72 72 65 64 3b    u8 isDeferred;
60b10 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 63      /* True if c
60b20 6f 6e 73 74 72 61 69 6e 74 20 63 68 65 63 6b 69  onstraint checki
60b30 6e 67 20 69 73 20 64 65 66 65 72 72 65 64 20 74  ng is deferred t
60b40 69 6c 6c 20 43 4f 4d 4d 49 54 20 2a 2f 0a 20 20  ill COMMIT */.  
60b50 75 38 20 75 70 64 61 74 65 43 6f 6e 66 3b 20 20  u8 updateConf;  
60b60 20 20 2f 2a 20 48 6f 77 20 74 6f 20 72 65 73 6f    /* How to reso
60b70 6c 76 65 20 63 6f 6e 66 6c 69 63 74 73 20 74 68  lve conflicts th
60b80 61 74 20 6f 63 63 75 72 20 6f 6e 20 55 50 44 41  at occur on UPDA
60b90 54 45 20 2a 2f 0a 20 20 75 38 20 64 65 6c 65 74  TE */.  u8 delet
60ba0 65 43 6f 6e 66 3b 20 20 20 20 2f 2a 20 48 6f 77  eConf;    /* How
60bb0 20 74 6f 20 72 65 73 6f 6c 76 65 20 63 6f 6e 66   to resolve conf
60bc0 6c 69 63 74 73 20 74 68 61 74 20 6f 63 63 75 72  licts that occur
60bd0 20 6f 6e 20 44 45 4c 45 54 45 20 2a 2f 0a 20 20   on DELETE */.  
60be0 75 38 20 69 6e 73 65 72 74 43 6f 6e 66 3b 20 20  u8 insertConf;  
60bf0 20 20 2f 2a 20 48 6f 77 20 74 6f 20 72 65 73 6f    /* How to reso
60c00 6c 76 65 20 63 6f 6e 66 6c 69 63 74 73 20 74 68  lve conflicts th
60c10 61 74 20 6f 63 63 75 72 20 6f 6e 20 49 4e 53 45  at occur on INSE
60c20 52 54 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73  RT */.  struct s
60c30 43 6f 6c 4d 61 70 20 7b 20 20 2f 2a 20 4d 61 70  ColMap {  /* Map
60c40 70 69 6e 67 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  ping of columns 
60c50 69 6e 20 70 46 72 6f 6d 20 74 6f 20 63 6f 6c 75  in pFrom to colu
60c60 6d 6e 73 20 69 6e 20 7a 54 6f 20 2a 2f 0a 20 20  mns in zTo */.  
60c70 20 20 69 6e 74 20 69 46 72 6f 6d 3b 20 20 20 20    int iFrom;    
60c80 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
60c90 20 63 6f 6c 75 6d 6e 20 69 6e 20 70 46 72 6f 6d   column in pFrom
60ca0 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 43   */.    char *zC
60cb0 6f 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 61  ol;        /* Na
60cc0 6d 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20  me of column in 
60cd0 7a 54 6f 2e 20 20 49 66 20 30 20 75 73 65 20 50  zTo.  If 0 use P
60ce0 52 49 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20  RIMARY KEY */.  
60cf0 7d 20 61 43 6f 6c 5b 31 5d 3b 20 20 20 20 20 20  } aCol[1];      
60d00 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66    /* One entry f
60d10 6f 72 20 65 61 63 68 20 6f 66 20 6e 43 6f 6c 20  or each of nCol 
60d20 63 6f 6c 75 6d 6e 20 73 20 2a 2f 0a 7d 3b 0a 0a  column s */.};..
60d30 2f 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 73 75 70  /*.** SQLite sup
60d40 70 6f 72 74 73 20 6d 61 6e 79 20 64 69 66 66 65  ports many diffe
60d50 72 65 6e 74 20 77 61 79 73 20 74 6f 20 72 65 73  rent ways to res
60d60 6f 6c 76 65 20 61 20 63 6f 6e 73 74 72 61 69 6e  olve a constrain
60d70 74 0a 2a 2a 20 65 72 72 6f 72 2e 20 20 52 4f 4c  t.** error.  ROL
60d80 4c 42 41 43 4b 20 70 72 6f 63 65 73 73 69 6e 67  LBACK processing
60d90 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 63 6f   means that a co
60da0 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69  nstraint violati
60db0 6f 6e 0a 2a 2a 20 63 61 75 73 65 73 20 74 68 65  on.** causes the
60dc0 20 6f 70 65 72 61 74 69 6f 6e 20 69 6e 20 70 72   operation in pr
60dd0 6f 63 65 73 73 20 74 6f 20 66 61 69 6c 20 61 6e  ocess to fail an
60de0 64 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e  d for the curren
60df0 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a  t transaction.**
60e00 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61   to be rolled ba
60e10 63 6b 2e 20 20 41 42 4f 52 54 20 70 72 6f 63 65  ck.  ABORT proce
60e20 73 73 69 6e 67 20 6d 65 61 6e 73 20 74 68 65 20  ssing means the 
60e30 6f 70 65 72 61 74 69 6f 6e 20 69 6e 20 70 72 6f  operation in pro
60e40 63 65 73 73 0a 2a 2a 20 66 61 69 6c 73 20 61 6e  cess.** fails an
60e50 64 20 61 6e 79 20 70 72 69 6f 72 20 63 68 61 6e  d any prior chan
60e60 67 65 73 20 66 72 6f 6d 20 74 68 61 74 20 6f 6e  ges from that on
60e70 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 72 65 20  e operation are 
60e80 62 61 63 6b 65 64 20 6f 75 74 2c 0a 2a 2a 20 62  backed out,.** b
60e90 75 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  ut the transacti
60ea0 6f 6e 20 69 73 20 6e 6f 74 20 72 6f 6c 6c 65 64  on is not rolled
60eb0 20 62 61 63 6b 2e 20 20 46 41 49 4c 20 70 72 6f   back.  FAIL pro
60ec0 63 65 73 73 69 6e 67 20 6d 65 61 6e 73 20 74 68  cessing means th
60ed0 61 74 0a 2a 2a 20 74 68 65 20 6f 70 65 72 61 74  at.** the operat
60ee0 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 20  ion in progress 
60ef0 73 74 6f 70 73 20 61 6e 64 20 72 65 74 75 72 6e  stops and return
60f00 73 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e  s an error code.
60f10 20 20 42 75 74 20 70 72 69 6f 72 0a 2a 2a 20 63    But prior.** c
60f20 68 61 6e 67 65 73 20 64 75 65 20 74 6f 20 74 68  hanges due to th
60f30 65 20 73 61 6d 65 20 6f 70 65 72 61 74 69 6f 6e  e same operation
60f40 20 61 72 65 20 6e 6f 74 20 62 61 63 6b 65 64 20   are not backed 
60f50 6f 75 74 20 61 6e 64 20 6e 6f 20 72 6f 6c 6c 62  out and no rollb
60f60 61 63 6b 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 20  ack.** occurs.  
60f70 49 47 4e 4f 52 45 20 6d 65 61 6e 73 20 74 68 61  IGNORE means tha
60f80 74 20 74 68 65 20 70 61 72 74 69 63 75 6c 61 72  t the particular
60f90 20 72 6f 77 20 74 68 61 74 20 63 61 75 73 65 64   row that caused
60fa0 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 0a   the constraint.
60fb0 2a 2a 20 65 72 72 6f 72 20 69 73 20 6e 6f 74 20  ** error is not 
60fc0 69 6e 73 65 72 74 65 64 20 6f 72 20 75 70 64 61  inserted or upda
60fd0 74 65 64 2e 20 20 50 72 6f 63 65 73 73 69 6e 67  ted.  Processing
60fe0 20 63 6f 6e 74 69 6e 75 65 73 20 61 6e 64 20 6e   continues and n
60ff0 6f 20 65 72 72 6f 72 0a 2a 2a 20 69 73 20 72 65  o error.** is re
61000 74 75 72 6e 65 64 2e 20 20 52 45 50 4c 41 43 45  turned.  REPLACE
61010 20 6d 65 61 6e 73 20 74 68 61 74 20 70 72 65 65   means that pree
61020 78 69 73 74 69 6e 67 20 64 61 74 61 62 61 73 65  xisting database
61030 20 72 6f 77 73 20 74 68 61 74 20 63 61 75 73 65   rows that cause
61040 64 0a 2a 2a 20 61 20 55 4e 49 51 55 45 20 63 6f  d.** a UNIQUE co
61050 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69  nstraint violati
61060 6f 6e 20 61 72 65 20 72 65 6d 6f 76 65 64 20 73  on are removed s
61070 6f 20 74 68 61 74 20 74 68 65 20 6e 65 77 20 69  o that the new i
61080 6e 73 65 72 74 20 6f 72 0a 2a 2a 20 75 70 64 61  nsert or.** upda
61090 74 65 20 63 61 6e 20 70 72 6f 63 65 65 64 2e 20  te can proceed. 
610a0 20 50 72 6f 63 65 73 73 69 6e 67 20 63 6f 6e 74   Processing cont
610b0 69 6e 75 65 73 20 61 6e 64 20 6e 6f 20 65 72 72  inues and no err
610c0 6f 72 20 69 73 20 72 65 70 6f 72 74 65 64 2e 0a  or is reported..
610d0 2a 2a 0a 2a 2a 20 52 45 53 54 52 49 43 54 2c 20  **.** RESTRICT, 
610e0 53 45 54 4e 55 4c 4c 2c 20 61 6e 64 20 43 41 53  SETNULL, and CAS
610f0 43 41 44 45 20 61 63 74 69 6f 6e 73 20 61 70 70  CADE actions app
61100 6c 79 20 6f 6e 6c 79 20 74 6f 20 66 6f 72 65 69  ly only to forei
61110 67 6e 20 6b 65 79 73 2e 0a 2a 2a 20 52 45 53 54  gn keys..** REST
61120 52 49 43 54 20 69 73 20 74 68 65 20 73 61 6d 65  RICT is the same
61130 20 61 73 20 41 42 4f 52 54 20 66 6f 72 20 49 4d   as ABORT for IM
61140 4d 45 44 49 41 54 45 20 66 6f 72 65 69 67 6e 20  MEDIATE foreign 
61150 6b 65 79 73 20 61 6e 64 20 74 68 65 0a 2a 2a 20  keys and the.** 
61160 73 61 6d 65 20 61 73 20 52 4f 4c 4c 42 41 43 4b  same as ROLLBACK
61170 20 66 6f 72 20 44 45 46 45 52 52 45 44 20 6b 65   for DEFERRED ke
61180 79 73 2e 20 20 53 45 54 4e 55 4c 4c 20 6d 65 61  ys.  SETNULL mea
61190 6e 73 20 74 68 61 74 20 74 68 65 20 66 6f 72 65  ns that the fore
611a0 69 67 6e 0a 2a 2a 20 6b 65 79 20 69 73 20 73 65  ign.** key is se
611b0 74 20 74 6f 20 4e 55 4c 4c 2e 20 20 43 41 53 43  t to NULL.  CASC
611c0 41 44 45 20 6d 65 61 6e 73 20 74 68 61 74 20 61  ADE means that a
611d0 20 44 45 4c 45 54 45 20 6f 72 20 55 50 44 41 54   DELETE or UPDAT
611e0 45 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65 66 65  E of the.** refe
611f0 72 65 6e 63 65 64 20 74 61 62 6c 65 20 72 6f 77  renced table row
61200 20 69 73 20 70 72 6f 70 61 67 61 74 65 64 20 69   is propagated i
61210 6e 74 6f 20 74 68 65 20 72 6f 77 20 74 68 61 74  nto the row that
61220 20 68 6f 6c 64 73 20 74 68 65 0a 2a 2a 20 66 6f   holds the.** fo
61230 72 65 69 67 6e 20 6b 65 79 2e 0a 2a 2a 20 0a 2a  reign key..** .*
61240 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
61250 73 79 6d 62 6f 6c 69 63 20 76 61 6c 75 65 73 20  symbolic values 
61260 61 72 65 20 75 73 65 64 20 74 6f 20 72 65 63 6f  are used to reco
61270 72 64 20 77 68 69 63 68 20 74 79 70 65 0a 2a 2a  rd which type.**
61280 20 6f 66 20 61 63 74 69 6f 6e 20 74 6f 20 74 61   of action to ta
61290 6b 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  ke..*/.#define O
612a0 45 5f 4e 6f 6e 65 20 20 20 20 20 30 20 20 20 2f  E_None     0   /
612b0 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20 63 6f  * There is no co
612c0 6e 73 74 72 61 69 6e 74 20 74 6f 20 63 68 65 63  nstraint to chec
612d0 6b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f  k */.#define OE_
612e0 52 6f 6c 6c 62 61 63 6b 20 31 20 20 20 2f 2a 20  Rollback 1   /* 
612f0 46 61 69 6c 20 74 68 65 20 6f 70 65 72 61 74 69  Fail the operati
61300 6f 6e 20 61 6e 64 20 72 6f 6c 6c 62 61 63 6b 20  on and rollback 
61310 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
61320 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 41 62  */.#define OE_Ab
61330 6f 72 74 20 20 20 20 32 20 20 20 2f 2a 20 42 61  ort    2   /* Ba
61340 63 6b 20 6f 75 74 20 63 68 61 6e 67 65 73 20 62  ck out changes b
61350 75 74 20 64 6f 20 6e 6f 20 72 6f 6c 6c 62 61 63  ut do no rollbac
61360 6b 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f  k transaction */
61370 0a 23 64 65 66 69 6e 65 20 4f 45 5f 46 61 69 6c  .#define OE_Fail
61380 20 20 20 20 20 33 20 20 20 2f 2a 20 53 74 6f 70       3   /* Stop
61390 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 62   the operation b
613a0 75 74 20 6c 65 61 76 65 20 61 6c 6c 20 70 72 69  ut leave all pri
613b0 6f 72 20 63 68 61 6e 67 65 73 20 2a 2f 0a 23 64  or changes */.#d
613c0 65 66 69 6e 65 20 4f 45 5f 49 67 6e 6f 72 65 20  efine OE_Ignore 
613d0 20 20 34 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20    4   /* Ignore 
613e0 74 68 65 20 65 72 72 6f 72 2e 20 44 6f 20 6e 6f  the error. Do no
613f0 74 20 64 6f 20 74 68 65 20 49 4e 53 45 52 54 20  t do the INSERT 
61400 6f 72 20 55 50 44 41 54 45 20 2a 2f 0a 23 64 65  or UPDATE */.#de
61410 66 69 6e 65 20 4f 45 5f 52 65 70 6c 61 63 65 20  fine OE_Replace 
61420 20 35 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 65   5   /* Delete e
61430 78 69 73 74 69 6e 67 20 72 65 63 6f 72 64 2c 20  xisting record, 
61440 74 68 65 6e 20 64 6f 20 49 4e 53 45 52 54 20 6f  then do INSERT o
61450 72 20 55 50 44 41 54 45 20 2a 2f 0a 0a 23 64 65  r UPDATE */..#de
61460 66 69 6e 65 20 4f 45 5f 52 65 73 74 72 69 63 74  fine OE_Restrict
61470 20 36 20 20 20 2f 2a 20 4f 45 5f 41 62 6f 72 74   6   /* OE_Abort
61480 20 66 6f 72 20 49 4d 4d 45 44 49 41 54 45 2c 20   for IMMEDIATE, 
61490 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 66 6f 72 20  OE_Rollback for 
614a0 44 45 46 45 52 52 45 44 20 2a 2f 0a 23 64 65 66  DEFERRED */.#def
614b0 69 6e 65 20 4f 45 5f 53 65 74 4e 75 6c 6c 20 20  ine OE_SetNull  
614c0 37 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 66  7   /* Set the f
614d0 6f 72 65 69 67 6e 20 6b 65 79 20 76 61 6c 75 65  oreign key value
614e0 20 74 6f 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66   to NULL */.#def
614f0 69 6e 65 20 4f 45 5f 53 65 74 44 66 6c 74 20 20  ine OE_SetDflt  
61500 38 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 66  8   /* Set the f
61510 6f 72 65 69 67 6e 20 6b 65 79 20 76 61 6c 75 65  oreign key value
61520 20 74 6f 20 69 74 73 20 64 65 66 61 75 6c 74 20   to its default 
61530 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 43 61  */.#define OE_Ca
61540 73 63 61 64 65 20 20 39 20 20 20 2f 2a 20 43 61  scade  9   /* Ca
61550 73 63 61 64 65 20 74 68 65 20 63 68 61 6e 67 65  scade the change
61560 73 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 4f 45  s */..#define OE
61570 5f 44 65 66 61 75 6c 74 20 20 39 39 20 20 2f 2a  _Default  99  /*
61580 20 44 6f 20 77 68 61 74 65 76 65 72 20 74 68 65   Do whatever the
61590 20 64 65 66 61 75 6c 74 20 61 63 74 69 6f 6e 20   default action 
615a0 69 73 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e  is */.../*.** An
615b0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
615c0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
615d0 74 75 72 65 20 69 73 20 70 61 73 73 65 64 20 61  ture is passed a
615e0 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 61  s the first.** a
615f0 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74  rgument to sqlit
61600 65 33 56 64 62 65 4b 65 79 43 6f 6d 70 61 72 65  e3VdbeKeyCompare
61610 20 61 6e 64 20 69 73 20 75 73 65 64 20 74 6f 20   and is used to 
61620 63 6f 6e 74 72 6f 6c 20 74 68 65 20 0a 2a 2a 20  control the .** 
61630 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20 74 68  comparison of th
61640 65 20 74 77 6f 20 69 6e 64 65 78 20 6b 65 79 73  e two index keys
61650 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4b 65 79 49  ..*/.struct KeyI
61660 6e 66 6f 20 7b 0a 20 20 73 71 6c 69 74 65 33 20  nfo {.  sqlite3 
61670 2a 64 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  *db;        /* T
61680 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
61690 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 20 65  ection */.  u8 e
616a0 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  nc;             
616b0 2f 2a 20 54 65 78 74 20 65 6e 63 6f 64 69 6e 67  /* Text encoding
616c0 20 2d 20 6f 6e 65 20 6f 66 20 74 68 65 20 54 45   - one of the TE
616d0 58 54 5f 55 74 66 2a 20 76 61 6c 75 65 73 20 2a  XT_Utf* values *
616e0 2f 0a 20 20 75 31 36 20 6e 46 69 65 6c 64 3b 20  /.  u16 nField; 
616f0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
61700 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
61710 61 43 6f 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20  aColl[] */.  u8 
61720 2a 61 53 6f 72 74 4f 72 64 65 72 3b 20 20 20 20  *aSortOrder;    
61730 20 2f 2a 20 49 66 20 64 65 66 69 6e 65 64 20 61   /* If defined a
61740 6e 20 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20  n aSortOrder[i] 
61750 69 73 20 74 72 75 65 2c 20 73 6f 72 74 20 44 45  is true, sort DE
61760 53 43 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20  SC */.  CollSeq 
61770 2a 61 43 6f 6c 6c 5b 31 5d 3b 20 20 2f 2a 20 43  *aColl[1];  /* C
61780 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
61790 65 20 66 6f 72 20 65 61 63 68 20 74 65 72 6d 20  e for each term 
617a0 6f 66 20 74 68 65 20 6b 65 79 20 2a 2f 0a 7d 3b  of the key */.};
617b0 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  ../*.** An insta
617c0 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
617d0 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 68  wing structure h
617e0 6f 6c 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  olds information
617f0 20 61 62 6f 75 74 20 61 0a 2a 2a 20 73 69 6e 67   about a.** sing
61800 6c 65 20 69 6e 64 65 78 20 72 65 63 6f 72 64 20  le index record 
61810 74 68 61 74 20 68 61 73 20 61 6c 72 65 61 64 79  that has already
61820 20 62 65 65 6e 20 70 61 72 73 65 64 20 6f 75 74   been parsed out
61830 20 69 6e 74 6f 20 69 6e 64 69 76 69 64 75 61 6c   into individual
61840 0a 2a 2a 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a  .** values..**.*
61850 2a 20 41 20 72 65 63 6f 72 64 20 69 73 20 61 6e  * A record is an
61860 20 6f 62 6a 65 63 74 20 74 68 61 74 20 63 6f 6e   object that con
61870 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72  tains one or mor
61880 65 20 66 69 65 6c 64 73 20 6f 66 20 64 61 74 61  e fields of data
61890 2e 0a 2a 2a 20 52 65 63 6f 72 64 73 20 61 72 65  ..** Records are
618a0 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74   used to store t
618b0 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20  he content of a 
618c0 74 61 62 6c 65 20 72 6f 77 20 61 6e 64 20 74 6f  table row and to
618d0 20 73 74 6f 72 65 0a 2a 2a 20 74 68 65 20 6b 65   store.** the ke
618e0 79 20 6f 66 20 61 6e 20 69 6e 64 65 78 2e 20 20  y of an index.  
618f0 41 20 62 6c 6f 62 20 65 6e 63 6f 64 69 6e 67 20  A blob encoding 
61900 6f 66 20 61 20 72 65 63 6f 72 64 20 69 73 20 63  of a record is c
61910 72 65 61 74 65 64 20 62 79 0a 2a 2a 20 74 68 65  reated by.** the
61920 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 6f   OP_MakeRecord o
61930 70 63 6f 64 65 20 6f 66 20 74 68 65 20 56 44 42  pcode of the VDB
61940 45 20 61 6e 64 20 69 73 20 64 69 73 61 73 73 65  E and is disasse
61950 6d 62 6c 65 64 20 62 79 20 74 68 65 0a 2a 2a 20  mbled by the.** 
61960 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65  OP_Column opcode
61970 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72  ..**.** This str
61980 75 63 74 75 72 65 20 68 6f 6c 64 73 20 61 20 72  ucture holds a r
61990 65 63 6f 72 64 20 74 68 61 74 20 68 61 73 20 61  ecord that has a
619a0 6c 72 65 61 64 79 20 62 65 65 6e 20 64 69 73 61  lready been disa
619b0 73 73 65 6d 62 6c 65 64 0a 2a 2a 20 69 6e 74 6f  ssembled.** into
619c0 20 69 74 73 20 63 6f 6e 73 74 69 74 75 65 6e 74   its constituent
619d0 20 66 69 65 6c 64 73 2e 0a 2a 2f 0a 73 74 72 75   fields..*/.stru
619e0 63 74 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  ct UnpackedRecor
619f0 64 20 7b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70  d {.  KeyInfo *p
61a00 4b 65 79 49 6e 66 6f 3b 20 20 2f 2a 20 43 6f 6c  KeyInfo;  /* Col
61a10 6c 61 74 69 6f 6e 20 61 6e 64 20 73 6f 72 74 2d  lation and sort-
61a20 6f 72 64 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f  order informatio
61a30 6e 20 2a 2f 0a 20 20 75 31 36 20 6e 46 69 65 6c  n */.  u16 nFiel
61a40 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  d;         /* Nu
61a50 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
61a60 69 6e 20 61 70 4d 65 6d 5b 5d 20 2a 2f 0a 20 20  in apMem[] */.  
61a70 75 31 36 20 66 6c 61 67 73 3b 20 20 20 20 20 20  u16 flags;      
61a80 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 61 6e 20 73      /* Boolean s
61a90 65 74 74 69 6e 67 73 2e 20 20 55 4e 50 41 43 4b  ettings.  UNPACK
61aa0 45 44 5f 2e 2e 2e 20 62 65 6c 6f 77 20 2a 2f 0a  ED_... below */.
61ab0 20 20 69 36 34 20 72 6f 77 69 64 3b 20 20 20 20    i64 rowid;    
61ac0 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 62 79        /* Used by
61ad0 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 58   UNPACKED_PREFIX
61ae0 5f 53 45 41 52 43 48 20 2a 2f 0a 20 20 4d 65 6d  _SEARCH */.  Mem
61af0 20 2a 61 4d 65 6d 3b 20 20 20 20 20 20 20 20 20   *aMem;         
61b00 20 2f 2a 20 56 61 6c 75 65 73 20 2a 2f 0a 7d 3b   /* Values */.};
61b10 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20  ../*.** Allowed 
61b20 76 61 6c 75 65 73 20 6f 66 20 55 6e 70 61 63 6b  values of Unpack
61b30 65 64 52 65 63 6f 72 64 2e 66 6c 61 67 73 0a 2a  edRecord.flags.*
61b40 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41 43 4b  /.#define UNPACK
61b50 45 44 5f 4e 45 45 44 5f 46 52 45 45 20 20 20 20  ED_NEED_FREE    
61b60 20 30 78 30 30 30 31 20 20 2f 2a 20 4d 65 6d 6f   0x0001  /* Memo
61b70 72 79 20 69 73 20 66 72 6f 6d 20 73 71 6c 69 74  ry is from sqlit
61b80 65 33 4d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 23 64  e3Malloc() */.#d
61b90 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 4e  efine UNPACKED_N
61ba0 45 45 44 5f 44 45 53 54 52 4f 59 20 20 30 78 30  EED_DESTROY  0x0
61bb0 30 30 32 20 20 2f 2a 20 61 70 4d 65 6d 5b 5d 73  002  /* apMem[]s
61bc0 20 73 68 6f 75 6c 64 20 61 6c 6c 20 62 65 20 64   should all be d
61bd0 65 73 74 72 6f 79 65 64 20 2a 2f 0a 23 64 65 66  estroyed */.#def
61be0 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e  ine UNPACKED_IGN
61bf0 4f 52 45 5f 52 4f 57 49 44 20 20 30 78 30 30 30  ORE_ROWID  0x000
61c00 34 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 72 61  4  /* Ignore tra
61c10 69 6c 69 6e 67 20 72 6f 77 69 64 20 6f 6e 20 6b  iling rowid on k
61c20 65 79 31 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55  ey1 */.#define U
61c30 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20  NPACKED_INCRKEY 
61c40 20 20 20 20 20 20 30 78 30 30 30 38 20 20 2f 2a        0x0008  /*
61c50 20 4d 61 6b 65 20 74 68 69 73 20 6b 65 79 20 61   Make this key a
61c60 6e 20 65 70 73 69 6c 6f 6e 20 6c 61 72 67 65 72  n epsilon larger
61c70 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41   */.#define UNPA
61c80 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 43  CKED_PREFIX_MATC
61c90 48 20 20 30 78 30 30 31 30 20 20 2f 2a 20 41 20  H  0x0010  /* A 
61ca0 70 72 65 66 69 78 20 6d 61 74 63 68 20 69 73 20  prefix match is 
61cb0 63 6f 6e 73 69 64 65 72 65 64 20 4f 4b 20 2a 2f  considered OK */
61cc0 0a 23 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45  .#define UNPACKE
61cd0 44 5f 50 52 45 46 49 58 5f 53 45 41 52 43 48 20  D_PREFIX_SEARCH 
61ce0 30 78 30 30 32 30 20 20 2f 2a 20 41 20 70 72 65  0x0020  /* A pre
61cf0 66 69 78 20 6d 61 74 63 68 20 69 73 20 63 6f 6e  fix match is con
61d00 73 69 64 65 72 65 64 20 4f 4b 20 2a 2f 0a 0a 2f  sidered OK */../
61d10 2a 0a 2a 2a 20 45 61 63 68 20 53 51 4c 20 69 6e  *.** Each SQL in
61d20 64 65 78 20 69 73 20 72 65 70 72 65 73 65 6e 74  dex is represent
61d30 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 62 79 20  ed in memory by 
61d40 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f  an.** instance o
61d50 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
61d60 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a  structure..**.**
61d70 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20   The columns of 
61d80 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 61  the table that a
61d90 72 65 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64  re to be indexed
61da0 20 61 72 65 20 64 65 73 63 72 69 62 65 64 0a 2a   are described.*
61db0 2a 20 62 79 20 74 68 65 20 61 69 43 6f 6c 75 6d  * by the aiColum
61dc0 6e 5b 5d 20 66 69 65 6c 64 20 6f 66 20 74 68 69  n[] field of thi
61dd0 73 20 73 74 72 75 63 74 75 72 65 2e 20 20 46 6f  s structure.  Fo
61de0 72 20 65 78 61 6d 70 6c 65 2c 20 73 75 70 70 6f  r example, suppo
61df0 73 65 0a 2a 2a 20 77 65 20 68 61 76 65 20 74 68  se.** we have th
61e00 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c  e following tabl
61e10 65 20 61 6e 64 20 69 6e 64 65 78 3a 0a 2a 2a 0a  e and index:.**.
61e20 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 54 41  **     CREATE TA
61e30 42 4c 45 20 45 78 31 28 63 31 20 69 6e 74 2c 20  BLE Ex1(c1 int, 
61e40 63 32 20 69 6e 74 2c 20 63 33 20 74 65 78 74 29  c2 int, c3 text)
61e50 3b 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20  ;.**     CREATE 
61e60 49 4e 44 45 58 20 45 78 32 20 4f 4e 20 45 78 31  INDEX Ex2 ON Ex1
61e70 28 63 33 2c 63 31 29 3b 0a 2a 2a 0a 2a 2a 20 49  (c3,c1);.**.** I
61e80 6e 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75  n the Table stru
61e90 63 74 75 72 65 20 64 65 73 63 72 69 62 69 6e 67  cture describing
61ea0 20 45 78 31 2c 20 6e 43 6f 6c 3d 3d 33 20 62 65   Ex1, nCol==3 be
61eb0 63 61 75 73 65 20 74 68 65 72 65 20 61 72 65 0a  cause there are.
61ec0 2a 2a 20 74 68 72 65 65 20 63 6f 6c 75 6d 6e 73  ** three columns
61ed0 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20   in the table.  
61ee0 49 6e 20 74 68 65 20 49 6e 64 65 78 20 73 74 72  In the Index str
61ef0 75 63 74 75 72 65 20 64 65 73 63 72 69 62 69 6e  ucture describin
61f00 67 0a 2a 2a 20 45 78 32 2c 20 6e 43 6f 6c 75 6d  g.** Ex2, nColum
61f10 6e 3d 3d 32 20 73 69 6e 63 65 20 32 20 6f 66 20  n==2 since 2 of 
61f20 74 68 65 20 33 20 63 6f 6c 75 6d 6e 73 20 6f 66  the 3 columns of
61f30 20 45 78 31 20 61 72 65 20 69 6e 64 65 78 65 64   Ex1 are indexed
61f40 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f  ..** The value o
61f50 66 20 61 69 43 6f 6c 75 6d 6e 20 69 73 20 7b 32  f aiColumn is {2
61f60 2c 20 30 7d 2e 20 20 61 69 43 6f 6c 75 6d 6e 5b  , 0}.  aiColumn[
61f70 30 5d 3d 3d 32 20 62 65 63 61 75 73 65 20 74 68  0]==2 because th
61f80 65 20 0a 2a 2a 20 66 69 72 73 74 20 63 6f 6c 75  e .** first colu
61f90 6d 6e 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64  mn to be indexed
61fa0 20 28 63 33 29 20 68 61 73 20 61 6e 20 69 6e 64   (c3) has an ind
61fb0 65 78 20 6f 66 20 32 20 69 6e 20 45 78 31 2e 61  ex of 2 in Ex1.a
61fc0 43 6f 6c 5b 5d 2e 0a 2a 2a 20 54 68 65 20 73 65  Col[]..** The se
61fd0 63 6f 6e 64 20 63 6f 6c 75 6d 6e 20 74 6f 20 62  cond column to b
61fe0 65 20 69 6e 64 65 78 65 64 20 28 63 31 29 20 68  e indexed (c1) h
61ff0 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 30  as an index of 0
62000 20 69 6e 0a 2a 2a 20 45 78 31 2e 61 43 6f 6c 5b   in.** Ex1.aCol[
62010 5d 2c 20 68 65 6e 63 65 20 45 78 32 2e 61 69 43  ], hence Ex2.aiC
62020 6f 6c 75 6d 6e 5b 31 5d 3d 3d 30 2e 0a 2a 2a 0a  olumn[1]==0..**.
62030 2a 2a 20 54 68 65 20 49 6e 64 65 78 2e 6f 6e 45  ** The Index.onE
62040 72 72 6f 72 20 66 69 65 6c 64 20 64 65 74 65 72  rror field deter
62050 6d 69 6e 65 73 20 77 68 65 74 68 65 72 20 6f 72  mines whether or
62060 20 6e 6f 74 20 74 68 65 20 69 6e 64 65 78 65 64   not the indexed
62070 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 6d 75 73 74   columns.** must
62080 20 62 65 20 75 6e 69 71 75 65 20 61 6e 64 20 77   be unique and w
62090 68 61 74 20 74 6f 20 64 6f 20 69 66 20 74 68 65  hat to do if the
620a0 79 20 61 72 65 20 6e 6f 74 2e 20 20 57 68 65 6e  y are not.  When
620b0 20 49 6e 64 65 78 2e 6f 6e 45 72 72 6f 72 3d 4f   Index.onError=O
620c0 45 5f 4e 6f 6e 65 2c 0a 2a 2a 20 69 74 20 6d 65  E_None,.** it me
620d0 61 6e 73 20 74 68 69 73 20 69 73 20 6e 6f 74 20  ans this is not 
620e0 61 20 75 6e 69 71 75 65 20 69 6e 64 65 78 2e 20  a unique index. 
620f0 20 4f 74 68 65 72 77 69 73 65 20 69 74 20 69 73   Otherwise it is
62100 20 61 20 75 6e 69 71 75 65 20 69 6e 64 65 78 0a   a unique index.
62110 2a 2a 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65  ** and the value
62120 20 6f 66 20 49 6e 64 65 78 2e 6f 6e 45 72 72 6f   of Index.onErro
62130 72 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 77  r indicate the w
62140 68 69 63 68 20 63 6f 6e 66 6c 69 63 74 20 72 65  hich conflict re
62150 73 6f 6c 75 74 69 6f 6e 20 0a 2a 2a 20 61 6c 67  solution .** alg
62160 6f 72 69 74 68 6d 20 74 6f 20 65 6d 70 6c 6f 79  orithm to employ
62170 20 77 68 65 6e 65 76 65 72 20 61 6e 20 61 74 74   whenever an att
62180 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20  empt is made to 
62190 69 6e 73 65 72 74 20 61 20 6e 6f 6e 2d 75 6e 69  insert a non-uni
621a0 71 75 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 2e 0a  que.** element..
621b0 2a 2f 0a 73 74 72 75 63 74 20 49 6e 64 65 78 20  */.struct Index 
621c0 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b  {.  char *zName;
621d0 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
621e0 74 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a 20 20  this index */.  
621f0 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20  int nColumn;    
62200 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f   /* Number of co
62210 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 74 61 62  lumns in the tab
62220 6c 65 20 75 73 65 64 20 62 79 20 74 68 69 73 20  le used by this 
62230 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 2a  index */.  int *
62240 61 69 43 6f 6c 75 6d 6e 3b 20 20 20 2f 2a 20 57  aiColumn;   /* W
62250 68 69 63 68 20 63 6f 6c 75 6d 6e 73 20 61 72 65  hich columns are
62260 20 75 73 65 64 20 62 79 20 74 68 69 73 20 69 6e   used by this in
62270 64 65 78 2e 20 20 31 73 74 20 69 73 20 30 20 2a  dex.  1st is 0 *
62280 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 2a 61 69  /.  unsigned *ai
62290 52 6f 77 45 73 74 3b 20 2f 2a 20 52 65 73 75 6c  RowEst; /* Resul
622a0 74 20 6f 66 20 41 4e 41 4c 59 5a 45 3a 20 45 73  t of ANALYZE: Es
622b0 74 2e 20 72 6f 77 73 20 73 65 6c 65 63 74 65 64  t. rows selected
622c0 20 62 79 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20   by each column 
622d0 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  */.  Table *pTab
622e0 6c 65 3b 20 20 20 2f 2a 20 54 68 65 20 53 51 4c  le;   /* The SQL
622f0 20 74 61 62 6c 65 20 62 65 69 6e 67 20 69 6e 64   table being ind
62300 65 78 65 64 20 2a 2f 0a 20 20 69 6e 74 20 74 6e  exed */.  int tn
62310 75 6d 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 61  um;        /* Pa
62320 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 72 6f  ge containing ro
62330 6f 74 20 6f 66 20 74 68 69 73 20 69 6e 64 65 78  ot of this index
62340 20 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c   in database fil
62350 65 20 2a 2f 0a 20 20 75 38 20 6f 6e 45 72 72 6f  e */.  u8 onErro
62360 72 3b 20 20 20 20 20 20 2f 2a 20 4f 45 5f 41 62  r;      /* OE_Ab
62370 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20  ort, OE_Ignore, 
62380 4f 45 5f 52 65 70 6c 61 63 65 2c 20 6f 72 20 4f  OE_Replace, or O
62390 45 5f 4e 6f 6e 65 20 2a 2f 0a 20 20 75 38 20 61  E_None */.  u8 a
623a0 75 74 6f 49 6e 64 65 78 3b 20 20 20 20 2f 2a 20  utoIndex;    /* 
623b0 54 72 75 65 20 69 66 20 69 73 20 61 75 74 6f 6d  True if is autom
623c0 61 74 69 63 61 6c 6c 79 20 63 72 65 61 74 65 64  atically created
623d0 20 28 65 78 3a 20 62 79 20 55 4e 49 51 55 45 29   (ex: by UNIQUE)
623e0 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c   */.  char *zCol
623f0 41 66 66 3b 20 20 20 2f 2a 20 53 74 72 69 6e 67  Aff;   /* String
62400 20 64 65 66 69 6e 69 6e 67 20 74 68 65 20 61 66   defining the af
62410 66 69 6e 69 74 79 20 6f 66 20 65 61 63 68 20 63  finity of each c
62420 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 49 6e 64 65 78  olumn */.  Index
62430 20 2a 70 4e 65 78 74 3b 20 20 20 20 2f 2a 20 54   *pNext;    /* T
62440 68 65 20 6e 65 78 74 20 69 6e 64 65 78 20 61 73  he next index as
62450 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
62460 65 20 73 61 6d 65 20 74 61 62 6c 65 20 2a 2f 0a  e same table */.
62470 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d    Schema *pSchem
62480 61 3b 20 2f 2a 20 53 63 68 65 6d 61 20 63 6f 6e  a; /* Schema con
62490 74 61 69 6e 69 6e 67 20 74 68 69 73 20 69 6e 64  taining this ind
624a0 65 78 20 2a 2f 0a 20 20 75 38 20 2a 61 53 6f 72  ex */.  u8 *aSor
624b0 74 4f 72 64 65 72 3b 20 20 2f 2a 20 41 72 72 61  tOrder;  /* Arra
624c0 79 20 6f 66 20 73 69 7a 65 20 49 6e 64 65 78 2e  y of size Index.
624d0 6e 43 6f 6c 75 6d 6e 2e 20 54 72 75 65 3d 3d 44  nColumn. True==D
624e0 45 53 43 2c 20 46 61 6c 73 65 3d 3d 41 53 43 20  ESC, False==ASC 
624f0 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f  */.  char **azCo
62500 6c 6c 3b 20 20 20 2f 2a 20 41 72 72 61 79 20 6f  ll;   /* Array o
62510 66 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  f collation sequ
62520 65 6e 63 65 20 6e 61 6d 65 73 20 66 6f 72 20 69  ence names for i
62530 6e 64 65 78 20 2a 2f 0a 20 20 49 6e 64 65 78 53  ndex */.  IndexS
62540 61 6d 70 6c 65 20 2a 61 53 61 6d 70 6c 65 3b 20  ample *aSample; 
62550 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 53     /* Array of S
62560 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50  QLITE_INDEX_SAMP
62570 4c 45 53 20 73 61 6d 70 6c 65 73 20 2a 2f 0a 7d  LES samples */.}
62580 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 73 61  ;../*.** Each sa
62590 6d 70 6c 65 20 73 74 6f 72 65 64 20 69 6e 20 74  mple stored in t
625a0 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 32 20  he sqlite_stat2 
625b0 74 61 62 6c 65 20 69 73 20 72 65 70 72 65 73 65  table is represe
625c0 6e 74 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 0a  nted in memory .
625d0 2a 2a 20 75 73 69 6e 67 20 61 20 73 74 72 75 63  ** using a struc
625e0 74 75 72 65 20 6f 66 20 74 68 69 73 20 74 79 70  ture of this typ
625f0 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 49 6e 64  e..*/.struct Ind
62600 65 78 53 61 6d 70 6c 65 20 7b 0a 20 20 75 6e 69  exSample {.  uni
62610 6f 6e 20 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a  on {.    char *z
62620 3b 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75  ;        /* Valu
62630 65 20 69 66 20 65 54 79 70 65 20 69 73 20 53 51  e if eType is SQ
62640 4c 49 54 45 5f 54 45 58 54 20 6f 72 20 53 51 4c  LITE_TEXT or SQL
62650 49 54 45 5f 42 4c 4f 42 20 2a 2f 0a 20 20 20 20  ITE_BLOB */.    
62660 64 6f 75 62 6c 65 20 72 3b 20 20 20 20 20 20 20  double r;       
62670 2f 2a 20 56 61 6c 75 65 20 69 66 20 65 54 79 70  /* Value if eTyp
62680 65 20 69 73 20 53 51 4c 49 54 45 5f 46 4c 4f 41  e is SQLITE_FLOA
62690 54 20 6f 72 20 53 51 4c 49 54 45 5f 49 4e 54 45  T or SQLITE_INTE
626a0 47 45 52 20 2a 2f 0a 20 20 7d 20 75 3b 0a 20 20  GER */.  } u;.  
626b0 75 38 20 65 54 79 70 65 3b 20 20 20 20 20 20 20  u8 eType;       
626c0 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4e 55 4c 4c    /* SQLITE_NULL
626d0 2c 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52  , SQLITE_INTEGER
626e0 20 2e 2e 2e 20 65 74 63 2e 20 2a 2f 0a 20 20 75   ... etc. */.  u
626f0 38 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20  8 nByte;        
62700 20 2f 2a 20 53 69 7a 65 20 69 6e 20 62 79 74 65   /* Size in byte
62710 20 6f 66 20 74 65 78 74 20 6f 72 20 62 6c 6f 62   of text or blob
62720 2e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45  . */.};../*.** E
62730 61 63 68 20 74 6f 6b 65 6e 20 63 6f 6d 69 6e 67  ach token coming
62740 20 6f 75 74 20 6f 66 20 74 68 65 20 6c 65 78 65   out of the lexe
62750 72 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65  r is an instance
62760 20 6f 66 0a 2a 2a 20 74 68 69 73 20 73 74 72 75   of.** this stru
62770 63 74 75 72 65 2e 20 20 54 6f 6b 65 6e 73 20 61  cture.  Tokens a
62780 72 65 20 61 6c 73 6f 20 75 73 65 64 20 61 73 20  re also used as 
62790 70 61 72 74 20 6f 66 20 61 6e 20 65 78 70 72 65  part of an expre
627a0 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74  ssion..**.** Not
627b0 65 20 69 66 20 54 6f 6b 65 6e 2e 7a 3d 3d 30 20  e if Token.z==0 
627c0 74 68 65 6e 20 54 6f 6b 65 6e 2e 64 79 6e 20 61  then Token.dyn a
627d0 6e 64 20 54 6f 6b 65 6e 2e 6e 20 61 72 65 20 75  nd Token.n are u
627e0 6e 64 65 66 69 6e 65 64 20 61 6e 64 0a 2a 2a 20  ndefined and.** 
627f0 6d 61 79 20 63 6f 6e 74 61 69 6e 20 72 61 6e 64  may contain rand
62800 6f 6d 20 76 61 6c 75 65 73 2e 20 20 44 6f 20 6e  om values.  Do n
62810 6f 74 20 6d 61 6b 65 20 61 6e 79 20 61 73 73 75  ot make any assu
62820 6d 70 74 69 6f 6e 73 20 61 62 6f 75 74 20 54 6f  mptions about To
62830 6b 65 6e 2e 64 79 6e 0a 2a 2a 20 61 6e 64 20 54  ken.dyn.** and T
62840 6f 6b 65 6e 2e 6e 20 77 68 65 6e 20 54 6f 6b 65  oken.n when Toke
62850 6e 2e 7a 3d 3d 30 2e 0a 2a 2f 0a 73 74 72 75 63  n.z==0..*/.struc
62860 74 20 54 6f 6b 65 6e 20 7b 0a 20 20 63 6f 6e 73  t Token {.  cons
62870 74 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 2f  t char *z;     /
62880 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 74 6f  * Text of the to
62890 6b 65 6e 2e 20 20 4e 6f 74 20 4e 55 4c 4c 2d 74  ken.  Not NULL-t
628a0 65 72 6d 69 6e 61 74 65 64 21 20 2a 2f 0a 20 20  erminated! */.  
628b0 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 3b 20  unsigned int n; 
628c0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
628d0 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 74 68  characters in th
628e0 69 73 20 74 6f 6b 65 6e 20 2a 2f 0a 7d 3b 0a 0a  is token */.};..
628f0 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
62900 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  e of this struct
62910 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 66  ure contains inf
62920 6f 72 6d 61 74 69 6f 6e 20 6e 65 65 64 65 64 20  ormation needed 
62930 74 6f 20 67 65 6e 65 72 61 74 65 0a 2a 2a 20 63  to generate.** c
62940 6f 64 65 20 66 6f 72 20 61 20 53 45 4c 45 43 54  ode for a SELECT
62950 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 61   that contains a
62960 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
62970 6e 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 45 78 70  ns..**.** If Exp
62980 72 2e 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c  r.op==TK_AGG_COL
62990 55 4d 4e 20 6f 72 20 54 4b 5f 41 47 47 5f 46 55  UMN or TK_AGG_FU
629a0 4e 43 54 49 4f 4e 20 74 68 65 6e 20 45 78 70 72  NCTION then Expr
629b0 2e 70 41 67 67 49 6e 66 6f 20 69 73 20 61 0a 2a  .pAggInfo is a.*
629c0 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69  * pointer to thi
629d0 73 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68  s structure.  Th
629e0 65 20 45 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 66  e Expr.iColumn f
629f0 69 65 6c 64 20 69 73 20 74 68 65 20 69 6e 64 65  ield is the inde
62a00 78 20 69 6e 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e  x in.** AggInfo.
62a10 61 43 6f 6c 5b 5d 20 6f 72 20 41 67 67 49 6e 66  aCol[] or AggInf
62a20 6f 2e 61 46 75 6e 63 5b 5d 20 6f 66 20 69 6e 66  o.aFunc[] of inf
62a30 6f 72 6d 61 74 69 6f 6e 20 6e 65 65 64 65 64 20  ormation needed 
62a40 74 6f 20 67 65 6e 65 72 61 74 65 0a 2a 2a 20 63  to generate.** c
62a50 6f 64 65 20 66 6f 72 20 74 68 61 74 20 6e 6f 64  ode for that nod
62a60 65 2e 0a 2a 2a 0a 2a 2a 20 41 67 67 49 6e 66 6f  e..**.** AggInfo
62a70 2e 70 47 72 6f 75 70 42 79 20 61 6e 64 20 41 67  .pGroupBy and Ag
62a80 67 49 6e 66 6f 2e 61 46 75 6e 63 2e 70 45 78 70  gInfo.aFunc.pExp
62a90 72 20 70 6f 69 6e 74 20 74 6f 20 66 69 65 6c 64  r point to field
62aa0 73 20 77 69 74 68 69 6e 20 74 68 65 0a 2a 2a 20  s within the.** 
62ab0 6f 72 69 67 69 6e 61 6c 20 53 65 6c 65 63 74 20  original Select 
62ac0 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 64  structure that d
62ad0 65 73 63 72 69 62 65 73 20 74 68 65 20 53 45 4c  escribes the SEL
62ae0 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  ECT statement.  
62af0 54 68 65 73 65 0a 2a 2a 20 66 69 65 6c 64 73 20  These.** fields 
62b00 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62  do not need to b
62b10 65 20 66 72 65 65 64 20 77 68 65 6e 20 64 65 61  e freed when dea
62b20 6c 6c 6f 63 61 74 69 6e 67 20 74 68 65 20 41 67  llocating the Ag
62b30 67 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e  gInfo structure.
62b40 0a 2a 2f 0a 73 74 72 75 63 74 20 41 67 67 49 6e  .*/.struct AggIn
62b50 66 6f 20 7b 0a 20 20 75 38 20 64 69 72 65 63 74  fo {.  u8 direct
62b60 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f  Mode;          /
62b70 2a 20 44 69 72 65 63 74 20 72 65 6e 64 65 72 69  * Direct renderi
62b80 6e 67 20 6d 6f 64 65 20 6d 65 61 6e 73 20 74 61  ng mode means ta
62b90 6b 65 20 64 61 74 61 20 64 69 72 65 63 74 6c 79  ke data directly
62ba0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
62bb0 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 72             ** fr
62bc0 6f 6d 20 73 6f 75 72 63 65 20 74 61 62 6c 65 73  om source tables
62bd0 20 72 61 74 68 65 72 20 74 68 61 6e 20 66 72 6f   rather than fro
62be0 6d 20 61 63 63 75 6d 75 6c 61 74 6f 72 73 20 2a  m accumulators *
62bf0 2f 0a 20 20 75 38 20 75 73 65 53 6f 72 74 69 6e  /.  u8 useSortin
62c00 67 49 64 78 3b 20 20 20 20 20 20 20 2f 2a 20 49  gIdx;       /* I
62c10 6e 20 64 69 72 65 63 74 20 6d 6f 64 65 2c 20 72  n direct mode, r
62c20 65 66 65 72 65 6e 63 65 20 74 68 65 20 73 6f 72  eference the sor
62c30 74 69 6e 67 20 69 6e 64 65 78 20 72 61 74 68 65  ting index rathe
62c40 72 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r.              
62c50 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74              ** t
62c60 68 61 6e 20 74 68 65 20 73 6f 75 72 63 65 20 74  han the source t
62c70 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 73 6f  able */.  int so
62c80 72 74 69 6e 67 49 64 78 3b 20 20 20 20 20 20 20  rtingIdx;       
62c90 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62    /* Cursor numb
62ca0 65 72 20 6f 66 20 74 68 65 20 73 6f 72 74 69 6e  er of the sortin
62cb0 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20 45 78 70  g index */.  Exp
62cc0 72 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79 3b  rList *pGroupBy;
62cd0 20 20 20 20 20 2f 2a 20 54 68 65 20 67 72 6f 75       /* The grou
62ce0 70 20 62 79 20 63 6c 61 75 73 65 20 2a 2f 0a 20  p by clause */. 
62cf0 20 69 6e 74 20 6e 53 6f 72 74 69 6e 67 43 6f 6c   int nSortingCol
62d00 75 6d 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62  umn;     /* Numb
62d10 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
62d20 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64   the sorting ind
62d30 65 78 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 41  ex */.  struct A
62d40 67 67 49 6e 66 6f 5f 63 6f 6c 20 7b 20 20 20 20  ggInfo_col {    
62d50 2f 2a 20 46 6f 72 20 65 61 63 68 20 63 6f 6c 75  /* For each colu
62d60 6d 6e 20 75 73 65 64 20 69 6e 20 73 6f 75 72 63  mn used in sourc
62d70 65 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 20 20  e tables */.    
62d80 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20  Table *pTab;    
62d90 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72           /* Sour
62da0 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  ce table */.    
62db0 69 6e 74 20 69 54 61 62 6c 65 3b 20 20 20 20 20  int iTable;     
62dc0 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73           /* Curs
62dd0 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  or number of the
62de0 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20 2a 2f   source table */
62df0 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e  .    int iColumn
62e00 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
62e10 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 77   Column number w
62e20 69 74 68 69 6e 20 74 68 65 20 73 6f 75 72 63 65  ithin the source
62e30 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e   table */.    in
62e40 74 20 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 3b  t iSorterColumn;
62e50 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e         /* Column
62e60 20 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 20 73   number in the s
62e70 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 2a 2f 0a  orting index */.
62e80 20 20 20 20 69 6e 74 20 69 4d 65 6d 3b 20 20 20      int iMem;   
62e90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
62ea0 4d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20  Memory location 
62eb0 74 68 61 74 20 61 63 74 73 20 61 73 20 61 63 63  that acts as acc
62ec0 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 20  umulator */.    
62ed0 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20  Expr *pExpr;    
62ee0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
62ef0 6f 72 69 67 69 6e 61 6c 20 65 78 70 72 65 73 73  original express
62f00 69 6f 6e 20 2a 2f 0a 20 20 7d 20 2a 61 43 6f 6c  ion */.  } *aCol
62f10 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b  ;.  int nColumn;
62f20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
62f30 75 6d 62 65 72 20 6f 66 20 75 73 65 64 20 65 6e  umber of used en
62f40 74 72 69 65 73 20 69 6e 20 61 43 6f 6c 5b 5d 20  tries in aCol[] 
62f50 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e  */.  int nColumn
62f60 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 2f 2a 20  Alloc;       /* 
62f70 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20  Number of slots 
62f80 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 43  allocated for aC
62f90 6f 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 41  ol[] */.  int nA
62fa0 63 63 75 6d 75 6c 61 74 6f 72 3b 20 20 20 20 20  ccumulator;     
62fb0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
62fc0 6f 6c 75 6d 6e 73 20 74 68 61 74 20 73 68 6f 77  olumns that show
62fd0 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20   through to the 
62fe0 6f 75 74 70 75 74 2e 0a 20 20 20 20 20 20 20 20  output..        
62ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63000 20 20 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20    ** Additional 
63010 63 6f 6c 75 6d 6e 73 20 61 72 65 20 75 73 65 64  columns are used
63020 20 6f 6e 6c 79 20 61 73 20 70 61 72 61 6d 65 74   only as paramet
63030 65 72 73 20 74 6f 0a 20 20 20 20 20 20 20 20 20  ers to.         
63040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63050 20 2a 2a 20 61 67 67 72 65 67 61 74 65 20 66 75   ** aggregate fu
63060 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 73 74 72  nctions */.  str
63070 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e 63  uct AggInfo_func
63080 20 7b 20 20 20 2f 2a 20 46 6f 72 20 65 61 63 68   {   /* For each
63090 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
630a0 69 6f 6e 20 2a 2f 0a 20 20 20 20 45 78 70 72 20  ion */.    Expr 
630b0 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 20  *pExpr;         
630c0 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f      /* Expressio
630d0 6e 20 65 6e 63 6f 64 69 6e 67 20 74 68 65 20 66  n encoding the f
630e0 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 46  unction */.    F
630f0 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 3b 20 20  uncDef *pFunc;  
63100 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61          /* The a
63110 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
63120 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  n implementation
63130 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d   */.    int iMem
63140 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
63150 20 2f 2a 20 4d 65 6d 6f 72 79 20 6c 6f 63 61 74   /* Memory locat
63160 69 6f 6e 20 74 68 61 74 20 61 63 74 73 20 61 73  ion that acts as
63170 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a   accumulator */.
63180 20 20 20 20 69 6e 74 20 69 44 69 73 74 69 6e 63      int iDistinc
63190 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t;           /* 
631a0 45 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20  Ephemeral table 
631b0 75 73 65 64 20 74 6f 20 65 6e 66 6f 72 63 65 20  used to enforce 
631c0 44 49 53 54 49 4e 43 54 20 2a 2f 0a 20 20 7d 20  DISTINCT */.  } 
631d0 2a 61 46 75 6e 63 3b 0a 20 20 69 6e 74 20 6e 46  *aFunc;.  int nF
631e0 75 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  unc;            
631f0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
63200 6e 74 72 69 65 73 20 69 6e 20 61 46 75 6e 63 5b  ntries in aFunc[
63210 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 75 6e 63  ] */.  int nFunc
63220 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 2f  Alloc;         /
63230 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74  * Number of slot
63240 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20  s allocated for 
63250 61 46 75 6e 63 5b 5d 20 2a 2f 0a 7d 3b 0a 0a 2f  aFunc[] */.};../
63260 2a 0a 2a 2a 20 45 61 63 68 20 6e 6f 64 65 20 6f  *.** Each node o
63270 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  f an expression 
63280 69 6e 20 74 68 65 20 70 61 72 73 65 20 74 72 65  in the parse tre
63290 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65  e is an instance
632a0 0a 2a 2a 20 6f 66 20 74 68 69 73 20 73 74 72 75  .** of this stru
632b0 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 70  cture..**.** Exp
632c0 72 2e 6f 70 20 69 73 20 74 68 65 20 6f 70 63 6f  r.op is the opco
632d0 64 65 2e 20 54 68 65 20 69 6e 74 65 67 65 72 20  de. The integer 
632e0 70 61 72 73 65 72 20 74 6f 6b 65 6e 20 63 6f 64  parser token cod
632f0 65 73 20 61 72 65 20 72 65 75 73 65 64 0a 2a 2a  es are reused.**
63300 20 61 73 20 6f 70 63 6f 64 65 73 20 68 65 72 65   as opcodes here
63310 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74  . For example, t
63320 68 65 20 70 61 72 73 65 72 20 64 65 66 69 6e 65  he parser define
63330 73 20 54 4b 5f 47 45 20 74 6f 20 62 65 20 61 6e  s TK_GE to be an
63340 20 69 6e 74 65 67 65 72 0a 2a 2a 20 63 6f 64 65   integer.** code
63350 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68   representing th
63360 65 20 22 3e 3d 22 20 6f 70 65 72 61 74 6f 72 2e  e ">=" operator.
63370 20 54 68 69 73 20 73 61 6d 65 20 69 6e 74 65 67   This same integ
63380 65 72 20 63 6f 64 65 20 69 73 20 72 65 75 73 65  er code is reuse
63390 64 0a 2a 2a 20 74 6f 20 72 65 70 72 65 73 65 6e  d.** to represen
633a0 74 20 74 68 65 20 67 72 65 61 74 65 72 2d 74 68  t the greater-th
633b0 61 6e 2d 6f 72 2d 65 71 75 61 6c 2d 74 6f 20 6f  an-or-equal-to o
633c0 70 65 72 61 74 6f 72 20 69 6e 20 74 68 65 20 65  perator in the e
633d0 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65  xpression.** tre
633e0 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
633f0 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 6e  expression is an
63400 20 53 51 4c 20 6c 69 74 65 72 61 6c 20 28 54 4b   SQL literal (TK
63410 5f 49 4e 54 45 47 45 52 2c 20 54 4b 5f 46 4c 4f  _INTEGER, TK_FLO
63420 41 54 2c 20 54 4b 5f 42 4c 4f 42 2c 20 0a 2a 2a  AT, TK_BLOB, .**
63430 20 6f 72 20 54 4b 5f 53 54 52 49 4e 47 29 2c 20   or TK_STRING), 
63440 74 68 65 6e 20 45 78 70 72 2e 74 6f 6b 65 6e 20  then Expr.token 
63450 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 65 78  contains the tex
63460 74 20 6f 66 20 74 68 65 20 53 51 4c 20 6c 69 74  t of the SQL lit
63470 65 72 61 6c 2e 20 49 66 0a 2a 2a 20 74 68 65 20  eral. If.** the 
63480 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20  expression is a 
63490 76 61 72 69 61 62 6c 65 20 28 54 4b 5f 56 41 52  variable (TK_VAR
634a0 49 41 42 4c 45 29 2c 20 74 68 65 6e 20 45 78 70  IABLE), then Exp
634b0 72 2e 74 6f 6b 65 6e 20 63 6f 6e 74 61 69 6e 73  r.token contains
634c0 20 74 68 65 20 0a 2a 2a 20 76 61 72 69 61 62 6c   the .** variabl
634d0 65 20 6e 61 6d 65 2e 20 46 69 6e 61 6c 6c 79 2c  e name. Finally,
634e0 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69   if the expressi
634f0 6f 6e 20 69 73 20 61 6e 20 53 51 4c 20 66 75 6e  on is an SQL fun
63500 63 74 69 6f 6e 20 28 54 4b 5f 46 55 4e 43 54 49  ction (TK_FUNCTI
63510 4f 4e 29 2c 0a 2a 2a 20 74 68 65 6e 20 45 78 70  ON),.** then Exp
63520 72 2e 74 6f 6b 65 6e 20 63 6f 6e 74 61 69 6e 73  r.token contains
63530 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
63540 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   function..**.**
63550 20 45 78 70 72 2e 70 52 69 67 68 74 20 61 6e 64   Expr.pRight and
63560 20 45 78 70 72 2e 70 4c 65 66 74 20 61 72 65 20   Expr.pLeft are 
63570 74 68 65 20 6c 65 66 74 20 61 6e 64 20 72 69 67  the left and rig
63580 68 74 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e  ht subexpression
63590 73 20 6f 66 20 61 0a 2a 2a 20 62 69 6e 61 72 79  s of a.** binary
635a0 20 6f 70 65 72 61 74 6f 72 2e 20 45 69 74 68 65   operator. Eithe
635b0 72 20 6f 72 20 62 6f 74 68 20 6d 61 79 20 62 65  r or both may be
635c0 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 45 78 70   NULL..**.** Exp
635d0 72 2e 78 2e 70 4c 69 73 74 20 69 73 20 61 20 6c  r.x.pList is a l
635e0 69 73 74 20 6f 66 20 61 72 67 75 6d 65 6e 74 73  ist of arguments
635f0 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69   if the expressi
63600 6f 6e 20 69 73 20 61 6e 20 53 51 4c 20 66 75 6e  on is an SQL fun
63610 63 74 69 6f 6e 2c 0a 2a 2a 20 61 20 43 41 53 45  ction,.** a CASE
63620 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 61   expression or a
63630 6e 20 49 4e 20 65 78 70 72 65 73 73 69 6f 6e 20  n IN expression 
63640 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3c 6c 68  of the form "<lh
63650 73 3e 20 49 4e 20 28 3c 79 3e 2c 20 3c 7a 3e 2e  s> IN (<y>, <z>.
63660 2e 2e 29 22 2e 0a 2a 2a 20 45 78 70 72 2e 78 2e  ..)"..** Expr.x.
63670 70 53 65 6c 65 63 74 20 69 73 20 75 73 65 64 20  pSelect is used 
63680 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  if the expressio
63690 6e 20 69 73 20 61 20 73 75 62 2d 73 65 6c 65 63  n is a sub-selec
636a0 74 20 6f 72 20 61 6e 20 65 78 70 72 65 73 73 69  t or an expressi
636b0 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 72  on of.** the for
636c0 6d 20 22 3c 6c 68 73 3e 20 49 4e 20 28 53 45 4c  m "<lhs> IN (SEL
636d0 45 43 54 20 2e 2e 2e 29 22 2e 20 49 66 20 74 68  ECT ...)". If th
636e0 65 20 45 50 5f 78 49 73 53 65 6c 65 63 74 20 62  e EP_xIsSelect b
636f0 69 74 20 69 73 20 73 65 74 20 69 6e 20 74 68 65  it is set in the
63700 0a 2a 2a 20 45 78 70 72 2e 66 6c 61 67 73 20 6d  .** Expr.flags m
63710 61 73 6b 2c 20 74 68 65 6e 20 45 78 70 72 2e 78  ask, then Expr.x
63720 2e 70 53 65 6c 65 63 74 20 69 73 20 76 61 6c 69  .pSelect is vali
63730 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 45 78  d. Otherwise, Ex
63740 70 72 2e 78 2e 70 4c 69 73 74 20 69 73 20 0a 2a  pr.x.pList is .*
63750 2a 20 76 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20 41  * valid..**.** A
63760 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 66 20  n expression of 
63770 74 68 65 20 66 6f 72 6d 20 49 44 20 6f 72 20 49  the form ID or I
63780 44 2e 49 44 20 72 65 66 65 72 73 20 74 6f 20 61  D.ID refers to a
63790 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 74 61 62   column in a tab
637a0 6c 65 2e 0a 2a 2a 20 46 6f 72 20 73 75 63 68 20  le..** For such 
637b0 65 78 70 72 65 73 73 69 6f 6e 73 2c 20 45 78 70  expressions, Exp
637c0 72 2e 6f 70 20 69 73 20 73 65 74 20 74 6f 20 54  r.op is set to T
637d0 4b 5f 43 4f 4c 55 4d 4e 20 61 6e 64 20 45 78 70  K_COLUMN and Exp
637e0 72 2e 69 54 61 62 6c 65 20 69 73 0a 2a 2a 20 74  r.iTable is.** t
637f0 68 65 20 69 6e 74 65 67 65 72 20 63 75 72 73 6f  he integer curso
63800 72 20 6e 75 6d 62 65 72 20 6f 66 20 61 20 56 44  r number of a VD
63810 42 45 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 69  BE cursor pointi
63820 6e 67 20 74 6f 20 74 68 61 74 20 74 61 62 6c 65  ng to that table
63830 20 61 6e 64 0a 2a 2a 20 45 78 70 72 2e 69 43 6f   and.** Expr.iCo
63840 6c 75 6d 6e 20 69 73 20 74 68 65 20 63 6f 6c 75  lumn is the colu
63850 6d 6e 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68  mn number for th
63860 65 20 73 70 65 63 69 66 69 63 20 63 6f 6c 75 6d  e specific colum
63870 6e 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 65 78  n.  If the.** ex
63880 70 72 65 73 73 69 6f 6e 20 69 73 20 75 73 65 64  pression is used
63890 20 61 73 20 61 20 72 65 73 75 6c 74 20 69 6e 20   as a result in 
638a0 61 6e 20 61 67 67 72 65 67 61 74 65 20 53 45 4c  an aggregate SEL
638b0 45 43 54 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a  ECT, then the.**
638c0 20 76 61 6c 75 65 20 69 73 20 61 6c 73 6f 20 73   value is also s
638d0 74 6f 72 65 64 20 69 6e 20 74 68 65 20 45 78 70  tored in the Exp
638e0 72 2e 69 41 67 67 20 63 6f 6c 75 6d 6e 20 69 6e  r.iAgg column in
638f0 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 73   the aggregate s
63900 6f 20 74 68 61 74 0a 2a 2a 20 69 74 20 63 61 6e  o that.** it can
63910 20 62 65 20 61 63 63 65 73 73 65 64 20 61 66 74   be accessed aft
63920 65 72 20 61 6c 6c 20 61 67 67 72 65 67 61 74 65  er all aggregate
63930 73 20 61 72 65 20 63 6f 6d 70 75 74 65 64 2e 0a  s are computed..
63940 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70  **.** If the exp
63950 72 65 73 73 69 6f 6e 20 69 73 20 61 6e 20 75 6e  ression is an un
63960 62 6f 75 6e 64 20 76 61 72 69 61 62 6c 65 20 6d  bound variable m
63970 61 72 6b 65 72 20 28 61 20 71 75 65 73 74 69 6f  arker (a questio
63980 6e 20 6d 61 72 6b 20 0a 2a 2a 20 63 68 61 72 61  n mark .** chara
63990 63 74 65 72 20 27 3f 27 20 69 6e 20 74 68 65 20  cter '?' in the 
639a0 6f 72 69 67 69 6e 61 6c 20 53 51 4c 29 20 74 68  original SQL) th
639b0 65 6e 20 74 68 65 20 45 78 70 72 2e 69 54 61 62  en the Expr.iTab
639c0 6c 65 20 68 6f 6c 64 73 20 74 68 65 20 69 6e 64  le holds the ind
639d0 65 78 20 0a 2a 2a 20 6e 75 6d 62 65 72 20 66 6f  ex .** number fo
639e0 72 20 74 68 61 74 20 76 61 72 69 61 62 6c 65 2e  r that variable.
639f0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78  .**.** If the ex
63a00 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 73 75  pression is a su
63a10 62 71 75 65 72 79 20 74 68 65 6e 20 45 78 70 72  bquery then Expr
63a20 2e 69 43 6f 6c 75 6d 6e 20 68 6f 6c 64 73 20 61  .iColumn holds a
63a30 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 72 65 67  n integer.** reg
63a40 69 73 74 65 72 20 6e 75 6d 62 65 72 20 63 6f 6e  ister number con
63a50 74 61 69 6e 69 6e 67 20 74 68 65 20 72 65 73 75  taining the resu
63a60 6c 74 20 6f 66 20 74 68 65 20 73 75 62 71 75 65  lt of the subque
63a70 72 79 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 73  ry.  If the.** s
63a80 75 62 71 75 65 72 79 20 67 69 76 65 73 20 61 20  ubquery gives a 
63a90 63 6f 6e 73 74 61 6e 74 20 72 65 73 75 6c 74 2c  constant result,
63aa0 20 74 68 65 6e 20 69 54 61 62 6c 65 20 69 73 20   then iTable is 
63ab0 2d 31 2e 20 20 49 66 20 74 68 65 20 73 75 62 71  -1.  If the subq
63ac0 75 65 72 79 0a 2a 2a 20 67 69 76 65 73 20 61 20  uery.** gives a 
63ad0 64 69 66 66 65 72 65 6e 74 20 61 6e 73 77 65 72  different answer
63ae0 20 61 74 20 64 69 66 66 65 72 65 6e 74 20 74 69   at different ti
63af0 6d 65 73 20 64 75 72 69 6e 67 20 73 74 61 74 65  mes during state
63b00 6d 65 6e 74 20 70 72 6f 63 65 73 73 69 6e 67 0a  ment processing.
63b10 2a 2a 20 74 68 65 6e 20 69 54 61 62 6c 65 20 69  ** then iTable i
63b20 73 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66  s the address of
63b30 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20 74 68   a subroutine th
63b40 61 74 20 63 6f 6d 70 75 74 65 73 20 74 68 65 20  at computes the 
63b50 73 75 62 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20  subquery..**.** 
63b60 49 66 20 74 68 65 20 45 78 70 72 20 69 73 20 6f  If the Expr is o
63b70 66 20 74 79 70 65 20 4f 50 5f 43 6f 6c 75 6d 6e  f type OP_Column
63b80 2c 20 61 6e 64 20 74 68 65 20 74 61 62 6c 65 20  , and the table 
63b90 69 74 20 69 73 20 73 65 6c 65 63 74 69 6e 67 20  it is selecting 
63ba0 66 72 6f 6d 0a 2a 2a 20 69 73 20 61 20 64 69 73  from.** is a dis
63bb0 6b 20 74 61 62 6c 65 20 6f 72 20 74 68 65 20 22  k table or the "
63bc0 6f 6c 64 2e 2a 22 20 70 73 65 75 64 6f 2d 74 61  old.*" pseudo-ta
63bd0 62 6c 65 2c 20 74 68 65 6e 20 70 54 61 62 20 70  ble, then pTab p
63be0 6f 69 6e 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20  oints to the.** 
63bf0 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 61  corresponding ta
63c00 62 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a  ble definition..
63c10 2a 2a 0a 2a 2a 20 41 4c 4c 4f 43 41 54 49 4f 4e  **.** ALLOCATION
63c20 20 4e 4f 54 45 53 3a 0a 2a 2a 0a 2a 2a 20 45 78   NOTES:.**.** Ex
63c30 70 72 20 6f 62 6a 65 63 74 73 20 63 61 6e 20 75  pr objects can u
63c40 73 65 20 61 20 6c 6f 74 20 6f 66 20 6d 65 6d 6f  se a lot of memo
63c50 72 79 20 73 70 61 63 65 20 69 6e 20 64 61 74 61  ry space in data
63c60 62 61 73 65 20 73 63 68 65 6d 61 2e 20 20 54 6f  base schema.  To
63c70 0a 2a 2a 20 68 65 6c 70 20 72 65 64 75 63 65 20  .** help reduce 
63c80 6d 65 6d 6f 72 79 20 72 65 71 75 69 72 65 6d 65  memory requireme
63c90 6e 74 73 2c 20 73 6f 6d 65 74 69 6d 65 73 20 61  nts, sometimes a
63ca0 6e 20 45 78 70 72 20 6f 62 6a 65 63 74 20 77 69  n Expr object wi
63cb0 6c 6c 20 62 65 0a 2a 2a 20 74 72 75 6e 63 61 74  ll be.** truncat
63cc0 65 64 2e 20 20 41 6e 64 20 74 6f 20 72 65 64 75  ed.  And to redu
63cd0 63 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ce the number of
63ce0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
63cf0 6f 6e 73 2c 20 73 6f 6d 65 74 69 6d 65 73 0a 2a  ons, sometimes.*
63d00 2a 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 45 78  * two or more Ex
63d10 70 72 20 6f 62 6a 65 63 74 73 20 77 69 6c 6c 20  pr objects will 
63d20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20 73  be stored in a s
63d30 69 6e 67 6c 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  ingle memory all
63d40 6f 63 61 74 69 6f 6e 2c 0a 2a 2a 20 74 6f 67 65  ocation,.** toge
63d50 74 68 65 72 20 77 69 74 68 20 45 78 70 72 2e 7a  ther with Expr.z
63d60 54 6f 6b 65 6e 20 73 74 72 69 6e 67 73 2e 0a 2a  Token strings..*
63d70 2a 0a 2a 2a 20 49 66 20 74 68 65 20 45 50 5f 52  *.** If the EP_R
63d80 65 64 75 63 65 64 20 61 6e 64 20 45 50 5f 54 6f  educed and EP_To
63d90 6b 65 6e 4f 6e 6c 79 20 66 6c 61 67 73 20 61 72  kenOnly flags ar
63da0 65 20 73 65 74 20 77 68 65 6e 0a 2a 2a 20 61 6e  e set when.** an
63db0 20 45 78 70 72 20 6f 62 6a 65 63 74 20 69 73 20   Expr object is 
63dc0 74 72 75 6e 63 61 74 65 64 2e 20 20 57 68 65 6e  truncated.  When
63dd0 20 45 50 5f 52 65 64 75 63 65 64 20 69 73 20 73   EP_Reduced is s
63de0 65 74 2c 20 74 68 65 6e 20 61 6c 6c 0a 2a 2a 20  et, then all.** 
63df0 74 68 65 20 63 68 69 6c 64 20 45 78 70 72 20 6f  the child Expr o
63e00 62 6a 65 63 74 73 20 69 6e 20 74 68 65 20 45 78  bjects in the Ex
63e10 70 72 2e 70 4c 65 66 74 20 61 6e 64 20 45 78 70  pr.pLeft and Exp
63e20 72 2e 70 52 69 67 68 74 20 73 75 62 74 72 65 65  r.pRight subtree
63e30 73 0a 2a 2a 20 61 72 65 20 63 6f 6e 74 61 69 6e  s.** are contain
63e40 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 73 61  ed within the sa
63e50 6d 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  me memory alloca
63e60 74 69 6f 6e 2e 20 20 4e 6f 74 65 2c 20 68 6f 77  tion.  Note, how
63e70 65 76 65 72 2c 20 74 68 61 74 0a 2a 2a 20 74 68  ever, that.** th
63e80 65 20 73 75 62 74 72 65 65 73 20 69 6e 20 45 78  e subtrees in Ex
63e90 70 72 2e 78 2e 70 4c 69 73 74 20 6f 72 20 45 78  pr.x.pList or Ex
63ea0 70 72 2e 78 2e 70 53 65 6c 65 63 74 20 61 72 65  pr.x.pSelect are
63eb0 20 61 6c 77 61 79 73 20 73 65 70 61 72 61 74 65   always separate
63ec0 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c  ly.** allocated,
63ed0 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77   regardless of w
63ee0 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 45 50  hether or not EP
63ef0 5f 52 65 64 75 63 65 64 20 69 73 20 73 65 74 2e  _Reduced is set.
63f00 0a 2a 2f 0a 73 74 72 75 63 74 20 45 78 70 72 20  .*/.struct Expr 
63f10 7b 0a 20 20 75 38 20 6f 70 3b 20 20 20 20 20 20  {.  u8 op;      
63f20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70             /* Op
63f30 65 72 61 74 69 6f 6e 20 70 65 72 66 6f 72 6d 65  eration performe
63f40 64 20 62 79 20 74 68 69 73 20 6e 6f 64 65 20 2a  d by this node *
63f50 2f 0a 20 20 63 68 61 72 20 61 66 66 69 6e 69 74  /.  char affinit
63f60 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  y;         /* Th
63f70 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68  e affinity of th
63f80 65 20 63 6f 6c 75 6d 6e 20 6f 72 20 30 20 69 66  e column or 0 if
63f90 20 6e 6f 74 20 61 20 63 6f 6c 75 6d 6e 20 2a 2f   not a column */
63fa0 0a 20 20 75 31 36 20 66 6c 61 67 73 3b 20 20 20  .  u16 flags;   
63fb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 72            /* Var
63fc0 69 6f 75 73 20 66 6c 61 67 73 2e 20 20 45 50 5f  ious flags.  EP_
63fd0 2a 20 53 65 65 20 62 65 6c 6f 77 20 2a 2f 0a 20  * See below */. 
63fe0 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 63 68 61   union {.    cha
63ff0 72 20 2a 7a 54 6f 6b 65 6e 3b 20 20 20 20 20 20  r *zToken;      
64000 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 76 61 6c      /* Token val
64010 75 65 2e 20 5a 65 72 6f 20 74 65 72 6d 69 6e 61  ue. Zero termina
64020 74 65 64 20 61 6e 64 20 64 65 71 75 6f 74 65 64  ted and dequoted
64030 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 56 61 6c   */.    int iVal
64040 75 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ue;            /
64050 2a 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20  * Integer value 
64060 69 66 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 2a  if EP_IntValue *
64070 2f 0a 20 20 7d 20 75 3b 0a 0a 20 20 2f 2a 20 49  /.  } u;..  /* I
64080 66 20 74 68 65 20 45 50 5f 54 6f 6b 65 6e 4f 6e  f the EP_TokenOn
64090 6c 79 20 66 6c 61 67 20 69 73 20 73 65 74 20 69  ly flag is set i
640a0 6e 20 74 68 65 20 45 78 70 72 2e 66 6c 61 67 73  n the Expr.flags
640b0 20 6d 61 73 6b 2c 20 74 68 65 6e 20 6e 6f 0a 20   mask, then no. 
640c0 20 2a 2a 20 73 70 61 63 65 20 69 73 20 61 6c 6c   ** space is all
640d0 6f 63 61 74 65 64 20 66 6f 72 20 74 68 65 20 66  ocated for the f
640e0 69 65 6c 64 73 20 62 65 6c 6f 77 20 74 68 69 73  ields below this
640f0 20 70 6f 69 6e 74 2e 20 41 6e 20 61 74 74 65 6d   point. An attem
64100 70 74 20 74 6f 0a 20 20 2a 2a 20 61 63 63 65 73  pt to.  ** acces
64110 73 20 74 68 65 6d 20 77 69 6c 6c 20 72 65 73 75  s them will resu
64120 6c 74 20 69 6e 20 61 20 73 65 67 66 61 75 6c 74  lt in a segfault
64130 20 6f 72 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 2e   or malfunction.
64140 20 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   .  ************
64150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 20 20 45 78  *********/..  Ex
64190 70 72 20 2a 70 4c 65 66 74 3b 20 20 20 20 20 20  pr *pLeft;      
641a0 20 20 20 20 20 2f 2a 20 4c 65 66 74 20 73 75 62       /* Left sub
641b0 6e 6f 64 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a  node */.  Expr *
641c0 70 52 69 67 68 74 3b 20 20 20 20 20 20 20 20 20  pRight;         
641d0 20 2f 2a 20 52 69 67 68 74 20 73 75 62 6e 6f 64   /* Right subnod
641e0 65 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20  e */.  union {. 
641f0 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69     ExprList *pLi
64200 73 74 3b 20 20 20 20 20 2f 2a 20 46 75 6e 63 74  st;     /* Funct
64210 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20 6f 72  ion arguments or
64220 20 69 6e 20 22 3c 65 78 70 72 3e 20 49 4e 20 28   in "<expr> IN (
64230 3c 65 78 70 72 2d 6c 69 73 74 29 22 20 2a 2f 0a  <expr-list)" */.
64240 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c      Select *pSel
64250 65 63 74 3b 20 20 20 20 20 2f 2a 20 55 73 65 64  ect;     /* Used
64260 20 66 6f 72 20 73 75 62 2d 73 65 6c 65 63 74 73   for sub-selects
64270 20 61 6e 64 20 22 3c 65 78 70 72 3e 20 49 4e 20   and "<expr> IN 
64280 28 3c 73 65 6c 65 63 74 3e 29 22 20 2a 2f 0a 20  (<select>)" */. 
64290 20 7d 20 78 3b 0a 20 20 43 6f 6c 6c 53 65 71 20   } x;.  CollSeq 
642a0 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 2f  *pColl;        /
642b0 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  * The collation 
642c0 74 79 70 65 20 6f 66 20 74 68 65 20 63 6f 6c 75  type of the colu
642d0 6d 6e 20 6f 72 20 30 20 2a 2f 0a 0a 20 20 2f 2a  mn or 0 */..  /*
642e0 20 49 66 20 74 68 65 20 45 50 5f 52 65 64 75 63   If the EP_Reduc
642f0 65 64 20 66 6c 61 67 20 69 73 20 73 65 74 20 69  ed flag is set i
64300 6e 20 74 68 65 20 45 78 70 72 2e 66 6c 61 67 73  n the Expr.flags
64310 20 6d 61 73 6b 2c 20 74 68 65 6e 20 6e 6f 0a 20   mask, then no. 
64320 20 2a 2a 20 73 70 61 63 65 20 69 73 20 61 6c 6c   ** space is all
64330 6f 63 61 74 65 64 20 66 6f 72 20 74 68 65 20 66  ocated for the f
64340 69 65 6c 64 73 20 62 65 6c 6f 77 20 74 68 69 73  ields below this
64350 20 70 6f 69 6e 74 2e 20 41 6e 20 61 74 74 65 6d   point. An attem
64360 70 74 20 74 6f 0a 20 20 2a 2a 20 61 63 63 65 73  pt to.  ** acces
64370 73 20 74 68 65 6d 20 77 69 6c 6c 20 72 65 73 75  s them will resu
64380 6c 74 20 69 6e 20 61 20 73 65 67 66 61 75 6c 74  lt in a segfault
64390 20 6f 72 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 2e   or malfunction.
643a0 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .  *************
643b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
643c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
643d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
643e0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 20 20 69 6e 74  ********/..  int
643f0 20 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20   iTable;        
64400 20 20 20 20 2f 2a 20 54 4b 5f 43 4f 4c 55 4d 4e      /* TK_COLUMN
64410 3a 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  : cursor number 
64420 6f 66 20 74 61 62 6c 65 20 68 6f 6c 64 69 6e 67  of table holding
64430 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 20 20   column.        
64440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64450 20 2a 2a 20 54 4b 5f 52 45 47 49 53 54 45 52 3a   ** TK_REGISTER:
64460 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72   register number
64470 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
64480 20 20 20 20 20 20 20 20 20 20 2a 2a 20 54 4b 5f            ** TK_
64490 54 52 49 47 47 45 52 3a 20 31 20 2d 3e 20 6e 65  TRIGGER: 1 -> ne
644a0 77 2c 20 30 20 2d 3e 20 6f 6c 64 20 2a 2f 0a 20  w, 0 -> old */. 
644b0 20 69 31 36 20 69 43 6f 6c 75 6d 6e 3b 20 20 20   i16 iColumn;   
644c0 20 20 20 20 20 20 20 20 2f 2a 20 54 4b 5f 43 4f          /* TK_CO
644d0 4c 55 4d 4e 3a 20 63 6f 6c 75 6d 6e 20 69 6e 64  LUMN: column ind
644e0 65 78 2e 20 20 2d 31 20 66 6f 72 20 72 6f 77 69  ex.  -1 for rowi
644f0 64 20 2a 2f 0a 20 20 69 31 36 20 69 41 67 67 3b  d */.  i16 iAgg;
64500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
64510 20 57 68 69 63 68 20 65 6e 74 72 79 20 69 6e 20   Which entry in 
64520 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d  pAggInfo->aCol[]
64530 20 6f 72 20 2d 3e 61 46 75 6e 63 5b 5d 20 2a 2f   or ->aFunc[] */
64540 0a 20 20 69 31 36 20 69 52 69 67 68 74 4a 6f 69  .  i16 iRightJoi
64550 6e 54 61 62 6c 65 3b 20 20 20 2f 2a 20 49 66 20  nTable;   /* If 
64560 45 50 5f 46 72 6f 6d 4a 6f 69 6e 2c 20 74 68 65  EP_FromJoin, the
64570 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20   right table of 
64580 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a 20 20 75 38  the join */.  u8
64590 20 66 6c 61 67 73 32 3b 20 20 20 20 20 20 20 20   flags2;        
645a0 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 73       /* Second s
645b0 65 74 20 6f 66 20 66 6c 61 67 73 2e 20 20 45 50  et of flags.  EP
645c0 32 5f 2e 2e 2e 20 2a 2f 0a 20 20 75 38 20 6f 70  2_... */.  u8 op
645d0 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  2;              
645e0 20 20 2f 2a 20 49 66 20 61 20 54 4b 5f 52 45 47    /* If a TK_REG
645f0 49 53 54 45 52 2c 20 74 68 65 20 6f 72 69 67 69  ISTER, the origi
64600 6e 61 6c 20 76 61 6c 75 65 20 6f 66 20 45 78 70  nal value of Exp
64610 72 2e 6f 70 20 2a 2f 0a 20 20 41 67 67 49 6e 66  r.op */.  AggInf
64620 6f 20 2a 70 41 67 67 49 6e 66 6f 3b 20 20 20 20  o *pAggInfo;    
64630 20 2f 2a 20 55 73 65 64 20 62 79 20 54 4b 5f 41   /* Used by TK_A
64640 47 47 5f 43 4f 4c 55 4d 4e 20 61 6e 64 20 54 4b  GG_COLUMN and TK
64650 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 2a 2f  _AGG_FUNCTION */
64660 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20  .  Table *pTab; 
64670 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62            /* Tab
64680 6c 65 20 66 6f 72 20 54 4b 5f 43 4f 4c 55 4d 4e  le for TK_COLUMN
64690 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 20 2a 2f   expressions. */
646a0 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  .#if SQLITE_MAX_
646b0 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 20 20 69  EXPR_DEPTH>0.  i
646c0 6e 74 20 6e 48 65 69 67 68 74 3b 20 20 20 20 20  nt nHeight;     
646d0 20 20 20 20 20 20 2f 2a 20 48 65 69 67 68 74 20        /* Height 
646e0 6f 66 20 74 68 65 20 74 72 65 65 20 68 65 61 64  of the tree head
646f0 65 64 20 62 79 20 74 68 69 73 20 6e 6f 64 65 20  ed by this node 
64700 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a  */.#endif.};../*
64710 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
64720 67 20 61 72 65 20 74 68 65 20 6d 65 61 6e 69 6e  g are the meanin
64730 67 73 20 6f 66 20 62 69 74 73 20 69 6e 20 74 68  gs of bits in th
64740 65 20 45 78 70 72 2e 66 6c 61 67 73 20 66 69 65  e Expr.flags fie
64750 6c 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45  ld..*/.#define E
64760 50 5f 46 72 6f 6d 4a 6f 69 6e 20 20 20 30 78 30  P_FromJoin   0x0
64770 30 30 31 20 20 2f 2a 20 4f 72 69 67 69 6e 61 74  001  /* Originat
64780 65 64 20 69 6e 20 4f 4e 20 6f 72 20 55 53 49 4e  ed in ON or USIN
64790 47 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f  G clause of a jo
647a0 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50  in */.#define EP
647b0 5f 41 67 67 20 20 20 20 20 20 20 20 30 78 30 30  _Agg        0x00
647c0 30 32 20 20 2f 2a 20 43 6f 6e 74 61 69 6e 73 20  02  /* Contains 
647d0 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 61 67 67 72  one or more aggr
647e0 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20  egate functions 
647f0 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 52 65  */.#define EP_Re
64800 73 6f 6c 76 65 64 20 20 20 30 78 30 30 30 34 20  solved   0x0004 
64810 20 2f 2a 20 49 44 73 20 68 61 76 65 20 62 65 65   /* IDs have bee
64820 6e 20 72 65 73 6f 6c 76 65 64 20 74 6f 20 43 4f  n resolved to CO
64830 4c 55 4d 4e 73 20 2a 2f 0a 23 64 65 66 69 6e 65  LUMNs */.#define
64840 20 45 50 5f 45 72 72 6f 72 20 20 20 20 20 20 30   EP_Error      0
64850 78 30 30 30 38 20 20 2f 2a 20 45 78 70 72 65 73  x0008  /* Expres
64860 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 6f 6e  sion contains on
64870 65 20 6f 72 20 6d 6f 72 65 20 65 72 72 6f 72 73  e or more errors
64880 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 44   */.#define EP_D
64890 69 73 74 69 6e 63 74 20 20 20 30 78 30 30 31 30  istinct   0x0010
648a0 20 20 2f 2a 20 41 67 67 72 65 67 61 74 65 20 66    /* Aggregate f
648b0 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 44 49 53  unction with DIS
648c0 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 2a 2f  TINCT keyword */
648d0 0a 23 64 65 66 69 6e 65 20 45 50 5f 56 61 72 53  .#define EP_VarS
648e0 65 6c 65 63 74 20 20 30 78 30 30 32 30 20 20 2f  elect  0x0020  /
648f0 2a 20 70 53 65 6c 65 63 74 20 69 73 20 63 6f 72  * pSelect is cor
64900 72 65 6c 61 74 65 64 2c 20 6e 6f 74 20 63 6f 6e  related, not con
64910 73 74 61 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65  stant */.#define
64920 20 45 50 5f 44 62 6c 51 75 6f 74 65 64 20 20 30   EP_DblQuoted  0
64930 78 30 30 34 30 20 20 2f 2a 20 74 6f 6b 65 6e 2e  x0040  /* token.
64940 7a 20 77 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79  z was originally
64950 20 69 6e 20 22 2e 2e 2e 22 20 2a 2f 0a 23 64 65   in "..." */.#de
64960 66 69 6e 65 20 45 50 5f 49 6e 66 69 78 46 75 6e  fine EP_InfixFun
64970 63 20 20 30 78 30 30 38 30 20 20 2f 2a 20 54 72  c  0x0080  /* Tr
64980 75 65 20 66 6f 72 20 61 6e 20 69 6e 66 69 78 20  ue for an infix 
64990 66 75 6e 63 74 69 6f 6e 3a 20 4c 49 4b 45 2c 20  function: LIKE, 
649a0 47 4c 4f 42 2c 20 65 74 63 20 2a 2f 0a 23 64 65  GLOB, etc */.#de
649b0 66 69 6e 65 20 45 50 5f 45 78 70 43 6f 6c 6c 61  fine EP_ExpColla
649c0 74 65 20 30 78 30 31 30 30 20 20 2f 2a 20 43 6f  te 0x0100  /* Co
649d0 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
649e0 20 73 70 65 63 69 66 69 65 64 20 65 78 70 6c 69   specified expli
649f0 63 69 74 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65  citly */.#define
64a00 20 45 50 5f 41 6e 79 41 66 66 20 20 20 20 20 30   EP_AnyAff     0
64a10 78 30 32 30 30 20 20 2f 2a 20 43 61 6e 20 74 61  x0200  /* Can ta
64a20 6b 65 20 61 20 63 61 63 68 65 64 20 63 6f 6c 75  ke a cached colu
64a30 6d 6e 20 6f 66 20 61 6e 79 20 61 66 66 69 6e 69  mn of any affini
64a40 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50  ty */.#define EP
64a50 5f 46 69 78 65 64 44 65 73 74 20 20 30 78 30 34  _FixedDest  0x04
64a60 30 30 20 20 2f 2a 20 52 65 73 75 6c 74 20 6e 65  00  /* Result ne
64a70 65 64 65 64 20 69 6e 20 61 20 73 70 65 63 69 66  eded in a specif
64a80 69 63 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 23  ic register */.#
64a90 64 65 66 69 6e 65 20 45 50 5f 49 6e 74 56 61 6c  define EP_IntVal
64aa0 75 65 20 20 20 30 78 30 38 30 30 20 20 2f 2a 20  ue   0x0800  /* 
64ab0 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 63 6f  Integer value co
64ac0 6e 74 61 69 6e 65 64 20 69 6e 20 75 2e 69 56 61  ntained in u.iVa
64ad0 6c 75 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45  lue */.#define E
64ae0 50 5f 78 49 73 53 65 6c 65 63 74 20 20 30 78 31  P_xIsSelect  0x1
64af0 30 30 30 20 20 2f 2a 20 78 2e 70 53 65 6c 65 63  000  /* x.pSelec
64b00 74 20 69 73 20 76 61 6c 69 64 20 28 6f 74 68 65  t is valid (othe
64b10 72 77 69 73 65 20 78 2e 70 4c 69 73 74 20 69 73  rwise x.pList is
64b20 29 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 45 50  ) */..#define EP
64b30 5f 52 65 64 75 63 65 64 20 20 20 20 30 78 32 30  _Reduced    0x20
64b40 30 30 20 20 2f 2a 20 45 78 70 72 20 73 74 72 75  00  /* Expr stru
64b50 63 74 20 69 73 20 45 58 50 52 5f 52 45 44 55 43  ct is EXPR_REDUC
64b60 45 44 53 49 5a 45 20 62 79 74 65 73 20 6f 6e 6c  EDSIZE bytes onl
64b70 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f  y */.#define EP_
64b80 54 6f 6b 65 6e 4f 6e 6c 79 20 20 30 78 34 30 30  TokenOnly  0x400
64b90 30 20 20 2f 2a 20 45 78 70 72 20 73 74 72 75 63  0  /* Expr struc
64ba0 74 20 69 73 20 45 58 50 52 5f 54 4f 4b 45 4e 4f  t is EXPR_TOKENO
64bb0 4e 4c 59 53 49 5a 45 20 62 79 74 65 73 20 6f 6e  NLYSIZE bytes on
64bc0 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50  ly */.#define EP
64bd0 5f 53 74 61 74 69 63 20 20 20 20 20 30 78 38 30  _Static     0x80
64be0 30 30 20 20 2f 2a 20 48 65 6c 64 20 69 6e 20 6d  00  /* Held in m
64bf0 65 6d 6f 72 79 20 6e 6f 74 20 6f 62 74 61 69 6e  emory not obtain
64c00 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29  ed from malloc()
64c10 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66   */../*.** The f
64c20 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 68 65  ollowing are the
64c30 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20 62 69 74   meanings of bit
64c40 73 20 69 6e 20 74 68 65 20 45 78 70 72 2e 66 6c  s in the Expr.fl
64c50 61 67 73 32 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23  ags2 field..*/.#
64c60 64 65 66 69 6e 65 20 45 50 32 5f 4d 61 6c 6c 6f  define EP2_Mallo
64c70 63 65 64 54 6f 6b 65 6e 20 20 30 78 30 30 30 31  cedToken  0x0001
64c80 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 73 71 6c    /* Need to sql
64c90 69 74 65 33 44 62 46 72 65 65 28 29 20 45 78 70  ite3DbFree() Exp
64ca0 72 2e 7a 54 6f 6b 65 6e 20 2a 2f 0a 23 64 65 66  r.zToken */.#def
64cb0 69 6e 65 20 45 50 32 5f 49 72 72 65 64 75 63 69  ine EP2_Irreduci
64cc0 62 6c 65 20 20 20 20 30 78 30 30 30 32 20 20 2f  ble    0x0002  /
64cd0 2a 20 43 61 6e 6e 6f 74 20 45 58 50 52 44 55 50  * Cannot EXPRDUP
64ce0 5f 52 45 44 55 43 45 20 74 68 69 73 20 45 78 70  _REDUCE this Exp
64cf0 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  r */../*.** The 
64d00 70 73 65 75 64 6f 2d 72 6f 75 74 69 6e 65 20 73  pseudo-routine s
64d10 71 6c 69 74 65 33 45 78 70 72 53 65 74 49 72 72  qlite3ExprSetIrr
64d20 65 64 75 63 69 62 6c 65 20 73 65 74 73 20 74 68  educible sets th
64d30 65 20 45 50 32 5f 49 72 72 65 64 75 63 69 62 6c  e EP2_Irreducibl
64d40 65 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20 61 6e 20  e.** flag on an 
64d50 65 78 70 72 65 73 73 69 6f 6e 20 73 74 72 75 63  expression struc
64d60 74 75 72 65 2e 20 20 54 68 69 73 20 66 6c 61 67  ture.  This flag
64d70 20 69 73 20 75 73 65 64 20 66 6f 72 20 56 56 26   is used for VV&
64d80 41 20 6f 6e 6c 79 2e 20 20 54 68 65 0a 2a 2a 20  A only.  The.** 
64d90 72 6f 75 74 69 6e 65 20 69 73 20 69 6d 70 6c 65  routine is imple
64da0 6d 65 6e 74 65 64 20 61 73 20 61 20 6d 61 63 72  mented as a macr
64db0 6f 20 74 68 61 74 20 6f 6e 6c 79 20 77 6f 72 6b  o that only work
64dc0 73 20 77 68 65 6e 20 69 6e 20 64 65 62 75 67 67  s when in debugg
64dd0 69 6e 67 20 6d 6f 64 65 2c 0a 2a 2a 20 73 6f 20  ing mode,.** so 
64de0 61 73 20 6e 6f 74 20 74 6f 20 62 75 72 64 65 6e  as not to burden
64df0 20 70 72 6f 64 75 63 74 69 6f 6e 20 63 6f 64 65   production code
64e00 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
64e10 54 45 5f 44 45 42 55 47 0a 23 20 64 65 66 69 6e  TE_DEBUG.# defin
64e20 65 20 45 78 70 72 53 65 74 49 72 72 65 64 75 63  e ExprSetIrreduc
64e30 69 62 6c 65 28 58 29 20 20 28 58 29 2d 3e 66 6c  ible(X)  (X)->fl
64e40 61 67 73 32 20 7c 3d 20 45 50 32 5f 49 72 72 65  ags2 |= EP2_Irre
64e50 64 75 63 69 62 6c 65 0a 23 65 6c 73 65 0a 23 20  ducible.#else.# 
64e60 64 65 66 69 6e 65 20 45 78 70 72 53 65 74 49 72  define ExprSetIr
64e70 72 65 64 75 63 69 62 6c 65 28 58 29 0a 23 65 6e  reducible(X).#en
64e80 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65  dif../*.** These
64e90 20 6d 61 63 72 6f 73 20 63 61 6e 20 62 65 20 75   macros can be u
64ea0 73 65 64 20 74 6f 20 74 65 73 74 2c 20 73 65 74  sed to test, set
64eb0 2c 20 6f 72 20 63 6c 65 61 72 20 62 69 74 73 20  , or clear bits 
64ec0 69 6e 20 74 68 65 20 0a 2a 2a 20 45 78 70 72 2e  in the .** Expr.
64ed0 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a  flags field..*/.
64ee0 23 64 65 66 69 6e 65 20 45 78 70 72 48 61 73 50  #define ExprHasP
64ef0 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20 20  roperty(E,P)    
64f00 20 28 28 28 45 29 2d 3e 66 6c 61 67 73 26 28 50   (((E)->flags&(P
64f10 29 29 3d 3d 28 50 29 29 0a 23 64 65 66 69 6e 65  ))==(P)).#define
64f20 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65   ExprHasAnyPrope
64f30 72 74 79 28 45 2c 50 29 20 20 28 28 28 45 29 2d  rty(E,P)  (((E)-
64f40 3e 66 6c 61 67 73 26 28 50 29 29 21 3d 30 29 0a  >flags&(P))!=0).
64f50 23 64 65 66 69 6e 65 20 45 78 70 72 53 65 74 50  #define ExprSetP
64f60 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20 20  roperty(E,P)    
64f70 20 28 45 29 2d 3e 66 6c 61 67 73 7c 3d 28 50 29   (E)->flags|=(P)
64f80 0a 23 64 65 66 69 6e 65 20 45 78 70 72 43 6c 65  .#define ExprCle
64f90 61 72 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20  arProperty(E,P) 
64fa0 20 20 28 45 29 2d 3e 66 6c 61 67 73 26 3d 7e 28    (E)->flags&=~(
64fb0 50 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73  P)../*.** Macros
64fc0 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68   to determine th
64fd0 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
64fe0 73 20 72 65 71 75 69 72 65 64 20 62 79 20 61 20  s required by a 
64ff0 6e 6f 72 6d 61 6c 20 45 78 70 72 20 0a 2a 2a 20  normal Expr .** 
65000 73 74 72 75 63 74 2c 20 61 6e 20 45 78 70 72 20  struct, an Expr 
65010 73 74 72 75 63 74 20 77 69 74 68 20 74 68 65 20  struct with the 
65020 45 50 5f 52 65 64 75 63 65 64 20 66 6c 61 67 20  EP_Reduced flag 
65030 73 65 74 20 69 6e 20 45 78 70 72 2e 66 6c 61 67  set in Expr.flag
65040 73 20 0a 2a 2a 20 61 6e 64 20 61 6e 20 45 78 70  s .** and an Exp
65050 72 20 73 74 72 75 63 74 20 77 69 74 68 20 74 68  r struct with th
65060 65 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 66  e EP_TokenOnly f
65070 6c 61 67 20 73 65 74 2e 0a 2a 2f 0a 23 64 65 66  lag set..*/.#def
65080 69 6e 65 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a  ine EXPR_FULLSIZ
65090 45 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65  E           size
650a0 6f 66 28 45 78 70 72 29 20 20 20 20 20 20 20 20  of(Expr)        
650b0 20 20 20 2f 2a 20 46 75 6c 6c 20 73 69 7a 65 20     /* Full size 
650c0 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 50 52 5f  */.#define EXPR_
650d0 52 45 44 55 43 45 44 53 49 5a 45 20 20 20 20 20  REDUCEDSIZE     
650e0 20 20 20 6f 66 66 73 65 74 6f 66 28 45 78 70 72     offsetof(Expr
650f0 2c 69 54 61 62 6c 65 29 20 20 2f 2a 20 43 6f 6d  ,iTable)  /* Com
65100 6d 6f 6e 20 66 65 61 74 75 72 65 73 20 2a 2f 0a  mon features */.
65110 23 64 65 66 69 6e 65 20 45 58 50 52 5f 54 4f 4b  #define EXPR_TOK
65120 45 4e 4f 4e 4c 59 53 49 5a 45 20 20 20 20 20 20  ENONLYSIZE      
65130 6f 66 66 73 65 74 6f 66 28 45 78 70 72 2c 70 4c  offsetof(Expr,pL
65140 65 66 74 29 20 20 20 2f 2a 20 46 65 77 65 72 20  eft)   /* Fewer 
65150 66 65 61 74 75 72 65 73 20 2a 2f 0a 0a 2f 2a 0a  features */../*.
65160 2a 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64 20  ** Flags passed 
65170 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 45 78  to the sqlite3Ex
65180 70 72 44 75 70 28 29 20 66 75 6e 63 74 69 6f 6e  prDup() function
65190 2e 20 53 65 65 20 74 68 65 20 68 65 61 64 65 72  . See the header
651a0 20 63 6f 6d 6d 65 6e 74 20 0a 2a 2a 20 61 62 6f   comment .** abo
651b0 76 65 20 73 71 6c 69 74 65 33 45 78 70 72 44 75  ve sqlite3ExprDu
651c0 70 28 29 20 66 6f 72 20 64 65 74 61 69 6c 73 2e  p() for details.
651d0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 50 52  .*/.#define EXPR
651e0 44 55 50 5f 52 45 44 55 43 45 20 20 20 20 20 20  DUP_REDUCE      
651f0 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 55 73     0x0001  /* Us
65200 65 64 20 72 65 64 75 63 65 64 2d 73 69 7a 65 20  ed reduced-size 
65210 45 78 70 72 20 6e 6f 64 65 73 20 2a 2f 0a 0a 2f  Expr nodes */../
65220 2a 0a 2a 2a 20 41 20 6c 69 73 74 20 6f 66 20 65  *.** A list of e
65230 78 70 72 65 73 73 69 6f 6e 73 2e 20 20 45 61 63  xpressions.  Eac
65240 68 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 61 79  h expression may
65250 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 68 61 76 65   optionally have
65260 20 61 0a 2a 2a 20 6e 61 6d 65 2e 20 20 41 6e 20   a.** name.  An 
65270 65 78 70 72 2f 6e 61 6d 65 20 63 6f 6d 62 69 6e  expr/name combin
65280 61 74 69 6f 6e 20 63 61 6e 20 62 65 20 75 73 65  ation can be use
65290 64 20 69 6e 20 73 65 76 65 72 61 6c 20 77 61 79  d in several way
652a0 73 2c 20 73 75 63 68 0a 2a 2a 20 61 73 20 74 68  s, such.** as th
652b0 65 20 6c 69 73 74 20 6f 66 20 22 65 78 70 72 20  e list of "expr 
652c0 41 53 20 49 44 22 20 66 69 65 6c 64 73 20 66 6f  AS ID" fields fo
652d0 6c 6c 6f 77 69 6e 67 20 61 20 22 53 45 4c 45 43  llowing a "SELEC
652e0 54 22 20 6f 72 20 69 6e 20 74 68 65 0a 2a 2a 20  T" or in the.** 
652f0 6c 69 73 74 20 6f 66 20 22 49 44 20 3d 20 65 78  list of "ID = ex
65300 70 72 22 20 69 74 65 6d 73 20 69 6e 20 61 6e 20  pr" items in an 
65310 55 50 44 41 54 45 2e 20 20 41 20 6c 69 73 74 20  UPDATE.  A list 
65320 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 63  of expressions c
65330 61 6e 0a 2a 2a 20 61 6c 73 6f 20 62 65 20 75 73  an.** also be us
65340 65 64 20 61 73 20 74 68 65 20 61 72 67 75 6d 65  ed as the argume
65350 6e 74 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e  nt to a function
65360 2c 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20  , in which case 
65370 74 68 65 20 61 2e 7a 4e 61 6d 65 0a 2a 2a 20 66  the a.zName.** f
65380 69 65 6c 64 20 69 73 20 6e 6f 74 20 75 73 65 64  ield is not used
65390 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 45 78 70 72  ..*/.struct Expr
653a0 4c 69 73 74 20 7b 0a 20 20 69 6e 74 20 6e 45 78  List {.  int nEx
653b0 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  pr;             
653c0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 78 70  /* Number of exp
653d0 72 65 73 73 69 6f 6e 73 20 6f 6e 20 74 68 65 20  ressions on the 
653e0 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 41  list */.  int nA
653f0 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 20 20  lloc;           
65400 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
65410 74 72 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 20  tries allocated 
65420 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 69  below */.  int i
65430 45 43 75 72 73 6f 72 3b 20 20 20 20 20 20 20 20  ECursor;        
65440 20 20 2f 2a 20 56 44 42 45 20 43 75 72 73 6f 72    /* VDBE Cursor
65450 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
65460 20 74 68 69 73 20 45 78 70 72 4c 69 73 74 20 2a   this ExprList *
65470 2f 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c  /.  struct ExprL
65480 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20 20 45  ist_item {.    E
65490 78 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20  xpr *pExpr;     
654a0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 69 73        /* The lis
654b0 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73  t of expressions
654c0 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e   */.    char *zN
654d0 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f  ame;           /
654e0 2a 20 54 6f 6b 65 6e 20 61 73 73 6f 63 69 61 74  * Token associat
654f0 65 64 20 77 69 74 68 20 74 68 69 73 20 65 78 70  ed with this exp
65500 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 63  ression */.    c
65510 68 61 72 20 2a 7a 53 70 61 6e 3b 20 20 20 20 20  har *zSpan;     
65520 20 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61        /* Origina
65530 6c 20 74 65 78 74 20 6f 66 20 74 68 65 20 65 78  l text of the ex
65540 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20  pression */.    
65550 75 38 20 73 6f 72 74 4f 72 64 65 72 3b 20 20 20  u8 sortOrder;   
65560 20 20 20 20 20 20 20 2f 2a 20 31 20 66 6f 72 20         /* 1 for 
65570 44 45 53 43 20 6f 72 20 30 20 66 6f 72 20 41 53  DESC or 0 for AS
65580 43 20 2a 2f 0a 20 20 20 20 75 38 20 64 6f 6e 65  C */.    u8 done
65590 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
655a0 2f 2a 20 41 20 66 6c 61 67 20 74 6f 20 69 6e 64  /* A flag to ind
655b0 69 63 61 74 65 20 77 68 65 6e 20 70 72 6f 63 65  icate when proce
655c0 73 73 69 6e 67 20 69 73 20 66 69 6e 69 73 68 65  ssing is finishe
655d0 64 20 2a 2f 0a 20 20 20 20 75 31 36 20 69 43 6f  d */.    u16 iCo
655e0 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
655f0 2f 2a 20 46 6f 72 20 4f 52 44 45 52 20 42 59 2c  /* For ORDER BY,
65600 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 69   column number i
65610 6e 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a  n result set */.
65620 20 20 20 20 75 31 36 20 69 41 6c 69 61 73 3b 20      u16 iAlias; 
65630 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
65640 64 65 78 20 69 6e 74 6f 20 50 61 72 73 65 2e 61  dex into Parse.a
65650 41 6c 69 61 73 5b 5d 20 66 6f 72 20 7a 4e 61 6d  Alias[] for zNam
65660 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b 20 20 20 20  e */.  } *a;    
65670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
65680 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 65   One entry for e
65690 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e 20 2a  ach expression *
656a0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  /.};../*.** An i
656b0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20  nstance of this 
656c0 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65  structure is use
656d0 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20  d by the parser 
656e0 74 6f 20 72 65 63 6f 72 64 20 62 6f 74 68 0a 2a  to record both.*
656f0 2a 20 74 68 65 20 70 61 72 73 65 20 74 72 65 65  * the parse tree
65700 20 66 6f 72 20 61 6e 20 65 78 70 72 65 73 73 69   for an expressi
65710 6f 6e 20 61 6e 64 20 74 68 65 20 73 70 61 6e 20  on and the span 
65720 6f 66 20 69 6e 70 75 74 20 74 65 78 74 20 66 6f  of input text fo
65730 72 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69  r an.** expressi
65740 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 45 78  on..*/.struct Ex
65750 70 72 53 70 61 6e 20 7b 0a 20 20 45 78 70 72 20  prSpan {.  Expr 
65760 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 20  *pExpr;         
65770 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69   /* The expressi
65780 6f 6e 20 70 61 72 73 65 20 74 72 65 65 20 2a 2f  on parse tree */
65790 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
657a0 53 74 61 72 74 3b 20 20 20 2f 2a 20 46 69 72 73  Start;   /* Firs
657b0 74 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 69  t character of i
657c0 6e 70 75 74 20 74 65 78 74 20 2a 2f 0a 20 20 63  nput text */.  c
657d0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 6e 64 3b  onst char *zEnd;
657e0 20 20 20 20 20 2f 2a 20 4f 6e 65 20 63 68 61 72       /* One char
657f0 61 63 74 65 72 20 70 61 73 74 20 74 68 65 20 65  acter past the e
65800 6e 64 20 6f 66 20 69 6e 70 75 74 20 74 65 78 74  nd of input text
65810 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e   */.};../*.** An
65820 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
65830 73 20 73 74 72 75 63 74 75 72 65 20 63 61 6e 20  s structure can 
65840 68 6f 6c 64 20 61 20 73 69 6d 70 6c 65 20 6c 69  hold a simple li
65850 73 74 20 6f 66 20 69 64 65 6e 74 69 66 69 65 72  st of identifier
65860 73 2c 0a 2a 2a 20 73 75 63 68 20 61 73 20 74 68  s,.** such as th
65870 65 20 6c 69 73 74 20 22 61 2c 62 2c 63 22 20 69  e list "a,b,c" i
65880 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  n the following 
65890 73 74 61 74 65 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a  statements:.**.*
658a0 2a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e  *      INSERT IN
658b0 54 4f 20 74 28 61 2c 62 2c 63 29 20 56 41 4c 55  TO t(a,b,c) VALU
658c0 45 53 20 2e 2e 2e 3b 0a 2a 2a 20 20 20 20 20 20  ES ...;.**      
658d0 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 64 78  CREATE INDEX idx
658e0 20 4f 4e 20 74 28 61 2c 62 2c 63 29 3b 0a 2a 2a   ON t(a,b,c);.**
658f0 20 20 20 20 20 20 43 52 45 41 54 45 20 54 52 49        CREATE TRI
65900 47 47 45 52 20 74 72 69 67 20 42 45 46 4f 52 45  GGER trig BEFORE
65910 20 55 50 44 41 54 45 20 4f 4e 20 74 28 61 2c 62   UPDATE ON t(a,b
65920 2c 63 29 20 2e 2e 2e 3b 0a 2a 2a 0a 2a 2a 20 54  ,c) ...;.**.** T
65930 68 65 20 49 64 4c 69 73 74 2e 61 2e 69 64 78 20  he IdList.a.idx 
65940 66 69 65 6c 64 20 69 73 20 75 73 65 64 20 77 68  field is used wh
65950 65 6e 20 74 68 65 20 49 64 4c 69 73 74 20 72 65  en the IdList re
65960 70 72 65 73 65 6e 74 73 20 74 68 65 20 6c 69 73  presents the lis
65970 74 20 6f 66 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6e  t of.** column n
65980 61 6d 65 73 20 61 66 74 65 72 20 61 20 74 61 62  ames after a tab
65990 6c 65 20 6e 61 6d 65 20 69 6e 20 61 6e 20 49 4e  le name in an IN
659a0 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 2e 20  SERT statement. 
659b0 20 49 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e   In the statemen
659c0 74 0a 2a 2a 0a 2a 2a 20 20 20 20 20 49 4e 53 45  t.**.**     INSE
659d0 52 54 20 49 4e 54 4f 20 74 28 61 2c 62 2c 63 29  RT INTO t(a,b,c)
659e0 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 22 61   ....**.** If "a
659f0 22 20 69 73 20 74 68 65 20 6b 2d 74 68 20 63 6f  " is the k-th co
65a00 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 22 74  lumn of table "t
65a10 22 2c 20 74 68 65 6e 20 49 64 4c 69 73 74 2e 61  ", then IdList.a
65a20 5b 30 5d 2e 69 64 78 3d 3d 6b 2e 0a 2a 2f 0a 73  [0].idx==k..*/.s
65a30 74 72 75 63 74 20 49 64 4c 69 73 74 20 7b 0a 20  truct IdList {. 
65a40 20 73 74 72 75 63 74 20 49 64 4c 69 73 74 5f 69   struct IdList_i
65a50 74 65 6d 20 7b 0a 20 20 20 20 63 68 61 72 20 2a  tem {.    char *
65a60 7a 4e 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20 4e  zName;      /* N
65a70 61 6d 65 20 6f 66 20 74 68 65 20 69 64 65 6e 74  ame of the ident
65a80 69 66 69 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74  ifier */.    int
65a90 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 2f   idx;          /
65aa0 2a 20 49 6e 64 65 78 20 69 6e 20 73 6f 6d 65 20  * Index in some 
65ab0 54 61 62 6c 65 2e 61 43 6f 6c 5b 5d 20 6f 66 20  Table.aCol[] of 
65ac0 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 7a  a column named z
65ad0 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b 0a  Name */.  } *a;.
65ae0 20 20 69 6e 74 20 6e 49 64 3b 20 20 20 20 20 20    int nId;      
65af0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
65b00 69 64 65 6e 74 69 66 69 65 72 73 20 6f 6e 20 74  identifiers on t
65b10 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74  he list */.  int
65b20 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 2f 2a   nAlloc;      /*
65b30 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
65b40 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72  es allocated for
65b50 20 61 5b 5d 20 62 65 6c 6f 77 20 2a 2f 0a 7d 3b   a[] below */.};
65b60 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 62 69 74 6d  ../*.** The bitm
65b70 61 73 6b 20 64 61 74 61 74 79 70 65 20 64 65 66  ask datatype def
65b80 69 6e 65 64 20 62 65 6c 6f 77 20 69 73 20 75 73  ined below is us
65b90 65 64 20 66 6f 72 20 76 61 72 69 6f 75 73 20 6f  ed for various o
65ba0 70 74 69 6d 69 7a 61 74 69 6f 6e 73 2e 0a 2a 2a  ptimizations..**
65bb0 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 68 69  .** Changing thi
65bc0 73 20 66 72 6f 6d 20 61 20 36 34 2d 62 69 74 20  s from a 64-bit 
65bd0 74 6f 20 61 20 33 32 2d 62 69 74 20 74 79 70 65  to a 32-bit type
65be0 20 6c 69 6d 69 74 73 20 74 68 65 20 6e 75 6d 62   limits the numb
65bf0 65 72 20 6f 66 0a 2a 2a 20 74 61 62 6c 65 73 20  er of.** tables 
65c00 69 6e 20 61 20 6a 6f 69 6e 20 74 6f 20 33 32 20  in a join to 32 
65c10 69 6e 73 74 65 61 64 20 6f 66 20 36 34 2e 20 20  instead of 64.  
65c20 42 75 74 20 69 74 20 61 6c 73 6f 20 72 65 64 75  But it also redu
65c30 63 65 73 20 74 68 65 20 73 69 7a 65 0a 2a 2a 20  ces the size.** 
65c40 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 62  of the library b
65c50 79 20 37 33 38 20 62 79 74 65 73 20 6f 6e 20 69  y 738 bytes on i
65c60 78 38 36 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  x86..*/.typedef 
65c70 75 36 34 20 42 69 74 6d 61 73 6b 3b 0a 0a 2f 2a  u64 Bitmask;../*
65c80 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f  .** The number o
65c90 66 20 62 69 74 73 20 69 6e 20 61 20 42 69 74 6d  f bits in a Bitm
65ca0 61 73 6b 2e 20 20 22 42 4d 53 22 20 6d 65 61 6e  ask.  "BMS" mean
65cb0 73 20 22 42 69 74 4d 61 73 6b 20 53 69 7a 65 22  s "BitMask Size"
65cc0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 4d 53  ..*/.#define BMS
65cd0 20 20 28 28 69 6e 74 29 28 73 69 7a 65 6f 66 28    ((int)(sizeof(
65ce0 42 69 74 6d 61 73 6b 29 2a 38 29 29 0a 0a 2f 2a  Bitmask)*8))../*
65cf0 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
65d00 67 20 73 74 72 75 63 74 75 72 65 20 64 65 73 63  g structure desc
65d10 72 69 62 65 73 20 74 68 65 20 46 52 4f 4d 20 63  ribes the FROM c
65d20 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43  lause of a SELEC
65d30 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20  T statement..** 
65d40 45 61 63 68 20 74 61 62 6c 65 20 6f 72 20 73 75  Each table or su
65d50 62 71 75 65 72 79 20 69 6e 20 74 68 65 20 46 52  bquery in the FR
65d60 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 61 20 73  OM clause is a s
65d70 65 70 61 72 61 74 65 20 65 6c 65 6d 65 6e 74 20  eparate element 
65d80 6f 66 0a 2a 2a 20 74 68 65 20 53 72 63 4c 69 73  of.** the SrcLis
65d90 74 2e 61 5b 5d 20 61 72 72 61 79 2e 0a 2a 2a 0a  t.a[] array..**.
65da0 2a 2a 20 57 69 74 68 20 74 68 65 20 61 64 64 69  ** With the addi
65db0 74 69 6f 6e 20 6f 66 20 6d 75 6c 74 69 70 6c 65  tion of multiple
65dc0 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72   database suppor
65dd0 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  t, the following
65de0 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 63 61   structure.** ca
65df0 6e 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20 74  n also be used t
65e00 6f 20 64 65 73 63 72 69 62 65 20 61 20 70 61 72  o describe a par
65e10 74 69 63 75 6c 61 72 20 74 61 62 6c 65 20 73 75  ticular table su
65e20 63 68 20 61 73 20 74 68 65 20 74 61 62 6c 65 20  ch as the table 
65e30 74 68 61 74 0a 2a 2a 20 69 73 20 6d 6f 64 69 66  that.** is modif
65e40 69 65 64 20 62 79 20 61 6e 20 49 4e 53 45 52 54  ied by an INSERT
65e50 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 50 44  , DELETE, or UPD
65e60 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  ATE statement.  
65e70 49 6e 20 73 74 61 6e 64 61 72 64 20 53 51 4c 2c  In standard SQL,
65e80 0a 2a 2a 20 73 75 63 68 20 61 20 74 61 62 6c 65  .** such a table
65e90 20 6d 75 73 74 20 62 65 20 61 20 73 69 6d 70 6c   must be a simpl
65ea0 65 20 6e 61 6d 65 3a 20 49 44 2e 20 20 42 75 74  e name: ID.  But
65eb0 20 69 6e 20 53 51 4c 69 74 65 2c 20 74 68 65 20   in SQLite, the 
65ec0 74 61 62 6c 65 20 63 61 6e 0a 2a 2a 20 6e 6f 77  table can.** now
65ed0 20 62 65 20 69 64 65 6e 74 69 66 69 65 64 20 62   be identified b
65ee0 79 20 61 20 64 61 74 61 62 61 73 65 20 6e 61 6d  y a database nam
65ef0 65 2c 20 61 20 64 6f 74 2c 20 74 68 65 6e 20 74  e, a dot, then t
65f00 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 3a 20 49  he table name: I
65f10 44 2e 49 44 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  D.ID..**.** The 
65f20 6a 6f 69 6e 74 79 70 65 20 73 74 61 72 74 73 20  jointype starts 
65f30 6f 75 74 20 73 68 6f 77 69 6e 67 20 74 68 65 20  out showing the 
65f40 6a 6f 69 6e 20 74 79 70 65 20 62 65 74 77 65 65  join type betwee
65f50 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 61  n the current ta
65f60 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6e  ble.** and the n
65f70 65 78 74 20 74 61 62 6c 65 20 6f 6e 20 74 68 65  ext table on the
65f80 20 6c 69 73 74 2e 20 20 54 68 65 20 70 61 72 73   list.  The pars
65f90 65 72 20 62 75 69 6c 64 73 20 74 68 65 20 6c 69  er builds the li
65fa0 73 74 20 74 68 69 73 20 77 61 79 2e 0a 2a 2a 20  st this way..** 
65fb0 42 75 74 20 73 71 6c 69 74 65 33 53 72 63 4c 69  But sqlite3SrcLi
65fc0 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 65 28  stShiftJoinType(
65fd0 29 20 6c 61 74 65 72 20 73 68 69 66 74 73 20 74  ) later shifts t
65fe0 68 65 20 6a 6f 69 6e 74 79 70 65 73 20 73 6f 20  he jointypes so 
65ff0 74 68 61 74 20 65 61 63 68 0a 2a 2a 20 6a 6f 69  that each.** joi
66000 6e 74 79 70 65 20 65 78 70 72 65 73 73 65 73 20  ntype expresses 
66010 74 68 65 20 6a 6f 69 6e 20 62 65 74 77 65 65 6e  the join between
66020 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 74   the table and t
66030 68 65 20 70 72 65 76 69 6f 75 73 20 74 61 62 6c  he previous tabl
66040 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 72 63  e..*/.struct Src
66050 4c 69 73 74 20 7b 0a 20 20 69 31 36 20 6e 53 72  List {.  i16 nSr
66060 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  c;        /* Num
66070 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 6f 72  ber of tables or
66080 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20 74   subqueries in t
66090 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a  he FROM clause *
660a0 2f 0a 20 20 69 31 36 20 6e 41 6c 6c 6f 63 3b 20  /.  i16 nAlloc; 
660b0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
660c0 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61  f entries alloca
660d0 74 65 64 20 69 6e 20 61 5b 5d 20 62 65 6c 6f 77  ted in a[] below
660e0 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63   */.  struct Src
660f0 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20 20  List_item {.    
66100 63 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65 3b  char *zDatabase;
66110 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74    /* Name of dat
66120 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 74 68  abase holding th
66130 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  is table */.    
66140 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20  char *zName;    
66150 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
66160 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 63 68   table */.    ch
66170 61 72 20 2a 7a 41 6c 69 61 73 3b 20 20 20 20 20  ar *zAlias;     
66180 2f 2a 20 54 68 65 20 22 42 22 20 70 61 72 74 20  /* The "B" part 
66190 6f 66 20 61 20 22 41 20 41 53 20 42 22 20 70 68  of a "A AS B" ph
661a0 72 61 73 65 2e 20 20 7a 4e 61 6d 65 20 69 73 20  rase.  zName is 
661b0 74 68 65 20 22 41 22 20 2a 2f 0a 20 20 20 20 54  the "A" */.    T
661c0 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20  able *pTab;     
661d0 20 2f 2a 20 41 6e 20 53 51 4c 20 74 61 62 6c 65   /* An SQL table
661e0 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74   corresponding t
661f0 6f 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 53  o zName */.    S
66200 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20  elect *pSelect; 
66210 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74 61   /* A SELECT sta
66220 74 65 6d 65 6e 74 20 75 73 65 64 20 69 6e 20 70  tement used in p
66230 6c 61 63 65 20 6f 66 20 61 20 74 61 62 6c 65 20  lace of a table 
66240 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 75 38 20 69  name */.    u8 i
66250 73 50 6f 70 75 6c 61 74 65 64 3b 20 20 20 2f 2a  sPopulated;   /*
66260 20 54 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   Temporary table
66270 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
66280 20 53 45 4c 45 43 54 20 69 73 20 70 6f 70 75 6c   SELECT is popul
66290 61 74 65 64 20 2a 2f 0a 20 20 20 20 75 38 20 6a  ated */.    u8 j
662a0 6f 69 6e 74 79 70 65 3b 20 20 20 20 20 20 2f 2a  ointype;      /*
662b0 20 54 79 70 65 20 6f 66 20 6a 6f 69 6e 20 62 65   Type of join be
662c0 74 77 65 65 6e 20 74 68 69 73 20 61 62 6c 65 20  tween this able 
662d0 61 6e 64 20 74 68 65 20 70 72 65 76 69 6f 75 73  and the previous
662e0 20 2a 2f 0a 20 20 20 20 75 38 20 6e 6f 74 49 6e   */.    u8 notIn
662f0 64 65 78 65 64 3b 20 20 20 20 2f 2a 20 54 72 75  dexed;    /* Tru
66300 65 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20  e if there is a 
66310 4e 4f 54 20 49 4e 44 45 58 45 44 20 63 6c 61 75  NOT INDEXED clau
66320 73 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43  se */.    int iC
66330 75 72 73 6f 72 3b 20 20 20 20 20 20 2f 2a 20 54  ursor;      /* T
66340 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e  he VDBE cursor n
66350 75 6d 62 65 72 20 75 73 65 64 20 74 6f 20 61 63  umber used to ac
66360 63 65 73 73 20 74 68 69 73 20 74 61 62 6c 65 20  cess this table 
66370 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 4f 6e  */.    Expr *pOn
66380 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ;        /* The 
66390 4f 4e 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a  ON clause of a j
663a0 6f 69 6e 20 2a 2f 0a 20 20 20 20 49 64 4c 69 73  oin */.    IdLis
663b0 74 20 2a 70 55 73 69 6e 67 3b 20 20 20 2f 2a 20  t *pUsing;   /* 
663c0 54 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65  The USING clause
663d0 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20   of a join */.  
663e0 20 20 42 69 74 6d 61 73 6b 20 63 6f 6c 55 73 65    Bitmask colUse
663f0 64 3b 20 20 2f 2a 20 42 69 74 20 4e 20 28 31 3c  d;  /* Bit N (1<
66400 3c 4e 29 20 73 65 74 20 69 66 20 63 6f 6c 75 6d  <N) set if colum
66410 6e 20 4e 20 6f 66 20 70 54 61 62 20 69 73 20 75  n N of pTab is u
66420 73 65 64 20 2a 2f 0a 20 20 20 20 63 68 61 72 20  sed */.    char 
66430 2a 7a 49 6e 64 65 78 3b 20 20 20 20 20 2f 2a 20  *zIndex;     /* 
66440 49 64 65 6e 74 69 66 69 65 72 20 66 72 6f 6d 20  Identifier from 
66450 22 49 4e 44 45 58 45 44 20 42 59 20 3c 7a 49 6e  "INDEXED BY <zIn
66460 64 65 78 3e 22 20 63 6c 61 75 73 65 20 2a 2f 0a  dex>" clause */.
66470 20 20 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65      Index *pInde
66480 78 3b 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 73  x;    /* Index s
66490 74 72 75 63 74 75 72 65 20 63 6f 72 72 65 73 70  tructure corresp
664a0 6f 6e 64 69 6e 67 20 74 6f 20 7a 49 6e 64 65 78  onding to zIndex
664b0 2c 20 69 66 20 61 6e 79 20 2a 2f 0a 20 20 7d 20  , if any */.  } 
664c0 61 5b 31 5d 3b 20 20 20 20 20 20 20 20 20 20 20  a[1];           
664d0 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66    /* One entry f
664e0 6f 72 20 65 61 63 68 20 69 64 65 6e 74 69 66 69  or each identifi
664f0 65 72 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a  er on the list *
66500 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 6d  /.};../*.** Perm
66510 69 74 74 65 64 20 76 61 6c 75 65 73 20 6f 66 20  itted values of 
66520 74 68 65 20 53 72 63 4c 69 73 74 2e 61 2e 6a 6f  the SrcList.a.jo
66530 69 6e 74 79 70 65 20 66 69 65 6c 64 0a 2a 2f 0a  intype field.*/.
66540 23 64 65 66 69 6e 65 20 4a 54 5f 49 4e 4e 45 52  #define JT_INNER
66550 20 20 20 20 20 30 78 30 30 30 31 20 20 20 20 2f       0x0001    /
66560 2a 20 41 6e 79 20 6b 69 6e 64 20 6f 66 20 69 6e  * Any kind of in
66570 6e 65 72 20 6f 72 20 63 72 6f 73 73 20 6a 6f 69  ner or cross joi
66580 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f  n */.#define JT_
66590 43 52 4f 53 53 20 20 20 20 20 30 78 30 30 30 32  CROSS     0x0002
665a0 20 20 20 20 2f 2a 20 45 78 70 6c 69 63 69 74 20      /* Explicit 
665b0 75 73 65 20 6f 66 20 74 68 65 20 43 52 4f 53 53  use of the CROSS
665c0 20 6b 65 79 77 6f 72 64 20 2a 2f 0a 23 64 65 66   keyword */.#def
665d0 69 6e 65 20 4a 54 5f 4e 41 54 55 52 41 4c 20 20  ine JT_NATURAL  
665e0 20 30 78 30 30 30 34 20 20 20 20 2f 2a 20 54 72   0x0004    /* Tr
665f0 75 65 20 66 6f 72 20 61 20 22 6e 61 74 75 72 61  ue for a "natura
66600 6c 22 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69  l" join */.#defi
66610 6e 65 20 4a 54 5f 4c 45 46 54 20 20 20 20 20 20  ne JT_LEFT      
66620 30 78 30 30 30 38 20 20 20 20 2f 2a 20 4c 65 66  0x0008    /* Lef
66630 74 20 6f 75 74 65 72 20 6a 6f 69 6e 20 2a 2f 0a  t outer join */.
66640 23 64 65 66 69 6e 65 20 4a 54 5f 52 49 47 48 54  #define JT_RIGHT
66650 20 20 20 20 20 30 78 30 30 31 30 20 20 20 20 2f       0x0010    /
66660 2a 20 52 69 67 68 74 20 6f 75 74 65 72 20 6a 6f  * Right outer jo
66670 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54  in */.#define JT
66680 5f 4f 55 54 45 52 20 20 20 20 20 30 78 30 30 32  _OUTER     0x002
66690 30 20 20 20 20 2f 2a 20 54 68 65 20 22 4f 55 54  0    /* The "OUT
666a0 45 52 22 20 6b 65 79 77 6f 72 64 20 69 73 20 70  ER" keyword is p
666b0 72 65 73 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e  resent */.#defin
666c0 65 20 4a 54 5f 45 52 52 4f 52 20 20 20 20 20 30  e JT_ERROR     0
666d0 78 30 30 34 30 20 20 20 20 2f 2a 20 75 6e 6b 6e  x0040    /* unkn
666e0 6f 77 6e 20 6f 72 20 75 6e 73 75 70 70 6f 72 74  own or unsupport
666f0 65 64 20 6a 6f 69 6e 20 74 79 70 65 20 2a 2f 0a  ed join type */.
66700 0a 0a 2f 2a 0a 2a 2a 20 41 20 57 68 65 72 65 50  ../*.** A WhereP
66710 6c 61 6e 20 6f 62 6a 65 63 74 20 68 6f 6c 64 73  lan object holds
66720 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61   information tha
66730 74 20 64 65 73 63 72 69 62 65 73 20 61 20 6c 6f  t describes a lo
66740 6f 6b 75 70 0a 2a 2a 20 73 74 72 61 74 65 67 79  okup.** strategy
66750 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 62 6a  ..**.** This obj
66760 65 63 74 20 69 73 20 69 6e 74 65 6e 64 65 64 20  ect is intended 
66770 74 6f 20 62 65 20 6f 70 61 71 75 65 20 6f 75 74  to be opaque out
66780 73 69 64 65 20 6f 66 20 74 68 65 20 77 68 65 72  side of the wher
66790 65 2e 63 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 20 49  e.c module..** I
667a0 74 20 69 73 20 69 6e 63 6c 75 64 65 64 20 68 65  t is included he
667b0 72 65 20 6f 6e 6c 79 20 73 6f 20 74 68 61 74 20  re only so that 
667c0 74 68 61 74 20 63 6f 6d 70 69 6c 65 72 20 77 69  that compiler wi
667d0 6c 6c 20 6b 6e 6f 77 20 68 6f 77 20 62 69 67 20  ll know how big 
667e0 69 74 0a 2a 2a 20 69 73 2e 20 20 4e 6f 6e 65 20  it.** is.  None 
667f0 6f 66 20 74 68 65 20 66 69 65 6c 64 73 20 69 6e  of the fields in
66800 20 74 68 69 73 20 6f 62 6a 65 63 74 20 73 68 6f   this object sho
66810 75 6c 64 20 62 65 20 75 73 65 64 20 6f 75 74 73  uld be used outs
66820 69 64 65 20 6f 66 0a 2a 2a 20 74 68 65 20 77 68  ide of.** the wh
66830 65 72 65 2e 63 20 6d 6f 64 75 6c 65 2e 0a 2a 2a  ere.c module..**
66840 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68 65 20 75  .** Within the u
66850 6e 69 6f 6e 2c 20 70 49 64 78 20 69 73 20 6f 6e  nion, pIdx is on
66860 6c 79 20 75 73 65 64 20 77 68 65 6e 20 77 73 46  ly used when wsF
66870 6c 61 67 73 26 57 48 45 52 45 5f 49 4e 44 45 58  lags&WHERE_INDEX
66880 45 44 20 69 73 20 74 72 75 65 2e 0a 2a 2a 20 70  ED is true..** p
66890 54 65 72 6d 20 69 73 20 6f 6e 6c 79 20 75 73 65  Term is only use
668a0 64 20 77 68 65 6e 20 77 73 46 6c 61 67 73 26 57  d when wsFlags&W
668b0 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 20 69 73  HERE_MULTI_OR is
668c0 20 74 72 75 65 2e 20 20 41 6e 64 20 70 56 74 61   true.  And pVta
668d0 62 49 64 78 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20  bIdx.** is only 
668e0 75 73 65 64 20 77 68 65 6e 20 77 73 46 6c 61 67  used when wsFlag
668f0 73 26 57 48 45 52 45 5f 56 49 52 54 55 41 4c 54  s&WHERE_VIRTUALT
66900 41 42 4c 45 20 69 73 20 74 72 75 65 2e 20 20 49  ABLE is true.  I
66910 74 20 69 73 20 6e 65 76 65 72 20 74 68 65 0a 2a  t is never the.*
66920 2a 20 63 61 73 65 20 74 68 61 74 20 6d 6f 72 65  * case that more
66930 20 74 68 61 6e 20 6f 6e 65 20 6f 66 20 74 68 65   than one of the
66940 73 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 69 73  se conditions is
66950 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 72 75 63 74   true..*/.struct
66960 20 57 68 65 72 65 50 6c 61 6e 20 7b 0a 20 20 75   WherePlan {.  u
66970 33 32 20 77 73 46 6c 61 67 73 3b 20 20 20 20 20  32 wsFlags;     
66980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
66990 20 57 48 45 52 45 5f 2a 20 66 6c 61 67 73 20 74   WHERE_* flags t
669a0 68 61 74 20 64 65 73 63 72 69 62 65 20 74 68 65  hat describe the
669b0 20 73 74 72 61 74 65 67 79 20 2a 2f 0a 20 20 75   strategy */.  u
669c0 33 32 20 6e 45 71 3b 20 20 20 20 20 20 20 20 20  32 nEq;         
669d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
669e0 20 4e 75 6d 62 65 72 20 6f 66 20 3d 3d 20 63 6f   Number of == co
669f0 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 75  nstraints */.  u
66a00 6e 69 6f 6e 20 7b 0a 20 20 20 20 49 6e 64 65 78  nion {.    Index
66a10 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20 20   *pIdx;         
66a20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
66a30 65 78 20 77 68 65 6e 20 57 48 45 52 45 5f 49 4e  ex when WHERE_IN
66a40 44 45 58 45 44 20 69 73 20 74 72 75 65 20 2a 2f  DEXED is true */
66a50 0a 20 20 20 20 73 74 72 75 63 74 20 57 68 65 72  .    struct Wher
66a60 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20  eTerm *pTerm;   
66a70 20 20 20 20 2f 2a 20 57 48 45 52 45 20 63 6c 61      /* WHERE cla
66a80 75 73 65 20 74 65 72 6d 20 66 6f 72 20 4f 52 2d  use term for OR-
66a90 73 65 61 72 63 68 20 2a 2f 0a 20 20 20 20 73 71  search */.    sq
66aa0 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f  lite3_index_info
66ab0 20 2a 70 56 74 61 62 49 64 78 3b 20 20 2f 2a 20   *pVtabIdx;  /* 
66ac0 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e  Virtual table in
66ad0 64 65 78 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20  dex to use */.  
66ae0 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46  } u;.};../*.** F
66af0 6f 72 20 65 61 63 68 20 6e 65 73 74 65 64 20 6c  or each nested l
66b00 6f 6f 70 20 69 6e 20 61 20 57 48 45 52 45 20 63  oop in a WHERE c
66b10 6c 61 75 73 65 20 69 6d 70 6c 65 6d 65 6e 74 61  lause implementa
66b20 74 69 6f 6e 2c 20 74 68 65 20 57 68 65 72 65 49  tion, the WhereI
66b30 6e 66 6f 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  nfo.** structure
66b40 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e 67   contains a sing
66b50 6c 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  le instance of t
66b60 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 20 20  his structure.  
66b70 54 68 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a  This structure.*
66b80 2a 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f  * is intended to
66b90 20 62 65 20 70 72 69 76 61 74 65 20 74 68 65 20   be private the 
66ba0 74 68 65 20 77 68 65 72 65 2e 63 20 6d 6f 64 75  the where.c modu
66bb0 6c 65 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f  le and should no
66bc0 74 20 62 65 0a 2a 2a 20 61 63 63 65 73 73 20 6f  t be.** access o
66bd0 72 20 6d 6f 64 69 66 69 65 64 20 62 79 20 6f 74  r modified by ot
66be0 68 65 72 20 6d 6f 64 75 6c 65 73 2e 0a 2a 2a 0a  her modules..**.
66bf0 2a 2a 20 54 68 65 20 70 49 64 78 49 6e 66 6f 20  ** The pIdxInfo 
66c00 66 69 65 6c 64 20 69 73 20 75 73 65 64 20 74 6f  field is used to
66c10 20 68 65 6c 70 20 70 69 63 6b 20 74 68 65 20 62   help pick the b
66c20 65 73 74 20 69 6e 64 65 78 20 6f 6e 20 61 0a 2a  est index on a.*
66c30 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  * virtual table.
66c40 20 20 54 68 65 20 70 49 64 78 49 6e 66 6f 20 70    The pIdxInfo p
66c50 6f 69 6e 74 65 72 20 63 6f 6e 74 61 69 6e 73 20  ointer contains 
66c60 69 6e 64 65 78 69 6e 67 0a 2a 2a 20 69 6e 66 6f  indexing.** info
66c70 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20  rmation for the 
66c80 69 2d 74 68 20 74 61 62 6c 65 20 69 6e 20 74 68  i-th table in th
66c90 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 62 65  e FROM clause be
66ca0 66 6f 72 65 20 72 65 6f 72 64 65 72 69 6e 67 2e  fore reordering.
66cb0 0a 2a 2a 20 41 6c 6c 20 74 68 65 20 70 49 64 78  .** All the pIdx
66cc0 49 6e 66 6f 20 70 6f 69 6e 74 65 72 73 20 61 72  Info pointers ar
66cd0 65 20 66 72 65 65 64 20 62 79 20 77 68 65 72 65  e freed by where
66ce0 49 6e 66 6f 46 72 65 65 28 29 20 69 6e 20 77 68  InfoFree() in wh
66cf0 65 72 65 2e 63 2e 0a 2a 2a 20 41 6c 6c 20 6f 74  ere.c..** All ot
66d00 68 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  her information 
66d10 69 6e 20 74 68 65 20 69 2d 74 68 20 57 68 65 72  in the i-th Wher
66d20 65 4c 65 76 65 6c 20 6f 62 6a 65 63 74 20 66 6f  eLevel object fo
66d30 72 20 74 68 65 20 69 2d 74 68 20 74 61 62 6c 65  r the i-th table
66d40 0a 2a 2a 20 61 66 74 65 72 20 46 52 4f 4d 20 63  .** after FROM c
66d50 6c 61 75 73 65 20 6f 72 64 65 72 69 6e 67 2e 0a  lause ordering..
66d60 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 65 4c  */.struct WhereL
66d70 65 76 65 6c 20 7b 0a 20 20 57 68 65 72 65 50 6c  evel {.  WherePl
66d80 61 6e 20 70 6c 61 6e 3b 20 20 20 20 20 20 20 2f  an plan;       /
66d90 2a 20 71 75 65 72 79 20 70 6c 61 6e 20 66 6f 72  * query plan for
66da0 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 6f 66   this element of
66db0 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
66dc0 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 65 66 74 4a   */.  int iLeftJ
66dd0 6f 69 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d  oin;        /* M
66de0 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20  emory cell used 
66df0 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 4c 45 46  to implement LEF
66e00 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20 2a 2f 0a  T OUTER JOIN */.
66e10 20 20 69 6e 74 20 69 54 61 62 43 75 72 3b 20 20    int iTabCur;  
66e20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56          /* The V
66e30 44 42 45 20 63 75 72 73 6f 72 20 75 73 65 64 20  DBE cursor used 
66e40 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 74 61  to access the ta
66e50 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 49 64  ble */.  int iId
66e60 78 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 2f  xCur;          /
66e70 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f  * The VDBE curso
66e80 72 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73  r used to access
66e90 20 70 49 64 78 20 2a 2f 0a 20 20 69 6e 74 20 61   pIdx */.  int a
66ea0 64 64 72 42 72 6b 3b 20 20 20 20 20 20 20 20 20  ddrBrk;         
66eb0 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f   /* Jump here to
66ec0 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 68   break out of th
66ed0 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20  e loop */.  int 
66ee0 61 64 64 72 4e 78 74 3b 20 20 20 20 20 20 20 20  addrNxt;        
66ef0 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74    /* Jump here t
66f00 6f 20 73 74 61 72 74 20 74 68 65 20 6e 65 78 74  o start the next
66f10 20 49 4e 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20   IN combination 
66f20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 43 6f 6e  */.  int addrCon
66f30 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75  t;         /* Ju
66f40 6d 70 20 68 65 72 65 20 74 6f 20 63 6f 6e 74 69  mp here to conti
66f50 6e 75 65 20 77 69 74 68 20 74 68 65 20 6e 65 78  nue with the nex
66f60 74 20 6c 6f 6f 70 20 63 79 63 6c 65 20 2a 2f 0a  t loop cycle */.
66f70 20 20 69 6e 74 20 61 64 64 72 46 69 72 73 74 3b    int addrFirst;
66f80 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
66f90 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 20   instruction of 
66fa0 69 6e 74 65 72 69 6f 72 20 6f 66 20 74 68 65 20  interior of the 
66fb0 6c 6f 6f 70 20 2a 2f 0a 20 20 75 38 20 69 46 72  loop */.  u8 iFr
66fc0 6f 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  om;             
66fd0 2f 2a 20 57 68 69 63 68 20 65 6e 74 72 79 20 69  /* Which entry i
66fe0 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  n the FROM claus
66ff0 65 20 2a 2f 0a 20 20 75 38 20 6f 70 2c 20 70 35  e */.  u8 op, p5
67000 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
67010 4f 70 63 6f 64 65 20 61 6e 64 20 50 35 20 6f 66  Opcode and P5 of
67020 20 74 68 65 20 6f 70 63 6f 64 65 20 74 68 61 74   the opcode that
67030 20 65 6e 64 73 20 74 68 65 20 6c 6f 6f 70 20 2a   ends the loop *
67040 2f 0a 20 20 69 6e 74 20 70 31 2c 20 70 32 3b 20  /.  int p1, p2; 
67050 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65            /* Ope
67060 72 61 6e 64 73 20 6f 66 20 74 68 65 20 6f 70 63  rands of the opc
67070 6f 64 65 20 75 73 65 64 20 74 6f 20 65 6e 64 73  ode used to ends
67080 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 75   the loop */.  u
67090 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20 20 20 20  nion {          
670a0 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74       /* Informat
670b0 69 6f 6e 20 74 68 61 74 20 64 65 70 65 6e 64 73  ion that depends
670c0 20 6f 6e 20 70 6c 61 6e 2e 77 73 46 6c 61 67 73   on plan.wsFlags
670d0 20 2a 2f 0a 20 20 20 20 73 74 72 75 63 74 20 7b   */.    struct {
670e0 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 6e 3b 20  .      int nIn; 
670f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
67100 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
67110 73 20 69 6e 20 61 49 6e 4c 6f 6f 70 5b 5d 20 2a  s in aInLoop[] *
67120 2f 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 49  /.      struct I
67130 6e 4c 6f 6f 70 20 7b 0a 20 20 20 20 20 20 20 20  nLoop {.        
67140 69 6e 74 20 69 43 75 72 3b 20 20 20 20 20 20 20  int iCur;       
67150 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44         /* The VD
67160 42 45 20 63 75 72 73 6f 72 20 75 73 65 64 20 62  BE cursor used b
67170 79 20 74 68 69 73 20 49 4e 20 6f 70 65 72 61 74  y this IN operat
67180 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e  or */.        in
67190 74 20 61 64 64 72 49 6e 54 6f 70 3b 20 20 20 20  t addrInTop;    
671a0 20 20 20 20 20 2f 2a 20 54 6f 70 20 6f 66 20 74       /* Top of t
671b0 68 65 20 49 4e 20 6c 6f 6f 70 20 2a 2f 0a 20 20  he IN loop */.  
671c0 20 20 20 20 7d 20 2a 61 49 6e 4c 6f 6f 70 3b 20      } *aInLoop; 
671d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66            /* Inf
671e0 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65  ormation about e
671f0 61 63 68 20 6e 65 73 74 65 64 20 49 4e 20 6f 70  ach nested IN op
67200 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 7d 20  erator */.    } 
67210 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  in;             
67220 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e      /* Used when
67230 20 70 6c 61 6e 2e 77 73 46 6c 61 67 73 26 57 48   plan.wsFlags&WH
67240 45 52 45 5f 49 4e 5f 41 42 4c 45 20 2a 2f 0a 20  ERE_IN_ABLE */. 
67250 20 7d 20 75 3b 0a 0a 20 20 2f 2a 20 54 68 65 20   } u;..  /* The 
67260 66 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64 20  following field 
67270 69 73 20 72 65 61 6c 6c 79 20 6e 6f 74 20 70 61  is really not pa
67280 72 74 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e  rt of the curren
67290 74 20 6c 65 76 65 6c 2e 20 20 42 75 74 0a 20 20  t level.  But.  
672a0 2a 2a 20 77 65 20 6e 65 65 64 20 61 20 70 6c 61  ** we need a pla
672b0 63 65 20 74 6f 20 63 61 63 68 65 20 76 69 72 74  ce to cache virt
672c0 75 61 6c 20 74 61 62 6c 65 20 69 6e 64 65 78 20  ual table index 
672d0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20  information for 
672e0 65 61 63 68 0a 20 20 2a 2a 20 76 69 72 74 75 61  each.  ** virtua
672f0 6c 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 46  l table in the F
67300 52 4f 4d 20 63 6c 61 75 73 65 20 61 6e 64 20 74  ROM clause and t
67310 68 65 20 57 68 65 72 65 4c 65 76 65 6c 20 73 74  he WhereLevel st
67320 72 75 63 74 75 72 65 20 69 73 0a 20 20 2a 2a 20  ructure is.  ** 
67330 61 20 63 6f 6e 76 65 6e 69 65 6e 74 20 70 6c 61  a convenient pla
67340 63 65 20 73 69 6e 63 65 20 74 68 65 72 65 20 69  ce since there i
67350 73 20 6f 6e 65 20 57 68 65 72 65 4c 65 76 65 6c  s one WhereLevel
67360 20 66 6f 72 20 65 61 63 68 20 46 52 4f 4d 20 63   for each FROM c
67370 6c 61 75 73 65 0a 20 20 2a 2a 20 65 6c 65 6d 65  lause.  ** eleme
67380 6e 74 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  nt..  */.  sqlit
67390 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70  e3_index_info *p
673a0 49 64 78 49 6e 66 6f 3b 20 20 2f 2a 20 49 6e 64  IdxInfo;  /* Ind
673b0 65 78 20 69 6e 66 6f 20 66 6f 72 20 6e 2d 74 68  ex info for n-th
673c0 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20 2a 2f   source table */
673d0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6c 61 67 73  .};../*.** Flags
673e0 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72   appropriate for
673f0 20 74 68 65 20 77 63 74 72 6c 46 6c 61 67 73 20   the wctrlFlags 
67400 70 61 72 61 6d 65 74 65 72 20 6f 66 20 73 71 6c  parameter of sql
67410 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29  ite3WhereBegin()
67420 0a 2a 2a 20 61 6e 64 20 74 68 65 20 57 68 65 72  .** and the Wher
67430 65 49 6e 66 6f 2e 77 63 74 72 6c 46 6c 61 67 73  eInfo.wctrlFlags
67440 20 6d 65 6d 62 65 72 2e 0a 2a 2f 0a 23 64 65 66   member..*/.#def
67450 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 52 42  ine WHERE_ORDERB
67460 59 5f 4e 4f 52 4d 41 4c 20 20 20 30 78 30 30 30  Y_NORMAL   0x000
67470 30 20 2f 2a 20 4e 6f 2d 6f 70 20 2a 2f 0a 23 64  0 /* No-op */.#d
67480 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45  efine WHERE_ORDE
67490 52 42 59 5f 4d 49 4e 20 20 20 20 20 20 30 78 30  RBY_MIN      0x0
674a0 30 30 31 20 2f 2a 20 4f 52 44 45 52 20 42 59 20  001 /* ORDER BY 
674b0 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 6d  processing for m
674c0 69 6e 28 29 20 66 75 6e 63 20 2a 2f 0a 23 64 65  in() func */.#de
674d0 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 52  fine WHERE_ORDER
674e0 42 59 5f 4d 41 58 20 20 20 20 20 20 30 78 30 30  BY_MAX      0x00
674f0 30 32 20 2f 2a 20 4f 52 44 45 52 20 42 59 20 70  02 /* ORDER BY p
67500 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 6d 61  rocessing for ma
67510 78 28 29 20 66 75 6e 63 20 2a 2f 0a 23 64 65 66  x() func */.#def
67520 69 6e 65 20 57 48 45 52 45 5f 4f 4e 45 50 41 53  ine WHERE_ONEPAS
67530 53 5f 44 45 53 49 52 45 44 20 20 30 78 30 30 30  S_DESIRED  0x000
67540 34 20 2f 2a 20 57 61 6e 74 20 74 6f 20 64 6f 20  4 /* Want to do 
67550 6f 6e 65 2d 70 61 73 73 20 55 50 44 41 54 45 2f  one-pass UPDATE/
67560 44 45 4c 45 54 45 20 2a 2f 0a 23 64 65 66 69 6e  DELETE */.#defin
67570 65 20 57 48 45 52 45 5f 44 55 50 4c 49 43 41 54  e WHERE_DUPLICAT
67580 45 53 5f 4f 4b 20 20 20 20 30 78 30 30 30 38 20  ES_OK    0x0008 
67590 2f 2a 20 4f 6b 20 74 6f 20 72 65 74 75 72 6e 20  /* Ok to return 
675a0 61 20 72 6f 77 20 6d 6f 72 65 20 74 68 61 6e 20  a row more than 
675b0 6f 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  once */.#define 
675c0 57 48 45 52 45 5f 4f 4d 49 54 5f 4f 50 45 4e 20  WHERE_OMIT_OPEN 
675d0 20 20 20 20 20 20 20 30 78 30 30 31 30 20 2f 2a         0x0010 /*
675e0 20 54 61 62 6c 65 20 63 75 72 73 6f 72 20 61 72   Table cursor ar
675f0 65 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 20 2a  e already open *
67600 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
67610 4f 4d 49 54 5f 43 4c 4f 53 45 20 20 20 20 20 20  OMIT_CLOSE      
67620 20 30 78 30 30 32 30 20 2f 2a 20 4f 6d 69 74 20   0x0020 /* Omit 
67630 63 6c 6f 73 65 20 6f 66 20 74 61 62 6c 65 20 26  close of table &
67640 20 69 6e 64 65 78 20 63 75 72 73 6f 72 73 20 2a   index cursors *
67650 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
67660 46 4f 52 43 45 5f 54 41 42 4c 45 20 20 20 20 20  FORCE_TABLE     
67670 20 30 78 30 30 34 30 20 2f 2a 20 44 6f 20 6e 6f   0x0040 /* Do no
67680 74 20 75 73 65 20 61 6e 20 69 6e 64 65 78 2d 6f  t use an index-o
67690 6e 6c 79 20 73 65 61 72 63 68 20 2a 2f 0a 0a 2f  nly search */../
676a0 2a 0a 2a 2a 20 54 68 65 20 57 48 45 52 45 20 63  *.** The WHERE c
676b0 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67  lause processing
676c0 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74 77 6f   routine has two
676d0 20 68 61 6c 76 65 73 2e 20 20 54 68 65 0a 2a 2a   halves.  The.**
676e0 20 66 69 72 73 74 20 70 61 72 74 20 64 6f 65 73   first part does
676f0 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68   the start of th
67700 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 61 6e 64  e WHERE loop and
67710 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 68   the second.** h
67720 61 6c 66 20 64 6f 65 73 20 74 68 65 20 74 61 69  alf does the tai
67730 6c 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 6c  l of the WHERE l
67740 6f 6f 70 2e 20 20 41 6e 20 69 6e 73 74 61 6e 63  oop.  An instanc
67750 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20 73 74 72  e of.** this str
67760 75 63 74 75 72 65 20 69 73 20 72 65 74 75 72 6e  ucture is return
67770 65 64 20 62 79 20 74 68 65 20 66 69 72 73 74 20  ed by the first 
67780 68 61 6c 66 20 61 6e 64 20 70 61 73 73 65 64 0a  half and passed.
67790 2a 2a 20 69 6e 74 6f 20 74 68 65 20 73 65 63 6f  ** into the seco
677a0 6e 64 20 68 61 6c 66 20 74 6f 20 67 69 76 65 20  nd half to give 
677b0 73 6f 6d 65 20 63 6f 6e 74 69 6e 75 69 74 79 2e  some continuity.
677c0 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 65  .*/.struct Where
677d0 49 6e 66 6f 20 7b 0a 20 20 50 61 72 73 65 20 2a  Info {.  Parse *
677e0 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 2f 2a  pParse;       /*
677f0 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64   Parsing and cod
67800 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e  e generating con
67810 74 65 78 74 20 2a 2f 0a 20 20 75 31 36 20 77 63  text */.  u16 wc
67820 74 72 6c 46 6c 61 67 73 3b 20 20 20 20 20 20 2f  trlFlags;      /
67830 2a 20 46 6c 61 67 73 20 6f 72 69 67 69 6e 61 6c  * Flags original
67840 6c 79 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c  ly passed to sql
67850 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29  ite3WhereBegin()
67860 20 2a 2f 0a 20 20 75 38 20 6f 6b 4f 6e 65 50 61   */.  u8 okOnePa
67870 73 73 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 6b  ss;        /* Ok
67880 20 74 6f 20 75 73 65 20 6f 6e 65 2d 70 61 73 73   to use one-pass
67890 20 61 6c 67 6f 72 69 74 68 6d 20 66 6f 72 20 55   algorithm for U
678a0 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 20  PDATE or DELETE 
678b0 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54  */.  SrcList *pT
678c0 61 62 4c 69 73 74 3b 20 20 20 20 20 20 20 20 20  abList;         
678d0 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74      /* List of t
678e0 61 62 6c 65 73 20 69 6e 20 74 68 65 20 6a 6f 69  ables in the joi
678f0 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 6f 70 3b  n */.  int iTop;
67900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
67910 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 65 72        /* The ver
67920 79 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74  y beginning of t
67930 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 2a 2f  he WHERE loop */
67940 0a 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75 65  .  int iContinue
67950 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
67960 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74    /* Jump here t
67970 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20  o continue with 
67980 6e 65 78 74 20 72 65 63 6f 72 64 20 2a 2f 0a 20  next record */. 
67990 20 69 6e 74 20 69 42 72 65 61 6b 3b 20 20 20 20   int iBreak;    
679a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
679b0 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20  /* Jump here to 
679c0 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 65  break out of the
679d0 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 6e   loop */.  int n
679e0 4c 65 76 65 6c 3b 20 20 20 20 20 20 20 20 20 20  Level;          
679f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
67a00 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20 6c 6f  ber of nested lo
67a10 6f 70 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 57  op */.  struct W
67a20 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 3b  hereClause *pWC;
67a30 20 20 20 20 20 20 20 2f 2a 20 44 65 63 6f 6d 70         /* Decomp
67a40 6f 73 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 57  osition of the W
67a50 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20  HERE clause */. 
67a60 20 57 68 65 72 65 4c 65 76 65 6c 20 61 5b 31 5d   WhereLevel a[1]
67a70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
67a80 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  /* Information a
67a90 62 6f 75 74 20 65 61 63 68 20 6e 65 73 74 20 6c  bout each nest l
67aa0 6f 6f 70 20 69 6e 20 57 48 45 52 45 20 2a 2f 0a  oop in WHERE */.
67ab0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 61 6d 65  };../*.** A Name
67ac0 43 6f 6e 74 65 78 74 20 64 65 66 69 6e 65 73 20  Context defines 
67ad0 61 20 63 6f 6e 74 65 78 74 20 69 6e 20 77 68 69  a context in whi
67ae0 63 68 20 74 6f 20 72 65 73 6f 6c 76 65 20 74 61  ch to resolve ta
67af0 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 0a 2a  ble and column.*
67b00 2a 20 6e 61 6d 65 73 2e 20 20 54 68 65 20 63 6f  * names.  The co
67b10 6e 74 65 78 74 20 63 6f 6e 73 69 73 74 73 20 6f  ntext consists o
67b20 66 20 61 20 6c 69 73 74 20 6f 66 20 74 61 62 6c  f a list of tabl
67b30 65 73 20 28 74 68 65 20 70 53 72 63 4c 69 73 74  es (the pSrcList
67b40 29 20 66 69 65 6c 64 20 61 6e 64 0a 2a 2a 20 61  ) field and.** a
67b50 20 6c 69 73 74 20 6f 66 20 6e 61 6d 65 64 20 65   list of named e
67b60 78 70 72 65 73 73 69 6f 6e 20 28 70 45 4c 69 73  xpression (pELis
67b70 74 29 2e 20 20 54 68 65 20 6e 61 6d 65 64 20 65  t).  The named e
67b80 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 6d  xpression list m
67b90 61 79 0a 2a 2a 20 62 65 20 4e 55 4c 4c 2e 20 20  ay.** be NULL.  
67ba0 54 68 65 20 70 53 72 63 20 63 6f 72 72 65 73 70  The pSrc corresp
67bb0 6f 6e 64 73 20 74 6f 20 74 68 65 20 46 52 4f 4d  onds to the FROM
67bc0 20 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c   clause of a SEL
67bd0 45 43 54 20 6f 72 0a 2a 2a 20 74 6f 20 74 68 65  ECT or.** to the
67be0 20 74 61 62 6c 65 20 62 65 69 6e 67 20 6f 70 65   table being ope
67bf0 72 61 74 65 64 20 6f 6e 20 62 79 20 49 4e 53 45  rated on by INSE
67c00 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 44  RT, UPDATE, or D
67c10 45 4c 45 54 45 2e 20 20 54 68 65 0a 2a 2a 20 70  ELETE.  The.** p
67c20 45 4c 69 73 74 20 63 6f 72 72 65 73 70 6f 6e 64  EList correspond
67c30 73 20 74 6f 20 74 68 65 20 72 65 73 75 6c 74 20  s to the result 
67c40 73 65 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20  set of a SELECT 
67c50 61 6e 64 20 69 73 20 4e 55 4c 4c 20 66 6f 72 0a  and is NULL for.
67c60 2a 2a 20 6f 74 68 65 72 20 73 74 61 74 65 6d 65  ** other stateme
67c70 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 4e 61 6d 65 43  nts..**.** NameC
67c80 6f 6e 74 65 78 74 73 20 63 61 6e 20 62 65 20 6e  ontexts can be n
67c90 65 73 74 65 64 2e 20 20 57 68 65 6e 20 72 65 73  ested.  When res
67ca0 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 2c 20 74 68  olving names, th
67cb0 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 0a 2a 2a  e inner-most .**
67cc0 20 63 6f 6e 74 65 78 74 20 69 73 20 73 65 61 72   context is sear
67cd0 63 68 65 64 20 66 69 72 73 74 2e 20 20 49 66 20  ched first.  If 
67ce0 6e 6f 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e  no match is foun
67cf0 64 2c 20 74 68 65 20 6e 65 78 74 20 6f 75 74 65  d, the next oute
67d00 72 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69 73 20  r.** context is 
67d10 63 68 65 63 6b 65 64 2e 20 20 49 66 20 74 68 65  checked.  If the
67d20 72 65 20 69 73 20 73 74 69 6c 6c 20 6e 6f 20 6d  re is still no m
67d30 61 74 63 68 2c 20 74 68 65 20 6e 65 78 74 20 63  atch, the next c
67d40 6f 6e 74 65 78 74 0a 2a 2a 20 69 73 20 63 68 65  ontext.** is che
67d50 63 6b 65 64 2e 20 20 54 68 69 73 20 70 72 6f 63  cked.  This proc
67d60 65 73 73 20 63 6f 6e 74 69 6e 75 65 73 20 75 6e  ess continues un
67d70 74 69 6c 20 65 69 74 68 65 72 20 61 20 6d 61 74  til either a mat
67d80 63 68 20 69 73 20 66 6f 75 6e 64 0a 2a 2a 20 6f  ch is found.** o
67d90 72 20 61 6c 6c 20 63 6f 6e 74 65 78 74 73 20 61  r all contexts a
67da0 72 65 20 63 68 65 63 6b 2e 20 20 57 68 65 6e 20  re check.  When 
67db0 61 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64  a match is found
67dc0 2c 20 74 68 65 20 6e 52 65 66 20 6d 65 6d 62 65  , the nRef membe
67dd0 72 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74  r of.** the cont
67de0 65 78 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ext containing t
67df0 68 65 20 6d 61 74 63 68 20 69 73 20 69 6e 63 72  he match is incr
67e00 65 6d 65 6e 74 65 64 2e 20 0a 2a 2a 0a 2a 2a 20  emented. .**.** 
67e10 45 61 63 68 20 73 75 62 71 75 65 72 79 20 67 65  Each subquery ge
67e20 74 73 20 61 20 6e 65 77 20 4e 61 6d 65 43 6f 6e  ts a new NameCon
67e30 74 65 78 74 2e 20 20 54 68 65 20 70 4e 65 78 74  text.  The pNext
67e40 20 66 69 65 6c 64 20 70 6f 69 6e 74 73 20 74 6f   field points to
67e50 20 74 68 65 0a 2a 2a 20 4e 61 6d 65 43 6f 6e 74   the.** NameCont
67e60 65 78 74 20 69 6e 20 74 68 65 20 70 61 72 65 6e  ext in the paren
67e70 74 20 71 75 65 72 79 2e 20 20 54 68 75 73 20 74  t query.  Thus t
67e80 68 65 20 70 72 6f 63 65 73 73 20 6f 66 20 73 63  he process of sc
67e90 61 6e 6e 69 6e 67 20 74 68 65 0a 2a 2a 20 4e 61  anning the.** Na
67ea0 6d 65 43 6f 6e 74 65 78 74 20 6c 69 73 74 20 63  meContext list c
67eb0 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 73 65  orresponds to se
67ec0 61 72 63 68 69 6e 67 20 74 68 72 6f 75 67 68 20  arching through 
67ed0 73 75 63 63 65 73 73 69 76 65 6c 79 20 6f 75 74  successively out
67ee0 65 72 0a 2a 2a 20 73 75 62 71 75 65 72 69 65 73  er.** subqueries
67ef0 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 6d   looking for a m
67f00 61 74 63 68 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  atch..*/.struct 
67f10 4e 61 6d 65 43 6f 6e 74 65 78 74 20 7b 0a 20 20  NameContext {.  
67f20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20  Parse *pParse;  
67f30 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
67f40 65 72 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20  er */.  SrcList 
67f50 2a 70 53 72 63 4c 69 73 74 3b 20 20 20 2f 2a 20  *pSrcList;   /* 
67f60 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 61 62 6c  One or more tabl
67f70 65 73 20 75 73 65 64 20 74 6f 20 72 65 73 6f 6c  es used to resol
67f80 76 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 45 78  ve names */.  Ex
67f90 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20  prList *pEList; 
67fa0 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 6c     /* Optional l
67fb0 69 73 74 20 6f 66 20 6e 61 6d 65 64 20 65 78 70  ist of named exp
67fc0 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e  ressions */.  in
67fd0 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20  t nRef;         
67fe0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
67ff0 6e 61 6d 65 73 20 72 65 73 6f 6c 76 65 64 20 62  names resolved b
68000 79 20 74 68 69 73 20 63 6f 6e 74 65 78 74 20 2a  y this context *
68010 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 20  /.  int nErr;   
68020 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
68030 65 72 20 6f 66 20 65 72 72 6f 72 73 20 65 6e 63  er of errors enc
68040 6f 75 6e 74 65 72 65 64 20 77 68 69 6c 65 20 72  ountered while r
68050 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20 2a  esolving names *
68060 2f 0a 20 20 75 38 20 61 6c 6c 6f 77 41 67 67 3b  /.  u8 allowAgg;
68070 20 20 20 20 20 20 20 20 20 2f 2a 20 41 67 67 72           /* Aggr
68080 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20  egate functions 
68090 61 6c 6c 6f 77 65 64 20 68 65 72 65 20 2a 2f 0a  allowed here */.
680a0 20 20 75 38 20 68 61 73 41 67 67 3b 20 20 20 20    u8 hasAgg;    
680b0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
680c0 66 20 61 67 67 72 65 67 61 74 65 73 20 61 72 65  f aggregates are
680d0 20 73 65 65 6e 20 2a 2f 0a 20 20 75 38 20 69 73   seen */.  u8 is
680e0 43 68 65 63 6b 3b 20 20 20 20 20 20 20 20 20 20  Check;          
680f0 2f 2a 20 54 72 75 65 20 69 66 20 72 65 73 6f 6c  /* True if resol
68100 76 69 6e 67 20 6e 61 6d 65 73 20 69 6e 20 61 20  ving names in a 
68110 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74  CHECK constraint
68120 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 65 70 74 68   */.  int nDepth
68130 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65  ;          /* De
68140 70 74 68 20 6f 66 20 73 75 62 71 75 65 72 79 20  pth of subquery 
68150 72 65 63 75 72 73 69 6f 6e 2e 20 31 20 66 6f 72  recursion. 1 for
68160 20 6e 6f 20 72 65 63 75 72 73 69 6f 6e 20 2a 2f   no recursion */
68170 0a 20 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 67  .  AggInfo *pAgg
68180 49 6e 66 6f 3b 20 20 20 2f 2a 20 49 6e 66 6f 72  Info;   /* Infor
68190 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 67 67  mation about agg
681a0 72 65 67 61 74 65 73 20 61 74 20 74 68 69 73 20  regates at this 
681b0 6c 65 76 65 6c 20 2a 2f 0a 20 20 4e 61 6d 65 43  level */.  NameC
681c0 6f 6e 74 65 78 74 20 2a 70 4e 65 78 74 3b 20 20  ontext *pNext;  
681d0 2f 2a 20 4e 65 78 74 20 6f 75 74 65 72 20 6e 61  /* Next outer na
681e0 6d 65 20 63 6f 6e 74 65 78 74 2e 20 20 4e 55 4c  me context.  NUL
681f0 4c 20 66 6f 72 20 6f 75 74 65 72 6d 6f 73 74 20  L for outermost 
68200 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  */.};../*.** An 
68210 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
68220 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
68230 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c  ure contains all
68240 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20   information.** 
68250 6e 65 65 64 65 64 20 74 6f 20 67 65 6e 65 72 61  needed to genera
68260 74 65 20 63 6f 64 65 20 66 6f 72 20 61 20 73 69  te code for a si
68270 6e 67 6c 65 20 53 45 4c 45 43 54 20 73 74 61 74  ngle SELECT stat
68280 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 6e 4c 69  ement..**.** nLi
68290 6d 69 74 20 69 73 20 73 65 74 20 74 6f 20 2d 31  mit is set to -1
682a0 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   if there is no 
682b0 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e 20 20 6e  LIMIT clause.  n
682c0 4f 66 66 73 65 74 20 69 73 20 73 65 74 20 74 6f  Offset is set to
682d0 20 30 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20   0..** If there 
682e0 69 73 20 61 20 4c 49 4d 49 54 20 63 6c 61 75 73  is a LIMIT claus
682f0 65 2c 20 74 68 65 20 70 61 72 73 65 72 20 73 65  e, the parser se
68300 74 73 20 6e 4c 69 6d 69 74 20 74 6f 20 74 68 65  ts nLimit to the
68310 20 76 61 6c 75 65 20 6f 66 20 74 68 65 0a 2a 2a   value of the.**
68320 20 6c 69 6d 69 74 20 61 6e 64 20 6e 4f 66 66 73   limit and nOffs
68330 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20  et to the value 
68340 6f 66 20 74 68 65 20 6f 66 66 73 65 74 20 28 6f  of the offset (o
68350 72 20 30 20 69 66 20 74 68 65 72 65 20 69 73 20  r 0 if there is 
68360 6e 6f 74 0a 2a 2a 20 6f 66 66 73 65 74 29 2e 20  not.** offset). 
68370 20 42 75 74 20 6c 61 74 65 72 20 6f 6e 2c 20 6e   But later on, n
68380 4c 69 6d 69 74 20 61 6e 64 20 6e 4f 66 66 73 65  Limit and nOffse
68390 74 20 62 65 63 6f 6d 65 20 74 68 65 20 6d 65 6d  t become the mem
683a0 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a  ory locations.**
683b0 20 69 6e 20 74 68 65 20 56 44 42 45 20 74 68 61   in the VDBE tha
683c0 74 20 72 65 63 6f 72 64 20 74 68 65 20 6c 69 6d  t record the lim
683d0 69 74 20 61 6e 64 20 6f 66 66 73 65 74 20 63 6f  it and offset co
683e0 75 6e 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 61 64  unters..**.** ad
683f0 64 72 4f 70 65 6e 45 70 68 6d 5b 5d 20 65 6e 74  drOpenEphm[] ent
68400 72 69 65 73 20 63 6f 6e 74 61 69 6e 20 74 68 65  ries contain the
68410 20 61 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 4f   address of OP_O
68420 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 6f 70 63  penEphemeral opc
68430 6f 64 65 73 2e 0a 2a 2a 20 54 68 65 73 65 20 61  odes..** These a
68440 64 64 72 65 73 73 65 73 20 6d 75 73 74 20 62 65  ddresses must be
68450 20 73 74 6f 72 65 64 20 73 6f 20 74 68 61 74 20   stored so that 
68460 77 65 20 63 61 6e 20 67 6f 20 62 61 63 6b 20 61  we can go back a
68470 6e 64 20 66 69 6c 6c 20 69 6e 0a 2a 2a 20 74 68  nd fill in.** th
68480 65 20 50 34 5f 4b 45 59 49 4e 46 4f 20 61 6e 64  e P4_KEYINFO and
68490 20 50 32 20 70 61 72 61 6d 65 74 65 72 73 20 6c   P2 parameters l
684a0 61 74 65 72 2e 20 20 4e 65 69 74 68 65 72 20 74  ater.  Neither t
684b0 68 65 20 4b 65 79 49 6e 66 6f 20 6e 6f 72 0a 2a  he KeyInfo nor.*
684c0 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * the number of 
684d0 63 6f 6c 75 6d 6e 73 20 69 6e 20 50 32 20 63 61  columns in P2 ca
684e0 6e 20 62 65 20 63 6f 6d 70 75 74 65 64 20 61 74  n be computed at
684f0 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 0a 2a   the same time.*
68500 2a 20 61 73 20 74 68 65 20 4f 50 5f 4f 70 65 6e  * as the OP_Open
68510 45 70 68 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e  Ephm instruction
68520 20 69 73 20 63 6f 64 65 64 20 62 65 63 61 75 73   is coded becaus
68530 65 20 6e 6f 74 0a 2a 2a 20 65 6e 6f 75 67 68 20  e not.** enough 
68540 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  information abou
68550 74 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 71  t the compound q
68560 75 65 72 79 20 69 73 20 6b 6e 6f 77 6e 20 61 74  uery is known at
68570 20 74 68 61 74 20 70 6f 69 6e 74 2e 0a 2a 2a 20   that point..** 
68580 54 68 65 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20  The KeyInfo for 
68590 61 64 64 72 4f 70 65 6e 54 72 61 6e 5b 30 5d 20  addrOpenTran[0] 
685a0 61 6e 64 20 5b 31 5d 20 63 6f 6e 74 61 69 6e 73  and [1] contains
685b0 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
685c0 6e 63 65 73 0a 2a 2a 20 66 6f 72 20 74 68 65 20  nces.** for the 
685d0 72 65 73 75 6c 74 20 73 65 74 2e 20 20 54 68 65  result set.  The
685e0 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20 61 64 64   KeyInfo for add
685f0 72 4f 70 65 6e 54 72 61 6e 5b 32 5d 20 63 6f 6e  rOpenTran[2] con
68600 74 61 69 6e 73 20 63 6f 6c 6c 61 74 69 6e 67 0a  tains collating.
68610 2a 2a 20 73 65 71 75 65 6e 63 65 73 20 66 6f 72  ** sequences for
68620 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
68630 61 75 73 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ause..*/.struct 
68640 53 65 6c 65 63 74 20 7b 0a 20 20 45 78 70 72 4c  Select {.  ExprL
68650 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 20  ist *pEList;    
68660 20 20 2f 2a 20 54 68 65 20 66 69 65 6c 64 73 20    /* The fields 
68670 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 2a 2f  of the result */
68680 0a 20 20 75 38 20 6f 70 3b 20 20 20 20 20 20 20  .  u8 op;       
68690 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
686a0 20 6f 66 3a 20 54 4b 5f 55 4e 49 4f 4e 20 54 4b   of: TK_UNION TK
686b0 5f 41 4c 4c 20 54 4b 5f 49 4e 54 45 52 53 45 43  _ALL TK_INTERSEC
686c0 54 20 54 4b 5f 45 58 43 45 50 54 20 2a 2f 0a 20  T TK_EXCEPT */. 
686d0 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b 20   char affinity; 
686e0 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 52          /* MakeR
686f0 65 63 6f 72 64 20 77 69 74 68 20 74 68 69 73 20  ecord with this 
68700 61 66 66 69 6e 69 74 79 20 66 6f 72 20 53 52 54  affinity for SRT
68710 5f 53 65 74 20 2a 2f 0a 20 20 75 31 36 20 73 65  _Set */.  u16 se
68720 6c 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20  lFlags;         
68730 20 2f 2a 20 56 61 72 69 6f 75 73 20 53 46 5f 2a   /* Various SF_*
68740 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 53 72 63   values */.  Src
68750 4c 69 73 74 20 2a 70 53 72 63 3b 20 20 20 20 20  List *pSrc;     
68760 20 20 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20      /* The FROM 
68770 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72  clause */.  Expr
68780 20 2a 70 57 68 65 72 65 3b 20 20 20 20 20 20 20   *pWhere;       
68790 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20     /* The WHERE 
687a0 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72  clause */.  Expr
687b0 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79 3b 20  List *pGroupBy; 
687c0 20 20 20 2f 2a 20 54 68 65 20 47 52 4f 55 50 20     /* The GROUP 
687d0 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45  BY clause */.  E
687e0 78 70 72 20 2a 70 48 61 76 69 6e 67 3b 20 20 20  xpr *pHaving;   
687f0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 48 41 56        /* The HAV
68800 49 4e 47 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ING clause */.  
68810 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72  ExprList *pOrder
68820 42 79 3b 20 20 20 20 2f 2a 20 54 68 65 20 4f 52  By;    /* The OR
68830 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f  DER BY clause */
68840 0a 20 20 53 65 6c 65 63 74 20 2a 70 50 72 69 6f  .  Select *pPrio
68850 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 72 69  r;        /* Pri
68860 6f 72 20 73 65 6c 65 63 74 20 69 6e 20 61 20 63  or select in a c
68870 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 20 73  ompound select s
68880 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 53 65  tatement */.  Se
68890 6c 65 63 74 20 2a 70 4e 65 78 74 3b 20 20 20 20  lect *pNext;    
688a0 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 73 65 6c       /* Next sel
688b0 65 63 74 20 74 6f 20 74 68 65 20 6c 65 66 74 20  ect to the left 
688c0 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 2a 2f  in a compound */
688d0 0a 20 20 53 65 6c 65 63 74 20 2a 70 52 69 67 68  .  Select *pRigh
688e0 74 6d 6f 73 74 3b 20 20 20 20 2f 2a 20 52 69 67  tmost;    /* Rig
688f0 68 74 2d 6d 6f 73 74 20 73 65 6c 65 63 74 20 69  ht-most select i
68900 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c  n a compound sel
68910 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  ect statement */
68920 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 3b  .  Expr *pLimit;
68930 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 49 4d            /* LIM
68940 49 54 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 4e  IT expression. N
68950 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f 74 20 75 73  ULL means not us
68960 65 64 2e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ed. */.  Expr *p
68970 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20  Offset;         
68980 2f 2a 20 4f 46 46 53 45 54 20 65 78 70 72 65 73  /* OFFSET expres
68990 73 69 6f 6e 2e 20 4e 55 4c 4c 20 6d 65 61 6e 73  sion. NULL means
689a0 20 6e 6f 74 20 75 73 65 64 2e 20 2a 2f 0a 20 20   not used. */.  
689b0 69 6e 74 20 69 4c 69 6d 69 74 2c 20 69 4f 66 66  int iLimit, iOff
689c0 73 65 74 3b 20 20 20 2f 2a 20 4d 65 6d 6f 72 79  set;   /* Memory
689d0 20 72 65 67 69 73 74 65 72 73 20 68 6f 6c 64 69   registers holdi
689e0 6e 67 20 4c 49 4d 49 54 20 26 20 4f 46 46 53 45  ng LIMIT & OFFSE
689f0 54 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20  T counters */.  
68a00 69 6e 74 20 61 64 64 72 4f 70 65 6e 45 70 68 6d  int addrOpenEphm
68a10 5b 33 5d 3b 20 20 20 2f 2a 20 4f 50 5f 4f 70 65  [3];   /* OP_Ope
68a20 6e 45 70 68 65 6d 20 6f 70 63 6f 64 65 73 20 72  nEphem opcodes r
68a30 65 6c 61 74 65 64 20 74 6f 20 74 68 69 73 20 73  elated to this s
68a40 65 6c 65 63 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  elect */.};../*.
68a50 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65  ** Allowed value
68a60 73 20 66 6f 72 20 53 65 6c 65 63 74 2e 73 65 6c  s for Select.sel
68a70 46 6c 61 67 73 2e 20 20 54 68 65 20 22 53 46 22  Flags.  The "SF"
68a80 20 70 72 65 66 69 78 20 73 74 61 6e 64 73 20 66   prefix stands f
68a90 6f 72 0a 2a 2a 20 22 53 65 6c 65 63 74 20 46 6c  or.** "Select Fl
68aa0 61 67 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ag"..*/.#define 
68ab0 53 46 5f 44 69 73 74 69 6e 63 74 20 20 20 20 20  SF_Distinct     
68ac0 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 4f 75     0x0001  /* Ou
68ad0 74 70 75 74 20 73 68 6f 75 6c 64 20 62 65 20 44  tput should be D
68ae0 49 53 54 49 4e 43 54 20 2a 2f 0a 23 64 65 66 69  ISTINCT */.#defi
68af0 6e 65 20 53 46 5f 52 65 73 6f 6c 76 65 64 20 20  ne SF_Resolved  
68b00 20 20 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a        0x0002  /*
68b10 20 49 64 65 6e 74 69 66 69 65 72 73 20 68 61 76   Identifiers hav
68b20 65 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 20  e been resolved 
68b30 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f 41 67  */.#define SF_Ag
68b40 67 72 65 67 61 74 65 20 20 20 20 20 20 20 30 78  gregate       0x
68b50 30 30 30 34 20 20 2f 2a 20 43 6f 6e 74 61 69 6e  0004  /* Contain
68b60 73 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  s aggregate func
68b70 74 69 6f 6e 73 20 2a 2f 0a 23 64 65 66 69 6e 65  tions */.#define
68b80 20 53 46 5f 55 73 65 73 45 70 68 65 6d 65 72 61   SF_UsesEphemera
68b90 6c 20 20 20 30 78 30 30 30 38 20 20 2f 2a 20 55  l   0x0008  /* U
68ba0 73 65 73 20 74 68 65 20 4f 70 65 6e 45 70 68 65  ses the OpenEphe
68bb0 6d 65 72 61 6c 20 6f 70 63 6f 64 65 20 2a 2f 0a  meral opcode */.
68bc0 23 64 65 66 69 6e 65 20 53 46 5f 45 78 70 61 6e  #define SF_Expan
68bd0 64 65 64 20 20 20 20 20 20 20 20 30 78 30 30 31  ded        0x001
68be0 30 20 20 2f 2a 20 73 71 6c 69 74 65 33 53 65 6c  0  /* sqlite3Sel
68bf0 65 63 74 45 78 70 61 6e 64 28 29 20 63 61 6c 6c  ectExpand() call
68c00 65 64 20 6f 6e 20 74 68 69 73 20 2a 2f 0a 23 64  ed on this */.#d
68c10 65 66 69 6e 65 20 53 46 5f 48 61 73 54 79 70 65  efine SF_HasType
68c20 49 6e 66 6f 20 20 20 20 20 30 78 30 30 32 30 20  Info     0x0020 
68c30 20 2f 2a 20 46 52 4f 4d 20 73 75 62 71 75 65 72   /* FROM subquer
68c40 69 65 73 20 68 61 76 65 20 54 61 62 6c 65 20 6d  ies have Table m
68c50 65 74 61 64 61 74 61 20 2a 2f 0a 0a 0a 2f 2a 0a  etadata */.../*.
68c60 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 73 20 6f  ** The results o
68c70 66 20 61 20 73 65 6c 65 63 74 20 63 61 6e 20 62  f a select can b
68c80 65 20 64 69 73 74 72 69 62 75 74 65 64 20 69 6e  e distributed in
68c90 20 73 65 76 65 72 61 6c 20 77 61 79 73 2e 20 20   several ways.  
68ca0 54 68 65 0a 2a 2a 20 22 53 52 54 22 20 70 72 65  The.** "SRT" pre
68cb0 66 69 78 20 6d 65 61 6e 73 20 22 53 45 4c 45 43  fix means "SELEC
68cc0 54 20 52 65 73 75 6c 74 20 54 79 70 65 22 2e 0a  T Result Type"..
68cd0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 55  */.#define SRT_U
68ce0 6e 69 6f 6e 20 20 20 20 20 20 20 20 31 20 20 2f  nion        1  /
68cf0 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 61  * Store result a
68d00 73 20 6b 65 79 73 20 69 6e 20 61 6e 20 69 6e 64  s keys in an ind
68d10 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52  ex */.#define SR
68d20 54 5f 45 78 63 65 70 74 20 20 20 20 20 20 20 32  T_Except       2
68d30 20 20 2f 2a 20 52 65 6d 6f 76 65 20 72 65 73 75    /* Remove resu
68d40 6c 74 20 66 72 6f 6d 20 61 20 55 4e 49 4f 4e 20  lt from a UNION 
68d50 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65  index */.#define
68d60 20 53 52 54 5f 45 78 69 73 74 73 20 20 20 20 20   SRT_Exists     
68d70 20 20 33 20 20 2f 2a 20 53 74 6f 72 65 20 31 20    3  /* Store 1 
68d80 69 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  if the result is
68d90 20 6e 6f 74 20 65 6d 70 74 79 20 2a 2f 0a 23 64   not empty */.#d
68da0 65 66 69 6e 65 20 53 52 54 5f 44 69 73 63 61 72  efine SRT_Discar
68db0 64 20 20 20 20 20 20 34 20 20 2f 2a 20 44 6f 20  d      4  /* Do 
68dc0 6e 6f 74 20 73 61 76 65 20 74 68 65 20 72 65 73  not save the res
68dd0 75 6c 74 73 20 61 6e 79 77 68 65 72 65 20 2a 2f  ults anywhere */
68de0 0a 0a 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42  ../* The ORDER B
68df0 59 20 63 6c 61 75 73 65 20 69 73 20 69 67 6e 6f  Y clause is igno
68e00 72 65 64 20 66 6f 72 20 61 6c 6c 20 6f 66 20 74  red for all of t
68e10 68 65 20 61 62 6f 76 65 20 2a 2f 0a 23 64 65 66  he above */.#def
68e20 69 6e 65 20 49 67 6e 6f 72 61 62 6c 65 4f 72 64  ine IgnorableOrd
68e30 65 72 62 79 28 58 29 20 28 28 58 2d 3e 65 44 65  erby(X) ((X->eDe
68e40 73 74 29 3c 3d 53 52 54 5f 44 69 73 63 61 72 64  st)<=SRT_Discard
68e50 29 0a 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 4f  )..#define SRT_O
68e60 75 74 70 75 74 20 20 20 20 20 20 20 35 20 20 2f  utput       5  /
68e70 2a 20 4f 75 74 70 75 74 20 65 61 63 68 20 72 6f  * Output each ro
68e80 77 20 6f 66 20 72 65 73 75 6c 74 20 2a 2f 0a 23  w of result */.#
68e90 64 65 66 69 6e 65 20 53 52 54 5f 4d 65 6d 20 20  define SRT_Mem  
68ea0 20 20 20 20 20 20 20 20 36 20 20 2f 2a 20 53 74          6  /* St
68eb0 6f 72 65 20 72 65 73 75 6c 74 20 69 6e 20 61 20  ore result in a 
68ec0 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 2a 2f 0a 23  memory cell */.#
68ed0 64 65 66 69 6e 65 20 53 52 54 5f 53 65 74 20 20  define SRT_Set  
68ee0 20 20 20 20 20 20 20 20 37 20 20 2f 2a 20 53 74          7  /* St
68ef0 6f 72 65 20 72 65 73 75 6c 74 73 20 61 73 20 6b  ore results as k
68f00 65 79 73 20 69 6e 20 61 6e 20 69 6e 64 65 78 20  eys in an index 
68f10 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 54  */.#define SRT_T
68f20 61 62 6c 65 20 20 20 20 20 20 20 20 38 20 20 2f  able        8  /
68f30 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 61  * Store result a
68f40 73 20 64 61 74 61 20 77 69 74 68 20 61 6e 20 61  s data with an a
68f50 75 74 6f 6d 61 74 69 63 20 72 6f 77 69 64 20 2a  utomatic rowid *
68f60 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 45 70  /.#define SRT_Ep
68f70 68 65 6d 54 61 62 20 20 20 20 20 39 20 20 2f 2a  hemTab     9  /*
68f80 20 43 72 65 61 74 65 20 74 72 61 6e 73 69 65 6e   Create transien
68f90 74 20 74 61 62 20 61 6e 64 20 73 74 6f 72 65 20  t tab and store 
68fa0 6c 69 6b 65 20 53 52 54 5f 54 61 62 6c 65 20 2a  like SRT_Table *
68fb0 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 43 6f  /.#define SRT_Co
68fc0 72 6f 75 74 69 6e 65 20 20 20 31 30 20 20 2f 2a  routine   10  /*
68fd0 20 47 65 6e 65 72 61 74 65 20 61 20 73 69 6e 67   Generate a sing
68fe0 6c 65 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74  le row of result
68ff0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 74 72   */../*.** A str
69000 75 63 74 75 72 65 20 75 73 65 64 20 74 6f 20 63  ucture used to c
69010 75 73 74 6f 6d 69 7a 65 20 74 68 65 20 62 65 68  ustomize the beh
69020 61 76 69 6f 72 20 6f 66 20 73 71 6c 69 74 65 33  avior of sqlite3
69030 53 65 6c 65 63 74 28 29 2e 20 53 65 65 0a 2a 2a  Select(). See.**
69040 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20   comments above 
69050 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 29 20  sqlite3Select() 
69060 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a  for details..*/.
69070 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53  typedef struct S
69080 65 6c 65 63 74 44 65 73 74 20 53 65 6c 65 63 74  electDest Select
69090 44 65 73 74 3b 0a 73 74 72 75 63 74 20 53 65 6c  Dest;.struct Sel
690a0 65 63 74 44 65 73 74 20 7b 0a 20 20 75 38 20 65  ectDest {.  u8 e
690b0 44 65 73 74 3b 20 20 20 20 20 20 20 20 20 2f 2a  Dest;         /*
690c0 20 48 6f 77 20 74 6f 20 64 69 73 70 6f 73 65 20   How to dispose 
690d0 6f 66 20 74 68 65 20 72 65 73 75 6c 74 73 20 2a  of the results *
690e0 2f 0a 20 20 75 38 20 61 66 66 69 6e 69 74 79 3b  /.  u8 affinity;
690f0 20 20 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 74        /* Affinit
69100 79 20 75 73 65 64 20 77 68 65 6e 20 65 44 65 73  y used when eDes
69110 74 3d 3d 53 52 54 5f 53 65 74 20 2a 2f 0a 20 20  t==SRT_Set */.  
69120 69 6e 74 20 69 50 61 72 6d 3b 20 20 20 20 20 20  int iParm;      
69130 20 20 2f 2a 20 41 20 70 61 72 61 6d 65 74 65 72    /* A parameter
69140 20 75 73 65 64 20 62 79 20 74 68 65 20 65 44 65   used by the eDe
69150 73 74 20 64 69 73 70 6f 73 61 6c 20 6d 65 74 68  st disposal meth
69160 6f 64 20 2a 2f 0a 20 20 69 6e 74 20 69 4d 65 6d  od */.  int iMem
69170 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73  ;         /* Bas
69180 65 20 72 65 67 69 73 74 65 72 20 77 68 65 72 65  e register where
69190 20 72 65 73 75 6c 74 73 20 61 72 65 20 77 72 69   results are wri
691a0 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d  tten */.  int nM
691b0 65 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  em;         /* N
691c0 75 6d 62 65 72 20 6f 66 20 72 65 67 69 73 74 65  umber of registe
691d0 72 73 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a  rs allocated */.
691e0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 44 75 72 69 6e 67  };../*.** During
691f0 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6f 6e   code generation
69200 20 6f 66 20 73 74 61 74 65 6d 65 6e 74 73 20 74   of statements t
69210 68 61 74 20 64 6f 20 69 6e 73 65 72 74 73 20 69  hat do inserts i
69220 6e 74 6f 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e  nto AUTOINCREMEN
69230 54 20 0a 2a 2a 20 74 61 62 6c 65 73 2c 20 74 68  T .** tables, th
69240 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 66 6f  e following info
69250 72 6d 61 74 69 6f 6e 20 69 73 20 61 74 74 61 63  rmation is attac
69260 68 65 64 20 74 6f 20 74 68 65 20 54 61 62 6c 65  hed to the Table
69270 2e 75 2e 61 75 74 6f 49 6e 63 2e 70 0a 2a 2a 20  .u.autoInc.p.** 
69280 70 6f 69 6e 74 65 72 20 6f 66 20 65 61 63 68 20  pointer of each 
69290 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 74 61  autoincrement ta
692a0 62 6c 65 20 74 6f 20 72 65 63 6f 72 64 20 73 6f  ble to record so
692b0 6d 65 20 73 69 64 65 20 69 6e 66 6f 72 6d 61 74  me side informat
692c0 69 6f 6e 20 74 68 61 74 0a 2a 2a 20 74 68 65 20  ion that.** the 
692d0 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20 6e  code generator n
692e0 65 65 64 73 2e 20 20 57 65 20 68 61 76 65 20 74  eeds.  We have t
692f0 6f 20 6b 65 65 70 20 70 65 72 2d 74 61 62 6c 65  o keep per-table
69300 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 0a 2a   autoincrement.*
69310 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e  * information in
69320 20 63 61 73 65 20 69 6e 73 65 72 74 73 20 61 72   case inserts ar
69330 65 20 64 6f 77 6e 20 77 69 74 68 69 6e 20 74 72  e down within tr
69340 69 67 67 65 72 73 2e 20 20 54 72 69 67 67 65 72  iggers.  Trigger
69350 73 20 64 6f 20 6e 6f 74 0a 2a 2a 20 6e 6f 72 6d  s do not.** norm
69360 61 6c 6c 79 20 63 6f 6f 72 64 69 6e 61 74 65 20  ally coordinate 
69370 74 68 65 69 72 20 61 63 74 69 76 69 74 69 65 73  their activities
69380 2c 20 62 75 74 20 77 65 20 64 6f 20 6e 65 65 64  , but we do need
69390 20 74 6f 20 63 6f 6f 72 64 69 6e 61 74 65 20 74   to coordinate t
693a0 68 65 0a 2a 2a 20 6c 6f 61 64 69 6e 67 20 61 6e  he.** loading an
693b0 64 20 73 61 76 69 6e 67 20 6f 66 20 61 75 74 6f  d saving of auto
693c0 69 6e 63 72 65 6d 65 6e 74 20 69 6e 66 6f 72 6d  increment inform
693d0 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74  ation..*/.struct
693e0 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 7b 0a 20   AutoincInfo {. 
693f0 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 2a 70 4e   AutoincInfo *pN
69400 65 78 74 3b 20 20 20 2f 2a 20 4e 65 78 74 20 69  ext;   /* Next i
69410 6e 66 6f 20 62 6c 6f 63 6b 20 69 6e 20 61 20 6c  nfo block in a l
69420 69 73 74 20 6f 66 20 74 68 65 6d 20 61 6c 6c 20  ist of them all 
69430 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  */.  Table *pTab
69440 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61  ;          /* Ta
69450 62 6c 65 20 74 68 69 73 20 69 6e 66 6f 20 62 6c  ble this info bl
69460 6f 63 6b 20 72 65 66 65 72 73 20 74 6f 20 2a 2f  ock refers to */
69470 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20  .  int iDb;     
69480 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
69490 78 20 69 6e 20 73 71 6c 69 74 65 33 2e 61 44 62  x in sqlite3.aDb
694a0 5b 5d 20 6f 66 20 64 61 74 61 62 61 73 65 20 68  [] of database h
694b0 6f 6c 64 69 6e 67 20 70 54 61 62 20 2a 2f 0a 20  olding pTab */. 
694c0 20 69 6e 74 20 72 65 67 43 74 72 3b 20 20 20 20   int regCtr;    
694d0 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79         /* Memory
694e0 20 72 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e   register holdin
694f0 67 20 74 68 65 20 72 6f 77 69 64 20 63 6f 75 6e  g the rowid coun
69500 74 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ter */.};../*.**
69510 20 53 69 7a 65 20 6f 66 20 74 68 65 20 63 6f 6c   Size of the col
69520 75 6d 6e 20 63 61 63 68 65 0a 2a 2f 0a 23 69 66  umn cache.*/.#if
69530 6e 64 65 66 20 53 51 4c 49 54 45 5f 4e 5f 43 4f  ndef SQLITE_N_CO
69540 4c 43 41 43 48 45 0a 23 20 64 65 66 69 6e 65 20  LCACHE.# define 
69550 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48  SQLITE_N_COLCACH
69560 45 20 31 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  E 10.#endif../*.
69570 2a 2a 20 41 74 20 6c 65 61 73 74 20 6f 6e 65 20  ** At least one 
69580 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
69590 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
695a0 75 72 65 20 69 73 20 63 72 65 61 74 65 64 20 66  ure is created f
695b0 6f 72 20 65 61 63 68 20 0a 2a 2a 20 74 72 69 67  or each .** trig
695c0 67 65 72 20 74 68 61 74 20 6d 61 79 20 62 65 20  ger that may be 
695d0 66 69 72 65 64 20 77 68 69 6c 65 20 70 61 72 73  fired while pars
695e0 69 6e 67 20 61 6e 20 49 4e 53 45 52 54 2c 20 55  ing an INSERT, U
695f0 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 0a  PDATE or DELETE.
69600 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 41 6c  ** statement. Al
69610 6c 20 73 75 63 68 20 6f 62 6a 65 63 74 73 20 61  l such objects a
69620 72 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  re stored in the
69630 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 68 65 61   linked list hea
69640 64 65 64 20 61 74 0a 2a 2a 20 50 61 72 73 65 2e  ded at.** Parse.
69650 70 54 72 69 67 67 65 72 50 72 67 20 61 6e 64 20  pTriggerPrg and 
69660 64 65 6c 65 74 65 64 20 6f 6e 63 65 20 73 74 61  deleted once sta
69670 74 65 6d 65 6e 74 20 63 6f 6d 70 69 6c 61 74 69  tement compilati
69680 6f 6e 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 63  on has been.** c
69690 6f 6d 70 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  ompleted..**.** 
696a0 41 20 56 64 62 65 20 73 75 62 2d 70 72 6f 67 72  A Vdbe sub-progr
696b0 61 6d 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e  am that implemen
696c0 74 73 20 74 68 65 20 62 6f 64 79 20 61 6e 64 20  ts the body and 
696d0 57 48 45 4e 20 63 6c 61 75 73 65 20 6f 66 20 74  WHEN clause of t
696e0 72 69 67 67 65 72 0a 2a 2a 20 54 72 69 67 67 65  rigger.** Trigge
696f0 72 50 72 67 2e 70 54 72 69 67 67 65 72 2c 20 61  rPrg.pTrigger, a
69700 73 73 75 6d 69 6e 67 20 61 20 64 65 66 61 75 6c  ssuming a defaul
69710 74 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c  t ON CONFLICT cl
69720 61 75 73 65 20 6f 66 0a 2a 2a 20 54 72 69 67 67  ause of.** Trigg
69730 65 72 50 72 67 2e 6f 72 63 6f 6e 66 2c 20 69 73  erPrg.orconf, is
69740 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 54   stored in the T
69750 72 69 67 67 65 72 50 72 67 2e 70 50 72 6f 67 72  riggerPrg.pProgr
69760 61 6d 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2a 20  am variable..** 
69770 54 68 65 20 50 61 72 73 65 2e 70 54 72 69 67 67  The Parse.pTrigg
69780 65 72 50 72 67 20 6c 69 73 74 20 6e 65 76 65 72  erPrg list never
69790 20 63 6f 6e 74 61 69 6e 73 20 74 77 6f 20 65 6e   contains two en
697a0 74 72 69 65 73 20 77 69 74 68 20 74 68 65 20 73  tries with the s
697b0 61 6d 65 0a 2a 2a 20 76 61 6c 75 65 73 20 66 6f  ame.** values fo
697c0 72 20 62 6f 74 68 20 70 54 72 69 67 67 65 72 20  r both pTrigger 
697d0 61 6e 64 20 6f 72 63 6f 6e 66 2e 0a 2a 2a 0a 2a  and orconf..**.*
697e0 2a 20 54 68 65 20 54 72 69 67 67 65 72 50 72 67  * The TriggerPrg
697f0 2e 6f 6c 64 6d 61 73 6b 20 76 61 72 69 61 62 6c  .oldmask variabl
69800 65 20 69 73 20 73 65 74 20 74 6f 20 61 20 6d 61  e is set to a ma
69810 73 6b 20 6f 66 20 6f 6c 64 2e 2a 20 63 6f 6c 75  sk of old.* colu
69820 6d 6e 73 0a 2a 2a 20 61 63 63 65 73 73 65 64 20  mns.** accessed 
69830 28 6f 72 20 73 65 74 20 74 6f 20 30 20 66 6f 72  (or set to 0 for
69840 20 74 72 69 67 67 65 72 73 20 66 69 72 65 64 20   triggers fired 
69850 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 49  as a result of I
69860 4e 53 45 52 54 20 0a 2a 2a 20 73 74 61 74 65 6d  NSERT .** statem
69870 65 6e 74 73 29 2e 0a 2a 2f 0a 73 74 72 75 63 74  ents)..*/.struct
69880 20 54 72 69 67 67 65 72 50 72 67 20 7b 0a 20 20   TriggerPrg {.  
69890 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65  Trigger *pTrigge
698a0 72 3b 20 20 20 20 20 20 2f 2a 20 54 72 69 67 67  r;      /* Trigg
698b0 65 72 20 74 68 69 73 20 70 72 6f 67 72 61 6d 20  er this program 
698c0 77 61 73 20 63 6f 64 65 64 20 66 72 6f 6d 20 2a  was coded from *
698d0 2f 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 3b 20  /.  int orconf; 
698e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
698f0 65 66 61 75 6c 74 20 4f 4e 20 43 4f 4e 46 4c 49  efault ON CONFLI
69900 43 54 20 70 6f 6c 69 63 79 20 2a 2f 0a 20 20 53  CT policy */.  S
69910 75 62 50 72 6f 67 72 61 6d 20 2a 70 50 72 6f 67  ubProgram *pProg
69920 72 61 6d 3b 20 20 20 2f 2a 20 50 72 6f 67 72 61  ram;   /* Progra
69930 6d 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 70  m implementing p
69940 54 72 69 67 67 65 72 2f 6f 72 63 6f 6e 66 20 2a  Trigger/orconf *
69950 2f 0a 20 20 75 33 32 20 6f 6c 64 6d 61 73 6b 3b  /.  u32 oldmask;
69960 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
69970 61 73 6b 20 6f 66 20 6f 6c 64 2e 2a 20 63 6f 6c  ask of old.* col
69980 75 6d 6e 73 20 61 63 63 65 73 73 65 64 20 2a 2f  umns accessed */
69990 0a 20 20 54 72 69 67 67 65 72 50 72 67 20 2a 70  .  TriggerPrg *p
699a0 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e 65  Next;      /* Ne
699b0 78 74 20 65 6e 74 72 79 20 69 6e 20 50 61 72 73  xt entry in Pars
699c0 65 2e 70 54 72 69 67 67 65 72 50 72 67 20 6c 69  e.pTriggerPrg li
699d0 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  st */.};../*.** 
699e0 41 6e 20 53 51 4c 20 70 61 72 73 65 72 20 63 6f  An SQL parser co
699f0 6e 74 65 78 74 2e 20 20 41 20 63 6f 70 79 20 6f  ntext.  A copy o
69a00 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
69a10 20 69 73 20 70 61 73 73 65 64 20 74 68 72 6f 75   is passed throu
69a20 67 68 0a 2a 2a 20 74 68 65 20 70 61 72 73 65 72  gh.** the parser
69a30 20 61 6e 64 20 64 6f 77 6e 20 69 6e 74 6f 20 61   and down into a
69a40 6c 6c 20 74 68 65 20 70 61 72 73 65 72 20 61 63  ll the parser ac
69a50 74 69 6f 6e 20 72 6f 75 74 69 6e 65 20 69 6e 20  tion routine in 
69a60 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 61 72 72  order to.** carr
69a70 79 20 61 72 6f 75 6e 64 20 69 6e 66 6f 72 6d 61  y around informa
69a80 74 69 6f 6e 20 74 68 61 74 20 69 73 20 67 6c 6f  tion that is glo
69a90 62 61 6c 20 74 6f 20 74 68 65 20 65 6e 74 69 72  bal to the entir
69aa0 65 20 70 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54  e parse..**.** T
69ab0 68 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20  he structure is 
69ac0 64 69 76 69 64 65 64 20 69 6e 74 6f 20 74 77 6f  divided into two
69ad0 20 70 61 72 74 73 2e 20 20 57 68 65 6e 20 74 68   parts.  When th
69ae0 65 20 70 61 72 73 65 72 20 61 6e 64 20 63 6f 64  e parser and cod
69af0 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65 20 63 61  e.** generate ca
69b00 6c 6c 20 74 68 65 6d 73 65 6c 76 65 73 20 72 65  ll themselves re
69b10 63 75 72 73 69 76 65 6c 79 2c 20 74 68 65 20 66  cursively, the f
69b20 69 72 73 74 20 70 61 72 74 20 6f 66 20 74 68 65  irst part of the
69b30 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 73   structure.** is
69b40 20 63 6f 6e 73 74 61 6e 74 20 62 75 74 20 74 68   constant but th
69b50 65 20 73 65 63 6f 6e 64 20 70 61 72 74 20 69 73  e second part is
69b60 20 72 65 73 65 74 20 61 74 20 74 68 65 20 62 65   reset at the be
69b70 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65 6e 64 20  ginning and end 
69b80 6f 66 0a 2a 2a 20 65 61 63 68 20 72 65 63 75 72  of.** each recur
69b90 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  sion..**.** The 
69ba0 6e 54 61 62 6c 65 4c 6f 63 6b 20 61 6e 64 20 61  nTableLock and a
69bb0 54 61 62 6c 65 4c 6f 63 6b 20 76 61 72 69 61 62  TableLock variab
69bc0 6c 65 73 20 61 72 65 20 6f 6e 6c 79 20 75 73 65  les are only use
69bd0 64 20 69 66 20 74 68 65 20 73 68 61 72 65 64 2d  d if the shared-
69be0 63 61 63 68 65 20 0a 2a 2a 20 66 65 61 74 75 72  cache .** featur
69bf0 65 20 69 73 20 65 6e 61 62 6c 65 64 20 28 69 66  e is enabled (if
69c00 20 73 71 6c 69 74 65 33 54 73 64 28 29 2d 3e 75   sqlite3Tsd()->u
69c10 73 65 53 68 61 72 65 64 44 61 74 61 20 69 73 20  seSharedData is 
69c20 74 72 75 65 29 2e 20 54 68 65 79 20 61 72 65 0a  true). They are.
69c30 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65  ** used to store
69c40 20 74 68 65 20 73 65 74 20 6f 66 20 74 61 62 6c   the set of tabl
69c50 65 2d 6c 6f 63 6b 73 20 72 65 71 75 69 72 65 64  e-locks required
69c60 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e   by the statemen
69c70 74 20 62 65 69 6e 67 0a 2a 2a 20 63 6f 6d 70 69  t being.** compi
69c80 6c 65 64 2e 20 46 75 6e 63 74 69 6f 6e 20 73 71  led. Function sq
69c90 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 29  lite3TableLock()
69ca0 20 69 73 20 75 73 65 64 20 74 6f 20 61 64 64 20   is used to add 
69cb0 65 6e 74 72 69 65 73 20 74 6f 20 74 68 65 0a 2a  entries to the.*
69cc0 2a 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72 75 63  * list..*/.struc
69cd0 74 20 50 61 72 73 65 20 7b 0a 20 20 73 71 6c 69  t Parse {.  sqli
69ce0 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20  te3 *db;        
69cf0 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 61 74   /* The main dat
69d00 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65 20  abase structure 
69d10 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  */.  int rc;    
69d20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
69d30 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 65 78  urn code from ex
69d40 65 63 75 74 69 6f 6e 20 2a 2f 0a 20 20 63 68 61  ecution */.  cha
69d50 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20  r *zErrMsg;     
69d60 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20 6d 65    /* An error me
69d70 73 73 61 67 65 20 2a 2f 0a 20 20 56 64 62 65 20  ssage */.  Vdbe 
69d80 2a 70 56 64 62 65 3b 20 20 20 20 20 20 20 20 20  *pVdbe;         
69d90 2f 2a 20 41 6e 20 65 6e 67 69 6e 65 20 66 6f 72  /* An engine for
69da0 20 65 78 65 63 75 74 69 6e 67 20 64 61 74 61 62   executing datab
69db0 61 73 65 20 62 79 74 65 63 6f 64 65 20 2a 2f 0a  ase bytecode */.
69dc0 20 20 75 38 20 63 6f 6c 4e 61 6d 65 73 53 65 74    u8 colNamesSet
69dd0 3b 20 20 20 20 20 20 2f 2a 20 54 52 55 45 20 61  ;      /* TRUE a
69de0 66 74 65 72 20 4f 50 5f 43 6f 6c 75 6d 6e 4e 61  fter OP_ColumnNa
69df0 6d 65 20 68 61 73 20 62 65 65 6e 20 69 73 73 75  me has been issu
69e00 65 64 20 74 6f 20 70 56 64 62 65 20 2a 2f 0a 20  ed to pVdbe */. 
69e10 20 75 38 20 6e 61 6d 65 43 6c 61 73 68 3b 20 20   u8 nameClash;  
69e20 20 20 20 20 20 20 2f 2a 20 41 20 70 65 72 6d 61        /* A perma
69e30 6e 65 6e 74 20 74 61 62 6c 65 20 6e 61 6d 65 20  nent table name 
69e40 63 6c 61 73 68 65 73 20 77 69 74 68 20 74 65 6d  clashes with tem
69e50 70 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a  p table name */.
69e60 20 20 75 38 20 63 68 65 63 6b 53 63 68 65 6d 61    u8 checkSchema
69e70 3b 20 20 20 20 20 20 2f 2a 20 43 61 75 73 65 73  ;      /* Causes
69e80 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 63   schema cookie c
69e90 68 65 63 6b 20 61 66 74 65 72 20 61 6e 20 65 72  heck after an er
69ea0 72 6f 72 20 2a 2f 0a 20 20 75 38 20 6e 65 73 74  ror */.  u8 nest
69eb0 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ed;           /*
69ec0 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65   Number of neste
69ed0 64 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 20 70  d calls to the p
69ee0 61 72 73 65 72 2f 63 6f 64 65 20 67 65 6e 65 72  arser/code gener
69ef0 61 74 6f 72 20 2a 2f 0a 20 20 75 38 20 70 61 72  ator */.  u8 par
69f00 73 65 45 72 72 6f 72 3b 20 20 20 20 20 20 20 2f  seError;       /
69f10 2a 20 54 72 75 65 20 61 66 74 65 72 20 61 20 70  * True after a p
69f20 61 72 73 69 6e 67 20 65 72 72 6f 72 2e 20 20 54  arsing error.  T
69f30 69 63 6b 65 74 20 23 31 37 39 34 20 2a 2f 0a 20  icket #1794 */. 
69f40 20 75 38 20 6e 54 65 6d 70 52 65 67 3b 20 20 20   u8 nTempReg;   
69f50 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
69f60 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67  of temporary reg
69f70 69 73 74 65 72 73 20 69 6e 20 61 54 65 6d 70 52  isters in aTempR
69f80 65 67 5b 5d 20 2a 2f 0a 20 20 75 38 20 6e 54 65  eg[] */.  u8 nTe
69f90 6d 70 49 6e 55 73 65 3b 20 20 20 20 20 20 20 2f  mpInUse;       /
69fa0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 54 65 6d  * Number of aTem
69fb0 70 52 65 67 5b 5d 20 63 75 72 72 65 6e 74 6c 79  pReg[] currently
69fc0 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a   checked out */.
69fd0 20 20 69 6e 74 20 61 54 65 6d 70 52 65 67 5b 38    int aTempReg[8
69fe0 5d 3b 20 20 20 20 20 2f 2a 20 48 6f 6c 64 69 6e  ];     /* Holdin
69ff0 67 20 61 72 65 61 20 66 6f 72 20 74 65 6d 70 6f  g area for tempo
6a000 72 61 72 79 20 72 65 67 69 73 74 65 72 73 20 2a  rary registers *
6a010 2f 0a 20 20 69 6e 74 20 6e 52 61 6e 67 65 52 65  /.  int nRangeRe
6a020 67 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65  g;       /* Size
6a030 20 6f 66 20 74 68 65 20 74 65 6d 70 6f 72 61 72   of the temporar
6a040 79 20 72 65 67 69 73 74 65 72 20 62 6c 6f 63 6b  y register block
6a050 20 2a 2f 0a 20 20 69 6e 74 20 69 52 61 6e 67 65   */.  int iRange
6a060 52 65 67 3b 20 20 20 20 20 20 20 2f 2a 20 46 69  Reg;       /* Fi
6a070 72 73 74 20 72 65 67 69 73 74 65 72 20 69 6e 20  rst register in 
6a080 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74  temporary regist
6a090 65 72 20 62 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e  er block */.  in
6a0a0 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 20 20  t nErr;         
6a0b0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
6a0c0 65 72 72 6f 72 73 20 73 65 65 6e 20 2a 2f 0a 20  errors seen */. 
6a0d0 20 69 6e 74 20 6e 54 61 62 3b 20 20 20 20 20 20   int nTab;      
6a0e0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
6a0f0 6f 66 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c  of previously al
6a100 6c 6f 63 61 74 65 64 20 56 44 42 45 20 63 75 72  located VDBE cur
6a110 73 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d  sors */.  int nM
6a120 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  em;            /
6a130 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f  * Number of memo
6a140 72 79 20 63 65 6c 6c 73 20 75 73 65 64 20 73 6f  ry cells used so
6a150 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 53   far */.  int nS
6a160 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  et;            /
6a170 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 65 74 73  * Number of sets
6a180 20 75 73 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a   used so far */.
6a190 20 20 69 6e 74 20 63 6b 42 61 73 65 3b 20 20 20    int ckBase;   
6a1a0 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20 72         /* Base r
6a1b0 65 67 69 73 74 65 72 20 6f 66 20 64 61 74 61 20  egister of data 
6a1c0 64 75 72 69 6e 67 20 63 68 65 63 6b 20 63 6f 6e  during check con
6a1d0 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 69 6e  straints */.  in
6a1e0 74 20 69 43 61 63 68 65 4c 65 76 65 6c 3b 20 20  t iCacheLevel;  
6a1f0 20 20 20 2f 2a 20 43 6f 6c 43 61 63 68 65 20 76     /* ColCache v
6a200 61 6c 69 64 20 77 68 65 6e 20 61 43 6f 6c 43 61  alid when aColCa
6a210 63 68 65 5b 5d 2e 69 4c 65 76 65 6c 3c 3d 69 43  che[].iLevel<=iC
6a220 61 63 68 65 4c 65 76 65 6c 20 2a 2f 0a 20 20 69  acheLevel */.  i
6a230 6e 74 20 69 43 61 63 68 65 43 6e 74 3b 20 20 20  nt iCacheCnt;   
6a240 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 20 75      /* Counter u
6a250 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20  sed to generate 
6a260 61 43 6f 6c 43 61 63 68 65 5b 5d 2e 6c 72 75 20  aColCache[].lru 
6a270 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 38 20 6e  values */.  u8 n
6a280 43 6f 6c 43 61 63 68 65 3b 20 20 20 20 20 20 20  ColCache;       
6a290 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
6a2a0 74 72 69 65 73 20 69 6e 20 74 68 65 20 63 6f 6c  tries in the col
6a2b0 75 6d 6e 20 63 61 63 68 65 20 2a 2f 0a 20 20 75  umn cache */.  u
6a2c0 38 20 69 43 6f 6c 43 61 63 68 65 3b 20 20 20 20  8 iColCache;    
6a2d0 20 20 20 20 2f 2a 20 4e 65 78 74 20 65 6e 74 72      /* Next entr
6a2e0 79 20 6f 66 20 74 68 65 20 63 61 63 68 65 20 74  y of the cache t
6a2f0 6f 20 72 65 70 6c 61 63 65 20 2a 2f 0a 20 20 73  o replace */.  s
6a300 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20  truct yColCache 
6a310 7b 0a 20 20 20 20 69 6e 74 20 69 54 61 62 6c 65  {.    int iTable
6a320 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
6a330 61 62 6c 65 20 63 75 72 73 6f 72 20 6e 75 6d 62  able cursor numb
6a340 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43  er */.    int iC
6a350 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20  olumn;          
6a360 2f 2a 20 54 61 62 6c 65 20 63 6f 6c 75 6d 6e 20  /* Table column 
6a370 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 75 38  number */.    u8
6a380 20 61 66 66 43 68 61 6e 67 65 3b 20 20 20 20 20   affChange;     
6a390 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
6a3a0 68 69 73 20 72 65 67 69 73 74 65 72 20 68 61 73  his register has
6a3b0 20 68 61 64 20 61 6e 20 61 66 66 69 6e 69 74 79   had an affinity
6a3c0 20 63 68 61 6e 67 65 20 2a 2f 0a 20 20 20 20 75   change */.    u
6a3d0 38 20 74 65 6d 70 52 65 67 3b 20 20 20 20 20 20  8 tempReg;      
6a3e0 20 20 20 20 20 2f 2a 20 69 52 65 67 20 69 73 20       /* iReg is 
6a3f0 61 20 74 65 6d 70 20 72 65 67 69 73 74 65 72 20  a temp register 
6a400 74 68 61 74 20 6e 65 65 64 73 20 74 6f 20 62 65  that needs to be
6a410 20 66 72 65 65 64 20 2a 2f 0a 20 20 20 20 69 6e   freed */.    in
6a420 74 20 69 4c 65 76 65 6c 3b 20 20 20 20 20 20 20  t iLevel;       
6a430 20 20 20 20 2f 2a 20 4e 65 73 74 69 6e 67 20 6c      /* Nesting l
6a440 65 76 65 6c 20 2a 2f 0a 20 20 20 20 69 6e 74 20  evel */.    int 
6a450 69 52 65 67 3b 20 20 20 20 20 20 20 20 20 20 20  iReg;           
6a460 20 20 2f 2a 20 52 65 67 20 77 69 74 68 20 76 61    /* Reg with va
6a470 6c 75 65 20 6f 66 20 74 68 69 73 20 63 6f 6c 75  lue of this colu
6a480 6d 6e 2e 20 30 20 6d 65 61 6e 73 20 6e 6f 6e 65  mn. 0 means none
6a490 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 72 75  . */.    int lru
6a4a0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
6a4b0 2a 20 4c 65 61 73 74 20 72 65 63 65 6e 74 6c 79  * Least recently
6a4c0 20 75 73 65 64 20 65 6e 74 72 79 20 68 61 73 20   used entry has 
6a4d0 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 76 61 6c  the smallest val
6a4e0 75 65 20 2a 2f 0a 20 20 7d 20 61 43 6f 6c 43 61  ue */.  } aColCa
6a4f0 63 68 65 5b 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c  che[SQLITE_N_COL
6a500 43 41 43 48 45 5d 3b 20 20 2f 2a 20 4f 6e 65 20  CACHE];  /* One 
6a510 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20  for each column 
6a520 63 61 63 68 65 20 65 6e 74 72 79 20 2a 2f 0a 20  cache entry */. 
6a530 20 75 33 32 20 77 72 69 74 65 4d 61 73 6b 3b 20   u32 writeMask; 
6a540 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74 20 61        /* Start a
6a550 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
6a560 6f 6e 20 6f 6e 20 74 68 65 73 65 20 64 61 74 61  on on these data
6a570 62 61 73 65 73 20 2a 2f 0a 20 20 75 33 32 20 63  bases */.  u32 c
6a580 6f 6f 6b 69 65 4d 61 73 6b 3b 20 20 20 20 20 20  ookieMask;      
6a590 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 73 63  /* Bitmask of sc
6a5a0 68 65 6d 61 20 76 65 72 69 66 69 65 64 20 64 61  hema verified da
6a5b0 74 61 62 61 73 65 73 20 2a 2f 0a 20 20 75 38 20  tabases */.  u8 
6a5c0 69 73 4d 75 6c 74 69 57 72 69 74 65 3b 20 20 20  isMultiWrite;   
6a5d0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 73 74 61    /* True if sta
6a5e0 74 65 6d 65 6e 74 20 6d 61 79 20 61 66 66 65 63  tement may affec
6a5f0 74 2f 69 6e 73 65 72 74 20 6d 75 6c 74 69 70 6c  t/insert multipl
6a600 65 20 72 6f 77 73 20 2a 2f 0a 20 20 75 38 20 6d  e rows */.  u8 m
6a610 61 79 41 62 6f 72 74 3b 20 20 20 20 20 20 20 20  ayAbort;        
6a620 20 2f 2a 20 54 72 75 65 20 69 66 20 73 74 61 74   /* True if stat
6a630 65 6d 65 6e 74 20 6d 61 79 20 74 68 72 6f 77 20  ement may throw 
6a640 61 6e 20 41 42 4f 52 54 20 65 78 63 65 70 74 69  an ABORT excepti
6a650 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f 6b  on */.  int cook
6a660 69 65 47 6f 74 6f 3b 20 20 20 20 20 20 2f 2a 20  ieGoto;      /* 
6a670 41 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 47 6f  Address of OP_Go
6a680 74 6f 20 74 6f 20 63 6f 6f 6b 69 65 20 76 65 72  to to cookie ver
6a690 69 66 69 65 72 20 73 75 62 72 6f 75 74 69 6e 65  ifier subroutine
6a6a0 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f 6b 69 65   */.  int cookie
6a6b0 56 61 6c 75 65 5b 53 51 4c 49 54 45 5f 4d 41 58  Value[SQLITE_MAX
6a6c0 5f 41 54 54 41 43 48 45 44 2b 32 5d 3b 20 20 2f  _ATTACHED+2];  /
6a6d0 2a 20 56 61 6c 75 65 73 20 6f 66 20 63 6f 6f 6b  * Values of cook
6a6e0 69 65 73 20 74 6f 20 76 65 72 69 66 79 20 2a 2f  ies to verify */
6a6f0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
6a700 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
6a710 45 0a 20 20 69 6e 74 20 6e 54 61 62 6c 65 4c 6f  E.  int nTableLo
6a720 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  ck;        /* Nu
6a730 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 69 6e  mber of locks in
6a740 20 61 54 61 62 6c 65 4c 6f 63 6b 20 2a 2f 0a 20   aTableLock */. 
6a750 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 61 54 61 62   TableLock *aTab
6a760 6c 65 4c 6f 63 6b 3b 20 2f 2a 20 52 65 71 75 69  leLock; /* Requi
6a770 72 65 64 20 74 61 62 6c 65 20 6c 6f 63 6b 73 20  red table locks 
6a780 66 6f 72 20 73 68 61 72 65 64 2d 63 61 63 68 65  for shared-cache
6a790 20 6d 6f 64 65 20 2a 2f 0a 23 65 6e 64 69 66 0a   mode */.#endif.
6a7a0 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64 3b 20    int regRowid; 
6a7b0 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
6a7c0 65 72 20 68 6f 6c 64 69 6e 67 20 72 6f 77 69 64  er holding rowid
6a7d0 20 6f 66 20 43 52 45 41 54 45 20 54 41 42 4c 45   of CREATE TABLE
6a7e0 20 65 6e 74 72 79 20 2a 2f 0a 20 20 69 6e 74 20   entry */.  int 
6a7f0 72 65 67 52 6f 6f 74 3b 20 20 20 20 20 20 20 20  regRoot;        
6a800 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c   /* Register hol
6a810 64 69 6e 67 20 72 6f 6f 74 20 70 61 67 65 20 6e  ding root page n
6a820 75 6d 62 65 72 20 66 6f 72 20 6e 65 77 20 6f 62  umber for new ob
6a830 6a 65 63 74 73 20 2a 2f 0a 20 20 41 75 74 6f 69  jects */.  Autoi
6a840 6e 63 49 6e 66 6f 20 2a 70 41 69 6e 63 3b 20 20  ncInfo *pAinc;  
6a850 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  /* Information a
6a860 62 6f 75 74 20 41 55 54 4f 49 4e 43 52 45 4d 45  bout AUTOINCREME
6a870 4e 54 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20  NT counters */. 
6a880 20 69 6e 74 20 6e 4d 61 78 41 72 67 3b 20 20 20   int nMaxArg;   
6a890 20 20 20 20 20 20 2f 2a 20 4d 61 78 20 61 72 67        /* Max arg
6a8a0 73 20 70 61 73 73 65 64 20 74 6f 20 75 73 65 72  s passed to user
6a8b0 20 66 75 6e 63 74 69 6f 6e 20 62 79 20 73 75 62   function by sub
6a8c0 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 0a 20 20 2f  -program */..  /
6a8d0 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73  * Information us
6a8e0 65 64 20 77 68 69 6c 65 20 63 6f 64 69 6e 67 20  ed while coding 
6a8f0 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 73  trigger programs
6a900 2e 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 54  . */.  Parse *pT
6a910 6f 70 6c 65 76 65 6c 3b 20 20 20 20 2f 2a 20 50  oplevel;    /* P
6a920 61 72 73 65 20 73 74 72 75 63 74 75 72 65 20 66  arse structure f
6a930 6f 72 20 6d 61 69 6e 20 70 72 6f 67 72 61 6d 20  or main program 
6a940 28 6f 72 20 4e 55 4c 4c 29 20 2a 2f 0a 20 20 54  (or NULL) */.  T
6a950 61 62 6c 65 20 2a 70 54 72 69 67 67 65 72 54 61  able *pTriggerTa
6a960 62 3b 20 20 2f 2a 20 54 61 62 6c 65 20 74 72 69  b;  /* Table tri
6a970 67 67 65 72 73 20 61 72 65 20 62 65 69 6e 67 20  ggers are being 
6a980 63 6f 64 65 64 20 66 6f 72 20 2a 2f 0a 20 20 75  coded for */.  u
6a990 33 32 20 6f 6c 64 6d 61 73 6b 3b 20 20 20 20 20  32 oldmask;     
6a9a0 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 6f      /* Mask of o
6a9b0 6c 64 2e 2a 20 63 6f 6c 75 6d 6e 73 20 72 65 66  ld.* columns ref
6a9c0 65 72 65 6e 63 65 64 20 2a 2f 0a 20 20 75 38 20  erenced */.  u8 
6a9d0 65 54 72 69 67 67 65 72 4f 70 3b 20 20 20 20 20  eTriggerOp;     
6a9e0 20 20 2f 2a 20 54 4b 5f 55 50 44 41 54 45 2c 20    /* TK_UPDATE, 
6a9f0 54 4b 5f 49 4e 53 45 52 54 20 6f 72 20 54 4b 5f  TK_INSERT or TK_
6aa00 44 45 4c 45 54 45 20 2a 2f 0a 20 20 75 38 20 65  DELETE */.  u8 e
6aa10 4f 72 63 6f 6e 66 3b 20 20 20 20 20 20 20 20 20  Orconf;         
6aa20 20 2f 2a 20 44 65 66 61 75 6c 74 20 4f 4e 20 43   /* Default ON C
6aa30 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 66  ONFLICT policy f
6aa40 6f 72 20 74 72 69 67 67 65 72 20 73 74 65 70 73  or trigger steps
6aa50 20 2a 2f 0a 0a 20 20 2f 2a 20 41 62 6f 76 65 20   */..  /* Above 
6aa60 69 73 20 63 6f 6e 73 74 61 6e 74 20 62 65 74 77  is constant betw
6aa70 65 65 6e 20 72 65 63 75 72 73 69 6f 6e 73 2e 20  een recursions. 
6aa80 20 42 65 6c 6f 77 20 69 73 20 72 65 73 65 74 20   Below is reset 
6aa90 62 65 66 6f 72 65 20 61 6e 64 20 61 66 74 65 72  before and after
6aaa0 0a 20 20 2a 2a 20 65 61 63 68 20 72 65 63 75 72  .  ** each recur
6aab0 73 69 6f 6e 20 2a 2f 0a 0a 20 20 69 6e 74 20 6e  sion */..  int n
6aac0 56 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  Var;            
6aad0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 27 3f 27  /* Number of '?'
6aae0 20 76 61 72 69 61 62 6c 65 73 20 73 65 65 6e 20   variables seen 
6aaf0 69 6e 20 74 68 65 20 53 51 4c 20 73 6f 20 66 61  in the SQL so fa
6ab00 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 45  r */.  int nVarE
6ab10 78 70 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e  xpr;        /* N
6ab20 75 6d 62 65 72 20 6f 66 20 75 73 65 64 20 73 6c  umber of used sl
6ab30 6f 74 73 20 69 6e 20 61 70 56 61 72 45 78 70 72  ots in apVarExpr
6ab40 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72  [] */.  int nVar
6ab50 45 78 70 72 41 6c 6c 6f 63 3b 20 20 20 2f 2a 20  ExprAlloc;   /* 
6ab60 4e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61  Number of alloca
6ab70 74 65 64 20 73 6c 6f 74 73 20 69 6e 20 61 70 56  ted slots in apV
6ab80 61 72 45 78 70 72 5b 5d 20 2a 2f 0a 20 20 45 78  arExpr[] */.  Ex
6ab90 70 72 20 2a 2a 61 70 56 61 72 45 78 70 72 3b 20  pr **apVarExpr; 
6aba0 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74     /* Pointers t
6abb0 6f 20 3a 61 61 61 20 61 6e 64 20 24 61 61 61 61  o :aaa and $aaaa
6abc0 20 77 69 6c 64 63 61 72 64 20 65 78 70 72 65 73   wildcard expres
6abd0 73 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  sions */.  int n
6abe0 41 6c 69 61 73 3b 20 20 20 20 20 20 20 20 20 20  Alias;          
6abf0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 6c 69  /* Number of ali
6ac00 61 73 65 64 20 72 65 73 75 6c 74 20 73 65 74 20  ased result set 
6ac10 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 69 6e 74  columns */.  int
6ac20 20 6e 41 6c 69 61 73 41 6c 6c 6f 63 3b 20 20 20   nAliasAlloc;   
6ac30 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61    /* Number of a
6ac40 6c 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73 20 66  llocated slots f
6ac50 6f 72 20 61 41 6c 69 61 73 5b 5d 20 2a 2f 0a 20  or aAlias[] */. 
6ac60 20 69 6e 74 20 2a 61 41 6c 69 61 73 3b 20 20 20   int *aAlias;   
6ac70 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
6ac80 72 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 61  r used to hold a
6ac90 6c 69 61 73 65 64 20 72 65 73 75 6c 74 20 2a 2f  liased result */
6aca0 0a 20 20 75 38 20 65 78 70 6c 61 69 6e 3b 20 20  .  u8 explain;  
6acb0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
6acc0 69 66 20 74 68 65 20 45 58 50 4c 41 49 4e 20 66  if the EXPLAIN f
6acd0 6c 61 67 20 69 73 20 66 6f 75 6e 64 20 6f 6e 20  lag is found on 
6ace0 74 68 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 54  the query */.  T
6acf0 6f 6b 65 6e 20 73 4e 61 6d 65 54 6f 6b 65 6e 3b  oken sNameToken;
6ad00 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 77 69 74      /* Token wit
6ad10 68 20 75 6e 71 75 61 6c 69 66 69 65 64 20 73 63  h unqualified sc
6ad20 68 65 6d 61 20 6f 62 6a 65 63 74 20 6e 61 6d 65  hema object name
6ad30 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 73 4c 61 73   */.  Token sLas
6ad40 74 54 6f 6b 65 6e 3b 20 20 20 20 2f 2a 20 54 68  tToken;    /* Th
6ad50 65 20 6c 61 73 74 20 74 6f 6b 65 6e 20 70 61 72  e last token par
6ad60 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  sed */.  const c
6ad70 68 61 72 20 2a 7a 54 61 69 6c 3b 20 20 20 2f 2a  har *zTail;   /*
6ad80 20 41 6c 6c 20 53 51 4c 20 74 65 78 74 20 70 61   All SQL text pa
6ad90 73 74 20 74 68 65 20 6c 61 73 74 20 73 65 6d 69  st the last semi
6ada0 63 6f 6c 6f 6e 20 70 61 72 73 65 64 20 2a 2f 0a  colon parsed */.
6adb0 20 20 54 61 62 6c 65 20 2a 70 4e 65 77 54 61 62    Table *pNewTab
6adc0 6c 65 3b 20 20 20 20 2f 2a 20 41 20 74 61 62 6c  le;    /* A tabl
6add0 65 20 62 65 69 6e 67 20 63 6f 6e 73 74 72 75 63  e being construc
6ade0 74 65 64 20 62 79 20 43 52 45 41 54 45 20 54 41  ted by CREATE TA
6adf0 42 4c 45 20 2a 2f 0a 20 20 54 72 69 67 67 65 72  BLE */.  Trigger
6ae00 20 2a 70 4e 65 77 54 72 69 67 67 65 72 3b 20 20   *pNewTrigger;  
6ae10 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 75 6e     /* Trigger un
6ae20 64 65 72 20 63 6f 6e 73 74 72 75 63 74 20 62 79  der construct by
6ae30 20 61 20 43 52 45 41 54 45 20 54 52 49 47 47 45   a CREATE TRIGGE
6ae40 52 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  R */.  const cha
6ae50 72 20 2a 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b  r *zAuthContext;
6ae60 20 2f 2a 20 54 68 65 20 36 74 68 20 70 61 72 61   /* The 6th para
6ae70 6d 65 74 65 72 20 74 6f 20 64 62 2d 3e 78 41 75  meter to db->xAu
6ae80 74 68 20 63 61 6c 6c 62 61 63 6b 73 20 2a 2f 0a  th callbacks */.
6ae90 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
6aea0 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
6aeb0 0a 20 20 54 6f 6b 65 6e 20 73 41 72 67 3b 20 20  .  Token sArg;  
6aec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
6aed0 20 43 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f   Complete text o
6aee0 66 20 61 20 6d 6f 64 75 6c 65 20 61 72 67 75 6d  f a module argum
6aef0 65 6e 74 20 2a 2f 0a 20 20 75 38 20 64 65 63 6c  ent */.  u8 decl
6af00 61 72 65 56 74 61 62 3b 20 20 20 20 20 20 20 20  areVtab;        
6af10 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69      /* True if i
6af20 6e 73 69 64 65 20 73 71 6c 69 74 65 33 5f 64 65  nside sqlite3_de
6af30 63 6c 61 72 65 5f 76 74 61 62 28 29 20 2a 2f 0a  clare_vtab() */.
6af40 20 20 69 6e 74 20 6e 56 74 61 62 4c 6f 63 6b 3b    int nVtabLock;
6af50 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
6af60 4e 75 6d 62 65 72 20 6f 66 20 76 69 72 74 75 61  Number of virtua
6af70 6c 20 74 61 62 6c 65 73 20 74 6f 20 6c 6f 63 6b  l tables to lock
6af80 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 2a 61 70   */.  Table **ap
6af90 56 74 61 62 4c 6f 63 6b 3b 20 20 20 20 20 20 20  VtabLock;       
6afa0 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76   /* Pointer to v
6afb0 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6e 65  irtual tables ne
6afc0 65 64 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 2a 2f  eding locking */
6afd0 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 6e 48  .#endif.  int nH
6afe0 65 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20  eight;          
6aff0 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20    /* Expression 
6b000 74 72 65 65 20 68 65 69 67 68 74 20 6f 66 20 63  tree height of c
6b010 75 72 72 65 6e 74 20 73 75 62 2d 73 65 6c 65 63  urrent sub-selec
6b020 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 5a  t */.  Table *pZ
6b030 6f 6d 62 69 65 54 61 62 3b 20 20 20 20 20 20 2f  ombieTab;      /
6b040 2a 20 4c 69 73 74 20 6f 66 20 54 61 62 6c 65 20  * List of Table 
6b050 6f 62 6a 65 63 74 73 20 74 6f 20 64 65 6c 65 74  objects to delet
6b060 65 20 61 66 74 65 72 20 63 6f 64 65 20 67 65 6e  e after code gen
6b070 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 50 72 67   */.  TriggerPrg
6b080 20 2a 70 54 72 69 67 67 65 72 50 72 67 3b 20 20   *pTriggerPrg;  
6b090 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74    /* Linked list
6b0a0 20 6f 66 20 63 6f 64 65 64 20 74 72 69 67 67 65   of coded trigge
6b0b0 72 73 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 64 65 66  rs */.};..#ifdef
6b0c0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
6b0d0 54 55 41 4c 54 41 42 4c 45 0a 20 20 23 64 65 66  TUALTABLE.  #def
6b0e0 69 6e 65 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56  ine IN_DECLARE_V
6b0f0 54 41 42 20 30 0a 23 65 6c 73 65 0a 20 20 23 64  TAB 0.#else.  #d
6b100 65 66 69 6e 65 20 49 4e 5f 44 45 43 4c 41 52 45  efine IN_DECLARE
6b110 5f 56 54 41 42 20 28 70 50 61 72 73 65 2d 3e 64  _VTAB (pParse->d
6b120 65 63 6c 61 72 65 56 74 61 62 29 0a 23 65 6e 64  eclareVtab).#end
6b130 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  if../*.** An ins
6b140 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
6b150 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
6b160 20 63 61 6e 20 62 65 20 64 65 63 6c 61 72 65 64   can be declared
6b170 20 6f 6e 20 61 20 73 74 61 63 6b 20 61 6e 64 20   on a stack and 
6b180 75 73 65 64 0a 2a 2a 20 74 6f 20 73 61 76 65 20  used.** to save 
6b190 74 68 65 20 50 61 72 73 65 2e 7a 41 75 74 68 43  the Parse.zAuthC
6b1a0 6f 6e 74 65 78 74 20 76 61 6c 75 65 20 73 6f 20  ontext value so 
6b1b0 74 68 61 74 20 69 74 20 63 61 6e 20 62 65 20 72  that it can be r
6b1c0 65 73 74 6f 72 65 64 20 6c 61 74 65 72 2e 0a 2a  estored later..*
6b1d0 2f 0a 73 74 72 75 63 74 20 41 75 74 68 43 6f 6e  /.struct AuthCon
6b1e0 74 65 78 74 20 7b 0a 20 20 63 6f 6e 73 74 20 63  text {.  const c
6b1f0 68 61 72 20 2a 7a 41 75 74 68 43 6f 6e 74 65 78  har *zAuthContex
6b200 74 3b 20 20 20 2f 2a 20 50 75 74 20 73 61 76 65  t;   /* Put save
6b210 64 20 50 61 72 73 65 2e 7a 41 75 74 68 43 6f 6e  d Parse.zAuthCon
6b220 74 65 78 74 20 68 65 72 65 20 2a 2f 0a 20 20 50  text here */.  P
6b230 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20  arse *pParse;   
6b240 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
6b250 65 20 50 61 72 73 65 20 73 74 72 75 63 74 75 72  e Parse structur
6b260 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 42  e */.};../*.** B
6b270 69 74 66 69 65 6c 64 20 66 6c 61 67 73 20 66 6f  itfield flags fo
6b280 72 20 50 35 20 76 61 6c 75 65 20 69 6e 20 4f 50  r P5 value in OP
6b290 5f 49 6e 73 65 72 74 20 61 6e 64 20 4f 50 5f 44  _Insert and OP_D
6b2a0 65 6c 65 74 65 0a 2a 2f 0a 23 64 65 66 69 6e 65  elete.*/.#define
6b2b0 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20   OPFLAG_NCHANGE 
6b2c0 20 20 20 20 20 20 30 78 30 31 20 20 20 20 2f 2a        0x01    /*
6b2d0 20 53 65 74 20 74 6f 20 75 70 64 61 74 65 20 64   Set to update d
6b2e0 62 2d 3e 6e 43 68 61 6e 67 65 20 2a 2f 0a 23 64  b->nChange */.#d
6b2f0 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f 4c 41 53  efine OPFLAG_LAS
6b300 54 52 4f 57 49 44 20 20 20 20 20 30 78 30 32 20  TROWID     0x02 
6b310 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 75 70 64     /* Set to upd
6b320 61 74 65 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69  ate db->lastRowi
6b330 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46  d */.#define OPF
6b340 4c 41 47 5f 49 53 55 50 44 41 54 45 20 20 20 20  LAG_ISUPDATE    
6b350 20 20 30 78 30 34 20 20 20 20 2f 2a 20 54 68 69    0x04    /* Thi
6b360 73 20 4f 50 5f 49 6e 73 65 72 74 20 69 73 20 61  s OP_Insert is a
6b370 6e 20 73 71 6c 20 55 50 44 41 54 45 20 2a 2f 0a  n sql UPDATE */.
6b380 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f 41  #define OPFLAG_A
6b390 50 50 45 4e 44 20 20 20 20 20 20 20 20 30 78 30  PPEND        0x0
6b3a0 38 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20  8    /* This is 
6b3b0 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 61 6e 20  likely to be an 
6b3c0 61 70 70 65 6e 64 20 2a 2f 0a 23 64 65 66 69 6e  append */.#defin
6b3d0 65 20 4f 50 46 4c 41 47 5f 55 53 45 53 45 45 4b  e OPFLAG_USESEEK
6b3e0 52 45 53 55 4c 54 20 30 78 31 30 20 20 20 20 2f  RESULT 0x10    /
6b3f0 2a 20 54 72 79 20 74 6f 20 61 76 6f 69 64 20 61  * Try to avoid a
6b400 20 73 65 65 6b 20 69 6e 20 42 74 72 65 65 49 6e   seek in BtreeIn
6b410 73 65 72 74 28 29 20 2a 2f 0a 23 64 65 66 69 6e  sert() */.#defin
6b420 65 20 4f 50 46 4c 41 47 5f 43 4c 45 41 52 43 41  e OPFLAG_CLEARCA
6b430 43 48 45 20 20 20 20 30 78 32 30 20 20 20 20 2f  CHE    0x20    /
6b440 2a 20 43 6c 65 61 72 20 70 73 65 75 64 6f 2d 74  * Clear pseudo-t
6b450 61 62 6c 65 20 63 61 63 68 65 20 69 6e 20 4f 50  able cache in OP
6b460 5f 43 6f 6c 75 6d 6e 20 2a 2f 0a 0a 2f 2a 0a 20  _Column */../*. 
6b470 2a 20 45 61 63 68 20 74 72 69 67 67 65 72 20 70  * Each trigger p
6b480 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20 64 61  resent in the da
6b490 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 69 73  tabase schema is
6b4a0 20 73 74 6f 72 65 64 20 61 73 20 61 6e 20 69 6e   stored as an in
6b4b0 73 74 61 6e 63 65 20 6f 66 0a 20 2a 20 73 74 72  stance of. * str
6b4c0 75 63 74 20 54 72 69 67 67 65 72 2e 20 0a 20 2a  uct Trigger. . *
6b4d0 0a 20 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20  . * Pointers to 
6b4e0 69 6e 73 74 61 6e 63 65 73 20 6f 66 20 73 74 72  instances of str
6b4f0 75 63 74 20 54 72 69 67 67 65 72 20 61 72 65 20  uct Trigger are 
6b500 73 74 6f 72 65 64 20 69 6e 20 74 77 6f 20 77 61  stored in two wa
6b510 79 73 2e 0a 20 2a 20 31 2e 20 49 6e 20 74 68 65  ys.. * 1. In the
6b520 20 22 74 72 69 67 48 61 73 68 22 20 68 61 73 68   "trigHash" hash
6b530 20 74 61 62 6c 65 20 28 70 61 72 74 20 6f 66 20   table (part of 
6b540 74 68 65 20 73 71 6c 69 74 65 33 2a 20 74 68 61  the sqlite3* tha
6b550 74 20 72 65 70 72 65 73 65 6e 74 73 20 74 68 65  t represents the
6b560 20 0a 20 2a 20 20 20 20 64 61 74 61 62 61 73 65   . *    database
6b570 29 2e 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 54  ). This allows T
6b580 72 69 67 67 65 72 20 73 74 72 75 63 74 75 72 65  rigger structure
6b590 73 20 74 6f 20 62 65 20 72 65 74 72 69 65 76 65  s to be retrieve
6b5a0 64 20 62 79 20 6e 61 6d 65 2e 0a 20 2a 20 32 2e  d by name.. * 2.
6b5b0 20 41 6c 6c 20 74 72 69 67 67 65 72 73 20 61 73   All triggers as
6b5c0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20  sociated with a 
6b5d0 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 66 6f 72  single table for
6b5e0 6d 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2c  m a linked list,
6b5f0 20 75 73 69 6e 67 20 74 68 65 0a 20 2a 20 20 20   using the. *   
6b600 20 70 4e 65 78 74 20 6d 65 6d 62 65 72 20 6f 66   pNext member of
6b610 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 2e   struct Trigger.
6b620 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   A pointer to th
6b630 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20  e first element 
6b640 6f 66 20 74 68 65 0a 20 2a 20 20 20 20 6c 69 6e  of the. *    lin
6b650 6b 65 64 20 6c 69 73 74 20 69 73 20 73 74 6f 72  ked list is stor
6b660 65 64 20 61 73 20 74 68 65 20 22 70 54 72 69 67  ed as the "pTrig
6b670 67 65 72 22 20 6d 65 6d 62 65 72 20 6f 66 20 74  ger" member of t
6b680 68 65 20 61 73 73 6f 63 69 61 74 65 64 0a 20 2a  he associated. *
6b690 20 20 20 20 73 74 72 75 63 74 20 54 61 62 6c 65      struct Table
6b6a0 2e 0a 20 2a 0a 20 2a 20 54 68 65 20 22 73 74 65  .. *. * The "ste
6b6b0 70 5f 6c 69 73 74 22 20 6d 65 6d 62 65 72 20 70  p_list" member p
6b6c0 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72  oints to the fir
6b6d0 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20  st element of a 
6b6e0 6c 69 6e 6b 65 64 20 6c 69 73 74 0a 20 2a 20 63  linked list. * c
6b6f0 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 53 51  ontaining the SQ
6b700 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 73 70 65  L statements spe
6b710 63 69 66 69 65 64 20 61 73 20 74 68 65 20 74 72  cified as the tr
6b720 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e 0a 20  igger program.. 
6b730 2a 2f 0a 73 74 72 75 63 74 20 54 72 69 67 67 65  */.struct Trigge
6b740 72 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d  r {.  char *zNam
6b750 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e;            /*
6b760 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   The name of the
6b770 20 74 72 69 67 67 65 72 20 20 20 20 20 20 20 20   trigger        
6b780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6b790 2a 2f 0a 20 20 63 68 61 72 20 2a 74 61 62 6c 65  */.  char *table
6b7a0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
6b7b0 54 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69 65  The table or vie
6b7c0 77 20 74 6f 20 77 68 69 63 68 20 74 68 65 20 74  w to which the t
6b7d0 72 69 67 67 65 72 20 61 70 70 6c 69 65 73 20 2a  rigger applies *
6b7e0 2f 0a 20 20 75 38 20 6f 70 3b 20 20 20 20 20 20  /.  u8 op;      
6b7f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
6b800 6e 65 20 6f 66 20 54 4b 5f 44 45 4c 45 54 45 2c  ne of TK_DELETE,
6b810 20 54 4b 5f 55 50 44 41 54 45 2c 20 54 4b 5f 49   TK_UPDATE, TK_I
6b820 4e 53 45 52 54 20 20 20 20 20 20 20 20 20 2a 2f  NSERT         */
6b830 0a 20 20 75 38 20 74 72 5f 74 6d 3b 20 20 20 20  .  u8 tr_tm;    
6b840 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e             /* On
6b850 65 20 6f 66 20 54 52 49 47 47 45 52 5f 42 45 46  e of TRIGGER_BEF
6b860 4f 52 45 2c 20 54 52 49 47 47 45 52 5f 41 46 54  ORE, TRIGGER_AFT
6b870 45 52 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57  ER */.  Expr *pW
6b880 68 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  hen;            
6b890 2f 2a 20 54 68 65 20 57 48 45 4e 20 63 6c 61 75  /* The WHEN clau
6b8a0 73 65 20 6f 66 20 74 68 65 20 65 78 70 72 65 73  se of the expres
6b8b0 73 69 6f 6e 20 28 6d 61 79 20 62 65 20 4e 55 4c  sion (may be NUL
6b8c0 4c 29 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a  L) */.  IdList *
6b8d0 70 43 6f 6c 75 6d 6e 73 3b 20 20 20 20 20 20 20  pColumns;       
6b8e0 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e  /* If this is an
6b8f0 20 55 50 44 41 54 45 20 4f 46 20 3c 63 6f 6c 75   UPDATE OF <colu
6b900 6d 6e 2d 6c 69 73 74 3e 20 74 72 69 67 67 65 72  mn-list> trigger
6b910 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
6b920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
6b930 68 65 20 3c 63 6f 6c 75 6d 6e 2d 6c 69 73 74 3e  he <column-list>
6b940 20 69 73 20 73 74 6f 72 65 64 20 68 65 72 65 20   is stored here 
6b950 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63  */.  Schema *pSc
6b960 68 65 6d 61 3b 20 20 20 20 20 20 20 20 2f 2a 20  hema;        /* 
6b970 53 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e 69 6e  Schema containin
6b980 67 20 74 68 65 20 74 72 69 67 67 65 72 20 2a 2f  g the trigger */
6b990 0a 20 20 53 63 68 65 6d 61 20 2a 70 54 61 62 53  .  Schema *pTabS
6b9a0 63 68 65 6d 61 3b 20 20 20 20 20 2f 2a 20 53 63  chema;     /* Sc
6b9b0 68 65 6d 61 20 63 6f 6e 74 61 69 6e 69 6e 67 20  hema containing 
6b9c0 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 54  the table */.  T
6b9d0 72 69 67 67 65 72 53 74 65 70 20 2a 73 74 65 70  riggerStep *step
6b9e0 5f 6c 69 73 74 3b 20 2f 2a 20 4c 69 6e 6b 20 6c  _list; /* Link l
6b9f0 69 73 74 20 6f 66 20 74 72 69 67 67 65 72 20 70  ist of trigger p
6ba00 72 6f 67 72 61 6d 20 73 74 65 70 73 20 20 20 20  rogram steps    
6ba10 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 54 72           */.  Tr
6ba20 69 67 67 65 72 20 2a 70 4e 65 78 74 3b 20 20 20  igger *pNext;   
6ba30 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 74 72        /* Next tr
6ba40 69 67 67 65 72 20 61 73 73 6f 63 69 61 74 65 64  igger associated
6ba50 20 77 69 74 68 20 74 68 65 20 74 61 62 6c 65 20   with the table 
6ba60 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 74  */.};../*.** A t
6ba70 72 69 67 67 65 72 20 69 73 20 65 69 74 68 65 72  rigger is either
6ba80 20 61 20 42 45 46 4f 52 45 20 6f 72 20 61 6e 20   a BEFORE or an 
6ba90 41 46 54 45 52 20 74 72 69 67 67 65 72 2e 20 20  AFTER trigger.  
6baa0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f  The following co
6bab0 6e 73 74 61 6e 74 73 0a 2a 2a 20 64 65 74 65 72  nstants.** deter
6bac0 6d 69 6e 65 20 77 68 69 63 68 2e 20 0a 2a 2a 0a  mine which. .**.
6bad0 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20  ** If there are 
6bae0 6d 75 6c 74 69 70 6c 65 20 74 72 69 67 67 65 72  multiple trigger
6baf0 73 2c 20 79 6f 75 20 6d 69 67 68 74 20 6f 66 20  s, you might of 
6bb00 73 6f 6d 65 20 42 45 46 4f 52 45 20 61 6e 64 20  some BEFORE and 
6bb10 73 6f 6d 65 20 41 46 54 45 52 2e 0a 2a 2a 20 49  some AFTER..** I
6bb20 6e 20 74 68 61 74 20 63 61 73 65 73 2c 20 74 68  n that cases, th
6bb30 65 20 63 6f 6e 73 74 61 6e 74 73 20 62 65 6c 6f  e constants belo
6bb40 77 20 63 61 6e 20 62 65 20 4f 52 65 64 20 74 6f  w can be ORed to
6bb50 67 65 74 68 65 72 2e 0a 2a 2f 0a 23 64 65 66 69  gether..*/.#defi
6bb60 6e 65 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52  ne TRIGGER_BEFOR
6bb70 45 20 20 31 0a 23 64 65 66 69 6e 65 20 54 52 49  E  1.#define TRI
6bb80 47 47 45 52 5f 41 46 54 45 52 20 20 20 32 0a 0a  GGER_AFTER   2..
6bb90 2f 2a 0a 20 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*. * An instanc
6bba0 65 20 6f 66 20 73 74 72 75 63 74 20 54 72 69 67  e of struct Trig
6bbb0 67 65 72 53 74 65 70 20 69 73 20 75 73 65 64 20  gerStep is used 
6bbc0 74 6f 20 73 74 6f 72 65 20 61 20 73 69 6e 67 6c  to store a singl
6bbd0 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 0a  e SQL statement.
6bbe0 20 2a 20 74 68 61 74 20 69 73 20 61 20 70 61 72   * that is a par
6bbf0 74 20 6f 66 20 61 20 74 72 69 67 67 65 72 2d 70  t of a trigger-p
6bc00 72 6f 67 72 61 6d 2e 20 0a 20 2a 0a 20 2a 20 49  rogram. . *. * I
6bc10 6e 73 74 61 6e 63 65 73 20 6f 66 20 73 74 72 75  nstances of stru
6bc20 63 74 20 54 72 69 67 67 65 72 53 74 65 70 20 61  ct TriggerStep a
6bc30 72 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20 73  re stored in a s
6bc40 69 6e 67 6c 79 20 6c 69 6e 6b 65 64 20 6c 69 73  ingly linked lis
6bc50 74 20 28 6c 69 6e 6b 65 64 0a 20 2a 20 75 73 69  t (linked. * usi
6bc60 6e 67 20 74 68 65 20 22 70 4e 65 78 74 22 20 6d  ng the "pNext" m
6bc70 65 6d 62 65 72 29 20 72 65 66 65 72 65 6e 63 65  ember) reference
6bc80 64 20 62 79 20 74 68 65 20 22 73 74 65 70 5f 6c  d by the "step_l
6bc90 69 73 74 22 20 6d 65 6d 62 65 72 20 6f 66 20 74  ist" member of t
6bca0 68 65 20 0a 20 2a 20 61 73 73 6f 63 69 61 74 65  he . * associate
6bcb0 64 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72  d struct Trigger
6bcc0 20 69 6e 73 74 61 6e 63 65 2e 20 54 68 65 20 66   instance. The f
6bcd0 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20  irst element of 
6bce0 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  the linked list 
6bcf0 69 73 0a 20 2a 20 74 68 65 20 66 69 72 73 74 20  is. * the first 
6bd00 73 74 65 70 20 6f 66 20 74 68 65 20 74 72 69 67  step of the trig
6bd10 67 65 72 2d 70 72 6f 67 72 61 6d 2e 0a 20 2a 20  ger-program.. * 
6bd20 0a 20 2a 20 54 68 65 20 22 6f 70 22 20 6d 65 6d  . * The "op" mem
6bd30 62 65 72 20 69 6e 64 69 63 61 74 65 73 20 77 68  ber indicates wh
6bd40 65 74 68 65 72 20 74 68 69 73 20 69 73 20 61 20  ether this is a 
6bd50 22 44 45 4c 45 54 45 22 2c 20 22 49 4e 53 45 52  "DELETE", "INSER
6bd60 54 22 2c 20 22 55 50 44 41 54 45 22 20 6f 72 0a  T", "UPDATE" or.
6bd70 20 2a 20 22 53 45 4c 45 43 54 22 20 73 74 61 74   * "SELECT" stat
6bd80 65 6d 65 6e 74 2e 20 54 68 65 20 6d 65 61 6e 69  ement. The meani
6bd90 6e 67 73 20 6f 66 20 74 68 65 20 6f 74 68 65 72  ngs of the other
6bda0 20 6d 65 6d 62 65 72 73 20 69 73 20 64 65 74 65   members is dete
6bdb0 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 0a 20  rmined by the . 
6bdc0 2a 20 76 61 6c 75 65 20 6f 66 20 22 6f 70 22 20  * value of "op" 
6bdd0 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 2a 0a 20  as follows:. *. 
6bde0 2a 20 28 6f 70 20 3d 3d 20 54 4b 5f 49 4e 53 45  * (op == TK_INSE
6bdf0 52 54 29 0a 20 2a 20 6f 72 63 6f 6e 66 20 20 20  RT). * orconf   
6be00 20 2d 3e 20 73 74 6f 72 65 73 20 74 68 65 20 4f   -> stores the O
6be10 4e 20 43 4f 4e 46 4c 49 43 54 20 61 6c 67 6f 72  N CONFLICT algor
6be20 69 74 68 6d 0a 20 2a 20 70 53 65 6c 65 63 74 20  ithm. * pSelect 
6be30 20 20 2d 3e 20 49 66 20 74 68 69 73 20 69 73 20    -> If this is 
6be40 61 6e 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e  an INSERT INTO .
6be50 2e 2e 20 53 45 4c 45 43 54 20 2e 2e 2e 20 73 74  .. SELECT ... st
6be60 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 0a 20 2a  atement, then. *
6be70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68                th
6be80 69 73 20 73 74 6f 72 65 73 20 61 20 70 6f 69 6e  is stores a poin
6be90 74 65 72 20 74 6f 20 74 68 65 20 53 45 4c 45 43  ter to the SELEC
6bea0 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 4f 74 68  T statement. Oth
6beb0 65 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20  erwise NULL.. * 
6bec0 74 61 72 67 65 74 20 20 20 20 2d 3e 20 41 20 74  target    -> A t
6bed0 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65  oken holding the
6bee0 20 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20   quoted name of 
6bef0 74 68 65 20 74 61 62 6c 65 20 74 6f 20 69 6e 73  the table to ins
6bf00 65 72 74 20 69 6e 74 6f 2e 0a 20 2a 20 70 45 78  ert into.. * pEx
6bf10 70 72 4c 69 73 74 20 2d 3e 20 49 66 20 74 68 69  prList -> If thi
6bf20 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54 20 49  s is an INSERT I
6bf30 4e 54 4f 20 2e 2e 2e 20 56 41 4c 55 45 53 20 2e  NTO ... VALUES .
6bf40 2e 2e 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68  .. statement, th
6bf50 65 6e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20  en. *           
6bf60 20 20 20 74 68 69 73 20 73 74 6f 72 65 73 20 76     this stores v
6bf70 61 6c 75 65 73 20 74 6f 20 62 65 20 69 6e 73 65  alues to be inse
6bf80 72 74 65 64 2e 20 4f 74 68 65 72 77 69 73 65 20  rted. Otherwise 
6bf90 4e 55 4c 4c 2e 0a 20 2a 20 70 49 64 4c 69 73 74  NULL.. * pIdList
6bfa0 20 20 20 2d 3e 20 49 66 20 74 68 69 73 20 69 73     -> If this is
6bfb0 20 61 6e 20 49 4e 53 45 52 54 20 49 4e 54 4f 20   an INSERT INTO 
6bfc0 2e 2e 2e 20 28 3c 63 6f 6c 75 6d 6e 2d 6e 61 6d  ... (<column-nam
6bfd0 65 73 3e 29 20 56 41 4c 55 45 53 20 2e 2e 2e 20  es>) VALUES ... 
6bfe0 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  . *             
6bff0 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e   statement, then
6c000 20 74 68 69 73 20 73 74 6f 72 65 73 20 74 68 65   this stores the
6c010 20 63 6f 6c 75 6d 6e 2d 6e 61 6d 65 73 20 74 6f   column-names to
6c020 20 62 65 0a 20 2a 20 20 20 20 20 20 20 20 20 20   be. *          
6c030 20 20 20 20 69 6e 73 65 72 74 65 64 20 69 6e 74      inserted int
6c040 6f 2e 0a 20 2a 0a 20 2a 20 28 6f 70 20 3d 3d 20  o.. *. * (op == 
6c050 54 4b 5f 44 45 4c 45 54 45 29 0a 20 2a 20 74 61  TK_DELETE). * ta
6c060 72 67 65 74 20 20 20 20 2d 3e 20 41 20 74 6f 6b  rget    -> A tok
6c070 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 71  en holding the q
6c080 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 74 68  uoted name of th
6c090 65 20 74 61 62 6c 65 20 74 6f 20 64 65 6c 65 74  e table to delet
6c0a0 65 20 66 72 6f 6d 2e 0a 20 2a 20 70 57 68 65 72  e from.. * pWher
6c0b0 65 20 20 20 20 2d 3e 20 54 68 65 20 57 48 45 52  e    -> The WHER
6c0c0 45 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20  E clause of the 
6c0d0 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74  DELETE statement
6c0e0 20 69 66 20 6f 6e 65 20 69 73 20 73 70 65 63 69   if one is speci
6c0f0 66 69 65 64 2e 0a 20 2a 20 20 20 20 20 20 20 20  fied.. *        
6c100 20 20 20 20 20 20 4f 74 68 65 72 77 69 73 65 20        Otherwise 
6c110 4e 55 4c 4c 2e 0a 20 2a 20 0a 20 2a 20 28 6f 70  NULL.. * . * (op
6c120 20 3d 3d 20 54 4b 5f 55 50 44 41 54 45 29 0a 20   == TK_UPDATE). 
6c130 2a 20 74 61 72 67 65 74 20 20 20 20 2d 3e 20 41  * target    -> A
6c140 20 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74   token holding t
6c150 68 65 20 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f  he quoted name o
6c160 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 75  f the table to u
6c170 70 64 61 74 65 20 72 6f 77 73 20 6f 66 2e 0a 20  pdate rows of.. 
6c180 2a 20 70 57 68 65 72 65 20 20 20 20 2d 3e 20 54  * pWhere    -> T
6c190 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
6c1a0 6f 66 20 74 68 65 20 55 50 44 41 54 45 20 73 74  of the UPDATE st
6c1b0 61 74 65 6d 65 6e 74 20 69 66 20 6f 6e 65 20 69  atement if one i
6c1c0 73 20 73 70 65 63 69 66 69 65 64 2e 0a 20 2a 20  s specified.. * 
6c1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 4f 74 68               Oth
6c1e0 65 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20  erwise NULL.. * 
6c1f0 70 45 78 70 72 4c 69 73 74 20 2d 3e 20 41 20 6c  pExprList -> A l
6c200 69 73 74 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d  ist of the colum
6c210 6e 73 20 74 6f 20 75 70 64 61 74 65 20 61 6e 64  ns to update and
6c220 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73   the expressions
6c230 20 74 6f 20 75 70 64 61 74 65 0a 20 2a 20 20 20   to update. *   
6c240 20 20 20 20 20 20 20 20 20 20 20 74 68 65 6d 20             them 
6c250 74 6f 2e 20 53 65 65 20 73 71 6c 69 74 65 33 55  to. See sqlite3U
6c260 70 64 61 74 65 28 29 20 64 6f 63 75 6d 65 6e 74  pdate() document
6c270 61 74 69 6f 6e 20 6f 66 20 22 70 43 68 61 6e 67  ation of "pChang
6c280 65 73 22 0a 20 2a 20 20 20 20 20 20 20 20 20 20  es". *          
6c290 20 20 20 20 61 72 67 75 6d 65 6e 74 2e 0a 20 2a      argument.. *
6c2a0 20 0a 20 2a 2f 0a 73 74 72 75 63 74 20 54 72 69   . */.struct Tri
6c2b0 67 67 65 72 53 74 65 70 20 7b 0a 20 20 75 38 20  ggerStep {.  u8 
6c2c0 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  op;             
6c2d0 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 44    /* One of TK_D
6c2e0 45 4c 45 54 45 2c 20 54 4b 5f 55 50 44 41 54 45  ELETE, TK_UPDATE
6c2f0 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20 54 4b 5f  , TK_INSERT, TK_
6c300 53 45 4c 45 43 54 20 2a 2f 0a 20 20 75 38 20 6f  SELECT */.  u8 o
6c310 72 63 6f 6e 66 3b 20 20 20 20 20 20 20 20 20 20  rconf;          
6c320 20 2f 2a 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20   /* OE_Rollback 
6c330 65 74 63 2e 20 2a 2f 0a 20 20 54 72 69 67 67 65  etc. */.  Trigge
6c340 72 20 2a 70 54 72 69 67 3b 20 20 20 20 20 20 2f  r *pTrig;      /
6c350 2a 20 54 68 65 20 74 72 69 67 67 65 72 20 74 68  * The trigger th
6c360 61 74 20 74 68 69 73 20 73 74 65 70 20 69 73 20  at this step is 
6c370 61 20 70 61 72 74 20 6f 66 20 2a 2f 0a 20 20 53  a part of */.  S
6c380 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20  elect *pSelect; 
6c390 20 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 73 74      /* SELECT st
6c3a0 61 74 6d 65 6e 74 20 6f 72 20 52 48 53 20 6f 66  atment or RHS of
6c3b0 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e 20   INSERT INTO .. 
6c3c0 53 45 4c 45 43 54 20 2e 2e 2e 20 2a 2f 0a 20 20  SELECT ... */.  
6c3d0 54 6f 6b 65 6e 20 74 61 72 67 65 74 3b 20 20 20  Token target;   
6c3e0 20 20 20 20 20 2f 2a 20 54 61 72 67 65 74 20 74       /* Target t
6c3f0 61 62 6c 65 20 66 6f 72 20 44 45 4c 45 54 45 2c  able for DELETE,
6c400 20 55 50 44 41 54 45 2c 20 49 4e 53 45 52 54 20   UPDATE, INSERT 
6c410 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72  */.  Expr *pWher
6c420 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  e;        /* The
6c430 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 66 6f   WHERE clause fo
6c440 72 20 44 45 4c 45 54 45 20 6f 72 20 55 50 44 41  r DELETE or UPDA
6c450 54 45 20 73 74 65 70 73 20 2a 2f 0a 20 20 45 78  TE steps */.  Ex
6c460 70 72 4c 69 73 74 20 2a 70 45 78 70 72 4c 69 73  prList *pExprLis
6c470 74 3b 20 2f 2a 20 53 45 54 20 63 6c 61 75 73 65  t; /* SET clause
6c480 20 66 6f 72 20 55 50 44 41 54 45 2e 20 20 56 41   for UPDATE.  VA
6c490 4c 55 45 53 20 63 6c 61 75 73 65 20 66 6f 72 20  LUES clause for 
6c4a0 49 4e 53 45 52 54 20 2a 2f 0a 20 20 49 64 4c 69  INSERT */.  IdLi
6c4b0 73 74 20 2a 70 49 64 4c 69 73 74 3b 20 20 20 20  st *pIdList;    
6c4c0 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73   /* Column names
6c4d0 20 66 6f 72 20 49 4e 53 45 52 54 20 2a 2f 0a 20   for INSERT */. 
6c4e0 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 4e   TriggerStep *pN
6c4f0 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 69 6e  ext;  /* Next in
6c500 20 74 68 65 20 6c 69 6e 6b 2d 6c 69 73 74 20 2a   the link-list *
6c510 2f 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20  /.  TriggerStep 
6c520 2a 70 4c 61 73 74 3b 20 20 2f 2a 20 4c 61 73 74  *pLast;  /* Last
6c530 20 65 6c 65 6d 65 6e 74 20 69 6e 20 6c 69 6e 6b   element in link
6c540 2d 6c 69 73 74 2e 20 56 61 6c 69 64 20 66 6f 72  -list. Valid for
6c550 20 31 73 74 20 65 6c 65 6d 20 6f 6e 6c 79 20 2a   1st elem only *
6c560 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  /.};../*.** The 
6c570 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
6c580 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 66  ure contains inf
6c590 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 62 79  ormation used by
6c5a0 20 74 68 65 20 73 71 6c 69 74 65 46 69 78 2e 2e   the sqliteFix..
6c5b0 2e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 61 73  ..** routines as
6c5c0 20 74 68 65 79 20 77 61 6c 6b 20 74 68 65 20 70   they walk the p
6c5d0 61 72 73 65 20 74 72 65 65 20 74 6f 20 6d 61 6b  arse tree to mak
6c5e0 65 20 64 61 74 61 62 61 73 65 20 72 65 66 65 72  e database refer
6c5f0 65 6e 63 65 73 0a 2a 2a 20 65 78 70 6c 69 63 69  ences.** explici
6c600 74 2e 20 20 0a 2a 2f 0a 74 79 70 65 64 65 66 20  t.  .*/.typedef 
6c610 73 74 72 75 63 74 20 44 62 46 69 78 65 72 20 44  struct DbFixer D
6c620 62 46 69 78 65 72 3b 0a 73 74 72 75 63 74 20 44  bFixer;.struct D
6c630 62 46 69 78 65 72 20 7b 0a 20 20 50 61 72 73 65  bFixer {.  Parse
6c640 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 2f   *pParse;      /
6c650 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f  * The parsing co
6c660 6e 74 65 78 74 2e 20 20 45 72 72 6f 72 20 6d 65  ntext.  Error me
6c670 73 73 61 67 65 73 20 77 72 69 74 74 65 6e 20 68  ssages written h
6c680 65 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ere */.  const c
6c690 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 2f 2a 20  har *zDb;    /* 
6c6a0 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 6f 62  Make sure all ob
6c6b0 6a 65 63 74 73 20 61 72 65 20 63 6f 6e 74 61 69  jects are contai
6c6c0 6e 65 64 20 69 6e 20 74 68 69 73 20 64 61 74 61  ned in this data
6c6d0 62 61 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  base */.  const 
6c6e0 63 68 61 72 20 2a 7a 54 79 70 65 3b 20 20 2f 2a  char *zType;  /*
6c6f0 20 54 79 70 65 20 6f 66 20 74 68 65 20 63 6f 6e   Type of the con
6c700 74 61 69 6e 65 72 20 2d 20 75 73 65 64 20 66 6f  tainer - used fo
6c710 72 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73  r error messages
6c720 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b 65   */.  const Toke
6c730 6e 20 2a 70 4e 61 6d 65 3b 20 2f 2a 20 4e 61 6d  n *pName; /* Nam
6c740 65 20 6f 66 20 74 68 65 20 63 6f 6e 74 61 69 6e  e of the contain
6c750 65 72 20 2d 20 75 73 65 64 20 66 6f 72 20 65 72  er - used for er
6c760 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a  ror messages */.
6c770 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 6f 62 6a  };../*.** An obj
6c780 65 63 74 65 64 20 75 73 65 64 20 74 6f 20 61 63  ected used to ac
6c790 63 75 6d 75 6c 61 74 65 20 74 68 65 20 74 65 78  cumulate the tex
6c7a0 74 20 6f 66 20 61 20 73 74 72 69 6e 67 20 77 68  t of a string wh
6c7b0 65 72 65 20 77 65 0a 2a 2a 20 64 6f 20 6e 6f 74  ere we.** do not
6c7c0 20 6e 65 63 65 73 73 61 72 69 6c 79 20 6b 6e 6f   necessarily kno
6c7d0 77 20 68 6f 77 20 62 69 67 20 74 68 65 20 73 74  w how big the st
6c7e0 72 69 6e 67 20 77 69 6c 6c 20 62 65 20 69 6e 20  ring will be in 
6c7f0 74 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74 72 75  the end..*/.stru
6c800 63 74 20 53 74 72 41 63 63 75 6d 20 7b 0a 20 20  ct StrAccum {.  
6c810 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20  sqlite3 *db;    
6c820 20 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c       /* Optional
6c830 20 64 61 74 61 62 61 73 65 20 66 6f 72 20 6c 6f   database for lo
6c840 6f 6b 61 73 69 64 65 2e 20 20 43 61 6e 20 62 65  okaside.  Can be
6c850 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 68 61 72 20   NULL */.  char 
6c860 2a 7a 42 61 73 65 3b 20 20 20 20 20 20 20 20 20  *zBase;         
6c870 2f 2a 20 41 20 62 61 73 65 20 61 6c 6c 6f 63 61  /* A base alloca
6c880 74 69 6f 6e 2e 20 20 4e 6f 74 20 66 72 6f 6d 20  tion.  Not from 
6c890 6d 61 6c 6c 6f 63 2e 20 2a 2f 0a 20 20 63 68 61  malloc. */.  cha
6c8a0 72 20 2a 7a 54 65 78 74 3b 20 20 20 20 20 20 20  r *zText;       
6c8b0 20 20 2f 2a 20 54 68 65 20 73 74 72 69 6e 67 20    /* The string 
6c8c0 63 6f 6c 6c 65 63 74 65 64 20 73 6f 20 66 61 72  collected so far
6c8d0 20 2a 2f 0a 20 20 69 6e 74 20 20 6e 43 68 61 72   */.  int  nChar
6c8e0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65  ;          /* Le
6c8f0 6e 67 74 68 20 6f 66 20 74 68 65 20 73 74 72 69  ngth of the stri
6c900 6e 67 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69  ng so far */.  i
6c910 6e 74 20 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20  nt  nAlloc;     
6c920 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66      /* Amount of
6c930 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64   space allocated
6c940 20 69 6e 20 7a 54 65 78 74 20 2a 2f 0a 20 20 69   in zText */.  i
6c950 6e 74 20 20 6d 78 41 6c 6c 6f 63 3b 20 20 20 20  nt  mxAlloc;    
6c960 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61      /* Maximum a
6c970 6c 6c 6f 77 65 64 20 73 74 72 69 6e 67 20 6c 65  llowed string le
6c980 6e 67 74 68 20 2a 2f 0a 20 20 75 38 20 20 20 6d  ngth */.  u8   m
6c990 61 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 20 20 2f  allocFailed;   /
6c9a0 2a 20 42 65 63 6f 6d 65 73 20 74 72 75 65 20 69  * Becomes true i
6c9b0 66 20 61 6e 79 20 6d 65 6d 6f 72 79 20 61 6c 6c  f any memory all
6c9c0 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 20 2a 2f  ocation fails */
6c9d0 0a 20 20 75 38 20 20 20 75 73 65 4d 61 6c 6c 6f  .  u8   useMallo
6c9e0 63 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  c;      /* True 
6c9f0 69 66 20 7a 54 65 78 74 20 69 73 20 65 6e 6c 61  if zText is enla
6ca00 72 67 65 61 62 6c 65 20 75 73 69 6e 67 20 72 65  rgeable using re
6ca10 61 6c 6c 6f 63 20 2a 2f 0a 20 20 75 38 20 20 20  alloc */.  u8   
6ca20 74 6f 6f 42 69 67 3b 20 20 20 20 20 20 20 20 20  tooBig;         
6ca30 2f 2a 20 42 65 63 6f 6d 65 73 20 74 72 75 65 20  /* Becomes true 
6ca40 69 66 20 73 74 72 69 6e 67 20 73 69 7a 65 20 65  if string size e
6ca50 78 63 65 65 64 73 20 6c 69 6d 69 74 73 20 2a 2f  xceeds limits */
6ca60 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 70 6f 69  .};../*.** A poi
6ca70 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74 72  nter to this str
6ca80 75 63 74 75 72 65 20 69 73 20 75 73 65 64 20 74  ucture is used t
6ca90 6f 20 63 6f 6d 6d 75 6e 69 63 61 74 65 20 69 6e  o communicate in
6caa0 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 66 72 6f  formation.** fro
6cab0 6d 20 73 71 6c 69 74 65 33 49 6e 69 74 20 61 6e  m sqlite3Init an
6cac0 64 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61  d OP_ParseSchema
6cad0 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65   into the sqlite
6cae0 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 2e 0a 2a  3InitCallback..*
6caf0 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
6cb00 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62   {.  sqlite3 *db
6cb10 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ;        /* The 
6cb20 64 61 74 61 62 61 73 65 20 62 65 69 6e 67 20 69  database being i
6cb30 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20  nitialized */.  
6cb40 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20  int iDb;        
6cb50 20 20 20 20 2f 2a 20 30 20 66 6f 72 20 6d 61 69      /* 0 for mai
6cb60 6e 20 64 61 74 61 62 61 73 65 2e 20 20 31 20 66  n database.  1 f
6cb70 6f 72 20 54 45 4d 50 2c 20 32 2e 2e 20 66 6f 72  or TEMP, 2.. for
6cb80 20 41 54 54 41 43 48 65 64 20 2a 2f 0a 20 20 63   ATTACHed */.  c
6cb90 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 3b 20  har **pzErrMsg; 
6cba0 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73     /* Error mess
6cbb0 61 67 65 20 73 74 6f 72 65 64 20 68 65 72 65 20  age stored here 
6cbc0 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  */.  int rc;    
6cbd0 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75           /* Resu
6cbe0 6c 74 20 63 6f 64 65 20 73 74 6f 72 65 64 20 68  lt code stored h
6cbf0 65 72 65 20 2a 2f 0a 7d 20 49 6e 69 74 44 61 74  ere */.} InitDat
6cc00 61 3b 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 74  a;../*.** Struct
6cc10 75 72 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 67  ure containing g
6cc20 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72 61 74  lobal configurat
6cc30 69 6f 6e 20 64 61 74 61 20 66 6f 72 20 74 68 65  ion data for the
6cc40 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e   SQLite library.
6cc50 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75  .**.** This stru
6cc60 63 74 75 72 65 20 61 6c 73 6f 20 63 6f 6e 74 61  cture also conta
6cc70 69 6e 73 20 73 6f 6d 65 20 73 74 61 74 65 20 69  ins some state i
6cc80 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73  nformation..*/.s
6cc90 74 72 75 63 74 20 53 71 6c 69 74 65 33 43 6f 6e  truct Sqlite3Con
6cca0 66 69 67 20 7b 0a 20 20 69 6e 74 20 62 4d 65 6d  fig {.  int bMem
6ccb0 73 74 61 74 3b 20 20 20 20 20 20 20 20 20 20 20  stat;           
6ccc0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
6ccd0 65 20 74 6f 20 65 6e 61 62 6c 65 20 6d 65 6d 6f  e to enable memo
6cce0 72 79 20 73 74 61 74 75 73 20 2a 2f 0a 20 20 69  ry status */.  i
6ccf0 6e 74 20 62 43 6f 72 65 4d 75 74 65 78 3b 20 20  nt bCoreMutex;  
6cd00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6cd10 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e 61 62   /* True to enab
6cd20 6c 65 20 63 6f 72 65 20 6d 75 74 65 78 69 6e 67  le core mutexing
6cd30 20 2a 2f 0a 20 20 69 6e 74 20 62 46 75 6c 6c 4d   */.  int bFullM
6cd40 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 20 20  utex;           
6cd50 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
6cd60 74 6f 20 65 6e 61 62 6c 65 20 66 75 6c 6c 20 6d  to enable full m
6cd70 75 74 65 78 69 6e 67 20 2a 2f 0a 20 20 69 6e 74  utexing */.  int
6cd80 20 6d 78 53 74 72 6c 65 6e 3b 20 20 20 20 20 20   mxStrlen;      
6cd90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
6cda0 2a 20 4d 61 78 69 6d 75 6d 20 73 74 72 69 6e 67  * Maximum string
6cdb0 20 6c 65 6e 67 74 68 20 2a 2f 0a 20 20 69 6e 74   length */.  int
6cdc0 20 73 7a 4c 6f 6f 6b 61 73 69 64 65 3b 20 20 20   szLookaside;   
6cdd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
6cde0 2a 20 44 65 66 61 75 6c 74 20 6c 6f 6f 6b 61 73  * Default lookas
6cdf0 69 64 65 20 62 75 66 66 65 72 20 73 69 7a 65 20  ide buffer size 
6ce00 2a 2f 0a 20 20 69 6e 74 20 6e 4c 6f 6f 6b 61 73  */.  int nLookas
6ce10 69 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ide;            
6ce20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c         /* Defaul
6ce30 74 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 66  t lookaside buff
6ce40 65 72 20 63 6f 75 6e 74 20 2a 2f 0a 20 20 73 71  er count */.  sq
6ce50 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64  lite3_mem_method
6ce60 73 20 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  s m;            
6ce70 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d  /* Low-level mem
6ce80 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69  ory allocation i
6ce90 6e 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 73 71  nterface */.  sq
6cea0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68  lite3_mutex_meth
6ceb0 6f 64 73 20 6d 75 74 65 78 3b 20 20 20 20 20 20  ods mutex;      
6cec0 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 6d 75 74  /* Low-level mut
6ced0 65 78 20 69 6e 74 65 72 66 61 63 65 20 2a 2f 0a  ex interface */.
6cee0 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65    sqlite3_pcache
6cef0 5f 6d 65 74 68 6f 64 73 20 70 63 61 63 68 65 3b  _methods pcache;
6cf00 20 20 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c      /* Low-level
6cf10 20 70 61 67 65 2d 63 61 63 68 65 20 69 6e 74 65   page-cache inte
6cf20 72 66 61 63 65 20 2a 2f 0a 20 20 76 6f 69 64 20  rface */.  void 
6cf30 2a 70 48 65 61 70 3b 20 20 20 20 20 20 20 20 20  *pHeap;         
6cf40 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
6cf50 48 65 61 70 20 73 74 6f 72 61 67 65 20 73 70 61  Heap storage spa
6cf60 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 48 65 61  ce */.  int nHea
6cf70 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
6cf80 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
6cf90 65 20 6f 66 20 70 48 65 61 70 5b 5d 20 2a 2f 0a  e of pHeap[] */.
6cfa0 20 20 69 6e 74 20 6d 6e 52 65 71 2c 20 6d 78 52    int mnReq, mxR
6cfb0 65 71 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  eq;             
6cfc0 20 20 20 20 2f 2a 20 4d 69 6e 20 61 6e 64 20 6d      /* Min and m
6cfd0 61 78 20 68 65 61 70 20 72 65 71 75 65 73 74 73  ax heap requests
6cfe0 20 73 69 7a 65 73 20 2a 2f 0a 20 20 76 6f 69 64   sizes */.  void
6cff0 20 2a 70 53 63 72 61 74 63 68 3b 20 20 20 20 20   *pScratch;     
6d000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
6d010 20 53 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 20   Scratch memory 
6d020 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 63 72 61 74  */.  int szScrat
6d030 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ch;             
6d040 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
6d050 66 20 65 61 63 68 20 73 63 72 61 74 63 68 20 62  f each scratch b
6d060 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e  uffer */.  int n
6d070 53 63 72 61 74 63 68 3b 20 20 20 20 20 20 20 20  Scratch;        
6d080 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
6d090 4e 75 6d 62 65 72 20 6f 66 20 73 63 72 61 74 63  Number of scratc
6d0a0 68 20 62 75 66 66 65 72 73 20 2a 2f 0a 20 20 76  h buffers */.  v
6d0b0 6f 69 64 20 2a 70 50 61 67 65 3b 20 20 20 20 20  oid *pPage;     
6d0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d0d0 20 2f 2a 20 50 61 67 65 20 63 61 63 68 65 20 6d   /* Page cache m
6d0e0 65 6d 6f 72 79 20 2a 2f 0a 20 20 69 6e 74 20 73  emory */.  int s
6d0f0 7a 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20  zPage;          
6d100 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
6d110 53 69 7a 65 20 6f 66 20 65 61 63 68 20 70 61 67  Size of each pag
6d120 65 20 69 6e 20 70 50 61 67 65 5b 5d 20 2a 2f 0a  e in pPage[] */.
6d130 20 20 69 6e 74 20 6e 50 61 67 65 3b 20 20 20 20    int nPage;    
6d140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d150 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
6d160 20 70 61 67 65 73 20 69 6e 20 70 50 61 67 65 5b   pages in pPage[
6d170 5d 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 50 61 72  ] */.  int mxPar
6d180 73 65 72 53 74 61 63 6b 3b 20 20 20 20 20 20 20  serStack;       
6d190 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 61 78 69           /* maxi
6d1a0 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 74 68 65  mum depth of the
6d1b0 20 70 61 72 73 65 72 20 73 74 61 63 6b 20 2a 2f   parser stack */
6d1c0 0a 20 20 69 6e 74 20 73 68 61 72 65 64 43 61 63  .  int sharedCac
6d1d0 68 65 45 6e 61 62 6c 65 64 3b 20 20 20 20 20 20  heEnabled;      
6d1e0 20 20 20 20 20 2f 2a 20 74 72 75 65 20 69 66 20       /* true if 
6d1f0 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f 64  shared-cache mod
6d200 65 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 2f  e enabled */.  /
6d210 2a 20 54 68 65 20 61 62 6f 76 65 20 6d 69 67 68  * The above migh
6d220 74 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64  t be initialized
6d230 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 54   to non-zero.  T
6d240 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e 65 65  he following nee
6d250 64 20 74 6f 20 61 6c 77 61 79 73 0a 20 20 2a 2a  d to always.  **
6d260 20 69 6e 69 74 69 61 6c 6c 79 20 62 65 20 7a 65   initially be ze
6d270 72 6f 2c 20 68 6f 77 65 76 65 72 2e 20 2a 2f 0a  ro, however. */.
6d280 20 20 69 6e 74 20 69 73 49 6e 69 74 3b 20 20 20    int isInit;   
6d290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d2a0 20 20 20 20 2f 2a 20 54 72 75 65 20 61 66 74 65      /* True afte
6d2b0 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  r initialization
6d2c0 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 2a 2f   has finished */
6d2d0 0a 20 20 69 6e 74 20 69 6e 50 72 6f 67 72 65 73  .  int inProgres
6d2e0 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
6d2f0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 77 68 69       /* True whi
6d300 6c 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  le initializatio
6d310 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 20 2a 2f  n in progress */
6d320 0a 20 20 69 6e 74 20 69 73 4d 75 74 65 78 49 6e  .  int isMutexIn
6d330 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  it;             
6d340 20 20 20 20 20 2f 2a 20 54 72 75 65 20 61 66 74       /* True aft
6d350 65 72 20 6d 75 74 65 78 65 73 20 61 72 65 20 69  er mutexes are i
6d360 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20  nitialized */.  
6d370 69 6e 74 20 69 73 4d 61 6c 6c 6f 63 49 6e 69 74  int isMallocInit
6d380 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
6d390 20 20 2f 2a 20 54 72 75 65 20 61 66 74 65 72 20    /* True after 
6d3a0 6d 61 6c 6c 6f 63 20 69 73 20 69 6e 69 74 69 61  malloc is initia
6d3b0 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69  lized */.  int i
6d3c0 73 50 43 61 63 68 65 49 6e 69 74 3b 20 20 20 20  sPCacheInit;    
6d3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
6d3e0 54 72 75 65 20 61 66 74 65 72 20 6d 61 6c 6c 6f  True after mallo
6d3f0 63 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64  c is initialized
6d400 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75   */.  sqlite3_mu
6d410 74 65 78 20 2a 70 49 6e 69 74 4d 75 74 65 78 3b  tex *pInitMutex;
6d420 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78          /* Mutex
6d430 20 75 73 65 64 20 62 79 20 73 71 6c 69 74 65 33   used by sqlite3
6d440 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 2a 2f  _initialize() */
6d450 0a 20 20 69 6e 74 20 6e 52 65 66 49 6e 69 74 4d  .  int nRefInitM
6d460 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 20 20  utex;           
6d470 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
6d480 66 20 75 73 65 72 73 20 6f 66 20 70 49 6e 69 74  f users of pInit
6d490 4d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  Mutex */.};../*.
6d4a0 2a 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69 6e 74  ** Context point
6d4b0 65 72 20 70 61 73 73 65 64 20 64 6f 77 6e 20 74  er passed down t
6d4c0 68 72 6f 75 67 68 20 74 68 65 20 74 72 65 65 2d  hrough the tree-
6d4d0 77 61 6c 6b 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  walk..*/.struct 
6d4e0 57 61 6c 6b 65 72 20 7b 0a 20 20 69 6e 74 20 28  Walker {.  int (
6d4f0 2a 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 29 28  *xExprCallback)(
6d500 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 2a 29 3b  Walker*, Expr*);
6d510 20 20 20 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b       /* Callback
6d520 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f 6e 73   for expressions
6d530 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 65 6c   */.  int (*xSel
6d540 65 63 74 43 61 6c 6c 62 61 63 6b 29 28 57 61 6c  ectCallback)(Wal
6d550 6b 65 72 2a 2c 53 65 6c 65 63 74 2a 29 3b 20 20  ker*,Select*);  
6d560 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 6f 72 20  /* Callback for 
6d570 53 45 4c 45 43 54 73 20 2a 2f 0a 20 20 50 61 72  SELECTs */.  Par
6d580 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20  se *pParse;     
6d590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d5a0 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72         /* Parser
6d5b0 20 63 6f 6e 74 65 78 74 2e 20 20 2a 2f 0a 20 20   context.  */.  
6d5c0 75 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20 20 20  union {         
6d5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d5e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74            /* Ext
6d5f0 72 61 20 64 61 74 61 20 66 6f 72 20 63 61 6c 6c  ra data for call
6d600 62 61 63 6b 20 2a 2f 0a 20 20 20 20 4e 61 6d 65  back */.    Name
6d610 43 6f 6e 74 65 78 74 20 2a 70 4e 43 3b 20 20 20  Context *pNC;   
6d620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d630 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 69 6e 67         /* Naming
6d640 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 20 20   context */.    
6d650 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
6d660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d670 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
6d680 74 65 67 65 72 20 76 61 6c 75 65 20 2a 2f 0a 20  teger value */. 
6d690 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 20 46 6f 72   } u;.};../* For
6d6a0 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e  ward declaration
6d6b0 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  s */.SQLITE_PRIV
6d6c0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57  ATE int sqlite3W
6d6d0 61 6c 6b 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c  alkExpr(Walker*,
6d6e0 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   Expr*);.SQLITE_
6d6f0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6d700 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28  te3WalkExprList(
6d710 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 4c 69 73  Walker*, ExprLis
6d720 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
6d730 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57  ATE int sqlite3W
6d740 61 6c 6b 53 65 6c 65 63 74 28 57 61 6c 6b 65 72  alkSelect(Walker
6d750 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c  *, Select*);.SQL
6d760 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6d770 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63  sqlite3WalkSelec
6d780 74 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c 20 53  tExpr(Walker*, S
6d790 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  elect*);.SQLITE_
6d7a0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6d7b0 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 46 72 6f  te3WalkSelectFro
6d7c0 6d 28 57 61 6c 6b 65 72 2a 2c 20 53 65 6c 65 63  m(Walker*, Selec
6d7d0 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  t*);../*.** Retu
6d7e0 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 65  rn code from the
6d7f0 20 70 61 72 73 65 2d 74 72 65 65 20 77 61 6c 6b   parse-tree walk
6d800 69 6e 67 20 70 72 69 6d 69 74 69 76 65 73 20 61  ing primitives a
6d810 6e 64 20 74 68 65 69 72 0a 2a 2a 20 63 61 6c 6c  nd their.** call
6d820 62 61 63 6b 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e  backs..*/.#defin
6d830 65 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 20 20  e WRC_Continue  
6d840 20 20 30 20 20 20 2f 2a 20 43 6f 6e 74 69 6e 75    0   /* Continu
6d850 65 20 64 6f 77 6e 20 69 6e 74 6f 20 63 68 69 6c  e down into chil
6d860 64 72 65 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  dren */.#define 
6d870 57 52 43 5f 50 72 75 6e 65 20 20 20 20 20 20 20  WRC_Prune       
6d880 31 20 20 20 2f 2a 20 4f 6d 69 74 20 63 68 69 6c  1   /* Omit chil
6d890 64 72 65 6e 20 62 75 74 20 63 6f 6e 74 69 6e 75  dren but continu
6d8a0 65 20 77 61 6c 6b 69 6e 67 20 73 69 62 6c 69 6e  e walking siblin
6d8b0 67 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 52  gs */.#define WR
6d8c0 43 5f 41 62 6f 72 74 20 20 20 20 20 20 20 32 20  C_Abort       2 
6d8d0 20 20 2f 2a 20 41 62 61 6e 64 6f 6e 20 74 68 65    /* Abandon the
6d8e0 20 74 72 65 65 20 77 61 6c 6b 20 2a 2f 0a 0a 2f   tree walk */../
6d8f0 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20 7a 49  *.** Assuming zI
6d900 6e 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20  n points to the 
6d910 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 61 20  first byte of a 
6d920 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 2c  UTF-8 character,
6d930 0a 2a 2a 20 61 64 76 61 6e 63 65 20 7a 49 6e 20  .** advance zIn 
6d940 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20  to point to the 
6d950 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68  first byte of th
6d960 65 20 6e 65 78 74 20 55 54 46 2d 38 20 63 68 61  e next UTF-8 cha
6d970 72 61 63 74 65 72 2e 0a 2a 2f 0a 23 64 65 66 69  racter..*/.#defi
6d980 6e 65 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55  ne SQLITE_SKIP_U
6d990 54 46 38 28 7a 49 6e 29 20 7b 20 20 20 20 20 20  TF8(zIn) {      
6d9a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d9b0 20 20 5c 0a 20 20 69 66 28 20 28 2a 28 7a 49 6e    \.  if( (*(zIn
6d9c0 2b 2b 29 29 3e 3d 30 78 63 30 20 29 7b 20 20 20  ++))>=0xc0 ){   
6d9d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d9e0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
6d9f0 20 77 68 69 6c 65 28 20 28 2a 7a 49 6e 20 26 20   while( (*zIn & 
6da00 30 78 63 30 29 3d 3d 30 78 38 30 20 29 7b 20 7a  0xc0)==0x80 ){ z
6da10 49 6e 2b 2b 3b 20 7d 20 20 20 20 20 20 20 20 20  In++; }         
6da20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20      \.  }       
6da30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6da40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6da50 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d               \.}
6da60 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49  ../*.** The SQLI
6da70 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 20  TE_CORRUPT_BKPT 
6da80 6d 61 63 72 6f 20 63 61 6e 20 62 65 20 65 69 74  macro can be eit
6da90 68 65 72 20 61 20 63 6f 6e 73 74 61 6e 74 20 28  her a constant (
6daa0 66 6f 72 20 70 72 6f 64 75 63 74 69 6f 6e 0a 2a  for production.*
6dab0 2a 20 62 75 69 6c 64 73 29 20 6f 72 20 61 20 66  * builds) or a f
6dac0 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 28 66 6f  unction call (fo
6dad0 72 20 64 65 62 75 67 67 69 6e 67 29 2e 20 20 49  r debugging).  I
6dae0 66 20 69 74 20 69 73 20 61 20 66 75 6e 63 74 69  f it is a functi
6daf0 6f 6e 20 63 61 6c 6c 2c 0a 2a 2a 20 69 74 20 61  on call,.** it a
6db00 6c 6c 6f 77 73 20 74 68 65 20 6f 70 65 72 61 74  llows the operat
6db10 6f 72 20 74 6f 20 73 65 74 20 61 20 62 72 65 61  or to set a brea
6db20 6b 70 6f 69 6e 74 20 61 74 20 74 68 65 20 73 70  kpoint at the sp
6db30 6f 74 20 77 68 65 72 65 20 64 61 74 61 62 61 73  ot where databas
6db40 65 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e 20  e.** corruption 
6db50 69 73 20 66 69 72 73 74 20 64 65 74 65 63 74 65  is first detecte
6db60 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  d..*/.#ifdef SQL
6db70 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45  ITE_DEBUG.SQLITE
6db80 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73  _PRIVATE   int s
6db90 71 6c 69 74 65 33 43 6f 72 72 75 70 74 28 76 6f  qlite3Corrupt(vo
6dba0 69 64 29 3b 0a 23 20 64 65 66 69 6e 65 20 53 51  id);.# define SQ
6dbb0 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
6dbc0 54 20 73 71 6c 69 74 65 33 43 6f 72 72 75 70 74  T sqlite3Corrupt
6dbd0 28 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  ().#else.# defin
6dbe0 65 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  e SQLITE_CORRUPT
6dbf0 5f 42 4b 50 54 20 53 51 4c 49 54 45 5f 43 4f 52  _BKPT SQLITE_COR
6dc00 52 55 50 54 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  RUPT.#endif../*.
6dc10 2a 2a 20 54 68 65 20 63 74 79 70 65 2e 68 20 68  ** The ctype.h h
6dc20 65 61 64 65 72 20 69 73 20 6e 65 65 64 65 64 20  eader is needed 
6dc30 66 6f 72 20 6e 6f 6e 2d 41 53 43 49 49 20 73 79  for non-ASCII sy
6dc40 73 74 65 6d 73 2e 20 20 49 74 20 69 73 20 61 6c  stems.  It is al
6dc50 73 6f 0a 2a 2a 20 6e 65 65 64 65 64 20 62 79 20  so.** needed by 
6dc60 46 54 53 33 20 77 68 65 6e 20 46 54 53 33 20 69  FTS3 when FTS3 i
6dc70 73 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68  s included in th
6dc80 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a  e amalgamation..
6dc90 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  */.#if !defined(
6dca0 53 51 4c 49 54 45 5f 41 53 43 49 49 29 20 7c 7c  SQLITE_ASCII) ||
6dcb0 20 5c 0a 20 20 20 20 28 64 65 66 69 6e 65 64 28   \.    (defined(
6dcc0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54  SQLITE_ENABLE_FT
6dcd0 53 33 29 20 26 26 20 64 65 66 69 6e 65 64 28 53  S3) && defined(S
6dce0 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49  QLITE_AMALGAMATI
6dcf0 4f 4e 29 29 0a 23 20 69 6e 63 6c 75 64 65 20 3c  ON)).# include <
6dd00 63 74 79 70 65 2e 68 3e 0a 23 65 6e 64 69 66 0a  ctype.h>.#endif.
6dd10 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
6dd20 77 69 6e 67 20 6d 61 63 72 6f 73 20 6d 69 6d 69  wing macros mimi
6dd30 63 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 6c  c the standard l
6dd40 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 73  ibrary functions
6dd50 20 74 6f 75 70 70 65 72 28 29 2c 0a 2a 2a 20 69   toupper(),.** i
6dd60 73 73 70 61 63 65 28 29 2c 20 69 73 61 6c 6e 75  sspace(), isalnu
6dd70 6d 28 29 2c 20 69 73 64 69 67 69 74 28 29 20 61  m(), isdigit() a
6dd80 6e 64 20 69 73 78 64 69 67 69 74 28 29 2c 20 72  nd isxdigit(), r
6dd90 65 73 70 65 63 74 69 76 65 6c 79 2e 20 54 68 65  espectively. The
6dda0 0a 2a 2a 20 73 71 6c 69 74 65 20 76 65 72 73 69  .** sqlite versi
6ddb0 6f 6e 73 20 6f 6e 6c 79 20 77 6f 72 6b 20 66 6f  ons only work fo
6ddc0 72 20 41 53 43 49 49 20 63 68 61 72 61 63 74 65  r ASCII characte
6ddd0 72 73 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f  rs, regardless o
6dde0 66 20 6c 6f 63 61 6c 65 2e 0a 2a 2f 0a 23 69 66  f locale..*/.#if
6ddf0 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49  def SQLITE_ASCII
6de00 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
6de10 33 54 6f 75 70 70 65 72 28 78 29 20 20 28 28 78  3Toupper(x)  ((x
6de20 29 26 7e 28 73 71 6c 69 74 65 33 43 74 79 70 65  )&~(sqlite3Ctype
6de30 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64 20 63 68  Map[(unsigned ch
6de40 61 72 29 28 78 29 5d 26 30 78 32 30 29 29 0a 23  ar)(x)]&0x20)).#
6de50 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49   define sqlite3I
6de60 73 73 70 61 63 65 28 78 29 20 20 20 28 73 71 6c  sspace(x)   (sql
6de70 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28 75 6e  ite3CtypeMap[(un
6de80 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d  signed char)(x)]
6de90 26 30 78 30 31 29 0a 23 20 64 65 66 69 6e 65 20  &0x01).# define 
6dea0 73 71 6c 69 74 65 33 49 73 61 6c 6e 75 6d 28 78  sqlite3Isalnum(x
6deb0 29 20 20 20 28 73 71 6c 69 74 65 33 43 74 79 70  )   (sqlite3Ctyp
6dec0 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64 20 63  eMap[(unsigned c
6ded0 68 61 72 29 28 78 29 5d 26 30 78 30 36 29 0a 23  har)(x)]&0x06).#
6dee0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49   define sqlite3I
6def0 73 61 6c 70 68 61 28 78 29 20 20 20 28 73 71 6c  salpha(x)   (sql
6df00 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28 75 6e  ite3CtypeMap[(un
6df10 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d  signed char)(x)]
6df20 26 30 78 30 32 29 0a 23 20 64 65 66 69 6e 65 20  &0x02).# define 
6df30 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 78  sqlite3Isdigit(x
6df40 29 20 20 20 28 73 71 6c 69 74 65 33 43 74 79 70  )   (sqlite3Ctyp
6df50 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64 20 63  eMap[(unsigned c
6df60 68 61 72 29 28 78 29 5d 26 30 78 30 34 29 0a 23  har)(x)]&0x04).#
6df70 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49   define sqlite3I
6df80 73 78 64 69 67 69 74 28 78 29 20 20 28 73 71 6c  sxdigit(x)  (sql
6df90 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28 75 6e  ite3CtypeMap[(un
6dfa0 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d  signed char)(x)]
6dfb0 26 30 78 30 38 29 0a 23 20 64 65 66 69 6e 65 20  &0x08).# define 
6dfc0 73 71 6c 69 74 65 33 54 6f 6c 6f 77 65 72 28 78  sqlite3Tolower(x
6dfd0 29 20 20 20 28 73 71 6c 69 74 65 33 55 70 70 65  )   (sqlite3Uppe
6dfe0 72 54 6f 4c 6f 77 65 72 5b 28 75 6e 73 69 67 6e  rToLower[(unsign
6dff0 65 64 20 63 68 61 72 29 28 78 29 5d 29 0a 23 65  ed char)(x)]).#e
6e000 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  lse.# define sql
6e010 69 74 65 33 54 6f 75 70 70 65 72 28 78 29 20 20  ite3Toupper(x)  
6e020 20 74 6f 75 70 70 65 72 28 28 75 6e 73 69 67 6e   toupper((unsign
6e030 65 64 20 63 68 61 72 29 28 78 29 29 0a 23 20 64  ed char)(x)).# d
6e040 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 73  efine sqlite3Iss
6e050 70 61 63 65 28 78 29 20 20 20 69 73 73 70 61 63  pace(x)   isspac
6e060 65 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  e((unsigned char
6e070 29 28 78 29 29 0a 23 20 64 65 66 69 6e 65 20 73  )(x)).# define s
6e080 71 6c 69 74 65 33 49 73 61 6c 6e 75 6d 28 78 29  qlite3Isalnum(x)
6e090 20 20 20 69 73 61 6c 6e 75 6d 28 28 75 6e 73 69     isalnum((unsi
6e0a0 67 6e 65 64 20 63 68 61 72 29 28 78 29 29 0a 23  gned char)(x)).#
6e0b0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49   define sqlite3I
6e0c0 73 61 6c 70 68 61 28 78 29 20 20 20 69 73 61 6c  salpha(x)   isal
6e0d0 70 68 61 28 28 75 6e 73 69 67 6e 65 64 20 63 68  pha((unsigned ch
6e0e0 61 72 29 28 78 29 29 0a 23 20 64 65 66 69 6e 65  ar)(x)).# define
6e0f0 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28   sqlite3Isdigit(
6e100 78 29 20 20 20 69 73 64 69 67 69 74 28 28 75 6e  x)   isdigit((un
6e110 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 29  signed char)(x))
6e120 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
6e130 33 49 73 78 64 69 67 69 74 28 78 29 20 20 69 73  3Isxdigit(x)  is
6e140 78 64 69 67 69 74 28 28 75 6e 73 69 67 6e 65 64  xdigit((unsigned
6e150 20 63 68 61 72 29 28 78 29 29 0a 23 20 64 65 66   char)(x)).# def
6e160 69 6e 65 20 73 71 6c 69 74 65 33 54 6f 6c 6f 77  ine sqlite3Tolow
6e170 65 72 28 78 29 20 20 20 74 6f 6c 6f 77 65 72 28  er(x)   tolower(
6e180 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28  (unsigned char)(
6e190 78 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  x)).#endif../*.*
6e1a0 2a 20 49 6e 74 65 72 6e 61 6c 20 66 75 6e 63 74  * Internal funct
6e1b0 69 6f 6e 20 70 72 6f 74 6f 74 79 70 65 73 0a 2a  ion prototypes.*
6e1c0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
6e1d0 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 49   int sqlite3StrI
6e1e0 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  Cmp(const char *
6e1f0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b  , const char *);
6e200 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6e210 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4e 75 6d  int sqlite3IsNum
6e220 62 65 72 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c  ber(const char*,
6e230 20 69 6e 74 2a 2c 20 75 38 29 3b 0a 53 51 4c 49   int*, u8);.SQLI
6e240 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6e250 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 63  qlite3Strlen30(c
6e260 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23 64 65  onst char*);.#de
6e270 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74 72 4e  fine sqlite3StrN
6e280 49 43 6d 70 20 73 71 6c 69 74 65 33 5f 73 74 72  ICmp sqlite3_str
6e290 6e 69 63 6d 70 0a 0a 53 51 4c 49 54 45 5f 50 52  nicmp..SQLITE_PR
6e2a0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6e2b0 33 4d 61 6c 6c 6f 63 49 6e 69 74 28 76 6f 69 64  3MallocInit(void
6e2c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6e2d0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61  E void sqlite3Ma
6e2e0 6c 6c 6f 63 45 6e 64 28 76 6f 69 64 29 3b 0a 53  llocEnd(void);.S
6e2f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6e300 69 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  id *sqlite3Mallo
6e310 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  c(int);.SQLITE_P
6e320 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
6e330 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 69  ite3MallocZero(i
6e340 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
6e350 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
6e360 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 71  3DbMallocZero(sq
6e370 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51  lite3*, int);.SQ
6e380 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6e390 64 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c  d *sqlite3DbMall
6e3a0 6f 63 52 61 77 28 73 71 6c 69 74 65 33 2a 2c 20  ocRaw(sqlite3*, 
6e3b0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
6e3c0 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74  VATE char *sqlit
6e3d0 65 33 44 62 53 74 72 44 75 70 28 73 71 6c 69 74  e3DbStrDup(sqlit
6e3e0 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29  e3*,const char*)
6e3f0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6e400 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 44 62   char *sqlite3Db
6e410 53 74 72 4e 44 75 70 28 73 71 6c 69 74 65 33 2a  StrNDup(sqlite3*
6e420 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e  ,const char*, in
6e430 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
6e440 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
6e450 52 65 61 6c 6c 6f 63 28 76 6f 69 64 2a 2c 20 69  Realloc(void*, i
6e460 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
6e470 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
6e480 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65  3DbReallocOrFree
6e490 28 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69 64  (sqlite3 *, void
6e4a0 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45   *, int);.SQLITE
6e4b0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
6e4c0 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28  qlite3DbRealloc(
6e4d0 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69 64 20  sqlite3 *, void 
6e4e0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
6e4f0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6e500 69 74 65 33 44 62 46 72 65 65 28 73 71 6c 69 74  ite3DbFree(sqlit
6e510 65 33 2a 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c  e3*, void*);.SQL
6e520 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6e530 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a  sqlite3MallocSiz
6e540 65 28 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45  e(void*);.SQLITE
6e550 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
6e560 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65  ite3DbMallocSize
6e570 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 2a  (sqlite3*, void*
6e580 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6e590 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 53  E void *sqlite3S
6e5a0 63 72 61 74 63 68 4d 61 6c 6c 6f 63 28 69 6e 74  cratchMalloc(int
6e5b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6e5c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 63  E void sqlite3Sc
6e5d0 72 61 74 63 68 46 72 65 65 28 76 6f 69 64 2a 29  ratchFree(void*)
6e5e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6e5f0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61   void *sqlite3Pa
6e600 67 65 4d 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53  geMalloc(int);.S
6e610 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6e620 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 46 72  id sqlite3PageFr
6e630 65 65 28 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54  ee(void*);.SQLIT
6e640 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6e650 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61  qlite3MemSetDefa
6e660 75 6c 74 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54  ult(void);.SQLIT
6e670 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6e680 71 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61 6c 6c  qlite3BenignMall
6e690 6f 63 48 6f 6f 6b 73 28 76 6f 69 64 20 28 2a 29  ocHooks(void (*)
6e6a0 28 76 6f 69 64 29 2c 20 76 6f 69 64 20 28 2a 29  (void), void (*)
6e6b0 28 76 6f 69 64 29 29 3b 0a 53 51 4c 49 54 45 5f  (void));.SQLITE_
6e6c0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6e6d0 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 76  te3MemoryAlarm(v
6e6e0 6f 69 64 20 28 2a 29 28 76 6f 69 64 2a 2c 20 73  oid (*)(void*, s
6e6f0 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 20 69 6e  qlite3_int64, in
6e700 74 29 2c 20 76 6f 69 64 2a 2c 20 73 71 6c 69 74  t), void*, sqlit
6e710 65 33 5f 69 6e 74 36 34 29 3b 0a 0a 2f 2a 0a 2a  e3_int64);../*.*
6e720 2a 20 4f 6e 20 73 79 73 74 65 6d 73 20 77 69 74  * On systems wit
6e730 68 20 61 6d 70 6c 65 20 73 74 61 63 6b 20 73 70  h ample stack sp
6e740 61 63 65 20 61 6e 64 20 74 68 61 74 20 73 75 70  ace and that sup
6e750 70 6f 72 74 20 61 6c 6c 6f 63 61 28 29 2c 20 6d  port alloca(), m
6e760 61 6b 65 0a 2a 2a 20 75 73 65 20 6f 66 20 61 6c  ake.** use of al
6e770 6c 6f 63 61 28 29 20 74 6f 20 6f 62 74 61 69 6e  loca() to obtain
6e780 20 73 70 61 63 65 20 66 6f 72 20 6c 61 72 67 65   space for large
6e790 20 61 75 74 6f 6d 61 74 69 63 20 6f 62 6a 65 63   automatic objec
6e7a0 74 73 2e 20 20 42 79 20 64 65 66 61 75 6c 74 2c  ts.  By default,
6e7b0 0a 2a 2a 20 6f 62 74 61 69 6e 20 73 70 61 63 65  .** obtain space
6e7c0 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a   from malloc()..
6e7d0 2a 2a 0a 2a 2a 20 54 68 65 20 61 6c 6c 6f 63 61  **.** The alloca
6e7e0 28 29 20 72 6f 75 74 69 6e 65 20 6e 65 76 65 72  () routine never
6e7f0 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2e 20 20   returns NULL.  
6e800 54 68 69 73 20 77 69 6c 6c 20 63 61 75 73 65 20  This will cause 
6e810 63 6f 64 65 20 70 61 74 68 73 0a 2a 2a 20 74 68  code paths.** th
6e820 61 74 20 64 65 61 6c 20 77 69 74 68 20 73 71 6c  at deal with sql
6e830 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 28 29  ite3StackAlloc()
6e840 20 66 61 69 6c 75 72 65 73 20 74 6f 20 62 65 20   failures to be 
6e850 75 6e 72 65 61 63 68 61 62 6c 65 2e 0a 2a 2f 0a  unreachable..*/.
6e860 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 55 53  #ifdef SQLITE_US
6e870 45 5f 41 4c 4c 4f 43 41 0a 23 20 64 65 66 69 6e  E_ALLOCA.# defin
6e880 65 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41 6c  e sqlite3StackAl
6e890 6c 6f 63 52 61 77 28 44 2c 4e 29 20 20 20 61 6c  locRaw(D,N)   al
6e8a0 6c 6f 63 61 28 4e 29 0a 23 20 64 65 66 69 6e 65  loca(N).# define
6e8b0 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c   sqlite3StackAll
6e8c0 6f 63 5a 65 72 6f 28 44 2c 4e 29 20 20 6d 65 6d  ocZero(D,N)  mem
6e8d0 73 65 74 28 61 6c 6c 6f 63 61 28 4e 29 2c 20 30  set(alloca(N), 0
6e8e0 2c 20 4e 29 0a 23 20 64 65 66 69 6e 65 20 73 71  , N).# define sq
6e8f0 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65 28 44  lite3StackFree(D
6e900 2c 50 29 20 20 20 20 20 20 20 0a 23 65 6c 73 65  ,P)       .#else
6e910 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
6e920 33 53 74 61 63 6b 41 6c 6c 6f 63 52 61 77 28 44  3StackAllocRaw(D
6e930 2c 4e 29 20 20 20 73 71 6c 69 74 65 33 44 62 4d  ,N)   sqlite3DbM
6e940 61 6c 6c 6f 63 52 61 77 28 44 2c 4e 29 0a 23 20  allocRaw(D,N).# 
6e950 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74  define sqlite3St
6e960 61 63 6b 41 6c 6c 6f 63 5a 65 72 6f 28 44 2c 4e  ackAllocZero(D,N
6e970 29 20 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c  )  sqlite3DbMall
6e980 6f 63 5a 65 72 6f 28 44 2c 4e 29 0a 23 20 64 65  ocZero(D,N).# de
6e990 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74 61 63  fine sqlite3Stac
6e9a0 6b 46 72 65 65 28 44 2c 50 29 20 20 20 20 20 20  kFree(D,P)      
6e9b0 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 44   sqlite3DbFree(D
6e9c0 2c 50 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64  ,P).#endif..#ifd
6e9d0 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
6e9e0 5f 4d 45 4d 53 59 53 33 0a 53 51 4c 49 54 45 5f  _MEMSYS3.SQLITE_
6e9f0 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71  PRIVATE const sq
6ea00 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64  lite3_mem_method
6ea10 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 74  s *sqlite3MemGet
6ea20 4d 65 6d 73 79 73 33 28 76 6f 69 64 29 3b 0a 23  Memsys3(void);.#
6ea30 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c  endif.#ifdef SQL
6ea40 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59  ITE_ENABLE_MEMSY
6ea50 53 35 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  S5.SQLITE_PRIVAT
6ea60 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  E const sqlite3_
6ea70 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c  mem_methods *sql
6ea80 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73  ite3MemGetMemsys
6ea90 35 28 76 6f 69 64 29 3b 0a 23 65 6e 64 69 66 0a  5(void);.#endif.
6eaa0 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
6eab0 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 53 51 4c 49  _MUTEX_OMIT.SQLI
6eac0 54 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c  TE_PRIVATE   sql
6ead0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
6eae0 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75  ds *sqlite3Defau
6eaf0 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 3b 0a 53  ltMutex(void);.S
6eb00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
6eb10 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73  sqlite3_mutex *s
6eb20 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
6eb30 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  (int);.SQLITE_PR
6eb40 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69  IVATE   int sqli
6eb50 74 65 33 4d 75 74 65 78 49 6e 69 74 28 76 6f 69  te3MutexInit(voi
6eb60 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  d);.SQLITE_PRIVA
6eb70 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33  TE   int sqlite3
6eb80 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 3b 0a  MutexEnd(void);.
6eb90 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50  #endif..SQLITE_P
6eba0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
6ebb0 65 33 53 74 61 74 75 73 56 61 6c 75 65 28 69 6e  e3StatusValue(in
6ebc0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
6ebd0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
6ebe0 74 61 74 75 73 41 64 64 28 69 6e 74 2c 20 69 6e  tatusAdd(int, in
6ebf0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
6ec00 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
6ec10 74 61 74 75 73 53 65 74 28 69 6e 74 2c 20 69 6e  tatusSet(int, in
6ec20 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  t);..SQLITE_PRIV
6ec30 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
6ec40 73 4e 61 4e 28 64 6f 75 62 6c 65 29 3b 0a 0a 53  sNaN(double);..S
6ec50 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6ec60 69 64 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e  id sqlite3VXPrin
6ec70 74 66 28 53 74 72 41 63 63 75 6d 2a 2c 20 69 6e  tf(StrAccum*, in
6ec80 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  t, const char*, 
6ec90 76 61 5f 6c 69 73 74 29 3b 0a 53 51 4c 49 54 45  va_list);.SQLITE
6eca0 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73  _PRIVATE char *s
6ecb0 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 73 71  qlite3MPrintf(sq
6ecc0 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61  lite3*,const cha
6ecd0 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45  r*, ...);.SQLITE
6ece0 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73  _PRIVATE char *s
6ecf0 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 73  qlite3VMPrintf(s
6ed00 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68  qlite3*,const ch
6ed10 61 72 2a 2c 20 76 61 5f 6c 69 73 74 29 3b 0a 53  ar*, va_list);.S
6ed20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
6ed30 61 72 20 2a 73 71 6c 69 74 65 33 4d 41 70 70 65  ar *sqlite3MAppe
6ed40 6e 64 66 28 73 71 6c 69 74 65 33 2a 2c 63 68 61  ndf(sqlite3*,cha
6ed50 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e  r*,const char*,.
6ed60 2e 2e 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64  ..);.#if defined
6ed70 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20 7c 7c  (SQLITE_TEST) ||
6ed80 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
6ed90 44 45 42 55 47 29 0a 53 51 4c 49 54 45 5f 50 52  DEBUG).SQLITE_PR
6eda0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
6edb0 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
6edc0 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e  const char*, ...
6edd0 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 64 65  );.#endif.#if de
6ede0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53  fined(SQLITE_TES
6edf0 54 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  T).SQLITE_PRIVAT
6ee00 45 20 20 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  E   void *sqlite
6ee10 33 54 65 73 74 54 65 78 74 54 6f 50 74 72 28 63  3TestTextToPtr(c
6ee20 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23 65 6e  onst char*);.#en
6ee30 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  dif.SQLITE_PRIVA
6ee40 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
6ee50 65 74 53 74 72 69 6e 67 28 63 68 61 72 20 2a 2a  etString(char **
6ee60 2c 20 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73  , sqlite3*, cons
6ee70 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53  t char*, ...);.S
6ee80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6ee90 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d  id sqlite3ErrorM
6eea0 73 67 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74  sg(Parse*, const
6eeb0 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51   char*, ...);.SQ
6eec0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6eed0 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 43 6c  d sqlite3ErrorCl
6eee0 65 61 72 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c  ear(Parse*);.SQL
6eef0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6ef00 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 63  sqlite3Dequote(c
6ef10 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  har*);.SQLITE_PR
6ef20 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6ef30 33 4b 65 79 77 6f 72 64 43 6f 64 65 28 63 6f 6e  3KeywordCode(con
6ef40 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
6ef50 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
6ef60 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6ef70 74 65 33 52 75 6e 50 61 72 73 65 72 28 50 61 72  te3RunParser(Par
6ef80 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  se*, const char*
6ef90 2c 20 63 68 61 72 20 2a 2a 29 3b 0a 53 51 4c 49  , char **);.SQLI
6efa0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6efb0 73 71 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64  sqlite3FinishCod
6efc0 69 6e 67 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c  ing(Parse*);.SQL
6efd0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6efe0 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
6eff0 67 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54  g(Parse*);.SQLIT
6f000 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6f010 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
6f020 70 52 65 67 28 50 61 72 73 65 2a 2c 69 6e 74 29  pReg(Parse*,int)
6f030 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6f040 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54   int sqlite3GetT
6f050 65 6d 70 52 61 6e 67 65 28 50 61 72 73 65 2a 2c  empRange(Parse*,
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 73 71 6c 69 74 65  VATE void sqlite
6f080 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67  3ReleaseTempRang
6f090 65 28 50 61 72 73 65 2a 2c 69 6e 74 2c 69 6e 74  e(Parse*,int,int
6f0a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6f0b0 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45  E Expr *sqlite3E
6f0c0 78 70 72 41 6c 6c 6f 63 28 73 71 6c 69 74 65 33  xprAlloc(sqlite3
6f0d0 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 54 6f 6b 65  *,int,const Toke
6f0e0 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  n*,int);.SQLITE_
6f0f0 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71  PRIVATE Expr *sq
6f100 6c 69 74 65 33 45 78 70 72 28 73 71 6c 69 74 65  lite3Expr(sqlite
6f110 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61  3*,int,const cha
6f120 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
6f130 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6f140 45 78 70 72 41 74 74 61 63 68 53 75 62 74 72 65  ExprAttachSubtre
6f150 65 73 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72  es(sqlite3*,Expr
6f160 2a 2c 45 78 70 72 2a 2c 45 78 70 72 2a 29 3b 0a  *,Expr*,Expr*);.
6f170 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45  SQLITE_PRIVATE E
6f180 78 70 72 20 2a 73 71 6c 69 74 65 33 50 45 78 70  xpr *sqlite3PExp
6f190 72 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 45  r(Parse*, int, E
6f1a0 78 70 72 2a 2c 20 45 78 70 72 2a 2c 20 63 6f 6e  xpr*, Expr*, con
6f1b0 73 74 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  st Token*);.SQLI
6f1c0 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20  TE_PRIVATE Expr 
6f1d0 2a 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28  *sqlite3ExprAnd(
6f1e0 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c 20  sqlite3*,Expr*, 
6f1f0 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Expr*);.SQLITE_P
6f200 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c  RIVATE Expr *sql
6f210 69 74 65 33 45 78 70 72 46 75 6e 63 74 69 6f 6e  ite3ExprFunction
6f220 28 50 61 72 73 65 2a 2c 45 78 70 72 4c 69 73 74  (Parse*,ExprList
6f230 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  *, Token*);.SQLI
6f240 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6f250 73 71 6c 69 74 65 33 45 78 70 72 41 73 73 69 67  sqlite3ExprAssig
6f260 6e 56 61 72 4e 75 6d 62 65 72 28 50 61 72 73 65  nVarNumber(Parse
6f270 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54  *, Expr*);.SQLIT
6f280 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6f290 71 6c 69 74 65 33 45 78 70 72 43 6c 65 61 72 28  qlite3ExprClear(
6f2a0 73 71 6c 69 74 65 33 2a 2c 20 45 78 70 72 2a 29  sqlite3*, Expr*)
6f2b0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6f2c0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
6f2d0 72 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 2a  rDelete(sqlite3*
6f2e0 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45  , Expr*);.SQLITE
6f2f0 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73  _PRIVATE ExprLis
6f300 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69  t *sqlite3ExprLi
6f310 73 74 41 70 70 65 6e 64 28 50 61 72 73 65 2a 2c  stAppend(Parse*,
6f320 45 78 70 72 4c 69 73 74 2a 2c 45 78 70 72 2a 29  ExprList*,Expr*)
6f330 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6f340 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
6f350 72 4c 69 73 74 53 65 74 4e 61 6d 65 28 50 61 72  rListSetName(Par
6f360 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 54 6f  se*,ExprList*,To
6f370 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  ken*,int);.SQLIT
6f380 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6f390 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53 65  qlite3ExprListSe
6f3a0 74 53 70 61 6e 28 50 61 72 73 65 2a 2c 45 78 70  tSpan(Parse*,Exp
6f3b0 72 4c 69 73 74 2a 2c 45 78 70 72 53 70 61 6e 2a  rList*,ExprSpan*
6f3c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6f3d0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
6f3e0 70 72 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c  prListDelete(sql
6f3f0 69 74 65 33 2a 2c 20 45 78 70 72 4c 69 73 74 2a  ite3*, ExprList*
6f400 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6f410 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 69  E int sqlite3Ini
6f420 74 28 73 71 6c 69 74 65 33 2a 2c 20 63 68 61 72  t(sqlite3*, char
6f430 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  **);.SQLITE_PRIV
6f440 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
6f450 6e 69 74 43 61 6c 6c 62 61 63 6b 28 76 6f 69 64  nitCallback(void
6f460 2a 2c 20 69 6e 74 2c 20 63 68 61 72 2a 2a 2c 20  *, int, char**, 
6f470 63 68 61 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f  char**);.SQLITE_
6f480 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6f490 69 74 65 33 50 72 61 67 6d 61 28 50 61 72 73 65  ite3Pragma(Parse
6f4a0 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c  *,Token*,Token*,
6f4b0 54 6f 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c  Token*,int);.SQL
6f4c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6f4d0 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74   sqlite3ResetInt
6f4e0 65 72 6e 61 6c 53 63 68 65 6d 61 28 73 71 6c 69  ernalSchema(sqli
6f4f0 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  te3*, int);.SQLI
6f500 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6f510 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61 72 73  sqlite3BeginPars
6f520 65 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b 0a 53  e(Parse*,int);.S
6f530 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6f540 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74  id sqlite3Commit
6f550 49 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65 73 28  InternalChanges(
6f560 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54  sqlite3*);.SQLIT
6f570 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65 20  E_PRIVATE Table 
6f580 2a 73 71 6c 69 74 65 33 52 65 73 75 6c 74 53 65  *sqlite3ResultSe
6f590 74 4f 66 53 65 6c 65 63 74 28 50 61 72 73 65 2a  tOfSelect(Parse*
6f5a0 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54  ,Select*);.SQLIT
6f5b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6f5c0 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 65 72  qlite3OpenMaster
6f5d0 54 61 62 6c 65 28 50 61 72 73 65 20 2a 2c 20 69  Table(Parse *, i
6f5e0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
6f5f0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6f600 53 74 61 72 74 54 61 62 6c 65 28 50 61 72 73 65  StartTable(Parse
6f610 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c  *,Token*,Token*,
6f620 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29  int,int,int,int)
6f630 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6f640 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64   void sqlite3Add
6f650 43 6f 6c 75 6d 6e 28 50 61 72 73 65 2a 2c 54 6f  Column(Parse*,To
6f660 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ken*);.SQLITE_PR
6f670 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6f680 65 33 41 64 64 4e 6f 74 4e 75 6c 6c 28 50 61 72  e3AddNotNull(Par
6f690 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  se*, int);.SQLIT
6f6a0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6f6b0 71 6c 69 74 65 33 41 64 64 50 72 69 6d 61 72 79  qlite3AddPrimary
6f6c0 4b 65 79 28 50 61 72 73 65 2a 2c 20 45 78 70 72  Key(Parse*, Expr
6f6d0 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c  List*, int, int,
6f6e0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
6f6f0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6f700 65 33 41 64 64 43 68 65 63 6b 43 6f 6e 73 74 72  e3AddCheckConstr
6f710 61 69 6e 74 28 50 61 72 73 65 2a 2c 20 45 78 70  aint(Parse*, Exp
6f720 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
6f730 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6f740 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28 50 61  AddColumnType(Pa
6f750 72 73 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51  rse*,Token*);.SQ
6f760 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6f770 64 20 73 71 6c 69 74 65 33 41 64 64 44 65 66 61  d sqlite3AddDefa
6f780 75 6c 74 56 61 6c 75 65 28 50 61 72 73 65 2a 2c  ultValue(Parse*,
6f790 45 78 70 72 53 70 61 6e 2a 29 3b 0a 53 51 4c 49  ExprSpan*);.SQLI
6f7a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6f7b0 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 6c 61 74  sqlite3AddCollat
6f7c0 65 54 79 70 65 28 50 61 72 73 65 2a 2c 20 54 6f  eType(Parse*, To
6f7d0 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ken*);.SQLITE_PR
6f7e0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6f7f0 65 33 45 6e 64 54 61 62 6c 65 28 50 61 72 73 65  e3EndTable(Parse
6f800 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c  *,Token*,Token*,
6f810 53 65 6c 65 63 74 2a 29 3b 0a 0a 53 51 4c 49 54  Select*);..SQLIT
6f820 45 5f 50 52 49 56 41 54 45 20 42 69 74 76 65 63  E_PRIVATE Bitvec
6f830 20 2a 73 71 6c 69 74 65 33 42 69 74 76 65 63 43   *sqlite3BitvecC
6f840 72 65 61 74 65 28 75 33 32 29 3b 0a 53 51 4c 49  reate(u32);.SQLI
6f850 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6f860 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74  qlite3BitvecTest
6f870 28 42 69 74 76 65 63 2a 2c 20 75 33 32 29 3b 0a  (Bitvec*, u32);.
6f880 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6f890 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76 65 63  nt sqlite3Bitvec
6f8a0 53 65 74 28 42 69 74 76 65 63 2a 2c 20 75 33 32  Set(Bitvec*, u32
6f8b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6f8c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 69  E void sqlite3Bi
6f8d0 74 76 65 63 43 6c 65 61 72 28 42 69 74 76 65 63  tvecClear(Bitvec
6f8e0 2a 2c 20 75 33 32 2c 20 76 6f 69 64 2a 29 3b 0a  *, u32, void*);.
6f8f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6f900 6f 69 64 20 73 71 6c 69 74 65 33 42 69 74 76 65  oid sqlite3Bitve
6f910 63 44 65 73 74 72 6f 79 28 42 69 74 76 65 63 2a  cDestroy(Bitvec*
6f920 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6f930 45 20 75 33 32 20 73 71 6c 69 74 65 33 42 69 74  E u32 sqlite3Bit
6f940 76 65 63 53 69 7a 65 28 42 69 74 76 65 63 2a 29  vecSize(Bitvec*)
6f950 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6f960 20 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76   int sqlite3Bitv
6f970 65 63 42 75 69 6c 74 69 6e 54 65 73 74 28 69 6e  ecBuiltinTest(in
6f980 74 2c 69 6e 74 2a 29 3b 0a 0a 53 51 4c 49 54 45  t,int*);..SQLITE
6f990 5f 50 52 49 56 41 54 45 20 52 6f 77 53 65 74 20  _PRIVATE RowSet 
6f9a0 2a 73 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e  *sqlite3RowSetIn
6f9b0 69 74 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69  it(sqlite3*, voi
6f9c0 64 2a 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  d*, unsigned int
6f9d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6f9e0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f  E void sqlite3Ro
6f9f0 77 53 65 74 43 6c 65 61 72 28 52 6f 77 53 65 74  wSetClear(RowSet
6fa00 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6fa10 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52  TE void sqlite3R
6fa20 6f 77 53 65 74 49 6e 73 65 72 74 28 52 6f 77 53  owSetInsert(RowS
6fa30 65 74 2a 2c 20 69 36 34 29 3b 0a 53 51 4c 49 54  et*, i64);.SQLIT
6fa40 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6fa50 6c 69 74 65 33 52 6f 77 53 65 74 54 65 73 74 28  lite3RowSetTest(
6fa60 52 6f 77 53 65 74 2a 2c 20 75 38 20 69 42 61 74  RowSet*, u8 iBat
6fa70 63 68 2c 20 69 36 34 29 3b 0a 53 51 4c 49 54 45  ch, i64);.SQLITE
6fa80 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
6fa90 69 74 65 33 52 6f 77 53 65 74 4e 65 78 74 28 52  ite3RowSetNext(R
6faa0 6f 77 53 65 74 2a 2c 20 69 36 34 2a 29 3b 0a 0a  owSet*, i64*);..
6fab0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6fac0 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65 61 74  oid sqlite3Creat
6fad0 65 56 69 65 77 28 50 61 72 73 65 2a 2c 54 6f 6b  eView(Parse*,Tok
6fae0 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e  en*,Token*,Token
6faf0 2a 2c 53 65 6c 65 63 74 2a 2c 69 6e 74 2c 69 6e  *,Select*,int,in
6fb00 74 29 3b 0a 0a 23 69 66 20 21 64 65 66 69 6e 65  t);..#if !define
6fb10 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  d(SQLITE_OMIT_VI
6fb20 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28  EW) || !defined(
6fb30 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
6fb40 55 41 4c 54 41 42 4c 45 29 0a 53 51 4c 49 54 45  UALTABLE).SQLITE
6fb50 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73  _PRIVATE   int s
6fb60 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c  qlite3ViewGetCol
6fb70 75 6d 6e 4e 61 6d 65 73 28 50 61 72 73 65 2a 2c  umnNames(Parse*,
6fb80 54 61 62 6c 65 2a 29 3b 0a 23 65 6c 73 65 0a 23  Table*);.#else.#
6fb90 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56   define sqlite3V
6fba0 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65  iewGetColumnName
6fbb0 73 28 41 2c 42 29 20 30 0a 23 65 6e 64 69 66 0a  s(A,B) 0.#endif.
6fbc0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6fbd0 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70  void sqlite3Drop
6fbe0 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 20 53 72  Table(Parse*, Sr
6fbf0 63 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74  cList*, int, int
6fc00 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6fc10 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65  E void sqlite3De
6fc20 6c 65 74 65 54 61 62 6c 65 28 54 61 62 6c 65 2a  leteTable(Table*
6fc30 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
6fc40 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45  E_OMIT_AUTOINCRE
6fc50 4d 45 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49 56  MENT.SQLITE_PRIV
6fc60 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
6fc70 65 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 42  e3AutoincrementB
6fc80 65 67 69 6e 28 50 61 72 73 65 20 2a 70 50 61 72  egin(Parse *pPar
6fc90 73 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  se);.SQLITE_PRIV
6fca0 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
6fcb0 65 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 45  e3AutoincrementE
6fcc0 6e 64 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  nd(Parse *pParse
6fcd0 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  );.#else.# defin
6fce0 65 20 73 71 6c 69 74 65 33 41 75 74 6f 69 6e 63  e sqlite3Autoinc
6fcf0 72 65 6d 65 6e 74 42 65 67 69 6e 28 58 29 0a 23  rementBegin(X).#
6fd00 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41   define sqlite3A
6fd10 75 74 6f 69 6e 63 72 65 6d 65 6e 74 45 6e 64 28  utoincrementEnd(
6fd20 58 29 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45  X).#endif.SQLITE
6fd30 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6fd40 6c 69 74 65 33 49 6e 73 65 72 74 28 50 61 72 73  lite3Insert(Pars
6fd50 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78  e*, SrcList*, Ex
6fd60 70 72 4c 69 73 74 2a 2c 20 53 65 6c 65 63 74 2a  prList*, Select*
6fd70 2c 20 49 64 4c 69 73 74 2a 2c 20 69 6e 74 29 3b  , IdList*, int);
6fd80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6fd90 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 41 72 72  void *sqlite3Arr
6fda0 61 79 41 6c 6c 6f 63 61 74 65 28 73 71 6c 69 74  ayAllocate(sqlit
6fdb0 65 33 2a 2c 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e  e3*,void*,int,in
6fdc0 74 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a  t,int*,int*,int*
6fdd0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6fde0 45 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65  E IdList *sqlite
6fdf0 33 49 64 4c 69 73 74 41 70 70 65 6e 64 28 73 71  3IdListAppend(sq
6fe00 6c 69 74 65 33 2a 2c 20 49 64 4c 69 73 74 2a 2c  lite3*, IdList*,
6fe10 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45   Token*);.SQLITE
6fe20 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
6fe30 69 74 65 33 49 64 4c 69 73 74 49 6e 64 65 78 28  ite3IdListIndex(
6fe40 49 64 4c 69 73 74 2a 2c 63 6f 6e 73 74 20 63 68  IdList*,const ch
6fe50 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ar*);.SQLITE_PRI
6fe60 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71  VATE SrcList *sq
6fe70 6c 69 74 65 33 53 72 63 4c 69 73 74 45 6e 6c 61  lite3SrcListEnla
6fe80 72 67 65 28 73 71 6c 69 74 65 33 2a 2c 20 53 72  rge(sqlite3*, Sr
6fe90 63 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74  cList*, int, int
6fea0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6feb0 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74  E SrcList *sqlit
6fec0 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28  e3SrcListAppend(
6fed0 73 71 6c 69 74 65 33 2a 2c 20 53 72 63 4c 69 73  sqlite3*, SrcLis
6fee0 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65  t*, Token*, Toke
6fef0 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  n*);.SQLITE_PRIV
6ff00 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c  ATE SrcList *sql
6ff10 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e  ite3SrcListAppen
6ff20 64 46 72 6f 6d 54 65 72 6d 28 50 61 72 73 65 2a  dFromTerm(Parse*
6ff30 2c 20 53 72 63 4c 69 73 74 2a 2c 20 54 6f 6b 65  , SrcList*, Toke
6ff40 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 0a 20 20 20 20  n*, Token*,.    
6ff50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6ff60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6ff70 20 20 54 6f 6b 65 6e 2a 2c 20 53 65 6c 65 63 74    Token*, Select
6ff80 2a 2c 20 45 78 70 72 2a 2c 20 49 64 4c 69 73 74  *, Expr*, IdList
6ff90 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6ffa0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
6ffb0 72 63 4c 69 73 74 49 6e 64 65 78 65 64 42 79 28  rcListIndexedBy(
6ffc0 50 61 72 73 65 20 2a 2c 20 53 72 63 4c 69 73 74  Parse *, SrcList
6ffd0 20 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51   *, Token *);.SQ
6ffe0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
6fff0 20 73 71 6c 69 74 65 33 49 6e 64 65 78 65 64 42   sqlite3IndexedB
70000 79 4c 6f 6f 6b 75 70 28 50 61 72 73 65 20 2a 2c  yLookup(Parse *,
70010 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
70020 69 74 65 6d 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  item *);.SQLITE_
70030 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
70040 69 74 65 33 53 72 63 4c 69 73 74 53 68 69 66 74  ite3SrcListShift
70050 4a 6f 69 6e 54 79 70 65 28 53 72 63 4c 69 73 74  JoinType(SrcList
70060 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
70070 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
70080 72 63 4c 69 73 74 41 73 73 69 67 6e 43 75 72 73  rcListAssignCurs
70090 6f 72 73 28 50 61 72 73 65 2a 2c 20 53 72 63 4c  ors(Parse*, SrcL
700a0 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ist*);.SQLITE_PR
700b0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
700c0 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 73  e3IdListDelete(s
700d0 71 6c 69 74 65 33 2a 2c 20 49 64 4c 69 73 74 2a  qlite3*, IdList*
700e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
700f0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72  E void sqlite3Sr
70100 63 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69  cListDelete(sqli
70110 74 65 33 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b  te3*, SrcList*);
70120 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
70130 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65 61  void sqlite3Crea
70140 74 65 49 6e 64 65 78 28 50 61 72 73 65 2a 2c 54  teIndex(Parse*,T
70150 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 72 63  oken*,Token*,Src
70160 4c 69 73 74 2a 2c 45 78 70 72 4c 69 73 74 2a 2c  List*,ExprList*,
70170 69 6e 74 2c 54 6f 6b 65 6e 2a 2c 0a 20 20 20 20  int,Token*,.    
70180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
70190 20 20 20 20 54 6f 6b 65 6e 2a 2c 20 69 6e 74 2c      Token*, int,
701a0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
701b0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
701c0 65 33 44 72 6f 70 49 6e 64 65 78 28 50 61 72 73  e3DropIndex(Pars
701d0 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e  e*, SrcList*, in
701e0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
701f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65  TE int sqlite3Se
70200 6c 65 63 74 28 50 61 72 73 65 2a 2c 20 53 65 6c  lect(Parse*, Sel
70210 65 63 74 2a 2c 20 53 65 6c 65 63 74 44 65 73 74  ect*, SelectDest
70220 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
70230 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74  TE Select *sqlit
70240 65 33 53 65 6c 65 63 74 4e 65 77 28 50 61 72 73  e3SelectNew(Pars
70250 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 53 72 63  e*,ExprList*,Src
70260 4c 69 73 74 2a 2c 45 78 70 72 2a 2c 45 78 70 72  List*,Expr*,Expr
70270 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20 20 20 20  List*,.         
70280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
70290 45 78 70 72 2a 2c 45 78 70 72 4c 69 73 74 2a 2c  Expr*,ExprList*,
702a0 69 6e 74 2c 45 78 70 72 2a 2c 45 78 70 72 2a 29  int,Expr*,Expr*)
702b0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
702c0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c   void sqlite3Sel
702d0 65 63 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65  ectDelete(sqlite
702e0 33 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51  3*, Select*);.SQ
702f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 62  LITE_PRIVATE Tab
70300 6c 65 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69  le *sqlite3SrcLi
70310 73 74 4c 6f 6f 6b 75 70 28 50 61 72 73 65 2a 2c  stLookup(Parse*,
70320 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49   SrcList*);.SQLI
70330 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
70340 71 6c 69 74 65 33 49 73 52 65 61 64 4f 6e 6c 79  qlite3IsReadOnly
70350 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c  (Parse*, Table*,
70360 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
70370 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
70380 65 33 4f 70 65 6e 54 61 62 6c 65 28 50 61 72 73  e3OpenTable(Pars
70390 65 2a 2c 20 69 6e 74 20 69 43 75 72 2c 20 69 6e  e*, int iCur, in
703a0 74 20 69 44 62 2c 20 54 61 62 6c 65 2a 2c 20 69  t iDb, Table*, i
703b0 6e 74 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64  nt);.#if defined
703c0 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55  (SQLITE_ENABLE_U
703d0 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d  PDATE_DELETE_LIM
703e0 49 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  IT) && !defined(
703f0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
70400 55 45 52 59 29 0a 53 51 4c 49 54 45 5f 50 52 49  UERY).SQLITE_PRI
70410 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74  VATE Expr *sqlit
70420 65 33 4c 69 6d 69 74 57 68 65 72 65 28 50 61 72  e3LimitWhere(Par
70430 73 65 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a 2c  se *, SrcList *,
70440 20 45 78 70 72 20 2a 2c 20 45 78 70 72 4c 69 73   Expr *, ExprLis
70450 74 20 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 70  t *, Expr *, Exp
70460 72 20 2a 2c 20 63 68 61 72 20 2a 29 3b 0a 23 65  r *, char *);.#e
70470 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56  ndif.SQLITE_PRIV
70480 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
70490 44 65 6c 65 74 65 46 72 6f 6d 28 50 61 72 73 65  DeleteFrom(Parse
704a0 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70  *, SrcList*, Exp
704b0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
704c0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
704d0 55 70 64 61 74 65 28 50 61 72 73 65 2a 2c 20 53  Update(Parse*, S
704e0 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 4c 69 73  rcList*, ExprLis
704f0 74 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b  t*, Expr*, int);
70500 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
70510 57 68 65 72 65 49 6e 66 6f 20 2a 73 71 6c 69 74  WhereInfo *sqlit
70520 65 33 57 68 65 72 65 42 65 67 69 6e 28 50 61 72  e3WhereBegin(Par
70530 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45  se*, SrcList*, E
70540 78 70 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2a  xpr*, ExprList**
70550 2c 20 75 31 36 29 3b 0a 53 51 4c 49 54 45 5f 50  , u16);.SQLITE_P
70560 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
70570 74 65 33 57 68 65 72 65 45 6e 64 28 57 68 65 72  te3WhereEnd(Wher
70580 65 49 6e 66 6f 2a 29 3b 0a 53 51 4c 49 54 45 5f  eInfo*);.SQLITE_
70590 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
705a0 74 65 33 45 78 70 72 43 6f 64 65 47 65 74 43 6f  te3ExprCodeGetCo
705b0 6c 75 6d 6e 28 50 61 72 73 65 2a 2c 20 54 61 62  lumn(Parse*, Tab
705c0 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69  le*, int, int, i
705d0 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
705e0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
705f0 6c 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76  lite3ExprCodeMov
70600 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69  e(Parse*, int, i
70610 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
70620 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
70630 6c 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f 70  lite3ExprCodeCop
70640 79 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69  y(Parse*, int, i
70650 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
70660 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
70670 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 53 74  lite3ExprCacheSt
70680 6f 72 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c  ore(Parse*, int,
70690 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49   int, int);.SQLI
706a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
706b0 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
706c0 50 75 73 68 28 50 61 72 73 65 2a 29 3b 0a 53 51  Push(Parse*);.SQ
706d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
706e0 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63  d sqlite3ExprCac
706f0 68 65 50 6f 70 28 50 61 72 73 65 2a 2c 20 69 6e  hePop(Parse*, in
70700 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
70710 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
70720 78 70 72 43 61 63 68 65 52 65 6d 6f 76 65 28 50  xprCacheRemove(P
70730 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  arse*, int);.SQL
70740 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
70750 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
70760 65 43 6c 65 61 72 28 50 61 72 73 65 2a 29 3b 0a  eClear(Parse*);.
70770 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
70780 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43  oid sqlite3ExprC
70790 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e  acheAffinityChan
707a0 67 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20  ge(Parse*, int, 
707b0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
707c0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
707d0 33 45 78 70 72 48 61 72 64 43 6f 70 79 28 50 61  3ExprHardCopy(Pa
707e0 72 73 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53  rse*,int,int);.S
707f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
70800 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  t sqlite3ExprCod
70810 65 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c  e(Parse*, Expr*,
70820 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
70830 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
70840 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 50 61  3ExprCodeTemp(Pa
70850 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74  rse*, Expr*, int
70860 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
70870 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78  TE int sqlite3Ex
70880 70 72 43 6f 64 65 54 61 72 67 65 74 28 50 61 72  prCodeTarget(Par
70890 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29  se*, Expr*, int)
708a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
708b0 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72   int sqlite3Expr
708c0 43 6f 64 65 41 6e 64 43 61 63 68 65 28 50 61 72  CodeAndCache(Par
708d0 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29  se*, Expr*, int)
708e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
708f0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
70900 72 43 6f 64 65 43 6f 6e 73 74 61 6e 74 73 28 50  rCodeConstants(P
70910 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53  arse*, Expr*);.S
70920 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
70930 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  t sqlite3ExprCod
70940 65 45 78 70 72 4c 69 73 74 28 50 61 72 73 65 2a  eExprList(Parse*
70950 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74  , ExprList*, int
70960 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
70970 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
70980 74 65 33 45 78 70 72 49 66 54 72 75 65 28 50 61  te3ExprIfTrue(Pa
70990 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74  rse*, Expr*, int
709a0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
709b0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
709c0 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 50  te3ExprIfFalse(P
709d0 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e  arse*, Expr*, in
709e0 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
709f0 50 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73  PRIVATE Table *s
70a00 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28  qlite3FindTable(
70a10 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63  sqlite3*,const c
70a20 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  har*, const char
70a30 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
70a40 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65  TE Table *sqlite
70a50 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 50 61 72  3LocateTable(Par
70a60 73 65 2a 2c 69 6e 74 20 69 73 56 69 65 77 2c 63  se*,int isView,c
70a70 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73  onst char*, cons
70a80 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45  t char*);.SQLITE
70a90 5f 50 52 49 56 41 54 45 20 49 6e 64 65 78 20 2a  _PRIVATE Index *
70aa0 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78  sqlite3FindIndex
70ab0 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
70ac0 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61  char*, const cha
70ad0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
70ae0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
70af0 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54  UnlinkAndDeleteT
70b00 61 62 6c 65 28 73 71 6c 69 74 65 33 2a 2c 69 6e  able(sqlite3*,in
70b10 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a  t,const char*);.
70b20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
70b30 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e  oid sqlite3Unlin
70b40 6b 41 6e 64 44 65 6c 65 74 65 49 6e 64 65 78 28  kAndDeleteIndex(
70b50 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e  sqlite3*,int,con
70b60 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54  st char*);.SQLIT
70b70 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
70b80 71 6c 69 74 65 33 56 61 63 75 75 6d 28 50 61 72  qlite3Vacuum(Par
70b90 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  se*);.SQLITE_PRI
70ba0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
70bb0 52 75 6e 56 61 63 75 75 6d 28 63 68 61 72 2a 2a  RunVacuum(char**
70bc0 2c 20 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c  , sqlite3*);.SQL
70bd0 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
70be0 20 2a 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f   *sqlite3NameFro
70bf0 6d 54 6f 6b 65 6e 28 73 71 6c 69 74 65 33 2a 2c  mToken(sqlite3*,
70c00 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45   Token*);.SQLITE
70c10 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
70c20 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28  ite3ExprCompare(
70c30 45 78 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53  Expr*, Expr*);.S
70c40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
70c50 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e  id sqlite3ExprAn
70c60 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73 28  alyzeAggregates(
70c70 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 20 45 78  NameContext*, Ex
70c80 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  pr*);.SQLITE_PRI
70c90 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
70ca0 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c  3ExprAnalyzeAggL
70cb0 69 73 74 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a  ist(NameContext*
70cc0 2c 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c  ,ExprList*);.SQL
70cd0 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 65  ITE_PRIVATE Vdbe
70ce0 20 2a 73 71 6c 69 74 65 33 47 65 74 56 64 62 65   *sqlite3GetVdbe
70cf0 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45  (Parse*);.SQLITE
70d00 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73  _PRIVATE Expr *s
70d10 71 6c 69 74 65 33 43 72 65 61 74 65 49 64 45 78  qlite3CreateIdEx
70d20 70 72 28 50 61 72 73 65 20 2a 2c 20 63 6f 6e 73  pr(Parse *, cons
70d30 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45  t char*);.SQLITE
70d40 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
70d50 6c 69 74 65 33 50 72 6e 67 53 61 76 65 53 74 61  lite3PrngSaveSta
70d60 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45  te(void);.SQLITE
70d70 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
70d80 6c 69 74 65 33 50 72 6e 67 52 65 73 74 6f 72 65  lite3PrngRestore
70d90 53 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c  State(void);.SQL
70da0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
70db0 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73 65   sqlite3PrngRese
70dc0 74 53 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51  tState(void);.SQ
70dd0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
70de0 64 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63  d sqlite3Rollbac
70df0 6b 41 6c 6c 28 73 71 6c 69 74 65 33 2a 29 3b 0a  kAll(sqlite3*);.
70e00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
70e10 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 56  oid sqlite3CodeV
70e20 65 72 69 66 79 53 63 68 65 6d 61 28 50 61 72 73  erifySchema(Pars
70e30 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  e*, int);.SQLITE
70e40 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
70e50 6c 69 74 65 33 42 65 67 69 6e 54 72 61 6e 73 61  lite3BeginTransa
70e60 63 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 69 6e  ction(Parse*, in
70e70 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
70e80 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43  TE void sqlite3C
70e90 6f 6d 6d 69 74 54 72 61 6e 73 61 63 74 69 6f 6e  ommitTransaction
70ea0 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45  (Parse*);.SQLITE
70eb0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
70ec0 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54 72 61  lite3RollbackTra
70ed0 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65 2a 29  nsaction(Parse*)
70ee0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
70ef0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 61 76   void sqlite3Sav
70f00 65 70 6f 69 6e 74 28 50 61 72 73 65 2a 2c 20 69  epoint(Parse*, i
70f10 6e 74 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c  nt, Token*);.SQL
70f20 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
70f30 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76   sqlite3CloseSav
70f40 65 70 6f 69 6e 74 73 28 73 71 6c 69 74 65 33 20  epoints(sqlite3 
70f50 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
70f60 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78  TE int sqlite3Ex
70f70 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 45 78 70  prIsConstant(Exp
70f80 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
70f90 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45  ATE int sqlite3E
70fa0 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74  xprIsConstantNot
70fb0 4a 6f 69 6e 28 45 78 70 72 2a 29 3b 0a 53 51 4c  Join(Expr*);.SQL
70fc0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
70fd0 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e  sqlite3ExprIsCon
70fe0 73 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28  stantOrFunction(
70ff0 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Expr*);.SQLITE_P
71000 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
71010 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28  e3ExprIsInteger(
71020 45 78 70 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51  Expr*, int*);.SQ
71030 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
71040 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28   sqlite3IsRowid(
71050 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51  const char*);.SQ
71060 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
71070 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74  d sqlite3Generat
71080 65 52 6f 77 44 65 6c 65 74 65 28 50 61 72 73 65  eRowDelete(Parse
71090 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20  *, Table*, int, 
710a0 69 6e 74 2c 20 69 6e 74 2c 20 54 72 69 67 67 65  int, int, Trigge
710b0 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  r *, int);.SQLIT
710c0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
710d0 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f  qlite3GenerateRo
710e0 77 49 6e 64 65 78 44 65 6c 65 74 65 28 50 61 72  wIndexDelete(Par
710f0 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74  se*, Table*, int
71100 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  , int*);.SQLITE_
71110 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
71120 74 65 33 47 65 6e 65 72 61 74 65 49 6e 64 65 78  te3GenerateIndex
71130 4b 65 79 28 50 61 72 73 65 2a 2c 20 49 6e 64 65  Key(Parse*, Inde
71140 78 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e  x*, int, int, in
71150 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
71160 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47  TE void sqlite3G
71170 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e  enerateConstrain
71180 74 43 68 65 63 6b 73 28 50 61 72 73 65 2a 2c 54  tChecks(Parse*,T
71190 61 62 6c 65 2a 2c 69 6e 74 2c 69 6e 74 2c 0a 20  able*,int,int,. 
711a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
711b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
711c0 20 20 20 20 69 6e 74 2a 2c 69 6e 74 2c 69 6e 74      int*,int,int
711d0 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2a 29 3b 0a  ,int,int,int*);.
711e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
711f0 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 70 6c  oid sqlite3Compl
71200 65 74 65 49 6e 73 65 72 74 69 6f 6e 28 50 61 72  eteInsertion(Par
71210 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74  se*, Table*, int
71220 2c 20 69 6e 74 2c 20 69 6e 74 2a 2c 20 69 6e 74  , int, int*, int
71230 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c  , int, int);.SQL
71240 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
71250 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65  sqlite3OpenTable
71260 41 6e 64 49 6e 64 69 63 65 73 28 50 61 72 73 65  AndIndices(Parse
71270 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20  *, Table*, int, 
71280 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
71290 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
712a0 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61  3BeginWriteOpera
712b0 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 69 6e 74  tion(Parse*, int
712c0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
712d0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
712e0 74 65 33 4d 61 79 41 62 6f 72 74 28 50 61 72 73  te3MayAbort(Pars
712f0 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  e *);.SQLITE_PRI
71300 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
71310 33 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e 74 28  3HaltConstraint(
71320 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 63 68 61  Parse*, int, cha
71330 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  r*, int);.SQLITE
71340 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73  _PRIVATE Expr *s
71350 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 73 71  qlite3ExprDup(sq
71360 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c 69 6e 74  lite3*,Expr*,int
71370 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
71380 45 20 45 78 70 72 4c 69 73 74 20 2a 73 71 6c 69  E ExprList *sqli
71390 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 73  te3ExprListDup(s
713a0 71 6c 69 74 65 33 2a 2c 45 78 70 72 4c 69 73 74  qlite3*,ExprList
713b0 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  *,int);.SQLITE_P
713c0 52 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a  RIVATE SrcList *
713d0 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75  sqlite3SrcListDu
713e0 70 28 73 71 6c 69 74 65 33 2a 2c 53 72 63 4c 69  p(sqlite3*,SrcLi
713f0 73 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  st*,int);.SQLITE
71400 5f 50 52 49 56 41 54 45 20 49 64 4c 69 73 74 20  _PRIVATE IdList 
71410 2a 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 75  *sqlite3IdListDu
71420 70 28 73 71 6c 69 74 65 33 2a 2c 49 64 4c 69 73  p(sqlite3*,IdLis
71430 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
71440 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69  ATE Select *sqli
71450 74 65 33 53 65 6c 65 63 74 44 75 70 28 73 71 6c  te3SelectDup(sql
71460 69 74 65 33 2a 2c 53 65 6c 65 63 74 2a 2c 69 6e  ite3*,Select*,in
71470 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
71480 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46  TE void sqlite3F
71490 75 6e 63 44 65 66 49 6e 73 65 72 74 28 46 75 6e  uncDefInsert(Fun
714a0 63 44 65 66 48 61 73 68 2a 2c 20 46 75 6e 63 44  cDefHash*, FuncD
714b0 65 66 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ef*);.SQLITE_PRI
714c0 56 41 54 45 20 46 75 6e 63 44 65 66 20 2a 73 71  VATE FuncDef *sq
714d0 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f  lite3FindFunctio
714e0 6e 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74  n(sqlite3*,const
714f0 20 63 68 61 72 2a 2c 69 6e 74 2c 69 6e 74 2c 75   char*,int,int,u
71500 38 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  8,int);.SQLITE_P
71510 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
71520 74 65 33 52 65 67 69 73 74 65 72 42 75 69 6c 74  te3RegisterBuilt
71530 69 6e 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69  inFunctions(sqli
71540 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  te3*);.SQLITE_PR
71550 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
71560 65 33 52 65 67 69 73 74 65 72 44 61 74 65 54 69  e3RegisterDateTi
71570 6d 65 46 75 6e 63 74 69 6f 6e 73 28 76 6f 69 64  meFunctions(void
71580 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
71590 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65  E void sqlite3Re
715a0 67 69 73 74 65 72 47 6c 6f 62 61 6c 46 75 6e 63  gisterGlobalFunc
715b0 74 69 6f 6e 73 28 76 6f 69 64 29 3b 0a 23 69 66  tions(void);.#if
715c0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
715d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
715e0 20 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66    int sqlite3Saf
715f0 65 74 79 4f 6e 28 73 71 6c 69 74 65 33 2a 29 3b  etyOn(sqlite3*);
71600 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
71610 20 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66    int sqlite3Saf
71620 65 74 79 4f 66 66 28 73 71 6c 69 74 65 33 2a 29  etyOff(sqlite3*)
71630 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  ;.#else.# define
71640 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e   sqlite3SafetyOn
71650 28 41 29 20 30 0a 23 20 64 65 66 69 6e 65 20 73  (A) 0.# define s
71660 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
71670 41 29 20 30 0a 23 65 6e 64 69 66 0a 53 51 4c 49  A) 0.#endif.SQLI
71680 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
71690 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63  qlite3SafetyChec
716a0 6b 4f 6b 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53  kOk(sqlite3*);.S
716b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
716c0 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43  t sqlite3SafetyC
716d0 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 73 71 6c  heckSickOrOk(sql
716e0 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ite3*);.SQLITE_P
716f0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
71700 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28  te3ChangeCookie(
71710 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 0a 23  Parse*, int);..#
71720 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
71730 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 26 26  TE_OMIT_VIEW) &&
71740 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
71750 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a 53  _OMIT_TRIGGER).S
71760 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
71770 69 64 20 73 71 6c 69 74 65 33 4d 61 74 65 72 69  id sqlite3Materi
71780 61 6c 69 7a 65 56 69 65 77 28 50 61 72 73 65 2a  alizeView(Parse*
71790 2c 20 54 61 62 6c 65 2a 2c 20 45 78 70 72 2a 2c  , Table*, Expr*,
717a0 20 69 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 0a 23   int);.#endif..#
717b0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
717c0 49 54 5f 54 52 49 47 47 45 52 0a 53 51 4c 49 54  IT_TRIGGER.SQLIT
717d0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
717e0 20 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72 69   sqlite3BeginTri
717f0 67 67 65 72 28 50 61 72 73 65 2a 2c 20 54 6f 6b  gger(Parse*, Tok
71800 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 2c 69  en*,Token*,int,i
71810 6e 74 2c 49 64 4c 69 73 74 2a 2c 53 72 63 4c 69  nt,IdList*,SrcLi
71820 73 74 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20  st*,.           
71830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71840 45 78 70 72 2a 2c 69 6e 74 2c 20 69 6e 74 29 3b  Expr*,int, int);
71850 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
71860 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 69    void sqlite3Fi
71870 6e 69 73 68 54 72 69 67 67 65 72 28 50 61 72 73  nishTrigger(Pars
71880 65 2a 2c 20 54 72 69 67 67 65 72 53 74 65 70 2a  e*, TriggerStep*
71890 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54  , Token*);.SQLIT
718a0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
718b0 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67   sqlite3DropTrig
718c0 67 65 72 28 50 61 72 73 65 2a 2c 20 53 72 63 4c  ger(Parse*, SrcL
718d0 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  ist*, int);.SQLI
718e0 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
718f0 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69  d sqlite3DropTri
71900 67 67 65 72 50 74 72 28 50 61 72 73 65 2a 2c 20  ggerPtr(Parse*, 
71910 54 72 69 67 67 65 72 2a 29 3b 0a 53 51 4c 49 54  Trigger*);.SQLIT
71920 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67  E_PRIVATE   Trig
71930 67 65 72 20 2a 73 71 6c 69 74 65 33 54 72 69 67  ger *sqlite3Trig
71940 67 65 72 73 45 78 69 73 74 28 50 61 72 73 65 20  gersExist(Parse 
71950 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20  *, Table*, int, 
71960 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 20 2a  ExprList*, int *
71970 70 4d 61 73 6b 29 3b 0a 53 51 4c 49 54 45 5f 50  pMask);.SQLITE_P
71980 52 49 56 41 54 45 20 20 20 54 72 69 67 67 65 72  RIVATE   Trigger
71990 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72   *sqlite3Trigger
719a0 4c 69 73 74 28 50 61 72 73 65 20 2a 2c 20 54 61  List(Parse *, Ta
719b0 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ble *);.SQLITE_P
719c0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
719d0 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67  lite3CodeRowTrig
719e0 67 65 72 28 50 61 72 73 65 2a 2c 20 54 72 69 67  ger(Parse*, Trig
719f0 67 65 72 20 2a 2c 20 69 6e 74 2c 20 45 78 70 72  ger *, int, Expr
71a00 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 54 61 62 6c  List*, int, Tabl
71a10 65 20 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20  e *,.           
71a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71a30 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 2c 20   int, int, int, 
71a40 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 73 71 6c  int);.  void sql
71a50 69 74 65 56 69 65 77 54 72 69 67 67 65 72 73 28  iteViewTriggers(
71a60 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20  Parse*, Table*, 
71a70 45 78 70 72 2a 2c 20 69 6e 74 2c 20 45 78 70 72  Expr*, int, Expr
71a80 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  List*);.SQLITE_P
71a90 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
71aa0 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67  lite3DeleteTrigg
71ab0 65 72 53 74 65 70 28 73 71 6c 69 74 65 33 2a 2c  erStep(sqlite3*,
71ac0 20 54 72 69 67 67 65 72 53 74 65 70 2a 29 3b 0a   TriggerStep*);.
71ad0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
71ae0 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 71   TriggerStep *sq
71af0 6c 69 74 65 33 54 72 69 67 67 65 72 53 65 6c 65  lite3TriggerSele
71b00 63 74 53 74 65 70 28 73 71 6c 69 74 65 33 2a 2c  ctStep(sqlite3*,
71b10 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45  Select*);.SQLITE
71b20 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67  _PRIVATE   Trigg
71b30 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54  erStep *sqlite3T
71b40 72 69 67 67 65 72 49 6e 73 65 72 74 53 74 65 70  riggerInsertStep
71b50 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a  (sqlite3*,Token*
71b60 2c 20 49 64 4c 69 73 74 2a 2c 0a 20 20 20 20 20  , IdList*,.     
71b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71b90 20 20 20 45 78 70 72 4c 69 73 74 2a 2c 53 65 6c     ExprList*,Sel
71ba0 65 63 74 2a 2c 75 38 29 3b 0a 53 51 4c 49 54 45  ect*,u8);.SQLITE
71bb0 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67  _PRIVATE   Trigg
71bc0 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54  erStep *sqlite3T
71bd0 72 69 67 67 65 72 55 70 64 61 74 65 53 74 65 70  riggerUpdateStep
71be0 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a  (sqlite3*,Token*
71bf0 2c 45 78 70 72 4c 69 73 74 2a 2c 20 45 78 70 72  ,ExprList*, Expr
71c00 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50  *, u8);.SQLITE_P
71c10 52 49 56 41 54 45 20 20 20 54 72 69 67 67 65 72  RIVATE   Trigger
71c20 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72 69  Step *sqlite3Tri
71c30 67 67 65 72 44 65 6c 65 74 65 53 74 65 70 28 73  ggerDeleteStep(s
71c40 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c 20  qlite3*,Token*, 
71c50 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Expr*);.SQLITE_P
71c60 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
71c70 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67  lite3DeleteTrigg
71c80 65 72 28 73 71 6c 69 74 65 33 2a 2c 20 54 72 69  er(sqlite3*, Tri
71c90 67 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  gger*);.SQLITE_P
71ca0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
71cb0 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65  lite3UnlinkAndDe
71cc0 6c 65 74 65 54 72 69 67 67 65 72 28 73 71 6c 69  leteTrigger(sqli
71cd0 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63  te3*,int,const c
71ce0 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  har*);.SQLITE_PR
71cf0 49 56 41 54 45 20 20 20 75 33 32 20 73 71 6c 69  IVATE   u32 sqli
71d00 74 65 33 54 72 69 67 67 65 72 4f 6c 64 6d 61 73  te3TriggerOldmas
71d10 6b 28 50 61 72 73 65 2a 2c 54 72 69 67 67 65 72  k(Parse*,Trigger
71d20 2a 2c 69 6e 74 2c 45 78 70 72 4c 69 73 74 2a 2c  *,int,ExprList*,
71d30 54 61 62 6c 65 2a 2c 69 6e 74 29 3b 0a 23 20 64  Table*,int);.# d
71d40 65 66 69 6e 65 20 73 71 6c 69 74 65 33 50 61 72  efine sqlite3Par
71d50 73 65 54 6f 70 6c 65 76 65 6c 28 70 29 20 28 28  seToplevel(p) ((
71d60 70 29 2d 3e 70 54 6f 70 6c 65 76 65 6c 20 3f 20  p)->pToplevel ? 
71d70 28 70 29 2d 3e 70 54 6f 70 6c 65 76 65 6c 20 3a  (p)->pToplevel :
71d80 20 28 70 29 29 0a 23 65 6c 73 65 0a 23 20 64 65   (p)).#else.# de
71d90 66 69 6e 65 20 73 71 6c 69 74 65 33 54 72 69 67  fine sqlite3Trig
71da0 67 65 72 73 45 78 69 73 74 28 42 2c 43 2c 44 2c  gersExist(B,C,D,
71db0 45 2c 46 29 20 30 0a 23 20 64 65 66 69 6e 65 20  E,F) 0.# define 
71dc0 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69  sqlite3DeleteTri
71dd0 67 67 65 72 28 41 2c 42 29 0a 23 20 64 65 66 69  gger(A,B).# defi
71de0 6e 65 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72  ne sqlite3DropTr
71df0 69 67 67 65 72 50 74 72 28 41 2c 42 29 0a 23 20  iggerPtr(A,B).# 
71e00 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 55 6e  define sqlite3Un
71e10 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 72 69  linkAndDeleteTri
71e20 67 67 65 72 28 41 2c 42 2c 43 29 0a 23 20 64 65  gger(A,B,C).# de
71e30 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6f 64 65  fine sqlite3Code
71e40 52 6f 77 54 72 69 67 67 65 72 28 41 2c 42 2c 43  RowTrigger(A,B,C
71e50 2c 44 2c 45 2c 46 2c 47 2c 48 2c 49 2c 4a 29 0a  ,D,E,F,G,H,I,J).
71e60 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
71e70 54 72 69 67 67 65 72 4c 69 73 74 28 58 2c 20 59  TriggerList(X, Y
71e80 29 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  ) 0.# define sql
71e90 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65  ite3ParseTopleve
71ea0 6c 28 70 29 20 70 0a 23 20 64 65 66 69 6e 65 20  l(p) p.# define 
71eb0 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 4f 6c  sqlite3TriggerOl
71ec0 64 6d 61 73 6b 28 41 2c 42 2c 43 2c 44 2c 45 2c  dmask(A,B,C,D,E,
71ed0 46 29 20 30 0a 23 65 6e 64 69 66 0a 0a 53 51 4c  F) 0.#endif..SQL
71ee0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
71ef0 73 71 6c 69 74 65 33 4a 6f 69 6e 54 79 70 65 28  sqlite3JoinType(
71f00 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 20  Parse*, Token*, 
71f10 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b  Token*, Token*);
71f20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
71f30 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65 61  void sqlite3Crea
71f40 74 65 46 6f 72 65 69 67 6e 4b 65 79 28 50 61 72  teForeignKey(Par
71f50 73 65 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20  se*, ExprList*, 
71f60 54 6f 6b 65 6e 2a 2c 20 45 78 70 72 4c 69 73 74  Token*, ExprList
71f70 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
71f80 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
71f90 69 74 65 33 44 65 66 65 72 46 6f 72 65 69 67 6e  ite3DeferForeign
71fa0 4b 65 79 28 50 61 72 73 65 2a 2c 20 69 6e 74 29  Key(Parse*, int)
71fb0 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
71fc0 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
71fd0 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ION.SQLITE_PRIVA
71fe0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
71ff0 33 41 75 74 68 52 65 61 64 28 50 61 72 73 65 2a  3AuthRead(Parse*
72000 2c 45 78 70 72 2a 2c 53 63 68 65 6d 61 2a 2c 53  ,Expr*,Schema*,S
72010 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45  rcList*);.SQLITE
72020 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73  _PRIVATE   int s
72030 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
72040 50 61 72 73 65 2a 2c 69 6e 74 2c 20 63 6f 6e 73  Parse*,int, cons
72050 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63  t char*, const c
72060 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  har*, const char
72070 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
72080 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
72090 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75 73 68  3AuthContextPush
720a0 28 50 61 72 73 65 2a 2c 20 41 75 74 68 43 6f 6e  (Parse*, AuthCon
720b0 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 63 68 61  text*, const cha
720c0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
720d0 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
720e0 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 6f 70  e3AuthContextPop
720f0 28 41 75 74 68 43 6f 6e 74 65 78 74 2a 29 3b 0a  (AuthContext*);.
72100 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73  #else.# define s
72110 71 6c 69 74 65 33 41 75 74 68 52 65 61 64 28 61  qlite3AuthRead(a
72120 2c 62 2c 63 2c 64 29 0a 23 20 64 65 66 69 6e 65  ,b,c,d).# define
72130 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
72140 6b 28 61 2c 62 2c 63 2c 64 2c 65 29 20 20 20 20  k(a,b,c,d,e)    
72150 53 51 4c 49 54 45 5f 4f 4b 0a 23 20 64 65 66 69  SQLITE_OK.# defi
72160 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f  ne sqlite3AuthCo
72170 6e 74 65 78 74 50 75 73 68 28 61 2c 62 2c 63 29  ntextPush(a,b,c)
72180 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
72190 33 41 75 74 68 43 6f 6e 74 65 78 74 50 6f 70 28  3AuthContextPop(
721a0 61 29 20 20 28 28 76 6f 69 64 29 28 61 29 29 0a  a)  ((void)(a)).
721b0 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52  #endif.SQLITE_PR
721c0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
721d0 65 33 41 74 74 61 63 68 28 50 61 72 73 65 2a 2c  e3Attach(Parse*,
721e0 20 45 78 70 72 2a 2c 20 45 78 70 72 2a 2c 20 45   Expr*, Expr*, E
721f0 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  xpr*);.SQLITE_PR
72200 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
72210 65 33 44 65 74 61 63 68 28 50 61 72 73 65 2a 2c  e3Detach(Parse*,
72220 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   Expr*);.SQLITE_
72230 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
72240 74 65 33 42 74 72 65 65 46 61 63 74 6f 72 79 28  te3BtreeFactory(
72250 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 20 2a 64  const sqlite3 *d
72260 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
72270 46 69 6c 65 6e 61 6d 65 2c 0a 20 20 20 20 20 20  Filename,.      
72280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
72290 20 69 6e 74 20 6f 6d 69 74 4a 6f 75 72 6e 61 6c   int omitJournal
722a0 2c 20 69 6e 74 20 6e 43 61 63 68 65 2c 20 69 6e  , int nCache, in
722b0 74 20 66 6c 61 67 73 2c 20 42 74 72 65 65 20 2a  t flags, Btree *
722c0 2a 70 70 42 74 72 65 65 29 3b 0a 53 51 4c 49 54  *ppBtree);.SQLIT
722d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
722e0 6c 69 74 65 33 46 69 78 49 6e 69 74 28 44 62 46  lite3FixInit(DbF
722f0 69 78 65 72 2a 2c 20 50 61 72 73 65 2a 2c 20 69  ixer*, Parse*, i
72300 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c  nt, const char*,
72310 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 2a 29 3b 0a   const Token*);.
72320 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
72330 6e 74 20 73 71 6c 69 74 65 33 46 69 78 53 72 63  nt sqlite3FixSrc
72340 4c 69 73 74 28 44 62 46 69 78 65 72 2a 2c 20 53  List(DbFixer*, S
72350 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45  rcList*);.SQLITE
72360 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
72370 69 74 65 33 46 69 78 53 65 6c 65 63 74 28 44 62  ite3FixSelect(Db
72380 46 69 78 65 72 2a 2c 20 53 65 6c 65 63 74 2a 29  Fixer*, Select*)
72390 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
723a0 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 45   int sqlite3FixE
723b0 78 70 72 28 44 62 46 69 78 65 72 2a 2c 20 45 78  xpr(DbFixer*, Ex
723c0 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  pr*);.SQLITE_PRI
723d0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
723e0 46 69 78 45 78 70 72 4c 69 73 74 28 44 62 46 69  FixExprList(DbFi
723f0 78 65 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29  xer*, ExprList*)
72400 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
72410 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 54   int sqlite3FixT
72420 72 69 67 67 65 72 53 74 65 70 28 44 62 46 69 78  riggerStep(DbFix
72430 65 72 2a 2c 20 54 72 69 67 67 65 72 53 74 65 70  er*, TriggerStep
72440 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
72450 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74  TE int sqlite3At
72460 6f 46 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  oF(const char *z
72470 2c 20 64 6f 75 62 6c 65 2a 29 3b 0a 53 51 4c 49  , double*);.SQLI
72480 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
72490 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32 28 63  qlite3GetInt32(c
724a0 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74  onst char *, int
724b0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
724c0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69  TE int sqlite3Fi
724d0 74 73 49 6e 36 34 42 69 74 73 28 63 6f 6e 73 74  tsIn64Bits(const
724e0 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 53   char *, int);.S
724f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
72500 74 20 73 71 6c 69 74 65 33 55 74 66 31 36 42 79  t sqlite3Utf16By
72510 74 65 4c 65 6e 28 63 6f 6e 73 74 20 76 6f 69 64  teLen(const void
72520 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 43 68   *pData, int nCh
72530 61 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  ar);.SQLITE_PRIV
72540 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55  ATE int sqlite3U
72550 74 66 38 43 68 61 72 4c 65 6e 28 63 6f 6e 73 74  tf8CharLen(const
72560 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e   char *pData, in
72570 74 20 6e 42 79 74 65 29 3b 0a 53 51 4c 49 54 45  t nByte);.SQLITE
72580 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
72590 69 74 65 33 55 74 66 38 52 65 61 64 28 63 6f 6e  ite3Utf8Read(con
725a0 73 74 20 75 38 2a 2c 20 63 6f 6e 73 74 20 75 38  st u8*, const u8
725b0 2a 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74  **);../*.** Rout
725c0 69 6e 65 73 20 74 6f 20 72 65 61 64 20 61 6e 64  ines to read and
725d0 20 77 72 69 74 65 20 76 61 72 69 61 62 6c 65 2d   write variable-
725e0 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73 2e  length integers.
725f0 20 20 54 68 65 73 65 20 75 73 65 64 20 74 6f 0a    These used to.
72600 2a 2a 20 62 65 20 64 65 66 69 6e 65 64 20 6c 6f  ** be defined lo
72610 63 61 6c 6c 79 2c 20 62 75 74 20 6e 6f 77 20 77  cally, but now w
72620 65 20 75 73 65 20 74 68 65 20 76 61 72 69 6e 74  e use the varint
72630 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 65   routines in the
72640 20 75 74 69 6c 2e 63 0a 2a 2a 20 66 69 6c 65 2e   util.c.** file.
72650 20 20 43 6f 64 65 20 73 68 6f 75 6c 64 20 75 73    Code should us
72660 65 20 74 68 65 20 4d 41 43 52 4f 20 66 6f 72 6d  e the MACRO form
72670 73 20 62 65 6c 6f 77 2c 20 61 73 20 74 68 65 20  s below, as the 
72680 56 61 72 69 6e 74 33 32 20 76 65 72 73 69 6f 6e  Varint32 version
72690 73 0a 2a 2a 20 61 72 65 20 63 6f 64 65 64 20 74  s.** are coded t
726a0 6f 20 61 73 73 75 6d 65 20 74 68 65 20 73 69 6e  o assume the sin
726b0 67 6c 65 20 62 79 74 65 20 63 61 73 65 20 69 73  gle byte case is
726c0 20 61 6c 72 65 61 64 79 20 68 61 6e 64 6c 65 64   already handled
726d0 20 28 77 68 69 63 68 20 0a 2a 2a 20 74 68 65 20   (which .** the 
726e0 4d 41 43 52 4f 20 66 6f 72 6d 20 64 6f 65 73 29  MACRO form does)
726f0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
72700 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
72710 75 74 56 61 72 69 6e 74 28 75 6e 73 69 67 6e 65  utVarint(unsigne
72720 64 20 63 68 61 72 2a 2c 20 75 36 34 29 3b 0a 53  d char*, u64);.S
72730 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
72740 74 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69  t sqlite3PutVari
72750 6e 74 33 32 28 75 6e 73 69 67 6e 65 64 20 63 68  nt32(unsigned ch
72760 61 72 2a 2c 20 75 33 32 29 3b 0a 53 51 4c 49 54  ar*, u32);.SQLIT
72770 45 5f 50 52 49 56 41 54 45 20 75 38 20 73 71 6c  E_PRIVATE u8 sql
72780 69 74 65 33 47 65 74 56 61 72 69 6e 74 28 63 6f  ite3GetVarint(co
72790 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
727a0 72 20 2a 2c 20 75 36 34 20 2a 29 3b 0a 53 51 4c  r *, u64 *);.SQL
727b0 49 54 45 5f 50 52 49 56 41 54 45 20 75 38 20 73  ITE_PRIVATE u8 s
727c0 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 33  qlite3GetVarint3
727d0 32 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  2(const unsigned
727e0 20 63 68 61 72 20 2a 2c 20 75 33 32 20 2a 29 3b   char *, u32 *);
727f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
72800 69 6e 74 20 73 71 6c 69 74 65 33 56 61 72 69 6e  int sqlite3Varin
72810 74 4c 65 6e 28 75 36 34 20 76 29 3b 0a 0a 2f 2a  tLen(u64 v);../*
72820 0a 2a 2a 20 54 68 65 20 68 65 61 64 65 72 20 6f  .** The header o
72830 66 20 61 20 72 65 63 6f 72 64 20 63 6f 6e 73 69  f a record consi
72840 73 74 73 20 6f 66 20 61 20 73 65 71 75 65 6e 63  sts of a sequenc
72850 65 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74  e variable-lengt
72860 68 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a 20 54  h integers..** T
72870 68 65 73 65 20 69 6e 74 65 67 65 72 73 20 61 72  hese integers ar
72880 65 20 61 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20  e almost always 
72890 73 6d 61 6c 6c 20 61 6e 64 20 61 72 65 20 65 6e  small and are en
728a0 63 6f 64 65 64 20 61 73 20 61 20 73 69 6e 67 6c  coded as a singl
728b0 65 20 62 79 74 65 2e 0a 2a 2a 20 54 68 65 20 66  e byte..** The f
728c0 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20  ollowing macros 
728d0 74 61 6b 65 20 61 64 76 61 6e 74 61 67 65 20 74  take advantage t
728e0 68 69 73 20 66 61 63 74 20 74 6f 20 70 72 6f 76  his fact to prov
728f0 69 64 65 20 61 20 66 61 73 74 20 65 6e 63 6f 64  ide a fast encod
72900 65 0a 2a 2a 20 61 6e 64 20 64 65 63 6f 64 65 20  e.** and decode 
72910 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 73 20  of the integers 
72920 69 6e 20 61 20 72 65 63 6f 72 64 20 68 65 61 64  in a record head
72930 65 72 2e 20 20 49 74 20 69 73 20 66 61 73 74 65  er.  It is faste
72940 72 20 66 6f 72 20 74 68 65 20 63 6f 6d 6d 6f 6e  r for the common
72950 0a 2a 2a 20 63 61 73 65 20 77 68 65 72 65 20 74  .** case where t
72960 68 65 20 69 6e 74 65 67 65 72 20 69 73 20 61 20  he integer is a 
72970 73 69 6e 67 6c 65 20 62 79 74 65 2e 20 20 49 74  single byte.  It
72980 20 69 73 20 61 20 6c 69 74 74 6c 65 20 73 6c 6f   is a little slo
72990 77 65 72 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20  wer when the.** 
729a0 69 6e 74 65 67 65 72 20 69 73 20 74 77 6f 20 6f  integer is two o
729b0 72 20 6d 6f 72 65 20 62 79 74 65 73 2e 20 20 42  r more bytes.  B
729c0 75 74 20 6f 76 65 72 61 6c 6c 20 69 74 20 69 73  ut overall it is
729d0 20 66 61 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54   faster..**.** T
729e0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 78 70  he following exp
729f0 72 65 73 73 69 6f 6e 73 20 61 72 65 20 65 71 75  ressions are equ
72a00 69 76 61 6c 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20  ivalent:.**.**  
72a10 20 20 20 78 20 3d 20 73 71 6c 69 74 65 33 47 65     x = sqlite3Ge
72a20 74 56 61 72 69 6e 74 33 32 28 20 41 2c 20 26 42  tVarint32( A, &B
72a30 20 29 3b 0a 2a 2a 20 20 20 20 20 78 20 3d 20 73   );.**     x = s
72a40 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 33  qlite3PutVarint3
72a50 32 28 20 41 2c 20 42 20 29 3b 0a 2a 2a 0a 2a 2a  2( A, B );.**.**
72a60 20 20 20 20 20 78 20 3d 20 67 65 74 56 61 72 69       x = getVari
72a70 6e 74 33 32 28 20 41 2c 20 42 20 29 3b 0a 2a 2a  nt32( A, B );.**
72a80 20 20 20 20 20 78 20 3d 20 70 75 74 56 61 72 69       x = putVari
72a90 6e 74 33 32 28 20 41 2c 20 42 20 29 3b 0a 2a 2a  nt32( A, B );.**
72aa0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 56  .*/.#define getV
72ab0 61 72 69 6e 74 33 32 28 41 2c 42 29 20 20 28 75  arint32(A,B)  (u
72ac0 38 29 28 28 2a 28 41 29 3c 28 75 38 29 30 78 38  8)((*(A)<(u8)0x8
72ad0 30 29 20 3f 20 28 28 42 29 20 3d 20 28 75 33 32  0) ? ((B) = (u32
72ae0 29 2a 28 41 29 29 2c 31 20 3a 20 73 71 6c 69 74  )*(A)),1 : sqlit
72af0 65 33 47 65 74 56 61 72 69 6e 74 33 32 28 28 41  e3GetVarint32((A
72b00 29 2c 20 28 75 33 32 20 2a 29 26 28 42 29 29 29  ), (u32 *)&(B)))
72b10 0a 23 64 65 66 69 6e 65 20 70 75 74 56 61 72 69  .#define putVari
72b20 6e 74 33 32 28 41 2c 42 29 20 20 28 75 38 29 28  nt32(A,B)  (u8)(
72b30 28 28 75 33 32 29 28 42 29 3c 28 75 33 32 29 30  ((u32)(B)<(u32)0
72b40 78 38 30 29 20 3f 20 28 2a 28 41 29 20 3d 20 28  x80) ? (*(A) = (
72b50 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 42  unsigned char)(B
72b60 29 29 2c 31 20 3a 20 73 71 6c 69 74 65 33 50 75  )),1 : sqlite3Pu
72b70 74 56 61 72 69 6e 74 33 32 28 28 41 29 2c 20 28  tVarint32((A), (
72b80 42 29 29 29 0a 23 64 65 66 69 6e 65 20 67 65 74  B))).#define get
72b90 56 61 72 69 6e 74 20 20 20 20 73 71 6c 69 74 65  Varint    sqlite
72ba0 33 47 65 74 56 61 72 69 6e 74 0a 23 64 65 66 69  3GetVarint.#defi
72bb0 6e 65 20 70 75 74 56 61 72 69 6e 74 20 20 20 20  ne putVarint    
72bc0 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74  sqlite3PutVarint
72bd0 0a 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ...SQLITE_PRIVAT
72be0 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  E const char *sq
72bf0 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69  lite3IndexAffini
72c00 74 79 53 74 72 28 56 64 62 65 20 2a 2c 20 49 6e  tyStr(Vdbe *, In
72c10 64 65 78 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  dex *);.SQLITE_P
72c20 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
72c30 74 65 33 54 61 62 6c 65 41 66 66 69 6e 69 74 79  te3TableAffinity
72c40 53 74 72 28 56 64 62 65 20 2a 2c 20 54 61 62 6c  Str(Vdbe *, Tabl
72c50 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  e *);.SQLITE_PRI
72c60 56 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65  VATE char sqlite
72c70 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79  3CompareAffinity
72c80 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68  (Expr *pExpr, ch
72c90 61 72 20 61 66 66 32 29 3b 0a 53 51 4c 49 54 45  ar aff2);.SQLITE
72ca0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
72cb0 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74  ite3IndexAffinit
72cc0 79 4f 6b 28 45 78 70 72 20 2a 70 45 78 70 72 2c  yOk(Expr *pExpr,
72cd0 20 63 68 61 72 20 69 64 78 5f 61 66 66 69 6e 69   char idx_affini
72ce0 74 79 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  ty);.SQLITE_PRIV
72cf0 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33  ATE char sqlite3
72d00 45 78 70 72 41 66 66 69 6e 69 74 79 28 45 78 70  ExprAffinity(Exp
72d10 72 20 2a 70 45 78 70 72 29 3b 0a 53 51 4c 49 54  r *pExpr);.SQLIT
72d20 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
72d30 6c 69 74 65 33 41 74 6f 69 36 34 28 63 6f 6e 73  lite3Atoi64(cons
72d40 74 20 63 68 61 72 2a 2c 20 69 36 34 2a 29 3b 0a  t char*, i64*);.
72d50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
72d60 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72  oid sqlite3Error
72d70 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20  (sqlite3*, int, 
72d80 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29  const char*,...)
72d90 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
72da0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 65   void *sqlite3He
72db0 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74 65 33 2a  xToBlob(sqlite3*
72dc0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c  , const char *z,
72dd0 20 69 6e 74 20 6e 29 3b 0a 53 51 4c 49 54 45 5f   int n);.SQLITE_
72de0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
72df0 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 50  te3TwoPartName(P
72e00 61 72 73 65 20 2a 2c 20 54 6f 6b 65 6e 20 2a 2c  arse *, Token *,
72e10 20 54 6f 6b 65 6e 20 2a 2c 20 54 6f 6b 65 6e 20   Token *, Token 
72e20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  **);.SQLITE_PRIV
72e30 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ATE const char *
72e40 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 69 6e  sqlite3ErrStr(in
72e50 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
72e60 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65  TE int sqlite3Re
72e70 61 64 53 63 68 65 6d 61 28 50 61 72 73 65 20 2a  adSchema(Parse *
72e80 70 50 61 72 73 65 29 3b 0a 53 51 4c 49 54 45 5f  pParse);.SQLITE_
72e90 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20  PRIVATE CollSeq 
72ea0 2a 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c  *sqlite3FindColl
72eb0 53 65 71 28 73 71 6c 69 74 65 33 2a 2c 75 38 20  Seq(sqlite3*,u8 
72ec0 65 6e 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  enc, const char*
72ed0 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
72ee0 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73  IVATE CollSeq *s
72ef0 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c  qlite3LocateColl
72f00 53 65 71 28 50 61 72 73 65 20 2a 70 50 61 72 73  Seq(Parse *pPars
72f10 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 7a 4e  e, const char*zN
72f20 61 6d 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ame);.SQLITE_PRI
72f30 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71  VATE CollSeq *sq
72f40 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71  lite3ExprCollSeq
72f50 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
72f60 45 78 70 72 20 2a 70 45 78 70 72 29 3b 0a 53 51  Expr *pExpr);.SQ
72f70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70  LITE_PRIVATE Exp
72f80 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 53 65  r *sqlite3ExprSe
72f90 74 43 6f 6c 6c 28 50 61 72 73 65 20 2a 70 50 61  tColl(Parse *pPa
72fa0 72 73 65 2c 20 45 78 70 72 20 2a 2c 20 54 6f 6b  rse, Expr *, Tok
72fb0 65 6e 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  en *);.SQLITE_PR
72fc0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
72fd0 33 43 68 65 63 6b 43 6f 6c 6c 53 65 71 28 50 61  3CheckCollSeq(Pa
72fe0 72 73 65 20 2a 2c 20 43 6f 6c 6c 53 65 71 20 2a  rse *, CollSeq *
72ff0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
73000 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 68 65  E int sqlite3Che
73010 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 50 61 72  ckObjectName(Par
73020 73 65 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  se *, const char
73030 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
73040 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
73050 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 73  VdbeSetChanges(s
73060 71 6c 69 74 65 33 20 2a 2c 20 69 6e 74 29 3b 0a  qlite3 *, int);.
73070 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
73080 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
73090 74 65 33 56 61 6c 75 65 54 65 78 74 28 73 71 6c  te3ValueText(sql
730a0 69 74 65 33 5f 76 61 6c 75 65 2a 2c 20 75 38 29  ite3_value*, u8)
730b0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
730c0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 6c 75   int sqlite3Valu
730d0 65 42 79 74 65 73 28 73 71 6c 69 74 65 33 5f 76  eBytes(sqlite3_v
730e0 61 6c 75 65 2a 2c 20 75 38 29 3b 0a 53 51 4c 49  alue*, u8);.SQLI
730f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
73100 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53  sqlite3ValueSetS
73110 74 72 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  tr(sqlite3_value
73120 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f  *, int, const vo
73130 69 64 20 2a 2c 75 38 2c 20 0a 20 20 20 20 20 20  id *,u8, .      
73140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
73150 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29    void(*)(void*)
73160 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
73170 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61  E void sqlite3Va
73180 6c 75 65 46 72 65 65 28 73 71 6c 69 74 65 33 5f  lueFree(sqlite3_
73190 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  value*);.SQLITE_
731a0 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f  PRIVATE sqlite3_
731b0 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 56 61  value *sqlite3Va
731c0 6c 75 65 4e 65 77 28 73 71 6c 69 74 65 33 20 2a  lueNew(sqlite3 *
731d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
731e0 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 55  E char *sqlite3U
731f0 74 66 31 36 74 6f 38 28 73 71 6c 69 74 65 33 20  tf16to8(sqlite3 
73200 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20  *, const void*, 
73210 69 6e 74 29 3b 0a 23 69 66 64 65 66 20 53 51 4c  int);.#ifdef SQL
73220 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32  ITE_ENABLE_STAT2
73230 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
73240 63 68 61 72 20 2a 73 71 6c 69 74 65 33 55 74 66  char *sqlite3Utf
73250 38 74 6f 31 36 28 73 71 6c 69 74 65 33 20 2a 2c  8to16(sqlite3 *,
73260 20 75 38 2c 20 63 68 61 72 20 2a 2c 20 69 6e 74   u8, char *, int
73270 2c 20 69 6e 74 20 2a 29 3b 0a 23 65 6e 64 69 66  , int *);.#endif
73280 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
73290 69 6e 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65  int sqlite3Value
732a0 46 72 6f 6d 45 78 70 72 28 73 71 6c 69 74 65 33  FromExpr(sqlite3
732b0 20 2a 2c 20 45 78 70 72 20 2a 2c 20 75 38 2c 20   *, Expr *, u8, 
732c0 75 38 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  u8, sqlite3_valu
732d0 65 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  e **);.SQLITE_PR
732e0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
732f0 65 33 56 61 6c 75 65 41 70 70 6c 79 41 66 66 69  e3ValueApplyAffi
73300 6e 69 74 79 28 73 71 6c 69 74 65 33 5f 76 61 6c  nity(sqlite3_val
73310 75 65 20 2a 2c 20 75 38 2c 20 75 38 29 3b 0a 23  ue *, u8, u8);.#
73320 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 4d  ifndef SQLITE_AM
73330 41 4c 47 41 4d 41 54 49 4f 4e 0a 53 51 4c 49 54  ALGAMATION.SQLIT
73340 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
73350 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 71  unsigned char sq
73360 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65  lite3UpperToLowe
73370 72 5b 5d 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r[];.SQLITE_PRIV
73380 41 54 45 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  ATE const unsign
73390 65 64 20 63 68 61 72 20 73 71 6c 69 74 65 33 43  ed char sqlite3C
733a0 74 79 70 65 4d 61 70 5b 5d 3b 0a 53 51 4c 49 54  typeMap[];.SQLIT
733b0 45 5f 50 52 49 56 41 54 45 20 53 51 4c 49 54 45  E_PRIVATE SQLITE
733c0 5f 57 53 44 20 73 74 72 75 63 74 20 53 71 6c 69  _WSD struct Sqli
733d0 74 65 33 43 6f 6e 66 69 67 20 73 71 6c 69 74 65  te3Config sqlite
733e0 33 43 6f 6e 66 69 67 3b 0a 53 51 4c 49 54 45 5f  3Config;.SQLITE_
733f0 50 52 49 56 41 54 45 20 53 51 4c 49 54 45 5f 57  PRIVATE SQLITE_W
73400 53 44 20 46 75 6e 63 44 65 66 48 61 73 68 20 73  SD FuncDefHash s
73410 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63  qlite3GlobalFunc
73420 74 69 6f 6e 73 3b 0a 53 51 4c 49 54 45 5f 50 52  tions;.SQLITE_PR
73430 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
73440 33 50 65 6e 64 69 6e 67 42 79 74 65 3b 0a 23 65  3PendingByte;.#e
73450 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56  ndif.SQLITE_PRIV
73460 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
73470 52 6f 6f 74 50 61 67 65 4d 6f 76 65 64 28 44 62  RootPageMoved(Db
73480 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51  *, int, int);.SQ
73490 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
734a0 64 20 73 71 6c 69 74 65 33 52 65 69 6e 64 65 78  d sqlite3Reindex
734b0 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c  (Parse*, Token*,
734c0 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45   Token*);.SQLITE
734d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
734e0 6c 69 74 65 33 41 6c 74 65 72 46 75 6e 63 74 69  lite3AlterFuncti
734f0 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53  ons(sqlite3*);.S
73500 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
73510 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 52  id sqlite3AlterR
73520 65 6e 61 6d 65 54 61 62 6c 65 28 50 61 72 73 65  enameTable(Parse
73530 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 54 6f 6b  *, SrcList*, Tok
73540 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  en*);.SQLITE_PRI
73550 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
73560 47 65 74 54 6f 6b 65 6e 28 63 6f 6e 73 74 20 75  GetToken(const u
73570 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2c 20  nsigned char *, 
73580 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  int *);.SQLITE_P
73590 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
735a0 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 50  te3NestedParse(P
735b0 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61  arse*, const cha
735c0 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45  r*, ...);.SQLITE
735d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
735e0 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61  lite3ExpirePrepa
735f0 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 73 71  redStatements(sq
73600 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f  lite3*);.SQLITE_
73610 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
73620 69 74 65 33 43 6f 64 65 53 75 62 73 65 6c 65 63  ite3CodeSubselec
73630 74 28 50 61 72 73 65 20 2a 2c 20 45 78 70 72 20  t(Parse *, Expr 
73640 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51  *, int, int);.SQ
73650 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
73660 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50  d sqlite3SelectP
73670 72 65 70 28 50 61 72 73 65 2a 2c 20 53 65 6c 65  rep(Parse*, Sele
73680 63 74 2a 2c 20 4e 61 6d 65 43 6f 6e 74 65 78 74  ct*, NameContext
73690 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
736a0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65  TE int sqlite3Re
736b0 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 4e  solveExprNames(N
736c0 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 20 45 78 70  ameContext*, Exp
736d0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
736e0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
736f0 52 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61 6d  ResolveSelectNam
73700 65 73 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 63  es(Parse*, Selec
73710 74 2a 2c 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2a  t*, NameContext*
73720 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
73730 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 73  E int sqlite3Res
73740 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79  olveOrderGroupBy
73750 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a  (Parse*, Select*
73760 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 63 6f 6e  , ExprList*, con
73770 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54  st char*);.SQLIT
73780 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
73790 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61  qlite3ColumnDefa
737a0 75 6c 74 28 56 64 62 65 20 2a 2c 20 54 61 62 6c  ult(Vdbe *, Tabl
737b0 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  e *, int, int);.
737c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
737d0 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72  oid sqlite3Alter
737e0 46 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28  FinishAddColumn(
737f0 50 61 72 73 65 20 2a 2c 20 54 6f 6b 65 6e 20 2a  Parse *, Token *
73800 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
73810 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c  E void sqlite3Al
73820 74 65 72 42 65 67 69 6e 41 64 64 43 6f 6c 75 6d  terBeginAddColum
73830 6e 28 50 61 72 73 65 20 2a 2c 20 53 72 63 4c 69  n(Parse *, SrcLi
73840 73 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  st *);.SQLITE_PR
73850 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73  IVATE CollSeq *s
73860 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53 65 71  qlite3GetCollSeq
73870 28 73 71 6c 69 74 65 33 2a 2c 20 75 38 2c 20 43  (sqlite3*, u8, C
73880 6f 6c 6c 53 65 71 20 2a 2c 20 63 6f 6e 73 74 20  ollSeq *, const 
73890 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  char*);.SQLITE_P
738a0 52 49 56 41 54 45 20 63 68 61 72 20 73 71 6c 69  RIVATE char sqli
738b0 74 65 33 41 66 66 69 6e 69 74 79 54 79 70 65 28  te3AffinityType(
738c0 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51  const char*);.SQ
738d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
738e0 64 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 7a 65  d sqlite3Analyze
738f0 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c  (Parse*, Token*,
73900 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45   Token*);.SQLITE
73910 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
73920 69 74 65 33 49 6e 76 6f 6b 65 42 75 73 79 48 61  ite3InvokeBusyHa
73930 6e 64 6c 65 72 28 42 75 73 79 48 61 6e 64 6c 65  ndler(BusyHandle
73940 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
73950 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
73960 69 6e 64 44 62 28 73 71 6c 69 74 65 33 2a 2c 20  indDb(sqlite3*, 
73970 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  Token*);.SQLITE_
73980 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
73990 74 65 33 46 69 6e 64 44 62 4e 61 6d 65 28 73 71  te3FindDbName(sq
739a0 6c 69 74 65 33 20 2a 2c 20 63 6f 6e 73 74 20 63  lite3 *, const c
739b0 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  har *);.SQLITE_P
739c0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
739d0 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61 64 28 73  e3AnalysisLoad(s
739e0 71 6c 69 74 65 33 2a 2c 69 6e 74 20 69 44 42 29  qlite3*,int iDB)
739f0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
73a00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c   void sqlite3Del
73a10 65 74 65 49 6e 64 65 78 53 61 6d 70 6c 65 73 28  eteIndexSamples(
73a20 49 6e 64 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f  Index*);.SQLITE_
73a30 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
73a40 69 74 65 33 44 65 66 61 75 6c 74 52 6f 77 45 73  ite3DefaultRowEs
73a50 74 28 49 6e 64 65 78 2a 29 3b 0a 53 51 4c 49 54  t(Index*);.SQLIT
73a60 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
73a70 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 4c 69  qlite3RegisterLi
73a80 6b 65 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69  keFunctions(sqli
73a90 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  te3*, int);.SQLI
73aa0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
73ab0 71 6c 69 74 65 33 49 73 4c 69 6b 65 46 75 6e 63  qlite3IsLikeFunc
73ac0 74 69 6f 6e 28 73 71 6c 69 74 65 33 2a 2c 45 78  tion(sqlite3*,Ex
73ad0 70 72 2a 2c 69 6e 74 2a 2c 63 68 61 72 2a 29 3b  pr*,int*,char*);
73ae0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
73af0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 69 6e 69  void sqlite3Mini
73b00 6d 75 6d 46 69 6c 65 46 6f 72 6d 61 74 28 50 61  mumFileFormat(Pa
73b10 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b  rse*, int, int);
73b20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
73b30 76 6f 69 64 20 73 71 6c 69 74 65 33 53 63 68 65  void sqlite3Sche
73b40 6d 61 46 72 65 65 28 76 6f 69 64 20 2a 29 3b 0a  maFree(void *);.
73b50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53  SQLITE_PRIVATE S
73b60 63 68 65 6d 61 20 2a 73 71 6c 69 74 65 33 53 63  chema *sqlite3Sc
73b70 68 65 6d 61 47 65 74 28 73 71 6c 69 74 65 33 20  hemaGet(sqlite3 
73b80 2a 2c 20 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c  *, Btree *);.SQL
73b90 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
73ba0 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
73bb0 6e 64 65 78 28 73 71 6c 69 74 65 33 20 2a 64 62  ndex(sqlite3 *db
73bc0 2c 20 53 63 68 65 6d 61 20 2a 29 3b 0a 53 51 4c  , Schema *);.SQL
73bd0 49 54 45 5f 50 52 49 56 41 54 45 20 4b 65 79 49  ITE_PRIVATE KeyI
73be0 6e 66 6f 20 2a 73 71 6c 69 74 65 33 49 6e 64 65  nfo *sqlite3Inde
73bf0 78 4b 65 79 69 6e 66 6f 28 50 61 72 73 65 20 2a  xKeyinfo(Parse *
73c00 2c 20 49 6e 64 65 78 20 2a 29 3b 0a 53 51 4c 49  , Index *);.SQLI
73c10 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
73c20 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63  qlite3CreateFunc
73c30 28 73 71 6c 69 74 65 33 20 2a 2c 20 63 6f 6e 73  (sqlite3 *, cons
73c40 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 69  t char *, int, i
73c50 6e 74 2c 20 76 6f 69 64 20 2a 2c 20 0a 20 20 76  nt, void *, .  v
73c60 6f 69 64 20 28 2a 29 28 73 71 6c 69 74 65 33 5f  oid (*)(sqlite3_
73c70 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c  context*,int,sql
73c80 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a  ite3_value **),.
73c90 20 20 76 6f 69 64 20 28 2a 29 28 73 71 6c 69 74    void (*)(sqlit
73ca0 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c  e3_context*,int,
73cb0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
73cc0 29 2c 20 76 6f 69 64 20 28 2a 29 28 73 71 6c 69  ), void (*)(sqli
73cd0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 29 3b 0a  te3_context*));.
73ce0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
73cf0 6e 74 20 73 71 6c 69 74 65 33 41 70 69 45 78 69  nt sqlite3ApiExi
73d00 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69  t(sqlite3 *db, i
73d10 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
73d20 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
73d30 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73 65 28  penTempDatabase(
73d40 50 61 72 73 65 20 2a 29 3b 0a 0a 53 51 4c 49 54  Parse *);..SQLIT
73d50 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
73d60 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e  qlite3StrAccumIn
73d70 69 74 28 53 74 72 41 63 63 75 6d 2a 2c 20 63 68  it(StrAccum*, ch
73d80 61 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  ar*, int, int);.
73d90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
73da0 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72 41 63  oid sqlite3StrAc
73db0 63 75 6d 41 70 70 65 6e 64 28 53 74 72 41 63 63  cumAppend(StrAcc
73dc0 75 6d 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  um*,const char*,
73dd0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
73de0 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74  VATE char *sqlit
73df0 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68  e3StrAccumFinish
73e00 28 53 74 72 41 63 63 75 6d 2a 29 3b 0a 53 51 4c  (StrAccum*);.SQL
73e10 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
73e20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
73e30 52 65 73 65 74 28 53 74 72 41 63 63 75 6d 2a 29  Reset(StrAccum*)
73e40 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
73e50 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c   void sqlite3Sel
73e60 65 63 74 44 65 73 74 49 6e 69 74 28 53 65 6c 65  ectDestInit(Sele
73e70 63 74 44 65 73 74 2a 2c 69 6e 74 2c 69 6e 74 29  ctDest*,int,int)
73e80 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
73e90 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 61  E void sqlite3Ba
73ea0 63 6b 75 70 52 65 73 74 61 72 74 28 73 71 6c 69  ckupRestart(sqli
73eb0 74 65 33 5f 62 61 63 6b 75 70 20 2a 29 3b 0a 53  te3_backup *);.S
73ec0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
73ed0 69 64 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70  id sqlite3Backup
73ee0 55 70 64 61 74 65 28 73 71 6c 69 74 65 33 5f 62  Update(sqlite3_b
73ef0 61 63 6b 75 70 20 2a 2c 20 50 67 6e 6f 2c 20 63  ackup *, Pgno, c
73f00 6f 6e 73 74 20 75 38 20 2a 29 3b 0a 0a 2f 2a 0a  onst u8 *);../*.
73f10 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65  ** The interface
73f20 20 74 6f 20 74 68 65 20 4c 45 4d 4f 4e 2d 67 65   to the LEMON-ge
73f30 6e 65 72 61 74 65 64 20 70 61 72 73 65 72 0a 2a  nerated parser.*
73f40 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
73f50 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61   void *sqlite3Pa
73f60 72 73 65 72 41 6c 6c 6f 63 28 76 6f 69 64 2a 28  rserAlloc(void*(
73f70 2a 29 28 73 69 7a 65 5f 74 29 29 3b 0a 53 51 4c  *)(size_t));.SQL
73f80 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
73f90 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 46 72   sqlite3ParserFr
73fa0 65 65 28 76 6f 69 64 2a 2c 20 76 6f 69 64 28 2a  ee(void*, void(*
73fb0 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54  )(void*));.SQLIT
73fc0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
73fd0 71 6c 69 74 65 33 50 61 72 73 65 72 28 76 6f 69  qlite3Parser(voi
73fe0 64 2a 2c 20 69 6e 74 2c 20 54 6f 6b 65 6e 2c 20  d*, int, Token, 
73ff0 50 61 72 73 65 2a 29 3b 0a 23 69 66 64 65 66 20  Parse*);.#ifdef 
74000 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b 44  YYTRACKMAXSTACKD
74010 45 50 54 48 0a 53 51 4c 49 54 45 5f 50 52 49 56  EPTH.SQLITE_PRIV
74020 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65  ATE   int sqlite
74030 33 50 61 72 73 65 72 53 74 61 63 6b 50 65 61 6b  3ParserStackPeak
74040 28 76 6f 69 64 2a 29 3b 0a 23 65 6e 64 69 66 0a  (void*);.#endif.
74050 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
74060 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 6f  void sqlite3Auto
74070 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 73 28 73  LoadExtensions(s
74080 71 6c 69 74 65 33 2a 29 3b 0a 23 69 66 6e 64 65  qlite3*);.#ifnde
74090 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f  f SQLITE_OMIT_LO
740a0 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c  AD_EXTENSION.SQL
740b0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
740c0 69 64 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 45  id sqlite3CloseE
740d0 78 74 65 6e 73 69 6f 6e 73 28 73 71 6c 69 74 65  xtensions(sqlite
740e0 33 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66  3*);.#else.# def
740f0 69 6e 65 20 73 71 6c 69 74 65 33 43 6c 6f 73 65  ine sqlite3Close
74100 45 78 74 65 6e 73 69 6f 6e 73 28 58 29 0a 23 65  Extensions(X).#e
74110 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
74120 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
74130 5f 43 41 43 48 45 0a 53 51 4c 49 54 45 5f 50 52  _CACHE.SQLITE_PR
74140 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
74150 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 50 61  ite3TableLock(Pa
74160 72 73 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c  rse *, int, int,
74170 20 75 38 2c 20 63 6f 6e 73 74 20 63 68 61 72 20   u8, const char 
74180 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66  *);.#else.  #def
74190 69 6e 65 20 73 71 6c 69 74 65 33 54 61 62 6c 65  ine sqlite3Table
741a0 4c 6f 63 6b 28 76 2c 77 2c 78 2c 79 2c 7a 29 0a  Lock(v,w,x,y,z).
741b0 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
741c0 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54  QLITE_TEST.SQLIT
741d0 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20  E_PRIVATE   int 
741e0 73 71 6c 69 74 65 33 55 74 66 38 54 6f 38 28 75  sqlite3Utf8To8(u
741f0 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 3b 0a  nsigned char*);.
74200 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
74210 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
74220 41 4c 54 41 42 4c 45 0a 23 20 20 64 65 66 69 6e  ALTABLE.#  defin
74230 65 20 73 71 6c 69 74 65 33 56 74 61 62 43 6c 65  e sqlite3VtabCle
74240 61 72 28 59 29 0a 23 20 20 64 65 66 69 6e 65 20  ar(Y).#  define 
74250 73 71 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28  sqlite3VtabSync(
74260 58 2c 59 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23  X,Y) SQLITE_OK.#
74270 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33    define sqlite3
74280 56 74 61 62 52 6f 6c 6c 62 61 63 6b 28 58 29 0a  VtabRollback(X).
74290 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  #  define sqlite
742a0 33 56 74 61 62 43 6f 6d 6d 69 74 28 58 29 0a 23  3VtabCommit(X).#
742b0 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33    define sqlite3
742c0 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29 20 30  VtabInSync(db) 0
742d0 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74  .#  define sqlit
742e0 65 33 56 74 61 62 4c 6f 63 6b 28 58 29 20 0a 23  e3VtabLock(X) .#
742f0 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33    define sqlite3
74300 56 74 61 62 55 6e 6c 6f 63 6b 28 58 29 0a 23 20  VtabUnlock(X).# 
74310 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56   define sqlite3V
74320 74 61 62 55 6e 6c 6f 63 6b 4c 69 73 74 28 58 29  tabUnlockList(X)
74330 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52  .#else.SQLITE_PR
74340 49 56 41 54 45 20 20 20 20 76 6f 69 64 20 73 71  IVATE    void sq
74350 6c 69 74 65 33 56 74 61 62 43 6c 65 61 72 28 54  lite3VtabClear(T
74360 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  able*);.SQLITE_P
74370 52 49 56 41 54 45 20 20 20 20 69 6e 74 20 73 71  RIVATE    int sq
74380 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28 73 71  lite3VtabSync(sq
74390 6c 69 74 65 33 20 2a 64 62 2c 20 63 68 61 72 20  lite3 *db, char 
743a0 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  **);.SQLITE_PRIV
743b0 41 54 45 20 20 20 20 69 6e 74 20 73 71 6c 69 74  ATE    int sqlit
743c0 65 33 56 74 61 62 52 6f 6c 6c 62 61 63 6b 28 73  e3VtabRollback(s
743d0 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c  qlite3 *db);.SQL
743e0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 69  ITE_PRIVATE    i
743f0 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 6f  nt sqlite3VtabCo
74400 6d 6d 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62  mmit(sqlite3 *db
74410 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
74420 45 20 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  E    void sqlite
74430 33 56 74 61 62 4c 6f 63 6b 28 56 54 61 62 6c 65  3VtabLock(VTable
74440 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
74450 41 54 45 20 20 20 20 76 6f 69 64 20 73 71 6c 69  ATE    void sqli
74460 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 56 54  te3VtabUnlock(VT
74470 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  able *);.SQLITE_
74480 50 52 49 56 41 54 45 20 20 20 20 76 6f 69 64 20  PRIVATE    void 
74490 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63  sqlite3VtabUnloc
744a0 6b 4c 69 73 74 28 73 71 6c 69 74 65 33 2a 29 3b  kList(sqlite3*);
744b0 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74  .#  define sqlit
744c0 65 33 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29  e3VtabInSync(db)
744d0 20 28 28 64 62 29 2d 3e 6e 56 54 72 61 6e 73 3e   ((db)->nVTrans>
744e0 30 20 26 26 20 28 64 62 29 2d 3e 61 56 54 72 61  0 && (db)->aVTra
744f0 6e 73 3d 3d 30 29 0a 23 65 6e 64 69 66 0a 53 51  ns==0).#endif.SQ
74500 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
74510 64 20 73 71 6c 69 74 65 33 56 74 61 62 4d 61 6b  d sqlite3VtabMak
74520 65 57 72 69 74 61 62 6c 65 28 50 61 72 73 65 2a  eWritable(Parse*
74530 2c 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45  ,Table*);.SQLITE
74540 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
74550 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e 50 61  lite3VtabBeginPa
74560 72 73 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65  rse(Parse*, Toke
74570 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65  n*, Token*, Toke
74580 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  n*);.SQLITE_PRIV
74590 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
745a0 56 74 61 62 46 69 6e 69 73 68 50 61 72 73 65 28  VtabFinishParse(
745b0 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 29 3b  Parse*, Token*);
745c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
745d0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62  void sqlite3Vtab
745e0 41 72 67 49 6e 69 74 28 50 61 72 73 65 2a 29 3b  ArgInit(Parse*);
745f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
74600 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62  void sqlite3Vtab
74610 41 72 67 45 78 74 65 6e 64 28 50 61 72 73 65 2a  ArgExtend(Parse*
74620 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54  , Token*);.SQLIT
74630 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
74640 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43 72 65  lite3VtabCallCre
74650 61 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e  ate(sqlite3*, in
74660 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c  t, const char *,
74670 20 63 68 61 72 20 2a 2a 29 3b 0a 53 51 4c 49 54   char **);.SQLIT
74680 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
74690 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43 6f 6e  lite3VtabCallCon
746a0 6e 65 63 74 28 50 61 72 73 65 2a 2c 20 54 61 62  nect(Parse*, Tab
746b0 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  le*);.SQLITE_PRI
746c0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
746d0 56 74 61 62 43 61 6c 6c 44 65 73 74 72 6f 79 28  VtabCallDestroy(
746e0 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 63  sqlite3*, int, c
746f0 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51  onst char *);.SQ
74700 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
74710 20 73 71 6c 69 74 65 33 56 74 61 62 42 65 67 69   sqlite3VtabBegi
74720 6e 28 73 71 6c 69 74 65 33 20 2a 2c 20 56 54 61  n(sqlite3 *, VTa
74730 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ble *);.SQLITE_P
74740 52 49 56 41 54 45 20 46 75 6e 63 44 65 66 20 2a  RIVATE FuncDef *
74750 73 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72 6c  sqlite3VtabOverl
74760 6f 61 64 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69  oadFunction(sqli
74770 74 65 33 20 2a 2c 46 75 6e 63 44 65 66 2a 2c 20  te3 *,FuncDef*, 
74780 69 6e 74 20 6e 41 72 67 2c 20 45 78 70 72 2a 29  int nArg, Expr*)
74790 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
747a0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 6e 76   void sqlite3Inv
747b0 61 6c 69 64 46 75 6e 63 74 69 6f 6e 28 73 71 6c  alidFunction(sql
747c0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
747d0 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  t,sqlite3_value*
747e0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
747f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 54 72  TE int sqlite3Tr
74800 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 73 28 73  ansferBindings(s
74810 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2c 20 73  qlite3_stmt *, s
74820 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 3b 0a  qlite3_stmt *);.
74830 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
74840 6e 74 20 73 71 6c 69 74 65 33 52 65 70 72 65 70  nt sqlite3Reprep
74850 61 72 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49  are(Vdbe*);.SQLI
74860 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
74870 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 43  sqlite3ExprListC
74880 68 65 63 6b 4c 65 6e 67 74 68 28 50 61 72 73 65  heckLength(Parse
74890 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 63 6f  *, ExprList*, co
748a0 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49  nst char*);.SQLI
748b0 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53  TE_PRIVATE CollS
748c0 65 71 20 2a 73 71 6c 69 74 65 33 42 69 6e 61 72  eq *sqlite3Binar
748d0 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28  yCompareCollSeq(
748e0 50 61 72 73 65 20 2a 2c 20 45 78 70 72 20 2a 2c  Parse *, Expr *,
748f0 20 45 78 70 72 20 2a 29 3b 0a 53 51 4c 49 54 45   Expr *);.SQLITE
74900 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
74910 69 74 65 33 54 65 6d 70 49 6e 4d 65 6d 6f 72 79  ite3TempInMemory
74920 28 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 2a 29  (const sqlite3*)
74930 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
74940 20 56 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33   VTable *sqlite3
74950 47 65 74 56 54 61 62 6c 65 28 73 71 6c 69 74 65  GetVTable(sqlite
74960 33 2a 2c 20 54 61 62 6c 65 2a 29 3b 0a 0a 0a 0a  3*, Table*);....
74970 2f 2a 0a 2a 2a 20 41 76 61 69 6c 61 62 6c 65 20  /*.** Available 
74980 66 61 75 6c 74 20 69 6e 6a 65 63 74 6f 72 73 2e  fault injectors.
74990 20 20 53 68 6f 75 6c 64 20 62 65 20 6e 75 6d 62    Should be numb
749a0 65 72 65 64 20 62 65 67 69 6e 6e 69 6e 67 20 77  ered beginning w
749b0 69 74 68 20 30 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ith 0..*/.#defin
749c0 65 20 53 51 4c 49 54 45 5f 46 41 55 4c 54 49 4e  e SQLITE_FAULTIN
749d0 4a 45 43 54 4f 52 5f 4d 41 4c 4c 4f 43 20 20 20  JECTOR_MALLOC   
749e0 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    0.#define SQLI
749f0 54 45 5f 46 41 55 4c 54 49 4e 4a 45 43 54 4f 52  TE_FAULTINJECTOR
74a00 5f 43 4f 55 4e 54 20 20 20 20 20 20 31 0a 0a 2f  _COUNT      1../
74a10 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61  *.** The interfa
74a20 63 65 20 74 6f 20 74 68 65 20 63 6f 64 65 20 69  ce to the code i
74a30 6e 20 66 61 75 6c 74 2e 63 20 75 73 65 64 20 66  n fault.c used f
74a40 6f 72 20 69 64 65 6e 74 69 66 79 69 6e 67 20 22  or identifying "
74a50 62 65 6e 69 67 6e 22 0a 2a 2a 20 6d 61 6c 6c 6f  benign".** mallo
74a60 63 20 66 61 69 6c 75 72 65 73 2e 20 54 68 69 73  c failures. This
74a70 20 69 73 20 6f 6e 6c 79 20 70 72 65 73 65 6e 74   is only present
74a80 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   if SQLITE_OMIT_
74a90 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a 2a 2a 20  BUILTIN_TEST.** 
74aa0 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 2e 0a  is not defined..
74ab0 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
74ac0 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54  E_OMIT_BUILTIN_T
74ad0 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  EST.SQLITE_PRIVA
74ae0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
74af0 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c  3BeginBenignMall
74b00 6f 63 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45  oc(void);.SQLITE
74b10 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
74b20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e  sqlite3EndBenign
74b30 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 3b 0a 23 65  Malloc(void);.#e
74b40 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71  lse.  #define sq
74b50 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e  lite3BeginBenign
74b60 4d 61 6c 6c 6f 63 28 29 0a 20 20 23 64 65 66 69  Malloc().  #defi
74b70 6e 65 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e  ne sqlite3EndBen
74b80 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a 23 65 6e 64  ignMalloc().#end
74b90 69 66 0a 0a 23 64 65 66 69 6e 65 20 49 4e 5f 49  if..#define IN_I
74ba0 4e 44 45 58 5f 52 4f 57 49 44 20 20 20 20 20 20  NDEX_ROWID      
74bb0 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 49       1.#define I
74bc0 4e 5f 49 4e 44 45 58 5f 45 50 48 20 20 20 20 20  N_INDEX_EPH     
74bd0 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e          2.#defin
74be0 65 20 49 4e 5f 49 4e 44 45 58 5f 49 4e 44 45 58  e IN_INDEX_INDEX
74bf0 20 20 20 20 20 20 20 20 20 20 20 33 0a 53 51 4c             3.SQL
74c00 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
74c10 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 49 6e 64  sqlite3FindInInd
74c20 65 78 28 50 61 72 73 65 20 2a 2c 20 45 78 70 72  ex(Parse *, Expr
74c30 20 2a 2c 20 69 6e 74 2a 29 3b 0a 0a 23 69 66 64   *, int*);..#ifd
74c40 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
74c50 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 53 51  _ATOMIC_WRITE.SQ
74c60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69  LITE_PRIVATE   i
74c70 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61  nt sqlite3Journa
74c80 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66  lOpen(sqlite3_vf
74c90 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  s *, const char 
74ca0 2a 2c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  *, sqlite3_file 
74cb0 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51  *, int, int);.SQ
74cc0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69  LITE_PRIVATE   i
74cd0 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61  nt sqlite3Journa
74ce0 6c 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 76 66  lSize(sqlite3_vf
74cf0 73 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  s *);.SQLITE_PRI
74d00 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74  VATE   int sqlit
74d10 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65 28  e3JournalCreate(
74d20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 3b  sqlite3_file *);
74d30 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65  .#else.  #define
74d40 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53   sqlite3JournalS
74d50 69 7a 65 28 70 56 66 73 29 20 28 28 70 56 66 73  ize(pVfs) ((pVfs
74d60 29 2d 3e 73 7a 4f 73 46 69 6c 65 29 0a 23 65 6e  )->szOsFile).#en
74d70 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  dif..SQLITE_PRIV
74d80 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
74d90 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 73  MemJournalOpen(s
74da0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a  qlite3_file *);.
74db0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
74dc0 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75  nt sqlite3MemJou
74dd0 72 6e 61 6c 53 69 7a 65 28 76 6f 69 64 29 3b 0a  rnalSize(void);.
74de0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
74df0 6e 74 20 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a  nt sqlite3IsMemJ
74e00 6f 75 72 6e 61 6c 28 73 71 6c 69 74 65 33 5f 66  ournal(sqlite3_f
74e10 69 6c 65 20 2a 29 3b 0a 0a 23 69 66 20 53 51 4c  ile *);..#if SQL
74e20 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50  ITE_MAX_EXPR_DEP
74e30 54 48 3e 30 0a 53 51 4c 49 54 45 5f 50 52 49 56  TH>0.SQLITE_PRIV
74e40 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
74e50 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28  e3ExprSetHeight(
74e60 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45  Parse *pParse, E
74e70 78 70 72 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f  xpr *p);.SQLITE_
74e80 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71  PRIVATE   int sq
74e90 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 72 48  lite3SelectExprH
74ea0 65 69 67 68 74 28 53 65 6c 65 63 74 20 2a 29 3b  eight(Select *);
74eb0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
74ec0 20 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70    int sqlite3Exp
74ed0 72 43 68 65 63 6b 48 65 69 67 68 74 28 50 61 72  rCheckHeight(Par
74ee0 73 65 2a 2c 20 69 6e 74 29 3b 0a 23 65 6c 73 65  se*, int);.#else
74ef0 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74  .  #define sqlit
74f00 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28  e3ExprSetHeight(
74f10 78 2c 79 29 0a 20 20 23 64 65 66 69 6e 65 20 73  x,y).  #define s
74f20 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 72  qlite3SelectExpr
74f30 48 65 69 67 68 74 28 78 29 20 30 0a 20 20 23 64  Height(x) 0.  #d
74f40 65 66 69 6e 65 20 73 71 6c 69 74 65 33 45 78 70  efine sqlite3Exp
74f50 72 43 68 65 63 6b 48 65 69 67 68 74 28 78 2c 79  rCheckHeight(x,y
74f60 29 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45  ).#endif..SQLITE
74f70 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71 6c  _PRIVATE u32 sql
74f80 69 74 65 33 47 65 74 34 62 79 74 65 28 63 6f 6e  ite3Get4byte(con
74f90 73 74 20 75 38 2a 29 3b 0a 53 51 4c 49 54 45 5f  st u8*);.SQLITE_
74fa0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
74fb0 69 74 65 33 50 75 74 34 62 79 74 65 28 75 38 2a  ite3Put4byte(u8*
74fc0 2c 20 75 33 32 29 3b 0a 0a 23 69 66 64 65 66 20  , u32);..#ifdef 
74fd0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 4e  SQLITE_ENABLE_UN
74fe0 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0a 53 51 4c 49  LOCK_NOTIFY.SQLI
74ff0 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
75000 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74  d sqlite3Connect
75010 69 6f 6e 42 6c 6f 63 6b 65 64 28 73 71 6c 69 74  ionBlocked(sqlit
75020 65 33 20 2a 2c 20 73 71 6c 69 74 65 33 20 2a 29  e3 *, sqlite3 *)
75030 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
75040 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43     void sqlite3C
75050 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65  onnectionUnlocke
75060 64 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a  d(sqlite3 *db);.
75070 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
75080 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6e   void sqlite3Con
75090 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64 28 73 71  nectionClosed(sq
750a0 6c 69 74 65 33 20 2a 64 62 29 3b 0a 23 65 6c 73  lite3 *db);.#els
750b0 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69  e.  #define sqli
750c0 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f  te3ConnectionBlo
750d0 63 6b 65 64 28 78 2c 79 29 0a 20 20 23 64 65 66  cked(x,y).  #def
750e0 69 6e 65 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65  ine sqlite3Conne
750f0 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 78 29  ctionUnlocked(x)
75100 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74  .  #define sqlit
75110 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73  e3ConnectionClos
75120 65 64 28 78 29 0a 23 65 6e 64 69 66 0a 0a 23 69  ed(x).#endif..#i
75130 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
75140 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  G.SQLITE_PRIVATE
75150 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50     void sqlite3P
75160 61 72 73 65 72 54 72 61 63 65 28 46 49 4c 45 2a  arserTrace(FILE*
75170 2c 20 63 68 61 72 20 2a 29 3b 0a 23 65 6e 64 69  , char *);.#endi
75180 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20  f../*.** If the 
75190 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 20 49 4f  SQLITE_ENABLE IO
751a0 54 52 41 43 45 20 65 78 69 73 74 73 20 74 68 65  TRACE exists the
751b0 6e 20 74 68 65 20 67 6c 6f 62 61 6c 20 76 61 72  n the global var
751c0 69 61 62 6c 65 0a 2a 2a 20 73 71 6c 69 74 65 33  iable.** sqlite3
751d0 49 6f 54 72 61 63 65 20 69 73 20 61 20 70 6f 69  IoTrace is a poi
751e0 6e 74 65 72 20 74 6f 20 61 20 70 72 69 6e 74 66  nter to a printf
751f0 2d 6c 69 6b 65 20 72 6f 75 74 69 6e 65 20 75 73  -like routine us
75200 65 64 20 74 6f 0a 2a 2a 20 70 72 69 6e 74 20 49  ed to.** print I
75210 2f 4f 20 74 72 61 63 69 6e 67 20 6d 65 73 73 61  /O tracing messa
75220 67 65 73 2e 20 0a 2a 2f 0a 23 69 66 64 65 66 20  ges. .*/.#ifdef 
75230 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 4f  SQLITE_ENABLE_IO
75240 54 52 41 43 45 0a 23 20 64 65 66 69 6e 65 20 49  TRACE.# define I
75250 4f 54 52 41 43 45 28 41 29 20 20 69 66 28 20 73  OTRACE(A)  if( s
75260 71 6c 69 74 65 33 49 6f 54 72 61 63 65 20 29 7b  qlite3IoTrace ){
75270 20 73 71 6c 69 74 65 33 49 6f 54 72 61 63 65 20   sqlite3IoTrace 
75280 41 3b 20 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  A; }.SQLITE_PRIV
75290 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
752a0 65 33 56 64 62 65 49 4f 54 72 61 63 65 53 71 6c  e3VdbeIOTraceSql
752b0 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  (Vdbe*);.SQLITE_
752c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 28 2a 73  PRIVATE void (*s
752d0 71 6c 69 74 65 33 49 6f 54 72 61 63 65 29 28 63  qlite3IoTrace)(c
752e0 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b  onst char*,...);
752f0 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
75300 49 4f 54 52 41 43 45 28 41 29 0a 23 20 64 65 66  IOTRACE(A).# def
75310 69 6e 65 20 73 71 6c 69 74 65 33 56 64 62 65 49  ine sqlite3VdbeI
75320 4f 54 72 61 63 65 53 71 6c 28 58 29 0a 23 65 6e  OTraceSql(X).#en
75330 64 69 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a  dif..#endif../**
75340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
75350 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20   of sqliteInt.h 
75360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
75370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
75380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
75390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
753a0 69 6e 20 66 69 6c 65 20 67 6c 6f 62 61 6c 2e 63  in file global.c
753b0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
753c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
753d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
753e0 2a 2a 20 32 30 30 38 20 4a 75 6e 65 20 31 33 0a  ** 2008 June 13.
753f0 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
75400 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
75410 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
75420 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
75430 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
75440 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
75450 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
75460 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
75470 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
75480 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
75490 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
754a0 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
754b0 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
754c0 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
754d0 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
754e0 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
754f0 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
75500 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
75510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
75520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
75530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
75540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
75550 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
75560 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e 69 74 69  ontains definiti
75570 6f 6e 73 20 6f 66 20 67 6c 6f 62 61 6c 20 76 61  ons of global va
75580 72 69 61 62 6c 65 73 20 61 6e 64 20 63 6f 6e 74  riables and cont
75590 61 6e 74 73 2e 0a 2a 2f 0a 0a 0a 2f 2a 20 41 6e  ants..*/.../* An
755a0 20 61 72 72 61 79 20 74 6f 20 6d 61 70 20 61 6c   array to map al
755b0 6c 20 75 70 70 65 72 2d 63 61 73 65 20 63 68 61  l upper-case cha
755c0 72 61 63 74 65 72 73 20 69 6e 74 6f 20 74 68 65  racters into the
755d0 69 72 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  ir corresponding
755e0 0a 2a 2a 20 6c 6f 77 65 72 2d 63 61 73 65 20 63  .** lower-case c
755f0 68 61 72 61 63 74 65 72 2e 20 0a 2a 2a 0a 2a 2a  haracter. .**.**
75600 20 53 51 4c 69 74 65 20 6f 6e 6c 79 20 63 6f 6e   SQLite only con
75610 73 69 64 65 72 73 20 55 53 2d 41 53 43 49 49 20  siders US-ASCII 
75620 28 6f 72 20 45 42 43 44 49 43 29 20 63 68 61 72  (or EBCDIC) char
75630 61 63 74 65 72 73 2e 20 20 57 65 20 64 6f 20 6e  acters.  We do n
75640 6f 74 0a 2a 2a 20 68 61 6e 64 6c 65 20 63 61 73  ot.** handle cas
75650 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 66 6f  e conversions fo
75660 72 20 74 68 65 20 55 54 46 20 63 68 61 72 61 63  r the UTF charac
75670 74 65 72 20 73 65 74 20 73 69 6e 63 65 20 74 68  ter set since th
75680 65 20 74 61 62 6c 65 73 0a 2a 2a 20 69 6e 76 6f  e tables.** invo
75690 6c 76 65 64 20 61 72 65 20 6e 65 61 72 6c 79 20  lved are nearly 
756a0 61 73 20 62 69 67 20 6f 72 20 62 69 67 67 65 72  as big or bigger
756b0 20 74 68 61 6e 20 53 51 4c 69 74 65 20 69 74 73   than SQLite its
756c0 65 6c 66 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  elf..*/.SQLITE_P
756d0 52 49 56 41 54 45 20 63 6f 6e 73 74 20 75 6e 73  RIVATE const uns
756e0 69 67 6e 65 64 20 63 68 61 72 20 73 71 6c 69 74  igned char sqlit
756f0 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 5d  e3UpperToLower[]
75700 20 3d 20 7b 0a 23 69 66 64 65 66 20 53 51 4c 49   = {.#ifdef SQLI
75710 54 45 5f 41 53 43 49 49 0a 20 20 20 20 20 20 30  TE_ASCII.      0
75720 2c 20 20 31 2c 20 20 32 2c 20 20 33 2c 20 20 34  ,  1,  2,  3,  4
75730 2c 20 20 35 2c 20 20 36 2c 20 20 37 2c 20 20 38  ,  5,  6,  7,  8
75740 2c 20 20 39 2c 20 31 30 2c 20 31 31 2c 20 31 32  ,  9, 10, 11, 12
75750 2c 20 31 33 2c 20 31 34 2c 20 31 35 2c 20 31 36  , 13, 14, 15, 16
75760 2c 20 31 37 2c 0a 20 20 20 20 20 31 38 2c 20 31  , 17,.     18, 1
75770 39 2c 20 32 30 2c 20 32 31 2c 20 32 32 2c 20 32  9, 20, 21, 22, 2
75780 33 2c 20 32 34 2c 20 32 35 2c 20 32 36 2c 20 32  3, 24, 25, 26, 2
75790 37 2c 20 32 38 2c 20 32 39 2c 20 33 30 2c 20 33  7, 28, 29, 30, 3
757a0 31 2c 20 33 32 2c 20 33 33 2c 20 33 34 2c 20 33  1, 32, 33, 34, 3
757b0 35 2c 0a 20 20 20 20 20 33 36 2c 20 33 37 2c 20  5,.     36, 37, 
757c0 33 38 2c 20 33 39 2c 20 34 30 2c 20 34 31 2c 20  38, 39, 40, 41, 
757d0 34 32 2c 20 34 33 2c 20 34 34 2c 20 34 35 2c 20  42, 43, 44, 45, 
757e0 34 36 2c 20 34 37 2c 20 34 38 2c 20 34 39 2c 20  46, 47, 48, 49, 
757f0 35 30 2c 20 35 31 2c 20 35 32 2c 20 35 33 2c 0a  50, 51, 52, 53,.
75800 20 20 20 20 20 35 34 2c 20 35 35 2c 20 35 36 2c       54, 55, 56,
75810 20 35 37 2c 20 35 38 2c 20 35 39 2c 20 36 30 2c   57, 58, 59, 60,
75820 20 36 31 2c 20 36 32 2c 20 36 33 2c 20 36 34 2c   61, 62, 63, 64,
75830 20 39 37 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c   97, 98, 99,100,
75840 31 30 31 2c 31 30 32 2c 31 30 33 2c 0a 20 20 20  101,102,103,.   
75850 20 31 30 34 2c 31 30 35 2c 31 30 36 2c 31 30 37   104,105,106,107
75860 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c 31 31 31  ,108,109,110,111
75870 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c 31 31 35  ,112,113,114,115
75880 2c 31 31 36 2c 31 31 37 2c 31 31 38 2c 31 31 39  ,116,117,118,119
75890 2c 31 32 30 2c 31 32 31 2c 0a 20 20 20 20 31 32  ,120,121,.    12
758a0 32 2c 20 39 31 2c 20 39 32 2c 20 39 33 2c 20 39  2, 91, 92, 93, 9
758b0 34 2c 20 39 35 2c 20 39 36 2c 20 39 37 2c 20 39  4, 95, 96, 97, 9
758c0 38 2c 20 39 39 2c 31 30 30 2c 31 30 31 2c 31 30  8, 99,100,101,10
758d0 32 2c 31 30 33 2c 31 30 34 2c 31 30 35 2c 31 30  2,103,104,105,10
758e0 36 2c 31 30 37 2c 0a 20 20 20 20 31 30 38 2c 31  6,107,.    108,1
758f0 30 39 2c 31 31 30 2c 31 31 31 2c 31 31 32 2c 31  09,110,111,112,1
75900 31 33 2c 31 31 34 2c 31 31 35 2c 31 31 36 2c 31  13,114,115,116,1
75910 31 37 2c 31 31 38 2c 31 31 39 2c 31 32 30 2c 31  17,118,119,120,1
75920 32 31 2c 31 32 32 2c 31 32 33 2c 31 32 34 2c 31  21,122,123,124,1
75930 32 35 2c 0a 20 20 20 20 31 32 36 2c 31 32 37 2c  25,.    126,127,
75940 31 32 38 2c 31 32 39 2c 31 33 30 2c 31 33 31 2c  128,129,130,131,
75950 31 33 32 2c 31 33 33 2c 31 33 34 2c 31 33 35 2c  132,133,134,135,
75960 31 33 36 2c 31 33 37 2c 31 33 38 2c 31 33 39 2c  136,137,138,139,
75970 31 34 30 2c 31 34 31 2c 31 34 32 2c 31 34 33 2c  140,141,142,143,
75980 0a 20 20 20 20 31 34 34 2c 31 34 35 2c 31 34 36  .    144,145,146
75990 2c 31 34 37 2c 31 34 38 2c 31 34 39 2c 31 35 30  ,147,148,149,150
759a0 2c 31 35 31 2c 31 35 32 2c 31 35 33 2c 31 35 34  ,151,152,153,154
759b0 2c 31 35 35 2c 31 35 36 2c 31 35 37 2c 31 35 38  ,155,156,157,158
759c0 2c 31 35 39 2c 31 36 30 2c 31 36 31 2c 0a 20 20  ,159,160,161,.  
759d0 20 20 31 36 32 2c 31 36 33 2c 31 36 34 2c 31 36    162,163,164,16
759e0 35 2c 31 36 36 2c 31 36 37 2c 31 36 38 2c 31 36  5,166,167,168,16
759f0 39 2c 31 37 30 2c 31 37 31 2c 31 37 32 2c 31 37  9,170,171,172,17
75a00 33 2c 31 37 34 2c 31 37 35 2c 31 37 36 2c 31 37  3,174,175,176,17
75a10 37 2c 31 37 38 2c 31 37 39 2c 0a 20 20 20 20 31  7,178,179,.    1
75a20 38 30 2c 31 38 31 2c 31 38 32 2c 31 38 33 2c 31  80,181,182,183,1
75a30 38 34 2c 31 38 35 2c 31 38 36 2c 31 38 37 2c 31  84,185,186,187,1
75a40 38 38 2c 31 38 39 2c 31 39 30 2c 31 39 31 2c 31  88,189,190,191,1
75a50 39 32 2c 31 39 33 2c 31 39 34 2c 31 39 35 2c 31  92,193,194,195,1
75a60 39 36 2c 31 39 37 2c 0a 20 20 20 20 31 39 38 2c  96,197,.    198,
75a70 31 39 39 2c 32 30 30 2c 32 30 31 2c 32 30 32 2c  199,200,201,202,
75a80 32 30 33 2c 32 30 34 2c 32 30 35 2c 32 30 36 2c  203,204,205,206,
75a90 32 30 37 2c 32 30 38 2c 32 30 39 2c 32 31 30 2c  207,208,209,210,
75aa0 32 31 31 2c 32 31 32 2c 32 31 33 2c 32 31 34 2c  211,212,213,214,
75ab0 32 31 35 2c 0a 20 20 20 20 32 31 36 2c 32 31 37  215,.    216,217
75ac0 2c 32 31 38 2c 32 31 39 2c 32 32 30 2c 32 32 31  ,218,219,220,221
75ad0 2c 32 32 32 2c 32 32 33 2c 32 32 34 2c 32 32 35  ,222,223,224,225
75ae0 2c 32 32 36 2c 32 32 37 2c 32 32 38 2c 32 32 39  ,226,227,228,229
75af0 2c 32 33 30 2c 32 33 31 2c 32 33 32 2c 32 33 33  ,230,231,232,233
75b00 2c 0a 20 20 20 20 32 33 34 2c 32 33 35 2c 32 33  ,.    234,235,23
75b10 36 2c 32 33 37 2c 32 33 38 2c 32 33 39 2c 32 34  6,237,238,239,24
75b20 30 2c 32 34 31 2c 32 34 32 2c 32 34 33 2c 32 34  0,241,242,243,24
75b30 34 2c 32 34 35 2c 32 34 36 2c 32 34 37 2c 32 34  4,245,246,247,24
75b40 38 2c 32 34 39 2c 32 35 30 2c 32 35 31 2c 0a 20  8,249,250,251,. 
75b50 20 20 20 32 35 32 2c 32 35 33 2c 32 35 34 2c 32     252,253,254,2
75b60 35 35 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66  55.#endif.#ifdef
75b70 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43 0a 20   SQLITE_EBCDIC. 
75b80 20 20 20 20 20 30 2c 20 20 31 2c 20 20 32 2c 20       0,  1,  2, 
75b90 20 33 2c 20 20 34 2c 20 20 35 2c 20 20 36 2c 20   3,  4,  5,  6, 
75ba0 20 37 2c 20 20 38 2c 20 20 39 2c 20 31 30 2c 20   7,  8,  9, 10, 
75bb0 31 31 2c 20 31 32 2c 20 31 33 2c 20 31 34 2c 20  11, 12, 13, 14, 
75bc0 31 35 2c 20 2f 2a 20 30 78 20 2a 2f 0a 20 20 20  15, /* 0x */.   
75bd0 20 20 31 36 2c 20 31 37 2c 20 31 38 2c 20 31 39    16, 17, 18, 19
75be0 2c 20 32 30 2c 20 32 31 2c 20 32 32 2c 20 32 33  , 20, 21, 22, 23
75bf0 2c 20 32 34 2c 20 32 35 2c 20 32 36 2c 20 32 37  , 24, 25, 26, 27
75c00 2c 20 32 38 2c 20 32 39 2c 20 33 30 2c 20 33 31  , 28, 29, 30, 31
75c10 2c 20 2f 2a 20 31 78 20 2a 2f 0a 20 20 20 20 20  , /* 1x */.     
75c20 33 32 2c 20 33 33 2c 20 33 34 2c 20 33 35 2c 20  32, 33, 34, 35, 
75c30 33 36 2c 20 33 37 2c 20 33 38 2c 20 33 39 2c 20  36, 37, 38, 39, 
75c40 34 30 2c 20 34 31 2c 20 34 32 2c 20 34 33 2c 20  40, 41, 42, 43, 
75c50 34 34 2c 20 34 35 2c 20 34 36 2c 20 34 37 2c 20  44, 45, 46, 47, 
75c60 2f 2a 20 32 78 20 2a 2f 0a 20 20 20 20 20 34 38  /* 2x */.     48
75c70 2c 20 34 39 2c 20 35 30 2c 20 35 31 2c 20 35 32  , 49, 50, 51, 52
75c80 2c 20 35 33 2c 20 35 34 2c 20 35 35 2c 20 35 36  , 53, 54, 55, 56
75c90 2c 20 35 37 2c 20 35 38 2c 20 35 39 2c 20 36 30  , 57, 58, 59, 60
75ca0 2c 20 36 31 2c 20 36 32 2c 20 36 33 2c 20 2f 2a  , 61, 62, 63, /*
75cb0 20 33 78 20 2a 2f 0a 20 20 20 20 20 36 34 2c 20   3x */.     64, 
75cc0 36 35 2c 20 36 36 2c 20 36 37 2c 20 36 38 2c 20  65, 66, 67, 68, 
75cd0 36 39 2c 20 37 30 2c 20 37 31 2c 20 37 32 2c 20  69, 70, 71, 72, 
75ce0 37 33 2c 20 37 34 2c 20 37 35 2c 20 37 36 2c 20  73, 74, 75, 76, 
75cf0 37 37 2c 20 37 38 2c 20 37 39 2c 20 2f 2a 20 34  77, 78, 79, /* 4
75d00 78 20 2a 2f 0a 20 20 20 20 20 38 30 2c 20 38 31  x */.     80, 81
75d10 2c 20 38 32 2c 20 38 33 2c 20 38 34 2c 20 38 35  , 82, 83, 84, 85
75d20 2c 20 38 36 2c 20 38 37 2c 20 38 38 2c 20 38 39  , 86, 87, 88, 89
75d30 2c 20 39 30 2c 20 39 31 2c 20 39 32 2c 20 39 33  , 90, 91, 92, 93
75d40 2c 20 39 34 2c 20 39 35 2c 20 2f 2a 20 35 78 20  , 94, 95, /* 5x 
75d50 2a 2f 0a 20 20 20 20 20 39 36 2c 20 39 37 2c 20  */.     96, 97, 
75d60 36 36 2c 20 36 37 2c 20 36 38 2c 20 36 39 2c 20  66, 67, 68, 69, 
75d70 37 30 2c 20 37 31 2c 20 37 32 2c 20 37 33 2c 31  70, 71, 72, 73,1
75d80 30 36 2c 31 30 37 2c 31 30 38 2c 31 30 39 2c 31  06,107,108,109,1
75d90 31 30 2c 31 31 31 2c 20 2f 2a 20 36 78 20 2a 2f  10,111, /* 6x */
75da0 0a 20 20 20 20 31 31 32 2c 20 38 31 2c 20 38 32  .    112, 81, 82
75db0 2c 20 38 33 2c 20 38 34 2c 20 38 35 2c 20 38 36  , 83, 84, 85, 86
75dc0 2c 20 38 37 2c 20 38 38 2c 20 38 39 2c 31 32 32  , 87, 88, 89,122
75dd0 2c 31 32 33 2c 31 32 34 2c 31 32 35 2c 31 32 36  ,123,124,125,126
75de0 2c 31 32 37 2c 20 2f 2a 20 37 78 20 2a 2f 0a 20  ,127, /* 7x */. 
75df0 20 20 20 31 32 38 2c 31 32 39 2c 31 33 30 2c 31     128,129,130,1
75e00 33 31 2c 31 33 32 2c 31 33 33 2c 31 33 34 2c 31  31,132,133,134,1
75e10 33 35 2c 31 33 36 2c 31 33 37 2c 31 33 38 2c 31  35,136,137,138,1
75e20 33 39 2c 31 34 30 2c 31 34 31 2c 31 34 32 2c 31  39,140,141,142,1
75e30 34 33 2c 20 2f 2a 20 38 78 20 2a 2f 0a 20 20 20  43, /* 8x */.   
75e40 20 31 34 34 2c 31 34 35 2c 31 34 36 2c 31 34 37   144,145,146,147
75e50 2c 31 34 38 2c 31 34 39 2c 31 35 30 2c 31 35 31  ,148,149,150,151
75e60 2c 31 35 32 2c 31 35 33 2c 31 35 34 2c 31 35 35  ,152,153,154,155
75e70 2c 31 35 36 2c 31 35 37 2c 31 35 36 2c 31 35 39  ,156,157,156,159
75e80 2c 20 2f 2a 20 39 78 20 2a 2f 0a 20 20 20 20 31  , /* 9x */.    1
75e90 36 30 2c 31 36 31 2c 31 36 32 2c 31 36 33 2c 31  60,161,162,163,1
75ea0 36 34 2c 31 36 35 2c 31 36 36 2c 31 36 37 2c 31  64,165,166,167,1
75eb0 36 38 2c 31 36 39 2c 31 37 30 2c 31 37 31 2c 31  68,169,170,171,1
75ec0 34 30 2c 31 34 31 2c 31 34 32 2c 31 37 35 2c 20  40,141,142,175, 
75ed0 2f 2a 20 41 78 20 2a 2f 0a 20 20 20 20 31 37 36  /* Ax */.    176
75ee0 2c 31 37 37 2c 31 37 38 2c 31 37 39 2c 31 38 30  ,177,178,179,180
75ef0 2c 31 38 31 2c 31 38 32 2c 31 38 33 2c 31 38 34  ,181,182,183,184
75f00 2c 31 38 35 2c 31 38 36 2c 31 38 37 2c 31 38 38  ,185,186,187,188
75f10 2c 31 38 39 2c 31 39 30 2c 31 39 31 2c 20 2f 2a  ,189,190,191, /*
75f20 20 42 78 20 2a 2f 0a 20 20 20 20 31 39 32 2c 31   Bx */.    192,1
75f30 32 39 2c 31 33 30 2c 31 33 31 2c 31 33 32 2c 31  29,130,131,132,1
75f40 33 33 2c 31 33 34 2c 31 33 35 2c 31 33 36 2c 31  33,134,135,136,1
75f50 33 37 2c 32 30 32 2c 32 30 33 2c 32 30 34 2c 32  37,202,203,204,2
75f60 30 35 2c 32 30 36 2c 32 30 37 2c 20 2f 2a 20 43  05,206,207, /* C
75f70 78 20 2a 2f 0a 20 20 20 20 32 30 38 2c 31 34 35  x */.    208,145
75f80 2c 31 34 36 2c 31 34 37 2c 31 34 38 2c 31 34 39  ,146,147,148,149
75f90 2c 31 35 30 2c 31 35 31 2c 31 35 32 2c 31 35 33  ,150,151,152,153
75fa0 2c 32 31 38 2c 32 31 39 2c 32 32 30 2c 32 32 31  ,218,219,220,221
75fb0 2c 32 32 32 2c 32 32 33 2c 20 2f 2a 20 44 78 20  ,222,223, /* Dx 
75fc0 2a 2f 0a 20 20 20 20 32 32 34 2c 32 32 35 2c 31  */.    224,225,1
75fd0 36 32 2c 31 36 33 2c 31 36 34 2c 31 36 35 2c 31  62,163,164,165,1
75fe0 36 36 2c 31 36 37 2c 31 36 38 2c 31 36 39 2c 32  66,167,168,169,2
75ff0 33 32 2c 32 30 33 2c 32 30 34 2c 32 30 35 2c 32  32,203,204,205,2
76000 30 36 2c 32 30 37 2c 20 2f 2a 20 45 78 20 2a 2f  06,207, /* Ex */
76010 0a 20 20 20 20 32 33 39 2c 32 34 30 2c 32 34 31  .    239,240,241
76020 2c 32 34 32 2c 32 34 33 2c 32 34 34 2c 32 34 35  ,242,243,244,245
76030 2c 32 34 36 2c 32 34 37 2c 32 34 38 2c 32 34 39  ,246,247,248,249
76040 2c 32 31 39 2c 32 32 30 2c 32 32 31 2c 32 32 32  ,219,220,221,222
76050 2c 32 35 35 2c 20 2f 2a 20 46 78 20 2a 2f 0a 23  ,255, /* Fx */.#
76060 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  endif.};../*.** 
76070 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 32 35  The following 25
76080 36 20 62 79 74 65 20 6c 6f 6f 6b 75 70 20 74 61  6 byte lookup ta
76090 62 6c 65 20 69 73 20 75 73 65 64 20 74 6f 20 73  ble is used to s
760a0 75 70 70 6f 72 74 20 53 51 4c 69 74 65 73 20 62  upport SQLites b
760b0 75 69 6c 74 2d 69 6e 0a 2a 2a 20 65 71 75 69 76  uilt-in.** equiv
760c0 61 6c 65 6e 74 73 20 74 6f 20 74 68 65 20 66 6f  alents to the fo
760d0 6c 6c 6f 77 69 6e 67 20 73 74 61 6e 64 61 72 64  llowing standard
760e0 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f   library functio
760f0 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 73 73 70  ns:.**.**   issp
76100 61 63 65 28 29 20 20 20 20 20 20 20 20 20 20 20  ace()           
76110 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30               0x0
76120 31 0a 2a 2a 20 20 20 69 73 61 6c 70 68 61 28 29  1.**   isalpha()
76130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
76140 20 20 20 20 20 20 20 20 30 78 30 32 0a 2a 2a 20          0x02.** 
76150 20 20 69 73 64 69 67 69 74 28 29 20 20 20 20 20    isdigit()     
76160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
76170 20 20 20 30 78 30 34 0a 2a 2a 20 20 20 69 73 61     0x04.**   isa
76180 6c 6e 75 6d 28 29 20 20 20 20 20 20 20 20 20 20  lnum()          
76190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78                0x
761a0 30 36 0a 2a 2a 20 20 20 69 73 78 64 69 67 69 74  06.**   isxdigit
761b0 28 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ()              
761c0 20 20 20 20 20 20 20 20 20 30 78 30 38 0a 2a 2a           0x08.**
761d0 20 20 20 74 6f 75 70 70 65 72 28 29 20 20 20 20     toupper()    
761e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
761f0 20 20 20 20 30 78 32 30 0a 2a 2a 0a 2a 2a 20 42      0x20.**.** B
76200 69 74 20 30 78 32 30 20 69 73 20 73 65 74 20 69  it 0x20 is set i
76210 66 20 74 68 65 20 6d 61 70 70 65 64 20 63 68 61  f the mapped cha
76220 72 61 63 74 65 72 20 72 65 71 75 69 72 65 73 20  racter requires 
76230 74 72 61 6e 73 6c 61 74 69 6f 6e 20 74 6f 20 75  translation to u
76240 70 70 65 72 0a 2a 2a 20 63 61 73 65 2e 20 69 2e  pper.** case. i.
76250 65 2e 20 69 66 20 74 68 65 20 63 68 61 72 61 63  e. if the charac
76260 74 65 72 20 69 73 20 61 20 6c 6f 77 65 72 2d 63  ter is a lower-c
76270 61 73 65 20 41 53 43 49 49 20 63 68 61 72 61 63  ase ASCII charac
76280 74 65 72 2e 0a 2a 2a 20 49 66 20 78 20 69 73 20  ter..** If x is 
76290 61 20 6c 6f 77 65 72 2d 63 61 73 65 20 41 53 43  a lower-case ASC
762a0 49 49 20 63 68 61 72 61 63 74 65 72 2c 20 74 68  II character, th
762b0 65 6e 20 69 74 73 20 75 70 70 65 72 2d 63 61 73  en its upper-cas
762c0 65 20 65 71 75 69 76 61 6c 65 6e 74 0a 2a 2a 20  e equivalent.** 
762d0 69 73 20 28 78 20 2d 20 30 78 32 30 29 2e 20 54  is (x - 0x20). T
762e0 68 65 72 65 66 6f 72 65 20 74 6f 75 70 70 65 72  herefore toupper
762f0 28 29 20 63 61 6e 20 62 65 20 69 6d 70 6c 65 6d  () can be implem
76300 65 6e 74 65 64 20 61 73 3a 0a 2a 2a 0a 2a 2a 20  ented as:.**.** 
76310 20 20 28 78 20 26 20 7e 28 6d 61 70 5b 78 5d 26    (x & ~(map[x]&
76320 30 78 32 30 29 29 0a 2a 2a 0a 2a 2a 20 53 74 61  0x20)).**.** Sta
76330 6e 64 61 72 64 20 66 75 6e 63 74 69 6f 6e 20 74  ndard function t
76340 6f 6c 6f 77 65 72 28 29 20 69 73 20 69 6d 70 6c  olower() is impl
76350 65 6d 65 6e 74 65 64 20 75 73 69 6e 67 20 74 68  emented using th
76360 65 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f  e sqlite3UpperTo
76370 4c 6f 77 65 72 5b 5d 0a 2a 2a 20 61 72 72 61 79  Lower[].** array
76380 2e 20 74 6f 6c 6f 77 65 72 28 29 20 69 73 20 75  . tolower() is u
76390 73 65 64 20 6d 6f 72 65 20 6f 66 74 65 6e 20 74  sed more often t
763a0 68 61 6e 20 74 6f 75 70 70 65 72 28 29 20 62 79  han toupper() by
763b0 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 53   SQLite..**.** S
763c0 51 4c 69 74 65 27 73 20 76 65 72 73 69 6f 6e 73  QLite's versions
763d0 20 61 72 65 20 69 64 65 6e 74 69 63 61 6c 20 74   are identical t
763e0 6f 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 76  o the standard v
763f0 65 72 73 69 6f 6e 73 20 61 73 73 75 6d 69 6e 67  ersions assuming
76400 20 61 0a 2a 2a 20 6c 6f 63 61 6c 65 20 6f 66 20   a.** locale of 
76410 22 43 22 2e 20 54 68 65 79 20 61 72 65 20 69 6d  "C". They are im
76420 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 6d 61 63  plemented as mac
76430 72 6f 73 20 69 6e 20 73 71 6c 69 74 65 49 6e 74  ros in sqliteInt
76440 2e 68 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  .h..*/.#ifdef SQ
76450 4c 49 54 45 5f 41 53 43 49 49 0a 53 51 4c 49 54  LITE_ASCII.SQLIT
76460 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
76470 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 71  unsigned char sq
76480 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b 32 35  lite3CtypeMap[25
76490 36 5d 20 3d 20 7b 0a 20 20 30 78 30 30 2c 20 30  6] = {.  0x00, 0
764a0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
764b0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
764c0 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 30 30 2e  0, 0x00,  /* 00.
764d0 2e 30 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20  .07    ........ 
764e0 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 31 2c  */.  0x00, 0x01,
764f0 20 30 78 30 31 2c 20 30 78 30 31 2c 20 30 78 30   0x01, 0x01, 0x0
76500 31 2c 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30  1, 0x01, 0x00, 0
76510 78 30 30 2c 20 20 2f 2a 20 30 38 2e 2e 30 66 20  x00,  /* 08..0f 
76520 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20     ........ */. 
76530 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
76540 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
76550 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
76560 20 20 2f 2a 20 31 30 2e 2e 31 37 20 20 20 20 2e    /* 10..17    .
76570 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30  ....... */.  0x0
76580 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
76590 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
765a0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a   0x00, 0x00,  /*
765b0 20 31 38 2e 2e 31 66 20 20 20 20 2e 2e 2e 2e 2e   18..1f    .....
765c0 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 31 2c 20 30  ... */.  0x01, 0
765d0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
765e0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
765f0 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 32 30 2e  0, 0x00,  /* 20.
76600 2e 32 37 20 20 20 20 20 21 22 23 24 25 26 27 20  .27     !"#$%&' 
76610 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c  */.  0x00, 0x00,
76620 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
76630 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
76640 78 30 30 2c 20 20 2f 2a 20 32 38 2e 2e 32 66 20  x00,  /* 28..2f 
76650 20 20 20 28 29 2a 2b 2c 2d 2e 2f 20 2a 2f 0a 20     ()*+,-./ */. 
76660 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30   0x0c, 0x0c, 0x0
76670 63 2c 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30  c, 0x0c, 0x0c, 0
76680 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 63 2c  x0c, 0x0c, 0x0c,
76690 20 20 2f 2a 20 33 30 2e 2e 33 37 20 20 20 20 30    /* 30..37    0
766a0 31 32 33 34 35 36 37 20 2a 2f 0a 20 20 30 78 30  1234567 */.  0x0
766b0 63 2c 20 30 78 30 63 2c 20 30 78 30 30 2c 20 30  c, 0x0c, 0x00, 0
766c0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
766d0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a   0x00, 0x00,  /*
766e0 20 33 38 2e 2e 33 66 20 20 20 20 38 39 3a 3b 3c   38..3f    89:;<
766f0 3d 3e 3f 20 2a 2f 0a 0a 20 20 30 78 30 30 2c 20  =>? */..  0x00, 
76700 30 78 30 61 2c 20 30 78 30 61 2c 20 30 78 30 61  0x0a, 0x0a, 0x0a
76710 2c 20 30 78 30 61 2c 20 30 78 30 61 2c 20 30 78  , 0x0a, 0x0a, 0x
76720 30 61 2c 20 30 78 30 32 2c 20 20 2f 2a 20 34 30  0a, 0x02,  /* 40
76730 2e 2e 34 37 20 20 20 20 40 41 42 43 44 45 46 47  ..47    @ABCDEFG
76740 20 2a 2f 0a 20 20 30 78 30 32 2c 20 30 78 30 32   */.  0x02, 0x02
76750 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78  , 0x02, 0x02, 0x
76760 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20  02, 0x02, 0x02, 
76770 30 78 30 32 2c 20 20 2f 2a 20 34 38 2e 2e 34 66  0x02,  /* 48..4f
76780 20 20 20 20 48 49 4a 4b 4c 4d 4e 4f 20 2a 2f 0a      HIJKLMNO */.
76790 20 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78    0x02, 0x02, 0x
767a0 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20  02, 0x02, 0x02, 
767b0 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32  0x02, 0x02, 0x02
767c0 2c 20 20 2f 2a 20 35 30 2e 2e 35 37 20 20 20 20  ,  /* 50..57    
767d0 50 51 52 53 54 55 56 57 20 2a 2f 0a 20 20 30 78  PQRSTUVW */.  0x
767e0 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20  02, 0x02, 0x02, 
767f0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
76800 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f  , 0x00, 0x00,  /
76810 2a 20 35 38 2e 2e 35 66 20 20 20 20 58 59 5a 5b  * 58..5f    XYZ[
76820 5c 5d 5e 5f 20 2a 2f 0a 20 20 30 78 30 30 2c 20  \]^_ */.  0x00, 
76830 30 78 32 61 2c 20 30 78 32 61 2c 20 30 78 32 61  0x2a, 0x2a, 0x2a
76840 2c 20 30 78 32 61 2c 20 30 78 32 61 2c 20 30 78  , 0x2a, 0x2a, 0x
76850 32 61 2c 20 30 78 32 32 2c 20 20 2f 2a 20 36 30  2a, 0x22,  /* 60
76860 2e 2e 36 37 20 20 20 20 60 61 62 63 64 65 66 67  ..67    `abcdefg
76870 20 2a 2f 0a 20 20 30 78 32 32 2c 20 30 78 32 32   */.  0x22, 0x22
76880 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78  , 0x22, 0x22, 0x
76890 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20  22, 0x22, 0x22, 
768a0 30 78 32 32 2c 20 20 2f 2a 20 36 38 2e 2e 36 66  0x22,  /* 68..6f
768b0 20 20 20 20 68 69 6a 6b 6c 6d 6e 6f 20 2a 2f 0a      hijklmno */.
768c0 20 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78    0x22, 0x22, 0x
768d0 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20  22, 0x22, 0x22, 
768e0 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32  0x22, 0x22, 0x22
768f0 2c 20 20 2f 2a 20 37 30 2e 2e 37 37 20 20 20 20  ,  /* 70..77    
76900 70 71 72 73 74 75 76 77 20 2a 2f 0a 20 20 30 78  pqrstuvw */.  0x
76910 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20  22, 0x22, 0x22, 
76920 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
76930 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f  , 0x00, 0x00,  /
76940 2a 20 37 38 2e 2e 37 66 20 20 20 20 78 79 7a 7b  * 78..7f    xyz{
76950 7c 7d 7e 2e 20 2a 2f 0a 0a 20 20 30 78 30 30 2c  |}~. */..  0x00,
76960 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
76970 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
76980 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 38  x00, 0x00,  /* 8
76990 30 2e 2e 38 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e  0..87    .......
769a0 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30  . */.  0x00, 0x0
769b0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
769c0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
769d0 20 30 78 30 30 2c 20 20 2f 2a 20 38 38 2e 2e 38   0x00,  /* 88..8
769e0 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f  f    ........ */
769f0 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  .  0x00, 0x00, 0
76a00 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
76a10 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
76a20 30 2c 20 20 2f 2a 20 39 30 2e 2e 39 37 20 20 20  0,  /* 90..97   
76a30 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30   ........ */.  0
76a40 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
76a50 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
76a60 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20  0, 0x00, 0x00,  
76a70 2f 2a 20 39 38 2e 2e 39 66 20 20 20 20 2e 2e 2e  /* 98..9f    ...
76a80 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c  ..... */.  0x00,
76a90 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
76aa0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
76ab0 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 61  x00, 0x00,  /* a
76ac0 30 2e 2e 61 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e  0..a7    .......
76ad0 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30  . */.  0x00, 0x0
76ae0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
76af0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
76b00 20 30 78 30 30 2c 20 20 2f 2a 20 61 38 2e 2e 61   0x00,  /* a8..a
76b10 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f  f    ........ */
76b20 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  .  0x00, 0x00, 0
76b30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
76b40 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
76b50 30 2c 20 20 2f 2a 20 62 30 2e 2e 62 37 20 20 20  0,  /* b0..b7   
76b60 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30   ........ */.  0
76b70 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
76b80 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
76b90 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20  0, 0x00, 0x00,  
76ba0 2f 2a 20 62 38 2e 2e 62 66 20 20 20 20 2e 2e 2e  /* b8..bf    ...
76bb0 2e 2e 2e 2e 2e 20 2a 2f 0a 0a 20 20 30 78 30 30  ..... */..  0x00
76bc0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
76bd0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
76be0 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20  0x00, 0x00,  /* 
76bf0 63 30 2e 2e 63 37 20 20 20 20 2e 2e 2e 2e 2e 2e  c0..c7    ......
76c00 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78  .. */.  0x00, 0x
76c10 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
76c20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
76c30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 63 38 2e 2e  , 0x00,  /* c8..
76c40 63 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a  cf    ........ *
76c50 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20  /.  0x00, 0x00, 
76c60 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
76c70 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
76c80 30 30 2c 20 20 2f 2a 20 64 30 2e 2e 64 37 20 20  00,  /* d0..d7  
76c90 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20    ........ */.  
76ca0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
76cb0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
76cc0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
76cd0 20 2f 2a 20 64 38 2e 2e 64 66 20 20 20 20 2e 2e   /* d8..df    ..
76ce0 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30  ...... */.  0x00
76cf0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
76d00 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
76d10 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20  0x00, 0x00,  /* 
76d20 65 30 2e 2e 65 37 20 20 20 20 2e 2e 2e 2e 2e 2e  e0..e7    ......
76d30 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78  .. */.  0x00, 0x
76d40 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
76d50 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
76d60 2c 20 30 78 30 30 2c 20 20 2f 2a 20 65 38 2e 2e  , 0x00,  /* e8..
76d70 65 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a  ef    ........ *
76d80 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20  /.  0x00, 0x00, 
76d90 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
76da0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
76db0 30 30 2c 20 20 2f 2a 20 66 30 2e 2e 66 37 20 20  00,  /* f0..f7  
76dc0 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20    ........ */.  
76dd0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
76de0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
76df0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 20 20  00, 0x00, 0x00  
76e00 20 2f 2a 20 66 38 2e 2e 66 66 20 20 20 20 2e 2e   /* f8..ff    ..
76e10 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 7d 3b 0a 23 65 6e  ...... */.};.#en
76e20 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  dif..../*.** The
76e30 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 69 6e 67 6c   following singl
76e40 65 74 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68  eton contains th
76e50 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 75  e global configu
76e60 72 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 74 68  ration for.** th
76e70 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79  e SQLite library
76e80 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
76e90 41 54 45 20 53 51 4c 49 54 45 5f 57 53 44 20 73  ATE SQLITE_WSD s
76ea0 74 72 75 63 74 20 53 71 6c 69 74 65 33 43 6f 6e  truct Sqlite3Con
76eb0 66 69 67 20 73 71 6c 69 74 65 33 43 6f 6e 66 69  fig sqlite3Confi
76ec0 67 20 3d 20 7b 0a 20 20 20 53 51 4c 49 54 45 5f  g = {.   SQLITE_
76ed0 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54 41 54 55  DEFAULT_MEMSTATU
76ee0 53 2c 20 20 2f 2a 20 62 4d 65 6d 73 74 61 74 20  S,  /* bMemstat 
76ef0 2a 2f 0a 20 20 20 31 2c 20 20 20 20 20 20 20 20  */.   1,        
76f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
76f10 20 2f 2a 20 62 43 6f 72 65 4d 75 74 65 78 20 2a   /* bCoreMutex *
76f20 2f 0a 20 20 20 53 51 4c 49 54 45 5f 54 48 52 45  /.   SQLITE_THRE
76f30 41 44 53 41 46 45 3d 3d 31 2c 20 20 20 20 20 20  ADSAFE==1,      
76f40 2f 2a 20 62 46 75 6c 6c 4d 75 74 65 78 20 2a 2f  /* bFullMutex */
76f50 0a 20 20 20 30 78 37 66 66 66 66 66 66 65 2c 20  .   0x7ffffffe, 
76f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
76f70 2a 20 6d 78 53 74 72 6c 65 6e 20 2a 2f 0a 20 20  * mxStrlen */.  
76f80 20 31 30 30 2c 20 20 20 20 20 20 20 20 20 20 20   100,           
76f90 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
76fa0 7a 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a 20 20  zLookaside */.  
76fb0 20 35 30 30 2c 20 20 20 20 20 20 20 20 20 20 20   500,           
76fc0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e              /* n
76fd0 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a 20 20 20  Lookaside */.   
76fe0 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30  {0,0,0,0,0,0,0,0
76ff0 7d 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 20  },         /* m 
77000 2a 2f 0a 20 20 20 7b 30 2c 30 2c 30 2c 30 2c 30  */.   {0,0,0,0,0
77010 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20 20 20 20 20  ,0,0,0,0},      
77020 20 2f 2a 20 6d 75 74 65 78 20 2a 2f 0a 20 20 20   /* mutex */.   
77030 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30  {0,0,0,0,0,0,0,0
77040 2c 30 2c 30 2c 30 7d 2c 20 20 20 2f 2a 20 70 63  ,0,0,0},   /* pc
77050 61 63 68 65 20 2a 2f 0a 20 20 20 28 76 6f 69 64  ache */.   (void
77060 2a 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20  *)0,            
77070 20 20 20 20 20 20 2f 2a 20 70 48 65 61 70 20 2a        /* pHeap *
77080 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20  /.   0,         
77090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
770a0 2f 2a 20 6e 48 65 61 70 20 2a 2f 0a 20 20 20 30  /* nHeap */.   0
770b0 2c 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20  , 0,            
770c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 6e 48            /* mnH
770d0 65 61 70 2c 20 6d 78 48 65 61 70 20 2a 2f 0a 20  eap, mxHeap */. 
770e0 20 20 28 76 6f 69 64 2a 29 30 2c 20 20 20 20 20    (void*)0,     
770f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
77100 70 53 63 72 61 74 63 68 20 2a 2f 0a 20 20 20 30  pScratch */.   0
77110 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
77120 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 7a 53            /* szS
77130 63 72 61 74 63 68 20 2a 2f 0a 20 20 20 30 2c 20  cratch */.   0, 
77140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77150 20 20 20 20 20 20 20 20 2f 2a 20 6e 53 63 72 61          /* nScra
77160 74 63 68 20 2a 2f 0a 20 20 20 28 76 6f 69 64 2a  tch */.   (void*
77170 29 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  )0,             
77180 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20 2a 2f       /* pPage */
77190 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20  .   0,          
771a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
771b0 2a 20 73 7a 50 61 67 65 20 2a 2f 0a 20 20 20 30  * szPage */.   0
771c0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
771d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 50 61            /* nPa
771e0 67 65 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20  ge */.   0,     
771f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77200 20 20 20 20 2f 2a 20 6d 78 50 61 72 73 65 72 53      /* mxParserS
77210 74 61 63 6b 20 2a 2f 0a 20 20 20 30 2c 20 20 20  tack */.   0,   
77220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77230 20 20 20 20 20 20 2f 2a 20 73 68 61 72 65 64 43        /* sharedC
77240 61 63 68 65 45 6e 61 62 6c 65 64 20 2a 2f 0a 20  acheEnabled */. 
77250 20 20 2f 2a 20 41 6c 6c 20 74 68 65 20 72 65 73    /* All the res
77260 74 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20  t should always 
77270 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74  be initialized t
77280 6f 20 7a 65 72 6f 20 2a 2f 0a 20 20 20 30 2c 20  o zero */.   0, 
77290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
772a0 20 20 20 20 20 20 20 20 2f 2a 20 69 73 49 6e 69          /* isIni
772b0 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20  t */.   0,      
772c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
772d0 20 20 20 2f 2a 20 69 6e 50 72 6f 67 72 65 73 73     /* inProgress
772e0 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20   */.   0,       
772f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77300 20 20 2f 2a 20 69 73 4d 75 74 65 78 49 6e 69 74    /* isMutexInit
77310 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20   */.   0,       
77320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77330 20 20 2f 2a 20 69 73 4d 61 6c 6c 6f 63 49 6e 69    /* isMallocIni
77340 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20  t */.   0,      
77350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77360 20 20 20 2f 2a 20 69 73 50 43 61 63 68 65 49 6e     /* isPCacheIn
77370 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20  it */.   0,     
77380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77390 20 20 20 20 2f 2a 20 70 49 6e 69 74 4d 75 74 65      /* pInitMute
773a0 78 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20  x */.   0,      
773b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
773c0 20 20 20 2f 2a 20 6e 52 65 66 49 6e 69 74 4d 75     /* nRefInitMu
773d0 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a  tex */.};.../*.*
773e0 2a 20 48 61 73 68 20 74 61 62 6c 65 20 66 6f 72  * Hash table for
773f0 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69 6f 6e   global function
77400 73 20 2d 20 66 75 6e 63 74 69 6f 6e 73 20 63 6f  s - functions co
77410 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 0a 2a 2a 20 64  mmon to all.** d
77420 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
77430 6f 6e 73 2e 20 20 41 66 74 65 72 20 69 6e 69 74  ons.  After init
77440 69 61 6c 69 7a 61 74 69 6f 6e 2c 20 74 68 69 73  ialization, this
77450 20 74 61 62 6c 65 20 69 73 0a 2a 2a 20 72 65 61   table is.** rea
77460 64 2d 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54  d-only..*/.SQLIT
77470 45 5f 50 52 49 56 41 54 45 20 53 51 4c 49 54 45  E_PRIVATE SQLITE
77480 5f 57 53 44 20 46 75 6e 63 44 65 66 48 61 73 68  _WSD FuncDefHash
77490 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75   sqlite3GlobalFu
774a0 6e 63 74 69 6f 6e 73 3b 0a 0a 2f 2a 0a 2a 2a 20  nctions;../*.** 
774b0 54 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  The value of the
774c0 20 22 70 65 6e 64 69 6e 67 22 20 62 79 74 65 20   "pending" byte 
774d0 6d 75 73 74 20 62 65 20 30 78 34 30 30 30 30 30  must be 0x400000
774e0 30 30 20 28 31 20 62 79 74 65 20 70 61 73 74 20  00 (1 byte past 
774f0 74 68 65 0a 2a 2a 20 31 2d 67 69 62 61 62 79 74  the.** 1-gibabyt
77500 65 20 62 6f 75 6e 64 61 72 79 29 20 69 6e 20 61  e boundary) in a
77510 20 63 6f 6d 70 61 74 69 62 6c 65 20 64 61 74 61   compatible data
77520 62 61 73 65 2e 20 20 53 51 4c 69 74 65 20 6e 65  base.  SQLite ne
77530 76 65 72 20 75 73 65 73 0a 2a 2a 20 74 68 65 20  ver uses.** the 
77540 64 61 74 61 62 61 73 65 20 70 61 67 65 20 74 68  database page th
77550 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  at contains the 
77560 70 65 6e 64 69 6e 67 20 62 79 74 65 2e 20 20 49  pending byte.  I
77570 74 20 6e 65 76 65 72 20 61 74 74 65 6d 70 74 73  t never attempts
77580 0a 2a 2a 20 74 6f 20 72 65 61 64 20 6f 72 20 77  .** to read or w
77590 72 69 74 65 20 74 68 61 74 20 70 61 67 65 2e 20  rite that page. 
775a0 20 54 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74   The pending byt
775b0 65 20 70 61 67 65 20 69 73 20 73 65 74 20 61 73  e page is set as
775c0 73 69 67 6e 0a 2a 2a 20 66 6f 72 20 75 73 65 20  sign.** for use 
775d0 62 79 20 74 68 65 20 56 46 53 20 6c 61 79 65 72  by the VFS layer
775e0 73 20 61 73 20 73 70 61 63 65 20 66 6f 72 20 6d  s as space for m
775f0 61 6e 61 67 69 6e 67 20 66 69 6c 65 20 6c 6f 63  anaging file loc
77600 6b 73 2e 0a 2a 2a 0a 2a 2a 20 44 75 72 69 6e 67  ks..**.** During
77610 20 74 65 73 74 69 6e 67 2c 20 69 74 20 69 73 20   testing, it is 
77620 6f 66 74 65 6e 20 64 65 73 69 72 61 62 6c 65 20  often desirable 
77630 74 6f 20 6d 6f 76 65 20 74 68 65 20 70 65 6e 64  to move the pend
77640 69 6e 67 20 62 79 74 65 20 74 6f 0a 2a 2a 20 61  ing byte to.** a
77650 20 64 69 66 66 65 72 65 6e 74 20 70 6f 73 69 74   different posit
77660 69 6f 6e 20 69 6e 20 74 68 65 20 66 69 6c 65 2e  ion in the file.
77670 20 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 63 6f    This allows co
77680 64 65 20 74 68 61 74 20 68 61 73 20 74 6f 0a 2a  de that has to.*
77690 2a 20 64 65 61 6c 20 77 69 74 68 20 74 68 65 20  * deal with the 
776a0 70 65 6e 64 69 6e 67 20 62 79 74 65 20 74 6f 20  pending byte to 
776b0 72 75 6e 20 6f 6e 20 66 69 6c 65 73 20 74 68 61  run on files tha
776c0 74 20 61 72 65 20 6d 75 63 68 20 73 6d 61 6c 6c  t are much small
776d0 65 72 0a 2a 2a 20 74 68 61 6e 20 31 20 47 69 42  er.** than 1 GiB
776e0 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 74  .  The sqlite3_t
776f0 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 20 69 6e  est_control() in
77700 74 65 72 66 61 63 65 20 63 61 6e 20 62 65 20 75  terface can be u
77710 73 65 64 20 74 6f 0a 2a 2a 20 6d 6f 76 65 20 74  sed to.** move t
77720 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 2e  he pending byte.
77730 0a 2a 2a 0a 2a 2a 20 49 4d 50 4f 52 54 41 4e 54  .**.** IMPORTANT
77740 3a 20 20 43 68 61 6e 67 69 6e 67 20 74 68 65 20  :  Changing the 
77750 70 65 6e 64 69 6e 67 20 62 79 74 65 20 74 6f 20  pending byte to 
77760 61 6e 79 20 76 61 6c 75 65 20 6f 74 68 65 72 20  any value other 
77770 74 68 61 6e 0a 2a 2a 20 30 78 34 30 30 30 30 30  than.** 0x400000
77780 30 30 20 72 65 73 75 6c 74 73 20 69 6e 20 61 6e  00 results in an
77790 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 64 61   incompatible da
777a0 74 61 62 61 73 65 20 66 69 6c 65 20 66 6f 72 6d  tabase file form
777b0 61 74 21 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20  at!.** Changing 
777c0 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65  the pending byte
777d0 20 64 75 72 69 6e 67 20 6f 70 65 72 61 74 69 6e   during operatin
777e0 67 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64  g results in und
777f0 65 66 69 6e 65 64 0a 2a 2a 20 61 6e 64 20 64 69  efined.** and di
77800 6c 65 74 65 72 69 6f 75 73 20 62 65 68 61 76 69  leterious behavi
77810 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  or..*/.SQLITE_PR
77820 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
77830 33 50 65 6e 64 69 6e 67 42 79 74 65 20 3d 20 30  3PendingByte = 0
77840 78 34 30 30 30 30 30 30 30 3b 0a 0a 2f 2a 2a 2a  x40000000;../***
77850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
77860 6f 66 20 67 6c 6f 62 61 6c 2e 63 20 2a 2a 2a 2a  of global.c ****
77870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
77880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
77890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
778a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
778b0 6e 20 66 69 6c 65 20 73 74 61 74 75 73 2e 63 20  n file status.c 
778c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
778d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
778e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
778f0 2a 20 32 30 30 38 20 4a 75 6e 65 20 31 38 0a 2a  * 2008 June 18.*
77900 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
77910 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
77920 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
77930 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
77940 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
77950 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
77960 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
77970 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
77980 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
77990 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
779a0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
779b0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
779c0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
779d0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
779e0 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
779f0 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
77a00 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
77a10 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
77a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
77a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
77a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
77a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
77a60 0a 2a 2a 20 54 68 69 73 20 6d 6f 64 75 6c 65 20  .** This module 
77a70 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 73  implements the s
77a80 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29 20  qlite3_status() 
77a90 69 6e 74 65 72 66 61 63 65 20 61 6e 64 20 72 65  interface and re
77aa0 6c 61 74 65 64 0a 2a 2a 20 66 75 6e 63 74 69 6f  lated.** functio
77ab0 6e 61 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 24 49  nality..**.** $I
77ac0 64 3a 20 73 74 61 74 75 73 2e 63 2c 76 20 31 2e  d: status.c,v 1.
77ad0 39 20 32 30 30 38 2f 30 39 2f 30 32 20 30 30 3a  9 2008/09/02 00:
77ae0 35 32 3a 35 32 20 64 72 68 20 45 78 70 20 24 0a  52:52 drh Exp $.
77af0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 61 72 69 61 62  */../*.** Variab
77b00 6c 65 73 20 69 6e 20 77 68 69 63 68 20 74 6f 20  les in which to 
77b10 72 65 63 6f 72 64 20 73 74 61 74 75 73 20 69 6e  record status in
77b20 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79  formation..*/.ty
77b30 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
77b40 69 74 65 33 53 74 61 74 54 79 70 65 20 73 71 6c  ite3StatType sql
77b50 69 74 65 33 53 74 61 74 54 79 70 65 3b 0a 73 74  ite3StatType;.st
77b60 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20  atic SQLITE_WSD 
77b70 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 53 74  struct sqlite3St
77b80 61 74 54 79 70 65 20 7b 0a 20 20 69 6e 74 20 6e  atType {.  int n
77b90 6f 77 56 61 6c 75 65 5b 39 5d 3b 20 20 20 20 20  owValue[9];     
77ba0 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 76      /* Current v
77bb0 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78  alue */.  int mx
77bc0 56 61 6c 75 65 5b 39 5d 3b 20 20 20 20 20 20 20  Value[9];       
77bd0 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 76 61     /* Maximum va
77be0 6c 75 65 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 33  lue */.} sqlite3
77bf0 53 74 61 74 20 3d 20 7b 20 7b 30 2c 7d 2c 20 7b  Stat = { {0,}, {
77c00 30 2c 7d 20 7d 3b 0a 0a 0a 2f 2a 20 54 68 65 20  0,} };.../* The 
77c10 22 77 73 64 53 74 61 74 22 20 6d 61 63 72 6f 20  "wsdStat" macro 
77c20 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20  will resolve to 
77c30 74 68 65 20 73 74 61 74 75 73 20 69 6e 66 6f 72  the status infor
77c40 6d 61 74 69 6f 6e 0a 2a 2a 20 73 74 61 74 65 20  mation.** state 
77c50 76 65 63 74 6f 72 2e 20 20 49 66 20 77 72 69 74  vector.  If writ
77c60 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 74 61  able static data
77c70 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65 64 20   is unsupported 
77c80 6f 6e 20 74 68 65 20 74 61 72 67 65 74 2c 0a 2a  on the target,.*
77c90 2a 20 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 63  * we have to loc
77ca0 61 74 65 20 74 68 65 20 73 74 61 74 65 20 76 65  ate the state ve
77cb0 63 74 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d 65  ctor at run-time
77cc0 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72 65 20 63  .  In the more c
77cd0 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 68  ommon.** case wh
77ce0 65 72 65 20 77 72 69 74 61 62 6c 65 20 73 74 61  ere writable sta
77cf0 74 69 63 20 64 61 74 61 20 69 73 20 73 75 70 70  tic data is supp
77d00 6f 72 74 65 64 2c 20 77 73 64 53 74 61 74 20 63  orted, wsdStat c
77d10 61 6e 20 72 65 66 65 72 20 64 69 72 65 63 74 6c  an refer directl
77d20 79 0a 2a 2a 20 74 6f 20 74 68 65 20 22 73 71 6c  y.** to the "sql
77d30 69 74 65 33 53 74 61 74 22 20 73 74 61 74 65 20  ite3Stat" state 
77d40 76 65 63 74 6f 72 20 64 65 63 6c 61 72 65 64 20  vector declared 
77d50 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69 66 64 65 66  above..*/.#ifdef
77d60 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44   SQLITE_OMIT_WSD
77d70 0a 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 61  .# define wsdSta
77d80 74 49 6e 69 74 20 20 73 71 6c 69 74 65 33 53 74  tInit  sqlite3St
77d90 61 74 54 79 70 65 20 2a 78 20 3d 20 26 47 4c 4f  atType *x = &GLO
77da0 42 41 4c 28 73 71 6c 69 74 65 33 53 74 61 74 54  BAL(sqlite3StatT
77db0 79 70 65 2c 73 71 6c 69 74 65 33 53 74 61 74 29  ype,sqlite3Stat)
77dc0 0a 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 61  .# define wsdSta
77dd0 74 20 78 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64  t x[0].#else.# d
77de0 65 66 69 6e 65 20 77 73 64 53 74 61 74 49 6e 69  efine wsdStatIni
77df0 74 0a 23 20 64 65 66 69 6e 65 20 77 73 64 53 74  t.# define wsdSt
77e00 61 74 20 73 71 6c 69 74 65 33 53 74 61 74 0a 23  at sqlite3Stat.#
77e10 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  endif../*.** Ret
77e20 75 72 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20  urn the current 
77e30 76 61 6c 75 65 20 6f 66 20 61 20 73 74 61 74 75  value of a statu
77e40 73 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a  s parameter..*/.
77e50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
77e60 6e 74 20 73 71 6c 69 74 65 33 53 74 61 74 75 73  nt sqlite3Status
77e70 56 61 6c 75 65 28 69 6e 74 20 6f 70 29 7b 0a 20  Value(int op){. 
77e80 20 77 73 64 53 74 61 74 49 6e 69 74 3b 0a 20 20   wsdStatInit;.  
77e90 61 73 73 65 72 74 28 20 6f 70 3e 3d 30 20 26 26  assert( op>=0 &&
77ea0 20 6f 70 3c 41 72 72 61 79 53 69 7a 65 28 77 73   op<ArraySize(ws
77eb0 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20  dStat.nowValue) 
77ec0 29 3b 0a 20 20 72 65 74 75 72 6e 20 77 73 64 53  );.  return wsdS
77ed0 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d  tat.nowValue[op]
77ee0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 4e  ;.}../*.** Add N
77ef0 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66   to the value of
77f00 20 61 20 73 74 61 74 75 73 20 72 65 63 6f 72 64   a status record
77f10 2e 20 20 49 74 20 69 73 20 61 73 73 75 6d 65 64  .  It is assumed
77f20 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 63 61 6c   that the.** cal
77f30 6c 65 72 20 68 6f 6c 64 73 20 61 70 70 72 6f 70  ler holds approp
77f40 72 69 61 74 65 20 6c 6f 63 6b 73 2e 0a 2a 2f 0a  riate locks..*/.
77f50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
77f60 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 74 75  oid sqlite3Statu
77f70 73 41 64 64 28 69 6e 74 20 6f 70 2c 20 69 6e 74  sAdd(int op, int
77f80 20 4e 29 7b 0a 20 20 77 73 64 53 74 61 74 49 6e   N){.  wsdStatIn
77f90 69 74 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70  it;.  assert( op
77fa0 3e 3d 30 20 26 26 20 6f 70 3c 41 72 72 61 79 53  >=0 && op<ArrayS
77fb0 69 7a 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 56  ize(wsdStat.nowV
77fc0 61 6c 75 65 29 20 29 3b 0a 20 20 77 73 64 53 74  alue) );.  wsdSt
77fd0 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 20  at.nowValue[op] 
77fe0 2b 3d 20 4e 3b 0a 20 20 69 66 28 20 77 73 64 53  += N;.  if( wsdS
77ff0 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d  tat.nowValue[op]
78000 3e 77 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65  >wsdStat.mxValue
78010 5b 6f 70 5d 20 29 7b 0a 20 20 20 20 77 73 64 53  [op] ){.    wsdS
78020 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20  tat.mxValue[op] 
78030 3d 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c  = wsdStat.nowVal
78040 75 65 5b 6f 70 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ue[op];.  }.}../
78050 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 76 61 6c  *.** Set the val
78060 75 65 20 6f 66 20 61 20 73 74 61 74 75 73 20 74  ue of a status t
78070 6f 20 58 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  o X..*/.SQLITE_P
78080 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
78090 74 65 33 53 74 61 74 75 73 53 65 74 28 69 6e 74  te3StatusSet(int
780a0 20 6f 70 2c 20 69 6e 74 20 58 29 7b 0a 20 20 77   op, int X){.  w
780b0 73 64 53 74 61 74 49 6e 69 74 3b 0a 20 20 61 73  sdStatInit;.  as
780c0 73 65 72 74 28 20 6f 70 3e 3d 30 20 26 26 20 6f  sert( op>=0 && o
780d0 70 3c 41 72 72 61 79 53 69 7a 65 28 77 73 64 53  p<ArraySize(wsdS
780e0 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20 29 3b  tat.nowValue) );
780f0 0a 20 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61  .  wsdStat.nowVa
78100 6c 75 65 5b 6f 70 5d 20 3d 20 58 3b 0a 20 20 69  lue[op] = X;.  i
78110 66 28 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61  f( wsdStat.nowVa
78120 6c 75 65 5b 6f 70 5d 3e 77 73 64 53 74 61 74 2e  lue[op]>wsdStat.
78130 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 29 7b 0a 20  mxValue[op] ){. 
78140 20 20 20 77 73 64 53 74 61 74 2e 6d 78 56 61 6c     wsdStat.mxVal
78150 75 65 5b 6f 70 5d 20 3d 20 77 73 64 53 74 61 74  ue[op] = wsdStat
78160 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20  .nowValue[op];. 
78170 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72   }.}../*.** Quer
78180 79 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61  y status informa
78190 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  tion..**.** This
781a0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
781b0 61 73 73 75 6d 65 73 20 74 68 61 74 20 72 65 61  assumes that rea
781c0 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 20  ding or writing 
781d0 61 6e 20 61 6c 69 67 6e 65 64 0a 2a 2a 20 33 32  an aligned.** 32
781e0 2d 62 69 74 20 69 6e 74 65 67 65 72 20 69 73 20  -bit integer is 
781f0 61 6e 20 61 74 6f 6d 69 63 20 6f 70 65 72 61 74  an atomic operat
78200 69 6f 6e 2e 20 20 49 66 20 74 68 61 74 20 61 73  ion.  If that as
78210 73 75 6d 70 74 69 6f 6e 20 69 73 20 6e 6f 74 20  sumption is not 
78220 74 72 75 65 2c 0a 2a 2a 20 74 68 65 6e 20 74 68  true,.** then th
78230 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f  is routine is no
78240 74 20 74 68 72 65 61 64 73 61 66 65 2e 0a 2a 2f  t threadsafe..*/
78250 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
78260 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 69  sqlite3_status(i
78270 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70 43 75 72  nt op, int *pCur
78280 72 65 6e 74 2c 20 69 6e 74 20 2a 70 48 69 67 68  rent, int *pHigh
78290 77 61 74 65 72 2c 20 69 6e 74 20 72 65 73 65 74  water, int reset
782a0 46 6c 61 67 29 7b 0a 20 20 77 73 64 53 74 61 74  Flag){.  wsdStat
782b0 49 6e 69 74 3b 0a 20 20 69 66 28 20 6f 70 3c 30  Init;.  if( op<0
782c0 20 7c 7c 20 6f 70 3e 3d 41 72 72 61 79 53 69 7a   || op>=ArraySiz
782d0 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c  e(wsdStat.nowVal
782e0 75 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ue) ){.    retur
782f0 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  n SQLITE_MISUSE;
78300 0a 20 20 7d 0a 20 20 2a 70 43 75 72 72 65 6e 74  .  }.  *pCurrent
78310 20 3d 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61   = wsdStat.nowVa
78320 6c 75 65 5b 6f 70 5d 3b 0a 20 20 2a 70 48 69 67  lue[op];.  *pHig
78330 68 77 61 74 65 72 20 3d 20 77 73 64 53 74 61 74  hwater = wsdStat
78340 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20  .mxValue[op];.  
78350 69 66 28 20 72 65 73 65 74 46 6c 61 67 20 29 7b  if( resetFlag ){
78360 0a 20 20 20 20 77 73 64 53 74 61 74 2e 6d 78 56  .    wsdStat.mxV
78370 61 6c 75 65 5b 6f 70 5d 20 3d 20 77 73 64 53 74  alue[op] = wsdSt
78380 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b  at.nowValue[op];
78390 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
783a0 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
783b0 2a 20 51 75 65 72 79 20 73 74 61 74 75 73 20 69  * Query status i
783c0 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61  nformation for a
783d0 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65   single database
783e0 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2f 0a 53   connection.*/.S
783f0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
78400 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28  lite3_db_status(
78410 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
78420 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
78430 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
78440 69 6f 6e 20 77 68 6f 73 65 20 73 74 61 74 75 73  ion whose status
78450 20 69 73 20 64 65 73 69 72 65 64 20 2a 2f 0a 20   is desired */. 
78460 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20   int op,        
78470 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73         /* Status
78480 20 76 65 72 62 20 2a 2f 0a 20 20 69 6e 74 20 2a   verb */.  int *
78490 70 43 75 72 72 65 6e 74 2c 20 20 20 20 20 20 20  pCurrent,       
784a0 20 2f 2a 20 57 72 69 74 65 20 63 75 72 72 65 6e   /* Write curren
784b0 74 20 76 61 6c 75 65 20 68 65 72 65 20 2a 2f 0a  t value here */.
784c0 20 20 69 6e 74 20 2a 70 48 69 67 68 77 61 74 65    int *pHighwate
784d0 72 2c 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65  r,      /* Write
784e0 20 68 69 67 68 2d 77 61 74 65 72 20 6d 61 72 6b   high-water mark
784f0 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 72   here */.  int r
78500 65 73 65 74 46 6c 61 67 20 20 20 20 20 20 20 20  esetFlag        
78510 20 2f 2a 20 52 65 73 65 74 20 68 69 67 68 2d 77   /* Reset high-w
78520 61 74 65 72 20 6d 61 72 6b 20 69 66 20 74 72 75  ater mark if tru
78530 65 20 2a 2f 0a 29 7b 0a 20 20 73 77 69 74 63 68  e */.){.  switch
78540 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65  ( op ){.    case
78550 20 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53   SQLITE_DBSTATUS
78560 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 3a  _LOOKASIDE_USED:
78570 20 7b 0a 20 20 20 20 20 20 2a 70 43 75 72 72 65   {.      *pCurre
78580 6e 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69  nt = db->lookasi
78590 64 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 20 2a  de.nOut;.      *
785a0 70 48 69 67 68 77 61 74 65 72 20 3d 20 64 62 2d  pHighwater = db-
785b0 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74  >lookaside.mxOut
785c0 3b 0a 20 20 20 20 20 20 69 66 28 20 72 65 73 65  ;.      if( rese
785d0 74 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20  tFlag ){.       
785e0 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d   db->lookaside.m
785f0 78 4f 75 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61  xOut = db->looka
78600 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20  side.nOut;.     
78610 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
78620 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c      }.    defaul
78630 74 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72  t: {.      retur
78640 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
78650 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
78660 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
78670 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
78680 20 45 6e 64 20 6f 66 20 73 74 61 74 75 73 2e 63   End of status.c
78690 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
786a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
786b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
786c0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
786d0 20 42 65 67 69 6e 20 66 69 6c 65 20 64 61 74 65   Begin file date
786e0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
786f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
78710 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 4f 63 74 6f  ./*.** 2003 Octo
78720 62 65 72 20 33 31 0a 2a 2a 0a 2a 2a 20 54 68 65  ber 31.**.** The
78730 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
78740 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
78750 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
78760 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
78770 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
78780 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
78790 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
787a0 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
787b0 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
787c0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
787d0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
787e0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
787f0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
78800 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
78810 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
78820 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
78830 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
78840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78880 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
78890 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  le contains the 
788a0 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74  C functions that
788b0 20 69 6d 70 6c 65 6d 65 6e 74 20 64 61 74 65 20   implement date 
788c0 61 6e 64 20 74 69 6d 65 0a 2a 2a 20 66 75 6e 63  and time.** func
788d0 74 69 6f 6e 73 20 66 6f 72 20 53 51 4c 69 74 65  tions for SQLite
788e0 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20  .  .**.** There 
788f0 69 73 20 6f 6e 6c 79 20 6f 6e 65 20 65 78 70 6f  is only one expo
78900 72 74 65 64 20 73 79 6d 62 6f 6c 20 69 6e 20 74  rted symbol in t
78910 68 69 73 20 66 69 6c 65 20 2d 20 74 68 65 20 66  his file - the f
78920 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73 71 6c 69 74  unction.** sqlit
78930 65 33 52 65 67 69 73 74 65 72 44 61 74 65 54 69  e3RegisterDateTi
78940 6d 65 46 75 6e 63 74 69 6f 6e 73 28 29 20 66 6f  meFunctions() fo
78950 75 6e 64 20 61 74 20 74 68 65 20 62 6f 74 74 6f  und at the botto
78960 6d 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a 2a  m of the file..*
78970 2a 20 41 6c 6c 20 6f 74 68 65 72 20 63 6f 64 65  * All other code
78980 20 68 61 73 20 66 69 6c 65 20 73 63 6f 70 65 2e   has file scope.
78990 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 64 61 74 65  .**.** $Id: date
789a0 2e 63 2c 76 20 31 2e 31 30 37 20 32 30 30 39 2f  .c,v 1.107 2009/
789b0 30 35 2f 30 33 20 32 30 3a 32 33 3a 35 33 20 64  05/03 20:23:53 d
789c0 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 53  rh Exp $.**.** S
789d0 51 4c 69 74 65 20 70 72 6f 63 65 73 73 65 73 20  QLite processes 
789e0 61 6c 6c 20 74 69 6d 65 73 20 61 6e 64 20 64 61  all times and da
789f0 74 65 73 20 61 73 20 4a 75 6c 69 61 6e 20 44 61  tes as Julian Da
78a00 79 20 6e 75 6d 62 65 72 73 2e 20 20 54 68 65 0a  y numbers.  The.
78a10 2a 2a 20 64 61 74 65 73 20 61 6e 64 20 74 69 6d  ** dates and tim
78a20 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 61 73  es are stored as
78a30 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64   the number of d
78a40 61 79 73 20 73 69 6e 63 65 20 6e 6f 6f 6e 0a 2a  ays since noon.*
78a50 2a 20 69 6e 20 47 72 65 65 6e 77 69 63 68 20 6f  * in Greenwich o
78a60 6e 20 4e 6f 76 65 6d 62 65 72 20 32 34 2c 20 34  n November 24, 4
78a70 37 31 34 20 42 2e 43 2e 20 61 63 63 6f 72 64 69  714 B.C. accordi
78a80 6e 67 20 74 6f 20 74 68 65 20 47 72 65 67 6f 72  ng to the Gregor
78a90 69 61 6e 0a 2a 2a 20 63 61 6c 65 6e 64 61 72 20  ian.** calendar 
78aa0 73 79 73 74 65 6d 2e 20 0a 2a 2a 0a 2a 2a 20 31  system. .**.** 1
78ab0 39 37 30 2d 30 31 2d 30 31 20 30 30 3a 30 30 3a  970-01-01 00:00:
78ac0 30 30 20 69 73 20 4a 44 20 32 34 34 30 35 38 37  00 is JD 2440587
78ad0 2e 35 0a 2a 2a 20 32 30 30 30 2d 30 31 2d 30 31  .5.** 2000-01-01
78ae0 20 30 30 3a 30 30 3a 30 30 20 69 73 20 4a 44 20   00:00:00 is JD 
78af0 32 34 35 31 35 34 34 2e 35 0a 2a 2a 0a 2a 2a 20  2451544.5.**.** 
78b00 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 69 6f  This implementio
78b10 6e 20 72 65 71 75 69 72 65 73 20 79 65 61 72 73  n requires years
78b20 20 74 6f 20 62 65 20 65 78 70 72 65 73 73 65 64   to be expressed
78b30 20 61 73 20 61 20 34 2d 64 69 67 69 74 20 6e 75   as a 4-digit nu
78b40 6d 62 65 72 0a 2a 2a 20 77 68 69 63 68 20 6d 65  mber.** which me
78b50 61 6e 73 20 74 68 61 74 20 6f 6e 6c 79 20 64 61  ans that only da
78b60 74 65 73 20 62 65 74 77 65 65 6e 20 30 30 30 30  tes between 0000
78b70 2d 30 31 2d 30 31 20 61 6e 64 20 39 39 39 39 2d  -01-01 and 9999-
78b80 31 32 2d 33 31 20 63 61 6e 0a 2a 2a 20 62 65 20  12-31 can.** be 
78b90 72 65 70 72 65 73 65 6e 74 65 64 2c 20 65 76 65  represented, eve
78ba0 6e 20 74 68 6f 75 67 68 20 6a 75 6c 69 61 6e 20  n though julian 
78bb0 64 61 79 20 6e 75 6d 62 65 72 73 20 61 6c 6c 6f  day numbers allo
78bc0 77 20 61 20 6d 75 63 68 20 77 69 64 65 72 0a 2a  w a much wider.*
78bd0 2a 20 72 61 6e 67 65 20 6f 66 20 64 61 74 65 73  * range of dates
78be0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 47 72 65 67  ..**.** The Greg
78bf0 6f 72 69 61 6e 20 63 61 6c 65 6e 64 61 72 20 73  orian calendar s
78c00 79 73 74 65 6d 20 69 73 20 75 73 65 64 20 66 6f  ystem is used fo
78c10 72 20 61 6c 6c 20 64 61 74 65 73 20 61 6e 64 20  r all dates and 
78c20 74 69 6d 65 73 2c 0a 2a 2a 20 65 76 65 6e 20 74  times,.** even t
78c30 68 6f 73 65 20 74 68 61 74 20 70 72 65 64 61 74  hose that predat
78c40 65 20 74 68 65 20 47 72 65 67 6f 72 69 61 6e 20  e the Gregorian 
78c50 63 61 6c 65 6e 64 61 72 2e 20 20 48 69 73 74 6f  calendar.  Histo
78c60 72 69 61 6e 73 20 75 73 75 61 6c 6c 79 0a 2a 2a  rians usually.**
78c70 20 75 73 65 20 74 68 65 20 4a 75 6c 69 61 6e 20   use the Julian 
78c80 63 61 6c 65 6e 64 61 72 20 66 6f 72 20 64 61 74  calendar for dat
78c90 65 73 20 70 72 69 6f 72 20 74 6f 20 31 35 38 32  es prior to 1582
78ca0 2d 31 30 2d 31 35 20 61 6e 64 20 66 6f 72 20 73  -10-15 and for s
78cb0 6f 6d 65 0a 2a 2a 20 64 61 74 65 73 20 61 66 74  ome.** dates aft
78cc0 65 72 77 61 72 64 73 2c 20 64 65 70 65 6e 64 69  erwards, dependi
78cd0 6e 67 20 6f 6e 20 6c 6f 63 61 6c 65 2e 20 20 42  ng on locale.  B
78ce0 65 77 61 72 65 20 6f 66 20 74 68 69 73 20 64 69  eware of this di
78cf0 66 66 65 72 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20  fference..**.** 
78d00 54 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 61  The conversion a
78d10 6c 67 6f 72 69 74 68 6d 73 20 61 72 65 20 69 6d  lgorithms are im
78d20 70 6c 65 6d 65 6e 74 65 64 20 62 61 73 65 64 20  plemented based 
78d30 6f 6e 20 64 65 73 63 72 69 70 74 69 6f 6e 73 0a  on descriptions.
78d40 2a 2a 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77  ** in the follow
78d50 69 6e 67 20 74 65 78 74 3a 0a 2a 2a 0a 2a 2a 20  ing text:.**.** 
78d60 20 20 20 20 20 4a 65 61 6e 20 4d 65 65 75 73 0a       Jean Meeus.
78d70 2a 2a 20 20 20 20 20 20 41 73 74 72 6f 6e 6f 6d  **      Astronom
78d80 69 63 61 6c 20 41 6c 67 6f 72 69 74 68 6d 73 2c  ical Algorithms,
78d90 20 32 6e 64 20 45 64 69 74 69 6f 6e 2c 20 31 39   2nd Edition, 19
78da0 39 38 0a 2a 2a 20 20 20 20 20 20 49 53 42 4d 20  98.**      ISBM 
78db0 30 2d 39 34 33 33 39 36 2d 36 31 2d 31 0a 2a 2a  0-943396-61-1.**
78dc0 20 20 20 20 20 20 57 69 6c 6c 6d 61 6e 6e 2d 42        Willmann-B
78dd0 65 6c 6c 2c 20 49 6e 63 0a 2a 2a 20 20 20 20 20  ell, Inc.**     
78de0 20 52 69 63 68 6d 6f 6e 64 2c 20 56 69 72 67 69   Richmond, Virgi
78df0 6e 69 61 20 28 55 53 41 29 0a 2a 2f 0a 23 69 6e  nia (USA).*/.#in
78e00 63 6c 75 64 65 20 3c 74 69 6d 65 2e 68 3e 0a 0a  clude <time.h>..
78e10 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
78e20 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55 4e  MIT_DATETIME_FUN
78e30 43 53 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20 72 65 63  CS../*.** On rec
78e40 65 6e 74 20 57 69 6e 64 6f 77 73 20 70 6c 61 74  ent Windows plat
78e50 66 6f 72 6d 73 2c 20 74 68 65 20 6c 6f 63 61 6c  forms, the local
78e60 74 69 6d 65 5f 73 28 29 20 66 75 6e 63 74 69 6f  time_s() functio
78e70 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 0a 2a  n is available.*
78e80 2a 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65  * as part of the
78e90 20 22 53 65 63 75 72 65 20 43 52 54 22 2e 20 49   "Secure CRT". I
78ea0 74 20 69 73 20 65 73 73 65 6e 74 69 61 6c 6c 79  t is essentially
78eb0 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 0a   equivalent to .
78ec0 2a 2a 20 6c 6f 63 61 6c 74 69 6d 65 5f 72 28 29  ** localtime_r()
78ed0 20 61 76 61 69 6c 61 62 6c 65 20 75 6e 64 65 72   available under
78ee0 20 6d 6f 73 74 20 50 4f 53 49 58 20 70 6c 61 74   most POSIX plat
78ef0 66 6f 72 6d 73 2c 20 65 78 63 65 70 74 20 74 68  forms, except th
78f00 61 74 20 74 68 65 20 0a 2a 2a 20 6f 72 64 65 72  at the .** order
78f10 20 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74 65   of the paramete
78f20 72 73 20 69 73 20 72 65 76 65 72 73 65 64 2e 0a  rs is reversed..
78f30 2a 2a 0a 2a 2a 20 53 65 65 20 68 74 74 70 3a 2f  **.** See http:/
78f40 2f 6d 73 64 6e 2e 6d 69 63 72 6f 73 6f 66 74 2e  /msdn.microsoft.
78f50 63 6f 6d 2f 65 6e 2d 75 73 2f 6c 69 62 72 61 72  com/en-us/librar
78f60 79 2f 61 34 34 32 78 33 79 65 28 56 53 2e 38 30  y/a442x3ye(VS.80
78f70 29 2e 61 73 70 78 2e 0a 2a 2a 0a 2a 2a 20 49 66  ).aspx..**.** If
78f80 20 74 68 65 20 75 73 65 72 20 68 61 73 20 6e 6f   the user has no
78f90 74 20 69 6e 64 69 63 61 74 65 64 20 74 6f 20 75  t indicated to u
78fa0 73 65 20 6c 6f 63 61 6c 74 69 6d 65 5f 72 28 29  se localtime_r()
78fb0 20 6f 72 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 28   or localtime_s(
78fc0 29 0a 2a 2a 20 61 6c 72 65 61 64 79 2c 20 63 68  ).** already, ch
78fd0 65 63 6b 20 66 6f 72 20 61 6e 20 4d 53 56 43 20  eck for an MSVC 
78fe0 62 75 69 6c 64 20 65 6e 76 69 72 6f 6e 6d 65 6e  build environmen
78ff0 74 20 74 68 61 74 20 70 72 6f 76 69 64 65 73 20  t that provides 
79000 0a 2a 2a 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 28  .** localtime_s(
79010 29 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e  )..*/.#if !defin
79020 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d  ed(HAVE_LOCALTIM
79030 45 5f 52 29 20 26 26 20 21 64 65 66 69 6e 65 64  E_R) && !defined
79040 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f  (HAVE_LOCALTIME_
79050 53 29 20 26 26 20 5c 0a 20 20 20 20 20 64 65 66  S) && \.     def
79060 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 20 26  ined(_MSC_VER) &
79070 26 20 64 65 66 69 6e 65 64 28 5f 43 52 54 5f 49  & defined(_CRT_I
79080 4e 53 45 43 55 52 45 5f 44 45 50 52 45 43 41 54  NSECURE_DEPRECAT
79090 45 29 0a 23 64 65 66 69 6e 65 20 48 41 56 45 5f  E).#define HAVE_
790a0 4c 4f 43 41 4c 54 49 4d 45 5f 53 20 31 0a 23 65  LOCALTIME_S 1.#e
790b0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 74  ndif../*.** A st
790c0 72 75 63 74 75 72 65 20 66 6f 72 20 68 6f 6c 64  ructure for hold
790d0 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 64 61 74  ing a single dat
790e0 65 20 61 6e 64 20 74 69 6d 65 2e 0a 2a 2f 0a 74  e and time..*/.t
790f0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 44 61  ypedef struct Da
79100 74 65 54 69 6d 65 20 44 61 74 65 54 69 6d 65 3b  teTime DateTime;
79110 0a 73 74 72 75 63 74 20 44 61 74 65 54 69 6d 65  .struct DateTime
79120 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74   {.  sqlite3_int
79130 36 34 20 69 4a 44 3b 20 2f 2a 20 54 68 65 20 6a  64 iJD; /* The j
79140 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72  ulian day number
79150 20 74 69 6d 65 73 20 38 36 34 30 30 30 30 30 20   times 86400000 
79160 2a 2f 0a 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44  */.  int Y, M, D
79170 3b 20 20 20 20 20 20 20 2f 2a 20 59 65 61 72 2c  ;       /* Year,
79180 20 6d 6f 6e 74 68 2c 20 61 6e 64 20 64 61 79 20   month, and day 
79190 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 6d 3b 20 20  */.  int h, m;  
791a0 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 75 72 20          /* Hour 
791b0 61 6e 64 20 6d 69 6e 75 74 65 73 20 2a 2f 0a 20  and minutes */. 
791c0 20 69 6e 74 20 74 7a 3b 20 20 20 20 20 20 20 20   int tz;        
791d0 20 20 20 20 2f 2a 20 54 69 6d 65 7a 6f 6e 65 20      /* Timezone 
791e0 6f 66 66 73 65 74 20 69 6e 20 6d 69 6e 75 74 65  offset in minute
791f0 73 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 73 3b  s */.  double s;
79200 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63            /* Sec
79210 6f 6e 64 73 20 2a 2f 0a 20 20 63 68 61 72 20 76  onds */.  char v
79220 61 6c 69 64 59 4d 44 3b 20 20 20 20 20 2f 2a 20  alidYMD;     /* 
79230 54 72 75 65 20 28 31 29 20 69 66 20 59 2c 4d 2c  True (1) if Y,M,
79240 44 20 61 72 65 20 76 61 6c 69 64 20 2a 2f 0a 20  D are valid */. 
79250 20 63 68 61 72 20 76 61 6c 69 64 48 4d 53 3b 20   char validHMS; 
79260 20 20 20 20 2f 2a 20 54 72 75 65 20 28 31 29 20      /* True (1) 
79270 69 66 20 68 2c 6d 2c 73 20 61 72 65 20 76 61 6c  if h,m,s are val
79280 69 64 20 2a 2f 0a 20 20 63 68 61 72 20 76 61 6c  id */.  char val
79290 69 64 4a 44 3b 20 20 20 20 20 20 2f 2a 20 54 72  idJD;      /* Tr
792a0 75 65 20 28 31 29 20 69 66 20 69 4a 44 20 69 73  ue (1) if iJD is
792b0 20 76 61 6c 69 64 20 2a 2f 0a 20 20 63 68 61 72   valid */.  char
792c0 20 76 61 6c 69 64 54 5a 3b 20 20 20 20 20 20 2f   validTZ;      /
792d0 2a 20 54 72 75 65 20 28 31 29 20 69 66 20 74 7a  * True (1) if tz
792e0 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 7d 3b 0a   is valid */.};.
792f0 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20  ../*.** Convert 
79300 7a 44 61 74 65 20 69 6e 74 6f 20 6f 6e 65 20 6f  zDate into one o
79310 72 20 6d 6f 72 65 20 69 6e 74 65 67 65 72 73 2e  r more integers.
79320 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 61 72 67    Additional arg
79330 75 6d 65 6e 74 73 0a 2a 2a 20 63 6f 6d 65 20 69  uments.** come i
79340 6e 20 67 72 6f 75 70 73 20 6f 66 20 35 20 61 73  n groups of 5 as
79350 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20   follows:.**.** 
79360 20 20 20 20 20 20 4e 20 20 20 20 20 20 20 6e 75        N       nu
79370 6d 62 65 72 20 6f 66 20 64 69 67 69 74 73 20 69  mber of digits i
79380 6e 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a  n the integer.**
79390 20 20 20 20 20 20 20 6d 69 6e 20 20 20 20 20 6d         min     m
793a0 69 6e 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 76  inimum allowed v
793b0 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e 74 65  alue of the inte
793c0 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 6d 61 78  ger.**       max
793d0 20 20 20 20 20 6d 61 78 69 6d 75 6d 20 61 6c 6c       maximum all
793e0 6f 77 65 64 20 76 61 6c 75 65 20 6f 66 20 74 68  owed value of th
793f0 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20  e integer.**    
79400 20 20 20 6e 65 78 74 43 20 20 20 66 69 72 73 74     nextC   first
79410 20 63 68 61 72 61 63 74 65 72 20 61 66 74 65 72   character after
79420 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20   the integer.** 
79430 20 20 20 20 20 20 70 56 61 6c 20 20 20 20 77 68        pVal    wh
79440 65 72 65 20 74 6f 20 77 72 69 74 65 20 74 68 65  ere to write the
79450 20 69 6e 74 65 67 65 72 73 20 76 61 6c 75 65 2e   integers value.
79460 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 73 69 6f  .**.** Conversio
79470 6e 73 20 63 6f 6e 74 69 6e 75 65 20 75 6e 74 69  ns continue unti
79480 6c 20 6f 6e 65 20 77 69 74 68 20 6e 65 78 74 43  l one with nextC
79490 3d 3d 30 20 69 73 20 65 6e 63 6f 75 6e 74 65 72  ==0 is encounter
794a0 65 64 2e 0a 2a 2a 20 54 68 65 20 66 75 6e 63 74  ed..** The funct
794b0 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20  ion returns the 
794c0 6e 75 6d 62 65 72 20 6f 66 20 73 75 63 63 65 73  number of succes
794d0 73 66 75 6c 20 63 6f 6e 76 65 72 73 69 6f 6e 73  sful conversions
794e0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
794f0 67 65 74 44 69 67 69 74 73 28 63 6f 6e 73 74 20  getDigits(const 
79500 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 2e 2e 2e  char *zDate, ...
79510 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b  ){.  va_list ap;
79520 0a 20 20 69 6e 74 20 76 61 6c 3b 0a 20 20 69 6e  .  int val;.  in
79530 74 20 4e 3b 0a 20 20 69 6e 74 20 6d 69 6e 3b 0a  t N;.  int min;.
79540 20 20 69 6e 74 20 6d 61 78 3b 0a 20 20 69 6e 74    int max;.  int
79550 20 6e 65 78 74 43 3b 0a 20 20 69 6e 74 20 2a 70   nextC;.  int *p
79560 56 61 6c 3b 0a 20 20 69 6e 74 20 63 6e 74 20 3d  Val;.  int cnt =
79570 20 30 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61   0;.  va_start(a
79580 70 2c 20 7a 44 61 74 65 29 3b 0a 20 20 64 6f 7b  p, zDate);.  do{
79590 0a 20 20 20 20 4e 20 3d 20 76 61 5f 61 72 67 28  .    N = va_arg(
795a0 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 6d 69  ap, int);.    mi
795b0 6e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69  n = va_arg(ap, i
795c0 6e 74 29 3b 0a 20 20 20 20 6d 61 78 20 3d 20 76  nt);.    max = v
795d0 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a  a_arg(ap, int);.
795e0 20 20 20 20 6e 65 78 74 43 20 3d 20 76 61 5f 61      nextC = va_a
795f0 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20  rg(ap, int);.   
79600 20 70 56 61 6c 20 3d 20 76 61 5f 61 72 67 28 61   pVal = va_arg(a
79610 70 2c 20 69 6e 74 2a 29 3b 0a 20 20 20 20 76 61  p, int*);.    va
79620 6c 20 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c 65  l = 0;.    while
79630 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 20 20 20 69  ( N-- ){.      i
79640 66 28 20 21 73 71 6c 69 74 65 33 49 73 64 69 67  f( !sqlite3Isdig
79650 69 74 28 2a 7a 44 61 74 65 29 20 29 7b 0a 20 20  it(*zDate) ){.  
79660 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 67        goto end_g
79670 65 74 44 69 67 69 74 73 3b 0a 20 20 20 20 20 20  etDigits;.      
79680 7d 0a 20 20 20 20 20 20 76 61 6c 20 3d 20 76 61  }.      val = va
79690 6c 2a 31 30 20 2b 20 2a 7a 44 61 74 65 20 2d 20  l*10 + *zDate - 
796a0 27 30 27 3b 0a 20 20 20 20 20 20 7a 44 61 74 65  '0';.      zDate
796b0 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ++;.    }.    if
796c0 28 20 76 61 6c 3c 6d 69 6e 20 7c 7c 20 76 61 6c  ( val<min || val
796d0 3e 6d 61 78 20 7c 7c 20 28 6e 65 78 74 43 21 3d  >max || (nextC!=
796e0 30 20 26 26 20 6e 65 78 74 43 21 3d 2a 7a 44 61  0 && nextC!=*zDa
796f0 74 65 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  te) ){.      got
79700 6f 20 65 6e 64 5f 67 65 74 44 69 67 69 74 73 3b  o end_getDigits;
79710 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 56 61 6c  .    }.    *pVal
79720 20 3d 20 76 61 6c 3b 0a 20 20 20 20 7a 44 61 74   = val;.    zDat
79730 65 2b 2b 3b 0a 20 20 20 20 63 6e 74 2b 2b 3b 0a  e++;.    cnt++;.
79740 20 20 7d 77 68 69 6c 65 28 20 6e 65 78 74 43 20    }while( nextC 
79750 29 3b 0a 65 6e 64 5f 67 65 74 44 69 67 69 74 73  );.end_getDigits
79760 3a 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a  :.  va_end(ap);.
79770 20 20 72 65 74 75 72 6e 20 63 6e 74 3b 0a 7d 0a    return cnt;.}.
79780 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 65 78 74  ./*.** Read text
79790 20 66 72 6f 6d 20 7a 5b 5d 20 61 6e 64 20 63 6f   from z[] and co
797a0 6e 76 65 72 74 20 69 6e 74 6f 20 61 20 66 6c 6f  nvert into a flo
797b0 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62  ating point numb
797c0 65 72 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74  er.  Return.** t
797d0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 67  he number of dig
797e0 69 74 73 20 63 6f 6e 76 65 72 74 65 64 2e 0a 2a  its converted..*
797f0 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 56 61 6c  /.#define getVal
79800 75 65 20 73 71 6c 69 74 65 33 41 74 6f 46 0a 0a  ue sqlite3AtoF..
79810 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 61 20 74 69  /*.** Parse a ti
79820 6d 65 7a 6f 6e 65 20 65 78 74 65 6e 73 69 6f 6e  mezone extension
79830 20 6f 6e 20 74 68 65 20 65 6e 64 20 6f 66 20 61   on the end of a
79840 20 64 61 74 65 2d 74 69 6d 65 2e 0a 2a 2a 20 54   date-time..** T
79850 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 73 20  he extension is 
79860 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a  of the form:.**.
79870 2a 2a 20 20 20 20 20 20 20 20 28 2b 2f 2d 29 48  **        (+/-)H
79880 48 3a 4d 4d 0a 2a 2a 0a 2a 2a 20 4f 72 20 74 68  H:MM.**.** Or th
79890 65 20 22 7a 75 6c 75 22 20 6e 6f 74 61 74 69 6f  e "zulu" notatio
798a0 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  n:.**.**        
798b0 5a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70  Z.**.** If the p
798c0 61 72 73 65 20 69 73 20 73 75 63 63 65 73 73 66  arse is successf
798d0 75 6c 2c 20 77 72 69 74 65 20 74 68 65 20 6e 75  ul, write the nu
798e0 6d 62 65 72 20 6f 66 20 6d 69 6e 75 74 65 73 0a  mber of minutes.
798f0 2a 2a 20 6f 66 20 63 68 61 6e 67 65 20 69 6e 20  ** of change in 
79900 70 2d 3e 74 7a 20 61 6e 64 20 72 65 74 75 72 6e  p->tz and return
79910 20 30 2e 20 20 49 66 20 61 20 70 61 72 73 65 72   0.  If a parser
79920 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 0a 2a   error occurs,.*
79930 2a 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72  * return non-zer
79940 6f 2e 0a 2a 2a 0a 2a 2a 20 41 20 6d 69 73 73 69  o..**.** A missi
79950 6e 67 20 73 70 65 63 69 66 69 65 72 20 69 73 20  ng specifier is 
79960 6e 6f 74 20 63 6f 6e 73 69 64 65 72 65 64 20 61  not considered a
79970 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74  n error..*/.stat
79980 69 63 20 69 6e 74 20 70 61 72 73 65 54 69 6d 65  ic int parseTime
79990 7a 6f 6e 65 28 63 6f 6e 73 74 20 63 68 61 72 20  zone(const char 
799a0 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 69 6d 65  *zDate, DateTime
799b0 20 2a 70 29 7b 0a 20 20 69 6e 74 20 73 67 6e 20   *p){.  int sgn 
799c0 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 48 72 2c 20  = 0;.  int nHr, 
799d0 6e 4d 6e 3b 0a 20 20 69 6e 74 20 63 3b 0a 20 20  nMn;.  int c;.  
799e0 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73  while( sqlite3Is
799f0 73 70 61 63 65 28 2a 7a 44 61 74 65 29 20 29 7b  space(*zDate) ){
79a00 20 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20 20 70 2d   zDate++; }.  p-
79a10 3e 74 7a 20 3d 20 30 3b 0a 20 20 63 20 3d 20 2a  >tz = 0;.  c = *
79a20 7a 44 61 74 65 3b 0a 20 20 69 66 28 20 63 3d 3d  zDate;.  if( c==
79a30 27 2d 27 20 29 7b 0a 20 20 20 20 73 67 6e 20 3d  '-' ){.    sgn =
79a40 20 2d 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28   -1;.  }else if(
79a50 20 63 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 73   c=='+' ){.    s
79a60 67 6e 20 3d 20 2b 31 3b 0a 20 20 7d 65 6c 73 65  gn = +1;.  }else
79a70 20 69 66 28 20 63 3d 3d 27 5a 27 20 7c 7c 20 63   if( c=='Z' || c
79a80 3d 3d 27 7a 27 20 29 7b 0a 20 20 20 20 7a 44 61  =='z' ){.    zDa
79a90 74 65 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f 20 7a  te++;.    goto z
79aa0 75 6c 75 5f 74 69 6d 65 3b 0a 20 20 7d 65 6c 73  ulu_time;.  }els
79ab0 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 63 21  e{.    return c!
79ac0 3d 30 3b 0a 20 20 7d 0a 20 20 7a 44 61 74 65 2b  =0;.  }.  zDate+
79ad0 2b 3b 0a 20 20 69 66 28 20 67 65 74 44 69 67 69  +;.  if( getDigi
79ae0 74 73 28 7a 44 61 74 65 2c 20 32 2c 20 30 2c 20  ts(zDate, 2, 0, 
79af0 31 34 2c 20 27 3a 27 2c 20 26 6e 48 72 2c 20 32  14, ':', &nHr, 2
79b00 2c 20 30 2c 20 35 39 2c 20 30 2c 20 26 6e 4d 6e  , 0, 59, 0, &nMn
79b10 29 21 3d 32 20 29 7b 0a 20 20 20 20 72 65 74 75  )!=2 ){.    retu
79b20 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a 44 61 74  rn 1;.  }.  zDat
79b30 65 20 2b 3d 20 35 3b 0a 20 20 70 2d 3e 74 7a 20  e += 5;.  p->tz 
79b40 3d 20 73 67 6e 2a 28 6e 4d 6e 20 2b 20 6e 48 72  = sgn*(nMn + nHr
79b50 2a 36 30 29 3b 0a 7a 75 6c 75 5f 74 69 6d 65 3a  *60);.zulu_time:
79b60 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65  .  while( sqlite
79b70 33 49 73 73 70 61 63 65 28 2a 7a 44 61 74 65 29  3Isspace(*zDate)
79b80 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20   ){ zDate++; }. 
79b90 20 72 65 74 75 72 6e 20 2a 7a 44 61 74 65 21 3d   return *zDate!=
79ba0 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73  0;.}../*.** Pars
79bb0 65 20 74 69 6d 65 73 20 6f 66 20 74 68 65 20 66  e times of the f
79bc0 6f 72 6d 20 48 48 3a 4d 4d 20 6f 72 20 48 48 3a  orm HH:MM or HH:
79bd0 4d 4d 3a 53 53 20 6f 72 20 48 48 3a 4d 4d 3a 53  MM:SS or HH:MM:S
79be0 53 2e 46 46 46 46 2e 0a 2a 2a 20 54 68 65 20 48  S.FFFF..** The H
79bf0 48 2c 20 4d 4d 2c 20 61 6e 64 20 53 53 20 6d 75  H, MM, and SS mu
79c00 73 74 20 65 61 63 68 20 62 65 20 65 78 61 63 74  st each be exact
79c10 6c 79 20 32 20 64 69 67 69 74 73 2e 20 20 54 68  ly 2 digits.  Th
79c20 65 0a 2a 2a 20 66 72 61 63 74 69 6f 6e 61 6c 20  e.** fractional 
79c30 73 65 63 6f 6e 64 73 20 46 46 46 46 20 63 61 6e  seconds FFFF can
79c40 20 62 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20   be one or more 
79c50 64 69 67 69 74 73 2e 0a 2a 2a 0a 2a 2a 20 52 65  digits..**.** Re
79c60 74 75 72 6e 20 31 20 69 66 20 74 68 65 72 65 20  turn 1 if there 
79c70 69 73 20 61 20 70 61 72 73 69 6e 67 20 65 72 72  is a parsing err
79c80 6f 72 20 61 6e 64 20 30 20 6f 6e 20 73 75 63 63  or and 0 on succ
79c90 65 73 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ess..*/.static i
79ca0 6e 74 20 70 61 72 73 65 48 68 4d 6d 53 73 28 63  nt parseHhMmSs(c
79cb0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74 65  onst char *zDate
79cc0 2c 20 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a  , DateTime *p){.
79cd0 20 20 69 6e 74 20 68 2c 20 6d 2c 20 73 3b 0a 20    int h, m, s;. 
79ce0 20 64 6f 75 62 6c 65 20 6d 73 20 3d 20 30 2e 30   double ms = 0.0
79cf0 3b 0a 20 20 69 66 28 20 67 65 74 44 69 67 69 74  ;.  if( getDigit
79d00 73 28 7a 44 61 74 65 2c 20 32 2c 20 30 2c 20 32  s(zDate, 2, 0, 2
79d10 34 2c 20 27 3a 27 2c 20 26 68 2c 20 32 2c 20 30  4, ':', &h, 2, 0
79d20 2c 20 35 39 2c 20 30 2c 20 26 6d 29 21 3d 32 20  , 59, 0, &m)!=2 
79d30 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  ){.    return 1;
79d40 0a 20 20 7d 0a 20 20 7a 44 61 74 65 20 2b 3d 20  .  }.  zDate += 
79d50 35 3b 0a 20 20 69 66 28 20 2a 7a 44 61 74 65 3d  5;.  if( *zDate=
79d60 3d 27 3a 27 20 29 7b 0a 20 20 20 20 7a 44 61 74  =':' ){.    zDat
79d70 65 2b 2b 3b 0a 20 20 20 20 69 66 28 20 67 65 74  e++;.    if( get
79d80 44 69 67 69 74 73 28 7a 44 61 74 65 2c 20 32 2c  Digits(zDate, 2,
79d90 20 30 2c 20 35 39 2c 20 30 2c 20 26 73 29 21 3d   0, 59, 0, &s)!=
79da0 31 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  1 ){.      retur
79db0 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a  n 1;.    }.    z
79dc0 44 61 74 65 20 2b 3d 20 32 3b 0a 20 20 20 20 69  Date += 2;.    i
79dd0 66 28 20 2a 7a 44 61 74 65 3d 3d 27 2e 27 20 26  f( *zDate=='.' &
79de0 26 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74  & sqlite3Isdigit
79df0 28 7a 44 61 74 65 5b 31 5d 29 20 29 7b 0a 20 20  (zDate[1]) ){.  
79e00 20 20 20 20 64 6f 75 62 6c 65 20 72 53 63 61 6c      double rScal
79e10 65 20 3d 20 31 2e 30 3b 0a 20 20 20 20 20 20 7a  e = 1.0;.      z
79e20 44 61 74 65 2b 2b 3b 0a 20 20 20 20 20 20 77 68  Date++;.      wh
79e30 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 69  ile( sqlite3Isdi
79e40 67 69 74 28 2a 7a 44 61 74 65 29 20 29 7b 0a 20  git(*zDate) ){. 
79e50 20 20 20 20 20 20 20 6d 73 20 3d 20 6d 73 2a 31         ms = ms*1
79e60 30 2e 30 20 2b 20 2a 7a 44 61 74 65 20 2d 20 27  0.0 + *zDate - '
79e70 30 27 3b 0a 20 20 20 20 20 20 20 20 72 53 63 61  0';.        rSca
79e80 6c 65 20 2a 3d 20 31 30 2e 30 3b 0a 20 20 20 20  le *= 10.0;.    
79e90 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20      zDate++;.   
79ea0 20 20 20 7d 0a 20 20 20 20 20 20 6d 73 20 2f 3d     }.      ms /=
79eb0 20 72 53 63 61 6c 65 3b 0a 20 20 20 20 7d 0a 20   rScale;.    }. 
79ec0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 20 3d 20   }else{.    s = 
79ed0 30 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76 61 6c 69  0;.  }.  p->vali
79ee0 64 4a 44 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61  dJD = 0;.  p->va
79ef0 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 20 20 70 2d  lidHMS = 1;.  p-
79f00 3e 68 20 3d 20 68 3b 0a 20 20 70 2d 3e 6d 20 3d  >h = h;.  p->m =
79f10 20 6d 3b 0a 20 20 70 2d 3e 73 20 3d 20 73 20 2b   m;.  p->s = s +
79f20 20 6d 73 3b 0a 20 20 69 66 28 20 70 61 72 73 65   ms;.  if( parse
79f30 54 69 6d 65 7a 6f 6e 65 28 7a 44 61 74 65 2c 20  Timezone(zDate, 
79f40 70 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20  p) ) return 1;. 
79f50 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 28 70   p->validTZ = (p
79f60 2d 3e 74 7a 21 3d 30 29 3f 31 3a 30 3b 0a 20 20  ->tz!=0)?1:0;.  
79f70 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
79f80 2a 2a 20 43 6f 6e 76 65 72 74 20 66 72 6f 6d 20  ** Convert from 
79f90 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d  YYYY-MM-DD HH:MM
79fa0 3a 53 53 20 74 6f 20 6a 75 6c 69 61 6e 20 64 61  :SS to julian da
79fb0 79 2e 20 20 57 65 20 61 6c 77 61 79 73 20 61 73  y.  We always as
79fc0 73 75 6d 65 0a 2a 2a 20 74 68 61 74 20 74 68 65  sume.** that the
79fd0 20 59 59 59 59 2d 4d 4d 2d 44 44 20 69 73 20 61   YYYY-MM-DD is a
79fe0 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20  ccording to the 
79ff0 47 72 65 67 6f 72 69 61 6e 20 63 61 6c 65 6e 64  Gregorian calend
7a000 61 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 66 65 72 65  ar..**.** Refere
7a010 6e 63 65 3a 20 20 4d 65 65 75 73 20 70 61 67 65  nce:  Meeus page
7a020 20 36 31 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f   61.*/.static vo
7a030 69 64 20 63 6f 6d 70 75 74 65 4a 44 28 44 61 74  id computeJD(Dat
7a040 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74  eTime *p){.  int
7a050 20 59 2c 20 4d 2c 20 44 2c 20 41 2c 20 42 2c 20   Y, M, D, A, B, 
7a060 58 31 2c 20 58 32 3b 0a 0a 20 20 69 66 28 20 70  X1, X2;..  if( p
7a070 2d 3e 76 61 6c 69 64 4a 44 20 29 20 72 65 74 75  ->validJD ) retu
7a080 72 6e 3b 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c  rn;.  if( p->val
7a090 69 64 59 4d 44 20 29 7b 0a 20 20 20 20 59 20 3d  idYMD ){.    Y =
7a0a0 20 70 2d 3e 59 3b 0a 20 20 20 20 4d 20 3d 20 70   p->Y;.    M = p
7a0b0 2d 3e 4d 3b 0a 20 20 20 20 44 20 3d 20 70 2d 3e  ->M;.    D = p->
7a0c0 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  D;.  }else{.    
7a0d0 59 20 3d 20 32 30 30 30 3b 20 20 2f 2a 20 49 66  Y = 2000;  /* If
7a0e0 20 6e 6f 20 59 4d 44 20 73 70 65 63 69 66 69 65   no YMD specifie
7a0f0 64 2c 20 61 73 73 75 6d 65 20 32 30 30 30 2d 4a  d, assume 2000-J
7a100 61 6e 2d 30 31 20 2a 2f 0a 20 20 20 20 4d 20 3d  an-01 */.    M =
7a110 20 31 3b 0a 20 20 20 20 44 20 3d 20 31 3b 0a 20   1;.    D = 1;. 
7a120 20 7d 0a 20 20 69 66 28 20 4d 3c 3d 32 20 29 7b   }.  if( M<=2 ){
7a130 0a 20 20 20 20 59 2d 2d 3b 0a 20 20 20 20 4d 20  .    Y--;.    M 
7a140 2b 3d 20 31 32 3b 0a 20 20 7d 0a 20 20 41 20 3d  += 12;.  }.  A =
7a150 20 59 2f 31 30 30 3b 0a 20 20 42 20 3d 20 32 20   Y/100;.  B = 2 
7a160 2d 20 41 20 2b 20 28 41 2f 34 29 3b 0a 20 20 58  - A + (A/4);.  X
7a170 31 20 3d 20 33 36 35 32 35 2a 28 59 2b 34 37 31  1 = 36525*(Y+471
7a180 36 29 2f 31 30 30 3b 0a 20 20 58 32 20 3d 20 33  6)/100;.  X2 = 3
7a190 30 36 30 30 31 2a 28 4d 2b 31 29 2f 31 30 30 30  06001*(M+1)/1000
7a1a0 30 3b 0a 20 20 70 2d 3e 69 4a 44 20 3d 20 28 73  0;.  p->iJD = (s
7a1b0 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 28 58  qlite3_int64)((X
7a1c0 31 20 2b 20 58 32 20 2b 20 44 20 2b 20 42 20 2d  1 + X2 + D + B -
7a1d0 20 31 35 32 34 2e 35 20 29 20 2a 20 38 36 34 30   1524.5 ) * 8640
7a1e0 30 30 30 30 29 3b 0a 20 20 70 2d 3e 76 61 6c 69  0000);.  p->vali
7a1f0 64 4a 44 20 3d 20 31 3b 0a 20 20 69 66 28 20 70  dJD = 1;.  if( p
7a200 2d 3e 76 61 6c 69 64 48 4d 53 20 29 7b 0a 20 20  ->validHMS ){.  
7a210 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 70 2d 3e 68    p->iJD += p->h
7a220 2a 33 36 30 30 30 30 30 20 2b 20 70 2d 3e 6d 2a  *3600000 + p->m*
7a230 36 30 30 30 30 20 2b 20 28 73 71 6c 69 74 65 33  60000 + (sqlite3
7a240 5f 69 6e 74 36 34 29 28 70 2d 3e 73 2a 31 30 30  _int64)(p->s*100
7a250 30 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 76  0);.    if( p->v
7a260 61 6c 69 64 54 5a 20 29 7b 0a 20 20 20 20 20 20  alidTZ ){.      
7a270 70 2d 3e 69 4a 44 20 2d 3d 20 70 2d 3e 74 7a 2a  p->iJD -= p->tz*
7a280 36 30 30 30 30 3b 0a 20 20 20 20 20 20 70 2d 3e  60000;.      p->
7a290 76 61 6c 69 64 59 4d 44 20 3d 20 30 3b 0a 20 20  validYMD = 0;.  
7a2a0 20 20 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20      p->validHMS 
7a2b0 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61  = 0;.      p->va
7a2c0 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20 20 20 7d  lidTZ = 0;.    }
7a2d0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61  .  }.}../*.** Pa
7a2e0 72 73 65 20 64 61 74 65 73 20 6f 66 20 74 68 65  rse dates of the
7a2f0 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 20 20   form.**.**     
7a300 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d  YYYY-MM-DD HH:MM
7a310 3a 53 53 2e 46 46 46 0a 2a 2a 20 20 20 20 20 59  :SS.FFF.**     Y
7a320 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 3a  YYY-MM-DD HH:MM:
7a330 53 53 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d  SS.**     YYYY-M
7a340 4d 2d 44 44 20 48 48 3a 4d 4d 0a 2a 2a 20 20 20  M-DD HH:MM.**   
7a350 20 20 59 59 59 59 2d 4d 4d 2d 44 44 0a 2a 2a 0a    YYYY-MM-DD.**.
7a360 2a 2a 20 57 72 69 74 65 20 74 68 65 20 72 65 73  ** Write the res
7a370 75 6c 74 20 69 6e 74 6f 20 74 68 65 20 44 61 74  ult into the Dat
7a380 65 54 69 6d 65 20 73 74 72 75 63 74 75 72 65 20  eTime structure 
7a390 61 6e 64 20 72 65 74 75 72 6e 20 30 0a 2a 2a 20  and return 0.** 
7a3a0 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 31  on success and 1
7a3b0 20 69 66 20 74 68 65 20 69 6e 70 75 74 20 73 74   if the input st
7a3c0 72 69 6e 67 20 69 73 20 6e 6f 74 20 61 20 77 65  ring is not a we
7a3d0 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a 20 64 61 74  ll-formed.** dat
7a3e0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
7a3f0 20 70 61 72 73 65 59 79 79 79 4d 6d 44 64 28 63   parseYyyyMmDd(c
7a400 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74 65  onst char *zDate
7a410 2c 20 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a  , DateTime *p){.
7a420 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44 2c 20 6e    int Y, M, D, n
7a430 65 67 3b 0a 0a 20 20 69 66 28 20 7a 44 61 74 65  eg;..  if( zDate
7a440 5b 30 5d 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20  [0]=='-' ){.    
7a450 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 6e 65 67  zDate++;.    neg
7a460 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 1;.  }else{. 
7a470 20 20 20 6e 65 67 20 3d 20 30 3b 0a 20 20 7d 0a     neg = 0;.  }.
7a480 20 20 69 66 28 20 67 65 74 44 69 67 69 74 73 28    if( getDigits(
7a490 7a 44 61 74 65 2c 34 2c 30 2c 39 39 39 39 2c 27  zDate,4,0,9999,'
7a4a0 2d 27 2c 26 59 2c 32 2c 31 2c 31 32 2c 27 2d 27  -',&Y,2,1,12,'-'
7a4b0 2c 26 4d 2c 32 2c 31 2c 33 31 2c 30 2c 26 44 29  ,&M,2,1,31,0,&D)
7a4c0 21 3d 33 20 29 7b 0a 20 20 20 20 72 65 74 75 72  !=3 ){.    retur
7a4d0 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a 44 61 74 65  n 1;.  }.  zDate
7a4e0 20 2b 3d 20 31 30 3b 0a 20 20 77 68 69 6c 65 28   += 10;.  while(
7a4f0 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28   sqlite3Isspace(
7a500 2a 7a 44 61 74 65 29 20 7c 7c 20 27 54 27 3d 3d  *zDate) || 'T'==
7a510 2a 28 75 38 2a 29 7a 44 61 74 65 20 29 7b 20 7a  *(u8*)zDate ){ z
7a520 44 61 74 65 2b 2b 3b 20 7d 0a 20 20 69 66 28 20  Date++; }.  if( 
7a530 70 61 72 73 65 48 68 4d 6d 53 73 28 7a 44 61 74  parseHhMmSs(zDat
7a540 65 2c 20 70 29 3d 3d 30 20 29 7b 0a 20 20 20 20  e, p)==0 ){.    
7a550 2f 2a 20 57 65 20 67 6f 74 20 74 68 65 20 74 69  /* We got the ti
7a560 6d 65 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66  me */.  }else if
7a570 28 20 2a 7a 44 61 74 65 3d 3d 30 20 29 7b 0a 20  ( *zDate==0 ){. 
7a580 20 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d     p->validHMS =
7a590 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
7a5a0 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20   return 1;.  }. 
7a5b0 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b   p->validJD = 0;
7a5c0 0a 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 3d  .  p->validYMD =
7a5d0 20 31 3b 0a 20 20 70 2d 3e 59 20 3d 20 6e 65 67   1;.  p->Y = neg
7a5e0 20 3f 20 2d 59 20 3a 20 59 3b 0a 20 20 70 2d 3e   ? -Y : Y;.  p->
7a5f0 4d 20 3d 20 4d 3b 0a 20 20 70 2d 3e 44 20 3d 20  M = M;.  p->D = 
7a600 44 3b 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c 69  D;.  if( p->vali
7a610 64 54 5a 20 29 7b 0a 20 20 20 20 63 6f 6d 70 75  dTZ ){.    compu
7a620 74 65 4a 44 28 70 29 3b 0a 20 20 7d 0a 20 20 72  teJD(p);.  }.  r
7a630 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
7a640 2a 20 53 65 74 20 74 68 65 20 74 69 6d 65 20 74  * Set the time t
7a650 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 69  o the current ti
7a660 6d 65 20 72 65 70 6f 72 74 65 64 20 62 79 20 74  me reported by t
7a670 68 65 20 56 46 53 0a 2a 2f 0a 73 74 61 74 69 63  he VFS.*/.static
7a680 20 76 6f 69 64 20 73 65 74 44 61 74 65 54 69 6d   void setDateTim
7a690 65 54 6f 43 75 72 72 65 6e 74 28 73 71 6c 69 74  eToCurrent(sqlit
7a6a0 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
7a6b0 65 78 74 2c 20 44 61 74 65 54 69 6d 65 20 2a 70  ext, DateTime *p
7a6c0 29 7b 0a 20 20 64 6f 75 62 6c 65 20 72 3b 0a 20  ){.  double r;. 
7a6d0 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73   sqlite3 *db = s
7a6e0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
7a6f0 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74  b_handle(context
7a700 29 3b 0a 20 20 73 71 6c 69 74 65 33 4f 73 43 75  );.  sqlite3OsCu
7a710 72 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70 56  rrentTime(db->pV
7a720 66 73 2c 20 26 72 29 3b 0a 20 20 70 2d 3e 69 4a  fs, &r);.  p->iJ
7a730 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74  D = (sqlite3_int
7a740 36 34 29 28 72 2a 38 36 34 30 30 30 30 30 2e 30  64)(r*86400000.0
7a750 20 2b 20 30 2e 35 29 3b 0a 20 20 70 2d 3e 76 61   + 0.5);.  p->va
7a760 6c 69 64 4a 44 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a  lidJD = 1;.}../*
7a770 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 70  .** Attempt to p
7a780 61 72 73 65 20 74 68 65 20 67 69 76 65 6e 20 73  arse the given s
7a790 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 4a 75 6c  tring into a Jul
7a7a0 69 61 6e 20 44 61 79 20 4e 75 6d 62 65 72 2e 20  ian Day Number. 
7a7b0 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 6e   Return.** the n
7a7c0 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 2e  umber of errors.
7a7d0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  .**.** The follo
7a7e0 77 69 6e 67 20 61 72 65 20 61 63 63 65 70 74 61  wing are accepta
7a7f0 62 6c 65 20 66 6f 72 6d 73 20 66 6f 72 20 74 68  ble forms for th
7a800 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 3a 0a  e input string:.
7a810 2a 2a 0a 2a 2a 20 20 20 20 20 20 59 59 59 59 2d  **.**      YYYY-
7a820 4d 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 2e 46  MM-DD HH:MM:SS.F
7a830 46 46 20 20 2b 2f 2d 48 48 3a 4d 4d 0a 2a 2a 20  FF  +/-HH:MM.** 
7a840 20 20 20 20 20 44 44 44 44 2e 44 44 20 0a 2a 2a       DDDD.DD .**
7a850 20 20 20 20 20 20 6e 6f 77 0a 2a 2a 0a 2a 2a 20        now.**.** 
7a860 49 6e 20 74 68 65 20 66 69 72 73 74 20 66 6f 72  In the first for
7a870 6d 2c 20 74 68 65 20 2b 2f 2d 48 48 3a 4d 4d 20  m, the +/-HH:MM 
7a880 69 73 20 61 6c 77 61 79 73 20 6f 70 74 69 6f 6e  is always option
7a890 61 6c 2e 20 20 54 68 65 20 66 72 61 63 74 69 6f  al.  The fractio
7a8a0 6e 61 6c 0a 2a 2a 20 73 65 63 6f 6e 64 73 20 65  nal.** seconds e
7a8b0 78 74 65 6e 73 69 6f 6e 20 28 74 68 65 20 22 2e  xtension (the ".
7a8c0 46 46 46 22 29 20 69 73 20 6f 70 74 69 6f 6e 61  FFF") is optiona
7a8d0 6c 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 73 20  l.  The seconds 
7a8e0 70 6f 72 74 69 6f 6e 0a 2a 2a 20 28 22 3a 53 53  portion.** (":SS
7a8f0 2e 46 46 46 22 29 20 69 73 20 6f 70 74 69 6f 6e  .FFF") is option
7a900 2e 20 20 54 68 65 20 79 65 61 72 20 61 6e 64 20  .  The year and 
7a910 64 61 74 65 20 63 61 6e 20 62 65 20 6f 6d 69 74  date can be omit
7a920 74 65 64 20 61 73 20 6c 6f 6e 67 0a 2a 2a 20 61  ted as long.** a
7a930 73 20 74 68 65 72 65 20 69 73 20 61 20 74 69 6d  s there is a tim
7a940 65 20 73 74 72 69 6e 67 2e 20 20 54 68 65 20 74  e string.  The t
7a950 69 6d 65 20 73 74 72 69 6e 67 20 63 61 6e 20 62  ime string can b
7a960 65 20 6f 6d 69 74 74 65 64 20 61 73 20 6c 6f 6e  e omitted as lon
7a970 67 0a 2a 2a 20 61 73 20 74 68 65 72 65 20 69 73  g.** as there is
7a980 20 61 20 79 65 61 72 20 61 6e 64 20 64 61 74 65   a year and date
7a990 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
7a9a0 70 61 72 73 65 44 61 74 65 4f 72 54 69 6d 65 28  parseDateOrTime(
7a9b0 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
7a9c0 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20  xt *context, .  
7a9d0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74  const char *zDat
7a9e0 65 2c 20 0a 20 20 44 61 74 65 54 69 6d 65 20 2a  e, .  DateTime *
7a9f0 70 0a 29 7b 0a 20 20 69 6e 74 20 69 73 52 65 61  p.){.  int isRea
7aa00 6c 4e 75 6d 3b 20 20 20 20 2f 2a 20 52 65 74 75  lNum;    /* Retu
7aa10 72 6e 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 49  rn from sqlite3I
7aa20 73 4e 75 6d 62 65 72 28 29 2e 20 20 4e 6f 74 20  sNumber().  Not 
7aa30 75 73 65 64 20 2a 2f 0a 20 20 69 66 28 20 70 61  used */.  if( pa
7aa40 72 73 65 59 79 79 79 4d 6d 44 64 28 7a 44 61 74  rseYyyyMmDd(zDat
7aa50 65 2c 70 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72  e,p)==0 ){.    r
7aa60 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65  eturn 0;.  }else
7aa70 20 69 66 28 20 70 61 72 73 65 48 68 4d 6d 53 73   if( parseHhMmSs
7aa80 28 7a 44 61 74 65 2c 20 70 29 3d 3d 30 20 29 7b  (zDate, p)==0 ){
7aa90 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
7aaa0 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74   }else if( sqlit
7aab0 65 33 53 74 72 49 43 6d 70 28 7a 44 61 74 65 2c  e3StrICmp(zDate,
7aac0 22 6e 6f 77 22 29 3d 3d 30 29 7b 0a 20 20 20 20  "now")==0){.    
7aad0 73 65 74 44 61 74 65 54 69 6d 65 54 6f 43 75 72  setDateTimeToCur
7aae0 72 65 6e 74 28 63 6f 6e 74 65 78 74 2c 20 70 29  rent(context, p)
7aaf0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  ;.    return 0;.
7ab00 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69    }else if( sqli
7ab10 74 65 33 49 73 4e 75 6d 62 65 72 28 7a 44 61 74  te3IsNumber(zDat
7ab20 65 2c 20 26 69 73 52 65 61 6c 4e 75 6d 2c 20 53  e, &isRealNum, S
7ab30 51 4c 49 54 45 5f 55 54 46 38 29 20 29 7b 0a 20  QLITE_UTF8) ){. 
7ab40 20 20 20 64 6f 75 62 6c 65 20 72 3b 0a 20 20 20     double r;.   
7ab50 20 67 65 74 56 61 6c 75 65 28 7a 44 61 74 65 2c   getValue(zDate,
7ab60 20 26 72 29 3b 0a 20 20 20 20 70 2d 3e 69 4a 44   &r);.    p->iJD
7ab70 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36   = (sqlite3_int6
7ab80 34 29 28 72 2a 38 36 34 30 30 30 30 30 2e 30 20  4)(r*86400000.0 
7ab90 2b 20 30 2e 35 29 3b 0a 20 20 20 20 70 2d 3e 76  + 0.5);.    p->v
7aba0 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 20 20 20  alidJD = 1;.    
7abb0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
7abc0 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a  return 1;.}../*.
7abd0 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 59  ** Compute the Y
7abe0 65 61 72 2c 20 4d 6f 6e 74 68 2c 20 61 6e 64 20  ear, Month, and 
7abf0 44 61 79 20 66 72 6f 6d 20 74 68 65 20 6a 75 6c  Day from the jul
7ac00 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 2e 0a  ian day number..
7ac10 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
7ac20 6f 6d 70 75 74 65 59 4d 44 28 44 61 74 65 54 69  omputeYMD(DateTi
7ac30 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 5a 2c  me *p){.  int Z,
7ac40 20 41 2c 20 42 2c 20 43 2c 20 44 2c 20 45 2c 20   A, B, C, D, E, 
7ac50 58 31 3b 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c  X1;.  if( p->val
7ac60 69 64 59 4d 44 20 29 20 72 65 74 75 72 6e 3b 0a  idYMD ) return;.
7ac70 20 20 69 66 28 20 21 70 2d 3e 76 61 6c 69 64 4a    if( !p->validJ
7ac80 44 20 29 7b 0a 20 20 20 20 70 2d 3e 59 20 3d 20  D ){.    p->Y = 
7ac90 32 30 30 30 3b 0a 20 20 20 20 70 2d 3e 4d 20 3d  2000;.    p->M =
7aca0 20 31 3b 0a 20 20 20 20 70 2d 3e 44 20 3d 20 31   1;.    p->D = 1
7acb0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 5a  ;.  }else{.    Z
7acc0 20 3d 20 28 69 6e 74 29 28 28 70 2d 3e 69 4a 44   = (int)((p->iJD
7acd0 20 2b 20 34 33 32 30 30 30 30 30 29 2f 38 36 34   + 43200000)/864
7ace0 30 30 30 30 30 29 3b 0a 20 20 20 20 41 20 3d 20  00000);.    A = 
7acf0 28 69 6e 74 29 28 28 5a 20 2d 20 31 38 36 37 32  (int)((Z - 18672
7ad00 31 36 2e 32 35 29 2f 33 36 35 32 34 2e 32 35 29  16.25)/36524.25)
7ad10 3b 0a 20 20 20 20 41 20 3d 20 5a 20 2b 20 31 20  ;.    A = Z + 1 
7ad20 2b 20 41 20 2d 20 28 41 2f 34 29 3b 0a 20 20 20  + A - (A/4);.   
7ad30 20 42 20 3d 20 41 20 2b 20 31 35 32 34 3b 0a 20   B = A + 1524;. 
7ad40 20 20 20 43 20 3d 20 28 69 6e 74 29 28 28 42 20     C = (int)((B 
7ad50 2d 20 31 32 32 2e 31 29 2f 33 36 35 2e 32 35 29  - 122.1)/365.25)
7ad60 3b 0a 20 20 20 20 44 20 3d 20 28 33 36 35 32 35  ;.    D = (36525
7ad70 2a 43 29 2f 31 30 30 3b 0a 20 20 20 20 45 20 3d  *C)/100;.    E =
7ad80 20 28 69 6e 74 29 28 28 42 2d 44 29 2f 33 30 2e   (int)((B-D)/30.
7ad90 36 30 30 31 29 3b 0a 20 20 20 20 58 31 20 3d 20  6001);.    X1 = 
7ada0 28 69 6e 74 29 28 33 30 2e 36 30 30 31 2a 45 29  (int)(30.6001*E)
7adb0 3b 0a 20 20 20 20 70 2d 3e 44 20 3d 20 42 20 2d  ;.    p->D = B -
7adc0 20 44 20 2d 20 58 31 3b 0a 20 20 20 20 70 2d 3e   D - X1;.    p->
7add0 4d 20 3d 20 45 3c 31 34 20 3f 20 45 2d 31 20 3a  M = E<14 ? E-1 :
7ade0 20 45 2d 31 33 3b 0a 20 20 20 20 70 2d 3e 59 20   E-13;.    p->Y 
7adf0 3d 20 70 2d 3e 4d 3e 32 20 3f 20 43 20 2d 20 34  = p->M>2 ? C - 4
7ae00 37 31 36 20 3a 20 43 20 2d 20 34 37 31 35 3b 0a  716 : C - 4715;.
7ae10 20 20 7d 0a 20 20 70 2d 3e 76 61 6c 69 64 59 4d    }.  p->validYM
7ae20 44 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  D = 1;.}../*.** 
7ae30 43 6f 6d 70 75 74 65 20 74 68 65 20 48 6f 75 72  Compute the Hour
7ae40 2c 20 4d 69 6e 75 74 65 2c 20 61 6e 64 20 53 65  , Minute, and Se
7ae50 63 6f 6e 64 73 20 66 72 6f 6d 20 74 68 65 20 6a  conds from the j
7ae60 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72  ulian day number
7ae70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
7ae80 20 63 6f 6d 70 75 74 65 48 4d 53 28 44 61 74 65   computeHMS(Date
7ae90 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  Time *p){.  int 
7aea0 73 3b 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c 69  s;.  if( p->vali
7aeb0 64 48 4d 53 20 29 20 72 65 74 75 72 6e 3b 0a 20  dHMS ) return;. 
7aec0 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20   computeJD(p);. 
7aed0 20 73 20 3d 20 28 69 6e 74 29 28 28 70 2d 3e 69   s = (int)((p->i
7aee0 4a 44 20 2b 20 34 33 32 30 30 30 30 30 29 20 25  JD + 43200000) %
7aef0 20 38 36 34 30 30 30 30 30 29 3b 0a 20 20 70 2d   86400000);.  p-
7af00 3e 73 20 3d 20 73 2f 31 30 30 30 2e 30 3b 0a 20  >s = s/1000.0;. 
7af10 20 73 20 3d 20 28 69 6e 74 29 70 2d 3e 73 3b 0a   s = (int)p->s;.
7af20 20 20 70 2d 3e 73 20 2d 3d 20 73 3b 0a 20 20 70    p->s -= s;.  p
7af30 2d 3e 68 20 3d 20 73 2f 33 36 30 30 3b 0a 20 20  ->h = s/3600;.  
7af40 73 20 2d 3d 20 70 2d 3e 68 2a 33 36 30 30 3b 0a  s -= p->h*3600;.
7af50 20 20 70 2d 3e 6d 20 3d 20 73 2f 36 30 3b 0a 20    p->m = s/60;. 
7af60 20 70 2d 3e 73 20 2b 3d 20 73 20 2d 20 70 2d 3e   p->s += s - p->
7af70 6d 2a 36 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64  m*60;.  p->valid
7af80 48 4d 53 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a  HMS = 1;.}../*.*
7af90 2a 20 43 6f 6d 70 75 74 65 20 62 6f 74 68 20 59  * Compute both Y
7afa0 4d 44 20 61 6e 64 20 48 4d 53 0a 2a 2f 0a 73 74  MD and HMS.*/.st
7afb0 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74  atic void comput
7afc0 65 59 4d 44 5f 48 4d 53 28 44 61 74 65 54 69 6d  eYMD_HMS(DateTim
7afd0 65 20 2a 70 29 7b 0a 20 20 63 6f 6d 70 75 74 65  e *p){.  compute
7afe0 59 4d 44 28 70 29 3b 0a 20 20 63 6f 6d 70 75 74  YMD(p);.  comput
7aff0 65 48 4d 53 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  eHMS(p);.}../*.*
7b000 2a 20 43 6c 65 61 72 20 74 68 65 20 59 4d 44 20  * Clear the YMD 
7b010 61 6e 64 20 48 4d 53 20 61 6e 64 20 74 68 65 20  and HMS and the 
7b020 54 5a 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  TZ.*/.static voi
7b030 64 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54  d clearYMD_HMS_T
7b040 5a 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a  Z(DateTime *p){.
7b050 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 3d 20    p->validYMD = 
7b060 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53  0;.  p->validHMS
7b070 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64   = 0;.  p->valid
7b080 54 5a 20 3d 20 30 3b 0a 7d 0a 0a 23 69 66 6e 64  TZ = 0;.}..#ifnd
7b090 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c  ef SQLITE_OMIT_L
7b0a0 4f 43 41 4c 54 49 4d 45 0a 2f 2a 0a 2a 2a 20 43  OCALTIME./*.** C
7b0b0 6f 6d 70 75 74 65 20 74 68 65 20 64 69 66 66 65  ompute the diffe
7b0c0 72 65 6e 63 65 20 28 69 6e 20 6d 69 6c 6c 69 73  rence (in millis
7b0d0 65 63 6f 6e 64 73 29 0a 2a 2a 20 62 65 74 77 65  econds).** betwe
7b0e0 65 6e 20 6c 6f 63 61 6c 74 69 6d 65 20 61 6e 64  en localtime and
7b0f0 20 55 54 43 20 28 61 2e 6b 2e 61 2e 20 47 4d 54   UTC (a.k.a. GMT
7b100 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 74 69 6d  ).** for the tim
7b110 65 20 76 61 6c 75 65 20 70 20 77 68 65 72 65 20  e value p where 
7b120 70 20 69 73 20 69 6e 20 55 54 43 2e 0a 2a 2f 0a  p is in UTC..*/.
7b130 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 69  static sqlite3_i
7b140 6e 74 36 34 20 6c 6f 63 61 6c 74 69 6d 65 4f 66  nt64 localtimeOf
7b150 66 73 65 74 28 44 61 74 65 54 69 6d 65 20 2a 70  fset(DateTime *p
7b160 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78 2c  ){.  DateTime x,
7b170 20 79 3b 0a 20 20 74 69 6d 65 5f 74 20 74 3b 0a   y;.  time_t t;.
7b180 20 20 78 20 3d 20 2a 70 3b 0a 20 20 63 6f 6d 70    x = *p;.  comp
7b190 75 74 65 59 4d 44 5f 48 4d 53 28 26 78 29 3b 0a  uteYMD_HMS(&x);.
7b1a0 20 20 69 66 28 20 78 2e 59 3c 31 39 37 31 20 7c    if( x.Y<1971 |
7b1b0 7c 20 78 2e 59 3e 3d 32 30 33 38 20 29 7b 0a 20  | x.Y>=2038 ){. 
7b1c0 20 20 20 78 2e 59 20 3d 20 32 30 30 30 3b 0a 20     x.Y = 2000;. 
7b1d0 20 20 20 78 2e 4d 20 3d 20 31 3b 0a 20 20 20 20     x.M = 1;.    
7b1e0 78 2e 44 20 3d 20 31 3b 0a 20 20 20 20 78 2e 68  x.D = 1;.    x.h
7b1f0 20 3d 20 30 3b 0a 20 20 20 20 78 2e 6d 20 3d 20   = 0;.    x.m = 
7b200 30 3b 0a 20 20 20 20 78 2e 73 20 3d 20 30 2e 30  0;.    x.s = 0.0
7b210 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20  ;.  } else {.   
7b220 20 69 6e 74 20 73 20 3d 20 28 69 6e 74 29 28 78   int s = (int)(x
7b230 2e 73 20 2b 20 30 2e 35 29 3b 0a 20 20 20 20 78  .s + 0.5);.    x
7b240 2e 73 20 3d 20 73 3b 0a 20 20 7d 0a 20 20 78 2e  .s = s;.  }.  x.
7b250 74 7a 20 3d 20 30 3b 0a 20 20 78 2e 76 61 6c 69  tz = 0;.  x.vali
7b260 64 4a 44 20 3d 20 30 3b 0a 20 20 63 6f 6d 70 75  dJD = 0;.  compu
7b270 74 65 4a 44 28 26 78 29 3b 0a 20 20 74 20 3d 20  teJD(&x);.  t = 
7b280 28 74 69 6d 65 5f 74 29 28 78 2e 69 4a 44 2f 31  (time_t)(x.iJD/1
7b290 30 30 30 20 2d 20 32 31 30 38 36 36 37 36 2a 28  000 - 21086676*(
7b2a0 69 36 34 29 31 30 30 30 30 29 3b 0a 23 69 66 64  i64)10000);.#ifd
7b2b0 65 66 20 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d  ef HAVE_LOCALTIM
7b2c0 45 5f 52 0a 20 20 7b 0a 20 20 20 20 73 74 72 75  E_R.  {.    stru
7b2d0 63 74 20 74 6d 20 73 4c 6f 63 61 6c 3b 0a 20 20  ct tm sLocal;.  
7b2e0 20 20 6c 6f 63 61 6c 74 69 6d 65 5f 72 28 26 74    localtime_r(&t
7b2f0 2c 20 26 73 4c 6f 63 61 6c 29 3b 0a 20 20 20 20  , &sLocal);.    
7b300 79 2e 59 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f  y.Y = sLocal.tm_
7b310 79 65 61 72 20 2b 20 31 39 30 30 3b 0a 20 20 20  year + 1900;.   
7b320 20 79 2e 4d 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d   y.M = sLocal.tm
7b330 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 20 79 2e  _mon + 1;.    y.
7b340 44 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 64  D = sLocal.tm_md
7b350 61 79 3b 0a 20 20 20 20 79 2e 68 20 3d 20 73 4c  ay;.    y.h = sL
7b360 6f 63 61 6c 2e 74 6d 5f 68 6f 75 72 3b 0a 20 20  ocal.tm_hour;.  
7b370 20 20 79 2e 6d 20 3d 20 73 4c 6f 63 61 6c 2e 74    y.m = sLocal.t
7b380 6d 5f 6d 69 6e 3b 0a 20 20 20 20 79 2e 73 20 3d  m_min;.    y.s =
7b390 20 73 4c 6f 63 61 6c 2e 74 6d 5f 73 65 63 3b 0a   sLocal.tm_sec;.
7b3a0 20 20 7d 0a 23 65 6c 69 66 20 64 65 66 69 6e 65    }.#elif define
7b3b0 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45  d(HAVE_LOCALTIME
7b3c0 5f 53 29 0a 20 20 7b 0a 20 20 20 20 73 74 72 75  _S).  {.    stru
7b3d0 63 74 20 74 6d 20 73 4c 6f 63 61 6c 3b 0a 20 20  ct tm sLocal;.  
7b3e0 20 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 28 26 73    localtime_s(&s
7b3f0 4c 6f 63 61 6c 2c 20 26 74 29 3b 0a 20 20 20 20  Local, &t);.    
7b400 79 2e 59 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f  y.Y = sLocal.tm_
7b410 79 65 61 72 20 2b 20 31 39 30 30 3b 0a 20 20 20  year + 1900;.   
7b420 20 79 2e 4d 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d   y.M = sLocal.tm
7b430 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 20 79 2e  _mon + 1;.    y.
7b440 44 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 64  D = sLocal.tm_md
7b450 61 79 3b 0a 20 20 20 20 79 2e 68 20 3d 20 73 4c  ay;.    y.h = sL
7b460 6f 63 61 6c 2e 74 6d 5f 68 6f 75 72 3b 0a 20 20  ocal.tm_hour;.  
7b470 20 20 79 2e 6d 20 3d 20 73 4c 6f 63 61 6c 2e 74    y.m = sLocal.t
7b480 6d 5f 6d 69 6e 3b 0a 20 20 20 20 79 2e 73 20 3d  m_min;.    y.s =
7b490 20 73 4c 6f 63 61 6c 2e 74 6d 5f 73 65 63 3b 0a   sLocal.tm_sec;.
7b4a0 20 20 7d 0a 23 65 6c 73 65 0a 20 20 7b 0a 20 20    }.#else.  {.  
7b4b0 20 20 73 74 72 75 63 74 20 74 6d 20 2a 70 54 6d    struct tm *pTm
7b4c0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
7b4d0 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65  tex_enter(sqlite
7b4e0 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49  3MutexAlloc(SQLI
7b4f0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
7b500 4d 41 53 54 45 52 29 29 3b 0a 20 20 20 20 70 54  MASTER));.    pT
7b510 6d 20 3d 20 6c 6f 63 61 6c 74 69 6d 65 28 26 74  m = localtime(&t
7b520 29 3b 0a 20 20 20 20 79 2e 59 20 3d 20 70 54 6d  );.    y.Y = pTm
7b530 2d 3e 74 6d 5f 79 65 61 72 20 2b 20 31 39 30 30  ->tm_year + 1900
7b540 3b 0a 20 20 20 20 79 2e 4d 20 3d 20 70 54 6d 2d  ;.    y.M = pTm-
7b550 3e 74 6d 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20  >tm_mon + 1;.   
7b560 20 79 2e 44 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d   y.D = pTm->tm_m
7b570 64 61 79 3b 0a 20 20 20 20 79 2e 68 20 3d 20 70  day;.    y.h = p
7b580 54 6d 2d 3e 74 6d 5f 68 6f 75 72 3b 0a 20 20 20  Tm->tm_hour;.   
7b590 20 79 2e 6d 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d   y.m = pTm->tm_m
7b5a0 69 6e 3b 0a 20 20 20 20 79 2e 73 20 3d 20 70 54  in;.    y.s = pT
7b5b0 6d 2d 3e 74 6d 5f 73 65 63 3b 0a 20 20 20 20 73  m->tm_sec;.    s
7b5c0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
7b5d0 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41  ve(sqlite3MutexA
7b5e0 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
7b5f0 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29  X_STATIC_MASTER)
7b600 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  );.  }.#endif.  
7b610 79 2e 76 61 6c 69 64 59 4d 44 20 3d 20 31 3b 0a  y.validYMD = 1;.
7b620 20 20 79 2e 76 61 6c 69 64 48 4d 53 20 3d 20 31    y.validHMS = 1
7b630 3b 0a 20 20 79 2e 76 61 6c 69 64 4a 44 20 3d 20  ;.  y.validJD = 
7b640 30 3b 0a 20 20 79 2e 76 61 6c 69 64 54 5a 20 3d  0;.  y.validTZ =
7b650 20 30 3b 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28   0;.  computeJD(
7b660 26 79 29 3b 0a 20 20 72 65 74 75 72 6e 20 79 2e  &y);.  return y.
7b670 69 4a 44 20 2d 20 78 2e 69 4a 44 3b 0a 7d 0a 23  iJD - x.iJD;.}.#
7b680 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
7b690 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 20 2a  OMIT_LOCALTIME *
7b6a0 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73  /../*.** Process
7b6b0 20 61 20 6d 6f 64 69 66 69 65 72 20 74 6f 20 61   a modifier to a
7b6c0 20 64 61 74 65 2d 74 69 6d 65 20 73 74 61 6d 70   date-time stamp
7b6d0 2e 20 20 54 68 65 20 6d 6f 64 69 66 69 65 72 73  .  The modifiers
7b6e0 20 61 72 65 0a 2a 2a 20 61 73 20 66 6f 6c 6c 6f   are.** as follo
7b6f0 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 4e 4e  ws:.**.**     NN
7b700 4e 20 64 61 79 73 0a 2a 2a 20 20 20 20 20 4e 4e  N days.**     NN
7b710 4e 20 68 6f 75 72 73 0a 2a 2a 20 20 20 20 20 4e  N hours.**     N
7b720 4e 4e 20 6d 69 6e 75 74 65 73 0a 2a 2a 20 20 20  NN minutes.**   
7b730 20 20 4e 4e 4e 2e 4e 4e 4e 4e 20 73 65 63 6f 6e    NNN.NNNN secon
7b740 64 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 6d 6f  ds.**     NNN mo
7b750 6e 74 68 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20  nths.**     NNN 
7b760 79 65 61 72 73 0a 2a 2a 20 20 20 20 20 73 74 61  years.**     sta
7b770 72 74 20 6f 66 20 6d 6f 6e 74 68 0a 2a 2a 20 20  rt of month.**  
7b780 20 20 20 73 74 61 72 74 20 6f 66 20 79 65 61 72     start of year
7b790 0a 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f 66  .**     start of
7b7a0 20 77 65 65 6b 0a 2a 2a 20 20 20 20 20 73 74 61   week.**     sta
7b7b0 72 74 20 6f 66 20 64 61 79 0a 2a 2a 20 20 20 20  rt of day.**    
7b7c0 20 77 65 65 6b 64 61 79 20 4e 0a 2a 2a 20 20 20   weekday N.**   
7b7d0 20 20 75 6e 69 78 65 70 6f 63 68 0a 2a 2a 20 20    unixepoch.**  
7b7e0 20 20 20 6c 6f 63 61 6c 74 69 6d 65 0a 2a 2a 20     localtime.** 
7b7f0 20 20 20 20 75 74 63 0a 2a 2a 0a 2a 2a 20 52 65      utc.**.** Re
7b800 74 75 72 6e 20 30 20 6f 6e 20 73 75 63 63 65 73  turn 0 on succes
7b810 73 20 61 6e 64 20 31 20 69 66 20 74 68 65 72 65  s and 1 if there
7b820 20 69 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20   is any kind of 
7b830 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  error..*/.static
7b840 20 69 6e 74 20 70 61 72 73 65 4d 6f 64 69 66 69   int parseModifi
7b850 65 72 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  er(const char *z
7b860 4d 6f 64 2c 20 44 61 74 65 54 69 6d 65 20 2a 70  Mod, DateTime *p
7b870 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 31 3b  ){.  int rc = 1;
7b880 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 64 6f 75 62  .  int n;.  doub
7b890 6c 65 20 72 3b 0a 20 20 63 68 61 72 20 2a 7a 2c  le r;.  char *z,
7b8a0 20 7a 42 75 66 5b 33 30 5d 3b 0a 20 20 7a 20 3d   zBuf[30];.  z =
7b8b0 20 7a 42 75 66 3b 0a 20 20 66 6f 72 28 6e 3d 30   zBuf;.  for(n=0
7b8c0 3b 20 6e 3c 41 72 72 61 79 53 69 7a 65 28 7a 42  ; n<ArraySize(zB
7b8d0 75 66 29 2d 31 20 26 26 20 7a 4d 6f 64 5b 6e 5d  uf)-1 && zMod[n]
7b8e0 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 7a 5b 6e 5d  ; n++){.    z[n]
7b8f0 20 3d 20 28 63 68 61 72 29 73 71 6c 69 74 65 33   = (char)sqlite3
7b900 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 38  UpperToLower[(u8
7b910 29 7a 4d 6f 64 5b 6e 5d 5d 3b 0a 20 20 7d 0a 20  )zMod[n]];.  }. 
7b920 20 7a 5b 6e 5d 20 3d 20 30 3b 0a 20 20 73 77 69   z[n] = 0;.  swi
7b930 74 63 68 28 20 7a 5b 30 5d 20 29 7b 0a 23 69 66  tch( z[0] ){.#if
7b940 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
7b950 5f 4c 4f 43 41 4c 54 49 4d 45 0a 20 20 20 20 63  _LOCALTIME.    c
7b960 61 73 65 20 27 6c 27 3a 20 7b 0a 20 20 20 20 20  ase 'l': {.     
7b970 20 2f 2a 20 20 20 20 6c 6f 63 61 6c 74 69 6d 65   /*    localtime
7b980 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
7b990 2a 2a 20 41 73 73 75 6d 69 6e 67 20 74 68 65 20  ** Assuming the 
7b9a0 63 75 72 72 65 6e 74 20 74 69 6d 65 20 76 61 6c  current time val
7b9b0 75 65 20 69 73 20 55 54 43 20 28 61 2e 6b 2e 61  ue is UTC (a.k.a
7b9c0 2e 20 47 4d 54 29 2c 20 73 68 69 66 74 20 69 74  . GMT), shift it
7b9d0 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 73 68 6f   to.      ** sho
7b9e0 77 20 6c 6f 63 61 6c 20 74 69 6d 65 2e 0a 20 20  w local time..  
7b9f0 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
7ba00 20 73 74 72 63 6d 70 28 7a 2c 20 22 6c 6f 63 61   strcmp(z, "loca
7ba10 6c 74 69 6d 65 22 29 3d 3d 30 20 29 7b 0a 20 20  ltime")==0 ){.  
7ba20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28        computeJD(
7ba30 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69  p);.        p->i
7ba40 4a 44 20 2b 3d 20 6c 6f 63 61 6c 74 69 6d 65 4f  JD += localtimeO
7ba50 66 66 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20  ffset(p);.      
7ba60 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54    clearYMD_HMS_T
7ba70 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 63  Z(p);.        rc
7ba80 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 0;.      }.  
7ba90 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
7baa0 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65  .#endif.    case
7bab0 20 27 75 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a   'u': {.      /*
7bac0 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 75 6e 69  .      **    uni
7bad0 78 65 70 6f 63 68 0a 20 20 20 20 20 20 2a 2a 0a  xepoch.      **.
7bae0 20 20 20 20 20 20 2a 2a 20 54 72 65 61 74 20 74        ** Treat t
7baf0 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65  he current value
7bb00 20 6f 66 20 70 2d 3e 69 4a 44 20 61 73 20 74 68   of p->iJD as th
7bb10 65 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 20 20  e number of.    
7bb20 20 20 2a 2a 20 73 65 63 6f 6e 64 73 20 73 69 6e    ** seconds sin
7bb30 63 65 20 31 39 37 30 2e 20 20 43 6f 6e 76 65 72  ce 1970.  Conver
7bb40 74 20 74 6f 20 61 20 72 65 61 6c 20 6a 75 6c 69  t to a real juli
7bb50 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 2e 0a 20  an day number.. 
7bb60 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66       */.      if
7bb70 28 20 73 74 72 63 6d 70 28 7a 2c 20 22 75 6e 69  ( strcmp(z, "uni
7bb80 78 65 70 6f 63 68 22 29 3d 3d 30 20 26 26 20 70  xepoch")==0 && p
7bb90 2d 3e 76 61 6c 69 64 4a 44 20 29 7b 0a 20 20 20  ->validJD ){.   
7bba0 20 20 20 20 20 70 2d 3e 69 4a 44 20 3d 20 28 70       p->iJD = (p
7bbb0 2d 3e 69 4a 44 20 2b 20 34 33 32 30 30 29 2f 38  ->iJD + 43200)/8
7bbc0 36 34 30 30 20 2b 20 32 31 30 38 36 36 37 36 2a  6400 + 21086676*
7bbd0 28 69 36 34 29 31 30 30 30 30 30 30 30 3b 0a 20  (i64)10000000;. 
7bbe0 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f         clearYMD_
7bbf0 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20  HMS_TZ(p);.     
7bc00 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20     rc = 0;.     
7bc10 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
7bc20 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45  E_OMIT_LOCALTIME
7bc30 0a 20 20 20 20 20 20 65 6c 73 65 20 69 66 28 20  .      else if( 
7bc40 73 74 72 63 6d 70 28 7a 2c 20 22 75 74 63 22 29  strcmp(z, "utc")
7bc50 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ==0 ){.        s
7bc60 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 63 31 3b  qlite3_int64 c1;
7bc70 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65  .        compute
7bc80 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20 63  JD(p);.        c
7bc90 31 20 3d 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66  1 = localtimeOff
7bca0 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 20 20  set(p);.        
7bcb0 70 2d 3e 69 4a 44 20 2d 3d 20 63 31 3b 0a 20 20  p->iJD -= c1;.  
7bcc0 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f 48        clearYMD_H
7bcd0 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 20  MS_TZ(p);.      
7bce0 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 63 31 20 2d    p->iJD += c1 -
7bcf0 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65 74   localtimeOffset
7bd00 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  (p);.        rc 
7bd10 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  = 0;.      }.#en
7bd20 64 69 66 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  dif.      break;
7bd30 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
7bd40 27 77 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 0a  'w': {.      /*.
7bd50 20 20 20 20 20 20 2a 2a 20 20 20 20 77 65 65 6b        **    week
7bd60 64 61 79 20 4e 0a 20 20 20 20 20 20 2a 2a 0a 20  day N.      **. 
7bd70 20 20 20 20 20 2a 2a 20 4d 6f 76 65 20 74 68 65       ** Move the
7bd80 20 64 61 74 65 20 74 6f 20 74 68 65 20 73 61 6d   date to the sam
7bd90 65 20 74 69 6d 65 20 6f 6e 20 74 68 65 20 6e 65  e time on the ne
7bda0 78 74 20 6f 63 63 75 72 72 65 6e 63 65 20 6f 66  xt occurrence of
7bdb0 0a 20 20 20 20 20 20 2a 2a 20 77 65 65 6b 64 61  .      ** weekda
7bdc0 79 20 4e 20 77 68 65 72 65 20 30 3d 3d 53 75 6e  y N where 0==Sun
7bdd0 64 61 79 2c 20 31 3d 3d 4d 6f 6e 64 61 79 2c 20  day, 1==Monday, 
7bde0 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 49  and so forth.  I
7bdf0 66 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 64  f the.      ** d
7be00 61 74 65 20 69 73 20 61 6c 72 65 61 64 79 20 6f  ate is already o
7be10 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  n the appropriat
7be20 65 20 77 65 65 6b 64 61 79 2c 20 74 68 69 73 20  e weekday, this 
7be30 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 20 20  is a no-op..    
7be40 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73    */.      if( s
7be50 74 72 6e 63 6d 70 28 7a 2c 20 22 77 65 65 6b 64  trncmp(z, "weekd
7be60 61 79 20 22 2c 20 38 29 3d 3d 30 20 26 26 20 67  ay ", 8)==0 && g
7be70 65 74 56 61 6c 75 65 28 26 7a 5b 38 5d 2c 26 72  etValue(&z[8],&r
7be80 29 3e 30 0a 20 20 20 20 20 20 20 20 20 20 20 20  )>0.            
7be90 20 20 20 20 20 26 26 20 28 6e 3d 28 69 6e 74 29       && (n=(int)
7bea0 72 29 3d 3d 72 20 26 26 20 6e 3e 3d 30 20 26 26  r)==r && n>=0 &&
7beb0 20 72 3c 37 20 29 7b 0a 20 20 20 20 20 20 20 20   r<7 ){.        
7bec0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 5a 3b  sqlite3_int64 Z;
7bed0 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65  .        compute
7bee0 59 4d 44 5f 48 4d 53 28 70 29 3b 0a 20 20 20 20  YMD_HMS(p);.    
7bef0 20 20 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d      p->validTZ =
7bf00 20 30 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76   0;.        p->v
7bf10 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20  alidJD = 0;.    
7bf20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29      computeJD(p)
7bf30 3b 0a 20 20 20 20 20 20 20 20 5a 20 3d 20 28 28  ;.        Z = ((
7bf40 70 2d 3e 69 4a 44 20 2b 20 31 32 39 36 30 30 30  p->iJD + 1296000
7bf50 30 30 29 2f 38 36 34 30 30 30 30 30 29 20 25 20  00)/86400000) % 
7bf60 37 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 5a  7;.        if( Z
7bf70 3e 6e 20 29 20 5a 20 2d 3d 20 37 3b 0a 20 20 20  >n ) Z -= 7;.   
7bf80 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28       p->iJD += (
7bf90 6e 20 2d 20 5a 29 2a 38 36 34 30 30 30 30 30 3b  n - Z)*86400000;
7bfa0 0a 20 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d  .        clearYM
7bfb0 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20  D_HMS_TZ(p);.   
7bfc0 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20       rc = 0;.   
7bfd0 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
7bfe0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
7bff0 20 27 73 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a   's': {.      /*
7c000 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 73 74 61  .      **    sta
7c010 72 74 20 6f 66 20 54 54 54 54 54 0a 20 20 20 20  rt of TTTTT.    
7c020 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4d 6f    **.      ** Mo
7c030 76 65 20 74 68 65 20 64 61 74 65 20 62 61 63 6b  ve the date back
7c040 77 61 72 64 73 20 74 6f 20 74 68 65 20 62 65 67  wards to the beg
7c050 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 63 75  inning of the cu
7c060 72 72 65 6e 74 20 64 61 79 2c 0a 20 20 20 20 20  rrent day,.     
7c070 20 2a 2a 20 6f 72 20 6d 6f 6e 74 68 20 6f 72 20   ** or month or 
7c080 79 65 61 72 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  year..      */. 
7c090 20 20 20 20 20 69 66 28 20 73 74 72 6e 63 6d 70       if( strncmp
7c0a0 28 7a 2c 20 22 73 74 61 72 74 20 6f 66 20 22 2c  (z, "start of ",
7c0b0 20 39 29 21 3d 30 20 29 20 62 72 65 61 6b 3b 0a   9)!=0 ) break;.
7c0c0 20 20 20 20 20 20 7a 20 2b 3d 20 39 3b 0a 20 20        z += 9;.  
7c0d0 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 28 70      computeYMD(p
7c0e0 29 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69  );.      p->vali
7c0f0 64 48 4d 53 20 3d 20 31 3b 0a 20 20 20 20 20 20  dHMS = 1;.      
7c100 70 2d 3e 68 20 3d 20 70 2d 3e 6d 20 3d 20 30 3b  p->h = p->m = 0;
7c110 0a 20 20 20 20 20 20 70 2d 3e 73 20 3d 20 30 2e  .      p->s = 0.
7c120 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69  0;.      p->vali
7c130 64 54 5a 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  dTZ = 0;.      p
7c140 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20  ->validJD = 0;. 
7c150 20 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28       if( strcmp(
7c160 7a 2c 22 6d 6f 6e 74 68 22 29 3d 3d 30 20 29 7b  z,"month")==0 ){
7c170 0a 20 20 20 20 20 20 20 20 70 2d 3e 44 20 3d 20  .        p->D = 
7c180 31 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  1;.        rc = 
7c190 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  0;.      }else i
7c1a0 66 28 20 73 74 72 63 6d 70 28 7a 2c 22 79 65 61  f( strcmp(z,"yea
7c1b0 72 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  r")==0 ){.      
7c1c0 20 20 63 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b    computeYMD(p);
7c1d0 0a 20 20 20 20 20 20 20 20 70 2d 3e 4d 20 3d 20  .        p->M = 
7c1e0 31 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 44 20  1;.        p->D 
7c1f0 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 72 63 20  = 1;.        rc 
7c200 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  = 0;.      }else
7c210 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c 22 64   if( strcmp(z,"d
7c220 61 79 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ay")==0 ){.     
7c230 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20     rc = 0;.     
7c240 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
7c250 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27      }.    case '
7c260 2b 27 3a 0a 20 20 20 20 63 61 73 65 20 27 2d 27  +':.    case '-'
7c270 3a 0a 20 20 20 20 63 61 73 65 20 27 30 27 3a 0a  :.    case '0':.
7c280 20 20 20 20 63 61 73 65 20 27 31 27 3a 0a 20 20      case '1':.  
7c290 20 20 63 61 73 65 20 27 32 27 3a 0a 20 20 20 20    case '2':.    
7c2a0 63 61 73 65 20 27 33 27 3a 0a 20 20 20 20 63 61  case '3':.    ca
7c2b0 73 65 20 27 34 27 3a 0a 20 20 20 20 63 61 73 65  se '4':.    case
7c2c0 20 27 35 27 3a 0a 20 20 20 20 63 61 73 65 20 27   '5':.    case '
7c2d0 36 27 3a 0a 20 20 20 20 63 61 73 65 20 27 37 27  6':.    case '7'
7c2e0 3a 0a 20 20 20 20 63 61 73 65 20 27 38 27 3a 0a  :.    case '8':.
7c2f0 20 20 20 20 63 61 73 65 20 27 39 27 3a 20 7b 0a      case '9': {.
7c300 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 52 6f        double rRo
7c310 75 6e 64 65 72 3b 0a 20 20 20 20 20 20 6e 20 3d  under;.      n =
7c320 20 67 65 74 56 61 6c 75 65 28 7a 2c 20 26 72 29   getValue(z, &r)
7c330 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
7c340 6e 3e 3d 31 20 29 3b 0a 20 20 20 20 20 20 69 66  n>=1 );.      if
7c350 28 20 7a 5b 6e 5d 3d 3d 27 3a 27 20 29 7b 0a 20  ( z[n]==':' ){. 
7c360 20 20 20 20 20 20 20 2f 2a 20 41 20 6d 6f 64 69         /* A modi
7c370 66 69 65 72 20 6f 66 20 74 68 65 20 66 6f 72 6d  fier of the form
7c380 20 28 2b 7c 2d 29 48 48 3a 4d 4d 3a 53 53 2e 46   (+|-)HH:MM:SS.F
7c390 46 46 20 61 64 64 73 20 28 6f 72 20 73 75 62 74  FF adds (or subt
7c3a0 72 61 63 74 73 29 20 74 68 65 0a 20 20 20 20 20  racts) the.     
7c3b0 20 20 20 2a 2a 20 73 70 65 63 69 66 69 65 64 20     ** specified 
7c3c0 6e 75 6d 62 65 72 20 6f 66 20 68 6f 75 72 73 2c  number of hours,
7c3d0 20 6d 69 6e 75 74 65 73 2c 20 73 65 63 6f 6e 64   minutes, second
7c3e0 73 2c 20 61 6e 64 20 66 72 61 63 74 69 6f 6e 61  s, and fractiona
7c3f0 6c 20 73 65 63 6f 6e 64 73 0a 20 20 20 20 20 20  l seconds.      
7c400 20 20 2a 2a 20 74 6f 20 74 68 65 20 74 69 6d 65    ** to the time
7c410 2e 20 20 54 68 65 20 22 2e 46 46 46 22 20 6d 61  .  The ".FFF" ma
7c420 79 20 62 65 20 6f 6d 69 74 74 65 64 2e 20 20 54  y be omitted.  T
7c430 68 65 20 22 3a 53 53 2e 46 46 46 22 20 6d 61 79  he ":SS.FFF" may
7c440 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f   be.        ** o
7c450 6d 69 74 74 65 64 2e 0a 20 20 20 20 20 20 20 20  mitted..        
7c460 2a 2f 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74  */.        const
7c470 20 63 68 61 72 20 2a 7a 32 20 3d 20 7a 3b 0a 20   char *z2 = z;. 
7c480 20 20 20 20 20 20 20 44 61 74 65 54 69 6d 65 20         DateTime 
7c490 74 78 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  tx;.        sqli
7c4a0 74 65 33 5f 69 6e 74 36 34 20 64 61 79 3b 0a 20  te3_int64 day;. 
7c4b0 20 20 20 20 20 20 20 69 66 28 20 21 73 71 6c 69         if( !sqli
7c4c0 74 65 33 49 73 64 69 67 69 74 28 2a 7a 32 29 20  te3Isdigit(*z2) 
7c4d0 29 20 7a 32 2b 2b 3b 0a 20 20 20 20 20 20 20 20  ) z2++;.        
7c4e0 6d 65 6d 73 65 74 28 26 74 78 2c 20 30 2c 20 73  memset(&tx, 0, s
7c4f0 69 7a 65 6f 66 28 74 78 29 29 3b 0a 20 20 20 20  izeof(tx));.    
7c500 20 20 20 20 69 66 28 20 70 61 72 73 65 48 68 4d      if( parseHhM
7c510 6d 53 73 28 7a 32 2c 20 26 74 78 29 20 29 20 62  mSs(z2, &tx) ) b
7c520 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 6f  reak;.        co
7c530 6d 70 75 74 65 4a 44 28 26 74 78 29 3b 0a 20 20  mputeJD(&tx);.  
7c540 20 20 20 20 20 20 74 78 2e 69 4a 44 20 2d 3d 20        tx.iJD -= 
7c550 34 33 32 30 30 30 30 30 3b 0a 20 20 20 20 20 20  43200000;.      
7c560 20 20 64 61 79 20 3d 20 74 78 2e 69 4a 44 2f 38    day = tx.iJD/8
7c570 36 34 30 30 30 30 30 3b 0a 20 20 20 20 20 20 20  6400000;.       
7c580 20 74 78 2e 69 4a 44 20 2d 3d 20 64 61 79 2a 38   tx.iJD -= day*8
7c590 36 34 30 30 30 30 30 3b 0a 20 20 20 20 20 20 20  6400000;.       
7c5a0 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 2d 27 20 29   if( z[0]=='-' )
7c5b0 20 74 78 2e 69 4a 44 20 3d 20 2d 74 78 2e 69 4a   tx.iJD = -tx.iJ
7c5c0 44 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75  D;.        compu
7c5d0 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 20  teJD(p);.       
7c5e0 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a   clearYMD_HMS_TZ
7c5f0 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  (p);.        p->
7c600 69 4a 44 20 2b 3d 20 74 78 2e 69 4a 44 3b 0a 20  iJD += tx.iJD;. 
7c610 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20         rc = 0;. 
7c620 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
7c630 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 20 2b 3d      }.      z +=
7c640 20 6e 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28   n;.      while(
7c650 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28   sqlite3Isspace(
7c660 2a 7a 29 20 29 20 7a 2b 2b 3b 0a 20 20 20 20 20  *z) ) z++;.     
7c670 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c   n = sqlite3Strl
7c680 65 6e 33 30 28 7a 29 3b 0a 20 20 20 20 20 20 69  en30(z);.      i
7c690 66 28 20 6e 3e 31 30 20 7c 7c 20 6e 3c 33 20 29  f( n>10 || n<3 )
7c6a0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 69 66   break;.      if
7c6b0 28 20 7a 5b 6e 2d 31 5d 3d 3d 27 73 27 20 29 7b  ( z[n-1]=='s' ){
7c6c0 20 7a 5b 6e 2d 31 5d 20 3d 20 30 3b 20 6e 2d 2d   z[n-1] = 0; n--
7c6d0 3b 20 7d 0a 20 20 20 20 20 20 63 6f 6d 70 75 74  ; }.      comput
7c6e0 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 72 63  eJD(p);.      rc
7c6f0 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 52 6f 75   = 0;.      rRou
7c700 6e 64 65 72 20 3d 20 72 3c 30 20 3f 20 2d 30 2e  nder = r<0 ? -0.
7c710 35 20 3a 20 2b 30 2e 35 3b 0a 20 20 20 20 20 20  5 : +0.5;.      
7c720 69 66 28 20 6e 3d 3d 33 20 26 26 20 73 74 72 63  if( n==3 && strc
7c730 6d 70 28 7a 2c 22 64 61 79 22 29 3d 3d 30 20 29  mp(z,"day")==0 )
7c740 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44  {.        p->iJD
7c750 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74   += (sqlite3_int
7c760 36 34 29 28 72 2a 38 36 34 30 30 30 30 30 2e 30  64)(r*86400000.0
7c770 20 2b 20 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20   + rRounder);.  
7c780 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d      }else if( n=
7c790 3d 34 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22  =4 && strcmp(z,"
7c7a0 68 6f 75 72 22 29 3d 3d 30 20 29 7b 0a 20 20 20  hour")==0 ){.   
7c7b0 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28       p->iJD += (
7c7c0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72  sqlite3_int64)(r
7c7d0 2a 28 38 36 34 30 30 30 30 30 2e 30 2f 32 34 2e  *(86400000.0/24.
7c7e0 30 29 20 2b 20 72 52 6f 75 6e 64 65 72 29 3b 0a  0) + rRounder);.
7c7f0 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
7c800 6e 3d 3d 36 20 26 26 20 73 74 72 63 6d 70 28 7a  n==6 && strcmp(z
7c810 2c 22 6d 69 6e 75 74 65 22 29 3d 3d 30 20 29 7b  ,"minute")==0 ){
7c820 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20  .        p->iJD 
7c830 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36  += (sqlite3_int6
7c840 34 29 28 72 2a 28 38 36 34 30 30 30 30 30 2e 30  4)(r*(86400000.0
7c850 2f 28 32 34 2e 30 2a 36 30 2e 30 29 29 20 2b 20  /(24.0*60.0)) + 
7c860 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20  rRounder);.     
7c870 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 36 20   }else if( n==6 
7c880 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 73 65 63  && strcmp(z,"sec
7c890 6f 6e 64 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ond")==0 ){.    
7c8a0 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73      p->iJD += (s
7c8b0 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a  qlite3_int64)(r*
7c8c0 28 38 36 34 30 30 30 30 30 2e 30 2f 28 32 34 2e  (86400000.0/(24.
7c8d0 30 2a 36 30 2e 30 2a 36 30 2e 30 29 29 20 2b 20  0*60.0*60.0)) + 
7c8e0 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20  rRounder);.     
7c8f0 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 35 20   }else if( n==5 
7c900 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 6d 6f 6e  && strcmp(z,"mon
7c910 74 68 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  th")==0 ){.     
7c920 20 20 20 69 6e 74 20 78 2c 20 79 3b 0a 20 20 20     int x, y;.   
7c930 20 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f       computeYMD_
7c940 48 4d 53 28 70 29 3b 0a 20 20 20 20 20 20 20 20  HMS(p);.        
7c950 70 2d 3e 4d 20 2b 3d 20 28 69 6e 74 29 72 3b 0a  p->M += (int)r;.
7c960 20 20 20 20 20 20 20 20 78 20 3d 20 70 2d 3e 4d          x = p->M
7c970 3e 30 20 3f 20 28 70 2d 3e 4d 2d 31 29 2f 31 32  >0 ? (p->M-1)/12
7c980 20 3a 20 28 70 2d 3e 4d 2d 31 32 29 2f 31 32 3b   : (p->M-12)/12;
7c990 0a 20 20 20 20 20 20 20 20 70 2d 3e 59 20 2b 3d  .        p->Y +=
7c9a0 20 78 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 4d   x;.        p->M
7c9b0 20 2d 3d 20 78 2a 31 32 3b 0a 20 20 20 20 20 20   -= x*12;.      
7c9c0 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30    p->validJD = 0
7c9d0 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74  ;.        comput
7c9e0 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20  eJD(p);.        
7c9f0 79 20 3d 20 28 69 6e 74 29 72 3b 0a 20 20 20 20  y = (int)r;.    
7ca00 20 20 20 20 69 66 28 20 79 21 3d 72 20 29 7b 0a      if( y!=r ){.
7ca10 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44            p->iJD
7ca20 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74   += (sqlite3_int
7ca30 36 34 29 28 28 72 20 2d 20 79 29 2a 33 30 2e 30  64)((r - y)*30.0
7ca40 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 20 72 52  *86400000.0 + rR
7ca50 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 20  ounder);.       
7ca60 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69   }.      }else i
7ca70 66 28 20 6e 3d 3d 34 20 26 26 20 73 74 72 63 6d  f( n==4 && strcm
7ca80 70 28 7a 2c 22 79 65 61 72 22 29 3d 3d 30 20 29  p(z,"year")==0 )
7ca90 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 79 20  {.        int y 
7caa0 3d 20 28 69 6e 74 29 72 3b 0a 20 20 20 20 20 20  = (int)r;.      
7cab0 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53    computeYMD_HMS
7cac0 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  (p);.        p->
7cad0 59 20 2b 3d 20 79 3b 0a 20 20 20 20 20 20 20 20  Y += y;.        
7cae0 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a  p->validJD = 0;.
7caf0 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a          computeJ
7cb00 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20 69 66  D(p);.        if
7cb10 28 20 79 21 3d 72 20 29 7b 0a 20 20 20 20 20 20  ( y!=r ){.      
7cb20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73      p->iJD += (s
7cb30 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 28 72  qlite3_int64)((r
7cb40 20 2d 20 79 29 2a 33 36 35 2e 30 2a 38 36 34 30   - y)*365.0*8640
7cb50 30 30 30 30 2e 30 20 2b 20 72 52 6f 75 6e 64 65  0000.0 + rRounde
7cb60 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  r);.        }.  
7cb70 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
7cb80 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 20     rc = 1;.     
7cb90 20 7d 0a 20 20 20 20 20 20 63 6c 65 61 72 59 4d   }.      clearYM
7cba0 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20  D_HMS_TZ(p);.   
7cbb0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
7cbc0 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
7cbd0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
7cbe0 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
7cbf0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63  c;.}../*.** Proc
7cc00 65 73 73 20 74 69 6d 65 20 66 75 6e 63 74 69 6f  ess time functio
7cc10 6e 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 61 72  n arguments.  ar
7cc20 67 76 5b 30 5d 20 69 73 20 61 20 64 61 74 65 2d  gv[0] is a date-
7cc30 74 69 6d 65 20 73 74 61 6d 70 2e 0a 2a 2a 20 61  time stamp..** a
7cc40 72 67 76 5b 31 5d 20 61 6e 64 20 66 6f 6c 6c 6f  rgv[1] and follo
7cc50 77 69 6e 67 20 61 72 65 20 6d 6f 64 69 66 69 65  wing are modifie
7cc60 72 73 2e 20 20 50 61 72 73 65 20 74 68 65 6d 20  rs.  Parse them 
7cc70 61 6c 6c 20 61 6e 64 20 77 72 69 74 65 0a 2a 2a  all and write.**
7cc80 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 74   the resulting t
7cc90 69 6d 65 20 69 6e 74 6f 20 74 68 65 20 44 61 74  ime into the Dat
7cca0 65 54 69 6d 65 20 73 74 72 75 63 74 75 72 65 20  eTime structure 
7ccb0 70 2e 20 20 52 65 74 75 72 6e 20 30 0a 2a 2a 20  p.  Return 0.** 
7ccc0 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 31  on success and 1
7ccd0 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61 6e   if there are an
7cce0 79 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20  y errors..**.** 
7ccf0 49 66 20 74 68 65 72 65 20 61 72 65 20 7a 65 72  If there are zer
7cd00 6f 20 70 61 72 61 6d 65 74 65 72 73 20 28 69 66  o parameters (if
7cd10 20 65 76 65 6e 20 61 72 67 76 5b 30 5d 20 69 73   even argv[0] is
7cd20 20 75 6e 64 65 66 69 6e 65 64 29 0a 2a 2a 20 74   undefined).** t
7cd30 68 65 6e 20 61 73 73 75 6d 65 20 61 20 64 65 66  hen assume a def
7cd40 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 22 6e  ault value of "n
7cd50 6f 77 22 20 66 6f 72 20 61 72 67 76 5b 30 5d 2e  ow" for argv[0].
7cd60 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .*/.static int i
7cd70 73 44 61 74 65 28 0a 20 20 73 71 6c 69 74 65 33  sDate(.  sqlite3
7cd80 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
7cd90 74 2c 20 0a 20 20 69 6e 74 20 61 72 67 63 2c 20  t, .  int argc, 
7cda0 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
7cdb0 20 2a 2a 61 72 67 76 2c 20 0a 20 20 44 61 74 65   **argv, .  Date
7cdc0 54 69 6d 65 20 2a 70 0a 29 7b 0a 20 20 69 6e 74  Time *p.){.  int
7cdd0 20 69 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69   i;.  const unsi
7cde0 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20  gned char *z;.  
7cdf0 69 6e 74 20 65 54 79 70 65 3b 0a 20 20 6d 65 6d  int eType;.  mem
7ce00 73 65 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66  set(p, 0, sizeof
7ce10 28 2a 70 29 29 3b 0a 20 20 69 66 28 20 61 72 67  (*p));.  if( arg
7ce20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 73 65 74 44  c==0 ){.    setD
7ce30 61 74 65 54 69 6d 65 54 6f 43 75 72 72 65 6e 74  ateTimeToCurrent
7ce40 28 63 6f 6e 74 65 78 74 2c 20 70 29 3b 0a 20 20  (context, p);.  
7ce50 7d 65 6c 73 65 20 69 66 28 20 28 65 54 79 70 65  }else if( (eType
7ce60 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
7ce70 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 29 3d  _type(argv[0]))=
7ce80 3d 53 51 4c 49 54 45 5f 46 4c 4f 41 54 0a 20 20  =SQLITE_FLOAT.  
7ce90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7cea0 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54   || eType==SQLIT
7ceb0 45 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 20  E_INTEGER ){.   
7cec0 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69 74   p->iJD = (sqlit
7ced0 65 33 5f 69 6e 74 36 34 29 28 73 71 6c 69 74 65  e3_int64)(sqlite
7cee0 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61  3_value_double(a
7cef0 72 67 76 5b 30 5d 29 2a 38 36 34 30 30 30 30 30  rgv[0])*86400000
7cf00 2e 30 20 2b 20 30 2e 35 29 3b 0a 20 20 20 20 70  .0 + 0.5);.    p
7cf10 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 20  ->validJD = 1;. 
7cf20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20   }else{.    z = 
7cf30 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
7cf40 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  xt(argv[0]);.   
7cf50 20 69 66 28 20 21 7a 20 7c 7c 20 70 61 72 73 65   if( !z || parse
7cf60 44 61 74 65 4f 72 54 69 6d 65 28 63 6f 6e 74 65  DateOrTime(conte
7cf70 78 74 2c 20 28 63 68 61 72 2a 29 7a 2c 20 70 29  xt, (char*)z, p)
7cf80 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
7cf90 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   1;.    }.  }.  
7cfa0 66 6f 72 28 69 3d 31 3b 20 69 3c 61 72 67 63 3b  for(i=1; i<argc;
7cfb0 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 28   i++){.    if( (
7cfc0 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  z = sqlite3_valu
7cfd0 65 5f 74 65 78 74 28 61 72 67 76 5b 69 5d 29 29  e_text(argv[i]))
7cfe0 3d 3d 30 20 7c 7c 20 70 61 72 73 65 4d 6f 64 69  ==0 || parseModi
7cff0 66 69 65 72 28 28 63 68 61 72 2a 29 7a 2c 20 70  fier((char*)z, p
7d000 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
7d010 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  n 1;.    }.  }. 
7d020 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f   return 0;.}.../
7d030 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
7d040 6e 67 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c  ng routines impl
7d050 65 6d 65 6e 74 20 74 68 65 20 76 61 72 69 6f 75  ement the variou
7d060 73 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 20  s date and time 
7d070 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 6f 66 20  functions.** of 
7d080 53 51 4c 69 74 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a  SQLite..*/../*.*
7d090 2a 20 20 20 20 6a 75 6c 69 61 6e 64 61 79 28 20  *    julianday( 
7d0a0 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c  TIMESTRING, MOD,
7d0b0 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a   MOD, ...).**.**
7d0c0 20 52 65 74 75 72 6e 20 74 68 65 20 6a 75 6c 69   Return the juli
7d0d0 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 20 6f 66  an day number of
7d0e0 20 74 68 65 20 64 61 74 65 20 73 70 65 63 69 66   the date specif
7d0f0 69 65 64 20 69 6e 20 74 68 65 20 61 72 67 75 6d  ied in the argum
7d100 65 6e 74 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ents.*/.static v
7d110 6f 69 64 20 6a 75 6c 69 61 6e 64 61 79 46 75 6e  oid juliandayFun
7d120 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
7d130 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
7d140 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c   int argc,.  sql
7d150 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
7d160 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20  v.){.  DateTime 
7d170 78 3b 0a 20 20 69 66 28 20 69 73 44 61 74 65 28  x;.  if( isDate(
7d180 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61  context, argc, a
7d190 72 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20  rgv, &x)==0 ){. 
7d1a0 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 78 29     computeJD(&x)
7d1b0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ;.    sqlite3_re
7d1c0 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74  sult_double(cont
7d1d0 65 78 74 2c 20 78 2e 69 4a 44 2f 38 36 34 30 30  ext, x.iJD/86400
7d1e0 30 30 30 2e 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  000.0);.  }.}../
7d1f0 2a 0a 2a 2a 20 20 20 20 64 61 74 65 74 69 6d 65  *.**    datetime
7d200 28 20 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f  ( TIMESTRING, MO
7d210 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a  D, MOD, ...).**.
7d220 2a 2a 20 52 65 74 75 72 6e 20 59 59 59 59 2d 4d  ** Return YYYY-M
7d230 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2f  M-DD HH:MM:SS.*/
7d240 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 61 74  .static void dat
7d250 65 74 69 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c  etimeFunc(.  sql
7d260 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
7d270 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67  ntext,.  int arg
7d280 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  c,.  sqlite3_val
7d290 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44  ue **argv.){.  D
7d2a0 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 66 28  ateTime x;.  if(
7d2b0 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74 2c   isDate(context,
7d2c0 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26 78 29   argc, argv, &x)
7d2d0 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 72 20  ==0 ){.    char 
7d2e0 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 63  zBuf[100];.    c
7d2f0 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 26 78  omputeYMD_HMS(&x
7d300 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  );.    sqlite3_s
7d310 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a  nprintf(sizeof(z
7d320 42 75 66 29 2c 20 7a 42 75 66 2c 20 22 25 30 34  Buf), zBuf, "%04
7d330 64 2d 25 30 32 64 2d 25 30 32 64 20 25 30 32 64  d-%02d-%02d %02d
7d340 3a 25 30 32 64 3a 25 30 32 64 22 2c 0a 20 20 20  :%02d:%02d",.   
7d350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7d360 20 20 78 2e 59 2c 20 78 2e 4d 2c 20 78 2e 44 2c    x.Y, x.M, x.D,
7d370 20 78 2e 68 2c 20 78 2e 6d 2c 20 28 69 6e 74 29   x.h, x.m, (int)
7d380 28 78 2e 73 29 29 3b 0a 20 20 20 20 73 71 6c 69  (x.s));.    sqli
7d390 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
7d3a0 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d  context, zBuf, -
7d3b0 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  1, SQLITE_TRANSI
7d3c0 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ENT);.  }.}../*.
7d3d0 2a 2a 20 20 20 20 74 69 6d 65 28 20 54 49 4d 45  **    time( TIME
7d3e0 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44  STRING, MOD, MOD
7d3f0 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74  , ...).**.** Ret
7d400 75 72 6e 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2f 0a  urn HH:MM:SS.*/.
7d410 73 74 61 74 69 63 20 76 6f 69 64 20 74 69 6d 65  static void time
7d420 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
7d430 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
7d440 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
7d450 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
7d460 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 69  argv.){.  DateTi
7d470 6d 65 20 78 3b 0a 20 20 69 66 28 20 69 73 44 61  me x;.  if( isDa
7d480 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63  te(context, argc
7d490 2c 20 61 72 67 76 2c 20 26 78 29 3d 3d 30 20 29  , argv, &x)==0 )
7d4a0 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b  {.    char zBuf[
7d4b0 31 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75 74  100];.    comput
7d4c0 65 48 4d 53 28 26 78 29 3b 0a 20 20 20 20 73 71  eHMS(&x);.    sq
7d4d0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
7d4e0 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42 75  izeof(zBuf), zBu
7d4f0 66 2c 20 22 25 30 32 64 3a 25 30 32 64 3a 25 30  f, "%02d:%02d:%0
7d500 32 64 22 2c 20 78 2e 68 2c 20 78 2e 6d 2c 20 28  2d", x.h, x.m, (
7d510 69 6e 74 29 78 2e 73 29 3b 0a 20 20 20 20 73 71  int)x.s);.    sq
7d520 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
7d530 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c  t(context, zBuf,
7d540 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e   -1, SQLITE_TRAN
7d550 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  SIENT);.  }.}../
7d560 2a 0a 2a 2a 20 20 20 20 64 61 74 65 28 20 54 49  *.**    date( TI
7d570 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d  MESTRING, MOD, M
7d580 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52  OD, ...).**.** R
7d590 65 74 75 72 6e 20 59 59 59 59 2d 4d 4d 2d 44 44  eturn YYYY-MM-DD
7d5a0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
7d5b0 64 61 74 65 46 75 6e 63 28 0a 20 20 73 71 6c 69  dateFunc(.  sqli
7d5c0 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
7d5d0 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63  text,.  int argc
7d5e0 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
7d5f0 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61  e **argv.){.  Da
7d600 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 66 28 20  teTime x;.  if( 
7d610 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74 2c 20  isDate(context, 
7d620 61 72 67 63 2c 20 61 72 67 76 2c 20 26 78 29 3d  argc, argv, &x)=
7d630 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 72 20 7a  =0 ){.    char z
7d640 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 63 6f  Buf[100];.    co
7d650 6d 70 75 74 65 59 4d 44 28 26 78 29 3b 0a 20 20  mputeYMD(&x);.  
7d660 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
7d670 74 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c  tf(sizeof(zBuf),
7d680 20 7a 42 75 66 2c 20 22 25 30 34 64 2d 25 30 32   zBuf, "%04d-%02
7d690 64 2d 25 30 32 64 22 2c 20 78 2e 59 2c 20 78 2e  d-%02d", x.Y, x.
7d6a0 4d 2c 20 78 2e 44 29 3b 0a 20 20 20 20 73 71 6c  M, x.D);.    sql
7d6b0 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
7d6c0 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20  (context, zBuf, 
7d6d0 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  -1, SQLITE_TRANS
7d6e0 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  IENT);.  }.}../*
7d6f0 0a 2a 2a 20 20 20 20 73 74 72 66 74 69 6d 65 28  .**    strftime(
7d700 20 46 4f 52 4d 41 54 2c 20 54 49 4d 45 53 54 52   FORMAT, TIMESTR
7d710 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e  ING, MOD, MOD, .
7d720 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  ..).**.** Return
7d730 20 61 20 73 74 72 69 6e 67 20 64 65 73 63 72 69   a string descri
7d740 62 65 64 20 62 79 20 46 4f 52 4d 41 54 2e 20 20  bed by FORMAT.  
7d750 43 6f 6e 76 65 72 73 69 6f 6e 73 20 61 73 20 66  Conversions as f
7d760 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  ollows:.**.**   
7d770 25 64 20 20 64 61 79 20 6f 66 20 6d 6f 6e 74 68  %d  day of month
7d780 0a 2a 2a 20 20 20 25 66 20 20 2a 2a 20 66 72 61  .**   %f  ** fra
7d790 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 73 20  ctional seconds 
7d7a0 20 53 53 2e 53 53 53 0a 2a 2a 20 20 20 25 48 20   SS.SSS.**   %H 
7d7b0 20 68 6f 75 72 20 30 30 2d 32 34 0a 2a 2a 20 20   hour 00-24.**  
7d7c0 20 25 6a 20 20 64 61 79 20 6f 66 20 79 65 61 72   %j  day of year
7d7d0 20 30 30 30 2d 33 36 36 0a 2a 2a 20 20 20 25 4a   000-366.**   %J
7d7e0 20 20 2a 2a 20 4a 75 6c 69 61 6e 20 64 61 79 20    ** Julian day 
7d7f0 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 25 6d 20 20  number.**   %m  
7d800 6d 6f 6e 74 68 20 30 31 2d 31 32 0a 2a 2a 20 20  month 01-12.**  
7d810 20 25 4d 20 20 6d 69 6e 75 74 65 20 30 30 2d 35   %M  minute 00-5
7d820 39 0a 2a 2a 20 20 20 25 73 20 20 73 65 63 6f 6e  9.**   %s  secon
7d830 64 73 20 73 69 6e 63 65 20 31 39 37 30 2d 30 31  ds since 1970-01
7d840 2d 30 31 0a 2a 2a 20 20 20 25 53 20 20 73 65 63  -01.**   %S  sec
7d850 6f 6e 64 73 20 30 30 2d 35 39 0a 2a 2a 20 20 20  onds 00-59.**   
7d860 25 77 20 20 64 61 79 20 6f 66 20 77 65 65 6b 20  %w  day of week 
7d870 30 2d 36 20 20 73 75 6e 64 61 79 3d 3d 30 0a 2a  0-6  sunday==0.*
7d880 2a 20 20 20 25 57 20 20 77 65 65 6b 20 6f 66 20  *   %W  week of 
7d890 79 65 61 72 20 30 30 2d 35 33 0a 2a 2a 20 20 20  year 00-53.**   
7d8a0 25 59 20 20 79 65 61 72 20 30 30 30 30 2d 39 39  %Y  year 0000-99
7d8b0 39 39 0a 2a 2a 20 20 20 25 25 20 20 25 0a 2a 2f  99.**   %%  %.*/
7d8c0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 74 72  .static void str
7d8d0 66 74 69 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c  ftimeFunc(.  sql
7d8e0 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
7d8f0 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67  ntext,.  int arg
7d900 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  c,.  sqlite3_val
7d910 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44  ue **argv.){.  D
7d920 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 75 36 34  ateTime x;.  u64
7d930 20 6e 3b 0a 20 20 73 69 7a 65 5f 74 20 69 2c 6a   n;.  size_t i,j
7d940 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 73  ;.  char *z;.  s
7d950 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 63 6f  qlite3 *db;.  co
7d960 6e 73 74 20 63 68 61 72 20 2a 7a 46 6d 74 20 3d  nst char *zFmt =
7d970 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71   (const char*)sq
7d980 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
7d990 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 63 68 61  (argv[0]);.  cha
7d9a0 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 69  r zBuf[100];.  i
7d9b0 66 28 20 7a 46 6d 74 3d 3d 30 20 7c 7c 20 69 73  f( zFmt==0 || is
7d9c0 44 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72  Date(context, ar
7d9d0 67 63 2d 31 2c 20 61 72 67 76 2b 31 2c 20 26 78  gc-1, argv+1, &x
7d9e0 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62  ) ) return;.  db
7d9f0 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65   = sqlite3_conte
7da00 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e  xt_db_handle(con
7da10 74 65 78 74 29 3b 0a 20 20 66 6f 72 28 69 3d 30  text);.  for(i=0
7da20 2c 20 6e 3d 31 3b 20 7a 46 6d 74 5b 69 5d 3b 20  , n=1; zFmt[i]; 
7da30 69 2b 2b 2c 20 6e 2b 2b 29 7b 0a 20 20 20 20 69  i++, n++){.    i
7da40 66 28 20 7a 46 6d 74 5b 69 5d 3d 3d 27 25 27 20  f( zFmt[i]=='%' 
7da50 29 7b 0a 20 20 20 20 20 20 73 77 69 74 63 68 28  ){.      switch(
7da60 20 7a 46 6d 74 5b 69 2b 31 5d 20 29 7b 0a 20 20   zFmt[i+1] ){.  
7da70 20 20 20 20 20 20 63 61 73 65 20 27 64 27 3a 0a        case 'd':.
7da80 20 20 20 20 20 20 20 20 63 61 73 65 20 27 48 27          case 'H'
7da90 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  :.        case '
7daa0 6d 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65  m':.        case
7dab0 20 27 4d 27 3a 0a 20 20 20 20 20 20 20 20 63 61   'M':.        ca
7dac0 73 65 20 27 53 27 3a 0a 20 20 20 20 20 20 20 20  se 'S':.        
7dad0 63 61 73 65 20 27 57 27 3a 0a 20 20 20 20 20 20  case 'W':.      
7dae0 20 20 20 20 6e 2b 2b 3b 0a 20 20 20 20 20 20 20      n++;.       
7daf0 20 20 20 2f 2a 20 66 61 6c 6c 20 74 68 72 75 20     /* fall thru 
7db00 2a 2f 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  */.        case 
7db10 27 77 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73  'w':.        cas
7db20 65 20 27 25 27 3a 0a 20 20 20 20 20 20 20 20 20  e '%':.         
7db30 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
7db40 63 61 73 65 20 27 66 27 3a 0a 20 20 20 20 20 20  case 'f':.      
7db50 20 20 20 20 6e 20 2b 3d 20 38 3b 0a 20 20 20 20      n += 8;.    
7db60 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
7db70 20 20 20 20 20 63 61 73 65 20 27 6a 27 3a 0a 20       case 'j':. 
7db80 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 33 3b           n += 3;
7db90 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
7dba0 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  ;.        case '
7dbb0 59 27 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 20  Y':.          n 
7dbc0 2b 3d 20 38 3b 0a 20 20 20 20 20 20 20 20 20 20  += 8;.          
7dbd0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63  break;.        c
7dbe0 61 73 65 20 27 73 27 3a 0a 20 20 20 20 20 20 20  ase 's':.       
7dbf0 20 63 61 73 65 20 27 4a 27 3a 0a 20 20 20 20 20   case 'J':.     
7dc00 20 20 20 20 20 6e 20 2b 3d 20 35 30 3b 0a 20 20       n += 50;.  
7dc10 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
7dc20 20 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 0a         default:.
7dc30 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
7dc40 3b 20 20 2f 2a 20 45 52 52 4f 52 2e 20 20 72 65  ;  /* ERROR.  re
7dc50 74 75 72 6e 20 61 20 4e 55 4c 4c 20 2a 2f 0a 20  turn a NULL */. 
7dc60 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 2b 2b       }.      i++
7dc70 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 74 65  ;.    }.  }.  te
7dc80 73 74 63 61 73 65 28 20 6e 3d 3d 73 69 7a 65 6f  stcase( n==sizeo
7dc90 66 28 7a 42 75 66 29 2d 31 20 29 3b 0a 20 20 74  f(zBuf)-1 );.  t
7dca0 65 73 74 63 61 73 65 28 20 6e 3d 3d 73 69 7a 65  estcase( n==size
7dcb0 6f 66 28 7a 42 75 66 29 20 29 3b 0a 20 20 74 65  of(zBuf) );.  te
7dcc0 73 74 63 61 73 65 28 20 6e 3d 3d 28 75 36 34 29  stcase( n==(u64)
7dcd0 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
7dce0 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 2b  E_LIMIT_LENGTH]+
7dcf0 31 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28  1 );.  testcase(
7dd00 20 6e 3d 3d 28 75 36 34 29 64 62 2d 3e 61 4c 69   n==(u64)db->aLi
7dd10 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
7dd20 5f 4c 45 4e 47 54 48 5d 20 29 3b 0a 20 20 69 66  _LENGTH] );.  if
7dd30 28 20 6e 3c 73 69 7a 65 6f 66 28 7a 42 75 66 29  ( n<sizeof(zBuf)
7dd40 20 29 7b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66   ){.    z = zBuf
7dd50 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3e  ;.  }else if( n>
7dd60 28 75 36 34 29 64 62 2d 3e 61 4c 69 6d 69 74 5b  (u64)db->aLimit[
7dd70 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e  SQLITE_LIMIT_LEN
7dd80 47 54 48 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69  GTH] ){.    sqli
7dd90 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
7dda0 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78 74 29  _toobig(context)
7ddb0 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
7ddc0 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20 73  }else{.    z = s
7ddd0 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
7dde0 77 28 64 62 2c 20 28 69 6e 74 29 6e 29 3b 0a 20  w(db, (int)n);. 
7ddf0 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20     if( z==0 ){. 
7de00 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
7de10 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28  ult_error_nomem(
7de20 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20  context);.      
7de30 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
7de40 7d 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 78  }.  computeJD(&x
7de50 29 3b 0a 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f  );.  computeYMD_
7de60 48 4d 53 28 26 78 29 3b 0a 20 20 66 6f 72 28 69  HMS(&x);.  for(i
7de70 3d 6a 3d 30 3b 20 7a 46 6d 74 5b 69 5d 3b 20 69  =j=0; zFmt[i]; i
7de80 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 46 6d  ++){.    if( zFm
7de90 74 5b 69 5d 21 3d 27 25 27 20 29 7b 0a 20 20 20  t[i]!='%' ){.   
7dea0 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 46 6d 74     z[j++] = zFmt
7deb0 5b 69 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  [i];.    }else{.
7dec0 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20        i++;.     
7ded0 20 73 77 69 74 63 68 28 20 7a 46 6d 74 5b 69 5d   switch( zFmt[i]
7dee0 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 73 65   ){.        case
7def0 20 27 64 27 3a 20 20 73 71 6c 69 74 65 33 5f 73   'd':  sqlite3_s
7df00 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d  nprintf(3, &z[j]
7df10 2c 22 25 30 32 64 22 2c 78 2e 44 29 3b 20 6a 2b  ,"%02d",x.D); j+
7df20 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  =2; break;.     
7df30 20 20 20 63 61 73 65 20 27 66 27 3a 20 7b 0a 20     case 'f': {. 
7df40 20 20 20 20 20 20 20 20 20 64 6f 75 62 6c 65 20           double 
7df50 73 20 3d 20 78 2e 73 3b 0a 20 20 20 20 20 20 20  s = x.s;.       
7df60 20 20 20 69 66 28 20 73 3e 35 39 2e 39 39 39 20     if( s>59.999 
7df70 29 20 73 20 3d 20 35 39 2e 39 39 39 3b 0a 20 20  ) s = 59.999;.  
7df80 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
7df90 73 6e 70 72 69 6e 74 66 28 37 2c 20 26 7a 5b 6a  snprintf(7, &z[j
7dfa0 5d 2c 22 25 30 36 2e 33 66 22 2c 20 73 29 3b 0a  ],"%06.3f", s);.
7dfb0 20 20 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 73            j += s
7dfc0 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 26  qlite3Strlen30(&
7dfd0 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 20 20 20  z[j]);.         
7dfe0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
7dff0 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  }.        case '
7e000 48 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  H':  sqlite3_snp
7e010 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22  rintf(3, &z[j],"
7e020 25 30 32 64 22 2c 78 2e 68 29 3b 20 6a 2b 3d 32  %02d",x.h); j+=2
7e030 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20  ; break;.       
7e040 20 63 61 73 65 20 27 57 27 3a 20 2f 2a 20 46 61   case 'W': /* Fa
7e050 6c 6c 20 74 68 72 75 20 2a 2f 0a 20 20 20 20 20  ll thru */.     
7e060 20 20 20 63 61 73 65 20 27 6a 27 3a 20 7b 0a 20     case 'j': {. 
7e070 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 44 61           int nDa
7e080 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  y;             /
7e090 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 61 79 73  * Number of days
7e0a0 20 73 69 6e 63 65 20 31 73 74 20 64 61 79 20 6f   since 1st day o
7e0b0 66 20 79 65 61 72 20 2a 2f 0a 20 20 20 20 20 20  f year */.      
7e0c0 20 20 20 20 44 61 74 65 54 69 6d 65 20 79 20 3d      DateTime y =
7e0d0 20 78 3b 0a 20 20 20 20 20 20 20 20 20 20 79 2e   x;.          y.
7e0e0 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20  validJD = 0;.   
7e0f0 20 20 20 20 20 20 20 79 2e 4d 20 3d 20 31 3b 0a         y.M = 1;.
7e100 20 20 20 20 20 20 20 20 20 20 79 2e 44 20 3d 20            y.D = 
7e110 31 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6d  1;.          com
7e120 70 75 74 65 4a 44 28 26 79 29 3b 0a 20 20 20 20  puteJD(&y);.    
7e130 20 20 20 20 20 20 6e 44 61 79 20 3d 20 28 69 6e        nDay = (in
7e140 74 29 28 28 78 2e 69 4a 44 2d 79 2e 69 4a 44 2b  t)((x.iJD-y.iJD+
7e150 34 33 32 30 30 30 30 30 29 2f 38 36 34 30 30 30  43200000)/864000
7e160 30 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  00);.          i
7e170 66 28 20 7a 46 6d 74 5b 69 5d 3d 3d 27 57 27 20  f( zFmt[i]=='W' 
7e180 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  ){.            i
7e190 6e 74 20 77 64 3b 20 20 20 2f 2a 20 30 3d 4d 6f  nt wd;   /* 0=Mo
7e1a0 6e 64 61 79 2c 20 31 3d 54 75 65 73 64 61 79 2c  nday, 1=Tuesday,
7e1b0 20 2e 2e 2e 20 36 3d 53 75 6e 64 61 79 20 2a 2f   ... 6=Sunday */
7e1c0 0a 20 20 20 20 20 20 20 20 20 20 20 20 77 64 20  .            wd 
7e1d0 3d 20 28 69 6e 74 29 28 28 28 78 2e 69 4a 44 2b  = (int)(((x.iJD+
7e1e0 34 33 32 30 30 30 30 30 29 2f 38 36 34 30 30 30  43200000)/864000
7e1f0 30 30 29 25 37 29 3b 0a 20 20 20 20 20 20 20 20  00)%7);.        
7e200 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
7e210 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25  intf(3, &z[j],"%
7e220 30 32 64 22 2c 28 6e 44 61 79 2b 37 2d 77 64 29  02d",(nDay+7-wd)
7e230 2f 37 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  /7);.           
7e240 20 6a 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 20   j += 2;.       
7e250 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
7e260 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
7e270 70 72 69 6e 74 66 28 34 2c 20 26 7a 5b 6a 5d 2c  printf(4, &z[j],
7e280 22 25 30 33 64 22 2c 6e 44 61 79 2b 31 29 3b 0a  "%03d",nDay+1);.
7e290 20 20 20 20 20 20 20 20 20 20 20 20 6a 20 2b 3d              j +=
7e2a0 20 33 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a   3;.          }.
7e2b0 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
7e2c0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
7e2d0 20 20 20 63 61 73 65 20 27 4a 27 3a 20 7b 0a 20     case 'J': {. 
7e2e0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
7e2f0 5f 73 6e 70 72 69 6e 74 66 28 32 30 2c 20 26 7a  _snprintf(20, &z
7e300 5b 6a 5d 2c 22 25 2e 31 36 67 22 2c 78 2e 69 4a  [j],"%.16g",x.iJ
7e310 44 2f 38 36 34 30 30 30 30 30 2e 30 29 3b 0a 20  D/86400000.0);. 
7e320 20 20 20 20 20 20 20 20 20 6a 2b 3d 73 71 6c 69           j+=sqli
7e330 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 5b 6a  te3Strlen30(&z[j
7e340 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72  ]);.          br
7e350 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
7e360 20 20 20 20 20 20 20 63 61 73 65 20 27 6d 27 3a         case 'm':
7e370 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
7e380 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32  tf(3, &z[j],"%02
7e390 64 22 2c 78 2e 4d 29 3b 20 6a 2b 3d 32 3b 20 62  d",x.M); j+=2; b
7e3a0 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61  reak;.        ca
7e3b0 73 65 20 27 4d 27 3a 20 20 73 71 6c 69 74 65 33  se 'M':  sqlite3
7e3c0 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b  _snprintf(3, &z[
7e3d0 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 6d 29 3b 20  j],"%02d",x.m); 
7e3e0 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20  j+=2; break;.   
7e3f0 20 20 20 20 20 63 61 73 65 20 27 73 27 3a 20 7b       case 's': {
7e400 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
7e410 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 30 2c 26  e3_snprintf(30,&
7e420 7a 5b 6a 5d 2c 22 25 6c 6c 64 22 2c 0a 20 20 20  z[j],"%lld",.   
7e430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7e440 20 20 20 20 20 20 20 20 28 69 36 34 29 28 78 2e          (i64)(x.
7e450 69 4a 44 2f 31 30 30 30 20 2d 20 32 31 30 38 36  iJD/1000 - 21086
7e460 36 37 36 2a 28 69 36 34 29 31 30 30 30 30 29 29  676*(i64)10000))
7e470 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 20 2b 3d  ;.          j +=
7e480 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
7e490 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 20  (&z[j]);.       
7e4a0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
7e4b0 20 20 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65    }.        case
7e4c0 20 27 53 27 3a 20 20 73 71 6c 69 74 65 33 5f 73   'S':  sqlite3_s
7e4d0 6e 70 72 69 6e 74 66 28 33 2c 26 7a 5b 6a 5d 2c  nprintf(3,&z[j],
7e4e0 22 25 30 32 64 22 2c 28 69 6e 74 29 78 2e 73 29  "%02d",(int)x.s)
7e4f0 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20  ; j+=2; break;. 
7e500 20 20 20 20 20 20 20 63 61 73 65 20 27 77 27 3a         case 'w':
7e510 20 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 5b 6a   {.          z[j
7e520 2b 2b 5d 20 3d 20 28 63 68 61 72 29 28 28 28 78  ++] = (char)(((x
7e530 2e 69 4a 44 2b 31 32 39 36 30 30 30 30 30 29 2f  .iJD+129600000)/
7e540 38 36 34 30 30 30 30 30 29 20 25 20 37 29 20 2b  86400000) % 7) +
7e550 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20   '0';.          
7e560 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
7e570 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 59  .        case 'Y
7e580 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 73  ': {.          s
7e590 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
7e5a0 35 2c 26 7a 5b 6a 5d 2c 22 25 30 34 64 22 2c 78  5,&z[j],"%04d",x
7e5b0 2e 59 29 3b 20 6a 2b 3d 73 71 6c 69 74 65 33 53  .Y); j+=sqlite3S
7e5c0 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a  trlen30(&z[j]);.
7e5d0 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
7e5e0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
7e5f0 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 7a 5b     default:   z[
7e600 6a 2b 2b 5d 20 3d 20 27 25 27 3b 20 62 72 65 61  j++] = '%'; brea
7e610 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
7e620 0a 20 20 7d 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b  .  }.  z[j] = 0;
7e630 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
7e640 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  t_text(context, 
7e650 7a 2c 20 2d 31 2c 0a 20 20 20 20 20 20 20 20 20  z, -1,.         
7e660 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 3d 3d               z==
7e670 7a 42 75 66 20 3f 20 53 51 4c 49 54 45 5f 54 52  zBuf ? SQLITE_TR
7e680 41 4e 53 49 45 4e 54 20 3a 20 53 51 4c 49 54 45  ANSIENT : SQLITE
7e690 5f 44 59 4e 41 4d 49 43 29 3b 0a 7d 0a 0a 2f 2a  _DYNAMIC);.}../*
7e6a0 0a 2a 2a 20 63 75 72 72 65 6e 74 5f 74 69 6d 65  .** current_time
7e6b0 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  ().**.** This fu
7e6c0 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74  nction returns t
7e6d0 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 61 73  he same value as
7e6e0 20 74 69 6d 65 28 27 6e 6f 77 27 29 2e 0a 2a 2f   time('now')..*/
7e6f0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 74 69  .static void cti
7e700 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  meFunc(.  sqlite
7e710 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
7e720 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65  xt,.  int NotUse
7e730 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  d,.  sqlite3_val
7e740 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b  ue **NotUsed2.){
7e750 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
7e760 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f  TER2(NotUsed, No
7e770 74 55 73 65 64 32 29 3b 0a 20 20 74 69 6d 65 46  tUsed2);.  timeF
7e780 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20 30 2c 20  unc(context, 0, 
7e790 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 72  0);.}../*.** cur
7e7a0 72 65 6e 74 5f 64 61 74 65 28 29 0a 2a 2a 0a 2a  rent_date().**.*
7e7b0 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
7e7c0 72 65 74 75 72 6e 73 20 74 68 65 20 73 61 6d 65  returns the same
7e7d0 20 76 61 6c 75 65 20 61 73 20 64 61 74 65 28 27   value as date('
7e7e0 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  now')..*/.static
7e7f0 20 76 6f 69 64 20 63 64 61 74 65 46 75 6e 63 28   void cdateFunc(
7e800 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
7e810 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
7e820 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71  nt NotUsed,.  sq
7e830 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f  lite3_value **No
7e840 74 55 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55 53  tUsed2.){.  UNUS
7e850 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f  ED_PARAMETER2(No
7e860 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29  tUsed, NotUsed2)
7e870 3b 0a 20 20 64 61 74 65 46 75 6e 63 28 63 6f 6e  ;.  dateFunc(con
7e880 74 65 78 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a  text, 0, 0);.}..
7e890 2f 2a 0a 2a 2a 20 63 75 72 72 65 6e 74 5f 74 69  /*.** current_ti
7e8a0 6d 65 73 74 61 6d 70 28 29 0a 2a 2a 0a 2a 2a 20  mestamp().**.** 
7e8b0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  This function re
7e8c0 74 75 72 6e 73 20 74 68 65 20 73 61 6d 65 20 76  turns the same v
7e8d0 61 6c 75 65 20 61 73 20 64 61 74 65 74 69 6d 65  alue as datetime
7e8e0 28 27 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61 74  ('now')..*/.stat
7e8f0 69 63 20 76 6f 69 64 20 63 74 69 6d 65 73 74 61  ic void ctimesta
7e900 6d 70 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  mpFunc(.  sqlite
7e910 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
7e920 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65  xt,.  int NotUse
7e930 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  d,.  sqlite3_val
7e940 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b  ue **NotUsed2.){
7e950 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
7e960 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f  TER2(NotUsed, No
7e970 74 55 73 65 64 32 29 3b 0a 20 20 64 61 74 65 74  tUsed2);.  datet
7e980 69 6d 65 46 75 6e 63 28 63 6f 6e 74 65 78 74 2c  imeFunc(context,
7e990 20 30 2c 20 30 29 3b 0a 7d 0a 23 65 6e 64 69 66   0, 0);.}.#endif
7e9a0 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c   /* !defined(SQL
7e9b0 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d  ITE_OMIT_DATETIM
7e9c0 45 5f 46 55 4e 43 53 29 20 2a 2f 0a 0a 23 69 66  E_FUNCS) */..#if
7e9d0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
7e9e0 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 0a 2f  DATETIME_FUNCS./
7e9f0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 69 62 72  *.** If the libr
7ea00 61 72 79 20 69 73 20 63 6f 6d 70 69 6c 65 64 20  ary is compiled 
7ea10 74 6f 20 6f 6d 69 74 20 74 68 65 20 66 75 6c 6c  to omit the full
7ea20 2d 73 63 61 6c 65 20 64 61 74 65 20 61 6e 64 20  -scale date and 
7ea30 74 69 6d 65 0a 2a 2a 20 68 61 6e 64 6c 69 6e 67  time.** handling
7ea40 20 28 74 6f 20 67 65 74 20 61 20 73 6d 61 6c 6c   (to get a small
7ea50 65 72 20 62 69 6e 61 72 79 29 2c 20 74 68 65 20  er binary), the 
7ea60 66 6f 6c 6c 6f 77 69 6e 67 20 6d 69 6e 69 6d 61  following minima
7ea70 6c 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 6f 66 20  l version.** of 
7ea80 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 63 75  the functions cu
7ea90 72 72 65 6e 74 5f 74 69 6d 65 28 29 2c 20 63 75  rrent_time(), cu
7eaa0 72 72 65 6e 74 5f 64 61 74 65 28 29 20 61 6e 64  rrent_date() and
7eab0 20 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74 61   current_timesta
7eac0 6d 70 28 29 0a 2a 2a 20 61 72 65 20 69 6e 63 6c  mp().** are incl
7ead0 75 64 65 64 20 69 6e 73 74 65 61 64 2e 20 54 68  uded instead. Th
7eae0 69 73 20 69 73 20 74 6f 20 73 75 70 70 6f 72 74  is is to support
7eaf0 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74   column declarat
7eb00 69 6f 6e 73 20 74 68 61 74 0a 2a 2a 20 69 6e 63  ions that.** inc
7eb10 6c 75 64 65 20 22 44 45 46 41 55 4c 54 20 43 55  lude "DEFAULT CU
7eb20 52 52 45 4e 54 5f 54 49 4d 45 22 20 65 74 63 2e  RRENT_TIME" etc.
7eb30 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
7eb40 74 69 6f 6e 20 75 73 65 73 20 74 68 65 20 43 2d  tion uses the C-
7eb50 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e  library function
7eb60 73 20 74 69 6d 65 28 29 2c 20 67 6d 74 69 6d 65  s time(), gmtime
7eb70 28 29 0a 2a 2a 20 61 6e 64 20 73 74 72 66 74 69  ().** and strfti
7eb80 6d 65 28 29 2e 20 54 68 65 20 66 6f 72 6d 61 74  me(). The format
7eb90 20 73 74 72 69 6e 67 20 74 6f 20 70 61 73 73 20   string to pass 
7eba0 74 6f 20 73 74 72 66 74 69 6d 65 28 29 20 69 73  to strftime() is
7ebb0 20 73 75 70 70 6c 69 65 64 0a 2a 2a 20 61 73 20   supplied.** as 
7ebc0 74 68 65 20 75 73 65 72 2d 64 61 74 61 20 66 6f  the user-data fo
7ebd0 72 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 0a  r the function..
7ebe0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
7ebf0 75 72 72 65 6e 74 54 69 6d 65 46 75 6e 63 28 0a  urrentTimeFunc(.
7ec00 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
7ec10 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
7ec20 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65  t argc,.  sqlite
7ec30 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
7ec40 7b 0a 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20  {.  time_t t;.  
7ec50 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 20 3d 20  char *zFormat = 
7ec60 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f  (char *)sqlite3_
7ec70 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78  user_data(contex
7ec80 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  t);.  sqlite3 *d
7ec90 62 3b 0a 20 20 64 6f 75 62 6c 65 20 72 54 3b 0a  b;.  double rT;.
7eca0 20 20 63 68 61 72 20 7a 42 75 66 5b 32 30 5d 3b    char zBuf[20];
7ecb0 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
7ecc0 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 55 4e  ETER(argc);.  UN
7ecd0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61  USED_PARAMETER(a
7ece0 72 67 76 29 3b 0a 0a 20 20 64 62 20 3d 20 73 71  rgv);..  db = sq
7ecf0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62  lite3_context_db
7ed00 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29  _handle(context)
7ed10 3b 0a 20 20 73 71 6c 69 74 65 33 4f 73 43 75 72  ;.  sqlite3OsCur
7ed20 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70 56 66  rentTime(db->pVf
7ed30 73 2c 20 26 72 54 29 3b 0a 23 69 66 6e 64 65 66  s, &rT);.#ifndef
7ed40 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
7ed50 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 74 20  ATING_POINT.  t 
7ed60 3d 20 38 36 34 30 30 2e 30 2a 28 72 54 20 2d 20  = 86400.0*(rT - 
7ed70 32 34 34 30 35 38 37 2e 35 29 20 2b 20 30 2e 35  2440587.5) + 0.5
7ed80 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 77 69 74  ;.#else.  /* wit
7ed90 68 6f 75 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f  hout floating po
7eda0 69 6e 74 20 73 75 70 70 6f 72 74 2c 20 72 54 20  int support, rT 
7edb0 77 69 6c 6c 20 68 61 76 65 0a 20 20 2a 2a 20 61  will have.  ** a
7edc0 6c 72 65 61 64 79 20 6c 6f 73 74 20 66 72 61 63  lready lost frac
7edd0 74 69 6f 6e 61 6c 20 64 61 79 20 70 72 65 63 69  tional day preci
7ede0 73 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 74 20 3d  sion..  */.  t =
7edf0 20 38 36 34 30 30 20 2a 20 28 72 54 20 2d 20 32   86400 * (rT - 2
7ee00 34 34 30 35 38 37 29 20 2d 20 34 33 32 30 30 3b  440587) - 43200;
7ee10 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 48  .#endif.#ifdef H
7ee20 41 56 45 5f 47 4d 54 49 4d 45 5f 52 0a 20 20 7b  AVE_GMTIME_R.  {
7ee30 0a 20 20 20 20 73 74 72 75 63 74 20 74 6d 20 73  .    struct tm s
7ee40 4e 6f 77 3b 0a 20 20 20 20 67 6d 74 69 6d 65 5f  Now;.    gmtime_
7ee50 72 28 26 74 2c 20 26 73 4e 6f 77 29 3b 0a 20 20  r(&t, &sNow);.  
7ee60 20 20 73 74 72 66 74 69 6d 65 28 7a 42 75 66 2c    strftime(zBuf,
7ee70 20 32 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 26 73   20, zFormat, &s
7ee80 4e 6f 77 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a  Now);.  }.#else.
7ee90 20 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74    {.    struct t
7eea0 6d 20 2a 70 54 6d 3b 0a 20 20 20 20 73 71 6c 69  m *pTm;.    sqli
7eeb0 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
7eec0 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
7eed0 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
7eee0 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a  TATIC_MASTER));.
7eef0 20 20 20 20 70 54 6d 20 3d 20 67 6d 74 69 6d 65      pTm = gmtime
7ef00 28 26 74 29 3b 0a 20 20 20 20 73 74 72 66 74 69  (&t);.    strfti
7ef10 6d 65 28 7a 42 75 66 2c 20 32 30 2c 20 7a 46 6f  me(zBuf, 20, zFo
7ef20 72 6d 61 74 2c 20 70 54 6d 29 3b 0a 20 20 20 20  rmat, pTm);.    
7ef30 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
7ef40 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78  ave(sqlite3Mutex
7ef50 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
7ef60 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
7ef70 29 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  ));.  }.#endif..
7ef80 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
7ef90 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a  _text(context, z
7efa0 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f  Buf, -1, SQLITE_
7efb0 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 65  TRANSIENT);.}.#e
7efc0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ndif../*.** This
7efd0 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 74   function regist
7efe0 65 72 65 64 20 61 6c 6c 20 6f 66 20 74 68 65 20  ered all of the 
7eff0 61 62 6f 76 65 20 43 20 66 75 6e 63 74 69 6f 6e  above C function
7f000 73 20 61 73 20 53 51 4c 0a 2a 2a 20 66 75 6e 63  s as SQL.** func
7f010 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 73 68 6f  tions.  This sho
7f020 75 6c 64 20 62 65 20 74 68 65 20 6f 6e 6c 79 20  uld be the only 
7f030 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20  routine in this 
7f040 66 69 6c 65 20 77 69 74 68 0a 2a 2a 20 65 78 74  file with.** ext
7f050 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a  ernal linkage..*
7f060 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
7f070 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67   void sqlite3Reg
7f080 69 73 74 65 72 44 61 74 65 54 69 6d 65 46 75 6e  isterDateTimeFun
7f090 63 74 69 6f 6e 73 28 76 6f 69 64 29 7b 0a 20 20  ctions(void){.  
7f0a0 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53  static SQLITE_WS
7f0b0 44 20 46 75 6e 63 44 65 66 20 61 44 61 74 65 54  D FuncDef aDateT
7f0c0 69 6d 65 46 75 6e 63 73 5b 5d 20 3d 20 7b 0a 23  imeFuncs[] = {.#
7f0d0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
7f0e0 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55 4e 43  IT_DATETIME_FUNC
7f0f0 53 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6a  S.    FUNCTION(j
7f100 75 6c 69 61 6e 64 61 79 2c 20 20 20 20 20 20 20  ulianday,       
7f110 20 2d 31 2c 20 30 2c 20 30 2c 20 6a 75 6c 69 61   -1, 0, 0, julia
7f120 6e 64 61 79 46 75 6e 63 20 29 2c 0a 20 20 20 20  ndayFunc ),.    
7f130 46 55 4e 43 54 49 4f 4e 28 64 61 74 65 2c 20 20  FUNCTION(date,  
7f140 20 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30             -1, 0
7f150 2c 20 30 2c 20 64 61 74 65 46 75 6e 63 20 20 20  , 0, dateFunc   
7f160 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49     ),.    FUNCTI
7f170 4f 4e 28 74 69 6d 65 2c 20 20 20 20 20 20 20 20  ON(time,        
7f180 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 74       -1, 0, 0, t
7f190 69 6d 65 46 75 6e 63 20 20 20 20 20 20 29 2c 0a  imeFunc      ),.
7f1a0 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 64 61 74      FUNCTION(dat
7f1b0 65 74 69 6d 65 2c 20 20 20 20 20 20 20 20 20 2d  etime,         -
7f1c0 31 2c 20 30 2c 20 30 2c 20 64 61 74 65 74 69 6d  1, 0, 0, datetim
7f1d0 65 46 75 6e 63 20 20 29 2c 0a 20 20 20 20 46 55  eFunc  ),.    FU
7f1e0 4e 43 54 49 4f 4e 28 73 74 72 66 74 69 6d 65 2c  NCTION(strftime,
7f1f0 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20           -1, 0, 
7f200 30 2c 20 73 74 72 66 74 69 6d 65 46 75 6e 63 20  0, strftimeFunc 
7f210 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e   ),.    FUNCTION
7f220 28 63 75 72 72 65 6e 74 5f 74 69 6d 65 2c 20 20  (current_time,  
7f230 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 63 74 69      0, 0, 0, cti
7f240 6d 65 46 75 6e 63 20 20 20 20 20 29 2c 0a 20 20  meFunc     ),.  
7f250 20 20 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65    FUNCTION(curre
7f260 6e 74 5f 74 69 6d 65 73 74 61 6d 70 2c 20 30 2c  nt_timestamp, 0,
7f270 20 30 2c 20 30 2c 20 63 74 69 6d 65 73 74 61 6d   0, 0, ctimestam
7f280 70 46 75 6e 63 29 2c 0a 20 20 20 20 46 55 4e 43  pFunc),.    FUNC
7f290 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 64 61 74  TION(current_dat
7f2a0 65 2c 20 20 20 20 20 20 30 2c 20 30 2c 20 30 2c  e,      0, 0, 0,
7f2b0 20 63 64 61 74 65 46 75 6e 63 20 20 20 20 20 29   cdateFunc     )
7f2c0 2c 0a 23 65 6c 73 65 0a 20 20 20 20 53 54 52 5f  ,.#else.    STR_
7f2d0 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74  FUNCTION(current
7f2e0 5f 74 69 6d 65 2c 20 20 20 20 20 20 30 2c 20 22  _time,      0, "
7f2f0 25 48 3a 25 4d 3a 25 53 22 2c 20 20 20 20 20 20  %H:%M:%S",      
7f300 20 20 20 20 30 2c 20 63 75 72 72 65 6e 74 54 69      0, currentTi
7f310 6d 65 46 75 6e 63 29 2c 0a 20 20 20 20 53 54 52  meFunc),.    STR
7f320 5f 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e  _FUNCTION(curren
7f330 74 5f 74 69 6d 65 73 74 61 6d 70 2c 20 30 2c 20  t_timestamp, 0, 
7f340 22 25 59 2d 25 6d 2d 25 64 22 2c 20 20 20 20 20  "%Y-%m-%d",     
7f350 20 20 20 20 20 30 2c 20 63 75 72 72 65 6e 74 54       0, currentT
7f360 69 6d 65 46 75 6e 63 29 2c 0a 20 20 20 20 53 54  imeFunc),.    ST
7f370 52 5f 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65  R_FUNCTION(curre
7f380 6e 74 5f 64 61 74 65 2c 20 20 20 20 20 20 30 2c  nt_date,      0,
7f390 20 22 25 59 2d 25 6d 2d 25 64 20 25 48 3a 25 4d   "%Y-%m-%d %H:%M
7f3a0 3a 25 53 22 2c 20 30 2c 20 63 75 72 72 65 6e 74  :%S", 0, current
7f3b0 54 69 6d 65 46 75 6e 63 29 2c 0a 23 65 6e 64 69  TimeFunc),.#endi
7f3c0 66 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a  f.  };.  int i;.
7f3d0 20 20 46 75 6e 63 44 65 66 48 61 73 68 20 2a 70    FuncDefHash *p
7f3e0 48 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c 28 46  Hash = &GLOBAL(F
7f3f0 75 6e 63 44 65 66 48 61 73 68 2c 20 73 71 6c 69  uncDefHash, sqli
7f400 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f  te3GlobalFunctio
7f410 6e 73 29 3b 0a 20 20 46 75 6e 63 44 65 66 20 2a  ns);.  FuncDef *
7f420 61 46 75 6e 63 20 3d 20 28 46 75 6e 63 44 65 66  aFunc = (FuncDef
7f430 2a 29 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65  *)&GLOBAL(FuncDe
7f440 66 2c 20 61 44 61 74 65 54 69 6d 65 46 75 6e 63  f, aDateTimeFunc
7f450 73 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20  s);..  for(i=0; 
7f460 69 3c 41 72 72 61 79 53 69 7a 65 28 61 44 61 74  i<ArraySize(aDat
7f470 65 54 69 6d 65 46 75 6e 63 73 29 3b 20 69 2b 2b  eTimeFuncs); i++
7f480 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 75  ){.    sqlite3Fu
7f490 6e 63 44 65 66 49 6e 73 65 72 74 28 70 48 61 73  ncDefInsert(pHas
7f4a0 68 2c 20 26 61 46 75 6e 63 5b 69 5d 29 3b 0a 20  h, &aFunc[i]);. 
7f4b0 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a   }.}../*********
7f4c0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 64 61 74  ***** End of dat
7f4d0 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
7f4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7f4f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7f500 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
7f510 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
7f520 20 6f 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   os.c **********
7f530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7f540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7f550 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35  ****/./*.** 2005
7f560 20 4e 6f 76 65 6d 62 65 72 20 32 39 0a 2a 2a 0a   November 29.**.
7f570 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
7f580 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
7f590 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
7f5a0 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
7f5b0 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
7f5c0 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
7f5d0 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
7f5e0 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
7f5f0 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
7f600 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
7f610 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
7f620 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
7f630 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
7f640 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
7f650 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
7f660 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
7f670 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
7f680 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
7f690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7f6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7f6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7f6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7f6d0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
7f6e0 20 63 6f 6e 74 61 69 6e 73 20 4f 53 20 69 6e 74   contains OS int
7f6f0 65 72 66 61 63 65 20 63 6f 64 65 20 74 68 61 74  erface code that
7f700 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c   is common to al
7f710 6c 0a 2a 2a 20 61 72 63 68 69 74 65 63 74 75 72  l.** architectur
7f720 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f  es..**.** $Id: o
7f730 73 2e 63 2c 76 20 31 2e 31 32 37 20 32 30 30 39  s.c,v 1.127 2009
7f740 2f 30 37 2f 32 37 20 31 31 3a 34 31 3a 32 31 20  /07/27 11:41:21 
7f750 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20  danielk1977 Exp 
7f760 24 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 5f 53 51  $.*/.#define _SQ
7f770 4c 49 54 45 5f 4f 53 5f 43 5f 20 31 0a 23 75 6e  LITE_OS_C_ 1.#un
7f780 64 65 66 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 43  def _SQLITE_OS_C
7f790 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 64 65 66  _../*.** The def
7f7a0 61 75 6c 74 20 53 51 4c 69 74 65 20 73 71 6c 69  ault SQLite sqli
7f7b0 74 65 33 5f 76 66 73 20 69 6d 70 6c 65 6d 65 6e  te3_vfs implemen
7f7c0 74 61 74 69 6f 6e 73 20 64 6f 20 6e 6f 74 20 61  tations do not a
7f7d0 6c 6c 6f 63 61 74 65 0a 2a 2a 20 6d 65 6d 6f 72  llocate.** memor
7f7e0 79 20 28 61 63 74 75 61 6c 6c 79 2c 20 6f 73 5f  y (actually, os_
7f7f0 75 6e 69 78 2e 63 20 61 6c 6c 6f 63 61 74 65 73  unix.c allocates
7f800 20 61 20 73 6d 61 6c 6c 20 61 6d 6f 75 6e 74 20   a small amount 
7f810 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 66 72 6f  of memory.** fro
7f820 6d 20 77 69 74 68 69 6e 20 4f 73 4f 70 65 6e 28  m within OsOpen(
7f830 29 29 2c 20 62 75 74 20 73 6f 6d 65 20 74 68 69  )), but some thi
7f840 72 64 2d 70 61 72 74 79 20 69 6d 70 6c 65 6d 65  rd-party impleme
7f850 6e 74 61 74 69 6f 6e 73 20 6d 61 79 2e 0a 2a 2a  ntations may..**
7f860 20 53 6f 20 77 65 20 74 65 73 74 20 74 68 65 20   So we test the 
7f870 65 66 66 65 63 74 73 20 6f 66 20 61 20 6d 61 6c  effects of a mal
7f880 6c 6f 63 28 29 20 66 61 69 6c 69 6e 67 20 61 6e  loc() failing an
7f890 64 20 74 68 65 20 73 71 6c 69 74 65 33 4f 73 58  d the sqlite3OsX
7f8a0 58 58 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  XX().** function
7f8b0 20 72 65 74 75 72 6e 69 6e 67 20 53 51 4c 49 54   returning SQLIT
7f8c0 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 75 73  E_IOERR_NOMEM us
7f8d0 69 6e 67 20 74 68 65 20 44 4f 5f 4f 53 5f 4d 41  ing the DO_OS_MA
7f8e0 4c 4c 4f 43 5f 54 45 53 54 20 6d 61 63 72 6f 2e  LLOC_TEST macro.
7f8f0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  .**.** The follo
7f900 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e 73 20 61  wing functions a
7f910 72 65 20 69 6e 73 74 72 75 6d 65 6e 74 65 64 20  re instrumented 
7f920 66 6f 72 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69  for malloc() fai
7f930 6c 75 72 65 20 0a 2a 2a 20 74 65 73 74 69 6e 67  lure .** testing
7f940 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69  :.**.**     sqli
7f950 74 65 33 4f 73 4f 70 65 6e 28 29 0a 2a 2a 20 20  te3OsOpen().**  
7f960 20 20 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64     sqlite3OsRead
7f970 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65  ().**     sqlite
7f980 33 4f 73 57 72 69 74 65 28 29 0a 2a 2a 20 20 20  3OsWrite().**   
7f990 20 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28    sqlite3OsSync(
7f9a0 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33  ).**     sqlite3
7f9b0 4f 73 4c 6f 63 6b 28 29 0a 2a 2a 0a 2a 2f 0a 23  OsLock().**.*/.#
7f9c0 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
7f9d0 45 5f 54 45 53 54 29 20 26 26 20 28 53 51 4c 49  E_TEST) && (SQLI
7f9e0 54 45 5f 4f 53 5f 57 49 4e 3d 3d 30 29 0a 20 20  TE_OS_WIN==0).  
7f9f0 23 64 65 66 69 6e 65 20 44 4f 5f 4f 53 5f 4d 41  #define DO_OS_MA
7fa00 4c 4c 4f 43 5f 54 45 53 54 28 78 29 20 69 66 20  LLOC_TEST(x) if 
7fa10 28 21 78 20 7c 7c 20 21 73 71 6c 69 74 65 33 49  (!x || !sqlite3I
7fa20 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 78 29 29 20  sMemJournal(x)) 
7fa30 7b 20 20 20 20 20 5c 0a 20 20 20 20 76 6f 69 64  {     \.    void
7fa40 20 2a 70 54 73 74 41 6c 6c 6f 63 20 3d 20 73 71   *pTstAlloc = sq
7fa50 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 31 30 29 3b  lite3Malloc(10);
7fa60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7fa70 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
7fa80 20 20 20 69 66 20 28 21 70 54 73 74 41 6c 6c 6f     if (!pTstAllo
7fa90 63 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  c) return SQLITE
7faa0 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 20 20 20  _IOERR_NOMEM;   
7fab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7fac0 20 20 20 20 5c 0a 20 20 20 20 73 71 6c 69 74 65      \.    sqlite
7fad0 33 5f 66 72 65 65 28 70 54 73 74 41 6c 6c 6f 63  3_free(pTstAlloc
7fae0 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
7faf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7fb00 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d             \.  }
7fb10 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65  .#else.  #define
7fb20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45   DO_OS_MALLOC_TE
7fb30 53 54 28 78 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ST(x).#endif../*
7fb40 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
7fb50 67 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63  g routines are c
7fb60 6f 6e 76 65 6e 69 65 6e 63 65 20 77 72 61 70 70  onvenience wrapp
7fb70 65 72 73 20 61 72 6f 75 6e 64 20 6d 65 74 68 6f  ers around metho
7fb80 64 73 0a 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c  ds.** of the sql
7fb90 69 74 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74  ite3_file object
7fba0 2e 20 20 54 68 69 73 20 69 73 20 6d 6f 73 74 6c  .  This is mostl
7fbb0 79 20 6a 75 73 74 20 73 79 6e 74 61 63 74 69 63  y just syntactic
7fbc0 20 73 75 67 61 72 2e 20 41 6c 6c 0a 2a 2a 20 6f   sugar. All.** o
7fbd0 66 20 74 68 69 73 20 77 6f 75 6c 64 20 62 65 20  f this would be 
7fbe0 63 6f 6d 70 6c 65 74 65 6c 79 20 61 75 74 6f 6d  completely autom
7fbf0 61 74 69 63 20 69 66 20 53 51 4c 69 74 65 20 77  atic if SQLite w
7fc00 65 72 65 20 63 6f 64 65 64 20 75 73 69 6e 67 0a  ere coded using.
7fc10 2a 2a 20 43 2b 2b 20 69 6e 73 74 65 61 64 20 6f  ** C++ instead o
7fc20 66 20 70 6c 61 69 6e 20 6f 6c 64 20 43 2e 0a 2a  f plain old C..*
7fc30 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
7fc40 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 6c   int sqlite3OsCl
7fc50 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ose(sqlite3_file
7fc60 20 2a 70 49 64 29 7b 0a 20 20 69 6e 74 20 72 63   *pId){.  int rc
7fc70 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
7fc80 69 66 28 20 70 49 64 2d 3e 70 4d 65 74 68 6f 64  if( pId->pMethod
7fc90 73 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 49  s ){.    rc = pI
7fca0 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 43 6c  d->pMethods->xCl
7fcb0 6f 73 65 28 70 49 64 29 3b 0a 20 20 20 20 70 49  ose(pId);.    pI
7fcc0 64 2d 3e 70 4d 65 74 68 6f 64 73 20 3d 20 30 3b  d->pMethods = 0;
7fcd0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
7fce0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
7fcf0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
7fd00 52 65 61 64 28 73 71 6c 69 74 65 33 5f 66 69 6c  Read(sqlite3_fil
7fd10 65 20 2a 69 64 2c 20 76 6f 69 64 20 2a 70 42 75  e *id, void *pBu
7fd20 66 2c 20 69 6e 74 20 61 6d 74 2c 20 69 36 34 20  f, int amt, i64 
7fd30 6f 66 66 73 65 74 29 7b 0a 20 20 44 4f 5f 4f 53  offset){.  DO_OS
7fd40 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 69 64 29  _MALLOC_TEST(id)
7fd50 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70  ;.  return id->p
7fd60 4d 65 74 68 6f 64 73 2d 3e 78 52 65 61 64 28 69  Methods->xRead(i
7fd70 64 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20 6f 66  d, pBuf, amt, of
7fd80 66 73 65 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  fset);.}.SQLITE_
7fd90 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
7fda0 74 65 33 4f 73 57 72 69 74 65 28 73 71 6c 69 74  te3OsWrite(sqlit
7fdb0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 63 6f 6e  e3_file *id, con
7fdc0 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69  st void *pBuf, i
7fdd0 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 73  nt amt, i64 offs
7fde0 65 74 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c  et){.  DO_OS_MAL
7fdf0 4c 4f 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 20  LOC_TEST(id);.  
7fe00 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68  return id->pMeth
7fe10 6f 64 73 2d 3e 78 57 72 69 74 65 28 69 64 2c 20  ods->xWrite(id, 
7fe20 70 42 75 66 2c 20 61 6d 74 2c 20 6f 66 66 73 65  pBuf, amt, offse
7fe30 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  t);.}.SQLITE_PRI
7fe40 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
7fe50 4f 73 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74  OsTruncate(sqlit
7fe60 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34  e3_file *id, i64
7fe70 20 73 69 7a 65 29 7b 0a 20 20 72 65 74 75 72 6e   size){.  return
7fe80 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78   id->pMethods->x
7fe90 54 72 75 6e 63 61 74 65 28 69 64 2c 20 73 69 7a  Truncate(id, siz
7fea0 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  e);.}.SQLITE_PRI
7feb0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
7fec0 4f 73 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66  OsSync(sqlite3_f
7fed0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61  ile *id, int fla
7fee0 67 73 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c  gs){.  DO_OS_MAL
7fef0 4c 4f 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 20  LOC_TEST(id);.  
7ff00 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68  return id->pMeth
7ff10 6f 64 73 2d 3e 78 53 79 6e 63 28 69 64 2c 20 66  ods->xSync(id, f
7ff20 6c 61 67 73 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  lags);.}.SQLITE_
7ff30 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
7ff40 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 73 71  te3OsFileSize(sq
7ff50 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
7ff60 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 44  i64 *pSize){.  D
7ff70 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54  O_OS_MALLOC_TEST
7ff80 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 69  (id);.  return i
7ff90 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 46 69  d->pMethods->xFi
7ffa0 6c 65 53 69 7a 65 28 69 64 2c 20 70 53 69 7a 65  leSize(id, pSize
7ffb0 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  );.}.SQLITE_PRIV
7ffc0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
7ffd0 73 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  sLock(sqlite3_fi
7ffe0 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b  le *id, int lock
7fff0 54 79 70 65 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d  Type){.  DO_OS_M
80000 41 4c 4c 4f 43 5f 54 45 53 54 28 69 64 29 3b 0a  ALLOC_TEST(id);.
80010 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65    return id->pMe
80020 74 68 6f 64 73 2d 3e 78 4c 6f 63 6b 28 69 64 2c  thods->xLock(id,
80030 20 6c 6f 63 6b 54 79 70 65 29 3b 0a 7d 0a 53 51   lockType);.}.SQ
80040 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
80050 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b   sqlite3OsUnlock
80060 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
80070 64 2c 20 69 6e 74 20 6c 6f 63 6b 54 79 70 65 29  d, int lockType)
80080 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70  {.  return id->p
80090 4d 65 74 68 6f 64 73 2d 3e 78 55 6e 6c 6f 63 6b  Methods->xUnlock
800a0 28 69 64 2c 20 6c 6f 63 6b 54 79 70 65 29 3b 0a  (id, lockType);.
800b0 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
800c0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 68   int sqlite3OsCh
800d0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28  eckReservedLock(
800e0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
800f0 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b  , int *pResOut){
80100 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f  .  DO_OS_MALLOC_
80110 54 45 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75  TEST(id);.  retu
80120 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d  rn id->pMethods-
80130 3e 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  >xCheckReservedL
80140 6f 63 6b 28 69 64 2c 20 70 52 65 73 4f 75 74 29  ock(id, pResOut)
80150 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
80160 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
80170 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69  FileControl(sqli
80180 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
80190 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67  t op, void *pArg
801a0 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e  ){.  return id->
801b0 70 4d 65 74 68 6f 64 73 2d 3e 78 46 69 6c 65 43  pMethods->xFileC
801c0 6f 6e 74 72 6f 6c 28 69 64 2c 20 6f 70 2c 20 70  ontrol(id, op, p
801d0 41 72 67 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  Arg);.}.SQLITE_P
801e0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
801f0 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 73  e3OsSectorSize(s
80200 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29  qlite3_file *id)
80210 7b 0a 20 20 69 6e 74 20 28 2a 78 53 65 63 74 6f  {.  int (*xSecto
80220 72 53 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 66  rSize)(sqlite3_f
80230 69 6c 65 2a 29 20 3d 20 69 64 2d 3e 70 4d 65 74  ile*) = id->pMet
80240 68 6f 64 73 2d 3e 78 53 65 63 74 6f 72 53 69 7a  hods->xSectorSiz
80250 65 3b 0a 20 20 72 65 74 75 72 6e 20 28 78 53 65  e;.  return (xSe
80260 63 74 6f 72 53 69 7a 65 20 3f 20 78 53 65 63 74  ctorSize ? xSect
80270 6f 72 53 69 7a 65 28 69 64 29 20 3a 20 53 51 4c  orSize(id) : SQL
80280 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54  ITE_DEFAULT_SECT
80290 4f 52 5f 53 49 5a 45 29 3b 0a 7d 0a 53 51 4c 49  OR_SIZE);.}.SQLI
802a0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
802b0 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68  qlite3OsDeviceCh
802c0 61 72 61 63 74 65 72 69 73 74 69 63 73 28 73 71  aracteristics(sq
802d0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b  lite3_file *id){
802e0 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d  .  return id->pM
802f0 65 74 68 6f 64 73 2d 3e 78 44 65 76 69 63 65 43  ethods->xDeviceC
80300 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 69  haracteristics(i
80310 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  d);.}../*.** The
80320 20 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 20 72   next group of r
80330 6f 75 74 69 6e 65 73 20 61 72 65 20 63 6f 6e 76  outines are conv
80340 65 6e 69 65 6e 63 65 20 77 72 61 70 70 65 72 73  enience wrappers
80350 20 61 72 6f 75 6e 64 20 74 68 65 0a 2a 2a 20 56   around the.** V
80360 46 53 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 53  FS methods..*/.S
80370 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
80380 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28  t sqlite3OsOpen(
80390 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
803a0 70 56 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63  pVfs, .  const c
803b0 68 61 72 20 2a 7a 50 61 74 68 2c 20 0a 20 20 73  har *zPath, .  s
803c0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69  qlite3_file *pFi
803d0 6c 65 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 73  le, .  int flags
803e0 2c 20 0a 20 20 69 6e 74 20 2a 70 46 6c 61 67 73  , .  int *pFlags
803f0 4f 75 74 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b  Out.){.  int rc;
80400 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f  .  DO_OS_MALLOC_
80410 54 45 53 54 28 30 29 3b 0a 20 20 2f 2a 20 30 78  TEST(0);.  /* 0x
80420 37 66 31 66 20 69 73 20 61 20 6d 61 73 6b 20 6f  7f1f is a mask o
80430 66 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 20 66  f SQLITE_OPEN_ f
80440 6c 61 67 73 20 74 68 61 74 20 61 72 65 20 76 61  lags that are va
80450 6c 69 64 20 74 6f 20 62 65 20 70 61 73 73 65 64  lid to be passed
80460 0a 20 20 2a 2a 20 64 6f 77 6e 20 69 6e 74 6f 20  .  ** down into 
80470 74 68 65 20 56 46 53 20 6c 61 79 65 72 2e 20 20  the VFS layer.  
80480 53 6f 6d 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e  Some SQLITE_OPEN
80490 5f 20 66 6c 61 67 73 20 28 66 6f 72 20 65 78 61  _ flags (for exa
804a0 6d 70 6c 65 2c 0a 20 20 2a 2a 20 53 51 4c 49 54  mple,.  ** SQLIT
804b0 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58  E_OPEN_FULLMUTEX
804c0 20 6f 72 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f   or SQLITE_OPEN_
804d0 53 48 41 52 45 44 43 41 43 48 45 29 20 61 72 65  SHAREDCACHE) are
804e0 20 62 6c 6f 63 6b 65 64 20 62 65 66 6f 72 65 0a   blocked before.
804f0 20 20 2a 2a 20 72 65 61 63 68 69 6e 67 20 74 68    ** reaching th
80500 65 20 56 46 53 2e 20 2a 2f 0a 20 20 72 63 20 3d  e VFS. */.  rc =
80510 20 70 56 66 73 2d 3e 78 4f 70 65 6e 28 70 56 66   pVfs->xOpen(pVf
80520 73 2c 20 7a 50 61 74 68 2c 20 70 46 69 6c 65 2c  s, zPath, pFile,
80530 20 66 6c 61 67 73 20 26 20 30 78 37 66 31 66 2c   flags & 0x7f1f,
80540 20 70 46 6c 61 67 73 4f 75 74 29 3b 0a 20 20 61   pFlagsOut);.  a
80550 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
80560 45 5f 4f 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 70  E_OK || pFile->p
80570 4d 65 74 68 6f 64 73 3d 3d 30 20 29 3b 0a 20 20  Methods==0 );.  
80580 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c  return rc;.}.SQL
80590 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
805a0 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28  sqlite3OsDelete(
805b0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
805c0 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  s, const char *z
805d0 50 61 74 68 2c 20 69 6e 74 20 64 69 72 53 79 6e  Path, int dirSyn
805e0 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66  c){.  return pVf
805f0 73 2d 3e 78 44 65 6c 65 74 65 28 70 56 66 73 2c  s->xDelete(pVfs,
80600 20 7a 50 61 74 68 2c 20 64 69 72 53 79 6e 63 29   zPath, dirSync)
80610 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
80620 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
80630 41 63 63 65 73 73 28 0a 20 20 73 71 6c 69 74 65  Access(.  sqlite
80640 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20  3_vfs *pVfs, .  
80650 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74  const char *zPat
80660 68 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c  h, .  int flags,
80670 20 0a 20 20 69 6e 74 20 2a 70 52 65 73 4f 75 74   .  int *pResOut
80680 0a 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c  .){.  DO_OS_MALL
80690 4f 43 5f 54 45 53 54 28 30 29 3b 0a 20 20 72 65  OC_TEST(0);.  re
806a0 74 75 72 6e 20 70 56 66 73 2d 3e 78 41 63 63 65  turn pVfs->xAcce
806b0 73 73 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20  ss(pVfs, zPath, 
806c0 66 6c 61 67 73 2c 20 70 52 65 73 4f 75 74 29 3b  flags, pResOut);
806d0 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
806e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46  E int sqlite3OsF
806f0 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73  ullPathname(.  s
80700 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
80710 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  , .  const char 
80720 2a 7a 50 61 74 68 2c 20 0a 20 20 69 6e 74 20 6e  *zPath, .  int n
80730 50 61 74 68 4f 75 74 2c 20 0a 20 20 63 68 61 72  PathOut, .  char
80740 20 2a 7a 50 61 74 68 4f 75 74 0a 29 7b 0a 20 20   *zPathOut.){.  
80750 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 46 75  return pVfs->xFu
80760 6c 6c 50 61 74 68 6e 61 6d 65 28 70 56 66 73 2c  llPathname(pVfs,
80770 20 7a 50 61 74 68 2c 20 6e 50 61 74 68 4f 75 74   zPath, nPathOut
80780 2c 20 7a 50 61 74 68 4f 75 74 29 3b 0a 7d 0a 23  , zPathOut);.}.#
80790 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
807a0 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f  IT_LOAD_EXTENSIO
807b0 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  N.SQLITE_PRIVATE
807c0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4f 73   void *sqlite3Os
807d0 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76  DlOpen(sqlite3_v
807e0 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20  fs *pVfs, const 
807f0 63 68 61 72 20 2a 7a 50 61 74 68 29 7b 0a 20 20  char *zPath){.  
80800 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 44 6c  return pVfs->xDl
80810 4f 70 65 6e 28 70 56 66 73 2c 20 7a 50 61 74 68  Open(pVfs, zPath
80820 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  );.}.SQLITE_PRIV
80830 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
80840 4f 73 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65  OsDlError(sqlite
80850 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74  3_vfs *pVfs, int
80860 20 6e 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 42   nByte, char *zB
80870 75 66 4f 75 74 29 7b 0a 20 20 70 56 66 73 2d 3e  ufOut){.  pVfs->
80880 78 44 6c 45 72 72 6f 72 28 70 56 66 73 2c 20 6e  xDlError(pVfs, n
80890 42 79 74 65 2c 20 7a 42 75 66 4f 75 74 29 3b 0a  Byte, zBufOut);.
808a0 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
808b0 20 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 4f   void (*sqlite3O
808c0 73 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76  sDlSym(sqlite3_v
808d0 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a  fs *pVfs, void *
808e0 70 48 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61  pHdle, const cha
808f0 72 20 2a 7a 53 79 6d 29 29 28 76 6f 69 64 29 7b  r *zSym))(void){
80900 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e  .  return pVfs->
80910 78 44 6c 53 79 6d 28 70 56 66 73 2c 20 70 48 64  xDlSym(pVfs, pHd
80920 6c 65 2c 20 7a 53 79 6d 29 3b 0a 7d 0a 53 51 4c  le, zSym);.}.SQL
80930 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
80940 20 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c 6f 73   sqlite3OsDlClos
80950 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70  e(sqlite3_vfs *p
80960 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64  Vfs, void *pHand
80970 6c 65 29 7b 0a 20 20 70 56 66 73 2d 3e 78 44 6c  le){.  pVfs->xDl
80980 43 6c 6f 73 65 28 70 56 66 73 2c 20 70 48 61 6e  Close(pVfs, pHan
80990 64 6c 65 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  dle);.}.#endif /
809a0 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f  * SQLITE_OMIT_LO
809b0 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20 2a 2f 0a  AD_EXTENSION */.
809c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
809d0 6e 74 20 73 71 6c 69 74 65 33 4f 73 52 61 6e 64  nt sqlite3OsRand
809e0 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76  omness(sqlite3_v
809f0 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42  fs *pVfs, int nB
80a00 79 74 65 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f  yte, char *zBufO
80a10 75 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56  ut){.  return pV
80a20 66 73 2d 3e 78 52 61 6e 64 6f 6d 6e 65 73 73 28  fs->xRandomness(
80a30 70 56 66 73 2c 20 6e 42 79 74 65 2c 20 7a 42 75  pVfs, nByte, zBu
80a40 66 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  fOut);.}.SQLITE_
80a50 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
80a60 74 65 33 4f 73 53 6c 65 65 70 28 73 71 6c 69 74  te3OsSleep(sqlit
80a70 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e  e3_vfs *pVfs, in
80a80 74 20 6e 4d 69 63 72 6f 29 7b 0a 20 20 72 65 74  t nMicro){.  ret
80a90 75 72 6e 20 70 56 66 73 2d 3e 78 53 6c 65 65 70  urn pVfs->xSleep
80aa0 28 70 56 66 73 2c 20 6e 4d 69 63 72 6f 29 3b 0a  (pVfs, nMicro);.
80ab0 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
80ac0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 75   int sqlite3OsCu
80ad0 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 65  rrentTime(sqlite
80ae0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 64 6f 75  3_vfs *pVfs, dou
80af0 62 6c 65 20 2a 70 54 69 6d 65 4f 75 74 29 7b 0a  ble *pTimeOut){.
80b00 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78    return pVfs->x
80b10 43 75 72 72 65 6e 74 54 69 6d 65 28 70 56 66 73  CurrentTime(pVfs
80b20 2c 20 70 54 69 6d 65 4f 75 74 29 3b 0a 7d 0a 0a  , pTimeOut);.}..
80b30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
80b40 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e  nt sqlite3OsOpen
80b50 4d 61 6c 6c 6f 63 28 0a 20 20 73 71 6c 69 74 65  Malloc(.  sqlite
80b60 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20  3_vfs *pVfs, .  
80b70 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
80b80 65 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 66 69  e, .  sqlite3_fi
80b90 6c 65 20 2a 2a 70 70 46 69 6c 65 2c 20 0a 20 20  le **ppFile, .  
80ba0 69 6e 74 20 66 6c 61 67 73 2c 0a 20 20 69 6e 74  int flags,.  int
80bb0 20 2a 70 4f 75 74 46 6c 61 67 73 0a 29 7b 0a 20   *pOutFlags.){. 
80bc0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
80bd0 5f 4e 4f 4d 45 4d 3b 0a 20 20 73 71 6c 69 74 65  _NOMEM;.  sqlite
80be0 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 3b 0a 20  3_file *pFile;. 
80bf0 20 70 46 69 6c 65 20 3d 20 28 73 71 6c 69 74 65   pFile = (sqlite
80c00 33 5f 66 69 6c 65 20 2a 29 73 71 6c 69 74 65 33  3_file *)sqlite3
80c10 4d 61 6c 6c 6f 63 28 70 56 66 73 2d 3e 73 7a 4f  Malloc(pVfs->szO
80c20 73 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 70 46  sFile);.  if( pF
80c30 69 6c 65 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ile ){.    rc = 
80c40 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56  sqlite3OsOpen(pV
80c50 66 73 2c 20 7a 46 69 6c 65 2c 20 70 46 69 6c 65  fs, zFile, pFile
80c60 2c 20 66 6c 61 67 73 2c 20 70 4f 75 74 46 6c 61  , flags, pOutFla
80c70 67 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  gs);.    if( rc!
80c80 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
80c90 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
80ca0 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 65 6c  (pFile);.    }el
80cb0 73 65 7b 0a 20 20 20 20 20 20 2a 70 70 46 69 6c  se{.      *ppFil
80cc0 65 20 3d 20 70 46 69 6c 65 3b 0a 20 20 20 20 7d  e = pFile;.    }
80cd0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
80ce0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
80cf0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
80d00 43 6c 6f 73 65 46 72 65 65 28 73 71 6c 69 74 65  CloseFree(sqlite
80d10 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a  3_file *pFile){.
80d20 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
80d30 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20  E_OK;.  assert( 
80d40 70 46 69 6c 65 20 29 3b 0a 20 20 72 63 20 3d 20  pFile );.  rc = 
80d50 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70  sqlite3OsClose(p
80d60 46 69 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  File);.  sqlite3
80d70 5f 66 72 65 65 28 70 46 69 6c 65 29 3b 0a 20 20  _free(pFile);.  
80d80 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
80d90 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
80da0 6e 20 69 73 20 61 20 77 72 61 70 70 65 72 20 61  n is a wrapper a
80db0 72 6f 75 6e 64 20 74 68 65 20 4f 53 20 73 70 65  round the OS spe
80dc0 63 69 66 69 63 20 69 6d 70 6c 65 6d 65 6e 74 61  cific implementa
80dd0 74 69 6f 6e 20 6f 66 0a 2a 2a 20 73 71 6c 69 74  tion of.** sqlit
80de0 65 33 5f 6f 73 5f 69 6e 69 74 28 29 2e 20 54 68  e3_os_init(). Th
80df0 65 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68 65  e purpose of the
80e00 20 77 72 61 70 70 65 72 20 69 73 20 74 6f 20 70   wrapper is to p
80e10 72 6f 76 69 64 65 20 74 68 65 0a 2a 2a 20 61 62  rovide the.** ab
80e20 69 6c 69 74 79 20 74 6f 20 73 69 6d 75 6c 61 74  ility to simulat
80e30 65 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75  e a malloc failu
80e40 72 65 2c 20 73 6f 20 74 68 61 74 20 74 68 65 20  re, so that the 
80e50 68 61 6e 64 6c 69 6e 67 20 6f 66 20 61 6e 0a 2a  handling of an.*
80e60 2a 20 65 72 72 6f 72 20 69 6e 20 73 71 6c 69 74  * error in sqlit
80e70 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 62 79 20  e3_os_init() by 
80e80 74 68 65 20 75 70 70 65 72 20 6c 61 79 65 72 73  the upper layers
80e90 20 63 61 6e 20 62 65 20 74 65 73 74 65 64 2e 0a   can be tested..
80ea0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
80eb0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 49  E int sqlite3OsI
80ec0 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20 76 6f 69  nit(void){.  voi
80ed0 64 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 5f 6d  d *p = sqlite3_m
80ee0 61 6c 6c 6f 63 28 31 30 29 3b 0a 20 20 69 66 28  alloc(10);.  if(
80ef0 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53   p==0 ) return S
80f00 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 73  QLITE_NOMEM;.  s
80f10 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a  qlite3_free(p);.
80f20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
80f30 5f 6f 73 5f 69 6e 69 74 28 29 3b 0a 7d 0a 0a 2f  _os_init();.}../
80f40 2a 0a 2a 2a 20 54 68 65 20 6c 69 73 74 20 6f 66  *.** The list of
80f50 20 61 6c 6c 20 72 65 67 69 73 74 65 72 65 64 20   all registered 
80f60 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  VFS implementati
80f70 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73  ons..*/.static s
80f80 71 6c 69 74 65 33 5f 76 66 73 20 2a 20 53 51 4c  qlite3_vfs * SQL
80f90 49 54 45 5f 57 53 44 20 76 66 73 4c 69 73 74 20  ITE_WSD vfsList 
80fa0 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 76 66 73  = 0;.#define vfs
80fb0 4c 69 73 74 20 47 4c 4f 42 41 4c 28 73 71 6c 69  List GLOBAL(sqli
80fc0 74 65 33 5f 76 66 73 20 2a 2c 20 76 66 73 4c 69  te3_vfs *, vfsLi
80fd0 73 74 29 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74  st)../*.** Locat
80fe0 65 20 61 20 56 46 53 20 62 79 20 6e 61 6d 65 2e  e a VFS by name.
80ff0 20 20 49 66 20 6e 6f 20 6e 61 6d 65 20 69 73 20    If no name is 
81000 67 69 76 65 6e 2c 20 73 69 6d 70 6c 79 20 72 65  given, simply re
81010 74 75 72 6e 20 74 68 65 0a 2a 2a 20 66 69 72 73  turn the.** firs
81020 74 20 56 46 53 20 6f 6e 20 74 68 65 20 6c 69 73  t VFS on the lis
81030 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  t..*/.SQLITE_API
81040 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 71   sqlite3_vfs *sq
81050 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 63  lite3_vfs_find(c
81060 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 66 73 29  onst char *zVfs)
81070 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  {.  sqlite3_vfs 
81080 2a 70 56 66 73 20 3d 20 30 3b 0a 23 69 66 20 53  *pVfs = 0;.#if S
81090 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
810a0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
810b0 20 2a 6d 75 74 65 78 3b 0a 23 65 6e 64 69 66 0a   *mutex;.#endif.
810c0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
810d0 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69  MIT_AUTOINIT.  i
810e0 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  nt rc = sqlite3_
810f0 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20  initialize();.  
81100 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
81110 30 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51  0;.#endif.#if SQ
81120 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a  LITE_THREADSAFE.
81130 20 20 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65    mutex = sqlite
81140 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49  3MutexAlloc(SQLI
81150 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
81160 4d 41 53 54 45 52 29 3b 0a 23 65 6e 64 69 66 0a  MASTER);.#endif.
81170 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
81180 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20  enter(mutex);.  
81190 66 6f 72 28 70 56 66 73 20 3d 20 76 66 73 4c 69  for(pVfs = vfsLi
811a0 73 74 3b 20 70 56 66 73 3b 20 70 56 66 73 3d 70  st; pVfs; pVfs=p
811b0 56 66 73 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  Vfs->pNext){.   
811c0 20 69 66 28 20 7a 56 66 73 3d 3d 30 20 29 20 62   if( zVfs==0 ) b
811d0 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 73 74  reak;.    if( st
811e0 72 63 6d 70 28 7a 56 66 73 2c 20 70 56 66 73 2d  rcmp(zVfs, pVfs-
811f0 3e 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72 65  >zName)==0 ) bre
81200 61 6b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  ak;.  }.  sqlite
81210 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75  3_mutex_leave(mu
81220 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  tex);.  return p
81230 56 66 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e  Vfs;.}../*.** Un
81240 6c 69 6e 6b 20 61 20 56 46 53 20 66 72 6f 6d 20  link a VFS from 
81250 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 0a  the linked list.
81260 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 76  */.static void v
81270 66 73 55 6e 6c 69 6e 6b 28 73 71 6c 69 74 65 33  fsUnlink(sqlite3
81280 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a 20 20 61  _vfs *pVfs){.  a
81290 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
812a0 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65  utex_held(sqlite
812b0 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49  3MutexAlloc(SQLI
812c0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
812d0 4d 41 53 54 45 52 29 29 20 29 3b 0a 20 20 69 66  MASTER)) );.  if
812e0 28 20 70 56 66 73 3d 3d 30 20 29 7b 0a 20 20 20  ( pVfs==0 ){.   
812f0 20 2f 2a 20 4e 6f 2d 6f 70 20 2a 2f 0a 20 20 7d   /* No-op */.  }
81300 65 6c 73 65 20 69 66 28 20 76 66 73 4c 69 73 74  else if( vfsList
81310 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 76 66  ==pVfs ){.    vf
81320 73 4c 69 73 74 20 3d 20 70 56 66 73 2d 3e 70 4e  sList = pVfs->pN
81330 65 78 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ext;.  }else if(
81340 20 76 66 73 4c 69 73 74 20 29 7b 0a 20 20 20 20   vfsList ){.    
81350 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 20 3d  sqlite3_vfs *p =
81360 20 76 66 73 4c 69 73 74 3b 0a 20 20 20 20 77 68   vfsList;.    wh
81370 69 6c 65 28 20 70 2d 3e 70 4e 65 78 74 20 26 26  ile( p->pNext &&
81380 20 70 2d 3e 70 4e 65 78 74 21 3d 70 56 66 73 20   p->pNext!=pVfs 
81390 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 70 2d 3e  ){.      p = p->
813a0 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20  pNext;.    }.   
813b0 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 3d 3d 70   if( p->pNext==p
813c0 56 66 73 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  Vfs ){.      p->
813d0 70 4e 65 78 74 20 3d 20 70 56 66 73 2d 3e 70 4e  pNext = pVfs->pN
813e0 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  ext;.    }.  }.}
813f0 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72  ../*.** Register
81400 20 61 20 56 46 53 20 77 69 74 68 20 74 68 65 20   a VFS with the 
81410 73 79 73 74 65 6d 2e 20 20 49 74 20 69 73 20 68  system.  It is h
81420 61 72 6d 6c 65 73 73 20 74 6f 20 72 65 67 69 73  armless to regis
81430 74 65 72 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20  ter the same.** 
81440 56 46 53 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d  VFS multiple tim
81450 65 73 2e 20 20 54 68 65 20 6e 65 77 20 56 46 53  es.  The new VFS
81460 20 62 65 63 6f 6d 65 73 20 74 68 65 20 64 65 66   becomes the def
81470 61 75 6c 74 20 69 66 20 6d 61 6b 65 44 66 6c 74  ault if makeDflt
81480 20 69 73 0a 2a 2a 20 74 72 75 65 2e 0a 2a 2f 0a   is.** true..*/.
81490 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
814a0 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73  qlite3_vfs_regis
814b0 74 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ter(sqlite3_vfs 
814c0 2a 70 56 66 73 2c 20 69 6e 74 20 6d 61 6b 65 44  *pVfs, int makeD
814d0 66 6c 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  flt){.  sqlite3_
814e0 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 30  mutex *mutex = 0
814f0 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
81500 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20  _OMIT_AUTOINIT. 
81510 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65   int rc = sqlite
81520 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a  3_initialize();.
81530 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
81540 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 20 20 6d  n rc;.#endif.  m
81550 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75  utex = sqlite3Mu
81560 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  texAlloc(SQLITE_
81570 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53  MUTEX_STATIC_MAS
81580 54 45 52 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  TER);.  sqlite3_
81590 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65  mutex_enter(mute
815a0 78 29 3b 0a 20 20 76 66 73 55 6e 6c 69 6e 6b 28  x);.  vfsUnlink(
815b0 70 56 66 73 29 3b 0a 20 20 69 66 28 20 6d 61 6b  pVfs);.  if( mak
815c0 65 44 66 6c 74 20 7c 7c 20 76 66 73 4c 69 73 74  eDflt || vfsList
815d0 3d 3d 30 20 29 7b 0a 20 20 20 20 70 56 66 73 2d  ==0 ){.    pVfs-
815e0 3e 70 4e 65 78 74 20 3d 20 76 66 73 4c 69 73 74  >pNext = vfsList
815f0 3b 0a 20 20 20 20 76 66 73 4c 69 73 74 20 3d 20  ;.    vfsList = 
81600 70 56 66 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  pVfs;.  }else{. 
81610 20 20 20 70 56 66 73 2d 3e 70 4e 65 78 74 20 3d     pVfs->pNext =
81620 20 76 66 73 4c 69 73 74 2d 3e 70 4e 65 78 74 3b   vfsList->pNext;
81630 0a 20 20 20 20 76 66 73 4c 69 73 74 2d 3e 70 4e  .    vfsList->pN
81640 65 78 74 20 3d 20 70 56 66 73 3b 0a 20 20 7d 0a  ext = pVfs;.  }.
81650 20 20 61 73 73 65 72 74 28 76 66 73 4c 69 73 74    assert(vfsList
81660 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
81670 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b  ex_leave(mutex);
81680 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
81690 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e  _OK;.}../*.** Un
816a0 72 65 67 69 73 74 65 72 20 61 20 56 46 53 20 73  register a VFS s
816b0 6f 20 74 68 61 74 20 69 74 20 69 73 20 6e 6f 20  o that it is no 
816c0 6c 6f 6e 67 65 72 20 61 63 63 65 73 73 69 62 6c  longer accessibl
816d0 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  e..*/.SQLITE_API
816e0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 73   int sqlite3_vfs
816f0 5f 75 6e 72 65 67 69 73 74 65 72 28 73 71 6c 69  _unregister(sqli
81700 74 65 33 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a  te3_vfs *pVfs){.
81710 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
81720 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65 33 5f  DSAFE.  sqlite3_
81730 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 73  mutex *mutex = s
81740 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
81750 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
81760 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65  ATIC_MASTER);.#e
81770 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ndif.  sqlite3_m
81780 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78  utex_enter(mutex
81790 29 3b 0a 20 20 76 66 73 55 6e 6c 69 6e 6b 28 70  );.  vfsUnlink(p
817a0 56 66 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  Vfs);.  sqlite3_
817b0 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65  mutex_leave(mute
817c0 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  x);.  return SQL
817d0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  ITE_OK;.}../****
817e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
817f0 66 20 6f 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  f os.c *********
81800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
81810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
81820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
81830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
81840 20 66 69 6c 65 20 66 61 75 6c 74 2e 63 20 2a 2a   file fault.c **
81850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
81860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
81870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
81880 20 32 30 30 38 20 4a 61 6e 20 32 32 0a 2a 2a 0a   2008 Jan 22.**.
81890 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
818a0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
818b0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
818c0 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
818d0 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
818e0 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
818f0 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
81900 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
81910 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
81920 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
81930 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
81940 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
81950 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
81960 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
81970 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
81980 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
81990 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
819a0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
819b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
819c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
819d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
819e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
819f0 2a 20 24 49 64 3a 20 66 61 75 6c 74 2e 63 2c 76  * $Id: fault.c,v
81a00 20 31 2e 31 31 20 32 30 30 38 2f 30 39 2f 30 32   1.11 2008/09/02
81a10 20 30 30 3a 35 32 3a 35 32 20 64 72 68 20 45 78   00:52:52 drh Ex
81a20 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  p $.*/../*.** Th
81a30 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
81a40 20 63 6f 64 65 20 74 6f 20 73 75 70 70 6f 72 74   code to support
81a50 20 74 68 65 20 63 6f 6e 63 65 70 74 20 6f 66 20   the concept of 
81a60 22 62 65 6e 69 67 6e 22 20 0a 2a 2a 20 6d 61 6c  "benign" .** mal
81a70 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 28 77 68  loc failures (wh
81a80 65 6e 20 74 68 65 20 78 4d 61 6c 6c 6f 63 28 29  en the xMalloc()
81a90 20 6f 72 20 78 52 65 61 6c 6c 6f 63 28 29 20 6d   or xRealloc() m
81aa0 65 74 68 6f 64 20 6f 66 20 74 68 65 0a 2a 2a 20  ethod of the.** 
81ab0 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68  sqlite3_mem_meth
81ac0 6f 64 73 20 73 74 72 75 63 74 75 72 65 20 66 61  ods structure fa
81ad0 69 6c 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  ils to allocate 
81ae0 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72  a block of memor
81af0 79 0a 2a 2a 20 61 6e 64 20 72 65 74 75 72 6e 73  y.** and returns
81b00 20 30 29 2e 20 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74   0). .**.** Most
81b10 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73   malloc failures
81b20 20 61 72 65 20 6e 6f 6e 2d 62 65 6e 69 67 6e 2e   are non-benign.
81b30 20 41 66 74 65 72 20 74 68 65 79 20 6f 63 63 75   After they occu
81b40 72 2c 20 53 51 4c 69 74 65 0a 2a 2a 20 61 62 61  r, SQLite.** aba
81b50 6e 64 6f 6e 73 20 74 68 65 20 63 75 72 72 65 6e  ndons the curren
81b60 74 20 6f 70 65 72 61 74 69 6f 6e 20 61 6e 64 20  t operation and 
81b70 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72  returns an error
81b80 20 63 6f 64 65 20 28 75 73 75 61 6c 6c 79 0a 2a   code (usually.*
81b90 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 20  * SQLITE_NOMEM) 
81ba0 74 6f 20 74 68 65 20 75 73 65 72 2e 20 48 6f 77  to the user. How
81bb0 65 76 65 72 2c 20 73 6f 6d 65 74 69 6d 65 73 20  ever, sometimes 
81bc0 61 20 66 61 75 6c 74 20 69 73 20 6e 6f 74 20 6e  a fault is not n
81bd0 65 63 65 73 73 61 72 69 6c 79 0a 2a 2a 20 66 61  ecessarily.** fa
81be0 74 61 6c 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65  tal. For example
81bf0 2c 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61  , if a malloc fa
81c00 69 6c 73 20 77 68 69 6c 65 20 72 65 73 69 7a 69  ils while resizi
81c10 6e 67 20 61 20 68 61 73 68 20 74 61 62 6c 65 2c  ng a hash table,
81c20 20 74 68 69 73 20 0a 2a 2a 20 69 73 20 63 6f 6d   this .** is com
81c30 70 6c 65 74 65 6c 79 20 72 65 63 6f 76 65 72 61  pletely recovera
81c40 62 6c 65 20 73 69 6d 70 6c 79 20 62 79 20 6e 6f  ble simply by no
81c50 74 20 63 61 72 72 79 69 6e 67 20 6f 75 74 20 74  t carrying out t
81c60 68 65 20 72 65 73 69 7a 65 2e 20 54 68 65 20 0a  he resize. The .
81c70 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 20 77 69  ** hash table wi
81c80 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 66  ll continue to f
81c90 75 6e 63 74 69 6f 6e 20 6e 6f 72 6d 61 6c 6c 79  unction normally
81ca0 2e 20 20 53 6f 20 61 20 6d 61 6c 6c 6f 63 20 66  .  So a malloc f
81cb0 61 69 6c 75 72 65 20 0a 2a 2a 20 64 75 72 69 6e  ailure .** durin
81cc0 67 20 61 20 68 61 73 68 20 74 61 62 6c 65 20 72  g a hash table r
81cd0 65 73 69 7a 65 20 69 73 20 61 20 62 65 6e 69 67  esize is a benig
81ce0 6e 20 66 61 75 6c 74 2e 0a 2a 2f 0a 0a 0a 23 69  n fault..*/...#i
81cf0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
81d00 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a 0a  T_BUILTIN_TEST..
81d10 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 6c 20 76 61 72  /*.** Global var
81d20 69 61 62 6c 65 73 2e 0a 2a 2f 0a 74 79 70 65 64  iables..*/.typed
81d30 65 66 20 73 74 72 75 63 74 20 42 65 6e 69 67 6e  ef struct Benign
81d40 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 20 42 65 6e 69  MallocHooks Beni
81d50 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 3b 0a 73  gnMallocHooks;.s
81d60 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44  tatic SQLITE_WSD
81d70 20 73 74 72 75 63 74 20 42 65 6e 69 67 6e 4d 61   struct BenignMa
81d80 6c 6c 6f 63 48 6f 6f 6b 73 20 7b 0a 20 20 76 6f  llocHooks {.  vo
81d90 69 64 20 28 2a 78 42 65 6e 69 67 6e 42 65 67 69  id (*xBenignBegi
81da0 6e 29 28 76 6f 69 64 29 3b 0a 20 20 76 6f 69 64  n)(void);.  void
81db0 20 28 2a 78 42 65 6e 69 67 6e 45 6e 64 29 28 76   (*xBenignEnd)(v
81dc0 6f 69 64 29 3b 0a 7d 20 73 71 6c 69 74 65 33 48  oid);.} sqlite3H
81dd0 6f 6f 6b 73 20 3d 20 7b 20 30 2c 20 30 20 7d 3b  ooks = { 0, 0 };
81de0 0a 0a 2f 2a 20 54 68 65 20 22 77 73 64 48 6f 6f  ../* The "wsdHoo
81df0 6b 73 22 20 6d 61 63 72 6f 20 77 69 6c 6c 20 72  ks" macro will r
81e00 65 73 6f 6c 76 65 20 74 6f 20 74 68 65 20 61 70  esolve to the ap
81e10 70 72 6f 70 72 69 61 74 65 20 42 65 6e 69 67 6e  propriate Benign
81e20 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 0a 2a 2a 20 73  MallocHooks.** s
81e30 74 72 75 63 74 75 72 65 2e 20 20 49 66 20 77 72  tructure.  If wr
81e40 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 61  itable static da
81e50 74 61 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65  ta is unsupporte
81e60 64 20 6f 6e 20 74 68 65 20 74 61 72 67 65 74 2c  d on the target,
81e70 0a 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 6c  .** we have to l
81e80 6f 63 61 74 65 20 74 68 65 20 73 74 61 74 65 20  ocate the state 
81e90 76 65 63 74 6f 72 20 61 74 20 72 75 6e 2d 74 69  vector at run-ti
81ea0 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72 65  me.  In the more
81eb0 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20   common.** case 
81ec0 77 68 65 72 65 20 77 72 69 74 61 62 6c 65 20 73  where writable s
81ed0 74 61 74 69 63 20 64 61 74 61 20 69 73 20 73 75  tatic data is su
81ee0 70 70 6f 72 74 65 64 2c 20 77 73 64 48 6f 6f 6b  pported, wsdHook
81ef0 73 20 63 61 6e 20 72 65 66 65 72 20 64 69 72 65  s can refer dire
81f00 63 74 6c 79 0a 2a 2a 20 74 6f 20 74 68 65 20 22  ctly.** to the "
81f10 73 71 6c 69 74 65 33 48 6f 6f 6b 73 22 20 73 74  sqlite3Hooks" st
81f20 61 74 65 20 76 65 63 74 6f 72 20 64 65 63 6c 61  ate vector decla
81f30 72 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69  red above..*/.#i
81f40 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
81f50 5f 57 53 44 0a 23 20 64 65 66 69 6e 65 20 77 73  _WSD.# define ws
81f60 64 48 6f 6f 6b 73 49 6e 69 74 20 5c 0a 20 20 42  dHooksInit \.  B
81f70 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73  enignMallocHooks
81f80 20 2a 78 20 3d 20 26 47 4c 4f 42 41 4c 28 42 65   *x = &GLOBAL(Be
81f90 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 2c  nignMallocHooks,
81fa0 73 71 6c 69 74 65 33 48 6f 6f 6b 73 29 0a 23 20  sqlite3Hooks).# 
81fb0 64 65 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73 20  define wsdHooks 
81fc0 78 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65 66  x[0].#else.# def
81fd0 69 6e 65 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74  ine wsdHooksInit
81fe0 0a 23 20 64 65 66 69 6e 65 20 77 73 64 48 6f 6f  .# define wsdHoo
81ff0 6b 73 20 73 71 6c 69 74 65 33 48 6f 6f 6b 73 0a  ks sqlite3Hooks.
82000 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 52  #endif.../*.** R
82010 65 67 69 73 74 65 72 20 68 6f 6f 6b 73 20 74 6f  egister hooks to
82020 20 63 61 6c 6c 20 77 68 65 6e 20 73 71 6c 69 74   call when sqlit
82030 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c  e3BeginBenignMal
82040 6c 6f 63 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c  loc() and.** sql
82050 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c  ite3EndBenignMal
82060 6c 6f 63 28 29 20 61 72 65 20 63 61 6c 6c 65 64  loc() are called
82070 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a  , respectively..
82080 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
82090 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65  E void sqlite3Be
820a0 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 28  nignMallocHooks(
820b0 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 67  .  void (*xBenig
820c0 6e 42 65 67 69 6e 29 28 76 6f 69 64 29 2c 0a 20  nBegin)(void),. 
820d0 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 67 6e 45   void (*xBenignE
820e0 6e 64 29 28 76 6f 69 64 29 0a 29 7b 0a 20 20 77  nd)(void).){.  w
820f0 73 64 48 6f 6f 6b 73 49 6e 69 74 3b 0a 20 20 77  sdHooksInit;.  w
82100 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 42  sdHooks.xBenignB
82110 65 67 69 6e 20 3d 20 78 42 65 6e 69 67 6e 42 65  egin = xBenignBe
82120 67 69 6e 3b 0a 20 20 77 73 64 48 6f 6f 6b 73 2e  gin;.  wsdHooks.
82130 78 42 65 6e 69 67 6e 45 6e 64 20 3d 20 78 42 65  xBenignEnd = xBe
82140 6e 69 67 6e 45 6e 64 3b 0a 7d 0a 0a 2f 2a 0a 2a  nignEnd;.}../*.*
82150 2a 20 54 68 69 73 20 28 73 71 6c 69 74 65 33 45  * This (sqlite3E
82160 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29  ndBenignMalloc()
82170 29 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 53  ) is called by S
82180 51 4c 69 74 65 20 63 6f 64 65 20 74 6f 20 69 6e  QLite code to in
82190 64 69 63 61 74 65 20 74 68 61 74 0a 2a 2a 20 73  dicate that.** s
821a0 75 62 73 65 71 75 65 6e 74 20 6d 61 6c 6c 6f 63  ubsequent malloc
821b0 20 66 61 69 6c 75 72 65 73 20 61 72 65 20 62 65   failures are be
821c0 6e 69 67 6e 2e 20 41 20 63 61 6c 6c 20 74 6f 20  nign. A call to 
821d0 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e  sqlite3EndBenign
821e0 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 69 6e 64 69  Malloc().** indi
821f0 63 61 74 65 73 20 74 68 61 74 20 73 75 62 73 65  cates that subse
82200 71 75 65 6e 74 20 6d 61 6c 6c 6f 63 20 66 61 69  quent malloc fai
82210 6c 75 72 65 73 20 61 72 65 20 6e 6f 6e 2d 62 65  lures are non-be
82220 6e 69 67 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  nign..*/.SQLITE_
82230 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
82240 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d  ite3BeginBenignM
82250 61 6c 6c 6f 63 28 76 6f 69 64 29 7b 0a 20 20 77  alloc(void){.  w
82260 73 64 48 6f 6f 6b 73 49 6e 69 74 3b 0a 20 20 69  sdHooksInit;.  i
82270 66 28 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e  f( wsdHooks.xBen
82280 69 67 6e 42 65 67 69 6e 20 29 7b 0a 20 20 20 20  ignBegin ){.    
82290 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e  wsdHooks.xBenign
822a0 42 65 67 69 6e 28 29 3b 0a 20 20 7d 0a 7d 0a 53  Begin();.  }.}.S
822b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
822c0 69 64 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e  id sqlite3EndBen
822d0 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 7b  ignMalloc(void){
822e0 0a 20 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 3b  .  wsdHooksInit;
822f0 0a 20 20 69 66 28 20 77 73 64 48 6f 6f 6b 73 2e  .  if( wsdHooks.
82300 78 42 65 6e 69 67 6e 45 6e 64 20 29 7b 0a 20 20  xBenignEnd ){.  
82310 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69    wsdHooks.xBeni
82320 67 6e 45 6e 64 28 29 3b 0a 20 20 7d 0a 7d 0a 0a  gnEnd();.  }.}..
82330 23 65 6e 64 69 66 20 20 20 2f 2a 20 23 69 66 6e  #endif   /* #ifn
82340 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
82350 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 2a 2f 0a  BUILTIN_TEST */.
82360 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
82370 20 45 6e 64 20 6f 66 20 66 61 75 6c 74 2e 63 20   End of fault.c 
82380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
823a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
823b0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
823c0 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 30   Begin file mem0
823d0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
823e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
823f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
82400 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 74 6f  ./*.** 2008 Octo
82410 62 65 72 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65  ber 28.**.** The
82420 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
82430 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
82440 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
82450 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
82460 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
82470 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
82480 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
82490 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
824a0 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
824b0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
824c0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
824d0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
824e0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
824f0 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
82500 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
82510 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
82520 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
82530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82570 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
82580 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 61   file contains a
82590 20 6e 6f 2d 6f 70 20 6d 65 6d 6f 72 79 20 61 6c   no-op memory al
825a0 6c 6f 63 61 74 69 6f 6e 20 64 72 69 76 65 72 73  location drivers
825b0 20 66 6f 72 20 75 73 65 20 77 68 65 6e 0a 2a 2a   for use when.**
825c0 20 53 51 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c   SQLITE_ZERO_MAL
825d0 4c 4f 43 20 69 73 20 64 65 66 69 6e 65 64 2e 20  LOC is defined. 
825e0 20 54 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20   The allocation 
825f0 64 72 69 76 65 72 73 20 69 6d 70 6c 65 6d 65 6e  drivers implemen
82600 74 65 64 0a 2a 2a 20 68 65 72 65 20 61 6c 77 61  ted.** here alwa
82610 79 73 20 66 61 69 6c 2e 20 20 53 51 4c 69 74 65  ys fail.  SQLite
82620 20 77 69 6c 6c 20 6e 6f 74 20 6f 70 65 72 61 74   will not operat
82630 65 20 77 69 74 68 20 74 68 65 73 65 20 64 72 69  e with these dri
82640 76 65 72 73 2e 20 20 54 68 65 73 65 0a 2a 2a 20  vers.  These.** 
82650 61 72 65 20 6d 65 72 65 6c 79 20 70 6c 61 63 65  are merely place
82660 68 6f 6c 64 65 72 73 2e 20 20 52 65 61 6c 20 64  holders.  Real d
82670 72 69 76 65 72 73 20 6d 75 73 74 20 62 65 20 73  rivers must be s
82680 75 62 73 74 69 74 75 74 65 64 20 75 73 69 6e 67  ubstituted using
82690 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66  .** sqlite3_conf
826a0 69 67 28 29 20 62 65 66 6f 72 65 20 53 51 4c 69  ig() before SQLi
826b0 74 65 20 77 69 6c 6c 20 6f 70 65 72 61 74 65 2e  te will operate.
826c0 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d 30  .**.** $Id: mem0
826d0 2e 63 2c 76 20 31 2e 31 20 32 30 30 38 2f 31 30  .c,v 1.1 2008/10
826e0 2f 32 38 20 31 38 3a 35 38 3a 32 30 20 64 72 68  /28 18:58:20 drh
826f0 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a   Exp $.*/../*.**
82700 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66   This version of
82710 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
82720 63 61 74 6f 72 20 69 73 20 74 68 65 20 64 65 66  cator is the def
82730 61 75 6c 74 2e 20 20 49 74 20 69 73 0a 2a 2a 20  ault.  It is.** 
82740 75 73 65 64 20 77 68 65 6e 20 6e 6f 20 6f 74 68  used when no oth
82750 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  er memory alloca
82760 74 6f 72 20 69 73 20 73 70 65 63 69 66 69 65 64  tor is specified
82770 20 75 73 69 6e 67 20 63 6f 6d 70 69 6c 65 2d 74   using compile-t
82780 69 6d 65 0a 2a 2a 20 6d 61 63 72 6f 73 2e 0a 2a  ime.** macros..*
82790 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
827a0 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 0a 0a 2f 2a 0a  ZERO_MALLOC../*.
827b0 2a 2a 20 4e 6f 2d 6f 70 20 76 65 72 73 69 6f 6e  ** No-op version
827c0 73 20 6f 66 20 61 6c 6c 20 6d 65 6d 6f 72 79 20  s of all memory 
827d0 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69  allocation routi
827e0 6e 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  nes.*/.static vo
827f0 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61  id *sqlite3MemMa
82800 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b  lloc(int nByte){
82810 20 72 65 74 75 72 6e 20 30 3b 20 7d 0a 73 74 61   return 0; }.sta
82820 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33  tic void sqlite3
82830 4d 65 6d 46 72 65 65 28 76 6f 69 64 20 2a 70 50  MemFree(void *pP
82840 72 69 6f 72 29 7b 20 72 65 74 75 72 6e 3b 20 7d  rior){ return; }
82850 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71  .static void *sq
82860 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 28  lite3MemRealloc(
82870 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e  void *pPrior, in
82880 74 20 6e 42 79 74 65 29 7b 20 72 65 74 75 72 6e  t nByte){ return
82890 20 30 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74   0; }.static int
828a0 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 28   sqlite3MemSize(
828b0 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 20 72  void *pPrior){ r
828c0 65 74 75 72 6e 20 30 3b 20 7d 0a 73 74 61 74 69  eturn 0; }.stati
828d0 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d  c int sqlite3Mem
828e0 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 20  Roundup(int n){ 
828f0 72 65 74 75 72 6e 20 6e 3b 20 7d 0a 73 74 61 74  return n; }.stat
82900 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65  ic int sqlite3Me
82910 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55  mInit(void *NotU
82920 73 65 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c  sed){ return SQL
82930 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63  ITE_OK; }.static
82940 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d   void sqlite3Mem
82950 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e  Shutdown(void *N
82960 6f 74 55 73 65 64 29 7b 20 72 65 74 75 72 6e 3b  otUsed){ return;
82970 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72   }../*.** This r
82980 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e  outine is the on
82990 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68  ly routine in th
829a0 69 73 20 66 69 6c 65 20 77 69 74 68 20 65 78 74  is file with ext
829b0 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a  ernal linkage..*
829c0 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20 74 68  *.** Populate th
829d0 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f  e low-level memo
829e0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75  ry allocation fu
829f0 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20  nction pointers 
82a00 69 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f  in.** sqlite3Glo
82a10 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68  balConfig.m with
82a20 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65   pointers to the
82a30 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69   routines in thi
82a40 73 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54  s file..*/.SQLIT
82a50 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
82a60 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61  qlite3MemSetDefa
82a70 75 6c 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61  ult(void){.  sta
82a80 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  tic const sqlite
82a90 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 64 65  3_mem_methods de
82aa0 66 61 75 6c 74 4d 65 74 68 6f 64 73 20 3d 20 7b  faultMethods = {
82ab0 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d  .     sqlite3Mem
82ac0 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c  Malloc,.     sql
82ad0 69 74 65 33 4d 65 6d 46 72 65 65 2c 0a 20 20 20  ite3MemFree,.   
82ae0 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c    sqlite3MemReal
82af0 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65  loc,.     sqlite
82b00 33 4d 65 6d 53 69 7a 65 2c 0a 20 20 20 20 20 73  3MemSize,.     s
82b10 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70  qlite3MemRoundup
82b20 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65  ,.     sqlite3Me
82b30 6d 49 6e 69 74 2c 0a 20 20 20 20 20 73 71 6c 69  mInit,.     sqli
82b40 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e 2c 0a  te3MemShutdown,.
82b50 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 73 71       0.  };.  sq
82b60 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c  lite3_config(SQL
82b70 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f  ITE_CONFIG_MALLO
82b80 43 2c 20 26 64 65 66 61 75 6c 74 4d 65 74 68 6f  C, &defaultMetho
82b90 64 73 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f  ds);.}..#endif /
82ba0 2a 20 53 51 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41  * SQLITE_ZERO_MA
82bb0 4c 4c 4f 43 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  LLOC */../******
82bc0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
82bd0 6d 65 6d 30 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  mem0.c *********
82be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82c00 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
82c10 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
82c20 69 6c 65 20 6d 65 6d 31 2e 63 20 2a 2a 2a 2a 2a  ile mem1.c *****
82c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82c50 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
82c60 30 30 37 20 41 75 67 75 73 74 20 31 34 0a 2a 2a  007 August 14.**
82c70 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
82c80 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
82c90 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
82ca0 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
82cb0 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
82cc0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
82cd0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
82ce0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
82cf0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
82d00 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
82d10 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
82d20 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
82d30 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
82d40 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
82d50 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
82d60 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
82d70 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
82d80 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
82d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
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 2a 2a  ****************
82dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
82dd0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
82de0 74 61 69 6e 73 20 6c 6f 77 2d 6c 65 76 65 6c 20  tains low-level 
82df0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
82e00 6e 20 64 72 69 76 65 72 73 20 66 6f 72 20 77 68  n drivers for wh
82e10 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c  en.** SQLite wil
82e20 6c 20 75 73 65 20 74 68 65 20 73 74 61 6e 64 61  l use the standa
82e30 72 64 20 43 2d 6c 69 62 72 61 72 79 20 6d 61 6c  rd C-library mal
82e40 6c 6f 63 2f 72 65 61 6c 6c 6f 63 2f 66 72 65 65  loc/realloc/free
82e50 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f   interface.** to
82e60 20 6f 62 74 61 69 6e 20 74 68 65 20 6d 65 6d 6f   obtain the memo
82e70 72 79 20 69 74 20 6e 65 65 64 73 2e 0a 2a 2a 0a  ry it needs..**.
82e80 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
82e90 74 61 69 6e 73 20 69 6d 70 6c 65 6d 65 6e 74 61  tains implementa
82ea0 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 6c 6f 77  tions of the low
82eb0 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c  -level memory al
82ec0 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74  location.** rout
82ed0 69 6e 65 73 20 73 70 65 63 69 66 69 65 64 20 69  ines specified i
82ee0 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6d 65  n the sqlite3_me
82ef0 6d 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74  m_methods object
82f00 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d  ..**.** $Id: mem
82f10 31 2e 63 2c 76 20 31 2e 33 30 20 32 30 30 39 2f  1.c,v 1.30 2009/
82f20 30 33 2f 32 33 20 30 34 3a 33 33 3a 33 33 20 64  03/23 04:33:33 d
82f30 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24  anielk1977 Exp $
82f40 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  .*/../*.** This 
82f50 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d  version of the m
82f60 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20  emory allocator 
82f70 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 2e 20  is the default. 
82f80 20 49 74 20 69 73 0a 2a 2a 20 75 73 65 64 20 77   It is.** used w
82f90 68 65 6e 20 6e 6f 20 6f 74 68 65 72 20 6d 65 6d  hen no other mem
82fa0 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73  ory allocator is
82fb0 20 73 70 65 63 69 66 69 65 64 20 75 73 69 6e 67   specified using
82fc0 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a   compile-time.**
82fd0 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 23 69 66 64   macros..*/.#ifd
82fe0 65 66 20 53 51 4c 49 54 45 5f 53 59 53 54 45 4d  ef SQLITE_SYSTEM
82ff0 5f 4d 41 4c 4c 4f 43 0a 0a 2f 2a 0a 2a 2a 20 4c  _MALLOC../*.** L
83000 69 6b 65 20 6d 61 6c 6c 6f 63 28 29 2c 20 62 75  ike malloc(), bu
83010 74 20 72 65 6d 65 6d 62 65 72 20 74 68 65 20 73  t remember the s
83020 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63  ize of the alloc
83030 61 74 69 6f 6e 0a 2a 2a 20 73 6f 20 74 68 61 74  ation.** so that
83040 20 77 65 20 63 61 6e 20 66 69 6e 64 20 69 74 20   we can find it 
83050 6c 61 74 65 72 20 75 73 69 6e 67 20 73 71 6c 69  later using sqli
83060 74 65 33 4d 65 6d 53 69 7a 65 28 29 2e 0a 2a 2a  te3MemSize()..**
83070 0a 2a 2a 20 46 6f 72 20 74 68 69 73 20 6c 6f 77  .** For this low
83080 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 2c 20  -level routine, 
83090 77 65 20 61 72 65 20 67 75 61 72 61 6e 74 65 65  we are guarantee
830a0 64 20 74 68 61 74 20 6e 42 79 74 65 3e 30 20 62  d that nByte>0 b
830b0 65 63 61 75 73 65 0a 2a 2a 20 63 61 73 65 73 20  ecause.** cases 
830c0 6f 66 20 6e 42 79 74 65 3c 3d 30 20 77 69 6c 6c  of nByte<=0 will
830d0 20 62 65 20 69 6e 74 65 72 63 65 70 74 65 64 20   be intercepted 
830e0 61 6e 64 20 64 65 61 6c 74 20 77 69 74 68 20 62  and dealt with b
830f0 79 20 68 69 67 68 65 72 20 6c 65 76 65 6c 0a 2a  y higher level.*
83100 2a 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73  * routines..*/.s
83110 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69  tatic void *sqli
83120 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74  te3MemMalloc(int
83130 20 6e 42 79 74 65 29 7b 0a 20 20 73 71 6c 69 74   nByte){.  sqlit
83140 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 20 61  e3_int64 *p;.  a
83150 73 73 65 72 74 28 20 6e 42 79 74 65 3e 30 20 29  ssert( nByte>0 )
83160 3b 0a 20 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e  ;.  nByte = ROUN
83170 44 38 28 6e 42 79 74 65 29 3b 0a 20 20 70 20 3d  D8(nByte);.  p =
83180 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 2b 38   malloc( nByte+8
83190 20 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20   );.  if( p ){. 
831a0 20 20 20 70 5b 30 5d 20 3d 20 6e 42 79 74 65 3b     p[0] = nByte;
831b0 0a 20 20 20 20 70 2b 2b 3b 0a 20 20 7d 0a 20 20  .    p++;.  }.  
831c0 72 65 74 75 72 6e 20 28 76 6f 69 64 20 2a 29 70  return (void *)p
831d0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20  ;.}../*.** Like 
831e0 66 72 65 65 28 29 20 62 75 74 20 77 6f 72 6b 73  free() but works
831f0 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 73   for allocations
83200 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
83210 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28  qlite3MemMalloc(
83220 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 4d  ).** or sqlite3M
83230 65 6d 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a  emRealloc()..**.
83240 2a 2a 20 46 6f 72 20 74 68 69 73 20 6c 6f 77 2d  ** For this low-
83250 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 2c 20 77  level routine, w
83260 65 20 61 6c 72 65 61 64 79 20 6b 6e 6f 77 20 74  e already know t
83270 68 61 74 20 70 50 72 69 6f 72 21 3d 30 20 73 69  hat pPrior!=0 si
83280 6e 63 65 0a 2a 2a 20 63 61 73 65 73 20 77 68 65  nce.** cases whe
83290 72 65 20 70 50 72 69 6f 72 3d 3d 30 20 77 69 6c  re pPrior==0 wil
832a0 6c 20 68 61 76 65 20 62 65 65 6e 20 69 6e 74 65  l have been inte
832b0 63 65 70 74 65 64 20 61 6e 64 20 64 65 61 6c 74  cepted and dealt
832c0 20 77 69 74 68 0a 2a 2a 20 62 79 20 68 69 67 68   with.** by high
832d0 65 72 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65  er-level routine
832e0 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  s..*/.static voi
832f0 64 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65  d sqlite3MemFree
83300 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a  (void *pPrior){.
83310 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
83320 2a 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e  *p = (sqlite3_in
83330 74 36 34 2a 29 70 50 72 69 6f 72 3b 0a 20 20 61  t64*)pPrior;.  a
83340 73 73 65 72 74 28 20 70 50 72 69 6f 72 21 3d 30  ssert( pPrior!=0
83350 20 29 3b 0a 20 20 70 2d 2d 3b 0a 20 20 66 72 65   );.  p--;.  fre
83360 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c  e(p);.}../*.** L
83370 69 6b 65 20 72 65 61 6c 6c 6f 63 28 29 2e 20 20  ike realloc().  
83380 52 65 73 69 7a 65 20 61 6e 20 61 6c 6c 6f 63 61  Resize an alloca
83390 74 69 6f 6e 20 70 72 65 76 69 6f 75 73 6c 79 20  tion previously 
833a0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a  obtained from.**
833b0 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f   sqlite3MemMallo
833c0 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74  c()..**.** For t
833d0 68 69 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 69 6e  his low-level in
833e0 74 65 72 66 61 63 65 2c 20 77 65 20 6b 6e 6f 77  terface, we know
833f0 20 74 68 61 74 20 70 50 72 69 6f 72 21 3d 30 2e   that pPrior!=0.
83400 20 20 43 61 73 65 73 20 77 68 65 72 65 0a 2a 2a    Cases where.**
83410 20 70 50 72 69 6f 72 3d 3d 30 20 77 68 69 6c 65   pPrior==0 while
83420 20 68 61 76 65 20 62 65 65 6e 20 69 6e 74 65 72   have been inter
83430 63 65 70 74 65 64 20 62 79 20 68 69 67 68 65 72  cepted by higher
83440 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 20 61  -level routine a
83450 6e 64 0a 2a 2a 20 72 65 64 69 72 65 63 74 65 64  nd.** redirected
83460 20 74 6f 20 78 4d 61 6c 6c 6f 63 2e 20 20 53 69   to xMalloc.  Si
83470 6d 69 6c 61 72 6c 79 2c 20 77 65 20 6b 6e 6f 77  milarly, we know
83480 20 74 68 61 74 20 6e 42 79 74 65 3e 30 20 62 65   that nByte>0 be
83490 63 61 75 73 65 73 0a 2a 2a 20 63 61 73 65 73 20  causes.** cases 
834a0 77 68 65 72 65 20 6e 42 79 74 65 3c 3d 30 20 77  where nByte<=0 w
834b0 69 6c 6c 20 68 61 76 65 20 62 65 65 6e 20 69 6e  ill have been in
834c0 74 65 72 63 65 70 74 65 64 20 62 79 20 68 69 67  tercepted by hig
834d0 68 65 72 2d 6c 65 76 65 6c 0a 2a 2a 20 72 6f 75  her-level.** rou
834e0 74 69 6e 65 73 20 61 6e 64 20 72 65 64 69 72 65  tines and redire
834f0 63 74 65 64 20 74 6f 20 78 46 72 65 65 2e 0a 2a  cted to xFree..*
83500 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73  /.static void *s
83510 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63  qlite3MemRealloc
83520 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 69  (void *pPrior, i
83530 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 73 71 6c  nt nByte){.  sql
83540 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 20 3d 20  ite3_int64 *p = 
83550 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29  (sqlite3_int64*)
83560 70 50 72 69 6f 72 3b 0a 20 20 61 73 73 65 72 74  pPrior;.  assert
83570 28 20 70 50 72 69 6f 72 21 3d 30 20 26 26 20 6e  ( pPrior!=0 && n
83580 42 79 74 65 3e 30 20 29 3b 0a 20 20 6e 42 79 74  Byte>0 );.  nByt
83590 65 20 3d 20 52 4f 55 4e 44 38 28 6e 42 79 74 65  e = ROUND8(nByte
835a0 29 3b 0a 20 20 70 20 3d 20 28 73 71 6c 69 74 65  );.  p = (sqlite
835b0 33 5f 69 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b  3_int64*)pPrior;
835c0 0a 20 20 70 2d 2d 3b 0a 20 20 70 20 3d 20 72 65  .  p--;.  p = re
835d0 61 6c 6c 6f 63 28 70 2c 20 6e 42 79 74 65 2b 38  alloc(p, nByte+8
835e0 20 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20   );.  if( p ){. 
835f0 20 20 20 70 5b 30 5d 20 3d 20 6e 42 79 74 65 3b     p[0] = nByte;
83600 0a 20 20 20 20 70 2b 2b 3b 0a 20 20 7d 0a 20 20  .    p++;.  }.  
83610 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b  return (void*)p;
83620 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 70 6f 72 74  .}../*.** Report
83630 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 73   the allocated s
83640 69 7a 65 20 6f 66 20 61 20 70 72 69 6f 72 20 72  ize of a prior r
83650 65 74 75 72 6e 20 66 72 6f 6d 20 78 4d 61 6c 6c  eturn from xMall
83660 6f 63 28 29 0a 2a 2a 20 6f 72 20 78 52 65 61 6c  oc().** or xReal
83670 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  loc()..*/.static
83680 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 53   int sqlite3MemS
83690 69 7a 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72  ize(void *pPrior
836a0 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  ){.  sqlite3_int
836b0 36 34 20 2a 70 3b 0a 20 20 69 66 28 20 70 50 72  64 *p;.  if( pPr
836c0 69 6f 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ior==0 ) return 
836d0 30 3b 0a 20 20 70 20 3d 20 28 73 71 6c 69 74 65  0;.  p = (sqlite
836e0 33 5f 69 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b  3_int64*)pPrior;
836f0 0a 20 20 70 2d 2d 3b 0a 20 20 72 65 74 75 72 6e  .  p--;.  return
83700 20 28 69 6e 74 29 70 5b 30 5d 3b 0a 7d 0a 0a 2f   (int)p[0];.}../
83710 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20  *.** Round up a 
83720 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20  request size to 
83730 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61  the next valid a
83740 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a  llocation size..
83750 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71  */.static int sq
83760 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 28  lite3MemRoundup(
83770 69 6e 74 20 6e 29 7b 0a 20 20 72 65 74 75 72 6e  int n){.  return
83780 20 52 4f 55 4e 44 38 28 6e 29 3b 0a 7d 0a 0a 2f   ROUND8(n);.}../
83790 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
837a0 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a  this module..*/.
837b0 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74  static int sqlit
837c0 65 33 4d 65 6d 49 6e 69 74 28 76 6f 69 64 20 2a  e3MemInit(void *
837d0 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53  NotUsed){.  UNUS
837e0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74  ED_PARAMETER(Not
837f0 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20  Used);.  return 
83800 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
83810 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65  .** Deinitialize
83820 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f   this module..*/
83830 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c  .static void sql
83840 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e 28  ite3MemShutdown(
83850 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a  void *NotUsed){.
83860 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
83870 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72  ER(NotUsed);.  r
83880 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  eturn;.}../*.** 
83890 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
838a0 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65  the only routine
838b0 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69   in this file wi
838c0 74 68 20 65 78 74 65 72 6e 61 6c 20 6c 69 6e 6b  th external link
838d0 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c  age..**.** Popul
838e0 61 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65  ate the low-leve
838f0 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  l memory allocat
83900 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69  ion function poi
83910 6e 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69  nters in.** sqli
83920 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
83930 6d 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20  m with pointers 
83940 74 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20  to the routines 
83950 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2f  in this file..*/
83960 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
83970 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53  void sqlite3MemS
83980 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64 29 7b  etDefault(void){
83990 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
839a0 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68  sqlite3_mem_meth
839b0 6f 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68 6f  ods defaultMetho
839c0 64 73 20 3d 20 7b 0a 20 20 20 20 20 73 71 6c 69  ds = {.     sqli
839d0 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20  te3MemMalloc,.  
839e0 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65     sqlite3MemFre
839f0 65 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d  e,.     sqlite3M
83a00 65 6d 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20  emRealloc,.     
83a10 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 2c 0a  sqlite3MemSize,.
83a20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52       sqlite3MemR
83a30 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 73 71 6c  oundup,.     sql
83a40 69 74 65 33 4d 65 6d 49 6e 69 74 2c 0a 20 20 20  ite3MemInit,.   
83a50 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74    sqlite3MemShut
83a60 64 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d  down,.     0.  }
83a70 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66  ;.  sqlite3_conf
83a80 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  ig(SQLITE_CONFIG
83a90 5f 4d 41 4c 4c 4f 43 2c 20 26 64 65 66 61 75 6c  _MALLOC, &defaul
83aa0 74 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 23 65  tMethods);.}..#e
83ab0 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 53  ndif /* SQLITE_S
83ac0 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 2a 2f 0a  YSTEM_MALLOC */.
83ad0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
83ae0 20 45 6e 64 20 6f 66 20 6d 65 6d 31 2e 63 20 2a   End of mem1.c *
83af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
83b20 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
83b30 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 32   Begin file mem2
83b40 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
83b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
83b70 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75  ./*.** 2007 Augu
83b80 73 74 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  st 15.**.** The 
83b90 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
83ba0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
83bb0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
83bc0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
83bd0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
83be0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
83bf0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
83c00 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
83c10 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
83c20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
83c30 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
83c40 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
83c50 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
83c60 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
83c70 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
83c80 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
83c90 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
83ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83ce0 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ****.**.** This 
83cf0 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6c 6f  file contains lo
83d00 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61  w-level memory a
83d10 6c 6c 6f 63 61 74 69 6f 6e 20 64 72 69 76 65 72  llocation driver
83d20 73 20 66 6f 72 20 77 68 65 6e 0a 2a 2a 20 53 51  s for when.** SQ
83d30 4c 69 74 65 20 77 69 6c 6c 20 75 73 65 20 74 68  Lite will use th
83d40 65 20 73 74 61 6e 64 61 72 64 20 43 2d 6c 69 62  e standard C-lib
83d50 72 61 72 79 20 6d 61 6c 6c 6f 63 2f 72 65 61 6c  rary malloc/real
83d60 6c 6f 63 2f 66 72 65 65 20 69 6e 74 65 72 66 61  loc/free interfa
83d70 63 65 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20  ce.** to obtain 
83d80 74 68 65 20 6d 65 6d 6f 72 79 20 69 74 20 6e 65  the memory it ne
83d90 65 64 73 20 77 68 69 6c 65 20 61 64 64 69 6e 67  eds while adding
83da0 20 6c 6f 74 73 20 6f 66 20 61 64 64 69 74 69 6f   lots of additio
83db0 6e 61 6c 20 64 65 62 75 67 67 69 6e 67 0a 2a 2a  nal debugging.**
83dc0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20   information to 
83dd0 65 61 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  each allocation 
83de0 69 6e 20 6f 72 64 65 72 20 74 6f 20 68 65 6c 70  in order to help
83df0 20 64 65 74 65 63 74 20 61 6e 64 20 66 69 78 20   detect and fix 
83e00 6d 65 6d 6f 72 79 0a 2a 2a 20 6c 65 61 6b 73 20  memory.** leaks 
83e10 61 6e 64 20 6d 65 6d 6f 72 79 20 75 73 61 67 65  and memory usage
83e20 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 54   errors..**.** T
83e30 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
83e40 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  s implementation
83e50 73 20 6f 66 20 74 68 65 20 6c 6f 77 2d 6c 65 76  s of the low-lev
83e60 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  el memory alloca
83e70 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73  tion.** routines
83e80 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68   specified in th
83e90 65 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65  e sqlite3_mem_me
83ea0 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a  thods object..**
83eb0 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d 32 2e 63 2c  .** $Id: mem2.c,
83ec0 76 20 31 2e 34 35 20 32 30 30 39 2f 30 33 2f 32  v 1.45 2009/03/2
83ed0 33 20 30 34 3a 33 33 3a 33 33 20 64 61 6e 69 65  3 04:33:33 danie
83ee0 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a  lk1977 Exp $.*/.
83ef0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73  ./*.** This vers
83f00 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72  ion of the memor
83f10 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 75  y allocator is u
83f20 73 65 64 20 6f 6e 6c 79 20 69 66 20 74 68 65 0a  sed only if the.
83f30 2a 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42  ** SQLITE_MEMDEB
83f40 55 47 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69  UG macro is defi
83f50 6e 65 64 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ned.*/.#ifdef SQ
83f60 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 0a 0a 2f  LITE_MEMDEBUG../
83f70 2a 0a 2a 2a 20 54 68 65 20 62 61 63 6b 74 72 61  *.** The backtra
83f80 63 65 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79  ce functionality
83f90 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62   is only availab
83fa0 6c 65 20 77 69 74 68 20 47 4c 49 42 43 0a 2a 2f  le with GLIBC.*/
83fb0 0a 23 69 66 64 65 66 20 5f 5f 47 4c 49 42 43 5f  .#ifdef __GLIBC_
83fc0 5f 0a 20 20 65 78 74 65 72 6e 20 69 6e 74 20 62  _.  extern int b
83fd0 61 63 6b 74 72 61 63 65 28 76 6f 69 64 2a 2a 2c  acktrace(void**,
83fe0 69 6e 74 29 3b 0a 20 20 65 78 74 65 72 6e 20 76  int);.  extern v
83ff0 6f 69 64 20 62 61 63 6b 74 72 61 63 65 5f 73 79  oid backtrace_sy
84000 6d 62 6f 6c 73 5f 66 64 28 76 6f 69 64 2a 63 6f  mbols_fd(void*co
84010 6e 73 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 23  nst*,int,int);.#
84020 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 62 61  else.# define ba
84030 63 6b 74 72 61 63 65 28 41 2c 42 29 20 31 0a 23  cktrace(A,B) 1.#
84040 20 64 65 66 69 6e 65 20 62 61 63 6b 74 72 61 63   define backtrac
84050 65 5f 73 79 6d 62 6f 6c 73 5f 66 64 28 41 2c 42  e_symbols_fd(A,B
84060 2c 43 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ,C).#endif../*.*
84070 2a 20 45 61 63 68 20 6d 65 6d 6f 72 79 20 61 6c  * Each memory al
84080 6c 6f 63 61 74 69 6f 6e 20 6c 6f 6f 6b 73 20 6c  location looks l
84090 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20  ike this:.**.** 
840a0 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
840b0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
840c0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
840d0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
840e0 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 7c 20  ---------.**  | 
840f0 54 69 74 6c 65 20 7c 20 20 62 61 63 6b 74 72 61  Title |  backtra
84100 63 65 20 70 6f 69 6e 74 65 72 73 20 7c 20 20 4d  ce pointers |  M
84110 65 6d 42 6c 6f 63 6b 48 64 72 20 7c 20 20 61 6c  emBlockHdr |  al
84120 6c 6f 63 61 74 69 6f 6e 20 7c 20 20 45 6e 64 47  location |  EndG
84130 75 61 72 64 20 7c 0a 2a 2a 20 20 2d 2d 2d 2d 2d  uard |.**  -----
84140 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
84150 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
84160 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
84170 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
84180 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 70  ---.**.** The ap
84190 70 6c 69 63 61 74 69 6f 6e 20 63 6f 64 65 20 73  plication code s
841a0 65 65 73 20 6f 6e 6c 79 20 61 20 70 6f 69 6e 74  ees only a point
841b0 65 72 20 74 6f 20 74 68 65 20 61 6c 6c 6f 63 61  er to the alloca
841c0 74 69 6f 6e 2e 20 20 57 65 20 68 61 76 65 0a 2a  tion.  We have.*
841d0 2a 20 74 6f 20 62 61 63 6b 20 75 70 20 66 72 6f  * to back up fro
841e0 6d 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  m the allocation
841f0 20 70 6f 69 6e 74 65 72 20 74 6f 20 66 69 6e 64   pointer to find
84200 20 74 68 65 20 4d 65 6d 42 6c 6f 63 6b 48 64 72   the MemBlockHdr
84210 2e 20 20 54 68 65 0a 2a 2a 20 4d 65 6d 42 6c 6f  .  The.** MemBlo
84220 63 6b 48 64 72 20 74 65 6c 6c 73 20 75 73 20 74  ckHdr tells us t
84230 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61  he size of the a
84240 6c 6c 6f 63 61 74 69 6f 6e 20 61 6e 64 20 74 68  llocation and th
84250 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62  e number of.** b
84260 61 63 6b 74 72 61 63 65 20 70 6f 69 6e 74 65 72  acktrace pointer
84270 73 2e 20 20 54 68 65 72 65 20 69 73 20 61 6c 73  s.  There is als
84280 6f 20 61 20 67 75 61 72 64 20 77 6f 72 64 20 61  o a guard word a
84290 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
842a0 0a 2a 2a 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2e  .** MemBlockHdr.
842b0 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 42 6c  .*/.struct MemBl
842c0 6f 63 6b 48 64 72 20 7b 0a 20 20 69 36 34 20 69  ockHdr {.  i64 i
842d0 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20  Size;           
842e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
842f0 2a 20 53 69 7a 65 20 6f 66 20 74 68 69 73 20 61  * Size of this a
84300 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 73  llocation */.  s
84310 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64  truct MemBlockHd
84320 72 20 2a 70 4e 65 78 74 2c 20 2a 70 50 72 65 76  r *pNext, *pPrev
84330 3b 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73  ;  /* Linked lis
84340 74 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64  t of all unfreed
84350 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 63 68 61   memory */.  cha
84360 72 20 6e 42 61 63 6b 74 72 61 63 65 3b 20 20 20  r nBacktrace;   
84370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
84380 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 61   /* Number of ba
84390 63 6b 74 72 61 63 65 73 20 6f 6e 20 74 68 69 73  cktraces on this
843a0 20 61 6c 6c 6f 63 20 2a 2f 0a 20 20 63 68 61 72   alloc */.  char
843b0 20 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73   nBacktraceSlots
843c0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
843d0 2f 2a 20 41 76 61 69 6c 61 62 6c 65 20 62 61 63  /* Available bac
843e0 6b 74 72 61 63 65 20 73 6c 6f 74 73 20 2a 2f 0a  ktrace slots */.
843f0 20 20 73 68 6f 72 74 20 6e 54 69 74 6c 65 3b 20    short nTitle; 
84400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
84410 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f        /* Bytes o
84420 66 20 74 69 74 6c 65 3b 20 69 6e 63 6c 75 64 65  f title; include
84430 73 20 27 5c 30 27 20 2a 2f 0a 20 20 69 6e 74 20  s '\0' */.  int 
84440 69 46 6f 72 65 47 75 61 72 64 3b 20 20 20 20 20  iForeGuard;     
84450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
84460 2f 2a 20 47 75 61 72 64 20 77 6f 72 64 20 66 6f  /* Guard word fo
84470 72 20 73 61 6e 69 74 79 20 2a 2f 0a 7d 3b 0a 0a  r sanity */.};..
84480 2f 2a 0a 2a 2a 20 47 75 61 72 64 20 77 6f 72 64  /*.** Guard word
84490 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4f 52  s.*/.#define FOR
844a0 45 47 55 41 52 44 20 30 78 38 30 46 35 45 31 35  EGUARD 0x80F5E15
844b0 33 0a 23 64 65 66 69 6e 65 20 52 45 41 52 47 55  3.#define REARGU
844c0 41 52 44 20 30 78 45 34 36 37 36 42 35 33 0a 0a  ARD 0xE4676B53..
844d0 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66 20  /*.** Number of 
844e0 6d 61 6c 6c 6f 63 20 73 69 7a 65 20 69 6e 63 72  malloc size incr
844f0 65 6d 65 6e 74 73 20 74 6f 20 74 72 61 63 6b 2e  ements to track.
84500 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 43 53 49  .*/.#define NCSI
84510 5a 45 20 20 31 30 30 30 0a 0a 2f 2a 0a 2a 2a 20  ZE  1000../*.** 
84520 41 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69  All of the stati
84530 63 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 64  c variables used
84540 20 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20   by this module 
84550 61 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a  are collected.**
84560 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73   into a single s
84570 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22  tructure named "
84580 6d 65 6d 22 2e 20 20 54 68 69 73 20 69 73 20 74  mem".  This is t
84590 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 74  o keep the.** st
845a0 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 6f  atic variables o
845b0 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20  rganized and to 
845c0 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63 65  reduce namespace
845d0 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68   pollution.** wh
845e0 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69  en this module i
845f0 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20  s combined with 
84600 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d 61  other in the ama
84610 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74  lgamation..*/.st
84620 61 74 69 63 20 73 74 72 75 63 74 20 7b 0a 20 20  atic struct {.  
84630 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78  .  /*.  ** Mutex
84640 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65   to control acce
84650 73 73 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79  ss to the memory
84660 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73   allocation subs
84670 79 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71  ystem..  */.  sq
84680 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74  lite3_mutex *mut
84690 65 78 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 48  ex;..  /*.  ** H
846a0 65 61 64 20 61 6e 64 20 74 61 69 6c 20 6f 66 20  ead and tail of 
846b0 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66  a linked list of
846c0 20 61 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67   all outstanding
846d0 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 20 20 2a   allocations.  *
846e0 2f 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c  /.  struct MemBl
846f0 6f 63 6b 48 64 72 20 2a 70 46 69 72 73 74 3b 0a  ockHdr *pFirst;.
84700 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63    struct MemBloc
84710 6b 48 64 72 20 2a 70 4c 61 73 74 3b 0a 20 20 0a  kHdr *pLast;.  .
84720 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 6e 75    /*.  ** The nu
84730 6d 62 65 72 20 6f 66 20 6c 65 76 65 6c 73 20 6f  mber of levels o
84740 66 20 62 61 63 6b 74 72 61 63 65 20 74 6f 20 73  f backtrace to s
84750 61 76 65 20 69 6e 20 6e 65 77 20 61 6c 6c 6f 63  ave in new alloc
84760 61 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69  ations..  */.  i
84770 6e 74 20 6e 42 61 63 6b 74 72 61 63 65 3b 0a 20  nt nBacktrace;. 
84780 20 76 6f 69 64 20 28 2a 78 42 61 63 6b 74 72 61   void (*xBacktra
84790 63 65 29 28 69 6e 74 2c 20 69 6e 74 2c 20 76 6f  ce)(int, int, vo
847a0 69 64 20 2a 2a 29 3b 0a 0a 20 20 2f 2a 0a 20 20  id **);..  /*.  
847b0 2a 2a 20 54 69 74 6c 65 20 74 65 78 74 20 74 6f  ** Title text to
847c0 20 69 6e 73 65 72 74 20 69 6e 20 66 72 6f 6e 74   insert in front
847d0 20 6f 66 20 65 61 63 68 20 62 6c 6f 63 6b 0a 20   of each block. 
847e0 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 69 74 6c 65   */.  int nTitle
847f0 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65  ;        /* Byte
84800 73 20 6f 66 20 7a 54 69 74 6c 65 20 74 6f 20 73  s of zTitle to s
84810 61 76 65 2e 20 20 49 6e 63 6c 75 64 65 73 20 27  ave.  Includes '
84820 5c 30 27 20 61 6e 64 20 70 61 64 64 69 6e 67 20  \0' and padding 
84830 2a 2f 0a 20 20 63 68 61 72 20 7a 54 69 74 6c 65  */.  char zTitle
84840 5b 31 30 30 5d 3b 20 20 2f 2a 20 54 68 65 20 74  [100];  /* The t
84850 69 74 6c 65 20 74 65 78 74 20 2a 2f 0a 0a 20 20  itle text */..  
84860 2f 2a 20 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33  /* .  ** sqlite3
84870 4d 61 6c 6c 6f 63 44 69 73 61 6c 6c 6f 77 28 29  MallocDisallow()
84880 20 69 6e 63 72 65 6d 65 6e 74 73 20 74 68 65 20   increments the 
84890 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 75 6e 74 65  following counte
848a0 72 2e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d  r..  ** sqlite3M
848b0 61 6c 6c 6f 63 41 6c 6c 6f 77 28 29 20 64 65 63  allocAllow() dec
848c0 72 65 6d 65 6e 74 73 20 69 74 2e 0a 20 20 2a 2f  rements it..  */
848d0 0a 20 20 69 6e 74 20 64 69 73 61 6c 6c 6f 77 3b  .  int disallow;
848e0 20 2f 2a 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 77   /* Do not allow
848f0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
84900 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  on */..  /*.  **
84910 20 47 61 74 68 65 72 20 73 74 61 74 69 73 74 69   Gather statisti
84920 63 73 20 6f 6e 20 74 68 65 20 73 69 7a 65 73 20  cs on the sizes 
84930 6f 66 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  of memory alloca
84940 74 69 6f 6e 73 2e 0a 20 20 2a 2a 20 6e 41 6c 6c  tions..  ** nAll
84950 6f 63 5b 69 5d 20 69 73 20 74 68 65 20 6e 75 6d  oc[i] is the num
84960 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f  ber of allocatio
84970 6e 20 61 74 74 65 6d 70 74 73 20 6f 66 20 69 2a  n attempts of i*
84980 38 0a 20 20 2a 2a 20 62 79 74 65 73 2e 20 20 69  8.  ** bytes.  i
84990 3d 3d 4e 43 53 49 5a 45 20 69 73 20 74 68 65 20  ==NCSIZE is the 
849a0 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61  number of alloca
849b0 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 66 6f  tion attempts fo
849c0 72 0a 20 20 2a 2a 20 73 69 7a 65 73 20 6d 6f 72  r.  ** sizes mor
849d0 65 20 74 68 61 6e 20 4e 43 53 49 5a 45 2a 38 20  e than NCSIZE*8 
849e0 62 79 74 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 6e  bytes..  */.  in
849f0 74 20 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 5d  t nAlloc[NCSIZE]
84a00 3b 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20  ;      /* Total 
84a10 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61  number of alloca
84a20 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  tions */.  int n
84a30 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 5d 3b  Current[NCSIZE];
84a40 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e      /* Current n
84a50 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74  umber of allocat
84a60 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6d 78  ions */.  int mx
84a70 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 5d 3b  Current[NCSIZE];
84a80 20 20 20 2f 2a 20 48 69 67 68 77 61 74 65 72 20     /* Highwater 
84a90 6d 61 72 6b 20 66 6f 72 20 6e 43 75 72 72 65 6e  mark for nCurren
84aa0 74 20 2a 2f 0a 0a 7d 20 6d 65 6d 3b 0a 0a 0a 2f  t */..} mem;.../
84ab0 2a 0a 2a 2a 20 41 64 6a 75 73 74 20 6d 65 6d 6f  *.** Adjust memo
84ac0 72 79 20 75 73 61 67 65 20 73 74 61 74 69 73 74  ry usage statist
84ad0 69 63 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ics.*/.static vo
84ae0 69 64 20 61 64 6a 75 73 74 53 74 61 74 73 28 69  id adjustStats(i
84af0 6e 74 20 69 53 69 7a 65 2c 20 69 6e 74 20 69 6e  nt iSize, int in
84b00 63 72 65 6d 65 6e 74 29 7b 0a 20 20 69 6e 74 20  crement){.  int 
84b10 69 20 3d 20 52 4f 55 4e 44 38 28 69 53 69 7a 65  i = ROUND8(iSize
84b20 29 2f 38 3b 0a 20 20 69 66 28 20 69 3e 4e 43 53  )/8;.  if( i>NCS
84b30 49 5a 45 2d 31 20 29 7b 0a 20 20 20 20 69 20 3d  IZE-1 ){.    i =
84b40 20 4e 43 53 49 5a 45 20 2d 20 31 3b 0a 20 20 7d   NCSIZE - 1;.  }
84b50 0a 20 20 69 66 28 20 69 6e 63 72 65 6d 65 6e 74  .  if( increment
84b60 3e 30 20 29 7b 0a 20 20 20 20 6d 65 6d 2e 6e 41  >0 ){.    mem.nA
84b70 6c 6c 6f 63 5b 69 5d 2b 2b 3b 0a 20 20 20 20 6d  lloc[i]++;.    m
84b80 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 2b 2b  em.nCurrent[i]++
84b90 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e 43  ;.    if( mem.nC
84ba0 75 72 72 65 6e 74 5b 69 5d 3e 6d 65 6d 2e 6d 78  urrent[i]>mem.mx
84bb0 43 75 72 72 65 6e 74 5b 69 5d 20 29 7b 0a 20 20  Current[i] ){.  
84bc0 20 20 20 20 6d 65 6d 2e 6d 78 43 75 72 72 65 6e      mem.mxCurren
84bd0 74 5b 69 5d 20 3d 20 6d 65 6d 2e 6e 43 75 72 72  t[i] = mem.nCurr
84be0 65 6e 74 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20  ent[i];.    }.  
84bf0 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 2e 6e  }else{.    mem.n
84c00 43 75 72 72 65 6e 74 5b 69 5d 2d 2d 3b 0a 20 20  Current[i]--;.  
84c10 20 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 6e 43    assert( mem.nC
84c20 75 72 72 65 6e 74 5b 69 5d 3e 3d 30 20 29 3b 0a  urrent[i]>=0 );.
84c30 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76    }.}../*.** Giv
84c40 65 6e 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e  en an allocation
84c50 2c 20 66 69 6e 64 20 74 68 65 20 4d 65 6d 42 6c  , find the MemBl
84c60 6f 63 6b 48 64 72 20 66 6f 72 20 74 68 61 74 20  ockHdr for that 
84c70 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a  allocation..**.*
84c80 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
84c90 68 65 63 6b 73 20 74 68 65 20 67 75 61 72 64 73  hecks the guards
84ca0 20 61 74 20 65 69 74 68 65 72 20 65 6e 64 20 6f   at either end o
84cb0 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  f the allocation
84cc0 20 61 6e 64 0a 2a 2a 20 69 66 20 74 68 65 79 20   and.** if they 
84cd0 61 72 65 20 69 6e 63 6f 72 72 65 63 74 20 69 74  are incorrect it
84ce0 20 61 73 73 65 72 74 73 2e 0a 2a 2f 0a 73 74 61   asserts..*/.sta
84cf0 74 69 63 20 73 74 72 75 63 74 20 4d 65 6d 42 6c  tic struct MemBl
84d00 6f 63 6b 48 64 72 20 2a 73 71 6c 69 74 65 33 4d  ockHdr *sqlite3M
84d10 65 6d 73 79 73 47 65 74 48 65 61 64 65 72 28 76  emsysGetHeader(v
84d20 6f 69 64 20 2a 70 41 6c 6c 6f 63 61 74 69 6f 6e  oid *pAllocation
84d30 29 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42  ){.  struct MemB
84d40 6c 6f 63 6b 48 64 72 20 2a 70 3b 0a 20 20 69 6e  lockHdr *p;.  in
84d50 74 20 2a 70 49 6e 74 3b 0a 20 20 75 38 20 2a 70  t *pInt;.  u8 *p
84d60 55 38 3b 0a 20 20 69 6e 74 20 6e 52 65 73 65 72  U8;.  int nReser
84d70 76 65 3b 0a 0a 20 20 70 20 3d 20 28 73 74 72 75  ve;..  p = (stru
84d80 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2a 29  ct MemBlockHdr*)
84d90 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20 70  pAllocation;.  p
84da0 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  --;.  assert( p-
84db0 3e 69 46 6f 72 65 47 75 61 72 64 3d 3d 28 69 6e  >iForeGuard==(in
84dc0 74 29 46 4f 52 45 47 55 41 52 44 20 29 3b 0a 20  t)FOREGUARD );. 
84dd0 20 6e 52 65 73 65 72 76 65 20 3d 20 52 4f 55 4e   nReserve = ROUN
84de0 44 38 28 70 2d 3e 69 53 69 7a 65 29 3b 0a 20 20  D8(p->iSize);.  
84df0 70 49 6e 74 20 3d 20 28 69 6e 74 2a 29 70 41 6c  pInt = (int*)pAl
84e00 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20 70 55 38 20  location;.  pU8 
84e10 3d 20 28 75 38 2a 29 70 41 6c 6c 6f 63 61 74 69  = (u8*)pAllocati
84e20 6f 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49  on;.  assert( pI
84e30 6e 74 5b 6e 52 65 73 65 72 76 65 2f 73 69 7a 65  nt[nReserve/size
84e40 6f 66 28 69 6e 74 29 5d 3d 3d 28 69 6e 74 29 52  of(int)]==(int)R
84e50 45 41 52 47 55 41 52 44 20 29 3b 0a 20 20 2f 2a  EARGUARD );.  /*
84e60 20 54 68 69 73 20 63 68 65 63 6b 73 20 61 6e 79   This checks any
84e70 20 6f 66 20 74 68 65 20 22 65 78 74 72 61 22 20   of the "extra" 
84e80 62 79 74 65 73 20 61 6c 6c 6f 63 61 74 65 64 20  bytes allocated 
84e90 64 75 65 0a 20 20 2a 2a 20 74 6f 20 72 6f 75 6e  due.  ** to roun
84ea0 64 69 6e 67 20 75 70 20 74 6f 20 61 6e 20 38 20  ding up to an 8 
84eb0 62 79 74 65 20 62 6f 75 6e 64 61 72 79 20 74 6f  byte boundary to
84ec0 20 65 6e 73 75 72 65 20 0a 20 20 2a 2a 20 74 68   ensure .  ** th
84ed0 65 79 20 68 61 76 65 6e 27 74 20 62 65 65 6e 20  ey haven't been 
84ee0 6f 76 65 72 77 72 69 74 74 65 6e 2e 0a 20 20 2a  overwritten..  *
84ef0 2f 0a 20 20 77 68 69 6c 65 28 20 6e 52 65 73 65  /.  while( nRese
84f00 72 76 65 2d 2d 20 3e 20 70 2d 3e 69 53 69 7a 65  rve-- > p->iSize
84f10 20 29 20 61 73 73 65 72 74 28 20 70 55 38 5b 6e   ) assert( pU8[n
84f20 52 65 73 65 72 76 65 5d 3d 3d 30 78 36 35 20 29  Reserve]==0x65 )
84f30 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  ;.  return p;.}.
84f40 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
84f50 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
84f60 73 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f  s currently allo
84f70 63 61 74 65 64 20 61 74 20 61 64 64 72 65 73 73  cated at address
84f80 20 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e   p..*/.static in
84f90 74 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65  t sqlite3MemSize
84fa0 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 73 74 72  (void *p){.  str
84fb0 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20  uct MemBlockHdr 
84fc0 2a 70 48 64 72 3b 0a 20 20 69 66 28 20 21 70 20  *pHdr;.  if( !p 
84fd0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
84fe0 0a 20 20 7d 0a 20 20 70 48 64 72 20 3d 20 73 71  .  }.  pHdr = sq
84ff0 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74 48 65  lite3MemsysGetHe
85000 61 64 65 72 28 70 29 3b 0a 20 20 72 65 74 75 72  ader(p);.  retur
85010 6e 20 70 48 64 72 2d 3e 69 53 69 7a 65 3b 0a 7d  n pHdr->iSize;.}
85020 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69  ../*.** Initiali
85030 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  ze the memory al
85040 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74  location subsyst
85050 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  em..*/.static in
85060 74 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74  t sqlite3MemInit
85070 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b  (void *NotUsed){
85080 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
85090 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20  TER(NotUsed);.  
850a0 61 73 73 65 72 74 28 20 28 73 69 7a 65 6f 66 28  assert( (sizeof(
850b0 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48  struct MemBlockH
850c0 64 72 29 26 37 29 20 3d 3d 20 30 20 29 3b 0a 20  dr)&7) == 0 );. 
850d0 20 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f   if( !sqlite3Glo
850e0 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74  balConfig.bMemst
850f0 61 74 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20  at ){.    /* If 
85100 6d 65 6d 6f 72 79 20 73 74 61 74 75 73 20 69 73  memory status is
85110 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 6e 20 74   enabled, then t
85120 68 65 20 6d 61 6c 6c 6f 63 2e 63 20 77 72 61 70  he malloc.c wrap
85130 70 65 72 20 77 69 6c 6c 20 61 6c 72 65 61 64 79  per will already
85140 0a 20 20 20 20 2a 2a 20 68 6f 6c 64 20 74 68 65  .    ** hold the
85150 20 53 54 41 54 49 43 5f 4d 45 4d 20 6d 75 74 65   STATIC_MEM mute
85160 78 20 77 68 65 6e 20 74 68 65 20 72 6f 75 74 69  x when the routi
85170 6e 65 73 20 68 65 72 65 20 61 72 65 20 69 6e 76  nes here are inv
85180 6f 6b 65 64 2e 20 2a 2f 0a 20 20 20 20 6d 65 6d  oked. */.    mem
85190 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33  .mutex = sqlite3
851a0 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54  MutexAlloc(SQLIT
851b0 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
851c0 45 4d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  EM);.  }.  retur
851d0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
851e0 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69  /*.** Deinitiali
851f0 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  ze the memory al
85200 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74  location subsyst
85210 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  em..*/.static vo
85220 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75  id sqlite3MemShu
85230 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55  tdown(void *NotU
85240 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50  sed){.  UNUSED_P
85250 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
85260 29 3b 0a 20 20 6d 65 6d 2e 6d 75 74 65 78 20 3d  );.  mem.mutex =
85270 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75   0;.}../*.** Rou
85280 6e 64 20 75 70 20 61 20 72 65 71 75 65 73 74 20  nd up a request 
85290 73 69 7a 65 20 74 6f 20 74 68 65 20 6e 65 78 74  size to the next
852a0 20 76 61 6c 69 64 20 61 6c 6c 6f 63 61 74 69 6f   valid allocatio
852b0 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69  n size..*/.stati
852c0 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d  c int sqlite3Mem
852d0 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a  Roundup(int n){.
852e0 20 20 72 65 74 75 72 6e 20 52 4f 55 4e 44 38 28    return ROUND8(
852f0 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c  n);.}../*.** All
85300 6f 63 61 74 65 20 6e 42 79 74 65 20 62 79 74 65  ocate nByte byte
85310 73 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a  s of memory..*/.
85320 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c  static void *sql
85330 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 69 6e  ite3MemMalloc(in
85340 74 20 6e 42 79 74 65 29 7b 0a 20 20 73 74 72 75  t nByte){.  stru
85350 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a  ct MemBlockHdr *
85360 70 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70  pHdr;.  void **p
85370 42 74 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20  Bt;.  char *z;. 
85380 20 69 6e 74 20 2a 70 49 6e 74 3b 0a 20 20 76 6f   int *pInt;.  vo
85390 69 64 20 2a 70 20 3d 20 30 3b 0a 20 20 69 6e 74  id *p = 0;.  int
853a0 20 74 6f 74 61 6c 53 69 7a 65 3b 0a 20 20 69 6e   totalSize;.  in
853b0 74 20 6e 52 65 73 65 72 76 65 3b 0a 20 20 73 71  t nReserve;.  sq
853c0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
853d0 72 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20  r(mem.mutex);.  
853e0 61 73 73 65 72 74 28 20 6d 65 6d 2e 64 69 73 61  assert( mem.disa
853f0 6c 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 6e 52 65  llow==0 );.  nRe
85400 73 65 72 76 65 20 3d 20 52 4f 55 4e 44 38 28 6e  serve = ROUND8(n
85410 42 79 74 65 29 3b 0a 20 20 74 6f 74 61 6c 53 69  Byte);.  totalSi
85420 7a 65 20 3d 20 6e 52 65 73 65 72 76 65 20 2b 20  ze = nReserve + 
85430 73 69 7a 65 6f 66 28 2a 70 48 64 72 29 20 2b 20  sizeof(*pHdr) + 
85440 73 69 7a 65 6f 66 28 69 6e 74 29 20 2b 0a 20 20  sizeof(int) +.  
85450 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d               mem
85460 2e 6e 42 61 63 6b 74 72 61 63 65 2a 73 69 7a 65  .nBacktrace*size
85470 6f 66 28 76 6f 69 64 2a 29 20 2b 20 6d 65 6d 2e  of(void*) + mem.
85480 6e 54 69 74 6c 65 3b 0a 20 20 70 20 3d 20 6d 61  nTitle;.  p = ma
85490 6c 6c 6f 63 28 74 6f 74 61 6c 53 69 7a 65 29 3b  lloc(totalSize);
854a0 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20  .  if( p ){.    
854b0 7a 20 3d 20 70 3b 0a 20 20 20 20 70 42 74 20 3d  z = p;.    pBt =
854c0 20 28 76 6f 69 64 2a 2a 29 26 7a 5b 6d 65 6d 2e   (void**)&z[mem.
854d0 6e 54 69 74 6c 65 5d 3b 0a 20 20 20 20 70 48 64  nTitle];.    pHd
854e0 72 20 3d 20 28 73 74 72 75 63 74 20 4d 65 6d 42  r = (struct MemB
854f0 6c 6f 63 6b 48 64 72 2a 29 26 70 42 74 5b 6d 65  lockHdr*)&pBt[me
85500 6d 2e 6e 42 61 63 6b 74 72 61 63 65 5d 3b 0a 20  m.nBacktrace];. 
85510 20 20 20 70 48 64 72 2d 3e 70 4e 65 78 74 20 3d     pHdr->pNext =
85520 20 30 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70 50   0;.    pHdr->pP
85530 72 65 76 20 3d 20 6d 65 6d 2e 70 4c 61 73 74 3b  rev = mem.pLast;
85540 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 70 4c 61  .    if( mem.pLa
85550 73 74 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 2e  st ){.      mem.
85560 70 4c 61 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70  pLast->pNext = p
85570 48 64 72 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  Hdr;.    }else{.
85580 20 20 20 20 20 20 6d 65 6d 2e 70 46 69 72 73 74        mem.pFirst
85590 20 3d 20 70 48 64 72 3b 0a 20 20 20 20 7d 0a 20   = pHdr;.    }. 
855a0 20 20 20 6d 65 6d 2e 70 4c 61 73 74 20 3d 20 70     mem.pLast = p
855b0 48 64 72 3b 0a 20 20 20 20 70 48 64 72 2d 3e 69  Hdr;.    pHdr->i
855c0 46 6f 72 65 47 75 61 72 64 20 3d 20 46 4f 52 45  ForeGuard = FORE
855d0 47 55 41 52 44 3b 0a 20 20 20 20 70 48 64 72 2d  GUARD;.    pHdr-
855e0 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73  >nBacktraceSlots
855f0 20 3d 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63   = mem.nBacktrac
85600 65 3b 0a 20 20 20 20 70 48 64 72 2d 3e 6e 54 69  e;.    pHdr->nTi
85610 74 6c 65 20 3d 20 6d 65 6d 2e 6e 54 69 74 6c 65  tle = mem.nTitle
85620 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e 42  ;.    if( mem.nB
85630 61 63 6b 74 72 61 63 65 20 29 7b 0a 20 20 20 20  acktrace ){.    
85640 20 20 76 6f 69 64 20 2a 61 41 64 64 72 5b 34 30    void *aAddr[40
85650 5d 3b 0a 20 20 20 20 20 20 70 48 64 72 2d 3e 6e  ];.      pHdr->n
85660 42 61 63 6b 74 72 61 63 65 20 3d 20 62 61 63 6b  Backtrace = back
85670 74 72 61 63 65 28 61 41 64 64 72 2c 20 6d 65 6d  trace(aAddr, mem
85680 2e 6e 42 61 63 6b 74 72 61 63 65 2b 31 29 2d 31  .nBacktrace+1)-1
85690 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70  ;.      memcpy(p
856a0 42 74 2c 20 26 61 41 64 64 72 5b 31 5d 2c 20 70  Bt, &aAddr[1], p
856b0 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 2a  Hdr->nBacktrace*
856c0 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 29 3b 0a  sizeof(void*));.
856d0 20 20 20 20 20 20 61 73 73 65 72 74 28 70 42 74        assert(pBt
856e0 5b 30 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20  [0]);.      if( 
856f0 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 20 29  mem.xBacktrace )
85700 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 2e 78 42  {.        mem.xB
85710 61 63 6b 74 72 61 63 65 28 6e 42 79 74 65 2c 20  acktrace(nByte, 
85720 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65  pHdr->nBacktrace
85730 2d 31 2c 20 26 61 41 64 64 72 5b 31 5d 29 3b 0a  -1, &aAddr[1]);.
85740 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
85750 65 7b 0a 20 20 20 20 20 20 70 48 64 72 2d 3e 6e  e{.      pHdr->n
85760 42 61 63 6b 74 72 61 63 65 20 3d 20 30 3b 0a 20  Backtrace = 0;. 
85770 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 65 6d     }.    if( mem
85780 2e 6e 54 69 74 6c 65 20 29 7b 0a 20 20 20 20 20  .nTitle ){.     
85790 20 6d 65 6d 63 70 79 28 7a 2c 20 6d 65 6d 2e 7a   memcpy(z, mem.z
857a0 54 69 74 6c 65 2c 20 6d 65 6d 2e 6e 54 69 74 6c  Title, mem.nTitl
857b0 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 48  e);.    }.    pH
857c0 64 72 2d 3e 69 53 69 7a 65 20 3d 20 6e 42 79 74  dr->iSize = nByt
857d0 65 3b 0a 20 20 20 20 61 64 6a 75 73 74 53 74 61  e;.    adjustSta
857e0 74 73 28 6e 42 79 74 65 2c 20 2b 31 29 3b 0a 20  ts(nByte, +1);. 
857f0 20 20 20 70 49 6e 74 20 3d 20 28 69 6e 74 2a 29     pInt = (int*)
85800 26 70 48 64 72 5b 31 5d 3b 0a 20 20 20 20 70 49  &pHdr[1];.    pI
85810 6e 74 5b 6e 52 65 73 65 72 76 65 2f 73 69 7a 65  nt[nReserve/size
85820 6f 66 28 69 6e 74 29 5d 20 3d 20 52 45 41 52 47  of(int)] = REARG
85830 55 41 52 44 3b 0a 20 20 20 20 6d 65 6d 73 65 74  UARD;.    memset
85840 28 70 49 6e 74 2c 20 30 78 36 35 2c 20 6e 52 65  (pInt, 0x65, nRe
85850 73 65 72 76 65 29 3b 0a 20 20 20 20 70 20 3d 20  serve);.    p = 
85860 28 76 6f 69 64 2a 29 70 49 6e 74 3b 0a 20 20 7d  (void*)pInt;.  }
85870 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
85880 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78  _leave(mem.mutex
85890 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 20 0a  );.  return p; .
858a0 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65  }../*.** Free me
858b0 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  mory..*/.static 
858c0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 46  void sqlite3MemF
858d0 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72  ree(void *pPrior
858e0 29 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42  ){.  struct MemB
858f0 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20  lockHdr *pHdr;. 
85900 20 76 6f 69 64 20 2a 2a 70 42 74 3b 0a 20 20 63   void **pBt;.  c
85910 68 61 72 20 2a 7a 3b 0a 20 20 61 73 73 65 72 74  har *z;.  assert
85920 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
85930 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 7c  onfig.bMemstat |
85940 7c 20 6d 65 6d 2e 6d 75 74 65 78 21 3d 30 20 29  | mem.mutex!=0 )
85950 3b 0a 20 20 70 48 64 72 20 3d 20 73 71 6c 69 74  ;.  pHdr = sqlit
85960 65 33 4d 65 6d 73 79 73 47 65 74 48 65 61 64 65  e3MemsysGetHeade
85970 72 28 70 50 72 69 6f 72 29 3b 0a 20 20 70 42 74  r(pPrior);.  pBt
85980 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72 3b   = (void**)pHdr;
85990 0a 20 20 70 42 74 20 2d 3d 20 70 48 64 72 2d 3e  .  pBt -= pHdr->
859a0 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b  nBacktraceSlots;
859b0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
859c0 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d 75 74 65 78  _enter(mem.mutex
859d0 29 3b 0a 20 20 69 66 28 20 70 48 64 72 2d 3e 70  );.  if( pHdr->p
859e0 50 72 65 76 20 29 7b 0a 20 20 20 20 61 73 73 65  Prev ){.    asse
859f0 72 74 28 20 70 48 64 72 2d 3e 70 50 72 65 76 2d  rt( pHdr->pPrev-
85a00 3e 70 4e 65 78 74 3d 3d 70 48 64 72 20 29 3b 0a  >pNext==pHdr );.
85a10 20 20 20 20 70 48 64 72 2d 3e 70 50 72 65 76 2d      pHdr->pPrev-
85a20 3e 70 4e 65 78 74 20 3d 20 70 48 64 72 2d 3e 70  >pNext = pHdr->p
85a30 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Next;.  }else{. 
85a40 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 70     assert( mem.p
85a50 46 69 72 73 74 3d 3d 70 48 64 72 20 29 3b 0a 20  First==pHdr );. 
85a60 20 20 20 6d 65 6d 2e 70 46 69 72 73 74 20 3d 20     mem.pFirst = 
85a70 70 48 64 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d  pHdr->pNext;.  }
85a80 0a 20 20 69 66 28 20 70 48 64 72 2d 3e 70 4e 65  .  if( pHdr->pNe
85a90 78 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  xt ){.    assert
85aa0 28 20 70 48 64 72 2d 3e 70 4e 65 78 74 2d 3e 70  ( pHdr->pNext->p
85ab0 50 72 65 76 3d 3d 70 48 64 72 20 29 3b 0a 20 20  Prev==pHdr );.  
85ac0 20 20 70 48 64 72 2d 3e 70 4e 65 78 74 2d 3e 70    pHdr->pNext->p
85ad0 50 72 65 76 20 3d 20 70 48 64 72 2d 3e 70 50 72  Prev = pHdr->pPr
85ae0 65 76 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ev;.  }else{.   
85af0 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 70 4c 61   assert( mem.pLa
85b00 73 74 3d 3d 70 48 64 72 20 29 3b 0a 20 20 20 20  st==pHdr );.    
85b10 6d 65 6d 2e 70 4c 61 73 74 20 3d 20 70 48 64 72  mem.pLast = pHdr
85b20 2d 3e 70 50 72 65 76 3b 0a 20 20 7d 0a 20 20 7a  ->pPrev;.  }.  z
85b30 20 3d 20 28 63 68 61 72 2a 29 70 42 74 3b 0a 20   = (char*)pBt;. 
85b40 20 7a 20 2d 3d 20 70 48 64 72 2d 3e 6e 54 69 74   z -= pHdr->nTit
85b50 6c 65 3b 0a 20 20 61 64 6a 75 73 74 53 74 61 74  le;.  adjustStat
85b60 73 28 70 48 64 72 2d 3e 69 53 69 7a 65 2c 20 2d  s(pHdr->iSize, -
85b70 31 29 3b 0a 20 20 6d 65 6d 73 65 74 28 7a 2c 20  1);.  memset(z, 
85b80 30 78 32 62 2c 20 73 69 7a 65 6f 66 28 76 6f 69  0x2b, sizeof(voi
85b90 64 2a 29 2a 70 48 64 72 2d 3e 6e 42 61 63 6b 74  d*)*pHdr->nBackt
85ba0 72 61 63 65 53 6c 6f 74 73 20 2b 20 73 69 7a 65  raceSlots + size
85bb0 6f 66 28 2a 70 48 64 72 29 20 2b 0a 20 20 20 20  of(*pHdr) +.    
85bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 48                pH
85bd0 64 72 2d 3e 69 53 69 7a 65 20 2b 20 73 69 7a 65  dr->iSize + size
85be0 6f 66 28 69 6e 74 29 20 2b 20 70 48 64 72 2d 3e  of(int) + pHdr->
85bf0 6e 54 69 74 6c 65 29 3b 0a 20 20 66 72 65 65 28  nTitle);.  free(
85c00 7a 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  z);.  sqlite3_mu
85c10 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d 75  tex_leave(mem.mu
85c20 74 65 78 29 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a  tex);  .}../*.**
85c30 20 43 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65   Change the size
85c40 20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20   of an existing 
85c50 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
85c60 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 69  n..**.** For thi
85c70 73 20 64 65 62 75 67 67 69 6e 67 20 69 6d 70 6c  s debugging impl
85c80 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 77 65 20 2a  ementation, we *
85c90 61 6c 77 61 79 73 2a 20 6d 61 6b 65 20 61 20 63  always* make a c
85ca0 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 61 6c  opy of the.** al
85cb0 6c 6f 63 61 74 69 6f 6e 20 69 6e 74 6f 20 61 20  location into a 
85cc0 6e 65 77 20 70 6c 61 63 65 20 69 6e 20 6d 65 6d  new place in mem
85cd0 6f 72 79 2e 20 20 49 6e 20 74 68 69 73 20 77 61  ory.  In this wa
85ce0 79 2c 20 69 66 20 74 68 65 20 0a 2a 2a 20 68 69  y, if the .** hi
85cf0 67 68 65 72 20 6c 65 76 65 6c 20 63 6f 64 65 20  gher level code 
85d00 69 73 20 75 73 69 6e 67 20 70 6f 69 6e 74 65 72  is using pointer
85d10 20 74 6f 20 74 68 65 20 6f 6c 64 20 61 6c 6c 6f   to the old allo
85d20 63 61 74 69 6f 6e 2c 20 69 74 20 69 73 20 0a 2a  cation, it is .*
85d30 2a 20 6d 75 63 68 20 6d 6f 72 65 20 6c 69 6b 65  * much more like
85d40 6c 79 20 74 6f 20 62 72 65 61 6b 20 61 6e 64 20  ly to break and 
85d50 77 65 20 61 72 65 20 6d 75 63 68 20 6d 6f 72 65  we are much more
85d60 20 6c 69 6b 69 6e 67 20 74 6f 20 66 69 6e 64 0a   liking to find.
85d70 2a 2a 20 74 68 65 20 65 72 72 6f 72 2e 0a 2a 2f  ** the error..*/
85d80 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71  .static void *sq
85d90 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 28  lite3MemRealloc(
85da0 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e  void *pPrior, in
85db0 74 20 6e 42 79 74 65 29 7b 0a 20 20 73 74 72 75  t nByte){.  stru
85dc0 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a  ct MemBlockHdr *
85dd0 70 4f 6c 64 48 64 72 3b 0a 20 20 76 6f 69 64 20  pOldHdr;.  void 
85de0 2a 70 4e 65 77 3b 0a 20 20 61 73 73 65 72 74 28  *pNew;.  assert(
85df0 20 6d 65 6d 2e 64 69 73 61 6c 6c 6f 77 3d 3d 30   mem.disallow==0
85e00 20 29 3b 0a 20 20 70 4f 6c 64 48 64 72 20 3d 20   );.  pOldHdr = 
85e10 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74  sqlite3MemsysGet
85e20 48 65 61 64 65 72 28 70 50 72 69 6f 72 29 3b 0a  Header(pPrior);.
85e30 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    pNew = sqlite3
85e40 4d 65 6d 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 29  MemMalloc(nByte)
85e50 3b 0a 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a  ;.  if( pNew ){.
85e60 20 20 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2c      memcpy(pNew,
85e70 20 70 50 72 69 6f 72 2c 20 6e 42 79 74 65 3c 70   pPrior, nByte<p
85e80 4f 6c 64 48 64 72 2d 3e 69 53 69 7a 65 20 3f 20  OldHdr->iSize ? 
85e90 6e 42 79 74 65 20 3a 20 70 4f 6c 64 48 64 72 2d  nByte : pOldHdr-
85ea0 3e 69 53 69 7a 65 29 3b 0a 20 20 20 20 69 66 28  >iSize);.    if(
85eb0 20 6e 42 79 74 65 3e 70 4f 6c 64 48 64 72 2d 3e   nByte>pOldHdr->
85ec0 69 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 6d  iSize ){.      m
85ed0 65 6d 73 65 74 28 26 28 28 63 68 61 72 2a 29 70  emset(&((char*)p
85ee0 4e 65 77 29 5b 70 4f 6c 64 48 64 72 2d 3e 69 53  New)[pOldHdr->iS
85ef0 69 7a 65 5d 2c 20 30 78 32 62 2c 20 6e 42 79 74  ize], 0x2b, nByt
85f00 65 20 2d 20 70 4f 6c 64 48 64 72 2d 3e 69 53 69  e - pOldHdr->iSi
85f10 7a 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  ze);.    }.    s
85f20 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 28 70 50  qlite3MemFree(pP
85f30 72 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74  rior);.  }.  ret
85f40 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a  urn pNew;.}../*.
85f50 2a 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20  ** Populate the 
85f60 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79  low-level memory
85f70 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63   allocation func
85f80 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e  tion pointers in
85f90 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  .** sqlite3Globa
85fa0 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70  lConfig.m with p
85fb0 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72  ointers to the r
85fc0 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20  outines in this 
85fd0 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  file..*/.SQLITE_
85fe0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
85ff0 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c  ite3MemSetDefaul
86000 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69  t(void){.  stati
86010 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  c const sqlite3_
86020 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 61  mem_methods defa
86030 75 6c 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20  ultMethods = {. 
86040 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61      sqlite3MemMa
86050 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74  lloc,.     sqlit
86060 65 33 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20 20  e3MemFree,.     
86070 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f  sqlite3MemReallo
86080 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d  c,.     sqlite3M
86090 65 6d 53 69 7a 65 2c 0a 20 20 20 20 20 73 71 6c  emSize,.     sql
860a0 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a  ite3MemRoundup,.
860b0 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 49       sqlite3MemI
860c0 6e 69 74 2c 0a 20 20 20 20 20 73 71 6c 69 74 65  nit,.     sqlite
860d0 33 4d 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 20  3MemShutdown,.  
860e0 20 20 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 69     0.  };.  sqli
860f0 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54  te3_config(SQLIT
86100 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c  E_CONFIG_MALLOC,
86110 20 26 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73   &defaultMethods
86120 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20  );.}../*.** Set 
86130 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 61  the number of ba
86140 63 6b 74 72 61 63 65 20 6c 65 76 65 6c 73 20 6b  cktrace levels k
86150 65 70 74 20 66 6f 72 20 65 61 63 68 20 61 6c 6c  ept for each all
86160 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 20 41 20 76 61  ocation..** A va
86170 6c 75 65 20 6f 66 20 7a 65 72 6f 20 74 75 72 6e  lue of zero turn
86180 73 20 6f 66 66 20 62 61 63 6b 74 72 61 63 69 6e  s off backtracin
86190 67 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 20 69  g.  The number i
861a0 73 20 61 6c 77 61 79 73 20 72 6f 75 6e 64 65 64  s always rounded
861b0 0a 2a 2a 20 75 70 20 74 6f 20 61 20 6d 75 6c 74  .** up to a mult
861c0 69 70 6c 65 20 6f 66 20 32 2e 0a 2a 2f 0a 53 51  iple of 2..*/.SQ
861d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
861e0 64 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75  d sqlite3Memdebu
861f0 67 42 61 63 6b 74 72 61 63 65 28 69 6e 74 20 64  gBacktrace(int d
86200 65 70 74 68 29 7b 0a 20 20 69 66 28 20 64 65 70  epth){.  if( dep
86210 74 68 3c 30 20 29 7b 20 64 65 70 74 68 20 3d 20  th<0 ){ depth = 
86220 30 3b 20 7d 0a 20 20 69 66 28 20 64 65 70 74 68  0; }.  if( depth
86230 3e 32 30 20 29 7b 20 64 65 70 74 68 20 3d 20 32  >20 ){ depth = 2
86240 30 3b 20 7d 0a 20 20 64 65 70 74 68 20 3d 20 28  0; }.  depth = (
86250 64 65 70 74 68 2b 31 29 26 30 78 66 65 3b 0a 20  depth+1)&0xfe;. 
86260 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 20   mem.nBacktrace 
86270 3d 20 64 65 70 74 68 3b 0a 7d 0a 0a 53 51 4c 49  = depth;.}..SQLI
86280 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
86290 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 42  sqlite3MemdebugB
862a0 61 63 6b 74 72 61 63 65 43 61 6c 6c 62 61 63 6b  acktraceCallback
862b0 28 76 6f 69 64 20 28 2a 78 42 61 63 6b 74 72 61  (void (*xBacktra
862c0 63 65 29 28 69 6e 74 2c 20 69 6e 74 2c 20 76 6f  ce)(int, int, vo
862d0 69 64 20 2a 2a 29 29 7b 0a 20 20 6d 65 6d 2e 78  id **)){.  mem.x
862e0 42 61 63 6b 74 72 61 63 65 20 3d 20 78 42 61 63  Backtrace = xBac
862f0 6b 74 72 61 63 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ktrace;.}../*.**
86300 20 53 65 74 20 74 68 65 20 74 69 74 6c 65 20 73   Set the title s
86310 74 72 69 6e 67 20 66 6f 72 20 73 75 62 73 65 71  tring for subseq
86320 75 65 6e 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  uent allocations
86330 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
86340 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
86350 4d 65 6d 64 65 62 75 67 53 65 74 74 69 74 6c 65  MemdebugSettitle
86360 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 69  (const char *zTi
86370 74 6c 65 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64  tle){.  unsigned
86380 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33   int n = sqlite3
86390 53 74 72 6c 65 6e 33 30 28 7a 54 69 74 6c 65 29  Strlen30(zTitle)
863a0 20 2b 20 31 3b 0a 20 20 73 71 6c 69 74 65 33 5f   + 1;.  sqlite3_
863b0 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 2e  mutex_enter(mem.
863c0 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 6e 3e  mutex);.  if( n>
863d0 3d 73 69 7a 65 6f 66 28 6d 65 6d 2e 7a 54 69 74  =sizeof(mem.zTit
863e0 6c 65 29 20 29 20 6e 20 3d 20 73 69 7a 65 6f 66  le) ) n = sizeof
863f0 28 6d 65 6d 2e 7a 54 69 74 6c 65 29 2d 31 3b 0a  (mem.zTitle)-1;.
86400 20 20 6d 65 6d 63 70 79 28 6d 65 6d 2e 7a 54 69    memcpy(mem.zTi
86410 74 6c 65 2c 20 7a 54 69 74 6c 65 2c 20 6e 29 3b  tle, zTitle, n);
86420 0a 20 20 6d 65 6d 2e 7a 54 69 74 6c 65 5b 6e 5d  .  mem.zTitle[n]
86430 20 3d 20 30 3b 0a 20 20 6d 65 6d 2e 6e 54 69 74   = 0;.  mem.nTit
86440 6c 65 20 3d 20 52 4f 55 4e 44 38 28 6e 29 3b 0a  le = ROUND8(n);.
86450 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
86460 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78 29  leave(mem.mutex)
86470 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  ;.}..SQLITE_PRIV
86480 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
86490 4d 65 6d 64 65 62 75 67 53 79 6e 63 28 29 7b 0a  MemdebugSync(){.
864a0 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63    struct MemBloc
864b0 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 66 6f  kHdr *pHdr;.  fo
864c0 72 28 70 48 64 72 3d 6d 65 6d 2e 70 46 69 72 73  r(pHdr=mem.pFirs
864d0 74 3b 20 70 48 64 72 3b 20 70 48 64 72 3d 70 48  t; pHdr; pHdr=pH
864e0 64 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  dr->pNext){.    
864f0 76 6f 69 64 20 2a 2a 70 42 74 20 3d 20 28 76 6f  void **pBt = (vo
86500 69 64 2a 2a 29 70 48 64 72 3b 0a 20 20 20 20 70  id**)pHdr;.    p
86510 42 74 20 2d 3d 20 70 48 64 72 2d 3e 6e 42 61 63  Bt -= pHdr->nBac
86520 6b 74 72 61 63 65 53 6c 6f 74 73 3b 0a 20 20 20  ktraceSlots;.   
86530 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 28   mem.xBacktrace(
86540 70 48 64 72 2d 3e 69 53 69 7a 65 2c 20 70 48 64  pHdr->iSize, pHd
86550 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 2d 31 2c  r->nBacktrace-1,
86560 20 26 70 42 74 5b 31 5d 29 3b 0a 20 20 7d 0a 7d   &pBt[1]);.  }.}
86570 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65  ../*.** Open the
86580 20 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64 20   file indicated 
86590 61 6e 64 20 77 72 69 74 65 20 61 20 6c 6f 67 20  and write a log 
865a0 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d  of all unfreed m
865b0 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63 61  emory .** alloca
865c0 74 69 6f 6e 73 20 69 6e 74 6f 20 74 68 61 74 20  tions into that 
865d0 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  log..*/.SQLITE_P
865e0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
865f0 74 65 33 4d 65 6d 64 65 62 75 67 44 75 6d 70 28  te3MemdebugDump(
86600 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
86610 65 6e 61 6d 65 29 7b 0a 20 20 46 49 4c 45 20 2a  ename){.  FILE *
86620 6f 75 74 3b 0a 20 20 73 74 72 75 63 74 20 4d 65  out;.  struct Me
86630 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b  mBlockHdr *pHdr;
86640 0a 20 20 76 6f 69 64 20 2a 2a 70 42 74 3b 0a 20  .  void **pBt;. 
86650 20 69 6e 74 20 69 3b 0a 20 20 6f 75 74 20 3d 20   int i;.  out = 
86660 66 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c  fopen(zFilename,
86670 20 22 77 22 29 3b 0a 20 20 69 66 28 20 6f 75 74   "w");.  if( out
86680 3d 3d 30 20 29 7b 0a 20 20 20 20 66 70 72 69 6e  ==0 ){.    fprin
86690 74 66 28 73 74 64 65 72 72 2c 20 22 2a 2a 20 55  tf(stderr, "** U
866a0 6e 61 62 6c 65 20 74 6f 20 6f 75 74 70 75 74 20  nable to output 
866b0 6d 65 6d 6f 72 79 20 64 65 62 75 67 20 6f 75 74  memory debug out
866c0 70 75 74 20 6c 6f 67 3a 20 25 73 20 2a 2a 5c 6e  put log: %s **\n
866d0 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
866e0 20 20 20 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65         zFilename
866f0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
86700 20 7d 0a 20 20 66 6f 72 28 70 48 64 72 3d 6d 65   }.  for(pHdr=me
86710 6d 2e 70 46 69 72 73 74 3b 20 70 48 64 72 3b 20  m.pFirst; pHdr; 
86720 70 48 64 72 3d 70 48 64 72 2d 3e 70 4e 65 78 74  pHdr=pHdr->pNext
86730 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 20 3d  ){.    char *z =
86740 20 28 63 68 61 72 2a 29 70 48 64 72 3b 0a 20 20   (char*)pHdr;.  
86750 20 20 7a 20 2d 3d 20 70 48 64 72 2d 3e 6e 42 61    z -= pHdr->nBa
86760 63 6b 74 72 61 63 65 53 6c 6f 74 73 2a 73 69 7a  cktraceSlots*siz
86770 65 6f 66 28 76 6f 69 64 2a 29 20 2b 20 70 48 64  eof(void*) + pHd
86780 72 2d 3e 6e 54 69 74 6c 65 3b 0a 20 20 20 20 66  r->nTitle;.    f
86790 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 2a 2a 2a  printf(out, "***
867a0 2a 20 25 6c 6c 64 20 62 79 74 65 73 20 61 74 20  * %lld bytes at 
867b0 25 70 20 66 72 6f 6d 20 25 73 20 2a 2a 2a 2a 5c  %p from %s ****\
867c0 6e 22 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  n", .           
867d0 20 70 48 64 72 2d 3e 69 53 69 7a 65 2c 20 26 70   pHdr->iSize, &p
867e0 48 64 72 5b 31 5d 2c 20 70 48 64 72 2d 3e 6e 54  Hdr[1], pHdr->nT
867f0 69 74 6c 65 20 3f 20 7a 20 3a 20 22 3f 3f 3f 22  itle ? z : "???"
86800 29 3b 0a 20 20 20 20 69 66 28 20 70 48 64 72 2d  );.    if( pHdr-
86810 3e 6e 42 61 63 6b 74 72 61 63 65 20 29 7b 0a 20  >nBacktrace ){. 
86820 20 20 20 20 20 66 66 6c 75 73 68 28 6f 75 74 29       fflush(out)
86830 3b 0a 20 20 20 20 20 20 70 42 74 20 3d 20 28 76  ;.      pBt = (v
86840 6f 69 64 2a 2a 29 70 48 64 72 3b 0a 20 20 20 20  oid**)pHdr;.    
86850 20 20 70 42 74 20 2d 3d 20 70 48 64 72 2d 3e 6e    pBt -= pHdr->n
86860 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b 0a  BacktraceSlots;.
86870 20 20 20 20 20 20 62 61 63 6b 74 72 61 63 65 5f        backtrace_
86880 73 79 6d 62 6f 6c 73 5f 66 64 28 70 42 74 2c 20  symbols_fd(pBt, 
86890 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65  pHdr->nBacktrace
868a0 2c 20 66 69 6c 65 6e 6f 28 6f 75 74 29 29 3b 0a  , fileno(out));.
868b0 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75        fprintf(ou
868c0 74 2c 20 22 5c 6e 22 29 3b 0a 20 20 20 20 7d 0a  t, "\n");.    }.
868d0 20 20 7d 0a 20 20 66 70 72 69 6e 74 66 28 6f 75    }.  fprintf(ou
868e0 74 2c 20 22 43 4f 55 4e 54 53 3a 5c 6e 22 29 3b  t, "COUNTS:\n");
868f0 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 43  .  for(i=0; i<NC
86900 53 49 5a 45 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20  SIZE-1; i++){.  
86910 20 20 69 66 28 20 6d 65 6d 2e 6e 41 6c 6c 6f 63    if( mem.nAlloc
86920 5b 69 5d 20 29 7b 0a 20 20 20 20 20 20 66 70 72  [i] ){.      fpr
86930 69 6e 74 66 28 6f 75 74 2c 20 22 20 20 20 25 35  intf(out, "   %5
86940 64 3a 20 25 31 30 64 20 25 31 30 64 20 25 31 30  d: %10d %10d %10
86950 64 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20 20 20  d\n", .         
86960 20 20 20 69 2a 38 2c 20 6d 65 6d 2e 6e 41 6c 6c     i*8, mem.nAll
86970 6f 63 5b 69 5d 2c 20 6d 65 6d 2e 6e 43 75 72 72  oc[i], mem.nCurr
86980 65 6e 74 5b 69 5d 2c 20 6d 65 6d 2e 6d 78 43 75  ent[i], mem.mxCu
86990 72 72 65 6e 74 5b 69 5d 29 3b 0a 20 20 20 20 7d  rrent[i]);.    }
869a0 0a 20 20 7d 0a 20 20 69 66 28 20 6d 65 6d 2e 6e  .  }.  if( mem.n
869b0 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 2d 31 5d 20  Alloc[NCSIZE-1] 
869c0 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f  ){.    fprintf(o
869d0 75 74 2c 20 22 20 20 20 25 35 64 3a 20 25 31 30  ut, "   %5d: %10
869e0 64 20 25 31 30 64 20 25 31 30 64 5c 6e 22 2c 0a  d %10d %10d\n",.
869f0 20 20 20 20 20 20 20 20 20 20 20 20 20 4e 43 53               NCS
86a00 49 5a 45 2a 38 2d 38 2c 20 6d 65 6d 2e 6e 41 6c  IZE*8-8, mem.nAl
86a10 6c 6f 63 5b 4e 43 53 49 5a 45 2d 31 5d 2c 0a 20  loc[NCSIZE-1],. 
86a20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 2e              mem.
86a30 6e 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 2d  nCurrent[NCSIZE-
86a40 31 5d 2c 20 6d 65 6d 2e 6d 78 43 75 72 72 65 6e  1], mem.mxCurren
86a50 74 5b 4e 43 53 49 5a 45 2d 31 5d 29 3b 0a 20 20  t[NCSIZE-1]);.  
86a60 7d 0a 20 20 66 63 6c 6f 73 65 28 6f 75 74 29 3b  }.  fclose(out);
86a70 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
86a80 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74   the number of t
86a90 69 6d 65 73 20 73 71 6c 69 74 65 33 4d 65 6d 4d  imes sqlite3MemM
86aa0 61 6c 6c 6f 63 28 29 20 68 61 73 20 62 65 65 6e  alloc() has been
86ab0 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49   called..*/.SQLI
86ac0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
86ad0 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 4d 61  qlite3MemdebugMa
86ae0 6c 6c 6f 63 43 6f 75 6e 74 28 29 7b 0a 20 20 69  llocCount(){.  i
86af0 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 54 6f 74  nt i;.  int nTot
86b00 61 6c 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d  al = 0;.  for(i=
86b10 30 3b 20 69 3c 4e 43 53 49 5a 45 3b 20 69 2b 2b  0; i<NCSIZE; i++
86b20 29 7b 0a 20 20 20 20 6e 54 6f 74 61 6c 20 2b 3d  ){.    nTotal +=
86b30 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 3b 0a   mem.nAlloc[i];.
86b40 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 54 6f    }.  return nTo
86b50 74 61 6c 3b 0a 7d 0a 0a 0a 23 65 6e 64 69 66 20  tal;.}...#endif 
86b60 2f 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42  /* SQLITE_MEMDEB
86b70 55 47 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  UG */../********
86b80 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65  ****** End of me
86b90 6d 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  m2.c ***********
86ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
86bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
86bc0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
86bd0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
86be0 65 20 6d 65 6d 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a  e mem3.c *******
86bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
86c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
86c10 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
86c20 37 20 4f 63 74 6f 62 65 72 20 31 34 0a 2a 2a 0a  7 October 14.**.
86c30 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
86c40 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
86c50 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
86c60 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
86c70 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
86c80 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
86c90 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
86ca0 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
86cb0 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
86cc0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
86cd0 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
86ce0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
86cf0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
86d00 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
86d10 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
86d20 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
86d30 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
86d40 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
86d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
86d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
86d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
86d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
86d90 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
86da0 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e  s the C function
86db0 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  s that implement
86dc0 20 61 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c   a memory.** all
86dd0 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65  ocation subsyste
86de0 6d 20 66 6f 72 20 75 73 65 20 62 79 20 53 51 4c  m for use by SQL
86df0 69 74 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ite. .**.** This
86e00 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20   version of the 
86e10 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
86e20 6e 20 73 75 62 73 79 73 74 65 6d 20 6f 6d 69 74  n subsystem omit
86e30 73 20 61 6c 6c 0a 2a 2a 20 75 73 65 20 6f 66 20  s all.** use of 
86e40 6d 61 6c 6c 6f 63 28 29 2e 20 54 68 65 20 53 51  malloc(). The SQ
86e50 4c 69 74 65 20 75 73 65 72 20 73 75 70 70 6c 69  Lite user suppli
86e60 65 73 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65  es a block of me
86e70 6d 6f 72 79 0a 2a 2a 20 62 65 66 6f 72 65 20 63  mory.** before c
86e80 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 69  alling sqlite3_i
86e90 6e 69 74 69 61 6c 69 7a 65 28 29 20 66 72 6f 6d  nitialize() from
86ea0 20 77 68 69 63 68 20 61 6c 6c 6f 63 61 74 69 6f   which allocatio
86eb0 6e 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20 61  ns.** are made a
86ec0 6e 64 20 72 65 74 75 72 6e 65 64 20 62 79 20 74  nd returned by t
86ed0 68 65 20 78 4d 61 6c 6c 6f 63 28 29 20 61 6e 64  he xMalloc() and
86ee0 20 78 52 65 61 6c 6c 6f 63 28 29 20 0a 2a 2a 20   xRealloc() .** 
86ef0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e  implementations.
86f00 20 4f 6e 63 65 20 73 71 6c 69 74 65 33 5f 69 6e   Once sqlite3_in
86f10 69 74 69 61 6c 69 7a 65 28 29 20 68 61 73 20 62  itialize() has b
86f20 65 65 6e 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 74  een called,.** t
86f30 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d  he amount of mem
86f40 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 74 6f  ory available to
86f50 20 53 51 4c 69 74 65 20 69 73 20 66 69 78 65 64   SQLite is fixed
86f60 20 61 6e 64 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62   and cannot.** b
86f70 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a  e changed..**.**
86f80 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66   This version of
86f90 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
86fa0 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d  cation subsystem
86fb0 20 69 73 20 69 6e 63 6c 75 64 65 64 0a 2a 2a 20   is included.** 
86fc0 69 6e 20 74 68 65 20 62 75 69 6c 64 20 6f 6e 6c  in the build onl
86fd0 79 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  y if SQLITE_ENAB
86fe0 4c 45 5f 4d 45 4d 53 59 53 33 20 69 73 20 64 65  LE_MEMSYS3 is de
86ff0 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  fined..**.** $Id
87000 3a 20 6d 65 6d 33 2e 63 2c 76 20 31 2e 32 35 20  : mem3.c,v 1.25 
87010 32 30 30 38 2f 31 31 2f 31 39 20 31 36 3a 35 32  2008/11/19 16:52
87020 3a 34 34 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :44 danielk1977 
87030 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  Exp $.*/../*.** 
87040 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20  This version of 
87050 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
87060 61 74 6f 72 20 69 73 20 6f 6e 6c 79 20 62 75 69  ator is only bui
87070 6c 74 20 69 6e 74 6f 20 74 68 65 20 6c 69 62 72  lt into the libr
87080 61 72 79 0a 2a 2a 20 53 51 4c 49 54 45 5f 45 4e  ary.** SQLITE_EN
87090 41 42 4c 45 5f 4d 45 4d 53 59 53 33 20 69 73 20  ABLE_MEMSYS3 is 
870a0 64 65 66 69 6e 65 64 2e 20 44 65 66 69 6e 69 6e  defined. Definin
870b0 67 20 74 68 69 73 20 73 79 6d 62 6f 6c 20 64 6f  g this symbol do
870c0 65 73 20 6e 6f 74 0a 2a 2a 20 6d 65 61 6e 20 74  es not.** mean t
870d0 68 61 74 20 74 68 65 20 6c 69 62 72 61 72 79 20  hat the library 
870e0 77 69 6c 6c 20 75 73 65 20 61 20 6d 65 6d 6f 72  will use a memor
870f0 79 2d 70 6f 6f 6c 20 62 79 20 64 65 66 61 75 6c  y-pool by defaul
87100 74 2c 20 6a 75 73 74 20 74 68 61 74 0a 2a 2a 20  t, just that.** 
87110 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e  it is available.
87120 20 54 68 65 20 6d 65 6d 70 6f 6f 6c 20 61 6c 6c   The mempool all
87130 6f 63 61 74 6f 72 20 69 73 20 61 63 74 69 76 61  ocator is activa
87140 74 65 64 20 62 79 20 63 61 6c 6c 69 6e 67 0a 2a  ted by calling.*
87150 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67  * sqlite3_config
87160 28 29 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ()..*/.#ifdef SQ
87170 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53  LITE_ENABLE_MEMS
87180 59 53 33 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d  YS3../*.** Maxim
87190 75 6d 20 73 69 7a 65 20 28 69 6e 20 4d 65 6d 33  um size (in Mem3
871a0 42 6c 6f 63 6b 73 29 20 6f 66 20 61 20 22 73 6d  Blocks) of a "sm
871b0 61 6c 6c 22 20 63 68 75 6e 6b 2e 0a 2a 2f 0a 23  all" chunk..*/.#
871c0 64 65 66 69 6e 65 20 4d 58 5f 53 4d 41 4c 4c 20  define MX_SMALL 
871d0 31 30 0a 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65  10.../*.** Numbe
871e0 72 20 6f 66 20 66 72 65 65 6c 69 73 74 20 68 61  r of freelist ha
871f0 73 68 20 73 6c 6f 74 73 0a 2a 2f 0a 23 64 65 66  sh slots.*/.#def
87200 69 6e 65 20 4e 5f 48 41 53 48 20 20 36 31 0a 0a  ine N_HASH  61..
87210 2f 2a 0a 2a 2a 20 41 20 6d 65 6d 6f 72 79 20 61  /*.** A memory a
87220 6c 6c 6f 63 61 74 69 6f 6e 20 28 61 6c 73 6f 20  llocation (also 
87230 63 61 6c 6c 65 64 20 61 20 22 63 68 75 6e 6b 22  called a "chunk"
87240 29 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 77  ) consists of tw
87250 6f 20 6f 72 20 0a 2a 2a 20 6d 6f 72 65 20 62 6c  o or .** more bl
87260 6f 63 6b 73 20 77 68 65 72 65 20 65 61 63 68 20  ocks where each 
87270 62 6c 6f 63 6b 20 69 73 20 38 20 62 79 74 65 73  block is 8 bytes
87280 2e 20 20 54 68 65 20 66 69 72 73 74 20 38 20 62  .  The first 8 b
87290 79 74 65 73 20 61 72 65 20 0a 2a 2a 20 61 20 68  ytes are .** a h
872a0 65 61 64 65 72 20 74 68 61 74 20 69 73 20 6e 6f  eader that is no
872b0 74 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68  t returned to th
872c0 65 20 75 73 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20  e user..**.** A 
872d0 63 68 75 6e 6b 20 69 73 20 74 77 6f 20 6f 72 20  chunk is two or 
872e0 6d 6f 72 65 20 62 6c 6f 63 6b 73 20 74 68 61 74  more blocks that
872f0 20 69 73 20 65 69 74 68 65 72 20 63 68 65 63 6b   is either check
87300 65 64 20 6f 75 74 20 6f 72 0a 2a 2a 20 66 72 65  ed out or.** fre
87310 65 2e 20 20 54 68 65 20 66 69 72 73 74 20 62 6c  e.  The first bl
87320 6f 63 6b 20 68 61 73 20 66 6f 72 6d 61 74 20 75  ock has format u
87330 2e 68 64 72 2e 20 20 75 2e 68 64 72 2e 73 69 7a  .hdr.  u.hdr.siz
87340 65 34 78 20 69 73 20 34 20 74 69 6d 65 73 20 74  e4x is 4 times t
87350 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 74 68  he.** size of th
87360 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 20  e allocation in 
87370 62 6c 6f 63 6b 73 20 69 66 20 74 68 65 20 61 6c  blocks if the al
87380 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65  location is free
87390 2e 0a 2a 2a 20 54 68 65 20 75 2e 68 64 72 2e 73  ..** The u.hdr.s
873a0 69 7a 65 34 78 26 31 20 62 69 74 20 69 73 20 74  ize4x&1 bit is t
873b0 72 75 65 20 69 66 20 74 68 65 20 63 68 75 6e 6b  rue if the chunk
873c0 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75 74 20   is checked out 
873d0 61 6e 64 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20  and.** false if 
873e0 74 68 65 20 63 68 75 6e 6b 20 69 73 20 6f 6e 20  the chunk is on 
873f0 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 54  the freelist.  T
87400 68 65 20 75 2e 68 64 72 2e 73 69 7a 65 34 78 26  he u.hdr.size4x&
87410 32 20 62 69 74 0a 2a 2a 20 69 73 20 74 72 75 65  2 bit.** is true
87420 20 69 66 20 74 68 65 20 70 72 65 76 69 6f 75 73   if the previous
87430 20 63 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65   chunk is checke
87440 64 20 6f 75 74 20 61 6e 64 20 66 61 6c 73 65 20  d out and false 
87450 69 66 20 74 68 65 0a 2a 2a 20 70 72 65 76 69 6f  if the.** previo
87460 75 73 20 63 68 75 6e 6b 20 69 73 20 66 72 65 65  us chunk is free
87470 2e 20 20 54 68 65 20 75 2e 68 64 72 2e 70 72 65  .  The u.hdr.pre
87480 76 53 69 7a 65 20 66 69 65 6c 64 20 69 73 20 74  vSize field is t
87490 68 65 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 74 68  he size of.** th
874a0 65 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b  e previous chunk
874b0 20 69 6e 20 62 6c 6f 63 6b 73 20 69 66 20 74 68   in blocks if th
874c0 65 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b  e previous chunk
874d0 20 69 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 66 72   is on the.** fr
874e0 65 65 6c 69 73 74 2e 20 49 66 20 74 68 65 20 70  eelist. If the p
874f0 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 73  revious chunk is
87500 20 63 68 65 63 6b 65 64 20 6f 75 74 2c 20 74 68   checked out, th
87510 65 6e 0a 2a 2a 20 75 2e 68 64 72 2e 70 72 65 76  en.** u.hdr.prev
87520 53 69 7a 65 20 63 61 6e 20 62 65 20 70 61 72 74  Size can be part
87530 20 6f 66 20 74 68 65 20 64 61 74 61 20 66 6f 72   of the data for
87540 20 74 68 61 74 20 63 68 75 6e 6b 20 61 6e 64 20   that chunk and 
87550 73 68 6f 75 6c 64 0a 2a 2a 20 6e 6f 74 20 62 65  should.** not be
87560 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e   read or written
87570 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 6f 66 74 65 6e  ..**.** We often
87580 20 69 64 65 6e 74 69 66 79 20 61 20 63 68 75 6e   identify a chun
87590 6b 20 62 79 20 69 74 73 20 69 6e 64 65 78 20 69  k by its index i
875a0 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 5d 2e 20  n mem3.aPool[]. 
875b0 20 57 68 65 6e 0a 2a 2a 20 74 68 69 73 20 69 73   When.** this is
875c0 20 64 6f 6e 65 2c 20 74 68 65 20 63 68 75 6e 6b   done, the chunk
875d0 20 69 6e 64 65 78 20 72 65 66 65 72 73 20 74 6f   index refers to
875e0 20 74 68 65 20 73 65 63 6f 6e 64 20 62 6c 6f 63   the second bloc
875f0 6b 20 6f 66 0a 2a 2a 20 74 68 65 20 63 68 75 6e  k of.** the chun
87600 6b 2e 20 20 49 6e 20 74 68 69 73 20 77 61 79 2c  k.  In this way,
87610 20 74 68 65 20 66 69 72 73 74 20 63 68 75 6e 6b   the first chunk
87620 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66   has an index of
87630 20 31 2e 0a 2a 2a 20 41 20 63 68 75 6e 6b 20 69   1..** A chunk i
87640 6e 64 65 78 20 6f 66 20 30 20 6d 65 61 6e 73 20  ndex of 0 means 
87650 22 6e 6f 20 73 75 63 68 20 63 68 75 6e 6b 22 20  "no such chunk" 
87660 61 6e 64 20 69 73 20 74 68 65 20 65 71 75 69 76  and is the equiv
87670 61 6c 65 6e 74 0a 2a 2a 20 6f 66 20 61 20 4e 55  alent.** of a NU
87680 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a  LL pointer..**.*
87690 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 62 6c 6f  * The second blo
876a0 63 6b 20 6f 66 20 66 72 65 65 20 63 68 75 6e 6b  ck of free chunk
876b0 73 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  s is of the form
876c0 20 75 2e 6c 69 73 74 2e 20 20 54 68 65 0a 2a 2a   u.list.  The.**
876d0 20 74 77 6f 20 66 69 65 6c 64 73 20 66 6f 72 6d   two fields form
876e0 20 61 20 64 6f 75 62 6c 65 2d 6c 69 6e 6b 65 64   a double-linked
876f0 20 6c 69 73 74 20 6f 66 20 63 68 75 6e 6b 73 20   list of chunks 
87700 6f 66 20 72 65 6c 61 74 65 64 20 73 69 7a 65 73  of related sizes
87710 2e 0a 2a 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f  ..** Pointers to
87720 20 74 68 65 20 68 65 61 64 20 6f 66 20 74 68 65   the head of the
87730 20 6c 69 73 74 20 61 72 65 20 73 74 6f 72 65 64   list are stored
87740 20 69 6e 20 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c   in mem3.aiSmall
87750 5b 5d 20 0a 2a 2a 20 66 6f 72 20 73 6d 61 6c 6c  [] .** for small
87760 65 72 20 63 68 75 6e 6b 73 20 61 6e 64 20 6d 65  er chunks and me
87770 6d 33 2e 61 69 48 61 73 68 5b 5d 20 66 6f 72 20  m3.aiHash[] for 
87780 6c 61 72 67 65 72 20 63 68 75 6e 6b 73 2e 0a 2a  larger chunks..*
87790 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20  *.** The second 
877a0 62 6c 6f 63 6b 20 6f 66 20 61 20 63 68 75 6e 6b  block of a chunk
877b0 20 69 73 20 75 73 65 72 20 64 61 74 61 20 69 66   is user data if
877c0 20 74 68 65 20 63 68 75 6e 6b 20 69 73 20 63 68   the chunk is ch
877d0 65 63 6b 65 64 20 0a 2a 2a 20 6f 75 74 2e 20 20  ecked .** out.  
877e0 49 66 20 61 20 63 68 75 6e 6b 20 69 73 20 63 68  If a chunk is ch
877f0 65 63 6b 65 64 20 6f 75 74 2c 20 74 68 65 20 75  ecked out, the u
87800 73 65 72 20 64 61 74 61 20 6d 61 79 20 65 78 74  ser data may ext
87810 65 6e 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20  end into.** the 
87820 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 76  u.hdr.prevSize v
87830 61 6c 75 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  alue of the foll
87840 6f 77 69 6e 67 20 63 68 75 6e 6b 2e 0a 2a 2f 0a  owing chunk..*/.
87850 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4d  typedef struct M
87860 65 6d 33 42 6c 6f 63 6b 20 4d 65 6d 33 42 6c 6f  em3Block Mem3Blo
87870 63 6b 3b 0a 73 74 72 75 63 74 20 4d 65 6d 33 42  ck;.struct Mem3B
87880 6c 6f 63 6b 20 7b 0a 20 20 75 6e 69 6f 6e 20 7b  lock {.  union {
87890 0a 20 20 20 20 73 74 72 75 63 74 20 7b 0a 20 20  .    struct {.  
878a0 20 20 20 20 75 33 32 20 70 72 65 76 53 69 7a 65      u32 prevSize
878b0 3b 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70  ;   /* Size of p
878c0 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 6e  revious chunk in
878d0 20 4d 65 6d 33 42 6c 6f 63 6b 20 65 6c 65 6d 65   Mem3Block eleme
878e0 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 75 33 32  nts */.      u32
878f0 20 73 69 7a 65 34 78 3b 20 20 20 20 20 2f 2a 20   size4x;     /* 
87900 34 78 20 74 68 65 20 73 69 7a 65 20 6f 66 20 63  4x the size of c
87910 75 72 72 65 6e 74 20 63 68 75 6e 6b 20 69 6e 20  urrent chunk in 
87920 4d 65 6d 33 42 6c 6f 63 6b 20 65 6c 65 6d 65 6e  Mem3Block elemen
87930 74 73 20 2a 2f 0a 20 20 20 20 7d 20 68 64 72 3b  ts */.    } hdr;
87940 0a 20 20 20 20 73 74 72 75 63 74 20 7b 0a 20 20  .    struct {.  
87950 20 20 20 20 75 33 32 20 6e 65 78 74 3b 20 20 20      u32 next;   
87960 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20      /* Index in 
87970 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 5d 20 6f 66 20  mem3.aPool[] of 
87980 6e 65 78 74 20 66 72 65 65 20 63 68 75 6e 6b 20  next free chunk 
87990 2a 2f 0a 20 20 20 20 20 20 75 33 32 20 70 72 65  */.      u32 pre
879a0 76 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65  v;       /* Inde
879b0 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  x in mem3.aPool[
879c0 5d 20 6f 66 20 70 72 65 76 69 6f 75 73 20 66 72  ] of previous fr
879d0 65 65 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 20 20  ee chunk */.    
879e0 7d 20 6c 69 73 74 3b 0a 20 20 7d 20 75 3b 0a 7d  } list;.  } u;.}
879f0 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20  ;../*.** All of 
87a00 74 68 65 20 73 74 61 74 69 63 20 76 61 72 69 61  the static varia
87a10 62 6c 65 73 20 75 73 65 64 20 62 79 20 74 68 69  bles used by thi
87a20 73 20 6d 6f 64 75 6c 65 20 61 72 65 20 63 6f 6c  s module are col
87a30 6c 65 63 74 65 64 0a 2a 2a 20 69 6e 74 6f 20 61  lected.** into a
87a40 20 73 69 6e 67 6c 65 20 73 74 72 75 63 74 75 72   single structur
87a50 65 20 6e 61 6d 65 64 20 22 6d 65 6d 33 22 2e 20  e named "mem3". 
87a60 20 54 68 69 73 20 69 73 20 74 6f 20 6b 65 65 70   This is to keep
87a70 20 74 68 65 0a 2a 2a 20 73 74 61 74 69 63 20 76   the.** static v
87a80 61 72 69 61 62 6c 65 73 20 6f 72 67 61 6e 69 7a  ariables organiz
87a90 65 64 20 61 6e 64 20 74 6f 20 72 65 64 75 63 65  ed and to reduce
87aa0 20 6e 61 6d 65 73 70 61 63 65 20 70 6f 6c 6c 75   namespace pollu
87ab0 74 69 6f 6e 0a 2a 2a 20 77 68 65 6e 20 74 68 69  tion.** when thi
87ac0 73 20 6d 6f 64 75 6c 65 20 69 73 20 63 6f 6d 62  s module is comb
87ad0 69 6e 65 64 20 77 69 74 68 20 6f 74 68 65 72 20  ined with other 
87ae0 69 6e 20 74 68 65 20 61 6d 61 6c 67 61 6d 61 74  in the amalgamat
87af0 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53  ion..*/.static S
87b00 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74  QLITE_WSD struct
87b10 20 4d 65 6d 33 47 6c 6f 62 61 6c 20 7b 0a 20 20   Mem3Global {.  
87b20 2f 2a 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 20 61  /*.  ** Memory a
87b30 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 61 6c 6c  vailable for all
87b40 6f 63 61 74 69 6f 6e 2e 20 6e 50 6f 6f 6c 20 69  ocation. nPool i
87b50 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  s the size of th
87b60 65 20 61 72 72 61 79 0a 20 20 2a 2a 20 28 69 6e  e array.  ** (in
87b70 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 20 70 6f 69   Mem3Blocks) poi
87b80 6e 74 65 64 20 74 6f 20 62 79 20 61 50 6f 6f 6c  nted to by aPool
87b90 20 6c 65 73 73 20 32 2e 0a 20 20 2a 2f 0a 20 20   less 2..  */.  
87ba0 75 33 32 20 6e 50 6f 6f 6c 3b 0a 20 20 4d 65 6d  u32 nPool;.  Mem
87bb0 33 42 6c 6f 63 6b 20 2a 61 50 6f 6f 6c 3b 0a 0a  3Block *aPool;..
87bc0 20 20 2f 2a 0a 20 20 2a 2a 20 54 72 75 65 20 69    /*.  ** True i
87bd0 66 20 77 65 20 61 72 65 20 65 76 61 6c 75 61 74  f we are evaluat
87be0 69 6e 67 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d 65  ing an out-of-me
87bf0 6d 6f 72 79 20 63 61 6c 6c 62 61 63 6b 2e 0a 20  mory callback.. 
87c00 20 2a 2f 0a 20 20 69 6e 74 20 61 6c 61 72 6d 42   */.  int alarmB
87c10 75 73 79 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a  usy;.  .  /*.  *
87c20 2a 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74 72  * Mutex to contr
87c30 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68 65  ol access to the
87c40 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
87c50 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 20 20  on subsystem..  
87c60 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  */.  sqlite3_mut
87c70 65 78 20 2a 6d 75 74 65 78 3b 0a 20 20 0a 20 20  ex *mutex;.  .  
87c80 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 6d 69 6e 69  /*.  ** The mini
87c90 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 66 72  mum amount of fr
87ca0 65 65 20 73 70 61 63 65 20 74 68 61 74 20 77 65  ee space that we
87cb0 20 68 61 76 65 20 73 65 65 6e 2e 0a 20 20 2a 2f   have seen..  */
87cc0 0a 20 20 75 33 32 20 6d 6e 4d 61 73 74 65 72 3b  .  u32 mnMaster;
87cd0 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 69 4d 61 73  ..  /*.  ** iMas
87ce0 74 65 72 20 69 73 20 74 68 65 20 69 6e 64 65 78  ter is the index
87cf0 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 63   of the master c
87d00 68 75 6e 6b 2e 20 20 4d 6f 73 74 20 6e 65 77 20  hunk.  Most new 
87d10 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 20 20 2a 2a  allocations.  **
87d20 20 6f 63 63 75 72 20 6f 66 66 20 6f 66 20 74 68   occur off of th
87d30 69 73 20 63 68 75 6e 6b 2e 20 20 73 7a 4d 61 73  is chunk.  szMas
87d40 74 65 72 20 69 73 20 74 68 65 20 73 69 7a 65 20  ter is the size 
87d50 28 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 0a  (in Mem3Blocks).
87d60 20 20 2a 2a 20 6f 66 20 74 68 65 20 63 75 72 72    ** of the curr
87d70 65 6e 74 20 6d 61 73 74 65 72 2e 20 20 69 4d 61  ent master.  iMa
87d80 73 74 65 72 20 69 73 20 30 20 69 66 20 74 68 65  ster is 0 if the
87d90 72 65 20 69 73 20 6e 6f 74 20 6d 61 73 74 65 72  re is not master
87da0 20 63 68 75 6e 6b 2e 0a 20 20 2a 2a 20 54 68 65   chunk..  ** The
87db0 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 69 73   master chunk is
87dc0 20 6e 6f 74 20 69 6e 20 65 69 74 68 65 72 20 74   not in either t
87dd0 68 65 20 61 69 48 61 73 68 5b 5d 20 6f 72 20 61  he aiHash[] or a
87de0 69 53 6d 61 6c 6c 5b 5d 2e 0a 20 20 2a 2f 0a 20  iSmall[]..  */. 
87df0 20 75 33 32 20 69 4d 61 73 74 65 72 3b 0a 20 20   u32 iMaster;.  
87e00 75 33 32 20 73 7a 4d 61 73 74 65 72 3b 0a 0a 20  u32 szMaster;.. 
87e10 20 2f 2a 0a 20 20 2a 2a 20 41 72 72 61 79 20 6f   /*.  ** Array o
87e20 66 20 6c 69 73 74 73 20 6f 66 20 66 72 65 65 20  f lists of free 
87e30 62 6c 6f 63 6b 73 20 61 63 63 6f 72 64 69 6e 67  blocks according
87e40 20 74 6f 20 74 68 65 20 62 6c 6f 63 6b 20 73 69   to the block si
87e50 7a 65 20 0a 20 20 2a 2a 20 66 6f 72 20 73 6d 61  ze .  ** for sma
87e60 6c 6c 65 72 20 63 68 75 6e 6b 73 2c 20 6f 72 20  ller chunks, or 
87e70 61 20 68 61 73 68 20 6f 6e 20 74 68 65 20 62 6c  a hash on the bl
87e80 6f 63 6b 20 73 69 7a 65 20 66 6f 72 20 6c 61 72  ock size for lar
87e90 67 65 72 0a 20 20 2a 2a 20 63 68 75 6e 6b 73 2e  ger.  ** chunks.
87ea0 0a 20 20 2a 2f 0a 20 20 75 33 32 20 61 69 53 6d  .  */.  u32 aiSm
87eb0 61 6c 6c 5b 4d 58 5f 53 4d 41 4c 4c 2d 31 5d 3b  all[MX_SMALL-1];
87ec0 20 20 20 2f 2a 20 46 6f 72 20 73 69 7a 65 73 20     /* For sizes 
87ed0 32 20 74 68 72 6f 75 67 68 20 4d 58 5f 53 4d 41  2 through MX_SMA
87ee0 4c 4c 2c 20 69 6e 63 6c 75 73 69 76 65 20 2a 2f  LL, inclusive */
87ef0 0a 20 20 75 33 32 20 61 69 48 61 73 68 5b 4e 5f  .  u32 aiHash[N_
87f00 48 41 53 48 5d 3b 20 20 20 20 20 20 20 20 2f 2a  HASH];        /*
87f10 20 46 6f 72 20 73 69 7a 65 73 20 4d 58 5f 53 4d   For sizes MX_SM
87f20 41 4c 4c 2b 31 20 61 6e 64 20 6c 61 72 67 65 72  ALL+1 and larger
87f30 20 2a 2f 0a 7d 20 6d 65 6d 33 20 3d 20 7b 20 39   */.} mem3 = { 9
87f40 37 35 33 35 35 37 35 20 7d 3b 0a 0a 23 64 65 66  7535575 };..#def
87f50 69 6e 65 20 6d 65 6d 33 20 47 4c 4f 42 41 4c 28  ine mem3 GLOBAL(
87f60 73 74 72 75 63 74 20 4d 65 6d 33 47 6c 6f 62 61  struct Mem3Globa
87f70 6c 2c 20 6d 65 6d 33 29 0a 0a 2f 2a 0a 2a 2a 20  l, mem3)../*.** 
87f80 55 6e 6c 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b  Unlink the chunk
87f90 20 61 74 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69   at mem3.aPool[i
87fa0 5d 20 66 72 6f 6d 20 6c 69 73 74 20 69 74 20 69  ] from list it i
87fb0 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f  s currently.** o
87fc0 6e 2e 20 20 2a 70 52 6f 6f 74 20 69 73 20 74 68  n.  *pRoot is th
87fd0 65 20 6c 69 73 74 20 74 68 61 74 20 69 20 69 73  e list that i is
87fe0 20 61 20 6d 65 6d 62 65 72 20 6f 66 2e 0a 2a 2f   a member of..*/
87ff0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d  .static void mem
88000 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69  sys3UnlinkFromLi
88010 73 74 28 75 33 32 20 69 2c 20 75 33 32 20 2a 70  st(u32 i, u32 *p
88020 52 6f 6f 74 29 7b 0a 20 20 75 33 32 20 6e 65 78  Root){.  u32 nex
88030 74 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  t = mem3.aPool[i
88040 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 3b 0a 20  ].u.list.next;. 
88050 20 75 33 32 20 70 72 65 76 20 3d 20 6d 65 6d 33   u32 prev = mem3
88060 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74  .aPool[i].u.list
88070 2e 70 72 65 76 3b 0a 20 20 61 73 73 65 72 74 28  .prev;.  assert(
88080 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
88090 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20  eld(mem3.mutex) 
880a0 29 3b 0a 20 20 69 66 28 20 70 72 65 76 3d 3d 30  );.  if( prev==0
880b0 20 29 7b 0a 20 20 20 20 2a 70 52 6f 6f 74 20 3d   ){.    *pRoot =
880c0 20 6e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a   next;.  }else{.
880d0 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70      mem3.aPool[p
880e0 72 65 76 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74  rev].u.list.next
880f0 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 69   = next;.  }.  i
88100 66 28 20 6e 65 78 74 20 29 7b 0a 20 20 20 20 6d  f( next ){.    m
88110 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 78 74 5d 2e  em3.aPool[next].
88120 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 70 72  u.list.prev = pr
88130 65 76 3b 0a 20 20 7d 0a 20 20 6d 65 6d 33 2e 61  ev;.  }.  mem3.a
88140 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e  Pool[i].u.list.n
88150 65 78 74 20 3d 20 30 3b 0a 20 20 6d 65 6d 33 2e  ext = 0;.  mem3.
88160 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e  aPool[i].u.list.
88170 70 72 65 76 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a  prev = 0;.}../*.
88180 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20 63 68  ** Unlink the ch
88190 75 6e 6b 20 61 74 20 69 6e 64 65 78 20 69 20 66  unk at index i f
881a0 72 6f 6d 20 0a 2a 2a 20 77 68 61 74 65 76 65 72  rom .** whatever
881b0 20 6c 69 73 74 20 69 73 20 63 75 72 72 65 6e 74   list is current
881c0 6c 79 20 61 20 6d 65 6d 62 65 72 20 6f 66 2e 0a  ly a member of..
881d0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d  */.static void m
881e0 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 75 33 32  emsys3Unlink(u32
881f0 20 69 29 7b 0a 20 20 75 33 32 20 73 69 7a 65 2c   i){.  u32 size,
88200 20 68 61 73 68 3b 0a 20 20 61 73 73 65 72 74 28   hash;.  assert(
88210 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
88220 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20  eld(mem3.mutex) 
88230 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65  );.  assert( (me
88240 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e  m3.aPool[i-1].u.
88250 68 64 72 2e 73 69 7a 65 34 78 20 26 20 31 29 3d  hdr.size4x & 1)=
88260 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
88270 69 3e 3d 31 20 29 3b 0a 20 20 73 69 7a 65 20 3d  i>=1 );.  size =
88280 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d   mem3.aPool[i-1]
88290 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 3b  .u.hdr.size4x/4;
882a0 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 3d  .  assert( size=
882b0 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69  =mem3.aPool[i+si
882c0 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76  ze-1].u.hdr.prev
882d0 53 69 7a 65 20 29 3b 0a 20 20 61 73 73 65 72 74  Size );.  assert
882e0 28 20 73 69 7a 65 3e 3d 32 20 29 3b 0a 20 20 69  ( size>=2 );.  i
882f0 66 28 20 73 69 7a 65 20 3c 3d 20 4d 58 5f 53 4d  f( size <= MX_SM
88300 41 4c 4c 20 29 7b 0a 20 20 20 20 6d 65 6d 73 79  ALL ){.    memsy
88310 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74  s3UnlinkFromList
88320 28 69 2c 20 26 6d 65 6d 33 2e 61 69 53 6d 61 6c  (i, &mem3.aiSmal
88330 6c 5b 73 69 7a 65 2d 32 5d 29 3b 0a 20 20 7d 65  l[size-2]);.  }e
88340 6c 73 65 7b 0a 20 20 20 20 68 61 73 68 20 3d 20  lse{.    hash = 
88350 73 69 7a 65 20 25 20 4e 5f 48 41 53 48 3b 0a 20  size % N_HASH;. 
88360 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b     memsys3Unlink
88370 46 72 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d  FromList(i, &mem
88380 33 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 29 3b  3.aiHash[hash]);
88390 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69  .  }.}../*.** Li
883a0 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20  nk the chunk at 
883b0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 20 73 6f  mem3.aPool[i] so
883c0 20 74 68 61 74 20 69 73 20 6f 6e 20 74 68 65 20   that is on the 
883d0 6c 69 73 74 20 72 6f 6f 74 65 64 0a 2a 2a 20 61  list rooted.** a
883e0 74 20 2a 70 52 6f 6f 74 2e 0a 2a 2f 0a 73 74 61  t *pRoot..*/.sta
883f0 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33  tic void memsys3
88400 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 28 75 33 32  LinkIntoList(u32
88410 20 69 2c 20 75 33 32 20 2a 70 52 6f 6f 74 29 7b   i, u32 *pRoot){
88420 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
88430 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65  e3_mutex_held(me
88440 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 6d  m3.mutex) );.  m
88450 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c  em3.aPool[i].u.l
88460 69 73 74 2e 6e 65 78 74 20 3d 20 2a 70 52 6f 6f  ist.next = *pRoo
88470 74 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  t;.  mem3.aPool[
88480 69 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d  i].u.list.prev =
88490 20 30 3b 0a 20 20 69 66 28 20 2a 70 52 6f 6f 74   0;.  if( *pRoot
884a0 20 29 7b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f   ){.    mem3.aPo
884b0 6f 6c 5b 2a 70 52 6f 6f 74 5d 2e 75 2e 6c 69 73  ol[*pRoot].u.lis
884c0 74 2e 70 72 65 76 20 3d 20 69 3b 0a 20 20 7d 0a  t.prev = i;.  }.
884d0 20 20 2a 70 52 6f 6f 74 20 3d 20 69 3b 0a 7d 0a    *pRoot = i;.}.
884e0 0a 2f 2a 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 20  ./*.** Link the 
884f0 63 68 75 6e 6b 20 61 74 20 69 6e 64 65 78 20 69  chunk at index i
88500 20 69 6e 74 6f 20 65 69 74 68 65 72 20 74 68 65   into either the
88510 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20   appropriate.** 
88520 73 6d 61 6c 6c 20 63 68 75 6e 6b 20 6c 69 73 74  small chunk list
88530 2c 20 6f 72 20 69 6e 74 6f 20 74 68 65 20 6c 61  , or into the la
88540 72 67 65 20 63 68 75 6e 6b 20 68 61 73 68 20 74  rge chunk hash t
88550 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  able..*/.static 
88560 76 6f 69 64 20 6d 65 6d 73 79 73 33 4c 69 6e 6b  void memsys3Link
88570 28 75 33 32 20 69 29 7b 0a 20 20 75 33 32 20 73  (u32 i){.  u32 s
88580 69 7a 65 2c 20 68 61 73 68 3b 0a 20 20 61 73 73  ize, hash;.  ass
88590 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
885a0 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74  ex_held(mem3.mut
885b0 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ex) );.  assert(
885c0 20 69 3e 3d 31 20 29 3b 0a 20 20 61 73 73 65 72   i>=1 );.  asser
885d0 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  t( (mem3.aPool[i
885e0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
885f0 20 26 20 31 29 3d 3d 30 20 29 3b 0a 20 20 73 69   & 1)==0 );.  si
88600 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  ze = mem3.aPool[
88610 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  i-1].u.hdr.size4
88620 78 2f 34 3b 0a 20 20 61 73 73 65 72 74 28 20 73  x/4;.  assert( s
88630 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  ize==mem3.aPool[
88640 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e  i+size-1].u.hdr.
88650 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 20 61 73  prevSize );.  as
88660 73 65 72 74 28 20 73 69 7a 65 3e 3d 32 20 29 3b  sert( size>=2 );
88670 0a 20 20 69 66 28 20 73 69 7a 65 20 3c 3d 20 4d  .  if( size <= M
88680 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 20 20 6d  X_SMALL ){.    m
88690 65 6d 73 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69  emsys3LinkIntoLi
886a0 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 53 6d  st(i, &mem3.aiSm
886b0 61 6c 6c 5b 73 69 7a 65 2d 32 5d 29 3b 0a 20 20  all[size-2]);.  
886c0 7d 65 6c 73 65 7b 0a 20 20 20 20 68 61 73 68 20  }else{.    hash 
886d0 3d 20 73 69 7a 65 20 25 20 4e 5f 48 41 53 48 3b  = size % N_HASH;
886e0 0a 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b  .    memsys3Link
886f0 49 6e 74 6f 4c 69 73 74 28 69 2c 20 26 6d 65 6d  IntoList(i, &mem
88700 33 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 29 3b  3.aiHash[hash]);
88710 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66  .  }.}../*.** If
88720 20 74 68 65 20 53 54 41 54 49 43 5f 4d 45 4d 20   the STATIC_MEM 
88730 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 61 6c 72  mutex is not alr
88740 65 61 64 79 20 68 65 6c 64 2c 20 6f 62 74 61 69  eady held, obtai
88750 6e 20 69 74 20 6e 6f 77 2e 20 54 68 65 20 6d 75  n it now. The mu
88760 74 65 78 0a 2a 2a 20 77 69 6c 6c 20 61 6c 72 65  tex.** will alre
88770 61 64 79 20 62 65 20 68 65 6c 64 20 28 6f 62 74  ady be held (obt
88780 61 69 6e 65 64 20 62 79 20 63 6f 64 65 20 69 6e  ained by code in
88790 20 6d 61 6c 6c 6f 63 2e 63 29 20 69 66 0a 2a 2a   malloc.c) if.**
887a0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
887b0 6e 66 69 67 2e 62 4d 65 6d 53 74 61 74 20 69 73  nfig.bMemStat is
887c0 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   true..*/.static
887d0 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 45 6e 74   void memsys3Ent
887e0 65 72 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20  er(void){.  if( 
887f0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
88800 66 69 67 2e 62 4d 65 6d 73 74 61 74 3d 3d 30 20  fig.bMemstat==0 
88810 26 26 20 6d 65 6d 33 2e 6d 75 74 65 78 3d 3d 30  && mem3.mutex==0
88820 20 29 7b 0a 20 20 20 20 6d 65 6d 33 2e 6d 75 74   ){.    mem3.mut
88830 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65  ex = sqlite3Mute
88840 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
88850 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29 3b  TEX_STATIC_MEM);
88860 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  .  }.  sqlite3_m
88870 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 33 2e  utex_enter(mem3.
88880 6d 75 74 65 78 29 3b 0a 7d 0a 73 74 61 74 69 63  mutex);.}.static
88890 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 4c 65 61   void memsys3Lea
888a0 76 65 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69  ve(void){.  sqli
888b0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
888c0 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a  mem3.mutex);.}..
888d0 2f 2a 0a 2a 2a 20 43 61 6c 6c 65 64 20 77 68 65  /*.** Called whe
888e0 6e 20 77 65 20 61 72 65 20 75 6e 61 62 6c 65 20  n we are unable 
888f0 74 6f 20 73 61 74 69 73 66 79 20 61 6e 20 61 6c  to satisfy an al
88900 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 6e 42 79 74  location of nByt
88910 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  es..*/.static vo
88920 69 64 20 6d 65 6d 73 79 73 33 4f 75 74 4f 66 4d  id memsys3OutOfM
88930 65 6d 6f 72 79 28 69 6e 74 20 6e 42 79 74 65 29  emory(int nByte)
88940 7b 0a 20 20 69 66 28 20 21 6d 65 6d 33 2e 61 6c  {.  if( !mem3.al
88950 61 72 6d 42 75 73 79 20 29 7b 0a 20 20 20 20 6d  armBusy ){.    m
88960 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79 20 3d 20  em3.alarmBusy = 
88970 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  1;.    assert( s
88980 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
88990 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b  d(mem3.mutex) );
889a0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
889b0 65 78 5f 6c 65 61 76 65 28 6d 65 6d 33 2e 6d 75  ex_leave(mem3.mu
889c0 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  tex);.    sqlite
889d0 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79  3_release_memory
889e0 28 6e 42 79 74 65 29 3b 0a 20 20 20 20 73 71 6c  (nByte);.    sql
889f0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
88a00 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 20 20  (mem3.mutex);.  
88a10 20 20 6d 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79    mem3.alarmBusy
88a20 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a   = 0;.  }.}.../*
88a30 0a 2a 2a 20 43 68 75 6e 6b 20 69 20 69 73 20 61  .** Chunk i is a
88a40 20 66 72 65 65 20 63 68 75 6e 6b 20 74 68 61 74   free chunk that
88a50 20 68 61 73 20 62 65 65 6e 20 75 6e 6c 69 6e 6b   has been unlink
88a60 65 64 2e 20 20 41 64 6a 75 73 74 20 69 74 73 20  ed.  Adjust its 
88a70 0a 2a 2a 20 73 69 7a 65 20 70 61 72 61 6d 65 74  .** size paramet
88a80 65 72 73 20 66 6f 72 20 63 68 65 63 6b 2d 6f 75  ers for check-ou
88a90 74 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70  t and return a p
88aa0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 0a 2a  ointer to the .*
88ab0 2a 20 75 73 65 72 20 70 6f 72 74 69 6f 6e 20 6f  * user portion o
88ac0 66 20 74 68 65 20 63 68 75 6e 6b 2e 0a 2a 2f 0a  f the chunk..*/.
88ad0 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d  static void *mem
88ae0 73 79 73 33 43 68 65 63 6b 6f 75 74 28 75 33 32  sys3Checkout(u32
88af0 20 69 2c 20 75 33 32 20 6e 42 6c 6f 63 6b 29 7b   i, u32 nBlock){
88b00 0a 20 20 75 33 32 20 78 3b 0a 20 20 61 73 73 65  .  u32 x;.  asse
88b10 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
88b20 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65  x_held(mem3.mute
88b30 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
88b40 69 3e 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 74  i>=1 );.  assert
88b50 28 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31  ( mem3.aPool[i-1
88b60 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34  ].u.hdr.size4x/4
88b70 3d 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 61 73  ==nBlock );.  as
88b80 73 65 72 74 28 20 6d 65 6d 33 2e 61 50 6f 6f 6c  sert( mem3.aPool
88b90 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 2e 68  [i+nBlock-1].u.h
88ba0 64 72 2e 70 72 65 76 53 69 7a 65 3d 3d 6e 42 6c  dr.prevSize==nBl
88bb0 6f 63 6b 20 29 3b 0a 20 20 78 20 3d 20 6d 65 6d  ock );.  x = mem
88bc0 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68  3.aPool[i-1].u.h
88bd0 64 72 2e 73 69 7a 65 34 78 3b 0a 20 20 6d 65 6d  dr.size4x;.  mem
88be0 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68  3.aPool[i-1].u.h
88bf0 64 72 2e 73 69 7a 65 34 78 20 3d 20 6e 42 6c 6f  dr.size4x = nBlo
88c00 63 6b 2a 34 20 7c 20 31 20 7c 20 28 78 26 32 29  ck*4 | 1 | (x&2)
88c10 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  ;.  mem3.aPool[i
88c20 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 2e 68 64 72  +nBlock-1].u.hdr
88c30 2e 70 72 65 76 53 69 7a 65 20 3d 20 6e 42 6c 6f  .prevSize = nBlo
88c40 63 6b 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c  ck;.  mem3.aPool
88c50 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 2e 68  [i+nBlock-1].u.h
88c60 64 72 2e 73 69 7a 65 34 78 20 7c 3d 20 32 3b 0a  dr.size4x |= 2;.
88c70 20 20 72 65 74 75 72 6e 20 26 6d 65 6d 33 2e 61    return &mem3.a
88c80 50 6f 6f 6c 5b 69 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a  Pool[i];.}../*.*
88c90 2a 20 43 61 72 76 65 20 61 20 70 69 65 63 65 20  * Carve a piece 
88ca0 6f 66 66 20 6f 66 20 74 68 65 20 65 6e 64 20 6f  off of the end o
88cb0 66 20 74 68 65 20 6d 65 6d 33 2e 69 4d 61 73 74  f the mem3.iMast
88cc0 65 72 20 66 72 65 65 20 63 68 75 6e 6b 2e 0a 2a  er free chunk..*
88cd0 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  * Return a point
88ce0 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20 61 6c  er to the new al
88cf0 6c 6f 63 61 74 69 6f 6e 2e 20 20 4f 72 2c 20 69  location.  Or, i
88d00 66 20 74 68 65 20 6d 61 73 74 65 72 20 63 68 75  f the master chu
88d10 6e 6b 0a 2a 2a 20 69 73 20 6e 6f 74 20 6c 61 72  nk.** is not lar
88d20 67 65 20 65 6e 6f 75 67 68 2c 20 72 65 74 75 72  ge enough, retur
88d30 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  n 0..*/.static v
88d40 6f 69 64 20 2a 6d 65 6d 73 79 73 33 46 72 6f 6d  oid *memsys3From
88d50 4d 61 73 74 65 72 28 75 33 32 20 6e 42 6c 6f 63  Master(u32 nBloc
88d60 6b 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71  k){.  assert( sq
88d70 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
88d80 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a  (mem3.mutex) );.
88d90 20 20 61 73 73 65 72 74 28 20 6d 65 6d 33 2e 73    assert( mem3.s
88da0 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20  zMaster>=nBlock 
88db0 29 3b 0a 20 20 69 66 28 20 6e 42 6c 6f 63 6b 3e  );.  if( nBlock>
88dc0 3d 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31  =mem3.szMaster-1
88dd0 20 29 7b 0a 20 20 20 20 2f 2a 20 55 73 65 20 74   ){.    /* Use t
88de0 68 65 20 65 6e 74 69 72 65 20 6d 61 73 74 65 72  he entire master
88df0 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 20   */.    void *p 
88e00 3d 20 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f 75  = memsys3Checkou
88e10 74 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2c 20  t(mem3.iMaster, 
88e20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 29 3b 0a  mem3.szMaster);.
88e30 20 20 20 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72      mem3.iMaster
88e40 20 3d 20 30 3b 0a 20 20 20 20 6d 65 6d 33 2e 73   = 0;.    mem3.s
88e50 7a 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20  zMaster = 0;.   
88e60 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 20 3d   mem3.mnMaster =
88e70 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70   0;.    return p
88e80 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  ;.  }else{.    /
88e90 2a 20 53 70 6c 69 74 20 74 68 65 20 6d 61 73 74  * Split the mast
88ea0 65 72 20 62 6c 6f 63 6b 2e 20 20 52 65 74 75 72  er block.  Retur
88eb0 6e 20 74 68 65 20 74 61 69 6c 2e 20 2a 2f 0a 20  n the tail. */. 
88ec0 20 20 20 75 33 32 20 6e 65 77 69 2c 20 78 3b 0a     u32 newi, x;.
88ed0 20 20 20 20 6e 65 77 69 20 3d 20 6d 65 6d 33 2e      newi = mem3.
88ee0 69 4d 61 73 74 65 72 20 2b 20 6d 65 6d 33 2e 73  iMaster + mem3.s
88ef0 7a 4d 61 73 74 65 72 20 2d 20 6e 42 6c 6f 63 6b  zMaster - nBlock
88f00 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 65  ;.    assert( ne
88f10 77 69 20 3e 20 6d 65 6d 33 2e 69 4d 61 73 74 65  wi > mem3.iMaste
88f20 72 2b 31 20 29 3b 0a 20 20 20 20 6d 65 6d 33 2e  r+1 );.    mem3.
88f30 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74  aPool[mem3.iMast
88f40 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72  er+mem3.szMaster
88f50 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69  -1].u.hdr.prevSi
88f60 7a 65 20 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 20 20  ze = nBlock;.   
88f70 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33   mem3.aPool[mem3
88f80 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a  .iMaster+mem3.sz
88f90 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e  Master-1].u.hdr.
88fa0 73 69 7a 65 34 78 20 7c 3d 20 32 3b 0a 20 20 20  size4x |= 2;.   
88fb0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 77 69   mem3.aPool[newi
88fc0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
88fd0 20 3d 20 6e 42 6c 6f 63 6b 2a 34 20 2b 20 31 3b   = nBlock*4 + 1;
88fe0 0a 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74  .    mem3.szMast
88ff0 65 72 20 2d 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 20  er -= nBlock;.  
89000 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 77    mem3.aPool[new
89010 69 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53  i-1].u.hdr.prevS
89020 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73  ize = mem3.szMas
89030 74 65 72 3b 0a 20 20 20 20 78 20 3d 20 6d 65 6d  ter;.    x = mem
89040 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61  3.aPool[mem3.iMa
89050 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  ster-1].u.hdr.si
89060 7a 65 34 78 20 26 20 32 3b 0a 20 20 20 20 6d 65  ze4x & 2;.    me
89070 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d  m3.aPool[mem3.iM
89080 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73  aster-1].u.hdr.s
89090 69 7a 65 34 78 20 3d 20 6d 65 6d 33 2e 73 7a 4d  ize4x = mem3.szM
890a0 61 73 74 65 72 2a 34 20 7c 20 78 3b 0a 20 20 20  aster*4 | x;.   
890b0 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61 73 74   if( mem3.szMast
890c0 65 72 20 3c 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74  er < mem3.mnMast
890d0 65 72 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 33  er ){.      mem3
890e0 2e 6d 6e 4d 61 73 74 65 72 20 3d 20 6d 65 6d 33  .mnMaster = mem3
890f0 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 7d  .szMaster;.    }
89100 0a 20 20 20 20 72 65 74 75 72 6e 20 28 76 6f 69  .    return (voi
89110 64 2a 29 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e  d*)&mem3.aPool[n
89120 65 77 69 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ewi];.  }.}../*.
89130 2a 2a 20 2a 70 52 6f 6f 74 20 69 73 20 74 68 65  ** *pRoot is the
89140 20 68 65 61 64 20 6f 66 20 61 20 6c 69 73 74 20   head of a list 
89150 6f 66 20 66 72 65 65 20 63 68 75 6e 6b 73 20 6f  of free chunks o
89160 66 20 74 68 65 20 73 61 6d 65 20 73 69 7a 65 0a  f the same size.
89170 2a 2a 20 6f 72 20 73 61 6d 65 20 73 69 7a 65 20  ** or same size 
89180 68 61 73 68 2e 20 20 49 6e 20 6f 74 68 65 72 20  hash.  In other 
89190 77 6f 72 64 73 2c 20 2a 70 52 6f 6f 74 20 69 73  words, *pRoot is
891a0 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 65 69 74   an entry in eit
891b0 68 65 72 0a 2a 2a 20 6d 65 6d 33 2e 61 69 53 6d  her.** mem3.aiSm
891c0 61 6c 6c 5b 5d 20 6f 72 20 6d 65 6d 33 2e 61 69  all[] or mem3.ai
891d0 48 61 73 68 5b 5d 2e 20 20 0a 2a 2a 0a 2a 2a 20  Hash[].  .**.** 
891e0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 78 61  This routine exa
891f0 6d 69 6e 65 73 20 61 6c 6c 20 65 6e 74 72 69 65  mines all entrie
89200 73 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 6c  s on the given l
89210 69 73 74 20 61 6e 64 20 74 72 69 65 73 0a 2a 2a  ist and tries.**
89220 20 74 6f 20 63 6f 61 6c 65 73 63 65 20 65 61 63   to coalesce eac
89230 68 20 65 6e 74 72 69 65 73 20 77 69 74 68 20 61  h entries with a
89240 64 6a 61 63 65 6e 74 20 66 72 65 65 20 63 68 75  djacent free chu
89250 6e 6b 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66 20  nks.  .**.** If 
89260 69 74 20 73 65 65 73 20 61 20 63 68 75 6e 6b 20  it sees a chunk 
89270 74 68 61 74 20 69 73 20 6c 61 72 67 65 72 20 74  that is larger t
89280 68 61 6e 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72  han mem3.iMaster
89290 2c 20 69 74 20 72 65 70 6c 61 63 65 73 20 0a 2a  , it replaces .*
892a0 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 65  * the current me
892b0 6d 33 2e 69 4d 61 73 74 65 72 20 77 69 74 68 20  m3.iMaster with 
892c0 74 68 65 20 6e 65 77 20 6c 61 72 67 65 72 20 63  the new larger c
892d0 68 75 6e 6b 2e 20 20 49 6e 20 6f 72 64 65 72 20  hunk.  In order 
892e0 66 6f 72 0a 2a 2a 20 74 68 69 73 20 6d 65 6d 33  for.** this mem3
892f0 2e 69 4d 61 73 74 65 72 20 72 65 70 6c 61 63 65  .iMaster replace
89300 6d 65 6e 74 20 74 6f 20 77 6f 72 6b 2c 20 74 68  ment to work, th
89310 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 6d  e master chunk m
89320 75 73 74 20 62 65 0a 2a 2a 20 6c 69 6e 6b 65 64  ust be.** linked
89330 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68 20 74   into the hash t
89340 61 62 6c 65 73 2e 20 20 54 68 61 74 20 69 73 20  ables.  That is 
89350 6e 6f 74 20 74 68 65 20 6e 6f 72 6d 61 6c 20 73  not the normal s
89360 74 61 74 65 20 6f 66 0a 2a 2a 20 61 66 66 61 69  tate of.** affai
89370 72 73 2c 20 6f 66 20 63 6f 75 72 73 65 2e 20 20  rs, of course.  
89380 54 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74  The calling rout
89390 69 6e 65 20 6d 75 73 74 20 6c 69 6e 6b 20 74 68  ine must link th
893a0 65 20 6d 61 73 74 65 72 0a 2a 2a 20 63 68 75 6e  e master.** chun
893b0 6b 20 62 65 66 6f 72 65 20 69 6e 76 6f 6b 69 6e  k before invokin
893c0 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20  g this routine, 
893d0 74 68 65 6e 20 6d 75 73 74 20 75 6e 6c 69 6e 6b  then must unlink
893e0 20 74 68 65 20 28 70 6f 73 73 69 62 6c 79 0a 2a   the (possibly.*
893f0 2a 20 63 68 61 6e 67 65 64 29 20 6d 61 73 74 65  * changed) maste
89400 72 20 63 68 75 6e 6b 20 6f 6e 63 65 20 74 68 69  r chunk once thi
89410 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 66 69  s routine has fi
89420 6e 69 73 68 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  nished..*/.stati
89430 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 4d 65  c void memsys3Me
89440 72 67 65 28 75 33 32 20 2a 70 52 6f 6f 74 29 7b  rge(u32 *pRoot){
89450 0a 20 20 75 33 32 20 69 4e 65 78 74 2c 20 70 72  .  u32 iNext, pr
89460 65 76 2c 20 73 69 7a 65 2c 20 69 2c 20 78 3b 0a  ev, size, i, x;.
89470 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
89480 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65  e3_mutex_held(me
89490 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66  m3.mutex) );.  f
894a0 6f 72 28 69 3d 2a 70 52 6f 6f 74 3b 20 69 3e 30  or(i=*pRoot; i>0
894b0 3b 20 69 3d 69 4e 65 78 74 29 7b 0a 20 20 20 20  ; i=iNext){.    
894c0 69 4e 65 78 74 20 3d 20 6d 65 6d 33 2e 61 50 6f  iNext = mem3.aPo
894d0 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78  ol[i].u.list.nex
894e0 74 3b 0a 20 20 20 20 73 69 7a 65 20 3d 20 6d 65  t;.    size = me
894f0 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e  m3.aPool[i-1].u.
89500 68 64 72 2e 73 69 7a 65 34 78 3b 0a 20 20 20 20  hdr.size4x;.    
89510 61 73 73 65 72 74 28 20 28 73 69 7a 65 26 31 29  assert( (size&1)
89520 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 28  ==0 );.    if( (
89530 73 69 7a 65 26 32 29 3d 3d 30 20 29 7b 0a 20 20  size&2)==0 ){.  
89540 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e      memsys3Unlin
89550 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20 70 52 6f  kFromList(i, pRo
89560 6f 74 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  ot);.      asser
89570 74 28 20 69 20 3e 20 6d 65 6d 33 2e 61 50 6f 6f  t( i > mem3.aPoo
89580 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65  l[i-1].u.hdr.pre
89590 76 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 70  vSize );.      p
895a0 72 65 76 20 3d 20 69 20 2d 20 6d 65 6d 33 2e 61  rev = i - mem3.a
895b0 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e  Pool[i-1].u.hdr.
895c0 70 72 65 76 53 69 7a 65 3b 0a 20 20 20 20 20 20  prevSize;.      
895d0 69 66 28 20 70 72 65 76 3d 3d 69 4e 65 78 74 20  if( prev==iNext 
895e0 29 7b 0a 20 20 20 20 20 20 20 20 69 4e 65 78 74  ){.        iNext
895f0 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72   = mem3.aPool[pr
89600 65 76 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 3b  ev].u.list.next;
89610 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d  .      }.      m
89620 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 70 72 65  emsys3Unlink(pre
89630 76 29 3b 0a 20 20 20 20 20 20 73 69 7a 65 20 3d  v);.      size =
89640 20 69 20 2b 20 73 69 7a 65 2f 34 20 2d 20 70 72   i + size/4 - pr
89650 65 76 3b 0a 20 20 20 20 20 20 78 20 3d 20 6d 65  ev;.      x = me
89660 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2d 31 5d  m3.aPool[prev-1]
89670 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20  .u.hdr.size4x & 
89680 32 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50  2;.      mem3.aP
89690 6f 6f 6c 5b 70 72 65 76 2d 31 5d 2e 75 2e 68 64  ool[prev-1].u.hd
896a0 72 2e 73 69 7a 65 34 78 20 3d 20 73 69 7a 65 2a  r.size4x = size*
896b0 34 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d 65 6d  4 | x;.      mem
896c0 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2b 73 69 7a  3.aPool[prev+siz
896d0 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53  e-1].u.hdr.prevS
896e0 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 20 20  ize = size;.    
896f0 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b 28 70 72    memsys3Link(pr
89700 65 76 29 3b 0a 20 20 20 20 20 20 69 20 3d 20 70  ev);.      i = p
89710 72 65 76 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  rev;.    }else{.
89720 20 20 20 20 20 20 73 69 7a 65 20 2f 3d 20 34 3b        size /= 4;
89730 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73  .    }.    if( s
89740 69 7a 65 3e 6d 65 6d 33 2e 73 7a 4d 61 73 74 65  ize>mem3.szMaste
89750 72 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 33 2e  r ){.      mem3.
89760 69 4d 61 73 74 65 72 20 3d 20 69 3b 0a 20 20 20  iMaster = i;.   
89770 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72     mem3.szMaster
89780 20 3d 20 73 69 7a 65 3b 0a 20 20 20 20 7d 0a 20   = size;.    }. 
89790 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75   }.}../*.** Retu
897a0 72 6e 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65  rn a block of me
897b0 6d 6f 72 79 20 6f 66 20 61 74 20 6c 65 61 73 74  mory of at least
897c0 20 6e 42 79 74 65 73 20 69 6e 20 73 69 7a 65 2e   nBytes in size.
897d0 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20  .** Return NULL 
897e0 69 66 20 75 6e 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  if unable..**.**
897f0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61   This function a
89800 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 20  ssumes that the 
89810 6e 65 63 65 73 73 61 72 79 20 6d 75 74 65 78 65  necessary mutexe
89820 73 2c 20 69 66 20 61 6e 79 2c 20 61 72 65 0a 2a  s, if any, are.*
89830 2a 20 61 6c 72 65 61 64 79 20 68 65 6c 64 20 62  * already held b
89840 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 48 65  y the caller. He
89850 6e 63 65 20 22 55 6e 73 61 66 65 22 2e 0a 2a 2f  nce "Unsafe"..*/
89860 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65  .static void *me
89870 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 6e 73 61 66  msys3MallocUnsaf
89880 65 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20  e(int nByte){.  
89890 75 33 32 20 69 3b 0a 20 20 75 33 32 20 6e 42 6c  u32 i;.  u32 nBl
898a0 6f 63 6b 3b 0a 20 20 75 33 32 20 74 6f 46 72 65  ock;.  u32 toFre
898b0 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  e;..  assert( sq
898c0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
898d0 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a  (mem3.mutex) );.
898e0 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66    assert( sizeof
898f0 28 4d 65 6d 33 42 6c 6f 63 6b 29 3d 3d 38 20 29  (Mem3Block)==8 )
89900 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3c 3d 31  ;.  if( nByte<=1
89910 32 20 29 7b 0a 20 20 20 20 6e 42 6c 6f 63 6b 20  2 ){.    nBlock 
89920 3d 20 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  = 2;.  }else{.  
89930 20 20 6e 42 6c 6f 63 6b 20 3d 20 28 6e 42 79 74    nBlock = (nByt
89940 65 20 2b 20 31 31 29 2f 38 3b 0a 20 20 7d 0a 20  e + 11)/8;.  }. 
89950 20 61 73 73 65 72 74 28 20 6e 42 6c 6f 63 6b 3e   assert( nBlock>
89960 3d 32 20 29 3b 0a 0a 20 20 2f 2a 20 53 54 45 50  =2 );..  /* STEP
89970 20 31 3a 0a 20 20 2a 2a 20 4c 6f 6f 6b 20 66 6f   1:.  ** Look fo
89980 72 20 61 6e 20 65 6e 74 72 79 20 6f 66 20 74 68  r an entry of th
89990 65 20 63 6f 72 72 65 63 74 20 73 69 7a 65 20 69  e correct size i
899a0 6e 20 65 69 74 68 65 72 20 74 68 65 20 73 6d 61  n either the sma
899b0 6c 6c 0a 20 20 2a 2a 20 63 68 75 6e 6b 20 74 61  ll.  ** chunk ta
899c0 62 6c 65 20 6f 72 20 69 6e 20 74 68 65 20 6c 61  ble or in the la
899d0 72 67 65 20 63 68 75 6e 6b 20 68 61 73 68 20 74  rge chunk hash t
899e0 61 62 6c 65 2e 20 20 54 68 69 73 20 69 73 0a 20  able.  This is. 
899f0 20 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 6d   ** successful m
89a00 6f 73 74 20 6f 66 20 74 68 65 20 74 69 6d 65 20  ost of the time 
89a10 28 61 62 6f 75 74 20 39 20 74 69 6d 65 73 20 6f  (about 9 times o
89a20 75 74 20 6f 66 20 31 30 29 2e 0a 20 20 2a 2f 0a  ut of 10)..  */.
89a30 20 20 69 66 28 20 6e 42 6c 6f 63 6b 20 3c 3d 20    if( nBlock <= 
89a40 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 20 20  MX_SMALL ){.    
89a50 69 20 3d 20 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c  i = mem3.aiSmall
89a60 5b 6e 42 6c 6f 63 6b 2d 32 5d 3b 0a 20 20 20 20  [nBlock-2];.    
89a70 69 66 28 20 69 3e 30 20 29 7b 0a 20 20 20 20 20  if( i>0 ){.     
89a80 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72   memsys3UnlinkFr
89a90 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e  omList(i, &mem3.
89aa0 61 69 53 6d 61 6c 6c 5b 6e 42 6c 6f 63 6b 2d 32  aiSmall[nBlock-2
89ab0 5d 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  ]);.      return
89ac0 20 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f 75 74   memsys3Checkout
89ad0 28 69 2c 20 6e 42 6c 6f 63 6b 29 3b 0a 20 20 20  (i, nBlock);.   
89ae0 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
89af0 69 6e 74 20 68 61 73 68 20 3d 20 6e 42 6c 6f 63  int hash = nBloc
89b00 6b 20 25 20 4e 5f 48 41 53 48 3b 0a 20 20 20 20  k % N_HASH;.    
89b10 66 6f 72 28 69 3d 6d 65 6d 33 2e 61 69 48 61 73  for(i=mem3.aiHas
89b20 68 5b 68 61 73 68 5d 3b 20 69 3e 30 3b 20 69 3d  h[hash]; i>0; i=
89b30 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e  mem3.aPool[i].u.
89b40 6c 69 73 74 2e 6e 65 78 74 29 7b 0a 20 20 20 20  list.next){.    
89b50 20 20 69 66 28 20 6d 65 6d 33 2e 61 50 6f 6f 6c    if( mem3.aPool
89b60 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65  [i-1].u.hdr.size
89b70 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b 20 29 7b 0a  4x/4==nBlock ){.
89b80 20 20 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55          memsys3U
89b90 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c  nlinkFromList(i,
89ba0 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68 61   &mem3.aiHash[ha
89bb0 73 68 5d 29 3b 0a 20 20 20 20 20 20 20 20 72 65  sh]);.        re
89bc0 74 75 72 6e 20 6d 65 6d 73 79 73 33 43 68 65 63  turn memsys3Chec
89bd0 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f 63 6b 29 3b  kout(i, nBlock);
89be0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
89bf0 20 7d 0a 0a 20 20 2f 2a 20 53 54 45 50 20 32 3a   }..  /* STEP 2:
89c00 0a 20 20 2a 2a 20 54 72 79 20 74 6f 20 73 61 74  .  ** Try to sat
89c10 69 73 66 79 20 74 68 65 20 61 6c 6c 6f 63 61 74  isfy the allocat
89c20 69 6f 6e 20 62 79 20 63 61 72 76 69 6e 67 20 61  ion by carving a
89c30 20 70 69 65 63 65 20 6f 66 66 20 6f 66 20 74 68   piece off of th
89c40 65 20 65 6e 64 0a 20 20 2a 2a 20 6f 66 20 74 68  e end.  ** of th
89c50 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 20  e master chunk. 
89c60 20 54 68 69 73 20 73 74 65 70 20 75 73 75 61 6c   This step usual
89c70 6c 79 20 77 6f 72 6b 73 20 69 66 20 73 74 65 70  ly works if step
89c80 20 31 20 66 61 69 6c 73 2e 0a 20 20 2a 2f 0a 20   1 fails..  */. 
89c90 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61 73 74   if( mem3.szMast
89ca0 65 72 3e 3d 6e 42 6c 6f 63 6b 20 29 7b 0a 20 20  er>=nBlock ){.  
89cb0 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 33    return memsys3
89cc0 46 72 6f 6d 4d 61 73 74 65 72 28 6e 42 6c 6f 63  FromMaster(nBloc
89cd0 6b 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 53  k);.  }...  /* S
89ce0 54 45 50 20 33 3a 20 20 0a 20 20 2a 2a 20 4c 6f  TEP 3:  .  ** Lo
89cf0 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 65  op through the e
89d00 6e 74 69 72 65 20 6d 65 6d 6f 72 79 20 70 6f 6f  ntire memory poo
89d10 6c 2e 20 20 43 6f 61 6c 65 73 63 65 20 61 64 6a  l.  Coalesce adj
89d20 61 63 65 6e 74 20 66 72 65 65 0a 20 20 2a 2a 20  acent free.  ** 
89d30 63 68 75 6e 6b 73 2e 20 20 52 65 63 6f 6d 70 75  chunks.  Recompu
89d40 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 63 68  te the master ch
89d50 75 6e 6b 20 61 73 20 74 68 65 20 6c 61 72 67 65  unk as the large
89d60 73 74 20 66 72 65 65 20 63 68 75 6e 6b 2e 0a 20  st free chunk.. 
89d70 20 2a 2a 20 54 68 65 6e 20 74 72 79 20 61 67 61   ** Then try aga
89d80 69 6e 20 74 6f 20 73 61 74 69 73 66 79 20 74 68  in to satisfy th
89d90 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 62 79 20  e allocation by 
89da0 63 61 72 76 69 6e 67 20 61 20 70 69 65 63 65 20  carving a piece 
89db0 6f 66 66 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20  off.  ** of the 
89dc0 65 6e 64 20 6f 66 20 74 68 65 20 6d 61 73 74 65  end of the maste
89dd0 72 20 63 68 75 6e 6b 2e 20 20 54 68 69 73 20 73  r chunk.  This s
89de0 74 65 70 20 68 61 70 70 65 6e 73 20 76 65 72 79  tep happens very
89df0 0a 20 20 2a 2a 20 72 61 72 65 6c 79 20 28 77 65  .  ** rarely (we
89e00 20 68 6f 70 65 21 29 0a 20 20 2a 2f 0a 20 20 66   hope!).  */.  f
89e10 6f 72 28 74 6f 46 72 65 65 3d 6e 42 6c 6f 63 6b  or(toFree=nBlock
89e20 2a 31 36 3b 20 74 6f 46 72 65 65 3c 28 6d 65 6d  *16; toFree<(mem
89e30 33 2e 6e 50 6f 6f 6c 2a 31 36 29 3b 20 74 6f 46  3.nPool*16); toF
89e40 72 65 65 20 2a 3d 20 32 29 7b 0a 20 20 20 20 6d  ree *= 2){.    m
89e50 65 6d 73 79 73 33 4f 75 74 4f 66 4d 65 6d 6f 72  emsys3OutOfMemor
89e60 79 28 74 6f 46 72 65 65 29 3b 0a 20 20 20 20 69  y(toFree);.    i
89e70 66 28 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20  f( mem3.iMaster 
89e80 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33  ){.      memsys3
89e90 4c 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65  Link(mem3.iMaste
89ea0 72 29 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 69  r);.      mem3.i
89eb0 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20  Master = 0;.    
89ec0 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20    mem3.szMaster 
89ed0 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66  = 0;.    }.    f
89ee0 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f 48 41 53 48  or(i=0; i<N_HASH
89ef0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 6d 65  ; i++){.      me
89f00 6d 73 79 73 33 4d 65 72 67 65 28 26 6d 65 6d 33  msys3Merge(&mem3
89f10 2e 61 69 48 61 73 68 5b 69 5d 29 3b 0a 20 20 20  .aiHash[i]);.   
89f20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20   }.    for(i=0; 
89f30 69 3c 4d 58 5f 53 4d 41 4c 4c 2d 31 3b 20 69 2b  i<MX_SMALL-1; i+
89f40 2b 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73  +){.      memsys
89f50 33 4d 65 72 67 65 28 26 6d 65 6d 33 2e 61 69 53  3Merge(&mem3.aiS
89f60 6d 61 6c 6c 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a  mall[i]);.    }.
89f70 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d      if( mem3.szM
89f80 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 6d  aster ){.      m
89f90 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 6d 65 6d  emsys3Unlink(mem
89fa0 33 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 20  3.iMaster);.    
89fb0 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61 73    if( mem3.szMas
89fc0 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20 29 7b 0a 20  ter>=nBlock ){. 
89fd0 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 6d 65         return me
89fe0 6d 73 79 73 33 46 72 6f 6d 4d 61 73 74 65 72 28  msys3FromMaster(
89ff0 6e 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d  nBlock);.      }
8a000 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
8a010 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20   If none of the 
8a020 61 62 6f 76 65 20 77 6f 72 6b 65 64 2c 20 74 68  above worked, th
8a030 65 6e 20 77 65 20 66 61 69 6c 2e 20 2a 2f 0a 20  en we fail. */. 
8a040 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
8a050 0a 2a 2a 20 46 72 65 65 20 61 6e 20 6f 75 74 73  .** Free an outs
8a060 74 61 6e 64 69 6e 67 20 6d 65 6d 6f 72 79 20 61  tanding memory a
8a070 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  llocation..**.**
8a080 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61   This function a
8a090 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 20  ssumes that the 
8a0a0 6e 65 63 65 73 73 61 72 79 20 6d 75 74 65 78 65  necessary mutexe
8a0b0 73 2c 20 69 66 20 61 6e 79 2c 20 61 72 65 0a 2a  s, if any, are.*
8a0c0 2a 20 61 6c 72 65 61 64 79 20 68 65 6c 64 20 62  * already held b
8a0d0 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 48 65  y the caller. He
8a0e0 6e 63 65 20 22 55 6e 73 61 66 65 22 2e 0a 2a 2f  nce "Unsafe"..*/
8a0f0 0a 76 6f 69 64 20 6d 65 6d 73 79 73 33 46 72 65  .void memsys3Fre
8a100 65 55 6e 73 61 66 65 28 76 6f 69 64 20 2a 70 4f  eUnsafe(void *pO
8a110 6c 64 29 7b 0a 20 20 4d 65 6d 33 42 6c 6f 63 6b  ld){.  Mem3Block
8a120 20 2a 70 20 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b   *p = (Mem3Block
8a130 2a 29 70 4f 6c 64 3b 0a 20 20 69 6e 74 20 69 3b  *)pOld;.  int i;
8a140 0a 20 20 75 33 32 20 73 69 7a 65 2c 20 78 3b 0a  .  u32 size, x;.
8a150 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
8a160 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d  3_mutex_held(mem
8a170 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  3.mutex) );.  as
8a180 73 65 72 74 28 20 70 3e 6d 65 6d 33 2e 61 50 6f  sert( p>mem3.aPo
8a190 6f 6c 20 26 26 20 70 3c 26 6d 65 6d 33 2e 61 50  ol && p<&mem3.aP
8a1a0 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 20  ool[mem3.nPool] 
8a1b0 29 3b 0a 20 20 69 20 3d 20 70 20 2d 20 6d 65 6d  );.  i = p - mem
8a1c0 33 2e 61 50 6f 6f 6c 3b 0a 20 20 61 73 73 65 72  3.aPool;.  asser
8a1d0 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  t( (mem3.aPool[i
8a1e0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
8a1f0 26 31 29 3d 3d 31 20 29 3b 0a 20 20 73 69 7a 65  &1)==1 );.  size
8a200 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d   = mem3.aPool[i-
8a210 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f  1].u.hdr.size4x/
8a220 34 3b 0a 20 20 61 73 73 65 72 74 28 20 69 2b 73  4;.  assert( i+s
8a230 69 7a 65 3c 3d 6d 65 6d 33 2e 6e 50 6f 6f 6c 2b  ize<=mem3.nPool+
8a240 31 20 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f  1 );.  mem3.aPoo
8a250 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  l[i-1].u.hdr.siz
8a260 65 34 78 20 26 3d 20 7e 31 3b 0a 20 20 6d 65 6d  e4x &= ~1;.  mem
8a270 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31  3.aPool[i+size-1
8a280 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65  ].u.hdr.prevSize
8a290 20 3d 20 73 69 7a 65 3b 0a 20 20 6d 65 6d 33 2e   = size;.  mem3.
8a2a0 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e  aPool[i+size-1].
8a2b0 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 3d 20  u.hdr.size4x &= 
8a2c0 7e 32 3b 0a 20 20 6d 65 6d 73 79 73 33 4c 69 6e  ~2;.  memsys3Lin
8a2d0 6b 28 69 29 3b 0a 0a 20 20 2f 2a 20 54 72 79 20  k(i);..  /* Try 
8a2e0 74 6f 20 65 78 70 61 6e 64 20 74 68 65 20 6d 61  to expand the ma
8a2f0 73 74 65 72 20 75 73 69 6e 67 20 74 68 65 20 6e  ster using the n
8a300 65 77 6c 79 20 66 72 65 65 64 20 63 68 75 6e 6b  ewly freed chunk
8a310 20 2a 2f 0a 20 20 69 66 28 20 6d 65 6d 33 2e 69   */.  if( mem3.i
8a320 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 77 68  Master ){.    wh
8a330 69 6c 65 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c  ile( (mem3.aPool
8a340 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d  [mem3.iMaster-1]
8a350 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 32 29  .u.hdr.size4x&2)
8a360 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 69 7a  ==0 ){.      siz
8a370 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d  e = mem3.aPool[m
8a380 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75  em3.iMaster-1].u
8a390 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 3b 0a 20  .hdr.prevSize;. 
8a3a0 20 20 20 20 20 6d 65 6d 33 2e 69 4d 61 73 74 65       mem3.iMaste
8a3b0 72 20 2d 3d 20 73 69 7a 65 3b 0a 20 20 20 20 20  r -= size;.     
8a3c0 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2b   mem3.szMaster +
8a3d0 3d 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65  = size;.      me
8a3e0 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 6d 65 6d 33  msys3Unlink(mem3
8a3f0 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20  .iMaster);.     
8a400 20 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b   x = mem3.aPool[
8a410 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e  mem3.iMaster-1].
8a420 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 32  u.hdr.size4x & 2
8a430 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50 6f  ;.      mem3.aPo
8a440 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d  ol[mem3.iMaster-
8a450 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20  1].u.hdr.size4x 
8a460 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2a  = mem3.szMaster*
8a470 34 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d 65 6d  4 | x;.      mem
8a480 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61  3.aPool[mem3.iMa
8a490 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74  ster+mem3.szMast
8a4a0 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76  er-1].u.hdr.prev
8a4b0 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61  Size = mem3.szMa
8a4c0 73 74 65 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ster;.    }.    
8a4d0 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d  x = mem3.aPool[m
8a4e0 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75  em3.iMaster-1].u
8a4f0 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 32 3b  .hdr.size4x & 2;
8a500 0a 20 20 20 20 77 68 69 6c 65 28 20 28 6d 65 6d  .    while( (mem
8a510 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61  3.aPool[mem3.iMa
8a520 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74  ster+mem3.szMast
8a530 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65  er-1].u.hdr.size
8a540 34 78 26 31 29 3d 3d 30 20 29 7b 0a 20 20 20 20  4x&1)==0 ){.    
8a550 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28    memsys3Unlink(
8a560 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d  mem3.iMaster+mem
8a570 33 2e 73 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20  3.szMaster);.   
8a580 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72     mem3.szMaster
8a590 20 2b 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d   += mem3.aPool[m
8a5a0 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33  em3.iMaster+mem3
8a5b0 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68  .szMaster-1].u.h
8a5c0 64 72 2e 73 69 7a 65 34 78 2f 34 3b 0a 20 20 20  dr.size4x/4;.   
8a5d0 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65     mem3.aPool[me
8a5e0 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e  m3.iMaster-1].u.
8a5f0 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6d 65 6d  hdr.size4x = mem
8a600 33 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c 20 78  3.szMaster*4 | x
8a610 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50 6f  ;.      mem3.aPo
8a620 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b  ol[mem3.iMaster+
8a630 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d  mem3.szMaster-1]
8a640 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20  .u.hdr.prevSize 
8a650 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3b  = mem3.szMaster;
8a660 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
8a670 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73  .** Return the s
8a680 69 7a 65 20 6f 66 20 61 6e 20 6f 75 74 73 74 61  ize of an outsta
8a690 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e  nding allocation
8a6a0 2c 20 69 6e 20 62 79 74 65 73 2e 20 20 54 68 65  , in bytes.  The
8a6b0 0a 2a 2a 20 73 69 7a 65 20 72 65 74 75 72 6e 65  .** size returne
8a6c0 64 20 6f 6d 69 74 73 20 74 68 65 20 38 2d 62 79  d omits the 8-by
8a6d0 74 65 20 68 65 61 64 65 72 20 6f 76 65 72 68 65  te header overhe
8a6e0 61 64 2e 20 20 54 68 69 73 20 6f 6e 6c 79 0a 2a  ad.  This only.*
8a6f0 2a 20 77 6f 72 6b 73 20 66 6f 72 20 63 68 75 6e  * works for chun
8a700 6b 73 20 74 68 61 74 20 61 72 65 20 63 75 72 72  ks that are curr
8a710 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 6f 75  ently checked ou
8a720 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t..*/.static int
8a730 20 6d 65 6d 73 79 73 33 53 69 7a 65 28 76 6f 69   memsys3Size(voi
8a740 64 20 2a 70 29 7b 0a 20 20 4d 65 6d 33 42 6c 6f  d *p){.  Mem3Blo
8a750 63 6b 20 2a 70 42 6c 6f 63 6b 3b 0a 20 20 69 66  ck *pBlock;.  if
8a760 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ( p==0 ) return 
8a770 30 3b 0a 20 20 70 42 6c 6f 63 6b 20 3d 20 28 4d  0;.  pBlock = (M
8a780 65 6d 33 42 6c 6f 63 6b 2a 29 70 3b 0a 20 20 61  em3Block*)p;.  a
8a790 73 73 65 72 74 28 20 28 70 42 6c 6f 63 6b 5b 2d  ssert( (pBlock[-
8a7a0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26  1].u.hdr.size4x&
8a7b0 31 29 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72  1)!=0 );.  retur
8a7c0 6e 20 28 70 42 6c 6f 63 6b 5b 2d 31 5d 2e 75 2e  n (pBlock[-1].u.
8a7d0 68 64 72 2e 73 69 7a 65 34 78 26 7e 33 29 2a 32  hdr.size4x&~3)*2
8a7e0 20 2d 20 34 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52   - 4;.}../*.** R
8a7f0 6f 75 6e 64 20 75 70 20 61 20 72 65 71 75 65 73  ound up a reques
8a800 74 20 73 69 7a 65 20 74 6f 20 74 68 65 20 6e 65  t size to the ne
8a810 78 74 20 76 61 6c 69 64 20 61 6c 6c 6f 63 61 74  xt valid allocat
8a820 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61  ion size..*/.sta
8a830 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 33 52  tic int memsys3R
8a840 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20  oundup(int n){. 
8a850 20 69 66 28 20 6e 3c 3d 31 32 20 29 7b 0a 20 20   if( n<=12 ){.  
8a860 20 20 72 65 74 75 72 6e 20 31 32 3b 0a 20 20 7d    return 12;.  }
8a870 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e  else{.    return
8a880 20 28 28 6e 2b 31 31 29 26 7e 37 29 20 2d 20 34   ((n+11)&~7) - 4
8a890 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  ;.  }.}../*.** A
8a8a0 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 73 20 6f  llocate nBytes o
8a8b0 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61  f memory..*/.sta
8a8c0 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73  tic void *memsys
8a8d0 33 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74  3Malloc(int nByt
8a8e0 65 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69  es){.  sqlite3_i
8a8f0 6e 74 36 34 20 2a 70 3b 0a 20 20 61 73 73 65 72  nt64 *p;.  asser
8a900 74 28 20 6e 42 79 74 65 73 3e 30 20 29 3b 20 20  t( nBytes>0 );  
8a910 20 20 20 20 20 20 20 20 2f 2a 20 6d 61 6c 6c 6f          /* mallo
8a920 63 2e 63 20 66 69 6c 74 65 72 73 20 6f 75 74 20  c.c filters out 
8a930 30 20 62 79 74 65 20 72 65 71 75 65 73 74 73 20  0 byte requests 
8a940 2a 2f 0a 20 20 6d 65 6d 73 79 73 33 45 6e 74 65  */.  memsys3Ente
8a950 72 28 29 3b 0a 20 20 70 20 3d 20 6d 65 6d 73 79  r();.  p = memsy
8a960 73 33 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e  s3MallocUnsafe(n
8a970 42 79 74 65 73 29 3b 0a 20 20 6d 65 6d 73 79 73  Bytes);.  memsys
8a980 33 4c 65 61 76 65 28 29 3b 0a 20 20 72 65 74 75  3Leave();.  retu
8a990 72 6e 20 28 76 6f 69 64 2a 29 70 3b 20 0a 7d 0a  rn (void*)p; .}.
8a9a0 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f  ./*.** Free memo
8a9b0 72 79 2e 0a 2a 2f 0a 76 6f 69 64 20 6d 65 6d 73  ry..*/.void mems
8a9c0 79 73 33 46 72 65 65 28 76 6f 69 64 20 2a 70 50  ys3Free(void *pP
8a9d0 72 69 6f 72 29 7b 0a 20 20 61 73 73 65 72 74 28  rior){.  assert(
8a9e0 20 70 50 72 69 6f 72 20 29 3b 0a 20 20 6d 65 6d   pPrior );.  mem
8a9f0 73 79 73 33 45 6e 74 65 72 28 29 3b 0a 20 20 6d  sys3Enter();.  m
8aa00 65 6d 73 79 73 33 46 72 65 65 55 6e 73 61 66 65  emsys3FreeUnsafe
8aa10 28 70 50 72 69 6f 72 29 3b 0a 20 20 6d 65 6d 73  (pPrior);.  mems
8aa20 79 73 33 4c 65 61 76 65 28 29 3b 0a 7d 0a 0a 2f  ys3Leave();.}../
8aa30 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
8aa40 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69 73 74  size of an exist
8aa50 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  ing memory alloc
8aa60 61 74 69 6f 6e 0a 2a 2f 0a 76 6f 69 64 20 2a 6d  ation.*/.void *m
8aa70 65 6d 73 79 73 33 52 65 61 6c 6c 6f 63 28 76 6f  emsys3Realloc(vo
8aa80 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20  id *pPrior, int 
8aa90 6e 42 79 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e  nBytes){.  int n
8aaa0 4f 6c 64 3b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a  Old;.  void *p;.
8aab0 20 20 69 66 28 20 70 50 72 69 6f 72 3d 3d 30 20    if( pPrior==0 
8aac0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71  ){.    return sq
8aad0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 42 79  lite3_malloc(nBy
8aae0 74 65 73 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  tes);.  }.  if( 
8aaf0 6e 42 79 74 65 73 3c 3d 30 20 29 7b 0a 20 20 20  nBytes<=0 ){.   
8ab00 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50   sqlite3_free(pP
8ab10 72 69 6f 72 29 3b 0a 20 20 20 20 72 65 74 75 72  rior);.    retur
8ab20 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 4f 6c 64 20  n 0;.  }.  nOld 
8ab30 3d 20 6d 65 6d 73 79 73 33 53 69 7a 65 28 70 50  = memsys3Size(pP
8ab40 72 69 6f 72 29 3b 0a 20 20 69 66 28 20 6e 42 79  rior);.  if( nBy
8ab50 74 65 73 3c 3d 6e 4f 6c 64 20 26 26 20 6e 42 79  tes<=nOld && nBy
8ab60 74 65 73 3e 3d 6e 4f 6c 64 2d 31 32 38 20 29 7b  tes>=nOld-128 ){
8ab70 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50 72 69  .    return pPri
8ab80 6f 72 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73  or;.  }.  memsys
8ab90 33 45 6e 74 65 72 28 29 3b 0a 20 20 70 20 3d 20  3Enter();.  p = 
8aba0 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 6e 73  memsys3MallocUns
8abb0 61 66 65 28 6e 42 79 74 65 73 29 3b 0a 20 20 69  afe(nBytes);.  i
8abc0 66 28 20 70 20 29 7b 0a 20 20 20 20 69 66 28 20  f( p ){.    if( 
8abd0 6e 4f 6c 64 3c 6e 42 79 74 65 73 20 29 7b 0a 20  nOld<nBytes ){. 
8abe0 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70       memcpy(p, p
8abf0 50 72 69 6f 72 2c 20 6e 4f 6c 64 29 3b 0a 20 20  Prior, nOld);.  
8ac00 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d    }else{.      m
8ac10 65 6d 63 70 79 28 70 2c 20 70 50 72 69 6f 72 2c  emcpy(p, pPrior,
8ac20 20 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 7d 0a   nBytes);.    }.
8ac30 20 20 20 20 6d 65 6d 73 79 73 33 46 72 65 65 55      memsys3FreeU
8ac40 6e 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0a 20  nsafe(pPrior);. 
8ac50 20 7d 0a 20 20 6d 65 6d 73 79 73 33 4c 65 61 76   }.  memsys3Leav
8ac60 65 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b  e();.  return p;
8ac70 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61  .}../*.** Initia
8ac80 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c 65  lize this module
8ac90 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
8aca0 6d 65 6d 73 79 73 33 49 6e 69 74 28 76 6f 69 64  memsys3Init(void
8acb0 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e   *NotUsed){.  UN
8acc0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
8acd0 6f 74 55 73 65 64 29 3b 0a 20 20 69 66 28 20 21  otUsed);.  if( !
8ace0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
8acf0 66 69 67 2e 70 48 65 61 70 20 29 7b 0a 20 20 20  fig.pHeap ){.   
8ad00 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
8ad10 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  RROR;.  }..  /* 
8ad20 53 74 6f 72 65 20 61 20 70 6f 69 6e 74 65 72 20  Store a pointer 
8ad30 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 62 6c  to the memory bl
8ad40 6f 63 6b 20 69 6e 20 67 6c 6f 62 61 6c 20 73 74  ock in global st
8ad50 72 75 63 74 75 72 65 20 6d 65 6d 33 2e 20 2a 2f  ructure mem3. */
8ad60 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f  .  assert( sizeo
8ad70 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 3d 3d 38 20  f(Mem3Block)==8 
8ad80 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 20  );.  mem3.aPool 
8ad90 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b 20 2a 29 73  = (Mem3Block *)s
8ada0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
8adb0 69 67 2e 70 48 65 61 70 3b 0a 20 20 6d 65 6d 33  ig.pHeap;.  mem3
8adc0 2e 6e 50 6f 6f 6c 20 3d 20 28 73 71 6c 69 74 65  .nPool = (sqlite
8add0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 48  3GlobalConfig.nH
8ade0 65 61 70 20 2f 20 73 69 7a 65 6f 66 28 4d 65 6d  eap / sizeof(Mem
8adf0 33 42 6c 6f 63 6b 29 29 20 2d 20 32 3b 0a 0a 20  3Block)) - 2;.. 
8ae00 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74   /* Initialize t
8ae10 68 65 20 6d 61 73 74 65 72 20 62 6c 6f 63 6b 2e  he master block.
8ae20 20 2a 2f 0a 20 20 6d 65 6d 33 2e 73 7a 4d 61 73   */.  mem3.szMas
8ae30 74 65 72 20 3d 20 6d 65 6d 33 2e 6e 50 6f 6f 6c  ter = mem3.nPool
8ae40 3b 0a 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65  ;.  mem3.mnMaste
8ae50 72 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65  r = mem3.szMaste
8ae60 72 3b 0a 20 20 6d 65 6d 33 2e 69 4d 61 73 74 65  r;.  mem3.iMaste
8ae70 72 20 3d 20 31 3b 0a 20 20 6d 65 6d 33 2e 61 50  r = 1;.  mem3.aP
8ae80 6f 6f 6c 5b 30 5d 2e 75 2e 68 64 72 2e 73 69 7a  ool[0].u.hdr.siz
8ae90 65 34 78 20 3d 20 28 6d 65 6d 33 2e 73 7a 4d 61  e4x = (mem3.szMa
8aea0 73 74 65 72 3c 3c 32 29 20 2b 20 32 3b 0a 20 20  ster<<2) + 2;.  
8aeb0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e  mem3.aPool[mem3.
8aec0 6e 50 6f 6f 6c 5d 2e 75 2e 68 64 72 2e 70 72 65  nPool].u.hdr.pre
8aed0 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 6e 50 6f  vSize = mem3.nPo
8aee0 6f 6c 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c  ol;.  mem3.aPool
8aef0 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 2e 75 2e 68  [mem3.nPool].u.h
8af00 64 72 2e 73 69 7a 65 34 78 20 3d 20 31 3b 0a 0a  dr.size4x = 1;..
8af10 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
8af20 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69  OK;.}../*.** Dei
8af30 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20 6d  nitialize this m
8af40 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  odule..*/.static
8af50 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 53 68 75   void memsys3Shu
8af60 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55  tdown(void *NotU
8af70 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50  sed){.  UNUSED_P
8af80 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
8af90 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a  );.  return;.}..
8afa0 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65  ../*.** Open the
8afb0 20 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64 20   file indicated 
8afc0 61 6e 64 20 77 72 69 74 65 20 61 20 6c 6f 67 20  and write a log 
8afd0 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d  of all unfreed m
8afe0 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63 61  emory .** alloca
8aff0 74 69 6f 6e 73 20 69 6e 74 6f 20 74 68 61 74 20  tions into that 
8b000 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  log..*/.SQLITE_P
8b010 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
8b020 74 65 33 4d 65 6d 73 79 73 33 44 75 6d 70 28 63  te3Memsys3Dump(c
8b030 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
8b040 6e 61 6d 65 29 7b 0a 23 69 66 64 65 66 20 53 51  name){.#ifdef SQ
8b050 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 46 49 4c  LITE_DEBUG.  FIL
8b060 45 20 2a 6f 75 74 3b 0a 20 20 75 33 32 20 69 2c  E *out;.  u32 i,
8b070 20 6a 3b 0a 20 20 75 33 32 20 73 69 7a 65 3b 0a   j;.  u32 size;.
8b080 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d    if( zFilename=
8b090 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e 61 6d 65 5b  =0 || zFilename[
8b0a0 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 6f 75 74  0]==0 ){.    out
8b0b0 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20 7d 65 6c   = stdout;.  }el
8b0c0 73 65 7b 0a 20 20 20 20 6f 75 74 20 3d 20 66 6f  se{.    out = fo
8b0d0 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 22  pen(zFilename, "
8b0e0 77 22 29 3b 0a 20 20 20 20 69 66 28 20 6f 75 74  w");.    if( out
8b0f0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 70 72  ==0 ){.      fpr
8b100 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 2a 2a  intf(stderr, "**
8b110 20 55 6e 61 62 6c 65 20 74 6f 20 6f 75 74 70 75   Unable to outpu
8b120 74 20 6d 65 6d 6f 72 79 20 64 65 62 75 67 20 6f  t memory debug o
8b130 75 74 70 75 74 20 6c 6f 67 3a 20 25 73 20 2a 2a  utput log: %s **
8b140 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \n",.           
8b150 20 20 20 20 20 20 20 20 20 20 20 7a 46 69 6c 65             zFile
8b160 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 65 74  name);.      ret
8b170 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  urn;.    }.  }. 
8b180 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 29 3b   memsys3Enter();
8b190 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20  .  fprintf(out, 
8b1a0 22 43 48 55 4e 4b 53 3a 5c 6e 22 29 3b 0a 20 20  "CHUNKS:\n");.  
8b1b0 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 6d 65 6d 33  for(i=1; i<=mem3
8b1c0 2e 6e 50 6f 6f 6c 3b 20 69 2b 3d 73 69 7a 65 2f  .nPool; i+=size/
8b1d0 34 29 7b 0a 20 20 20 20 73 69 7a 65 20 3d 20 6d  4){.    size = m
8b1e0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75  em3.aPool[i-1].u
8b1f0 2e 68 64 72 2e 73 69 7a 65 34 78 3b 0a 20 20 20  .hdr.size4x;.   
8b200 20 69 66 28 20 73 69 7a 65 2f 34 3c 3d 31 20 29   if( size/4<=1 )
8b210 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28  {.      fprintf(
8b220 6f 75 74 2c 20 22 25 70 20 73 69 7a 65 20 65 72  out, "%p size er
8b230 72 6f 72 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50  ror\n", &mem3.aP
8b240 6f 6f 6c 5b 69 5d 29 3b 0a 20 20 20 20 20 20 61  ool[i]);.      a
8b250 73 73 65 72 74 28 20 30 20 29 3b 0a 20 20 20 20  ssert( 0 );.    
8b260 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
8b270 20 20 20 69 66 28 20 28 73 69 7a 65 26 31 29 3d     if( (size&1)=
8b280 3d 30 20 26 26 20 6d 65 6d 33 2e 61 50 6f 6f 6c  =0 && mem3.aPool
8b290 5b 69 2b 73 69 7a 65 2f 34 2d 31 5d 2e 75 2e 68  [i+size/4-1].u.h
8b2a0 64 72 2e 70 72 65 76 53 69 7a 65 21 3d 73 69 7a  dr.prevSize!=siz
8b2b0 65 2f 34 20 29 7b 0a 20 20 20 20 20 20 66 70 72  e/4 ){.      fpr
8b2c0 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 74 61  intf(out, "%p ta
8b2d0 69 6c 20 73 69 7a 65 20 64 6f 65 73 20 6e 6f 74  il size does not
8b2e0 20 6d 61 74 63 68 5c 6e 22 2c 20 26 6d 65 6d 33   match\n", &mem3
8b2f0 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a 20 20 20 20  .aPool[i]);.    
8b300 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a 20    assert( 0 );. 
8b310 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
8b320 7d 0a 20 20 20 20 69 66 28 20 28 28 6d 65 6d 33  }.    if( ((mem3
8b330 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2f 34 2d  .aPool[i+size/4-
8b340 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26  1].u.hdr.size4x&
8b350 32 29 3e 3e 31 29 21 3d 28 73 69 7a 65 26 31 29  2)>>1)!=(size&1)
8b360 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74   ){.      fprint
8b370 66 28 6f 75 74 2c 20 22 25 70 20 74 61 69 6c 20  f(out, "%p tail 
8b380 63 68 65 63 6b 6f 75 74 20 62 69 74 20 69 73 20  checkout bit is 
8b390 69 6e 63 6f 72 72 65 63 74 5c 6e 22 2c 20 26 6d  incorrect\n", &m
8b3a0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a 20  em3.aPool[i]);. 
8b3b0 20 20 20 20 20 61 73 73 65 72 74 28 20 30 20 29       assert( 0 )
8b3c0 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
8b3d0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 69 7a     }.    if( siz
8b3e0 65 26 31 20 29 7b 0a 20 20 20 20 20 20 66 70 72  e&1 ){.      fpr
8b3f0 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 25 36  intf(out, "%p %6
8b400 64 20 62 79 74 65 73 20 63 68 65 63 6b 65 64 20  d bytes checked 
8b410 6f 75 74 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50  out\n", &mem3.aP
8b420 6f 6f 6c 5b 69 5d 2c 20 28 73 69 7a 65 2f 34 29  ool[i], (size/4)
8b430 2a 38 2d 38 29 3b 0a 20 20 20 20 7d 65 6c 73 65  *8-8);.    }else
8b440 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28  {.      fprintf(
8b450 6f 75 74 2c 20 22 25 70 20 25 36 64 20 62 79 74  out, "%p %6d byt
8b460 65 73 20 66 72 65 65 25 73 5c 6e 22 2c 20 26 6d  es free%s\n", &m
8b470 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2c 20 28 73  em3.aPool[i], (s
8b480 69 7a 65 2f 34 29 2a 38 2d 38 2c 0a 20 20 20 20  ize/4)*8-8,.    
8b490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 3d                i=
8b4a0 3d 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3f 20  =mem3.iMaster ? 
8b4b0 22 20 2a 2a 6d 61 73 74 65 72 2a 2a 22 20 3a 20  " **master**" : 
8b4c0 22 22 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  "");.    }.  }. 
8b4d0 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4d 58 5f 53   for(i=0; i<MX_S
8b4e0 4d 41 4c 4c 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20  MALL-1; i++){.  
8b4f0 20 20 69 66 28 20 6d 65 6d 33 2e 61 69 53 6d 61    if( mem3.aiSma
8b500 6c 6c 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69  ll[i]==0 ) conti
8b510 6e 75 65 3b 0a 20 20 20 20 66 70 72 69 6e 74 66  nue;.    fprintf
8b520 28 6f 75 74 2c 20 22 73 6d 61 6c 6c 28 25 32 64  (out, "small(%2d
8b530 29 3a 22 2c 20 69 29 3b 0a 20 20 20 20 66 6f 72  ):", i);.    for
8b540 28 6a 20 3d 20 6d 65 6d 33 2e 61 69 53 6d 61 6c  (j = mem3.aiSmal
8b550 6c 5b 69 5d 3b 20 6a 3e 30 3b 20 6a 3d 6d 65 6d  l[i]; j>0; j=mem
8b560 33 2e 61 50 6f 6f 6c 5b 6a 5d 2e 75 2e 6c 69 73  3.aPool[j].u.lis
8b570 74 2e 6e 65 78 74 29 7b 0a 20 20 20 20 20 20 66  t.next){.      f
8b580 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 25 70  printf(out, " %p
8b590 28 25 64 29 22 2c 20 26 6d 65 6d 33 2e 61 50 6f  (%d)", &mem3.aPo
8b5a0 6f 6c 5b 6a 5d 2c 0a 20 20 20 20 20 20 20 20 20  ol[j],.         
8b5b0 20 20 20 20 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c       (mem3.aPool
8b5c0 5b 6a 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65  [j-1].u.hdr.size
8b5d0 34 78 2f 34 29 2a 38 2d 38 29 3b 0a 20 20 20 20  4x/4)*8-8);.    
8b5e0 7d 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75  }.    fprintf(ou
8b5f0 74 2c 20 22 5c 6e 22 29 3b 20 0a 20 20 7d 0a 20  t, "\n"); .  }. 
8b600 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f 48 41   for(i=0; i<N_HA
8b610 53 48 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  SH; i++){.    if
8b620 28 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69 5d  ( mem3.aiHash[i]
8b630 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ==0 ) continue;.
8b640 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c      fprintf(out,
8b650 20 22 68 61 73 68 28 25 32 64 29 3a 22 2c 20 69   "hash(%2d):", i
8b660 29 3b 0a 20 20 20 20 66 6f 72 28 6a 20 3d 20 6d  );.    for(j = m
8b670 65 6d 33 2e 61 69 48 61 73 68 5b 69 5d 3b 20 6a  em3.aiHash[i]; j
8b680 3e 30 3b 20 6a 3d 6d 65 6d 33 2e 61 50 6f 6f 6c  >0; j=mem3.aPool
8b690 5b 6a 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29  [j].u.list.next)
8b6a0 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28  {.      fprintf(
8b6b0 6f 75 74 2c 20 22 20 25 70 28 25 64 29 22 2c 20  out, " %p(%d)", 
8b6c0 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2c 0a  &mem3.aPool[j],.
8b6d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 6d                (m
8b6e0 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 2d 31 5d 2e 75  em3.aPool[j-1].u
8b6f0 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 29 2a 38  .hdr.size4x/4)*8
8b700 2d 38 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66  -8);.    }.    f
8b710 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22  printf(out, "\n"
8b720 29 3b 20 0a 20 20 7d 0a 20 20 66 70 72 69 6e 74  ); .  }.  fprint
8b730 66 28 6f 75 74 2c 20 22 6d 61 73 74 65 72 3d 25  f(out, "master=%
8b740 64 5c 6e 22 2c 20 6d 65 6d 33 2e 69 4d 61 73 74  d\n", mem3.iMast
8b750 65 72 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f  er);.  fprintf(o
8b760 75 74 2c 20 22 6e 6f 77 55 73 65 64 3d 25 64 5c  ut, "nowUsed=%d\
8b770 6e 22 2c 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 38  n", mem3.nPool*8
8b780 20 2d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72   - mem3.szMaster
8b790 2a 38 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f  *8);.  fprintf(o
8b7a0 75 74 2c 20 22 6d 78 55 73 65 64 3d 25 64 5c 6e  ut, "mxUsed=%d\n
8b7b0 22 2c 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 38 20  ", mem3.nPool*8 
8b7c0 2d 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 2a  - mem3.mnMaster*
8b7d0 38 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  8);.  sqlite3_mu
8b7e0 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 33 2e 6d  tex_leave(mem3.m
8b7f0 75 74 65 78 29 3b 0a 20 20 69 66 28 20 6f 75 74  utex);.  if( out
8b800 3d 3d 73 74 64 6f 75 74 20 29 7b 0a 20 20 20 20  ==stdout ){.    
8b810 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29 3b 0a  fflush(stdout);.
8b820 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 63 6c    }else{.    fcl
8b830 6f 73 65 28 6f 75 74 29 3b 0a 20 20 7d 0a 23 65  ose(out);.  }.#e
8b840 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  lse.  UNUSED_PAR
8b850 41 4d 45 54 45 52 28 7a 46 69 6c 65 6e 61 6d 65  AMETER(zFilename
8b860 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a  );.#endif.}../*.
8b870 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
8b880 69 73 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74  is the only rout
8b890 69 6e 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65  ine in this file
8b8a0 20 77 69 74 68 20 65 78 74 65 72 6e 61 6c 20 0a   with external .
8b8b0 2a 2a 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a  ** linkage..**.*
8b8c0 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 6c  * Populate the l
8b8d0 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20  ow-level memory 
8b8e0 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74  allocation funct
8b8f0 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a  ion pointers in.
8b900 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  ** sqlite3Global
8b910 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 6f  Config.m with po
8b920 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72 6f  inters to the ro
8b930 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66  utines in this f
8b940 69 6c 65 2e 20 54 68 65 0a 2a 2a 20 61 72 67 75  ile. The.** argu
8b950 6d 65 6e 74 73 20 73 70 65 63 69 66 79 20 74 68  ments specify th
8b960 65 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72  e block of memor
8b970 79 20 74 6f 20 6d 61 6e 61 67 65 2e 0a 2a 2a 0a  y to manage..**.
8b980 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
8b990 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 62  is only called b
8b9a0 79 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67  y sqlite3_config
8b9b0 28 29 2c 20 61 6e 64 20 74 68 65 72 65 66 6f 72  (), and therefor
8b9c0 65 0a 2a 2a 20 69 73 20 6e 6f 74 20 72 65 71 75  e.** is not requ
8b9d0 69 72 65 64 20 74 6f 20 62 65 20 74 68 72 65 61  ired to be threa
8b9e0 64 73 61 66 65 20 28 69 74 20 69 73 20 6e 6f 74  dsafe (it is not
8b9f0 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
8ba00 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74  VATE const sqlit
8ba10 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a  e3_mem_methods *
8ba20 73 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d  sqlite3MemGetMem
8ba30 73 79 73 33 28 76 6f 69 64 29 7b 0a 20 20 73 74  sys3(void){.  st
8ba40 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74  atic const sqlit
8ba50 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6d  e3_mem_methods m
8ba60 65 6d 70 6f 6f 6c 4d 65 74 68 6f 64 73 20 3d 20  empoolMethods = 
8ba70 7b 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 4d 61  {.     memsys3Ma
8ba80 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79  lloc,.     memsy
8ba90 73 33 46 72 65 65 2c 0a 20 20 20 20 20 6d 65 6d  s3Free,.     mem
8baa0 73 79 73 33 52 65 61 6c 6c 6f 63 2c 0a 20 20 20  sys3Realloc,.   
8bab0 20 20 6d 65 6d 73 79 73 33 53 69 7a 65 2c 0a 20    memsys3Size,. 
8bac0 20 20 20 20 6d 65 6d 73 79 73 33 52 6f 75 6e 64      memsys3Round
8bad0 75 70 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 33  up,.     memsys3
8bae0 49 6e 69 74 2c 0a 20 20 20 20 20 6d 65 6d 73 79  Init,.     memsy
8baf0 73 33 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20  s3Shutdown,.    
8bb00 20 30 0a 20 20 7d 3b 0a 20 20 72 65 74 75 72 6e   0.  };.  return
8bb10 20 26 6d 65 6d 70 6f 6f 6c 4d 65 74 68 6f 64 73   &mempoolMethods
8bb20 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53  ;.}..#endif /* S
8bb30 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
8bb40 53 59 53 33 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  SYS3 */../******
8bb50 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
8bb60 6d 65 6d 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  mem3.c *********
8bb70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8bb80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8bb90 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
8bba0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
8bbb0 69 6c 65 20 6d 65 6d 35 2e 63 20 2a 2a 2a 2a 2a  ile mem5.c *****
8bbc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8bbd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8bbe0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
8bbf0 30 30 37 20 4f 63 74 6f 62 65 72 20 31 34 0a 2a  007 October 14.*
8bc00 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
8bc10 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
8bc20 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
8bc30 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
8bc40 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
8bc50 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
8bc60 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
8bc70 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
8bc80 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
8bc90 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
8bca0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
8bcb0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
8bcc0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
8bcd0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
8bce0 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
8bcf0 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
8bd00 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
8bd10 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
8bd20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8bd30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8bd40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8bd50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
8bd60 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
8bd70 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69  ins the C functi
8bd80 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  ons that impleme
8bd90 6e 74 20 61 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61  nt a memory.** a
8bda0 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73  llocation subsys
8bdb0 74 65 6d 20 66 6f 72 20 75 73 65 20 62 79 20 53  tem for use by S
8bdc0 51 4c 69 74 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68  QLite. .**.** Th
8bdd0 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68  is version of th
8bde0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
8bdf0 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 6f 6d  ion subsystem om
8be00 69 74 73 20 61 6c 6c 0a 2a 2a 20 75 73 65 20 6f  its all.** use o
8be10 66 20 6d 61 6c 6c 6f 63 28 29 2e 20 54 68 65 20  f malloc(). The 
8be20 61 70 70 6c 69 63 61 74 69 6f 6e 20 67 69 76 65  application give
8be30 73 20 53 51 4c 69 74 65 20 61 20 62 6c 6f 63 6b  s SQLite a block
8be40 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 65   of memory.** be
8be50 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73 71 6c  fore calling sql
8be60 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
8be70 29 20 66 72 6f 6d 20 77 68 69 63 68 20 61 6c 6c  ) from which all
8be80 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20  ocations.** are 
8be90 6d 61 64 65 20 61 6e 64 20 72 65 74 75 72 6e 65  made and returne
8bea0 64 20 62 79 20 74 68 65 20 78 4d 61 6c 6c 6f 63  d by the xMalloc
8beb0 28 29 20 61 6e 64 20 78 52 65 61 6c 6c 6f 63 28  () and xRealloc(
8bec0 29 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61  ) .** implementa
8bed0 74 69 6f 6e 73 2e 20 4f 6e 63 65 20 73 71 6c 69  tions. Once sqli
8bee0 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
8bef0 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64   has been called
8bf00 2c 0a 2a 2a 20 74 68 65 20 61 6d 6f 75 6e 74 20  ,.** the amount 
8bf10 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61 69 6c 61  of memory availa
8bf20 62 6c 65 20 74 6f 20 53 51 4c 69 74 65 20 69 73  ble to SQLite is
8bf30 20 66 69 78 65 64 20 61 6e 64 20 63 61 6e 6e 6f   fixed and canno
8bf40 74 0a 2a 2a 20 62 65 20 63 68 61 6e 67 65 64 2e  t.** be changed.
8bf50 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73  .**.** This vers
8bf60 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72  ion of the memor
8bf70 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62  y allocation sub
8bf80 73 79 73 74 65 6d 20 69 73 20 69 6e 63 6c 75 64  system is includ
8bf90 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 62 75 69  ed.** in the bui
8bfa0 6c 64 20 6f 6e 6c 79 20 69 66 20 53 51 4c 49 54  ld only if SQLIT
8bfb0 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35  E_ENABLE_MEMSYS5
8bfc0 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a   is defined..**.
8bfd0 2a 2a 20 54 68 69 73 20 6d 65 6d 6f 72 79 20 61  ** This memory a
8bfe0 6c 6c 6f 63 61 74 6f 72 20 75 73 65 73 20 74 68  llocator uses th
8bff0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 6c 67 6f  e following algo
8c000 72 69 74 68 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 31  rithm:.**.**   1
8c010 2e 20 20 41 6c 6c 20 6d 65 6d 6f 72 79 20 61 6c  .  All memory al
8c020 6c 6f 63 61 74 69 6f 6e 73 20 73 69 7a 65 73 20  locations sizes 
8c030 61 72 65 20 72 6f 75 6e 64 65 64 20 75 70 20 74  are rounded up t
8c040 6f 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2e 0a  o a power of 2..
8c050 2a 2a 0a 2a 2a 20 20 20 32 2e 20 20 49 66 20 74  **.**   2.  If t
8c060 77 6f 20 61 64 6a 61 63 65 6e 74 20 66 72 65 65  wo adjacent free
8c070 20 62 6c 6f 63 6b 73 20 61 72 65 20 74 68 65 20   blocks are the 
8c080 68 61 6c 76 65 73 20 6f 66 20 61 20 6c 61 72 67  halves of a larg
8c090 65 72 20 62 6c 6f 63 6b 2c 0a 2a 2a 20 20 20 20  er block,.**    
8c0a0 20 20 20 74 68 65 6e 20 74 68 65 20 74 77 6f 20     then the two 
8c0b0 62 6c 6f 63 6b 73 20 61 72 65 20 63 6f 61 6c 65  blocks are coale
8c0c0 73 65 64 20 69 6e 74 6f 20 74 68 65 20 73 69 6e  sed into the sin
8c0d0 67 6c 65 20 6c 61 72 67 65 72 20 62 6c 6f 63 6b  gle larger block
8c0e0 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 2e 20 20 4e 65  ..**.**   3.  Ne
8c0f0 77 20 6d 65 6d 6f 72 79 20 69 73 20 61 6c 6c 6f  w memory is allo
8c100 63 61 74 65 64 20 66 72 6f 6d 20 74 68 65 20 66  cated from the f
8c110 69 72 73 74 20 61 76 61 69 6c 61 62 6c 65 20 66  irst available f
8c120 72 65 65 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a  ree block..**.**
8c130 20 54 68 69 73 20 61 6c 67 6f 72 69 74 68 6d 20   This algorithm 
8c140 69 73 20 64 65 73 63 72 69 62 65 64 20 69 6e 3a  is described in:
8c150 20 4a 2e 20 4d 2e 20 52 6f 62 73 6f 6e 2e 20 22   J. M. Robson. "
8c160 42 6f 75 6e 64 73 20 66 6f 72 20 53 6f 6d 65 20  Bounds for Some 
8c170 46 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 43 6f 6e  Functions.** Con
8c180 63 65 72 6e 69 6e 67 20 44 79 6e 61 6d 69 63 20  cerning Dynamic 
8c190 53 74 6f 72 61 67 65 20 41 6c 6c 6f 63 61 74 69  Storage Allocati
8c1a0 6f 6e 22 2e 20 4a 6f 75 72 6e 61 6c 20 6f 66 20  on". Journal of 
8c1b0 74 68 65 20 41 73 73 6f 63 69 61 74 69 6f 6e 20  the Association 
8c1c0 66 6f 72 0a 2a 2a 20 43 6f 6d 70 75 74 69 6e 67  for.** Computing
8c1d0 20 4d 61 63 68 69 6e 65 72 79 2c 20 56 6f 6c 75   Machinery, Volu
8c1e0 6d 65 20 32 31 2c 20 4e 75 6d 62 65 72 20 38 2c  me 21, Number 8,
8c1f0 20 4a 75 6c 79 20 31 39 37 34 2c 20 70 61 67 65   July 1974, page
8c200 73 20 34 39 31 2d 34 39 39 2e 0a 2a 2a 20 0a 2a  s 491-499..** .*
8c210 2a 20 4c 65 74 20 6e 20 62 65 20 74 68 65 20 73  * Let n be the s
8c220 69 7a 65 20 6f 66 20 74 68 65 20 6c 61 72 67 65  ize of the large
8c230 73 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 69  st allocation di
8c240 76 69 64 65 64 20 62 79 20 74 68 65 20 6d 69 6e  vided by the min
8c250 69 6d 75 6d 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69  imum.** allocati
8c260 6f 6e 20 73 69 7a 65 20 28 61 66 74 65 72 20 72  on size (after r
8c270 6f 75 6e 64 69 6e 67 20 61 6c 6c 20 73 69 7a 65  ounding all size
8c280 73 20 75 70 20 74 6f 20 61 20 70 6f 77 65 72 20  s up to a power 
8c290 6f 66 20 32 2e 29 20 20 4c 65 74 20 4d 0a 2a 2a  of 2.)  Let M.**
8c2a0 20 62 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20   be the maximum 
8c2b0 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79  amount of memory
8c2c0 20 65 76 65 72 20 6f 75 74 73 74 61 6e 64 69 6e   ever outstandin
8c2d0 67 20 61 74 20 6f 6e 65 20 74 69 6d 65 2e 20 20  g at one time.  
8c2e0 4c 65 74 0a 2a 2a 20 4e 20 62 65 20 74 68 65 20  Let.** N be the 
8c2f0 74 6f 74 61 6c 20 61 6d 6f 75 6e 74 20 6f 66 20  total amount of 
8c300 6d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65  memory available
8c310 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e   for allocation.
8c320 20 20 52 6f 62 73 6f 6e 0a 2a 2a 20 70 72 6f 76    Robson.** prov
8c330 65 64 20 74 68 61 74 20 74 68 69 73 20 6d 65 6d  ed that this mem
8c340 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 77 69  ory allocator wi
8c350 6c 6c 20 6e 65 76 65 72 20 62 72 65 61 6b 64 6f  ll never breakdo
8c360 77 6e 20 64 75 65 20 74 6f 20 0a 2a 2a 20 66 72  wn due to .** fr
8c370 61 67 6d 65 6e 74 61 74 69 6f 6e 20 61 73 20 6c  agmentation as l
8c380 6f 6e 67 20 61 73 20 74 68 65 20 66 6f 6c 6c 6f  ong as the follo
8c390 77 69 6e 67 20 63 6f 6e 73 74 72 61 69 6e 74 20  wing constraint 
8c3a0 68 6f 6c 64 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  holds:.**.**    
8c3b0 20 20 4e 20 3e 3d 20 20 4d 2a 28 31 20 2b 20 6c    N >=  M*(1 + l
8c3c0 6f 67 32 28 6e 29 2f 32 29 20 2d 20 6e 20 2b 20  og2(n)/2) - n + 
8c3d0 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  1.**.** The sqli
8c3e0 74 65 33 5f 73 74 61 74 75 73 28 29 20 6c 6f 67  te3_status() log
8c3f0 69 63 20 74 72 61 63 6b 73 20 74 68 65 20 6d 61  ic tracks the ma
8c400 78 69 6d 75 6d 20 76 61 6c 75 65 73 20 6f 66 20  ximum values of 
8c410 6e 20 61 6e 64 20 4d 20 73 6f 0a 2a 2a 20 74 68  n and M so.** th
8c420 61 74 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f  at an applicatio
8c430 6e 20 63 61 6e 2c 20 61 74 20 61 6e 79 20 74 69  n can, at any ti
8c440 6d 65 2c 20 76 65 72 69 66 79 20 74 68 69 73 20  me, verify this 
8c450 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 2a 2f 0a 0a  constraint..*/..
8c460 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69  /*.** This versi
8c470 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79  on of the memory
8c480 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 75 73   allocator is us
8c490 65 64 20 6f 6e 6c 79 20 77 68 65 6e 20 0a 2a 2a  ed only when .**
8c4a0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d   SQLITE_ENABLE_M
8c4b0 45 4d 53 59 53 35 20 69 73 20 64 65 66 69 6e 65  EMSYS5 is define
8c4c0 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  d..*/.#ifdef SQL
8c4d0 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59  ITE_ENABLE_MEMSY
8c4e0 53 35 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 69 6e 69  S5../*.** A mini
8c4f0 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69  mum allocation i
8c500 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  s an instance of
8c510 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
8c520 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 4c 61 72  tructure..** Lar
8c530 67 65 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20  ger allocations 
8c540 61 72 65 20 61 6e 20 61 72 72 61 79 20 6f 66 20  are an array of 
8c550 74 68 65 73 65 20 73 74 72 75 63 74 75 72 65 73  these structures
8c560 20 77 68 65 72 65 20 74 68 65 0a 2a 2a 20 73 69   where the.** si
8c570 7a 65 20 6f 66 20 74 68 65 20 61 72 72 61 79 20  ze of the array 
8c580 69 73 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2e  is a power of 2.
8c590 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20  .**.** The size 
8c5a0 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 6d  of this object m
8c5b0 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f  ust be a power o
8c5c0 66 20 74 77 6f 2e 20 20 54 68 61 74 20 66 61 63  f two.  That fac
8c5d0 74 20 69 73 0a 2a 2a 20 76 65 72 69 66 69 65 64  t is.** verified
8c5e0 20 69 6e 20 6d 65 6d 73 79 73 35 49 6e 69 74 28   in memsys5Init(
8c5f0 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  )..*/.typedef st
8c600 72 75 63 74 20 4d 65 6d 35 4c 69 6e 6b 20 4d 65  ruct Mem5Link Me
8c610 6d 35 4c 69 6e 6b 3b 0a 73 74 72 75 63 74 20 4d  m5Link;.struct M
8c620 65 6d 35 4c 69 6e 6b 20 7b 0a 20 20 69 6e 74 20  em5Link {.  int 
8c630 6e 65 78 74 3b 20 20 20 20 20 20 20 2f 2a 20 49  next;       /* I
8c640 6e 64 65 78 20 6f 66 20 6e 65 78 74 20 66 72 65  ndex of next fre
8c650 65 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 69 6e 74  e chunk */.  int
8c660 20 70 72 65 76 3b 20 20 20 20 20 20 20 2f 2a 20   prev;       /* 
8c670 49 6e 64 65 78 20 6f 66 20 70 72 65 76 69 6f 75  Index of previou
8c680 73 20 66 72 65 65 20 63 68 75 6e 6b 20 2a 2f 0a  s free chunk */.
8c690 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75  };../*.** Maximu
8c6a0 6d 20 73 69 7a 65 20 6f 66 20 61 6e 79 20 61 6c  m size of any al
8c6b0 6c 6f 63 61 74 69 6f 6e 20 69 73 20 28 28 31 3c  location is ((1<
8c6c0 3c 4c 4f 47 4d 41 58 29 2a 6d 65 6d 35 2e 73 7a  <LOGMAX)*mem5.sz
8c6d0 41 74 6f 6d 29 2e 20 53 69 6e 63 65 0a 2a 2a 20  Atom). Since.** 
8c6e0 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 69 73 20 61  mem5.szAtom is a
8c6f0 6c 77 61 79 73 20 61 74 20 6c 65 61 73 74 20 38  lways at least 8
8c700 20 61 6e 64 20 33 32 2d 62 69 74 20 69 6e 74 65   and 32-bit inte
8c710 67 65 72 73 20 61 72 65 20 75 73 65 64 2c 0a 2a  gers are used,.*
8c720 2a 20 69 74 20 69 73 20 6e 6f 74 20 61 63 74 75  * it is not actu
8c730 61 6c 6c 79 20 70 6f 73 73 69 62 6c 65 20 74 6f  ally possible to
8c740 20 72 65 61 63 68 20 74 68 69 73 20 6c 69 6d 69   reach this limi
8c750 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4c 4f  t..*/.#define LO
8c760 47 4d 41 58 20 33 30 0a 0a 2f 2a 0a 2a 2a 20 4d  GMAX 30../*.** M
8c770 61 73 6b 73 20 75 73 65 64 20 66 6f 72 20 6d 65  asks used for me
8c780 6d 35 2e 61 43 74 72 6c 5b 5d 20 65 6c 65 6d 65  m5.aCtrl[] eleme
8c790 6e 74 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  nts..*/.#define 
8c7a0 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 20 20 30 78  CTRL_LOGSIZE  0x
8c7b0 31 66 20 20 20 20 2f 2a 20 4c 6f 67 32 20 53 69  1f    /* Log2 Si
8c7c0 7a 65 20 6f 66 20 74 68 69 73 20 62 6c 6f 63 6b  ze of this block
8c7d0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 43 54 52 4c   */.#define CTRL
8c7e0 5f 46 52 45 45 20 20 20 20 20 30 78 32 30 20 20  _FREE     0x20  
8c7f0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6e 6f 74    /* True if not
8c800 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a   checked out */.
8c810 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68  ./*.** All of th
8c820 65 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c  e static variabl
8c830 65 73 20 75 73 65 64 20 62 79 20 74 68 69 73 20  es used by this 
8c840 6d 6f 64 75 6c 65 20 61 72 65 20 63 6f 6c 6c 65  module are colle
8c850 63 74 65 64 0a 2a 2a 20 69 6e 74 6f 20 61 20 73  cted.** into a s
8c860 69 6e 67 6c 65 20 73 74 72 75 63 74 75 72 65 20  ingle structure 
8c870 6e 61 6d 65 64 20 22 6d 65 6d 35 22 2e 20 20 54  named "mem5".  T
8c880 68 69 73 20 69 73 20 74 6f 20 6b 65 65 70 20 74  his is to keep t
8c890 68 65 0a 2a 2a 20 73 74 61 74 69 63 20 76 61 72  he.** static var
8c8a0 69 61 62 6c 65 73 20 6f 72 67 61 6e 69 7a 65 64  iables organized
8c8b0 20 61 6e 64 20 74 6f 20 72 65 64 75 63 65 20 6e   and to reduce n
8c8c0 61 6d 65 73 70 61 63 65 20 70 6f 6c 6c 75 74 69  amespace polluti
8c8d0 6f 6e 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20  on.** when this 
8c8e0 6d 6f 64 75 6c 65 20 69 73 20 63 6f 6d 62 69 6e  module is combin
8c8f0 65 64 20 77 69 74 68 20 6f 74 68 65 72 20 69 6e  ed with other in
8c900 20 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f   the amalgamatio
8c910 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c  n..*/.static SQL
8c920 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 4d  ITE_WSD struct M
8c930 65 6d 35 47 6c 6f 62 61 6c 20 7b 0a 20 20 2f 2a  em5Global {.  /*
8c940 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 20 61 76 61  .  ** Memory ava
8c950 69 6c 61 62 6c 65 20 66 6f 72 20 61 6c 6c 6f 63  ilable for alloc
8c960 61 74 69 6f 6e 0a 20 20 2a 2f 0a 20 20 69 6e 74  ation.  */.  int
8c970 20 73 7a 41 74 6f 6d 3b 20 20 20 20 20 20 2f 2a   szAtom;      /*
8c980 20 53 6d 61 6c 6c 65 73 74 20 70 6f 73 73 69 62   Smallest possib
8c990 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e  le allocation in
8c9a0 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20   bytes */.  int 
8c9b0 6e 42 6c 6f 63 6b 3b 20 20 20 20 20 20 2f 2a 20  nBlock;      /* 
8c9c0 4e 75 6d 62 65 72 20 6f 66 20 73 7a 41 74 6f 6d  Number of szAtom
8c9d0 20 73 69 7a 65 64 20 62 6c 6f 63 6b 73 20 69 6e   sized blocks in
8c9e0 20 7a 50 6f 6f 6c 20 2a 2f 0a 20 20 75 38 20 2a   zPool */.  u8 *
8c9f0 7a 50 6f 6f 6c 3b 20 20 20 20 20 20 20 2f 2a 20  zPool;       /* 
8ca00 4d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65  Memory available
8ca10 20 74 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65 64   to be allocated
8ca20 20 2a 2f 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a   */.  .  /*.  **
8ca30 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74 72 6f   Mutex to contro
8ca40 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 20  l access to the 
8ca50 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
8ca60 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 20 20 2a  n subsystem..  *
8ca70 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  /.  sqlite3_mute
8ca80 78 20 2a 6d 75 74 65 78 3b 0a 0a 20 20 2f 2a 0a  x *mutex;..  /*.
8ca90 20 20 2a 2a 20 50 65 72 66 6f 72 6d 61 6e 63 65    ** Performance
8caa0 20 73 74 61 74 69 73 74 69 63 73 0a 20 20 2a 2f   statistics.  */
8cab0 0a 20 20 75 36 34 20 6e 41 6c 6c 6f 63 3b 20 20  .  u64 nAlloc;  
8cac0 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20         /* Total 
8cad0 6e 75 6d 62 65 72 20 6f 66 20 63 61 6c 6c 73 20  number of calls 
8cae0 74 6f 20 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 75  to malloc */.  u
8caf0 36 34 20 74 6f 74 61 6c 41 6c 6c 6f 63 3b 20 20  64 totalAlloc;  
8cb00 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6f 66 20 61     /* Total of a
8cb10 6c 6c 20 6d 61 6c 6c 6f 63 20 63 61 6c 6c 73 20  ll malloc calls 
8cb20 2d 20 69 6e 63 6c 75 64 65 73 20 69 6e 74 65 72  - includes inter
8cb30 6e 61 6c 20 66 72 61 67 20 2a 2f 0a 20 20 75 36  nal frag */.  u6
8cb40 34 20 74 6f 74 61 6c 45 78 63 65 73 73 3b 20 20  4 totalExcess;  
8cb50 20 20 2f 2a 20 54 6f 74 61 6c 20 69 6e 74 65 72    /* Total inter
8cb60 6e 61 6c 20 66 72 61 67 6d 65 6e 74 61 74 69 6f  nal fragmentatio
8cb70 6e 20 2a 2f 0a 20 20 75 33 32 20 63 75 72 72 65  n */.  u32 curre
8cb80 6e 74 4f 75 74 3b 20 20 20 20 20 2f 2a 20 43 75  ntOut;     /* Cu
8cb90 72 72 65 6e 74 20 63 68 65 63 6b 6f 75 74 2c 20  rrent checkout, 
8cba0 69 6e 63 6c 75 64 69 6e 67 20 69 6e 74 65 72 6e  including intern
8cbb0 61 6c 20 66 72 61 67 6d 65 6e 74 61 74 69 6f 6e  al fragmentation
8cbc0 20 2a 2f 0a 20 20 75 33 32 20 63 75 72 72 65 6e   */.  u32 curren
8cbd0 74 43 6f 75 6e 74 3b 20 20 20 2f 2a 20 43 75 72  tCount;   /* Cur
8cbe0 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 64  rent number of d
8cbf0 69 73 74 69 6e 63 74 20 63 68 65 63 6b 6f 75 74  istinct checkout
8cc00 73 20 2a 2f 0a 20 20 75 33 32 20 6d 61 78 4f 75  s */.  u32 maxOu
8cc10 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61  t;         /* Ma
8cc20 78 69 6d 75 6d 20 69 6e 73 74 61 6e 74 61 6e 65  ximum instantane
8cc30 6f 75 73 20 63 75 72 72 65 6e 74 4f 75 74 20 2a  ous currentOut *
8cc40 2f 0a 20 20 75 33 32 20 6d 61 78 43 6f 75 6e 74  /.  u32 maxCount
8cc50 3b 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d  ;       /* Maxim
8cc60 75 6d 20 69 6e 73 74 61 6e 74 61 6e 65 6f 75 73  um instantaneous
8cc70 20 63 75 72 72 65 6e 74 43 6f 75 6e 74 20 2a 2f   currentCount */
8cc80 0a 20 20 75 33 32 20 6d 61 78 52 65 71 75 65 73  .  u32 maxReques
8cc90 74 3b 20 20 20 20 20 2f 2a 20 4c 61 72 67 65 73  t;     /* Larges
8cca0 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 28 65 78  t allocation (ex
8ccb0 63 6c 75 73 69 76 65 20 6f 66 20 69 6e 74 65 72  clusive of inter
8ccc0 6e 61 6c 20 66 72 61 67 29 20 2a 2f 0a 20 20 0a  nal frag) */.  .
8ccd0 20 20 2f 2a 0a 20 20 2a 2a 20 4c 69 73 74 73 20    /*.  ** Lists 
8cce0 6f 66 20 66 72 65 65 20 62 6c 6f 63 6b 73 2e 20  of free blocks. 
8ccf0 20 61 69 46 72 65 65 6c 69 73 74 5b 30 5d 20 69   aiFreelist[0] i
8cd00 73 20 61 20 6c 69 73 74 20 6f 66 20 66 72 65 65  s a list of free
8cd10 20 62 6c 6f 63 6b 73 20 6f 66 0a 20 20 2a 2a 20   blocks of.  ** 
8cd20 73 69 7a 65 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d  size mem5.szAtom
8cd30 2e 20 20 61 69 46 72 65 65 6c 69 73 74 5b 31 5d  .  aiFreelist[1]
8cd40 20 68 6f 6c 64 73 20 62 6c 6f 63 6b 73 20 6f 66   holds blocks of
8cd50 20 73 69 7a 65 20 73 7a 41 74 6f 6d 2a 32 2e 0a   size szAtom*2..
8cd60 20 20 2a 2a 20 61 6e 64 20 73 6f 20 66 6f 72 74    ** and so fort
8cd70 68 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 61 69  h..  */.  int ai
8cd80 46 72 65 65 6c 69 73 74 5b 4c 4f 47 4d 41 58 2b  Freelist[LOGMAX+
8cd90 31 5d 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 53  1];..  /*.  ** S
8cda0 70 61 63 65 20 66 6f 72 20 74 72 61 63 6b 69 6e  pace for trackin
8cdb0 67 20 77 68 69 63 68 20 62 6c 6f 63 6b 73 20 61  g which blocks a
8cdc0 72 65 20 63 68 65 63 6b 65 64 20 6f 75 74 20 61  re checked out a
8cdd0 6e 64 20 74 68 65 20 73 69 7a 65 0a 20 20 2a 2a  nd the size.  **
8cde0 20 6f 66 20 65 61 63 68 20 62 6c 6f 63 6b 2e 20   of each block. 
8cdf0 20 4f 6e 65 20 62 79 74 65 20 70 65 72 20 62 6c   One byte per bl
8ce00 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 75 38 20 2a  ock..  */.  u8 *
8ce10 61 43 74 72 6c 3b 0a 0a 7d 20 6d 65 6d 35 20 3d  aCtrl;..} mem5 =
8ce20 20 7b 20 30 20 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41   { 0 };../*.** A
8ce30 63 63 65 73 73 20 74 68 65 20 73 74 61 74 69 63  ccess the static
8ce40 20 76 61 72 69 61 62 6c 65 20 74 68 72 6f 75 67   variable throug
8ce50 68 20 61 20 6d 61 63 72 6f 20 66 6f 72 20 53 51  h a macro for SQ
8ce60 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 2a 2f  LITE_OMIT_WSD.*/
8ce70 0a 23 64 65 66 69 6e 65 20 6d 65 6d 35 20 47 4c  .#define mem5 GL
8ce80 4f 42 41 4c 28 73 74 72 75 63 74 20 4d 65 6d 35  OBAL(struct Mem5
8ce90 47 6c 6f 62 61 6c 2c 20 6d 65 6d 35 29 0a 0a 2f  Global, mem5)../
8cea0 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20 6d 65  *.** Assuming me
8ceb0 6d 35 2e 7a 50 6f 6f 6c 20 69 73 20 64 69 76 69  m5.zPool is divi
8cec0 64 65 64 20 75 70 20 69 6e 74 6f 20 61 6e 20 61  ded up into an a
8ced0 72 72 61 79 20 6f 66 20 4d 65 6d 35 4c 69 6e 6b  rray of Mem5Link
8cee0 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73 2c 20  .** structures, 
8cef0 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  return a pointer
8cf00 20 74 6f 20 74 68 65 20 69 64 78 2d 74 68 20 73   to the idx-th s
8cf10 75 63 68 20 6c 69 6b 2e 0a 2a 2f 0a 23 64 65 66  uch lik..*/.#def
8cf20 69 6e 65 20 4d 45 4d 35 4c 49 4e 4b 28 69 64 78  ine MEM5LINK(idx
8cf30 29 20 28 28 4d 65 6d 35 4c 69 6e 6b 20 2a 29 28  ) ((Mem5Link *)(
8cf40 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b 28 69 64 78  &mem5.zPool[(idx
8cf50 29 2a 6d 65 6d 35 2e 73 7a 41 74 6f 6d 5d 29 29  )*mem5.szAtom]))
8cf60 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74  ../*.** Unlink t
8cf70 68 65 20 63 68 75 6e 6b 20 61 74 20 6d 65 6d 35  he chunk at mem5
8cf80 2e 61 50 6f 6f 6c 5b 69 5d 20 66 72 6f 6d 20 6c  .aPool[i] from l
8cf90 69 73 74 20 69 74 20 69 73 20 63 75 72 72 65 6e  ist it is curren
8cfa0 74 6c 79 0a 2a 2a 20 6f 6e 2e 20 20 49 74 20 73  tly.** on.  It s
8cfb0 68 6f 75 6c 64 20 62 65 20 66 6f 75 6e 64 20 6f  hould be found o
8cfc0 6e 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73  n mem5.aiFreelis
8cfd0 74 5b 69 4c 6f 67 73 69 7a 65 5d 2e 0a 2a 2f 0a  t[iLogsize]..*/.
8cfe0 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73  static void mems
8cff0 79 73 35 55 6e 6c 69 6e 6b 28 69 6e 74 20 69 2c  ys5Unlink(int i,
8d000 20 69 6e 74 20 69 4c 6f 67 73 69 7a 65 29 7b 0a   int iLogsize){.
8d010 20 20 69 6e 74 20 6e 65 78 74 2c 20 70 72 65 76    int next, prev
8d020 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30  ;.  assert( i>=0
8d030 20 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63   && i<mem5.nBloc
8d040 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  k );.  assert( i
8d050 4c 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 69 4c  Logsize>=0 && iL
8d060 6f 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20 29  ogsize<=LOGMAX )
8d070 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65 6d  ;.  assert( (mem
8d080 35 2e 61 43 74 72 6c 5b 69 5d 20 26 20 43 54 52  5.aCtrl[i] & CTR
8d090 4c 5f 4c 4f 47 53 49 5a 45 29 3d 3d 69 4c 6f 67  L_LOGSIZE)==iLog
8d0a0 73 69 7a 65 20 29 3b 0a 0a 20 20 6e 65 78 74 20  size );..  next 
8d0b0 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 6e  = MEM5LINK(i)->n
8d0c0 65 78 74 3b 0a 20 20 70 72 65 76 20 3d 20 4d 45  ext;.  prev = ME
8d0d0 4d 35 4c 49 4e 4b 28 69 29 2d 3e 70 72 65 76 3b  M5LINK(i)->prev;
8d0e0 0a 20 20 69 66 28 20 70 72 65 76 3c 30 20 29 7b  .  if( prev<0 ){
8d0f0 0a 20 20 20 20 6d 65 6d 35 2e 61 69 46 72 65 65  .    mem5.aiFree
8d100 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 20 3d  list[iLogsize] =
8d110 20 6e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a   next;.  }else{.
8d120 20 20 20 20 4d 45 4d 35 4c 49 4e 4b 28 70 72 65      MEM5LINK(pre
8d130 76 29 2d 3e 6e 65 78 74 20 3d 20 6e 65 78 74 3b  v)->next = next;
8d140 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 78 74 3e  .  }.  if( next>
8d150 3d 30 20 29 7b 0a 20 20 20 20 4d 45 4d 35 4c 49  =0 ){.    MEM5LI
8d160 4e 4b 28 6e 65 78 74 29 2d 3e 70 72 65 76 20 3d  NK(next)->prev =
8d170 20 70 72 65 76 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a   prev;.  }.}../*
8d180 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 20 63 68 75  .** Link the chu
8d190 6e 6b 20 61 74 20 6d 65 6d 35 2e 61 50 6f 6f 6c  nk at mem5.aPool
8d1a0 5b 69 5d 20 73 6f 20 74 68 61 74 20 69 73 20 6f  [i] so that is o
8d1b0 6e 20 74 68 65 20 69 4c 6f 67 73 69 7a 65 0a 2a  n the iLogsize.*
8d1c0 2a 20 66 72 65 65 20 6c 69 73 74 2e 0a 2a 2f 0a  * free list..*/.
8d1d0 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73  static void mems
8d1e0 79 73 35 4c 69 6e 6b 28 69 6e 74 20 69 2c 20 69  ys5Link(int i, i
8d1f0 6e 74 20 69 4c 6f 67 73 69 7a 65 29 7b 0a 20 20  nt iLogsize){.  
8d200 69 6e 74 20 78 3b 0a 20 20 61 73 73 65 72 74 28  int x;.  assert(
8d210 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
8d220 65 6c 64 28 6d 65 6d 35 2e 6d 75 74 65 78 29 20  eld(mem5.mutex) 
8d230 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d  );.  assert( i>=
8d240 30 20 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f  0 && i<mem5.nBlo
8d250 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ck );.  assert( 
8d260 69 4c 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 69  iLogsize>=0 && i
8d270 4c 6f 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20  Logsize<=LOGMAX 
8d280 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65  );.  assert( (me
8d290 6d 35 2e 61 43 74 72 6c 5b 69 5d 20 26 20 43 54  m5.aCtrl[i] & CT
8d2a0 52 4c 5f 4c 4f 47 53 49 5a 45 29 3d 3d 69 4c 6f  RL_LOGSIZE)==iLo
8d2b0 67 73 69 7a 65 20 29 3b 0a 0a 20 20 78 20 3d 20  gsize );..  x = 
8d2c0 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 6e 65 78  MEM5LINK(i)->nex
8d2d0 74 20 3d 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c  t = mem5.aiFreel
8d2e0 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 3b 0a 20  ist[iLogsize];. 
8d2f0 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 70 72   MEM5LINK(i)->pr
8d300 65 76 20 3d 20 2d 31 3b 0a 20 20 69 66 28 20 78  ev = -1;.  if( x
8d310 3e 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72  >=0 ){.    asser
8d320 74 28 20 78 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b  t( x<mem5.nBlock
8d330 20 29 3b 0a 20 20 20 20 4d 45 4d 35 4c 49 4e 4b   );.    MEM5LINK
8d340 28 78 29 2d 3e 70 72 65 76 20 3d 20 69 3b 0a 20  (x)->prev = i;. 
8d350 20 7d 0a 20 20 6d 65 6d 35 2e 61 69 46 72 65 65   }.  mem5.aiFree
8d360 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 20 3d  list[iLogsize] =
8d370 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20   i;.}../*.** If 
8d380 74 68 65 20 53 54 41 54 49 43 5f 4d 45 4d 20 6d  the STATIC_MEM m
8d390 75 74 65 78 20 69 73 20 6e 6f 74 20 61 6c 72 65  utex is not alre
8d3a0 61 64 79 20 68 65 6c 64 2c 20 6f 62 74 61 69 6e  ady held, obtain
8d3b0 20 69 74 20 6e 6f 77 2e 20 54 68 65 20 6d 75 74   it now. The mut
8d3c0 65 78 0a 2a 2a 20 77 69 6c 6c 20 61 6c 72 65 61  ex.** will alrea
8d3d0 64 79 20 62 65 20 68 65 6c 64 20 28 6f 62 74 61  dy be held (obta
8d3e0 69 6e 65 64 20 62 79 20 63 6f 64 65 20 69 6e 20  ined by code in 
8d3f0 6d 61 6c 6c 6f 63 2e 63 29 20 69 66 0a 2a 2a 20  malloc.c) if.** 
8d400 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
8d410 66 69 67 2e 62 4d 65 6d 53 74 61 74 20 69 73 20  fig.bMemStat is 
8d420 74 72 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  true..*/.static 
8d430 76 6f 69 64 20 6d 65 6d 73 79 73 35 45 6e 74 65  void memsys5Ente
8d440 72 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74  r(void){.  sqlit
8d450 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d  e3_mutex_enter(m
8d460 65 6d 35 2e 6d 75 74 65 78 29 3b 0a 7d 0a 73 74  em5.mutex);.}.st
8d470 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73  atic void memsys
8d480 35 4c 65 61 76 65 28 76 6f 69 64 29 7b 0a 20 20  5Leave(void){.  
8d490 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
8d4a0 61 76 65 28 6d 65 6d 35 2e 6d 75 74 65 78 29 3b  ave(mem5.mutex);
8d4b0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
8d4c0 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20   the size of an 
8d4d0 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 6c 6c 6f  outstanding allo
8d4e0 63 61 74 69 6f 6e 2c 20 69 6e 20 62 79 74 65 73  cation, in bytes
8d4f0 2e 20 20 54 68 65 0a 2a 2a 20 73 69 7a 65 20 72  .  The.** size r
8d500 65 74 75 72 6e 65 64 20 6f 6d 69 74 73 20 74 68  eturned omits th
8d510 65 20 38 2d 62 79 74 65 20 68 65 61 64 65 72 20  e 8-byte header 
8d520 6f 76 65 72 68 65 61 64 2e 20 20 54 68 69 73 20  overhead.  This 
8d530 6f 6e 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f  only.** works fo
8d540 72 20 63 68 75 6e 6b 73 20 74 68 61 74 20 61 72  r chunks that ar
8d550 65 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63  e currently chec
8d560 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a 73 74 61 74  ked out..*/.stat
8d570 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 53 69  ic int memsys5Si
8d580 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 69  ze(void *p){.  i
8d590 6e 74 20 69 53 69 7a 65 20 3d 20 30 3b 0a 20 20  nt iSize = 0;.  
8d5a0 69 66 28 20 70 20 29 7b 0a 20 20 20 20 69 6e 74  if( p ){.    int
8d5b0 20 69 20 3d 20 28 28 75 38 20 2a 29 70 2d 6d 65   i = ((u8 *)p-me
8d5c0 6d 35 2e 7a 50 6f 6f 6c 29 2f 6d 65 6d 35 2e 73  m5.zPool)/mem5.s
8d5d0 7a 41 74 6f 6d 3b 0a 20 20 20 20 61 73 73 65 72  zAtom;.    asser
8d5e0 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 6d 65 6d  t( i>=0 && i<mem
8d5f0 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 20 20  5.nBlock );.    
8d600 69 53 69 7a 65 20 3d 20 6d 65 6d 35 2e 73 7a 41  iSize = mem5.szA
8d610 74 6f 6d 20 2a 20 28 31 20 3c 3c 20 28 6d 65 6d  tom * (1 << (mem
8d620 35 2e 61 43 74 72 6c 5b 69 5d 26 43 54 52 4c 5f  5.aCtrl[i]&CTRL_
8d630 4c 4f 47 53 49 5a 45 29 29 3b 0a 20 20 7d 0a 20  LOGSIZE));.  }. 
8d640 20 72 65 74 75 72 6e 20 69 53 69 7a 65 3b 0a 7d   return iSize;.}
8d650 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65  ../*.** Find the
8d660 20 66 69 72 73 74 20 65 6e 74 72 79 20 6f 6e 20   first entry on 
8d670 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 4c 6f  the freelist iLo
8d680 67 73 69 7a 65 2e 20 20 55 6e 6c 69 6e 6b 20 74  gsize.  Unlink t
8d690 68 61 74 0a 2a 2a 20 65 6e 74 72 79 20 61 6e 64  hat.** entry and
8d6a0 20 72 65 74 75 72 6e 20 69 74 73 20 69 6e 64 65   return its inde
8d6b0 78 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  x. .*/.static in
8d6c0 74 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 46  t memsys5UnlinkF
8d6d0 69 72 73 74 28 69 6e 74 20 69 4c 6f 67 73 69 7a  irst(int iLogsiz
8d6e0 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  e){.  int i;.  i
8d6f0 6e 74 20 69 46 69 72 73 74 3b 0a 0a 20 20 61 73  nt iFirst;..  as
8d700 73 65 72 74 28 20 69 4c 6f 67 73 69 7a 65 3e 3d  sert( iLogsize>=
8d710 30 20 26 26 20 69 4c 6f 67 73 69 7a 65 3c 3d 4c  0 && iLogsize<=L
8d720 4f 47 4d 41 58 20 29 3b 0a 20 20 69 20 3d 20 69  OGMAX );.  i = i
8d730 46 69 72 73 74 20 3d 20 6d 65 6d 35 2e 61 69 46  First = mem5.aiF
8d740 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65  reelist[iLogsize
8d750 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 69 46 69  ];.  assert( iFi
8d760 72 73 74 3e 3d 30 20 29 3b 0a 20 20 77 68 69 6c  rst>=0 );.  whil
8d770 65 28 20 69 3e 30 20 29 7b 0a 20 20 20 20 69 66  e( i>0 ){.    if
8d780 28 20 69 3c 69 46 69 72 73 74 20 29 20 69 46 69  ( i<iFirst ) iFi
8d790 72 73 74 20 3d 20 69 3b 0a 20 20 20 20 69 20 3d  rst = i;.    i =
8d7a0 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 6e 65   MEM5LINK(i)->ne
8d7b0 78 74 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73  xt;.  }.  memsys
8d7c0 35 55 6e 6c 69 6e 6b 28 69 46 69 72 73 74 2c 20  5Unlink(iFirst, 
8d7d0 69 4c 6f 67 73 69 7a 65 29 3b 0a 20 20 72 65 74  iLogsize);.  ret
8d7e0 75 72 6e 20 69 46 69 72 73 74 3b 0a 7d 0a 0a 2f  urn iFirst;.}../
8d7f0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 62 6c  *.** Return a bl
8d800 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 6f 66  ock of memory of
8d810 20 61 74 20 6c 65 61 73 74 20 6e 42 79 74 65 73   at least nBytes
8d820 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 20 52 65 74   in size..** Ret
8d830 75 72 6e 20 4e 55 4c 4c 20 69 66 20 75 6e 61 62  urn NULL if unab
8d840 6c 65 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c  le.  Return NULL
8d850 20 69 66 20 6e 42 79 74 65 73 3d 3d 30 2e 0a 2a   if nBytes==0..*
8d860 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20  *.** The caller 
8d870 67 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 20  guarantees that 
8d880 6e 42 79 74 65 20 70 6f 73 69 74 69 76 65 2e 0a  nByte positive..
8d890 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72  **.** The caller
8d8a0 20 68 61 73 20 6f 62 74 61 69 6e 65 64 20 61 20   has obtained a 
8d8b0 6d 75 74 65 78 20 70 72 69 6f 72 20 74 6f 20 69  mutex prior to i
8d8c0 6e 76 6f 6b 69 6e 67 20 74 68 69 73 0a 2a 2a 20  nvoking this.** 
8d8d0 72 6f 75 74 69 6e 65 20 73 6f 20 74 68 65 72 65  routine so there
8d8e0 20 69 73 20 6e 65 76 65 72 20 61 6e 79 20 63 68   is never any ch
8d8f0 61 6e 63 65 20 74 68 61 74 20 74 77 6f 20 6f 72  ance that two or
8d900 20 6d 6f 72 65 0a 2a 2a 20 74 68 72 65 61 64 73   more.** threads
8d910 20 63 61 6e 20 62 65 20 69 6e 20 74 68 69 73 20   can be in this 
8d920 72 6f 75 74 69 6e 65 20 61 74 20 74 68 65 20 73  routine at the s
8d930 61 6d 65 20 74 69 6d 65 2e 0a 2a 2f 0a 73 74 61  ame time..*/.sta
8d940 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73  tic void *memsys
8d950 35 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 69 6e  5MallocUnsafe(in
8d960 74 20 6e 42 79 74 65 29 7b 0a 20 20 69 6e 74 20  t nByte){.  int 
8d970 69 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  i;           /* 
8d980 49 6e 64 65 78 20 6f 66 20 61 20 6d 65 6d 35 2e  Index of a mem5.
8d990 61 50 6f 6f 6c 5b 5d 20 73 6c 6f 74 20 2a 2f 0a  aPool[] slot */.
8d9a0 20 20 69 6e 74 20 69 42 69 6e 3b 20 20 20 20 20    int iBin;     
8d9b0 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f     /* Index into
8d9c0 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74   mem5.aiFreelist
8d9d0 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 46 75 6c  [] */.  int iFul
8d9e0 6c 53 7a 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65  lSz;     /* Size
8d9f0 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72   of allocation r
8da00 6f 75 6e 64 65 64 20 75 70 20 74 6f 20 70 6f 77  ounded up to pow
8da10 65 72 20 6f 66 20 32 20 2a 2f 0a 20 20 69 6e 74  er of 2 */.  int
8da20 20 69 4c 6f 67 73 69 7a 65 3b 20 20 20 20 2f 2a   iLogsize;    /*
8da30 20 4c 6f 67 32 20 6f 66 20 69 46 75 6c 6c 53 7a   Log2 of iFullSz
8da40 2f 50 4f 57 32 5f 4d 49 4e 20 2a 2f 0a 0a 20 20  /POW2_MIN */..  
8da50 2f 2a 20 6e 42 79 74 65 20 6d 75 73 74 20 62 65  /* nByte must be
8da60 20 61 20 70 6f 73 69 74 69 76 65 20 2a 2f 0a 20   a positive */. 
8da70 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 3e 30   assert( nByte>0
8da80 20 29 3b 0a 0a 20 20 2f 2a 20 4b 65 65 70 20 74   );..  /* Keep t
8da90 72 61 63 6b 20 6f 66 20 74 68 65 20 6d 61 78 69  rack of the maxi
8daa0 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72  mum allocation r
8dab0 65 71 75 65 73 74 2e 20 20 45 76 65 6e 20 75 6e  equest.  Even un
8dac0 66 75 6c 66 69 6c 6c 65 64 0a 20 20 2a 2a 20 72  fulfilled.  ** r
8dad0 65 71 75 65 73 74 73 20 61 72 65 20 63 6f 75 6e  equests are coun
8dae0 74 65 64 20 2a 2f 0a 20 20 69 66 28 20 28 75 33  ted */.  if( (u3
8daf0 32 29 6e 42 79 74 65 3e 6d 65 6d 35 2e 6d 61 78  2)nByte>mem5.max
8db00 52 65 71 75 65 73 74 20 29 7b 0a 20 20 20 20 6d  Request ){.    m
8db10 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 20 3d  em5.maxRequest =
8db20 20 6e 42 79 74 65 3b 0a 20 20 7d 0a 0a 20 20 2f   nByte;.  }..  /
8db30 2a 20 41 62 6f 72 74 20 69 66 20 74 68 65 20 72  * Abort if the r
8db40 65 71 75 65 73 74 65 64 20 61 6c 6c 6f 63 61 74  equested allocat
8db50 69 6f 6e 20 73 69 7a 65 20 69 73 20 6c 61 72 67  ion size is larg
8db60 65 72 20 74 68 61 6e 20 74 68 65 20 6c 61 72 67  er than the larg
8db70 65 73 74 0a 20 20 2a 2a 20 70 6f 77 65 72 20 6f  est.  ** power o
8db80 66 20 74 77 6f 20 74 68 61 74 20 77 65 20 63 61  f two that we ca
8db90 6e 20 72 65 70 72 65 73 65 6e 74 20 75 73 69 6e  n represent usin
8dba0 67 20 33 32 2d 62 69 74 20 73 69 67 6e 65 64 20  g 32-bit signed 
8dbb0 69 6e 74 65 67 65 72 73 2e 0a 20 20 2a 2f 0a 20  integers..  */. 
8dbc0 20 69 66 28 20 6e 42 79 74 65 20 3e 20 30 78 34   if( nByte > 0x4
8dbd0 30 30 30 30 30 30 30 20 29 7b 0a 20 20 20 20 72  0000000 ){.    r
8dbe0 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20  eturn 0;.  }..  
8dbf0 2f 2a 20 52 6f 75 6e 64 20 6e 42 79 74 65 20 75  /* Round nByte u
8dc00 70 20 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61  p to the next va
8dc10 6c 69 64 20 70 6f 77 65 72 20 6f 66 20 74 77 6f  lid power of two
8dc20 20 2a 2f 0a 20 20 66 6f 72 28 69 46 75 6c 6c 53   */.  for(iFullS
8dc30 7a 3d 6d 65 6d 35 2e 73 7a 41 74 6f 6d 2c 20 69  z=mem5.szAtom, i
8dc40 4c 6f 67 73 69 7a 65 3d 30 3b 20 69 46 75 6c 6c  Logsize=0; iFull
8dc50 53 7a 3c 6e 42 79 74 65 3b 20 69 46 75 6c 6c 53  Sz<nByte; iFullS
8dc60 7a 20 2a 3d 20 32 2c 20 69 4c 6f 67 73 69 7a 65  z *= 2, iLogsize
8dc70 2b 2b 29 7b 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65  ++){}..  /* Make
8dc80 20 73 75 72 65 20 6d 65 6d 35 2e 61 69 46 72 65   sure mem5.aiFre
8dc90 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 20  elist[iLogsize] 
8dca0 63 6f 6e 74 61 69 6e 73 20 61 74 20 6c 65 61 73  contains at leas
8dcb0 74 20 6f 6e 65 20 66 72 65 65 0a 20 20 2a 2a 20  t one free.  ** 
8dcc0 62 6c 6f 63 6b 2e 20 20 49 66 20 6e 6f 74 2c 20  block.  If not, 
8dcd0 74 68 65 6e 20 73 70 6c 69 74 20 61 20 62 6c 6f  then split a blo
8dce0 63 6b 20 6f 66 20 74 68 65 20 6e 65 78 74 20 6c  ck of the next l
8dcf0 61 72 67 65 72 20 70 6f 77 65 72 20 6f 66 0a 20  arger power of. 
8dd00 20 2a 2a 20 74 77 6f 20 69 6e 20 6f 72 64 65 72   ** two in order
8dd10 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77   to create a new
8dd20 20 66 72 65 65 20 62 6c 6f 63 6b 20 6f 66 20 73   free block of s
8dd30 69 7a 65 20 69 4c 6f 67 73 69 7a 65 2e 0a 20 20  ize iLogsize..  
8dd40 2a 2f 0a 20 20 66 6f 72 28 69 42 69 6e 3d 69 4c  */.  for(iBin=iL
8dd50 6f 67 73 69 7a 65 3b 20 6d 65 6d 35 2e 61 69 46  ogsize; mem5.aiF
8dd60 72 65 65 6c 69 73 74 5b 69 42 69 6e 5d 3c 30 20  reelist[iBin]<0 
8dd70 26 26 20 69 42 69 6e 3c 3d 4c 4f 47 4d 41 58 3b  && iBin<=LOGMAX;
8dd80 20 69 42 69 6e 2b 2b 29 7b 7d 0a 20 20 69 66 28   iBin++){}.  if(
8dd90 20 69 42 69 6e 3e 4c 4f 47 4d 41 58 20 29 20 72   iBin>LOGMAX ) r
8dda0 65 74 75 72 6e 20 30 3b 0a 20 20 69 20 3d 20 6d  eturn 0;.  i = m
8ddb0 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 46 69 72 73  emsys5UnlinkFirs
8ddc0 74 28 69 42 69 6e 29 3b 0a 20 20 77 68 69 6c 65  t(iBin);.  while
8ddd0 28 20 69 42 69 6e 3e 69 4c 6f 67 73 69 7a 65 20  ( iBin>iLogsize 
8dde0 29 7b 0a 20 20 20 20 69 6e 74 20 6e 65 77 53 69  ){.    int newSi
8ddf0 7a 65 3b 0a 0a 20 20 20 20 69 42 69 6e 2d 2d 3b  ze;..    iBin--;
8de00 0a 20 20 20 20 6e 65 77 53 69 7a 65 20 3d 20 31  .    newSize = 1
8de10 20 3c 3c 20 69 42 69 6e 3b 0a 20 20 20 20 6d 65   << iBin;.    me
8de20 6d 35 2e 61 43 74 72 6c 5b 69 2b 6e 65 77 53 69  m5.aCtrl[i+newSi
8de30 7a 65 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20  ze] = CTRL_FREE 
8de40 7c 20 69 42 69 6e 3b 0a 20 20 20 20 6d 65 6d 73  | iBin;.    mems
8de50 79 73 35 4c 69 6e 6b 28 69 2b 6e 65 77 53 69 7a  ys5Link(i+newSiz
8de60 65 2c 20 69 42 69 6e 29 3b 0a 20 20 7d 0a 20 20  e, iBin);.  }.  
8de70 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 5d 20 3d 20  mem5.aCtrl[i] = 
8de80 69 4c 6f 67 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20  iLogsize;..  /* 
8de90 55 70 64 61 74 65 20 61 6c 6c 6f 63 61 74 6f 72  Update allocator
8dea0 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 73 74 61   performance sta
8deb0 74 69 73 74 69 63 73 2e 20 2a 2f 0a 20 20 6d 65  tistics. */.  me
8dec0 6d 35 2e 6e 41 6c 6c 6f 63 2b 2b 3b 0a 20 20 6d  m5.nAlloc++;.  m
8ded0 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c 6f 63 20 2b  em5.totalAlloc +
8dee0 3d 20 69 46 75 6c 6c 53 7a 3b 0a 20 20 6d 65 6d  = iFullSz;.  mem
8def0 35 2e 74 6f 74 61 6c 45 78 63 65 73 73 20 2b 3d  5.totalExcess +=
8df00 20 69 46 75 6c 6c 53 7a 20 2d 20 6e 42 79 74 65   iFullSz - nByte
8df10 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74  ;.  mem5.current
8df20 43 6f 75 6e 74 2b 2b 3b 0a 20 20 6d 65 6d 35 2e  Count++;.  mem5.
8df30 63 75 72 72 65 6e 74 4f 75 74 20 2b 3d 20 69 46  currentOut += iF
8df40 75 6c 6c 53 7a 3b 0a 20 20 69 66 28 20 6d 65 6d  ullSz;.  if( mem
8df50 35 2e 6d 61 78 43 6f 75 6e 74 3c 6d 65 6d 35 2e  5.maxCount<mem5.
8df60 63 75 72 72 65 6e 74 43 6f 75 6e 74 20 29 20 6d  currentCount ) m
8df70 65 6d 35 2e 6d 61 78 43 6f 75 6e 74 20 3d 20 6d  em5.maxCount = m
8df80 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74  em5.currentCount
8df90 3b 0a 20 20 69 66 28 20 6d 65 6d 35 2e 6d 61 78  ;.  if( mem5.max
8dfa0 4f 75 74 3c 6d 65 6d 35 2e 63 75 72 72 65 6e 74  Out<mem5.current
8dfb0 4f 75 74 20 29 20 6d 65 6d 35 2e 6d 61 78 4f 75  Out ) mem5.maxOu
8dfc0 74 20 3d 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74  t = mem5.current
8dfd0 4f 75 74 3b 0a 0a 20 20 2f 2a 20 52 65 74 75 72  Out;..  /* Retur
8dfe0 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
8dff0 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d  he allocated mem
8e000 6f 72 79 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e  ory. */.  return
8e010 20 28 76 6f 69 64 2a 29 26 6d 65 6d 35 2e 7a 50   (void*)&mem5.zP
8e020 6f 6f 6c 5b 69 2a 6d 65 6d 35 2e 73 7a 41 74 6f  ool[i*mem5.szAto
8e030 6d 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65  m];.}../*.** Fre
8e040 65 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67  e an outstanding
8e050 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
8e060 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  on..*/.static vo
8e070 69 64 20 6d 65 6d 73 79 73 35 46 72 65 65 55 6e  id memsys5FreeUn
8e080 73 61 66 65 28 76 6f 69 64 20 2a 70 4f 6c 64 29  safe(void *pOld)
8e090 7b 0a 20 20 75 33 32 20 73 69 7a 65 2c 20 69 4c  {.  u32 size, iL
8e0a0 6f 67 73 69 7a 65 3b 0a 20 20 69 6e 74 20 69 42  ogsize;.  int iB
8e0b0 6c 6f 63 6b 3b 0a 0a 20 20 2f 2a 20 53 65 74 20  lock;..  /* Set 
8e0c0 69 42 6c 6f 63 6b 20 74 6f 20 74 68 65 20 69 6e  iBlock to the in
8e0d0 64 65 78 20 6f 66 20 74 68 65 20 62 6c 6f 63 6b  dex of the block
8e0e0 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70   pointed to by p
8e0f0 4f 6c 64 20 69 6e 20 0a 20 20 2a 2a 20 74 68 65  Old in .  ** the
8e100 20 61 72 72 61 79 20 6f 66 20 6d 65 6d 35 2e 73   array of mem5.s
8e110 7a 41 74 6f 6d 20 62 79 74 65 20 62 6c 6f 63 6b  zAtom byte block
8e120 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  s pointed to by 
8e130 6d 65 6d 35 2e 7a 50 6f 6f 6c 2e 0a 20 20 2a 2f  mem5.zPool..  */
8e140 0a 20 20 69 42 6c 6f 63 6b 20 3d 20 28 28 75 38  .  iBlock = ((u8
8e150 20 2a 29 70 4f 6c 64 2d 6d 65 6d 35 2e 7a 50 6f   *)pOld-mem5.zPo
8e160 6f 6c 29 2f 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3b  ol)/mem5.szAtom;
8e170 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61  ..  /* Check tha
8e180 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 70 4f  t the pointer pO
8e190 6c 64 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 76  ld points to a v
8e1a0 61 6c 69 64 2c 20 6e 6f 6e 2d 66 72 65 65 20 62  alid, non-free b
8e1b0 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 61 73 73 65 72  lock. */.  asser
8e1c0 74 28 20 69 42 6c 6f 63 6b 3e 3d 30 20 26 26 20  t( iBlock>=0 && 
8e1d0 69 42 6c 6f 63 6b 3c 6d 65 6d 35 2e 6e 42 6c 6f  iBlock<mem5.nBlo
8e1e0 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ck );.  assert( 
8e1f0 28 28 75 38 20 2a 29 70 4f 6c 64 2d 6d 65 6d 35  ((u8 *)pOld-mem5
8e200 2e 7a 50 6f 6f 6c 29 25 6d 65 6d 35 2e 73 7a 41  .zPool)%mem5.szA
8e210 74 6f 6d 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  tom==0 );.  asse
8e220 72 74 28 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b  rt( (mem5.aCtrl[
8e230 69 42 6c 6f 63 6b 5d 20 26 20 43 54 52 4c 5f 46  iBlock] & CTRL_F
8e240 52 45 45 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 4c  REE)==0 );..  iL
8e250 6f 67 73 69 7a 65 20 3d 20 6d 65 6d 35 2e 61 43  ogsize = mem5.aC
8e260 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 26 20 43 54  trl[iBlock] & CT
8e270 52 4c 5f 4c 4f 47 53 49 5a 45 3b 0a 20 20 73 69  RL_LOGSIZE;.  si
8e280 7a 65 20 3d 20 31 3c 3c 69 4c 6f 67 73 69 7a 65  ze = 1<<iLogsize
8e290 3b 0a 20 20 61 73 73 65 72 74 28 20 69 42 6c 6f  ;.  assert( iBlo
8e2a0 63 6b 2b 73 69 7a 65 2d 31 3c 28 75 33 32 29 6d  ck+size-1<(u32)m
8e2b0 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 0a 20  em5.nBlock );.. 
8e2c0 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f   mem5.aCtrl[iBlo
8e2d0 63 6b 5d 20 7c 3d 20 43 54 52 4c 5f 46 52 45 45  ck] |= CTRL_FREE
8e2e0 3b 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69  ;.  mem5.aCtrl[i
8e2f0 42 6c 6f 63 6b 2b 73 69 7a 65 2d 31 5d 20 7c 3d  Block+size-1] |=
8e300 20 43 54 52 4c 5f 46 52 45 45 3b 0a 20 20 61 73   CTRL_FREE;.  as
8e310 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65  sert( mem5.curre
8e320 6e 74 43 6f 75 6e 74 3e 30 20 29 3b 0a 20 20 61  ntCount>0 );.  a
8e330 73 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72  ssert( mem5.curr
8e340 65 6e 74 4f 75 74 3e 3d 28 73 69 7a 65 2a 6d 65  entOut>=(size*me
8e350 6d 35 2e 73 7a 41 74 6f 6d 29 20 29 3b 0a 20 20  m5.szAtom) );.  
8e360 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e  mem5.currentCoun
8e370 74 2d 2d 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 72  t--;.  mem5.curr
8e380 65 6e 74 4f 75 74 20 2d 3d 20 73 69 7a 65 2a 6d  entOut -= size*m
8e390 65 6d 35 2e 73 7a 41 74 6f 6d 3b 0a 20 20 61 73  em5.szAtom;.  as
8e3a0 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65  sert( mem5.curre
8e3b0 6e 74 4f 75 74 3e 30 20 7c 7c 20 6d 65 6d 35 2e  ntOut>0 || mem5.
8e3c0 63 75 72 72 65 6e 74 43 6f 75 6e 74 3d 3d 30 20  currentCount==0 
8e3d0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d  );.  assert( mem
8e3e0 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 3e 30  5.currentCount>0
8e3f0 20 7c 7c 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74   || mem5.current
8e400 4f 75 74 3d 3d 30 20 29 3b 0a 0a 20 20 6d 65 6d  Out==0 );..  mem
8e410 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20  5.aCtrl[iBlock] 
8e420 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c  = CTRL_FREE | iL
8e430 6f 67 73 69 7a 65 3b 0a 20 20 77 68 69 6c 65 28  ogsize;.  while(
8e440 20 41 4c 57 41 59 53 28 69 4c 6f 67 73 69 7a 65   ALWAYS(iLogsize
8e450 3c 4c 4f 47 4d 41 58 29 20 29 7b 0a 20 20 20 20  <LOGMAX) ){.    
8e460 69 6e 74 20 69 42 75 64 64 79 3b 0a 20 20 20 20  int iBuddy;.    
8e470 69 66 28 20 28 69 42 6c 6f 63 6b 3e 3e 69 4c 6f  if( (iBlock>>iLo
8e480 67 73 69 7a 65 29 20 26 20 31 20 29 7b 0a 20 20  gsize) & 1 ){.  
8e490 20 20 20 20 69 42 75 64 64 79 20 3d 20 69 42 6c      iBuddy = iBl
8e4a0 6f 63 6b 20 2d 20 73 69 7a 65 3b 0a 20 20 20 20  ock - size;.    
8e4b0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 42 75  }else{.      iBu
8e4c0 64 64 79 20 3d 20 69 42 6c 6f 63 6b 20 2b 20 73  ddy = iBlock + s
8e4d0 69 7a 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  ize;.    }.    a
8e4e0 73 73 65 72 74 28 20 69 42 75 64 64 79 3e 3d 30  ssert( iBuddy>=0
8e4f0 20 29 3b 0a 20 20 20 20 69 66 28 20 28 69 42 75   );.    if( (iBu
8e500 64 64 79 2b 28 31 3c 3c 69 4c 6f 67 73 69 7a 65  ddy+(1<<iLogsize
8e510 29 29 3e 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29  ))>mem5.nBlock )
8e520 20 62 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 20   break;.    if( 
8e530 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75 64 64  mem5.aCtrl[iBudd
8e540 79 5d 21 3d 28 43 54 52 4c 5f 46 52 45 45 20 7c  y]!=(CTRL_FREE |
8e550 20 69 4c 6f 67 73 69 7a 65 29 20 29 20 62 72 65   iLogsize) ) bre
8e560 61 6b 3b 0a 20 20 20 20 6d 65 6d 73 79 73 35 55  ak;.    memsys5U
8e570 6e 6c 69 6e 6b 28 69 42 75 64 64 79 2c 20 69 4c  nlink(iBuddy, iL
8e580 6f 67 73 69 7a 65 29 3b 0a 20 20 20 20 69 4c 6f  ogsize);.    iLo
8e590 67 73 69 7a 65 2b 2b 3b 0a 20 20 20 20 69 66 28  gsize++;.    if(
8e5a0 20 69 42 75 64 64 79 3c 69 42 6c 6f 63 6b 20 29   iBuddy<iBlock )
8e5b0 7b 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61 43 74  {.      mem5.aCt
8e5c0 72 6c 5b 69 42 75 64 64 79 5d 20 3d 20 43 54 52  rl[iBuddy] = CTR
8e5d0 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73 69 7a  L_FREE | iLogsiz
8e5e0 65 3b 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61 43  e;.      mem5.aC
8e5f0 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 3d 20 30 3b  trl[iBlock] = 0;
8e600 0a 20 20 20 20 20 20 69 42 6c 6f 63 6b 20 3d 20  .      iBlock = 
8e610 69 42 75 64 64 79 3b 0a 20 20 20 20 7d 65 6c 73  iBuddy;.    }els
8e620 65 7b 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61 43  e{.      mem5.aC
8e630 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 3d 20 43 54  trl[iBlock] = CT
8e640 52 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73 69  RL_FREE | iLogsi
8e650 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61  ze;.      mem5.a
8e660 43 74 72 6c 5b 69 42 75 64 64 79 5d 20 3d 20 30  Ctrl[iBuddy] = 0
8e670 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 69 7a 65  ;.    }.    size
8e680 20 2a 3d 20 32 3b 0a 20 20 7d 0a 20 20 6d 65 6d   *= 2;.  }.  mem
8e690 73 79 73 35 4c 69 6e 6b 28 69 42 6c 6f 63 6b 2c  sys5Link(iBlock,
8e6a0 20 69 4c 6f 67 73 69 7a 65 29 3b 0a 7d 0a 0a 2f   iLogsize);.}../
8e6b0 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42  *.** Allocate nB
8e6c0 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a  ytes of memory.*
8e6d0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d  /.static void *m
8e6e0 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 28 69 6e 74  emsys5Malloc(int
8e6f0 20 6e 42 79 74 65 73 29 7b 0a 20 20 73 71 6c 69   nBytes){.  sqli
8e700 74 65 33 5f 69 6e 74 36 34 20 2a 70 20 3d 20 30  te3_int64 *p = 0
8e710 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3e 30  ;.  if( nBytes>0
8e720 20 29 7b 0a 20 20 20 20 6d 65 6d 73 79 73 35 45   ){.    memsys5E
8e730 6e 74 65 72 28 29 3b 0a 20 20 20 20 70 20 3d 20  nter();.    p = 
8e740 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 55 6e 73  memsys5MallocUns
8e750 61 66 65 28 6e 42 79 74 65 73 29 3b 0a 20 20 20  afe(nBytes);.   
8e760 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 29 3b   memsys5Leave();
8e770 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 76  .  }.  return (v
8e780 6f 69 64 2a 29 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a  oid*)p; .}../*.*
8e790 2a 20 46 72 65 65 20 6d 65 6d 6f 72 79 2e 0a 2a  * Free memory..*
8e7a0 2a 0a 2a 2a 20 54 68 65 20 6f 75 74 65 72 20 6c  *.** The outer l
8e7b0 61 79 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  ayer memory allo
8e7c0 63 61 74 6f 72 20 70 72 65 76 65 6e 74 73 20 74  cator prevents t
8e7d0 68 69 73 20 72 6f 75 74 69 6e 65 20 66 72 6f 6d  his routine from
8e7e0 0a 2a 2a 20 62 65 69 6e 67 20 63 61 6c 6c 65 64  .** being called
8e7f0 20 77 69 74 68 20 70 50 72 69 6f 72 3d 3d 30 2e   with pPrior==0.
8e800 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
8e810 6d 65 6d 73 79 73 35 46 72 65 65 28 76 6f 69 64  memsys5Free(void
8e820 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 61 73 73   *pPrior){.  ass
8e830 65 72 74 28 20 70 50 72 69 6f 72 21 3d 30 20 29  ert( pPrior!=0 )
8e840 3b 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72  ;.  memsys5Enter
8e850 28 29 3b 0a 20 20 6d 65 6d 73 79 73 35 46 72 65  ();.  memsys5Fre
8e860 65 55 6e 73 61 66 65 28 70 50 72 69 6f 72 29 3b  eUnsafe(pPrior);
8e870 0a 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28  .  memsys5Leave(
8e880 29 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68  );  .}../*.** Ch
8e890 61 6e 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66  ange the size of
8e8a0 20 61 6e 20 65 78 69 73 74 69 6e 67 20 6d 65 6d   an existing mem
8e8b0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a  ory allocation..
8e8c0 2a 2a 0a 2a 2a 20 54 68 65 20 6f 75 74 65 72 20  **.** The outer 
8e8d0 6c 61 79 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c  layer memory all
8e8e0 6f 63 61 74 6f 72 20 70 72 65 76 65 6e 74 73 20  ocator prevents 
8e8f0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66 72 6f  this routine fro
8e900 6d 0a 2a 2a 20 62 65 69 6e 67 20 63 61 6c 6c 65  m.** being calle
8e910 64 20 77 69 74 68 20 70 50 72 69 6f 72 3d 3d 30  d with pPrior==0
8e920 2e 20 20 0a 2a 2a 0a 2a 2a 20 6e 42 79 74 65 73  .  .**.** nBytes
8e930 20 69 73 20 61 6c 77 61 79 73 20 61 20 76 61 6c   is always a val
8e940 75 65 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ue obtained from
8e950 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f   a prior call to
8e960 0a 2a 2a 20 6d 65 6d 73 79 73 35 52 6f 75 6e 64  .** memsys5Round
8e970 28 29 2e 20 20 48 65 6e 63 65 20 6e 42 79 74 65  ().  Hence nByte
8e980 73 20 69 73 20 61 6c 77 61 79 73 20 61 20 6e 6f  s is always a no
8e990 6e 2d 6e 65 67 61 74 69 76 65 20 70 6f 77 65 72  n-negative power
8e9a0 0a 2a 2a 20 6f 66 20 74 77 6f 2e 20 20 49 66 20  .** of two.  If 
8e9b0 6e 42 79 74 65 73 3d 3d 30 20 74 68 61 74 20 6d  nBytes==0 that m
8e9c0 65 61 6e 73 20 74 68 61 74 20 61 6e 20 6f 76 65  eans that an ove
8e9d0 72 73 69 7a 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  rsize allocation
8e9e0 0a 2a 2a 20 28 61 6e 20 61 6c 6c 6f 63 61 74 69  .** (an allocati
8e9f0 6f 6e 20 6c 61 72 67 65 72 20 74 68 61 6e 20 30  on larger than 0
8ea00 78 34 30 30 30 30 30 30 30 29 20 77 61 73 20 72  x40000000) was r
8ea10 65 71 75 65 73 74 65 64 20 61 6e 64 20 74 68 69  equested and thi
8ea20 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 73 68 6f  s.** routine sho
8ea30 75 6c 64 20 72 65 74 75 72 6e 20 30 20 77 69 74  uld return 0 wit
8ea40 68 6f 75 74 20 66 72 65 65 69 6e 67 20 70 50 72  hout freeing pPr
8ea50 69 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ior..*/.static v
8ea60 6f 69 64 20 2a 6d 65 6d 73 79 73 35 52 65 61 6c  oid *memsys5Real
8ea70 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 6f 72  loc(void *pPrior
8ea80 2c 20 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20  , int nBytes){. 
8ea90 20 69 6e 74 20 6e 4f 6c 64 3b 0a 20 20 76 6f 69   int nOld;.  voi
8eaa0 64 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20  d *p;.  assert( 
8eab0 70 50 72 69 6f 72 21 3d 30 20 29 3b 0a 20 20 61  pPrior!=0 );.  a
8eac0 73 73 65 72 74 28 20 28 6e 42 79 74 65 73 26 28  ssert( (nBytes&(
8ead0 6e 42 79 74 65 73 2d 31 29 29 3d 3d 30 20 29 3b  nBytes-1))==0 );
8eae0 0a 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 65  .  assert( nByte
8eaf0 73 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 6e 42  s>=0 );.  if( nB
8eb00 79 74 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 72  ytes==0 ){.    r
8eb10 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e  eturn 0;.  }.  n
8eb20 4f 6c 64 20 3d 20 6d 65 6d 73 79 73 35 53 69 7a  Old = memsys5Siz
8eb30 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 69 66 28  e(pPrior);.  if(
8eb40 20 6e 42 79 74 65 73 3c 3d 6e 4f 6c 64 20 29 7b   nBytes<=nOld ){
8eb50 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50 72 69  .    return pPri
8eb60 6f 72 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73  or;.  }.  memsys
8eb70 35 45 6e 74 65 72 28 29 3b 0a 20 20 70 20 3d 20  5Enter();.  p = 
8eb80 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 55 6e 73  memsys5MallocUns
8eb90 61 66 65 28 6e 42 79 74 65 73 29 3b 0a 20 20 69  afe(nBytes);.  i
8eba0 66 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 63  f( p ){.    memc
8ebb0 70 79 28 70 2c 20 70 50 72 69 6f 72 2c 20 6e 4f  py(p, pPrior, nO
8ebc0 6c 64 29 3b 0a 20 20 20 20 6d 65 6d 73 79 73 35  ld);.    memsys5
8ebd0 46 72 65 65 55 6e 73 61 66 65 28 70 50 72 69 6f  FreeUnsafe(pPrio
8ebe0 72 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73  r);.  }.  memsys
8ebf0 35 4c 65 61 76 65 28 29 3b 0a 20 20 72 65 74 75  5Leave();.  retu
8ec00 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  rn p;.}../*.** R
8ec10 6f 75 6e 64 20 75 70 20 61 20 72 65 71 75 65 73  ound up a reques
8ec20 74 20 73 69 7a 65 20 74 6f 20 74 68 65 20 6e 65  t size to the ne
8ec30 78 74 20 76 61 6c 69 64 20 61 6c 6c 6f 63 61 74  xt valid allocat
8ec40 69 6f 6e 20 73 69 7a 65 2e 20 20 49 66 0a 2a 2a  ion size.  If.**
8ec50 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20   the allocation 
8ec60 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 74 6f 20  is too large to 
8ec70 62 65 20 68 61 6e 64 6c 65 64 20 62 79 20 74 68  be handled by th
8ec80 69 73 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 79  is allocation sy
8ec90 73 74 65 6d 2c 0a 2a 2a 20 72 65 74 75 72 6e 20  stem,.** return 
8eca0 30 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 61 6c 6c  0..**.** All all
8ecb0 6f 63 61 74 69 6f 6e 73 20 6d 75 73 74 20 62 65  ocations must be
8ecc0 20 61 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 20   a power of two 
8ecd0 61 6e 64 20 6d 75 73 74 20 62 65 20 65 78 70 72  and must be expr
8ece0 65 73 73 65 64 20 62 79 20 61 0a 2a 2a 20 33 32  essed by a.** 32
8ecf0 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65  -bit signed inte
8ed00 67 65 72 2e 20 20 48 65 6e 63 65 20 74 68 65 20  ger.  Hence the 
8ed10 6c 61 72 67 65 73 74 20 61 6c 6c 6f 63 61 74 69  largest allocati
8ed20 6f 6e 20 69 73 20 30 78 34 30 30 30 30 30 30 30  on is 0x40000000
8ed30 0a 2a 2a 20 6f 72 20 31 30 37 33 37 34 31 38 32  .** or 107374182
8ed40 34 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74  4 bytes..*/.stat
8ed50 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 52 6f  ic int memsys5Ro
8ed60 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20 20  undup(int n){.  
8ed70 69 6e 74 20 69 46 75 6c 6c 53 7a 3b 0a 20 20 69  int iFullSz;.  i
8ed80 66 28 20 6e 20 3e 20 30 78 34 30 30 30 30 30 30  f( n > 0x4000000
8ed90 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
8eda0 66 6f 72 28 69 46 75 6c 6c 53 7a 3d 6d 65 6d 35  for(iFullSz=mem5
8edb0 2e 73 7a 41 74 6f 6d 3b 20 69 46 75 6c 6c 53 7a  .szAtom; iFullSz
8edc0 3c 6e 3b 20 69 46 75 6c 6c 53 7a 20 2a 3d 20 32  <n; iFullSz *= 2
8edd0 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 46 75 6c  );.  return iFul
8ede0 6c 53 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  lSz;.}../*.** Re
8edf0 74 75 72 6e 20 74 68 65 20 63 65 69 6c 69 6e 67  turn the ceiling
8ee00 20 6f 66 20 74 68 65 20 6c 6f 67 61 72 69 74 68   of the logarith
8ee10 6d 20 62 61 73 65 20 32 20 6f 66 20 69 56 61 6c  m base 2 of iVal
8ee20 75 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c  ue..**.** Exampl
8ee30 65 73 3a 20 20 20 6d 65 6d 73 79 73 35 4c 6f 67  es:   memsys5Log
8ee40 28 31 29 20 2d 3e 20 30 0a 2a 2a 20 20 20 20 20  (1) -> 0.**     
8ee50 20 20 20 20 20 20 20 20 6d 65 6d 73 79 73 35 4c          memsys5L
8ee60 6f 67 28 32 29 20 2d 3e 20 31 0a 2a 2a 20 20 20  og(2) -> 1.**   
8ee70 20 20 20 20 20 20 20 20 20 20 6d 65 6d 73 79 73            memsys
8ee80 35 4c 6f 67 28 34 29 20 2d 3e 20 32 0a 2a 2a 20  5Log(4) -> 2.** 
8ee90 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 73              mems
8eea0 79 73 35 4c 6f 67 28 35 29 20 2d 3e 20 33 0a 2a  ys5Log(5) -> 3.*
8eeb0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65  *             me
8eec0 6d 73 79 73 35 4c 6f 67 28 38 29 20 2d 3e 20 33  msys5Log(8) -> 3
8eed0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
8eee0 6d 65 6d 73 79 73 35 4c 6f 67 28 39 29 20 2d 3e  memsys5Log(9) ->
8eef0 20 34 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74   4.*/.static int
8ef00 20 6d 65 6d 73 79 73 35 4c 6f 67 28 69 6e 74 20   memsys5Log(int 
8ef10 69 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 69  iValue){.  int i
8ef20 4c 6f 67 3b 0a 20 20 66 6f 72 28 69 4c 6f 67 3d  Log;.  for(iLog=
8ef30 30 3b 20 28 31 3c 3c 69 4c 6f 67 29 3c 69 56 61  0; (1<<iLog)<iVa
8ef40 6c 75 65 3b 20 69 4c 6f 67 2b 2b 29 3b 0a 20 20  lue; iLog++);.  
8ef50 72 65 74 75 72 6e 20 69 4c 6f 67 3b 0a 7d 0a 0a  return iLog;.}..
8ef60 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65  /*.** Initialize
8ef70 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
8ef80 63 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  cator..**.** Thi
8ef90 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f 74  s routine is not
8efa0 20 74 68 72 65 61 64 73 61 66 65 2e 20 20 54 68   threadsafe.  Th
8efb0 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 62 65  e caller must be
8efc0 20 68 6f 6c 64 69 6e 67 20 61 20 6d 75 74 65 78   holding a mutex
8efd0 0a 2a 2a 20 74 6f 20 70 72 65 76 65 6e 74 20 6d  .** to prevent m
8efe0 75 6c 74 69 70 6c 65 20 74 68 72 65 61 64 73 20  ultiple threads 
8eff0 66 72 6f 6d 20 65 6e 74 65 72 69 6e 67 20 61 74  from entering at
8f000 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a   the same time..
8f010 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65  */.static int me
8f020 6d 73 79 73 35 49 6e 69 74 28 76 6f 69 64 20 2a  msys5Init(void *
8f030 4e 6f 74 55 73 65 64 29 7b 0a 20 20 69 6e 74 20  NotUsed){.  int 
8f040 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ii;            /
8f050 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
8f060 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 20 20  /.  int nByte;  
8f070 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
8f080 20 6f 66 20 62 79 74 65 73 20 6f 66 20 6d 65 6d   of bytes of mem
8f090 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 74 6f  ory available to
8f0a0 20 74 68 69 73 20 61 6c 6c 6f 63 61 74 6f 72 20   this allocator 
8f0b0 2a 2f 0a 20 20 75 38 20 2a 7a 42 79 74 65 3b 20  */.  u8 *zByte; 
8f0c0 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72          /* Memor
8f0d0 79 20 75 73 61 62 6c 65 20 62 79 20 74 68 69 73  y usable by this
8f0e0 20 61 6c 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20 20   allocator */.  
8f0f0 69 6e 74 20 6e 4d 69 6e 4c 6f 67 3b 20 20 20 20  int nMinLog;    
8f100 20 20 20 2f 2a 20 4c 6f 67 20 62 61 73 65 20 32     /* Log base 2
8f110 20 6f 66 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f   of minimum allo
8f120 63 61 74 69 6f 6e 20 73 69 7a 65 20 69 6e 20 62  cation size in b
8f130 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 4f  ytes */.  int iO
8f140 66 66 73 65 74 3b 20 20 20 20 20 20 20 2f 2a 20  ffset;       /* 
8f150 41 6e 20 6f 66 66 73 65 74 20 69 6e 74 6f 20 6d  An offset into m
8f160 65 6d 35 2e 61 43 74 72 6c 5b 5d 20 2a 2f 0a 0a  em5.aCtrl[] */..
8f170 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
8f180 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 20  ER(NotUsed);..  
8f190 2f 2a 20 46 6f 72 20 74 68 65 20 70 75 72 70 6f  /* For the purpo
8f1a0 73 65 73 20 6f 66 20 74 68 69 73 20 72 6f 75 74  ses of this rout
8f1b0 69 6e 65 2c 20 64 69 73 61 62 6c 65 20 74 68 65  ine, disable the
8f1c0 20 6d 75 74 65 78 20 2a 2f 0a 20 20 6d 65 6d 35   mutex */.  mem5
8f1d0 2e 6d 75 74 65 78 20 3d 20 30 3b 0a 0a 20 20 2f  .mutex = 0;..  /
8f1e0 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 61 20  * The size of a 
8f1f0 4d 65 6d 35 4c 69 6e 6b 20 6f 62 6a 65 63 74 20  Mem5Link object 
8f200 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 20  must be a power 
8f210 6f 66 20 74 77 6f 2e 20 20 56 65 72 69 66 79 20  of two.  Verify 
8f220 74 68 61 74 0a 20 20 2a 2a 20 74 68 69 73 20 69  that.  ** this i
8f230 73 20 63 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 61  s case..  */.  a
8f240 73 73 65 72 74 28 20 28 73 69 7a 65 6f 66 28 4d  ssert( (sizeof(M
8f250 65 6d 35 4c 69 6e 6b 29 26 28 73 69 7a 65 6f 66  em5Link)&(sizeof
8f260 28 4d 65 6d 35 4c 69 6e 6b 29 2d 31 29 29 3d 3d  (Mem5Link)-1))==
8f270 30 20 29 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20  0 );..  nByte = 
8f280 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
8f290 66 69 67 2e 6e 48 65 61 70 3b 0a 20 20 7a 42 79  fig.nHeap;.  zBy
8f2a0 74 65 20 3d 20 28 75 38 2a 29 73 71 6c 69 74 65  te = (u8*)sqlite
8f2b0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48  3GlobalConfig.pH
8f2c0 65 61 70 3b 0a 20 20 61 73 73 65 72 74 28 20 7a  eap;.  assert( z
8f2d0 42 79 74 65 21 3d 30 20 29 3b 20 20 2f 2a 20 73  Byte!=0 );  /* s
8f2e0 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20  qlite3_config() 
8f2f0 64 6f 65 73 20 6e 6f 74 20 61 6c 6c 6f 77 20 6f  does not allow o
8f300 74 68 65 72 77 69 73 65 20 2a 2f 0a 0a 20 20 6e  therwise */..  n
8f310 4d 69 6e 4c 6f 67 20 3d 20 6d 65 6d 73 79 73 35  MinLog = memsys5
8f320 4c 6f 67 28 73 71 6c 69 74 65 33 47 6c 6f 62 61  Log(sqlite3Globa
8f330 6c 43 6f 6e 66 69 67 2e 6d 6e 52 65 71 29 3b 0a  lConfig.mnReq);.
8f340 20 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 3d 20    mem5.szAtom = 
8f350 28 31 3c 3c 6e 4d 69 6e 4c 6f 67 29 3b 0a 20 20  (1<<nMinLog);.  
8f360 77 68 69 6c 65 28 20 28 69 6e 74 29 73 69 7a 65  while( (int)size
8f370 6f 66 28 4d 65 6d 35 4c 69 6e 6b 29 3e 6d 65 6d  of(Mem5Link)>mem
8f380 35 2e 73 7a 41 74 6f 6d 20 29 7b 0a 20 20 20 20  5.szAtom ){.    
8f390 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 3d 20 6d 65  mem5.szAtom = me
8f3a0 6d 35 2e 73 7a 41 74 6f 6d 20 3c 3c 20 31 3b 0a  m5.szAtom << 1;.
8f3b0 20 20 7d 0a 0a 20 20 6d 65 6d 35 2e 6e 42 6c 6f    }..  mem5.nBlo
8f3c0 63 6b 20 3d 20 28 6e 42 79 74 65 20 2f 20 28 6d  ck = (nByte / (m
8f3d0 65 6d 35 2e 73 7a 41 74 6f 6d 2b 73 69 7a 65 6f  em5.szAtom+sizeo
8f3e0 66 28 75 38 29 29 29 3b 0a 20 20 6d 65 6d 35 2e  f(u8)));.  mem5.
8f3f0 7a 50 6f 6f 6c 20 3d 20 7a 42 79 74 65 3b 0a 20  zPool = zByte;. 
8f400 20 6d 65 6d 35 2e 61 43 74 72 6c 20 3d 20 28 75   mem5.aCtrl = (u
8f410 38 20 2a 29 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b  8 *)&mem5.zPool[
8f420 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 2a 6d 65 6d 35  mem5.nBlock*mem5
8f430 2e 73 7a 41 74 6f 6d 5d 3b 0a 0a 20 20 66 6f 72  .szAtom];..  for
8f440 28 69 69 3d 30 3b 20 69 69 3c 3d 4c 4f 47 4d 41  (ii=0; ii<=LOGMA
8f450 58 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 6d 65  X; ii++){.    me
8f460 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 69  m5.aiFreelist[ii
8f470 5d 20 3d 20 2d 31 3b 0a 20 20 7d 0a 0a 20 20 69  ] = -1;.  }..  i
8f480 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 66 6f  Offset = 0;.  fo
8f490 72 28 69 69 3d 4c 4f 47 4d 41 58 3b 20 69 69 3e  r(ii=LOGMAX; ii>
8f4a0 3d 30 3b 20 69 69 2d 2d 29 7b 0a 20 20 20 20 69  =0; ii--){.    i
8f4b0 6e 74 20 6e 41 6c 6c 6f 63 20 3d 20 28 31 3c 3c  nt nAlloc = (1<<
8f4c0 69 69 29 3b 0a 20 20 20 20 69 66 28 20 28 69 4f  ii);.    if( (iO
8f4d0 66 66 73 65 74 2b 6e 41 6c 6c 6f 63 29 3c 3d 6d  ffset+nAlloc)<=m
8f4e0 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 7b 0a 20 20  em5.nBlock ){.  
8f4f0 20 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69      mem5.aCtrl[i
8f500 4f 66 66 73 65 74 5d 20 3d 20 69 69 20 7c 20 43  Offset] = ii | C
8f510 54 52 4c 5f 46 52 45 45 3b 0a 20 20 20 20 20 20  TRL_FREE;.      
8f520 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 69 4f 66 66  memsys5Link(iOff
8f530 73 65 74 2c 20 69 69 29 3b 0a 20 20 20 20 20 20  set, ii);.      
8f540 69 4f 66 66 73 65 74 20 2b 3d 20 6e 41 6c 6c 6f  iOffset += nAllo
8f550 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  c;.    }.    ass
8f560 65 72 74 28 28 69 4f 66 66 73 65 74 2b 6e 41 6c  ert((iOffset+nAl
8f570 6c 6f 63 29 3e 6d 65 6d 35 2e 6e 42 6c 6f 63 6b  loc)>mem5.nBlock
8f580 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  );.  }..  /* If 
8f590 61 20 6d 75 74 65 78 20 69 73 20 72 65 71 75 69  a mutex is requi
8f5a0 72 65 64 20 66 6f 72 20 6e 6f 72 6d 61 6c 20 6f  red for normal o
8f5b0 70 65 72 61 74 69 6f 6e 2c 20 61 6c 6c 6f 63 61  peration, alloca
8f5c0 74 65 20 6f 6e 65 20 2a 2f 0a 20 20 69 66 28 20  te one */.  if( 
8f5d0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
8f5e0 66 69 67 2e 62 4d 65 6d 73 74 61 74 3d 3d 30 20  fig.bMemstat==0 
8f5f0 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 6d 75 74 65  ){.    mem5.mute
8f600 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78  x = sqlite3Mutex
8f610 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
8f620 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29 3b 0a  EX_STATIC_MEM);.
8f630 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51    }..  return SQ
8f640 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
8f650 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 20 74  * Deinitialize t
8f660 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73  his module..*/.s
8f670 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79  tatic void memsy
8f680 73 35 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 20  s5Shutdown(void 
8f690 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55  *NotUsed){.  UNU
8f6a0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f  SED_PARAMETER(No
8f6b0 74 55 73 65 64 29 3b 0a 20 20 6d 65 6d 35 2e 6d  tUsed);.  mem5.m
8f6c0 75 74 65 78 20 3d 20 30 3b 0a 20 20 72 65 74 75  utex = 0;.  retu
8f6d0 72 6e 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51  rn;.}..#ifdef SQ
8f6e0 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20  LITE_TEST./*.** 
8f6f0 4f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 69 6e  Open the file in
8f700 64 69 63 61 74 65 64 20 61 6e 64 20 77 72 69 74  dicated and writ
8f710 65 20 61 20 6c 6f 67 20 6f 66 20 61 6c 6c 20 75  e a log of all u
8f720 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 0a 2a  nfreed memory .*
8f730 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 6e  * allocations in
8f740 74 6f 20 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a  to that log..*/.
8f750 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
8f760 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 73 79  oid sqlite3Memsy
8f770 73 35 44 75 6d 70 28 63 6f 6e 73 74 20 63 68 61  s5Dump(const cha
8f780 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20  r *zFilename){. 
8f790 20 46 49 4c 45 20 2a 6f 75 74 3b 0a 20 20 69 6e   FILE *out;.  in
8f7a0 74 20 69 2c 20 6a 2c 20 6e 3b 0a 20 20 69 6e 74  t i, j, n;.  int
8f7b0 20 6e 4d 69 6e 4c 6f 67 3b 0a 0a 20 20 69 66 28   nMinLog;..  if(
8f7c0 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 7c 7c   zFilename==0 ||
8f7d0 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 3d 3d 30   zFilename[0]==0
8f7e0 20 29 7b 0a 20 20 20 20 6f 75 74 20 3d 20 73 74   ){.    out = st
8f7f0 64 6f 75 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  dout;.  }else{. 
8f800 20 20 20 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a     out = fopen(z
8f810 46 69 6c 65 6e 61 6d 65 2c 20 22 77 22 29 3b 0a  Filename, "w");.
8f820 20 20 20 20 69 66 28 20 6f 75 74 3d 3d 30 20 29      if( out==0 )
8f830 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28  {.      fprintf(
8f840 73 74 64 65 72 72 2c 20 22 2a 2a 20 55 6e 61 62  stderr, "** Unab
8f850 6c 65 20 74 6f 20 6f 75 74 70 75 74 20 6d 65 6d  le to output mem
8f860 6f 72 79 20 64 65 62 75 67 20 6f 75 74 70 75 74  ory debug output
8f870 20 6c 6f 67 3a 20 25 73 20 2a 2a 5c 6e 22 2c 0a   log: %s **\n",.
8f880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8f890 20 20 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 29        zFilename)
8f8a0 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  ;.      return;.
8f8b0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73      }.  }.  mems
8f8c0 79 73 35 45 6e 74 65 72 28 29 3b 0a 20 20 6e 4d  ys5Enter();.  nM
8f8d0 69 6e 4c 6f 67 20 3d 20 6d 65 6d 73 79 73 35 4c  inLog = memsys5L
8f8e0 6f 67 28 6d 65 6d 35 2e 73 7a 41 74 6f 6d 29 3b  og(mem5.szAtom);
8f8f0 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 4c  .  for(i=0; i<=L
8f900 4f 47 4d 41 58 20 26 26 20 69 2b 6e 4d 69 6e 4c  OGMAX && i+nMinL
8f910 6f 67 3c 33 32 3b 20 69 2b 2b 29 7b 0a 20 20 20  og<32; i++){.   
8f920 20 66 6f 72 28 6e 3d 30 2c 20 6a 3d 6d 65 6d 35   for(n=0, j=mem5
8f930 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 5d 3b 20  .aiFreelist[i]; 
8f940 6a 3e 3d 30 3b 20 6a 20 3d 20 4d 45 4d 35 4c 49  j>=0; j = MEM5LI
8f950 4e 4b 28 6a 29 2d 3e 6e 65 78 74 2c 20 6e 2b 2b  NK(j)->next, n++
8f960 29 7b 7d 0a 20 20 20 20 66 70 72 69 6e 74 66 28  ){}.    fprintf(
8f970 6f 75 74 2c 20 22 66 72 65 65 6c 69 73 74 20 69  out, "freelist i
8f980 74 65 6d 73 20 6f 66 20 73 69 7a 65 20 25 64 3a  tems of size %d:
8f990 20 25 64 5c 6e 22 2c 20 6d 65 6d 35 2e 73 7a 41   %d\n", mem5.szA
8f9a0 74 6f 6d 20 3c 3c 20 69 2c 20 6e 29 3b 0a 20 20  tom << i, n);.  
8f9b0 7d 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c  }.  fprintf(out,
8f9c0 20 22 6d 65 6d 35 2e 6e 41 6c 6c 6f 63 20 20 20   "mem5.nAlloc   
8f9d0 20 20 20 20 3d 20 25 6c 6c 75 5c 6e 22 2c 20 6d      = %llu\n", m
8f9e0 65 6d 35 2e 6e 41 6c 6c 6f 63 29 3b 0a 20 20 66  em5.nAlloc);.  f
8f9f0 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d  printf(out, "mem
8fa00 35 2e 74 6f 74 61 6c 41 6c 6c 6f 63 20 20 20 3d  5.totalAlloc   =
8fa10 20 25 6c 6c 75 5c 6e 22 2c 20 6d 65 6d 35 2e 74   %llu\n", mem5.t
8fa20 6f 74 61 6c 41 6c 6c 6f 63 29 3b 0a 20 20 66 70  otalAlloc);.  fp
8fa30 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35  rintf(out, "mem5
8fa40 2e 74 6f 74 61 6c 45 78 63 65 73 73 20 20 3d 20  .totalExcess  = 
8fa50 25 6c 6c 75 5c 6e 22 2c 20 6d 65 6d 35 2e 74 6f  %llu\n", mem5.to
8fa60 74 61 6c 45 78 63 65 73 73 29 3b 0a 20 20 66 70  talExcess);.  fp
8fa70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35  rintf(out, "mem5
8fa80 2e 63 75 72 72 65 6e 74 4f 75 74 20 20 20 3d 20  .currentOut   = 
8fa90 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e 63 75 72 72  %u\n", mem5.curr
8faa0 65 6e 74 4f 75 74 29 3b 0a 20 20 66 70 72 69 6e  entOut);.  fprin
8fab0 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 63 75  tf(out, "mem5.cu
8fac0 72 72 65 6e 74 43 6f 75 6e 74 20 3d 20 25 75 5c  rrentCount = %u\
8fad0 6e 22 2c 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74  n", mem5.current
8fae0 43 6f 75 6e 74 29 3b 0a 20 20 66 70 72 69 6e 74  Count);.  fprint
8faf0 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6d 61 78  f(out, "mem5.max
8fb00 4f 75 74 20 20 20 20 20 20 20 3d 20 25 75 5c 6e  Out       = %u\n
8fb10 22 2c 20 6d 65 6d 35 2e 6d 61 78 4f 75 74 29 3b  ", mem5.maxOut);
8fb20 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20  .  fprintf(out, 
8fb30 22 6d 65 6d 35 2e 6d 61 78 43 6f 75 6e 74 20 20  "mem5.maxCount  
8fb40 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35     = %u\n", mem5
8fb50 2e 6d 61 78 43 6f 75 6e 74 29 3b 0a 20 20 66 70  .maxCount);.  fp
8fb60 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35  rintf(out, "mem5
8fb70 2e 6d 61 78 52 65 71 75 65 73 74 20 20 20 3d 20  .maxRequest   = 
8fb80 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6d 61 78 52  %u\n", mem5.maxR
8fb90 65 71 75 65 73 74 29 3b 0a 20 20 6d 65 6d 73 79  equest);.  memsy
8fba0 73 35 4c 65 61 76 65 28 29 3b 0a 20 20 69 66 28  s5Leave();.  if(
8fbb0 20 6f 75 74 3d 3d 73 74 64 6f 75 74 20 29 7b 0a   out==stdout ){.
8fbc0 20 20 20 20 66 66 6c 75 73 68 28 73 74 64 6f 75      fflush(stdou
8fbd0 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  t);.  }else{.   
8fbe0 20 66 63 6c 6f 73 65 28 6f 75 74 29 3b 0a 20 20   fclose(out);.  
8fbf0 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  }.}.#endif../*.*
8fc00 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
8fc10 73 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69  s the only routi
8fc20 6e 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  ne in this file 
8fc30 77 69 74 68 20 65 78 74 65 72 6e 61 6c 20 0a 2a  with external .*
8fc40 2a 20 6c 69 6e 6b 61 67 65 2e 20 49 74 20 72 65  * linkage. It re
8fc50 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  turns a pointer 
8fc60 74 6f 20 61 20 73 74 61 74 69 63 20 73 71 6c 69  to a static sqli
8fc70 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 0a  te3_mem_methods.
8fc80 2a 2a 20 73 74 72 75 63 74 20 70 6f 70 75 6c 61  ** struct popula
8fc90 74 65 64 20 77 69 74 68 20 74 68 65 20 6d 65 6d  ted with the mem
8fca0 73 79 73 35 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f  sys5 methods..*/
8fcb0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
8fcc0 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65  const sqlite3_me
8fcd0 6d 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74  m_methods *sqlit
8fce0 65 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73 35 28  e3MemGetMemsys5(
8fcf0 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20  void){.  static 
8fd00 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65  const sqlite3_me
8fd10 6d 5f 6d 65 74 68 6f 64 73 20 6d 65 6d 73 79 73  m_methods memsys
8fd20 35 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20  5Methods = {.   
8fd30 20 20 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 2c    memsys5Malloc,
8fd40 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 46 72 65  .     memsys5Fre
8fd50 65 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 52  e,.     memsys5R
8fd60 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 6d  ealloc,.     mem
8fd70 73 79 73 35 53 69 7a 65 2c 0a 20 20 20 20 20 6d  sys5Size,.     m
8fd80 65 6d 73 79 73 35 52 6f 75 6e 64 75 70 2c 0a 20  emsys5Roundup,. 
8fd90 20 20 20 20 6d 65 6d 73 79 73 35 49 6e 69 74 2c      memsys5Init,
8fda0 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 53 68 75  .     memsys5Shu
8fdb0 74 64 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20  tdown,.     0.  
8fdc0 7d 3b 0a 20 20 72 65 74 75 72 6e 20 26 6d 65 6d  };.  return &mem
8fdd0 73 79 73 35 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a  sys5Methods;.}..
8fde0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
8fdf0 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20  _ENABLE_MEMSYS5 
8fe00 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
8fe10 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 6d 35 2e  *** End of mem5.
8fe20 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
8fe30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8fe40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8fe50 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
8fe60 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d  *** Begin file m
8fe70 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  utex.c *********
8fe80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8fe90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8fea0 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41  **/./*.** 2007 A
8feb0 75 67 75 73 74 20 31 34 0a 2a 2a 0a 2a 2a 20 54  ugust 14.**.** T
8fec0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
8fed0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
8fee0 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
8fef0 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
8ff00 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
8ff10 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
8ff20 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
8ff30 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
8ff40 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
8ff50 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
8ff60 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
8ff70 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
8ff80 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
8ff90 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
8ffa0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
8ffb0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
8ffc0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
8ffd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8ffe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8fff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90010 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
90020 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68  file contains th
90030 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68  e C functions th
90040 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74  at implement mut
90050 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  exes..**.** This
90060 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
90070 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d  ode that is comm
90080 6f 6e 20 61 63 72 6f 73 73 20 61 6c 6c 20 6d 75  on across all mu
90090 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  tex implementati
900a0 6f 6e 73 2e 0a 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  ons...**.** $Id:
900b0 20 6d 75 74 65 78 2e 63 2c 76 20 31 2e 33 31 20   mutex.c,v 1.31 
900c0 32 30 30 39 2f 30 37 2f 31 36 20 31 38 3a 32 31  2009/07/16 18:21
900d0 3a 31 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :18 drh Exp $.*/
900e0 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  ..#if defined(SQ
900f0 4c 49 54 45 5f 44 45 42 55 47 29 20 26 26 20 21  LITE_DEBUG) && !
90100 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d  defined(SQLITE_M
90110 55 54 45 58 5f 4f 4d 49 54 29 0a 2f 2a 0a 2a 2a  UTEX_OMIT)./*.**
90120 20 46 6f 72 20 64 65 62 75 67 67 69 6e 67 20 70   For debugging p
90130 75 72 70 6f 73 65 73 2c 20 72 65 63 6f 72 64 20  urposes, record 
90140 77 68 65 6e 20 74 68 65 20 6d 75 74 65 78 20 73  when the mutex s
90150 75 62 73 79 73 74 65 6d 20 69 73 20 69 6e 69 74  ubsystem is init
90160 69 61 6c 69 7a 65 64 0a 2a 2a 20 61 6e 64 20 75  ialized.** and u
90170 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 73 6f 20  ninitialized so 
90180 74 68 61 74 20 77 65 20 63 61 6e 20 61 73 73 65  that we can asse
90190 72 74 28 29 20 69 66 20 74 68 65 72 65 20 69 73  rt() if there is
901a0 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a 2a   an attempt to.*
901b0 2a 20 61 6c 6c 6f 63 61 74 65 20 61 20 6d 75 74  * allocate a mut
901c0 65 78 20 77 68 69 6c 65 20 74 68 65 20 73 79 73  ex while the sys
901d0 74 65 6d 20 69 73 20 75 6e 69 6e 69 74 69 61 6c  tem is uninitial
901e0 69 7a 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ized..*/.static 
901f0 53 51 4c 49 54 45 5f 57 53 44 20 69 6e 74 20 6d  SQLITE_WSD int m
90200 75 74 65 78 49 73 49 6e 69 74 20 3d 20 30 3b 0a  utexIsInit = 0;.
90210 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
90220 5f 44 45 42 55 47 20 2a 2f 0a 0a 0a 23 69 66 6e  _DEBUG */...#ifn
90230 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  def SQLITE_MUTEX
90240 5f 4f 4d 49 54 0a 2f 2a 0a 2a 2a 20 49 6e 69 74  _OMIT./*.** Init
90250 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78  ialize the mutex
90260 20 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49   system..*/.SQLI
90270 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
90280 71 6c 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28  qlite3MutexInit(
90290 76 6f 69 64 29 7b 20 0a 20 20 69 6e 74 20 72 63  void){ .  int rc
902a0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
902b0 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
902c0 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74  lConfig.bCoreMut
902d0 65 78 20 29 7b 0a 20 20 20 20 69 66 28 20 21 73  ex ){.    if( !s
902e0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
902f0 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 41  ig.mutex.xMutexA
90300 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 2f 2a  lloc ){.      /*
90310 20 49 66 20 74 68 65 20 78 4d 75 74 65 78 41 6c   If the xMutexAl
90320 6c 6f 63 20 6d 65 74 68 6f 64 20 68 61 73 20 6e  loc method has n
90330 6f 74 20 62 65 65 6e 20 73 65 74 2c 20 74 68 65  ot been set, the
90340 6e 20 74 68 65 20 75 73 65 72 20 64 69 64 20 6e  n the user did n
90350 6f 74 0a 20 20 20 20 20 20 2a 2a 20 69 6e 73 74  ot.      ** inst
90360 61 6c 6c 20 61 20 6d 75 74 65 78 20 69 6d 70 6c  all a mutex impl
90370 65 6d 65 6e 74 61 74 69 6f 6e 20 76 69 61 20 73  ementation via s
90380 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20  qlite3_config() 
90390 70 72 69 6f 72 20 74 6f 20 0a 20 20 20 20 20 20  prior to .      
903a0 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  ** sqlite3_initi
903b0 61 6c 69 7a 65 28 29 20 62 65 69 6e 67 20 63 61  alize() being ca
903c0 6c 6c 65 64 2e 20 54 68 69 73 20 62 6c 6f 63 6b  lled. This block
903d0 20 63 6f 70 69 65 73 20 70 6f 69 6e 74 65 72 73   copies pointers
903e0 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65   to.      ** the
903f0 20 64 65 66 61 75 6c 74 20 69 6d 70 6c 65 6d 65   default impleme
90400 6e 74 61 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65  ntation into the
90410 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
90420 6e 66 69 67 20 73 74 72 75 63 74 75 72 65 2e 0a  nfig structure..
90430 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73        */.      s
90440 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74  qlite3_mutex_met
90450 68 6f 64 73 20 2a 70 46 72 6f 6d 20 3d 20 73 71  hods *pFrom = sq
90460 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65  lite3DefaultMute
90470 78 28 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  x();.      sqlit
90480 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73  e3_mutex_methods
90490 20 2a 70 54 6f 20 3d 20 26 73 71 6c 69 74 65 33   *pTo = &sqlite3
904a0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74  GlobalConfig.mut
904b0 65 78 3b 0a 0a 20 20 20 20 20 20 6d 65 6d 63 70  ex;..      memcp
904c0 79 28 70 54 6f 2c 20 70 46 72 6f 6d 2c 20 6f 66  y(pTo, pFrom, of
904d0 66 73 65 74 6f 66 28 73 71 6c 69 74 65 33 5f 6d  fsetof(sqlite3_m
904e0 75 74 65 78 5f 6d 65 74 68 6f 64 73 2c 20 78 4d  utex_methods, xM
904f0 75 74 65 78 41 6c 6c 6f 63 29 29 3b 0a 20 20 20  utexAlloc));.   
90500 20 20 20 6d 65 6d 63 70 79 28 26 70 54 6f 2d 3e     memcpy(&pTo->
90510 78 4d 75 74 65 78 46 72 65 65 2c 20 26 70 46 72  xMutexFree, &pFr
90520 6f 6d 2d 3e 78 4d 75 74 65 78 46 72 65 65 2c 0a  om->xMutexFree,.
90530 20 20 20 20 20 20 20 20 20 20 20 20 20 73 69 7a               siz
90540 65 6f 66 28 2a 70 54 6f 29 20 2d 20 6f 66 66 73  eof(*pTo) - offs
90550 65 74 6f 66 28 73 71 6c 69 74 65 33 5f 6d 75 74  etof(sqlite3_mut
90560 65 78 5f 6d 65 74 68 6f 64 73 2c 20 78 4d 75 74  ex_methods, xMut
90570 65 78 46 72 65 65 29 29 3b 0a 20 20 20 20 20 20  exFree));.      
90580 70 54 6f 2d 3e 78 4d 75 74 65 78 41 6c 6c 6f 63  pTo->xMutexAlloc
90590 20 3d 20 70 46 72 6f 6d 2d 3e 78 4d 75 74 65 78   = pFrom->xMutex
905a0 41 6c 6c 6f 63 3b 0a 20 20 20 20 7d 0a 20 20 20  Alloc;.    }.   
905b0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f   rc = sqlite3Glo
905c0 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e  balConfig.mutex.
905d0 78 4d 75 74 65 78 49 6e 69 74 28 29 3b 0a 20 20  xMutexInit();.  
905e0 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  }..#ifdef SQLITE
905f0 5f 44 45 42 55 47 0a 20 20 47 4c 4f 42 41 4c 28  _DEBUG.  GLOBAL(
90600 69 6e 74 2c 20 6d 75 74 65 78 49 73 49 6e 69 74  int, mutexIsInit
90610 29 20 3d 20 31 3b 0a 23 65 6e 64 69 66 0a 0a 20  ) = 1;.#endif.. 
90620 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
90630 2a 0a 2a 2a 20 53 68 75 74 64 6f 77 6e 20 74 68  *.** Shutdown th
90640 65 20 6d 75 74 65 78 20 73 79 73 74 65 6d 2e 20  e mutex system. 
90650 54 68 69 73 20 63 61 6c 6c 20 66 72 65 65 73 20  This call frees 
90660 72 65 73 6f 75 72 63 65 73 20 61 6c 6c 6f 63 61  resources alloca
90670 74 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65  ted by.** sqlite
90680 33 4d 75 74 65 78 49 6e 69 74 28 29 2e 0a 2a 2f  3MutexInit()..*/
90690 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
906a0 69 6e 74 20 73 71 6c 69 74 65 33 4d 75 74 65 78  int sqlite3Mutex
906b0 45 6e 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74  End(void){.  int
906c0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
906d0 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c  .  if( sqlite3Gl
906e0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78  obalConfig.mutex
906f0 2e 78 4d 75 74 65 78 45 6e 64 20 29 7b 0a 20 20  .xMutexEnd ){.  
90700 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 47 6c    rc = sqlite3Gl
90710 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78  obalConfig.mutex
90720 2e 78 4d 75 74 65 78 45 6e 64 28 29 3b 0a 20 20  .xMutexEnd();.  
90730 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  }..#ifdef SQLITE
90740 5f 44 45 42 55 47 0a 20 20 47 4c 4f 42 41 4c 28  _DEBUG.  GLOBAL(
90750 69 6e 74 2c 20 6d 75 74 65 78 49 73 49 6e 69 74  int, mutexIsInit
90760 29 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20  ) = 0;.#endif.. 
90770 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
90780 2a 0a 2a 2a 20 52 65 74 72 69 65 76 65 20 61 20  *.** Retrieve a 
90790 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61  pointer to a sta
907a0 74 69 63 20 6d 75 74 65 78 20 6f 72 20 61 6c 6c  tic mutex or all
907b0 6f 63 61 74 65 20 61 20 6e 65 77 20 64 79 6e 61  ocate a new dyna
907c0 6d 69 63 20 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c 49  mic one..*/.SQLI
907d0 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 6d  TE_API sqlite3_m
907e0 75 74 65 78 20 2a 73 71 6c 69 74 65 33 5f 6d 75  utex *sqlite3_mu
907f0 74 65 78 5f 61 6c 6c 6f 63 28 69 6e 74 20 69 64  tex_alloc(int id
90800 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ){.#ifndef SQLIT
90810 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a  E_OMIT_AUTOINIT.
90820 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e    if( sqlite3_in
90830 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65 74  itialize() ) ret
90840 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20  urn 0;.#endif.  
90850 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c  return sqlite3Gl
90860 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78  obalConfig.mutex
90870 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 28 69 64 29  .xMutexAlloc(id)
90880 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  ;.}..SQLITE_PRIV
90890 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ATE sqlite3_mute
908a0 78 20 2a 73 71 6c 69 74 65 33 4d 75 74 65 78 41  x *sqlite3MutexA
908b0 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 0a 20 20  lloc(int id){.  
908c0 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f 62  if( !sqlite3Glob
908d0 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75  alConfig.bCoreMu
908e0 74 65 78 20 29 7b 0a 20 20 20 20 72 65 74 75 72  tex ){.    retur
908f0 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  n 0;.  }.  asser
90900 74 28 20 47 4c 4f 42 41 4c 28 69 6e 74 2c 20 6d  t( GLOBAL(int, m
90910 75 74 65 78 49 73 49 6e 69 74 29 20 29 3b 0a 20  utexIsInit) );. 
90920 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47   return sqlite3G
90930 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65  lobalConfig.mute
90940 78 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 28 69 64  x.xMutexAlloc(id
90950 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65  );.}../*.** Free
90960 20 61 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78   a dynamic mutex
90970 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
90980 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74  void sqlite3_mut
90990 65 78 5f 66 72 65 65 28 73 71 6c 69 74 65 33 5f  ex_free(sqlite3_
909a0 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 66 28  mutex *p){.  if(
909b0 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65   p ){.    sqlite
909c0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75  3GlobalConfig.mu
909d0 74 65 78 2e 78 4d 75 74 65 78 46 72 65 65 28 70  tex.xMutexFree(p
909e0 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
909f0 4f 62 74 61 69 6e 20 74 68 65 20 6d 75 74 65 78  Obtain the mutex
90a00 20 70 2e 20 49 66 20 73 6f 6d 65 20 6f 74 68 65   p. If some othe
90a10 72 20 74 68 72 65 61 64 20 61 6c 72 65 61 64 79  r thread already
90a20 20 68 61 73 20 74 68 65 20 6d 75 74 65 78 2c 20   has the mutex, 
90a30 62 6c 6f 63 6b 0a 2a 2a 20 75 6e 74 69 6c 20 69  block.** until i
90a40 74 20 63 61 6e 20 62 65 20 6f 62 74 61 69 6e 65  t can be obtaine
90a50 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  d..*/.SQLITE_API
90a60 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75   void sqlite3_mu
90a70 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65  tex_enter(sqlite
90a80 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69  3_mutex *p){.  i
90a90 66 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69  f( p ){.    sqli
90aa0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
90ab0 6d 75 74 65 78 2e 78 4d 75 74 65 78 45 6e 74 65  mutex.xMutexEnte
90ac0 72 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  r(p);.  }.}../*.
90ad0 2a 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 6d 75  ** Obtain the mu
90ae0 74 65 78 20 70 2e 20 49 66 20 73 75 63 63 65 73  tex p. If succes
90af0 73 66 75 6c 2c 20 72 65 74 75 72 6e 20 53 51 4c  sful, return SQL
90b00 49 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73  ITE_OK. Otherwis
90b10 65 2c 20 69 66 20 61 6e 6f 74 68 65 72 0a 2a 2a  e, if another.**
90b20 20 74 68 72 65 61 64 20 68 6f 6c 64 73 20 74 68   thread holds th
90b30 65 20 6d 75 74 65 78 20 61 6e 64 20 69 74 20 63  e mutex and it c
90b40 61 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65  annot be obtaine
90b50 64 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  d, return SQLITE
90b60 5f 42 55 53 59 2e 0a 2a 2f 0a 53 51 4c 49 54 45  _BUSY..*/.SQLITE
90b70 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
90b80 5f 6d 75 74 65 78 5f 74 72 79 28 73 71 6c 69 74  _mutex_try(sqlit
90b90 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
90ba0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
90bb0 4f 4b 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  OK;.  if( p ){. 
90bc0 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65     return sqlite
90bd0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75  3GlobalConfig.mu
90be0 74 65 78 2e 78 4d 75 74 65 78 54 72 79 28 70 29  tex.xMutexTry(p)
90bf0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
90c00 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  c;.}../*.** The 
90c10 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
90c20 61 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78  ave() routine ex
90c30 69 74 73 20 61 20 6d 75 74 65 78 20 74 68 61 74  its a mutex that
90c40 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 0a   was previously.
90c50 2a 2a 20 65 6e 74 65 72 65 64 20 62 79 20 74 68  ** entered by th
90c60 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20  e same thread.  
90c70 54 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20  The behavior is 
90c80 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65  undefined if the
90c90 20 6d 75 74 65 78 20 0a 2a 2a 20 69 73 20 6e 6f   mutex .** is no
90ca0 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65  t currently ente
90cb0 72 65 64 2e 20 49 66 20 61 20 4e 55 4c 4c 20 70  red. If a NULL p
90cc0 6f 69 6e 74 65 72 20 69 73 20 70 61 73 73 65 64  ointer is passed
90cd0 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 0a   as an argument.
90ce0 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** this function
90cf0 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a   is a no-op..*/.
90d00 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
90d10 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
90d20 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  ave(sqlite3_mute
90d30 78 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29  x *p){.  if( p )
90d40 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f  {.    sqlite3Glo
90d50 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e  balConfig.mutex.
90d60 78 4d 75 74 65 78 4c 65 61 76 65 28 70 29 3b 0a  xMutexLeave(p);.
90d70 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e    }.}..#ifndef N
90d80 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20  DEBUG./*.** The 
90d90 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
90da0 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  ld() and sqlite3
90db0 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29  _mutex_notheld()
90dc0 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20   routine are.** 
90dd0 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65  intended for use
90de0 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29   inside assert()
90df0 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a   statements..*/.
90e00 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
90e10 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
90e20 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  d(sqlite3_mutex 
90e30 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d  *p){.  return p=
90e40 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f  =0 || sqlite3Glo
90e50 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e  balConfig.mutex.
90e60 78 4d 75 74 65 78 48 65 6c 64 28 70 29 3b 0a 7d  xMutexHeld(p);.}
90e70 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
90e80 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f  sqlite3_mutex_no
90e90 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75  theld(sqlite3_mu
90ea0 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72  tex *p){.  retur
90eb0 6e 20 70 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65  n p==0 || sqlite
90ec0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75  3GlobalConfig.mu
90ed0 74 65 78 2e 78 4d 75 74 65 78 4e 6f 74 68 65 6c  tex.xMutexNothel
90ee0 64 28 70 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  d(p);.}.#endif..
90ef0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
90f00 5f 4f 4d 49 54 5f 4d 55 54 45 58 20 2a 2f 0a 0a  _OMIT_MUTEX */..
90f10 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
90f20 45 6e 64 20 6f 66 20 6d 75 74 65 78 2e 63 20 2a  End of mutex.c *
90f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
90f60 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
90f70 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78  Begin file mutex
90f80 5f 6e 6f 6f 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  _noop.c ********
90f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
90fb0 2f 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 74 6f 62  /*.** 2008 Octob
90fc0 65 72 20 30 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 07.**.** The 
90fd0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
90fe0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
90ff0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
91000 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
91010 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
91020 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
91030 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
91040 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
91050 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
91060 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
91070 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
91080 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
91090 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
910a0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
910b0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
910c0 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
910d0 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
910e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
910f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91120 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
91130 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43  e contains the C
91140 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20   functions that 
91150 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65  implement mutexe
91160 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d  s..**.** This im
91170 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 20  plementation in 
91180 74 68 69 73 20 66 69 6c 65 20 64 6f 65 73 20 6e  this file does n
91190 6f 74 20 70 72 6f 76 69 64 65 20 61 6e 79 20 6d  ot provide any m
911a0 75 74 75 61 6c 0a 2a 2a 20 65 78 63 6c 75 73 69  utual.** exclusi
911b0 6f 6e 20 61 6e 64 20 69 73 20 74 68 75 73 20 73  on and is thus s
911c0 75 69 74 61 62 6c 65 20 66 6f 72 20 75 73 65 20  uitable for use 
911d0 6f 6e 6c 79 20 69 6e 20 61 70 70 6c 69 63 61 74  only in applicat
911e0 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 75 73 65  ions.** that use
911f0 20 53 51 4c 69 74 65 20 69 6e 20 61 20 73 69 6e   SQLite in a sin
91200 67 6c 65 20 74 68 72 65 61 64 2e 20 20 54 68 65  gle thread.  The
91210 20 72 6f 75 74 69 6e 65 73 20 64 65 66 69 6e 65   routines define
91220 64 0a 2a 2a 20 68 65 72 65 20 61 72 65 20 70 6c  d.** here are pl
91230 61 63 65 2d 68 6f 6c 64 65 72 73 2e 20 20 41 70  ace-holders.  Ap
91240 70 6c 69 63 61 74 69 6f 6e 73 20 63 61 6e 20 73  plications can s
91250 75 62 73 74 69 74 75 74 65 20 77 6f 72 6b 69 6e  ubstitute workin
91260 67 0a 2a 2a 20 6d 75 74 65 78 20 72 6f 75 74 69  g.** mutex routi
91270 6e 65 73 20 61 74 20 73 74 61 72 74 2d 74 69 6d  nes at start-tim
91280 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 0a 2a  e using the.**.*
91290 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f  *     sqlite3_co
912a0 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46  nfig(SQLITE_CONF
912b0 49 47 5f 4d 55 54 45 58 2c 2e 2e 2e 29 0a 2a 2a  IG_MUTEX,...).**
912c0 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a  .** interface..*
912d0 2a 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 65 64  *.** If compiled
912e0 20 77 69 74 68 20 53 51 4c 49 54 45 5f 44 45 42   with SQLITE_DEB
912f0 55 47 2c 20 74 68 65 6e 20 61 64 64 69 74 69 6f  UG, then additio
91300 6e 61 6c 20 6c 6f 67 69 63 20 69 73 20 69 6e 73  nal logic is ins
91310 65 72 74 65 64 0a 2a 2a 20 74 68 61 74 20 64 6f  erted.** that do
91320 65 73 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e  es error checkin
91330 67 20 6f 6e 20 6d 75 74 65 78 65 73 20 74 6f 20  g on mutexes to 
91340 6d 61 6b 65 20 73 75 72 65 20 74 68 65 79 20 61  make sure they a
91350 72 65 20 62 65 69 6e 67 0a 2a 2a 20 63 61 6c 6c  re being.** call
91360 65 64 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a  ed correctly..**
91370 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 5f 6e  .** $Id: mutex_n
91380 6f 6f 70 2e 63 2c 76 20 31 2e 33 20 32 30 30 38  oop.c,v 1.3 2008
91390 2f 31 32 2f 30 35 20 31 37 3a 31 37 3a 30 38 20  /12/05 17:17:08 
913a0 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 23  drh Exp $.*/...#
913b0 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
913c0 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26  E_MUTEX_NOOP) &&
913d0 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
913e0 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 53 74  _DEBUG)./*.** St
913f0 75 62 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20  ub routines for 
91400 61 6c 6c 20 6d 75 74 65 78 20 6d 65 74 68 6f 64  all mutex method
91410 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  s..**.** This ro
91420 75 74 69 6e 65 73 20 70 72 6f 76 69 64 65 20 6e  utines provide n
91430 6f 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 73 69  o mutual exclusi
91440 6f 6e 20 6f 72 20 65 72 72 6f 72 20 63 68 65 63  on or error chec
91450 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  king..*/.static 
91460 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 48 65 6c  int noopMutexHel
91470 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  d(sqlite3_mutex 
91480 2a 70 29 7b 20 72 65 74 75 72 6e 20 31 3b 20 7d  *p){ return 1; }
91490 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70  .static int noop
914a0 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c  MutexNotheld(sql
914b0 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20  ite3_mutex *p){ 
914c0 72 65 74 75 72 6e 20 31 3b 20 7d 0a 73 74 61 74  return 1; }.stat
914d0 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78  ic int noopMutex
914e0 49 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75  Init(void){ retu
914f0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a  rn SQLITE_OK; }.
91500 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d  static int noopM
91510 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72  utexEnd(void){ r
91520 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
91530 20 7d 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65   }.static sqlite
91540 33 5f 6d 75 74 65 78 20 2a 6e 6f 6f 70 4d 75 74  3_mutex *noopMut
91550 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b  exAlloc(int id){
91560 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 33   return (sqlite3
91570 5f 6d 75 74 65 78 2a 29 38 3b 20 7d 0a 73 74 61  _mutex*)8; }.sta
91580 74 69 63 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74  tic void noopMut
91590 65 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d  exFree(sqlite3_m
915a0 75 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e  utex *p){ return
915b0 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  ; }.static void 
915c0 6e 6f 6f 70 4d 75 74 65 78 45 6e 74 65 72 28 73  noopMutexEnter(s
915d0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29  qlite3_mutex *p)
915e0 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 73 74 61 74  { return; }.stat
915f0 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78  ic int noopMutex
91600 54 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  Try(sqlite3_mute
91610 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 20 53 51  x *p){ return SQ
91620 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69  LITE_OK; }.stati
91630 63 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 65 78  c void noopMutex
91640 4c 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75  Leave(sqlite3_mu
91650 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 3b  tex *p){ return;
91660 20 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   }..SQLITE_PRIVA
91670 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  TE sqlite3_mutex
91680 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65  _methods *sqlite
91690 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f  3DefaultMutex(vo
916a0 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71  id){.  static sq
916b0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68  lite3_mutex_meth
916c0 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20  ods sMutex = {. 
916d0 20 20 20 6e 6f 6f 70 4d 75 74 65 78 49 6e 69 74     noopMutexInit
916e0 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 45  ,.    noopMutexE
916f0 6e 64 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65  nd,.    noopMute
91700 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 6e 6f 6f 70  xAlloc,.    noop
91710 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 6e  MutexFree,.    n
91720 6f 6f 70 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20  oopMutexEnter,. 
91730 20 20 20 6e 6f 6f 70 4d 75 74 65 78 54 72 79 2c     noopMutexTry,
91740 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 4c 65  .    noopMutexLe
91750 61 76 65 2c 0a 0a 20 20 20 20 6e 6f 6f 70 4d 75  ave,..    noopMu
91760 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 6e 6f 6f  texHeld,.    noo
91770 70 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 20 20  pMutexNotheld.  
91780 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d  };..  return &sM
91790 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  utex;.}.#endif /
917a0 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  * defined(SQLITE
917b0 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20  _MUTEX_NOOP) && 
917c0 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
917d0 44 45 42 55 47 29 20 2a 2f 0a 0a 23 69 66 20 64  DEBUG) */..#if d
917e0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55  efined(SQLITE_MU
917f0 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 64 65 66  TEX_NOOP) && def
91800 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
91810 47 29 0a 2f 2a 0a 2a 2a 20 49 6e 20 74 68 69 73  G)./*.** In this
91820 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c   implementation,
91830 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 20   error checking 
91840 69 73 20 70 72 6f 76 69 64 65 64 20 66 6f 72 20  is provided for 
91850 74 65 73 74 69 6e 67 0a 2a 2a 20 61 6e 64 20 64  testing.** and d
91860 65 62 75 67 67 69 6e 67 20 70 75 72 70 6f 73 65  ebugging purpose
91870 73 2e 20 20 54 68 65 20 6d 75 74 65 78 65 73 20  s.  The mutexes 
91880 73 74 69 6c 6c 20 64 6f 20 6e 6f 74 20 70 72 6f  still do not pro
91890 76 69 64 65 20 61 6e 79 0a 2a 2a 20 6d 75 74 75  vide any.** mutu
918a0 61 6c 20 65 78 63 6c 75 73 69 6f 6e 2e 0a 2a 2f  al exclusion..*/
918b0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 65  ../*.** The mute
918c0 78 20 6f 62 6a 65 63 74 0a 2a 2f 0a 73 74 72 75  x object.*/.stru
918d0 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ct sqlite3_mutex
918e0 20 7b 0a 20 20 69 6e 74 20 69 64 3b 20 20 20 20   {.  int id;    
918f0 20 2f 2a 20 54 68 65 20 6d 75 74 65 78 20 74 79   /* The mutex ty
91900 70 65 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b  pe */.  int cnt;
91910 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
91920 20 65 6e 74 72 69 65 73 20 77 69 74 68 6f 75 74   entries without
91930 20 61 20 6d 61 74 63 68 69 6e 67 20 6c 65 61 76   a matching leav
91940 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54  e */.};../*.** T
91950 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
91960 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69  _held() and sqli
91970 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c  te3_mutex_nothel
91980 64 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a  d() routine are.
91990 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20  ** intended for 
919a0 75 73 65 20 69 6e 73 69 64 65 20 61 73 73 65 72  use inside asser
919b0 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  t() statements..
919c0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65  */.static int de
919d0 62 75 67 4d 75 74 65 78 48 65 6c 64 28 73 71 6c  bugMutexHeld(sql
919e0 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
919f0 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c    return p==0 ||
91a00 20 70 2d 3e 63 6e 74 3e 30 3b 0a 7d 0a 73 74 61   p->cnt>0;.}.sta
91a10 74 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74  tic int debugMut
91a20 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65  exNotheld(sqlite
91a30 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72  3_mutex *p){.  r
91a40 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d  eturn p==0 || p-
91a50 3e 63 6e 74 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  >cnt==0;.}../*.*
91a60 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64  * Initialize and
91a70 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68   deinitialize th
91a80 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65  e mutex subsyste
91a90 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  m..*/.static int
91aa0 20 64 65 62 75 67 4d 75 74 65 78 49 6e 69 74 28   debugMutexInit(
91ab0 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51  void){ return SQ
91ac0 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69  LITE_OK; }.stati
91ad0 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78  c int debugMutex
91ae0 45 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75 72  End(void){ retur
91af0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a  n SQLITE_OK; }..
91b00 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  /*.** The sqlite
91b10 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20  3_mutex_alloc() 
91b20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65  routine allocate
91b30 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78  s a new.** mutex
91b40 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70   and returns a p
91b50 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49  ointer to it.  I
91b60 66 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c  f it returns NUL
91b70 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20  L.** that means 
91b80 74 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75  that a mutex cou
91b90 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61  ld not be alloca
91ba0 74 65 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20  ted. .*/.static 
91bb0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 64  sqlite3_mutex *d
91bc0 65 62 75 67 4d 75 74 65 78 41 6c 6c 6f 63 28 69  ebugMutexAlloc(i
91bd0 6e 74 20 69 64 29 7b 0a 20 20 73 74 61 74 69 63  nt id){.  static
91be0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 61   sqlite3_mutex a
91bf0 53 74 61 74 69 63 5b 36 5d 3b 0a 20 20 73 71 6c  Static[6];.  sql
91c00 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 4e 65 77  ite3_mutex *pNew
91c10 20 3d 20 30 3b 0a 20 20 73 77 69 74 63 68 28 20   = 0;.  switch( 
91c20 69 64 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53  id ){.    case S
91c30 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54  QLITE_MUTEX_FAST
91c40 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  :.    case SQLIT
91c50 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
91c60 45 3a 20 7b 0a 20 20 20 20 20 20 70 4e 65 77 20  E: {.      pNew 
91c70 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28  = sqlite3Malloc(
91c80 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29 3b 0a  sizeof(*pNew));.
91c90 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 20 29        if( pNew )
91ca0 7b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e  {.        pNew->
91cb0 69 64 20 3d 20 69 64 3b 0a 20 20 20 20 20 20 20  id = id;.       
91cc0 20 70 4e 65 77 2d 3e 63 6e 74 20 3d 20 30 3b 0a   pNew->cnt = 0;.
91cd0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
91ce0 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  eak;.    }.    d
91cf0 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20  efault: {.      
91d00 61 73 73 65 72 74 28 20 69 64 2d 32 20 3e 3d 20  assert( id-2 >= 
91d10 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  0 );.      asser
91d20 74 28 20 69 64 2d 32 20 3c 20 28 69 6e 74 29 28  t( id-2 < (int)(
91d30 73 69 7a 65 6f 66 28 61 53 74 61 74 69 63 29 2f  sizeof(aStatic)/
91d40 73 69 7a 65 6f 66 28 61 53 74 61 74 69 63 5b 30  sizeof(aStatic[0
91d50 5d 29 29 20 29 3b 0a 20 20 20 20 20 20 70 4e 65  ])) );.      pNe
91d60 77 20 3d 20 26 61 53 74 61 74 69 63 5b 69 64 2d  w = &aStatic[id-
91d70 32 5d 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e  2];.      pNew->
91d80 69 64 20 3d 20 69 64 3b 0a 20 20 20 20 20 20 62  id = id;.      b
91d90 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
91da0 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d    return pNew;.}
91db0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
91dc0 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73  tine deallocates
91dd0 20 61 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c   a previously al
91de0 6c 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 0a 2a  located mutex..*
91df0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65  /.static void de
91e00 62 75 67 4d 75 74 65 78 46 72 65 65 28 73 71 6c  bugMutexFree(sql
91e10 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
91e20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 63 6e 74    assert( p->cnt
91e30 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
91e40 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d   p->id==SQLITE_M
91e50 55 54 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e  UTEX_FAST || p->
91e60 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58  id==SQLITE_MUTEX
91e70 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20  _RECURSIVE );.  
91e80 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b  sqlite3_free(p);
91e90 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71  .}../*.** The sq
91ea0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
91eb0 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  r() and sqlite3_
91ec0 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74  mutex_try() rout
91ed0 69 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20  ines attempt.** 
91ee0 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78  to enter a mutex
91ef0 2e 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68  .  If another th
91f00 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20  read is already 
91f10 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78  within the mutex
91f20 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74  ,.** sqlite3_mut
91f30 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20  ex_enter() will 
91f40 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65  block and sqlite
91f50 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69  3_mutex_try() wi
91f60 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c  ll return.** SQL
91f70 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73  ITE_BUSY.  The s
91f80 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
91f90 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74  () interface ret
91fa0 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a  urns SQLITE_OK.*
91fb0 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75  * upon successfu
91fc0 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 65  l entry.  Mutexe
91fd0 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20  s created using 
91fe0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
91ff0 55 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65  URSIVE can.** be
92000 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c   entered multipl
92010 65 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73  e times by the s
92020 61 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e 20  ame thread.  In 
92030 73 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a  such cases the,.
92040 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65  ** mutex must be
92050 20 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c   exited an equal
92060 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73   number of times
92070 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20   before another 
92080 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e  thread.** can en
92090 74 65 72 2e 20 20 49 66 20 74 68 65 20 73 61 6d  ter.  If the sam
920a0 65 20 74 68 72 65 61 64 20 74 72 69 65 73 20 74  e thread tries t
920b0 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65  o enter any othe
920c0 72 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a  r kind of mutex.
920d0 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63  ** more than onc
920e0 65 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20  e, the behavior 
920f0 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f  is undefined..*/
92100 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 62  .static void deb
92110 75 67 4d 75 74 65 78 45 6e 74 65 72 28 73 71 6c  ugMutexEnter(sql
92120 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
92130 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d    assert( p->id=
92140 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45  =SQLITE_MUTEX_RE
92150 43 55 52 53 49 56 45 20 7c 7c 20 64 65 62 75 67  CURSIVE || debug
92160 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20  MutexNotheld(p) 
92170 29 3b 0a 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 7d  );.  p->cnt++;.}
92180 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 62 75  .static int debu
92190 67 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 65  gMutexTry(sqlite
921a0 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61  3_mutex *p){.  a
921b0 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51  ssert( p->id==SQ
921c0 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
921d0 53 49 56 45 20 7c 7c 20 64 65 62 75 67 4d 75 74  SIVE || debugMut
921e0 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a  exNotheld(p) );.
921f0 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 20 20 72 65    p->cnt++;.  re
92200 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
92210 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  }../*.** The sql
92220 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
92230 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73  () routine exits
92240 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 61   a mutex that wa
92250 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20  s.** previously 
92260 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 73  entered by the s
92270 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 65  ame thread.  The
92280 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20   behavior.** is 
92290 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65  undefined if the
922a0 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75   mutex is not cu
922b0 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20  rrently entered 
922c0 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72  or.** is not cur
922d0 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64  rently allocated
922e0 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e  .  SQLite will n
922f0 65 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a  ever do either..
92300 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64  */.static void d
92310 65 62 75 67 4d 75 74 65 78 4c 65 61 76 65 28 73  ebugMutexLeave(s
92320 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29  qlite3_mutex *p)
92330 7b 0a 20 20 61 73 73 65 72 74 28 20 64 65 62 75  {.  assert( debu
92340 67 4d 75 74 65 78 48 65 6c 64 28 70 29 20 29 3b  gMutexHeld(p) );
92350 0a 20 20 70 2d 3e 63 6e 74 2d 2d 3b 0a 20 20 61  .  p->cnt--;.  a
92360 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51  ssert( p->id==SQ
92370 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
92380 53 49 56 45 20 7c 7c 20 64 65 62 75 67 4d 75 74  SIVE || debugMut
92390 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a  exNotheld(p) );.
923a0 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  }..SQLITE_PRIVAT
923b0 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  E sqlite3_mutex_
923c0 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33  methods *sqlite3
923d0 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69  DefaultMutex(voi
923e0 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c  d){.  static sql
923f0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
92400 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20  ds sMutex = {.  
92410 20 20 64 65 62 75 67 4d 75 74 65 78 49 6e 69 74    debugMutexInit
92420 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78  ,.    debugMutex
92430 45 6e 64 2c 0a 20 20 20 20 64 65 62 75 67 4d 75  End,.    debugMu
92440 74 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 64 65  texAlloc,.    de
92450 62 75 67 4d 75 74 65 78 46 72 65 65 2c 0a 20 20  bugMutexFree,.  
92460 20 20 64 65 62 75 67 4d 75 74 65 78 45 6e 74 65    debugMutexEnte
92470 72 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65  r,.    debugMute
92480 78 54 72 79 2c 0a 20 20 20 20 64 65 62 75 67 4d  xTry,.    debugM
92490 75 74 65 78 4c 65 61 76 65 2c 0a 0a 20 20 20 20  utexLeave,..    
924a0 64 65 62 75 67 4d 75 74 65 78 48 65 6c 64 2c 0a  debugMutexHeld,.
924b0 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 4e 6f      debugMutexNo
924c0 74 68 65 6c 64 0a 20 20 7d 3b 0a 0a 20 20 72 65  theld.  };..  re
924d0 74 75 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a  turn &sMutex;.}.
924e0 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65  #endif /* define
924f0 64 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e  d(SQLITE_MUTEX_N
92500 4f 4f 50 29 20 26 26 20 64 65 66 69 6e 65 64 28  OOP) && defined(
92510 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20 2a 2f  SQLITE_DEBUG) */
92520 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
92530 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 5f 6e  * End of mutex_n
92540 6f 6f 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  oop.c **********
92550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
92560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
92570 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
92580 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74  * Begin file mut
92590 65 78 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a  ex_os2.c *******
925a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
925b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
925c0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67  /./*.** 2007 Aug
925d0 75 73 74 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65  ust 28.**.** The
925e0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
925f0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
92600 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
92610 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
92620 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
92630 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
92640 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
92650 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
92660 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
92670 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
92680 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
92690 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
926a0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
926b0 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
926c0 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
926d0 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
926e0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
926f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
92700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
92710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
92720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
92730 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
92740 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  le contains the 
92750 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74  C functions that
92760 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78   implement mutex
92770 65 73 20 66 6f 72 20 4f 53 2f 32 0a 2a 2a 0a 2a  es for OS/2.**.*
92780 2a 20 24 49 64 3a 20 6d 75 74 65 78 5f 6f 73 32  * $Id: mutex_os2
92790 2e 63 2c 76 20 31 2e 31 31 20 32 30 30 38 2f 31  .c,v 1.11 2008/1
927a0 31 2f 32 32 20 31 39 3a 35 30 3a 35 34 20 70 77  1/22 19:50:54 pw
927b0 65 69 6c 62 61 63 68 65 72 20 45 78 70 20 24 0a  eilbacher Exp $.
927c0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f  */../*.** The co
927d0 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  de in this file 
927e0 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20  is only used if 
927f0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32  SQLITE_MUTEX_OS2
92800 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 20   is defined..** 
92810 53 65 65 20 74 68 65 20 6d 75 74 65 78 2e 68 20  See the mutex.h 
92820 66 69 6c 65 20 66 6f 72 20 64 65 74 61 69 6c 73  file for details
92830 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
92840 54 45 5f 4d 55 54 45 58 5f 4f 53 32 0a 0a 2f 2a  TE_MUTEX_OS2../*
92850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
92860 2a 2a 2a 2a 2a 20 4f 53 2f 32 20 4d 75 74 65 78  ***** OS/2 Mutex
92870 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
92880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
92890 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
928a0 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  s implementation
928b0 20 6f 66 20 6d 75 74 65 78 65 73 20 69 73 20 62   of mutexes is b
928c0 75 69 6c 74 20 75 73 69 6e 67 20 74 68 65 20 4f  uilt using the O
928d0 53 2f 32 20 41 50 49 2e 0a 2a 2f 0a 0a 2f 2a 0a  S/2 API..*/../*.
928e0 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 6f 62 6a  ** The mutex obj
928f0 65 63 74 0a 2a 2a 20 45 61 63 68 20 72 65 63 75  ect.** Each recu
92900 72 73 69 76 65 20 6d 75 74 65 78 20 69 73 20 61  rsive mutex is a
92910 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
92920 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
92930 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74  cture..*/.struct
92940 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b   sqlite3_mutex {
92950 0a 20 20 48 4d 54 58 20 6d 75 74 65 78 3b 20 20  .  HMTX mutex;  
92960 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f       /* Mutex co
92970 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c 6f  ntrolling the lo
92980 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 20 69 64 3b  ck */.  int  id;
92990 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74            /* Mut
929a0 65 78 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e 74  ex type */.  int
929b0 20 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 2f    nRef;        /
929c0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 65  * Number of refe
929d0 72 65 6e 63 65 73 20 2a 2f 0a 20 20 54 49 44 20  rences */.  TID 
929e0 20 6f 77 6e 65 72 3b 20 20 20 20 20 20 20 2f 2a   owner;       /*
929f0 20 54 68 72 65 61 64 20 68 6f 6c 64 69 6e 67 20   Thread holding 
92a00 74 68 69 73 20 6d 75 74 65 78 20 2a 2f 0a 7d 3b  this mutex */.};
92a10 0a 0a 23 64 65 66 69 6e 65 20 4f 53 32 5f 4d 55  ..#define OS2_MU
92a20 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20  TEX_INITIALIZER 
92a30 20 20 30 2c 30 2c 30 2c 30 0a 0a 2f 2a 0a 2a 2a    0,0,0,0../*.**
92a40 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20   Initialize and 
92a50 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  deinitialize the
92a60 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d   mutex subsystem
92a70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
92a80 6f 73 32 4d 75 74 65 78 49 6e 69 74 28 76 6f 69  os2MutexInit(voi
92a90 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54  d){ return SQLIT
92aa0 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 69  E_OK; }.static i
92ab0 6e 74 20 6f 73 32 4d 75 74 65 78 45 6e 64 28 76  nt os2MutexEnd(v
92ac0 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c  oid){ return SQL
92ad0 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK; }../*.**
92ae0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
92af0 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69  ex_alloc() routi
92b00 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e  ne allocates a n
92b10 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20  ew.** mutex and 
92b20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
92b30 72 20 74 6f 20 69 74 2e 20 20 49 66 20 69 74 20  r to it.  If it 
92b40 72 65 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20  returns NULL.** 
92b50 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20  that means that 
92b60 61 20 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f  a mutex could no
92b70 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20  t be allocated. 
92b80 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  .** SQLite will 
92b90 75 6e 77 69 6e 64 20 69 74 73 20 73 74 61 63 6b  unwind its stack
92ba0 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65   and return an e
92bb0 72 72 6f 72 2e 20 20 54 68 65 20 61 72 67 75 6d  rror.  The argum
92bc0 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65  ent.** to sqlite
92bd0 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20  3_mutex_alloc() 
92be0 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20  is one of these 
92bf0 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74  integer constant
92c00 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a  s:.**.** <ul>.**
92c10 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55   <li>  SQLITE_MU
92c20 54 45 58 5f 46 41 53 54 20 20 20 20 20 20 20 20  TEX_FAST        
92c30 20 20 20 20 20 20 20 30 0a 2a 2a 20 3c 6c 69 3e         0.** <li>
92c40 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52    SQLITE_MUTEX_R
92c50 45 43 55 52 53 49 56 45 20 20 20 20 20 20 20 20  ECURSIVE        
92c60 20 20 31 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c    1.** <li>  SQL
92c70 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
92c80 5f 4d 41 53 54 45 52 20 20 20 20 20 20 32 0a 2a  _MASTER      2.*
92c90 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d  * <li>  SQLITE_M
92ca0 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 20  UTEX_STATIC_MEM 
92cb0 20 20 20 20 20 20 20 20 33 0a 2a 2a 20 3c 6c 69          3.** <li
92cc0 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  >  SQLITE_MUTEX_
92cd0 53 54 41 54 49 43 5f 50 52 4e 47 20 20 20 20 20  STATIC_PRNG     
92ce0 20 20 20 34 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a     4.** </ul>.**
92cf0 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 77  .** The first tw
92d00 6f 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 75 73  o constants caus
92d10 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
92d20 61 6c 6c 6f 63 28 29 20 74 6f 20 63 72 65 61 74  alloc() to creat
92d30 65 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65 78  e.** a new mutex
92d40 2e 20 20 54 68 65 20 6e 65 77 20 6d 75 74 65 78  .  The new mutex
92d50 20 69 73 20 72 65 63 75 72 73 69 76 65 20 77 68   is recursive wh
92d60 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  en SQLITE_MUTEX_
92d70 52 45 43 55 52 53 49 56 45 0a 2a 2a 20 69 73 20  RECURSIVE.** is 
92d80 75 73 65 64 20 62 75 74 20 6e 6f 74 20 6e 65 63  used but not nec
92d90 65 73 73 61 72 69 6c 79 20 73 6f 20 77 68 65 6e  essarily so when
92da0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
92db0 53 54 20 69 73 20 75 73 65 64 2e 0a 2a 2a 20 54  ST is used..** T
92dc0 68 65 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65  he mutex impleme
92dd0 6e 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74  ntation does not
92de0 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 20   need to make a 
92df0 64 69 73 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20 62  distinction.** b
92e00 65 74 77 65 65 6e 20 53 51 4c 49 54 45 5f 4d 55  etween SQLITE_MU
92e10 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 61 6e  TEX_RECURSIVE an
92e20 64 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  d SQLITE_MUTEX_F
92e30 41 53 54 20 69 66 20 69 74 20 64 6f 65 73 0a 2a  AST if it does.*
92e40 2a 20 6e 6f 74 20 77 61 6e 74 20 74 6f 2e 20 20  * not want to.  
92e50 42 75 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  But SQLite will 
92e60 6f 6e 6c 79 20 72 65 71 75 65 73 74 20 61 20 72  only request a r
92e70 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 69  ecursive mutex i
92e80 6e 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65  n.** cases where
92e90 20 69 74 20 72 65 61 6c 6c 79 20 6e 65 65 64 73   it really needs
92ea0 20 6f 6e 65 2e 20 20 49 66 20 61 20 66 61 73 74   one.  If a fast
92eb0 65 72 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65  er non-recursive
92ec0 20 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d   mutex.** implem
92ed0 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 76 61 69  entation is avai
92ee0 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 68 6f 73  lable on the hos
92ef0 74 20 70 6c 61 74 66 6f 72 6d 2c 20 74 68 65 20  t platform, the 
92f00 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 0a  mutex subsystem.
92f10 2a 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20  ** might return 
92f20 73 75 63 68 20 61 20 6d 75 74 65 78 20 69 6e 20  such a mutex in 
92f30 72 65 73 70 6f 6e 73 65 20 74 6f 20 53 51 4c 49  response to SQLI
92f40 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 2e 0a 2a  TE_MUTEX_FAST..*
92f50 2a 0a 2a 2a 20 54 68 65 20 6f 74 68 65 72 20 61  *.** The other a
92f60 6c 6c 6f 77 65 64 20 70 61 72 61 6d 65 74 65 72  llowed parameter
92f70 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74  s to sqlite3_mut
92f80 65 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63 68 20  ex_alloc() each 
92f90 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e  return.** a poin
92fa0 74 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20  ter to a static 
92fb0 70 72 65 65 78 69 73 74 69 6e 67 20 6d 75 74 65  preexisting mute
92fc0 78 2e 20 20 54 68 72 65 65 20 73 74 61 74 69 63  x.  Three static
92fd0 20 6d 75 74 65 78 65 73 20 61 72 65 0a 2a 2a 20   mutexes are.** 
92fe0 75 73 65 64 20 62 79 20 74 68 65 20 63 75 72 72  used by the curr
92ff0 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 53  ent version of S
93000 51 4c 69 74 65 2e 20 20 46 75 74 75 72 65 20 76  QLite.  Future v
93010 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74  ersions of SQLit
93020 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20 61 64 64  e.** may add add
93030 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 63 20 6d  itional static m
93040 75 74 65 78 65 73 2e 20 20 53 74 61 74 69 63 20  utexes.  Static 
93050 6d 75 74 65 78 65 73 20 61 72 65 20 66 6f 72 20  mutexes are for 
93060 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20  internal.** use 
93070 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79 2e 20  by SQLite only. 
93080 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68   Applications th
93090 61 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d 75  at use SQLite mu
930a0 74 65 78 65 73 20 73 68 6f 75 6c 64 0a 2a 2a 20  texes should.** 
930b0 75 73 65 20 6f 6e 6c 79 20 74 68 65 20 64 79 6e  use only the dyn
930c0 61 6d 69 63 20 6d 75 74 65 78 65 73 20 72 65 74  amic mutexes ret
930d0 75 72 6e 65 64 20 62 79 20 53 51 4c 49 54 45 5f  urned by SQLITE_
930e0 4d 55 54 45 58 5f 46 41 53 54 20 6f 72 0a 2a 2a  MUTEX_FAST or.**
930f0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
93100 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 4e  CURSIVE..**.** N
93110 6f 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65 20  ote that if one 
93120 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d  of the dynamic m
93130 75 74 65 78 20 70 61 72 61 6d 65 74 65 72 73 20  utex parameters 
93140 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41  (SQLITE_MUTEX_FA
93150 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f  ST.** or SQLITE_
93160 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29  MUTEX_RECURSIVE)
93170 20 69 73 20 75 73 65 64 20 74 68 65 6e 20 73 71   is used then sq
93180 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
93190 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61  c().** returns a
931a0 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65 78   different mutex
931b0 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e 20   on every call. 
931c0 20 42 75 74 20 66 6f 72 20 74 68 65 20 73 74 61   But for the sta
931d0 74 69 63 0a 2a 2a 20 6d 75 74 65 78 20 74 79 70  tic.** mutex typ
931e0 65 73 2c 20 74 68 65 20 73 61 6d 65 20 6d 75 74  es, the same mut
931f0 65 78 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f  ex is returned o
93200 6e 20 65 76 65 72 79 20 63 61 6c 6c 20 74 68 61  n every call tha
93210 74 20 68 61 73 0a 2a 2a 20 74 68 65 20 73 61 6d  t has.** the sam
93220 65 20 74 79 70 65 20 6e 75 6d 62 65 72 2e 0a 2a  e type number..*
93230 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33  /.static sqlite3
93240 5f 6d 75 74 65 78 20 2a 6f 73 32 4d 75 74 65 78  _mutex *os2Mutex
93250 41 6c 6c 6f 63 28 69 6e 74 20 69 54 79 70 65 29  Alloc(int iType)
93260 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  {.  sqlite3_mute
93270 78 20 2a 70 20 3d 20 4e 55 4c 4c 3b 0a 20 20 73  x *p = NULL;.  s
93280 77 69 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a  witch( iType ){.
93290 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
932a0 4d 55 54 45 58 5f 46 41 53 54 3a 0a 20 20 20 20  MUTEX_FAST:.    
932b0 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45  case SQLITE_MUTE
932c0 58 5f 52 45 43 55 52 53 49 56 45 3a 20 7b 0a 20  X_RECURSIVE: {. 
932d0 20 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33       p = sqlite3
932e0 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65  MallocZero( size
932f0 6f 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20  of(*p) );.      
93300 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20  if( p ){.       
93310 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a   p->id = iType;.
93320 20 20 20 20 20 20 20 20 69 66 28 20 44 6f 73 43          if( DosC
93330 72 65 61 74 65 4d 75 74 65 78 53 65 6d 28 20 30  reateMutexSem( 0
93340 2c 20 26 70 2d 3e 6d 75 74 65 78 2c 20 30 2c 20  , &p->mutex, 0, 
93350 46 41 4c 53 45 20 29 20 21 3d 20 4e 4f 5f 45 52  FALSE ) != NO_ER
93360 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ROR ){.         
93370 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 20 70   sqlite3_free( p
93380 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 20   );.          p 
93390 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 20 20  = NULL;.        
933a0 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
933b0 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
933c0 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20   default: {.    
933d0 20 20 73 74 61 74 69 63 20 76 6f 6c 61 74 69 6c    static volatil
933e0 65 20 69 6e 74 20 69 73 49 6e 69 74 20 3d 20 30  e int isInit = 0
933f0 3b 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 73  ;.      static s
93400 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 73 74 61  qlite3_mutex sta
93410 74 69 63 4d 75 74 65 78 65 73 5b 5d 20 3d 20 7b  ticMutexes[] = {
93420 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d  .        { OS2_M
93430 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52  UTEX_INITIALIZER
93440 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f  , },.        { O
93450 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c  S2_MUTEX_INITIAL
93460 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 20  IZER, },.       
93470 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49   { OS2_MUTEX_INI
93480 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20  TIALIZER, },.   
93490 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58       { OS2_MUTEX
934a0 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c  _INITIALIZER, },
934b0 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d  .        { OS2_M
934c0 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52  UTEX_INITIALIZER
934d0 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f  , },.        { O
934e0 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c  S2_MUTEX_INITIAL
934f0 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 7d  IZER, },.      }
93500 3b 0a 20 20 20 20 20 20 69 66 20 28 20 21 69 73  ;.      if ( !is
93510 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Init ){.        
93520 41 50 49 52 45 54 20 72 63 3b 0a 20 20 20 20 20  APIRET rc;.     
93530 20 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20 20     PTIB ptib;.  
93540 20 20 20 20 20 20 50 50 49 42 20 70 70 69 62 3b        PPIB ppib;
93550 0a 20 20 20 20 20 20 20 20 48 4d 54 58 20 6d 75  .        HMTX mu
93560 74 65 78 3b 0a 20 20 20 20 20 20 20 20 63 68 61  tex;.        cha
93570 72 20 6e 61 6d 65 5b 33 32 5d 3b 0a 20 20 20 20  r name[32];.    
93580 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c      DosGetInfoBl
93590 6f 63 6b 73 28 20 26 70 74 69 62 2c 20 26 70 70  ocks( &ptib, &pp
935a0 69 62 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71  ib );.        sq
935b0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 20  lite3_snprintf( 
935c0 73 69 7a 65 6f 66 28 6e 61 6d 65 29 2c 20 6e 61  sizeof(name), na
935d0 6d 65 2c 20 22 5c 5c 53 45 4d 33 32 5c 5c 53 51  me, "\\SEM32\\SQ
935e0 4c 49 54 45 25 30 34 78 22 2c 0a 20 20 20 20 20  LITE%04x",.     
935f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
93600 20 20 20 20 20 70 70 69 62 2d 3e 70 69 62 5f 75       ppib->pib_u
93610 6c 70 69 64 20 29 3b 0a 20 20 20 20 20 20 20 20  lpid );.        
93620 77 68 69 6c 65 28 20 21 69 73 49 6e 69 74 20 29  while( !isInit )
93630 7b 0a 20 20 20 20 20 20 20 20 20 20 6d 75 74 65  {.          mute
93640 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  x = 0;.         
93650 20 72 63 20 3d 20 44 6f 73 43 72 65 61 74 65 4d   rc = DosCreateM
93660 75 74 65 78 53 65 6d 28 20 6e 61 6d 65 2c 20 26  utexSem( name, &
93670 6d 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 29  mutex, 0, FALSE)
93680 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
93690 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29  rc == NO_ERROR )
936a0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 75 6e  {.            un
936b0 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a 20 20  signed int i;.  
936c0 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 69            if( !i
936d0 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20  sInit ){.       
936e0 20 20 20 20 20 20 20 66 6f 72 28 20 69 20 3d 20         for( i = 
936f0 30 3b 20 69 20 3c 20 73 69 7a 65 6f 66 28 73 74  0; i < sizeof(st
93700 61 74 69 63 4d 75 74 65 78 65 73 29 2f 73 69 7a  aticMutexes)/siz
93710 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78 65  eof(staticMutexe
93720 73 5b 30 5d 29 3b 20 69 2b 2b 20 29 7b 0a 20 20  s[0]); i++ ){.  
93730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 44 6f                Do
93740 73 43 72 65 61 74 65 4d 75 74 65 78 53 65 6d 28  sCreateMutexSem(
93750 20 30 2c 20 26 73 74 61 74 69 63 4d 75 74 65 78   0, &staticMutex
93760 65 73 5b 69 5d 2e 6d 75 74 65 78 2c 20 30 2c 20  es[i].mutex, 0, 
93770 46 41 4c 53 45 20 29 3b 0a 20 20 20 20 20 20 20  FALSE );.       
93780 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
93790 20 20 20 20 20 20 20 69 73 49 6e 69 74 20 3d 20         isInit = 
937a0 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  1;.            }
937b0 0a 20 20 20 20 20 20 20 20 20 20 20 20 44 6f 73  .            Dos
937c0 43 6c 6f 73 65 4d 75 74 65 78 53 65 6d 28 20 6d  CloseMutexSem( m
937d0 75 74 65 78 20 29 3b 0a 20 20 20 20 20 20 20 20  utex );.        
937e0 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 20 3d    }else if( rc =
937f0 3d 20 45 52 52 4f 52 5f 44 55 50 4c 49 43 41 54  = ERROR_DUPLICAT
93800 45 5f 4e 41 4d 45 20 29 7b 0a 20 20 20 20 20 20  E_NAME ){.      
93810 20 20 20 20 20 20 44 6f 73 53 6c 65 65 70 28 20        DosSleep( 
93820 31 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  1 );.          }
93830 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
93840 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 20 20    return p;.    
93850 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
93860 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
93870 61 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 20  assert( iType-2 
93880 3e 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 61 73  >= 0 );.      as
93890 73 65 72 74 28 20 69 54 79 70 65 2d 32 20 3c 20  sert( iType-2 < 
938a0 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74  sizeof(staticMut
938b0 65 78 65 73 29 2f 73 69 7a 65 6f 66 28 73 74 61  exes)/sizeof(sta
938c0 74 69 63 4d 75 74 65 78 65 73 5b 30 5d 29 20 29  ticMutexes[0]) )
938d0 3b 0a 20 20 20 20 20 20 70 20 3d 20 26 73 74 61  ;.      p = &sta
938e0 74 69 63 4d 75 74 65 78 65 73 5b 69 54 79 70 65  ticMutexes[iType
938f0 2d 32 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64  -2];.      p->id
93900 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20   = iType;.      
93910 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
93920 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
93930 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
93940 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20  ine deallocates 
93950 61 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c  a previously all
93960 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 0a 2a 2a  ocated mutex..**
93970 20 53 51 4c 69 74 65 20 69 73 20 63 61 72 65 66   SQLite is caref
93980 75 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65  ul to deallocate
93990 20 65 76 65 72 79 20 6d 75 74 65 78 20 74 68 61   every mutex tha
939a0 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e 0a  t it allocates..
939b0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f  */.static void o
939c0 73 32 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69  s2MutexFree(sqli
939d0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
939e0 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75   if( p==0 ) retu
939f0 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  rn;.  assert( p-
93a00 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 61 73  >nRef==0 );.  as
93a10 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c  sert( p->id==SQL
93a20 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 7c  ITE_MUTEX_FAST |
93a30 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f  | p->id==SQLITE_
93a40 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20  MUTEX_RECURSIVE 
93a50 29 3b 0a 20 20 44 6f 73 43 6c 6f 73 65 4d 75 74  );.  DosCloseMut
93a60 65 78 53 65 6d 28 20 70 2d 3e 6d 75 74 65 78 20  exSem( p->mutex 
93a70 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
93a80 65 28 20 70 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  e( p );.}../*.**
93a90 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
93aa0 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73  ex_enter() and s
93ab0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
93ac0 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65  () routines atte
93ad0 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20  mpt.** to enter 
93ae0 61 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f  a mutex.  If ano
93af0 74 68 65 72 20 74 68 72 65 61 64 20 69 73 20 61  ther thread is a
93b00 6c 72 65 61 64 79 20 77 69 74 68 69 6e 20 74 68  lready within th
93b10 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69  e mutex,.** sqli
93b20 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
93b30 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64  ) will block and
93b40 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74   sqlite3_mutex_t
93b50 72 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e  ry() will return
93b60 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e  .** SQLITE_BUSY.
93b70 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75    The sqlite3_mu
93b80 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66  tex_try() interf
93b90 61 63 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49  ace returns SQLI
93ba0 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75  TE_OK.** upon su
93bb0 63 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20  ccessful entry. 
93bc0 20 4d 75 74 65 78 65 73 20 63 72 65 61 74 65 64   Mutexes created
93bd0 20 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55   using SQLITE_MU
93be0 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61  TEX_RECURSIVE ca
93bf0 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20  n.** be entered 
93c00 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62  multiple times b
93c10 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61  y the same threa
93c20 64 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73 65  d.  In such case
93c30 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20  s the,.** mutex 
93c40 6d 75 73 74 20 62 65 20 65 78 69 74 65 64 20 61  must be exited a
93c50 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f  n equal number o
93c60 66 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61  f times before a
93c70 6e 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a  nother thread.**
93c80 20 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 20   can enter.  If 
93c90 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20  the same thread 
93ca0 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61  tries to enter a
93cb0 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66  ny other kind of
93cc0 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74   mutex.** more t
93cd0 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65  han once, the be
93ce0 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69  havior is undefi
93cf0 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ned..*/.static v
93d00 6f 69 64 20 6f 73 32 4d 75 74 65 78 45 6e 74 65  oid os2MutexEnte
93d10 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  r(sqlite3_mutex 
93d20 2a 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b 0a  *p){.  TID tid;.
93d30 20 20 50 49 44 20 68 6f 6c 64 65 72 31 3b 0a 20    PID holder1;. 
93d40 20 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 32 3b 0a   ULONG holder2;.
93d50 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74    if( p==0 ) ret
93d60 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70  urn;.  assert( p
93d70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54  ->id==SQLITE_MUT
93d80 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20  EX_RECURSIVE || 
93d90 6f 73 32 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28  os2MutexNotheld(
93da0 70 29 20 29 3b 0a 20 20 44 6f 73 52 65 71 75 65  p) );.  DosReque
93db0 73 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75  stMutexSem(p->mu
93dc0 74 65 78 2c 20 53 45 4d 5f 49 4e 44 45 46 49 4e  tex, SEM_INDEFIN
93dd0 49 54 45 5f 57 41 49 54 29 3b 0a 20 20 44 6f 73  ITE_WAIT);.  Dos
93de0 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70 2d  QueryMutexSem(p-
93df0 3e 6d 75 74 65 78 2c 20 26 68 6f 6c 64 65 72 31  >mutex, &holder1
93e00 2c 20 26 74 69 64 2c 20 26 68 6f 6c 64 65 72 32  , &tid, &holder2
93e10 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20  );.  p->owner = 
93e20 74 69 64 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b  tid;.  p->nRef++
93e30 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  ;.}.static int o
93e40 73 32 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74  s2MutexTry(sqlit
93e50 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
93e60 69 6e 74 20 72 63 3b 0a 20 20 54 49 44 20 74 69  int rc;.  TID ti
93e70 64 3b 0a 20 20 50 49 44 20 68 6f 6c 64 65 72 31  d;.  PID holder1
93e80 3b 0a 20 20 55 4c 4f 4e 47 20 68 6f 6c 64 65 72  ;.  ULONG holder
93e90 32 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20  2;.  if( p==0 ) 
93ea0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
93eb0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69  ;.  assert( p->i
93ec0 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  d==SQLITE_MUTEX_
93ed0 52 45 43 55 52 53 49 56 45 20 7c 7c 20 6f 73 32  RECURSIVE || os2
93ee0 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20  MutexNotheld(p) 
93ef0 29 3b 0a 20 20 69 66 28 20 44 6f 73 52 65 71 75  );.  if( DosRequ
93f00 65 73 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d  estMutexSem(p->m
93f10 75 74 65 78 2c 20 53 45 4d 5f 49 4d 4d 45 44 49  utex, SEM_IMMEDI
93f20 41 54 45 5f 52 45 54 55 52 4e 29 20 3d 3d 20 4e  ATE_RETURN) == N
93f30 4f 5f 45 52 52 4f 52 29 20 7b 0a 20 20 20 20 44  O_ERROR) {.    D
93f40 6f 73 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28  osQueryMutexSem(
93f50 70 2d 3e 6d 75 74 65 78 2c 20 26 68 6f 6c 64 65  p->mutex, &holde
93f60 72 31 2c 20 26 74 69 64 2c 20 26 68 6f 6c 64 65  r1, &tid, &holde
93f70 72 32 29 3b 0a 20 20 20 20 70 2d 3e 6f 77 6e 65  r2);.    p->owne
93f80 72 20 3d 20 74 69 64 3b 0a 20 20 20 20 70 2d 3e  r = tid;.    p->
93f90 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d  nRef++;.    rc =
93fa0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 20   SQLITE_OK;.  } 
93fb0 65 6c 73 65 20 7b 0a 20 20 20 20 72 63 20 3d 20  else {.    rc = 
93fc0 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d  SQLITE_BUSY;.  }
93fd0 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
93fe0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ../*.** The sqli
93ff0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
94000 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20  ) routine exits 
94010 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73  a mutex that was
94020 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65  .** previously e
94030 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61  ntered by the sa
94040 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20  me thread.  The 
94050 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75  behavior.** is u
94060 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20  ndefined if the 
94070 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72  mutex is not cur
94080 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 6f  rently entered o
94090 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72  r.** is not curr
940a0 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e  ently allocated.
940b0 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65    SQLite will ne
940c0 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a  ver do either..*
940d0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73  /.static void os
940e0 32 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c 69  2MutexLeave(sqli
940f0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
94100 20 54 49 44 20 74 69 64 3b 0a 20 20 50 49 44 20   TID tid;.  PID 
94110 68 6f 6c 64 65 72 31 3b 0a 20 20 55 4c 4f 4e 47  holder1;.  ULONG
94120 20 68 6f 6c 64 65 72 32 3b 0a 20 20 69 66 28 20   holder2;.  if( 
94130 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  p==0 ) return;. 
94140 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66   assert( p->nRef
94150 3e 30 20 29 3b 0a 20 20 44 6f 73 51 75 65 72 79  >0 );.  DosQuery
94160 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65  MutexSem(p->mute
94170 78 2c 20 26 68 6f 6c 64 65 72 31 2c 20 26 74 69  x, &holder1, &ti
94180 64 2c 20 26 68 6f 6c 64 65 72 32 29 3b 0a 20 20  d, &holder2);.  
94190 61 73 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65 72  assert( p->owner
941a0 3d 3d 74 69 64 20 29 3b 0a 20 20 70 2d 3e 6e 52  ==tid );.  p->nR
941b0 65 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20  ef--;.  assert( 
941c0 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d  p->nRef==0 || p-
941d0 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45  >id==SQLITE_MUTE
941e0 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20  X_RECURSIVE );. 
941f0 20 44 6f 73 52 65 6c 65 61 73 65 4d 75 74 65 78   DosReleaseMutex
94200 53 65 6d 28 70 2d 3e 6d 75 74 65 78 29 3b 0a 7d  Sem(p->mutex);.}
94210 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
94220 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20  DEBUG./*.** The 
94230 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
94240 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  ld() and sqlite3
94250 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29  _mutex_notheld()
94260 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20   routine are.** 
94270 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65  intended for use
94280 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29   inside assert()
94290 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a   statements..*/.
942a0 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75  static int os2Mu
942b0 74 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f  texHeld(sqlite3_
942c0 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 49 44  mutex *p){.  TID
942d0 20 74 69 64 3b 0a 20 20 50 49 44 20 70 69 64 3b   tid;.  PID pid;
942e0 0a 20 20 55 4c 4f 4e 47 20 75 6c 43 6f 75 6e 74  .  ULONG ulCount
942f0 3b 0a 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20  ;.  PTIB ptib;. 
94300 20 69 66 28 20 70 21 3d 30 20 29 20 7b 0a 20 20   if( p!=0 ) {.  
94310 20 20 44 6f 73 51 75 65 72 79 4d 75 74 65 78 53    DosQueryMutexS
94320 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 70 69  em(p->mutex, &pi
94330 64 2c 20 26 74 69 64 2c 20 26 75 6c 43 6f 75 6e  d, &tid, &ulCoun
94340 74 29 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20  t);.  } else {. 
94350 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f     DosGetInfoBlo
94360 63 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c 29  cks(&ptib, NULL)
94370 3b 0a 20 20 20 20 74 69 64 20 3d 20 70 74 69 62  ;.    tid = ptib
94380 2d 3e 74 69 62 5f 70 74 69 62 32 2d 3e 74 69 62  ->tib_ptib2->tib
94390 32 5f 75 6c 74 69 64 3b 0a 20 20 7d 0a 20 20 72  2_ultid;.  }.  r
943a0 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 28 70  eturn p==0 || (p
943b0 2d 3e 6e 52 65 66 21 3d 30 20 26 26 20 70 2d 3e  ->nRef!=0 && p->
943c0 6f 77 6e 65 72 3d 3d 74 69 64 29 3b 0a 7d 0a 73  owner==tid);.}.s
943d0 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74  tatic int os2Mut
943e0 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65  exNotheld(sqlite
943f0 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 54  3_mutex *p){.  T
94400 49 44 20 74 69 64 3b 0a 20 20 50 49 44 20 70 69  ID tid;.  PID pi
94410 64 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 43 6f 75  d;.  ULONG ulCou
94420 6e 74 3b 0a 20 20 50 54 49 42 20 70 74 69 62 3b  nt;.  PTIB ptib;
94430 0a 20 20 69 66 28 20 70 21 3d 20 30 20 29 20 7b  .  if( p!= 0 ) {
94440 0a 20 20 20 20 44 6f 73 51 75 65 72 79 4d 75 74  .    DosQueryMut
94450 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20  exSem(p->mutex, 
94460 26 70 69 64 2c 20 26 74 69 64 2c 20 26 75 6c 43  &pid, &tid, &ulC
94470 6f 75 6e 74 29 3b 0a 20 20 7d 20 65 6c 73 65 20  ount);.  } else 
94480 7b 0a 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f  {.    DosGetInfo
94490 42 6c 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e 55  Blocks(&ptib, NU
944a0 4c 4c 29 3b 0a 20 20 20 20 74 69 64 20 3d 20 70  LL);.    tid = p
944b0 74 69 62 2d 3e 74 69 62 5f 70 74 69 62 32 2d 3e  tib->tib_ptib2->
944c0 74 69 62 32 5f 75 6c 74 69 64 3b 0a 20 20 7d 0a  tib2_ultid;.  }.
944d0 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c    return p==0 ||
944e0 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70   p->nRef==0 || p
944f0 2d 3e 6f 77 6e 65 72 21 3d 74 69 64 3b 0a 7d 0a  ->owner!=tid;.}.
94500 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50  #endif..SQLITE_P
94510 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d  RIVATE sqlite3_m
94520 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71  utex_methods *sq
94530 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65  lite3DefaultMute
94540 78 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69  x(void){.  stati
94550 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  c sqlite3_mutex_
94560 6d 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20 3d  methods sMutex =
94570 20 7b 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 49   {.    os2MutexI
94580 6e 69 74 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65  nit,.    os2Mute
94590 78 45 6e 64 2c 0a 20 20 20 20 6f 73 32 4d 75 74  xEnd,.    os2Mut
945a0 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 6f 73 32  exAlloc,.    os2
945b0 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 6f  MutexFree,.    o
945c0 73 32 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20  s2MutexEnter,.  
945d0 20 20 6f 73 32 4d 75 74 65 78 54 72 79 2c 0a 20    os2MutexTry,. 
945e0 20 20 20 6f 73 32 4d 75 74 65 78 4c 65 61 76 65     os2MutexLeave
945f0 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ,.#ifdef SQLITE_
94600 44 45 42 55 47 0a 20 20 20 20 6f 73 32 4d 75 74  DEBUG.    os2Mut
94610 65 78 48 65 6c 64 2c 0a 20 20 20 20 6f 73 32 4d  exHeld,.    os2M
94620 75 74 65 78 4e 6f 74 68 65 6c 64 0a 23 65 6e 64  utexNotheld.#end
94630 69 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72  if.  };..  retur
94640 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e  n &sMutex;.}.#en
94650 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55  dif /* SQLITE_MU
94660 54 45 58 5f 4f 53 32 20 2a 2f 0a 0a 2f 2a 2a 2a  TEX_OS2 */../***
94670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
94680 6f 66 20 6d 75 74 65 78 5f 6f 73 32 2e 63 20 2a  of mutex_os2.c *
94690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
946a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
946b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
946c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
946d0 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f 75 6e 69  n file mutex_uni
946e0 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  x.c ************
946f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
94700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
94710 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 32 38  * 2007 August 28
94720 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
94730 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
94740 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
94750 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
94760 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
94770 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
94780 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
94790 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
947a0 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
947b0 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
947c0 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
947d0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
947e0 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
947f0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
94800 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
94810 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
94820 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
94830 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
94840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
94850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
94860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
94870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
94880 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
94890 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63  tains the C func
948a0 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65  tions that imple
948b0 6d 65 6e 74 20 6d 75 74 65 78 65 73 20 66 6f 72  ment mutexes for
948c0 20 70 74 68 72 65 61 64 73 0a 2a 2a 0a 2a 2a 20   pthreads.**.** 
948d0 24 49 64 3a 20 6d 75 74 65 78 5f 75 6e 69 78 2e  $Id: mutex_unix.
948e0 63 2c 76 20 31 2e 31 36 20 32 30 30 38 2f 31 32  c,v 1.16 2008/12
948f0 2f 30 38 20 31 38 3a 31 39 3a 31 38 20 64 72 68  /08 18:19:18 drh
94900 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a   Exp $.*/../*.**
94910 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69   The code in thi
94920 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 75  s file is only u
94930 73 65 64 20 69 66 20 77 65 20 61 72 65 20 63 6f  sed if we are co
94940 6d 70 69 6c 69 6e 67 20 74 68 72 65 61 64 73 61  mpiling threadsa
94950 66 65 0a 2a 2a 20 75 6e 64 65 72 20 75 6e 69 78  fe.** under unix
94960 20 77 69 74 68 20 70 74 68 72 65 61 64 73 2e 0a   with pthreads..
94970 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  **.** Note that 
94980 74 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74  this implementat
94990 69 6f 6e 20 72 65 71 75 69 72 65 73 20 61 20 76  ion requires a v
949a0 65 72 73 69 6f 6e 20 6f 66 20 70 74 68 72 65 61  ersion of pthrea
949b0 64 73 20 74 68 61 74 0a 2a 2a 20 73 75 70 70 6f  ds that.** suppo
949c0 72 74 73 20 72 65 63 75 72 73 69 76 65 20 6d 75  rts recursive mu
949d0 74 65 78 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66  texes..*/.#ifdef
949e0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54   SQLITE_MUTEX_PT
949f0 48 52 45 41 44 53 0a 0a 23 69 6e 63 6c 75 64 65  HREADS..#include
94a00 20 3c 70 74 68 72 65 61 64 2e 68 3e 0a 0a 0a 2f   <pthread.h>.../
94a10 2a 0a 2a 2a 20 45 61 63 68 20 72 65 63 75 72 73  *.** Each recurs
94a20 69 76 65 20 6d 75 74 65 78 20 69 73 20 61 6e 20  ive mutex is an 
94a30 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
94a40 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
94a50 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73  ure..*/.struct s
94a60 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a 20  qlite3_mutex {. 
94a70 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 74   pthread_mutex_t
94a80 20 6d 75 74 65 78 3b 20 20 20 20 20 2f 2a 20 4d   mutex;     /* M
94a90 75 74 65 78 20 63 6f 6e 74 72 6f 6c 6c 69 6e 67  utex controlling
94aa0 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69   the lock */.  i
94ab0 6e 74 20 69 64 3b 20 20 20 20 20 20 20 20 20 20  nt id;          
94ac0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74            /* Mut
94ad0 65 78 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e 74  ex type */.  int
94ae0 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20   nRef;          
94af0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
94b00 72 20 6f 66 20 65 6e 74 72 61 6e 63 65 73 20 2a  r of entrances *
94b10 2f 0a 20 20 70 74 68 72 65 61 64 5f 74 20 6f 77  /.  pthread_t ow
94b20 6e 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f  ner;           /
94b30 2a 20 54 68 72 65 61 64 20 74 68 61 74 20 69 73  * Thread that is
94b40 20 77 69 74 68 69 6e 20 74 68 69 73 20 6d 75 74   within this mut
94b50 65 78 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  ex */.#ifdef SQL
94b60 49 54 45 5f 44 45 42 55 47 0a 20 20 69 6e 74 20  ITE_DEBUG.  int 
94b70 74 72 61 63 65 3b 20 20 20 20 20 20 20 20 20 20  trace;          
94b80 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74         /* True t
94b90 6f 20 74 72 61 63 65 20 63 68 61 6e 67 65 73 20  o trace changes 
94ba0 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 23 69 66  */.#endif.};.#if
94bb0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
94bc0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 33  .#define SQLITE3
94bd0 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a  _MUTEX_INITIALIZ
94be0 45 52 20 7b 20 50 54 48 52 45 41 44 5f 4d 55 54  ER { PTHREAD_MUT
94bf0 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20  EX_INITIALIZER, 
94c00 30 2c 20 30 2c 20 28 70 74 68 72 65 61 64 5f 74  0, 0, (pthread_t
94c10 29 30 2c 20 30 20 7d 0a 23 65 6c 73 65 0a 23 64  )0, 0 }.#else.#d
94c20 65 66 69 6e 65 20 53 51 4c 49 54 45 33 5f 4d 55  efine SQLITE3_MU
94c30 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20  TEX_INITIALIZER 
94c40 7b 20 50 54 48 52 45 41 44 5f 4d 55 54 45 58 5f  { PTHREAD_MUTEX_
94c50 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 30 2c 20  INITIALIZER, 0, 
94c60 30 2c 20 28 70 74 68 72 65 61 64 5f 74 29 30 20  0, (pthread_t)0 
94c70 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
94c80 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
94c90 78 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c  x_held() and sql
94ca0 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65  ite3_mutex_nothe
94cb0 6c 64 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65  ld() routine are
94cc0 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72  .** intended for
94cd0 20 75 73 65 20 6f 6e 6c 79 20 69 6e 73 69 64 65   use only inside
94ce0 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d   assert() statem
94cf0 65 6e 74 73 2e 20 20 4f 6e 20 73 6f 6d 65 20 70  ents.  On some p
94d00 6c 61 74 66 6f 72 6d 73 2c 0a 2a 2a 20 74 68 65  latforms,.** the
94d10 72 65 20 6d 69 67 68 74 20 62 65 20 72 61 63 65  re might be race
94d20 20 63 6f 6e 64 69 74 69 6f 6e 73 20 74 68 61 74   conditions that
94d30 20 63 61 6e 20 63 61 75 73 65 20 74 68 65 73 65   can cause these
94d40 20 72 6f 75 74 69 6e 65 73 20 74 6f 0a 2a 2a 20   routines to.** 
94d50 64 65 6c 69 76 65 72 20 69 6e 63 6f 72 72 65 63  deliver incorrec
94d60 74 20 72 65 73 75 6c 74 73 2e 20 20 49 6e 20 70  t results.  In p
94d70 61 72 74 69 63 75 6c 61 72 2c 20 69 66 20 70 74  articular, if pt
94d80 68 72 65 61 64 5f 65 71 75 61 6c 28 29 20 69 73  hread_equal() is
94d90 0a 2a 2a 20 6e 6f 74 20 61 6e 20 61 74 6f 6d 69  .** not an atomi
94da0 63 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65  c operation, the
94db0 6e 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  n these routines
94dc0 20 6d 69 67 68 74 20 64 65 6c 69 76 65 72 79 0a   might delivery.
94dd0 2a 2a 20 69 6e 63 6f 72 72 65 63 74 20 72 65 73  ** incorrect res
94de0 75 6c 74 73 2e 20 20 4f 6e 20 6d 6f 73 74 20 70  ults.  On most p
94df0 6c 61 74 66 6f 72 6d 73 2c 20 70 74 68 72 65 61  latforms, pthrea
94e00 64 5f 65 71 75 61 6c 28 29 20 69 73 20 61 20 0a  d_equal() is a .
94e10 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66  ** comparison of
94e20 20 74 77 6f 20 69 6e 74 65 67 65 72 73 20 61 6e   two integers an
94e30 64 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 61  d is therefore a
94e40 74 6f 6d 69 63 2e 20 20 42 75 74 20 77 65 20 61  tomic.  But we a
94e50 72 65 0a 2a 2a 20 74 6f 6c 64 20 74 68 61 74 20  re.** told that 
94e60 48 50 55 58 20 69 73 20 6e 6f 74 20 73 75 63 68  HPUX is not such
94e70 20 61 20 70 6c 61 74 66 6f 72 6d 2e 20 20 49 66   a platform.  If
94e80 20 73 6f 2c 20 74 68 65 6e 20 74 68 65 73 65 20   so, then these 
94e90 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 77 69 6c 6c  routines.** will
94ea0 20 6e 6f 74 20 61 6c 77 61 79 73 20 77 6f 72 6b   not always work
94eb0 20 63 6f 72 72 65 63 74 6c 79 20 6f 6e 20 48 50   correctly on HP
94ec0 55 58 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 74 68 6f  UX..**.** On tho
94ed0 73 65 20 70 6c 61 74 66 6f 72 6d 73 20 77 68 65  se platforms whe
94ee0 72 65 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c  re pthread_equal
94ef0 28 29 20 69 73 20 6e 6f 74 20 61 74 6f 6d 69 63  () is not atomic
94f00 2c 20 53 51 4c 69 74 65 0a 2a 2a 20 73 68 6f 75  , SQLite.** shou
94f10 6c 64 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 77  ld be compiled w
94f20 69 74 68 6f 75 74 20 2d 44 53 51 4c 49 54 45 5f  ithout -DSQLITE_
94f30 44 45 42 55 47 20 61 6e 64 20 77 69 74 68 20 2d  DEBUG and with -
94f40 44 4e 44 45 42 55 47 20 74 6f 0a 2a 2a 20 6d 61  DNDEBUG to.** ma
94f50 6b 65 20 73 75 72 65 20 6e 6f 20 61 73 73 65 72  ke sure no asser
94f60 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 61  t() statements a
94f70 72 65 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64  re evaluated and
94f80 20 68 65 6e 63 65 20 74 68 65 73 65 0a 2a 2a 20   hence these.** 
94f90 72 6f 75 74 69 6e 65 73 20 61 72 65 20 6e 65 76  routines are nev
94fa0 65 72 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 23 69  er called..*/.#i
94fb0 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55  f !defined(NDEBU
94fc0 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  G) || defined(SQ
94fd0 4c 49 54 45 5f 44 45 42 55 47 29 0a 73 74 61 74  LITE_DEBUG).stat
94fe0 69 63 20 69 6e 74 20 70 74 68 72 65 61 64 4d 75  ic int pthreadMu
94ff0 74 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f  texHeld(sqlite3_
95000 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74  mutex *p){.  ret
95010 75 72 6e 20 28 70 2d 3e 6e 52 65 66 21 3d 30 20  urn (p->nRef!=0 
95020 26 26 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c  && pthread_equal
95030 28 70 2d 3e 6f 77 6e 65 72 2c 20 70 74 68 72 65  (p->owner, pthre
95040 61 64 5f 73 65 6c 66 28 29 29 29 3b 0a 7d 0a 73  ad_self()));.}.s
95050 74 61 74 69 63 20 69 6e 74 20 70 74 68 72 65 61  tatic int pthrea
95060 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71  dMutexNotheld(sq
95070 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b  lite3_mutex *p){
95080 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65  .  return p->nRe
95090 66 3d 3d 30 20 7c 7c 20 70 74 68 72 65 61 64 5f  f==0 || pthread_
950a0 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20  equal(p->owner, 
950b0 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 29 3d  pthread_self())=
950c0 3d 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  =0;.}.#endif../*
950d0 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61  .** Initialize a
950e0 6e 64 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20  nd deinitialize 
950f0 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73  the mutex subsys
95100 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  tem..*/.static i
95110 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65 78 49  nt pthreadMutexI
95120 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72  nit(void){ retur
95130 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73  n SQLITE_OK; }.s
95140 74 61 74 69 63 20 69 6e 74 20 70 74 68 72 65 61  tatic int pthrea
95150 64 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b  dMutexEnd(void){
95160 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
95170 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  K; }../*.** The 
95180 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
95190 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c  loc() routine al
951a0 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a  locates a new.**
951b0 20 6d 75 74 65 78 20 61 6e 64 20 72 65 74 75 72   mutex and retur
951c0 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ns a pointer to 
951d0 69 74 2e 20 20 49 66 20 69 74 20 72 65 74 75 72  it.  If it retur
951e0 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20  ns NULL.** that 
951f0 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74  means that a mut
95200 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20  ex could not be 
95210 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69  allocated.  SQLi
95220 74 65 0a 2a 2a 20 77 69 6c 6c 20 75 6e 77 69 6e  te.** will unwin
95230 64 20 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20  d its stack and 
95240 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e  return an error.
95250 20 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a    The argument.*
95260 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74  * to sqlite3_mut
95270 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e  ex_alloc() is on
95280 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67  e of these integ
95290 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a  er constants:.**
952a0 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e  .** <ul>.** <li>
952b0 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46    SQLITE_MUTEX_F
952c0 41 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c  AST.** <li>  SQL
952d0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
952e0 49 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c  IVE.** <li>  SQL
952f0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
95300 5f 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20  _MASTER.** <li> 
95310 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
95320 41 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e  ATIC_MEM.** <li>
95330 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53    SQLITE_MUTEX_S
95340 54 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c  TATIC_MEM2.** <l
95350 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  i>  SQLITE_MUTEX
95360 5f 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a 2a 20  _STATIC_PRNG.** 
95370 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
95380 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a 2a 2a  EX_STATIC_LRU.**
95390 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55   <li>  SQLITE_MU
953a0 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 0a  TEX_STATIC_LRU2.
953b0 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54  ** </ul>.**.** T
953c0 68 65 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e  he first two con
953d0 73 74 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c  stants cause sql
953e0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
953f0 28 29 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20  () to create.** 
95400 61 20 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68  a new mutex.  Th
95410 65 20 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72  e new mutex is r
95420 65 63 75 72 73 69 76 65 20 77 68 65 6e 20 53 51  ecursive when SQ
95430 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
95440 53 49 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20  SIVE.** is used 
95450 62 75 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72  but not necessar
95460 69 6c 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49  ily so when SQLI
95470 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73  TE_MUTEX_FAST is
95480 20 75 73 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75   used..** The mu
95490 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  tex implementati
954a0 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64  on does not need
954b0 20 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69   to make a disti
954c0 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65  nction.** betwee
954d0 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  n SQLITE_MUTEX_R
954e0 45 43 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c  ECURSIVE and SQL
954f0 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69  ITE_MUTEX_FAST i
95500 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74  f it does.** not
95510 20 77 61 6e 74 20 74 6f 2e 20 20 42 75 74 20 53   want to.  But S
95520 51 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20  QLite will only 
95530 72 65 71 75 65 73 74 20 61 20 72 65 63 75 72 73  request a recurs
95540 69 76 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20  ive mutex in.** 
95550 63 61 73 65 73 20 77 68 65 72 65 20 69 74 20 72  cases where it r
95560 65 61 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e  eally needs one.
95570 20 20 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f    If a faster no
95580 6e 2d 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  n-recursive mute
95590 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74  x.** implementat
955a0 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65  ion is available
955b0 20 6f 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61   on the host pla
955c0 74 66 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78  tform, the mutex
955d0 20 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69   subsystem.** mi
955e0 67 68 74 20 72 65 74 75 72 6e 20 73 75 63 68 20  ght return such 
955f0 61 20 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f  a mutex in respo
95600 6e 73 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55  nse to SQLITE_MU
95610 54 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20  TEX_FAST..**.** 
95620 54 68 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65  The other allowe
95630 64 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20  d parameters to 
95640 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
95650 6c 6f 63 28 29 20 65 61 63 68 20 72 65 74 75 72  loc() each retur
95660 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74  n.** a pointer t
95670 6f 20 61 20 73 74 61 74 69 63 20 70 72 65 65 78  o a static preex
95680 69 73 74 69 6e 67 20 6d 75 74 65 78 2e 20 20 53  isting mutex.  S
95690 69 78 20 73 74 61 74 69 63 20 6d 75 74 65 78 65  ix static mutexe
956a0 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79  s are.** used by
956b0 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 65 72   the current ver
956c0 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20  sion of SQLite. 
956d0 20 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73   Future versions
956e0 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61   of SQLite.** ma
956f0 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c  y add additional
95700 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e   static mutexes.
95710 20 20 53 74 61 74 69 63 20 6d 75 74 65 78 65 73    Static mutexes
95720 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61   are for interna
95730 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69  l.** use by SQLi
95740 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63  te only.  Applic
95750 61 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20  ations that use 
95760 53 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73  SQLite mutexes s
95770 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c  hould.** use onl
95780 79 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75  y the dynamic mu
95790 74 65 78 65 73 20 72 65 74 75 72 6e 65 64 20 62  texes returned b
957a0 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  y SQLITE_MUTEX_F
957b0 41 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45  AST or.** SQLITE
957c0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
957d0 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ..**.** Note tha
957e0 74 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20  t if one of the 
957f0 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 61  dynamic mutex pa
95800 72 61 6d 65 74 65 72 73 20 28 53 51 4c 49 54 45  rameters (SQLITE
95810 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f  _MUTEX_FAST.** o
95820 72 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  r SQLITE_MUTEX_R
95830 45 43 55 52 53 49 56 45 29 20 69 73 20 75 73 65  ECURSIVE) is use
95840 64 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d  d then sqlite3_m
95850 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20  utex_alloc().** 
95860 72 65 74 75 72 6e 73 20 61 20 64 69 66 66 65 72  returns a differ
95870 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 76 65  ent mutex on eve
95880 72 79 20 63 61 6c 6c 2e 20 20 42 75 74 20 66 6f  ry call.  But fo
95890 72 20 74 68 65 20 73 74 61 74 69 63 20 0a 2a 2a  r the static .**
958a0 20 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68   mutex types, th
958b0 65 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20  e same mutex is 
958c0 72 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72  returned on ever
958d0 79 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a  y call that has.
958e0 2a 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65  ** the same type
958f0 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74   number..*/.stat
95900 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ic sqlite3_mutex
95910 20 2a 70 74 68 72 65 61 64 4d 75 74 65 78 41 6c   *pthreadMutexAl
95920 6c 6f 63 28 69 6e 74 20 69 54 79 70 65 29 7b 0a  loc(int iType){.
95930 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33    static sqlite3
95940 5f 6d 75 74 65 78 20 73 74 61 74 69 63 4d 75 74  _mutex staticMut
95950 65 78 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 53  exes[] = {.    S
95960 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49  QLITE3_MUTEX_INI
95970 54 49 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51  TIALIZER,.    SQ
95980 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54  LITE3_MUTEX_INIT
95990 49 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c  IALIZER,.    SQL
959a0 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49  ITE3_MUTEX_INITI
959b0 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49  ALIZER,.    SQLI
959c0 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41  TE3_MUTEX_INITIA
959d0 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54  LIZER,.    SQLIT
959e0 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c  E3_MUTEX_INITIAL
959f0 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45  IZER,.    SQLITE
95a00 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49  3_MUTEX_INITIALI
95a10 5a 45 52 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74  ZER.  };.  sqlit
95a20 65 33 5f 6d 75 74 65 78 20 2a 70 3b 0a 20 20 73  e3_mutex *p;.  s
95a30 77 69 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a  witch( iType ){.
95a40 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
95a50 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 3a  MUTEX_RECURSIVE:
95a60 20 7b 0a 20 20 20 20 20 20 70 20 3d 20 73 71 6c   {.      p = sql
95a70 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20  ite3MallocZero( 
95a80 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20  sizeof(*p) );.  
95a90 20 20 20 20 69 66 28 20 70 20 29 7b 0a 23 69 66      if( p ){.#if
95aa0 64 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47  def SQLITE_HOMEG
95ab0 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d  ROWN_RECURSIVE_M
95ac0 55 54 45 58 0a 20 20 20 20 20 20 20 20 2f 2a 20  UTEX.        /* 
95ad0 49 66 20 72 65 63 75 72 73 69 76 65 20 6d 75 74  If recursive mut
95ae0 65 78 65 73 20 61 72 65 20 6e 6f 74 20 61 76 61  exes are not ava
95af0 69 6c 61 62 6c 65 2c 20 77 65 20 77 69 6c 6c 20  ilable, we will 
95b00 68 61 76 65 20 74 6f 0a 20 20 20 20 20 20 20 20  have to.        
95b10 2a 2a 20 62 75 69 6c 64 20 6f 75 72 20 6f 77 6e  ** build our own
95b20 2e 20 20 53 65 65 20 62 65 6c 6f 77 2e 20 2a 2f  .  See below. */
95b30 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64  .        pthread
95b40 5f 6d 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e  _mutex_init(&p->
95b50 6d 75 74 65 78 2c 20 30 29 3b 0a 23 65 6c 73 65  mutex, 0);.#else
95b60 0a 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20  .        /* Use 
95b70 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  a recursive mute
95b80 78 20 69 66 20 69 74 20 69 73 20 61 76 61 69 6c  x if it is avail
95b90 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 20  able */.        
95ba0 70 74 68 72 65 61 64 5f 6d 75 74 65 78 61 74 74  pthread_mutexatt
95bb0 72 5f 74 20 72 65 63 75 72 73 69 76 65 41 74 74  r_t recursiveAtt
95bc0 72 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72 65  r;.        pthre
95bd0 61 64 5f 6d 75 74 65 78 61 74 74 72 5f 69 6e 69  ad_mutexattr_ini
95be0 74 28 26 72 65 63 75 72 73 69 76 65 41 74 74 72  t(&recursiveAttr
95bf0 29 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72 65  );.        pthre
95c00 61 64 5f 6d 75 74 65 78 61 74 74 72 5f 73 65 74  ad_mutexattr_set
95c10 74 79 70 65 28 26 72 65 63 75 72 73 69 76 65 41  type(&recursiveA
95c20 74 74 72 2c 20 50 54 48 52 45 41 44 5f 4d 55 54  ttr, PTHREAD_MUT
95c30 45 58 5f 52 45 43 55 52 53 49 56 45 29 3b 0a 20  EX_RECURSIVE);. 
95c40 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d         pthread_m
95c50 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d 75  utex_init(&p->mu
95c60 74 65 78 2c 20 26 72 65 63 75 72 73 69 76 65 41  tex, &recursiveA
95c70 74 74 72 29 3b 0a 20 20 20 20 20 20 20 20 70 74  ttr);.        pt
95c80 68 72 65 61 64 5f 6d 75 74 65 78 61 74 74 72 5f  hread_mutexattr_
95c90 64 65 73 74 72 6f 79 28 26 72 65 63 75 72 73 69  destroy(&recursi
95ca0 76 65 41 74 74 72 29 3b 0a 23 65 6e 64 69 66 0a  veAttr);.#endif.
95cb0 20 20 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20          p->id = 
95cc0 69 54 79 70 65 3b 0a 20 20 20 20 20 20 7d 0a 20  iType;.      }. 
95cd0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
95ce0 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  }.    case SQLIT
95cf0 45 5f 4d 55 54 45 58 5f 46 41 53 54 3a 20 7b 0a  E_MUTEX_FAST: {.
95d00 20 20 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65        p = sqlite
95d10 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a  3MallocZero( siz
95d20 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20  eof(*p) );.     
95d30 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20   if( p ){.      
95d40 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b    p->id = iType;
95d50 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64  .        pthread
95d60 5f 6d 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e  _mutex_init(&p->
95d70 6d 75 74 65 78 2c 20 30 29 3b 0a 20 20 20 20 20  mutex, 0);.     
95d80 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
95d90 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c      }.    defaul
95da0 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72  t: {.      asser
95db0 74 28 20 69 54 79 70 65 2d 32 20 3e 3d 20 30 20  t( iType-2 >= 0 
95dc0 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
95dd0 20 69 54 79 70 65 2d 32 20 3c 20 41 72 72 61 79   iType-2 < Array
95de0 53 69 7a 65 28 73 74 61 74 69 63 4d 75 74 65 78  Size(staticMutex
95df0 65 73 29 20 29 3b 0a 20 20 20 20 20 20 70 20 3d  es) );.      p =
95e00 20 26 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b   &staticMutexes[
95e10 69 54 79 70 65 2d 32 5d 3b 0a 20 20 20 20 20 20  iType-2];.      
95e20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20  p->id = iType;. 
95e30 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
95e40 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  }.  }.  return p
95e50 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ;.}.../*.** This
95e60 20 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63   routine dealloc
95e70 61 74 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c  ates a previousl
95e80 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 6d  y.** allocated m
95e90 75 74 65 78 2e 20 20 53 51 4c 69 74 65 20 69 73  utex.  SQLite is
95ea0 20 63 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c   careful to deal
95eb0 6c 6f 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20  locate every.** 
95ec0 6d 75 74 65 78 20 74 68 61 74 20 69 74 20 61 6c  mutex that it al
95ed0 6c 6f 63 61 74 65 73 2e 0a 2a 2f 0a 73 74 61 74  locates..*/.stat
95ee0 69 63 20 76 6f 69 64 20 70 74 68 72 65 61 64 4d  ic void pthreadM
95ef0 75 74 65 78 46 72 65 65 28 73 71 6c 69 74 65 33  utexFree(sqlite3
95f00 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73  _mutex *p){.  as
95f10 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30  sert( p->nRef==0
95f20 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d   );.  assert( p-
95f30 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45  >id==SQLITE_MUTE
95f40 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 3d  X_FAST || p->id=
95f50 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45  =SQLITE_MUTEX_RE
95f60 43 55 52 53 49 56 45 20 29 3b 0a 20 20 70 74 68  CURSIVE );.  pth
95f70 72 65 61 64 5f 6d 75 74 65 78 5f 64 65 73 74 72  read_mutex_destr
95f80 6f 79 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20  oy(&p->mutex);. 
95f90 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29   sqlite3_free(p)
95fa0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  ;.}../*.** The s
95fb0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
95fc0 65 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  er() and sqlite3
95fd0 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75  _mutex_try() rou
95fe0 74 69 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a  tines attempt.**
95ff0 20 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65   to enter a mute
96000 78 2e 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74  x.  If another t
96010 68 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 79  hread is already
96020 20 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65   within the mute
96030 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75  x,.** sqlite3_mu
96040 74 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c  tex_enter() will
96050 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74   block and sqlit
96060 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77  e3_mutex_try() w
96070 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51  ill return.** SQ
96080 4c 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20  LITE_BUSY.  The 
96090 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
960a0 79 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65  y() interface re
960b0 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a  turns SQLITE_OK.
960c0 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66  ** upon successf
960d0 75 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78  ul entry.  Mutex
960e0 65 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67  es created using
960f0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
96100 43 55 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62  CURSIVE can.** b
96110 65 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70  e entered multip
96120 6c 65 20 74 69 6d 65 73 20 62 79 20 74 68 65 20  le times by the 
96130 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e  same thread.  In
96140 20 73 75 63 68 20 63 61 73 65 73 20 74 68 65 2c   such cases the,
96150 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62  .** mutex must b
96160 65 20 65 78 69 74 65 64 20 61 6e 20 65 71 75 61  e exited an equa
96170 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65  l number of time
96180 73 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72  s before another
96190 20 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65   thread.** can e
961a0 6e 74 65 72 2e 20 20 49 66 20 74 68 65 20 73 61  nter.  If the sa
961b0 6d 65 20 74 68 72 65 61 64 20 74 72 69 65 73 20  me thread tries 
961c0 74 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68  to enter any oth
961d0 65 72 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78  er kind of mutex
961e0 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e  .** more than on
961f0 63 65 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72  ce, the behavior
96200 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a   is undefined..*
96210 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 74  /.static void pt
96220 68 72 65 61 64 4d 75 74 65 78 45 6e 74 65 72 28  hreadMutexEnter(
96230 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
96240 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  ){.  assert( p->
96250 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58  id==SQLITE_MUTEX
96260 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 70 74  _RECURSIVE || pt
96270 68 72 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 6c  hreadMutexNothel
96280 64 28 70 29 20 29 3b 0a 0a 23 69 66 64 65 66 20  d(p) );..#ifdef 
96290 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e  SQLITE_HOMEGROWN
962a0 5f 52 45 43 55 52 53 49 56 45 5f 4d 55 54 45 58  _RECURSIVE_MUTEX
962b0 0a 20 20 2f 2a 20 49 66 20 72 65 63 75 72 73 69  .  /* If recursi
962c0 76 65 20 6d 75 74 65 78 65 73 20 61 72 65 20 6e  ve mutexes are n
962d0 6f 74 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68  ot available, th
962e0 65 6e 20 77 65 20 68 61 76 65 20 74 6f 20 67 72  en we have to gr
962f0 6f 77 0a 20 20 2a 2a 20 6f 75 72 20 6f 77 6e 2e  ow.  ** our own.
96300 20 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74    This implement
96310 61 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68  ation assumes th
96320 61 74 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c  at pthread_equal
96330 28 29 0a 20 20 2a 2a 20 69 73 20 61 74 6f 6d 69  ().  ** is atomi
96340 63 20 2d 20 74 68 61 74 20 69 74 20 63 61 6e 6e  c - that it cann
96350 6f 74 20 62 65 20 64 65 63 65 69 76 65 64 20 69  ot be deceived i
96360 6e 74 6f 20 74 68 69 6e 6b 69 6e 67 20 73 65 6c  nto thinking sel
96370 66 0a 20 20 2a 2a 20 61 6e 64 20 70 2d 3e 6f 77  f.  ** and p->ow
96380 6e 65 72 20 61 72 65 20 65 71 75 61 6c 20 69 66  ner are equal if
96390 20 70 2d 3e 6f 77 6e 65 72 20 63 68 61 6e 67 65   p->owner change
963a0 73 20 62 65 74 77 65 65 6e 20 74 77 6f 20 76 61  s between two va
963b0 6c 75 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 61  lues.  ** that a
963c0 72 65 20 6e 6f 74 20 65 71 75 61 6c 20 74 6f 20  re not equal to 
963d0 73 65 6c 66 20 77 68 69 6c 65 20 74 68 65 20 63  self while the c
963e0 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 74 61 6b  omparison is tak
963f0 69 6e 67 20 70 6c 61 63 65 2e 0a 20 20 2a 2a 20  ing place..  ** 
96400 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74  This implementat
96410 69 6f 6e 20 61 6c 73 6f 20 61 73 73 75 6d 65 73  ion also assumes
96420 20 61 20 63 6f 68 65 72 65 6e 74 20 63 61 63 68   a coherent cach
96430 65 20 2d 20 74 68 61 74 20 0a 20 20 2a 2a 20 73  e - that .  ** s
96440 65 70 61 72 61 74 65 20 70 72 6f 63 65 73 73 65  eparate processe
96450 73 20 63 61 6e 6e 6f 74 20 72 65 61 64 20 64 69  s cannot read di
96460 66 66 65 72 65 6e 74 20 76 61 6c 75 65 73 20 66  fferent values f
96470 72 6f 6d 20 74 68 65 20 73 61 6d 65 0a 20 20 2a  rom the same.  *
96480 2a 20 61 64 64 72 65 73 73 20 61 74 20 74 68 65  * address at the
96490 20 73 61 6d 65 20 74 69 6d 65 2e 20 20 49 66 20   same time.  If 
964a0 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20  either of these 
964b0 74 77 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 0a 20  two conditions. 
964c0 20 2a 2a 20 61 72 65 20 6e 6f 74 20 6d 65 74 2c   ** are not met,
964d0 20 74 68 65 6e 20 74 68 65 20 6d 75 74 65 78 65   then the mutexe
964e0 73 20 77 69 6c 6c 20 66 61 69 6c 20 61 6e 64 20  s will fail and 
964f0 70 72 6f 62 6c 65 6d 73 20 77 69 6c 6c 20 72 65  problems will re
96500 73 75 6c 74 2e 0a 20 20 2a 2f 0a 20 20 7b 0a 20  sult..  */.  {. 
96510 20 20 20 70 74 68 72 65 61 64 5f 74 20 73 65 6c     pthread_t sel
96520 66 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66  f = pthread_self
96530 28 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e  ();.    if( p->n
96540 52 65 66 3e 30 20 26 26 20 70 74 68 72 65 61 64  Ref>0 && pthread
96550 5f 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c  _equal(p->owner,
96560 20 73 65 6c 66 29 20 29 7b 0a 20 20 20 20 20 20   self) ){.      
96570 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d  p->nRef++;.    }
96580 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 74 68 72  else{.      pthr
96590 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 26  ead_mutex_lock(&
965a0 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20  p->mutex);.     
965b0 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66   assert( p->nRef
965c0 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e  ==0 );.      p->
965d0 6f 77 6e 65 72 20 3d 20 73 65 6c 66 3b 0a 20 20  owner = self;.  
965e0 20 20 20 20 70 2d 3e 6e 52 65 66 20 3d 20 31 3b      p->nRef = 1;
965f0 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65  .    }.  }.#else
96600 0a 20 20 2f 2a 20 55 73 65 20 74 68 65 20 62 75  .  /* Use the bu
96610 69 6c 74 2d 69 6e 20 72 65 63 75 72 73 69 76 65  ilt-in recursive
96620 20 6d 75 74 65 78 65 73 20 69 66 20 74 68 65 79   mutexes if they
96630 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2e 0a   are available..
96640 20 20 2a 2f 0a 20 20 70 74 68 72 65 61 64 5f 6d    */.  pthread_m
96650 75 74 65 78 5f 6c 6f 63 6b 28 26 70 2d 3e 6d 75  utex_lock(&p->mu
96660 74 65 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72  tex);.  p->owner
96670 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28   = pthread_self(
96680 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a  );.  p->nRef++;.
96690 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
966a0 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 69 66  QLITE_DEBUG.  if
966b0 28 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20  ( p->trace ){.  
966c0 20 20 70 72 69 6e 74 66 28 22 65 6e 74 65 72 20    printf("enter 
966d0 6d 75 74 65 78 20 25 70 20 28 25 64 29 20 77 69  mutex %p (%d) wi
966e0 74 68 20 6e 52 65 66 3d 25 64 5c 6e 22 2c 20 70  th nRef=%d\n", p
966f0 2c 20 70 2d 3e 74 72 61 63 65 2c 20 70 2d 3e 6e  , p->trace, p->n
96700 52 65 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  Ref);.  }.#endif
96710 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74  .}.static int pt
96720 68 72 65 61 64 4d 75 74 65 78 54 72 79 28 73 71  hreadMutexTry(sq
96730 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b  lite3_mutex *p){
96740 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73  .  int rc;.  ass
96750 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49  ert( p->id==SQLI
96760 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
96770 56 45 20 7c 7c 20 70 74 68 72 65 61 64 4d 75 74  VE || pthreadMut
96780 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a  exNotheld(p) );.
96790 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48  .#ifdef SQLITE_H
967a0 4f 4d 45 47 52 4f 57 4e 5f 52 45 43 55 52 53 49  OMEGROWN_RECURSI
967b0 56 45 5f 4d 55 54 45 58 0a 20 20 2f 2a 20 49 66  VE_MUTEX.  /* If
967c0 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78   recursive mutex
967d0 65 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c  es are not avail
967e0 61 62 6c 65 2c 20 74 68 65 6e 20 77 65 20 68 61  able, then we ha
967f0 76 65 20 74 6f 20 67 72 6f 77 0a 20 20 2a 2a 20  ve to grow.  ** 
96800 6f 75 72 20 6f 77 6e 2e 20 20 54 68 69 73 20 69  our own.  This i
96810 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 73  mplementation as
96820 73 75 6d 65 73 20 74 68 61 74 20 70 74 68 72 65  sumes that pthre
96830 61 64 5f 65 71 75 61 6c 28 29 0a 20 20 2a 2a 20  ad_equal().  ** 
96840 69 73 20 61 74 6f 6d 69 63 20 2d 20 74 68 61 74  is atomic - that
96850 20 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 64 65   it cannot be de
96860 63 65 69 76 65 64 20 69 6e 74 6f 20 74 68 69 6e  ceived into thin
96870 6b 69 6e 67 20 73 65 6c 66 0a 20 20 2a 2a 20 61  king self.  ** a
96880 6e 64 20 70 2d 3e 6f 77 6e 65 72 20 61 72 65 20  nd p->owner are 
96890 65 71 75 61 6c 20 69 66 20 70 2d 3e 6f 77 6e 65  equal if p->owne
968a0 72 20 63 68 61 6e 67 65 73 20 62 65 74 77 65 65  r changes betwee
968b0 6e 20 74 77 6f 20 76 61 6c 75 65 73 0a 20 20 2a  n two values.  *
968c0 2a 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20 65  * that are not e
968d0 71 75 61 6c 20 74 6f 20 73 65 6c 66 20 77 68 69  qual to self whi
968e0 6c 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  le the compariso
968f0 6e 20 69 73 20 74 61 6b 69 6e 67 20 70 6c 61 63  n is taking plac
96900 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 6d 70  e..  ** This imp
96910 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 6c 73 6f  lementation also
96920 20 61 73 73 75 6d 65 73 20 61 20 63 6f 68 65 72   assumes a coher
96930 65 6e 74 20 63 61 63 68 65 20 2d 20 74 68 61 74  ent cache - that
96940 20 0a 20 20 2a 2a 20 73 65 70 61 72 61 74 65 20   .  ** separate 
96950 70 72 6f 63 65 73 73 65 73 20 63 61 6e 6e 6f 74  processes cannot
96960 20 72 65 61 64 20 64 69 66 66 65 72 65 6e 74 20   read different 
96970 76 61 6c 75 65 73 20 66 72 6f 6d 20 74 68 65 20  values from the 
96980 73 61 6d 65 0a 20 20 2a 2a 20 61 64 64 72 65 73  same.  ** addres
96990 73 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69  s at the same ti
969a0 6d 65 2e 20 20 49 66 20 65 69 74 68 65 72 20 6f  me.  If either o
969b0 66 20 74 68 65 73 65 20 74 77 6f 20 63 6f 6e 64  f these two cond
969c0 69 74 69 6f 6e 73 0a 20 20 2a 2a 20 61 72 65 20  itions.  ** are 
969d0 6e 6f 74 20 6d 65 74 2c 20 74 68 65 6e 20 74 68  not met, then th
969e0 65 20 6d 75 74 65 78 65 73 20 77 69 6c 6c 20 66  e mutexes will f
969f0 61 69 6c 20 61 6e 64 20 70 72 6f 62 6c 65 6d 73  ail and problems
96a00 20 77 69 6c 6c 20 72 65 73 75 6c 74 2e 0a 20 20   will result..  
96a10 2a 2f 0a 20 20 7b 0a 20 20 20 20 70 74 68 72 65  */.  {.    pthre
96a20 61 64 5f 74 20 73 65 6c 66 20 3d 20 70 74 68 72  ad_t self = pthr
96a30 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20 20  ead_self();.    
96a40 69 66 28 20 70 2d 3e 6e 52 65 66 3e 30 20 26 26  if( p->nRef>0 &&
96a50 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70   pthread_equal(p
96a60 2d 3e 6f 77 6e 65 72 2c 20 73 65 6c 66 29 20 29  ->owner, self) )
96a70 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 2b  {.      p->nRef+
96a80 2b 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  +;.      rc = SQ
96a90 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c  LITE_OK;.    }el
96aa0 73 65 20 69 66 28 20 70 74 68 72 65 61 64 5f 6d  se if( pthread_m
96ab0 75 74 65 78 5f 74 72 79 6c 6f 63 6b 28 26 70 2d  utex_trylock(&p-
96ac0 3e 6d 75 74 65 78 29 3d 3d 30 20 29 7b 0a 20 20  >mutex)==0 ){.  
96ad0 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e      assert( p->n
96ae0 52 65 66 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  Ref==0 );.      
96af0 70 2d 3e 6f 77 6e 65 72 20 3d 20 73 65 6c 66 3b  p->owner = self;
96b00 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 20 3d  .      p->nRef =
96b10 20 31 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   1;.      rc = S
96b20 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65  QLITE_OK;.    }e
96b30 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  lse{.      rc = 
96b40 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20  SQLITE_BUSY;.   
96b50 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 2f   }.  }.#else.  /
96b60 2a 20 55 73 65 20 74 68 65 20 62 75 69 6c 74 2d  * Use the built-
96b70 69 6e 20 72 65 63 75 72 73 69 76 65 20 6d 75 74  in recursive mut
96b80 65 78 65 73 20 69 66 20 74 68 65 79 20 61 72 65  exes if they are
96b90 20 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f   available..  */
96ba0 0a 20 20 69 66 28 20 70 74 68 72 65 61 64 5f 6d  .  if( pthread_m
96bb0 75 74 65 78 5f 74 72 79 6c 6f 63 6b 28 26 70 2d  utex_trylock(&p-
96bc0 3e 6d 75 74 65 78 29 3d 3d 30 20 29 7b 0a 20 20  >mutex)==0 ){.  
96bd0 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 70 74 68    p->owner = pth
96be0 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20  read_self();.   
96bf0 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20   p->nRef++;.    
96c00 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
96c10 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
96c20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20  = SQLITE_BUSY;. 
96c30 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65   }.#endif..#ifde
96c40 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
96c50 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
96c60 4f 4b 20 26 26 20 70 2d 3e 74 72 61 63 65 20 29  OK && p->trace )
96c70 7b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 65 6e  {.    printf("en
96c80 74 65 72 20 6d 75 74 65 78 20 25 70 20 28 25 64  ter mutex %p (%d
96c90 29 20 77 69 74 68 20 6e 52 65 66 3d 25 64 5c 6e  ) with nRef=%d\n
96ca0 22 2c 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c 20  ", p, p->trace, 
96cb0 70 2d 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 23 65  p->nRef);.  }.#e
96cc0 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 72 63  ndif.  return rc
96cd0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  ;.}../*.** The s
96ce0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
96cf0 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69  ve() routine exi
96d00 74 73 20 61 20 6d 75 74 65 78 20 74 68 61 74 20  ts a mutex that 
96d10 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c  was.** previousl
96d20 79 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 65  y entered by the
96d30 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54   same thread.  T
96d40 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69  he behavior.** i
96d50 73 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74  s undefined if t
96d60 68 65 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20  he mutex is not 
96d70 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65  currently entere
96d80 64 20 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63  d or.** is not c
96d90 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74  urrently allocat
96da0 65 64 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c  ed.  SQLite will
96db0 20 6e 65 76 65 72 20 64 6f 20 65 69 74 68 65 72   never do either
96dc0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
96dd0 20 70 74 68 72 65 61 64 4d 75 74 65 78 4c 65 61   pthreadMutexLea
96de0 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ve(sqlite3_mutex
96df0 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20   *p){.  assert( 
96e00 70 74 68 72 65 61 64 4d 75 74 65 78 48 65 6c 64  pthreadMutexHeld
96e10 28 70 29 20 29 3b 0a 20 20 70 2d 3e 6e 52 65 66  (p) );.  p->nRef
96e20 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  --;.  assert( p-
96e30 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e 69  >nRef==0 || p->i
96e40 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  d==SQLITE_MUTEX_
96e50 52 45 43 55 52 53 49 56 45 20 29 3b 0a 0a 23 69  RECURSIVE );..#i
96e60 66 64 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45  fdef SQLITE_HOME
96e70 47 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 5f  GROWN_RECURSIVE_
96e80 4d 55 54 45 58 0a 20 20 69 66 28 20 70 2d 3e 6e  MUTEX.  if( p->n
96e90 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 70 74  Ref==0 ){.    pt
96ea0 68 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f  hread_mutex_unlo
96eb0 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20  ck(&p->mutex);. 
96ec0 20 7d 0a 23 65 6c 73 65 0a 20 20 70 74 68 72 65   }.#else.  pthre
96ed0 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28  ad_mutex_unlock(
96ee0 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 23 65 6e 64  &p->mutex);.#end
96ef0 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
96f00 45 5f 44 45 42 55 47 0a 20 20 69 66 28 20 70 2d  E_DEBUG.  if( p-
96f10 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20 70 72  >trace ){.    pr
96f20 69 6e 74 66 28 22 6c 65 61 76 65 20 6d 75 74 65  intf("leave mute
96f30 78 20 25 70 20 28 25 64 29 20 77 69 74 68 20 6e  x %p (%d) with n
96f40 52 65 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 2d  Ref=%d\n", p, p-
96f50 3e 74 72 61 63 65 2c 20 70 2d 3e 6e 52 65 66 29  >trace, p->nRef)
96f60 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a  ;.  }.#endif.}..
96f70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73  SQLITE_PRIVATE s
96f80 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74  qlite3_mutex_met
96f90 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66  hods *sqlite3Def
96fa0 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 7b  aultMutex(void){
96fb0 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65  .  static sqlite
96fc0 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20  3_mutex_methods 
96fd0 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 70  sMutex = {.    p
96fe0 74 68 72 65 61 64 4d 75 74 65 78 49 6e 69 74 2c  threadMutexInit,
96ff0 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65  .    pthreadMute
97000 78 45 6e 64 2c 0a 20 20 20 20 70 74 68 72 65 61  xEnd,.    pthrea
97010 64 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 20 20 20  dMutexAlloc,.   
97020 20 70 74 68 72 65 61 64 4d 75 74 65 78 46 72 65   pthreadMutexFre
97030 65 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75  e,.    pthreadMu
97040 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 70 74  texEnter,.    pt
97050 68 72 65 61 64 4d 75 74 65 78 54 72 79 2c 0a 20  hreadMutexTry,. 
97060 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 4c     pthreadMutexL
97070 65 61 76 65 2c 0a 23 69 66 64 65 66 20 53 51 4c  eave,.#ifdef SQL
97080 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 70 74  ITE_DEBUG.    pt
97090 68 72 65 61 64 4d 75 74 65 78 48 65 6c 64 2c 0a  hreadMutexHeld,.
970a0 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78      pthreadMutex
970b0 4e 6f 74 68 65 6c 64 0a 23 65 6c 73 65 0a 20 20  Notheld.#else.  
970c0 20 20 30 2c 0a 20 20 20 20 30 0a 23 65 6e 64 69    0,.    0.#endi
970d0 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e  f.  };..  return
970e0 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 0a 23 65 6e   &sMutex;.}..#en
970f0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55  dif /* SQLITE_MU
97100 54 45 58 5f 50 54 48 52 45 41 44 20 2a 2f 0a 0a  TEX_PTHREAD */..
97110 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
97120 45 6e 64 20 6f 66 20 6d 75 74 65 78 5f 75 6e 69  End of mutex_uni
97130 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  x.c ************
97140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
97160 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
97170 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78  Begin file mutex
97180 5f 77 33 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  _w32.c *********
97190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
971a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
971b0 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73  /*.** 2007 Augus
971c0 74 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  t 14.**.** The a
971d0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
971e0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
971f0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
97200 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
97210 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
97220 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
97230 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
97240 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
97250 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
97260 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
97270 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
97280 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
97290 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
972a0 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
972b0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
972c0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
972d0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
972e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
972f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97320 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
97330 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20   contains the C 
97340 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69  functions that i
97350 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73  mplement mutexes
97360 20 66 6f 72 20 77 69 6e 33 32 0a 2a 2a 0a 2a 2a   for win32.**.**
97370 20 24 49 64 3a 20 6d 75 74 65 78 5f 77 33 32 2e   $Id: mutex_w32.
97380 63 2c 76 20 31 2e 31 38 20 32 30 30 39 2f 30 38  c,v 1.18 2009/08
97390 2f 31 30 20 30 33 3a 32 33 3a 32 31 20 73 68 61  /10 03:23:21 sha
973a0 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a  ne Exp $.*/../*.
973b0 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74  ** The code in t
973c0 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79  his file is only
973d0 20 75 73 65 64 20 69 66 20 77 65 20 61 72 65 20   used if we are 
973e0 63 6f 6d 70 69 6c 69 6e 67 20 6d 75 6c 74 69 74  compiling multit
973f0 68 72 65 61 64 65 64 0a 2a 2a 20 6f 6e 20 61 20  hreaded.** on a 
97400 77 69 6e 33 32 20 73 79 73 74 65 6d 2e 0a 2a 2f  win32 system..*/
97410 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d  .#ifdef SQLITE_M
97420 55 54 45 58 5f 57 33 32 0a 0a 2f 2a 0a 2a 2a 20  UTEX_W32../*.** 
97430 45 61 63 68 20 72 65 63 75 72 73 69 76 65 20 6d  Each recursive m
97440 75 74 65 78 20 69 73 20 61 6e 20 69 6e 73 74 61  utex is an insta
97450 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
97460 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a  wing structure..
97470 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65  */.struct sqlite
97480 33 5f 6d 75 74 65 78 20 7b 0a 20 20 43 52 49 54  3_mutex {.  CRIT
97490 49 43 41 4c 5f 53 45 43 54 49 4f 4e 20 6d 75 74  ICAL_SECTION mut
974a0 65 78 3b 20 20 20 20 2f 2a 20 4d 75 74 65 78 20  ex;    /* Mutex 
974b0 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20  controlling the 
974c0 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 64  lock */.  int id
974d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
974e0 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79       /* Mutex ty
974f0 70 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66  pe */.  int nRef
97500 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
97510 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
97520 65 6e 74 65 72 61 6e 63 65 73 20 2a 2f 0a 20 20  enterances */.  
97530 44 57 4f 52 44 20 6f 77 6e 65 72 3b 20 20 20 20  DWORD owner;    
97540 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
97550 72 65 61 64 20 68 6f 6c 64 69 6e 67 20 74 68 69  read holding thi
97560 73 20 6d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f  s mutex */.};../
97570 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65  *.** Return true
97580 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 77   (non-zero) if w
97590 65 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 75 6e  e are running un
975a0 64 65 72 20 57 69 6e 4e 54 2c 20 57 69 6e 32 4b  der WinNT, Win2K
975b0 2c 20 57 69 6e 58 50 2c 0a 2a 2a 20 6f 72 20 57  , WinXP,.** or W
975c0 69 6e 43 45 2e 20 20 52 65 74 75 72 6e 20 66 61  inCE.  Return fa
975d0 6c 73 65 20 28 7a 65 72 6f 29 20 66 6f 72 20 57  lse (zero) for W
975e0 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 6f 72 20  in95, Win98, or 
975f0 57 69 6e 4d 45 2e 0a 2a 2a 0a 2a 2a 20 48 65 72  WinME..**.** Her
97600 65 20 69 73 20 61 6e 20 69 6e 74 65 72 65 73 74  e is an interest
97610 69 6e 67 20 6f 62 73 65 72 76 61 74 69 6f 6e 3a  ing observation:
97620 20 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20    Win95, Win98, 
97630 61 6e 64 20 57 69 6e 4d 45 20 6c 61 63 6b 0a 2a  and WinME lack.*
97640 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78  * the LockFileEx
97650 28 29 20 41 50 49 2e 20 20 42 75 74 20 77 65 20  () API.  But we 
97660 63 61 6e 20 73 74 69 6c 6c 20 73 74 61 74 69 63  can still static
97670 61 6c 6c 79 20 6c 69 6e 6b 20 61 67 61 69 6e 73  ally link agains
97680 74 20 74 68 61 74 0a 2a 2a 20 41 50 49 20 61 73  t that.** API as
97690 20 6c 6f 6e 67 20 61 73 20 77 65 20 64 6f 6e 27   long as we don'
976a0 74 20 63 61 6c 6c 20 69 74 20 77 69 6e 20 72 75  t call it win ru
976b0 6e 6e 69 6e 67 20 57 69 6e 39 35 2f 39 38 2f 4d  nning Win95/98/M
976c0 45 2e 20 20 41 20 63 61 6c 6c 20 74 6f 0a 2a 2a  E.  A call to.**
976d0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
976e0 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69   used to determi
976f0 6e 65 20 69 66 20 74 68 65 20 68 6f 73 74 20 69  ne if the host i
97700 73 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20 6f 72  s Win95/98/ME or
97710 0a 2a 2a 20 57 69 6e 4e 54 2f 32 4b 2f 58 50 20  .** WinNT/2K/XP 
97720 73 6f 20 74 68 61 74 20 77 65 20 77 69 6c 6c 20  so that we will 
97730 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 6f 72 20  know whether or 
97740 6e 6f 74 20 77 65 20 63 61 6e 20 73 61 66 65 6c  not we can safel
97750 79 20 63 61 6c 6c 0a 2a 2a 20 74 68 65 20 4c 6f  y call.** the Lo
97760 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e 0a  ckFileEx() API..
97770 2a 2a 0a 2a 2a 20 6d 75 74 65 78 49 73 4e 54 28  **.** mutexIsNT(
97780 29 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 66  ) is only used f
97790 6f 72 20 74 68 65 20 54 72 79 45 6e 74 65 72 43  or the TryEnterC
977a0 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 29  riticalSection()
977b0 20 41 50 49 20 63 61 6c 6c 2c 0a 2a 2a 20 77 68   API call,.** wh
977c0 69 63 68 20 69 73 20 6f 6e 6c 79 20 61 76 61 69  ich is only avai
977d0 6c 61 62 6c 65 20 69 66 20 79 6f 75 72 20 61 70  lable if your ap
977e0 70 6c 69 63 61 74 69 6f 6e 20 77 61 73 20 63 6f  plication was co
977f0 6d 70 69 6c 65 64 20 77 69 74 68 20 0a 2a 2a 20  mpiled with .** 
97800 5f 57 49 4e 33 32 5f 57 49 4e 4e 54 20 64 65 66  _WIN32_WINNT def
97810 69 6e 65 64 20 74 6f 20 61 20 76 61 6c 75 65 20  ined to a value 
97820 3e 3d 20 30 78 30 34 30 30 2e 20 20 43 75 72 72  >= 0x0400.  Curr
97830 65 6e 74 6c 79 2c 20 74 68 65 20 6f 6e 6c 79 0a  ently, the only.
97840 2a 2a 20 63 61 6c 6c 20 74 6f 20 54 72 79 45 6e  ** call to TryEn
97850 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 74 69  terCriticalSecti
97860 6f 6e 28 29 20 69 73 20 23 69 66 64 65 66 27 65  on() is #ifdef'e
97870 64 20 6f 75 74 2c 20 73 6f 20 23 69 66 64 65 66  d out, so #ifdef
97880 20 0a 2a 2a 20 74 68 69 73 20 6f 75 74 20 61 73   .** this out as
97890 20 77 65 6c 6c 2e 0a 2a 2f 0a 23 69 66 20 30 0a   well..*/.#if 0.
978a0 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
978b0 4e 43 45 0a 23 20 64 65 66 69 6e 65 20 6d 75 74  NCE.# define mut
978c0 65 78 49 73 4e 54 28 29 20 20 28 31 29 0a 23 65  exIsNT()  (1).#e
978d0 6c 73 65 0a 20 20 73 74 61 74 69 63 20 69 6e 74  lse.  static int
978e0 20 6d 75 74 65 78 49 73 4e 54 28 76 6f 69 64 29   mutexIsNT(void)
978f0 7b 0a 20 20 20 20 73 74 61 74 69 63 20 69 6e 74  {.    static int
97900 20 6f 73 54 79 70 65 20 3d 20 30 3b 0a 20 20 20   osType = 0;.   
97910 20 69 66 28 20 6f 73 54 79 70 65 3d 3d 30 20 29   if( osType==0 )
97920 7b 0a 20 20 20 20 20 20 4f 53 56 45 52 53 49 4f  {.      OSVERSIO
97930 4e 49 4e 46 4f 20 73 49 6e 66 6f 3b 0a 20 20 20  NINFO sInfo;.   
97940 20 20 20 73 49 6e 66 6f 2e 64 77 4f 53 56 65 72     sInfo.dwOSVer
97950 73 69 6f 6e 49 6e 66 6f 53 69 7a 65 20 3d 20 73  sionInfoSize = s
97960 69 7a 65 6f 66 28 73 49 6e 66 6f 29 3b 0a 20 20  izeof(sInfo);.  
97970 20 20 20 20 47 65 74 56 65 72 73 69 6f 6e 45 78      GetVersionEx
97980 28 26 73 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20  (&sInfo);.      
97990 6f 73 54 79 70 65 20 3d 20 73 49 6e 66 6f 2e 64  osType = sInfo.d
979a0 77 50 6c 61 74 66 6f 72 6d 49 64 3d 3d 56 45 52  wPlatformId==VER
979b0 5f 50 4c 41 54 46 4f 52 4d 5f 57 49 4e 33 32 5f  _PLATFORM_WIN32_
979c0 4e 54 20 3f 20 32 20 3a 20 31 3b 0a 20 20 20 20  NT ? 2 : 1;.    
979d0 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 73 54  }.    return osT
979e0 79 70 65 3d 3d 32 3b 0a 20 20 7d 0a 23 65 6e 64  ype==2;.  }.#end
979f0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f  if /* SQLITE_OS_
97a00 57 49 4e 43 45 20 2a 2f 0a 23 65 6e 64 69 66 0a  WINCE */.#endif.
97a10 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
97a20 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  EBUG./*.** The s
97a30 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
97a40 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  d() and sqlite3_
97a50 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20  mutex_notheld() 
97a60 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69  routine are.** i
97a70 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20  ntended for use 
97a80 6f 6e 6c 79 20 69 6e 73 69 64 65 20 61 73 73 65  only inside asse
97a90 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e  rt() statements.
97aa0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
97ab0 69 6e 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69  inMutexHeld(sqli
97ac0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
97ad0 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 21   return p->nRef!
97ae0 3d 30 20 26 26 20 70 2d 3e 6f 77 6e 65 72 3d 3d  =0 && p->owner==
97af0 47 65 74 43 75 72 72 65 6e 74 54 68 72 65 61 64  GetCurrentThread
97b00 49 64 28 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69  Id();.}.static i
97b10 6e 74 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65  nt winMutexNothe
97b20 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ld(sqlite3_mutex
97b30 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70   *p){.  return p
97b40 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e  ->nRef==0 || p->
97b50 6f 77 6e 65 72 21 3d 47 65 74 43 75 72 72 65 6e  owner!=GetCurren
97b60 74 54 68 72 65 61 64 49 64 28 29 3b 0a 7d 0a 23  tThreadId();.}.#
97b70 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e  endif.../*.** In
97b80 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69  itialize and dei
97b90 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 75  nitialize the mu
97ba0 74 65 78 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a  tex subsystem..*
97bb0 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33  /.static sqlite3
97bc0 5f 6d 75 74 65 78 20 77 69 6e 4d 75 74 65 78 5f  _mutex winMutex_
97bd0 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 36 5d  staticMutexes[6]
97be0 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e  ;.static int win
97bf0 4d 75 74 65 78 5f 69 73 49 6e 69 74 20 3d 20 30  Mutex_isInit = 0
97c00 3b 0a 2f 2a 20 41 73 20 77 69 6e 4d 75 74 65 78  ;./* As winMutex
97c10 49 6e 69 74 28 29 20 61 6e 64 20 77 69 6e 4d 75  Init() and winMu
97c20 74 65 78 45 6e 64 28 29 20 61 72 65 20 63 61 6c  texEnd() are cal
97c30 6c 65 64 20 61 73 20 70 61 72 74 0a 2a 2a 20 6f  led as part.** o
97c40 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e  f the sqlite3_in
97c50 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 73 71 6c  itialize and sql
97c60 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 0a  ite3_shutdown().
97c70 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67 2c 20 74  ** processing, t
97c80 68 65 20 22 69 6e 74 65 72 6c 6f 63 6b 65 64 22  he "interlocked"
97c90 20 6d 61 67 69 63 20 69 73 20 70 72 6f 62 61 62   magic is probab
97ca0 6c 79 20 6e 6f 74 0a 2a 2a 20 73 74 72 69 63 74  ly not.** strict
97cb0 6c 79 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f  ly necessary..*/
97cc0 0a 73 74 61 74 69 63 20 6c 6f 6e 67 20 77 69 6e  .static long win
97cd0 4d 75 74 65 78 5f 6c 6f 63 6b 20 3d 20 30 3b 0a  Mutex_lock = 0;.
97ce0 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d  .static int winM
97cf0 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 7b 20  utexInit(void){ 
97d00 0a 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20  .  /* The first 
97d10 74 6f 20 69 6e 63 72 65 6d 65 6e 74 20 74 6f 20  to increment to 
97d20 31 20 64 6f 65 73 20 61 63 74 75 61 6c 20 69 6e  1 does actual in
97d30 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 2a 2f 0a  itialization */.
97d40 20 20 69 66 28 20 49 6e 74 65 72 6c 6f 63 6b 65    if( Interlocke
97d50 64 43 6f 6d 70 61 72 65 45 78 63 68 61 6e 67 65  dCompareExchange
97d60 28 26 77 69 6e 4d 75 74 65 78 5f 6c 6f 63 6b 2c  (&winMutex_lock,
97d70 20 31 2c 20 30 29 3d 3d 30 20 29 7b 0a 20 20 20   1, 0)==0 ){.   
97d80 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28   int i;.    for(
97d90 69 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65  i=0; i<ArraySize
97da0 28 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63  (winMutex_static
97db0 4d 75 74 65 78 65 73 29 3b 20 69 2b 2b 29 7b 0a  Mutexes); i++){.
97dc0 20 20 20 20 20 20 49 6e 69 74 69 61 6c 69 7a 65        Initialize
97dd0 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28  CriticalSection(
97de0 26 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63  &winMutex_static
97df0 4d 75 74 65 78 65 73 5b 69 5d 2e 6d 75 74 65 78  Mutexes[i].mutex
97e00 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 77 69 6e  );.    }.    win
97e10 4d 75 74 65 78 5f 69 73 49 6e 69 74 20 3d 20 31  Mutex_isInit = 1
97e20 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  ;.  }else{.    /
97e30 2a 20 53 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 69  * Someone else i
97e40 73 20 69 6e 20 74 68 65 20 70 72 6f 63 65 73 73  s in the process
97e50 20 6f 66 20 69 6e 69 74 69 6e 67 20 74 68 65 20   of initing the 
97e60 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 2a  static mutexes *
97e70 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 21 77 69  /.    while( !wi
97e80 6e 4d 75 74 65 78 5f 69 73 49 6e 69 74 20 29 7b  nMutex_isInit ){
97e90 0a 20 20 20 20 20 20 53 6c 65 65 70 28 31 29 3b  .      Sleep(1);
97ea0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
97eb0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a  urn SQLITE_OK; .
97ec0 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69  }..static int wi
97ed0 6e 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b  nMutexEnd(void){
97ee0 20 0a 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74   .  /* The first
97ef0 20 74 6f 20 64 65 63 72 65 6d 65 6e 74 20 74 6f   to decrement to
97f00 20 30 20 64 6f 65 73 20 61 63 74 75 61 6c 20 73   0 does actual s
97f10 68 75 74 64 6f 77 6e 20 0a 20 20 2a 2a 20 28 77  hutdown .  ** (w
97f20 68 69 63 68 20 73 68 6f 75 6c 64 20 62 65 20 74  hich should be t
97f30 68 65 20 6c 61 73 74 20 74 6f 20 73 68 75 74 64  he last to shutd
97f40 6f 77 6e 2e 29 20 2a 2f 0a 20 20 69 66 28 20 49  own.) */.  if( I
97f50 6e 74 65 72 6c 6f 63 6b 65 64 43 6f 6d 70 61 72  nterlockedCompar
97f60 65 45 78 63 68 61 6e 67 65 28 26 77 69 6e 4d 75  eExchange(&winMu
97f70 74 65 78 5f 6c 6f 63 6b 2c 20 30 2c 20 31 29 3d  tex_lock, 0, 1)=
97f80 3d 31 20 29 7b 0a 20 20 20 20 69 66 28 20 77 69  =1 ){.    if( wi
97f90 6e 4d 75 74 65 78 5f 69 73 49 6e 69 74 3d 3d 31  nMutex_isInit==1
97fa0 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b   ){.      int i;
97fb0 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  .      for(i=0; 
97fc0 69 3c 41 72 72 61 79 53 69 7a 65 28 77 69 6e 4d  i<ArraySize(winM
97fd0 75 74 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78  utex_staticMutex
97fe0 65 73 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  es); i++){.     
97ff0 20 20 20 44 65 6c 65 74 65 43 72 69 74 69 63 61     DeleteCritica
98000 6c 53 65 63 74 69 6f 6e 28 26 77 69 6e 4d 75 74  lSection(&winMut
98010 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 65 73  ex_staticMutexes
98020 5b 69 5d 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20  [i].mutex);.    
98030 20 20 7d 0a 20 20 20 20 20 20 77 69 6e 4d 75 74    }.      winMut
98040 65 78 5f 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20  ex_isInit = 0;. 
98050 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
98060 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a  n SQLITE_OK; .}.
98070 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ./*.** The sqlit
98080 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29  e3_mutex_alloc()
98090 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74   routine allocat
980a0 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65  es a new.** mute
980b0 78 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20  x and returns a 
980c0 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20  pointer to it.  
980d0 49 66 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55  If it returns NU
980e0 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73  LL.** that means
980f0 20 74 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f   that a mutex co
98100 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63  uld not be alloc
98110 61 74 65 64 2e 20 20 53 51 4c 69 74 65 0a 2a 2a  ated.  SQLite.**
98120 20 77 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73   will unwind its
98130 20 73 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72   stack and retur
98140 6e 20 61 6e 20 65 72 72 6f 72 2e 20 20 54 68 65  n an error.  The
98150 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20   argument.** to 
98160 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
98170 6c 6f 63 28 29 20 69 73 20 6f 6e 65 20 6f 66 20  loc() is one of 
98180 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f  these integer co
98190 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c  nstants:.**.** <
981a0 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c  ul>.** <li>  SQL
981b0 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a  ITE_MUTEX_FAST.*
981c0 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d  * <li>  SQLITE_M
981d0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a  UTEX_RECURSIVE.*
981e0 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d  * <li>  SQLITE_M
981f0 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
98200 45 52 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49  ER.** <li>  SQLI
98210 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
98220 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c  MEM.** <li>  SQL
98230 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
98240 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53  _MEM2.** <li>  S
98250 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
98260 49 43 5f 50 52 4e 47 0a 2a 2a 20 3c 6c 69 3e 20  IC_PRNG.** <li> 
98270 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
98280 41 54 49 43 5f 4c 52 55 0a 2a 2a 20 3c 6c 69 3e  ATIC_LRU.** <li>
98290 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53    SQLITE_MUTEX_S
982a0 54 41 54 49 43 5f 4c 52 55 32 0a 2a 2a 20 3c 2f  TATIC_LRU2.** </
982b0 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  ul>.**.** The fi
982c0 72 73 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74  rst two constant
982d0 73 20 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f  s cause sqlite3_
982e0 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f  mutex_alloc() to
982f0 20 63 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77   create.** a new
98300 20 6d 75 74 65 78 2e 20 20 54 68 65 20 6e 65 77   mutex.  The new
98310 20 6d 75 74 65 78 20 69 73 20 72 65 63 75 72 73   mutex is recurs
98320 69 76 65 20 77 68 65 6e 20 53 51 4c 49 54 45 5f  ive when SQLITE_
98330 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a  MUTEX_RECURSIVE.
98340 2a 2a 20 69 73 20 75 73 65 64 20 62 75 74 20 6e  ** is used but n
98350 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73  ot necessarily s
98360 6f 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55  o when SQLITE_MU
98370 54 45 58 5f 46 41 53 54 20 69 73 20 75 73 65 64  TEX_FAST is used
98380 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 69  ..** The mutex i
98390 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f  mplementation do
983a0 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d  es not need to m
983b0 61 6b 65 20 61 20 64 69 73 74 69 6e 63 74 69 6f  ake a distinctio
983c0 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 53 51 4c  n.** between SQL
983d0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
983e0 49 56 45 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d  IVE and SQLITE_M
983f0 55 54 45 58 5f 46 41 53 54 20 69 66 20 69 74 20  UTEX_FAST if it 
98400 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74  does.** not want
98410 20 74 6f 2e 20 20 42 75 74 20 53 51 4c 69 74 65   to.  But SQLite
98420 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65   will only reque
98430 73 74 20 61 20 72 65 63 75 72 73 69 76 65 20 6d  st a recursive m
98440 75 74 65 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73  utex in.** cases
98450 20 77 68 65 72 65 20 69 74 20 72 65 61 6c 6c 79   where it really
98460 20 6e 65 65 64 73 20 6f 6e 65 2e 20 20 49 66 20   needs one.  If 
98470 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63  a faster non-rec
98480 75 72 73 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20  ursive mutex.** 
98490 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69  implementation i
984a0 73 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74  s available on t
984b0 68 65 20 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d  he host platform
984c0 2c 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 73  , the mutex subs
984d0 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72  ystem.** might r
984e0 65 74 75 72 6e 20 73 75 63 68 20 61 20 6d 75 74  eturn such a mut
984f0 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74  ex in response t
98500 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  o SQLITE_MUTEX_F
98510 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f  AST..**.** The o
98520 74 68 65 72 20 61 6c 6c 6f 77 65 64 20 70 61 72  ther allowed par
98530 61 6d 65 74 65 72 73 20 74 6f 20 73 71 6c 69 74  ameters to sqlit
98540 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29  e3_mutex_alloc()
98550 20 65 61 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20   each return.** 
98560 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73  a pointer to a s
98570 74 61 74 69 63 20 70 72 65 65 78 69 73 74 69 6e  tatic preexistin
98580 67 20 6d 75 74 65 78 2e 20 20 53 69 78 20 73 74  g mutex.  Six st
98590 61 74 69 63 20 6d 75 74 65 78 65 73 20 61 72 65  atic mutexes are
985a0 0a 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65 20  .** used by the 
985b0 63 75 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20  current version 
985c0 6f 66 20 53 51 4c 69 74 65 2e 20 20 46 75 74 75  of SQLite.  Futu
985d0 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53  re versions of S
985e0 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64  QLite.** may add
985f0 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74   additional stat
98600 69 63 20 6d 75 74 65 78 65 73 2e 20 20 53 74 61  ic mutexes.  Sta
98610 74 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 20  tic mutexes are 
98620 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20  for internal.** 
98630 75 73 65 20 62 79 20 53 51 4c 69 74 65 20 6f 6e  use by SQLite on
98640 6c 79 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e  ly.  Application
98650 73 20 74 68 61 74 20 75 73 65 20 53 51 4c 69 74  s that use SQLit
98660 65 20 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64  e mutexes should
98670 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68 65  .** use only the
98680 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 73   dynamic mutexes
98690 20 72 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c   returned by SQL
986a0 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f  ITE_MUTEX_FAST o
986b0 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45  r.** SQLITE_MUTE
986c0 58 5f 52 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a  X_RECURSIVE..**.
986d0 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20  ** Note that if 
986e0 6f 6e 65 20 6f 66 20 74 68 65 20 64 79 6e 61 6d  one of the dynam
986f0 69 63 20 6d 75 74 65 78 20 70 61 72 61 6d 65 74  ic mutex paramet
98700 65 72 73 20 28 53 51 4c 49 54 45 5f 4d 55 54 45  ers (SQLITE_MUTE
98710 58 5f 46 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c  X_FAST.** or SQL
98720 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
98730 49 56 45 29 20 69 73 20 75 73 65 64 20 74 68 65  IVE) is used the
98740 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  n sqlite3_mutex_
98750 61 6c 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72  alloc().** retur
98760 6e 73 20 61 20 64 69 66 66 65 72 65 6e 74 20 6d  ns a different m
98770 75 74 65 78 20 6f 6e 20 65 76 65 72 79 20 63 61  utex on every ca
98780 6c 6c 2e 20 20 42 75 74 20 66 6f 72 20 74 68 65  ll.  But for the
98790 20 73 74 61 74 69 63 20 0a 2a 2a 20 6d 75 74 65   static .** mute
987a0 78 20 74 79 70 65 73 2c 20 74 68 65 20 73 61 6d  x types, the sam
987b0 65 20 6d 75 74 65 78 20 69 73 20 72 65 74 75 72  e mutex is retur
987c0 6e 65 64 20 6f 6e 20 65 76 65 72 79 20 63 61 6c  ned on every cal
987d0 6c 20 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 68  l that has.** th
987e0 65 20 73 61 6d 65 20 74 79 70 65 20 6e 75 6d 62  e same type numb
987f0 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71  er..*/.static sq
98800 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 77 69 6e  lite3_mutex *win
98810 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69  MutexAlloc(int i
98820 54 79 70 65 29 7b 0a 20 20 73 71 6c 69 74 65 33  Type){.  sqlite3
98830 5f 6d 75 74 65 78 20 2a 70 3b 0a 0a 20 20 73 77  _mutex *p;..  sw
98840 69 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a 20  itch( iType ){. 
98850 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d     case SQLITE_M
98860 55 54 45 58 5f 46 41 53 54 3a 0a 20 20 20 20 63  UTEX_FAST:.    c
98870 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ase SQLITE_MUTEX
98880 5f 52 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20  _RECURSIVE: {.  
98890 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d      p = sqlite3M
988a0 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f  allocZero( sizeo
988b0 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69  f(*p) );.      i
988c0 66 28 20 70 20 29 7b 20 20 0a 20 20 20 20 20 20  f( p ){  .      
988d0 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b    p->id = iType;
988e0 0a 20 20 20 20 20 20 20 20 49 6e 69 74 69 61 6c  .        Initial
988f0 69 7a 65 43 72 69 74 69 63 61 6c 53 65 63 74 69  izeCriticalSecti
98900 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20  on(&p->mutex);. 
98910 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
98920 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65  ak;.    }.    de
98930 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61  fault: {.      a
98940 73 73 65 72 74 28 20 77 69 6e 4d 75 74 65 78 5f  ssert( winMutex_
98950 69 73 49 6e 69 74 3d 3d 31 20 29 3b 0a 20 20 20  isInit==1 );.   
98960 20 20 20 61 73 73 65 72 74 28 20 69 54 79 70 65     assert( iType
98970 2d 32 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20 20  -2 >= 0 );.     
98980 20 61 73 73 65 72 74 28 20 69 54 79 70 65 2d 32   assert( iType-2
98990 20 3c 20 41 72 72 61 79 53 69 7a 65 28 77 69 6e   < ArraySize(win
989a0 4d 75 74 65 78 5f 73 74 61 74 69 63 4d 75 74 65  Mutex_staticMute
989b0 78 65 73 29 20 29 3b 0a 20 20 20 20 20 20 70 20  xes) );.      p 
989c0 3d 20 26 77 69 6e 4d 75 74 65 78 5f 73 74 61 74  = &winMutex_stat
989d0 69 63 4d 75 74 65 78 65 73 5b 69 54 79 70 65 2d  icMutexes[iType-
989e0 32 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 20  2];.      p->id 
989f0 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 62  = iType;.      b
98a00 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
98a10 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a    return p;.}...
98a20 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
98a30 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61  ne deallocates a
98a40 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 61   previously.** a
98a50 6c 6c 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 20  llocated mutex. 
98a60 20 53 51 4c 69 74 65 20 69 73 20 63 61 72 65 66   SQLite is caref
98a70 75 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65  ul to deallocate
98a80 20 65 76 65 72 79 0a 2a 2a 20 6d 75 74 65 78 20   every.** mutex 
98a90 74 68 61 74 20 69 74 20 61 6c 6c 6f 63 61 74 65  that it allocate
98aa0 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  s..*/.static voi
98ab0 64 20 77 69 6e 4d 75 74 65 78 46 72 65 65 28 73  d winMutexFree(s
98ac0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29  qlite3_mutex *p)
98ad0 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 29 3b  {.  assert( p );
98ae0 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52  .  assert( p->nR
98af0 65 66 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ef==0 );.  asser
98b00 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45  t( p->id==SQLITE
98b10 5f 4d 55 54 45 58 5f 46 41 53 54 20 7c 7c 20 70  _MUTEX_FAST || p
98b20 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54  ->id==SQLITE_MUT
98b30 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a  EX_RECURSIVE );.
98b40 20 20 44 65 6c 65 74 65 43 72 69 74 69 63 61 6c    DeleteCritical
98b50 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65  Section(&p->mute
98b60 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  x);.  sqlite3_fr
98b70 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ee(p);.}../*.** 
98b80 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
98b90 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71  x_enter() and sq
98ba0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
98bb0 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d  ) routines attem
98bc0 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61  pt.** to enter a
98bd0 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f 74   mutex.  If anot
98be0 68 65 72 20 74 68 72 65 61 64 20 69 73 20 61 6c  her thread is al
98bf0 72 65 61 64 79 20 77 69 74 68 69 6e 20 74 68 65  ready within the
98c00 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74   mutex,.** sqlit
98c10 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29  e3_mutex_enter()
98c20 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20   will block and 
98c30 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
98c40 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a  y() will return.
98c50 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20  ** SQLITE_BUSY. 
98c60 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
98c70 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61  ex_try() interfa
98c80 63 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  ce returns SQLIT
98c90 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63  E_OK.** upon suc
98ca0 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20  cessful entry.  
98cb0 4d 75 74 65 78 65 73 20 63 72 65 61 74 65 64 20  Mutexes created 
98cc0 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54  using SQLITE_MUT
98cd0 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e  EX_RECURSIVE can
98ce0 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d  .** be entered m
98cf0 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79  ultiple times by
98d00 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64   the same thread
98d10 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73 65 73  .  In such cases
98d20 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d   the,.** mutex m
98d30 75 73 74 20 62 65 20 65 78 69 74 65 64 20 61 6e  ust be exited an
98d40 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66   equal number of
98d50 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e   times before an
98d60 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20  other thread.** 
98d70 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74  can enter.  If t
98d80 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 74  he same thread t
98d90 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e  ries to enter an
98da0 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20  y other kind of 
98db0 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68  mutex.** more th
98dc0 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68  an once, the beh
98dd0 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e  avior is undefin
98de0 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ed..*/.static vo
98df0 69 64 20 77 69 6e 4d 75 74 65 78 45 6e 74 65 72  id winMutexEnter
98e00 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
98e10 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  p){.  assert( p-
98e20 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45  >id==SQLITE_MUTE
98e30 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 77  X_RECURSIVE || w
98e40 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70  inMutexNotheld(p
98e50 29 20 29 3b 0a 20 20 45 6e 74 65 72 43 72 69 74  ) );.  EnterCrit
98e60 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e  icalSection(&p->
98e70 6d 75 74 65 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e  mutex);.  p->own
98e80 65 72 20 3d 20 47 65 74 43 75 72 72 65 6e 74 54  er = GetCurrentT
98e90 68 72 65 61 64 49 64 28 29 3b 20 0a 20 20 70 2d  hreadId(); .  p-
98ea0 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a 73 74 61 74 69  >nRef++;.}.stati
98eb0 63 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 54 72  c int winMutexTr
98ec0 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  y(sqlite3_mutex 
98ed0 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  *p){.  int rc = 
98ee0 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 61  SQLITE_BUSY;.  a
98ef0 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51  ssert( p->id==SQ
98f00 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
98f10 53 49 56 45 20 7c 7c 20 77 69 6e 4d 75 74 65 78  SIVE || winMutex
98f20 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20  Notheld(p) );.  
98f30 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 73 71 6c 69  /*.  ** The sqli
98f40 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20  te3_mutex_try() 
98f50 72 6f 75 74 69 6e 65 20 69 73 20 76 65 72 79 20  routine is very 
98f60 72 61 72 65 6c 79 20 75 73 65 64 2c 20 61 6e 64  rarely used, and
98f70 20 77 68 65 6e 20 69 74 0a 20 20 2a 2a 20 69 73   when it.  ** is
98f80 20 75 73 65 64 20 69 74 20 69 73 20 6d 65 72 65   used it is mere
98f90 6c 79 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69  ly an optimizati
98fa0 6f 6e 2e 20 20 53 6f 20 69 74 20 69 73 20 4f 4b  on.  So it is OK
98fb0 20 66 6f 72 20 69 74 20 74 6f 20 61 6c 77 61 79   for it to alway
98fc0 73 0a 20 20 2a 2a 20 66 61 69 6c 2e 20 20 0a 20  s.  ** fail.  . 
98fd0 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 54 72 79   **.  ** The Try
98fe0 45 6e 74 65 72 43 72 69 74 69 63 61 6c 53 65 63  EnterCriticalSec
98ff0 74 69 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65  tion() interface
99000 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62   is only availab
99010 6c 65 20 6f 6e 20 57 69 6e 4e 54 2e 0a 20 20 2a  le on WinNT..  *
99020 2a 20 41 6e 64 20 73 6f 6d 65 20 77 69 6e 64 6f  * And some windo
99030 77 73 20 63 6f 6d 70 69 6c 65 72 73 20 63 6f 6d  ws compilers com
99040 70 6c 61 69 6e 20 69 66 20 79 6f 75 20 74 72 79  plain if you try
99050 20 74 6f 20 75 73 65 20 69 74 20 77 69 74 68 6f   to use it witho
99060 75 74 0a 20 20 2a 2a 20 66 69 72 73 74 20 64 6f  ut.  ** first do
99070 69 6e 67 20 73 6f 6d 65 20 23 64 65 66 69 6e 65  ing some #define
99080 73 20 74 68 61 74 20 70 72 65 76 65 6e 74 20 53  s that prevent S
99090 51 4c 69 74 65 20 66 72 6f 6d 20 62 75 69 6c 64  QLite from build
990a0 69 6e 67 20 6f 6e 20 57 69 6e 39 38 2e 0a 20 20  ing on Win98..  
990b0 2a 2a 20 46 6f 72 20 74 68 61 74 20 72 65 61 73  ** For that reas
990c0 6f 6e 2c 20 77 65 20 77 69 6c 6c 20 6f 6d 69 74  on, we will omit
990d0 20 74 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69   this optimizati
990e0 6f 6e 20 66 6f 72 20 6e 6f 77 2e 20 20 53 65 65  on for now.  See
990f0 0a 20 20 2a 2a 20 74 69 63 6b 65 74 20 23 32 36  .  ** ticket #26
99100 38 35 2e 0a 20 20 2a 2f 0a 23 69 66 20 30 0a 20  85..  */.#if 0. 
99110 20 69 66 28 20 6d 75 74 65 78 49 73 4e 54 28 29   if( mutexIsNT()
99120 20 26 26 20 54 72 79 45 6e 74 65 72 43 72 69 74   && TryEnterCrit
99130 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e  icalSection(&p->
99140 6d 75 74 65 78 29 20 29 7b 0a 20 20 20 20 70 2d  mutex) ){.    p-
99150 3e 6f 77 6e 65 72 20 3d 20 47 65 74 43 75 72 72  >owner = GetCurr
99160 65 6e 74 54 68 72 65 61 64 49 64 28 29 3b 0a 20  entThreadId();. 
99170 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20     p->nRef++;.  
99180 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
99190 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 55 4e  ;.  }.#else.  UN
991a0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70  USED_PARAMETER(p
991b0 29 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75  );.#endif.  retu
991c0 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
991d0 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
991e0 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69 6e  x_leave() routin
991f0 65 20 65 78 69 74 73 20 61 20 6d 75 74 65 78 20  e exits a mutex 
99200 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65 76  that was.** prev
99210 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20 62  iously entered b
99220 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61  y the same threa
99230 64 2e 20 20 54 68 65 20 62 65 68 61 76 69 6f 72  d.  The behavior
99240 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64  .** is undefined
99250 20 69 66 20 74 68 65 20 6d 75 74 65 78 20 69 73   if the mutex is
99260 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65   not currently e
99270 6e 74 65 72 65 64 20 6f 72 0a 2a 2a 20 69 73 20  ntered or.** is 
99280 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61 6c  not currently al
99290 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74 65  located.  SQLite
992a0 20 77 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20 65   will never do e
992b0 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  ither..*/.static
992c0 20 76 6f 69 64 20 77 69 6e 4d 75 74 65 78 4c 65   void winMutexLe
992d0 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  ave(sqlite3_mute
992e0 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28  x *p){.  assert(
992f0 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20   p->nRef>0 );.  
99300 61 73 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65 72  assert( p->owner
99310 3d 3d 47 65 74 43 75 72 72 65 6e 74 54 68 72 65  ==GetCurrentThre
99320 61 64 49 64 28 29 20 29 3b 0a 20 20 70 2d 3e 6e  adId() );.  p->n
99330 52 65 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28  Ref--;.  assert(
99340 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70   p->nRef==0 || p
99350 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54  ->id==SQLITE_MUT
99360 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a  EX_RECURSIVE );.
99370 20 20 4c 65 61 76 65 43 72 69 74 69 63 61 6c 53    LeaveCriticalS
99380 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78  ection(&p->mutex
99390 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49  );.}..SQLITE_PRI
993a0 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74  VATE sqlite3_mut
993b0 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69  ex_methods *sqli
993c0 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28  te3DefaultMutex(
993d0 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20  void){.  static 
993e0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
993f0 74 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 7b  thods sMutex = {
99400 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 49 6e 69  .    winMutexIni
99410 74 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 45  t,.    winMutexE
99420 6e 64 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78  nd,.    winMutex
99430 41 6c 6c 6f 63 2c 0a 20 20 20 20 77 69 6e 4d 75  Alloc,.    winMu
99440 74 65 78 46 72 65 65 2c 0a 20 20 20 20 77 69 6e  texFree,.    win
99450 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 20  MutexEnter,.    
99460 77 69 6e 4d 75 74 65 78 54 72 79 2c 0a 20 20 20  winMutexTry,.   
99470 20 77 69 6e 4d 75 74 65 78 4c 65 61 76 65 2c 0a   winMutexLeave,.
99480 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
99490 42 55 47 0a 20 20 20 20 77 69 6e 4d 75 74 65 78  BUG.    winMutex
994a0 48 65 6c 64 2c 0a 20 20 20 20 77 69 6e 4d 75 74  Held,.    winMut
994b0 65 78 4e 6f 74 68 65 6c 64 0a 23 65 6c 73 65 0a  exNotheld.#else.
994c0 20 20 20 20 30 2c 0a 20 20 20 20 30 0a 23 65 6e      0,.    0.#en
994d0 64 69 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75  dif.  };..  retu
994e0 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65  rn &sMutex;.}.#e
994f0 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d  ndif /* SQLITE_M
99500 55 54 45 58 5f 57 33 32 20 2a 2f 0a 0a 2f 2a 2a  UTEX_W32 */../**
99510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
99520 20 6f 66 20 6d 75 74 65 78 5f 77 33 32 2e 63 20   of mutex_w32.c 
99530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
99540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
99550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
99560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
99570 69 6e 20 66 69 6c 65 20 6d 61 6c 6c 6f 63 2e 63  in file malloc.c
99580 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
99590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
995a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
995b0 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
995c0 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 15.**.** The a
995d0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
995e0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
995f0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
99600 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
99610 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
99620 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
99630 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
99640 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
99650 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
99660 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
99670 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
99680 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
99690 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
996a0 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
996b0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
996c0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
996d0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
996e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
996f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
99700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
99710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
99720 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4d 65 6d 6f 72 79  ***.**.** Memory
99730 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63   allocation func
99740 74 69 6f 6e 73 20 75 73 65 64 20 74 68 72 6f 75  tions used throu
99750 67 68 6f 75 74 20 73 71 6c 69 74 65 2e 0a 2a 2a  ghout sqlite..**
99760 0a 2a 2a 20 24 49 64 3a 20 6d 61 6c 6c 6f 63 2e  .** $Id: malloc.
99770 63 2c 76 20 31 2e 36 36 20 32 30 30 39 2f 30 37  c,v 1.66 2009/07
99780 2f 31 37 20 31 31 3a 34 34 3a 30 37 20 64 72 68  /17 11:44:07 drh
99790 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a   Exp $.*/../*.**
997a0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 75   This routine ru
997b0 6e 73 20 77 68 65 6e 20 74 68 65 20 6d 65 6d 6f  ns when the memo
997c0 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 73 65 65  ry allocator see
997d0 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 74 6f  s that the.** to
997e0 74 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  tal memory alloc
997f0 61 74 69 6f 6e 20 69 73 20 61 62 6f 75 74 20 74  ation is about t
99800 6f 20 65 78 63 65 65 64 20 74 68 65 20 73 6f 66  o exceed the sof
99810 74 20 68 65 61 70 0a 2a 2a 20 6c 69 6d 69 74 2e  t heap.** limit.
99820 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
99830 73 6f 66 74 48 65 61 70 4c 69 6d 69 74 45 6e 66  softHeapLimitEnf
99840 6f 72 63 65 72 28 0a 20 20 76 6f 69 64 20 2a 4e  orcer(.  void *N
99850 6f 74 55 73 65 64 2c 20 0a 20 20 73 71 6c 69 74  otUsed, .  sqlit
99860 65 33 5f 69 6e 74 36 34 20 4e 6f 74 55 73 65 64  e3_int64 NotUsed
99870 32 2c 0a 20 20 69 6e 74 20 61 6c 6c 6f 63 53 69  2,.  int allocSi
99880 7a 65 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50  ze.){.  UNUSED_P
99890 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65  ARAMETER2(NotUse
998a0 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20  d, NotUsed2);.  
998b0 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f  sqlite3_release_
998c0 6d 65 6d 6f 72 79 28 61 6c 6c 6f 63 53 69 7a 65  memory(allocSize
998d0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20  );.}../*.** Set 
998e0 74 68 65 20 73 6f 66 74 20 68 65 61 70 2d 73 69  the soft heap-si
998f0 7a 65 20 6c 69 6d 69 74 20 66 6f 72 20 74 68 65  ze limit for the
99900 20 6c 69 62 72 61 72 79 2e 20 50 61 73 73 69 6e   library. Passin
99910 67 20 61 20 7a 65 72 6f 20 6f 72 20 0a 2a 2a 20  g a zero or .** 
99920 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65 20 69  negative value i
99930 6e 64 69 63 61 74 65 73 20 6e 6f 20 6c 69 6d 69  ndicates no limi
99940 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  t..*/.SQLITE_API
99950 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 73 6f   void sqlite3_so
99960 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 69 6e  ft_heap_limit(in
99970 74 20 6e 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  t n){.  sqlite3_
99980 75 69 6e 74 36 34 20 69 4c 69 6d 69 74 3b 0a 20  uint64 iLimit;. 
99990 20 69 6e 74 20 6f 76 65 72 61 67 65 3b 0a 20 20   int overage;.  
999a0 69 66 28 20 6e 3c 30 20 29 7b 0a 20 20 20 20 69  if( n<0 ){.    i
999b0 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 7d 65 6c  Limit = 0;.  }el
999c0 73 65 7b 0a 20 20 20 20 69 4c 69 6d 69 74 20 3d  se{.    iLimit =
999d0 20 6e 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20   n;.  }.#ifndef 
999e0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
999f0 49 4e 49 54 0a 20 20 73 71 6c 69 74 65 33 5f 69  INIT.  sqlite3_i
99a00 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 23 65 6e  nitialize();.#en
99a10 64 69 66 0a 20 20 69 66 28 20 69 4c 69 6d 69 74  dif.  if( iLimit
99a20 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  >0 ){.    sqlite
99a30 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 73 6f 66  3MemoryAlarm(sof
99a40 74 48 65 61 70 4c 69 6d 69 74 45 6e 66 6f 72 63  tHeapLimitEnforc
99a50 65 72 2c 20 30 2c 20 69 4c 69 6d 69 74 29 3b 0a  er, 0, iLimit);.
99a60 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
99a70 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28  ite3MemoryAlarm(
99a80 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  0, 0, 0);.  }.  
99a90 6f 76 65 72 61 67 65 20 3d 20 28 69 6e 74 29 28  overage = (int)(
99aa0 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75  sqlite3_memory_u
99ab0 73 65 64 28 29 20 2d 20 28 69 36 34 29 6e 29 3b  sed() - (i64)n);
99ac0 0a 20 20 69 66 28 20 6f 76 65 72 61 67 65 3e 30  .  if( overage>0
99ad0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
99ae0 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 6f  release_memory(o
99af0 76 65 72 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a  verage);.  }.}..
99b00 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f  /*.** Attempt to
99b10 20 72 65 6c 65 61 73 65 20 75 70 20 74 6f 20 6e   release up to n
99b20 20 62 79 74 65 73 20 6f 66 20 6e 6f 6e 2d 65 73   bytes of non-es
99b30 73 65 6e 74 69 61 6c 20 6d 65 6d 6f 72 79 20 63  sential memory c
99b40 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 68 65 6c 64  urrently.** held
99b50 20 62 79 20 53 51 4c 69 74 65 2e 20 41 6e 20 65   by SQLite. An e
99b60 78 61 6d 70 6c 65 20 6f 66 20 6e 6f 6e 2d 65 73  xample of non-es
99b70 73 65 6e 74 69 61 6c 20 6d 65 6d 6f 72 79 20 69  sential memory i
99b80 73 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 74 6f  s memory used to
99b90 0a 2a 2a 20 63 61 63 68 65 20 64 61 74 61 62 61  .** cache databa
99ba0 73 65 20 70 61 67 65 73 20 74 68 61 74 20 61 72  se pages that ar
99bb0 65 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20  e not currently 
99bc0 69 6e 20 75 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54  in use..*/.SQLIT
99bd0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
99be0 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79  3_release_memory
99bf0 28 69 6e 74 20 6e 29 7b 0a 23 69 66 64 65 66 20  (int n){.#ifdef 
99c00 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45  SQLITE_ENABLE_ME
99c10 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a  MORY_MANAGEMENT.
99c20 20 20 69 6e 74 20 6e 52 65 74 20 3d 20 30 3b 0a    int nRet = 0;.
99c30 23 69 66 20 30 0a 20 20 6e 52 65 74 20 2b 3d 20  #if 0.  nRet += 
99c40 73 71 6c 69 74 65 33 56 64 62 65 52 65 6c 65 61  sqlite3VdbeRelea
99c50 73 65 4d 65 6d 6f 72 79 28 6e 29 3b 0a 23 65 6e  seMemory(n);.#en
99c60 64 69 66 0a 20 20 6e 52 65 74 20 2b 3d 20 73 71  dif.  nRet += sq
99c70 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61  lite3PcacheRelea
99c80 73 65 4d 65 6d 6f 72 79 28 6e 2d 6e 52 65 74 29  seMemory(n-nRet)
99c90 3b 0a 20 20 72 65 74 75 72 6e 20 6e 52 65 74 3b  ;.  return nRet;
99ca0 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f  .#else.  UNUSED_
99cb0 50 41 52 41 4d 45 54 45 52 28 6e 29 3b 0a 20 20  PARAMETER(n);.  
99cc0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
99cd0 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a  ;.#endif.}../*.*
99ce0 2a 20 53 74 61 74 65 20 69 6e 66 6f 72 6d 61 74  * State informat
99cf0 69 6f 6e 20 6c 6f 63 61 6c 20 74 6f 20 74 68 65  ion local to the
99d00 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
99d10 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f  on subsystem..*/
99d20 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57  .static SQLITE_W
99d30 53 44 20 73 74 72 75 63 74 20 4d 65 6d 30 47 6c  SD struct Mem0Gl
99d40 6f 62 61 6c 20 7b 0a 20 20 2f 2a 20 4e 75 6d 62  obal {.  /* Numb
99d50 65 72 20 6f 66 20 66 72 65 65 20 70 61 67 65 73  er of free pages
99d60 20 66 6f 72 20 73 63 72 61 74 63 68 20 61 6e 64   for scratch and
99d70 20 70 61 67 65 2d 63 61 63 68 65 20 6d 65 6d 6f   page-cache memo
99d80 72 79 20 2a 2f 0a 20 20 75 33 32 20 6e 53 63 72  ry */.  u32 nScr
99d90 61 74 63 68 46 72 65 65 3b 0a 20 20 75 33 32 20  atchFree;.  u32 
99da0 6e 50 61 67 65 46 72 65 65 3b 0a 0a 20 20 73 71  nPageFree;..  sq
99db0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74  lite3_mutex *mut
99dc0 65 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  ex;         /* M
99dd0 75 74 65 78 20 74 6f 20 73 65 72 69 61 6c 69 7a  utex to serializ
99de0 65 20 61 63 63 65 73 73 20 2a 2f 0a 0a 20 20 2f  e access */..  /
99df0 2a 0a 20 20 2a 2a 20 54 68 65 20 61 6c 61 72 6d  *.  ** The alarm
99e00 20 63 61 6c 6c 62 61 63 6b 20 61 6e 64 20 69 74   callback and it
99e10 73 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 54 68  s arguments.  Th
99e20 65 20 6d 65 6d 30 2e 6d 75 74 65 78 20 6c 6f 63  e mem0.mutex loc
99e30 6b 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 68  k will.  ** be h
99e40 65 6c 64 20 77 68 69 6c 65 20 74 68 65 20 63 61  eld while the ca
99e50 6c 6c 62 61 63 6b 20 69 73 20 72 75 6e 6e 69 6e  llback is runnin
99e60 67 2e 20 20 52 65 63 75 72 73 69 76 65 20 63 61  g.  Recursive ca
99e70 6c 6c 73 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68  lls into.  ** th
99e80 65 20 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 74  e memory subsyst
99e90 65 6d 20 61 72 65 20 61 6c 6c 6f 77 65 64 2c 20  em are allowed, 
99ea0 62 75 74 20 6e 6f 20 6e 65 77 20 63 61 6c 6c 62  but no new callb
99eb0 61 63 6b 73 20 77 69 6c 6c 20 62 65 0a 20 20 2a  acks will be.  *
99ec0 2a 20 69 73 73 75 65 64 2e 0a 20 20 2a 2f 0a 20  * issued..  */. 
99ed0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 61   sqlite3_int64 a
99ee0 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 3b 0a 20  larmThreshold;. 
99ef0 20 76 6f 69 64 20 28 2a 61 6c 61 72 6d 43 61 6c   void (*alarmCal
99f00 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 20 73 71  lback)(void*, sq
99f10 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74 29  lite3_int64,int)
99f20 3b 0a 20 20 76 6f 69 64 20 2a 61 6c 61 72 6d 41  ;.  void *alarmA
99f30 72 67 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 50  rg;..  /*.  ** P
99f40 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 65  ointers to the e
99f50 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 47 6c 6f  nd of sqlite3Glo
99f60 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74  balConfig.pScrat
99f70 63 68 20 61 6e 64 0a 20 20 2a 2a 20 73 71 6c 69  ch and.  ** sqli
99f80 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
99f90 70 50 61 67 65 20 74 6f 20 61 20 62 6c 6f 63 6b  pPage to a block
99fa0 20 6f 66 20 6d 65 6d 6f 72 79 20 74 68 61 74 20   of memory that 
99fb0 72 65 63 6f 72 64 73 0a 20 20 2a 2a 20 77 68 69  records.  ** whi
99fc0 63 68 20 70 61 67 65 73 20 61 72 65 20 61 76 61  ch pages are ava
99fd0 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 75  ilable..  */.  u
99fe0 33 32 20 2a 61 53 63 72 61 74 63 68 46 72 65 65  32 *aScratchFree
99ff0 3b 0a 20 20 75 33 32 20 2a 61 50 61 67 65 46 72  ;.  u32 *aPageFr
9a000 65 65 3b 0a 7d 20 6d 65 6d 30 20 3d 20 7b 20 30  ee;.} mem0 = { 0
9a010 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
9a020 20 30 2c 20 30 20 7d 3b 0a 0a 23 64 65 66 69 6e   0, 0 };..#defin
9a030 65 20 6d 65 6d 30 20 47 4c 4f 42 41 4c 28 73 74  e mem0 GLOBAL(st
9a040 72 75 63 74 20 4d 65 6d 30 47 6c 6f 62 61 6c 2c  ruct Mem0Global,
9a050 20 6d 65 6d 30 29 0a 0a 2f 2a 0a 2a 2a 20 49 6e   mem0)../*.** In
9a060 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d  itialize the mem
9a070 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73  ory allocation s
9a080 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c  ubsystem..*/.SQL
9a090 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
9a0a0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 49 6e 69  sqlite3MallocIni
9a0b0 74 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 73  t(void){.  if( s
9a0c0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9a0d0 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 3d 3d 30 20  ig.m.xMalloc==0 
9a0e0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 65  ){.    sqlite3Me
9a0f0 6d 53 65 74 44 65 66 61 75 6c 74 28 29 3b 0a 20  mSetDefault();. 
9a100 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26 6d 65 6d   }.  memset(&mem
9a110 30 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d 65 6d  0, 0, sizeof(mem
9a120 30 29 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  0));.  if( sqlit
9a130 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62  e3GlobalConfig.b
9a140 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 20  CoreMutex ){.   
9a150 20 6d 65 6d 30 2e 6d 75 74 65 78 20 3d 20 73 71   mem0.mutex = sq
9a160 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
9a170 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
9a180 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20  TIC_MEM);.  }.  
9a190 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
9a1a0 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68  lConfig.pScratch
9a1b0 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61   && sqlite3Globa
9a1c0 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63  lConfig.szScratc
9a1d0 68 3e 3d 31 30 30 0a 20 20 20 20 20 20 26 26 20  h>=100.      && 
9a1e0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9a1f0 66 69 67 2e 6e 53 63 72 61 74 63 68 3e 3d 30 20  fig.nScratch>=0 
9a200 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  ){.    int i;.  
9a210 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
9a220 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68 20  onfig.szScratch 
9a230 3d 20 52 4f 55 4e 44 44 4f 57 4e 38 28 73 71 6c  = ROUNDDOWN8(sql
9a240 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9a250 2e 73 7a 53 63 72 61 74 63 68 2d 34 29 3b 0a 20  .szScratch-4);. 
9a260 20 20 20 6d 65 6d 30 2e 61 53 63 72 61 74 63 68     mem0.aScratch
9a270 46 72 65 65 20 3d 20 28 75 33 32 2a 29 26 28 28  Free = (u32*)&((
9a280 63 68 61 72 2a 29 73 71 6c 69 74 65 33 47 6c 6f  char*)sqlite3Glo
9a290 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74  balConfig.pScrat
9a2a0 63 68 29 0a 20 20 20 20 20 20 20 20 20 20 20 20  ch).            
9a2b0 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 47 6c        [sqlite3Gl
9a2c0 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72  obalConfig.szScr
9a2d0 61 74 63 68 2a 73 71 6c 69 74 65 33 47 6c 6f 62  atch*sqlite3Glob
9a2e0 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63  alConfig.nScratc
9a2f0 68 5d 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  h];.    for(i=0;
9a300 20 69 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c   i<sqlite3Global
9a310 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 3b  Config.nScratch;
9a320 20 69 2b 2b 29 7b 20 6d 65 6d 30 2e 61 53 63 72   i++){ mem0.aScr
9a330 61 74 63 68 46 72 65 65 5b 69 5d 20 3d 20 69 3b  atchFree[i] = i;
9a340 20 7d 0a 20 20 20 20 6d 65 6d 30 2e 6e 53 63 72   }.    mem0.nScr
9a350 61 74 63 68 46 72 65 65 20 3d 20 73 71 6c 69 74  atchFree = sqlit
9a360 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e  e3GlobalConfig.n
9a370 53 63 72 61 74 63 68 3b 0a 20 20 7d 65 6c 73 65  Scratch;.  }else
9a380 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f  {.    sqlite3Glo
9a390 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74  balConfig.pScrat
9a3a0 63 68 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69  ch = 0;.    sqli
9a3b0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9a3c0 73 7a 53 63 72 61 74 63 68 20 3d 20 30 3b 0a 20  szScratch = 0;. 
9a3d0 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33   }.  if( sqlite3
9a3e0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61  GlobalConfig.pPa
9a3f0 67 65 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f  ge && sqlite3Glo
9a400 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65  balConfig.szPage
9a410 3e 3d 35 31 32 0a 20 20 20 20 20 20 26 26 20 73  >=512.      && s
9a420 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9a430 69 67 2e 6e 50 61 67 65 3e 3d 31 20 29 7b 0a 20  ig.nPage>=1 ){. 
9a440 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 69 6e     int i;.    in
9a450 74 20 6f 76 65 72 68 65 61 64 3b 0a 20 20 20 20  t overhead;.    
9a460 69 6e 74 20 73 7a 20 3d 20 52 4f 55 4e 44 44 4f  int sz = ROUNDDO
9a470 57 4e 38 28 73 71 6c 69 74 65 33 47 6c 6f 62 61  WN8(sqlite3Globa
9a480 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 29 3b  lConfig.szPage);
9a490 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c  .    int n = sql
9a4a0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9a4b0 2e 6e 50 61 67 65 3b 0a 20 20 20 20 6f 76 65 72  .nPage;.    over
9a4c0 68 65 61 64 20 3d 20 28 34 2a 6e 20 2b 20 73 7a  head = (4*n + sz
9a4d0 20 2d 20 31 29 2f 73 7a 3b 0a 20 20 20 20 73 71   - 1)/sz;.    sq
9a4e0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9a4f0 67 2e 6e 50 61 67 65 20 2d 3d 20 6f 76 65 72 68  g.nPage -= overh
9a500 65 61 64 3b 0a 20 20 20 20 6d 65 6d 30 2e 61 50  ead;.    mem0.aP
9a510 61 67 65 46 72 65 65 20 3d 20 28 75 33 32 2a 29  ageFree = (u32*)
9a520 26 28 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33  &((char*)sqlite3
9a530 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61  GlobalConfig.pPa
9a540 67 65 29 0a 20 20 20 20 20 20 20 20 20 20 20 20  ge).            
9a550 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 47 6c        [sqlite3Gl
9a560 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67  obalConfig.szPag
9a570 65 2a 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  e*sqlite3GlobalC
9a580 6f 6e 66 69 67 2e 6e 50 61 67 65 5d 3b 0a 20 20  onfig.nPage];.  
9a590 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 71 6c    for(i=0; i<sql
9a5a0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9a5b0 2e 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 20 6d 65  .nPage; i++){ me
9a5c0 6d 30 2e 61 50 61 67 65 46 72 65 65 5b 69 5d 20  m0.aPageFree[i] 
9a5d0 3d 20 69 3b 20 7d 0a 20 20 20 20 6d 65 6d 30 2e  = i; }.    mem0.
9a5e0 6e 50 61 67 65 46 72 65 65 20 3d 20 73 71 6c 69  nPageFree = sqli
9a5f0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9a600 6e 50 61 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a  nPage;.  }else{.
9a610 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
9a620 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 3d 20  lConfig.pPage = 
9a630 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c  0;.    sqlite3Gl
9a640 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67  obalConfig.szPag
9a650 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74  e = 0;.  }.  ret
9a660 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  urn sqlite3Globa
9a670 6c 43 6f 6e 66 69 67 2e 6d 2e 78 49 6e 69 74 28  lConfig.m.xInit(
9a680 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9a690 66 69 67 2e 6d 2e 70 41 70 70 44 61 74 61 29 3b  fig.m.pAppData);
9a6a0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74  .}../*.** Deinit
9a6b0 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72  ialize the memor
9a6c0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62  y allocation sub
9a6d0 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54  system..*/.SQLIT
9a6e0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
9a6f0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 45 6e 64 28  qlite3MallocEnd(
9a700 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 73 71 6c  void){.  if( sql
9a710 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9a720 2e 6d 2e 78 53 68 75 74 64 6f 77 6e 20 29 7b 0a  .m.xShutdown ){.
9a730 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
9a740 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 68 75 74 64  lConfig.m.xShutd
9a750 6f 77 6e 28 73 71 6c 69 74 65 33 47 6c 6f 62 61  own(sqlite3Globa
9a760 6c 43 6f 6e 66 69 67 2e 6d 2e 70 41 70 70 44 61  lConfig.m.pAppDa
9a770 74 61 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65  ta);.  }.  memse
9a780 74 28 26 6d 65 6d 30 2c 20 30 2c 20 73 69 7a 65  t(&mem0, 0, size
9a790 6f 66 28 6d 65 6d 30 29 29 3b 0a 7d 0a 0a 2f 2a  of(mem0));.}../*
9a7a0 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 61  .** Return the a
9a7b0 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20  mount of memory 
9a7c0 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 65  currently checke
9a7d0 64 20 6f 75 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  d out..*/.SQLITE
9a7e0 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74  _API sqlite3_int
9a7f0 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72  64 sqlite3_memor
9a800 79 5f 75 73 65 64 28 76 6f 69 64 29 7b 0a 20 20  y_used(void){.  
9a810 69 6e 74 20 6e 2c 20 6d 78 3b 0a 20 20 73 71 6c  int n, mx;.  sql
9a820 69 74 65 33 5f 69 6e 74 36 34 20 72 65 73 3b 0a  ite3_int64 res;.
9a830 20 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73    sqlite3_status
9a840 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d  (SQLITE_STATUS_M
9a850 45 4d 4f 52 59 5f 55 53 45 44 2c 20 26 6e 2c 20  EMORY_USED, &n, 
9a860 26 6d 78 2c 20 30 29 3b 0a 20 20 72 65 73 20 3d  &mx, 0);.  res =
9a870 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29   (sqlite3_int64)
9a880 6e 3b 20 20 2f 2a 20 57 6f 72 6b 20 61 72 6f 75  n;  /* Work arou
9a890 6e 64 20 62 75 67 20 69 6e 20 42 6f 72 6c 61 6e  nd bug in Borlan
9a8a0 64 20 43 2e 20 54 69 63 6b 65 74 20 23 33 32 31  d C. Ticket #321
9a8b0 36 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 65  6 */.  return re
9a8c0 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  s;.}../*.** Retu
9a8d0 72 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61  rn the maximum a
9a8e0 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20  mount of memory 
9a8f0 74 68 61 74 20 68 61 73 20 65 76 65 72 20 62 65  that has ever be
9a900 65 6e 0a 2a 2a 20 63 68 65 63 6b 65 64 20 6f 75  en.** checked ou
9a910 74 20 73 69 6e 63 65 20 65 69 74 68 65 72 20 74  t since either t
9a920 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
9a930 74 68 69 73 20 70 72 6f 63 65 73 73 0a 2a 2a 20  this process.** 
9a940 6f 72 20 73 69 6e 63 65 20 74 68 65 20 6d 6f 73  or since the mos
9a950 74 20 72 65 63 65 6e 74 20 72 65 73 65 74 2e 0a  t recent reset..
9a960 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71  */.SQLITE_API sq
9a970 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69  lite3_int64 sqli
9a980 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77  te3_memory_highw
9a990 61 74 65 72 28 69 6e 74 20 72 65 73 65 74 46 6c  ater(int resetFl
9a9a0 61 67 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 6d 78  ag){.  int n, mx
9a9b0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ;.  sqlite3_int6
9a9c0 34 20 72 65 73 3b 0a 20 20 73 71 6c 69 74 65 33  4 res;.  sqlite3
9a9d0 5f 73 74 61 74 75 73 28 53 51 4c 49 54 45 5f 53  _status(SQLITE_S
9a9e0 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45  TATUS_MEMORY_USE
9a9f0 44 2c 20 26 6e 2c 20 26 6d 78 2c 20 72 65 73 65  D, &n, &mx, rese
9aa00 74 46 6c 61 67 29 3b 0a 20 20 72 65 73 20 3d 20  tFlag);.  res = 
9aa10 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 6d  (sqlite3_int64)m
9aa20 78 3b 20 20 2f 2a 20 57 6f 72 6b 20 61 72 6f 75  x;  /* Work arou
9aa30 6e 64 20 62 75 67 20 69 6e 20 42 6f 72 6c 61 6e  nd bug in Borlan
9aa40 64 20 43 2e 20 54 69 63 6b 65 74 20 23 33 32 31  d C. Ticket #321
9aa50 36 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 65  6 */.  return re
9aa60 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e  s;.}../*.** Chan
9aa70 67 65 20 74 68 65 20 61 6c 61 72 6d 20 63 61 6c  ge the alarm cal
9aa80 6c 62 61 63 6b 0a 2a 2f 0a 53 51 4c 49 54 45 5f  lback.*/.SQLITE_
9aa90 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
9aaa0 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 0a  te3MemoryAlarm(.
9aab0 20 20 76 6f 69 64 28 2a 78 43 61 6c 6c 62 61 63    void(*xCallbac
9aac0 6b 29 28 76 6f 69 64 20 2a 70 41 72 67 2c 20 73  k)(void *pArg, s
9aad0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 75 73 65  qlite3_int64 use
9aae0 64 2c 69 6e 74 20 4e 29 2c 0a 20 20 76 6f 69 64  d,int N),.  void
9aaf0 20 2a 70 41 72 67 2c 0a 20 20 73 71 6c 69 74 65   *pArg,.  sqlite
9ab00 33 5f 69 6e 74 36 34 20 69 54 68 72 65 73 68 6f  3_int64 iThresho
9ab10 6c 64 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  ld.){.  sqlite3_
9ab20 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30  mutex_enter(mem0
9ab30 2e 6d 75 74 65 78 29 3b 0a 20 20 6d 65 6d 30 2e  .mutex);.  mem0.
9ab40 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20 3d 20  alarmCallback = 
9ab50 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 6d 65 6d  xCallback;.  mem
9ab60 30 2e 61 6c 61 72 6d 41 72 67 20 3d 20 70 41 72  0.alarmArg = pAr
9ab70 67 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 54  g;.  mem0.alarmT
9ab80 68 72 65 73 68 6f 6c 64 20 3d 20 69 54 68 72 65  hreshold = iThre
9ab90 73 68 6f 6c 64 3b 0a 20 20 73 71 6c 69 74 65 33  shold;.  sqlite3
9aba0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d  _mutex_leave(mem
9abb0 30 2e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  0.mutex);.  retu
9abc0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
9abd0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
9abe0 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a  OMIT_DEPRECATED.
9abf0 2f 2a 0a 2a 2a 20 44 65 70 72 65 63 61 74 65 64  /*.** Deprecated
9ac00 20 65 78 74 65 72 6e 61 6c 20 69 6e 74 65 72 66   external interf
9ac10 61 63 65 2e 20 20 49 6e 74 65 72 6e 61 6c 2f 63  ace.  Internal/c
9ac20 6f 72 65 20 53 51 4c 69 74 65 20 63 6f 64 65 0a  ore SQLite code.
9ac30 2a 2a 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 73  ** should call s
9ac40 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72  qlite3MemoryAlar
9ac50 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  m..*/.SQLITE_API
9ac60 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d   int sqlite3_mem
9ac70 6f 72 79 5f 61 6c 61 72 6d 28 0a 20 20 76 6f 69  ory_alarm(.  voi
9ac80 64 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f  d(*xCallback)(vo
9ac90 69 64 20 2a 70 41 72 67 2c 20 73 71 6c 69 74 65  id *pArg, sqlite
9aca0 33 5f 69 6e 74 36 34 20 75 73 65 64 2c 69 6e 74  3_int64 used,int
9acb0 20 4e 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72   N),.  void *pAr
9acc0 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  g,.  sqlite3_int
9acd0 36 34 20 69 54 68 72 65 73 68 6f 6c 64 0a 29 7b  64 iThreshold.){
9ace0 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
9acf0 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 78 43 61  3MemoryAlarm(xCa
9ad00 6c 6c 62 61 63 6b 2c 20 70 41 72 67 2c 20 69 54  llback, pArg, iT
9ad10 68 72 65 73 68 6f 6c 64 29 3b 0a 7d 0a 23 65 6e  hreshold);.}.#en
9ad20 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 72 69 67 67  dif../*.** Trigg
9ad30 65 72 20 74 68 65 20 61 6c 61 72 6d 20 0a 2a 2f  er the alarm .*/
9ad40 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c  .static void sql
9ad50 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28  ite3MallocAlarm(
9ad60 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 76 6f  int nByte){.  vo
9ad70 69 64 20 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28  id (*xCallback)(
9ad80 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f 69 6e  void*,sqlite3_in
9ad90 74 36 34 2c 69 6e 74 29 3b 0a 20 20 73 71 6c 69  t64,int);.  sqli
9ada0 74 65 33 5f 69 6e 74 36 34 20 6e 6f 77 55 73 65  te3_int64 nowUse
9adb0 64 3b 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 3b  d;.  void *pArg;
9adc0 0a 20 20 69 66 28 20 6d 65 6d 30 2e 61 6c 61 72  .  if( mem0.alar
9add0 6d 43 61 6c 6c 62 61 63 6b 3d 3d 30 20 29 20 72  mCallback==0 ) r
9ade0 65 74 75 72 6e 3b 0a 20 20 78 43 61 6c 6c 62 61  eturn;.  xCallba
9adf0 63 6b 20 3d 20 6d 65 6d 30 2e 61 6c 61 72 6d 43  ck = mem0.alarmC
9ae00 61 6c 6c 62 61 63 6b 3b 0a 20 20 6e 6f 77 55 73  allback;.  nowUs
9ae10 65 64 20 3d 20 73 71 6c 69 74 65 33 53 74 61 74  ed = sqlite3Stat
9ae20 75 73 56 61 6c 75 65 28 53 51 4c 49 54 45 5f 53  usValue(SQLITE_S
9ae30 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45  TATUS_MEMORY_USE
9ae40 44 29 3b 0a 20 20 70 41 72 67 20 3d 20 6d 65 6d  D);.  pArg = mem
9ae50 30 2e 61 6c 61 72 6d 41 72 67 3b 0a 20 20 6d 65  0.alarmArg;.  me
9ae60 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b  m0.alarmCallback
9ae70 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f   = 0;.  sqlite3_
9ae80 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30  mutex_leave(mem0
9ae90 2e 6d 75 74 65 78 29 3b 0a 20 20 78 43 61 6c 6c  .mutex);.  xCall
9aea0 62 61 63 6b 28 70 41 72 67 2c 20 6e 6f 77 55 73  back(pArg, nowUs
9aeb0 65 64 2c 20 6e 42 79 74 65 29 3b 0a 20 20 73 71  ed, nByte);.  sq
9aec0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
9aed0 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20  r(mem0.mutex);. 
9aee0 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62   mem0.alarmCallb
9aef0 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b  ack = xCallback;
9af00 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 41 72 67  .  mem0.alarmArg
9af10 20 3d 20 70 41 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a   = pArg;.}../*.*
9af20 2a 20 44 6f 20 61 20 6d 65 6d 6f 72 79 20 61 6c  * Do a memory al
9af30 6c 6f 63 61 74 69 6f 6e 20 77 69 74 68 20 73 74  location with st
9af40 61 74 69 73 74 69 63 73 20 61 6e 64 20 61 6c 61  atistics and ala
9af50 72 6d 73 2e 20 20 41 73 73 75 6d 65 20 74 68 65  rms.  Assume the
9af60 0a 2a 2a 20 6c 6f 63 6b 20 69 73 20 61 6c 72 65  .** lock is alre
9af70 61 64 79 20 68 65 6c 64 2e 0a 2a 2f 0a 73 74 61  ady held..*/.sta
9af80 74 69 63 20 69 6e 74 20 6d 61 6c 6c 6f 63 57 69  tic int mallocWi
9af90 74 68 41 6c 61 72 6d 28 69 6e 74 20 6e 2c 20 76  thAlarm(int n, v
9afa0 6f 69 64 20 2a 2a 70 70 29 7b 0a 20 20 69 6e 74  oid **pp){.  int
9afb0 20 6e 46 75 6c 6c 3b 0a 20 20 76 6f 69 64 20 2a   nFull;.  void *
9afc0 70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  p;.  assert( sql
9afd0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
9afe0 6d 65 6d 30 2e 6d 75 74 65 78 29 20 29 3b 0a 20  mem0.mutex) );. 
9aff0 20 6e 46 75 6c 6c 20 3d 20 73 71 6c 69 74 65 33   nFull = sqlite3
9b000 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78  GlobalConfig.m.x
9b010 52 6f 75 6e 64 75 70 28 6e 29 3b 0a 20 20 73 71  Roundup(n);.  sq
9b020 6c 69 74 65 33 53 74 61 74 75 73 53 65 74 28 53  lite3StatusSet(S
9b030 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 41 4c  QLITE_STATUS_MAL
9b040 4c 4f 43 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20 20  LOC_SIZE, n);.  
9b050 69 66 28 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61  if( mem0.alarmCa
9b060 6c 6c 62 61 63 6b 21 3d 30 20 29 7b 0a 20 20 20  llback!=0 ){.   
9b070 20 69 6e 74 20 6e 55 73 65 64 20 3d 20 73 71 6c   int nUsed = sql
9b080 69 74 65 33 53 74 61 74 75 73 56 61 6c 75 65 28  ite3StatusValue(
9b090 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45  SQLITE_STATUS_ME
9b0a0 4d 4f 52 59 5f 55 53 45 44 29 3b 0a 20 20 20 20  MORY_USED);.    
9b0b0 69 66 28 20 6e 55 73 65 64 2b 6e 46 75 6c 6c 20  if( nUsed+nFull 
9b0c0 3e 3d 20 6d 65 6d 30 2e 61 6c 61 72 6d 54 68 72  >= mem0.alarmThr
9b0d0 65 73 68 6f 6c 64 20 29 7b 0a 20 20 20 20 20 20  eshold ){.      
9b0e0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61  sqlite3MallocAla
9b0f0 72 6d 28 6e 46 75 6c 6c 29 3b 0a 20 20 20 20 7d  rm(nFull);.    }
9b100 0a 20 20 7d 0a 20 20 70 20 3d 20 73 71 6c 69 74  .  }.  p = sqlit
9b110 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
9b120 2e 78 4d 61 6c 6c 6f 63 28 6e 46 75 6c 6c 29 3b  .xMalloc(nFull);
9b130 0a 20 20 69 66 28 20 70 3d 3d 30 20 26 26 20 6d  .  if( p==0 && m
9b140 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63  em0.alarmCallbac
9b150 6b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  k ){.    sqlite3
9b160 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 46 75 6c  MallocAlarm(nFul
9b170 6c 29 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69  l);.    p = sqli
9b180 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9b190 6d 2e 78 4d 61 6c 6c 6f 63 28 6e 46 75 6c 6c 29  m.xMalloc(nFull)
9b1a0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 20 29 7b  ;.  }.  if( p ){
9b1b0 0a 20 20 20 20 6e 46 75 6c 6c 20 3d 20 73 71 6c  .    nFull = sql
9b1c0 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70  ite3MallocSize(p
9b1d0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74  );.    sqlite3St
9b1e0 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53  atusAdd(SQLITE_S
9b1f0 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45  TATUS_MEMORY_USE
9b200 44 2c 20 6e 46 75 6c 6c 29 3b 0a 20 20 7d 0a 20  D, nFull);.  }. 
9b210 20 2a 70 70 20 3d 20 70 3b 0a 20 20 72 65 74 75   *pp = p;.  retu
9b220 72 6e 20 6e 46 75 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a  rn nFull;.}../*.
9b230 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f  ** Allocate memo
9b240 72 79 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  ry.  This routin
9b250 65 20 69 73 20 6c 69 6b 65 20 73 71 6c 69 74 65  e is like sqlite
9b260 33 5f 6d 61 6c 6c 6f 63 28 29 20 65 78 63 65 70  3_malloc() excep
9b270 74 20 74 68 61 74 20 69 74 0a 2a 2a 20 61 73 73  t that it.** ass
9b280 75 6d 65 73 20 74 68 65 20 6d 65 6d 6f 72 79 20  umes the memory 
9b290 73 75 62 73 79 73 74 65 6d 20 68 61 73 20 61 6c  subsystem has al
9b2a0 72 65 61 64 79 20 62 65 65 6e 20 69 6e 69 74 69  ready been initi
9b2b0 61 6c 69 7a 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  alized..*/.SQLIT
9b2c0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
9b2d0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 69 6e  sqlite3Malloc(in
9b2e0 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b  t n){.  void *p;
9b2f0 0a 20 20 69 66 28 20 6e 3c 3d 30 20 7c 7c 20 6e  .  if( n<=0 || n
9b300 3e 3d 30 78 37 66 66 66 66 66 30 30 20 29 7b 0a  >=0x7fffff00 ){.
9b310 20 20 20 20 2f 2a 20 41 20 6d 65 6d 6f 72 79 20      /* A memory 
9b320 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 61 20  allocation of a 
9b330 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
9b340 77 68 69 63 68 20 69 73 20 6e 65 61 72 20 74 68  which is near th
9b350 65 20 6d 61 78 69 6d 75 6d 0a 20 20 20 20 2a 2a  e maximum.    **
9b360 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20   signed integer 
9b370 76 61 6c 75 65 20 6d 69 67 68 74 20 63 61 75 73  value might caus
9b380 65 20 61 6e 20 69 6e 74 65 67 65 72 20 6f 76 65  e an integer ove
9b390 72 66 6c 6f 77 20 69 6e 73 69 64 65 20 6f 66 20  rflow inside of 
9b3a0 74 68 65 0a 20 20 20 20 2a 2a 20 78 4d 61 6c 6c  the.    ** xMall
9b3b0 6f 63 28 29 2e 20 20 48 65 6e 63 65 20 77 65 20  oc().  Hence we 
9b3c0 6c 69 6d 69 74 20 74 68 65 20 6d 61 78 69 6d 75  limit the maximu
9b3d0 6d 20 73 69 7a 65 20 74 6f 20 30 78 37 66 66 66  m size to 0x7fff
9b3e0 66 66 30 30 2c 20 67 69 76 69 6e 67 0a 20 20 20  ff00, giving.   
9b3f0 20 2a 2a 20 32 35 35 20 62 79 74 65 73 20 6f 66   ** 255 bytes of
9b400 20 6f 76 65 72 68 65 61 64 2e 20 20 53 51 4c 69   overhead.  SQLi
9b410 74 65 20 69 74 73 65 6c 66 20 77 69 6c 6c 20 6e  te itself will n
9b420 65 76 65 72 20 75 73 65 20 61 6e 79 74 68 69 6e  ever use anythin
9b430 67 20 6e 65 61 72 0a 20 20 20 20 2a 2a 20 74 68  g near.    ** th
9b440 69 73 20 61 6d 6f 75 6e 74 2e 20 20 54 68 65 20  is amount.  The 
9b450 6f 6e 6c 79 20 77 61 79 20 74 6f 20 72 65 61 63  only way to reac
9b460 68 20 74 68 65 20 6c 69 6d 69 74 20 69 73 20 77  h the limit is w
9b470 69 74 68 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  ith sqlite3_mall
9b480 6f 63 28 29 20 2a 2f 0a 20 20 20 20 70 20 3d 20  oc() */.    p = 
9b490 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73  0;.  }else if( s
9b4a0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9b4b0 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20  ig.bMemstat ){. 
9b4c0 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
9b4d0 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65  _enter(mem0.mute
9b4e0 78 29 3b 0a 20 20 20 20 6d 61 6c 6c 6f 63 57 69  x);.    mallocWi
9b4f0 74 68 41 6c 61 72 6d 28 6e 2c 20 26 70 29 3b 0a  thAlarm(n, &p);.
9b500 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
9b510 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74  x_leave(mem0.mut
9b520 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ex);.  }else{.  
9b530 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f    p = sqlite3Glo
9b540 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c  balConfig.m.xMal
9b550 6c 6f 63 28 6e 29 3b 0a 20 20 7d 0a 20 20 72 65  loc(n);.  }.  re
9b560 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn p;.}../*.**
9b570 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66   This version of
9b580 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
9b590 63 61 74 69 6f 6e 20 69 73 20 66 6f 72 20 75 73  cation is for us
9b5a0 65 20 62 79 20 74 68 65 20 61 70 70 6c 69 63 61  e by the applica
9b5b0 74 69 6f 6e 2e 0a 2a 2a 20 46 69 72 73 74 20 6d  tion..** First m
9b5c0 61 6b 65 20 73 75 72 65 20 74 68 65 20 6d 65 6d  ake sure the mem
9b5d0 6f 72 79 20 73 75 62 73 79 73 74 65 6d 20 69 73  ory subsystem is
9b5e0 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20 74 68   initialized, th
9b5f0 65 6e 20 64 6f 20 74 68 65 0a 2a 2a 20 61 6c 6c  en do the.** all
9b600 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49  ocation..*/.SQLI
9b610 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
9b620 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69 6e 74 20  ite3_malloc(int 
9b630 6e 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  n){.#ifndef SQLI
9b640 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54  TE_OMIT_AUTOINIT
9b650 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69  .  if( sqlite3_i
9b660 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65  nitialize() ) re
9b670 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20  turn 0;.#endif. 
9b680 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4d   return sqlite3M
9b690 61 6c 6c 6f 63 28 6e 29 3b 0a 7d 0a 0a 2f 2a 0a  alloc(n);.}../*.
9b6a0 2a 2a 20 45 61 63 68 20 74 68 72 65 61 64 20 6d  ** Each thread m
9b6b0 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 61 20 73  ay only have a s
9b6c0 69 6e 67 6c 65 20 6f 75 74 73 74 61 6e 64 69 6e  ingle outstandin
9b6d0 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 72 6f  g allocation fro
9b6e0 6d 0a 2a 2a 20 78 53 63 72 61 74 63 68 4d 61 6c  m.** xScratchMal
9b6f0 6c 6f 63 28 29 2e 20 20 57 65 20 76 65 72 69 66  loc().  We verif
9b700 79 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e  y this constrain
9b710 74 20 69 6e 20 74 68 65 20 73 69 6e 67 6c 65 2d  t in the single-
9b720 74 68 72 65 61 64 65 64 0a 2a 2a 20 63 61 73 65  threaded.** case
9b730 20 62 79 20 73 65 74 74 69 6e 67 20 73 63 72 61   by setting scra
9b740 74 63 68 41 6c 6c 6f 63 4f 75 74 20 74 6f 20 31  tchAllocOut to 1
9b750 20 77 68 65 6e 20 61 6e 20 61 6c 6c 6f 63 61 74   when an allocat
9b760 69 6f 6e 0a 2a 2a 20 69 73 20 6f 75 74 73 74 61  ion.** is outsta
9b770 6e 64 69 6e 67 20 63 6c 65 61 72 69 6e 67 20 69  nding clearing i
9b780 74 20 77 68 65 6e 20 74 68 65 20 61 6c 6c 6f 63  t when the alloc
9b790 61 74 69 6f 6e 20 69 73 20 66 72 65 65 64 2e 0a  ation is freed..
9b7a0 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  */.#if SQLITE_TH
9b7b0 52 45 41 44 53 41 46 45 3d 3d 30 20 26 26 20 21  READSAFE==0 && !
9b7c0 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 0a  defined(NDEBUG).
9b7d0 73 74 61 74 69 63 20 69 6e 74 20 73 63 72 61 74  static int scrat
9b7e0 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 30 3b 0a  chAllocOut = 0;.
9b7f0 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41  #endif.../*.** A
9b800 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 74  llocate memory t
9b810 68 61 74 20 69 73 20 74 6f 20 62 65 20 75 73 65  hat is to be use
9b820 64 20 61 6e 64 20 72 65 6c 65 61 73 65 64 20 72  d and released r
9b830 69 67 68 74 20 61 77 61 79 2e 0a 2a 2a 20 54 68  ight away..** Th
9b840 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 73 69  is routine is si
9b850 6d 69 6c 61 72 20 74 6f 20 61 6c 6c 6f 63 61 28  milar to alloca(
9b860 29 20 69 6e 20 74 68 61 74 20 69 74 20 69 73 20  ) in that it is 
9b870 6e 6f 74 20 69 6e 74 65 6e 64 65 64 0a 2a 2a 20  not intended.** 
9b880 66 6f 72 20 73 69 74 75 61 74 69 6f 6e 73 20 77  for situations w
9b890 68 65 72 65 20 74 68 65 20 6d 65 6d 6f 72 79 20  here the memory 
9b8a0 6d 69 67 68 74 20 62 65 20 68 65 6c 64 20 6c 6f  might be held lo
9b8b0 6e 67 2d 74 65 72 6d 2e 20 20 54 68 69 73 0a 2a  ng-term.  This.*
9b8c0 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6e 74  * routine is int
9b8d0 65 6e 64 65 64 20 74 6f 20 67 65 74 20 6d 65 6d  ended to get mem
9b8e0 6f 72 79 20 74 6f 20 6f 6c 64 20 6c 61 72 67 65  ory to old large
9b8f0 20 74 72 61 6e 73 69 65 6e 74 20 64 61 74 61 0a   transient data.
9b900 2a 2a 20 73 74 72 75 63 74 75 72 65 73 20 74 68  ** structures th
9b910 61 74 20 77 6f 75 6c 64 20 6e 6f 74 20 6e 6f 72  at would not nor
9b920 6d 61 6c 6c 79 20 66 69 74 20 6f 6e 20 74 68 65  mally fit on the
9b930 20 73 74 61 63 6b 20 6f 66 20 61 6e 0a 2a 2a 20   stack of an.** 
9b940 65 6d 62 65 64 64 65 64 20 70 72 6f 63 65 73 73  embedded process
9b950 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  or..*/.SQLITE_PR
9b960 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
9b970 74 65 33 53 63 72 61 74 63 68 4d 61 6c 6c 6f 63  te3ScratchMalloc
9b980 28 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20  (int n){.  void 
9b990 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3e  *p;.  assert( n>
9b9a0 30 20 29 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45  0 );..#if SQLITE
9b9b0 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26  _THREADSAFE==0 &
9b9c0 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55  & !defined(NDEBU
9b9d0 47 29 0a 20 20 2f 2a 20 56 65 72 69 66 79 20 74  G).  /* Verify t
9b9e0 68 61 74 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e  hat no more than
9b9f0 20 6f 6e 65 20 73 63 72 61 74 63 68 20 61 6c 6c   one scratch all
9ba00 6f 63 61 74 69 6f 6e 20 70 65 72 20 74 68 72 65  ocation per thre
9ba10 61 64 0a 20 20 2a 2a 20 69 73 20 6f 75 74 73 74  ad.  ** is outst
9ba20 61 6e 64 69 6e 67 20 61 74 20 6f 6e 65 20 74 69  anding at one ti
9ba30 6d 65 2e 20 20 28 54 68 69 73 20 69 73 20 6f 6e  me.  (This is on
9ba40 6c 79 20 63 68 65 63 6b 65 64 20 69 6e 20 74 68  ly checked in th
9ba50 65 0a 20 20 2a 2a 20 73 69 6e 67 6c 65 2d 74 68  e.  ** single-th
9ba60 72 65 61 64 65 64 20 63 61 73 65 20 73 69 6e 63  readed case sinc
9ba70 65 20 63 68 65 63 6b 69 6e 67 20 69 6e 20 74 68  e checking in th
9ba80 65 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64  e multi-threaded
9ba90 20 63 61 73 65 0a 20 20 2a 2a 20 77 6f 75 6c 64   case.  ** would
9baa0 20 62 65 20 6d 75 63 68 20 6d 6f 72 65 20 63 6f   be much more co
9bab0 6d 70 6c 69 63 61 74 65 64 2e 29 20 2a 2f 0a 20  mplicated.) */. 
9bac0 20 61 73 73 65 72 74 28 20 73 63 72 61 74 63 68   assert( scratch
9bad0 41 6c 6c 6f 63 4f 75 74 3d 3d 30 20 29 3b 0a 23  AllocOut==0 );.#
9bae0 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 73 71 6c  endif..  if( sql
9baf0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9bb00 2e 73 7a 53 63 72 61 74 63 68 3c 6e 20 29 7b 0a  .szScratch<n ){.
9bb10 20 20 20 20 67 6f 74 6f 20 73 63 72 61 74 63 68      goto scratch
9bb20 5f 6f 76 65 72 66 6c 6f 77 3b 0a 20 20 7d 65 6c  _overflow;.  }el
9bb30 73 65 7b 20 20 0a 20 20 20 20 73 71 6c 69 74 65  se{  .    sqlite
9bb40 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65  3_mutex_enter(me
9bb50 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 69  m0.mutex);.    i
9bb60 66 28 20 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68  f( mem0.nScratch
9bb70 46 72 65 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Free==0 ){.     
9bb80 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
9bb90 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29  eave(mem0.mutex)
9bba0 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 73 63 72  ;.      goto scr
9bbb0 61 74 63 68 5f 6f 76 65 72 66 6c 6f 77 3b 0a 20  atch_overflow;. 
9bbc0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
9bbd0 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 69 20 3d  int i;.      i =
9bbe0 20 6d 65 6d 30 2e 61 53 63 72 61 74 63 68 46 72   mem0.aScratchFr
9bbf0 65 65 5b 2d 2d 6d 65 6d 30 2e 6e 53 63 72 61 74  ee[--mem0.nScrat
9bc00 63 68 46 72 65 65 5d 3b 0a 20 20 20 20 20 20 69  chFree];.      i
9bc10 20 2a 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61   *= sqlite3Globa
9bc20 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63  lConfig.szScratc
9bc30 68 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  h;.      sqlite3
9bc40 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45  StatusAdd(SQLITE
9bc50 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f  _STATUS_SCRATCH_
9bc60 55 53 45 44 2c 20 31 29 3b 0a 20 20 20 20 20 20  USED, 1);.      
9bc70 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 65 74  sqlite3StatusSet
9bc80 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53  (SQLITE_STATUS_S
9bc90 43 52 41 54 43 48 5f 53 49 5a 45 2c 20 6e 29 3b  CRATCH_SIZE, n);
9bca0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  .      sqlite3_m
9bcb0 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e  utex_leave(mem0.
9bcc0 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 70 20  mutex);.      p 
9bcd0 3d 20 28 76 6f 69 64 2a 29 26 28 28 63 68 61 72  = (void*)&((char
9bce0 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  *)sqlite3GlobalC
9bcf0 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 29 5b  onfig.pScratch)[
9bd00 69 5d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  i];.      assert
9bd10 28 20 20 28 28 28 75 38 2a 29 70 20 2d 20 28 75  (  (((u8*)p - (u
9bd20 38 2a 29 30 29 20 26 20 37 29 3d 3d 30 20 29 3b  8*)0) & 7)==0 );
9bd30 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 69 66 20 53  .    }.  }.#if S
9bd40 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
9bd50 3d 3d 30 20 26 26 20 21 64 65 66 69 6e 65 64 28  ==0 && !defined(
9bd60 4e 44 45 42 55 47 29 0a 20 20 73 63 72 61 74 63  NDEBUG).  scratc
9bd70 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 70 21 3d 30  hAllocOut = p!=0
9bd80 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75  ;.#endif..  retu
9bd90 72 6e 20 70 3b 0a 0a 73 63 72 61 74 63 68 5f 6f  rn p;..scratch_o
9bda0 76 65 72 66 6c 6f 77 3a 0a 20 20 69 66 28 20 73  verflow:.  if( s
9bdb0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9bdc0 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20  ig.bMemstat ){. 
9bdd0 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
9bde0 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65  _enter(mem0.mute
9bdf0 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53  x);.    sqlite3S
9be00 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f  tatusSet(SQLITE_
9be10 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 53  STATUS_SCRATCH_S
9be20 49 5a 45 2c 20 6e 29 3b 0a 20 20 20 20 6e 20 3d  IZE, n);.    n =
9be30 20 6d 61 6c 6c 6f 63 57 69 74 68 41 6c 61 72 6d   mallocWithAlarm
9be40 28 6e 2c 20 26 70 29 3b 0a 20 20 20 20 69 66 28  (n, &p);.    if(
9be50 20 70 20 29 20 73 71 6c 69 74 65 33 53 74 61 74   p ) sqlite3Stat
9be60 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41  usAdd(SQLITE_STA
9be70 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 45 52  TUS_SCRATCH_OVER
9be80 46 4c 4f 57 2c 20 6e 29 3b 0a 20 20 20 20 73 71  FLOW, n);.    sq
9be90 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
9bea0 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20  e(mem0.mutex);. 
9beb0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20   }else{.    p = 
9bec0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9bed0 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e 29  fig.m.xMalloc(n)
9bee0 3b 0a 20 20 7d 0a 23 69 66 20 53 51 4c 49 54 45  ;.  }.#if SQLITE
9bef0 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26  _THREADSAFE==0 &
9bf00 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55  & !defined(NDEBU
9bf10 47 29 0a 20 20 73 63 72 61 74 63 68 41 6c 6c 6f  G).  scratchAllo
9bf20 63 4f 75 74 20 3d 20 70 21 3d 30 3b 0a 23 65 6e  cOut = p!=0;.#en
9bf30 64 69 66 0a 20 20 72 65 74 75 72 6e 20 70 3b 20  dif.  return p; 
9bf40 20 20 20 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49     .}.SQLITE_PRI
9bf50 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
9bf60 33 53 63 72 61 74 63 68 46 72 65 65 28 76 6f 69  3ScratchFree(voi
9bf70 64 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29  d *p){.  if( p )
9bf80 7b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  {..#if SQLITE_TH
9bf90 52 45 41 44 53 41 46 45 3d 3d 30 20 26 26 20 21  READSAFE==0 && !
9bfa0 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 0a  defined(NDEBUG).
9bfb0 20 20 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68      /* Verify th
9bfc0 61 74 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e 20  at no more than 
9bfd0 6f 6e 65 20 73 63 72 61 74 63 68 20 61 6c 6c 6f  one scratch allo
9bfe0 63 61 74 69 6f 6e 20 70 65 72 20 74 68 72 65 61  cation per threa
9bff0 64 0a 20 20 20 20 2a 2a 20 69 73 20 6f 75 74 73  d.    ** is outs
9c000 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e 65 20 74  tanding at one t
9c010 69 6d 65 2e 20 20 28 54 68 69 73 20 69 73 20 6f  ime.  (This is o
9c020 6e 6c 79 20 63 68 65 63 6b 65 64 20 69 6e 20 74  nly checked in t
9c030 68 65 0a 20 20 20 20 2a 2a 20 73 69 6e 67 6c 65  he.    ** single
9c040 2d 74 68 72 65 61 64 65 64 20 63 61 73 65 20 73  -threaded case s
9c050 69 6e 63 65 20 63 68 65 63 6b 69 6e 67 20 69 6e  ince checking in
9c060 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 72 65 61   the multi-threa
9c070 64 65 64 20 63 61 73 65 0a 20 20 20 20 2a 2a 20  ded case.    ** 
9c080 77 6f 75 6c 64 20 62 65 20 6d 75 63 68 20 6d 6f  would be much mo
9c090 72 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 2e 29  re complicated.)
9c0a0 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
9c0b0 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 3d  scratchAllocOut=
9c0c0 3d 31 20 29 3b 0a 20 20 20 20 73 63 72 61 74 63  =1 );.    scratc
9c0d0 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 30 3b 0a 23  hAllocOut = 0;.#
9c0e0 65 6e 64 69 66 0a 0a 20 20 20 20 69 66 28 20 73  endif..    if( s
9c0f0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9c100 69 67 2e 70 53 63 72 61 74 63 68 3d 3d 30 0a 20  ig.pScratch==0. 
9c110 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 3c 73            || p<s
9c120 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9c130 69 67 2e 70 53 63 72 61 74 63 68 0a 20 20 20 20  ig.pScratch.    
9c140 20 20 20 20 20 20 20 7c 7c 20 70 3e 3d 28 76 6f         || p>=(vo
9c150 69 64 2a 29 6d 65 6d 30 2e 61 53 63 72 61 74 63  id*)mem0.aScratc
9c160 68 46 72 65 65 20 29 7b 0a 20 20 20 20 20 20 69  hFree ){.      i
9c170 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  f( sqlite3Global
9c180 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20  Config.bMemstat 
9c190 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  ){.        int i
9c1a0 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 4d 61  Size = sqlite3Ma
9c1b0 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20 20 20  llocSize(p);.   
9c1c0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74       sqlite3_mut
9c1d0 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75  ex_enter(mem0.mu
9c1e0 74 65 78 29 3b 0a 20 20 20 20 20 20 20 20 73 71  tex);.        sq
9c1f0 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53  lite3StatusAdd(S
9c200 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52  QLITE_STATUS_SCR
9c210 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57 2c 20 2d  ATCH_OVERFLOW, -
9c220 69 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20  iSize);.        
9c230 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64  sqlite3StatusAdd
9c240 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d  (SQLITE_STATUS_M
9c250 45 4d 4f 52 59 5f 55 53 45 44 2c 20 2d 69 53 69  EMORY_USED, -iSi
9c260 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ze);.        sql
9c270 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9c280 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a 20 20 20  .m.xFree(p);.   
9c290 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74       sqlite3_mut
9c2a0 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75  ex_leave(mem0.mu
9c2b0 74 65 78 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  tex);.      }els
9c2c0 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  e{.        sqlit
9c2d0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
9c2e0 2e 78 46 72 65 65 28 70 29 3b 0a 20 20 20 20 20  .xFree(p);.     
9c2f0 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
9c300 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20      int i;.     
9c310 20 69 20 3d 20 28 69 6e 74 29 28 28 75 38 2a 29   i = (int)((u8*)
9c320 70 20 2d 20 28 75 38 2a 29 73 71 6c 69 74 65 33  p - (u8*)sqlite3
9c330 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63  GlobalConfig.pSc
9c340 72 61 74 63 68 29 3b 0a 20 20 20 20 20 20 69 20  ratch);.      i 
9c350 2f 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  /= sqlite3Global
9c360 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68  Config.szScratch
9c370 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
9c380 69 3e 3d 30 20 26 26 20 69 3c 73 71 6c 69 74 65  i>=0 && i<sqlite
9c390 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53  3GlobalConfig.nS
9c3a0 63 72 61 74 63 68 20 29 3b 0a 20 20 20 20 20 20  cratch );.      
9c3b0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
9c3c0 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b  ter(mem0.mutex);
9c3d0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6d  .      assert( m
9c3e0 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65 65  em0.nScratchFree
9c3f0 3c 28 75 33 32 29 73 71 6c 69 74 65 33 47 6c 6f  <(u32)sqlite3Glo
9c400 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74  balConfig.nScrat
9c410 63 68 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d 30  ch );.      mem0
9c420 2e 61 53 63 72 61 74 63 68 46 72 65 65 5b 6d 65  .aScratchFree[me
9c430 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65 65 2b  m0.nScratchFree+
9c440 2b 5d 20 3d 20 69 3b 0a 20 20 20 20 20 20 73 71  +] = i;.      sq
9c450 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53  lite3StatusAdd(S
9c460 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52  QLITE_STATUS_SCR
9c470 41 54 43 48 5f 55 53 45 44 2c 20 2d 31 29 3b 0a  ATCH_USED, -1);.
9c480 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75        sqlite3_mu
9c490 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d  tex_leave(mem0.m
9c4a0 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  utex);.    }.  }
9c4b0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 52 55 45 20 69  .}../*.** TRUE i
9c4c0 66 20 70 20 69 73 20 61 20 6c 6f 6f 6b 61 73 69  f p is a lookasi
9c4d0 64 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  de memory alloca
9c4e0 74 69 6f 6e 20 66 72 6f 6d 20 64 62 0a 2a 2f 0a  tion from db.*/.
9c4f0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
9c500 4d 49 54 5f 4c 4f 4f 4b 41 53 49 44 45 0a 73 74  MIT_LOOKASIDE.st
9c510 61 74 69 63 20 69 6e 74 20 69 73 4c 6f 6f 6b 61  atic int isLooka
9c520 73 69 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62  side(sqlite3 *db
9c530 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20 20 72 65  , void *p){.  re
9c540 74 75 72 6e 20 64 62 20 26 26 20 70 20 26 26 20  turn db && p && 
9c550 70 3e 3d 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65  p>=db->lookaside
9c560 2e 70 53 74 61 72 74 20 26 26 20 70 3c 64 62 2d  .pStart && p<db-
9c570 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 45 6e 64 3b  >lookaside.pEnd;
9c580 0a 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65  .}.#else.#define
9c590 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 41 2c 42   isLookaside(A,B
9c5a0 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ) 0.#endif../*.*
9c5b0 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a  * Return the siz
9c5c0 65 20 6f 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c  e of a memory al
9c5d0 6c 6f 63 61 74 69 6f 6e 20 70 72 65 76 69 6f 75  location previou
9c5e0 73 6c 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  sly obtained fro
9c5f0 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 4d 61 6c 6c  m.** sqlite3Mall
9c600 6f 63 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f  oc() or sqlite3_
9c610 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c  malloc()..*/.SQL
9c620 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
9c630 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a  sqlite3MallocSiz
9c640 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 72 65  e(void *p){.  re
9c650 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62  turn sqlite3Glob
9c660 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 69 7a 65  alConfig.m.xSize
9c670 28 70 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  (p);.}.SQLITE_PR
9c680 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
9c690 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 73 71  3DbMallocSize(sq
9c6a0 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20  lite3 *db, void 
9c6b0 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 64  *p){.  assert( d
9c6c0 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f  b==0 || sqlite3_
9c6d0 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d  mutex_held(db->m
9c6e0 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 69  utex) );.  if( i
9c6f0 73 4c 6f 6f 6b 61 73 69 64 65 28 64 62 2c 20 70  sLookaside(db, p
9c700 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
9c710 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a  db->lookaside.sz
9c720 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
9c730 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f  eturn sqlite3Glo
9c740 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 69 7a  balConfig.m.xSiz
9c750 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  e(p);.  }.}../*.
9c760 2a 2a 20 46 72 65 65 20 6d 65 6d 6f 72 79 20 70  ** Free memory p
9c770 72 65 76 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e  reviously obtain
9c780 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4d  ed from sqlite3M
9c790 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49  alloc()..*/.SQLI
9c7a0 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
9c7b0 74 65 33 5f 66 72 65 65 28 76 6f 69 64 20 2a 70  te3_free(void *p
9c7c0 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20  ){.  if( p==0 ) 
9c7d0 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 73 71  return;.  if( sq
9c7e0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9c7f0 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20  g.bMemstat ){.  
9c800 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
9c810 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78  enter(mem0.mutex
9c820 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74  );.    sqlite3St
9c830 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53  atusAdd(SQLITE_S
9c840 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45  TATUS_MEMORY_USE
9c850 44 2c 20 2d 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  D, -sqlite3Mallo
9c860 63 53 69 7a 65 28 70 29 29 3b 0a 20 20 20 20 73  cSize(p));.    s
9c870 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9c880 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a 20  ig.m.xFree(p);. 
9c890 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
9c8a0 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65  _leave(mem0.mute
9c8b0 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  x);.  }else{.   
9c8c0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
9c8d0 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b  nfig.m.xFree(p);
9c8e0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72  .  }.}../*.** Fr
9c8f0 65 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 6d  ee memory that m
9c900 69 67 68 74 20 62 65 20 61 73 73 6f 63 69 61 74  ight be associat
9c910 65 64 20 77 69 74 68 20 61 20 70 61 72 74 69 63  ed with a partic
9c920 75 6c 61 72 20 64 61 74 61 62 61 73 65 0a 2a 2a  ular database.**
9c930 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a   connection..*/.
9c940 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
9c950 6f 69 64 20 73 71 6c 69 74 65 33 44 62 46 72 65  oid sqlite3DbFre
9c960 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76  e(sqlite3 *db, v
9c970 6f 69 64 20 2a 70 29 7b 0a 20 20 61 73 73 65 72  oid *p){.  asser
9c980 74 28 20 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69  t( db==0 || sqli
9c990 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64  te3_mutex_held(d
9c9a0 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69  b->mutex) );.  i
9c9b0 66 28 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 64  f( isLookaside(d
9c9c0 62 2c 20 70 29 20 29 7b 0a 20 20 20 20 4c 6f 6f  b, p) ){.    Loo
9c9d0 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 42 75 66  kasideSlot *pBuf
9c9e0 20 3d 20 28 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f   = (LookasideSlo
9c9f0 74 2a 29 70 3b 0a 20 20 20 20 70 42 75 66 2d 3e  t*)p;.    pBuf->
9ca00 70 4e 65 78 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b  pNext = db->look
9ca10 61 73 69 64 65 2e 70 46 72 65 65 3b 0a 20 20 20  aside.pFree;.   
9ca20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70   db->lookaside.p
9ca30 46 72 65 65 20 3d 20 70 42 75 66 3b 0a 20 20 20  Free = pBuf;.   
9ca40 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e   db->lookaside.n
9ca50 4f 75 74 2d 2d 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Out--;.  }else{.
9ca60 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
9ca70 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  (p);.  }.}../*.*
9ca80 2a 20 43 68 61 6e 67 65 20 74 68 65 20 73 69 7a  * Change the siz
9ca90 65 20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67  e of an existing
9caa0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
9cab0 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  on.*/.SQLITE_PRI
9cac0 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
9cad0 65 33 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a  e3Realloc(void *
9cae0 70 4f 6c 64 2c 20 69 6e 74 20 6e 42 79 74 65 73  pOld, int nBytes
9caf0 29 7b 0a 20 20 69 6e 74 20 6e 4f 6c 64 2c 20 6e  ){.  int nOld, n
9cb00 4e 65 77 3b 0a 20 20 76 6f 69 64 20 2a 70 4e 65  New;.  void *pNe
9cb10 77 3b 0a 20 20 69 66 28 20 70 4f 6c 64 3d 3d 30  w;.  if( pOld==0
9cb20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73   ){.    return s
9cb30 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 42 79  qlite3Malloc(nBy
9cb40 74 65 73 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  tes);.  }.  if( 
9cb50 6e 42 79 74 65 73 3c 3d 30 20 29 7b 0a 20 20 20  nBytes<=0 ){.   
9cb60 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4f   sqlite3_free(pO
9cb70 6c 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ld);.    return 
9cb80 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 42 79  0;.  }.  if( nBy
9cb90 74 65 73 3e 3d 30 78 37 66 66 66 66 66 30 30 20  tes>=0x7fffff00 
9cba0 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 30 78  ){.    /* The 0x
9cbb0 37 66 66 66 66 30 30 20 6c 69 6d 69 74 20 74 65  7ffff00 limit te
9cbc0 72 6d 20 69 73 20 65 78 70 6c 61 69 6e 65 64 20  rm is explained 
9cbd0 69 6e 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 73  in comments on s
9cbe0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20 2a  qlite3Malloc() *
9cbf0 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  /.    return 0;.
9cc00 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 73 71 6c    }.  nOld = sql
9cc10 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70  ite3MallocSize(p
9cc20 4f 6c 64 29 3b 0a 20 20 6e 4e 65 77 20 3d 20 73  Old);.  nNew = s
9cc30 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9cc40 69 67 2e 6d 2e 78 52 6f 75 6e 64 75 70 28 6e 42  ig.m.xRoundup(nB
9cc50 79 74 65 73 29 3b 0a 20 20 69 66 28 20 6e 4f 6c  ytes);.  if( nOl
9cc60 64 3d 3d 6e 4e 65 77 20 29 7b 0a 20 20 20 20 70  d==nNew ){.    p
9cc70 4e 65 77 20 3d 20 70 4f 6c 64 3b 0a 20 20 7d 65  New = pOld;.  }e
9cc80 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 47  lse if( sqlite3G
9cc90 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d  lobalConfig.bMem
9cca0 73 74 61 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  stat ){.    sqli
9ccb0 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
9ccc0 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20  mem0.mutex);.   
9ccd0 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 65   sqlite3StatusSe
9cce0 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  t(SQLITE_STATUS_
9ccf0 4d 41 4c 4c 4f 43 5f 53 49 5a 45 2c 20 6e 42 79  MALLOC_SIZE, nBy
9cd00 74 65 73 29 3b 0a 20 20 20 20 69 66 28 20 73 71  tes);.    if( sq
9cd10 6c 69 74 65 33 53 74 61 74 75 73 56 61 6c 75 65  lite3StatusValue
9cd20 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d  (SQLITE_STATUS_M
9cd30 45 4d 4f 52 59 5f 55 53 45 44 29 2b 6e 4e 65 77  EMORY_USED)+nNew
9cd40 2d 6e 4f 6c 64 20 3e 3d 20 0a 20 20 20 20 20 20  -nOld >= .      
9cd50 20 20 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 54 68      mem0.alarmTh
9cd60 72 65 73 68 6f 6c 64 20 29 7b 0a 20 20 20 20 20  reshold ){.     
9cd70 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c   sqlite3MallocAl
9cd80 61 72 6d 28 6e 4e 65 77 2d 6e 4f 6c 64 29 3b 0a  arm(nNew-nOld);.
9cd90 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 20 3d      }.    pNew =
9cda0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
9cdb0 6e 66 69 67 2e 6d 2e 78 52 65 61 6c 6c 6f 63 28  nfig.m.xRealloc(
9cdc0 70 4f 6c 64 2c 20 6e 4e 65 77 29 3b 0a 20 20 20  pOld, nNew);.   
9cdd0 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 26 26 20   if( pNew==0 && 
9cde0 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61  mem0.alarmCallba
9cdf0 63 6b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ck ){.      sqli
9ce00 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e  te3MallocAlarm(n
9ce10 42 79 74 65 73 29 3b 0a 20 20 20 20 20 20 70 4e  Bytes);.      pN
9ce20 65 77 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62  ew = sqlite3Glob
9ce30 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 65 61 6c  alConfig.m.xReal
9ce40 6c 6f 63 28 70 4f 6c 64 2c 20 6e 4e 65 77 29 3b  loc(pOld, nNew);
9ce50 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
9ce60 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 6e 4e 65  New ){.      nNe
9ce70 77 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  w = sqlite3Mallo
9ce80 63 53 69 7a 65 28 70 4e 65 77 29 3b 0a 20 20 20  cSize(pNew);.   
9ce90 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73     sqlite3Status
9cea0 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55  Add(SQLITE_STATU
9ceb0 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 6e  S_MEMORY_USED, n
9cec0 4e 65 77 2d 6e 4f 6c 64 29 3b 0a 20 20 20 20 7d  New-nOld);.    }
9ced0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
9cee0 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75  ex_leave(mem0.mu
9cef0 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  tex);.  }else{. 
9cf00 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65     pNew = sqlite
9cf10 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e  3GlobalConfig.m.
9cf20 78 52 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e  xRealloc(pOld, n
9cf30 4e 65 77 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  New);.  }.  retu
9cf40 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn pNew;.}../*.*
9cf50 2a 20 54 68 65 20 70 75 62 6c 69 63 20 69 6e 74  * The public int
9cf60 65 72 66 61 63 65 20 74 6f 20 73 71 6c 69 74 65  erface to sqlite
9cf70 33 52 65 61 6c 6c 6f 63 2e 20 20 4d 61 6b 65 20  3Realloc.  Make 
9cf80 73 75 72 65 20 74 68 61 74 20 74 68 65 20 6d 65  sure that the me
9cf90 6d 6f 72 79 0a 2a 2a 20 73 75 62 73 79 73 74 65  mory.** subsyste
9cfa0 6d 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64  m is initialized
9cfb0 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69   prior to invoki
9cfc0 6e 67 20 73 71 6c 69 74 65 52 65 61 6c 6c 6f 63  ng sqliteRealloc
9cfd0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
9cfe0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 65  void *sqlite3_re
9cff0 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 4f 6c 64  alloc(void *pOld
9d000 2c 20 69 6e 74 20 6e 29 7b 0a 23 69 66 6e 64 65  , int n){.#ifnde
9d010 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
9d020 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c  TOINIT.  if( sql
9d030 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
9d040 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65  ) ) return 0;.#e
9d050 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 73 71  ndif.  return sq
9d060 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 70 4f 6c  lite3Realloc(pOl
9d070 64 2c 20 6e 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  d, n);.}.../*.**
9d080 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 7a 65   Allocate and ze
9d090 72 6f 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 20 0a 53  ro memory..*/ .S
9d0a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
9d0b0 69 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  id *sqlite3Mallo
9d0c0 63 5a 65 72 6f 28 69 6e 74 20 6e 29 7b 0a 20 20  cZero(int n){.  
9d0d0 76 6f 69 64 20 2a 70 20 3d 20 73 71 6c 69 74 65  void *p = sqlite
9d0e0 33 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 69 66  3Malloc(n);.  if
9d0f0 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65  ( p ){.    memse
9d100 74 28 70 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a  t(p, 0, n);.  }.
9d110 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f    return p;.}../
9d120 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e  *.** Allocate an
9d130 64 20 7a 65 72 6f 20 6d 65 6d 6f 72 79 2e 20 20  d zero memory.  
9d140 49 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f  If the allocatio
9d150 6e 20 66 61 69 6c 73 2c 20 6d 61 6b 65 0a 2a 2a  n fails, make.**
9d160 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65   the mallocFaile
9d170 64 20 66 6c 61 67 20 69 6e 20 74 68 65 20 63 6f  d flag in the co
9d180 6e 6e 65 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72  nnection pointer
9d190 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
9d1a0 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
9d1b0 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 71  3DbMallocZero(sq
9d1c0 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6e  lite3 *db, int n
9d1d0 29 7b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 73  ){.  void *p = s
9d1e0 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
9d1f0 77 28 64 62 2c 20 6e 29 3b 0a 20 20 69 66 28 20  w(db, n);.  if( 
9d200 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28  p ){.    memset(
9d210 70 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20  p, 0, n);.  }.  
9d220 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a  return p;.}../*.
9d230 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20  ** Allocate and 
9d240 7a 65 72 6f 20 6d 65 6d 6f 72 79 2e 20 20 49 66  zero memory.  If
9d250 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20   the allocation 
9d260 66 61 69 6c 73 2c 20 6d 61 6b 65 0a 2a 2a 20 74  fails, make.** t
9d270 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  he mallocFailed 
9d280 66 6c 61 67 20 69 6e 20 74 68 65 20 63 6f 6e 6e  flag in the conn
9d290 65 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 2e 0a  ection pointer..
9d2a0 2a 2a 0a 2a 2a 20 49 66 20 64 62 21 3d 30 20 61  **.** If db!=0 a
9d2b0 6e 64 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  nd db->mallocFai
9d2c0 6c 65 64 20 69 73 20 74 72 75 65 20 28 69 6e 64  led is true (ind
9d2d0 69 63 61 74 69 6e 67 20 61 20 70 72 69 6f 72 20  icating a prior 
9d2e0 6d 61 6c 6c 6f 63 0a 2a 2a 20 66 61 69 6c 75 72  malloc.** failur
9d2f0 65 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 61  e on the same da
9d300 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
9d310 6e 29 20 74 68 65 6e 20 61 6c 77 61 79 73 20 72  n) then always r
9d320 65 74 75 72 6e 20 30 2e 0a 2a 2a 20 48 65 6e 63  eturn 0..** Henc
9d330 65 20 66 6f 72 20 61 20 70 61 72 74 69 63 75 6c  e for a particul
9d340 61 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ar database conn
9d350 65 63 74 69 6f 6e 2c 20 6f 6e 63 65 20 6d 61 6c  ection, once mal
9d360 6c 6f 63 20 73 74 61 72 74 73 0a 2a 2a 20 66 61  loc starts.** fa
9d370 69 6c 69 6e 67 2c 20 69 74 20 66 61 69 6c 73 20  iling, it fails 
9d380 63 6f 6e 73 69 73 74 65 6e 74 6c 79 20 75 6e 74  consistently unt
9d390 69 6c 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  il mallocFailed 
9d3a0 69 73 20 72 65 73 65 74 2e 0a 2a 2a 20 54 68 69  is reset..** Thi
9d3b0 73 20 69 73 20 61 6e 20 69 6d 70 6f 72 74 61 6e  s is an importan
9d3c0 74 20 61 73 73 75 6d 70 74 69 6f 6e 2e 20 20 54  t assumption.  T
9d3d0 68 65 72 65 20 61 72 65 20 6d 61 6e 79 20 70 6c  here are many pl
9d3e0 61 63 65 73 20 69 6e 20 74 68 65 0a 2a 2a 20 63  aces in the.** c
9d3f0 6f 64 65 20 74 68 61 74 20 64 6f 20 74 68 69 6e  ode that do thin
9d400 67 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a  gs like this:.**
9d410 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 74 20  .**         int 
9d420 2a 61 20 3d 20 28 69 6e 74 2a 29 73 71 6c 69 74  *a = (int*)sqlit
9d430 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
9d440 2c 20 31 30 30 29 3b 0a 2a 2a 20 20 20 20 20 20  , 100);.**      
9d450 20 20 20 69 6e 74 20 2a 62 20 3d 20 28 69 6e 74     int *b = (int
9d460 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  *)sqlite3DbMallo
9d470 63 52 61 77 28 64 62 2c 20 32 30 30 29 3b 0a 2a  cRaw(db, 200);.*
9d480 2a 20 20 20 20 20 20 20 20 20 69 66 28 20 62 20  *         if( b 
9d490 29 20 61 5b 31 30 5d 20 3d 20 39 3b 0a 2a 2a 0a  ) a[10] = 9;.**.
9d4a0 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  ** In other word
9d4b0 73 2c 20 69 66 20 61 20 73 75 62 73 65 71 75 65  s, if a subseque
9d4c0 6e 74 20 6d 61 6c 6c 6f 63 20 28 65 78 3a 20 22  nt malloc (ex: "
9d4d0 62 22 29 20 77 6f 72 6b 65 64 2c 20 69 74 20 69  b") worked, it i
9d4e0 73 20 61 73 73 75 6d 65 64 0a 2a 2a 20 74 68 61  s assumed.** tha
9d4f0 74 20 61 6c 6c 20 70 72 69 6f 72 20 6d 61 6c 6c  t all prior mall
9d500 6f 63 73 20 28 65 78 3a 20 22 61 22 29 20 77 6f  ocs (ex: "a") wo
9d510 72 6b 65 64 20 74 6f 6f 2e 0a 2a 2f 0a 53 51 4c  rked too..*/.SQL
9d520 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
9d530 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f   *sqlite3DbMallo
9d540 63 52 61 77 28 73 71 6c 69 74 65 33 20 2a 64 62  cRaw(sqlite3 *db
9d550 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64  , int n){.  void
9d560 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 64   *p;.  assert( d
9d570 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f  b==0 || sqlite3_
9d580 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d  mutex_held(db->m
9d590 75 74 65 78 29 20 29 3b 0a 23 69 66 6e 64 65 66  utex) );.#ifndef
9d5a0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 4f   SQLITE_OMIT_LOO
9d5b0 4b 41 53 49 44 45 0a 20 20 69 66 28 20 64 62 20  KASIDE.  if( db 
9d5c0 29 7b 0a 20 20 20 20 4c 6f 6f 6b 61 73 69 64 65  ){.    Lookaside
9d5d0 53 6c 6f 74 20 2a 70 42 75 66 3b 0a 20 20 20 20  Slot *pBuf;.    
9d5e0 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
9d5f0 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 72 65  iled ){.      re
9d600 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20  turn 0;.    }.  
9d610 20 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73    if( db->lookas
9d620 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 26 26 20  ide.bEnabled && 
9d630 6e 3c 3d 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65  n<=db->lookaside
9d640 2e 73 7a 0a 20 20 20 20 20 20 20 20 20 26 26 20  .sz.         && 
9d650 28 70 42 75 66 20 3d 20 64 62 2d 3e 6c 6f 6f 6b  (pBuf = db->look
9d660 61 73 69 64 65 2e 70 46 72 65 65 29 21 3d 30 20  aside.pFree)!=0 
9d670 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f  ){.      db->loo
9d680 6b 61 73 69 64 65 2e 70 46 72 65 65 20 3d 20 70  kaside.pFree = p
9d690 42 75 66 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  Buf->pNext;.    
9d6a0 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e    db->lookaside.
9d6b0 6e 4f 75 74 2b 2b 3b 0a 20 20 20 20 20 20 69 66  nOut++;.      if
9d6c0 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  ( db->lookaside.
9d6d0 6e 4f 75 74 3e 64 62 2d 3e 6c 6f 6f 6b 61 73 69  nOut>db->lookasi
9d6e0 64 65 2e 6d 78 4f 75 74 20 29 7b 0a 20 20 20 20  de.mxOut ){.    
9d6f0 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64      db->lookasid
9d700 65 2e 6d 78 4f 75 74 20 3d 20 64 62 2d 3e 6c 6f  e.mxOut = db->lo
9d710 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 20  okaside.nOut;.  
9d720 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75      }.      retu
9d730 72 6e 20 28 76 6f 69 64 2a 29 70 42 75 66 3b 0a  rn (void*)pBuf;.
9d740 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a      }.  }.#else.
9d750 20 20 69 66 28 20 64 62 20 26 26 20 64 62 2d 3e    if( db && db->
9d760 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
9d770 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
9d780 7d 0a 23 65 6e 64 69 66 0a 20 20 70 20 3d 20 73  }.#endif.  p = s
9d790 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b  qlite3Malloc(n);
9d7a0 0a 20 20 69 66 28 20 21 70 20 26 26 20 64 62 20  .  if( !p && db 
9d7b0 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  ){.    db->mallo
9d7c0 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d  cFailed = 1;.  }
9d7d0 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
9d7e0 2f 2a 0a 2a 2a 20 52 65 73 69 7a 65 20 74 68 65  /*.** Resize the
9d7f0 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79   block of memory
9d800 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70   pointed to by p
9d810 20 74 6f 20 6e 20 62 79 74 65 73 2e 20 49 66 20   to n bytes. If 
9d820 74 68 65 0a 2a 2a 20 72 65 73 69 7a 65 20 66 61  the.** resize fa
9d830 69 6c 73 2c 20 73 65 74 20 74 68 65 20 6d 61 6c  ils, set the mal
9d840 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 69  locFailed flag i
9d850 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  n the connection
9d860 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49   object..*/.SQLI
9d870 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
9d880 2a 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f  *sqlite3DbReallo
9d890 63 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76  c(sqlite3 *db, v
9d8a0 6f 69 64 20 2a 70 2c 20 69 6e 74 20 6e 29 7b 0a  oid *p, int n){.
9d8b0 20 20 76 6f 69 64 20 2a 70 4e 65 77 20 3d 20 30    void *pNew = 0
9d8c0 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 21 3d  ;.  assert( db!=
9d8d0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  0 );.  assert( s
9d8e0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
9d8f0 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  d(db->mutex) );.
9d900 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
9d910 46 61 69 6c 65 64 3d 3d 30 20 29 7b 0a 20 20 20  Failed==0 ){.   
9d920 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20   if( p==0 ){.   
9d930 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65     return sqlite
9d940 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
9d950 20 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69   n);.    }.    i
9d960 66 28 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 64  f( isLookaside(d
9d970 62 2c 20 70 29 20 29 7b 0a 20 20 20 20 20 20 69  b, p) ){.      i
9d980 66 28 20 6e 3c 3d 64 62 2d 3e 6c 6f 6f 6b 61 73  f( n<=db->lookas
9d990 69 64 65 2e 73 7a 20 29 7b 0a 20 20 20 20 20 20  ide.sz ){.      
9d9a0 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 20 20    return p;.    
9d9b0 20 20 7d 0a 20 20 20 20 20 20 70 4e 65 77 20 3d    }.      pNew =
9d9c0 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
9d9d0 52 61 77 28 64 62 2c 20 6e 29 3b 0a 20 20 20 20  Raw(db, n);.    
9d9e0 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20    if( pNew ){.  
9d9f0 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 4e 65        memcpy(pNe
9da00 77 2c 20 70 2c 20 64 62 2d 3e 6c 6f 6f 6b 61 73  w, p, db->lookas
9da10 69 64 65 2e 73 7a 29 3b 0a 20 20 20 20 20 20 20  ide.sz);.       
9da20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
9da30 62 2c 20 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20  b, p);.      }. 
9da40 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
9da50 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72  pNew = sqlite3_r
9da60 65 61 6c 6c 6f 63 28 70 2c 20 6e 29 3b 0a 20 20  ealloc(p, n);.  
9da70 20 20 20 20 69 66 28 20 21 70 4e 65 77 20 29 7b      if( !pNew ){
9da80 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c  .        db->mal
9da90 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
9daa0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
9dab0 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a  .  return pNew;.
9dac0 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74  }../*.** Attempt
9dad0 20 74 6f 20 72 65 61 6c 6c 6f 63 61 74 65 20 70   to reallocate p
9dae0 2e 20 20 49 66 20 74 68 65 20 72 65 61 6c 6c 6f  .  If the reallo
9daf0 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 74 68  cation fails, th
9db00 65 6e 20 66 72 65 65 20 70 0a 2a 2a 20 61 6e 64  en free p.** and
9db10 20 73 65 74 20 74 68 65 20 6d 61 6c 6c 6f 63 46   set the mallocF
9db20 61 69 6c 65 64 20 66 6c 61 67 20 69 6e 20 74 68  ailed flag in th
9db30 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
9db40 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ction..*/.SQLITE
9db50 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
9db60 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f  qlite3DbReallocO
9db70 72 46 72 65 65 28 73 71 6c 69 74 65 33 20 2a 64  rFree(sqlite3 *d
9db80 62 2c 20 76 6f 69 64 20 2a 70 2c 20 69 6e 74 20  b, void *p, int 
9db90 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4e 65 77  n){.  void *pNew
9dba0 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74  ;.  pNew = sqlit
9dbb0 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20  e3DbRealloc(db, 
9dbc0 70 2c 20 6e 29 3b 0a 20 20 69 66 28 20 21 70 4e  p, n);.  if( !pN
9dbd0 65 77 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ew ){.    sqlite
9dbe0 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a  3DbFree(db, p);.
9dbf0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65    }.  return pNe
9dc00 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65  w;.}../*.** Make
9dc10 20 61 20 63 6f 70 79 20 6f 66 20 61 20 73 74 72   a copy of a str
9dc20 69 6e 67 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62  ing in memory ob
9dc30 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69  tained from sqli
9dc40 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 54 68 65 73  teMalloc(). Thes
9dc50 65 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20  e .** functions 
9dc60 63 61 6c 6c 20 73 71 6c 69 74 65 33 4d 61 6c 6c  call sqlite3Mall
9dc70 6f 63 52 61 77 28 29 20 64 69 72 65 63 74 6c 79  ocRaw() directly
9dc80 20 69 6e 73 74 65 61 64 20 6f 66 20 73 71 6c 69   instead of sqli
9dc90 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 54 68 69 73  teMalloc(). This
9dca0 0a 2a 2a 20 69 73 20 62 65 63 61 75 73 65 20 77  .** is because w
9dcb0 68 65 6e 20 6d 65 6d 6f 72 79 20 64 65 62 75 67  hen memory debug
9dcc0 67 69 6e 67 20 69 73 20 74 75 72 6e 65 64 20 6f  ging is turned o
9dcd0 6e 2c 20 74 68 65 73 65 20 74 77 6f 20 66 75 6e  n, these two fun
9dce0 63 74 69 6f 6e 73 20 61 72 65 20 0a 2a 2a 20 63  ctions are .** c
9dcf0 61 6c 6c 65 64 20 76 69 61 20 6d 61 63 72 6f 73  alled via macros
9dd00 20 74 68 61 74 20 72 65 63 6f 72 64 20 74 68 65   that record the
9dd10 20 63 75 72 72 65 6e 74 20 66 69 6c 65 20 61 6e   current file an
9dd20 64 20 6c 69 6e 65 20 6e 75 6d 62 65 72 20 69 6e  d line number in
9dd30 20 74 68 65 0a 2a 2a 20 54 68 72 65 61 64 44 61   the.** ThreadDa
9dd40 74 61 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  ta structure..*/
9dd50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
9dd60 63 68 61 72 20 2a 73 71 6c 69 74 65 33 44 62 53  char *sqlite3DbS
9dd70 74 72 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64  trDup(sqlite3 *d
9dd80 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
9dd90 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b  ){.  char *zNew;
9dda0 0a 20 20 73 69 7a 65 5f 74 20 6e 3b 0a 20 20 69  .  size_t n;.  i
9ddb0 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 72  f( z==0 ){.    r
9ddc0 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e  eturn 0;.  }.  n
9ddd0 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
9dde0 33 30 28 7a 29 20 2b 20 31 3b 0a 20 20 61 73 73  30(z) + 1;.  ass
9ddf0 65 72 74 28 20 28 6e 26 30 78 37 66 66 66 66 66  ert( (n&0x7fffff
9de00 66 66 29 3d 3d 6e 20 29 3b 0a 20 20 7a 4e 65 77  ff)==n );.  zNew
9de10 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
9de20 6f 63 52 61 77 28 64 62 2c 20 28 69 6e 74 29 6e  ocRaw(db, (int)n
9de30 29 3b 0a 20 20 69 66 28 20 7a 4e 65 77 20 29 7b  );.  if( zNew ){
9de40 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 4e 65 77  .    memcpy(zNew
9de50 2c 20 7a 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 72  , z, n);.  }.  r
9de60 65 74 75 72 6e 20 7a 4e 65 77 3b 0a 7d 0a 53 51  eturn zNew;.}.SQ
9de70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
9de80 72 20 2a 73 71 6c 69 74 65 33 44 62 53 74 72 4e  r *sqlite3DbStrN
9de90 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  Dup(sqlite3 *db,
9dea0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20   const char *z, 
9deb0 69 6e 74 20 6e 29 7b 0a 20 20 63 68 61 72 20 2a  int n){.  char *
9dec0 7a 4e 65 77 3b 0a 20 20 69 66 28 20 7a 3d 3d 30  zNew;.  if( z==0
9ded0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
9dee0 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
9def0 28 6e 26 30 78 37 66 66 66 66 66 66 66 29 3d 3d  (n&0x7fffffff)==
9df00 6e 20 29 3b 0a 20 20 7a 4e 65 77 20 3d 20 73 71  n );.  zNew = sq
9df10 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
9df20 28 64 62 2c 20 6e 2b 31 29 3b 0a 20 20 69 66 28  (db, n+1);.  if(
9df30 20 7a 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d   zNew ){.    mem
9df40 63 70 79 28 7a 4e 65 77 2c 20 7a 2c 20 6e 29 3b  cpy(zNew, z, n);
9df50 0a 20 20 20 20 7a 4e 65 77 5b 6e 5d 20 3d 20 30  .    zNew[n] = 0
9df60 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a  ;.  }.  return z
9df70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72  New;.}../*.** Cr
9df80 65 61 74 65 20 61 20 73 74 72 69 6e 67 20 66 72  eate a string fr
9df90 6f 6d 20 74 68 65 20 7a 46 72 6f 6d 61 74 20 61  om the zFromat a
9dfa0 72 67 75 6d 65 6e 74 20 61 6e 64 20 74 68 65 20  rgument and the 
9dfb0 76 61 5f 6c 69 73 74 20 74 68 61 74 20 66 6f 6c  va_list that fol
9dfc0 6c 6f 77 73 2e 0a 2a 2a 20 53 74 6f 72 65 20 74  lows..** Store t
9dfd0 68 65 20 73 74 72 69 6e 67 20 69 6e 20 6d 65 6d  he string in mem
9dfe0 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ory obtained fro
9dff0 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29  m sqliteMalloc()
9e000 20 61 6e 64 20 6d 61 6b 65 20 2a 70 7a 0a 2a 2a   and make *pz.**
9e010 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 20 73   point to that s
9e020 74 72 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45  tring..*/.SQLITE
9e030 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
9e040 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 63  lite3SetString(c
9e050 68 61 72 20 2a 2a 70 7a 2c 20 73 71 6c 69 74 65  har **pz, sqlite
9e060 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61  3 *db, const cha
9e070 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29  r *zFormat, ...)
9e080 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a  {.  va_list ap;.
9e090 20 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 76 61    char *z;..  va
9e0a0 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d  _start(ap, zForm
9e0b0 61 74 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74  at);.  z = sqlit
9e0c0 65 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a  e3VMPrintf(db, z
9e0d0 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76  Format, ap);.  v
9e0e0 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 73 71 6c  a_end(ap);.  sql
9e0f0 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 2a  ite3DbFree(db, *
9e100 70 7a 29 3b 0a 20 20 2a 70 7a 20 3d 20 7a 3b 0a  pz);.  *pz = z;.
9e110 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  }.../*.** This f
9e120 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20  unction must be 
9e130 63 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 65 78  called before ex
9e140 69 74 69 6e 67 20 61 6e 79 20 41 50 49 20 66 75  iting any API fu
9e150 6e 63 74 69 6f 6e 20 28 69 2e 65 2e 20 0a 2a 2a  nction (i.e. .**
9e160 20 72 65 74 75 72 6e 69 6e 67 20 63 6f 6e 74 72   returning contr
9e170 6f 6c 20 74 6f 20 74 68 65 20 75 73 65 72 29 20  ol to the user) 
9e180 74 68 61 74 20 68 61 73 20 63 61 6c 6c 65 64 20  that has called 
9e190 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 6f  sqlite3_malloc o
9e1a0 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 61  r.** sqlite3_rea
9e1b0 6c 6c 6f 63 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  lloc..**.** The 
9e1c0 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69  returned value i
9e1d0 73 20 6e 6f 72 6d 61 6c 6c 79 20 61 20 63 6f 70  s normally a cop
9e1e0 79 20 6f 66 20 74 68 65 20 73 65 63 6f 6e 64 20  y of the second 
9e1f0 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73  argument to this
9e200 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 48 6f  .** function. Ho
9e210 77 65 76 65 72 2c 20 69 66 20 61 20 6d 61 6c 6c  wever, if a mall
9e220 6f 63 28 29 20 66 61 69 6c 75 72 65 20 68 61 73  oc() failure has
9e230 20 6f 63 63 75 72 72 65 64 20 73 69 6e 63 65 20   occurred since 
9e240 74 68 65 20 70 72 65 76 69 6f 75 73 0a 2a 2a 20  the previous.** 
9e250 69 6e 76 6f 63 61 74 69 6f 6e 20 53 51 4c 49 54  invocation SQLIT
9e260 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72  E_NOMEM is retur
9e270 6e 65 64 20 69 6e 73 74 65 61 64 2e 20 0a 2a 2a  ned instead. .**
9e280 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 72 73 74  .** If the first
9e290 20 61 72 67 75 6d 65 6e 74 2c 20 64 62 2c 20 69   argument, db, i
9e2a0 73 20 6e 6f 74 20 4e 55 4c 4c 20 61 6e 64 20 61  s not NULL and a
9e2b0 20 6d 61 6c 6c 6f 63 28 29 20 65 72 72 6f 72 20   malloc() error 
9e2c0 68 61 73 20 6f 63 63 75 72 72 65 64 2c 0a 2a 2a  has occurred,.**
9e2d0 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 6e 65 63   then the connec
9e2e0 74 69 6f 6e 20 65 72 72 6f 72 2d 63 6f 64 65 20  tion error-code 
9e2f0 28 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72  (the value retur
9e300 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 65  ned by sqlite3_e
9e310 72 72 63 6f 64 65 28 29 29 0a 2a 2a 20 69 73 20  rrcode()).** is 
9e320 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4e 4f  set to SQLITE_NO
9e330 4d 45 4d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  MEM..*/.SQLITE_P
9e340 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
9e350 65 33 41 70 69 45 78 69 74 28 73 71 6c 69 74 65  e3ApiExit(sqlite
9e360 33 2a 20 64 62 2c 20 69 6e 74 20 72 63 29 7b 0a  3* db, int rc){.
9e370 20 20 2f 2a 20 49 66 20 74 68 65 20 64 62 20 68    /* If the db h
9e380 61 6e 64 6c 65 20 69 73 20 6e 6f 74 20 4e 55 4c  andle is not NUL
9e390 4c 2c 20 74 68 65 6e 20 77 65 20 6d 75 73 74 20  L, then we must 
9e3a0 68 6f 6c 64 20 74 68 65 20 63 6f 6e 6e 65 63 74  hold the connect
9e3b0 69 6f 6e 20 68 61 6e 64 6c 65 0a 20 20 2a 2a 20  ion handle.  ** 
9e3c0 6d 75 74 65 78 20 68 65 72 65 2e 20 4f 74 68 65  mutex here. Othe
9e3d0 72 77 69 73 65 20 74 68 65 20 72 65 61 64 20 28  rwise the read (
9e3e0 61 6e 64 20 70 6f 73 73 69 62 6c 65 20 77 72 69  and possible wri
9e3f0 74 65 29 20 6f 66 20 64 62 2d 3e 6d 61 6c 6c 6f  te) of db->mallo
9e400 63 46 61 69 6c 65 64 20 0a 20 20 2a 2a 20 69 73  cFailed .  ** is
9e410 20 75 6e 73 61 66 65 2c 20 61 73 20 69 73 20 74   unsafe, as is t
9e420 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  he call to sqlit
9e430 65 33 45 72 72 6f 72 28 29 2e 0a 20 20 2a 2f 0a  e3Error()..  */.
9e440 20 20 61 73 73 65 72 74 28 20 21 64 62 20 7c 7c    assert( !db ||
9e450 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
9e460 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29  eld(db->mutex) )
9e470 3b 0a 20 20 69 66 28 20 64 62 20 26 26 20 28 64  ;.  if( db && (d
9e480 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
9e490 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f  || rc==SQLITE_IO
9e4a0 45 52 52 5f 4e 4f 4d 45 4d 29 20 29 7b 0a 20 20  ERR_NOMEM) ){.  
9e4b0 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
9e4c0 62 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c  b, SQLITE_NOMEM,
9e4d0 20 30 29 3b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c   0);.    db->mal
9e4e0 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20  locFailed = 0;. 
9e4f0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
9e500 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 72 65 74 75  OMEM;.  }.  retu
9e510 72 6e 20 72 63 20 26 20 28 64 62 20 3f 20 64 62  rn rc & (db ? db
9e520 2d 3e 65 72 72 4d 61 73 6b 20 3a 20 30 78 66 66  ->errMask : 0xff
9e530 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  );.}../*********
9e540 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 61 6c  ***** End of mal
9e550 6c 6f 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  loc.c **********
9e560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e580 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
9e590 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
9e5a0 20 70 72 69 6e 74 66 2e 63 20 2a 2a 2a 2a 2a 2a   printf.c ******
9e5b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e5c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e5d0 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ****/./*.** The 
9e5e0 22 70 72 69 6e 74 66 22 20 63 6f 64 65 20 74 68  "printf" code th
9e5f0 61 74 20 66 6f 6c 6c 6f 77 73 20 64 61 74 65 73  at follows dates
9e600 20 66 72 6f 6d 20 74 68 65 20 31 39 38 30 27 73   from the 1980's
9e610 2e 20 20 49 74 20 69 73 20 69 6e 0a 2a 2a 20 74  .  It is in.** t
9e620 68 65 20 70 75 62 6c 69 63 20 64 6f 6d 61 69 6e  he public domain
9e630 2e 20 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20  .  The original 
9e640 63 6f 6d 6d 65 6e 74 73 20 61 72 65 20 69 6e 63  comments are inc
9e650 6c 75 64 65 64 20 68 65 72 65 20 66 6f 72 0a 2a  luded here for.*
9e660 2a 20 63 6f 6d 70 6c 65 74 65 6e 65 73 73 2e 20  * completeness. 
9e670 20 54 68 65 79 20 61 72 65 20 76 65 72 79 20 6f   They are very o
9e680 75 74 2d 6f 66 2d 64 61 74 65 20 62 75 74 20 6d  ut-of-date but m
9e690 69 67 68 74 20 62 65 20 75 73 65 66 75 6c 20 61  ight be useful a
9e6a0 73 0a 2a 2a 20 61 6e 20 68 69 73 74 6f 72 69 63  s.** an historic
9e6b0 61 6c 20 72 65 66 65 72 65 6e 63 65 2e 20 20 4d  al reference.  M
9e6c0 6f 73 74 20 6f 66 20 74 68 65 20 22 65 6e 68 61  ost of the "enha
9e6d0 6e 63 65 6d 65 6e 74 73 22 20 68 61 76 65 20 62  ncements" have b
9e6e0 65 65 6e 20 62 61 63 6b 65 64 0a 2a 2a 20 6f 75  een backed.** ou
9e6f0 74 20 73 6f 20 74 68 61 74 20 74 68 65 20 66 75  t so that the fu
9e700 6e 63 74 69 6f 6e 61 6c 69 74 79 20 69 73 20 6e  nctionality is n
9e710 6f 77 20 74 68 65 20 73 61 6d 65 20 61 73 20 73  ow the same as s
9e720 74 61 6e 64 61 72 64 20 70 72 69 6e 74 66 28 29  tandard printf()
9e730 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 70 72 69  ..**.** $Id: pri
9e740 6e 74 66 2e 63 2c 76 20 31 2e 31 30 34 20 32 30  ntf.c,v 1.104 20
9e750 30 39 2f 30 36 2f 30 33 20 30 31 3a 32 34 3a 35  09/06/03 01:24:5
9e760 34 20 64 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a  4 drh Exp $.**.*
9e770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e7a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9e7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
9e7c0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 6f  The following mo
9e7d0 64 75 6c 65 73 20 69 73 20 61 6e 20 65 6e 68 61  dules is an enha
9e7e0 6e 63 65 64 20 72 65 70 6c 61 63 65 6d 65 6e 74  nced replacement
9e7f0 20 66 6f 72 20 74 68 65 20 22 70 72 69 6e 74 66   for the "printf
9e800 22 20 73 75 62 72 6f 75 74 69 6e 65 73 0a 2a 2a  " subroutines.**
9e810 20 66 6f 75 6e 64 20 69 6e 20 74 68 65 20 73 74   found in the st
9e820 61 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72 79  andard C library
9e830 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  .  The following
9e840 20 65 6e 68 61 6e 63 65 6d 65 6e 74 73 20 61 72   enhancements ar
9e850 65 0a 2a 2a 20 73 75 70 70 6f 72 74 65 64 3a 0a  e.** supported:.
9e860 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20 41 64  **.**      +  Ad
9e870 64 69 74 69 6f 6e 61 6c 20 66 75 6e 63 74 69 6f  ditional functio
9e880 6e 73 2e 20 20 54 68 65 20 73 74 61 6e 64 61 72  ns.  The standar
9e890 64 20 73 65 74 20 6f 66 20 22 70 72 69 6e 74 66  d set of "printf
9e8a0 22 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 20  " functions.**  
9e8b0 20 20 20 20 20 20 20 69 6e 63 6c 75 64 65 73 20         includes 
9e8c0 70 72 69 6e 74 66 2c 20 66 70 72 69 6e 74 66 2c  printf, fprintf,
9e8d0 20 73 70 72 69 6e 74 66 2c 20 76 70 72 69 6e 74   sprintf, vprint
9e8e0 66 2c 20 76 66 70 72 69 6e 74 66 2c 20 61 6e 64  f, vfprintf, and
9e8f0 0a 2a 2a 20 20 20 20 20 20 20 20 20 76 73 70 72  .**         vspr
9e900 69 6e 74 66 2e 20 20 54 68 69 73 20 6d 6f 64 75  intf.  This modu
9e910 6c 65 20 61 64 64 73 20 74 68 65 20 66 6f 6c 6c  le adds the foll
9e920 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  owing:.**.**    
9e930 20 20 20 20 20 20 20 2a 20 20 73 6e 70 72 69 6e         *  snprin
9e940 74 66 20 2d 2d 20 57 6f 72 6b 73 20 6c 69 6b 65  tf -- Works like
9e950 20 73 70 72 69 6e 74 66 2c 20 62 75 74 20 68 61   sprintf, but ha
9e960 73 20 61 6e 20 65 78 74 72 61 20 61 72 67 75 6d  s an extra argum
9e970 65 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ent.**          
9e980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9e990 77 68 69 63 68 20 69 73 20 74 68 65 20 73 69 7a  which is the siz
9e9a0 65 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 20  e of the buffer 
9e9b0 77 72 69 74 74 65 6e 20 74 6f 2e 0a 2a 2a 0a 2a  written to..**.*
9e9c0 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 6d  *           *  m
9e9d0 70 72 69 6e 74 66 20 2d 2d 20 20 53 69 6d 69 6c  printf --  Simil
9e9e0 61 72 20 74 6f 20 73 70 72 69 6e 74 66 2e 20 20  ar to sprintf.  
9e9f0 57 72 69 74 65 73 20 6f 75 74 70 75 74 20 74 6f  Writes output to
9ea00 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 20 20 20   memory.**      
9ea10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9ea20 20 20 20 20 6f 62 74 61 69 6e 65 64 20 66 72 6f      obtained fro
9ea30 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a 0a 2a 2a 20  m malloc..**.** 
9ea40 20 20 20 20 20 20 20 20 20 20 2a 20 20 78 70 72            *  xpr
9ea50 69 6e 74 66 20 2d 2d 20 20 43 61 6c 6c 73 20 61  intf --  Calls a
9ea60 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 64 69 73   function to dis
9ea70 70 6f 73 65 20 6f 66 20 6f 75 74 70 75 74 2e 0a  pose of output..
9ea80 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  **.**           
9ea90 2a 20 20 6e 70 72 69 6e 74 66 20 2d 2d 20 20 4e  *  nprintf --  N
9eaa0 6f 20 6f 75 74 70 75 74 2c 20 62 75 74 20 72 65  o output, but re
9eab0 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72  turns the number
9eac0 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 0a 2a   of characters.*
9ead0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
9eae0 20 20 20 20 20 20 20 20 20 20 20 74 68 61 74 20             that 
9eaf0 77 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e 20  would have been 
9eb00 6f 75 74 70 75 74 20 62 79 20 70 72 69 6e 74 66  output by printf
9eb10 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20  ..**.**         
9eb20 20 20 2a 20 20 41 20 76 2d 20 76 65 72 73 69 6f    *  A v- versio
9eb30 6e 20 28 65 78 3a 20 76 73 6e 70 72 69 6e 74 66  n (ex: vsnprintf
9eb40 29 20 6f 66 20 65 76 65 72 79 20 66 75 6e 63 74  ) of every funct
9eb50 69 6f 6e 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 20  ion is also.**  
9eb60 20 20 20 20 20 20 20 20 20 20 20 20 73 75 70 70              supp
9eb70 6c 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  lied..**.**     
9eb80 20 2b 20 20 41 20 66 65 77 20 65 78 74 65 6e 73   +  A few extens
9eb90 69 6f 6e 73 20 74 6f 20 74 68 65 20 66 6f 72 6d  ions to the form
9eba0 61 74 74 69 6e 67 20 6e 6f 74 61 74 69 6f 6e 20  atting notation 
9ebb0 61 72 65 20 73 75 70 70 6f 72 74 65 64 3a 0a 2a  are supported:.*
9ebc0 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a  *.**           *
9ebd0 20 20 54 68 65 20 22 3d 22 20 66 6c 61 67 20 28    The "=" flag (
9ebe0 73 69 6d 69 6c 61 72 20 74 6f 20 22 2d 22 29 20  similar to "-") 
9ebf0 63 61 75 73 65 73 20 74 68 65 20 6f 75 74 70 75  causes the outpu
9ec00 74 20 74 6f 20 62 65 0a 2a 2a 20 20 20 20 20 20  t to be.**      
9ec10 20 20 20 20 20 20 20 20 62 65 20 63 65 6e 74 65          be cente
9ec20 72 65 64 20 69 6e 20 74 68 65 20 61 70 70 72 6f  red in the appro
9ec30 70 72 69 61 74 65 6c 79 20 73 69 7a 65 64 20 66  priately sized f
9ec40 69 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ield..**.**     
9ec50 20 20 20 20 20 20 2a 20 20 54 68 65 20 25 62 20        *  The %b 
9ec60 66 69 65 6c 64 20 6f 75 74 70 75 74 73 20 61 6e  field outputs an
9ec70 20 69 6e 74 65 67 65 72 20 69 6e 20 62 69 6e 61   integer in bina
9ec80 72 79 20 6e 6f 74 61 74 69 6f 6e 2e 0a 2a 2a 0a  ry notation..**.
9ec90 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20  **           *  
9eca0 54 68 65 20 25 63 20 66 69 65 6c 64 20 6e 6f 77  The %c field now
9ecb0 20 61 63 63 65 70 74 73 20 61 20 70 72 65 63 69   accepts a preci
9ecc0 73 69 6f 6e 2e 20 20 54 68 65 20 63 68 61 72 61  sion.  The chara
9ecd0 63 74 65 72 20 6f 75 74 70 75 74 0a 2a 2a 20 20  cter output.**  
9ece0 20 20 20 20 20 20 20 20 20 20 20 20 69 73 20 72              is r
9ecf0 65 70 65 61 74 65 64 20 62 79 20 74 68 65 20 6e  epeated by the n
9ed00 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74  umber of times t
9ed10 68 65 20 70 72 65 63 69 73 69 6f 6e 20 73 70 65  he precision spe
9ed20 63 69 66 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20  cifies..**.**   
9ed30 20 20 20 20 20 20 20 20 2a 20 20 54 68 65 20 25          *  The %
9ed40 27 20 66 69 65 6c 64 20 77 6f 72 6b 73 20 6c 69  ' field works li
9ed50 6b 65 20 25 63 2c 20 62 75 74 20 74 61 6b 65 73  ke %c, but takes
9ed60 20 61 73 20 69 74 73 20 63 68 61 72 61 63 74 65   as its characte
9ed70 72 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  r the.**        
9ed80 20 20 20 20 20 20 6e 65 78 74 20 63 68 61 72 61        next chara
9ed90 63 74 65 72 20 6f 66 20 74 68 65 20 66 6f 72 6d  cter of the form
9eda0 61 74 20 73 74 72 69 6e 67 2c 20 69 6e 73 74 65  at string, inste
9edb0 61 64 20 6f 66 20 74 68 65 20 6e 65 78 74 0a 2a  ad of the next.*
9edc0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61  *              a
9edd0 72 67 75 6d 65 6e 74 2e 20 20 46 6f 72 20 65 78  rgument.  For ex
9ede0 61 6d 70 6c 65 2c 20 20 70 72 69 6e 74 66 28 22  ample,  printf("
9edf0 25 2e 37 38 27 2d 22 29 20 20 70 72 69 6e 74 73  %.78'-")  prints
9ee00 20 37 38 20 6d 69 6e 75 73 0a 2a 2a 20 20 20 20   78 minus.**    
9ee10 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 73 2c            signs,
9ee20 20 74 68 65 20 73 61 6d 65 20 61 73 20 20 70 72   the same as  pr
9ee30 69 6e 74 66 28 22 25 2e 37 38 63 22 2c 27 2d 27  intf("%.78c",'-'
9ee40 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20  )..**.**      + 
9ee50 20 57 68 65 6e 20 63 6f 6d 70 69 6c 65 64 20 75   When compiled u
9ee60 73 69 6e 67 20 47 43 43 20 6f 6e 20 61 20 53 50  sing GCC on a SP
9ee70 41 52 43 2c 20 74 68 69 73 20 76 65 72 73 69 6f  ARC, this versio
9ee80 6e 20 6f 66 20 70 72 69 6e 74 66 20 69 73 0a 2a  n of printf is.*
9ee90 2a 20 20 20 20 20 20 20 20 20 66 61 73 74 65 72  *         faster
9eea0 20 74 68 61 6e 20 74 68 65 20 6c 69 62 72 61 72   than the librar
9eeb0 79 20 70 72 69 6e 74 66 20 66 6f 72 20 53 55 4e  y printf for SUN
9eec0 20 4f 53 20 34 2e 31 2e 0a 2a 2a 0a 2a 2a 20 20   OS 4.1..**.**  
9eed0 20 20 20 20 2b 20 20 41 6c 6c 20 66 75 6e 63 74      +  All funct
9eee0 69 6f 6e 73 20 61 72 65 20 66 75 6c 6c 79 20 72  ions are fully r
9eef0 65 65 6e 74 72 61 6e 74 2e 0a 2a 2a 0a 2a 2f 0a  eentrant..**.*/.
9ef00 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 73 69 6f  ./*.** Conversio
9ef10 6e 20 74 79 70 65 73 20 66 61 6c 6c 20 69 6e 74  n types fall int
9ef20 6f 20 76 61 72 69 6f 75 73 20 63 61 74 65 67 6f  o various catego
9ef30 72 69 65 73 20 61 73 20 64 65 66 69 6e 65 64 20  ries as defined 
9ef40 62 79 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77  by the.** follow
9ef50 69 6e 67 20 65 6e 75 6d 65 72 61 74 69 6f 6e 2e  ing enumeration.
9ef60 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 52 41  .*/.#define etRA
9ef70 44 49 58 20 20 20 20 20 20 20 31 20 2f 2a 20 49  DIX       1 /* I
9ef80 6e 74 65 67 65 72 20 74 79 70 65 73 2e 20 20 25  nteger types.  %
9ef90 64 2c 20 25 78 2c 20 25 6f 2c 20 61 6e 64 20 73  d, %x, %o, and s
9efa0 6f 20 66 6f 72 74 68 20 2a 2f 0a 23 64 65 66 69  o forth */.#defi
9efb0 6e 65 20 65 74 46 4c 4f 41 54 20 20 20 20 20 20  ne etFLOAT      
9efc0 20 32 20 2f 2a 20 46 6c 6f 61 74 69 6e 67 20 70   2 /* Floating p
9efd0 6f 69 6e 74 2e 20 20 25 66 20 2a 2f 0a 23 64 65  oint.  %f */.#de
9efe0 66 69 6e 65 20 65 74 45 58 50 20 20 20 20 20 20  fine etEXP      
9eff0 20 20 20 33 20 2f 2a 20 45 78 70 6f 6e 65 6e 74     3 /* Exponent
9f000 69 6f 6e 61 6c 20 6e 6f 74 61 74 69 6f 6e 2e 20  ional notation. 
9f010 25 65 20 61 6e 64 20 25 45 20 2a 2f 0a 23 64 65  %e and %E */.#de
9f020 66 69 6e 65 20 65 74 47 45 4e 45 52 49 43 20 20  fine etGENERIC  
9f030 20 20 20 34 20 2f 2a 20 46 6c 6f 61 74 69 6e 67     4 /* Floating
9f040 20 6f 72 20 65 78 70 6f 6e 65 6e 74 69 61 6c 2c   or exponential,
9f050 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 65 78   depending on ex
9f060 70 6f 6e 65 6e 74 2e 20 25 67 20 2a 2f 0a 23 64  ponent. %g */.#d
9f070 65 66 69 6e 65 20 65 74 53 49 5a 45 20 20 20 20  efine etSIZE    
9f080 20 20 20 20 35 20 2f 2a 20 52 65 74 75 72 6e 20      5 /* Return 
9f090 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63  number of charac
9f0a0 74 65 72 73 20 70 72 6f 63 65 73 73 65 64 20 73  ters processed s
9f0b0 6f 20 66 61 72 2e 20 25 6e 20 2a 2f 0a 23 64 65  o far. %n */.#de
9f0c0 66 69 6e 65 20 65 74 53 54 52 49 4e 47 20 20 20  fine etSTRING   
9f0d0 20 20 20 36 20 2f 2a 20 53 74 72 69 6e 67 73 2e     6 /* Strings.
9f0e0 20 25 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65   %s */.#define e
9f0f0 74 44 59 4e 53 54 52 49 4e 47 20 20 20 37 20 2f  tDYNSTRING   7 /
9f100 2a 20 44 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c  * Dynamically al
9f110 6c 6f 63 61 74 65 64 20 73 74 72 69 6e 67 73 2e  located strings.
9f120 20 25 7a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65   %z */.#define e
9f130 74 50 45 52 43 45 4e 54 20 20 20 20 20 38 20 2f  tPERCENT     8 /
9f140 2a 20 50 65 72 63 65 6e 74 20 73 79 6d 62 6f 6c  * Percent symbol
9f150 2e 20 25 25 20 2a 2f 0a 23 64 65 66 69 6e 65 20  . %% */.#define 
9f160 65 74 43 48 41 52 58 20 20 20 20 20 20 20 39 20  etCHARX       9 
9f170 2f 2a 20 43 68 61 72 61 63 74 65 72 73 2e 20 25  /* Characters. %
9f180 63 20 2a 2f 0a 2f 2a 20 54 68 65 20 72 65 73 74  c */./* The rest
9f190 20 61 72 65 20 65 78 74 65 6e 73 69 6f 6e 73 2c   are extensions,
9f1a0 20 6e 6f 74 20 6e 6f 72 6d 61 6c 6c 79 20 66 6f   not normally fo
9f1b0 75 6e 64 20 69 6e 20 70 72 69 6e 74 66 28 29 20  und in printf() 
9f1c0 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 51 4c  */.#define etSQL
9f1d0 45 53 43 41 50 45 20 20 31 30 20 2f 2a 20 53 74  ESCAPE  10 /* St
9f1e0 72 69 6e 67 73 20 77 69 74 68 20 27 5c 27 27 20  rings with '\'' 
9f1f0 64 6f 75 62 6c 65 64 2e 20 20 25 71 20 2a 2f 0a  doubled.  %q */.
9f200 23 64 65 66 69 6e 65 20 65 74 53 51 4c 45 53 43  #define etSQLESC
9f210 41 50 45 32 20 31 31 20 2f 2a 20 53 74 72 69 6e  APE2 11 /* Strin
9f220 67 73 20 77 69 74 68 20 27 5c 27 27 20 64 6f 75  gs with '\'' dou
9f230 62 6c 65 64 20 61 6e 64 20 65 6e 63 6c 6f 73 65  bled and enclose
9f240 64 20 69 6e 20 27 27 2c 0a 20 20 20 20 20 20 20  d in '',.       
9f250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9f260 20 20 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73     NULL pointers
9f270 20 72 65 70 6c 61 63 65 64 20 62 79 20 53 51 4c   replaced by SQL
9f280 20 4e 55 4c 4c 2e 20 20 25 51 20 2a 2f 0a 23 64   NULL.  %Q */.#d
9f290 65 66 69 6e 65 20 65 74 54 4f 4b 45 4e 20 20 20  efine etTOKEN   
9f2a0 20 20 20 31 32 20 2f 2a 20 61 20 70 6f 69 6e 74     12 /* a point
9f2b0 65 72 20 74 6f 20 61 20 54 6f 6b 65 6e 20 73 74  er to a Token st
9f2c0 72 75 63 74 75 72 65 20 2a 2f 0a 23 64 65 66 69  ructure */.#defi
9f2d0 6e 65 20 65 74 53 52 43 4c 49 53 54 20 20 20 20  ne etSRCLIST    
9f2e0 31 33 20 2f 2a 20 61 20 70 6f 69 6e 74 65 72 20  13 /* a pointer 
9f2f0 74 6f 20 61 20 53 72 63 4c 69 73 74 20 2a 2f 0a  to a SrcList */.
9f300 23 64 65 66 69 6e 65 20 65 74 50 4f 49 4e 54 45  #define etPOINTE
9f310 52 20 20 20 20 31 34 20 2f 2a 20 54 68 65 20 25  R    14 /* The %
9f320 70 20 63 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f 0a  p conversion */.
9f330 23 64 65 66 69 6e 65 20 65 74 53 51 4c 45 53 43  #define etSQLESC
9f340 41 50 45 33 20 31 35 20 2f 2a 20 25 77 20 2d 3e  APE3 15 /* %w ->
9f350 20 53 74 72 69 6e 67 73 20 77 69 74 68 20 27 5c   Strings with '\
9f360 22 27 20 64 6f 75 62 6c 65 64 20 2a 2f 0a 23 64  "' doubled */.#d
9f370 65 66 69 6e 65 20 65 74 4f 52 44 49 4e 41 4c 20  efine etORDINAL 
9f380 20 20 20 31 36 20 2f 2a 20 25 72 20 2d 3e 20 31     16 /* %r -> 1
9f390 73 74 2c 20 32 6e 64 2c 20 33 72 64 2c 20 34 74  st, 2nd, 3rd, 4t
9f3a0 68 2c 20 65 74 63 2e 20 20 45 6e 67 6c 69 73 68  h, etc.  English
9f3b0 20 6f 6e 6c 79 20 2a 2f 0a 0a 23 64 65 66 69 6e   only */..#defin
9f3c0 65 20 65 74 49 4e 56 41 4c 49 44 20 20 20 20 20  e etINVALID     
9f3d0 30 20 2f 2a 20 41 6e 79 20 75 6e 72 65 63 6f 67  0 /* Any unrecog
9f3e0 6e 69 7a 65 64 20 63 6f 6e 76 65 72 73 69 6f 6e  nized conversion
9f3f0 20 74 79 70 65 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a   type */.../*.**
9f400 20 41 6e 20 22 65 74 42 79 74 65 22 20 69 73 20   An "etByte" is 
9f410 61 6e 20 38 2d 62 69 74 20 75 6e 73 69 67 6e 65  an 8-bit unsigne
9f420 64 20 76 61 6c 75 65 2e 0a 2a 2f 0a 74 79 70 65  d value..*/.type
9f430 64 65 66 20 75 6e 73 69 67 6e 65 64 20 63 68 61  def unsigned cha
9f440 72 20 65 74 42 79 74 65 3b 0a 0a 2f 2a 0a 2a 2a  r etByte;../*.**
9f450 20 45 61 63 68 20 62 75 69 6c 74 69 6e 20 63 6f   Each builtin co
9f460 6e 76 65 72 73 69 6f 6e 20 63 68 61 72 61 63 74  nversion charact
9f470 65 72 20 28 65 78 3a 20 74 68 65 20 27 64 27 20  er (ex: the 'd' 
9f480 69 6e 20 22 25 64 22 29 20 69 73 20 64 65 73 63  in "%d") is desc
9f490 72 69 62 65 64 0a 2a 2a 20 62 79 20 61 6e 20 69  ribed.** by an i
9f4a0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
9f4b0 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
9f4c0 72 65 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  re.*/.typedef st
9f4d0 72 75 63 74 20 65 74 5f 69 6e 66 6f 20 7b 20 20  ruct et_info {  
9f4e0 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20   /* Information 
9f4f0 61 62 6f 75 74 20 65 61 63 68 20 66 6f 72 6d 61  about each forma
9f500 74 20 66 69 65 6c 64 20 2a 2f 0a 20 20 63 68 61  t field */.  cha
9f510 72 20 66 6d 74 74 79 70 65 3b 20 20 20 20 20 20  r fmttype;      
9f520 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 72        /* The for
9f530 6d 61 74 20 66 69 65 6c 64 20 63 6f 64 65 20 6c  mat field code l
9f540 65 74 74 65 72 20 2a 2f 0a 20 20 65 74 42 79 74  etter */.  etByt
9f550 65 20 62 61 73 65 3b 20 20 20 20 20 20 20 20 20  e base;         
9f560 20 20 20 20 2f 2a 20 54 68 65 20 62 61 73 65 20      /* The base 
9f570 66 6f 72 20 72 61 64 69 78 20 63 6f 6e 76 65 72  for radix conver
9f580 73 69 6f 6e 20 2a 2f 0a 20 20 65 74 42 79 74 65  sion */.  etByte
9f590 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20   flags;         
9f5a0 20 20 20 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72     /* One or mor
9f5b0 65 20 6f 66 20 46 4c 41 47 5f 20 63 6f 6e 73 74  e of FLAG_ const
9f5c0 61 6e 74 73 20 62 65 6c 6f 77 20 2a 2f 0a 20 20  ants below */.  
9f5d0 65 74 42 79 74 65 20 74 79 70 65 3b 20 20 20 20  etByte type;    
9f5e0 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 76           /* Conv
9f5f0 65 72 73 69 6f 6e 20 70 61 72 61 64 69 67 6d 20  ersion paradigm 
9f600 2a 2f 0a 20 20 65 74 42 79 74 65 20 63 68 61 72  */.  etByte char
9f610 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  set;          /*
9f620 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 61 44 69   Offset into aDi
9f630 67 69 74 73 5b 5d 20 6f 66 20 74 68 65 20 64 69  gits[] of the di
9f640 67 69 74 73 20 73 74 72 69 6e 67 20 2a 2f 0a 20  gits string */. 
9f650 20 65 74 42 79 74 65 20 70 72 65 66 69 78 3b 20   etByte prefix; 
9f660 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66            /* Off
9f670 73 65 74 20 69 6e 74 6f 20 61 50 72 65 66 69 78  set into aPrefix
9f680 5b 5d 20 6f 66 20 74 68 65 20 70 72 65 66 69 78  [] of the prefix
9f690 20 73 74 72 69 6e 67 20 2a 2f 0a 7d 20 65 74 5f   string */.} et_
9f6a0 69 6e 66 6f 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c  info;../*.** All
9f6b0 6f 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20  owed values for 
9f6c0 65 74 5f 69 6e 66 6f 2e 66 6c 61 67 73 0a 2a 2f  et_info.flags.*/
9f6d0 0a 23 64 65 66 69 6e 65 20 46 4c 41 47 5f 53 49  .#define FLAG_SI
9f6e0 47 4e 45 44 20 20 31 20 20 20 20 20 2f 2a 20 54  GNED  1     /* T
9f6f0 72 75 65 20 69 66 20 74 68 65 20 76 61 6c 75 65  rue if the value
9f700 20 74 6f 20 63 6f 6e 76 65 72 74 20 69 73 20 73   to convert is s
9f710 69 67 6e 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65  igned */.#define
9f720 20 46 4c 41 47 5f 49 4e 54 45 52 4e 20 20 32 20   FLAG_INTERN  2 
9f730 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 66      /* True if f
9f740 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 20  or internal use 
9f750 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20  only */.#define 
9f760 46 4c 41 47 5f 53 54 52 49 4e 47 20 20 34 20 20  FLAG_STRING  4  
9f770 20 20 20 2f 2a 20 41 6c 6c 6f 77 20 69 6e 66 69     /* Allow infi
9f780 6e 69 74 79 20 70 72 65 63 69 73 69 6f 6e 20 2a  nity precision *
9f790 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  /.../*.** The fo
9f7a0 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 69 73  llowing table is
9f7b0 20 73 65 61 72 63 68 65 64 20 6c 69 6e 65 61 72   searched linear
9f7c0 6c 79 2c 20 73 6f 20 69 74 20 69 73 20 67 6f 6f  ly, so it is goo
9f7d0 64 20 74 6f 20 70 75 74 20 74 68 65 0a 2a 2a 20  d to put the.** 
9f7e0 6d 6f 73 74 20 66 72 65 71 75 65 6e 74 6c 79 20  most frequently 
9f7f0 75 73 65 64 20 63 6f 6e 76 65 72 73 69 6f 6e 20  used conversion 
9f800 74 79 70 65 73 20 66 69 72 73 74 2e 0a 2a 2f 0a  types first..*/.
9f810 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
9f820 72 20 61 44 69 67 69 74 73 5b 5d 20 3d 20 22 30  r aDigits[] = "0
9f830 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46 30  123456789ABCDEF0
9f840 31 32 33 34 35 36 37 38 39 61 62 63 64 65 66 22  123456789abcdef"
9f850 3b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  ;.static const c
9f860 68 61 72 20 61 50 72 65 66 69 78 5b 5d 20 3d 20  har aPrefix[] = 
9f870 22 2d 78 30 5c 30 30 30 58 30 22 3b 0a 73 74 61  "-x0\000X0";.sta
9f880 74 69 63 20 63 6f 6e 73 74 20 65 74 5f 69 6e 66  tic const et_inf
9f890 6f 20 66 6d 74 69 6e 66 6f 5b 5d 20 3d 20 7b 0a  o fmtinfo[] = {.
9f8a0 20 20 7b 20 20 27 64 27 2c 20 31 30 2c 20 31 2c    {  'd', 10, 1,
9f8b0 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 30   etRADIX,      0
9f8c0 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 73 27  ,  0 },.  {  's'
9f8d0 2c 20 20 30 2c 20 34 2c 20 65 74 53 54 52 49 4e  ,  0, 4, etSTRIN
9f8e0 47 2c 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a  G,     0,  0 },.
9f8f0 20 20 7b 20 20 27 67 27 2c 20 20 30 2c 20 31 2c    {  'g',  0, 1,
9f900 20 65 74 47 45 4e 45 52 49 43 2c 20 20 20 20 33   etGENERIC,    3
9f910 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 20 27 7a 27  0, 0 },.  {  'z'
9f920 2c 20 20 30 2c 20 34 2c 20 65 74 44 59 4e 53 54  ,  0, 4, etDYNST
9f930 52 49 4e 47 2c 20 20 30 2c 20 20 30 20 7d 2c 0a  RING,  0,  0 },.
9f940 20 20 7b 20 20 27 71 27 2c 20 20 30 2c 20 34 2c    {  'q',  0, 4,
9f950 20 65 74 53 51 4c 45 53 43 41 50 45 2c 20 20 30   etSQLESCAPE,  0
9f960 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 51 27  ,  0 },.  {  'Q'
9f970 2c 20 20 30 2c 20 34 2c 20 65 74 53 51 4c 45 53  ,  0, 4, etSQLES
9f980 43 41 50 45 32 2c 20 30 2c 20 20 30 20 7d 2c 0a  CAPE2, 0,  0 },.
9f990 20 20 7b 20 20 27 77 27 2c 20 20 30 2c 20 34 2c    {  'w',  0, 4,
9f9a0 20 65 74 53 51 4c 45 53 43 41 50 45 33 2c 20 30   etSQLESCAPE3, 0
9f9b0 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 63 27  ,  0 },.  {  'c'
9f9c0 2c 20 20 30 2c 20 30 2c 20 65 74 43 48 41 52 58  ,  0, 0, etCHARX
9f9d0 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a  ,      0,  0 },.
9f9e0 20 20 7b 20 20 27 6f 27 2c 20 20 38 2c 20 30 2c    {  'o',  8, 0,
9f9f0 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 30   etRADIX,      0
9fa00 2c 20 20 32 20 7d 2c 0a 20 20 7b 20 20 27 75 27  ,  2 },.  {  'u'
9fa10 2c 20 31 30 2c 20 30 2c 20 65 74 52 41 44 49 58  , 10, 0, etRADIX
9fa20 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a  ,      0,  0 },.
9fa30 20 20 7b 20 20 27 78 27 2c 20 31 36 2c 20 30 2c    {  'x', 16, 0,
9fa40 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 31   etRADIX,      1
9fa50 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 20 27 58 27  6, 1 },.  {  'X'
9fa60 2c 20 31 36 2c 20 30 2c 20 65 74 52 41 44 49 58  , 16, 0, etRADIX
9fa70 2c 20 20 20 20 20 20 30 2c 20 20 34 20 7d 2c 0a  ,      0,  4 },.
9fa80 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
9fa90 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49  MIT_FLOATING_POI
9faa0 4e 54 0a 20 20 7b 20 20 27 66 27 2c 20 20 30 2c  NT.  {  'f',  0,
9fab0 20 31 2c 20 65 74 46 4c 4f 41 54 2c 20 20 20 20   1, etFLOAT,    
9fac0 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20    0,  0 },.  {  
9fad0 27 65 27 2c 20 20 30 2c 20 31 2c 20 65 74 45 58  'e',  0, 1, etEX
9fae0 50 2c 20 20 20 20 20 20 20 20 33 30 2c 20 30 20  P,        30, 0 
9faf0 7d 2c 0a 20 20 7b 20 20 27 45 27 2c 20 20 30 2c  },.  {  'E',  0,
9fb00 20 31 2c 20 65 74 45 58 50 2c 20 20 20 20 20 20   1, etEXP,      
9fb10 20 20 31 34 2c 20 30 20 7d 2c 0a 20 20 7b 20 20    14, 0 },.  {  
9fb20 27 47 27 2c 20 20 30 2c 20 31 2c 20 65 74 47 45  'G',  0, 1, etGE
9fb30 4e 45 52 49 43 2c 20 20 20 20 31 34 2c 20 30 20  NERIC,    14, 0 
9fb40 7d 2c 0a 23 65 6e 64 69 66 0a 20 20 7b 20 20 27  },.#endif.  {  '
9fb50 69 27 2c 20 31 30 2c 20 31 2c 20 65 74 52 41 44  i', 10, 1, etRAD
9fb60 49 58 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d  IX,      0,  0 }
9fb70 2c 0a 20 20 7b 20 20 27 6e 27 2c 20 20 30 2c 20  ,.  {  'n',  0, 
9fb80 30 2c 20 65 74 53 49 5a 45 2c 20 20 20 20 20 20  0, etSIZE,      
9fb90 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27   0,  0 },.  {  '
9fba0 25 27 2c 20 20 30 2c 20 30 2c 20 65 74 50 45 52  %',  0, 0, etPER
9fbb0 43 45 4e 54 2c 20 20 20 20 30 2c 20 20 30 20 7d  CENT,    0,  0 }
9fbc0 2c 0a 20 20 7b 20 20 27 70 27 2c 20 31 36 2c 20  ,.  {  'p', 16, 
9fbd0 30 2c 20 65 74 50 4f 49 4e 54 45 52 2c 20 20 20  0, etPOINTER,   
9fbe0 20 30 2c 20 20 31 20 7d 2c 0a 0a 2f 2a 20 41 6c   0,  1 },../* Al
9fbf0 6c 20 74 68 65 20 72 65 73 74 20 68 61 76 65 20  l the rest have 
9fc00 74 68 65 20 46 4c 41 47 5f 49 4e 54 45 52 4e 20  the FLAG_INTERN 
9fc10 62 69 74 20 73 65 74 20 61 6e 64 20 61 72 65 20  bit set and are 
9fc20 74 68 75 73 20 66 6f 72 20 69 6e 74 65 72 6e 61  thus for interna
9fc30 6c 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 2a 2f  l.** use only */
9fc40 0a 20 20 7b 20 20 27 54 27 2c 20 20 30 2c 20 32  .  {  'T',  0, 2
9fc50 2c 20 65 74 54 4f 4b 45 4e 2c 20 20 20 20 20 20  , etTOKEN,      
9fc60 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 53  0,  0 },.  {  'S
9fc70 27 2c 20 20 30 2c 20 32 2c 20 65 74 53 52 43 4c  ',  0, 2, etSRCL
9fc80 49 53 54 2c 20 20 20 20 30 2c 20 20 30 20 7d 2c  IST,    0,  0 },
9fc90 0a 20 20 7b 20 20 27 72 27 2c 20 31 30 2c 20 33  .  {  'r', 10, 3
9fca0 2c 20 65 74 4f 52 44 49 4e 41 4c 2c 20 20 20 20  , etORDINAL,    
9fcb0 30 2c 20 20 30 20 7d 2c 0a 7d 3b 0a 0a 2f 2a 0a  0,  0 },.};../*.
9fcc0 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ** If SQLITE_OMI
9fcd0 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
9fce0 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65   is defined, the
9fcf0 6e 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 66 6c  n none of the fl
9fd00 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20  oating point.** 
9fd10 63 6f 6e 76 65 72 73 69 6f 6e 73 20 77 69 6c 6c  conversions will
9fd20 20 77 6f 72 6b 2e 0a 2a 2f 0a 23 69 66 6e 64 65   work..*/.#ifnde
9fd30 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  f SQLITE_OMIT_FL
9fd40 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 2f 2a 0a  OATING_POINT./*.
9fd50 2a 2a 20 22 2a 76 61 6c 22 20 69 73 20 61 20 64  ** "*val" is a d
9fd60 6f 75 62 6c 65 20 73 75 63 68 20 74 68 61 74 20  ouble such that 
9fd70 30 2e 31 20 3c 3d 20 2a 76 61 6c 20 3c 20 31 30  0.1 <= *val < 10
9fd80 2e 30 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  .0.** Return the
9fd90 20 61 73 63 69 69 20 63 6f 64 65 20 66 6f 72 20   ascii code for 
9fda0 74 68 65 20 6c 65 61 64 69 6e 67 20 64 69 67 69  the leading digi
9fdb0 74 20 6f 66 20 2a 76 61 6c 2c 20 74 68 65 6e 0a  t of *val, then.
9fdc0 2a 2a 20 6d 75 6c 74 69 70 6c 79 20 22 2a 76 61  ** multiply "*va
9fdd0 6c 22 20 62 79 20 31 30 2e 30 20 74 6f 20 72 65  l" by 10.0 to re
9fde0 6e 6f 72 6d 61 6c 69 7a 65 2e 0a 2a 2a 0a 2a 2a  normalize..**.**
9fdf0 20 45 78 61 6d 70 6c 65 3a 0a 2a 2a 20 20 20 20   Example:.**    
9fe00 20 69 6e 70 75 74 3a 20 20 20 20 20 2a 76 61 6c   input:     *val
9fe10 20 3d 20 33 2e 31 34 31 35 39 0a 2a 2a 20 20 20   = 3.14159.**   
9fe20 20 20 6f 75 74 70 75 74 3a 20 20 20 20 2a 76 61    output:    *va
9fe30 6c 20 3d 20 31 2e 34 31 35 39 20 20 20 20 66 75  l = 1.4159    fu
9fe40 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 20 3d 20  nction return = 
9fe50 27 33 27 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  '3'.**.** The co
9fe60 75 6e 74 65 72 20 2a 63 6e 74 20 69 73 20 69 6e  unter *cnt is in
9fe70 63 72 65 6d 65 6e 74 65 64 20 65 61 63 68 20 74  cremented each t
9fe80 69 6d 65 2e 20 20 41 66 74 65 72 20 63 6f 75 6e  ime.  After coun
9fe90 74 65 72 20 65 78 63 65 65 64 73 0a 2a 2a 20 31  ter exceeds.** 1
9fea0 36 20 28 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  6 (the number of
9feb0 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67   significant dig
9fec0 69 74 73 20 69 6e 20 61 20 36 34 2d 62 69 74 20  its in a 64-bit 
9fed0 66 6c 6f 61 74 29 20 27 30 27 20 69 73 0a 2a 2a  float) '0' is.**
9fee0 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 65 64   always returned
9fef0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72  ..*/.static char
9ff00 20 65 74 5f 67 65 74 64 69 67 69 74 28 4c 4f 4e   et_getdigit(LON
9ff10 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 2a 76 61  GDOUBLE_TYPE *va
9ff20 6c 2c 20 69 6e 74 20 2a 63 6e 74 29 7b 0a 20 20  l, int *cnt){.  
9ff30 69 6e 74 20 64 69 67 69 74 3b 0a 20 20 4c 4f 4e  int digit;.  LON
9ff40 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 64 3b 0a  GDOUBLE_TYPE d;.
9ff50 20 20 69 66 28 20 28 2a 63 6e 74 29 2b 2b 20 3e    if( (*cnt)++ >
9ff60 3d 20 31 36 20 29 20 72 65 74 75 72 6e 20 27 30  = 16 ) return '0
9ff70 27 3b 0a 20 20 64 69 67 69 74 20 3d 20 28 69 6e  ';.  digit = (in
9ff80 74 29 2a 76 61 6c 3b 0a 20 20 64 20 3d 20 64 69  t)*val;.  d = di
9ff90 67 69 74 3b 0a 20 20 64 69 67 69 74 20 2b 3d 20  git;.  digit += 
9ffa0 27 30 27 3b 0a 20 20 2a 76 61 6c 20 3d 20 28 2a  '0';.  *val = (*
9ffb0 76 61 6c 20 2d 20 64 29 2a 31 30 2e 30 3b 0a 20  val - d)*10.0;. 
9ffc0 20 72 65 74 75 72 6e 20 28 63 68 61 72 29 64 69   return (char)di
9ffd0 67 69 74 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  git;.}.#endif /*
9ffe0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
9fff0 41 54 49 4e 47 5f 50 4f 49 4e 54 20 2a 2f 0a 0a  ATING_POINT */..
a0000 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 4e 20 73  /*.** Append N s
a0010 70 61 63 65 20 63 68 61 72 61 63 74 65 72 73 20  pace characters 
a0020 74 6f 20 74 68 65 20 67 69 76 65 6e 20 73 74 72  to the given str
a0030 69 6e 67 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 73  ing buffer..*/.s
a0040 74 61 74 69 63 20 76 6f 69 64 20 61 70 70 65 6e  tatic void appen
a0050 64 53 70 61 63 65 28 53 74 72 41 63 63 75 6d 20  dSpace(StrAccum 
a0060 2a 70 41 63 63 75 6d 2c 20 69 6e 74 20 4e 29 7b  *pAccum, int N){
a0070 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
a0080 63 68 61 72 20 7a 53 70 61 63 65 73 5b 5d 20 3d  char zSpaces[] =
a0090 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20   "              
a00a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
a00b0 3b 0a 20 20 77 68 69 6c 65 28 20 4e 3e 3d 28 69  ;.  while( N>=(i
a00c0 6e 74 29 73 69 7a 65 6f 66 28 7a 53 70 61 63 65  nt)sizeof(zSpace
a00d0 73 29 2d 31 20 29 7b 0a 20 20 20 20 73 71 6c 69  s)-1 ){.    sqli
a00e0 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e  te3StrAccumAppen
a00f0 64 28 70 41 63 63 75 6d 2c 20 7a 53 70 61 63 65  d(pAccum, zSpace
a0100 73 2c 20 73 69 7a 65 6f 66 28 7a 53 70 61 63 65  s, sizeof(zSpace
a0110 73 29 2d 31 29 3b 0a 20 20 20 20 4e 20 2d 3d 20  s)-1);.    N -= 
a0120 73 69 7a 65 6f 66 28 7a 53 70 61 63 65 73 29 2d  sizeof(zSpaces)-
a0130 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4e 3e 30  1;.  }.  if( N>0
a0140 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53   ){.    sqlite3S
a0150 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41  trAccumAppend(pA
a0160 63 63 75 6d 2c 20 7a 53 70 61 63 65 73 2c 20 4e  ccum, zSpaces, N
a0170 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
a0180 4f 6e 20 6d 61 63 68 69 6e 65 73 20 77 69 74 68  On machines with
a0190 20 61 20 73 6d 61 6c 6c 20 73 74 61 63 6b 20 73   a small stack s
a01a0 69 7a 65 2c 20 79 6f 75 20 63 61 6e 20 72 65 64  ize, you can red
a01b0 65 66 69 6e 65 20 74 68 65 0a 2a 2a 20 53 51 4c  efine the.** SQL
a01c0 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49  ITE_PRINT_BUF_SI
a01d0 5a 45 20 74 6f 20 62 65 20 6c 65 73 73 20 74 68  ZE to be less th
a01e0 61 6e 20 33 35 30 2e 0a 2a 2f 0a 23 69 66 6e 64  an 350..*/.#ifnd
a01f0 65 66 20 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f  ef SQLITE_PRINT_
a0200 42 55 46 5f 53 49 5a 45 0a 23 20 69 66 20 64 65  BUF_SIZE.# if de
a0210 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 53 4d 41  fined(SQLITE_SMA
a0220 4c 4c 5f 53 54 41 43 4b 29 0a 23 20 20 20 64 65  LL_STACK).#   de
a0230 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 52 49 4e  fine SQLITE_PRIN
a0240 54 5f 42 55 46 5f 53 49 5a 45 20 35 30 0a 23 20  T_BUF_SIZE 50.# 
a0250 65 6c 73 65 0a 23 20 20 20 64 65 66 69 6e 65 20  else.#   define 
a0260 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46  SQLITE_PRINT_BUF
a0270 5f 53 49 5a 45 20 33 35 30 0a 23 20 65 6e 64 69  _SIZE 350.# endi
a0280 66 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65  f.#endif.#define
a0290 20 65 74 42 55 46 53 49 5a 45 20 53 51 4c 49 54   etBUFSIZE SQLIT
a02a0 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45  E_PRINT_BUF_SIZE
a02b0 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65    /* Size of the
a02c0 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 2a   output buffer *
a02d0 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f  /../*.** The roo
a02e0 74 20 70 72 6f 67 72 61 6d 2e 20 20 41 6c 6c 20  t program.  All 
a02f0 76 61 72 69 61 74 69 6f 6e 73 20 63 61 6c 6c 20  variations call 
a0300 74 68 69 73 20 63 6f 72 65 2e 0a 2a 2a 0a 2a 2a  this core..**.**
a0310 20 49 4e 50 55 54 53 3a 0a 2a 2a 20 20 20 66 75   INPUTS:.**   fu
a0320 6e 63 20 20 20 54 68 69 73 20 69 73 20 61 20 70  nc   This is a p
a0330 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63  ointer to a func
a0340 74 69 6f 6e 20 74 61 6b 69 6e 67 20 74 68 72 65  tion taking thre
a0350 65 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 20  e arguments.**  
a0360 20 20 20 20 20 20 20 20 20 20 31 2e 20 41 20 70            1. A p
a0370 6f 69 6e 74 65 72 20 74 6f 20 61 6e 79 74 68 69  ointer to anythi
a0380 6e 67 2e 20 20 53 61 6d 65 20 61 73 20 74 68 65  ng.  Same as the
a0390 20 22 61 72 67 22 20 70 61 72 61 6d 65 74 65 72   "arg" parameter
a03a0 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  ..**            
a03b0 32 2e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  2. A pointer to 
a03c0 74 68 65 20 6c 69 73 74 20 6f 66 20 63 68 61 72  the list of char
a03d0 61 63 74 65 72 73 20 74 6f 20 62 65 20 6f 75 74  acters to be out
a03e0 70 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  put.**          
a03f0 20 20 20 20 20 28 4e 6f 74 65 2c 20 74 68 69 73       (Note, this
a0400 20 6c 69 73 74 20 69 73 20 4e 4f 54 20 6e 75 6c   list is NOT nul
a0410 6c 20 74 65 72 6d 69 6e 61 74 65 64 2e 29 0a 2a  l terminated.).*
a0420 2a 20 20 20 20 20 20 20 20 20 20 20 20 33 2e 20  *            3. 
a0430 41 6e 20 69 6e 74 65 67 65 72 20 6e 75 6d 62 65  An integer numbe
a0440 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20  r of characters 
a0450 74 6f 20 62 65 20 6f 75 74 70 75 74 2e 0a 2a 2a  to be output..**
a0460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
a0470 4e 6f 74 65 3a 20 54 68 69 73 20 6e 75 6d 62 65  Note: This numbe
a0480 72 20 6d 69 67 68 74 20 62 65 20 7a 65 72 6f 2e  r might be zero.
a0490 29 0a 2a 2a 0a 2a 2a 20 20 20 61 72 67 20 20 20  ).**.**   arg   
a04a0 20 54 68 69 73 20 69 73 20 74 68 65 20 70 6f 69   This is the poi
a04b0 6e 74 65 72 20 74 6f 20 61 6e 79 74 68 69 6e 67  nter to anything
a04c0 20 77 68 69 63 68 20 77 69 6c 6c 20 62 65 20 70   which will be p
a04d0 61 73 73 65 64 20 61 73 20 74 68 65 0a 2a 2a 20  assed as the.** 
a04e0 20 20 20 20 20 20 20 20 20 66 69 72 73 74 20 61           first a
a04f0 72 67 75 6d 65 6e 74 20 74 6f 20 22 66 75 6e 63  rgument to "func
a0500 22 2e 20 20 55 73 65 20 69 74 20 66 6f 72 20 77  ".  Use it for w
a0510 68 61 74 65 76 65 72 20 79 6f 75 20 6c 69 6b 65  hatever you like
a0520 2e 0a 2a 2a 0a 2a 2a 20 20 20 66 6d 74 20 20 20  ..**.**   fmt   
a0530 20 54 68 69 73 20 69 73 20 74 68 65 20 66 6f 72   This is the for
a0540 6d 61 74 20 73 74 72 69 6e 67 2c 20 61 73 20 69  mat string, as i
a0550 6e 20 74 68 65 20 75 73 75 61 6c 20 70 72 69 6e  n the usual prin
a0560 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 61 70 20 20 20  t..**.**   ap   
a0570 20 20 54 68 69 73 20 69 73 20 61 20 70 6f 69 6e    This is a poin
a0580 74 65 72 20 74 6f 20 61 20 6c 69 73 74 20 6f 66  ter to a list of
a0590 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 53 61 6d   arguments.  Sam
a05a0 65 20 61 73 20 69 6e 0a 2a 2a 20 20 20 20 20 20  e as in.**      
a05b0 20 20 20 20 76 66 70 72 69 6e 74 2e 0a 2a 2a 0a      vfprint..**.
a05c0 2a 2a 20 4f 55 54 50 55 54 53 3a 0a 2a 2a 20 20  ** OUTPUTS:.**  
a05d0 20 20 20 20 20 20 20 20 54 68 65 20 72 65 74 75          The retu
a05e0 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20  rn value is the 
a05f0 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  total number of 
a0600 63 68 61 72 61 63 74 65 72 73 20 73 65 6e 74 20  characters sent 
a0610 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  to.**          t
a0620 68 65 20 66 75 6e 63 74 69 6f 6e 20 22 66 75 6e  he function "fun
a0630 63 22 2e 20 20 52 65 74 75 72 6e 73 20 2d 31 20  c".  Returns -1 
a0640 6f 6e 20 61 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a  on a error..**.*
a0650 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20  * Note that the 
a0660 6f 72 64 65 72 20 69 6e 20 77 68 69 63 68 20 61  order in which a
a0670 75 74 6f 6d 61 74 69 63 20 76 61 72 69 61 62 6c  utomatic variabl
a0680 65 73 20 61 72 65 20 64 65 63 6c 61 72 65 64 20  es are declared 
a0690 62 65 6c 6f 77 0a 2a 2a 20 73 65 65 6d 73 20 74  below.** seems t
a06a0 6f 20 6d 61 6b 65 20 61 20 62 69 67 20 64 69 66  o make a big dif
a06b0 66 65 72 65 6e 63 65 20 69 6e 20 64 65 74 65 72  ference in deter
a06c0 6d 69 6e 69 6e 67 20 68 6f 77 20 66 61 73 74 20  mining how fast 
a06d0 74 68 69 73 20 62 65 61 73 74 0a 2a 2a 20 77 69  this beast.** wi
a06e0 6c 6c 20 72 75 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  ll run..*/.SQLIT
a06f0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
a0700 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 0a  qlite3VXPrintf(.
a0710 20 20 53 74 72 41 63 63 75 6d 20 2a 70 41 63 63    StrAccum *pAcc
a0720 75 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  um,             
a0730 20 20 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61       /* Accumula
a0740 74 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 20  te results here 
a0750 2a 2f 0a 20 20 69 6e 74 20 75 73 65 45 78 74 65  */.  int useExte
a0760 6e 64 65 64 2c 20 20 20 20 20 20 20 20 20 20 20  nded,           
a0770 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 77          /* Allow
a0780 20 65 78 74 65 6e 64 65 64 20 25 2d 63 6f 6e 76   extended %-conv
a0790 65 72 73 69 6f 6e 73 20 2a 2f 0a 20 20 63 6f 6e  ersions */.  con
a07a0 73 74 20 63 68 61 72 20 2a 66 6d 74 2c 20 20 20  st char *fmt,   
a07b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a07c0 2f 2a 20 46 6f 72 6d 61 74 20 73 74 72 69 6e 67  /* Format string
a07d0 20 2a 2f 0a 20 20 76 61 5f 6c 69 73 74 20 61 70   */.  va_list ap
a07e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a07f0 20 20 20 20 20 20 20 20 20 2f 2a 20 61 72 67 75           /* argu
a0800 6d 65 6e 74 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e  ments */.){.  in
a0810 74 20 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  t c;            
a0820 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74           /* Next
a0830 20 63 68 61 72 61 63 74 65 72 20 69 6e 20 74 68   character in th
a0840 65 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20  e format string 
a0850 2a 2f 0a 20 20 63 68 61 72 20 2a 62 75 66 70 74  */.  char *bufpt
a0860 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
a0870 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68  /* Pointer to th
a0880 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 62 75 66  e conversion buf
a0890 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 72 65  fer */.  int pre
a08a0 63 69 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20  cision;         
a08b0 20 20 20 20 2f 2a 20 50 72 65 63 69 73 69 6f 6e      /* Precision
a08c0 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20   of the current 
a08d0 66 69 65 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 6c  field */.  int l
a08e0 65 6e 67 74 68 3b 20 20 20 20 20 20 20 20 20 20  ength;          
a08f0 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20        /* Length 
a0900 6f 66 20 74 68 65 20 66 69 65 6c 64 20 2a 2f 0a  of the field */.
a0910 20 20 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20    int idx;      
a0920 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a0930 41 20 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73  A general purpos
a0940 65 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  e loop counter *
a0950 2f 0a 20 20 69 6e 74 20 77 69 64 74 68 3b 20 20  /.  int width;  
a0960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
a0970 2a 20 57 69 64 74 68 20 6f 66 20 74 68 65 20 63  * Width of the c
a0980 75 72 72 65 6e 74 20 66 69 65 6c 64 20 2a 2f 0a  urrent field */.
a0990 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 6c 65    etByte flag_le
a09a0 66 74 6a 75 73 74 69 66 79 3b 20 20 20 2f 2a 20  ftjustify;   /* 
a09b0 54 72 75 65 20 69 66 20 22 2d 22 20 66 6c 61 67  True if "-" flag
a09c0 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20   is present */. 
a09d0 20 65 74 42 79 74 65 20 66 6c 61 67 5f 70 6c 75   etByte flag_plu
a09e0 73 73 69 67 6e 3b 20 20 20 20 20 20 2f 2a 20 54  ssign;      /* T
a09f0 72 75 65 20 69 66 20 22 2b 22 20 66 6c 61 67 20  rue if "+" flag 
a0a00 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20  is present */.  
a0a10 65 74 42 79 74 65 20 66 6c 61 67 5f 62 6c 61 6e  etByte flag_blan
a0a20 6b 73 69 67 6e 3b 20 20 20 20 20 2f 2a 20 54 72  ksign;     /* Tr
a0a30 75 65 20 69 66 20 22 20 22 20 66 6c 61 67 20 69  ue if " " flag i
a0a40 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65  s present */.  e
a0a50 74 42 79 74 65 20 66 6c 61 67 5f 61 6c 74 65 72  tByte flag_alter
a0a60 6e 61 74 65 66 6f 72 6d 3b 20 2f 2a 20 54 72 75  nateform; /* Tru
a0a70 65 20 69 66 20 22 23 22 20 66 6c 61 67 20 69 73  e if "#" flag is
a0a80 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74   present */.  et
a0a90 42 79 74 65 20 66 6c 61 67 5f 61 6c 74 66 6f 72  Byte flag_altfor
a0aa0 6d 32 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65  m2;      /* True
a0ab0 20 69 66 20 22 21 22 20 66 6c 61 67 20 69 73 20   if "!" flag is 
a0ac0 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42  present */.  etB
a0ad0 79 74 65 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64  yte flag_zeropad
a0ae0 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  ;       /* True 
a0af0 69 66 20 66 69 65 6c 64 20 77 69 64 74 68 20 63  if field width c
a0b00 6f 6e 73 74 61 6e 74 20 73 74 61 72 74 73 20 77  onstant starts w
a0b10 69 74 68 20 7a 65 72 6f 20 2a 2f 0a 20 20 65 74  ith zero */.  et
a0b20 42 79 74 65 20 66 6c 61 67 5f 6c 6f 6e 67 3b 20  Byte flag_long; 
a0b30 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
a0b40 20 69 66 20 22 6c 22 20 66 6c 61 67 20 69 73 20   if "l" flag is 
a0b50 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42  present */.  etB
a0b60 79 74 65 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e  yte flag_longlon
a0b70 67 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  g;      /* True 
a0b80 69 66 20 74 68 65 20 22 6c 6c 22 20 66 6c 61 67  if the "ll" flag
a0b90 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20   is present */. 
a0ba0 20 65 74 42 79 74 65 20 64 6f 6e 65 3b 20 20 20   etByte done;   
a0bb0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
a0bc0 6f 6f 70 20 74 65 72 6d 69 6e 61 74 69 6f 6e 20  oop termination 
a0bd0 66 6c 61 67 20 2a 2f 0a 20 20 73 71 6c 69 74 65  flag */.  sqlite
a0be0 5f 75 69 6e 74 36 34 20 6c 6f 6e 67 76 61 6c 75  _uint64 longvalu
a0bf0 65 3b 20 20 20 2f 2a 20 56 61 6c 75 65 20 66 6f  e;   /* Value fo
a0c00 72 20 69 6e 74 65 67 65 72 20 74 79 70 65 73 20  r integer types 
a0c10 2a 2f 0a 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f  */.  LONGDOUBLE_
a0c20 54 59 50 45 20 72 65 61 6c 76 61 6c 75 65 3b 20  TYPE realvalue; 
a0c30 2f 2a 20 56 61 6c 75 65 20 66 6f 72 20 72 65 61  /* Value for rea
a0c40 6c 20 74 79 70 65 73 20 2a 2f 0a 20 20 63 6f 6e  l types */.  con
a0c50 73 74 20 65 74 5f 69 6e 66 6f 20 2a 69 6e 66 6f  st et_info *info
a0c60 70 3b 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74  p;      /* Point
a0c70 65 72 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70  er to the approp
a0c80 72 69 61 74 65 20 69 6e 66 6f 20 73 74 72 75 63  riate info struc
a0c90 74 75 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 62  ture */.  char b
a0ca0 75 66 5b 65 74 42 55 46 53 49 5a 45 5d 3b 20 20  uf[etBUFSIZE];  
a0cb0 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69       /* Conversi
a0cc0 6f 6e 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 63  on buffer */.  c
a0cd0 68 61 72 20 70 72 65 66 69 78 3b 20 20 20 20 20  har prefix;     
a0ce0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65            /* Pre
a0cf0 66 69 78 20 63 68 61 72 61 63 74 65 72 2e 20 20  fix character.  
a0d00 22 2b 22 20 6f 72 20 22 2d 22 20 6f 72 20 22 20  "+" or "-" or " 
a0d10 22 20 6f 72 20 27 5c 30 27 2e 20 2a 2f 0a 20 20  " or '\0'. */.  
a0d20 65 74 42 79 74 65 20 78 74 79 70 65 20 3d 20 30  etByte xtype = 0
a0d30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f  ;          /* Co
a0d40 6e 76 65 72 73 69 6f 6e 20 70 61 72 61 64 69 67  nversion paradig
a0d50 6d 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 78  m */.  char *zEx
a0d60 74 72 61 3b 20 20 20 20 20 20 20 20 20 20 20 20  tra;            
a0d70 20 20 2f 2a 20 45 78 74 72 61 20 6d 65 6d 6f 72    /* Extra memor
a0d80 79 20 75 73 65 64 20 66 6f 72 20 65 74 54 43 4c  y used for etTCL
a0d90 45 53 43 41 50 45 20 63 6f 6e 76 65 72 73 69 6f  ESCAPE conversio
a0da0 6e 73 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  ns */.#ifndef SQ
a0db0 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
a0dc0 4e 47 5f 50 4f 49 4e 54 0a 20 20 69 6e 74 20 20  NG_POINT.  int  
a0dd0 65 78 70 2c 20 65 32 3b 20 20 20 20 20 20 20 20  exp, e2;        
a0de0 20 20 20 20 20 20 2f 2a 20 65 78 70 6f 6e 65 6e        /* exponen
a0df0 74 20 6f 66 20 72 65 61 6c 20 6e 75 6d 62 65 72  t of real number
a0e00 73 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 6f  s */.  double ro
a0e10 75 6e 64 65 72 3b 20 20 20 20 20 20 20 20 20 20  under;          
a0e20 20 20 2f 2a 20 55 73 65 64 20 66 6f 72 20 72 6f    /* Used for ro
a0e30 75 6e 64 69 6e 67 20 66 6c 6f 61 74 69 6e 67 20  unding floating 
a0e40 70 6f 69 6e 74 20 76 61 6c 75 65 73 20 2a 2f 0a  point values */.
a0e50 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 64 70    etByte flag_dp
a0e60 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
a0e70 54 72 75 65 20 69 66 20 64 65 63 69 6d 61 6c 20  True if decimal 
a0e80 70 6f 69 6e 74 20 73 68 6f 75 6c 64 20 62 65 20  point should be 
a0e90 73 68 6f 77 6e 20 2a 2f 0a 20 20 65 74 42 79 74  shown */.  etByt
a0ea0 65 20 66 6c 61 67 5f 72 74 7a 3b 20 20 20 20 20  e flag_rtz;     
a0eb0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
a0ec0 20 74 72 61 69 6c 69 6e 67 20 7a 65 72 6f 73 20   trailing zeros 
a0ed0 73 68 6f 75 6c 64 20 62 65 20 72 65 6d 6f 76 65  should be remove
a0ee0 64 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c  d */.  etByte fl
a0ef0 61 67 5f 65 78 70 3b 20 20 20 20 20 20 20 20 20  ag_exp;         
a0f00 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 66 6f 72    /* True to for
a0f10 63 65 20 64 69 73 70 6c 61 79 20 6f 66 20 74 68  ce display of th
a0f20 65 20 65 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20  e exponent */.  
a0f30 69 6e 74 20 6e 73 64 3b 20 20 20 20 20 20 20 20  int nsd;        
a0f40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
a0f50 6d 62 65 72 20 6f 66 20 73 69 67 6e 69 66 69 63  mber of signific
a0f60 61 6e 74 20 64 69 67 69 74 73 20 72 65 74 75 72  ant digits retur
a0f70 6e 65 64 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20  ned */.#endif.. 
a0f80 20 6c 65 6e 67 74 68 20 3d 20 30 3b 0a 20 20 62   length = 0;.  b
a0f90 75 66 70 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28  ufpt = 0;.  for(
a0fa0 3b 20 28 63 3d 28 2a 66 6d 74 29 29 21 3d 30 3b  ; (c=(*fmt))!=0;
a0fb0 20 2b 2b 66 6d 74 29 7b 0a 20 20 20 20 69 66 28   ++fmt){.    if(
a0fc0 20 63 21 3d 27 25 27 20 29 7b 0a 20 20 20 20 20   c!='%' ){.     
a0fd0 20 69 6e 74 20 61 6d 74 3b 0a 20 20 20 20 20 20   int amt;.      
a0fe0 62 75 66 70 74 20 3d 20 28 63 68 61 72 20 2a 29  bufpt = (char *)
a0ff0 66 6d 74 3b 0a 20 20 20 20 20 20 61 6d 74 20 3d  fmt;.      amt =
a1000 20 31 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28   1;.      while(
a1010 20 28 63 3d 28 2a 2b 2b 66 6d 74 29 29 21 3d 27   (c=(*++fmt))!='
a1020 25 27 20 26 26 20 63 21 3d 30 20 29 20 61 6d 74  %' && c!=0 ) amt
a1030 2b 2b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ++;.      sqlite
a1040 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28  3StrAccumAppend(
a1050 70 41 63 63 75 6d 2c 20 62 75 66 70 74 2c 20 61  pAccum, bufpt, a
a1060 6d 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 63  mt);.      if( c
a1070 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  ==0 ) break;.   
a1080 20 7d 0a 20 20 20 20 69 66 28 20 28 63 3d 28 2a   }.    if( (c=(*
a1090 2b 2b 66 6d 74 29 29 3d 3d 30 20 29 7b 0a 20 20  ++fmt))==0 ){.  
a10a0 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63      sqlite3StrAc
a10b0 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d  cumAppend(pAccum
a10c0 2c 20 22 25 22 2c 20 31 29 3b 0a 20 20 20 20 20  , "%", 1);.     
a10d0 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
a10e0 20 20 2f 2a 20 46 69 6e 64 20 6f 75 74 20 77 68    /* Find out wh
a10f0 61 74 20 66 6c 61 67 73 20 61 72 65 20 70 72 65  at flags are pre
a1100 73 65 6e 74 20 2a 2f 0a 20 20 20 20 66 6c 61 67  sent */.    flag
a1110 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 3d 20 66  _leftjustify = f
a1120 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 3d 20 66  lag_plussign = f
a1130 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 3d 20  lag_blanksign = 
a1140 0a 20 20 20 20 20 66 6c 61 67 5f 61 6c 74 65 72  .     flag_alter
a1150 6e 61 74 65 66 6f 72 6d 20 3d 20 66 6c 61 67 5f  nateform = flag_
a1160 61 6c 74 66 6f 72 6d 32 20 3d 20 66 6c 61 67 5f  altform2 = flag_
a1170 7a 65 72 6f 70 61 64 20 3d 20 30 3b 0a 20 20 20  zeropad = 0;.   
a1180 20 64 6f 6e 65 20 3d 20 30 3b 0a 20 20 20 20 64   done = 0;.    d
a1190 6f 7b 0a 20 20 20 20 20 20 73 77 69 74 63 68 28  o{.      switch(
a11a0 20 63 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61   c ){.        ca
a11b0 73 65 20 27 2d 27 3a 20 20 20 66 6c 61 67 5f 6c  se '-':   flag_l
a11c0 65 66 74 6a 75 73 74 69 66 79 20 3d 20 31 3b 20  eftjustify = 1; 
a11d0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
a11e0 20 20 20 63 61 73 65 20 27 2b 27 3a 20 20 20 66     case '+':   f
a11f0 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 3d 20 31  lag_plussign = 1
a1200 3b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  ;        break;.
a1210 20 20 20 20 20 20 20 20 63 61 73 65 20 27 20 27          case ' '
a1220 3a 20 20 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69  :   flag_blanksi
a1230 67 6e 20 3d 20 31 3b 20 20 20 20 20 20 20 62 72  gn = 1;       br
a1240 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73  eak;.        cas
a1250 65 20 27 23 27 3a 20 20 20 66 6c 61 67 5f 61 6c  e '#':   flag_al
a1260 74 65 72 6e 61 74 65 66 6f 72 6d 20 3d 20 31 3b  ternateform = 1;
a1270 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
a1280 20 20 63 61 73 65 20 27 21 27 3a 20 20 20 66 6c    case '!':   fl
a1290 61 67 5f 61 6c 74 66 6f 72 6d 32 20 3d 20 31 3b  ag_altform2 = 1;
a12a0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
a12b0 20 20 20 20 20 20 20 63 61 73 65 20 27 30 27 3a         case '0':
a12c0 20 20 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20     flag_zeropad 
a12d0 3d 20 31 3b 20 20 20 20 20 20 20 20 20 62 72 65  = 1;         bre
a12e0 61 6b 3b 0a 20 20 20 20 20 20 20 20 64 65 66 61  ak;.        defa
a12f0 75 6c 74 3a 20 20 20 20 64 6f 6e 65 20 3d 20 31  ult:    done = 1
a1300 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
a1310 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
a1320 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 21 64 6f  .    }while( !do
a1330 6e 65 20 26 26 20 28 63 3d 28 2a 2b 2b 66 6d 74  ne && (c=(*++fmt
a1340 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 2f 2a 20  ))!=0 );.    /* 
a1350 47 65 74 20 74 68 65 20 66 69 65 6c 64 20 77 69  Get the field wi
a1360 64 74 68 20 2a 2f 0a 20 20 20 20 77 69 64 74 68  dth */.    width
a1370 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 63 3d   = 0;.    if( c=
a1380 3d 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 77 69  ='*' ){.      wi
a1390 64 74 68 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  dth = va_arg(ap,
a13a0 69 6e 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20  int);.      if( 
a13b0 77 69 64 74 68 3c 30 20 29 7b 0a 20 20 20 20 20  width<0 ){.     
a13c0 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74     flag_leftjust
a13d0 69 66 79 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  ify = 1;.       
a13e0 20 77 69 64 74 68 20 3d 20 2d 77 69 64 74 68 3b   width = -width;
a13f0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63  .      }.      c
a1400 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 7d   = *++fmt;.    }
a1410 65 6c 73 65 7b 0a 20 20 20 20 20 20 77 68 69 6c  else{.      whil
a1420 65 28 20 63 3e 3d 27 30 27 20 26 26 20 63 3c 3d  e( c>='0' && c<=
a1430 27 39 27 20 29 7b 0a 20 20 20 20 20 20 20 20 77  '9' ){.        w
a1440 69 64 74 68 20 3d 20 77 69 64 74 68 2a 31 30 20  idth = width*10 
a1450 2b 20 63 20 2d 20 27 30 27 3b 0a 20 20 20 20 20  + c - '0';.     
a1460 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20     c = *++fmt;. 
a1470 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
a1480 20 69 66 28 20 77 69 64 74 68 20 3e 20 65 74 42   if( width > etB
a1490 55 46 53 49 5a 45 2d 31 30 20 29 7b 0a 20 20 20  UFSIZE-10 ){.   
a14a0 20 20 20 77 69 64 74 68 20 3d 20 65 74 42 55 46     width = etBUF
a14b0 53 49 5a 45 2d 31 30 3b 0a 20 20 20 20 7d 0a 20  SIZE-10;.    }. 
a14c0 20 20 20 2f 2a 20 47 65 74 20 74 68 65 20 70 72     /* Get the pr
a14d0 65 63 69 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 69  ecision */.    i
a14e0 66 28 20 63 3d 3d 27 2e 27 20 29 7b 0a 20 20 20  f( c=='.' ){.   
a14f0 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 30     precision = 0
a1500 3b 0a 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66  ;.      c = *++f
a1510 6d 74 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3d  mt;.      if( c=
a1520 3d 27 2a 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='*' ){.        
a1530 70 72 65 63 69 73 69 6f 6e 20 3d 20 76 61 5f 61  precision = va_a
a1540 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20  rg(ap,int);.    
a1550 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69 6f      if( precisio
a1560 6e 3c 30 20 29 20 70 72 65 63 69 73 69 6f 6e 20  n<0 ) precision 
a1570 3d 20 2d 70 72 65 63 69 73 69 6f 6e 3b 0a 20 20  = -precision;.  
a1580 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74        c = *++fmt
a1590 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
a15a0 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 63 3e         while( c>
a15b0 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 27 20 29  ='0' && c<='9' )
a15c0 7b 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 63  {.          prec
a15d0 69 73 69 6f 6e 20 3d 20 70 72 65 63 69 73 69 6f  ision = precisio
a15e0 6e 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 3b 0a  n*10 + c - '0';.
a15f0 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 2a 2b            c = *+
a1600 2b 66 6d 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  +fmt;.        }.
a1610 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
a1620 65 7b 0a 20 20 20 20 20 20 70 72 65 63 69 73 69  e{.      precisi
a1630 6f 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20  on = -1;.    }. 
a1640 20 20 20 2f 2a 20 47 65 74 20 74 68 65 20 63 6f     /* Get the co
a1650 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 20 6d 6f  nversion type mo
a1660 64 69 66 69 65 72 20 2a 2f 0a 20 20 20 20 69 66  difier */.    if
a1670 28 20 63 3d 3d 27 6c 27 20 29 7b 0a 20 20 20 20  ( c=='l' ){.    
a1680 20 20 66 6c 61 67 5f 6c 6f 6e 67 20 3d 20 31 3b    flag_long = 1;
a1690 0a 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d  .      c = *++fm
a16a0 74 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d  t;.      if( c==
a16b0 27 6c 27 20 29 7b 0a 20 20 20 20 20 20 20 20 66  'l' ){.        f
a16c0 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 31  lag_longlong = 1
a16d0 3b 0a 20 20 20 20 20 20 20 20 63 20 3d 20 2a 2b  ;.        c = *+
a16e0 2b 66 6d 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73  +fmt;.      }els
a16f0 65 7b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f  e{.        flag_
a1700 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 30 3b 0a 20 20  longlong = 0;.  
a1710 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b      }.    }else{
a1720 0a 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67  .      flag_long
a1730 20 3d 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67   = flag_longlong
a1740 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
a1750 2f 2a 20 46 65 74 63 68 20 74 68 65 20 69 6e 66  /* Fetch the inf
a1760 6f 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20  o entry for the 
a1770 66 69 65 6c 64 20 2a 2f 0a 20 20 20 20 69 6e 66  field */.    inf
a1780 6f 70 20 3d 20 26 66 6d 74 69 6e 66 6f 5b 30 5d  op = &fmtinfo[0]
a1790 3b 0a 20 20 20 20 78 74 79 70 65 20 3d 20 65 74  ;.    xtype = et
a17a0 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 66 6f 72  INVALID;.    for
a17b0 28 69 64 78 3d 30 3b 20 69 64 78 3c 41 72 72 61  (idx=0; idx<Arra
a17c0 79 53 69 7a 65 28 66 6d 74 69 6e 66 6f 29 3b 20  ySize(fmtinfo); 
a17d0 69 64 78 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  idx++){.      if
a17e0 28 20 63 3d 3d 66 6d 74 69 6e 66 6f 5b 69 64 78  ( c==fmtinfo[idx
a17f0 5d 2e 66 6d 74 74 79 70 65 20 29 7b 0a 20 20 20  ].fmttype ){.   
a1800 20 20 20 20 20 69 6e 66 6f 70 20 3d 20 26 66 6d       infop = &fm
a1810 74 69 6e 66 6f 5b 69 64 78 5d 3b 0a 20 20 20 20  tinfo[idx];.    
a1820 20 20 20 20 69 66 28 20 75 73 65 45 78 74 65 6e      if( useExten
a1830 64 65 64 20 7c 7c 20 28 69 6e 66 6f 70 2d 3e 66  ded || (infop->f
a1840 6c 61 67 73 20 26 20 46 4c 41 47 5f 49 4e 54 45  lags & FLAG_INTE
a1850 52 4e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  RN)==0 ){.      
a1860 20 20 20 20 78 74 79 70 65 20 3d 20 69 6e 66 6f      xtype = info
a1870 70 2d 3e 74 79 70 65 3b 0a 20 20 20 20 20 20 20  p->type;.       
a1880 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
a1890 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
a18a0 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61    }.        brea
a18b0 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
a18c0 0a 20 20 20 20 7a 45 78 74 72 61 20 3d 20 30 3b  .    zExtra = 0;
a18d0 0a 0a 0a 20 20 20 20 2f 2a 20 4c 69 6d 69 74 20  ...    /* Limit 
a18e0 74 68 65 20 70 72 65 63 69 73 69 6f 6e 20 74 6f  the precision to
a18f0 20 70 72 65 76 65 6e 74 20 6f 76 65 72 66 6c 6f   prevent overflo
a1900 77 69 6e 67 20 62 75 66 5b 5d 20 64 75 72 69 6e  wing buf[] durin
a1910 67 20 63 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f 0a  g conversion */.
a1920 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69 6f      if( precisio
a1930 6e 3e 65 74 42 55 46 53 49 5a 45 2d 34 30 20 26  n>etBUFSIZE-40 &
a1940 26 20 28 69 6e 66 6f 70 2d 3e 66 6c 61 67 73 20  & (infop->flags 
a1950 26 20 46 4c 41 47 5f 53 54 52 49 4e 47 29 3d 3d  & FLAG_STRING)==
a1960 30 20 29 7b 0a 20 20 20 20 20 20 70 72 65 63 69  0 ){.      preci
a1970 73 69 6f 6e 20 3d 20 65 74 42 55 46 53 49 5a 45  sion = etBUFSIZE
a1980 2d 34 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  -40;.    }..    
a1990 2f 2a 0a 20 20 20 20 2a 2a 20 41 74 20 74 68 69  /*.    ** At thi
a19a0 73 20 70 6f 69 6e 74 2c 20 76 61 72 69 61 62 6c  s point, variabl
a19b0 65 73 20 61 72 65 20 69 6e 69 74 69 61 6c 69 7a  es are initializ
a19c0 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20  ed as follows:. 
a19d0 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 66     **.    **   f
a19e0 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72  lag_alternatefor
a19f0 6d 20 20 20 20 20 20 20 20 20 20 54 52 55 45 20  m          TRUE 
a1a00 69 66 20 61 20 27 23 27 20 69 73 20 70 72 65 73  if a '#' is pres
a1a10 65 6e 74 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c  ent..    **   fl
a1a20 61 67 5f 61 6c 74 66 6f 72 6d 32 20 20 20 20 20  ag_altform2     
a1a30 20 20 20 20 20 20 20 20 20 20 54 52 55 45 20 69            TRUE i
a1a40 66 20 61 20 27 21 27 20 69 73 20 70 72 65 73 65  f a '!' is prese
a1a50 6e 74 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61  nt..    **   fla
a1a60 67 5f 70 6c 75 73 73 69 67 6e 20 20 20 20 20 20  g_plussign      
a1a70 20 20 20 20 20 20 20 20 20 54 52 55 45 20 69 66           TRUE if
a1a80 20 61 20 27 2b 27 20 69 73 20 70 72 65 73 65 6e   a '+' is presen
a1a90 74 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67  t..    **   flag
a1aa0 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 20 20 20  _leftjustify    
a1ab0 20 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20          TRUE if 
a1ac0 61 20 27 2d 27 20 69 73 20 70 72 65 73 65 6e 74  a '-' is present
a1ad0 20 6f 72 20 69 66 20 74 68 65 0a 20 20 20 20 2a   or if the.    *
a1ae0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
a1af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1b00 66 69 65 6c 64 20 77 69 64 74 68 20 77 61 73 20  field width was 
a1b10 6e 65 67 61 74 69 76 65 2e 0a 20 20 20 20 2a 2a  negative..    **
a1b20 20 20 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20     flag_zeropad 
a1b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54                 T
a1b40 52 55 45 20 69 66 20 74 68 65 20 77 69 64 74 68  RUE if the width
a1b50 20 62 65 67 61 6e 20 77 69 74 68 20 30 2e 0a 20   began with 0.. 
a1b60 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 6c 6f 6e     **   flag_lon
a1b70 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g               
a1b80 20 20 20 20 54 52 55 45 20 69 66 20 74 68 65 20      TRUE if the 
a1b90 6c 65 74 74 65 72 20 27 6c 27 20 28 65 6c 6c 29  letter 'l' (ell)
a1ba0 20 70 72 65 66 69 78 65 64 0a 20 20 20 20 2a 2a   prefixed.    **
a1bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
a1bd0 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 63 68  he conversion ch
a1be0 61 72 61 63 74 65 72 2e 0a 20 20 20 20 2a 2a 20  aracter..    ** 
a1bf0 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20    flag_longlong 
a1c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 52                TR
a1c10 55 45 20 69 66 20 74 68 65 20 6c 65 74 74 65 72  UE if the letter
a1c20 20 27 6c 6c 27 20 28 65 6c 6c 20 65 6c 6c 29 20   'll' (ell ell) 
a1c30 70 72 65 66 69 78 65 64 0a 20 20 20 20 2a 2a 20  prefixed.    ** 
a1c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68                th
a1c60 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 63 68 61  e conversion cha
a1c70 72 61 63 74 65 72 2e 0a 20 20 20 20 2a 2a 20 20  racter..    **  
a1c80 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20   flag_blanksign 
a1c90 20 20 20 20 20 20 20 20 20 20 20 20 20 54 52 55               TRU
a1ca0 45 20 69 66 20 61 20 27 20 27 20 69 73 20 70 72  E if a ' ' is pr
a1cb0 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a 20 20 20  esent..    **   
a1cc0 77 69 64 74 68 20 20 20 20 20 20 20 20 20 20 20  width           
a1cd0 20 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20              The 
a1ce0 73 70 65 63 69 66 69 65 64 20 66 69 65 6c 64 20  specified field 
a1cf0 77 69 64 74 68 2e 20 20 54 68 69 73 20 69 73 0a  width.  This is.
a1d00 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
a1d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1d20 20 20 20 20 20 61 6c 77 61 79 73 20 6e 6f 6e 2d       always non-
a1d30 6e 65 67 61 74 69 76 65 2e 20 20 5a 65 72 6f 20  negative.  Zero 
a1d40 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 2e 0a  is the default..
a1d50 20 20 20 20 2a 2a 20 20 20 70 72 65 63 69 73 69      **   precisi
a1d60 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  on              
a1d70 20 20 20 20 20 54 68 65 20 73 70 65 63 69 66 69       The specifi
a1d80 65 64 20 70 72 65 63 69 73 69 6f 6e 2e 20 20 54  ed precision.  T
a1d90 68 65 20 64 65 66 61 75 6c 74 0a 20 20 20 20 2a  he default.    *
a1da0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
a1db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1dc0 69 73 20 2d 31 2e 0a 20 20 20 20 2a 2a 20 20 20  is -1..    **   
a1dd0 78 74 79 70 65 20 20 20 20 20 20 20 20 20 20 20  xtype           
a1de0 20 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20              The 
a1df0 63 6c 61 73 73 20 6f 66 20 74 68 65 20 63 6f 6e  class of the con
a1e00 76 65 72 73 69 6f 6e 2e 0a 20 20 20 20 2a 2a 20  version..    ** 
a1e10 20 20 69 6e 66 6f 70 20 20 20 20 20 20 20 20 20    infop         
a1e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 6f                Po
a1e30 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 70 70  inter to the app
a1e40 72 6f 70 72 69 61 74 65 20 69 6e 66 6f 20 73 74  ropriate info st
a1e50 72 75 63 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ruct..    */.   
a1e60 20 73 77 69 74 63 68 28 20 78 74 79 70 65 20 29   switch( xtype )
a1e70 7b 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 50  {.      case etP
a1e80 4f 49 4e 54 45 52 3a 0a 20 20 20 20 20 20 20 20  OINTER:.        
a1e90 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20  flag_longlong = 
a1ea0 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 3d 3d 73  sizeof(char*)==s
a1eb0 69 7a 65 6f 66 28 69 36 34 29 3b 0a 20 20 20 20  izeof(i64);.    
a1ec0 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 20 3d 20      flag_long = 
a1ed0 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 3d 3d 73  sizeof(char*)==s
a1ee0 69 7a 65 6f 66 28 6c 6f 6e 67 20 69 6e 74 29 3b  izeof(long int);
a1ef0 0a 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c  .        /* Fall
a1f00 20 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 74 68   through into th
a1f10 65 20 6e 65 78 74 20 63 61 73 65 20 2a 2f 0a 20  e next case */. 
a1f20 20 20 20 20 20 63 61 73 65 20 65 74 4f 52 44 49       case etORDI
a1f30 4e 41 4c 3a 0a 20 20 20 20 20 20 63 61 73 65 20  NAL:.      case 
a1f40 65 74 52 41 44 49 58 3a 0a 20 20 20 20 20 20 20  etRADIX:.       
a1f50 20 69 66 28 20 69 6e 66 6f 70 2d 3e 66 6c 61 67   if( infop->flag
a1f60 73 20 26 20 46 4c 41 47 5f 53 49 47 4e 45 44 20  s & FLAG_SIGNED 
a1f70 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 36 34  ){.          i64
a1f80 20 76 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66   v;.          if
a1f90 28 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20  ( flag_longlong 
a1fa0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 76  ){.            v
a1fb0 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 36 34   = va_arg(ap,i64
a1fc0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  );.          }el
a1fd0 73 65 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e 67  se if( flag_long
a1fe0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
a1ff0 76 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 6c 6f  v = va_arg(ap,lo
a2000 6e 67 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20  ng int);.       
a2010 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
a2020 20 20 20 20 20 20 76 20 3d 20 76 61 5f 61 72 67        v = va_arg
a2030 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20 20  (ap,int);.      
a2040 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
a2050 69 66 28 20 76 3c 30 20 29 7b 0a 20 20 20 20 20  if( v<0 ){.     
a2060 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65         longvalue
a2070 20 3d 20 2d 76 3b 0a 20 20 20 20 20 20 20 20 20   = -v;.         
a2080 20 20 20 70 72 65 66 69 78 20 3d 20 27 2d 27 3b     prefix = '-';
a2090 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
a20a0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f  {.            lo
a20b0 6e 67 76 61 6c 75 65 20 3d 20 76 3b 0a 20 20 20  ngvalue = v;.   
a20c0 20 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61           if( fla
a20d0 67 5f 70 6c 75 73 73 69 67 6e 20 29 20 20 20 20  g_plussign )    
a20e0 20 20 20 20 70 72 65 66 69 78 20 3d 20 27 2b 27      prefix = '+'
a20f0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 65 6c  ;.            el
a2100 73 65 20 69 66 28 20 66 6c 61 67 5f 62 6c 61 6e  se if( flag_blan
a2110 6b 73 69 67 6e 20 29 20 20 70 72 65 66 69 78 20  ksign )  prefix 
a2120 3d 20 27 20 27 3b 0a 20 20 20 20 20 20 20 20 20  = ' ';.         
a2130 20 20 20 65 6c 73 65 20 20 20 20 20 20 20 20 20     else         
a2140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 72                pr
a2150 65 66 69 78 20 3d 20 30 3b 0a 20 20 20 20 20 20  efix = 0;.      
a2160 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65      }.        }e
a2170 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 69  lse{.          i
a2180 66 28 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67  f( flag_longlong
a2190 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
a21a0 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 76 61 5f 61  longvalue = va_a
a21b0 72 67 28 61 70 2c 75 36 34 29 3b 0a 20 20 20 20  rg(ap,u64);.    
a21c0 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
a21d0 66 6c 61 67 5f 6c 6f 6e 67 20 29 7b 0a 20 20 20  flag_long ){.   
a21e0 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c           longval
a21f0 75 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 75  ue = va_arg(ap,u
a2200 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 69 6e 74  nsigned long int
a2210 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  );.          }el
a2220 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
a2230 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 76 61 5f 61  longvalue = va_a
a2240 72 67 28 61 70 2c 75 6e 73 69 67 6e 65 64 20 69  rg(ap,unsigned i
a2250 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  nt);.          }
a2260 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 66 69  .          prefi
a2270 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d  x = 0;.        }
a2280 0a 20 20 20 20 20 20 20 20 69 66 28 20 6c 6f 6e  .        if( lon
a2290 67 76 61 6c 75 65 3d 3d 30 20 29 20 66 6c 61 67  gvalue==0 ) flag
a22a0 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 3d  _alternateform =
a22b0 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   0;.        if( 
a22c0 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 26 26 20  flag_zeropad && 
a22d0 70 72 65 63 69 73 69 6f 6e 3c 77 69 64 74 68 2d  precision<width-
a22e0 28 70 72 65 66 69 78 21 3d 30 29 20 29 7b 0a 20  (prefix!=0) ){. 
a22f0 20 20 20 20 20 20 20 20 20 70 72 65 63 69 73 69           precisi
a2300 6f 6e 20 3d 20 77 69 64 74 68 2d 28 70 72 65 66  on = width-(pref
a2310 69 78 21 3d 30 29 3b 0a 20 20 20 20 20 20 20 20  ix!=0);.        
a2320 7d 0a 20 20 20 20 20 20 20 20 62 75 66 70 74 20  }.        bufpt 
a2330 3d 20 26 62 75 66 5b 65 74 42 55 46 53 49 5a 45  = &buf[etBUFSIZE
a2340 2d 31 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28  -1];.        if(
a2350 20 78 74 79 70 65 3d 3d 65 74 4f 52 44 49 4e 41   xtype==etORDINA
a2360 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  L ){.          s
a2370 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
a2380 20 7a 4f 72 64 5b 5d 20 3d 20 22 74 68 73 74 6e   zOrd[] = "thstn
a2390 64 72 64 22 3b 0a 20 20 20 20 20 20 20 20 20 20  drd";.          
a23a0 69 6e 74 20 78 20 3d 20 28 69 6e 74 29 28 6c 6f  int x = (int)(lo
a23b0 6e 67 76 61 6c 75 65 20 25 20 31 30 29 3b 0a 20  ngvalue % 10);. 
a23c0 20 20 20 20 20 20 20 20 20 69 66 28 20 78 3e 3d           if( x>=
a23d0 34 20 7c 7c 20 28 6c 6f 6e 67 76 61 6c 75 65 2f  4 || (longvalue/
a23e0 31 30 29 25 31 30 3d 3d 31 20 29 7b 0a 20 20 20  10)%10==1 ){.   
a23f0 20 20 20 20 20 20 20 20 20 78 20 3d 20 30 3b 0a           x = 0;.
a2400 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
a2410 20 20 20 20 20 20 62 75 66 5b 65 74 42 55 46 53        buf[etBUFS
a2420 49 5a 45 2d 33 5d 20 3d 20 7a 4f 72 64 5b 78 2a  IZE-3] = zOrd[x*
a2430 32 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 62 75  2];.          bu
a2440 66 5b 65 74 42 55 46 53 49 5a 45 2d 32 5d 20 3d  f[etBUFSIZE-2] =
a2450 20 7a 4f 72 64 5b 78 2a 32 2b 31 5d 3b 0a 20 20   zOrd[x*2+1];.  
a2460 20 20 20 20 20 20 20 20 62 75 66 70 74 20 2d 3d          bufpt -=
a2470 20 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   2;.        }.  
a2480 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20        {.        
a2490 20 20 72 65 67 69 73 74 65 72 20 63 6f 6e 73 74    register const
a24a0 20 63 68 61 72 20 2a 63 73 65 74 3b 20 20 20 20   char *cset;    
a24b0 20 20 2f 2a 20 55 73 65 20 72 65 67 69 73 74 65    /* Use registe
a24c0 72 73 20 66 6f 72 20 73 70 65 65 64 20 2a 2f 0a  rs for speed */.
a24d0 20 20 20 20 20 20 20 20 20 20 72 65 67 69 73 74            regist
a24e0 65 72 20 69 6e 74 20 62 61 73 65 3b 0a 20 20 20  er int base;.   
a24f0 20 20 20 20 20 20 20 63 73 65 74 20 3d 20 26 61         cset = &a
a2500 44 69 67 69 74 73 5b 69 6e 66 6f 70 2d 3e 63 68  Digits[infop->ch
a2510 61 72 73 65 74 5d 3b 0a 20 20 20 20 20 20 20 20  arset];.        
a2520 20 20 62 61 73 65 20 3d 20 69 6e 66 6f 70 2d 3e    base = infop->
a2530 62 61 73 65 3b 0a 20 20 20 20 20 20 20 20 20 20  base;.          
a2540 64 6f 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  do{             
a2550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
a2570 20 43 6f 6e 76 65 72 74 20 74 6f 20 61 73 63 69   Convert to asci
a2580 69 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  i */.           
a2590 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 63 73   *(--bufpt) = cs
a25a0 65 74 5b 6c 6f 6e 67 76 61 6c 75 65 25 62 61 73  et[longvalue%bas
a25b0 65 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  e];.            
a25c0 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 6c 6f 6e 67  longvalue = long
a25d0 76 61 6c 75 65 2f 62 61 73 65 3b 0a 20 20 20 20  value/base;.    
a25e0 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20 6c 6f        }while( lo
a25f0 6e 67 76 61 6c 75 65 3e 30 20 29 3b 0a 20 20 20  ngvalue>0 );.   
a2600 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6c       }.        l
a2610 65 6e 67 74 68 20 3d 20 28 69 6e 74 29 28 26 62  ength = (int)(&b
a2620 75 66 5b 65 74 42 55 46 53 49 5a 45 2d 31 5d 2d  uf[etBUFSIZE-1]-
a2630 62 75 66 70 74 29 3b 0a 20 20 20 20 20 20 20 20  bufpt);.        
a2640 66 6f 72 28 69 64 78 3d 70 72 65 63 69 73 69 6f  for(idx=precisio
a2650 6e 2d 6c 65 6e 67 74 68 3b 20 69 64 78 3e 30 3b  n-length; idx>0;
a2660 20 69 64 78 2d 2d 29 7b 0a 20 20 20 20 20 20 20   idx--){.       
a2670 20 20 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20     *(--bufpt) = 
a2680 27 30 27 3b 20 20 20 20 20 20 20 20 20 20 20 20  '0';            
a2690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a26a0 20 2f 2a 20 5a 65 72 6f 20 70 61 64 20 2a 2f 0a   /* Zero pad */.
a26b0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a26c0 20 20 69 66 28 20 70 72 65 66 69 78 20 29 20 2a    if( prefix ) *
a26d0 28 2d 2d 62 75 66 70 74 29 20 3d 20 70 72 65 66  (--bufpt) = pref
a26e0 69 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ix;             
a26f0 20 20 2f 2a 20 41 64 64 20 73 69 67 6e 20 2a 2f    /* Add sign */
a2700 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61  .        if( fla
a2710 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20  g_alternateform 
a2720 26 26 20 69 6e 66 6f 70 2d 3e 70 72 65 66 69 78  && infop->prefix
a2730 20 29 7b 20 20 20 20 20 20 2f 2a 20 41 64 64 20   ){      /* Add 
a2740 22 30 22 20 6f 72 20 22 30 78 22 20 2a 2f 0a 20  "0" or "0x" */. 
a2750 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63           const c
a2760 68 61 72 20 2a 70 72 65 3b 0a 20 20 20 20 20 20  har *pre;.      
a2770 20 20 20 20 63 68 61 72 20 78 3b 0a 20 20 20 20      char x;.    
a2780 20 20 20 20 20 20 70 72 65 20 3d 20 26 61 50 72        pre = &aPr
a2790 65 66 69 78 5b 69 6e 66 6f 70 2d 3e 70 72 65 66  efix[infop->pref
a27a0 69 78 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 66  ix];.          f
a27b0 6f 72 28 3b 20 28 78 3d 28 2a 70 72 65 29 29 21  or(; (x=(*pre))!
a27c0 3d 30 3b 20 70 72 65 2b 2b 29 20 2a 28 2d 2d 62  =0; pre++) *(--b
a27d0 75 66 70 74 29 20 3d 20 78 3b 0a 20 20 20 20 20  ufpt) = x;.     
a27e0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6c 65 6e     }.        len
a27f0 67 74 68 20 3d 20 28 69 6e 74 29 28 26 62 75 66  gth = (int)(&buf
a2800 5b 65 74 42 55 46 53 49 5a 45 2d 31 5d 2d 62 75  [etBUFSIZE-1]-bu
a2810 66 70 74 29 3b 0a 20 20 20 20 20 20 20 20 62 72  fpt);.        br
a2820 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
a2830 65 74 46 4c 4f 41 54 3a 0a 20 20 20 20 20 20 63  etFLOAT:.      c
a2840 61 73 65 20 65 74 45 58 50 3a 0a 20 20 20 20 20  ase etEXP:.     
a2850 20 63 61 73 65 20 65 74 47 45 4e 45 52 49 43 3a   case etGENERIC:
a2860 0a 20 20 20 20 20 20 20 20 72 65 61 6c 76 61 6c  .        realval
a2870 75 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 64  ue = va_arg(ap,d
a2880 6f 75 62 6c 65 29 3b 0a 23 69 66 6e 64 65 66 20  ouble);.#ifndef 
a2890 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41  SQLITE_OMIT_FLOA
a28a0 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 20  TING_POINT.     
a28b0 20 20 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e     if( precision
a28c0 3c 30 20 29 20 70 72 65 63 69 73 69 6f 6e 20 3d  <0 ) precision =
a28d0 20 36 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53   6;         /* S
a28e0 65 74 20 64 65 66 61 75 6c 74 20 70 72 65 63 69  et default preci
a28f0 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20  sion */.        
a2900 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3e 65 74  if( precision>et
a2910 42 55 46 53 49 5a 45 2f 32 2d 31 30 20 29 20 70  BUFSIZE/2-10 ) p
a2920 72 65 63 69 73 69 6f 6e 20 3d 20 65 74 42 55 46  recision = etBUF
a2930 53 49 5a 45 2f 32 2d 31 30 3b 0a 20 20 20 20 20  SIZE/2-10;.     
a2940 20 20 20 69 66 28 20 72 65 61 6c 76 61 6c 75 65     if( realvalue
a2950 3c 30 2e 30 20 29 7b 0a 20 20 20 20 20 20 20 20  <0.0 ){.        
a2960 20 20 72 65 61 6c 76 61 6c 75 65 20 3d 20 2d 72    realvalue = -r
a2970 65 61 6c 76 61 6c 75 65 3b 0a 20 20 20 20 20 20  ealvalue;.      
a2980 20 20 20 20 70 72 65 66 69 78 20 3d 20 27 2d 27      prefix = '-'
a2990 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
a29a0 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 66  .          if( f
a29b0 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 29 20 20  lag_plussign )  
a29c0 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d          prefix =
a29d0 20 27 2b 27 3b 0a 20 20 20 20 20 20 20 20 20 20   '+';.          
a29e0 65 6c 73 65 20 69 66 28 20 66 6c 61 67 5f 62 6c  else if( flag_bl
a29f0 61 6e 6b 73 69 67 6e 20 29 20 20 20 20 70 72 65  anksign )    pre
a2a00 66 69 78 20 3d 20 27 20 27 3b 0a 20 20 20 20 20  fix = ' ';.     
a2a10 20 20 20 20 20 65 6c 73 65 20 20 20 20 20 20 20       else       
a2a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2a30 20 20 70 72 65 66 69 78 20 3d 20 30 3b 0a 20 20    prefix = 0;.  
a2a40 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
a2a50 69 66 28 20 78 74 79 70 65 3d 3d 65 74 47 45 4e  if( xtype==etGEN
a2a60 45 52 49 43 20 26 26 20 70 72 65 63 69 73 69 6f  ERIC && precisio
a2a70 6e 3e 30 20 29 20 70 72 65 63 69 73 69 6f 6e 2d  n>0 ) precision-
a2a80 2d 3b 0a 23 69 66 20 30 0a 20 20 20 20 20 20 20  -;.#if 0.       
a2a90 20 2f 2a 20 52 6f 75 6e 64 69 6e 67 20 77 6f 72   /* Rounding wor
a2aa0 6b 73 20 6c 69 6b 65 20 42 53 44 20 77 68 65 6e  ks like BSD when
a2ab0 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 30 2e   the constant 0.
a2ac0 34 39 39 39 20 69 73 20 75 73 65 64 2e 20 20 57  4999 is used.  W
a2ad0 69 65 72 64 21 20 2a 2f 0a 20 20 20 20 20 20 20  ierd! */.       
a2ae0 20 66 6f 72 28 69 64 78 3d 70 72 65 63 69 73 69   for(idx=precisi
a2af0 6f 6e 2c 20 72 6f 75 6e 64 65 72 3d 30 2e 34 39  on, rounder=0.49
a2b00 39 39 3b 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d  99; idx>0; idx--
a2b10 2c 20 72 6f 75 6e 64 65 72 2a 3d 30 2e 31 29 3b  , rounder*=0.1);
a2b20 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20 2f  .#else.        /
a2b30 2a 20 49 74 20 6d 61 6b 65 73 20 6d 6f 72 65 20  * It makes more 
a2b40 73 65 6e 73 65 20 74 6f 20 75 73 65 20 30 2e 35  sense to use 0.5
a2b50 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28   */.        for(
a2b60 69 64 78 3d 70 72 65 63 69 73 69 6f 6e 2c 20 72  idx=precision, r
a2b70 6f 75 6e 64 65 72 3d 30 2e 35 3b 20 69 64 78 3e  ounder=0.5; idx>
a2b80 30 3b 20 69 64 78 2d 2d 2c 20 72 6f 75 6e 64 65  0; idx--, rounde
a2b90 72 2a 3d 30 2e 31 29 7b 7d 0a 23 65 6e 64 69 66  r*=0.1){}.#endif
a2ba0 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 74 79  .        if( xty
a2bb0 70 65 3d 3d 65 74 46 4c 4f 41 54 20 29 20 72 65  pe==etFLOAT ) re
a2bc0 61 6c 76 61 6c 75 65 20 2b 3d 20 72 6f 75 6e 64  alvalue += round
a2bd0 65 72 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e  er;.        /* N
a2be0 6f 72 6d 61 6c 69 7a 65 20 72 65 61 6c 76 61 6c  ormalize realval
a2bf0 75 65 20 74 6f 20 77 69 74 68 69 6e 20 31 30 2e  ue to within 10.
a2c00 30 20 3e 20 72 65 61 6c 76 61 6c 75 65 20 3e 3d  0 > realvalue >=
a2c10 20 31 2e 30 20 2a 2f 0a 20 20 20 20 20 20 20 20   1.0 */.        
a2c20 65 78 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  exp = 0;.       
a2c30 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e 61   if( sqlite3IsNa
a2c40 4e 28 28 64 6f 75 62 6c 65 29 72 65 61 6c 76 61  N((double)realva
a2c50 6c 75 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20  lue) ){.        
a2c60 20 20 62 75 66 70 74 20 3d 20 22 4e 61 4e 22 3b    bufpt = "NaN";
a2c70 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74  .          lengt
a2c80 68 20 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 20  h = 3;.         
a2c90 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
a2ca0 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 65  }.        if( re
a2cb0 61 6c 76 61 6c 75 65 3e 30 2e 30 20 29 7b 0a 20  alvalue>0.0 ){. 
a2cc0 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20           while( 
a2cd0 72 65 61 6c 76 61 6c 75 65 3e 3d 31 65 33 32 20  realvalue>=1e32 
a2ce0 26 26 20 65 78 70 3c 3d 33 35 30 20 29 7b 20 72  && exp<=350 ){ r
a2cf0 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31 65 2d 33  ealvalue *= 1e-3
a2d00 32 3b 20 65 78 70 2b 3d 33 32 3b 20 7d 0a 20 20  2; exp+=32; }.  
a2d10 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 72          while( r
a2d20 65 61 6c 76 61 6c 75 65 3e 3d 31 65 38 20 26 26  ealvalue>=1e8 &&
a2d30 20 65 78 70 3c 3d 33 35 30 20 29 7b 20 72 65 61   exp<=350 ){ rea
a2d40 6c 76 61 6c 75 65 20 2a 3d 20 31 65 2d 38 3b 20  lvalue *= 1e-8; 
a2d50 65 78 70 2b 3d 38 3b 20 7d 0a 20 20 20 20 20 20  exp+=8; }.      
a2d60 20 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76      while( realv
a2d70 61 6c 75 65 3e 3d 31 30 2e 30 20 26 26 20 65 78  alue>=10.0 && ex
a2d80 70 3c 3d 33 35 30 20 29 7b 20 72 65 61 6c 76 61  p<=350 ){ realva
a2d90 6c 75 65 20 2a 3d 20 30 2e 31 3b 20 65 78 70 2b  lue *= 0.1; exp+
a2da0 2b 3b 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77  +; }.          w
a2db0 68 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3c  hile( realvalue<
a2dc0 31 65 2d 38 20 29 7b 20 72 65 61 6c 76 61 6c 75  1e-8 ){ realvalu
a2dd0 65 20 2a 3d 20 31 65 38 3b 20 65 78 70 2d 3d 38  e *= 1e8; exp-=8
a2de0 3b 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68  ; }.          wh
a2df0 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3c 31  ile( realvalue<1
a2e00 2e 30 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20  .0 ){ realvalue 
a2e10 2a 3d 20 31 30 2e 30 3b 20 65 78 70 2d 2d 3b 20  *= 10.0; exp--; 
a2e20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  }.          if( 
a2e30 65 78 70 3e 33 35 30 20 29 7b 0a 20 20 20 20 20  exp>350 ){.     
a2e40 20 20 20 20 20 20 20 69 66 28 20 70 72 65 66 69         if( prefi
a2e50 78 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20 20  x=='-' ){.      
a2e60 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20          bufpt = 
a2e70 22 2d 49 6e 66 22 3b 0a 20 20 20 20 20 20 20 20  "-Inf";.        
a2e80 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 72      }else if( pr
a2e90 65 66 69 78 3d 3d 27 2b 27 20 29 7b 0a 20 20 20  efix=='+' ){.   
a2ea0 20 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74             bufpt
a2eb0 20 3d 20 22 2b 49 6e 66 22 3b 0a 20 20 20 20 20   = "+Inf";.     
a2ec0 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
a2ed0 20 20 20 20 20 20 20 20 20 20 20 20 62 75 66 70              bufp
a2ee0 74 20 3d 20 22 49 6e 66 22 3b 0a 20 20 20 20 20  t = "Inf";.     
a2ef0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
a2f00 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 73 71       length = sq
a2f10 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 62 75  lite3Strlen30(bu
a2f20 66 70 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20  fpt);.          
a2f30 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
a2f40 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
a2f50 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 62         bufpt = b
a2f60 75 66 3b 0a 20 20 20 20 20 20 20 20 2f 2a 0a 20  uf;.        /*. 
a2f70 20 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65         ** If the
a2f80 20 66 69 65 6c 64 20 74 79 70 65 20 69 73 20 65   field type is e
a2f90 74 47 45 4e 45 52 49 43 2c 20 74 68 65 6e 20 63  tGENERIC, then c
a2fa0 6f 6e 76 65 72 74 20 74 6f 20 65 69 74 68 65 72  onvert to either
a2fb0 20 65 74 45 58 50 0a 20 20 20 20 20 20 20 20 2a   etEXP.        *
a2fc0 2a 20 6f 72 20 65 74 46 4c 4f 41 54 2c 20 61 73  * or etFLOAT, as
a2fd0 20 61 70 70 72 6f 70 72 69 61 74 65 2e 0a 20 20   appropriate..  
a2fe0 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
a2ff0 20 66 6c 61 67 5f 65 78 70 20 3d 20 78 74 79 70   flag_exp = xtyp
a3000 65 3d 3d 65 74 45 58 50 3b 0a 20 20 20 20 20 20  e==etEXP;.      
a3010 20 20 69 66 28 20 78 74 79 70 65 21 3d 65 74 46    if( xtype!=etF
a3020 4c 4f 41 54 20 29 7b 0a 20 20 20 20 20 20 20 20  LOAT ){.        
a3030 20 20 72 65 61 6c 76 61 6c 75 65 20 2b 3d 20 72    realvalue += r
a3040 6f 75 6e 64 65 72 3b 0a 20 20 20 20 20 20 20 20  ounder;.        
a3050 20 20 69 66 28 20 72 65 61 6c 76 61 6c 75 65 3e    if( realvalue>
a3060 3d 31 30 2e 30 20 29 7b 20 72 65 61 6c 76 61 6c  =10.0 ){ realval
a3070 75 65 20 2a 3d 20 30 2e 31 3b 20 65 78 70 2b 2b  ue *= 0.1; exp++
a3080 3b 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ; }.        }.  
a3090 20 20 20 20 20 20 69 66 28 20 78 74 79 70 65 3d        if( xtype=
a30a0 3d 65 74 47 45 4e 45 52 49 43 20 29 7b 0a 20 20  =etGENERIC ){.  
a30b0 20 20 20 20 20 20 20 20 66 6c 61 67 5f 72 74 7a          flag_rtz
a30c0 20 3d 20 21 66 6c 61 67 5f 61 6c 74 65 72 6e 61   = !flag_alterna
a30d0 74 65 66 6f 72 6d 3b 0a 20 20 20 20 20 20 20 20  teform;.        
a30e0 20 20 69 66 28 20 65 78 70 3c 2d 34 20 7c 7c 20    if( exp<-4 || 
a30f0 65 78 70 3e 70 72 65 63 69 73 69 6f 6e 20 29 7b  exp>precision ){
a3100 0a 20 20 20 20 20 20 20 20 20 20 20 20 78 74 79  .            xty
a3110 70 65 20 3d 20 65 74 45 58 50 3b 0a 20 20 20 20  pe = etEXP;.    
a3120 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
a3130 20 20 20 20 20 20 20 20 20 70 72 65 63 69 73 69           precisi
a3140 6f 6e 20 3d 20 70 72 65 63 69 73 69 6f 6e 20 2d  on = precision -
a3150 20 65 78 70 3b 0a 20 20 20 20 20 20 20 20 20 20   exp;.          
a3160 20 20 78 74 79 70 65 20 3d 20 65 74 46 4c 4f 41    xtype = etFLOA
a3170 54 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  T;.          }. 
a3180 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
a3190 20 20 20 20 20 20 20 20 66 6c 61 67 5f 72 74 7a          flag_rtz
a31a0 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = 0;.        }.
a31b0 20 20 20 20 20 20 20 20 69 66 28 20 78 74 79 70          if( xtyp
a31c0 65 3d 3d 65 74 45 58 50 20 29 7b 0a 20 20 20 20  e==etEXP ){.    
a31d0 20 20 20 20 20 20 65 32 20 3d 20 30 3b 0a 20 20        e2 = 0;.  
a31e0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
a31f0 20 20 20 20 20 20 20 65 32 20 3d 20 65 78 70 3b         e2 = exp;
a3200 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
a3210 20 20 20 6e 73 64 20 3d 20 30 3b 0a 20 20 20 20     nsd = 0;.    
a3220 20 20 20 20 66 6c 61 67 5f 64 70 20 3d 20 28 70      flag_dp = (p
a3230 72 65 63 69 73 69 6f 6e 3e 30 20 3f 31 3a 30 29  recision>0 ?1:0)
a3240 20 7c 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74   | flag_alternat
a3250 65 66 6f 72 6d 20 7c 20 66 6c 61 67 5f 61 6c 74  eform | flag_alt
a3260 66 6f 72 6d 32 3b 0a 20 20 20 20 20 20 20 20 2f  form2;.        /
a3270 2a 20 54 68 65 20 73 69 67 6e 20 69 6e 20 66 72  * The sign in fr
a3280 6f 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65  ont of the numbe
a3290 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28  r */.        if(
a32a0 20 70 72 65 66 69 78 20 29 7b 0a 20 20 20 20 20   prefix ){.     
a32b0 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20       *(bufpt++) 
a32c0 3d 20 70 72 65 66 69 78 3b 0a 20 20 20 20 20 20  = prefix;.      
a32d0 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 44    }.        /* D
a32e0 69 67 69 74 73 20 70 72 69 6f 72 20 74 6f 20 74  igits prior to t
a32f0 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74  he decimal point
a3300 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
a3310 65 32 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20  e2<0 ){.        
a3320 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27    *(bufpt++) = '
a3330 30 27 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  0';.        }els
a3340 65 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72  e{.          for
a3350 28 3b 20 65 32 3e 3d 30 3b 20 65 32 2d 2d 29 7b  (; e2>=0; e2--){
a3360 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 62  .            *(b
a3370 75 66 70 74 2b 2b 29 20 3d 20 65 74 5f 67 65 74  ufpt++) = et_get
a3380 64 69 67 69 74 28 26 72 65 61 6c 76 61 6c 75 65  digit(&realvalue
a3390 2c 26 6e 73 64 29 3b 0a 20 20 20 20 20 20 20 20  ,&nsd);.        
a33a0 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
a33b0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 65 63        /* The dec
a33c0 69 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20  imal point */.  
a33d0 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 64        if( flag_d
a33e0 70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a  p ){.          *
a33f0 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 2e 27 3b  (bufpt++) = '.';
a3400 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
a3410 20 20 20 2f 2a 20 22 30 22 20 64 69 67 69 74 73     /* "0" digits
a3420 20 61 66 74 65 72 20 74 68 65 20 64 65 63 69 6d   after the decim
a3430 61 6c 20 70 6f 69 6e 74 20 62 75 74 20 62 65 66  al point but bef
a3440 6f 72 65 20 74 68 65 20 66 69 72 73 74 0a 20 20  ore the first.  
a3450 20 20 20 20 20 20 2a 2a 20 73 69 67 6e 69 66 69        ** signifi
a3460 63 61 6e 74 20 64 69 67 69 74 20 6f 66 20 74 68  cant digit of th
a3470 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20  e number */.    
a3480 20 20 20 20 66 6f 72 28 65 32 2b 2b 3b 20 65 32      for(e2++; e2
a3490 3c 30 3b 20 70 72 65 63 69 73 69 6f 6e 2d 2d 2c  <0; precision--,
a34a0 20 65 32 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20   e2++){.        
a34b0 20 20 61 73 73 65 72 74 28 20 70 72 65 63 69 73    assert( precis
a34c0 69 6f 6e 3e 30 20 29 3b 0a 20 20 20 20 20 20 20  ion>0 );.       
a34d0 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20     *(bufpt++) = 
a34e0 27 30 27 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  '0';.        }. 
a34f0 20 20 20 20 20 20 20 2f 2a 20 53 69 67 6e 69 66         /* Signif
a3500 69 63 61 6e 74 20 64 69 67 69 74 73 20 61 66 74  icant digits aft
a3510 65 72 20 74 68 65 20 64 65 63 69 6d 61 6c 20 70  er the decimal p
a3520 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20  oint */.        
a3530 77 68 69 6c 65 28 20 28 70 72 65 63 69 73 69 6f  while( (precisio
a3540 6e 2d 2d 29 3e 30 20 29 7b 0a 20 20 20 20 20 20  n--)>0 ){.      
a3550 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d      *(bufpt++) =
a3560 20 65 74 5f 67 65 74 64 69 67 69 74 28 26 72 65   et_getdigit(&re
a3570 61 6c 76 61 6c 75 65 2c 26 6e 73 64 29 3b 0a 20  alvalue,&nsd);. 
a3580 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
a3590 20 2f 2a 20 52 65 6d 6f 76 65 20 74 72 61 69 6c   /* Remove trail
a35a0 69 6e 67 20 7a 65 72 6f 73 20 61 6e 64 20 74 68  ing zeros and th
a35b0 65 20 22 2e 22 20 69 66 20 6e 6f 20 64 69 67 69  e "." if no digi
a35c0 74 73 20 66 6f 6c 6c 6f 77 20 74 68 65 20 22 2e  ts follow the ".
a35d0 22 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28  " */.        if(
a35e0 20 66 6c 61 67 5f 72 74 7a 20 26 26 20 66 6c 61   flag_rtz && fla
a35f0 67 5f 64 70 20 29 7b 0a 20 20 20 20 20 20 20 20  g_dp ){.        
a3600 20 20 77 68 69 6c 65 28 20 62 75 66 70 74 5b 2d    while( bufpt[-
a3610 31 5d 3d 3d 27 30 27 20 29 20 2a 28 2d 2d 62 75  1]=='0' ) *(--bu
a3620 66 70 74 29 20 3d 20 30 3b 0a 20 20 20 20 20 20  fpt) = 0;.      
a3630 20 20 20 20 61 73 73 65 72 74 28 20 62 75 66 70      assert( bufp
a3640 74 3e 62 75 66 20 29 3b 0a 20 20 20 20 20 20 20  t>buf );.       
a3650 20 20 20 69 66 28 20 62 75 66 70 74 5b 2d 31 5d     if( bufpt[-1]
a3660 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20 20 20 20  =='.' ){.       
a3670 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 61 6c       if( flag_al
a3680 74 66 6f 72 6d 32 20 29 7b 0a 20 20 20 20 20 20  tform2 ){.      
a3690 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b          *(bufpt+
a36a0 2b 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 20  +) = '0';.      
a36b0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
a36c0 20 20 20 20 20 20 20 20 20 20 20 2a 28 2d 2d 62             *(--b
a36d0 75 66 70 74 29 20 3d 20 30 3b 0a 20 20 20 20 20  ufpt) = 0;.     
a36e0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
a36f0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
a3700 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20 74 68         /* Add th
a3710 65 20 22 65 4e 4e 4e 22 20 73 75 66 66 69 78 20  e "eNNN" suffix 
a3720 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 66  */.        if( f
a3730 6c 61 67 5f 65 78 70 20 7c 7c 20 78 74 79 70 65  lag_exp || xtype
a3740 3d 3d 65 74 45 58 50 20 29 7b 0a 20 20 20 20 20  ==etEXP ){.     
a3750 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20       *(bufpt++) 
a3760 3d 20 61 44 69 67 69 74 73 5b 69 6e 66 6f 70 2d  = aDigits[infop-
a3770 3e 63 68 61 72 73 65 74 5d 3b 0a 20 20 20 20 20  >charset];.     
a3780 20 20 20 20 20 69 66 28 20 65 78 70 3c 30 20 29       if( exp<0 )
a3790 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 28  {.            *(
a37a0 62 75 66 70 74 2b 2b 29 20 3d 20 27 2d 27 3b 20  bufpt++) = '-'; 
a37b0 65 78 70 20 3d 20 2d 65 78 70 3b 0a 20 20 20 20  exp = -exp;.    
a37c0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
a37d0 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74           *(bufpt
a37e0 2b 2b 29 20 3d 20 27 2b 27 3b 0a 20 20 20 20 20  ++) = '+';.     
a37f0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
a3800 20 69 66 28 20 65 78 70 3e 3d 31 30 30 20 29 7b   if( exp>=100 ){
a3810 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 62  .            *(b
a3820 75 66 70 74 2b 2b 29 20 3d 20 28 63 68 61 72 29  ufpt++) = (char)
a3830 28 28 65 78 70 2f 31 30 30 29 2b 27 30 27 29 3b  ((exp/100)+'0');
a3840 20 20 20 20 20 20 20 20 2f 2a 20 31 30 30 27 73          /* 100's
a3850 20 64 69 67 69 74 20 2a 2f 0a 20 20 20 20 20 20   digit */.      
a3860 20 20 20 20 20 20 65 78 70 20 25 3d 20 31 30 30        exp %= 100
a3870 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
a3880 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b          *(bufpt+
a3890 2b 29 20 3d 20 28 63 68 61 72 29 28 65 78 70 2f  +) = (char)(exp/
a38a0 31 30 2b 27 30 27 29 3b 20 20 20 20 20 20 20 20  10+'0');        
a38b0 20 20 20 20 20 2f 2a 20 31 30 27 73 20 64 69 67       /* 10's dig
a38c0 69 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  it */.          
a38d0 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 28 63 68  *(bufpt++) = (ch
a38e0 61 72 29 28 65 78 70 25 31 30 2b 27 30 27 29 3b  ar)(exp%10+'0');
a38f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a3900 31 27 73 20 64 69 67 69 74 20 2a 2f 0a 20 20 20  1's digit */.   
a3910 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2a       }.        *
a3920 62 75 66 70 74 20 3d 20 30 3b 0a 0a 20 20 20 20  bufpt = 0;..    
a3930 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 76 65      /* The conve
a3940 72 74 65 64 20 6e 75 6d 62 65 72 20 69 73 20 69  rted number is i
a3950 6e 20 62 75 66 5b 5d 20 61 6e 64 20 7a 65 72 6f  n buf[] and zero
a3960 20 74 65 72 6d 69 6e 61 74 65 64 2e 20 4f 75 74   terminated. Out
a3970 70 75 74 20 69 74 2e 0a 20 20 20 20 20 20 20 20  put it..        
a3980 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65  ** Note that the
a3990 20 6e 75 6d 62 65 72 20 69 73 20 69 6e 20 74 68   number is in th
a39a0 65 20 75 73 75 61 6c 20 6f 72 64 65 72 2c 20 6e  e usual order, n
a39b0 6f 74 20 72 65 76 65 72 73 65 64 20 61 73 20 77  ot reversed as w
a39c0 69 74 68 0a 20 20 20 20 20 20 20 20 2a 2a 20 69  ith.        ** i
a39d0 6e 74 65 67 65 72 20 63 6f 6e 76 65 72 73 69 6f  nteger conversio
a39e0 6e 73 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 6c  ns. */.        l
a39f0 65 6e 67 74 68 20 3d 20 28 69 6e 74 29 28 62 75  ength = (int)(bu
a3a00 66 70 74 2d 62 75 66 29 3b 0a 20 20 20 20 20 20  fpt-buf);.      
a3a10 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 0a    bufpt = buf;..
a3a20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 65 63 69          /* Speci
a3a30 61 6c 20 63 61 73 65 3a 20 20 41 64 64 20 6c 65  al case:  Add le
a3a40 61 64 69 6e 67 20 7a 65 72 6f 73 20 69 66 20 74  ading zeros if t
a3a50 68 65 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20  he flag_zeropad 
a3a60 66 6c 61 67 20 69 73 0a 20 20 20 20 20 20 20 20  flag is.        
a3a70 2a 2a 20 73 65 74 20 61 6e 64 20 77 65 20 61 72  ** set and we ar
a3a80 65 20 6e 6f 74 20 6c 65 66 74 20 6a 75 73 74 69  e not left justi
a3a90 66 69 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20  fied */.        
a3aa0 69 66 28 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64  if( flag_zeropad
a3ab0 20 26 26 20 21 66 6c 61 67 5f 6c 65 66 74 6a 75   && !flag_leftju
a3ac0 73 74 69 66 79 20 26 26 20 6c 65 6e 67 74 68 20  stify && length 
a3ad0 3c 20 77 69 64 74 68 29 7b 0a 20 20 20 20 20 20  < width){.      
a3ae0 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20      int i;.     
a3af0 20 20 20 20 20 69 6e 74 20 6e 50 61 64 20 3d 20       int nPad = 
a3b00 77 69 64 74 68 20 2d 20 6c 65 6e 67 74 68 3b 0a  width - length;.
a3b10 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d            for(i=
a3b20 77 69 64 74 68 3b 20 69 3e 3d 6e 50 61 64 3b 20  width; i>=nPad; 
a3b30 69 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20 20  i--){.          
a3b40 20 20 62 75 66 70 74 5b 69 5d 20 3d 20 62 75 66    bufpt[i] = buf
a3b50 70 74 5b 69 2d 6e 50 61 64 5d 3b 0a 20 20 20 20  pt[i-nPad];.    
a3b60 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
a3b70 20 20 69 20 3d 20 70 72 65 66 69 78 21 3d 30 3b    i = prefix!=0;
a3b80 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65  .          while
a3b90 28 20 6e 50 61 64 2d 2d 20 29 20 62 75 66 70 74  ( nPad-- ) bufpt
a3ba0 5b 69 2b 2b 5d 20 3d 20 27 30 27 3b 0a 20 20 20  [i++] = '0';.   
a3bb0 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20         length = 
a3bc0 77 69 64 74 68 3b 0a 20 20 20 20 20 20 20 20 7d  width;.        }
a3bd0 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20  .#endif.        
a3be0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
a3bf0 65 20 65 74 53 49 5a 45 3a 0a 20 20 20 20 20 20  e etSIZE:.      
a3c00 20 20 2a 28 76 61 5f 61 72 67 28 61 70 2c 69 6e    *(va_arg(ap,in
a3c10 74 2a 29 29 20 3d 20 70 41 63 63 75 6d 2d 3e 6e  t*)) = pAccum->n
a3c20 43 68 61 72 3b 0a 20 20 20 20 20 20 20 20 6c 65  Char;.        le
a3c30 6e 67 74 68 20 3d 20 77 69 64 74 68 20 3d 20 30  ngth = width = 0
a3c40 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
a3c50 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 50 45  .      case etPE
a3c60 52 43 45 4e 54 3a 0a 20 20 20 20 20 20 20 20 62  RCENT:.        b
a3c70 75 66 5b 30 5d 20 3d 20 27 25 27 3b 0a 20 20 20  uf[0] = '%';.   
a3c80 20 20 20 20 20 62 75 66 70 74 20 3d 20 62 75 66       bufpt = buf
a3c90 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68  ;.        length
a3ca0 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 62 72   = 1;.        br
a3cb0 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
a3cc0 65 74 43 48 41 52 58 3a 0a 20 20 20 20 20 20 20  etCHARX:.       
a3cd0 20 63 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69   c = va_arg(ap,i
a3ce0 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 62 75 66  nt);.        buf
a3cf0 5b 30 5d 20 3d 20 28 63 68 61 72 29 63 3b 0a 20  [0] = (char)c;. 
a3d00 20 20 20 20 20 20 20 69 66 28 20 70 72 65 63 69         if( preci
a3d10 73 69 6f 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 20  sion>=0 ){.     
a3d20 20 20 20 20 20 66 6f 72 28 69 64 78 3d 31 3b 20       for(idx=1; 
a3d30 69 64 78 3c 70 72 65 63 69 73 69 6f 6e 3b 20 69  idx<precision; i
a3d40 64 78 2b 2b 29 20 62 75 66 5b 69 64 78 5d 20 3d  dx++) buf[idx] =
a3d50 20 28 63 68 61 72 29 63 3b 0a 20 20 20 20 20 20   (char)c;.      
a3d60 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 70 72 65      length = pre
a3d70 63 69 73 69 6f 6e 3b 0a 20 20 20 20 20 20 20 20  cision;.        
a3d80 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
a3d90 20 6c 65 6e 67 74 68 20 3d 31 3b 0a 20 20 20 20   length =1;.    
a3da0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 75      }.        bu
a3db0 66 70 74 20 3d 20 62 75 66 3b 0a 20 20 20 20 20  fpt = buf;.     
a3dc0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
a3dd0 63 61 73 65 20 65 74 53 54 52 49 4e 47 3a 0a 20  case etSTRING:. 
a3de0 20 20 20 20 20 63 61 73 65 20 65 74 44 59 4e 53       case etDYNS
a3df0 54 52 49 4e 47 3a 0a 20 20 20 20 20 20 20 20 62  TRING:.        b
a3e00 75 66 70 74 20 3d 20 76 61 5f 61 72 67 28 61 70  ufpt = va_arg(ap
a3e10 2c 63 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 20  ,char*);.       
a3e20 20 69 66 28 20 62 75 66 70 74 3d 3d 30 20 29 7b   if( bufpt==0 ){
a3e30 0a 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74  .          bufpt
a3e40 20 3d 20 22 22 3b 0a 20 20 20 20 20 20 20 20 7d   = "";.        }
a3e50 65 6c 73 65 20 69 66 28 20 78 74 79 70 65 3d 3d  else if( xtype==
a3e60 65 74 44 59 4e 53 54 52 49 4e 47 20 29 7b 0a 20  etDYNSTRING ){. 
a3e70 20 20 20 20 20 20 20 20 20 7a 45 78 74 72 61 20           zExtra 
a3e80 3d 20 62 75 66 70 74 3b 0a 20 20 20 20 20 20 20  = bufpt;.       
a3e90 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70   }.        if( p
a3ea0 72 65 63 69 73 69 6f 6e 3e 3d 30 20 29 7b 0a 20  recision>=0 ){. 
a3eb0 20 20 20 20 20 20 20 20 20 66 6f 72 28 6c 65 6e           for(len
a3ec0 67 74 68 3d 30 3b 20 6c 65 6e 67 74 68 3c 70 72  gth=0; length<pr
a3ed0 65 63 69 73 69 6f 6e 20 26 26 20 62 75 66 70 74  ecision && bufpt
a3ee0 5b 6c 65 6e 67 74 68 5d 3b 20 6c 65 6e 67 74 68  [length]; length
a3ef0 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 20 20 7d 65  ++){}.        }e
a3f00 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 6c  lse{.          l
a3f10 65 6e 67 74 68 20 3d 20 73 71 6c 69 74 65 33 53  ength = sqlite3S
a3f20 74 72 6c 65 6e 33 30 28 62 75 66 70 74 29 3b 0a  trlen30(bufpt);.
a3f30 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a3f40 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
a3f50 61 73 65 20 65 74 53 51 4c 45 53 43 41 50 45 3a  ase etSQLESCAPE:
a3f60 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 53 51  .      case etSQ
a3f70 4c 45 53 43 41 50 45 32 3a 0a 20 20 20 20 20 20  LESCAPE2:.      
a3f80 63 61 73 65 20 65 74 53 51 4c 45 53 43 41 50 45  case etSQLESCAPE
a3f90 33 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  3: {.        int
a3fa0 20 69 2c 20 6a 2c 20 6e 2c 20 69 73 6e 75 6c 6c   i, j, n, isnull
a3fb0 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 65  ;.        int ne
a3fc0 65 64 51 75 6f 74 65 3b 0a 20 20 20 20 20 20 20  edQuote;.       
a3fd0 20 63 68 61 72 20 63 68 3b 0a 20 20 20 20 20 20   char ch;.      
a3fe0 20 20 63 68 61 72 20 71 20 3d 20 28 28 78 74 79    char q = ((xty
a3ff0 70 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 45 33  pe==etSQLESCAPE3
a4000 29 3f 27 22 27 3a 27 5c 27 27 29 3b 20 20 20 2f  )?'"':'\'');   /
a4010 2a 20 51 75 6f 74 65 20 63 68 61 72 61 63 74 65  * Quote characte
a4020 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 68 61  r */.        cha
a4030 72 20 2a 65 73 63 61 72 67 20 3d 20 76 61 5f 61  r *escarg = va_a
a4040 72 67 28 61 70 2c 63 68 61 72 2a 29 3b 0a 20 20  rg(ap,char*);.  
a4050 20 20 20 20 20 20 69 73 6e 75 6c 6c 20 3d 20 65        isnull = e
a4060 73 63 61 72 67 3d 3d 30 3b 0a 20 20 20 20 20 20  scarg==0;.      
a4070 20 20 69 66 28 20 69 73 6e 75 6c 6c 20 29 20 65    if( isnull ) e
a4080 73 63 61 72 67 20 3d 20 28 78 74 79 70 65 3d 3d  scarg = (xtype==
a4090 65 74 53 51 4c 45 53 43 41 50 45 32 20 3f 20 22  etSQLESCAPE2 ? "
a40a0 4e 55 4c 4c 22 20 3a 20 22 28 4e 55 4c 4c 29 22  NULL" : "(NULL)"
a40b0 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  );.        for(i
a40c0 3d 6e 3d 30 3b 20 28 63 68 3d 65 73 63 61 72 67  =n=0; (ch=escarg
a40d0 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20  [i])!=0; i++){. 
a40e0 20 20 20 20 20 20 20 20 20 69 66 28 20 63 68 3d           if( ch=
a40f0 3d 71 20 29 20 20 6e 2b 2b 3b 0a 20 20 20 20 20  =q )  n++;.     
a4100 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 65 65     }.        nee
a4110 64 51 75 6f 74 65 20 3d 20 21 69 73 6e 75 6c 6c  dQuote = !isnull
a4120 20 26 26 20 78 74 79 70 65 3d 3d 65 74 53 51 4c   && xtype==etSQL
a4130 45 53 43 41 50 45 32 3b 0a 20 20 20 20 20 20 20  ESCAPE2;.       
a4140 20 6e 20 2b 3d 20 69 20 2b 20 31 20 2b 20 6e 65   n += i + 1 + ne
a4150 65 64 51 75 6f 74 65 2a 32 3b 0a 20 20 20 20 20  edQuote*2;.     
a4160 20 20 20 69 66 28 20 6e 3e 65 74 42 55 46 53 49     if( n>etBUFSI
a4170 5a 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ZE ){.          
a4180 62 75 66 70 74 20 3d 20 7a 45 78 74 72 61 20 3d  bufpt = zExtra =
a4190 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20   sqlite3Malloc( 
a41a0 6e 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  n );.          i
a41b0 66 28 20 62 75 66 70 74 3d 3d 30 20 29 7b 0a 20  f( bufpt==0 ){. 
a41c0 20 20 20 20 20 20 20 20 20 20 20 70 41 63 63 75             pAccu
a41d0 6d 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  m->mallocFailed 
a41e0 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20  = 1;.           
a41f0 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20   return;.       
a4200 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c     }.        }el
a4210 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 62 75  se{.          bu
a4220 66 70 74 20 3d 20 62 75 66 3b 0a 20 20 20 20 20  fpt = buf;.     
a4230 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6a 20 3d     }.        j =
a4240 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   0;.        if( 
a4250 6e 65 65 64 51 75 6f 74 65 20 29 20 62 75 66 70  needQuote ) bufp
a4260 74 5b 6a 2b 2b 5d 20 3d 20 71 3b 0a 20 20 20 20  t[j++] = q;.    
a4270 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 28 63 68      for(i=0; (ch
a4280 3d 65 73 63 61 72 67 5b 69 5d 29 21 3d 30 3b 20  =escarg[i])!=0; 
a4290 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  i++){.          
a42a0 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 63 68 3b  bufpt[j++] = ch;
a42b0 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 63  .          if( c
a42c0 68 3d 3d 71 20 29 20 62 75 66 70 74 5b 6a 2b 2b  h==q ) bufpt[j++
a42d0 5d 20 3d 20 63 68 3b 0a 20 20 20 20 20 20 20 20  ] = ch;.        
a42e0 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 65  }.        if( ne
a42f0 65 64 51 75 6f 74 65 20 29 20 62 75 66 70 74 5b  edQuote ) bufpt[
a4300 6a 2b 2b 5d 20 3d 20 71 3b 0a 20 20 20 20 20 20  j++] = q;.      
a4310 20 20 62 75 66 70 74 5b 6a 5d 20 3d 20 30 3b 0a    bufpt[j] = 0;.
a4320 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d          length =
a4330 20 6a 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54   j;.        /* T
a4340 68 65 20 70 72 65 63 69 73 69 6f 6e 20 69 73 20  he precision is 
a4350 69 67 6e 6f 72 65 64 20 6f 6e 20 25 71 20 61 6e  ignored on %q an
a4360 64 20 25 51 20 2a 2f 0a 20 20 20 20 20 20 20 20  d %Q */.        
a4370 2f 2a 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e  /* if( precision
a4380 3e 3d 30 20 26 26 20 70 72 65 63 69 73 69 6f 6e  >=0 && precision
a4390 3c 6c 65 6e 67 74 68 20 29 20 6c 65 6e 67 74 68  <length ) length
a43a0 20 3d 20 70 72 65 63 69 73 69 6f 6e 3b 20 2a 2f   = precision; */
a43b0 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
a43c0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61        }.      ca
a43d0 73 65 20 65 74 54 4f 4b 45 4e 3a 20 7b 0a 20 20  se etTOKEN: {.  
a43e0 20 20 20 20 20 20 54 6f 6b 65 6e 20 2a 70 54 6f        Token *pTo
a43f0 6b 65 6e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  ken = va_arg(ap,
a4400 20 54 6f 6b 65 6e 2a 29 3b 0a 20 20 20 20 20 20   Token*);.      
a4410 20 20 69 66 28 20 70 54 6f 6b 65 6e 20 29 7b 0a    if( pToken ){.
a4420 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
a4430 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28  3StrAccumAppend(
a4440 70 41 63 63 75 6d 2c 20 28 63 6f 6e 73 74 20 63  pAccum, (const c
a4450 68 61 72 2a 29 70 54 6f 6b 65 6e 2d 3e 7a 2c 20  har*)pToken->z, 
a4460 70 54 6f 6b 65 6e 2d 3e 6e 29 3b 0a 20 20 20 20  pToken->n);.    
a4470 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6c 65      }.        le
a4480 6e 67 74 68 20 3d 20 77 69 64 74 68 20 3d 20 30  ngth = width = 0
a4490 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
a44a0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63  .      }.      c
a44b0 61 73 65 20 65 74 53 52 43 4c 49 53 54 3a 20 7b  ase etSRCLIST: {
a44c0 0a 20 20 20 20 20 20 20 20 53 72 63 4c 69 73 74  .        SrcList
a44d0 20 2a 70 53 72 63 20 3d 20 76 61 5f 61 72 67 28   *pSrc = va_arg(
a44e0 61 70 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 20  ap, SrcList*);. 
a44f0 20 20 20 20 20 20 20 69 6e 74 20 6b 20 3d 20 76         int k = v
a4500 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a  a_arg(ap, int);.
a4510 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20 53          struct S
a4520 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  rcList_item *pIt
a4530 65 6d 20 3d 20 26 70 53 72 63 2d 3e 61 5b 6b 5d  em = &pSrc->a[k]
a4540 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
a4550 28 20 6b 3e 3d 30 20 26 26 20 6b 3c 70 53 72 63  ( k>=0 && k<pSrc
a4560 2d 3e 6e 53 72 63 20 29 3b 0a 20 20 20 20 20 20  ->nSrc );.      
a4570 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 44 61    if( pItem->zDa
a4580 74 61 62 61 73 65 20 29 7b 0a 20 20 20 20 20 20  tabase ){.      
a4590 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63      sqlite3StrAc
a45a0 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d  cumAppend(pAccum
a45b0 2c 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61  , pItem->zDataba
a45c0 73 65 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 20  se, -1);.       
a45d0 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63     sqlite3StrAcc
a45e0 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c  umAppend(pAccum,
a45f0 20 22 2e 22 2c 20 31 29 3b 0a 20 20 20 20 20 20   ".", 1);.      
a4600 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69    }.        sqli
a4610 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e  te3StrAccumAppen
a4620 64 28 70 41 63 63 75 6d 2c 20 70 49 74 65 6d 2d  d(pAccum, pItem-
a4630 3e 7a 4e 61 6d 65 2c 20 2d 31 29 3b 0a 20 20 20  >zName, -1);.   
a4640 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 77 69       length = wi
a4650 64 74 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  dth = 0;.       
a4660 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
a4670 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b        default: {
a4680 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
a4690 20 78 74 79 70 65 3d 3d 65 74 49 4e 56 41 4c 49   xtype==etINVALI
a46a0 44 20 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74  D );.        ret
a46b0 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
a46c0 20 7d 2f 2a 20 45 6e 64 20 73 77 69 74 63 68 20   }/* End switch 
a46d0 6f 76 65 72 20 74 68 65 20 66 6f 72 6d 61 74 20  over the format 
a46e0 74 79 70 65 20 2a 2f 0a 20 20 20 20 2f 2a 0a 20  type */.    /*. 
a46f0 20 20 20 2a 2a 20 54 68 65 20 74 65 78 74 20 6f     ** The text o
a4700 66 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e  f the conversion
a4710 20 69 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62   is pointed to b
a4720 79 20 22 62 75 66 70 74 22 20 61 6e 64 20 69 73  y "bufpt" and is
a4730 0a 20 20 20 20 2a 2a 20 22 6c 65 6e 67 74 68 22  .    ** "length"
a4740 20 63 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 67   characters long
a4750 2e 20 20 54 68 65 20 66 69 65 6c 64 20 77 69 64  .  The field wid
a4760 74 68 20 69 73 20 22 77 69 64 74 68 22 2e 20 20  th is "width".  
a4770 44 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 6f 75  Do.    ** the ou
a4780 74 70 75 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  tput..    */.   
a4790 20 69 66 28 20 21 66 6c 61 67 5f 6c 65 66 74 6a   if( !flag_leftj
a47a0 75 73 74 69 66 79 20 29 7b 0a 20 20 20 20 20 20  ustify ){.      
a47b0 72 65 67 69 73 74 65 72 20 69 6e 74 20 6e 73 70  register int nsp
a47c0 61 63 65 3b 0a 20 20 20 20 20 20 6e 73 70 61 63  ace;.      nspac
a47d0 65 20 3d 20 77 69 64 74 68 2d 6c 65 6e 67 74 68  e = width-length
a47e0 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 73 70 61  ;.      if( nspa
a47f0 63 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20  ce>0 ){.        
a4800 61 70 70 65 6e 64 53 70 61 63 65 28 70 41 63 63  appendSpace(pAcc
a4810 75 6d 2c 20 6e 73 70 61 63 65 29 3b 0a 20 20 20  um, nspace);.   
a4820 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
a4830 66 28 20 6c 65 6e 67 74 68 3e 30 20 29 7b 0a 20  f( length>0 ){. 
a4840 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41       sqlite3StrA
a4850 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75  ccumAppend(pAccu
a4860 6d 2c 20 62 75 66 70 74 2c 20 6c 65 6e 67 74 68  m, bufpt, length
a4870 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
a4880 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66   flag_leftjustif
a4890 79 20 29 7b 0a 20 20 20 20 20 20 72 65 67 69 73  y ){.      regis
a48a0 74 65 72 20 69 6e 74 20 6e 73 70 61 63 65 3b 0a  ter int nspace;.
a48b0 20 20 20 20 20 20 6e 73 70 61 63 65 20 3d 20 77        nspace = w
a48c0 69 64 74 68 2d 6c 65 6e 67 74 68 3b 0a 20 20 20  idth-length;.   
a48d0 20 20 20 69 66 28 20 6e 73 70 61 63 65 3e 30 20     if( nspace>0 
a48e0 29 7b 0a 20 20 20 20 20 20 20 20 61 70 70 65 6e  ){.        appen
a48f0 64 53 70 61 63 65 28 70 41 63 63 75 6d 2c 20 6e  dSpace(pAccum, n
a4900 73 70 61 63 65 29 3b 0a 20 20 20 20 20 20 7d 0a  space);.      }.
a4910 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 7a 45      }.    if( zE
a4920 78 74 72 61 20 29 7b 0a 20 20 20 20 20 20 73 71  xtra ){.      sq
a4930 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 78 74 72  lite3_free(zExtr
a4940 61 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 2f 2a 20  a);.    }.  }/* 
a4950 45 6e 64 20 66 6f 72 20 6c 6f 6f 70 20 6f 76 65  End for loop ove
a4960 72 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74 72  r the format str
a4970 69 6e 67 20 2a 2f 0a 7d 20 2f 2a 20 45 6e 64 20  ing */.} /* End 
a4980 6f 66 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a  of function */..
a4990 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 4e 20 62  /*.** Append N b
a49a0 79 74 65 73 20 6f 66 20 74 65 78 74 20 66 72 6f  ytes of text fro
a49b0 6d 20 7a 20 74 6f 20 74 68 65 20 53 74 72 41 63  m z to the StrAc
a49c0 63 75 6d 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53  cum object..*/.S
a49d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
a49e0 69 64 20 73 71 6c 69 74 65 33 53 74 72 41 63 63  id sqlite3StrAcc
a49f0 75 6d 41 70 70 65 6e 64 28 53 74 72 41 63 63 75  umAppend(StrAccu
a4a00 6d 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72  m *p, const char
a4a10 20 2a 7a 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 61   *z, int N){.  a
a4a20 73 73 65 72 74 28 20 7a 21 3d 30 20 7c 7c 20 4e  ssert( z!=0 || N
a4a30 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e  ==0 );.  if( p->
a4a40 74 6f 6f 42 69 67 20 7c 20 70 2d 3e 6d 61 6c 6c  tooBig | p->mall
a4a50 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
a4a60 74 65 73 74 63 61 73 65 28 70 2d 3e 74 6f 6f 42  testcase(p->tooB
a4a70 69 67 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73  ig);.    testcas
a4a80 65 28 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  e(p->mallocFaile
a4a90 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  d);.    return;.
a4aa0 20 20 7d 0a 20 20 69 66 28 20 4e 3c 30 20 29 7b    }.  if( N<0 ){
a4ab0 0a 20 20 20 20 4e 20 3d 20 73 71 6c 69 74 65 33  .    N = sqlite3
a4ac0 53 74 72 6c 65 6e 33 30 28 7a 29 3b 0a 20 20 7d  Strlen30(z);.  }
a4ad0 0a 20 20 69 66 28 20 4e 3d 3d 30 20 7c 7c 20 4e  .  if( N==0 || N
a4ae0 45 56 45 52 28 7a 3d 3d 30 29 20 29 7b 0a 20 20  EVER(z==0) ){.  
a4af0 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
a4b00 69 66 28 20 70 2d 3e 6e 43 68 61 72 2b 4e 20 3e  if( p->nChar+N >
a4b10 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20  = p->nAlloc ){. 
a4b20 20 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20     char *zNew;. 
a4b30 20 20 20 69 66 28 20 21 70 2d 3e 75 73 65 4d 61     if( !p->useMa
a4b40 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 70 2d  lloc ){.      p-
a4b50 3e 74 6f 6f 42 69 67 20 3d 20 31 3b 0a 20 20 20  >tooBig = 1;.   
a4b60 20 20 20 4e 20 3d 20 70 2d 3e 6e 41 6c 6c 6f 63     N = p->nAlloc
a4b70 20 2d 20 70 2d 3e 6e 43 68 61 72 20 2d 20 31 3b   - p->nChar - 1;
a4b80 0a 20 20 20 20 20 20 69 66 28 20 4e 3c 3d 30 20  .      if( N<=0 
a4b90 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
a4ba0 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  n;.      }.    }
a4bb0 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 36 34 20  else{.      i64 
a4bc0 73 7a 4e 65 77 20 3d 20 70 2d 3e 6e 43 68 61 72  szNew = p->nChar
a4bd0 3b 0a 20 20 20 20 20 20 73 7a 4e 65 77 20 2b 3d  ;.      szNew +=
a4be0 20 4e 20 2b 20 31 3b 0a 20 20 20 20 20 20 69 66   N + 1;.      if
a4bf0 28 20 73 7a 4e 65 77 20 3e 20 70 2d 3e 6d 78 41  ( szNew > p->mxA
a4c00 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 20 20  lloc ){.        
a4c10 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52  sqlite3StrAccumR
a4c20 65 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 20  eset(p);.       
a4c30 20 70 2d 3e 74 6f 6f 42 69 67 20 3d 20 31 3b 0a   p->tooBig = 1;.
a4c40 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a          return;.
a4c50 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
a4c60 20 20 20 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d       p->nAlloc =
a4c70 20 28 69 6e 74 29 73 7a 4e 65 77 3b 0a 20 20 20   (int)szNew;.   
a4c80 20 20 20 7d 0a 20 20 20 20 20 20 7a 4e 65 77 20     }.      zNew 
a4c90 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
a4ca0 63 52 61 77 28 70 2d 3e 64 62 2c 20 70 2d 3e 6e  cRaw(p->db, p->n
a4cb0 41 6c 6c 6f 63 20 29 3b 0a 20 20 20 20 20 20 69  Alloc );.      i
a4cc0 66 28 20 7a 4e 65 77 20 29 7b 0a 20 20 20 20 20  f( zNew ){.     
a4cd0 20 20 20 6d 65 6d 63 70 79 28 7a 4e 65 77 2c 20     memcpy(zNew, 
a4ce0 70 2d 3e 7a 54 65 78 74 2c 20 70 2d 3e 6e 43 68  p->zText, p->nCh
a4cf0 61 72 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ar);.        sql
a4d00 69 74 65 33 53 74 72 41 63 63 75 6d 52 65 73 65  ite3StrAccumRese
a4d10 74 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d  t(p);.        p-
a4d20 3e 7a 54 65 78 74 20 3d 20 7a 4e 65 77 3b 0a 20  >zText = zNew;. 
a4d30 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
a4d40 20 20 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69      p->mallocFai
a4d50 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  led = 1;.       
a4d60 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
a4d70 52 65 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20  Reset(p);.      
a4d80 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
a4d90 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 65  }.    }.  }.  me
a4da0 6d 63 70 79 28 26 70 2d 3e 7a 54 65 78 74 5b 70  mcpy(&p->zText[p
a4db0 2d 3e 6e 43 68 61 72 5d 2c 20 7a 2c 20 4e 29 3b  ->nChar], z, N);
a4dc0 0a 20 20 70 2d 3e 6e 43 68 61 72 20 2b 3d 20 4e  .  p->nChar += N
a4dd0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 69 73  ;.}../*.** Finis
a4de0 68 20 6f 66 66 20 61 20 73 74 72 69 6e 67 20 62  h off a string b
a4df0 79 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 69 74  y making sure it
a4e00 20 69 73 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61   is zero-termina
a4e10 74 65 64 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ted..** Return a
a4e20 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
a4e30 72 65 73 75 6c 74 69 6e 67 20 73 74 72 69 6e 67  resulting string
a4e40 2e 20 20 52 65 74 75 72 6e 20 61 20 4e 55 4c 4c  .  Return a NULL
a4e50 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 69 66 20 61  .** pointer if a
a4e60 6e 79 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72  ny kind of error
a4e70 20 77 61 73 20 65 6e 63 6f 75 6e 74 65 72 65 64   was encountered
a4e80 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
a4e90 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65  ATE char *sqlite
a4ea0 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28  3StrAccumFinish(
a4eb0 53 74 72 41 63 63 75 6d 20 2a 70 29 7b 0a 20 20  StrAccum *p){.  
a4ec0 69 66 28 20 70 2d 3e 7a 54 65 78 74 20 29 7b 0a  if( p->zText ){.
a4ed0 20 20 20 20 70 2d 3e 7a 54 65 78 74 5b 70 2d 3e      p->zText[p->
a4ee0 6e 43 68 61 72 5d 20 3d 20 30 3b 0a 20 20 20 20  nChar] = 0;.    
a4ef0 69 66 28 20 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63  if( p->useMalloc
a4f00 20 26 26 20 70 2d 3e 7a 54 65 78 74 3d 3d 70 2d   && p->zText==p-
a4f10 3e 7a 42 61 73 65 20 29 7b 0a 20 20 20 20 20 20  >zBase ){.      
a4f20 70 2d 3e 7a 54 65 78 74 20 3d 20 73 71 6c 69 74  p->zText = sqlit
a4f30 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 2d  e3DbMallocRaw(p-
a4f40 3e 64 62 2c 20 70 2d 3e 6e 43 68 61 72 2b 31 20  >db, p->nChar+1 
a4f50 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e  );.      if( p->
a4f60 7a 54 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20  zText ){.       
a4f70 20 6d 65 6d 63 70 79 28 70 2d 3e 7a 54 65 78 74   memcpy(p->zText
a4f80 2c 20 70 2d 3e 7a 42 61 73 65 2c 20 70 2d 3e 6e  , p->zBase, p->n
a4f90 43 68 61 72 2b 31 29 3b 0a 20 20 20 20 20 20 7d  Char+1);.      }
a4fa0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 2d  else{.        p-
a4fb0 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
a4fc0 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
a4fd0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 2d  .  }.  return p-
a4fe0 3e 7a 54 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  >zText;.}../*.**
a4ff0 20 52 65 73 65 74 20 61 6e 20 53 74 72 41 63 63   Reset an StrAcc
a5000 75 6d 20 73 74 72 69 6e 67 2e 20 20 52 65 63 6c  um string.  Recl
a5010 61 69 6d 20 61 6c 6c 20 6d 61 6c 6c 6f 63 65 64  aim all malloced
a5020 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 53 51 4c 49   memory..*/.SQLI
a5030 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
a5040 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52  sqlite3StrAccumR
a5050 65 73 65 74 28 53 74 72 41 63 63 75 6d 20 2a 70  eset(StrAccum *p
a5060 29 7b 0a 20 20 69 66 28 20 70 2d 3e 7a 54 65 78  ){.  if( p->zTex
a5070 74 21 3d 70 2d 3e 7a 42 61 73 65 20 29 7b 0a 20  t!=p->zBase ){. 
a5080 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
a5090 28 70 2d 3e 64 62 2c 20 70 2d 3e 7a 54 65 78 74  (p->db, p->zText
a50a0 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 7a 54 65 78  );.  }.  p->zTex
a50b0 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  t = 0;.}../*.** 
a50c0 49 6e 69 74 69 61 6c 69 7a 65 20 61 20 73 74 72  Initialize a str
a50d0 69 6e 67 20 61 63 63 75 6d 75 6c 61 74 6f 72 0a  ing accumulator.
a50e0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
a50f0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74  E void sqlite3St
a5100 72 41 63 63 75 6d 49 6e 69 74 28 53 74 72 41 63  rAccumInit(StrAc
a5110 63 75 6d 20 2a 70 2c 20 63 68 61 72 20 2a 7a 42  cum *p, char *zB
a5120 61 73 65 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20  ase, int n, int 
a5130 6d 78 29 7b 0a 20 20 70 2d 3e 7a 54 65 78 74 20  mx){.  p->zText 
a5140 3d 20 70 2d 3e 7a 42 61 73 65 20 3d 20 7a 42 61  = p->zBase = zBa
a5150 73 65 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 30 3b  se;.  p->db = 0;
a5160 0a 20 20 70 2d 3e 6e 43 68 61 72 20 3d 20 30 3b  .  p->nChar = 0;
a5170 0a 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 6e  .  p->nAlloc = n
a5180 3b 0a 20 20 70 2d 3e 6d 78 41 6c 6c 6f 63 20 3d  ;.  p->mxAlloc =
a5190 20 6d 78 3b 0a 20 20 70 2d 3e 75 73 65 4d 61 6c   mx;.  p->useMal
a51a0 6c 6f 63 20 3d 20 31 3b 0a 20 20 70 2d 3e 74 6f  loc = 1;.  p->to
a51b0 6f 42 69 67 20 3d 20 30 3b 0a 20 20 70 2d 3e 6d  oBig = 0;.  p->m
a51c0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b  allocFailed = 0;
a51d0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20  .}../*.** Print 
a51e0 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61  into memory obta
a51f0 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
a5200 4d 61 6c 6c 6f 63 28 29 2e 20 20 55 73 65 20 74  Malloc().  Use t
a5210 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25  he internal.** %
a5220 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 65 78 74 65  -conversion exte
a5230 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54  nsions..*/.SQLIT
a5240 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a  E_PRIVATE char *
a5250 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28  sqlite3VMPrintf(
a5260 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e  sqlite3 *db, con
a5270 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74  st char *zFormat
a5280 2c 20 76 61 5f 6c 69 73 74 20 61 70 29 7b 0a 20  , va_list ap){. 
a5290 20 63 68 61 72 20 2a 7a 3b 0a 20 20 63 68 61 72   char *z;.  char
a52a0 20 7a 42 61 73 65 5b 53 51 4c 49 54 45 5f 50 52   zBase[SQLITE_PR
a52b0 49 4e 54 5f 42 55 46 5f 53 49 5a 45 5d 3b 0a 20  INT_BUF_SIZE];. 
a52c0 20 53 74 72 41 63 63 75 6d 20 61 63 63 3b 0a 20   StrAccum acc;. 
a52d0 20 61 73 73 65 72 74 28 20 64 62 21 3d 30 20 29   assert( db!=0 )
a52e0 3b 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63  ;.  sqlite3StrAc
a52f0 63 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42  cumInit(&acc, zB
a5300 61 73 65 2c 20 73 69 7a 65 6f 66 28 7a 42 61 73  ase, sizeof(zBas
a5310 65 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  e),.            
a5320 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 61 4c            db->aL
a5330 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
a5340 54 5f 4c 45 4e 47 54 48 5d 29 3b 0a 20 20 61 63  T_LENGTH]);.  ac
a5350 63 2e 64 62 20 3d 20 64 62 3b 0a 20 20 73 71 6c  c.db = db;.  sql
a5360 69 74 65 33 56 58 50 72 69 6e 74 66 28 26 61 63  ite3VXPrintf(&ac
a5370 63 2c 20 31 2c 20 7a 46 6f 72 6d 61 74 2c 20 61  c, 1, zFormat, a
a5380 70 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65  p);.  z = sqlite
a5390 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28  3StrAccumFinish(
a53a0 26 61 63 63 29 3b 0a 20 20 69 66 28 20 61 63 63  &acc);.  if( acc
a53b0 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  .mallocFailed ){
a53c0 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  .    db->mallocF
a53d0 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20  ailed = 1;.  }. 
a53e0 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a   return z;.}../*
a53f0 0a 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20 6d  .** Print into m
a5400 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66  emory obtained f
a5410 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63  rom sqliteMalloc
a5420 28 29 2e 20 20 55 73 65 20 74 68 65 20 69 6e 74  ().  Use the int
a5430 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65  ernal.** %-conve
a5440 72 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73  rsion extensions
a5450 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
a5460 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65  ATE char *sqlite
a5470 33 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 65 33  3MPrintf(sqlite3
a5480 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72   *db, const char
a5490 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b   *zFormat, ...){
a54a0 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20  .  va_list ap;. 
a54b0 20 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61 5f 73   char *z;.  va_s
a54c0 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74  tart(ap, zFormat
a54d0 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33  );.  z = sqlite3
a54e0 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 6f  VMPrintf(db, zFo
a54f0 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f  rmat, ap);.  va_
a5500 65 6e 64 28 61 70 29 3b 0a 20 20 72 65 74 75 72  end(ap);.  retur
a5510 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69  n z;.}../*.** Li
a5520 6b 65 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74  ke sqlite3MPrint
a5530 66 28 29 2c 20 62 75 74 20 63 61 6c 6c 20 73 71  f(), but call sq
a5540 6c 69 74 65 33 44 62 46 72 65 65 28 29 20 6f 6e  lite3DbFree() on
a5550 20 7a 53 74 72 20 61 66 74 65 72 20 66 6f 72 6d   zStr after form
a5560 61 74 74 69 6e 67 0a 2a 2a 20 74 68 65 20 73 74  atting.** the st
a5570 72 69 6e 67 20 61 6e 64 20 62 65 66 6f 72 65 20  ring and before 
a5580 72 65 74 75 72 6e 6e 69 6e 67 2e 20 20 54 68 69  returnning.  Thi
a5590 73 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6e 74  s routine is int
a55a0 65 6e 64 65 64 20 74 6f 20 62 65 20 75 73 65 64  ended to be used
a55b0 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66 79 20 61 6e  .** to modify an
a55c0 20 65 78 69 73 74 69 6e 67 20 73 74 72 69 6e 67   existing string
a55d0 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a  .  For example:.
a55e0 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 78 20 3d 20  **.**       x = 
a55f0 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
a5600 62 2c 20 78 2c 20 22 70 72 65 66 69 78 20 25 73  b, x, "prefix %s
a5610 20 73 75 66 66 69 78 22 2c 20 78 29 3b 0a 2a 2a   suffix", x);.**
a5620 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
a5630 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  TE char *sqlite3
a5640 4d 41 70 70 65 6e 64 66 28 73 71 6c 69 74 65 33  MAppendf(sqlite3
a5650 20 2a 64 62 2c 20 63 68 61 72 20 2a 7a 53 74 72   *db, char *zStr
a5660 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  , const char *zF
a5670 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76  ormat, ...){.  v
a5680 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61  a_list ap;.  cha
a5690 72 20 2a 7a 3b 0a 20 20 76 61 5f 73 74 61 72 74  r *z;.  va_start
a56a0 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20  (ap, zFormat);. 
a56b0 20 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72   z = sqlite3VMPr
a56c0 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74  intf(db, zFormat
a56d0 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28  , ap);.  va_end(
a56e0 61 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  ap);.  sqlite3Db
a56f0 46 72 65 65 28 64 62 2c 20 7a 53 74 72 29 3b 0a  Free(db, zStr);.
a5700 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f    return z;.}../
a5710 2a 0a 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20  *.** Print into 
a5720 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20  memory obtained 
a5730 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  from sqlite3_mal
a5740 6c 6f 63 28 29 2e 20 20 4f 6d 69 74 20 74 68 65  loc().  Omit the
a5750 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63   internal.** %-c
a5760 6f 6e 76 65 72 73 69 6f 6e 20 65 78 74 65 6e 73  onversion extens
a5770 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ions..*/.SQLITE_
a5780 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65  API char *sqlite
a5790 33 5f 76 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74  3_vmprintf(const
a57a0 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20   char *zFormat, 
a57b0 76 61 5f 6c 69 73 74 20 61 70 29 7b 0a 20 20 63  va_list ap){.  c
a57c0 68 61 72 20 2a 7a 3b 0a 20 20 63 68 61 72 20 7a  har *z;.  char z
a57d0 42 61 73 65 5b 53 51 4c 49 54 45 5f 50 52 49 4e  Base[SQLITE_PRIN
a57e0 54 5f 42 55 46 5f 53 49 5a 45 5d 3b 0a 20 20 53  T_BUF_SIZE];.  S
a57f0 74 72 41 63 63 75 6d 20 61 63 63 3b 0a 23 69 66  trAccum acc;.#if
a5800 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
a5810 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 28 20  _AUTOINIT.  if( 
a5820 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
a5830 7a 65 28 29 20 29 20 72 65 74 75 72 6e 20 30 3b  ze() ) return 0;
a5840 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65  .#endif.  sqlite
a5850 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26 61  3StrAccumInit(&a
a5860 63 63 2c 20 7a 42 61 73 65 2c 20 73 69 7a 65 6f  cc, zBase, sizeo
a5870 66 28 7a 42 61 73 65 29 2c 20 53 51 4c 49 54 45  f(zBase), SQLITE
a5880 5f 4d 41 58 5f 4c 45 4e 47 54 48 29 3b 0a 20 20  _MAX_LENGTH);.  
a5890 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28  sqlite3VXPrintf(
a58a0 26 61 63 63 2c 20 30 2c 20 7a 46 6f 72 6d 61 74  &acc, 0, zFormat
a58b0 2c 20 61 70 29 3b 0a 20 20 7a 20 3d 20 73 71 6c  , ap);.  z = sql
a58c0 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69  ite3StrAccumFini
a58d0 73 68 28 26 61 63 63 29 3b 0a 20 20 72 65 74 75  sh(&acc);.  retu
a58e0 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  rn z;.}../*.** P
a58f0 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79  rint into memory
a5900 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
a5910 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 28  qlite3_malloc()(
a5920 29 2e 20 20 4f 6d 69 74 20 74 68 65 20 69 6e 74  ).  Omit the int
a5930 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65  ernal.** %-conve
a5940 72 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73  rsion extensions
a5950 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
a5960 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6d 70  char *sqlite3_mp
a5970 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72  rintf(const char
a5980 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b   *zFormat, ...){
a5990 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20  .  va_list ap;. 
a59a0 20 63 68 61 72 20 2a 7a 3b 0a 23 69 66 6e 64 65   char *z;.#ifnde
a59b0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
a59c0 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c  TOINIT.  if( sql
a59d0 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
a59e0 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65  ) ) return 0;.#e
a59f0 6e 64 69 66 0a 20 20 76 61 5f 73 74 61 72 74 28  ndif.  va_start(
a5a00 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20  ap, zFormat);.  
a5a10 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 6d 70 72  z = sqlite3_vmpr
a5a20 69 6e 74 66 28 7a 46 6f 72 6d 61 74 2c 20 61 70  intf(zFormat, ap
a5a30 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b  );.  va_end(ap);
a5a40 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a  .  return z;.}..
a5a50 2f 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 6e  /*.** sqlite3_sn
a5a60 70 72 69 6e 74 66 28 29 20 77 6f 72 6b 73 20 6c  printf() works l
a5a70 69 6b 65 20 73 6e 70 72 69 6e 74 66 28 29 20 65  ike snprintf() e
a5a80 78 63 65 70 74 20 74 68 61 74 20 69 74 20 69 67  xcept that it ig
a5a90 6e 6f 72 65 73 20 74 68 65 0a 2a 2a 20 63 75 72  nores the.** cur
a5aa0 72 65 6e 74 20 6c 6f 63 61 6c 65 20 73 65 74 74  rent locale sett
a5ab0 69 6e 67 73 2e 20 20 54 68 69 73 20 69 73 20 69  ings.  This is i
a5ac0 6d 70 6f 72 74 61 6e 74 20 66 6f 72 20 53 51 4c  mportant for SQL
a5ad0 69 74 65 20 62 65 63 61 75 73 65 20 77 65 0a 2a  ite because we.*
a5ae0 2a 20 61 72 65 20 6e 6f 74 20 61 62 6c 65 20 74  * are not able t
a5af0 6f 20 75 73 65 20 61 20 22 2c 22 20 61 73 20 74  o use a "," as t
a5b00 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74  he decimal point
a5b10 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 22 2e 22   in place of "."
a5b20 20 61 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 64   as.** specified
a5b30 20 62 79 20 73 6f 6d 65 20 6c 6f 63 61 6c 65 73   by some locales
a5b40 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
a5b50 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 6e  char *sqlite3_sn
a5b60 70 72 69 6e 74 66 28 69 6e 74 20 6e 2c 20 63 68  printf(int n, ch
a5b70 61 72 20 2a 7a 42 75 66 2c 20 63 6f 6e 73 74 20  ar *zBuf, const 
a5b80 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e  char *zFormat, .
a5b90 2e 2e 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a  ..){.  char *z;.
a5ba0 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20    va_list ap;.  
a5bb0 53 74 72 41 63 63 75 6d 20 61 63 63 3b 0a 0a 20  StrAccum acc;.. 
a5bc0 20 69 66 28 20 6e 3c 3d 30 20 29 7b 0a 20 20 20   if( n<=0 ){.   
a5bd0 20 72 65 74 75 72 6e 20 7a 42 75 66 3b 0a 20 20   return zBuf;.  
a5be0 7d 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63  }.  sqlite3StrAc
a5bf0 63 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42  cumInit(&acc, zB
a5c00 75 66 2c 20 6e 2c 20 30 29 3b 0a 20 20 61 63 63  uf, n, 0);.  acc
a5c10 2e 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a  .useMalloc = 0;.
a5c20 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 7a 46    va_start(ap,zF
a5c30 6f 72 6d 61 74 29 3b 0a 20 20 73 71 6c 69 74 65  ormat);.  sqlite
a5c40 33 56 58 50 72 69 6e 74 66 28 26 61 63 63 2c 20  3VXPrintf(&acc, 
a5c50 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b  0, zFormat, ap);
a5c60 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20  .  va_end(ap);. 
a5c70 20 7a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 41   z = sqlite3StrA
a5c80 63 63 75 6d 46 69 6e 69 73 68 28 26 61 63 63 29  ccumFinish(&acc)
a5c90 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a  ;.  return z;.}.
a5ca0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
a5cb0 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a  ITE_DEBUG)./*.**
a5cc0 20 41 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 72   A version of pr
a5cd0 69 6e 74 66 28 29 20 74 68 61 74 20 75 6e 64 65  intf() that unde
a5ce0 72 73 74 61 6e 64 73 20 25 6c 6c 64 2e 20 20 55  rstands %lld.  U
a5cf0 73 65 64 20 66 6f 72 20 64 65 62 75 67 67 69 6e  sed for debuggin
a5d00 67 2e 0a 2a 2a 20 54 68 65 20 70 72 69 6e 74 66  g..** The printf
a5d10 28 29 20 62 75 69 6c 74 20 69 6e 74 6f 20 73 6f  () built into so
a5d20 6d 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 77  me versions of w
a5d30 69 6e 64 6f 77 73 20 64 6f 65 73 20 6e 6f 74 20  indows does not 
a5d40 75 6e 64 65 72 73 74 61 6e 64 20 25 6c 6c 64 0a  understand %lld.
a5d50 2a 2a 20 61 6e 64 20 73 65 67 66 61 75 6c 74 73  ** and segfaults
a5d60 20 69 66 20 79 6f 75 20 67 69 76 65 20 69 74 20   if you give it 
a5d70 61 20 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 2e  a long long int.
a5d80 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
a5d90 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
a5da0 65 62 75 67 50 72 69 6e 74 66 28 63 6f 6e 73 74  ebugPrintf(const
a5db0 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20   char *zFormat, 
a5dc0 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20  ...){.  va_list 
a5dd0 61 70 3b 0a 20 20 53 74 72 41 63 63 75 6d 20 61  ap;.  StrAccum a
a5de0 63 63 3b 0a 20 20 63 68 61 72 20 7a 42 75 66 5b  cc;.  char zBuf[
a5df0 35 30 30 5d 3b 0a 20 20 73 71 6c 69 74 65 33 53  500];.  sqlite3S
a5e00 74 72 41 63 63 75 6d 49 6e 69 74 28 26 61 63 63  trAccumInit(&acc
a5e10 2c 20 7a 42 75 66 2c 20 73 69 7a 65 6f 66 28 7a  , zBuf, sizeof(z
a5e20 42 75 66 29 2c 20 30 29 3b 0a 20 20 61 63 63 2e  Buf), 0);.  acc.
a5e30 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20  useMalloc = 0;. 
a5e40 20 76 61 5f 73 74 61 72 74 28 61 70 2c 7a 46 6f   va_start(ap,zFo
a5e50 72 6d 61 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  rmat);.  sqlite3
a5e60 56 58 50 72 69 6e 74 66 28 26 61 63 63 2c 20 30  VXPrintf(&acc, 0
a5e70 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a  , zFormat, ap);.
a5e80 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20    va_end(ap);.  
a5e90 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46  sqlite3StrAccumF
a5ea0 69 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 20 66  inish(&acc);.  f
a5eb0 70 72 69 6e 74 66 28 73 74 64 6f 75 74 2c 22 25  printf(stdout,"%
a5ec0 73 22 2c 20 7a 42 75 66 29 3b 0a 20 20 66 66 6c  s", zBuf);.  ffl
a5ed0 75 73 68 28 73 74 64 6f 75 74 29 3b 0a 7d 0a 23  ush(stdout);.}.#
a5ee0 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  endif../********
a5ef0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 72  ****** End of pr
a5f00 69 6e 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  intf.c *********
a5f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a5f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a5f30 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
a5f40 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
a5f50 65 20 72 61 6e 64 6f 6d 2e 63 20 2a 2a 2a 2a 2a  e random.c *****
a5f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a5f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a5f80 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
a5f90 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a  1 September 15.*
a5fa0 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
a5fb0 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
a5fc0 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
a5fd0 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
a5fe0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
a5ff0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
a6000 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
a6010 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
a6020 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
a6030 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
a6040 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
a6050 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
a6060 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
a6070 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
a6080 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
a6090 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
a60a0 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
a60b0 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
a60c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a60d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a60e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a60f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
a6100 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
a6110 69 6e 73 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c  ins code to impl
a6120 65 6d 65 6e 74 20 61 20 70 73 65 75 64 6f 2d 72  ement a pseudo-r
a6130 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 0a 2a 2a 20  andom number.** 
a6140 67 65 6e 65 72 61 74 6f 72 20 28 50 52 4e 47 29  generator (PRNG)
a6150 20 66 6f 72 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a   for SQLite..**.
a6160 2a 2a 20 52 61 6e 64 6f 6d 20 6e 75 6d 62 65 72  ** Random number
a6170 73 20 61 72 65 20 75 73 65 64 20 62 79 20 73 6f  s are used by so
a6180 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  me of the databa
a6190 73 65 20 62 61 63 6b 65 6e 64 73 20 69 6e 20 6f  se backends in o
a61a0 72 64 65 72 0a 2a 2a 20 74 6f 20 67 65 6e 65 72  rder.** to gener
a61b0 61 74 65 20 72 61 6e 64 6f 6d 20 69 6e 74 65 67  ate random integ
a61c0 65 72 20 6b 65 79 73 20 66 6f 72 20 74 61 62 6c  er keys for tabl
a61d0 65 73 20 6f 72 20 72 61 6e 64 6f 6d 20 66 69 6c  es or random fil
a61e0 65 6e 61 6d 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49  enames..**.** $I
a61f0 64 3a 20 72 61 6e 64 6f 6d 2e 63 2c 76 20 31 2e  d: random.c,v 1.
a6200 32 39 20 32 30 30 38 2f 31 32 2f 31 30 20 31 39  29 2008/12/10 19
a6210 3a 32 36 3a 32 34 20 64 72 68 20 45 78 70 20 24  :26:24 drh Exp $
a6220 0a 2a 2f 0a 0a 0a 2f 2a 20 41 6c 6c 20 74 68 72  .*/.../* All thr
a6230 65 61 64 73 20 73 68 61 72 65 20 61 20 73 69 6e  eads share a sin
a6240 67 6c 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65  gle random numbe
a6250 72 20 67 65 6e 65 72 61 74 6f 72 2e 0a 2a 2a 20  r generator..** 
a6260 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69  This structure i
a6270 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 74  s the current st
a6280 61 74 65 20 6f 66 20 74 68 65 20 67 65 6e 65 72  ate of the gener
a6290 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ator..*/.static 
a62a0 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63  SQLITE_WSD struc
a62b0 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70  t sqlite3PrngTyp
a62c0 65 20 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  e {.  unsigned c
a62d0 68 61 72 20 69 73 49 6e 69 74 3b 20 20 20 20 20  har isInit;     
a62e0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
a62f0 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20  initialized */. 
a6300 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69   unsigned char i
a6310 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20  , j;            
a6320 2f 2a 20 53 74 61 74 65 20 76 61 72 69 61 62 6c  /* State variabl
a6330 65 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  es */.  unsigned
a6340 20 63 68 61 72 20 73 5b 32 35 36 5d 3b 20 20 20   char s[256];   
a6350 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 65 20         /* State 
a6360 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a 7d 20 73  variables */.} s
a6370 71 6c 69 74 65 33 50 72 6e 67 3b 0a 0a 2f 2a 0a  qlite3Prng;../*.
a6380 2a 2a 20 47 65 74 20 61 20 73 69 6e 67 6c 65 20  ** Get a single 
a6390 38 2d 62 69 74 20 72 61 6e 64 6f 6d 20 76 61 6c  8-bit random val
a63a0 75 65 20 66 72 6f 6d 20 74 68 65 20 52 43 34 20  ue from the RC4 
a63b0 50 52 4e 47 2e 20 20 54 68 65 20 4d 75 74 65 78  PRNG.  The Mutex
a63c0 0a 2a 2a 20 6d 75 73 74 20 62 65 20 68 65 6c 64  .** must be held
a63d0 20 77 68 69 6c 65 20 65 78 65 63 75 74 69 6e 67   while executing
a63e0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a   this routine..*
a63f0 2a 0a 2a 2a 20 57 68 79 20 6e 6f 74 20 6a 75 73  *.** Why not jus
a6400 74 20 75 73 65 20 61 20 6c 69 62 72 61 72 79 20  t use a library 
a6410 72 61 6e 64 6f 6d 20 67 65 6e 65 72 61 74 6f 72  random generator
a6420 20 6c 69 6b 65 20 6c 72 61 6e 64 34 38 28 29 20   like lrand48() 
a6430 66 6f 72 20 74 68 69 73 3f 0a 2a 2a 20 42 65 63  for this?.** Bec
a6440 61 75 73 65 20 74 68 65 20 4f 50 5f 4e 65 77 52  ause the OP_NewR
a6450 6f 77 69 64 20 6f 70 63 6f 64 65 20 69 6e 20 74  owid opcode in t
a6460 68 65 20 56 44 42 45 20 64 65 70 65 6e 64 73 20  he VDBE depends 
a6470 6f 6e 20 68 61 76 69 6e 67 20 61 20 76 65 72 79  on having a very
a6480 0a 2a 2a 20 67 6f 6f 64 20 73 6f 75 72 63 65 20  .** good source 
a6490 6f 66 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72  of random number
a64a0 73 2e 20 20 54 68 65 20 6c 72 61 6e 64 34 38 28  s.  The lrand48(
a64b0 29 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69  ) library functi
a64c0 6f 6e 20 6d 61 79 0a 2a 2a 20 77 65 6c 6c 20 62  on may.** well b
a64d0 65 20 67 6f 6f 64 20 65 6e 6f 75 67 68 2e 20 20  e good enough.  
a64e0 42 75 74 20 6d 61 79 62 65 20 6e 6f 74 2e 20 20  But maybe not.  
a64f0 4f 72 20 6d 61 79 62 65 20 6c 72 61 6e 64 34 38  Or maybe lrand48
a6500 28 29 20 68 61 73 20 73 6f 6d 65 0a 2a 2a 20 73  () has some.** s
a6510 75 62 74 6c 65 20 70 72 6f 62 6c 65 6d 73 20 6f  ubtle problems o
a6520 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 20 74  n some systems t
a6530 68 61 74 20 63 6f 75 6c 64 20 63 61 75 73 65 20  hat could cause 
a6540 70 72 6f 62 6c 65 6d 73 2e 20 20 49 74 20 69 73  problems.  It is
a6550 20 68 61 72 64 0a 2a 2a 20 74 6f 20 6b 6e 6f 77   hard.** to know
a6560 2e 20 20 54 6f 20 6d 69 6e 69 6d 69 7a 65 20 74  .  To minimize t
a6570 68 65 20 72 69 73 6b 20 6f 66 20 70 72 6f 62 6c  he risk of probl
a6580 65 6d 73 20 64 75 65 20 74 6f 20 62 61 64 20 6c  ems due to bad l
a6590 72 61 6e 64 34 38 28 29 0a 2a 2a 20 69 6d 70 6c  rand48().** impl
a65a0 65 6d 65 6e 74 61 74 69 6f 6e 73 2c 20 53 51 4c  ementations, SQL
a65b0 69 74 65 20 75 73 65 73 20 74 68 69 73 20 72 61  ite uses this ra
a65c0 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65  ndom number gene
a65d0 72 61 74 6f 72 20 62 61 73 65 64 0a 2a 2a 20 6f  rator based.** o
a65e0 6e 20 52 43 34 2c 20 77 68 69 63 68 20 77 65 20  n RC4, which we 
a65f0 6b 6e 6f 77 20 77 6f 72 6b 73 20 76 65 72 79 20  know works very 
a6600 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 28 4c 61 74  well..**.** (Lat
a6610 65 72 29 3a 20 20 41 63 74 75 61 6c 6c 79 2c 20  er):  Actually, 
a6620 4f 50 5f 4e 65 77 52 6f 77 69 64 20 64 6f 65 73  OP_NewRowid does
a6630 20 6e 6f 74 20 64 65 70 65 6e 64 20 6f 6e 20 61   not depend on a
a6640 20 67 6f 6f 64 20 73 6f 75 72 63 65 20 6f 66 0a   good source of.
a6650 2a 2a 20 72 61 6e 64 6f 6d 6e 65 73 73 20 61 6e  ** randomness an
a6660 79 20 6d 6f 72 65 2e 20 20 42 75 74 20 77 65 20  y more.  But we 
a6670 77 69 6c 6c 20 6c 65 61 76 65 20 74 68 69 73 20  will leave this 
a6680 63 6f 64 65 20 69 6e 20 61 6c 6c 20 74 68 65 20  code in all the 
a6690 73 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  same..*/.static 
a66a0 75 38 20 72 61 6e 64 6f 6d 42 79 74 65 28 76 6f  u8 randomByte(vo
a66b0 69 64 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20  id){.  unsigned 
a66c0 63 68 61 72 20 74 3b 0a 0a 0a 20 20 2f 2a 20 54  char t;...  /* T
a66d0 68 65 20 22 77 73 64 50 72 6e 67 22 20 6d 61 63  he "wsdPrng" mac
a66e0 72 6f 20 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20  ro will resolve 
a66f0 74 6f 20 74 68 65 20 70 73 65 75 64 6f 2d 72 61  to the pseudo-ra
a6700 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65  ndom number gene
a6710 72 61 74 6f 72 0a 20 20 2a 2a 20 73 74 61 74 65  rator.  ** state
a6720 20 76 65 63 74 6f 72 2e 20 20 49 66 20 77 72 69   vector.  If wri
a6730 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 74  table static dat
a6740 61 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65 64  a is unsupported
a6750 20 6f 6e 20 74 68 65 20 74 61 72 67 65 74 2c 0a   on the target,.
a6760 20 20 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20    ** we have to 
a6770 6c 6f 63 61 74 65 20 74 68 65 20 73 74 61 74 65  locate the state
a6780 20 76 65 63 74 6f 72 20 61 74 20 72 75 6e 2d 74   vector at run-t
a6790 69 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72  ime.  In the mor
a67a0 65 20 63 6f 6d 6d 6f 6e 0a 20 20 2a 2a 20 63 61  e common.  ** ca
a67b0 73 65 20 77 68 65 72 65 20 77 72 69 74 61 62 6c  se where writabl
a67c0 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69 73  e static data is
a67d0 20 73 75 70 70 6f 72 74 65 64 2c 20 77 73 64 50   supported, wsdP
a67e0 72 6e 67 20 63 61 6e 20 72 65 66 65 72 20 64 69  rng can refer di
a67f0 72 65 63 74 6c 79 0a 20 20 2a 2a 20 74 6f 20 74  rectly.  ** to t
a6800 68 65 20 22 73 71 6c 69 74 65 33 50 72 6e 67 22  he "sqlite3Prng"
a6810 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 64 65   state vector de
a6820 63 6c 61 72 65 64 20 61 62 6f 76 65 2e 0a 20 20  clared above..  
a6830 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
a6840 5f 4f 4d 49 54 5f 57 53 44 0a 20 20 73 74 72 75  _OMIT_WSD.  stru
a6850 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79  ct sqlite3PrngTy
a6860 70 65 20 2a 70 20 3d 20 26 47 4c 4f 42 41 4c 28  pe *p = &GLOBAL(
a6870 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72  struct sqlite3Pr
a6880 6e 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 50  ngType, sqlite3P
a6890 72 6e 67 29 3b 0a 23 20 64 65 66 69 6e 65 20 77  rng);.# define w
a68a0 73 64 50 72 6e 67 20 70 5b 30 5d 0a 23 65 6c 73  sdPrng p[0].#els
a68b0 65 0a 23 20 64 65 66 69 6e 65 20 77 73 64 50 72  e.# define wsdPr
a68c0 6e 67 20 73 71 6c 69 74 65 33 50 72 6e 67 0a 23  ng sqlite3Prng.#
a68d0 65 6e 64 69 66 0a 0a 0a 20 20 2f 2a 20 49 6e 69  endif...  /* Ini
a68e0 74 69 61 6c 69 7a 65 20 74 68 65 20 73 74 61 74  tialize the stat
a68f0 65 20 6f 66 20 74 68 65 20 72 61 6e 64 6f 6d 20  e of the random 
a6900 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72  number generator
a6910 20 6f 6e 63 65 2c 0a 20 20 2a 2a 20 74 68 65 20   once,.  ** the 
a6920 66 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20  first time this 
a6930 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
a6940 64 2e 20 20 54 68 65 20 73 65 65 64 20 76 61 6c  d.  The seed val
a6950 75 65 20 64 6f 65 73 0a 20 20 2a 2a 20 6e 6f 74  ue does.  ** not
a6960 20 6e 65 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e   need to contain
a6970 20 61 20 6c 6f 74 20 6f 66 20 72 61 6e 64 6f 6d   a lot of random
a6980 6e 65 73 73 20 73 69 6e 63 65 20 77 65 20 61 72  ness since we ar
a6990 65 20 6e 6f 74 0a 20 20 2a 2a 20 74 72 79 69 6e  e not.  ** tryin
a69a0 67 20 74 6f 20 64 6f 20 73 65 63 75 72 65 20 65  g to do secure e
a69b0 6e 63 72 79 70 74 69 6f 6e 20 6f 72 20 61 6e 79  ncryption or any
a69c0 74 68 69 6e 67 20 6c 69 6b 65 20 74 68 61 74 2e  thing like that.
a69d0 2e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74  ...  **.  ** Not
a69e0 68 69 6e 67 20 69 6e 20 74 68 69 73 20 66 69 6c  hing in this fil
a69f0 65 20 6f 72 20 61 6e 79 77 68 65 72 65 20 65 6c  e or anywhere el
a6a00 73 65 20 69 6e 20 53 51 4c 69 74 65 20 64 6f 65  se in SQLite doe
a6a10 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 0a 20 20  s any kind of.  
a6a20 2a 2a 20 65 6e 63 72 79 70 74 69 6f 6e 2e 20 20  ** encryption.  
a6a30 54 68 65 20 52 43 34 20 61 6c 67 6f 72 69 74 68  The RC4 algorith
a6a40 6d 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 20  m is being used 
a6a50 61 73 20 61 20 50 52 4e 47 20 28 70 73 65 75 64  as a PRNG (pseud
a6a60 6f 2d 72 61 6e 64 6f 6d 0a 20 20 2a 2a 20 6e 75  o-random.  ** nu
a6a70 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 29 20  mber generator) 
a6a80 6e 6f 74 20 61 73 20 61 6e 20 65 6e 63 72 79 70  not as an encryp
a6a90 74 69 6f 6e 20 64 65 76 69 63 65 2e 0a 20 20 2a  tion device..  *
a6aa0 2f 0a 20 20 69 66 28 20 21 77 73 64 50 72 6e 67  /.  if( !wsdPrng
a6ab0 2e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 69  .isInit ){.    i
a6ac0 6e 74 20 69 3b 0a 20 20 20 20 63 68 61 72 20 6b  nt i;.    char k
a6ad0 5b 32 35 36 5d 3b 0a 20 20 20 20 77 73 64 50 72  [256];.    wsdPr
a6ae0 6e 67 2e 6a 20 3d 20 30 3b 0a 20 20 20 20 77 73  ng.j = 0;.    ws
a6af0 64 50 72 6e 67 2e 69 20 3d 20 30 3b 0a 20 20 20  dPrng.i = 0;.   
a6b00 20 73 71 6c 69 74 65 33 4f 73 52 61 6e 64 6f 6d   sqlite3OsRandom
a6b10 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73  ness(sqlite3_vfs
a6b20 5f 66 69 6e 64 28 30 29 2c 20 32 35 36 2c 20 6b  _find(0), 256, k
a6b30 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  );.    for(i=0; 
a6b40 69 3c 32 35 36 3b 20 69 2b 2b 29 7b 0a 20 20 20  i<256; i++){.   
a6b50 20 20 20 77 73 64 50 72 6e 67 2e 73 5b 69 5d 20     wsdPrng.s[i] 
a6b60 3d 20 28 75 38 29 69 3b 0a 20 20 20 20 7d 0a 20  = (u8)i;.    }. 
a6b70 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 35     for(i=0; i<25
a6b80 36 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 77  6; i++){.      w
a6b90 73 64 50 72 6e 67 2e 6a 20 2b 3d 20 77 73 64 50  sdPrng.j += wsdP
a6ba0 72 6e 67 2e 73 5b 69 5d 20 2b 20 6b 5b 69 5d 3b  rng.s[i] + k[i];
a6bb0 0a 20 20 20 20 20 20 74 20 3d 20 77 73 64 50 72  .      t = wsdPr
a6bc0 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 6a 5d 3b  ng.s[wsdPrng.j];
a6bd0 0a 20 20 20 20 20 20 77 73 64 50 72 6e 67 2e 73  .      wsdPrng.s
a6be0 5b 77 73 64 50 72 6e 67 2e 6a 5d 20 3d 20 77 73  [wsdPrng.j] = ws
a6bf0 64 50 72 6e 67 2e 73 5b 69 5d 3b 0a 20 20 20 20  dPrng.s[i];.    
a6c00 20 20 77 73 64 50 72 6e 67 2e 73 5b 69 5d 20 3d    wsdPrng.s[i] =
a6c10 20 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 77 73   t;.    }.    ws
a6c20 64 50 72 6e 67 2e 69 73 49 6e 69 74 20 3d 20 31  dPrng.isInit = 1
a6c30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65  ;.  }..  /* Gene
a6c40 72 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20  rate and return 
a6c50 73 69 6e 67 6c 65 20 72 61 6e 64 6f 6d 20 62 79  single random by
a6c60 74 65 0a 20 20 2a 2f 0a 20 20 77 73 64 50 72 6e  te.  */.  wsdPrn
a6c70 67 2e 69 2b 2b 3b 0a 20 20 74 20 3d 20 77 73 64  g.i++;.  t = wsd
a6c80 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 69  Prng.s[wsdPrng.i
a6c90 5d 3b 0a 20 20 77 73 64 50 72 6e 67 2e 6a 20 2b  ];.  wsdPrng.j +
a6ca0 3d 20 74 3b 0a 20 20 77 73 64 50 72 6e 67 2e 73  = t;.  wsdPrng.s
a6cb0 5b 77 73 64 50 72 6e 67 2e 69 5d 20 3d 20 77 73  [wsdPrng.i] = ws
a6cc0 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e  dPrng.s[wsdPrng.
a6cd0 6a 5d 3b 0a 20 20 77 73 64 50 72 6e 67 2e 73 5b  j];.  wsdPrng.s[
a6ce0 77 73 64 50 72 6e 67 2e 6a 5d 20 3d 20 74 3b 0a  wsdPrng.j] = t;.
a6cf0 20 20 74 20 2b 3d 20 77 73 64 50 72 6e 67 2e 73    t += wsdPrng.s
a6d00 5b 77 73 64 50 72 6e 67 2e 69 5d 3b 0a 20 20 72  [wsdPrng.i];.  r
a6d10 65 74 75 72 6e 20 77 73 64 50 72 6e 67 2e 73 5b  eturn wsdPrng.s[
a6d20 74 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  t];.}../*.** Ret
a6d30 75 72 6e 20 4e 20 72 61 6e 64 6f 6d 20 62 79 74  urn N random byt
a6d40 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  es..*/.SQLITE_AP
a6d50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
a6d60 61 6e 64 6f 6d 6e 65 73 73 28 69 6e 74 20 4e 2c  andomness(int N,
a6d70 20 76 6f 69 64 20 2a 70 42 75 66 29 7b 0a 20 20   void *pBuf){.  
a6d80 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
a6d90 42 75 66 20 3d 20 70 42 75 66 3b 0a 23 69 66 20  Buf = pBuf;.#if 
a6da0 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
a6db0 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  E.  sqlite3_mute
a6dc0 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c 69 74  x *mutex = sqlit
a6dd0 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
a6de0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
a6df0 5f 50 52 4e 47 29 3b 0a 23 65 6e 64 69 66 0a 20  _PRNG);.#endif. 
a6e00 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
a6e10 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 77  nter(mutex);.  w
a6e20 68 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20  hile( N-- ){.   
a6e30 20 2a 28 7a 42 75 66 2b 2b 29 20 3d 20 72 61 6e   *(zBuf++) = ran
a6e40 64 6f 6d 42 79 74 65 28 29 3b 0a 20 20 7d 0a 20  domByte();.  }. 
a6e50 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
a6e60 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 7d 0a 0a  eave(mutex);.}..
a6e70 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
a6e80 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54  MIT_BUILTIN_TEST
a6e90 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 74 65 73 74 69  ./*.** For testi
a6ea0 6e 67 20 70 75 72 70 6f 73 65 73 2c 20 77 65 20  ng purposes, we 
a6eb0 73 6f 6d 65 74 69 6d 65 73 20 77 61 6e 74 20 74  sometimes want t
a6ec0 6f 20 70 72 65 73 65 72 76 65 20 74 68 65 20 73  o preserve the s
a6ed0 74 61 74 65 20 6f 66 0a 2a 2a 20 50 52 4e 47 20  tate of.** PRNG 
a6ee0 61 6e 64 20 72 65 73 74 6f 72 65 20 74 68 65 20  and restore the 
a6ef0 50 52 4e 47 20 74 6f 20 69 74 73 20 73 61 76 65  PRNG to its save
a6f00 64 20 73 74 61 74 65 20 61 74 20 61 20 6c 61 74  d state at a lat
a6f10 65 72 20 74 69 6d 65 2c 20 6f 72 0a 2a 2a 20 74  er time, or.** t
a6f20 6f 20 72 65 73 65 74 20 74 68 65 20 50 52 4e 47  o reset the PRNG
a6f30 20 74 6f 20 69 74 73 20 69 6e 69 74 69 61 6c 20   to its initial 
a6f40 73 74 61 74 65 2e 20 20 54 68 65 73 65 20 72 6f  state.  These ro
a6f50 75 74 69 6e 65 73 20 61 63 63 6f 6d 70 6c 69 73  utines accomplis
a6f60 68 0a 2a 2a 20 74 68 6f 73 65 20 74 61 73 6b 73  h.** those tasks
a6f70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
a6f80 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c  te3_test_control
a6f90 28 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 6c  () interface cal
a6fa0 6c 73 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  ls these routine
a6fb0 73 20 74 6f 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 20  s to.** control 
a6fc0 74 68 65 20 50 52 4e 47 2e 0a 2a 2f 0a 73 74 61  the PRNG..*/.sta
a6fd0 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73  tic SQLITE_WSD s
a6fe0 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e  truct sqlite3Prn
a6ff0 67 54 79 70 65 20 73 71 6c 69 74 65 33 53 61 76  gType sqlite3Sav
a7000 65 64 50 72 6e 67 3b 0a 53 51 4c 49 54 45 5f 50  edPrng;.SQLITE_P
a7010 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
a7020 74 65 33 50 72 6e 67 53 61 76 65 53 74 61 74 65  te3PrngSaveState
a7030 28 76 6f 69 64 29 7b 0a 20 20 6d 65 6d 63 70 79  (void){.  memcpy
a7040 28 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 74  (.    &GLOBAL(st
a7050 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67  ruct sqlite3Prng
a7060 54 79 70 65 2c 20 73 71 6c 69 74 65 33 53 61 76  Type, sqlite3Sav
a7070 65 64 50 72 6e 67 29 2c 0a 20 20 20 20 26 47 4c  edPrng),.    &GL
a7080 4f 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69  OBAL(struct sqli
a7090 74 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c  te3PrngType, sql
a70a0 69 74 65 33 50 72 6e 67 29 2c 0a 20 20 20 20 73  ite3Prng),.    s
a70b0 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 50 72 6e  izeof(sqlite3Prn
a70c0 67 29 0a 20 20 29 3b 0a 7d 0a 53 51 4c 49 54 45  g).  );.}.SQLITE
a70d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
a70e0 6c 69 74 65 33 50 72 6e 67 52 65 73 74 6f 72 65  lite3PrngRestore
a70f0 53 74 61 74 65 28 76 6f 69 64 29 7b 0a 20 20 6d  State(void){.  m
a7100 65 6d 63 70 79 28 0a 20 20 20 20 26 47 4c 4f 42  emcpy(.    &GLOB
a7110 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 74 65  AL(struct sqlite
a7120 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c 69 74  3PrngType, sqlit
a7130 65 33 50 72 6e 67 29 2c 0a 20 20 20 20 26 47 4c  e3Prng),.    &GL
a7140 4f 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69  OBAL(struct sqli
a7150 74 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c  te3PrngType, sql
a7160 69 74 65 33 53 61 76 65 64 50 72 6e 67 29 2c 0a  ite3SavedPrng),.
a7170 20 20 20 20 73 69 7a 65 6f 66 28 73 71 6c 69 74      sizeof(sqlit
a7180 65 33 50 72 6e 67 29 0a 20 20 29 3b 0a 7d 0a 53  e3Prng).  );.}.S
a7190 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
a71a0 69 64 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65  id sqlite3PrngRe
a71b0 73 65 74 53 74 61 74 65 28 76 6f 69 64 29 7b 0a  setState(void){.
a71c0 20 20 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20    GLOBAL(struct 
a71d0 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c  sqlite3PrngType,
a71e0 20 73 71 6c 69 74 65 33 50 72 6e 67 29 2e 69 73   sqlite3Prng).is
a71f0 49 6e 69 74 20 3d 20 30 3b 0a 7d 0a 23 65 6e 64  Init = 0;.}.#end
a7200 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
a7210 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 2a  T_BUILTIN_TEST *
a7220 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
a7230 2a 2a 20 45 6e 64 20 6f 66 20 72 61 6e 64 6f 6d  ** End of random
a7240 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
a7250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7270 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
a7280 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 75 74  ** Begin file ut
a7290 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f.c ************
a72a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a72b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a72c0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 41 70  */./*.** 2004 Ap
a72d0 72 69 6c 20 31 33 0a 2a 2a 0a 2a 2a 20 54 68 65  ril 13.**.** The
a72e0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
a72f0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
a7300 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
a7310 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
a7320 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
a7330 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
a7340 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
a7350 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
a7360 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
a7370 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
a7380 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
a7390 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
a73a0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
a73b0 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
a73c0 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
a73d0 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
a73e0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
a73f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7430 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
a7440 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 74  le contains rout
a7450 69 6e 65 73 20 75 73 65 64 20 74 6f 20 74 72 61  ines used to tra
a7460 6e 73 6c 61 74 65 20 62 65 74 77 65 65 6e 20 55  nslate between U
a7470 54 46 2d 38 2c 20 0a 2a 2a 20 55 54 46 2d 31 36  TF-8, .** UTF-16
a7480 2c 20 55 54 46 2d 31 36 42 45 2c 20 61 6e 64 20  , UTF-16BE, and 
a7490 55 54 46 2d 31 36 4c 45 2e 0a 2a 2a 0a 2a 2a 20  UTF-16LE..**.** 
a74a0 24 49 64 3a 20 75 74 66 2e 63 2c 76 20 31 2e 37  $Id: utf.c,v 1.7
a74b0 33 20 32 30 30 39 2f 30 34 2f 30 31 20 31 38 3a  3 2009/04/01 18:
a74c0 34 30 3a 33 32 20 64 72 68 20 45 78 70 20 24 0a  40:32 drh Exp $.
a74d0 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 20 6f 6e 20 55  **.** Notes on U
a74e0 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a 20 20 20 42 79  TF-8:.**.**   By
a74f0 74 65 2d 30 20 20 20 20 42 79 74 65 2d 31 20 20  te-0    Byte-1  
a7500 20 20 42 79 74 65 2d 32 20 20 20 20 42 79 74 65    Byte-2    Byte
a7510 2d 33 20 20 20 20 56 61 6c 75 65 0a 2a 2a 20 20  -3    Value.**  
a7520 30 78 78 78 78 78 78 78 20 20 20 20 20 20 20 20  0xxxxxxx        
a7530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7540 20 20 20 20 20 20 20 20 20 30 30 30 30 30 30 30           0000000
a7550 30 20 30 30 30 30 30 30 30 30 20 30 78 78 78 78  0 00000000 0xxxx
a7560 78 78 78 0a 2a 2a 20 20 31 31 30 79 79 79 79 79  xxx.**  110yyyyy
a7570 20 20 31 30 78 78 78 78 78 78 20 20 20 20 20 20    10xxxxxx      
a7580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7590 20 30 30 30 30 30 30 30 30 20 30 30 30 30 30 79   00000000 00000y
a75a0 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 20 20  yy yyxxxxxx.**  
a75b0 31 31 31 30 7a 7a 7a 7a 20 20 31 30 79 79 79 79  1110zzzz  10yyyy
a75c0 79 79 20 20 31 30 78 78 78 78 78 78 20 20 20 20  yy  10xxxxxx    
a75d0 20 20 20 20 20 20 20 20 20 30 30 30 30 30 30 30           0000000
a75e0 30 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 78  0 zzzzyyyy yyxxx
a75f0 78 78 78 0a 2a 2a 20 20 31 31 31 31 30 75 75 75  xxx.**  11110uuu
a7600 20 20 31 30 75 75 7a 7a 7a 7a 20 20 31 30 79 79    10uuzzzz  10yy
a7610 79 79 79 79 20 20 31 30 78 78 78 78 78 78 20 20  yyyy  10xxxxxx  
a7620 20 30 30 30 75 75 75 75 75 20 7a 7a 7a 7a 79 79   000uuuuu zzzzyy
a7630 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 0a 2a  yy yyxxxxxx.**.*
a7640 2a 0a 2a 2a 20 4e 6f 74 65 73 20 6f 6e 20 55 54  *.** Notes on UT
a7650 46 2d 31 36 3a 20 20 28 77 69 74 68 20 77 77 77  F-16:  (with www
a7660 77 2b 31 3d 3d 75 75 75 75 75 29 0a 2a 2a 0a 2a  w+1==uuuuu).**.*
a7670 2a 20 20 20 20 20 20 57 6f 72 64 2d 30 20 20 20  *      Word-0   
a7680 20 20 20 20 20 20 20 20 20 20 20 20 57 6f 72 64              Word
a7690 2d 31 20 20 20 20 20 20 20 20 20 20 56 61 6c 75  -1          Valu
a76a0 65 0a 2a 2a 20 20 31 31 30 31 31 30 77 77 20 77  e.**  110110ww w
a76b0 77 7a 7a 7a 7a 79 79 20 20 20 31 31 30 31 31 31  wzzzzyy   110111
a76c0 79 79 20 79 79 78 78 78 78 78 78 20 20 20 20 30  yy yyxxxxxx    0
a76d0 30 30 75 75 75 75 75 20 7a 7a 7a 7a 79 79 79 79  00uuuuu zzzzyyyy
a76e0 20 79 79 78 78 78 78 78 78 0a 2a 2a 20 20 7a 7a   yyxxxxxx.**  zz
a76f0 7a 7a 79 79 79 79 20 79 79 78 78 78 78 78 78 20  zzyyyy yyxxxxxx 
a7700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7710 20 20 20 20 20 20 20 30 30 30 30 30 30 30 30 20         00000000 
a7720 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 78 78  zzzzyyyy yyxxxxx
a7730 78 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 42 4f 4d 20 6f  x.**.**.** BOM o
a7740 72 20 42 79 74 65 20 4f 72 64 65 72 20 4d 61 72  r Byte Order Mar
a7750 6b 3a 0a 2a 2a 20 20 20 20 20 30 78 66 66 20 30  k:.**     0xff 0
a7760 78 66 65 20 20 20 6c 69 74 74 6c 65 2d 65 6e 64  xfe   little-end
a7770 69 61 6e 20 75 74 66 2d 31 36 20 66 6f 6c 6c 6f  ian utf-16 follo
a7780 77 73 0a 2a 2a 20 20 20 20 20 30 78 66 65 20 30  ws.**     0xfe 0
a7790 78 66 66 20 20 20 62 69 67 2d 65 6e 64 69 61 6e  xff   big-endian
a77a0 20 75 74 66 2d 31 36 20 66 6f 6c 6c 6f 77 73 0a   utf-16 follows.
a77b0 2a 2a 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*/./*********
a77c0 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 76 64  ***** Include vd
a77d0 62 65 49 6e 74 2e 68 20 69 6e 20 74 68 65 20 6d  beInt.h in the m
a77e0 69 64 64 6c 65 20 6f 66 20 75 74 66 2e 63 20 2a  iddle of utf.c *
a77f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7800 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
a7810 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
a7820 20 76 64 62 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a   vdbeInt.h *****
a7830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7850 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33  ****/./*.** 2003
a7860 20 53 65 70 74 65 6d 62 65 72 20 36 0a 2a 2a 0a   September 6.**.
a7870 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
a7880 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
a7890 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
a78a0 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
a78b0 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
a78c0 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
a78d0 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
a78e0 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
a78f0 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
a7900 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
a7910 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
a7920 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
a7930 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
a7940 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
a7950 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
a7960 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
a7970 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
a7980 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
a7990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a79a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a79b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a79c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
a79d0 68 69 73 20 69 73 20 74 68 65 20 68 65 61 64 65  his is the heade
a79e0 72 20 66 69 6c 65 20 66 6f 72 20 69 6e 66 6f 72  r file for infor
a79f0 6d 61 74 69 6f 6e 20 74 68 61 74 20 69 73 20 70  mation that is p
a7a00 72 69 76 61 74 65 20 74 6f 20 74 68 65 0a 2a 2a  rivate to the.**
a7a10 20 56 44 42 45 2e 20 20 54 68 69 73 20 69 6e 66   VDBE.  This inf
a7a20 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 74 6f  ormation used to
a7a30 20 61 6c 6c 20 62 65 20 61 74 20 74 68 65 20 74   all be at the t
a7a40 6f 70 20 6f 66 20 74 68 65 20 73 69 6e 67 6c 65  op of the single
a7a50 0a 2a 2a 20 73 6f 75 72 63 65 20 63 6f 64 65 20  .** source code 
a7a60 66 69 6c 65 20 22 76 64 62 65 2e 63 22 2e 20 20  file "vdbe.c".  
a7a70 57 68 65 6e 20 74 68 61 74 20 66 69 6c 65 20 62  When that file b
a7a80 65 63 61 6d 65 20 74 6f 6f 20 62 69 67 20 28 6f  ecame too big (o
a7a90 76 65 72 0a 2a 2a 20 36 30 30 30 20 6c 69 6e 65  ver.** 6000 line
a7aa0 73 20 6c 6f 6e 67 29 20 69 74 20 77 61 73 20 73  s long) it was s
a7ab0 70 6c 69 74 20 75 70 20 69 6e 74 6f 20 73 65 76  plit up into sev
a7ac0 65 72 61 6c 20 73 6d 61 6c 6c 65 72 20 66 69 6c  eral smaller fil
a7ad0 65 73 20 61 6e 64 0a 2a 2a 20 74 68 69 73 20 68  es and.** this h
a7ae0 65 61 64 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f  eader informatio
a7af0 6e 20 77 61 73 20 66 61 63 74 6f 72 65 64 20 6f  n was factored o
a7b00 75 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76  ut..**.** $Id: v
a7b10 64 62 65 49 6e 74 2e 68 2c 76 20 31 2e 31 37 34  dbeInt.h,v 1.174
a7b20 20 32 30 30 39 2f 30 36 2f 32 33 20 31 34 3a 31   2009/06/23 14:1
a7b30 35 3a 30 34 20 64 72 68 20 45 78 70 20 24 0a 2a  5:04 drh Exp $.*
a7b40 2f 0a 23 69 66 6e 64 65 66 20 5f 56 44 42 45 49  /.#ifndef _VDBEI
a7b50 4e 54 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 56  NT_H_.#define _V
a7b60 44 42 45 49 4e 54 5f 48 5f 0a 0a 2f 2a 0a 2a 2a  DBEINT_H_../*.**
a7b70 20 53 51 4c 20 69 73 20 74 72 61 6e 73 6c 61 74   SQL is translat
a7b80 65 64 20 69 6e 74 6f 20 61 20 73 65 71 75 65 6e  ed into a sequen
a7b90 63 65 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f  ce of instructio
a7ba0 6e 73 20 74 6f 20 62 65 0a 2a 2a 20 65 78 65 63  ns to be.** exec
a7bb0 75 74 65 64 20 62 79 20 61 20 76 69 72 74 75 61  uted by a virtua
a7bc0 6c 20 6d 61 63 68 69 6e 65 2e 20 20 45 61 63 68  l machine.  Each
a7bd0 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20   instruction is 
a7be0 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f  an instance.** o
a7bf0 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
a7c00 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79  structure..*/.ty
a7c10 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62  pedef struct Vdb
a7c20 65 4f 70 20 4f 70 3b 0a 0a 2f 2a 0a 2a 2a 20 42  eOp Op;../*.** B
a7c30 6f 6f 6c 65 61 6e 20 76 61 6c 75 65 73 0a 2a 2f  oolean values.*/
a7c40 0a 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65  .typedef unsigne
a7c50 64 20 63 68 61 72 20 42 6f 6f 6c 3b 0a 0a 2f 2a  d char Bool;../*
a7c60 0a 2a 2a 20 41 20 63 75 72 73 6f 72 20 69 73 20  .** A cursor is 
a7c70 61 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f 20 61  a pointer into a
a7c80 20 73 69 6e 67 6c 65 20 42 54 72 65 65 20 77 69   single BTree wi
a7c90 74 68 69 6e 20 61 20 64 61 74 61 62 61 73 65 20  thin a database 
a7ca0 66 69 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 75 72  file..** The cur
a7cb0 73 6f 72 20 63 61 6e 20 73 65 65 6b 20 74 6f 20  sor can seek to 
a7cc0 61 20 42 54 72 65 65 20 65 6e 74 72 79 20 77 69  a BTree entry wi
a7cd0 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  th a particular 
a7ce0 6b 65 79 2c 20 6f 72 0a 2a 2a 20 6c 6f 6f 70 20  key, or.** loop 
a7cf0 6f 76 65 72 20 61 6c 6c 20 65 6e 74 72 69 65 73  over all entries
a7d00 20 6f 66 20 74 68 65 20 42 74 72 65 65 2e 20 20   of the Btree.  
a7d10 59 6f 75 20 63 61 6e 20 61 6c 73 6f 20 69 6e 73  You can also ins
a7d20 65 72 74 20 6e 65 77 20 42 54 72 65 65 0a 2a 2a  ert new BTree.**
a7d30 20 65 6e 74 72 69 65 73 20 6f 72 20 72 65 74 72   entries or retr
a7d40 69 65 76 65 20 74 68 65 20 6b 65 79 20 6f 72 20  ieve the key or 
a7d50 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 65 6e  data from the en
a7d60 74 72 79 20 74 68 61 74 20 74 68 65 20 63 75 72  try that the cur
a7d70 73 6f 72 0a 2a 2a 20 69 73 20 63 75 72 72 65 6e  sor.** is curren
a7d80 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e  tly pointing to.
a7d90 0a 2a 2a 20 0a 2a 2a 20 45 76 65 72 79 20 63 75  .** .** Every cu
a7da0 72 73 6f 72 20 74 68 61 74 20 74 68 65 20 76 69  rsor that the vi
a7db0 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 68 61  rtual machine ha
a7dc0 73 20 6f 70 65 6e 20 69 73 20 72 65 70 72 65 73  s open is repres
a7dd0 65 6e 74 65 64 20 62 79 20 61 6e 0a 2a 2a 20 69  ented by an.** i
a7de0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
a7df0 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
a7e00 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  re..**.** If the
a7e10 20 56 64 62 65 43 75 72 73 6f 72 2e 69 73 54 72   VdbeCursor.isTr
a7e20 69 67 67 65 72 52 6f 77 20 66 6c 61 67 20 69 73  iggerRow flag is
a7e30 20 73 65 74 20 69 74 20 6d 65 61 6e 73 20 74 68   set it means th
a7e40 61 74 20 74 68 69 73 20 63 75 72 73 6f 72 20 69  at this cursor i
a7e50 73 0a 2a 2a 20 72 65 61 6c 6c 79 20 61 20 73 69  s.** really a si
a7e60 6e 67 6c 65 20 72 6f 77 20 74 68 61 74 20 72 65  ngle row that re
a7e70 70 72 65 73 65 6e 74 73 20 74 68 65 20 4e 45 57  presents the NEW
a7e80 20 6f 72 20 4f 4c 44 20 70 73 65 75 64 6f 2d 74   or OLD pseudo-t
a7e90 61 62 6c 65 20 6f 66 0a 2a 2a 20 61 20 72 6f 77  able of.** a row
a7ea0 20 74 72 69 67 67 65 72 2e 20 20 54 68 65 20 64   trigger.  The d
a7eb0 61 74 61 20 66 6f 72 20 74 68 65 20 72 6f 77 20  ata for the row 
a7ec0 69 73 20 73 74 6f 72 65 64 20 69 6e 20 56 64 62  is stored in Vdb
a7ed0 65 43 75 72 73 6f 72 2e 70 44 61 74 61 20 61 6e  eCursor.pData an
a7ee0 64 0a 2a 2a 20 74 68 65 20 72 6f 77 69 64 20 69  d.** the rowid i
a7ef0 73 20 69 6e 20 56 64 62 65 43 75 72 73 6f 72 2e  s in VdbeCursor.
a7f00 69 4b 65 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  iKey..*/.struct 
a7f10 56 64 62 65 43 75 72 73 6f 72 20 7b 0a 20 20 42  VdbeCursor {.  B
a7f20 74 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  tCursor *pCursor
a7f30 3b 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73  ;    /* The curs
a7f40 6f 72 20 73 74 72 75 63 74 75 72 65 20 6f 66 20  or structure of 
a7f50 74 68 65 20 62 61 63 6b 65 6e 64 20 2a 2f 0a 20  the backend */. 
a7f60 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20   int iDb;       
a7f70 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
a7f80 6f 66 20 63 75 72 73 6f 72 20 64 61 74 61 62 61  of cursor databa
a7f90 73 65 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 20  se in db->aDb[] 
a7fa0 28 6f 72 20 2d 31 29 20 2a 2f 0a 20 20 69 36 34  (or -1) */.  i64
a7fb0 20 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 20 20   lastRowid;     
a7fc0 20 20 20 2f 2a 20 4c 61 73 74 20 72 6f 77 69 64     /* Last rowid
a7fd0 20 66 72 6f 6d 20 61 20 4e 65 78 74 20 6f 72 20   from a Next or 
a7fe0 4e 65 78 74 49 64 78 20 6f 70 65 72 61 74 69 6f  NextIdx operatio
a7ff0 6e 20 2a 2f 0a 20 20 42 6f 6f 6c 20 7a 65 72 6f  n */.  Bool zero
a8000 65 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ed;          /* 
a8010 54 72 75 65 20 69 66 20 7a 65 72 6f 65 64 20 6f  True if zeroed o
a8020 75 74 20 61 6e 64 20 72 65 61 64 79 20 66 6f 72  ut and ready for
a8030 20 72 65 75 73 65 20 2a 2f 0a 20 20 42 6f 6f 6c   reuse */.  Bool
a8040 20 72 6f 77 69 64 49 73 56 61 6c 69 64 3b 20 20   rowidIsValid;  
a8050 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6c 61 73    /* True if las
a8060 74 52 6f 77 69 64 20 69 73 20 76 61 6c 69 64 20  tRowid is valid 
a8070 2a 2f 0a 20 20 42 6f 6f 6c 20 61 74 46 69 72 73  */.  Bool atFirs
a8080 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  t;         /* Tr
a8090 75 65 20 69 66 20 70 6f 69 6e 74 69 6e 67 20 74  ue if pointing t
a80a0 6f 20 66 69 72 73 74 20 65 6e 74 72 79 20 2a 2f  o first entry */
a80b0 0a 20 20 42 6f 6f 6c 20 75 73 65 52 61 6e 64 6f  .  Bool useRando
a80c0 6d 52 6f 77 69 64 3b 20 20 2f 2a 20 47 65 6e 65  mRowid;  /* Gene
a80d0 72 61 74 65 20 6e 65 77 20 72 65 63 6f 72 64 20  rate new record 
a80e0 6e 75 6d 62 65 72 73 20 73 65 6d 69 2d 72 61 6e  numbers semi-ran
a80f0 64 6f 6d 6c 79 20 2a 2f 0a 20 20 42 6f 6f 6c 20  domly */.  Bool 
a8100 6e 75 6c 6c 52 6f 77 3b 20 20 20 20 20 20 20 20  nullRow;        
a8110 20 2f 2a 20 54 72 75 65 20 69 66 20 70 6f 69 6e   /* True if poin
a8120 74 69 6e 67 20 74 6f 20 61 20 72 6f 77 20 77 69  ting to a row wi
a8130 74 68 20 6e 6f 20 64 61 74 61 20 2a 2f 0a 20 20  th no data */.  
a8140 42 6f 6f 6c 20 64 65 66 65 72 72 65 64 4d 6f 76  Bool deferredMov
a8150 65 74 6f 3b 20 20 2f 2a 20 41 20 63 61 6c 6c 20  eto;  /* A call 
a8160 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d  to sqlite3BtreeM
a8170 6f 76 65 74 6f 28 29 20 69 73 20 6e 65 65 64 65  oveto() is neede
a8180 64 20 2a 2f 0a 20 20 42 6f 6f 6c 20 69 73 54 61  d */.  Bool isTa
a8190 62 6c 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ble;         /* 
a81a0 54 72 75 65 20 69 66 20 61 20 74 61 62 6c 65 20  True if a table 
a81b0 72 65 71 75 69 72 69 6e 67 20 69 6e 74 65 67 65  requiring intege
a81c0 72 20 6b 65 79 73 20 2a 2f 0a 20 20 42 6f 6f 6c  r keys */.  Bool
a81d0 20 69 73 49 6e 64 65 78 3b 20 20 20 20 20 20 20   isIndex;       
a81e0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 20    /* True if an 
a81f0 69 6e 64 65 78 20 63 6f 6e 74 61 69 6e 69 6e 67  index containing
a8200 20 6b 65 79 73 20 6f 6e 6c 79 20 2d 20 6e 6f 20   keys only - no 
a8210 64 61 74 61 20 2a 2f 0a 20 20 69 36 34 20 6d 6f  data */.  i64 mo
a8220 76 65 74 6f 54 61 72 67 65 74 3b 20 20 20 20 20  vetoTarget;     
a8230 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74  /* Argument to t
a8240 68 65 20 64 65 66 65 72 72 65 64 20 73 71 6c 69  he deferred sqli
a8250 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 29  te3BtreeMoveto()
a8260 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 74   */.  Btree *pBt
a8270 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53  ;           /* S
a8280 65 70 61 72 61 74 65 20 66 69 6c 65 20 68 6f 6c  eparate file hol
a8290 64 69 6e 67 20 74 65 6d 70 6f 72 61 72 79 20 74  ding temporary t
a82a0 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 70 73  able */.  int ps
a82b0 65 75 64 6f 54 61 62 6c 65 52 65 67 3b 20 20 20  eudoTableReg;   
a82c0 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64  /* Register hold
a82d0 69 6e 67 20 70 73 65 75 64 6f 74 61 62 6c 65 20  ing pseudotable 
a82e0 63 6f 6e 74 65 6e 74 2e 20 2a 2f 0a 20 20 4b 65  content. */.  Ke
a82f0 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b  yInfo *pKeyInfo;
a8300 20 20 20 20 2f 2a 20 49 6e 66 6f 20 61 62 6f 75      /* Info abou
a8310 74 20 69 6e 64 65 78 20 6b 65 79 73 20 6e 65 65  t index keys nee
a8320 64 65 64 20 62 79 20 69 6e 64 65 78 20 63 75 72  ded by index cur
a8330 73 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 46  sors */.  int nF
a8340 69 65 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20  ield;           
a8350 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 69 65  /* Number of fie
a8360 6c 64 73 20 69 6e 20 74 68 65 20 68 65 61 64 65  lds in the heade
a8370 72 20 2a 2f 0a 20 20 69 36 34 20 73 65 71 43 6f  r */.  i64 seqCo
a8380 75 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  unt;         /* 
a8390 53 65 71 75 65 6e 63 65 20 63 6f 75 6e 74 65 72  Sequence counter
a83a0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74   */.  sqlite3_vt
a83b0 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62  ab_cursor *pVtab
a83c0 43 75 72 73 6f 72 3b 20 20 2f 2a 20 54 68 65 20  Cursor;  /* The 
a83d0 63 75 72 73 6f 72 20 66 6f 72 20 61 20 76 69 72  cursor for a vir
a83e0 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20  tual table */.  
a83f0 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f  const sqlite3_mo
a8400 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 20 20  dule *pModule;  
a8410 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 66 6f 72     /* Module for
a8420 20 63 75 72 73 6f 72 20 70 56 74 61 62 43 75 72   cursor pVtabCur
a8430 73 6f 72 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 73  sor */..  /* Res
a8440 75 6c 74 20 6f 66 20 6c 61 73 74 20 73 71 6c 69  ult of last sqli
a8450 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 29  te3BtreeMoveto()
a8460 20 64 6f 6e 65 20 62 79 20 61 6e 20 4f 50 5f 4e   done by an OP_N
a8470 6f 74 45 78 69 73 74 73 20 6f 72 20 0a 20 20 2a  otExists or .  *
a8480 2a 20 4f 50 5f 49 73 55 6e 69 71 75 65 20 6f 70  * OP_IsUnique op
a8490 63 6f 64 65 20 6f 6e 20 74 68 69 73 20 63 75 72  code on this cur
a84a0 73 6f 72 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 65  sor. */.  int se
a84b0 65 6b 52 65 73 75 6c 74 3b 0a 0a 20 20 2f 2a 20  ekResult;..  /* 
a84c0 43 61 63 68 65 64 20 69 6e 66 6f 72 6d 61 74 69  Cached informati
a84d0 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 68 65 61  on about the hea
a84e0 64 65 72 20 66 6f 72 20 74 68 65 20 64 61 74 61  der for the data
a84f0 20 72 65 63 6f 72 64 20 74 68 61 74 20 74 68 65   record that the
a8500 0a 20 20 2a 2a 20 63 75 72 73 6f 72 20 69 73 20  .  ** cursor is 
a8510 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69  currently pointi
a8520 6e 67 20 74 6f 2e 20 20 4f 6e 6c 79 20 76 61 6c  ng to.  Only val
a8530 69 64 20 69 66 20 63 61 63 68 65 53 74 61 74 75  id if cacheStatu
a8540 73 20 6d 61 74 63 68 65 73 0a 20 20 2a 2a 20 56  s matches.  ** V
a8550 64 62 65 2e 63 61 63 68 65 43 74 72 2e 20 20 56  dbe.cacheCtr.  V
a8560 64 62 65 2e 63 61 63 68 65 43 74 72 20 77 69 6c  dbe.cacheCtr wil
a8570 6c 20 6e 65 76 65 72 20 74 61 6b 65 20 6f 6e 20  l never take on 
a8580 74 68 65 20 76 61 6c 75 65 20 6f 66 0a 20 20 2a  the value of.  *
a8590 2a 20 43 41 43 48 45 5f 53 54 41 4c 45 20 61 6e  * CACHE_STALE an
a85a0 64 20 73 6f 20 73 65 74 74 69 6e 67 20 63 61 63  d so setting cac
a85b0 68 65 53 74 61 74 75 73 3d 43 41 43 48 45 5f 53  heStatus=CACHE_S
a85c0 54 41 4c 45 20 67 75 61 72 61 6e 74 65 65 73 20  TALE guarantees 
a85d0 74 68 61 74 0a 20 20 2a 2a 20 74 68 65 20 63 61  that.  ** the ca
a85e0 63 68 65 20 69 73 20 6f 75 74 20 6f 66 20 64 61  che is out of da
a85f0 74 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 61 52  te..  **.  ** aR
a8600 6f 77 20 6d 69 67 68 74 20 70 6f 69 6e 74 20 74  ow might point t
a8610 6f 20 28 65 70 68 65 6d 65 72 61 6c 29 20 64 61  o (ephemeral) da
a8620 74 61 20 66 6f 72 20 74 68 65 20 63 75 72 72 65  ta for the curre
a8630 6e 74 20 72 6f 77 2c 20 6f 72 20 69 74 20 6d 69  nt row, or it mi
a8640 67 68 74 0a 20 20 2a 2a 20 62 65 20 4e 55 4c 4c  ght.  ** be NULL
a8650 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 63 61 63  ..  */.  u32 cac
a8660 68 65 53 74 61 74 75 73 3b 20 20 20 20 20 20 2f  heStatus;      /
a8670 2a 20 43 61 63 68 65 20 69 73 20 76 61 6c 69 64  * Cache is valid
a8680 20 69 66 20 74 68 69 73 20 6d 61 74 63 68 65 73   if this matches
a8690 20 56 64 62 65 2e 63 61 63 68 65 43 74 72 20 2a   Vdbe.cacheCtr *
a86a0 2f 0a 20 20 69 6e 74 20 70 61 79 6c 6f 61 64 53  /.  int payloadS
a86b0 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20 54 6f 74  ize;      /* Tot
a86c0 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  al number of byt
a86d0 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64  es in the record
a86e0 20 2a 2f 0a 20 20 75 33 32 20 2a 61 54 79 70 65   */.  u32 *aType
a86f0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
a8700 79 70 65 20 76 61 6c 75 65 73 20 66 6f 72 20 61  ype values for a
a8710 6c 6c 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68  ll entries in th
a8720 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 33  e record */.  u3
a8730 32 20 2a 61 4f 66 66 73 65 74 3b 20 20 20 20 20  2 *aOffset;     
a8740 20 20 20 20 2f 2a 20 43 61 63 68 65 64 20 6f 66      /* Cached of
a8750 66 73 65 74 73 20 74 6f 20 74 68 65 20 73 74 61  fsets to the sta
a8760 72 74 20 6f 66 20 65 61 63 68 20 63 6f 6c 75 6d  rt of each colum
a8770 6e 73 20 64 61 74 61 20 2a 2f 0a 20 20 75 38 20  ns data */.  u8 
a8780 2a 61 52 6f 77 3b 20 20 20 20 20 20 20 20 20 20  *aRow;          
a8790 20 20 20 2f 2a 20 44 61 74 61 20 66 6f 72 20 74     /* Data for t
a87a0 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 2c 20  he current row, 
a87b0 69 66 20 61 6c 6c 20 6f 6e 20 6f 6e 65 20 70 61  if all on one pa
a87c0 67 65 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66  ge */.};.typedef
a87d0 20 73 74 72 75 63 74 20 56 64 62 65 43 75 72 73   struct VdbeCurs
a87e0 6f 72 20 56 64 62 65 43 75 72 73 6f 72 3b 0a 0a  or VdbeCursor;..
a87f0 2f 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 73 75 62  /*.** When a sub
a8800 2d 70 72 6f 67 72 61 6d 20 69 73 20 65 78 65 63  -program is exec
a8810 75 74 65 64 20 28 4f 50 5f 50 72 6f 67 72 61 6d  uted (OP_Program
a8820 29 2c 20 61 20 73 74 72 75 63 74 75 72 65 20 6f  ), a structure o
a8830 66 20 74 68 69 73 20 74 79 70 65 0a 2a 2a 20 69  f this type.** i
a8840 73 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 73  s allocated to s
a8850 74 6f 72 65 20 74 68 65 20 63 75 72 72 65 6e 74  tore the current
a8860 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 70 72   value of the pr
a8870 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 2c 20 61  ogram counter, a
a8880 73 0a 2a 2a 20 77 65 6c 6c 20 61 73 20 74 68 65  s.** well as the
a8890 20 63 75 72 72 65 6e 74 20 6d 65 6d 6f 72 79 20   current memory 
a88a0 63 65 6c 6c 20 61 72 72 61 79 20 61 6e 64 20 76  cell array and v
a88b0 61 72 69 6f 75 73 20 6f 74 68 65 72 20 66 72 61  arious other fra
a88c0 6d 65 20 73 70 65 63 69 66 69 63 0a 2a 2a 20 76  me specific.** v
a88d0 61 6c 75 65 73 20 73 74 6f 72 65 64 20 69 6e 20  alues stored in 
a88e0 74 68 65 20 56 64 62 65 20 73 74 72 75 63 74 2e  the Vdbe struct.
a88f0 20 57 68 65 6e 20 74 68 65 20 73 75 62 2d 70 72   When the sub-pr
a8900 6f 67 72 61 6d 20 69 73 20 66 69 6e 69 73 68 65  ogram is finishe
a8910 64 2c 20 0a 2a 2a 20 74 68 65 73 65 20 76 61 6c  d, .** these val
a8920 75 65 73 20 61 72 65 20 63 6f 70 69 65 64 20 62  ues are copied b
a8930 61 63 6b 20 74 6f 20 74 68 65 20 56 64 62 65 20  ack to the Vdbe 
a8940 66 72 6f 6d 20 74 68 65 20 56 64 62 65 46 72 61  from the VdbeFra
a8950 6d 65 20 73 74 72 75 63 74 75 72 65 2c 0a 2a 2a  me structure,.**
a8960 20 72 65 73 74 6f 72 69 6e 67 20 74 68 65 20 73   restoring the s
a8970 74 61 74 65 20 6f 66 20 74 68 65 20 56 4d 20 74  tate of the VM t
a8980 6f 20 61 73 20 69 74 20 77 61 73 20 62 65 66 6f  o as it was befo
a8990 72 65 20 74 68 65 20 73 75 62 2d 70 72 6f 67 72  re the sub-progr
a89a0 61 6d 0a 2a 2a 20 62 65 67 61 6e 20 65 78 65 63  am.** began exec
a89b0 75 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 46 72 61  uting..**.** Fra
a89c0 6d 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 69  mes are stored i
a89d0 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  n a linked list 
a89e0 68 65 61 64 65 64 20 61 74 20 56 64 62 65 2e 70  headed at Vdbe.p
a89f0 50 61 72 65 6e 74 2e 20 56 64 62 65 2e 70 50 61  Parent. Vdbe.pPa
a8a00 72 65 6e 74 0a 2a 2a 20 69 73 20 74 68 65 20 70  rent.** is the p
a8a10 61 72 65 6e 74 20 6f 66 20 74 68 65 20 63 75 72  arent of the cur
a8a20 72 65 6e 74 20 66 72 61 6d 65 2c 20 6f 72 20 7a  rent frame, or z
a8a30 65 72 6f 20 69 66 20 74 68 65 20 63 75 72 72 65  ero if the curre
a8a40 6e 74 20 66 72 61 6d 65 0a 2a 2a 20 69 73 20 74  nt frame.** is t
a8a50 68 65 20 6d 61 69 6e 20 56 64 62 65 20 70 72 6f  he main Vdbe pro
a8a60 67 72 61 6d 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  gram..*/.typedef
a8a70 20 73 74 72 75 63 74 20 56 64 62 65 46 72 61 6d   struct VdbeFram
a8a80 65 20 56 64 62 65 46 72 61 6d 65 3b 0a 73 74 72  e VdbeFrame;.str
a8a90 75 63 74 20 56 64 62 65 46 72 61 6d 65 20 7b 0a  uct VdbeFrame {.
a8aa0 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20    Vdbe *v;      
a8ab0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 4d 20            /* VM 
a8ac0 74 68 69 73 20 66 72 61 6d 65 20 62 65 6c 6f 6e  this frame belon
a8ad0 67 73 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 70  gs to */.  int p
a8ae0 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
a8af0 20 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 43 6f     /* Program Co
a8b00 75 6e 74 65 72 20 2a 2f 0a 20 20 4f 70 20 2a 61  unter */.  Op *a
a8b10 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Op;             
a8b20 20 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 69 6e     /* Program in
a8b30 73 74 72 75 63 74 69 6f 6e 73 20 2a 2f 0a 20 20  structions */.  
a8b40 69 6e 74 20 6e 4f 70 3b 20 20 20 20 20 20 20 20  int nOp;        
a8b50 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
a8b60 6f 66 20 61 4f 70 20 61 72 72 61 79 20 2a 2f 0a  of aOp array */.
a8b70 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20 20 20 20    Mem *aMem;    
a8b80 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72            /* Arr
a8b90 61 79 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c  ay of memory cel
a8ba0 6c 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d  ls */.  int nMem
a8bb0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
a8bc0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74  /* Number of ent
a8bd0 72 69 65 73 20 69 6e 20 61 4d 65 6d 20 2a 2f 0a  ries in aMem */.
a8be0 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 2a 61    VdbeCursor **a
a8bf0 70 43 73 72 3b 20 20 20 20 20 2f 2a 20 45 6c 65  pCsr;     /* Ele
a8c00 6d 65 6e 74 20 6f 66 20 56 64 62 65 20 63 75 72  ment of Vdbe cur
a8c10 73 6f 72 73 20 2a 2f 0a 20 20 75 31 36 20 6e 43  sors */.  u16 nC
a8c20 75 72 73 6f 72 3b 20 20 20 20 20 20 20 20 20 20  ursor;          
a8c30 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
a8c40 6e 74 72 69 65 73 20 69 6e 20 61 70 43 73 72 20  ntries in apCsr 
a8c50 2a 2f 0a 20 20 76 6f 69 64 20 2a 74 6f 6b 65 6e  */.  void *token
a8c60 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
a8c70 43 6f 70 79 20 6f 66 20 53 75 62 50 72 6f 67 72  Copy of SubProgr
a8c80 61 6d 2e 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e  am.token */.  in
a8c90 74 20 6e 43 68 69 6c 64 4d 65 6d 3b 20 20 20 20  t nChildMem;    
a8ca0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
a8cb0 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20  of memory cells 
a8cc0 66 6f 72 20 63 68 69 6c 64 20 66 72 61 6d 65 20  for child frame 
a8cd0 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 69 6c 64 43  */.  int nChildC
a8ce0 73 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  sr;          /* 
a8cf0 4e 75 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72  Number of cursor
a8d00 73 20 66 6f 72 20 63 68 69 6c 64 20 66 72 61 6d  s for child fram
a8d10 65 20 2a 2f 0a 20 20 69 36 34 20 6c 61 73 74 52  e */.  i64 lastR
a8d20 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20 20 2f  owid;          /
a8d30 2a 20 4c 61 73 74 20 69 6e 73 65 72 74 20 72 6f  * Last insert ro
a8d40 77 69 64 20 28 73 71 6c 69 74 65 33 2e 6c 61 73  wid (sqlite3.las
a8d50 74 52 6f 77 69 64 29 20 2a 2f 0a 20 20 69 6e 74  tRowid) */.  int
a8d60 20 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20   nChange;       
a8d70 20 20 20 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e       /* Statemen
a8d80 74 20 63 68 61 6e 67 65 73 20 28 56 64 62 65 2e  t changes (Vdbe.
a8d90 6e 43 68 61 6e 67 65 73 29 20 20 20 20 20 2a 2f  nChanges)     */
a8da0 0a 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 50  .  VdbeFrame *pP
a8db0 61 72 65 6e 74 3b 20 20 20 20 20 2f 2a 20 50 61  arent;     /* Pa
a8dc0 72 65 6e 74 20 6f 66 20 74 68 69 73 20 66 72 61  rent of this fra
a8dd0 6d 65 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e  me */.};..#defin
a8de0 65 20 56 64 62 65 46 72 61 6d 65 4d 65 6d 28 70  e VdbeFrameMem(p
a8df0 29 20 28 28 4d 65 6d 20 2a 29 26 28 28 75 38 20  ) ((Mem *)&((u8 
a8e00 2a 29 70 29 5b 52 4f 55 4e 44 38 28 73 69 7a 65  *)p)[ROUND8(size
a8e10 6f 66 28 56 64 62 65 46 72 61 6d 65 29 29 5d 29  of(VdbeFrame))])
a8e20 0a 0a 2f 2a 0a 2a 2a 20 41 20 76 61 6c 75 65 20  ../*.** A value 
a8e30 66 6f 72 20 56 64 62 65 43 75 72 73 6f 72 2e 63  for VdbeCursor.c
a8e40 61 63 68 65 56 61 6c 69 64 20 74 68 61 74 20 6d  acheValid that m
a8e50 65 61 6e 73 20 74 68 65 20 63 61 63 68 65 20 69  eans the cache i
a8e60 73 20 61 6c 77 61 79 73 20 69 6e 76 61 6c 69 64  s always invalid
a8e70 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 41 43  ..*/.#define CAC
a8e80 48 45 5f 53 54 41 4c 45 20 30 0a 0a 2f 2a 0a 2a  HE_STALE 0../*.*
a8e90 2a 20 49 6e 74 65 72 6e 61 6c 6c 79 2c 20 74 68  * Internally, th
a8ea0 65 20 76 64 62 65 20 6d 61 6e 69 70 75 6c 61 74  e vdbe manipulat
a8eb0 65 73 20 6e 65 61 72 6c 79 20 61 6c 6c 20 53 51  es nearly all SQ
a8ec0 4c 20 76 61 6c 75 65 73 20 61 73 20 4d 65 6d 0a  L values as Mem.
a8ed0 2a 2a 20 73 74 72 75 63 74 75 72 65 73 2e 20 45  ** structures. E
a8ee0 61 63 68 20 4d 65 6d 20 73 74 72 75 63 74 20 6d  ach Mem struct m
a8ef0 61 79 20 63 61 63 68 65 20 6d 75 6c 74 69 70 6c  ay cache multipl
a8f00 65 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  e representation
a8f10 73 20 28 73 74 72 69 6e 67 2c 0a 2a 2a 20 69 6e  s (string,.** in
a8f20 74 65 67 65 72 20 65 74 63 2e 29 20 6f 66 20 74  teger etc.) of t
a8f30 68 65 20 73 61 6d 65 20 76 61 6c 75 65 2e 20 20  he same value.  
a8f40 41 20 76 61 6c 75 65 20 28 61 6e 64 20 74 68 65  A value (and the
a8f50 72 65 66 6f 72 65 20 4d 65 6d 20 73 74 72 75 63  refore Mem struc
a8f60 74 75 72 65 29 0a 2a 2a 20 68 61 73 20 74 68 65  ture).** has the
a8f70 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 72 6f 70 65   following prope
a8f80 72 74 69 65 73 3a 0a 2a 2a 0a 2a 2a 20 45 61 63  rties:.**.** Eac
a8f90 68 20 76 61 6c 75 65 20 68 61 73 20 61 20 6d 61  h value has a ma
a8fa0 6e 69 66 65 73 74 20 74 79 70 65 2e 20 54 68 65  nifest type. The
a8fb0 20 6d 61 6e 69 66 65 73 74 20 74 79 70 65 20 6f   manifest type o
a8fc0 66 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72  f the value stor
a8fd0 65 64 0a 2a 2a 20 69 6e 20 61 20 4d 65 6d 20 73  ed.** in a Mem s
a8fe0 74 72 75 63 74 20 69 73 20 72 65 74 75 72 6e 65  truct is returne
a8ff0 64 20 62 79 20 74 68 65 20 4d 65 6d 54 79 70 65  d by the MemType
a9000 28 4d 65 6d 2a 29 20 6d 61 63 72 6f 2e 20 54 68  (Mem*) macro. Th
a9010 65 20 74 79 70 65 20 69 73 0a 2a 2a 20 6f 6e 65  e type is.** one
a9020 20 6f 66 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c   of SQLITE_NULL,
a9030 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 2c   SQLITE_INTEGER,
a9040 20 53 51 4c 49 54 45 5f 52 45 41 4c 2c 20 53 51   SQLITE_REAL, SQ
a9050 4c 49 54 45 5f 54 45 58 54 20 6f 72 0a 2a 2a 20  LITE_TEXT or.** 
a9060 53 51 4c 49 54 45 5f 42 4c 4f 42 2e 0a 2a 2f 0a  SQLITE_BLOB..*/.
a9070 73 74 72 75 63 74 20 4d 65 6d 20 7b 0a 20 20 75  struct Mem {.  u
a9080 6e 69 6f 6e 20 7b 0a 20 20 20 20 69 36 34 20 69  nion {.    i64 i
a9090 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
a90a0 2a 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 2e  * Integer value.
a90b0 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 5a 65 72   */.    int nZer
a90c0 6f 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55  o;          /* U
a90d0 73 65 64 20 77 68 65 6e 20 62 69 74 20 4d 45 4d  sed when bit MEM
a90e0 5f 5a 65 72 6f 20 69 73 20 73 65 74 20 69 6e 20  _Zero is set in 
a90f0 66 6c 61 67 73 20 2a 2f 0a 20 20 20 20 46 75 6e  flags */.    Fun
a9100 63 44 65 66 20 2a 70 44 65 66 3b 20 20 20 20 20  cDef *pDef;     
a9110 20 2f 2a 20 55 73 65 64 20 6f 6e 6c 79 20 77 68   /* Used only wh
a9120 65 6e 20 66 6c 61 67 73 3d 3d 4d 45 4d 5f 41 67  en flags==MEM_Ag
a9130 67 20 2a 2f 0a 20 20 20 20 52 6f 77 53 65 74 20  g */.    RowSet 
a9140 2a 70 52 6f 77 53 65 74 3b 20 20 20 20 2f 2a 20  *pRowSet;    /* 
a9150 55 73 65 64 20 6f 6e 6c 79 20 77 68 65 6e 20 66  Used only when f
a9160 6c 61 67 73 3d 3d 4d 45 4d 5f 52 6f 77 53 65 74  lags==MEM_RowSet
a9170 20 2a 2f 0a 20 20 20 20 56 64 62 65 46 72 61 6d   */.    VdbeFram
a9180 65 20 2a 70 46 72 61 6d 65 3b 20 20 2f 2a 20 55  e *pFrame;  /* U
a9190 73 65 64 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d  sed when flags==
a91a0 4d 45 4d 5f 46 72 61 6d 65 20 2a 2f 0a 20 20 7d  MEM_Frame */.  }
a91b0 20 75 3b 0a 20 20 64 6f 75 62 6c 65 20 72 3b 20   u;.  double r; 
a91c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 61            /* Rea
a91d0 6c 20 76 61 6c 75 65 20 2a 2f 0a 20 20 73 71 6c  l value */.  sql
a91e0 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
a91f0 20 2f 2a 20 54 68 65 20 61 73 73 6f 63 69 61 74   /* The associat
a9200 65 64 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ed database conn
a9210 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 68 61 72  ection */.  char
a9220 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20   *z;            
a9230 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20 42 4c 4f  /* String or BLO
a9240 42 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74  B value */.  int
a9250 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   n;             
a9260 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68   /* Number of ch
a9270 61 72 61 63 74 65 72 73 20 69 6e 20 73 74 72 69  aracters in stri
a9280 6e 67 20 76 61 6c 75 65 2c 20 65 78 63 6c 75 64  ng value, exclud
a9290 69 6e 67 20 27 5c 30 27 20 2a 2f 0a 20 20 75 31  ing '\0' */.  u1
a92a0 36 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20  6 flags;        
a92b0 20 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d 62 69 6e    /* Some combin
a92c0 61 74 69 6f 6e 20 6f 66 20 4d 45 4d 5f 4e 75 6c  ation of MEM_Nul
a92d0 6c 2c 20 4d 45 4d 5f 53 74 72 2c 20 4d 45 4d 5f  l, MEM_Str, MEM_
a92e0 44 79 6e 2c 20 65 74 63 2e 20 2a 2f 0a 20 20 75  Dyn, etc. */.  u
a92f0 38 20 20 74 79 70 65 3b 20 20 20 20 20 20 20 20  8  type;        
a9300 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 53 51 4c     /* One of SQL
a9310 49 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54 45  ITE_NULL, SQLITE
a9320 5f 54 45 58 54 2c 20 53 51 4c 49 54 45 5f 49 4e  _TEXT, SQLITE_IN
a9330 54 45 47 45 52 2c 20 65 74 63 20 2a 2f 0a 20 20  TEGER, etc */.  
a9340 75 38 20 20 65 6e 63 3b 20 20 20 20 20 20 20 20  u8  enc;        
a9350 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 55 54      /* SQLITE_UT
a9360 46 38 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36  F8, SQLITE_UTF16
a9370 42 45 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36  BE, SQLITE_UTF16
a9380 4c 45 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78  LE */.  void (*x
a9390 44 65 6c 29 28 76 6f 69 64 20 2a 29 3b 20 20 2f  Del)(void *);  /
a93a0 2a 20 49 66 20 6e 6f 74 20 6e 75 6c 6c 2c 20 63  * If not null, c
a93b0 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  all this functio
a93c0 6e 20 74 6f 20 64 65 6c 65 74 65 20 4d 65 6d 2e  n to delete Mem.
a93d0 7a 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61  z */.  char *zMa
a93e0 6c 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20 44 79  lloc;      /* Dy
a93f0 6e 61 6d 69 63 20 62 75 66 66 65 72 20 61 6c 6c  namic buffer all
a9400 6f 63 61 74 65 64 20 62 79 20 73 71 6c 69 74 65  ocated by sqlite
a9410 33 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 7d 3b  3_malloc() */.};
a9420 0a 0a 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65  ../* One or more
a9430 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
a9440 67 20 66 6c 61 67 73 20 61 72 65 20 73 65 74 20  g flags are set 
a9450 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 65 20  to indicate the 
a9460 76 61 6c 69 64 4f 4b 0a 2a 2a 20 72 65 70 72 65  validOK.** repre
a9470 73 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68  sentations of th
a9480 65 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69  e value stored i
a9490 6e 20 74 68 65 20 4d 65 6d 20 73 74 72 75 63 74  n the Mem struct
a94a0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4d  ..**.** If the M
a94b0 45 4d 5f 4e 75 6c 6c 20 66 6c 61 67 20 69 73 20  EM_Null flag is 
a94c0 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 76 61  set, then the va
a94d0 6c 75 65 20 69 73 20 61 6e 20 53 51 4c 20 4e 55  lue is an SQL NU
a94e0 4c 4c 20 76 61 6c 75 65 2e 0a 2a 2a 20 4e 6f 20  LL value..** No 
a94f0 6f 74 68 65 72 20 66 6c 61 67 73 20 6d 61 79 20  other flags may 
a9500 62 65 20 73 65 74 20 69 6e 20 74 68 69 73 20 63  be set in this c
a9510 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ase..**.** If th
a9520 65 20 4d 45 4d 5f 53 74 72 20 66 6c 61 67 20 69  e MEM_Str flag i
a9530 73 20 73 65 74 20 74 68 65 6e 20 4d 65 6d 2e 7a  s set then Mem.z
a9540 20 70 6f 69 6e 74 73 20 61 74 20 61 20 73 74 72   points at a str
a9550 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69  ing representati
a9560 6f 6e 2e 0a 2a 2a 20 55 73 75 61 6c 6c 79 20 74  on..** Usually t
a9570 68 69 73 20 69 73 20 65 6e 63 6f 64 65 64 20 69  his is encoded i
a9580 6e 20 74 68 65 20 73 61 6d 65 20 75 6e 69 63 6f  n the same unico
a9590 64 65 20 65 6e 63 6f 64 69 6e 67 20 61 73 20 74  de encoding as t
a95a0 68 65 20 6d 61 69 6e 0a 2a 2a 20 64 61 74 61 62  he main.** datab
a95b0 61 73 65 20 28 73 65 65 20 62 65 6c 6f 77 20 66  ase (see below f
a95c0 6f 72 20 65 78 63 65 70 74 69 6f 6e 73 29 2e 20  or exceptions). 
a95d0 49 66 20 74 68 65 20 4d 45 4d 5f 54 65 72 6d 20  If the MEM_Term 
a95e0 66 6c 61 67 20 69 73 20 61 6c 73 6f 0a 2a 2a 20  flag is also.** 
a95f0 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 73 74  set, then the st
a9600 72 69 6e 67 20 69 73 20 6e 75 6c 20 74 65 72 6d  ring is nul term
a9610 69 6e 61 74 65 64 2e 20 54 68 65 20 4d 45 4d 5f  inated. The MEM_
a9620 49 6e 74 20 61 6e 64 20 4d 45 4d 5f 52 65 61 6c  Int and MEM_Real
a9630 20 0a 2a 2a 20 66 6c 61 67 73 20 6d 61 79 20 63   .** flags may c
a9640 6f 65 78 69 73 74 20 77 69 74 68 20 74 68 65 20  oexist with the 
a9650 4d 45 4d 5f 53 74 72 20 66 6c 61 67 2e 0a 2a 2a  MEM_Str flag..**
a9660 0a 2a 2a 20 4d 75 6c 74 69 70 6c 65 20 6f 66 20  .** Multiple of 
a9670 74 68 65 73 65 20 76 61 6c 75 65 73 20 63 61 6e  these values can
a9680 20 61 70 70 65 61 72 20 69 6e 20 4d 65 6d 2e 66   appear in Mem.f
a9690 6c 61 67 73 2e 20 20 42 75 74 20 6f 6e 6c 79 20  lags.  But only 
a96a0 6f 6e 65 0a 2a 2a 20 61 74 20 61 20 74 69 6d 65  one.** at a time
a96b0 20 63 61 6e 20 61 70 70 65 61 72 20 69 6e 20 4d   can appear in M
a96c0 65 6d 2e 74 79 70 65 2e 0a 2a 2f 0a 23 64 65 66  em.type..*/.#def
a96d0 69 6e 65 20 4d 45 4d 5f 4e 75 6c 6c 20 20 20 20  ine MEM_Null    
a96e0 20 20 30 78 30 30 30 31 20 20 20 2f 2a 20 56 61    0x0001   /* Va
a96f0 6c 75 65 20 69 73 20 4e 55 4c 4c 20 2a 2f 0a 23  lue is NULL */.#
a9700 64 65 66 69 6e 65 20 4d 45 4d 5f 53 74 72 20 20  define MEM_Str  
a9710 20 20 20 20 20 30 78 30 30 30 32 20 20 20 2f 2a       0x0002   /*
a9720 20 56 61 6c 75 65 20 69 73 20 61 20 73 74 72 69   Value is a stri
a9730 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45  ng */.#define ME
a9740 4d 5f 49 6e 74 20 20 20 20 20 20 20 30 78 30 30  M_Int       0x00
a9750 30 34 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73  04   /* Value is
a9760 20 61 6e 20 69 6e 74 65 67 65 72 20 2a 2f 0a 23   an integer */.#
a9770 64 65 66 69 6e 65 20 4d 45 4d 5f 52 65 61 6c 20  define MEM_Real 
a9780 20 20 20 20 20 30 78 30 30 30 38 20 20 20 2f 2a       0x0008   /*
a9790 20 56 61 6c 75 65 20 69 73 20 61 20 72 65 61 6c   Value is a real
a97a0 20 6e 75 6d 62 65 72 20 2a 2f 0a 23 64 65 66 69   number */.#defi
a97b0 6e 65 20 4d 45 4d 5f 42 6c 6f 62 20 20 20 20 20  ne MEM_Blob     
a97c0 20 30 78 30 30 31 30 20 20 20 2f 2a 20 56 61 6c   0x0010   /* Val
a97d0 75 65 20 69 73 20 61 20 42 4c 4f 42 20 2a 2f 0a  ue is a BLOB */.
a97e0 23 64 65 66 69 6e 65 20 4d 45 4d 5f 52 6f 77 53  #define MEM_RowS
a97f0 65 74 20 20 20 20 30 78 30 30 32 30 20 20 20 2f  et    0x0020   /
a9800 2a 20 56 61 6c 75 65 20 69 73 20 61 20 52 6f 77  * Value is a Row
a9810 53 65 74 20 6f 62 6a 65 63 74 20 2a 2f 0a 23 64  Set object */.#d
a9820 65 66 69 6e 65 20 4d 45 4d 5f 46 72 61 6d 65 20  efine MEM_Frame 
a9830 20 20 20 20 30 78 30 30 34 30 20 20 20 2f 2a 20      0x0040   /* 
a9840 56 61 6c 75 65 20 69 73 20 61 20 56 64 62 65 46  Value is a VdbeF
a9850 72 61 6d 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 23  rame object */.#
a9860 64 65 66 69 6e 65 20 4d 45 4d 5f 54 79 70 65 4d  define MEM_TypeM
a9870 61 73 6b 20 20 30 78 30 30 66 66 20 20 20 2f 2a  ask  0x00ff   /*
a9880 20 4d 61 73 6b 20 6f 66 20 74 79 70 65 20 62 69   Mask of type bi
a9890 74 73 20 2a 2f 0a 0a 2f 2a 20 57 68 65 6e 65 76  ts */../* Whenev
a98a0 65 72 20 4d 65 6d 20 63 6f 6e 74 61 69 6e 73 20  er Mem contains 
a98b0 61 20 76 61 6c 69 64 20 73 74 72 69 6e 67 20 6f  a valid string o
a98c0 72 20 62 6c 6f 62 20 72 65 70 72 65 73 65 6e 74  r blob represent
a98d0 61 74 69 6f 6e 2c 20 6f 6e 65 20 6f 66 0a 2a 2a  ation, one of.**
a98e0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66   the following f
a98f0 6c 61 67 73 20 6d 75 73 74 20 62 65 20 73 65 74  lags must be set
a9900 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68   to determine th
a9910 65 20 6d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 6d  e memory managem
a9920 65 6e 74 0a 2a 2a 20 70 6f 6c 69 63 79 20 66 6f  ent.** policy fo
a9930 72 20 4d 65 6d 2e 7a 2e 20 20 54 68 65 20 4d 45  r Mem.z.  The ME
a9940 4d 5f 54 65 72 6d 20 66 6c 61 67 20 74 65 6c 6c  M_Term flag tell
a9950 73 20 75 73 20 77 68 65 74 68 65 72 20 6f 72 20  s us whether or 
a9960 6e 6f 74 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e  not the.** strin
a9970 67 20 69 73 20 5c 30 30 30 20 6f 72 20 5c 75 30  g is \000 or \u0
a9980 30 30 30 20 74 65 72 6d 69 6e 61 74 65 64 0a 2a  000 terminated.*
a9990 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 54 65  /.#define MEM_Te
a99a0 72 6d 20 20 20 20 20 20 30 78 30 32 30 30 20 20  rm      0x0200  
a99b0 20 2f 2a 20 53 74 72 69 6e 67 20 72 65 70 20 69   /* String rep i
a99c0 73 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64  s nul terminated
a99d0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f   */.#define MEM_
a99e0 44 79 6e 20 20 20 20 20 20 20 30 78 30 34 30 30  Dyn       0x0400
a99f0 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 63 61     /* Need to ca
a9a00 6c 6c 20 73 71 6c 69 74 65 46 72 65 65 28 29 20  ll sqliteFree() 
a9a10 6f 6e 20 4d 65 6d 2e 7a 20 2a 2f 0a 23 64 65 66  on Mem.z */.#def
a9a20 69 6e 65 20 4d 45 4d 5f 53 74 61 74 69 63 20 20  ine MEM_Static  
a9a30 20 20 30 78 30 38 30 30 20 20 20 2f 2a 20 4d 65    0x0800   /* Me
a9a40 6d 2e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20  m.z points to a 
a9a50 73 74 61 74 69 63 20 73 74 72 69 6e 67 20 2a 2f  static string */
a9a60 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 45 70 68  .#define MEM_Eph
a9a70 65 6d 20 20 20 20 20 30 78 31 30 30 30 20 20 20  em     0x1000   
a9a80 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20  /* Mem.z points 
a9a90 74 6f 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20  to an ephemeral 
a9aa0 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e  string */.#defin
a9ab0 65 20 4d 45 4d 5f 41 67 67 20 20 20 20 20 20 20  e MEM_Agg       
a9ac0 30 78 32 30 30 30 20 20 20 2f 2a 20 4d 65 6d 2e  0x2000   /* Mem.
a9ad0 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61  z points to an a
a9ae0 67 67 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74  gg function cont
a9af0 65 78 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  ext */.#define M
a9b00 45 4d 5f 5a 65 72 6f 20 20 20 20 20 20 30 78 34  EM_Zero      0x4
a9b10 30 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 69 20 63  000   /* Mem.i c
a9b20 6f 6e 74 61 69 6e 73 20 63 6f 75 6e 74 20 6f 66  ontains count of
a9b30 20 30 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20   0s appended to 
a9b40 62 6c 6f 62 20 2a 2f 0a 0a 23 69 66 64 65 66 20  blob */..#ifdef 
a9b50 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52  SQLITE_OMIT_INCR
a9b60 42 4c 4f 42 0a 20 20 23 75 6e 64 65 66 20 4d 45  BLOB.  #undef ME
a9b70 4d 5f 5a 65 72 6f 0a 20 20 23 64 65 66 69 6e 65  M_Zero.  #define
a9b80 20 4d 45 4d 5f 5a 65 72 6f 20 30 78 30 30 30 30   MEM_Zero 0x0000
a9b90 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
a9ba0 43 6c 65 61 72 20 61 6e 79 20 65 78 69 73 74 69  Clear any existi
a9bb0 6e 67 20 74 79 70 65 20 66 6c 61 67 73 20 66 72  ng type flags fr
a9bc0 6f 6d 20 61 20 4d 65 6d 20 61 6e 64 20 72 65 70  om a Mem and rep
a9bd0 6c 61 63 65 20 74 68 65 6d 20 77 69 74 68 20 66  lace them with f
a9be0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 65 6d 53  .*/.#define MemS
a9bf0 65 74 54 79 70 65 46 6c 61 67 28 70 2c 20 66 29  etTypeFlag(p, f)
a9c00 20 5c 0a 20 20 20 28 28 70 29 2d 3e 66 6c 61 67   \.   ((p)->flag
a9c10 73 20 3d 20 28 28 70 29 2d 3e 66 6c 61 67 73 26  s = ((p)->flags&
a9c20 7e 28 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 7c 4d  ~(MEM_TypeMask|M
a9c30 45 4d 5f 5a 65 72 6f 29 29 7c 66 29 0a 0a 0a 2f  EM_Zero))|f).../
a9c40 2a 20 41 20 56 64 62 65 46 75 6e 63 20 69 73 20  * A VdbeFunc is 
a9c50 6a 75 73 74 20 61 20 46 75 6e 63 44 65 66 20 28  just a FuncDef (
a9c60 64 65 66 69 6e 65 64 20 69 6e 20 73 71 6c 69 74  defined in sqlit
a9c70 65 49 6e 74 2e 68 29 20 74 68 61 74 20 63 6f 6e  eInt.h) that con
a9c80 74 61 69 6e 73 0a 2a 2a 20 61 64 64 69 74 69 6f  tains.** additio
a9c90 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  nal information 
a9ca0 61 62 6f 75 74 20 61 75 78 69 6c 69 61 72 79 20  about auxiliary 
a9cb0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 62 6f 75 6e  information boun
a9cc0 64 20 74 6f 20 61 72 67 75 6d 65 6e 74 73 0a 2a  d to arguments.*
a9cd0 2a 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f  * of the functio
a9ce0 6e 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64  n.  This is used
a9cf0 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68   to implement th
a9d00 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75  e sqlite3_get_au
a9d10 78 64 61 74 61 28 29 0a 2a 2a 20 61 6e 64 20 73  xdata().** and s
a9d20 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61  qlite3_set_auxda
a9d30 74 61 28 29 20 41 50 49 73 2e 20 20 54 68 65 20  ta() APIs.  The 
a9d40 22 61 75 78 64 61 74 61 22 20 69 73 20 73 6f 6d  "auxdata" is som
a9d50 65 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61  e auxiliary data
a9d60 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20  .** that can be 
a9d70 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
a9d80 61 20 63 6f 6e 73 74 61 6e 74 20 61 72 67 75 6d  a constant argum
a9d90 65 6e 74 20 74 6f 20 61 20 66 75 6e 63 74 69 6f  ent to a functio
a9da0 6e 2e 20 20 54 68 69 73 0a 2a 2a 20 61 6c 6c 6f  n.  This.** allo
a9db0 77 73 20 66 75 6e 63 74 69 6f 6e 73 20 73 75 63  ws functions suc
a9dc0 68 20 61 73 20 22 72 65 67 65 78 70 22 20 74 6f  h as "regexp" to
a9dd0 20 63 6f 6d 70 69 6c 65 20 74 68 65 69 72 20 63   compile their c
a9de0 6f 6e 73 74 61 6e 74 20 72 65 67 75 6c 61 72 0a  onstant regular.
a9df0 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 61 72  ** expression ar
a9e00 67 75 6d 65 6e 74 20 6f 6e 63 65 20 61 6e 64 20  gument once and 
a9e10 72 65 75 73 65 64 20 74 68 65 20 63 6f 6d 70 69  reused the compi
a9e20 6c 65 64 20 63 6f 64 65 20 66 6f 72 20 6d 75 6c  led code for mul
a9e30 74 69 70 6c 65 0a 2a 2a 20 69 6e 76 6f 63 61 74  tiple.** invocat
a9e40 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ions..*/.struct 
a9e50 56 64 62 65 46 75 6e 63 20 7b 0a 20 20 46 75 6e  VdbeFunc {.  Fun
a9e60 63 44 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20  cDef *pFunc;    
a9e70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
a9e80 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20  e definition of 
a9e90 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  the function */.
a9ea0 20 20 69 6e 74 20 6e 41 75 78 3b 20 20 20 20 20    int nAux;     
a9eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a9ec0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74  /* Number of ent
a9ed0 72 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66  ries allocated f
a9ee0 6f 72 20 61 70 41 75 78 5b 5d 20 2a 2f 0a 20 20  or apAux[] */.  
a9ef0 73 74 72 75 63 74 20 41 75 78 44 61 74 61 20 7b  struct AuxData {
a9f00 0a 20 20 20 20 76 6f 69 64 20 2a 70 41 75 78 3b  .    void *pAux;
a9f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a9f20 20 20 20 2f 2a 20 41 75 78 20 64 61 74 61 20 66     /* Aux data f
a9f30 6f 72 20 74 68 65 20 69 2d 74 68 20 61 72 67 75  or the i-th argu
a9f40 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 76 6f 69 64  ment */.    void
a9f50 20 28 2a 78 44 65 6c 65 74 65 29 28 76 6f 69 64   (*xDelete)(void
a9f60 20 2a 29 3b 20 20 20 20 20 20 2f 2a 20 44 65 73   *);      /* Des
a9f70 74 72 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20  tructor for the 
a9f80 61 75 78 20 64 61 74 61 20 2a 2f 0a 20 20 7d 20  aux data */.  } 
a9f90 61 70 41 75 78 5b 31 5d 3b 20 20 20 20 20 20 20  apAux[1];       
a9fa0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
a9fb0 6e 65 20 73 6c 6f 74 20 66 6f 72 20 65 61 63 68  ne slot for each
a9fc0 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65   function argume
a9fd0 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  nt */.};../*.** 
a9fe0 54 68 65 20 22 63 6f 6e 74 65 78 74 22 20 61 72  The "context" ar
a9ff0 67 75 6d 65 6e 74 20 66 6f 72 20 61 20 69 6e 73  gument for a ins
aa000 74 61 6c 6c 61 62 6c 65 20 66 75 6e 63 74 69 6f  tallable functio
aa010 6e 2e 20 20 41 20 70 6f 69 6e 74 65 72 20 74 6f  n.  A pointer to
aa020 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20   an.** instance 
aa030 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
aa040 65 20 69 73 20 74 68 65 20 66 69 72 73 74 20 61  e is the first a
aa050 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 72  rgument to the r
aa060 6f 75 74 69 6e 65 73 20 75 73 65 64 0a 2a 2a 20  outines used.** 
aa070 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51  implement the SQ
aa080 4c 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a  L functions..**.
aa090 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20 74 79  ** There is a ty
aa0a0 70 65 64 65 66 20 66 6f 72 20 74 68 69 73 20 73  pedef for this s
aa0b0 74 72 75 63 74 75 72 65 20 69 6e 20 73 71 6c 69  tructure in sqli
aa0c0 74 65 2e 68 2e 20 20 53 6f 20 61 6c 6c 20 72 6f  te.h.  So all ro
aa0d0 75 74 69 6e 65 73 2c 0a 2a 2a 20 65 76 65 6e 20  utines,.** even 
aa0e0 74 68 65 20 70 75 62 6c 69 63 20 69 6e 74 65 72  the public inter
aa0f0 66 61 63 65 20 74 6f 20 53 51 4c 69 74 65 2c 20  face to SQLite, 
aa100 63 61 6e 20 75 73 65 20 61 20 70 6f 69 6e 74 65  can use a pointe
aa110 72 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74  r to this struct
aa120 75 72 65 2e 0a 2a 2a 20 42 75 74 20 74 68 69 73  ure..** But this
aa130 20 66 69 6c 65 20 69 73 20 74 68 65 20 6f 6e 6c   file is the onl
aa140 79 20 70 6c 61 63 65 20 77 68 65 72 65 20 74 68  y place where th
aa150 65 20 69 6e 74 65 72 6e 61 6c 20 64 65 74 61 69  e internal detai
aa160 6c 73 20 6f 66 20 74 68 69 73 0a 2a 2a 20 73 74  ls of this.** st
aa170 72 75 63 74 75 72 65 20 61 72 65 20 6b 6e 6f 77  ructure are know
aa180 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74  n..**.** This st
aa190 72 75 63 74 75 72 65 20 69 73 20 64 65 66 69 6e  ructure is defin
aa1a0 65 64 20 69 6e 73 69 64 65 20 6f 66 20 76 64 62  ed inside of vdb
aa1b0 65 49 6e 74 2e 68 20 62 65 63 61 75 73 65 20 69  eInt.h because i
aa1c0 74 20 75 73 65 73 20 73 75 62 73 74 72 75 63 74  t uses substruct
aa1d0 75 72 65 73 0a 2a 2a 20 28 4d 65 6d 29 20 77 68  ures.** (Mem) wh
aa1e0 69 63 68 20 61 72 65 20 6f 6e 6c 79 20 64 65 66  ich are only def
aa1f0 69 6e 65 64 20 74 68 65 72 65 2e 0a 2a 2f 0a 73  ined there..*/.s
aa200 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 63 6f  truct sqlite3_co
aa210 6e 74 65 78 74 20 7b 0a 20 20 46 75 6e 63 44 65  ntext {.  FuncDe
aa220 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20  f *pFunc;       
aa230 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 75  /* Pointer to fu
aa240 6e 63 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69  nction informati
aa250 6f 6e 2e 20 20 4d 55 53 54 20 42 45 20 46 49 52  on.  MUST BE FIR
aa260 53 54 20 2a 2f 0a 20 20 56 64 62 65 46 75 6e 63  ST */.  VdbeFunc
aa270 20 2a 70 56 64 62 65 46 75 6e 63 3b 20 20 2f 2a   *pVdbeFunc;  /*
aa280 20 41 75 78 69 6c 61 72 79 20 64 61 74 61 2c 20   Auxilary data, 
aa290 69 66 20 63 72 65 61 74 65 64 2e 20 2a 2f 0a 20  if created. */. 
aa2a0 20 4d 65 6d 20 73 3b 20 20 20 20 20 20 20 20 20   Mem s;         
aa2b0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65         /* The re
aa2c0 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 73 74  turn value is st
aa2d0 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20 20 4d  ored here */.  M
aa2e0 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20  em *pMem;       
aa2f0 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63       /* Memory c
aa300 65 6c 6c 20 75 73 65 64 20 74 6f 20 73 74 6f 72  ell used to stor
aa310 65 20 61 67 67 72 65 67 61 74 65 20 63 6f 6e 74  e aggregate cont
aa320 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 73 45  ext */.  int isE
aa330 72 72 6f 72 3b 20 20 20 20 20 20 20 20 20 20 2f  rror;          /
aa340 2a 20 45 72 72 6f 72 20 63 6f 64 65 20 72 65 74  * Error code ret
aa350 75 72 6e 65 64 20 62 79 20 74 68 65 20 66 75 6e  urned by the fun
aa360 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 43 6f 6c 6c  ction. */.  Coll
aa370 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20  Seq *pColl;     
aa380 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73    /* Collating s
aa390 65 71 75 65 6e 63 65 20 2a 2f 0a 7d 3b 0a 0a 2f  equence */.};../
aa3a0 2a 0a 2a 2a 20 41 20 53 65 74 20 73 74 72 75 63  *.** A Set struc
aa3b0 74 75 72 65 20 69 73 20 75 73 65 64 20 66 6f 72  ture is used for
aa3c0 20 71 75 69 63 6b 20 74 65 73 74 69 6e 67 20 74   quick testing t
aa3d0 6f 20 73 65 65 20 69 66 20 61 20 76 61 6c 75 65  o see if a value
aa3e0 0a 2a 2a 20 69 73 20 70 61 72 74 20 6f 66 20 61  .** is part of a
aa3f0 20 73 6d 61 6c 6c 20 73 65 74 2e 20 20 53 65 74   small set.  Set
aa400 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 69 6d  s are used to im
aa410 70 6c 65 6d 65 6e 74 20 63 6f 64 65 20 6c 69 6b  plement code lik
aa420 65 0a 2a 2a 20 74 68 69 73 3a 0a 2a 2a 20 20 20  e.** this:.**   
aa430 20 20 20 20 20 20 20 20 20 78 2e 79 20 49 4e 20           x.y IN 
aa440 28 27 68 69 27 2c 27 68 6f 6f 27 2c 27 68 75 6d  ('hi','hoo','hum
aa450 27 29 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  ').*/.typedef st
aa460 72 75 63 74 20 53 65 74 20 53 65 74 3b 0a 73 74  ruct Set Set;.st
aa470 72 75 63 74 20 53 65 74 20 7b 0a 20 20 48 61 73  ruct Set {.  Has
aa480 68 20 68 61 73 68 3b 20 20 20 20 20 20 20 20 20  h hash;         
aa490 20 20 20 20 2f 2a 20 41 20 73 65 74 20 69 73 20      /* A set is 
aa4a0 6a 75 73 74 20 61 20 68 61 73 68 20 74 61 62 6c  just a hash tabl
aa4b0 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20  e */.  HashElem 
aa4c0 2a 70 72 65 76 3b 20 20 20 20 20 20 20 20 2f 2a  *prev;        /*
aa4d0 20 50 72 65 76 69 6f 75 73 6c 79 20 61 63 63 65   Previously acce
aa4e0 73 73 65 64 20 68 61 73 68 20 65 6c 65 6d 65 6e  ssed hash elemen
aa4f0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e   */.};../*.** An
aa500 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
aa510 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
aa520 2e 20 20 54 68 69 73 20 73 74 72 75 63 74 75 72  .  This structur
aa530 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63  e contains the c
aa540 6f 6d 70 6c 65 74 65 0a 2a 2a 20 73 74 61 74 65  omplete.** state
aa550 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20   of the virtual 
aa560 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54  machine..**.** T
aa570 68 65 20 22 73 71 6c 69 74 65 33 5f 73 74 6d 74  he "sqlite3_stmt
aa580 22 20 73 74 72 75 63 74 75 72 65 20 70 6f 69 6e  " structure poin
aa590 74 65 72 20 74 68 61 74 20 69 73 20 72 65 74 75  ter that is retu
aa5a0 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  rned by sqlite3_
aa5b0 63 6f 6d 70 69 6c 65 28 29 0a 2a 2a 20 69 73 20  compile().** is 
aa5c0 72 65 61 6c 6c 79 20 61 20 70 6f 69 6e 74 65 72  really a pointer
aa5d0 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   to an instance 
aa5e0 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
aa5f0 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 64 62  e..**.** The Vdb
aa600 65 2e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 76  e.inVtabMethod v
aa610 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 74  ariable is set t
aa620 6f 20 6e 6f 6e 2d 7a 65 72 6f 20 66 6f 72 20 74  o non-zero for t
aa630 68 65 20 64 75 72 61 74 69 6f 6e 20 6f 66 0a 2a  he duration of.*
aa640 2a 20 61 6e 79 20 76 69 72 74 75 61 6c 20 74 61  * any virtual ta
aa650 62 6c 65 20 6d 65 74 68 6f 64 20 69 6e 76 6f 63  ble method invoc
aa660 61 74 69 6f 6e 73 20 6d 61 64 65 20 62 79 20 74  ations made by t
aa670 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2e  he vdbe program.
aa680 20 49 74 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f   It is.** set to
aa690 20 32 20 66 6f 72 20 78 44 65 73 74 72 6f 79 20   2 for xDestroy 
aa6a0 6d 65 74 68 6f 64 20 63 61 6c 6c 73 20 61 6e 64  method calls and
aa6b0 20 31 20 66 6f 72 20 61 6c 6c 20 6f 74 68 65 72   1 for all other
aa6c0 20 6d 65 74 68 6f 64 73 2e 20 54 68 69 73 0a 2a   methods. This.*
aa6d0 2a 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 73  * variable is us
aa6e0 65 64 20 66 6f 72 20 74 77 6f 20 70 75 72 70 6f  ed for two purpo
aa6f0 73 65 73 3a 20 74 6f 20 61 6c 6c 6f 77 20 78 44  ses: to allow xD
aa700 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 73 20 74  estroy methods t
aa710 6f 20 65 78 65 63 75 74 65 0a 2a 2a 20 22 44 52  o execute.** "DR
aa720 4f 50 20 54 41 42 4c 45 22 20 73 74 61 74 65 6d  OP TABLE" statem
aa730 65 6e 74 73 20 61 6e 64 20 74 6f 20 70 72 65 76  ents and to prev
aa740 65 6e 74 20 73 6f 6d 65 20 6e 61 73 74 79 20 73  ent some nasty s
aa750 69 64 65 20 65 66 66 65 63 74 73 20 6f 66 0a 2a  ide effects of.*
aa760 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65  * malloc failure
aa770 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20   when SQLite is 
aa780 69 6e 76 6f 6b 65 64 20 72 65 63 75 72 73 69 76  invoked recursiv
aa790 65 6c 79 20 62 79 20 61 20 76 69 72 74 75 61 6c  ely by a virtual
aa7a0 20 74 61 62 6c 65 20 0a 2a 2a 20 6d 65 74 68 6f   table .** metho
aa7b0 64 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73  d function..*/.s
aa7c0 74 72 75 63 74 20 56 64 62 65 20 7b 0a 20 20 73  truct Vdbe {.  s
aa7d0 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20  qlite3 *db;     
aa7e0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
aa7f0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
aa800 6e 20 74 68 61 74 20 6f 77 6e 73 20 74 68 69 73  n that owns this
aa810 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
aa820 56 64 62 65 20 2a 70 50 72 65 76 2c 2a 70 4e 65  Vdbe *pPrev,*pNe
aa830 78 74 3b 20 20 20 20 20 2f 2a 20 4c 69 6e 6b 65  xt;     /* Linke
aa840 64 20 6c 69 73 74 20 6f 66 20 56 44 42 45 73 20  d list of VDBEs 
aa850 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 56 64  with the same Vd
aa860 62 65 2e 64 62 20 2a 2f 0a 20 20 69 6e 74 20 6e  be.db */.  int n
aa870 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Op;             
aa880 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
aa890 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 69 6e 20  instructions in 
aa8a0 74 68 65 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20  the program */. 
aa8b0 20 69 6e 74 20 6e 4f 70 41 6c 6c 6f 63 3b 20 20   int nOpAlloc;  
aa8c0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
aa8d0 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f  er of slots allo
aa8e0 63 61 74 65 64 20 66 6f 72 20 61 4f 70 5b 5d 20  cated for aOp[] 
aa8f0 2a 2f 0a 20 20 4f 70 20 2a 61 4f 70 3b 20 20 20  */.  Op *aOp;   
aa900 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
aa910 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68  Space to hold th
aa920 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e  e virtual machin
aa930 65 27 73 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20  e's program */. 
aa940 20 69 6e 74 20 6e 4c 61 62 65 6c 3b 20 20 20 20   int nLabel;    
aa950 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
aa960 65 72 20 6f 66 20 6c 61 62 65 6c 73 20 75 73 65  er of labels use
aa970 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 61 62 65  d */.  int nLabe
aa980 6c 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 2f  lAlloc;        /
aa990 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74  * Number of slot
aa9a0 73 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 61  s allocated in a
aa9b0 4c 61 62 65 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74  Label[] */.  int
aa9c0 20 2a 61 4c 61 62 65 6c 3b 20 20 20 20 20 20 20   *aLabel;       
aa9d0 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 74 6f       /* Space to
aa9e0 20 68 6f 6c 64 20 74 68 65 20 6c 61 62 65 6c 73   hold the labels
aa9f0 20 2a 2f 0a 20 20 4d 65 6d 20 2a 2a 61 70 41 72   */.  Mem **apAr
aaa00 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  g;            /*
aaa10 20 41 72 67 75 6d 65 6e 74 73 20 74 6f 20 63 75   Arguments to cu
aaa20 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74 69 6e  rrently executin
aaa30 67 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20  g user function 
aaa40 2a 2f 0a 20 20 4d 65 6d 20 2a 61 43 6f 6c 4e 61  */.  Mem *aColNa
aaa50 6d 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  me;          /* 
aaa60 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 74 6f 20  Column names to 
aaa70 72 65 74 75 72 6e 20 2a 2f 0a 20 20 4d 65 6d 20  return */.  Mem 
aaa80 2a 70 52 65 73 75 6c 74 53 65 74 3b 20 20 20 20  *pResultSet;    
aaa90 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
aaaa0 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20 72 65  o an array of re
aaab0 73 75 6c 74 73 20 2a 2f 0a 20 20 75 31 36 20 6e  sults */.  u16 n
aaac0 52 65 73 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20  ResColumn;      
aaad0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
aaae0 63 6f 6c 75 6d 6e 73 20 69 6e 20 6f 6e 65 20 72  columns in one r
aaaf0 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  ow of the result
aab00 20 73 65 74 20 2a 2f 0a 20 20 75 31 36 20 6e 43   set */.  u16 nC
aab10 75 72 73 6f 72 3b 20 20 20 20 20 20 20 20 20 20  ursor;          
aab20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73    /* Number of s
aab30 6c 6f 74 73 20 69 6e 20 61 70 43 73 72 5b 5d 20  lots in apCsr[] 
aab40 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20  */.  VdbeCursor 
aab50 2a 2a 61 70 43 73 72 3b 20 20 20 20 20 2f 2a 20  **apCsr;     /* 
aab60 4f 6e 65 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74  One element of t
aab70 68 69 73 20 61 72 72 61 79 20 66 6f 72 20 65 61  his array for ea
aab80 63 68 20 6f 70 65 6e 20 63 75 72 73 6f 72 20 2a  ch open cursor *
aab90 2f 0a 20 20 75 38 20 65 72 72 6f 72 41 63 74 69  /.  u8 errorActi
aaba0 6f 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52  on;         /* R
aabb0 65 63 6f 76 65 72 79 20 61 63 74 69 6f 6e 20 74  ecovery action t
aabc0 6f 20 64 6f 20 69 6e 20 63 61 73 65 20 6f 66 20  o do in case of 
aabd0 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 75 38  an error */.  u8
aabe0 20 6f 6b 56 61 72 3b 20 20 20 20 20 20 20 20 20   okVar;         
aabf0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
aac00 20 61 7a 56 61 72 5b 5d 20 68 61 73 20 62 65 65   azVar[] has bee
aac10 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f  n initialized */
aac20 0a 20 20 75 31 36 20 6e 56 61 72 3b 20 20 20 20  .  u16 nVar;    
aac30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
aac40 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
aac50 69 6e 20 61 56 61 72 5b 5d 20 2a 2f 0a 20 20 4d  in aVar[] */.  M
aac60 65 6d 20 2a 61 56 61 72 3b 20 20 20 20 20 20 20  em *aVar;       
aac70 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 73         /* Values
aac80 20 66 6f 72 20 74 68 65 20 4f 50 5f 56 61 72 69   for the OP_Vari
aac90 61 62 6c 65 20 6f 70 63 6f 64 65 2e 20 2a 2f 0a  able opcode. */.
aaca0 20 20 63 68 61 72 20 2a 2a 61 7a 56 61 72 3b 20    char **azVar; 
aacb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
aacc0 65 20 6f 66 20 76 61 72 69 61 62 6c 65 73 20 2a  e of variables *
aacd0 2f 0a 20 20 75 33 32 20 6d 61 67 69 63 3b 20 20  /.  u32 magic;  
aace0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
aacf0 61 67 69 63 20 6e 75 6d 62 65 72 20 66 6f 72 20  agic number for 
aad00 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20  sanity checking 
aad10 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20  */.  int nMem;  
aad20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
aad30 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79  Number of memory
aad40 20 6c 6f 63 61 74 69 6f 6e 73 20 63 75 72 72 65   locations curre
aad50 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 2a  ntly allocated *
aad60 2f 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20 20  /.  Mem *aMem;  
aad70 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
aad80 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69  he memory locati
aad90 6f 6e 73 20 2a 2f 0a 20 20 75 33 32 20 63 61 63  ons */.  u32 cac
aada0 68 65 43 74 72 3b 20 20 20 20 20 20 20 20 20 20  heCtr;          
aadb0 20 2f 2a 20 56 64 62 65 43 75 72 73 6f 72 20 72   /* VdbeCursor r
aadc0 6f 77 20 63 61 63 68 65 20 67 65 6e 65 72 61 74  ow cache generat
aadd0 69 6f 6e 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  ion counter */. 
aade0 20 69 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20   int pc;        
aadf0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
aae00 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 20  program counter 
aae10 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  */.  int rc;    
aae20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
aae30 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20  Value to return 
aae40 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d  */.  char *zErrM
aae50 73 67 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  sg;          /* 
aae60 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 72  Error message wr
aae70 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20  itten here */.  
aae80 75 38 20 65 78 70 6c 61 69 6e 3b 20 20 20 20 20  u8 explain;     
aae90 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
aaea0 69 66 20 45 58 50 4c 41 49 4e 20 70 72 65 73 65  if EXPLAIN prese
aaeb0 6e 74 20 6f 6e 20 53 51 4c 20 63 6f 6d 6d 61 6e  nt on SQL comman
aaec0 64 20 2a 2f 0a 20 20 75 38 20 63 68 61 6e 67 65  d */.  u8 change
aaed0 43 6e 74 4f 6e 3b 20 20 20 20 20 20 20 20 20 2f  CntOn;         /
aaee0 2a 20 54 72 75 65 20 74 6f 20 75 70 64 61 74 65  * True to update
aaef0 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e   the change-coun
aaf00 74 65 72 20 2a 2f 0a 20 20 75 38 20 65 78 70 69  ter */.  u8 expi
aaf10 72 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  red;            
aaf20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20   /* True if the 
aaf30 56 4d 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72  VM needs to be r
aaf40 65 63 6f 6d 70 69 6c 65 64 20 2a 2f 0a 20 20 75  ecompiled */.  u
aaf50 38 20 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f  8 minWriteFileFo
aaf60 72 6d 61 74 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75  rmat;  /* Minimu
aaf70 6d 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 66 6f  m file format fo
aaf80 72 20 77 72 69 74 61 62 6c 65 20 64 61 74 61 62  r writable datab
aaf90 61 73 65 20 66 69 6c 65 73 20 2a 2f 0a 20 20 75  ase files */.  u
aafa0 38 20 69 6e 56 74 61 62 4d 65 74 68 6f 64 3b 20  8 inVtabMethod; 
aafb0 20 20 20 20 20 20 20 2f 2a 20 53 65 65 20 63 6f         /* See co
aafc0 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20 2a 2f 0a  mments above */.
aafd0 20 20 75 38 20 75 73 65 73 53 74 6d 74 4a 6f 75    u8 usesStmtJou
aafe0 72 6e 61 6c 3b 20 20 20 20 20 2f 2a 20 54 72 75  rnal;     /* Tru
aaff0 65 20 69 66 20 75 73 65 73 20 61 20 73 74 61 74  e if uses a stat
ab000 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 2a 2f  ement journal */
ab010 0a 20 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20  .  u8 readOnly; 
ab020 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
ab030 75 65 20 66 6f 72 20 72 65 61 64 2d 6f 6e 6c 79  ue for read-only
ab040 20 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 20   statements */. 
ab050 20 75 38 20 69 73 50 72 65 70 61 72 65 56 32 3b   u8 isPrepareV2;
ab060 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
ab070 20 69 66 20 70 72 65 70 61 72 65 64 20 77 69 74   if prepared wit
ab080 68 20 70 72 65 70 61 72 65 5f 76 32 28 29 20 2a  h prepare_v2() *
ab090 2f 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67 65 3b  /.  int nChange;
ab0a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
ab0b0 75 6d 62 65 72 20 6f 66 20 64 62 20 63 68 61 6e  umber of db chan
ab0c0 67 65 73 20 6d 61 64 65 20 73 69 6e 63 65 20 6c  ges made since l
ab0d0 61 73 74 20 72 65 73 65 74 20 2a 2f 0a 20 20 69  ast reset */.  i
ab0e0 6e 74 20 62 74 72 65 65 4d 61 73 6b 3b 20 20 20  nt btreeMask;   
ab0f0 20 20 20 20 20 20 20 2f 2a 20 42 69 74 6d 61 73         /* Bitmas
ab100 6b 20 6f 66 20 64 62 2d 3e 61 44 62 5b 5d 20 65  k of db->aDb[] e
ab110 6e 74 72 69 65 73 20 72 65 66 65 72 65 6e 63 65  ntries reference
ab120 64 20 2a 2f 0a 20 20 69 36 34 20 73 74 61 72 74  d */.  i64 start
ab130 54 69 6d 65 3b 20 20 20 20 20 20 20 20 20 20 2f  Time;          /
ab140 2a 20 54 69 6d 65 20 77 68 65 6e 20 71 75 65 72  * Time when quer
ab150 79 20 73 74 61 72 74 65 64 20 2d 20 75 73 65 64  y started - used
ab160 20 66 6f 72 20 70 72 6f 66 69 6c 69 6e 67 20 2a   for profiling *
ab170 2f 0a 20 20 42 74 72 65 65 4d 75 74 65 78 41 72  /.  BtreeMutexAr
ab180 72 61 79 20 61 4d 75 74 65 78 3b 20 2f 2a 20 41  ray aMutex; /* A
ab190 6e 20 61 72 72 61 79 20 6f 66 20 42 74 72 65 65  n array of Btree
ab1a0 20 75 73 65 64 20 68 65 72 65 20 61 6e 64 20 6e   used here and n
ab1b0 65 65 64 69 6e 67 20 6c 6f 63 6b 73 20 2a 2f 0a  eeding locks */.
ab1c0 20 20 69 6e 74 20 61 43 6f 75 6e 74 65 72 5b 32    int aCounter[2
ab1d0 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75  ];        /* Cou
ab1e0 6e 74 65 72 73 20 75 73 65 64 20 62 79 20 73 71  nters used by sq
ab1f0 6c 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75  lite3_stmt_statu
ab200 73 28 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  s() */.  char *z
ab210 53 71 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  Sql;            
ab220 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20   /* Text of the 
ab230 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68  SQL statement th
ab240 61 74 20 67 65 6e 65 72 61 74 65 64 20 74 68 69  at generated thi
ab250 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 46 72  s */.  void *pFr
ab260 65 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ee;            /
ab270 2a 20 46 72 65 65 20 74 68 69 73 20 77 68 65 6e  * Free this when
ab280 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 76 64   deleting the vd
ab290 62 65 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74 61  be */.  int iSta
ab2a0 74 65 6d 65 6e 74 3b 20 20 20 20 20 20 20 20 20  tement;         
ab2b0 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 6e 75 6d  /* Statement num
ab2c0 62 65 72 20 28 6f 72 20 30 20 69 66 20 68 61 73  ber (or 0 if has
ab2d0 20 6e 6f 74 20 6f 70 65 6e 65 64 20 73 74 6d 74   not opened stmt
ab2e0 29 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ) */.#ifdef SQLI
ab2f0 54 45 5f 44 45 42 55 47 0a 20 20 46 49 4c 45 20  TE_DEBUG.  FILE 
ab300 2a 74 72 61 63 65 3b 20 20 20 20 20 20 20 20 20  *trace;         
ab310 20 20 20 2f 2a 20 57 72 69 74 65 20 61 6e 20 65     /* Write an e
ab320 78 65 63 75 74 69 6f 6e 20 74 72 61 63 65 20 68  xecution trace h
ab330 65 72 65 2c 20 69 66 20 6e 6f 74 20 4e 55 4c 4c  ere, if not NULL
ab340 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 56 64 62   */.#endif.  Vdb
ab350 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 20  eFrame *pFrame; 
ab360 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 66       /* Parent f
ab370 72 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 46  rame */.  int nF
ab380 72 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20  rame;           
ab390 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66    /* Number of f
ab3a0 72 61 6d 65 73 20 69 6e 20 70 46 72 61 6d 65 20  rames in pFrame 
ab3b0 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  list */.};../*.*
ab3c0 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
ab3d0 61 72 65 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75  are allowed valu
ab3e0 65 73 20 66 6f 72 20 56 64 62 65 2e 6d 61 67 69  es for Vdbe.magi
ab3f0 63 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42  c.*/.#define VDB
ab400 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 20 20 20  E_MAGIC_INIT    
ab410 20 30 78 32 36 62 63 65 61 61 35 20 20 20 20 2f   0x26bceaa5    /
ab420 2a 20 42 75 69 6c 64 69 6e 67 20 61 20 56 44 42  * Building a VDB
ab430 45 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 23 64 65  E program */.#de
ab440 66 69 6e 65 20 56 44 42 45 5f 4d 41 47 49 43 5f  fine VDBE_MAGIC_
ab450 52 55 4e 20 20 20 20 20 20 30 78 62 64 66 32 30  RUN      0xbdf20
ab460 64 61 33 20 20 20 20 2f 2a 20 56 44 42 45 20 69  da3    /* VDBE i
ab470 73 20 72 65 61 64 79 20 74 6f 20 65 78 65 63 75  s ready to execu
ab480 74 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44  te */.#define VD
ab490 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 20 20  BE_MAGIC_HALT   
ab4a0 20 20 30 78 35 31 39 63 32 39 37 33 20 20 20 20    0x519c2973    
ab4b0 2f 2a 20 56 44 42 45 20 68 61 73 20 63 6f 6d 70  /* VDBE has comp
ab4c0 6c 65 74 65 64 20 65 78 65 63 75 74 69 6f 6e 20  leted execution 
ab4d0 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f  */.#define VDBE_
ab4e0 4d 41 47 49 43 5f 44 45 41 44 20 20 20 20 20 30  MAGIC_DEAD     0
ab4f0 78 62 36 30 36 63 33 63 38 20 20 20 20 2f 2a 20  xb606c3c8    /* 
ab500 54 68 65 20 56 44 42 45 20 68 61 73 20 62 65 65  The VDBE has bee
ab510 6e 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 2a 2f  n deallocated */
ab520 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e  ../*.** Function
ab530 20 70 72 6f 74 6f 74 79 70 65 73 0a 2a 2f 0a 53   prototypes.*/.S
ab540 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
ab550 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 46 72  id sqlite3VdbeFr
ab560 65 65 43 75 72 73 6f 72 28 56 64 62 65 20 2a 2c  eeCursor(Vdbe *,
ab570 20 56 64 62 65 43 75 72 73 6f 72 2a 29 3b 0a 76   VdbeCursor*);.v
ab580 6f 69 64 20 73 71 6c 69 74 65 56 64 62 65 50 6f  oid sqliteVdbePo
ab590 70 53 74 61 63 6b 28 56 64 62 65 2a 2c 69 6e 74  pStack(Vdbe*,int
ab5a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
ab5b0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
ab5c0 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 56 64  eCursorMoveto(Vd
ab5d0 62 65 43 75 72 73 6f 72 2a 29 3b 0a 23 69 66 20  beCursor*);.#if 
ab5e0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
ab5f0 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64  EBUG) || defined
ab600 28 56 44 42 45 5f 50 52 4f 46 49 4c 45 29 0a 53  (VDBE_PROFILE).S
ab610 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
ab620 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 50 72  id sqlite3VdbePr
ab630 69 6e 74 4f 70 28 46 49 4c 45 2a 2c 20 69 6e 74  intOp(FILE*, int
ab640 2c 20 4f 70 2a 29 3b 0a 23 65 6e 64 69 66 0a 53  , Op*);.#endif.S
ab650 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33  QLITE_PRIVATE u3
ab660 32 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  2 sqlite3VdbeSer
ab670 69 61 6c 54 79 70 65 4c 65 6e 28 75 33 32 29 3b  ialTypeLen(u32);
ab680 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
ab690 75 33 32 20 73 71 6c 69 74 65 33 56 64 62 65 53  u32 sqlite3VdbeS
ab6a0 65 72 69 61 6c 54 79 70 65 28 4d 65 6d 2a 2c 20  erialType(Mem*, 
ab6b0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
ab6c0 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33  VATE u32 sqlite3
ab6d0 56 64 62 65 53 65 72 69 61 6c 50 75 74 28 75 6e  VdbeSerialPut(un
ab6e0 73 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 69 6e  signed char*, in
ab6f0 74 2c 20 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53  t, Mem*, int);.S
ab700 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33  QLITE_PRIVATE u3
ab710 32 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  2 sqlite3VdbeSer
ab720 69 61 6c 47 65 74 28 63 6f 6e 73 74 20 75 6e 73  ialGet(const uns
ab730 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 75 33 32  igned char*, u32
ab740 2c 20 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f  , Mem*);.SQLITE_
ab750 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
ab760 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 41 75  ite3VdbeDeleteAu
ab770 78 44 61 74 61 28 56 64 62 65 46 75 6e 63 2a 2c  xData(VdbeFunc*,
ab780 20 69 6e 74 29 3b 0a 0a 69 6e 74 20 73 71 6c 69   int);..int sqli
ab790 74 65 32 42 74 72 65 65 4b 65 79 43 6f 6d 70 61  te2BtreeKeyCompa
ab7a0 72 65 28 42 74 43 75 72 73 6f 72 20 2a 2c 20 63  re(BtCursor *, c
ab7b0 6f 6e 73 74 20 76 6f 69 64 20 2a 2c 20 69 6e 74  onst void *, int
ab7c0 2c 20 69 6e 74 2c 20 69 6e 74 20 2a 29 3b 0a 53  , int, int *);.S
ab7d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
ab7e0 74 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 78  t sqlite3VdbeIdx
ab7f0 4b 65 79 43 6f 6d 70 61 72 65 28 56 64 62 65 43  KeyCompare(VdbeC
ab800 75 72 73 6f 72 2a 2c 55 6e 70 61 63 6b 65 64 52  ursor*,UnpackedR
ab810 65 63 6f 72 64 2a 2c 69 6e 74 2a 29 3b 0a 53 51  ecord*,int*);.SQ
ab820 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
ab830 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 52   sqlite3VdbeIdxR
ab840 6f 77 69 64 28 73 71 6c 69 74 65 33 2a 2c 20 42  owid(sqlite3*, B
ab850 74 43 75 72 73 6f 72 20 2a 2c 20 69 36 34 20 2a  tCursor *, i64 *
ab860 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
ab870 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d  E int sqlite3Mem
ab880 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20 4d 65  Compare(const Me
ab890 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20  m*, const Mem*, 
ab8a0 63 6f 6e 73 74 20 43 6f 6c 6c 53 65 71 2a 29 3b  const CollSeq*);
ab8b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
ab8c0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 45  int sqlite3VdbeE
ab8d0 78 65 63 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49  xec(Vdbe*);.SQLI
ab8e0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
ab8f0 71 6c 69 74 65 33 56 64 62 65 4c 69 73 74 28 56  qlite3VdbeList(V
ab900 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  dbe*);.SQLITE_PR
ab910 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
ab920 33 56 64 62 65 48 61 6c 74 28 56 64 62 65 2a 29  3VdbeHalt(Vdbe*)
ab930 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
ab940 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
ab950 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 4d  ChangeEncoding(M
ab960 65 6d 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  em *, int);.SQLI
ab970 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
ab980 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f  qlite3VdbeMemToo
ab990 42 69 67 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54  Big(Mem*);.SQLIT
ab9a0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
ab9b0 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 79  lite3VdbeMemCopy
ab9c0 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d  (Mem*, const Mem
ab9d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
ab9e0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
ab9f0 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70  dbeMemShallowCop
aba00 79 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65  y(Mem*, const Me
aba10 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  m*, int);.SQLITE
aba20 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
aba30 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65  lite3VdbeMemMove
aba40 28 4d 65 6d 2a 2c 20 4d 65 6d 2a 29 3b 0a 53 51  (Mem*, Mem*);.SQ
aba50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
aba60 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e   sqlite3VdbeMemN
aba70 75 6c 54 65 72 6d 69 6e 61 74 65 28 4d 65 6d 2a  ulTerminate(Mem*
aba80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
aba90 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
abaa0 65 4d 65 6d 53 65 74 53 74 72 28 4d 65 6d 2a 2c  eMemSetStr(Mem*,
abab0 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e   const char*, in
abac0 74 2c 20 75 38 2c 20 76 6f 69 64 28 2a 29 28 76  t, u8, void(*)(v
abad0 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50  oid*));.SQLITE_P
abae0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
abaf0 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74  te3VdbeMemSetInt
abb00 36 34 28 4d 65 6d 2a 2c 20 69 36 34 29 3b 0a 53  64(Mem*, i64);.S
abb10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
abb20 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  id sqlite3VdbeMe
abb30 6d 53 65 74 44 6f 75 62 6c 65 28 4d 65 6d 2a 2c  mSetDouble(Mem*,
abb40 20 64 6f 75 62 6c 65 29 3b 0a 53 51 4c 49 54 45   double);.SQLITE
abb50 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
abb60 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e  lite3VdbeMemSetN
abb70 75 6c 6c 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54  ull(Mem*);.SQLIT
abb80 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
abb90 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
abba0 5a 65 72 6f 42 6c 6f 62 28 4d 65 6d 2a 2c 69 6e  ZeroBlob(Mem*,in
abbb0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
abbc0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
abbd0 64 62 65 4d 65 6d 53 65 74 52 6f 77 53 65 74 28  dbeMemSetRowSet(
abbe0 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Mem*);.SQLITE_PR
abbf0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
abc00 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74  3VdbeMemMakeWrit
abc10 65 61 62 6c 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c  eable(Mem*);.SQL
abc20 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
abc30 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74  sqlite3VdbeMemSt
abc40 72 69 6e 67 69 66 79 28 4d 65 6d 2a 2c 20 69 6e  ringify(Mem*, in
abc50 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
abc60 54 45 20 69 36 34 20 73 71 6c 69 74 65 33 56 64  TE i64 sqlite3Vd
abc70 62 65 49 6e 74 56 61 6c 75 65 28 4d 65 6d 2a 29  beIntValue(Mem*)
abc80 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
abc90 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
abca0 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 4d 65  MemIntegerify(Me
abcb0 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  m*);.SQLITE_PRIV
abcc0 41 54 45 20 64 6f 75 62 6c 65 20 73 71 6c 69 74  ATE double sqlit
abcd0 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28  e3VdbeRealValue(
abce0 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Mem*);.SQLITE_PR
abcf0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
abd00 65 33 56 64 62 65 49 6e 74 65 67 65 72 41 66 66  e3VdbeIntegerAff
abd10 69 6e 69 74 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c  inity(Mem*);.SQL
abd20 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
abd30 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
abd40 61 6c 69 66 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c  alify(Mem*);.SQL
abd50 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
abd60 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75  sqlite3VdbeMemNu
abd70 6d 65 72 69 66 79 28 4d 65 6d 2a 29 3b 0a 53 51  merify(Mem*);.SQ
abd80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
abd90 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46   sqlite3VdbeMemF
abda0 72 6f 6d 42 74 72 65 65 28 42 74 43 75 72 73 6f  romBtree(BtCurso
abdb0 72 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 4d  r*,int,int,int,M
abdc0 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  em*);.SQLITE_PRI
abdd0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
abde0 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
abdf0 4d 65 6d 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f  Mem *p);.SQLITE_
abe00 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
abe10 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
abe20 73 65 45 78 74 65 72 6e 61 6c 28 4d 65 6d 20 2a  seExternal(Mem *
abe30 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  p);.SQLITE_PRIVA
abe40 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
abe50 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 4d 65  beMemFinalize(Me
abe60 6d 2a 2c 20 46 75 6e 63 44 65 66 2a 29 3b 0a 53  m*, FuncDef*);.S
abe70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
abe80 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
abe90 33 4f 70 63 6f 64 65 4e 61 6d 65 28 69 6e 74 29  3OpcodeName(int)
abea0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
abeb0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
abec0 4f 70 63 6f 64 65 48 61 73 50 72 6f 70 65 72 74  OpcodeHasPropert
abed0 79 28 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c  y(int, int);.SQL
abee0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
abef0 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72  sqlite3VdbeMemGr
abf00 6f 77 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e  ow(Mem *pMem, in
abf10 74 20 6e 2c 20 69 6e 74 20 70 72 65 73 65 72 76  t n, int preserv
abf20 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  e);.SQLITE_PRIVA
abf30 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
abf40 62 65 43 6c 6f 73 65 53 74 61 74 65 6d 65 6e 74  beCloseStatement
abf50 28 56 64 62 65 20 2a 2c 20 69 6e 74 29 3b 0a 53  (Vdbe *, int);.S
abf60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
abf70 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 46 72  id sqlite3VdbeFr
abf80 61 6d 65 44 65 6c 65 74 65 28 56 64 62 65 46 72  ameDelete(VdbeFr
abf90 61 6d 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ame*);.SQLITE_PR
abfa0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
abfb0 33 56 64 62 65 46 72 61 6d 65 52 65 73 74 6f 72  3VdbeFrameRestor
abfc0 65 28 56 64 62 65 46 72 61 6d 65 20 2a 29 3b 0a  e(VdbeFrame *);.
abfd0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
abfe0 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41  ABLE_MEMORY_MANA
abff0 47 45 4d 45 4e 54 0a 53 51 4c 49 54 45 5f 50 52  GEMENT.SQLITE_PR
ac000 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
ac010 33 56 64 62 65 52 65 6c 65 61 73 65 42 75 66 66  3VdbeReleaseBuff
ac020 65 72 73 28 56 64 62 65 20 2a 70 29 3b 0a 23 65  ers(Vdbe *p);.#e
ac030 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
ac040 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
ac050 5f 43 41 43 48 45 0a 53 51 4c 49 54 45 5f 50 52  _CACHE.SQLITE_PR
ac060 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
ac070 65 33 56 64 62 65 4d 75 74 65 78 41 72 72 61 79  e3VdbeMutexArray
ac080 45 6e 74 65 72 28 56 64 62 65 20 2a 70 29 3b 0a  Enter(Vdbe *p);.
ac090 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73  #else.# define s
ac0a0 71 6c 69 74 65 33 56 64 62 65 4d 75 74 65 78 41  qlite3VdbeMutexA
ac0b0 72 72 61 79 45 6e 74 65 72 28 70 29 0a 23 65 6e  rrayEnter(p).#en
ac0c0 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  dif..SQLITE_PRIV
ac0d0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
ac0e0 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28  dbeMemTranslate(
ac0f0 4d 65 6d 2a 2c 20 75 38 29 3b 0a 23 69 66 64 65  Mem*, u8);.#ifde
ac100 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53  f SQLITE_DEBUG.S
ac110 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
ac120 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
ac130 50 72 69 6e 74 53 71 6c 28 56 64 62 65 2a 29 3b  PrintSql(Vdbe*);
ac140 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
ac150 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64    void sqlite3Vd
ac160 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74  beMemPrettyPrint
ac170 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 63 68 61 72  (Mem *pMem, char
ac180 20 2a 7a 42 75 66 29 3b 0a 23 65 6e 64 69 66 0a   *zBuf);.#endif.
ac190 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
ac1a0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
ac1b0 6d 48 61 6e 64 6c 65 42 6f 6d 28 4d 65 6d 20 2a  mHandleBom(Mem *
ac1c0 70 4d 65 6d 29 3b 0a 0a 23 69 66 6e 64 65 66 20  pMem);..#ifndef 
ac1d0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52  SQLITE_OMIT_INCR
ac1e0 42 4c 4f 42 0a 53 51 4c 49 54 45 5f 50 52 49 56  BLOB.SQLITE_PRIV
ac1f0 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65  ATE   int sqlite
ac200 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c  3VdbeMemExpandBl
ac210 6f 62 28 4d 65 6d 20 2a 29 3b 0a 23 65 6c 73 65  ob(Mem *);.#else
ac220 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74  .  #define sqlit
ac230 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42  e3VdbeMemExpandB
ac240 6c 6f 62 28 78 29 20 53 51 4c 49 54 45 5f 4f 4b  lob(x) SQLITE_OK
ac250 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20  .#endif..#endif 
ac260 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 56 44 42  /* !defined(_VDB
ac270 45 49 4e 54 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a  EINT_H_) */../**
ac280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
ac290 20 6f 66 20 76 64 62 65 49 6e 74 2e 68 20 2a 2a   of vdbeInt.h **
ac2a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ac2b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ac2c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
ac2d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
ac2e0 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
ac2f0 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 75 74 66   left off in utf
ac300 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
ac310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 69  ***********/..#i
ac320 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 4d 41  fndef SQLITE_AMA
ac330 4c 47 41 4d 41 54 49 4f 4e 0a 2f 2a 0a 2a 2a 20  LGAMATION./*.** 
ac340 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f  The following co
ac350 6e 73 74 61 6e 74 20 76 61 6c 75 65 20 69 73 20  nstant value is 
ac360 75 73 65 64 20 62 79 20 74 68 65 20 53 51 4c 49  used by the SQLI
ac370 54 45 5f 42 49 47 45 4e 44 49 41 4e 20 61 6e 64  TE_BIGENDIAN and
ac380 0a 2a 2a 20 53 51 4c 49 54 45 5f 4c 49 54 54 4c  .** SQLITE_LITTL
ac390 45 45 4e 44 49 41 4e 20 6d 61 63 72 6f 73 2e 0a  EENDIAN macros..
ac3a0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
ac3b0 45 20 63 6f 6e 73 74 20 69 6e 74 20 73 71 6c 69  E const int sqli
ac3c0 74 65 33 6f 6e 65 20 3d 20 31 3b 0a 23 65 6e 64  te3one = 1;.#end
ac3d0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 41 4d 41  if /* SQLITE_AMA
ac3e0 4c 47 41 4d 41 54 49 4f 4e 20 2a 2f 0a 0a 2f 2a  LGAMATION */../*
ac3f0 0a 2a 2a 20 54 68 69 73 20 6c 6f 6f 6b 75 70 20  .** This lookup 
ac400 74 61 62 6c 65 20 69 73 20 75 73 65 64 20 74 6f  table is used to
ac410 20 68 65 6c 70 20 64 65 63 6f 64 65 20 74 68 65   help decode the
ac420 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 0a 2a   first byte of.*
ac430 2a 20 61 20 6d 75 6c 74 69 2d 62 79 74 65 20 55  * a multi-byte U
ac440 54 46 38 20 63 68 61 72 61 63 74 65 72 2e 0a 2a  TF8 character..*
ac450 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75  /.static const u
ac460 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 71 6c  nsigned char sql
ac470 69 74 65 33 55 74 66 38 54 72 61 6e 73 31 5b 5d  ite3Utf8Trans1[]
ac480 20 3d 20 7b 0a 20 20 30 78 30 30 2c 20 30 78 30   = {.  0x00, 0x0
ac490 31 2c 20 30 78 30 32 2c 20 30 78 30 33 2c 20 30  1, 0x02, 0x03, 0
ac4a0 78 30 34 2c 20 30 78 30 35 2c 20 30 78 30 36 2c  x04, 0x05, 0x06,
ac4b0 20 30 78 30 37 2c 0a 20 20 30 78 30 38 2c 20 30   0x07,.  0x08, 0
ac4c0 78 30 39 2c 20 30 78 30 61 2c 20 30 78 30 62 2c  x09, 0x0a, 0x0b,
ac4d0 20 30 78 30 63 2c 20 30 78 30 64 2c 20 30 78 30   0x0c, 0x0d, 0x0
ac4e0 65 2c 20 30 78 30 66 2c 0a 20 20 30 78 31 30 2c  e, 0x0f,.  0x10,
ac4f0 20 30 78 31 31 2c 20 30 78 31 32 2c 20 30 78 31   0x11, 0x12, 0x1
ac500 33 2c 20 30 78 31 34 2c 20 30 78 31 35 2c 20 30  3, 0x14, 0x15, 0
ac510 78 31 36 2c 20 30 78 31 37 2c 0a 20 20 30 78 31  x16, 0x17,.  0x1
ac520 38 2c 20 30 78 31 39 2c 20 30 78 31 61 2c 20 30  8, 0x19, 0x1a, 0
ac530 78 31 62 2c 20 30 78 31 63 2c 20 30 78 31 64 2c  x1b, 0x1c, 0x1d,
ac540 20 30 78 31 65 2c 20 30 78 31 66 2c 0a 20 20 30   0x1e, 0x1f,.  0
ac550 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 32 2c  x00, 0x01, 0x02,
ac560 20 30 78 30 33 2c 20 30 78 30 34 2c 20 30 78 30   0x03, 0x04, 0x0
ac570 35 2c 20 30 78 30 36 2c 20 30 78 30 37 2c 0a 20  5, 0x06, 0x07,. 
ac580 20 30 78 30 38 2c 20 30 78 30 39 2c 20 30 78 30   0x08, 0x09, 0x0
ac590 61 2c 20 30 78 30 62 2c 20 30 78 30 63 2c 20 30  a, 0x0b, 0x0c, 0
ac5a0 78 30 64 2c 20 30 78 30 65 2c 20 30 78 30 66 2c  x0d, 0x0e, 0x0f,
ac5b0 0a 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30  .  0x00, 0x01, 0
ac5c0 78 30 32 2c 20 30 78 30 33 2c 20 30 78 30 34 2c  x02, 0x03, 0x04,
ac5d0 20 30 78 30 35 2c 20 30 78 30 36 2c 20 30 78 30   0x05, 0x06, 0x0
ac5e0 37 2c 0a 20 20 30 78 30 30 2c 20 30 78 30 31 2c  7,.  0x00, 0x01,
ac5f0 20 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78 30   0x02, 0x03, 0x0
ac600 30 2c 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30  0, 0x01, 0x00, 0
ac610 78 30 30 2c 0a 7d 3b 0a 0a 0a 23 64 65 66 69 6e  x00,.};...#defin
ac620 65 20 57 52 49 54 45 5f 55 54 46 38 28 7a 4f 75  e WRITE_UTF8(zOu
ac630 74 2c 20 63 29 20 7b 20 20 20 20 20 20 20 20 20  t, c) {         
ac640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac650 20 5c 0a 20 20 69 66 28 20 63 3c 30 78 30 30 30   \.  if( c<0x000
ac660 38 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20  80 ){           
ac670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac680 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
ac690 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 63  *zOut++ = (u8)(c
ac6a0 26 30 78 46 46 29 3b 20 20 20 20 20 20 20 20 20  &0xFF);         
ac6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac6c0 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20     \.  }        
ac6d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac6e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac6f0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
ac700 65 6c 73 65 20 69 66 28 20 63 3c 30 78 30 30 38  else if( c<0x008
ac710 30 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20  00 ){           
ac720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac730 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74       \.    *zOut
ac740 2b 2b 20 3d 20 30 78 43 30 20 2b 20 28 75 38 29  ++ = 0xC0 + (u8)
ac750 28 28 63 3e 3e 36 29 26 30 78 31 46 29 3b 20 20  ((c>>6)&0x1F);  
ac760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
ac770 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78      *zOut++ = 0x
ac780 38 30 20 2b 20 28 75 38 29 28 63 20 26 20 30 78  80 + (u8)(c & 0x
ac790 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  3F);            
ac7a0 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20         \.  }    
ac7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac7c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac7e0 5c 0a 20 20 65 6c 73 65 20 69 66 28 20 63 3c 30  \.  else if( c<0
ac7f0 78 31 30 30 30 30 20 29 7b 20 20 20 20 20 20 20  x10000 ){       
ac800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac810 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a           \.    *
ac820 7a 4f 75 74 2b 2b 20 3d 20 30 78 45 30 20 2b 20  zOut++ = 0xE0 + 
ac830 28 75 38 29 28 28 63 3e 3e 31 32 29 26 30 78 30  (u8)((c>>12)&0x0
ac840 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  F);             
ac850 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20    \.    *zOut++ 
ac860 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28 28 63  = 0x80 + (u8)((c
ac870 3e 3e 36 29 20 26 20 30 78 33 46 29 3b 20 20 20  >>6) & 0x3F);   
ac880 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
ac890 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20   *zOut++ = 0x80 
ac8a0 2b 20 28 75 38 29 28 63 20 26 20 30 78 33 46 29  + (u8)(c & 0x3F)
ac8b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
ac8c0 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20 20      \.  }else{  
ac8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
ac900 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 46     *zOut++ = 0xF
ac910 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 31 38 29  0 + (u8)((c>>18)
ac920 20 26 20 30 78 30 37 29 3b 20 20 20 20 20 20 20   & 0x07);       
ac930 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75        \.    *zOu
ac940 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38  t++ = 0x80 + (u8
ac950 29 28 28 63 3e 3e 31 32 29 20 26 20 30 78 33 46  )((c>>12) & 0x3F
ac960 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 5c  );             \
ac970 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30  .    *zOut++ = 0
ac980 78 38 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 36  x80 + (u8)((c>>6
ac990 29 20 26 20 30 78 33 46 29 3b 20 20 20 20 20 20  ) & 0x3F);      
ac9a0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a          \.    *z
ac9b0 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28  Out++ = 0x80 + (
ac9c0 75 38 29 28 63 20 26 20 30 78 33 46 29 3b 20 20  u8)(c & 0x3F);  
ac9d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ac9e0 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20   \.  }          
ac9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aca00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aca10 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23            \.}..#
aca20 64 65 66 69 6e 65 20 57 52 49 54 45 5f 55 54 46  define WRITE_UTF
aca30 31 36 4c 45 28 7a 4f 75 74 2c 20 63 29 20 7b 20  16LE(zOut, c) { 
aca40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aca50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aca60 20 20 20 5c 0a 20 20 69 66 28 20 63 3c 3d 30 78     \.  if( c<=0x
aca70 46 46 46 46 20 29 7b 20 20 20 20 20 20 20 20 20  FFFF ){         
aca80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aca90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acaa0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a           \.    *
acab0 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 63 26  zOut++ = (u8)(c&
acac0 30 78 30 30 46 46 29 3b 20 20 20 20 20 20 20 20  0x00FF);        
acad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
acaf0 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28  .    *zOut++ = (
acb00 75 38 29 28 28 63 3e 3e 38 29 26 30 78 30 30 46  u8)((c>>8)&0x00F
acb10 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  F);             
acb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acb30 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20       \.  }else{ 
acb40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acb50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acb60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acb70 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
acb80 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28   *zOut++ = (u8)(
acb90 28 28 63 3e 3e 31 30 29 26 30 78 30 30 33 46 29  ((c>>10)&0x003F)
acba0 20 2b 20 28 28 28 63 2d 30 78 31 30 30 30 30 29   + (((c-0x10000)
acbb0 3e 3e 31 30 29 26 30 78 30 30 43 30 29 29 3b 20  >>10)&0x00C0)); 
acbc0 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d   \.    *zOut++ =
acbd0 20 28 75 38 29 28 30 78 30 30 44 38 20 2b 20 28   (u8)(0x00D8 + (
acbe0 28 28 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 38  ((c-0x10000)>>18
acbf0 29 26 30 78 30 33 29 29 3b 20 20 20 20 20 20 20  )&0x03));       
acc00 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f         \.    *zO
acc10 75 74 2b 2b 20 3d 20 28 75 38 29 28 63 26 30 78  ut++ = (u8)(c&0x
acc20 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 20 20  00FF);          
acc30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acc40 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
acc50 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38     *zOut++ = (u8
acc60 29 28 30 78 30 30 44 43 20 2b 20 28 28 63 3e 3e  )(0x00DC + ((c>>
acc70 38 29 26 30 78 30 33 29 29 3b 20 20 20 20 20 20  8)&0x03));      
acc80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acc90 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20     \.  }        
acca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
accb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
accc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
accd0 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 64           \.}..#d
acce0 65 66 69 6e 65 20 57 52 49 54 45 5f 55 54 46 31  efine WRITE_UTF1
accf0 36 42 45 28 7a 4f 75 74 2c 20 63 29 20 7b 20 20  6BE(zOut, c) {  
acd00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acd10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acd20 20 20 5c 0a 20 20 69 66 28 20 63 3c 3d 30 78 46    \.  if( c<=0xF
acd30 46 46 46 20 29 7b 20 20 20 20 20 20 20 20 20 20  FFF ){          
acd40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acd50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acd60 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a          \.    *z
acd70 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 28 63 3e  Out++ = (u8)((c>
acd80 3e 38 29 26 30 78 30 30 46 46 29 3b 20 20 20 20  >8)&0x00FF);    
acd90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
acdb0 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75      *zOut++ = (u
acdc0 38 29 28 63 26 30 78 30 30 46 46 29 3b 20 20 20  8)(c&0x00FF);   
acdd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acdf0 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20 20      \.  }else{  
ace00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ace10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ace20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ace30 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
ace40 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 30  *zOut++ = (u8)(0
ace50 78 30 30 44 38 20 2b 20 28 28 28 63 2d 30 78 31  x00D8 + (((c-0x1
ace60 30 30 30 30 29 3e 3e 31 38 29 26 30 78 30 33 29  0000)>>18)&0x03)
ace70 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
ace80 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20  \.    *zOut++ = 
ace90 28 75 38 29 28 28 28 63 3e 3e 31 30 29 26 30 78  (u8)(((c>>10)&0x
acea0 30 30 33 46 29 20 2b 20 28 28 28 63 2d 30 78 31  003F) + (((c-0x1
aceb0 30 30 30 30 29 3e 3e 31 30 29 26 30 78 30 30 43  0000)>>10)&0x00C
acec0 30 29 29 3b 20 20 5c 0a 20 20 20 20 2a 7a 4f 75  0));  \.    *zOu
aced0 74 2b 2b 20 3d 20 28 75 38 29 28 30 78 30 30 44  t++ = (u8)(0x00D
acee0 43 20 2b 20 28 28 63 3e 3e 38 29 26 30 78 30 33  C + ((c>>8)&0x03
acef0 29 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ));             
acf00 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
acf10 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29    *zOut++ = (u8)
acf20 28 63 26 30 78 30 30 46 46 29 3b 20 20 20 20 20  (c&0x00FF);     
acf30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acf40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acf50 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20    \.  }         
acf60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acf70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acf80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acf90 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 65          \.}..#de
acfa0 66 69 6e 65 20 52 45 41 44 5f 55 54 46 31 36 4c  fine READ_UTF16L
acfb0 45 28 7a 49 6e 2c 20 63 29 7b 20 20 20 20 20 20  E(zIn, c){      
acfc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acfd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
acfe0 20 20 20 5c 0a 20 20 63 20 3d 20 28 2a 7a 49 6e     \.  c = (*zIn
acff0 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  ++);            
ad000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad020 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 63             \.  c
ad030 20 2b 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38   += ((*zIn++)<<8
ad040 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
ad050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad070 20 20 20 5c 0a 20 20 69 66 28 20 63 3e 3d 30 78     \.  if( c>=0x
ad080 44 38 30 30 20 26 26 20 63 3c 30 78 45 30 30 30  D800 && c<0xE000
ad090 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20   ){             
ad0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad0b0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
ad0c0 20 69 6e 74 20 63 32 20 3d 20 28 2a 7a 49 6e 2b   int c2 = (*zIn+
ad0d0 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  +);             
ad0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad100 20 20 20 5c 0a 20 20 20 20 63 32 20 2b 3d 20 28     \.    c2 += (
ad110 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20  (*zIn++)<<8);   
ad120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad140 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
ad150 20 63 20 3d 20 28 63 32 26 30 78 30 33 46 46 29   c = (c2&0x03FF)
ad160 20 2b 20 28 28 63 26 30 78 30 30 33 46 29 3c 3c   + ((c&0x003F)<<
ad170 31 30 29 20 2b 20 28 28 28 63 26 30 78 30 33 43  10) + (((c&0x03C
ad180 30 29 2b 30 78 30 30 34 30 29 3c 3c 31 30 29 3b  0)+0x0040)<<10);
ad190 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20     \.  }        
ad1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad1d0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a             \.}..
ad1e0 23 64 65 66 69 6e 65 20 52 45 41 44 5f 55 54 46  #define READ_UTF
ad1f0 31 36 42 45 28 7a 49 6e 2c 20 63 29 7b 20 20 20  16BE(zIn, c){   
ad200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad220 20 20 20 20 20 20 5c 0a 20 20 63 20 3d 20 28 28        \.  c = ((
ad230 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 20  *zIn++)<<8);    
ad240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
ad270 20 20 63 20 2b 3d 20 28 2a 7a 49 6e 2b 2b 29 3b    c += (*zIn++);
ad280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad2b0 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3e        \.  if( c>
ad2c0 3d 30 78 44 38 30 30 20 26 26 20 63 3c 30 78 45  =0xD800 && c<0xE
ad2d0 30 30 30 20 29 7b 20 20 20 20 20 20 20 20 20 20  000 ){          
ad2e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad2f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
ad300 20 20 20 20 69 6e 74 20 63 32 20 3d 20 28 28 2a      int c2 = ((*
ad310 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 20  zIn++)<<8);     
ad320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad340 20 20 20 20 20 20 5c 0a 20 20 20 20 63 32 20 2b        \.    c2 +
ad350 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20  = (*zIn++);     
ad360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
ad390 20 20 20 20 63 20 3d 20 28 63 32 26 30 78 30 33      c = (c2&0x03
ad3a0 46 46 29 20 2b 20 28 28 63 26 30 78 30 30 33 46  FF) + ((c&0x003F
ad3b0 29 3c 3c 31 30 29 20 2b 20 28 28 28 63 26 30 78  )<<10) + (((c&0x
ad3c0 30 33 43 30 29 2b 30 78 30 30 34 30 29 3c 3c 31  03C0)+0x0040)<<1
ad3d0 30 29 3b 20 20 20 5c 0a 20 20 7d 20 20 20 20 20  0);   \.  }     
ad3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad3f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
ad420 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61  }../*.** Transla
ad430 74 65 20 61 20 73 69 6e 67 6c 65 20 55 54 46 2d  te a single UTF-
ad440 38 20 63 68 61 72 61 63 74 65 72 2e 20 20 52 65  8 character.  Re
ad450 74 75 72 6e 20 74 68 65 20 75 6e 69 63 6f 64 65  turn the unicode
ad460 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 44 75   value..**.** Du
ad470 72 69 6e 67 20 74 72 61 6e 73 6c 61 74 69 6f 6e  ring translation
ad480 2c 20 61 73 73 75 6d 65 20 74 68 61 74 20 74 68  , assume that th
ad490 65 20 62 79 74 65 20 74 68 61 74 20 7a 54 65 72  e byte that zTer
ad4a0 6d 20 70 6f 69 6e 74 73 0a 2a 2a 20 69 73 20 61  m points.** is a
ad4b0 20 30 78 30 30 2e 0a 2a 2a 0a 2a 2a 20 57 72 69   0x00..**.** Wri
ad4c0 74 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  te a pointer to 
ad4d0 74 68 65 20 6e 65 78 74 20 75 6e 72 65 61 64 20  the next unread 
ad4e0 62 79 74 65 20 62 61 63 6b 20 69 6e 74 6f 20 2a  byte back into *
ad4f0 70 7a 4e 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 4e 6f  pzNext..**.** No
ad500 74 65 73 20 4f 6e 20 49 6e 76 61 6c 69 64 20 55  tes On Invalid U
ad510 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a 20 20 2a 20 20  TF-8:.**.**  *  
ad520 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6e 65 76  This routine nev
ad530 65 72 20 61 6c 6c 6f 77 73 20 61 20 37 2d 62 69  er allows a 7-bi
ad540 74 20 63 68 61 72 61 63 74 65 72 20 28 30 78 30  t character (0x0
ad550 30 20 74 68 72 6f 75 67 68 20 30 78 37 66 29 20  0 through 0x7f) 
ad560 74 6f 0a 2a 2a 20 20 20 20 20 62 65 20 65 6e 63  to.**     be enc
ad570 6f 64 65 64 20 61 73 20 61 20 6d 75 6c 74 69 2d  oded as a multi-
ad580 62 79 74 65 20 63 68 61 72 61 63 74 65 72 2e 20  byte character. 
ad590 20 41 6e 79 20 6d 75 6c 74 69 2d 62 79 74 65 20   Any multi-byte 
ad5a0 63 68 61 72 61 63 74 65 72 20 74 68 61 74 0a 2a  character that.*
ad5b0 2a 20 20 20 20 20 61 74 74 65 6d 70 74 73 20 74  *     attempts t
ad5c0 6f 20 65 6e 63 6f 64 65 20 61 20 76 61 6c 75 65  o encode a value
ad5d0 20 62 65 74 77 65 65 6e 20 30 78 30 30 20 61 6e   between 0x00 an
ad5e0 64 20 30 78 37 66 20 69 73 20 72 65 6e 64 65 72  d 0x7f is render
ad5f0 65 64 20 61 73 20 30 78 66 66 66 64 2e 0a 2a 2a  ed as 0xfffd..**
ad600 0a 2a 2a 20 20 2a 20 20 54 68 69 73 20 72 6f 75  .**  *  This rou
ad610 74 69 6e 65 20 6e 65 76 65 72 20 61 6c 6c 6f 77  tine never allow
ad620 73 20 61 20 55 54 46 31 36 20 73 75 72 72 6f 67  s a UTF16 surrog
ad630 61 74 65 20 76 61 6c 75 65 20 74 6f 20 62 65 20  ate value to be 
ad640 65 6e 63 6f 64 65 64 2e 0a 2a 2a 20 20 20 20 20  encoded..**     
ad650 49 66 20 61 20 6d 75 6c 74 69 2d 62 79 74 65 20  If a multi-byte 
ad660 63 68 61 72 61 63 74 65 72 20 61 74 74 65 6d 70  character attemp
ad670 74 73 20 74 6f 20 65 6e 63 6f 64 65 20 61 20 76  ts to encode a v
ad680 61 6c 75 65 20 62 65 74 77 65 65 6e 0a 2a 2a 20  alue between.** 
ad690 20 20 20 20 30 78 64 38 30 30 20 61 6e 64 20 30      0xd800 and 0
ad6a0 78 65 30 30 30 20 74 68 65 6e 20 69 74 20 69 73  xe000 then it is
ad6b0 20 72 65 6e 64 65 72 65 64 20 61 73 20 30 78 66   rendered as 0xf
ad6c0 66 66 64 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 42  ffd..**.**  *  B
ad6d0 79 74 65 73 20 69 6e 20 74 68 65 20 72 61 6e 67  ytes in the rang
ad6e0 65 20 6f 66 20 30 78 38 30 20 74 68 72 6f 75 67  e of 0x80 throug
ad6f0 68 20 30 78 62 66 20 77 68 69 63 68 20 6f 63 63  h 0xbf which occ
ad700 75 72 20 61 73 20 74 68 65 20 66 69 72 73 74 0a  ur as the first.
ad710 2a 2a 20 20 20 20 20 62 79 74 65 20 6f 66 20 61  **     byte of a
ad720 20 63 68 61 72 61 63 74 65 72 20 61 72 65 20 69   character are i
ad730 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 73 69  nterpreted as si
ad740 6e 67 6c 65 2d 62 79 74 65 20 63 68 61 72 61 63  ngle-byte charac
ad750 74 65 72 73 0a 2a 2a 20 20 20 20 20 61 6e 64 20  ters.**     and 
ad760 72 65 6e 64 65 72 65 64 20 61 73 20 74 68 65 6d  rendered as them
ad770 73 65 6c 76 65 73 20 65 76 65 6e 20 74 68 6f 75  selves even thou
ad780 67 68 20 74 68 65 79 20 61 72 65 20 74 65 63 68  gh they are tech
ad790 6e 69 63 61 6c 6c 79 0a 2a 2a 20 20 20 20 20 69  nically.**     i
ad7a0 6e 76 61 6c 69 64 20 63 68 61 72 61 63 74 65 72  nvalid character
ad7b0 73 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54 68 69  s..**.**  *  Thi
ad7c0 73 20 72 6f 75 74 69 6e 65 20 61 63 63 65 70 74  s routine accept
ad7d0 73 20 61 6e 20 69 6e 66 69 6e 69 74 65 20 6e 75  s an infinite nu
ad7e0 6d 62 65 72 20 6f 66 20 64 69 66 66 65 72 65 6e  mber of differen
ad7f0 74 20 55 54 46 38 20 65 6e 63 6f 64 69 6e 67 73  t UTF8 encodings
ad800 0a 2a 2a 20 20 20 20 20 66 6f 72 20 75 6e 69 63  .**     for unic
ad810 6f 64 65 20 76 61 6c 75 65 73 20 30 78 38 30 20  ode values 0x80 
ad820 61 6e 64 20 67 72 65 61 74 65 72 2e 20 20 49 74  and greater.  It
ad830 20 64 6f 20 6e 6f 74 20 63 68 61 6e 67 65 20 6f   do not change o
ad840 76 65 72 2d 6c 65 6e 67 74 68 0a 2a 2a 20 20 20  ver-length.**   
ad850 20 20 65 6e 63 6f 64 69 6e 67 73 20 74 6f 20 30    encodings to 0
ad860 78 66 66 66 64 20 61 73 20 73 6f 6d 65 20 73 79  xfffd as some sy
ad870 73 74 65 6d 73 20 72 65 63 6f 6d 6d 65 6e 64 2e  stems recommend.
ad880 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 45 41 44  .*/.#define READ
ad890 5f 55 54 46 38 28 7a 49 6e 2c 20 7a 54 65 72 6d  _UTF8(zIn, zTerm
ad8a0 2c 20 63 29 20 20 20 20 20 20 20 20 20 20 20 20  , c)            
ad8b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
ad8c0 0a 20 20 63 20 3d 20 2a 28 7a 49 6e 2b 2b 29 3b  .  c = *(zIn++);
ad8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad8f0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
ad900 69 66 28 20 63 3e 3d 30 78 63 30 20 29 7b 20 20  if( c>=0xc0 ){  
ad910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad930 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63           \.    c
ad940 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 54 72   = sqlite3Utf8Tr
ad950 61 6e 73 31 5b 63 2d 30 78 63 30 5d 3b 20 20 20  ans1[c-0xc0];   
ad960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad970 20 20 20 20 20 20 5c 0a 20 20 20 20 77 68 69 6c        \.    whil
ad980 65 28 20 7a 49 6e 21 3d 7a 54 65 72 6d 20 26 26  e( zIn!=zTerm &&
ad990 20 28 2a 7a 49 6e 20 26 20 30 78 63 30 29 3d 3d   (*zIn & 0xc0)==
ad9a0 30 78 38 30 20 29 7b 20 20 20 20 20 20 20 20 20  0x80 ){         
ad9b0 20 20 20 5c 0a 20 20 20 20 20 20 63 20 3d 20 28     \.      c = (
ad9c0 63 3c 3c 36 29 20 2b 20 28 30 78 33 66 20 26 20  c<<6) + (0x3f & 
ad9d0 2a 28 7a 49 6e 2b 2b 29 29 3b 20 20 20 20 20 20  *(zIn++));      
ad9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad9f0 5c 0a 20 20 20 20 7d 20 20 20 20 20 20 20 20 20  \.    }         
ada00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ada10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ada20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
ada30 20 20 20 69 66 28 20 63 3c 30 78 38 30 20 20 20     if( c<0x80   
ada40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ada50 20 20 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 5c 0a 20 20 20 20            \.    
ada70 20 20 20 20 7c 7c 20 28 63 26 30 78 46 46 46 46      || (c&0xFFFF
ada80 46 38 30 30 29 3d 3d 30 78 44 38 30 30 20 20 20  F800)==0xD800   
ada90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adaa0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 20 20 20         \.       
adab0 20 7c 7c 20 28 63 26 30 78 46 46 46 46 46 46 46   || (c&0xFFFFFFF
adac0 45 29 3d 3d 30 78 46 46 46 45 20 29 7b 20 20 63  E)==0xFFFE ){  c
adad0 20 3d 20 30 78 46 46 46 44 3b 20 7d 20 20 20 20   = 0xFFFD; }    
adae0 20 20 20 20 5c 0a 20 20 7d 0a 53 51 4c 49 54 45      \.  }.SQLITE
adaf0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
adb00 69 74 65 33 55 74 66 38 52 65 61 64 28 0a 20 20  ite3Utf8Read(.  
adb10 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
adb20 68 61 72 20 2a 7a 49 6e 2c 20 20 20 20 20 20 20  har *zIn,       
adb30 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f 66  /* First byte of
adb40 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72   UTF-8 character
adb50 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69   */.  const unsi
adb60 67 6e 65 64 20 63 68 61 72 20 2a 2a 70 7a 4e 65  gned char **pzNe
adb70 78 74 20 20 20 20 2f 2a 20 57 72 69 74 65 20 66  xt    /* Write f
adb80 69 72 73 74 20 62 79 74 65 20 70 61 73 74 20 55  irst byte past U
adb90 54 46 2d 38 20 63 68 61 72 20 68 65 72 65 20 2a  TF-8 char here *
adba0 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 3b 0a 0a 20  /.){.  int c;.. 
adbb0 20 2f 2a 20 53 61 6d 65 20 61 73 20 52 45 41 44   /* Same as READ
adbc0 5f 55 54 46 38 28 29 20 61 62 6f 76 65 20 62 75  _UTF8() above bu
adbd0 74 20 77 69 74 68 6f 75 74 20 74 68 65 20 7a 54  t without the zT
adbe0 65 72 6d 20 70 61 72 61 6d 65 74 65 72 2e 0a 20  erm parameter.. 
adbf0 20 2a 2a 20 46 6f 72 20 74 68 69 73 20 72 6f 75   ** For this rou
adc00 74 69 6e 65 2c 20 77 65 20 61 73 73 75 6d 65 20  tine, we assume 
adc10 74 68 65 20 55 54 46 38 20 73 74 72 69 6e 67 20  the UTF8 string 
adc20 69 73 20 61 6c 77 61 79 73 20 7a 65 72 6f 2d 74  is always zero-t
adc30 65 72 6d 69 6e 61 74 65 64 2e 0a 20 20 2a 2f 0a  erminated..  */.
adc40 20 20 63 20 3d 20 2a 28 7a 49 6e 2b 2b 29 3b 0a    c = *(zIn++);.
adc50 20 20 69 66 28 20 63 3e 3d 30 78 63 30 20 29 7b    if( c>=0xc0 ){
adc60 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33  .    c = sqlite3
adc70 55 74 66 38 54 72 61 6e 73 31 5b 63 2d 30 78 63  Utf8Trans1[c-0xc
adc80 30 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28  0];.    while( (
adc90 2a 7a 49 6e 20 26 20 30 78 63 30 29 3d 3d 30 78  *zIn & 0xc0)==0x
adca0 38 30 20 29 7b 0a 20 20 20 20 20 20 63 20 3d 20  80 ){.      c = 
adcb0 28 63 3c 3c 36 29 20 2b 20 28 30 78 33 66 20 26  (c<<6) + (0x3f &
adcc0 20 2a 28 7a 49 6e 2b 2b 29 29 3b 0a 20 20 20 20   *(zIn++));.    
adcd0 7d 0a 20 20 20 20 69 66 28 20 63 3c 30 78 38 30  }.    if( c<0x80
adce0 0a 20 20 20 20 20 20 20 20 7c 7c 20 28 63 26 30  .        || (c&0
adcf0 78 46 46 46 46 46 38 30 30 29 3d 3d 30 78 44 38  xFFFFF800)==0xD8
add00 30 30 0a 20 20 20 20 20 20 20 20 7c 7c 20 28 63  00.        || (c
add10 26 30 78 46 46 46 46 46 46 46 45 29 3d 3d 30 78  &0xFFFFFFFE)==0x
add20 46 46 46 45 20 29 7b 20 20 63 20 3d 20 30 78 46  FFFE ){  c = 0xF
add30 46 46 44 3b 20 7d 0a 20 20 7d 0a 20 20 2a 70 7a  FFD; }.  }.  *pz
add40 4e 65 78 74 20 3d 20 7a 49 6e 3b 0a 20 20 72 65  Next = zIn;.  re
add50 74 75 72 6e 20 63 3b 0a 7d 0a 0a 0a 0a 0a 2f 2a  turn c;.}...../*
add60 0a 2a 2a 20 49 66 20 74 68 65 20 54 52 41 4e 53  .** If the TRANS
add70 4c 41 54 45 5f 54 52 41 43 45 20 6d 61 63 72 6f  LATE_TRACE macro
add80 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65   is defined, the
add90 20 76 61 6c 75 65 20 6f 66 20 65 61 63 68 20 4d   value of each M
adda0 65 6d 20 69 73 0a 2a 2a 20 70 72 69 6e 74 65 64  em is.** printed
addb0 20 6f 6e 20 73 74 64 65 72 72 20 6f 6e 20 74 68   on stderr on th
addc0 65 20 77 61 79 20 69 6e 74 6f 20 61 6e 64 20 6f  e way into and o
addd0 75 74 20 6f 66 20 73 71 6c 69 74 65 33 56 64 62  ut of sqlite3Vdb
adde0 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 29 2e  eMemTranslate().
addf0 0a 2a 2f 20 0a 2f 2a 20 23 64 65 66 69 6e 65 20  .*/ ./* #define 
ade00 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 20  TRANSLATE_TRACE 
ade10 31 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  1 */..#ifndef SQ
ade20 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a  LITE_OMIT_UTF16.
ade30 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
ade40 6e 65 20 74 72 61 6e 73 66 6f 72 6d 73 20 74 68  ne transforms th
ade50 65 20 69 6e 74 65 72 6e 61 6c 20 74 65 78 74 20  e internal text 
ade60 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 62 79  encoding used by
ade70 20 70 4d 65 6d 20 74 6f 0a 2a 2a 20 64 65 73 69   pMem to.** desi
ade80 72 65 64 45 6e 63 2e 20 49 74 20 69 73 20 61 6e  redEnc. It is an
ade90 20 65 72 72 6f 72 20 69 66 20 74 68 65 20 73 74   error if the st
adea0 72 69 6e 67 20 69 73 20 61 6c 72 65 61 64 79 20  ring is already 
adeb0 6f 66 20 74 68 65 20 64 65 73 69 72 65 64 0a 2a  of the desired.*
adec0 2a 20 65 6e 63 6f 64 69 6e 67 2c 20 6f 72 20 69  * encoding, or i
aded0 66 20 2a 70 4d 65 6d 20 64 6f 65 73 20 6e 6f 74  f *pMem does not
adee0 20 63 6f 6e 74 61 69 6e 20 61 20 73 74 72 69 6e   contain a strin
adef0 67 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49  g value..*/.SQLI
adf00 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
adf10 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 72 61  qlite3VdbeMemTra
adf20 6e 73 6c 61 74 65 28 4d 65 6d 20 2a 70 4d 65 6d  nslate(Mem *pMem
adf30 2c 20 75 38 20 64 65 73 69 72 65 64 45 6e 63 29  , u8 desiredEnc)
adf40 7b 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20 20  {.  int len;    
adf50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adf60 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74  /* Maximum lengt
adf70 68 20 6f 66 20 6f 75 74 70 75 74 20 73 74 72 69  h of output stri
adf80 6e 67 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20  ng in bytes */. 
adf90 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
adfa0 7a 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20 20  zOut;           
adfb0 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74         /* Output
adfc0 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 75 6e 73   buffer */.  uns
adfd0 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 49 6e 3b  igned char *zIn;
adfe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adff0 20 20 20 2f 2a 20 49 6e 70 75 74 20 69 74 65 72     /* Input iter
ae000 61 74 6f 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  ator */.  unsign
ae010 65 64 20 63 68 61 72 20 2a 7a 54 65 72 6d 3b 20  ed char *zTerm; 
ae020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae030 2f 2a 20 45 6e 64 20 6f 66 20 69 6e 70 75 74 20  /* End of input 
ae040 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  */.  unsigned ch
ae050 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20  ar *z;          
ae060 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75             /* Ou
ae070 74 70 75 74 20 69 74 65 72 61 74 6f 72 20 2a 2f  tput iterator */
ae080 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  .  unsigned int 
ae090 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4d  c;..  assert( pM
ae0a0 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  em->db==0 || sql
ae0b0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
ae0c0 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  pMem->db->mutex)
ae0d0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d   );.  assert( pM
ae0e0 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74  em->flags&MEM_St
ae0f0 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  r );.  assert( p
ae100 4d 65 6d 2d 3e 65 6e 63 21 3d 64 65 73 69 72 65  Mem->enc!=desire
ae110 64 45 6e 63 20 29 3b 0a 20 20 61 73 73 65 72 74  dEnc );.  assert
ae120 28 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 30 20 29  ( pMem->enc!=0 )
ae130 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d  ;.  assert( pMem
ae140 2d 3e 6e 3e 3d 30 20 29 3b 0a 0a 23 69 66 20 64  ->n>=0 );..#if d
ae150 65 66 69 6e 65 64 28 54 52 41 4e 53 4c 41 54 45  efined(TRANSLATE
ae160 5f 54 52 41 43 45 29 20 26 26 20 64 65 66 69 6e  _TRACE) && defin
ae170 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29  ed(SQLITE_DEBUG)
ae180 0a 20 20 7b 0a 20 20 20 20 63 68 61 72 20 7a 42  .  {.    char zB
ae190 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 73 71 6c  uf[100];.    sql
ae1a0 69 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74  ite3VdbeMemPrett
ae1b0 79 50 72 69 6e 74 28 70 4d 65 6d 2c 20 7a 42 75  yPrint(pMem, zBu
ae1c0 66 29 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28  f);.    fprintf(
ae1d0 73 74 64 65 72 72 2c 20 22 49 4e 50 55 54 3a 20  stderr, "INPUT: 
ae1e0 20 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b 0a 20   %s\n", zBuf);. 
ae1f0 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20   }.#endif..  /* 
ae200 49 66 20 74 68 65 20 74 72 61 6e 73 6c 61 74 69  If the translati
ae210 6f 6e 20 69 73 20 62 65 74 77 65 65 6e 20 55 54  on is between UT
ae220 46 2d 31 36 20 6c 69 74 74 6c 65 20 61 6e 64 20  F-16 little and 
ae230 62 69 67 20 65 6e 64 69 61 6e 2c 20 74 68 65 6e  big endian, then
ae240 20 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 61 74 20   .  ** all that 
ae250 69 73 20 72 65 71 75 69 72 65 64 20 69 73 20 74  is required is t
ae260 6f 20 73 77 61 70 20 74 68 65 20 62 79 74 65 20  o swap the byte 
ae270 6f 72 64 65 72 2e 20 54 68 69 73 20 63 61 73 65  order. This case
ae280 20 69 73 20 68 61 6e 64 6c 65 64 0a 20 20 2a 2a   is handled.  **
ae290 20 64 69 66 66 65 72 65 6e 74 6c 79 20 66 72 6f   differently fro
ae2a0 6d 20 74 68 65 20 6f 74 68 65 72 73 2e 0a 20 20  m the others..  
ae2b0 2a 2f 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65  */.  if( pMem->e
ae2c0 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20  nc!=SQLITE_UTF8 
ae2d0 26 26 20 64 65 73 69 72 65 64 45 6e 63 21 3d 53  && desiredEnc!=S
ae2e0 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 20  QLITE_UTF8 ){.  
ae2f0 20 20 75 38 20 74 65 6d 70 3b 0a 20 20 20 20 69    u8 temp;.    i
ae300 6e 74 20 72 63 3b 0a 20 20 20 20 72 63 20 3d 20  nt rc;.    rc = 
ae310 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61  sqlite3VdbeMemMa
ae320 6b 65 57 72 69 74 65 61 62 6c 65 28 70 4d 65 6d  keWriteable(pMem
ae330 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
ae340 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
ae350 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
ae360 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20  LITE_NOMEM );.  
ae370 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
ae380 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20  E_NOMEM;.    }. 
ae390 20 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29 70 4d     zIn = (u8*)pM
ae3a0 65 6d 2d 3e 7a 3b 0a 20 20 20 20 7a 54 65 72 6d  em->z;.    zTerm
ae3b0 20 3d 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 26   = &zIn[pMem->n&
ae3c0 7e 31 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  ~1];.    while( 
ae3d0 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20  zIn<zTerm ){.   
ae3e0 20 20 20 74 65 6d 70 20 3d 20 2a 7a 49 6e 3b 0a     temp = *zIn;.
ae3f0 20 20 20 20 20 20 2a 7a 49 6e 20 3d 20 2a 28 7a        *zIn = *(z
ae400 49 6e 2b 31 29 3b 0a 20 20 20 20 20 20 7a 49 6e  In+1);.      zIn
ae410 2b 2b 3b 0a 20 20 20 20 20 20 2a 7a 49 6e 2b 2b  ++;.      *zIn++
ae420 20 3d 20 74 65 6d 70 3b 0a 20 20 20 20 7d 0a 20   = temp;.    }. 
ae430 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 64     pMem->enc = d
ae440 65 73 69 72 65 64 45 6e 63 3b 0a 20 20 20 20 67  esiredEnc;.    g
ae450 6f 74 6f 20 74 72 61 6e 73 6c 61 74 65 5f 6f 75  oto translate_ou
ae460 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74  t;.  }..  /* Set
ae470 20 6c 65 6e 20 74 6f 20 74 68 65 20 6d 61 78 69   len to the maxi
ae480 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  mum number of by
ae490 74 65 73 20 72 65 71 75 69 72 65 64 20 69 6e 20  tes required in 
ae4a0 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65  the output buffe
ae4b0 72 2e 20 2a 2f 0a 20 20 69 66 28 20 64 65 73 69  r. */.  if( desi
ae4c0 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  redEnc==SQLITE_U
ae4d0 54 46 38 20 29 7b 0a 20 20 20 20 2f 2a 20 57 68  TF8 ){.    /* Wh
ae4e0 65 6e 20 63 6f 6e 76 65 72 74 69 6e 67 20 66 72  en converting fr
ae4f0 6f 6d 20 55 54 46 2d 31 36 2c 20 74 68 65 20 6d  om UTF-16, the m
ae500 61 78 69 6d 75 6d 20 67 72 6f 77 74 68 20 72 65  aximum growth re
ae510 73 75 6c 74 73 20 66 72 6f 6d 0a 20 20 20 20 2a  sults from.    *
ae520 2a 20 74 72 61 6e 73 6c 61 74 69 6e 67 20 61 20  * translating a 
ae530 32 2d 62 79 74 65 20 63 68 61 72 61 63 74 65 72  2-byte character
ae540 20 74 6f 20 61 20 34 2d 62 79 74 65 20 55 54 46   to a 4-byte UTF
ae550 2d 38 20 63 68 61 72 61 63 74 65 72 2e 0a 20 20  -8 character..  
ae560 20 20 2a 2a 20 41 20 73 69 6e 67 6c 65 20 62 79    ** A single by
ae570 74 65 20 69 73 20 72 65 71 75 69 72 65 64 20 66  te is required f
ae580 6f 72 20 74 68 65 20 6f 75 74 70 75 74 20 73 74  or the output st
ae590 72 69 6e 67 0a 20 20 20 20 2a 2a 20 6e 75 6c 2d  ring.    ** nul-
ae5a0 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 20 20 20 20  terminator..    
ae5b0 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 26  */.    pMem->n &
ae5c0 3d 20 7e 31 3b 0a 20 20 20 20 6c 65 6e 20 3d 20  = ~1;.    len = 
ae5d0 70 4d 65 6d 2d 3e 6e 20 2a 20 32 20 2b 20 31 3b  pMem->n * 2 + 1;
ae5e0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
ae5f0 20 57 68 65 6e 20 63 6f 6e 76 65 72 74 69 6e 67   When converting
ae600 20 66 72 6f 6d 20 55 54 46 2d 38 20 74 6f 20 55   from UTF-8 to U
ae610 54 46 2d 31 36 20 74 68 65 20 6d 61 78 69 6d 75  TF-16 the maximu
ae620 6d 20 67 72 6f 77 74 68 20 69 73 20 63 61 75 73  m growth is caus
ae630 65 64 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20 61  ed.    ** when a
ae640 20 31 2d 62 79 74 65 20 55 54 46 2d 38 20 63 68   1-byte UTF-8 ch
ae650 61 72 61 63 74 65 72 20 69 73 20 74 72 61 6e 73  aracter is trans
ae660 6c 61 74 65 64 20 69 6e 74 6f 20 61 20 32 2d 62  lated into a 2-b
ae670 79 74 65 20 55 54 46 2d 31 36 0a 20 20 20 20 2a  yte UTF-16.    *
ae680 2a 20 63 68 61 72 61 63 74 65 72 2e 20 54 77 6f  * character. Two
ae690 20 62 79 74 65 73 20 61 72 65 20 72 65 71 75 69   bytes are requi
ae6a0 72 65 64 20 69 6e 20 74 68 65 20 6f 75 74 70 75  red in the outpu
ae6b0 74 20 62 75 66 66 65 72 20 66 6f 72 20 74 68 65  t buffer for the
ae6c0 0a 20 20 20 20 2a 2a 20 6e 75 6c 2d 74 65 72 6d  .    ** nul-term
ae6d0 69 6e 61 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20  inator..    */. 
ae6e0 20 20 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d 3e 6e     len = pMem->n
ae6f0 20 2a 20 32 20 2b 20 32 3b 0a 20 20 7d 0a 0a 20   * 2 + 2;.  }.. 
ae700 20 2f 2a 20 53 65 74 20 7a 49 6e 20 74 6f 20 70   /* Set zIn to p
ae710 6f 69 6e 74 20 61 74 20 74 68 65 20 73 74 61 72  oint at the star
ae720 74 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 62  t of the input b
ae730 75 66 66 65 72 20 61 6e 64 20 7a 54 65 72 6d 20  uffer and zTerm 
ae740 74 6f 20 70 6f 69 6e 74 20 31 0a 20 20 2a 2a 20  to point 1.  ** 
ae750 62 79 74 65 20 70 61 73 74 20 74 68 65 20 65 6e  byte past the en
ae760 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 72  d..  **.  ** Var
ae770 69 61 62 6c 65 20 7a 4f 75 74 20 69 73 20 73 65  iable zOut is se
ae780 74 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68  t to point at th
ae790 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 2c  e output buffer,
ae7a0 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64 0a   space obtained.
ae7b0 20 20 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65    ** from sqlite
ae7c0 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 20 20 2a 2f  3_malloc()..  */
ae7d0 0a 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29 70 4d  .  zIn = (u8*)pM
ae7e0 65 6d 2d 3e 7a 3b 0a 20 20 7a 54 65 72 6d 20 3d  em->z;.  zTerm =
ae7f0 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 5d 3b 0a   &zIn[pMem->n];.
ae800 20 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 33    zOut = sqlite3
ae810 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 4d 65 6d  DbMallocRaw(pMem
ae820 2d 3e 64 62 2c 20 6c 65 6e 29 3b 0a 20 20 69 66  ->db, len);.  if
ae830 28 20 21 7a 4f 75 74 20 29 7b 0a 20 20 20 20 72  ( !zOut ){.    r
ae840 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
ae850 45 4d 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 7a 4f  EM;.  }.  z = zO
ae860 75 74 3b 0a 0a 20 20 69 66 28 20 70 4d 65 6d 2d  ut;..  if( pMem-
ae870 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46  >enc==SQLITE_UTF
ae880 38 20 29 7b 0a 20 20 20 20 69 66 28 20 64 65 73  8 ){.    if( des
ae890 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f  iredEnc==SQLITE_
ae8a0 55 54 46 31 36 4c 45 20 29 7b 0a 20 20 20 20 20  UTF16LE ){.     
ae8b0 20 2f 2a 20 55 54 46 2d 38 20 2d 3e 20 55 54 46   /* UTF-8 -> UTF
ae8c0 2d 31 36 20 4c 69 74 74 6c 65 2d 65 6e 64 69 61  -16 Little-endia
ae8d0 6e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65  n */.      while
ae8e0 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20  ( zIn<zTerm ){. 
ae8f0 20 20 20 20 20 20 20 2f 2a 20 63 20 3d 20 73 71         /* c = sq
ae900 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 49  lite3Utf8Read(zI
ae910 6e 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74  n, zTerm, (const
ae920 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a 2f 0a   u8**)&zIn); */.
ae930 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 46          READ_UTF
ae940 38 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29  8(zIn, zTerm, c)
ae950 3b 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f  ;.        WRITE_
ae960 55 54 46 31 36 4c 45 28 7a 2c 20 63 29 3b 0a 20  UTF16LE(z, c);. 
ae970 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
ae980 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
ae990 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49  desiredEnc==SQLI
ae9a0 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 20  TE_UTF16BE );.  
ae9b0 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 2d 3e 20      /* UTF-8 -> 
ae9c0 55 54 46 2d 31 36 20 42 69 67 2d 65 6e 64 69 61  UTF-16 Big-endia
ae9d0 6e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65  n */.      while
ae9e0 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20  ( zIn<zTerm ){. 
ae9f0 20 20 20 20 20 20 20 2f 2a 20 63 20 3d 20 73 71         /* c = sq
aea00 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 49  lite3Utf8Read(zI
aea10 6e 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 74  n, zTerm, (const
aea20 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a 2f 0a   u8**)&zIn); */.
aea30 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 46          READ_UTF
aea40 38 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29  8(zIn, zTerm, c)
aea50 3b 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f  ;.        WRITE_
aea60 55 54 46 31 36 42 45 28 7a 2c 20 63 29 3b 0a 20  UTF16BE(z, c);. 
aea70 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
aea80 20 70 4d 65 6d 2d 3e 6e 20 3d 20 28 69 6e 74 29   pMem->n = (int)
aea90 28 7a 20 2d 20 7a 4f 75 74 29 3b 0a 20 20 20 20  (z - zOut);.    
aeaa0 2a 7a 2b 2b 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  *z++ = 0;.  }els
aeab0 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64  e{.    assert( d
aeac0 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54  esiredEnc==SQLIT
aead0 45 5f 55 54 46 38 20 29 3b 0a 20 20 20 20 69 66  E_UTF8 );.    if
aeae0 28 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c  ( pMem->enc==SQL
aeaf0 49 54 45 5f 55 54 46 31 36 4c 45 20 29 7b 0a 20  ITE_UTF16LE ){. 
aeb00 20 20 20 20 20 2f 2a 20 55 54 46 2d 31 36 20 4c       /* UTF-16 L
aeb10 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 2d 3e 20  ittle-endian -> 
aeb20 55 54 46 2d 38 20 2a 2f 0a 20 20 20 20 20 20 77  UTF-8 */.      w
aeb30 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20  hile( zIn<zTerm 
aeb40 29 7b 0a 20 20 20 20 20 20 20 20 52 45 41 44 5f  ){.        READ_
aeb50 55 54 46 31 36 4c 45 28 7a 49 6e 2c 20 63 29 3b  UTF16LE(zIn, c);
aeb60 20 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f   .        WRITE_
aeb70 55 54 46 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20  UTF8(z, c);.    
aeb80 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
aeb90 20 20 20 20 20 2f 2a 20 55 54 46 2d 31 36 20 42       /* UTF-16 B
aeba0 69 67 2d 65 6e 64 69 61 6e 20 2d 3e 20 55 54 46  ig-endian -> UTF
aebb0 2d 38 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c  -8 */.      whil
aebc0 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a  e( zIn<zTerm ){.
aebd0 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 46          READ_UTF
aebe0 31 36 42 45 28 7a 49 6e 2c 20 63 29 3b 20 0a 20  16BE(zIn, c); . 
aebf0 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 54 46         WRITE_UTF
aec00 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20 7d  8(z, c);.      }
aec10 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d  .    }.    pMem-
aec20 3e 6e 20 3d 20 28 69 6e 74 29 28 7a 20 2d 20 7a  >n = (int)(z - z
aec30 4f 75 74 29 3b 0a 20 20 7d 0a 20 20 2a 7a 20 3d  Out);.  }.  *z =
aec40 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70   0;.  assert( (p
aec50 4d 65 6d 2d 3e 6e 2b 28 64 65 73 69 72 65 64 45  Mem->n+(desiredE
aec60 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f  nc==SQLITE_UTF8?
aec70 31 3a 32 29 29 3c 3d 6c 65 6e 20 29 3b 0a 0a 20  1:2))<=len );.. 
aec80 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
aec90 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20  elease(pMem);.  
aeca0 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 7e  pMem->flags &= ~
aecb0 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f  (MEM_Static|MEM_
aecc0 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a  Dyn|MEM_Ephem);.
aecd0 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 64 65    pMem->enc = de
aece0 73 69 72 65 64 45 6e 63 3b 0a 20 20 70 4d 65 6d  siredEnc;.  pMem
aecf0 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 4d 45 4d 5f  ->flags |= (MEM_
aed00 54 65 72 6d 7c 4d 45 4d 5f 44 79 6e 29 3b 0a 20  Term|MEM_Dyn);. 
aed10 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72   pMem->z = (char
aed20 2a 29 7a 4f 75 74 3b 0a 20 20 70 4d 65 6d 2d 3e  *)zOut;.  pMem->
aed30 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d 2d 3e  zMalloc = pMem->
aed40 7a 3b 0a 0a 74 72 61 6e 73 6c 61 74 65 5f 6f 75  z;..translate_ou
aed50 74 3a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54  t:.#if defined(T
aed60 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 29 20  RANSLATE_TRACE) 
aed70 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  && defined(SQLIT
aed80 45 5f 44 45 42 55 47 29 0a 20 20 7b 0a 20 20 20  E_DEBUG).  {.   
aed90 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b   char zBuf[100];
aeda0 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
aedb0 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74 28 70  MemPrettyPrint(p
aedc0 4d 65 6d 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20  Mem, zBuf);.    
aedd0 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20  fprintf(stderr, 
aede0 22 4f 55 54 50 55 54 3a 20 25 73 5c 6e 22 2c 20  "OUTPUT: %s\n", 
aedf0 7a 42 75 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  zBuf);.  }.#endi
aee00 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  f.  return SQLIT
aee10 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  E_OK;.}../*.** T
aee20 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63  his routine chec
aee30 6b 73 20 66 6f 72 20 61 20 62 79 74 65 2d 6f 72  ks for a byte-or
aee40 64 65 72 20 6d 61 72 6b 20 61 74 20 74 68 65 20  der mark at the 
aee50 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65  beginning of the
aee60 20 0a 2a 2a 20 55 54 46 2d 31 36 20 73 74 72 69   .** UTF-16 stri
aee70 6e 67 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d  ng stored in *pM
aee80 65 6d 2e 20 49 66 20 6f 6e 65 20 69 73 20 70 72  em. If one is pr
aee90 65 73 65 6e 74 2c 20 69 74 20 69 73 20 72 65 6d  esent, it is rem
aeea0 6f 76 65 64 20 61 6e 64 0a 2a 2a 20 74 68 65 20  oved and.** the 
aeeb0 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20  encoding of the 
aeec0 4d 65 6d 20 61 64 6a 75 73 74 65 64 2e 20 54 68  Mem adjusted. Th
aeed0 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20  is routine does 
aeee0 6e 6f 74 20 64 6f 20 61 6e 79 0a 2a 2a 20 62 79  not do any.** by
aeef0 74 65 2d 73 77 61 70 70 69 6e 67 2c 20 69 74 20  te-swapping, it 
aef00 6a 75 73 74 20 73 65 74 73 20 4d 65 6d 2e 65 6e  just sets Mem.en
aef10 63 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e  c appropriately.
aef20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 6c 6c 6f 63  .**.** The alloc
aef30 61 74 69 6f 6e 20 28 73 74 61 74 69 63 2c 20 64  ation (static, d
aef40 79 6e 61 6d 69 63 20 65 74 63 2e 29 20 61 6e 64  ynamic etc.) and
aef50 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65   encoding of the
aef60 20 4d 65 6d 20 6d 61 79 20 62 65 0a 2a 2a 20 63   Mem may be.** c
aef70 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 66  hanged by this f
aef80 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49  unction..*/.SQLI
aef90 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
aefa0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 48 61 6e  qlite3VdbeMemHan
aefb0 64 6c 65 42 6f 6d 28 4d 65 6d 20 2a 70 4d 65 6d  dleBom(Mem *pMem
aefc0 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
aefd0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 62 6f  LITE_OK;.  u8 bo
aefe0 6d 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74  m = 0;..  assert
aeff0 28 20 70 4d 65 6d 2d 3e 6e 3e 3d 30 20 29 3b 0a  ( pMem->n>=0 );.
af000 20 20 69 66 28 20 70 4d 65 6d 2d 3e 6e 3e 31 20    if( pMem->n>1 
af010 29 7b 0a 20 20 20 20 75 38 20 62 31 20 3d 20 2a  ){.    u8 b1 = *
af020 28 75 38 20 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20  (u8 *)pMem->z;. 
af030 20 20 20 75 38 20 62 32 20 3d 20 2a 28 28 28 75     u8 b2 = *(((u
af040 38 20 2a 29 70 4d 65 6d 2d 3e 7a 29 20 2b 20 31  8 *)pMem->z) + 1
af050 29 3b 0a 20 20 20 20 69 66 28 20 62 31 3d 3d 30  );.    if( b1==0
af060 78 46 45 20 26 26 20 62 32 3d 3d 30 78 46 46 20  xFE && b2==0xFF 
af070 29 7b 0a 20 20 20 20 20 20 62 6f 6d 20 3d 20 53  ){.      bom = S
af080 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3b 0a 20  QLITE_UTF16BE;. 
af090 20 20 20 7d 0a 20 20 20 20 69 66 28 20 62 31 3d     }.    if( b1=
af0a0 3d 30 78 46 46 20 26 26 20 62 32 3d 3d 30 78 46  =0xFF && b2==0xF
af0b0 45 20 29 7b 0a 20 20 20 20 20 20 62 6f 6d 20 3d  E ){.      bom =
af0c0 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 3b   SQLITE_UTF16LE;
af0d0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20  .    }.  }.  .  
af0e0 69 66 28 20 62 6f 6d 20 29 7b 0a 20 20 20 20 72  if( bom ){.    r
af0f0 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  c = sqlite3VdbeM
af100 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28  emMakeWriteable(
af110 70 4d 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 72  pMem);.    if( r
af120 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
af130 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 2d 3d        pMem->n -=
af140 20 32 3b 0a 20 20 20 20 20 20 6d 65 6d 6d 6f 76   2;.      memmov
af150 65 28 70 4d 65 6d 2d 3e 7a 2c 20 26 70 4d 65 6d  e(pMem->z, &pMem
af160 2d 3e 7a 5b 32 5d 2c 20 70 4d 65 6d 2d 3e 6e 29  ->z[2], pMem->n)
af170 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b  ;.      pMem->z[
af180 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 27 5c 30 27 3b  pMem->n] = '\0';
af190 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70  .      pMem->z[p
af1a0 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 27 5c 30 27  Mem->n+1] = '\0'
af1b0 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c  ;.      pMem->fl
af1c0 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b  ags |= MEM_Term;
af1d0 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63  .      pMem->enc
af1e0 20 3d 20 62 6f 6d 3b 0a 20 20 20 20 7d 0a 20 20   = bom;.    }.  
af1f0 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
af200 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
af210 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a  E_OMIT_UTF16 */.
af220 0a 2f 2a 0a 2a 2a 20 70 5a 20 69 73 20 61 20 55  ./*.** pZ is a U
af230 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 75 6e 69  TF-8 encoded uni
af240 63 6f 64 65 20 73 74 72 69 6e 67 2e 20 49 66 20  code string. If 
af250 6e 42 79 74 65 20 69 73 20 6c 65 73 73 20 74 68  nByte is less th
af260 61 6e 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65 74 75  an zero,.** retu
af270 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
af280 20 75 6e 69 63 6f 64 65 20 63 68 61 72 61 63 74   unicode charact
af290 65 72 73 20 69 6e 20 70 5a 20 75 70 20 74 6f 20  ers in pZ up to 
af2a0 28 62 75 74 20 6e 6f 74 20 69 6e 63 6c 75 64 69  (but not includi
af2b0 6e 67 29 0a 2a 2a 20 74 68 65 20 66 69 72 73 74  ng).** the first
af2c0 20 30 78 30 30 20 62 79 74 65 2e 20 49 66 20 6e   0x00 byte. If n
af2d0 42 79 74 65 20 69 73 20 6e 6f 74 20 6c 65 73 73  Byte is not less
af2e0 20 74 68 61 6e 20 7a 65 72 6f 2c 20 72 65 74 75   than zero, retu
af2f0 72 6e 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72  rn the.** number
af300 20 6f 66 20 75 6e 69 63 6f 64 65 20 63 68 61 72   of unicode char
af310 61 63 74 65 72 73 20 69 6e 20 74 68 65 20 66 69  acters in the fi
af320 72 73 74 20 6e 42 79 74 65 20 6f 66 20 70 5a 20  rst nByte of pZ 
af330 28 6f 72 20 75 70 20 74 6f 20 0a 2a 2a 20 74 68  (or up to .** th
af340 65 20 66 69 72 73 74 20 30 78 30 30 2c 20 77 68  e first 0x00, wh
af350 69 63 68 65 76 65 72 20 63 6f 6d 65 73 20 66 69  ichever comes fi
af360 72 73 74 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rst)..*/.SQLITE_
af370 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
af380 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 63  te3Utf8CharLen(c
af390 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 2c 20  onst char *zIn, 
af3a0 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 69 6e  int nByte){.  in
af3b0 74 20 72 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74  t r = 0;.  const
af3c0 20 75 38 20 2a 7a 20 3d 20 28 63 6f 6e 73 74 20   u8 *z = (const 
af3d0 75 38 2a 29 7a 49 6e 3b 0a 20 20 63 6f 6e 73 74  u8*)zIn;.  const
af3e0 20 75 38 20 2a 7a 54 65 72 6d 3b 0a 20 20 69 66   u8 *zTerm;.  if
af3f0 28 20 6e 42 79 74 65 3e 3d 30 20 29 7b 0a 20 20  ( nByte>=0 ){.  
af400 20 20 7a 54 65 72 6d 20 3d 20 26 7a 5b 6e 42 79    zTerm = &z[nBy
af410 74 65 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  te];.  }else{.  
af420 20 20 7a 54 65 72 6d 20 3d 20 28 63 6f 6e 73 74    zTerm = (const
af430 20 75 38 2a 29 28 2d 31 29 3b 0a 20 20 7d 0a 20   u8*)(-1);.  }. 
af440 20 61 73 73 65 72 74 28 20 7a 3c 3d 7a 54 65 72   assert( z<=zTer
af450 6d 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a  m );.  while( *z
af460 21 3d 30 20 26 26 20 7a 3c 7a 54 65 72 6d 20 29  !=0 && z<zTerm )
af470 7b 0a 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49  {.    SQLITE_SKI
af480 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 72  P_UTF8(z);.    r
af490 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ++;.  }.  return
af4a0 20 72 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 74   r;.}../* This t
af4b0 65 73 74 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  est function is 
af4c0 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 75 73  not currently us
af4d0 65 64 20 62 79 20 74 68 65 20 61 75 74 6f 6d 61  ed by the automa
af4e0 74 65 64 20 74 65 73 74 2d 73 75 69 74 65 2e 20  ted test-suite. 
af4f0 0a 2a 2a 20 48 65 6e 63 65 20 69 74 20 69 73 20  .** Hence it is 
af500 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69  only available i
af510 6e 20 64 65 62 75 67 20 62 75 69 6c 64 73 2e 0a  n debug builds..
af520 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  */.#if defined(S
af530 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26 20 64  QLITE_TEST) && d
af540 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
af550 42 55 47 29 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73  BUG)./*.** Trans
af560 6c 61 74 65 20 55 54 46 2d 38 20 74 6f 20 55 54  late UTF-8 to UT
af570 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  F-8..**.** This 
af580 68 61 73 20 74 68 65 20 65 66 66 65 63 74 20 6f  has the effect o
af590 66 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 74 68  f making sure th
af5a0 61 74 20 74 68 65 20 73 74 72 69 6e 67 20 69 73  at the string is
af5b0 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a 20   well-formed.** 
af5c0 55 54 46 2d 38 2e 20 20 4d 69 73 63 6f 64 65 64  UTF-8.  Miscoded
af5d0 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65 20   characters are 
af5e0 72 65 6d 6f 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  removed..**.** T
af5f0 68 65 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 69  he translation i
af600 73 20 64 6f 6e 65 20 69 6e 2d 70 6c 61 63 65 20  s done in-place 
af610 28 73 69 6e 63 65 20 69 74 20 69 73 20 69 6d 70  (since it is imp
af620 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 68 65 0a  ossible for the.
af630 2a 2a 20 63 6f 72 72 65 63 74 20 55 54 46 2d 38  ** correct UTF-8
af640 20 65 6e 63 6f 64 69 6e 67 20 74 6f 20 62 65 20   encoding to be 
af650 6c 6f 6e 67 65 72 20 74 68 61 6e 20 61 20 6d 61  longer than a ma
af660 6c 66 6f 72 6d 65 64 20 65 6e 63 6f 64 69 6e 67  lformed encoding
af670 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
af680 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
af690 55 74 66 38 54 6f 38 28 75 6e 73 69 67 6e 65 64  Utf8To8(unsigned
af6a0 20 63 68 61 72 20 2a 7a 49 6e 29 7b 0a 20 20 75   char *zIn){.  u
af6b0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 4f  nsigned char *zO
af6c0 75 74 20 3d 20 7a 49 6e 3b 0a 20 20 75 6e 73 69  ut = zIn;.  unsi
af6d0 67 6e 65 64 20 63 68 61 72 20 2a 7a 53 74 61 72  gned char *zStar
af6e0 74 20 3d 20 7a 49 6e 3b 0a 20 20 75 33 32 20 63  t = zIn;.  u32 c
af6f0 3b 0a 0a 20 20 77 68 69 6c 65 28 20 7a 49 6e 5b  ;..  while( zIn[
af700 30 5d 20 29 7b 0a 20 20 20 20 63 20 3d 20 73 71  0] ){.    c = sq
af710 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 49  lite3Utf8Read(zI
af720 6e 2c 20 28 63 6f 6e 73 74 20 75 38 2a 2a 29 26  n, (const u8**)&
af730 7a 49 6e 29 3b 0a 20 20 20 20 69 66 28 20 63 21  zIn);.    if( c!
af740 3d 30 78 66 66 66 64 20 29 7b 0a 20 20 20 20 20  =0xfffd ){.     
af750 20 57 52 49 54 45 5f 55 54 46 38 28 7a 4f 75 74   WRITE_UTF8(zOut
af760 2c 20 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  , c);.    }.  }.
af770 20 20 2a 7a 4f 75 74 20 3d 20 30 3b 0a 20 20 72    *zOut = 0;.  r
af780 65 74 75 72 6e 20 28 69 6e 74 29 28 7a 4f 75 74  eturn (int)(zOut
af790 20 2d 20 7a 53 74 61 72 74 29 3b 0a 7d 0a 23 65   - zStart);.}.#e
af7a0 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
af7b0 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a  LITE_OMIT_UTF16.
af7c0 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20  /*.** Convert a 
af7d0 55 54 46 2d 31 36 20 73 74 72 69 6e 67 20 69 6e  UTF-16 string in
af7e0 20 74 68 65 20 6e 61 74 69 76 65 20 65 6e 63 6f   the native enco
af7f0 64 69 6e 67 20 69 6e 74 6f 20 61 20 55 54 46 2d  ding into a UTF-
af800 38 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 4d 65 6d  8 string..** Mem
af810 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ory to hold the 
af820 55 54 46 2d 38 20 73 74 72 69 6e 67 20 69 73 20  UTF-8 string is 
af830 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71  obtained from sq
af840 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 61 6e 64  lite3_malloc and
af850 20 6d 75 73 74 0a 2a 2a 20 62 65 20 66 72 65 65   must.** be free
af860 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67  d by the calling
af870 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   function..**.**
af880 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65   NULL is returne
af890 64 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e  d if there is an
af8a0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f   allocation erro
af8b0 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  r..*/.SQLITE_PRI
af8c0 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74  VATE char *sqlit
af8d0 65 33 55 74 66 31 36 74 6f 38 28 73 71 6c 69 74  e3Utf16to8(sqlit
af8e0 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 76 6f  e3 *db, const vo
af8f0 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 42 79 74 65  id *z, int nByte
af900 29 7b 0a 20 20 4d 65 6d 20 6d 3b 0a 20 20 6d 65  ){.  Mem m;.  me
af910 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 7a 65  mset(&m, 0, size
af920 6f 66 28 6d 29 29 3b 0a 20 20 6d 2e 64 62 20 3d  of(m));.  m.db =
af930 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56 64   db;.  sqlite3Vd
af940 62 65 4d 65 6d 53 65 74 53 74 72 28 26 6d 2c 20  beMemSetStr(&m, 
af950 7a 2c 20 6e 42 79 74 65 2c 20 53 51 4c 49 54 45  z, nByte, SQLITE
af960 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20 53 51  _UTF16NATIVE, SQ
af970 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
af980 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
af990 65 45 6e 63 6f 64 69 6e 67 28 26 6d 2c 20 53 51  eEncoding(&m, SQ
af9a0 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 69 66  LITE_UTF8);.  if
af9b0 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
af9c0 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ed ){.    sqlite
af9d0 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
af9e0 26 6d 29 3b 0a 20 20 20 20 6d 2e 7a 20 3d 20 30  &m);.    m.z = 0
af9f0 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
afa00 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 54  (m.flags & MEM_T
afa10 65 72 6d 29 21 3d 30 20 7c 7c 20 64 62 2d 3e 6d  erm)!=0 || db->m
afa20 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
afa30 20 61 73 73 65 72 74 28 20 28 6d 2e 66 6c 61 67   assert( (m.flag
afa40 73 20 26 20 4d 45 4d 5f 53 74 72 29 21 3d 30 20  s & MEM_Str)!=0 
afa50 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
afa60 6c 65 64 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  led );.  return 
afa70 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44  (m.flags & MEM_D
afa80 79 6e 29 21 3d 30 20 3f 20 6d 2e 7a 20 3a 20 73  yn)!=0 ? m.z : s
afa90 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64  qlite3DbStrDup(d
afaa0 62 2c 20 6d 2e 7a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  b, m.z);.}../*.*
afab0 2a 20 43 6f 6e 76 65 72 74 20 61 20 55 54 46 2d  * Convert a UTF-
afac0 38 20 73 74 72 69 6e 67 20 74 6f 20 74 68 65 20  8 string to the 
afad0 55 54 46 2d 31 36 20 65 6e 63 6f 64 69 6e 67 20  UTF-16 encoding 
afae0 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72  specified by par
afaf0 61 6d 65 74 65 72 0a 2a 2a 20 65 6e 63 2e 20 41  ameter.** enc. A
afb00 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
afb10 6e 65 77 20 73 74 72 69 6e 67 20 69 73 20 72 65  new string is re
afb20 74 75 72 6e 65 64 2c 20 61 6e 64 20 74 68 65 20  turned, and the 
afb30 76 61 6c 75 65 20 6f 66 20 2a 70 6e 4f 75 74 0a  value of *pnOut.
afb40 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  ** is set to the
afb50 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 72   length of the r
afb60 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69  eturned string i
afb70 6e 20 62 79 74 65 73 2e 20 54 68 65 20 63 61 6c  n bytes. The cal
afb80 6c 20 73 68 6f 75 6c 64 0a 2a 2a 20 61 72 72 61  l should.** arra
afb90 6e 67 65 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69  nge to call sqli
afba0 74 65 33 44 62 46 72 65 65 28 29 20 6f 6e 20 74  te3DbFree() on t
afbb0 68 65 20 72 65 74 75 72 6e 65 64 20 70 6f 69 6e  he returned poin
afbc0 74 65 72 20 77 68 65 6e 20 69 74 20 69 73 0a 2a  ter when it is.*
afbd0 2a 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 65 71 75  * no longer requ
afbe0 69 72 65 64 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20  ired..** .** If 
afbf0 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65  a malloc failure
afc00 20 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69 73   occurs, NULL is
afc10 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68   returned and th
afc20 65 20 64 62 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65  e db.mallocFaile
afc30 64 0a 2a 2a 20 66 6c 61 67 20 73 65 74 2e 0a 2a  d.** flag set..*
afc40 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
afc50 45 4e 41 42 4c 45 5f 53 54 41 54 32 0a 53 51 4c  ENABLE_STAT2.SQL
afc60 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
afc70 20 2a 73 71 6c 69 74 65 33 55 74 66 38 74 6f 31   *sqlite3Utf8to1
afc80 36 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 75  6(sqlite3 *db, u
afc90 38 20 65 6e 63 2c 20 63 68 61 72 20 2a 7a 2c 20  8 enc, char *z, 
afca0 69 6e 74 20 6e 2c 20 69 6e 74 20 2a 70 6e 4f 75  int n, int *pnOu
afcb0 74 29 7b 0a 20 20 4d 65 6d 20 6d 3b 0a 20 20 6d  t){.  Mem m;.  m
afcc0 65 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 7a  emset(&m, 0, siz
afcd0 65 6f 66 28 6d 29 29 3b 0a 20 20 6d 2e 64 62 20  eof(m));.  m.db 
afce0 3d 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56  = db;.  sqlite3V
afcf0 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 6d 2c  dbeMemSetStr(&m,
afd00 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54   z, n, SQLITE_UT
afd10 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  F8, SQLITE_STATI
afd20 43 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  C);.  if( sqlite
afd30 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74  3VdbeMemTranslat
afd40 65 28 26 6d 2c 20 65 6e 63 29 20 29 7b 0a 20 20  e(&m, enc) ){.  
afd50 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61    assert( db->ma
afd60 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
afd70 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
afd80 20 20 61 73 73 65 72 74 28 20 6d 2e 7a 3d 3d 6d    assert( m.z==m
afd90 2e 7a 4d 61 6c 6c 6f 63 20 29 3b 0a 20 20 2a 70  .zMalloc );.  *p
afda0 6e 4f 75 74 20 3d 20 6d 2e 6e 3b 0a 20 20 72 65  nOut = m.n;.  re
afdb0 74 75 72 6e 20 6d 2e 7a 3b 0a 7d 0a 23 65 6e 64  turn m.z;.}.#end
afdc0 69 66 0a 0a 2f 2a 0a 2a 2a 20 70 5a 20 69 73 20  if../*.** pZ is 
afdd0 61 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64  a UTF-16 encoded
afde0 20 75 6e 69 63 6f 64 65 20 73 74 72 69 6e 67 20   unicode string 
afdf0 61 74 20 6c 65 61 73 74 20 6e 43 68 61 72 20 63  at least nChar c
afe00 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e 0a  haracters long..
afe10 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
afe20 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
afe30 20 74 68 65 20 66 69 72 73 74 20 6e 43 68 61 72   the first nChar
afe40 20 75 6e 69 63 6f 64 65 20 63 68 61 72 61 63 74   unicode charact
afe50 65 72 73 0a 2a 2a 20 69 6e 20 70 5a 2e 20 20 6e  ers.** in pZ.  n
afe60 43 68 61 72 20 6d 75 73 74 20 62 65 20 6e 6f 6e  Char must be non
afe70 2d 6e 65 67 61 74 69 76 65 2e 0a 2a 2f 0a 53 51  -negative..*/.SQ
afe80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
afe90 20 73 71 6c 69 74 65 33 55 74 66 31 36 42 79 74   sqlite3Utf16Byt
afea0 65 4c 65 6e 28 63 6f 6e 73 74 20 76 6f 69 64 20  eLen(const void 
afeb0 2a 7a 49 6e 2c 20 69 6e 74 20 6e 43 68 61 72 29  *zIn, int nChar)
afec0 7b 0a 20 20 69 6e 74 20 63 3b 0a 20 20 75 6e 73  {.  int c;.  uns
afed0 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74  igned char const
afee0 20 2a 7a 20 3d 20 7a 49 6e 3b 0a 20 20 69 6e 74   *z = zIn;.  int
afef0 20 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 53 51   n = 0;.  if( SQ
aff00 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45  LITE_UTF16NATIVE
aff10 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45  ==SQLITE_UTF16BE
aff20 20 29 7b 0a 20 20 20 20 2f 2a 20 55 73 69 6e 67   ){.    /* Using
aff30 20 61 6e 20 22 69 66 20 28 53 51 4c 49 54 45 5f   an "if (SQLITE_
aff40 55 54 46 31 36 4e 41 54 49 56 45 3d 3d 53 51 4c  UTF16NATIVE==SQL
aff50 49 54 45 5f 55 54 46 31 36 42 45 29 22 20 63 6f  ITE_UTF16BE)" co
aff60 6e 73 74 72 75 63 74 20 68 65 72 65 0a 20 20 20  nstruct here.   
aff70 20 2a 2a 20 61 6e 64 20 69 6e 20 6f 74 68 65 72   ** and in other
aff80 20 70 61 72 74 73 20 6f 66 20 74 68 69 73 20 66   parts of this f
aff90 69 6c 65 20 6d 65 61 6e 73 20 74 68 61 74 20 61  ile means that a
affa0 74 20 6f 6e 65 20 62 72 61 6e 63 68 20 77 69 6c  t one branch wil
affb0 6c 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 62 65 20  l.    ** not be 
affc0 63 6f 76 65 72 65 64 20 62 79 20 63 6f 76 65 72  covered by cover
affd0 61 67 65 20 74 65 73 74 69 6e 67 20 6f 6e 20 61  age testing on a
affe0 6e 79 20 73 69 6e 67 6c 65 20 68 6f 73 74 2e 20  ny single host. 
afff0 42 75 74 20 63 6f 76 65 72 61 67 65 0a 20 20 20  But coverage.   
b0000 20 2a 2a 20 77 69 6c 6c 20 62 65 20 63 6f 6d 70   ** will be comp
b0010 6c 65 74 65 20 69 66 20 74 68 65 20 74 65 73 74  lete if the test
b0020 73 20 61 72 65 20 72 75 6e 20 6f 6e 20 62 6f 74  s are run on bot
b0030 68 20 61 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61  h a little-endia
b0040 6e 20 61 6e 64 20 0a 20 20 20 20 2a 2a 20 62 69  n and .    ** bi
b0050 67 2d 65 6e 64 69 61 6e 20 68 6f 73 74 2e 20 42  g-endian host. B
b0060 65 63 61 75 73 65 20 62 6f 74 68 20 74 68 65 20  ecause both the 
b0070 55 54 46 31 36 4e 41 54 49 56 45 20 61 6e 64 20  UTF16NATIVE and 
b0080 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 0a 20  SQLITE_UTF16BE. 
b0090 20 20 20 2a 2a 20 6d 61 63 72 6f 73 20 61 72 65     ** macros are
b00a0 20 63 6f 6e 73 74 61 6e 74 20 61 74 20 63 6f 6d   constant at com
b00b0 70 69 6c 65 20 74 69 6d 65 20 74 68 65 20 63 6f  pile time the co
b00c0 6d 70 69 6c 65 72 20 63 61 6e 20 64 65 74 65 72  mpiler can deter
b00d0 6d 69 6e 65 0a 20 20 20 20 2a 2a 20 77 68 69 63  mine.    ** whic
b00e0 68 20 62 72 61 6e 63 68 20 77 69 6c 6c 20 62 65  h branch will be
b00f0 20 66 6f 6c 6c 6f 77 65 64 2e 20 49 74 20 69 73   followed. It is
b0100 20 74 68 65 72 65 66 6f 72 65 20 61 73 73 75 6d   therefore assum
b0110 65 64 20 74 68 61 74 20 6e 6f 20 72 75 6e 74 69  ed that no runti
b0120 6d 65 0a 20 20 20 20 2a 2a 20 70 65 6e 61 6c 74  me.    ** penalt
b0130 79 20 69 73 20 70 61 69 64 20 66 6f 72 20 74 68  y is paid for th
b0140 69 73 20 22 69 66 22 20 73 74 61 74 65 6d 65 6e  is "if" statemen
b0150 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77 68  t..    */.    wh
b0160 69 6c 65 28 20 6e 3c 6e 43 68 61 72 20 29 7b 0a  ile( n<nChar ){.
b0170 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 31 36        READ_UTF16
b0180 42 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20  BE(z, c);.      
b0190 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  n++;.    }.  }el
b01a0 73 65 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 6e  se{.    while( n
b01b0 3c 6e 43 68 61 72 20 29 7b 0a 20 20 20 20 20 20  <nChar ){.      
b01c0 52 45 41 44 5f 55 54 46 31 36 4c 45 28 7a 2c 20  READ_UTF16LE(z, 
b01d0 63 29 3b 0a 20 20 20 20 20 20 6e 2b 2b 3b 0a 20  c);.      n++;. 
b01e0 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
b01f0 6e 20 28 69 6e 74 29 28 7a 2d 28 75 6e 73 69 67  n (int)(z-(unsig
b0200 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  ned char const *
b0210 29 7a 49 6e 29 3b 0a 7d 0a 0a 23 69 66 20 64 65  )zIn);.}..#if de
b0220 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53  fined(SQLITE_TES
b0230 54 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  T)./*.** This ro
b0240 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
b0250 66 72 6f 6d 20 74 68 65 20 54 43 4c 20 74 65 73  from the TCL tes
b0260 74 20 66 75 6e 63 74 69 6f 6e 20 22 74 72 61 6e  t function "tran
b0270 73 6c 61 74 65 5f 73 65 6c 66 74 65 73 74 22 2e  slate_selftest".
b0280 0a 2a 2a 20 49 74 20 63 68 65 63 6b 73 20 74 68  .** It checks th
b0290 61 74 20 74 68 65 20 70 72 69 6d 69 74 69 76 65  at the primitive
b02a0 73 20 66 6f 72 20 73 65 72 69 61 6c 69 7a 69 6e  s for serializin
b02b0 67 20 61 6e 64 20 64 65 73 65 72 69 61 6c 69 7a  g and deserializ
b02c0 69 6e 67 0a 2a 2a 20 63 68 61 72 61 63 74 65 72  ing.** character
b02d0 73 20 69 6e 20 65 61 63 68 20 65 6e 63 6f 64 69  s in each encodi
b02e0 6e 67 20 61 72 65 20 69 6e 76 65 72 73 65 73 20  ng are inverses 
b02f0 6f 66 20 65 61 63 68 20 6f 74 68 65 72 2e 0a 2a  of each other..*
b0300 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
b0310 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 74 66   void sqlite3Utf
b0320 53 65 6c 66 54 65 73 74 28 76 6f 69 64 29 7b 0a  SelfTest(void){.
b0330 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69    unsigned int i
b0340 2c 20 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20  , t;.  unsigned 
b0350 63 68 61 72 20 7a 42 75 66 5b 32 30 5d 3b 0a 20  char zBuf[20];. 
b0360 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
b0370 7a 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 75 6e  z;.  int n;.  un
b0380 73 69 67 6e 65 64 20 69 6e 74 20 63 3b 0a 0a 20  signed int c;.. 
b0390 20 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30   for(i=0; i<0x00
b03a0 31 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20  110000; i++){.  
b03b0 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20    z = zBuf;.    
b03c0 57 52 49 54 45 5f 55 54 46 38 28 7a 2c 20 69 29  WRITE_UTF8(z, i)
b03d0 3b 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 28  ;.    n = (int)(
b03e0 7a 2d 7a 42 75 66 29 3b 0a 20 20 20 20 61 73 73  z-zBuf);.    ass
b03f0 65 72 74 28 20 6e 3e 30 20 26 26 20 6e 3c 3d 34  ert( n>0 && n<=4
b0400 20 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 30   );.    z[0] = 0
b0410 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a  ;.    z = zBuf;.
b0420 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55      c = sqlite3U
b0430 74 66 38 52 65 61 64 28 7a 2c 20 28 63 6f 6e 73  tf8Read(z, (cons
b0440 74 20 75 38 2a 2a 29 26 7a 29 3b 0a 20 20 20 20  t u8**)&z);.    
b0450 74 20 3d 20 69 3b 0a 20 20 20 20 69 66 28 20 69  t = i;.    if( i
b0460 3e 3d 30 78 44 38 30 30 20 26 26 20 69 3c 3d 30  >=0xD800 && i<=0
b0470 78 44 46 46 46 20 29 20 74 20 3d 20 30 78 46 46  xDFFF ) t = 0xFF
b0480 46 44 3b 0a 20 20 20 20 69 66 28 20 28 69 26 30  FD;.    if( (i&0
b0490 78 46 46 46 46 46 46 46 45 29 3d 3d 30 78 46 46  xFFFFFFFE)==0xFF
b04a0 46 45 20 29 20 74 20 3d 20 30 78 46 46 46 44 3b  FE ) t = 0xFFFD;
b04b0 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 3d 3d  .    assert( c==
b04c0 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  t );.    assert(
b04d0 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a   (z-zBuf)==n );.
b04e0 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69    }.  for(i=0; i
b04f0 3c 30 78 30 30 31 31 30 30 30 30 3b 20 69 2b 2b  <0x00110000; i++
b0500 29 7b 0a 20 20 20 20 69 66 28 20 69 3e 3d 30 78  ){.    if( i>=0x
b0510 44 38 30 30 20 26 26 20 69 3c 30 78 45 30 30 30  D800 && i<0xE000
b0520 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
b0530 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57   z = zBuf;.    W
b0540 52 49 54 45 5f 55 54 46 31 36 4c 45 28 7a 2c 20  RITE_UTF16LE(z, 
b0550 69 29 3b 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74  i);.    n = (int
b0560 29 28 7a 2d 7a 42 75 66 29 3b 0a 20 20 20 20 61  )(z-zBuf);.    a
b0570 73 73 65 72 74 28 20 6e 3e 30 20 26 26 20 6e 3c  ssert( n>0 && n<
b0580 3d 34 20 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d  =4 );.    z[0] =
b0590 20 30 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66   0;.    z = zBuf
b05a0 3b 0a 20 20 20 20 52 45 41 44 5f 55 54 46 31 36  ;.    READ_UTF16
b05b0 4c 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 61 73  LE(z, c);.    as
b05c0 73 65 72 74 28 20 63 3d 3d 69 20 29 3b 0a 20 20  sert( c==i );.  
b05d0 20 20 61 73 73 65 72 74 28 20 28 7a 2d 7a 42 75    assert( (z-zBu
b05e0 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a 20 20 66  f)==n );.  }.  f
b05f0 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 31 31  or(i=0; i<0x0011
b0600 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  0000; i++){.    
b0610 69 66 28 20 69 3e 3d 30 78 44 38 30 30 20 26 26  if( i>=0xD800 &&
b0620 20 69 3c 30 78 45 30 30 30 20 29 20 63 6f 6e 74   i<0xE000 ) cont
b0630 69 6e 75 65 3b 0a 20 20 20 20 7a 20 3d 20 7a 42  inue;.    z = zB
b0640 75 66 3b 0a 20 20 20 20 57 52 49 54 45 5f 55 54  uf;.    WRITE_UT
b0650 46 31 36 42 45 28 7a 2c 20 69 29 3b 0a 20 20 20  F16BE(z, i);.   
b0660 20 6e 20 3d 20 28 69 6e 74 29 28 7a 2d 7a 42 75   n = (int)(z-zBu
b0670 66 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  f);.    assert( 
b0680 6e 3e 30 20 26 26 20 6e 3c 3d 34 20 29 3b 0a 20  n>0 && n<=4 );. 
b0690 20 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a 20 20 20     z[0] = 0;.   
b06a0 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 52   z = zBuf;.    R
b06b0 45 41 44 5f 55 54 46 31 36 42 45 28 7a 2c 20 63  EAD_UTF16BE(z, c
b06c0 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 63  );.    assert( c
b06d0 3d 3d 69 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ==i );.    asser
b06e0 74 28 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29  t( (z-zBuf)==n )
b06f0 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f  ;.  }.}.#endif /
b0700 2a 20 53 51 4c 49 54 45 5f 54 45 53 54 20 2a 2f  * SQLITE_TEST */
b0710 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
b0720 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a  E_OMIT_UTF16 */.
b0730 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
b0740 20 45 6e 64 20 6f 66 20 75 74 66 2e 63 20 2a 2a   End of utf.c **
b0750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b0760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b0770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
b0780 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
b0790 20 42 65 67 69 6e 20 66 69 6c 65 20 75 74 69 6c   Begin file util
b07a0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
b07b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b07c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
b07d0 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74  ./*.** 2001 Sept
b07e0 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54  ember 15.**.** T
b07f0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
b0800 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
b0810 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
b0820 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
b0830 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
b0840 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
b0850 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
b0860 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
b0870 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
b0880 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
b0890 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
b08a0 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
b08b0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
b08c0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
b08d0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
b08e0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
b08f0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
b0900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b0910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b0920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b0930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b0940 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 55 74 69 6c 69  *******.** Utili
b0950 74 79 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65  ty functions use
b0960 64 20 74 68 72 6f 75 67 68 6f 75 74 20 73 71 6c  d throughout sql
b0970 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ite..**.** This 
b0980 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 66 75  file contains fu
b0990 6e 63 74 69 6f 6e 73 20 66 6f 72 20 61 6c 6c 6f  nctions for allo
b09a0 63 61 74 69 6e 67 20 6d 65 6d 6f 72 79 2c 20 63  cating memory, c
b09b0 6f 6d 70 61 72 69 6e 67 0a 2a 2a 20 73 74 72 69  omparing.** stri
b09c0 6e 67 73 2c 20 61 6e 64 20 73 74 75 66 66 20 6c  ngs, and stuff l
b09d0 69 6b 65 20 74 68 61 74 2e 0a 2a 2a 0a 2a 2f 0a  ike that..**.*/.
b09e0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41  #ifdef SQLITE_HA
b09f0 56 45 5f 49 53 4e 41 4e 0a 23 20 69 6e 63 6c 75  VE_ISNAN.# inclu
b0a00 64 65 20 3c 6d 61 74 68 2e 68 3e 0a 23 65 6e 64  de <math.h>.#end
b0a10 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e  if../*.** Routin
b0a20 65 20 6e 65 65 64 65 64 20 74 6f 20 73 75 70 70  e needed to supp
b0a30 6f 72 74 20 74 68 65 20 74 65 73 74 63 61 73 65  ort the testcase
b0a40 28 29 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66  () macro..*/.#if
b0a50 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 56 45 52  def SQLITE_COVER
b0a60 41 47 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f  AGE_TEST.SQLITE_
b0a70 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
b0a80 69 74 65 33 43 6f 76 65 72 61 67 65 28 69 6e 74  ite3Coverage(int
b0a90 20 78 29 7b 0a 20 20 73 74 61 74 69 63 20 69 6e   x){.  static in
b0aa0 74 20 64 75 6d 6d 79 20 3d 20 30 3b 0a 20 20 64  t dummy = 0;.  d
b0ab0 75 6d 6d 79 20 2b 3d 20 78 3b 0a 7d 0a 23 65 6e  ummy += x;.}.#en
b0ac0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  dif../*.** Retur
b0ad0 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 66 6c  n true if the fl
b0ae0 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c  oating point val
b0af0 75 65 20 69 73 20 4e 6f 74 20 61 20 4e 75 6d 62  ue is Not a Numb
b0b00 65 72 20 28 4e 61 4e 29 2e 0a 2a 2a 0a 2a 2a 20  er (NaN)..**.** 
b0b10 55 73 65 20 74 68 65 20 6d 61 74 68 20 6c 69 62  Use the math lib
b0b20 72 61 72 79 20 69 73 6e 61 6e 28 29 20 66 75 6e  rary isnan() fun
b0b30 63 74 69 6f 6e 20 69 66 20 63 6f 6d 70 69 6c 65  ction if compile
b0b40 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f 48 41  d with SQLITE_HA
b0b50 56 45 5f 49 53 4e 41 4e 2e 0a 2a 2a 20 4f 74 68  VE_ISNAN..** Oth
b0b60 65 72 77 69 73 65 2c 20 77 65 20 68 61 76 65 20  erwise, we have 
b0b70 6f 75 72 20 6f 77 6e 20 69 6d 70 6c 65 6d 65 6e  our own implemen
b0b80 74 61 74 69 6f 6e 20 74 68 61 74 20 77 6f 72 6b  tation that work
b0b90 73 20 6f 6e 20 6d 6f 73 74 20 73 79 73 74 65 6d  s on most system
b0ba0 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
b0bb0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
b0bc0 49 73 4e 61 4e 28 64 6f 75 62 6c 65 20 78 29 7b  IsNaN(double x){
b0bd0 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 2f 2a 20  .  int rc;   /* 
b0be0 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  The value return
b0bf0 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64   */.#if !defined
b0c00 28 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 53 4e  (SQLITE_HAVE_ISN
b0c10 41 4e 29 0a 20 20 2f 2a 0a 20 20 2a 2a 20 53 79  AN).  /*.  ** Sy
b0c20 73 74 65 6d 73 20 74 68 61 74 20 73 75 70 70 6f  stems that suppo
b0c30 72 74 20 74 68 65 20 69 73 6e 61 6e 28 29 20 6c  rt the isnan() l
b0c40 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 20  ibrary function 
b0c50 73 68 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79 0a  should probably.
b0c60 20 20 2a 2a 20 6d 61 6b 65 20 75 73 65 20 6f 66    ** make use of
b0c70 20 69 74 20 62 79 20 63 6f 6d 70 69 6c 69 6e 67   it by compiling
b0c80 20 77 69 74 68 20 2d 44 53 51 4c 49 54 45 5f 48   with -DSQLITE_H
b0c90 41 56 45 5f 49 53 4e 41 4e 2e 20 20 42 75 74 20  AVE_ISNAN.  But 
b0ca0 77 65 20 68 61 76 65 0a 20 20 2a 2a 20 66 6f 75  we have.  ** fou
b0cb0 6e 64 20 74 68 61 74 20 6d 61 6e 79 20 73 79 73  nd that many sys
b0cc0 74 65 6d 73 20 64 6f 20 6e 6f 74 20 68 61 76 65  tems do not have
b0cd0 20 61 20 77 6f 72 6b 69 6e 67 20 69 73 6e 61 6e   a working isnan
b0ce0 28 29 20 66 75 6e 63 74 69 6f 6e 20 73 6f 0a 20  () function so. 
b0cf0 20 2a 2a 20 74 68 69 73 20 69 6d 70 6c 65 6d 65   ** this impleme
b0d00 6e 74 61 74 69 6f 6e 20 69 73 20 70 72 6f 76 69  ntation is provi
b0d10 64 65 64 20 61 73 20 61 6e 20 61 6c 74 65 72 6e  ded as an altern
b0d20 61 74 69 76 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  ative..  **.  **
b0d30 20 54 68 69 73 20 4e 61 4e 20 74 65 73 74 20 73   This NaN test s
b0d40 6f 6d 65 74 69 6d 65 73 20 66 61 69 6c 73 20 69  ometimes fails i
b0d50 66 20 63 6f 6d 70 69 6c 65 64 20 6f 6e 20 47 43  f compiled on GC
b0d60 43 20 77 69 74 68 20 2d 66 66 61 73 74 2d 6d 61  C with -ffast-ma
b0d70 74 68 2e 0a 20 20 2a 2a 20 4f 6e 20 74 68 65 20  th..  ** On the 
b0d80 6f 74 68 65 72 20 68 61 6e 64 2c 20 74 68 65 20  other hand, the 
b0d90 75 73 65 20 6f 66 20 2d 66 66 61 73 74 2d 6d 61  use of -ffast-ma
b0da0 74 68 20 63 6f 6d 65 73 20 77 69 74 68 20 74 68  th comes with th
b0db0 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a  e following.  **
b0dc0 20 77 61 72 6e 69 6e 67 3a 0a 20 20 2a 2a 0a 20   warning:.  **. 
b0dd0 20 2a 2a 20 20 20 20 20 20 54 68 69 73 20 6f 70   **      This op
b0de0 74 69 6f 6e 20 5b 2d 66 66 61 73 74 2d 6d 61 74  tion [-ffast-mat
b0df0 68 5d 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20  h] should never 
b0e00 62 65 20 74 75 72 6e 65 64 20 6f 6e 20 62 79 20  be turned on by 
b0e10 61 6e 79 0a 20 20 2a 2a 20 20 20 20 20 20 2d 4f  any.  **      -O
b0e20 20 6f 70 74 69 6f 6e 20 73 69 6e 63 65 20 69 74   option since it
b0e30 20 63 61 6e 20 72 65 73 75 6c 74 20 69 6e 20 69   can result in i
b0e40 6e 63 6f 72 72 65 63 74 20 6f 75 74 70 75 74 20  ncorrect output 
b0e50 66 6f 72 20 70 72 6f 67 72 61 6d 73 0a 20 20 2a  for programs.  *
b0e60 2a 20 20 20 20 20 20 77 68 69 63 68 20 64 65 70  *      which dep
b0e70 65 6e 64 20 6f 6e 20 61 6e 20 65 78 61 63 74 20  end on an exact 
b0e80 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
b0e90 66 20 49 45 45 45 20 6f 72 20 49 53 4f 20 0a 20  f IEEE or ISO . 
b0ea0 20 2a 2a 20 20 20 20 20 20 72 75 6c 65 73 2f 73   **      rules/s
b0eb0 70 65 63 69 66 69 63 61 74 69 6f 6e 73 20 66 6f  pecifications fo
b0ec0 72 20 6d 61 74 68 20 66 75 6e 63 74 69 6f 6e 73  r math functions
b0ed0 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 55 6e 64 65  ..  **.  ** Unde
b0ee0 72 20 4d 53 56 43 2c 20 74 68 69 73 20 4e 61 4e  r MSVC, this NaN
b0ef0 20 74 65 73 74 20 6d 61 79 20 66 61 69 6c 20 69   test may fail i
b0f00 66 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20  f compiled with 
b0f10 61 20 66 6c 6f 61 74 69 6e 67 2d 0a 20 20 2a 2a  a floating-.  **
b0f20 20 70 6f 69 6e 74 20 70 72 65 63 69 73 69 6f 6e   point precision
b0f30 20 6d 6f 64 65 20 6f 74 68 65 72 20 74 68 61 6e   mode other than
b0f40 20 2f 66 70 3a 70 72 65 63 69 73 65 2e 20 20 46   /fp:precise.  F
b0f50 72 6f 6d 20 74 68 65 20 4d 53 44 4e 20 0a 20 20  rom the MSDN .  
b0f60 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e  ** documentation
b0f70 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20  :.  **.  **     
b0f80 20 54 68 65 20 63 6f 6d 70 69 6c 65 72 20 5b 77   The compiler [w
b0f90 69 74 68 20 2f 66 70 3a 70 72 65 63 69 73 65 5d  ith /fp:precise]
b0fa0 20 77 69 6c 6c 20 70 72 6f 70 65 72 6c 79 20 68   will properly h
b0fb0 61 6e 64 6c 65 20 63 6f 6d 70 61 72 69 73 6f 6e  andle comparison
b0fc0 73 20 0a 20 20 2a 2a 20 20 20 20 20 20 69 6e 76  s .  **      inv
b0fd0 6f 6c 76 69 6e 67 20 4e 61 4e 2e 20 46 6f 72 20  olving NaN. For 
b0fe0 65 78 61 6d 70 6c 65 2c 20 78 20 21 3d 20 78 20  example, x != x 
b0ff0 65 76 61 6c 75 61 74 65 73 20 74 6f 20 74 72 75  evaluates to tru
b1000 65 20 69 66 20 78 20 69 73 20 4e 61 4e 20 0a 20  e if x is NaN . 
b1010 20 2a 2a 20 20 20 20 20 20 2e 2e 2e 0a 20 20 2a   **      ....  *
b1020 2f 0a 23 69 66 64 65 66 20 5f 5f 46 41 53 54 5f  /.#ifdef __FAST_
b1030 4d 41 54 48 5f 5f 0a 23 20 65 72 72 6f 72 20 53  MATH__.# error S
b1040 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74 20 77  QLite will not w
b1050 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 20 77 69  ork correctly wi
b1060 74 68 20 74 68 65 20 2d 66 66 61 73 74 2d 6d 61  th the -ffast-ma
b1070 74 68 20 6f 70 74 69 6f 6e 20 6f 66 20 47 43 43  th option of GCC
b1080 2e 0a 23 65 6e 64 69 66 0a 20 20 76 6f 6c 61 74  ..#endif.  volat
b1090 69 6c 65 20 64 6f 75 62 6c 65 20 79 20 3d 20 78  ile double y = x
b10a0 3b 0a 20 20 76 6f 6c 61 74 69 6c 65 20 64 6f 75  ;.  volatile dou
b10b0 62 6c 65 20 7a 20 3d 20 79 3b 0a 20 20 72 63 20  ble z = y;.  rc 
b10c0 3d 20 28 79 21 3d 7a 29 3b 0a 23 65 6c 73 65 20  = (y!=z);.#else 
b10d0 20 2f 2a 20 69 66 20 64 65 66 69 6e 65 64 28 53   /* if defined(S
b10e0 51 4c 49 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e  QLITE_HAVE_ISNAN
b10f0 29 20 2a 2f 0a 20 20 72 63 20 3d 20 69 73 6e 61  ) */.  rc = isna
b1100 6e 28 78 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20  n(x);.#endif /* 
b1110 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 53 4e 41  SQLITE_HAVE_ISNA
b1120 4e 20 2a 2f 0a 20 20 74 65 73 74 63 61 73 65 28  N */.  testcase(
b1130 20 72 63 20 29 3b 0a 20 20 72 65 74 75 72 6e 20   rc );.  return 
b1140 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d  rc;.}../*.** Com
b1150 70 75 74 65 20 61 20 73 74 72 69 6e 67 20 6c 65  pute a string le
b1160 6e 67 74 68 20 74 68 61 74 20 69 73 20 6c 69 6d  ngth that is lim
b1170 69 74 65 64 20 74 6f 20 77 68 61 74 20 63 61 6e  ited to what can
b1180 20 62 65 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a   be stored in.**
b1190 20 6c 6f 77 65 72 20 33 30 20 62 69 74 73 20 6f   lower 30 bits o
b11a0 66 20 61 20 33 32 2d 62 69 74 20 73 69 67 6e 65  f a 32-bit signe
b11b0 64 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a  d integer..**.**
b11c0 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   The value retur
b11d0 6e 65 64 20 77 69 6c 6c 20 6e 65 76 65 72 20 62  ned will never b
b11e0 65 20 6e 65 67 61 74 69 76 65 2e 20 20 4e 6f 72  e negative.  Nor
b11f0 20 77 69 6c 6c 20 69 74 20 65 76 65 72 20 62 65   will it ever be
b1200 20 67 72 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e   greater.** than
b1210 20 74 68 65 20 61 63 74 75 61 6c 20 6c 65 6e 67   the actual leng
b1220 74 68 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67  th of the string
b1230 2e 20 20 46 6f 72 20 76 65 72 79 20 6c 6f 6e 67  .  For very long
b1240 20 73 74 72 69 6e 67 73 20 28 67 72 65 61 74 65   strings (greate
b1250 72 0a 2a 2a 20 74 68 61 6e 20 31 47 69 42 29 20  r.** than 1GiB) 
b1260 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  the value return
b1270 65 64 20 6d 69 67 68 74 20 62 65 20 6c 65 73 73  ed might be less
b1280 20 74 68 61 6e 20 74 68 65 20 74 72 75 65 20 73   than the true s
b1290 74 72 69 6e 67 20 6c 65 6e 67 74 68 2e 0a 2a 2f  tring length..*/
b12a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b12b0 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 6c 65  int sqlite3Strle
b12c0 6e 33 30 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  n30(const char *
b12d0 7a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  z){.  const char
b12e0 20 2a 7a 32 20 3d 20 7a 3b 0a 20 20 69 66 28 20   *z2 = z;.  if( 
b12f0 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  z==0 ) return 0;
b1300 0a 20 20 77 68 69 6c 65 28 20 2a 7a 32 20 29 7b  .  while( *z2 ){
b1310 20 7a 32 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72   z2++; }.  retur
b1320 6e 20 30 78 33 66 66 66 66 66 66 66 20 26 20 28  n 0x3fffffff & (
b1330 69 6e 74 29 28 7a 32 20 2d 20 7a 29 3b 0a 7d 0a  int)(z2 - z);.}.
b1340 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 6d  ./*.** Set the m
b1350 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72  ost recent error
b1360 20 63 6f 64 65 20 61 6e 64 20 65 72 72 6f 72 20   code and error 
b1370 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65 20 73  string for the s
b1380 71 6c 69 74 65 0a 2a 2a 20 68 61 6e 64 6c 65 20  qlite.** handle 
b1390 22 64 62 22 2e 20 54 68 65 20 65 72 72 6f 72 20  "db". The error 
b13a0 63 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20 22  code is set to "
b13b0 65 72 72 5f 63 6f 64 65 22 2e 0a 2a 2a 0a 2a 2a  err_code"..**.**
b13c0 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20 4e 55   If it is not NU
b13d0 4c 4c 2c 20 73 74 72 69 6e 67 20 7a 46 6f 72 6d  LL, string zForm
b13e0 61 74 20 73 70 65 63 69 66 69 65 73 20 74 68 65  at specifies the
b13f0 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 65 0a 2a   format of the.*
b1400 2a 20 65 72 72 6f 72 20 73 74 72 69 6e 67 20 69  * error string i
b1410 6e 20 74 68 65 20 73 74 79 6c 65 20 6f 66 20 74  n the style of t
b1420 68 65 20 70 72 69 6e 74 66 20 66 75 6e 63 74 69  he printf functi
b1430 6f 6e 73 3a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ons: The followi
b1440 6e 67 0a 2a 2a 20 66 6f 72 6d 61 74 20 63 68 61  ng.** format cha
b1450 72 61 63 74 65 72 73 20 61 72 65 20 61 6c 6c 6f  racters are allo
b1460 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  wed:.**.**      
b1470 25 73 20 20 20 20 20 20 49 6e 73 65 72 74 20 61  %s      Insert a
b1480 20 73 74 72 69 6e 67 0a 2a 2a 20 20 20 20 20 20   string.**      
b1490 25 7a 20 20 20 20 20 20 41 20 73 74 72 69 6e 67  %z      A string
b14a0 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20   that should be 
b14b0 66 72 65 65 64 20 61 66 74 65 72 20 75 73 65 0a  freed after use.
b14c0 2a 2a 20 20 20 20 20 20 25 64 20 20 20 20 20 20  **      %d      
b14d0 49 6e 73 65 72 74 20 61 6e 20 69 6e 74 65 67 65  Insert an intege
b14e0 72 0a 2a 2a 20 20 20 20 20 20 25 54 20 20 20 20  r.**      %T    
b14f0 20 20 49 6e 73 65 72 74 20 61 20 74 6f 6b 65 6e    Insert a token
b1500 0a 2a 2a 20 20 20 20 20 20 25 53 20 20 20 20 20  .**      %S     
b1510 20 49 6e 73 65 72 74 20 74 68 65 20 66 69 72 73   Insert the firs
b1520 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 53  t element of a S
b1530 72 63 4c 69 73 74 0a 2a 2a 0a 2a 2a 20 7a 46 6f  rcList.**.** zFo
b1540 72 6d 61 74 20 61 6e 64 20 61 6e 79 20 73 74 72  rmat and any str
b1550 69 6e 67 20 74 6f 6b 65 6e 73 20 74 68 61 74 20  ing tokens that 
b1560 66 6f 6c 6c 6f 77 20 69 74 20 61 72 65 20 61 73  follow it are as
b1570 73 75 6d 65 64 20 74 6f 20 62 65 0a 2a 2a 20 65  sumed to be.** e
b1580 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 2e  ncoded in UTF-8.
b1590 0a 2a 2a 0a 2a 2a 20 54 6f 20 63 6c 65 61 72 20  .**.** To clear 
b15a0 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
b15b0 65 72 72 6f 72 20 66 6f 72 20 73 71 6c 69 74 65  error for sqlite
b15c0 20 68 61 6e 64 6c 65 20 22 64 62 22 2c 20 73 71   handle "db", sq
b15d0 6c 69 74 65 33 45 72 72 6f 72 0a 2a 2a 20 73 68  lite3Error.** sh
b15e0 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 77  ould be called w
b15f0 69 74 68 20 65 72 72 5f 63 6f 64 65 20 73 65 74  ith err_code set
b1600 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e   to SQLITE_OK an
b1610 64 20 7a 46 6f 72 6d 61 74 20 73 65 74 0a 2a 2a  d zFormat set.**
b1620 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c   to NULL..*/.SQL
b1630 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
b1640 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 73 71   sqlite3Error(sq
b1650 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 65  lite3 *db, int e
b1660 72 72 5f 63 6f 64 65 2c 20 63 6f 6e 73 74 20 63  rr_code, const c
b1670 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e  har *zFormat, ..
b1680 2e 29 7b 0a 20 20 69 66 28 20 64 62 20 26 26 20  .){.  if( db && 
b1690 28 64 62 2d 3e 70 45 72 72 20 7c 7c 20 28 64 62  (db->pErr || (db
b16a0 2d 3e 70 45 72 72 20 3d 20 73 71 6c 69 74 65 33  ->pErr = sqlite3
b16b0 56 61 6c 75 65 4e 65 77 28 64 62 29 29 21 3d 30  ValueNew(db))!=0
b16c0 29 20 29 7b 0a 20 20 20 20 64 62 2d 3e 65 72 72  ) ){.    db->err
b16d0 43 6f 64 65 20 3d 20 65 72 72 5f 63 6f 64 65 3b  Code = err_code;
b16e0 0a 20 20 20 20 69 66 28 20 7a 46 6f 72 6d 61 74  .    if( zFormat
b16f0 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a   ){.      char *
b1700 7a 3b 0a 20 20 20 20 20 20 76 61 5f 6c 69 73 74  z;.      va_list
b1710 20 61 70 3b 0a 20 20 20 20 20 20 76 61 5f 73 74   ap;.      va_st
b1720 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29  art(ap, zFormat)
b1730 3b 0a 20 20 20 20 20 20 7a 20 3d 20 73 71 6c 69  ;.      z = sqli
b1740 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20  te3VMPrintf(db, 
b1750 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20  zFormat, ap);.  
b1760 20 20 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a      va_end(ap);.
b1770 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c        sqlite3Val
b1780 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70 45 72  ueSetStr(db->pEr
b1790 72 2c 20 2d 31 2c 20 7a 2c 20 53 51 4c 49 54 45  r, -1, z, SQLITE
b17a0 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 44 59  _UTF8, SQLITE_DY
b17b0 4e 41 4d 49 43 29 3b 0a 20 20 20 20 7d 65 6c 73  NAMIC);.    }els
b17c0 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
b17d0 56 61 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e  ValueSetStr(db->
b17e0 70 45 72 72 2c 20 30 2c 20 30 2c 20 53 51 4c 49  pErr, 0, 0, SQLI
b17f0 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f  TE_UTF8, SQLITE_
b1800 53 54 41 54 49 43 29 3b 0a 20 20 20 20 7d 0a 20  STATIC);.    }. 
b1810 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20   }.}../*.** Add 
b1820 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
b1830 20 74 6f 20 70 50 61 72 73 65 2d 3e 7a 45 72 72   to pParse->zErr
b1840 4d 73 67 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e  Msg and incremen
b1850 74 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2e 0a  t pParse->nErr..
b1860 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
b1870 20 66 6f 72 6d 61 74 74 69 6e 67 20 63 68 61 72   formatting char
b1880 61 63 74 65 72 73 20 61 72 65 20 61 6c 6c 6f 77  acters are allow
b1890 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 25  ed:.**.**      %
b18a0 73 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 20  s      Insert a 
b18b0 73 74 72 69 6e 67 0a 2a 2a 20 20 20 20 20 20 25  string.**      %
b18c0 7a 20 20 20 20 20 20 41 20 73 74 72 69 6e 67 20  z      A string 
b18d0 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 66  that should be f
b18e0 72 65 65 64 20 61 66 74 65 72 20 75 73 65 0a 2a  reed after use.*
b18f0 2a 20 20 20 20 20 20 25 64 20 20 20 20 20 20 49  *      %d      I
b1900 6e 73 65 72 74 20 61 6e 20 69 6e 74 65 67 65 72  nsert an integer
b1910 0a 2a 2a 20 20 20 20 20 20 25 54 20 20 20 20 20  .**      %T     
b1920 20 49 6e 73 65 72 74 20 61 20 74 6f 6b 65 6e 0a   Insert a token.
b1930 2a 2a 20 20 20 20 20 20 25 53 20 20 20 20 20 20  **      %S      
b1940 49 6e 73 65 72 74 20 74 68 65 20 66 69 72 73 74  Insert the first
b1950 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 53 72   element of a Sr
b1960 63 4c 69 73 74 0a 2a 2a 0a 2a 2a 20 54 68 69 73  cList.**.** This
b1970 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64   function should
b1980 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 70 6f   be used to repo
b1990 72 74 20 61 6e 79 20 65 72 72 6f 72 20 74 68 61  rt any error tha
b19a0 74 20 6f 63 63 75 72 73 20 77 68 69 6c 73 74 0a  t occurs whilst.
b19b0 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 20 61 6e 20  ** compiling an 
b19c0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 28 69  SQL statement (i
b19d0 2e 65 2e 20 77 69 74 68 69 6e 20 73 71 6c 69 74  .e. within sqlit
b19e0 65 33 5f 70 72 65 70 61 72 65 28 29 29 2e 20 54  e3_prepare()). T
b19f0 68 65 0a 2a 2a 20 6c 61 73 74 20 74 68 69 6e 67  he.** last thing
b1a00 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65   the sqlite3_pre
b1a10 70 61 72 65 28 29 20 66 75 6e 63 74 69 6f 6e 20  pare() function 
b1a20 64 6f 65 73 20 69 73 20 63 6f 70 79 20 74 68 65  does is copy the
b1a30 20 65 72 72 6f 72 0a 2a 2a 20 73 74 6f 72 65 64   error.** stored
b1a40 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f   by this functio
b1a50 6e 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62  n into the datab
b1a60 61 73 65 20 68 61 6e 64 6c 65 20 75 73 69 6e 67  ase handle using
b1a70 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29 2e   sqlite3Error().
b1a80 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 73 71 6c  .** Function sql
b1a90 69 74 65 33 45 72 72 6f 72 28 29 20 73 68 6f 75  ite3Error() shou
b1aa0 6c 64 20 62 65 20 75 73 65 64 20 64 75 72 69 6e  ld be used durin
b1ab0 67 20 73 74 61 74 65 6d 65 6e 74 20 65 78 65 63  g statement exec
b1ac0 75 74 69 6f 6e 0a 2a 2a 20 28 73 71 6c 69 74 65  ution.** (sqlite
b1ad0 33 5f 73 74 65 70 28 29 20 65 74 63 2e 29 2e 0a  3_step() etc.)..
b1ae0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
b1af0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72  E void sqlite3Er
b1b00 72 6f 72 4d 73 67 28 50 61 72 73 65 20 2a 70 50  rorMsg(Parse *pP
b1b10 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72  arse, const char
b1b20 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b   *zFormat, ...){
b1b30 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20  .  va_list ap;. 
b1b40 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
b1b50 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70 50 61  Parse->db;.  pPa
b1b60 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 73  rse->nErr++;.  s
b1b70 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
b1b80 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67   pParse->zErrMsg
b1b90 29 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70  );.  va_start(ap
b1ba0 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 70 50  , zFormat);.  pP
b1bb0 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20  arse->zErrMsg = 
b1bc0 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28  sqlite3VMPrintf(
b1bd0 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29  db, zFormat, ap)
b1be0 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a  ;.  va_end(ap);.
b1bf0 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53    pParse->rc = S
b1c00 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a  QLITE_ERROR;.}..
b1c10 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20  /*.** Clear the 
b1c20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e  error message in
b1c30 20 70 50 61 72 73 65 2c 20 69 66 20 61 6e 79 0a   pParse, if any.
b1c40 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
b1c50 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72  E void sqlite3Er
b1c60 72 6f 72 43 6c 65 61 72 28 50 61 72 73 65 20 2a  rorClear(Parse *
b1c70 70 50 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74  pParse){.  sqlit
b1c80 65 33 44 62 46 72 65 65 28 70 50 61 72 73 65 2d  e3DbFree(pParse-
b1c90 3e 64 62 2c 20 70 50 61 72 73 65 2d 3e 7a 45 72  >db, pParse->zEr
b1ca0 72 4d 73 67 29 3b 0a 20 20 70 50 61 72 73 65 2d  rMsg);.  pParse-
b1cb0 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20  >zErrMsg = 0;.  
b1cc0 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 3d 20 30  pParse->nErr = 0
b1cd0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  ;.}../*.** Conve
b1ce0 72 74 20 61 6e 20 53 51 4c 2d 73 74 79 6c 65 20  rt an SQL-style 
b1cf0 71 75 6f 74 65 64 20 73 74 72 69 6e 67 20 69 6e  quoted string in
b1d00 74 6f 20 61 20 6e 6f 72 6d 61 6c 20 73 74 72 69  to a normal stri
b1d10 6e 67 20 62 79 20 72 65 6d 6f 76 69 6e 67 0a 2a  ng by removing.*
b1d20 2a 20 74 68 65 20 71 75 6f 74 65 20 63 68 61 72  * the quote char
b1d30 61 63 74 65 72 73 2e 20 20 54 68 65 20 63 6f 6e  acters.  The con
b1d40 76 65 72 73 69 6f 6e 20 69 73 20 64 6f 6e 65 20  version is done 
b1d50 69 6e 2d 70 6c 61 63 65 2e 20 20 49 66 20 74 68  in-place.  If th
b1d60 65 0a 2a 2a 20 69 6e 70 75 74 20 64 6f 65 73 20  e.** input does 
b1d70 6e 6f 74 20 62 65 67 69 6e 20 77 69 74 68 20 61  not begin with a
b1d80 20 71 75 6f 74 65 20 63 68 61 72 61 63 74 65 72   quote character
b1d90 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74  , then this rout
b1da0 69 6e 65 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f  ine.** is a no-o
b1db0 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70  p..**.** The inp
b1dc0 75 74 20 73 74 72 69 6e 67 20 6d 75 73 74 20 62  ut string must b
b1dd0 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65  e zero-terminate
b1de0 64 2e 20 20 41 20 6e 65 77 20 7a 65 72 6f 2d 74  d.  A new zero-t
b1df0 65 72 6d 69 6e 61 74 6f 72 0a 2a 2a 20 69 73 20  erminator.** is 
b1e00 61 64 64 65 64 20 74 6f 20 74 68 65 20 64 65 71  added to the deq
b1e10 75 6f 74 65 64 20 73 74 72 69 6e 67 2e 0a 2a 2a  uoted string..**
b1e20 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76  .** The return v
b1e30 61 6c 75 65 20 69 73 20 2d 31 20 69 66 20 6e 6f  alue is -1 if no
b1e40 20 64 65 71 75 6f 74 69 6e 67 20 6f 63 63 75 72   dequoting occur
b1e50 73 20 6f 72 20 74 68 65 20 6c 65 6e 67 74 68 20  s or the length 
b1e60 6f 66 20 74 68 65 0a 2a 2a 20 64 65 71 75 6f 74  of the.** dequot
b1e70 65 64 20 73 74 72 69 6e 67 2c 20 65 78 63 6c 75  ed string, exclu
b1e80 73 69 76 65 20 6f 66 20 74 68 65 20 7a 65 72 6f  sive of the zero
b1e90 20 74 65 72 6d 69 6e 61 74 6f 72 2c 20 69 66 20   terminator, if 
b1ea0 64 65 71 75 6f 74 69 6e 67 20 64 6f 65 73 0a 2a  dequoting does.*
b1eb0 2a 20 6f 63 63 75 72 2e 0a 2a 2a 0a 2a 2a 20 32  * occur..**.** 2
b1ec0 30 30 32 2d 46 65 62 2d 31 34 3a 20 54 68 69 73  002-Feb-14: This
b1ed0 20 72 6f 75 74 69 6e 65 20 69 73 20 65 78 74 65   routine is exte
b1ee0 6e 64 65 64 20 74 6f 20 72 65 6d 6f 76 65 20 4d  nded to remove M
b1ef0 53 2d 41 63 63 65 73 73 20 73 74 79 6c 65 0a 2a  S-Access style.*
b1f00 2a 20 62 72 61 63 6b 65 74 73 20 66 72 6f 6d 20  * brackets from 
b1f10 61 72 6f 75 6e 64 20 69 64 65 6e 74 69 66 65 72  around identifer
b1f20 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a  s.  For example:
b1f30 20 20 22 5b 61 2d 62 2d 63 5d 22 20 62 65 63 6f    "[a-b-c]" beco
b1f40 6d 65 73 0a 2a 2a 20 22 61 2d 62 2d 63 22 2e 0a  mes.** "a-b-c"..
b1f50 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
b1f60 45 20 69 6e 74 20 73 71 6c 69 74 65 33 44 65 71  E int sqlite3Deq
b1f70 75 6f 74 65 28 63 68 61 72 20 2a 7a 29 7b 0a 20  uote(char *z){. 
b1f80 20 63 68 61 72 20 71 75 6f 74 65 3b 0a 20 20 69   char quote;.  i
b1f90 6e 74 20 69 2c 20 6a 3b 0a 20 20 69 66 28 20 7a  nt i, j;.  if( z
b1fa0 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 2d 31 3b  ==0 ) return -1;
b1fb0 0a 20 20 71 75 6f 74 65 20 3d 20 7a 5b 30 5d 3b  .  quote = z[0];
b1fc0 0a 20 20 73 77 69 74 63 68 28 20 71 75 6f 74 65  .  switch( quote
b1fd0 20 29 7b 0a 20 20 20 20 63 61 73 65 20 27 5c 27   ){.    case '\'
b1fe0 27 3a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63  ':  break;.    c
b1ff0 61 73 65 20 27 22 27 3a 20 20 20 62 72 65 61 6b  ase '"':   break
b2000 3b 0a 20 20 20 20 63 61 73 65 20 27 60 27 3a 20  ;.    case '`': 
b2010 20 20 62 72 65 61 6b 3b 20 20 20 20 20 20 20 20    break;        
b2020 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 4d          /* For M
b2030 79 53 51 4c 20 63 6f 6d 70 61 74 69 62 69 6c 69  ySQL compatibili
b2040 74 79 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 27  ty */.    case '
b2050 5b 27 3a 20 20 20 71 75 6f 74 65 20 3d 20 27 5d  [':   quote = ']
b2060 27 3b 20 20 62 72 65 61 6b 3b 20 20 2f 2a 20 46  ';  break;  /* F
b2070 6f 72 20 4d 53 20 53 71 6c 53 65 72 76 65 72 20  or MS SqlServer 
b2080 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 2a 2f  compatibility */
b2090 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20  .    default:   
b20a0 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a   return -1;.  }.
b20b0 20 20 66 6f 72 28 69 3d 31 2c 20 6a 3d 30 3b 20    for(i=1, j=0; 
b20c0 41 4c 57 41 59 53 28 7a 5b 69 5d 29 3b 20 69 2b  ALWAYS(z[i]); i+
b20d0 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 5b 69 5d  +){.    if( z[i]
b20e0 3d 3d 71 75 6f 74 65 20 29 7b 0a 20 20 20 20 20  ==quote ){.     
b20f0 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 71 75 6f   if( z[i+1]==quo
b2100 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 5b  te ){.        z[
b2110 6a 2b 2b 5d 20 3d 20 71 75 6f 74 65 3b 0a 20 20  j++] = quote;.  
b2120 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20        i++;.     
b2130 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
b2140 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
b2150 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
b2160 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20  z[j++] = z[i];. 
b2170 20 20 20 7d 0a 20 20 7d 0a 20 20 7a 5b 6a 5d 20     }.  }.  z[j] 
b2180 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 6a 3b  = 0;.  return j;
b2190 0a 7d 0a 0a 2f 2a 20 43 6f 6e 76 65 6e 69 65 6e  .}../* Convenien
b21a0 74 20 73 68 6f 72 74 2d 68 61 6e 64 20 2a 2f 0a  t short-hand */.
b21b0 23 64 65 66 69 6e 65 20 55 70 70 65 72 54 6f 4c  #define UpperToL
b21c0 6f 77 65 72 20 73 71 6c 69 74 65 33 55 70 70 65  ower sqlite3Uppe
b21d0 72 54 6f 4c 6f 77 65 72 0a 0a 2f 2a 0a 2a 2a 20  rToLower../*.** 
b21e0 53 6f 6d 65 20 73 79 73 74 65 6d 73 20 68 61 76  Some systems hav
b21f0 65 20 73 74 72 69 63 6d 70 28 29 2e 20 20 4f 74  e stricmp().  Ot
b2200 68 65 72 73 20 68 61 76 65 20 73 74 72 63 61 73  hers have strcas
b2210 65 63 6d 70 28 29 2e 20 20 42 65 63 61 75 73 65  ecmp().  Because
b2220 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20  .** there is no 
b2230 63 6f 6e 73 69 73 74 65 6e 63 79 2c 20 77 65 20  consistency, we 
b2240 77 69 6c 6c 20 64 65 66 69 6e 65 20 6f 75 72 20  will define our 
b2250 6f 77 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  own..*/.SQLITE_P
b2260 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
b2270 65 33 53 74 72 49 43 6d 70 28 63 6f 6e 73 74 20  e3StrICmp(const 
b2280 63 68 61 72 20 2a 7a 4c 65 66 74 2c 20 63 6f 6e  char *zLeft, con
b2290 73 74 20 63 68 61 72 20 2a 7a 52 69 67 68 74 29  st char *zRight)
b22a0 7b 0a 20 20 72 65 67 69 73 74 65 72 20 75 6e 73  {.  register uns
b22b0 69 67 6e 65 64 20 63 68 61 72 20 2a 61 2c 20 2a  igned char *a, *
b22c0 62 3b 0a 20 20 61 20 3d 20 28 75 6e 73 69 67 6e  b;.  a = (unsign
b22d0 65 64 20 63 68 61 72 20 2a 29 7a 4c 65 66 74 3b  ed char *)zLeft;
b22e0 0a 20 20 62 20 3d 20 28 75 6e 73 69 67 6e 65 64  .  b = (unsigned
b22f0 20 63 68 61 72 20 2a 29 7a 52 69 67 68 74 3b 0a   char *)zRight;.
b2300 20 20 77 68 69 6c 65 28 20 2a 61 21 3d 30 20 26    while( *a!=0 &
b2310 26 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a  & UpperToLower[*
b2320 61 5d 3d 3d 55 70 70 65 72 54 6f 4c 6f 77 65 72  a]==UpperToLower
b2330 5b 2a 62 5d 29 7b 20 61 2b 2b 3b 20 62 2b 2b 3b  [*b]){ a++; b++;
b2340 20 7d 0a 20 20 72 65 74 75 72 6e 20 55 70 70 65   }.  return Uppe
b2350 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d 20 2d 20 55  rToLower[*a] - U
b2360 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 3b  pperToLower[*b];
b2370 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  .}.SQLITE_API in
b2380 74 20 73 71 6c 69 74 65 33 5f 73 74 72 6e 69 63  t sqlite3_strnic
b2390 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  mp(const char *z
b23a0 4c 65 66 74 2c 20 63 6f 6e 73 74 20 63 68 61 72  Left, const char
b23b0 20 2a 7a 52 69 67 68 74 2c 20 69 6e 74 20 4e 29   *zRight, int N)
b23c0 7b 0a 20 20 72 65 67 69 73 74 65 72 20 75 6e 73  {.  register uns
b23d0 69 67 6e 65 64 20 63 68 61 72 20 2a 61 2c 20 2a  igned char *a, *
b23e0 62 3b 0a 20 20 61 20 3d 20 28 75 6e 73 69 67 6e  b;.  a = (unsign
b23f0 65 64 20 63 68 61 72 20 2a 29 7a 4c 65 66 74 3b  ed char *)zLeft;
b2400 0a 20 20 62 20 3d 20 28 75 6e 73 69 67 6e 65 64  .  b = (unsigned
b2410 20 63 68 61 72 20 2a 29 7a 52 69 67 68 74 3b 0a   char *)zRight;.
b2420 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 3e 20 30    while( N-- > 0
b2430 20 26 26 20 2a 61 21 3d 30 20 26 26 20 55 70 70   && *a!=0 && Upp
b2440 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d 3d 3d 55  erToLower[*a]==U
b2450 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 29  pperToLower[*b])
b2460 7b 20 61 2b 2b 3b 20 62 2b 2b 3b 20 7d 0a 20 20  { a++; b++; }.  
b2470 72 65 74 75 72 6e 20 4e 3c 30 20 3f 20 30 20 3a  return N<0 ? 0 :
b2480 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61   UpperToLower[*a
b2490 5d 20 2d 20 55 70 70 65 72 54 6f 4c 6f 77 65 72  ] - UpperToLower
b24a0 5b 2a 62 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  [*b];.}../*.** R
b24b0 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 7a 20  eturn TRUE if z 
b24c0 69 73 20 61 20 70 75 72 65 20 6e 75 6d 65 72 69  is a pure numeri
b24d0 63 20 73 74 72 69 6e 67 2e 20 20 52 65 74 75 72  c string.  Retur
b24e0 6e 20 46 41 4c 53 45 20 61 6e 64 20 6c 65 61 76  n FALSE and leav
b24f0 65 0a 2a 2a 20 2a 72 65 61 6c 6e 75 6d 20 75 6e  e.** *realnum un
b2500 63 68 61 6e 67 65 64 20 69 66 20 74 68 65 20 73  changed if the s
b2510 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 73 20 61  tring contains a
b2520 6e 79 20 63 68 61 72 61 63 74 65 72 20 77 68 69  ny character whi
b2530 63 68 20 69 73 20 6e 6f 74 0a 2a 2a 20 70 61 72  ch is not.** par
b2540 74 20 6f 66 20 61 20 6e 75 6d 62 65 72 2e 0a 2a  t of a number..*
b2550 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 72 69  *.** If the stri
b2560 6e 67 20 69 73 20 70 75 72 65 20 6e 75 6d 65 72  ng is pure numer
b2570 69 63 2c 20 73 65 74 20 2a 72 65 61 6c 6e 75 6d  ic, set *realnum
b2580 20 74 6f 20 54 52 55 45 20 69 66 20 74 68 65 20   to TRUE if the 
b2590 73 74 72 69 6e 67 0a 2a 2a 20 63 6f 6e 74 61 69  string.** contai
b25a0 6e 73 20 74 68 65 20 27 2e 27 20 63 68 61 72 61  ns the '.' chara
b25b0 63 74 65 72 20 6f 72 20 61 6e 20 22 45 2b 30 30  cter or an "E+00
b25c0 30 22 20 73 74 79 6c 65 20 65 78 70 6f 6e 65 6e  0" style exponen
b25d0 74 69 61 74 69 6f 6e 20 73 75 66 66 69 78 2e 0a  tiation suffix..
b25e0 2a 2a 20 4f 74 68 65 72 77 69 73 65 20 73 65 74  ** Otherwise set
b25f0 20 2a 72 65 61 6c 6e 75 6d 20 74 6f 20 46 41 4c   *realnum to FAL
b2600 53 45 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 6a  SE.  Note that j
b2610 75 73 74 20 62 65 63 61 75 65 20 2a 72 65 61 6c  ust becaue *real
b2620 6e 75 6d 20 69 73 0a 2a 2a 20 66 61 6c 73 65 20  num is.** false 
b2630 64 6f 65 73 20 6e 6f 74 20 6d 65 61 6e 20 74 68  does not mean th
b2640 61 74 20 74 68 65 20 6e 75 6d 62 65 72 20 63 61  at the number ca
b2650 6e 20 62 65 20 73 75 63 63 65 73 73 66 75 6c 6c  n be successfull
b2660 79 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f  y converted into
b2670 0a 2a 2a 20 61 6e 20 69 6e 74 65 67 65 72 20 2d  .** an integer -
b2680 20 69 74 20 6d 69 67 68 74 20 62 65 20 74 6f 6f   it might be too
b2690 20 62 69 67 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65   big..**.** An e
b26a0 6d 70 74 79 20 73 74 72 69 6e 67 20 69 73 20 63  mpty string is c
b26b0 6f 6e 73 69 64 65 72 65 64 20 6e 6f 6e 2d 6e 75  onsidered non-nu
b26c0 6d 65 72 69 63 2e 0a 2a 2f 0a 53 51 4c 49 54 45  meric..*/.SQLITE
b26d0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
b26e0 69 74 65 33 49 73 4e 75 6d 62 65 72 28 63 6f 6e  ite3IsNumber(con
b26f0 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20  st char *z, int 
b2700 2a 72 65 61 6c 6e 75 6d 2c 20 75 38 20 65 6e 63  *realnum, u8 enc
b2710 29 7b 0a 20 20 69 6e 74 20 69 6e 63 72 20 3d 20  ){.  int incr = 
b2720 28 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46  (enc==SQLITE_UTF
b2730 38 3f 31 3a 32 29 3b 0a 20 20 69 66 28 20 65 6e  8?1:2);.  if( en
b2740 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42  c==SQLITE_UTF16B
b2750 45 20 29 20 7a 2b 2b 3b 0a 20 20 69 66 28 20 2a  E ) z++;.  if( *
b2760 7a 3d 3d 27 2d 27 20 7c 7c 20 2a 7a 3d 3d 27 2b  z=='-' || *z=='+
b2770 27 20 29 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20  ' ) z += incr;. 
b2780 20 69 66 28 20 21 73 71 6c 69 74 65 33 49 73 64   if( !sqlite3Isd
b2790 69 67 69 74 28 2a 7a 29 20 29 7b 0a 20 20 20 20  igit(*z) ){.    
b27a0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
b27b0 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 2a 72 65  z += incr;.  *re
b27c0 61 6c 6e 75 6d 20 3d 20 30 3b 0a 20 20 77 68 69  alnum = 0;.  whi
b27d0 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 69 67  le( sqlite3Isdig
b27e0 69 74 28 2a 7a 29 20 29 7b 20 7a 20 2b 3d 20 69  it(*z) ){ z += i
b27f0 6e 63 72 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d  ncr; }.  if( *z=
b2800 3d 27 2e 27 20 29 7b 0a 20 20 20 20 7a 20 2b 3d  ='.' ){.    z +=
b2810 20 69 6e 63 72 3b 0a 20 20 20 20 69 66 28 20 21   incr;.    if( !
b2820 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a  sqlite3Isdigit(*
b2830 7a 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  z) ) return 0;. 
b2840 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65     while( sqlite
b2850 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b 20  3Isdigit(*z) ){ 
b2860 7a 20 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20 20 20  z += incr; }.   
b2870 20 2a 72 65 61 6c 6e 75 6d 20 3d 20 31 3b 0a 20   *realnum = 1;. 
b2880 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 65 27   }.  if( *z=='e'
b2890 20 7c 7c 20 2a 7a 3d 3d 27 45 27 20 29 7b 0a 20   || *z=='E' ){. 
b28a0 20 20 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20     z += incr;.  
b28b0 20 20 69 66 28 20 2a 7a 3d 3d 27 2b 27 20 7c 7c    if( *z=='+' ||
b28c0 20 2a 7a 3d 3d 27 2d 27 20 29 20 7a 20 2b 3d 20   *z=='-' ) z += 
b28d0 69 6e 63 72 3b 0a 20 20 20 20 69 66 28 20 21 73  incr;.    if( !s
b28e0 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a  qlite3Isdigit(*z
b28f0 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  ) ) return 0;.  
b2900 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33    while( sqlite3
b2910 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b 20 7a  Isdigit(*z) ){ z
b2920 20 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20 20 20 20   += incr; }.    
b2930 2a 72 65 61 6c 6e 75 6d 20 3d 20 31 3b 0a 20 20  *realnum = 1;.  
b2940 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a 3d 3d 30  }.  return *z==0
b2950 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  ;.}../*.** The s
b2960 74 72 69 6e 67 20 7a 5b 5d 20 69 73 20 61 6e 20  tring z[] is an 
b2970 41 53 43 49 49 20 72 65 70 72 65 73 65 6e 74 61  ASCII representa
b2980 74 69 6f 6e 20 6f 66 20 61 20 72 65 61 6c 20 6e  tion of a real n
b2990 75 6d 62 65 72 2e 0a 2a 2a 20 43 6f 6e 76 65 72  umber..** Conver
b29a0 74 20 74 68 69 73 20 73 74 72 69 6e 67 20 74 6f  t this string to
b29b0 20 61 20 64 6f 75 62 6c 65 2e 0a 2a 2a 0a 2a 2a   a double..**.**
b29c0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 73   This routine as
b29d0 73 75 6d 65 73 20 74 68 61 74 20 7a 5b 5d 20 72  sumes that z[] r
b29e0 65 61 6c 6c 79 20 69 73 20 61 20 76 61 6c 69 64  eally is a valid
b29f0 20 6e 75 6d 62 65 72 2e 20 20 49 66 20 69 74 0a   number.  If it.
b2a00 2a 2a 20 69 73 20 6e 6f 74 2c 20 74 68 65 20 72  ** is not, the r
b2a10 65 73 75 6c 74 20 69 73 20 75 6e 64 65 66 69 6e  esult is undefin
b2a20 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ed..**.** This r
b2a30 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 69  outine is used i
b2a40 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 6c 69  nstead of the li
b2a50 62 72 61 72 79 20 61 74 6f 66 28 29 20 66 75 6e  brary atof() fun
b2a60 63 74 69 6f 6e 20 62 65 63 61 75 73 65 0a 2a 2a  ction because.**
b2a70 20 74 68 65 20 6c 69 62 72 61 72 79 20 61 74 6f   the library ato
b2a80 66 28 29 20 6d 69 67 68 74 20 77 61 6e 74 20 74  f() might want t
b2a90 6f 20 75 73 65 20 22 2c 22 20 61 73 20 74 68 65  o use "," as the
b2aa0 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 69   decimal point i
b2ab0 6e 73 74 65 61 64 0a 2a 2a 20 6f 66 20 22 2e 22  nstead.** of "."
b2ac0 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 68 6f   depending on ho
b2ad0 77 20 6c 6f 63 61 6c 65 20 69 73 20 73 65 74 2e  w locale is set.
b2ae0 20 20 42 75 74 20 74 68 61 74 20 77 6f 75 6c 64    But that would
b2af0 20 63 61 75 73 65 20 70 72 6f 62 6c 65 6d 73 0a   cause problems.
b2b00 2a 2a 20 66 6f 72 20 53 51 4c 2e 20 20 53 6f 20  ** for SQL.  So 
b2b10 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 77  this routine alw
b2b20 61 79 73 20 75 73 65 73 20 22 2e 22 20 72 65 67  ays uses "." reg
b2b30 61 72 64 6c 65 73 73 20 6f 66 20 6c 6f 63 61 6c  ardless of local
b2b40 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
b2b50 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
b2b60 41 74 6f 46 28 63 6f 6e 73 74 20 63 68 61 72 20  AtoF(const char 
b2b70 2a 7a 2c 20 64 6f 75 62 6c 65 20 2a 70 52 65 73  *z, double *pRes
b2b80 75 6c 74 29 7b 0a 23 69 66 6e 64 65 66 20 53 51  ult){.#ifndef SQ
b2b90 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
b2ba0 4e 47 5f 50 4f 49 4e 54 0a 20 20 63 6f 6e 73 74  NG_POINT.  const
b2bb0 20 63 68 61 72 20 2a 7a 42 65 67 69 6e 20 3d 20   char *zBegin = 
b2bc0 7a 3b 0a 20 20 2f 2a 20 73 69 67 6e 20 2a 20 73  z;.  /* sign * s
b2bd0 69 67 6e 69 66 69 63 61 6e 64 20 2a 20 28 31 30  ignificand * (10
b2be0 20 5e 20 28 65 73 69 67 6e 20 2a 20 65 78 70 6f   ^ (esign * expo
b2bf0 6e 65 6e 74 29 29 20 2a 2f 0a 20 20 69 6e 74 20  nent)) */.  int 
b2c00 73 69 67 6e 20 3d 20 31 3b 20 20 20 2f 2a 20 73  sign = 1;   /* s
b2c10 69 67 6e 20 6f 66 20 73 69 67 6e 69 66 69 63 61  ign of significa
b2c20 6e 64 20 2a 2f 0a 20 20 69 36 34 20 73 20 3d 20  nd */.  i64 s = 
b2c30 30 3b 20 20 20 20 20 20 2f 2a 20 73 69 67 6e 69  0;      /* signi
b2c40 66 69 63 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20  ficand */.  int 
b2c50 64 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 61  d = 0;      /* a
b2c60 64 6a 75 73 74 20 65 78 70 6f 6e 65 6e 74 20 66  djust exponent f
b2c70 6f 72 20 73 68 69 66 74 69 6e 67 20 64 65 63 69  or shifting deci
b2c80 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 69  mal point */.  i
b2c90 6e 74 20 65 73 69 67 6e 20 3d 20 31 3b 20 20 2f  nt esign = 1;  /
b2ca0 2a 20 73 69 67 6e 20 6f 66 20 65 78 70 6f 6e 65  * sign of expone
b2cb0 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 65 20 3d 20  nt */.  int e = 
b2cc0 30 3b 20 20 20 20 20 20 2f 2a 20 65 78 70 6f 6e  0;      /* expon
b2cd0 65 6e 74 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20  ent */.  double 
b2ce0 72 65 73 75 6c 74 3b 0a 20 20 69 6e 74 20 6e 44  result;.  int nD
b2cf0 69 67 69 74 73 20 3d 20 30 3b 0a 0a 20 20 2f 2a  igits = 0;..  /*
b2d00 20 73 6b 69 70 20 6c 65 61 64 69 6e 67 20 73 70   skip leading sp
b2d10 61 63 65 73 20 2a 2f 0a 20 20 77 68 69 6c 65 28  aces */.  while(
b2d20 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28   sqlite3Isspace(
b2d30 2a 7a 29 20 29 20 7a 2b 2b 3b 0a 20 20 2f 2a 20  *z) ) z++;.  /* 
b2d40 67 65 74 20 73 69 67 6e 20 6f 66 20 73 69 67 6e  get sign of sign
b2d50 69 66 69 63 61 6e 64 20 2a 2f 0a 20 20 69 66 28  ificand */.  if(
b2d60 20 2a 7a 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20   *z=='-' ){.    
b2d70 73 69 67 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 7a  sign = -1;.    z
b2d80 2b 2b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  ++;.  }else if( 
b2d90 2a 7a 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 7a  *z=='+' ){.    z
b2da0 2b 2b 3b 0a 20 20 7d 0a 20 20 2f 2a 20 73 6b 69  ++;.  }.  /* ski
b2db0 70 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f 65 73  p leading zeroes
b2dc0 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 7a 5b 30   */.  while( z[0
b2dd0 5d 3d 3d 27 30 27 20 29 20 7a 2b 2b 2c 20 6e 44  ]=='0' ) z++, nD
b2de0 69 67 69 74 73 2b 2b 3b 0a 0a 20 20 2f 2a 20 63  igits++;..  /* c
b2df0 6f 70 79 20 6d 61 78 20 73 69 67 6e 69 66 69 63  opy max signific
b2e00 61 6e 74 20 64 69 67 69 74 73 20 74 6f 20 73 69  ant digits to si
b2e10 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0a 20 20 77  gnificand */.  w
b2e20 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64  hile( sqlite3Isd
b2e30 69 67 69 74 28 2a 7a 29 20 26 26 20 73 3c 28 28  igit(*z) && s<((
b2e40 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 2d 39 29  LARGEST_INT64-9)
b2e50 2f 31 30 29 20 29 7b 0a 20 20 20 20 73 20 3d 20  /10) ){.    s = 
b2e60 73 2a 31 30 20 2b 20 28 2a 7a 20 2d 20 27 30 27  s*10 + (*z - '0'
b2e70 29 3b 0a 20 20 20 20 7a 2b 2b 2c 20 6e 44 69 67  );.    z++, nDig
b2e80 69 74 73 2b 2b 3b 0a 20 20 7d 0a 20 20 2f 2a 20  its++;.  }.  /* 
b2e90 73 6b 69 70 20 6e 6f 6e 2d 73 69 67 6e 69 66 69  skip non-signifi
b2ea0 63 61 6e 74 20 73 69 67 6e 69 66 69 63 61 6e 64  cant significand
b2eb0 20 64 69 67 69 74 73 0a 20 20 2a 2a 20 28 69 6e   digits.  ** (in
b2ec0 63 72 65 61 73 65 20 65 78 70 6f 6e 65 6e 74 20  crease exponent 
b2ed0 62 79 20 64 20 74 6f 20 73 68 69 66 74 20 64 65  by d to shift de
b2ee0 63 69 6d 61 6c 20 6c 65 66 74 29 20 2a 2f 0a 20  cimal left) */. 
b2ef0 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49   while( sqlite3I
b2f00 73 64 69 67 69 74 28 2a 7a 29 20 29 20 7a 2b 2b  sdigit(*z) ) z++
b2f10 2c 20 6e 44 69 67 69 74 73 2b 2b 2c 20 64 2b 2b  , nDigits++, d++
b2f20 3b 0a 0a 20 20 2f 2a 20 69 66 20 64 65 63 69 6d  ;..  /* if decim
b2f30 61 6c 20 70 6f 69 6e 74 20 69 73 20 70 72 65 73  al point is pres
b2f40 65 6e 74 20 2a 2f 0a 20 20 69 66 28 20 2a 7a 3d  ent */.  if( *z=
b2f50 3d 27 2e 27 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b  ='.' ){.    z++;
b2f60 0a 20 20 20 20 2f 2a 20 63 6f 70 79 20 64 69 67  .    /* copy dig
b2f70 69 74 73 20 66 72 6f 6d 20 61 66 74 65 72 20 64  its from after d
b2f80 65 63 69 6d 61 6c 20 74 6f 20 73 69 67 6e 69 66  ecimal to signif
b2f90 69 63 61 6e 64 0a 20 20 20 20 2a 2a 20 28 64 65  icand.    ** (de
b2fa0 63 72 65 61 73 65 20 65 78 70 6f 6e 65 6e 74 20  crease exponent 
b2fb0 62 79 20 64 20 74 6f 20 73 68 69 66 74 20 64 65  by d to shift de
b2fc0 63 69 6d 61 6c 20 72 69 67 68 74 29 20 2a 2f 0a  cimal right) */.
b2fd0 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74      while( sqlit
b2fe0 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 26 26  e3Isdigit(*z) &&
b2ff0 20 73 3c 28 28 4c 41 52 47 45 53 54 5f 49 4e 54   s<((LARGEST_INT
b3000 36 34 2d 39 29 2f 31 30 29 20 29 7b 0a 20 20 20  64-9)/10) ){.   
b3010 20 20 20 73 20 3d 20 73 2a 31 30 20 2b 20 28 2a     s = s*10 + (*
b3020 7a 20 2d 20 27 30 27 29 3b 0a 20 20 20 20 20 20  z - '0');.      
b3030 7a 2b 2b 2c 20 6e 44 69 67 69 74 73 2b 2b 2c 20  z++, nDigits++, 
b3040 64 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f  d--;.    }.    /
b3050 2a 20 73 6b 69 70 20 6e 6f 6e 2d 73 69 67 6e 69  * skip non-signi
b3060 66 69 63 61 6e 74 20 64 69 67 69 74 73 20 2a 2f  ficant digits */
b3070 0a 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69  .    while( sqli
b3080 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29  te3Isdigit(*z) )
b3090 20 7a 2b 2b 2c 20 6e 44 69 67 69 74 73 2b 2b 3b   z++, nDigits++;
b30a0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 69 66 20 65 78  .  }..  /* if ex
b30b0 70 6f 6e 65 6e 74 20 69 73 20 70 72 65 73 65 6e  ponent is presen
b30c0 74 20 2a 2f 0a 20 20 69 66 28 20 2a 7a 3d 3d 27  t */.  if( *z=='
b30d0 65 27 20 7c 7c 20 2a 7a 3d 3d 27 45 27 20 29 7b  e' || *z=='E' ){
b30e0 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 2f 2a  .    z++;.    /*
b30f0 20 67 65 74 20 73 69 67 6e 20 6f 66 20 65 78 70   get sign of exp
b3100 6f 6e 65 6e 74 20 2a 2f 0a 20 20 20 20 69 66 28  onent */.    if(
b3110 20 2a 7a 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20   *z=='-' ){.    
b3120 20 20 65 73 69 67 6e 20 3d 20 2d 31 3b 0a 20 20    esign = -1;.  
b3130 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 65 6c      z++;.    }el
b3140 73 65 20 69 66 28 20 2a 7a 3d 3d 27 2b 27 20 29  se if( *z=='+' )
b3150 7b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20  {.      z++;.   
b3160 20 7d 0a 20 20 20 20 2f 2a 20 63 6f 70 79 20 64   }.    /* copy d
b3170 69 67 69 74 73 20 74 6f 20 65 78 70 6f 6e 65 6e  igits to exponen
b3180 74 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20  t */.    while( 
b3190 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a  sqlite3Isdigit(*
b31a0 7a 29 20 29 7b 0a 20 20 20 20 20 20 65 20 3d 20  z) ){.      e = 
b31b0 65 2a 31 30 20 2b 20 28 2a 7a 20 2d 20 27 30 27  e*10 + (*z - '0'
b31c0 29 3b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20  );.      z++;.  
b31d0 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 61 64    }.  }..  /* ad
b31e0 6a 75 73 74 20 65 78 70 6f 6e 65 6e 74 20 62 79  just exponent by
b31f0 20 64 2c 20 61 6e 64 20 75 70 64 61 74 65 20 73   d, and update s
b3200 69 67 6e 20 2a 2f 0a 20 20 65 20 3d 20 28 65 2a  ign */.  e = (e*
b3210 65 73 69 67 6e 29 20 2b 20 64 3b 0a 20 20 69 66  esign) + d;.  if
b3220 28 20 65 3c 30 20 29 20 7b 0a 20 20 20 20 65 73  ( e<0 ) {.    es
b3230 69 67 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 65 20  ign = -1;.    e 
b3240 2a 3d 20 2d 31 3b 0a 20 20 7d 20 65 6c 73 65 20  *= -1;.  } else 
b3250 7b 0a 20 20 20 20 65 73 69 67 6e 20 3d 20 31 3b  {.    esign = 1;
b3260 0a 20 20 7d 0a 0a 20 20 2f 2a 20 69 66 20 30 20  .  }..  /* if 0 
b3270 73 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0a 20  significand */. 
b3280 20 69 66 28 20 21 73 20 29 20 7b 0a 20 20 20 20   if( !s ) {.    
b3290 2f 2a 20 49 6e 20 74 68 65 20 49 45 45 45 20 37  /* In the IEEE 7
b32a0 35 34 20 73 74 61 6e 64 61 72 64 2c 20 7a 65 72  54 standard, zer
b32b0 6f 20 69 73 20 73 69 67 6e 65 64 2e 0a 20 20 20  o is signed..   
b32c0 20 2a 2a 20 41 64 64 20 74 68 65 20 73 69 67 6e   ** Add the sign
b32d0 20 69 66 20 77 65 27 76 65 20 73 65 65 6e 20 61   if we've seen a
b32e0 74 20 6c 65 61 73 74 20 6f 6e 65 20 64 69 67 69  t least one digi
b32f0 74 20 2a 2f 0a 20 20 20 20 72 65 73 75 6c 74 20  t */.    result 
b3300 3d 20 28 73 69 67 6e 3c 30 20 26 26 20 6e 44 69  = (sign<0 && nDi
b3310 67 69 74 73 29 20 3f 20 2d 28 64 6f 75 62 6c 65  gits) ? -(double
b3320 29 30 20 3a 20 28 64 6f 75 62 6c 65 29 30 3b 0a  )0 : (double)0;.
b3330 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 2f    } else {.    /
b3340 2a 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 64  * attempt to red
b3350 75 63 65 20 65 78 70 6f 6e 65 6e 74 20 2a 2f 0a  uce exponent */.
b3360 20 20 20 20 69 66 28 20 65 73 69 67 6e 3e 30 20      if( esign>0 
b3370 29 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  ){.      while( 
b3380 73 3c 28 4c 41 52 47 45 53 54 5f 49 4e 54 36 34  s<(LARGEST_INT64
b3390 2f 31 30 29 20 26 26 20 65 3e 30 20 29 20 65 2d  /10) && e>0 ) e-
b33a0 2d 2c 73 2a 3d 31 30 3b 0a 20 20 20 20 7d 65 6c  -,s*=10;.    }el
b33b0 73 65 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 28  se{.      while(
b33c0 20 21 28 73 25 31 30 29 20 26 26 20 65 3e 30 20   !(s%10) && e>0 
b33d0 29 20 65 2d 2d 2c 73 2f 3d 31 30 3b 0a 20 20 20  ) e--,s/=10;.   
b33e0 20 7d 0a 0a 20 20 20 20 2f 2a 20 61 64 6a 75 73   }..    /* adjus
b33f0 74 20 74 68 65 20 73 69 67 6e 20 6f 66 20 73 69  t the sign of si
b3400 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0a 20 20 20  gnificand */.   
b3410 20 73 20 3d 20 73 69 67 6e 3c 30 20 3f 20 2d 73   s = sign<0 ? -s
b3420 20 3a 20 73 3b 0a 0a 20 20 20 20 2f 2a 20 69 66   : s;..    /* if
b3430 20 65 78 70 6f 6e 65 6e 74 2c 20 73 63 61 6c 65   exponent, scale
b3440 20 73 69 67 6e 69 66 69 63 61 6e 64 20 61 73 20   significand as 
b3450 61 70 70 72 6f 70 72 69 61 74 65 0a 20 20 20 20  appropriate.    
b3460 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20 69 6e 20  ** and store in 
b3470 72 65 73 75 6c 74 2e 20 2a 2f 0a 20 20 20 20 69  result. */.    i
b3480 66 28 20 65 20 29 7b 0a 20 20 20 20 20 20 64 6f  f( e ){.      do
b3490 75 62 6c 65 20 73 63 61 6c 65 20 3d 20 31 2e 30  uble scale = 1.0
b34a0 3b 0a 20 20 20 20 20 20 2f 2a 20 61 74 74 65 6d  ;.      /* attem
b34b0 70 74 20 74 6f 20 68 61 6e 64 6c 65 20 65 78 74  pt to handle ext
b34c0 72 65 6d 65 6c 79 20 73 6d 61 6c 6c 2f 6c 61 72  remely small/lar
b34d0 67 65 20 6e 75 6d 62 65 72 73 20 62 65 74 74 65  ge numbers bette
b34e0 72 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 65  r */.      if( e
b34f0 3e 33 30 37 20 26 26 20 65 3c 33 34 32 20 29 7b  >307 && e<342 ){
b3500 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
b3510 65 25 33 30 38 20 29 20 7b 20 73 63 61 6c 65 20  e%308 ) { scale 
b3520 2a 3d 20 31 2e 30 65 2b 31 3b 20 65 20 2d 3d 20  *= 1.0e+1; e -= 
b3530 31 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28  1; }.        if(
b3540 20 65 73 69 67 6e 3c 30 20 29 7b 0a 20 20 20 20   esign<0 ){.    
b3550 20 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20 73        result = s
b3560 20 2f 20 73 63 61 6c 65 3b 0a 20 20 20 20 20 20   / scale;.      
b3570 20 20 20 20 72 65 73 75 6c 74 20 2f 3d 20 31 2e      result /= 1.
b3580 30 65 2b 33 30 38 3b 0a 20 20 20 20 20 20 20 20  0e+308;.        
b3590 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
b35a0 20 72 65 73 75 6c 74 20 3d 20 73 20 2a 20 73 63   result = s * sc
b35b0 61 6c 65 3b 0a 20 20 20 20 20 20 20 20 20 20 72  ale;.          r
b35c0 65 73 75 6c 74 20 2a 3d 20 31 2e 30 65 2b 33 30  esult *= 1.0e+30
b35d0 38 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  8;.        }.   
b35e0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
b35f0 20 20 2f 2a 20 31 2e 30 65 2b 32 32 20 69 73 20    /* 1.0e+22 is 
b3600 74 68 65 20 6c 61 72 67 65 73 74 20 70 6f 77 65  the largest powe
b3610 72 20 6f 66 20 31 30 20 74 68 61 6e 20 63 61 6e  r of 10 than can
b3620 20 62 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20   be .        ** 
b3630 72 65 70 72 65 73 65 6e 74 65 64 20 65 78 61 63  represented exac
b3640 74 6c 79 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  tly. */.        
b3650 77 68 69 6c 65 28 20 65 25 32 32 20 29 20 7b 20  while( e%22 ) { 
b3660 73 63 61 6c 65 20 2a 3d 20 31 2e 30 65 2b 31 3b  scale *= 1.0e+1;
b3670 20 65 20 2d 3d 20 31 3b 20 7d 0a 20 20 20 20 20   e -= 1; }.     
b3680 20 20 20 77 68 69 6c 65 28 20 65 3e 30 20 29 20     while( e>0 ) 
b3690 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e 30 65 2b  { scale *= 1.0e+
b36a0 32 32 3b 20 65 20 2d 3d 20 32 32 3b 20 7d 0a 20  22; e -= 22; }. 
b36b0 20 20 20 20 20 20 20 69 66 28 20 65 73 69 67 6e         if( esign
b36c0 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  <0 ){.          
b36d0 72 65 73 75 6c 74 20 3d 20 73 20 2f 20 73 63 61  result = s / sca
b36e0 6c 65 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  le;.        }els
b36f0 65 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 73  e{.          res
b3700 75 6c 74 20 3d 20 73 20 2a 20 73 63 61 6c 65 3b  ult = s * scale;
b3710 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
b3720 20 7d 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a   }.    } else {.
b3730 20 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20 28        result = (
b3740 64 6f 75 62 6c 65 29 73 3b 0a 20 20 20 20 7d 0a  double)s;.    }.
b3750 20 20 7d 0a 0a 20 20 2f 2a 20 73 74 6f 72 65 20    }..  /* store 
b3760 74 68 65 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20  the result */.  
b3770 2a 70 52 65 73 75 6c 74 20 3d 20 72 65 73 75 6c  *pResult = resul
b3780 74 3b 0a 0a 20 20 2f 2a 20 72 65 74 75 72 6e 20  t;..  /* return 
b3790 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63  number of charac
b37a0 74 65 72 73 20 75 73 65 64 20 2a 2f 0a 20 20 72  ters used */.  r
b37b0 65 74 75 72 6e 20 28 69 6e 74 29 28 7a 20 2d 20  eturn (int)(z - 
b37c0 7a 42 65 67 69 6e 29 3b 0a 23 65 6c 73 65 0a 20  zBegin);.#else. 
b37d0 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41   return sqlite3A
b37e0 74 6f 69 36 34 28 7a 2c 20 70 52 65 73 75 6c 74  toi64(z, pResult
b37f0 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  );.#endif /* SQL
b3800 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e  ITE_OMIT_FLOATIN
b3810 47 5f 50 4f 49 4e 54 20 2a 2f 0a 7d 0a 0a 2f 2a  G_POINT */.}../*
b3820 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20  .** Compare the 
b3830 31 39 2d 63 68 61 72 61 63 74 65 72 20 73 74 72  19-character str
b3840 69 6e 67 20 7a 4e 75 6d 20 61 67 61 69 6e 73 74  ing zNum against
b3850 20 74 68 65 20 74 65 78 74 20 72 65 70 72 65 73   the text repres
b3860 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 76 61 6c 75  entation.** valu
b3870 65 20 32 5e 36 33 3a 20 20 39 32 32 33 33 37 32  e 2^63:  9223372
b3880 30 33 36 38 35 34 37 37 35 38 30 38 2e 20 20 52  036854775808.  R
b3890 65 74 75 72 6e 20 6e 65 67 61 74 69 76 65 2c 20  eturn negative, 
b38a0 7a 65 72 6f 2c 20 6f 72 20 70 6f 73 69 74 69 76  zero, or positiv
b38b0 65 0a 2a 2a 20 69 66 20 7a 4e 75 6d 20 69 73 20  e.** if zNum is 
b38c0 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61 6c  less than, equal
b38d0 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 65 72 20   to, or greater 
b38e0 74 68 61 6e 20 74 68 65 20 73 74 72 69 6e 67 2e  than the string.
b38f0 0a 2a 2a 0a 2a 2a 20 55 6e 6c 69 6b 65 20 6d 65  .**.** Unlike me
b3900 6d 63 6d 70 28 29 20 74 68 69 73 20 72 6f 75 74  mcmp() this rout
b3910 69 6e 65 20 69 73 20 67 75 61 72 61 6e 74 65 65  ine is guarantee
b3920 64 20 74 6f 20 72 65 74 75 72 6e 20 74 68 65 20  d to return the 
b3930 64 69 66 66 65 72 65 6e 63 65 0a 2a 2a 20 69 6e  difference.** in
b3940 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20 74   the values of t
b3950 68 65 20 6c 61 73 74 20 64 69 67 69 74 20 69 66  he last digit if
b3960 20 74 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 72   the only differ
b3970 65 6e 63 65 20 69 73 20 69 6e 20 74 68 65 0a 2a  ence is in the.*
b3980 2a 20 6c 61 73 74 20 64 69 67 69 74 2e 20 20 53  * last digit.  S
b3990 6f 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 0a  o, for example,.
b39a0 2a 2a 0a 2a 2a 20 20 20 20 20 20 63 6f 6d 70 61  **.**      compa
b39b0 72 65 32 70 6f 77 36 33 28 22 39 32 32 33 33 37  re2pow63("922337
b39c0 32 30 33 36 38 35 34 37 37 35 38 30 30 22 29 0a  2036854775800").
b39d0 2a 2a 0a 2a 2a 20 77 69 6c 6c 20 72 65 74 75 72  **.** will retur
b39e0 6e 20 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  n -8..*/.static 
b39f0 69 6e 74 20 63 6f 6d 70 61 72 65 32 70 6f 77 36  int compare2pow6
b3a00 33 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  3(const char *zN
b3a10 75 6d 29 7b 0a 20 20 69 6e 74 20 63 3b 0a 20 20  um){.  int c;.  
b3a20 63 20 3d 20 6d 65 6d 63 6d 70 28 7a 4e 75 6d 2c  c = memcmp(zNum,
b3a30 22 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37  "922337203685477
b3a40 35 38 30 22 2c 31 38 29 2a 31 30 3b 0a 20 20 69  580",18)*10;.  i
b3a50 66 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 63  f( c==0 ){.    c
b3a60 20 3d 20 7a 4e 75 6d 5b 31 38 5d 20 2d 20 27 38   = zNum[18] - '8
b3a70 27 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  ';.  }.  return 
b3a80 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  c;.}.../*.** Ret
b3a90 75 72 6e 20 54 52 55 45 20 69 66 20 7a 4e 75 6d  urn TRUE if zNum
b3aa0 20 69 73 20 61 20 36 34 2d 62 69 74 20 73 69 67   is a 64-bit sig
b3ab0 6e 65 64 20 69 6e 74 65 67 65 72 20 61 6e 64 20  ned integer and 
b3ac0 77 72 69 74 65 0a 2a 2a 20 74 68 65 20 76 61 6c  write.** the val
b3ad0 75 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65  ue of the intege
b3ae0 72 20 69 6e 74 6f 20 2a 70 4e 75 6d 2e 20 20 49  r into *pNum.  I
b3af0 66 20 7a 4e 75 6d 20 69 73 20 6e 6f 74 20 61 6e  f zNum is not an
b3b00 20 69 6e 74 65 67 65 72 0a 2a 2a 20 6f 72 20 69   integer.** or i
b3b10 73 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61  s an integer tha
b3b20 74 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 74  t is too large t
b3b30 6f 20 62 65 20 65 78 70 72 65 73 73 65 64 20 77  o be expressed w
b3b40 69 74 68 20 36 34 20 62 69 74 73 2c 0a 2a 2a 20  ith 64 bits,.** 
b3b50 74 68 65 6e 20 72 65 74 75 72 6e 20 66 61 6c 73  then return fals
b3b60 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68  e..**.** When th
b3b70 69 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20 6f  is routine was o
b3b80 72 69 67 69 6e 61 6c 6c 79 20 77 72 69 74 74 65  riginally writte
b3b90 6e 20 69 74 20 64 65 61 6c 74 20 77 69 74 68 20  n it dealt with 
b3ba0 6f 6e 6c 79 0a 2a 2a 20 33 32 2d 62 69 74 20 6e  only.** 32-bit n
b3bb0 75 6d 62 65 72 73 2e 20 20 41 74 20 74 68 61 74  umbers.  At that
b3bc0 20 74 69 6d 65 2c 20 69 74 20 77 61 73 20 6d 75   time, it was mu
b3bd0 63 68 20 66 61 73 74 65 72 20 74 68 61 6e 20 74  ch faster than t
b3be0 68 65 0a 2a 2a 20 61 74 6f 69 28 29 20 6c 69 62  he.** atoi() lib
b3bf0 72 61 72 79 20 72 6f 75 74 69 6e 65 20 69 6e 20  rary routine in 
b3c00 52 65 64 48 61 74 20 37 2e 32 2e 0a 2a 2f 0a 53  RedHat 7.2..*/.S
b3c10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
b3c20 74 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28  t sqlite3Atoi64(
b3c30 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d  const char *zNum
b3c40 2c 20 69 36 34 20 2a 70 4e 75 6d 29 7b 0a 20 20  , i64 *pNum){.  
b3c50 69 36 34 20 76 20 3d 20 30 3b 0a 20 20 69 6e 74  i64 v = 0;.  int
b3c60 20 6e 65 67 3b 0a 20 20 69 6e 74 20 69 2c 20 63   neg;.  int i, c
b3c70 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
b3c80 7a 53 74 61 72 74 3b 0a 20 20 77 68 69 6c 65 28  zStart;.  while(
b3c90 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28   sqlite3Isspace(
b3ca0 2a 7a 4e 75 6d 29 20 29 20 7a 4e 75 6d 2b 2b 3b  *zNum) ) zNum++;
b3cb0 0a 20 20 69 66 28 20 2a 7a 4e 75 6d 3d 3d 27 2d  .  if( *zNum=='-
b3cc0 27 20 29 7b 0a 20 20 20 20 6e 65 67 20 3d 20 31  ' ){.    neg = 1
b3cd0 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20  ;.    zNum++;.  
b3ce0 7d 65 6c 73 65 20 69 66 28 20 2a 7a 4e 75 6d 3d  }else if( *zNum=
b3cf0 3d 27 2b 27 20 29 7b 0a 20 20 20 20 6e 65 67 20  ='+' ){.    neg 
b3d00 3d 20 30 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b  = 0;.    zNum++;
b3d10 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 65  .  }else{.    ne
b3d20 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 7a 53 74  g = 0;.  }.  zSt
b3d30 61 72 74 20 3d 20 7a 4e 75 6d 3b 0a 20 20 77 68  art = zNum;.  wh
b3d40 69 6c 65 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 30  ile( zNum[0]=='0
b3d50 27 20 29 7b 20 7a 4e 75 6d 2b 2b 3b 20 7d 20 2f  ' ){ zNum++; } /
b3d60 2a 20 53 6b 69 70 20 6f 76 65 72 20 6c 65 61 64  * Skip over lead
b3d70 69 6e 67 20 7a 65 72 6f 73 2e 20 54 69 63 6b 65  ing zeros. Ticke
b3d80 74 20 23 32 34 35 34 20 2a 2f 0a 20 20 66 6f 72  t #2454 */.  for
b3d90 28 69 3d 30 3b 20 28 63 3d 7a 4e 75 6d 5b 69 5d  (i=0; (c=zNum[i]
b3da0 29 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 27  )>='0' && c<='9'
b3db0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 76 20 3d 20  ; i++){.    v = 
b3dc0 76 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 3b 0a  v*10 + c - '0';.
b3dd0 20 20 7d 0a 20 20 2a 70 4e 75 6d 20 3d 20 6e 65    }.  *pNum = ne
b3de0 67 20 3f 20 2d 76 20 3a 20 76 3b 0a 20 20 69 66  g ? -v : v;.  if
b3df0 28 20 63 21 3d 30 20 7c 7c 20 28 69 3d 3d 30 20  ( c!=0 || (i==0 
b3e00 26 26 20 7a 53 74 61 72 74 3d 3d 7a 4e 75 6d 29  && zStart==zNum)
b3e10 20 7c 7c 20 69 3e 31 39 20 29 7b 0a 20 20 20 20   || i>19 ){.    
b3e20 2f 2a 20 7a 4e 75 6d 20 69 73 20 65 6d 70 74 79  /* zNum is empty
b3e30 20 6f 72 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 6e   or contains non
b3e40 2d 6e 75 6d 65 72 69 63 20 74 65 78 74 20 6f 72  -numeric text or
b3e50 20 69 73 20 6c 6f 6e 67 65 72 0a 20 20 20 20 2a   is longer.    *
b3e60 2a 20 74 68 61 6e 20 31 39 20 64 69 67 69 74 73  * than 19 digits
b3e70 20 28 74 68 75 73 20 67 75 61 72 61 6e 74 69 6e   (thus guarantin
b3e80 67 20 74 68 61 74 20 69 74 20 69 73 20 74 6f 6f  g that it is too
b3e90 20 6c 61 72 67 65 29 20 2a 2f 0a 20 20 20 20 72   large) */.    r
b3ea0 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65  eturn 0;.  }else
b3eb0 20 69 66 28 20 69 3c 31 39 20 29 7b 0a 20 20 20   if( i<19 ){.   
b3ec0 20 2f 2a 20 4c 65 73 73 20 74 68 61 6e 20 31 39   /* Less than 19
b3ed0 20 64 69 67 69 74 73 2c 20 73 6f 20 77 65 20 6b   digits, so we k
b3ee0 6e 6f 77 20 74 68 61 74 20 69 74 20 66 69 74 73  now that it fits
b3ef0 20 69 6e 20 36 34 20 62 69 74 73 20 2a 2f 0a 20   in 64 bits */. 
b3f00 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
b3f10 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 31 39 2d  else{.    /* 19-
b3f20 64 69 67 69 74 20 6e 75 6d 62 65 72 73 20 6d 75  digit numbers mu
b3f30 73 74 20 62 65 20 6e 6f 20 6c 61 72 67 65 72 20  st be no larger 
b3f40 74 68 61 6e 20 39 32 32 33 33 37 32 30 33 36 38  than 92233720368
b3f50 35 34 37 37 35 38 30 37 20 69 66 20 70 6f 73 69  54775807 if posi
b3f60 74 69 76 65 0a 20 20 20 20 2a 2a 20 6f 72 20 39  tive.    ** or 9
b3f70 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38  2233720368547758
b3f80 30 38 20 69 66 20 6e 65 67 61 74 69 76 65 2e 20  08 if negative. 
b3f90 20 4e 6f 74 65 20 74 68 61 74 20 39 32 32 33 33   Note that 92233
b3fa0 37 32 30 33 36 38 35 34 36 36 35 38 30 38 0a 20  72036854665808. 
b3fb0 20 20 20 2a 2a 20 69 73 20 32 5e 36 33 2e 20 2a     ** is 2^63. *
b3fc0 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 63 6f 6d  /.    return com
b3fd0 70 61 72 65 32 70 6f 77 36 33 28 7a 4e 75 6d 29  pare2pow63(zNum)
b3fe0 3c 6e 65 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  <neg;.  }.}../*.
b3ff0 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 20 7a 4e  ** The string zN
b4000 75 6d 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e  um represents an
b4010 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65   unsigned intege
b4020 72 2e 20 20 54 68 65 20 7a 4e 75 6d 20 73 74 72  r.  The zNum str
b4030 69 6e 67 0a 2a 2a 20 63 6f 6e 73 69 73 74 73 20  ing.** consists 
b4040 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 64  of one or more d
b4050 69 67 69 74 20 63 68 61 72 61 63 74 65 72 73 20  igit characters 
b4060 61 6e 64 20 69 73 20 74 65 72 6d 69 6e 61 74 65  and is terminate
b4070 64 20 62 79 0a 2a 2a 20 61 20 7a 65 72 6f 20 63  d by.** a zero c
b4080 68 61 72 61 63 74 65 72 2e 20 20 41 6e 79 20 73  haracter.  Any s
b4090 74 72 61 79 20 63 68 61 72 61 63 74 65 72 73 20  tray characters 
b40a0 69 6e 20 7a 4e 75 6d 20 72 65 73 75 6c 74 20 69  in zNum result i
b40b0 6e 20 75 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 62  n undefined.** b
b40c0 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49  ehavior..**.** I
b40d0 66 20 74 68 65 20 75 6e 73 69 67 6e 65 64 20 69  f the unsigned i
b40e0 6e 74 65 67 65 72 20 74 68 61 74 20 7a 4e 75 6d  nteger that zNum
b40f0 20 72 65 70 72 65 73 65 6e 74 73 20 77 69 6c 6c   represents will
b4100 20 66 69 74 20 69 6e 20 61 0a 2a 2a 20 36 34 2d   fit in a.** 64-
b4110 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67  bit signed integ
b4120 65 72 2c 20 72 65 74 75 72 6e 20 54 52 55 45 2e  er, return TRUE.
b4130 20 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 75    Otherwise retu
b4140 72 6e 20 46 41 4c 53 45 2e 0a 2a 2a 0a 2a 2a 20  rn FALSE..**.** 
b4150 49 66 20 74 68 65 20 6e 65 67 46 6c 61 67 20 70  If the negFlag p
b4160 61 72 61 6d 65 74 65 72 20 69 73 20 74 72 75 65  arameter is true
b4170 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61  , that means tha
b4180 74 20 7a 4e 75 6d 20 72 65 61 6c 6c 79 20 72 65  t zNum really re
b4190 70 72 65 73 65 6e 74 73 0a 2a 2a 20 61 20 6e 65  presents.** a ne
b41a0 67 61 74 69 76 65 20 6e 75 6d 62 65 72 2e 20 20  gative number.  
b41b0 28 54 68 65 20 6c 65 61 64 69 6e 67 20 22 2d 22  (The leading "-"
b41c0 20 69 73 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d   is omitted from
b41d0 20 7a 4e 75 6d 2e 29 20 20 54 68 69 73 0a 2a 2a   zNum.)  This.**
b41e0 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 65   parameter is ne
b41f0 65 64 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e  eded to determin
b4200 65 20 61 20 62 6f 75 6e 64 61 72 79 20 63 61 73  e a boundary cas
b4210 65 2e 20 20 41 20 73 74 72 69 6e 67 0a 2a 2a 20  e.  A string.** 
b4220 6f 66 20 22 39 32 32 33 33 37 33 30 33 36 38 35  of "922337303685
b4230 34 37 37 35 38 30 38 22 20 72 65 74 75 72 6e 73  4775808" returns
b4240 20 66 61 6c 73 65 20 69 66 20 6e 65 67 46 6c 61   false if negFla
b4250 67 20 69 73 20 66 61 6c 73 65 20 6f 72 20 74 72  g is false or tr
b4260 75 65 0a 2a 2a 20 69 66 20 6e 65 67 46 6c 61 67  ue.** if negFlag
b4270 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20   is true..**.** 
b4280 4c 65 61 64 69 6e 67 20 7a 65 72 6f 73 20 61 72  Leading zeros ar
b4290 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2f 0a 53 51  e ignored..*/.SQ
b42a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
b42b0 20 73 71 6c 69 74 65 33 46 69 74 73 49 6e 36 34   sqlite3FitsIn64
b42c0 42 69 74 73 28 63 6f 6e 73 74 20 63 68 61 72 20  Bits(const char 
b42d0 2a 7a 4e 75 6d 2c 20 69 6e 74 20 6e 65 67 46 6c  *zNum, int negFl
b42e0 61 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  ag){.  int i;.  
b42f0 69 6e 74 20 6e 65 67 20 3d 20 30 3b 0a 0a 20 20  int neg = 0;..  
b4300 61 73 73 65 72 74 28 20 7a 4e 75 6d 5b 30 5d 3e  assert( zNum[0]>
b4310 3d 27 30 27 20 26 26 20 7a 4e 75 6d 5b 30 5d 3c  ='0' && zNum[0]<
b4320 3d 27 39 27 20 29 3b 20 2f 2a 20 7a 4e 75 6d 20  ='9' ); /* zNum 
b4330 69 73 20 61 6e 20 75 6e 73 69 67 6e 65 64 20 6e  is an unsigned n
b4340 75 6d 62 65 72 20 2a 2f 0a 0a 20 20 69 66 28 20  umber */..  if( 
b4350 6e 65 67 46 6c 61 67 20 29 20 6e 65 67 20 3d 20  negFlag ) neg = 
b4360 31 2d 6e 65 67 3b 0a 20 20 77 68 69 6c 65 28 20  1-neg;.  while( 
b4370 2a 7a 4e 75 6d 3d 3d 27 30 27 20 29 7b 0a 20 20  *zNum=='0' ){.  
b4380 20 20 7a 4e 75 6d 2b 2b 3b 20 20 20 2f 2a 20 53    zNum++;   /* S
b4390 6b 69 70 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f  kip leading zero
b43a0 73 2e 20 20 54 69 63 6b 65 74 20 23 32 34 35 34  s.  Ticket #2454
b43b0 20 2a 2f 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d   */.  }.  for(i=
b43c0 30 3b 20 7a 4e 75 6d 5b 69 5d 3b 20 69 2b 2b 29  0; zNum[i]; i++)
b43d0 7b 20 61 73 73 65 72 74 28 20 7a 4e 75 6d 5b 69  { assert( zNum[i
b43e0 5d 3e 3d 27 30 27 20 26 26 20 7a 4e 75 6d 5b 69  ]>='0' && zNum[i
b43f0 5d 3c 3d 27 39 27 20 29 3b 20 7d 0a 20 20 69 66  ]<='9' ); }.  if
b4400 28 20 69 3c 31 39 20 29 7b 0a 20 20 20 20 2f 2a  ( i<19 ){.    /*
b4410 20 47 75 61 72 61 6e 74 65 65 64 20 74 6f 20 66   Guaranteed to f
b4420 69 74 20 69 66 20 6c 65 73 73 20 74 68 61 6e 20  it if less than 
b4430 31 39 20 64 69 67 69 74 73 20 2a 2f 0a 20 20 20  19 digits */.   
b4440 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c   return 1;.  }el
b4450 73 65 20 69 66 28 20 69 3e 31 39 20 29 7b 0a 20  se if( i>19 ){. 
b4460 20 20 20 2f 2a 20 47 75 61 72 61 6e 74 65 65 64     /* Guaranteed
b4470 20 74 6f 20 62 65 20 74 6f 6f 20 62 69 67 20 69   to be too big i
b4480 66 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 31  f greater than 1
b4490 39 20 64 69 67 69 74 73 20 2a 2f 0a 20 20 20 20  9 digits */.    
b44a0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73  return 0;.  }els
b44b0 65 7b 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 61 72  e{.    /* Compar
b44c0 65 20 61 67 61 69 6e 73 74 20 32 5e 36 33 2e 20  e against 2^63. 
b44d0 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 63 6f  */.    return co
b44e0 6d 70 61 72 65 32 70 6f 77 36 33 28 7a 4e 75 6d  mpare2pow63(zNum
b44f0 29 3c 6e 65 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  )<neg;.  }.}../*
b4500 0a 2a 2a 20 49 66 20 7a 4e 75 6d 20 72 65 70 72  .** If zNum repr
b4510 65 73 65 6e 74 73 20 61 6e 20 69 6e 74 65 67 65  esents an intege
b4520 72 20 74 68 61 74 20 77 69 6c 6c 20 66 69 74 20  r that will fit 
b4530 69 6e 20 33 32 2d 62 69 74 73 2c 20 74 68 65 6e  in 32-bits, then
b4540 20 73 65 74 0a 2a 2a 20 2a 70 56 61 6c 75 65 20   set.** *pValue 
b4550 74 6f 20 74 68 61 74 20 69 6e 74 65 67 65 72 20  to that integer 
b4560 61 6e 64 20 72 65 74 75 72 6e 20 74 72 75 65 2e  and return true.
b4570 20 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 75    Otherwise retu
b4580 72 6e 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20  rn false..**.** 
b4590 41 6e 79 20 6e 6f 6e 2d 6e 75 6d 65 72 69 63 20  Any non-numeric 
b45a0 63 68 61 72 61 63 74 65 72 73 20 74 68 61 74 20  characters that 
b45b0 66 6f 6c 6c 6f 77 69 6e 67 20 7a 4e 75 6d 20 61  following zNum a
b45c0 72 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 20 54  re ignored..** T
b45d0 68 69 73 20 69 73 20 64 69 66 66 65 72 65 6e 74  his is different
b45e0 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 41 74 6f   from sqlite3Ato
b45f0 69 36 34 28 29 20 77 68 69 63 68 20 72 65 71 75  i64() which requ
b4600 69 72 65 73 20 74 68 65 0a 2a 2a 20 69 6e 70 75  ires the.** inpu
b4610 74 20 6e 75 6d 62 65 72 20 74 6f 20 62 65 20 7a  t number to be z
b4620 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 0a  ero-terminated..
b4630 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
b4640 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74  E int sqlite3Get
b4650 49 6e 74 33 32 28 63 6f 6e 73 74 20 63 68 61 72  Int32(const char
b4660 20 2a 7a 4e 75 6d 2c 20 69 6e 74 20 2a 70 56 61   *zNum, int *pVa
b4670 6c 75 65 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69  lue){.  sqlite_i
b4680 6e 74 36 34 20 76 20 3d 20 30 3b 0a 20 20 69 6e  nt64 v = 0;.  in
b4690 74 20 69 2c 20 63 3b 0a 20 20 69 6e 74 20 6e 65  t i, c;.  int ne
b46a0 67 20 3d 20 30 3b 0a 20 20 69 66 28 20 7a 4e 75  g = 0;.  if( zNu
b46b0 6d 5b 30 5d 3d 3d 27 2d 27 20 29 7b 0a 20 20 20  m[0]=='-' ){.   
b46c0 20 6e 65 67 20 3d 20 31 3b 0a 20 20 20 20 7a 4e   neg = 1;.    zN
b46d0 75 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 65 20 69 66  um++;.  }else if
b46e0 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 2b 27 20 29  ( zNum[0]=='+' )
b46f0 7b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20  {.    zNum++;.  
b4700 7d 0a 20 20 77 68 69 6c 65 28 20 7a 4e 75 6d 5b  }.  while( zNum[
b4710 30 5d 3d 3d 27 30 27 20 29 20 7a 4e 75 6d 2b 2b  0]=='0' ) zNum++
b4720 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31  ;.  for(i=0; i<1
b4730 31 20 26 26 20 28 63 20 3d 20 7a 4e 75 6d 5b 69  1 && (c = zNum[i
b4740 5d 20 2d 20 27 30 27 29 3e 3d 30 20 26 26 20 63  ] - '0')>=0 && c
b4750 3c 3d 39 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 76  <=9; i++){.    v
b4760 20 3d 20 76 2a 31 30 20 2b 20 63 3b 0a 20 20 7d   = v*10 + c;.  }
b4770 0a 0a 20 20 2f 2a 20 54 68 65 20 6c 6f 6e 67 65  ..  /* The longe
b4780 73 74 20 64 65 63 69 6d 61 6c 20 72 65 70 72 65  st decimal repre
b4790 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 33  sentation of a 3
b47a0 32 20 62 69 74 20 69 6e 74 65 67 65 72 20 69 73  2 bit integer is
b47b0 20 31 30 20 64 69 67 69 74 73 3a 0a 20 20 2a 2a   10 digits:.  **
b47c0 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20  .  **           
b47d0 20 20 31 32 33 34 35 36 37 38 39 30 0a 20 20 2a    1234567890.  *
b47e0 2a 20 20 20 20 20 32 5e 33 31 20 2d 3e 20 32 31  *     2^31 -> 21
b47f0 34 37 34 38 33 36 34 38 0a 20 20 2a 2f 0a 20 20  47483648.  */.  
b4800 69 66 28 20 69 3e 31 30 20 29 7b 0a 20 20 20 20  if( i>10 ){.    
b4810 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
b4820 69 66 28 20 76 2d 6e 65 67 3e 32 31 34 37 34 38  if( v-neg>214748
b4830 33 36 34 37 20 29 7b 0a 20 20 20 20 72 65 74 75  3647 ){.    retu
b4840 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20  rn 0;.  }.  if( 
b4850 6e 65 67 20 29 7b 0a 20 20 20 20 76 20 3d 20 2d  neg ){.    v = -
b4860 76 3b 0a 20 20 7d 0a 20 20 2a 70 56 61 6c 75 65  v;.  }.  *pValue
b4870 20 3d 20 28 69 6e 74 29 76 3b 0a 20 20 72 65 74   = (int)v;.  ret
b4880 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 1;.}../*.** 
b4890 54 68 65 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e  The variable-len
b48a0 67 74 68 20 69 6e 74 65 67 65 72 20 65 6e 63 6f  gth integer enco
b48b0 64 69 6e 67 20 69 73 20 61 73 20 66 6f 6c 6c 6f  ding is as follo
b48c0 77 73 3a 0a 2a 2a 0a 2a 2a 20 4b 45 59 3a 0a 2a  ws:.**.** KEY:.*
b48d0 2a 20 20 20 20 20 20 20 20 20 41 20 3d 20 30 78  *         A = 0x
b48e0 78 78 78 78 78 78 20 20 20 20 37 20 62 69 74 73  xxxxxx    7 bits
b48f0 20 6f 66 20 64 61 74 61 20 61 6e 64 20 6f 6e 65   of data and one
b4900 20 66 6c 61 67 20 62 69 74 0a 2a 2a 20 20 20 20   flag bit.**    
b4910 20 20 20 20 20 42 20 3d 20 31 78 78 78 78 78 78       B = 1xxxxxx
b4920 78 20 20 20 20 37 20 62 69 74 73 20 6f 66 20 64  x    7 bits of d
b4930 61 74 61 20 61 6e 64 20 6f 6e 65 20 66 6c 61 67  ata and one flag
b4940 20 62 69 74 0a 2a 2a 20 20 20 20 20 20 20 20 20   bit.**         
b4950 43 20 3d 20 78 78 78 78 78 78 78 78 20 20 20 20  C = xxxxxxxx    
b4960 38 20 62 69 74 73 20 6f 66 20 64 61 74 61 0a 2a  8 bits of data.*
b4970 2a 0a 2a 2a 20 20 37 20 62 69 74 73 20 2d 20 41  *.**  7 bits - A
b4980 0a 2a 2a 20 31 34 20 62 69 74 73 20 2d 20 42 41  .** 14 bits - BA
b4990 0a 2a 2a 20 32 31 20 62 69 74 73 20 2d 20 42 42  .** 21 bits - BB
b49a0 41 0a 2a 2a 20 32 38 20 62 69 74 73 20 2d 20 42  A.** 28 bits - B
b49b0 42 42 41 0a 2a 2a 20 33 35 20 62 69 74 73 20 2d  BBA.** 35 bits -
b49c0 20 42 42 42 42 41 0a 2a 2a 20 34 32 20 62 69 74   BBBBA.** 42 bit
b49d0 73 20 2d 20 42 42 42 42 42 41 0a 2a 2a 20 34 39  s - BBBBBA.** 49
b49e0 20 62 69 74 73 20 2d 20 42 42 42 42 42 42 41 0a   bits - BBBBBBA.
b49f0 2a 2a 20 35 36 20 62 69 74 73 20 2d 20 42 42 42  ** 56 bits - BBB
b4a00 42 42 42 42 41 0a 2a 2a 20 36 34 20 62 69 74 73  BBBBA.** 64 bits
b4a10 20 2d 20 42 42 42 42 42 42 42 42 43 0a 2a 2f 0a   - BBBBBBBBC.*/.
b4a20 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 36  ./*.** Write a 6
b4a30 34 2d 62 69 74 20 76 61 72 69 61 62 6c 65 2d 6c  4-bit variable-l
b4a40 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 74 6f  ength integer to
b4a50 20 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67   memory starting
b4a60 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20 54 68 65   at p[0]..** The
b4a70 20 6c 65 6e 67 74 68 20 6f 66 20 64 61 74 61 20   length of data 
b4a80 77 72 69 74 65 20 77 69 6c 6c 20 62 65 20 62 65  write will be be
b4a90 74 77 65 65 6e 20 31 20 61 6e 64 20 39 20 62 79  tween 1 and 9 by
b4aa0 74 65 73 2e 20 20 54 68 65 20 6e 75 6d 62 65 72  tes.  The number
b4ab0 0a 2a 2a 20 6f 66 20 62 79 74 65 73 20 77 72 69  .** of bytes wri
b4ac0 74 74 65 6e 20 69 73 20 72 65 74 75 72 6e 65 64  tten is returned
b4ad0 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 61 72 69 61 62  ..**.** A variab
b4ae0 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65  le-length intege
b4af0 72 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 68  r consists of th
b4b00 65 20 6c 6f 77 65 72 20 37 20 62 69 74 73 20 6f  e lower 7 bits o
b4b10 66 20 65 61 63 68 20 62 79 74 65 0a 2a 2a 20 66  f each byte.** f
b4b20 6f 72 20 61 6c 6c 20 62 79 74 65 73 20 74 68 61  or all bytes tha
b4b30 74 20 68 61 76 65 20 74 68 65 20 38 74 68 20 62  t have the 8th b
b4b40 69 74 20 73 65 74 20 61 6e 64 20 6f 6e 65 20 62  it set and one b
b4b50 79 74 65 20 77 69 74 68 20 74 68 65 20 38 74 68  yte with the 8th
b4b60 0a 2a 2a 20 62 69 74 20 63 6c 65 61 72 2e 20 20  .** bit clear.  
b4b70 45 78 63 65 70 74 2c 20 69 66 20 77 65 20 67 65  Except, if we ge
b4b80 74 20 74 6f 20 74 68 65 20 39 74 68 20 62 79 74  t to the 9th byt
b4b90 65 2c 20 69 74 20 73 74 6f 72 65 73 20 74 68 65  e, it stores the
b4ba0 20 66 75 6c 6c 0a 2a 2a 20 38 20 62 69 74 73 20   full.** 8 bits 
b4bb0 61 6e 64 20 69 73 20 74 68 65 20 6c 61 73 74 20  and is the last 
b4bc0 62 79 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  byte..*/.SQLITE_
b4bd0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
b4be0 74 65 33 50 75 74 56 61 72 69 6e 74 28 75 6e 73  te3PutVarint(uns
b4bf0 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75  igned char *p, u
b4c00 36 34 20 76 29 7b 0a 20 20 69 6e 74 20 69 2c 20  64 v){.  int i, 
b4c10 6a 2c 20 6e 3b 0a 20 20 75 38 20 62 75 66 5b 31  j, n;.  u8 buf[1
b4c20 30 5d 3b 0a 20 20 69 66 28 20 76 20 26 20 28 28  0];.  if( v & ((
b4c30 28 75 36 34 29 30 78 66 66 30 30 30 30 30 30 29  (u64)0xff000000)
b4c40 3c 3c 33 32 29 20 29 7b 0a 20 20 20 20 70 5b 38  <<32) ){.    p[8
b4c50 5d 20 3d 20 28 75 38 29 76 3b 0a 20 20 20 20 76  ] = (u8)v;.    v
b4c60 20 3e 3e 3d 20 38 3b 0a 20 20 20 20 66 6f 72 28   >>= 8;.    for(
b4c70 69 3d 37 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b  i=7; i>=0; i--){
b4c80 0a 20 20 20 20 20 20 70 5b 69 5d 20 3d 20 28 75  .      p[i] = (u
b4c90 38 29 28 28 76 20 26 20 30 78 37 66 29 20 7c 20  8)((v & 0x7f) | 
b4ca0 30 78 38 30 29 3b 0a 20 20 20 20 20 20 76 20 3e  0x80);.      v >
b4cb0 3e 3d 20 37 3b 0a 20 20 20 20 7d 0a 20 20 20 20  >= 7;.    }.    
b4cc0 72 65 74 75 72 6e 20 39 3b 0a 20 20 7d 20 20 20  return 9;.  }   
b4cd0 20 0a 20 20 6e 20 3d 20 30 3b 0a 20 20 64 6f 7b   .  n = 0;.  do{
b4ce0 0a 20 20 20 20 62 75 66 5b 6e 2b 2b 5d 20 3d 20  .    buf[n++] = 
b4cf0 28 75 38 29 28 28 76 20 26 20 30 78 37 66 29 20  (u8)((v & 0x7f) 
b4d00 7c 20 30 78 38 30 29 3b 0a 20 20 20 20 76 20 3e  | 0x80);.    v >
b4d10 3e 3d 20 37 3b 0a 20 20 7d 77 68 69 6c 65 28 20  >= 7;.  }while( 
b4d20 76 21 3d 30 20 29 3b 0a 20 20 62 75 66 5b 30 5d  v!=0 );.  buf[0]
b4d30 20 26 3d 20 30 78 37 66 3b 0a 20 20 61 73 73 65   &= 0x7f;.  asse
b4d40 72 74 28 20 6e 3c 3d 39 20 29 3b 0a 20 20 66 6f  rt( n<=9 );.  fo
b4d50 72 28 69 3d 30 2c 20 6a 3d 6e 2d 31 3b 20 6a 3e  r(i=0, j=n-1; j>
b4d60 3d 30 3b 20 6a 2d 2d 2c 20 69 2b 2b 29 7b 0a 20  =0; j--, i++){. 
b4d70 20 20 20 70 5b 69 5d 20 3d 20 62 75 66 5b 6a 5d     p[i] = buf[j]
b4d80 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e  ;.  }.  return n
b4d90 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
b4da0 72 6f 75 74 69 6e 65 20 69 73 20 61 20 66 61 73  routine is a fas
b4db0 74 65 72 20 76 65 72 73 69 6f 6e 20 6f 66 20 73  ter version of s
b4dc0 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 28  qlite3PutVarint(
b4dd0 29 20 74 68 61 74 20 6f 6e 6c 79 0a 2a 2a 20 77  ) that only.** w
b4de0 6f 72 6b 73 20 66 6f 72 20 33 32 2d 62 69 74 20  orks for 32-bit 
b4df0 70 6f 73 69 74 69 76 65 20 69 6e 74 65 67 65 72  positive integer
b4e00 73 20 61 6e 64 20 77 68 69 63 68 20 69 73 20 6f  s and which is o
b4e10 70 74 69 6d 69 7a 65 64 20 66 6f 72 0a 2a 2a 20  ptimized for.** 
b4e20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20  the common case 
b4e30 6f 66 20 73 6d 61 6c 6c 20 69 6e 74 65 67 65 72  of small integer
b4e40 73 2e 20 20 41 20 4d 41 43 52 4f 20 76 65 72 73  s.  A MACRO vers
b4e50 69 6f 6e 2c 20 70 75 74 56 61 72 69 6e 74 33 32  ion, putVarint32
b4e60 2c 0a 2a 2a 20 69 73 20 70 72 6f 76 69 64 65 64  ,.** is provided
b4e70 20 77 68 69 63 68 20 69 6e 6c 69 6e 65 73 20 74   which inlines t
b4e80 68 65 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63  he single-byte c
b4e90 61 73 65 2e 20 20 41 6c 6c 20 63 6f 64 65 20 73  ase.  All code s
b4ea0 68 6f 75 6c 64 20 75 73 65 0a 2a 2a 20 74 68 65  hould use.** the
b4eb0 20 4d 41 43 52 4f 20 76 65 72 73 69 6f 6e 20 61   MACRO version a
b4ec0 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  s this function 
b4ed0 61 73 73 75 6d 65 73 20 74 68 65 20 73 69 6e 67  assumes the sing
b4ee0 6c 65 2d 62 79 74 65 20 63 61 73 65 20 68 61 73  le-byte case has
b4ef0 0a 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e  .** already been
b4f00 20 68 61 6e 64 6c 65 64 2e 0a 2a 2f 0a 53 51 4c   handled..*/.SQL
b4f10 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
b4f20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74  sqlite3PutVarint
b4f30 33 32 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  32(unsigned char
b4f40 20 2a 70 2c 20 75 33 32 20 76 29 7b 0a 23 69 66   *p, u32 v){.#if
b4f50 6e 64 65 66 20 70 75 74 56 61 72 69 6e 74 33 32  ndef putVarint32
b4f60 0a 20 20 69 66 28 20 28 76 20 26 20 7e 30 78 37  .  if( (v & ~0x7
b4f70 66 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 5b 30  f)==0 ){.    p[0
b4f80 5d 20 3d 20 76 3b 0a 20 20 20 20 72 65 74 75 72  ] = v;.    retur
b4f90 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  n 1;.  }.#endif.
b4fa0 20 20 69 66 28 20 28 76 20 26 20 7e 30 78 33 66    if( (v & ~0x3f
b4fb0 66 66 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 5b  ff)==0 ){.    p[
b4fc0 30 5d 20 3d 20 28 75 38 29 28 28 76 3e 3e 37 29  0] = (u8)((v>>7)
b4fd0 20 7c 20 30 78 38 30 29 3b 0a 20 20 20 20 70 5b   | 0x80);.    p[
b4fe0 31 5d 20 3d 20 28 75 38 29 28 76 20 26 20 30 78  1] = (u8)(v & 0x
b4ff0 37 66 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  7f);.    return 
b5000 32 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  2;.  }.  return 
b5010 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74  sqlite3PutVarint
b5020 28 70 2c 20 76 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  (p, v);.}../*.**
b5030 20 52 65 61 64 20 61 20 36 34 2d 62 69 74 20 76   Read a 64-bit v
b5040 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69  ariable-length i
b5050 6e 74 65 67 65 72 20 66 72 6f 6d 20 6d 65 6d 6f  nteger from memo
b5060 72 79 20 73 74 61 72 74 69 6e 67 20 61 74 20 70  ry starting at p
b5070 5b 30 5d 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74  [0]..** Return t
b5080 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
b5090 65 73 20 72 65 61 64 2e 20 20 54 68 65 20 76 61  es read.  The va
b50a0 6c 75 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e  lue is stored in
b50b0 20 2a 76 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50   *v..*/.SQLITE_P
b50c0 52 49 56 41 54 45 20 75 38 20 73 71 6c 69 74 65  RIVATE u8 sqlite
b50d0 33 47 65 74 56 61 72 69 6e 74 28 63 6f 6e 73 74  3GetVarint(const
b50e0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
b50f0 70 2c 20 75 36 34 20 2a 76 29 7b 0a 20 20 75 33  p, u64 *v){.  u3
b5100 32 20 61 2c 62 2c 73 3b 0a 0a 20 20 61 20 3d 20  2 a,b,s;..  a = 
b5110 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 20 28  *p;.  /* a: p0 (
b5120 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69  unmasked) */.  i
b5130 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 20  f (!(a&0x80)).  
b5140 7b 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20  {.    *v = a;.  
b5150 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
b5160 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 2a 70  .  p++;.  b = *p
b5170 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 20 28 75 6e  ;.  /* b: p1 (un
b5180 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20  masked) */.  if 
b5190 28 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a  (!(b&0x80)).  {.
b51a0 20 20 20 20 61 20 26 3d 20 30 78 37 66 3b 0a 20      a &= 0x7f;. 
b51b0 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20     a = a<<7;.   
b51c0 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 2a 76 20   a |= b;.    *v 
b51d0 3d 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = a;.    return 
b51e0 32 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20  2;.  }..  p++;. 
b51f0 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 20   a = a<<14;.  a 
b5200 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70  |= *p;.  /* a: p
b5210 30 3c 3c 31 34 20 7c 20 70 32 20 28 75 6e 6d 61  0<<14 | p2 (unma
b5220 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21  sked) */.  if (!
b5230 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20  (a&0x80)).  {.  
b5240 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34    a &= (0x7f<<14
b5250 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62 20  )|(0x7f);.    b 
b5260 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 62 20 3d  &= 0x7f;.    b =
b5270 20 62 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20   b<<7;.    a |= 
b5280 62 3b 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20  b;.    *v = a;. 
b5290 20 20 20 72 65 74 75 72 6e 20 33 3b 0a 20 20 7d     return 3;.  }
b52a0 0a 0a 20 20 2f 2a 20 43 53 45 31 20 66 72 6f 6d  ..  /* CSE1 from
b52b0 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 61 20 26 3d   below */.  a &=
b52c0 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37   (0x7f<<14)|(0x7
b52d0 66 29 3b 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d  f);.  p++;.  b =
b52e0 20 62 3c 3c 31 34 3b 0a 20 20 62 20 7c 3d 20 2a   b<<14;.  b |= *
b52f0 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 3c 3c 31  p;.  /* b: p1<<1
b5300 34 20 7c 20 70 33 20 28 75 6e 6d 61 73 6b 65 64  4 | p3 (unmasked
b5310 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 30  ) */.  if (!(b&0
b5320 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 62 20  x80)).  {.    b 
b5330 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30  &= (0x7f<<14)|(0
b5340 78 37 66 29 3b 0a 20 20 20 20 2f 2a 20 6d 6f 76  x7f);.    /* mov
b5350 65 64 20 43 53 45 31 20 75 70 20 2a 2f 0a 20 20  ed CSE1 up */.  
b5360 20 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 3c    /* a &= (0x7f<
b5370 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a  <14)|(0x7f); */.
b5380 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20      a = a<<7;.  
b5390 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 2a 76    a |= b;.    *v
b53a0 20 3d 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e   = a;.    return
b53b0 20 34 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 61 3a   4;.  }..  /* a:
b53c0 20 70 30 3c 3c 31 34 20 7c 20 70 32 20 28 6d 61   p0<<14 | p2 (ma
b53d0 73 6b 65 64 29 20 2a 2f 0a 20 20 2f 2a 20 62 3a  sked) */.  /* b:
b53e0 20 70 31 3c 3c 31 34 20 7c 20 70 33 20 28 75 6e   p1<<14 | p3 (un
b53f0 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 2f 2a 20  masked) */.  /* 
b5400 31 3a 73 61 76 65 20 6f 66 66 20 70 30 3c 3c 32  1:save off p0<<2
b5410 31 20 7c 20 70 31 3c 3c 31 34 20 7c 20 70 32 3c  1 | p1<<14 | p2<
b5420 3c 37 20 7c 20 70 33 20 28 6d 61 73 6b 65 64 29  <7 | p3 (masked)
b5430 20 2a 2f 0a 20 20 2f 2a 20 6d 6f 76 65 64 20 43   */.  /* moved C
b5440 53 45 31 20 75 70 20 2a 2f 0a 20 20 2f 2a 20 61  SE1 up */.  /* a
b5450 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28   &= (0x7f<<14)|(
b5460 30 78 37 66 29 3b 20 2a 2f 0a 20 20 62 20 26 3d  0x7f); */.  b &=
b5470 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37   (0x7f<<14)|(0x7
b5480 66 29 3b 0a 20 20 73 20 3d 20 61 3b 0a 20 20 2f  f);.  s = a;.  /
b5490 2a 20 73 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32  * s: p0<<14 | p2
b54a0 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a 0a 20 20   (masked) */..  
b54b0 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34  p++;.  a = a<<14
b54c0 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f  ;.  a |= *p;.  /
b54d0 2a 20 61 3a 20 70 30 3c 3c 32 38 20 7c 20 70 32  * a: p0<<28 | p2
b54e0 3c 3c 31 34 20 7c 20 70 34 20 28 75 6e 6d 61 73  <<14 | p4 (unmas
b54f0 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28  ked) */.  if (!(
b5500 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20  a&0x80)).  {.   
b5510 20 2f 2a 20 77 65 20 63 61 6e 20 73 6b 69 70 20   /* we can skip 
b5520 74 68 65 73 65 20 63 61 75 73 65 20 74 68 65 79  these cause they
b5530 20 77 65 72 65 20 28 65 66 66 65 63 74 69 76 65   were (effective
b5540 6c 79 29 20 64 6f 6e 65 20 61 62 6f 76 65 20 69  ly) done above i
b5550 6e 20 63 61 6c 63 27 69 6e 67 20 73 20 2a 2f 0a  n calc'ing s */.
b5560 20 20 20 20 2f 2a 20 61 20 26 3d 20 28 30 78 37      /* a &= (0x7
b5570 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 34  f<<28)|(0x7f<<14
b5580 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20  )|(0x7f); */.   
b5590 20 2f 2a 20 62 20 26 3d 20 28 30 78 37 66 3c 3c   /* b &= (0x7f<<
b55a0 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20  14)|(0x7f); */. 
b55b0 20 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20     b = b<<7;.   
b55c0 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 73 20 3d   a |= b;.    s =
b55d0 20 73 3e 3e 31 38 3b 0a 20 20 20 20 2a 76 20 3d   s>>18;.    *v =
b55e0 20 28 28 75 36 34 29 73 29 3c 3c 33 32 20 7c 20   ((u64)s)<<32 | 
b55f0 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 35 3b  a;.    return 5;
b5600 0a 20 20 7d 0a 0a 20 20 2f 2a 20 32 3a 73 61 76  .  }..  /* 2:sav
b5610 65 20 6f 66 66 20 70 30 3c 3c 32 31 20 7c 20 70  e off p0<<21 | p
b5620 31 3c 3c 31 34 20 7c 20 70 32 3c 3c 37 20 7c 20  1<<14 | p2<<7 | 
b5630 70 33 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a 20  p3 (masked) */. 
b5640 20 73 20 3d 20 73 3c 3c 37 3b 0a 20 20 73 20 7c   s = s<<7;.  s |
b5650 3d 20 62 3b 0a 20 20 2f 2a 20 73 3a 20 70 30 3c  = b;.  /* s: p0<
b5660 3c 32 31 20 7c 20 70 31 3c 3c 31 34 20 7c 20 70  <21 | p1<<14 | p
b5670 32 3c 3c 37 20 7c 20 70 33 20 28 6d 61 73 6b 65  2<<7 | p3 (maske
b5680 64 29 20 2a 2f 0a 0a 20 20 70 2b 2b 3b 0a 20 20  d) */..  p++;.  
b5690 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 20 62 20 7c  b = b<<14;.  b |
b56a0 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 31  = *p;.  /* b: p1
b56b0 3c 3c 32 38 20 7c 20 70 33 3c 3c 31 34 20 7c 20  <<28 | p3<<14 | 
b56c0 70 35 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f  p5 (unmasked) */
b56d0 0a 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29  .  if (!(b&0x80)
b56e0 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 77 65 20  ).  {.    /* we 
b56f0 63 61 6e 20 73 6b 69 70 20 74 68 69 73 20 63 61  can skip this ca
b5700 75 73 65 20 69 74 20 77 61 73 20 28 65 66 66 65  use it was (effe
b5710 63 74 69 76 65 6c 79 29 20 64 6f 6e 65 20 61 62  ctively) done ab
b5720 6f 76 65 20 69 6e 20 63 61 6c 63 27 69 6e 67 20  ove in calc'ing 
b5730 73 20 2a 2f 0a 20 20 20 20 2f 2a 20 62 20 26 3d  s */.    /* b &=
b5740 20 28 30 78 37 66 3c 3c 32 38 29 7c 28 30 78 37   (0x7f<<28)|(0x7
b5750 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 2a  f<<14)|(0x7f); *
b5760 2f 0a 20 20 20 20 61 20 26 3d 20 28 30 78 37 66  /.    a &= (0x7f
b5770 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20  <<14)|(0x7f);.  
b5780 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20    a = a<<7;.    
b5790 61 20 7c 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20  a |= b;.    s = 
b57a0 73 3e 3e 31 38 3b 0a 20 20 20 20 2a 76 20 3d 20  s>>18;.    *v = 
b57b0 28 28 75 36 34 29 73 29 3c 3c 33 32 20 7c 20 61  ((u64)s)<<32 | a
b57c0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 36 3b 0a  ;.    return 6;.
b57d0 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20    }..  p++;.  a 
b57e0 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20  = a<<14;.  a |= 
b57f0 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 32 3c 3c  *p;.  /* a: p2<<
b5800 32 38 20 7c 20 70 34 3c 3c 31 34 20 7c 20 70 36  28 | p4<<14 | p6
b5810 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20   (unmasked) */. 
b5820 20 69 66 20 28 21 28 61 26 30 78 38 30 29 29 0a   if (!(a&0x80)).
b5830 20 20 7b 0a 20 20 20 20 61 20 26 3d 20 28 30 78    {.    a &= (0x
b5840 31 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31  1f<<28)|(0x7f<<1
b5850 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62  4)|(0x7f);.    b
b5860 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28   &= (0x7f<<14)|(
b5870 30 78 37 66 29 3b 0a 20 20 20 20 62 20 3d 20 62  0x7f);.    b = b
b5880 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b  <<7;.    a |= b;
b5890 0a 20 20 20 20 73 20 3d 20 73 3e 3e 31 31 3b 0a  .    s = s>>11;.
b58a0 20 20 20 20 2a 76 20 3d 20 28 28 75 36 34 29 73      *v = ((u64)s
b58b0 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 20 20 72  )<<32 | a;.    r
b58c0 65 74 75 72 6e 20 37 3b 0a 20 20 7d 0a 0a 20 20  eturn 7;.  }..  
b58d0 2f 2a 20 43 53 45 32 20 66 72 6f 6d 20 62 65 6c  /* CSE2 from bel
b58e0 6f 77 20 2a 2f 0a 20 20 61 20 26 3d 20 28 30 78  ow */.  a &= (0x
b58f0 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a  7f<<14)|(0x7f);.
b5900 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c    p++;.  b = b<<
b5910 31 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20  14;.  b |= *p;. 
b5920 20 2f 2a 20 62 3a 20 70 33 3c 3c 32 38 20 7c 20   /* b: p3<<28 | 
b5930 70 35 3c 3c 31 34 20 7c 20 70 37 20 28 75 6e 6d  p5<<14 | p7 (unm
b5940 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28  asked) */.  if (
b5950 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20  !(b&0x80)).  {. 
b5960 20 20 20 62 20 26 3d 20 28 30 78 31 66 3c 3c 32     b &= (0x1f<<2
b5970 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30  8)|(0x7f<<14)|(0
b5980 78 37 66 29 3b 0a 20 20 20 20 2f 2a 20 6d 6f 76  x7f);.    /* mov
b5990 65 64 20 43 53 45 32 20 75 70 20 2a 2f 0a 20 20  ed CSE2 up */.  
b59a0 20 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 3c    /* a &= (0x7f<
b59b0 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a  <14)|(0x7f); */.
b59c0 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20      a = a<<7;.  
b59d0 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 73 20    a |= b;.    s 
b59e0 3d 20 73 3e 3e 34 3b 0a 20 20 20 20 2a 76 20 3d  = s>>4;.    *v =
b59f0 20 28 28 75 36 34 29 73 29 3c 3c 33 32 20 7c 20   ((u64)s)<<32 | 
b5a00 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 38 3b  a;.    return 8;
b5a10 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61  .  }..  p++;.  a
b5a20 20 3d 20 61 3c 3c 31 35 3b 0a 20 20 61 20 7c 3d   = a<<15;.  a |=
b5a30 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 34 3c   *p;.  /* a: p4<
b5a40 3c 32 39 20 7c 20 70 36 3c 3c 31 35 20 7c 20 70  <29 | p6<<15 | p
b5a50 38 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a  8 (unmasked) */.
b5a60 0a 20 20 2f 2a 20 6d 6f 76 65 64 20 43 53 45 32  .  /* moved CSE2
b5a70 20 75 70 20 2a 2f 0a 20 20 2f 2a 20 61 20 26 3d   up */.  /* a &=
b5a80 20 28 30 78 37 66 3c 3c 32 39 29 7c 28 30 78 37   (0x7f<<29)|(0x7
b5a90 66 3c 3c 31 35 29 7c 28 30 78 66 66 29 3b 20 2a  f<<15)|(0xff); *
b5aa0 2f 0a 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c  /.  b &= (0x7f<<
b5ab0 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 62 20  14)|(0x7f);.  b 
b5ac0 3d 20 62 3c 3c 38 3b 0a 20 20 61 20 7c 3d 20 62  = b<<8;.  a |= b
b5ad0 3b 0a 0a 20 20 73 20 3d 20 73 3c 3c 34 3b 0a 20  ;..  s = s<<4;. 
b5ae0 20 62 20 3d 20 70 5b 2d 34 5d 3b 0a 20 20 62 20   b = p[-4];.  b 
b5af0 26 3d 20 30 78 37 66 3b 0a 20 20 62 20 3d 20 62  &= 0x7f;.  b = b
b5b00 3e 3e 33 3b 0a 20 20 73 20 7c 3d 20 62 3b 0a 0a  >>3;.  s |= b;..
b5b10 20 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c    *v = ((u64)s)<
b5b20 3c 33 32 20 7c 20 61 3b 0a 0a 20 20 72 65 74 75  <32 | a;..  retu
b5b30 72 6e 20 39 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  rn 9;.}../*.** R
b5b40 65 61 64 20 61 20 33 32 2d 62 69 74 20 76 61 72  ead a 32-bit var
b5b50 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74  iable-length int
b5b60 65 67 65 72 20 66 72 6f 6d 20 6d 65 6d 6f 72 79  eger from memory
b5b70 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 5b 30   starting at p[0
b5b80 5d 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  ]..** Return the
b5b90 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
b5ba0 20 72 65 61 64 2e 20 20 54 68 65 20 76 61 6c 75   read.  The valu
b5bb0 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 2a  e is stored in *
b5bc0 76 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  v..**.** If the 
b5bd0 76 61 72 69 6e 74 20 73 74 6f 72 65 64 20 69 6e  varint stored in
b5be0 20 70 5b 30 5d 20 69 73 20 6c 61 72 67 65 72 20   p[0] is larger 
b5bf0 74 68 61 6e 20 63 61 6e 20 66 69 74 20 69 6e 20  than can fit in 
b5c00 61 20 33 32 2d 62 69 74 20 75 6e 73 69 67 6e 65  a 32-bit unsigne
b5c10 64 0a 2a 2a 20 69 6e 74 65 67 65 72 2c 20 74 68  d.** integer, th
b5c20 65 6e 20 73 65 74 20 2a 76 20 74 6f 20 30 78 66  en set *v to 0xf
b5c30 66 66 66 66 66 66 66 2e 0a 2a 2a 0a 2a 2a 20 41  fffffff..**.** A
b5c40 20 4d 41 43 52 4f 20 76 65 72 73 69 6f 6e 2c 20   MACRO version, 
b5c50 67 65 74 56 61 72 69 6e 74 33 32 2c 20 69 73 20  getVarint32, is 
b5c60 70 72 6f 76 69 64 65 64 20 77 68 69 63 68 20 69  provided which i
b5c70 6e 6c 69 6e 65 73 20 74 68 65 20 0a 2a 2a 20 73  nlines the .** s
b5c80 69 6e 67 6c 65 2d 62 79 74 65 20 63 61 73 65 2e  ingle-byte case.
b5c90 20 20 41 6c 6c 20 63 6f 64 65 20 73 68 6f 75 6c    All code shoul
b5ca0 64 20 75 73 65 20 74 68 65 20 4d 41 43 52 4f 20  d use the MACRO 
b5cb0 76 65 72 73 69 6f 6e 20 61 73 20 0a 2a 2a 20 74  version as .** t
b5cc0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73  his function ass
b5cd0 75 6d 65 73 20 74 68 65 20 73 69 6e 67 6c 65 2d  umes the single-
b5ce0 62 79 74 65 20 63 61 73 65 20 68 61 73 20 61 6c  byte case has al
b5cf0 72 65 61 64 79 20 62 65 65 6e 20 68 61 6e 64 6c  ready been handl
b5d00 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
b5d10 49 56 41 54 45 20 75 38 20 73 71 6c 69 74 65 33  IVATE u8 sqlite3
b5d20 47 65 74 56 61 72 69 6e 74 33 32 28 63 6f 6e 73  GetVarint32(cons
b5d30 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
b5d40 2a 70 2c 20 75 33 32 20 2a 76 29 7b 0a 20 20 75  *p, u32 *v){.  u
b5d50 33 32 20 61 2c 62 3b 0a 0a 20 20 2f 2a 20 54 68  32 a,b;..  /* Th
b5d60 65 20 31 2d 62 79 74 65 20 63 61 73 65 2e 20 20  e 1-byte case.  
b5d70 4f 76 65 72 77 68 65 6c 6d 69 6e 67 6c 79 20 74  Overwhelmingly t
b5d80 68 65 20 6d 6f 73 74 20 63 6f 6d 6d 6f 6e 2e 20  he most common. 
b5d90 20 48 61 6e 64 6c 65 64 20 69 6e 6c 69 6e 65 0a   Handled inline.
b5da0 20 20 2a 2a 20 62 79 20 74 68 65 20 67 65 74 56    ** by the getV
b5db0 61 72 69 6e 33 32 28 29 20 6d 61 63 72 6f 20 2a  arin32() macro *
b5dc0 2f 0a 20 20 61 20 3d 20 2a 70 3b 0a 20 20 2f 2a  /.  a = *p;.  /*
b5dd0 20 61 3a 20 70 30 20 28 75 6e 6d 61 73 6b 65 64   a: p0 (unmasked
b5de0 29 20 2a 2f 0a 23 69 66 6e 64 65 66 20 67 65 74  ) */.#ifndef get
b5df0 56 61 72 69 6e 74 33 32 0a 20 20 69 66 20 28 21  Varint32.  if (!
b5e00 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20  (a&0x80)).  {.  
b5e10 20 20 2f 2a 20 56 61 6c 75 65 73 20 62 65 74 77    /* Values betw
b5e20 65 65 6e 20 30 20 61 6e 64 20 31 32 37 20 2a 2f  een 0 and 127 */
b5e30 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20  .    *v = a;.   
b5e40 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23   return 1;.  }.#
b5e50 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 54 68 65 20  endif..  /* The 
b5e60 32 2d 62 79 74 65 20 63 61 73 65 20 2a 2f 0a 20  2-byte case */. 
b5e70 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 2a 70 3b 0a   p++;.  b = *p;.
b5e80 20 20 2f 2a 20 62 3a 20 70 31 20 28 75 6e 6d 61    /* b: p1 (unma
b5e90 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21  sked) */.  if (!
b5ea0 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20  (b&0x80)).  {.  
b5eb0 20 20 2f 2a 20 56 61 6c 75 65 73 20 62 65 74 77    /* Values betw
b5ec0 65 65 6e 20 31 32 38 20 61 6e 64 20 31 36 33 38  een 128 and 1638
b5ed0 33 20 2a 2f 0a 20 20 20 20 61 20 26 3d 20 30 78  3 */.    a &= 0x
b5ee0 37 66 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37  7f;.    a = a<<7
b5ef0 3b 0a 20 20 20 20 2a 76 20 3d 20 61 20 7c 20 62  ;.    *v = a | b
b5f00 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a  ;.    return 2;.
b5f10 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 33 2d    }..  /* The 3-
b5f20 62 79 74 65 20 63 61 73 65 20 2a 2f 0a 20 20 70  byte case */.  p
b5f30 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b  ++;.  a = a<<14;
b5f40 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a  .  a |= *p;.  /*
b5f50 20 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32 20   a: p0<<14 | p2 
b5f60 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20  (unmasked) */.  
b5f70 69 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20  if (!(a&0x80)). 
b5f80 20 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65 73   {.    /* Values
b5f90 20 62 65 74 77 65 65 6e 20 31 36 33 38 34 20 61   between 16384 a
b5fa0 6e 64 20 32 30 39 37 31 35 31 20 2a 2f 0a 20 20  nd 2097151 */.  
b5fb0 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34    a &= (0x7f<<14
b5fc0 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62 20  )|(0x7f);.    b 
b5fd0 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 62 20 3d  &= 0x7f;.    b =
b5fe0 20 62 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20   b<<7;.    *v = 
b5ff0 61 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 72  a | b;.    retur
b6000 6e 20 33 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41  n 3;.  }..  /* A
b6010 20 33 32 2d 62 69 74 20 76 61 72 69 6e 74 20 69   32-bit varint i
b6020 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20  s used to store 
b6030 73 69 7a 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  size information
b6040 20 69 6e 20 62 74 72 65 65 73 2e 0a 20 20 2a 2a   in btrees..  **
b6050 20 4f 62 6a 65 63 74 73 20 61 72 65 20 72 61 72   Objects are rar
b6060 65 6c 79 20 6c 61 72 67 65 72 20 74 68 61 6e 20  ely larger than 
b6070 32 4d 69 42 20 6c 69 6d 69 74 20 6f 66 20 61 20  2MiB limit of a 
b6080 33 2d 62 79 74 65 20 76 61 72 69 6e 74 2e 0a 20  3-byte varint.. 
b6090 20 2a 2a 20 41 20 33 2d 62 79 74 65 20 76 61 72   ** A 3-byte var
b60a0 69 6e 74 20 69 73 20 73 75 66 66 69 63 69 65 6e  int is sufficien
b60b0 74 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20  t, for example, 
b60c0 74 6f 20 72 65 63 6f 72 64 20 74 68 65 20 73 69  to record the si
b60d0 7a 65 0a 20 20 2a 2a 20 6f 66 20 61 20 31 30 34  ze.  ** of a 104
b60e0 38 35 36 39 2d 62 79 74 65 20 42 4c 4f 42 20 6f  8569-byte BLOB o
b60f0 72 20 73 74 72 69 6e 67 2e 0a 20 20 2a 2a 0a 20  r string..  **. 
b6100 20 2a 2a 20 57 65 20 6f 6e 6c 79 20 75 6e 72 6f   ** We only unro
b6110 6c 6c 20 74 68 65 20 66 69 72 73 74 20 31 2d 2c  ll the first 1-,
b6120 20 32 2d 2c 20 61 6e 64 20 33 2d 20 62 79 74 65   2-, and 3- byte
b6130 20 63 61 73 65 73 2e 20 20 54 68 65 20 76 65 72   cases.  The ver
b6140 79 0a 20 20 2a 2a 20 72 61 72 65 20 6c 61 72 67  y.  ** rare larg
b6150 65 72 20 63 61 73 65 73 20 63 61 6e 20 62 65 20  er cases can be 
b6160 68 61 6e 64 6c 65 64 20 62 79 20 74 68 65 20 73  handled by the s
b6170 6c 6f 77 65 72 20 36 34 2d 62 69 74 20 76 61 72  lower 64-bit var
b6180 69 6e 74 0a 20 20 2a 2a 20 72 6f 75 74 69 6e 65  int.  ** routine
b6190 2e 0a 20 20 2a 2f 0a 23 69 66 20 31 0a 20 20 7b  ..  */.#if 1.  {
b61a0 0a 20 20 20 20 75 36 34 20 76 36 34 3b 0a 20 20  .    u64 v64;.  
b61b0 20 20 75 38 20 6e 3b 0a 0a 20 20 20 20 70 20 2d    u8 n;..    p -
b61c0 3d 20 32 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c  = 2;.    n = sql
b61d0 69 74 65 33 47 65 74 56 61 72 69 6e 74 28 70 2c  ite3GetVarint(p,
b61e0 20 26 76 36 34 29 3b 0a 20 20 20 20 61 73 73 65   &v64);.    asse
b61f0 72 74 28 20 6e 3e 33 20 26 26 20 6e 3c 3d 39 20  rt( n>3 && n<=9 
b6200 29 3b 0a 20 20 20 20 69 66 28 20 28 76 36 34 20  );.    if( (v64 
b6210 26 20 53 51 4c 49 54 45 5f 4d 41 58 5f 55 33 32  & SQLITE_MAX_U32
b6220 29 21 3d 76 36 34 20 29 7b 0a 20 20 20 20 20 20  )!=v64 ){.      
b6230 2a 76 20 3d 20 30 78 66 66 66 66 66 66 66 66 3b  *v = 0xffffffff;
b6240 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
b6250 20 20 2a 76 20 3d 20 28 75 33 32 29 76 36 34 3b    *v = (u32)v64;
b6260 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
b6270 6e 20 6e 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a  n n;.  }..#else.
b6280 20 20 2f 2a 20 46 6f 72 20 66 6f 6c 6c 6f 77 69    /* For followi
b6290 6e 67 20 63 6f 64 65 20 28 6b 65 70 74 20 66 6f  ng code (kept fo
b62a0 72 20 68 69 73 74 6f 72 69 63 61 6c 20 72 65 63  r historical rec
b62b0 6f 72 64 20 6f 6e 6c 79 29 20 73 68 6f 77 73 20  ord only) shows 
b62c0 61 6e 0a 20 20 2a 2a 20 75 6e 72 6f 6c 6c 69 6e  an.  ** unrollin
b62d0 67 20 66 6f 72 20 74 68 65 20 33 2d 20 61 6e 64  g for the 3- and
b62e0 20 34 2d 62 79 74 65 20 76 61 72 69 6e 74 20 63   4-byte varint c
b62f0 61 73 65 73 2e 20 20 54 68 69 73 20 63 6f 64 65  ases.  This code
b6300 20 69 73 0a 20 20 2a 2a 20 73 6c 69 67 68 74 6c   is.  ** slightl
b6310 79 20 66 61 73 74 65 72 2c 20 62 75 74 20 69 74  y faster, but it
b6320 20 69 73 20 61 6c 73 6f 20 6c 61 72 67 65 72 20   is also larger 
b6330 61 6e 64 20 6d 75 63 68 20 68 61 72 64 65 72 20  and much harder 
b6340 74 6f 20 74 65 73 74 2e 0a 20 20 2a 2f 0a 20 20  to test..  */.  
b6350 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34  p++;.  b = b<<14
b6360 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f  ;.  b |= *p;.  /
b6370 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20 70 33  * b: p1<<14 | p3
b6380 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20   (unmasked) */. 
b6390 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29 0a   if (!(b&0x80)).
b63a0 20 20 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65    {.    /* Value
b63b0 73 20 62 65 74 77 65 65 6e 20 32 30 39 37 31 35  s between 209715
b63c0 32 20 61 6e 64 20 32 36 38 34 33 35 34 35 35 20  2 and 268435455 
b63d0 2a 2f 0a 20 20 20 20 62 20 26 3d 20 28 30 78 37  */.    b &= (0x7
b63e0 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20  f<<14)|(0x7f);. 
b63f0 20 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31     a &= (0x7f<<1
b6400 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 61  4)|(0x7f);.    a
b6410 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 2a 76 20   = a<<7;.    *v 
b6420 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20 72 65 74  = a | b;.    ret
b6430 75 72 6e 20 34 3b 0a 20 20 7d 0a 0a 20 20 70 2b  urn 4;.  }..  p+
b6440 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a  +;.  a = a<<14;.
b6450 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20    a |= *p;.  /* 
b6460 61 3a 20 70 30 3c 3c 32 38 20 7c 20 70 32 3c 3c  a: p0<<28 | p2<<
b6470 31 34 20 7c 20 70 34 20 28 75 6e 6d 61 73 6b 65  14 | p4 (unmaske
b6480 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26  d) */.  if (!(a&
b6490 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f  0x80)).  {.    /
b64a0 2a 20 57 61 6c 75 65 73 20 20 62 65 74 77 65 65  * Walues  betwee
b64b0 6e 20 32 36 38 34 33 35 34 35 36 20 61 6e 64 20  n 268435456 and 
b64c0 33 34 33 35 39 37 33 38 33 36 37 20 2a 2f 0a 20  34359738367 */. 
b64d0 20 20 20 61 20 26 3d 20 28 30 78 31 66 3c 3c 32     a &= (0x1f<<2
b64e0 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30  8)|(0x7f<<14)|(0
b64f0 78 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 28  x7f);.    b &= (
b6500 30 78 31 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c  0x1f<<28)|(0x7f<
b6510 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20  <14)|(0x7f);.   
b6520 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 2a   b = b<<7;.    *
b6530 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20 72  v = a | b;.    r
b6540 65 74 75 72 6e 20 35 3b 0a 20 20 7d 0a 0a 20 20  eturn 5;.  }..  
b6550 2f 2a 20 57 65 20 63 61 6e 20 6f 6e 6c 79 20 72  /* We can only r
b6560 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74 20  each this point 
b6570 77 68 65 6e 20 72 65 61 64 69 6e 67 20 61 20 63  when reading a c
b6580 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65 0a  orrupt database.
b6590 20 20 2a 2a 20 66 69 6c 65 2e 20 20 49 6e 20 74    ** file.  In t
b65a0 68 61 74 20 63 61 73 65 20 77 65 20 61 72 65 20  hat case we are 
b65b0 6e 6f 74 20 69 6e 20 61 6e 79 20 68 75 72 72 79  not in any hurry
b65c0 2e 20 20 55 73 65 20 74 68 65 20 28 72 65 6c 61  .  Use the (rela
b65d0 74 69 76 65 6c 79 0a 20 20 2a 2a 20 73 6c 6f 77  tively.  ** slow
b65e0 29 20 67 65 6e 65 72 61 6c 2d 70 75 72 70 6f 73  ) general-purpos
b65f0 65 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69  e sqlite3GetVari
b6600 6e 74 28 29 20 72 6f 75 74 69 6e 65 20 74 6f 20  nt() routine to 
b6610 65 78 74 72 61 63 74 20 74 68 65 0a 20 20 2a 2a  extract the.  **
b6620 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 7b 0a 20   value. */.  {. 
b6630 20 20 20 75 36 34 20 76 36 34 3b 0a 20 20 20 20     u64 v64;.    
b6640 75 38 20 6e 3b 0a 0a 20 20 20 20 70 20 2d 3d 20  u8 n;..    p -= 
b6650 34 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 74  4;.    n = sqlit
b6660 65 33 47 65 74 56 61 72 69 6e 74 28 70 2c 20 26  e3GetVarint(p, &
b6670 76 36 34 29 3b 0a 20 20 20 20 61 73 73 65 72 74  v64);.    assert
b6680 28 20 6e 3e 35 20 26 26 20 6e 3c 3d 39 20 29 3b  ( n>5 && n<=9 );
b6690 0a 20 20 20 20 2a 76 20 3d 20 28 75 33 32 29 76  .    *v = (u32)v
b66a0 36 34 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 6e  64;.    return n
b66b0 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a  ;.  }.#endif.}..
b66c0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
b66d0 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
b66e0 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 6e 65   that will be ne
b66f0 65 64 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68  eded to store th
b6700 65 20 67 69 76 65 6e 0a 2a 2a 20 36 34 2d 62 69  e given.** 64-bi
b6710 74 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 53 51  t integer..*/.SQ
b6720 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
b6730 20 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65   sqlite3VarintLe
b6740 6e 28 75 36 34 20 76 29 7b 0a 20 20 69 6e 74 20  n(u64 v){.  int 
b6750 69 20 3d 20 30 3b 0a 20 20 64 6f 7b 0a 20 20 20  i = 0;.  do{.   
b6760 20 69 2b 2b 3b 0a 20 20 20 20 76 20 3e 3e 3d 20   i++;.    v >>= 
b6770 37 3b 0a 20 20 7d 77 68 69 6c 65 28 20 76 21 3d  7;.  }while( v!=
b6780 30 20 26 26 20 41 4c 57 41 59 53 28 69 3c 39 29  0 && ALWAYS(i<9)
b6790 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a   );.  return i;.
b67a0 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 6f  }.../*.** Read o
b67b0 72 20 77 72 69 74 65 20 61 20 66 6f 75 72 2d 62  r write a four-b
b67c0 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69  yte big-endian i
b67d0 6e 74 65 67 65 72 20 76 61 6c 75 65 2e 0a 2a 2f  nteger value..*/
b67e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b67f0 75 33 32 20 73 71 6c 69 74 65 33 47 65 74 34 62  u32 sqlite3Get4b
b6800 79 74 65 28 63 6f 6e 73 74 20 75 38 20 2a 70 29  yte(const u8 *p)
b6810 7b 0a 20 20 72 65 74 75 72 6e 20 28 70 5b 30 5d  {.  return (p[0]
b6820 3c 3c 32 34 29 20 7c 20 28 70 5b 31 5d 3c 3c 31  <<24) | (p[1]<<1
b6830 36 29 20 7c 20 28 70 5b 32 5d 3c 3c 38 29 20 7c  6) | (p[2]<<8) |
b6840 20 70 5b 33 5d 3b 0a 7d 0a 53 51 4c 49 54 45 5f   p[3];.}.SQLITE_
b6850 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
b6860 69 74 65 33 50 75 74 34 62 79 74 65 28 75 6e 73  ite3Put4byte(uns
b6870 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75  igned char *p, u
b6880 33 32 20 76 29 7b 0a 20 20 70 5b 30 5d 20 3d 20  32 v){.  p[0] = 
b6890 28 75 38 29 28 76 3e 3e 32 34 29 3b 0a 20 20 70  (u8)(v>>24);.  p
b68a0 5b 31 5d 20 3d 20 28 75 38 29 28 76 3e 3e 31 36  [1] = (u8)(v>>16
b68b0 29 3b 0a 20 20 70 5b 32 5d 20 3d 20 28 75 38 29  );.  p[2] = (u8)
b68c0 28 76 3e 3e 38 29 3b 0a 20 20 70 5b 33 5d 20 3d  (v>>8);.  p[3] =
b68d0 20 28 75 38 29 76 3b 0a 7d 0a 0a 0a 0a 23 69 66   (u8)v;.}....#if
b68e0 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
b68f0 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52  _OMIT_BLOB_LITER
b6900 41 4c 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  AL) || defined(S
b6910 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 29  QLITE_HAS_CODEC)
b6920 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61 74 65  ./*.** Translate
b6930 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 6f   a single byte o
b6940 66 20 48 65 78 20 69 6e 74 6f 20 61 6e 20 69 6e  f Hex into an in
b6950 74 65 67 65 72 2e 0a 2a 2a 20 54 68 69 73 20 72  teger..** This r
b6960 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b  outine only work
b6970 73 20 69 66 20 68 20 72 65 61 6c 6c 79 20 69 73  s if h really is
b6980 20 61 20 76 61 6c 69 64 20 68 65 78 61 64 65 63   a valid hexadec
b6990 69 6d 61 6c 0a 2a 2a 20 63 68 61 72 61 63 74 65  imal.** characte
b69a0 72 3a 20 20 30 2e 2e 39 61 2e 2e 66 41 2e 2e 46  r:  0..9a..fA..F
b69b0 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 68 65  .*/.static u8 he
b69c0 78 54 6f 49 6e 74 28 69 6e 74 20 68 29 7b 0a 20  xToInt(int h){. 
b69d0 20 61 73 73 65 72 74 28 20 28 68 3e 3d 27 30 27   assert( (h>='0'
b69e0 20 26 26 20 68 3c 3d 27 39 27 29 20 7c 7c 20 20   && h<='9') ||  
b69f0 28 68 3e 3d 27 61 27 20 26 26 20 68 3c 3d 27 66  (h>='a' && h<='f
b6a00 27 29 20 7c 7c 20 20 28 68 3e 3d 27 41 27 20 26  ') ||  (h>='A' &
b6a10 26 20 68 3c 3d 27 46 27 29 20 29 3b 0a 23 69 66  & h<='F') );.#if
b6a20 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49  def SQLITE_ASCII
b6a30 0a 20 20 68 20 2b 3d 20 39 2a 28 31 26 28 68 3e  .  h += 9*(1&(h>
b6a40 3e 36 29 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66  >6));.#endif.#if
b6a50 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44 49  def SQLITE_EBCDI
b6a60 43 0a 20 20 68 20 2b 3d 20 39 2a 28 31 26 7e 28  C.  h += 9*(1&~(
b6a70 68 3e 3e 34 29 29 3b 0a 23 65 6e 64 69 66 0a 20  h>>4));.#endif. 
b6a80 20 72 65 74 75 72 6e 20 28 75 38 29 28 68 20 26   return (u8)(h &
b6a90 20 30 78 66 29 3b 0a 7d 0a 23 65 6e 64 69 66 20   0xf);.}.#endif 
b6aa0 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  /* !SQLITE_OMIT_
b6ab0 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 20 7c 7c 20  BLOB_LITERAL || 
b6ac0 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43  SQLITE_HAS_CODEC
b6ad0 20 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65   */..#if !define
b6ae0 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c  d(SQLITE_OMIT_BL
b6af0 4f 42 5f 4c 49 54 45 52 41 4c 29 20 7c 7c 20 64  OB_LITERAL) || d
b6b00 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 48 41  efined(SQLITE_HA
b6b10 53 5f 43 4f 44 45 43 29 0a 2f 2a 0a 2a 2a 20 43  S_CODEC)./*.** C
b6b20 6f 6e 76 65 72 74 20 61 20 42 4c 4f 42 20 6c 69  onvert a BLOB li
b6b30 74 65 72 61 6c 20 6f 66 20 74 68 65 20 66 6f 72  teral of the for
b6b40 6d 20 22 78 27 68 68 68 68 68 68 27 22 20 69 6e  m "x'hhhhhh'" in
b6b50 74 6f 20 69 74 73 20 62 69 6e 61 72 79 0a 2a 2a  to its binary.**
b6b60 20 76 61 6c 75 65 2e 20 20 52 65 74 75 72 6e 20   value.  Return 
b6b70 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 73  a pointer to its
b6b80 20 62 69 6e 61 72 79 20 76 61 6c 75 65 2e 20 20   binary value.  
b6b90 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68  Space to hold th
b6ba0 65 0a 2a 2a 20 62 69 6e 61 72 79 20 76 61 6c 75  e.** binary valu
b6bb0 65 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69  e has been obtai
b6bc0 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 20  ned from malloc 
b6bd0 61 6e 64 20 6d 75 73 74 20 62 65 20 66 72 65 65  and must be free
b6be0 64 20 62 79 0a 2a 2a 20 74 68 65 20 63 61 6c 6c  d by.** the call
b6bf0 69 6e 67 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a  ing routine..*/.
b6c00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
b6c10 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 65 78 54  oid *sqlite3HexT
b6c20 6f 42 6c 6f 62 28 73 71 6c 69 74 65 33 20 2a 64  oBlob(sqlite3 *d
b6c30 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
b6c40 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 63 68 61 72  , int n){.  char
b6c50 20 2a 7a 42 6c 6f 62 3b 0a 20 20 69 6e 74 20 69   *zBlob;.  int i
b6c60 3b 0a 0a 20 20 7a 42 6c 6f 62 20 3d 20 28 63 68  ;..  zBlob = (ch
b6c70 61 72 20 2a 29 73 71 6c 69 74 65 33 44 62 4d 61  ar *)sqlite3DbMa
b6c80 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 2f 32 20  llocRaw(db, n/2 
b6c90 2b 20 31 29 3b 0a 20 20 6e 2d 2d 3b 0a 20 20 69  + 1);.  n--;.  i
b6ca0 66 28 20 7a 42 6c 6f 62 20 29 7b 0a 20 20 20 20  f( zBlob ){.    
b6cb0 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b  for(i=0; i<n; i+
b6cc0 3d 32 29 7b 0a 20 20 20 20 20 20 7a 42 6c 6f 62  =2){.      zBlob
b6cd0 5b 69 2f 32 5d 20 3d 20 28 68 65 78 54 6f 49 6e  [i/2] = (hexToIn
b6ce0 74 28 7a 5b 69 5d 29 3c 3c 34 29 20 7c 20 68 65  t(z[i])<<4) | he
b6cf0 78 54 6f 49 6e 74 28 7a 5b 69 2b 31 5d 29 3b 0a  xToInt(z[i+1]);.
b6d00 20 20 20 20 7d 0a 20 20 20 20 7a 42 6c 6f 62 5b      }.    zBlob[
b6d10 69 2f 32 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  i/2] = 0;.  }.  
b6d20 72 65 74 75 72 6e 20 7a 42 6c 6f 62 3b 0a 7d 0a  return zBlob;.}.
b6d30 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54  #endif /* !SQLIT
b6d40 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45  E_OMIT_BLOB_LITE
b6d50 52 41 4c 20 7c 7c 20 53 51 4c 49 54 45 5f 48 41  RAL || SQLITE_HA
b6d60 53 5f 43 4f 44 45 43 20 2a 2f 0a 0a 0a 2f 2a 0a  S_CODEC */.../*.
b6d70 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 73 71  ** Change the sq
b6d80 6c 69 74 65 2e 6d 61 67 69 63 20 66 72 6f 6d 20  lite.magic from 
b6d90 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45  SQLITE_MAGIC_OPE
b6da0 4e 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49  N to SQLITE_MAGI
b6db0 43 5f 42 55 53 59 2e 0a 2a 2a 20 52 65 74 75 72  C_BUSY..** Retur
b6dc0 6e 20 61 6e 20 65 72 72 6f 72 20 28 6e 6f 6e 2d  n an error (non-
b6dd0 7a 65 72 6f 29 20 69 66 20 74 68 65 20 6d 61 67  zero) if the mag
b6de0 69 63 20 77 61 73 20 6e 6f 74 20 53 51 4c 49 54  ic was not SQLIT
b6df0 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a 20  E_MAGIC_OPEN.** 
b6e00 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  when this routin
b6e10 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a  e is called..**.
b6e20 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
b6e30 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 65  is called when e
b6e40 6e 74 65 72 69 6e 67 20 61 6e 20 53 51 4c 69 74  ntering an SQLit
b6e50 65 20 41 50 49 2e 20 20 54 68 65 20 53 51 4c 49  e API.  The SQLI
b6e60 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a  TE_MAGIC_OPEN.**
b6e70 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73   value indicates
b6e80 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61   that the databa
b6e90 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 61  se connection pa
b6ea0 73 73 65 64 20 69 6e 74 6f 20 74 68 65 20 41 50  ssed into the AP
b6eb0 49 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 61 6e 64  I is.** open and
b6ec0 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 75 73   is not being us
b6ed0 65 64 20 62 79 20 61 6e 6f 74 68 65 72 20 74 68  ed by another th
b6ee0 72 65 61 64 2e 20 20 42 79 20 63 68 61 6e 67 69  read.  By changi
b6ef0 6e 67 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20  ng the value.** 
b6f00 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f  to SQLITE_MAGIC_
b6f10 42 55 53 59 20 77 65 20 69 6e 64 69 63 61 74 65  BUSY we indicate
b6f20 20 74 68 61 74 20 74 68 65 20 63 6f 6e 6e 65 63   that the connec
b6f30 74 69 6f 6e 20 69 73 20 69 6e 20 75 73 65 2e 0a  tion is in use..
b6f40 2a 2a 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  ** sqlite3Safety
b6f50 4f 66 66 28 29 20 62 65 6c 6f 77 20 77 69 6c 6c  Off() below will
b6f60 20 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75   change the valu
b6f70 65 20 62 61 63 6b 20 74 6f 20 53 51 4c 49 54 45  e back to SQLITE
b6f80 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a 20 77  _MAGIC_OPEN.** w
b6f90 68 65 6e 20 74 68 65 20 41 50 49 20 65 78 69 74  hen the API exit
b6fa0 73 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  s. .**.** This r
b6fb0 6f 75 74 69 6e 65 20 69 73 20 61 20 61 74 74 65  outine is a atte
b6fc0 6d 70 74 20 74 6f 20 64 65 74 65 63 74 20 69 66  mpt to detect if
b6fd0 20 74 77 6f 20 74 68 72 65 61 64 73 20 75 73 65   two threads use
b6fe0 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 73 71 6c   the.** same sql
b6ff0 69 74 65 2a 20 70 6f 69 6e 74 65 72 20 61 74 20  ite* pointer at 
b7000 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 20 20  the same time.  
b7010 54 68 65 72 65 20 69 73 20 61 20 72 61 63 65 20  There is a race 
b7020 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e 20 73 6f  .** condition so
b7030 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20   it is possible 
b7040 74 68 61 74 20 74 68 65 20 65 72 72 6f 72 20 69  that the error i
b7050 73 20 6e 6f 74 20 64 65 74 65 63 74 65 64 2e 0a  s not detected..
b7060 2a 2a 20 42 75 74 20 75 73 75 61 6c 6c 79 20 74  ** But usually t
b7070 68 65 20 70 72 6f 62 6c 65 6d 20 77 69 6c 6c 20  he problem will 
b7080 62 65 20 73 65 65 6e 2e 20 20 54 68 65 20 72 65  be seen.  The re
b7090 73 75 6c 74 20 77 69 6c 6c 20 62 65 20 61 6e 0a  sult will be an.
b70a0 2a 2a 20 65 72 72 6f 72 20 77 68 69 63 68 20 63  ** error which c
b70b0 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 64 65  an be used to de
b70c0 62 75 67 20 74 68 65 20 61 70 70 6c 69 63 61 74  bug the applicat
b70d0 69 6f 6e 20 74 68 61 74 20 69 73 0a 2a 2a 20 75  ion that is.** u
b70e0 73 69 6e 67 20 53 51 4c 69 74 65 20 69 6e 63 6f  sing SQLite inco
b70f0 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54  rrectly..**.** T
b7100 69 63 6b 65 74 20 23 32 30 32 3a 20 20 49 66 20  icket #202:  If 
b7110 64 62 2d 3e 6d 61 67 69 63 20 69 73 20 6e 6f 74  db->magic is not
b7120 20 61 20 76 61 6c 69 64 20 6f 70 65 6e 20 76 61   a valid open va
b7130 6c 75 65 2c 20 74 61 6b 65 20 63 61 72 65 20 6e  lue, take care n
b7140 6f 74 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66 79 20  ot.** to modify 
b7150 74 68 65 20 64 62 20 73 74 72 75 63 74 75 72 65  the db structure
b7160 20 61 74 20 61 6c 6c 2e 20 20 49 74 20 63 6f 75   at all.  It cou
b7170 6c 64 20 62 65 20 74 68 61 74 20 64 62 20 69 73  ld be that db is
b7180 20 61 20 73 74 61 6c 65 0a 2a 2a 20 70 6f 69 6e   a stale.** poin
b7190 74 65 72 2e 20 20 49 6e 20 6f 74 68 65 72 20 77  ter.  In other w
b71a0 6f 72 64 73 2c 20 69 74 20 63 6f 75 6c 64 20 62  ords, it could b
b71b0 65 20 74 68 61 74 20 74 68 65 72 65 20 68 61 73  e that there has
b71c0 20 62 65 65 6e 20 61 20 70 72 69 6f 72 0a 2a 2a   been a prior.**
b71d0 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
b71e0 5f 63 6c 6f 73 65 28 64 62 29 20 61 6e 64 20 64  _close(db) and d
b71f0 62 20 68 61 73 20 62 65 65 6e 20 64 65 61 6c 6c  b has been deall
b7200 6f 63 61 74 65 64 2e 20 20 41 6e 64 20 77 65 20  ocated.  And we 
b7210 64 6f 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74  do.** not want t
b7220 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 64 65 61  o write into dea
b7230 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e  llocated memory.
b7240 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
b7250 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50  E_DEBUG.SQLITE_P
b7260 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
b7270 65 33 53 61 66 65 74 79 4f 6e 28 73 71 6c 69 74  e3SafetyOn(sqlit
b7280 65 33 20 2a 64 62 29 7b 0a 20 20 69 66 28 20 64  e3 *db){.  if( d
b7290 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45  b->magic==SQLITE
b72a0 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 29 7b 0a 20  _MAGIC_OPEN ){. 
b72b0 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53     db->magic = S
b72c0 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59  QLITE_MAGIC_BUSY
b72d0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71  ;.    assert( sq
b72e0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
b72f0 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  (db->mutex) );. 
b7300 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
b7310 65 6c 73 65 20 69 66 28 20 64 62 2d 3e 6d 61 67  else if( db->mag
b7320 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43  ic==SQLITE_MAGIC
b7330 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 64 62 2d  _BUSY ){.    db-
b7340 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f  >magic = SQLITE_
b7350 4d 41 47 49 43 5f 45 52 52 4f 52 3b 0a 20 20 20  MAGIC_ERROR;.   
b7360 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72   db->u1.isInterr
b7370 75 70 74 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20  upted = 1;.  }. 
b7380 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e   return 1;.}.#en
b7390 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67  dif../*.** Chang
b73a0 65 20 74 68 65 20 6d 61 67 69 63 20 66 72 6f 6d  e the magic from
b73b0 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55   SQLITE_MAGIC_BU
b73c0 53 59 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47  SY to SQLITE_MAG
b73d0 49 43 5f 4f 50 45 4e 2e 0a 2a 2a 20 52 65 74 75  IC_OPEN..** Retu
b73e0 72 6e 20 61 6e 20 65 72 72 6f 72 20 28 6e 6f 6e  rn an error (non
b73f0 2d 7a 65 72 6f 29 20 69 66 20 74 68 65 20 6d 61  -zero) if the ma
b7400 67 69 63 20 77 61 73 20 6e 6f 74 20 53 51 4c 49  gic was not SQLI
b7410 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 0a 2a 2a  TE_MAGIC_BUSY.**
b7420 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   when this routi
b7430 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f  ne is called..*/
b7440 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
b7450 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56  EBUG.SQLITE_PRIV
b7460 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53  ATE int sqlite3S
b7470 61 66 65 74 79 4f 66 66 28 73 71 6c 69 74 65 33  afetyOff(sqlite3
b7480 20 2a 64 62 29 7b 0a 20 20 69 66 28 20 64 62 2d   *db){.  if( db-
b7490 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d  >magic==SQLITE_M
b74a0 41 47 49 43 5f 42 55 53 59 20 29 7b 0a 20 20 20  AGIC_BUSY ){.   
b74b0 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c   db->magic = SQL
b74c0 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 3b 0a  ITE_MAGIC_OPEN;.
b74d0 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
b74e0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64  te3_mutex_held(d
b74f0 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20  b->mutex) );.   
b7500 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c   return 0;.  }el
b7510 73 65 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69  se{.    db->magi
b7520 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43  c = SQLITE_MAGIC
b7530 5f 45 52 52 4f 52 3b 0a 20 20 20 20 64 62 2d 3e  _ERROR;.    db->
b7540 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64  u1.isInterrupted
b7550 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e   = 1;.    return
b7560 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66   1;.  }.}.#endif
b7570 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f  ../*.** Check to
b7580 20 6d 61 6b 65 20 73 75 72 65 20 77 65 20 68 61   make sure we ha
b7590 76 65 20 61 20 76 61 6c 69 64 20 64 62 20 70 6f  ve a valid db po
b75a0 69 6e 74 65 72 2e 20 20 54 68 69 73 20 74 65 73  inter.  This tes
b75b0 74 20 69 73 20 6e 6f 74 0a 2a 2a 20 66 6f 6f 6c  t is not.** fool
b75c0 70 72 6f 6f 66 20 62 75 74 20 69 74 20 64 6f 65  proof but it doe
b75d0 73 20 70 72 6f 76 69 64 65 20 73 6f 6d 65 20 6d  s provide some m
b75e0 65 61 73 75 72 65 20 6f 66 20 70 72 6f 74 65 63  easure of protec
b75f0 74 69 6f 6e 20 61 67 61 69 6e 73 74 0a 2a 2a 20  tion against.** 
b7600 6d 69 73 75 73 65 20 6f 66 20 74 68 65 20 69 6e  misuse of the in
b7610 74 65 72 66 61 63 65 20 73 75 63 68 20 61 73 20  terface such as 
b7620 70 61 73 73 69 6e 67 20 69 6e 20 64 62 20 70 6f  passing in db po
b7630 69 6e 74 65 72 73 20 74 68 61 74 20 61 72 65 0a  inters that are.
b7640 2a 2a 20 4e 55 4c 4c 20 6f 72 20 77 68 69 63 68  ** NULL or which
b7650 20 68 61 76 65 20 62 65 65 6e 20 70 72 65 76 69   have been previ
b7660 6f 75 73 6c 79 20 63 6c 6f 73 65 64 2e 20 20 49  ously closed.  I
b7670 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72  f this routine r
b7680 65 74 75 72 6e 73 0a 2a 2a 20 31 20 69 74 20 6d  eturns.** 1 it m
b7690 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 64 62  eans that the db
b76a0 20 70 6f 69 6e 74 65 72 20 69 73 20 76 61 6c 69   pointer is vali
b76b0 64 20 61 6e 64 20 30 20 69 66 20 69 74 20 73 68  d and 0 if it sh
b76c0 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a 20 64  ould not be.** d
b76d0 65 72 65 66 65 72 65 6e 63 65 64 20 66 6f 72 20  ereferenced for 
b76e0 61 6e 79 20 72 65 61 73 6f 6e 2e 20 20 54 68 65  any reason.  The
b76f0 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f   calling functio
b7700 6e 20 73 68 6f 75 6c 64 20 69 6e 76 6f 6b 65 0a  n should invoke.
b7710 2a 2a 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  ** SQLITE_MISUSE
b7720 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 2a 2a   immediately..**
b7730 0a 2a 2a 20 73 71 6c 69 74 65 33 53 61 66 65 74  .** sqlite3Safet
b7740 79 43 68 65 63 6b 4f 6b 28 29 20 72 65 71 75 69  yCheckOk() requi
b7750 72 65 73 20 74 68 61 74 20 74 68 65 20 64 62 20  res that the db 
b7760 70 6f 69 6e 74 65 72 20 62 65 20 76 61 6c 69 64  pointer be valid
b7770 20 66 6f 72 0a 2a 2a 20 75 73 65 2e 20 20 73 71   for.** use.  sq
b7780 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b  lite3SafetyCheck
b7790 53 69 63 6b 4f 72 4f 6b 28 29 20 61 6c 6c 6f 77  SickOrOk() allow
b77a0 73 20 61 20 64 62 20 70 6f 69 6e 74 65 72 20 74  s a db pointer t
b77b0 68 61 74 20 66 61 69 6c 65 64 20 74 6f 0a 2a 2a  hat failed to.**
b77c0 20 6f 70 65 6e 20 70 72 6f 70 65 72 6c 79 20 61   open properly a
b77d0 6e 64 20 69 73 20 6e 6f 74 20 66 69 74 20 66 6f  nd is not fit fo
b77e0 72 20 67 65 6e 65 72 61 6c 20 75 73 65 20 62 75  r general use bu
b77f0 74 20 77 68 69 63 68 20 63 61 6e 20 62 65 0a 2a  t which can be.*
b7800 2a 20 75 73 65 64 20 61 73 20 61 6e 20 61 72 67  * used as an arg
b7810 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33  ument to sqlite3
b7820 5f 65 72 72 6d 73 67 28 29 20 6f 72 20 73 71 6c  _errmsg() or sql
b7830 69 74 65 33 5f 63 6c 6f 73 65 28 29 2e 0a 2a 2f  ite3_close()..*/
b7840 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b7850 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74  int sqlite3Safet
b7860 79 43 68 65 63 6b 4f 6b 28 73 71 6c 69 74 65 33  yCheckOk(sqlite3
b7870 20 2a 64 62 29 7b 0a 20 20 75 33 32 20 6d 61 67   *db){.  u32 mag
b7880 69 63 3b 0a 20 20 69 66 28 20 64 62 3d 3d 30 20  ic;.  if( db==0 
b7890 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 6d 61  ) return 0;.  ma
b78a0 67 69 63 20 3d 20 64 62 2d 3e 6d 61 67 69 63 3b  gic = db->magic;
b78b0 0a 20 20 69 66 28 20 6d 61 67 69 63 21 3d 53 51  .  if( magic!=SQ
b78c0 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20  LITE_MAGIC_OPEN 
b78d0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
b78e0 45 42 55 47 0a 20 20 20 20 20 26 26 20 6d 61 67  EBUG.     && mag
b78f0 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43  ic!=SQLITE_MAGIC
b7900 5f 42 55 53 59 0a 23 65 6e 64 69 66 0a 20 20 29  _BUSY.#endif.  )
b7910 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
b7920 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
b7930 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 53 51 4c  urn 1;.  }.}.SQL
b7940 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
b7950 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65  sqlite3SafetyChe
b7960 63 6b 53 69 63 6b 4f 72 4f 6b 28 73 71 6c 69 74  ckSickOrOk(sqlit
b7970 65 33 20 2a 64 62 29 7b 0a 20 20 75 33 32 20 6d  e3 *db){.  u32 m
b7980 61 67 69 63 3b 0a 20 20 6d 61 67 69 63 20 3d 20  agic;.  magic = 
b7990 64 62 2d 3e 6d 61 67 69 63 3b 0a 20 20 69 66 28  db->magic;.  if(
b79a0 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d   magic!=SQLITE_M
b79b0 41 47 49 43 5f 53 49 43 4b 20 26 26 0a 20 20 20  AGIC_SICK &&.   
b79c0 20 20 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45     magic!=SQLITE
b79d0 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 26 26 0a 20  _MAGIC_OPEN &&. 
b79e0 20 20 20 20 20 6d 61 67 69 63 21 3d 53 51 4c 49       magic!=SQLI
b79f0 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 20  TE_MAGIC_BUSY ) 
b7a00 72 65 74 75 72 6e 20 30 3b 0a 20 20 72 65 74 75  return 0;.  retu
b7a10 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  rn 1;.}../******
b7a20 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
b7a30 75 74 69 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  util.c *********
b7a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b7a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b7a60 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
b7a70 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
b7a80 69 6c 65 20 68 61 73 68 2e 63 20 2a 2a 2a 2a 2a  ile hash.c *****
b7a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b7aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b7ab0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
b7ac0 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 32 32  001 September 22
b7ad0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
b7ae0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
b7af0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
b7b00 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
b7b10 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
b7b20 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
b7b30 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
b7b40 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
b7b50 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
b7b60 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
b7b70 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
b7b80 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
b7b90 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
b7ba0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
b7bb0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
b7bc0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
b7bd0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
b7be0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
b7bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b7c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b7c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b7c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
b7c30 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 69  ** This is the i
b7c40 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
b7c50 20 67 65 6e 65 72 69 63 20 68 61 73 68 2d 74 61   generic hash-ta
b7c60 62 6c 65 73 0a 2a 2a 20 75 73 65 64 20 69 6e 20  bles.** used in 
b7c70 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49  SQLite..**.** $I
b7c80 64 3a 20 68 61 73 68 2e 63 2c 76 20 31 2e 33 38  d: hash.c,v 1.38
b7c90 20 32 30 30 39 2f 30 35 2f 30 39 20 32 33 3a 32   2009/05/09 23:2
b7ca0 39 3a 31 32 20 64 72 68 20 45 78 70 20 24 0a 2a  9:12 drh Exp $.*
b7cb0 2f 0a 0a 2f 2a 20 54 75 72 6e 20 62 75 6c 6b 20  /../* Turn bulk 
b7cc0 6d 65 6d 6f 72 79 20 69 6e 74 6f 20 61 20 68 61  memory into a ha
b7cd0 73 68 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 20  sh table object 
b7ce0 62 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20  by initializing 
b7cf0 74 68 65 0a 2a 2a 20 66 69 65 6c 64 73 20 6f 66  the.** fields of
b7d00 20 74 68 65 20 48 61 73 68 20 73 74 72 75 63 74   the Hash struct
b7d10 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 22 70 4e 65 77  ure..**.** "pNew
b7d20 22 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  " is a pointer t
b7d30 6f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  o the hash table
b7d40 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 20 69   that is to be i
b7d50 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2f 0a 53  nitialized..*/.S
b7d60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
b7d70 69 64 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e  id sqlite3HashIn
b7d80 69 74 28 48 61 73 68 20 2a 70 4e 65 77 29 7b 0a  it(Hash *pNew){.
b7d90 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 21 3d    assert( pNew!=
b7da0 30 20 29 3b 0a 20 20 70 4e 65 77 2d 3e 66 69 72  0 );.  pNew->fir
b7db0 73 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e  st = 0;.  pNew->
b7dc0 63 6f 75 6e 74 20 3d 20 30 3b 0a 20 20 70 4e 65  count = 0;.  pNe
b7dd0 77 2d 3e 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20  w->htsize = 0;. 
b7de0 20 70 4e 65 77 2d 3e 68 74 20 3d 20 30 3b 0a 7d   pNew->ht = 0;.}
b7df0 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 6c 6c 20  ../* Remove all 
b7e00 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 61 20 68  entries from a h
b7e10 61 73 68 20 74 61 62 6c 65 2e 20 20 52 65 63 6c  ash table.  Recl
b7e20 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 2e 0a  aim all memory..
b7e30 2a 2a 20 43 61 6c 6c 20 74 68 69 73 20 72 6f 75  ** Call this rou
b7e40 74 69 6e 65 20 74 6f 20 64 65 6c 65 74 65 20 61  tine to delete a
b7e50 20 68 61 73 68 20 74 61 62 6c 65 20 6f 72 20 74   hash table or t
b7e60 6f 20 72 65 73 65 74 20 61 20 68 61 73 68 20 74  o reset a hash t
b7e70 61 62 6c 65 0a 2a 2a 20 74 6f 20 74 68 65 20 65  able.** to the e
b7e80 6d 70 74 79 20 73 74 61 74 65 2e 0a 2a 2f 0a 53  mpty state..*/.S
b7e90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
b7ea0 69 64 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c  id sqlite3HashCl
b7eb0 65 61 72 28 48 61 73 68 20 2a 70 48 29 7b 0a 20  ear(Hash *pH){. 
b7ec0 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b   HashElem *elem;
b7ed0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20           /* For 
b7ee0 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c  looping over all
b7ef0 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65   elements of the
b7f00 20 74 61 62 6c 65 20 2a 2f 0a 0a 20 20 61 73 73   table */..  ass
b7f10 65 72 74 28 20 70 48 21 3d 30 20 29 3b 0a 20 20  ert( pH!=0 );.  
b7f20 65 6c 65 6d 20 3d 20 70 48 2d 3e 66 69 72 73 74  elem = pH->first
b7f30 3b 0a 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20  ;.  pH->first = 
b7f40 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  0;.  sqlite3_fre
b7f50 65 28 70 48 2d 3e 68 74 29 3b 0a 20 20 70 48 2d  e(pH->ht);.  pH-
b7f60 3e 68 74 20 3d 20 30 3b 0a 20 20 70 48 2d 3e 68  >ht = 0;.  pH->h
b7f70 74 73 69 7a 65 20 3d 20 30 3b 0a 20 20 77 68 69  tsize = 0;.  whi
b7f80 6c 65 28 20 65 6c 65 6d 20 29 7b 0a 20 20 20 20  le( elem ){.    
b7f90 48 61 73 68 45 6c 65 6d 20 2a 6e 65 78 74 5f 65  HashElem *next_e
b7fa0 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74  lem = elem->next
b7fb0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
b7fc0 65 65 28 65 6c 65 6d 29 3b 0a 20 20 20 20 65 6c  ee(elem);.    el
b7fd0 65 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 3b 0a  em = next_elem;.
b7fe0 20 20 7d 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 20    }.  pH->count 
b7ff0 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  = 0;.}../*.** Th
b8000 65 20 68 61 73 68 69 6e 67 20 66 75 6e 63 74 69  e hashing functi
b8010 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 6e  on..*/.static un
b8020 73 69 67 6e 65 64 20 69 6e 74 20 73 74 72 48 61  signed int strHa
b8030 73 68 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  sh(const char *z
b8040 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 69  , int nKey){.  i
b8050 6e 74 20 68 20 3d 20 30 3b 0a 20 20 61 73 73 65  nt h = 0;.  asse
b8060 72 74 28 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20  rt( nKey>=0 );. 
b8070 20 77 68 69 6c 65 28 20 6e 4b 65 79 20 3e 20 30   while( nKey > 0
b8080 20 20 29 7b 0a 20 20 20 20 68 20 3d 20 28 68 3c    ){.    h = (h<
b8090 3c 33 29 20 5e 20 68 20 5e 20 73 71 6c 69 74 65  <3) ^ h ^ sqlite
b80a0 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75  3UpperToLower[(u
b80b0 6e 73 69 67 6e 65 64 20 63 68 61 72 29 2a 7a 2b  nsigned char)*z+
b80c0 2b 5d 3b 0a 20 20 20 20 6e 4b 65 79 2d 2d 3b 0a  +];.    nKey--;.
b80d0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 3b 0a    }.  return h;.
b80e0 7d 0a 0a 0a 2f 2a 20 4c 69 6e 6b 20 70 4e 65 77  }.../* Link pNew
b80f0 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74 68   element into th
b8100 65 20 68 61 73 68 20 74 61 62 6c 65 20 70 48 2e  e hash table pH.
b8110 20 20 49 66 20 70 45 6e 74 72 79 21 3d 30 20 74    If pEntry!=0 t
b8120 68 65 6e 20 61 6c 73 6f 0a 2a 2a 20 69 6e 73 65  hen also.** inse
b8130 72 74 20 70 4e 65 77 20 69 6e 74 6f 20 74 68 65  rt pNew into the
b8140 20 70 45 6e 74 72 79 20 68 61 73 68 20 62 75 63   pEntry hash buc
b8150 6b 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ket..*/.static v
b8160 6f 69 64 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e  oid insertElemen
b8170 74 28 0a 20 20 48 61 73 68 20 2a 70 48 2c 20 20  t(.  Hash *pH,  
b8180 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
b8190 68 65 20 63 6f 6d 70 6c 65 74 65 20 68 61 73 68  he complete hash
b81a0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73 74 72 75   table */.  stru
b81b0 63 74 20 5f 68 74 20 2a 70 45 6e 74 72 79 2c 20  ct _ht *pEntry, 
b81c0 20 20 20 2f 2a 20 54 68 65 20 65 6e 74 72 79 20     /* The entry 
b81d0 69 6e 74 6f 20 77 68 69 63 68 20 70 4e 65 77 20  into which pNew 
b81e0 69 73 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20  is inserted */. 
b81f0 20 48 61 73 68 45 6c 65 6d 20 2a 70 4e 65 77 20   HashElem *pNew 
b8200 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65          /* The e
b8210 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20 69 6e 73  lement to be ins
b8220 65 72 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 48 61  erted */.){.  Ha
b8230 73 68 45 6c 65 6d 20 2a 70 48 65 61 64 3b 20 20  shElem *pHead;  
b8240 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 65 6c       /* First el
b8250 65 6d 65 6e 74 20 61 6c 72 65 61 64 79 20 69 6e  ement already in
b8260 20 70 45 6e 74 72 79 20 2a 2f 0a 20 20 69 66 28   pEntry */.  if(
b8270 20 70 45 6e 74 72 79 20 29 7b 0a 20 20 20 20 70   pEntry ){.    p
b8280 48 65 61 64 20 3d 20 70 45 6e 74 72 79 2d 3e 63  Head = pEntry->c
b8290 6f 75 6e 74 20 3f 20 70 45 6e 74 72 79 2d 3e 63  ount ? pEntry->c
b82a0 68 61 69 6e 20 3a 20 30 3b 0a 20 20 20 20 70 45  hain : 0;.    pE
b82b0 6e 74 72 79 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20  ntry->count++;. 
b82c0 20 20 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e     pEntry->chain
b82d0 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 65 6c 73 65   = pNew;.  }else
b82e0 7b 0a 20 20 20 20 70 48 65 61 64 20 3d 20 30 3b  {.    pHead = 0;
b82f0 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 65 61 64  .  }.  if( pHead
b8300 20 29 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 65   ){.    pNew->ne
b8310 78 74 20 3d 20 70 48 65 61 64 3b 0a 20 20 20 20  xt = pHead;.    
b8320 70 4e 65 77 2d 3e 70 72 65 76 20 3d 20 70 48 65  pNew->prev = pHe
b8330 61 64 2d 3e 70 72 65 76 3b 0a 20 20 20 20 69 66  ad->prev;.    if
b8340 28 20 70 48 65 61 64 2d 3e 70 72 65 76 20 29 7b  ( pHead->prev ){
b8350 20 70 48 65 61 64 2d 3e 70 72 65 76 2d 3e 6e 65   pHead->prev->ne
b8360 78 74 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20  xt = pNew; }.   
b8370 20 65 6c 73 65 20 20 20 20 20 20 20 20 20 20 20   else           
b8380 20 20 7b 20 70 48 2d 3e 66 69 72 73 74 20 3d 20    { pH->first = 
b8390 70 4e 65 77 3b 20 7d 0a 20 20 20 20 70 48 65 61  pNew; }.    pHea
b83a0 64 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b 0a  d->prev = pNew;.
b83b0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65    }else{.    pNe
b83c0 77 2d 3e 6e 65 78 74 20 3d 20 70 48 2d 3e 66 69  w->next = pH->fi
b83d0 72 73 74 3b 0a 20 20 20 20 69 66 28 20 70 48 2d  rst;.    if( pH-
b83e0 3e 66 69 72 73 74 20 29 7b 20 70 48 2d 3e 66 69  >first ){ pH->fi
b83f0 72 73 74 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77  rst->prev = pNew
b8400 3b 20 7d 0a 20 20 20 20 70 4e 65 77 2d 3e 70 72  ; }.    pNew->pr
b8410 65 76 20 3d 20 30 3b 0a 20 20 20 20 70 48 2d 3e  ev = 0;.    pH->
b8420 66 69 72 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20  first = pNew;.  
b8430 7d 0a 7d 0a 0a 0a 2f 2a 20 52 65 73 69 7a 65 20  }.}.../* Resize 
b8440 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 73  the hash table s
b8450 6f 20 74 68 61 74 20 69 74 20 63 61 6e 74 61 69  o that it cantai
b8460 6e 73 20 22 6e 65 77 5f 73 69 7a 65 22 20 62 75  ns "new_size" bu
b8470 63 6b 65 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ckets..**.** The
b8480 20 68 61 73 68 20 74 61 62 6c 65 20 6d 69 67 68   hash table migh
b8490 74 20 66 61 69 6c 20 74 6f 20 72 65 73 69 7a 65  t fail to resize
b84a0 20 69 66 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c   if sqlite3_mall
b84b0 6f 63 28 29 20 66 61 69 6c 73 20 6f 72 0a 2a 2a  oc() fails or.**
b84c0 20 69 66 20 74 68 65 20 6e 65 77 20 73 69 7a 65   if the new size
b84d0 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20   is the same as 
b84e0 74 68 65 20 70 72 69 6f 72 20 73 69 7a 65 2e 0a  the prior size..
b84f0 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69  ** Return TRUE i
b8500 66 20 74 68 65 20 72 65 73 69 7a 65 20 6f 63 63  f the resize occ
b8510 75 72 73 20 61 6e 64 20 66 61 6c 73 65 20 69 66  urs and false if
b8520 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   not..*/.static 
b8530 69 6e 74 20 72 65 68 61 73 68 28 48 61 73 68 20  int rehash(Hash 
b8540 2a 70 48 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e  *pH, unsigned in
b8550 74 20 6e 65 77 5f 73 69 7a 65 29 7b 0a 20 20 73  t new_size){.  s
b8560 74 72 75 63 74 20 5f 68 74 20 2a 6e 65 77 5f 68  truct _ht *new_h
b8570 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t;            /*
b8580 20 54 68 65 20 6e 65 77 20 68 61 73 68 20 74 61   The new hash ta
b8590 62 6c 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65  ble */.  HashEle
b85a0 6d 20 2a 65 6c 65 6d 2c 20 2a 6e 65 78 74 5f 65  m *elem, *next_e
b85b0 6c 65 6d 3b 20 20 20 20 2f 2a 20 46 6f 72 20 6c  lem;    /* For l
b85c0 6f 6f 70 69 6e 67 20 6f 76 65 72 20 65 78 69 73  ooping over exis
b85d0 74 69 6e 67 20 65 6c 65 6d 65 6e 74 73 20 2a 2f  ting elements */
b85e0 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 4c  ..#if SQLITE_MAL
b85f0 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 3e 30  LOC_SOFT_LIMIT>0
b8600 0a 20 20 69 66 28 20 6e 65 77 5f 73 69 7a 65 2a  .  if( new_size*
b8610 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68  sizeof(struct _h
b8620 74 29 3e 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43  t)>SQLITE_MALLOC
b8630 5f 53 4f 46 54 5f 4c 49 4d 49 54 20 29 7b 0a 20  _SOFT_LIMIT ){. 
b8640 20 20 20 6e 65 77 5f 73 69 7a 65 20 3d 20 53 51     new_size = SQ
b8650 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54  LITE_MALLOC_SOFT
b8660 5f 4c 49 4d 49 54 2f 73 69 7a 65 6f 66 28 73 74  _LIMIT/sizeof(st
b8670 72 75 63 74 20 5f 68 74 29 3b 0a 20 20 7d 0a 20  ruct _ht);.  }. 
b8680 20 69 66 28 20 6e 65 77 5f 73 69 7a 65 3d 3d 70   if( new_size==p
b8690 48 2d 3e 68 74 73 69 7a 65 20 29 20 72 65 74 75  H->htsize ) retu
b86a0 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20  rn 0;.#endif..  
b86b0 2f 2a 20 54 68 65 20 69 6e 61 62 69 6c 69 74 79  /* The inability
b86c0 20 74 6f 20 61 6c 6c 6f 63 61 74 65 73 20 73 70   to allocates sp
b86d0 61 63 65 20 66 6f 72 20 61 20 6c 61 72 67 65 72  ace for a larger
b86e0 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 0a 20   hash table is. 
b86f0 20 2a 2a 20 61 20 70 65 72 66 6f 72 6d 61 6e 63   ** a performanc
b8700 65 20 68 69 74 20 62 75 74 20 69 74 20 69 73 20  e hit but it is 
b8710 6e 6f 74 20 61 20 66 61 74 61 6c 20 65 72 72 6f  not a fatal erro
b8720 72 2e 20 20 53 6f 20 6d 61 72 6b 20 74 68 65 0a  r.  So mark the.
b8730 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20    ** allocation 
b8740 61 73 20 61 20 62 65 6e 69 67 6e 2e 0a 20 20 2a  as a benign..  *
b8750 2f 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e  /.  sqlite3Begin
b8760 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a  BenignMalloc();.
b8770 20 20 6e 65 77 5f 68 74 20 3d 20 28 73 74 72 75    new_ht = (stru
b8780 63 74 20 5f 68 74 20 2a 29 73 71 6c 69 74 65 33  ct _ht *)sqlite3
b8790 4d 61 6c 6c 6f 63 28 20 6e 65 77 5f 73 69 7a 65  Malloc( new_size
b87a0 2a 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f  *sizeof(struct _
b87b0 68 74 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  ht) );.  sqlite3
b87c0 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  EndBenignMalloc(
b87d0 29 3b 0a 0a 20 20 69 66 28 20 6e 65 77 5f 68 74  );..  if( new_ht
b87e0 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
b87f0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
b8800 48 2d 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68 74  H->ht);.  pH->ht
b8810 20 3d 20 6e 65 77 5f 68 74 3b 0a 20 20 70 48 2d   = new_ht;.  pH-
b8820 3e 68 74 73 69 7a 65 20 3d 20 6e 65 77 5f 73 69  >htsize = new_si
b8830 7a 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  ze = sqlite3Mall
b8840 6f 63 53 69 7a 65 28 6e 65 77 5f 68 74 29 2f 73  ocSize(new_ht)/s
b8850 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74  izeof(struct _ht
b8860 29 3b 0a 20 20 6d 65 6d 73 65 74 28 6e 65 77 5f  );.  memset(new_
b8870 68 74 2c 20 30 2c 20 6e 65 77 5f 73 69 7a 65 2a  ht, 0, new_size*
b8880 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68  sizeof(struct _h
b8890 74 29 29 3b 0a 20 20 66 6f 72 28 65 6c 65 6d 3d  t));.  for(elem=
b88a0 70 48 2d 3e 66 69 72 73 74 2c 20 70 48 2d 3e 66  pH->first, pH->f
b88b0 69 72 73 74 3d 30 3b 20 65 6c 65 6d 3b 20 65 6c  irst=0; elem; el
b88c0 65 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 29 7b  em = next_elem){
b88d0 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  .    unsigned in
b88e0 74 20 68 20 3d 20 73 74 72 48 61 73 68 28 65 6c  t h = strHash(el
b88f0 65 6d 2d 3e 70 4b 65 79 2c 20 65 6c 65 6d 2d 3e  em->pKey, elem->
b8900 6e 4b 65 79 29 20 25 20 6e 65 77 5f 73 69 7a 65  nKey) % new_size
b8910 3b 0a 20 20 20 20 6e 65 78 74 5f 65 6c 65 6d 20  ;.    next_elem 
b8920 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20  = elem->next;.  
b8930 20 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28    insertElement(
b8940 70 48 2c 20 26 6e 65 77 5f 68 74 5b 68 5d 2c 20  pH, &new_ht[h], 
b8950 65 6c 65 6d 29 3b 0a 20 20 7d 0a 20 20 72 65 74  elem);.  }.  ret
b8960 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 20 54 68 69  urn 1;.}../* Thi
b8970 73 20 66 75 6e 63 74 69 6f 6e 20 28 66 6f 72 20  s function (for 
b8980 69 6e 74 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c  internal use onl
b8990 79 29 20 6c 6f 63 61 74 65 73 20 61 6e 20 65 6c  y) locates an el
b89a0 65 6d 65 6e 74 20 69 6e 20 61 6e 0a 2a 2a 20 68  ement in an.** h
b89b0 61 73 68 20 74 61 62 6c 65 20 74 68 61 74 20 6d  ash table that m
b89c0 61 74 63 68 65 73 20 74 68 65 20 67 69 76 65 6e  atches the given
b89d0 20 6b 65 79 2e 20 20 54 68 65 20 68 61 73 68 20   key.  The hash 
b89e0 66 6f 72 20 74 68 69 73 20 6b 65 79 20 68 61 73  for this key has
b89f0 0a 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e  .** already been
b8a00 20 63 6f 6d 70 75 74 65 64 20 61 6e 64 20 69 73   computed and is
b8a10 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 34   passed as the 4
b8a20 74 68 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f  th parameter..*/
b8a30 0a 73 74 61 74 69 63 20 48 61 73 68 45 6c 65 6d  .static HashElem
b8a40 20 2a 66 69 6e 64 45 6c 65 6d 65 6e 74 47 69 76   *findElementGiv
b8a50 65 6e 48 61 73 68 28 0a 20 20 63 6f 6e 73 74 20  enHash(.  const 
b8a60 48 61 73 68 20 2a 70 48 2c 20 20 20 20 20 2f 2a  Hash *pH,     /*
b8a70 20 54 68 65 20 70 48 20 74 6f 20 62 65 20 73 65   The pH to be se
b8a80 61 72 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73  arched */.  cons
b8a90 74 20 63 68 61 72 20 2a 70 4b 65 79 2c 20 20 20  t char *pKey,   
b8aa0 2f 2a 20 54 68 65 20 6b 65 79 20 77 65 20 61 72  /* The key we ar
b8ab0 65 20 73 65 61 72 63 68 69 6e 67 20 66 6f 72 20  e searching for 
b8ac0 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 20 20  */.  int nKey,  
b8ad0 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65           /* Byte
b8ae0 73 20 69 6e 20 6b 65 79 20 28 6e 6f 74 20 63 6f  s in key (not co
b8af0 75 6e 74 69 6e 67 20 7a 65 72 6f 20 74 65 72 6d  unting zero term
b8b00 69 6e 61 74 6f 72 29 20 2a 2f 0a 20 20 75 6e 73  inator) */.  uns
b8b10 69 67 6e 65 64 20 69 6e 74 20 68 20 20 20 20 20  igned int h     
b8b20 20 2f 2a 20 54 68 65 20 68 61 73 68 20 66 6f 72   /* The hash for
b8b30 20 74 68 69 73 20 6b 65 79 2e 20 2a 2f 0a 29 7b   this key. */.){
b8b40 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65  .  HashElem *ele
b8b50 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m;              
b8b60 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6c 6f 6f    /* Used to loo
b8b70 70 20 74 68 72 75 20 74 68 65 20 65 6c 65 6d 65  p thru the eleme
b8b80 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74  nt list */.  int
b8b90 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20   count;         
b8ba0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
b8bb0 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74  umber of element
b8bc0 73 20 6c 65 66 74 20 74 6f 20 74 65 73 74 20 2a  s left to test *
b8bd0 2f 0a 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 20  /..  if( pH->ht 
b8be0 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 5f 68  ){.    struct _h
b8bf0 74 20 2a 70 45 6e 74 72 79 20 3d 20 26 70 48 2d  t *pEntry = &pH-
b8c00 3e 68 74 5b 68 5d 3b 0a 20 20 20 20 65 6c 65 6d  >ht[h];.    elem
b8c10 20 3d 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e   = pEntry->chain
b8c20 3b 0a 20 20 20 20 63 6f 75 6e 74 20 3d 20 70 45  ;.    count = pE
b8c30 6e 74 72 79 2d 3e 63 6f 75 6e 74 3b 0a 20 20 7d  ntry->count;.  }
b8c40 65 6c 73 65 7b 0a 20 20 20 20 65 6c 65 6d 20 3d  else{.    elem =
b8c50 20 70 48 2d 3e 66 69 72 73 74 3b 0a 20 20 20 20   pH->first;.    
b8c60 63 6f 75 6e 74 20 3d 20 70 48 2d 3e 63 6f 75 6e  count = pH->coun
b8c70 74 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20  t;.  }.  while( 
b8c80 63 6f 75 6e 74 2d 2d 20 26 26 20 41 4c 57 41 59  count-- && ALWAY
b8c90 53 28 65 6c 65 6d 29 20 29 7b 0a 20 20 20 20 69  S(elem) ){.    i
b8ca0 66 28 20 65 6c 65 6d 2d 3e 6e 4b 65 79 3d 3d 6e  f( elem->nKey==n
b8cb0 4b 65 79 20 26 26 20 73 71 6c 69 74 65 33 53 74  Key && sqlite3St
b8cc0 72 4e 49 43 6d 70 28 65 6c 65 6d 2d 3e 70 4b 65  rNICmp(elem->pKe
b8cd0 79 2c 70 4b 65 79 2c 6e 4b 65 79 29 3d 3d 30 20  y,pKey,nKey)==0 
b8ce0 29 7b 20 0a 20 20 20 20 20 20 72 65 74 75 72 6e  ){ .      return
b8cf0 20 65 6c 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20   elem;.    }.   
b8d00 20 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65   elem = elem->ne
b8d10 78 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  xt;.  }.  return
b8d20 20 30 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65   0;.}../* Remove
b8d30 20 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 20   a single entry 
b8d40 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61  from the hash ta
b8d50 62 6c 65 20 67 69 76 65 6e 20 61 20 70 6f 69 6e  ble given a poin
b8d60 74 65 72 20 74 6f 20 74 68 61 74 0a 2a 2a 20 65  ter to that.** e
b8d70 6c 65 6d 65 6e 74 20 61 6e 64 20 61 20 68 61 73  lement and a has
b8d80 68 20 6f 6e 20 74 68 65 20 65 6c 65 6d 65 6e 74  h on the element
b8d90 27 73 20 6b 65 79 2e 0a 2a 2f 0a 73 74 61 74 69  's key..*/.stati
b8da0 63 20 76 6f 69 64 20 72 65 6d 6f 76 65 45 6c 65  c void removeEle
b8db0 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 28 0a 20  mentGivenHash(. 
b8dc0 20 48 61 73 68 20 2a 70 48 2c 20 20 20 20 20 20   Hash *pH,      
b8dd0 20 20 20 2f 2a 20 54 68 65 20 70 48 20 63 6f 6e     /* The pH con
b8de0 74 61 69 6e 69 6e 67 20 22 65 6c 65 6d 22 20 2a  taining "elem" *
b8df0 2f 0a 20 20 48 61 73 68 45 6c 65 6d 2a 20 65 6c  /.  HashElem* el
b8e00 65 6d 2c 20 20 20 2f 2a 20 54 68 65 20 65 6c 65  em,   /* The ele
b8e10 6d 65 6e 74 20 74 6f 20 62 65 20 72 65 6d 6f 76  ment to be remov
b8e20 65 64 20 66 72 6f 6d 20 74 68 65 20 70 48 20 2a  ed from the pH *
b8e30 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  /.  unsigned int
b8e40 20 68 20 20 20 20 2f 2a 20 48 61 73 68 20 76 61   h    /* Hash va
b8e50 6c 75 65 20 66 6f 72 20 74 68 65 20 65 6c 65 6d  lue for the elem
b8e60 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75  ent */.){.  stru
b8e70 63 74 20 5f 68 74 20 2a 70 45 6e 74 72 79 3b 0a  ct _ht *pEntry;.
b8e80 20 20 69 66 28 20 65 6c 65 6d 2d 3e 70 72 65 76    if( elem->prev
b8e90 20 29 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 70 72   ){.    elem->pr
b8ea0 65 76 2d 3e 6e 65 78 74 20 3d 20 65 6c 65 6d 2d  ev->next = elem-
b8eb0 3e 6e 65 78 74 3b 20 0a 20 20 7d 65 6c 73 65 7b  >next; .  }else{
b8ec0 0a 20 20 20 20 70 48 2d 3e 66 69 72 73 74 20 3d  .    pH->first =
b8ed0 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d   elem->next;.  }
b8ee0 0a 20 20 69 66 28 20 65 6c 65 6d 2d 3e 6e 65 78  .  if( elem->nex
b8ef0 74 20 29 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 6e  t ){.    elem->n
b8f00 65 78 74 2d 3e 70 72 65 76 20 3d 20 65 6c 65 6d  ext->prev = elem
b8f10 2d 3e 70 72 65 76 3b 0a 20 20 7d 0a 20 20 69 66  ->prev;.  }.  if
b8f20 28 20 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20 20  ( pH->ht ){.    
b8f30 70 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68 74  pEntry = &pH->ht
b8f40 5b 68 5d 3b 0a 20 20 20 20 69 66 28 20 70 45 6e  [h];.    if( pEn
b8f50 74 72 79 2d 3e 63 68 61 69 6e 3d 3d 65 6c 65 6d  try->chain==elem
b8f60 20 29 7b 0a 20 20 20 20 20 20 70 45 6e 74 72 79   ){.      pEntry
b8f70 2d 3e 63 68 61 69 6e 20 3d 20 65 6c 65 6d 2d 3e  ->chain = elem->
b8f80 6e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  next;.    }.    
b8f90 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 2d 2d 3b  pEntry->count--;
b8fa0 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 6e  .    assert( pEn
b8fb0 74 72 79 2d 3e 63 6f 75 6e 74 3e 3d 30 20 29 3b  try->count>=0 );
b8fc0 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66  .  }.  sqlite3_f
b8fd0 72 65 65 28 20 65 6c 65 6d 20 29 3b 0a 20 20 70  ree( elem );.  p
b8fe0 48 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 20 69 66  H->count--;.  if
b8ff0 28 20 70 48 2d 3e 63 6f 75 6e 74 3c 3d 30 20 29  ( pH->count<=0 )
b9000 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 48  {.    assert( pH
b9010 2d 3e 66 69 72 73 74 3d 3d 30 20 29 3b 0a 20 20  ->first==0 );.  
b9020 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e 63 6f    assert( pH->co
b9030 75 6e 74 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71  unt==0 );.    sq
b9040 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 70  lite3HashClear(p
b9050 48 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 41 74  H);.  }.}../* At
b9060 74 65 6d 70 74 20 74 6f 20 6c 6f 63 61 74 65 20  tempt to locate 
b9070 61 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68  an element of th
b9080 65 20 68 61 73 68 20 74 61 62 6c 65 20 70 48 20  e hash table pH 
b9090 77 69 74 68 20 61 20 6b 65 79 0a 2a 2a 20 74 68  with a key.** th
b90a0 61 74 20 6d 61 74 63 68 65 73 20 70 4b 65 79 2c  at matches pKey,
b90b0 6e 4b 65 79 2e 20 20 52 65 74 75 72 6e 20 74 68  nKey.  Return th
b90c0 65 20 64 61 74 61 20 66 6f 72 20 74 68 69 73 20  e data for this 
b90d0 65 6c 65 6d 65 6e 74 20 69 66 20 69 74 20 69 73  element if it is
b90e0 0a 2a 2a 20 66 6f 75 6e 64 2c 20 6f 72 20 4e 55  .** found, or NU
b90f0 4c 4c 20 69 66 20 74 68 65 72 65 20 69 73 20 6e  LL if there is n
b9100 6f 20 6d 61 74 63 68 2e 0a 2a 2f 0a 53 51 4c 49  o match..*/.SQLI
b9110 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
b9120 2a 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64  *sqlite3HashFind
b9130 28 63 6f 6e 73 74 20 48 61 73 68 20 2a 70 48 2c  (const Hash *pH,
b9140 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b 65   const char *pKe
b9150 79 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20  y, int nKey){.  
b9160 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20  HashElem *elem; 
b9170 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e     /* The elemen
b9180 74 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 6b  t that matches k
b9190 65 79 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  ey */.  unsigned
b91a0 20 69 6e 74 20 68 3b 20 20 20 20 2f 2a 20 41 20   int h;    /* A 
b91b0 68 61 73 68 20 6f 6e 20 6b 65 79 20 2a 2f 0a 0a  hash on key */..
b91c0 20 20 61 73 73 65 72 74 28 20 70 48 21 3d 30 20    assert( pH!=0 
b91d0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4b 65  );.  assert( pKe
b91e0 79 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  y!=0 );.  assert
b91f0 28 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 69  ( nKey>=0 );.  i
b9200 66 28 20 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20  f( pH->ht ){.   
b9210 20 68 20 3d 20 73 74 72 48 61 73 68 28 70 4b 65   h = strHash(pKe
b9220 79 2c 20 6e 4b 65 79 29 20 25 20 70 48 2d 3e 68  y, nKey) % pH->h
b9230 74 73 69 7a 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a  tsize;.  }else{.
b9240 20 20 20 20 68 20 3d 20 30 3b 0a 20 20 7d 0a 20      h = 0;.  }. 
b9250 20 65 6c 65 6d 20 3d 20 66 69 6e 64 45 6c 65 6d   elem = findElem
b9260 65 6e 74 47 69 76 65 6e 48 61 73 68 28 70 48 2c  entGivenHash(pH,
b9270 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 68 29 3b   pKey, nKey, h);
b9280 0a 20 20 72 65 74 75 72 6e 20 65 6c 65 6d 20 3f  .  return elem ?
b9290 20 65 6c 65 6d 2d 3e 64 61 74 61 20 3a 20 30 3b   elem->data : 0;
b92a0 0a 7d 0a 0a 2f 2a 20 49 6e 73 65 72 74 20 61 6e  .}../* Insert an
b92b0 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74 68   element into th
b92c0 65 20 68 61 73 68 20 74 61 62 6c 65 20 70 48 2e  e hash table pH.
b92d0 20 20 54 68 65 20 6b 65 79 20 69 73 20 70 4b 65    The key is pKe
b92e0 79 2c 6e 4b 65 79 0a 2a 2a 20 61 6e 64 20 74 68  y,nKey.** and th
b92f0 65 20 64 61 74 61 20 69 73 20 22 64 61 74 61 22  e data is "data"
b9300 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 65 6c  ..**.** If no el
b9310 65 6d 65 6e 74 20 65 78 69 73 74 73 20 77 69 74  ement exists wit
b9320 68 20 61 20 6d 61 74 63 68 69 6e 67 20 6b 65 79  h a matching key
b9330 2c 20 74 68 65 6e 20 61 20 6e 65 77 0a 2a 2a 20  , then a new.** 
b9340 65 6c 65 6d 65 6e 74 20 69 73 20 63 72 65 61 74  element is creat
b9350 65 64 20 61 6e 64 20 4e 55 4c 4c 20 69 73 20 72  ed and NULL is r
b9360 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  eturned..**.** I
b9370 66 20 61 6e 6f 74 68 65 72 20 65 6c 65 6d 65 6e  f another elemen
b9380 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73  t already exists
b9390 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 6b   with the same k
b93a0 65 79 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20  ey, then the.** 
b93b0 6e 65 77 20 64 61 74 61 20 72 65 70 6c 61 63 65  new data replace
b93c0 73 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20 61  s the old data a
b93d0 6e 64 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20  nd the old data 
b93e0 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20  is returned..** 
b93f0 54 68 65 20 6b 65 79 20 69 73 20 6e 6f 74 20 63  The key is not c
b9400 6f 70 69 65 64 20 69 6e 20 74 68 69 73 20 69 6e  opied in this in
b9410 73 74 61 6e 63 65 2e 20 20 49 66 20 61 20 6d 61  stance.  If a ma
b9420 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 74 68 65 6e  lloc fails, then
b9430 0a 2a 2a 20 74 68 65 20 6e 65 77 20 64 61 74 61  .** the new data
b9440 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64   is returned and
b9450 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
b9460 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a  is unchanged..**
b9470 0a 2a 2a 20 49 66 20 74 68 65 20 22 64 61 74 61  .** If the "data
b9480 22 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74  " parameter to t
b9490 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
b94a0 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a 2a  NULL, then the.*
b94b0 2a 20 65 6c 65 6d 65 6e 74 20 63 6f 72 72 65 73  * element corres
b94c0 70 6f 6e 64 69 6e 67 20 74 6f 20 22 6b 65 79 22  ponding to "key"
b94d0 20 69 73 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d   is removed from
b94e0 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e   the hash table.
b94f0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
b9500 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
b9510 48 61 73 68 49 6e 73 65 72 74 28 48 61 73 68 20  HashInsert(Hash 
b9520 2a 70 48 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  *pH, const char 
b9530 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 2c  *pKey, int nKey,
b9540 20 76 6f 69 64 20 2a 64 61 74 61 29 7b 0a 20 20   void *data){.  
b9550 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 3b 20  unsigned int h; 
b9560 20 20 20 20 20 20 2f 2a 20 74 68 65 20 68 61 73        /* the has
b9570 68 20 6f 66 20 74 68 65 20 6b 65 79 20 6d 6f 64  h of the key mod
b9580 75 6c 6f 20 68 61 73 68 20 74 61 62 6c 65 20 73  ulo hash table s
b9590 69 7a 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65  ize */.  HashEle
b95a0 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20 2f  m *elem;       /
b95b0 2a 20 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20 74  * Used to loop t
b95c0 68 72 75 20 74 68 65 20 65 6c 65 6d 65 6e 74 20  hru the element 
b95d0 6c 69 73 74 20 2a 2f 0a 20 20 48 61 73 68 45 6c  list */.  HashEl
b95e0 65 6d 20 2a 6e 65 77 5f 65 6c 65 6d 3b 20 20 20  em *new_elem;   
b95f0 2f 2a 20 4e 65 77 20 65 6c 65 6d 65 6e 74 20 61  /* New element a
b9600 64 64 65 64 20 74 6f 20 74 68 65 20 70 48 20 2a  dded to the pH *
b9610 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 48 21  /..  assert( pH!
b9620 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
b9630 70 4b 65 79 21 3d 30 20 29 3b 0a 20 20 61 73 73  pKey!=0 );.  ass
b9640 65 72 74 28 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a  ert( nKey>=0 );.
b9650 20 20 69 66 28 20 70 48 2d 3e 68 74 73 69 7a 65    if( pH->htsize
b9660 20 29 7b 0a 20 20 20 20 68 20 3d 20 73 74 72 48   ){.    h = strH
b9670 61 73 68 28 70 4b 65 79 2c 20 6e 4b 65 79 29 20  ash(pKey, nKey) 
b9680 25 20 70 48 2d 3e 68 74 73 69 7a 65 3b 0a 20 20  % pH->htsize;.  
b9690 7d 65 6c 73 65 7b 0a 20 20 20 20 68 20 3d 20 30  }else{.    h = 0
b96a0 3b 0a 20 20 7d 0a 20 20 65 6c 65 6d 20 3d 20 66  ;.  }.  elem = f
b96b0 69 6e 64 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48  indElementGivenH
b96c0 61 73 68 28 70 48 2c 70 4b 65 79 2c 6e 4b 65 79  ash(pH,pKey,nKey
b96d0 2c 68 29 3b 0a 20 20 69 66 28 20 65 6c 65 6d 20  ,h);.  if( elem 
b96e0 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 6f 6c 64  ){.    void *old
b96f0 5f 64 61 74 61 20 3d 20 65 6c 65 6d 2d 3e 64 61  _data = elem->da
b9700 74 61 3b 0a 20 20 20 20 69 66 28 20 64 61 74 61  ta;.    if( data
b9710 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 6d  ==0 ){.      rem
b9720 6f 76 65 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48  oveElementGivenH
b9730 61 73 68 28 70 48 2c 65 6c 65 6d 2c 68 29 3b 0a  ash(pH,elem,h);.
b9740 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
b9750 20 65 6c 65 6d 2d 3e 64 61 74 61 20 3d 20 64 61   elem->data = da
b9760 74 61 3b 0a 20 20 20 20 20 20 65 6c 65 6d 2d 3e  ta;.      elem->
b9770 70 4b 65 79 20 3d 20 70 4b 65 79 3b 0a 20 20 20  pKey = pKey;.   
b9780 20 20 20 61 73 73 65 72 74 28 6e 4b 65 79 3d 3d     assert(nKey==
b9790 65 6c 65 6d 2d 3e 6e 4b 65 79 29 3b 0a 20 20 20  elem->nKey);.   
b97a0 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 6c   }.    return ol
b97b0 64 5f 64 61 74 61 3b 0a 20 20 7d 0a 20 20 69 66  d_data;.  }.  if
b97c0 28 20 64 61 74 61 3d 3d 30 20 29 20 72 65 74 75  ( data==0 ) retu
b97d0 72 6e 20 30 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d  rn 0;.  new_elem
b97e0 20 3d 20 28 48 61 73 68 45 6c 65 6d 2a 29 73 71   = (HashElem*)sq
b97f0 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 73 69 7a  lite3Malloc( siz
b9800 65 6f 66 28 48 61 73 68 45 6c 65 6d 29 20 29 3b  eof(HashElem) );
b9810 0a 20 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d 3d  .  if( new_elem=
b9820 3d 30 20 29 20 72 65 74 75 72 6e 20 64 61 74 61  =0 ) return data
b9830 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b  ;.  new_elem->pK
b9840 65 79 20 3d 20 70 4b 65 79 3b 0a 20 20 6e 65 77  ey = pKey;.  new
b9850 5f 65 6c 65 6d 2d 3e 6e 4b 65 79 20 3d 20 6e 4b  _elem->nKey = nK
b9860 65 79 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e  ey;.  new_elem->
b9870 64 61 74 61 20 3d 20 64 61 74 61 3b 0a 20 20 70  data = data;.  p
b9880 48 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 69 66  H->count++;.  if
b9890 28 20 70 48 2d 3e 63 6f 75 6e 74 3e 3d 31 30 20  ( pH->count>=10 
b98a0 26 26 20 70 48 2d 3e 63 6f 75 6e 74 20 3e 20 32  && pH->count > 2
b98b0 2a 70 48 2d 3e 68 74 73 69 7a 65 20 29 7b 0a 20  *pH->htsize ){. 
b98c0 20 20 20 69 66 28 20 72 65 68 61 73 68 28 70 48     if( rehash(pH
b98d0 2c 20 70 48 2d 3e 63 6f 75 6e 74 2a 32 29 20 29  , pH->count*2) )
b98e0 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
b98f0 70 48 2d 3e 68 74 73 69 7a 65 3e 30 20 29 3b 0a  pH->htsize>0 );.
b9900 20 20 20 20 20 20 68 20 3d 20 73 74 72 48 61 73        h = strHas
b9910 68 28 70 4b 65 79 2c 20 6e 4b 65 79 29 20 25 20  h(pKey, nKey) % 
b9920 70 48 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 20 20  pH->htsize;.    
b9930 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 2d 3e  }.  }.  if( pH->
b9940 68 74 20 29 7b 0a 20 20 20 20 69 6e 73 65 72 74  ht ){.    insert
b9950 45 6c 65 6d 65 6e 74 28 70 48 2c 20 26 70 48 2d  Element(pH, &pH-
b9960 3e 68 74 5b 68 5d 2c 20 6e 65 77 5f 65 6c 65 6d  >ht[h], new_elem
b9970 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
b9980 69 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 70 48  insertElement(pH
b9990 2c 20 30 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b 0a  , 0, new_elem);.
b99a0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
b99b0 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
b99c0 2a 2a 20 45 6e 64 20 6f 66 20 68 61 73 68 2e 63  ** End of hash.c
b99d0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
b99e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b99f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b9a00 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
b9a10 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 70  ** Begin file op
b9a20 63 6f 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  codes.c ********
b9a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b9a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b9a50 2a 2f 0a 2f 2a 20 41 75 74 6f 6d 61 74 69 63 61  */./* Automatica
b9a60 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 2e 20 20  lly generated.  
b9a70 44 6f 20 6e 6f 74 20 65 64 69 74 20 2a 2f 0a 2f  Do not edit */./
b9a80 2a 20 53 65 65 20 74 68 65 20 6d 6b 6f 70 63 6f  * See the mkopco
b9a90 64 65 63 2e 61 77 6b 20 73 63 72 69 70 74 20 66  dec.awk script f
b9aa0 6f 72 20 64 65 74 61 69 6c 73 2e 20 2a 2f 0a 23  or details. */.#
b9ab0 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
b9ac0 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 29  TE_OMIT_EXPLAIN)
b9ad0 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 4e 44 45   || !defined(NDE
b9ae0 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  BUG) || defined(
b9af0 56 44 42 45 5f 50 52 4f 46 49 4c 45 29 20 7c 7c  VDBE_PROFILE) ||
b9b00 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
b9b10 44 45 42 55 47 29 0a 53 51 4c 49 54 45 5f 50 52  DEBUG).SQLITE_PR
b9b20 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72  IVATE const char
b9b30 20 2a 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e   *sqlite3OpcodeN
b9b40 61 6d 65 28 69 6e 74 20 69 29 7b 0a 20 73 74 61  ame(int i){. sta
b9b50 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
b9b60 63 6f 6e 73 74 20 61 7a 4e 61 6d 65 5b 5d 20 3d  const azName[] =
b9b70 20 7b 20 22 3f 22 2c 0a 20 20 20 20 20 2f 2a 20   { "?",.     /* 
b9b80 20 20 31 20 2a 2f 20 22 56 4e 65 78 74 22 2c 0a    1 */ "VNext",.
b9b90 20 20 20 20 20 2f 2a 20 20 20 32 20 2a 2f 20 22       /*   2 */ "
b9ba0 41 66 66 69 6e 69 74 79 22 2c 0a 20 20 20 20 20  Affinity",.     
b9bb0 2f 2a 20 20 20 33 20 2a 2f 20 22 43 6f 6c 75 6d  /*   3 */ "Colum
b9bc0 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 34 20  n",.     /*   4 
b9bd0 2a 2f 20 22 53 65 74 43 6f 6f 6b 69 65 22 2c 0a  */ "SetCookie",.
b9be0 20 20 20 20 20 2f 2a 20 20 20 35 20 2a 2f 20 22       /*   5 */ "
b9bf0 53 65 65 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 20  Seek",.     /*  
b9c00 20 36 20 2a 2f 20 22 53 65 71 75 65 6e 63 65 22   6 */ "Sequence"
b9c10 2c 0a 20 20 20 20 20 2f 2a 20 20 20 37 20 2a 2f  ,.     /*   7 */
b9c20 20 22 53 61 76 65 70 6f 69 6e 74 22 2c 0a 20 20   "Savepoint",.  
b9c30 20 20 20 2f 2a 20 20 20 38 20 2a 2f 20 22 52 6f     /*   8 */ "Ro
b9c40 77 4b 65 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20  wKey",.     /*  
b9c50 20 39 20 2a 2f 20 22 53 43 6f 70 79 22 2c 0a 20   9 */ "SCopy",. 
b9c60 20 20 20 20 2f 2a 20 20 31 30 20 2a 2f 20 22 4f      /*  10 */ "O
b9c70 70 65 6e 57 72 69 74 65 22 2c 0a 20 20 20 20 20  penWrite",.     
b9c80 2f 2a 20 20 31 31 20 2a 2f 20 22 49 66 22 2c 0a  /*  11 */ "If",.
b9c90 20 20 20 20 20 2f 2a 20 20 31 32 20 2a 2f 20 22       /*  12 */ "
b9ca0 43 6f 6c 6c 53 65 71 22 2c 0a 20 20 20 20 20 2f  CollSeq",.     /
b9cb0 2a 20 20 31 33 20 2a 2f 20 22 4f 70 65 6e 52 65  *  13 */ "OpenRe
b9cc0 61 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 34  ad",.     /*  14
b9cd0 20 2a 2f 20 22 45 78 70 69 72 65 22 2c 0a 20 20   */ "Expire",.  
b9ce0 20 20 20 2f 2a 20 20 31 35 20 2a 2f 20 22 41 75     /*  15 */ "Au
b9cf0 74 6f 43 6f 6d 6d 69 74 22 2c 0a 20 20 20 20 20  toCommit",.     
b9d00 2f 2a 20 20 31 36 20 2a 2f 20 22 50 61 67 65 63  /*  16 */ "Pagec
b9d10 6f 75 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20  ount",.     /*  
b9d20 31 37 20 2a 2f 20 22 49 6e 74 65 67 72 69 74 79  17 */ "Integrity
b9d30 43 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 38  Ck",.     /*  18
b9d40 20 2a 2f 20 22 53 6f 72 74 22 2c 0a 20 20 20 20   */ "Sort",.    
b9d50 20 2f 2a 20 20 31 39 20 2a 2f 20 22 4e 6f 74 22   /*  19 */ "Not"
b9d60 2c 0a 20 20 20 20 20 2f 2a 20 20 32 30 20 2a 2f  ,.     /*  20 */
b9d70 20 22 43 6f 70 79 22 2c 0a 20 20 20 20 20 2f 2a   "Copy",.     /*
b9d80 20 20 32 31 20 2a 2f 20 22 54 72 61 63 65 22 2c    21 */ "Trace",
b9d90 0a 20 20 20 20 20 2f 2a 20 20 32 32 20 2a 2f 20  .     /*  22 */ 
b9da0 22 46 75 6e 63 74 69 6f 6e 22 2c 0a 20 20 20 20  "Function",.    
b9db0 20 2f 2a 20 20 32 33 20 2a 2f 20 22 49 66 4e 65   /*  23 */ "IfNe
b9dc0 67 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 34 20  g",.     /*  24 
b9dd0 2a 2f 20 22 4e 6f 6f 70 22 2c 0a 20 20 20 20 20  */ "Noop",.     
b9de0 2f 2a 20 20 32 35 20 2a 2f 20 22 50 72 6f 67 72  /*  25 */ "Progr
b9df0 61 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 36  am",.     /*  26
b9e00 20 2a 2f 20 22 52 65 74 75 72 6e 22 2c 0a 20 20   */ "Return",.  
b9e10 20 20 20 2f 2a 20 20 32 37 20 2a 2f 20 22 4e 65     /*  27 */ "Ne
b9e20 77 52 6f 77 69 64 22 2c 0a 20 20 20 20 20 2f 2a  wRowid",.     /*
b9e30 20 20 32 38 20 2a 2f 20 22 56 61 72 69 61 62 6c    28 */ "Variabl
b9e40 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 39 20  e",.     /*  29 
b9e50 2a 2f 20 22 53 74 72 69 6e 67 22 2c 0a 20 20 20  */ "String",.   
b9e60 20 20 2f 2a 20 20 33 30 20 2a 2f 20 22 52 65 61    /*  30 */ "Rea
b9e70 6c 41 66 66 69 6e 69 74 79 22 2c 0a 20 20 20 20  lAffinity",.    
b9e80 20 2f 2a 20 20 33 31 20 2a 2f 20 22 56 52 65 6e   /*  31 */ "VRen
b9e90 61 6d 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33  ame",.     /*  3
b9ea0 32 20 2a 2f 20 22 50 61 72 73 65 53 63 68 65 6d  2 */ "ParseSchem
b9eb0 61 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 33 20  a",.     /*  33 
b9ec0 2a 2f 20 22 56 4f 70 65 6e 22 2c 0a 20 20 20 20  */ "VOpen",.    
b9ed0 20 2f 2a 20 20 33 34 20 2a 2f 20 22 43 6c 6f 73   /*  34 */ "Clos
b9ee0 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 35 20  e",.     /*  35 
b9ef0 2a 2f 20 22 43 72 65 61 74 65 49 6e 64 65 78 22  */ "CreateIndex"
b9f00 2c 0a 20 20 20 20 20 2f 2a 20 20 33 36 20 2a 2f  ,.     /*  36 */
b9f10 20 22 49 73 55 6e 69 71 75 65 22 2c 0a 20 20 20   "IsUnique",.   
b9f20 20 20 2f 2a 20 20 33 37 20 2a 2f 20 22 4e 6f 74    /*  37 */ "Not
b9f30 46 6f 75 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20  Found",.     /* 
b9f40 20 33 38 20 2a 2f 20 22 49 6e 74 36 34 22 2c 0a   38 */ "Int64",.
b9f50 20 20 20 20 20 2f 2a 20 20 33 39 20 2a 2f 20 22       /*  39 */ "
b9f60 4d 75 73 74 42 65 49 6e 74 22 2c 0a 20 20 20 20  MustBeInt",.    
b9f70 20 2f 2a 20 20 34 30 20 2a 2f 20 22 48 61 6c 74   /*  40 */ "Halt
b9f80 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 31 20 2a  ",.     /*  41 *
b9f90 2f 20 22 52 6f 77 69 64 22 2c 0a 20 20 20 20 20  / "Rowid",.     
b9fa0 2f 2a 20 20 34 32 20 2a 2f 20 22 49 64 78 4c 54  /*  42 */ "IdxLT
b9fb0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 33 20 2a  ",.     /*  43 *
b9fc0 2f 20 22 41 64 64 49 6d 6d 22 2c 0a 20 20 20 20  / "AddImm",.    
b9fd0 20 2f 2a 20 20 34 34 20 2a 2f 20 22 52 6f 77 44   /*  44 */ "RowD
b9fe0 61 74 61 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34  ata",.     /*  4
b9ff0 35 20 2a 2f 20 22 4d 65 6d 4d 61 78 22 2c 0a 20  5 */ "MemMax",. 
ba000 20 20 20 20 2f 2a 20 20 34 36 20 2a 2f 20 22 4e      /*  46 */ "N
ba010 6f 74 45 78 69 73 74 73 22 2c 0a 20 20 20 20 20  otExists",.     
ba020 2f 2a 20 20 34 37 20 2a 2f 20 22 47 6f 73 75 62  /*  47 */ "Gosub
ba030 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 38 20 2a  ",.     /*  48 *
ba040 2f 20 22 49 6e 74 65 67 65 72 22 2c 0a 20 20 20  / "Integer",.   
ba050 20 20 2f 2a 20 20 34 39 20 2a 2f 20 22 50 72 65    /*  49 */ "Pre
ba060 76 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 30 20  v",.     /*  50 
ba070 2a 2f 20 22 52 6f 77 53 65 74 52 65 61 64 22 2c  */ "RowSetRead",
ba080 0a 20 20 20 20 20 2f 2a 20 20 35 31 20 2a 2f 20  .     /*  51 */ 
ba090 22 52 6f 77 53 65 74 41 64 64 22 2c 0a 20 20 20  "RowSetAdd",.   
ba0a0 20 20 2f 2a 20 20 35 32 20 2a 2f 20 22 56 43 6f    /*  52 */ "VCo
ba0b0 6c 75 6d 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20  lumn",.     /*  
ba0c0 35 33 20 2a 2f 20 22 43 72 65 61 74 65 54 61 62  53 */ "CreateTab
ba0d0 6c 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 34  le",.     /*  54
ba0e0 20 2a 2f 20 22 4c 61 73 74 22 2c 0a 20 20 20 20   */ "Last",.    
ba0f0 20 2f 2a 20 20 35 35 20 2a 2f 20 22 53 65 65 6b   /*  55 */ "Seek
ba100 4c 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 36  Le",.     /*  56
ba110 20 2a 2f 20 22 49 6e 63 72 56 61 63 75 75 6d 22   */ "IncrVacuum"
ba120 2c 0a 20 20 20 20 20 2f 2a 20 20 35 37 20 2a 2f  ,.     /*  57 */
ba130 20 22 49 64 78 52 6f 77 69 64 22 2c 0a 20 20 20   "IdxRowid",.   
ba140 20 20 2f 2a 20 20 35 38 20 2a 2f 20 22 52 65 73    /*  58 */ "Res
ba150 65 74 43 6f 75 6e 74 22 2c 0a 20 20 20 20 20 2f  etCount",.     /
ba160 2a 20 20 35 39 20 2a 2f 20 22 59 69 65 6c 64 22  *  59 */ "Yield"
ba170 2c 0a 20 20 20 20 20 2f 2a 20 20 36 30 20 2a 2f  ,.     /*  60 */
ba180 20 22 44 72 6f 70 54 72 69 67 67 65 72 22 2c 0a   "DropTrigger",.
ba190 20 20 20 20 20 2f 2a 20 20 36 31 20 2a 2f 20 22       /*  61 */ "
ba1a0 44 72 6f 70 49 6e 64 65 78 22 2c 0a 20 20 20 20  DropIndex",.    
ba1b0 20 2f 2a 20 20 36 32 20 2a 2f 20 22 50 61 72 61   /*  62 */ "Para
ba1c0 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 33 20  m",.     /*  63 
ba1d0 2a 2f 20 22 49 64 78 47 45 22 2c 0a 20 20 20 20  */ "IdxGE",.    
ba1e0 20 2f 2a 20 20 36 34 20 2a 2f 20 22 49 64 78 44   /*  64 */ "IdxD
ba1f0 65 6c 65 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20  elete",.     /* 
ba200 20 36 35 20 2a 2f 20 22 56 61 63 75 75 6d 22 2c   65 */ "Vacuum",
ba210 0a 20 20 20 20 20 2f 2a 20 20 36 36 20 2a 2f 20  .     /*  66 */ 
ba220 22 4f 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36  "Or",.     /*  6
ba230 37 20 2a 2f 20 22 41 6e 64 22 2c 0a 20 20 20 20  7 */ "And",.    
ba240 20 2f 2a 20 20 36 38 20 2a 2f 20 22 49 66 4e 6f   /*  68 */ "IfNo
ba250 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 39 20  t",.     /*  69 
ba260 2a 2f 20 22 44 72 6f 70 54 61 62 6c 65 22 2c 0a  */ "DropTable",.
ba270 20 20 20 20 20 2f 2a 20 20 37 30 20 2a 2f 20 22       /*  70 */ "
ba280 53 65 65 6b 4c 74 22 2c 0a 20 20 20 20 20 2f 2a  SeekLt",.     /*
ba290 20 20 37 31 20 2a 2f 20 22 49 73 4e 75 6c 6c 22    71 */ "IsNull"
ba2a0 2c 0a 20 20 20 20 20 2f 2a 20 20 37 32 20 2a 2f  ,.     /*  72 */
ba2b0 20 22 4e 6f 74 4e 75 6c 6c 22 2c 0a 20 20 20 20   "NotNull",.    
ba2c0 20 2f 2a 20 20 37 33 20 2a 2f 20 22 4e 65 22 2c   /*  73 */ "Ne",
ba2d0 0a 20 20 20 20 20 2f 2a 20 20 37 34 20 2a 2f 20  .     /*  74 */ 
ba2e0 22 45 71 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37  "Eq",.     /*  7
ba2f0 35 20 2a 2f 20 22 47 74 22 2c 0a 20 20 20 20 20  5 */ "Gt",.     
ba300 2f 2a 20 20 37 36 20 2a 2f 20 22 4c 65 22 2c 0a  /*  76 */ "Le",.
ba310 20 20 20 20 20 2f 2a 20 20 37 37 20 2a 2f 20 22       /*  77 */ "
ba320 4c 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 38  Lt",.     /*  78
ba330 20 2a 2f 20 22 47 65 22 2c 0a 20 20 20 20 20 2f   */ "Ge",.     /
ba340 2a 20 20 37 39 20 2a 2f 20 22 4d 61 6b 65 52 65  *  79 */ "MakeRe
ba350 63 6f 72 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20  cord",.     /*  
ba360 38 30 20 2a 2f 20 22 42 69 74 41 6e 64 22 2c 0a  80 */ "BitAnd",.
ba370 20 20 20 20 20 2f 2a 20 20 38 31 20 2a 2f 20 22       /*  81 */ "
ba380 42 69 74 4f 72 22 2c 0a 20 20 20 20 20 2f 2a 20  BitOr",.     /* 
ba390 20 38 32 20 2a 2f 20 22 53 68 69 66 74 4c 65 66   82 */ "ShiftLef
ba3a0 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 33 20  t",.     /*  83 
ba3b0 2a 2f 20 22 53 68 69 66 74 52 69 67 68 74 22 2c  */ "ShiftRight",
ba3c0 0a 20 20 20 20 20 2f 2a 20 20 38 34 20 2a 2f 20  .     /*  84 */ 
ba3d0 22 41 64 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20  "Add",.     /*  
ba3e0 38 35 20 2a 2f 20 22 53 75 62 74 72 61 63 74 22  85 */ "Subtract"
ba3f0 2c 0a 20 20 20 20 20 2f 2a 20 20 38 36 20 2a 2f  ,.     /*  86 */
ba400 20 22 4d 75 6c 74 69 70 6c 79 22 2c 0a 20 20 20   "Multiply",.   
ba410 20 20 2f 2a 20 20 38 37 20 2a 2f 20 22 44 69 76    /*  87 */ "Div
ba420 69 64 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38  ide",.     /*  8
ba430 38 20 2a 2f 20 22 52 65 6d 61 69 6e 64 65 72 22  8 */ "Remainder"
ba440 2c 0a 20 20 20 20 20 2f 2a 20 20 38 39 20 2a 2f  ,.     /*  89 */
ba450 20 22 43 6f 6e 63 61 74 22 2c 0a 20 20 20 20 20   "Concat",.     
ba460 2f 2a 20 20 39 30 20 2a 2f 20 22 52 65 73 75 6c  /*  90 */ "Resul
ba470 74 52 6f 77 22 2c 0a 20 20 20 20 20 2f 2a 20 20  tRow",.     /*  
ba480 39 31 20 2a 2f 20 22 44 65 6c 65 74 65 22 2c 0a  91 */ "Delete",.
ba490 20 20 20 20 20 2f 2a 20 20 39 32 20 2a 2f 20 22       /*  92 */ "
ba4a0 41 67 67 46 69 6e 61 6c 22 2c 0a 20 20 20 20 20  AggFinal",.     
ba4b0 2f 2a 20 20 39 33 20 2a 2f 20 22 42 69 74 4e 6f  /*  93 */ "BitNo
ba4c0 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 34 20  t",.     /*  94 
ba4d0 2a 2f 20 22 53 74 72 69 6e 67 38 22 2c 0a 20 20  */ "String8",.  
ba4e0 20 20 20 2f 2a 20 20 39 35 20 2a 2f 20 22 43 6f     /*  95 */ "Co
ba4f0 6d 70 61 72 65 22 2c 0a 20 20 20 20 20 2f 2a 20  mpare",.     /* 
ba500 20 39 36 20 2a 2f 20 22 47 6f 74 6f 22 2c 0a 20   96 */ "Goto",. 
ba510 20 20 20 20 2f 2a 20 20 39 37 20 2a 2f 20 22 54      /*  97 */ "T
ba520 61 62 6c 65 4c 6f 63 6b 22 2c 0a 20 20 20 20 20  ableLock",.     
ba530 2f 2a 20 20 39 38 20 2a 2f 20 22 43 6c 65 61 72  /*  98 */ "Clear
ba540 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 39 20 2a  ",.     /*  99 *
ba550 2f 20 22 56 65 72 69 66 79 43 6f 6f 6b 69 65 22  / "VerifyCookie"
ba560 2c 0a 20 20 20 20 20 2f 2a 20 31 30 30 20 2a 2f  ,.     /* 100 */
ba570 20 22 41 67 67 53 74 65 70 22 2c 0a 20 20 20 20   "AggStep",.    
ba580 20 2f 2a 20 31 30 31 20 2a 2f 20 22 54 72 61 6e   /* 101 */ "Tran
ba590 73 61 63 74 69 6f 6e 22 2c 0a 20 20 20 20 20 2f  saction",.     /
ba5a0 2a 20 31 30 32 20 2a 2f 20 22 56 46 69 6c 74 65  * 102 */ "VFilte
ba5b0 72 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 33 20  r",.     /* 103 
ba5c0 2a 2f 20 22 56 44 65 73 74 72 6f 79 22 2c 0a 20  */ "VDestroy",. 
ba5d0 20 20 20 20 2f 2a 20 31 30 34 20 2a 2f 20 22 4e      /* 104 */ "N
ba5e0 65 78 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30  ext",.     /* 10
ba5f0 35 20 2a 2f 20 22 43 6f 75 6e 74 22 2c 0a 20 20  5 */ "Count",.  
ba600 20 20 20 2f 2a 20 31 30 36 20 2a 2f 20 22 49 64     /* 106 */ "Id
ba610 78 49 6e 73 65 72 74 22 2c 0a 20 20 20 20 20 2f  xInsert",.     /
ba620 2a 20 31 30 37 20 2a 2f 20 22 53 65 65 6b 47 65  * 107 */ "SeekGe
ba630 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 38 20 2a  ",.     /* 108 *
ba640 2f 20 22 49 6e 73 65 72 74 22 2c 0a 20 20 20 20  / "Insert",.    
ba650 20 2f 2a 20 31 30 39 20 2a 2f 20 22 44 65 73 74   /* 109 */ "Dest
ba660 72 6f 79 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31  roy",.     /* 11
ba670 30 20 2a 2f 20 22 52 65 61 64 43 6f 6f 6b 69 65  0 */ "ReadCookie
ba680 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 31 20 2a  ",.     /* 111 *
ba690 2f 20 22 52 6f 77 53 65 74 54 65 73 74 22 2c 0a  / "RowSetTest",.
ba6a0 20 20 20 20 20 2f 2a 20 31 31 32 20 2a 2f 20 22       /* 112 */ "
ba6b0 4c 6f 61 64 41 6e 61 6c 79 73 69 73 22 2c 0a 20  LoadAnalysis",. 
ba6c0 20 20 20 20 2f 2a 20 31 31 33 20 2a 2f 20 22 45      /* 113 */ "E
ba6d0 78 70 6c 61 69 6e 22 2c 0a 20 20 20 20 20 2f 2a  xplain",.     /*
ba6e0 20 31 31 34 20 2a 2f 20 22 48 61 6c 74 49 66 4e   114 */ "HaltIfN
ba6f0 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31  ull",.     /* 11
ba700 35 20 2a 2f 20 22 4f 70 65 6e 50 73 65 75 64 6f  5 */ "OpenPseudo
ba710 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 36 20 2a  ",.     /* 116 *
ba720 2f 20 22 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  / "OpenEphemeral
ba730 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 37 20 2a  ",.     /* 117 *
ba740 2f 20 22 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 2f  / "Null",.     /
ba750 2a 20 31 31 38 20 2a 2f 20 22 4d 6f 76 65 22 2c  * 118 */ "Move",
ba760 0a 20 20 20 20 20 2f 2a 20 31 31 39 20 2a 2f 20  .     /* 119 */ 
ba770 22 42 6c 6f 62 22 2c 0a 20 20 20 20 20 2f 2a 20  "Blob",.     /* 
ba780 31 32 30 20 2a 2f 20 22 52 65 77 69 6e 64 22 2c  120 */ "Rewind",
ba790 0a 20 20 20 20 20 2f 2a 20 31 32 31 20 2a 2f 20  .     /* 121 */ 
ba7a0 22 53 65 65 6b 47 74 22 2c 0a 20 20 20 20 20 2f  "SeekGt",.     /
ba7b0 2a 20 31 32 32 20 2a 2f 20 22 56 42 65 67 69 6e  * 122 */ "VBegin
ba7c0 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 33 20 2a  ",.     /* 123 *
ba7d0 2f 20 22 56 55 70 64 61 74 65 22 2c 0a 20 20 20  / "VUpdate",.   
ba7e0 20 20 2f 2a 20 31 32 34 20 2a 2f 20 22 49 66 5a    /* 124 */ "IfZ
ba7f0 65 72 6f 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32  ero",.     /* 12
ba800 35 20 2a 2f 20 22 56 43 72 65 61 74 65 22 2c 0a  5 */ "VCreate",.
ba810 20 20 20 20 20 2f 2a 20 31 32 36 20 2a 2f 20 22       /* 126 */ "
ba820 46 6f 75 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20  Found",.     /* 
ba830 31 32 37 20 2a 2f 20 22 49 66 50 6f 73 22 2c 0a  127 */ "IfPos",.
ba840 20 20 20 20 20 2f 2a 20 31 32 38 20 2a 2f 20 22       /* 128 */ "
ba850 4e 75 6c 6c 52 6f 77 22 2c 0a 20 20 20 20 20 2f  NullRow",.     /
ba860 2a 20 31 32 39 20 2a 2f 20 22 4a 75 6d 70 22 2c  * 129 */ "Jump",
ba870 0a 20 20 20 20 20 2f 2a 20 31 33 30 20 2a 2f 20  .     /* 130 */ 
ba880 22 52 65 61 6c 22 2c 0a 20 20 20 20 20 2f 2a 20  "Real",.     /* 
ba890 31 33 31 20 2a 2f 20 22 50 65 72 6d 75 74 61 74  131 */ "Permutat
ba8a0 69 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33  ion",.     /* 13
ba8b0 32 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33  2 */ "NotUsed_13
ba8c0 32 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 33 20  2",.     /* 133 
ba8d0 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 33 22  */ "NotUsed_133"
ba8e0 2c 0a 20 20 20 20 20 2f 2a 20 31 33 34 20 2a 2f  ,.     /* 134 */
ba8f0 20 22 4e 6f 74 55 73 65 64 5f 31 33 34 22 2c 0a   "NotUsed_134",.
ba900 20 20 20 20 20 2f 2a 20 31 33 35 20 2a 2f 20 22       /* 135 */ "
ba910 4e 6f 74 55 73 65 64 5f 31 33 35 22 2c 0a 20 20  NotUsed_135",.  
ba920 20 20 20 2f 2a 20 31 33 36 20 2a 2f 20 22 4e 6f     /* 136 */ "No
ba930 74 55 73 65 64 5f 31 33 36 22 2c 0a 20 20 20 20  tUsed_136",.    
ba940 20 2f 2a 20 31 33 37 20 2a 2f 20 22 4e 6f 74 55   /* 137 */ "NotU
ba950 73 65 64 5f 31 33 37 22 2c 0a 20 20 20 20 20 2f  sed_137",.     /
ba960 2a 20 31 33 38 20 2a 2f 20 22 4e 6f 74 55 73 65  * 138 */ "NotUse
ba970 64 5f 31 33 38 22 2c 0a 20 20 20 20 20 2f 2a 20  d_138",.     /* 
ba980 31 33 39 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f  139 */ "NotUsed_
ba990 31 33 39 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34  139",.     /* 14
ba9a0 30 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 34  0 */ "NotUsed_14
ba9b0 30 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 31 20  0",.     /* 141 
ba9c0 2a 2f 20 22 54 6f 54 65 78 74 22 2c 0a 20 20 20  */ "ToText",.   
ba9d0 20 20 2f 2a 20 31 34 32 20 2a 2f 20 22 54 6f 42    /* 142 */ "ToB
ba9e0 6c 6f 62 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34  lob",.     /* 14
ba9f0 33 20 2a 2f 20 22 54 6f 4e 75 6d 65 72 69 63 22  3 */ "ToNumeric"
baa00 2c 0a 20 20 20 20 20 2f 2a 20 31 34 34 20 2a 2f  ,.     /* 144 */
baa10 20 22 54 6f 49 6e 74 22 2c 0a 20 20 20 20 20 2f   "ToInt",.     /
baa20 2a 20 31 34 35 20 2a 2f 20 22 54 6f 52 65 61 6c  * 145 */ "ToReal
baa30 22 2c 0a 20 20 7d 3b 0a 20 20 72 65 74 75 72 6e  ",.  };.  return
baa40 20 61 7a 4e 61 6d 65 5b 69 5d 3b 0a 7d 0a 23 65   azName[i];.}.#e
baa50 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ndif../*********
baa60 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 70 63  ***** End of opc
baa70 6f 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  odes.c *********
baa80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
baa90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
baaa0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
baab0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
baac0 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a   os_os2.c ******
baad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
baae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
baaf0 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36  ****/./*.** 2006
bab00 20 46 65 62 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68   Feb 14.**.** Th
bab10 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
bab20 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
bab30 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
bab40 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
bab50 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
bab60 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
bab70 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
bab80 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
bab90 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
baba0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
babb0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
babc0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
babd0 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
babe0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
babf0 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
bac00 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
bac10 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
bac20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bac30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bac40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bac50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bac60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
bac70 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
bac80 61 69 6e 73 20 63 6f 64 65 20 74 68 61 74 20 69  ains code that i
bac90 73 20 73 70 65 63 69 66 69 63 20 74 6f 20 4f 53  s specific to OS
baca0 2f 32 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f  /2..**.** $Id: o
bacb0 73 5f 6f 73 32 2e 63 2c 76 20 31 2e 36 33 20 32  s_os2.c,v 1.63 2
bacc0 30 30 38 2f 31 32 2f 31 30 20 31 39 3a 32 36 3a  008/12/10 19:26:
bacd0 32 34 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a  24 drh Exp $.*/.
bace0 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  ..#if SQLITE_OS_
bacf0 4f 53 32 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 6f 74  OS2../*.** A Not
bad00 65 20 41 62 6f 75 74 20 4d 65 6d 6f 72 79 20 41  e About Memory A
bad10 6c 6c 6f 63 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a  llocation:.**.**
bad20 20 54 68 69 73 20 64 72 69 76 65 72 20 75 73 65   This driver use
bad30 73 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28  s malloc()/free(
bad40 29 20 64 69 72 65 63 74 6c 79 20 72 61 74 68 65  ) directly rathe
bad50 72 20 74 68 61 6e 20 67 6f 69 6e 67 20 74 68 72  r than going thr
bad60 6f 75 67 68 0a 2a 2a 20 74 68 65 20 53 51 4c 69  ough.** the SQLi
bad70 74 65 2d 77 72 61 70 70 65 72 73 20 73 71 6c 69  te-wrappers sqli
bad80 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2f 73 71 6c  te3_malloc()/sql
bad90 69 74 65 33 5f 66 72 65 65 28 29 2e 20 20 54 68  ite3_free().  Th
bada0 6f 73 65 20 77 72 61 70 70 65 72 73 0a 2a 2a 20  ose wrappers.** 
badb0 61 72 65 20 64 65 73 69 67 6e 65 64 20 66 6f 72  are designed for
badc0 20 75 73 65 20 6f 6e 20 65 6d 62 65 64 64 65 64   use on embedded
badd0 20 73 79 73 74 65 6d 73 20 77 68 65 72 65 20 6d   systems where m
bade0 65 6d 6f 72 79 20 69 73 20 73 63 61 72 63 65 20  emory is scarce 
badf0 61 6e 64 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61  and.** malloc fa
bae00 69 6c 75 72 65 73 20 68 61 70 70 65 6e 20 66 72  ilures happen fr
bae10 65 71 75 65 6e 74 6c 79 2e 20 20 4f 53 2f 32 20  equently.  OS/2 
bae20 64 6f 65 73 20 6e 6f 74 20 74 79 70 69 63 61 6c  does not typical
bae30 6c 79 20 72 75 6e 20 6f 6e 0a 2a 2a 20 65 6d 62  ly run on.** emb
bae40 65 64 64 65 64 20 73 79 73 74 65 6d 73 2c 20 61  edded systems, a
bae50 6e 64 20 77 68 65 6e 20 69 74 20 64 6f 65 73 20  nd when it does 
bae60 74 68 65 20 64 65 76 65 6c 6f 70 65 72 73 20 6e  the developers n
bae70 6f 72 6d 61 6c 6c 79 20 68 61 76 65 20 62 69 67  ormally have big
bae80 67 65 72 0a 2a 2a 20 70 72 6f 62 6c 65 6d 73 20  ger.** problems 
bae90 74 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 20 74  to worry about t
baea0 68 61 6e 20 72 75 6e 6e 69 6e 67 20 6f 75 74 20  han running out 
baeb0 6f 66 20 6d 65 6d 6f 72 79 2e 20 20 53 6f 20 74  of memory.  So t
baec0 68 65 72 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 61  here is not.** a
baed0 20 63 6f 6d 70 65 6c 6c 69 6e 67 20 6e 65 65 64   compelling need
baee0 20 74 6f 20 75 73 65 20 74 68 65 20 77 72 61 70   to use the wrap
baef0 70 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 20  pers..**.** But 
baf00 74 68 65 72 65 20 69 73 20 61 20 67 6f 6f 64 20  there is a good 
baf10 72 65 61 73 6f 6e 20 74 6f 20 6e 6f 74 20 75 73  reason to not us
baf20 65 20 74 68 65 20 77 72 61 70 70 65 72 73 2e 20  e the wrappers. 
baf30 20 49 66 20 77 65 20 75 73 65 20 74 68 65 0a 2a   If we use the.*
baf40 2a 20 77 72 61 70 70 65 72 73 20 74 68 65 6e 20  * wrappers then 
baf50 77 65 20 77 69 6c 6c 20 67 65 74 20 73 69 6d 75  we will get simu
baf60 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 28 29 20 66  lated malloc() f
baf70 61 69 6c 75 72 65 73 20 77 69 74 68 69 6e 20 74  ailures within t
baf80 68 69 73 0a 2a 2a 20 64 72 69 76 65 72 2e 20 20  his.** driver.  
baf90 41 6e 64 20 74 68 61 74 20 63 61 75 73 65 73 20  And that causes 
bafa0 61 6c 6c 20 6b 69 6e 64 73 20 6f 66 20 70 72 6f  all kinds of pro
bafb0 62 6c 65 6d 73 20 66 6f 72 20 6f 75 72 20 74 65  blems for our te
bafc0 73 74 73 2e 20 20 57 65 0a 2a 2a 20 63 6f 75 6c  sts.  We.** coul
bafd0 64 20 65 6e 68 61 6e 63 65 20 53 51 4c 69 74 65  d enhance SQLite
bafe0 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 73 69   to deal with si
baff0 6d 75 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 20 66  mulated malloc f
bb000 61 69 6c 75 72 65 73 20 77 69 74 68 69 6e 0a 2a  ailures within.*
bb010 2a 20 74 68 65 20 4f 53 20 64 72 69 76 65 72 2c  * the OS driver,
bb020 20 62 75 74 20 74 68 65 20 63 6f 64 65 20 74 6f   but the code to
bb030 20 64 65 61 6c 20 77 69 74 68 20 74 68 6f 73 65   deal with those
bb040 20 66 61 69 6c 75 72 65 20 77 6f 75 6c 64 20 6e   failure would n
bb050 6f 74 0a 2a 2a 20 62 65 20 65 78 65 72 63 69 73  ot.** be exercis
bb060 65 64 20 6f 6e 20 4c 69 6e 75 78 20 28 77 68 69  ed on Linux (whi
bb070 63 68 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64  ch does not need
bb080 20 74 6f 20 6d 61 6c 6c 6f 63 28 29 20 69 6e 20   to malloc() in 
bb090 74 68 65 20 64 72 69 76 65 72 29 0a 2a 2a 20 61  the driver).** a
bb0a0 6e 64 20 73 6f 20 77 65 20 77 6f 75 6c 64 20 68  nd so we would h
bb0b0 61 76 65 20 64 69 66 66 69 63 75 6c 74 79 20 77  ave difficulty w
bb0c0 72 69 74 69 6e 67 20 63 6f 76 65 72 61 67 65 20  riting coverage 
bb0d0 74 65 73 74 73 20 66 6f 72 20 74 68 61 74 0a 2a  tests for that.*
bb0e0 2a 20 63 6f 64 65 2e 20 20 42 65 74 74 65 72 20  * code.  Better 
bb0f0 74 6f 20 6c 65 61 76 65 20 74 68 65 20 63 6f 64  to leave the cod
bb100 65 20 6f 75 74 2c 20 77 65 20 74 68 69 6e 6b 2e  e out, we think.
bb110 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74  .**.** The point
bb120 20 6f 66 20 74 68 69 73 20 64 69 73 63 75 73 73   of this discuss
bb130 69 6f 6e 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77  ion is as follow
bb140 73 3a 20 20 57 68 65 6e 20 63 72 65 61 74 69 6e  s:  When creatin
bb150 67 20 61 20 6e 65 77 0a 2a 2a 20 4f 53 20 6c 61  g a new.** OS la
bb160 79 65 72 20 66 6f 72 20 61 6e 20 65 6d 62 65 64  yer for an embed
bb170 64 65 64 20 73 79 73 74 65 6d 2c 20 69 66 20 79  ded system, if y
bb180 6f 75 20 75 73 65 20 74 68 69 73 20 66 69 6c 65  ou use this file
bb190 20 61 73 20 61 6e 20 65 78 61 6d 70 6c 65 2c 0a   as an example,.
bb1a0 2a 2a 20 61 76 6f 69 64 20 74 68 65 20 75 73 65  ** avoid the use
bb1b0 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65   of malloc()/fre
bb1c0 65 28 29 2e 20 20 54 68 6f 73 65 20 72 6f 75 74  e().  Those rout
bb1d0 69 6e 65 73 20 77 6f 72 6b 20 6f 6b 20 6f 6e 20  ines work ok on 
bb1e0 4f 53 2f 32 0a 2a 2a 20 64 65 73 6b 74 6f 70 73  OS/2.** desktops
bb1f0 20 62 75 74 20 6e 6f 74 20 73 6f 20 77 65 6c 6c   but not so well
bb200 20 69 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73   in embedded sys
bb210 74 65 6d 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  tems..*/../*.** 
bb220 4d 61 63 72 6f 73 20 75 73 65 64 20 74 6f 20 64  Macros used to d
bb230 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72  etermine whether
bb240 20 6f 72 20 6e 6f 74 20 74 6f 20 75 73 65 20 74   or not to use t
bb250 68 72 65 61 64 73 2e 0a 2a 2f 0a 23 69 66 20 64  hreads..*/.#if d
bb260 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 48  efined(SQLITE_TH
bb270 52 45 41 44 53 41 46 45 29 20 26 26 20 53 51 4c  READSAFE) && SQL
bb280 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 23  ITE_THREADSAFE.#
bb290 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f   define SQLITE_O
bb2a0 53 32 5f 54 48 52 45 41 44 53 20 31 0a 23 65 6e  S2_THREADS 1.#en
bb2b0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75  dif../*.** Inclu
bb2c0 64 65 20 63 6f 64 65 20 74 68 61 74 20 69 73 20  de code that is 
bb2d0 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73  common to all os
bb2e0 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a  _*.c files.*/./*
bb2f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
bb300 63 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e  clude os_common.
bb310 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
bb320 6f 66 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a  of os_os2.c ****
bb330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
bb340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
bb350 67 69 6e 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d  gin file os_comm
bb360 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  on.h ***********
bb370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
bb390 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a  .** 2004 May 22.
bb3a0 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
bb3b0 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
bb3c0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
bb3d0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
bb3e0 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
bb3f0 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
bb400 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
bb410 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
bb420 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
bb430 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
bb440 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
bb450 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
bb460 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
bb470 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
bb480 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
bb490 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
bb4a0 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
bb4b0 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
bb4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb4f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb500 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ***.**.** This f
bb510 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6d 61 63  ile contains mac
bb520 72 6f 73 20 61 6e 64 20 61 20 6c 69 74 74 6c 65  ros and a little
bb530 20 62 69 74 20 6f 66 20 63 6f 64 65 20 74 68 61   bit of code tha
bb540 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a  t is common to.*
bb550 2a 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 6c 61  * all of the pla
bb560 74 66 6f 72 6d 2d 73 70 65 63 69 66 69 63 20 66  tform-specific f
bb570 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 29 20 61 6e  iles (os_*.c) an
bb580 64 20 69 73 20 23 69 6e 63 6c 75 64 65 64 20 69  d is #included i
bb590 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a 20 66 69 6c  nto those.** fil
bb5a0 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  es..**.** This f
bb5b0 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 23 69  ile should be #i
bb5c0 6e 63 6c 75 64 65 64 20 62 79 20 74 68 65 20 6f  ncluded by the o
bb5d0 73 5f 2a 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79  s_*.c files only
bb5e0 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 61 0a 2a  .  It is not a.*
bb5f0 2a 20 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73  * general purpos
bb600 65 20 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a  e header file..*
bb610 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f 63 6f 6d  *.** $Id: os_com
bb620 6d 6f 6e 2e 68 2c 76 20 31 2e 33 38 20 32 30 30  mon.h,v 1.38 200
bb630 39 2f 30 32 2f 32 34 20 31 38 3a 34 30 3a 35 30  9/02/24 18:40:50
bb640 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70   danielk1977 Exp
bb650 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 4f   $.*/.#ifndef _O
bb660 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66  S_COMMON_H_.#def
bb670 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48  ine _OS_COMMON_H
bb680 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73  _../*.** At leas
bb690 74 20 74 77 6f 20 62 75 67 73 20 68 61 76 65 20  t two bugs have 
bb6a0 73 6c 69 70 70 65 64 20 69 6e 20 62 65 63 61 75  slipped in becau
bb6b0 73 65 20 77 65 20 63 68 61 6e 67 65 64 20 74 68  se we changed th
bb6c0 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a  e MEMORY_DEBUG.*
bb6d0 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51 4c 49 54  * macro to SQLIT
bb6e0 45 5f 44 45 42 55 47 20 61 6e 64 20 73 6f 6d 65  E_DEBUG and some
bb6f0 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69 6c 65 73   older makefiles
bb700 20 68 61 76 65 20 6e 6f 74 20 79 65 74 20 6d 61   have not yet ma
bb710 64 65 20 74 68 65 0a 2a 2a 20 73 77 69 74 63 68  de the.** switch
bb720 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  .  The following
bb730 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 63 61 74   code should cat
bb740 63 68 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20  ch this problem 
bb750 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e  at compile-time.
bb760 0a 2a 2f 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52  .*/.#ifdef MEMOR
bb770 59 5f 44 45 42 55 47 0a 23 20 65 72 72 6f 72 20  Y_DEBUG.# error 
bb780 22 54 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55  "The MEMORY_DEBU
bb790 47 20 6d 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c  G macro is obsol
bb7a0 65 74 65 2e 20 20 55 73 65 20 53 51 4c 49 54 45  ete.  Use SQLITE
bb7b0 5f 44 45 42 55 47 20 69 6e 73 74 65 61 64 2e 22  _DEBUG instead."
bb7c0 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
bb7d0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c  SQLITE_DEBUG.SQL
bb7e0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
bb7f0 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 3d  sqlite3OSTrace =
bb800 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 53 54 52   0;.#define OSTR
bb810 41 43 45 31 28 58 29 20 20 20 20 20 20 20 20 20  ACE1(X)         
bb820 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61  if( sqlite3OSTra
bb830 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75  ce ) sqlite3Debu
bb840 67 50 72 69 6e 74 66 28 58 29 0a 23 64 65 66 69  gPrintf(X).#defi
bb850 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29  ne OSTRACE2(X,Y)
bb860 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
bb870 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69  e3OSTrace ) sqli
bb880 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58  te3DebugPrintf(X
bb890 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,Y).#define OSTR
bb8a0 41 43 45 33 28 58 2c 59 2c 5a 29 20 20 20 20 20  ACE3(X,Y,Z)     
bb8b0 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61  if( sqlite3OSTra
bb8c0 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75  ce ) sqlite3Debu
bb8d0 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 29 0a 23  gPrintf(X,Y,Z).#
bb8e0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28  define OSTRACE4(
bb8f0 58 2c 59 2c 5a 2c 41 29 20 20 20 69 66 28 20 73  X,Y,Z,A)   if( s
bb900 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20  qlite3OSTrace ) 
bb910 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
bb920 74 66 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66  tf(X,Y,Z,A).#def
bb930 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59  ine OSTRACE5(X,Y
bb940 2c 5a 2c 41 2c 42 29 20 69 66 28 20 73 71 6c 69  ,Z,A,B) if( sqli
bb950 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c  te3OSTrace ) sql
bb960 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
bb970 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69  X,Y,Z,A,B).#defi
bb980 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c  ne OSTRACE6(X,Y,
bb990 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20 20 20 20 69  Z,A,B,C) \.    i
bb9a0 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65  f(sqlite3OSTrace
bb9b0 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72  ) sqlite3DebugPr
bb9c0 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43  intf(X,Y,Z,A,B,C
bb9d0 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
bb9e0 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44  E7(X,Y,Z,A,B,C,D
bb9f0 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74  ) \.    if(sqlit
bba00 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74  e3OSTrace) sqlit
bba10 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c  e3DebugPrintf(X,
bba20 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6c  Y,Z,A,B,C,D).#el
bba30 73 65 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  se.#define OSTRA
bba40 43 45 31 28 58 29 0a 23 64 65 66 69 6e 65 20 4f  CE1(X).#define O
bba50 53 54 52 41 43 45 32 28 58 2c 59 29 0a 23 64 65  STRACE2(X,Y).#de
bba60 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c  fine OSTRACE3(X,
bba70 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54  Y,Z).#define OST
bba80 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 0a 23  RACE4(X,Y,Z,A).#
bba90 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28  define OSTRACE5(
bbaa0 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69  X,Y,Z,A,B).#defi
bbab0 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c  ne OSTRACE6(X,Y,
bbac0 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65  Z,A,B,C).#define
bbad0 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c   OSTRACE7(X,Y,Z,
bbae0 41 2c 42 2c 43 2c 44 29 0a 23 65 6e 64 69 66 0a  A,B,C,D).#endif.
bbaf0 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f  ./*.** Macros fo
bbb00 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 72  r performance tr
bbb10 61 63 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79  acing.  Normally
bbb20 20 74 75 72 6e 65 64 20 6f 66 66 2e 20 20 4f 6e   turned off.  On
bbb30 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 69  ly works.** on i
bbb40 34 38 36 20 68 61 72 64 77 61 72 65 2e 0a 2a 2f  486 hardware..*/
bbb50 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50  .#ifdef SQLITE_P
bbb60 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52 41 43 45  ERFORMANCE_TRACE
bbb70 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 2e  ../* .** hwtime.
bbb80 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e  h contains inlin
bbb90 65 20 61 73 73 65 6d 62 6c 65 72 20 63 6f 64 65  e assembler code
bbba0 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e   for implementin
bbbb0 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66 6f  g .** high-perfo
bbbc0 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67 20 72 6f  rmance timing ro
bbbd0 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a  utines..*/./****
bbbe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
bbbf0 64 65 20 68 77 74 69 6d 65 2e 68 20 69 6e 20 74  de hwtime.h in t
bbc00 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f  he middle of os_
bbc10 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a  common.h *******
bbc20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
bbc30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
bbc40 20 66 69 6c 65 20 68 77 74 69 6d 65 2e 68 20 2a   file hwtime.h *
bbc50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbc60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbc70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
bbc80 20 32 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a 0a   2008 May 27.**.
bbc90 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
bbca0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
bbcb0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
bbcc0 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
bbcd0 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
bbce0 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
bbcf0 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
bbd00 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
bbd10 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
bbd20 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
bbd30 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
bbd40 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
bbd50 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
bbd60 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
bbd70 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
bbd80 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
bbd90 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
bbda0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
bbdb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbdc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbdd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbde0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbdf0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
bbe00 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65   contains inline
bbe10 20 61 73 6d 20 63 6f 64 65 20 66 6f 72 20 72 65   asm code for re
bbe20 74 72 69 65 76 69 6e 67 20 22 68 69 67 68 2d 70  trieving "high-p
bbe30 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 63  erformance".** c
bbe40 6f 75 6e 74 65 72 73 20 66 6f 72 20 78 38 36 20  ounters for x86 
bbe50 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a 2a 0a 2a  class CPUs..**.*
bbe60 2a 20 24 49 64 3a 20 68 77 74 69 6d 65 2e 68 2c  * $Id: hwtime.h,
bbe70 76 20 31 2e 33 20 32 30 30 38 2f 30 38 2f 30 31  v 1.3 2008/08/01
bbe80 20 31 34 3a 33 33 3a 31 35 20 73 68 61 6e 65 20   14:33:15 shane 
bbe90 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66  Exp $.*/.#ifndef
bbea0 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 23 64 65 66   _HWTIME_H_.#def
bbeb0 69 6e 65 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a  ine _HWTIME_H_..
bbec0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
bbed0 69 6e 67 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79  ing routine only
bbee0 20 77 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 69 75   works on pentiu
bbef0 6d 2d 63 6c 61 73 73 20 28 6f 72 20 6e 65 77 65  m-class (or newe
bbf00 72 29 20 70 72 6f 63 65 73 73 6f 72 73 2e 0a 2a  r) processors..*
bbf10 2a 20 49 74 20 75 73 65 73 20 74 68 65 20 52 44  * It uses the RD
bbf20 54 53 43 20 6f 70 63 6f 64 65 20 74 6f 20 72 65  TSC opcode to re
bbf30 61 64 20 74 68 65 20 63 79 63 6c 65 20 63 6f 75  ad the cycle cou
bbf40 6e 74 20 76 61 6c 75 65 20 6f 75 74 20 6f 66 20  nt value out of 
bbf50 74 68 65 0a 2a 2a 20 70 72 6f 63 65 73 73 6f 72  the.** processor
bbf60 20 61 6e 64 20 72 65 74 75 72 6e 73 20 74 68 61   and returns tha
bbf70 74 20 76 61 6c 75 65 2e 20 20 54 68 69 73 20 63  t value.  This c
bbf80 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20 68  an be used for h
bbf90 69 67 68 2d 72 65 73 0a 2a 2a 20 70 72 6f 66 69  igh-res.** profi
bbfa0 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28 64 65  ling..*/.#if (de
bbfb0 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20  fined(__GNUC__) 
bbfc0 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f  || defined(_MSC_
bbfd0 56 45 52 29 29 20 26 26 20 5c 0a 20 20 20 20 20  VER)) && \.     
bbfe0 20 28 64 65 66 69 6e 65 64 28 69 33 38 36 29 20   (defined(i386) 
bbff0 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 38  || defined(__i38
bc000 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  6__) || defined(
bc010 5f 4d 5f 49 58 38 36 29 29 0a 0a 20 20 23 69 66  _M_IX86))..  #if
bc020 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f   defined(__GNUC_
bc030 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f  _)..  __inline__
bc040 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73   sqlite_uint64 s
bc050 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69  qlite3Hwtime(voi
bc060 64 29 7b 0a 20 20 20 20 20 75 6e 73 69 67 6e 65  d){.     unsigne
bc070 64 20 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a 20 20  d int lo, hi;.  
bc080 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c     __asm__ __vol
bc090 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22  atile__ ("rdtsc"
bc0a0 20 3a 20 22 3d 61 22 20 28 6c 6f 29 2c 20 22 3d   : "=a" (lo), "=
bc0b0 64 22 20 28 68 69 29 29 3b 0a 20 20 20 20 20 72  d" (hi));.     r
bc0c0 65 74 75 72 6e 20 28 73 71 6c 69 74 65 5f 75 69  eturn (sqlite_ui
bc0d0 6e 74 36 34 29 68 69 20 3c 3c 20 33 32 20 7c 20  nt64)hi << 32 | 
bc0e0 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c 69 66  lo;.  }..  #elif
bc0f0 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45   defined(_MSC_VE
bc100 52 29 0a 0a 20 20 5f 5f 64 65 63 6c 73 70 65 63  R)..  __declspec
bc110 28 6e 61 6b 65 64 29 20 5f 5f 69 6e 6c 69 6e 65  (naked) __inline
bc120 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 5f   sqlite_uint64 _
bc130 5f 63 64 65 63 6c 20 73 71 6c 69 74 65 33 48 77  _cdecl sqlite3Hw
bc140 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20  time(void){.    
bc150 20 5f 5f 61 73 6d 20 7b 0a 20 20 20 20 20 20 20   __asm {.       
bc160 20 72 64 74 73 63 0a 20 20 20 20 20 20 20 20 72   rdtsc.        r
bc170 65 74 20 20 20 20 20 20 20 3b 20 72 65 74 75 72  et       ; retur
bc180 6e 20 76 61 6c 75 65 20 61 74 20 45 44 58 3a 45  n value at EDX:E
bc190 41 58 0a 20 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  AX.     }.  }.. 
bc1a0 20 23 65 6e 64 69 66 0a 0a 23 65 6c 69 66 20 28   #endif..#elif (
bc1b0 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f  defined(__GNUC__
bc1c0 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 78  ) && defined(__x
bc1d0 38 36 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69  86_64__))..  __i
bc1e0 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75  nline__ sqlite_u
bc1f0 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74  int64 sqlite3Hwt
bc200 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20  ime(void){.     
bc210 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 76   unsigned long v
bc220 61 6c 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f  al;.      __asm_
bc230 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28  _ __volatile__ (
bc240 22 72 64 74 73 63 22 20 3a 20 22 3d 41 22 20 28  "rdtsc" : "=A" (
bc250 76 61 6c 29 29 3b 0a 20 20 20 20 20 20 72 65 74  val));.      ret
bc260 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23  urn val;.  }. .#
bc270 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f  elif (defined(__
bc280 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e  GNUC__) && defin
bc290 65 64 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a 20 20  ed(__ppc__))..  
bc2a0 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74  __inline__ sqlit
bc2b0 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33  e_uint64 sqlite3
bc2c0 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20  Hwtime(void){.  
bc2d0 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e      unsigned lon
bc2e0 67 20 6c 6f 6e 67 20 72 65 74 76 61 6c 3b 0a 20  g long retval;. 
bc2f0 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f       unsigned lo
bc300 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20 20 20 20 5f  ng junk;.      _
bc310 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c  _asm__ __volatil
bc320 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20 20 20 20 20  e__ ("\n\.      
bc330 20 20 20 20 31 3a 20 20 20 20 20 20 6d 66 74 62      1:      mftb
bc340 75 20 20 20 25 31 5c 6e 5c 0a 20 20 20 20 20 20  u   %1\n\.      
bc350 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 62              mftb
bc360 20 20 20 20 25 4c 30 5c 6e 5c 0a 20 20 20 20 20      %L0\n\.     
bc370 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74               mft
bc380 62 75 20 20 20 25 30 5c 6e 5c 0a 20 20 20 20 20  bu   %0\n\.     
bc390 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6d 70               cmp
bc3a0 77 20 20 20 20 25 30 2c 25 31 5c 6e 5c 0a 20 20  w    %0,%1\n\.  
bc3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bc3c0 62 6e 65 20 20 20 20 20 31 62 22 0a 20 20 20 20  bne     1b".    
bc3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 3a 20                : 
bc3e0 22 3d 72 22 20 28 72 65 74 76 61 6c 29 2c 20 22  "=r" (retval), "
bc3f0 3d 72 22 20 28 6a 75 6e 6b 29 29 3b 0a 20 20 20  =r" (junk));.   
bc400 20 20 20 72 65 74 75 72 6e 20 72 65 74 76 61 6c     return retval
bc410 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20  ;.  }..#else..  
bc420 23 65 72 72 6f 72 20 4e 65 65 64 20 69 6d 70 6c  #error Need impl
bc430 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71  ementation of sq
bc440 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f  lite3Hwtime() fo
bc450 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2e  r your platform.
bc460 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63  ..  /*.  ** To c
bc470 6f 6d 70 69 6c 65 20 77 69 74 68 6f 75 74 20 69  ompile without i
bc480 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 73 71 6c 69  mplementing sqli
bc490 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20  te3Hwtime() for 
bc4a0 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2c 0a 20  your platform,. 
bc4b0 20 2a 2a 20 79 6f 75 20 63 61 6e 20 72 65 6d 6f   ** you can remo
bc4c0 76 65 20 74 68 65 20 61 62 6f 76 65 20 23 65 72  ve the above #er
bc4d0 72 6f 72 20 61 6e 64 20 75 73 65 20 74 68 65 20  ror and use the 
bc4e0 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 73  following.  ** s
bc4f0 74 75 62 20 66 75 6e 63 74 69 6f 6e 2e 20 20 59  tub function.  Y
bc500 6f 75 20 77 69 6c 6c 20 6c 6f 73 65 20 74 69 6d  ou will lose tim
bc510 69 6e 67 20 73 75 70 70 6f 72 74 20 66 6f 72 20  ing support for 
bc520 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65  many.  ** of the
bc530 20 64 65 62 75 67 67 69 6e 67 20 61 6e 64 20 74   debugging and t
bc540 65 73 74 69 6e 67 20 75 74 69 6c 69 74 69 65 73  esting utilities
bc550 2c 20 62 75 74 20 69 74 20 73 68 6f 75 6c 64 20  , but it should 
bc560 61 74 0a 20 20 2a 2a 20 6c 65 61 73 74 20 63 6f  at.  ** least co
bc570 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e 2e 0a 20  mpile and run.. 
bc580 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   */.SQLITE_PRIVA
bc590 54 45 20 20 20 73 71 6c 69 74 65 5f 75 69 6e 74  TE   sqlite_uint
bc5a0 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65  64 sqlite3Hwtime
bc5b0 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 28  (void){ return (
bc5c0 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30  (sqlite_uint64)0
bc5d0 29 3b 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a 23 65  ); }..#endif..#e
bc5e0 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
bc5f0 28 5f 48 57 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a  (_HWTIME_H_) */.
bc600 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
bc610 20 45 6e 64 20 6f 66 20 68 77 74 69 6d 65 2e 68   End of hwtime.h
bc620 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
bc630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
bc650 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
bc660 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72   Continuing wher
bc670 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e  e we left off in
bc680 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a   os_common.h ***
bc690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
bc6a0 0a 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f  ..static sqlite_
bc6b0 75 69 6e 74 36 34 20 67 5f 73 74 61 72 74 3b 0a  uint64 g_start;.
bc6c0 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69  static sqlite_ui
bc6d0 6e 74 36 34 20 67 5f 65 6c 61 70 73 65 64 3b 0a  nt64 g_elapsed;.
bc6e0 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54  #define TIMER_ST
bc6f0 41 52 54 20 20 20 20 20 20 20 67 5f 73 74 61 72  ART       g_star
bc700 74 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  t=sqlite3Hwtime(
bc710 29 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f  ).#define TIMER_
bc720 45 4e 44 20 20 20 20 20 20 20 20 20 67 5f 65 6c  END         g_el
bc730 61 70 73 65 64 3d 73 71 6c 69 74 65 33 48 77 74  apsed=sqlite3Hwt
bc740 69 6d 65 28 29 2d 67 5f 73 74 61 72 74 0a 23 64  ime()-g_start.#d
bc750 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50  efine TIMER_ELAP
bc760 53 45 44 20 20 20 20 20 67 5f 65 6c 61 70 73 65  SED     g_elapse
bc770 64 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20  d.#else.#define 
bc780 54 49 4d 45 52 5f 53 54 41 52 54 0a 23 64 65 66  TIMER_START.#def
bc790 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 0a 23 64  ine TIMER_END.#d
bc7a0 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50  efine TIMER_ELAP
bc7b0 53 45 44 20 20 20 20 20 28 28 73 71 6c 69 74 65  SED     ((sqlite
bc7c0 5f 75 69 6e 74 36 34 29 30 29 0a 23 65 6e 64 69  _uint64)0).#endi
bc7d0 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 63  f../*.** If we c
bc7e0 6f 6d 70 69 6c 65 20 77 69 74 68 20 74 68 65 20  ompile with the 
bc7f0 53 51 4c 49 54 45 5f 54 45 53 54 20 6d 61 63 72  SQLITE_TEST macr
bc800 6f 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20  o set, then the 
bc810 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a  following block.
bc820 2a 2a 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c 20  ** of code will 
bc830 67 69 76 65 20 75 73 20 74 68 65 20 61 62 69 6c  give us the abil
bc840 69 74 79 20 74 6f 20 73 69 6d 75 6c 61 74 65 20  ity to simulate 
bc850 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72  a disk I/O error
bc860 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73 20 75 73  .  This.** is us
bc870 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 74  ed for testing t
bc880 68 65 20 49 2f 4f 20 72 65 63 6f 76 65 72 79 20  he I/O recovery 
bc890 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69 66 64 65 66  logic..*/.#ifdef
bc8a0 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
bc8b0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
bc8c0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74  te3_io_error_hit
bc8d0 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
bc8e0 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72   /* Total number
bc8f0 20 6f 66 20 49 2f 4f 20 45 72 72 6f 72 73 20 2a   of I/O Errors *
bc900 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
bc910 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
bc920 72 5f 68 61 72 64 68 69 74 20 3d 20 30 3b 20 20  r_hardhit = 0;  
bc930 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
bc940 6f 66 20 6e 6f 6e 2d 62 65 6e 69 67 6e 20 65 72  of non-benign er
bc950 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41  rors */.SQLITE_A
bc960 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69  PI int sqlite3_i
bc970 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20  o_error_pending 
bc980 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 43  = 0;        /* C
bc990 6f 75 6e 74 20 64 6f 77 6e 20 74 6f 20 66 69 72  ount down to fir
bc9a0 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a  st I/O error */.
bc9b0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
bc9c0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
bc9d0 70 65 72 73 69 73 74 20 3d 20 30 3b 20 20 20 20  persist = 0;    
bc9e0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 49      /* True if I
bc9f0 2f 4f 20 65 72 72 6f 72 73 20 70 65 72 73 69 73  /O errors persis
bca00 74 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  t */.SQLITE_API 
bca10 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  int sqlite3_io_e
bca20 72 72 6f 72 5f 62 65 6e 69 67 6e 20 3d 20 30 3b  rror_benign = 0;
bca30 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
bca40 20 69 66 20 65 72 72 6f 72 73 20 61 72 65 20 62   if errors are b
bca50 65 6e 69 67 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f  enign */.SQLITE_
bca60 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
bca70 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67  diskfull_pending
bca80 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49   = 0;.SQLITE_API
bca90 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73   int sqlite3_dis
bcaa0 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23 64 65 66 69  kfull = 0;.#defi
bcab0 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  ne SimulateIOErr
bcac0 6f 72 42 65 6e 69 67 6e 28 58 29 20 73 71 6c 69  orBenign(X) sqli
bcad0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e  te3_io_error_ben
bcae0 69 67 6e 3d 28 58 29 0a 23 64 65 66 69 6e 65 20  ign=(X).#define 
bcaf0 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
bcb00 43 4f 44 45 29 20 20 5c 0a 20 20 69 66 28 20 28  CODE)  \.  if( (
bcb10 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
bcb20 5f 70 65 72 73 69 73 74 20 26 26 20 73 71 6c 69  _persist && sqli
bcb30 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74  te3_io_error_hit
bcb40 29 20 5c 0a 20 20 20 20 20 20 20 7c 7c 20 73 71  ) \.       || sq
bcb50 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70  lite3_io_error_p
bcb60 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31 20 29 20  ending-- == 1 ) 
bcb70 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   \.             
bcb80 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29   { local_ioerr()
bcb90 3b 20 43 4f 44 45 3b 20 7d 0a 73 74 61 74 69 63  ; CODE; }.static
bcba0 20 76 6f 69 64 20 6c 6f 63 61 6c 5f 69 6f 65 72   void local_ioer
bcbb0 72 28 29 7b 0a 20 20 49 4f 54 52 41 43 45 28 28  r(){.  IOTRACE((
bcbc0 22 49 4f 45 52 52 5c 6e 22 29 29 3b 0a 20 20 73  "IOERR\n"));.  s
bcbd0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
bcbe0 68 69 74 2b 2b 3b 0a 20 20 69 66 28 20 21 73 71  hit++;.  if( !sq
bcbf0 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62  lite3_io_error_b
bcc00 65 6e 69 67 6e 20 29 20 73 71 6c 69 74 65 33 5f  enign ) sqlite3_
bcc10 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74  io_error_hardhit
bcc20 2b 2b 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 53 69  ++;.}.#define Si
bcc30 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72  mulateDiskfullEr
bcc40 72 6f 72 28 43 4f 44 45 29 20 5c 0a 20 20 20 69  ror(CODE) \.   i
bcc50 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66  f( sqlite3_diskf
bcc60 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 29 7b 20 5c  ull_pending ){ \
bcc70 0a 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  .     if( sqlite
bcc80 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69  3_diskfull_pendi
bcc90 6e 67 20 3d 3d 20 31 20 29 7b 20 5c 0a 20 20 20  ng == 1 ){ \.   
bcca0 20 20 20 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28      local_ioerr(
bccb0 29 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69  ); \.       sqli
bccc0 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 31  te3_diskfull = 1
bccd0 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74  ; \.       sqlit
bcce0 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20  e3_io_error_hit 
bccf0 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 43 4f  = 1; \.       CO
bcd00 44 45 3b 20 5c 0a 20 20 20 20 20 7d 65 6c 73 65  DE; \.     }else
bcd10 7b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74  { \.       sqlit
bcd20 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64  e3_diskfull_pend
bcd30 69 6e 67 2d 2d 3b 20 5c 0a 20 20 20 20 20 7d 20  ing--; \.     } 
bcd40 5c 0a 20 20 20 7d 0a 23 65 6c 73 65 0a 23 64 65  \.   }.#else.#de
bcd50 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45  fine SimulateIOE
bcd60 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 0a 23 64  rrorBenign(X).#d
bcd70 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f  efine SimulateIO
bcd80 45 72 72 6f 72 28 41 29 0a 23 64 65 66 69 6e 65  Error(A).#define
bcd90 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c   SimulateDiskful
bcda0 6c 45 72 72 6f 72 28 41 29 0a 23 65 6e 64 69 66  lError(A).#endif
bcdb0 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 65 73  ../*.** When tes
bcdc0 74 69 6e 67 2c 20 6b 65 65 70 20 61 20 63 6f 75  ting, keep a cou
bcdd0 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72  nt of the number
bcde0 20 6f 66 20 6f 70 65 6e 20 66 69 6c 65 73 2e 0a   of open files..
bcdf0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
bce00 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49  _TEST.SQLITE_API
bce10 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65   int sqlite3_ope
bce20 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 20 3d 20 30  n_file_count = 0
bce30 3b 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f  ;.#define OpenCo
bce40 75 6e 74 65 72 28 58 29 20 20 73 71 6c 69 74 65  unter(X)  sqlite
bce50 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e  3_open_file_coun
bce60 74 2b 3d 28 58 29 0a 23 65 6c 73 65 0a 23 64 65  t+=(X).#else.#de
bce70 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72  fine OpenCounter
bce80 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64  (X).#endif..#end
bce90 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f  if /* !defined(_
bcea0 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f  OS_COMMON_H_) */
bceb0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
bcec0 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 63 6f 6d 6d  * End of os_comm
bced0 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  on.h ***********
bcee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcf00 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
bcf10 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
bcf20 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
bcf30 6e 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a  n os_os2.c *****
bcf40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcf50 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6f 73 32  /../*.** The os2
bcf60 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 69  File structure i
bcf70 73 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71  s subclass of sq
bcf80 6c 69 74 65 33 5f 66 69 6c 65 20 73 70 65 63 69  lite3_file speci
bcf90 66 69 63 20 66 6f 72 20 74 68 65 20 4f 53 2f 32  fic for the OS/2
bcfa0 0a 2a 2a 20 70 72 6f 74 61 62 69 6c 69 74 79 20  .** protability 
bcfb0 6c 61 79 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65  layer..*/.typede
bcfc0 66 20 73 74 72 75 63 74 20 6f 73 32 46 69 6c 65  f struct os2File
bcfd0 20 6f 73 32 46 69 6c 65 3b 0a 73 74 72 75 63 74   os2File;.struct
bcfe0 20 6f 73 32 46 69 6c 65 20 7b 0a 20 20 63 6f 6e   os2File {.  con
bcff0 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  st sqlite3_io_me
bd000 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b 20  thods *pMethod; 
bd010 20 2f 2a 20 41 6c 77 61 79 73 20 74 68 65 20 66   /* Always the f
bd020 69 72 73 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20  irst entry */.  
bd030 48 46 49 4c 45 20 68 3b 20 20 20 20 20 20 20 20  HFILE h;        
bd040 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e            /* Han
bd050 64 6c 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e  dle for accessin
bd060 67 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20  g the file */.  
bd070 63 68 61 72 2a 20 70 61 74 68 54 6f 44 65 6c 3b  char* pathToDel;
bd080 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
bd090 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c  e of file to del
bd0a0 65 74 65 20 6f 6e 20 63 6c 6f 73 65 2c 20 4e 55  ete on close, NU
bd0b0 4c 4c 20 69 66 20 6e 6f 74 20 2a 2f 0a 20 20 75  LL if not */.  u
bd0c0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6c 6f 63  nsigned char loc
bd0d0 6b 74 79 70 65 3b 20 20 20 2f 2a 20 54 79 70 65  ktype;   /* Type
bd0e0 20 6f 66 20 6c 6f 63 6b 20 63 75 72 72 65 6e 74   of lock current
bd0f0 6c 79 20 68 65 6c 64 20 6f 6e 20 74 68 69 73 20  ly held on this 
bd100 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66  file */.};..#def
bd110 69 6e 65 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54  ine LOCK_TIMEOUT
bd120 20 31 30 4c 20 2f 2a 20 74 68 65 20 64 65 66 61   10L /* the defa
bd130 75 6c 74 20 6c 6f 63 6b 69 6e 67 20 74 69 6d 65  ult locking time
bd140 6f 75 74 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  out */../*******
bd150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd190 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65  ******.** The ne
bd1a0 78 74 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74  xt group of rout
bd1b0 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 74  ines implement t
bd1c0 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 73  he I/O methods s
bd1d0 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 74  pecified.** by t
bd1e0 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  he sqlite3_io_me
bd1f0 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a  thods object..**
bd200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f  ************/../
bd250 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c  *.** Close a fil
bd260 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
bd270 20 6f 73 32 43 6c 6f 73 65 28 20 73 71 6c 69 74   os2Close( sqlit
bd280 65 33 5f 66 69 6c 65 20 2a 69 64 20 29 7b 0a 20  e3_file *id ){. 
bd290 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f   APIRET rc = NO_
bd2a0 45 52 52 4f 52 3b 0a 20 20 6f 73 32 46 69 6c 65  ERROR;.  os2File
bd2b0 20 2a 70 46 69 6c 65 3b 0a 20 20 69 66 28 20 69   *pFile;.  if( i
bd2c0 64 20 26 26 20 28 70 46 69 6c 65 20 3d 20 28 6f  d && (pFile = (o
bd2d0 73 32 46 69 6c 65 2a 29 69 64 29 20 21 3d 20 30  s2File*)id) != 0
bd2e0 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 32   ){.    OSTRACE2
bd2f0 28 20 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20  ( "CLOSE %d\n", 
bd300 70 46 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 20 20  pFile->h );.    
bd310 72 63 20 3d 20 44 6f 73 43 6c 6f 73 65 28 20 70  rc = DosClose( p
bd320 46 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 20 20 70  File->h );.    p
bd330 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  File->locktype =
bd340 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 69 66   NO_LOCK;.    if
bd350 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44  ( pFile->pathToD
bd360 65 6c 20 21 3d 20 4e 55 4c 4c 20 29 7b 0a 20 20  el != NULL ){.  
bd370 20 20 20 20 72 63 20 3d 20 44 6f 73 46 6f 72 63      rc = DosForc
bd380 65 44 65 6c 65 74 65 28 20 28 50 53 5a 29 70 46  eDelete( (PSZ)pF
bd390 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29  ile->pathToDel )
bd3a0 3b 0a 20 20 20 20 20 20 66 72 65 65 28 20 70 46  ;.      free( pF
bd3b0 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29  ile->pathToDel )
bd3c0 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 70  ;.      pFile->p
bd3d0 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b  athToDel = NULL;
bd3e0 0a 20 20 20 20 7d 0a 20 20 20 20 69 64 20 3d 20  .    }.    id = 
bd3f0 30 3b 0a 20 20 20 20 4f 70 65 6e 43 6f 75 6e 74  0;.    OpenCount
bd400 65 72 28 20 2d 31 20 29 3b 0a 20 20 7d 0a 0a 20  er( -1 );.  }.. 
bd410 20 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f   return rc == NO
bd420 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f  _ERROR ? SQLITE_
bd430 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52  OK : SQLITE_IOER
bd440 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64  R;.}../*.** Read
bd450 20 64 61 74 61 20 66 72 6f 6d 20 61 20 66 69 6c   data from a fil
bd460 65 20 69 6e 74 6f 20 61 20 62 75 66 66 65 72 2e  e into a buffer.
bd470 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    Return SQLITE_
bd480 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74  OK if all.** byt
bd490 65 73 20 77 65 72 65 20 72 65 61 64 20 73 75 63  es were read suc
bd4a0 63 65 73 73 66 75 6c 6c 79 20 61 6e 64 20 53 51  cessfully and SQ
bd4b0 4c 49 54 45 5f 49 4f 45 52 52 20 69 66 20 61 6e  LITE_IOERR if an
bd4c0 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77  ything goes.** w
bd4d0 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rong..*/.static 
bd4e0 69 6e 74 20 6f 73 32 52 65 61 64 28 0a 20 20 73  int os2Read(.  s
bd4f0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
bd500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
bd510 2a 20 46 69 6c 65 20 74 6f 20 72 65 61 64 20 66  * File to read f
bd520 72 6f 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  rom */.  void *p
bd530 42 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20  Buf,            
bd540 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
bd550 65 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 74  e content into t
bd560 68 69 73 20 62 75 66 66 65 72 20 2a 2f 0a 20 20  his buffer */.  
bd570 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20  int amt,        
bd580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bd590 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
bd5a0 65 73 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20  es to read */.  
bd5b0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66  sqlite3_int64 of
bd5c0 66 73 65 74 20 20 20 20 20 20 20 20 20 20 20 20  fset            
bd5d0 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67  /* Begin reading
bd5e0 20 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20   at this offset 
bd5f0 2a 2f 0a 29 7b 0a 20 20 55 4c 4f 4e 47 20 66 69  */.){.  ULONG fi
bd600 6c 65 4c 6f 63 61 74 69 6f 6e 20 3d 20 30 4c 3b  leLocation = 0L;
bd610 0a 20 20 55 4c 4f 4e 47 20 67 6f 74 3b 0a 20 20  .  ULONG got;.  
bd620 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  os2File *pFile =
bd630 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20   (os2File*)id;. 
bd640 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29   assert( id!=0 )
bd650 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ;.  SimulateIOEr
bd660 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49  ror( return SQLI
bd670 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 20 29 3b  TE_IOERR_READ );
bd680 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 52 45  .  OSTRACE3( "RE
bd690 41 44 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22  AD %d lock=%d\n"
bd6a0 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c  , pFile->h, pFil
bd6b0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20  e->locktype );. 
bd6c0 20 69 66 28 20 44 6f 73 53 65 74 46 69 6c 65 50   if( DosSetFileP
bd6d0 74 72 28 70 46 69 6c 65 2d 3e 68 2c 20 6f 66 66  tr(pFile->h, off
bd6e0 73 65 74 2c 20 46 49 4c 45 5f 42 45 47 49 4e 2c  set, FILE_BEGIN,
bd6f0 20 26 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 29 20   &fileLocation) 
bd700 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20  != NO_ERROR ){. 
bd710 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
bd720 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 20 20 69 66  _IOERR;.  }.  if
bd730 28 20 44 6f 73 52 65 61 64 28 20 70 46 69 6c 65  ( DosRead( pFile
bd740 2d 3e 68 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20  ->h, pBuf, amt, 
bd750 26 67 6f 74 20 29 20 21 3d 20 4e 4f 5f 45 52 52  &got ) != NO_ERR
bd760 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OR ){.    return
bd770 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45   SQLITE_IOERR_RE
bd780 41 44 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 6f  AD;.  }.  if( go
bd790 74 20 3d 3d 20 28 55 4c 4f 4e 47 29 61 6d 74 20  t == (ULONG)amt 
bd7a0 29 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ).    return SQL
bd7b0 49 54 45 5f 4f 4b 3b 0a 20 20 65 6c 73 65 20 7b  ITE_OK;.  else {
bd7c0 0a 20 20 20 20 2f 2a 20 55 6e 72 65 61 64 20 70  .    /* Unread p
bd7d0 6f 72 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 69  ortions of the i
bd7e0 6e 70 75 74 20 62 75 66 66 65 72 20 6d 75 73 74  nput buffer must
bd7f0 20 62 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20   be zero-filled 
bd800 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28  */.    memset(&(
bd810 28 63 68 61 72 2a 29 70 42 75 66 29 5b 67 6f 74  (char*)pBuf)[got
bd820 5d 2c 20 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a  ], 0, amt-got);.
bd830 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
bd840 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45  E_IOERR_SHORT_RE
bd850 41 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  AD;.  }.}../*.**
bd860 20 57 72 69 74 65 20 64 61 74 61 20 66 72 6f 6d   Write data from
bd870 20 61 20 62 75 66 66 65 72 20 69 6e 74 6f 20 61   a buffer into a
bd880 20 66 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 53   file.  Return S
bd890 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63  QLITE_OK on succ
bd8a0 65 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f  ess.** or some o
bd8b0 74 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65 20  ther error code 
bd8c0 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73  on failure..*/.s
bd8d0 74 61 74 69 63 20 69 6e 74 20 6f 73 32 57 72 69  tatic int os2Wri
bd8e0 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69  te(.  sqlite3_fi
bd8f0 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20  le *id,         
bd900 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f        /* File to
bd910 20 77 72 69 74 65 20 69 6e 74 6f 20 2a 2f 0a 20   write into */. 
bd920 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75   const void *pBu
bd930 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f,              
bd940 20 2f 2a 20 54 68 65 20 62 79 74 65 73 20 74 6f   /* The bytes to
bd950 20 62 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20   be written */. 
bd960 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20   int amt,       
bd970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bd980 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
bd990 74 65 73 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a  tes to write */.
bd9a0 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
bd9b0 6f 66 66 73 65 74 20 20 20 20 20 20 20 20 20 20  offset          
bd9c0 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f    /* Offset into
bd9d0 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65 67   the file to beg
bd9e0 69 6e 20 77 72 69 74 69 6e 67 20 61 74 20 2a 2f  in writing at */
bd9f0 0a 29 7b 0a 20 20 55 4c 4f 4e 47 20 66 69 6c 65  .){.  ULONG file
bda00 4c 6f 63 61 74 69 6f 6e 20 3d 20 30 4c 3b 0a 20  Location = 0L;. 
bda10 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f   APIRET rc = NO_
bda20 45 52 52 4f 52 3b 0a 20 20 55 4c 4f 4e 47 20 77  ERROR;.  ULONG w
bda30 72 6f 74 65 3b 0a 20 20 6f 73 32 46 69 6c 65 20  rote;.  os2File 
bda40 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c  *pFile = (os2Fil
bda50 65 2a 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28  e*)id;.  assert(
bda60 20 69 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75   id!=0 );.  Simu
bda70 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74  lateIOError( ret
bda80 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
bda90 5f 57 52 49 54 45 20 29 3b 0a 20 20 53 69 6d 75  _WRITE );.  Simu
bdaa0 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f  lateDiskfullErro
bdab0 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  r( return SQLITE
bdac0 5f 46 55 4c 4c 20 29 3b 0a 20 20 4f 53 54 52 41  _FULL );.  OSTRA
bdad0 43 45 33 28 20 22 57 52 49 54 45 20 25 64 20 6c  CE3( "WRITE %d l
bdae0 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65  ock=%d\n", pFile
bdaf0 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ->h, pFile->lock
bdb00 74 79 70 65 20 29 3b 0a 20 20 69 66 28 20 44 6f  type );.  if( Do
bdb10 73 53 65 74 46 69 6c 65 50 74 72 28 70 46 69 6c  sSetFilePtr(pFil
bdb20 65 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20 46 49  e->h, offset, FI
bdb30 4c 45 5f 42 45 47 49 4e 2c 20 26 66 69 6c 65 4c  LE_BEGIN, &fileL
bdb40 6f 63 61 74 69 6f 6e 29 20 21 3d 20 4e 4f 5f 45  ocation) != NO_E
bdb50 52 52 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75  RROR ){.    retu
bdb60 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b  rn SQLITE_IOERR;
bdb70 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 61  .  }.  assert( a
bdb80 6d 74 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 28  mt>0 );.  while(
bdb90 20 61 6d 74 20 3e 20 30 20 26 26 0a 20 20 20 20   amt > 0 &&.    
bdba0 20 20 20 20 20 28 20 72 63 20 3d 20 44 6f 73 57       ( rc = DosW
bdbb0 72 69 74 65 28 20 70 46 69 6c 65 2d 3e 68 2c 20  rite( pFile->h, 
bdbc0 28 50 56 4f 49 44 29 70 42 75 66 2c 20 61 6d 74  (PVOID)pBuf, amt
bdbd0 2c 20 26 77 72 6f 74 65 20 29 20 29 20 3d 3d 20  , &wrote ) ) == 
bdbe0 4e 4f 5f 45 52 52 4f 52 20 26 26 0a 20 20 20 20  NO_ERROR &&.    
bdbf0 20 20 20 20 20 77 72 6f 74 65 20 3e 20 30 0a 20       wrote > 0. 
bdc00 20 29 7b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 77   ){.    amt -= w
bdc10 72 6f 74 65 3b 0a 20 20 20 20 70 42 75 66 20 3d  rote;.    pBuf =
bdc20 20 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b   &((char*)pBuf)[
bdc30 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a 0a 20 20 72  wrote];.  }..  r
bdc40 65 74 75 72 6e 20 28 20 72 63 20 21 3d 20 4e 4f  eturn ( rc != NO
bdc50 5f 45 52 52 4f 52 20 7c 7c 20 61 6d 74 20 3e 20  _ERROR || amt > 
bdc60 28 69 6e 74 29 77 72 6f 74 65 20 29 20 3f 20 53  (int)wrote ) ? S
bdc70 51 4c 49 54 45 5f 46 55 4c 4c 20 3a 20 53 51 4c  QLITE_FULL : SQL
bdc80 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
bdc90 20 54 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65   Truncate an ope
bdca0 6e 20 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63  n file to a spec
bdcb0 69 66 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74  ified size.*/.st
bdcc0 61 74 69 63 20 69 6e 74 20 6f 73 32 54 72 75 6e  atic int os2Trun
bdcd0 63 61 74 65 28 20 73 71 6c 69 74 65 33 5f 66 69  cate( sqlite3_fi
bdce0 6c 65 20 2a 69 64 2c 20 69 36 34 20 6e 42 79 74  le *id, i64 nByt
bdcf0 65 20 29 7b 0a 20 20 41 50 49 52 45 54 20 72 63  e ){.  APIRET rc
bdd00 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 6f   = NO_ERROR;.  o
bdd10 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  s2File *pFile = 
bdd20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20  (os2File*)id;.  
bdd30 4f 53 54 52 41 43 45 33 28 20 22 54 52 55 4e 43  OSTRACE3( "TRUNC
bdd40 41 54 45 20 25 64 20 25 6c 6c 64 5c 6e 22 2c 20  ATE %d %lld\n", 
bdd50 70 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 20  pFile->h, nByte 
bdd60 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  );.  SimulateIOE
bdd70 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c  rror( return SQL
bdd80 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41  ITE_IOERR_TRUNCA
bdd90 54 45 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73  TE );.  rc = Dos
bdda0 53 65 74 46 69 6c 65 53 69 7a 65 28 20 70 46 69  SetFileSize( pFi
bddb0 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 20 29 3b 0a  le->h, nByte );.
bddc0 20 20 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e    return rc == N
bddd0 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45  O_ERROR ? SQLITE
bdde0 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45  _OK : SQLITE_IOE
bddf0 52 52 5f 54 52 55 4e 43 41 54 45 3b 0a 7d 0a 0a  RR_TRUNCATE;.}..
bde00 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
bde10 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74  ST./*.** Count t
bde20 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 75 6c  he number of ful
bde30 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d 61  lsyncs and norma
bde40 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 73 20 69  l syncs.  This i
bde50 73 20 75 73 65 64 20 74 6f 20 74 65 73 74 0a 2a  s used to test.*
bde60 2a 20 74 68 61 74 20 73 79 6e 63 73 20 61 6e 64  * that syncs and
bde70 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 20 6f   fullsyncs are o
bde80 63 63 75 72 69 6e 67 20 61 74 20 74 68 65 20 72  ccuring at the r
bde90 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a 53  ight times..*/.S
bdea0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
bdeb0 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74  lite3_sync_count
bdec0 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49   = 0;.SQLITE_API
bded0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 75 6c   int sqlite3_ful
bdee0 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b  lsync_count = 0;
bdef0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d  .#endif../*.** M
bdf00 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 77 72 69  ake sure all wri
bdf10 74 65 73 20 74 6f 20 61 20 70 61 72 74 69 63 75  tes to a particu
bdf20 6c 61 72 20 66 69 6c 65 20 61 72 65 20 63 6f 6d  lar file are com
bdf30 6d 69 74 74 65 64 20 74 6f 20 64 69 73 6b 2e 0a  mitted to disk..
bdf40 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73  */.static int os
bdf50 32 53 79 6e 63 28 20 73 71 6c 69 74 65 33 5f 66  2Sync( sqlite3_f
bdf60 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61  ile *id, int fla
bdf70 67 73 20 29 7b 0a 20 20 6f 73 32 46 69 6c 65 20  gs ){.  os2File 
bdf80 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c  *pFile = (os2Fil
bdf90 65 2a 29 69 64 3b 0a 20 20 4f 53 54 52 41 43 45  e*)id;.  OSTRACE
bdfa0 33 28 20 22 53 59 4e 43 20 25 64 20 6c 6f 63 6b  3( "SYNC %d lock
bdfb0 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  =%d\n", pFile->h
bdfc0 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  , pFile->locktyp
bdfd0 65 20 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  e );.#ifdef SQLI
bdfe0 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20 66 6c  TE_TEST.  if( fl
bdff0 61 67 73 20 26 20 53 51 4c 49 54 45 5f 53 59 4e  ags & SQLITE_SYN
be000 43 5f 46 55 4c 4c 29 7b 0a 20 20 20 20 73 71 6c  C_FULL){.    sql
be010 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f  ite3_fullsync_co
be020 75 6e 74 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c  unt++;.  }.  sql
be030 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b  ite3_sync_count+
be040 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 2f 2a 20 49  +;.#endif.  /* I
be050 66 20 77 65 20 63 6f 6d 70 69 6c 65 64 20 77 69  f we compiled wi
be060 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f  th the SQLITE_NO
be070 5f 53 59 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e  _SYNC flag, then
be080 20 73 79 6e 63 69 6e 67 20 69 73 20 61 0a 20 20   syncing is a.  
be090 2a 2a 20 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69  ** no-op.  */.#i
be0a0 66 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53  fdef SQLITE_NO_S
be0b0 59 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  YNC.  UNUSED_PAR
be0c0 41 4d 45 54 45 52 28 70 46 69 6c 65 29 3b 0a 20  AMETER(pFile);. 
be0d0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
be0e0 4b 3b 0a 23 65 6c 73 65 0a 20 20 72 65 74 75 72  K;.#else.  retur
be0f0 6e 20 44 6f 73 52 65 73 65 74 42 75 66 66 65 72  n DosResetBuffer
be100 28 20 70 46 69 6c 65 2d 3e 68 20 29 20 3d 3d 20  ( pFile->h ) == 
be110 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54  NO_ERROR ? SQLIT
be120 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f  E_OK : SQLITE_IO
be130 45 52 52 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f  ERR;.#endif.}../
be140 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74  *.** Determine t
be150 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20  he current size 
be160 6f 66 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74  of a file in byt
be170 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  es.*/.static int
be180 20 6f 73 32 46 69 6c 65 53 69 7a 65 28 20 73 71   os2FileSize( sq
be190 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
be1a0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70  sqlite3_int64 *p
be1b0 53 69 7a 65 20 29 7b 0a 20 20 41 50 49 52 45 54  Size ){.  APIRET
be1c0 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a   rc = NO_ERROR;.
be1d0 20 20 46 49 4c 45 53 54 41 54 55 53 33 20 66 73    FILESTATUS3 fs
be1e0 74 73 33 46 69 6c 65 49 6e 66 6f 3b 0a 20 20 6d  ts3FileInfo;.  m
be1f0 65 6d 73 65 74 28 26 66 73 74 73 33 46 69 6c 65  emset(&fsts3File
be200 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Info, 0, sizeof(
be210 66 73 74 73 33 46 69 6c 65 49 6e 66 6f 29 29 3b  fsts3FileInfo));
be220 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30  .  assert( id!=0
be230 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f   );.  SimulateIO
be240 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51  Error( return SQ
be250 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54  LITE_IOERR_FSTAT
be260 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 51 75   );.  rc = DosQu
be270 65 72 79 46 69 6c 65 49 6e 66 6f 28 20 28 28 6f  eryFileInfo( ((o
be280 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20  s2File*)id)->h, 
be290 46 49 4c 5f 53 54 41 4e 44 41 52 44 2c 20 26 66  FIL_STANDARD, &f
be2a0 73 74 73 33 46 69 6c 65 49 6e 66 6f 2c 20 73 69  sts3FileInfo, si
be2b0 7a 65 6f 66 28 46 49 4c 45 53 54 41 54 55 53 33  zeof(FILESTATUS3
be2c0 29 20 29 3b 0a 20 20 69 66 28 20 72 63 20 3d 3d  ) );.  if( rc ==
be2d0 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20   NO_ERROR ){.   
be2e0 20 2a 70 53 69 7a 65 20 3d 20 66 73 74 73 33 46   *pSize = fsts3F
be2f0 69 6c 65 49 6e 66 6f 2e 63 62 46 69 6c 65 3b 0a  ileInfo.cbFile;.
be300 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
be310 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  E_OK;.  }else{. 
be320 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
be330 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20 20  _IOERR_FSTAT;.  
be340 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69  }.}../*.** Acqui
be350 72 65 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b  re a reader lock
be360 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
be370 67 65 74 52 65 61 64 4c 6f 63 6b 28 20 6f 73 32  getReadLock( os2
be380 46 69 6c 65 20 2a 70 46 69 6c 65 20 29 7b 0a 20  File *pFile ){. 
be390 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41   FILELOCK  LockA
be3a0 72 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  rea,.           
be3b0 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 41   UnlockArea;.  A
be3c0 50 49 52 45 54 20 72 65 73 3b 0a 20 20 6d 65 6d  PIRET res;.  mem
be3d0 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30  set(&LockArea, 0
be3e0 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65  , sizeof(LockAre
be3f0 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55  a));.  memset(&U
be400 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69  nlockArea, 0, si
be410 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29  zeof(UnlockArea)
be420 29 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f  );.  LockArea.lO
be430 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46  ffset = SHARED_F
be440 49 52 53 54 3b 0a 20 20 4c 6f 63 6b 41 72 65 61  IRST;.  LockArea
be450 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41 52 45 44  .lRange = SHARED
be460 5f 53 49 5a 45 3b 0a 20 20 55 6e 6c 6f 63 6b 41  _SIZE;.  UnlockA
be470 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c  rea.lOffset = 0L
be480 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c  ;.  UnlockArea.l
be490 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 72 65  Range = 0L;.  re
be4a0 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f  s = DosSetFileLo
be4b0 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26  cks( pFile->h, &
be4c0 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63  UnlockArea, &Loc
be4d0 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45  kArea, LOCK_TIME
be4e0 4f 55 54 2c 20 31 4c 20 29 3b 0a 20 20 4f 53 54  OUT, 1L );.  OST
be4f0 52 41 43 45 33 28 20 22 47 45 54 52 45 41 44 4c  RACE3( "GETREADL
be500 4f 43 4b 20 25 64 20 72 65 73 3d 25 64 5c 6e 22  OCK %d res=%d\n"
be510 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20  , pFile->h, res 
be520 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b  );.  return res;
be530 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 61  .}../*.** Undo a
be540 20 72 65 61 64 6c 6f 63 6b 0a 2a 2f 0a 73 74 61   readlock.*/.sta
be550 74 69 63 20 69 6e 74 20 75 6e 6c 6f 63 6b 52 65  tic int unlockRe
be560 61 64 4c 6f 63 6b 28 20 6f 73 32 46 69 6c 65 20  adLock( os2File 
be570 2a 69 64 20 29 7b 0a 20 20 46 49 4c 45 4c 4f 43  *id ){.  FILELOC
be580 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20  K  LockArea,.   
be590 20 20 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41           UnlockA
be5a0 72 65 61 3b 0a 20 20 41 50 49 52 45 54 20 72 65  rea;.  APIRET re
be5b0 73 3b 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63  s;.  memset(&Loc
be5c0 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66  kArea, 0, sizeof
be5d0 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d  (LockArea));.  m
be5e0 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65  emset(&UnlockAre
be5f0 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c  a, 0, sizeof(Unl
be600 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 4c 6f 63  ockArea));.  Loc
be610 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20  kArea.lOffset = 
be620 30 4c 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c  0L;.  LockArea.l
be630 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 55 6e  Range = 0L;.  Un
be640 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74  lockArea.lOffset
be650 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b   = SHARED_FIRST;
be660 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52  .  UnlockArea.lR
be670 61 6e 67 65 20 3d 20 53 48 41 52 45 44 5f 53 49  ange = SHARED_SI
be680 5a 45 3b 0a 20 20 72 65 73 20 3d 20 44 6f 73 53  ZE;.  res = DosS
be690 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 69 64 2d  etFileLocks( id-
be6a0 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c  >h, &UnlockArea,
be6b0 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b   &LockArea, LOCK
be6c0 5f 54 49 4d 45 4f 55 54 2c 20 31 4c 20 29 3b 0a  _TIMEOUT, 1L );.
be6d0 20 20 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c    OSTRACE3( "UNL
be6e0 4f 43 4b 2d 52 45 41 44 4c 4f 43 4b 20 66 69 6c  OCK-READLOCK fil
be6f0 65 20 68 61 6e 64 6c 65 3d 25 64 20 72 65 73 3d  e handle=%d res=
be700 25 64 3f 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 72  %d?\n", id->h, r
be710 65 73 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  es );.  return r
be720 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63  es;.}../*.** Loc
be730 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20  k the file with 
be740 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69  the lock specifi
be750 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20  ed by parameter 
be760 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a  locktype - one.*
be770 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  * of the followi
be780 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31  ng:.**.**     (1
be790 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a  ) SHARED_LOCK.**
be7a0 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45       (2) RESERVE
be7b0 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33  D_LOCK.**     (3
be7c0 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a  ) PENDING_LOCK.*
be7d0 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53  *     (4) EXCLUS
be7e0 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53  IVE_LOCK.**.** S
be7f0 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65  ometimes when re
be800 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63  questing one loc
be810 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f  k state, additio
be820 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a  nal lock states.
be830 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20  ** are inserted 
be840 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65  in between.  The
be850 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66   locking might f
be860 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68  ail on one of th
be870 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73  e later.** trans
be880 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74  itions leaving t
be890 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69  he lock state di
be8a0 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61  fferent from wha
be8b0 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74  t it started but
be8c0 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20  .** still short 
be8d0 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68  of its goal.  Th
be8e0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72  e following char
be8f0 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f  t shows the allo
be900 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f  wed.** transitio
be910 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72  ns and the inser
be920 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65  ted intermediate
be930 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20   states:.**.**  
be940 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48    UNLOCKED -> SH
be950 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45  ARED.**    SHARE
be960 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a  D -> RESERVED.**
be970 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50      SHARED -> (P
be980 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55  ENDING) -> EXCLU
be990 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52  SIVE.**    RESER
be9a0 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29  VED -> (PENDING)
be9b0 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a   -> EXCLUSIVE.**
be9c0 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45      PENDING -> E
be9d0 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54  XCLUSIVE.**.** T
be9e0 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c  his routine will
be9f0 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61   only increase a
bea00 20 6c 6f 63 6b 2e 20 20 54 68 65 20 6f 73 32 55   lock.  The os2U
bea10 6e 6c 6f 63 6b 28 29 20 72 6f 75 74 69 6e 65 0a  nlock() routine.
bea20 2a 2a 20 65 72 61 73 65 73 20 61 6c 6c 20 6c 6f  ** erases all lo
bea30 63 6b 73 20 61 74 20 6f 6e 63 65 20 61 6e 64 20  cks at once and 
bea40 72 65 74 75 72 6e 73 20 75 73 20 69 6d 6d 65 64  returns us immed
bea50 69 61 74 65 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e  iately to lockin
bea60 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2a 20 49 74  g level 0..** It
bea70 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
bea80 20 74 6f 20 6c 6f 77 65 72 20 74 68 65 20 6c 6f   to lower the lo
bea90 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 65 20  cking level one 
beaa0 73 74 65 70 20 61 74 20 61 20 74 69 6d 65 2e 20  step at a time. 
beab0 20 59 6f 75 0a 2a 2a 20 6d 75 73 74 20 67 6f 20   You.** must go 
beac0 73 74 72 61 69 67 68 74 20 74 6f 20 6c 6f 63 6b  straight to lock
bead0 69 6e 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a  ing level 0..*/.
beae0 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4c 6f  static int os2Lo
beaf0 63 6b 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  ck( sqlite3_file
beb00 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79   *id, int lockty
beb10 70 65 20 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  pe ){.  int rc =
beb20 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20   SQLITE_OK;     
beb30 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
beb40 20 66 72 6f 6d 20 73 75 62 72 6f 75 74 69 6e 65   from subroutine
beb50 73 20 2a 2f 0a 20 20 41 50 49 52 45 54 20 72 65  s */.  APIRET re
beb60 73 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 20 20 20  s = NO_ERROR;   
beb70 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 6e   /* Result of an
beb80 20 4f 53 2f 32 20 6c 6f 63 6b 20 63 61 6c 6c 20   OS/2 lock call 
beb90 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 4c 6f 63 6b  */.  int newLock
beba0 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 53  type;       /* S
bebb0 65 74 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  et pFile->lockty
bebc0 70 65 20 74 6f 20 74 68 69 73 20 76 61 6c 75 65  pe to this value
bebd0 20 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20   before exiting 
bebe0 2a 2f 0a 20 20 69 6e 74 20 67 6f 74 50 65 6e 64  */.  int gotPend
bebf0 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20 54  ingLock = 0;/* T
bec00 72 75 65 20 69 66 20 77 65 20 61 63 71 75 69 72  rue if we acquir
bec10 65 64 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63  ed a PENDING loc
bec20 6b 20 74 68 69 73 20 74 69 6d 65 20 2a 2f 0a 20  k this time */. 
bec30 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41   FILELOCK  LockA
bec40 72 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  rea,.           
bec50 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 6f   UnlockArea;.  o
bec60 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  s2File *pFile = 
bec70 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20  (os2File*)id;.  
bec80 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 61  memset(&LockArea
bec90 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b  , 0, sizeof(Lock
beca0 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74  Area));.  memset
becb0 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c  (&UnlockArea, 0,
becc0 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72   sizeof(UnlockAr
becd0 65 61 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ea));.  assert( 
bece0 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 4f 53  pFile!=0 );.  OS
becf0 54 52 41 43 45 34 28 20 22 4c 4f 43 4b 20 25 64  TRACE4( "LOCK %d
bed00 20 25 64 20 77 61 73 20 25 64 5c 6e 22 2c 20 70   %d was %d\n", p
bed10 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70  File->h, locktyp
bed20 65 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  e, pFile->lockty
bed30 70 65 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  pe );..  /* If t
bed40 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20  here is already 
bed50 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74  a lock of this t
bed60 79 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74  ype or more rest
bed70 72 69 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20  rictive on the. 
bed80 20 2a 2a 20 6f 73 32 46 69 6c 65 2c 20 64 6f 20   ** os2File, do 
bed90 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75  nothing. Don't u
beda0 73 65 20 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a  se the end_lock:
bedb0 20 65 78 69 74 20 70 61 74 68 2c 20 61 73 0a 20   exit path, as. 
bedc0 20 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   ** sqlite3_mute
bedd0 78 5f 65 6e 74 65 72 28 29 20 68 61 73 6e 27 74  x_enter() hasn't
bede0 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74   been called yet
bedf0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69  ..  */.  if( pFi
bee00 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f  le->locktype>=lo
bee10 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 4f 53  cktype ){.    OS
bee20 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64  TRACE3( "LOCK %d
bee30 20 25 64 20 6f 6b 20 28 61 6c 72 65 61 64 79 20   %d ok (already 
bee40 68 65 6c 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d  held)\n", pFile-
bee50 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 20 29 3b 0a  >h, locktype );.
bee60 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
bee70 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  E_OK;.  }..  /* 
bee80 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f  Make sure the lo
bee90 63 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69  cking sequence i
beea0 73 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20  s correct.  */. 
beeb0 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e   assert( pFile->
beec0 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43  locktype!=NO_LOC
beed0 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53  K || locktype==S
beee0 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20  HARED_LOCK );.  
beef0 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65  assert( locktype
bef00 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29  !=PENDING_LOCK )
bef10 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b  ;.  assert( lock
bef20 74 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c  type!=RESERVED_L
bef30 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f  OCK || pFile->lo
bef40 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c  cktype==SHARED_L
bef50 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63  OCK );..  /* Loc
bef60 6b 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f  k the PENDING_LO
bef70 43 4b 20 62 79 74 65 20 69 66 20 77 65 20 6e 65  CK byte if we ne
bef80 65 64 20 74 6f 20 61 63 71 75 69 72 65 20 61 20  ed to acquire a 
bef90 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a  PENDING lock or.
befa0 20 20 2a 2a 20 61 20 53 48 41 52 45 44 20 6c 6f    ** a SHARED lo
befb0 63 6b 2e 20 20 49 66 20 77 65 20 61 72 65 20 61  ck.  If we are a
befc0 63 71 75 69 72 69 6e 67 20 61 20 53 48 41 52 45  cquiring a SHARE
befd0 44 20 6c 6f 63 6b 2c 20 74 68 65 20 61 63 71 75  D lock, the acqu
befe0 69 73 69 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20  isition of.  ** 
beff0 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b  the PENDING_LOCK
bf000 20 62 79 74 65 20 69 73 20 74 65 6d 70 6f 72 61   byte is tempora
bf010 72 79 2e 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f  ry..  */.  newLo
bf020 63 6b 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e  cktype = pFile->
bf030 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20  locktype;.  if( 
bf040 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pFile->locktype=
bf050 3d 4e 4f 5f 4c 4f 43 4b 0a 20 20 20 20 20 20 7c  =NO_LOCK.      |
bf060 7c 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43  | (locktype==EXC
bf070 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70  LUSIVE_LOCK && p
bf080 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d  File->locktype==
bf090 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 0a 20  RESERVED_LOCK). 
bf0a0 20 29 7b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61   ){.    LockArea
bf0b0 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 49  .lOffset = PENDI
bf0c0 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 4c 6f 63  NG_BYTE;.    Loc
bf0d0 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31  kArea.lRange = 1
bf0e0 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65  L;.    UnlockAre
bf0f0 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a  a.lOffset = 0L;.
bf100 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c      UnlockArea.l
bf110 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 0a 20 20 20  Range = 0L;..   
bf120 20 2f 2a 20 77 61 69 74 20 6c 6f 6e 67 65 72 20   /* wait longer 
bf130 74 68 61 6e 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55  than LOCK_TIMEOU
bf140 54 20 68 65 72 65 20 6e 6f 74 20 74 6f 20 68 61  T here not to ha
bf150 76 65 20 74 6f 20 74 72 79 20 6d 75 6c 74 69 70  ve to try multip
bf160 6c 65 20 74 69 6d 65 73 20 2a 2f 0a 20 20 20 20  le times */.    
bf170 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65  res = DosSetFile
bf180 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c  Locks( pFile->h,
bf190 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c   &UnlockArea, &L
bf1a0 6f 63 6b 41 72 65 61 2c 20 31 30 30 4c 2c 20 30  ockArea, 100L, 0
bf1b0 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73  L );.    if( res
bf1c0 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a   == NO_ERROR ){.
bf1d0 20 20 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67        gotPending
bf1e0 4c 6f 63 6b 20 3d 20 31 3b 0a 20 20 20 20 20 20  Lock = 1;.      
bf1f0 4f 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20  OSTRACE3( "LOCK 
bf200 25 64 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20  %d pending lock 
bf210 62 6f 6f 6c 65 61 6e 20 73 65 74 2e 20 20 72 65  boolean set.  re
bf220 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  s=%d\n", pFile->
bf230 68 2c 20 72 65 73 20 29 3b 0a 20 20 20 20 7d 0a  h, res );.    }.
bf240 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72    }..  /* Acquir
bf250 65 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 0a  e a shared lock.
bf260 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74    */.  if( lockt
bf270 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
bf280 20 26 26 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52   && res == NO_ER
bf290 52 4f 52 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ROR ){.    asser
bf2a0 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  t( pFile->lockty
bf2b0 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20  pe==NO_LOCK );. 
bf2c0 20 20 20 72 65 73 20 3d 20 67 65 74 52 65 61 64     res = getRead
bf2d0 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20  Lock(pFile);.   
bf2e0 20 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45   if( res == NO_E
bf2f0 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 6e 65  RROR ){.      ne
bf300 77 4c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52  wLocktype = SHAR
bf310 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20  ED_LOCK;.    }. 
bf320 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c 4f     OSTRACE3( "LO
bf330 43 4b 20 25 64 20 61 63 71 75 69 72 65 20 73 68  CK %d acquire sh
bf340 61 72 65 64 20 6c 6f 63 6b 2e 20 72 65 73 3d 25  ared lock. res=%
bf350 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
bf360 72 65 73 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  res );.  }..  /*
bf370 20 41 63 71 75 69 72 65 20 61 20 52 45 53 45 52   Acquire a RESER
bf380 56 45 44 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20  VED lock.  */.  
bf390 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 45  if( locktype==RE
bf3a0 53 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 72  SERVED_LOCK && r
bf3b0 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29  es == NO_ERROR )
bf3c0 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46  {.    assert( pF
bf3d0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53  ile->locktype==S
bf3e0 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20  HARED_LOCK );.  
bf3f0 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73    LockArea.lOffs
bf400 65 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59  et = RESERVED_BY
bf410 54 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61  TE;.    LockArea
bf420 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20  .lRange = 1L;.  
bf430 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66    UnlockArea.lOf
bf440 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55  fset = 0L;.    U
bf450 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65  nlockArea.lRange
bf460 20 3d 20 30 4c 3b 0a 20 20 20 20 72 65 73 20 3d   = 0L;.    res =
bf470 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73   DosSetFileLocks
bf480 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c  ( pFile->h, &Unl
bf490 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72  ockArea, &LockAr
bf4a0 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54  ea, LOCK_TIMEOUT
bf4b0 2c 20 30 4c 20 29 3b 0a 20 20 20 20 69 66 28 20  , 0L );.    if( 
bf4c0 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20  res == NO_ERROR 
bf4d0 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b  ){.      newLock
bf4e0 74 79 70 65 20 3d 20 52 45 53 45 52 56 45 44 5f  type = RESERVED_
bf4f0 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  LOCK;.    }.    
bf500 4f 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20  OSTRACE3( "LOCK 
bf510 25 64 20 61 63 71 75 69 72 65 20 72 65 73 65 72  %d acquire reser
bf520 76 65 64 20 6c 6f 63 6b 2e 20 72 65 73 3d 25 64  ved lock. res=%d
bf530 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72  \n", pFile->h, r
bf540 65 73 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  es );.  }..  /* 
bf550 41 63 71 75 69 72 65 20 61 20 50 45 4e 44 49 4e  Acquire a PENDIN
bf560 47 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66  G lock.  */.  if
bf570 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c  ( locktype==EXCL
bf580 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65  USIVE_LOCK && re
bf590 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b  s == NO_ERROR ){
bf5a0 0a 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65  .    newLocktype
bf5b0 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b   = PENDING_LOCK;
bf5c0 0a 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c  .    gotPendingL
bf5d0 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20 4f 53 54  ock = 0;.    OST
bf5e0 52 41 43 45 32 28 20 22 4c 4f 43 4b 20 25 64 20  RACE2( "LOCK %d 
bf5f0 61 63 71 75 69 72 65 20 70 65 6e 64 69 6e 67 20  acquire pending 
bf600 6c 6f 63 6b 2e 20 70 65 6e 64 69 6e 67 20 6c 6f  lock. pending lo
bf610 63 6b 20 62 6f 6f 6c 65 61 6e 20 75 6e 73 65 74  ck boolean unset
bf620 2e 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 20 29  .\n", pFile->h )
bf630 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75  ;.  }..  /* Acqu
bf640 69 72 65 20 61 6e 20 45 58 43 4c 55 53 49 56 45  ire an EXCLUSIVE
bf650 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28   lock.  */.  if(
bf660 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55   locktype==EXCLU
bf670 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73  SIVE_LOCK && res
bf680 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a   == NO_ERROR ){.
bf690 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c      assert( pFil
bf6a0 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53 48 41  e->locktype>=SHA
bf6b0 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20  RED_LOCK );.    
bf6c0 72 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64  res = unlockRead
bf6d0 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20  Lock(pFile);.   
bf6e0 20 4f 53 54 52 41 43 45 32 28 20 22 75 6e 72 65   OSTRACE2( "unre
bf6f0 61 64 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c 20  adlock = %d\n", 
bf700 72 65 73 20 29 3b 0a 20 20 20 20 4c 6f 63 6b 41  res );.    LockA
bf710 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53 48  rea.lOffset = SH
bf720 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20  ARED_FIRST;.    
bf730 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  LockArea.lRange 
bf740 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20  = SHARED_SIZE;. 
bf750 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f     UnlockArea.lO
bf760 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20  ffset = 0L;.    
bf770 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67  UnlockArea.lRang
bf780 65 20 3d 20 30 4c 3b 0a 20 20 20 20 72 65 73 20  e = 0L;.    res 
bf790 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b  = DosSetFileLock
bf7a0 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e  s( pFile->h, &Un
bf7b0 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41  lockArea, &LockA
bf7c0 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55  rea, LOCK_TIMEOU
bf7d0 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 69 66 28  T, 0L );.    if(
bf7e0 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52   res == NO_ERROR
bf7f0 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63   ){.      newLoc
bf800 6b 74 79 70 65 20 3d 20 45 58 43 4c 55 53 49 56  ktype = EXCLUSIV
bf810 45 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73  E_LOCK;.    }els
bf820 65 7b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45  e{.      OSTRACE
bf830 32 28 20 22 4f 53 2f 32 20 65 72 72 6f 72 2d 63  2( "OS/2 error-c
bf840 6f 64 65 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73  ode = %d\n", res
bf850 20 29 3b 0a 20 20 20 20 20 20 67 65 74 52 65 61   );.      getRea
bf860 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20  dLock(pFile);.  
bf870 20 20 7d 0a 20 20 20 20 4f 53 54 52 41 43 45 33    }.    OSTRACE3
bf880 28 20 22 4c 4f 43 4b 20 25 64 20 61 63 71 75 69  ( "LOCK %d acqui
bf890 72 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63  re exclusive loc
bf8a0 6b 2e 20 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70  k.  res=%d\n", p
bf8b0 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a  File->h, res );.
bf8c0 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20    }..  /* If we 
bf8d0 61 72 65 20 68 6f 6c 64 69 6e 67 20 61 20 50 45  are holding a PE
bf8e0 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68 61 74 20  NDING lock that 
bf8f0 6f 75 67 68 74 20 74 6f 20 62 65 20 72 65 6c 65  ought to be rele
bf900 61 73 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20  ased, then.  ** 
bf910 72 65 6c 65 61 73 65 20 69 74 20 6e 6f 77 2e 0a  release it now..
bf920 20 20 2a 2f 0a 20 20 69 66 28 20 67 6f 74 50 65    */.  if( gotPe
bf930 6e 64 69 6e 67 4c 6f 63 6b 20 26 26 20 6c 6f 63  ndingLock && loc
bf940 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
bf950 43 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 3b  CK ){.    int r;
bf960 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f  .    LockArea.lO
bf970 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20  ffset = 0L;.    
bf980 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  LockArea.lRange 
bf990 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b  = 0L;.    Unlock
bf9a0 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 50  Area.lOffset = P
bf9b0 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20  ENDING_BYTE;.   
bf9c0 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e   UnlockArea.lRan
bf9d0 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 72 20 3d  ge = 1L;.    r =
bf9e0 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73   DosSetFileLocks
bf9f0 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c  ( pFile->h, &Unl
bfa00 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72  ockArea, &LockAr
bfa10 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54  ea, LOCK_TIMEOUT
bfa20 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52  , 0L );.    OSTR
bfa30 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 75  ACE3( "LOCK %d u
bfa40 6e 6c 6f 63 6b 69 6e 67 20 70 65 6e 64 69 6e 67  nlocking pending
bfa50 2f 69 73 20 73 68 61 72 65 64 2e 20 72 3d 25 64  /is shared. r=%d
bfa60 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72  \n", pFile->h, r
bfa70 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70   );.  }..  /* Up
bfa80 64 61 74 65 20 74 68 65 20 73 74 61 74 65 20 6f  date the state o
bfa90 66 20 74 68 65 20 6c 6f 63 6b 20 68 61 73 20 68  f the lock has h
bfaa0 65 6c 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20  eld in the file 
bfab0 64 65 73 63 72 69 70 74 6f 72 20 74 68 65 6e 0a  descriptor then.
bfac0 20 20 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20    ** return the 
bfad0 61 70 70 72 6f 70 72 69 61 74 65 20 72 65 73 75  appropriate resu
bfae0 6c 74 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20  lt code..  */.  
bfaf0 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52  if( res == NO_ER
bfb00 52 4f 52 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ROR ){.    rc = 
bfb10 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c  SQLITE_OK;.  }el
bfb20 73 65 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 34  se{.    OSTRACE4
bfb30 28 20 22 4c 4f 43 4b 20 46 41 49 4c 45 44 20 25  ( "LOCK FAILED %
bfb40 64 20 74 72 79 69 6e 67 20 66 6f 72 20 25 64 20  d trying for %d 
bfb50 62 75 74 20 67 6f 74 20 25 64 5c 6e 22 2c 20 70  but got %d\n", p
bfb60 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20  File->h,.       
bfb70 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 2c         locktype,
bfb80 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 29 3b 0a   newLocktype );.
bfb90 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
bfba0 42 55 53 59 3b 0a 20 20 7d 0a 20 20 70 46 69 6c  BUSY;.  }.  pFil
bfbb0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6e 65  e->locktype = ne
bfbc0 77 4c 6f 63 6b 74 79 70 65 3b 0a 20 20 4f 53 54  wLocktype;.  OST
bfbd0 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20  RACE3( "LOCK %d 
bfbe0 6e 6f 77 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65  now %d\n", pFile
bfbf0 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ->h, pFile->lock
bfc00 74 79 70 65 20 29 3b 0a 20 20 72 65 74 75 72 6e  type );.  return
bfc10 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   rc;.}../*.** Th
bfc20 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b  is routine check
bfc30 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20  s if there is a 
bfc40 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65  RESERVED lock he
bfc50 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66  ld on the specif
bfc60 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74  ied.** file by t
bfc70 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72  his or any other
bfc80 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63   process. If suc
bfc90 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64  h a lock is held
bfca0 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d  , return.** non-
bfcb0 7a 65 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 20  zero, otherwise 
bfcc0 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  zero..*/.static 
bfcd0 69 6e 74 20 6f 73 32 43 68 65 63 6b 52 65 73 65  int os2CheckRese
bfce0 72 76 65 64 4c 6f 63 6b 28 20 73 71 6c 69 74 65  rvedLock( sqlite
bfcf0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
bfd00 2a 70 4f 75 74 20 29 7b 0a 20 20 69 6e 74 20 72  *pOut ){.  int r
bfd10 20 3d 20 30 3b 0a 20 20 6f 73 32 46 69 6c 65 20   = 0;.  os2File 
bfd20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c  *pFile = (os2Fil
bfd30 65 2a 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28  e*)id;.  assert(
bfd40 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 69   pFile!=0 );.  i
bfd50 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  f( pFile->lockty
bfd60 70 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43  pe>=RESERVED_LOC
bfd70 4b 20 29 7b 0a 20 20 20 20 72 20 3d 20 31 3b 0a  K ){.    r = 1;.
bfd80 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 54      OSTRACE3( "T
bfd90 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25  EST WR-LOCK %d %
bfda0 64 20 28 6c 6f 63 61 6c 29 5c 6e 22 2c 20 70 46  d (local)\n", pF
bfdb0 69 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d  ile->h, r );.  }
bfdc0 65 6c 73 65 7b 0a 20 20 20 20 46 49 4c 45 4c 4f  else{.    FILELO
bfdd0 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20  CK  LockArea,.  
bfde0 20 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c 6f              Unlo
bfdf0 63 6b 41 72 65 61 3b 0a 20 20 20 20 41 50 49 52  ckArea;.    APIR
bfe00 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52  ET rc = NO_ERROR
bfe10 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 4c 6f  ;.    memset(&Lo
bfe20 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f  ckArea, 0, sizeo
bfe30 66 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20  f(LockArea));.  
bfe40 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b    memset(&Unlock
bfe50 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Area, 0, sizeof(
bfe60 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20  UnlockArea));.  
bfe70 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73    LockArea.lOffs
bfe80 65 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59  et = RESERVED_BY
bfe90 54 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61  TE;.    LockArea
bfea0 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20  .lRange = 1L;.  
bfeb0 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66    UnlockArea.lOf
bfec0 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55  fset = 0L;.    U
bfed0 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65  nlockArea.lRange
bfee0 20 3d 20 30 4c 3b 0a 20 20 20 20 72 63 20 3d 20   = 0L;.    rc = 
bfef0 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28  DosSetFileLocks(
bff00 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f   pFile->h, &Unlo
bff10 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65  ckArea, &LockAre
bff20 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c  a, LOCK_TIMEOUT,
bff30 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41   0L );.    OSTRA
bff40 43 45 33 28 20 22 54 45 53 54 20 57 52 2d 4c 4f  CE3( "TEST WR-LO
bff50 43 4b 20 25 64 20 6c 6f 63 6b 20 72 65 73 65 72  CK %d lock reser
bff60 76 65 64 20 62 79 74 65 20 72 63 3d 25 64 5c 6e  ved byte rc=%d\n
bff70 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 20  ", pFile->h, rc 
bff80 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 3d 3d  );.    if( rc ==
bff90 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20   NO_ERROR ){.   
bffa0 20 20 20 41 50 49 52 45 54 20 72 63 75 20 3d 20     APIRET rcu = 
bffb0 4e 4f 5f 45 52 52 4f 52 3b 20 2f 2a 20 72 65 74  NO_ERROR; /* ret
bffc0 75 72 6e 20 63 6f 64 65 20 66 6f 72 20 75 6e 6c  urn code for unl
bffd0 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20  ocking */.      
bffe0 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74  LockArea.lOffset
bfff0 20 3d 20 30 4c 3b 0a 20 20 20 20 20 20 4c 6f 63   = 0L;.      Loc
c0000 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30  kArea.lRange = 0
c0010 4c 3b 0a 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41  L;.      UnlockA
c0020 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45  rea.lOffset = RE
c0030 53 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20  SERVED_BYTE;.   
c0040 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52     UnlockArea.lR
c0050 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 20  ange = 1L;.     
c0060 20 72 63 75 20 3d 20 44 6f 73 53 65 74 46 69 6c   rcu = DosSetFil
c0070 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68  eLocks( pFile->h
c0080 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26  , &UnlockArea, &
c0090 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54  LockArea, LOCK_T
c00a0 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20  IMEOUT, 0L );.  
c00b0 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 54      OSTRACE3( "T
c00c0 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 75  EST WR-LOCK %d u
c00d0 6e 6c 6f 63 6b 20 72 65 73 65 72 76 65 64 20 62  nlock reserved b
c00e0 79 74 65 20 72 3d 25 64 5c 6e 22 2c 20 70 46 69  yte r=%d\n", pFi
c00f0 6c 65 2d 3e 68 2c 20 72 63 75 20 29 3b 0a 20 20  le->h, rcu );.  
c0100 20 20 7d 0a 20 20 20 20 72 20 3d 20 21 28 72 63    }.    r = !(rc
c0110 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 3b 0a 20   == NO_ERROR);. 
c0120 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 45     OSTRACE3( "TE
c0130 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64  ST WR-LOCK %d %d
c0140 20 28 72 65 6d 6f 74 65 29 5c 6e 22 2c 20 70 46   (remote)\n", pF
c0150 69 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d  ile->h, r );.  }
c0160 0a 20 20 2a 70 4f 75 74 20 3d 20 72 3b 0a 20 20  .  *pOut = r;.  
c0170 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
c0180 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72  ;.}../*.** Lower
c0190 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76   the locking lev
c01a0 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72  el on file descr
c01b0 69 70 74 6f 72 20 69 64 20 74 6f 20 6c 6f 63 6b  iptor id to lock
c01c0 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a  type.  locktype.
c01d0 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65  ** must be eithe
c01e0 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41  r NO_LOCK or SHA
c01f0 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20  RED_LOCK..**.** 
c0200 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c  If the locking l
c0210 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65  evel of the file
c0220 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61   descriptor is a
c0230 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c  lready at or bel
c0240 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73  ow.** the reques
c0250 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  ted locking leve
c0260 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  l, this routine 
c0270 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a  is a no-op..**.*
c0280 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73  * It is not poss
c0290 69 62 6c 65 20 66 6f 72 20 74 68 69 73 20 72 6f  ible for this ro
c02a0 75 74 69 6e 65 20 74 6f 20 66 61 69 6c 20 69 66  utine to fail if
c02b0 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
c02c0 6d 65 6e 74 0a 2a 2a 20 69 73 20 4e 4f 5f 4c 4f  ment.** is NO_LO
c02d0 43 4b 2e 20 20 49 66 20 74 68 65 20 73 65 63 6f  CK.  If the seco
c02e0 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53  nd argument is S
c02f0 48 41 52 45 44 5f 4c 4f 43 4b 20 74 68 65 6e 20  HARED_LOCK then 
c0300 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
c0310 6d 69 67 68 74 20 72 65 74 75 72 6e 20 53 51 4c  might return SQL
c0320 49 54 45 5f 49 4f 45 52 52 3b 0a 2a 2f 0a 73 74  ITE_IOERR;.*/.st
c0330 61 74 69 63 20 69 6e 74 20 6f 73 32 55 6e 6c 6f  atic int os2Unlo
c0340 63 6b 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  ck( sqlite3_file
c0350 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79   *id, int lockty
c0360 70 65 20 29 7b 0a 20 20 69 6e 74 20 74 79 70 65  pe ){.  int type
c0370 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69  ;.  os2File *pFi
c0380 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69  le = (os2File*)i
c0390 64 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d  d;.  APIRET rc =
c03a0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 41 50   SQLITE_OK;.  AP
c03b0 49 52 45 54 20 72 65 73 20 3d 20 4e 4f 5f 45 52  IRET res = NO_ER
c03c0 52 4f 52 3b 0a 20 20 46 49 4c 45 4c 4f 43 4b 20  ROR;.  FILELOCK 
c03d0 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20   LockArea,.     
c03e0 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65         UnlockAre
c03f0 61 3b 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63  a;.  memset(&Loc
c0400 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66  kArea, 0, sizeof
c0410 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d  (LockArea));.  m
c0420 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65  emset(&UnlockAre
c0430 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c  a, 0, sizeof(Unl
c0440 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 61 73 73  ockArea));.  ass
c0450 65 72 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b  ert( pFile!=0 );
c0460 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74  .  assert( lockt
c0470 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype<=SHARED_LOCK
c0480 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 20   );.  OSTRACE4( 
c0490 22 55 4e 4c 4f 43 4b 20 25 64 20 74 6f 20 25 64  "UNLOCK %d to %d
c04a0 20 77 61 73 20 25 64 5c 6e 22 2c 20 70 46 69 6c   was %d\n", pFil
c04b0 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 20  e->h, locktype, 
c04c0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
c04d0 29 3b 0a 20 20 74 79 70 65 20 3d 20 70 46 69 6c  );.  type = pFil
c04e0 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69  e->locktype;.  i
c04f0 66 28 20 74 79 70 65 3e 3d 45 58 43 4c 55 53 49  f( type>=EXCLUSI
c0500 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c  VE_LOCK ){.    L
c0510 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20  ockArea.lOffset 
c0520 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72  = 0L;.    LockAr
c0530 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a  ea.lRange = 0L;.
c0540 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c      UnlockArea.l
c0550 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f  Offset = SHARED_
c0560 46 49 52 53 54 3b 0a 20 20 20 20 55 6e 6c 6f 63  FIRST;.    Unloc
c0570 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53  kArea.lRange = S
c0580 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20  HARED_SIZE;.    
c0590 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65  res = DosSetFile
c05a0 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c  Locks( pFile->h,
c05b0 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c   &UnlockArea, &L
c05c0 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49  ockArea, LOCK_TI
c05d0 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20  MEOUT, 0L );.   
c05e0 20 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f   OSTRACE3( "UNLO
c05f0 43 4b 20 25 64 20 65 78 63 6c 75 73 69 76 65 20  CK %d exclusive 
c0600 6c 6f 63 6b 20 72 65 73 3d 25 64 5c 6e 22 2c 20  lock res=%d\n", 
c0610 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b  pFile->h, res );
c0620 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 70  .    if( locktyp
c0630 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26  e==SHARED_LOCK &
c0640 26 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46  & getReadLock(pF
c0650 69 6c 65 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52  ile) != NO_ERROR
c0660 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69   ){.      /* Thi
c0670 73 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 68  s should never h
c0680 61 70 70 65 6e 2e 20 20 57 65 20 73 68 6f 75 6c  appen.  We shoul
c0690 64 20 61 6c 77 61 79 73 20 62 65 20 61 62 6c 65  d always be able
c06a0 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 72 65 61   to.      ** rea
c06b0 63 71 75 69 72 65 20 74 68 65 20 72 65 61 64 20  cquire the read 
c06c0 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 4f 53  lock */.      OS
c06d0 54 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20  TRACE3( "UNLOCK 
c06e0 25 64 20 74 6f 20 25 64 20 67 65 74 52 65 61 64  %d to %d getRead
c06f0 4c 6f 63 6b 28 29 20 66 61 69 6c 65 64 5c 6e 22  Lock() failed\n"
c0700 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b  , pFile->h, lock
c0710 74 79 70 65 20 29 3b 0a 20 20 20 20 20 20 72 63  type );.      rc
c0720 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f   = SQLITE_IOERR_
c0730 55 4e 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20  UNLOCK;.    }.  
c0740 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 52 45  }.  if( type>=RE
c0750 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20  SERVED_LOCK ){. 
c0760 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66     LockArea.lOff
c0770 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f  set = 0L;.    Lo
c0780 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20  ckArea.lRange = 
c0790 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72  0L;.    UnlockAr
c07a0 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53  ea.lOffset = RES
c07b0 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20  ERVED_BYTE;.    
c07c0 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67  UnlockArea.lRang
c07d0 65 20 3d 20 31 4c 3b 0a 20 20 20 20 72 65 73 20  e = 1L;.    res 
c07e0 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b  = DosSetFileLock
c07f0 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e  s( pFile->h, &Un
c0800 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41  lockArea, &LockA
c0810 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55  rea, LOCK_TIMEOU
c0820 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54  T, 0L );.    OST
c0830 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25  RACE3( "UNLOCK %
c0840 64 20 72 65 73 65 72 76 65 64 20 72 65 73 3d 25  d reserved res=%
c0850 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
c0860 72 65 73 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28  res );.  }.  if(
c0870 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f   locktype==NO_LO
c0880 43 4b 20 26 26 20 74 79 70 65 3e 3d 53 48 41 52  CK && type>=SHAR
c0890 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72  ED_LOCK ){.    r
c08a0 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c  es = unlockReadL
c08b0 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20  ock(pFile);.    
c08c0 4f 53 54 52 41 43 45 35 28 20 22 55 4e 4c 4f 43  OSTRACE5( "UNLOC
c08d0 4b 20 25 64 20 69 73 20 25 64 20 77 61 6e 74 20  K %d is %d want 
c08e0 25 64 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46  %d res=%d\n", pF
c08f0 69 6c 65 2d 3e 68 2c 20 74 79 70 65 2c 20 6c 6f  ile->h, type, lo
c0900 63 6b 74 79 70 65 2c 20 72 65 73 20 29 3b 0a 20  cktype, res );. 
c0910 20 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 50   }.  if( type>=P
c0920 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20  ENDING_LOCK ){. 
c0930 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66     LockArea.lOff
c0940 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f  set = 0L;.    Lo
c0950 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20  ckArea.lRange = 
c0960 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72  0L;.    UnlockAr
c0970 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e  ea.lOffset = PEN
c0980 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 55  DING_BYTE;.    U
c0990 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65  nlockArea.lRange
c09a0 20 3d 20 31 4c 3b 0a 20 20 20 20 72 65 73 20 3d   = 1L;.    res =
c09b0 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73   DosSetFileLocks
c09c0 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c  ( pFile->h, &Unl
c09d0 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72  ockArea, &LockAr
c09e0 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54  ea, LOCK_TIMEOUT
c09f0 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52  , 0L );.    OSTR
c0a00 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64  ACE3( "UNLOCK %d
c0a10 20 70 65 6e 64 69 6e 67 20 72 65 73 3d 25 64 5c   pending res=%d\
c0a20 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65  n", pFile->h, re
c0a30 73 20 29 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65  s );.  }.  pFile
c0a40 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63  ->locktype = loc
c0a50 6b 74 79 70 65 3b 0a 20 20 4f 53 54 52 41 43 45  ktype;.  OSTRACE
c0a60 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 6e 6f  3( "UNLOCK %d no
c0a70 77 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  w %d\n", pFile->
c0a80 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  h, pFile->lockty
c0a90 70 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  pe );.  return r
c0aa0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74  c;.}../*.** Cont
c0ab0 72 6f 6c 20 61 6e 64 20 71 75 65 72 79 20 6f 66  rol and query of
c0ac0 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 68   the open file h
c0ad0 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  andle..*/.static
c0ae0 20 69 6e 74 20 6f 73 32 46 69 6c 65 43 6f 6e 74   int os2FileCont
c0af0 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  rol(sqlite3_file
c0b00 20 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f   *id, int op, vo
c0b10 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 73 77 69  id *pArg){.  swi
c0b20 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63  tch( op ){.    c
c0b30 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c  ase SQLITE_FCNTL
c0b40 5f 4c 4f 43 4b 53 54 41 54 45 3a 20 7b 0a 20 20  _LOCKSTATE: {.  
c0b50 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20      *(int*)pArg 
c0b60 3d 20 28 28 6f 73 32 46 69 6c 65 2a 29 69 64 29  = ((os2File*)id)
c0b70 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20  ->locktype;.    
c0b80 20 20 4f 53 54 52 41 43 45 33 28 20 22 46 43 4e    OSTRACE3( "FCN
c0b90 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 20 25 64 20  TL_LOCKSTATE %d 
c0ba0 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 28 28 6f 73  lock=%d\n", ((os
c0bb0 32 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 28  2File*)id)->h, (
c0bc0 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 6c  (os2File*)id)->l
c0bd0 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 20  ocktype );.     
c0be0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
c0bf0 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  K;.    }.  }.  r
c0c00 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
c0c10 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  OR;.}../*.** Ret
c0c20 75 72 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73  urn the sector s
c0c30 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20  ize in bytes of 
c0c40 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62  the underlying b
c0c50 6c 6f 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a  lock device for.
c0c60 2a 2a 20 74 68 65 20 73 70 65 63 69 66 69 65 64  ** the specified
c0c70 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61   file. This is a
c0c80 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32  lmost always 512
c0c90 20 62 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20   bytes, but may 
c0ca0 62 65 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72  be.** larger for
c0cb0 20 73 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a   some devices..*
c0cc0 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65  *.** SQLite code
c0cd0 20 61 73 73 75 6d 65 73 20 74 68 69 73 20 66 75   assumes this fu
c0ce0 6e 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61  nction cannot fa
c0cf0 69 6c 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75  il. It also assu
c0d00 6d 65 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74  mes that.** if t
c0d10 77 6f 20 66 69 6c 65 73 20 61 72 65 20 63 72 65  wo files are cre
c0d20 61 74 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65  ated in the same
c0d30 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72   file-system dir
c0d40 65 63 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20  ectory (i.e..** 
c0d50 61 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 69  a database and i
c0d60 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29  ts journal file)
c0d70 20 74 68 61 74 20 74 68 65 20 73 65 63 74 6f 72   that the sector
c0d80 20 73 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68   size will be th
c0d90 65 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f  e.** same for bo
c0da0 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  th..*/.static in
c0db0 74 20 6f 73 32 53 65 63 74 6f 72 53 69 7a 65 28  t os2SectorSize(
c0dc0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
c0dd0 29 7b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  ){.  return SQLI
c0de0 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f  TE_DEFAULT_SECTO
c0df0 52 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  R_SIZE;.}../*.**
c0e00 20 52 65 74 75 72 6e 20 61 20 76 65 63 74 6f 72   Return a vector
c0e10 20 6f 66 20 64 65 76 69 63 65 20 63 68 61 72 61   of device chara
c0e20 63 74 65 72 69 73 74 69 63 73 2e 0a 2a 2f 0a 73  cteristics..*/.s
c0e30 74 61 74 69 63 20 69 6e 74 20 6f 73 32 44 65 76  tatic int os2Dev
c0e40 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69  iceCharacteristi
c0e50 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  cs(sqlite3_file 
c0e60 2a 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 30  *id){.  return 0
c0e70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 72  ;.}.../*.** Char
c0e80 61 63 74 65 72 20 73 65 74 20 63 6f 6e 76 65 72  acter set conver
c0e90 73 69 6f 6e 20 6f 62 6a 65 63 74 73 20 75 73 65  sion objects use
c0ea0 64 20 62 79 20 63 6f 6e 76 65 72 73 69 6f 6e 20  d by conversion 
c0eb0 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61  routines..*/.sta
c0ec0 74 69 63 20 55 63 6f 6e 76 4f 62 6a 65 63 74 20  tic UconvObject 
c0ed0 75 63 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 20 2f  ucUtf8 = NULL; /
c0ee0 2a 20 63 6f 6e 76 65 72 74 20 62 65 74 77 65 65  * convert betwee
c0ef0 6e 20 55 54 46 2d 38 20 61 6e 64 20 55 43 53 2d  n UTF-8 and UCS-
c0f00 32 20 2a 2f 0a 73 74 61 74 69 63 20 55 63 6f 6e  2 */.static Ucon
c0f10 76 4f 62 6a 65 63 74 20 75 63 6c 43 70 20 3d 20  vObject uclCp = 
c0f20 4e 55 4c 4c 3b 20 20 2f 2a 20 63 6f 6e 76 65 72  NULL;  /* conver
c0f30 74 20 62 65 74 77 65 65 6e 20 6c 6f 63 61 6c 20  t between local 
c0f40 63 6f 64 65 70 61 67 65 20 61 6e 64 20 55 43 53  codepage and UCS
c0f50 2d 32 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c  -2 */../*.** Hel
c0f60 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  per function to 
c0f70 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63  initialize the c
c0f80 6f 6e 76 65 72 73 69 6f 6e 20 6f 62 6a 65 63 74  onversion object
c0f90 73 20 66 72 6f 6d 20 61 6e 64 20 74 6f 20 55 54  s from and to UT
c0fa0 46 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  F-8..*/.static v
c0fb0 6f 69 64 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a  oid initUconvObj
c0fc0 65 63 74 73 28 20 76 6f 69 64 20 29 7b 0a 20 20  ects( void ){.  
c0fd0 69 66 28 20 55 6e 69 43 72 65 61 74 65 55 63 6f  if( UniCreateUco
c0fe0 6e 76 4f 62 6a 65 63 74 28 20 55 54 46 5f 38 2c  nvObject( UTF_8,
c0ff0 20 26 75 63 55 74 66 38 20 29 20 21 3d 20 55 4c   &ucUtf8 ) != UL
c1000 53 5f 53 55 43 43 45 53 53 20 29 0a 20 20 20 20  S_SUCCESS ).    
c1010 75 63 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 0a 20  ucUtf8 = NULL;. 
c1020 20 69 66 20 28 20 55 6e 69 43 72 65 61 74 65 55   if ( UniCreateU
c1030 63 6f 6e 76 4f 62 6a 65 63 74 28 20 28 55 6e 69  convObject( (Uni
c1040 43 68 61 72 20 2a 29 4c 22 40 70 61 74 68 3d 79  Char *)L"@path=y
c1050 65 73 22 2c 20 26 75 63 6c 43 70 20 29 20 21 3d  es", &uclCp ) !=
c1060 20 55 4c 53 5f 53 55 43 43 45 53 53 20 29 0a 20   ULS_SUCCESS ). 
c1070 20 20 20 75 63 6c 43 70 20 3d 20 4e 55 4c 4c 3b     uclCp = NULL;
c1080 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72  .}../*.** Helper
c1090 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 66 72 65   function to fre
c10a0 65 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e  e the conversion
c10b0 20 6f 62 6a 65 63 74 73 20 66 72 6f 6d 20 61 6e   objects from an
c10c0 64 20 74 6f 20 55 54 46 2d 38 2e 0a 2a 2f 0a 73  d to UTF-8..*/.s
c10d0 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 65 55  tatic void freeU
c10e0 63 6f 6e 76 4f 62 6a 65 63 74 73 28 20 76 6f 69  convObjects( voi
c10f0 64 20 29 7b 0a 20 20 69 66 20 28 20 75 63 55 74  d ){.  if ( ucUt
c1100 66 38 20 29 0a 20 20 20 20 55 6e 69 46 72 65 65  f8 ).    UniFree
c1110 55 63 6f 6e 76 4f 62 6a 65 63 74 28 20 75 63 55  UconvObject( ucU
c1120 74 66 38 20 29 3b 0a 20 20 69 66 20 28 20 75 63  tf8 );.  if ( uc
c1130 6c 43 70 20 29 0a 20 20 20 20 55 6e 69 46 72 65  lCp ).    UniFre
c1140 65 55 63 6f 6e 76 4f 62 6a 65 63 74 28 20 75 63  eUconvObject( uc
c1150 6c 43 70 20 29 3b 0a 20 20 75 63 55 74 66 38 20  lCp );.  ucUtf8 
c1160 3d 20 4e 55 4c 4c 3b 0a 20 20 75 63 6c 43 70 20  = NULL;.  uclCp 
c1170 3d 20 4e 55 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  = NULL;.}../*.**
c1180 20 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e   Helper function
c1190 20 74 6f 20 63 6f 6e 76 65 72 74 20 55 54 46 2d   to convert UTF-
c11a0 38 20 66 69 6c 65 6e 61 6d 65 73 20 74 6f 20 6c  8 filenames to l
c11b0 6f 63 61 6c 20 4f 53 2f 32 20 63 6f 64 65 70 61  ocal OS/2 codepa
c11c0 67 65 2e 0a 2a 2a 20 54 68 65 20 74 77 6f 2d 73  ge..** The two-s
c11d0 74 65 70 20 70 72 6f 63 65 73 73 3a 20 66 69 72  tep process: fir
c11e0 73 74 20 63 6f 6e 76 65 72 74 20 74 68 65 20 69  st convert the i
c11f0 6e 63 6f 6d 69 6e 67 20 55 54 46 2d 38 20 73 74  ncoming UTF-8 st
c1200 72 69 6e 67 0a 2a 2a 20 69 6e 74 6f 20 55 43 53  ring.** into UCS
c1210 2d 32 20 61 6e 64 20 74 68 65 6e 20 66 72 6f 6d  -2 and then from
c1220 20 55 43 53 2d 32 20 74 6f 20 74 68 65 20 63 75   UCS-2 to the cu
c1230 72 72 65 6e 74 20 63 6f 64 65 70 61 67 65 2e 0a  rrent codepage..
c1240 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20  ** The returned 
c1250 63 68 61 72 20 70 6f 69 6e 74 65 72 20 68 61 73  char pointer has
c1260 20 74 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a 2f   to be freed..*/
c1270 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 63 6f  .static char *co
c1280 6e 76 65 72 74 55 74 66 38 50 61 74 68 54 6f 43  nvertUtf8PathToC
c1290 70 28 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69  p( const char *i
c12a0 6e 20 29 7b 0a 20 20 55 6e 69 43 68 61 72 20 74  n ){.  UniChar t
c12b0 65 6d 70 50 61 74 68 5b 43 43 48 4d 41 58 50 41  empPath[CCHMAXPA
c12c0 54 48 5d 3b 0a 20 20 63 68 61 72 20 2a 6f 75 74  TH];.  char *out
c12d0 20 3d 20 28 63 68 61 72 20 2a 29 63 61 6c 6c 6f   = (char *)callo
c12e0 63 28 20 43 43 48 4d 41 58 50 41 54 48 2c 20 31  c( CCHMAXPATH, 1
c12f0 20 29 3b 0a 0a 20 20 69 66 28 20 21 6f 75 74 20   );..  if( !out 
c1300 29 0a 20 20 20 20 72 65 74 75 72 6e 20 4e 55 4c  ).    return NUL
c1310 4c 3b 0a 0a 20 20 69 66 28 20 21 75 63 55 74 66  L;..  if( !ucUtf
c1320 38 20 7c 7c 20 21 75 63 6c 43 70 20 29 0a 20 20  8 || !uclCp ).  
c1330 20 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 63    initUconvObjec
c1340 74 73 28 29 3b 0a 0a 20 20 2f 2a 20 64 65 74 65  ts();..  /* dete
c1350 72 6d 69 6e 65 20 73 74 72 69 6e 67 20 66 6f 72  rmine string for
c1360 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20   the conversion 
c1370 6f 66 20 55 54 46 2d 38 20 77 68 69 63 68 20 69  of UTF-8 which i
c1380 73 20 43 50 31 32 30 38 20 2a 2f 0a 20 20 69 66  s CP1208 */.  if
c1390 28 20 55 6e 69 53 74 72 54 6f 55 63 73 28 20 75  ( UniStrToUcs( u
c13a0 63 55 74 66 38 2c 20 74 65 6d 70 50 61 74 68 2c  cUtf8, tempPath,
c13b0 20 28 63 68 61 72 20 2a 29 69 6e 2c 20 43 43 48   (char *)in, CCH
c13c0 4d 41 58 50 41 54 48 20 29 20 21 3d 20 55 4c 53  MAXPATH ) != ULS
c13d0 5f 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 72  _SUCCESS ).    r
c13e0 65 74 75 72 6e 20 6f 75 74 3b 20 2f 2a 20 69 66  eturn out; /* if
c13f0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 61 69 6c   conversion fail
c1400 73 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65 6d  s, return the em
c1410 70 74 79 20 73 74 72 69 6e 67 20 2a 2f 0a 0a 20  pty string */.. 
c1420 20 2f 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66   /* conversion f
c1430 6f 72 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70  or current codep
c1440 61 67 65 20 77 68 69 63 68 20 63 61 6e 20 62 65  age which can be
c1450 20 75 73 65 64 20 66 6f 72 20 70 61 74 68 73 20   used for paths 
c1460 2a 2f 0a 20 20 55 6e 69 53 74 72 46 72 6f 6d 55  */.  UniStrFromU
c1470 63 73 28 20 75 63 6c 43 70 2c 20 6f 75 74 2c 20  cs( uclCp, out, 
c1480 74 65 6d 70 50 61 74 68 2c 20 43 43 48 4d 41 58  tempPath, CCHMAX
c1490 50 41 54 48 20 29 3b 0a 0a 20 20 72 65 74 75 72  PATH );..  retur
c14a0 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n out;.}../*.** 
c14b0 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20  Helper function 
c14c0 74 6f 20 63 6f 6e 76 65 72 74 20 66 69 6c 65 6e  to convert filen
c14d0 61 6d 65 73 20 66 72 6f 6d 20 6c 6f 63 61 6c 20  ames from local 
c14e0 63 6f 64 65 70 61 67 65 20 74 6f 20 55 54 46 2d  codepage to UTF-
c14f0 38 2e 0a 2a 2a 20 54 68 65 20 74 77 6f 2d 73 74  8..** The two-st
c1500 65 70 20 70 72 6f 63 65 73 73 3a 20 66 69 72 73  ep process: firs
c1510 74 20 63 6f 6e 76 65 72 74 20 74 68 65 20 69 6e  t convert the in
c1520 63 6f 6d 69 6e 67 20 63 6f 64 65 70 61 67 65 2d  coming codepage-
c1530 73 70 65 63 69 66 69 63 0a 2a 2a 20 73 74 72 69  specific.** stri
c1540 6e 67 20 69 6e 74 6f 20 55 43 53 2d 32 20 61 6e  ng into UCS-2 an
c1550 64 20 74 68 65 6e 20 66 72 6f 6d 20 55 43 53 2d  d then from UCS-
c1560 32 20 74 6f 20 74 68 65 20 63 6f 64 65 70 61 67  2 to the codepag
c1570 65 20 6f 66 20 55 54 46 2d 38 2e 0a 2a 2a 20 54  e of UTF-8..** T
c1580 68 65 20 72 65 74 75 72 6e 65 64 20 63 68 61 72  he returned char
c1590 20 70 6f 69 6e 74 65 72 20 68 61 73 20 74 6f 20   pointer has to 
c15a0 62 65 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20  be freed..**.** 
c15b0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
c15c0 20 6e 6f 6e 2d 73 74 61 74 69 63 20 74 6f 20 62   non-static to b
c15d0 65 20 61 62 6c 65 20 74 6f 20 75 73 65 20 74 68  e able to use th
c15e0 69 73 20 69 6e 20 73 68 65 6c 6c 2e 63 20 61 6e  is in shell.c an
c15f0 64 0a 2a 2a 20 73 69 6d 69 6c 61 72 20 61 70 70  d.** similar app
c1600 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 74  lications that t
c1610 61 6b 65 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65  ake command line
c1620 20 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 63   arguments..*/.c
c1630 68 61 72 20 2a 63 6f 6e 76 65 72 74 43 70 50 61  har *convertCpPa
c1640 74 68 54 6f 55 74 66 38 28 20 63 6f 6e 73 74 20  thToUtf8( const 
c1650 63 68 61 72 20 2a 69 6e 20 29 7b 0a 20 20 55 6e  char *in ){.  Un
c1660 69 43 68 61 72 20 74 65 6d 70 50 61 74 68 5b 43  iChar tempPath[C
c1670 43 48 4d 41 58 50 41 54 48 5d 3b 0a 20 20 63 68  CHMAXPATH];.  ch
c1680 61 72 20 2a 6f 75 74 20 3d 20 28 63 68 61 72 20  ar *out = (char 
c1690 2a 29 63 61 6c 6c 6f 63 28 20 43 43 48 4d 41 58  *)calloc( CCHMAX
c16a0 50 41 54 48 2c 20 31 20 29 3b 0a 0a 20 20 69 66  PATH, 1 );..  if
c16b0 28 20 21 6f 75 74 20 29 0a 20 20 20 20 72 65 74  ( !out ).    ret
c16c0 75 72 6e 20 4e 55 4c 4c 3b 0a 0a 20 20 69 66 28  urn NULL;..  if(
c16d0 20 21 75 63 55 74 66 38 20 7c 7c 20 21 75 63 6c   !ucUtf8 || !ucl
c16e0 43 70 20 29 0a 20 20 20 20 69 6e 69 74 55 63 6f  Cp ).    initUco
c16f0 6e 76 4f 62 6a 65 63 74 73 28 29 3b 0a 0a 20 20  nvObjects();..  
c1700 2f 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 6f  /* conversion fo
c1710 72 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61  r current codepa
c1720 67 65 20 77 68 69 63 68 20 63 61 6e 20 62 65 20  ge which can be 
c1730 75 73 65 64 20 66 6f 72 20 70 61 74 68 73 20 2a  used for paths *
c1740 2f 0a 20 20 69 66 28 20 55 6e 69 53 74 72 54 6f  /.  if( UniStrTo
c1750 55 63 73 28 20 75 63 6c 43 70 2c 20 74 65 6d 70  Ucs( uclCp, temp
c1760 50 61 74 68 2c 20 28 63 68 61 72 20 2a 29 69 6e  Path, (char *)in
c1770 2c 20 43 43 48 4d 41 58 50 41 54 48 20 29 20 21  , CCHMAXPATH ) !
c1780 3d 20 55 4c 53 5f 53 55 43 43 45 53 53 20 29 0a  = ULS_SUCCESS ).
c1790 20 20 20 20 72 65 74 75 72 6e 20 6f 75 74 3b 20      return out; 
c17a0 2f 2a 20 69 66 20 63 6f 6e 76 65 72 73 69 6f 6e  /* if conversion
c17b0 20 66 61 69 6c 73 2c 20 72 65 74 75 72 6e 20 74   fails, return t
c17c0 68 65 20 65 6d 70 74 79 20 73 74 72 69 6e 67 20  he empty string 
c17d0 2a 2f 0a 0a 20 20 2f 2a 20 64 65 74 65 72 6d 69  */..  /* determi
c17e0 6e 65 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68  ne string for th
c17f0 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 66 20  e conversion of 
c1800 55 54 46 2d 38 20 77 68 69 63 68 20 69 73 20 43  UTF-8 which is C
c1810 50 31 32 30 38 20 2a 2f 0a 20 20 55 6e 69 53 74  P1208 */.  UniSt
c1820 72 46 72 6f 6d 55 63 73 28 20 75 63 55 74 66 38  rFromUcs( ucUtf8
c1830 2c 20 6f 75 74 2c 20 74 65 6d 70 50 61 74 68 2c  , out, tempPath,
c1840 20 43 43 48 4d 41 58 50 41 54 48 20 29 3b 0a 0a   CCHMAXPATH );..
c1850 20 20 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a    return out;.}.
c1860 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 63 74  ./*.** This vect
c1870 6f 72 20 64 65 66 69 6e 65 73 20 61 6c 6c 20 74  or defines all t
c1880 68 65 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20  he methods that 
c1890 63 61 6e 20 6f 70 65 72 61 74 65 20 6f 6e 20 61  can operate on a
c18a0 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 69 6c  n.** sqlite3_fil
c18b0 65 20 66 6f 72 20 6f 73 32 2e 0a 2a 2f 0a 73 74  e for os2..*/.st
c18c0 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74  atic const sqlit
c18d0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 73  e3_io_methods os
c18e0 32 49 6f 4d 65 74 68 6f 64 20 3d 20 7b 0a 20 20  2IoMethod = {.  
c18f0 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1,              
c1900 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65            /* iVe
c1910 72 73 69 6f 6e 20 2a 2f 0a 20 20 6f 73 32 43 6c  rsion */.  os2Cl
c1920 6f 73 65 2c 0a 20 20 6f 73 32 52 65 61 64 2c 0a  ose,.  os2Read,.
c1930 20 20 6f 73 32 57 72 69 74 65 2c 0a 20 20 6f 73    os2Write,.  os
c1940 32 54 72 75 6e 63 61 74 65 2c 0a 20 20 6f 73 32  2Truncate,.  os2
c1950 53 79 6e 63 2c 0a 20 20 6f 73 32 46 69 6c 65 53  Sync,.  os2FileS
c1960 69 7a 65 2c 0a 20 20 6f 73 32 4c 6f 63 6b 2c 0a  ize,.  os2Lock,.
c1970 20 20 6f 73 32 55 6e 6c 6f 63 6b 2c 0a 20 20 6f    os2Unlock,.  o
c1980 73 32 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  s2CheckReservedL
c1990 6f 63 6b 2c 0a 20 20 6f 73 32 46 69 6c 65 43 6f  ock,.  os2FileCo
c19a0 6e 74 72 6f 6c 2c 0a 20 20 6f 73 32 53 65 63 74  ntrol,.  os2Sect
c19b0 6f 72 53 69 7a 65 2c 0a 20 20 6f 73 32 44 65 76  orSize,.  os2Dev
c19c0 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69  iceCharacteristi
c19d0 63 73 0a 7d 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  cs.};../********
c19e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c19f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1a20 2a 2a 2a 0a 2a 2a 20 48 65 72 65 20 65 6e 64 73  ***.** Here ends
c1a30 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73   the I/O methods
c1a40 20 74 68 61 74 20 66 6f 72 6d 20 74 68 65 20 73   that form the s
c1a50 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
c1a60 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20  s object..**.** 
c1a70 54 68 65 20 6e 65 78 74 20 62 6c 6f 63 6b 20 6f  The next block o
c1a80 66 20 63 6f 64 65 20 69 6d 70 6c 65 6d 65 6e 74  f code implement
c1a90 73 20 74 68 65 20 56 46 53 20 6d 65 74 68 6f 64  s the VFS method
c1aa0 73 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s..*************
c1ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c1ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
c1af0 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61  ../*.** Create a
c1b00 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20   temporary file 
c1b10 6e 61 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a  name in zBuf.  z
c1b20 42 75 66 20 6d 75 73 74 20 62 65 20 62 69 67 20  Buf must be big 
c1b30 65 6e 6f 75 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c  enough to.** hol
c1b40 64 20 61 74 20 70 56 66 73 2d 3e 6d 78 50 61 74  d at pVfs->mxPat
c1b50 68 6e 61 6d 65 20 63 68 61 72 61 63 74 65 72 73  hname characters
c1b60 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
c1b70 67 65 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20  getTempname(int 
c1b80 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66  nBuf, char *zBuf
c1b90 20 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e   ){.  static con
c1ba0 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
c1bb0 20 7a 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20   zChars[] =.    
c1bc0 22 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f  "abcdefghijklmno
c1bd0 70 71 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20  pqrstuvwxyz".   
c1be0 20 22 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e   "ABCDEFGHIJKLMN
c1bf0 4f 50 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20  OPQRSTUVWXYZ".  
c1c00 20 20 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a    "0123456789";.
c1c10 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 63 68    int i, j;.  ch
c1c20 61 72 20 7a 54 65 6d 70 50 61 74 68 42 75 66 5b  ar zTempPathBuf[
c1c30 33 5d 3b 0a 20 20 50 53 5a 20 7a 54 65 6d 70 50  3];.  PSZ zTempP
c1c40 61 74 68 20 3d 20 28 50 53 5a 29 26 7a 54 65 6d  ath = (PSZ)&zTem
c1c50 70 50 61 74 68 42 75 66 3b 0a 20 20 69 66 28 20  pPathBuf;.  if( 
c1c60 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72  sqlite3_temp_dir
c1c70 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 7a 54  ectory ){.    zT
c1c80 65 6d 70 50 61 74 68 20 3d 20 73 71 6c 69 74 65  empPath = sqlite
c1c90 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79  3_temp_directory
c1ca0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  ;.  }else{.    i
c1cb0 66 28 20 44 6f 73 53 63 61 6e 45 6e 76 28 20 28  f( DosScanEnv( (
c1cc0 50 53 5a 29 22 54 45 4d 50 22 2c 20 26 7a 54 65  PSZ)"TEMP", &zTe
c1cd0 6d 70 50 61 74 68 20 29 20 29 7b 0a 20 20 20 20  mpPath ) ){.    
c1ce0 20 20 69 66 28 20 44 6f 73 53 63 61 6e 45 6e 76    if( DosScanEnv
c1cf0 28 20 28 50 53 5a 29 22 54 4d 50 22 2c 20 26 7a  ( (PSZ)"TMP", &z
c1d00 54 65 6d 70 50 61 74 68 20 29 20 29 7b 0a 20 20  TempPath ) ){.  
c1d10 20 20 20 20 20 20 69 66 28 20 44 6f 73 53 63 61        if( DosSca
c1d20 6e 45 6e 76 28 20 28 50 53 5a 29 22 54 4d 50 44  nEnv( (PSZ)"TMPD
c1d30 49 52 22 2c 20 26 7a 54 65 6d 70 50 61 74 68 20  IR", &zTempPath 
c1d40 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ) ){.           
c1d50 55 4c 4f 4e 47 20 75 6c 44 72 69 76 65 4e 75 6d  ULONG ulDriveNum
c1d60 20 3d 20 30 2c 20 75 6c 44 72 69 76 65 4d 61 70   = 0, ulDriveMap
c1d70 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
c1d80 20 44 6f 73 51 75 65 72 79 43 75 72 72 65 6e 74   DosQueryCurrent
c1d90 44 69 73 6b 28 20 26 75 6c 44 72 69 76 65 4e 75  Disk( &ulDriveNu
c1da0 6d 2c 20 26 75 6c 44 72 69 76 65 4d 61 70 20 29  m, &ulDriveMap )
c1db0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 73 70 72  ;.           spr
c1dc0 69 6e 74 66 28 20 28 63 68 61 72 2a 29 7a 54 65  intf( (char*)zTe
c1dd0 6d 70 50 61 74 68 2c 20 22 25 63 3a 22 2c 20 28  mpPath, "%c:", (
c1de0 63 68 61 72 29 28 20 27 41 27 20 2b 20 75 6c 44  char)( 'A' + ulD
c1df0 72 69 76 65 4e 75 6d 20 2d 20 31 20 29 20 29 3b  riveNum - 1 ) );
c1e00 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
c1e10 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f   }.    }.  }.  /
c1e20 2a 20 53 74 72 69 70 20 6f 66 66 20 61 20 74 72  * Strip off a tr
c1e30 61 69 6c 69 6e 67 20 73 6c 61 73 68 65 73 20 6f  ailing slashes o
c1e40 72 20 62 61 63 6b 73 6c 61 73 68 65 73 2c 20 6f  r backslashes, o
c1e50 74 68 65 72 77 69 73 65 20 77 65 20 77 6f 75 6c  therwise we woul
c1e60 64 20 67 65 74 20 2a 0a 20 20 20 2a 20 6d 75 6c  d get *.   * mul
c1e70 74 69 70 6c 65 20 28 62 61 63 6b 29 73 6c 61 73  tiple (back)slas
c1e80 68 65 73 20 77 68 69 63 68 20 63 61 75 73 65 73  hes which causes
c1e90 20 44 6f 73 4f 70 65 6e 28 29 20 74 6f 20 66 61   DosOpen() to fa
c1ea0 69 6c 2e 20 20 20 20 20 20 20 20 20 20 20 20 20  il.             
c1eb0 20 2a 0a 20 20 20 2a 20 54 72 61 69 6c 69 6e 67   *.   * Trailing
c1ec0 20 73 70 61 63 65 73 20 61 72 65 20 6e 6f 74 20   spaces are not 
c1ed0 61 6c 6c 6f 77 65 64 2c 20 65 69 74 68 65 72 2e  allowed, either.
c1ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c1ef0 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20              */. 
c1f00 20 6a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c   j = sqlite3Strl
c1f10 65 6e 33 30 28 7a 54 65 6d 70 50 61 74 68 29 3b  en30(zTempPath);
c1f20 0a 20 20 77 68 69 6c 65 28 20 6a 20 3e 20 30 20  .  while( j > 0 
c1f30 26 26 20 28 20 7a 54 65 6d 70 50 61 74 68 5b 6a  && ( zTempPath[j
c1f40 2d 31 5d 20 3d 3d 20 27 5c 5c 27 20 7c 7c 20 7a  -1] == '\\' || z
c1f50 54 65 6d 70 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d  TempPath[j-1] ==
c1f60 20 27 2f 27 0a 20 20 20 20 20 20 20 20 20 20 20   '/'.           
c1f70 20 20 20 20 20 20 20 20 20 7c 7c 20 7a 54 65 6d           || zTem
c1f80 70 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20 27 20  pPath[j-1] == ' 
c1f90 27 20 29 20 29 7b 0a 20 20 20 20 6a 2d 2d 3b 0a  ' ) ){.    j--;.
c1fa0 20 20 7d 0a 20 20 7a 54 65 6d 70 50 61 74 68 5b    }.  zTempPath[
c1fb0 6a 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66 28  j] = '\0';.  if(
c1fc0 20 21 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64   !sqlite3_temp_d
c1fd0 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20  irectory ){.    
c1fe0 63 68 61 72 20 2a 7a 54 65 6d 70 50 61 74 68 55  char *zTempPathU
c1ff0 54 46 20 3d 20 63 6f 6e 76 65 72 74 43 70 50 61  TF = convertCpPa
c2000 74 68 54 6f 55 74 66 38 28 20 7a 54 65 6d 70 50  thToUtf8( zTempP
c2010 61 74 68 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  ath );.    sqlit
c2020 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 6e 42 75  e3_snprintf( nBu
c2030 66 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 20 20 20  f-30, zBuf,.    
c2040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c2050 20 20 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54    "%s\\"SQLITE_T
c2060 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c  EMP_FILE_PREFIX,
c2070 20 7a 54 65 6d 70 50 61 74 68 55 54 46 20 29 3b   zTempPathUTF );
c2080 0a 20 20 20 20 66 72 65 65 28 20 7a 54 65 6d 70  .    free( zTemp
c2090 50 61 74 68 55 54 46 20 29 3b 0a 20 20 7d 65 6c  PathUTF );.  }el
c20a0 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  se{.    sqlite3_
c20b0 73 6e 70 72 69 6e 74 66 28 20 6e 42 75 66 2d 33  snprintf( nBuf-3
c20c0 30 2c 20 7a 42 75 66 2c 0a 20 20 20 20 20 20 20  0, zBuf,.       
c20d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
c20e0 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d 50  %s\\"SQLITE_TEMP
c20f0 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 54  _FILE_PREFIX, zT
c2100 65 6d 70 50 61 74 68 20 29 3b 0a 20 20 7d 0a 20  empPath );.  }. 
c2110 20 6a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c   j = sqlite3Strl
c2120 65 6e 33 30 28 20 7a 42 75 66 20 29 3b 0a 20 20  en30( zBuf );.  
c2130 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65  sqlite3_randomne
c2140 73 73 28 20 32 30 2c 20 26 7a 42 75 66 5b 6a 5d  ss( 20, &zBuf[j]
c2150 20 29 3b 0a 20 20 66 6f 72 28 20 69 20 3d 20 30   );.  for( i = 0
c2160 3b 20 69 20 3c 20 32 30 3b 20 69 2b 2b 2c 20 6a  ; i < 20; i++, j
c2170 2b 2b 20 29 7b 0a 20 20 20 20 7a 42 75 66 5b 6a  ++ ){.    zBuf[j
c2180 5d 20 3d 20 28 63 68 61 72 29 7a 43 68 61 72 73  ] = (char)zChars
c2190 5b 20 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61  [ ((unsigned cha
c21a0 72 29 7a 42 75 66 5b 6a 5d 29 25 28 73 69 7a 65  r)zBuf[j])%(size
c21b0 6f 66 28 7a 43 68 61 72 73 29 2d 31 29 20 5d 3b  of(zChars)-1) ];
c21c0 0a 20 20 7d 0a 20 20 7a 42 75 66 5b 6a 5d 20 3d  .  }.  zBuf[j] =
c21d0 20 30 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 20   0;.  OSTRACE2( 
c21e0 22 54 45 4d 50 20 46 49 4c 45 4e 41 4d 45 3a 20  "TEMP FILENAME: 
c21f0 25 73 5c 6e 22 2c 20 7a 42 75 66 20 29 3b 0a 20  %s\n", zBuf );. 
c2200 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
c2210 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 72  K;.}.../*.** Tur
c2220 6e 20 61 20 72 65 6c 61 74 69 76 65 20 70 61 74  n a relative pat
c2230 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20 66 75 6c  hname into a ful
c2240 6c 20 70 61 74 68 6e 61 6d 65 2e 20 20 57 72 69  l pathname.  Wri
c2250 74 65 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 70  te the full.** p
c2260 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 7a 46 75  athname into zFu
c2270 6c 6c 5b 5d 2e 20 20 7a 46 75 6c 6c 5b 5d 20 77  ll[].  zFull[] w
c2280 69 6c 6c 20 62 65 20 61 74 20 6c 65 61 73 74 20  ill be at least 
c2290 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65  pVfs->mxPathname
c22a0 0a 2a 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a  .** bytes in siz
c22b0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
c22c0 20 6f 73 32 46 75 6c 6c 50 61 74 68 6e 61 6d 65   os2FullPathname
c22d0 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  (.  sqlite3_vfs 
c22e0 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20  *pVfs,          
c22f0 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66  /* Pointer to vf
c2300 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f  s object */.  co
c2310 6e 73 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74  nst char *zRelat
c2320 69 76 65 2c 20 20 20 20 20 20 2f 2a 20 50 6f 73  ive,      /* Pos
c2330 73 69 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69  sibly relative i
c2340 6e 70 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69  nput path */.  i
c2350 6e 74 20 6e 46 75 6c 6c 2c 20 20 20 20 20 20 20  nt nFull,       
c2360 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
c2370 7a 65 20 6f 66 20 6f 75 74 70 75 74 20 62 75 66  ze of output buf
c2380 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  fer in bytes */.
c2390 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c 20 20 20    char *zFull   
c23a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
c23b0 20 4f 75 74 70 75 74 20 62 75 66 66 65 72 20 2a   Output buffer *
c23c0 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 52 65  /.){.  char *zRe
c23d0 6c 61 74 69 76 65 43 70 20 3d 20 63 6f 6e 76 65  lativeCp = conve
c23e0 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 20  rtUtf8PathToCp( 
c23f0 7a 52 65 6c 61 74 69 76 65 20 29 3b 0a 20 20 63  zRelative );.  c
c2400 68 61 72 20 7a 46 75 6c 6c 43 70 5b 43 43 48 4d  har zFullCp[CCHM
c2410 41 58 50 41 54 48 5d 20 3d 20 22 5c 30 22 3b 0a  AXPATH] = "\0";.
c2420 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c 55 54 46    char *zFullUTF
c2430 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20  ;.  APIRET rc = 
c2440 44 6f 73 51 75 65 72 79 50 61 74 68 49 6e 66 6f  DosQueryPathInfo
c2450 28 20 7a 52 65 6c 61 74 69 76 65 43 70 2c 20 46  ( zRelativeCp, F
c2460 49 4c 5f 51 55 45 52 59 46 55 4c 4c 4e 41 4d 45  IL_QUERYFULLNAME
c2470 2c 20 7a 46 75 6c 6c 43 70 2c 0a 20 20 20 20 20  , zFullCp,.     
c2480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c2490 20 20 20 20 20 20 20 20 20 20 20 43 43 48 4d 41             CCHMA
c24a0 58 50 41 54 48 20 29 3b 0a 20 20 66 72 65 65 28  XPATH );.  free(
c24b0 20 7a 52 65 6c 61 74 69 76 65 43 70 20 29 3b 0a   zRelativeCp );.
c24c0 20 20 7a 46 75 6c 6c 55 54 46 20 3d 20 63 6f 6e    zFullUTF = con
c24d0 76 65 72 74 43 70 50 61 74 68 54 6f 55 74 66 38  vertCpPathToUtf8
c24e0 28 20 7a 46 75 6c 6c 43 70 20 29 3b 0a 20 20 73  ( zFullCp );.  s
c24f0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
c2500 20 6e 46 75 6c 6c 2c 20 7a 46 75 6c 6c 2c 20 7a   nFull, zFull, z
c2510 46 75 6c 6c 55 54 46 20 29 3b 0a 20 20 66 72 65  FullUTF );.  fre
c2520 65 28 20 7a 46 75 6c 6c 55 54 46 20 29 3b 0a 20  e( zFullUTF );. 
c2530 20 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f   return rc == NO
c2540 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f  _ERROR ? SQLITE_
c2550 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52  OK : SQLITE_IOER
c2560 52 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65  R;.}.../*.** Ope
c2570 6e 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61  n a file..*/.sta
c2580 74 69 63 20 69 6e 74 20 6f 73 32 4f 70 65 6e 28  tic int os2Open(
c2590 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
c25a0 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20  pVfs,           
c25b0 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a   /* Not used */.
c25c0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
c25d0 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ame,            
c25e0 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66  /* Name of the f
c25f0 69 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ile */.  sqlite3
c2600 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20  _file *id,      
c2610 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
c2620 74 68 65 20 53 51 4c 69 74 65 20 66 69 6c 65 20  the SQLite file 
c2630 68 61 6e 64 6c 65 20 68 65 72 65 20 2a 2f 0a 20  handle here */. 
c2640 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20   int flags,     
c2650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
c2660 2a 20 4f 70 65 6e 20 6d 6f 64 65 20 66 6c 61 67  * Open mode flag
c2670 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74  s */.  int *pOut
c2680 46 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 20  Flags           
c2690 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20 72       /* Status r
c26a0 65 74 75 72 6e 20 66 6c 61 67 73 20 2a 2f 0a 29  eturn flags */.)
c26b0 7b 0a 20 20 48 46 49 4c 45 20 68 3b 0a 20 20 55  {.  HFILE h;.  U
c26c0 4c 4f 4e 47 20 75 6c 46 69 6c 65 41 74 74 72 69  LONG ulFileAttri
c26d0 62 75 74 65 20 3d 20 46 49 4c 45 5f 4e 4f 52 4d  bute = FILE_NORM
c26e0 41 4c 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 4f 70  AL;.  ULONG ulOp
c26f0 65 6e 46 6c 61 67 73 20 3d 20 30 3b 0a 20 20 55  enFlags = 0;.  U
c2700 4c 4f 4e 47 20 75 6c 4f 70 65 6e 4d 6f 64 65 20  LONG ulOpenMode 
c2710 3d 20 30 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a  = 0;.  os2File *
c2720 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65  pFile = (os2File
c2730 2a 29 69 64 3b 0a 20 20 41 50 49 52 45 54 20 72  *)id;.  APIRET r
c2740 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20  c = NO_ERROR;.  
c2750 55 4c 4f 4e 47 20 75 6c 41 63 74 69 6f 6e 3b 0a  ULONG ulAction;.
c2760 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 43 70 3b    char *zNameCp;
c2770 0a 20 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d 65  .  char zTmpname
c2780 5b 43 43 48 4d 41 58 50 41 54 48 2b 31 5d 3b 20  [CCHMAXPATH+1]; 
c2790 20 20 20 2f 2a 20 42 75 66 66 65 72 20 74 6f 20     /* Buffer to 
c27a0 68 6f 6c 64 20 6e 61 6d 65 20 6f 66 20 74 65 6d  hold name of tem
c27b0 70 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 2f 2a 20  p file */..  /* 
c27c0 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  If the second ar
c27d0 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66  gument to this f
c27e0 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c  unction is NULL,
c27f0 20 67 65 6e 65 72 61 74 65 20 61 20 0a 20 20 2a   generate a .  *
c2800 2a 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  * temporary file
c2810 20 6e 61 6d 65 20 74 6f 20 75 73 65 20 0a 20 20   name to use .  
c2820 2a 2f 0a 20 20 69 66 28 20 21 7a 4e 61 6d 65 20  */.  if( !zName 
c2830 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20  ){.    int rc = 
c2840 67 65 74 54 65 6d 70 6e 61 6d 65 28 43 43 48 4d  getTempname(CCHM
c2850 41 58 50 41 54 48 2b 31 2c 20 7a 54 6d 70 6e 61  AXPATH+1, zTmpna
c2860 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  me);.    if( rc!
c2870 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
c2880 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
c2890 20 20 20 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d     }.    zName =
c28a0 20 7a 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a   zTmpname;.  }..
c28b0 0a 20 20 6d 65 6d 73 65 74 28 20 70 46 69 6c 65  .  memset( pFile
c28c0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 46 69  , 0, sizeof(*pFi
c28d0 6c 65 29 20 29 3b 0a 0a 20 20 4f 53 54 52 41 43  le) );..  OSTRAC
c28e0 45 32 28 20 22 4f 50 45 4e 20 77 61 6e 74 20 25  E2( "OPEN want %
c28f0 64 5c 6e 22 2c 20 66 6c 61 67 73 20 29 3b 0a 0a  d\n", flags );..
c2900 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51    if( flags & SQ
c2910 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
c2920 49 54 45 20 29 7b 0a 20 20 20 20 75 6c 4f 70 65  ITE ){.    ulOpe
c2930 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 41 43  nMode |= OPEN_AC
c2940 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 3b 0a  CESS_READWRITE;.
c2950 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f      OSTRACE1( "O
c2960 50 45 4e 20 72 65 61 64 2f 77 72 69 74 65 5c 6e  PEN read/write\n
c2970 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  " );.  }else{.  
c2980 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20    ulOpenMode |= 
c2990 4f 50 45 4e 5f 41 43 43 45 53 53 5f 52 45 41 44  OPEN_ACCESS_READ
c29a0 4f 4e 4c 59 3b 0a 20 20 20 20 4f 53 54 52 41 43  ONLY;.    OSTRAC
c29b0 45 31 28 20 22 4f 50 45 4e 20 72 65 61 64 20 6f  E1( "OPEN read o
c29c0 6e 6c 79 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20  nly\n" );.  }.. 
c29d0 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c   if( flags & SQL
c29e0 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20  ITE_OPEN_CREATE 
c29f0 29 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 46 6c 61  ){.    ulOpenFla
c2a00 67 73 20 7c 3d 20 4f 50 45 4e 5f 41 43 54 49 4f  gs |= OPEN_ACTIO
c2a10 4e 5f 4f 50 45 4e 5f 49 46 5f 45 58 49 53 54 53  N_OPEN_IF_EXISTS
c2a20 20 7c 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 43   | OPEN_ACTION_C
c2a30 52 45 41 54 45 5f 49 46 5f 4e 45 57 3b 0a 20 20  REATE_IF_NEW;.  
c2a40 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45    OSTRACE1( "OPE
c2a50 4e 20 6f 70 65 6e 20 6e 65 77 2f 63 72 65 61 74  N open new/creat
c2a60 65 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b  e\n" );.  }else{
c2a70 0a 20 20 20 20 75 6c 4f 70 65 6e 46 6c 61 67 73  .    ulOpenFlags
c2a80 20 7c 3d 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f   |= OPEN_ACTION_
c2a90 4f 50 45 4e 5f 49 46 5f 45 58 49 53 54 53 20 7c  OPEN_IF_EXISTS |
c2aa0 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 46 41 49   OPEN_ACTION_FAI
c2ab0 4c 5f 49 46 5f 4e 45 57 3b 0a 20 20 20 20 4f 53  L_IF_NEW;.    OS
c2ac0 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 6f 70  TRACE1( "OPEN op
c2ad0 65 6e 20 65 78 69 73 74 69 6e 67 5c 6e 22 20 29  en existing\n" )
c2ae0 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61  ;.  }..  if( fla
c2af0 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs & SQLITE_OPEN
c2b00 5f 4d 41 49 4e 5f 44 42 20 29 7b 0a 20 20 20 20  _MAIN_DB ){.    
c2b10 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50  ulOpenMode |= OP
c2b20 45 4e 5f 53 48 41 52 45 5f 44 45 4e 59 4e 4f 4e  EN_SHARE_DENYNON
c2b30 45 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28  E;.    OSTRACE1(
c2b40 20 22 4f 50 45 4e 20 73 68 61 72 65 20 72 65 61   "OPEN share rea
c2b50 64 2f 77 72 69 74 65 5c 6e 22 20 29 3b 0a 20 20  d/write\n" );.  
c2b60 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6c 4f 70 65  }else{.    ulOpe
c2b70 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 53 48  nMode |= OPEN_SH
c2b80 41 52 45 5f 44 45 4e 59 57 52 49 54 45 3b 0a 20  ARE_DENYWRITE;. 
c2b90 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50     OSTRACE1( "OP
c2ba0 45 4e 20 73 68 61 72 65 20 72 65 61 64 20 6f 6e  EN share read on
c2bb0 6c 79 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20  ly\n" );.  }..  
c2bc0 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49  if( flags & SQLI
c2bd0 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e  TE_OPEN_DELETEON
c2be0 43 4c 4f 53 45 20 29 7b 0a 20 20 20 20 63 68 61  CLOSE ){.    cha
c2bf0 72 20 70 61 74 68 55 74 66 38 5b 43 43 48 4d 41  r pathUtf8[CCHMA
c2c00 58 50 41 54 48 5d 3b 0a 23 69 66 64 65 66 20 4e  XPATH];.#ifdef N
c2c10 44 45 42 55 47 20 2f 2a 20 77 68 65 6e 20 64 65  DEBUG /* when de
c2c20 62 75 67 67 69 6e 67 20 77 65 20 77 61 6e 74 20  bugging we want 
c2c30 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20  to make sure it 
c2c40 69 73 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20  is deleted */.  
c2c50 20 20 75 6c 46 69 6c 65 41 74 74 72 69 62 75 74    ulFileAttribut
c2c60 65 20 3d 20 46 49 4c 45 5f 48 49 44 44 45 4e 3b  e = FILE_HIDDEN;
c2c70 0a 23 65 6e 64 69 66 0a 20 20 20 20 6f 73 32 46  .#endif.    os2F
c2c80 75 6c 6c 50 61 74 68 6e 61 6d 65 28 20 70 56 66  ullPathname( pVf
c2c90 73 2c 20 7a 4e 61 6d 65 2c 20 43 43 48 4d 41 58  s, zName, CCHMAX
c2ca0 50 41 54 48 2c 20 70 61 74 68 55 74 66 38 20 29  PATH, pathUtf8 )
c2cb0 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74  ;.    pFile->pat
c2cc0 68 54 6f 44 65 6c 20 3d 20 63 6f 6e 76 65 72 74  hToDel = convert
c2cd0 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 70 61  Utf8PathToCp( pa
c2ce0 74 68 55 74 66 38 20 29 3b 0a 20 20 20 20 4f 53  thUtf8 );.    OS
c2cf0 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 68 69  TRACE1( "OPEN hi
c2d00 64 64 65 6e 2f 64 65 6c 65 74 65 20 6f 6e 20 63  dden/delete on c
c2d10 6c 6f 73 65 20 66 69 6c 65 20 61 74 74 72 69 62  lose file attrib
c2d20 75 74 65 73 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c  utes\n" );.  }el
c2d30 73 65 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70  se{.    pFile->p
c2d40 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b  athToDel = NULL;
c2d50 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22  .    OSTRACE1( "
c2d60 4f 50 45 4e 20 6e 6f 72 6d 61 6c 20 66 69 6c 65  OPEN normal file
c2d70 20 61 74 74 72 69 62 75 74 65 5c 6e 22 20 29 3b   attribute\n" );
c2d80 0a 20 20 7d 0a 0a 20 20 2f 2a 20 61 6c 77 61 79  .  }..  /* alway
c2d90 73 20 6f 70 65 6e 20 69 6e 20 72 61 6e 64 6f 6d  s open in random
c2da0 20 61 63 63 65 73 73 20 6d 6f 64 65 20 66 6f 72   access mode for
c2db0 20 70 6f 73 73 69 62 6c 79 20 62 65 74 74 65 72   possibly better
c2dc0 20 73 70 65 65 64 20 2a 2f 0a 20 20 75 6c 4f 70   speed */.  ulOp
c2dd0 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 46  enMode |= OPEN_F
c2de0 4c 41 47 53 5f 52 41 4e 44 4f 4d 3b 0a 20 20 75  LAGS_RANDOM;.  u
c2df0 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45  lOpenMode |= OPE
c2e00 4e 5f 46 4c 41 47 53 5f 46 41 49 4c 5f 4f 4e 5f  N_FLAGS_FAIL_ON_
c2e10 45 52 52 4f 52 3b 0a 20 20 75 6c 4f 70 65 6e 4d  ERROR;.  ulOpenM
c2e20 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47  ode |= OPEN_FLAG
c2e30 53 5f 4e 4f 49 4e 48 45 52 49 54 3b 0a 0a 20 20  S_NOINHERIT;..  
c2e40 7a 4e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72  zNameCp = conver
c2e50 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a  tUtf8PathToCp( z
c2e60 4e 61 6d 65 20 29 3b 0a 20 20 72 63 20 3d 20 44  Name );.  rc = D
c2e70 6f 73 4f 70 65 6e 28 20 28 50 53 5a 29 7a 4e 61  osOpen( (PSZ)zNa
c2e80 6d 65 43 70 2c 0a 20 20 20 20 20 20 20 20 20 20  meCp,.          
c2e90 20 20 20 20 20 20 26 68 2c 0a 20 20 20 20 20 20        &h,.      
c2ea0 20 20 20 20 20 20 20 20 20 20 26 75 6c 41 63 74            &ulAct
c2eb0 69 6f 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ion,.           
c2ec0 20 20 20 20 20 30 4c 2c 0a 20 20 20 20 20 20 20       0L,.       
c2ed0 20 20 20 20 20 20 20 20 20 75 6c 46 69 6c 65 41           ulFileA
c2ee0 74 74 72 69 62 75 74 65 2c 0a 20 20 20 20 20 20  ttribute,.      
c2ef0 20 20 20 20 20 20 20 20 20 20 75 6c 4f 70 65 6e            ulOpen
c2f00 46 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20  Flags,.         
c2f10 20 20 20 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64         ulOpenMod
c2f20 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e,.             
c2f30 20 20 20 28 50 45 41 4f 50 32 29 4e 55 4c 4c 20     (PEAOP2)NULL 
c2f40 29 3b 0a 20 20 66 72 65 65 28 20 7a 4e 61 6d 65  );.  free( zName
c2f50 43 70 20 29 3b 0a 20 20 69 66 28 20 72 63 20 21  Cp );.  if( rc !
c2f60 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20  = NO_ERROR ){.  
c2f70 20 20 4f 53 54 52 41 43 45 37 28 20 22 4f 50 45    OSTRACE7( "OPE
c2f80 4e 20 49 6e 76 61 6c 69 64 20 68 61 6e 64 6c 65  N Invalid handle
c2f90 20 72 63 3d 25 64 3a 20 7a 4e 61 6d 65 3d 25 73   rc=%d: zName=%s
c2fa0 2c 20 75 6c 41 63 74 69 6f 6e 3d 25 23 6c 78 2c  , ulAction=%#lx,
c2fb0 20 75 6c 41 74 74 72 3d 25 23 6c 78 2c 20 75 6c   ulAttr=%#lx, ul
c2fc0 46 6c 61 67 73 3d 25 23 6c 78 2c 20 75 6c 4d 6f  Flags=%#lx, ulMo
c2fd0 64 65 3d 25 23 6c 78 5c 6e 22 2c 0a 20 20 20 20  de=%#lx\n",.    
c2fe0 20 20 20 20 20 20 20 20 20 20 72 63 2c 20 7a 4e            rc, zN
c2ff0 61 6d 65 2c 20 75 6c 41 63 74 69 6f 6e 2c 20 75  ame, ulAction, u
c3000 6c 46 69 6c 65 41 74 74 72 69 62 75 74 65 2c 20  lFileAttribute, 
c3010 75 6c 4f 70 65 6e 46 6c 61 67 73 2c 20 75 6c 4f  ulOpenFlags, ulO
c3020 70 65 6e 4d 6f 64 65 20 29 3b 0a 20 20 20 20 69  penMode );.    i
c3030 66 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f  f( pFile->pathTo
c3040 44 65 6c 20 29 0a 20 20 20 20 20 20 66 72 65 65  Del ).      free
c3050 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44  ( pFile->pathToD
c3060 65 6c 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d  el );.    pFile-
c3070 3e 70 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c  >pathToDel = NUL
c3080 4c 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73  L;.    if( flags
c3090 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52   & SQLITE_OPEN_R
c30a0 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20 20 20  EADWRITE ){.    
c30b0 20 20 4f 53 54 52 41 43 45 32 28 20 22 4f 50 45    OSTRACE2( "OPE
c30c0 4e 20 25 64 20 49 6e 76 61 6c 69 64 20 68 61 6e  N %d Invalid han
c30d0 64 6c 65 5c 6e 22 2c 20 28 28 66 6c 61 67 73 20  dle\n", ((flags 
c30e0 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45  | SQLITE_OPEN_RE
c30f0 41 44 4f 4e 4c 59 29 20 26 20 7e 53 51 4c 49 54  ADONLY) & ~SQLIT
c3100 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
c3110 29 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  ) );.      retur
c3120 6e 20 6f 73 32 4f 70 65 6e 28 20 70 56 66 73 2c  n os2Open( pVfs,
c3130 20 7a 4e 61 6d 65 2c 20 69 64 2c 0a 20 20 20 20   zName, id,.    
c3140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3150 20 20 28 28 66 6c 61 67 73 20 7c 20 53 51 4c 49    ((flags | SQLI
c3160 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59  TE_OPEN_READONLY
c3170 29 20 26 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e  ) & ~SQLITE_OPEN
c3180 5f 52 45 41 44 57 52 49 54 45 29 2c 0a 20 20 20  _READWRITE),.   
c3190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c31a0 20 20 20 70 4f 75 74 46 6c 61 67 73 20 29 3b 0a     pOutFlags );.
c31b0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
c31c0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
c31d0 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20  ANTOPEN;.    }. 
c31e0 20 7d 0a 0a 20 20 69 66 28 20 70 4f 75 74 46 6c   }..  if( pOutFl
c31f0 61 67 73 20 29 7b 0a 20 20 20 20 2a 70 4f 75 74  ags ){.    *pOut
c3200 46 6c 61 67 73 20 3d 20 66 6c 61 67 73 20 26 20  Flags = flags & 
c3210 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
c3220 57 52 49 54 45 20 3f 20 53 51 4c 49 54 45 5f 4f  WRITE ? SQLITE_O
c3230 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 3a 20  PEN_READWRITE : 
c3240 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
c3250 4f 4e 4c 59 3b 0a 20 20 7d 0a 0a 20 20 70 46 69  ONLY;.  }..  pFi
c3260 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 6f  le->pMethod = &o
c3270 73 32 49 6f 4d 65 74 68 6f 64 3b 0a 20 20 70 46  s2IoMethod;.  pF
c3280 69 6c 65 2d 3e 68 20 3d 20 68 3b 0a 20 20 4f 70  ile->h = h;.  Op
c3290 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20  enCounter(+1);. 
c32a0 20 4f 53 54 52 41 43 45 33 28 20 22 4f 50 45 4e   OSTRACE3( "OPEN
c32b0 20 25 64 20 70 4f 75 74 46 6c 61 67 73 3d 25 64   %d pOutFlags=%d
c32c0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70  \n", pFile->h, p
c32d0 4f 75 74 46 6c 61 67 73 20 29 3b 0a 20 20 72 65  OutFlags );.  re
c32e0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
c32f0 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20  }../*.** Delete 
c3300 74 68 65 20 6e 61 6d 65 64 20 66 69 6c 65 2e 0a  the named file..
c3310 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73  */.static int os
c3320 32 44 65 6c 65 74 65 28 0a 20 20 73 71 6c 69 74  2Delete(.  sqlit
c3330 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20  e3_vfs *pVfs,   
c3340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3350 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e    /* Not used on
c3360 20 6f 73 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20   os2 */.  const 
c3370 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c  char *zFilename,
c3380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3390 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65   /* Name of file
c33a0 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20   to delete */.  
c33b0 69 6e 74 20 73 79 6e 63 44 69 72 20 20 20 20 20  int syncDir     
c33c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c33d0 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73         /* Not us
c33e0 65 64 20 6f 6e 20 6f 73 32 20 2a 2f 0a 29 7b 0a  ed on os2 */.){.
c33f0 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f    APIRET rc = NO
c3400 5f 45 52 52 4f 52 3b 0a 20 20 63 68 61 72 20 2a  _ERROR;.  char *
c3410 7a 46 69 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f  zFilenameCp = co
c3420 6e 76 65 72 74 55 74 66 38 50 61 74 68 54 6f 43  nvertUtf8PathToC
c3430 70 28 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a  p( zFilename );.
c3440 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
c3450 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  r( return SQLITE
c3460 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 20 29 3b  _IOERR_DELETE );
c3470 0a 20 20 72 63 20 3d 20 44 6f 73 44 65 6c 65 74  .  rc = DosDelet
c3480 65 28 20 28 50 53 5a 29 7a 46 69 6c 65 6e 61 6d  e( (PSZ)zFilenam
c3490 65 43 70 20 29 3b 0a 20 20 66 72 65 65 28 20 7a  eCp );.  free( z
c34a0 46 69 6c 65 6e 61 6d 65 43 70 20 29 3b 0a 20 20  FilenameCp );.  
c34b0 4f 53 54 52 41 43 45 32 28 20 22 44 45 4c 45 54  OSTRACE2( "DELET
c34c0 45 20 5c 22 25 73 5c 22 5c 6e 22 2c 20 7a 46 69  E \"%s\"\n", zFi
c34d0 6c 65 6e 61 6d 65 20 29 3b 0a 20 20 72 65 74 75  lename );.  retu
c34e0 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f  rn rc == NO_ERRO
c34f0 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20  R ? SQLITE_OK : 
c3500 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c  SQLITE_IOERR_DEL
c3510 45 54 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68  ETE;.}../*.** Ch
c3520 65 63 6b 20 74 68 65 20 65 78 69 73 74 61 6e 63  eck the existanc
c3530 65 20 61 6e 64 20 73 74 61 74 75 73 20 6f 66 20  e and status of 
c3540 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  a file..*/.stati
c3550 63 20 69 6e 74 20 6f 73 32 41 63 63 65 73 73 28  c int os2Access(
c3560 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
c3570 70 56 66 73 2c 20 20 20 20 20 20 20 20 2f 2a 20  pVfs,        /* 
c3580 4e 6f 74 20 75 73 65 64 20 6f 6e 20 6f 73 32 20  Not used on os2 
c3590 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
c35a0 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 2f  *zFilename,    /
c35b0 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74  * Name of file t
c35c0 6f 20 63 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74  o check */.  int
c35d0 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20   flags,         
c35e0 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f         /* Type o
c35f0 66 20 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 6f  f test to make o
c3600 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20  n this file */. 
c3610 20 69 6e 74 20 2a 70 4f 75 74 20 20 20 20 20 20   int *pOut      
c3620 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72             /* Wr
c3630 69 74 65 20 72 65 73 75 6c 74 73 20 68 65 72 65  ite results here
c3640 20 2a 2f 0a 29 7b 0a 20 20 46 49 4c 45 53 54 41   */.){.  FILESTA
c3650 54 55 53 33 20 66 73 74 73 33 43 6f 6e 66 69 67  TUS3 fsts3Config
c3660 49 6e 66 6f 3b 0a 20 20 41 50 49 52 45 54 20 72  Info;.  APIRET r
c3670 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20  c = NO_ERROR;.  
c3680 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 43  char *zFilenameC
c3690 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50  p = convertUtf8P
c36a0 61 74 68 54 6f 43 70 28 20 7a 46 69 6c 65 6e 61  athToCp( zFilena
c36b0 6d 65 20 29 3b 0a 0a 20 20 6d 65 6d 73 65 74 28  me );..  memset(
c36c0 20 26 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66   &fsts3ConfigInf
c36d0 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66 73 74  o, 0, sizeof(fst
c36e0 73 33 43 6f 6e 66 69 67 49 6e 66 6f 29 20 29 3b  s3ConfigInfo) );
c36f0 0a 20 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79  .  rc = DosQuery
c3700 50 61 74 68 49 6e 66 6f 28 20 28 50 53 5a 29 7a  PathInfo( (PSZ)z
c3710 46 69 6c 65 6e 61 6d 65 43 70 2c 20 46 49 4c 5f  FilenameCp, FIL_
c3720 53 54 41 4e 44 41 52 44 2c 0a 20 20 20 20 20 20  STANDARD,.      
c3730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3740 20 20 20 26 66 73 74 73 33 43 6f 6e 66 69 67 49     &fsts3ConfigI
c3750 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 46 49 4c 45  nfo, sizeof(FILE
c3760 53 54 41 54 55 53 33 29 20 29 3b 0a 20 20 66 72  STATUS3) );.  fr
c3770 65 65 28 20 7a 46 69 6c 65 6e 61 6d 65 43 70 20  ee( zFilenameCp 
c3780 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 20 22  );.  OSTRACE4( "
c3790 41 43 43 45 53 53 20 66 73 74 73 33 43 6f 6e 66  ACCESS fsts3Conf
c37a0 69 67 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 3d  igInfo.attrFile=
c37b0 25 64 20 66 6c 61 67 73 3d 25 64 20 72 63 3d 25  %d flags=%d rc=%
c37c0 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  d\n",.          
c37d0 20 20 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66    fsts3ConfigInf
c37e0 6f 2e 61 74 74 72 46 69 6c 65 2c 20 66 6c 61 67  o.attrFile, flag
c37f0 73 2c 20 72 63 20 29 3b 0a 20 20 73 77 69 74 63  s, rc );.  switc
c3800 68 28 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20  h( flags ){.    
c3810 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45  case SQLITE_ACCE
c3820 53 53 5f 52 45 41 44 3a 0a 20 20 20 20 63 61 73  SS_READ:.    cas
c3830 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f  e SQLITE_ACCESS_
c3840 45 58 49 53 54 53 3a 0a 20 20 20 20 20 20 72 63  EXISTS:.      rc
c3850 20 3d 20 28 72 63 20 3d 3d 20 4e 4f 5f 45 52 52   = (rc == NO_ERR
c3860 4f 52 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41  OR);.      OSTRA
c3870 43 45 33 28 20 22 41 43 43 45 53 53 20 25 73 20  CE3( "ACCESS %s 
c3880 61 63 63 65 73 73 20 6f 66 20 72 65 61 64 20 61  access of read a
c3890 6e 64 20 65 78 69 73 74 73 20 20 72 63 3d 25 64  nd exists  rc=%d
c38a0 5c 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20  \n", zFilename, 
c38b0 72 63 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61  rc );.      brea
c38c0 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  k;.    case SQLI
c38d0 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52  TE_ACCESS_READWR
c38e0 49 54 45 3a 0a 20 20 20 20 20 20 72 63 20 3d 20  ITE:.      rc = 
c38f0 28 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29  (rc == NO_ERROR)
c3900 20 26 26 20 28 20 28 66 73 74 73 33 43 6f 6e 66   && ( (fsts3Conf
c3910 69 67 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 20  igInfo.attrFile 
c3920 26 20 46 49 4c 45 5f 52 45 41 44 4f 4e 4c 59 29  & FILE_READONLY)
c3930 20 3d 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 4f   == 0 );.      O
c3940 53 54 52 41 43 45 33 28 20 22 41 43 43 45 53 53  STRACE3( "ACCESS
c3950 20 25 73 20 61 63 63 65 73 73 20 6f 66 20 72 65   %s access of re
c3960 61 64 2f 77 72 69 74 65 20 20 72 63 3d 25 64 5c  ad/write  rc=%d\
c3970 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 72  n", zFilename, r
c3980 63 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  c );.      break
c3990 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20  ;.    default:. 
c39a0 20 20 20 20 20 61 73 73 65 72 74 28 20 21 22 49       assert( !"I
c39b0 6e 76 61 6c 69 64 20 66 6c 61 67 73 20 61 72 67  nvalid flags arg
c39c0 75 6d 65 6e 74 22 20 29 3b 0a 20 20 7d 0a 20 20  ument" );.  }.  
c39d0 2a 70 4f 75 74 20 3d 20 72 63 3b 0a 20 20 72 65  *pOut = rc;.  re
c39e0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
c39f0 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  }...#ifndef SQLI
c3a00 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54  TE_OMIT_LOAD_EXT
c3a10 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74  ENSION./*.** Int
c3a20 65 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e  erfaces for open
c3a30 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62  ing a shared lib
c3a40 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e  rary, finding en
c3a50 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69  try points.** wi
c3a60 74 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20  thin the shared 
c3a70 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f  library, and clo
c3a80 73 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20  sing the shared 
c3a90 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a  library..*/./*.*
c3aa0 2a 20 49 6e 74 65 72 66 61 63 65 73 20 66 6f 72  * Interfaces for
c3ab0 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 72 65   opening a share
c3ac0 64 20 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 69  d library, findi
c3ad0 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a  ng entry points.
c3ae0 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 73 68  ** within the sh
c3af0 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 61 6e  ared library, an
c3b00 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 68  d closing the sh
c3b10 61 72 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f  ared library..*/
c3b20 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6f 73  .static void *os
c3b30 32 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f  2DlOpen(sqlite3_
c3b40 76 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74  vfs *pVfs, const
c3b50 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
c3b60 29 7b 0a 20 20 55 43 48 41 52 20 6c 6f 61 64 45  ){.  UCHAR loadE
c3b70 72 72 5b 32 35 36 5d 3b 0a 20 20 48 4d 4f 44 55  rr[256];.  HMODU
c3b80 4c 45 20 68 6d 6f 64 3b 0a 20 20 41 50 49 52 45  LE hmod;.  APIRE
c3b90 54 20 72 63 3b 0a 20 20 63 68 61 72 20 2a 7a 46  T rc;.  char *zF
c3ba0 69 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f 6e 76  ilenameCp = conv
c3bb0 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 28  ertUtf8PathToCp(
c3bc0 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 72 63  zFilename);.  rc
c3bd0 20 3d 20 44 6f 73 4c 6f 61 64 4d 6f 64 75 6c 65   = DosLoadModule
c3be0 28 28 50 53 5a 29 6c 6f 61 64 45 72 72 2c 20 73  ((PSZ)loadErr, s
c3bf0 69 7a 65 6f 66 28 6c 6f 61 64 45 72 72 29 2c 20  izeof(loadErr), 
c3c00 7a 46 69 6c 65 6e 61 6d 65 43 70 2c 20 26 68 6d  zFilenameCp, &hm
c3c10 6f 64 29 3b 0a 20 20 66 72 65 65 28 7a 46 69 6c  od);.  free(zFil
c3c20 65 6e 61 6d 65 43 70 29 3b 0a 20 20 72 65 74 75  enameCp);.  retu
c3c30 72 6e 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f  rn rc != NO_ERRO
c3c40 52 20 3f 20 30 20 3a 20 28 76 6f 69 64 2a 29 68  R ? 0 : (void*)h
c3c50 6d 6f 64 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 41 20 6e  mod;.}./*.** A n
c3c60 6f 2d 6f 70 20 73 69 6e 63 65 20 74 68 65 20 65  o-op since the e
c3c70 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74  rror code is ret
c3c80 75 72 6e 65 64 20 6f 6e 20 74 68 65 20 44 6f 73  urned on the Dos
c3c90 4c 6f 61 64 4d 6f 64 75 6c 65 20 63 61 6c 6c 2e  LoadModule call.
c3ca0 0a 2a 2a 20 6f 73 32 44 6c 6f 70 65 6e 20 72 65  .** os2Dlopen re
c3cb0 74 75 72 6e 73 20 7a 65 72 6f 20 69 66 20 44 6f  turns zero if Do
c3cc0 73 4c 6f 61 64 4d 6f 64 75 6c 65 20 69 73 20 6e  sLoadModule is n
c3cd0 6f 74 20 73 75 63 63 65 73 73 66 75 6c 2e 0a 2a  ot successful..*
c3ce0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73  /.static void os
c3cf0 32 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33  2DlError(sqlite3
c3d00 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20  _vfs *pVfs, int 
c3d10 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66  nBuf, char *zBuf
c3d20 4f 75 74 29 7b 0a 2f 2a 20 6e 6f 2d 6f 70 20 2a  Out){./* no-op *
c3d30 2f 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  /.}.static void 
c3d40 2a 6f 73 32 44 6c 53 79 6d 28 73 71 6c 69 74 65  *os2DlSym(sqlite
c3d50 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69  3_vfs *pVfs, voi
c3d60 64 20 2a 70 48 61 6e 64 6c 65 2c 20 63 6f 6e 73  d *pHandle, cons
c3d70 74 20 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c 29  t char *zSymbol)
c3d80 7b 0a 20 20 50 46 4e 20 70 66 6e 3b 0a 20 20 41  {.  PFN pfn;.  A
c3d90 50 49 52 45 54 20 72 63 3b 0a 20 20 72 63 20 3d  PIRET rc;.  rc =
c3da0 20 44 6f 73 51 75 65 72 79 50 72 6f 63 41 64 64   DosQueryProcAdd
c3db0 72 28 28 48 4d 4f 44 55 4c 45 29 70 48 61 6e 64  r((HMODULE)pHand
c3dc0 6c 65 2c 20 30 4c 2c 20 7a 53 79 6d 62 6f 6c 2c  le, 0L, zSymbol,
c3dd0 20 26 70 66 6e 29 3b 0a 20 20 69 66 28 20 72 63   &pfn);.  if( rc
c3de0 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a   != NO_ERROR ){.
c3df0 20 20 20 20 2f 2a 20 69 66 20 74 68 65 20 73 79      /* if the sy
c3e00 6d 62 6f 6c 20 69 74 73 65 6c 66 20 77 61 73 20  mbol itself was 
c3e10 6e 6f 74 20 66 6f 75 6e 64 2c 20 73 65 61 72 63  not found, searc
c3e20 68 20 61 67 61 69 6e 20 66 6f 72 20 74 68 65 20  h again for the 
c3e30 73 61 6d 65 0a 20 20 20 20 20 2a 20 73 79 6d 62  same.     * symb
c3e40 6f 6c 20 77 69 74 68 20 61 6e 20 65 78 74 72 61  ol with an extra
c3e50 20 75 6e 64 65 72 73 63 6f 72 65 2c 20 74 68 61   underscore, tha
c3e60 74 20 6d 69 67 68 74 20 62 65 20 6e 65 65 64 65  t might be neede
c3e70 64 20 64 65 70 65 6e 64 69 6e 67 0a 20 20 20 20  d depending.    
c3e80 20 2a 20 6f 6e 20 74 68 65 20 63 61 6c 6c 69 6e   * on the callin
c3e90 67 20 63 6f 6e 76 65 6e 74 69 6f 6e 20 2a 2f 0a  g convention */.
c3ea0 20 20 20 20 63 68 61 72 20 5f 7a 53 79 6d 62 6f      char _zSymbo
c3eb0 6c 5b 32 35 36 5d 20 3d 20 22 5f 22 3b 0a 20 20  l[256] = "_";.  
c3ec0 20 20 73 74 72 6e 63 61 74 28 5f 7a 53 79 6d 62    strncat(_zSymb
c3ed0 6f 6c 2c 20 7a 53 79 6d 62 6f 6c 2c 20 32 35 35  ol, zSymbol, 255
c3ee0 29 3b 0a 20 20 20 20 72 63 20 3d 20 44 6f 73 51  );.    rc = DosQ
c3ef0 75 65 72 79 50 72 6f 63 41 64 64 72 28 28 48 4d  ueryProcAddr((HM
c3f00 4f 44 55 4c 45 29 70 48 61 6e 64 6c 65 2c 20 30  ODULE)pHandle, 0
c3f10 4c 2c 20 5f 7a 53 79 6d 62 6f 6c 2c 20 26 70 66  L, _zSymbol, &pf
c3f20 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  n);.  }.  return
c3f30 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20   rc != NO_ERROR 
c3f40 3f 20 30 20 3a 20 28 76 6f 69 64 2a 29 70 66 6e  ? 0 : (void*)pfn
c3f50 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  ;.}.static void 
c3f60 6f 73 32 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74  os2DlClose(sqlit
c3f70 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f  e3_vfs *pVfs, vo
c3f80 69 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20  id *pHandle){.  
c3f90 44 6f 73 46 72 65 65 4d 6f 64 75 6c 65 28 28 48  DosFreeModule((H
c3fa0 4d 4f 44 55 4c 45 29 70 48 61 6e 64 6c 65 29 3b  MODULE)pHandle);
c3fb0 0a 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 53  .}.#else /* if S
c3fc0 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f  QLITE_OMIT_LOAD_
c3fd0 45 58 54 45 4e 53 49 4f 4e 20 69 73 20 64 65 66  EXTENSION is def
c3fe0 69 6e 65 64 3a 20 2a 2f 0a 20 20 23 64 65 66 69  ined: */.  #defi
c3ff0 6e 65 20 6f 73 32 44 6c 4f 70 65 6e 20 30 0a 20  ne os2DlOpen 0. 
c4000 20 23 64 65 66 69 6e 65 20 6f 73 32 44 6c 45 72   #define os2DlEr
c4010 72 6f 72 20 30 0a 20 20 23 64 65 66 69 6e 65 20  ror 0.  #define 
c4020 6f 73 32 44 6c 53 79 6d 20 30 0a 20 20 23 64 65  os2DlSym 0.  #de
c4030 66 69 6e 65 20 6f 73 32 44 6c 43 6c 6f 73 65 20  fine os2DlClose 
c4040 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  0.#endif.../*.**
c4050 20 57 72 69 74 65 20 75 70 20 74 6f 20 6e 42 75   Write up to nBu
c4060 66 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f  f bytes of rando
c4070 6d 6e 65 73 73 20 69 6e 74 6f 20 7a 42 75 66 2e  mness into zBuf.
c4080 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  .*/.static int o
c4090 73 32 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c  s2Randomness(sql
c40a0 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
c40b0 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a  int nBuf, char *
c40c0 7a 42 75 66 20 29 7b 0a 20 20 69 6e 74 20 6e 20  zBuf ){.  int n 
c40d0 3d 20 30 3b 0a 23 69 66 20 64 65 66 69 6e 65 64  = 0;.#if defined
c40e0 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 20 20  (SQLITE_TEST).  
c40f0 6e 20 3d 20 6e 42 75 66 3b 0a 20 20 6d 65 6d 73  n = nBuf;.  mems
c4100 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e 42 75 66  et(zBuf, 0, nBuf
c4110 29 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20 73  );.#else.  int s
c4120 69 7a 65 6f 66 55 4c 6f 6e 67 20 3d 20 73 69 7a  izeofULong = siz
c4130 65 6f 66 28 55 4c 4f 4e 47 29 3b 0a 20 20 69 66  eof(ULONG);.  if
c4140 28 20 28 69 6e 74 29 73 69 7a 65 6f 66 28 44 41  ( (int)sizeof(DA
c4150 54 45 54 49 4d 45 29 20 3c 3d 20 6e 42 75 66 20  TETIME) <= nBuf 
c4160 2d 20 6e 20 29 7b 0a 20 20 20 20 44 41 54 45 54  - n ){.    DATET
c4170 49 4d 45 20 78 3b 0a 20 20 20 20 44 6f 73 47 65  IME x;.    DosGe
c4180 74 44 61 74 65 54 69 6d 65 28 26 78 29 3b 0a 20  tDateTime(&x);. 
c4190 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b     memcpy(&zBuf[
c41a0 6e 5d 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 78  n], &x, sizeof(x
c41b0 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a  ));.    n += siz
c41c0 65 6f 66 28 78 29 3b 0a 20 20 7d 0a 0a 20 20 69  eof(x);.  }..  i
c41d0 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c  f( sizeofULong <
c41e0 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20  = nBuf - n ){.  
c41f0 20 20 50 50 49 42 20 70 70 69 62 3b 0a 20 20 20    PPIB ppib;.   
c4200 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b   DosGetInfoBlock
c4210 73 28 4e 55 4c 4c 2c 20 26 70 70 69 62 29 3b 0a  s(NULL, &ppib);.
c4220 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66      memcpy(&zBuf
c4230 5b 6e 5d 2c 20 26 70 70 69 62 2d 3e 70 69 62 5f  [n], &ppib->pib_
c4240 75 6c 70 69 64 2c 20 73 69 7a 65 6f 66 55 4c 6f  ulpid, sizeofULo
c4250 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69  ng);.    n += si
c4260 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a  zeofULong;.  }..
c4270 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e    if( sizeofULon
c4280 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b  g <= nBuf - n ){
c4290 0a 20 20 20 20 50 54 49 42 20 70 74 69 62 3b 0a  .    PTIB ptib;.
c42a0 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c      DosGetInfoBl
c42b0 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c  ocks(&ptib, NULL
c42c0 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a  );.    memcpy(&z
c42d0 42 75 66 5b 6e 5d 2c 20 26 70 74 69 62 2d 3e 74  Buf[n], &ptib->t
c42e0 69 62 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f 75  ib_ptib2->tib2_u
c42f0 6c 74 69 64 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e  ltid, sizeofULon
c4300 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a  g);.    n += siz
c4310 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a 20  eofULong;.  }.. 
c4320 20 2f 2a 20 69 66 20 77 65 20 73 74 69 6c 6c 20   /* if we still 
c4330 68 61 76 65 6e 27 74 20 66 69 6c 6c 65 64 20 74  haven't filled t
c4340 68 65 20 62 75 66 66 65 72 20 79 65 74 20 74 68  he buffer yet th
c4350 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 77 69 6c 6c  e following will
c4360 20 2a 2f 0a 20 20 2f 2a 20 67 72 61 62 20 65 76   */.  /* grab ev
c4370 65 72 79 74 68 69 6e 67 20 6f 6e 63 65 20 69 6e  erything once in
c4380 73 74 65 61 64 20 6f 66 20 6d 61 6b 69 6e 67 20  stead of making 
c4390 73 65 76 65 72 61 6c 20 63 61 6c 6c 73 20 66 6f  several calls fo
c43a0 72 20 61 20 73 69 6e 67 6c 65 20 69 74 65 6d 20  r a single item 
c43b0 2a 2f 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 55  */.  if( sizeofU
c43c0 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e  Long <= nBuf - n
c43d0 20 29 7b 0a 20 20 20 20 55 4c 4f 4e 47 20 75 6c   ){.    ULONG ul
c43e0 53 79 73 49 6e 66 6f 5b 51 53 56 5f 4d 41 58 5d  SysInfo[QSV_MAX]
c43f0 3b 0a 20 20 20 20 44 6f 73 51 75 65 72 79 53 79  ;.    DosQuerySy
c4400 73 49 6e 66 6f 28 31 4c 2c 20 51 53 56 5f 4d 41  sInfo(1L, QSV_MA
c4410 58 2c 20 75 6c 53 79 73 49 6e 66 6f 2c 20 73 69  X, ulSysInfo, si
c4420 7a 65 6f 66 55 4c 6f 6e 67 20 2a 20 51 53 56 5f  zeofULong * QSV_
c4430 4d 41 58 29 3b 0a 0a 20 20 20 20 6d 65 6d 63 70  MAX);..    memcp
c4440 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53  y(&zBuf[n], &ulS
c4450 79 73 49 6e 66 6f 5b 51 53 56 5f 4d 53 5f 43 4f  ysInfo[QSV_MS_CO
c4460 55 4e 54 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66  UNT - 1], sizeof
c4470 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d  ULong);.    n +=
c4480 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 0a 20   sizeofULong;.. 
c4490 20 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f     if( sizeofULo
c44a0 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29  ng <= nBuf - n )
c44b0 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26  {.      memcpy(&
c44c0 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49  zBuf[n], &ulSysI
c44d0 6e 66 6f 5b 51 53 56 5f 54 49 4d 45 52 5f 49 4e  nfo[QSV_TIMER_IN
c44e0 54 45 52 56 41 4c 20 2d 20 31 5d 2c 20 73 69 7a  TERVAL - 1], siz
c44f0 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20  eofULong);.     
c4500 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e   n += sizeofULon
c4510 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  g;.    }.    if(
c4520 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20   sizeofULong <= 
c4530 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20  nBuf - n ){.    
c4540 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e    memcpy(&zBuf[n
c4550 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51 53  ], &ulSysInfo[QS
c4560 56 5f 54 49 4d 45 5f 4c 4f 57 20 2d 20 31 5d 2c  V_TIME_LOW - 1],
c4570 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20   sizeofULong);. 
c4580 20 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66       n += sizeof
c4590 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20 20 20  ULong;.    }.   
c45a0 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67   if( sizeofULong
c45b0 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a   <= nBuf - n ){.
c45c0 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42        memcpy(&zB
c45d0 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66  uf[n], &ulSysInf
c45e0 6f 5b 51 53 56 5f 54 49 4d 45 5f 48 49 47 48 20  o[QSV_TIME_HIGH 
c45f0 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e  - 1], sizeofULon
c4600 67 29 3b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 73  g);.      n += s
c4610 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20 20  izeofULong;.    
c4620 7d 0a 20 20 20 20 69 66 28 20 73 69 7a 65 6f 66  }.    if( sizeof
c4630 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20  ULong <= nBuf - 
c4640 6e 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70  n ){.      memcp
c4650 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53  y(&zBuf[n], &ulS
c4660 79 73 49 6e 66 6f 5b 51 53 56 5f 54 4f 54 41 56  ysInfo[QSV_TOTAV
c4670 41 49 4c 4d 45 4d 20 2d 20 31 5d 2c 20 73 69 7a  AILMEM - 1], siz
c4680 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20  eofULong);.     
c4690 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e   n += sizeofULon
c46a0 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e  g;.    }.  }.#en
c46b0 64 69 66 0a 0a 20 20 72 65 74 75 72 6e 20 6e 3b  dif..  return n;
c46c0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20  .}../*.** Sleep 
c46d0 66 6f 72 20 61 20 6c 69 74 74 6c 65 20 77 68 69  for a little whi
c46e0 6c 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20  le.  Return the 
c46f0 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20 73  amount of time s
c4700 6c 65 70 74 2e 0a 2a 2a 20 54 68 65 20 61 72 67  lept..** The arg
c4710 75 6d 65 6e 74 20 69 73 20 74 68 65 20 6e 75 6d  ument is the num
c4720 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f  ber of microseco
c4730 6e 64 73 20 77 65 20 77 61 6e 74 20 74 6f 20 73  nds we want to s
c4740 6c 65 65 70 2e 0a 2a 2a 20 54 68 65 20 72 65 74  leep..** The ret
c4750 75 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65  urn value is the
c4760 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f   number of micro
c4770 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70  seconds of sleep
c4780 20 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 72 65 71   actually.** req
c4790 75 65 73 74 65 64 20 66 72 6f 6d 20 74 68 65 20  uested from the 
c47a0 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61  underlying opera
c47b0 74 69 6e 67 20 73 79 73 74 65 6d 2c 20 61 20 6e  ting system, a n
c47c0 75 6d 62 65 72 20 77 68 69 63 68 0a 2a 2a 20 6d  umber which.** m
c47d0 69 67 68 74 20 62 65 20 67 72 65 61 74 65 72 20  ight be greater 
c47e0 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f  than or equal to
c47f0 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2c 20 62   the argument, b
c4800 75 74 20 6e 6f 74 20 6c 65 73 73 0a 2a 2a 20 74  ut not less.** t
c4810 68 61 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74  han the argument
c4820 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
c4830 6f 73 32 53 6c 65 65 70 28 20 73 71 6c 69 74 65  os2Sleep( sqlite
c4840 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74  3_vfs *pVfs, int
c4850 20 6d 69 63 72 6f 73 65 63 20 29 7b 0a 20 20 44   microsec ){.  D
c4860 6f 73 53 6c 65 65 70 28 20 28 6d 69 63 72 6f 73  osSleep( (micros
c4870 65 63 2f 31 30 30 30 29 20 29 3b 0a 20 20 72 65  ec/1000) );.  re
c4880 74 75 72 6e 20 6d 69 63 72 6f 73 65 63 3b 0a 7d  turn microsec;.}
c4890 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
c48a0 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20  owing variable, 
c48b0 69 66 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d  if set to a non-
c48c0 7a 65 72 6f 20 76 61 6c 75 65 2c 20 62 65 63 6f  zero value, beco
c48d0 6d 65 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a  mes the result.*
c48e0 2a 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20  * returned from 
c48f0 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74  sqlite3OsCurrent
c4900 54 69 6d 65 28 29 2e 20 20 54 68 69 73 20 69 73  Time().  This is
c4910 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e   used for testin
c4920 67 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  g..*/.#ifdef SQL
c4930 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f  ITE_TEST.SQLITE_
c4940 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
c4950 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30  current_time = 0
c4960 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ;.#endif../*.** 
c4970 46 69 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74  Find the current
c4980 20 74 69 6d 65 20 28 69 6e 20 55 6e 69 76 65 72   time (in Univer
c4990 73 61 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64 20  sal Coordinated 
c49a0 54 69 6d 65 29 2e 20 20 57 72 69 74 65 20 74 68  Time).  Write th
c49b0 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 74 69 6d  e.** current tim
c49c0 65 20 61 6e 64 20 64 61 74 65 20 61 73 20 61 20  e and date as a 
c49d0 4a 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65  Julian Day numbe
c49e0 72 20 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61 6e  r into *prNow an
c49f0 64 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20 20  d.** return 0.  
c4a00 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20  Return 1 if the 
c4a10 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 63 61  time and date ca
c4a20 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a  nnot be found..*
c4a30 2f 0a 69 6e 74 20 6f 73 32 43 75 72 72 65 6e 74  /.int os2Current
c4a40 54 69 6d 65 28 20 73 71 6c 69 74 65 33 5f 76 66  Time( sqlite3_vf
c4a50 73 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65 20  s *pVfs, double 
c4a60 2a 70 72 4e 6f 77 20 29 7b 0a 20 20 64 6f 75 62  *prNow ){.  doub
c4a70 6c 65 20 6e 6f 77 3b 0a 20 20 53 48 4f 52 54 20  le now;.  SHORT 
c4a80 6d 69 6e 75 74 65 3b 20 2f 2a 20 6e 65 65 64 73  minute; /* needs
c4a90 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 63   to be able to c
c4aa0 6f 70 65 20 77 69 74 68 20 6e 65 67 61 74 69 76  ope with negativ
c4ab0 65 20 74 69 6d 65 7a 6f 6e 65 20 6f 66 66 73 65  e timezone offse
c4ac0 74 20 2a 2f 0a 20 20 55 53 48 4f 52 54 20 73 65  t */.  USHORT se
c4ad0 63 6f 6e 64 2c 20 68 6f 75 72 2c 0a 20 20 20 20  cond, hour,.    
c4ae0 20 20 20 20 20 64 61 79 2c 20 6d 6f 6e 74 68 2c       day, month,
c4af0 20 79 65 61 72 3b 0a 20 20 44 41 54 45 54 49 4d   year;.  DATETIM
c4b00 45 20 64 74 3b 0a 20 20 44 6f 73 47 65 74 44 61  E dt;.  DosGetDa
c4b10 74 65 54 69 6d 65 28 20 26 64 74 20 29 3b 0a 20  teTime( &dt );. 
c4b20 20 73 65 63 6f 6e 64 20 3d 20 28 55 53 48 4f 52   second = (USHOR
c4b30 54 29 64 74 2e 73 65 63 6f 6e 64 73 3b 0a 20 20  T)dt.seconds;.  
c4b40 6d 69 6e 75 74 65 20 3d 20 28 53 48 4f 52 54 29  minute = (SHORT)
c4b50 64 74 2e 6d 69 6e 75 74 65 73 20 2b 20 64 74 2e  dt.minutes + dt.
c4b60 74 69 6d 65 7a 6f 6e 65 3b 0a 20 20 68 6f 75 72  timezone;.  hour
c4b70 20 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 68 6f   = (USHORT)dt.ho
c4b80 75 72 73 3b 0a 20 20 64 61 79 20 3d 20 28 55 53  urs;.  day = (US
c4b90 48 4f 52 54 29 64 74 2e 64 61 79 3b 0a 20 20 6d  HORT)dt.day;.  m
c4ba0 6f 6e 74 68 20 3d 20 28 55 53 48 4f 52 54 29 64  onth = (USHORT)d
c4bb0 74 2e 6d 6f 6e 74 68 3b 0a 20 20 79 65 61 72 20  t.month;.  year 
c4bc0 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 79 65 61  = (USHORT)dt.yea
c4bd0 72 3b 0a 0a 20 20 2f 2a 20 43 61 6c 63 75 6c 61  r;..  /* Calcula
c4be0 74 69 6f 6e 73 20 66 72 6f 6d 20 68 74 74 70 3a  tions from http:
c4bf0 2f 2f 77 77 77 2e 61 73 74 72 6f 2e 6b 65 65 6c  //www.astro.keel
c4c00 65 2e 61 63 2e 75 6b 2f 7e 72 6e 6f 2f 41 73 74  e.ac.uk/~rno/Ast
c4c10 72 6f 6e 6f 6d 79 2f 68 6a 64 2e 68 74 6d 6c 0a  ronomy/hjd.html.
c4c20 20 20 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e       http://www.
c4c30 61 73 74 72 6f 2e 6b 65 65 6c 65 2e 61 63 2e 75  astro.keele.ac.u
c4c40 6b 2f 7e 72 6e 6f 2f 41 73 74 72 6f 6e 6f 6d 79  k/~rno/Astronomy
c4c50 2f 68 6a 64 2d 30 2e 31 2e 63 20 2a 2f 0a 20 20  /hjd-0.1.c */.  
c4c60 2f 2a 20 43 61 6c 63 75 6c 61 74 65 20 74 68 65  /* Calculate the
c4c70 20 4a 75 6c 69 61 6e 20 64 61 79 73 20 2a 2f 0a   Julian days */.
c4c80 20 20 6e 6f 77 20 3d 20 64 61 79 20 2d 20 33 32    now = day - 32
c4c90 30 37 36 20 2b 0a 20 20 20 20 31 34 36 31 2a 28  076 +.    1461*(
c4ca0 79 65 61 72 20 2b 20 34 38 30 30 20 2b 20 28 6d  year + 4800 + (m
c4cb0 6f 6e 74 68 20 2d 20 31 34 29 2f 31 32 29 2f 34  onth - 14)/12)/4
c4cc0 20 2b 0a 20 20 20 20 33 36 37 2a 28 6d 6f 6e 74   +.    367*(mont
c4cd0 68 20 2d 20 32 20 2d 20 28 6d 6f 6e 74 68 20 2d  h - 2 - (month -
c4ce0 20 31 34 29 2f 31 32 2a 31 32 29 2f 31 32 20 2d   14)/12*12)/12 -
c4cf0 0a 20 20 20 20 33 2a 28 28 79 65 61 72 20 2b 20  .    3*((year + 
c4d00 34 39 30 30 20 2b 20 28 6d 6f 6e 74 68 20 2d 20  4900 + (month - 
c4d10 31 34 29 2f 31 32 29 2f 31 30 30 29 2f 34 3b 0a  14)/12)/100)/4;.
c4d20 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20 66 72  .  /* Add the fr
c4d30 61 63 74 69 6f 6e 61 6c 20 68 6f 75 72 73 2c 20  actional hours, 
c4d40 6d 69 6e 73 20 61 6e 64 20 73 65 63 6f 6e 64 73  mins and seconds
c4d50 20 2a 2f 0a 20 20 6e 6f 77 20 2b 3d 20 28 68 6f   */.  now += (ho
c4d60 75 72 20 2b 20 31 32 2e 30 29 2f 32 34 2e 30 3b  ur + 12.0)/24.0;
c4d70 0a 20 20 6e 6f 77 20 2b 3d 20 6d 69 6e 75 74 65  .  now += minute
c4d80 2f 31 34 34 30 2e 30 3b 0a 20 20 6e 6f 77 20 2b  /1440.0;.  now +
c4d90 3d 20 73 65 63 6f 6e 64 2f 38 36 34 30 30 2e 30  = second/86400.0
c4da0 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 6e 6f 77  ;.  *prNow = now
c4db0 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
c4dc0 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74  TEST.  if( sqlit
c4dd0 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20  e3_current_time 
c4de0 29 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20  ){.    *prNow = 
c4df0 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f  sqlite3_current_
c4e00 74 69 6d 65 2f 38 36 34 30 30 2e 30 20 2b 20 32  time/86400.0 + 2
c4e10 34 34 30 35 38 37 2e 35 3b 0a 20 20 7d 0a 23 65  440587.5;.  }.#e
c4e20 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 30 3b  ndif.  return 0;
c4e30 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  .}..static int o
c4e40 73 32 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73  s2GetLastError(s
c4e50 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
c4e60 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72  , int nBuf, char
c4e70 20 2a 7a 42 75 66 29 7b 0a 20 20 72 65 74 75 72   *zBuf){.  retur
c4e80 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  n 0;.}../*.** In
c4e90 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69  itialize and dei
c4ea0 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70  nitialize the op
c4eb0 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69  erating system i
c4ec0 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 53 51 4c  nterface..*/.SQL
c4ed0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
c4ee0 74 65 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64  te3_os_init(void
c4ef0 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69  ){.  static sqli
c4f00 74 65 33 5f 76 66 73 20 6f 73 32 56 66 73 20 3d  te3_vfs os2Vfs =
c4f10 20 7b 0a 20 20 20 20 31 2c 20 20 20 20 20 20 20   {.    1,       
c4f20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65            /* iVe
c4f30 72 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 73 69 7a  rsion */.    siz
c4f40 65 6f 66 28 6f 73 32 46 69 6c 65 29 2c 20 20 20  eof(os2File),   
c4f50 2f 2a 20 73 7a 4f 73 46 69 6c 65 20 2a 2f 0a 20  /* szOsFile */. 
c4f60 20 20 20 43 43 48 4d 41 58 50 41 54 48 2c 20 20     CCHMAXPATH,  
c4f70 20 20 20 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e        /* mxPathn
c4f80 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20  ame */.    0,   
c4f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
c4fa0 20 70 4e 65 78 74 20 2a 2f 0a 20 20 20 20 22 6f   pNext */.    "o
c4fb0 73 32 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  s2",            
c4fc0 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20   /* zName */.   
c4fd0 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
c4fe0 20 20 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20      /* pAppData 
c4ff0 2a 2f 0a 0a 20 20 20 20 6f 73 32 4f 70 65 6e 2c  */..    os2Open,
c5000 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f             /* xO
c5010 70 65 6e 20 2a 2f 0a 20 20 20 20 6f 73 32 44 65  pen */.    os2De
c5020 6c 65 74 65 2c 20 20 20 20 20 20 20 20 20 2f 2a  lete,         /*
c5030 20 78 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20 20   xDelete */.    
c5040 6f 73 32 41 63 63 65 73 73 2c 20 20 20 20 20 20  os2Access,      
c5050 20 20 20 2f 2a 20 78 41 63 63 65 73 73 20 2a 2f     /* xAccess */
c5060 0a 20 20 20 20 6f 73 32 46 75 6c 6c 50 61 74 68  .    os2FullPath
c5070 6e 61 6d 65 2c 20 20 20 2f 2a 20 78 46 75 6c 6c  name,   /* xFull
c5080 50 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 20  Pathname */.    
c5090 6f 73 32 44 6c 4f 70 65 6e 2c 20 20 20 20 20 20  os2DlOpen,      
c50a0 20 20 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f     /* xDlOpen */
c50b0 0a 20 20 20 20 6f 73 32 44 6c 45 72 72 6f 72 2c  .    os2DlError,
c50c0 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72          /* xDlEr
c50d0 72 6f 72 20 2a 2f 0a 20 20 20 20 6f 73 32 44 6c  ror */.    os2Dl
c50e0 53 79 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  Sym,          /*
c50f0 20 78 44 6c 53 79 6d 20 2a 2f 0a 20 20 20 20 6f   xDlSym */.    o
c5100 73 32 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 20  s2DlClose,      
c5110 20 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a 2f    /* xDlClose */
c5120 0a 20 20 20 20 6f 73 32 52 61 6e 64 6f 6d 6e 65  .    os2Randomne
c5130 73 73 2c 20 20 20 20 20 2f 2a 20 78 52 61 6e 64  ss,     /* xRand
c5140 6f 6d 6e 65 73 73 20 2a 2f 0a 20 20 20 20 6f 73  omness */.    os
c5150 32 53 6c 65 65 70 2c 20 20 20 20 20 20 20 20 20  2Sleep,         
c5160 20 2f 2a 20 78 53 6c 65 65 70 20 2a 2f 0a 20 20   /* xSleep */.  
c5170 20 20 6f 73 32 43 75 72 72 65 6e 74 54 69 6d 65    os2CurrentTime
c5180 2c 20 20 20 20 2f 2a 20 78 43 75 72 72 65 6e 74  ,    /* xCurrent
c5190 54 69 6d 65 20 2a 2f 0a 20 20 20 20 6f 73 32 47  Time */.    os2G
c51a0 65 74 4c 61 73 74 45 72 72 6f 72 20 20 20 20 2f  etLastError    /
c51b0 2a 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72 20  * xGetLastError 
c51c0 2a 2f 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65  */.  };.  sqlite
c51d0 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 26  3_vfs_register(&
c51e0 6f 73 32 56 66 73 2c 20 31 29 3b 0a 20 20 69 6e  os2Vfs, 1);.  in
c51f0 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29  itUconvObjects()
c5200 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
c5210 45 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  E_OK;.}.SQLITE_A
c5220 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  PI int sqlite3_o
c5230 73 5f 65 6e 64 28 76 6f 69 64 29 7b 0a 20 20 66  s_end(void){.  f
c5240 72 65 65 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28  reeUconvObjects(
c5250 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
c5260 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66  TE_OK;.}..#endif
c5270 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53   /* SQLITE_OS_OS
c5280 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  2 */../*********
c5290 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f  ***** End of os_
c52a0 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  os2.c **********
c52b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c52c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c52d0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
c52e0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
c52f0 20 6f 73 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a   os_unix.c *****
c5300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5320 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34  ****/./*.** 2004
c5330 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68   May 22.**.** Th
c5340 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
c5350 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
c5360 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
c5370 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
c5380 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
c5390 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
c53a0 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
c53b0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
c53c0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
c53d0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
c53e0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
c53f0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
c5400 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
c5410 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
c5420 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
c5430 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
c5440 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
c5450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
c54a0 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
c54b0 61 69 6e 73 20 74 68 65 20 56 46 53 20 69 6d 70  ains the VFS imp
c54c0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20  lementation for 
c54d0 75 6e 69 78 2d 6c 69 6b 65 20 6f 70 65 72 61 74  unix-like operat
c54e0 69 6e 67 20 73 79 73 74 65 6d 73 0a 2a 2a 20 69  ing systems.** i
c54f0 6e 63 6c 75 64 65 20 4c 69 6e 75 78 2c 20 4d 61  nclude Linux, Ma
c5500 63 4f 53 58 2c 20 2a 42 53 44 2c 20 51 4e 58 2c  cOSX, *BSD, QNX,
c5510 20 56 78 57 6f 72 6b 73 2c 20 41 49 58 2c 20 48   VxWorks, AIX, H
c5520 50 55 58 2c 20 61 6e 64 20 6f 74 68 65 72 73 2e  PUX, and others.
c5530 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65  .**.** There are
c5540 20 61 63 74 75 61 6c 6c 79 20 73 65 76 65 72 61   actually severa
c5550 6c 20 64 69 66 66 65 72 65 6e 74 20 56 46 53 20  l different VFS 
c5560 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
c5570 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2a  in this file..**
c5580 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 73   The differences
c5590 20 61 72 65 20 69 6e 20 74 68 65 20 77 61 79 20   are in the way 
c55a0 74 68 61 74 20 66 69 6c 65 20 6c 6f 63 6b 69 6e  that file lockin
c55b0 67 20 69 73 20 64 6f 6e 65 2e 20 20 54 68 65 20  g is done.  The 
c55c0 64 65 66 61 75 6c 74 0a 2a 2a 20 69 6d 70 6c 65  default.** imple
c55d0 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 50  mentation uses P
c55e0 6f 73 69 78 20 41 64 76 69 73 6f 72 79 20 4c 6f  osix Advisory Lo
c55f0 63 6b 73 2e 20 20 41 6c 74 65 72 6e 61 74 69 76  cks.  Alternativ
c5600 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
c5610 73 0a 2a 2a 20 75 73 65 20 66 6c 6f 63 6b 28 29  s.** use flock()
c5620 2c 20 64 6f 74 2d 66 69 6c 65 73 2c 20 76 61 72  , dot-files, var
c5630 69 6f 75 73 20 70 72 6f 70 72 69 65 74 61 72 79  ious proprietary
c5640 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d 61 73   locking schemas
c5650 2c 20 6f 72 20 73 69 6d 70 6c 79 0a 2a 2a 20 73  , or simply.** s
c5660 6b 69 70 20 6c 6f 63 6b 69 6e 67 20 61 6c 6c 20  kip locking all 
c5670 74 6f 67 65 74 68 65 72 2e 0a 2a 2a 0a 2a 2a 20  together..**.** 
c5680 54 68 69 73 20 73 6f 75 72 63 65 20 66 69 6c 65  This source file
c5690 20 69 73 20 6f 72 67 61 6e 69 7a 65 64 20 69 6e   is organized in
c56a0 74 6f 20 64 69 76 69 73 69 6f 6e 73 20 77 68 65  to divisions whe
c56b0 72 65 20 74 68 65 20 6c 6f 67 69 63 20 66 6f 72  re the logic for
c56c0 20 76 61 72 69 6f 75 73 0a 2a 2a 20 73 75 62 66   various.** subf
c56d0 75 6e 63 74 69 6f 6e 73 20 69 73 20 63 6f 6e 74  unctions is cont
c56e0 61 69 6e 65 64 20 77 69 74 68 69 6e 20 74 68 65  ained within the
c56f0 20 61 70 70 72 6f 70 72 69 61 74 65 20 64 69 76   appropriate div
c5700 69 73 69 6f 6e 2e 20 20 50 4c 45 41 53 45 0a 2a  ision.  PLEASE.*
c5710 2a 20 4b 45 45 50 20 54 48 45 20 53 54 52 55 43  * KEEP THE STRUC
c5720 54 55 52 45 20 4f 46 20 54 48 49 53 20 46 49 4c  TURE OF THIS FIL
c5730 45 20 49 4e 54 41 43 54 2e 20 20 4e 65 77 20 63  E INTACT.  New c
c5740 6f 64 65 20 73 68 6f 75 6c 64 20 62 65 20 70 6c  ode should be pl
c5750 61 63 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 63  aced.** in the c
c5760 6f 72 72 65 63 74 20 64 69 76 69 73 69 6f 6e 20  orrect division 
c5770 61 6e 64 20 73 68 6f 75 6c 64 20 62 65 20 63 6c  and should be cl
c5780 65 61 72 6c 79 20 6c 61 62 65 6c 65 64 2e 0a 2a  early labeled..*
c5790 2a 0a 2a 2a 20 54 68 65 20 6c 61 79 6f 75 74 20  *.** The layout 
c57a0 6f 66 20 64 69 76 69 73 69 6f 6e 73 20 69 73 20  of divisions is 
c57b0 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
c57c0 2a 20 20 20 2a 20 20 47 65 6e 65 72 61 6c 2d 70  *   *  General-p
c57d0 75 72 70 6f 73 65 20 64 65 63 6c 61 72 61 74 69  urpose declarati
c57e0 6f 6e 73 20 61 6e 64 20 75 74 69 6c 69 74 79 20  ons and utility 
c57f0 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20 20 20  functions..**   
c5800 2a 20 20 55 6e 69 71 75 65 20 66 69 6c 65 20 49  *  Unique file I
c5810 44 20 6c 6f 67 69 63 20 75 73 65 64 20 62 79 20  D logic used by 
c5820 56 78 57 6f 72 6b 73 2e 0a 2a 2a 20 20 20 2a 20  VxWorks..**   * 
c5830 20 56 61 72 69 6f 75 73 20 6c 6f 63 6b 69 6e 67   Various locking
c5840 20 70 72 69 6d 69 74 69 76 65 20 69 6d 70 6c 65   primitive imple
c5850 6d 65 6e 74 61 74 69 6f 6e 73 20 28 61 6c 6c 20  mentations (all 
c5860 65 78 63 65 70 74 20 70 72 6f 78 79 20 6c 6f 63  except proxy loc
c5870 6b 69 6e 67 29 3a 0a 2a 2a 20 20 20 20 20 20 2b  king):.**      +
c5880 20 66 6f 72 20 50 6f 73 69 78 20 41 64 76 69 73   for Posix Advis
c5890 6f 72 79 20 4c 6f 63 6b 73 0a 2a 2a 20 20 20 20  ory Locks.**    
c58a0 20 20 2b 20 66 6f 72 20 6e 6f 2d 6f 70 20 6c 6f    + for no-op lo
c58b0 63 6b 73 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f  cks.**      + fo
c58c0 72 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 73  r dot-file locks
c58d0 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 66  .**      + for f
c58e0 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 0a 2a  lock() locking.*
c58f0 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 6e 61 6d  *      + for nam
c5900 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63  ed semaphore loc
c5910 6b 73 20 28 56 78 57 6f 72 6b 73 20 6f 6e 6c 79  ks (VxWorks only
c5920 29 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20  ).**      + for 
c5930 41 46 50 20 66 69 6c 65 73 79 73 74 65 6d 20 6c  AFP filesystem l
c5940 6f 63 6b 73 20 28 4d 61 63 4f 53 58 20 6f 6e 6c  ocks (MacOSX onl
c5950 79 29 0a 2a 2a 20 20 20 2a 20 20 73 71 6c 69 74  y).**   *  sqlit
c5960 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 20  e3_file methods 
c5970 6e 6f 74 20 61 73 73 6f 63 69 61 74 65 64 20 77  not associated w
c5980 69 74 68 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20  ith locking..** 
c5990 20 20 2a 20 20 44 65 66 69 6e 69 74 69 6f 6e 73    *  Definitions
c59a0 20 6f 66 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d   of sqlite3_io_m
c59b0 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 73 20 66  ethods objects f
c59c0 6f 72 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 0a 2a  or all locking.*
c59d0 2a 20 20 20 20 20 20 6d 65 74 68 6f 64 73 20 70  *      methods p
c59e0 6c 75 73 20 22 66 69 6e 64 65 72 22 20 66 75 6e  lus "finder" fun
c59f0 63 74 69 6f 6e 73 20 66 6f 72 20 65 61 63 68 20  ctions for each 
c5a00 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 2e 0a  locking method..
c5a10 2a 2a 20 20 20 2a 20 20 73 71 6c 69 74 65 33 5f  **   *  sqlite3_
c5a20 76 66 73 20 6d 65 74 68 6f 64 20 69 6d 70 6c 65  vfs method imple
c5a30 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 20 20  mentations..**  
c5a40 20 2a 20 20 4c 6f 63 6b 69 6e 67 20 70 72 69 6d   *  Locking prim
c5a50 69 74 69 76 65 73 20 66 6f 72 20 74 68 65 20 70  itives for the p
c5a60 72 6f 78 79 20 75 62 65 72 2d 6c 6f 63 6b 69 6e  roxy uber-lockin
c5a70 67 2d 6d 65 74 68 6f 64 2e 20 28 4d 61 63 4f 53  g-method. (MacOS
c5a80 58 20 6f 6e 6c 79 29 0a 2a 2a 20 20 20 2a 20 20  X only).**   *  
c5a90 44 65 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20 73  Definitions of s
c5aa0 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63  qlite3_vfs objec
c5ab0 74 73 20 66 6f 72 20 61 6c 6c 20 6c 6f 63 6b 69  ts for all locki
c5ac0 6e 67 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 20 20  ng methods.**   
c5ad0 20 20 20 70 6c 75 73 20 69 6d 70 6c 65 6d 65 6e     plus implemen
c5ae0 74 61 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74  tations of sqlit
c5af0 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 61 6e 64  e3_os_init() and
c5b00 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28   sqlite3_os_end(
c5b10 29 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  )..*/.#if SQLITE
c5b20 5f 4f 53 5f 55 4e 49 58 20 20 20 20 20 20 20 20  _OS_UNIX        
c5b30 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 66 69        /* This fi
c5b40 6c 65 20 69 73 20 75 73 65 64 20 6f 6e 20 75 6e  le is used on un
c5b50 69 78 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f 2a 0a 2a  ix only */../*.*
c5b60 2a 20 54 68 65 72 65 20 61 72 65 20 76 61 72 69  * There are vari
c5b70 6f 75 73 20 6d 65 74 68 6f 64 73 20 66 6f 72 20  ous methods for 
c5b80 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 75 73 65  file locking use
c5b90 64 20 66 6f 72 20 63 6f 6e 63 75 72 72 65 6e 63  d for concurrenc
c5ba0 79 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 3a 0a 2a 2a  y.** control:.**
c5bb0 0a 2a 2a 20 20 20 31 2e 20 50 4f 53 49 58 20 6c  .**   1. POSIX l
c5bc0 6f 63 6b 69 6e 67 20 28 74 68 65 20 64 65 66 61  ocking (the defa
c5bd0 75 6c 74 29 2c 0a 2a 2a 20 20 20 32 2e 20 4e 6f  ult),.**   2. No
c5be0 20 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20 20 33   locking,.**   3
c5bf0 2e 20 44 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 69  . Dot-file locki
c5c00 6e 67 2c 0a 2a 2a 20 20 20 34 2e 20 66 6c 6f 63  ng,.**   4. floc
c5c10 6b 28 29 20 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20  k() locking,.** 
c5c20 20 20 35 2e 20 41 46 50 20 6c 6f 63 6b 69 6e 67    5. AFP locking
c5c30 20 28 4f 53 58 20 6f 6e 6c 79 29 2c 0a 2a 2a 20   (OSX only),.** 
c5c40 20 20 36 2e 20 4e 61 6d 65 64 20 50 4f 53 49 58    6. Named POSIX
c5c50 20 73 65 6d 61 70 68 6f 72 65 73 20 28 56 58 57   semaphores (VXW
c5c60 6f 72 6b 73 20 6f 6e 6c 79 29 2c 0a 2a 2a 20 20  orks only),.**  
c5c70 20 37 2e 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e   7. proxy lockin
c5c80 67 2e 20 28 4f 53 58 20 6f 6e 6c 79 29 0a 2a 2a  g. (OSX only).**
c5c90 0a 2a 2a 20 53 74 79 6c 65 73 20 34 2c 20 35 2c  .** Styles 4, 5,
c5ca0 20 61 6e 64 20 37 20 61 72 65 20 6f 6e 6c 79 20   and 7 are only 
c5cb0 61 76 61 69 6c 61 62 6c 65 20 6f 66 20 53 51 4c  available of SQL
c5cc0 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
c5cd0 4e 47 5f 53 54 59 4c 45 0a 2a 2a 20 69 73 20 64  NG_STYLE.** is d
c5ce0 65 66 69 6e 65 64 20 74 6f 20 31 2e 20 20 54 68  efined to 1.  Th
c5cf0 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  e SQLITE_ENABLE_
c5d00 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 61 6c  LOCKING_STYLE al
c5d10 73 6f 20 65 6e 61 62 6c 65 73 20 61 75 74 6f 6d  so enables autom
c5d20 61 74 69 63 0a 2a 2a 20 73 65 6c 65 63 74 69 6f  atic.** selectio
c5d30 6e 20 6f 66 20 74 68 65 20 61 70 70 72 6f 70 72  n of the appropr
c5d40 69 61 74 65 20 6c 6f 63 6b 69 6e 67 20 73 74 79  iate locking sty
c5d50 6c 65 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20  le based on the 
c5d60 66 69 6c 65 73 79 73 74 65 6d 0a 2a 2a 20 77 68  filesystem.** wh
c5d70 65 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ere the database
c5d80 20 69 73 20 6c 6f 63 61 74 65 64 2e 20 20 0a 2a   is located.  .*
c5d90 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  /.#if !defined(S
c5da0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
c5db0 4b 49 4e 47 5f 53 54 59 4c 45 29 0a 23 20 20 69  KING_STYLE).#  i
c5dc0 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c  f defined(__APPL
c5dd0 45 5f 5f 29 0a 23 20 20 20 20 64 65 66 69 6e 65  E__).#    define
c5de0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
c5df0 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 31 0a 23  OCKING_STYLE 1.#
c5e00 20 20 65 6c 73 65 0a 23 20 20 20 20 64 65 66 69    else.#    defi
c5e10 6e 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ne SQLITE_ENABLE
c5e20 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 30  _LOCKING_STYLE 0
c5e30 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 66  .#  endif.#endif
c5e40 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e 65 20 74  ../*.** Define t
c5e50 68 65 20 4f 53 5f 56 58 57 4f 52 4b 53 20 70 72  he OS_VXWORKS pr
c5e60 65 2d 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72  e-processor macr
c5e70 6f 20 74 6f 20 31 20 69 66 20 62 75 69 6c 64 69  o to 1 if buildi
c5e80 6e 67 20 6f 6e 20 0a 2a 2a 20 76 78 77 6f 72 6b  ng on .** vxwork
c5e90 73 2c 20 6f 72 20 30 20 6f 74 68 65 72 77 69 73  s, or 0 otherwis
c5ea0 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4f 53  e..*/.#ifndef OS
c5eb0 5f 56 58 57 4f 52 4b 53 0a 23 20 20 69 66 20 64  _VXWORKS.#  if d
c5ec0 65 66 69 6e 65 64 28 5f 5f 52 54 50 5f 5f 29 20  efined(__RTP__) 
c5ed0 7c 7c 20 64 65 66 69 6e 65 64 28 5f 57 52 53 5f  || defined(_WRS_
c5ee0 4b 45 52 4e 45 4c 29 0a 23 20 20 20 20 64 65 66  KERNEL).#    def
c5ef0 69 6e 65 20 4f 53 5f 56 58 57 4f 52 4b 53 20 31  ine OS_VXWORKS 1
c5f00 0a 23 20 20 65 6c 73 65 0a 23 20 20 20 20 64 65  .#  else.#    de
c5f10 66 69 6e 65 20 4f 53 5f 56 58 57 4f 52 4b 53 20  fine OS_VXWORKS 
c5f20 30 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69  0.#  endif.#endi
c5f30 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 23  f../*.** These #
c5f40 64 65 66 69 6e 65 73 20 73 68 6f 75 6c 64 20 65  defines should e
c5f50 6e 61 62 6c 65 20 3e 32 47 42 20 66 69 6c 65 20  nable >2GB file 
c5f60 73 75 70 70 6f 72 74 20 6f 6e 20 50 6f 73 69 78  support on Posix
c5f70 20 69 66 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72   if the.** under
c5f80 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20  lying operating 
c5f90 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20  system supports 
c5fa0 69 74 2e 20 20 49 66 20 74 68 65 20 4f 53 20 6c  it.  If the OS l
c5fb0 61 63 6b 73 0a 2a 2a 20 6c 61 72 67 65 20 66 69  acks.** large fi
c5fc0 6c 65 20 73 75 70 70 6f 72 74 2c 20 74 68 65 73  le support, thes
c5fd0 65 20 73 68 6f 75 6c 64 20 62 65 20 6e 6f 2d 6f  e should be no-o
c5fe0 70 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61 72 67 65 20  ps..**.** Large 
c5ff0 66 69 6c 65 20 73 75 70 70 6f 72 74 20 63 61 6e  file support can
c6000 20 62 65 20 64 69 73 61 62 6c 65 64 20 75 73 69   be disabled usi
c6010 6e 67 20 74 68 65 20 2d 44 53 51 4c 49 54 45 5f  ng the -DSQLITE_
c6020 44 49 53 41 42 4c 45 5f 4c 46 53 20 73 77 69 74  DISABLE_LFS swit
c6030 63 68 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 6f 6d  ch.** on the com
c6040 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 20 6c 69  piler command li
c6050 6e 65 2e 20 20 54 68 69 73 20 69 73 20 6e 65 63  ne.  This is nec
c6060 65 73 73 61 72 79 20 69 66 20 79 6f 75 20 61 72  essary if you ar
c6070 65 20 63 6f 6d 70 69 6c 69 6e 67 0a 2a 2a 20 6f  e compiling.** o
c6080 6e 20 61 20 72 65 63 65 6e 74 20 6d 61 63 68 69  n a recent machi
c6090 6e 65 20 28 65 78 3a 20 52 65 64 48 61 74 20 37  ne (ex: RedHat 7
c60a0 2e 32 29 20 62 75 74 20 79 6f 75 20 77 61 6e 74  .2) but you want
c60b0 20 79 6f 75 72 20 63 6f 64 65 20 74 6f 20 77 6f   your code to wo
c60c0 72 6b 0a 2a 2a 20 6f 6e 20 61 6e 20 6f 6c 64 65  rk.** on an olde
c60d0 72 20 6d 61 63 68 69 6e 65 20 28 65 78 3a 20 52  r machine (ex: R
c60e0 65 64 48 61 74 20 36 2e 30 29 2e 20 20 49 66 20  edHat 6.0).  If 
c60f0 79 6f 75 20 63 6f 6d 70 69 6c 65 20 6f 6e 20 52  you compile on R
c6100 65 64 48 61 74 20 37 2e 32 0a 2a 2a 20 77 69 74  edHat 7.2.** wit
c6110 68 6f 75 74 20 74 68 69 73 20 6f 70 74 69 6f 6e  hout this option
c6120 2c 20 4c 46 53 20 69 73 20 65 6e 61 62 6c 65 2e  , LFS is enable.
c6130 20 20 42 75 74 20 4c 46 53 20 64 6f 65 73 20 6e    But LFS does n
c6140 6f 74 20 65 78 69 73 74 20 69 6e 20 74 68 65 20  ot exist in the 
c6150 6b 65 72 6e 65 6c 0a 2a 2a 20 69 6e 20 52 65 64  kernel.** in Red
c6160 48 61 74 20 36 2e 30 2c 20 73 6f 20 74 68 65 20  Hat 6.0, so the 
c6170 63 6f 64 65 20 77 6f 6e 27 74 20 77 6f 72 6b 2e  code won't work.
c6180 20 20 48 65 6e 63 65 2c 20 66 6f 72 20 6d 61 78    Hence, for max
c6190 69 6d 75 6d 20 62 69 6e 61 72 79 0a 2a 2a 20 70  imum binary.** p
c61a0 6f 72 74 61 62 69 6c 69 74 79 20 79 6f 75 20 73  ortability you s
c61b0 68 6f 75 6c 64 20 6f 6d 69 74 20 4c 46 53 2e 0a  hould omit LFS..
c61c0 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 65 76 69 6f  **.** The previo
c61d0 75 73 20 70 61 72 61 67 72 61 70 68 20 77 61 73  us paragraph was
c61e0 20 77 72 69 74 74 65 6e 20 69 6e 20 32 30 30 35   written in 2005
c61f0 2e 20 20 28 54 68 69 73 20 70 61 72 61 67 72 61  .  (This paragra
c6200 70 68 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a  ph is written.**
c6210 20 6f 6e 20 32 30 30 38 2d 31 31 2d 32 38 2e 29   on 2008-11-28.)
c6220 20 54 68 65 73 65 20 64 61 79 73 2c 20 61 6c 6c   These days, all
c6230 20 4c 69 6e 75 78 20 6b 65 72 6e 65 6c 73 20 73   Linux kernels s
c6240 75 70 70 6f 72 74 20 6c 61 72 67 65 20 66 69 6c  upport large fil
c6250 65 73 2c 20 73 6f 0a 2a 2a 20 79 6f 75 20 73 68  es, so.** you sh
c6260 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79 20 6c 65  ould probably le
c6270 61 76 65 20 4c 46 53 20 65 6e 61 62 6c 65 64 2e  ave LFS enabled.
c6280 20 20 42 75 74 20 73 6f 6d 65 20 65 6d 62 65 64    But some embed
c6290 64 65 64 20 70 6c 61 74 66 6f 72 6d 73 20 6d 69  ded platforms mi
c62a0 67 68 74 0a 2a 2a 20 6c 61 63 6b 20 4c 46 53 20  ght.** lack LFS 
c62b0 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68  in which case th
c62c0 65 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45  e SQLITE_DISABLE
c62d0 5f 4c 46 53 20 6d 61 63 72 6f 20 6d 69 67 68 74  _LFS macro might
c62e0 20 73 74 69 6c 6c 20 62 65 20 75 73 65 66 75 6c   still be useful
c62f0 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
c6300 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 0a  ITE_DISABLE_LFS.
c6310 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 47 45 5f  # define _LARGE_
c6320 46 49 4c 45 20 20 20 20 20 20 20 31 0a 23 20 69  FILE       1.# i
c6330 66 6e 64 65 66 20 5f 46 49 4c 45 5f 4f 46 46 53  fndef _FILE_OFFS
c6340 45 54 5f 42 49 54 53 0a 23 20 20 20 64 65 66 69  ET_BITS.#   defi
c6350 6e 65 20 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f  ne _FILE_OFFSET_
c6360 42 49 54 53 20 36 34 0a 23 20 65 6e 64 69 66 0a  BITS 64.# endif.
c6370 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 47 45 46  # define _LARGEF
c6380 49 4c 45 5f 53 4f 55 52 43 45 20 31 0a 23 65 6e  ILE_SOURCE 1.#en
c6390 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 73 74 61 6e 64  dif../*.** stand
c63a0 61 72 64 20 69 6e 63 6c 75 64 65 20 66 69 6c 65  ard include file
c63b0 73 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c  s..*/.#include <
c63c0 73 79 73 2f 74 79 70 65 73 2e 68 3e 0a 23 69 6e  sys/types.h>.#in
c63d0 63 6c 75 64 65 20 3c 73 79 73 2f 73 74 61 74 2e  clude <sys/stat.
c63e0 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 66 63 6e  h>.#include <fcn
c63f0 74 6c 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c  tl.h>.#include <
c6400 75 6e 69 73 74 64 2e 68 3e 0a 23 69 6e 63 6c 75  unistd.h>.#inclu
c6410 64 65 20 3c 73 79 73 2f 74 69 6d 65 2e 68 3e 0a  de <sys/time.h>.
c6420 23 69 6e 63 6c 75 64 65 20 3c 65 72 72 6e 6f 2e  #include <errno.
c6430 68 3e 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45  h>..#if SQLITE_E
c6440 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
c6450 59 4c 45 0a 23 20 69 6e 63 6c 75 64 65 20 3c 73  YLE.# include <s
c6460 79 73 2f 69 6f 63 74 6c 2e 68 3e 0a 23 20 69 66  ys/ioctl.h>.# if
c6470 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 23 20 20 69   OS_VXWORKS.#  i
c6480 6e 63 6c 75 64 65 20 3c 73 65 6d 61 70 68 6f 72  nclude <semaphor
c6490 65 2e 68 3e 0a 23 20 20 69 6e 63 6c 75 64 65 20  e.h>.#  include 
c64a0 3c 6c 69 6d 69 74 73 2e 68 3e 0a 23 20 65 6c 73  <limits.h>.# els
c64b0 65 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 79  e.#  include <sy
c64c0 73 2f 66 69 6c 65 2e 68 3e 0a 23 20 20 69 6e 63  s/file.h>.#  inc
c64d0 6c 75 64 65 20 3c 73 79 73 2f 70 61 72 61 6d 2e  lude <sys/param.
c64e0 68 3e 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 73  h>.#  include <s
c64f0 79 73 2f 6d 6f 75 6e 74 2e 68 3e 0a 23 20 65 6e  ys/mount.h>.# en
c6500 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  dif.#endif /* SQ
c6510 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b  LITE_ENABLE_LOCK
c6520 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a 2f 2a  ING_STYLE */../*
c6530 0a 2a 2a 20 49 66 20 77 65 20 61 72 65 20 74 6f  .** If we are to
c6540 20 62 65 20 74 68 72 65 61 64 2d 73 61 66 65 2c   be thread-safe,
c6550 20 69 6e 63 6c 75 64 65 20 74 68 65 20 70 74 68   include the pth
c6560 72 65 61 64 73 20 68 65 61 64 65 72 20 61 6e 64  reads header and
c6570 20 64 65 66 69 6e 65 0a 2a 2a 20 74 68 65 20 53   define.** the S
c6580 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48 52 45 41  QLITE_UNIX_THREA
c6590 44 53 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66  DS macro..*/.#if
c65a0 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
c65b0 46 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  FE.# define SQLI
c65c0 54 45 5f 55 4e 49 58 5f 54 48 52 45 41 44 53 20  TE_UNIX_THREADS 
c65d0 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  1.#endif../*.** 
c65e0 44 65 66 61 75 6c 74 20 70 65 72 6d 69 73 73 69  Default permissi
c65f0 6f 6e 73 20 77 68 65 6e 20 63 72 65 61 74 69 6e  ons when creatin
c6600 67 20 61 20 6e 65 77 20 66 69 6c 65 0a 2a 2f 0a  g a new file.*/.
c6610 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44  #ifndef SQLITE_D
c6620 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d  EFAULT_FILE_PERM
c6630 49 53 53 49 4f 4e 53 0a 23 20 64 65 66 69 6e 65  ISSIONS.# define
c6640 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
c6650 46 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53  FILE_PERMISSIONS
c6660 20 30 36 34 34 0a 23 65 6e 64 69 66 0a 0a 2f 2a   0644.#endif../*
c6670 0a 20 2a 2a 20 44 65 66 61 75 6c 74 20 70 65 72  . ** Default per
c6680 6d 69 73 73 69 6f 6e 73 20 77 68 65 6e 20 63 72  missions when cr
c6690 65 61 74 69 6e 67 20 61 75 74 6f 20 70 72 6f 78  eating auto prox
c66a0 79 20 64 69 72 0a 20 2a 2f 0a 23 69 66 6e 64 65  y dir. */.#ifnde
c66b0 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  f SQLITE_DEFAULT
c66c0 5f 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53  _PROXYDIR_PERMIS
c66d0 53 49 4f 4e 53 0a 23 20 64 65 66 69 6e 65 20 53  SIONS.# define S
c66e0 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 52  QLITE_DEFAULT_PR
c66f0 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 53 49 4f  OXYDIR_PERMISSIO
c6700 4e 53 20 30 37 35 35 0a 23 65 6e 64 69 66 0a 0a  NS 0755.#endif..
c6710 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 73 75  /*.** Maximum su
c6720 70 70 6f 72 74 65 64 20 70 61 74 68 2d 6c 65 6e  pported path-len
c6730 67 74 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  gth..*/.#define 
c6740 4d 41 58 5f 50 41 54 48 4e 41 4d 45 20 35 31 32  MAX_PATHNAME 512
c6750 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 6c 79 20 73 65 74  ../*.** Only set
c6760 20 74 68 65 20 6c 61 73 74 45 72 72 6e 6f 20 69   the lastErrno i
c6770 66 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65  f the error code
c6780 20 69 73 20 61 20 72 65 61 6c 20 65 72 72 6f 72   is a real error
c6790 20 61 6e 64 20 6e 6f 74 20 0a 2a 2a 20 61 20 6e   and not .** a n
c67a0 6f 72 6d 61 6c 20 65 78 70 65 63 74 65 64 20 72  ormal expected r
c67b0 65 74 75 72 6e 20 63 6f 64 65 20 6f 66 20 53 51  eturn code of SQ
c67c0 4c 49 54 45 5f 42 55 53 59 20 6f 72 20 53 51 4c  LITE_BUSY or SQL
c67d0 49 54 45 5f 4f 4b 0a 2a 2f 0a 23 64 65 66 69 6e  ITE_OK.*/.#defin
c67e0 65 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28  e IS_LOCK_ERROR(
c67f0 78 29 20 20 28 28 78 20 21 3d 20 53 51 4c 49 54  x)  ((x != SQLIT
c6800 45 5f 4f 4b 29 20 26 26 20 28 78 20 21 3d 20 53  E_OK) && (x != S
c6810 51 4c 49 54 45 5f 42 55 53 59 29 29 0a 0a 0a 2f  QLITE_BUSY)).../
c6820 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 2c 20  *.** Sometimes, 
c6830 61 66 74 65 72 20 61 20 66 69 6c 65 20 68 61 6e  after a file han
c6840 64 6c 65 20 69 73 20 63 6c 6f 73 65 64 20 62 79  dle is closed by
c6850 20 53 51 4c 69 74 65 2c 20 74 68 65 20 66 69 6c   SQLite, the fil
c6860 65 20 64 65 73 63 72 69 70 74 6f 72 0a 2a 2a 20  e descriptor.** 
c6870 63 61 6e 6e 6f 74 20 62 65 20 63 6c 6f 73 65 64  cannot be closed
c6880 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 49 6e   immediately. In
c6890 20 74 68 65 73 65 20 63 61 73 65 73 2c 20 69 6e   these cases, in
c68a0 73 74 61 6e 63 65 73 20 6f 66 20 74 68 65 20 66  stances of the f
c68b0 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75  ollowing.** stru
c68c0 63 74 75 72 65 20 61 72 65 20 75 73 65 64 20 74  cture are used t
c68d0 6f 20 73 74 6f 72 65 20 74 68 65 20 66 69 6c 65  o store the file
c68e0 20 64 65 73 63 72 69 70 74 6f 72 20 77 68 69 6c   descriptor whil
c68f0 65 20 77 61 69 74 69 6e 67 20 66 6f 72 20 61 6e  e waiting for an
c6900 0a 2a 2a 20 6f 70 70 6f 72 74 75 6e 69 74 79 20  .** opportunity 
c6910 74 6f 20 65 69 74 68 65 72 20 63 6c 6f 73 65 20  to either close 
c6920 6f 72 20 72 65 75 73 65 20 69 74 2e 0a 2a 2f 0a  or reuse it..*/.
c6930 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 55  typedef struct U
c6940 6e 69 78 55 6e 75 73 65 64 46 64 20 55 6e 69 78  nixUnusedFd Unix
c6950 55 6e 75 73 65 64 46 64 3b 0a 73 74 72 75 63 74  UnusedFd;.struct
c6960 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 7b 0a   UnixUnusedFd {.
c6970 20 20 69 6e 74 20 66 64 3b 20 20 20 20 20 20 20    int fd;       
c6980 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
c6990 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74  ile descriptor t
c69a0 6f 20 63 6c 6f 73 65 20 2a 2f 0a 20 20 69 6e 74  o close */.  int
c69b0 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20   flags;         
c69c0 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20         /* Flags 
c69d0 74 68 69 73 20 66 69 6c 65 20 64 65 73 63 72 69  this file descri
c69e0 70 74 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 20  ptor was opened 
c69f0 77 69 74 68 20 2a 2f 0a 20 20 55 6e 69 78 55 6e  with */.  UnixUn
c6a00 75 73 65 64 46 64 20 2a 70 4e 65 78 74 3b 20 20  usedFd *pNext;  
c6a10 20 20 20 20 2f 2a 20 4e 65 78 74 20 75 6e 75 73      /* Next unus
c6a20 65 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  ed file descript
c6a30 6f 72 20 6f 6e 20 73 61 6d 65 20 66 69 6c 65 20  or on same file 
c6a40 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  */.};../*.** The
c6a50 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74   unixFile struct
c6a60 75 72 65 20 69 73 20 73 75 62 63 6c 61 73 73 20  ure is subclass 
c6a70 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  of sqlite3_file 
c6a80 73 70 65 63 69 66 69 63 20 74 6f 20 74 68 65 20  specific to the 
c6a90 75 6e 69 78 0a 2a 2a 20 56 46 53 20 69 6d 70 6c  unix.** VFS impl
c6aa0 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a  ementations..*/.
c6ab0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 75  typedef struct u
c6ac0 6e 69 78 46 69 6c 65 20 75 6e 69 78 46 69 6c 65  nixFile unixFile
c6ad0 3b 0a 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c  ;.struct unixFil
c6ae0 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f  e {.  sqlite3_io
c6af0 5f 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a  _methods const *
c6b00 70 4d 65 74 68 6f 64 3b 20 20 2f 2a 20 41 6c 77  pMethod;  /* Alw
c6b10 61 79 73 20 74 68 65 20 66 69 72 73 74 20 65 6e  ays the first en
c6b20 74 72 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  try */.  struct 
c6b30 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70  unixOpenCnt *pOp
c6b40 65 6e 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 66  en;       /* Inf
c6b50 6f 20 61 62 6f 75 74 20 61 6c 6c 20 6f 70 65 6e  o about all open
c6b60 20 66 64 27 73 20 6f 6e 20 74 68 69 73 20 69 6e   fd's on this in
c6b70 6f 64 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ode */.  struct 
c6b80 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c  unixLockInfo *pL
c6b90 6f 63 6b 3b 20 20 20 20 20 20 2f 2a 20 49 6e 66  ock;      /* Inf
c6ba0 6f 20 61 62 6f 75 74 20 6c 6f 63 6b 73 20 6f 6e  o about locks on
c6bb0 20 74 68 69 73 20 69 6e 6f 64 65 20 2a 2f 0a 20   this inode */. 
c6bc0 20 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20   int h;         
c6bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c6be0 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65    /* The file de
c6bf0 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69 6e  scriptor */.  in
c6c00 74 20 64 69 72 66 64 3b 20 20 20 20 20 20 20 20  t dirfd;        
c6c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
c6c20 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f  * File descripto
c6c30 72 20 66 6f 72 20 74 68 65 20 64 69 72 65 63 74  r for the direct
c6c40 6f 72 79 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  ory */.  unsigne
c6c50 64 20 63 68 61 72 20 6c 6f 63 6b 74 79 70 65 3b  d char locktype;
c6c60 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
c6c70 20 74 79 70 65 20 6f 66 20 6c 6f 63 6b 20 68 65   type of lock he
c6c80 6c 64 20 6f 6e 20 74 68 69 73 20 66 64 20 2a 2f  ld on this fd */
c6c90 0a 20 20 69 6e 74 20 6c 61 73 74 45 72 72 6e 6f  .  int lastErrno
c6ca0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
c6cb0 20 20 20 20 2f 2a 20 54 68 65 20 75 6e 69 78 20      /* The unix 
c6cc0 65 72 72 6e 6f 20 66 72 6f 6d 20 74 68 65 20 6c  errno from the l
c6cd0 61 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f  ast I/O error */
c6ce0 0a 20 20 76 6f 69 64 20 2a 6c 6f 63 6b 69 6e 67  .  void *locking
c6cf0 43 6f 6e 74 65 78 74 3b 20 20 20 20 20 20 20 20  Context;        
c6d00 20 20 20 20 2f 2a 20 4c 6f 63 6b 69 6e 67 20 73      /* Locking s
c6d10 74 79 6c 65 20 73 70 65 63 69 66 69 63 20 73 74  tyle specific st
c6d20 61 74 65 20 2a 2f 0a 20 20 55 6e 69 78 55 6e 75  ate */.  UnixUnu
c6d30 73 65 64 46 64 20 2a 70 55 6e 75 73 65 64 3b 20  sedFd *pUnused; 
c6d40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65            /* Pre
c6d50 2d 61 6c 6c 6f 63 61 74 65 64 20 55 6e 69 78 55  -allocated UnixU
c6d60 6e 75 73 65 64 46 64 20 2a 2f 0a 20 20 69 6e 74  nusedFd */.  int
c6d70 20 66 69 6c 65 46 6c 61 67 73 3b 20 20 20 20 20   fileFlags;     
c6d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
c6d90 20 4d 69 73 63 65 6c 6c 61 6e 6f 75 73 20 66 6c   Miscellanous fl
c6da0 61 67 73 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54  ags */.#if SQLIT
c6db0 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
c6dc0 5f 53 54 59 4c 45 0a 20 20 69 6e 74 20 6f 70 65  _STYLE.  int ope
c6dd0 6e 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20  nFlags;         
c6de0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
c6df0 20 66 6c 61 67 73 20 73 70 65 63 69 66 69 65 64   flags specified
c6e00 20 61 74 20 6f 70 65 6e 28 29 20 2a 2f 0a 23 65   at open() */.#e
c6e10 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f  ndif.#if SQLITE_
c6e20 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65  THREADSAFE && de
c6e30 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29  fined(__linux__)
c6e40 0a 20 20 70 74 68 72 65 61 64 5f 74 20 74 69 64  .  pthread_t tid
c6e50 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
c6e60 20 20 20 20 2f 2a 20 54 68 65 20 74 68 72 65 61      /* The threa
c6e70 64 20 74 68 61 74 20 22 6f 77 6e 73 22 20 74 68  d that "owns" th
c6e80 69 73 20 75 6e 69 78 46 69 6c 65 20 2a 2f 0a 23  is unixFile */.#
c6e90 65 6e 64 69 66 0a 23 69 66 20 4f 53 5f 56 58 57  endif.#if OS_VXW
c6ea0 4f 52 4b 53 0a 20 20 69 6e 74 20 69 73 44 65 6c  ORKS.  int isDel
c6eb0 65 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ete;            
c6ec0 20 20 20 20 20 20 20 20 2f 2a 20 44 65 6c 65 74          /* Delet
c6ed0 65 20 6f 6e 20 63 6c 6f 73 65 20 69 66 20 74 72  e on close if tr
c6ee0 75 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 76  ue */.  struct v
c6ef0 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 49  xworksFileId *pI
c6f00 64 3b 20 20 20 20 20 20 20 2f 2a 20 55 6e 69 71  d;       /* Uniq
c6f10 75 65 20 66 69 6c 65 20 49 44 20 2a 2f 0a 23 65  ue file ID */.#e
c6f20 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4e 44 45  ndif.#ifndef NDE
c6f30 42 55 47 0a 20 20 2f 2a 20 54 68 65 20 6e 65 78  BUG.  /* The nex
c6f40 74 20 67 72 6f 75 70 20 6f 66 20 76 61 72 69 61  t group of varia
c6f50 62 6c 65 73 20 61 72 65 20 75 73 65 64 20 74 6f  bles are used to
c6f60 20 74 72 61 63 6b 20 77 68 65 74 68 65 72 20 6f   track whether o
c6f70 72 20 6e 6f 74 20 74 68 65 0a 20 20 2a 2a 20 74  r not the.  ** t
c6f80 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74  ransaction count
c6f90 65 72 20 69 6e 20 62 79 74 65 73 20 32 34 2d 32  er in bytes 24-2
c6fa0 37 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69  7 of database fi
c6fb0 6c 65 73 20 61 72 65 20 75 70 64 61 74 65 64 0a  les are updated.
c6fc0 20 20 2a 2a 20 77 68 65 6e 65 76 65 72 20 61 6e    ** whenever an
c6fd0 79 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61  y part of the da
c6fe0 74 61 62 61 73 65 20 63 68 61 6e 67 65 73 2e 20  tabase changes. 
c6ff0 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61   An assertion fa
c7000 75 6c 74 20 77 69 6c 6c 0a 20 20 2a 2a 20 6f 63  ult will.  ** oc
c7010 63 75 72 20 69 66 20 61 20 66 69 6c 65 20 69 73  cur if a file is
c7020 20 75 70 64 61 74 65 64 20 77 69 74 68 6f 75 74   updated without
c7030 20 61 6c 73 6f 20 75 70 64 61 74 69 6e 67 20 74   also updating t
c7040 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20  he transaction. 
c7050 20 2a 2a 20 63 6f 75 6e 74 65 72 2e 20 20 54 68   ** counter.  Th
c7060 69 73 20 74 65 73 74 20 69 73 20 6d 61 64 65 20  is test is made 
c7070 74 6f 20 61 76 6f 69 64 20 6e 65 77 20 70 72 6f  to avoid new pro
c7080 62 6c 65 6d 73 20 73 69 6d 69 6c 61 72 20 74 6f  blems similar to
c7090 20 74 68 65 0a 20 20 2a 2a 20 6f 6e 65 20 64 65   the.  ** one de
c70a0 73 63 72 69 62 65 64 20 62 79 20 74 69 63 6b 65  scribed by ticke
c70b0 74 20 23 33 35 38 34 2e 20 0a 20 20 2a 2f 0a 20  t #3584. .  */. 
c70c0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 74   unsigned char t
c70d0 72 61 6e 73 43 6e 74 72 43 68 6e 67 3b 20 20 20  ransCntrChng;   
c70e0 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 74  /* True if the t
c70f0 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74  ransaction count
c7100 65 72 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20  er changed */.  
c7110 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 64 62  unsigned char db
c7120 55 70 64 61 74 65 3b 20 20 20 20 20 20 20 20 2f  Update;        /
c7130 2a 20 54 72 75 65 20 69 66 20 61 6e 79 20 70 61  * True if any pa
c7140 72 74 20 6f 66 20 64 61 74 61 62 61 73 65 20 66  rt of database f
c7150 69 6c 65 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20  ile changed */. 
c7160 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69   unsigned char i
c7170 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 3b 20 20 20  nNormalWrite;   
c7180 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 20 61 20  /* True if in a 
c7190 6e 6f 72 6d 61 6c 20 77 72 69 74 65 20 6f 70 65  normal write ope
c71a0 72 61 74 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66  ration */.#endif
c71b0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
c71c0 45 53 54 0a 20 20 2f 2a 20 49 6e 20 74 65 73 74  EST.  /* In test
c71d0 20 6d 6f 64 65 2c 20 69 6e 63 72 65 61 73 65 20   mode, increase 
c71e0 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 69 73  the size of this
c71f0 20 73 74 72 75 63 74 75 72 65 20 61 20 62 69 74   structure a bit
c7200 20 73 6f 20 74 68 61 74 20 0a 20 20 2a 2a 20 69   so that .  ** i
c7210 74 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e  t is larger than
c7220 20 74 68 65 20 73 74 72 75 63 74 20 43 72 61 73   the struct Cras
c7230 68 46 69 6c 65 20 64 65 66 69 6e 65 64 20 69 6e  hFile defined in
c7240 20 74 65 73 74 36 2e 63 2e 0a 20 20 2a 2f 0a 20   test6.c..  */. 
c7250 20 63 68 61 72 20 61 50 61 64 64 69 6e 67 5b 33   char aPadding[3
c7260 32 5d 3b 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f  2];.#endif.};../
c7270 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
c7280 6e 67 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65  ng macros define
c7290 20 62 69 74 73 20 69 6e 20 75 6e 69 78 46 69 6c   bits in unixFil
c72a0 65 2e 66 69 6c 65 46 6c 61 67 73 0a 2a 2f 0a 23  e.fileFlags.*/.#
c72b0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 48  define SQLITE_WH
c72c0 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47  OLE_FILE_LOCKING
c72d0 20 20 30 78 30 30 30 31 20 20 20 2f 2a 20 55 73    0x0001   /* Us
c72e0 65 20 77 68 6f 6c 65 2d 66 69 6c 65 20 6c 6f 63  e whole-file loc
c72f0 6b 69 6e 67 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49  king */../*.** I
c7300 6e 63 6c 75 64 65 20 63 6f 64 65 20 74 68 61 74  nclude code that
c7310 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c   is common to al
c7320 6c 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a  l os_*.c files.*
c7330 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
c7340 2a 20 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f 6d  * Include os_com
c7350 6d 6f 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69 64  mon.h in the mid
c7360 64 6c 65 20 6f 66 20 6f 73 5f 75 6e 69 78 2e 63  dle of os_unix.c
c7370 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
c7380 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
c7390 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f  * Begin file os_
c73a0 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a  common.h *******
c73b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c73c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c73d0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79  /./*.** 2004 May
c73e0 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   22.**.** The au
c73f0 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
c7400 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
c7410 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
c7420 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
c7430 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
c7440 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
c7450 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
c7460 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
c7470 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
c7480 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
c7490 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
c74a0 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
c74b0 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
c74c0 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
c74d0 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
c74e0 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
c74f0 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
c7500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c7510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c7520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c7530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c7540 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
c7550 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
c7560 20 6d 61 63 72 6f 73 20 61 6e 64 20 61 20 6c 69   macros and a li
c7570 74 74 6c 65 20 62 69 74 20 6f 66 20 63 6f 64 65  ttle bit of code
c7580 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20   that is common 
c7590 74 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 65  to.** all of the
c75a0 20 70 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69 66   platform-specif
c75b0 69 63 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e 63  ic files (os_*.c
c75c0 29 20 61 6e 64 20 69 73 20 23 69 6e 63 6c 75 64  ) and is #includ
c75d0 65 64 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a  ed into those.**
c75e0 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68   files..**.** Th
c75f0 69 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62  is file should b
c7600 65 20 23 69 6e 63 6c 75 64 65 64 20 62 79 20 74  e #included by t
c7610 68 65 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 20  he os_*.c files 
c7620 6f 6e 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f 74  only.  It is not
c7630 20 61 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70 75   a.** general pu
c7640 72 70 6f 73 65 20 68 65 61 64 65 72 20 66 69 6c  rpose header fil
c7650 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73  e..**.** $Id: os
c7660 5f 63 6f 6d 6d 6f 6e 2e 68 2c 76 20 31 2e 33 38  _common.h,v 1.38
c7670 20 32 30 30 39 2f 30 32 2f 32 34 20 31 38 3a 34   2009/02/24 18:4
c7680 30 3a 35 30 20 64 61 6e 69 65 6c 6b 31 39 37 37  0:50 danielk1977
c7690 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65   Exp $.*/.#ifnde
c76a0 66 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a  f _OS_COMMON_H_.
c76b0 23 64 65 66 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d  #define _OS_COMM
c76c0 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20  ON_H_../*.** At 
c76d0 6c 65 61 73 74 20 74 77 6f 20 62 75 67 73 20 68  least two bugs h
c76e0 61 76 65 20 73 6c 69 70 70 65 64 20 69 6e 20 62  ave slipped in b
c76f0 65 63 61 75 73 65 20 77 65 20 63 68 61 6e 67 65  ecause we change
c7700 64 20 74 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42  d the MEMORY_DEB
c7710 55 47 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f 20 53  UG.** macro to S
c7720 51 4c 49 54 45 5f 44 45 42 55 47 20 61 6e 64 20  QLITE_DEBUG and 
c7730 73 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 6b 65 66  some older makef
c7740 69 6c 65 73 20 68 61 76 65 20 6e 6f 74 20 79 65  iles have not ye
c7750 74 20 6d 61 64 65 20 74 68 65 0a 2a 2a 20 73 77  t made the.** sw
c7760 69 74 63 68 2e 20 20 54 68 65 20 66 6f 6c 6c 6f  itch.  The follo
c7770 77 69 6e 67 20 63 6f 64 65 20 73 68 6f 75 6c 64  wing code should
c7780 20 63 61 74 63 68 20 74 68 69 73 20 70 72 6f 62   catch this prob
c7790 6c 65 6d 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74  lem at compile-t
c77a0 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 4d  ime..*/.#ifdef M
c77b0 45 4d 4f 52 59 5f 44 45 42 55 47 0a 23 20 65 72  EMORY_DEBUG.# er
c77c0 72 6f 72 20 22 54 68 65 20 4d 45 4d 4f 52 59 5f  ror "The MEMORY_
c77d0 44 45 42 55 47 20 6d 61 63 72 6f 20 69 73 20 6f  DEBUG macro is o
c77e0 62 73 6f 6c 65 74 65 2e 20 20 55 73 65 20 53 51  bsolete.  Use SQ
c77f0 4c 49 54 45 5f 44 45 42 55 47 20 69 6e 73 74 65  LITE_DEBUG inste
c7800 61 64 2e 22 0a 23 65 6e 64 69 66 0a 0a 23 69 66  ad.".#endif..#if
c7810 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
c7820 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
c7830 69 6e 74 20 73 71 6c 69 74 65 33 4f 53 54 72 61  int sqlite3OSTra
c7840 63 65 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20  ce = 0;.#define 
c7850 4f 53 54 52 41 43 45 31 28 58 29 20 20 20 20 20  OSTRACE1(X)     
c7860 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f      if( sqlite3O
c7870 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33  STrace ) sqlite3
c7880 44 65 62 75 67 50 72 69 6e 74 66 28 58 29 0a 23  DebugPrintf(X).#
c7890 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28  define OSTRACE2(
c78a0 58 2c 59 29 20 20 20 20 20 20 20 69 66 28 20 73  X,Y)       if( s
c78b0 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20  qlite3OSTrace ) 
c78c0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
c78d0 74 66 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20  tf(X,Y).#define 
c78e0 4f 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 20  OSTRACE3(X,Y,Z) 
c78f0 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f      if( sqlite3O
c7900 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33  STrace ) sqlite3
c7910 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c  DebugPrintf(X,Y,
c7920 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  Z).#define OSTRA
c7930 43 45 34 28 58 2c 59 2c 5a 2c 41 29 20 20 20 69  CE4(X,Y,Z,A)   i
c7940 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63  f( sqlite3OSTrac
c7950 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67  e ) sqlite3Debug
c7960 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 29 0a  Printf(X,Y,Z,A).
c7970 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35  #define OSTRACE5
c7980 28 58 2c 59 2c 5a 2c 41 2c 42 29 20 69 66 28 20  (X,Y,Z,A,B) if( 
c7990 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29  sqlite3OSTrace )
c79a0 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
c79b0 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23  ntf(X,Y,Z,A,B).#
c79c0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28  define OSTRACE6(
c79d0 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20  X,Y,Z,A,B,C) \. 
c79e0 20 20 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54     if(sqlite3OST
c79f0 72 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 62  race) sqlite3Deb
c7a00 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41  ugPrintf(X,Y,Z,A
c7a10 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53  ,B,C).#define OS
c7a20 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42  TRACE7(X,Y,Z,A,B
c7a30 2c 43 2c 44 29 20 5c 0a 20 20 20 20 69 66 28 73  ,C,D) \.    if(s
c7a40 71 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20 73  qlite3OSTrace) s
c7a50 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
c7a60 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29  f(X,Y,Z,A,B,C,D)
c7a70 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f  .#else.#define O
c7a80 53 54 52 41 43 45 31 28 58 29 0a 23 64 65 66 69  STRACE1(X).#defi
c7a90 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29  ne OSTRACE2(X,Y)
c7aa0 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
c7ab0 33 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65  3(X,Y,Z).#define
c7ac0 20 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c   OSTRACE4(X,Y,Z,
c7ad0 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  A).#define OSTRA
c7ae0 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23  CE5(X,Y,Z,A,B).#
c7af0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28  define OSTRACE6(
c7b00 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65  X,Y,Z,A,B,C).#de
c7b10 66 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c  fine OSTRACE7(X,
c7b20 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6e  Y,Z,A,B,C,D).#en
c7b30 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f  dif../*.** Macro
c7b40 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63  s for performanc
c7b50 65 20 74 72 61 63 69 6e 67 2e 20 20 4e 6f 72 6d  e tracing.  Norm
c7b60 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 2e  ally turned off.
c7b70 20 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20    Only works.** 
c7b80 6f 6e 20 69 34 38 36 20 68 61 72 64 77 61 72 65  on i486 hardware
c7b90 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
c7ba0 54 45 5f 50 45 52 46 4f 52 4d 41 4e 43 45 5f 54  TE_PERFORMANCE_T
c7bb0 52 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74  RACE../* .** hwt
c7bc0 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 69  ime.h contains i
c7bd0 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 20  nline assembler 
c7be0 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65  code for impleme
c7bf0 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d 70  nting .** high-p
c7c00 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 6e  erformance timin
c7c10 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f  g routines..*/./
c7c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49  ************** I
c7c30 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 20  nclude hwtime.h 
c7c40 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
c7c50 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a   os_common.h ***
c7c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
c7c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
c7c80 65 67 69 6e 20 66 69 6c 65 20 68 77 74 69 6d 65  egin file hwtime
c7c90 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
c7ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c7cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
c7cc0 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 37  *.** 2008 May 27
c7cd0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
c7ce0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
c7cf0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
c7d00 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
c7d10 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
c7d20 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
c7d30 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
c7d40 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
c7d50 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
c7d60 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
c7d70 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
c7d80 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
c7d90 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
c7da0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
c7db0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
c7dc0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
c7dd0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
c7de0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
c7df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c7e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c7e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c7e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c7e30 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ****.**.** This 
c7e40 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e  file contains in
c7e50 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 6f  line asm code fo
c7e60 72 20 72 65 74 72 69 65 76 69 6e 67 20 22 68 69  r retrieving "hi
c7e70 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 0a  gh-performance".
c7e80 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 20  ** counters for 
c7e90 78 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e 0a  x86 class CPUs..
c7ea0 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 77 74 69 6d  **.** $Id: hwtim
c7eb0 65 2e 68 2c 76 20 31 2e 33 20 32 30 30 38 2f 30  e.h,v 1.3 2008/0
c7ec0 38 2f 30 31 20 31 34 3a 33 33 3a 31 35 20 73 68  8/01 14:33:15 sh
c7ed0 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66  ane Exp $.*/.#if
c7ee0 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f 48 5f 0a  ndef _HWTIME_H_.
c7ef0 23 64 65 66 69 6e 65 20 5f 48 57 54 49 4d 45 5f  #define _HWTIME_
c7f00 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  H_../*.** The fo
c7f10 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20  llowing routine 
c7f20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70 65  only works on pe
c7f30 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72 20  ntium-class (or 
c7f40 6e 65 77 65 72 29 20 70 72 6f 63 65 73 73 6f 72  newer) processor
c7f50 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20 74 68  s..** It uses th
c7f60 65 20 52 44 54 53 43 20 6f 70 63 6f 64 65 20 74  e RDTSC opcode t
c7f70 6f 20 72 65 61 64 20 74 68 65 20 63 79 63 6c 65  o read the cycle
c7f80 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75 74   count value out
c7f90 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63 65   of the.** proce
c7fa0 73 73 6f 72 20 61 6e 64 20 72 65 74 75 72 6e 73  ssor and returns
c7fb0 20 74 68 61 74 20 76 61 6c 75 65 2e 20 20 54 68   that value.  Th
c7fc0 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 66  is can be used f
c7fd0 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20 70  or high-res.** p
c7fe0 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66  rofiling..*/.#if
c7ff0 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43   (defined(__GNUC
c8000 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f  __) || defined(_
c8010 4d 53 43 5f 56 45 52 29 29 20 26 26 20 5c 0a 20  MSC_VER)) && \. 
c8020 20 20 20 20 20 28 64 65 66 69 6e 65 64 28 69 33       (defined(i3
c8030 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f  86) || defined(_
c8040 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69  _i386__) || defi
c8050 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29 0a 0a 20  ned(_M_IX86)).. 
c8060 20 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 47   #if defined(__G
c8070 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69  NUC__)..  __inli
c8080 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74  ne__ sqlite_uint
c8090 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65  64 sqlite3Hwtime
c80a0 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 75 6e 73  (void){.     uns
c80b0 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 68 69  igned int lo, hi
c80c0 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f  ;.     __asm__ _
c80d0 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64  _volatile__ ("rd
c80e0 74 73 63 22 20 3a 20 22 3d 61 22 20 28 6c 6f 29  tsc" : "=a" (lo)
c80f0 2c 20 22 3d 64 22 20 28 68 69 29 29 3b 0a 20 20  , "=d" (hi));.  
c8100 20 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74     return (sqlit
c8110 65 5f 75 69 6e 74 36 34 29 68 69 20 3c 3c 20 33  e_uint64)hi << 3
c8120 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23  2 | lo;.  }..  #
c8130 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53  elif defined(_MS
c8140 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 65 63 6c  C_VER)..  __decl
c8150 73 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f 69 6e  spec(naked) __in
c8160 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 69 6e 74  line sqlite_uint
c8170 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c 69 74  64 __cdecl sqlit
c8180 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a  e3Hwtime(void){.
c8190 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 20 20       __asm {.   
c81a0 20 20 20 20 20 72 64 74 73 63 0a 20 20 20 20 20       rdtsc.     
c81b0 20 20 20 72 65 74 20 20 20 20 20 20 20 3b 20 72     ret       ; r
c81c0 65 74 75 72 6e 20 76 61 6c 75 65 20 61 74 20 45  eturn value at E
c81d0 44 58 3a 45 41 58 0a 20 20 20 20 20 7d 0a 20 20  DX:EAX.     }.  
c81e0 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a 23 65 6c  }..  #endif..#el
c81f0 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e  if (defined(__GN
c8200 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64  UC__) && defined
c8210 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 0a 0a 20  (__x86_64__)).. 
c8220 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69   __inline__ sqli
c8230 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65  te_uint64 sqlite
c8240 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20  3Hwtime(void){. 
c8250 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f       unsigned lo
c8260 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20 20 5f 5f  ng val;.      __
c8270 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65  asm__ __volatile
c8280 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d  __ ("rdtsc" : "=
c8290 41 22 20 28 76 61 6c 29 29 3b 0a 20 20 20 20 20  A" (val));.     
c82a0 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d   return val;.  }
c82b0 0a 20 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65  . .#elif (define
c82c0 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64  d(__GNUC__) && d
c82d0 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f 5f 29 29  efined(__ppc__))
c82e0 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73  ..  __inline__ s
c82f0 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c  qlite_uint64 sql
c8300 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29  ite3Hwtime(void)
c8310 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64  {.      unsigned
c8320 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 76 61   long long retva
c8330 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65  l;.      unsigne
c8340 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20  d long junk;.   
c8350 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c     __asm__ __vol
c8360 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20  atile__ ("\n\.  
c8370 20 20 20 20 20 20 20 20 31 3a 20 20 20 20 20 20          1:      
c8380 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c 0a 20 20  mftbu   %1\n\.  
c8390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c83a0 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e 5c 0a 20  mftb    %L0\n\. 
c83b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c83c0 20 6d 66 74 62 75 20 20 20 25 30 5c 6e 5c 0a 20   mftbu   %0\n\. 
c83d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c83e0 20 63 6d 70 77 20 20 20 20 25 30 2c 25 31 5c 6e   cmpw    %0,%1\n
c83f0 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  \.              
c8400 20 20 20 20 62 6e 65 20 20 20 20 20 31 62 22 0a      bne     1b".
c8410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c8420 20 20 3a 20 22 3d 72 22 20 28 72 65 74 76 61 6c    : "=r" (retval
c8430 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29 3b  ), "=r" (junk));
c8440 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 65  .      return re
c8450 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65  tval;.  }..#else
c8460 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65 65 64 20  ..  #error Need 
c8470 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
c8480 66 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  f sqlite3Hwtime(
c8490 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66  ) for your platf
c84a0 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  orm...  /*.  ** 
c84b0 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 6f  To compile witho
c84c0 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20  ut implementing 
c84d0 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20  sqlite3Hwtime() 
c84e0 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72  for your platfor
c84f0 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 61 6e 20  m,.  ** you can 
c8500 72 65 6d 6f 76 65 20 74 68 65 20 61 62 6f 76 65  remove the above
c8510 20 23 65 72 72 6f 72 20 61 6e 64 20 75 73 65 20   #error and use 
c8520 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20  the following.  
c8530 2a 2a 20 73 74 75 62 20 66 75 6e 63 74 69 6f 6e  ** stub function
c8540 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c 6f 73 65  .  You will lose
c8550 20 74 69 6d 69 6e 67 20 73 75 70 70 6f 72 74 20   timing support 
c8560 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66  for many.  ** of
c8570 20 74 68 65 20 64 65 62 75 67 67 69 6e 67 20 61   the debugging a
c8580 6e 64 20 74 65 73 74 69 6e 67 20 75 74 69 6c 69  nd testing utili
c8590 74 69 65 73 2c 20 62 75 74 20 69 74 20 73 68 6f  ties, but it sho
c85a0 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c 65 61 73  uld at.  ** leas
c85b0 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75  t compile and ru
c85c0 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  n..  */.SQLITE_P
c85d0 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 65 5f  RIVATE   sqlite_
c85e0 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77  uint64 sqlite3Hw
c85f0 74 69 6d 65 28 76 6f 69 64 29 7b 20 72 65 74 75  time(void){ retu
c8600 72 6e 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74  rn ((sqlite_uint
c8610 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 69 66  64)0); }..#endif
c8620 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  ..#endif /* !def
c8630 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f 29  ined(_HWTIME_H_)
c8640 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
c8650 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 77 74 69  **** End of hwti
c8660 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  me.h ***********
c8670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8690 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
c86a0 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
c86b0 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
c86c0 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68  f in os_common.h
c86d0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
c86e0 2a 2a 2a 2f 0a 0a 73 74 61 74 69 63 20 73 71 6c  ***/..static sql
c86f0 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 73 74 61  ite_uint64 g_sta
c8700 72 74 3b 0a 73 74 61 74 69 63 20 73 71 6c 69 74  rt;.static sqlit
c8710 65 5f 75 69 6e 74 36 34 20 67 5f 65 6c 61 70 73  e_uint64 g_elaps
c8720 65 64 3b 0a 23 64 65 66 69 6e 65 20 54 49 4d 45  ed;.#define TIME
c8730 52 5f 53 54 41 52 54 20 20 20 20 20 20 20 67 5f  R_START       g_
c8740 73 74 61 72 74 3d 73 71 6c 69 74 65 33 48 77 74  start=sqlite3Hwt
c8750 69 6d 65 28 29 0a 23 64 65 66 69 6e 65 20 54 49  ime().#define TI
c8760 4d 45 52 5f 45 4e 44 20 20 20 20 20 20 20 20 20  MER_END         
c8770 67 5f 65 6c 61 70 73 65 64 3d 73 71 6c 69 74 65  g_elapsed=sqlite
c8780 33 48 77 74 69 6d 65 28 29 2d 67 5f 73 74 61 72  3Hwtime()-g_star
c8790 74 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f  t.#define TIMER_
c87a0 45 4c 41 50 53 45 44 20 20 20 20 20 67 5f 65 6c  ELAPSED     g_el
c87b0 61 70 73 65 64 0a 23 65 6c 73 65 0a 23 64 65 66  apsed.#else.#def
c87c0 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 0a  ine TIMER_START.
c87d0 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e  #define TIMER_EN
c87e0 44 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f  D.#define TIMER_
c87f0 45 4c 41 50 53 45 44 20 20 20 20 20 28 28 73 71  ELAPSED     ((sq
c8800 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29 0a 23  lite_uint64)0).#
c8810 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  endif../*.** If 
c8820 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20  we compile with 
c8830 74 68 65 20 53 51 4c 49 54 45 5f 54 45 53 54 20  the SQLITE_TEST 
c8840 6d 61 63 72 6f 20 73 65 74 2c 20 74 68 65 6e 20  macro set, then 
c8850 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c  the following bl
c8860 6f 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64 65 20 77  ock.** of code w
c8870 69 6c 6c 20 67 69 76 65 20 75 73 20 74 68 65 20  ill give us the 
c8880 61 62 69 6c 69 74 79 20 74 6f 20 73 69 6d 75 6c  ability to simul
c8890 61 74 65 20 61 20 64 69 73 6b 20 49 2f 4f 20 65  ate a disk I/O e
c88a0 72 72 6f 72 2e 20 20 54 68 69 73 0a 2a 2a 20 69  rror.  This.** i
c88b0 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69  s used for testi
c88c0 6e 67 20 74 68 65 20 49 2f 4f 20 72 65 63 6f 76  ng the I/O recov
c88d0 65 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69  ery logic..*/.#i
c88e0 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
c88f0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
c8900 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
c8910 5f 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20  _hit = 0;       
c8920 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75       /* Total nu
c8930 6d 62 65 72 20 6f 66 20 49 2f 4f 20 45 72 72 6f  mber of I/O Erro
c8940 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  rs */.SQLITE_API
c8950 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f   int sqlite3_io_
c8960 65 72 72 6f 72 5f 68 61 72 64 68 69 74 20 3d 20  error_hardhit = 
c8970 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  0;        /* Num
c8980 62 65 72 20 6f 66 20 6e 6f 6e 2d 62 65 6e 69 67  ber of non-benig
c8990 6e 20 65 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49  n errors */.SQLI
c89a0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
c89b0 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64  e3_io_error_pend
c89c0 69 6e 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ing = 0;        
c89d0 2f 2a 20 43 6f 75 6e 74 20 64 6f 77 6e 20 74 6f  /* Count down to
c89e0 20 66 69 72 73 74 20 49 2f 4f 20 65 72 72 6f 72   first I/O error
c89f0 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   */.SQLITE_API i
c8a00 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  nt sqlite3_io_er
c8a10 72 6f 72 5f 70 65 72 73 69 73 74 20 3d 20 30 3b  ror_persist = 0;
c8a20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
c8a30 69 66 20 49 2f 4f 20 65 72 72 6f 72 73 20 70 65  if I/O errors pe
c8a40 72 73 69 73 74 20 2a 2f 0a 53 51 4c 49 54 45 5f  rsist */.SQLITE_
c8a50 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
c8a60 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20  io_error_benign 
c8a70 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  = 0;         /* 
c8a80 54 72 75 65 20 69 66 20 65 72 72 6f 72 73 20 61  True if errors a
c8a90 72 65 20 62 65 6e 69 67 6e 20 2a 2f 0a 53 51 4c  re benign */.SQL
c8aa0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
c8ab0 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e  te3_diskfull_pen
c8ac0 64 69 6e 67 20 3d 20 30 3b 0a 53 51 4c 49 54 45  ding = 0;.SQLITE
c8ad0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
c8ae0 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23  _diskfull = 0;.#
c8af0 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49  define SimulateI
c8b00 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 20  OErrorBenign(X) 
c8b10 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
c8b20 5f 62 65 6e 69 67 6e 3d 28 58 29 0a 23 64 65 66  _benign=(X).#def
c8b30 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ine SimulateIOEr
c8b40 72 6f 72 28 43 4f 44 45 29 20 20 5c 0a 20 20 69  ror(CODE)  \.  i
c8b50 66 28 20 28 73 71 6c 69 74 65 33 5f 69 6f 5f 65  f( (sqlite3_io_e
c8b60 72 72 6f 72 5f 70 65 72 73 69 73 74 20 26 26 20  rror_persist && 
c8b70 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
c8b80 5f 68 69 74 29 20 5c 0a 20 20 20 20 20 20 20 7c  _hit) \.       |
c8b90 7c 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  | sqlite3_io_err
c8ba0 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20  or_pending-- == 
c8bb0 31 20 29 20 20 5c 0a 20 20 20 20 20 20 20 20 20  1 )  \.         
c8bc0 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65       { local_ioe
c8bd0 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0a 73 74  rr(); CODE; }.st
c8be0 61 74 69 63 20 76 6f 69 64 20 6c 6f 63 61 6c 5f  atic void local_
c8bf0 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f 54 52 41  ioerr(){.  IOTRA
c8c00 43 45 28 28 22 49 4f 45 52 52 5c 6e 22 29 29 3b  CE(("IOERR\n"));
c8c10 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  .  sqlite3_io_er
c8c20 72 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20 69 66 28  ror_hit++;.  if(
c8c30 20 21 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72   !sqlite3_io_err
c8c40 6f 72 5f 62 65 6e 69 67 6e 20 29 20 73 71 6c 69  or_benign ) sqli
c8c50 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72  te3_io_error_har
c8c60 64 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65 66 69 6e  dhit++;.}.#defin
c8c70 65 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75  e SimulateDiskfu
c8c80 6c 6c 45 72 72 6f 72 28 43 4f 44 45 29 20 5c 0a  llError(CODE) \.
c8c90 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64     if( sqlite3_d
c8ca0 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20  iskfull_pending 
c8cb0 29 7b 20 5c 0a 20 20 20 20 20 69 66 28 20 73 71  ){ \.     if( sq
c8cc0 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70  lite3_diskfull_p
c8cd0 65 6e 64 69 6e 67 20 3d 3d 20 31 20 29 7b 20 5c  ending == 1 ){ \
c8ce0 0a 20 20 20 20 20 20 20 6c 6f 63 61 6c 5f 69 6f  .       local_io
c8cf0 65 72 72 28 29 3b 20 5c 0a 20 20 20 20 20 20 20  err(); \.       
c8d00 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c  sqlite3_diskfull
c8d10 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 73   = 1; \.       s
c8d20 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
c8d30 68 69 74 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20  hit = 1; \.     
c8d40 20 20 43 4f 44 45 3b 20 5c 0a 20 20 20 20 20 7d    CODE; \.     }
c8d50 65 6c 73 65 7b 20 5c 0a 20 20 20 20 20 20 20 73  else{ \.       s
c8d60 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f  qlite3_diskfull_
c8d70 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a 20 20 20  pending--; \.   
c8d80 20 20 7d 20 5c 0a 20 20 20 7d 0a 23 65 6c 73 65    } \.   }.#else
c8d90 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74  .#define Simulat
c8da0 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58  eIOErrorBenign(X
c8db0 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61  ).#define Simula
c8dc0 74 65 49 4f 45 72 72 6f 72 28 41 29 0a 23 64 65  teIOError(A).#de
c8dd0 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73  fine SimulateDis
c8de0 6b 66 75 6c 6c 45 72 72 6f 72 28 41 29 0a 23 65  kfullError(A).#e
c8df0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e  ndif../*.** When
c8e00 20 74 65 73 74 69 6e 67 2c 20 6b 65 65 70 20 61   testing, keep a
c8e10 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e 75   count of the nu
c8e20 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20 66 69 6c  mber of open fil
c8e30 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  es..*/.#ifdef SQ
c8e40 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
c8e50 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
c8e60 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74  _open_file_count
c8e70 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 70   = 0;.#define Op
c8e80 65 6e 43 6f 75 6e 74 65 72 28 58 29 20 20 73 71  enCounter(X)  sq
c8e90 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f  lite3_open_file_
c8ea0 63 6f 75 6e 74 2b 3d 28 58 29 0a 23 65 6c 73 65  count+=(X).#else
c8eb0 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75  .#define OpenCou
c8ec0 6e 74 65 72 28 58 29 0a 23 65 6e 64 69 66 0a 0a  nter(X).#endif..
c8ed0 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e  #endif /* !defin
c8ee0 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f  ed(_OS_COMMON_H_
c8ef0 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ) */../*********
c8f00 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f  ***** End of os_
c8f10 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a  common.h *******
c8f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8f40 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
c8f50 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
c8f60 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
c8f70 66 66 20 69 6e 20 6f 73 5f 75 6e 69 78 2e 63 20  ff in os_unix.c 
c8f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8f90 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 44 65 66  ****/../*.** Def
c8fa0 69 6e 65 20 76 61 72 69 6f 75 73 20 6d 61 63 72  ine various macr
c8fb0 6f 73 20 74 68 61 74 20 61 72 65 20 6d 69 73 73  os that are miss
c8fc0 69 6e 67 20 66 72 6f 6d 20 73 6f 6d 65 20 73 79  ing from some sy
c8fd0 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65  stems..*/.#ifnde
c8fe0 66 20 4f 5f 4c 41 52 47 45 46 49 4c 45 0a 23 20  f O_LARGEFILE.# 
c8ff0 64 65 66 69 6e 65 20 4f 5f 4c 41 52 47 45 46 49  define O_LARGEFI
c9000 4c 45 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 64  LE 0.#endif.#ifd
c9010 65 66 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c  ef SQLITE_DISABL
c9020 45 5f 4c 46 53 0a 23 20 75 6e 64 65 66 20 4f 5f  E_LFS.# undef O_
c9030 4c 41 52 47 45 46 49 4c 45 0a 23 20 64 65 66 69  LARGEFILE.# defi
c9040 6e 65 20 4f 5f 4c 41 52 47 45 46 49 4c 45 20 30  ne O_LARGEFILE 0
c9050 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20  .#endif.#ifndef 
c9060 4f 5f 4e 4f 46 4f 4c 4c 4f 57 0a 23 20 64 65 66  O_NOFOLLOW.# def
c9070 69 6e 65 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 20 30  ine O_NOFOLLOW 0
c9080 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20  .#endif.#ifndef 
c9090 4f 5f 42 49 4e 41 52 59 0a 23 20 64 65 66 69 6e  O_BINARY.# defin
c90a0 65 20 4f 5f 42 49 4e 41 52 59 20 30 0a 23 65 6e  e O_BINARY 0.#en
c90b0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 44  dif../*.** The D
c90c0 4a 47 50 50 20 63 6f 6d 70 69 6c 65 72 20 65 6e  JGPP compiler en
c90d0 76 69 72 6f 6e 6d 65 6e 74 20 6c 6f 6f 6b 73 20  vironment looks 
c90e0 6d 6f 73 74 6c 79 20 6c 69 6b 65 20 55 6e 69 78  mostly like Unix
c90f0 2c 20 62 75 74 20 69 74 0a 2a 2a 20 6c 61 63 6b  , but it.** lack
c9100 73 20 74 68 65 20 66 63 6e 74 6c 28 29 20 73 79  s the fcntl() sy
c9110 73 74 65 6d 20 63 61 6c 6c 2e 20 20 53 6f 20 72  stem call.  So r
c9120 65 64 65 66 69 6e 65 20 66 63 6e 74 6c 28 29 20  edefine fcntl() 
c9130 74 6f 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 0a  to be something.
c9140 2a 2a 20 74 68 61 74 20 61 6c 77 61 79 73 20 73  ** that always s
c9150 75 63 63 65 65 64 73 2e 20 20 54 68 69 73 20 6d  ucceeds.  This m
c9160 65 61 6e 73 20 74 68 61 74 20 6c 6f 63 6b 69 6e  eans that lockin
c9170 67 20 64 6f 65 73 20 6e 6f 74 20 6f 63 63 75 72  g does not occur
c9180 20 75 6e 64 65 72 0a 2a 2a 20 44 4a 47 50 50 2e   under.** DJGPP.
c9190 20 20 42 75 74 20 69 74 20 69 73 20 44 4f 53 20    But it is DOS 
c91a0 2d 20 77 68 61 74 20 64 69 64 20 79 6f 75 20 65  - what did you e
c91b0 78 70 65 63 74 3f 0a 2a 2f 0a 23 69 66 64 65 66  xpect?.*/.#ifdef
c91c0 20 5f 5f 44 4a 47 50 50 5f 5f 0a 23 20 64 65 66   __DJGPP__.# def
c91d0 69 6e 65 20 66 63 6e 74 6c 28 41 2c 42 2c 43 29  ine fcntl(A,B,C)
c91e0 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a   0.#endif../*.**
c91f0 20 54 68 65 20 74 68 72 65 61 64 69 64 20 6d 61   The threadid ma
c9200 63 72 6f 20 72 65 73 6f 6c 76 65 73 20 74 6f 20  cro resolves to 
c9210 74 68 65 20 74 68 72 65 61 64 2d 69 64 20 6f 72  the thread-id or
c9220 20 74 6f 20 30 2e 20 20 55 73 65 64 20 66 6f 72   to 0.  Used for
c9230 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 20  .** testing and 
c9240 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a  debugging only..
c9250 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  */.#if SQLITE_TH
c9260 52 45 41 44 53 41 46 45 0a 23 64 65 66 69 6e 65  READSAFE.#define
c9270 20 74 68 72 65 61 64 69 64 20 70 74 68 72 65 61   threadid pthrea
c9280 64 5f 73 65 6c 66 28 29 0a 23 65 6c 73 65 0a 23  d_self().#else.#
c9290 64 65 66 69 6e 65 20 74 68 72 65 61 64 69 64 20  define threadid 
c92a0 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  0.#endif.../*.**
c92b0 20 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e   Helper function
c92c0 73 20 74 6f 20 6f 62 74 61 69 6e 20 61 6e 64 20  s to obtain and 
c92d0 72 65 6c 69 6e 71 75 69 73 68 20 74 68 65 20 67  relinquish the g
c92e0 6c 6f 62 61 6c 20 6d 75 74 65 78 2e 20 54 68 65  lobal mutex. The
c92f0 0a 2a 2a 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78  .** global mutex
c9300 20 69 73 20 75 73 65 64 20 74 6f 20 70 72 6f 74   is used to prot
c9310 65 63 74 20 74 68 65 20 75 6e 69 78 4f 70 65 6e  ect the unixOpen
c9320 43 6e 74 2c 20 75 6e 69 78 4c 6f 63 6b 49 6e 66  Cnt, unixLockInf
c9330 6f 20 61 6e 64 0a 2a 2a 20 76 78 77 6f 72 6b 73  o and.** vxworks
c9340 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 73 20 75  FileId objects u
c9350 73 65 64 20 62 79 20 74 68 69 73 20 66 69 6c 65  sed by this file
c9360 2c 20 61 6c 6c 20 6f 66 20 77 68 69 63 68 20 6d  , all of which m
c9370 61 79 20 62 65 20 0a 2a 2a 20 73 68 61 72 65 64  ay be .** shared
c9380 20 62 79 20 6d 75 6c 74 69 70 6c 65 20 74 68 72   by multiple thr
c9390 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 46 75 6e 63  eads..**.** Func
c93a0 74 69 6f 6e 20 75 6e 69 78 4d 75 74 65 78 48 65  tion unixMutexHe
c93b0 6c 64 28 29 20 69 73 20 75 73 65 64 20 74 6f 20  ld() is used to 
c93c0 61 73 73 65 72 74 28 29 20 74 68 61 74 20 74 68  assert() that th
c93d0 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 0a  e global mutex .
c93e0 2a 2a 20 69 73 20 68 65 6c 64 20 77 68 65 6e 20  ** is held when 
c93f0 72 65 71 75 69 72 65 64 2e 20 54 68 69 73 20 66  required. This f
c9400 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20  unction is only 
c9410 75 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20  used as part of 
c9420 61 73 73 65 72 74 28 29 20 0a 2a 2a 20 73 74 61  assert() .** sta
c9430 74 65 6d 65 6e 74 73 2e 20 65 2e 67 2e 0a 2a 2a  tements. e.g..**
c9440 0a 2a 2a 20 20 20 75 6e 69 78 45 6e 74 65 72 4d  .**   unixEnterM
c9450 75 74 65 78 28 29 0a 2a 2a 20 20 20 20 20 61 73  utex().**     as
c9460 73 65 72 74 28 20 75 6e 69 78 4d 75 74 65 78 48  sert( unixMutexH
c9470 65 6c 64 28 29 20 29 3b 0a 2a 2a 20 20 20 75 6e  eld() );.**   un
c9480 69 78 45 6e 74 65 72 4c 65 61 76 65 28 29 0a 2a  ixEnterLeave().*
c9490 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e  /.static void un
c94a0 69 78 45 6e 74 65 72 4d 75 74 65 78 28 76 6f 69  ixEnterMutex(voi
c94b0 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  d){.  sqlite3_mu
c94c0 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65  tex_enter(sqlite
c94d0 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49  3MutexAlloc(SQLI
c94e0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
c94f0 4d 41 53 54 45 52 29 29 3b 0a 7d 0a 73 74 61 74  MASTER));.}.stat
c9500 69 63 20 76 6f 69 64 20 75 6e 69 78 4c 65 61 76  ic void unixLeav
c9510 65 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20  eMutex(void){.  
c9520 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
c9530 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78  ave(sqlite3Mutex
c9540 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
c9550 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
c9560 29 29 3b 0a 7d 0a 23 69 66 64 65 66 20 53 51 4c  ));.}.#ifdef SQL
c9570 49 54 45 5f 44 45 42 55 47 0a 73 74 61 74 69 63  ITE_DEBUG.static
c9580 20 69 6e 74 20 75 6e 69 78 4d 75 74 65 78 48 65   int unixMutexHe
c9590 6c 64 28 76 6f 69 64 29 20 7b 0a 20 20 72 65 74  ld(void) {.  ret
c95a0 75 72 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  urn sqlite3_mute
c95b0 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 4d 75  x_held(sqlite3Mu
c95c0 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  texAlloc(SQLITE_
c95d0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53  MUTEX_STATIC_MAS
c95e0 54 45 52 29 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  TER));.}.#endif.
c95f0 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
c9600 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 48 65 6c 70  DEBUG./*.** Help
c9610 65 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20  er function for 
c9620 70 72 69 6e 74 69 6e 67 20 6f 75 74 20 74 72 61  printing out tra
c9630 63 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  ce information f
c9640 72 6f 6d 20 64 65 62 75 67 67 69 6e 67 0a 2a 2a  rom debugging.**
c9650 20 62 69 6e 61 72 69 65 73 2e 20 54 68 69 73 20   binaries. This 
c9660 72 65 74 75 72 6e 73 20 74 68 65 20 73 74 72 69  returns the stri
c9670 6e 67 20 72 65 70 72 65 73 65 74 61 74 69 6f 6e  ng represetation
c9680 20 6f 66 20 74 68 65 20 73 75 70 70 6c 69 65 64   of the supplied
c9690 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6c 6f 63 6b  .** integer lock
c96a0 2d 74 79 70 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  -type..*/.static
c96b0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 6c 6f 63   const char *loc
c96c0 6b 74 79 70 65 4e 61 6d 65 28 69 6e 74 20 6c 6f  ktypeName(int lo
c96d0 63 6b 74 79 70 65 29 7b 0a 20 20 73 77 69 74 63  cktype){.  switc
c96e0 68 28 20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20  h( locktype ){. 
c96f0 20 20 20 63 61 73 65 20 4e 4f 5f 4c 4f 43 4b 3a     case NO_LOCK:
c9700 20 72 65 74 75 72 6e 20 22 4e 4f 4e 45 22 3b 0a   return "NONE";.
c9710 20 20 20 20 63 61 73 65 20 53 48 41 52 45 44 5f      case SHARED_
c9720 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 53 48  LOCK: return "SH
c9730 41 52 45 44 22 3b 0a 20 20 20 20 63 61 73 65 20  ARED";.    case 
c9740 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3a 20 72  RESERVED_LOCK: r
c9750 65 74 75 72 6e 20 22 52 45 53 45 52 56 45 44 22  eturn "RESERVED"
c9760 3b 0a 20 20 20 20 63 61 73 65 20 50 45 4e 44 49  ;.    case PENDI
c9770 4e 47 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20  NG_LOCK: return 
c9780 22 50 45 4e 44 49 4e 47 22 3b 0a 20 20 20 20 63  "PENDING";.    c
c9790 61 73 65 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f  ase EXCLUSIVE_LO
c97a0 43 4b 3a 20 72 65 74 75 72 6e 20 22 45 58 43 4c  CK: return "EXCL
c97b0 55 53 49 56 45 22 3b 0a 20 20 7d 0a 20 20 72 65  USIVE";.  }.  re
c97c0 74 75 72 6e 20 22 45 52 52 4f 52 22 3b 0a 7d 0a  turn "ERROR";.}.
c97d0 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
c97e0 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 45  QLITE_LOCK_TRACE
c97f0 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 6f 75 74  ./*.** Print out
c9800 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
c9810 75 74 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 6f  ut all locking o
c9820 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a  perations..**.**
c9830 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
c9840 20 75 73 65 64 20 66 6f 72 20 74 72 6f 75 62 6c   used for troubl
c9850 65 73 68 6f 6f 74 69 6e 67 20 6c 6f 63 6b 73 20  eshooting locks 
c9860 6f 6e 20 6d 75 6c 74 69 74 68 72 65 61 64 65 64  on multithreaded
c9870 0a 2a 2a 20 70 6c 61 74 66 6f 72 6d 73 2e 20 20  .** platforms.  
c9880 45 6e 61 62 6c 65 20 62 79 20 63 6f 6d 70 69 6c  Enable by compil
c9890 69 6e 67 20 77 69 74 68 20 74 68 65 20 2d 44 53  ing with the -DS
c98a0 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 45  QLITE_LOCK_TRACE
c98b0 0a 2a 2a 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65  .** command-line
c98c0 20 6f 70 74 69 6f 6e 20 6f 6e 20 74 68 65 20 63   option on the c
c98d0 6f 6d 70 69 6c 65 72 2e 20 20 54 68 69 73 20 63  ompiler.  This c
c98e0 6f 64 65 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 0a  ode is normally.
c98f0 2a 2a 20 74 75 72 6e 65 64 20 6f 66 66 2e 0a 2a  ** turned off..*
c9900 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 63  /.static int loc
c9910 6b 54 72 61 63 65 28 69 6e 74 20 66 64 2c 20 69  kTrace(int fd, i
c9920 6e 74 20 6f 70 2c 20 73 74 72 75 63 74 20 66 6c  nt op, struct fl
c9930 6f 63 6b 20 2a 70 29 7b 0a 20 20 63 68 61 72 20  ock *p){.  char 
c9940 2a 7a 4f 70 4e 61 6d 65 2c 20 2a 7a 54 79 70 65  *zOpName, *zType
c9950 3b 0a 20 20 69 6e 74 20 73 3b 0a 20 20 69 6e 74  ;.  int s;.  int
c9960 20 73 61 76 65 64 45 72 72 6e 6f 3b 0a 20 20 69   savedErrno;.  i
c9970 66 28 20 6f 70 3d 3d 46 5f 47 45 54 4c 4b 20 29  f( op==F_GETLK )
c9980 7b 0a 20 20 20 20 7a 4f 70 4e 61 6d 65 20 3d 20  {.    zOpName = 
c9990 22 47 45 54 4c 4b 22 3b 0a 20 20 7d 65 6c 73 65  "GETLK";.  }else
c99a0 20 69 66 28 20 6f 70 3d 3d 46 5f 53 45 54 4c 4b   if( op==F_SETLK
c99b0 20 29 7b 0a 20 20 20 20 7a 4f 70 4e 61 6d 65 20   ){.    zOpName 
c99c0 3d 20 22 53 45 54 4c 4b 22 3b 0a 20 20 7d 65 6c  = "SETLK";.  }el
c99d0 73 65 7b 0a 20 20 20 20 73 20 3d 20 66 63 6e 74  se{.    s = fcnt
c99e0 6c 28 66 64 2c 20 6f 70 2c 20 70 29 3b 0a 20 20  l(fd, op, p);.  
c99f0 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
c9a00 69 6e 74 66 28 22 66 63 6e 74 6c 20 75 6e 6b 6e  intf("fcntl unkn
c9a10 6f 77 6e 20 25 64 20 25 64 20 25 64 5c 6e 22 2c  own %d %d %d\n",
c9a20 20 66 64 2c 20 6f 70 2c 20 73 29 3b 0a 20 20 20   fd, op, s);.   
c9a30 20 72 65 74 75 72 6e 20 73 3b 0a 20 20 7d 0a 20   return s;.  }. 
c9a40 20 69 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d   if( p->l_type==
c9a50 46 5f 52 44 4c 43 4b 20 29 7b 0a 20 20 20 20 7a  F_RDLCK ){.    z
c9a60 54 79 70 65 20 3d 20 22 52 44 4c 43 4b 22 3b 0a  Type = "RDLCK";.
c9a70 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 6c    }else if( p->l
c9a80 5f 74 79 70 65 3d 3d 46 5f 57 52 4c 43 4b 20 29  _type==F_WRLCK )
c9a90 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 22 57  {.    zType = "W
c9aa0 52 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 20 69  RLCK";.  }else i
c9ab0 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f  f( p->l_type==F_
c9ac0 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 7a 54 79  UNLCK ){.    zTy
c9ad0 70 65 20 3d 20 22 55 4e 4c 43 4b 22 3b 0a 20 20  pe = "UNLCK";.  
c9ae0 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72  }else{.    asser
c9af0 74 28 20 30 20 29 3b 0a 20 20 7d 0a 20 20 61 73  t( 0 );.  }.  as
c9b00 73 65 72 74 28 20 70 2d 3e 6c 5f 77 68 65 6e 63  sert( p->l_whenc
c9b10 65 3d 3d 53 45 45 4b 5f 53 45 54 20 29 3b 0a 20  e==SEEK_SET );. 
c9b20 20 73 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20 6f   s = fcntl(fd, o
c9b30 70 2c 20 70 29 3b 0a 20 20 73 61 76 65 64 45 72  p, p);.  savedEr
c9b40 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 73  rno = errno;.  s
c9b50 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
c9b60 66 28 22 66 63 6e 74 6c 20 25 64 20 25 64 20 25  f("fcntl %d %d %
c9b70 73 20 25 73 20 25 64 20 25 64 20 25 64 20 25 64  s %s %d %d %d %d
c9b80 5c 6e 22 2c 0a 20 20 20 20 20 74 68 72 65 61 64  \n",.     thread
c9b90 69 64 2c 20 66 64 2c 20 7a 4f 70 4e 61 6d 65 2c  id, fd, zOpName,
c9ba0 20 7a 54 79 70 65 2c 20 28 69 6e 74 29 70 2d 3e   zType, (int)p->
c9bb0 6c 5f 73 74 61 72 74 2c 20 28 69 6e 74 29 70 2d  l_start, (int)p-
c9bc0 3e 6c 5f 6c 65 6e 2c 0a 20 20 20 20 20 28 69 6e  >l_len,.     (in
c9bd0 74 29 70 2d 3e 6c 5f 70 69 64 2c 20 73 29 3b 0a  t)p->l_pid, s);.
c9be0 20 20 69 66 28 20 73 3d 3d 28 2d 31 29 20 26 26    if( s==(-1) &&
c9bf0 20 6f 70 3d 3d 46 5f 53 45 54 4c 4b 20 26 26 20   op==F_SETLK && 
c9c00 28 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 52 44  (p->l_type==F_RD
c9c10 4c 43 4b 20 7c 7c 20 70 2d 3e 6c 5f 74 79 70 65  LCK || p->l_type
c9c20 3d 3d 46 5f 57 52 4c 43 4b 29 20 29 7b 0a 20 20  ==F_WRLCK) ){.  
c9c30 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c    struct flock l
c9c40 32 3b 0a 20 20 20 20 6c 32 20 3d 20 2a 70 3b 0a  2;.    l2 = *p;.
c9c50 20 20 20 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f      fcntl(fd, F_
c9c60 47 45 54 4c 4b 2c 20 26 6c 32 29 3b 0a 20 20 20  GETLK, &l2);.   
c9c70 20 69 66 28 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d   if( l2.l_type==
c9c80 46 5f 52 44 4c 43 4b 20 29 7b 0a 20 20 20 20 20  F_RDLCK ){.     
c9c90 20 7a 54 79 70 65 20 3d 20 22 52 44 4c 43 4b 22   zType = "RDLCK"
c9ca0 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
c9cb0 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f 57 52 4c  l2.l_type==F_WRL
c9cc0 43 4b 20 29 7b 0a 20 20 20 20 20 20 7a 54 79 70  CK ){.      zTyp
c9cd0 65 20 3d 20 22 57 52 4c 43 4b 22 3b 0a 20 20 20  e = "WRLCK";.   
c9ce0 20 7d 65 6c 73 65 20 69 66 28 20 6c 32 2e 6c 5f   }else if( l2.l_
c9cf0 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 20 29 7b  type==F_UNLCK ){
c9d00 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 22  .      zType = "
c9d10 55 4e 4c 43 4b 22 3b 0a 20 20 20 20 7d 65 6c 73  UNLCK";.    }els
c9d20 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  e{.      assert(
c9d30 20 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20   0 );.    }.    
c9d40 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
c9d50 74 66 28 22 66 63 6e 74 6c 2d 66 61 69 6c 75 72  tf("fcntl-failur
c9d60 65 2d 72 65 61 73 6f 6e 3a 20 25 73 20 25 64 20  e-reason: %s %d 
c9d70 25 64 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20  %d %d\n",.      
c9d80 20 7a 54 79 70 65 2c 20 28 69 6e 74 29 6c 32 2e   zType, (int)l2.
c9d90 6c 5f 73 74 61 72 74 2c 20 28 69 6e 74 29 6c 32  l_start, (int)l2
c9da0 2e 6c 5f 6c 65 6e 2c 20 28 69 6e 74 29 6c 32 2e  .l_len, (int)l2.
c9db0 6c 5f 70 69 64 29 3b 0a 20 20 7d 0a 20 20 65 72  l_pid);.  }.  er
c9dc0 72 6e 6f 20 3d 20 73 61 76 65 64 45 72 72 6e 6f  rno = savedErrno
c9dd0 3b 0a 20 20 72 65 74 75 72 6e 20 73 3b 0a 7d 0a  ;.  return s;.}.
c9de0 23 64 65 66 69 6e 65 20 66 63 6e 74 6c 20 6c 6f  #define fcntl lo
c9df0 63 6b 54 72 61 63 65 0a 23 65 6e 64 69 66 20 2f  ckTrace.#endif /
c9e00 2a 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52  * SQLITE_LOCK_TR
c9e10 41 43 45 20 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20  ACE */..../*.** 
c9e20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 74 72 61  This routine tra
c9e30 6e 73 6c 61 74 65 73 20 61 20 73 74 61 6e 64 61  nslates a standa
c9e40 72 64 20 50 4f 53 49 58 20 65 72 72 6e 6f 20 63  rd POSIX errno c
c9e50 6f 64 65 20 69 6e 74 6f 20 73 6f 6d 65 74 68 69  ode into somethi
c9e60 6e 67 0a 2a 2a 20 75 73 65 66 75 6c 20 74 6f 20  ng.** useful to 
c9e70 74 68 65 20 63 6c 69 65 6e 74 73 20 6f 66 20 74  the clients of t
c9e80 68 65 20 73 71 6c 69 74 65 33 20 66 75 6e 63 74  he sqlite3 funct
c9e90 69 6f 6e 73 2e 20 20 53 70 65 63 69 66 69 63 61  ions.  Specifica
c9ea0 6c 6c 79 2c 20 69 74 20 69 73 0a 2a 2a 20 69 6e  lly, it is.** in
c9eb0 74 65 6e 64 65 64 20 74 6f 20 74 72 61 6e 73 6c  tended to transl
c9ec0 61 74 65 20 61 20 76 61 72 69 65 74 79 20 6f 66  ate a variety of
c9ed0 20 22 74 72 79 20 61 67 61 69 6e 22 20 65 72 72   "try again" err
c9ee0 6f 72 73 20 69 6e 74 6f 20 53 51 4c 49 54 45 5f  ors into SQLITE_
c9ef0 42 55 53 59 0a 2a 2a 20 61 6e 64 20 61 20 76 61  BUSY.** and a va
c9f00 72 69 65 74 79 20 6f 66 20 22 70 6c 65 61 73 65  riety of "please
c9f10 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20   close the file 
c9f20 64 65 73 63 72 69 70 74 6f 72 20 4e 4f 57 22 20  descriptor NOW" 
c9f30 65 72 72 6f 72 73 20 69 6e 74 6f 20 0a 2a 2a 20  errors into .** 
c9f40 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a 2a 2a 20  SQLITE_IOERR.** 
c9f50 0a 2a 2a 20 45 72 72 6f 72 73 20 64 75 72 69 6e  .** Errors durin
c9f60 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  g initialization
c9f70 20 6f 66 20 6c 6f 63 6b 73 2c 20 6f 72 20 66 69   of locks, or fi
c9f80 6c 65 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72  le system suppor
c9f90 74 20 66 6f 72 20 6c 6f 63 6b 73 2c 0a 2a 2a 20  t for locks,.** 
c9fa0 73 68 6f 75 6c 64 20 68 61 6e 64 6c 65 20 45 4e  should handle EN
c9fb0 4f 4c 43 4b 2c 20 45 4e 4f 54 53 55 50 2c 20 45  OLCK, ENOTSUP, E
c9fc0 4f 50 4e 4f 54 53 55 50 50 20 73 65 70 61 72 61  OPNOTSUPP separa
c9fd0 74 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tely..*/.static 
c9fe0 69 6e 74 20 73 71 6c 69 74 65 45 72 72 6f 72 46  int sqliteErrorF
c9ff0 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 69 6e  romPosixError(in
ca000 74 20 70 6f 73 69 78 45 72 72 6f 72 2c 20 69 6e  t posixError, in
ca010 74 20 73 71 6c 69 74 65 49 4f 45 72 72 29 20 7b  t sqliteIOErr) {
ca020 0a 20 20 73 77 69 74 63 68 20 28 70 6f 73 69 78  .  switch (posix
ca030 45 72 72 6f 72 29 20 7b 0a 20 20 63 61 73 65 20  Error) {.  case 
ca040 30 3a 20 0a 20 20 20 20 72 65 74 75 72 6e 20 53  0: .    return S
ca050 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 0a 20  QLITE_OK;.    . 
ca060 20 63 61 73 65 20 45 41 47 41 49 4e 3a 0a 20 20   case EAGAIN:.  
ca070 63 61 73 65 20 45 54 49 4d 45 44 4f 55 54 3a 0a  case ETIMEDOUT:.
ca080 20 20 63 61 73 65 20 45 42 55 53 59 3a 0a 20 20    case EBUSY:.  
ca090 63 61 73 65 20 45 49 4e 54 52 3a 0a 20 20 63 61  case EINTR:.  ca
ca0a0 73 65 20 45 4e 4f 4c 43 4b 3a 20 20 0a 20 20 20  se ENOLCK:  .   
ca0b0 20 2f 2a 20 72 61 6e 64 6f 6d 20 4e 46 53 20 72   /* random NFS r
ca0c0 65 74 72 79 20 65 72 72 6f 72 2c 20 75 6e 6c 65  etry error, unle
ca0d0 73 73 20 64 75 72 69 6e 67 20 66 69 6c 65 20 73  ss during file s
ca0e0 79 73 74 65 6d 20 73 75 70 70 6f 72 74 20 0a 20  ystem support . 
ca0f0 20 20 20 20 2a 20 69 6e 74 72 6f 73 70 65 63 74      * introspect
ca100 69 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 69 74  ion, in which it
ca110 20 61 63 74 75 61 6c 6c 79 20 6d 65 61 6e 73 20   actually means 
ca120 77 68 61 74 20 69 74 20 73 61 79 73 20 2a 2f 0a  what it says */.
ca130 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
ca140 45 5f 42 55 53 59 3b 0a 20 20 20 20 0a 20 20 63  E_BUSY;.    .  c
ca150 61 73 65 20 45 41 43 43 45 53 3a 20 0a 20 20 20  ase EACCES: .   
ca160 20 2f 2a 20 45 41 43 43 45 53 20 69 73 20 6c 69   /* EACCES is li
ca170 6b 65 20 45 41 47 41 49 4e 20 64 75 72 69 6e 67  ke EAGAIN during
ca180 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69   locking operati
ca190 6f 6e 73 2c 20 62 75 74 20 6e 6f 74 20 61 6e 79  ons, but not any
ca1a0 20 6f 74 68 65 72 20 74 69 6d 65 2a 2f 0a 20 20   other time*/.  
ca1b0 20 20 69 66 28 20 28 73 71 6c 69 74 65 49 4f 45    if( (sqliteIOE
ca1c0 72 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45  rr == SQLITE_IOE
ca1d0 52 52 5f 4c 4f 43 4b 29 20 7c 7c 20 0a 09 28 73  RR_LOCK) || ..(s
ca1e0 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53 51  qliteIOErr == SQ
ca1f0 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43  LITE_IOERR_UNLOC
ca200 4b 29 20 7c 7c 20 0a 09 28 73 71 6c 69 74 65 49  K) || ..(sqliteI
ca210 4f 45 72 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49  OErr == SQLITE_I
ca220 4f 45 52 52 5f 52 44 4c 4f 43 4b 29 20 7c 7c 0a  OERR_RDLOCK) ||.
ca230 09 28 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d  .(sqliteIOErr ==
ca240 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48   SQLITE_IOERR_CH
ca250 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 29  ECKRESERVEDLOCK)
ca260 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
ca270 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
ca280 20 20 7d 0a 20 20 20 20 2f 2a 20 65 6c 73 65 20    }.    /* else 
ca290 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 2a 2f 0a  fall through */.
ca2a0 20 20 63 61 73 65 20 45 50 45 52 4d 3a 20 0a 20    case EPERM: . 
ca2b0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
ca2c0 5f 50 45 52 4d 3b 0a 20 20 20 20 0a 20 20 63 61  _PERM;.    .  ca
ca2d0 73 65 20 45 44 45 41 44 4c 4b 3a 0a 20 20 20 20  se EDEADLK:.    
ca2e0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
ca2f0 45 52 52 5f 42 4c 4f 43 4b 45 44 3b 0a 20 20 20  ERR_BLOCKED;.   
ca300 20 0a 23 69 66 20 45 4f 50 4e 4f 54 53 55 50 50   .#if EOPNOTSUPP
ca310 21 3d 45 4e 4f 54 53 55 50 0a 20 20 63 61 73 65  !=ENOTSUP.  case
ca320 20 45 4f 50 4e 4f 54 53 55 50 50 3a 20 0a 20 20   EOPNOTSUPP: .  
ca330 20 20 2f 2a 20 73 6f 6d 65 74 68 69 6e 67 20 77    /* something w
ca340 65 6e 74 20 74 65 72 72 69 62 6c 79 20 61 77 72  ent terribly awr
ca350 79 2c 20 75 6e 6c 65 73 73 20 64 75 72 69 6e 67  y, unless during
ca360 20 66 69 6c 65 20 73 79 73 74 65 6d 20 73 75 70   file system sup
ca370 70 6f 72 74 20 0a 20 20 20 20 20 2a 20 69 6e 74  port .     * int
ca380 72 6f 73 70 65 63 74 69 6f 6e 2c 20 69 6e 20 77  rospection, in w
ca390 68 69 63 68 20 69 74 20 61 63 74 75 61 6c 6c 79  hich it actually
ca3a0 20 6d 65 61 6e 73 20 77 68 61 74 20 69 74 20 73   means what it s
ca3b0 61 79 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69  ays */.#endif.#i
ca3c0 66 64 65 66 20 45 4e 4f 54 53 55 50 0a 20 20 63  fdef ENOTSUP.  c
ca3d0 61 73 65 20 45 4e 4f 54 53 55 50 3a 20 0a 20 20  ase ENOTSUP: .  
ca3e0 20 20 2f 2a 20 69 6e 76 61 6c 69 64 20 66 64 2c    /* invalid fd,
ca3f0 20 75 6e 6c 65 73 73 20 64 75 72 69 6e 67 20 66   unless during f
ca400 69 6c 65 20 73 79 73 74 65 6d 20 73 75 70 70 6f  ile system suppo
ca410 72 74 20 69 6e 74 72 6f 73 70 65 63 74 69 6f 6e  rt introspection
ca420 2c 20 69 6e 20 77 68 69 63 68 20 0a 20 20 20 20  , in which .    
ca430 20 2a 20 69 74 20 61 63 74 75 61 6c 6c 79 20 6d   * it actually m
ca440 65 61 6e 73 20 77 68 61 74 20 69 74 20 73 61 79  eans what it say
ca450 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 63 61  s */.#endif.  ca
ca460 73 65 20 45 49 4f 3a 0a 20 20 63 61 73 65 20 45  se EIO:.  case E
ca470 42 41 44 46 3a 0a 20 20 63 61 73 65 20 45 49 4e  BADF:.  case EIN
ca480 56 41 4c 3a 0a 20 20 63 61 73 65 20 45 4e 4f 54  VAL:.  case ENOT
ca490 43 4f 4e 4e 3a 0a 20 20 63 61 73 65 20 45 4e 4f  CONN:.  case ENO
ca4a0 44 45 56 3a 0a 20 20 63 61 73 65 20 45 4e 58 49  DEV:.  case ENXI
ca4b0 4f 3a 0a 20 20 63 61 73 65 20 45 4e 4f 45 4e 54  O:.  case ENOENT
ca4c0 3a 0a 20 20 63 61 73 65 20 45 53 54 41 4c 45 3a  :.  case ESTALE:
ca4d0 0a 20 20 63 61 73 65 20 45 4e 4f 53 59 53 3a 0a  .  case ENOSYS:.
ca4e0 20 20 20 20 2f 2a 20 74 68 65 73 65 20 73 68 6f      /* these sho
ca4f0 75 6c 64 20 66 6f 72 63 65 20 74 68 65 20 63 6c  uld force the cl
ca500 69 65 6e 74 20 74 6f 20 63 6c 6f 73 65 20 74 68  ient to close th
ca510 65 20 66 69 6c 65 20 61 6e 64 20 72 65 63 6f 6e  e file and recon
ca520 6e 65 63 74 20 2a 2f 0a 20 20 20 20 0a 20 20 64  nect */.    .  d
ca530 65 66 61 75 6c 74 3a 20 0a 20 20 20 20 72 65 74  efault: .    ret
ca540 75 72 6e 20 73 71 6c 69 74 65 49 4f 45 72 72 3b  urn sqliteIOErr;
ca550 0a 20 20 7d 0a 7d 0a 0a 0a 0a 2f 2a 2a 2a 2a 2a  .  }.}..../*****
ca560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ca570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ca580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ca590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ca5a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a  *********.******
ca5b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
ca5c0 69 6e 20 55 6e 69 71 75 65 20 46 69 6c 65 20 49  in Unique File I
ca5d0 44 20 55 74 69 6c 69 74 79 20 55 73 65 64 20 42  D Utility Used B
ca5e0 79 20 56 78 57 6f 72 6b 73 20 2a 2a 2a 2a 2a 2a  y VxWorks ******
ca5f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
ca600 4f 6e 20 6d 6f 73 74 20 76 65 72 73 69 6f 6e 73  On most versions
ca610 20 6f 66 20 75 6e 69 78 2c 20 77 65 20 63 61 6e   of unix, we can
ca620 20 67 65 74 20 61 20 75 6e 69 71 75 65 20 49 44   get a unique ID
ca630 20 66 6f 72 20 61 20 66 69 6c 65 20 62 79 20 63   for a file by c
ca640 6f 6e 63 61 74 65 6e 61 74 69 6e 67 0a 2a 2a 20  oncatenating.** 
ca650 74 68 65 20 64 65 76 69 63 65 20 6e 75 6d 62 65  the device numbe
ca660 72 20 61 6e 64 20 74 68 65 20 69 6e 6f 64 65 20  r and the inode 
ca670 6e 75 6d 62 65 72 2e 20 20 42 75 74 20 74 68 69  number.  But thi
ca680 73 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20  s does not work 
ca690 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 20 4f  on VxWorks..** O
ca6a0 6e 20 56 78 57 6f 72 6b 73 2c 20 61 20 75 6e 69  n VxWorks, a uni
ca6b0 71 75 65 20 66 69 6c 65 20 69 64 20 6d 75 73 74  que file id must
ca6c0 20 62 65 20 62 61 73 65 64 20 6f 6e 20 74 68 65   be based on the
ca6d0 20 63 61 6e 6f 6e 69 63 61 6c 20 66 69 6c 65 6e   canonical filen
ca6e0 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69  ame..**.** A poi
ca6f0 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61  nter to an insta
ca700 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
ca710 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 63  wing structure c
ca720 61 6e 20 62 65 20 75 73 65 64 20 61 73 20 61 0a  an be used as a.
ca730 2a 2a 20 75 6e 69 71 75 65 20 66 69 6c 65 20 49  ** unique file I
ca740 44 20 69 6e 20 56 78 57 6f 72 6b 73 2e 20 20 45  D in VxWorks.  E
ca750 61 63 68 20 69 6e 73 74 61 6e 63 65 20 6f 66 20  ach instance of 
ca760 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 63  this structure c
ca770 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 63 6f 70  ontains.** a cop
ca780 79 20 6f 66 20 74 68 65 20 63 61 6e 6f 6e 69 63  y of the canonic
ca790 61 6c 20 66 69 6c 65 6e 61 6d 65 2e 20 20 54 68  al filename.  Th
ca7a0 65 72 65 20 69 73 20 61 6c 73 6f 20 61 20 72 65  ere is also a re
ca7b0 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 2e 20 20  ference count.  
ca7c0 0a 2a 2a 20 54 68 65 20 73 74 72 75 63 74 75 72  .** The structur
ca7d0 65 20 69 73 20 72 65 63 6c 61 69 6d 65 64 20 77  e is reclaimed w
ca7e0 68 65 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  hen the number o
ca7f0 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 69 74  f pointers to it
ca800 20 64 72 6f 70 73 20 74 6f 0a 2a 2a 20 7a 65 72   drops to.** zer
ca810 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61  o..**.** There a
ca820 72 65 20 6e 65 76 65 72 20 76 65 72 79 20 6d 61  re never very ma
ca830 6e 79 20 66 69 6c 65 73 20 6f 70 65 6e 20 61 74  ny files open at
ca840 20 6f 6e 65 20 74 69 6d 65 20 61 6e 64 20 6c 6f   one time and lo
ca850 6f 6b 75 70 73 20 61 72 65 20 6e 6f 74 0a 2a 2a  okups are not.**
ca860 20 61 20 70 65 72 66 6f 72 6d 61 6e 63 65 2d 63   a performance-c
ca870 72 69 74 69 63 61 6c 20 70 61 74 68 2c 20 73 6f  ritical path, so
ca880 20 69 74 20 69 73 20 73 75 66 66 69 63 69 65 6e   it is sufficien
ca890 74 20 74 6f 20 70 75 74 20 74 68 65 73 65 0a 2a  t to put these.*
ca8a0 2a 20 73 74 72 75 63 74 75 72 65 73 20 6f 6e 20  * structures on 
ca8b0 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a  a linked list..*
ca8c0 2f 0a 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73  /.struct vxworks
ca8d0 46 69 6c 65 49 64 20 7b 0a 20 20 73 74 72 75 63  FileId {.  struc
ca8e0 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20  t vxworksFileId 
ca8f0 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74  *pNext;  /* Next
ca900 20 69 6e 20 61 20 6c 69 73 74 20 6f 66 20 74 68   in a list of th
ca910 65 6d 20 61 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20  em all */.  int 
ca920 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20  nRef;           
ca930 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
ca940 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65  ber of reference
ca950 73 20 74 6f 20 74 68 69 73 20 6f 6e 65 20 2a 2f  s to this one */
ca960 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 20 20 20  .  int nName;   
ca970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ca980 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68   /* Length of th
ca990 65 20 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65  e zCanonicalName
ca9a0 5b 5d 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 63  [] string */.  c
ca9b0 68 61 72 20 2a 7a 43 61 6e 6f 6e 69 63 61 6c 4e  har *zCanonicalN
ca9c0 61 6d 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ame;         /* 
ca9d0 43 61 6e 6f 6e 69 63 61 6c 20 66 69 6c 65 6e 61  Canonical filena
ca9e0 6d 65 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 20 4f 53  me */.};..#if OS
ca9f0 5f 56 58 57 4f 52 4b 53 0a 2f 2a 20 0a 2a 2a 20  _VXWORKS./* .** 
caa00 41 6c 6c 20 75 6e 69 71 75 65 20 66 69 6c 65 6e  All unique filen
caa10 61 6d 65 73 20 61 72 65 20 68 65 6c 64 20 6f 6e  ames are held on
caa20 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 68   a linked list h
caa30 65 61 64 65 64 20 62 79 20 74 68 69 73 0a 2a 2a  eaded by this.**
caa40 20 76 61 72 69 61 62 6c 65 3a 0a 2a 2f 0a 73 74   variable:.*/.st
caa50 61 74 69 63 20 73 74 72 75 63 74 20 76 78 77 6f  atic struct vxwo
caa60 72 6b 73 46 69 6c 65 49 64 20 2a 76 78 77 6f 72  rksFileId *vxwor
caa70 6b 73 46 69 6c 65 4c 69 73 74 20 3d 20 30 3b 0a  ksFileList = 0;.
caa80 0a 2f 2a 0a 2a 2a 20 53 69 6d 70 6c 69 66 79 20  ./*.** Simplify 
caa90 61 20 66 69 6c 65 6e 61 6d 65 20 69 6e 74 6f 20  a filename into 
caaa0 69 74 73 20 63 61 6e 6f 6e 69 63 61 6c 20 66 6f  its canonical fo
caab0 72 6d 0a 2a 2a 20 62 79 20 6d 61 6b 69 6e 67 20  rm.** by making 
caac0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68  the following ch
caad0 61 6e 67 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 2a 20  anges:.**.**  * 
caae0 72 65 6d 6f 76 69 6e 67 20 61 6e 79 20 74 72 61  removing any tra
caaf0 69 6c 69 6e 67 20 61 6e 64 20 64 75 70 6c 69 63  iling and duplic
cab00 61 74 65 20 2f 0a 2a 2a 20 20 2a 20 63 6f 6e 76  ate /.**  * conv
cab10 65 72 74 20 2f 2e 2f 20 69 6e 74 6f 20 6a 75 73  ert /./ into jus
cab20 74 20 2f 0a 2a 2a 20 20 2a 20 63 6f 6e 76 65 72  t /.**  * conver
cab30 74 20 2f 41 2f 2e 2e 2f 20 77 68 65 72 65 20 41  t /A/../ where A
cab40 20 69 73 20 61 6e 79 20 73 69 6d 70 6c 65 20 6e   is any simple n
cab50 61 6d 65 20 69 6e 74 6f 20 6a 75 73 74 20 2f 0a  ame into just /.
cab60 2a 2a 0a 2a 2a 20 43 68 61 6e 67 65 73 20 61 72  **.** Changes ar
cab70 65 20 6d 61 64 65 20 69 6e 2d 70 6c 61 63 65 2e  e made in-place.
cab80 20 20 52 65 74 75 72 6e 20 74 68 65 20 6e 65 77    Return the new
cab90 20 6e 61 6d 65 20 6c 65 6e 67 74 68 2e 0a 2a 2a   name length..**
caba0 0a 2a 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c  .** The original
cabb0 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 69 6e 20   filename is in 
cabc0 7a 5b 30 2e 2e 6e 2d 31 5d 2e 20 20 52 65 74 75  z[0..n-1].  Retu
cabd0 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
cabe0 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73 20 69  .** characters i
cabf0 6e 20 74 68 65 20 73 69 6d 70 6c 69 66 69 65 64  n the simplified
cac00 20 6e 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   name..*/.static
cac10 20 69 6e 74 20 76 78 77 6f 72 6b 73 53 69 6d 70   int vxworksSimp
cac20 6c 69 66 79 4e 61 6d 65 28 63 68 61 72 20 2a 7a  lifyName(char *z
cac30 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 69 6e 74 20  , int n){.  int 
cac40 69 2c 20 6a 3b 0a 20 20 77 68 69 6c 65 28 20 6e  i, j;.  while( n
cac50 3e 31 20 26 26 20 7a 5b 6e 2d 31 5d 3d 3d 27 2f  >1 && z[n-1]=='/
cac60 27 20 29 7b 20 6e 2d 2d 3b 20 7d 0a 20 20 66 6f  ' ){ n--; }.  fo
cac70 72 28 69 3d 6a 3d 30 3b 20 69 3c 6e 3b 20 69 2b  r(i=j=0; i<n; i+
cac80 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 5b 69 5d  +){.    if( z[i]
cac90 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 69  =='/' ){.      i
caca0 66 28 20 7a 5b 69 2b 31 5d 3d 3d 27 2f 27 20 29  f( z[i+1]=='/' )
cacb0 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
cacc0 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 27 2e 27   if( z[i+1]=='.'
cacd0 20 26 26 20 69 2b 32 3c 6e 20 26 26 20 7a 5b 69   && i+2<n && z[i
cace0 2b 32 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20  +2]=='/' ){.    
cacf0 20 20 20 20 69 20 2b 3d 20 31 3b 0a 20 20 20 20      i += 1;.    
cad00 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20      continue;.  
cad10 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
cad20 7a 5b 69 2b 31 5d 3d 3d 27 2e 27 20 26 26 20 69  z[i+1]=='.' && i
cad30 2b 33 3c 6e 20 26 26 20 7a 5b 69 2b 32 5d 3d 3d  +3<n && z[i+2]==
cad40 27 2e 27 20 26 26 20 7a 5b 69 2b 33 5d 3d 3d 27  '.' && z[i+3]=='
cad50 2f 27 20 29 7b 0a 20 20 20 20 20 20 20 20 77 68  /' ){.        wh
cad60 69 6c 65 28 20 6a 3e 30 20 26 26 20 7a 5b 6a 2d  ile( j>0 && z[j-
cad70 31 5d 21 3d 27 2f 27 20 29 7b 20 6a 2d 2d 3b 20  1]!='/' ){ j--; 
cad80 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 6a 3e  }.        if( j>
cad90 30 20 29 7b 20 6a 2d 2d 3b 20 7d 0a 20 20 20 20  0 ){ j--; }.    
cada0 20 20 20 20 69 20 2b 3d 20 32 3b 0a 20 20 20 20      i += 2;.    
cadb0 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20      continue;.  
cadc0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
cadd0 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20  z[j++] = z[i];. 
cade0 20 7d 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20   }.  z[j] = 0;. 
cadf0 20 72 65 74 75 72 6e 20 6a 3b 0a 7d 0a 0a 2f 2a   return j;.}../*
cae00 0a 2a 2a 20 46 69 6e 64 20 61 20 75 6e 69 71 75  .** Find a uniqu
cae10 65 20 66 69 6c 65 20 49 44 20 66 6f 72 20 74 68  e file ID for th
cae20 65 20 67 69 76 65 6e 20 61 62 73 6f 6c 75 74 65  e given absolute
cae30 20 70 61 74 68 6e 61 6d 65 2e 20 20 52 65 74 75   pathname.  Retu
cae40 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20  rn.** a pointer 
cae50 74 6f 20 74 68 65 20 76 78 77 6f 72 6b 73 46 69  to the vxworksFi
cae60 6c 65 49 64 20 6f 62 6a 65 63 74 2e 20 20 54 68  leId object.  Th
cae70 69 73 20 70 6f 69 6e 74 65 72 20 69 73 20 74 68  is pointer is th
cae80 65 20 75 6e 69 71 75 65 0a 2a 2a 20 66 69 6c 65  e unique.** file
cae90 20 49 44 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e   ID..**.** The n
caea0 52 65 66 20 66 69 65 6c 64 20 6f 66 20 74 68 65  Ref field of the
caeb0 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f   vxworksFileId o
caec0 62 6a 65 63 74 20 69 73 20 69 6e 63 72 65 6d 65  bject is increme
caed0 6e 74 65 64 20 62 65 66 6f 72 65 0a 2a 2a 20 74  nted before.** t
caee0 68 65 20 6f 62 6a 65 63 74 20 69 73 20 72 65 74  he object is ret
caef0 75 72 6e 65 64 2e 20 20 41 20 6e 65 77 20 76 78  urned.  A new vx
caf00 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f 62 6a 65  worksFileId obje
caf10 63 74 20 69 73 20 63 72 65 61 74 65 64 0a 2a 2a  ct is created.**
caf20 20 61 6e 64 20 61 64 64 65 64 20 74 6f 20 74 68   and added to th
caf30 65 20 67 6c 6f 62 61 6c 20 6c 69 73 74 20 69 66  e global list if
caf40 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a 0a 2a   necessary..**.*
caf50 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c  * If a memory al
caf60 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f  location error o
caf70 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20 4e 55  ccurs, return NU
caf80 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74  LL..*/.static st
caf90 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65  ruct vxworksFile
cafa0 49 64 20 2a 76 78 77 6f 72 6b 73 46 69 6e 64 46  Id *vxworksFindF
cafb0 69 6c 65 49 64 28 63 6f 6e 73 74 20 63 68 61 72  ileId(const char
cafc0 20 2a 7a 41 62 73 6f 6c 75 74 65 4e 61 6d 65 29   *zAbsoluteName)
cafd0 7b 0a 20 20 73 74 72 75 63 74 20 76 78 77 6f 72  {.  struct vxwor
cafe0 6b 73 46 69 6c 65 49 64 20 2a 70 4e 65 77 3b 20  ksFileId *pNew; 
caff0 20 20 20 20 20 20 20 20 2f 2a 20 73 65 61 72 63          /* searc
cb000 68 20 6b 65 79 20 61 6e 64 20 6e 65 77 20 66 69  h key and new fi
cb010 6c 65 20 49 44 20 2a 2f 0a 20 20 73 74 72 75 63  le ID */.  struc
cb020 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20  t vxworksFileId 
cb030 2a 70 43 61 6e 64 69 64 61 74 65 3b 20 20 20 2f  *pCandidate;   /
cb040 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76  * For looping ov
cb050 65 72 20 65 78 69 73 74 69 6e 67 20 66 69 6c 65  er existing file
cb060 20 49 44 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b   IDs */.  int n;
cb070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cb080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
cb090 20 4c 65 6e 67 74 68 20 6f 66 20 7a 41 62 73 6f   Length of zAbso
cb0a0 6c 75 74 65 4e 61 6d 65 20 73 74 72 69 6e 67 20  luteName string 
cb0b0 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 7a 41  */..  assert( zA
cb0c0 62 73 6f 6c 75 74 65 4e 61 6d 65 5b 30 5d 3d 3d  bsoluteName[0]==
cb0d0 27 2f 27 20 29 3b 0a 20 20 6e 20 3d 20 28 69 6e  '/' );.  n = (in
cb0e0 74 29 73 74 72 6c 65 6e 28 7a 41 62 73 6f 6c 75  t)strlen(zAbsolu
cb0f0 74 65 4e 61 6d 65 29 3b 0a 20 20 70 4e 65 77 20  teName);.  pNew 
cb100 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
cb110 28 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20  ( sizeof(*pNew) 
cb120 2b 20 28 6e 2b 31 29 20 29 3b 0a 20 20 69 66 28  + (n+1) );.  if(
cb130 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72   pNew==0 ) retur
cb140 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 7a 43 61  n 0;.  pNew->zCa
cb150 6e 6f 6e 69 63 61 6c 4e 61 6d 65 20 3d 20 28 63  nonicalName = (c
cb160 68 61 72 2a 29 26 70 4e 65 77 5b 31 5d 3b 0a 20  har*)&pNew[1];. 
cb170 20 6d 65 6d 63 70 79 28 70 4e 65 77 2d 3e 7a 43   memcpy(pNew->zC
cb180 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 7a 41  anonicalName, zA
cb190 62 73 6f 6c 75 74 65 4e 61 6d 65 2c 20 6e 2b 31  bsoluteName, n+1
cb1a0 29 3b 0a 20 20 6e 20 3d 20 76 78 77 6f 72 6b 73  );.  n = vxworks
cb1b0 53 69 6d 70 6c 69 66 79 4e 61 6d 65 28 70 4e 65  SimplifyName(pNe
cb1c0 77 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d  w->zCanonicalNam
cb1d0 65 2c 20 6e 29 3b 0a 0a 20 20 2f 2a 20 53 65 61  e, n);..  /* Sea
cb1e0 72 63 68 20 66 6f 72 20 61 6e 20 65 78 69 73 74  rch for an exist
cb1f0 69 6e 67 20 65 6e 74 72 79 20 74 68 61 74 20 6d  ing entry that m
cb200 61 74 63 68 69 6e 67 20 74 68 65 20 63 61 6e 6f  atching the cano
cb210 6e 69 63 61 6c 20 6e 61 6d 65 2e 0a 20 20 2a 2a  nical name..  **
cb220 20 49 66 20 66 6f 75 6e 64 2c 20 69 6e 63 72 65   If found, incre
cb230 6d 65 6e 74 20 74 68 65 20 72 65 66 65 72 65 6e  ment the referen
cb240 63 65 20 63 6f 75 6e 74 20 61 6e 64 20 72 65 74  ce count and ret
cb250 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
cb260 0a 20 20 2a 2a 20 74 68 65 20 65 78 69 73 74 69  .  ** the existi
cb270 6e 67 20 66 69 6c 65 20 49 44 2e 0a 20 20 2a 2f  ng file ID..  */
cb280 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65  .  unixEnterMute
cb290 78 28 29 3b 0a 20 20 66 6f 72 28 70 43 61 6e 64  x();.  for(pCand
cb2a0 69 64 61 74 65 3d 76 78 77 6f 72 6b 73 46 69 6c  idate=vxworksFil
cb2b0 65 4c 69 73 74 3b 20 70 43 61 6e 64 69 64 61 74  eList; pCandidat
cb2c0 65 3b 20 70 43 61 6e 64 69 64 61 74 65 3d 70 43  e; pCandidate=pC
cb2d0 61 6e 64 69 64 61 74 65 2d 3e 70 4e 65 78 74 29  andidate->pNext)
cb2e0 7b 0a 20 20 20 20 69 66 28 20 70 43 61 6e 64 69  {.    if( pCandi
cb2f0 64 61 74 65 2d 3e 6e 4e 61 6d 65 3d 3d 6e 20 0a  date->nName==n .
cb300 20 20 20 20 20 26 26 20 6d 65 6d 63 6d 70 28 70       && memcmp(p
cb310 43 61 6e 64 69 64 61 74 65 2d 3e 7a 43 61 6e 6f  Candidate->zCano
cb320 6e 69 63 61 6c 4e 61 6d 65 2c 20 70 4e 65 77 2d  nicalName, pNew-
cb330 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c  >zCanonicalName,
cb340 20 6e 29 3d 3d 30 0a 20 20 20 20 29 7b 0a 20 20   n)==0.    ){.  
cb350 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
cb360 65 28 70 4e 65 77 29 3b 0a 20 20 20 20 20 20 20  e(pNew);.       
cb370 70 43 61 6e 64 69 64 61 74 65 2d 3e 6e 52 65 66  pCandidate->nRef
cb380 2b 2b 3b 0a 20 20 20 20 20 20 20 75 6e 69 78 4c  ++;.       unixL
cb390 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20  eaveMutex();.   
cb3a0 20 20 20 20 72 65 74 75 72 6e 20 70 43 61 6e 64      return pCand
cb3b0 69 64 61 74 65 3b 0a 20 20 20 20 7d 0a 20 20 7d  idate;.    }.  }
cb3c0 0a 0a 20 20 2f 2a 20 4e 6f 20 6d 61 74 63 68 20  ..  /* No match 
cb3d0 77 61 73 20 66 6f 75 6e 64 2e 20 20 57 65 20 77  was found.  We w
cb3e0 69 6c 6c 20 6d 61 6b 65 20 61 20 6e 65 77 20 66  ill make a new f
cb3f0 69 6c 65 20 49 44 20 2a 2f 0a 20 20 70 4e 65 77  ile ID */.  pNew
cb400 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 70 4e  ->nRef = 1;.  pN
cb410 65 77 2d 3e 6e 4e 61 6d 65 20 3d 20 6e 3b 0a 20  ew->nName = n;. 
cb420 20 70 4e 65 77 2d 3e 70 4e 65 78 74 20 3d 20 76   pNew->pNext = v
cb430 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 3b 0a  xworksFileList;.
cb440 20 20 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73    vxworksFileLis
cb450 74 20 3d 20 70 4e 65 77 3b 0a 20 20 75 6e 69 78  t = pNew;.  unix
cb460 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20  LeaveMutex();.  
cb470 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a  return pNew;.}..
cb480 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20  /*.** Decrement 
cb490 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f  the reference co
cb4a0 75 6e 74 20 6f 6e 20 61 20 76 78 77 6f 72 6b 73  unt on a vxworks
cb4b0 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 2e 20 20  FileId object.  
cb4c0 46 72 65 65 0a 2a 2a 20 74 68 65 20 6f 62 6a 65  Free.** the obje
cb4d0 63 74 20 77 68 65 6e 20 74 68 65 20 72 65 66 65  ct when the refe
cb4e0 72 65 6e 63 65 20 63 6f 75 6e 74 20 72 65 61 63  rence count reac
cb4f0 68 65 73 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61  hes zero..*/.sta
cb500 74 69 63 20 76 6f 69 64 20 76 78 77 6f 72 6b 73  tic void vxworks
cb510 52 65 6c 65 61 73 65 46 69 6c 65 49 64 28 73 74  ReleaseFileId(st
cb520 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65  ruct vxworksFile
cb530 49 64 20 2a 70 49 64 29 7b 0a 20 20 75 6e 69 78  Id *pId){.  unix
cb540 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20  EnterMutex();.  
cb550 61 73 73 65 72 74 28 20 70 49 64 2d 3e 6e 52 65  assert( pId->nRe
cb560 66 3e 30 20 29 3b 0a 20 20 70 49 64 2d 3e 6e 52  f>0 );.  pId->nR
cb570 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 49 64 2d  ef--;.  if( pId-
cb580 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20  >nRef==0 ){.    
cb590 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69  struct vxworksFi
cb5a0 6c 65 49 64 20 2a 2a 70 70 3b 0a 20 20 20 20 66  leId **pp;.    f
cb5b0 6f 72 28 70 70 3d 26 76 78 77 6f 72 6b 73 46 69  or(pp=&vxworksFi
cb5c0 6c 65 4c 69 73 74 3b 20 2a 70 70 20 26 26 20 2a  leList; *pp && *
cb5d0 70 70 21 3d 70 49 64 3b 20 70 70 20 3d 20 26 28  pp!=pId; pp = &(
cb5e0 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 29 7b 7d  (*pp)->pNext)){}
cb5f0 0a 20 20 20 20 61 73 73 65 72 74 28 20 2a 70 70  .    assert( *pp
cb600 3d 3d 70 49 64 20 29 3b 0a 20 20 20 20 2a 70 70  ==pId );.    *pp
cb610 20 3d 20 70 49 64 2d 3e 70 4e 65 78 74 3b 0a 20   = pId->pNext;. 
cb620 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
cb630 70 49 64 29 3b 0a 20 20 7d 0a 20 20 75 6e 69 78  pId);.  }.  unix
cb640 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a  LeaveMutex();.}.
cb650 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 58 57  #endif /* OS_VXW
cb660 4f 52 4b 53 20 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ORKS */./*******
cb670 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
cb680 55 6e 69 71 75 65 20 46 69 6c 65 20 49 44 20 55  Unique File ID U
cb690 74 69 6c 69 74 79 20 55 73 65 64 20 42 79 20 56  tility Used By V
cb6a0 78 57 6f 72 6b 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  xWorks *********
cb6b0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  *******.********
cb6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb6f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb700 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a  ******/.../*****
cb710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb750 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a  *********.******
cb760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb770 2a 2a 2a 2a 2a 20 50 6f 73 69 78 20 41 64 76 69  ***** Posix Advi
cb780 73 6f 72 79 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a  sory Locking ***
cb790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb7a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
cb7b0 50 4f 53 49 58 20 61 64 76 69 73 6f 72 79 20 6c  POSIX advisory l
cb7c0 6f 63 6b 73 20 61 72 65 20 62 72 6f 6b 65 6e 20  ocks are broken 
cb7d0 62 79 20 64 65 73 69 67 6e 2e 20 20 41 4e 53 49  by design.  ANSI
cb7e0 20 53 54 44 20 31 30 30 33 2e 31 20 28 31 39 39   STD 1003.1 (199
cb7f0 36 29 0a 2a 2a 20 73 65 63 74 69 6f 6e 20 36 2e  6).** section 6.
cb800 35 2e 32 2e 32 20 6c 69 6e 65 73 20 34 38 33 20  5.2.2 lines 483 
cb810 74 68 72 6f 75 67 68 20 34 39 30 20 73 70 65 63  through 490 spec
cb820 69 66 79 20 74 68 61 74 20 77 68 65 6e 20 61 20  ify that when a 
cb830 70 72 6f 63 65 73 73 0a 2a 2a 20 73 65 74 73 20  process.** sets 
cb840 6f 72 20 63 6c 65 61 72 73 20 61 20 6c 6f 63 6b  or clears a lock
cb850 2c 20 74 68 61 74 20 6f 70 65 72 61 74 69 6f 6e  , that operation
cb860 20 6f 76 65 72 72 69 64 65 73 20 61 6e 79 20 70   overrides any p
cb870 72 69 6f 72 20 6c 6f 63 6b 73 20 73 65 74 0a 2a  rior locks set.*
cb880 2a 20 62 79 20 74 68 65 20 73 61 6d 65 20 70 72  * by the same pr
cb890 6f 63 65 73 73 2e 20 20 49 74 20 64 6f 65 73 20  ocess.  It does 
cb8a0 6e 6f 74 20 65 78 70 6c 69 63 69 74 6c 79 20 73  not explicitly s
cb8b0 61 79 20 73 6f 2c 20 62 75 74 20 74 68 69 73 20  ay so, but this 
cb8c0 69 6d 70 6c 69 65 73 0a 2a 2a 20 74 68 61 74 20  implies.** that 
cb8d0 69 74 20 6f 76 65 72 72 69 64 65 73 20 6c 6f 63  it overrides loc
cb8e0 6b 73 20 73 65 74 20 62 79 20 74 68 65 20 73 61  ks set by the sa
cb8f0 6d 65 20 70 72 6f 63 65 73 73 20 75 73 69 6e 67  me process using
cb900 20 61 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20   a different.** 
cb910 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e  file descriptor.
cb920 20 20 43 6f 6e 73 69 64 65 72 20 74 68 69 73 20    Consider this 
cb930 74 65 73 74 20 63 61 73 65 3a 0a 2a 2a 0a 2a 2a  test case:.**.**
cb940 20 20 20 20 20 20 20 69 6e 74 20 66 64 31 20 3d         int fd1 =
cb950 20 6f 70 65 6e 28 22 2e 2f 66 69 6c 65 31 22 2c   open("./file1",
cb960 20 4f 5f 52 44 57 52 7c 4f 5f 43 52 45 41 54 2c   O_RDWR|O_CREAT,
cb970 20 30 36 34 34 29 3b 0a 2a 2a 20 20 20 20 20 20   0644);.**      
cb980 20 69 6e 74 20 66 64 32 20 3d 20 6f 70 65 6e 28   int fd2 = open(
cb990 22 2e 2f 66 69 6c 65 32 22 2c 20 4f 5f 52 44 57  "./file2", O_RDW
cb9a0 52 7c 4f 5f 43 52 45 41 54 2c 20 30 36 34 34 29  R|O_CREAT, 0644)
cb9b0 3b 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73 65 20  ;.**.** Suppose 
cb9c0 2e 2f 66 69 6c 65 31 20 61 6e 64 20 2e 2f 66 69  ./file1 and ./fi
cb9d0 6c 65 32 20 61 72 65 20 72 65 61 6c 6c 79 20 74  le2 are really t
cb9e0 68 65 20 73 61 6d 65 20 66 69 6c 65 20 28 62 65  he same file (be
cb9f0 63 61 75 73 65 0a 2a 2a 20 6f 6e 65 20 69 73 20  cause.** one is 
cba00 61 20 68 61 72 64 20 6f 72 20 73 79 6d 62 6f 6c  a hard or symbol
cba10 69 63 20 6c 69 6e 6b 20 74 6f 20 74 68 65 20 6f  ic link to the o
cba20 74 68 65 72 29 20 74 68 65 6e 20 69 66 20 79 6f  ther) then if yo
cba30 75 20 73 65 74 0a 2a 2a 20 61 6e 20 65 78 63 6c  u set.** an excl
cba40 75 73 69 76 65 20 6c 6f 63 6b 20 6f 6e 20 66 64  usive lock on fd
cba50 31 2c 20 74 68 65 6e 20 74 72 79 20 74 6f 20 67  1, then try to g
cba60 65 74 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20  et an exclusive 
cba70 6c 6f 63 6b 0a 2a 2a 20 6f 6e 20 66 64 32 2c 20  lock.** on fd2, 
cba80 69 74 20 77 6f 72 6b 73 2e 20 20 49 20 77 6f 75  it works.  I wou
cba90 6c 64 20 68 61 76 65 20 65 78 70 65 63 74 65 64  ld have expected
cbaa0 20 74 68 65 20 73 65 63 6f 6e 64 20 6c 6f 63 6b   the second lock
cbab0 20 74 6f 0a 2a 2a 20 66 61 69 6c 20 73 69 6e 63   to.** fail sinc
cbac0 65 20 74 68 65 72 65 20 77 61 73 20 61 6c 72 65  e there was alre
cbad0 61 64 79 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68  ady a lock on th
cbae0 65 20 66 69 6c 65 20 64 75 65 20 74 6f 20 66 64  e file due to fd
cbaf0 31 2e 0a 2a 2a 20 42 75 74 20 6e 6f 74 20 73 6f  1..** But not so
cbb00 2e 20 20 53 69 6e 63 65 20 62 6f 74 68 20 6c 6f  .  Since both lo
cbb10 63 6b 73 20 63 61 6d 65 20 66 72 6f 6d 20 74 68  cks came from th
cbb20 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 2c 20  e same process, 
cbb30 74 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 6f 76  the.** second ov
cbb40 65 72 72 69 64 65 73 20 74 68 65 20 66 69 72 73  errides the firs
cbb50 74 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 20 74  t, even though t
cbb60 68 65 79 20 77 65 72 65 20 6f 6e 20 64 69 66 66  hey were on diff
cbb70 65 72 65 6e 74 0a 2a 2a 20 66 69 6c 65 20 64 65  erent.** file de
cbb80 73 63 72 69 70 74 6f 72 73 20 6f 70 65 6e 65 64  scriptors opened
cbb90 20 6f 6e 20 64 69 66 66 65 72 65 6e 74 20 66 69   on different fi
cbba0 6c 65 20 6e 61 6d 65 73 2e 0a 2a 2a 0a 2a 2a 20  le names..**.** 
cbbb0 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20  This means that 
cbbc0 77 65 20 63 61 6e 6e 6f 74 20 75 73 65 20 50 4f  we cannot use PO
cbbd0 53 49 58 20 6c 6f 63 6b 73 20 74 6f 20 73 79 6e  SIX locks to syn
cbbe0 63 68 72 6f 6e 69 7a 65 20 66 69 6c 65 20 61 63  chronize file ac
cbbf0 63 65 73 73 0a 2a 2a 20 61 6d 6f 6e 67 20 63 6f  cess.** among co
cbc00 6d 70 65 74 69 6e 67 20 74 68 72 65 61 64 73 20  mpeting threads 
cbc10 6f 66 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63  of the same proc
cbc20 65 73 73 2e 20 20 50 4f 53 49 58 20 6c 6f 63 6b  ess.  POSIX lock
cbc30 73 20 77 69 6c 6c 20 77 6f 72 6b 20 66 69 6e 65  s will work fine
cbc40 0a 2a 2a 20 74 6f 20 73 79 6e 63 68 72 6f 6e 69  .** to synchroni
cbc50 7a 65 20 61 63 63 65 73 73 20 66 6f 72 20 74 68  ze access for th
cbc60 72 65 61 64 73 20 69 6e 20 73 65 70 61 72 61 74  reads in separat
cbc70 65 20 70 72 6f 63 65 73 73 65 73 2c 20 62 75 74  e processes, but
cbc80 20 6e 6f 74 0a 2a 2a 20 74 68 72 65 61 64 73 20   not.** threads 
cbc90 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65 20  within the same 
cbca0 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54  process..**.** T
cbcb0 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68  o work around th
cbcc0 65 20 70 72 6f 62 6c 65 6d 2c 20 53 51 4c 69 74  e problem, SQLit
cbcd0 65 20 68 61 73 20 74 6f 20 6d 61 6e 61 67 65 20  e has to manage 
cbce0 66 69 6c 65 20 6c 6f 63 6b 73 20 69 6e 74 65 72  file locks inter
cbcf0 6e 61 6c 6c 79 0a 2a 2a 20 6f 6e 20 69 74 73 20  nally.** on its 
cbd00 6f 77 6e 2e 20 20 57 68 65 6e 65 76 65 72 20 61  own.  Whenever a
cbd10 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 69 73   new database is
cbd20 20 6f 70 65 6e 65 64 2c 20 77 65 20 68 61 76 65   opened, we have
cbd30 20 74 6f 20 66 69 6e 64 20 74 68 65 0a 2a 2a 20   to find the.** 
cbd40 73 70 65 63 69 66 69 63 20 69 6e 6f 64 65 20 6f  specific inode o
cbd50 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  f the database f
cbd60 69 6c 65 20 28 74 68 65 20 69 6e 6f 64 65 20 69  ile (the inode i
cbd70 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20  s determined by 
cbd80 74 68 65 0a 2a 2a 20 73 74 5f 64 65 76 20 61 6e  the.** st_dev an
cbd90 64 20 73 74 5f 69 6e 6f 20 66 69 65 6c 64 73 20  d st_ino fields 
cbda0 6f 66 20 74 68 65 20 73 74 61 74 20 73 74 72 75  of the stat stru
cbdb0 63 74 75 72 65 20 74 68 61 74 20 66 73 74 61 74  cture that fstat
cbdc0 28 29 20 66 69 6c 6c 73 20 69 6e 29 0a 2a 2a 20  () fills in).** 
cbdd0 61 6e 64 20 63 68 65 63 6b 20 66 6f 72 20 6c 6f  and check for lo
cbde0 63 6b 73 20 61 6c 72 65 61 64 79 20 65 78 69 73  cks already exis
cbdf0 74 69 6e 67 20 6f 6e 20 74 68 61 74 20 69 6e 6f  ting on that ino
cbe00 64 65 2e 20 20 57 68 65 6e 20 6c 6f 63 6b 73 20  de.  When locks 
cbe10 61 72 65 0a 2a 2a 20 63 72 65 61 74 65 64 20 6f  are.** created o
cbe20 72 20 72 65 6d 6f 76 65 64 2c 20 77 65 20 68 61  r removed, we ha
cbe30 76 65 20 74 6f 20 6c 6f 6f 6b 20 61 74 20 6f 75  ve to look at ou
cbe40 72 20 6f 77 6e 20 69 6e 74 65 72 6e 61 6c 20 72  r own internal r
cbe50 65 63 6f 72 64 20 6f 66 20 74 68 65 0a 2a 2a 20  ecord of the.** 
cbe60 6c 6f 63 6b 73 20 74 6f 20 73 65 65 20 69 66 20  locks to see if 
cbe70 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 68  another thread h
cbe80 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 73 65  as previously se
cbe90 74 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 61 74  t a lock on that
cbea0 20 73 61 6d 65 0a 2a 2a 20 69 6e 6f 64 65 2e 0a   same.** inode..
cbeb0 2a 2a 0a 2a 2a 20 28 41 73 69 64 65 3a 20 54 68  **.** (Aside: Th
cbec0 65 20 75 73 65 20 6f 66 20 69 6e 6f 64 65 20 6e  e use of inode n
cbed0 75 6d 62 65 72 73 20 61 73 20 75 6e 69 71 75 65  umbers as unique
cbee0 20 49 44 73 20 64 6f 65 73 20 6e 6f 74 20 77 6f   IDs does not wo
cbef0 72 6b 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a  rk on VxWorks..*
cbf00 2a 20 46 6f 72 20 56 78 57 6f 72 6b 73 2c 20 77  * For VxWorks, w
cbf10 65 20 68 61 76 65 20 74 6f 20 75 73 65 20 74 68  e have to use th
cbf20 65 20 61 6c 74 65 72 6e 61 74 69 76 65 20 75 6e  e alternative un
cbf30 69 71 75 65 20 49 44 20 73 79 73 74 65 6d 20 62  ique ID system b
cbf40 61 73 65 64 20 6f 6e 0a 2a 2a 20 63 61 6e 6f 6e  ased on.** canon
cbf50 69 63 61 6c 20 66 69 6c 65 6e 61 6d 65 20 61 6e  ical filename an
cbf60 64 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e  d implemented in
cbf70 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 64 69   the previous di
cbf80 76 69 73 69 6f 6e 2e 29 0a 2a 2a 0a 2a 2a 20 54  vision.).**.** T
cbf90 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  he sqlite3_file 
cbfa0 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 50 4f  structure for PO
cbfb0 53 49 58 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72  SIX is no longer
cbfc0 20 6a 75 73 74 20 61 6e 20 69 6e 74 65 67 65 72   just an integer
cbfd0 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70   file.** descrip
cbfe0 74 6f 72 2e 20 20 49 74 20 69 73 20 6e 6f 77 20  tor.  It is now 
cbff0 61 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  a structure that
cc000 20 68 6f 6c 64 73 20 74 68 65 20 69 6e 74 65 67   holds the integ
cc010 65 72 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72  er file.** descr
cc020 69 70 74 6f 72 20 61 6e 64 20 61 20 70 6f 69 6e  iptor and a poin
cc030 74 65 72 20 74 6f 20 61 20 73 74 72 75 63 74 75  ter to a structu
cc040 72 65 20 74 68 61 74 20 64 65 73 63 72 69 62 65  re that describe
cc050 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a  s the internal.*
cc060 2a 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 63  * locks on the c
cc070 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 69 6e 6f  orresponding ino
cc080 64 65 2e 20 20 54 68 65 72 65 20 69 73 20 6f 6e  de.  There is on
cc090 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 75 63 74  e locking struct
cc0a0 75 72 65 0a 2a 2a 20 70 65 72 20 69 6e 6f 64 65  ure.** per inode
cc0b0 2c 20 73 6f 20 69 66 20 74 68 65 20 73 61 6d 65  , so if the same
cc0c0 20 69 6e 6f 64 65 20 69 73 20 6f 70 65 6e 65 64   inode is opened
cc0d0 20 74 77 69 63 65 2c 20 62 6f 74 68 20 75 6e 69   twice, both uni
cc0e0 78 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 73  xFile structures
cc0f0 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 74 68 65  .** point to the
cc100 20 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 20 73 74   same locking st
cc110 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 6c 6f  ructure.  The lo
cc120 63 6b 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  cking structure 
cc130 6b 65 65 70 73 0a 2a 2a 20 61 20 72 65 66 65 72  keeps.** a refer
cc140 65 6e 63 65 20 63 6f 75 6e 74 20 28 73 6f 20 77  ence count (so w
cc150 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 77 68 65 6e  e will know when
cc160 20 74 6f 20 64 65 6c 65 74 65 20 69 74 29 20 61   to delete it) a
cc170 6e 64 20 61 20 22 63 6e 74 22 0a 2a 2a 20 66 69  nd a "cnt".** fi
cc180 65 6c 64 20 74 68 61 74 20 74 65 6c 6c 73 20 75  eld that tells u
cc190 73 20 69 74 73 20 69 6e 74 65 72 6e 61 6c 20 6c  s its internal l
cc1a0 6f 63 6b 20 73 74 61 74 75 73 2e 20 20 63 6e 74  ock status.  cnt
cc1b0 3d 3d 30 20 6d 65 61 6e 73 20 74 68 65 0a 2a 2a  ==0 means the.**
cc1c0 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b 65   file is unlocke
cc1d0 64 2e 20 20 63 6e 74 3d 3d 2d 31 20 6d 65 61 6e  d.  cnt==-1 mean
cc1e0 73 20 74 68 65 20 66 69 6c 65 20 68 61 73 20 61  s the file has a
cc1f0 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  n exclusive lock
cc200 2e 0a 2a 2a 20 63 6e 74 3e 30 20 6d 65 61 6e 73  ..** cnt>0 means
cc210 20 74 68 65 72 65 20 61 72 65 20 63 6e 74 20 73   there are cnt s
cc220 68 61 72 65 64 20 6c 6f 63 6b 73 20 6f 6e 20 74  hared locks on t
cc230 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41  he file..**.** A
cc240 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 6c 6f  ny attempt to lo
cc250 63 6b 20 6f 72 20 75 6e 6c 6f 63 6b 20 61 20 66  ck or unlock a f
cc260 69 6c 65 20 66 69 72 73 74 20 63 68 65 63 6b 73  ile first checks
cc270 20 74 68 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20   the locking.** 
cc280 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20  structure.  The 
cc290 66 63 6e 74 6c 28 29 20 73 79 73 74 65 6d 20 63  fcntl() system c
cc2a0 61 6c 6c 20 69 73 20 6f 6e 6c 79 20 69 6e 76 6f  all is only invo
cc2b0 6b 65 64 20 74 6f 20 73 65 74 20 61 20 0a 2a 2a  ked to set a .**
cc2c0 20 50 4f 53 49 58 20 6c 6f 63 6b 20 69 66 20 74   POSIX lock if t
cc2d0 68 65 20 69 6e 74 65 72 6e 61 6c 20 6c 6f 63 6b  he internal lock
cc2e0 20 73 74 72 75 63 74 75 72 65 20 74 72 61 6e 73   structure trans
cc2f0 69 74 69 6f 6e 73 20 62 65 74 77 65 65 6e 0a 2a  itions between.*
cc300 2a 20 61 20 6c 6f 63 6b 65 64 20 61 6e 64 20 61  * a locked and a
cc310 6e 20 75 6e 6c 6f 63 6b 65 64 20 73 74 61 74 65  n unlocked state
cc320 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 20 77 61 69 74  ..**.** But wait
cc330 3a 20 20 74 68 65 72 65 20 61 72 65 20 79 65 74  :  there are yet
cc340 20 6d 6f 72 65 20 70 72 6f 62 6c 65 6d 73 20 77   more problems w
cc350 69 74 68 20 50 4f 53 49 58 20 61 64 76 69 73 6f  ith POSIX adviso
cc360 72 79 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20  ry locks..**.** 
cc370 49 66 20 79 6f 75 20 63 6c 6f 73 65 20 61 20 66  If you close a f
cc380 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74  ile descriptor t
cc390 68 61 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20  hat points to a 
cc3a0 66 69 6c 65 20 74 68 61 74 20 68 61 73 20 6c 6f  file that has lo
cc3b0 63 6b 73 2c 0a 2a 2a 20 61 6c 6c 20 6c 6f 63 6b  cks,.** all lock
cc3c0 73 20 6f 6e 20 74 68 61 74 20 66 69 6c 65 20 74  s on that file t
cc3d0 68 61 74 20 61 72 65 20 6f 77 6e 65 64 20 62 79  hat are owned by
cc3e0 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 72 6f   the current pro
cc3f0 63 65 73 73 20 61 72 65 0a 2a 2a 20 72 65 6c 65  cess are.** rele
cc400 61 73 65 64 2e 20 20 54 6f 20 77 6f 72 6b 20 61  ased.  To work a
cc410 72 6f 75 6e 64 20 74 68 69 73 20 70 72 6f 62 6c  round this probl
cc420 65 6d 2c 20 65 61 63 68 20 75 6e 69 78 46 69 6c  em, each unixFil
cc430 65 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74  e structure cont
cc440 61 69 6e 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65  ains.** a pointe
cc450 72 20 74 6f 20 61 6e 20 75 6e 69 78 4f 70 65 6e  r to an unixOpen
cc460 43 6e 74 20 73 74 72 75 63 74 75 72 65 2e 20 20  Cnt structure.  
cc470 54 68 65 72 65 20 69 73 20 6f 6e 65 20 75 6e 69  There is one uni
cc480 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75  xOpenCnt structu
cc490 72 65 0a 2a 2a 20 70 65 72 20 6f 70 65 6e 20 69  re.** per open i
cc4a0 6e 6f 64 65 2c 20 77 68 69 63 68 20 6d 65 61 6e  node, which mean
cc4b0 73 20 74 68 61 74 20 6d 75 6c 74 69 70 6c 65 20  s that multiple 
cc4c0 75 6e 69 78 46 69 6c 65 20 63 61 6e 20 70 6f 69  unixFile can poi
cc4d0 6e 74 20 74 6f 20 61 20 73 69 6e 67 6c 65 0a 2a  nt to a single.*
cc4e0 2a 20 75 6e 69 78 4f 70 65 6e 43 6e 74 2e 20 20  * unixOpenCnt.  
cc4f0 57 68 65 6e 20 61 6e 20 61 74 74 65 6d 70 74 20  When an attempt 
cc500 69 73 20 6d 61 64 65 20 74 6f 20 63 6c 6f 73 65  is made to close
cc510 20 61 6e 20 75 6e 69 78 46 69 6c 65 2c 20 69 66   an unixFile, if
cc520 20 74 68 65 72 65 20 61 72 65 0a 2a 2a 20 6f 74   there are.** ot
cc530 68 65 72 20 75 6e 69 78 46 69 6c 65 20 6f 70 65  her unixFile ope
cc540 6e 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 69 6e  n on the same in
cc550 6f 64 65 20 74 68 61 74 20 61 72 65 20 68 6f 6c  ode that are hol
cc560 64 69 6e 67 20 6c 6f 63 6b 73 2c 20 74 68 65 20  ding locks, the 
cc570 63 61 6c 6c 0a 2a 2a 20 74 6f 20 63 6c 6f 73 65  call.** to close
cc580 28 29 20 74 68 65 20 66 69 6c 65 20 64 65 73 63  () the file desc
cc590 72 69 70 74 6f 72 20 69 73 20 64 65 66 65 72 72  riptor is deferr
cc5a0 65 64 20 75 6e 74 69 6c 20 61 6c 6c 20 6f 66 20  ed until all of 
cc5b0 74 68 65 20 6c 6f 63 6b 73 20 63 6c 65 61 72 2e  the locks clear.
cc5c0 0a 2a 2a 20 54 68 65 20 75 6e 69 78 4f 70 65 6e  .** The unixOpen
cc5d0 43 6e 74 20 73 74 72 75 63 74 75 72 65 20 6b 65  Cnt structure ke
cc5e0 65 70 73 20 61 20 6c 69 73 74 20 6f 66 20 66 69  eps a list of fi
cc5f0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 74  le descriptors t
cc600 68 61 74 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 62  hat need to.** b
cc610 65 20 63 6c 6f 73 65 64 20 61 6e 64 20 74 68 61  e closed and tha
cc620 74 20 6c 69 73 74 20 69 73 20 77 61 6c 6b 65 64  t list is walked
cc630 20 28 61 6e 64 20 63 6c 65 61 72 65 64 29 20 77   (and cleared) w
cc640 68 65 6e 20 74 68 65 20 6c 61 73 74 20 6c 6f 63  hen the last loc
cc650 6b 0a 2a 2a 20 63 6c 65 61 72 73 2e 0a 2a 2a 0a  k.** clears..**.
cc660 2a 2a 20 59 65 74 20 61 6e 6f 74 68 65 72 20 70  ** Yet another p
cc670 72 6f 62 6c 65 6d 3a 20 20 4c 69 6e 75 78 54 68  roblem:  LinuxTh
cc680 72 65 61 64 73 20 64 6f 20 6e 6f 74 20 70 6c 61  reads do not pla
cc690 79 20 77 65 6c 6c 20 77 69 74 68 20 70 6f 73 69  y well with posi
cc6a0 78 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 4d  x locks..**.** M
cc6b0 61 6e 79 20 6f 6c 64 65 72 20 76 65 72 73 69 6f  any older versio
cc6c0 6e 73 20 6f 66 20 6c 69 6e 75 78 20 75 73 65 20  ns of linux use 
cc6d0 74 68 65 20 4c 69 6e 75 78 54 68 72 65 61 64 73  the LinuxThreads
cc6e0 20 6c 69 62 72 61 72 79 20 77 68 69 63 68 20 69   library which i
cc6f0 73 0a 2a 2a 20 6e 6f 74 20 70 6f 73 69 78 20 63  s.** not posix c
cc700 6f 6d 70 6c 69 61 6e 74 2e 20 20 55 6e 64 65 72  ompliant.  Under
cc710 20 4c 69 6e 75 78 54 68 72 65 61 64 73 2c 20 61   LinuxThreads, a
cc720 20 6c 6f 63 6b 20 63 72 65 61 74 65 64 20 62 79   lock created by
cc730 20 74 68 72 65 61 64 0a 2a 2a 20 41 20 63 61 6e   thread.** A can
cc740 6e 6f 74 20 62 65 20 6d 6f 64 69 66 69 65 64 20  not be modified 
cc750 6f 72 20 6f 76 65 72 72 69 64 64 65 6e 20 62 79  or overridden by
cc760 20 61 20 64 69 66 66 65 72 65 6e 74 20 74 68 72   a different thr
cc770 65 61 64 20 42 2e 0a 2a 2a 20 4f 6e 6c 79 20 74  ead B..** Only t
cc780 68 72 65 61 64 20 41 20 63 61 6e 20 6d 6f 64 69  hread A can modi
cc790 66 79 20 74 68 65 20 6c 6f 63 6b 2e 20 20 4c 6f  fy the lock.  Lo
cc7a0 63 6b 69 6e 67 20 62 65 68 61 76 69 6f 72 20 69  cking behavior i
cc7b0 73 20 63 6f 72 72 65 63 74 0a 2a 2a 20 69 66 20  s correct.** if 
cc7c0 74 68 65 20 61 70 70 6c 69 61 74 69 6f 6e 20 75  the appliation u
cc7d0 73 65 73 20 74 68 65 20 6e 65 77 65 72 20 4e 61  ses the newer Na
cc7e0 74 69 76 65 20 50 6f 73 69 78 20 54 68 72 65 61  tive Posix Threa
cc7f0 64 20 4c 69 62 72 61 72 79 20 28 4e 50 54 4c 29  d Library (NPTL)
cc800 0a 2a 2a 20 6f 6e 20 6c 69 6e 75 78 20 2d 20 77  .** on linux - w
cc810 69 74 68 20 4e 50 54 4c 20 61 20 6c 6f 63 6b 20  ith NPTL a lock 
cc820 63 72 65 61 74 65 64 20 62 79 20 74 68 72 65 61  created by threa
cc830 64 20 41 20 63 61 6e 20 6f 76 65 72 72 69 64 65  d A can override
cc840 20 6c 6f 63 6b 73 0a 2a 2a 20 69 6e 20 74 68 72   locks.** in thr
cc850 65 61 64 20 42 2e 20 20 42 75 74 20 74 68 65 72  ead B.  But ther
cc860 65 20 69 73 20 6e 6f 20 77 61 79 20 74 6f 20 6b  e is no way to k
cc870 6e 6f 77 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74  now at compile-t
cc880 69 6d 65 20 77 68 69 63 68 0a 2a 2a 20 74 68 72  ime which.** thr
cc890 65 61 64 69 6e 67 20 6c 69 62 72 61 72 79 20 69  eading library i
cc8a0 73 20 62 65 69 6e 67 20 75 73 65 64 2e 20 20 53  s being used.  S
cc8b0 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 61  o there is no wa
cc8c0 79 20 74 6f 20 6b 6e 6f 77 20 61 74 0a 2a 2a 20  y to know at.** 
cc8d0 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 77 68 65  compile-time whe
cc8e0 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65  ther or not thre
cc8f0 61 64 20 41 20 63 61 6e 20 6f 76 65 72 72 69 64  ad A can overrid
cc900 65 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 72 65 61  e locks on threa
cc910 64 20 42 2e 0a 2a 2a 20 57 65 20 68 61 76 65 20  d B..** We have 
cc920 74 6f 20 64 6f 20 61 20 72 75 6e 2d 74 69 6d 65  to do a run-time
cc930 20 63 68 65 63 6b 20 74 6f 20 64 69 73 63 6f 76   check to discov
cc940 65 72 20 74 68 65 20 62 65 68 61 76 69 6f 72 20  er the behavior 
cc950 6f 66 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e  of the.** curren
cc960 74 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a  t process..**.**
cc970 20 4f 6e 20 73 79 73 74 65 6d 73 20 77 68 65 72   On systems wher
cc980 65 20 74 68 72 65 61 64 20 41 20 69 73 20 75 6e  e thread A is un
cc990 61 62 6c 65 20 74 6f 20 6d 6f 64 69 66 79 20 6c  able to modify l
cc9a0 6f 63 6b 73 20 63 72 65 61 74 65 64 20 62 79 0a  ocks created by.
cc9b0 2a 2a 20 74 68 72 65 61 64 20 42 2c 20 77 65 20  ** thread B, we 
cc9c0 68 61 76 65 20 74 6f 20 6b 65 65 70 20 74 72 61  have to keep tra
cc9d0 63 6b 20 6f 66 20 77 68 69 63 68 20 74 68 72 65  ck of which thre
cc9e0 61 64 20 63 72 65 61 74 65 64 20 65 61 63 68 0a  ad created each.
cc9f0 2a 2a 20 6c 6f 63 6b 2e 20 20 48 65 6e 63 65 20  ** lock.  Hence 
cca00 74 68 65 72 65 20 69 73 20 61 6e 20 65 78 74 72  there is an extr
cca10 61 20 66 69 65 6c 64 20 69 6e 20 74 68 65 20 6b  a field in the k
cca20 65 79 20 74 6f 20 74 68 65 20 75 6e 69 78 4c 6f  ey to the unixLo
cca30 63 6b 49 6e 66 6f 0a 2a 2a 20 73 74 72 75 63 74  ckInfo.** struct
cca40 75 72 65 20 74 6f 20 72 65 63 6f 72 64 20 74 68  ure to record th
cca50 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20  is information. 
cca60 20 41 6e 64 20 6f 6e 20 74 68 6f 73 65 20 73 79   And on those sy
cca70 73 74 65 6d 73 20 69 74 0a 2a 2a 20 69 73 20 69  stems it.** is i
cca80 6c 6c 65 67 61 6c 20 74 6f 20 62 65 67 69 6e 20  llegal to begin 
cca90 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e  a transaction in
ccaa0 20 6f 6e 65 20 74 68 72 65 61 64 20 61 6e 64 20   one thread and 
ccab0 66 69 6e 69 73 68 20 69 74 0a 2a 2a 20 69 6e 20  finish it.** in 
ccac0 61 6e 6f 74 68 65 72 2e 20 20 46 6f 72 20 74 68  another.  For th
ccad0 69 73 20 6c 61 74 74 65 72 20 72 65 73 74 72 69  is latter restri
ccae0 63 74 69 6f 6e 2c 20 74 68 65 72 65 20 69 73 20  ction, there is 
ccaf0 6e 6f 20 77 6f 72 6b 2d 61 72 6f 75 6e 64 2e 0a  no work-around..
ccb00 2a 2a 20 49 74 20 69 73 20 61 20 6c 69 6d 69 74  ** It is a limit
ccb10 61 74 69 6f 6e 20 6f 66 20 4c 69 6e 75 78 54 68  ation of LinuxTh
ccb20 72 65 61 64 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  reads..*/../*.**
ccb30 20 53 65 74 20 6f 72 20 63 68 65 63 6b 20 74 68   Set or check th
ccb40 65 20 75 6e 69 78 46 69 6c 65 2e 74 69 64 20 66  e unixFile.tid f
ccb50 69 65 6c 64 2e 20 20 54 68 69 73 20 66 69 65 6c  ield.  This fiel
ccb60 64 20 69 73 20 73 65 74 20 77 68 65 6e 20 61 6e  d is set when an
ccb70 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 69 73 20   unixFile.** is 
ccb80 66 69 72 73 74 20 6f 70 65 6e 65 64 2e 20 20 41  first opened.  A
ccb90 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20 75 73  ll subsequent us
ccba0 65 73 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69  es of the unixFi
ccbb0 6c 65 20 76 65 72 69 66 79 20 74 68 61 74 20 74  le verify that t
ccbc0 68 65 0a 2a 2a 20 73 61 6d 65 20 74 68 72 65 61  he.** same threa
ccbd0 64 20 69 73 20 6f 70 65 72 61 74 69 6e 67 20 6f  d is operating o
ccbe0 6e 20 74 68 65 20 75 6e 69 78 46 69 6c 65 2e 20  n the unixFile. 
ccbf0 20 53 6f 6d 65 20 6f 70 65 72 61 74 69 6e 67 20   Some operating 
ccc00 73 79 73 74 65 6d 73 20 64 6f 0a 2a 2a 20 6e 6f  systems do.** no
ccc10 74 20 61 6c 6c 6f 77 20 6c 6f 63 6b 73 20 74 6f  t allow locks to
ccc20 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e 20 62   be overridden b
ccc30 79 20 6f 74 68 65 72 20 74 68 72 65 61 64 73 20  y other threads 
ccc40 61 6e 64 20 74 68 61 74 20 72 65 73 74 72 69 63  and that restric
ccc50 74 69 6f 6e 0a 2a 2a 20 6d 65 61 6e 73 20 74 68  tion.** means th
ccc60 61 74 20 73 71 6c 69 74 65 33 2a 20 64 61 74 61  at sqlite3* data
ccc70 62 61 73 65 20 68 61 6e 64 6c 65 73 20 63 61 6e  base handles can
ccc80 6e 6f 74 20 62 65 20 6d 6f 76 65 64 20 66 72 6f  not be moved fro
ccc90 6d 20 6f 6e 65 20 74 68 72 65 61 64 0a 2a 2a 20  m one thread.** 
ccca0 74 6f 20 61 6e 6f 74 68 65 72 20 77 68 69 6c 65  to another while
cccb0 20 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c 64 2e   locks are held.
cccc0 0a 2a 2a 0a 2a 2a 20 56 65 72 73 69 6f 6e 20 33  .**.** Version 3
cccd0 2e 33 2e 31 20 28 32 30 30 36 2d 30 31 2d 31 35  .3.1 (2006-01-15
ccce0 29 3a 20 20 75 6e 69 78 46 69 6c 65 20 63 61 6e  ):  unixFile can
cccf0 20 62 65 20 6d 6f 76 65 64 20 66 72 6f 6d 20 6f   be moved from o
ccd00 6e 65 20 74 68 72 65 61 64 20 74 6f 0a 2a 2a 20  ne thread to.** 
ccd10 61 6e 6f 74 68 65 72 20 61 73 20 6c 6f 6e 67 20  another as long 
ccd20 61 73 20 77 65 20 61 72 65 20 72 75 6e 6e 69 6e  as we are runnin
ccd30 67 20 6f 6e 20 61 20 73 79 73 74 65 6d 20 74 68  g on a system th
ccd40 61 74 20 73 75 70 70 6f 72 74 73 20 74 68 72 65  at supports thre
ccd50 61 64 73 0a 2a 2a 20 6f 76 65 72 72 69 64 69 6e  ads.** overridin
ccd60 67 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f  g each others lo
ccd70 63 6b 73 20 28 77 68 69 63 68 20 69 73 20 6e 6f  cks (which is no
ccd80 77 20 74 68 65 20 6d 6f 73 74 20 63 6f 6d 6d 6f  w the most commo
ccd90 6e 20 62 65 68 61 76 69 6f 72 29 0a 2a 2a 20 6f  n behavior).** o
ccda0 72 20 69 66 20 6e 6f 20 6c 6f 63 6b 73 20 61 72  r if no locks ar
ccdb0 65 20 68 65 6c 64 2e 20 20 42 75 74 20 74 68 65  e held.  But the
ccdc0 20 75 6e 69 78 46 69 6c 65 2e 70 4c 6f 63 6b 20   unixFile.pLock 
ccdd0 66 69 65 6c 64 20 6e 65 65 64 73 20 74 6f 20 62  field needs to b
ccde0 65 0a 2a 2a 20 72 65 63 6f 6d 70 75 74 65 64 20  e.** recomputed 
ccdf0 62 65 63 61 75 73 65 20 69 74 73 20 6b 65 79 20  because its key 
cce00 69 6e 63 6c 75 64 65 73 20 74 68 65 20 74 68 72  includes the thr
cce10 65 61 64 2d 69 64 2e 20 20 53 65 65 20 74 68 65  ead-id.  See the
cce20 20 0a 2a 2a 20 74 72 61 6e 73 66 65 72 4f 77 6e   .** transferOwn
cce30 65 72 73 68 69 70 28 29 20 66 75 6e 63 74 69 6f  ership() functio
cce40 6e 20 62 65 6c 6f 77 20 66 6f 72 20 61 64 64 69  n below for addi
cce50 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69  tional informati
cce60 6f 6e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  on.*/.#if SQLITE
cce70 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64  _THREADSAFE && d
cce80 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f  efined(__linux__
cce90 29 0a 23 20 64 65 66 69 6e 65 20 53 45 54 5f 54  ).# define SET_T
ccea0 48 52 45 41 44 49 44 28 58 29 20 20 20 28 58 29  HREADID(X)   (X)
cceb0 2d 3e 74 69 64 20 3d 20 70 74 68 72 65 61 64 5f  ->tid = pthread_
ccec0 73 65 6c 66 28 29 0a 23 20 64 65 66 69 6e 65 20  self().# define 
cced0 43 48 45 43 4b 5f 54 48 52 45 41 44 49 44 28 58  CHECK_THREADID(X
ccee0 29 20 28 74 68 72 65 61 64 73 4f 76 65 72 72 69  ) (threadsOverri
ccef0 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b  deEachOthersLock
ccf00 73 3d 3d 30 20 26 26 20 5c 0a 20 20 20 20 20 20  s==0 && \.      
ccf10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ccf20 20 20 20 20 20 20 21 70 74 68 72 65 61 64 5f 65        !pthread_e
ccf30 71 75 61 6c 28 28 58 29 2d 3e 74 69 64 2c 20 70  qual((X)->tid, p
ccf40 74 68 72 65 61 64 5f 73 65 6c 66 28 29 29 29 0a  thread_self())).
ccf50 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53  #else.# define S
ccf60 45 54 5f 54 48 52 45 41 44 49 44 28 58 29 0a 23  ET_THREADID(X).#
ccf70 20 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 54 48   define CHECK_TH
ccf80 52 45 41 44 49 44 28 58 29 20 30 0a 23 65 6e 64  READID(X) 0.#end
ccf90 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  if../*.** An ins
ccfa0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
ccfb0 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
ccfc0 20 73 65 72 76 65 73 20 61 73 20 74 68 65 20 6b   serves as the k
ccfd0 65 79 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f  ey used.** to lo
ccfe0 63 61 74 65 20 61 20 70 61 72 74 69 63 75 6c 61  cate a particula
ccff0 72 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74  r unixOpenCnt st
cd000 72 75 63 74 75 72 65 20 67 69 76 65 6e 20 69 74  ructure given it
cd010 73 20 69 6e 6f 64 65 2e 20 20 54 68 69 73 0a 2a  s inode.  This.*
cd020 2a 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73  * is the same as
cd030 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b 4b 65 79   the unixLockKey
cd040 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65   except that the
cd050 20 74 68 72 65 61 64 20 49 44 20 69 73 20 6f 6d   thread ID is om
cd060 69 74 74 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74  itted..*/.struct
cd070 20 75 6e 69 78 46 69 6c 65 49 64 20 7b 0a 20 20   unixFileId {.  
cd080 64 65 76 5f 74 20 64 65 76 3b 20 20 20 20 20 20  dev_t dev;      
cd090 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
cd0a0 65 76 69 63 65 20 6e 75 6d 62 65 72 20 2a 2f 0a  evice number */.
cd0b0 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20  #if OS_VXWORKS. 
cd0c0 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46   struct vxworksF
cd0d0 69 6c 65 49 64 20 2a 70 49 64 3b 20 20 2f 2a 20  ileId *pId;  /* 
cd0e0 55 6e 69 71 75 65 20 66 69 6c 65 20 49 44 20 66  Unique file ID f
cd0f0 6f 72 20 76 78 77 6f 72 6b 73 2e 20 2a 2f 0a 23  or vxworks. */.#
cd100 65 6c 73 65 0a 20 20 69 6e 6f 5f 74 20 69 6e 6f  else.  ino_t ino
cd110 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
cd120 20 20 20 2f 2a 20 49 6e 6f 64 65 20 6e 75 6d 62     /* Inode numb
cd130 65 72 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a  er */.#endif.};.
cd140 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  ./*.** An instan
cd150 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
cd160 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 73 65  ing structure se
cd170 72 76 65 73 20 61 73 20 74 68 65 20 6b 65 79 20  rves as the key 
cd180 75 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f 63 61 74  used.** to locat
cd190 65 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 75  e a particular u
cd1a0 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 73 74 72 75  nixLockInfo stru
cd1b0 63 74 75 72 65 20 67 69 76 65 6e 20 69 74 73 20  cture given its 
cd1c0 69 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  inode..**.** If 
cd1d0 74 68 72 65 61 64 73 20 63 61 6e 6e 6f 74 20 6f  threads cannot o
cd1e0 76 65 72 72 69 64 65 20 65 61 63 68 20 6f 74 68  verride each oth
cd1f0 65 72 73 20 6c 6f 63 6b 73 20 28 4c 69 6e 75 78  ers locks (Linux
cd200 54 68 72 65 61 64 73 29 2c 20 74 68 65 6e 20 77  Threads), then w
cd210 65 0a 2a 2a 20 73 65 74 20 74 68 65 20 75 6e 69  e.** set the uni
cd220 78 4c 6f 63 6b 4b 65 79 2e 74 69 64 20 66 69 65  xLockKey.tid fie
cd230 6c 64 20 74 6f 20 74 68 65 20 74 68 72 65 61 64  ld to the thread
cd240 20 49 44 2e 20 20 49 66 20 74 68 72 65 61 64 73   ID.  If threads
cd250 20 63 61 6e 20 6f 76 65 72 72 69 64 65 0a 2a 2a   can override.**
cd260 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63   each others loc
cd270 6b 73 20 28 50 6f 73 69 78 20 61 6e 64 20 4e 50  ks (Posix and NP
cd280 54 4c 29 20 74 68 65 6e 20 74 69 64 20 69 73 20  TL) then tid is 
cd290 61 6c 77 61 79 73 20 73 65 74 20 74 6f 20 7a 65  always set to ze
cd2a0 72 6f 2e 0a 2a 2a 20 74 69 64 20 69 73 20 6f 6d  ro..** tid is om
cd2b0 69 74 74 65 64 20 69 66 20 77 65 20 63 6f 6d 70  itted if we comp
cd2c0 69 6c 65 20 77 69 74 68 6f 75 74 20 74 68 72 65  ile without thre
cd2d0 61 64 69 6e 67 20 73 75 70 70 6f 72 74 20 6f 72  ading support or
cd2e0 20 6f 6e 20 61 6e 20 4f 53 0a 2a 2a 20 6f 74 68   on an OS.** oth
cd2f0 65 72 20 74 68 61 6e 20 6c 69 6e 75 78 2e 0a 2a  er than linux..*
cd300 2f 0a 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63  /.struct unixLoc
cd310 6b 4b 65 79 20 7b 0a 20 20 73 74 72 75 63 74 20  kKey {.  struct 
cd320 75 6e 69 78 46 69 6c 65 49 64 20 66 69 64 3b 20  unixFileId fid; 
cd330 20 2f 2a 20 55 6e 69 71 75 65 20 69 64 65 6e 74   /* Unique ident
cd340 69 66 69 65 72 20 66 6f 72 20 74 68 65 20 66 69  ifier for the fi
cd350 6c 65 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  le */.#if SQLITE
cd360 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64  _THREADSAFE && d
cd370 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f  efined(__linux__
cd380 29 0a 20 20 70 74 68 72 65 61 64 5f 74 20 74 69  ).  pthread_t ti
cd390 64 3b 20 20 2f 2a 20 54 68 72 65 61 64 20 49 44  d;  /* Thread ID
cd3a0 20 6f 66 20 6c 6f 63 6b 20 6f 77 6e 65 72 2e 20   of lock owner. 
cd3b0 5a 65 72 6f 20 69 66 20 6e 6f 74 20 75 73 69 6e  Zero if not usin
cd3c0 67 20 4c 69 6e 75 78 54 68 72 65 61 64 73 20 2a  g LinuxThreads *
cd3d0 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a  /.#endif.};../*.
cd3e0 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
cd3f0 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
cd400 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c  structure is all
cd410 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63 68 20  ocated for each 
cd420 6f 70 65 6e 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20  open.** inode.  
cd430 4f 72 2c 20 6f 6e 20 4c 69 6e 75 78 54 68 72 65  Or, on LinuxThre
cd440 61 64 73 2c 20 74 68 65 72 65 20 69 73 20 6f 6e  ads, there is on
cd450 65 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 63  e of these struc
cd460 74 75 72 65 73 20 66 6f 72 0a 2a 2a 20 65 61 63  tures for.** eac
cd470 68 20 69 6e 6f 64 65 20 6f 70 65 6e 65 64 20 62  h inode opened b
cd480 79 20 65 61 63 68 20 74 68 72 65 61 64 2e 0a 2a  y each thread..*
cd490 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 69 6e  *.** A single in
cd4a0 6f 64 65 20 63 61 6e 20 68 61 76 65 20 6d 75 6c  ode can have mul
cd4b0 74 69 70 6c 65 20 66 69 6c 65 20 64 65 73 63 72  tiple file descr
cd4c0 69 70 74 6f 72 73 2c 20 73 6f 20 65 61 63 68 20  iptors, so each 
cd4d0 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 73 74 72 75  unixFile.** stru
cd4e0 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61  cture contains a
cd4f0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69   pointer to an i
cd500 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20  nstance of this 
cd510 6f 62 6a 65 63 74 20 61 6e 64 20 74 68 69 73 0a  object and this.
cd520 2a 2a 20 6f 62 6a 65 63 74 20 6b 65 65 70 73 20  ** object keeps 
cd530 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e  a count of the n
cd540 75 6d 62 65 72 20 6f 66 20 75 6e 69 78 46 69 6c  umber of unixFil
cd550 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 69 74  e pointing to it
cd560 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69 78  ..*/.struct unix
cd570 4c 6f 63 6b 49 6e 66 6f 20 7b 0a 20 20 73 74 72  LockInfo {.  str
cd580 75 63 74 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 20  uct unixLockKey 
cd590 6c 6f 63 6b 4b 65 79 3b 20 20 20 20 20 2f 2a 20  lockKey;     /* 
cd5a0 54 68 65 20 6c 6f 6f 6b 75 70 20 6b 65 79 20 2a  The lookup key *
cd5b0 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20  /.  int cnt;    
cd5c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd5d0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
cd5e0 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 68 65   SHARED locks he
cd5f0 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 6c 6f 63 6b  ld */.  int lock
cd600 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20  type;           
cd610 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f          /* One o
cd620 66 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2c 20 52  f SHARED_LOCK, R
cd630 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 65 74 63  ESERVED_LOCK etc
cd640 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b  . */.  int nRef;
cd650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cd660 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
cd670 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20   of pointers to 
cd680 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a  this structure *
cd690 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4c  /.  struct unixL
cd6a0 6f 63 6b 49 6e 66 6f 20 2a 70 4e 65 78 74 3b 20  ockInfo *pNext; 
cd6b0 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61      /* List of a
cd6c0 6c 6c 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20  ll unixLockInfo 
cd6d0 6f 62 6a 65 63 74 73 20 2a 2f 0a 20 20 73 74 72  objects */.  str
cd6e0 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f  uct unixLockInfo
cd6f0 20 2a 70 50 72 65 76 3b 20 20 20 20 20 2f 2a 20   *pPrev;     /* 
cd700 20 20 20 2e 2e 2e 2e 20 64 6f 75 62 6c 79 20 6c     .... doubly l
cd710 69 6e 6b 65 64 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  inked */.};../*.
cd720 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
cd730 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
cd740 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c  structure is all
cd750 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63 68 20  ocated for each 
cd760 6f 70 65 6e 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20  open.** inode.  
cd770 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 6b  This structure k
cd780 65 65 70 73 20 74 72 61 63 6b 20 6f 66 20 74 68  eeps track of th
cd790 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b  e number of lock
cd7a0 73 20 6f 6e 20 74 68 61 74 0a 2a 2a 20 69 6e 6f  s on that.** ino
cd7b0 64 65 2e 20 20 49 66 20 61 20 63 6c 6f 73 65 20  de.  If a close 
cd7c0 69 73 20 61 74 74 65 6d 70 74 65 64 20 61 67 61  is attempted aga
cd7d0 69 6e 73 74 20 61 6e 20 69 6e 6f 64 65 20 74 68  inst an inode th
cd7e0 61 74 20 69 73 20 68 6f 6c 64 69 6e 67 0a 2a 2a  at is holding.**
cd7f0 20 6c 6f 63 6b 73 2c 20 74 68 65 20 63 6c 6f 73   locks, the clos
cd800 65 20 69 73 20 64 65 66 65 72 72 65 64 20 75 6e  e is deferred un
cd810 74 69 6c 20 61 6c 6c 20 6c 6f 63 6b 73 20 63 6c  til all locks cl
cd820 65 61 72 20 62 79 20 61 64 64 69 6e 67 20 74 68  ear by adding th
cd830 65 0a 2a 2a 20 66 69 6c 65 20 64 65 73 63 72 69  e.** file descri
cd840 70 74 6f 72 20 74 6f 20 62 65 20 63 6c 6f 73 65  ptor to be close
cd850 64 20 74 6f 20 74 68 65 20 70 65 6e 64 69 6e 67  d to the pending
cd860 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44   list..**.** TOD
cd870 4f 3a 20 20 43 6f 6e 73 69 64 65 72 20 63 68 61  O:  Consider cha
cd880 6e 67 69 6e 67 20 74 68 69 73 20 73 6f 20 74 68  nging this so th
cd890 61 74 20 74 68 65 72 65 20 69 73 20 6f 6e 6c 79  at there is only
cd8a0 20 61 20 73 69 6e 67 6c 65 20 66 69 6c 65 0a 2a   a single file.*
cd8b0 2a 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72  * descriptor for
cd8c0 20 65 61 63 68 20 6f 70 65 6e 20 66 69 6c 65 2c   each open file,
cd8d0 20 65 76 65 6e 20 77 68 65 6e 20 69 74 20 69 73   even when it is
cd8e0 20 6f 70 65 6e 65 64 20 6d 75 6c 74 69 70 6c 65   opened multiple
cd8f0 20 74 69 6d 65 73 2e 0a 2a 2a 20 54 68 65 20 63   times..** The c
cd900 6c 6f 73 65 28 29 20 73 79 73 74 65 6d 20 63 61  lose() system ca
cd910 6c 6c 20 77 6f 75 6c 64 20 6f 6e 6c 79 20 6f 63  ll would only oc
cd920 63 75 72 20 77 68 65 6e 20 74 68 65 20 6c 61 73  cur when the las
cd930 74 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 75 73  t database.** us
cd940 69 6e 67 20 74 68 65 20 66 69 6c 65 20 63 6c 6f  ing the file clo
cd950 73 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 75  ses..*/.struct u
cd960 6e 69 78 4f 70 65 6e 43 6e 74 20 7b 0a 20 20 73  nixOpenCnt {.  s
cd970 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 49 64  truct unixFileId
cd980 20 66 69 6c 65 49 64 3b 20 20 20 2f 2a 20 54 68   fileId;   /* Th
cd990 65 20 6c 6f 6f 6b 75 70 20 6b 65 79 20 2a 2f 0a  e lookup key */.
cd9a0 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20    int nRef;     
cd9b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
cd9c0 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74   Number of point
cd9d0 65 72 73 20 74 6f 20 74 68 69 73 20 73 74 72 75  ers to this stru
cd9e0 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  cture */.  int n
cd9f0 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20  Lock;           
cda00 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
cda10 20 6f 66 20 6f 75 74 73 74 61 6e 64 69 6e 67 20   of outstanding 
cda20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 55 6e 69 78 55  locks */.  UnixU
cda30 6e 75 73 65 64 46 64 20 2a 70 55 6e 75 73 65 64  nusedFd *pUnused
cda40 3b 20 20 20 20 20 20 2f 2a 20 55 6e 75 73 65 64  ;      /* Unused
cda50 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
cda60 73 20 74 6f 20 63 6c 6f 73 65 20 2a 2f 0a 23 69  s to close */.#i
cda70 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73  f OS_VXWORKS.  s
cda80 65 6d 5f 74 20 2a 70 53 65 6d 3b 20 20 20 20 20  em_t *pSem;     
cda90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cdaa0 2f 2a 20 4e 61 6d 65 64 20 50 4f 53 49 58 20 73  /* Named POSIX s
cdab0 65 6d 61 70 68 6f 72 65 20 2a 2f 0a 20 20 63 68  emaphore */.  ch
cdac0 61 72 20 61 53 65 6d 4e 61 6d 65 5b 4d 41 58 5f  ar aSemName[MAX_
cdad0 50 41 54 48 4e 41 4d 45 2b 32 5d 3b 20 20 20 2f  PATHNAME+2];   /
cdae0 2a 20 4e 61 6d 65 20 6f 66 20 74 68 61 74 20 73  * Name of that s
cdaf0 65 6d 61 70 68 6f 72 65 20 2a 2f 0a 23 65 6e 64  emaphore */.#end
cdb00 69 66 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78  if.  struct unix
cdb10 4f 70 65 6e 43 6e 74 20 2a 70 4e 65 78 74 2c 20  OpenCnt *pNext, 
cdb20 2a 70 50 72 65 76 3b 20 20 20 2f 2a 20 4c 69 73  *pPrev;   /* Lis
cdb30 74 20 6f 66 20 61 6c 6c 20 75 6e 69 78 4f 70 65  t of all unixOpe
cdb40 6e 43 6e 74 20 6f 62 6a 65 63 74 73 20 2a 2f 0a  nCnt objects */.
cdb50 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4c 69 73 74 73 20  };../*.** Lists 
cdb60 6f 66 20 61 6c 6c 20 75 6e 69 78 4c 6f 63 6b 49  of all unixLockI
cdb70 6e 66 6f 20 61 6e 64 20 75 6e 69 78 4f 70 65 6e  nfo and unixOpen
cdb80 43 6e 74 20 6f 62 6a 65 63 74 73 2e 20 20 54 68  Cnt objects.  Th
cdb90 65 73 65 20 75 73 65 64 20 74 6f 20 62 65 20 68  ese used to be h
cdba0 61 73 68 0a 2a 2a 20 74 61 62 6c 65 73 2e 20 20  ash.** tables.  
cdbb0 42 75 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  But the number o
cdbc0 66 20 6f 62 6a 65 63 74 73 20 69 73 20 72 61 72  f objects is rar
cdbd0 65 6c 79 20 6d 6f 72 65 20 74 68 61 6e 20 61 20  ely more than a 
cdbe0 64 6f 7a 65 6e 20 61 6e 64 0a 2a 2a 20 6e 65 76  dozen and.** nev
cdbf0 65 72 20 65 78 63 65 65 64 73 20 61 20 66 65 77  er exceeds a few
cdc00 20 74 68 6f 75 73 61 6e 64 2e 20 20 41 6e 64 20   thousand.  And 
cdc10 6c 6f 6f 6b 75 70 20 69 73 20 6e 6f 74 20 6f 6e  lookup is not on
cdc20 20 61 20 63 72 69 74 69 63 61 6c 0a 2a 2a 20 70   a critical.** p
cdc30 61 74 68 20 73 6f 20 61 20 73 69 6d 70 6c 65 20  ath so a simple 
cdc40 6c 69 6e 6b 65 64 20 6c 69 73 74 20 77 69 6c 6c  linked list will
cdc50 20 73 75 66 66 69 63 65 2e 0a 2a 2f 0a 73 74 61   suffice..*/.sta
cdc60 74 69 63 20 73 74 72 75 63 74 20 75 6e 69 78 4c  tic struct unixL
cdc70 6f 63 6b 49 6e 66 6f 20 2a 6c 6f 63 6b 4c 69 73  ockInfo *lockLis
cdc80 74 20 3d 20 30 3b 0a 73 74 61 74 69 63 20 73 74  t = 0;.static st
cdc90 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74  ruct unixOpenCnt
cdca0 20 2a 6f 70 65 6e 4c 69 73 74 20 3d 20 30 3b 0a   *openList = 0;.
cdcb0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 61 72 69  ./*.** This vari
cdcc0 61 62 6c 65 20 72 65 6d 65 6d 62 65 72 73 20 77  able remembers w
cdcd0 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
cdce0 72 65 61 64 73 20 63 61 6e 20 6f 76 65 72 72 69  reads can overri
cdcf0 64 65 20 65 61 63 68 20 6f 74 68 65 72 73 0a 2a  de each others.*
cdd00 2a 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 20  * locks..**.**  
cdd10 20 20 30 3a 20 20 4e 6f 2e 20 20 54 68 72 65 61    0:  No.  Threa
cdd20 64 73 20 63 61 6e 6e 6f 74 20 6f 76 65 72 72 69  ds cannot overri
cdd30 64 65 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c  de each others l
cdd40 6f 63 6b 73 2e 20 20 28 4c 69 6e 75 78 54 68 72  ocks.  (LinuxThr
cdd50 65 61 64 73 29 0a 2a 2a 20 20 20 20 31 3a 20 20  eads).**    1:  
cdd60 59 65 73 2e 20 20 54 68 72 65 61 64 73 20 63 61  Yes.  Threads ca
cdd70 6e 20 6f 76 65 72 72 69 64 65 20 65 61 63 68 20  n override each 
cdd80 6f 74 68 65 72 73 20 6c 6f 63 6b 73 2e 20 20 28  others locks.  (
cdd90 50 6f 73 69 78 20 26 20 4e 4c 50 54 29 0a 2a 2a  Posix & NLPT).**
cdda0 20 20 20 2d 31 3a 20 20 57 65 20 64 6f 6e 27 74     -1:  We don't
cddb0 20 6b 6e 6f 77 20 79 65 74 2e 0a 2a 2a 0a 2a 2a   know yet..**.**
cddc0 20 4f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73   On some systems
cddd0 2c 20 77 65 20 6b 6e 6f 77 20 61 74 20 63 6f 6d  , we know at com
cdde0 70 69 6c 65 2d 74 69 6d 65 20 69 66 20 74 68 72  pile-time if thr
cddf0 65 61 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64  eads can overrid
cde00 65 20 65 61 63 68 0a 2a 2a 20 6f 74 68 65 72 73  e each.** others
cde10 20 6c 6f 63 6b 73 2e 20 20 4f 6e 20 74 68 6f 73   locks.  On thos
cde20 65 20 73 79 73 74 65 6d 73 2c 20 74 68 65 20 53  e systems, the S
cde30 51 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 45  QLITE_THREAD_OVE
cde40 52 52 49 44 45 5f 4c 4f 43 4b 20 6d 61 63 72 6f  RRIDE_LOCK macro
cde50 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 73 65 74 20  .** will be set 
cde60 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 20 20  appropriately.  
cde70 4f 6e 20 6f 74 68 65 72 20 73 79 73 74 65 6d 73  On other systems
cde80 2c 20 77 65 20 68 61 76 65 20 74 6f 20 63 68 65  , we have to che
cde90 63 6b 20 61 74 0a 2a 2a 20 72 75 6e 74 69 6d 65  ck at.** runtime
cdea0 2e 20 20 4f 6e 20 74 68 65 73 65 20 6c 61 74 74  .  On these latt
cdeb0 65 72 20 73 79 73 74 65 6d 73 2c 20 53 51 4c 54  er systems, SQLT
cdec0 49 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49  IE_THREAD_OVERRI
cded0 44 45 5f 4c 4f 43 4b 20 69 73 0a 2a 2a 20 75 6e  DE_LOCK is.** un
cdee0 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  defined..**.** T
cdef0 68 69 73 20 76 61 72 69 61 62 6c 65 20 6e 6f 72  his variable nor
cdf00 6d 61 6c 6c 79 20 68 61 73 20 66 69 6c 65 20 73  mally has file s
cdf10 63 6f 70 65 20 6f 6e 6c 79 2e 20 20 42 75 74 20  cope only.  But 
cdf20 64 75 72 69 6e 67 20 74 65 73 74 69 6e 67 2c 20  during testing, 
cdf30 77 65 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 61 20  we make.** it a 
cdf40 67 6c 6f 62 61 6c 20 73 6f 20 74 68 61 74 20 74  global so that t
cdf50 68 65 20 74 65 73 74 20 63 6f 64 65 20 63 61 6e  he test code can
cdf60 20 63 68 61 6e 67 65 20 69 74 73 20 76 61 6c 75   change its valu
cdf70 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 76 65  e in order to ve
cdf80 72 69 66 79 0a 2a 2a 20 74 68 61 74 20 74 68 65  rify.** that the
cdf90 20 72 69 67 68 74 20 73 74 75 66 66 20 68 61 70   right stuff hap
cdfa0 70 65 6e 73 20 69 6e 20 65 69 74 68 65 72 20 63  pens in either c
cdfb0 61 73 65 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49  ase..*/.#if SQLI
cdfc0 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26  TE_THREADSAFE &&
cdfd0 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78   defined(__linux
cdfe0 5f 5f 29 0a 23 20 20 69 66 6e 64 65 66 20 53 51  __).#  ifndef SQ
cdff0 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 52  LITE_THREAD_OVER
ce000 52 49 44 45 5f 4c 4f 43 4b 0a 23 20 20 20 20 64  RIDE_LOCK.#    d
ce010 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 48 52  efine SQLITE_THR
ce020 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43  EAD_OVERRIDE_LOC
ce030 4b 20 2d 31 0a 23 20 20 65 6e 64 69 66 0a 23 20  K -1.#  endif.# 
ce040 20 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45   ifdef SQLITE_TE
ce050 53 54 0a 69 6e 74 20 74 68 72 65 61 64 73 4f 76  ST.int threadsOv
ce060 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73  errideEachOthers
ce070 4c 6f 63 6b 73 20 3d 20 53 51 4c 49 54 45 5f 54  Locks = SQLITE_T
ce080 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c  HREAD_OVERRIDE_L
ce090 4f 43 4b 3b 0a 23 20 20 65 6c 73 65 0a 73 74 61  OCK;.#  else.sta
ce0a0 74 69 63 20 69 6e 74 20 74 68 72 65 61 64 73 4f  tic int threadsO
ce0b0 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72  verrideEachOther
ce0c0 73 4c 6f 63 6b 73 20 3d 20 53 51 4c 49 54 45 5f  sLocks = SQLITE_
ce0d0 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f  THREAD_OVERRIDE_
ce0e0 4c 4f 43 4b 3b 0a 23 20 20 65 6e 64 69 66 0a 23  LOCK;.#  endif.#
ce0f0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  endif../*.** Thi
ce100 73 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64  s structure hold
ce110 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 70 61  s information pa
ce120 73 73 65 64 20 69 6e 74 6f 20 69 6e 64 69 76 69  ssed into indivi
ce130 64 75 61 6c 20 74 65 73 74 0a 2a 2a 20 74 68 72  dual test.** thr
ce140 65 61 64 73 20 62 79 20 74 68 65 20 74 65 73 74  eads by the test
ce150 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68  ThreadLockingBeh
ce160 61 76 69 6f 72 28 29 20 72 6f 75 74 69 6e 65 2e  avior() routine.
ce170 0a 2a 2f 0a 73 74 72 75 63 74 20 74 68 72 65 61  .*/.struct threa
ce180 64 54 65 73 74 44 61 74 61 20 7b 0a 20 20 69 6e  dTestData {.  in
ce190 74 20 66 64 3b 20 20 20 20 20 20 20 20 20 20 20  t fd;           
ce1a0 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20       /* File to 
ce1b0 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 73  be locked */.  s
ce1c0 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b  truct flock lock
ce1d0 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 63  ;     /* The loc
ce1e0 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 2a  king operation *
ce1f0 2f 0a 20 20 69 6e 74 20 72 65 73 75 6c 74 3b 20  /.  int result; 
ce200 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
ce210 73 75 6c 74 20 6f 66 20 74 68 65 20 6c 6f 63 6b  sult of the lock
ce220 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f  ing operation */
ce230 0a 7d 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f  .};..#if SQLITE_
ce240 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65  THREADSAFE && de
ce250 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29  fined(__linux__)
ce260 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
ce270 74 69 6f 6e 20 69 73 20 75 73 65 64 20 61 73 20  tion is used as 
ce280 74 68 65 20 6d 61 69 6e 20 72 6f 75 74 69 6e 65  the main routine
ce290 20 66 6f 72 20 61 20 74 68 72 65 61 64 20 6c 61   for a thread la
ce2a0 75 6e 63 68 65 64 20 62 79 0a 2a 2a 20 74 65 73  unched by.** tes
ce2b0 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65  tThreadLockingBe
ce2c0 68 61 76 69 6f 72 28 29 2e 20 49 74 20 74 65 73  havior(). It tes
ce2d0 74 73 20 77 68 65 74 68 65 72 20 74 68 65 20 73  ts whether the s
ce2e0 68 61 72 65 64 2d 6c 6f 63 6b 20 6f 62 74 61 69  hared-lock obtai
ce2f0 6e 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 6d 61  ned.** by the ma
ce300 69 6e 20 74 68 72 65 61 64 20 69 6e 20 74 65 73  in thread in tes
ce310 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65  tThreadLockingBe
ce320 68 61 76 69 6f 72 28 29 20 63 6f 6e 66 6c 69 63  havior() conflic
ce330 74 73 20 77 69 74 68 20 61 0a 2a 2a 20 68 79 70  ts with a.** hyp
ce340 6f 74 68 65 74 69 63 61 6c 20 77 72 69 74 65 2d  othetical write-
ce350 6c 6f 63 6b 20 6f 62 74 61 69 6e 65 64 20 62 79  lock obtained by
ce360 20 74 68 69 73 20 74 68 72 65 61 64 20 6f 6e 20   this thread on 
ce370 74 68 65 20 73 61 6d 65 20 66 69 6c 65 2e 0a 2a  the same file..*
ce380 2a 0a 2a 2a 20 54 68 65 20 77 72 69 74 65 2d 6c  *.** The write-l
ce390 6f 63 6b 20 69 73 20 6e 6f 74 20 61 63 74 75 61  ock is not actua
ce3a0 6c 6c 79 20 61 63 71 75 69 72 65 64 2c 20 61 73  lly acquired, as
ce3b0 20 74 68 69 73 20 69 73 20 6e 6f 74 20 70 6f 73   this is not pos
ce3c0 73 69 62 6c 65 20 69 66 20 0a 2a 2a 20 74 68 65  sible if .** the
ce3d0 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 20 69 6e   file is open in
ce3e0 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64 65 20   read-only mode 
ce3f0 28 73 65 65 20 74 69 63 6b 65 74 20 23 33 34 37  (see ticket #347
ce400 32 29 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20 76  2)..*/ .static v
ce410 6f 69 64 20 2a 74 68 72 65 61 64 4c 6f 63 6b 69  oid *threadLocki
ce420 6e 67 54 65 73 74 28 76 6f 69 64 20 2a 70 41 72  ngTest(void *pAr
ce430 67 29 7b 0a 20 20 73 74 72 75 63 74 20 74 68 72  g){.  struct thr
ce440 65 61 64 54 65 73 74 44 61 74 61 20 2a 70 44 61  eadTestData *pDa
ce450 74 61 20 3d 20 28 73 74 72 75 63 74 20 74 68 72  ta = (struct thr
ce460 65 61 64 54 65 73 74 44 61 74 61 2a 29 70 41 72  eadTestData*)pAr
ce470 67 3b 0a 20 20 70 44 61 74 61 2d 3e 72 65 73 75  g;.  pData->resu
ce480 6c 74 20 3d 20 66 63 6e 74 6c 28 70 44 61 74 61  lt = fcntl(pData
ce490 2d 3e 66 64 2c 20 46 5f 47 45 54 4c 4b 2c 20 26  ->fd, F_GETLK, &
ce4a0 70 44 61 74 61 2d 3e 6c 6f 63 6b 29 3b 0a 20 20  pData->lock);.  
ce4b0 72 65 74 75 72 6e 20 70 41 72 67 3b 0a 7d 0a 23  return pArg;.}.#
ce4c0 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
ce4d0 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65  THREADSAFE && de
ce4e0 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29  fined(__linux__)
ce4f0 20 2a 2f 0a 0a 0a 23 69 66 20 53 51 4c 49 54 45   */...#if SQLITE
ce500 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64  _THREADSAFE && d
ce510 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f  efined(__linux__
ce520 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 70 72 6f  )./*.** This pro
ce530 63 65 64 75 72 65 20 61 74 74 65 6d 70 74 73 20  cedure attempts 
ce540 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65  to determine whe
ce550 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65  ther or not thre
ce560 61 64 73 0a 2a 2a 20 63 61 6e 20 6f 76 65 72 72  ads.** can overr
ce570 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 73 20  ide each others 
ce580 6c 6f 63 6b 73 20 74 68 65 6e 20 73 65 74 73 20  locks then sets 
ce590 74 68 65 20 0a 2a 2a 20 74 68 72 65 61 64 73 4f  the .** threadsO
ce5a0 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72  verrideEachOther
ce5b0 73 4c 6f 63 6b 73 20 76 61 72 69 61 62 6c 65 20  sLocks variable 
ce5c0 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a  appropriately..*
ce5d0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 65  /.static void te
ce5e0 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42  stThreadLockingB
ce5f0 65 68 61 76 69 6f 72 28 69 6e 74 20 66 64 5f 6f  ehavior(int fd_o
ce600 72 69 67 29 7b 0a 20 20 69 6e 74 20 66 64 3b 0a  rig){.  int fd;.
ce610 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 74 72 75    int rc;.  stru
ce620 63 74 20 74 68 72 65 61 64 54 65 73 74 44 61 74  ct threadTestDat
ce630 61 20 64 3b 0a 20 20 73 74 72 75 63 74 20 66 6c  a d;.  struct fl
ce640 6f 63 6b 20 6c 3b 0a 20 20 70 74 68 72 65 61 64  ock l;.  pthread
ce650 5f 74 20 74 3b 0a 0a 20 20 66 64 20 3d 20 64 75  _t t;..  fd = du
ce660 70 28 66 64 5f 6f 72 69 67 29 3b 0a 20 20 69 66  p(fd_orig);.  if
ce670 28 20 66 64 3c 30 20 29 20 72 65 74 75 72 6e 3b  ( fd<0 ) return;
ce680 0a 20 20 6d 65 6d 73 65 74 28 26 6c 2c 20 30 2c  .  memset(&l, 0,
ce690 20 73 69 7a 65 6f 66 28 6c 29 29 3b 0a 20 20 6c   sizeof(l));.  l
ce6a0 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c 43  .l_type = F_RDLC
ce6b0 4b 3b 0a 20 20 6c 2e 6c 5f 6c 65 6e 20 3d 20 31  K;.  l.l_len = 1
ce6c0 3b 0a 20 20 6c 2e 6c 5f 73 74 61 72 74 20 3d 20  ;.  l.l_start = 
ce6d0 30 3b 0a 20 20 6c 2e 6c 5f 77 68 65 6e 63 65 20  0;.  l.l_whence 
ce6e0 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 72 63  = SEEK_SET;.  rc
ce6f0 20 3d 20 66 63 6e 74 6c 28 66 64 5f 6f 72 69 67   = fcntl(fd_orig
ce700 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 29 3b 0a  , F_SETLK, &l);.
ce710 20 20 69 66 28 20 72 63 21 3d 30 20 29 20 72 65    if( rc!=0 ) re
ce720 74 75 72 6e 3b 0a 20 20 6d 65 6d 73 65 74 28 26  turn;.  memset(&
ce730 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28 64 29 29  d, 0, sizeof(d))
ce740 3b 0a 20 20 64 2e 66 64 20 3d 20 66 64 3b 0a 20  ;.  d.fd = fd;. 
ce750 20 64 2e 6c 6f 63 6b 20 3d 20 6c 3b 0a 20 20 64   d.lock = l;.  d
ce760 2e 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46  .lock.l_type = F
ce770 5f 57 52 4c 43 4b 3b 0a 20 20 69 66 28 20 70 74  _WRLCK;.  if( pt
ce780 68 72 65 61 64 5f 63 72 65 61 74 65 28 26 74 2c  hread_create(&t,
ce790 20 30 2c 20 74 68 72 65 61 64 4c 6f 63 6b 69 6e   0, threadLockin
ce7a0 67 54 65 73 74 2c 20 26 64 29 3d 3d 30 20 29 7b  gTest, &d)==0 ){
ce7b0 0a 20 20 20 20 70 74 68 72 65 61 64 5f 6a 6f 69  .    pthread_joi
ce7c0 6e 28 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 63  n(t, 0);.  }.  c
ce7d0 6c 6f 73 65 28 66 64 29 3b 0a 20 20 69 66 28 20  lose(fd);.  if( 
ce7e0 64 2e 72 65 73 75 6c 74 21 3d 30 20 29 20 72 65  d.result!=0 ) re
ce7f0 74 75 72 6e 3b 0a 20 20 74 68 72 65 61 64 73 4f  turn;.  threadsO
ce800 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72  verrideEachOther
ce810 73 4c 6f 63 6b 73 20 3d 20 28 64 2e 6c 6f 63 6b  sLocks = (d.lock
ce820 2e 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b  .l_type==F_UNLCK
ce830 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  );.}.#endif /* S
ce840 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
ce850 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69   && defined(__li
ce860 6e 75 78 5f 5f 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  nux__) */../*.**
ce870 20 52 65 6c 65 61 73 65 20 61 20 75 6e 69 78 4c   Release a unixL
ce880 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63 74 75 72  ockInfo structur
ce890 65 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c  e previously all
ce8a0 6f 63 61 74 65 64 20 62 79 20 66 69 6e 64 4c 6f  ocated by findLo
ce8b0 63 6b 49 6e 66 6f 28 29 2e 0a 2a 2a 0a 2a 2a 20  ckInfo()..**.** 
ce8c0 54 68 65 20 6d 75 74 65 78 20 65 6e 74 65 72 65  The mutex entere
ce8d0 64 20 75 73 69 6e 67 20 74 68 65 20 75 6e 69 78  d using the unix
ce8e0 45 6e 74 65 72 4d 75 74 65 78 28 29 20 66 75 6e  EnterMutex() fun
ce8f0 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 68 65  ction must be he
ce900 6c 64 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20  ld.** when this 
ce910 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
ce920 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ed..*/.static vo
ce930 69 64 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e  id releaseLockIn
ce940 66 6f 28 73 74 72 75 63 74 20 75 6e 69 78 4c 6f  fo(struct unixLo
ce950 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 29 7b 0a  ckInfo *pLock){.
ce960 20 20 61 73 73 65 72 74 28 20 75 6e 69 78 4d 75    assert( unixMu
ce970 74 65 78 48 65 6c 64 28 29 20 29 3b 0a 20 20 69  texHeld() );.  i
ce980 66 28 20 70 4c 6f 63 6b 20 29 7b 0a 20 20 20 20  f( pLock ){.    
ce990 70 4c 6f 63 6b 2d 3e 6e 52 65 66 2d 2d 3b 0a 20  pLock->nRef--;. 
ce9a0 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 6e 52     if( pLock->nR
ce9b0 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69  ef==0 ){.      i
ce9c0 66 28 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 20  f( pLock->pPrev 
ce9d0 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ){.        asser
ce9e0 74 28 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 2d  t( pLock->pPrev-
ce9f0 3e 70 4e 65 78 74 3d 3d 70 4c 6f 63 6b 20 29 3b  >pNext==pLock );
cea00 0a 20 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e  .        pLock->
cea10 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70  pPrev->pNext = p
cea20 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  Lock->pNext;.   
cea30 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
cea40 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 4c 69    assert( lockLi
cea50 73 74 3d 3d 70 4c 6f 63 6b 20 29 3b 0a 20 20 20  st==pLock );.   
cea60 20 20 20 20 20 6c 6f 63 6b 4c 69 73 74 20 3d 20       lockList = 
cea70 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20  pLock->pNext;.  
cea80 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
cea90 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 20 29 7b 0a  pLock->pNext ){.
ceaa0 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
ceab0 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 2d 3e 70 50  pLock->pNext->pP
ceac0 72 65 76 3d 3d 70 4c 6f 63 6b 20 29 3b 0a 20 20  rev==pLock );.  
cead0 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 4e 65        pLock->pNe
ceae0 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 4c 6f 63  xt->pPrev = pLoc
ceaf0 6b 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 20 20  k->pPrev;.      
ceb00 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  }.      sqlite3_
ceb10 66 72 65 65 28 70 4c 6f 63 6b 29 3b 0a 20 20 20  free(pLock);.   
ceb20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
ceb30 52 65 6c 65 61 73 65 20 61 20 75 6e 69 78 4f 70  Release a unixOp
ceb40 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 20  enCnt structure 
ceb50 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63  previously alloc
ceb60 61 74 65 64 20 62 79 20 66 69 6e 64 4c 6f 63 6b  ated by findLock
ceb70 49 6e 66 6f 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68  Info()..**.** Th
ceb80 65 20 6d 75 74 65 78 20 65 6e 74 65 72 65 64 20  e mutex entered 
ceb90 75 73 69 6e 67 20 74 68 65 20 75 6e 69 78 45 6e  using the unixEn
ceba0 74 65 72 4d 75 74 65 78 28 29 20 66 75 6e 63 74  terMutex() funct
cebb0 69 6f 6e 20 6d 75 73 74 20 62 65 20 68 65 6c 64  ion must be held
cebc0 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 66 75  .** when this fu
cebd0 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
cebe0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
cebf0 20 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28   releaseOpenCnt(
cec00 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43  struct unixOpenC
cec10 6e 74 20 2a 70 4f 70 65 6e 29 7b 0a 20 20 61 73  nt *pOpen){.  as
cec20 73 65 72 74 28 20 75 6e 69 78 4d 75 74 65 78 48  sert( unixMutexH
cec30 65 6c 64 28 29 20 29 3b 0a 20 20 69 66 28 20 70  eld() );.  if( p
cec40 4f 70 65 6e 20 29 7b 0a 20 20 20 20 70 4f 70 65  Open ){.    pOpe
cec50 6e 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 69  n->nRef--;.    i
cec60 66 28 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 3d 3d  f( pOpen->nRef==
cec70 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  0 ){.      if( p
cec80 4f 70 65 6e 2d 3e 70 50 72 65 76 20 29 7b 0a 20  Open->pPrev ){. 
cec90 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
ceca0 4f 70 65 6e 2d 3e 70 50 72 65 76 2d 3e 70 4e 65  Open->pPrev->pNe
cecb0 78 74 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20  xt==pOpen );.   
cecc0 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 50 72 65       pOpen->pPre
cecd0 76 2d 3e 70 4e 65 78 74 20 3d 20 70 4f 70 65 6e  v->pNext = pOpen
cece0 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d  ->pNext;.      }
cecf0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73  else{.        as
ced00 73 65 72 74 28 20 6f 70 65 6e 4c 69 73 74 3d 3d  sert( openList==
ced10 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20 20 20  pOpen );.       
ced20 20 6f 70 65 6e 4c 69 73 74 20 3d 20 70 4f 70 65   openList = pOpe
ced30 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20  n->pNext;.      
ced40 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 65  }.      if( pOpe
ced50 6e 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20  n->pNext ){.    
ced60 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 65      assert( pOpe
ced70 6e 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 3d  n->pNext->pPrev=
ced80 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20 20  =pOpen );.      
ced90 20 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 2d 3e    pOpen->pNext->
ceda0 70 50 72 65 76 20 3d 20 70 4f 70 65 6e 2d 3e 70  pPrev = pOpen->p
cedb0 50 72 65 76 3b 0a 20 20 20 20 20 20 7d 0a 23 69  Prev;.      }.#i
cedc0 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  f SQLITE_THREADS
cedd0 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f  AFE && defined(_
cede0 5f 6c 69 6e 75 78 5f 5f 29 0a 20 20 20 20 20 20  _linux__).      
cedf0 61 73 73 65 72 74 28 20 21 70 4f 70 65 6e 2d 3e  assert( !pOpen->
cee00 70 55 6e 75 73 65 64 20 7c 7c 20 74 68 72 65 61  pUnused || threa
cee10 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74  dsOverrideEachOt
cee20 68 65 72 73 4c 6f 63 6b 73 3d 3d 30 20 29 3b 0a  hersLocks==0 );.
cee30 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 2f 2a  #endif..      /*
cee40 20 49 66 20 70 4f 70 65 6e 2d 3e 70 55 6e 75 73   If pOpen->pUnus
cee50 65 64 20 69 73 20 6e 6f 74 20 6e 75 6c 6c 2c 20  ed is not null, 
cee60 74 68 65 6e 20 6d 65 6d 6f 72 79 20 61 6e 64 20  then memory and 
cee70 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f 72 73  file-descriptors
cee80 0a 20 20 20 20 20 20 2a 2a 20 61 72 65 20 6c 65  .      ** are le
cee90 61 6b 65 64 2e 0a 20 20 20 20 20 20 2a 2a 0a 20  aked..      **. 
ceea0 20 20 20 20 20 2a 2a 20 54 68 69 73 20 77 69 6c       ** This wil
ceeb0 6c 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 69 66  l only happen if
ceec0 2c 20 75 6e 64 65 72 20 4c 69 6e 75 78 74 68 72  , under Linuxthr
ceed0 65 61 64 73 2c 20 74 68 65 20 75 73 65 72 20 68  eads, the user h
ceee0 61 73 20 6f 70 65 6e 65 64 0a 20 20 20 20 20 20  as opened.      
ceef0 2a 2a 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  ** a transaction
cef00 20 69 6e 20 6f 6e 65 20 74 68 72 65 61 64 2c 20   in one thread, 
cef10 74 68 65 6e 20 61 74 74 65 6d 70 74 73 20 74 6f  then attempts to
cef20 20 63 6c 6f 73 65 20 74 68 65 20 64 61 74 61 62   close the datab
cef30 61 73 65 0a 20 20 20 20 20 20 2a 2a 20 68 61 6e  ase.      ** han
cef40 64 6c 65 20 66 72 6f 6d 20 61 6e 6f 74 68 65 72  dle from another
cef50 20 74 68 72 65 61 64 20 28 77 69 74 68 6f 75 74   thread (without
cef60 20 66 69 72 73 74 20 75 6e 6c 6f 63 6b 69 6e 67   first unlocking
cef70 20 74 68 65 20 64 62 20 66 69 6c 65 29 2e 0a 20   the db file).. 
cef80 20 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20       ** This is 
cef90 61 20 6d 69 73 75 73 65 2e 20 20 2a 2f 0a 20 20  a misuse.  */.  
cefa0 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
cefb0 28 70 4f 70 65 6e 29 3b 0a 20 20 20 20 7d 0a 20  (pOpen);.    }. 
cefc0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65   }.}../*.** Give
cefd0 6e 20 61 20 66 69 6c 65 20 64 65 73 63 72 69 70  n a file descrip
cefe0 74 6f 72 2c 20 6c 6f 63 61 74 65 20 75 6e 69 78  tor, locate unix
ceff0 4c 6f 63 6b 49 6e 66 6f 20 61 6e 64 20 75 6e 69  LockInfo and uni
cf000 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75  xOpenCnt structu
cf010 72 65 73 20 74 68 61 74 0a 2a 2a 20 64 65 73 63  res that.** desc
cf020 72 69 62 65 73 20 74 68 61 74 20 66 69 6c 65 20  ribes that file 
cf030 64 65 73 63 72 69 70 74 6f 72 2e 20 20 43 72 65  descriptor.  Cre
cf040 61 74 65 20 6e 65 77 20 6f 6e 65 73 20 69 66 20  ate new ones if 
cf050 6e 65 63 65 73 73 61 72 79 2e 20 20 54 68 65 0a  necessary.  The.
cf060 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c 75 65 73  ** return values
cf070 20 6d 69 67 68 74 20 62 65 20 75 6e 69 6e 69 74   might be uninit
cf080 69 61 6c 69 7a 65 64 20 69 66 20 61 6e 20 65 72  ialized if an er
cf090 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a  ror occurs..**.*
cf0a0 2a 20 54 68 65 20 6d 75 74 65 78 20 65 6e 74 65  * The mutex ente
cf0b0 72 65 64 20 75 73 69 6e 67 20 74 68 65 20 75 6e  red using the un
cf0c0 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 20 66  ixEnterMutex() f
cf0d0 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20  unction must be 
cf0e0 68 65 6c 64 0a 2a 2a 20 77 68 65 6e 20 74 68 69  held.** when thi
cf0f0 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
cf100 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  lled..**.** Retu
cf110 72 6e 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74  rn an appropriat
cf120 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f  e error code..*/
cf130 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6e 64  .static int find
cf140 4c 6f 63 6b 49 6e 66 6f 28 0a 20 20 75 6e 69 78  LockInfo(.  unix
cf150 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 20 20  File *pFile,    
cf160 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e             /* Un
cf170 69 78 20 66 69 6c 65 20 77 69 74 68 20 66 69 6c  ix file with fil
cf180 65 20 64 65 73 63 20 75 73 65 64 20 69 6e 20 74  e desc used in t
cf190 68 65 20 6b 65 79 20 2a 2f 0a 20 20 73 74 72 75  he key */.  stru
cf1a0 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20  ct unixLockInfo 
cf1b0 2a 2a 70 70 4c 6f 63 6b 2c 20 20 2f 2a 20 52 65  **ppLock,  /* Re
cf1c0 74 75 72 6e 20 74 68 65 20 75 6e 69 78 4c 6f 63  turn the unixLoc
cf1d0 6b 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  kInfo structure 
cf1e0 68 65 72 65 20 2a 2f 0a 20 20 73 74 72 75 63 74  here */.  struct
cf1f0 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 2a 70   unixOpenCnt **p
cf200 70 4f 70 65 6e 20 20 20 20 2f 2a 20 52 65 74 75  pOpen    /* Retu
cf210 72 6e 20 74 68 65 20 75 6e 69 78 4f 70 65 6e 43  rn the unixOpenC
cf220 6e 74 20 73 74 72 75 63 74 75 72 65 20 68 65 72  nt structure her
cf230 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  e */.){.  int rc
cf240 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
cf250 20 20 20 20 20 20 20 20 20 2f 2a 20 53 79 73 74           /* Syst
cf260 65 6d 20 63 61 6c 6c 20 72 65 74 75 72 6e 20 63  em call return c
cf270 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 66 64 3b  ode */.  int fd;
cf280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cf290 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66          /* The f
cf2a0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 66  ile descriptor f
cf2b0 6f 72 20 70 46 69 6c 65 20 2a 2f 0a 20 20 73 74  or pFile */.  st
cf2c0 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 4b 65 79  ruct unixLockKey
cf2d0 20 6c 6f 63 6b 4b 65 79 3b 20 20 20 20 2f 2a 20   lockKey;    /* 
cf2e0 4c 6f 6f 6b 75 70 20 6b 65 79 20 66 6f 72 20 74  Lookup key for t
cf2f0 68 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20  he unixLockInfo 
cf300 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 73  structure */.  s
cf310 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 49 64  truct unixFileId
cf320 20 66 69 6c 65 49 64 3b 20 20 20 20 20 20 2f 2a   fileId;      /*
cf330 20 4c 6f 6f 6b 75 70 20 6b 65 79 20 66 6f 72 20   Lookup key for 
cf340 74 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20  the unixOpenCnt 
cf350 73 74 72 75 63 74 20 2a 2f 0a 20 20 73 74 72 75  struct */.  stru
cf360 63 74 20 73 74 61 74 20 73 74 61 74 62 75 66 3b  ct stat statbuf;
cf370 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
cf380 77 2d 6c 65 76 65 6c 20 66 69 6c 65 20 69 6e 66  w-level file inf
cf390 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 74  ormation */.  st
cf3a0 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66  ruct unixLockInf
cf3b0 6f 20 2a 70 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20  o *pLock = 0;/* 
cf3c0 43 61 6e 64 69 64 61 74 65 20 75 6e 69 78 4c 6f  Candidate unixLo
cf3d0 63 6b 49 6e 66 6f 20 6f 62 6a 65 63 74 20 2a 2f  ckInfo object */
cf3e0 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70  .  struct unixOp
cf3f0 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 3b 20 20 20  enCnt *pOpen;   
cf400 20 20 2f 2a 20 43 61 6e 64 69 64 61 74 65 20 75    /* Candidate u
cf410 6e 69 78 4f 70 65 6e 43 6e 74 20 6f 62 6a 65 63  nixOpenCnt objec
cf420 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  t */..  assert( 
cf430 75 6e 69 78 4d 75 74 65 78 48 65 6c 64 28 29 20  unixMutexHeld() 
cf440 29 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 6c 6f 77  );..  /* Get low
cf450 2d 6c 65 76 65 6c 20 69 6e 66 6f 72 6d 61 74 69  -level informati
cf460 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 66 69 6c  on about the fil
cf470 65 20 74 68 61 74 20 77 65 20 63 61 6e 20 75 73  e that we can us
cf480 65 64 20 74 6f 0a 20 20 2a 2a 20 63 72 65 61 74  ed to.  ** creat
cf490 65 20 61 20 75 6e 69 71 75 65 20 6e 61 6d 65 20  e a unique name 
cf4a0 66 6f 72 20 74 68 65 20 66 69 6c 65 2e 0a 20 20  for the file..  
cf4b0 2a 2f 0a 20 20 66 64 20 3d 20 70 46 69 6c 65 2d  */.  fd = pFile-
cf4c0 3e 68 3b 0a 20 20 72 63 20 3d 20 66 73 74 61 74  >h;.  rc = fstat
cf4d0 28 66 64 2c 20 26 73 74 61 74 62 75 66 29 3b 0a  (fd, &statbuf);.
cf4e0 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20    if( rc!=0 ){. 
cf4f0 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72     pFile->lastEr
cf500 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 23 69 66  rno = errno;.#if
cf510 64 65 66 20 45 4f 56 45 52 46 4c 4f 57 0a 20 20  def EOVERFLOW.  
cf520 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 61 73    if( pFile->las
cf530 74 45 72 72 6e 6f 3d 3d 45 4f 56 45 52 46 4c 4f  tErrno==EOVERFLO
cf540 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  W ) return SQLIT
cf550 45 5f 4e 4f 4c 46 53 3b 0a 23 65 6e 64 69 66 0a  E_NOLFS;.#endif.
cf560 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
cf570 45 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 0a 23 69  E_IOERR;.  }..#i
cf580 66 64 65 66 20 5f 5f 41 50 50 4c 45 5f 5f 0a 20  fdef __APPLE__. 
cf590 20 2f 2a 20 4f 6e 20 4f 53 20 58 20 6f 6e 20 61   /* On OS X on a
cf5a0 6e 20 6d 73 64 6f 73 20 66 69 6c 65 73 79 73 74  n msdos filesyst
cf5b0 65 6d 2c 20 74 68 65 20 69 6e 6f 64 65 20 6e 75  em, the inode nu
cf5c0 6d 62 65 72 20 69 73 20 72 65 70 6f 72 74 65 64  mber is reported
cf5d0 0a 20 20 2a 2a 20 69 6e 63 6f 72 72 65 63 74 6c  .  ** incorrectl
cf5e0 79 20 66 6f 72 20 7a 65 72 6f 2d 73 69 7a 65 20  y for zero-size 
cf5f0 66 69 6c 65 73 2e 20 20 53 65 65 20 74 69 63 6b  files.  See tick
cf600 65 74 20 23 33 32 36 30 2e 20 20 54 6f 20 77 6f  et #3260.  To wo
cf610 72 6b 0a 20 20 2a 2a 20 61 72 6f 75 6e 64 20 74  rk.  ** around t
cf620 68 69 73 20 70 72 6f 62 6c 65 6d 20 28 77 65 20  his problem (we 
cf630 63 6f 6e 73 69 64 65 72 20 69 74 20 61 20 62 75  consider it a bu
cf640 67 20 69 6e 20 4f 53 20 58 2c 20 6e 6f 74 20 53  g in OS X, not S
cf650 51 4c 69 74 65 29 0a 20 20 2a 2a 20 77 65 20 61  QLite).  ** we a
cf660 6c 77 61 79 73 20 69 6e 63 72 65 61 73 65 20 74  lways increase t
cf670 68 65 20 66 69 6c 65 20 73 69 7a 65 20 74 6f 20  he file size to 
cf680 31 20 62 79 20 77 72 69 74 69 6e 67 20 61 20 73  1 by writing a s
cf690 69 6e 67 6c 65 20 62 79 74 65 0a 20 20 2a 2a 20  ingle byte.  ** 
cf6a0 70 72 69 6f 72 20 74 6f 20 61 63 63 65 73 73 69  prior to accessi
cf6b0 6e 67 20 74 68 65 20 69 6e 6f 64 65 20 6e 75 6d  ng the inode num
cf6c0 62 65 72 2e 20 20 54 68 65 20 6f 6e 65 20 62 79  ber.  The one by
cf6d0 74 65 20 77 72 69 74 74 65 6e 20 69 73 0a 20 20  te written is.  
cf6e0 2a 2a 20 61 6e 20 41 53 43 49 49 20 27 53 27 20  ** an ASCII 'S' 
cf6f0 63 68 61 72 61 63 74 65 72 20 77 68 69 63 68 20  character which 
cf700 61 6c 73 6f 20 68 61 70 70 65 6e 73 20 74 6f 20  also happens to 
cf710 62 65 20 74 68 65 20 66 69 72 73 74 20 62 79 74  be the first byt
cf720 65 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 68 65  e.  ** in the he
cf730 61 64 65 72 20 6f 66 20 65 76 65 72 79 20 53 51  ader of every SQ
cf740 4c 69 74 65 20 64 61 74 61 62 61 73 65 2e 20 20  Lite database.  
cf750 49 6e 20 74 68 69 73 20 77 61 79 2c 20 69 66 20  In this way, if 
cf760 74 68 65 72 65 0a 20 20 2a 2a 20 69 73 20 61 20  there.  ** is a 
cf770 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 20 73  race condition s
cf780 75 63 68 20 74 68 61 74 20 61 6e 6f 74 68 65 72  uch that another
cf790 20 74 68 72 65 61 64 20 68 61 73 20 61 6c 72 65   thread has alre
cf7a0 61 64 79 20 70 6f 70 75 6c 61 74 65 64 0a 20 20  ady populated.  
cf7b0 2a 2a 20 74 68 65 20 66 69 72 73 74 20 70 61 67  ** the first pag
cf7c0 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
cf7d0 65 2c 20 6e 6f 20 64 61 6d 61 67 65 20 69 73 20  e, no damage is 
cf7e0 64 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  done..  */.  if(
cf7f0 20 73 74 61 74 62 75 66 2e 73 74 5f 73 69 7a 65   statbuf.st_size
cf800 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ==0 ){.    rc = 
cf810 77 72 69 74 65 28 66 64 2c 20 22 53 22 2c 20 31  write(fd, "S", 1
cf820 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 31  );.    if( rc!=1
cf830 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
cf840 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20   SQLITE_IOERR;. 
cf850 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 66 73     }.    rc = fs
cf860 74 61 74 28 66 64 2c 20 26 73 74 61 74 62 75 66  tat(fd, &statbuf
cf870 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 30  );.    if( rc!=0
cf880 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d   ){.      pFile-
cf890 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72  >lastErrno = err
cf8a0 6e 6f 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  no;.      return
cf8b0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20   SQLITE_IOERR;. 
cf8c0 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a     }.  }.#endif.
cf8d0 0a 20 20 6d 65 6d 73 65 74 28 26 6c 6f 63 6b 4b  .  memset(&lockK
cf8e0 65 79 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6c 6f  ey, 0, sizeof(lo
cf8f0 63 6b 4b 65 79 29 29 3b 0a 20 20 6c 6f 63 6b 4b  ckKey));.  lockK
cf900 65 79 2e 66 69 64 2e 64 65 76 20 3d 20 73 74 61  ey.fid.dev = sta
cf910 74 62 75 66 2e 73 74 5f 64 65 76 3b 0a 23 69 66  tbuf.st_dev;.#if
cf920 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 6c 6f   OS_VXWORKS.  lo
cf930 63 6b 4b 65 79 2e 66 69 64 2e 70 49 64 20 3d 20  ckKey.fid.pId = 
cf940 70 46 69 6c 65 2d 3e 70 49 64 3b 0a 23 65 6c 73  pFile->pId;.#els
cf950 65 0a 20 20 6c 6f 63 6b 4b 65 79 2e 66 69 64 2e  e.  lockKey.fid.
cf960 69 6e 6f 20 3d 20 73 74 61 74 62 75 66 2e 73 74  ino = statbuf.st
cf970 5f 69 6e 6f 3b 0a 23 65 6e 64 69 66 0a 23 69 66  _ino;.#endif.#if
cf980 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
cf990 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f  FE && defined(__
cf9a0 6c 69 6e 75 78 5f 5f 29 0a 20 20 69 66 28 20 74  linux__).  if( t
cf9b0 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45 61  hreadsOverrideEa
cf9c0 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 3c 30 20  chOthersLocks<0 
cf9d0 29 7b 0a 20 20 20 20 74 65 73 74 54 68 72 65 61  ){.    testThrea
cf9e0 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f 72  dLockingBehavior
cf9f0 28 66 64 29 3b 0a 20 20 7d 0a 20 20 6c 6f 63 6b  (fd);.  }.  lock
cfa00 4b 65 79 2e 74 69 64 20 3d 20 74 68 72 65 61 64  Key.tid = thread
cfa10 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68  sOverrideEachOth
cfa20 65 72 73 4c 6f 63 6b 73 20 3f 20 30 20 3a 20 70  ersLocks ? 0 : p
cfa30 74 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 23  thread_self();.#
cfa40 65 6e 64 69 66 0a 20 20 66 69 6c 65 49 64 20 3d  endif.  fileId =
cfa50 20 6c 6f 63 6b 4b 65 79 2e 66 69 64 3b 0a 20 20   lockKey.fid;.  
cfa60 69 66 28 20 70 70 4c 6f 63 6b 21 3d 30 20 29 7b  if( ppLock!=0 ){
cfa70 0a 20 20 20 20 70 4c 6f 63 6b 20 3d 20 6c 6f 63  .    pLock = loc
cfa80 6b 4c 69 73 74 3b 0a 20 20 20 20 77 68 69 6c 65  kList;.    while
cfa90 28 20 70 4c 6f 63 6b 20 26 26 20 6d 65 6d 63 6d  ( pLock && memcm
cfaa0 70 28 26 6c 6f 63 6b 4b 65 79 2c 20 26 70 4c 6f  p(&lockKey, &pLo
cfab0 63 6b 2d 3e 6c 6f 63 6b 4b 65 79 2c 20 73 69 7a  ck->lockKey, siz
cfac0 65 6f 66 28 6c 6f 63 6b 4b 65 79 29 29 20 29 7b  eof(lockKey)) ){
cfad0 0a 20 20 20 20 20 20 70 4c 6f 63 6b 20 3d 20 70  .      pLock = p
cfae0 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  Lock->pNext;.   
cfaf0 20 7d 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 6b   }.    if( pLock
cfb00 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 4c 6f  ==0 ){.      pLo
cfb10 63 6b 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  ck = sqlite3_mal
cfb20 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 4c 6f  loc( sizeof(*pLo
cfb30 63 6b 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28  ck) );.      if(
cfb40 20 70 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20   pLock==0 ){.   
cfb50 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
cfb60 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20  _NOMEM;.        
cfb70 67 6f 74 6f 20 65 78 69 74 5f 66 69 6e 64 6c 6f  goto exit_findlo
cfb80 63 6b 69 6e 66 6f 3b 0a 20 20 20 20 20 20 7d 0a  ckinfo;.      }.
cfb90 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63        pLock->loc
cfba0 6b 4b 65 79 20 3d 20 6c 6f 63 6b 4b 65 79 3b 0a  kKey = lockKey;.
cfbb0 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65        pLock->nRe
cfbc0 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 4c 6f  f = 1;.      pLo
cfbd0 63 6b 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20 20 20  ck->cnt = 0;.   
cfbe0 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79     pLock->lockty
cfbf0 70 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4c  pe = 0;.      pL
cfc00 6f 63 6b 2d 3e 70 4e 65 78 74 20 3d 20 6c 6f 63  ock->pNext = loc
cfc10 6b 4c 69 73 74 3b 0a 20 20 20 20 20 20 70 4c 6f  kList;.      pLo
cfc20 63 6b 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20  ck->pPrev = 0;. 
cfc30 20 20 20 20 20 69 66 28 20 6c 6f 63 6b 4c 69 73       if( lockLis
cfc40 74 20 29 20 6c 6f 63 6b 4c 69 73 74 2d 3e 70 50  t ) lockList->pP
cfc50 72 65 76 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 20  rev = pLock;.   
cfc60 20 20 20 6c 6f 63 6b 4c 69 73 74 20 3d 20 70 4c     lockList = pL
cfc70 6f 63 6b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ock;.    }else{.
cfc80 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65        pLock->nRe
cfc90 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a  f++;.    }.    *
cfca0 70 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a  ppLock = pLock;.
cfcb0 20 20 7d 0a 20 20 69 66 28 20 70 70 4f 70 65 6e    }.  if( ppOpen
cfcc0 21 3d 30 20 29 7b 0a 20 20 20 20 70 4f 70 65 6e  !=0 ){.    pOpen
cfcd0 20 3d 20 6f 70 65 6e 4c 69 73 74 3b 0a 20 20 20   = openList;.   
cfce0 20 77 68 69 6c 65 28 20 70 4f 70 65 6e 20 26 26   while( pOpen &&
cfcf0 20 6d 65 6d 63 6d 70 28 26 66 69 6c 65 49 64 2c   memcmp(&fileId,
cfd00 20 26 70 4f 70 65 6e 2d 3e 66 69 6c 65 49 64 2c   &pOpen->fileId,
cfd10 20 73 69 7a 65 6f 66 28 66 69 6c 65 49 64 29 29   sizeof(fileId))
cfd20 20 29 7b 0a 20 20 20 20 20 20 70 4f 70 65 6e 20   ){.      pOpen 
cfd30 3d 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 3b 0a  = pOpen->pNext;.
cfd40 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4f      }.    if( pO
cfd50 70 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  pen==0 ){.      
cfd60 70 4f 70 65 6e 20 3d 20 73 71 6c 69 74 65 33 5f  pOpen = sqlite3_
cfd70 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a  malloc( sizeof(*
cfd80 70 4f 70 65 6e 29 20 29 3b 0a 20 20 20 20 20 20  pOpen) );.      
cfd90 69 66 28 20 70 4f 70 65 6e 3d 3d 30 20 29 7b 0a  if( pOpen==0 ){.
cfda0 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 4c          releaseL
cfdb0 6f 63 6b 49 6e 66 6f 28 70 4c 6f 63 6b 29 3b 0a  ockInfo(pLock);.
cfdc0 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
cfdd0 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
cfde0 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 66 69 6e     goto exit_fin
cfdf0 64 6c 6f 63 6b 69 6e 66 6f 3b 0a 20 20 20 20 20  dlockinfo;.     
cfe00 20 7d 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28   }.      memset(
cfe10 70 4f 70 65 6e 2c 20 30 2c 20 73 69 7a 65 6f 66  pOpen, 0, sizeof
cfe20 28 2a 70 4f 70 65 6e 29 29 3b 0a 20 20 20 20 20  (*pOpen));.     
cfe30 20 70 4f 70 65 6e 2d 3e 66 69 6c 65 49 64 20 3d   pOpen->fileId =
cfe40 20 66 69 6c 65 49 64 3b 0a 20 20 20 20 20 20 70   fileId;.      p
cfe50 4f 70 65 6e 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a  Open->nRef = 1;.
cfe60 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 4e 65        pOpen->pNe
cfe70 78 74 20 3d 20 6f 70 65 6e 4c 69 73 74 3b 0a 20  xt = openList;. 
cfe80 20 20 20 20 20 69 66 28 20 6f 70 65 6e 4c 69 73       if( openLis
cfe90 74 20 29 20 6f 70 65 6e 4c 69 73 74 2d 3e 70 50  t ) openList->pP
cfea0 72 65 76 20 3d 20 70 4f 70 65 6e 3b 0a 20 20 20  rev = pOpen;.   
cfeb0 20 20 20 6f 70 65 6e 4c 69 73 74 20 3d 20 70 4f     openList = pO
cfec0 70 65 6e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  pen;.    }else{.
cfed0 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65        pOpen->nRe
cfee0 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a  f++;.    }.    *
cfef0 70 70 4f 70 65 6e 20 3d 20 70 4f 70 65 6e 3b 0a  ppOpen = pOpen;.
cff00 20 20 7d 0a 0a 65 78 69 74 5f 66 69 6e 64 6c 6f    }..exit_findlo
cff10 63 6b 69 6e 66 6f 3a 0a 20 20 72 65 74 75 72 6e  ckinfo:.  return
cff20 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66   rc;.}../*.** If
cff30 20 77 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c   we are currentl
cff40 79 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74  y in a different
cff50 20 74 68 72 65 61 64 20 74 68 61 6e 20 74 68 65   thread than the
cff60 20 74 68 72 65 61 64 20 74 68 61 74 20 74 68 65   thread that the
cff70 0a 2a 2a 20 75 6e 69 78 46 69 6c 65 20 61 72 67  .** unixFile arg
cff80 75 6d 65 6e 74 20 62 65 6c 6f 6e 67 73 20 74 6f  ument belongs to
cff90 2c 20 74 68 65 6e 20 74 72 61 6e 73 66 65 72 20  , then transfer 
cffa0 6f 77 6e 65 72 73 68 69 70 20 6f 66 20 74 68 65  ownership of the
cffb0 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 6f 76 65   unixFile.** ove
cffc0 72 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74  r to the current
cffd0 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41   thread..**.** A
cffe0 20 75 6e 69 78 46 69 6c 65 20 69 73 20 6f 6e 6c   unixFile is onl
cfff0 79 20 6f 77 6e 65 64 20 62 79 20 61 20 74 68 72  y owned by a thr
d0000 65 61 64 20 6f 6e 20 73 79 73 74 65 6d 73 20 74  ead on systems t
d0010 68 61 74 20 75 73 65 20 4c 69 6e 75 78 54 68 72  hat use LinuxThr
d0020 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 4f 77 6e 65  eads..**.** Owne
d0030 72 73 68 69 70 20 74 72 61 6e 73 66 65 72 20 69  rship transfer i
d0040 73 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 69  s only allowed i
d0050 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65 20 69  f the unixFile i
d0060 73 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 6c 6f  s currently unlo
d0070 63 6b 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20  cked..** If the 
d0080 75 6e 69 78 46 69 6c 65 20 69 73 20 6c 6f 63 6b  unixFile is lock
d0090 65 64 20 61 6e 64 20 61 6e 20 6f 77 6e 65 72 73  ed and an owners
d00a0 68 69 70 20 69 73 20 77 72 6f 6e 67 2c 20 74 68  hip is wrong, th
d00b0 65 6e 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c  en return.** SQL
d00c0 49 54 45 5f 4d 49 53 55 53 45 2e 20 20 53 51 4c  ITE_MISUSE.  SQL
d00d0 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
d00e0 65 64 20 69 66 20 65 76 65 72 79 74 68 69 6e 67  ed if everything
d00f0 20 77 6f 72 6b 73 2e 0a 2a 2f 0a 23 69 66 20 53   works..*/.#if S
d0100 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
d0110 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69   && defined(__li
d0120 6e 75 78 5f 5f 29 0a 73 74 61 74 69 63 20 69 6e  nux__).static in
d0130 74 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73  t transferOwners
d0140 68 69 70 28 75 6e 69 78 46 69 6c 65 20 2a 70 46  hip(unixFile *pF
d0150 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  ile){.  int rc;.
d0160 20 20 70 74 68 72 65 61 64 5f 74 20 68 53 65 6c    pthread_t hSel
d0170 66 3b 0a 20 20 69 66 28 20 74 68 72 65 61 64 73  f;.  if( threads
d0180 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65  OverrideEachOthe
d0190 72 73 4c 6f 63 6b 73 20 29 7b 0a 20 20 20 20 2f  rsLocks ){.    /
d01a0 2a 20 4f 77 6e 65 72 73 68 69 70 20 74 72 61 6e  * Ownership tran
d01b0 73 66 65 72 73 20 6e 6f 74 20 6e 65 65 64 65 64  sfers not needed
d01c0 20 6f 6e 20 74 68 69 73 20 73 79 73 74 65 6d 20   on this system 
d01d0 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  */.    return SQ
d01e0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 68  LITE_OK;.  }.  h
d01f0 53 65 6c 66 20 3d 20 70 74 68 72 65 61 64 5f 73  Self = pthread_s
d0200 65 6c 66 28 29 3b 0a 20 20 69 66 28 20 70 74 68  elf();.  if( pth
d0210 72 65 61 64 5f 65 71 75 61 6c 28 70 46 69 6c 65  read_equal(pFile
d0220 2d 3e 74 69 64 2c 20 68 53 65 6c 66 29 20 29 7b  ->tid, hSelf) ){
d0230 0a 20 20 20 20 2f 2a 20 57 65 20 61 72 65 20 73  .    /* We are s
d0240 74 69 6c 6c 20 69 6e 20 74 68 65 20 73 61 6d 65  till in the same
d0250 20 74 68 72 65 61 64 20 2a 2f 0a 20 20 20 20 4f   thread */.    O
d0260 53 54 52 41 43 45 31 28 22 4e 6f 2d 74 72 61 6e  STRACE1("No-tran
d0270 73 66 65 72 2c 20 73 61 6d 65 20 74 68 72 65 61  sfer, same threa
d0280 64 5c 6e 22 29 3b 0a 20 20 20 20 72 65 74 75 72  d\n");.    retur
d0290 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
d02a0 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f  .  if( pFile->lo
d02b0 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20  cktype!=NO_LOCK 
d02c0 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 63 61 6e  ){.    /* We can
d02d0 6e 6f 74 20 63 68 61 6e 67 65 20 6f 77 6e 65 72  not change owner
d02e0 73 68 69 70 20 77 68 69 6c 65 20 77 65 20 61 72  ship while we ar
d02f0 65 20 68 6f 6c 64 69 6e 67 20 61 20 6c 6f 63 6b  e holding a lock
d0300 21 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20  ! */.    return 
d0310 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
d0320 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54   }.  OSTRACE4("T
d0330 72 61 6e 73 66 65 72 20 6f 77 6e 65 72 73 68 69  ransfer ownershi
d0340 70 20 6f 66 20 25 64 20 66 72 6f 6d 20 25 64 20  p of %d from %d 
d0350 74 6f 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20  to %d\n",.      
d0360 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 2c 20        pFile->h, 
d0370 70 46 69 6c 65 2d 3e 74 69 64 2c 20 68 53 65 6c  pFile->tid, hSel
d0380 66 29 3b 0a 20 20 70 46 69 6c 65 2d 3e 74 69 64  f);.  pFile->tid
d0390 20 3d 20 68 53 65 6c 66 3b 0a 20 20 69 66 20 28   = hSelf;.  if (
d03a0 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 21 3d 20  pFile->pLock != 
d03b0 4e 55 4c 4c 29 20 7b 0a 20 20 20 20 72 65 6c 65  NULL) {.    rele
d03c0 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 6c  aseLockInfo(pFil
d03d0 65 2d 3e 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 72  e->pLock);.    r
d03e0 63 20 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f  c = findLockInfo
d03f0 28 70 46 69 6c 65 2c 20 26 70 46 69 6c 65 2d 3e  (pFile, &pFile->
d0400 70 4c 6f 63 6b 2c 20 30 29 3b 0a 20 20 20 20 4f  pLock, 0);.    O
d0410 53 54 52 41 43 45 35 28 22 4c 4f 43 4b 20 20 20  STRACE5("LOCK   
d0420 20 25 64 20 69 73 20 6e 6f 77 20 25 73 28 25 73   %d is now %s(%s
d0430 2c 25 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  ,%d)\n", pFile->
d0440 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 6c 6f  h,.           lo
d0450 63 6b 74 79 70 65 4e 61 6d 65 28 70 46 69 6c 65  cktypeName(pFile
d0460 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 0a 20 20 20  ->locktype),.   
d0470 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65          locktype
d0480 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 70 4c 6f 63  Name(pFile->pLoc
d0490 6b 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 20 70 46  k->locktype), pF
d04a0 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 63 6e 74 29  ile->pLock->cnt)
d04b0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  ;.    return rc;
d04c0 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20  .  } else {.    
d04d0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
d04e0 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 20 20 2f  ;.  }.}.#else  /
d04f0 2a 20 69 66 20 6e 6f 74 20 53 51 4c 49 54 45 5f  * if not SQLITE_
d0500 54 48 52 45 41 44 53 41 46 45 20 2a 2f 0a 20 20  THREADSAFE */.  
d0510 2f 2a 20 4f 6e 20 73 69 6e 67 6c 65 2d 74 68 72  /* On single-thr
d0520 65 61 64 65 64 20 62 75 69 6c 64 73 2c 20 6f 77  eaded builds, ow
d0530 6e 65 72 73 68 69 70 20 74 72 61 6e 73 66 65 72  nership transfer
d0540 20 69 73 20 61 20 6e 6f 2d 6f 70 20 2a 2f 0a 23   is a no-op */.#
d0550 20 64 65 66 69 6e 65 20 74 72 61 6e 73 66 65 72   define transfer
d0560 4f 77 6e 65 72 73 68 69 70 28 58 29 20 53 51 4c  Ownership(X) SQL
d0570 49 54 45 5f 4f 4b 0a 23 65 6e 64 69 66 20 2f 2a  ITE_OK.#endif /*
d0580 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
d0590 46 45 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68  FE */.../*.** Th
d05a0 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b  is routine check
d05b0 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20  s if there is a 
d05c0 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65  RESERVED lock he
d05d0 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66  ld on the specif
d05e0 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74  ied.** file by t
d05f0 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72  his or any other
d0600 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63   process. If suc
d0610 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64  h a lock is held
d0620 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a  , set *pResOut.*
d0630 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20  * to a non-zero 
d0640 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73 65 20  value otherwise 
d0650 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65 74 20  *pResOut is set 
d0660 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 65  to zero.  The re
d0670 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73  turn value.** is
d0680 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f   set to SQLITE_O
d0690 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20  K unless an I/O 
d06a0 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72  error occurs dur
d06b0 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e  ing lock checkin
d06c0 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  g..*/.static int
d06d0 20 75 6e 69 78 43 68 65 63 6b 52 65 73 65 72 76   unixCheckReserv
d06e0 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  edLock(sqlite3_f
d06f0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52  ile *id, int *pR
d0700 65 73 4f 75 74 29 7b 0a 20 20 69 6e 74 20 72 63  esOut){.  int rc
d0710 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
d0720 69 6e 74 20 72 65 73 65 72 76 65 64 20 3d 20 30  int reserved = 0
d0730 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46  ;.  unixFile *pF
d0740 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a  ile = (unixFile*
d0750 29 69 64 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65  )id;..  Simulate
d0760 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20  IOError( return 
d0770 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45  SQLITE_IOERR_CHE
d0780 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20  CKRESERVEDLOCK; 
d0790 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46  );..  assert( pF
d07a0 69 6c 65 20 29 3b 0a 20 20 75 6e 69 78 45 6e 74  ile );.  unixEnt
d07b0 65 72 4d 75 74 65 78 28 29 3b 20 2f 2a 20 42 65  erMutex(); /* Be
d07c0 63 61 75 73 65 20 70 46 69 6c 65 2d 3e 70 4c 6f  cause pFile->pLo
d07d0 63 6b 20 69 73 20 73 68 61 72 65 64 20 61 63 72  ck is shared acr
d07e0 6f 73 73 20 74 68 72 65 61 64 73 20 2a 2f 0a 0a  oss threads */..
d07f0 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 61 20    /* Check if a 
d0800 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 20 70  thread in this p
d0810 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 75 63  rocess holds suc
d0820 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66  h a lock */.  if
d0830 28 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e  ( pFile->pLock->
d0840 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f  locktype>SHARED_
d0850 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65  LOCK ){.    rese
d0860 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20  rved = 1;.  }.. 
d0870 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 73 65   /* Otherwise se
d0880 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20  e if some other 
d0890 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 69 74  process holds it
d08a0 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 5f  ..  */.#ifndef _
d08b0 5f 44 4a 47 50 50 5f 5f 0a 20 20 69 66 28 20 21  _DJGPP__.  if( !
d08c0 72 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20 20  reserved ){.    
d08d0 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63  struct flock loc
d08e0 6b 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68  k;.    lock.l_wh
d08f0 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b  ence = SEEK_SET;
d0900 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72  .    lock.l_star
d0910 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54  t = RESERVED_BYT
d0920 45 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65  E;.    lock.l_le
d0930 6e 20 3d 20 31 3b 0a 20 20 20 20 6c 6f 63 6b 2e  n = 1;.    lock.
d0940 6c 5f 74 79 70 65 20 3d 20 46 5f 57 52 4c 43 4b  l_type = F_WRLCK
d0950 3b 0a 20 20 20 20 69 66 20 28 2d 31 20 3d 3d 20  ;.    if (-1 == 
d0960 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20  fcntl(pFile->h, 
d0970 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 29  F_GETLK, &lock))
d0980 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 45 72   {.      int tEr
d0990 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20  rno = errno;.   
d09a0 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72     rc = sqliteEr
d09b0 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f  rorFromPosixErro
d09c0 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45  r(tErrno, SQLITE
d09d0 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45  _IOERR_CHECKRESE
d09e0 52 56 45 44 4c 4f 43 4b 29 3b 0a 20 20 20 20 20  RVEDLOCK);.     
d09f0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
d0a00 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20  o = tErrno;.    
d0a10 7d 20 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 2e  } else if( lock.
d0a20 6c 5f 74 79 70 65 21 3d 46 5f 55 4e 4c 43 4b 20  l_type!=F_UNLCK 
d0a30 29 7b 0a 20 20 20 20 20 20 72 65 73 65 72 76 65  ){.      reserve
d0a40 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  d = 1;.    }.  }
d0a50 0a 23 65 6e 64 69 66 0a 20 20 0a 20 20 75 6e 69  .#endif.  .  uni
d0a60 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20  xLeaveMutex();. 
d0a70 20 4f 53 54 52 41 43 45 34 28 22 54 45 53 54 20   OSTRACE4("TEST 
d0a80 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25 64  WR-LOCK %d %d %d
d0a90 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72  \n", pFile->h, r
d0aa0 63 2c 20 72 65 73 65 72 76 65 64 29 3b 0a 0a 20  c, reserved);.. 
d0ab0 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65 73 65   *pResOut = rese
d0ac0 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 72  rved;.  return r
d0ad0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66  c;.}../*.** Perf
d0ae0 6f 72 6d 20 61 20 66 69 6c 65 20 6c 6f 63 6b 69  orm a file locki
d0af0 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 6f 6e 20  ng operation on 
d0b00 61 20 72 61 6e 67 65 20 6f 66 20 62 79 74 65 73  a range of bytes
d0b10 20 69 6e 20 61 20 66 69 6c 65 2e 0a 2a 2a 20 54   in a file..** T
d0b20 68 65 20 22 6f 70 22 20 70 61 72 61 6d 65 74 65  he "op" paramete
d0b30 72 20 73 68 6f 75 6c 64 20 62 65 20 6f 6e 65 20  r should be one 
d0b40 6f 66 20 46 5f 52 44 4c 43 4b 2c 20 46 5f 57 52  of F_RDLCK, F_WR
d0b50 4c 43 4b 2c 20 6f 72 20 46 5f 55 4e 4c 43 4b 2e  LCK, or F_UNLCK.
d0b60 0a 2a 2a 20 52 65 74 75 72 6e 20 30 20 6f 6e 20  .** Return 0 on 
d0b70 73 75 63 63 65 73 73 20 6f 72 20 2d 31 20 66 6f  success or -1 fo
d0b80 72 20 66 61 69 6c 75 72 65 2e 20 20 4f 6e 20 66  r failure.  On f
d0b90 61 69 6c 75 72 65 2c 20 77 72 69 74 65 20 74 68  ailure, write th
d0ba0 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20  e error.** code 
d0bb0 69 6e 74 6f 20 2a 70 45 72 72 63 6f 64 65 2e 0a  into *pErrcode..
d0bc0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c  **.** If the SQL
d0bd0 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c  ITE_WHOLE_FILE_L
d0be0 4f 43 4b 49 4e 47 20 62 69 74 20 69 73 20 63 6c  OCKING bit is cl
d0bf0 65 61 72 2c 20 74 68 65 6e 20 6f 6e 6c 79 20 6c  ear, then only l
d0c00 6f 63 6b 0a 2a 2a 20 74 68 65 20 72 61 6e 67 65  ock.** the range
d0c10 20 6f 66 20 62 79 74 65 73 20 6f 6e 20 74 68 65   of bytes on the
d0c20 20 6c 6f 63 6b 69 6e 67 20 70 61 67 65 20 62 65   locking page be
d0c30 74 77 65 65 6e 20 53 48 41 52 45 44 5f 46 49 52  tween SHARED_FIR
d0c40 53 54 20 61 6e 64 0a 2a 2a 20 53 48 41 52 45 44  ST and.** SHARED
d0c50 5f 53 49 5a 45 2e 20 20 49 66 20 53 51 4c 49 54  _SIZE.  If SQLIT
d0c60 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43  E_WHOLE_FILE_LOC
d0c70 4b 49 4e 47 20 69 73 20 73 65 74 2c 20 74 68 65  KING is set, the
d0c80 6e 20 6c 6f 63 6b 20 61 6c 6c 0a 2a 2a 20 62 79  n lock all.** by
d0c90 74 65 73 20 66 72 6f 6d 20 30 20 75 70 20 74 6f  tes from 0 up to
d0ca0 20 62 75 74 20 6e 6f 74 20 69 6e 63 6c 75 64 69   but not includi
d0cb0 6e 67 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c  ng PENDING_BYTE,
d0cc0 20 61 6e 64 20 61 6c 6c 20 62 79 74 65 73 0a 2a   and all bytes.*
d0cd0 2a 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 53 48  * that follow SH
d0ce0 41 52 45 44 5f 46 49 52 53 54 2e 0a 2a 2a 0a 2a  ARED_FIRST..**.*
d0cf0 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73  * In other words
d0d00 2c 20 6f 66 20 53 51 4c 49 54 45 5f 57 48 4f 4c  , of SQLITE_WHOL
d0d10 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 20 69  E_FILE_LOCKING i
d0d20 66 20 66 61 6c 73 65 20 28 74 68 65 20 68 69 73  f false (the his
d0d30 74 6f 72 69 63 61 6c 0a 2a 2a 20 64 65 66 61 75  torical.** defau
d0d40 6c 74 20 63 61 73 65 29 20 74 68 65 6e 20 6f 6e  lt case) then on
d0d50 6c 79 20 6c 6f 63 6b 20 61 20 73 6d 61 6c 6c 20  ly lock a small 
d0d60 72 61 6e 67 65 20 6f 66 20 62 79 74 65 73 20 66  range of bytes f
d0d70 72 6f 6d 20 53 48 41 52 45 44 5f 46 49 52 53 54  rom SHARED_FIRST
d0d80 0a 2a 2a 20 74 68 72 6f 75 67 68 20 53 48 41 52  .** through SHAR
d0d90 45 44 5f 46 49 52 53 54 2b 53 48 41 52 45 44 5f  ED_FIRST+SHARED_
d0da0 53 49 5a 45 2d 31 2e 20 20 42 75 74 20 69 66 20  SIZE-1.  But if 
d0db0 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c  SQLITE_WHOLE_FIL
d0dc0 45 5f 4c 4f 43 4b 49 4e 47 20 69 73 0a 2a 2a 20  E_LOCKING is.** 
d0dd0 74 72 75 65 20 74 68 65 6e 20 6c 6f 63 6b 20 65  true then lock e
d0de0 76 65 72 79 20 62 79 74 65 20 69 6e 20 74 68 65  very byte in the
d0df0 20 66 69 6c 65 20 65 78 63 65 70 74 20 66 6f 72   file except for
d0e00 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 61 6e   PENDING_BYTE an
d0e10 64 0a 2a 2a 20 52 45 53 45 52 56 45 44 5f 42 59  d.** RESERVED_BY
d0e20 54 45 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45  TE..**.** SQLITE
d0e30 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b  _WHOLE_FILE_LOCK
d0e40 49 4e 47 3d 74 72 75 65 20 6f 76 65 72 6c 61 70  ING=true overlap
d0e50 73 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46  s SQLITE_WHOLE_F
d0e60 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3d 66 61 6c 73  ILE_LOCKING=fals
d0e70 65 0a 2a 2a 20 61 6e 64 20 73 6f 20 74 68 65 20  e.** and so the 
d0e80 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d 65 73 20  locking schemes 
d0e90 61 72 65 20 63 6f 6d 70 61 74 69 62 6c 65 2e 20  are compatible. 
d0ea0 20 4f 6e 65 20 74 79 70 65 20 6f 66 20 6c 6f 63   One type of loc
d0eb0 6b 20 77 69 6c 6c 0a 2a 2a 20 65 66 66 65 63 74  k will.** effect
d0ec0 69 76 65 6c 79 20 65 78 63 6c 75 64 65 20 74 68  ively exclude th
d0ed0 65 20 6f 74 68 65 72 20 74 79 70 65 2e 20 20 54  e other type.  T
d0ee0 68 65 20 72 65 61 73 6f 6e 20 66 6f 72 20 75 73  he reason for us
d0ef0 69 6e 67 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54  ing the.** SQLIT
d0f00 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43  E_WHOLE_FILE_LOC
d0f10 4b 49 4e 47 3d 74 72 75 65 20 69 73 20 74 68 61  KING=true is tha
d0f20 74 20 62 79 20 69 6e 64 69 63 61 74 69 6e 67 20  t by indicating 
d0f30 74 68 65 20 66 75 6c 6c 20 72 61 6e 67 65 0a 2a  the full range.*
d0f40 2a 20 6f 66 20 62 79 74 65 73 20 74 6f 20 62 65  * of bytes to be
d0f50 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e   read or written
d0f60 2c 20 77 65 20 67 69 76 65 20 68 69 6e 74 73 20  , we give hints 
d0f70 74 6f 20 4e 46 53 20 74 6f 20 68 65 6c 70 20 69  to NFS to help i
d0f80 74 0a 2a 2a 20 6d 61 69 6e 74 61 69 6e 20 63 61  t.** maintain ca
d0f90 63 68 65 20 63 6f 68 65 72 65 6e 63 79 2e 20 20  che coherency.  
d0fa0 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e  On the other han
d0fb0 64 2c 20 77 68 6f 6c 65 20 66 69 6c 65 20 6c 6f  d, whole file lo
d0fc0 63 6b 69 6e 67 0a 2a 2a 20 69 73 20 73 6c 6f 77  cking.** is slow
d0fd0 65 72 2c 20 73 6f 20 77 65 20 64 6f 6e 27 74 20  er, so we don't 
d0fe0 77 61 6e 74 20 74 6f 20 75 73 65 20 69 74 20 65  want to use it e
d0ff0 78 63 65 70 74 20 66 6f 72 20 4e 46 53 2e 0a 2a  xcept for NFS..*
d1000 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 61 6e  /.static int ran
d1010 67 65 4c 6f 63 6b 28 75 6e 69 78 46 69 6c 65 20  geLock(unixFile 
d1020 2a 70 46 69 6c 65 2c 20 69 6e 74 20 6f 70 2c 20  *pFile, int op, 
d1030 69 6e 74 20 2a 70 45 72 72 63 6f 64 65 29 7b 0a  int *pErrcode){.
d1040 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c    struct flock l
d1050 6f 63 6b 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  ock;.  int rc;. 
d1060 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 6f   lock.l_type = o
d1070 70 3b 0a 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72  p;.  lock.l_star
d1080 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54  t = SHARED_FIRST
d1090 3b 0a 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63  ;.  lock.l_whenc
d10a0 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20  e = SEEK_SET;.  
d10b0 69 66 28 20 28 70 46 69 6c 65 2d 3e 66 69 6c 65  if( (pFile->file
d10c0 46 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 57  Flags & SQLITE_W
d10d0 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e  HOLE_FILE_LOCKIN
d10e0 47 29 3d 3d 30 20 29 7b 0a 20 20 20 20 6c 6f 63  G)==0 ){.    loc
d10f0 6b 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 52 45 44  k.l_len = SHARED
d1100 5f 53 49 5a 45 3b 0a 20 20 20 20 72 63 20 3d 20  _SIZE;.    rc = 
d1110 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20  fcntl(pFile->h, 
d1120 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3b  F_SETLK, &lock);
d1130 0a 20 20 20 20 2a 70 45 72 72 63 6f 64 65 20 3d  .    *pErrcode =
d1140 20 65 72 72 6e 6f 3b 0a 20 20 7d 65 6c 73 65 7b   errno;.  }else{
d1150 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20  .    lock.l_len 
d1160 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 66 63  = 0;.    rc = fc
d1170 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f  ntl(pFile->h, F_
d1180 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 20  SETLK, &lock);. 
d1190 20 20 20 2a 70 45 72 72 63 6f 64 65 20 3d 20 65     *pErrcode = e
d11a0 72 72 6e 6f 3b 0a 20 20 20 20 69 66 28 20 4e 45  rrno;.    if( NE
d11b0 56 45 52 28 6f 70 3d 3d 46 5f 55 4e 4c 43 4b 29  VER(op==F_UNLCK)
d11c0 20 7c 7c 20 72 63 21 3d 28 2d 31 29 20 29 7b 0a   || rc!=(-1) ){.
d11d0 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61        lock.l_sta
d11e0 72 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 6c 6f  rt = 0;.      lo
d11f0 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 50 45 4e 44 49  ck.l_len = PENDI
d1200 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 20 20 72  NG_BYTE;.      r
d1210 63 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d  c = fcntl(pFile-
d1220 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f  >h, F_SETLK, &lo
d1230 63 6b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 41  ck);.      if( A
d1240 4c 57 41 59 53 28 6f 70 21 3d 46 5f 55 4e 4c 43  LWAYS(op!=F_UNLC
d1250 4b 29 20 26 26 20 72 63 3d 3d 28 2d 31 29 20 29  K) && rc==(-1) )
d1260 7b 0a 20 20 20 20 20 20 20 20 2a 70 45 72 72 63  {.        *pErrc
d1270 6f 64 65 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20  ode = errno;.   
d1280 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65       lock.l_type
d1290 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20   = F_UNLCK;.    
d12a0 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74      lock.l_start
d12b0 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b   = SHARED_FIRST;
d12c0 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f  .        lock.l_
d12d0 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  len = 0;.       
d12e0 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c   fcntl(pFile->h,
d12f0 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29   F_SETLK, &lock)
d1300 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
d1310 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
d1320 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74  .}../*.** Lock t
d1330 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65  he file with the
d1340 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20   lock specified 
d1350 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63  by parameter loc
d1360 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f  ktype - one.** o
d1370 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  f the following:
d1380 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53  .**.**     (1) S
d1390 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  HARED_LOCK.**   
d13a0 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c    (2) RESERVED_L
d13b0 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50  OCK.**     (3) P
d13c0 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20  ENDING_LOCK.**  
d13d0 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45     (4) EXCLUSIVE
d13e0 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65  _LOCK.**.** Some
d13f0 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65  times when reque
d1400 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73  sting one lock s
d1410 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c  tate, additional
d1420 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20   lock states.** 
d1430 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20  are inserted in 
d1440 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f  between.  The lo
d1450 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c  cking might fail
d1460 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c   on one of the l
d1470 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69  ater.** transiti
d1480 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20  ons leaving the 
d1490 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65  lock state diffe
d14a0 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69  rent from what i
d14b0 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a  t started but.**
d14c0 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20   still short of 
d14d0 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66  its goal.  The f
d14e0 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73  ollowing chart s
d14f0 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64  hows the allowed
d1500 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20  .** transitions 
d1510 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64  and the inserted
d1520 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74   intermediate st
d1530 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55  ates:.**.**    U
d1540 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45  NLOCKED -> SHARE
d1550 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d  D.**    SHARED -
d1560 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20  > RESERVED.**   
d1570 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44   SHARED -> (PEND
d1580 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56  ING) -> EXCLUSIV
d1590 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44  E.**    RESERVED
d15a0 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e   -> (PENDING) ->
d15b0 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20   EXCLUSIVE.**   
d15c0 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c   PENDING -> EXCL
d15d0 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73  USIVE.**.** This
d15e0 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e   routine will on
d15f0 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f  ly increase a lo
d1600 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c  ck.  Use the sql
d1610 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a  ite3OsUnlock().*
d1620 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77  * routine to low
d1630 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76  er a locking lev
d1640 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  el..*/.static in
d1650 74 20 75 6e 69 78 4c 6f 63 6b 28 73 71 6c 69 74  t unixLock(sqlit
d1660 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
d1670 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 2f 2a   locktype){.  /*
d1680 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 64   The following d
d1690 65 73 63 72 69 62 65 73 20 74 68 65 20 69 6d 70  escribes the imp
d16a0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
d16b0 68 65 20 76 61 72 69 6f 75 73 20 6c 6f 63 6b 73  he various locks
d16c0 20 61 6e 64 0a 20 20 2a 2a 20 6c 6f 63 6b 20 74   and.  ** lock t
d16d0 72 61 6e 73 69 74 69 6f 6e 73 20 69 6e 20 74 65  ransitions in te
d16e0 72 6d 73 20 6f 66 20 74 68 65 20 50 4f 53 49 58  rms of the POSIX
d16f0 20 61 64 76 69 73 6f 72 79 20 73 68 61 72 65 64   advisory shared
d1700 20 61 6e 64 20 65 78 63 6c 75 73 69 76 65 0a 20   and exclusive. 
d1710 20 2a 2a 20 6c 6f 63 6b 20 70 72 69 6d 69 74 69   ** lock primiti
d1720 76 65 73 20 28 63 61 6c 6c 65 64 20 72 65 61 64  ves (called read
d1730 2d 6c 6f 63 6b 73 20 61 6e 64 20 77 72 69 74 65  -locks and write
d1740 2d 6c 6f 63 6b 73 20 62 65 6c 6f 77 2c 20 74 6f  -locks below, to
d1750 20 61 76 6f 69 64 0a 20 20 2a 2a 20 63 6f 6e 66   avoid.  ** conf
d1760 75 73 69 6f 6e 20 77 69 74 68 20 53 51 4c 69 74  usion with SQLit
d1770 65 20 6c 6f 63 6b 20 6e 61 6d 65 73 29 2e 20 54  e lock names). T
d1780 68 65 20 61 6c 67 6f 72 69 74 68 6d 73 20 61 72  he algorithms ar
d1790 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 0a 20 20  e complicated.  
d17a0 2a 2a 20 73 6c 69 67 68 74 6c 79 20 69 6e 20 6f  ** slightly in o
d17b0 72 64 65 72 20 74 6f 20 62 65 20 63 6f 6d 70 61  rder to be compa
d17c0 74 69 62 6c 65 20 77 69 74 68 20 77 69 6e 64 6f  tible with windo
d17d0 77 73 20 73 79 73 74 65 6d 73 20 73 69 6d 75 6c  ws systems simul
d17e0 74 61 6e 65 6f 75 73 6c 79 0a 20 20 2a 2a 20 61  taneously.  ** a
d17f0 63 63 65 73 73 69 6e 67 20 74 68 65 20 73 61 6d  ccessing the sam
d1800 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  e database file,
d1810 20 69 6e 20 63 61 73 65 20 74 68 61 74 20 69 73   in case that is
d1820 20 65 76 65 72 20 72 65 71 75 69 72 65 64 2e 0a   ever required..
d1830 20 20 2a 2a 0a 20 20 2a 2a 20 53 79 6d 62 6f 6c    **.  ** Symbol
d1840 73 20 64 65 66 69 6e 65 64 20 69 6e 20 6f 73 2e  s defined in os.
d1850 68 20 69 6e 64 65 6e 74 69 66 79 20 74 68 65 20  h indentify the 
d1860 27 70 65 6e 64 69 6e 67 20 62 79 74 65 27 20 61  'pending byte' a
d1870 6e 64 20 74 68 65 20 27 72 65 73 65 72 76 65 64  nd the 'reserved
d1880 0a 20 20 2a 2a 20 62 79 74 65 27 2c 20 65 61 63  .  ** byte', eac
d1890 68 20 73 69 6e 67 6c 65 20 62 79 74 65 73 20 61  h single bytes a
d18a0 74 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66  t well known off
d18b0 73 65 74 73 2c 20 61 6e 64 20 74 68 65 20 27 73  sets, and the 's
d18c0 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a 20  hared byte.  ** 
d18d0 72 61 6e 67 65 27 2c 20 61 20 72 61 6e 67 65 20  range', a range 
d18e0 6f 66 20 35 31 30 20 62 79 74 65 73 20 61 74 20  of 510 bytes at 
d18f0 61 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66  a well known off
d1900 73 65 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  set..  **.  ** T
d1910 6f 20 6f 62 74 61 69 6e 20 61 20 53 48 41 52 45  o obtain a SHARE
d1920 44 20 6c 6f 63 6b 2c 20 61 20 72 65 61 64 2d 6c  D lock, a read-l
d1930 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20  ock is obtained 
d1940 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67 0a  on the 'pending.
d1950 20 20 2a 2a 20 62 79 74 65 27 2e 20 20 49 66 20    ** byte'.  If 
d1960 74 68 69 73 20 69 73 20 73 75 63 63 65 73 73 66  this is successf
d1970 75 6c 2c 20 61 20 72 61 6e 64 6f 6d 20 62 79 74  ul, a random byt
d1980 65 20 66 72 6f 6d 20 74 68 65 20 27 73 68 61 72  e from the 'shar
d1990 65 64 20 62 79 74 65 0a 20 20 2a 2a 20 72 61 6e  ed byte.  ** ran
d19a0 67 65 27 20 69 73 20 72 65 61 64 2d 6c 6f 63 6b  ge' is read-lock
d19b0 65 64 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20  ed and the lock 
d19c0 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67 20  on the 'pending 
d19d0 62 79 74 65 27 20 72 65 6c 65 61 73 65 64 2e 0a  byte' released..
d19e0 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 70 72 6f 63    **.  ** A proc
d19f0 65 73 73 20 6d 61 79 20 6f 6e 6c 79 20 6f 62 74  ess may only obt
d1a00 61 69 6e 20 61 20 52 45 53 45 52 56 45 44 20 6c  ain a RESERVED l
d1a10 6f 63 6b 20 61 66 74 65 72 20 69 74 20 68 61 73  ock after it has
d1a20 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e 0a   a SHARED lock..
d1a30 20 20 2a 2a 20 41 20 52 45 53 45 52 56 45 44 20    ** A RESERVED 
d1a40 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65 6d 65 6e  lock is implemen
d1a50 74 65 64 20 62 79 20 67 72 61 62 62 69 6e 67 20  ted by grabbing 
d1a60 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20  a write-lock on 
d1a70 74 68 65 0a 20 20 2a 2a 20 27 72 65 73 65 72 76  the.  ** 'reserv
d1a80 65 64 20 62 79 74 65 27 2e 20 0a 20 20 2a 2a 0a  ed byte'. .  **.
d1a90 20 20 2a 2a 20 41 20 70 72 6f 63 65 73 73 20 6d    ** A process m
d1aa0 61 79 20 6f 6e 6c 79 20 6f 62 74 61 69 6e 20 61  ay only obtain a
d1ab0 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 61 66   PENDING lock af
d1ac0 74 65 72 20 69 74 20 68 61 73 20 6f 62 74 61 69  ter it has obtai
d1ad0 6e 65 64 20 61 0a 20 20 2a 2a 20 53 48 41 52 45  ned a.  ** SHARE
d1ae0 44 20 6c 6f 63 6b 2e 20 41 20 50 45 4e 44 49 4e  D lock. A PENDIN
d1af0 47 20 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65 6d  G lock is implem
d1b00 65 6e 74 65 64 20 62 79 20 6f 62 74 61 69 6e 69  ented by obtaini
d1b10 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 0a  ng a write-lock.
d1b20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 27 70 65 6e    ** on the 'pen
d1b30 64 69 6e 67 20 62 79 74 65 27 2e 20 54 68 69 73  ding byte'. This
d1b40 20 65 6e 73 75 72 65 73 20 74 68 61 74 20 6e 6f   ensures that no
d1b50 20 6e 65 77 20 53 48 41 52 45 44 20 6c 6f 63 6b   new SHARED lock
d1b60 73 20 63 61 6e 20 62 65 0a 20 20 2a 2a 20 6f 62  s can be.  ** ob
d1b70 74 61 69 6e 65 64 2c 20 62 75 74 20 65 78 69 73  tained, but exis
d1b80 74 69 6e 67 20 53 48 41 52 45 44 20 6c 6f 63 6b  ting SHARED lock
d1b90 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f  s are allowed to
d1ba0 20 70 65 72 73 69 73 74 2e 20 41 20 70 72 6f 63   persist. A proc
d1bb0 65 73 73 0a 20 20 2a 2a 20 64 6f 65 73 20 6e 6f  ess.  ** does no
d1bc0 74 20 68 61 76 65 20 74 6f 20 6f 62 74 61 69 6e  t have to obtain
d1bd0 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b   a RESERVED lock
d1be0 20 6f 6e 20 74 68 65 20 77 61 79 20 74 6f 20 61   on the way to a
d1bf0 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 0a 20   PENDING lock.. 
d1c00 20 2a 2a 20 54 68 69 73 20 70 72 6f 70 65 72 74   ** This propert
d1c10 79 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65  y is used by the
d1c20 20 61 6c 67 6f 72 69 74 68 6d 20 66 6f 72 20 72   algorithm for r
d1c30 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 6a 6f  olling back a jo
d1c40 75 72 6e 61 6c 20 66 69 6c 65 0a 20 20 2a 2a 20  urnal file.  ** 
d1c50 61 66 74 65 72 20 61 20 63 72 61 73 68 2e 0a 20  after a crash.. 
d1c60 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 45 58 43 4c   **.  ** An EXCL
d1c70 55 53 49 56 45 20 6c 6f 63 6b 2c 20 6f 62 74 61  USIVE lock, obta
d1c80 69 6e 65 64 20 61 66 74 65 72 20 61 20 50 45 4e  ined after a PEN
d1c90 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 68 65 6c  DING lock is hel
d1ca0 64 2c 20 69 73 0a 20 20 2a 2a 20 69 6d 70 6c 65  d, is.  ** imple
d1cb0 6d 65 6e 74 65 64 20 62 79 20 6f 62 74 61 69 6e  mented by obtain
d1cc0 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b  ing a write-lock
d1cd0 20 6f 6e 20 74 68 65 20 65 6e 74 69 72 65 20 27   on the entire '
d1ce0 73 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a  shared byte.  **
d1cf0 20 72 61 6e 67 65 27 2e 20 53 69 6e 63 65 20 61   range'. Since a
d1d00 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 6b 73 20 72  ll other locks r
d1d10 65 71 75 69 72 65 20 61 20 72 65 61 64 2d 6c 6f  equire a read-lo
d1d20 63 6b 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65  ck on one of the
d1d30 20 62 79 74 65 73 0a 20 20 2a 2a 20 77 69 74 68   bytes.  ** with
d1d40 69 6e 20 74 68 69 73 20 72 61 6e 67 65 2c 20 74  in this range, t
d1d50 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 61 74  his ensures that
d1d60 20 6e 6f 20 6f 74 68 65 72 20 6c 6f 63 6b 73 20   no other locks 
d1d70 61 72 65 20 68 65 6c 64 20 6f 6e 20 74 68 65 0a  are held on the.
d1d80 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 0a    ** database. .
d1d90 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 72 65    **.  ** The re
d1da0 61 73 6f 6e 20 61 20 73 69 6e 67 6c 65 20 62 79  ason a single by
d1db0 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 65  te cannot be use
d1dc0 64 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65  d instead of the
d1dd0 20 27 73 68 61 72 65 64 20 62 79 74 65 0a 20 20   'shared byte.  
d1de0 2a 2a 20 72 61 6e 67 65 27 20 69 73 20 74 68 61  ** range' is tha
d1df0 74 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20  t some versions 
d1e00 6f 66 20 77 69 6e 64 6f 77 73 20 64 6f 20 6e 6f  of windows do no
d1e10 74 20 73 75 70 70 6f 72 74 20 72 65 61 64 2d 6c  t support read-l
d1e20 6f 63 6b 73 2e 20 42 79 0a 20 20 2a 2a 20 6c 6f  ocks. By.  ** lo
d1e30 63 6b 69 6e 67 20 61 20 72 61 6e 64 6f 6d 20 62  cking a random b
d1e40 79 74 65 20 66 72 6f 6d 20 61 20 72 61 6e 67 65  yte from a range
d1e50 2c 20 63 6f 6e 63 75 72 72 65 6e 74 20 53 48 41  , concurrent SHA
d1e60 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79 20 65 78  RED locks may ex
d1e70 69 73 74 0a 20 20 2a 2a 20 65 76 65 6e 20 69 66  ist.  ** even if
d1e80 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 70 72 69   the locking pri
d1e90 6d 69 74 69 76 65 20 75 73 65 64 20 69 73 20 61  mitive used is a
d1ea0 6c 77 61 79 73 20 61 20 77 72 69 74 65 2d 6c 6f  lways a write-lo
d1eb0 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 72  ck..  */.  int r
d1ec0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
d1ed0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
d1ee0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
d1ef0 3b 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4c  ;.  struct unixL
d1f00 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 20 3d  ockInfo *pLock =
d1f10 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a 20   pFile->pLock;. 
d1f20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f   struct flock lo
d1f30 63 6b 3b 0a 20 20 69 6e 74 20 73 20 3d 20 30 3b  ck;.  int s = 0;
d1f40 0a 20 20 69 6e 74 20 74 45 72 72 6e 6f 3b 0a 0a  .  int tErrno;..
d1f50 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20    assert( pFile 
d1f60 29 3b 0a 20 20 4f 53 54 52 41 43 45 37 28 22 4c  );.  OSTRACE7("L
d1f70 4f 43 4b 20 20 20 20 25 64 20 25 73 20 77 61 73  OCK    %d %s was
d1f80 20 25 73 28 25 73 2c 25 64 29 20 70 69 64 3d 25   %s(%s,%d) pid=%
d1f90 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a  d\n", pFile->h,.
d1fa0 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61        locktypeNa
d1fb0 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 6c 6f  me(locktype), lo
d1fc0 63 6b 74 79 70 65 4e 61 6d 65 28 70 46 69 6c 65  cktypeName(pFile
d1fd0 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 0a 20 20 20  ->locktype),.   
d1fe0 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28     locktypeName(
d1ff0 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 29  pLock->locktype)
d2000 2c 20 70 4c 6f 63 6b 2d 3e 63 6e 74 20 2c 20 67  , pLock->cnt , g
d2010 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 2f 2a 20  etpid());..  /* 
d2020 49 66 20 74 68 65 72 65 20 69 73 20 61 6c 72 65  If there is alre
d2030 61 64 79 20 61 20 6c 6f 63 6b 20 6f 66 20 74 68  ady a lock of th
d2040 69 73 20 74 79 70 65 20 6f 72 20 6d 6f 72 65 20  is type or more 
d2050 72 65 73 74 72 69 63 74 69 76 65 20 6f 6e 20 74  restrictive on t
d2060 68 65 0a 20 20 2a 2a 20 75 6e 69 78 46 69 6c 65  he.  ** unixFile
d2070 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 6f  , do nothing. Do
d2080 6e 27 74 20 75 73 65 20 74 68 65 20 65 6e 64 5f  n't use the end_
d2090 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61 74 68 2c  lock: exit path,
d20a0 20 61 73 0a 20 20 2a 2a 20 75 6e 69 78 45 6e 74   as.  ** unixEnt
d20b0 65 72 4d 75 74 65 78 28 29 20 68 61 73 6e 27 74  erMutex() hasn't
d20c0 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74   been called yet
d20d0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69  ..  */.  if( pFi
d20e0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f  le->locktype>=lo
d20f0 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 4f 53  cktype ){.    OS
d2100 54 52 41 43 45 33 28 22 4c 4f 43 4b 20 20 20 20  TRACE3("LOCK    
d2110 25 64 20 25 73 20 6f 6b 20 28 61 6c 72 65 61 64  %d %s ok (alread
d2120 79 20 68 65 6c 64 29 5c 6e 22 2c 20 70 46 69 6c  y held)\n", pFil
d2130 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20  e->h,.          
d2140 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c    locktypeName(l
d2150 6f 63 6b 74 79 70 65 29 29 3b 0a 20 20 20 20 72  ocktype));.    r
d2160 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
d2170 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20  .  }..  /* Make 
d2180 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67  sure the locking
d2190 20 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f 72   sequence is cor
d21a0 72 65 63 74 2e 0a 20 20 2a 2a 20 20 28 31 29 20  rect..  **  (1) 
d21b0 57 65 20 6e 65 76 65 72 20 6d 6f 76 65 20 66 72  We never move fr
d21c0 6f 6d 20 75 6e 6c 6f 63 6b 65 64 20 74 6f 20 61  om unlocked to a
d21d0 6e 79 74 68 69 6e 67 20 68 69 67 68 65 72 20 74  nything higher t
d21e0 68 61 6e 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e  han shared lock.
d21f0 0a 20 20 2a 2a 20 20 28 32 29 20 53 51 4c 69 74  .  **  (2) SQLit
d2200 65 20 6e 65 76 65 72 20 65 78 70 6c 69 63 69 74  e never explicit
d2210 6c 79 20 72 65 71 75 65 73 74 73 20 61 20 70 65  ly requests a pe
d2220 6e 64 69 67 20 6c 6f 63 6b 2e 0a 20 20 2a 2a 20  ndig lock..  ** 
d2230 20 28 33 29 20 41 20 73 68 61 72 65 64 20 6c 6f   (3) A shared lo
d2240 63 6b 20 69 73 20 61 6c 77 61 79 73 20 68 65 6c  ck is always hel
d2250 64 20 77 68 65 6e 20 61 20 72 65 73 65 72 76 65  d when a reserve
d2260 20 6c 6f 63 6b 20 69 73 20 72 65 71 75 65 73 74   lock is request
d2270 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  ed..  */.  asser
d2280 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  t( pFile->lockty
d2290 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c  pe!=NO_LOCK || l
d22a0 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f  ocktype==SHARED_
d22b0 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74  LOCK );.  assert
d22c0 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44  ( locktype!=PEND
d22d0 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73  ING_LOCK );.  as
d22e0 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d  sert( locktype!=
d22f0 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c  RESERVED_LOCK ||
d2300 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
d2310 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b  ==SHARED_LOCK );
d2320 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6d 75 74 65  ..  /* This mute
d2330 78 20 69 73 20 6e 65 65 64 65 64 20 62 65 63 61  x is needed beca
d2340 75 73 65 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b  use pFile->pLock
d2350 20 69 73 20 73 68 61 72 65 64 20 61 63 72 6f 73   is shared acros
d2360 73 20 74 68 72 65 61 64 73 0a 20 20 2a 2f 0a 20  s threads.  */. 
d2370 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28   unixEnterMutex(
d2380 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75  );..  /* Make su
d2390 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  re the current t
d23a0 68 72 65 61 64 20 6f 77 6e 73 20 74 68 65 20 70  hread owns the p
d23b0 46 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20  File..  */.  rc 
d23c0 3d 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73  = transferOwners
d23d0 68 69 70 28 70 46 69 6c 65 29 3b 0a 20 20 69 66  hip(pFile);.  if
d23e0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
d23f0 29 7b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65  ){.    unixLeave
d2400 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72 65 74  Mutex();.    ret
d2410 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 4c  urn rc;.  }.  pL
d2420 6f 63 6b 20 3d 20 70 46 69 6c 65 2d 3e 70 4c 6f  ock = pFile->pLo
d2430 63 6b 3b 0a 0a 20 20 2f 2a 20 49 66 20 73 6f 6d  ck;..  /* If som
d2440 65 20 74 68 72 65 61 64 20 75 73 69 6e 67 20 74  e thread using t
d2450 68 69 73 20 50 49 44 20 68 61 73 20 61 20 6c 6f  his PID has a lo
d2460 63 6b 20 76 69 61 20 61 20 64 69 66 66 65 72 65  ck via a differe
d2470 6e 74 20 75 6e 69 78 46 69 6c 65 2a 0a 20 20 2a  nt unixFile*.  *
d2480 2a 20 68 61 6e 64 6c 65 20 74 68 61 74 20 70 72  * handle that pr
d2490 65 63 6c 75 64 65 73 20 74 68 65 20 72 65 71 75  ecludes the requ
d24a0 65 73 74 65 64 20 6c 6f 63 6b 2c 20 72 65 74 75  ested lock, retu
d24b0 72 6e 20 42 55 53 59 2e 0a 20 20 2a 2f 0a 20 20  rn BUSY..  */.  
d24c0 69 66 28 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b  if( (pFile->lock
d24d0 74 79 70 65 21 3d 70 4c 6f 63 6b 2d 3e 6c 6f 63  type!=pLock->loc
d24e0 6b 74 79 70 65 20 26 26 20 0a 20 20 20 20 20 20  ktype && .      
d24f0 20 20 20 20 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b      (pLock->lock
d2500 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f  type>=PENDING_LO
d2510 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3e 53  CK || locktype>S
d2520 48 41 52 45 44 5f 4c 4f 43 4b 29 29 0a 20 20 29  HARED_LOCK)).  )
d2530 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
d2540 45 5f 42 55 53 59 3b 0a 20 20 20 20 67 6f 74 6f  E_BUSY;.    goto
d2550 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a   end_lock;.  }..
d2560 20 20 2f 2a 20 49 66 20 61 20 53 48 41 52 45 44    /* If a SHARED
d2570 20 6c 6f 63 6b 20 69 73 20 72 65 71 75 65 73 74   lock is request
d2580 65 64 2c 20 61 6e 64 20 73 6f 6d 65 20 74 68 72  ed, and some thr
d2590 65 61 64 20 75 73 69 6e 67 20 74 68 69 73 20 50  ead using this P
d25a0 49 44 20 61 6c 72 65 61 64 79 0a 20 20 2a 2a 20  ID already.  ** 
d25b0 68 61 73 20 61 20 53 48 41 52 45 44 20 6f 72 20  has a SHARED or 
d25c0 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2c 20 74  RESERVED lock, t
d25d0 68 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 72 65  hen increment re
d25e0 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 73 20 61  ference counts a
d25f0 6e 64 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 53  nd.  ** return S
d2600 51 4c 49 54 45 5f 4f 4b 2e 0a 20 20 2a 2f 0a 20  QLITE_OK..  */. 
d2610 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53   if( locktype==S
d2620 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 0a 20  HARED_LOCK && . 
d2630 20 20 20 20 20 28 70 4c 6f 63 6b 2d 3e 6c 6f 63       (pLock->loc
d2640 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
d2650 43 4b 20 7c 7c 20 70 4c 6f 63 6b 2d 3e 6c 6f 63  CK || pLock->loc
d2660 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f  ktype==RESERVED_
d2670 4c 4f 43 4b 29 20 29 7b 0a 20 20 20 20 61 73 73  LOCK) ){.    ass
d2680 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53  ert( locktype==S
d2690 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20  HARED_LOCK );.  
d26a0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d    assert( pFile-
d26b0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 30 20 29 3b 0a  >locktype==0 );.
d26c0 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63      assert( pLoc
d26d0 6b 2d 3e 63 6e 74 3e 30 20 29 3b 0a 20 20 20 20  k->cnt>0 );.    
d26e0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
d26f0 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20  = SHARED_LOCK;. 
d2700 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 2b 2b 3b     pLock->cnt++;
d2710 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65  .    pFile->pOpe
d2720 6e 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20  n->nLock++;.    
d2730 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20  goto end_lock;. 
d2740 20 7d 0a 0a 0a 20 20 2f 2a 20 41 20 50 45 4e 44   }...  /* A PEND
d2750 49 4e 47 20 6c 6f 63 6b 20 69 73 20 6e 65 65 64  ING lock is need
d2760 65 64 20 62 65 66 6f 72 65 20 61 63 71 75 69 72  ed before acquir
d2770 69 6e 67 20 61 20 53 48 41 52 45 44 20 6c 6f 63  ing a SHARED loc
d2780 6b 20 61 6e 64 20 62 65 66 6f 72 65 0a 20 20 2a  k and before.  *
d2790 2a 20 61 63 71 75 69 72 69 6e 67 20 61 6e 20 45  * acquiring an E
d27a0 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20  XCLUSIVE lock.  
d27b0 46 6f 72 20 74 68 65 20 53 48 41 52 45 44 20 6c  For the SHARED l
d27c0 6f 63 6b 2c 20 74 68 65 20 50 45 4e 44 49 4e 47  ock, the PENDING
d27d0 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 65   will.  ** be re
d27e0 6c 65 61 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 6c  leased..  */.  l
d27f0 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 4c 3b 0a  ock.l_len = 1L;.
d2800 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20    lock.l_whence 
d2810 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 69 66  = SEEK_SET;.  if
d2820 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52  ( locktype==SHAR
d2830 45 44 5f 4c 4f 43 4b 20 0a 20 20 20 20 20 20 7c  ED_LOCK .      |
d2840 7c 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43  | (locktype==EXC
d2850 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70  LUSIVE_LOCK && p
d2860 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 50  File->locktype<P
d2870 45 4e 44 49 4e 47 5f 4c 4f 43 4b 29 0a 20 20 29  ENDING_LOCK).  )
d2880 7b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70  {.    lock.l_typ
d2890 65 20 3d 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 53  e = (locktype==S
d28a0 48 41 52 45 44 5f 4c 4f 43 4b 3f 46 5f 52 44 4c  HARED_LOCK?F_RDL
d28b0 43 4b 3a 46 5f 57 52 4c 43 4b 29 3b 0a 20 20 20  CK:F_WRLCK);.   
d28c0 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20   lock.l_start = 
d28d0 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20  PENDING_BYTE;.  
d28e0 20 20 73 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c    s = fcntl(pFil
d28f0 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26  e->h, F_SETLK, &
d2900 6c 6f 63 6b 29 3b 0a 20 20 20 20 69 66 28 20 73  lock);.    if( s
d2910 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20  ==(-1) ){.      
d2920 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a  tErrno = errno;.
d2930 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
d2940 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45  eErrorFromPosixE
d2950 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c  rror(tErrno, SQL
d2960 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b  ITE_IOERR_LOCK);
d2970 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f  .      if( IS_LO
d2980 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a  CK_ERROR(rc) ){.
d2990 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c          pFile->l
d29a0 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e  astErrno = tErrn
d29b0 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  o;.      }.     
d29c0 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a   goto end_lock;.
d29d0 20 20 20 20 7d 0a 20 20 7d 0a 0a 0a 20 20 2f 2a      }.  }...  /*
d29e0 20 49 66 20 63 6f 6e 74 72 6f 6c 20 67 65 74 73   If control gets
d29f0 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 2c 20   to this point, 
d2a00 74 68 65 6e 20 61 63 74 75 61 6c 6c 79 20 67 6f  then actually go
d2a10 20 61 68 65 61 64 20 61 6e 64 20 6d 61 6b 65 0a   ahead and make.
d2a20 20 20 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20 73    ** operating s
d2a30 79 73 74 65 6d 20 63 61 6c 6c 73 20 66 6f 72 20  ystem calls for 
d2a40 74 68 65 20 73 70 65 63 69 66 69 65 64 20 6c 6f  the specified lo
d2a50 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c  ck..  */.  if( l
d2a60 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f  ocktype==SHARED_
d2a70 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 61 73 73 65  LOCK ){.    asse
d2a80 72 74 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74 3d 3d  rt( pLock->cnt==
d2a90 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
d2aa0 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65   pLock->locktype
d2ab0 3d 3d 30 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 4e  ==0 );..    /* N
d2ac0 6f 77 20 67 65 74 20 74 68 65 20 72 65 61 64 2d  ow get the read-
d2ad0 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 73 20 3d 20  lock */.    s = 
d2ae0 72 61 6e 67 65 4c 6f 63 6b 28 70 46 69 6c 65 2c  rangeLock(pFile,
d2af0 20 46 5f 52 44 4c 43 4b 2c 20 26 74 45 72 72 6e   F_RDLCK, &tErrn
d2b00 6f 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 72 6f 70  o);..    /* Drop
d2b10 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 50   the temporary P
d2b20 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 2a 2f 0a 20  ENDING lock */. 
d2b30 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20     lock.l_start 
d2b40 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a  = PENDING_BYTE;.
d2b50 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d      lock.l_len =
d2b60 20 31 4c 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f   1L;.    lock.l_
d2b70 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a  type = F_UNLCK;.
d2b80 20 20 20 20 69 66 28 20 66 63 6e 74 6c 28 70 46      if( fcntl(pF
d2b90 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c  ile->h, F_SETLK,
d2ba0 20 26 6c 6f 63 6b 29 21 3d 30 20 29 7b 0a 20 20   &lock)!=0 ){.  
d2bb0 20 20 20 20 69 66 28 20 73 20 21 3d 20 2d 31 20      if( s != -1 
d2bc0 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ){.        /* Th
d2bd0 69 73 20 63 6f 75 6c 64 20 68 61 70 70 65 6e 20  is could happen 
d2be0 77 69 74 68 20 61 20 6e 65 74 77 6f 72 6b 20 6d  with a network m
d2bf0 6f 75 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20  ount */.        
d2c00 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 20  tErrno = errno; 
d2c10 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
d2c20 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73  liteErrorFromPos
d2c30 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20  ixError(tErrno, 
d2c40 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c  SQLITE_IOERR_UNL
d2c50 4f 43 4b 29 3b 20 0a 20 20 20 20 20 20 20 20 69  OCK); .        i
d2c60 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52  f( IS_LOCK_ERROR
d2c70 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20  (rc) ){.        
d2c80 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
d2c90 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20  no = tErrno;.   
d2ca0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 67       }.        g
d2cb0 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20  oto end_lock;.  
d2cc0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
d2cd0 69 66 28 20 73 3d 3d 28 2d 31 29 20 29 7b 0a 20  if( s==(-1) ){. 
d2ce0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
d2cf0 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72  ErrorFromPosixEr
d2d00 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49  ror(tErrno, SQLI
d2d10 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a  TE_IOERR_LOCK);.
d2d20 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43        if( IS_LOC
d2d30 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20  K_ERROR(rc) ){. 
d2d40 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61         pFile->la
d2d50 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f  stErrno = tErrno
d2d60 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
d2d70 6c 73 65 7b 0a 20 20 20 20 20 20 70 46 69 6c 65  lse{.      pFile
d2d80 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41  ->locktype = SHA
d2d90 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20  RED_LOCK;.      
d2da0 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c  pFile->pOpen->nL
d2db0 6f 63 6b 2b 2b 3b 0a 20 20 20 20 20 20 70 4c 6f  ock++;.      pLo
d2dc0 63 6b 2d 3e 63 6e 74 20 3d 20 31 3b 0a 20 20 20  ck->cnt = 1;.   
d2dd0 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6c   }.  }else if( l
d2de0 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49  ocktype==EXCLUSI
d2df0 56 45 5f 4c 4f 43 4b 20 26 26 20 70 4c 6f 63 6b  VE_LOCK && pLock
d2e00 2d 3e 63 6e 74 3e 31 20 29 7b 0a 20 20 20 20 2f  ->cnt>1 ){.    /
d2e10 2a 20 57 65 20 61 72 65 20 74 72 79 69 6e 67 20  * We are trying 
d2e20 66 6f 72 20 61 6e 20 65 78 63 6c 75 73 69 76 65  for an exclusive
d2e30 20 6c 6f 63 6b 20 62 75 74 20 61 6e 6f 74 68 65   lock but anothe
d2e40 72 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73  r thread in this
d2e50 0a 20 20 20 20 2a 2a 20 73 61 6d 65 20 70 72 6f  .    ** same pro
d2e60 63 65 73 73 20 69 73 20 73 74 69 6c 6c 20 68 6f  cess is still ho
d2e70 6c 64 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c  lding a shared l
d2e80 6f 63 6b 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d  ock. */.    rc =
d2e90 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
d2ea0 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68  }else{.    /* Th
d2eb0 65 20 72 65 71 75 65 73 74 20 77 61 73 20 66 6f  e request was fo
d2ec0 72 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20  r a RESERVED or 
d2ed0 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20  EXCLUSIVE lock. 
d2ee0 20 49 74 20 69 73 0a 20 20 20 20 2a 2a 20 61 73   It is.    ** as
d2ef0 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 72 65  sumed that there
d2f00 20 69 73 20 61 20 53 48 41 52 45 44 20 6f 72 20   is a SHARED or 
d2f10 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20  greater lock on 
d2f20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20  the file.    ** 
d2f30 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a  already..    */.
d2f40 20 20 20 20 61 73 73 65 72 74 28 20 30 21 3d 70      assert( 0!=p
d2f50 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29  File->locktype )
d2f60 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70  ;.    lock.l_typ
d2f70 65 20 3d 20 46 5f 57 52 4c 43 4b 3b 0a 20 20 20  e = F_WRLCK;.   
d2f80 20 73 77 69 74 63 68 28 20 6c 6f 63 6b 74 79 70   switch( locktyp
d2f90 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20  e ){.      case 
d2fa0 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3a 0a 20  RESERVED_LOCK:. 
d2fb0 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74         lock.l_st
d2fc0 61 72 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42  art = RESERVED_B
d2fd0 59 54 45 3b 0a 20 20 20 20 20 20 20 20 73 20 3d  YTE;.        s =
d2fe0 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c   fcntl(pFile->h,
d2ff0 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29   F_SETLK, &lock)
d3000 3b 0a 20 20 20 20 20 20 20 20 74 45 72 72 6e 6f  ;.        tErrno
d3010 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20   = errno;.      
d3020 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
d3030 61 73 65 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f  ase EXCLUSIVE_LO
d3040 43 4b 3a 0a 20 20 20 20 20 20 20 20 73 20 3d 20  CK:.        s = 
d3050 72 61 6e 67 65 4c 6f 63 6b 28 70 46 69 6c 65 2c  rangeLock(pFile,
d3060 20 46 5f 57 52 4c 43 4b 2c 20 26 74 45 72 72 6e   F_WRLCK, &tErrn
d3070 6f 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  o);.        brea
d3080 6b 3b 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74  k;.      default
d3090 3a 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  :.        assert
d30a0 28 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  (0);.    }.    i
d30b0 66 28 20 73 3d 3d 28 2d 31 29 20 29 7b 0a 20 20  f( s==(-1) ){.  
d30c0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45      rc = sqliteE
d30d0 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72  rrorFromPosixErr
d30e0 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54  or(tErrno, SQLIT
d30f0 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20  E_IOERR_LOCK);. 
d3100 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b       if( IS_LOCK
d3110 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20  _ERROR(rc) ){.  
d3120 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73        pFile->las
d3130 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b  tErrno = tErrno;
d3140 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
d3150 20 7d 0a 20 20 0a 0a 23 69 66 6e 64 65 66 20 4e   }.  ..#ifndef N
d3160 44 45 42 55 47 0a 20 20 2f 2a 20 53 65 74 20 75  DEBUG.  /* Set u
d3170 70 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  p the transactio
d3180 6e 2d 63 6f 75 6e 74 65 72 20 63 68 61 6e 67 65  n-counter change
d3190 20 63 68 65 63 6b 69 6e 67 20 66 6c 61 67 73 20   checking flags 
d31a0 77 68 65 6e 0a 20 20 2a 2a 20 74 72 61 6e 73 69  when.  ** transi
d31b0 74 69 6f 6e 69 6e 67 20 66 72 6f 6d 20 61 20 53  tioning from a S
d31c0 48 41 52 45 44 20 74 6f 20 61 20 52 45 53 45 52  HARED to a RESER
d31d0 56 45 44 20 6c 6f 63 6b 2e 20 20 54 68 65 20 63  VED lock.  The c
d31e0 68 61 6e 67 65 0a 20 20 2a 2a 20 66 72 6f 6d 20  hange.  ** from 
d31f0 53 48 41 52 45 44 20 74 6f 20 52 45 53 45 52 56  SHARED to RESERV
d3200 45 44 20 6d 61 72 6b 73 20 74 68 65 20 62 65 67  ED marks the beg
d3210 69 6e 6e 69 6e 67 20 6f 66 20 61 20 6e 6f 72 6d  inning of a norm
d3220 61 6c 0a 20 20 2a 2a 20 77 72 69 74 65 20 6f 70  al.  ** write op
d3230 65 72 61 74 69 6f 6e 20 28 6e 6f 74 20 61 20 68  eration (not a h
d3240 6f 74 20 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62  ot journal rollb
d3250 61 63 6b 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ack)..  */.  if(
d3260 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20   rc==SQLITE_OK. 
d3270 20 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b    && pFile->lock
d3280 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43  type<=SHARED_LOC
d3290 4b 0a 20 20 20 26 26 20 6c 6f 63 6b 74 79 70 65  K.   && locktype
d32a0 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a  ==RESERVED_LOCK.
d32b0 20 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e    ){.    pFile->
d32c0 74 72 61 6e 73 43 6e 74 72 43 68 6e 67 20 3d 20  transCntrChng = 
d32d0 30 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 64 62  0;.    pFile->db
d32e0 55 70 64 61 74 65 20 3d 20 30 3b 0a 20 20 20 20  Update = 0;.    
d32f0 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57  pFile->inNormalW
d3300 72 69 74 65 20 3d 20 31 3b 0a 20 20 7d 0a 23 65  rite = 1;.  }.#e
d3310 6e 64 69 66 0a 0a 0a 20 20 69 66 28 20 72 63 3d  ndif...  if( rc=
d3320 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
d3330 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
d3340 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20  e = locktype;.  
d3350 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70    pLock->locktyp
d3360 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20  e = locktype;.  
d3370 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74 79  }else if( lockty
d3380 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f  pe==EXCLUSIVE_LO
d3390 43 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d  CK ){.    pFile-
d33a0 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e 44  >locktype = PEND
d33b0 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20 70 4c  ING_LOCK;.    pL
d33c0 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20  ock->locktype = 
d33d0 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20  PENDING_LOCK;.  
d33e0 7d 0a 0a 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 75  }..end_lock:.  u
d33f0 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b  nixLeaveMutex();
d3400 0a 20 20 4f 53 54 52 41 43 45 34 28 22 4c 4f 43  .  OSTRACE4("LOC
d3410 4b 20 20 20 20 25 64 20 25 73 20 25 73 5c 6e 22  K    %d %s %s\n"
d3420 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b  , pFile->h, lock
d3430 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70  typeName(locktyp
d3440 65 29 2c 20 0a 20 20 20 20 20 20 72 63 3d 3d 53  e), .      rc==S
d3450 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20  QLITE_OK ? "ok" 
d3460 3a 20 22 66 61 69 6c 65 64 22 29 3b 0a 20 20 72  : "failed");.  r
d3470 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
d3480 2a 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 66 69 6c  ** Close all fil
d3490 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 61 63  e descriptors ac
d34a0 63 75 6d 75 61 74 65 64 20 69 6e 20 74 68 65 20  cumuated in the 
d34b0 75 6e 69 78 4f 70 65 6e 43 6e 74 2d 3e 70 55 6e  unixOpenCnt->pUn
d34c0 75 73 65 64 20 6c 69 73 74 2e 0a 2a 2a 20 49 66  used list..** If
d34d0 20 61 6c 6c 20 73 75 63 68 20 66 69 6c 65 20 64   all such file d
d34e0 65 73 63 72 69 70 74 6f 72 73 20 61 72 65 20 63  escriptors are c
d34f0 6c 6f 73 65 64 20 77 69 74 68 6f 75 74 20 65 72  losed without er
d3500 72 6f 72 2c 20 74 68 65 20 6c 69 73 74 20 69 73  ror, the list is
d3510 0a 2a 2a 20 63 6c 65 61 72 65 64 20 61 6e 64 20  .** cleared and 
d3520 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e  SQLITE_OK return
d3530 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77  ed..**.** Otherw
d3540 69 73 65 2c 20 69 66 20 61 6e 20 65 72 72 6f 72  ise, if an error
d3550 20 6f 63 63 75 72 73 2c 20 74 68 65 6e 20 73 75   occurs, then su
d3560 63 63 65 73 73 66 75 6c 6c 79 20 63 6c 6f 73 65  ccessfully close
d3570 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  d file descripto
d3580 72 0a 2a 2a 20 65 6e 74 72 69 65 73 20 61 72 65  r.** entries are
d3590 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68   removed from th
d35a0 65 20 6c 69 73 74 2c 20 61 6e 64 20 53 51 4c 49  e list, and SQLI
d35b0 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 20 72  TE_IOERR_CLOSE r
d35c0 65 74 75 72 6e 65 64 2e 20 0a 2a 2a 20 6e 6f 74  eturned. .** not
d35d0 20 64 65 6c 65 74 65 64 20 61 6e 64 20 53 51 4c   deleted and SQL
d35e0 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 20  ITE_IOERR_CLOSE 
d35f0 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 20 0a 73 74  returned..*/ .st
d3600 61 74 69 63 20 69 6e 74 20 63 6c 6f 73 65 50 65  atic int closePe
d3610 6e 64 69 6e 67 46 64 73 28 75 6e 69 78 46 69 6c  ndingFds(unixFil
d3620 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74  e *pFile){.  int
d3630 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
d3640 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70  .  struct unixOp
d3650 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 20 3d 20 70  enCnt *pOpen = p
d3660 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 55  File->pOpen;.  U
d3670 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 70 45 72  nixUnusedFd *pEr
d3680 72 6f 72 20 3d 20 30 3b 0a 20 20 55 6e 69 78 55  ror = 0;.  UnixU
d3690 6e 75 73 65 64 46 64 20 2a 70 3b 0a 20 20 55 6e  nusedFd *p;.  Un
d36a0 69 78 55 6e 75 73 65 64 46 64 20 2a 70 4e 65 78  ixUnusedFd *pNex
d36b0 74 3b 0a 20 20 66 6f 72 28 70 3d 70 4f 70 65 6e  t;.  for(p=pOpen
d36c0 2d 3e 70 55 6e 75 73 65 64 3b 20 70 3b 20 70 3d  ->pUnused; p; p=
d36d0 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 4e 65 78  pNext){.    pNex
d36e0 74 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20  t = p->pNext;.  
d36f0 20 20 69 66 28 20 63 6c 6f 73 65 28 70 2d 3e 66    if( close(p->f
d3700 64 29 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c  d) ){.      pFil
d3710 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65  e->lastErrno = e
d3720 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d  rrno;.      rc =
d3730 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c   SQLITE_IOERR_CL
d3740 4f 53 45 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4e  OSE;.      p->pN
d3750 65 78 74 20 3d 20 70 45 72 72 6f 72 3b 0a 20 20  ext = pError;.  
d3760 20 20 20 20 70 45 72 72 6f 72 20 3d 20 70 3b 0a      pError = p;.
d3770 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
d3780 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29   sqlite3_free(p)
d3790 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 4f  ;.    }.  }.  pO
d37a0 70 65 6e 2d 3e 70 55 6e 75 73 65 64 20 3d 20 70  pen->pUnused = p
d37b0 45 72 72 6f 72 3b 0a 20 20 72 65 74 75 72 6e 20  Error;.  return 
d37c0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64  rc;.}../*.** Add
d37d0 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   the file descri
d37e0 70 74 6f 72 20 75 73 65 64 20 62 79 20 66 69 6c  ptor used by fil
d37f0 65 20 68 61 6e 64 6c 65 20 70 46 69 6c 65 20 74  e handle pFile t
d3800 6f 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  o the correspond
d3810 69 6e 67 0a 2a 2a 20 70 55 6e 75 73 65 64 20 6c  ing.** pUnused l
d3820 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ist..*/.static v
d3830 6f 69 64 20 73 65 74 50 65 6e 64 69 6e 67 46 64  oid setPendingFd
d3840 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65  (unixFile *pFile
d3850 29 7b 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78  ){.  struct unix
d3860 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 20 3d  OpenCnt *pOpen =
d3870 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20   pFile->pOpen;. 
d3880 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 70   UnixUnusedFd *p
d3890 20 3d 20 70 46 69 6c 65 2d 3e 70 55 6e 75 73 65   = pFile->pUnuse
d38a0 64 3b 0a 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20  d;.  p->pNext = 
d38b0 70 4f 70 65 6e 2d 3e 70 55 6e 75 73 65 64 3b 0a  pOpen->pUnused;.
d38c0 20 20 70 4f 70 65 6e 2d 3e 70 55 6e 75 73 65 64    pOpen->pUnused
d38d0 20 3d 20 70 3b 0a 20 20 70 46 69 6c 65 2d 3e 68   = p;.  pFile->h
d38e0 20 3d 20 2d 31 3b 0a 20 20 70 46 69 6c 65 2d 3e   = -1;.  pFile->
d38f0 70 55 6e 75 73 65 64 20 3d 20 30 3b 0a 7d 0a 0a  pUnused = 0;.}..
d3900 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20  /*.** Lower the 
d3910 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e  locking level on
d3920 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
d3930 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79   pFile to lockty
d3940 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a  pe.  locktype.**
d3950 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20   must be either 
d3960 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45  NO_LOCK or SHARE
d3970 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66  D_LOCK..**.** If
d3980 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76   the locking lev
d3990 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64  el of the file d
d39a0 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72  escriptor is alr
d39b0 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77  eady at or below
d39c0 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 65  .** the requeste
d39d0 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c  d locking level,
d39e0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
d39f0 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61   a no-op..*/.sta
d3a00 74 69 63 20 69 6e 74 20 75 6e 69 78 55 6e 6c 6f  tic int unixUnlo
d3a10 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ck(sqlite3_file 
d3a20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70  *id, int locktyp
d3a30 65 29 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a  e){.  unixFile *
d3a40 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c  pFile = (unixFil
d3a50 65 2a 29 69 64 3b 20 2f 2a 20 54 68 65 20 6f 70  e*)id; /* The op
d3a60 65 6e 20 66 69 6c 65 20 2a 2f 0a 20 20 73 74 72  en file */.  str
d3a70 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f  uct unixLockInfo
d3a80 20 2a 70 4c 6f 63 6b 3b 20 20 20 20 20 20 2f 2a   *pLock;      /*
d3a90 20 53 74 72 75 63 74 75 72 65 20 64 65 73 63 72   Structure descr
d3aa0 69 62 69 6e 67 20 63 75 72 72 65 6e 74 20 6c 6f  ibing current lo
d3ab0 63 6b 20 73 74 61 74 65 20 2a 2f 0a 20 20 73 74  ck state */.  st
d3ac0 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b  ruct flock lock;
d3ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
d3ae0 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 70 61  * Information pa
d3af0 73 73 65 64 20 69 6e 74 6f 20 66 63 6e 74 6c 28  ssed into fcntl(
d3b00 29 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20  ) */.  int rc = 
d3b10 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20  SQLITE_OK;      
d3b20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
d3b30 6e 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 69 73  n code from this
d3b40 20 69 6e 74 65 72 66 61 63 65 20 2a 2f 0a 20 20   interface */.  
d3b50 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20 20  int h;          
d3b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d3b70 20 2f 2a 20 54 68 65 20 75 6e 64 65 72 6c 79 69   /* The underlyi
d3b80 6e 67 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  ng file descript
d3b90 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 74 45 72 72  or */.  int tErr
d3ba0 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  no;             
d3bb0 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f           /* Erro
d3bc0 72 20 63 6f 64 65 20 66 72 6f 6d 20 73 79 73 74  r code from syst
d3bd0 65 6d 20 63 61 6c 6c 20 65 72 72 6f 72 73 20 2a  em call errors *
d3be0 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69  /..  assert( pFi
d3bf0 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 37  le );.  OSTRACE7
d3c00 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20  ("UNLOCK  %d %d 
d3c10 77 61 73 20 25 64 28 25 64 2c 25 64 29 20 70 69  was %d(%d,%d) pi
d3c20 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  d=%d\n", pFile->
d3c30 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 20  h, locktype,.   
d3c40 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79     pFile->lockty
d3c50 70 65 2c 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b  pe, pFile->pLock
d3c60 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c  ->locktype, pFil
d3c70 65 2d 3e 70 4c 6f 63 6b 2d 3e 63 6e 74 2c 20 67  e->pLock->cnt, g
d3c80 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 61 73 73  etpid());..  ass
d3c90 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53  ert( locktype<=S
d3ca0 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20  HARED_LOCK );.  
d3cb0 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  if( pFile->lockt
d3cc0 79 70 65 3c 3d 6c 6f 63 6b 74 79 70 65 20 29 7b  ype<=locktype ){
d3cd0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
d3ce0 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28  TE_OK;.  }.  if(
d3cf0 20 43 48 45 43 4b 5f 54 48 52 45 41 44 49 44 28   CHECK_THREADID(
d3d00 70 46 69 6c 65 29 20 29 7b 0a 20 20 20 20 72 65  pFile) ){.    re
d3d10 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55  turn SQLITE_MISU
d3d20 53 45 3b 0a 20 20 7d 0a 20 20 75 6e 69 78 45 6e  SE;.  }.  unixEn
d3d30 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 68 20  terMutex();.  h 
d3d40 3d 20 70 46 69 6c 65 2d 3e 68 3b 0a 20 20 70 4c  = pFile->h;.  pL
d3d50 6f 63 6b 20 3d 20 70 46 69 6c 65 2d 3e 70 4c 6f  ock = pFile->pLo
d3d60 63 6b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c  ck;.  assert( pL
d3d70 6f 63 6b 2d 3e 63 6e 74 21 3d 30 20 29 3b 0a 20  ock->cnt!=0 );. 
d3d80 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b   if( pFile->lock
d3d90 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b  type>SHARED_LOCK
d3da0 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
d3db0 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pLock->locktype=
d3dc0 3d 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65  =pFile->locktype
d3dd0 20 29 3b 0a 20 20 20 20 53 69 6d 75 6c 61 74 65   );.    Simulate
d3de0 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 31 29  IOErrorBenign(1)
d3df0 3b 0a 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f  ;.    SimulateIO
d3e00 45 72 72 6f 72 28 20 68 3d 28 2d 31 29 20 29 0a  Error( h=(-1) ).
d3e10 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72      SimulateIOEr
d3e20 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b 0a 0a 23  rorBenign(0);..#
d3e30 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20  ifndef NDEBUG.  
d3e40 20 20 2f 2a 20 57 68 65 6e 20 72 65 64 75 63 69    /* When reduci
d3e50 6e 67 20 61 20 6c 6f 63 6b 20 73 75 63 68 20 74  ng a lock such t
d3e60 68 61 74 20 6f 74 68 65 72 20 70 72 6f 63 65 73  hat other proces
d3e70 73 65 73 20 63 61 6e 20 73 74 61 72 74 0a 20 20  ses can start.  
d3e80 20 20 2a 2a 20 72 65 61 64 69 6e 67 20 74 68 65    ** reading the
d3e90 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
d3ea0 67 61 69 6e 2c 20 6d 61 6b 65 20 73 75 72 65 20  gain, make sure 
d3eb0 74 68 61 74 20 74 68 65 0a 20 20 20 20 2a 2a 20  that the.    ** 
d3ec0 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e  transaction coun
d3ed0 74 65 72 20 77 61 73 20 75 70 64 61 74 65 64 20  ter was updated 
d3ee0 69 66 20 61 6e 79 20 70 61 72 74 20 6f 66 20 74  if any part of t
d3ef0 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20  he database.    
d3f00 2a 2a 20 66 69 6c 65 20 63 68 61 6e 67 65 64 2e  ** file changed.
d3f10 20 20 49 66 20 74 68 65 20 74 72 61 6e 73 61 63    If the transac
d3f20 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 69 73 20  tion counter is 
d3f30 6e 6f 74 20 75 70 64 61 74 65 64 2c 0a 20 20 20  not updated,.   
d3f40 20 2a 2a 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63   ** other connec
d3f50 74 69 6f 6e 73 20 74 6f 20 74 68 65 20 73 61 6d  tions to the sam
d3f60 65 20 66 69 6c 65 20 6d 69 67 68 74 20 6e 6f 74  e file might not
d3f70 20 72 65 61 6c 69 7a 65 20 74 68 61 74 0a 20 20   realize that.  
d3f80 20 20 2a 2a 20 74 68 65 20 66 69 6c 65 20 68 61    ** the file ha
d3f90 73 20 63 68 61 6e 67 65 64 20 61 6e 64 20 68 65  s changed and he
d3fa0 6e 63 65 20 6d 69 67 68 74 20 6e 6f 74 20 6b 6e  nce might not kn
d3fb0 6f 77 20 74 6f 20 66 6c 75 73 68 20 74 68 65 69  ow to flush thei
d3fc0 72 0a 20 20 20 20 2a 2a 20 63 61 63 68 65 2e 20  r.    ** cache. 
d3fd0 20 54 68 65 20 75 73 65 20 6f 66 20 61 20 73 74   The use of a st
d3fe0 61 6c 65 20 63 61 63 68 65 20 63 61 6e 20 6c 65  ale cache can le
d3ff0 61 64 20 74 6f 20 64 61 74 61 62 61 73 65 20 63  ad to database c
d4000 6f 72 72 75 70 74 69 6f 6e 2e 0a 20 20 20 20 2a  orruption..    *
d4010 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46  /.    assert( pF
d4020 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69  ile->inNormalWri
d4030 74 65 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 7c  te==0.         |
d4040 7c 20 70 46 69 6c 65 2d 3e 64 62 55 70 64 61 74  | pFile->dbUpdat
d4050 65 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 7c 7c  e==0.         ||
d4060 20 70 46 69 6c 65 2d 3e 74 72 61 6e 73 43 6e 74   pFile->transCnt
d4070 72 43 68 6e 67 3d 3d 31 20 29 3b 0a 20 20 20 20  rChng==1 );.    
d4080 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57  pFile->inNormalW
d4090 72 69 74 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66  rite = 0;.#endif
d40a0 0a 0a 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 74  ...    if( lockt
d40b0 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
d40c0 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 72 61   ){.      if( ra
d40d0 6e 67 65 4c 6f 63 6b 28 70 46 69 6c 65 2c 20 46  ngeLock(pFile, F
d40e0 5f 52 44 4c 43 4b 2c 20 26 74 45 72 72 6e 6f 29  _RDLCK, &tErrno)
d40f0 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20  ==(-1) ){.      
d4100 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72    rc = sqliteErr
d4110 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72  orFromPosixError
d4120 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f  (tErrno, SQLITE_
d4130 49 4f 45 52 52 5f 52 44 4c 4f 43 4b 29 3b 0a 20  IOERR_RDLOCK);. 
d4140 20 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f         if( IS_LO
d4150 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a  CK_ERROR(rc) ){.
d4160 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d            pFile-
d4170 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72  >lastErrno = tEr
d4180 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  rno;.        }. 
d4190 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f         goto end_
d41a0 75 6e 6c 6f 63 6b 3b 0a 20 20 20 20 20 20 7d 0a  unlock;.      }.
d41b0 20 20 20 20 7d 0a 20 20 20 20 6c 6f 63 6b 2e 6c      }.    lock.l
d41c0 5f 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b  _type = F_UNLCK;
d41d0 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e  .    lock.l_when
d41e0 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20  ce = SEEK_SET;. 
d41f0 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20     lock.l_start 
d4200 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a  = PENDING_BYTE;.
d4210 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d      lock.l_len =
d4220 20 32 4c 3b 20 20 61 73 73 65 72 74 28 20 50 45   2L;  assert( PE
d4230 4e 44 49 4e 47 5f 42 59 54 45 2b 31 3d 3d 52 45  NDING_BYTE+1==RE
d4240 53 45 52 56 45 44 5f 42 59 54 45 20 29 3b 0a 20  SERVED_BYTE );. 
d4250 20 20 20 69 66 28 20 66 63 6e 74 6c 28 68 2c 20     if( fcntl(h, 
d4260 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 21  F_SETLK, &lock)!
d4270 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 70  =(-1) ){.      p
d4280 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  Lock->locktype =
d4290 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20   SHARED_LOCK;.  
d42a0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 74    }else{.      t
d42b0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
d42c0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
d42d0 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72  ErrorFromPosixEr
d42e0 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49  ror(tErrno, SQLI
d42f0 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29  TE_IOERR_UNLOCK)
d4300 3b 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c  ;.      if( IS_L
d4310 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b  OCK_ERROR(rc) ){
d4320 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  .        pFile->
d4330 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72  lastErrno = tErr
d4340 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  no;.      }.    
d4350 20 20 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63    goto end_unloc
d4360 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  k;.    }.  }.  i
d4370 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f  f( locktype==NO_
d4380 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 73 74 72 75  LOCK ){.    stru
d4390 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a  ct unixOpenCnt *
d43a0 70 4f 70 65 6e 3b 0a 0a 20 20 20 20 2f 2a 20 44  pOpen;..    /* D
d43b0 65 63 72 65 6d 65 6e 74 20 74 68 65 20 73 68 61  ecrement the sha
d43c0 72 65 64 20 6c 6f 63 6b 20 63 6f 75 6e 74 65 72  red lock counter
d43d0 2e 20 20 52 65 6c 65 61 73 65 20 74 68 65 20 6c  .  Release the l
d43e0 6f 63 6b 20 75 73 69 6e 67 20 61 6e 0a 20 20 20  ock using an.   
d43f0 20 2a 2a 20 4f 53 20 63 61 6c 6c 20 6f 6e 6c 79   ** OS call only
d4400 20 77 68 65 6e 20 61 6c 6c 20 74 68 72 65 61 64   when all thread
d4410 73 20 69 6e 20 74 68 69 73 20 73 61 6d 65 20 70  s in this same p
d4420 72 6f 63 65 73 73 20 68 61 76 65 20 72 65 6c 65  rocess have rele
d4430 61 73 65 64 0a 20 20 20 20 2a 2a 20 74 68 65 20  ased.    ** the 
d4440 6c 6f 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  lock..    */.   
d4450 20 70 4c 6f 63 6b 2d 3e 63 6e 74 2d 2d 3b 0a 20   pLock->cnt--;. 
d4460 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 63 6e     if( pLock->cn
d4470 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6c 6f  t==0 ){.      lo
d4480 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e  ck.l_type = F_UN
d4490 4c 43 4b 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e  LCK;.      lock.
d44a0 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f  l_whence = SEEK_
d44b0 53 45 54 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e  SET;.      lock.
d44c0 6c 5f 73 74 61 72 74 20 3d 20 6c 6f 63 6b 2e 6c  l_start = lock.l
d44d0 5f 6c 65 6e 20 3d 20 30 4c 3b 0a 20 20 20 20 20  _len = 0L;.     
d44e0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
d44f0 42 65 6e 69 67 6e 28 31 29 3b 0a 20 20 20 20 20  Benign(1);.     
d4500 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
d4510 28 20 68 3d 28 2d 31 29 20 29 0a 20 20 20 20 20  ( h=(-1) ).     
d4520 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
d4530 42 65 6e 69 67 6e 28 30 29 3b 0a 20 20 20 20 20  Benign(0);.     
d4540 20 69 66 28 20 66 63 6e 74 6c 28 68 2c 20 46 5f   if( fcntl(h, F_
d4550 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 21 3d 28  SETLK, &lock)!=(
d4560 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70  -1) ){.        p
d4570 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  Lock->locktype =
d4580 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20   NO_LOCK;.      
d4590 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 74  }else{.        t
d45a0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
d45b0 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
d45c0 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78  teErrorFromPosix
d45d0 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51  Error(tErrno, SQ
d45e0 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43  LITE_IOERR_UNLOC
d45f0 4b 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  K);.        if( 
d4600 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63  IS_LOCK_ERROR(rc
d4610 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  ) ){.          p
d4620 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  File->lastErrno 
d4630 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20  = tErrno;.      
d4640 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4c 6f 63    }.        pLoc
d4650 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f  k->locktype = NO
d4660 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 20 20 70  _LOCK;.        p
d4670 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  File->locktype =
d4680 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20   NO_LOCK;.      
d4690 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  }.    }..    /* 
d46a0 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 63 6f  Decrement the co
d46b0 75 6e 74 20 6f 66 20 6c 6f 63 6b 73 20 61 67 61  unt of locks aga
d46c0 69 6e 73 74 20 74 68 69 73 20 73 61 6d 65 20 66  inst this same f
d46d0 69 6c 65 2e 20 20 57 68 65 6e 20 74 68 65 0a 20  ile.  When the. 
d46e0 20 20 20 2a 2a 20 63 6f 75 6e 74 20 72 65 61 63     ** count reac
d46f0 68 65 73 20 7a 65 72 6f 2c 20 63 6c 6f 73 65 20  hes zero, close 
d4700 61 6e 79 20 6f 74 68 65 72 20 66 69 6c 65 20 64  any other file d
d4710 65 73 63 72 69 70 74 6f 72 73 20 77 68 6f 73 65  escriptors whose
d4720 20 63 6c 6f 73 65 0a 20 20 20 20 2a 2a 20 77 61   close.    ** wa
d4730 73 20 64 65 66 65 72 72 65 64 20 62 65 63 61 75  s deferred becau
d4740 73 65 20 6f 66 20 6f 75 74 73 74 61 6e 64 69 6e  se of outstandin
d4750 67 20 6c 6f 63 6b 73 2e 0a 20 20 20 20 2a 2f 0a  g locks..    */.
d4760 20 20 20 20 70 4f 70 65 6e 20 3d 20 70 46 69 6c      pOpen = pFil
d4770 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 20 20 70 4f  e->pOpen;.    pO
d4780 70 65 6e 2d 3e 6e 4c 6f 63 6b 2d 2d 3b 0a 20 20  pen->nLock--;.  
d4790 20 20 61 73 73 65 72 74 28 20 70 4f 70 65 6e 2d    assert( pOpen-
d47a0 3e 6e 4c 6f 63 6b 3e 3d 30 20 29 3b 0a 20 20 20  >nLock>=0 );.   
d47b0 20 69 66 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63   if( pOpen->nLoc
d47c0 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e  k==0 ){.      in
d47d0 74 20 72 63 32 20 3d 20 63 6c 6f 73 65 50 65 6e  t rc2 = closePen
d47e0 64 69 6e 67 46 64 73 28 70 46 69 6c 65 29 3b 0a  dingFds(pFile);.
d47f0 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
d4800 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
d4810 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 20     rc = rc2;.   
d4820 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 09     }.    }.  }..
d4830 0a 65 6e 64 5f 75 6e 6c 6f 63 6b 3a 0a 20 20 75  .end_unlock:.  u
d4840 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b  nixLeaveMutex();
d4850 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
d4860 45 5f 4f 4b 20 29 20 70 46 69 6c 65 2d 3e 6c 6f  E_OK ) pFile->lo
d4870 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70  cktype = locktyp
d4880 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  e;.  return rc;.
d4890 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
d48a0 6e 63 74 69 6f 6e 20 70 65 72 66 6f 72 6d 73 20  nction performs 
d48b0 74 68 65 20 70 61 72 74 73 20 6f 66 20 74 68 65  the parts of the
d48c0 20 22 63 6c 6f 73 65 20 66 69 6c 65 22 20 6f 70   "close file" op
d48d0 65 72 61 74 69 6f 6e 20 0a 2a 2a 20 63 6f 6d 6d  eration .** comm
d48e0 6f 6e 20 74 6f 20 61 6c 6c 20 6c 6f 63 6b 69 6e  on to all lockin
d48f0 67 20 73 63 68 65 6d 65 73 2e 20 49 74 20 63 6c  g schemes. It cl
d4900 6f 73 65 73 20 74 68 65 20 64 69 72 65 63 74 6f  oses the directo
d4910 72 79 20 61 6e 64 20 66 69 6c 65 0a 2a 2a 20 68  ry and file.** h
d4920 61 6e 64 6c 65 73 2c 20 69 66 20 74 68 65 79 20  andles, if they 
d4930 61 72 65 20 76 61 6c 69 64 2c 20 61 6e 64 20 73  are valid, and s
d4940 65 74 73 20 61 6c 6c 20 66 69 65 6c 64 73 20 6f  ets all fields o
d4950 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65 0a 2a  f the unixFile.*
d4960 2a 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 30  * structure to 0
d4970 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 2a 6e  ..**.** It is *n
d4980 6f 74 2a 20 6e 65 63 65 73 73 61 72 79 20 74 6f  ot* necessary to
d4990 20 68 6f 6c 64 20 74 68 65 20 6d 75 74 65 78 20   hold the mutex 
d49a0 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  when this routin
d49b0 65 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20  e is called,.** 
d49c0 65 76 65 6e 20 6f 6e 20 56 78 57 6f 72 6b 73 2e  even on VxWorks.
d49d0 20 20 41 20 6d 75 74 65 78 20 77 69 6c 6c 20 62    A mutex will b
d49e0 65 20 61 63 71 75 69 72 65 64 20 6f 6e 20 56 78  e acquired on Vx
d49f0 57 6f 72 6b 73 20 62 79 20 74 68 65 0a 2a 2a 20  Works by the.** 
d4a00 76 78 77 6f 72 6b 73 52 65 6c 65 61 73 65 46 69  vxworksReleaseFi
d4a10 6c 65 49 64 28 29 20 72 6f 75 74 69 6e 65 2e 0a  leId() routine..
d4a20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6c  */.static int cl
d4a30 6f 73 65 55 6e 69 78 46 69 6c 65 28 73 71 6c 69  oseUnixFile(sqli
d4a40 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20  te3_file *id){. 
d4a50 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
d4a60 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
d4a70 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 20 29 7b  ;.  if( pFile ){
d4a80 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  .    if( pFile->
d4a90 64 69 72 66 64 3e 3d 30 20 29 7b 0a 20 20 20 20  dirfd>=0 ){.    
d4aa0 20 20 69 6e 74 20 65 72 72 20 3d 20 63 6c 6f 73    int err = clos
d4ab0 65 28 70 46 69 6c 65 2d 3e 64 69 72 66 64 29 3b  e(pFile->dirfd);
d4ac0 0a 20 20 20 20 20 20 69 66 28 20 65 72 72 20 29  .      if( err )
d4ad0 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d  {.        pFile-
d4ae0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72  >lastErrno = err
d4af0 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  no;.        retu
d4b00 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
d4b10 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 20  DIR_CLOSE;.     
d4b20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
d4b30 70 46 69 6c 65 2d 3e 64 69 72 66 64 3d 2d 31 3b  pFile->dirfd=-1;
d4b40 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
d4b50 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 68 3e     if( pFile->h>
d4b60 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  =0 ){.      int 
d4b70 65 72 72 20 3d 20 63 6c 6f 73 65 28 70 46 69 6c  err = close(pFil
d4b80 65 2d 3e 68 29 3b 0a 20 20 20 20 20 20 69 66 28  e->h);.      if(
d4b90 20 65 72 72 20 29 7b 0a 20 20 20 20 20 20 20 20   err ){.        
d4ba0 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
d4bb0 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20   = errno;.      
d4bc0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
d4bd0 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20 20  IOERR_CLOSE;.   
d4be0 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 20 4f     }.    }.#if O
d4bf0 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 69 66  S_VXWORKS.    if
d4c00 28 20 70 46 69 6c 65 2d 3e 70 49 64 20 29 7b 0a  ( pFile->pId ){.
d4c10 20 20 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d        if( pFile-
d4c20 3e 69 73 44 65 6c 65 74 65 20 29 7b 0a 20 20 20  >isDelete ){.   
d4c30 20 20 20 20 20 75 6e 6c 69 6e 6b 28 70 46 69 6c       unlink(pFil
d4c40 65 2d 3e 70 49 64 2d 3e 7a 43 61 6e 6f 6e 69 63  e->pId->zCanonic
d4c50 61 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d  alName);.      }
d4c60 0a 20 20 20 20 20 20 76 78 77 6f 72 6b 73 52 65  .      vxworksRe
d4c70 6c 65 61 73 65 46 69 6c 65 49 64 28 70 46 69 6c  leaseFileId(pFil
d4c80 65 2d 3e 70 49 64 29 3b 0a 20 20 20 20 20 20 70  e->pId);.      p
d4c90 46 69 6c 65 2d 3e 70 49 64 20 3d 20 30 3b 0a 20  File->pId = 0;. 
d4ca0 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
d4cb0 4f 53 54 52 41 43 45 32 28 22 43 4c 4f 53 45 20  OSTRACE2("CLOSE 
d4cc0 20 20 25 2d 33 64 5c 6e 22 2c 20 70 46 69 6c 65    %-3d\n", pFile
d4cd0 2d 3e 68 29 3b 0a 20 20 20 20 4f 70 65 6e 43 6f  ->h);.    OpenCo
d4ce0 75 6e 74 65 72 28 2d 31 29 3b 0a 20 20 20 20 73  unter(-1);.    s
d4cf0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69 6c  qlite3_free(pFil
d4d00 65 2d 3e 70 55 6e 75 73 65 64 29 3b 0a 20 20 20  e->pUnused);.   
d4d10 20 6d 65 6d 73 65 74 28 70 46 69 6c 65 2c 20 30   memset(pFile, 0
d4d20 2c 20 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c  , sizeof(unixFil
d4d30 65 29 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  e));.  }.  retur
d4d40 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
d4d50 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69  /*.** Close a fi
d4d60 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
d4d70 74 20 75 6e 69 78 43 6c 6f 73 65 28 73 71 6c 69  t unixClose(sqli
d4d80 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20  te3_file *id){. 
d4d90 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
d4da0 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 64 20 29 7b  _OK;.  if( id ){
d4db0 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70  .    unixFile *p
d4dc0 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
d4dd0 20 2a 29 69 64 3b 0a 20 20 20 20 75 6e 69 78 55   *)id;.    unixU
d4de0 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43  nlock(id, NO_LOC
d4df0 4b 29 3b 0a 20 20 20 20 75 6e 69 78 45 6e 74 65  K);.    unixEnte
d4e00 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 69 66  rMutex();.    if
d4e10 28 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 20 26  ( pFile->pOpen &
d4e20 26 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e  & pFile->pOpen->
d4e30 6e 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 2f  nLock ){.      /
d4e40 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6f  * If there are o
d4e50 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73  utstanding locks
d4e60 2c 20 64 6f 20 6e 6f 74 20 61 63 74 75 61 6c 6c  , do not actuall
d4e70 79 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65  y close the file
d4e80 20 6a 75 73 74 0a 20 20 20 20 20 20 2a 2a 20 79   just.      ** y
d4e90 65 74 20 62 65 63 61 75 73 65 20 74 68 61 74 20  et because that 
d4ea0 77 6f 75 6c 64 20 63 6c 65 61 72 20 74 68 6f 73  would clear thos
d4eb0 65 20 6c 6f 63 6b 73 2e 20 20 49 6e 73 74 65 61  e locks.  Instea
d4ec0 64 2c 20 61 64 64 20 74 68 65 20 66 69 6c 65 0a  d, add the file.
d4ed0 20 20 20 20 20 20 2a 2a 20 64 65 73 63 72 69 70        ** descrip
d4ee0 74 6f 72 20 74 6f 20 70 4f 70 65 6e 2d 3e 70 55  tor to pOpen->pU
d4ef0 6e 75 73 65 64 20 6c 69 73 74 2e 20 20 49 74 20  nused list.  It 
d4f00 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 69  will be automati
d4f10 63 61 6c 6c 79 20 63 6c 6f 73 65 64 20 0a 20 20  cally closed .  
d4f20 20 20 20 20 2a 2a 20 77 68 65 6e 20 74 68 65 20      ** when the 
d4f30 6c 61 73 74 20 6c 6f 63 6b 20 69 73 20 63 6c 65  last lock is cle
d4f40 61 72 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  ared..      */. 
d4f50 20 20 20 20 20 73 65 74 50 65 6e 64 69 6e 67 46       setPendingF
d4f60 64 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a  d(pFile);.    }.
d4f70 20 20 20 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49      releaseLockI
d4f80 6e 66 6f 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b  nfo(pFile->pLock
d4f90 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 4f 70  );.    releaseOp
d4fa0 65 6e 43 6e 74 28 70 46 69 6c 65 2d 3e 70 4f 70  enCnt(pFile->pOp
d4fb0 65 6e 29 3b 0a 20 20 20 20 72 63 20 3d 20 63 6c  en);.    rc = cl
d4fc0 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b  oseUnixFile(id);
d4fd0 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75  .    unixLeaveMu
d4fe0 74 65 78 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74  tex();.  }.  ret
d4ff0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  urn rc;.}../****
d5000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
d5010 66 20 74 68 65 20 70 6f 73 69 78 20 61 64 76 69  f the posix advi
d5020 73 6f 72 79 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d  sory lock implem
d5030 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a  entation *******
d5040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a  **********.*****
d5050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a  *********/../***
d50a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d50b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d50c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d50d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d50e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a  ***********.****
d50f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4e 6f 2d 6f 70  ********** No-op
d5110 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a   Locking *******
d5120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
d5140 2a 20 4f 66 20 74 68 65 20 76 61 72 69 6f 75 73  * Of the various
d5150 20 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 6d 65   locking impleme
d5160 6e 74 61 74 69 6f 6e 73 20 61 76 61 69 6c 61 62  ntations availab
d5170 6c 65 2c 20 74 68 69 73 20 69 73 20 62 79 20 66  le, this is by f
d5180 61 72 20 74 68 65 0a 2a 2a 20 73 69 6d 70 6c 65  ar the.** simple
d5190 73 74 3a 20 20 6c 6f 63 6b 69 6e 67 20 69 73 20  st:  locking is 
d51a0 69 67 6e 6f 72 65 64 2e 20 20 4e 6f 20 61 74 74  ignored.  No att
d51b0 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20  empt is made to 
d51c0 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 73  lock the databas
d51d0 65 0a 2a 2a 20 66 69 6c 65 20 66 6f 72 20 72 65  e.** file for re
d51e0 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67  ading or writing
d51f0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6c 6f 63  ..**.** This loc
d5200 6b 69 6e 67 20 6d 6f 64 65 20 69 73 20 61 70 70  king mode is app
d5210 72 6f 70 72 69 61 74 65 20 66 6f 72 20 75 73 65  ropriate for use
d5220 20 6f 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 64 61   on read-only da
d5230 74 61 62 61 73 65 73 0a 2a 2a 20 28 65 78 3a 20  tabases.** (ex: 
d5240 64 61 74 61 62 61 73 65 73 20 74 68 61 74 20 61  databases that a
d5250 72 65 20 62 75 72 6e 65 64 20 69 6e 74 6f 20 43  re burned into C
d5260 44 2d 52 4f 4d 2c 20 66 6f 72 20 65 78 61 6d 70  D-ROM, for examp
d5270 6c 65 2e 29 20 20 49 74 20 63 61 6e 0a 2a 2a 20  le.)  It can.** 
d5280 61 6c 73 6f 20 62 65 20 75 73 65 64 20 69 66 20  also be used if 
d5290 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  the application 
d52a0 65 6d 70 6c 6f 79 73 20 73 6f 6d 65 20 65 78 74  employs some ext
d52b0 65 72 6e 61 6c 20 6d 65 63 68 61 6e 69 73 6d 20  ernal mechanism 
d52c0 74 6f 0a 2a 2a 20 70 72 65 76 65 6e 74 20 73 69  to.** prevent si
d52d0 6d 75 6c 74 61 6e 65 6f 75 73 20 61 63 63 65 73  multaneous acces
d52e0 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 64 61  s of the same da
d52f0 74 61 62 61 73 65 20 62 79 20 74 77 6f 20 6f 72  tabase by two or
d5300 20 6d 6f 72 65 0a 2a 2a 20 64 61 74 61 62 61 73   more.** databas
d5310 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20  e connections.  
d5320 42 75 74 20 74 68 65 72 65 20 69 73 20 61 20 73  But there is a s
d5330 65 72 69 6f 75 73 20 72 69 73 6b 20 6f 66 20 64  erious risk of d
d5340 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 72 72 75  atabase.** corru
d5350 70 74 69 6f 6e 20 69 66 20 74 68 69 73 20 6c 6f  ption if this lo
d5360 63 6b 69 6e 67 20 6d 6f 64 65 20 69 73 20 75 73  cking mode is us
d5370 65 64 20 69 6e 20 73 69 74 75 61 74 69 6f 6e 73  ed in situations
d5380 20 77 68 65 72 65 20 6d 75 6c 74 69 70 6c 65 0a   where multiple.
d5390 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ** database conn
d53a0 65 63 74 69 6f 6e 73 20 61 72 65 20 61 63 63 65  ections are acce
d53b0 73 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64  ssing the same d
d53c0 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 74 20  atabase file at 
d53d0 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74 69 6d 65  the same.** time
d53e0 20 61 6e 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65   and one or more
d53f0 20 6f 66 20 74 68 6f 73 65 20 63 6f 6e 6e 65 63   of those connec
d5400 74 69 6f 6e 73 20 61 72 65 20 77 72 69 74 69 6e  tions are writin
d5410 67 2e 0a 2a 2f 0a 0a 73 74 61 74 69 63 20 69 6e  g..*/..static in
d5420 74 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b 52 65 73  t nolockCheckRes
d5430 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65  ervedLock(sqlite
d5440 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c  3_file *NotUsed,
d5450 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a   int *pResOut){.
d5460 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
d5470 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 2a  ER(NotUsed);.  *
d5480 70 52 65 73 4f 75 74 20 3d 20 30 3b 0a 20 20 72  pResOut = 0;.  r
d5490 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
d54a0 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f  .}.static int no
d54b0 6c 6f 63 6b 4c 6f 63 6b 28 73 71 6c 69 74 65 33  lockLock(sqlite3
d54c0 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20  _file *NotUsed, 
d54d0 69 6e 74 20 4e 6f 74 55 73 65 64 32 29 7b 0a 20  int NotUsed2){. 
d54e0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
d54f0 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55  R2(NotUsed, NotU
d5500 73 65 64 32 29 3b 0a 20 20 72 65 74 75 72 6e 20  sed2);.  return 
d5510 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 73 74 61  SQLITE_OK;.}.sta
d5520 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 55 6e  tic int nolockUn
d5530 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  lock(sqlite3_fil
d5540 65 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20  e *NotUsed, int 
d5550 4e 6f 74 55 73 65 64 32 29 7b 0a 20 20 55 4e 55  NotUsed2){.  UNU
d5560 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e  SED_PARAMETER2(N
d5570 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32  otUsed, NotUsed2
d5580 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
d5590 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
d55a0 43 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 2e 0a  Close the file..
d55b0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f  */.static int no
d55c0 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 6c 69 74 65  lockClose(sqlite
d55d0 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20  3_file *id) {.  
d55e0 72 65 74 75 72 6e 20 63 6c 6f 73 65 55 6e 69 78  return closeUnix
d55f0 46 69 6c 65 28 69 64 29 3b 0a 7d 0a 0a 2f 2a 2a  File(id);.}../**
d5600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5610 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 6e 6f 2d  * End of the no-
d5620 6f 70 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e  op lock implemen
d5630 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  tation *********
d5640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a  ************.***
d5650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a  ***********/../*
d56a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d56b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d56c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d56d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d56e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
d56f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5700 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 64 6f  ******* Begin do
d5710 74 2d 66 69 6c 65 20 4c 6f 63 6b 69 6e 67 20 2a  t-file Locking *
d5720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
d5740 0a 2a 2a 20 54 68 65 20 64 6f 74 66 69 6c 65 20  .** The dotfile 
d5750 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 6e  locking implemen
d5760 74 61 74 69 6f 6e 20 75 73 65 73 20 74 68 65 20  tation uses the 
d5770 65 78 69 73 74 61 6e 63 65 20 6f 66 20 73 65 70  existance of sep
d5780 61 72 61 74 65 20 6c 6f 63 6b 0a 2a 2a 20 66 69  arate lock.** fi
d5790 6c 65 73 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  les in order to 
d57a0 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 20 74  control access t
d57b0 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  o the database. 
d57c0 20 54 68 69 73 20 77 6f 72 6b 73 20 6f 6e 20 6a   This works on j
d57d0 75 73 74 0a 2a 2a 20 61 62 6f 75 74 20 65 76 65  ust.** about eve
d57e0 72 79 20 66 69 6c 65 73 79 73 74 65 6d 20 69 6d  ry filesystem im
d57f0 61 67 69 6e 61 62 6c 65 2e 20 20 42 75 74 20 74  aginable.  But t
d5800 68 65 72 65 20 61 72 65 20 73 65 72 69 6f 75 73  here are serious
d5810 20 64 6f 77 6e 73 69 64 65 73 3a 0a 2a 2a 0a 2a   downsides:.**.*
d5820 2a 20 20 20 20 28 31 29 20 20 54 68 65 72 65 20  *    (1)  There 
d5830 69 73 20 7a 65 72 6f 20 63 6f 6e 63 75 72 72 65  is zero concurre
d5840 6e 63 79 2e 20 20 41 20 73 69 6e 67 6c 65 20 72  ncy.  A single r
d5850 65 61 64 65 72 20 62 6c 6f 63 6b 73 20 61 6c 6c  eader blocks all
d5860 20 6f 74 68 65 72 0a 2a 2a 20 20 20 20 20 20 20   other.**       
d5870 20 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 72    connections fr
d5880 6f 6d 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72  om reading or wr
d5890 69 74 69 6e 67 20 74 68 65 20 64 61 74 61 62 61  iting the databa
d58a0 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29  se..**.**    (2)
d58b0 20 20 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e    An application
d58c0 20 63 72 61 73 68 20 6f 72 20 70 6f 77 65 72 20   crash or power 
d58d0 6c 6f 73 73 20 63 61 6e 20 6c 65 61 76 65 20 73  loss can leave s
d58e0 74 61 6c 65 20 6c 6f 63 6b 20 66 69 6c 65 73 0a  tale lock files.
d58f0 2a 2a 20 20 20 20 20 20 20 20 20 73 69 74 74 69  **         sitti
d5900 6e 67 20 61 72 6f 75 6e 64 20 74 68 61 74 20 6e  ng around that n
d5910 65 65 64 20 74 6f 20 62 65 20 63 6c 65 61 72 65  eed to be cleare
d5920 64 20 6d 61 6e 75 61 6c 6c 79 2e 0a 2a 2a 0a 2a  d manually..**.*
d5930 2a 20 4e 65 76 65 72 74 68 65 6c 65 73 73 2c 20  * Nevertheless, 
d5940 61 20 64 6f 74 6c 6f 63 6b 20 69 73 20 61 6e 20  a dotlock is an 
d5950 61 70 70 72 6f 70 72 69 61 74 65 20 6c 6f 63 6b  appropriate lock
d5960 69 6e 67 20 6d 6f 64 65 20 66 6f 72 20 75 73 65  ing mode for use
d5970 20 69 66 20 6e 6f 0a 2a 2a 20 6f 74 68 65 72 20   if no.** other 
d5980 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79  locking strategy
d5990 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a   is available..*
d59a0 2a 0a 2a 2a 20 44 6f 74 66 69 6c 65 20 6c 6f 63  *.** Dotfile loc
d59b0 6b 69 6e 67 20 77 6f 72 6b 73 20 62 79 20 63 72  king works by cr
d59c0 65 61 74 69 6e 67 20 61 20 66 69 6c 65 20 69 6e  eating a file in
d59d0 20 74 68 65 20 73 61 6d 65 20 64 69 72 65 63 74   the same direct
d59e0 6f 72 79 20 61 73 20 74 68 65 0a 2a 2a 20 64 61  ory as the.** da
d59f0 74 61 62 61 73 65 20 61 6e 64 20 77 69 74 68 20  tabase and with 
d5a00 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 20 62 75  the same name bu
d5a10 74 20 77 69 74 68 20 61 20 22 2e 6c 6f 63 6b 22  t with a ".lock"
d5a20 20 65 78 74 65 6e 73 69 6f 6e 20 61 64 64 65 64   extension added
d5a30 2e 0a 2a 2a 20 54 68 65 20 65 78 69 73 74 61 6e  ..** The existan
d5a40 63 65 20 6f 66 20 61 20 6c 6f 63 6b 20 66 69 6c  ce of a lock fil
d5a50 65 20 69 6d 70 6c 69 65 73 20 61 6e 20 45 58 43  e implies an EXC
d5a60 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 41 6c  LUSIVE lock.  Al
d5a70 6c 20 6f 74 68 65 72 20 6c 6f 63 6b 0a 2a 2a 20  l other lock.** 
d5a80 74 79 70 65 73 20 28 53 48 41 52 45 44 2c 20 52  types (SHARED, R
d5a90 45 53 45 52 56 45 44 2c 20 50 45 4e 44 49 4e 47  ESERVED, PENDING
d5aa0 29 20 61 72 65 20 6d 61 70 70 65 64 20 69 6e 74  ) are mapped int
d5ab0 6f 20 45 58 43 4c 55 53 49 56 45 2e 0a 2a 2f 0a  o EXCLUSIVE..*/.
d5ac0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20  ./*.** The file 
d5ad0 73 75 66 66 69 78 20 61 64 64 65 64 20 74 6f 20  suffix added to 
d5ae0 74 68 65 20 64 61 74 61 20 62 61 73 65 20 66 69  the data base fi
d5af0 6c 65 6e 61 6d 65 20 69 6e 20 6f 72 64 65 72 20  lename in order 
d5b00 74 6f 20 63 72 65 61 74 65 20 74 68 65 0a 2a 2a  to create the.**
d5b10 20 6c 6f 63 6b 20 66 69 6c 65 2e 0a 2a 2f 0a 23   lock file..*/.#
d5b20 64 65 66 69 6e 65 20 44 4f 54 4c 4f 43 4b 5f 53  define DOTLOCK_S
d5b30 55 46 46 49 58 20 22 2e 6c 6f 63 6b 22 0a 0a 2f  UFFIX ".lock"../
d5b40 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
d5b50 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72  e checks if ther
d5b60 65 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20  e is a RESERVED 
d5b70 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65  lock held on the
d5b80 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69   specified.** fi
d5b90 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e  le by this or an
d5ba0 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e  y other process.
d5bb0 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20   If such a lock 
d5bc0 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52  is held, set *pR
d5bd0 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f  esOut.** to a no
d5be0 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68  n-zero value oth
d5bf0 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20  erwise *pResOut 
d5c00 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20  is set to zero. 
d5c10 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   The return valu
d5c20 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53  e.** is set to S
d5c30 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20  QLITE_OK unless 
d5c40 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63  an I/O error occ
d5c50 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20  urs during lock 
d5c60 63 68 65 63 6b 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  checking..**.** 
d5c70 49 6e 20 64 6f 74 66 69 6c 65 20 6c 6f 63 6b 69  In dotfile locki
d5c80 6e 67 2c 20 65 69 74 68 65 72 20 61 20 6c 6f 63  ng, either a loc
d5c90 6b 20 65 78 69 73 74 73 20 6f 72 20 69 74 20 64  k exists or it d
d5ca0 6f 65 73 20 6e 6f 74 2e 20 20 53 6f 20 69 6e 20  oes not.  So in 
d5cb0 74 68 69 73 0a 2a 2a 20 76 61 72 69 61 74 69 6f  this.** variatio
d5cc0 6e 20 6f 66 20 43 68 65 63 6b 52 65 73 65 72 76  n of CheckReserv
d5cd0 65 64 4c 6f 63 6b 28 29 2c 20 2a 70 52 65 73 4f  edLock(), *pResO
d5ce0 75 74 20 69 73 20 73 65 74 20 74 6f 20 74 72 75  ut is set to tru
d5cf0 65 20 69 66 20 61 6e 79 20 6c 6f 63 6b 0a 2a 2a  e if any lock.**
d5d00 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65 20   is held on the 
d5d10 66 69 6c 65 20 61 6e 64 20 66 61 6c 73 65 20 69  file and false i
d5d20 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 75 6e  f the file is un
d5d30 6c 6f 63 6b 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  locked..*/.stati
d5d40 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b 43 68 65  c int dotlockChe
d5d50 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73  ckReservedLock(s
d5d60 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
d5d70 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 7b   int *pResOut) {
d5d80 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
d5d90 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73  TE_OK;.  int res
d5da0 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69  erved = 0;.  uni
d5db0 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  xFile *pFile = (
d5dc0 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20  unixFile*)id;.. 
d5dd0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
d5de0 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ( return SQLITE_
d5df0 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52  IOERR_CHECKRESER
d5e00 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20  VEDLOCK; );.  . 
d5e10 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29   assert( pFile )
d5e20 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66  ;..  /* Check if
d5e30 20 61 20 74 68 72 65 61 64 20 69 6e 20 74 68 69   a thread in thi
d5e40 73 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20  s process holds 
d5e50 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20  such a lock */. 
d5e60 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b   if( pFile->lock
d5e70 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b  type>SHARED_LOCK
d5e80 20 29 7b 0a 20 20 20 20 2f 2a 20 45 69 74 68 65   ){.    /* Eithe
d5e90 72 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f  r this connectio
d5ea0 6e 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20  n or some other 
d5eb0 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 6e 20 74 68  connection in th
d5ec0 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 0a 20  e same process. 
d5ed0 20 20 20 2a 2a 20 68 6f 6c 64 73 20 61 20 6c 6f     ** holds a lo
d5ee0 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e 20  ck on the file. 
d5ef0 20 4e 6f 20 6e 65 65 64 20 74 6f 20 63 68 65 63   No need to chec
d5f00 6b 20 66 75 72 74 68 65 72 2e 20 2a 2f 0a 20 20  k further. */.  
d5f10 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a    reserved = 1;.
d5f20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
d5f30 54 68 65 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64  The lock is held
d5f40 20 69 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66 20   if and only if 
d5f50 74 68 65 20 6c 6f 63 6b 66 69 6c 65 20 65 78 69  the lockfile exi
d5f60 73 74 73 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74  sts */.    const
d5f70 20 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65   char *zLockFile
d5f80 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29   = (const char*)
d5f90 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
d5fa0 6e 74 65 78 74 3b 0a 20 20 20 20 72 65 73 65 72  ntext;.    reser
d5fb0 76 65 64 20 3d 20 61 63 63 65 73 73 28 7a 4c 6f  ved = access(zLo
d5fc0 63 6b 46 69 6c 65 2c 20 30 29 3d 3d 30 3b 0a 20  ckFile, 0)==0;. 
d5fd0 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54   }.  OSTRACE4("T
d5fe0 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25  EST WR-LOCK %d %
d5ff0 64 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  d %d\n", pFile->
d6000 68 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64 29  h, rc, reserved)
d6010 3b 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72  ;.  *pResOut = r
d6020 65 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72  eserved;.  retur
d6030 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c  n rc;.}../*.** L
d6040 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74  ock the file wit
d6050 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69  h the lock speci
d6060 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65  fied by paramete
d6070 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65  r locktype - one
d6080 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  .** of the follo
d6090 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  wing:.**.**     
d60a0 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a  (1) SHARED_LOCK.
d60b0 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 52  **     (2) RESER
d60c0 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20  VED_LOCK.**     
d60d0 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b  (3) PENDING_LOCK
d60e0 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c  .**     (4) EXCL
d60f0 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a  USIVE_LOCK.**.**
d6100 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20   Sometimes when 
d6110 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c  requesting one l
d6120 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74  ock state, addit
d6130 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65  ional lock state
d6140 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65  s.** are inserte
d6150 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54  d in between.  T
d6160 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74  he locking might
d6170 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20   fail on one of 
d6180 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61  the later.** tra
d6190 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67  nsitions leaving
d61a0 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20   the lock state 
d61b0 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77  different from w
d61c0 68 61 74 20 69 74 20 73 74 61 72 74 65 64 20 62  hat it started b
d61d0 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72  ut.** still shor
d61e0 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20  t of its goal.  
d61f0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68  The following ch
d6200 61 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c  art shows the al
d6210 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74  lowed.** transit
d6220 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73  ions and the ins
d6230 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61  erted intermedia
d6240 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a  te states:.**.**
d6250 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20      UNLOCKED -> 
d6260 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41  SHARED.**    SHA
d6270 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a  RED -> RESERVED.
d6280 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20  **    SHARED -> 
d6290 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43  (PENDING) -> EXC
d62a0 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53  LUSIVE.**    RES
d62b0 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e  ERVED -> (PENDIN
d62c0 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a  G) -> EXCLUSIVE.
d62d0 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e  **    PENDING ->
d62e0 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a   EXCLUSIVE.**.**
d62f0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69   This routine wi
d6300 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65  ll only increase
d6310 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68   a lock.  Use th
d6320 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63  e sqlite3OsUnloc
d6330 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74  k().** routine t
d6340 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e  o lower a lockin
d6350 67 20 6c 65 76 65 6c 2e 0a 2a 2a 0a 2a 2a 20 57  g level..**.** W
d6360 69 74 68 20 64 6f 74 66 69 6c 65 20 6c 6f 63 6b  ith dotfile lock
d6370 69 6e 67 2c 20 77 65 20 72 65 61 6c 6c 79 20 6f  ing, we really o
d6380 6e 6c 79 20 73 75 70 70 6f 72 74 20 73 74 61 74  nly support stat
d6390 65 20 28 34 29 3a 20 45 58 43 4c 55 53 49 56 45  e (4): EXCLUSIVE
d63a0 2e 0a 2a 2a 20 42 75 74 20 77 65 20 74 72 61 63  ..** But we trac
d63b0 6b 20 74 68 65 20 6f 74 68 65 72 20 6c 6f 63 6b  k the other lock
d63c0 69 6e 67 20 6c 65 76 65 6c 73 20 69 6e 74 65 72  ing levels inter
d63d0 6e 61 6c 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  nally..*/.static
d63e0 20 69 6e 74 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b   int dotlockLock
d63f0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
d6400 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29  d, int locktype)
d6410 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70   {.  unixFile *p
d6420 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
d6430 2a 29 69 64 3b 0a 20 20 69 6e 74 20 66 64 3b 0a  *)id;.  int fd;.
d6440 20 20 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c    char *zLockFil
d6450 65 20 3d 20 28 63 68 61 72 20 2a 29 70 46 69 6c  e = (char *)pFil
d6460 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
d6470 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  t;.  int rc = SQ
d6480 4c 49 54 45 5f 4f 4b 3b 0a 0a 0a 20 20 2f 2a 20  LITE_OK;...  /* 
d6490 49 66 20 77 65 20 68 61 76 65 20 61 6e 79 20 6c  If we have any l
d64a0 6f 63 6b 2c 20 74 68 65 6e 20 74 68 65 20 6c 6f  ock, then the lo
d64b0 63 6b 20 66 69 6c 65 20 61 6c 72 65 61 64 79 20  ck file already 
d64c0 65 78 69 73 74 73 2e 20 20 41 6c 6c 20 77 65 20  exists.  All we 
d64d0 68 61 76 65 0a 20 20 2a 2a 20 74 6f 20 64 6f 20  have.  ** to do 
d64e0 69 73 20 61 64 6a 75 73 74 20 6f 75 72 20 69 6e  is adjust our in
d64f0 74 65 72 6e 61 6c 20 72 65 63 6f 72 64 20 6f 66  ternal record of
d6500 20 74 68 65 20 6c 6f 63 6b 20 6c 65 76 65 6c 2e   the lock level.
d6510 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c  .  */.  if( pFil
d6520 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3e 20 4e 4f  e->locktype > NO
d6530 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 70 46 69  _LOCK ){.    pFi
d6540 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c  le->locktype = l
d6550 6f 63 6b 74 79 70 65 3b 0a 23 69 66 20 21 4f 53  ocktype;.#if !OS
d6560 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 2f 2a 20  _VXWORKS.    /* 
d6570 41 6c 77 61 79 73 20 75 70 64 61 74 65 20 74 68  Always update th
d6580 65 20 74 69 6d 65 73 74 61 6d 70 20 6f 6e 20 74  e timestamp on t
d6590 68 65 20 6f 6c 64 20 66 69 6c 65 20 2a 2f 0a 20  he old file */. 
d65a0 20 20 20 75 74 69 6d 65 73 28 7a 4c 6f 63 6b 46     utimes(zLockF
d65b0 69 6c 65 2c 20 4e 55 4c 4c 29 3b 0a 23 65 6e 64  ile, NULL);.#end
d65c0 69 66 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  if.    return SQ
d65d0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a  LITE_OK;.  }.  .
d65e0 20 20 2f 2a 20 67 72 61 62 20 61 6e 20 65 78 63    /* grab an exc
d65f0 6c 75 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20  lusive lock */. 
d6600 20 66 64 20 3d 20 6f 70 65 6e 28 7a 4c 6f 63 6b   fd = open(zLock
d6610 46 69 6c 65 2c 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f  File,O_RDONLY|O_
d6620 43 52 45 41 54 7c 4f 5f 45 58 43 4c 2c 30 36 30  CREAT|O_EXCL,060
d6630 30 29 3b 0a 20 20 69 66 28 20 66 64 3c 30 20 29  0);.  if( fd<0 )
d6640 7b 0a 20 20 20 20 2f 2a 20 66 61 69 6c 65 64 20  {.    /* failed 
d6650 74 6f 20 6f 70 65 6e 2f 63 72 65 61 74 65 20 74  to open/create t
d6660 68 65 20 66 69 6c 65 2c 20 73 6f 6d 65 6f 6e 65  he file, someone
d6670 20 65 6c 73 65 20 6d 61 79 20 68 61 76 65 20 73   else may have s
d6680 74 6f 6c 65 6e 20 74 68 65 20 6c 6f 63 6b 20 2a  tolen the lock *
d6690 2f 0a 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f  /.    int tErrno
d66a0 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 69 66   = errno;.    if
d66b0 28 20 45 45 58 49 53 54 20 3d 3d 20 74 45 72 72  ( EEXIST == tErr
d66c0 6e 6f 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  no ){.      rc =
d66d0 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
d66e0 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20    } else {.     
d66f0 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f   rc = sqliteErro
d6700 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28  rFromPosixError(
d6710 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49  tErrno, SQLITE_I
d6720 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20  OERR_LOCK);.    
d6730 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52    if( IS_LOCK_ER
d6740 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20  ROR(rc) ){.     
d6750 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72     pFile->lastEr
d6760 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20  rno = tErrno;.  
d6770 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
d6780 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 20 0a  return rc;.  } .
d6790 20 20 69 66 28 20 63 6c 6f 73 65 28 66 64 29 20    if( close(fd) 
d67a0 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61  ){.    pFile->la
d67b0 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b  stErrno = errno;
d67c0 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
d67d0 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20  _IOERR_CLOSE;.  
d67e0 7d 0a 20 20 0a 20 20 2f 2a 20 67 6f 74 20 69 74  }.  .  /* got it
d67f0 2c 20 73 65 74 20 74 68 65 20 74 79 70 65 20 61  , set the type a
d6800 6e 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f 0a  nd return ok */.
d6810 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
d6820 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20  e = locktype;.  
d6830 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
d6840 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f  .** Lower the lo
d6850 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66  cking level on f
d6860 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70  ile descriptor p
d6870 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65  File to locktype
d6880 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d  .  locktype.** m
d6890 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f  ust be either NO
d68a0 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f  _LOCK or SHARED_
d68b0 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  LOCK..**.** If t
d68c0 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  he locking level
d68d0 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73   of the file des
d68e0 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61  criptor is alrea
d68f0 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a  dy at or below.*
d6900 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  * the requested 
d6910 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74  locking level, t
d6920 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
d6930 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 57 68   no-op..**.** Wh
d6940 65 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c  en the locking l
d6950 65 76 65 6c 20 72 65 61 63 68 65 73 20 4e 4f 5f  evel reaches NO_
d6960 4c 4f 43 4b 2c 20 64 65 6c 65 74 65 20 74 68 65  LOCK, delete the
d6970 20 6c 6f 63 6b 20 66 69 6c 65 2e 0a 2a 2f 0a 73   lock file..*/.s
d6980 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63  tatic int dotloc
d6990 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  kUnlock(sqlite3_
d69a0 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f  file *id, int lo
d69b0 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78  cktype) {.  unix
d69c0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
d69d0 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 63  nixFile*)id;.  c
d69e0 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 20 3d  har *zLockFile =
d69f0 20 28 63 68 61 72 20 2a 29 70 46 69 6c 65 2d 3e   (char *)pFile->
d6a00 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a  lockingContext;.
d6a10 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65  .  assert( pFile
d6a20 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22   );.  OSTRACE5("
d6a30 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61  UNLOCK  %d %d wa
d6a40 73 20 25 64 20 70 69 64 3d 25 64 5c 6e 22 2c 20  s %d pid=%d\n", 
d6a50 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79  pFile->h, lockty
d6a60 70 65 2c 0a 09 20 20 20 70 46 69 6c 65 2d 3e 6c  pe,..   pFile->l
d6a70 6f 63 6b 74 79 70 65 2c 20 67 65 74 70 69 64 28  ocktype, getpid(
d6a80 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f  ));.  assert( lo
d6a90 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c  cktype<=SHARED_L
d6aa0 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 6e  OCK );.  .  /* n
d6ab0 6f 2d 6f 70 20 69 66 20 70 6f 73 73 69 62 6c 65  o-op if possible
d6ac0 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d   */.  if( pFile-
d6ad0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 6c 6f 63 6b 74  >locktype==lockt
d6ae0 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ype ){.    retur
d6af0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
d6b00 0a 0a 20 20 2f 2a 20 54 6f 20 64 6f 77 6e 67 72  ..  /* To downgr
d6b10 61 64 65 20 74 6f 20 73 68 61 72 65 64 2c 20 73  ade to shared, s
d6b20 69 6d 70 6c 79 20 75 70 64 61 74 65 20 6f 75 72  imply update our
d6b30 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 74 69 6f 6e   internal notion
d6b40 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 6c 6f 63   of the.  ** loc
d6b50 6b 20 73 74 61 74 65 2e 20 20 4e 6f 20 6e 65 65  k state.  No nee
d6b60 64 20 74 6f 20 6d 65 73 73 20 77 69 74 68 20 74  d to mess with t
d6b70 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e  he file on disk.
d6b80 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b  .  */.  if( lock
d6b90 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
d6ba0 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  K ){.    pFile->
d6bb0 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45  locktype = SHARE
d6bc0 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 72 65 74 75  D_LOCK;.    retu
d6bd0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
d6be0 7d 0a 20 20 0a 20 20 2f 2a 20 54 6f 20 66 75 6c  }.  .  /* To ful
d6bf0 6c 79 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64 61  ly unlock the da
d6c00 74 61 62 61 73 65 2c 20 64 65 6c 65 74 65 20 74  tabase, delete t
d6c10 68 65 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a  he lock file */.
d6c20 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79    assert( lockty
d6c30 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20  pe==NO_LOCK );. 
d6c40 20 69 66 28 20 75 6e 6c 69 6e 6b 28 7a 4c 6f 63   if( unlink(zLoc
d6c50 6b 46 69 6c 65 29 20 29 7b 0a 20 20 20 20 69 6e  kFile) ){.    in
d6c60 74 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 69 6e  t rc = 0;.    in
d6c70 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f  t tErrno = errno
d6c80 3b 0a 20 20 20 20 69 66 28 20 45 4e 4f 45 4e 54  ;.    if( ENOENT
d6c90 20 21 3d 20 74 45 72 72 6e 6f 20 29 7b 0a 20 20   != tErrno ){.  
d6ca0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45      rc = sqliteE
d6cb0 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72  rrorFromPosixErr
d6cc0 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54  or(tErrno, SQLIT
d6cd0 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b  E_IOERR_UNLOCK);
d6ce0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 49  .    }.    if( I
d6cf0 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29  S_LOCK_ERROR(rc)
d6d00 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d   ){.      pFile-
d6d10 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72  >lastErrno = tEr
d6d20 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  rno;.    }.    r
d6d30 65 74 75 72 6e 20 72 63 3b 20 0a 20 20 7d 0a 20  eturn rc; .  }. 
d6d40 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
d6d50 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 72 65   = NO_LOCK;.  re
d6d60 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
d6d70 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61  }../*.** Close a
d6d80 20 66 69 6c 65 2e 20 20 4d 61 6b 65 20 73 75 72   file.  Make sur
d6d90 65 20 74 68 65 20 6c 6f 63 6b 20 68 61 73 20 62  e the lock has b
d6da0 65 65 6e 20 72 65 6c 65 61 73 65 64 20 62 65 66  een released bef
d6db0 6f 72 65 20 63 6c 6f 73 69 6e 67 2e 0a 2a 2f 0a  ore closing..*/.
d6dc0 73 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f  static int dotlo
d6dd0 63 6b 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f  ckClose(sqlite3_
d6de0 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69 6e  file *id) {.  in
d6df0 74 20 72 63 3b 0a 20 20 69 66 28 20 69 64 20 29  t rc;.  if( id )
d6e00 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a  {.    unixFile *
d6e10 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c  pFile = (unixFil
d6e20 65 2a 29 69 64 3b 0a 20 20 20 20 64 6f 74 6c 6f  e*)id;.    dotlo
d6e30 63 6b 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f  ckUnlock(id, NO_
d6e40 4c 4f 43 4b 29 3b 0a 20 20 20 20 73 71 6c 69 74  LOCK);.    sqlit
d6e50 65 33 5f 66 72 65 65 28 70 46 69 6c 65 2d 3e 6c  e3_free(pFile->l
d6e60 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3b 0a  ockingContext);.
d6e70 20 20 7d 0a 20 20 72 63 20 3d 20 63 6c 6f 73 65    }.  rc = close
d6e80 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a 20 20  UnixFile(id);.  
d6e90 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 2f 2a 2a  return rc;.}./**
d6ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6eb0 20 45 6e 64 20 6f 66 20 74 68 65 20 64 6f 74 2d   End of the dot-
d6ec0 66 69 6c 65 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d  file lock implem
d6ed0 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a  entation *******
d6ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a  ************.***
d6ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a  ***********/../*
d6f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
d6f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6fa0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
d6fb0 6c 6f 63 6b 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a  lock Locking ***
d6fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
d6fe0 0a 2a 2a 20 55 73 65 20 74 68 65 20 66 6c 6f 63  .** Use the floc
d6ff0 6b 28 29 20 73 79 73 74 65 6d 20 63 61 6c 6c 20  k() system call 
d7000 74 6f 20 64 6f 20 66 69 6c 65 20 6c 6f 63 6b 69  to do file locki
d7010 6e 67 2e 0a 2a 2a 0a 2a 2a 20 66 6c 6f 63 6b 28  ng..**.** flock(
d7020 29 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6c 69 6b  ) locking is lik
d7030 65 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 69  e dot-file locki
d7040 6e 67 20 69 6e 20 74 68 61 74 20 74 68 65 20 76  ng in that the v
d7050 61 72 69 6f 75 73 0a 2a 2a 20 66 69 6e 65 2d 67  arious.** fine-g
d7060 72 61 69 6e 20 6c 6f 63 6b 69 6e 67 20 6c 65 76  rain locking lev
d7070 65 6c 73 20 73 75 70 70 6f 72 74 65 64 20 62 79  els supported by
d7080 20 53 51 4c 69 74 65 20 61 72 65 20 63 6f 6c 6c   SQLite are coll
d7090 61 70 73 65 64 20 69 6e 74 6f 0a 2a 2a 20 61 20  apsed into.** a 
d70a0 73 69 6e 67 6c 65 20 65 78 63 6c 75 73 69 76 65  single exclusive
d70b0 20 6c 6f 63 6b 2e 20 20 49 6e 20 6f 74 68 65 72   lock.  In other
d70c0 20 77 6f 72 64 73 2c 20 53 48 41 52 45 44 2c 20   words, SHARED, 
d70d0 52 45 53 45 52 56 45 44 2c 20 61 6e 64 0a 2a 2a  RESERVED, and.**
d70e0 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 73 20 61   PENDING locks a
d70f0 72 65 20 74 68 65 20 73 61 6d 65 20 74 68 69 6e  re the same thin
d7100 67 20 61 73 20 61 6e 20 45 58 43 4c 55 53 49 56  g as an EXCLUSIV
d7110 45 20 6c 6f 63 6b 2e 20 20 53 51 4c 69 74 65 0a  E lock.  SQLite.
d7120 2a 2a 20 73 74 69 6c 6c 20 77 6f 72 6b 73 20 77  ** still works w
d7130 68 65 6e 20 79 6f 75 20 64 6f 20 74 68 69 73 2c  hen you do this,
d7140 20 62 75 74 20 63 6f 6e 63 75 72 72 65 6e 63 79   but concurrency
d7150 20 69 73 20 72 65 64 75 63 65 64 20 73 69 6e 63   is reduced sinc
d7160 65 0a 2a 2a 20 6f 6e 6c 79 20 61 20 73 69 6e 67  e.** only a sing
d7170 6c 65 20 70 72 6f 63 65 73 73 20 63 61 6e 20 62  le process can b
d7180 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 64 61  e reading the da
d7190 74 61 62 61 73 65 20 61 74 20 61 20 74 69 6d 65  tabase at a time
d71a0 2e 0a 2a 2a 0a 2a 2a 20 4f 6d 69 74 20 74 68 69  ..**.** Omit thi
d71b0 73 20 73 65 63 74 69 6f 6e 20 69 66 20 53 51 4c  s section if SQL
d71c0 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
d71d0 4e 47 5f 53 54 59 4c 45 20 69 73 20 74 75 72 6e  NG_STYLE is turn
d71e0 65 64 20 6f 66 66 20 6f 72 20 69 66 0a 2a 2a 20  ed off or if.** 
d71f0 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 56 58  compiling for VX
d7200 57 4f 52 4b 53 2e 0a 2a 2f 0a 23 69 66 20 53 51  WORKS..*/.#if SQ
d7210 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b  LITE_ENABLE_LOCK
d7220 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 21 4f 53  ING_STYLE && !OS
d7230 5f 56 58 57 4f 52 4b 53 0a 0a 2f 2a 0a 2a 2a 20  _VXWORKS../*.** 
d7240 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65  This routine che
d7250 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 20  cks if there is 
d7260 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20  a RESERVED lock 
d7270 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63  held on the spec
d7280 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79  ified.** file by
d7290 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68   this or any oth
d72a0 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73  er process. If s
d72b0 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65  uch a lock is he
d72c0 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74  ld, set *pResOut
d72d0 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72  .** to a non-zer
d72e0 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73  o value otherwis
d72f0 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65  e *pResOut is se
d7300 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20  t to zero.  The 
d7310 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20  return value.** 
d7320 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45  is set to SQLITE
d7330 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f  _OK unless an I/
d7340 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64  O error occurs d
d7350 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b  uring lock check
d7360 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ing..*/.static i
d7370 6e 74 20 66 6c 6f 63 6b 43 68 65 63 6b 52 65 73  nt flockCheckRes
d7380 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65  ervedLock(sqlite
d7390 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
d73a0 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e 74  *pResOut){.  int
d73b0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
d73c0 0a 20 20 69 6e 74 20 72 65 73 65 72 76 65 64 20  .  int reserved 
d73d0 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20  = 0;.  unixFile 
d73e0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
d73f0 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 20 53 69 6d  le*)id;.  .  Sim
d7400 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65  ulateIOError( re
d7410 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
d7420 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c  R_CHECKRESERVEDL
d7430 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 73 73  OCK; );.  .  ass
d7440 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20  ert( pFile );.  
d7450 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 61  .  /* Check if a
d7460 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 20   thread in this 
d7470 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 75  process holds su
d7480 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69  ch a lock */.  i
d7490 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  f( pFile->lockty
d74a0 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  pe>SHARED_LOCK )
d74b0 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64 20 3d  {.    reserved =
d74c0 20 31 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20   1;.  }.  .  /* 
d74d0 4f 74 68 65 72 77 69 73 65 20 73 65 65 20 69 66  Otherwise see if
d74e0 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63   some other proc
d74f0 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 20 2a 2f  ess holds it. */
d7500 0a 20 20 69 66 28 20 21 72 65 73 65 72 76 65 64  .  if( !reserved
d7510 20 29 7b 0a 20 20 20 20 2f 2a 20 61 74 74 65 6d   ){.    /* attem
d7520 70 74 20 74 6f 20 67 65 74 20 74 68 65 20 6c 6f  pt to get the lo
d7530 63 6b 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 72  ck */.    int lr
d7540 63 20 3d 20 66 6c 6f 63 6b 28 70 46 69 6c 65 2d  c = flock(pFile-
d7550 3e 68 2c 20 4c 4f 43 4b 5f 45 58 20 7c 20 4c 4f  >h, LOCK_EX | LO
d7560 43 4b 5f 4e 42 29 3b 0a 20 20 20 20 69 66 28 20  CK_NB);.    if( 
d7570 21 6c 72 63 20 29 7b 0a 20 20 20 20 20 20 2f 2a  !lrc ){.      /*
d7580 20 67 6f 74 20 74 68 65 20 6c 6f 63 6b 2c 20 75   got the lock, u
d7590 6e 6c 6f 63 6b 20 69 74 20 2a 2f 0a 20 20 20 20  nlock it */.    
d75a0 20 20 6c 72 63 20 3d 20 66 6c 6f 63 6b 28 70 46    lrc = flock(pF
d75b0 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 55 4e 29  ile->h, LOCK_UN)
d75c0 3b 0a 20 20 20 20 20 20 69 66 20 28 20 6c 72 63  ;.      if ( lrc
d75d0 20 29 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74   ) {.        int
d75e0 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b   tErrno = errno;
d75f0 0a 20 20 20 20 20 20 20 20 2f 2a 20 75 6e 6c 6f  .        /* unlo
d7600 63 6b 20 66 61 69 6c 65 64 20 77 69 74 68 20 61  ck failed with a
d7610 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20  n error */.     
d7620 20 20 20 6c 72 63 20 3d 20 73 71 6c 69 74 65 45     lrc = sqliteE
d7630 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72  rrorFromPosixErr
d7640 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54  or(tErrno, SQLIT
d7650 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b  E_IOERR_UNLOCK);
d7660 20 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 53   .        if( IS
d7670 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 29  _LOCK_ERROR(lrc)
d7680 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46   ){.          pF
d7690 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
d76a0 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20   tErrno;.       
d76b0 20 20 20 72 63 20 3d 20 6c 72 63 3b 0a 20 20 20     rc = lrc;.   
d76c0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
d76d0 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20     } else {.    
d76e0 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65    int tErrno = e
d76f0 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65 73 65  rrno;.      rese
d7700 72 76 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  rved = 1;.      
d7710 2f 2a 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20  /* someone else 
d7720 6d 69 67 68 74 20 68 61 76 65 20 69 74 20 72 65  might have it re
d7730 73 65 72 76 65 64 20 2a 2f 0a 20 20 20 20 20 20  served */.      
d7740 6c 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f  lrc = sqliteErro
d7750 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28  rFromPosixError(
d7760 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49  tErrno, SQLITE_I
d7770 4f 45 52 52 5f 4c 4f 43 4b 29 3b 20 0a 20 20 20  OERR_LOCK); .   
d7780 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45     if( IS_LOCK_E
d7790 52 52 4f 52 28 6c 72 63 29 20 29 7b 0a 20 20 20  RROR(lrc) ){.   
d77a0 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74       pFile->last
d77b0 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a  Errno = tErrno;.
d77c0 20 20 20 20 20 20 20 20 72 63 20 3d 20 6c 72 63          rc = lrc
d77d0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
d77e0 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 22    }.  OSTRACE4("
d77f0 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20  TEST WR-LOCK %d 
d7800 25 64 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  %d %d\n", pFile-
d7810 3e 68 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64  >h, rc, reserved
d7820 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  );..#ifdef SQLIT
d7830 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c  E_IGNORE_FLOCK_L
d7840 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 20 69 66 28  OCK_ERRORS.  if(
d7850 20 28 72 63 20 26 20 53 51 4c 49 54 45 5f 49 4f   (rc & SQLITE_IO
d7860 45 52 52 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49  ERR) == SQLITE_I
d7870 4f 45 52 52 20 29 7b 0a 20 20 20 20 72 63 20 3d  OERR ){.    rc =
d7880 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
d7890 72 65 73 65 72 76 65 64 3d 31 3b 0a 20 20 7d 0a  reserved=1;.  }.
d78a0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
d78b0 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f  _IGNORE_FLOCK_LO
d78c0 43 4b 5f 45 52 52 4f 52 53 20 2a 2f 0a 20 20 2a  CK_ERRORS */.  *
d78d0 70 52 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76  pResOut = reserv
d78e0 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  ed;.  return rc;
d78f0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74  .}../*.** Lock t
d7900 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65  he file with the
d7910 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20   lock specified 
d7920 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63  by parameter loc
d7930 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f  ktype - one.** o
d7940 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  f the following:
d7950 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53  .**.**     (1) S
d7960 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  HARED_LOCK.**   
d7970 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c    (2) RESERVED_L
d7980 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50  OCK.**     (3) P
d7990 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20  ENDING_LOCK.**  
d79a0 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45     (4) EXCLUSIVE
d79b0 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65  _LOCK.**.** Some
d79c0 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65  times when reque
d79d0 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73  sting one lock s
d79e0 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c  tate, additional
d79f0 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20   lock states.** 
d7a00 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20  are inserted in 
d7a10 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f  between.  The lo
d7a20 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c  cking might fail
d7a30 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c   on one of the l
d7a40 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69  ater.** transiti
d7a50 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20  ons leaving the 
d7a60 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65  lock state diffe
d7a70 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69  rent from what i
d7a80 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a  t started but.**
d7a90 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20   still short of 
d7aa0 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66  its goal.  The f
d7ab0 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73  ollowing chart s
d7ac0 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64  hows the allowed
d7ad0 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20  .** transitions 
d7ae0 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64  and the inserted
d7af0 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74   intermediate st
d7b00 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55  ates:.**.**    U
d7b10 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45  NLOCKED -> SHARE
d7b20 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d  D.**    SHARED -
d7b30 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20  > RESERVED.**   
d7b40 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44   SHARED -> (PEND
d7b50 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56  ING) -> EXCLUSIV
d7b60 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44  E.**    RESERVED
d7b70 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e   -> (PENDING) ->
d7b80 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20   EXCLUSIVE.**   
d7b90 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c   PENDING -> EXCL
d7ba0 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 66 6c 6f 63  USIVE.**.** floc
d7bb0 6b 28 29 20 6f 6e 6c 79 20 72 65 61 6c 6c 79 20  k() only really 
d7bc0 73 75 70 70 6f 72 74 20 45 58 43 4c 55 53 49 56  support EXCLUSIV
d7bd0 45 20 6c 6f 63 6b 73 2e 20 20 57 65 20 74 72 61  E locks.  We tra
d7be0 63 6b 20 69 6e 74 65 72 6d 65 64 69 61 74 65 0a  ck intermediate.
d7bf0 2a 2a 20 6c 6f 63 6b 20 73 74 61 74 65 73 20 69  ** lock states i
d7c00 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69  n the sqlite3_fi
d7c10 6c 65 20 73 74 72 75 63 74 75 72 65 2c 20 62 75  le structure, bu
d7c20 74 20 61 6c 6c 20 6c 6f 63 6b 73 20 53 48 41 52  t all locks SHAR
d7c30 45 44 20 6f 72 0a 2a 2a 20 61 62 6f 76 65 20 61  ED or.** above a
d7c40 72 65 20 72 65 61 6c 6c 79 20 45 58 43 4c 55 53  re really EXCLUS
d7c50 49 56 45 20 6c 6f 63 6b 73 20 61 6e 64 20 65 78  IVE locks and ex
d7c60 63 6c 75 64 65 20 61 6c 6c 20 6f 74 68 65 72 20  clude all other 
d7c70 70 72 6f 63 65 73 73 65 73 20 66 72 6f 6d 0a 2a  processes from.*
d7c80 2a 20 61 63 63 65 73 73 20 74 68 65 20 66 69 6c  * access the fil
d7c90 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  e..**.** This ro
d7ca0 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20  utine will only 
d7cb0 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e  increase a lock.
d7cc0 20 20 55 73 65 20 74 68 65 20 73 71 6c 69 74 65    Use the sqlite
d7cd0 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72  3OsUnlock().** r
d7ce0 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20  outine to lower 
d7cf0 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e  a locking level.
d7d00 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
d7d10 6c 6f 63 6b 4c 6f 63 6b 28 73 71 6c 69 74 65 33  lockLock(sqlite3
d7d20 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c  _file *id, int l
d7d30 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 69 6e 74  ocktype) {.  int
d7d40 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
d7d50 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69  .  unixFile *pFi
d7d60 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29  le = (unixFile*)
d7d70 69 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  id;..  assert( p
d7d80 46 69 6c 65 20 29 3b 0a 0a 20 20 2f 2a 20 69 66  File );..  /* if
d7d90 20 77 65 20 61 6c 72 65 61 64 79 20 68 61 76 65   we already have
d7da0 20 61 20 6c 6f 63 6b 2c 20 69 74 20 69 73 20 65   a lock, it is e
d7db0 78 63 6c 75 73 69 76 65 2e 20 20 0a 20 20 2a 2a  xclusive.  .  **
d7dc0 20 4a 75 73 74 20 61 64 6a 75 73 74 20 6c 65 76   Just adjust lev
d7dd0 65 6c 20 61 6e 64 20 70 75 6e 74 20 6f 6e 20 6f  el and punt on o
d7de0 75 74 74 61 20 68 65 72 65 2e 20 2a 2f 0a 20 20  utta here. */.  
d7df0 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  if (pFile->lockt
d7e00 79 70 65 20 3e 20 4e 4f 5f 4c 4f 43 4b 29 20 7b  ype > NO_LOCK) {
d7e10 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  .    pFile->lock
d7e20 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b  type = locktype;
d7e30 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
d7e40 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20  TE_OK;.  }.  .  
d7e50 2f 2a 20 67 72 61 62 20 61 6e 20 65 78 63 6c 75  /* grab an exclu
d7e60 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 0a  sive lock */.  .
d7e70 20 20 69 66 20 28 66 6c 6f 63 6b 28 70 46 69 6c    if (flock(pFil
d7e80 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 45 58 20 7c 20  e->h, LOCK_EX | 
d7e90 4c 4f 43 4b 5f 4e 42 29 29 20 7b 0a 20 20 20 20  LOCK_NB)) {.    
d7ea0 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72  int tErrno = err
d7eb0 6e 6f 3b 0a 20 20 20 20 2f 2a 20 64 69 64 6e 27  no;.    /* didn'
d7ec0 74 20 67 65 74 2c 20 6d 75 73 74 20 62 65 20 62  t get, must be b
d7ed0 75 73 79 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  usy */.    rc = 
d7ee0 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50  sqliteErrorFromP
d7ef0 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f  osixError(tErrno
d7f00 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c  , SQLITE_IOERR_L
d7f10 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 49 53  OCK);.    if( IS
d7f20 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20  _LOCK_ERROR(rc) 
d7f30 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  ){.      pFile->
d7f40 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72  lastErrno = tErr
d7f50 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 65 6c  no;.    }.  } el
d7f60 73 65 20 7b 0a 20 20 20 20 2f 2a 20 67 6f 74 20  se {.    /* got 
d7f70 69 74 2c 20 73 65 74 20 74 68 65 20 74 79 70 65  it, set the type
d7f80 20 61 6e 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a   and return ok *
d7f90 2f 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63  /.    pFile->loc
d7fa0 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65  ktype = locktype
d7fb0 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 34  ;.  }.  OSTRACE4
d7fc0 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20  ("LOCK    %d %s 
d7fd0 25 73 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %s\n", pFile->h,
d7fe0 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f   locktypeName(lo
d7ff0 63 6b 74 79 70 65 29 2c 20 0a 20 20 20 20 20 20  cktype), .      
d8000 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f       rc==SQLITE_
d8010 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69  OK ? "ok" : "fai
d8020 6c 65 64 22 29 3b 0a 23 69 66 64 65 66 20 53 51  led");.#ifdef SQ
d8030 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43  LITE_IGNORE_FLOC
d8040 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 20  K_LOCK_ERRORS.  
d8050 69 66 28 20 28 72 63 20 26 20 53 51 4c 49 54 45  if( (rc & SQLITE
d8060 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c 49 54  _IOERR) == SQLIT
d8070 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 20 20 72  E_IOERR ){.    r
d8080 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  c = SQLITE_BUSY;
d8090 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  .  }.#endif /* S
d80a0 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f  QLITE_IGNORE_FLO
d80b0 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 2a  CK_LOCK_ERRORS *
d80c0 2f 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  /.  return rc;.}
d80d0 0a 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74  .../*.** Lower t
d80e0 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  he locking level
d80f0 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70   on file descrip
d8100 74 6f 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63  tor pFile to loc
d8110 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65  ktype.  locktype
d8120 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68  .** must be eith
d8130 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48  er NO_LOCK or SH
d8140 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a  ARED_LOCK..**.**
d8150 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20   If the locking 
d8160 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c  level of the fil
d8170 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20  e descriptor is 
d8180 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65  already at or be
d8190 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65  low.** the reque
d81a0 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76  sted locking lev
d81b0 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  el, this routine
d81c0 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a   is a no-op..*/.
d81d0 73 74 61 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b  static int flock
d81e0 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  Unlock(sqlite3_f
d81f0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63  ile *id, int loc
d8200 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 46  ktype) {.  unixF
d8210 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e  ile *pFile = (un
d8220 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 0a 20  ixFile*)id;.  . 
d8230 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29   assert( pFile )
d8240 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 55 4e  ;.  OSTRACE5("UN
d8250 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73 20  LOCK  %d %d was 
d8260 25 64 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46  %d pid=%d\n", pF
d8270 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65  ile->h, locktype
d8280 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 46 69  ,.           pFi
d8290 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 65  le->locktype, ge
d82a0 74 70 69 64 28 29 29 3b 0a 20 20 61 73 73 65 72  tpid());.  asser
d82b0 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41  t( locktype<=SHA
d82c0 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20  RED_LOCK );.  . 
d82d0 20 2f 2a 20 6e 6f 2d 6f 70 20 69 66 20 70 6f 73   /* no-op if pos
d82e0 73 69 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 70  sible */.  if( p
d82f0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d  File->locktype==
d8300 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20  locktype ){.    
d8310 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
d8320 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 73 68  ;.  }.  .  /* sh
d8330 61 72 65 64 20 63 61 6e 20 6a 75 73 74 20 62 65  ared can just be
d8340 20 73 65 74 20 62 65 63 61 75 73 65 20 77 65 20   set because we 
d8350 61 6c 77 61 79 73 20 68 61 76 65 20 61 6e 20 65  always have an e
d8360 78 63 6c 75 73 69 76 65 20 2a 2f 0a 20 20 69 66  xclusive */.  if
d8370 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52   (locktype==SHAR
d8380 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70  ED_LOCK) {.    p
d8390 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  File->locktype =
d83a0 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 72   locktype;.    r
d83b0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
d83c0 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2c  .  }.  .  /* no,
d83d0 20 72 65 61 6c 6c 79 2c 20 75 6e 6c 6f 63 6b 2e   really, unlock.
d83e0 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 66   */.  int rc = f
d83f0 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c  lock(pFile->h, L
d8400 4f 43 4b 5f 55 4e 29 3b 0a 20 20 69 66 20 28 72  OCK_UN);.  if (r
d8410 63 29 20 7b 0a 20 20 20 20 69 6e 74 20 72 2c 20  c) {.    int r, 
d8420 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a  tErrno = errno;.
d8430 20 20 20 20 72 20 3d 20 73 71 6c 69 74 65 45 72      r = sqliteEr
d8440 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f  rorFromPosixErro
d8450 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45  r(tErrno, SQLITE
d8460 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a  _IOERR_UNLOCK);.
d8470 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f      if( IS_LOCK_
d8480 45 52 52 4f 52 28 72 29 20 29 7b 0a 20 20 20 20  ERROR(r) ){.    
d8490 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
d84a0 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20  no = tErrno;.   
d84b0 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45   }.#ifdef SQLITE
d84c0 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f  _IGNORE_FLOCK_LO
d84d0 43 4b 5f 45 52 52 4f 52 53 0a 20 20 20 20 69 66  CK_ERRORS.    if
d84e0 28 20 28 72 20 26 20 53 51 4c 49 54 45 5f 49 4f  ( (r & SQLITE_IO
d84f0 45 52 52 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49  ERR) == SQLITE_I
d8500 4f 45 52 52 20 29 7b 0a 20 20 20 20 20 20 72 20  OERR ){.      r 
d8510 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20  = SQLITE_BUSY;. 
d8520 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53     }.#endif /* S
d8530 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f  QLITE_IGNORE_FLO
d8540 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 2a  CK_LOCK_ERRORS *
d8550 2f 0a 20 20 20 20 0a 20 20 20 20 72 65 74 75 72  /.    .    retur
d8560 6e 20 72 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a  n r;.  } else {.
d8570 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74      pFile->lockt
d8580 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20  ype = NO_LOCK;. 
d8590 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
d85a0 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  _OK;.  }.}../*.*
d85b0 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a  * Close a file..
d85c0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c  */.static int fl
d85d0 6f 63 6b 43 6c 6f 73 65 28 73 71 6c 69 74 65 33  ockClose(sqlite3
d85e0 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69  _file *id) {.  i
d85f0 66 28 20 69 64 20 29 7b 0a 20 20 20 20 66 6c 6f  f( id ){.    flo
d8600 63 6b 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f  ckUnlock(id, NO_
d8610 4c 4f 43 4b 29 3b 0a 20 20 7d 0a 20 20 72 65 74  LOCK);.  }.  ret
d8620 75 72 6e 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c  urn closeUnixFil
d8630 65 28 69 64 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66  e(id);.}..#endif
d8640 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   /* SQLITE_ENABL
d8650 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
d8660 26 26 20 21 4f 53 5f 56 58 57 4f 52 4b 20 2a 2f  && !OS_VXWORK */
d8670 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
d8680 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68  ****** End of th
d8690 65 20 66 6c 6f 63 6b 20 6c 6f 63 6b 20 69 6d 70  e flock lock imp
d86a0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a  lementation ****
d86b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d86c0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
d86d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d86e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d86f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8710 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
d8720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8760 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
d8770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
d8780 6e 20 4e 61 6d 65 64 20 53 65 6d 61 70 68 6f 72  n Named Semaphor
d8790 65 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a  e Locking ******
d87a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d87b0 2a 2a 0a 2a 2a 0a 2a 2a 20 4e 61 6d 65 64 20 73  **.**.** Named s
d87c0 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e 67  emaphore locking
d87d0 20 69 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74   is only support
d87e0 65 64 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a  ed on VxWorks..*
d87f0 2a 0a 2a 2a 20 53 65 6d 61 70 68 6f 72 65 20 6c  *.** Semaphore l
d8800 6f 63 6b 69 6e 67 20 69 73 20 6c 69 6b 65 20 64  ocking is like d
d8810 6f 74 2d 6c 6f 63 6b 20 61 6e 64 20 66 6c 6f 63  ot-lock and floc
d8820 6b 20 69 6e 20 74 68 61 74 20 69 74 20 72 65 61  k in that it rea
d8830 6c 6c 79 20 6f 6e 6c 79 0a 2a 2a 20 73 75 70 70  lly only.** supp
d8840 6f 72 74 73 20 45 58 43 4c 55 53 49 56 45 20 6c  orts EXCLUSIVE l
d8850 6f 63 6b 69 6e 67 2e 20 20 4f 6e 6c 79 20 61 20  ocking.  Only a 
d8860 73 69 6e 67 6c 65 20 70 72 6f 63 65 73 73 20 63  single process c
d8870 61 6e 20 72 65 61 64 20 6f 72 20 77 72 69 74 65  an read or write
d8880 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65  .** the database
d8890 20 66 69 6c 65 20 61 74 20 61 20 74 69 6d 65 2e   file at a time.
d88a0 20 20 54 68 69 73 20 72 65 64 75 63 65 73 20 70    This reduces p
d88b0 6f 74 65 6e 74 69 61 6c 20 63 6f 6e 63 75 72 72  otential concurr
d88c0 65 6e 63 79 2c 20 62 75 74 0a 2a 2a 20 6d 61 6b  ency, but.** mak
d88d0 65 73 20 74 68 65 20 6c 6f 63 6b 20 69 6d 70 6c  es the lock impl
d88e0 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 75 63 68 20  ementation much 
d88f0 65 61 73 69 65 72 2e 0a 2a 2f 0a 23 69 66 20 4f  easier..*/.#if O
d8900 53 5f 56 58 57 4f 52 4b 53 0a 0a 2f 2a 0a 2a 2a  S_VXWORKS../*.**
d8910 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68   This routine ch
d8920 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 73  ecks if there is
d8930 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b   a RESERVED lock
d8940 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65   held on the spe
d8950 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62  cified.** file b
d8960 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74  y this or any ot
d8970 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20  her process. If 
d8980 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68  such a lock is h
d8990 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 4f 75  eld, set *pResOu
d89a0 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65  t.** to a non-ze
d89b0 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77 69  ro value otherwi
d89c0 73 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73  se *pResOut is s
d89d0 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65  et to zero.  The
d89e0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a   return value.**
d89f0 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54   is set to SQLIT
d8a00 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49  E_OK unless an I
d8a10 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  /O error occurs 
d8a20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63  during lock chec
d8a30 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  king..*/.static 
d8a40 69 6e 74 20 73 65 6d 43 68 65 63 6b 52 65 73 65  int semCheckRese
d8a50 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33  rvedLock(sqlite3
d8a60 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a  _file *id, int *
d8a70 70 52 65 73 4f 75 74 29 20 7b 0a 20 20 69 6e 74  pResOut) {.  int
d8a80 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
d8a90 0a 20 20 69 6e 74 20 72 65 73 65 72 76 65 64 20  .  int reserved 
d8aa0 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20  = 0;.  unixFile 
d8ab0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
d8ac0 6c 65 2a 29 69 64 3b 0a 0a 20 20 53 69 6d 75 6c  le*)id;..  Simul
d8ad0 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75  ateIOError( retu
d8ae0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
d8af0 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43  CHECKRESERVEDLOC
d8b00 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 73 73 65 72  K; );.  .  asser
d8b10 74 28 20 70 46 69 6c 65 20 29 3b 0a 0a 20 20 2f  t( pFile );..  /
d8b20 2a 20 43 68 65 63 6b 20 69 66 20 61 20 74 68 72  * Check if a thr
d8b30 65 61 64 20 69 6e 20 74 68 69 73 20 70 72 6f 63  ead in this proc
d8b40 65 73 73 20 68 6f 6c 64 73 20 73 75 63 68 20 61  ess holds such a
d8b50 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70   lock */.  if( p
d8b60 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53  File->locktype>S
d8b70 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20  HARED_LOCK ){.  
d8b80 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a    reserved = 1;.
d8b90 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 4f 74 68 65    }.  .  /* Othe
d8ba0 72 77 69 73 65 20 73 65 65 20 69 66 20 73 6f 6d  rwise see if som
d8bb0 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20  e other process 
d8bc0 68 6f 6c 64 73 20 69 74 2e 20 2a 2f 0a 20 20 69  holds it. */.  i
d8bd0 66 28 20 21 72 65 73 65 72 76 65 64 20 29 7b 0a  f( !reserved ){.
d8be0 20 20 20 20 73 65 6d 5f 74 20 2a 70 53 65 6d 20      sem_t *pSem 
d8bf0 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e  = pFile->pOpen->
d8c00 70 53 65 6d 3b 0a 20 20 20 20 73 74 72 75 63 74  pSem;.    struct
d8c10 20 73 74 61 74 20 73 74 61 74 42 75 66 3b 0a 0a   stat statBuf;..
d8c20 20 20 20 20 69 66 28 20 73 65 6d 5f 74 72 79 77      if( sem_tryw
d8c30 61 69 74 28 70 53 65 6d 29 3d 3d 2d 31 20 29 7b  ait(pSem)==-1 ){
d8c40 0a 20 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e  .      int tErrn
d8c50 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20  o = errno;.     
d8c60 20 69 66 28 20 45 41 47 41 49 4e 20 21 3d 20 74   if( EAGAIN != t
d8c70 45 72 72 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20  Errno ){.       
d8c80 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f   rc = sqliteErro
d8c90 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28  rFromPosixError(
d8ca0 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49  tErrno, SQLITE_I
d8cb0 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56  OERR_CHECKRESERV
d8cc0 45 44 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20  EDLOCK);.       
d8cd0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
d8ce0 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20  o = tErrno;.    
d8cf0 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20    } else {.     
d8d00 20 20 20 2f 2a 20 73 6f 6d 65 6f 6e 65 20 65 6c     /* someone el
d8d10 73 65 20 68 61 73 20 74 68 65 20 6c 6f 63 6b 20  se has the lock 
d8d20 77 68 65 6e 20 77 65 20 61 72 65 20 69 6e 20 4e  when we are in N
d8d30 4f 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 20 20 20 20  O_LOCK */.      
d8d40 20 20 72 65 73 65 72 76 65 64 20 3d 20 28 70 46    reserved = (pF
d8d50 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3c 20  ile->locktype < 
d8d60 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20  SHARED_LOCK);.  
d8d70 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b      }.    }else{
d8d80 0a 20 20 20 20 20 20 2f 2a 20 77 65 20 63 6f 75  .      /* we cou
d8d90 6c 64 20 68 61 76 65 20 69 74 20 69 66 20 77 65  ld have it if we
d8da0 20 77 61 6e 74 20 69 74 20 2a 2f 0a 20 20 20 20   want it */.    
d8db0 20 20 73 65 6d 5f 70 6f 73 74 28 70 53 65 6d 29    sem_post(pSem)
d8dc0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 4f 53  ;.    }.  }.  OS
d8dd0 54 52 41 43 45 34 28 22 54 45 53 54 20 57 52 2d  TRACE4("TEST WR-
d8de0 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 5c 6e 22  LOCK %d %d %d\n"
d8df0 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20  , pFile->h, rc, 
d8e00 72 65 73 65 72 76 65 64 29 3b 0a 0a 20 20 2a 70  reserved);..  *p
d8e10 52 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76 65  ResOut = reserve
d8e20 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  d;.  return rc;.
d8e30 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68  }../*.** Lock th
d8e40 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20  e file with the 
d8e50 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62  lock specified b
d8e60 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b  y parameter lock
d8e70 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66  type - one.** of
d8e80 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
d8e90 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48  **.**     (1) SH
d8ea0 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20  ARED_LOCK.**    
d8eb0 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f   (2) RESERVED_LO
d8ec0 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45  CK.**     (3) PE
d8ed0 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  NDING_LOCK.**   
d8ee0 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f    (4) EXCLUSIVE_
d8ef0 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74  LOCK.**.** Somet
d8f00 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73  imes when reques
d8f10 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74  ting one lock st
d8f20 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20  ate, additional 
d8f30 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61  lock states.** a
d8f40 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62  re inserted in b
d8f50 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63  etween.  The loc
d8f60 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20  king might fail 
d8f70 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61  on one of the la
d8f80 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f  ter.** transitio
d8f90 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c  ns leaving the l
d8fa0 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72  ock state differ
d8fb0 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74  ent from what it
d8fc0 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20   started but.** 
d8fd0 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69  still short of i
d8fe0 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f  ts goal.  The fo
d8ff0 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68  llowing chart sh
d9000 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a  ows the allowed.
d9010 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61  ** transitions a
d9020 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20  nd the inserted 
d9030 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61  intermediate sta
d9040 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e  tes:.**.**    UN
d9050 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44  LOCKED -> SHARED
d9060 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e  .**    SHARED ->
d9070 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20   RESERVED.**    
d9080 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49  SHARED -> (PENDI
d9090 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45  NG) -> EXCLUSIVE
d90a0 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20  .**    RESERVED 
d90b0 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20  -> (PENDING) -> 
d90c0 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20  EXCLUSIVE.**    
d90d0 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55  PENDING -> EXCLU
d90e0 53 49 56 45 0a 2a 2a 0a 2a 2a 20 53 65 6d 61 70  SIVE.**.** Semap
d90f0 68 6f 72 65 20 6c 6f 63 6b 73 20 6f 6e 6c 79 20  hore locks only 
d9100 72 65 61 6c 6c 79 20 73 75 70 70 6f 72 74 20 45  really support E
d9110 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73 2e 20  XCLUSIVE locks. 
d9120 20 57 65 20 74 72 61 63 6b 20 69 6e 74 65 72 6d   We track interm
d9130 65 64 69 61 74 65 0a 2a 2a 20 6c 6f 63 6b 20 73  ediate.** lock s
d9140 74 61 74 65 73 20 69 6e 20 74 68 65 20 73 71 6c  tates in the sql
d9150 69 74 65 33 5f 66 69 6c 65 20 73 74 72 75 63 74  ite3_file struct
d9160 75 72 65 2c 20 62 75 74 20 61 6c 6c 20 6c 6f 63  ure, but all loc
d9170 6b 73 20 53 48 41 52 45 44 20 6f 72 0a 2a 2a 20  ks SHARED or.** 
d9180 61 62 6f 76 65 20 61 72 65 20 72 65 61 6c 6c 79  above are really
d9190 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73   EXCLUSIVE locks
d91a0 20 61 6e 64 20 65 78 63 6c 75 64 65 20 61 6c 6c   and exclude all
d91b0 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73   other processes
d91c0 20 66 72 6f 6d 0a 2a 2a 20 61 63 63 65 73 73 20   from.** access 
d91d0 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  the file..**.** 
d91e0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c  This routine wil
d91f0 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20  l only increase 
d9200 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 65  a lock.  Use the
d9210 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b   sqlite3OsUnlock
d9220 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f  ().** routine to
d9230 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67   lower a locking
d9240 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69   level..*/.stati
d9250 63 20 69 6e 74 20 73 65 6d 4c 6f 63 6b 28 73 71  c int semLock(sq
d9260 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
d9270 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a  int locktype) {.
d9280 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
d9290 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
d92a0 64 3b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 20 73  d;.  int fd;.  s
d92b0 65 6d 5f 74 20 2a 70 53 65 6d 20 3d 20 70 46 69  em_t *pSem = pFi
d92c0 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 3b  le->pOpen->pSem;
d92d0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
d92e0 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 69 66 20  TE_OK;..  /* if 
d92f0 77 65 20 61 6c 72 65 61 64 79 20 68 61 76 65 20  we already have 
d9300 61 20 6c 6f 63 6b 2c 20 69 74 20 69 73 20 65 78  a lock, it is ex
d9310 63 6c 75 73 69 76 65 2e 20 20 0a 20 20 2a 2a 20  clusive.  .  ** 
d9320 4a 75 73 74 20 61 64 6a 75 73 74 20 6c 65 76 65  Just adjust leve
d9330 6c 20 61 6e 64 20 70 75 6e 74 20 6f 6e 20 6f 75  l and punt on ou
d9340 74 74 61 20 68 65 72 65 2e 20 2a 2f 0a 20 20 69  tta here. */.  i
d9350 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  f (pFile->lockty
d9360 70 65 20 3e 20 4e 4f 5f 4c 4f 43 4b 29 20 7b 0a  pe > NO_LOCK) {.
d9370 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74      pFile->lockt
d9380 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a  ype = locktype;.
d9390 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
d93a0 4f 4b 3b 0a 20 20 20 20 67 6f 74 6f 20 73 65 6d  OK;.    goto sem
d93b0 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 20  _end_lock;.  }. 
d93c0 20 0a 20 20 2f 2a 20 6c 6f 63 6b 20 73 65 6d 61   .  /* lock sema
d93d0 70 68 6f 72 65 20 6e 6f 77 20 62 75 74 20 62 61  phore now but ba
d93e0 69 6c 20 6f 75 74 20 77 68 65 6e 20 61 6c 72 65  il out when alre
d93f0 61 64 79 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20  ady locked. */. 
d9400 20 69 66 28 20 73 65 6d 5f 74 72 79 77 61 69 74   if( sem_trywait
d9410 28 70 53 65 6d 29 3d 3d 2d 31 20 29 7b 0a 20 20  (pSem)==-1 ){.  
d9420 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55    rc = SQLITE_BU
d9430 53 59 3b 0a 20 20 20 20 67 6f 74 6f 20 73 65 6d  SY;.    goto sem
d9440 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a  _end_lock;.  }..
d9450 20 20 2f 2a 20 67 6f 74 20 69 74 2c 20 73 65 74    /* got it, set
d9460 20 74 68 65 20 74 79 70 65 20 61 6e 64 20 72 65   the type and re
d9470 74 75 72 6e 20 6f 6b 20 2a 2f 0a 20 20 70 46 69  turn ok */.  pFi
d9480 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c  le->locktype = l
d9490 6f 63 6b 74 79 70 65 3b 0a 0a 20 73 65 6d 5f 65  ocktype;.. sem_e
d94a0 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 72 65 74 75 72  nd_lock:.  retur
d94b0 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c  n rc;.}../*.** L
d94c0 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67  ower the locking
d94d0 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64   level on file d
d94e0 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20  escriptor pFile 
d94f0 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f  to locktype.  lo
d9500 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62  cktype.** must b
d9510 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b  e either NO_LOCK
d9520 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e   or SHARED_LOCK.
d9530 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f  .**.** If the lo
d9540 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74  cking level of t
d9550 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
d9560 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74  or is already at
d9570 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65   or below.** the
d9580 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69   requested locki
d9590 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72  ng level, this r
d95a0 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f  outine is a no-o
d95b0 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  p..*/.static int
d95c0 20 73 65 6d 55 6e 6c 6f 63 6b 28 73 71 6c 69 74   semUnlock(sqlit
d95d0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
d95e0 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75   locktype) {.  u
d95f0 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  nixFile *pFile =
d9600 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a   (unixFile*)id;.
d9610 20 20 73 65 6d 5f 74 20 2a 70 53 65 6d 20 3d 20    sem_t *pSem = 
d9620 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 70 53  pFile->pOpen->pS
d9630 65 6d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  em;..  assert( p
d9640 46 69 6c 65 20 29 3b 0a 20 20 61 73 73 65 72 74  File );.  assert
d9650 28 20 70 53 65 6d 20 29 3b 0a 20 20 4f 53 54 52  ( pSem );.  OSTR
d9660 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64  ACE5("UNLOCK  %d
d9670 20 25 64 20 77 61 73 20 25 64 20 70 69 64 3d 25   %d was %d pid=%
d9680 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
d9690 6c 6f 63 6b 74 79 70 65 2c 0a 09 20 20 20 70 46  locktype,..   pF
d96a0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 67  ile->locktype, g
d96b0 65 74 70 69 64 28 29 29 3b 0a 20 20 61 73 73 65  etpid());.  asse
d96c0 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48  rt( locktype<=SH
d96d0 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a  ARED_LOCK );.  .
d96e0 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69 66 20 70 6f    /* no-op if po
d96f0 73 73 69 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20  ssible */.  if( 
d9700 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pFile->locktype=
d9710 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20  =locktype ){.   
d9720 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
d9730 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 73  K;.  }.  .  /* s
d9740 68 61 72 65 64 20 63 61 6e 20 6a 75 73 74 20 62  hared can just b
d9750 65 20 73 65 74 20 62 65 63 61 75 73 65 20 77 65  e set because we
d9760 20 61 6c 77 61 79 73 20 68 61 76 65 20 61 6e 20   always have an 
d9770 65 78 63 6c 75 73 69 76 65 20 2a 2f 0a 20 20 69  exclusive */.  i
d9780 66 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  f (locktype==SHA
d9790 52 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20  RED_LOCK) {.    
d97a0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
d97b0 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20  = locktype;.    
d97c0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
d97d0 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 6e 6f  ;.  }.  .  /* no
d97e0 2c 20 72 65 61 6c 6c 79 20 75 6e 6c 6f 63 6b 2e  , really unlock.
d97f0 20 2a 2f 0a 20 20 69 66 20 28 20 73 65 6d 5f 70   */.  if ( sem_p
d9800 6f 73 74 28 70 53 65 6d 29 3d 3d 2d 31 20 29 20  ost(pSem)==-1 ) 
d9810 7b 0a 20 20 20 20 69 6e 74 20 72 63 2c 20 74 45  {.    int rc, tE
d9820 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  rrno = errno;.  
d9830 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72    rc = sqliteErr
d9840 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72  orFromPosixError
d9850 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f  (tErrno, SQLITE_
d9860 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20  IOERR_UNLOCK);. 
d9870 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45     if( IS_LOCK_E
d9880 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20  RROR(rc) ){.    
d9890 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
d98a0 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20  no = tErrno;.   
d98b0 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63   }.    return rc
d98c0 3b 20 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e  ; .  }.  pFile->
d98d0 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f  locktype = NO_LO
d98e0 43 4b 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  CK;.  return SQL
d98f0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 20 2a  ITE_OK;.}../*. *
d9900 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a  * Close a file..
d9910 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73   */.static int s
d9920 65 6d 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f  emClose(sqlite3_
d9930 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69 66  file *id) {.  if
d9940 28 20 69 64 20 29 7b 0a 20 20 20 20 75 6e 69 78  ( id ){.    unix
d9950 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
d9960 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20  nixFile*)id;.   
d9970 20 73 65 6d 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e   semUnlock(id, N
d9980 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 61 73 73  O_LOCK);.    ass
d9990 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20  ert( pFile );.  
d99a0 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78    unixEnterMutex
d99b0 28 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 4c  ();.    releaseL
d99c0 6f 63 6b 49 6e 66 6f 28 70 46 69 6c 65 2d 3e 70  ockInfo(pFile->p
d99d0 4c 6f 63 6b 29 3b 0a 20 20 20 20 72 65 6c 65 61  Lock);.    relea
d99e0 73 65 4f 70 65 6e 43 6e 74 28 70 46 69 6c 65 2d  seOpenCnt(pFile-
d99f0 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 75 6e 69  >pOpen);.    uni
d9a00 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20  xLeaveMutex();. 
d9a10 20 20 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65     closeUnixFile
d9a20 28 69 64 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  (id);.  }.  retu
d9a30 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
d9a40 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 58  .#endif /* OS_VX
d9a50 57 4f 52 4b 53 20 2a 2f 0a 2f 2a 0a 2a 2a 20 4e  WORKS */./*.** N
d9a60 61 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c  amed semaphore l
d9a70 6f 63 6b 69 6e 67 20 69 73 20 6f 6e 6c 79 20 61  ocking is only a
d9a80 76 61 69 6c 61 62 6c 65 20 6f 6e 20 56 78 57 6f  vailable on VxWo
d9a90 72 6b 73 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  rks..**.********
d9aa0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74  ******* End of t
d9ab0 68 65 20 6e 61 6d 65 64 20 73 65 6d 61 70 68 6f  he named semapho
d9ac0 72 65 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e  re lock implemen
d9ad0 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  tation *********
d9ae0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  *******.********
d9af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9b30 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a  ******/.../*****
d9b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a  *********.******
d9b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9ba0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 41 46 50 20  ***** Begin AFP 
d9bb0 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a  Locking ********
d9bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
d9be0 41 46 50 20 69 73 20 74 68 65 20 41 70 70 6c 65  AFP is the Apple
d9bf0 20 46 69 6c 69 6e 67 20 50 72 6f 74 6f 63 6f 6c   Filing Protocol
d9c00 2e 20 20 41 46 50 20 69 73 20 61 20 6e 65 74 77  .  AFP is a netw
d9c10 6f 72 6b 20 66 69 6c 65 73 79 73 74 65 6d 20 66  ork filesystem f
d9c20 6f 75 6e 64 0a 2a 2a 20 6f 6e 20 41 70 70 6c 65  ound.** on Apple
d9c30 20 4d 61 63 69 6e 74 6f 73 68 20 63 6f 6d 70 75   Macintosh compu
d9c40 74 65 72 73 20 2d 20 62 6f 74 68 20 4f 53 39 20  ters - both OS9 
d9c50 61 6e 64 20 4f 53 58 2e 0a 2a 2a 0a 2a 2a 20 54  and OSX..**.** T
d9c60 68 69 72 64 2d 70 61 72 74 79 20 69 6d 70 6c 65  hird-party imple
d9c70 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 41 46  mentations of AF
d9c80 50 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2e  P are available.
d9c90 20 20 42 75 74 20 74 68 69 73 20 63 6f 64 65 20    But this code 
d9ca0 68 65 72 65 0a 2a 2a 20 6f 6e 6c 79 20 77 6f 72  here.** only wor
d9cb0 6b 73 20 6f 6e 20 4f 53 58 2e 0a 2a 2f 0a 0a 23  ks on OSX..*/..#
d9cc0 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50  if defined(__APP
d9cd0 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f  LE__) && SQLITE_
d9ce0 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
d9cf0 54 59 4c 45 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61  TYLE./*.** The a
d9d00 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  fpLockingContext
d9d10 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61   structure conta
d9d20 69 6e 73 20 61 6c 6c 20 61 66 70 20 6c 6f 63 6b  ins all afp lock
d9d30 20 73 70 65 63 69 66 69 63 20 73 74 61 74 65 0a   specific state.
d9d40 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
d9d50 74 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74  t afpLockingCont
d9d60 65 78 74 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f  ext afpLockingCo
d9d70 6e 74 65 78 74 3b 0a 73 74 72 75 63 74 20 61 66  ntext;.struct af
d9d80 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  pLockingContext 
d9d90 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e  {.  unsigned lon
d9da0 67 20 6c 6f 6e 67 20 73 68 61 72 65 64 42 79 74  g long sharedByt
d9db0 65 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  e;.  const char 
d9dc0 2a 64 62 50 61 74 68 3b 20 20 20 20 20 20 20 20  *dbPath;        
d9dd0 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
d9de0 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 2a 2f  the open file */
d9df0 0a 7d 3b 0a 0a 73 74 72 75 63 74 20 42 79 74 65  .};..struct Byte
d9e00 52 61 6e 67 65 4c 6f 63 6b 50 42 32 0a 7b 0a 20  RangeLockPB2.{. 
d9e10 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c   unsigned long l
d9e20 6f 6e 67 20 6f 66 66 73 65 74 3b 20 20 20 20 20  ong offset;     
d9e30 20 20 20 2f 2a 20 6f 66 66 73 65 74 20 74 6f 20     /* offset to 
d9e40 66 69 72 73 74 20 62 79 74 65 20 74 6f 20 6c 6f  first byte to lo
d9e50 63 6b 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  ck */.  unsigned
d9e60 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6c 65 6e 67 74   long long lengt
d9e70 68 3b 20 20 20 20 20 20 20 20 2f 2a 20 6e 62 72  h;        /* nbr
d9e80 20 6f 66 20 62 79 74 65 73 20 74 6f 20 6c 6f 63   of bytes to loc
d9e90 6b 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  k */.  unsigned 
d9ea0 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 52 61 6e  long long retRan
d9eb0 67 65 53 74 61 72 74 3b 20 2f 2a 20 6e 62 72 20  geStart; /* nbr 
d9ec0 6f 66 20 31 73 74 20 62 79 74 65 20 6c 6f 63 6b  of 1st byte lock
d9ed0 65 64 20 69 66 20 73 75 63 63 65 73 73 66 75 6c  ed if successful
d9ee0 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   */.  unsigned c
d9ef0 68 61 72 20 75 6e 4c 6f 63 6b 46 6c 61 67 3b 20  har unLockFlag; 
d9f00 20 20 20 20 20 20 20 20 2f 2a 20 31 20 3d 20 75          /* 1 = u
d9f10 6e 6c 6f 63 6b 2c 20 30 20 3d 20 6c 6f 63 6b 20  nlock, 0 = lock 
d9f20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  */.  unsigned ch
d9f30 61 72 20 73 74 61 72 74 45 6e 64 46 6c 61 67 3b  ar startEndFlag;
d9f40 20 20 20 20 20 20 20 2f 2a 20 31 3d 72 65 6c 20         /* 1=rel 
d9f50 74 6f 20 65 6e 64 20 6f 66 20 66 6f 72 6b 2c 20  to end of fork, 
d9f60 30 3d 72 65 6c 20 74 6f 20 73 74 61 72 74 20 2a  0=rel to start *
d9f70 2f 0a 20 20 69 6e 74 20 66 64 3b 20 20 20 20 20  /.  int fd;     
d9f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9f90 20 20 20 20 20 20 2f 2a 20 66 69 6c 65 20 64 65        /* file de
d9fa0 73 63 20 74 6f 20 61 73 73 6f 63 20 74 68 69 73  sc to assoc this
d9fb0 20 6c 6f 63 6b 20 77 69 74 68 20 2a 2f 0a 7d 3b   lock with */.};
d9fc0 0a 0a 23 64 65 66 69 6e 65 20 61 66 70 66 73 42  ..#define afpfsB
d9fd0 79 74 65 52 61 6e 67 65 4c 6f 63 6b 32 46 53 43  yteRangeLock2FSC
d9fe0 54 4c 20 20 20 20 20 20 20 20 5f 49 4f 57 52 28  TL        _IOWR(
d9ff0 27 7a 27 2c 20 32 33 2c 20 73 74 72 75 63 74 20  'z', 23, struct 
da000 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32  ByteRangeLockPB2
da010 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73  )../*.** This is
da020 20 61 20 75 74 69 6c 69 74 79 20 66 6f 72 20 73   a utility for s
da030 65 74 74 69 6e 67 20 6f 72 20 63 6c 65 61 72 69  etting or cleari
da040 6e 67 20 61 20 62 69 74 2d 72 61 6e 67 65 20 6c  ng a bit-range l
da050 6f 63 6b 20 6f 6e 20 61 6e 0a 2a 2a 20 41 46 50  ock on an.** AFP
da060 20 66 69 6c 65 73 79 73 74 65 6d 2e 0a 2a 2a 20   filesystem..** 
da070 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54  .** Return SQLIT
da080 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2c  E_OK on success,
da090 20 53 51 4c 49 54 45 5f 42 55 53 59 20 6f 6e 20   SQLITE_BUSY on 
da0a0 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74  failure..*/.stat
da0b0 69 63 20 69 6e 74 20 61 66 70 53 65 74 4c 6f 63  ic int afpSetLoc
da0c0 6b 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  k(.  const char 
da0d0 2a 70 61 74 68 2c 20 20 20 20 20 20 20 20 20 20  *path,          
da0e0 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
da0f0 68 65 20 66 69 6c 65 20 74 6f 20 62 65 20 6c 6f  he file to be lo
da100 63 6b 65 64 20 6f 72 20 75 6e 6c 6f 63 6b 65 64  cked or unlocked
da110 20 2a 2f 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a   */.  unixFile *
da120 70 46 69 6c 65 2c 20 20 20 20 20 20 20 20 20 20  pFile,          
da130 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 66 69 6c       /* Open fil
da140 65 20 64 65 73 63 72 69 70 74 6f 72 20 6f 6e 20  e descriptor on 
da150 70 61 74 68 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  path */.  unsign
da160 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6f 66 66  ed long long off
da170 73 65 74 2c 20 20 20 20 20 2f 2a 20 46 69 72 73  set,     /* Firs
da180 74 20 62 79 74 65 20 74 6f 20 62 65 20 6c 6f 63  t byte to be loc
da190 6b 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  ked */.  unsigne
da1a0 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6c 65 6e 67  d long long leng
da1b0 74 68 2c 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  th,     /* Numbe
da1c0 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 6c 6f  r of bytes to lo
da1d0 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 73 65 74 4c  ck */.  int setL
da1e0 6f 63 6b 46 6c 61 67 20 20 20 20 20 20 20 20 20  ockFlag         
da1f0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74         /* True t
da200 6f 20 73 65 74 20 6c 6f 63 6b 2e 20 20 46 61 6c  o set lock.  Fal
da210 73 65 20 74 6f 20 63 6c 65 61 72 20 6c 6f 63 6b  se to clear lock
da220 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20   */.){.  struct 
da230 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32  ByteRangeLockPB2
da240 20 70 62 3b 0a 20 20 69 6e 74 20 65 72 72 3b 0a   pb;.  int err;.
da250 20 20 0a 20 20 70 62 2e 75 6e 4c 6f 63 6b 46 6c    .  pb.unLockFl
da260 61 67 20 3d 20 73 65 74 4c 6f 63 6b 46 6c 61 67  ag = setLockFlag
da270 20 3f 20 30 20 3a 20 31 3b 0a 20 20 70 62 2e 73   ? 0 : 1;.  pb.s
da280 74 61 72 74 45 6e 64 46 6c 61 67 20 3d 20 30 3b  tartEndFlag = 0;
da290 0a 20 20 70 62 2e 6f 66 66 73 65 74 20 3d 20 6f  .  pb.offset = o
da2a0 66 66 73 65 74 3b 0a 20 20 70 62 2e 6c 65 6e 67  ffset;.  pb.leng
da2b0 74 68 20 3d 20 6c 65 6e 67 74 68 3b 20 0a 20 20  th = length; .  
da2c0 70 62 2e 66 64 20 3d 20 70 46 69 6c 65 2d 3e 68  pb.fd = pFile->h
da2d0 3b 0a 20 20 0a 20 20 4f 53 54 52 41 43 45 36 28  ;.  .  OSTRACE6(
da2e0 22 41 46 50 53 45 54 4c 4f 43 4b 20 5b 25 73 5d  "AFPSETLOCK [%s]
da2f0 20 66 6f 72 20 25 64 25 73 20 69 6e 20 72 61 6e   for %d%s in ran
da300 67 65 20 25 6c 6c 78 3a 25 6c 6c 78 5c 6e 22 2c  ge %llx:%llx\n",
da310 20 0a 20 20 20 20 28 73 65 74 4c 6f 63 6b 46 6c   .    (setLockFl
da320 61 67 3f 22 4f 4e 22 3a 22 4f 46 46 22 29 2c 20  ag?"ON":"OFF"), 
da330 70 46 69 6c 65 2d 3e 68 2c 20 28 70 62 2e 66 64  pFile->h, (pb.fd
da340 3d 3d 2d 31 3f 22 5b 74 65 73 74 76 61 6c 2d 31  ==-1?"[testval-1
da350 5d 22 3a 22 22 29 2c 0a 20 20 20 20 6f 66 66 73  ]":""),.    offs
da360 65 74 2c 20 6c 65 6e 67 74 68 29 3b 0a 20 20 65  et, length);.  e
da370 72 72 20 3d 20 66 73 63 74 6c 28 70 61 74 68 2c  rr = fsctl(path,
da380 20 61 66 70 66 73 42 79 74 65 52 61 6e 67 65 4c   afpfsByteRangeL
da390 6f 63 6b 32 46 53 43 54 4c 2c 20 26 70 62 2c 20  ock2FSCTL, &pb, 
da3a0 30 29 3b 0a 20 20 69 66 20 28 20 65 72 72 3d 3d  0);.  if ( err==
da3b0 2d 31 20 29 20 7b 0a 20 20 20 20 69 6e 74 20 72  -1 ) {.    int r
da3c0 63 3b 0a 20 20 20 20 69 6e 74 20 74 45 72 72 6e  c;.    int tErrn
da3d0 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 4f  o = errno;.    O
da3e0 53 54 52 41 43 45 34 28 22 41 46 50 53 45 54 4c  STRACE4("AFPSETL
da3f0 4f 43 4b 20 66 61 69 6c 65 64 20 74 6f 20 66 73  OCK failed to fs
da400 63 74 6c 28 29 20 27 25 73 27 20 25 64 20 25 73  ctl() '%s' %d %s
da410 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \n",.           
da420 20 20 70 61 74 68 2c 20 74 45 72 72 6e 6f 2c 20    path, tErrno, 
da430 73 74 72 65 72 72 6f 72 28 74 45 72 72 6e 6f 29  strerror(tErrno)
da440 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  );.#ifdef SQLITE
da450 5f 49 47 4e 4f 52 45 5f 41 46 50 5f 4c 4f 43 4b  _IGNORE_AFP_LOCK
da460 5f 45 52 52 4f 52 53 0a 20 20 20 20 72 63 20 3d  _ERRORS.    rc =
da470 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 23 65   SQLITE_BUSY;.#e
da480 6c 73 65 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  lse.    rc = sql
da490 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69  iteErrorFromPosi
da4a0 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 0a 20  xError(tErrno,. 
da4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
da4c0 20 20 20 73 65 74 4c 6f 63 6b 46 6c 61 67 20 3f     setLockFlag ?
da4d0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f   SQLITE_IOERR_LO
da4e0 43 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52  CK : SQLITE_IOER
da4f0 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 23 65 6e 64 69  R_UNLOCK);.#endi
da500 66 20 2f 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f  f /* SQLITE_IGNO
da510 52 45 5f 41 46 50 5f 4c 4f 43 4b 5f 45 52 52 4f  RE_AFP_LOCK_ERRO
da520 52 53 20 2a 2f 0a 20 20 20 20 69 66 28 20 49 53  RS */.    if( IS
da530 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20  _LOCK_ERROR(rc) 
da540 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  ){.      pFile->
da550 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72  lastErrno = tErr
da560 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65  no;.    }.    re
da570 74 75 72 6e 20 72 63 3b 0a 20 20 7d 20 65 6c 73  turn rc;.  } els
da580 65 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  e {.    return S
da590 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a  QLITE_OK;.  }.}.
da5a0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
da5b0 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74 68  ine checks if th
da5c0 65 72 65 20 69 73 20 61 20 52 45 53 45 52 56 45  ere is a RESERVE
da5d0 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74  D lock held on t
da5e0 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20  he specified.** 
da5f0 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20  file by this or 
da600 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73  any other proces
da610 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63  s. If such a loc
da620 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a  k is held, set *
da630 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20  pResOut.** to a 
da640 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f  non-zero value o
da650 74 68 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75  therwise *pResOu
da660 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f  t is set to zero
da670 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61  .  The return va
da680 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f  lue.** is set to
da690 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73   SQLITE_OK unles
da6a0 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f  s an I/O error o
da6b0 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63  ccurs during loc
da6c0 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73  k checking..*/.s
da6d0 74 61 74 69 63 20 69 6e 74 20 61 66 70 43 68 65  tatic int afpChe
da6e0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73  ckReservedLock(s
da6f0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
da700 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a   int *pResOut){.
da710 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
da720 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 65  E_OK;.  int rese
da730 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 78  rved = 0;.  unix
da740 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
da750 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 0a  nixFile*)id;.  .
da760 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
da770 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  r( return SQLITE
da780 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45  _IOERR_CHECKRESE
da790 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a  RVEDLOCK; );.  .
da7a0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20    assert( pFile 
da7b0 29 3b 0a 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43  );.  afpLockingC
da7c0 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 20  ontext *context 
da7d0 3d 20 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e  = (afpLockingCon
da7e0 74 65 78 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c  text *) pFile->l
da7f0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20  ockingContext;. 
da800 20 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20   .  /* Check if 
da810 61 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73  a thread in this
da820 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73   process holds s
da830 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20  uch a lock */.  
da840 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  if( pFile->lockt
da850 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20  ype>SHARED_LOCK 
da860 29 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64 20  ){.    reserved 
da870 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a  = 1;.  }.  .  /*
da880 20 4f 74 68 65 72 77 69 73 65 20 73 65 65 20 69   Otherwise see i
da890 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f  f some other pro
da8a0 63 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 0a 20  cess holds it.. 
da8b0 20 20 2a 2f 0a 20 20 69 66 28 20 21 72 65 73 65    */.  if( !rese
da8c0 72 76 65 64 20 29 7b 0a 20 20 20 20 2f 2a 20 6c  rved ){.    /* l
da8d0 6f 63 6b 20 74 68 65 20 52 45 53 45 52 56 45 44  ock the RESERVED
da8e0 20 62 79 74 65 20 2a 2f 0a 20 20 20 20 69 6e 74   byte */.    int
da8f0 20 6c 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63   lrc = afpSetLoc
da900 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74  k(context->dbPat
da910 68 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 52 56  h, pFile, RESERV
da920 45 44 5f 42 59 54 45 2c 20 31 2c 31 29 3b 20 20  ED_BYTE, 1,1);  
da930 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f  .    if( SQLITE_
da940 4f 4b 3d 3d 6c 72 63 20 29 7b 0a 20 20 20 20 20  OK==lrc ){.     
da950 20 2f 2a 20 69 66 20 77 65 20 73 75 63 63 65 65   /* if we succee
da960 64 65 64 20 69 6e 20 74 61 6b 69 6e 67 20 74 68  ded in taking th
da970 65 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 2c  e reserved lock,
da980 20 75 6e 6c 6f 63 6b 20 69 74 20 74 6f 20 72 65   unlock it to re
da990 73 74 6f 72 65 0a 20 20 20 20 20 20 2a 2a 20 74  store.      ** t
da9a0 68 65 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74  he original stat
da9b0 65 20 2a 2f 0a 20 20 20 20 20 20 6c 72 63 20 3d  e */.      lrc =
da9c0 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74   afpSetLock(cont
da9d0 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69  ext->dbPath, pFi
da9e0 6c 65 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54  le, RESERVED_BYT
da9f0 45 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 20  E, 1, 0);.    } 
daa00 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 2f 2a 20  else {.      /* 
daa10 69 66 20 77 65 20 66 61 69 6c 65 64 20 74 6f 20  if we failed to 
daa20 67 65 74 20 74 68 65 20 6c 6f 63 6b 20 74 68 65  get the lock the
daa30 6e 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 6d  n someone else m
daa40 75 73 74 20 68 61 76 65 20 69 74 20 2a 2f 0a 20  ust have it */. 
daa50 20 20 20 20 20 72 65 73 65 72 76 65 64 20 3d 20       reserved = 
daa60 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  1;.    }.    if(
daa70 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c   IS_LOCK_ERROR(l
daa80 72 63 29 20 29 7b 0a 20 20 20 20 20 20 72 63 3d  rc) ){.      rc=
daa90 6c 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  lrc;.    }.  }. 
daaa0 20 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54 45   .  OSTRACE4("TE
daab0 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64  ST WR-LOCK %d %d
daac0 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68   %d\n", pFile->h
daad0 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64 29 3b  , rc, reserved);
daae0 0a 20 20 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d  .  .  *pResOut =
daaf0 20 72 65 73 65 72 76 65 64 3b 0a 20 20 72 65 74   reserved;.  ret
dab00 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
dab10 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77   Lock the file w
dab20 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65  ith the lock spe
dab30 63 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65  cified by parame
dab40 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f  ter locktype - o
dab50 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c  ne.** of the fol
dab60 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  lowing:.**.**   
dab70 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43    (1) SHARED_LOC
dab80 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53  K.**     (2) RES
dab90 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  ERVED_LOCK.**   
daba0 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f    (3) PENDING_LO
dabb0 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58  CK.**     (4) EX
dabc0 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a  CLUSIVE_LOCK.**.
dabd0 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65  ** Sometimes whe
dabe0 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65  n requesting one
dabf0 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64   lock state, add
dac00 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61  itional lock sta
dac10 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72  tes.** are inser
dac20 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20  ted in between. 
dac30 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67   The locking mig
dac40 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f  ht fail on one o
dac50 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74  f the later.** t
dac60 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69  ransitions leavi
dac70 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74  ng the lock stat
dac80 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d  e different from
dac90 20 77 68 61 74 20 69 74 20 73 74 61 72 74 65 64   what it started
daca0 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68   but.** still sh
dacb0 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e  ort of its goal.
dacc0 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
dacd0 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 65 20  chart shows the 
dace0 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73  allowed.** trans
dacf0 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69  itions and the i
dad00 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64  nserted intermed
dad10 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a  iate states:.**.
dad20 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d  **    UNLOCKED -
dad30 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53  > SHARED.**    S
dad40 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45  HARED -> RESERVE
dad50 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d  D.**    SHARED -
dad60 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45  > (PENDING) -> E
dad70 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52  XCLUSIVE.**    R
dad80 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44  ESERVED -> (PEND
dad90 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56  ING) -> EXCLUSIV
dada0 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20  E.**    PENDING 
dadb0 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a  -> EXCLUSIVE.**.
dadc0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
dadd0 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61  will only increa
dade0 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20  se a lock.  Use 
dadf0 74 68 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c  the sqlite3OsUnl
dae00 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65  ock().** routine
dae10 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b   to lower a lock
dae20 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74  ing level..*/.st
dae30 61 74 69 63 20 69 6e 74 20 61 66 70 4c 6f 63 6b  atic int afpLock
dae40 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
dae50 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29  d, int locktype)
dae60 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
dae70 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69  ITE_OK;.  unixFi
dae80 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69  le *pFile = (uni
dae90 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 66 70  xFile*)id;.  afp
daea0 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a  LockingContext *
daeb0 63 6f 6e 74 65 78 74 20 3d 20 28 61 66 70 4c 6f  context = (afpLo
daec0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20  ckingContext *) 
daed0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
daee0 6e 74 65 78 74 3b 0a 20 20 0a 20 20 61 73 73 65  ntext;.  .  asse
daef0 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f  rt( pFile );.  O
daf00 53 54 52 41 43 45 35 28 22 4c 4f 43 4b 20 20 20  STRACE5("LOCK   
daf10 20 25 64 20 25 73 20 77 61 73 20 25 73 20 70 69   %d %s was %s pi
daf20 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  d=%d\n", pFile->
daf30 68 2c 0a 20 20 20 20 20 20 20 20 20 6c 6f 63 6b  h,.         lock
daf40 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70  typeName(locktyp
daf50 65 29 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65  e), locktypeName
daf60 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65  (pFile->locktype
daf70 29 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20  ), getpid());.. 
daf80 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20   /* If there is 
daf90 61 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f  already a lock o
dafa0 66 20 74 68 69 73 20 74 79 70 65 20 6f 72 20 6d  f this type or m
dafb0 6f 72 65 20 72 65 73 74 72 69 63 74 69 76 65 20  ore restrictive 
dafc0 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 75 6e 69 78  on the.  ** unix
dafd0 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67  File, do nothing
dafe0 2e 20 44 6f 6e 27 74 20 75 73 65 20 74 68 65 20  . Don't use the 
daff0 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78  afp_end_lock: ex
db000 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a  it path, as.  **
db010 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28   unixEnterMutex(
db020 29 20 68 61 73 6e 27 74 20 62 65 65 6e 20 63 61  ) hasn't been ca
db030 6c 6c 65 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20  lled yet..  */. 
db040 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b   if( pFile->lock
db050 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29  type>=locktype )
db060 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 22  {.    OSTRACE3("
db070 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 6f 6b  LOCK    %d %s ok
db080 20 28 61 6c 72 65 61 64 79 20 68 65 6c 64 29 5c   (already held)\
db090 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20  n", pFile->h,.  
db0a0 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70           locktyp
db0b0 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 29  eName(locktype))
db0c0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
db0d0 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f  ITE_OK;.  }..  /
db0e0 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20  * Make sure the 
db0f0 6c 6f 63 6b 69 6e 67 20 73 65 71 75 65 6e 63 65  locking sequence
db100 20 69 73 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f   is correct.  */
db110 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65  .  assert( pFile
db120 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c  ->locktype!=NO_L
db130 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d  OCK || locktype=
db140 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a  =SHARED_LOCK );.
db150 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79    assert( lockty
db160 70 65 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b  pe!=PENDING_LOCK
db170 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f   );.  assert( lo
db180 63 6b 74 79 70 65 21 3d 52 45 53 45 52 56 45 44  cktype!=RESERVED
db190 5f 4c 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e  _LOCK || pFile->
db1a0 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44  locktype==SHARED
db1b0 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a  _LOCK );.  .  /*
db1c0 20 54 68 69 73 20 6d 75 74 65 78 20 69 73 20 6e   This mutex is n
db1d0 65 65 64 65 64 20 62 65 63 61 75 73 65 20 70 46  eeded because pF
db1e0 69 6c 65 2d 3e 70 4c 6f 63 6b 20 69 73 20 73 68  ile->pLock is sh
db1f0 61 72 65 64 20 61 63 72 6f 73 73 20 74 68 72 65  ared across thre
db200 61 64 73 0a 20 20 2a 2f 0a 20 20 75 6e 69 78 45  ads.  */.  unixE
db210 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 0a 20 20  nterMutex();..  
db220 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65  /* Make sure the
db230 20 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 20   current thread 
db240 6f 77 6e 73 20 74 68 65 20 70 46 69 6c 65 2e 0a  owns the pFile..
db250 20 20 2a 2f 0a 20 20 72 63 20 3d 20 74 72 61 6e    */.  rc = tran
db260 73 66 65 72 4f 77 6e 65 72 73 68 69 70 28 70 46  sferOwnership(pF
db270 69 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  ile);.  if( rc!=
db280 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
db290 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28   unixLeaveMutex(
db2a0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  );.    return rc
db2b0 3b 0a 20 20 7d 0a 20 20 20 20 0a 20 20 2f 2a 20  ;.  }.    .  /* 
db2c0 41 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69  A PENDING lock i
db2d0 73 20 6e 65 65 64 65 64 20 62 65 66 6f 72 65 20  s needed before 
db2e0 61 63 71 75 69 72 69 6e 67 20 61 20 53 48 41 52  acquiring a SHAR
db2f0 45 44 20 6c 6f 63 6b 20 61 6e 64 20 62 65 66 6f  ED lock and befo
db300 72 65 0a 20 20 2a 2a 20 61 63 71 75 69 72 69 6e  re.  ** acquirin
db310 67 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c  g an EXCLUSIVE l
db320 6f 63 6b 2e 20 20 46 6f 72 20 74 68 65 20 53 48  ock.  For the SH
db330 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20 50  ARED lock, the P
db340 45 4e 44 49 4e 47 20 77 69 6c 6c 0a 20 20 2a 2a  ENDING will.  **
db350 20 62 65 20 72 65 6c 65 61 73 65 64 2e 0a 20 20   be released..  
db360 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70  */.  if( locktyp
db370 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 0a  e==SHARED_LOCK .
db380 20 20 20 20 20 20 7c 7c 20 28 6c 6f 63 6b 74 79        || (lockty
db390 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f  pe==EXCLUSIVE_LO
db3a0 43 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63  CK && pFile->loc
db3b0 6b 74 79 70 65 3c 50 45 4e 44 49 4e 47 5f 4c 4f  ktype<PENDING_LO
db3c0 43 4b 29 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74  CK).  ){.    int
db3d0 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 66 61 69   failed;.    fai
db3e0 6c 65 64 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b  led = afpSetLock
db3f0 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68  (context->dbPath
db400 2c 20 70 46 69 6c 65 2c 20 50 45 4e 44 49 4e 47  , pFile, PENDING
db410 5f 42 59 54 45 2c 20 31 2c 20 31 29 3b 0a 20 20  _BYTE, 1, 1);.  
db420 20 20 69 66 20 28 66 61 69 6c 65 64 29 20 7b 0a    if (failed) {.
db430 20 20 20 20 20 20 72 63 20 3d 20 66 61 69 6c 65        rc = faile
db440 64 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 66  d;.      goto af
db450 70 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20  p_end_lock;.    
db460 7d 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 49 66  }.  }.  .  /* If
db470 20 63 6f 6e 74 72 6f 6c 20 67 65 74 73 20 74 6f   control gets to
db480 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65   this point, the
db490 6e 20 61 63 74 75 61 6c 6c 79 20 67 6f 20 61 68  n actually go ah
db4a0 65 61 64 20 61 6e 64 20 6d 61 6b 65 0a 20 20 2a  ead and make.  *
db4b0 2a 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74  * operating syst
db4c0 65 6d 20 63 61 6c 6c 73 20 66 6f 72 20 74 68 65  em calls for the
db4d0 20 73 70 65 63 69 66 69 65 64 20 6c 6f 63 6b 2e   specified lock.
db4e0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b  .  */.  if( lock
db4f0 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
db500 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 6c 6b 2c  K ){.    int lk,
db510 20 6c 72 63 31 2c 20 6c 72 63 32 2c 20 6c 72 63   lrc1, lrc2, lrc
db520 31 45 72 72 6e 6f 3b 0a 20 20 20 20 0a 20 20 20  1Errno;.    .   
db530 20 2f 2a 20 4e 6f 77 20 67 65 74 20 74 68 65 20   /* Now get the 
db540 72 65 61 64 2d 6c 6f 63 6b 20 53 48 41 52 45 44  read-lock SHARED
db550 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 20 20 2f 2a 20  _LOCK */.    /* 
db560 6e 6f 74 65 20 74 68 61 74 20 74 68 65 20 71 75  note that the qu
db570 61 6c 69 74 79 20 6f 66 20 74 68 65 20 72 61 6e  ality of the ran
db580 64 6f 6d 6e 65 73 73 20 64 6f 65 73 6e 27 74 20  domness doesn't 
db590 6d 61 74 74 65 72 20 74 68 61 74 20 6d 75 63 68  matter that much
db5a0 20 2a 2f 0a 20 20 20 20 6c 6b 20 3d 20 72 61 6e   */.    lk = ran
db5b0 64 6f 6d 28 29 3b 20 0a 20 20 20 20 63 6f 6e 74  dom(); .    cont
db5c0 65 78 74 2d 3e 73 68 61 72 65 64 42 79 74 65 20  ext->sharedByte 
db5d0 3d 20 28 6c 6b 20 26 20 30 78 37 66 66 66 66 66  = (lk & 0x7fffff
db5e0 66 66 29 25 28 53 48 41 52 45 44 5f 53 49 5a 45  ff)%(SHARED_SIZE
db5f0 20 2d 20 31 29 3b 0a 20 20 20 20 6c 72 63 31 20   - 1);.    lrc1 
db600 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e  = afpSetLock(con
db610 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46  text->dbPath, pF
db620 69 6c 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20  ile, .          
db630 53 48 41 52 45 44 5f 46 49 52 53 54 2b 63 6f 6e  SHARED_FIRST+con
db640 74 65 78 74 2d 3e 73 68 61 72 65 64 42 79 74 65  text->sharedByte
db650 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 69 66 28  , 1, 1);.    if(
db660 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c   IS_LOCK_ERROR(l
db670 72 63 31 29 20 29 7b 0a 20 20 20 20 20 20 6c 72  rc1) ){.      lr
db680 63 31 45 72 72 6e 6f 20 3d 20 70 46 69 6c 65 2d  c1Errno = pFile-
db690 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20 20 20  >lastErrno;.    
db6a0 7d 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 74 68  }.    /* Drop th
db6b0 65 20 74 65 6d 70 6f 72 61 72 79 20 50 45 4e 44  e temporary PEND
db6c0 49 4e 47 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20  ING lock */.    
db6d0 6c 72 63 32 20 3d 20 61 66 70 53 65 74 4c 6f 63  lrc2 = afpSetLoc
db6e0 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74  k(context->dbPat
db6f0 68 2c 20 70 46 69 6c 65 2c 20 50 45 4e 44 49 4e  h, pFile, PENDIN
db700 47 5f 42 59 54 45 2c 20 31 2c 20 30 29 3b 0a 20  G_BYTE, 1, 0);. 
db710 20 20 20 0a 20 20 20 20 69 66 28 20 49 53 5f 4c     .    if( IS_L
db720 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 31 29 20  OCK_ERROR(lrc1) 
db730 29 20 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d  ) {.      pFile-
db740 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6c 72 63  >lastErrno = lrc
db750 31 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63  1Errno;.      rc
db760 20 3d 20 6c 72 63 31 3b 0a 20 20 20 20 20 20 67   = lrc1;.      g
db770 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b  oto afp_end_lock
db780 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 69 66 28  ;.    } else if(
db790 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c   IS_LOCK_ERROR(l
db7a0 72 63 32 29 20 29 7b 0a 20 20 20 20 20 20 72 63  rc2) ){.      rc
db7b0 20 3d 20 6c 72 63 32 3b 0a 20 20 20 20 20 20 67   = lrc2;.      g
db7c0 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b  oto afp_end_lock
db7d0 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 69 66 28  ;.    } else if(
db7e0 20 6c 72 63 31 20 21 3d 20 53 51 4c 49 54 45 5f   lrc1 != SQLITE_
db7f0 4f 4b 20 29 20 7b 0a 20 20 20 20 20 20 72 63 20  OK ) {.      rc 
db800 3d 20 6c 72 63 31 3b 0a 20 20 20 20 7d 20 65 6c  = lrc1;.    } el
db810 73 65 20 7b 0a 20 20 20 20 20 20 70 46 69 6c 65  se {.      pFile
db820 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41  ->locktype = SHA
db830 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20  RED_LOCK;.      
db840 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c  pFile->pOpen->nL
db850 6f 63 6b 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d  ock++;.    }.  }
db860 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65  else{.    /* The
db870 20 72 65 71 75 65 73 74 20 77 61 73 20 66 6f 72   request was for
db880 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 45   a RESERVED or E
db890 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20  XCLUSIVE lock.  
db8a0 49 74 20 69 73 0a 20 20 20 20 2a 2a 20 61 73 73  It is.    ** ass
db8b0 75 6d 65 64 20 74 68 61 74 20 74 68 65 72 65 20  umed that there 
db8c0 69 73 20 61 20 53 48 41 52 45 44 20 6f 72 20 67  is a SHARED or g
db8d0 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74  reater lock on t
db8e0 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 61  he file.    ** a
db8f0 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a 20  lready..    */. 
db900 20 20 20 69 6e 74 20 66 61 69 6c 65 64 20 3d 20     int failed = 
db910 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 30  0;.    assert( 0
db920 21 3d 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  !=pFile->locktyp
db930 65 20 29 3b 0a 20 20 20 20 69 66 20 28 6c 6f 63  e );.    if (loc
db940 6b 74 79 70 65 20 3e 3d 20 52 45 53 45 52 56 45  ktype >= RESERVE
db950 44 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65 2d  D_LOCK && pFile-
db960 3e 6c 6f 63 6b 74 79 70 65 20 3c 20 52 45 53 45  >locktype < RESE
db970 52 56 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20  RVED_LOCK) {.   
db980 20 20 20 20 20 2f 2a 20 41 63 71 75 69 72 65 20       /* Acquire 
db990 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20  a RESERVED lock 
db9a0 2a 2f 0a 20 20 20 20 20 20 20 20 66 61 69 6c 65  */.        faile
db9b0 64 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63  d = afpSetLock(c
db9c0 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20  ontext->dbPath, 
db9d0 70 46 69 6c 65 2c 20 52 45 53 45 52 56 45 44 5f  pFile, RESERVED_
db9e0 42 59 54 45 2c 20 31 2c 31 29 3b 0a 20 20 20 20  BYTE, 1,1);.    
db9f0 7d 0a 20 20 20 20 69 66 20 28 21 66 61 69 6c 65  }.    if (!faile
dba00 64 20 26 26 20 6c 6f 63 6b 74 79 70 65 20 3d 3d  d && locktype ==
dba10 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29   EXCLUSIVE_LOCK)
dba20 20 7b 0a 20 20 20 20 20 20 2f 2a 20 41 63 71 75   {.      /* Acqu
dba30 69 72 65 20 61 6e 20 45 58 43 4c 55 53 49 56 45  ire an EXCLUSIVE
dba40 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 20   lock */.       
dba50 20 0a 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76   .      /* Remov
dba60 65 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63  e the shared loc
dba70 6b 20 62 65 66 6f 72 65 20 74 72 79 69 6e 67 20  k before trying 
dba80 74 68 65 20 72 61 6e 67 65 2e 20 20 77 65 27 6c  the range.  we'l
dba90 6c 20 6e 65 65 64 20 74 6f 20 0a 20 20 20 20 20  l need to .     
dbaa0 20 2a 2a 20 72 65 65 73 74 61 62 6c 69 73 68 20   ** reestablish 
dbab0 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20  the shared lock 
dbac0 69 66 20 77 65 20 63 61 6e 27 74 20 67 65 74 20  if we can't get 
dbad0 74 68 65 20 20 61 66 70 55 6e 6c 6f 63 6b 0a 20  the  afpUnlock. 
dbae0 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66       */.      if
dbaf0 28 20 21 28 66 61 69 6c 65 64 20 3d 20 61 66 70  ( !(failed = afp
dbb00 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d  SetLock(context-
dbb10 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20  >dbPath, pFile, 
dbb20 53 48 41 52 45 44 5f 46 49 52 53 54 20 2b 0a 20  SHARED_FIRST +. 
dbb30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dbb40 20 20 20 20 20 20 20 20 63 6f 6e 74 65 78 74 2d          context-
dbb50 3e 73 68 61 72 65 64 42 79 74 65 2c 20 31 2c 20  >sharedByte, 1, 
dbb60 30 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69  0)) ){.        i
dbb70 6e 74 20 66 61 69 6c 65 64 32 20 3d 20 53 51 4c  nt failed2 = SQL
dbb80 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20  ITE_OK;.        
dbb90 2f 2a 20 6e 6f 77 20 61 74 74 65 6d 6d 70 74 20  /* now attemmpt 
dbba0 74 6f 20 67 65 74 20 74 68 65 20 65 78 63 6c 75  to get the exclu
dbbb0 73 69 76 65 20 6c 6f 63 6b 20 72 61 6e 67 65 20  sive lock range 
dbbc0 2a 2f 0a 20 20 20 20 20 20 20 20 66 61 69 6c 65  */.        faile
dbbd0 64 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63  d = afpSetLock(c
dbbe0 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20  ontext->dbPath, 
dbbf0 70 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49  pFile, SHARED_FI
dbc00 52 53 54 2c 20 0a 20 20 20 20 20 20 20 20 20 20  RST, .          
dbc10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dbc20 20 20 20 20 20 53 48 41 52 45 44 5f 53 49 5a 45       SHARED_SIZE
dbc30 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 69 66  , 1);.        if
dbc40 28 20 66 61 69 6c 65 64 20 26 26 20 28 66 61 69  ( failed && (fai
dbc50 6c 65 64 32 20 3d 20 61 66 70 53 65 74 4c 6f 63  led2 = afpSetLoc
dbc60 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74  k(context->dbPat
dbc70 68 2c 20 70 46 69 6c 65 2c 20 0a 20 20 20 20 20  h, pFile, .     
dbc80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dbc90 20 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 2b    SHARED_FIRST +
dbca0 20 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64   context->shared
dbcb0 42 79 74 65 2c 20 31 2c 20 31 29 29 20 29 7b 0a  Byte, 1, 1)) ){.
dbcc0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 6e            /* Can
dbcd0 27 74 20 72 65 65 73 74 61 62 6c 69 73 68 20 74  't reestablish t
dbce0 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e 20  he shared lock. 
dbcf0 20 53 71 6c 69 74 65 20 63 61 6e 27 74 20 64 65   Sqlite can't de
dbd00 61 6c 2c 20 74 68 69 73 20 69 73 0a 20 20 20 20  al, this is.    
dbd10 20 20 20 20 20 20 2a 2a 20 61 20 63 72 69 74 69        ** a criti
dbd20 63 61 6c 20 49 2f 4f 20 65 72 72 6f 72 0a 20 20  cal I/O error.  
dbd30 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
dbd40 20 20 20 20 20 72 63 20 3d 20 28 28 66 61 69 6c       rc = ((fail
dbd50 65 64 20 26 20 53 51 4c 49 54 45 5f 49 4f 45 52  ed & SQLITE_IOER
dbd60 52 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45  R) == SQLITE_IOE
dbd70 52 52 29 20 3f 20 66 61 69 6c 65 64 32 20 3a 20  RR) ? failed2 : 
dbd80 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
dbd90 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43  SQLITE_IOERR_LOC
dbda0 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  K;.          got
dbdb0 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a  o afp_end_lock;.
dbdc0 20 20 20 20 20 20 20 20 7d 20 0a 20 20 20 20 20          } .     
dbdd0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
dbde0 72 63 20 3d 20 66 61 69 6c 65 64 3b 20 0a 20 20  rc = failed; .  
dbdf0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
dbe00 69 66 28 20 66 61 69 6c 65 64 20 29 7b 0a 20 20  if( failed ){.  
dbe10 20 20 20 20 72 63 20 3d 20 66 61 69 6c 65 64 3b      rc = failed;
dbe20 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20  .    }.  }.  .  
dbe30 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
dbe40 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  K ){.    pFile->
dbe50 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74  locktype = lockt
dbe60 79 70 65 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ype;.  }else if(
dbe70 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55   locktype==EXCLU
dbe80 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  SIVE_LOCK ){.   
dbe90 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
dbea0 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b   = PENDING_LOCK;
dbeb0 0a 20 20 7d 0a 20 20 0a 61 66 70 5f 65 6e 64 5f  .  }.  .afp_end_
dbec0 6c 6f 63 6b 3a 0a 20 20 75 6e 69 78 4c 65 61 76  lock:.  unixLeav
dbed0 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f 53 54 52  eMutex();.  OSTR
dbee0 41 43 45 34 28 22 4c 4f 43 4b 20 20 20 20 25 64  ACE4("LOCK    %d
dbef0 20 25 73 20 25 73 5c 6e 22 2c 20 70 46 69 6c 65   %s %s\n", pFile
dbf00 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d  ->h, locktypeNam
dbf10 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 0a 20 20  e(locktype), .  
dbf20 20 20 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54         rc==SQLIT
dbf30 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66  E_OK ? "ok" : "f
dbf40 61 69 6c 65 64 22 29 3b 0a 20 20 72 65 74 75 72  ailed");.  retur
dbf50 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c  n rc;.}../*.** L
dbf60 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67  ower the locking
dbf70 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64   level on file d
dbf80 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20  escriptor pFile 
dbf90 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f  to locktype.  lo
dbfa0 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62  cktype.** must b
dbfb0 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b  e either NO_LOCK
dbfc0 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e   or SHARED_LOCK.
dbfd0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f  .**.** If the lo
dbfe0 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74  cking level of t
dbff0 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
dc000 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74  or is already at
dc010 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65   or below.** the
dc020 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69   requested locki
dc030 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72  ng level, this r
dc040 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f  outine is a no-o
dc050 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  p..*/.static int
dc060 20 61 66 70 55 6e 6c 6f 63 6b 28 73 71 6c 69 74   afpUnlock(sqlit
dc070 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
dc080 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 69   locktype) {.  i
dc090 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
dc0a0 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70  K;.  unixFile *p
dc0b0 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
dc0c0 2a 29 69 64 3b 0a 20 20 61 66 70 4c 6f 63 6b 69  *)id;.  afpLocki
dc0d0 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20  ngContext *pCtx 
dc0e0 3d 20 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e  = (afpLockingCon
dc0f0 74 65 78 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c  text *) pFile->l
dc100 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 0a  ockingContext;..
dc110 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20    assert( pFile 
dc120 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 55  );.  OSTRACE5("U
dc130 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73  NLOCK  %d %d was
dc140 20 25 64 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70   %d pid=%d\n", p
dc150 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70  File->h, locktyp
dc160 65 2c 0a 20 20 20 20 20 20 20 20 20 70 46 69 6c  e,.         pFil
dc170 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74  e->locktype, get
dc180 70 69 64 28 29 29 3b 0a 0a 20 20 61 73 73 65 72  pid());..  asser
dc190 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41  t( locktype<=SHA
dc1a0 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66  RED_LOCK );.  if
dc1b0 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  ( pFile->locktyp
dc1c0 65 3c 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20  e<=locktype ){. 
dc1d0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
dc1e0 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 43  _OK;.  }.  if( C
dc1f0 48 45 43 4b 5f 54 48 52 45 41 44 49 44 28 70 46  HECK_THREADID(pF
dc200 69 6c 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75  ile) ){.    retu
dc210 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  rn SQLITE_MISUSE
dc220 3b 0a 20 20 7d 0a 20 20 75 6e 69 78 45 6e 74 65  ;.  }.  unixEnte
dc230 72 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20  rMutex();.  if( 
dc240 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e  pFile->locktype>
dc250 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20  SHARED_LOCK ){. 
dc260 20 20 20 0a 20 20 20 20 69 66 28 20 70 46 69 6c     .    if( pFil
dc270 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43  e->locktype==EXC
dc280 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20  LUSIVE_LOCK ){. 
dc290 20 20 20 20 20 72 63 20 3d 20 61 66 70 53 65 74       rc = afpSet
dc2a0 4c 6f 63 6b 28 70 43 74 78 2d 3e 64 62 50 61 74  Lock(pCtx->dbPat
dc2b0 68 2c 20 70 46 69 6c 65 2c 20 53 48 41 52 45 44  h, pFile, SHARED
dc2c0 5f 46 49 52 53 54 2c 20 53 48 41 52 45 44 5f 53  _FIRST, SHARED_S
dc2d0 49 5a 45 2c 20 30 29 3b 0a 20 20 20 20 20 20 69  IZE, 0);.      i
dc2e0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
dc2f0 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48   && locktype==SH
dc300 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  ARED_LOCK ){.   
dc310 20 20 20 20 20 2f 2a 20 6f 6e 6c 79 20 72 65 2d       /* only re-
dc320 65 73 74 61 62 6c 69 73 68 20 74 68 65 20 73 68  establish the sh
dc330 61 72 65 64 20 6c 6f 63 6b 20 69 66 20 6e 65 63  ared lock if nec
dc340 65 73 73 61 72 79 20 2a 2f 0a 20 20 20 20 20 20  essary */.      
dc350 20 20 69 6e 74 20 73 68 61 72 65 64 4c 6f 63 6b    int sharedLock
dc360 42 79 74 65 20 3d 20 53 48 41 52 45 44 5f 46 49  Byte = SHARED_FI
dc370 52 53 54 2b 70 43 74 78 2d 3e 73 68 61 72 65 64  RST+pCtx->shared
dc380 42 79 74 65 3b 0a 20 20 20 20 20 20 20 20 72 63  Byte;.        rc
dc390 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43   = afpSetLock(pC
dc3a0 74 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c  tx->dbPath, pFil
dc3b0 65 2c 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74  e, sharedLockByt
dc3c0 65 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 20 20  e, 1, 1);.      
dc3d0 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
dc3e0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
dc3f0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
dc400 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29  >=PENDING_LOCK )
dc410 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61 66 70  {.      rc = afp
dc420 53 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e 64 62  SetLock(pCtx->db
dc430 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 50 45 4e  Path, pFile, PEN
dc440 44 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20 30 29  DING_BYTE, 1, 0)
dc450 3b 0a 20 20 20 20 7d 20 0a 20 20 20 20 69 66 28  ;.    } .    if(
dc460 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
dc470 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  & pFile->locktyp
dc480 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  e>=RESERVED_LOCK
dc490 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61   ){.      rc = a
dc4a0 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e  fpSetLock(pCtx->
dc4b0 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 52  dbPath, pFile, R
dc4c0 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 31 2c  ESERVED_BYTE, 1,
dc4d0 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c   0);.    }.  }el
dc4e0 73 65 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d  se if( locktype=
dc4f0 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  =NO_LOCK ){.    
dc500 2f 2a 20 63 6c 65 61 72 20 74 68 65 20 73 68 61  /* clear the sha
dc510 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20  red lock */.    
dc520 69 6e 74 20 73 68 61 72 65 64 4c 6f 63 6b 42 79  int sharedLockBy
dc530 74 65 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53  te = SHARED_FIRS
dc540 54 2b 70 43 74 78 2d 3e 73 68 61 72 65 64 42 79  T+pCtx->sharedBy
dc550 74 65 3b 0a 20 20 20 20 72 63 20 3d 20 61 66 70  te;.    rc = afp
dc560 53 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e 64 62  SetLock(pCtx->db
dc570 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 73 68 61  Path, pFile, sha
dc580 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 31 2c 20  redLockByte, 1, 
dc590 30 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72  0);.  }..  if( r
dc5a0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
dc5b0 20 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65      if( locktype
dc5c0 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  ==NO_LOCK ){.   
dc5d0 20 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70     struct unixOp
dc5e0 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 20 3d 20 70  enCnt *pOpen = p
dc5f0 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 20  File->pOpen;.   
dc600 20 20 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2d     pOpen->nLock-
dc610 2d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  -;.      assert(
dc620 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3e 3d 30   pOpen->nLock>=0
dc630 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f   );.      if( pO
dc640 70 65 6e 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20 29 7b  pen->nLock==0 ){
dc650 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 63 6c  .        rc = cl
dc660 6f 73 65 50 65 6e 64 69 6e 67 46 64 73 28 70 46  osePendingFds(pF
dc670 69 6c 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ile);.      }.  
dc680 20 20 7d 0a 20 20 7d 0a 20 20 75 6e 69 78 4c 65    }.  }.  unixLe
dc690 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 69 66  aveMutex();.  if
dc6a0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
dc6b0 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f  ){.    pFile->lo
dc6c0 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70  cktype = locktyp
dc6d0 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  e;.  }.  return 
dc6e0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f  rc;.}../*.** Clo
dc6f0 73 65 20 61 20 66 69 6c 65 20 26 20 63 6c 65 61  se a file & clea
dc700 6e 75 70 20 41 46 50 20 73 70 65 63 69 66 69 63  nup AFP specific
dc710 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74   locking context
dc720 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20   .*/.static int 
dc730 61 66 70 43 6c 6f 73 65 28 73 71 6c 69 74 65 33  afpClose(sqlite3
dc740 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69  _file *id) {.  i
dc750 66 28 20 69 64 20 29 7b 0a 20 20 20 20 75 6e 69  f( id ){.    uni
dc760 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  xFile *pFile = (
dc770 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20  unixFile*)id;.  
dc780 20 20 61 66 70 55 6e 6c 6f 63 6b 28 69 64 2c 20    afpUnlock(id, 
dc790 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 75 6e  NO_LOCK);.    un
dc7a0 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a  ixEnterMutex();.
dc7b0 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70      if( pFile->p
dc7c0 4f 70 65 6e 20 26 26 20 70 46 69 6c 65 2d 3e 70  Open && pFile->p
dc7d0 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 20 29 7b 0a 20  Open->nLock ){. 
dc7e0 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65       /* If there
dc7f0 20 61 72 65 20 6f 75 74 73 74 61 6e 64 69 6e 67   are outstanding
dc800 20 6c 6f 63 6b 73 2c 20 64 6f 20 6e 6f 74 20 61   locks, do not a
dc810 63 74 75 61 6c 6c 79 20 63 6c 6f 73 65 20 74 68  ctually close th
dc820 65 20 66 69 6c 65 20 6a 75 73 74 0a 20 20 20 20  e file just.    
dc830 20 20 2a 2a 20 79 65 74 20 62 65 63 61 75 73 65    ** yet because
dc840 20 74 68 61 74 20 77 6f 75 6c 64 20 63 6c 65 61   that would clea
dc850 72 20 74 68 6f 73 65 20 6c 6f 63 6b 73 2e 20 20  r those locks.  
dc860 49 6e 73 74 65 61 64 2c 20 61 64 64 20 74 68 65  Instead, add the
dc870 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 64   file.      ** d
dc880 65 73 63 72 69 70 74 6f 72 20 74 6f 20 70 4f 70  escriptor to pOp
dc890 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 2e 20 20 49  en->aPending.  I
dc8a0 74 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61  t will be automa
dc8b0 74 69 63 61 6c 6c 79 20 63 6c 6f 73 65 64 20 77  tically closed w
dc8c0 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  hen.      ** the
dc8d0 20 6c 61 73 74 20 6c 6f 63 6b 20 69 73 20 63 6c   last lock is cl
dc8e0 65 61 72 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a  eared..      */.
dc8f0 20 20 20 20 20 20 73 65 74 50 65 6e 64 69 6e 67        setPending
dc900 46 64 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d  Fd(pFile);.    }
dc910 0a 20 20 20 20 72 65 6c 65 61 73 65 4f 70 65 6e  .    releaseOpen
dc920 43 6e 74 28 70 46 69 6c 65 2d 3e 70 4f 70 65 6e  Cnt(pFile->pOpen
dc930 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
dc940 72 65 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69  ree(pFile->locki
dc950 6e 67 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20  ngContext);.    
dc960 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64  closeUnixFile(id
dc970 29 3b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65  );.    unixLeave
dc980 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a 20 20 72  Mutex();.  }.  r
dc990 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
dc9a0 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65  .}..#endif /* de
dc9b0 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29  fined(__APPLE__)
dc9c0 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   && SQLITE_ENABL
dc9d0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
dc9e0 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64  */./*.** The cod
dc9f0 65 20 61 62 6f 76 65 20 69 73 20 74 68 65 20 41  e above is the A
dca00 46 50 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e  FP lock implemen
dca10 74 61 74 69 6f 6e 2e 20 20 54 68 65 20 63 6f 64  tation.  The cod
dca20 65 20 69 73 20 73 70 65 63 69 66 69 63 0a 2a 2a  e is specific.**
dca30 20 74 6f 20 4d 61 63 4f 53 58 20 61 6e 64 20 64   to MacOSX and d
dca40 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20  oes not work on 
dca50 6f 74 68 65 72 20 75 6e 69 78 20 70 6c 61 74 66  other unix platf
dca60 6f 72 6d 73 2e 20 20 4e 6f 20 61 6c 74 65 72 6e  orms.  No altern
dca70 61 74 69 76 65 0a 2a 2a 20 69 73 20 61 76 61 69  ative.** is avai
dca80 6c 61 62 6c 65 2e 20 20 49 66 20 79 6f 75 20 64  lable.  If you d
dca90 6f 6e 27 74 20 63 6f 6d 70 69 6c 65 20 66 6f 72  on't compile for
dcaa0 20 61 20 6d 61 63 2c 20 74 68 65 6e 20 74 68 65   a mac, then the
dcab0 20 22 75 6e 69 78 2d 61 66 70 22 0a 2a 2a 20 56   "unix-afp".** V
dcac0 46 53 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61  FS is not availa
dcad0 62 6c 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ble..**.********
dcae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
dcaf0 64 20 6f 66 20 74 68 65 20 41 46 50 20 6c 6f 63  d of the AFP loc
dcb00 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  k implementation
dcb10 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
dcb20 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  *******.********
dcb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dcb40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dcb50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dcb60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dcb70 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a  ******/.../*****
dcb80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dcb90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dcba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dcbb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dcbc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a  *********.******
dcbd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4e 6f 6e 2d 6c  ********** Non-l
dcbe0 6f 63 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f 66  ocking sqlite3_f
dcbf0 69 6c 65 20 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a  ile methods ****
dcc00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dcc10 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
dcc20 54 68 65 20 6e 65 78 74 20 64 69 76 69 73 69 6f  The next divisio
dcc30 6e 20 63 6f 6e 74 61 69 6e 73 20 69 6d 70 6c 65  n contains imple
dcc40 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20 61  mentations for a
dcc50 6c 6c 20 6d 65 74 68 6f 64 73 20 6f 66 20 74 68  ll methods of th
dcc60 65 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 69  e .** sqlite3_fi
dcc70 6c 65 20 6f 62 6a 65 63 74 20 6f 74 68 65 72 20  le object other 
dcc80 74 68 61 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67  than the locking
dcc90 20 6d 65 74 68 6f 64 73 2e 20 20 54 68 65 20 6c   methods.  The l
dcca0 6f 63 6b 69 6e 67 0a 2a 2a 20 6d 65 74 68 6f 64  ocking.** method
dccb0 73 20 77 65 72 65 20 64 65 66 69 6e 65 64 20 69  s were defined i
dccc0 6e 20 64 69 76 69 73 69 6f 6e 73 20 61 62 6f 76  n divisions abov
dccd0 65 20 28 6f 6e 65 20 6c 6f 63 6b 69 6e 67 20 6d  e (one locking m
dcce0 65 74 68 6f 64 20 70 65 72 0a 2a 2a 20 64 69 76  ethod per.** div
dccf0 69 73 69 6f 6e 29 2e 20 20 54 68 6f 73 65 20 6d  ision).  Those m
dcd00 65 74 68 6f 64 73 20 74 68 61 74 20 61 72 65 20  ethods that are 
dcd10 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6c 6f  common to all lo
dcd20 63 6b 69 6e 67 20 6d 6f 64 65 73 0a 2a 2a 20 61  cking modes.** a
dcd30 72 65 20 67 61 74 68 65 72 20 74 6f 67 65 74 68  re gather togeth
dcd40 65 72 20 69 6e 74 6f 20 74 68 69 73 20 64 69 76  er into this div
dcd50 69 73 69 6f 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  ision..*/../*.**
dcd60 20 53 65 65 6b 20 74 6f 20 74 68 65 20 6f 66 66   Seek to the off
dcd70 73 65 74 20 70 61 73 73 65 64 20 61 73 20 74 68  set passed as th
dcd80 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
dcd90 74 2c 20 74 68 65 6e 20 72 65 61 64 20 63 6e 74  t, then read cnt
dcda0 20 0a 2a 2a 20 62 79 74 65 73 20 69 6e 74 6f 20   .** bytes into 
dcdb0 70 42 75 66 2e 20 52 65 74 75 72 6e 20 74 68 65  pBuf. Return the
dcdc0 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
dcdd0 20 61 63 74 75 61 6c 6c 79 20 72 65 61 64 2e 0a   actually read..
dcde0 2a 2a 0a 2a 2a 20 4e 42 3a 20 20 49 66 20 79 6f  **.** NB:  If yo
dcdf0 75 20 64 65 66 69 6e 65 20 55 53 45 5f 50 52 45  u define USE_PRE
dce00 41 44 20 6f 72 20 55 53 45 5f 50 52 45 41 44 36  AD or USE_PREAD6
dce10 34 2c 20 74 68 65 6e 20 69 74 20 6d 69 67 68 74  4, then it might
dce20 20 61 6c 73 6f 0a 2a 2a 20 62 65 20 6e 65 63 65   also.** be nece
dce30 73 73 61 72 79 20 74 6f 20 64 65 66 69 6e 65 20  ssary to define 
dce40 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 74 6f  _XOPEN_SOURCE to
dce50 20 62 65 20 35 30 30 2e 20 20 54 68 69 73 20 76   be 500.  This v
dce60 61 72 69 65 73 20 66 72 6f 6d 0a 2a 2a 20 6f 6e  aries from.** on
dce70 65 20 73 79 73 74 65 6d 20 74 6f 20 61 6e 6f 74  e system to anot
dce80 68 65 72 2e 20 20 53 69 6e 63 65 20 53 51 4c 69  her.  Since SQLi
dce90 74 65 20 64 6f 65 73 20 6e 6f 74 20 64 65 66 69  te does not defi
dcea0 6e 65 20 55 53 45 5f 50 52 45 41 44 0a 2a 2a 20  ne USE_PREAD.** 
dceb0 61 6e 79 20 61 6e 79 20 66 6f 72 6d 20 62 79 20  any any form by 
dcec0 64 65 66 61 75 6c 74 2c 20 77 65 20 77 69 6c 6c  default, we will
dced0 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20   not attempt to 
dcee0 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f  define _XOPEN_SO
dcef0 55 52 43 45 2e 0a 2a 2a 20 53 65 65 20 74 69 63  URCE..** See tic
dcf00 6b 65 74 73 20 23 32 37 34 31 20 61 6e 64 20 23  kets #2741 and #
dcf10 32 36 38 31 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61  2681..**.** To a
dcf20 76 6f 69 64 20 73 74 6f 6d 70 69 6e 67 20 74 68  void stomping th
dcf30 65 20 65 72 72 6e 6f 20 76 61 6c 75 65 20 6f 6e  e errno value on
dcf40 20 61 20 66 61 69 6c 65 64 20 72 65 61 64 20 74   a failed read t
dcf50 68 65 20 6c 61 73 74 45 72 72 6e 6f 20 76 61 6c  he lastErrno val
dcf60 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 62 65 66  ue.** is set bef
dcf70 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a  ore returning..*
dcf80 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 65  /.static int see
dcf90 6b 41 6e 64 52 65 61 64 28 75 6e 69 78 46 69 6c  kAndRead(unixFil
dcfa0 65 20 2a 69 64 2c 20 73 71 6c 69 74 65 33 5f 69  e *id, sqlite3_i
dcfb0 6e 74 36 34 20 6f 66 66 73 65 74 2c 20 76 6f 69  nt64 offset, voi
dcfc0 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 63 6e 74  d *pBuf, int cnt
dcfd0 29 7b 0a 20 20 69 6e 74 20 67 6f 74 3b 0a 20 20  ){.  int got;.  
dcfe0 69 36 34 20 6e 65 77 4f 66 66 73 65 74 3b 0a 20  i64 newOffset;. 
dcff0 20 54 49 4d 45 52 5f 53 54 41 52 54 3b 0a 23 69   TIMER_START;.#i
dd000 66 20 64 65 66 69 6e 65 64 28 55 53 45 5f 50 52  f defined(USE_PR
dd010 45 41 44 29 0a 20 20 67 6f 74 20 3d 20 70 72 65  EAD).  got = pre
dd020 61 64 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20  ad(id->h, pBuf, 
dd030 63 6e 74 2c 20 6f 66 66 73 65 74 29 3b 0a 20 20  cnt, offset);.  
dd040 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
dd050 20 67 6f 74 20 3d 20 2d 31 20 29 3b 0a 23 65 6c   got = -1 );.#el
dd060 69 66 20 64 65 66 69 6e 65 64 28 55 53 45 5f 50  if defined(USE_P
dd070 52 45 41 44 36 34 29 0a 20 20 67 6f 74 20 3d 20  READ64).  got = 
dd080 70 72 65 61 64 36 34 28 69 64 2d 3e 68 2c 20 70  pread64(id->h, p
dd090 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74  Buf, cnt, offset
dd0a0 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  );.  SimulateIOE
dd0b0 72 72 6f 72 28 20 67 6f 74 20 3d 20 2d 31 20 29  rror( got = -1 )
dd0c0 3b 0a 23 65 6c 73 65 0a 20 20 6e 65 77 4f 66 66  ;.#else.  newOff
dd0d0 73 65 74 20 3d 20 6c 73 65 65 6b 28 69 64 2d 3e  set = lseek(id->
dd0e0 68 2c 20 6f 66 66 73 65 74 2c 20 53 45 45 4b 5f  h, offset, SEEK_
dd0f0 53 45 54 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65  SET);.  Simulate
dd100 49 4f 45 72 72 6f 72 28 20 6e 65 77 4f 66 66 73  IOError( newOffs
dd110 65 74 2d 2d 20 29 3b 0a 20 20 69 66 28 20 6e 65  et-- );.  if( ne
dd120 77 4f 66 66 73 65 74 21 3d 6f 66 66 73 65 74 20  wOffset!=offset 
dd130 29 7b 0a 20 20 20 20 69 66 28 20 6e 65 77 4f 66  ){.    if( newOf
dd140 66 73 65 74 20 3d 3d 20 2d 31 20 29 7b 0a 20 20  fset == -1 ){.  
dd150 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29      ((unixFile*)
dd160 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  id)->lastErrno =
dd170 20 65 72 72 6e 6f 3b 0a 20 20 20 20 7d 65 6c 73   errno;.    }els
dd180 65 7b 0a 20 20 20 20 20 20 28 28 75 6e 69 78 46  e{.      ((unixF
dd190 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72  ile*)id)->lastEr
dd1a0 72 6e 6f 20 3d 20 30 3b 09 09 09 0a 20 20 20 20  rno = 0;....    
dd1b0 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b  }.    return -1;
dd1c0 0a 20 20 7d 0a 20 20 67 6f 74 20 3d 20 72 65 61  .  }.  got = rea
dd1d0 64 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63  d(id->h, pBuf, c
dd1e0 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20 54 49  nt);.#endif.  TI
dd1f0 4d 45 52 5f 45 4e 44 3b 0a 20 20 69 66 28 20 67  MER_END;.  if( g
dd200 6f 74 3c 30 20 29 7b 0a 20 20 20 20 28 28 75 6e  ot<0 ){.    ((un
dd210 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73  ixFile*)id)->las
dd220 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a  tErrno = errno;.
dd230 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 35 28 22    }.  OSTRACE5("
dd240 52 45 41 44 20 20 20 20 25 2d 33 64 20 25 35 64  READ    %-3d %5d
dd250 20 25 37 6c 6c 64 20 25 6c 6c 75 5c 6e 22 2c 20   %7lld %llu\n", 
dd260 69 64 2d 3e 68 2c 20 67 6f 74 2c 20 6f 66 66 73  id->h, got, offs
dd270 65 74 2c 20 54 49 4d 45 52 5f 45 4c 41 50 53 45  et, TIMER_ELAPSE
dd280 44 29 3b 0a 20 20 72 65 74 75 72 6e 20 67 6f 74  D);.  return got
dd290 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20  ;.}../*.** Read 
dd2a0 64 61 74 61 20 66 72 6f 6d 20 61 20 66 69 6c 65  data from a file
dd2b0 20 69 6e 74 6f 20 61 20 62 75 66 66 65 72 2e 20   into a buffer. 
dd2c0 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
dd2d0 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 65  K if all.** byte
dd2e0 73 20 77 65 72 65 20 72 65 61 64 20 73 75 63 63  s were read succ
dd2f0 65 73 73 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c  essfully and SQL
dd300 49 54 45 5f 49 4f 45 52 52 20 69 66 20 61 6e 79  ITE_IOERR if any
dd310 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72  thing goes.** wr
dd320 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ong..*/.static i
dd330 6e 74 20 75 6e 69 78 52 65 61 64 28 0a 20 20 73  nt unixRead(.  s
dd340 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
dd350 20 0a 20 20 76 6f 69 64 20 2a 70 42 75 66 2c 20   .  void *pBuf, 
dd360 0a 20 20 69 6e 74 20 61 6d 74 2c 0a 20 20 73 71  .  int amt,.  sq
dd370 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73  lite3_int64 offs
dd380 65 74 0a 29 7b 0a 20 20 75 6e 69 78 46 69 6c 65  et.){.  unixFile
dd390 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46   *pFile = (unixF
dd3a0 69 6c 65 20 2a 29 69 64 3b 0a 20 20 69 6e 74 20  ile *)id;.  int 
dd3b0 67 6f 74 3b 0a 20 20 61 73 73 65 72 74 28 20 69  got;.  assert( i
dd3c0 64 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  d );..  /* If th
dd3d0 69 73 20 69 73 20 61 20 64 61 74 61 62 61 73 65  is is a database
dd3e0 20 66 69 6c 65 20 28 6e 6f 74 20 61 20 6a 6f 75   file (not a jou
dd3f0 72 6e 61 6c 2c 20 6d 61 73 74 65 72 2d 6a 6f 75  rnal, master-jou
dd400 72 6e 61 6c 20 6f 72 20 74 65 6d 70 0a 20 20 2a  rnal or temp.  *
dd410 2a 20 66 69 6c 65 29 2c 20 74 68 65 20 62 79 74  * file), the byt
dd420 65 73 20 69 6e 20 74 68 65 20 6c 6f 63 6b 69 6e  es in the lockin
dd430 67 20 72 61 6e 67 65 20 73 68 6f 75 6c 64 20 6e  g range should n
dd440 65 76 65 72 20 62 65 20 72 65 61 64 20 6f 72 20  ever be read or 
dd450 77 72 69 74 74 65 6e 2e 20 2a 2f 0a 20 20 61 73  written. */.  as
dd460 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 70 55 6e  sert( pFile->pUn
dd470 75 73 65 64 3d 3d 30 0a 20 20 20 20 20 20 20 7c  used==0.       |
dd480 7c 20 6f 66 66 73 65 74 3e 3d 50 45 4e 44 49 4e  | offset>=PENDIN
dd490 47 5f 42 59 54 45 2b 35 31 32 0a 20 20 20 20 20  G_BYTE+512.     
dd4a0 20 20 7c 7c 20 6f 66 66 73 65 74 2b 61 6d 74 3c    || offset+amt<
dd4b0 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 0a 20  =PENDING_BYTE . 
dd4c0 20 29 3b 0a 0a 20 20 67 6f 74 20 3d 20 73 65 65   );..  got = see
dd4d0 6b 41 6e 64 52 65 61 64 28 70 46 69 6c 65 2c 20  kAndRead(pFile, 
dd4e0 6f 66 66 73 65 74 2c 20 70 42 75 66 2c 20 61 6d  offset, pBuf, am
dd4f0 74 29 3b 0a 20 20 69 66 28 20 67 6f 74 3d 3d 61  t);.  if( got==a
dd500 6d 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  mt ){.    return
dd510 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65   SQLITE_OK;.  }e
dd520 6c 73 65 20 69 66 28 20 67 6f 74 3c 30 20 29 7b  lse if( got<0 ){
dd530 0a 20 20 20 20 2f 2a 20 6c 61 73 74 45 72 72 6e  .    /* lastErrn
dd540 6f 20 73 65 74 20 62 79 20 73 65 65 6b 41 6e 64  o set by seekAnd
dd550 52 65 61 64 20 2a 2f 0a 20 20 20 20 72 65 74 75  Read */.    retu
dd560 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
dd570 52 45 41 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  READ;.  }else{. 
dd580 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72     pFile->lastEr
dd590 72 6e 6f 20 3d 20 30 3b 20 2f 2a 20 6e 6f 74 20  rno = 0; /* not 
dd5a0 61 20 73 79 73 74 65 6d 20 65 72 72 6f 72 20 2a  a system error *
dd5b0 2f 0a 20 20 20 20 2f 2a 20 55 6e 72 65 61 64 20  /.    /* Unread 
dd5c0 70 61 72 74 73 20 6f 66 20 74 68 65 20 62 75 66  parts of the buf
dd5d0 66 65 72 20 6d 75 73 74 20 62 65 20 7a 65 72 6f  fer must be zero
dd5e0 2d 66 69 6c 6c 65 64 20 2a 2f 0a 20 20 20 20 6d  -filled */.    m
dd5f0 65 6d 73 65 74 28 26 28 28 63 68 61 72 2a 29 70  emset(&((char*)p
dd600 42 75 66 29 5b 67 6f 74 5d 2c 20 30 2c 20 61 6d  Buf)[got], 0, am
dd610 74 2d 67 6f 74 29 3b 0a 20 20 20 20 72 65 74 75  t-got);.    retu
dd620 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
dd630 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d 0a  SHORT_READ;.  }.
dd640 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 65 6b 20 74 6f  }../*.** Seek to
dd650 20 74 68 65 20 6f 66 66 73 65 74 20 69 6e 20 69   the offset in i
dd660 64 2d 3e 6f 66 66 73 65 74 20 74 68 65 6e 20 72  d->offset then r
dd670 65 61 64 20 63 6e 74 20 62 79 74 65 73 20 69 6e  ead cnt bytes in
dd680 74 6f 20 70 42 75 66 2e 0a 2a 2a 20 52 65 74 75  to pBuf..** Retu
dd690 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
dd6a0 20 62 79 74 65 73 20 61 63 74 75 61 6c 6c 79 20   bytes actually 
dd6b0 72 65 61 64 2e 20 20 55 70 64 61 74 65 20 74 68  read.  Update th
dd6c0 65 20 6f 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a 20  e offset..**.** 
dd6d0 54 6f 20 61 76 6f 69 64 20 73 74 6f 6d 70 69 6e  To avoid stompin
dd6e0 67 20 74 68 65 20 65 72 72 6e 6f 20 76 61 6c 75  g the errno valu
dd6f0 65 20 6f 6e 20 61 20 66 61 69 6c 65 64 20 77 72  e on a failed wr
dd700 69 74 65 20 74 68 65 20 6c 61 73 74 45 72 72 6e  ite the lastErrn
dd710 6f 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65  o value.** is se
dd720 74 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  t before returni
dd730 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ng..*/.static in
dd740 74 20 73 65 65 6b 41 6e 64 57 72 69 74 65 28 75  t seekAndWrite(u
dd750 6e 69 78 46 69 6c 65 20 2a 69 64 2c 20 69 36 34  nixFile *id, i64
dd760 20 6f 66 66 73 65 74 2c 20 63 6f 6e 73 74 20 76   offset, const v
dd770 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 63  oid *pBuf, int c
dd780 6e 74 29 7b 0a 20 20 69 6e 74 20 67 6f 74 3b 0a  nt){.  int got;.
dd790 20 20 69 36 34 20 6e 65 77 4f 66 66 73 65 74 3b    i64 newOffset;
dd7a0 0a 20 20 54 49 4d 45 52 5f 53 54 41 52 54 3b 0a  .  TIMER_START;.
dd7b0 23 69 66 20 64 65 66 69 6e 65 64 28 55 53 45 5f  #if defined(USE_
dd7c0 50 52 45 41 44 29 0a 20 20 67 6f 74 20 3d 20 70  PREAD).  got = p
dd7d0 77 72 69 74 65 28 69 64 2d 3e 68 2c 20 70 42 75  write(id->h, pBu
dd7e0 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74 29 3b  f, cnt, offset);
dd7f0 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 55  .#elif defined(U
dd800 53 45 5f 50 52 45 41 44 36 34 29 0a 20 20 67 6f  SE_PREAD64).  go
dd810 74 20 3d 20 70 77 72 69 74 65 36 34 28 69 64 2d  t = pwrite64(id-
dd820 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f  >h, pBuf, cnt, o
dd830 66 66 73 65 74 29 3b 0a 23 65 6c 73 65 0a 20 20  ffset);.#else.  
dd840 6e 65 77 4f 66 66 73 65 74 20 3d 20 6c 73 65 65  newOffset = lsee
dd850 6b 28 69 64 2d 3e 68 2c 20 6f 66 66 73 65 74 2c  k(id->h, offset,
dd860 20 53 45 45 4b 5f 53 45 54 29 3b 0a 20 20 69 66   SEEK_SET);.  if
dd870 28 20 6e 65 77 4f 66 66 73 65 74 21 3d 6f 66 66  ( newOffset!=off
dd880 73 65 74 20 29 7b 0a 20 20 20 20 69 66 28 20 6e  set ){.    if( n
dd890 65 77 4f 66 66 73 65 74 20 3d 3d 20 2d 31 20 29  ewOffset == -1 )
dd8a0 7b 0a 20 20 20 20 20 20 28 28 75 6e 69 78 46 69  {.      ((unixFi
dd8b0 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72  le*)id)->lastErr
dd8c0 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20  no = errno;.    
dd8d0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 28 75  }else{.      ((u
dd8e0 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61  nixFile*)id)->la
dd8f0 73 74 45 72 72 6e 6f 20 3d 20 30 3b 09 09 09 0a  stErrno = 0;....
dd900 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
dd910 20 2d 31 3b 0a 20 20 7d 0a 20 20 67 6f 74 20 3d   -1;.  }.  got =
dd920 20 77 72 69 74 65 28 69 64 2d 3e 68 2c 20 70 42   write(id->h, pB
dd930 75 66 2c 20 63 6e 74 29 3b 0a 23 65 6e 64 69 66  uf, cnt);.#endif
dd940 0a 20 20 54 49 4d 45 52 5f 45 4e 44 3b 0a 20 20  .  TIMER_END;.  
dd950 69 66 28 20 67 6f 74 3c 30 20 29 7b 0a 20 20 20  if( got<0 ){.   
dd960 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29   ((unixFile*)id)
dd970 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72  ->lastErrno = er
dd980 72 6e 6f 3b 0a 20 20 7d 0a 0a 20 20 4f 53 54 52  rno;.  }..  OSTR
dd990 41 43 45 35 28 22 57 52 49 54 45 20 20 20 25 2d  ACE5("WRITE   %-
dd9a0 33 64 20 25 35 64 20 25 37 6c 6c 64 20 25 6c 6c  3d %5d %7lld %ll
dd9b0 75 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 67 6f 74  u\n", id->h, got
dd9c0 2c 20 6f 66 66 73 65 74 2c 20 54 49 4d 45 52 5f  , offset, TIMER_
dd9d0 45 4c 41 50 53 45 44 29 3b 0a 20 20 72 65 74 75  ELAPSED);.  retu
dd9e0 72 6e 20 67 6f 74 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  rn got;.}.../*.*
dd9f0 2a 20 57 72 69 74 65 20 64 61 74 61 20 66 72 6f  * Write data fro
dda00 6d 20 61 20 62 75 66 66 65 72 20 69 6e 74 6f 20  m a buffer into 
dda10 61 20 66 69 6c 65 2e 20 20 52 65 74 75 72 6e 20  a file.  Return 
dda20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63  SQLITE_OK on suc
dda30 63 65 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20  cess.** or some 
dda40 6f 74 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65  other error code
dda50 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a   on failure..*/.
dda60 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 57  static int unixW
dda70 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f  rite(.  sqlite3_
dda80 66 69 6c 65 20 2a 69 64 2c 20 0a 20 20 63 6f 6e  file *id, .  con
dda90 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 0a  st void *pBuf, .
ddaa0 20 20 69 6e 74 20 61 6d 74 2c 0a 20 20 73 71 6c    int amt,.  sql
ddab0 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65  ite3_int64 offse
ddac0 74 20 0a 29 7b 0a 20 20 75 6e 69 78 46 69 6c 65  t .){.  unixFile
ddad0 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46   *pFile = (unixF
ddae0 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 77  ile*)id;.  int w
ddaf0 72 6f 74 65 20 3d 20 30 3b 0a 20 20 61 73 73 65  rote = 0;.  asse
ddb00 72 74 28 20 69 64 20 29 3b 0a 20 20 61 73 73 65  rt( id );.  asse
ddb10 72 74 28 20 61 6d 74 3e 30 20 29 3b 0a 0a 20 20  rt( amt>0 );..  
ddb20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20  /* If this is a 
ddb30 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 6e  database file (n
ddb40 6f 74 20 61 20 6a 6f 75 72 6e 61 6c 2c 20 6d 61  ot a journal, ma
ddb50 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6f 72 20  ster-journal or 
ddb60 74 65 6d 70 0a 20 20 2a 2a 20 66 69 6c 65 29 2c  temp.  ** file),
ddb70 20 74 68 65 20 62 79 74 65 73 20 69 6e 20 74 68   the bytes in th
ddb80 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 65 20  e locking range 
ddb90 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 20  should never be 
ddba0 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e 2e  read or written.
ddbb0 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 46   */.  assert( pF
ddbc0 69 6c 65 2d 3e 70 55 6e 75 73 65 64 3d 3d 30 0a  ile->pUnused==0.
ddbd0 20 20 20 20 20 20 20 7c 7c 20 6f 66 66 73 65 74         || offset
ddbe0 3e 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 35  >=PENDING_BYTE+5
ddbf0 31 32 0a 20 20 20 20 20 20 20 7c 7c 20 6f 66 66  12.       || off
ddc00 73 65 74 2b 61 6d 74 3c 3d 50 45 4e 44 49 4e 47  set+amt<=PENDING
ddc10 5f 42 59 54 45 20 0a 20 20 29 3b 0a 0a 23 69 66  _BYTE .  );..#if
ddc20 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a  ndef NDEBUG.  /*
ddc30 20 49 66 20 77 65 20 61 72 65 20 64 6f 69 6e 67   If we are doing
ddc40 20 61 20 6e 6f 72 6d 61 6c 20 77 72 69 74 65 20   a normal write 
ddc50 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 66 69  to a database fi
ddc60 6c 65 20 28 61 73 20 6f 70 70 6f 73 65 64 20 74  le (as opposed t
ddc70 6f 0a 20 20 2a 2a 20 64 6f 69 6e 67 20 61 20 68  o.  ** doing a h
ddc80 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62  ot-journal rollb
ddc90 61 63 6b 20 6f 72 20 61 20 77 72 69 74 65 20 74  ack or a write t
ddca0 6f 20 73 6f 6d 65 20 66 69 6c 65 20 6f 74 68 65  o some file othe
ddcb0 72 20 74 68 61 6e 20 61 0a 20 20 2a 2a 20 6e 6f  r than a.  ** no
ddcc0 72 6d 61 6c 20 64 61 74 61 62 61 73 65 20 66 69  rmal database fi
ddcd0 6c 65 29 20 74 68 65 6e 20 72 65 63 6f 72 64 20  le) then record 
ddce0 74 68 65 20 66 61 63 74 20 74 68 61 74 20 74 68  the fact that th
ddcf0 65 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20  e database.  ** 
ddd00 68 61 73 20 63 68 61 6e 67 65 64 2e 20 20 49 66  has changed.  If
ddd10 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
ddd20 20 63 6f 75 6e 74 65 72 20 69 73 20 6d 6f 64 69   counter is modi
ddd30 66 69 65 64 2c 20 72 65 63 6f 72 64 20 74 68 61  fied, record tha
ddd40 74 0a 20 20 2a 2a 20 66 61 63 74 20 74 6f 6f 2e  t.  ** fact too.
ddd50 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c  .  */.  if( pFil
ddd60 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65  e->inNormalWrite
ddd70 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 64   ){.    pFile->d
ddd80 62 55 70 64 61 74 65 20 3d 20 31 3b 20 20 2f 2a  bUpdate = 1;  /*
ddd90 20 54 68 65 20 64 61 74 61 62 61 73 65 20 68 61   The database ha
ddda0 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20  s been modified 
dddb0 2a 2f 0a 20 20 20 20 69 66 28 20 6f 66 66 73 65  */.    if( offse
dddc0 74 3c 3d 32 34 20 26 26 20 6f 66 66 73 65 74 2b  t<=24 && offset+
dddd0 61 6d 74 3e 3d 32 37 20 29 7b 0a 20 20 20 20 20  amt>=27 ){.     
ddde0 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 20 20 63   int rc;.      c
dddf0 68 61 72 20 6f 6c 64 43 6e 74 72 5b 34 5d 3b 0a  har oldCntr[4];.
dde00 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f        SimulateIO
dde10 45 72 72 6f 72 42 65 6e 69 67 6e 28 31 29 3b 0a  ErrorBenign(1);.
dde20 20 20 20 20 20 20 72 63 20 3d 20 73 65 65 6b 41        rc = seekA
dde30 6e 64 52 65 61 64 28 70 46 69 6c 65 2c 20 32 34  ndRead(pFile, 24
dde40 2c 20 6f 6c 64 43 6e 74 72 2c 20 34 29 3b 0a 20  , oldCntr, 4);. 
dde50 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45       SimulateIOE
dde60 72 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b 0a 20  rrorBenign(0);. 
dde70 20 20 20 20 20 69 66 28 20 72 63 21 3d 34 20 7c       if( rc!=4 |
dde80 7c 20 6d 65 6d 63 6d 70 28 6f 6c 64 43 6e 74 72  | memcmp(oldCntr
dde90 2c 20 26 28 28 63 68 61 72 2a 29 70 42 75 66 29  , &((char*)pBuf)
ddea0 5b 32 34 2d 6f 66 66 73 65 74 5d 2c 20 34 29 21  [24-offset], 4)!
ddeb0 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46  =0 ){.        pF
ddec0 69 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 72 43 68  ile->transCntrCh
dded0 6e 67 20 3d 20 31 3b 20 20 2f 2a 20 54 68 65 20  ng = 1;  /* The 
ddee0 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e  transaction coun
ddef0 74 65 72 20 68 61 73 20 63 68 61 6e 67 65 64 20  ter has changed 
ddf00 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  */.      }.    }
ddf10 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 77  .  }.#endif..  w
ddf20 68 69 6c 65 28 20 61 6d 74 3e 30 20 26 26 20 28  hile( amt>0 && (
ddf30 77 72 6f 74 65 20 3d 20 73 65 65 6b 41 6e 64 57  wrote = seekAndW
ddf40 72 69 74 65 28 70 46 69 6c 65 2c 20 6f 66 66 73  rite(pFile, offs
ddf50 65 74 2c 20 70 42 75 66 2c 20 61 6d 74 29 29 3e  et, pBuf, amt))>
ddf60 30 20 29 7b 0a 20 20 20 20 61 6d 74 20 2d 3d 20  0 ){.    amt -= 
ddf70 77 72 6f 74 65 3b 0a 20 20 20 20 6f 66 66 73 65  wrote;.    offse
ddf80 74 20 2b 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20  t += wrote;.    
ddf90 70 42 75 66 20 3d 20 26 28 28 63 68 61 72 2a 29  pBuf = &((char*)
ddfa0 70 42 75 66 29 5b 77 72 6f 74 65 5d 3b 0a 20 20  pBuf)[wrote];.  
ddfb0 7d 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  }.  SimulateIOEr
ddfc0 72 6f 72 28 28 20 77 72 6f 74 65 3d 28 2d 31 29  ror(( wrote=(-1)
ddfd0 2c 20 61 6d 74 3d 31 20 29 29 3b 0a 20 20 53 69  , amt=1 ));.  Si
ddfe0 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72  mulateDiskfullEr
ddff0 72 6f 72 28 28 20 77 72 6f 74 65 3d 30 2c 20 61  ror(( wrote=0, a
de000 6d 74 3d 31 20 29 29 3b 0a 20 20 69 66 28 20 61  mt=1 ));.  if( a
de010 6d 74 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20  mt>0 ){.    if( 
de020 77 72 6f 74 65 3c 30 20 29 7b 0a 20 20 20 20 20  wrote<0 ){.     
de030 20 2f 2a 20 6c 61 73 74 45 72 72 6e 6f 20 73 65   /* lastErrno se
de040 74 20 62 79 20 73 65 65 6b 41 6e 64 57 72 69 74  t by seekAndWrit
de050 65 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72  e */.      retur
de060 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57  n SQLITE_IOERR_W
de070 52 49 54 45 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  RITE;.    }else{
de080 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61  .      pFile->la
de090 73 74 45 72 72 6e 6f 20 3d 20 30 3b 20 2f 2a 20  stErrno = 0; /* 
de0a0 6e 6f 74 20 61 20 73 79 73 74 65 6d 20 65 72 72  not a system err
de0b0 6f 72 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75  or */.      retu
de0c0 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a  rn SQLITE_FULL;.
de0d0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
de0e0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
de0f0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
de100 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20  EST./*.** Count 
de110 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 75  the number of fu
de120 6c 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d  llsyncs and norm
de130 61 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 73 20  al syncs.  This 
de140 69 73 20 75 73 65 64 20 74 6f 20 74 65 73 74 0a  is used to test.
de150 2a 2a 20 74 68 61 74 20 73 79 6e 63 73 20 61 6e  ** that syncs an
de160 64 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 20  d fullsyncs are 
de170 6f 63 63 75 72 72 69 6e 67 20 61 74 20 74 68 65  occurring at the
de180 20 72 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f   right times..*/
de190 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
de1a0 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75  sqlite3_sync_cou
de1b0 6e 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41  nt = 0;.SQLITE_A
de1c0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66  PI int sqlite3_f
de1d0 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20  ullsync_count = 
de1e0 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  0;.#endif../*.**
de1f0 20 57 65 20 64 6f 20 6e 6f 74 20 74 72 75 73 74   We do not trust
de200 20 73 79 73 74 65 6d 73 20 74 6f 20 70 72 6f 76   systems to prov
de210 69 64 65 20 61 20 77 6f 72 6b 69 6e 67 20 66 64  ide a working fd
de220 61 74 61 73 79 6e 63 28 29 2e 20 20 53 6f 6d 65  atasync().  Some
de230 20 64 6f 2e 0a 2a 2a 20 4f 74 68 65 72 73 20 64   do..** Others d
de240 6f 20 6e 6f 2e 20 20 54 6f 20 62 65 20 73 61 66  o no.  To be saf
de250 65 2c 20 77 65 20 77 69 6c 6c 20 73 74 69 63 6b  e, we will stick
de260 20 77 69 74 68 20 74 68 65 20 28 73 6c 6f 77 65   with the (slowe
de270 72 29 20 66 73 79 6e 63 28 29 2e 0a 2a 2a 20 49  r) fsync()..** I
de280 66 20 79 6f 75 20 6b 6e 6f 77 20 74 68 61 74 20  f you know that 
de290 79 6f 75 72 20 73 79 73 74 65 6d 20 64 6f 65 73  your system does
de2a0 20 73 75 70 70 6f 72 74 20 66 64 61 74 61 73 79   support fdatasy
de2b0 6e 63 28 29 20 63 6f 72 72 65 63 74 6c 79 2c 0a  nc() correctly,.
de2c0 2a 2a 20 74 68 65 6e 20 73 69 6d 70 6c 79 20 63  ** then simply c
de2d0 6f 6d 70 69 6c 65 20 77 69 74 68 20 2d 44 66 64  ompile with -Dfd
de2e0 61 74 61 73 79 6e 63 3d 66 64 61 74 61 73 79 6e  atasync=fdatasyn
de2f0 63 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65  c.*/.#if !define
de300 64 28 66 64 61 74 61 73 79 6e 63 29 20 26 26 20  d(fdatasync) && 
de310 21 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78  !defined(__linux
de320 5f 5f 29 0a 23 20 64 65 66 69 6e 65 20 66 64 61  __).# define fda
de330 74 61 73 79 6e 63 20 66 73 79 6e 63 0a 23 65 6e  tasync fsync.#en
de340 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e  dif../*.** Defin
de350 65 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43  e HAVE_FULLFSYNC
de360 20 74 6f 20 30 20 6f 72 20 31 20 64 65 70 65 6e   to 0 or 1 depen
de370 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 72 20  ding on whether 
de380 6f 72 20 6e 6f 74 0a 2a 2a 20 74 68 65 20 46 5f  or not.** the F_
de390 46 55 4c 4c 46 53 59 4e 43 20 6d 61 63 72 6f 20  FULLFSYNC macro 
de3a0 69 73 20 64 65 66 69 6e 65 64 2e 20 20 46 5f 46  is defined.  F_F
de3b0 55 4c 4c 46 53 59 4e 43 20 69 73 20 63 75 72 72  ULLFSYNC is curr
de3c0 65 6e 74 6c 79 0a 2a 2a 20 6f 6e 6c 79 20 61 76  ently.** only av
de3d0 61 69 6c 61 62 6c 65 20 6f 6e 20 4d 61 63 20 4f  ailable on Mac O
de3e0 53 20 58 2e 20 20 42 75 74 20 74 68 61 74 20 63  S X.  But that c
de3f0 6f 75 6c 64 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a  ould change..*/.
de400 23 69 66 64 65 66 20 46 5f 46 55 4c 4c 46 53 59  #ifdef F_FULLFSY
de410 4e 43 0a 23 20 64 65 66 69 6e 65 20 48 41 56 45  NC.# define HAVE
de420 5f 46 55 4c 4c 46 53 59 4e 43 20 31 0a 23 65 6c  _FULLFSYNC 1.#el
de430 73 65 0a 23 20 64 65 66 69 6e 65 20 48 41 56 45  se.# define HAVE
de440 5f 46 55 4c 4c 46 53 59 4e 43 20 30 0a 23 65 6e  _FULLFSYNC 0.#en
de450 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  dif.../*.** The 
de460 66 73 79 6e 63 28 29 20 73 79 73 74 65 6d 20 63  fsync() system c
de470 61 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72  all does not wor
de480 6b 20 61 73 20 61 64 76 65 72 74 69 73 65 64 20  k as advertised 
de490 6f 6e 20 6d 61 6e 79 0a 2a 2a 20 75 6e 69 78 20  on many.** unix 
de4a0 73 79 73 74 65 6d 73 2e 20 20 54 68 65 20 66 6f  systems.  The fo
de4b0 6c 6c 6f 77 69 6e 67 20 70 72 6f 63 65 64 75 72  llowing procedur
de4c0 65 20 69 73 20 61 6e 20 61 74 74 65 6d 70 74 20  e is an attempt 
de4d0 74 6f 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 77 6f  to make.** it wo
de4e0 72 6b 20 62 65 74 74 65 72 2e 0a 2a 2a 0a 2a 2a  rk better..**.**
de4f0 20 54 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53   The SQLITE_NO_S
de500 59 4e 43 20 6d 61 63 72 6f 20 64 69 73 61 62 6c  YNC macro disabl
de510 65 73 20 61 6c 6c 20 66 73 79 6e 63 28 29 73 2e  es all fsync()s.
de520 20 20 54 68 69 73 20 69 73 20 75 73 65 66 75 6c    This is useful
de530 0a 2a 2a 20 66 6f 72 20 74 65 73 74 69 6e 67 20  .** for testing 
de540 77 68 65 6e 20 77 65 20 77 61 6e 74 20 74 6f 20  when we want to 
de550 72 75 6e 20 74 68 72 6f 75 67 68 20 74 68 65 20  run through the 
de560 74 65 73 74 20 73 75 69 74 65 20 71 75 69 63 6b  test suite quick
de570 6c 79 2e 0a 2a 2a 20 59 6f 75 20 61 72 65 20 73  ly..** You are s
de580 74 72 6f 6e 67 6c 79 20 61 64 76 69 73 65 64 20  trongly advised 
de590 2a 6e 6f 74 2a 20 74 6f 20 64 65 70 6c 6f 79 20  *not* to deploy 
de5a0 77 69 74 68 20 53 51 4c 49 54 45 5f 4e 4f 5f 53  with SQLITE_NO_S
de5b0 59 4e 43 0a 2a 2a 20 65 6e 61 62 6c 65 64 2c 20  YNC.** enabled, 
de5c0 68 6f 77 65 76 65 72 2c 20 73 69 6e 63 65 20 77  however, since w
de5d0 69 74 68 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59  ith SQLITE_NO_SY
de5e0 4e 43 20 65 6e 61 62 6c 65 64 2c 20 61 6e 20 4f  NC enabled, an O
de5f0 53 20 63 72 61 73 68 0a 2a 2a 20 6f 72 20 70 6f  S crash.** or po
de600 77 65 72 20 66 61 69 6c 75 72 65 20 77 69 6c 6c  wer failure will
de610 20 6c 69 6b 65 6c 79 20 63 6f 72 72 75 70 74 20   likely corrupt 
de620 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
de630 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  e..**.** SQLite 
de640 73 65 74 73 20 74 68 65 20 64 61 74 61 4f 6e 6c  sets the dataOnl
de650 79 20 66 6c 61 67 20 69 66 20 74 68 65 20 73 69  y flag if the si
de660 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69  ze of the file i
de670 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 20  s unchanged..** 
de680 54 68 65 20 69 64 65 61 20 62 65 68 69 6e 64 20  The idea behind 
de690 64 61 74 61 4f 6e 6c 79 20 69 73 20 74 68 61 74  dataOnly is that
de6a0 20 69 74 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20   it should only 
de6b0 77 72 69 74 65 20 74 68 65 20 66 69 6c 65 20 63  write the file c
de6c0 6f 6e 74 65 6e 74 0a 2a 2a 20 74 6f 20 64 69 73  ontent.** to dis
de6d0 6b 2c 20 6e 6f 74 20 74 68 65 20 69 6e 6f 64 65  k, not the inode
de6e0 2e 20 20 57 65 20 6f 6e 6c 79 20 73 65 74 20 64  .  We only set d
de6f0 61 74 61 4f 6e 6c 79 20 69 66 20 74 68 65 20 66  ataOnly if the f
de700 69 6c 65 20 73 69 7a 65 20 69 73 20 0a 2a 2a 20  ile size is .** 
de710 75 6e 63 68 61 6e 67 65 64 20 73 69 6e 63 65 20  unchanged since 
de720 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 69 73  the file size is
de730 20 70 61 72 74 20 6f 66 20 74 68 65 20 69 6e 6f   part of the ino
de740 64 65 2e 20 20 48 6f 77 65 76 65 72 2c 20 0a 2a  de.  However, .*
de750 2a 20 54 65 64 20 54 73 27 6f 20 74 65 6c 6c 73  * Ted Ts'o tells
de760 20 75 73 20 74 68 61 74 20 66 64 61 74 61 73 79   us that fdatasy
de770 6e 63 28 29 20 77 69 6c 6c 20 61 6c 73 6f 20 77  nc() will also w
de780 72 69 74 65 20 74 68 65 20 69 6e 6f 64 65 20 69  rite the inode i
de790 66 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 73 69  f the.** file si
de7a0 7a 65 20 68 61 73 20 63 68 61 6e 67 65 64 2e 20  ze has changed. 
de7b0 20 54 68 65 20 6f 6e 6c 79 20 72 65 61 6c 20 64   The only real d
de7c0 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65  ifference betwee
de7d0 6e 20 66 64 61 74 61 73 79 6e 63 28 29 0a 2a 2a  n fdatasync().**
de7e0 20 61 6e 64 20 66 73 79 6e 63 28 29 2c 20 54 65   and fsync(), Te
de7f0 64 20 74 65 6c 6c 73 20 75 73 2c 20 69 73 20 74  d tells us, is t
de800 68 61 74 20 66 64 61 74 61 73 79 6e 63 28 29 20  hat fdatasync() 
de810 77 69 6c 6c 20 6e 6f 74 20 66 6c 75 73 68 20 74  will not flush t
de820 68 65 0a 2a 2a 20 69 6e 6f 64 65 20 69 66 20 74  he.** inode if t
de830 68 65 20 6d 74 69 6d 65 20 6f 72 20 6f 77 6e 65  he mtime or owne
de840 72 20 6f 72 20 6f 74 68 65 72 20 69 6e 6f 64 65  r or other inode
de850 20 61 74 74 72 69 62 75 74 65 73 20 68 61 76 65   attributes have
de860 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 20 57 65 20   changed..** We 
de870 6f 6e 6c 79 20 63 61 72 65 20 61 62 6f 75 74 20  only care about 
de880 74 68 65 20 66 69 6c 65 20 73 69 7a 65 2c 20 6e  the file size, n
de890 6f 74 20 74 68 65 20 6f 74 68 65 72 20 66 69 6c  ot the other fil
de8a0 65 20 61 74 74 72 69 62 75 74 65 73 2c 20 73 6f  e attributes, so
de8b0 0a 2a 2a 20 61 73 20 66 61 72 20 61 73 20 53 51  .** as far as SQ
de8c0 4c 69 74 65 20 69 73 20 63 6f 6e 63 65 72 6e 65  Lite is concerne
de8d0 64 2c 20 61 6e 20 66 64 61 74 61 73 79 6e 63 28  d, an fdatasync(
de8e0 29 20 69 73 20 61 6c 77 61 79 73 20 61 64 65 71  ) is always adeq
de8f0 75 61 74 65 2e 0a 2a 2a 20 53 6f 2c 20 77 65 20  uate..** So, we 
de900 61 6c 77 61 79 73 20 75 73 65 20 66 64 61 74 61  always use fdata
de910 73 79 6e 63 28 29 20 69 66 20 69 74 20 69 73 20  sync() if it is 
de920 61 76 61 69 6c 61 62 6c 65 2c 20 72 65 67 61 72  available, regar
de930 64 6c 65 73 73 20 6f 66 0a 2a 2a 20 74 68 65 20  dless of.** the 
de940 76 61 6c 75 65 20 6f 66 20 74 68 65 20 64 61 74  value of the dat
de950 61 4f 6e 6c 79 20 66 6c 61 67 2e 0a 2a 2f 0a 73  aOnly flag..*/.s
de960 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 5f 66  tatic int full_f
de970 73 79 6e 63 28 69 6e 74 20 66 64 2c 20 69 6e 74  sync(int fd, int
de980 20 66 75 6c 6c 53 79 6e 63 2c 20 69 6e 74 20 64   fullSync, int d
de990 61 74 61 4f 6e 6c 79 29 7b 0a 20 20 69 6e 74 20  ataOnly){.  int 
de9a0 72 63 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f  rc;..  /* The fo
de9b0 6c 6c 6f 77 69 6e 67 20 22 69 66 64 65 66 2f 65  llowing "ifdef/e
de9c0 6c 69 66 2f 65 6c 73 65 2f 22 20 62 6c 6f 63 6b  lif/else/" block
de9d0 20 68 61 73 20 74 68 65 20 73 61 6d 65 20 73 74   has the same st
de9e0 72 75 63 74 75 72 65 20 61 73 0a 20 20 2a 2a 20  ructure as.  ** 
de9f0 74 68 65 20 6f 6e 65 20 62 65 6c 6f 77 2e 20 49  the one below. I
dea00 74 20 69 73 20 72 65 70 6c 69 63 61 74 65 64 20  t is replicated 
dea10 68 65 72 65 20 73 6f 6c 65 6c 79 20 74 6f 20 61  here solely to a
dea20 76 6f 69 64 20 63 6c 75 74 74 65 72 69 6e 67 20  void cluttering 
dea30 0a 20 20 2a 2a 20 75 70 20 74 68 65 20 72 65 61  .  ** up the rea
dea40 6c 20 63 6f 64 65 20 77 69 74 68 20 74 68 65 20  l code with the 
dea50 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
dea60 28 29 20 6d 61 63 72 6f 73 2e 0a 20 20 2a 2f 0a  () macros..  */.
dea70 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f  #ifdef SQLITE_NO
dea80 5f 53 59 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50  _SYNC.  UNUSED_P
dea90 41 52 41 4d 45 54 45 52 28 66 64 29 3b 0a 20 20  ARAMETER(fd);.  
deaa0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
deab0 28 66 75 6c 6c 53 79 6e 63 29 3b 0a 20 20 55 4e  (fullSync);.  UN
deac0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 64  USED_PARAMETER(d
dead0 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 6c 69 66 20  ataOnly);.#elif 
deae0 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 0a 20  HAVE_FULLFSYNC. 
deaf0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
deb00 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 6c  R(dataOnly);.#el
deb10 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  se.  UNUSED_PARA
deb20 4d 45 54 45 52 28 66 75 6c 6c 53 79 6e 63 29 3b  METER(fullSync);
deb30 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
deb40 54 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23  TER(dataOnly);.#
deb50 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 63 6f  endif..  /* Reco
deb60 72 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rd the number of
deb70 20 74 69 6d 65 73 20 74 68 61 74 20 77 65 20 64   times that we d
deb80 6f 20 61 20 6e 6f 72 6d 61 6c 20 66 73 79 6e 63  o a normal fsync
deb90 28 29 20 61 6e 64 20 0a 20 20 2a 2a 20 46 55 4c  () and .  ** FUL
deba0 4c 53 59 4e 43 2e 20 20 54 68 69 73 20 69 73 20  LSYNC.  This is 
debb0 75 73 65 64 20 64 75 72 69 6e 67 20 74 65 73 74  used during test
debc0 69 6e 67 20 74 6f 20 76 65 72 69 66 79 20 74 68  ing to verify th
debd0 61 74 20 74 68 69 73 20 70 72 6f 63 65 64 75 72  at this procedur
debe0 65 0a 20 20 2a 2a 20 67 65 74 73 20 63 61 6c 6c  e.  ** gets call
debf0 65 64 20 77 69 74 68 20 74 68 65 20 63 6f 72 72  ed with the corr
dec00 65 63 74 20 61 72 67 75 6d 65 6e 74 73 2e 0a 20  ect arguments.. 
dec10 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54   */.#ifdef SQLIT
dec20 45 5f 54 45 53 54 0a 20 20 69 66 28 20 66 75 6c  E_TEST.  if( ful
dec30 6c 53 79 6e 63 20 29 20 73 71 6c 69 74 65 33 5f  lSync ) sqlite3_
dec40 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b  fullsync_count++
dec50 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 79 6e 63  ;.  sqlite3_sync
dec60 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66  _count++;.#endif
dec70 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 63 6f 6d  ..  /* If we com
dec80 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 20 53  piled with the S
dec90 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 66 6c  QLITE_NO_SYNC fl
deca0 61 67 2c 20 74 68 65 6e 20 73 79 6e 63 69 6e 67  ag, then syncing
decb0 20 69 73 20 61 0a 20 20 2a 2a 20 6e 6f 2d 6f 70   is a.  ** no-op
decc0 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  .  */.#ifdef SQL
decd0 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 72 63  ITE_NO_SYNC.  rc
dece0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65   = SQLITE_OK;.#e
decf0 6c 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 59  lif HAVE_FULLFSY
ded00 4e 43 0a 20 20 69 66 28 20 66 75 6c 6c 53 79 6e  NC.  if( fullSyn
ded10 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 66 63  c ){.    rc = fc
ded20 6e 74 6c 28 66 64 2c 20 46 5f 46 55 4c 4c 46 53  ntl(fd, F_FULLFS
ded30 59 4e 43 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65  YNC, 0);.  }else
ded40 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20  {.    rc = 1;.  
ded50 7d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 46 55  }.  /* If the FU
ded60 4c 4c 46 53 59 4e 43 20 66 61 69 6c 65 64 2c 20  LLFSYNC failed, 
ded70 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 61 74 74  fall back to att
ded80 65 6d 70 74 69 6e 67 20 61 6e 20 66 73 79 6e 63  empting an fsync
ded90 28 29 2e 0a 20 20 2a 2a 20 49 74 20 73 68 6f 75  ()..  ** It shou
deda0 6c 64 6e 27 74 20 62 65 20 70 6f 73 73 69 62 6c  ldn't be possibl
dedb0 65 20 66 6f 72 20 66 75 6c 6c 66 73 79 6e 63 20  e for fullfsync 
dedc0 74 6f 20 66 61 69 6c 20 6f 6e 20 74 68 65 20 6c  to fail on the l
dedd0 6f 63 61 6c 20 0a 20 20 2a 2a 20 66 69 6c 65 20  ocal .  ** file 
dede0 73 79 73 74 65 6d 20 28 6f 6e 20 4f 53 58 29 2c  system (on OSX),
dedf0 20 73 6f 20 66 61 69 6c 75 72 65 20 69 6e 64 69   so failure indi
dee00 63 61 74 65 73 20 74 68 61 74 20 46 55 4c 4c 46  cates that FULLF
dee10 53 59 4e 43 0a 20 20 2a 2a 20 69 73 6e 27 74 20  SYNC.  ** isn't 
dee20 73 75 70 70 6f 72 74 65 64 20 66 6f 72 20 74 68  supported for th
dee30 69 73 20 66 69 6c 65 20 73 79 73 74 65 6d 2e 20  is file system. 
dee40 53 6f 2c 20 61 74 74 65 6d 70 74 20 61 6e 20 66  So, attempt an f
dee50 73 79 6e 63 20 0a 20 20 2a 2a 20 61 6e 64 20 28  sync .  ** and (
dee60 66 6f 72 20 6e 6f 77 29 20 69 67 6e 6f 72 65 20  for now) ignore 
dee70 74 68 65 20 6f 76 65 72 68 65 61 64 20 6f 66 20  the overhead of 
dee80 61 20 73 75 70 65 72 66 6c 75 6f 75 73 20 66 63  a superfluous fc
dee90 6e 74 6c 20 63 61 6c 6c 2e 20 20 0a 20 20 2a 2a  ntl call.  .  **
deea0 20 49 74 27 64 20 62 65 20 62 65 74 74 65 72 20   It'd be better 
deeb0 74 6f 20 64 65 74 65 63 74 20 66 75 6c 6c 66 73  to detect fullfs
deec0 79 6e 63 20 73 75 70 70 6f 72 74 20 6f 6e 63 65  ync support once
deed0 20 61 6e 64 20 61 76 6f 69 64 20 0a 20 20 2a 2a   and avoid .  **
deee0 20 74 68 65 20 66 63 6e 74 6c 20 63 61 6c 6c 20   the fcntl call 
deef0 65 76 65 72 79 20 74 69 6d 65 20 73 79 6e 63 20  every time sync 
def00 69 73 20 63 61 6c 6c 65 64 2e 0a 20 20 2a 2f 0a  is called..  */.
def10 20 20 69 66 28 20 72 63 20 29 20 72 63 20 3d 20    if( rc ) rc = 
def20 66 73 79 6e 63 28 66 64 29 3b 0a 0a 23 65 6c 73  fsync(fd);..#els
def30 65 20 0a 20 20 72 63 20 3d 20 66 64 61 74 61 73  e .  rc = fdatas
def40 79 6e 63 28 66 64 29 3b 0a 23 69 66 20 4f 53 5f  ync(fd);.#if OS_
def50 56 58 57 4f 52 4b 53 0a 20 20 69 66 28 20 72 63  VXWORKS.  if( rc
def60 3d 3d 2d 31 20 26 26 20 65 72 72 6e 6f 3d 3d 45  ==-1 && errno==E
def70 4e 4f 54 53 55 50 20 29 7b 0a 20 20 20 20 72 63  NOTSUP ){.    rc
def80 20 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 20 20   = fsync(fd);.  
def90 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56  }.#endif /* OS_V
defa0 58 57 4f 52 4b 53 20 2a 2f 0a 23 65 6e 64 69 66  XWORKS */.#endif
defb0 20 2f 2a 20 69 66 64 65 66 20 53 51 4c 49 54 45   /* ifdef SQLITE
defc0 5f 4e 4f 5f 53 59 4e 43 20 65 6c 69 66 20 48 41  _NO_SYNC elif HA
defd0 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 2a 2f 0a  VE_FULLFSYNC */.
defe0 0a 20 20 69 66 28 20 4f 53 5f 56 58 57 4f 52 4b  .  if( OS_VXWORK
deff0 53 20 26 26 20 72 63 21 3d 20 2d 31 20 29 7b 0a  S && rc!= -1 ){.
df000 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 7d 0a      rc = 0;.  }.
df010 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
df020 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20  /*.** Make sure 
df030 61 6c 6c 20 77 72 69 74 65 73 20 74 6f 20 61 20  all writes to a 
df040 70 61 72 74 69 63 75 6c 61 72 20 66 69 6c 65 20  particular file 
df050 61 72 65 20 63 6f 6d 6d 69 74 74 65 64 20 74 6f  are committed to
df060 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   disk..**.** If 
df070 64 61 74 61 4f 6e 6c 79 3d 3d 30 20 74 68 65 6e  dataOnly==0 then
df080 20 62 6f 74 68 20 74 68 65 20 66 69 6c 65 20 69   both the file i
df090 74 73 65 6c 66 20 61 6e 64 20 69 74 73 20 6d 65  tself and its me
df0a0 74 61 64 61 74 61 20 28 66 69 6c 65 0a 2a 2a 20  tadata (file.** 
df0b0 73 69 7a 65 2c 20 61 63 63 65 73 73 20 74 69 6d  size, access tim
df0c0 65 2c 20 65 74 63 29 20 61 72 65 20 73 79 6e 63  e, etc) are sync
df0d0 65 64 2e 20 20 49 66 20 64 61 74 61 4f 6e 6c 79  ed.  If dataOnly
df0e0 21 3d 30 20 74 68 65 6e 20 6f 6e 6c 79 20 74 68  !=0 then only th
df0f0 65 0a 2a 2a 20 66 69 6c 65 20 64 61 74 61 20 69  e.** file data i
df100 73 20 73 79 6e 63 65 64 2e 0a 2a 2a 0a 2a 2a 20  s synced..**.** 
df110 55 6e 64 65 72 20 55 6e 69 78 2c 20 61 6c 73 6f  Under Unix, also
df120 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20   make sure that 
df130 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 65 6e  the directory en
df140 74 72 79 20 66 6f 72 20 74 68 65 20 66 69 6c 65  try for the file
df150 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 63 72 65  .** has been cre
df160 61 74 65 64 20 62 79 20 66 73 79 6e 63 2d 69 6e  ated by fsync-in
df170 67 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20  g the directory 
df180 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68  that contains th
df190 65 20 66 69 6c 65 2e 0a 2a 2a 20 49 66 20 77 65  e file..** If we
df1a0 20 64 6f 20 6e 6f 74 20 64 6f 20 74 68 69 73 20   do not do this 
df1b0 61 6e 64 20 77 65 20 65 6e 63 6f 75 6e 74 65 72  and we encounter
df1c0 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65   a power failure
df1d0 2c 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 0a  , the directory.
df1e0 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65  ** entry for the
df1f0 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68 74 20 6e   journal might n
df200 6f 74 20 65 78 69 73 74 20 61 66 74 65 72 20 77  ot exist after w
df210 65 20 72 65 62 6f 6f 74 2e 20 20 54 68 65 20 6e  e reboot.  The n
df220 65 78 74 0a 2a 2a 20 53 51 4c 69 74 65 20 74 6f  ext.** SQLite to
df230 20 61 63 63 65 73 73 20 74 68 65 20 66 69 6c 65   access the file
df240 20 77 69 6c 6c 20 6e 6f 74 20 6b 6e 6f 77 20 74   will not know t
df250 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  hat the journal 
df260 65 78 69 73 74 73 20 28 62 65 63 61 75 73 65 0a  exists (because.
df270 2a 2a 20 74 68 65 20 64 69 72 65 63 74 6f 72 79  ** the directory
df280 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6a   entry for the j
df290 6f 75 72 6e 61 6c 20 77 61 73 20 6e 65 76 65 72  ournal was never
df2a0 20 63 72 65 61 74 65 64 29 20 61 6e 64 20 74 68   created) and th
df2b0 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a  e transaction.**
df2c0 20 77 69 6c 6c 20 6e 6f 74 20 72 6f 6c 6c 20 62   will not roll b
df2d0 61 63 6b 20 2d 20 70 6f 73 73 69 62 6c 79 20 6c  ack - possibly l
df2e0 65 61 64 69 6e 67 20 74 6f 20 64 61 74 61 62 61  eading to databa
df2f0 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a  se corruption..*
df300 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
df310 78 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69  xSync(sqlite3_fi
df320 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67  le *id, int flag
df330 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  s){.  int rc;.  
df340 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
df350 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b  = (unixFile*)id;
df360 0a 0a 20 20 69 6e 74 20 69 73 44 61 74 61 4f 6e  ..  int isDataOn
df370 6c 79 20 3d 20 28 66 6c 61 67 73 26 53 51 4c 49  ly = (flags&SQLI
df380 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59  TE_SYNC_DATAONLY
df390 29 3b 0a 20 20 69 6e 74 20 69 73 46 75 6c 6c 73  );.  int isFulls
df3a0 79 6e 63 20 3d 20 28 66 6c 61 67 73 26 30 78 30  ync = (flags&0x0
df3b0 46 29 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f  F)==SQLITE_SYNC_
df3c0 46 55 4c 4c 3b 0a 0a 20 20 2f 2a 20 43 68 65 63  FULL;..  /* Chec
df3d0 6b 20 74 68 61 74 20 6f 6e 65 20 6f 66 20 53 51  k that one of SQ
df3e0 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c  LITE_SYNC_NORMAL
df3f0 20 6f 72 20 46 55 4c 4c 20 77 61 73 20 70 61 73   or FULL was pas
df400 73 65 64 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  sed */.  assert(
df410 28 66 6c 61 67 73 26 30 78 30 46 29 3d 3d 53 51  (flags&0x0F)==SQ
df420 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c  LITE_SYNC_NORMAL
df430 0a 20 20 20 20 20 20 7c 7c 20 28 66 6c 61 67 73  .      || (flags
df440 26 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f 53  &0x0F)==SQLITE_S
df450 59 4e 43 5f 46 55 4c 4c 0a 20 20 29 3b 0a 0a 20  YNC_FULL.  );.. 
df460 20 2f 2a 20 55 6e 69 78 20 63 61 6e 6e 6f 74 2c   /* Unix cannot,
df470 20 62 75 74 20 73 6f 6d 65 20 73 79 73 74 65 6d   but some system
df480 73 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c  s may return SQL
df490 49 54 45 5f 46 55 4c 4c 20 66 72 6f 6d 20 68 65  ITE_FULL from he
df4a0 72 65 2e 20 54 68 69 73 0a 20 20 2a 2a 20 6c 69  re. This.  ** li
df4b0 6e 65 20 69 73 20 74 6f 20 74 65 73 74 20 74 68  ne is to test th
df4c0 61 74 20 64 6f 69 6e 67 20 73 6f 20 64 6f 65 73  at doing so does
df4d0 20 6e 6f 74 20 63 61 75 73 65 20 61 6e 79 20 70   not cause any p
df4e0 72 6f 62 6c 65 6d 73 2e 0a 20 20 2a 2f 0a 20 20  roblems..  */.  
df4f0 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c  SimulateDiskfull
df500 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51  Error( return SQ
df510 4c 49 54 45 5f 46 55 4c 4c 20 29 3b 0a 0a 20 20  LITE_FULL );..  
df520 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b  assert( pFile );
df530 0a 20 20 4f 53 54 52 41 43 45 32 28 22 53 59 4e  .  OSTRACE2("SYN
df540 43 20 20 20 20 25 2d 33 64 5c 6e 22 2c 20 70 46  C    %-3d\n", pF
df550 69 6c 65 2d 3e 68 29 3b 0a 20 20 72 63 20 3d 20  ile->h);.  rc = 
df560 66 75 6c 6c 5f 66 73 79 6e 63 28 70 46 69 6c 65  full_fsync(pFile
df570 2d 3e 68 2c 20 69 73 46 75 6c 6c 73 79 6e 63 2c  ->h, isFullsync,
df580 20 69 73 44 61 74 61 4f 6e 6c 79 29 3b 0a 20 20   isDataOnly);.  
df590 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
df5a0 20 72 63 3d 31 20 29 3b 0a 20 20 69 66 28 20 72   rc=1 );.  if( r
df5b0 63 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  c ){.    pFile->
df5c0 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  lastErrno = errn
df5d0 6f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  o;.    return SQ
df5e0 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 59 4e 43  LITE_IOERR_FSYNC
df5f0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 46 69 6c  ;.  }.  if( pFil
df600 65 2d 3e 64 69 72 66 64 3e 3d 30 20 29 7b 0a 20  e->dirfd>=0 ){. 
df610 20 20 20 69 6e 74 20 65 72 72 3b 0a 20 20 20 20     int err;.    
df620 4f 53 54 52 41 43 45 34 28 22 44 49 52 53 59 4e  OSTRACE4("DIRSYN
df630 43 20 25 2d 33 64 20 28 68 61 76 65 5f 66 75 6c  C %-3d (have_ful
df640 6c 66 73 79 6e 63 3d 25 64 20 66 75 6c 6c 73 79  lfsync=%d fullsy
df650 6e 63 3d 25 64 29 5c 6e 22 2c 20 70 46 69 6c 65  nc=%d)\n", pFile
df660 2d 3e 64 69 72 66 64 2c 0a 20 20 20 20 20 20 20  ->dirfd,.       
df670 20 20 20 20 20 48 41 56 45 5f 46 55 4c 4c 46 53       HAVE_FULLFS
df680 59 4e 43 2c 20 69 73 46 75 6c 6c 73 79 6e 63 29  YNC, isFullsync)
df690 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
df6a0 5f 44 49 53 41 42 4c 45 5f 44 49 52 53 59 4e 43  _DISABLE_DIRSYNC
df6b0 0a 20 20 20 20 2f 2a 20 54 68 65 20 64 69 72 65  .    /* The dire
df6c0 63 74 6f 72 79 20 73 79 6e 63 20 69 73 20 6f 6e  ctory sync is on
df6d0 6c 79 20 61 74 74 65 6d 70 74 65 64 20 69 66 20  ly attempted if 
df6e0 66 75 6c 6c 5f 66 73 79 6e 63 20 69 73 0a 20 20  full_fsync is.  
df6f0 20 20 2a 2a 20 74 75 72 6e 65 64 20 6f 66 66 20    ** turned off 
df700 6f 72 20 75 6e 61 76 61 69 6c 61 62 6c 65 2e 20  or unavailable. 
df710 20 49 66 20 61 20 66 75 6c 6c 5f 66 73 79 6e 63   If a full_fsync
df720 20 6f 63 63 75 72 72 65 64 20 61 62 6f 76 65 2c   occurred above,
df730 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65  .    ** then the
df740 20 64 69 72 65 63 74 6f 72 79 20 73 79 6e 63 20   directory sync 
df750 69 73 20 73 75 70 65 72 66 6c 75 6f 75 73 2e 0a  is superfluous..
df760 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 28      */.    if( (
df770 21 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20  !HAVE_FULLFSYNC 
df780 7c 7c 20 21 69 73 46 75 6c 6c 73 79 6e 63 29 20  || !isFullsync) 
df790 26 26 20 66 75 6c 6c 5f 66 73 79 6e 63 28 70 46  && full_fsync(pF
df7a0 69 6c 65 2d 3e 64 69 72 66 64 2c 30 2c 30 29 20  ile->dirfd,0,0) 
df7b0 29 7b 0a 20 20 20 20 20 20 20 2f 2a 0a 20 20 20  ){.       /*.   
df7c0 20 20 20 20 2a 2a 20 57 65 20 68 61 76 65 20 72      ** We have r
df7d0 65 63 65 69 76 65 64 20 6d 75 6c 74 69 70 6c 65  eceived multiple
df7e0 20 72 65 70 6f 72 74 73 20 6f 66 20 66 73 79 6e   reports of fsyn
df7f0 63 28 29 20 72 65 74 75 72 6e 69 6e 67 0a 20 20  c() returning.  
df800 20 20 20 20 20 2a 2a 20 65 72 72 6f 72 73 20 77       ** errors w
df810 68 65 6e 20 61 70 70 6c 69 65 64 20 74 6f 20 64  hen applied to d
df820 69 72 65 63 74 6f 72 69 65 73 20 6f 6e 20 63 65  irectories on ce
df830 72 74 61 69 6e 20 66 69 6c 65 20 73 79 73 74 65  rtain file syste
df840 6d 73 2e 0a 20 20 20 20 20 20 20 2a 2a 20 41 20  ms..       ** A 
df850 66 61 69 6c 65 64 20 64 69 72 65 63 74 6f 72 79  failed directory
df860 20 73 79 6e 63 20 69 73 20 6e 6f 74 20 61 20 62   sync is not a b
df870 69 67 20 64 65 61 6c 2e 20 20 53 6f 20 69 74 20  ig deal.  So it 
df880 73 65 65 6d 73 0a 20 20 20 20 20 20 20 2a 2a 20  seems.       ** 
df890 62 65 74 74 65 72 20 74 6f 20 69 67 6e 6f 72 65  better to ignore
df8a0 20 74 68 65 20 65 72 72 6f 72 2e 20 20 54 69 63   the error.  Tic
df8b0 6b 65 74 20 23 31 36 35 37 0a 20 20 20 20 20 20  ket #1657.      
df8c0 20 2a 2f 0a 20 20 20 20 20 20 20 2f 2a 20 70 46   */.       /* pF
df8d0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
df8e0 20 65 72 72 6e 6f 3b 20 2a 2f 0a 20 20 20 20 20   errno; */.     
df8f0 20 20 2f 2a 20 72 65 74 75 72 6e 20 53 51 4c 49    /* return SQLI
df900 54 45 5f 49 4f 45 52 52 3b 20 2a 2f 0a 20 20 20  TE_IOERR; */.   
df910 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 65 72   }.#endif.    er
df920 72 20 3d 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d  r = close(pFile-
df930 3e 64 69 72 66 64 29 3b 20 2f 2a 20 4f 6e 6c 79  >dirfd); /* Only
df940 20 6e 65 65 64 20 74 6f 20 73 79 6e 63 20 6f 6e   need to sync on
df950 63 65 2c 20 73 6f 20 63 6c 6f 73 65 20 74 68 65  ce, so close the
df960 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 72 72 3d   */.    if( err=
df970 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20  =0 ){           
df980 20 20 20 2f 2a 20 64 69 72 65 63 74 6f 72 79 20     /* directory 
df990 77 68 65 6e 20 77 65 20 61 72 65 20 64 6f 6e 65  when we are done
df9a0 20 2a 2f 0a 20 20 20 20 20 20 70 46 69 6c 65 2d   */.      pFile-
df9b0 3e 64 69 72 66 64 20 3d 20 2d 31 3b 0a 20 20 20  >dirfd = -1;.   
df9c0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 46   }else{.      pF
df9d0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
df9e0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63   errno;.      rc
df9f0 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f   = SQLITE_IOERR_
dfa00 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 7d  DIR_CLOSE;.    }
dfa10 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
dfa20 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63  ;.}../*.** Trunc
dfa30 61 74 65 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65  ate an open file
dfa40 20 74 6f 20 61 20 73 70 65 63 69 66 69 65 64 20   to a specified 
dfa50 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 63 20 69  size.*/.static i
dfa60 6e 74 20 75 6e 69 78 54 72 75 6e 63 61 74 65 28  nt unixTruncate(
dfa70 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
dfa80 2c 20 69 36 34 20 6e 42 79 74 65 29 7b 0a 20 20  , i64 nByte){.  
dfa90 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74  int rc;.  assert
dfaa0 28 20 69 64 20 29 3b 0a 20 20 53 69 6d 75 6c 61  ( id );.  Simula
dfab0 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72  teIOError( retur
dfac0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54  n SQLITE_IOERR_T
dfad0 52 55 4e 43 41 54 45 20 29 3b 0a 20 20 72 63 20  RUNCATE );.  rc 
dfae0 3d 20 66 74 72 75 6e 63 61 74 65 28 28 28 75 6e  = ftruncate(((un
dfaf0 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20  ixFile*)id)->h, 
dfb00 28 6f 66 66 5f 74 29 6e 42 79 74 65 29 3b 0a 20  (off_t)nByte);. 
dfb10 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 28   if( rc ){.    (
dfb20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e  (unixFile*)id)->
dfb30 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  lastErrno = errn
dfb40 6f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  o;.    return SQ
dfb50 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43  LITE_IOERR_TRUNC
dfb60 41 54 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ATE;.  }else{.  
dfb70 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
dfb80 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  OK;.  }.}../*.**
dfb90 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 63   Determine the c
dfba0 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 61  urrent size of a
dfbb0 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 0a 2a   file in bytes.*
dfbc0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
dfbd0 78 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65  xFileSize(sqlite
dfbe0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34 20  3_file *id, i64 
dfbf0 2a 70 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72  *pSize){.  int r
dfc00 63 3b 0a 20 20 73 74 72 75 63 74 20 73 74 61 74  c;.  struct stat
dfc10 20 62 75 66 3b 0a 20 20 61 73 73 65 72 74 28 20   buf;.  assert( 
dfc20 69 64 20 29 3b 0a 20 20 72 63 20 3d 20 66 73 74  id );.  rc = fst
dfc30 61 74 28 28 28 75 6e 69 78 46 69 6c 65 2a 29 69  at(((unixFile*)i
dfc40 64 29 2d 3e 68 2c 20 26 62 75 66 29 3b 0a 20 20  d)->h, &buf);.  
dfc50 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
dfc60 20 72 63 3d 31 20 29 3b 0a 20 20 69 66 28 20 72   rc=1 );.  if( r
dfc70 63 21 3d 30 20 29 7b 0a 20 20 20 20 28 28 75 6e  c!=0 ){.    ((un
dfc80 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73  ixFile*)id)->las
dfc90 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a  tErrno = errno;.
dfca0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
dfcb0 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20  E_IOERR_FSTAT;. 
dfcc0 20 7d 0a 20 20 2a 70 53 69 7a 65 20 3d 20 62 75   }.  *pSize = bu
dfcd0 66 2e 73 74 5f 73 69 7a 65 3b 0a 0a 20 20 2f 2a  f.st_size;..  /*
dfce0 20 57 68 65 6e 20 6f 70 65 6e 69 6e 67 20 61 20   When opening a 
dfcf0 7a 65 72 6f 2d 73 69 7a 65 20 64 61 74 61 62 61  zero-size databa
dfd00 73 65 2c 20 74 68 65 20 66 69 6e 64 4c 6f 63 6b  se, the findLock
dfd10 49 6e 66 6f 28 29 20 70 72 6f 63 65 64 75 72 65  Info() procedure
dfd20 0a 20 20 2a 2a 20 77 72 69 74 65 73 20 61 20 73  .  ** writes a s
dfd30 69 6e 67 6c 65 20 62 79 74 65 20 69 6e 74 6f 20  ingle byte into 
dfd40 74 68 61 74 20 66 69 6c 65 20 69 6e 20 6f 72 64  that file in ord
dfd50 65 72 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 6e  er to work aroun
dfd60 64 20 61 20 62 75 67 0a 20 20 2a 2a 20 69 6e 20  d a bug.  ** in 
dfd70 74 68 65 20 4f 53 2d 58 20 6d 73 64 6f 73 20 66  the OS-X msdos f
dfd80 69 6c 65 73 79 73 74 65 6d 2e 20 20 49 6e 20 6f  ilesystem.  In o
dfd90 72 64 65 72 20 74 6f 20 61 76 6f 69 64 20 70 72  rder to avoid pr
dfda0 6f 62 6c 65 6d 73 20 77 69 74 68 20 75 70 70 65  oblems with uppe
dfdb0 72 0a 20 20 2a 2a 20 6c 61 79 65 72 73 2c 20 77  r.  ** layers, w
dfdc0 65 20 6e 65 65 64 20 74 6f 20 72 65 70 6f 72 74  e need to report
dfdd0 20 74 68 69 73 20 66 69 6c 65 20 73 69 7a 65 20   this file size 
dfde0 61 73 20 7a 65 72 6f 20 65 76 65 6e 20 74 68 6f  as zero even tho
dfdf0 75 67 68 20 69 74 20 69 73 0a 20 20 2a 2a 20 72  ugh it is.  ** r
dfe00 65 61 6c 6c 79 20 31 2e 20 20 20 54 69 63 6b 65  eally 1.   Ticke
dfe10 74 20 23 33 32 36 30 2e 0a 20 20 2a 2f 0a 20 20  t #3260..  */.  
dfe20 69 66 28 20 2a 70 53 69 7a 65 3d 3d 31 20 29 20  if( *pSize==1 ) 
dfe30 2a 70 53 69 7a 65 20 3d 20 30 3b 0a 0a 0a 20 20  *pSize = 0;...  
dfe40 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
dfe50 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f  ;.}..#if SQLITE_
dfe60 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
dfe70 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28  TYLE && defined(
dfe80 5f 5f 41 50 50 4c 45 5f 5f 29 0a 2f 2a 0a 2a 2a  __APPLE__)./*.**
dfe90 20 48 61 6e 64 6c 65 72 20 66 6f 72 20 70 72 6f   Handler for pro
dfea0 78 79 2d 6c 6f 63 6b 69 6e 67 20 66 69 6c 65 2d  xy-locking file-
dfeb0 63 6f 6e 74 72 6f 6c 20 76 65 72 62 73 2e 20 20  control verbs.  
dfec0 44 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 69 6e  Defined below in
dfed0 20 74 68 65 0a 2a 2a 20 70 72 6f 78 79 69 6e 67   the.** proxying
dfee0 20 6c 6f 63 6b 69 6e 67 20 64 69 76 69 73 69 6f   locking divisio
dfef0 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  n..*/.static int
dff00 20 70 72 6f 78 79 46 69 6c 65 43 6f 6e 74 72 6f   proxyFileContro
dff10 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c  l(sqlite3_file*,
dff20 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a 23 65 6e 64  int,void*);.#end
dff30 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 66 6f 72  if.../*.** Infor
dff40 6d 61 74 69 6f 6e 20 61 6e 64 20 63 6f 6e 74 72  mation and contr
dff50 6f 6c 20 6f 66 20 61 6e 20 6f 70 65 6e 20 66 69  ol of an open fi
dff60 6c 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74  le handle..*/.st
dff70 61 74 69 63 20 69 6e 74 20 75 6e 69 78 46 69 6c  atic int unixFil
dff80 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33  eControl(sqlite3
dff90 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f  _file *id, int o
dffa0 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a  p, void *pArg){.
dffb0 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a    switch( op ){.
dffc0 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
dffd0 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 3a  FCNTL_LOCKSTATE:
dffe0 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a 29   {.      *(int*)
dfff0 70 41 72 67 20 3d 20 28 28 75 6e 69 78 46 69 6c  pArg = ((unixFil
e0000 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65  e*)id)->locktype
e0010 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
e0020 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a  QLITE_OK;.    }.
e0030 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
e0040 4c 41 53 54 5f 45 52 52 4e 4f 3a 20 7b 0a 20 20  LAST_ERRNO: {.  
e0050 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20      *(int*)pArg 
e0060 3d 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64  = ((unixFile*)id
e0070 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20  )->lastErrno;.  
e0080 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
e0090 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e  E_OK;.    }.#ifn
e00a0 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 2f  def NDEBUG.    /
e00b0 2a 20 54 68 65 20 70 61 67 65 72 20 63 61 6c 6c  * The pager call
e00c0 73 20 74 68 69 73 20 6d 65 74 68 6f 64 20 74 6f  s this method to
e00d0 20 73 69 67 6e 61 6c 20 74 68 61 74 20 69 74 20   signal that it 
e00e0 68 61 73 20 64 6f 6e 65 0a 20 20 20 20 2a 2a 20  has done.    ** 
e00f0 61 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 64 20 74  a rollback and t
e0100 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65  hat the database
e0110 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 75 6e   is therefore un
e0120 63 68 61 6e 67 65 64 20 61 6e 64 0a 20 20 20 20  changed and.    
e0130 2a 2a 20 69 74 20 68 65 6e 63 65 20 69 74 20 69  ** it hence it i
e0140 73 20 4f 4b 20 66 6f 72 20 74 68 65 20 74 72 61  s OK for the tra
e0150 6e 73 61 63 74 69 6f 6e 20 63 68 61 6e 67 65 20  nsaction change 
e0160 63 6f 75 6e 74 65 72 20 74 6f 20 62 65 0a 20 20  counter to be.  
e0170 20 20 2a 2a 20 75 6e 63 68 61 6e 67 65 64 2e 0a    ** unchanged..
e0180 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20      */.    case 
e0190 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42 5f  SQLITE_FCNTL_DB_
e01a0 55 4e 43 48 41 4e 47 45 44 3a 20 7b 0a 20 20 20  UNCHANGED: {.   
e01b0 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69     ((unixFile*)i
e01c0 64 29 2d 3e 64 62 55 70 64 61 74 65 20 3d 20 30  d)->dbUpdate = 0
e01d0 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
e01e0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a  QLITE_OK;.    }.
e01f0 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54  #endif.#if SQLIT
e0200 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
e0210 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65  _STYLE && define
e0220 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 20  d(__APPLE__).   
e0230 20 63 61 73 65 20 53 51 4c 49 54 45 5f 53 45 54   case SQLITE_SET
e0240 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 3a 0a  _LOCKPROXYFILE:.
e0250 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
e0260 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c  GET_LOCKPROXYFIL
e0270 45 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72  E: {.      retur
e0280 6e 20 70 72 6f 78 79 46 69 6c 65 43 6f 6e 74 72  n proxyFileContr
e0290 6f 6c 28 69 64 2c 6f 70 2c 70 41 72 67 29 3b 0a  ol(id,op,pArg);.
e02a0 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20      }.#endif /* 
e02b0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
e02c0 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64  CKING_STYLE && d
e02d0 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f  efined(__APPLE__
e02e0 29 20 2a 2f 0a 20 20 7d 0a 20 20 72 65 74 75 72  ) */.  }.  retur
e02f0 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
e0300 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
e0310 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20  the sector size 
e0320 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65 20  in bytes of the 
e0330 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f 63 6b  underlying block
e0340 20 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a 20 74   device for.** t
e0350 68 65 20 73 70 65 63 69 66 69 65 64 20 66 69 6c  he specified fil
e0360 65 2e 20 54 68 69 73 20 69 73 20 61 6c 6d 6f 73  e. This is almos
e0370 74 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 74  t always 512 byt
e0380 65 73 2c 20 62 75 74 20 6d 61 79 20 62 65 0a 2a  es, but may be.*
e0390 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 73 6f 6d  * larger for som
e03a0 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a 2a 2a  e devices..**.**
e03b0 20 53 51 4c 69 74 65 20 63 6f 64 65 20 61 73 73   SQLite code ass
e03c0 75 6d 65 73 20 74 68 69 73 20 66 75 6e 63 74 69  umes this functi
e03d0 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20  on cannot fail. 
e03e0 49 74 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20  It also assumes 
e03f0 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f 20 66  that.** if two f
e0400 69 6c 65 73 20 61 72 65 20 63 72 65 61 74 65 64  iles are created
e0410 20 69 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c   in the same fil
e0420 65 2d 73 79 73 74 65 6d 20 64 69 72 65 63 74 6f  e-system directo
e0430 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 64 61  ry (i.e..** a da
e0440 74 61 62 61 73 65 20 61 6e 64 20 69 74 73 20 6a  tabase and its j
e0450 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 74 68 61  ournal file) tha
e0460 74 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a  t the sector siz
e0470 65 20 77 69 6c 6c 20 62 65 20 74 68 65 0a 2a 2a  e will be the.**
e0480 20 73 61 6d 65 20 66 6f 72 20 62 6f 74 68 2e 0a   same for both..
e0490 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e  */.static int un
e04a0 69 78 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c  ixSectorSize(sql
e04b0 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73  ite3_file *NotUs
e04c0 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41  ed){.  UNUSED_PA
e04d0 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
e04e0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
e04f0 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52  E_DEFAULT_SECTOR
e0500 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  _SIZE;.}../*.** 
e0510 52 65 74 75 72 6e 20 74 68 65 20 64 65 76 69 63  Return the devic
e0520 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63  e characteristic
e0530 73 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2e 20  s for the file. 
e0540 54 68 69 73 20 69 73 20 61 6c 77 61 79 73 20 30  This is always 0
e0550 20 66 6f 72 20 75 6e 69 78 2e 0a 2a 2f 0a 73 74   for unix..*/.st
e0560 61 74 69 63 20 69 6e 74 20 75 6e 69 78 44 65 76  atic int unixDev
e0570 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69  iceCharacteristi
e0580 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  cs(sqlite3_file 
e0590 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55  *NotUsed){.  UNU
e05a0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f  SED_PARAMETER(No
e05b0 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e  tUsed);.  return
e05c0 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 72   0;.}../*.** Her
e05d0 65 20 65 6e 64 73 20 74 68 65 20 69 6d 70 6c 65  e ends the imple
e05e0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6c 6c  mentation of all
e05f0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6d 65   sqlite3_file me
e0600 74 68 6f 64 73 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a  thods..**.******
e0610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0620 20 45 6e 64 20 73 71 6c 69 74 65 33 5f 66 69 6c   End sqlite3_fil
e0630 65 20 4d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a  e Methods ******
e0640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0650 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a  *********.******
e0660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e0690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e06a0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a  ********/../*.**
e06b0 20 54 68 69 73 20 64 69 76 69 73 69 6f 6e 20 63   This division c
e06c0 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e 69 74 69  ontains definiti
e06d0 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 69  ons of sqlite3_i
e06e0 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74  o_methods object
e06f0 73 20 74 68 61 74 0a 2a 2a 20 69 6d 70 6c 65 6d  s that.** implem
e0700 65 6e 74 20 76 61 72 69 6f 75 73 20 66 69 6c 65  ent various file
e0710 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67   locking strateg
e0720 69 65 73 2e 20 20 49 74 20 61 6c 73 6f 20 63 6f  ies.  It also co
e0730 6e 74 61 69 6e 73 20 64 65 66 69 6e 69 74 69 6f  ntains definitio
e0740 6e 73 0a 2a 2a 20 6f 66 20 22 66 69 6e 64 65 72  ns.** of "finder
e0750 22 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 41 20  " functions.  A 
e0760 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20  finder-function 
e0770 69 73 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74  is used to locat
e0780 65 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  e the appropriat
e0790 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f  e.** sqlite3_io_
e07a0 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 66  methods object f
e07b0 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  or a particular 
e07c0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20  database file.  
e07d0 54 68 65 20 70 41 70 70 44 61 74 61 0a 2a 2a 20  The pAppData.** 
e07e0 66 69 65 6c 64 20 6f 66 20 74 68 65 20 73 71 6c  field of the sql
e07f0 69 74 65 33 5f 76 66 73 20 56 46 53 20 6f 62 6a  ite3_vfs VFS obj
e0800 65 63 74 73 20 61 72 65 20 69 6e 69 74 69 61 6c  ects are initial
e0810 69 7a 65 64 20 74 6f 20 62 65 20 70 6f 69 6e 74  ized to be point
e0820 65 72 73 20 74 6f 0a 2a 2a 20 74 68 65 20 63 6f  ers to.** the co
e0830 72 72 65 63 74 20 66 69 6e 64 65 72 2d 66 75 6e  rrect finder-fun
e0840 63 74 69 6f 6e 20 66 6f 72 20 74 68 61 74 20 56  ction for that V
e0850 46 53 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 66  FS..**.** Most f
e0860 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 73 20  inder functions 
e0870 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  return a pointer
e0880 20 74 6f 20 61 20 66 69 78 65 64 20 73 71 6c 69   to a fixed sqli
e0890 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a  te3_io_methods.*
e08a0 2a 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 6f  * object.  The o
e08b0 6e 6c 79 20 69 6e 74 65 72 65 73 74 69 6e 67 20  nly interesting 
e08c0 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20  finder-function 
e08d0 69 73 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e  is autolockIoFin
e08e0 64 65 72 2c 20 77 68 69 63 68 0a 2a 2a 20 6c 6f  der, which.** lo
e08f0 6f 6b 73 20 61 74 20 74 68 65 20 66 69 6c 65 73  oks at the files
e0900 79 73 74 65 6d 20 74 79 70 65 20 61 6e 64 20 74  ystem type and t
e0910 72 69 65 73 20 74 6f 20 67 75 65 73 73 20 74 68  ries to guess th
e0920 65 20 62 65 73 74 20 6c 6f 63 6b 69 6e 67 0a 2a  e best locking.*
e0930 2a 20 73 74 72 61 74 65 67 79 20 66 72 6f 6d 20  * strategy from 
e0940 74 68 61 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  that..**.** For 
e0950 66 69 6e 64 65 72 2d 66 75 6e 74 69 6f 6e 20 46  finder-funtion F
e0960 2c 20 74 77 6f 20 6f 62 6a 65 63 74 73 20 61 72  , two objects ar
e0970 65 20 63 72 65 61 74 65 64 3a 0a 2a 2a 0a 2a 2a  e created:.**.**
e0980 20 20 20 20 28 31 29 20 54 68 65 20 72 65 61 6c      (1) The real
e0990 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e   finder-function
e09a0 20 6e 61 6d 65 64 20 22 46 49 6d 70 74 28 29 22   named "FImpt()"
e09b0 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20 41  ..**.**    (2) A
e09c0 20 63 6f 6e 73 74 61 6e 74 20 70 6f 69 6e 74 65   constant pointe
e09d0 72 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69  r to this functi
e09e0 6f 6e 20 6e 61 6d 65 64 20 6a 75 73 74 20 22 46  on named just "F
e09f0 22 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 41 20 70 6f  "..**.**.** A po
e0a00 69 6e 74 65 72 20 74 6f 20 74 68 65 20 46 20 70  inter to the F p
e0a10 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64 20 61  ointer is used a
e0a20 73 20 74 68 65 20 70 41 70 70 44 61 74 61 20 76  s the pAppData v
e0a30 61 6c 75 65 20 66 6f 72 20 56 46 53 0a 2a 2a 20  alue for VFS.** 
e0a40 6f 62 6a 65 63 74 73 2e 20 20 57 65 20 68 61 76  objects.  We hav
e0a50 65 20 74 6f 20 64 6f 20 74 68 69 73 20 69 6e 73  e to do this ins
e0a60 74 65 61 64 20 6f 66 20 6c 65 74 74 69 6e 67 20  tead of letting 
e0a70 70 41 70 70 44 61 74 61 20 70 6f 69 6e 74 0a 2a  pAppData point.*
e0a80 2a 20 64 69 72 65 63 74 6c 79 20 61 74 20 74 68  * directly at th
e0a90 65 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f  e finder-functio
e0aa0 6e 20 73 69 6e 63 65 20 43 39 30 20 72 75 6c 65  n since C90 rule
e0ab0 73 20 70 72 65 76 65 6e 74 20 61 20 76 6f 69 64  s prevent a void
e0ac0 2a 0a 2a 2a 20 66 72 6f 6d 20 62 65 20 63 61 73  *.** from be cas
e0ad0 74 20 69 6e 74 6f 20 61 20 66 75 6e 63 74 69 6f  t into a functio
e0ae0 6e 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a  n pointer..**.**
e0af0 0a 2a 2a 20 45 61 63 68 20 69 6e 73 74 61 6e 63  .** Each instanc
e0b00 65 20 6f 66 20 74 68 69 73 20 6d 61 63 72 6f 20  e of this macro 
e0b10 67 65 6e 65 72 61 74 65 73 20 74 77 6f 20 6f 62  generates two ob
e0b20 6a 65 63 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a  jects:.**.**   *
e0b30 20 20 41 20 63 6f 6e 73 74 61 6e 74 20 73 71 6c    A constant sql
e0b40 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20  ite3_io_methods 
e0b50 6f 62 6a 65 63 74 20 63 61 6c 6c 20 4d 45 54 48  object call METH
e0b60 4f 44 20 74 68 61 74 20 68 61 73 20 6c 6f 63 6b  OD that has lock
e0b70 69 6e 67 0a 2a 2a 20 20 20 20 20 20 6d 65 74 68  ing.**      meth
e0b80 6f 64 73 20 43 4c 4f 53 45 2c 20 4c 4f 43 4b 2c  ods CLOSE, LOCK,
e0b90 20 55 4e 4c 4f 43 4b 2c 20 43 4b 52 45 53 4c 4f   UNLOCK, CKRESLO
e0ba0 43 4b 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 41  CK..**.**   *  A
e0bb0 6e 20 49 2f 4f 20 6d 65 74 68 6f 64 20 66 69 6e  n I/O method fin
e0bc0 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c  der function cal
e0bd0 6c 65 64 20 46 49 4e 44 45 52 20 74 68 61 74 20  led FINDER that 
e0be0 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
e0bf0 72 0a 2a 2a 20 20 20 20 20 20 74 6f 20 74 68 65  r.**      to the
e0c00 20 4d 45 54 48 4f 44 20 6f 62 6a 65 63 74 20 69   METHOD object i
e0c10 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 62  n the previous b
e0c20 75 6c 6c 65 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ullet..*/.#defin
e0c30 65 20 49 4f 4d 45 54 48 4f 44 53 28 46 49 4e 44  e IOMETHODS(FIND
e0c40 45 52 2c 20 4d 45 54 48 4f 44 2c 20 43 4c 4f 53  ER, METHOD, CLOS
e0c50 45 2c 20 4c 4f 43 4b 2c 20 55 4e 4c 4f 43 4b 2c  E, LOCK, UNLOCK,
e0c60 20 43 4b 4c 4f 43 4b 29 20 20 20 20 20 20 20 20   CKLOCK)        
e0c70 20 20 20 20 20 20 20 5c 0a 73 74 61 74 69 63 20         \.static 
e0c80 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f  const sqlite3_io
e0c90 5f 6d 65 74 68 6f 64 73 20 4d 45 54 48 4f 44 20  _methods METHOD 
e0ca0 3d 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  = {             
e0cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0cc0 20 20 20 20 20 20 5c 0a 20 20 20 31 2c 20 20 20        \.   1,   
e0cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0ce0 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69         /* iVersi
e0cf0 6f 6e 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20  on */           
e0d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0d10 20 20 20 20 20 5c 0a 20 20 20 43 4c 4f 53 45 2c       \.   CLOSE,
e0d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0d30 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20        /* xClose 
e0d40 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
e0d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0d60 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 52 65 61      \.   unixRea
e0d70 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d,              
e0d80 20 20 20 20 20 2f 2a 20 78 52 65 61 64 20 2a 2f       /* xRead */
e0d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0db0 20 20 20 5c 0a 20 20 20 75 6e 69 78 57 72 69 74     \.   unixWrit
e0dc0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
e0dd0 20 20 20 20 2f 2a 20 78 57 72 69 74 65 20 2a 2f      /* xWrite */
e0de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0e00 20 20 5c 0a 20 20 20 75 6e 69 78 54 72 75 6e 63    \.   unixTrunc
e0e10 61 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ate,            
e0e20 20 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 20     /* xTruncate 
e0e30 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
e0e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0e50 20 5c 0a 20 20 20 75 6e 69 78 53 79 6e 63 2c 20   \.   unixSync, 
e0e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0e70 20 20 2f 2a 20 78 53 79 6e 63 20 2a 2f 20 20 20    /* xSync */   
e0e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0ea0 5c 0a 20 20 20 75 6e 69 78 46 69 6c 65 53 69 7a  \.   unixFileSiz
e0eb0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
e0ec0 20 2f 2a 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f   /* xFileSize */
e0ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
e0ef0 0a 20 20 20 4c 4f 43 4b 2c 20 20 20 20 20 20 20  .   LOCK,       
e0f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0f10 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 20 20 20 20 20  /* xLock */     
e0f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
e0f40 20 20 20 55 4e 4c 4f 43 4b 2c 20 20 20 20 20 20     UNLOCK,      
e0f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
e0f60 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 20 20 20 20  * xUnlock */    
e0f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0f80 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
e0f90 20 20 43 4b 4c 4f 43 4b 2c 20 20 20 20 20 20 20    CKLOCK,       
e0fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e0fb0 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c   xCheckReservedL
e0fc0 6f 63 6b 20 2a 2f 20 20 20 20 20 20 20 20 20 20  ock */          
e0fd0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
e0fe0 20 75 6e 69 78 46 69 6c 65 43 6f 6e 74 72 6f 6c   unixFileControl
e0ff0 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
e1000 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 20  xFileControl */ 
e1010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1020 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
e1030 75 6e 69 78 53 65 63 74 6f 72 53 69 7a 65 2c 20  unixSectorSize, 
e1040 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
e1050 53 65 63 74 6f 72 53 69 7a 65 20 2a 2f 20 20 20  SectorSize */   
e1060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1070 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 75            \.   u
e1080 6e 69 78 44 65 76 69 63 65 43 68 61 72 61 63 74  nixDeviceCharact
e1090 65 72 69 73 74 69 63 73 20 20 20 2f 2a 20 78 44  eristics   /* xD
e10a0 65 76 69 63 65 43 61 70 61 62 69 6c 69 74 69 65  eviceCapabilitie
e10b0 73 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  s */            
e10c0 20 20 20 20 20 20 20 20 20 5c 0a 7d 3b 20 20 20           \.};   
e10d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e10e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e10f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1110 20 20 20 20 20 20 20 20 5c 0a 73 74 61 74 69 63          \.static
e1120 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69   const sqlite3_i
e1130 6f 5f 6d 65 74 68 6f 64 73 20 2a 46 49 4e 44 45  o_methods *FINDE
e1140 52 23 23 49 6d 70 6c 28 63 6f 6e 73 74 20 63 68  R##Impl(const ch
e1150 61 72 20 2a 7a 2c 20 75 6e 69 78 46 69 6c 65 20  ar *z, unixFile 
e1160 2a 70 29 7b 20 20 20 5c 0a 20 20 55 4e 55 53 45  *p){   \.  UNUSE
e1170 44 5f 50 41 52 41 4d 45 54 45 52 28 7a 29 3b 20  D_PARAMETER(z); 
e1180 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
e1190 28 70 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  (p);            
e11a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e11b0 20 20 20 20 20 20 5c 0a 20 20 72 65 74 75 72 6e        \.  return
e11c0 20 26 4d 45 54 48 4f 44 3b 20 20 20 20 20 20 20   &METHOD;       
e11d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e11e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e11f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1200 20 20 20 20 20 5c 0a 7d 20 20 20 20 20 20 20 20       \.}        
e1210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1250 20 20 20 20 5c 0a 73 74 61 74 69 63 20 63 6f 6e      \.static con
e1260 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  st sqlite3_io_me
e1270 74 68 6f 64 73 20 2a 28 2a 63 6f 6e 73 74 20 46  thods *(*const F
e1280 49 4e 44 45 52 29 28 63 6f 6e 73 74 20 63 68 61  INDER)(const cha
e1290 72 2a 2c 75 6e 69 78 46 69 6c 65 20 2a 70 29 20  r*,unixFile *p) 
e12a0 20 20 20 5c 0a 20 20 20 20 3d 20 46 49 4e 44 45     \.    = FINDE
e12b0 52 23 23 49 6d 70 6c 3b 0a 0a 2f 2a 0a 2a 2a 20  R##Impl;../*.** 
e12c0 48 65 72 65 20 61 72 65 20 61 6c 6c 20 6f 66 20  Here are all of 
e12d0 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  the sqlite3_io_m
e12e0 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 73 20 66  ethods objects f
e12f0 6f 72 20 65 61 63 68 20 6f 66 20 74 68 65 0a 2a  or each of the.*
e1300 2a 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65  * locking strate
e1310 67 69 65 73 2e 20 20 46 75 6e 63 74 69 6f 6e 73  gies.  Functions
e1320 20 74 68 61 74 20 72 65 74 75 72 6e 20 70 6f 69   that return poi
e1330 6e 74 65 72 73 20 74 6f 20 74 68 65 73 65 20 6d  nters to these m
e1340 65 74 68 6f 64 73 0a 2a 2a 20 61 72 65 20 61 6c  ethods.** are al
e1350 73 6f 20 63 72 65 61 74 65 64 2e 0a 2a 2f 0a 49  so created..*/.I
e1360 4f 4d 45 54 48 4f 44 53 28 0a 20 20 70 6f 73 69  OMETHODS(.  posi
e1370 78 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20  xIoFinder,      
e1380 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20        /* Finder 
e1390 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f  function name */
e13a0 0a 20 20 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64  .  posixIoMethod
e13b0 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  s,           /* 
e13c0 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
e13d0 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a  ds object name *
e13e0 2f 0a 20 20 75 6e 69 78 43 6c 6f 73 65 2c 20 20  /.  unixClose,  
e13f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e1400 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a   xClose method *
e1410 2f 0a 20 20 75 6e 69 78 4c 6f 63 6b 2c 20 20 20  /.  unixLock,   
e1420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e1430 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f   xLock method */
e1440 0a 20 20 75 6e 69 78 55 6e 6c 6f 63 6b 2c 20 20  .  unixUnlock,  
e1450 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e1460 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a  xUnlock method *
e1470 2f 0a 20 20 75 6e 69 78 43 68 65 63 6b 52 65 73  /.  unixCheckRes
e1480 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 20 2f 2a  ervedLock     /*
e1490 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c   xCheckReservedL
e14a0 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a  ock method */.).
e14b0 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 6e 6f 6c  IOMETHODS(.  nol
e14c0 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 20 20  ockIoFinder,    
e14d0 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72         /* Finder
e14e0 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a   function name *
e14f0 2f 0a 20 20 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68  /.  nolockIoMeth
e1500 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  ods,          /*
e1510 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
e1520 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20  ods object name 
e1530 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 6c 6f 73 65  */.  nolockClose
e1540 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
e1550 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20  * xClose method 
e1560 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b 2c  */.  nolockLock,
e1570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
e1580 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a  * xLock method *
e1590 2f 0a 20 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63 6b  /.  nolockUnlock
e15a0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
e15b0 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20   xUnlock method 
e15c0 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b  */.  nolockCheck
e15d0 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 2f  ReservedLock   /
e15e0 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64  * xCheckReserved
e15f0 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29  Lock method */.)
e1600 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 64 6f  .IOMETHODS(.  do
e1610 74 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20  tlockIoFinder,  
e1620 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65          /* Finde
e1630 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20  r function name 
e1640 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 49 6f 4d 65  */.  dotlockIoMe
e1650 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 2f  thods,         /
e1660 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  * sqlite3_io_met
e1670 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65  hods object name
e1680 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 43 6c 6f   */.  dotlockClo
e1690 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  se,             
e16a0 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64  /* xClose method
e16b0 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 4c 6f 63   */.  dotlockLoc
e16c0 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k,              
e16d0 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20  /* xLock method 
e16e0 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f  */.  dotlockUnlo
e16f0 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  ck,            /
e1700 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64  * xUnlock method
e1710 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 43 68 65   */.  dotlockChe
e1720 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20  ckReservedLock  
e1730 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65  /* xCheckReserve
e1740 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a  dLock method */.
e1750 29 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e  )..#if SQLITE_EN
e1760 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
e1770 4c 45 20 26 26 20 21 4f 53 5f 56 58 57 4f 52 4b  LE && !OS_VXWORK
e1780 53 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 66  S.IOMETHODS(.  f
e1790 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 20  lockIoFinder,   
e17a0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64           /* Find
e17b0 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65  er function name
e17c0 20 2a 2f 0a 20 20 66 6c 6f 63 6b 49 6f 4d 65 74   */.  flockIoMet
e17d0 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 20  hods,           
e17e0 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  /* sqlite3_io_me
e17f0 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d  thods object nam
e1800 65 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 6c 6f 73  e */.  flockClos
e1810 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
e1820 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f   /* xClose metho
e1830 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 4c 6f 63 6b  d */.  flockLock
e1840 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
e1850 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64   /* xLock method
e1860 20 2a 2f 0a 20 20 66 6c 6f 63 6b 55 6e 6c 6f 63   */.  flockUnloc
e1870 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k,              
e1880 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f  /* xUnlock metho
e1890 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 68 65 63  d */.  flockChec
e18a0 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20  kReservedLock   
e18b0 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76   /* xCheckReserv
e18c0 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f  edLock method */
e18d0 0a 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 4f  .).#endif..#if O
e18e0 53 5f 56 58 57 4f 52 4b 53 0a 49 4f 4d 45 54 48  S_VXWORKS.IOMETH
e18f0 4f 44 53 28 0a 20 20 73 65 6d 49 6f 46 69 6e 64  ODS(.  semIoFind
e1900 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  er,             
e1910 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74   /* Finder funct
e1920 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 73 65  ion name */.  se
e1930 6d 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20  mIoMethods,     
e1940 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74          /* sqlit
e1950 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62  e3_io_methods ob
e1960 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 73  ject name */.  s
e1970 65 6d 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20  emClose,        
e1980 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f           /* xClo
e1990 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73  se method */.  s
e19a0 65 6d 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20  emLock,         
e19b0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63           /* xLoc
e19c0 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 65  k method */.  se
e19d0 6d 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20  mUnlock,        
e19e0 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f          /* xUnlo
e19f0 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73  ck method */.  s
e1a00 65 6d 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  emCheckReservedL
e1a10 6f 63 6b 20 20 20 20 20 20 2f 2a 20 78 43 68 65  ock      /* xChe
e1a20 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d  ckReservedLock m
e1a30 65 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69  ethod */.).#endi
e1a40 66 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f  f..#if defined(_
e1a50 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c  _APPLE__) && SQL
e1a60 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
e1a70 4e 47 5f 53 54 59 4c 45 0a 49 4f 4d 45 54 48 4f  NG_STYLE.IOMETHO
e1a80 44 53 28 0a 20 20 61 66 70 49 6f 46 69 6e 64 65  DS(.  afpIoFinde
e1a90 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
e1aa0 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69  /* Finder functi
e1ab0 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 61 66 70  on name */.  afp
e1ac0 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20  IoMethods,      
e1ad0 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65         /* sqlite
e1ae0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a  3_io_methods obj
e1af0 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 61 66  ect name */.  af
e1b00 70 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20  pClose,         
e1b10 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73          /* xClos
e1b20 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66  e method */.  af
e1b30 70 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20  pLock,          
e1b40 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b          /* xLock
e1b50 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70   method */.  afp
e1b60 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20  Unlock,         
e1b70 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63         /* xUnloc
e1b80 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66  k method */.  af
e1b90 70 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  pCheckReservedLo
e1ba0 63 6b 20 20 20 20 20 20 2f 2a 20 78 43 68 65 63  ck      /* xChec
e1bb0 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65  kReservedLock me
e1bc0 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66  thod */.).#endif
e1bd0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 57 68 6f  ../*.** The "Who
e1be0 6c 65 20 46 69 6c 65 20 4c 6f 63 6b 69 6e 67 22  le File Locking"
e1bf0 20 66 69 6e 64 65 72 20 72 65 74 75 72 6e 73 20   finder returns 
e1c00 74 68 65 20 73 61 6d 65 20 73 65 74 20 6f 66 20  the same set of 
e1c10 6d 65 74 68 6f 64 73 20 61 73 0a 2a 2a 20 74 68  methods as.** th
e1c20 65 20 70 6f 73 69 78 20 6c 6f 63 6b 69 6e 67 20  e posix locking 
e1c30 66 69 6e 64 65 72 2e 20 20 42 75 74 20 69 74 20  finder.  But it 
e1c40 61 6c 73 6f 20 73 65 74 73 20 74 68 65 20 53 51  also sets the SQ
e1c50 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f  LITE_WHOLE_FILE_
e1c60 4c 4f 43 4b 49 4e 47 0a 2a 2a 20 66 6c 61 67 20  LOCKING.** flag 
e1c70 74 6f 20 66 6f 72 63 65 20 74 68 65 20 70 6f 73  to force the pos
e1c80 69 78 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b  ix advisory lock
e1c90 73 20 74 6f 20 63 6f 76 65 72 20 74 68 65 20 77  s to cover the w
e1ca0 68 6f 6c 65 20 66 69 6c 65 20 69 6e 73 74 65 61  hole file instea
e1cb0 64 0a 2a 2a 20 6f 66 20 6a 75 73 74 20 61 20 73  d.** of just a s
e1cc0 6d 61 6c 6c 20 73 70 61 6e 20 6f 66 20 62 79 74  mall span of byt
e1cd0 65 73 20 6e 65 61 72 20 74 68 65 20 31 47 69 42  es near the 1GiB
e1ce0 20 62 6f 75 6e 64 61 72 79 2e 20 20 57 68 6f 6c   boundary.  Whol
e1cf0 65 20 46 69 6c 65 20 4c 6f 63 6b 69 6e 67 0a 2a  e File Locking.*
e1d00 2a 20 69 73 20 75 73 65 66 75 6c 20 6f 6e 20 4e  * is useful on N
e1d10 46 53 2d 6d 6f 75 6e 74 65 64 20 66 69 6c 65 73  FS-mounted files
e1d20 20 73 69 6e 63 65 20 69 74 20 68 65 6c 70 73 20   since it helps 
e1d30 4e 46 53 20 74 6f 20 6d 61 69 6e 74 61 69 6e 20  NFS to maintain 
e1d40 63 61 63 68 65 0a 2a 2a 20 63 6f 68 65 72 65 6e  cache.** coheren
e1d50 63 79 2e 20 20 42 75 74 20 69 74 20 69 73 20 61  cy.  But it is a
e1d60 20 64 65 74 72 69 6d 65 6e 74 20 74 6f 20 6f 74   detriment to ot
e1d70 68 65 72 20 66 69 6c 65 73 79 73 74 65 6d 73 20  her filesystems 
e1d80 73 69 6e 63 65 20 69 74 20 72 75 6e 73 0a 2a 2a  since it runs.**
e1d90 20 73 6c 6f 77 65 72 2e 0a 2a 2f 0a 73 74 61 74   slower..*/.stat
e1da0 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
e1db0 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 6f 73  _io_methods *pos
e1dc0 69 78 57 66 6c 49 6f 46 69 6e 64 65 72 49 6d 70  ixWflIoFinderImp
e1dd0 6c 28 63 6f 6e 73 74 20 63 68 61 72 2a 7a 2c 20  l(const char*z, 
e1de0 75 6e 69 78 46 69 6c 65 2a 70 29 7b 0a 20 20 55  unixFile*p){.  U
e1df0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
e1e00 7a 29 3b 0a 20 20 70 2d 3e 66 69 6c 65 46 6c 61  z);.  p->fileFla
e1e10 67 73 20 3d 20 53 51 4c 49 54 45 5f 57 48 4f 4c  gs = SQLITE_WHOL
e1e20 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3b 0a  E_FILE_LOCKING;.
e1e30 20 20 72 65 74 75 72 6e 20 26 70 6f 73 69 78 49    return &posixI
e1e40 6f 4d 65 74 68 6f 64 73 3b 0a 7d 0a 73 74 61 74  oMethods;.}.stat
e1e50 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
e1e60 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 0a 20 20 2a  _io_methods .  *
e1e70 28 2a 63 6f 6e 73 74 20 70 6f 73 69 78 57 66 6c  (*const posixWfl
e1e80 49 6f 46 69 6e 64 65 72 29 28 63 6f 6e 73 74 20  IoFinder)(const 
e1e90 63 68 61 72 2a 2c 75 6e 69 78 46 69 6c 65 20 2a  char*,unixFile *
e1ea0 70 29 20 3d 20 70 6f 73 69 78 57 66 6c 49 6f 46  p) = posixWflIoF
e1eb0 69 6e 64 65 72 49 6d 70 6c 3b 0a 0a 2f 2a 0a 2a  inderImpl;../*.*
e1ec0 2a 20 54 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b  * The proxy lock
e1ed0 69 6e 67 20 6d 65 74 68 6f 64 20 69 73 20 61 20  ing method is a 
e1ee0 22 73 75 70 65 72 2d 6d 65 74 68 6f 64 22 20 69  "super-method" i
e1ef0 6e 20 74 68 65 20 73 65 6e 73 65 20 74 68 61 74  n the sense that
e1f00 20 69 74 0a 2a 2a 20 6f 70 65 6e 73 20 73 65 63   it.** opens sec
e1f10 6f 6e 64 61 72 79 20 66 69 6c 65 20 64 65 73 63  ondary file desc
e1f20 72 69 70 74 6f 72 73 20 66 6f 72 20 74 68 65 20  riptors for the 
e1f30 63 6f 6e 63 68 20 61 6e 64 20 6c 6f 63 6b 20 66  conch and lock f
e1f40 69 6c 65 73 20 61 6e 64 0a 2a 2a 20 69 74 20 75  iles and.** it u
e1f50 73 65 73 20 70 72 6f 78 79 2c 20 64 6f 74 2d 66  ses proxy, dot-f
e1f60 69 6c 65 2c 20 41 46 50 2c 20 61 6e 64 20 66 6c  ile, AFP, and fl
e1f70 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 20 6d 65  ock() locking me
e1f80 74 68 6f 64 73 20 6f 6e 20 74 68 6f 73 65 0a 2a  thods on those.*
e1f90 2a 20 73 65 63 6f 6e 64 61 72 79 20 66 69 6c 65  * secondary file
e1fa0 73 2e 20 20 46 6f 72 20 74 68 69 73 20 72 65 61  s.  For this rea
e1fb0 73 6f 6e 2c 20 74 68 65 20 64 69 76 69 73 69 6f  son, the divisio
e1fc0 6e 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  n that implement
e1fd0 73 0a 2a 2a 20 70 72 6f 78 79 20 6c 6f 63 6b 69  s.** proxy locki
e1fe0 6e 67 20 69 73 20 6c 6f 63 61 74 65 64 20 6d 75  ng is located mu
e1ff0 63 68 20 66 75 72 74 68 65 72 20 64 6f 77 6e 20  ch further down 
e2000 69 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 42 75  in the file.  Bu
e2010 74 20 77 65 20 6e 65 65 64 0a 2a 2a 20 74 6f 20  t we need.** to 
e2020 67 6f 20 61 68 65 61 64 20 61 6e 64 20 64 65 66  go ahead and def
e2030 69 6e 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f  ine the sqlite3_
e2040 69 6f 5f 6d 65 74 68 6f 64 73 20 61 6e 64 20 66  io_methods and f
e2050 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 0a 2a  inder function.*
e2060 2a 20 66 6f 72 20 70 72 6f 78 79 20 6c 6f 63 6b  * for proxy lock
e2070 69 6e 67 20 68 65 72 65 2e 20 20 53 6f 20 77 65  ing here.  So we
e2080 20 66 6f 72 77 61 72 64 20 64 65 63 6c 61 72 65   forward declare
e2090 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73   the I/O methods
e20a0 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64  ..*/.#if defined
e20b0 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53  (__APPLE__) && S
e20c0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
e20d0 4b 49 4e 47 5f 53 54 59 4c 45 0a 73 74 61 74 69  KING_STYLE.stati
e20e0 63 20 69 6e 74 20 70 72 6f 78 79 43 6c 6f 73 65  c int proxyClose
e20f0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 3b  (sqlite3_file*);
e2100 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78  .static int prox
e2110 79 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  yLock(sqlite3_fi
e2120 6c 65 2a 2c 20 69 6e 74 29 3b 0a 73 74 61 74 69  le*, int);.stati
e2130 63 20 69 6e 74 20 70 72 6f 78 79 55 6e 6c 6f 63  c int proxyUnloc
e2140 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c  k(sqlite3_file*,
e2150 20 69 6e 74 29 3b 0a 73 74 61 74 69 63 20 69 6e   int);.static in
e2160 74 20 70 72 6f 78 79 43 68 65 63 6b 52 65 73 65  t proxyCheckRese
e2170 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33  rvedLock(sqlite3
e2180 5f 66 69 6c 65 2a 2c 20 69 6e 74 2a 29 3b 0a 49  _file*, int*);.I
e2190 4f 4d 45 54 48 4f 44 53 28 0a 20 20 70 72 6f 78  OMETHODS(.  prox
e21a0 79 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20  yIoFinder,      
e21b0 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20        /* Finder 
e21c0 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f  function name */
e21d0 0a 20 20 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64  .  proxyIoMethod
e21e0 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  s,           /* 
e21f0 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
e2200 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a  ds object name *
e2210 2f 0a 20 20 70 72 6f 78 79 43 6c 6f 73 65 2c 20  /.  proxyClose, 
e2220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e2230 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a   xClose method *
e2240 2f 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 2c 20 20  /.  proxyLock,  
e2250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e2260 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f   xLock method */
e2270 0a 20 20 70 72 6f 78 79 55 6e 6c 6f 63 6b 2c 20  .  proxyUnlock, 
e2280 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e2290 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a  xUnlock method *
e22a0 2f 0a 20 20 70 72 6f 78 79 43 68 65 63 6b 52 65  /.  proxyCheckRe
e22b0 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 2f 2a  servedLock    /*
e22c0 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c   xCheckReservedL
e22d0 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a  ock method */.).
e22e0 23 65 6e 64 69 66 0a 0a 0a 23 69 66 20 64 65 66  #endif...#if def
e22f0 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20  ined(__APPLE__) 
e2300 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  && SQLITE_ENABLE
e2310 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 2f  _LOCKING_STYLE./
e2320 2a 20 0a 2a 2a 20 54 68 69 73 20 22 66 69 6e 64  * .** This "find
e2330 65 72 22 20 66 75 6e 63 74 69 6f 6e 20 61 74 74  er" function att
e2340 65 6d 70 74 73 20 74 6f 20 64 65 74 65 72 6d 69  empts to determi
e2350 6e 65 20 74 68 65 20 62 65 73 74 20 6c 6f 63 6b  ne the best lock
e2360 69 6e 67 20 73 74 72 61 74 65 67 79 20 0a 2a 2a  ing strategy .**
e2370 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73   for the databas
e2380 65 20 66 69 6c 65 20 22 66 69 6c 65 50 61 74 68  e file "filePath
e2390 22 2e 20 20 49 74 20 74 68 65 6e 20 72 65 74 75  ".  It then retu
e23a0 72 6e 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f  rns the sqlite3_
e23b0 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 62  io_methods.** ob
e23c0 6a 65 63 74 20 74 68 61 74 20 69 6d 70 6c 65 6d  ject that implem
e23d0 65 6e 74 73 20 74 68 61 74 20 73 74 72 61 74 65  ents that strate
e23e0 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  gy..**.** This i
e23f0 73 20 66 6f 72 20 4d 61 63 4f 53 58 20 6f 6e 6c  s for MacOSX onl
e2400 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  y..*/.static con
e2410 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  st sqlite3_io_me
e2420 74 68 6f 64 73 20 2a 61 75 74 6f 6c 6f 63 6b 49  thods *autolockI
e2430 6f 46 69 6e 64 65 72 49 6d 70 6c 28 0a 20 20 63  oFinderImpl(.  c
e2440 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 50  onst char *fileP
e2450 61 74 68 2c 20 20 20 20 2f 2a 20 6e 61 6d 65 20  ath,    /* name 
e2460 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
e2470 66 69 6c 65 20 2a 2f 0a 20 20 75 6e 69 78 46 69  file */.  unixFi
e2480 6c 65 20 2a 70 4e 65 77 20 20 20 20 20 20 20 20  le *pNew        
e2490 20 20 20 2f 2a 20 6f 70 65 6e 20 66 69 6c 65 20     /* open file 
e24a0 6f 62 6a 65 63 74 20 66 6f 72 20 74 68 65 20 64  object for the d
e24b0 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a  atabase file */.
e24c0 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  ){.  static cons
e24d0 74 20 73 74 72 75 63 74 20 4d 61 70 70 69 6e 67  t struct Mapping
e24e0 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61   {.    const cha
e24f0 72 20 2a 7a 46 69 6c 65 73 79 73 74 65 6d 3b 20  r *zFilesystem; 
e2500 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e2510 46 69 6c 65 73 79 73 74 65 6d 20 74 79 70 65 20  Filesystem type 
e2520 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 63 6f 6e 73  name */.    cons
e2530 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
e2540 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 73 3b 20  hods *pMethods; 
e2550 20 20 2f 2a 20 41 70 70 72 6f 70 72 69 61 74 65    /* Appropriate
e2560 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 20   locking method 
e2570 2a 2f 0a 20 20 7d 20 61 4d 61 70 5b 5d 20 3d 20  */.  } aMap[] = 
e2580 7b 0a 20 20 20 20 7b 20 22 68 66 73 22 2c 20 20  {.    { "hfs",  
e2590 20 20 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64    &posixIoMethod
e25a0 73 20 7d 2c 0a 20 20 20 20 7b 20 22 75 66 73 22  s },.    { "ufs"
e25b0 2c 20 20 20 20 26 70 6f 73 69 78 49 6f 4d 65 74  ,    &posixIoMet
e25c0 68 6f 64 73 20 7d 2c 0a 20 20 20 20 7b 20 22 61  hods },.    { "a
e25d0 66 70 66 73 22 2c 20 20 26 61 66 70 49 6f 4d 65  fpfs",  &afpIoMe
e25e0 74 68 6f 64 73 20 7d 2c 0a 23 69 66 64 65 66 20  thods },.#ifdef 
e25f0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 46  SQLITE_ENABLE_AF
e2600 50 5f 4c 4f 43 4b 49 4e 47 5f 53 4d 42 0a 20 20  P_LOCKING_SMB.  
e2610 20 20 7b 20 22 73 6d 62 66 73 22 2c 20 20 26 61    { "smbfs",  &a
e2620 66 70 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 23  fpIoMethods },.#
e2630 65 6c 73 65 0a 20 20 20 20 7b 20 22 73 6d 62 66  else.    { "smbf
e2640 73 22 2c 20 20 26 66 6c 6f 63 6b 49 6f 4d 65 74  s",  &flockIoMet
e2650 68 6f 64 73 20 7d 2c 0a 23 65 6e 64 69 66 0a 20  hods },.#endif. 
e2660 20 20 20 7b 20 22 77 65 62 64 61 76 22 2c 20 26     { "webdav", &
e2670 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 20  nolockIoMethods 
e2680 7d 2c 0a 20 20 20 20 7b 20 30 2c 20 30 20 7d 0a  },.    { 0, 0 }.
e2690 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20    };.  int i;.  
e26a0 73 74 72 75 63 74 20 73 74 61 74 66 73 20 66 73  struct statfs fs
e26b0 49 6e 66 6f 3b 0a 20 20 73 74 72 75 63 74 20 66  Info;.  struct f
e26c0 6c 6f 63 6b 20 6c 6f 63 6b 49 6e 66 6f 3b 0a 0a  lock lockInfo;..
e26d0 20 20 69 66 28 20 21 66 69 6c 65 50 61 74 68 20    if( !filePath 
e26e0 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 66 69 6c  ){.    /* If fil
e26f0 65 50 61 74 68 3d 3d 4e 55 4c 4c 20 74 68 61 74  ePath==NULL that
e2700 20 6d 65 61 6e 73 20 77 65 20 61 72 65 20 64 65   means we are de
e2710 61 6c 69 6e 67 20 77 69 74 68 20 61 20 74 72 61  aling with a tra
e2720 6e 73 69 65 6e 74 20 66 69 6c 65 0a 20 20 20 20  nsient file.    
e2730 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74  ** that does not
e2740 20 6e 65 65 64 20 74 6f 20 62 65 20 6c 6f 63 6b   need to be lock
e2750 65 64 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  ed. */.    retur
e2760 6e 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f  n &nolockIoMetho
e2770 64 73 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 74  ds;.  }.  if( st
e2780 61 74 66 73 28 66 69 6c 65 50 61 74 68 2c 20 26  atfs(filePath, &
e2790 66 73 49 6e 66 6f 29 20 21 3d 20 2d 31 20 29 7b  fsInfo) != -1 ){
e27a0 0a 20 20 20 20 69 66 28 20 66 73 49 6e 66 6f 2e  .    if( fsInfo.
e27b0 66 5f 66 6c 61 67 73 20 26 20 4d 4e 54 5f 52 44  f_flags & MNT_RD
e27c0 4f 4e 4c 59 20 29 7b 0a 20 20 20 20 20 20 72 65  ONLY ){.      re
e27d0 74 75 72 6e 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65  turn &nolockIoMe
e27e0 74 68 6f 64 73 3b 0a 20 20 20 20 7d 0a 20 20 20  thods;.    }.   
e27f0 20 66 6f 72 28 69 3d 30 3b 20 61 4d 61 70 5b 69   for(i=0; aMap[i
e2800 5d 2e 7a 46 69 6c 65 73 79 73 74 65 6d 3b 20 69  ].zFilesystem; i
e2810 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73  ++){.      if( s
e2820 74 72 63 6d 70 28 66 73 49 6e 66 6f 2e 66 5f 66  trcmp(fsInfo.f_f
e2830 73 74 79 70 65 6e 61 6d 65 2c 20 61 4d 61 70 5b  stypename, aMap[
e2840 69 5d 2e 7a 46 69 6c 65 73 79 73 74 65 6d 29 3d  i].zFilesystem)=
e2850 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  =0 ){.        re
e2860 74 75 72 6e 20 61 4d 61 70 5b 69 5d 2e 70 4d 65  turn aMap[i].pMe
e2870 74 68 6f 64 73 3b 0a 20 20 20 20 20 20 7d 0a 20  thods;.      }. 
e2880 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44     }.  }..  /* D
e2890 65 66 61 75 6c 74 20 63 61 73 65 2e 20 48 61 6e  efault case. Han
e28a0 64 6c 65 73 2c 20 61 6d 6f 6e 67 73 74 20 6f 74  dles, amongst ot
e28b0 68 65 72 73 2c 20 22 6e 66 73 22 2e 0a 20 20 2a  hers, "nfs"..  *
e28c0 2a 20 54 65 73 74 20 62 79 74 65 2d 72 61 6e 67  * Test byte-rang
e28d0 65 20 6c 6f 63 6b 20 75 73 69 6e 67 20 66 63 6e  e lock using fcn
e28e0 74 6c 28 29 2e 20 49 66 20 74 68 65 20 63 61 6c  tl(). If the cal
e28f0 6c 20 73 75 63 63 65 65 64 73 2c 20 0a 20 20 2a  l succeeds, .  *
e2900 2a 20 61 73 73 75 6d 65 20 74 68 61 74 20 74 68  * assume that th
e2910 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 73 75  e file-system su
e2920 70 70 6f 72 74 73 20 50 4f 53 49 58 20 73 74 79  pports POSIX sty
e2930 6c 65 20 6c 6f 63 6b 73 2e 20 0a 20 20 2a 2f 0a  le locks. .  */.
e2940 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 6c 65 6e    lockInfo.l_len
e2950 20 3d 20 31 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f   = 1;.  lockInfo
e2960 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b 0a 20 20  .l_start = 0;.  
e2970 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 77 68 65 6e 63  lockInfo.l_whenc
e2980 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20  e = SEEK_SET;.  
e2990 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 74 79 70 65 20  lockInfo.l_type 
e29a0 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 69 66 28  = F_RDLCK;.  if(
e29b0 20 66 63 6e 74 6c 28 70 4e 65 77 2d 3e 68 2c 20   fcntl(pNew->h, 
e29c0 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b 49 6e  F_GETLK, &lockIn
e29d0 66 6f 29 21 3d 2d 31 20 29 20 7b 0a 20 20 20 20  fo)!=-1 ) {.    
e29e0 70 4e 65 77 2d 3e 66 69 6c 65 46 6c 61 67 73 20  pNew->fileFlags 
e29f0 3d 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46  = SQLITE_WHOLE_F
e2a00 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3b 0a 20 20 20  ILE_LOCKING;.   
e2a10 20 72 65 74 75 72 6e 20 26 70 6f 73 69 78 49 6f   return &posixIo
e2a20 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c 73 65  Methods;.  }else
e2a30 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 64 6f  {.    return &do
e2a40 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a  tlockIoMethods;.
e2a50 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e    }.}.static con
e2a60 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  st sqlite3_io_me
e2a70 74 68 6f 64 73 20 0a 20 20 2a 28 2a 63 6f 6e 73  thods .  *(*cons
e2a80 74 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64  t autolockIoFind
e2a90 65 72 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c  er)(const char*,
e2aa0 75 6e 69 78 46 69 6c 65 2a 29 20 3d 20 61 75 74  unixFile*) = aut
e2ab0 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70  olockIoFinderImp
e2ac0 6c 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65  l;..#endif /* de
e2ad0 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29  fined(__APPLE__)
e2ae0 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   && SQLITE_ENABL
e2af0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
e2b00 2a 2f 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52  */..#if OS_VXWOR
e2b10 4b 53 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41  KS && SQLITE_ENA
e2b20 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
e2b30 45 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 22 66  E./* .** This "f
e2b40 69 6e 64 65 72 22 20 66 75 6e 63 74 69 6f 6e 20  inder" function 
e2b50 61 74 74 65 6d 70 74 73 20 74 6f 20 64 65 74 65  attempts to dete
e2b60 72 6d 69 6e 65 20 74 68 65 20 62 65 73 74 20 6c  rmine the best l
e2b70 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79 20  ocking strategy 
e2b80 0a 2a 2a 20 66 6f 72 20 74 68 65 20 64 61 74 61  .** for the data
e2b90 62 61 73 65 20 66 69 6c 65 20 22 66 69 6c 65 50  base file "fileP
e2ba0 61 74 68 22 2e 20 20 49 74 20 74 68 65 6e 20 72  ath".  It then r
e2bb0 65 74 75 72 6e 73 20 74 68 65 20 73 71 6c 69 74  eturns the sqlit
e2bc0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a  e3_io_methods.**
e2bd0 20 6f 62 6a 65 63 74 20 74 68 61 74 20 69 6d 70   object that imp
e2be0 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 73 74 72  lements that str
e2bf0 61 74 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ategy..**.** Thi
e2c00 73 20 69 73 20 66 6f 72 20 56 58 57 6f 72 6b 73  s is for VXWorks
e2c10 20 6f 6e 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63   only..*/.static
e2c20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69   const sqlite3_i
e2c30 6f 5f 6d 65 74 68 6f 64 73 20 2a 61 75 74 6f 6c  o_methods *autol
e2c40 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 28  ockIoFinderImpl(
e2c50 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66  .  const char *f
e2c60 69 6c 65 50 61 74 68 2c 20 20 20 20 2f 2a 20 6e  ilePath,    /* n
e2c70 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
e2c80 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 6e  ase file */.  un
e2c90 69 78 46 69 6c 65 20 2a 70 4e 65 77 20 20 20 20  ixFile *pNew    
e2ca0 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 6f 70         /* the op
e2cb0 65 6e 20 66 69 6c 65 20 6f 62 6a 65 63 74 20 2a  en file object *
e2cc0 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 66 6c  /.){.  struct fl
e2cd0 6f 63 6b 20 6c 6f 63 6b 49 6e 66 6f 3b 0a 0a 20  ock lockInfo;.. 
e2ce0 20 69 66 28 20 21 66 69 6c 65 50 61 74 68 20 29   if( !filePath )
e2cf0 7b 0a 20 20 20 20 2f 2a 20 49 66 20 66 69 6c 65  {.    /* If file
e2d00 50 61 74 68 3d 3d 4e 55 4c 4c 20 74 68 61 74 20  Path==NULL that 
e2d10 6d 65 61 6e 73 20 77 65 20 61 72 65 20 64 65 61  means we are dea
e2d20 6c 69 6e 67 20 77 69 74 68 20 61 20 74 72 61 6e  ling with a tran
e2d30 73 69 65 6e 74 20 66 69 6c 65 0a 20 20 20 20 2a  sient file.    *
e2d40 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20  * that does not 
e2d50 6e 65 65 64 20 74 6f 20 62 65 20 6c 6f 63 6b 65  need to be locke
e2d60 64 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e  d. */.    return
e2d70 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64   &nolockIoMethod
e2d80 73 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73  s;.  }..  /* Tes
e2d90 74 20 69 66 20 66 63 6e 74 6c 28 29 20 69 73 20  t if fcntl() is 
e2da0 73 75 70 70 6f 72 74 65 64 20 61 6e 64 20 75 73  supported and us
e2db0 65 20 50 4f 53 49 58 20 73 74 79 6c 65 20 6c 6f  e POSIX style lo
e2dc0 63 6b 73 2e 0a 20 20 2a 2a 20 4f 74 68 65 72 77  cks..  ** Otherw
e2dd0 69 73 65 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f  ise fall back to
e2de0 20 74 68 65 20 6e 61 6d 65 64 20 73 65 6d 61 70   the named semap
e2df0 68 6f 72 65 20 6d 65 74 68 6f 64 2e 0a 20 20 2a  hore method..  *
e2e00 2f 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 6c  /.  lockInfo.l_l
e2e10 65 6e 20 3d 20 31 3b 0a 20 20 6c 6f 63 6b 49 6e  en = 1;.  lockIn
e2e20 66 6f 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b 0a  fo.l_start = 0;.
e2e30 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 77 68 65    lockInfo.l_whe
e2e40 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a  nce = SEEK_SET;.
e2e50 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 74 79 70    lockInfo.l_typ
e2e60 65 20 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 69  e = F_RDLCK;.  i
e2e70 66 28 20 66 63 6e 74 6c 28 70 4e 65 77 2d 3e 68  f( fcntl(pNew->h
e2e80 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b  , F_GETLK, &lock
e2e90 49 6e 66 6f 29 21 3d 2d 31 20 29 20 7b 0a 20 20  Info)!=-1 ) {.  
e2ea0 20 20 72 65 74 75 72 6e 20 26 70 6f 73 69 78 49    return &posixI
e2eb0 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c 73  oMethods;.  }els
e2ec0 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 73  e{.    return &s
e2ed0 65 6d 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d  emIoMethods;.  }
e2ee0 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .}.static const 
e2ef0 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
e2f00 64 73 20 0a 20 20 2a 28 2a 63 6f 6e 73 74 20 61  ds .  *(*const a
e2f10 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 29  utolockIoFinder)
e2f20 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 6e 69  (const char*,uni
e2f30 78 46 69 6c 65 2a 29 20 3d 20 61 75 74 6f 6c 6f  xFile*) = autolo
e2f40 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 3b 0a  ckIoFinderImpl;.
e2f50 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 58  .#endif /* OS_VX
e2f60 57 4f 52 4b 53 20 26 26 20 53 51 4c 49 54 45 5f  WORKS && SQLITE_
e2f70 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
e2f80 54 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41  TYLE */../*.** A
e2f90 6e 20 61 62 73 74 72 61 63 74 20 74 79 70 65 20  n abstract type 
e2fa0 66 6f 72 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  for a pointer to
e2fb0 20 61 20 49 4f 20 6d 65 74 68 6f 64 20 66 69 6e   a IO method fin
e2fc0 64 65 72 20 66 75 6e 63 74 69 6f 6e 3a 0a 2a 2f  der function:.*/
e2fd0 0a 74 79 70 65 64 65 66 20 63 6f 6e 73 74 20 73  .typedef const s
e2fe0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
e2ff0 73 20 2a 28 2a 66 69 6e 64 65 72 5f 74 79 70 65  s *(*finder_type
e3000 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 6e  )(const char*,un
e3010 69 78 46 69 6c 65 2a 29 3b 0a 0a 0a 2f 2a 2a 2a  ixFile*);.../***
e3020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3060 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a  *********.******
e3070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3080 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 76  ****** sqlite3_v
e3090 66 73 20 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a  fs methods *****
e30a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e30b0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
e30c0 69 73 20 64 69 76 69 73 69 6f 6e 20 63 6f 6e 74  is division cont
e30d0 61 69 6e 73 20 74 68 65 20 69 6d 70 6c 65 6d 65  ains the impleme
e30e0 6e 74 61 74 69 6f 6e 20 6f 66 20 6d 65 74 68 6f  ntation of metho
e30f0 64 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 73 71 6c  ds on the.** sql
e3100 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 2e  ite3_vfs object.
e3110 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69  .*/../*.** Initi
e3120 61 6c 69 7a 65 20 74 68 65 20 63 6f 6e 74 65 6e  alize the conten
e3130 74 73 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69  ts of the unixFi
e3140 6c 65 20 73 74 72 75 63 74 75 72 65 20 70 6f 69  le structure poi
e3150 6e 74 65 64 20 74 6f 20 62 79 20 70 49 64 2e 0a  nted to by pId..
e3160 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69  */.static int fi
e3170 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28 0a 20 20  llInUnixFile(.  
e3180 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
e3190 73 2c 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74  s,      /* Point
e31a0 65 72 20 74 6f 20 76 66 73 20 6f 62 6a 65 63 74  er to vfs object
e31b0 20 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 20 20 20   */.  int h,    
e31c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e31d0 20 4f 70 65 6e 20 66 69 6c 65 20 64 65 73 63 72   Open file descr
e31e0 69 70 74 6f 72 20 6f 66 20 66 69 6c 65 20 62 65  iptor of file be
e31f0 69 6e 67 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20  ing opened */.  
e3200 69 6e 74 20 64 69 72 66 64 2c 20 20 20 20 20 20  int dirfd,      
e3210 20 20 20 20 20 20 20 20 2f 2a 20 44 69 72 65 63          /* Direc
e3220 74 6f 72 79 20 66 69 6c 65 20 64 65 73 63 72 69  tory file descri
e3230 70 74 6f 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ptor */.  sqlite
e3240 33 5f 66 69 6c 65 20 2a 70 49 64 2c 20 20 20 20  3_file *pId,    
e3250 20 20 2f 2a 20 57 72 69 74 65 20 74 6f 20 74 68    /* Write to th
e3260 65 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63  e unixFile struc
e3270 74 75 72 65 20 68 65 72 65 20 2a 2f 0a 20 20 63  ture here */.  c
e3280 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
e3290 6e 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d 65 20 6f  name,  /* Name o
e32a0 66 20 74 68 65 20 66 69 6c 65 20 62 65 69 6e 67  f the file being
e32b0 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 69 6e 74   opened */.  int
e32c0 20 6e 6f 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20   noLock,        
e32d0 20 20 20 20 20 2f 2a 20 4f 6d 69 74 20 6c 6f 63       /* Omit loc
e32e0 6b 69 6e 67 20 69 66 20 74 72 75 65 20 2a 2f 0a  king if true */.
e32f0 20 20 69 6e 74 20 69 73 44 65 6c 65 74 65 20 20    int isDelete  
e3300 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 6c            /* Del
e3310 65 74 65 20 6f 6e 20 63 6c 6f 73 65 20 69 66 20  ete on close if 
e3320 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e  true */.){.  con
e3330 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  st sqlite3_io_me
e3340 74 68 6f 64 73 20 2a 70 4c 6f 63 6b 69 6e 67 53  thods *pLockingS
e3350 74 79 6c 65 3b 0a 20 20 75 6e 69 78 46 69 6c 65  tyle;.  unixFile
e3360 20 2a 70 4e 65 77 20 3d 20 28 75 6e 69 78 46 69   *pNew = (unixFi
e3370 6c 65 20 2a 29 70 49 64 3b 0a 20 20 69 6e 74 20  le *)pId;.  int 
e3380 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
e3390 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d  .  assert( pNew-
e33a0 3e 70 4c 6f 63 6b 3d 3d 4e 55 4c 4c 20 29 3b 0a  >pLock==NULL );.
e33b0 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e    assert( pNew->
e33c0 70 4f 70 65 6e 3d 3d 4e 55 4c 4c 20 29 3b 0a 0a  pOpen==NULL );..
e33d0 20 20 2f 2a 20 50 61 72 61 6d 65 74 65 72 20 69    /* Parameter i
e33e0 73 44 65 6c 65 74 65 20 69 73 20 6f 6e 6c 79 20  sDelete is only 
e33f0 75 73 65 64 20 6f 6e 20 76 78 77 6f 72 6b 73 2e  used on vxworks.
e3400 20 45 78 70 72 65 73 73 20 74 68 69 73 20 65 78   Express this ex
e3410 70 6c 69 63 69 74 6c 79 20 0a 20 20 2a 2a 20 68  plicitly .  ** h
e3420 65 72 65 20 74 6f 20 70 72 65 76 65 6e 74 20 63  ere to prevent c
e3430 6f 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e 67 73  ompiler warnings
e3440 20 61 62 6f 75 74 20 75 6e 75 73 65 64 20 70 61   about unused pa
e3450 72 61 6d 65 74 65 72 73 2e 0a 20 20 2a 2f 0a 20  rameters..  */. 
e3460 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
e3470 52 28 69 73 44 65 6c 65 74 65 29 3b 0a 0a 20 20  R(isDelete);..  
e3480 4f 53 54 52 41 43 45 33 28 22 4f 50 45 4e 20 20  OSTRACE3("OPEN  
e3490 20 20 25 2d 33 64 20 25 73 5c 6e 22 2c 20 68 2c    %-3d %s\n", h,
e34a0 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 20 20 20 20   zFilename);    
e34b0 0a 20 20 70 4e 65 77 2d 3e 68 20 3d 20 68 3b 0a  .  pNew->h = h;.
e34c0 20 20 70 4e 65 77 2d 3e 64 69 72 66 64 20 3d 20    pNew->dirfd = 
e34d0 64 69 72 66 64 3b 0a 20 20 53 45 54 5f 54 48 52  dirfd;.  SET_THR
e34e0 45 41 44 49 44 28 70 4e 65 77 29 3b 0a 20 20 70  EADID(pNew);.  p
e34f0 4e 65 77 2d 3e 66 69 6c 65 46 6c 61 67 73 20 3d  New->fileFlags =
e3500 20 30 3b 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f   0;..#if OS_VXWO
e3510 52 4b 53 0a 20 20 70 4e 65 77 2d 3e 70 49 64 20  RKS.  pNew->pId 
e3520 3d 20 76 78 77 6f 72 6b 73 46 69 6e 64 46 69 6c  = vxworksFindFil
e3530 65 49 64 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a  eId(zFilename);.
e3540 20 20 69 66 28 20 70 4e 65 77 2d 3e 70 49 64 3d    if( pNew->pId=
e3550 3d 30 20 29 7b 0a 20 20 20 20 6e 6f 4c 6f 63 6b  =0 ){.    noLock
e3560 20 3d 20 31 3b 0a 20 20 20 20 72 63 20 3d 20 53   = 1;.    rc = S
e3570 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
e3580 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 6e  .#endif..  if( n
e3590 6f 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f  oLock ){.    pLo
e35a0 63 6b 69 6e 67 53 74 79 6c 65 20 3d 20 26 6e 6f  ckingStyle = &no
e35b0 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20  lockIoMethods;. 
e35c0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4c 6f 63   }else{.    pLoc
e35d0 6b 69 6e 67 53 74 79 6c 65 20 3d 20 28 2a 2a 28  kingStyle = (**(
e35e0 66 69 6e 64 65 72 5f 74 79 70 65 2a 29 70 56 66  finder_type*)pVf
e35f0 73 2d 3e 70 41 70 70 44 61 74 61 29 28 7a 46 69  s->pAppData)(zFi
e3600 6c 65 6e 61 6d 65 2c 20 70 4e 65 77 29 3b 0a 23  lename, pNew);.#
e3610 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  if SQLITE_ENABLE
e3620 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20  _LOCKING_STYLE. 
e3630 20 20 20 2f 2a 20 43 61 63 68 65 20 7a 46 69 6c     /* Cache zFil
e3640 65 6e 61 6d 65 20 69 6e 20 74 68 65 20 6c 6f 63  ename in the loc
e3650 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 28 41 46  king context (AF
e3660 50 20 61 6e 64 20 64 6f 74 6c 6f 63 6b 20 6f 76  P and dotlock ov
e3670 65 72 72 69 64 65 29 20 66 6f 72 0a 20 20 20 20  erride) for.    
e3680 2a 2a 20 70 72 6f 78 79 4c 6f 63 6b 20 61 63 74  ** proxyLock act
e3690 69 76 61 74 69 6f 6e 20 69 73 20 70 6f 73 73 69  ivation is possi
e36a0 62 6c 65 20 28 72 65 6d 6f 74 65 20 70 72 6f 78  ble (remote prox
e36b0 79 20 69 73 20 62 61 73 65 64 20 6f 6e 20 64 62  y is based on db
e36c0 20 6e 61 6d 65 29 0a 20 20 20 20 2a 2a 20 7a 46   name).    ** zF
e36d0 69 6c 65 6e 61 6d 65 20 72 65 6d 61 69 6e 73 20  ilename remains 
e36e0 76 61 6c 69 64 20 75 6e 74 69 6c 20 66 69 6c 65  valid until file
e36f0 20 69 73 20 63 6c 6f 73 65 64 2c 20 74 6f 20 73   is closed, to s
e3700 75 70 70 6f 72 74 20 2a 2f 0a 20 20 20 20 70 4e  upport */.    pN
e3710 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  ew->lockingConte
e3720 78 74 20 3d 20 28 76 6f 69 64 2a 29 7a 46 69 6c  xt = (void*)zFil
e3730 65 6e 61 6d 65 3b 0a 23 65 6e 64 69 66 0a 20 20  ename;.#endif.  
e3740 7d 0a 0a 20 20 69 66 28 20 70 4c 6f 63 6b 69 6e  }..  if( pLockin
e3750 67 53 74 79 6c 65 20 3d 3d 20 26 70 6f 73 69 78  gStyle == &posix
e3760 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20  IoMethods ){.   
e3770 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28   unixEnterMutex(
e3780 29 3b 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64  );.    rc = find
e3790 4c 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c 20 26  LockInfo(pNew, &
e37a0 70 4e 65 77 2d 3e 70 4c 6f 63 6b 2c 20 26 70 4e  pNew->pLock, &pN
e37b0 65 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20  ew->pOpen);.    
e37c0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
e37d0 4b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66  K ){.      /* If
e37e0 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 65   an error occure
e37f0 64 20 69 6e 20 66 69 6e 64 4c 6f 63 6b 49 6e 66  d in findLockInf
e3800 6f 28 29 2c 20 63 6c 6f 73 65 20 74 68 65 20 66  o(), close the f
e3810 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 0a 20  ile descriptor. 
e3820 20 20 20 20 20 2a 2a 20 69 6d 6d 65 64 69 61 74       ** immediat
e3830 65 6c 79 2c 20 62 65 66 6f 72 65 20 72 65 6c 65  ely, before rele
e3840 61 73 69 6e 67 20 74 68 65 20 6d 75 74 65 78 2e  asing the mutex.
e3850 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 20   findLockInfo() 
e3860 6d 61 79 20 66 61 69 6c 0a 20 20 20 20 20 20 2a  may fail.      *
e3870 2a 20 69 6e 20 74 77 6f 20 73 63 65 6e 61 72 69  * in two scenari
e3880 6f 73 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  os:.      **.   
e3890 20 20 20 2a 2a 20 20 20 28 61 29 20 41 20 63 61     **   (a) A ca
e38a0 6c 6c 20 74 6f 20 66 73 74 61 74 28 29 20 66 61  ll to fstat() fa
e38b0 69 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2a 20 20  iled..      **  
e38c0 20 28 62 29 20 41 20 6d 61 6c 6c 6f 63 20 66 61   (b) A malloc fa
e38d0 69 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2a 0a 20  iled..      **. 
e38e0 20 20 20 20 20 2a 2a 20 53 63 65 6e 61 72 69 6f       ** Scenario
e38f0 20 28 62 29 20 6d 61 79 20 6f 6e 6c 79 20 6f 63   (b) may only oc
e3900 63 75 72 20 69 66 20 74 68 65 20 70 72 6f 63 65  cur if the proce
e3910 73 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 6e 6f  ss is holding no
e3920 20 6f 74 68 65 72 0a 20 20 20 20 20 20 2a 2a 20   other.      ** 
e3930 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73  file descriptors
e3940 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 73 61 6d   open on the sam
e3950 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65 72 65  e file. If there
e3960 20 77 65 72 65 20 6f 74 68 65 72 20 66 69 6c 65   were other file
e3970 0a 20 20 20 20 20 20 2a 2a 20 64 65 73 63 72 69  .      ** descri
e3980 70 74 6f 72 73 20 6f 6e 20 74 68 69 73 20 66 69  ptors on this fi
e3990 6c 65 2c 20 74 68 65 6e 20 6e 6f 20 6d 61 6c 6c  le, then no mall
e39a0 6f 63 20 77 6f 75 6c 64 20 62 65 20 72 65 71 75  oc would be requ
e39b0 69 72 65 64 20 62 79 0a 20 20 20 20 20 20 2a 2a  ired by.      **
e39c0 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 2e   findLockInfo().
e39d0 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20   If this is the 
e39e0 63 61 73 65 2c 20 69 74 20 69 73 20 71 75 69 74  case, it is quit
e39f0 65 20 73 61 66 65 20 74 6f 20 63 6c 6f 73 65 0a  e safe to close.
e3a00 20 20 20 20 20 20 2a 2a 20 68 61 6e 64 6c 65 20        ** handle 
e3a10 68 20 2d 20 61 73 20 69 74 20 69 73 20 67 75 61  h - as it is gua
e3a20 72 61 6e 74 65 65 64 20 74 68 61 74 20 6e 6f 20  ranteed that no 
e3a30 70 6f 73 69 78 20 6c 6f 63 6b 73 20 77 69 6c 6c  posix locks will
e3a40 20 62 65 20 72 65 6c 65 61 73 65 64 0a 20 20 20   be released.   
e3a50 20 20 20 2a 2a 20 62 79 20 64 6f 69 6e 67 20 73     ** by doing s
e3a60 6f 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  o..      **.    
e3a70 20 20 2a 2a 20 49 66 20 73 63 65 6e 61 72 69 6f    ** If scenario
e3a80 20 28 61 29 20 63 61 75 73 65 64 20 74 68 65 20   (a) caused the 
e3a90 65 72 72 6f 72 20 74 68 65 6e 20 74 68 69 6e 67  error then thing
e3aa0 73 20 61 72 65 20 6e 6f 74 20 73 6f 20 73 61 66  s are not so saf
e3ab0 65 2e 20 54 68 65 0a 20 20 20 20 20 20 2a 2a 20  e. The.      ** 
e3ac0 69 6d 70 6c 69 63 69 74 20 61 73 73 75 6d 70 74  implicit assumpt
e3ad0 69 6f 6e 20 68 65 72 65 20 69 73 20 74 68 61 74  ion here is that
e3ae0 20 69 66 20 66 73 74 61 74 28 29 20 66 61 69 6c   if fstat() fail
e3af0 73 2c 20 74 68 69 6e 67 73 20 61 72 65 20 69 6e  s, things are in
e3b00 0a 20 20 20 20 20 20 2a 2a 20 73 75 63 68 20 62  .      ** such b
e3b10 61 64 20 73 68 61 70 65 20 74 68 61 74 20 64 72  ad shape that dr
e3b20 6f 70 70 69 6e 67 20 61 20 6c 6f 63 6b 20 6f 72  opping a lock or
e3b30 20 74 77 6f 20 64 6f 65 73 6e 27 74 20 6d 61 74   two doesn't mat
e3b40 74 65 72 20 6d 75 63 68 2e 0a 20 20 20 20 20 20  ter much..      
e3b50 2a 2f 0a 20 20 20 20 20 20 63 6c 6f 73 65 28 68  */.      close(h
e3b60 29 3b 0a 20 20 20 20 20 20 68 20 3d 20 2d 31 3b  );.      h = -1;
e3b70 0a 20 20 20 20 7d 0a 20 20 20 20 75 6e 69 78 4c  .    }.    unixL
e3b80 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d  eaveMutex();.  }
e3b90 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41  ..#if SQLITE_ENA
e3ba0 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
e3bb0 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41  E && defined(__A
e3bc0 50 50 4c 45 5f 5f 29 0a 20 20 65 6c 73 65 20 69  PPLE__).  else i
e3bd0 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65  f( pLockingStyle
e3be0 20 3d 3d 20 26 61 66 70 49 6f 4d 65 74 68 6f 64   == &afpIoMethod
e3bf0 73 20 29 7b 0a 20 20 20 20 2f 2a 20 41 46 50 20  s ){.    /* AFP 
e3c00 6c 6f 63 6b 69 6e 67 20 75 73 65 73 20 74 68 65  locking uses the
e3c10 20 66 69 6c 65 20 70 61 74 68 20 73 6f 20 69 74   file path so it
e3c20 20 6e 65 65 64 73 20 74 6f 20 62 65 20 69 6e 63   needs to be inc
e3c30 6c 75 64 65 64 20 69 6e 0a 20 20 20 20 2a 2a 20  luded in.    ** 
e3c40 74 68 65 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f  the afpLockingCo
e3c50 6e 74 65 78 74 2e 0a 20 20 20 20 2a 2f 0a 20 20  ntext..    */.  
e3c60 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74    afpLockingCont
e3c70 65 78 74 20 2a 70 43 74 78 3b 0a 20 20 20 20 70  ext *pCtx;.    p
e3c80 4e 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74  New->lockingCont
e3c90 65 78 74 20 3d 20 70 43 74 78 20 3d 20 73 71 6c  ext = pCtx = sql
e3ca0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a  ite3_malloc( siz
e3cb0 65 6f 66 28 2a 70 43 74 78 29 20 29 3b 0a 20 20  eof(*pCtx) );.  
e3cc0 20 20 69 66 28 20 70 43 74 78 3d 3d 30 20 29 7b    if( pCtx==0 ){
e3cd0 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
e3ce0 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65  TE_NOMEM;.    }e
e3cf0 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 4e 42  lse{.      /* NB
e3d00 3a 20 7a 46 69 6c 65 6e 61 6d 65 20 65 78 69 73  : zFilename exis
e3d10 74 73 20 61 6e 64 20 72 65 6d 61 69 6e 73 20 76  ts and remains v
e3d20 61 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 20 66  alid until the f
e3d30 69 6c 65 20 69 73 20 63 6c 6f 73 65 64 0a 20 20  ile is closed.  
e3d40 20 20 20 20 2a 2a 20 61 63 63 6f 72 64 69 6e 67      ** according
e3d50 20 74 6f 20 72 65 71 75 69 72 65 6d 65 6e 74 20   to requirement 
e3d60 46 31 31 31 34 31 2e 20 20 53 6f 20 77 65 20 64  F11141.  So we d
e3d70 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61  o not need to ma
e3d80 6b 65 20 61 0a 20 20 20 20 20 20 2a 2a 20 63 6f  ke a.      ** co
e3d90 70 79 20 6f 66 20 74 68 65 20 66 69 6c 65 6e 61  py of the filena
e3da0 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20 70 43 74  me. */.      pCt
e3db0 78 2d 3e 64 62 50 61 74 68 20 3d 20 7a 46 69 6c  x->dbPath = zFil
e3dc0 65 6e 61 6d 65 3b 0a 20 20 20 20 20 20 73 72 61  ename;.      sra
e3dd0 6e 64 6f 6d 64 65 76 28 29 3b 0a 20 20 20 20 20  ndomdev();.     
e3de0 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28   unixEnterMutex(
e3df0 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 69  );.      rc = fi
e3e00 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c  ndLockInfo(pNew,
e3e10 20 4e 55 4c 4c 2c 20 26 70 4e 65 77 2d 3e 70 4f   NULL, &pNew->pO
e3e20 70 65 6e 29 3b 0a 20 20 20 20 20 20 75 6e 69 78  pen);.      unix
e3e30 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 20 20 20  LeaveMutex();   
e3e40 20 20 20 20 20 0a 20 20 20 20 7d 0a 20 20 7d 0a       .    }.  }.
e3e50 23 65 6e 64 69 66 0a 0a 20 20 65 6c 73 65 20 69  #endif..  else i
e3e60 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65  f( pLockingStyle
e3e70 20 3d 3d 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65   == &dotlockIoMe
e3e80 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20  thods ){.    /* 
e3e90 44 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20  Dotfile locking 
e3ea0 75 73 65 73 20 74 68 65 20 66 69 6c 65 20 70 61  uses the file pa
e3eb0 74 68 20 73 6f 20 69 74 20 6e 65 65 64 73 20 74  th so it needs t
e3ec0 6f 20 62 65 20 69 6e 63 6c 75 64 65 64 20 69 6e  o be included in
e3ed0 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 6f 74 6c  .    ** the dotl
e3ee0 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  ockLockingContex
e3ef0 74 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 68  t .    */.    ch
e3f00 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 3b 0a 20  ar *zLockFile;. 
e3f10 20 20 20 69 6e 74 20 6e 46 69 6c 65 6e 61 6d 65     int nFilename
e3f20 3b 0a 20 20 20 20 6e 46 69 6c 65 6e 61 6d 65 20  ;.    nFilename 
e3f30 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 46  = (int)strlen(zF
e3f40 69 6c 65 6e 61 6d 65 29 20 2b 20 36 3b 0a 20 20  ilename) + 6;.  
e3f50 20 20 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63    zLockFile = (c
e3f60 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61  har *)sqlite3_ma
e3f70 6c 6c 6f 63 28 6e 46 69 6c 65 6e 61 6d 65 29 3b  lloc(nFilename);
e3f80 0a 20 20 20 20 69 66 28 20 7a 4c 6f 63 6b 46 69  .    if( zLockFi
e3f90 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72  le==0 ){.      r
e3fa0 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
e3fb0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
e3fc0 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
e3fd0 6e 74 66 28 6e 46 69 6c 65 6e 61 6d 65 2c 20 7a  ntf(nFilename, z
e3fe0 4c 6f 63 6b 46 69 6c 65 2c 20 22 25 73 22 20 44  LockFile, "%s" D
e3ff0 4f 54 4c 4f 43 4b 5f 53 55 46 46 49 58 2c 20 7a  OTLOCK_SUFFIX, z
e4000 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7d  Filename);.    }
e4010 0a 20 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b 69  .    pNew->locki
e4020 6e 67 43 6f 6e 74 65 78 74 20 3d 20 7a 4c 6f 63  ngContext = zLoc
e4030 6b 46 69 6c 65 3b 0a 20 20 7d 0a 0a 23 69 66 20  kFile;.  }..#if 
e4040 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 65 6c 73  OS_VXWORKS.  els
e4050 65 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74  e if( pLockingSt
e4060 79 6c 65 20 3d 3d 20 26 73 65 6d 49 6f 4d 65 74  yle == &semIoMet
e4070 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 4e  hods ){.    /* N
e4080 61 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c  amed semaphore l
e4090 6f 63 6b 69 6e 67 20 75 73 65 73 20 74 68 65 20  ocking uses the 
e40a0 66 69 6c 65 20 70 61 74 68 20 73 6f 20 69 74 20  file path so it 
e40b0 6e 65 65 64 73 20 74 6f 20 62 65 0a 20 20 20 20  needs to be.    
e40c0 2a 2a 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74  ** included in t
e40d0 68 65 20 73 65 6d 4c 6f 63 6b 69 6e 67 43 6f 6e  he semLockingCon
e40e0 74 65 78 74 0a 20 20 20 20 2a 2f 0a 20 20 20 20  text.    */.    
e40f0 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29  unixEnterMutex()
e4100 3b 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c  ;.    rc = findL
e4110 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c 20 26 70  ockInfo(pNew, &p
e4120 4e 65 77 2d 3e 70 4c 6f 63 6b 2c 20 26 70 4e 65  New->pLock, &pNe
e4130 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 69  w->pOpen);.    i
e4140 66 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  f( (rc==SQLITE_O
e4150 4b 29 20 26 26 20 28 70 4e 65 77 2d 3e 70 4f 70  K) && (pNew->pOp
e4160 65 6e 2d 3e 70 53 65 6d 3d 3d 4e 55 4c 4c 29 20  en->pSem==NULL) 
e4170 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  ){.      char *z
e4180 53 65 6d 4e 61 6d 65 20 3d 20 70 4e 65 77 2d 3e  SemName = pNew->
e4190 70 4f 70 65 6e 2d 3e 61 53 65 6d 4e 61 6d 65 3b  pOpen->aSemName;
e41a0 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20  .      int n;.  
e41b0 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
e41c0 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 4e 41 4d  intf(MAX_PATHNAM
e41d0 45 2c 20 7a 53 65 6d 4e 61 6d 65 2c 20 22 2f 25  E, zSemName, "/%
e41e0 73 2e 73 65 6d 22 2c 0a 20 20 20 20 20 20 20 20  s.sem",.        
e41f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
e4200 4e 65 77 2d 3e 70 49 64 2d 3e 7a 43 61 6e 6f 6e  New->pId->zCanon
e4210 69 63 61 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20  icalName);.     
e4220 20 66 6f 72 28 20 6e 3d 31 3b 20 7a 53 65 6d 4e   for( n=1; zSemN
e4230 61 6d 65 5b 6e 5d 3b 20 6e 2b 2b 20 29 0a 20 20  ame[n]; n++ ).  
e4240 20 20 20 20 20 20 69 66 28 20 7a 53 65 6d 4e 61        if( zSemNa
e4250 6d 65 5b 6e 5d 3d 3d 27 2f 27 20 29 20 7a 53 65  me[n]=='/' ) zSe
e4260 6d 4e 61 6d 65 5b 6e 5d 20 3d 20 27 5f 27 3b 0a  mName[n] = '_';.
e4270 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4f 70 65        pNew->pOpe
e4280 6e 2d 3e 70 53 65 6d 20 3d 20 73 65 6d 5f 6f 70  n->pSem = sem_op
e4290 65 6e 28 7a 53 65 6d 4e 61 6d 65 2c 20 4f 5f 43  en(zSemName, O_C
e42a0 52 45 41 54 2c 20 30 36 36 36 2c 20 31 29 3b 0a  REAT, 0666, 1);.
e42b0 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 2d 3e        if( pNew->
e42c0 70 4f 70 65 6e 2d 3e 70 53 65 6d 20 3d 3d 20 53  pOpen->pSem == S
e42d0 45 4d 5f 46 41 49 4c 45 44 20 29 7b 0a 20 20 20  EM_FAILED ){.   
e42e0 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
e42f0 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20  _NOMEM;.        
e4300 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e 61 53 65  pNew->pOpen->aSe
e4310 6d 4e 61 6d 65 5b 30 5d 20 3d 20 27 5c 30 27 3b  mName[0] = '\0';
e4320 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
e4330 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65     unixLeaveMute
e4340 78 28 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  x();.  }.#endif.
e4350 20 20 0a 20 20 70 4e 65 77 2d 3e 6c 61 73 74 45    .  pNew->lastE
e4360 72 72 6e 6f 20 3d 20 30 3b 0a 23 69 66 20 4f 53  rrno = 0;.#if OS
e4370 5f 56 58 57 4f 52 4b 53 0a 20 20 69 66 28 20 72  _VXWORKS.  if( r
e4380 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
e4390 20 20 20 20 75 6e 6c 69 6e 6b 28 7a 46 69 6c 65      unlink(zFile
e43a0 6e 61 6d 65 29 3b 0a 20 20 20 20 69 73 44 65 6c  name);.    isDel
e43b0 65 74 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70  ete = 0;.  }.  p
e43c0 4e 65 77 2d 3e 69 73 44 65 6c 65 74 65 20 3d 20  New->isDelete = 
e43d0 69 73 44 65 6c 65 74 65 3b 0a 23 65 6e 64 69 66  isDelete;.#endif
e43e0 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
e43f0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20  E_OK ){.    if( 
e4400 64 69 72 66 64 3e 3d 30 20 29 20 63 6c 6f 73 65  dirfd>=0 ) close
e4410 28 64 69 72 66 64 29 3b 20 2f 2a 20 73 69 6c 65  (dirfd); /* sile
e4420 6e 74 20 6c 65 61 6b 20 69 66 20 66 61 69 6c 2c  nt leak if fail,
e4430 20 61 6c 72 65 61 64 79 20 69 6e 20 65 72 72 6f   already in erro
e4440 72 20 2a 2f 0a 20 20 20 20 69 66 28 20 68 3e 3d  r */.    if( h>=
e4450 30 20 29 20 63 6c 6f 73 65 28 68 29 3b 0a 20 20  0 ) close(h);.  
e4460 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 2d  }else{.    pNew-
e4470 3e 70 4d 65 74 68 6f 64 20 3d 20 70 4c 6f 63 6b  >pMethod = pLock
e4480 69 6e 67 53 74 79 6c 65 3b 0a 20 20 20 20 4f 70  ingStyle;.    Op
e4490 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20  enCounter(+1);. 
e44a0 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
e44b0 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20  }../*.** Open a 
e44c0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
e44d0 74 6f 20 74 68 65 20 64 69 72 65 63 74 6f 72 79  to the directory
e44e0 20 63 6f 6e 74 61 69 6e 69 6e 67 20 66 69 6c 65   containing file
e44f0 20 7a 46 69 6c 65 6e 61 6d 65 2e 0a 2a 2a 20 49   zFilename..** I
e4500 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 2a 70  f successful, *p
e4510 46 64 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  Fd is set to the
e4520 20 6f 70 65 6e 65 64 20 66 69 6c 65 20 64 65 73   opened file des
e4530 63 72 69 70 74 6f 72 20 61 6e 64 0a 2a 2a 20 53  criptor and.** S
e4540 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
e4550 72 6e 65 64 2e 20 49 66 20 61 6e 20 65 72 72 6f  rned. If an erro
e4560 72 20 6f 63 63 75 72 73 2c 20 65 69 74 68 65 72  r occurs, either
e4570 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a   SQLITE_NOMEM.**
e4580 20 6f 72 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f   or SQLITE_CANTO
e4590 50 45 4e 20 69 73 20 72 65 74 75 72 6e 65 64 20  PEN is returned 
e45a0 61 6e 64 20 2a 70 46 64 20 69 73 20 73 65 74 20  and *pFd is set 
e45b0 74 6f 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 0a  to an undefined.
e45c0 2a 2a 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20  ** value..**.** 
e45d0 49 66 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  If SQLITE_OK is 
e45e0 72 65 74 75 72 6e 65 64 2c 20 74 68 65 20 63 61  returned, the ca
e45f0 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73 69  ller is responsi
e4600 62 6c 65 20 66 6f 72 20 63 6c 6f 73 69 6e 67 0a  ble for closing.
e4610 2a 2a 20 74 68 65 20 66 69 6c 65 20 64 65 73 63  ** the file desc
e4620 72 69 70 74 6f 72 20 2a 70 46 64 20 75 73 69 6e  riptor *pFd usin
e4630 67 20 63 6c 6f 73 65 28 29 2e 0a 2a 2f 0a 73 74  g close()..*/.st
e4640 61 74 69 63 20 69 6e 74 20 6f 70 65 6e 44 69 72  atic int openDir
e4650 65 63 74 6f 72 79 28 63 6f 6e 73 74 20 63 68 61  ectory(const cha
e4660 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 69 6e  r *zFilename, in
e4670 74 20 2a 70 46 64 29 7b 0a 20 20 69 6e 74 20 69  t *pFd){.  int i
e4680 69 3b 0a 20 20 69 6e 74 20 66 64 20 3d 20 2d 31  i;.  int fd = -1
e4690 3b 0a 20 20 63 68 61 72 20 7a 44 69 72 6e 61 6d  ;.  char zDirnam
e46a0 65 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31  e[MAX_PATHNAME+1
e46b0 5d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e  ];..  sqlite3_sn
e46c0 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 4e  printf(MAX_PATHN
e46d0 41 4d 45 2c 20 7a 44 69 72 6e 61 6d 65 2c 20 22  AME, zDirname, "
e46e0 25 73 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b  %s", zFilename);
e46f0 0a 20 20 66 6f 72 28 69 69 3d 28 69 6e 74 29 73  .  for(ii=(int)s
e4700 74 72 6c 65 6e 28 7a 44 69 72 6e 61 6d 65 29 3b  trlen(zDirname);
e4710 20 69 69 3e 31 20 26 26 20 7a 44 69 72 6e 61 6d   ii>1 && zDirnam
e4720 65 5b 69 69 5d 21 3d 27 2f 27 3b 20 69 69 2d 2d  e[ii]!='/'; ii--
e4730 29 3b 0a 20 20 69 66 28 20 69 69 3e 30 20 29 7b  );.  if( ii>0 ){
e4740 0a 20 20 20 20 7a 44 69 72 6e 61 6d 65 5b 69 69  .    zDirname[ii
e4750 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 66 64  ] = '\0';.    fd
e4760 20 3d 20 6f 70 65 6e 28 7a 44 69 72 6e 61 6d 65   = open(zDirname
e4770 2c 20 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 42 49 4e  , O_RDONLY|O_BIN
e4780 41 52 59 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  ARY, 0);.    if(
e4790 20 66 64 3e 3d 30 20 29 7b 0a 23 69 66 64 65 66   fd>=0 ){.#ifdef
e47a0 20 46 44 5f 43 4c 4f 45 58 45 43 0a 20 20 20 20   FD_CLOEXEC.    
e47b0 20 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 53 45    fcntl(fd, F_SE
e47c0 54 46 44 2c 20 66 63 6e 74 6c 28 66 64 2c 20 46  TFD, fcntl(fd, F
e47d0 5f 47 45 54 46 44 2c 20 30 29 20 7c 20 46 44 5f  _GETFD, 0) | FD_
e47e0 43 4c 4f 45 58 45 43 29 3b 0a 23 65 6e 64 69 66  CLOEXEC);.#endif
e47f0 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 33 28  .      OSTRACE3(
e4800 22 4f 50 45 4e 44 49 52 20 25 2d 33 64 20 25 73  "OPENDIR %-3d %s
e4810 5c 6e 22 2c 20 66 64 2c 20 7a 44 69 72 6e 61 6d  \n", fd, zDirnam
e4820 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  e);.    }.  }.  
e4830 2a 70 46 64 20 3d 20 66 64 3b 0a 20 20 72 65 74  *pFd = fd;.  ret
e4840 75 72 6e 20 28 66 64 3e 3d 30 3f 53 51 4c 49 54  urn (fd>=0?SQLIT
e4850 45 5f 4f 4b 3a 53 51 4c 49 54 45 5f 43 41 4e 54  E_OK:SQLITE_CANT
e4860 4f 50 45 4e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  OPEN);.}../*.** 
e4870 43 72 65 61 74 65 20 61 20 74 65 6d 70 6f 72 61  Create a tempora
e4880 72 79 20 66 69 6c 65 20 6e 61 6d 65 20 69 6e 20  ry file name in 
e4890 7a 42 75 66 2e 20 20 7a 42 75 66 20 6d 75 73 74  zBuf.  zBuf must
e48a0 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a   be allocated.**
e48b0 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20   by the calling 
e48c0 70 72 6f 63 65 73 73 20 61 6e 64 20 6d 75 73 74  process and must
e48d0 20 62 65 20 62 69 67 20 65 6e 6f 75 67 68 20 74   be big enough t
e48e0 6f 20 68 6f 6c 64 20 61 74 20 6c 65 61 73 74 0a  o hold at least.
e48f0 2a 2a 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e  ** pVfs->mxPathn
e4900 61 6d 65 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74  ame bytes..*/.st
e4910 61 74 69 63 20 69 6e 74 20 67 65 74 54 65 6d 70  atic int getTemp
e4920 6e 61 6d 65 28 69 6e 74 20 6e 42 75 66 2c 20 63  name(int nBuf, c
e4930 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 73 74  har *zBuf){.  st
e4940 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
e4950 2a 61 7a 44 69 72 73 5b 5d 20 3d 20 7b 0a 20 20  *azDirs[] = {.  
e4960 20 20 20 30 2c 0a 20 20 20 20 20 30 2c 0a 20 20     0,.     0,.  
e4970 20 20 20 22 2f 76 61 72 2f 74 6d 70 22 2c 0a 20     "/var/tmp",. 
e4980 20 20 20 20 22 2f 75 73 72 2f 74 6d 70 22 2c 0a      "/usr/tmp",.
e4990 20 20 20 20 20 22 2f 74 6d 70 22 2c 0a 20 20 20       "/tmp",.   
e49a0 20 20 22 2e 22 2c 0a 20 20 7d 3b 0a 20 20 73 74    ".",.  };.  st
e49b0 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67  atic const unsig
e49c0 6e 65 64 20 63 68 61 72 20 7a 43 68 61 72 73 5b  ned char zChars[
e49d0 5d 20 3d 0a 20 20 20 20 22 61 62 63 64 65 66 67  ] =.    "abcdefg
e49e0 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77  hijklmnopqrstuvw
e49f0 78 79 7a 22 0a 20 20 20 20 22 41 42 43 44 45 46  xyz".    "ABCDEF
e4a00 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56  GHIJKLMNOPQRSTUV
e4a10 57 58 59 5a 22 0a 20 20 20 20 22 30 31 32 33 34  WXYZ".    "01234
e4a20 35 36 37 38 39 22 3b 0a 20 20 75 6e 73 69 67 6e  56789";.  unsign
e4a30 65 64 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 73  ed int i, j;.  s
e4a40 74 72 75 63 74 20 73 74 61 74 20 62 75 66 3b 0a  truct stat buf;.
e4a50 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
e4a60 69 72 20 3d 20 22 2e 22 3b 0a 0a 20 20 2f 2a 20  ir = ".";..  /* 
e4a70 49 74 27 73 20 6f 64 64 20 74 6f 20 73 69 6d 75  It's odd to simu
e4a80 6c 61 74 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72  late an io-error
e4a90 20 68 65 72 65 2c 20 62 75 74 20 72 65 61 6c 6c   here, but reall
e4aa0 79 20 74 68 69 73 20 69 73 20 6a 75 73 74 0a 20  y this is just. 
e4ab0 20 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 69 6f   ** using the io
e4ac0 2d 65 72 72 6f 72 20 69 6e 66 72 61 73 74 72 75  -error infrastru
e4ad0 63 74 75 72 65 20 74 6f 20 74 65 73 74 20 74 68  cture to test th
e4ae0 61 74 20 53 51 4c 69 74 65 20 68 61 6e 64 6c 65  at SQLite handle
e4af0 73 20 74 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63  s this.  ** func
e4b00 74 69 6f 6e 20 66 61 69 6c 69 6e 67 2e 20 0a 20  tion failing. . 
e4b10 20 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f   */.  SimulateIO
e4b20 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51  Error( return SQ
e4b30 4c 49 54 45 5f 49 4f 45 52 52 20 29 3b 0a 0a 20  LITE_IOERR );.. 
e4b40 20 61 7a 44 69 72 73 5b 30 5d 20 3d 20 73 71 6c   azDirs[0] = sql
e4b50 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74  ite3_temp_direct
e4b60 6f 72 79 3b 0a 20 20 69 66 20 28 4e 55 4c 4c 20  ory;.  if (NULL 
e4b70 3d 3d 20 61 7a 44 69 72 73 5b 31 5d 29 20 7b 0a  == azDirs[1]) {.
e4b80 20 20 20 20 61 7a 44 69 72 73 5b 31 5d 20 3d 20      azDirs[1] = 
e4b90 67 65 74 65 6e 76 28 22 54 4d 50 44 49 52 22 29  getenv("TMPDIR")
e4ba0 3b 0a 20 20 7d 0a 20 20 0a 20 20 66 6f 72 28 69  ;.  }.  .  for(i
e4bb0 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 7a 44  =0; i<sizeof(azD
e4bc0 69 72 73 29 2f 73 69 7a 65 6f 66 28 61 7a 44 69  irs)/sizeof(azDi
e4bd0 72 73 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20  rs[0]); i++){.  
e4be0 20 20 69 66 28 20 61 7a 44 69 72 73 5b 69 5d 3d    if( azDirs[i]=
e4bf0 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  =0 ) continue;. 
e4c00 20 20 20 69 66 28 20 73 74 61 74 28 61 7a 44 69     if( stat(azDi
e4c10 72 73 5b 69 5d 2c 20 26 62 75 66 29 20 29 20 63  rs[i], &buf) ) c
e4c20 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28  ontinue;.    if(
e4c30 20 21 53 5f 49 53 44 49 52 28 62 75 66 2e 73 74   !S_ISDIR(buf.st
e4c40 5f 6d 6f 64 65 29 20 29 20 63 6f 6e 74 69 6e 75  _mode) ) continu
e4c50 65 3b 0a 20 20 20 20 69 66 28 20 61 63 63 65 73  e;.    if( acces
e4c60 73 28 61 7a 44 69 72 73 5b 69 5d 2c 20 30 37 29  s(azDirs[i], 07)
e4c70 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
e4c80 20 7a 44 69 72 20 3d 20 61 7a 44 69 72 73 5b 69   zDir = azDirs[i
e4c90 5d 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  ];.    break;.  
e4ca0 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68  }..  /* Check th
e4cb0 61 74 20 74 68 65 20 6f 75 74 70 75 74 20 62 75  at the output bu
e4cc0 66 66 65 72 20 69 73 20 6c 61 72 67 65 20 65 6e  ffer is large en
e4cd0 6f 75 67 68 20 66 6f 72 20 74 68 65 20 74 65 6d  ough for the tem
e4ce0 70 6f 72 61 72 79 20 66 69 6c 65 20 0a 20 20 2a  porary file .  *
e4cf0 2a 20 6e 61 6d 65 2e 20 49 66 20 69 74 20 69 73  * name. If it is
e4d00 20 6e 6f 74 2c 20 72 65 74 75 72 6e 20 53 51 4c   not, return SQL
e4d10 49 54 45 5f 45 52 52 4f 52 2e 0a 20 20 2a 2f 0a  ITE_ERROR..  */.
e4d20 20 20 69 66 28 20 28 73 74 72 6c 65 6e 28 7a 44    if( (strlen(zD
e4d30 69 72 29 20 2b 20 73 74 72 6c 65 6e 28 53 51 4c  ir) + strlen(SQL
e4d40 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52  ITE_TEMP_FILE_PR
e4d50 45 46 49 58 29 20 2b 20 31 37 29 20 3e 3d 20 28  EFIX) + 17) >= (
e4d60 73 69 7a 65 5f 74 29 6e 42 75 66 20 29 7b 0a 20  size_t)nBuf ){. 
e4d70 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
e4d80 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 64  _ERROR;.  }..  d
e4d90 6f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  o{.    sqlite3_s
e4da0 6e 70 72 69 6e 74 66 28 6e 42 75 66 2d 31 37 2c  nprintf(nBuf-17,
e4db0 20 7a 42 75 66 2c 20 22 25 73 2f 22 53 51 4c 49   zBuf, "%s/"SQLI
e4dc0 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45  TE_TEMP_FILE_PRE
e4dd0 46 49 58 2c 20 7a 44 69 72 29 3b 0a 20 20 20 20  FIX, zDir);.    
e4de0 6a 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28  j = (int)strlen(
e4df0 7a 42 75 66 29 3b 0a 20 20 20 20 73 71 6c 69 74  zBuf);.    sqlit
e4e00 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 31 35  e3_randomness(15
e4e10 2c 20 26 7a 42 75 66 5b 6a 5d 29 3b 0a 20 20 20  , &zBuf[j]);.   
e4e20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31 35 3b 20   for(i=0; i<15; 
e4e30 69 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  i++, j++){.     
e4e40 20 7a 42 75 66 5b 6a 5d 20 3d 20 28 63 68 61 72   zBuf[j] = (char
e4e50 29 7a 43 68 61 72 73 5b 20 28 28 75 6e 73 69 67  )zChars[ ((unsig
e4e60 6e 65 64 20 63 68 61 72 29 7a 42 75 66 5b 6a 5d  ned char)zBuf[j]
e4e70 29 25 28 73 69 7a 65 6f 66 28 7a 43 68 61 72 73  )%(sizeof(zChars
e4e80 29 2d 31 29 20 5d 3b 0a 20 20 20 20 7d 0a 20 20  )-1) ];.    }.  
e4e90 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20    zBuf[j] = 0;. 
e4ea0 20 7d 77 68 69 6c 65 28 20 61 63 63 65 73 73 28   }while( access(
e4eb0 7a 42 75 66 2c 30 29 3d 3d 30 20 29 3b 0a 20 20  zBuf,0)==0 );.  
e4ec0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
e4ed0 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f  ;.}..#if SQLITE_
e4ee0 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
e4ef0 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28  TYLE && defined(
e4f00 5f 5f 41 50 50 4c 45 5f 5f 29 0a 2f 2a 0a 2a 2a  __APPLE__)./*.**
e4f10 20 52 6f 75 74 69 6e 65 20 74 6f 20 74 72 61 6e   Routine to tran
e4f20 73 66 6f 72 6d 20 61 20 75 6e 69 78 46 69 6c 65  sform a unixFile
e4f30 20 69 6e 74 6f 20 61 20 70 72 6f 78 79 2d 6c 6f   into a proxy-lo
e4f40 63 6b 69 6e 67 20 75 6e 69 78 46 69 6c 65 2e 0a  cking unixFile..
e4f50 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
e4f60 6e 20 69 6e 20 74 68 65 20 70 72 6f 78 79 2d 6c  n in the proxy-l
e4f70 6f 63 6b 20 64 69 76 69 73 69 6f 6e 2c 20 62 75  ock division, bu
e4f80 74 20 75 73 65 64 20 62 79 20 75 6e 69 78 4f 70  t used by unixOp
e4f90 65 6e 28 29 0a 2a 2a 20 69 66 20 53 51 4c 49 54  en().** if SQLIT
e4fa0 45 5f 50 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c  E_PREFER_PROXY_L
e4fb0 4f 43 4b 49 4e 47 20 69 73 20 64 65 66 69 6e 65  OCKING is define
e4fc0 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
e4fd0 20 70 72 6f 78 79 54 72 61 6e 73 66 6f 72 6d 55   proxyTransformU
e4fe0 6e 69 78 46 69 6c 65 28 75 6e 69 78 46 69 6c 65  nixFile(unixFile
e4ff0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  *, const char*);
e5000 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53  .#endif../*.** S
e5010 65 61 72 63 68 20 66 6f 72 20 61 6e 20 75 6e 75  earch for an unu
e5020 73 65 64 20 66 69 6c 65 20 64 65 73 63 72 69 70  sed file descrip
e5030 74 6f 72 20 74 68 61 74 20 77 61 73 20 6f 70 65  tor that was ope
e5040 6e 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ned on the datab
e5050 61 73 65 20 0a 2a 2a 20 66 69 6c 65 20 28 6e 6f  ase .** file (no
e5060 74 20 61 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 6d  t a journal or m
e5070 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 69  aster-journal fi
e5080 6c 65 29 20 69 64 65 6e 74 69 66 69 65 64 20 62  le) identified b
e5090 79 20 70 61 74 68 6e 61 6d 65 0a 2a 2a 20 7a 50  y pathname.** zP
e50a0 61 74 68 20 77 69 74 68 20 53 51 4c 49 54 45 5f  ath with SQLITE_
e50b0 4f 50 45 4e 5f 58 58 58 20 66 6c 61 67 73 20 6d  OPEN_XXX flags m
e50c0 61 74 63 68 69 6e 67 20 74 68 6f 73 65 20 70 61  atching those pa
e50d0 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f  ssed as the seco
e50e0 6e 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74  nd.** argument t
e50f0 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  o this function.
e5100 0a 2a 2a 0a 2a 2a 20 53 75 63 68 20 61 20 66 69  .**.** Such a fi
e5110 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 6d 61  le descriptor ma
e5120 79 20 65 78 69 73 74 20 69 66 20 61 20 64 61 74  y exist if a dat
e5130 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
e5140 20 77 61 73 20 63 6c 6f 73 65 64 0a 2a 2a 20 62   was closed.** b
e5150 75 74 20 74 68 65 20 61 73 73 6f 63 69 61 74 65  ut the associate
e5160 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  d file descripto
e5170 72 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63  r could not be c
e5180 6c 6f 73 65 64 20 62 65 63 61 75 73 65 20 73 6f  losed because so
e5190 6d 65 0a 2a 2a 20 6f 74 68 65 72 20 66 69 6c 65  me.** other file
e51a0 20 64 65 73 63 72 69 70 74 6f 72 20 6f 70 65 6e   descriptor open
e51b0 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c   on the same fil
e51c0 65 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 66  e is holding a f
e51d0 69 6c 65 2d 6c 6f 63 6b 2e 0a 2a 2a 20 52 65 66  ile-lock..** Ref
e51e0 65 72 20 74 6f 20 63 6f 6d 6d 65 6e 74 73 20 69  er to comments i
e51f0 6e 20 74 68 65 20 75 6e 69 78 43 6c 6f 73 65 28  n the unixClose(
e5200 29 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 74  ) function and t
e5210 68 65 20 6c 65 6e 67 74 68 79 20 63 6f 6d 6d 65  he lengthy comme
e5220 6e 74 0a 2a 2a 20 64 65 73 63 72 69 62 69 6e 67  nt.** describing
e5230 20 22 50 6f 73 69 78 20 41 64 76 69 73 6f 72 79   "Posix Advisory
e5240 20 4c 6f 63 6b 69 6e 67 22 20 61 74 20 74 68 65   Locking" at the
e5250 20 73 74 61 72 74 20 6f 66 20 74 68 69 73 20 66   start of this f
e5260 69 6c 65 20 66 6f 72 20 0a 2a 2a 20 66 75 72 74  ile for .** furt
e5270 68 65 72 20 64 65 74 61 69 6c 73 2e 20 41 6c 73  her details. Als
e5280 6f 2c 20 74 69 63 6b 65 74 20 23 34 30 31 38 2e  o, ticket #4018.
e5290 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 75 69 74  .**.** If a suit
e52a0 61 62 6c 65 20 66 69 6c 65 20 64 65 73 63 72 69  able file descri
e52b0 70 74 6f 72 20 69 73 20 66 6f 75 6e 64 2c 20 74  ptor is found, t
e52c0 68 65 6e 20 69 74 20 69 73 20 72 65 74 75 72 6e  hen it is return
e52d0 65 64 2e 20 49 66 20 6e 6f 0a 2a 2a 20 73 75 63  ed. If no.** suc
e52e0 68 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  h file descripto
e52f0 72 20 69 73 20 6c 6f 63 61 74 65 64 2c 20 2d 31  r is located, -1
e5300 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
e5310 0a 73 74 61 74 69 63 20 55 6e 69 78 55 6e 75 73  .static UnixUnus
e5320 65 64 46 64 20 2a 66 69 6e 64 52 65 75 73 61 62  edFd *findReusab
e5330 6c 65 46 64 28 63 6f 6e 73 74 20 63 68 61 72 20  leFd(const char 
e5340 2a 7a 50 61 74 68 2c 20 69 6e 74 20 66 6c 61 67  *zPath, int flag
e5350 73 29 7b 0a 20 20 55 6e 69 78 55 6e 75 73 65 64  s){.  UnixUnused
e5360 46 64 20 2a 70 55 6e 75 73 65 64 20 3d 20 30 3b  Fd *pUnused = 0;
e5370 0a 0a 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 73 65  ..  /* Do not se
e5380 61 72 63 68 20 66 6f 72 20 61 6e 20 75 6e 75 73  arch for an unus
e5390 65 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  ed file descript
e53a0 6f 72 20 6f 6e 20 76 78 77 6f 72 6b 73 2e 20 4e  or on vxworks. N
e53b0 6f 74 20 62 65 63 61 75 73 65 0a 20 20 2a 2a 20  ot because.  ** 
e53c0 76 78 77 6f 72 6b 73 20 77 6f 75 6c 64 20 6e 6f  vxworks would no
e53d0 74 20 62 65 6e 65 66 69 74 20 66 72 6f 6d 20 74  t benefit from t
e53e0 68 65 20 63 68 61 6e 67 65 20 28 69 74 20 6d 69  he change (it mi
e53f0 67 68 74 2c 20 77 65 27 72 65 20 6e 6f 74 20 73  ght, we're not s
e5400 75 72 65 29 2c 0a 20 20 2a 2a 20 62 75 74 20 62  ure),.  ** but b
e5410 65 63 61 75 73 65 20 6e 6f 20 77 61 79 20 74 6f  ecause no way to
e5420 20 74 65 73 74 20 69 74 20 69 73 20 63 75 72 72   test it is curr
e5430 65 6e 74 6c 79 20 61 76 61 69 6c 61 62 6c 65 2e  ently available.
e5440 20 49 74 20 69 73 20 62 65 74 74 65 72 20 0a 20   It is better . 
e5450 20 2a 2a 20 6e 6f 74 20 74 6f 20 72 69 73 6b 20   ** not to risk 
e5460 62 72 65 61 6b 69 6e 67 20 76 78 77 6f 72 6b 73  breaking vxworks
e5470 20 73 75 70 70 6f 72 74 20 66 6f 72 20 74 68 65   support for the
e5480 20 73 61 6b 65 20 6f 66 20 73 75 63 68 20 61 6e   sake of such an
e5490 20 6f 62 73 63 75 72 65 20 0a 20 20 2a 2a 20 66   obscure .  ** f
e54a0 65 61 74 75 72 65 2e 20 20 2a 2f 0a 23 69 66 20  eature.  */.#if 
e54b0 21 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74  !OS_VXWORKS.  st
e54c0 72 75 63 74 20 73 74 61 74 20 73 53 74 61 74 3b  ruct stat sStat;
e54d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e54e0 20 20 20 2f 2a 20 52 65 73 75 6c 74 73 20 6f 66     /* Results of
e54f0 20 73 74 61 74 28 29 20 63 61 6c 6c 20 2a 2f 0a   stat() call */.
e5500 0a 20 20 2f 2a 20 41 20 73 74 61 74 28 29 20 63  .  /* A stat() c
e5510 61 6c 6c 20 6d 61 79 20 66 61 69 6c 20 66 6f 72  all may fail for
e5520 20 76 61 72 69 6f 75 73 20 72 65 61 73 6f 6e 73   various reasons
e5530 2e 20 49 66 20 74 68 69 73 20 68 61 70 70 65 6e  . If this happen
e5540 73 2c 20 69 74 20 69 73 0a 20 20 2a 2a 20 61 6c  s, it is.  ** al
e5550 6d 6f 73 74 20 63 65 72 74 61 69 6e 20 74 68 61  most certain tha
e5560 74 20 61 6e 20 6f 70 65 6e 28 29 20 63 61 6c 6c  t an open() call
e5570 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 70 61 74   on the same pat
e5580 68 20 77 69 6c 6c 20 61 6c 73 6f 20 66 61 69 6c  h will also fail
e5590 2e 0a 20 20 2a 2a 20 46 6f 72 20 74 68 69 73 20  ..  ** For this 
e55a0 72 65 61 73 6f 6e 2c 20 69 66 20 61 6e 20 65 72  reason, if an er
e55b0 72 6f 72 20 6f 63 63 75 72 73 20 69 6e 20 74 68  ror occurs in th
e55c0 65 20 73 74 61 74 28 29 20 63 61 6c 6c 20 68 65  e stat() call he
e55d0 72 65 2c 20 69 74 20 69 73 0a 20 20 2a 2a 20 69  re, it is.  ** i
e55e0 67 6e 6f 72 65 64 20 61 6e 64 20 2d 31 20 69 73  gnored and -1 is
e55f0 20 72 65 74 75 72 6e 65 64 2e 20 54 68 65 20 63   returned. The c
e5600 61 6c 6c 65 72 20 77 69 6c 6c 20 74 72 79 20 74  aller will try t
e5610 6f 20 6f 70 65 6e 20 61 20 6e 65 77 20 66 69 6c  o open a new fil
e5620 65 0a 20 20 2a 2a 20 64 65 73 63 72 69 70 74 6f  e.  ** descripto
e5630 72 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 70 61  r on the same pa
e5640 74 68 2c 20 66 61 69 6c 2c 20 61 6e 64 20 72 65  th, fail, and re
e5650 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 74 6f  turn an error to
e5660 20 53 51 4c 69 74 65 2e 0a 20 20 2a 2a 0a 20 20   SQLite..  **.  
e5670 2a 2a 20 45 76 65 6e 20 69 66 20 61 20 73 75 62  ** Even if a sub
e5680 73 65 71 75 65 6e 74 20 6f 70 65 6e 28 29 20 63  sequent open() c
e5690 61 6c 6c 20 64 6f 65 73 20 73 75 63 63 65 65 64  all does succeed
e56a0 2c 20 74 68 65 20 63 6f 6e 73 65 71 75 65 6e 63  , the consequenc
e56b0 65 73 20 6f 66 0a 20 20 2a 2a 20 6e 6f 74 20 73  es of.  ** not s
e56c0 65 61 72 63 68 69 6e 67 20 66 6f 72 20 61 20 72  earching for a r
e56d0 65 73 75 73 61 62 6c 65 20 66 69 6c 65 20 64 65  esusable file de
e56e0 73 63 72 69 70 74 6f 72 20 61 72 65 20 6e 6f 74  scriptor are not
e56f0 20 64 69 72 65 2e 20 20 2a 2f 0a 20 20 69 66 28   dire.  */.  if(
e5700 20 30 3d 3d 73 74 61 74 28 7a 50 61 74 68 2c 20   0==stat(zPath, 
e5710 26 73 53 74 61 74 29 20 29 7b 0a 20 20 20 20 73  &sStat) ){.    s
e5720 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e  truct unixOpenCn
e5730 74 20 2a 70 4f 3b 0a 20 20 20 20 73 74 72 75 63  t *pO;.    struc
e5740 74 20 75 6e 69 78 46 69 6c 65 49 64 20 69 64 3b  t unixFileId id;
e5750 0a 20 20 20 20 69 64 2e 64 65 76 20 3d 20 73 53  .    id.dev = sS
e5760 74 61 74 2e 73 74 5f 64 65 76 3b 0a 20 20 20 20  tat.st_dev;.    
e5770 69 64 2e 69 6e 6f 20 3d 20 73 53 74 61 74 2e 73  id.ino = sStat.s
e5780 74 5f 69 6e 6f 3b 0a 0a 20 20 20 20 75 6e 69 78  t_ino;..    unix
e5790 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20  EnterMutex();.  
e57a0 20 20 66 6f 72 28 70 4f 3d 6f 70 65 6e 4c 69 73    for(pO=openLis
e57b0 74 3b 20 70 4f 20 26 26 20 6d 65 6d 63 6d 70 28  t; pO && memcmp(
e57c0 26 69 64 2c 20 26 70 4f 2d 3e 66 69 6c 65 49 64  &id, &pO->fileId
e57d0 2c 20 73 69 7a 65 6f 66 28 69 64 29 29 3b 20 70  , sizeof(id)); p
e57e0 4f 3d 70 4f 2d 3e 70 4e 65 78 74 29 3b 0a 20 20  O=pO->pNext);.  
e57f0 20 20 69 66 28 20 70 4f 20 29 7b 0a 20 20 20 20    if( pO ){.    
e5800 20 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 2a    UnixUnusedFd *
e5810 2a 70 70 3b 0a 20 20 20 20 20 20 66 6f 72 28 70  *pp;.      for(p
e5820 70 3d 26 70 4f 2d 3e 70 55 6e 75 73 65 64 3b 20  p=&pO->pUnused; 
e5830 2a 70 70 20 26 26 20 28 2a 70 70 29 2d 3e 66 6c  *pp && (*pp)->fl
e5840 61 67 73 21 3d 66 6c 61 67 73 3b 20 70 70 3d 26  ags!=flags; pp=&
e5850 28 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 29 3b  ((*pp)->pNext));
e5860 0a 20 20 20 20 20 20 70 55 6e 75 73 65 64 20 3d  .      pUnused =
e5870 20 2a 70 70 3b 0a 20 20 20 20 20 20 69 66 28 20   *pp;.      if( 
e5880 70 55 6e 75 73 65 64 20 29 7b 0a 20 20 20 20 20  pUnused ){.     
e5890 20 20 20 2a 70 70 20 3d 20 70 55 6e 75 73 65 64     *pp = pUnused
e58a0 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d  ->pNext;.      }
e58b0 0a 20 20 20 20 7d 0a 20 20 20 20 75 6e 69 78 4c  .    }.    unixL
e58c0 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d  eaveMutex();.  }
e58d0 0a 23 65 6e 64 69 66 20 20 20 20 2f 2a 20 69 66  .#endif    /* if
e58e0 20 21 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a   !OS_VXWORKS */.
e58f0 20 20 72 65 74 75 72 6e 20 70 55 6e 75 73 65 64    return pUnused
e5900 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20  ;.}../*.** Open 
e5910 74 68 65 20 66 69 6c 65 20 7a 50 61 74 68 2e 0a  the file zPath..
e5920 2a 2a 20 0a 2a 2a 20 50 72 65 76 69 6f 75 73 6c  ** .** Previousl
e5930 79 2c 20 74 68 65 20 53 51 4c 69 74 65 20 4f 53  y, the SQLite OS
e5940 20 6c 61 79 65 72 20 75 73 65 64 20 74 68 72 65   layer used thre
e5950 65 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 70  e functions in p
e5960 6c 61 63 65 20 6f 66 20 74 68 69 73 0a 2a 2a 20  lace of this.** 
e5970 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73  one:.**.**     s
e5980 71 6c 69 74 65 33 4f 73 4f 70 65 6e 52 65 61 64  qlite3OsOpenRead
e5990 57 72 69 74 65 28 29 3b 0a 2a 2a 20 20 20 20 20  Write();.**     
e59a0 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 52 65 61  sqlite3OsOpenRea
e59b0 64 4f 6e 6c 79 28 29 3b 0a 2a 2a 20 20 20 20 20  dOnly();.**     
e59c0 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 45 78 63  sqlite3OsOpenExc
e59d0 6c 75 73 69 76 65 28 29 3b 0a 2a 2a 0a 2a 2a 20  lusive();.**.** 
e59e0 54 68 65 73 65 20 63 61 6c 6c 73 20 63 6f 72 72  These calls corr
e59f0 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 20 66 6f  espond to the fo
e5a00 6c 6c 6f 77 69 6e 67 20 63 6f 6d 62 69 6e 61 74  llowing combinat
e5a10 69 6f 6e 73 20 6f 66 20 66 6c 61 67 73 3a 0a 2a  ions of flags:.*
e5a20 2a 0a 2a 2a 20 20 20 20 20 52 65 61 64 57 72 69  *.**     ReadWri
e5a30 74 65 28 29 20 2d 3e 20 20 20 20 20 28 52 45 41  te() ->     (REA
e5a40 44 57 52 49 54 45 20 7c 20 43 52 45 41 54 45 29  DWRITE | CREATE)
e5a50 0a 2a 2a 20 20 20 20 20 52 65 61 64 4f 6e 6c 79  .**     ReadOnly
e5a60 28 29 20 20 2d 3e 20 20 20 20 20 28 52 45 41 44  ()  ->     (READ
e5a70 4f 4e 4c 59 29 20 0a 2a 2a 20 20 20 20 20 4f 70  ONLY) .**     Op
e5a80 65 6e 45 78 63 6c 75 73 69 76 65 28 29 20 2d 3e  enExclusive() ->
e5a90 20 28 52 45 41 44 57 52 49 54 45 20 7c 20 43 52   (READWRITE | CR
e5aa0 45 41 54 45 20 7c 20 45 58 43 4c 55 53 49 56 45  EATE | EXCLUSIVE
e5ab0 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6c 64 20  ).**.** The old 
e5ac0 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28 29 20  OpenExclusive() 
e5ad0 61 63 63 65 70 74 65 64 20 61 20 62 6f 6f 6c 65  accepted a boole
e5ae0 61 6e 20 61 72 67 75 6d 65 6e 74 20 2d 20 22 64  an argument - "d
e5af0 65 6c 46 6c 61 67 22 2e 20 49 66 0a 2a 2a 20 74  elFlag". If.** t
e5b00 72 75 65 2c 20 74 68 65 20 66 69 6c 65 20 77 61  rue, the file wa
e5b10 73 20 63 6f 6e 66 69 67 75 72 65 64 20 74 6f 20  s configured to 
e5b20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  be automatically
e5b30 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68   deleted when th
e5b40 65 0a 2a 2a 20 66 69 6c 65 20 68 61 6e 64 6c 65  e.** file handle
e5b50 20 63 6c 6f 73 65 64 2e 20 54 6f 20 61 63 68 69   closed. To achi
e5b60 65 76 65 20 74 68 65 20 73 61 6d 65 20 65 66 66  eve the same eff
e5b70 65 63 74 20 75 73 69 6e 67 20 74 68 69 73 20 6e  ect using this n
e5b80 65 77 20 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  ew .** interface
e5b90 2c 20 61 64 64 20 74 68 65 20 44 45 4c 45 54 45  , add the DELETE
e5ba0 4f 4e 43 4c 4f 53 45 20 66 6c 61 67 20 74 6f 20  ONCLOSE flag to 
e5bb0 74 68 6f 73 65 20 73 70 65 63 69 66 69 65 64 20  those specified 
e5bc0 61 62 6f 76 65 20 66 6f 72 20 0a 2a 2a 20 4f 70  above for .** Op
e5bd0 65 6e 45 78 63 6c 75 73 69 76 65 28 29 2e 0a 2a  enExclusive()..*
e5be0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
e5bf0 78 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33  xOpen(.  sqlite3
e5c00 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20  _vfs *pVfs,     
e5c10 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53        /* The VFS
e5c20 20 66 6f 72 20 77 68 69 63 68 20 74 68 69 73 20   for which this 
e5c30 69 73 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 74  is the xOpen met
e5c40 68 6f 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  hod */.  const c
e5c50 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20  har *zPath,     
e5c60 20 20 20 20 20 20 2f 2a 20 50 61 74 68 6e 61 6d        /* Pathnam
e5c70 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 62 65 20  e of file to be 
e5c80 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 73 71 6c 69  opened */.  sqli
e5c90 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c  te3_file *pFile,
e5ca0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
e5cb0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
e5cc0 74 6f 20 62 65 20 66 69 6c 6c 65 64 20 69 6e 20  to be filled in 
e5cd0 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20  */.  int flags, 
e5ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e5cf0 20 20 2f 2a 20 49 6e 70 75 74 20 66 6c 61 67 73    /* Input flags
e5d00 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20   to control the 
e5d10 6f 70 65 6e 69 6e 67 20 2a 2f 0a 20 20 69 6e 74  opening */.  int
e5d20 20 2a 70 4f 75 74 46 6c 61 67 73 20 20 20 20 20   *pOutFlags     
e5d30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74            /* Out
e5d40 70 75 74 20 66 6c 61 67 73 20 72 65 74 75 72 6e  put flags return
e5d50 65 64 20 74 6f 20 53 51 4c 69 74 65 20 63 6f 72  ed to SQLite cor
e5d60 65 20 2a 2f 0a 29 7b 0a 20 20 75 6e 69 78 46 69  e */.){.  unixFi
e5d70 6c 65 20 2a 70 20 3d 20 28 75 6e 69 78 46 69 6c  le *p = (unixFil
e5d80 65 20 2a 29 70 46 69 6c 65 3b 0a 20 20 69 6e 74  e *)pFile;.  int
e5d90 20 66 64 20 3d 20 2d 31 3b 20 20 20 20 20 20 20   fd = -1;       
e5da0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
e5db0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 72  ile descriptor r
e5dc0 65 74 75 72 6e 65 64 20 62 79 20 6f 70 65 6e 28  eturned by open(
e5dd0 29 20 2a 2f 0a 20 20 69 6e 74 20 64 69 72 66 64  ) */.  int dirfd
e5de0 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 20 20   = -1;          
e5df0 20 20 20 20 20 20 2f 2a 20 44 69 72 65 63 74 6f        /* Directo
e5e00 72 79 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  ry file descript
e5e10 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 65 6e  or */.  int open
e5e20 46 6c 61 67 73 20 3d 20 30 3b 20 20 20 20 20 20  Flags = 0;      
e5e30 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20         /* Flags 
e5e40 74 6f 20 70 61 73 73 20 74 6f 20 6f 70 65 6e 28  to pass to open(
e5e50 29 20 2a 2f 0a 20 20 69 6e 74 20 65 54 79 70 65  ) */.  int eType
e5e60 20 3d 20 66 6c 61 67 73 26 30 78 46 46 46 46 46   = flags&0xFFFFF
e5e70 46 30 30 3b 20 20 2f 2a 20 54 79 70 65 20 6f 66  F00;  /* Type of
e5e80 20 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f   file to open */
e5e90 0a 20 20 69 6e 74 20 6e 6f 4c 6f 63 6b 3b 20 20  .  int noLock;  
e5ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e5eb0 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 6f 6d 69    /* True to omi
e5ec0 74 20 6c 6f 63 6b 69 6e 67 20 70 72 69 6d 69 74  t locking primit
e5ed0 69 76 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 63  ives */.  int rc
e5ee0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20   = SQLITE_OK;   
e5ef0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 75 6e 63           /* Func
e5f00 74 69 6f 6e 20 52 65 74 75 72 6e 20 43 6f 64 65  tion Return Code
e5f10 20 2a 2f 0a 0a 20 20 69 6e 74 20 69 73 45 78 63   */..  int isExc
e5f20 6c 75 73 69 76 65 20 20 3d 20 28 66 6c 61 67 73  lusive  = (flags
e5f30 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45   & SQLITE_OPEN_E
e5f40 58 43 4c 55 53 49 56 45 29 3b 0a 20 20 69 6e 74  XCLUSIVE);.  int
e5f50 20 69 73 44 65 6c 65 74 65 20 20 20 20 20 3d 20   isDelete     = 
e5f60 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  (flags & SQLITE_
e5f70 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f  OPEN_DELETEONCLO
e5f80 53 45 29 3b 0a 20 20 69 6e 74 20 69 73 43 72 65  SE);.  int isCre
e5f90 61 74 65 20 20 20 20 20 3d 20 28 66 6c 61 67 73  ate     = (flags
e5fa0 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43   & SQLITE_OPEN_C
e5fb0 52 45 41 54 45 29 3b 0a 20 20 69 6e 74 20 69 73  REATE);.  int is
e5fc0 52 65 61 64 6f 6e 6c 79 20 20 20 3d 20 28 66 6c  Readonly   = (fl
e5fd0 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45  ags & SQLITE_OPE
e5fe0 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a 20 20 69  N_READONLY);.  i
e5ff0 6e 74 20 69 73 52 65 61 64 57 72 69 74 65 20 20  nt isReadWrite  
e6000 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54  = (flags & SQLIT
e6010 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
e6020 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 63 72 65 61  );..  /* If crea
e6030 74 69 6e 67 20 61 20 6d 61 73 74 65 72 20 6f 72  ting a master or
e6040 20 6d 61 69 6e 2d 66 69 6c 65 20 6a 6f 75 72 6e   main-file journ
e6050 61 6c 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  al, this functio
e6060 6e 20 77 69 6c 6c 20 6f 70 65 6e 0a 20 20 2a 2a  n will open.  **
e6070 20 61 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74   a file-descript
e6080 6f 72 20 6f 6e 20 74 68 65 20 64 69 72 65 63 74  or on the direct
e6090 6f 72 79 20 74 6f 6f 2e 20 54 68 65 20 66 69 72  ory too. The fir
e60a0 73 74 20 74 69 6d 65 20 75 6e 69 78 53 79 6e 63  st time unixSync
e60b0 28 29 0a 20 20 2a 2a 20 69 73 20 63 61 6c 6c 65  ().  ** is calle
e60c0 64 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20  d the directory 
e60d0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
e60e0 77 69 6c 6c 20 62 65 20 66 73 79 6e 63 28 29 65  will be fsync()e
e60f0 64 20 61 6e 64 20 63 6c 6f 73 65 28 29 64 2e 0a  d and close()d..
e6100 20 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4f 70 65    */.  int isOpe
e6110 6e 44 69 72 65 63 74 6f 72 79 20 3d 20 28 69 73  nDirectory = (is
e6120 43 72 65 61 74 65 20 26 26 20 0a 20 20 20 20 20  Create && .     
e6130 20 28 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f   (eType==SQLITE_
e6140 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52  OPEN_MASTER_JOUR
e6150 4e 41 4c 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51  NAL || eType==SQ
e6160 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a  LITE_OPEN_MAIN_J
e6170 4f 55 52 4e 41 4c 29 0a 20 20 29 3b 0a 0a 20 20  OURNAL).  );..  
e6180 2f 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 20 7a  /* If argument z
e6190 50 61 74 68 20 69 73 20 61 20 4e 55 4c 4c 20 70  Path is a NULL p
e61a0 6f 69 6e 74 65 72 2c 20 74 68 69 73 20 66 75 6e  ointer, this fun
e61b0 63 74 69 6f 6e 20 69 73 20 72 65 71 75 69 72 65  ction is require
e61c0 64 20 74 6f 20 6f 70 65 6e 0a 20 20 2a 2a 20 61  d to open.  ** a
e61d0 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e   temporary file.
e61e0 20 55 73 65 20 74 68 69 73 20 62 75 66 66 65 72   Use this buffer
e61f0 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 66 69   to store the fi
e6200 6c 65 20 6e 61 6d 65 20 69 6e 2e 0a 20 20 2a 2f  le name in..  */
e6210 0a 20 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d 65  .  char zTmpname
e6220 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 5d  [MAX_PATHNAME+1]
e6230 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
e6240 7a 4e 61 6d 65 20 3d 20 7a 50 61 74 68 3b 0a 0a  zName = zPath;..
e6250 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 65 20 66    /* Check the f
e6260 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65  ollowing stateme
e6270 6e 74 73 20 61 72 65 20 74 72 75 65 3a 20 0a 20  nts are true: . 
e6280 20 2a 2a 0a 20 20 2a 2a 20 20 20 28 61 29 20 45   **.  **   (a) E
e6290 78 61 63 74 6c 79 20 6f 6e 65 20 6f 66 20 74 68  xactly one of th
e62a0 65 20 52 45 41 44 57 52 49 54 45 20 61 6e 64 20  e READWRITE and 
e62b0 52 45 41 44 4f 4e 4c 59 20 66 6c 61 67 73 20 6d  READONLY flags m
e62c0 75 73 74 20 62 65 20 73 65 74 2c 20 61 6e 64 20  ust be set, and 
e62d0 0a 20 20 2a 2a 20 20 20 28 62 29 20 69 66 20 43  .  **   (b) if C
e62e0 52 45 41 54 45 20 69 73 20 73 65 74 2c 20 74 68  REATE is set, th
e62f0 65 6e 20 52 45 41 44 57 52 49 54 45 20 6d 75 73  en READWRITE mus
e6300 74 20 61 6c 73 6f 20 62 65 20 73 65 74 2c 20 61  t also be set, a
e6310 6e 64 0a 20 20 2a 2a 20 20 20 28 63 29 20 69 66  nd.  **   (c) if
e6320 20 45 58 43 4c 55 53 49 56 45 20 69 73 20 73 65   EXCLUSIVE is se
e6330 74 2c 20 74 68 65 6e 20 43 52 45 41 54 45 20 6d  t, then CREATE m
e6340 75 73 74 20 61 6c 73 6f 20 62 65 20 73 65 74 2e  ust also be set.
e6350 0a 20 20 2a 2a 20 20 20 28 64 29 20 69 66 20 44  .  **   (d) if D
e6360 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 69 73 20  ELETEONCLOSE is 
e6370 73 65 74 2c 20 74 68 65 6e 20 43 52 45 41 54 45  set, then CREATE
e6380 20 6d 75 73 74 20 61 6c 73 6f 20 62 65 20 73 65   must also be se
e6390 74 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  t..  */.  assert
e63a0 28 28 69 73 52 65 61 64 6f 6e 6c 79 3d 3d 30 20  ((isReadonly==0 
e63b0 7c 7c 20 69 73 52 65 61 64 57 72 69 74 65 3d 3d  || isReadWrite==
e63c0 30 29 20 26 26 20 28 69 73 52 65 61 64 57 72 69  0) && (isReadWri
e63d0 74 65 20 7c 7c 20 69 73 52 65 61 64 6f 6e 6c 79  te || isReadonly
e63e0 29 29 3b 0a 20 20 61 73 73 65 72 74 28 69 73 43  ));.  assert(isC
e63f0 72 65 61 74 65 3d 3d 30 20 7c 7c 20 69 73 52 65  reate==0 || isRe
e6400 61 64 57 72 69 74 65 29 3b 0a 20 20 61 73 73 65  adWrite);.  asse
e6410 72 74 28 69 73 45 78 63 6c 75 73 69 76 65 3d 3d  rt(isExclusive==
e6420 30 20 7c 7c 20 69 73 43 72 65 61 74 65 29 3b 0a  0 || isCreate);.
e6430 20 20 61 73 73 65 72 74 28 69 73 44 65 6c 65 74    assert(isDelet
e6440 65 3d 3d 30 20 7c 7c 20 69 73 43 72 65 61 74 65  e==0 || isCreate
e6450 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 61 69  );..  /* The mai
e6460 6e 20 44 42 2c 20 6d 61 69 6e 20 6a 6f 75 72 6e  n DB, main journ
e6470 61 6c 2c 20 61 6e 64 20 6d 61 73 74 65 72 20 6a  al, and master j
e6480 6f 75 72 6e 61 6c 20 61 72 65 20 6e 65 76 65 72  ournal are never
e6490 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 20   automatically. 
e64a0 20 2a 2a 20 64 65 6c 65 74 65 64 2e 20 4e 6f 72   ** deleted. Nor
e64b0 20 61 72 65 20 74 68 65 79 20 65 76 65 72 20 74   are they ever t
e64c0 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2e 20  emporary files. 
e64d0 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 21   */.  assert( (!
e64e0 69 73 44 65 6c 65 74 65 20 26 26 20 7a 4e 61 6d  isDelete && zNam
e64f0 65 29 20 7c 7c 20 65 54 79 70 65 21 3d 53 51 4c  e) || eType!=SQL
e6500 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42  ITE_OPEN_MAIN_DB
e6510 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 21   );.  assert( (!
e6520 69 73 44 65 6c 65 74 65 20 26 26 20 7a 4e 61 6d  isDelete && zNam
e6530 65 29 20 7c 7c 20 65 54 79 70 65 21 3d 53 51 4c  e) || eType!=SQL
e6540 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f  ITE_OPEN_MAIN_JO
e6550 55 52 4e 41 4c 20 29 3b 0a 20 20 61 73 73 65 72  URNAL );.  asser
e6560 74 28 20 28 21 69 73 44 65 6c 65 74 65 20 26 26  t( (!isDelete &&
e6570 20 7a 4e 61 6d 65 29 20 7c 7c 20 65 54 79 70 65   zName) || eType
e6580 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41  !=SQLITE_OPEN_MA
e6590 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 29 3b 0a  STER_JOURNAL );.
e65a0 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61  .  /* Assert tha
e65b0 74 20 74 68 65 20 75 70 70 65 72 20 6c 61 79 65  t the upper laye
e65c0 72 20 68 61 73 20 73 65 74 20 6f 6e 65 20 6f 66  r has set one of
e65d0 20 74 68 65 20 22 66 69 6c 65 2d 74 79 70 65 22   the "file-type"
e65e0 20 66 6c 61 67 73 2e 20 2a 2f 0a 20 20 61 73 73   flags. */.  ass
e65f0 65 72 74 28 20 65 54 79 70 65 3d 3d 53 51 4c 49  ert( eType==SQLI
e6600 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20  TE_OPEN_MAIN_DB 
e6610 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53       || eType==S
e6620 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f  QLITE_OPEN_TEMP_
e6630 44 42 20 0a 20 20 20 20 20 20 20 7c 7c 20 65 54  DB .       || eT
e6640 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e  ype==SQLITE_OPEN
e6650 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 7c 7c  _MAIN_JOURNAL ||
e6660 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f   eType==SQLITE_O
e6670 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c  PEN_TEMP_JOURNAL
e6680 20 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70   .       || eTyp
e6690 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53  e==SQLITE_OPEN_S
e66a0 55 42 4a 4f 55 52 4e 41 4c 20 20 20 7c 7c 20 65  UBJOURNAL   || e
e66b0 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45  Type==SQLITE_OPE
e66c0 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c  N_MASTER_JOURNAL
e66d0 20 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70   .       || eTyp
e66e0 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54  e==SQLITE_OPEN_T
e66f0 52 41 4e 53 49 45 4e 54 5f 44 42 0a 20 20 29 3b  RANSIENT_DB.  );
e6700 0a 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c  ..  memset(p, 0,
e6710 20 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65   sizeof(unixFile
e6720 29 29 3b 0a 0a 20 20 69 66 28 20 65 54 79 70 65  ));..  if( eType
e6730 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41  ==SQLITE_OPEN_MA
e6740 49 4e 5f 44 42 20 29 7b 0a 20 20 20 20 55 6e 69  IN_DB ){.    Uni
e6750 78 55 6e 75 73 65 64 46 64 20 2a 70 55 6e 75 73  xUnusedFd *pUnus
e6760 65 64 3b 0a 20 20 20 20 70 55 6e 75 73 65 64 20  ed;.    pUnused 
e6770 3d 20 66 69 6e 64 52 65 75 73 61 62 6c 65 46 64  = findReusableFd
e6780 28 7a 4e 61 6d 65 2c 20 66 6c 61 67 73 29 3b 0a  (zName, flags);.
e6790 20 20 20 20 69 66 28 20 70 55 6e 75 73 65 64 20      if( pUnused 
e67a0 29 7b 0a 20 20 20 20 20 20 66 64 20 3d 20 70 55  ){.      fd = pU
e67b0 6e 75 73 65 64 2d 3e 66 64 3b 0a 20 20 20 20 7d  nused->fd;.    }
e67c0 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 55 6e 75  else{.      pUnu
e67d0 73 65 64 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  sed = sqlite3_ma
e67e0 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 70 55 6e  lloc(sizeof(*pUn
e67f0 75 73 65 64 29 29 3b 0a 20 20 20 20 20 20 69 66  used));.      if
e6800 28 20 21 70 55 6e 75 73 65 64 20 29 7b 0a 20 20  ( !pUnused ){.  
e6810 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
e6820 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
e6830 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e   }.    }.    p->
e6840 70 55 6e 75 73 65 64 20 3d 20 70 55 6e 75 73 65  pUnused = pUnuse
e6850 64 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 21  d;.  }else if( !
e6860 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 2f 2a 20  zName ){.    /* 
e6870 49 66 20 7a 4e 61 6d 65 20 69 73 20 4e 55 4c 4c  If zName is NULL
e6880 2c 20 74 68 65 20 75 70 70 65 72 20 6c 61 79 65  , the upper laye
e6890 72 20 69 73 20 72 65 71 75 65 73 74 69 6e 67 20  r is requesting 
e68a0 61 20 74 65 6d 70 20 66 69 6c 65 2e 20 2a 2f 0a  a temp file. */.
e68b0 20 20 20 20 61 73 73 65 72 74 28 69 73 44 65 6c      assert(isDel
e68c0 65 74 65 20 26 26 20 21 69 73 4f 70 65 6e 44 69  ete && !isOpenDi
e68d0 72 65 63 74 6f 72 79 29 3b 0a 20 20 20 20 72 63  rectory);.    rc
e68e0 20 3d 20 67 65 74 54 65 6d 70 6e 61 6d 65 28 4d   = getTempname(M
e68f0 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 2c 20 7a  AX_PATHNAME+1, z
e6900 54 6d 70 6e 61 6d 65 29 3b 0a 20 20 20 20 69 66  Tmpname);.    if
e6910 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
e6920 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
e6930 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 4e  rc;.    }.    zN
e6940 61 6d 65 20 3d 20 7a 54 6d 70 6e 61 6d 65 3b 0a  ame = zTmpname;.
e6950 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 74 65 72 6d    }..  /* Determ
e6960 69 6e 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66  ine the value of
e6970 20 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d   the flags param
e6980 65 74 65 72 20 70 61 73 73 65 64 20 74 6f 20 50  eter passed to P
e6990 4f 53 49 58 20 66 75 6e 63 74 69 6f 6e 0a 20 20  OSIX function.  
e69a0 2a 2a 20 6f 70 65 6e 28 29 2e 20 54 68 65 73 65  ** open(). These
e69b0 20 6d 75 73 74 20 62 65 20 63 61 6c 63 75 6c 61   must be calcula
e69c0 74 65 64 20 65 76 65 6e 20 69 66 20 6f 70 65 6e  ted even if open
e69d0 28 29 20 69 73 20 6e 6f 74 20 63 61 6c 6c 65 64  () is not called
e69e0 2c 20 61 73 0a 20 20 2a 2a 20 74 68 65 79 20 6d  , as.  ** they m
e69f0 61 79 20 62 65 20 73 74 6f 72 65 64 20 61 73 20  ay be stored as 
e6a00 70 61 72 74 20 6f 66 20 74 68 65 20 66 69 6c 65  part of the file
e6a10 20 68 61 6e 64 6c 65 20 61 6e 64 20 75 73 65 64   handle and used
e6a20 20 62 79 20 74 68 65 20 0a 20 20 2a 2a 20 27 63   by the .  ** 'c
e6a30 6f 6e 63 68 20 66 69 6c 65 27 20 6c 6f 63 6b 69  onch file' locki
e6a40 6e 67 20 66 75 6e 63 74 69 6f 6e 73 20 6c 61 74  ng functions lat
e6a50 65 72 20 6f 6e 2e 20 20 2a 2f 0a 20 20 69 66 28  er on.  */.  if(
e6a60 20 69 73 52 65 61 64 6f 6e 6c 79 20 29 20 20 6f   isReadonly )  o
e6a70 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 5f 52 44  penFlags |= O_RD
e6a80 4f 4e 4c 59 3b 0a 20 20 69 66 28 20 69 73 52 65  ONLY;.  if( isRe
e6a90 61 64 57 72 69 74 65 20 29 20 6f 70 65 6e 46 6c  adWrite ) openFl
e6aa0 61 67 73 20 7c 3d 20 4f 5f 52 44 57 52 3b 0a 20  ags |= O_RDWR;. 
e6ab0 20 69 66 28 20 69 73 43 72 65 61 74 65 20 29 20   if( isCreate ) 
e6ac0 20 20 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20     openFlags |= 
e6ad0 4f 5f 43 52 45 41 54 3b 0a 20 20 69 66 28 20 69  O_CREAT;.  if( i
e6ae0 73 45 78 63 6c 75 73 69 76 65 20 29 20 6f 70 65  sExclusive ) ope
e6af0 6e 46 6c 61 67 73 20 7c 3d 20 28 4f 5f 45 58 43  nFlags |= (O_EXC
e6b00 4c 7c 4f 5f 4e 4f 46 4f 4c 4c 4f 57 29 3b 0a 20  L|O_NOFOLLOW);. 
e6b10 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 28 4f   openFlags |= (O
e6b20 5f 4c 41 52 47 45 46 49 4c 45 7c 4f 5f 42 49 4e  _LARGEFILE|O_BIN
e6b30 41 52 59 29 3b 0a 0a 20 20 69 66 28 20 66 64 3c  ARY);..  if( fd<
e6b40 30 20 29 7b 0a 20 20 20 20 6d 6f 64 65 5f 74 20  0 ){.    mode_t 
e6b50 6f 70 65 6e 4d 6f 64 65 20 3d 20 28 69 73 44 65  openMode = (isDe
e6b60 6c 65 74 65 3f 30 36 30 30 3a 53 51 4c 49 54 45  lete?0600:SQLITE
e6b70 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45  _DEFAULT_FILE_PE
e6b80 52 4d 49 53 53 49 4f 4e 53 29 3b 0a 20 20 20 20  RMISSIONS);.    
e6b90 66 64 20 3d 20 6f 70 65 6e 28 7a 4e 61 6d 65 2c  fd = open(zName,
e6ba0 20 6f 70 65 6e 46 6c 61 67 73 2c 20 6f 70 65 6e   openFlags, open
e6bb0 4d 6f 64 65 29 3b 0a 20 20 20 20 4f 53 54 52 41  Mode);.    OSTRA
e6bc0 43 45 34 28 22 4f 50 45 4e 58 20 20 20 25 2d 33  CE4("OPENX   %-3
e6bd0 64 20 25 73 20 30 25 6f 5c 6e 22 2c 20 66 64 2c  d %s 0%o\n", fd,
e6be0 20 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 6c 61 67   zName, openFlag
e6bf0 73 29 3b 0a 20 20 20 20 69 66 28 20 66 64 3c 30  s);.    if( fd<0
e6c00 20 26 26 20 65 72 72 6e 6f 21 3d 45 49 53 44 49   && errno!=EISDI
e6c10 52 20 26 26 20 69 73 52 65 61 64 57 72 69 74 65  R && isReadWrite
e6c20 20 26 26 20 21 69 73 45 78 63 6c 75 73 69 76 65   && !isExclusive
e6c30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 46 61 69   ){.      /* Fai
e6c40 6c 65 64 20 74 6f 20 6f 70 65 6e 20 74 68 65 20  led to open the 
e6c50 66 69 6c 65 20 66 6f 72 20 72 65 61 64 2f 77 72  file for read/wr
e6c60 69 74 65 20 61 63 63 65 73 73 2e 20 54 72 79 20  ite access. Try 
e6c70 72 65 61 64 2d 6f 6e 6c 79 2e 20 2a 2f 0a 20 20  read-only. */.  
e6c80 20 20 20 20 66 6c 61 67 73 20 26 3d 20 7e 28 53      flags &= ~(S
e6c90 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
e6ca0 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e  RITE|SQLITE_OPEN
e6cb0 5f 43 52 45 41 54 45 29 3b 0a 20 20 20 20 20 20  _CREATE);.      
e6cc0 6f 70 65 6e 46 6c 61 67 73 20 26 3d 20 7e 28 4f  openFlags &= ~(O
e6cd0 5f 52 44 57 52 7c 4f 5f 43 52 45 41 54 29 3b 0a  _RDWR|O_CREAT);.
e6ce0 20 20 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 53        flags |= S
e6cf0 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f  QLITE_OPEN_READO
e6d00 4e 4c 59 3b 0a 20 20 20 20 20 20 6f 70 65 6e 46  NLY;.      openF
e6d10 6c 61 67 73 20 7c 3d 20 4f 5f 52 44 4f 4e 4c 59  lags |= O_RDONLY
e6d20 3b 0a 20 20 20 20 20 20 66 64 20 3d 20 6f 70 65  ;.      fd = ope
e6d30 6e 28 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 6c 61  n(zName, openFla
e6d40 67 73 2c 20 6f 70 65 6e 4d 6f 64 65 29 3b 0a 20  gs, openMode);. 
e6d50 20 20 20 7d 0a 20 20 20 20 69 66 28 20 66 64 3c     }.    if( fd<
e6d60 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  0 ){.      rc = 
e6d70 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b  SQLITE_CANTOPEN;
e6d80 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e  .      goto open
e6d90 5f 66 69 6e 69 73 68 65 64 3b 0a 20 20 20 20 7d  _finished;.    }
e6da0 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 66  .  }.  assert( f
e6db0 64 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4f  d>=0 );.  if( pO
e6dc0 75 74 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 2a  utFlags ){.    *
e6dd0 70 4f 75 74 46 6c 61 67 73 20 3d 20 66 6c 61 67  pOutFlags = flag
e6de0 73 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 2d  s;.  }..  if( p-
e6df0 3e 70 55 6e 75 73 65 64 20 29 7b 0a 20 20 20 20  >pUnused ){.    
e6e00 70 2d 3e 70 55 6e 75 73 65 64 2d 3e 66 64 20 3d  p->pUnused->fd =
e6e10 20 66 64 3b 0a 20 20 20 20 70 2d 3e 70 55 6e 75   fd;.    p->pUnu
e6e20 73 65 64 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61  sed->flags = fla
e6e30 67 73 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69  gs;.  }..  if( i
e6e40 73 44 65 6c 65 74 65 20 29 7b 0a 23 69 66 20 4f  sDelete ){.#if O
e6e50 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 7a 50  S_VXWORKS.    zP
e6e60 61 74 68 20 3d 20 7a 4e 61 6d 65 3b 0a 23 65 6c  ath = zName;.#el
e6e70 73 65 0a 20 20 20 20 75 6e 6c 69 6e 6b 28 7a 4e  se.    unlink(zN
e6e80 61 6d 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d  ame);.#endif.  }
e6e90 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  .#if SQLITE_ENAB
e6ea0 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
e6eb0 0a 20 20 65 6c 73 65 7b 0a 20 20 20 20 70 2d 3e  .  else{.    p->
e6ec0 6f 70 65 6e 46 6c 61 67 73 20 3d 20 6f 70 65 6e  openFlags = open
e6ed0 46 6c 61 67 73 3b 0a 20 20 7d 0a 23 65 6e 64 69  Flags;.  }.#endi
e6ee0 66 0a 0a 20 20 69 66 28 20 69 73 4f 70 65 6e 44  f..  if( isOpenD
e6ef0 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20  irectory ){.    
e6f00 72 63 20 3d 20 6f 70 65 6e 44 69 72 65 63 74 6f  rc = openDirecto
e6f10 72 79 28 7a 50 61 74 68 2c 20 26 64 69 72 66 64  ry(zPath, &dirfd
e6f20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
e6f30 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
e6f40 20 20 2f 2a 20 49 74 20 69 73 20 73 61 66 65 20    /* It is safe 
e6f50 74 6f 20 63 6c 6f 73 65 20 66 64 20 61 74 20 74  to close fd at t
e6f60 68 69 73 20 70 6f 69 6e 74 2c 20 62 65 63 61 75  his point, becau
e6f70 73 65 20 69 74 20 69 73 20 67 75 61 72 61 6e 74  se it is guarant
e6f80 65 65 64 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a  eed not.      **
e6f90 20 74 6f 20 62 65 20 6f 70 65 6e 20 6f 6e 20 61   to be open on a
e6fa0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
e6fb0 49 66 20 69 74 20 77 65 72 65 20 6f 70 65 6e 20  If it were open 
e6fc0 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 69  on a database fi
e6fd0 6c 65 2c 0a 20 20 20 20 20 20 2a 2a 20 69 74 20  le,.      ** it 
e6fe0 77 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 61 66  would not be saf
e6ff0 65 20 74 6f 20 63 6c 6f 73 65 20 61 73 20 74 68  e to close as th
e7000 69 73 20 77 6f 75 6c 64 20 72 65 6c 65 61 73 65  is would release
e7010 20 61 6e 79 20 6c 6f 63 6b 73 20 68 65 6c 64 0a   any locks held.
e7020 20 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20        ** on the 
e7030 66 69 6c 65 20 62 79 20 74 68 69 73 20 70 72 6f  file by this pro
e7040 63 65 73 73 2e 20 20 2a 2f 0a 20 20 20 20 20 20  cess.  */.      
e7050 61 73 73 65 72 74 28 20 65 54 79 70 65 21 3d 53  assert( eType!=S
e7060 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
e7070 44 42 20 29 3b 0a 20 20 20 20 20 20 63 6c 6f 73  DB );.      clos
e7080 65 28 66 64 29 3b 20 20 20 20 20 20 20 20 20 20  e(fd);          
e7090 20 20 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c     /* silently l
e70a0 65 61 6b 20 69 66 20 66 61 69 6c 2c 20 61 6c 72  eak if fail, alr
e70b0 65 61 64 79 20 69 6e 20 65 72 72 6f 72 20 2a 2f  eady in error */
e70c0 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e  .      goto open
e70d0 5f 66 69 6e 69 73 68 65 64 3b 0a 20 20 20 20 7d  _finished;.    }
e70e0 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20 46 44 5f  .  }..#ifdef FD_
e70f0 43 4c 4f 45 58 45 43 0a 20 20 66 63 6e 74 6c 28  CLOEXEC.  fcntl(
e7100 66 64 2c 20 46 5f 53 45 54 46 44 2c 20 66 63 6e  fd, F_SETFD, fcn
e7110 74 6c 28 66 64 2c 20 46 5f 47 45 54 46 44 2c 20  tl(fd, F_GETFD, 
e7120 30 29 20 7c 20 46 44 5f 43 4c 4f 45 58 45 43 29  0) | FD_CLOEXEC)
e7130 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 6e 6f 4c 6f  ;.#endif..  noLo
e7140 63 6b 20 3d 20 65 54 79 70 65 21 3d 53 51 4c 49  ck = eType!=SQLI
e7150 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 3b  TE_OPEN_MAIN_DB;
e7160 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 50 52 45  ..#if SQLITE_PRE
e7170 46 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e  FER_PROXY_LOCKIN
e7180 47 0a 20 20 69 66 28 20 7a 50 61 74 68 21 3d 4e  G.  if( zPath!=N
e7190 55 4c 4c 20 26 26 20 21 6e 6f 4c 6f 63 6b 20 26  ULL && !noLock &
e71a0 26 20 70 56 66 73 2d 3e 78 4f 70 65 6e 20 29 7b  & pVfs->xOpen ){
e71b0 0a 20 20 20 20 63 68 61 72 20 2a 65 6e 76 66 6f  .    char *envfo
e71c0 72 63 65 20 3d 20 67 65 74 65 6e 76 28 22 53 51  rce = getenv("SQ
e71d0 4c 49 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59  LITE_FORCE_PROXY
e71e0 5f 4c 4f 43 4b 49 4e 47 22 29 3b 0a 20 20 20 20  _LOCKING");.    
e71f0 69 6e 74 20 75 73 65 50 72 6f 78 79 20 3d 20 30  int useProxy = 0
e7200 3b 0a 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45  ;..    /* SQLITE
e7210 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43  _FORCE_PROXY_LOC
e7220 4b 49 4e 47 3d 3d 31 20 6d 65 61 6e 73 20 66 6f  KING==1 means fo
e7230 72 63 65 20 61 6c 77 61 79 73 20 75 73 65 20 70  rce always use p
e7240 72 6f 78 79 2c 20 30 20 6d 65 61 6e 73 20 0a 20  roxy, 0 means . 
e7250 20 20 20 2a 2a 20 6e 65 76 65 72 20 75 73 65 20     ** never use 
e7260 70 72 6f 78 79 2c 20 4e 55 4c 4c 20 6d 65 61 6e  proxy, NULL mean
e7270 73 20 75 73 65 20 70 72 6f 78 79 20 66 6f 72 20  s use proxy for 
e7280 6e 6f 6e 2d 6c 6f 63 61 6c 20 66 69 6c 65 73 20  non-local files 
e7290 6f 6e 6c 79 2e 20 20 2a 2f 0a 20 20 20 20 69 66  only.  */.    if
e72a0 28 20 65 6e 76 66 6f 72 63 65 21 3d 4e 55 4c 4c  ( envforce!=NULL
e72b0 20 29 7b 0a 20 20 20 20 20 20 75 73 65 50 72 6f   ){.      usePro
e72c0 78 79 20 3d 20 61 74 6f 69 28 65 6e 76 66 6f 72  xy = atoi(envfor
e72d0 63 65 29 3e 30 3b 0a 20 20 20 20 7d 65 6c 73 65  ce)>0;.    }else
e72e0 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 73  {.      struct s
e72f0 74 61 74 66 73 20 66 73 49 6e 66 6f 3b 0a 20 20  tatfs fsInfo;.  
e7300 20 20 20 20 69 66 28 20 73 74 61 74 66 73 28 7a      if( statfs(z
e7310 50 61 74 68 2c 20 26 66 73 49 6e 66 6f 29 20 3d  Path, &fsInfo) =
e7320 3d 20 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20  = -1 ){.        
e7330 2f 2a 20 49 6e 20 74 68 65 6f 72 79 2c 20 74 68  /* In theory, th
e7340 65 20 63 6c 6f 73 65 28 66 64 29 20 63 61 6c 6c  e close(fd) call
e7350 20 69 73 20 73 75 62 2d 6f 70 74 69 6d 61 6c 2e   is sub-optimal.
e7360 20 49 66 20 74 68 65 20 66 69 6c 65 20 6f 70 65   If the file ope
e7370 6e 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 77  ned.        ** w
e7380 69 74 68 20 66 64 20 69 73 20 61 20 64 61 74 61  ith fd is a data
e7390 62 61 73 65 20 66 69 6c 65 2c 20 61 6e 64 20 74  base file, and t
e73a0 68 65 72 65 20 61 72 65 20 6f 74 68 65 72 20 63  here are other c
e73b0 6f 6e 6e 65 63 74 69 6f 6e 73 20 6f 70 65 6e 0a  onnections open.
e73c0 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68          ** on th
e73d0 61 74 20 66 69 6c 65 20 74 68 61 74 20 61 72 65  at file that are
e73e0 20 63 75 72 72 65 6e 74 6c 79 20 68 6f 6c 64 69   currently holdi
e73f0 6e 67 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b  ng advisory lock
e7400 73 20 6f 6e 20 69 74 2c 0a 20 20 20 20 20 20 20  s on it,.       
e7410 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 61 6c   ** then the cal
e7420 6c 20 74 6f 20 63 6c 6f 73 65 28 29 20 77 69 6c  l to close() wil
e7430 6c 20 63 61 6e 63 65 6c 20 74 68 6f 73 65 20 6c  l cancel those l
e7440 6f 63 6b 73 2e 20 49 6e 20 70 72 61 63 74 69 63  ocks. In practic
e7450 65 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 65  e,.        ** we
e7460 27 72 65 20 61 73 73 75 6d 69 6e 67 20 74 68 61  're assuming tha
e7470 74 20 73 74 61 74 66 73 28 29 20 64 6f 65 73 6e  t statfs() doesn
e7480 27 74 20 66 61 69 6c 20 76 65 72 79 20 6f 66 74  't fail very oft
e7490 65 6e 2e 20 41 74 20 6c 65 61 73 74 0a 20 20 20  en. At least.   
e74a0 20 20 20 20 20 2a 2a 20 6e 6f 74 20 77 68 69 6c       ** not whil
e74b0 65 20 6f 74 68 65 72 20 66 69 6c 65 20 64 65 73  e other file des
e74c0 63 72 69 70 74 6f 72 73 20 6f 70 65 6e 65 64 20  criptors opened 
e74d0 62 79 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63  by the same proc
e74e0 65 73 73 20 6f 6e 0a 20 20 20 20 20 20 20 20 2a  ess on.        *
e74f0 2a 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 20  * the same file 
e7500 61 72 65 20 77 6f 72 6b 69 6e 67 2e 20 20 2a 2f  are working.  */
e7510 0a 20 20 20 20 20 20 20 20 70 2d 3e 6c 61 73 74  .        p->last
e7520 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
e7530 20 20 20 20 20 20 20 69 66 28 20 64 69 72 66 64         if( dirfd
e7540 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  >=0 ){.         
e7550 20 63 6c 6f 73 65 28 64 69 72 66 64 29 3b 20 2f   close(dirfd); /
e7560 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20  * silently leak 
e7570 69 66 20 66 61 69 6c 2c 20 69 6e 20 65 72 72 6f  if fail, in erro
e7580 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20  r */.        }. 
e7590 20 20 20 20 20 20 20 63 6c 6f 73 65 28 66 64 29         close(fd)
e75a0 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65  ; /* silently le
e75b0 61 6b 20 69 66 20 66 61 69 6c 2c 20 69 6e 20 65  ak if fail, in e
e75c0 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20  rror */.        
e75d0 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52  rc = SQLITE_IOER
e75e0 52 5f 41 43 43 45 53 53 3b 0a 20 20 20 20 20 20  R_ACCESS;.      
e75f0 20 20 67 6f 74 6f 20 6f 70 65 6e 5f 66 69 6e 69    goto open_fini
e7600 73 68 65 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20  shed;.      }.  
e7610 20 20 20 20 75 73 65 50 72 6f 78 79 20 3d 20 21      useProxy = !
e7620 28 66 73 49 6e 66 6f 2e 66 5f 66 6c 61 67 73 26  (fsInfo.f_flags&
e7630 4d 4e 54 5f 4c 4f 43 41 4c 29 3b 0a 20 20 20 20  MNT_LOCAL);.    
e7640 7d 0a 20 20 20 20 69 66 28 20 75 73 65 50 72 6f  }.    if( usePro
e7650 78 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  xy ){.      rc =
e7660 20 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28   fillInUnixFile(
e7670 70 56 66 73 2c 20 66 64 2c 20 64 69 72 66 64 2c  pVfs, fd, dirfd,
e7680 20 70 46 69 6c 65 2c 20 7a 50 61 74 68 2c 20 6e   pFile, zPath, n
e7690 6f 4c 6f 63 6b 2c 20 69 73 44 65 6c 65 74 65 29  oLock, isDelete)
e76a0 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  ;.      if( rc==
e76b0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
e76c0 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 54       rc = proxyT
e76d0 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c 65  ransformUnixFile
e76e0 28 28 75 6e 69 78 46 69 6c 65 2a 29 70 46 69 6c  ((unixFile*)pFil
e76f0 65 2c 20 22 3a 61 75 74 6f 3a 22 29 3b 0a 20 20  e, ":auto:");.  
e7700 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f      }.      goto
e7710 20 6f 70 65 6e 5f 66 69 6e 69 73 68 65 64 3b 0a   open_finished;.
e7720 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
e7730 0a 20 20 0a 20 20 72 63 20 3d 20 66 69 6c 6c 49  .  .  rc = fillI
e7740 6e 55 6e 69 78 46 69 6c 65 28 70 56 66 73 2c 20  nUnixFile(pVfs, 
e7750 66 64 2c 20 64 69 72 66 64 2c 20 70 46 69 6c 65  fd, dirfd, pFile
e7760 2c 20 7a 50 61 74 68 2c 20 6e 6f 4c 6f 63 6b 2c  , zPath, noLock,
e7770 20 69 73 44 65 6c 65 74 65 29 3b 0a 6f 70 65 6e   isDelete);.open
e7780 5f 66 69 6e 69 73 68 65 64 3a 0a 20 20 69 66 28  _finished:.  if(
e7790 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
e77a0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  {.    sqlite3_fr
e77b0 65 65 28 70 2d 3e 70 55 6e 75 73 65 64 29 3b 0a  ee(p->pUnused);.
e77c0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
e77d0 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74  .}.../*.** Delet
e77e0 65 20 74 68 65 20 66 69 6c 65 20 61 74 20 7a 50  e the file at zP
e77f0 61 74 68 2e 20 49 66 20 74 68 65 20 64 69 72 53  ath. If the dirS
e7800 79 6e 63 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ync argument is 
e7810 74 72 75 65 2c 20 66 73 79 6e 63 28 29 0a 2a 2a  true, fsync().**
e7820 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 61   the directory a
e7830 66 74 65 72 20 64 65 6c 65 74 69 6e 67 20 74 68  fter deleting th
e7840 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  e file..*/.stati
e7850 63 20 69 6e 74 20 75 6e 69 78 44 65 6c 65 74 65  c int unixDelete
e7860 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  (.  sqlite3_vfs 
e7870 2a 4e 6f 74 55 73 65 64 2c 20 20 20 20 20 2f 2a  *NotUsed,     /*
e7880 20 56 46 53 20 63 6f 6e 74 61 69 6e 69 6e 67 20   VFS containing 
e7890 74 68 69 73 20 61 73 20 74 68 65 20 78 44 65 6c  this as the xDel
e78a0 65 74 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20  ete method */.  
e78b0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74  const char *zPat
e78c0 68 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d  h,        /* Nam
e78d0 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 62 65 20  e of file to be 
e78e0 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 69 6e 74  deleted */.  int
e78f0 20 64 69 72 53 79 6e 63 20 20 20 20 20 20 20 20   dirSync        
e7900 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75         /* If tru
e7910 65 2c 20 66 73 79 6e 63 28 29 20 64 69 72 65 63  e, fsync() direc
e7920 74 6f 72 79 20 61 66 74 65 72 20 64 65 6c 65 74  tory after delet
e7930 69 6e 67 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20  ing file */.){. 
e7940 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
e7950 5f 4f 4b 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  _OK;.  UNUSED_PA
e7960 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
e7970 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ;.  SimulateIOEr
e7980 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54  ror(return SQLIT
e7990 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 29 3b  E_IOERR_DELETE);
e79a0 0a 20 20 75 6e 6c 69 6e 6b 28 7a 50 61 74 68 29  .  unlink(zPath)
e79b0 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
e79c0 5f 44 49 53 41 42 4c 45 5f 44 49 52 53 59 4e 43  _DISABLE_DIRSYNC
e79d0 0a 20 20 69 66 28 20 64 69 72 53 79 6e 63 20 29  .  if( dirSync )
e79e0 7b 0a 20 20 20 20 69 6e 74 20 66 64 3b 0a 20 20  {.    int fd;.  
e79f0 20 20 72 63 20 3d 20 6f 70 65 6e 44 69 72 65 63    rc = openDirec
e7a00 74 6f 72 79 28 7a 50 61 74 68 2c 20 26 66 64 29  tory(zPath, &fd)
e7a10 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
e7a20 4c 49 54 45 5f 4f 4b 20 29 7b 0a 23 69 66 20 4f  LITE_OK ){.#if O
e7a30 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 20 20  S_VXWORKS.      
e7a40 69 66 28 20 66 73 79 6e 63 28 66 64 29 3d 3d 2d  if( fsync(fd)==-
e7a50 31 20 29 0a 23 65 6c 73 65 0a 20 20 20 20 20 20  1 ).#else.      
e7a60 69 66 28 20 66 73 79 6e 63 28 66 64 29 20 29 0a  if( fsync(fd) ).
e7a70 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a 20  #endif.      {. 
e7a80 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
e7a90 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f 46 53 59  TE_IOERR_DIR_FSY
e7aa0 4e 43 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  NC;.      }.    
e7ab0 20 20 69 66 28 20 63 6c 6f 73 65 28 66 64 29 26    if( close(fd)&
e7ac0 26 21 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20  &!rc ){.        
e7ad0 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52  rc = SQLITE_IOER
e7ae0 52 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20  R_DIR_CLOSE;.   
e7af0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23     }.    }.  }.#
e7b00 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 72  endif.  return r
e7b10 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74  c;.}../*.** Test
e7b20 20 74 68 65 20 65 78 69 73 74 61 6e 63 65 20 6f   the existance o
e7b30 66 20 6f 72 20 61 63 63 65 73 73 20 70 65 72 6d  f or access perm
e7b40 69 73 73 69 6f 6e 73 20 6f 66 20 66 69 6c 65 20  issions of file 
e7b50 7a 50 61 74 68 2e 20 54 68 65 0a 2a 2a 20 74 65  zPath. The.** te
e7b60 73 74 20 70 65 72 66 6f 72 6d 65 64 20 64 65 70  st performed dep
e7b70 65 6e 64 73 20 6f 6e 20 74 68 65 20 76 61 6c 75  ends on the valu
e7b80 65 20 6f 66 20 66 6c 61 67 73 3a 0a 2a 2a 0a 2a  e of flags:.**.*
e7b90 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 41 43 43  *     SQLITE_ACC
e7ba0 45 53 53 5f 45 58 49 53 54 53 3a 20 52 65 74 75  ESS_EXISTS: Retu
e7bb0 72 6e 20 31 20 69 66 20 74 68 65 20 66 69 6c 65  rn 1 if the file
e7bc0 20 65 78 69 73 74 73 0a 2a 2a 20 20 20 20 20 53   exists.**     S
e7bd0 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41  QLITE_ACCESS_REA
e7be0 44 57 52 49 54 45 3a 20 52 65 74 75 72 6e 20 31  DWRITE: Return 1
e7bf0 20 69 66 20 74 68 65 20 66 69 6c 65 20 69 73 20   if the file is 
e7c00 72 65 61 64 20 61 6e 64 20 77 72 69 74 61 62 6c  read and writabl
e7c10 65 2e 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45  e..**     SQLITE
e7c20 5f 41 43 43 45 53 53 5f 52 45 41 44 4f 4e 4c 59  _ACCESS_READONLY
e7c30 3a 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68  : Return 1 if th
e7c40 65 20 66 69 6c 65 20 69 73 20 72 65 61 64 61 62  e file is readab
e7c50 6c 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77  le..**.** Otherw
e7c60 69 73 65 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f  ise return 0..*/
e7c70 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78  .static int unix
e7c80 41 63 63 65 73 73 28 0a 20 20 73 71 6c 69 74 65  Access(.  sqlite
e7c90 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20  3_vfs *NotUsed, 
e7ca0 20 20 2f 2a 20 54 68 65 20 56 46 53 20 63 6f 6e    /* The VFS con
e7cb0 74 61 69 6e 69 6e 67 20 74 68 69 73 20 78 41 63  taining this xAc
e7cc0 63 65 73 73 20 6d 65 74 68 6f 64 20 2a 2f 0a 20  cess method */. 
e7cd0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61   const char *zPa
e7ce0 74 68 2c 20 20 20 20 20 20 2f 2a 20 50 61 74 68  th,      /* Path
e7cf0 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74 6f 20   of the file to 
e7d00 65 78 61 6d 69 6e 65 20 2a 2f 0a 20 20 69 6e 74  examine */.  int
e7d10 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20   flags,         
e7d20 20 20 20 20 20 2f 2a 20 57 68 61 74 20 64 6f 20       /* What do 
e7d30 77 65 20 77 61 6e 74 20 74 6f 20 6c 65 61 72 6e  we want to learn
e7d40 20 61 62 6f 75 74 20 74 68 65 20 7a 50 61 74 68   about the zPath
e7d50 20 66 69 6c 65 3f 20 2a 2f 0a 20 20 69 6e 74 20   file? */.  int 
e7d60 2a 70 52 65 73 4f 75 74 20 20 20 20 20 20 20 20  *pResOut        
e7d70 20 20 20 20 2f 2a 20 57 72 69 74 65 20 72 65 73      /* Write res
e7d80 75 6c 74 20 62 6f 6f 6c 65 61 6e 20 68 65 72 65  ult boolean here
e7d90 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61 6d 6f   */.){.  int amo
e7da0 64 65 20 3d 20 30 3b 0a 20 20 55 4e 55 53 45 44  de = 0;.  UNUSED
e7db0 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73  _PARAMETER(NotUs
e7dc0 65 64 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49  ed);.  SimulateI
e7dd0 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53  OError( return S
e7de0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45  QLITE_IOERR_ACCE
e7df0 53 53 3b 20 29 3b 0a 20 20 73 77 69 74 63 68 28  SS; );.  switch(
e7e00 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 63 61   flags ){.    ca
e7e10 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53  se SQLITE_ACCESS
e7e20 5f 45 58 49 53 54 53 3a 0a 20 20 20 20 20 20 61  _EXISTS:.      a
e7e30 6d 6f 64 65 20 3d 20 46 5f 4f 4b 3b 0a 20 20 20  mode = F_OK;.   
e7e40 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
e7e50 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53  se SQLITE_ACCESS
e7e60 5f 52 45 41 44 57 52 49 54 45 3a 0a 20 20 20 20  _READWRITE:.    
e7e70 20 20 61 6d 6f 64 65 20 3d 20 57 5f 4f 4b 7c 52    amode = W_OK|R
e7e80 5f 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  _OK;.      break
e7e90 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
e7ea0 45 5f 41 43 43 45 53 53 5f 52 45 41 44 3a 0a 20  E_ACCESS_READ:. 
e7eb0 20 20 20 20 20 61 6d 6f 64 65 20 3d 20 52 5f 4f       amode = R_O
e7ec0 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  K;.      break;.
e7ed0 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20  .    default:.  
e7ee0 20 20 20 20 61 73 73 65 72 74 28 21 22 49 6e 76      assert(!"Inv
e7ef0 61 6c 69 64 20 66 6c 61 67 73 20 61 72 67 75 6d  alid flags argum
e7f00 65 6e 74 22 29 3b 0a 20 20 7d 0a 20 20 2a 70 52  ent");.  }.  *pR
e7f10 65 73 4f 75 74 20 3d 20 28 61 63 63 65 73 73 28  esOut = (access(
e7f20 7a 50 61 74 68 2c 20 61 6d 6f 64 65 29 3d 3d 30  zPath, amode)==0
e7f30 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
e7f40 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  TE_OK;.}.../*.**
e7f50 20 54 75 72 6e 20 61 20 72 65 6c 61 74 69 76 65   Turn a relative
e7f60 20 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 61   pathname into a
e7f70 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 2e 20   full pathname. 
e7f80 54 68 65 20 72 65 6c 61 74 69 76 65 20 70 61 74  The relative pat
e7f90 68 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64 20 61  h.** is stored a
e7fa0 73 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74  s a nul-terminat
e7fb0 65 64 20 73 74 72 69 6e 67 20 69 6e 20 74 68 65  ed string in the
e7fc0 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20   buffer pointed 
e7fd0 74 6f 20 62 79 0a 2a 2a 20 7a 50 61 74 68 2e 20  to by.** zPath. 
e7fe0 0a 2a 2a 0a 2a 2a 20 7a 4f 75 74 20 70 6f 69 6e  .**.** zOut poin
e7ff0 74 73 20 74 6f 20 61 20 62 75 66 66 65 72 20 6f  ts to a buffer o
e8000 66 20 61 74 20 6c 65 61 73 74 20 73 71 6c 69 74  f at least sqlit
e8010 65 33 5f 76 66 73 2e 6d 78 50 61 74 68 6e 61 6d  e3_vfs.mxPathnam
e8020 65 20 62 79 74 65 73 20 0a 2a 2a 20 28 69 6e 20  e bytes .** (in 
e8030 74 68 69 73 20 63 61 73 65 2c 20 4d 41 58 5f 50  this case, MAX_P
e8040 41 54 48 4e 41 4d 45 20 62 79 74 65 73 29 2e 20  ATHNAME bytes). 
e8050 54 68 65 20 66 75 6c 6c 2d 70 61 74 68 20 69 73  The full-path is
e8060 20 77 72 69 74 74 65 6e 20 74 6f 0a 2a 2a 20 74   written to.** t
e8070 68 69 73 20 62 75 66 66 65 72 20 62 65 66 6f 72  his buffer befor
e8080 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a  e returning..*/.
e8090 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 46  static int unixF
e80a0 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73  ullPathname(.  s
e80b0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
e80c0 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
e80d0 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 20 6f  Pointer to vfs o
e80e0 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74  bject */.  const
e80f0 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20   char *zPath,   
e8100 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 73 73           /* Poss
e8110 69 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69 6e  ibly relative in
e8120 70 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69 6e  put path */.  in
e8130 74 20 6e 4f 75 74 2c 20 20 20 20 20 20 20 20 20  t nOut,         
e8140 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
e8150 69 7a 65 20 6f 66 20 6f 75 74 70 75 74 20 62 75  ize of output bu
e8160 66 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f  ffer in bytes */
e8170 0a 20 20 63 68 61 72 20 2a 7a 4f 75 74 20 20 20  .  char *zOut   
e8180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e8190 20 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 66 65   /* Output buffe
e81a0 72 20 2a 2f 0a 29 7b 0a 0a 20 20 2f 2a 20 49 74  r */.){..  /* It
e81b0 27 73 20 6f 64 64 20 74 6f 20 73 69 6d 75 6c 61  's odd to simula
e81c0 74 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72 20 68  te an io-error h
e81d0 65 72 65 2c 20 62 75 74 20 72 65 61 6c 6c 79 20  ere, but really 
e81e0 74 68 69 73 20 69 73 20 6a 75 73 74 0a 20 20 2a  this is just.  *
e81f0 2a 20 75 73 69 6e 67 20 74 68 65 20 69 6f 2d 65  * using the io-e
e8200 72 72 6f 72 20 69 6e 66 72 61 73 74 72 75 63 74  rror infrastruct
e8210 75 72 65 20 74 6f 20 74 65 73 74 20 74 68 61 74  ure to test that
e8220 20 53 51 4c 69 74 65 20 68 61 6e 64 6c 65 73 20   SQLite handles 
e8230 74 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 69  this.  ** functi
e8240 6f 6e 20 66 61 69 6c 69 6e 67 2e 20 54 68 69 73  on failing. This
e8250 20 66 75 6e 63 74 69 6f 6e 20 63 6f 75 6c 64 20   function could 
e8260 66 61 69 6c 20 69 66 2c 20 66 6f 72 20 65 78 61  fail if, for exa
e8270 6d 70 6c 65 2c 20 74 68 65 0a 20 20 2a 2a 20 63  mple, the.  ** c
e8280 75 72 72 65 6e 74 20 77 6f 72 6b 69 6e 67 20 64  urrent working d
e8290 69 72 65 63 74 6f 72 79 20 68 61 73 20 62 65 65  irectory has bee
e82a0 6e 20 75 6e 6c 69 6e 6b 65 64 2e 0a 20 20 2a 2f  n unlinked..  */
e82b0 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  .  SimulateIOErr
e82c0 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54  or( return SQLIT
e82d0 45 5f 45 52 52 4f 52 20 29 3b 0a 0a 20 20 61 73  E_ERROR );..  as
e82e0 73 65 72 74 28 20 70 56 66 73 2d 3e 6d 78 50 61  sert( pVfs->mxPa
e82f0 74 68 6e 61 6d 65 3d 3d 4d 41 58 5f 50 41 54 48  thname==MAX_PATH
e8300 4e 41 4d 45 20 29 3b 0a 20 20 55 4e 55 53 45 44  NAME );.  UNUSED
e8310 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29  _PARAMETER(pVfs)
e8320 3b 0a 0a 20 20 7a 4f 75 74 5b 6e 4f 75 74 2d 31  ;..  zOut[nOut-1
e8330 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66 28 20  ] = '\0';.  if( 
e8340 7a 50 61 74 68 5b 30 5d 3d 3d 27 2f 27 20 29 7b  zPath[0]=='/' ){
e8350 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
e8360 72 69 6e 74 66 28 6e 4f 75 74 2c 20 7a 4f 75 74  rintf(nOut, zOut
e8370 2c 20 22 25 73 22 2c 20 7a 50 61 74 68 29 3b 0a  , "%s", zPath);.
e8380 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74    }else{.    int
e8390 20 6e 43 77 64 3b 0a 20 20 20 20 69 66 28 20 67   nCwd;.    if( g
e83a0 65 74 63 77 64 28 7a 4f 75 74 2c 20 6e 4f 75 74  etcwd(zOut, nOut
e83b0 2d 31 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  -1)==0 ){.      
e83c0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41  return SQLITE_CA
e83d0 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20  NTOPEN;.    }.  
e83e0 20 20 6e 43 77 64 20 3d 20 28 69 6e 74 29 73 74    nCwd = (int)st
e83f0 72 6c 65 6e 28 7a 4f 75 74 29 3b 0a 20 20 20 20  rlen(zOut);.    
e8400 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
e8410 28 6e 4f 75 74 2d 6e 43 77 64 2c 20 26 7a 4f 75  (nOut-nCwd, &zOu
e8420 74 5b 6e 43 77 64 5d 2c 20 22 2f 25 73 22 2c 20  t[nCwd], "/%s", 
e8430 7a 50 61 74 68 29 3b 0a 20 20 7d 0a 20 20 72 65  zPath);.  }.  re
e8440 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
e8450 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  }...#ifndef SQLI
e8460 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54  TE_OMIT_LOAD_EXT
e8470 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74  ENSION./*.** Int
e8480 65 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e  erfaces for open
e8490 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62  ing a shared lib
e84a0 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e  rary, finding en
e84b0 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69  try points.** wi
e84c0 74 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20  thin the shared 
e84d0 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f  library, and clo
e84e0 73 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20  sing the shared 
e84f0 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 6e 63  library..*/.#inc
e8500 6c 75 64 65 20 3c 64 6c 66 63 6e 2e 68 3e 0a 73  lude <dlfcn.h>.s
e8510 74 61 74 69 63 20 76 6f 69 64 20 2a 75 6e 69 78  tatic void *unix
e8520 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76  DlOpen(sqlite3_v
e8530 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 63 6f 6e  fs *NotUsed, con
e8540 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  st char *zFilena
e8550 6d 65 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41  me){.  UNUSED_PA
e8560 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
e8570 3b 0a 20 20 72 65 74 75 72 6e 20 64 6c 6f 70 65  ;.  return dlope
e8580 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 52 54 4c  n(zFilename, RTL
e8590 44 5f 4e 4f 57 20 7c 20 52 54 4c 44 5f 47 4c 4f  D_NOW | RTLD_GLO
e85a0 42 41 4c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  BAL);.}../*.** S
e85b0 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 69 73  QLite calls this
e85c0 20 66 75 6e 63 74 69 6f 6e 20 69 6d 6d 65 64 69   function immedi
e85d0 61 74 65 6c 79 20 61 66 74 65 72 20 61 20 63 61  ately after a ca
e85e0 6c 6c 20 74 6f 20 75 6e 69 78 44 6c 53 79 6d 28  ll to unixDlSym(
e85f0 29 20 6f 72 0a 2a 2a 20 75 6e 69 78 44 6c 4f 70  ) or.** unixDlOp
e8600 65 6e 28 29 20 66 61 69 6c 73 20 28 72 65 74 75  en() fails (retu
e8610 72 6e 73 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74  rns a null point
e8620 65 72 29 2e 20 49 66 20 61 20 6d 6f 72 65 20 64  er). If a more d
e8630 65 74 61 69 6c 65 64 20 65 72 72 6f 72 0a 2a 2a  etailed error.**
e8640 20 6d 65 73 73 61 67 65 20 69 73 20 61 76 61 69   message is avai
e8650 6c 61 62 6c 65 2c 20 69 74 20 69 73 20 77 72 69  lable, it is wri
e8660 74 74 65 6e 20 74 6f 20 7a 42 75 66 4f 75 74 2e  tten to zBufOut.
e8670 20 49 66 20 6e 6f 20 65 72 72 6f 72 20 6d 65 73   If no error mes
e8680 73 61 67 65 0a 2a 2a 20 69 73 20 61 76 61 69 6c  sage.** is avail
e8690 61 62 6c 65 2c 20 7a 42 75 66 4f 75 74 20 69 73  able, zBufOut is
e86a0 20 6c 65 66 74 20 75 6e 6d 6f 64 69 66 69 65 64   left unmodified
e86b0 20 61 6e 64 20 53 51 4c 69 74 65 20 75 73 65 73   and SQLite uses
e86c0 20 61 20 64 65 66 61 75 6c 74 0a 2a 2a 20 65 72   a default.** er
e86d0 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2f 0a  ror message..*/.
e86e0 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e 69 78  static void unix
e86f0 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f  DlError(sqlite3_
e8700 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e  vfs *NotUsed, in
e8710 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42  t nBuf, char *zB
e8720 75 66 4f 75 74 29 7b 0a 20 20 63 68 61 72 20 2a  ufOut){.  char *
e8730 7a 45 72 72 3b 0a 20 20 55 4e 55 53 45 44 5f 50  zErr;.  UNUSED_P
e8740 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
e8750 29 3b 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75  );.  unixEnterMu
e8760 74 65 78 28 29 3b 0a 20 20 7a 45 72 72 20 3d 20  tex();.  zErr = 
e8770 64 6c 65 72 72 6f 72 28 29 3b 0a 20 20 69 66 28  dlerror();.  if(
e8780 20 7a 45 72 72 20 29 7b 0a 20 20 20 20 73 71 6c   zErr ){.    sql
e8790 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42  ite3_snprintf(nB
e87a0 75 66 2c 20 7a 42 75 66 4f 75 74 2c 20 22 25 73  uf, zBufOut, "%s
e87b0 22 2c 20 7a 45 72 72 29 3b 0a 20 20 7d 0a 20 20  ", zErr);.  }.  
e87c0 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29  unixLeaveMutex()
e87d0 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  ;.}.static void 
e87e0 28 2a 75 6e 69 78 44 6c 53 79 6d 28 73 71 6c 69  (*unixDlSym(sqli
e87f0 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64  te3_vfs *NotUsed
e8800 2c 20 76 6f 69 64 20 2a 70 2c 20 63 6f 6e 73 74  , void *p, const
e8810 20 63 68 61 72 2a 7a 53 79 6d 29 29 28 76 6f 69   char*zSym))(voi
e8820 64 29 7b 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 47  d){.  /* .  ** G
e8830 43 43 20 77 69 74 68 20 2d 70 65 64 61 6e 74 69  CC with -pedanti
e8840 63 2d 65 72 72 6f 72 73 20 73 61 79 73 20 74 68  c-errors says th
e8850 61 74 20 43 39 30 20 64 6f 65 73 20 6e 6f 74 20  at C90 does not 
e8860 61 6c 6c 6f 77 20 61 20 76 6f 69 64 2a 20 74 6f  allow a void* to
e8870 20 62 65 0a 20 20 2a 2a 20 63 61 73 74 20 69 6e   be.  ** cast in
e8880 74 6f 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  to a pointer to 
e8890 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 41 6e 64  a function.  And
e88a0 20 79 65 74 20 74 68 65 20 6c 69 62 72 61 72 79   yet the library
e88b0 20 64 6c 73 79 6d 28 29 20 72 6f 75 74 69 6e 65   dlsym() routine
e88c0 0a 20 20 2a 2a 20 72 65 74 75 72 6e 73 20 61 20  .  ** returns a 
e88d0 76 6f 69 64 2a 20 77 68 69 63 68 20 69 73 20 72  void* which is r
e88e0 65 61 6c 6c 79 20 61 20 70 6f 69 6e 74 65 72 20  eally a pointer 
e88f0 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20  to a function.  
e8900 53 6f 20 68 6f 77 20 64 6f 20 77 65 0a 20 20 2a  So how do we.  *
e8910 2a 20 75 73 65 20 64 6c 73 79 6d 28 29 20 77 69  * use dlsym() wi
e8920 74 68 20 2d 70 65 64 61 6e 74 69 63 2d 65 72 72  th -pedantic-err
e8930 6f 72 73 3f 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56  ors?.  **.  ** V
e8940 61 72 69 61 62 6c 65 20 78 20 62 65 6c 6f 77 20  ariable x below 
e8950 69 73 20 64 65 66 69 6e 65 64 20 74 6f 20 62 65  is defined to be
e8960 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
e8970 66 75 6e 63 74 69 6f 6e 20 74 61 6b 69 6e 67 0a  function taking.
e8980 20 20 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20    ** parameters 
e8990 76 6f 69 64 2a 20 61 6e 64 20 63 6f 6e 73 74 20  void* and const 
e89a0 63 68 61 72 2a 20 61 6e 64 20 72 65 74 75 72 6e  char* and return
e89b0 69 6e 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  ing a pointer to
e89c0 20 61 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a   a function..  *
e89d0 2a 20 57 65 20 69 6e 69 74 69 61 6c 69 7a 65 20  * We initialize 
e89e0 78 20 62 79 20 61 73 73 69 67 6e 69 6e 67 20 69  x by assigning i
e89f0 74 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  t a pointer to t
e8a00 68 65 20 64 6c 73 79 6d 28 29 20 66 75 6e 63 74  he dlsym() funct
e8a10 69 6f 6e 2e 0a 20 20 2a 2a 20 28 54 68 61 74 20  ion..  ** (That 
e8a20 61 73 73 69 67 6e 6d 65 6e 74 20 72 65 71 75 69  assignment requi
e8a30 72 65 73 20 61 20 63 61 73 74 2e 29 20 20 54 68  res a cast.)  Th
e8a40 65 6e 20 77 65 20 63 61 6c 6c 20 74 68 65 20 66  en we call the f
e8a50 75 6e 63 74 69 6f 6e 20 74 68 61 74 0a 20 20 2a  unction that.  *
e8a60 2a 20 78 20 70 6f 69 6e 74 73 20 74 6f 2e 20 20  * x points to.  
e8a70 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20  .  **.  ** This 
e8a80 77 6f 72 6b 2d 61 72 6f 75 6e 64 20 69 73 20 75  work-around is u
e8a90 6e 6c 69 6b 65 6c 79 20 74 6f 20 77 6f 72 6b 20  nlikely to work 
e8aa0 63 6f 72 72 65 63 74 6c 79 20 6f 6e 20 61 6e 79  correctly on any
e8ab0 20 73 79 73 74 65 6d 20 77 68 65 72 65 0a 20 20   system where.  
e8ac0 2a 2a 20 79 6f 75 20 72 65 61 6c 6c 79 20 63 61  ** you really ca
e8ad0 6e 6e 6f 74 20 63 61 73 74 20 61 20 66 75 6e 63  nnot cast a func
e8ae0 74 69 6f 6e 20 70 6f 69 6e 74 65 72 20 69 6e 74  tion pointer int
e8af0 6f 20 76 6f 69 64 2a 2e 20 20 42 75 74 20 74 68  o void*.  But th
e8b00 65 6e 2c 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20  en, on the.  ** 
e8b10 6f 74 68 65 72 20 68 61 6e 64 2c 20 64 6c 73 79  other hand, dlsy
e8b20 6d 28 29 20 77 69 6c 6c 20 6e 6f 74 20 77 6f 72  m() will not wor
e8b30 6b 20 6f 6e 20 73 75 63 68 20 61 20 73 79 73 74  k on such a syst
e8b40 65 6d 20 65 69 74 68 65 72 2c 20 73 6f 20 77 65  em either, so we
e8b50 20 68 61 76 65 0a 20 20 2a 2a 20 6e 6f 74 20 72   have.  ** not r
e8b60 65 61 6c 6c 79 20 6c 6f 73 74 20 61 6e 79 74 68  eally lost anyth
e8b70 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 76 6f 69 64  ing..  */.  void
e8b80 20 28 2a 28 2a 78 29 28 76 6f 69 64 2a 2c 63 6f   (*(*x)(void*,co
e8b90 6e 73 74 20 63 68 61 72 2a 29 29 28 76 6f 69 64  nst char*))(void
e8ba0 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
e8bb0 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a  METER(NotUsed);.
e8bc0 20 20 78 20 3d 20 28 76 6f 69 64 28 2a 28 2a 29    x = (void(*(*)
e8bd0 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61  (void*,const cha
e8be0 72 2a 29 29 28 76 6f 69 64 29 29 64 6c 73 79 6d  r*))(void))dlsym
e8bf0 3b 0a 20 20 72 65 74 75 72 6e 20 28 2a 78 29 28  ;.  return (*x)(
e8c00 70 2c 20 7a 53 79 6d 29 3b 0a 7d 0a 73 74 61 74  p, zSym);.}.stat
e8c10 69 63 20 76 6f 69 64 20 75 6e 69 78 44 6c 43 6c  ic void unixDlCl
e8c20 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ose(sqlite3_vfs 
e8c30 2a 4e 6f 74 55 73 65 64 2c 20 76 6f 69 64 20 2a  *NotUsed, void *
e8c40 70 48 61 6e 64 6c 65 29 7b 0a 20 20 55 4e 55 53  pHandle){.  UNUS
e8c50 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74  ED_PARAMETER(Not
e8c60 55 73 65 64 29 3b 0a 20 20 64 6c 63 6c 6f 73 65  Used);.  dlclose
e8c70 28 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c  (pHandle);.}.#el
e8c80 73 65 20 2f 2a 20 69 66 20 53 51 4c 49 54 45 5f  se /* if SQLITE_
e8c90 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53  OMIT_LOAD_EXTENS
e8ca0 49 4f 4e 20 69 73 20 64 65 66 69 6e 65 64 3a 20  ION is defined: 
e8cb0 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69  */.  #define uni
e8cc0 78 44 6c 4f 70 65 6e 20 20 30 0a 20 20 23 64 65  xDlOpen  0.  #de
e8cd0 66 69 6e 65 20 75 6e 69 78 44 6c 45 72 72 6f 72  fine unixDlError
e8ce0 20 30 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69   0.  #define uni
e8cf0 78 44 6c 53 79 6d 20 20 20 30 0a 20 20 23 64 65  xDlSym   0.  #de
e8d00 66 69 6e 65 20 75 6e 69 78 44 6c 43 6c 6f 73 65  fine unixDlClose
e8d10 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a   0.#endif../*.**
e8d20 20 57 72 69 74 65 20 6e 42 75 66 20 62 79 74 65   Write nBuf byte
e8d30 73 20 6f 66 20 72 61 6e 64 6f 6d 20 64 61 74 61  s of random data
e8d40 20 74 6f 20 74 68 65 20 73 75 70 70 6c 69 65 64   to the supplied
e8d50 20 62 75 66 66 65 72 20 7a 42 75 66 2e 0a 2a 2f   buffer zBuf..*/
e8d60 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78  .static int unix
e8d70 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74  Randomness(sqlit
e8d80 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c  e3_vfs *NotUsed,
e8d90 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20   int nBuf, char 
e8da0 2a 7a 42 75 66 29 7b 0a 20 20 55 4e 55 53 45 44  *zBuf){.  UNUSED
e8db0 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73  _PARAMETER(NotUs
e8dc0 65 64 29 3b 0a 20 20 61 73 73 65 72 74 28 28 73  ed);.  assert((s
e8dd0 69 7a 65 5f 74 29 6e 42 75 66 3e 3d 28 73 69 7a  ize_t)nBuf>=(siz
e8de0 65 6f 66 28 74 69 6d 65 5f 74 29 2b 73 69 7a 65  eof(time_t)+size
e8df0 6f 66 28 69 6e 74 29 29 29 3b 0a 0a 20 20 2f 2a  of(int)));..  /*
e8e00 20 57 65 20 68 61 76 65 20 74 6f 20 69 6e 69 74   We have to init
e8e10 69 61 6c 69 7a 65 20 7a 42 75 66 20 74 6f 20 70  ialize zBuf to p
e8e20 72 65 76 65 6e 74 20 76 61 6c 67 72 69 6e 64 20  revent valgrind 
e8e30 66 72 6f 6d 20 72 65 70 6f 72 74 69 6e 67 0a 20  from reporting. 
e8e40 20 2a 2a 20 65 72 72 6f 72 73 2e 20 20 54 68 65   ** errors.  The
e8e50 20 72 65 70 6f 72 74 73 20 69 73 73 75 65 64 20   reports issued 
e8e60 62 79 20 76 61 6c 67 72 69 6e 64 20 61 72 65 20  by valgrind are 
e8e70 69 6e 63 6f 72 72 65 63 74 20 2d 20 77 65 20 77  incorrect - we w
e8e80 6f 75 6c 64 0a 20 20 2a 2a 20 70 72 65 66 65 72  ould.  ** prefer
e8e90 20 74 68 61 74 20 74 68 65 20 72 61 6e 64 6f 6d   that the random
e8ea0 6e 65 73 73 20 62 65 20 69 6e 63 72 65 61 73 65  ness be increase
e8eb0 64 20 62 79 20 6d 61 6b 69 6e 67 20 75 73 65 20  d by making use 
e8ec0 6f 66 20 74 68 65 0a 20 20 2a 2a 20 75 6e 69 6e  of the.  ** unin
e8ed0 69 74 69 61 6c 69 7a 65 64 20 73 70 61 63 65 20  itialized space 
e8ee0 69 6e 20 7a 42 75 66 20 2d 20 62 75 74 20 76 61  in zBuf - but va
e8ef0 6c 67 72 69 6e 64 20 65 72 72 6f 72 73 20 74 65  lgrind errors te
e8f00 6e 64 20 74 6f 20 77 6f 72 72 79 0a 20 20 2a 2a  nd to worry.  **
e8f10 20 73 6f 6d 65 20 75 73 65 72 73 2e 20 20 52 61   some users.  Ra
e8f20 74 68 65 72 20 74 68 61 6e 20 61 72 67 75 65 2c  ther than argue,
e8f30 20 69 74 20 73 65 65 6d 73 20 65 61 73 69 65 72   it seems easier
e8f40 20 6a 75 73 74 20 74 6f 20 69 6e 69 74 69 61 6c   just to initial
e8f50 69 7a 65 0a 20 20 2a 2a 20 74 68 65 20 77 68 6f  ize.  ** the who
e8f60 6c 65 20 61 72 72 61 79 20 61 6e 64 20 73 69 6c  le array and sil
e8f70 65 6e 63 65 20 76 61 6c 67 72 69 6e 64 2c 20 65  ence valgrind, e
e8f80 76 65 6e 20 69 66 20 74 68 61 74 20 6d 65 61 6e  ven if that mean
e8f90 73 20 6c 65 73 73 20 72 61 6e 64 6f 6d 6e 65 73  s less randomnes
e8fa0 73 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 72 61  s.  ** in the ra
e8fb0 6e 64 6f 6d 20 73 65 65 64 2e 0a 20 20 2a 2a 0a  ndom seed..  **.
e8fc0 20 20 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 6e    ** When testin
e8fd0 67 2c 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20  g, initializing 
e8fe0 7a 42 75 66 5b 5d 20 74 6f 20 7a 65 72 6f 20 69  zBuf[] to zero i
e8ff0 73 20 61 6c 6c 20 77 65 20 64 6f 2e 20 20 54 68  s all we do.  Th
e9000 61 74 20 6d 65 61 6e 73 0a 20 20 2a 2a 20 74 68  at means.  ** th
e9010 61 74 20 77 65 20 61 6c 77 61 79 73 20 75 73 65  at we always use
e9020 20 74 68 65 20 73 61 6d 65 20 72 61 6e 64 6f 6d   the same random
e9030 20 6e 75 6d 62 65 72 20 73 65 71 75 65 6e 63 65   number sequence
e9040 2e 20 20 54 68 69 73 20 6d 61 6b 65 73 20 74 68  .  This makes th
e9050 65 0a 20 20 2a 2a 20 74 65 73 74 73 20 72 65 70  e.  ** tests rep
e9060 65 61 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20  eatable..  */.  
e9070 6d 65 6d 73 65 74 28 7a 42 75 66 2c 20 30 2c 20  memset(zBuf, 0, 
e9080 6e 42 75 66 29 3b 0a 23 69 66 20 21 64 65 66 69  nBuf);.#if !defi
e9090 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29  ned(SQLITE_TEST)
e90a0 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 70 69 64  .  {.    int pid
e90b0 2c 20 66 64 3b 0a 20 20 20 20 66 64 20 3d 20 6f  , fd;.    fd = o
e90c0 70 65 6e 28 22 2f 64 65 76 2f 75 72 61 6e 64 6f  pen("/dev/urando
e90d0 6d 22 2c 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a 20  m", O_RDONLY);. 
e90e0 20 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20     if( fd<0 ){. 
e90f0 20 20 20 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20       time_t t;. 
e9100 20 20 20 20 20 74 69 6d 65 28 26 74 29 3b 0a 20       time(&t);. 
e9110 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 42 75 66       memcpy(zBuf
e9120 2c 20 26 74 2c 20 73 69 7a 65 6f 66 28 74 29 29  , &t, sizeof(t))
e9130 3b 0a 20 20 20 20 20 20 70 69 64 20 3d 20 67 65  ;.      pid = ge
e9140 74 70 69 64 28 29 3b 0a 20 20 20 20 20 20 6d 65  tpid();.      me
e9150 6d 63 70 79 28 26 7a 42 75 66 5b 73 69 7a 65 6f  mcpy(&zBuf[sizeo
e9160 66 28 74 29 5d 2c 20 26 70 69 64 2c 20 73 69 7a  f(t)], &pid, siz
e9170 65 6f 66 28 70 69 64 29 29 3b 0a 20 20 20 20 20  eof(pid));.     
e9180 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28   assert( sizeof(
e9190 74 29 2b 73 69 7a 65 6f 66 28 70 69 64 29 3c 3d  t)+sizeof(pid)<=
e91a0 28 73 69 7a 65 5f 74 29 6e 42 75 66 20 29 3b 0a  (size_t)nBuf );.
e91b0 20 20 20 20 20 20 6e 42 75 66 20 3d 20 73 69 7a        nBuf = siz
e91c0 65 6f 66 28 74 29 20 2b 20 73 69 7a 65 6f 66 28  eof(t) + sizeof(
e91d0 70 69 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  pid);.    }else{
e91e0 0a 20 20 20 20 20 20 6e 42 75 66 20 3d 20 72 65  .      nBuf = re
e91f0 61 64 28 66 64 2c 20 7a 42 75 66 2c 20 6e 42 75  ad(fd, zBuf, nBu
e9200 66 29 3b 0a 20 20 20 20 20 20 63 6c 6f 73 65 28  f);.      close(
e9210 66 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23  fd);.    }.  }.#
e9220 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 6e  endif.  return n
e9230 42 75 66 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53  Buf;.}.../*.** S
e9240 6c 65 65 70 20 66 6f 72 20 61 20 6c 69 74 74 6c  leep for a littl
e9250 65 20 77 68 69 6c 65 2e 20 20 52 65 74 75 72 6e  e while.  Return
e9260 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74   the amount of t
e9270 69 6d 65 20 73 6c 65 70 74 2e 0a 2a 2a 20 54 68  ime slept..** Th
e9280 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68  e argument is th
e9290 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72  e number of micr
e92a0 6f 73 65 63 6f 6e 64 73 20 77 65 20 77 61 6e 74  oseconds we want
e92b0 20 74 6f 20 73 6c 65 65 70 2e 0a 2a 2a 20 54 68   to sleep..** Th
e92c0 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69  e return value i
e92d0 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
e92e0 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 6f 66 20  microseconds of 
e92f0 73 6c 65 65 70 20 61 63 74 75 61 6c 6c 79 0a 2a  sleep actually.*
e9300 2a 20 72 65 71 75 65 73 74 65 64 20 66 72 6f 6d  * requested from
e9310 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20   the underlying 
e9320 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  operating system
e9330 2c 20 61 20 6e 75 6d 62 65 72 20 77 68 69 63 68  , a number which
e9340 0a 2a 2a 20 6d 69 67 68 74 20 62 65 20 67 72 65  .** might be gre
e9350 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75  ater than or equ
e9360 61 6c 20 74 6f 20 74 68 65 20 61 72 67 75 6d 65  al to the argume
e9370 6e 74 2c 20 62 75 74 20 6e 6f 74 20 6c 65 73 73  nt, but not less
e9380 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 61 72 67  .** than the arg
e9390 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  ument..*/.static
e93a0 20 69 6e 74 20 75 6e 69 78 53 6c 65 65 70 28 73   int unixSleep(s
e93b0 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55  qlite3_vfs *NotU
e93c0 73 65 64 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65  sed, int microse
e93d0 63 6f 6e 64 73 29 7b 0a 23 69 66 20 4f 53 5f 56  conds){.#if OS_V
e93e0 58 57 4f 52 4b 53 0a 20 20 73 74 72 75 63 74 20  XWORKS.  struct 
e93f0 74 69 6d 65 73 70 65 63 20 73 70 3b 0a 0a 20 20  timespec sp;..  
e9400 73 70 2e 74 76 5f 73 65 63 20 3d 20 6d 69 63 72  sp.tv_sec = micr
e9410 6f 73 65 63 6f 6e 64 73 20 2f 20 31 30 30 30 30  oseconds / 10000
e9420 30 30 3b 0a 20 20 73 70 2e 74 76 5f 6e 73 65 63  00;.  sp.tv_nsec
e9430 20 3d 20 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73   = (microseconds
e9440 20 25 20 31 30 30 30 30 30 30 29 20 2a 20 31 30   % 1000000) * 10
e9450 30 30 3b 0a 20 20 6e 61 6e 6f 73 6c 65 65 70 28  00;.  nanosleep(
e9460 26 73 70 2c 20 4e 55 4c 4c 29 3b 0a 20 20 55 4e  &sp, NULL);.  UN
e9470 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
e9480 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72  otUsed);.  retur
e9490 6e 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 3b 0a  n microseconds;.
e94a0 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 48 41  #elif defined(HA
e94b0 56 45 5f 55 53 4c 45 45 50 29 20 26 26 20 48 41  VE_USLEEP) && HA
e94c0 56 45 5f 55 53 4c 45 45 50 0a 20 20 75 73 6c 65  VE_USLEEP.  usle
e94d0 65 70 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29  ep(microseconds)
e94e0 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
e94f0 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20  ETER(NotUsed);. 
e9500 20 72 65 74 75 72 6e 20 6d 69 63 72 6f 73 65 63   return microsec
e9510 6f 6e 64 73 3b 0a 23 65 6c 73 65 0a 20 20 69 6e  onds;.#else.  in
e9520 74 20 73 65 63 6f 6e 64 73 20 3d 20 28 6d 69 63  t seconds = (mic
e9530 72 6f 73 65 63 6f 6e 64 73 2b 39 39 39 39 39 39  roseconds+999999
e9540 29 2f 31 30 30 30 30 30 30 3b 0a 20 20 73 6c 65  )/1000000;.  sle
e9550 65 70 28 73 65 63 6f 6e 64 73 29 3b 0a 20 20 55  ep(seconds);.  U
e9560 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
e9570 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75  NotUsed);.  retu
e9580 72 6e 20 73 65 63 6f 6e 64 73 2a 31 30 30 30 30  rn seconds*10000
e9590 30 30 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a  00;.#endif.}../*
e95a0 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
e95b0 67 20 76 61 72 69 61 62 6c 65 2c 20 69 66 20 73  g variable, if s
e95c0 65 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f  et to a non-zero
e95d0 20 76 61 6c 75 65 2c 20 69 73 20 69 6e 74 65 72   value, is inter
e95e0 70 72 65 74 65 64 20 61 73 0a 2a 2a 20 74 68 65  preted as.** the
e95f0 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 63 6f 6e   number of secon
e9600 64 73 20 73 69 6e 63 65 20 31 39 37 30 20 61 6e  ds since 1970 an
e9610 64 20 69 73 20 75 73 65 64 20 74 6f 20 73 65 74  d is used to set
e9620 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a   the result of.*
e9630 2a 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65  * sqlite3OsCurre
e9640 6e 74 54 69 6d 65 28 29 20 64 75 72 69 6e 67 20  ntTime() during 
e9650 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 64  testing..*/.#ifd
e9660 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53  ef SQLITE_TEST.S
e9670 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
e9680 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69  lite3_current_ti
e9690 6d 65 20 3d 20 30 3b 20 20 2f 2a 20 46 61 6b 65  me = 0;  /* Fake
e96a0 20 73 79 73 74 65 6d 20 74 69 6d 65 20 69 6e 20   system time in 
e96b0 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 31 39  seconds since 19
e96c0 37 30 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f  70. */.#endif../
e96d0 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 63 75  *.** Find the cu
e96e0 72 72 65 6e 74 20 74 69 6d 65 20 28 69 6e 20 55  rrent time (in U
e96f0 6e 69 76 65 72 73 61 6c 20 43 6f 6f 72 64 69 6e  niversal Coordin
e9700 61 74 65 64 20 54 69 6d 65 29 2e 20 20 57 72 69  ated Time).  Wri
e9710 74 65 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e  te the.** curren
e9720 74 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20  t time and date 
e9730 61 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20  as a Julian Day 
e9740 6e 75 6d 62 65 72 20 69 6e 74 6f 20 2a 70 72 4e  number into *prN
e9750 6f 77 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e  ow and.** return
e9760 20 30 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66   0.  Return 1 if
e9770 20 74 68 65 20 74 69 6d 65 20 61 6e 64 20 64 61   the time and da
e9780 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75  te cannot be fou
e9790 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  nd..*/.static in
e97a0 74 20 75 6e 69 78 43 75 72 72 65 6e 74 54 69 6d  t unixCurrentTim
e97b0 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e  e(sqlite3_vfs *N
e97c0 6f 74 55 73 65 64 2c 20 64 6f 75 62 6c 65 20 2a  otUsed, double *
e97d0 70 72 4e 6f 77 29 7b 0a 23 69 66 20 64 65 66 69  prNow){.#if defi
e97e0 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
e97f0 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 29 0a  FLOATING_POINT).
e9800 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 74 69    time_t t;.  ti
e9810 6d 65 28 26 74 29 3b 0a 20 20 2a 70 72 4e 6f 77  me(&t);.  *prNow
e9820 20 3d 20 28 28 28 73 71 6c 69 74 65 33 5f 69 6e   = (((sqlite3_in
e9830 74 36 34 29 74 29 2f 38 36 34 30 20 2b 20 32 34  t64)t)/8640 + 24
e9840 34 30 35 38 37 35 29 2f 31 30 3b 0a 23 65 6c 69  405875)/10;.#eli
e9850 66 20 64 65 66 69 6e 65 64 28 4e 4f 5f 47 45 54  f defined(NO_GET
e9860 54 4f 44 29 0a 20 20 74 69 6d 65 5f 74 20 74 3b  TOD).  time_t t;
e9870 0a 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 2a  .  time(&t);.  *
e9880 70 72 4e 6f 77 20 3d 20 74 2f 38 36 34 30 30 2e  prNow = t/86400.
e9890 30 20 2b 20 32 34 34 30 35 38 37 2e 35 3b 0a 23  0 + 2440587.5;.#
e98a0 65 6c 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a  elif OS_VXWORKS.
e98b0 20 20 73 74 72 75 63 74 20 74 69 6d 65 73 70 65    struct timespe
e98c0 63 20 73 4e 6f 77 3b 0a 20 20 63 6c 6f 63 6b 5f  c sNow;.  clock_
e98d0 67 65 74 74 69 6d 65 28 43 4c 4f 43 4b 5f 52 45  gettime(CLOCK_RE
e98e0 41 4c 54 49 4d 45 2c 20 26 73 4e 6f 77 29 3b 0a  ALTIME, &sNow);.
e98f0 20 20 2a 70 72 4e 6f 77 20 3d 20 32 34 34 30 35    *prNow = 24405
e9900 38 37 2e 35 20 2b 20 73 4e 6f 77 2e 74 76 5f 73  87.5 + sNow.tv_s
e9910 65 63 2f 38 36 34 30 30 2e 30 20 2b 20 73 4e 6f  ec/86400.0 + sNo
e9920 77 2e 74 76 5f 6e 73 65 63 2f 38 36 34 30 30 30  w.tv_nsec/864000
e9930 30 30 30 30 30 30 30 30 2e 30 3b 0a 23 65 6c 73  00000000.0;.#els
e9940 65 0a 20 20 73 74 72 75 63 74 20 74 69 6d 65 76  e.  struct timev
e9950 61 6c 20 73 4e 6f 77 3b 0a 20 20 67 65 74 74 69  al sNow;.  getti
e9960 6d 65 6f 66 64 61 79 28 26 73 4e 6f 77 2c 20 30  meofday(&sNow, 0
e9970 29 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 32 34  );.  *prNow = 24
e9980 34 30 35 38 37 2e 35 20 2b 20 73 4e 6f 77 2e 74  40587.5 + sNow.t
e9990 76 5f 73 65 63 2f 38 36 34 30 30 2e 30 20 2b 20  v_sec/86400.0 + 
e99a0 73 4e 6f 77 2e 74 76 5f 75 73 65 63 2f 38 36 34  sNow.tv_usec/864
e99b0 30 30 30 30 30 30 30 30 2e 30 3b 0a 23 65 6e 64  00000000.0;.#end
e99c0 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
e99d0 45 5f 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c  E_TEST.  if( sql
e99e0 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d  ite3_current_tim
e99f0 65 20 29 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20  e ){.    *prNow 
e9a00 3d 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e  = sqlite3_curren
e9a10 74 5f 74 69 6d 65 2f 38 36 34 30 30 2e 30 20 2b  t_time/86400.0 +
e9a20 20 32 34 34 30 35 38 37 2e 35 3b 0a 20 20 7d 0a   2440587.5;.  }.
e9a30 23 65 6e 64 69 66 0a 20 20 55 4e 55 53 45 44 5f  #endif.  UNUSED_
e9a40 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
e9a50 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a  d);.  return 0;.
e9a60 7d 0a 0a 2f 2a 0a 2a 2a 20 57 65 20 61 64 64 65  }../*.** We adde
e9a70 64 20 74 68 65 20 78 47 65 74 4c 61 73 74 45 72  d the xGetLastEr
e9a80 72 6f 72 28 29 20 6d 65 74 68 6f 64 20 77 69 74  ror() method wit
e9a90 68 20 74 68 65 20 69 6e 74 65 6e 74 69 6f 6e 20  h the intention 
e9aa0 6f 66 20 70 72 6f 76 69 64 69 6e 67 0a 2a 2a 20  of providing.** 
e9ab0 62 65 74 74 65 72 20 6c 6f 77 2d 6c 65 76 65 6c  better low-level
e9ac0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20   error messages 
e9ad0 77 68 65 6e 20 6f 70 65 72 61 74 69 6e 67 2d 73  when operating-s
e9ae0 79 73 74 65 6d 20 70 72 6f 62 6c 65 6d 73 20 63  ystem problems c
e9af0 6f 6d 65 20 75 70 0a 2a 2a 20 64 75 72 69 6e 67  ome up.** during
e9b00 20 53 51 4c 69 74 65 20 6f 70 65 72 61 74 69 6f   SQLite operatio
e9b10 6e 2e 20 20 42 75 74 20 73 6f 20 66 61 72 2c 20  n.  But so far, 
e9b20 6e 6f 6e 65 20 6f 66 20 74 68 61 74 20 68 61 73  none of that has
e9b30 20 62 65 65 6e 20 69 6d 70 6c 65 6d 65 6e 74 65   been implemente
e9b40 64 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f 72 65  d.** in the core
e9b50 2e 20 20 53 6f 20 74 68 69 73 20 72 6f 75 74 69  .  So this routi
e9b60 6e 65 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c  ne is never call
e9b70 65 64 2e 20 20 46 6f 72 20 6e 6f 77 2c 20 69 74  ed.  For now, it
e9b80 20 69 73 20 6d 65 72 65 6c 79 0a 2a 2a 20 61 20   is merely.** a 
e9b90 70 6c 61 63 65 2d 68 6f 6c 64 65 72 2e 0a 2a 2f  place-holder..*/
e9ba0 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78  .static int unix
e9bb0 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c  GetLastError(sql
e9bc0 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65  ite3_vfs *NotUse
e9bd0 64 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64 32 2c  d, int NotUsed2,
e9be0 20 63 68 61 72 20 2a 4e 6f 74 55 73 65 64 33 29   char *NotUsed3)
e9bf0 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  {.  UNUSED_PARAM
e9c00 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20  ETER(NotUsed);. 
e9c10 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
e9c20 52 28 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 55  R(NotUsed2);.  U
e9c30 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
e9c40 4e 6f 74 55 73 65 64 33 29 3b 0a 20 20 72 65 74  NotUsed3);.  ret
e9c50 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 2a  urn 0;.}../*.***
e9c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9c70 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c  ***** End of sql
e9c80 69 74 65 33 5f 76 66 73 20 6d 65 74 68 6f 64 73  ite3_vfs methods
e9c90 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
e9ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a  ************.***
e9cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a  ***********/../*
e9d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
e9d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9d60 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 50  ******** Begin P
e9d70 72 6f 78 79 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a  roxy Locking ***
e9d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e9d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
e9da0 0a 2a 2a 20 50 72 6f 78 79 20 6c 6f 63 6b 69 6e  .** Proxy lockin
e9db0 67 20 69 73 20 61 20 22 75 62 65 72 2d 6c 6f 63  g is a "uber-loc
e9dc0 6b 69 6e 67 2d 6d 65 74 68 6f 64 22 20 69 6e 20  king-method" in 
e9dd0 74 68 69 73 20 73 65 6e 73 65 3a 20 20 49 74 20  this sense:  It 
e9de0 75 73 65 73 20 74 68 65 0a 2a 2a 20 6f 74 68 65  uses the.** othe
e9df0 72 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64  r locking method
e9e00 73 20 6f 6e 20 73 65 63 6f 6e 64 61 72 79 20 6c  s on secondary l
e9e10 6f 63 6b 20 66 69 6c 65 73 2e 20 20 50 72 6f 78  ock files.  Prox
e9e20 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 61 0a 2a  y locking is a.*
e9e30 2a 20 6d 65 74 61 2d 6c 61 79 65 72 20 6f 76 65  * meta-layer ove
e9e40 72 20 74 6f 70 20 6f 66 20 74 68 65 20 70 72 69  r top of the pri
e9e50 6d 69 74 69 76 65 20 6c 6f 63 6b 69 6e 67 20 69  mitive locking i
e9e60 6d 70 6c 65 6d 65 6e 74 65 64 20 61 62 6f 76 65  mplemented above
e9e70 2e 20 20 46 6f 72 0a 2a 2a 20 74 68 69 73 20 72  .  For.** this r
e9e80 65 61 73 6f 6e 2c 20 74 68 65 20 64 69 76 69 73  eason, the divis
e9e90 69 6f 6e 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  ion that impleme
e9ea0 6e 74 73 20 6f 66 20 70 72 6f 78 79 20 6c 6f 63  nts of proxy loc
e9eb0 6b 69 6e 67 20 69 73 20 64 65 66 65 72 72 65 64  king is deferred
e9ec0 0a 2a 2a 20 75 6e 74 69 6c 20 6c 61 74 65 20 69  .** until late i
e9ed0 6e 20 74 68 65 20 66 69 6c 65 20 28 68 65 72 65  n the file (here
e9ee0 29 20 61 66 74 65 72 20 61 6c 6c 20 6f 66 20 74  ) after all of t
e9ef0 68 65 20 6f 74 68 65 72 20 49 2f 4f 20 6d 65 74  he other I/O met
e9f00 68 6f 64 73 20 68 61 76 65 0a 2a 2a 20 62 65 65  hods have.** bee
e9f10 6e 20 64 65 66 69 6e 65 64 20 2d 20 73 6f 20 74  n defined - so t
e9f20 68 61 74 20 74 68 65 20 70 72 69 6d 69 74 69 76  hat the primitiv
e9f30 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64  e locking method
e9f40 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 0a  s are available.
e9f50 2a 2a 20 61 73 20 73 65 72 76 69 63 65 73 20 74  ** as services t
e9f60 6f 20 68 65 6c 70 20 77 69 74 68 20 74 68 65 20  o help with the 
e9f70 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
e9f80 66 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2e  f proxy locking.
e9f90 0a 2a 2a 0a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  .**.****.**.** T
e9fa0 68 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69  he default locki
e9fb0 6e 67 20 73 63 68 65 6d 65 73 20 69 6e 20 53 51  ng schemes in SQ
e9fc0 4c 69 74 65 20 75 73 65 20 62 79 74 65 2d 72 61  Lite use byte-ra
e9fd0 6e 67 65 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65  nge locks on the
e9fe0 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c  .** database fil
e9ff0 65 20 74 6f 20 63 6f 6f 72 64 69 6e 61 74 65 20  e to coordinate 
ea000 73 61 66 65 2c 20 63 6f 6e 63 75 72 72 65 6e 74  safe, concurrent
ea010 20 61 63 63 65 73 73 20 62 79 20 6d 75 6c 74 69   access by multi
ea020 70 6c 65 20 72 65 61 64 65 72 73 0a 2a 2a 20 61  ple readers.** a
ea030 6e 64 20 77 72 69 74 65 72 73 20 5b 68 74 74 70  nd writers [http
ea040 3a 2f 2f 73 71 6c 69 74 65 2e 6f 72 67 2f 6c 6f  ://sqlite.org/lo
ea050 63 6b 69 6e 67 76 33 2e 68 74 6d 6c 5d 2e 20 20  ckingv3.html].  
ea060 54 68 65 20 66 69 76 65 20 66 69 6c 65 20 6c 6f  The five file lo
ea070 63 6b 69 6e 67 0a 2a 2a 20 73 74 61 74 65 73 20  cking.** states 
ea080 28 55 4e 4c 4f 43 4b 45 44 2c 20 50 45 4e 44 49  (UNLOCKED, PENDI
ea090 4e 47 2c 20 53 48 41 52 45 44 2c 20 52 45 53 45  NG, SHARED, RESE
ea0a0 52 56 45 44 2c 20 45 58 43 4c 55 53 49 56 45 29  RVED, EXCLUSIVE)
ea0b0 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64   are implemented
ea0c0 0a 2a 2a 20 61 73 20 50 4f 53 49 58 20 72 65 61  .** as POSIX rea
ea0d0 64 20 26 20 77 72 69 74 65 20 6c 6f 63 6b 73 20  d & write locks 
ea0e0 6f 76 65 72 20 66 69 78 65 64 20 73 65 74 20 6f  over fixed set o
ea0f0 66 20 6c 6f 63 61 74 69 6f 6e 73 20 28 76 69 61  f locations (via
ea100 20 66 73 63 74 6c 29 2c 0a 2a 2a 20 6f 6e 20 41   fsctl),.** on A
ea110 46 50 20 61 6e 64 20 53 4d 42 20 6f 6e 6c 79 20  FP and SMB only 
ea120 65 78 63 6c 75 73 69 76 65 20 62 79 74 65 2d 72  exclusive byte-r
ea130 61 6e 67 65 20 6c 6f 63 6b 73 20 61 72 65 20 61  ange locks are a
ea140 76 61 69 6c 61 62 6c 65 20 76 69 61 20 66 73 63  vailable via fsc
ea150 74 6c 0a 2a 2a 20 77 69 74 68 20 5f 49 4f 57 52  tl.** with _IOWR
ea160 28 27 7a 27 2c 20 32 33 2c 20 73 74 72 75 63 74  ('z', 23, struct
ea170 20 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42   ByteRangeLockPB
ea180 32 29 20 74 6f 20 74 72 61 63 6b 20 74 68 65 20  2) to track the 
ea190 73 61 6d 65 20 35 20 73 74 61 74 65 73 2e 0a 2a  same 5 states..*
ea1a0 2a 20 54 6f 20 73 69 6d 75 6c 61 74 65 20 61 20  * To simulate a 
ea1b0 46 5f 52 44 4c 43 4b 20 6f 6e 20 74 68 65 20 73  F_RDLCK on the s
ea1c0 68 61 72 65 64 20 72 61 6e 67 65 2c 20 6f 6e 20  hared range, on 
ea1d0 41 46 50 20 61 20 72 61 6e 64 6f 6d 6c 79 20 73  AFP a randomly s
ea1e0 65 6c 65 63 74 65 64 0a 2a 2a 20 61 64 64 72 65  elected.** addre
ea1f0 73 73 20 69 6e 20 74 68 65 20 73 68 61 72 65 64  ss in the shared
ea200 20 72 61 6e 67 65 20 69 73 20 74 61 6b 65 6e 20   range is taken 
ea210 66 6f 72 20 61 20 53 48 41 52 45 44 20 6c 6f 63  for a SHARED loc
ea220 6b 2c 20 74 68 65 20 65 6e 74 69 72 65 0a 2a 2a  k, the entire.**
ea230 20 73 68 61 72 65 64 20 72 61 6e 67 65 20 69 73   shared range is
ea240 20 74 61 6b 65 6e 20 66 6f 72 20 61 6e 20 45 58   taken for an EX
ea250 43 4c 55 53 49 56 45 20 6c 6f 63 6b 29 3a 0a 2a  CLUSIVE lock):.*
ea260 2a 0a 2a 2a 20 20 20 20 20 20 50 45 4e 44 49 4e  *.**      PENDIN
ea270 47 5f 42 59 54 45 20 20 20 20 20 20 20 20 30 78  G_BYTE        0x
ea280 34 30 30 30 30 30 30 30 09 09 20 20 20 09 0a 2a  40000000..   ..*
ea290 2a 20 20 20 20 20 20 52 45 53 45 52 56 45 44 5f  *      RESERVED_
ea2a0 42 59 54 45 20 20 20 20 20 20 20 30 78 34 30 30  BYTE       0x400
ea2b0 30 30 30 30 31 0a 2a 2a 20 20 20 20 20 20 53 48  00001.**      SH
ea2c0 41 52 45 44 5f 52 41 4e 47 45 20 20 20 20 20 20  ARED_RANGE      
ea2d0 20 20 30 78 34 30 30 30 30 30 30 32 20 2d 3e 20    0x40000002 -> 
ea2e0 30 78 34 30 30 30 30 32 30 30 0a 2a 2a 0a 2a 2a  0x40000200.**.**
ea2f0 20 54 68 69 73 20 77 6f 72 6b 73 20 77 65 6c 6c   This works well
ea300 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 66 69   on the local fi
ea310 6c 65 20 73 79 73 74 65 6d 2c 20 62 75 74 20 73  le system, but s
ea320 68 6f 77 73 20 61 20 6e 65 61 72 6c 79 20 31 30  hows a nearly 10
ea330 30 78 0a 2a 2a 20 73 6c 6f 77 64 6f 77 6e 20 69  0x.** slowdown i
ea340 6e 20 72 65 61 64 20 70 65 72 66 6f 72 6d 61 6e  n read performan
ea350 63 65 20 6f 6e 20 41 46 50 20 62 65 63 61 75 73  ce on AFP becaus
ea360 65 20 74 68 65 20 41 46 50 20 63 6c 69 65 6e 74  e the AFP client
ea370 20 64 69 73 61 62 6c 65 73 0a 2a 2a 20 74 68 65   disables.** the
ea380 20 72 65 61 64 20 63 61 63 68 65 20 77 68 65 6e   read cache when
ea390 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b   byte-range lock
ea3a0 73 20 61 72 65 20 70 72 65 73 65 6e 74 2e 20 20  s are present.  
ea3b0 45 6e 61 62 6c 69 6e 67 20 74 68 65 20 72 65 61  Enabling the rea
ea3c0 64 0a 2a 2a 20 63 61 63 68 65 20 65 78 70 6f 73  d.** cache expos
ea3d0 65 73 20 61 20 63 61 63 68 65 20 63 6f 68 65 72  es a cache coher
ea3e0 65 6e 63 79 20 70 72 6f 62 6c 65 6d 20 74 68 61  ency problem tha
ea3f0 74 20 69 73 20 70 72 65 73 65 6e 74 20 6f 6e 20  t is present on 
ea400 61 6c 6c 20 4f 53 20 58 0a 2a 2a 20 73 75 70 70  all OS X.** supp
ea410 6f 72 74 65 64 20 6e 65 74 77 6f 72 6b 20 66 69  orted network fi
ea420 6c 65 20 73 79 73 74 65 6d 73 2e 20 20 4e 46 53  le systems.  NFS
ea430 20 61 6e 64 20 41 46 50 20 62 6f 74 68 20 6f 62   and AFP both ob
ea440 73 65 72 76 65 20 74 68 65 0a 2a 2a 20 63 6c 6f  serve the.** clo
ea450 73 65 2d 74 6f 2d 6f 70 65 6e 20 73 65 6d 61 6e  se-to-open seman
ea460 74 69 63 73 20 66 6f 72 20 65 6e 73 75 72 69 6e  tics for ensurin
ea470 67 20 63 61 63 68 65 20 63 6f 68 65 72 65 6e 63  g cache coherenc
ea480 79 0a 2a 2a 20 5b 68 74 74 70 3a 2f 2f 6e 66 73  y.** [http://nfs
ea490 2e 73 6f 75 72 63 65 66 6f 72 67 65 2e 6e 65 74  .sourceforge.net
ea4a0 2f 23 66 61 71 5f 61 38 5d 2c 20 77 68 69 63 68  /#faq_a8], which
ea4b0 20 64 6f 65 73 20 6e 6f 74 20 65 66 66 65 63 74   does not effect
ea4c0 69 76 65 6c 79 0a 2a 2a 20 61 64 64 72 65 73 73  ively.** address
ea4d0 20 74 68 65 20 72 65 71 75 69 72 65 6d 65 6e 74   the requirement
ea4e0 73 20 66 6f 72 20 63 6f 6e 63 75 72 72 65 6e 74  s for concurrent
ea4f0 20 64 61 74 61 62 61 73 65 20 61 63 63 65 73 73   database access
ea500 20 62 79 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20   by multiple.** 
ea510 72 65 61 64 65 72 73 20 61 6e 64 20 77 72 69 74  readers and writ
ea520 65 72 73 0a 2a 2a 20 5b 68 74 74 70 3a 2f 2f 77  ers.** [http://w
ea530 77 77 2e 6e 61 62 62 6c 65 2e 63 6f 6d 2f 53 51  ww.nabble.com/SQ
ea540 4c 69 74 65 2d 6f 6e 2d 4e 46 53 2d 63 61 63 68  Lite-on-NFS-cach
ea550 65 2d 63 6f 68 65 72 65 6e 63 79 2d 74 64 31 35  e-coherency-td15
ea560 36 35 35 37 30 31 2e 68 74 6d 6c 5d 2e 0a 2a 2a  655701.html]..**
ea570 0a 2a 2a 20 54 6f 20 61 64 64 72 65 73 73 20 74  .** To address t
ea580 68 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 61  he performance a
ea590 6e 64 20 63 61 63 68 65 20 63 6f 68 65 72 65 6e  nd cache coheren
ea5a0 63 79 20 69 73 73 75 65 73 2c 20 70 72 6f 78 79  cy issues, proxy
ea5b0 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a   file locking.**
ea5c0 20 63 68 61 6e 67 65 73 20 74 68 65 20 77 61 79   changes the way
ea5d0 20 64 61 74 61 62 61 73 65 20 61 63 63 65 73 73   database access
ea5e0 20 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62   is controlled b
ea5f0 79 20 6c 69 6d 69 74 69 6e 67 20 61 63 63 65 73  y limiting acces
ea600 73 20 74 6f 20 61 0a 2a 2a 20 73 69 6e 67 6c 65  s to a.** single
ea610 20 68 6f 73 74 20 61 74 20 61 20 74 69 6d 65 20   host at a time 
ea620 61 6e 64 20 6d 6f 76 69 6e 67 20 66 69 6c 65 20  and moving file 
ea630 6c 6f 63 6b 73 20 6f 66 66 20 6f 66 20 74 68 65  locks off of the
ea640 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a   database file.*
ea650 2a 20 61 6e 64 20 6f 6e 74 6f 20 61 20 70 72 6f  * and onto a pro
ea660 78 79 20 66 69 6c 65 20 6f 6e 20 74 68 65 20 6c  xy file on the l
ea670 6f 63 61 6c 20 66 69 6c 65 20 73 79 73 74 65 6d  ocal file system
ea680 2e 20 20 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 55 73 69  .  .**.**.** Usi
ea690 6e 67 20 70 72 6f 78 79 20 6c 6f 63 6b 73 0a 2a  ng proxy locks.*
ea6a0 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  * --------------
ea6b0 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 43 20 41 50 49 73  ---.**.** C APIs
ea6c0 0a 2a 2a 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f  .**.**  sqlite3_
ea6d0 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64 62 2c  file_control(db,
ea6e0 20 64 62 6e 61 6d 65 2c 20 53 51 4c 49 54 45 5f   dbname, SQLITE_
ea6f0 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c  SET_LOCKPROXYFIL
ea700 45 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  E,.**           
ea710 20 20 20 20 20 20 20 20 20 20 20 20 3c 70 72 6f              <pro
ea720 78 79 5f 70 61 74 68 3e 20 7c 20 22 3a 61 75 74  xy_path> | ":aut
ea730 6f 3a 22 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65  o:");.**  sqlite
ea740 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64  3_file_control(d
ea750 62 2c 20 64 62 6e 61 6d 65 2c 20 53 51 4c 49 54  b, dbname, SQLIT
ea760 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46  E_GET_LOCKPROXYF
ea770 49 4c 45 2c 20 26 3c 70 72 6f 78 79 5f 70 61 74  ILE, &<proxy_pat
ea780 68 3e 29 3b 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 53 51  h>);.**.**.** SQ
ea790 4c 20 70 72 61 67 6d 61 73 0a 2a 2a 0a 2a 2a 20  L pragmas.**.** 
ea7a0 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
ea7b0 65 2e 5d 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69  e.]lock_proxy_fi
ea7c0 6c 65 3d 3c 70 72 6f 78 79 5f 70 61 74 68 3e 20  le=<proxy_path> 
ea7d0 7c 20 3a 61 75 74 6f 3a 0a 2a 2a 20 20 50 52 41  | :auto:.**  PRA
ea7e0 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c  GMA [database.]l
ea7f0 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 0a 2a  ock_proxy_file.*
ea800 2a 0a 2a 2a 20 53 70 65 63 69 66 79 69 6e 67 20  *.** Specifying 
ea810 22 3a 61 75 74 6f 3a 22 20 6d 65 61 6e 73 20 74  ":auto:" means t
ea820 68 61 74 20 69 66 20 74 68 65 72 65 20 69 73 20  hat if there is 
ea830 61 20 63 6f 6e 63 68 20 66 69 6c 65 20 77 69 74  a conch file wit
ea840 68 20 61 20 6d 61 74 63 68 69 6e 67 0a 2a 2a 20  h a matching.** 
ea850 68 6f 73 74 20 49 44 20 69 6e 20 69 74 2c 20 74  host ID in it, t
ea860 68 65 20 70 72 6f 78 79 20 70 61 74 68 20 69 6e  he proxy path in
ea870 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20   the conch file 
ea880 77 69 6c 6c 20 62 65 20 75 73 65 64 2c 20 6f 74  will be used, ot
ea890 68 65 72 77 69 73 65 0a 2a 2a 20 61 20 70 72 6f  herwise.** a pro
ea8a0 78 79 20 70 61 74 68 20 62 61 73 65 64 20 6f 6e  xy path based on
ea8b0 20 74 68 65 20 75 73 65 72 27 73 20 74 65 6d 70   the user's temp
ea8c0 20 64 69 72 0a 2a 2a 20 28 76 69 61 20 63 6f 6e   dir.** (via con
ea8d0 66 73 74 72 28 5f 43 53 5f 44 41 52 57 49 4e 5f  fstr(_CS_DARWIN_
ea8e0 55 53 45 52 5f 54 45 4d 50 5f 44 49 52 2c 2e 2e  USER_TEMP_DIR,..
ea8f0 2e 29 29 20 77 69 6c 6c 20 62 65 20 75 73 65 64  .)) will be used
ea900 20 61 6e 64 20 74 68 65 0a 2a 2a 20 61 63 74 75   and the.** actu
ea910 61 6c 20 70 72 6f 78 79 20 66 69 6c 65 20 6e 61  al proxy file na
ea920 6d 65 20 69 73 20 67 65 6e 65 72 61 74 65 64 20  me is generated 
ea930 66 72 6f 6d 20 74 68 65 20 6e 61 6d 65 20 61 6e  from the name an
ea940 64 20 70 61 74 68 20 6f 66 20 74 68 65 0a 2a 2a  d path of the.**
ea950 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
ea960 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a   For example:.**
ea970 0a 2a 2a 20 20 20 20 20 20 20 46 6f 72 20 64 61  .**       For da
ea980 74 61 62 61 73 65 20 70 61 74 68 20 22 2f 55 73  tabase path "/Us
ea990 65 72 73 2f 6d 65 2f 66 6f 6f 2e 64 62 22 20 0a  ers/me/foo.db" .
ea9a0 2a 2a 20 20 20 20 20 20 20 54 68 65 20 6c 6f 63  **       The loc
ea9b0 6b 20 70 61 74 68 20 77 69 6c 6c 20 62 65 20 22  k path will be "
ea9c0 3c 74 6d 70 64 69 72 3e 2f 73 71 6c 69 74 65 70  <tmpdir>/sqlitep
ea9d0 6c 6f 63 6b 73 2f 5f 55 73 65 72 73 5f 6d 65 5f  locks/_Users_me_
ea9e0 66 6f 6f 2e 64 62 3a 61 75 74 6f 3a 22 29 0a 2a  foo.db:auto:").*
ea9f0 2a 0a 2a 2a 20 4f 6e 63 65 20 61 20 6c 6f 63 6b  *.** Once a lock
eaa00 20 70 72 6f 78 79 20 69 73 20 63 6f 6e 66 69 67   proxy is config
eaa10 75 72 65 64 20 66 6f 72 20 61 20 64 61 74 61 62  ured for a datab
eaa20 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20  ase connection, 
eaa30 69 74 20 63 61 6e 20 6e 6f 74 0a 2a 2a 20 62 65  it can not.** be
eaa40 20 72 65 6d 6f 76 65 64 2c 20 68 6f 77 65 76 65   removed, howeve
eaa50 72 20 69 74 20 6d 61 79 20 62 65 20 73 77 69 74  r it may be swit
eaa60 63 68 65 64 20 74 6f 20 61 20 64 69 66 66 65 72  ched to a differ
eaa70 65 6e 74 20 70 72 6f 78 79 20 70 61 74 68 20 76  ent proxy path v
eaa80 69 61 0a 2a 2a 20 74 68 65 20 61 62 6f 76 65 20  ia.** the above 
eaa90 41 50 49 73 20 28 61 73 73 75 6d 69 6e 67 20 74  APIs (assuming t
eaaa0 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 69 73  he conch file is
eaab0 20 6e 6f 74 20 62 65 69 6e 67 20 68 65 6c 64 20   not being held 
eaac0 62 79 20 61 6e 6f 74 68 65 72 0a 2a 2a 20 63 6f  by another.** co
eaad0 6e 6e 65 63 74 69 6f 6e 20 6f 72 20 70 72 6f 63  nnection or proc
eaae0 65 73 73 29 2e 20 0a 2a 2a 0a 2a 2a 0a 2a 2a 20  ess). .**.**.** 
eaaf0 48 6f 77 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e  How proxy lockin
eab00 67 20 77 6f 72 6b 73 0a 2a 2a 20 2d 2d 2d 2d 2d  g works.** -----
eab10 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
eab20 2d 2d 0a 2a 2a 0a 2a 2a 20 50 72 6f 78 79 20 66  --.**.** Proxy f
eab30 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 72 65 6c 69  ile locking reli
eab40 65 73 20 70 72 69 6d 61 72 69 6c 79 20 6f 6e 20  es primarily on 
eab50 74 77 6f 20 6e 65 77 20 73 75 70 70 6f 72 74 69  two new supporti
eab60 6e 67 20 66 69 6c 65 73 3a 20 0a 2a 2a 0a 2a 2a  ng files: .**.**
eab70 20 20 20 2a 20 20 63 6f 6e 63 68 20 66 69 6c 65     *  conch file
eab80 20 74 6f 20 6c 69 6d 69 74 20 61 63 63 65 73 73   to limit access
eab90 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
eaba0 20 66 69 6c 65 20 74 6f 20 61 20 73 69 6e 67 6c   file to a singl
eabb0 65 20 68 6f 73 74 0a 2a 2a 20 20 20 20 20 20 61  e host.**      a
eabc0 74 20 61 20 74 69 6d 65 0a 2a 2a 0a 2a 2a 20 20  t a time.**.**  
eabd0 20 2a 20 20 70 72 6f 78 79 20 66 69 6c 65 20 74   *  proxy file t
eabe0 6f 20 61 63 74 20 61 73 20 61 20 70 72 6f 78 79  o act as a proxy
eabf0 20 66 6f 72 20 74 68 65 20 61 64 76 69 73 6f 72   for the advisor
eac00 79 20 6c 6f 63 6b 73 20 6e 6f 72 6d 61 6c 6c 79  y locks normally
eac10 0a 2a 2a 20 20 20 20 20 20 74 61 6b 65 6e 20 6f  .**      taken o
eac20 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a  n the database.*
eac30 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 63 68 20 66  *.** The conch f
eac40 69 6c 65 20 2d 20 74 6f 20 75 73 65 20 61 20 70  ile - to use a p
eac50 72 6f 78 79 20 66 69 6c 65 2c 20 73 71 6c 69 74  roxy file, sqlit
eac60 65 20 6d 75 73 74 20 66 69 72 73 74 20 22 68 6f  e must first "ho
eac70 6c 64 20 74 68 65 20 63 6f 6e 63 68 22 0a 2a 2a  ld the conch".**
eac80 20 62 79 20 74 61 6b 69 6e 67 20 61 6e 20 73 71   by taking an sq
eac90 6c 69 74 65 2d 73 74 79 6c 65 20 73 68 61 72 65  lite-style share
eaca0 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 63 6f  d lock on the co
eacb0 6e 63 68 20 66 69 6c 65 2c 20 72 65 61 64 69 6e  nch file, readin
eacc0 67 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74  g the.** content
eacd0 73 20 61 6e 64 20 63 6f 6d 70 61 72 69 6e 67 20  s and comparing 
eace0 74 68 65 20 68 6f 73 74 27 73 20 75 6e 69 71 75  the host's uniqu
eacf0 65 20 68 6f 73 74 20 49 44 20 28 73 65 65 20 62  e host ID (see b
ead00 65 6c 6f 77 29 20 61 6e 64 20 6c 6f 63 6b 0a 2a  elow) and lock.*
ead10 2a 20 70 72 6f 78 79 20 70 61 74 68 20 61 67 61  * proxy path aga
ead20 69 6e 73 74 20 74 68 65 20 76 61 6c 75 65 73 20  inst the values 
ead30 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 6f  stored in the co
ead40 6e 63 68 2e 20 20 54 68 65 20 63 6f 6e 63 68 20  nch.  The conch 
ead50 66 69 6c 65 20 69 73 0a 2a 2a 20 73 74 6f 72 65  file is.** store
ead60 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 69  d in the same di
ead70 72 65 63 74 6f 72 79 20 61 73 20 74 68 65 20 64  rectory as the d
ead80 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64  atabase file and
ead90 20 74 68 65 20 66 69 6c 65 20 6e 61 6d 65 0a 2a   the file name.*
eada0 2a 20 69 73 20 70 61 74 74 65 72 6e 65 64 20 61  * is patterned a
eadb0 66 74 65 72 20 74 68 65 20 64 61 74 61 62 61 73  fter the databas
eadc0 65 20 66 69 6c 65 20 6e 61 6d 65 20 61 73 20 22  e file name as "
eadd0 2e 3c 64 61 74 61 62 61 73 65 6e 61 6d 65 3e 2d  .<databasename>-
eade0 63 6f 6e 63 68 22 2e 0a 2a 2a 20 49 66 20 74 68  conch"..** If th
eadf0 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 64 6f 65  e conch file doe
eae00 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 6f 72 20  s not exist, or 
eae10 69 74 27 73 20 63 6f 6e 74 65 6e 74 73 20 64 6f  it's contents do
eae20 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 0a 2a   not match the.*
eae30 2a 20 68 6f 73 74 20 49 44 20 61 6e 64 2f 6f 72  * host ID and/or
eae40 20 70 72 6f 78 79 20 70 61 74 68 2c 20 74 68 65   proxy path, the
eae50 6e 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 65 73  n the lock is es
eae60 63 61 6c 61 74 65 64 20 74 6f 20 61 6e 20 65 78  calated to an ex
eae70 63 6c 75 73 69 76 65 0a 2a 2a 20 6c 6f 63 6b 20  clusive.** lock 
eae80 61 6e 64 20 74 68 65 20 63 6f 6e 63 68 20 66 69  and the conch fi
eae90 6c 65 20 63 6f 6e 74 65 6e 74 73 20 69 73 20 75  le contents is u
eaea0 70 64 61 74 65 64 20 77 69 74 68 20 74 68 65 20  pdated with the 
eaeb0 68 6f 73 74 20 49 44 20 61 6e 64 20 70 72 6f 78  host ID and prox
eaec0 79 0a 2a 2a 20 70 61 74 68 20 61 6e 64 20 74 68  y.** path and th
eaed0 65 20 6c 6f 63 6b 20 69 73 20 64 6f 77 6e 67 72  e lock is downgr
eaee0 61 64 65 64 20 74 6f 20 61 20 73 68 61 72 65 64  aded to a shared
eaef0 20 6c 6f 63 6b 20 61 67 61 69 6e 2e 20 20 49 66   lock again.  If
eaf00 20 74 68 65 20 63 6f 6e 63 68 0a 2a 2a 20 69 73   the conch.** is
eaf10 20 68 65 6c 64 20 62 79 20 61 6e 6f 74 68 65 72   held by another
eaf20 20 70 72 6f 63 65 73 73 20 28 77 69 74 68 20 61   process (with a
eaf30 20 73 68 61 72 65 64 20 6c 6f 63 6b 29 2c 20 74   shared lock), t
eaf40 68 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63  he exclusive loc
eaf50 6b 0a 2a 2a 20 77 69 6c 6c 20 66 61 69 6c 20 61  k.** will fail a
eaf60 6e 64 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69  nd SQLITE_BUSY i
eaf70 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
eaf80 2a 20 54 68 65 20 70 72 6f 78 79 20 66 69 6c 65  * The proxy file
eaf90 20 2d 20 61 20 73 69 6e 67 6c 65 2d 62 79 74 65   - a single-byte
eafa0 20 66 69 6c 65 20 75 73 65 64 20 66 6f 72 20 61   file used for a
eafb0 6c 6c 20 61 64 76 69 73 6f 72 79 20 66 69 6c 65  ll advisory file
eafc0 20 6c 6f 63 6b 73 0a 2a 2a 20 6e 6f 72 6d 61 6c   locks.** normal
eafd0 6c 79 20 74 61 6b 65 6e 20 6f 6e 20 74 68 65 20  ly taken on the 
eafe0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20  database file.  
eaff0 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 66 6f 72   This allows for
eb000 20 73 61 66 65 20 73 68 61 72 69 6e 67 0a 2a 2a   safe sharing.**
eb010 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
eb020 20 66 69 6c 65 20 66 6f 72 20 6d 75 6c 74 69 70   file for multip
eb030 6c 65 20 72 65 61 64 65 72 73 20 61 6e 64 20 77  le readers and w
eb040 72 69 74 65 72 73 20 6f 6e 20 74 68 65 20 73 61  riters on the sa
eb050 6d 65 0a 2a 2a 20 68 6f 73 74 20 28 74 68 65 20  me.** host (the 
eb060 63 6f 6e 63 68 20 65 6e 73 75 72 65 73 20 74 68  conch ensures th
eb070 61 74 20 74 68 65 79 20 61 6c 6c 20 75 73 65 20  at they all use 
eb080 74 68 65 20 73 61 6d 65 20 6c 6f 63 61 6c 20 6c  the same local l
eb090 6f 63 6b 20 66 69 6c 65 29 2e 0a 2a 2a 0a 2a 2a  ock file)..**.**
eb0a0 20 54 68 65 72 65 20 69 73 20 61 20 74 68 69 72   There is a thir
eb0b0 64 20 66 69 6c 65 20 2d 20 74 68 65 20 68 6f 73  d file - the hos
eb0c0 74 20 49 44 20 66 69 6c 65 20 2d 20 75 73 65 64  t ID file - used
eb0d0 20 61 73 20 61 20 70 65 72 73 69 73 74 65 6e 74   as a persistent
eb0e0 20 72 65 63 6f 72 64 0a 2a 2a 20 6f 66 20 61 20   record.** of a 
eb0f0 75 6e 69 71 75 65 20 69 64 65 6e 74 69 66 69 65  unique identifie
eb100 72 20 66 6f 72 20 74 68 65 20 68 6f 73 74 2c 20  r for the host, 
eb110 61 20 31 32 38 2d 62 79 74 65 20 75 6e 69 71 75  a 128-byte uniqu
eb120 65 20 68 6f 73 74 20 69 64 20 66 69 6c 65 0a 2a  e host id file.*
eb130 2a 20 69 6e 20 74 68 65 20 70 61 74 68 20 64 65  * in the path de
eb140 66 69 6e 65 64 20 62 79 20 74 68 65 20 48 4f 53  fined by the HOS
eb150 54 49 44 50 41 54 48 20 6d 61 63 72 6f 20 28 64  TIDPATH macro (d
eb160 65 66 61 75 6c 74 20 76 61 6c 75 65 20 69 73 0a  efault value is.
eb170 2a 2a 20 2f 4c 69 62 72 61 72 79 2f 43 61 63 68  ** /Library/Cach
eb180 65 73 2f 2e 63 6f 6d 2e 61 70 70 6c 65 2e 73 71  es/.com.apple.sq
eb190 6c 69 74 65 43 6f 6e 63 68 48 6f 73 74 49 64 29  liteConchHostId)
eb1a0 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 65 73 74 69  ..**.** Requesti
eb1b0 6e 67 20 74 68 65 20 6c 6f 63 6b 20 70 72 6f 78  ng the lock prox
eb1c0 79 20 64 6f 65 73 20 6e 6f 74 20 69 6d 6d 65 64  y does not immed
eb1d0 69 61 74 65 6c 79 20 74 61 6b 65 20 74 68 65 20  iately take the 
eb1e0 63 6f 6e 63 68 2c 20 69 74 20 69 73 0a 2a 2a 20  conch, it is.** 
eb1f0 6f 6e 6c 79 20 74 61 6b 65 6e 20 77 68 65 6e 20  only taken when 
eb200 74 68 65 20 66 69 72 73 74 20 72 65 71 75 65 73  the first reques
eb210 74 20 74 6f 20 6c 6f 63 6b 20 64 61 74 61 62 61  t to lock databa
eb220 73 65 20 66 69 6c 65 20 69 73 20 6d 61 64 65 2e  se file is made.
eb230 20 20 0a 2a 2a 20 54 68 69 73 20 6d 61 74 63 68    .** This match
eb240 65 73 20 74 68 65 20 73 65 6d 61 6e 74 69 63 73  es the semantics
eb250 20 6f 66 20 74 68 65 20 74 72 61 64 69 74 69 6f   of the traditio
eb260 6e 61 6c 20 6c 6f 63 6b 69 6e 67 20 62 65 68 61  nal locking beha
eb270 76 69 6f 72 2c 20 77 68 65 72 65 0a 2a 2a 20 6f  vior, where.** o
eb280 70 65 6e 69 6e 67 20 61 20 63 6f 6e 6e 65 63 74  pening a connect
eb290 69 6f 6e 20 74 6f 20 61 20 64 61 74 61 62 61 73  ion to a databas
eb2a0 65 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20  e file does not 
eb2b0 74 61 6b 65 20 61 20 6c 6f 63 6b 20 6f 6e 20 69  take a lock on i
eb2c0 74 2e 0a 2a 2a 20 54 68 65 20 73 68 61 72 65 64  t..** The shared
eb2d0 20 6c 6f 63 6b 20 61 6e 64 20 61 6e 20 6f 70 65   lock and an ope
eb2e0 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  n file descripto
eb2f0 72 20 61 72 65 20 6d 61 69 6e 74 61 69 6e 65 64  r are maintained
eb300 20 75 6e 74 69 6c 20 0a 2a 2a 20 74 68 65 20 63   until .** the c
eb310 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 74 68 65  onnection to the
eb320 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6c 6f   database is clo
eb330 73 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20  sed. .**.** The 
eb340 70 72 6f 78 79 20 66 69 6c 65 20 61 6e 64 20 74  proxy file and t
eb350 68 65 20 6c 6f 63 6b 20 66 69 6c 65 20 61 72 65  he lock file are
eb360 20 6e 65 76 65 72 20 64 65 6c 65 74 65 64 20 73   never deleted s
eb370 6f 20 74 68 65 79 20 6f 6e 6c 79 20 6e 65 65 64  o they only need
eb380 0a 2a 2a 20 74 6f 20 62 65 20 63 72 65 61 74 65  .** to be create
eb390 64 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65  d the first time
eb3a0 20 74 68 65 79 20 61 72 65 20 75 73 65 64 2e 0a   they are used..
eb3b0 2a 2a 0a 2a 2a 20 43 6f 6e 66 69 67 75 72 61 74  **.** Configurat
eb3c0 69 6f 6e 20 6f 70 74 69 6f 6e 73 0a 2a 2a 20 2d  ion options.** -
eb3d0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
eb3e0 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 20 53 51 4c 49  ----.**.**  SQLI
eb3f0 54 45 5f 50 52 45 46 45 52 5f 50 52 4f 58 59 5f  TE_PREFER_PROXY_
eb400 4c 4f 43 4b 49 4e 47 0a 2a 2a 0a 2a 2a 20 20 20  LOCKING.**.**   
eb410 20 20 20 20 44 61 74 61 62 61 73 65 20 66 69 6c      Database fil
eb420 65 73 20 61 63 63 65 73 73 65 64 20 6f 6e 20 6e  es accessed on n
eb430 6f 6e 2d 6c 6f 63 61 6c 20 66 69 6c 65 20 73 79  on-local file sy
eb440 73 74 65 6d 73 20 61 72 65 0a 2a 2a 20 20 20 20  stems are.**    
eb450 20 20 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79     automatically
eb460 20 63 6f 6e 66 69 67 75 72 65 64 20 66 6f 72 20   configured for 
eb470 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2c 20 6c  proxy locking, l
eb480 6f 63 6b 20 66 69 6c 65 73 20 61 72 65 0a 2a 2a  ock files are.**
eb490 20 20 20 20 20 20 20 6e 61 6d 65 64 20 61 75 74         named aut
eb4a0 6f 6d 61 74 69 63 61 6c 6c 79 20 75 73 69 6e 67  omatically using
eb4b0 20 74 68 65 20 73 61 6d 65 20 6c 6f 67 69 63 20   the same logic 
eb4c0 61 73 0a 2a 2a 20 20 20 20 20 20 20 50 52 41 47  as.**       PRAG
eb4d0 4d 41 20 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69  MA lock_proxy_fi
eb4e0 6c 65 3d 22 3a 61 75 74 6f 3a 22 0a 2a 2a 20 20  le=":auto:".**  
eb4f0 20 20 0a 2a 2a 20 20 53 51 4c 49 54 45 5f 50 52    .**  SQLITE_PR
eb500 4f 58 59 5f 44 45 42 55 47 0a 2a 2a 0a 2a 2a 20  OXY_DEBUG.**.** 
eb510 20 20 20 20 20 20 45 6e 61 62 6c 65 73 20 74 68        Enables th
eb520 65 20 6c 6f 67 67 69 6e 67 20 6f 66 20 65 72 72  e logging of err
eb530 6f 72 20 6d 65 73 73 61 67 65 73 20 64 75 72 69  or messages duri
eb540 6e 67 20 68 6f 73 74 20 69 64 20 66 69 6c 65 0a  ng host id file.
eb550 2a 2a 20 20 20 20 20 20 20 72 65 74 72 69 65 76  **       retriev
eb560 61 6c 20 61 6e 64 20 63 72 65 61 74 69 6f 6e 0a  al and creation.
eb570 2a 2a 0a 2a 2a 20 20 48 4f 53 54 49 44 50 41 54  **.**  HOSTIDPAT
eb580 48 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4f 76  H.**.**       Ov
eb590 65 72 72 69 64 65 73 20 74 68 65 20 64 65 66 61  errides the defa
eb5a0 75 6c 74 20 68 6f 73 74 20 49 44 20 66 69 6c 65  ult host ID file
eb5b0 20 70 61 74 68 20 6c 6f 63 61 74 69 6f 6e 0a 2a   path location.*
eb5c0 2a 0a 2a 2a 20 20 4c 4f 43 4b 50 52 4f 58 59 44  *.**  LOCKPROXYD
eb5d0 49 52 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4f  IR.**.**       O
eb5e0 76 65 72 72 69 64 65 73 20 74 68 65 20 64 65 66  verrides the def
eb5f0 61 75 6c 74 20 64 69 72 65 63 74 6f 72 79 20 75  ault directory u
eb600 73 65 64 20 66 6f 72 20 6c 6f 63 6b 20 70 72 6f  sed for lock pro
eb610 78 79 20 66 69 6c 65 73 20 74 68 61 74 0a 2a 2a  xy files that.**
eb620 20 20 20 20 20 20 20 61 72 65 20 6e 61 6d 65 64         are named
eb630 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 76   automatically v
eb640 69 61 20 74 68 65 20 22 3a 61 75 74 6f 3a 22 20  ia the ":auto:" 
eb650 73 65 74 74 69 6e 67 0a 2a 2a 0a 2a 2a 20 20 53  setting.**.**  S
eb660 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 52  QLITE_DEFAULT_PR
eb670 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 53 49 4f  OXYDIR_PERMISSIO
eb680 4e 53 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 50  NS.**.**       P
eb690 65 72 6d 69 73 73 69 6f 6e 73 20 74 6f 20 75 73  ermissions to us
eb6a0 65 20 77 68 65 6e 20 63 72 65 61 74 69 6e 67 20  e when creating 
eb6b0 61 20 64 69 72 65 63 74 6f 72 79 20 66 6f 72 20  a directory for 
eb6c0 73 74 6f 72 69 6e 67 20 74 68 65 0a 2a 2a 20 20  storing the.**  
eb6d0 20 20 20 20 20 6c 6f 63 6b 20 70 72 6f 78 79 20       lock proxy 
eb6e0 66 69 6c 65 73 2c 20 6f 6e 6c 79 20 75 73 65 64  files, only used
eb6f0 20 77 68 65 6e 20 4c 4f 43 4b 50 52 4f 58 59 44   when LOCKPROXYD
eb700 49 52 20 69 73 20 6e 6f 74 20 73 65 74 2e 0a 2a  IR is not set..*
eb710 2a 20 20 20 20 0a 2a 2a 20 20 20 20 0a 2a 2a 20  *    .**    .** 
eb720 41 73 20 6d 65 6e 74 69 6f 6e 65 64 20 61 62 6f  As mentioned abo
eb730 76 65 2c 20 77 68 65 6e 20 63 6f 6d 70 69 6c 65  ve, when compile
eb740 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f 50 52  d with SQLITE_PR
eb750 45 46 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49  EFER_PROXY_LOCKI
eb760 4e 47 2c 0a 2a 2a 20 73 65 74 74 69 6e 67 20 74  NG,.** setting t
eb770 68 65 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 76  he environment v
eb780 61 72 69 61 62 6c 65 20 53 51 4c 49 54 45 5f 46  ariable SQLITE_F
eb790 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49  ORCE_PROXY_LOCKI
eb7a0 4e 47 20 74 6f 20 31 20 77 69 6c 6c 0a 2a 2a 20  NG to 1 will.** 
eb7b0 66 6f 72 63 65 20 70 72 6f 78 79 20 6c 6f 63 6b  force proxy lock
eb7c0 69 6e 67 20 74 6f 20 62 65 20 75 73 65 64 20 66  ing to be used f
eb7d0 6f 72 20 65 76 65 72 79 20 64 61 74 61 62 61 73  or every databas
eb7e0 65 20 66 69 6c 65 20 6f 70 65 6e 65 64 2c 20 61  e file opened, a
eb7f0 6e 64 20 30 0a 2a 2a 20 77 69 6c 6c 20 66 6f 72  nd 0.** will for
eb800 63 65 20 61 75 74 6f 6d 61 74 69 63 20 70 72 6f  ce automatic pro
eb810 78 79 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 62 65  xy locking to be
eb820 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20 61 6c   disabled for al
eb830 6c 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69  l database.** fi
eb840 6c 65 73 20 28 65 78 70 6c 69 63 69 74 79 20 63  les (explicity c
eb850 61 6c 6c 69 6e 67 20 74 68 65 20 53 51 4c 49 54  alling the SQLIT
eb860 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46  E_SET_LOCKPROXYF
eb870 49 4c 45 20 70 72 61 67 6d 61 20 6f 72 0a 2a 2a  ILE pragma or.**
eb880 20 73 71 6c 69 74 65 5f 66 69 6c 65 5f 63 6f 6e   sqlite_file_con
eb890 74 72 6f 6c 20 41 50 49 20 69 73 20 6e 6f 74 20  trol API is not 
eb8a0 61 66 66 65 63 74 65 64 20 62 79 20 53 51 4c 49  affected by SQLI
eb8b0 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c  TE_FORCE_PROXY_L
eb8c0 4f 43 4b 49 4e 47 29 2e 0a 2a 2f 0a 0a 2f 2a 0a  OCKING)..*/../*.
eb8d0 2a 2a 20 50 72 6f 78 79 20 6c 6f 63 6b 69 6e 67  ** Proxy locking
eb8e0 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62   is only availab
eb8f0 6c 65 20 6f 6e 20 4d 61 63 4f 53 58 20 0a 2a 2f  le on MacOSX .*/
eb900 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41  .#if defined(__A
eb910 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54  PPLE__) && SQLIT
eb920 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
eb930 5f 53 54 59 4c 45 0a 0a 23 69 66 64 65 66 20 53  _STYLE..#ifdef S
eb940 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 20 73 69  QLITE_TEST./* si
eb950 6d 75 6c 61 74 65 20 6d 75 6c 74 69 70 6c 65 20  mulate multiple 
eb960 68 6f 73 74 73 20 62 79 20 63 72 65 61 74 69 6e  hosts by creatin
eb970 67 20 75 6e 69 71 75 65 20 68 6f 73 74 69 64 20  g unique hostid 
eb980 66 69 6c 65 20 70 61 74 68 73 20 2a 2f 0a 53 51  file paths */.SQ
eb990 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
eb9a0 69 74 65 33 5f 68 6f 73 74 69 64 5f 6e 75 6d 20  ite3_hostid_num 
eb9b0 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  = 0;.#endif../*.
eb9c0 2a 2a 20 54 68 65 20 70 72 6f 78 79 4c 6f 63 6b  ** The proxyLock
eb9d0 69 6e 67 43 6f 6e 74 65 78 74 20 68 61 73 20 74  ingContext has t
eb9e0 68 65 20 70 61 74 68 20 61 6e 64 20 66 69 6c 65  he path and file
eb9f0 20 73 74 72 75 63 74 75 72 65 73 20 66 6f 72 20   structures for 
eba00 74 68 65 20 72 65 6d 6f 74 65 20 0a 2a 2a 20 61  the remote .** a
eba10 6e 64 20 6c 6f 63 61 6c 20 70 72 6f 78 79 20 66  nd local proxy f
eba20 69 6c 65 73 20 69 6e 20 69 74 0a 2a 2f 0a 74 79  iles in it.*/.ty
eba30 70 65 64 65 66 20 73 74 72 75 63 74 20 70 72 6f  pedef struct pro
eba40 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  xyLockingContext
eba50 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e   proxyLockingCon
eba60 74 65 78 74 3b 0a 73 74 72 75 63 74 20 70 72 6f  text;.struct pro
eba70 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  xyLockingContext
eba80 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 63   {.  unixFile *c
eba90 6f 6e 63 68 46 69 6c 65 3b 20 20 20 20 20 20 20  onchFile;       
ebaa0 20 20 2f 2a 20 4f 70 65 6e 20 63 6f 6e 63 68 20    /* Open conch 
ebab0 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a  file */.  char *
ebac0 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 3b 20 20  conchFilePath;  
ebad0 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
ebae0 66 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65  f the conch file
ebaf0 20 2a 2f 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a   */.  unixFile *
ebb00 6c 6f 63 6b 50 72 6f 78 79 3b 20 20 20 20 20 20  lockProxy;      
ebb10 20 20 20 2f 2a 20 4f 70 65 6e 20 70 72 6f 78 79     /* Open proxy
ebb20 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a 20 20   lock file */.  
ebb30 63 68 61 72 20 2a 6c 6f 63 6b 50 72 6f 78 79 50  char *lockProxyP
ebb40 61 74 68 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ath;         /* 
ebb50 4e 61 6d 65 20 6f 66 20 74 68 65 20 70 72 6f 78  Name of the prox
ebb60 79 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a 20  y lock file */. 
ebb70 20 63 68 61 72 20 2a 64 62 50 61 74 68 3b 20 20   char *dbPath;  
ebb80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
ebb90 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6f 70 65   Name of the ope
ebba0 6e 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20  n file */.  int 
ebbb0 63 6f 6e 63 68 48 65 6c 64 3b 20 20 20 20 20 20  conchHeld;      
ebbc0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
ebbd0 20 69 66 20 74 68 65 20 63 6f 6e 63 68 20 69 73   if the conch is
ebbe0 20 63 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 20   currently held 
ebbf0 2a 2f 0a 20 20 76 6f 69 64 20 2a 6f 6c 64 4c 6f  */.  void *oldLo
ebc00 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 20 20 20  ckingContext;   
ebc10 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 6c 6f    /* Original lo
ebc20 63 6b 69 6e 67 63 6f 6e 74 65 78 74 20 74 6f 20  ckingcontext to 
ebc30 72 65 73 74 6f 72 65 20 6f 6e 20 63 6c 6f 73 65  restore on close
ebc40 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f   */.  sqlite3_io
ebc50 5f 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a  _methods const *
ebc60 70 4f 6c 64 4d 65 74 68 6f 64 3b 20 20 20 20 20  pOldMethod;     
ebc70 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 49 2f 4f 20  /* Original I/O 
ebc80 6d 65 74 68 6f 64 73 20 66 6f 72 20 63 6c 6f 73  methods for clos
ebc90 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 48 4f 53 54  e */.};../* HOST
ebca0 49 44 4c 45 4e 20 61 6e 64 20 43 4f 4e 43 48 4c  IDLEN and CONCHL
ebcb0 45 4e 20 62 6f 74 68 20 69 6e 63 6c 75 64 65 20  EN both include 
ebcc0 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 73 74  space for the st
ebcd0 72 69 6e 67 20 0a 2a 2a 20 74 65 72 6d 69 6e 61  ring .** termina
ebce0 74 69 6e 67 20 6e 75 6c 20 0a 2a 2f 0a 23 64 65  ting nul .*/.#de
ebcf0 66 69 6e 65 20 48 4f 53 54 49 44 4c 45 4e 20 20  fine HOSTIDLEN  
ebd00 20 20 20 20 20 20 20 31 32 38 0a 23 64 65 66 69         128.#defi
ebd10 6e 65 20 43 4f 4e 43 48 4c 45 4e 20 20 20 20 20  ne CONCHLEN     
ebd20 20 20 20 20 20 28 4d 41 58 50 41 54 48 4c 45 4e       (MAXPATHLEN
ebd30 2b 48 4f 53 54 49 44 4c 45 4e 2b 31 29 0a 23 69  +HOSTIDLEN+1).#i
ebd40 66 6e 64 65 66 20 48 4f 53 54 49 44 50 41 54 48  fndef HOSTIDPATH
ebd50 0a 23 20 64 65 66 69 6e 65 20 48 4f 53 54 49 44  .# define HOSTID
ebd60 50 41 54 48 20 20 20 20 20 20 20 22 2f 4c 69 62  PATH       "/Lib
ebd70 72 61 72 79 2f 43 61 63 68 65 73 2f 2e 63 6f 6d  rary/Caches/.com
ebd80 2e 61 70 70 6c 65 2e 73 71 6c 69 74 65 43 6f 6e  .apple.sqliteCon
ebd90 63 68 48 6f 73 74 49 64 22 0a 23 65 6e 64 69 66  chHostId".#endif
ebda0 0a 0a 2f 2a 20 62 61 73 69 63 61 6c 6c 79 20 61  ../* basically a
ebdb0 20 63 6f 70 79 20 6f 66 20 75 6e 69 78 52 61 6e   copy of unixRan
ebdc0 64 6f 6d 6e 65 73 73 20 77 69 74 68 20 64 69 66  domness with dif
ebdd0 66 65 72 65 6e 74 0a 2a 2a 20 74 65 73 74 20 62  ferent.** test b
ebde0 65 68 61 76 69 6f 72 20 62 75 69 6c 74 20 69 6e  ehavior built in
ebdf0 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70   */.static int p
ebe00 72 6f 78 79 47 65 6e 65 72 61 74 65 48 6f 73 74  roxyGenerateHost
ebe10 49 44 28 63 68 61 72 20 2a 70 48 6f 73 74 49 44  ID(char *pHostID
ebe20 29 7b 0a 20 20 69 6e 74 20 70 69 64 2c 20 66 64  ){.  int pid, fd
ebe30 2c 20 6c 65 6e 3b 0a 20 20 75 6e 73 69 67 6e 65  , len;.  unsigne
ebe40 64 20 63 68 61 72 20 2a 6b 65 79 20 3d 20 28 75  d char *key = (u
ebe50 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70  nsigned char *)p
ebe60 48 6f 73 74 49 44 3b 0a 20 20 0a 20 20 6d 65 6d  HostID;.  .  mem
ebe70 73 65 74 28 6b 65 79 2c 20 30 2c 20 48 4f 53 54  set(key, 0, HOST
ebe80 49 44 4c 45 4e 29 3b 0a 20 20 6c 65 6e 20 3d 20  IDLEN);.  len = 
ebe90 30 3b 0a 20 20 66 64 20 3d 20 6f 70 65 6e 28 22  0;.  fd = open("
ebea0 2f 64 65 76 2f 75 72 61 6e 64 6f 6d 22 2c 20 4f  /dev/urandom", O
ebeb0 5f 52 44 4f 4e 4c 59 29 3b 0a 20 20 69 66 28 20  _RDONLY);.  if( 
ebec0 66 64 3e 3d 30 20 29 7b 0a 20 20 20 20 6c 65 6e  fd>=0 ){.    len
ebed0 20 3d 20 72 65 61 64 28 66 64 2c 20 6b 65 79 2c   = read(fd, key,
ebee0 20 48 4f 53 54 49 44 4c 45 4e 29 3b 0a 20 20 20   HOSTIDLEN);.   
ebef0 20 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a 20 73   close(fd); /* s
ebf00 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 74 68 65  ilently leak the
ebf10 20 66 64 20 69 66 20 69 74 20 66 61 69 6c 73 20   fd if it fails 
ebf20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 6c 65 6e  */.  }.  if( len
ebf30 20 3c 20 48 4f 53 54 49 44 4c 45 4e 20 29 7b 0a   < HOSTIDLEN ){.
ebf40 20 20 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20      time_t t;.  
ebf50 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 20 20    time(&t);.    
ebf60 6d 65 6d 63 70 79 28 6b 65 79 2c 20 26 74 2c 20  memcpy(key, &t, 
ebf70 73 69 7a 65 6f 66 28 74 29 29 3b 0a 20 20 20 20  sizeof(t));.    
ebf80 70 69 64 20 3d 20 67 65 74 70 69 64 28 29 3b 0a  pid = getpid();.
ebf90 20 20 20 20 6d 65 6d 63 70 79 28 26 6b 65 79 5b      memcpy(&key[
ebfa0 73 69 7a 65 6f 66 28 74 29 5d 2c 20 26 70 69 64  sizeof(t)], &pid
ebfb0 2c 20 73 69 7a 65 6f 66 28 70 69 64 29 29 3b 0a  , sizeof(pid));.
ebfc0 20 20 7d 0a 20 20 0a 23 69 66 64 65 66 20 4d 41    }.  .#ifdef MA
ebfd0 4b 45 5f 50 52 45 54 54 59 5f 48 4f 53 54 49 44  KE_PRETTY_HOSTID
ebfe0 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  .  {.    int i;.
ebff0 20 20 20 20 2f 2a 20 66 69 6c 74 65 72 20 74 68      /* filter th
ec000 65 20 62 79 74 65 73 20 69 6e 74 6f 20 70 72 69  e bytes into pri
ec010 6e 74 61 62 6c 65 20 61 73 63 69 69 20 63 68 61  ntable ascii cha
ec020 72 61 63 74 65 72 73 20 61 6e 64 20 4e 55 4c 20  racters and NUL 
ec030 74 65 72 6d 69 6e 61 74 65 20 2a 2f 0a 20 20 20  terminate */.   
ec040 20 6b 65 79 5b 28 48 4f 53 54 49 44 4c 45 4e 2d   key[(HOSTIDLEN-
ec050 31 29 5d 20 3d 20 30 78 30 30 3b 0a 20 20 20 20  1)] = 0x00;.    
ec060 66 6f 72 28 20 69 3d 30 3b 20 69 3c 28 48 4f 53  for( i=0; i<(HOS
ec070 54 49 44 4c 45 4e 2d 31 29 3b 20 69 2b 2b 20 29  TIDLEN-1); i++ )
ec080 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64  {.      unsigned
ec090 20 63 68 61 72 20 70 61 20 3d 20 6b 65 79 5b 69   char pa = key[i
ec0a0 5d 26 30 78 37 46 3b 0a 20 20 20 20 20 20 69 66  ]&0x7F;.      if
ec0b0 28 20 70 61 3c 30 78 32 30 20 29 7b 0a 20 20 20  ( pa<0x20 ){.   
ec0c0 20 20 20 20 20 6b 65 79 5b 69 5d 20 3d 20 28 6b       key[i] = (k
ec0d0 65 79 5b 69 5d 26 30 78 38 30 20 3d 3d 20 30 78  ey[i]&0x80 == 0x
ec0e0 38 30 29 20 3f 20 70 61 2b 30 78 34 30 20 3a 20  80) ? pa+0x40 : 
ec0f0 70 61 2b 30 78 32 30 3b 0a 20 20 20 20 20 20 7d  pa+0x20;.      }
ec100 65 6c 73 65 20 69 66 28 20 70 61 3d 3d 30 78 37  else if( pa==0x7
ec110 46 20 29 7b 0a 20 20 20 20 20 20 20 20 6b 65 79  F ){.        key
ec120 5b 69 5d 20 3d 20 28 6b 65 79 5b 69 5d 26 30 78  [i] = (key[i]&0x
ec130 38 30 20 3d 3d 20 30 78 38 30 29 20 3f 20 70 61  80 == 0x80) ? pa
ec140 3d 30 78 32 30 20 3a 20 70 61 2b 30 78 37 45 3b  =0x20 : pa+0x7E;
ec150 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
ec160 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75   }.#endif.  retu
ec170 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
ec180 0a 2f 2a 20 77 72 69 74 65 73 20 74 68 65 20 68  ./* writes the h
ec190 6f 73 74 20 69 64 20 70 61 74 68 20 74 6f 20 70  ost id path to p
ec1a0 61 74 68 2c 20 70 61 74 68 20 73 68 6f 75 6c 64  ath, path should
ec1b0 20 62 65 20 61 6e 20 70 72 65 2d 61 6c 6c 6f 63   be an pre-alloc
ec1c0 61 74 65 64 20 62 75 66 66 65 72 0a 2a 2a 20 77  ated buffer.** w
ec1d0 69 74 68 20 65 6e 6f 75 67 68 20 73 70 61 63 65  ith enough space
ec1e0 20 66 6f 72 20 61 20 70 61 74 68 20 0a 2a 2f 0a   for a path .*/.
ec1f0 73 74 61 74 69 63 20 76 6f 69 64 20 70 72 6f 78  static void prox
ec200 79 47 65 74 48 6f 73 74 49 44 50 61 74 68 28 63  yGetHostIDPath(c
ec210 68 61 72 20 2a 70 61 74 68 2c 20 73 69 7a 65 5f  har *path, size_
ec220 74 20 6c 65 6e 29 7b 0a 20 20 73 74 72 6c 63 70  t len){.  strlcp
ec230 79 28 70 61 74 68 2c 20 48 4f 53 54 49 44 50 41  y(path, HOSTIDPA
ec240 54 48 2c 20 6c 65 6e 29 3b 0a 23 69 66 64 65 66  TH, len);.#ifdef
ec250 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69   SQLITE_TEST.  i
ec260 66 28 20 73 71 6c 69 74 65 33 5f 68 6f 73 74 69  f( sqlite3_hosti
ec270 64 5f 6e 75 6d 3e 30 20 29 7b 0a 20 20 20 20 63  d_num>0 ){.    c
ec280 68 61 72 20 73 75 66 66 69 78 5b 32 5d 20 3d 20  har suffix[2] = 
ec290 22 31 22 3b 0a 20 20 20 20 73 75 66 66 69 78 5b  "1";.    suffix[
ec2a0 30 5d 20 3d 20 73 75 66 66 69 78 5b 30 5d 20 2b  0] = suffix[0] +
ec2b0 20 73 71 6c 69 74 65 33 5f 68 6f 73 74 69 64 5f   sqlite3_hostid_
ec2c0 6e 75 6d 3b 0a 20 20 20 20 73 74 72 6c 63 61 74  num;.    strlcat
ec2d0 28 70 61 74 68 2c 20 73 75 66 66 69 78 2c 20 6c  (path, suffix, l
ec2e0 65 6e 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  en);.  }.#endif.
ec2f0 20 20 4f 53 54 52 41 43 45 33 28 22 47 45 54 48    OSTRACE3("GETH
ec300 4f 53 54 49 44 50 41 54 48 20 20 25 73 20 70 69  OSTIDPATH  %s pi
ec310 64 3d 25 64 5c 6e 22 2c 20 70 61 74 68 2c 20 67  d=%d\n", path, g
ec320 65 74 70 69 64 28 29 29 3b 0a 7d 0a 0a 2f 2a 20  etpid());.}../* 
ec330 67 65 74 20 74 68 65 20 68 6f 73 74 20 49 44 20  get the host ID 
ec340 66 72 6f 6d 20 61 20 73 71 6c 69 74 65 20 68 6f  from a sqlite ho
ec350 73 74 69 64 20 66 69 6c 65 20 73 74 6f 72 65 64  stid file stored
ec360 20 69 6e 20 74 68 65 20 0a 2a 2a 20 75 73 65 72   in the .** user
ec370 2d 73 70 65 63 69 66 69 63 20 74 6d 70 20 64 69  -specific tmp di
ec380 72 65 63 74 6f 72 79 2c 20 63 72 65 61 74 65 20  rectory, create 
ec390 74 68 65 20 49 44 20 69 66 20 69 74 27 73 20 6e  the ID if it's n
ec3a0 6f 74 20 74 68 65 72 65 20 61 6c 72 65 61 64 79  ot there already
ec3b0 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20   .*/.static int 
ec3c0 70 72 6f 78 79 47 65 74 48 6f 73 74 49 44 28 63  proxyGetHostID(c
ec3d0 68 61 72 20 2a 70 48 6f 73 74 49 44 2c 20 69 6e  har *pHostID, in
ec3e0 74 20 2a 70 45 72 72 6f 72 29 7b 0a 20 20 69 6e  t *pError){.  in
ec3f0 74 20 66 64 3b 0a 20 20 63 68 61 72 20 70 61 74  t fd;.  char pat
ec400 68 5b 4d 41 58 50 41 54 48 4c 45 4e 5d 3b 20 0a  h[MAXPATHLEN]; .
ec410 20 20 73 69 7a 65 5f 74 20 6c 65 6e 3b 0a 20 20    size_t len;.  
ec420 69 6e 74 20 72 63 3d 53 51 4c 49 54 45 5f 4f 4b  int rc=SQLITE_OK
ec430 3b 0a 0a 20 20 70 72 6f 78 79 47 65 74 48 6f 73  ;..  proxyGetHos
ec440 74 49 44 50 61 74 68 28 70 61 74 68 2c 20 4d 41  tIDPath(path, MA
ec450 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20 2f 2a 20  XPATHLEN);.  /* 
ec460 74 72 79 20 74 6f 20 63 72 65 61 74 65 20 74 68  try to create th
ec470 65 20 68 6f 73 74 20 49 44 20 66 69 6c 65 2c 20  e host ID file, 
ec480 69 66 20 69 74 20 61 6c 72 65 61 64 79 20 65 78  if it already ex
ec490 69 73 74 73 20 72 65 61 64 20 74 68 65 20 63 6f  ists read the co
ec4a0 6e 74 65 6e 74 73 20 2a 2f 0a 20 20 66 64 20 3d  ntents */.  fd =
ec4b0 20 6f 70 65 6e 28 70 61 74 68 2c 20 4f 5f 43 52   open(path, O_CR
ec4c0 45 41 54 7c 4f 5f 57 52 4f 4e 4c 59 7c 4f 5f 45  EAT|O_WRONLY|O_E
ec4d0 58 43 4c 2c 20 30 36 34 34 29 3b 0a 20 20 69 66  XCL, 0644);.  if
ec4e0 28 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 69 6e  ( fd<0 ){.    in
ec4f0 74 20 65 72 72 3d 65 72 72 6e 6f 3b 0a 09 09 0a  t err=errno;....
ec500 20 20 20 20 69 66 28 20 65 72 72 21 3d 45 45 58      if( err!=EEX
ec510 49 53 54 20 29 7b 0a 23 69 66 64 65 66 20 53 51  IST ){.#ifdef SQ
ec520 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 55 47  LITE_PROXY_DEBUG
ec530 20 2f 2a 20 73 65 74 20 74 68 65 20 73 71 6c 69   /* set the sqli
ec540 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  te error message
ec550 20 69 6e 73 74 65 61 64 20 2a 2f 0a 20 20 20 20   instead */.    
ec560 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72    fprintf(stderr
ec570 2c 20 22 73 71 6c 69 74 65 20 65 72 72 6f 72 20  , "sqlite error 
ec580 63 72 65 61 74 69 6e 67 20 68 6f 73 74 20 49 44  creating host ID
ec590 20 66 69 6c 65 20 25 73 3a 20 25 73 5c 6e 22 2c   file %s: %s\n",
ec5a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
ec5b0 61 74 68 2c 20 73 74 72 65 72 72 6f 72 28 65 72  ath, strerror(er
ec5c0 72 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  r));.#endif.    
ec5d0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
ec5e0 50 45 52 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  PERM;.    }.    
ec5f0 2f 2a 20 63 6f 75 6c 64 6e 27 74 20 63 72 65 61  /* couldn't crea
ec600 74 65 20 74 68 65 20 66 69 6c 65 2c 20 72 65 61  te the file, rea
ec610 64 20 69 74 20 69 6e 73 74 65 61 64 20 2a 2f 0a  d it instead */.
ec620 20 20 20 20 66 64 20 3d 20 6f 70 65 6e 28 70 61      fd = open(pa
ec630 74 68 2c 20 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 45  th, O_RDONLY|O_E
ec640 58 43 4c 29 3b 0a 20 20 20 20 69 66 28 20 66 64  XCL);.    if( fd
ec650 3c 30 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c  <0 ){.#ifdef SQL
ec660 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 55 47 20  ITE_PROXY_DEBUG 
ec670 2f 2a 20 73 65 74 20 74 68 65 20 73 71 6c 69 74  /* set the sqlit
ec680 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  e error message 
ec690 69 6e 73 74 65 61 64 20 2a 2f 0a 20 20 20 20 20  instead */.     
ec6a0 20 69 6e 74 20 65 72 72 20 3d 20 65 72 72 6e 6f   int err = errno
ec6b0 3b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28  ;.      fprintf(
ec6c0 73 74 64 65 72 72 2c 20 22 73 71 6c 69 74 65 20  stderr, "sqlite 
ec6d0 65 72 72 6f 72 20 6f 70 65 6e 69 6e 67 20 68 6f  error opening ho
ec6e0 73 74 20 49 44 20 66 69 6c 65 20 25 73 3a 20 25  st ID file %s: %
ec6f0 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  s\n",.          
ec700 20 20 20 20 70 61 74 68 2c 20 73 74 72 65 72 72      path, strerr
ec710 6f 72 28 65 72 72 29 29 3b 0a 23 65 6e 64 69 66  or(err));.#endif
ec720 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
ec730 4c 49 54 45 5f 50 45 52 4d 3b 0a 20 20 20 20 7d  LITE_PERM;.    }
ec740 0a 20 20 20 20 6c 65 6e 20 3d 20 70 72 65 61 64  .    len = pread
ec750 28 66 64 2c 20 70 48 6f 73 74 49 44 2c 20 48 4f  (fd, pHostID, HO
ec760 53 54 49 44 4c 45 4e 2c 20 30 29 3b 0a 20 20 20  STIDLEN, 0);.   
ec770 20 69 66 28 20 6c 65 6e 3c 30 20 29 7b 0a 20 20   if( len<0 ){.  
ec780 20 20 20 20 2a 70 45 72 72 6f 72 20 3d 20 65 72      *pError = er
ec790 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  rno;.      rc = 
ec7a0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41  SQLITE_IOERR_REA
ec7b0 44 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  D;.    }else if(
ec7c0 20 6c 65 6e 3c 48 4f 53 54 49 44 4c 45 4e 20 29   len<HOSTIDLEN )
ec7d0 7b 0a 20 20 20 20 20 20 2a 70 45 72 72 6f 72 20  {.      *pError 
ec7e0 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  = 0;.      rc = 
ec7f0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f  SQLITE_IOERR_SHO
ec800 52 54 5f 52 45 41 44 3b 0a 20 20 20 20 7d 0a 20  RT_READ;.    }. 
ec810 20 20 20 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a     close(fd); /*
ec820 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 74   silently leak t
ec830 68 65 20 66 64 20 69 66 20 69 74 20 66 61 69 6c  he fd if it fail
ec840 73 20 2a 2f 0a 20 20 20 20 4f 53 54 52 41 43 45  s */.    OSTRACE
ec850 33 28 22 47 45 54 48 4f 53 54 49 44 20 20 72 65  3("GETHOSTID  re
ec860 61 64 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c  ad %s pid=%d\n",
ec870 20 70 48 6f 73 74 49 44 2c 20 67 65 74 70 69 64   pHostID, getpid
ec880 28 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ());.    return 
ec890 72 63 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  rc;.  }else{.   
ec8a0 20 2f 2a 20 77 65 27 72 65 20 63 72 65 61 74 69   /* we're creati
ec8b0 6e 67 20 74 68 65 20 68 6f 73 74 20 49 44 20 66  ng the host ID f
ec8c0 69 6c 65 20 28 75 73 65 20 61 20 72 61 6e 64 6f  ile (use a rando
ec8d0 6d 20 73 74 72 69 6e 67 20 6f 66 20 62 79 74 65  m string of byte
ec8e0 73 29 20 2a 2f 0a 20 20 20 20 70 72 6f 78 79 47  s) */.    proxyG
ec8f0 65 6e 65 72 61 74 65 48 6f 73 74 49 44 28 70 48  enerateHostID(pH
ec900 6f 73 74 49 44 29 3b 0a 20 20 20 20 6c 65 6e 20  ostID);.    len 
ec910 3d 20 70 77 72 69 74 65 28 66 64 2c 20 70 48 6f  = pwrite(fd, pHo
ec920 73 74 49 44 2c 20 48 4f 53 54 49 44 4c 45 4e 2c  stID, HOSTIDLEN,
ec930 20 30 29 3b 0a 20 20 20 20 69 66 28 20 6c 65 6e   0);.    if( len
ec940 3c 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 45 72  <0 ){.      *pEr
ec950 72 6f 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20  ror = errno;.   
ec960 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49     rc = SQLITE_I
ec970 4f 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 20  OERR_WRITE;.    
ec980 7d 65 6c 73 65 20 69 66 28 20 6c 65 6e 3c 48 4f  }else if( len<HO
ec990 53 54 49 44 4c 45 4e 20 29 7b 0a 20 20 20 20 20  STIDLEN ){.     
ec9a0 20 2a 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20   *pError = 0;.  
ec9b0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
ec9c0 49 4f 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 20  IOERR_WRITE;.   
ec9d0 20 7d 0a 20 20 20 20 63 6c 6f 73 65 28 66 64 29   }.    close(fd)
ec9e0 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65  ; /* silently le
ec9f0 61 6b 20 74 68 65 20 66 64 20 69 66 20 69 74 20  ak the fd if it 
eca00 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20 4f 53 54  fails */.    OST
eca10 52 41 43 45 33 28 22 47 45 54 48 4f 53 54 49 44  RACE3("GETHOSTID
eca20 20 20 77 72 6f 74 65 20 25 73 20 70 69 64 3d 25    wrote %s pid=%
eca30 64 5c 6e 22 2c 20 70 48 6f 73 74 49 44 2c 20 67  d\n", pHostID, g
eca40 65 74 70 69 64 28 29 29 3b 0a 20 20 20 20 72 65  etpid());.    re
eca50 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a  turn rc;.  }.}..
eca60 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79  static int proxy
eca70 47 65 74 4c 6f 63 6b 50 61 74 68 28 63 6f 6e 73  GetLockPath(cons
eca80 74 20 63 68 61 72 20 2a 64 62 50 61 74 68 2c 20  t char *dbPath, 
eca90 63 68 61 72 20 2a 6c 50 61 74 68 2c 20 73 69 7a  char *lPath, siz
ecaa0 65 5f 74 20 6d 61 78 4c 65 6e 29 7b 0a 20 20 69  e_t maxLen){.  i
ecab0 6e 74 20 6c 65 6e 3b 0a 20 20 69 6e 74 20 64 62  nt len;.  int db
ecac0 4c 65 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 23  Len;.  int i;..#
ecad0 69 66 64 65 66 20 4c 4f 43 4b 50 52 4f 58 59 44  ifdef LOCKPROXYD
ecae0 49 52 0a 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63  IR.  len = strlc
ecaf0 70 79 28 6c 50 61 74 68 2c 20 4c 4f 43 4b 50 52  py(lPath, LOCKPR
ecb00 4f 58 59 44 49 52 2c 20 6d 61 78 4c 65 6e 29 3b  OXYDIR, maxLen);
ecb10 0a 23 65 6c 73 65 0a 23 20 69 66 64 65 66 20 5f  .#else.# ifdef _
ecb20 43 53 5f 44 41 52 57 49 4e 5f 55 53 45 52 5f 54  CS_DARWIN_USER_T
ecb30 45 4d 50 5f 44 49 52 0a 20 20 7b 0a 20 20 20 20  EMP_DIR.  {.    
ecb40 63 6f 6e 66 73 74 72 28 5f 43 53 5f 44 41 52 57  confstr(_CS_DARW
ecb50 49 4e 5f 55 53 45 52 5f 54 45 4d 50 5f 44 49 52  IN_USER_TEMP_DIR
ecb60 2c 20 6c 50 61 74 68 2c 20 6d 61 78 4c 65 6e 29  , lPath, maxLen)
ecb70 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 73 74 72 6c  ;.    len = strl
ecb80 63 61 74 28 6c 50 61 74 68 2c 20 22 73 71 6c 69  cat(lPath, "sqli
ecb90 74 65 70 6c 6f 63 6b 73 22 2c 20 6d 61 78 4c 65  teplocks", maxLe
ecba0 6e 29 3b 0a 20 20 20 20 69 66 28 20 6d 6b 64 69  n);.    if( mkdi
ecbb0 72 28 6c 50 61 74 68 2c 20 53 51 4c 49 54 45 5f  r(lPath, SQLITE_
ecbc0 44 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 49 52  DEFAULT_PROXYDIR
ecbd0 5f 50 45 52 4d 49 53 53 49 4f 4e 53 29 20 29 7b  _PERMISSIONS) ){
ecbe0 0a 20 20 20 20 20 20 2f 2a 20 69 66 20 6d 6b 64  .      /* if mkd
ecbf0 69 72 20 66 61 69 6c 73 2c 20 68 61 6e 64 6c 65  ir fails, handle
ecc00 20 61 73 20 6c 6f 63 6b 20 66 69 6c 65 20 63 72   as lock file cr
ecc10 65 61 74 69 6f 6e 20 66 61 69 6c 75 72 65 20 2a  eation failure *
ecc20 2f 0a 23 20 20 69 66 64 65 66 20 53 51 4c 49 54  /.#  ifdef SQLIT
ecc30 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20 69 6e  E_DEBUG.      in
ecc40 74 20 65 72 72 20 3d 20 65 72 72 6e 6f 3b 0a 20  t err = errno;. 
ecc50 20 20 20 20 20 69 66 28 20 65 72 72 21 3d 45 45       if( err!=EE
ecc60 58 49 53 54 20 29 7b 0a 20 20 20 20 20 20 20 20  XIST ){.        
ecc70 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20  fprintf(stderr, 
ecc80 22 70 72 6f 78 79 47 65 74 4c 6f 63 6b 50 61 74  "proxyGetLockPat
ecc90 68 3a 20 6d 6b 64 69 72 28 25 73 2c 30 25 6f 29  h: mkdir(%s,0%o)
ecca0 20 65 72 72 6f 72 20 25 64 20 25 73 5c 6e 22 2c   error %d %s\n",
eccb0 20 6c 50 61 74 68 2c 0a 20 20 20 20 20 20 20 20   lPath,.        
eccc0 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44          SQLITE_D
eccd0 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f  EFAULT_PROXYDIR_
ecce0 50 45 52 4d 49 53 53 49 4f 4e 53 2c 20 65 72 72  PERMISSIONS, err
eccf0 2c 20 73 74 72 65 72 72 6f 72 28 65 72 72 29 29  , strerror(err))
ecd00 3b 0a 20 20 20 20 20 20 7d 0a 23 20 20 65 6e 64  ;.      }.#  end
ecd10 69 66 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  if.    }else{.  
ecd20 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 47 45      OSTRACE3("GE
ecd30 54 4c 4f 43 4b 50 41 54 48 20 20 6d 6b 64 69 72  TLOCKPATH  mkdir
ecd40 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 6c   %s pid=%d\n", l
ecd50 50 61 74 68 2c 20 67 65 74 70 69 64 28 29 29 3b  Path, getpid());
ecd60 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 7d 0a  .    }.    .  }.
ecd70 23 20 65 6c 73 65 0a 20 20 6c 65 6e 20 3d 20 73  # else.  len = s
ecd80 74 72 6c 63 70 79 28 6c 50 61 74 68 2c 20 22 2f  trlcpy(lPath, "/
ecd90 74 6d 70 2f 22 2c 20 6d 61 78 4c 65 6e 29 3b 0a  tmp/", maxLen);.
ecda0 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a  # endif.#endif..
ecdb0 20 20 69 66 28 20 6c 50 61 74 68 5b 6c 65 6e 2d    if( lPath[len-
ecdc0 31 5d 21 3d 27 2f 27 20 29 7b 0a 20 20 20 20 6c  1]!='/' ){.    l
ecdd0 65 6e 20 3d 20 73 74 72 6c 63 61 74 28 6c 50 61  en = strlcat(lPa
ecde0 74 68 2c 20 22 2f 22 2c 20 6d 61 78 4c 65 6e 29  th, "/", maxLen)
ecdf0 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 74 72  ;.  }.  .  /* tr
ece00 61 6e 73 66 6f 72 6d 20 74 68 65 20 64 62 20 70  ansform the db p
ece10 61 74 68 20 74 6f 20 61 20 75 6e 69 71 75 65 20  ath to a unique 
ece20 63 61 63 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20  cache name */.  
ece30 64 62 4c 65 6e 20 3d 20 28 69 6e 74 29 73 74 72  dbLen = (int)str
ece40 6c 65 6e 28 64 62 50 61 74 68 29 3b 0a 20 20 66  len(dbPath);.  f
ece50 6f 72 28 20 69 3d 30 3b 20 69 3c 64 62 4c 65 6e  or( i=0; i<dbLen
ece60 20 26 26 20 28 69 2b 6c 65 6e 2b 37 29 3c 6d 61   && (i+len+7)<ma
ece70 78 4c 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  xLen; i++){.    
ece80 63 68 61 72 20 63 20 3d 20 64 62 50 61 74 68 5b  char c = dbPath[
ece90 69 5d 3b 0a 20 20 20 20 6c 50 61 74 68 5b 69 2b  i];.    lPath[i+
ecea0 6c 65 6e 5d 20 3d 20 28 63 3d 3d 27 2f 27 29 3f  len] = (c=='/')?
eceb0 27 5f 27 3a 63 3b 0a 20 20 7d 0a 20 20 6c 50 61  '_':c;.  }.  lPa
ecec0 74 68 5b 69 2b 6c 65 6e 5d 3d 27 5c 30 27 3b 0a  th[i+len]='\0';.
eced0 20 20 73 74 72 6c 63 61 74 28 6c 50 61 74 68 2c    strlcat(lPath,
ecee0 20 22 3a 61 75 74 6f 3a 22 2c 20 6d 61 78 4c 65   ":auto:", maxLe
ecef0 6e 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  n);.  return SQL
ecf00 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
ecf10 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 56 46   Create a new VF
ecf20 53 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  S file descripto
ecf30 72 20 28 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d  r (stored in mem
ecf40 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ory obtained fro
ecf50 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c  m.** sqlite3_mal
ecf60 6c 6f 63 29 20 61 6e 64 20 6f 70 65 6e 20 74 68  loc) and open th
ecf70 65 20 66 69 6c 65 20 6e 61 6d 65 64 20 22 70 61  e file named "pa
ecf80 74 68 22 20 69 6e 20 74 68 65 20 66 69 6c 65 20  th" in the file 
ecf90 64 65 73 63 72 69 70 74 6f 72 2e 0a 2a 2a 0a 2a  descriptor..**.*
ecfa0 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 69 73 20  * The caller is 
ecfb0 72 65 73 70 6f 6e 73 69 62 6c 65 20 6e 6f 74 20  responsible not 
ecfc0 6f 6e 6c 79 20 66 6f 72 20 63 6c 6f 73 69 6e 67  only for closing
ecfd0 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   the file descri
ecfe0 70 74 6f 72 0a 2a 2a 20 62 75 74 20 61 6c 73 6f  ptor.** but also
ecff0 20 66 6f 72 20 66 72 65 65 69 6e 67 20 74 68 65   for freeing the
ed000 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74   memory associat
ed010 65 64 20 77 69 74 68 20 74 68 65 20 66 69 6c 65  ed with the file
ed020 20 64 65 73 63 72 69 70 74 6f 72 2e 0a 2a 2f 0a   descriptor..*/.
ed030 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79  static int proxy
ed040 43 72 65 61 74 65 55 6e 69 78 46 69 6c 65 28 63  CreateUnixFile(c
ed050 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 2c  onst char *path,
ed060 20 75 6e 69 78 46 69 6c 65 20 2a 2a 70 70 46 69   unixFile **ppFi
ed070 6c 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65  le) {.  unixFile
ed080 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20 66 6c   *pNew;.  int fl
ed090 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45  ags = SQLITE_OPE
ed0a0 4e 5f 4d 41 49 4e 5f 44 42 7c 53 51 4c 49 54 45  N_MAIN_DB|SQLITE
ed0b0 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c 53 51 4c  _OPEN_CREATE|SQL
ed0c0 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
ed0d0 54 45 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  TE;.  int rc = S
ed0e0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c 69  QLITE_OK;.  sqli
ed0f0 74 65 33 5f 76 66 73 20 64 75 6d 6d 79 56 66 73  te3_vfs dummyVfs
ed100 3b 0a 0a 20 20 70 4e 65 77 20 3d 20 28 75 6e 69  ;..  pNew = (uni
ed110 78 46 69 6c 65 20 2a 29 73 71 6c 69 74 65 33 5f  xFile *)sqlite3_
ed120 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 75 6e  malloc(sizeof(un
ed130 69 78 46 69 6c 65 29 29 3b 0a 20 20 69 66 28 20  ixFile));.  if( 
ed140 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 72 65 74  !pNew ){.    ret
ed150 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
ed160 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70  ;.  }.  memset(p
ed170 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75  New, 0, sizeof(u
ed180 6e 69 78 46 69 6c 65 29 29 3b 0a 0a 20 20 2f 2a  nixFile));..  /*
ed190 20 43 61 6c 6c 20 75 6e 69 78 4f 70 65 6e 28 29   Call unixOpen()
ed1a0 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 70 72 6f   to open the pro
ed1b0 78 79 20 66 69 6c 65 2e 20 54 68 65 20 66 6c 61  xy file. The fla
ed1c0 67 73 20 70 61 73 73 65 64 20 74 6f 20 75 6e 69  gs passed to uni
ed1d0 78 4f 70 65 6e 28 29 0a 20 20 2a 2a 20 73 75 67  xOpen().  ** sug
ed1e0 67 65 73 74 20 74 68 61 74 20 74 68 65 20 66 69  gest that the fi
ed1f0 6c 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20  le being opened 
ed200 69 73 20 61 20 22 6d 61 69 6e 20 64 61 74 61 62  is a "main datab
ed210 61 73 65 22 2e 20 54 68 69 73 20 69 73 0a 20 20  ase". This is.  
ed220 2a 2a 20 6e 65 63 65 73 73 61 72 79 20 61 73 20  ** necessary as 
ed230 6f 74 68 65 72 20 66 69 6c 65 20 74 79 70 65 73  other file types
ed240 20 64 6f 20 6e 6f 74 20 6e 65 63 65 73 73 61 72   do not necessar
ed250 69 6c 79 20 73 75 70 70 6f 72 74 20 6c 6f 63 6b  ily support lock
ed260 69 6e 67 2e 20 49 74 0a 20 20 2a 2a 20 69 73 20  ing. It.  ** is 
ed270 62 65 74 74 65 72 20 74 6f 20 75 73 65 20 75 6e  better to use un
ed280 69 78 4f 70 65 6e 28 29 20 69 6e 73 74 65 61 64  ixOpen() instead
ed290 20 6f 66 20 6f 70 65 6e 69 6e 67 20 74 68 65 20   of opening the 
ed2a0 66 69 6c 65 20 64 69 72 65 63 74 6c 79 20 77 69  file directly wi
ed2b0 74 68 0a 20 20 2a 2a 20 6f 70 65 6e 28 29 2c 20  th.  ** open(), 
ed2c0 61 73 20 75 6e 69 78 4f 70 65 6e 28 29 20 73 65  as unixOpen() se
ed2d0 74 73 20 75 70 20 74 68 65 20 76 61 72 69 6f 75  ts up the variou
ed2e0 73 20 6d 65 63 68 61 6e 69 73 6d 73 20 72 65 71  s mechanisms req
ed2f0 75 69 72 65 64 20 74 6f 0a 20 20 2a 2a 20 6d 61  uired to.  ** ma
ed300 6b 65 20 73 75 72 65 20 61 20 63 61 6c 6c 20 74  ke sure a call t
ed310 6f 20 63 6c 6f 73 65 28 29 20 64 6f 65 73 20 6e  o close() does n
ed320 6f 74 20 63 61 75 73 65 20 74 68 65 20 73 79 73  ot cause the sys
ed330 74 65 6d 20 74 6f 20 64 69 73 63 61 72 64 0a 20  tem to discard. 
ed340 20 2a 2a 20 50 4f 53 49 58 20 6c 6f 63 6b 73 20   ** POSIX locks 
ed350 70 72 65 6d 61 74 75 72 65 6c 79 2e 0a 20 20 2a  prematurely..  *
ed360 2a 0a 20 20 2a 2a 20 49 74 20 69 73 20 69 6d 70  *.  ** It is imp
ed370 6f 72 74 61 6e 74 20 74 68 61 74 20 74 68 65 20  ortant that the 
ed380 78 4f 70 65 6e 20 6d 65 6d 62 65 72 20 6f 66 20  xOpen member of 
ed390 74 68 65 20 56 46 53 20 6f 62 6a 65 63 74 20 70  the VFS object p
ed3a0 61 73 73 65 64 20 74 6f 20 0a 20 20 2a 2a 20 75  assed to .  ** u
ed3b0 6e 69 78 4f 70 65 6e 28 29 20 69 73 20 4e 55 4c  nixOpen() is NUL
ed3c0 4c 2e 20 54 68 69 73 20 74 65 6c 6c 73 20 75 6e  L. This tells un
ed3d0 69 78 4f 70 65 6e 28 29 20 6d 61 79 20 74 72 79  ixOpen() may try
ed3e0 20 74 6f 20 6f 70 65 6e 20 61 20 70 72 6f 78 79   to open a proxy
ed3f0 2d 66 69 6c 65 20 0a 20 20 2a 2a 20 66 6f 72 20  -file .  ** for 
ed400 74 68 65 20 70 72 6f 78 79 2d 66 69 6c 65 20 28  the proxy-file (
ed410 63 72 65 61 74 69 6e 67 20 61 20 70 6f 74 65 6e  creating a poten
ed420 74 69 61 6c 20 69 6e 66 69 6e 69 74 65 20 6c 6f  tial infinite lo
ed430 6f 70 29 2e 0a 20 20 2a 2f 0a 20 20 64 75 6d 6d  op)..  */.  dumm
ed440 79 56 66 73 2e 70 41 70 70 44 61 74 61 20 3d 20  yVfs.pAppData = 
ed450 28 76 6f 69 64 2a 29 26 61 75 74 6f 6c 6f 63 6b  (void*)&autolock
ed460 49 6f 46 69 6e 64 65 72 3b 0a 20 20 64 75 6d 6d  IoFinder;.  dumm
ed470 79 56 66 73 2e 78 4f 70 65 6e 20 3d 20 30 3b 0a  yVfs.xOpen = 0;.
ed480 20 20 72 63 20 3d 20 75 6e 69 78 4f 70 65 6e 28    rc = unixOpen(
ed490 26 64 75 6d 6d 79 56 66 73 2c 20 70 61 74 68 2c  &dummyVfs, path,
ed4a0 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a   (sqlite3_file *
ed4b0 29 70 4e 65 77 2c 20 66 6c 61 67 73 2c 20 26 66  )pNew, flags, &f
ed4c0 6c 61 67 73 29 3b 0a 20 20 69 66 28 20 72 63 3d  lags);.  if( rc=
ed4d0 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 28 66  =SQLITE_OK && (f
ed4e0 6c 61 67 73 26 53 51 4c 49 54 45 5f 4f 50 45 4e  lags&SQLITE_OPEN
ed4f0 5f 52 45 41 44 4f 4e 4c 59 29 20 29 7b 0a 20 20  _READONLY) ){.  
ed500 20 20 70 4e 65 77 2d 3e 70 4d 65 74 68 6f 64 2d    pNew->pMethod-
ed510 3e 78 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33  >xClose((sqlite3
ed520 5f 66 69 6c 65 20 2a 29 70 4e 65 77 29 3b 0a 20  _file *)pNew);. 
ed530 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43     rc = SQLITE_C
ed540 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a 0a 20 20  ANTOPEN;.  }..  
ed550 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
ed560 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  K ){.    sqlite3
ed570 5f 66 72 65 65 28 70 4e 65 77 29 3b 0a 20 20 20  _free(pNew);.   
ed580 20 70 4e 65 77 20 3d 20 30 3b 0a 20 20 7d 0a 0a   pNew = 0;.  }..
ed590 20 20 2a 70 70 46 69 6c 65 20 3d 20 70 4e 65 77    *ppFile = pNew
ed5a0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
ed5b0 0a 0a 2f 2a 20 74 61 6b 65 73 20 74 68 65 20 63  ../* takes the c
ed5c0 6f 6e 63 68 20 62 79 20 74 61 6b 69 6e 67 20 61  onch by taking a
ed5d0 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 6e 64   shared lock and
ed5e0 20 72 65 61 64 20 74 68 65 20 63 6f 6e 74 65 6e   read the conten
ed5f0 74 73 20 63 6f 6e 63 68 2c 20 69 66 20 0a 2a 2a  ts conch, if .**
ed600 20 6c 6f 63 6b 50 61 74 68 20 69 73 20 6e 6f 6e   lockPath is non
ed610 2d 4e 55 4c 4c 2c 20 74 68 65 20 68 6f 73 74 20  -NULL, the host 
ed620 49 44 20 61 6e 64 20 6c 6f 63 6b 20 66 69 6c 65  ID and lock file
ed630 20 70 61 74 68 20 6d 75 73 74 20 6d 61 74 63 68   path must match
ed640 2e 20 20 41 20 4e 55 4c 4c 20 0a 2a 2a 20 6c 6f  .  A NULL .** lo
ed650 63 6b 50 61 74 68 20 6d 65 61 6e 73 20 74 68 61  ckPath means tha
ed660 74 20 74 68 65 20 6c 6f 63 6b 50 61 74 68 20 69  t the lockPath i
ed670 6e 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65  n the conch file
ed680 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 69 66   will be used if
ed690 20 74 68 65 20 0a 2a 2a 20 68 6f 73 74 20 49 44   the .** host ID
ed6a0 73 20 6d 61 74 63 68 2c 20 6f 72 20 61 20 6e 65  s match, or a ne
ed6b0 77 20 6c 6f 63 6b 20 70 61 74 68 20 77 69 6c 6c  w lock path will
ed6c0 20 62 65 20 67 65 6e 65 72 61 74 65 64 20 61 75   be generated au
ed6d0 74 6f 6d 61 74 69 63 61 6c 6c 79 20 0a 2a 2a 20  tomatically .** 
ed6e0 61 6e 64 20 77 72 69 74 74 65 6e 20 74 6f 20 74  and written to t
ed6f0 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 2e 0a 2a  he conch file..*
ed700 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f  /.static int pro
ed710 78 79 54 61 6b 65 43 6f 6e 63 68 28 75 6e 69 78  xyTakeConch(unix
ed720 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20  File *pFile){.  
ed730 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74  proxyLockingCont
ed740 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f  ext *pCtx = (pro
ed750 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  xyLockingContext
ed760 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e   *)pFile->lockin
ed770 67 43 6f 6e 74 65 78 74 3b 20 0a 20 20 0a 20 20  gContext; .  .  
ed780 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48  if( pCtx->conchH
ed790 65 6c 64 3e 30 20 29 7b 0a 20 20 20 20 72 65 74  eld>0 ){.    ret
ed7a0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
ed7b0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e 69 78   }else{.    unix
ed7c0 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 20  File *conchFile 
ed7d0 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c  = pCtx->conchFil
ed7e0 65 3b 0a 20 20 20 20 63 68 61 72 20 74 65 73 74  e;.    char test
ed7f0 56 61 6c 75 65 5b 43 4f 4e 43 48 4c 45 4e 5d 3b  Value[CONCHLEN];
ed800 0a 20 20 20 20 63 68 61 72 20 63 6f 6e 63 68 56  .    char conchV
ed810 61 6c 75 65 5b 43 4f 4e 43 48 4c 45 4e 5d 3b 0a  alue[CONCHLEN];.
ed820 20 20 20 20 63 68 61 72 20 6c 6f 63 6b 50 61 74      char lockPat
ed830 68 5b 4d 41 58 50 41 54 48 4c 45 4e 5d 3b 0a 20  h[MAXPATHLEN];. 
ed840 20 20 20 63 68 61 72 20 2a 74 4c 6f 63 6b 50 61     char *tLockPa
ed850 74 68 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69  th = NULL;.    i
ed860 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
ed870 4b 3b 0a 20 20 20 20 69 6e 74 20 72 65 61 64 52  K;.    int readR
ed880 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
ed890 20 20 20 69 6e 74 20 73 79 6e 63 50 65 72 6d 73     int syncPerms
ed8a0 20 3d 20 30 3b 0a 0a 20 20 20 20 4f 53 54 52 41   = 0;..    OSTRA
ed8b0 43 45 34 28 22 54 41 4b 45 43 4f 4e 43 48 20 20  CE4("TAKECONCH  
ed8c0 25 64 20 66 6f 72 20 25 73 20 70 69 64 3d 25 64  %d for %s pid=%d
ed8d0 5c 6e 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d 3e  \n", conchFile->
ed8e0 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  h,.             
ed8f0 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79  (pCtx->lockProxy
ed900 50 61 74 68 20 3f 20 70 43 74 78 2d 3e 6c 6f 63  Path ? pCtx->loc
ed910 6b 50 72 6f 78 79 50 61 74 68 20 3a 20 22 3a 61  kProxyPath : ":a
ed920 75 74 6f 3a 22 29 2c 20 67 65 74 70 69 64 28 29  uto:"), getpid()
ed930 29 3b 0a 0a 20 20 20 20 72 63 20 3d 20 63 6f 6e  );..    rc = con
ed940 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d  chFile->pMethod-
ed950 3e 78 4c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f  >xLock((sqlite3_
ed960 66 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c  file*)conchFile,
ed970 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20   SHARED_LOCK);. 
ed980 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
ed990 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e  E_OK ){.      in
ed9a0 74 20 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20  t pError = 0;.  
ed9b0 20 20 20 20 6d 65 6d 73 65 74 28 74 65 73 74 56      memset(testV
ed9c0 61 6c 75 65 2c 20 30 2c 20 43 4f 4e 43 48 4c 45  alue, 0, CONCHLE
ed9d0 4e 29 3b 20 2f 2a 20 63 6f 6e 63 68 20 69 73 20  N); /* conch is 
ed9e0 66 69 78 65 64 20 73 69 7a 65 20 2a 2f 0a 20 20  fixed size */.  
ed9f0 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 47 65      rc = proxyGe
eda00 74 48 6f 73 74 49 44 28 74 65 73 74 56 61 6c 75  tHostID(testValu
eda10 65 2c 20 26 70 45 72 72 6f 72 29 3b 0a 20 20 20  e, &pError);.   
eda20 20 20 20 69 66 28 20 28 72 63 26 30 78 66 66 29     if( (rc&0xff)
eda30 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29  ==SQLITE_IOERR )
eda40 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d  {.        pFile-
eda50 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 70 45 72  >lastErrno = pEr
eda60 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ror;.      }.   
eda70 20 20 20 69 66 28 20 70 43 74 78 2d 3e 6c 6f 63     if( pCtx->loc
eda80 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 20  kProxyPath ){.  
eda90 20 20 20 20 20 20 73 74 72 6c 63 70 79 28 26 74        strlcpy(&t
edaa0 65 73 74 56 61 6c 75 65 5b 48 4f 53 54 49 44 4c  estValue[HOSTIDL
edab0 45 4e 5d 2c 20 70 43 74 78 2d 3e 6c 6f 63 6b 50  EN], pCtx->lockP
edac0 72 6f 78 79 50 61 74 68 2c 20 4d 41 58 50 41 54  roxyPath, MAXPAT
edad0 48 4c 45 4e 29 3b 0a 20 20 20 20 20 20 7d 0a 20  HLEN);.      }. 
edae0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21     }.    if( rc!
edaf0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
edb00 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b      goto end_tak
edb10 65 63 6f 6e 63 68 3b 0a 20 20 20 20 7d 0a 20 20  econch;.    }.  
edb20 20 20 0a 20 20 20 20 72 65 61 64 52 63 20 3d 20    .    readRc = 
edb30 75 6e 69 78 52 65 61 64 28 28 73 71 6c 69 74 65  unixRead((sqlite
edb40 33 5f 66 69 6c 65 20 2a 29 63 6f 6e 63 68 46 69  3_file *)conchFi
edb50 6c 65 2c 20 63 6f 6e 63 68 56 61 6c 75 65 2c 20  le, conchValue, 
edb60 43 4f 4e 43 48 4c 45 4e 2c 20 30 29 3b 0a 20 20  CONCHLEN, 0);.  
edb70 20 20 69 66 28 20 72 65 61 64 52 63 21 3d 53 51    if( readRc!=SQ
edb80 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54  LITE_IOERR_SHORT
edb90 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 20 20 69  _READ ){.      i
edba0 66 28 20 72 65 61 64 52 63 21 3d 53 51 4c 49 54  f( readRc!=SQLIT
edbb0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
edbc0 69 66 28 20 28 72 63 26 30 78 66 66 29 3d 3d 53  if( (rc&0xff)==S
edbd0 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20  QLITE_IOERR ){. 
edbe0 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e           pFile->
edbf0 6c 61 73 74 45 72 72 6e 6f 20 3d 20 63 6f 6e 63  lastErrno = conc
edc00 68 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  hFile->lastErrno
edc10 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
edc20 20 20 20 20 72 63 20 3d 20 72 65 61 64 52 63 3b      rc = readRc;
edc30 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e  .        goto en
edc40 64 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20  d_takeconch;.   
edc50 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 69 66     }.      /* if
edc60 20 74 68 65 20 63 6f 6e 63 68 20 68 61 73 20 64   the conch has d
edc70 61 74 61 20 63 6f 6d 70 61 72 65 20 74 68 65 20  ata compare the 
edc80 63 6f 6e 74 65 6e 74 73 20 2a 2f 0a 20 20 20 20  contents */.    
edc90 20 20 69 66 28 20 21 70 43 74 78 2d 3e 6c 6f 63    if( !pCtx->loc
edca0 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 20  kProxyPath ){.  
edcb0 20 20 20 20 20 20 2f 2a 20 66 6f 72 20 61 75 74        /* for aut
edcc0 6f 2d 6e 61 6d 65 64 20 6c 6f 63 61 6c 20 6c 6f  o-named local lo
edcd0 63 6b 20 66 69 6c 65 2c 20 6a 75 73 74 20 63 68  ck file, just ch
edce0 65 63 6b 20 74 68 65 20 68 6f 73 74 20 49 44 20  eck the host ID 
edcf0 61 6e 64 20 77 65 27 6c 6c 0a 20 20 20 20 20 20  and we'll.      
edd00 20 20 20 2a 2a 20 75 73 65 20 74 68 65 20 6c 6f     ** use the lo
edd10 63 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 20 70 61  cal lock file pa
edd20 74 68 20 74 68 61 74 27 73 20 61 6c 72 65 61 64  th that's alread
edd30 79 20 69 6e 20 74 68 65 72 65 20 2a 2f 0a 20 20  y in there */.  
edd40 20 20 20 20 20 20 69 66 28 20 21 6d 65 6d 63 6d        if( !memcm
edd50 70 28 74 65 73 74 56 61 6c 75 65 2c 20 63 6f 6e  p(testValue, con
edd60 63 68 56 61 6c 75 65 2c 20 48 4f 53 54 49 44 4c  chValue, HOSTIDL
edd70 45 4e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  EN) ){.         
edd80 20 74 4c 6f 63 6b 50 61 74 68 20 3d 20 28 63 68   tLockPath = (ch
edd90 61 72 20 2a 29 26 63 6f 6e 63 68 56 61 6c 75 65  ar *)&conchValue
edda0 5b 48 4f 53 54 49 44 4c 45 4e 5d 3b 0a 20 20 20  [HOSTIDLEN];.   
eddb0 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f         goto end_
eddc0 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 20 20  takeconch;.     
eddd0 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
edde0 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 77 65 27  {.        /* we'
eddf0 76 65 20 67 6f 74 20 74 68 65 20 63 6f 6e 63 68  ve got the conch
ede00 20 69 66 20 63 6f 6e 63 68 56 61 6c 75 65 20 6d   if conchValue m
ede10 61 74 63 68 65 73 20 6f 75 72 20 70 61 74 68 20  atches our path 
ede20 61 6e 64 20 68 6f 73 74 20 49 44 20 2a 2f 0a 20  and host ID */. 
ede30 20 20 20 20 20 20 20 69 66 28 20 21 6d 65 6d 63         if( !memc
ede40 6d 70 28 74 65 73 74 56 61 6c 75 65 2c 20 63 6f  mp(testValue, co
ede50 6e 63 68 56 61 6c 75 65 2c 20 43 4f 4e 43 48 4c  nchValue, CONCHL
ede60 45 4e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  EN) ){.         
ede70 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 63 6f   goto end_takeco
ede80 6e 63 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  nch;.        }. 
ede90 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
edea0 7b 0a 20 20 20 20 20 20 2f 2a 20 61 20 73 68 6f  {.      /* a sho
edeb0 72 74 20 72 65 61 64 20 6d 65 61 6e 73 20 77 65  rt read means we
edec0 27 72 65 20 22 63 72 65 61 74 69 6e 67 22 20 74  're "creating" t
eded0 68 65 20 63 6f 6e 63 68 20 28 65 76 65 6e 20 74  he conch (even t
edee0 68 6f 75 67 68 20 69 74 20 63 6f 75 6c 64 20 0a  hough it could .
edef0 20 20 20 20 20 20 2a 2a 20 68 61 76 65 20 62 65        ** have be
edf00 65 6e 20 75 73 65 72 2d 69 6e 74 65 72 76 65 6e  en user-interven
edf10 74 69 6f 6e 29 2c 20 69 66 20 77 65 20 61 63 71  tion), if we acq
edf20 75 69 72 65 20 74 68 65 20 65 78 63 6c 75 73 69  uire the exclusi
edf30 76 65 20 6c 6f 63 6b 2c 0a 20 20 20 20 20 20 2a  ve lock,.      *
edf40 2a 20 77 65 27 6c 6c 20 74 72 79 20 74 6f 20 6d  * we'll try to m
edf50 61 74 63 68 20 74 68 65 20 63 75 72 72 65 6e 74  atch the current
edf60 20 6f 6e 2d 64 69 73 6b 20 70 65 72 6d 69 73 73   on-disk permiss
edf70 69 6f 6e 73 20 6f 66 20 74 68 65 20 64 61 74 61  ions of the data
edf80 62 61 73 65 0a 20 20 20 20 20 20 2a 2f 0a 20 20  base.      */.  
edf90 20 20 20 20 73 79 6e 63 50 65 72 6d 73 20 3d 20      syncPerms = 
edfa0 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20  1;.    }.    .  
edfb0 20 20 2f 2a 20 65 69 74 68 65 72 20 63 6f 6e 63    /* either conc
edfc0 68 20 77 61 73 20 65 6d 74 70 79 20 6f 72 20 64  h was emtpy or d
edfd0 69 64 6e 27 74 20 6d 61 74 63 68 20 2a 2f 0a 20  idn't match */. 
edfe0 20 20 20 69 66 28 20 21 70 43 74 78 2d 3e 6c 6f     if( !pCtx->lo
edff0 63 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20  ckProxyPath ){. 
ee000 20 20 20 20 20 70 72 6f 78 79 47 65 74 4c 6f 63       proxyGetLoc
ee010 6b 50 61 74 68 28 70 43 74 78 2d 3e 64 62 50 61  kPath(pCtx->dbPa
ee020 74 68 2c 20 6c 6f 63 6b 50 61 74 68 2c 20 4d 41  th, lockPath, MA
ee030 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20 20 20 20  XPATHLEN);.     
ee040 20 74 4c 6f 63 6b 50 61 74 68 20 3d 20 6c 6f 63   tLockPath = loc
ee050 6b 50 61 74 68 3b 0a 20 20 20 20 20 20 73 74 72  kPath;.      str
ee060 6c 63 70 79 28 26 74 65 73 74 56 61 6c 75 65 5b  lcpy(&testValue[
ee070 48 4f 53 54 49 44 4c 45 4e 5d 2c 20 6c 6f 63 6b  HOSTIDLEN], lock
ee080 50 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e  Path, MAXPATHLEN
ee090 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20  );.    }.    .  
ee0a0 20 20 2f 2a 20 75 70 64 61 74 65 20 63 6f 6e 63    /* update conc
ee0b0 68 20 77 69 74 68 20 68 6f 73 74 20 61 6e 64 20  h with host and 
ee0c0 70 61 74 68 20 28 74 68 69 73 20 77 69 6c 6c 20  path (this will 
ee0d0 66 61 69 6c 20 69 66 20 6f 74 68 65 72 20 70 72  fail if other pr
ee0e0 6f 63 65 73 73 0a 20 20 20 20 20 2a 2a 20 68 61  ocess.     ** ha
ee0f0 73 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20  s a shared lock 
ee100 61 6c 72 65 61 64 79 29 20 2a 2f 0a 20 20 20 20  already) */.    
ee110 72 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e  rc = conchFile->
ee120 70 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28  pMethod->xLock((
ee130 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f  sqlite3_file*)co
ee140 6e 63 68 46 69 6c 65 2c 20 45 58 43 4c 55 53 49  nchFile, EXCLUSI
ee150 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66  VE_LOCK);.    if
ee160 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
ee170 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 75 6e  ){.      rc = un
ee180 69 78 57 72 69 74 65 28 28 73 71 6c 69 74 65 33  ixWrite((sqlite3
ee190 5f 66 69 6c 65 20 2a 29 63 6f 6e 63 68 46 69 6c  _file *)conchFil
ee1a0 65 2c 20 74 65 73 74 56 61 6c 75 65 2c 20 43 4f  e, testValue, CO
ee1b0 4e 43 48 4c 45 4e 2c 20 30 29 3b 0a 20 20 20 20  NCHLEN, 0);.    
ee1c0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
ee1d0 5f 4f 4b 20 26 26 20 73 79 6e 63 50 65 72 6d 73  _OK && syncPerms
ee1e0 20 29 7b 0a 20 20 20 20 20 20 20 20 73 74 72 75   ){.        stru
ee1f0 63 74 20 73 74 61 74 20 62 75 66 3b 0a 20 20 20  ct stat buf;.   
ee200 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d 20 66       int err = f
ee210 73 74 61 74 28 70 46 69 6c 65 2d 3e 68 2c 20 26  stat(pFile->h, &
ee220 62 75 66 29 3b 0a 20 20 20 20 20 20 20 20 69 66  buf);.        if
ee230 28 20 65 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20  ( err==0 ){.    
ee240 20 20 20 20 20 20 2f 2a 20 74 72 79 20 74 6f 20        /* try to 
ee250 6d 61 74 63 68 20 74 68 65 20 64 61 74 61 62 61  match the databa
ee260 73 65 20 66 69 6c 65 20 70 65 72 6d 69 73 73 69  se file permissi
ee270 6f 6e 73 2c 20 69 67 6e 6f 72 65 20 66 61 69 6c  ons, ignore fail
ee280 75 72 65 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ure */.#ifndef S
ee290 51 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 55  QLITE_PROXY_DEBU
ee2a0 47 0a 20 20 20 20 20 20 20 20 20 20 66 63 68 6d  G.          fchm
ee2b0 6f 64 28 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c  od(conchFile->h,
ee2c0 20 62 75 66 2e 73 74 5f 6d 6f 64 65 29 3b 0a 23   buf.st_mode);.#
ee2d0 65 6c 73 65 0a 20 20 20 20 20 20 20 20 20 20 69  else.          i
ee2e0 66 28 20 66 63 68 6d 6f 64 28 63 6f 6e 63 68 46  f( fchmod(conchF
ee2f0 69 6c 65 2d 3e 68 2c 20 62 75 66 2e 73 74 5f 6d  ile->h, buf.st_m
ee300 6f 64 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  ode)!=0 ){.     
ee310 20 20 20 20 20 20 20 69 6e 74 20 63 6f 64 65 20         int code 
ee320 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20  = errno;.       
ee330 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64       fprintf(std
ee340 65 72 72 2c 20 22 66 63 68 6d 6f 64 20 25 6f 20  err, "fchmod %o 
ee350 46 41 49 4c 45 44 20 77 69 74 68 20 25 64 20 25  FAILED with %d %
ee360 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  s\n",.          
ee370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ee380 20 20 20 62 75 66 2e 73 74 5f 6d 6f 64 65 2c 20     buf.st_mode, 
ee390 63 6f 64 65 2c 20 73 74 72 65 72 72 6f 72 28 63  code, strerror(c
ee3a0 6f 64 65 29 29 3b 0a 20 20 20 20 20 20 20 20 20  ode));.         
ee3b0 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20   } else {.      
ee3c0 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74        fprintf(st
ee3d0 64 65 72 72 2c 20 22 66 63 68 6d 6f 64 20 25 6f  derr, "fchmod %o
ee3e0 20 53 55 43 43 45 44 45 44 5c 6e 22 2c 62 75 66   SUCCEDED\n",buf
ee3f0 2e 73 74 5f 6d 6f 64 65 29 3b 0a 20 20 20 20 20  .st_mode);.     
ee400 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
ee410 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
ee420 69 6e 74 20 63 6f 64 65 20 3d 20 65 72 72 6e 6f  int code = errno
ee430 3b 0a 20 20 20 20 20 20 20 20 20 20 66 70 72 69  ;.          fpri
ee440 6e 74 66 28 73 74 64 65 72 72 2c 20 22 53 54 41  ntf(stderr, "STA
ee450 54 20 46 41 49 4c 45 44 5b 25 64 5d 20 77 69 74  T FAILED[%d] wit
ee460 68 20 25 64 20 25 73 5c 6e 22 2c 20 0a 20 20 20  h %d %s\n", .   
ee470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ee480 20 20 20 20 20 20 20 65 72 72 2c 20 63 6f 64 65         err, code
ee490 2c 20 73 74 72 65 72 72 6f 72 28 63 6f 64 65 29  , strerror(code)
ee4a0 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20  );.#endif.      
ee4b0 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
ee4c0 7d 0a 20 20 20 20 63 6f 6e 63 68 46 69 6c 65 2d  }.    conchFile-
ee4d0 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63  >pMethod->xUnloc
ee4e0 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  k((sqlite3_file*
ee4f0 29 63 6f 6e 63 68 46 69 6c 65 2c 20 53 48 41 52  )conchFile, SHAR
ee500 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 0a 65 6e 64  ED_LOCK);.  .end
ee510 5f 74 61 6b 65 63 6f 6e 63 68 3a 0a 20 20 20 20  _takeconch:.    
ee520 4f 53 54 52 41 43 45 32 28 22 54 52 41 4e 53 50  OSTRACE2("TRANSP
ee530 52 4f 58 59 3a 20 43 4c 4f 53 45 20 20 25 64 5c  ROXY: CLOSE  %d\
ee540 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20  n", pFile->h);. 
ee550 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
ee560 45 5f 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6f  E_OK && pFile->o
ee570 70 65 6e 46 6c 61 67 73 20 29 7b 0a 20 20 20 20  penFlags ){.    
ee580 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 68 3e 3d    if( pFile->h>=
ee590 30 20 29 7b 0a 23 69 66 64 65 66 20 53 54 52 49  0 ){.#ifdef STRI
ee5a0 43 54 5f 43 4c 4f 53 45 5f 45 52 52 4f 52 0a 20  CT_CLOSE_ERROR. 
ee5b0 20 20 20 20 20 20 20 69 66 28 20 63 6c 6f 73 65         if( close
ee5c0 28 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a 20 20  (pFile->h) ){.  
ee5d0 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c          pFile->l
ee5e0 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f  astErrno = errno
ee5f0 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  ;.          retu
ee600 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
ee610 43 4c 4f 53 45 3b 0a 20 20 20 20 20 20 20 20 7d  CLOSE;.        }
ee620 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20 63  .#else.        c
ee630 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 68 29 3b 20  lose(pFile->h); 
ee640 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b  /* silently leak
ee650 20 66 64 20 69 66 20 66 61 69 6c 20 2a 2f 0a 23   fd if fail */.#
ee660 65 6e 64 69 66 0a 20 20 20 20 20 20 7d 0a 20 20  endif.      }.  
ee670 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 2d      pFile->h = -
ee680 31 3b 0a 20 20 20 20 20 20 69 6e 74 20 66 64 20  1;.      int fd 
ee690 3d 20 6f 70 65 6e 28 70 43 74 78 2d 3e 64 62 50  = open(pCtx->dbP
ee6a0 61 74 68 2c 20 70 46 69 6c 65 2d 3e 6f 70 65 6e  ath, pFile->open
ee6b0 46 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20  Flags,.         
ee6c0 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
ee6d0 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50  E_DEFAULT_FILE_P
ee6e0 45 52 4d 49 53 53 49 4f 4e 53 29 3b 0a 20 20 20  ERMISSIONS);.   
ee6f0 20 20 20 4f 53 54 52 41 43 45 32 28 22 54 52 41     OSTRACE2("TRA
ee700 4e 53 50 52 4f 58 59 3a 20 4f 50 45 4e 20 20 25  NSPROXY: OPEN  %
ee710 64 5c 6e 22 2c 20 66 64 29 3b 0a 20 20 20 20 20  d\n", fd);.     
ee720 20 69 66 28 20 66 64 3e 3d 30 20 29 7b 0a 20 20   if( fd>=0 ){.  
ee730 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d        pFile->h =
ee740 20 66 64 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65   fd;.      }else
ee750 7b 0a 20 20 20 20 20 20 20 20 72 63 3d 53 51 4c  {.        rc=SQL
ee760 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 20 2f 2a  ITE_CANTOPEN; /*
ee770 20 53 51 4c 49 54 45 5f 42 55 53 59 3f 20 70 72   SQLITE_BUSY? pr
ee780 6f 78 79 54 61 6b 65 43 6f 6e 63 68 20 63 61 6c  oxyTakeConch cal
ee790 6c 65 64 0a 20 20 20 20 20 20 20 20 20 20 20 20  led.            
ee7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ee7b0 20 20 20 64 75 72 69 6e 67 20 6c 6f 63 6b 69 6e     during lockin
ee7c0 67 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20  g */.      }.   
ee7d0 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53   }.    if( rc==S
ee7e0 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 70 43 74  QLITE_OK && !pCt
ee7f0 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 20 29 7b 0a  x->lockProxy ){.
ee800 20 20 20 20 20 20 63 68 61 72 20 2a 70 61 74 68        char *path
ee810 20 3d 20 74 4c 6f 63 6b 50 61 74 68 20 3f 20 74   = tLockPath ? t
ee820 4c 6f 63 6b 50 61 74 68 20 3a 20 70 43 74 78 2d  LockPath : pCtx-
ee830 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b 0a  >lockProxyPath;.
ee840 20 20 20 20 20 20 2f 2a 20 41 43 53 3a 20 4e 65        /* ACS: Ne
ee850 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 63 6f 70  ed to make a cop
ee860 79 20 6f 66 20 70 61 74 68 20 73 6f 6d 65 74 69  y of path someti
ee870 6d 65 73 20 2a 2f 0a 20 20 20 20 20 20 72 63 20  mes */.      rc 
ee880 3d 20 70 72 6f 78 79 43 72 65 61 74 65 55 6e 69  = proxyCreateUni
ee890 78 46 69 6c 65 28 70 61 74 68 2c 20 26 70 43 74  xFile(path, &pCt
ee8a0 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20  x->lockProxy);. 
ee8b0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d     }.    if( rc=
ee8c0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
ee8d0 20 20 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48      pCtx->conchH
ee8e0 65 6c 64 20 3d 20 31 3b 0a 0a 20 20 20 20 20 20  eld = 1;..      
ee8f0 69 66 28 20 74 4c 6f 63 6b 50 61 74 68 20 29 7b  if( tLockPath ){
ee900 0a 20 20 20 20 20 20 20 20 70 43 74 78 2d 3e 6c  .        pCtx->l
ee910 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3d 20 73  ockProxyPath = s
ee920 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 30  qlite3DbStrDup(0
ee930 2c 20 74 4c 6f 63 6b 50 61 74 68 29 3b 0a 20 20  , tLockPath);.  
ee940 20 20 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e        if( pCtx->
ee950 6c 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d 65 74 68  lockProxy->pMeth
ee960 6f 64 20 3d 3d 20 26 61 66 70 49 6f 4d 65 74 68  od == &afpIoMeth
ee970 6f 64 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ods ){.         
ee980 20 28 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e   ((afpLockingCon
ee990 74 65 78 74 20 2a 29 70 43 74 78 2d 3e 6c 6f 63  text *)pCtx->loc
ee9a0 6b 50 72 6f 78 79 2d 3e 6c 6f 63 6b 69 6e 67 43  kProxy->lockingC
ee9b0 6f 6e 74 65 78 74 29 2d 3e 64 62 50 61 74 68 20  ontext)->dbPath 
ee9c0 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  =.              
ee9d0 20 20 20 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63         pCtx->loc
ee9e0 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 20 20  kProxyPath;.    
ee9f0 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
eea00 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20    } else {.     
eea10 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74   conchFile->pMet
eea20 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71  hod->xUnlock((sq
eea30 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63  lite3_file*)conc
eea40 68 46 69 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b  hFile, NO_LOCK);
eea50 0a 20 20 20 20 7d 0a 20 20 20 20 4f 53 54 52 41  .    }.    OSTRA
eea60 43 45 33 28 22 54 41 4b 45 43 4f 4e 43 48 20 20  CE3("TAKECONCH  
eea70 25 64 20 25 73 5c 6e 22 2c 20 63 6f 6e 63 68 46  %d %s\n", conchF
eea80 69 6c 65 2d 3e 68 2c 20 72 63 3d 3d 53 51 4c 49  ile->h, rc==SQLI
eea90 54 45 5f 4f 4b 3f 22 6f 6b 22 3a 22 66 61 69 6c  TE_OK?"ok":"fail
eeaa0 65 64 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ed");.    return
eeab0 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a   rc;.  }.}../*.*
eeac0 2a 20 49 66 20 70 46 69 6c 65 20 68 6f 6c 64 73  * If pFile holds
eead0 20 61 20 6c 6f 63 6b 20 6f 6e 20 61 20 63 6f 6e   a lock on a con
eeae0 63 68 20 66 69 6c 65 2c 20 74 68 65 6e 20 72 65  ch file, then re
eeaf0 6c 65 61 73 65 20 74 68 61 74 20 6c 6f 63 6b 2e  lease that lock.
eeb00 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
eeb10 72 6f 78 79 52 65 6c 65 61 73 65 43 6f 6e 63 68  roxyReleaseConch
eeb20 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65  (unixFile *pFile
eeb30 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  ){.  int rc;    
eeb40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
eeb50 20 2f 2a 20 53 75 62 72 6f 75 74 69 6e 65 20 72   /* Subroutine r
eeb60 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
eeb70 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74  proxyLockingCont
eeb80 65 78 74 20 2a 70 43 74 78 3b 20 20 2f 2a 20 54  ext *pCtx;  /* T
eeb90 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65  he locking conte
eeba0 78 74 20 66 6f 72 20 74 68 65 20 70 72 6f 78 79  xt for the proxy
eebb0 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 69 78 46   lock */.  unixF
eebc0 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 3b 20  ile *conchFile; 
eebd0 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
eebe0 66 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65  f the conch file
eebf0 20 2a 2f 0a 0a 20 20 70 43 74 78 20 3d 20 28 70   */..  pCtx = (p
eec00 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  roxyLockingConte
eec10 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b  xt *)pFile->lock
eec20 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 63 6f  ingContext;.  co
eec30 6e 63 68 46 69 6c 65 20 3d 20 70 43 74 78 2d 3e  nchFile = pCtx->
eec40 63 6f 6e 63 68 46 69 6c 65 3b 0a 20 20 4f 53 54  conchFile;.  OST
eec50 52 41 43 45 34 28 22 52 45 4c 45 41 53 45 43 4f  RACE4("RELEASECO
eec60 4e 43 48 20 20 25 64 20 66 6f 72 20 25 73 20 70  NCH  %d for %s p
eec70 69 64 3d 25 64 5c 6e 22 2c 20 63 6f 6e 63 68 46  id=%d\n", conchF
eec80 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20  ile->h,.        
eec90 20 20 20 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 72     (pCtx->lockPr
eeca0 6f 78 79 50 61 74 68 20 3f 20 70 43 74 78 2d 3e  oxyPath ? pCtx->
eecb0 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3a 20  lockProxyPath : 
eecc0 22 3a 61 75 74 6f 3a 22 29 2c 20 0a 20 20 20 20  ":auto:"), .    
eecd0 20 20 20 20 20 20 20 67 65 74 70 69 64 28 29 29         getpid())
eece0 3b 0a 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48  ;.  pCtx->conchH
eecf0 65 6c 64 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20  eld = 0;.  rc = 
eed00 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68  conchFile->pMeth
eed10 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c  od->xUnlock((sql
eed20 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68  ite3_file*)conch
eed30 46 69 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a  File, NO_LOCK);.
eed40 20 20 4f 53 54 52 41 43 45 33 28 22 52 45 4c 45    OSTRACE3("RELE
eed50 41 53 45 43 4f 4e 43 48 20 20 25 64 20 25 73 5c  ASECONCH  %d %s\
eed60 6e 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 68  n", conchFile->h
eed70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 28 72 63  ,.           (rc
eed80 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f  ==SQLITE_OK ? "o
eed90 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 29 3b  k" : "failed"));
eeda0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
eedb0 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65  ./*.** Given the
eedc0 20 6e 61 6d 65 20 6f 66 20 61 20 64 61 74 61 62   name of a datab
eedd0 61 73 65 20 66 69 6c 65 2c 20 63 6f 6d 70 75 74  ase file, comput
eede0 65 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 69 74  e the name of it
eedf0 73 20 63 6f 6e 63 68 20 66 69 6c 65 2e 0a 2a 2a  s conch file..**
eee00 20 53 74 6f 72 65 20 74 68 65 20 63 6f 6e 63 68   Store the conch
eee10 20 66 69 6c 65 6e 61 6d 65 20 69 6e 20 6d 65 6d   filename in mem
eee20 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ory obtained fro
eee30 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  m sqlite3_malloc
eee40 28 29 2e 0a 2a 2a 20 4d 61 6b 65 20 2a 70 43 6f  ()..** Make *pCo
eee50 6e 63 68 50 61 74 68 20 70 6f 69 6e 74 20 74 6f  nchPath point to
eee60 20 74 68 65 20 6e 65 77 20 6e 61 6d 65 2e 20 20   the new name.  
eee70 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
eee80 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f   on success.** o
eee90 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69  r SQLITE_NOMEM i
eeea0 66 20 75 6e 61 62 6c 65 20 74 6f 20 6f 62 74 61  f unable to obta
eeeb0 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a  in memory..**.**
eeec0 20 54 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72   The caller is r
eeed0 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 65  esponsible for e
eeee0 6e 73 75 72 69 6e 67 20 74 68 61 74 20 74 68 65  nsuring that the
eeef0 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72   allocated memor
eef00 79 0a 2a 2a 20 73 70 61 63 65 20 69 73 20 65 76  y.** space is ev
eef10 65 6e 74 75 61 6c 6c 79 20 66 72 65 65 64 2e 0a  entually freed..
eef20 2a 2a 0a 2a 2a 20 2a 70 43 6f 6e 63 68 50 61 74  **.** *pConchPat
eef30 68 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c  h is set to NULL
eef40 20 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c   if a memory all
eef50 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63  ocation error oc
eef60 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  curs..*/.static 
eef70 69 6e 74 20 70 72 6f 78 79 43 72 65 61 74 65 43  int proxyCreateC
eef80 6f 6e 63 68 50 61 74 68 6e 61 6d 65 28 63 68 61  onchPathname(cha
eef90 72 20 2a 64 62 50 61 74 68 2c 20 63 68 61 72 20  r *dbPath, char 
eefa0 2a 2a 70 43 6f 6e 63 68 50 61 74 68 29 7b 0a 20  **pConchPath){. 
eefb0 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
eefc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
eefd0 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
eefe0 2f 0a 20 20 69 6e 74 20 6c 65 6e 20 3d 20 28 69  /.  int len = (i
eeff0 6e 74 29 73 74 72 6c 65 6e 28 64 62 50 61 74 68  nt)strlen(dbPath
ef000 29 3b 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20  ); /* Length of 
ef010 64 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d  database filenam
ef020 65 20 2d 20 64 62 50 61 74 68 20 2a 2f 0a 20 20  e - dbPath */.  
ef030 63 68 61 72 20 2a 63 6f 6e 63 68 50 61 74 68 3b  char *conchPath;
ef040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
ef050 20 62 75 66 66 65 72 20 69 6e 20 77 68 69 63 68   buffer in which
ef060 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20 63 6f   to construct co
ef070 6e 63 68 20 6e 61 6d 65 20 2a 2f 0a 0a 20 20 2f  nch name */..  /
ef080 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65  * Allocate space
ef090 20 66 6f 72 20 74 68 65 20 63 6f 6e 63 68 20 66   for the conch f
ef0a0 69 6c 65 6e 61 6d 65 20 61 6e 64 20 69 6e 69 74  ilename and init
ef0b0 69 61 6c 69 7a 65 20 74 68 65 20 6e 61 6d 65 20  ialize the name 
ef0c0 74 6f 0a 20 20 2a 2a 20 74 68 65 20 6e 61 6d 65  to.  ** the name
ef0d0 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c   of the original
ef0e0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
ef0f0 2a 2f 20 20 0a 20 20 2a 70 43 6f 6e 63 68 50 61  */  .  *pConchPa
ef100 74 68 20 3d 20 63 6f 6e 63 68 50 61 74 68 20 3d  th = conchPath =
ef110 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33   (char *)sqlite3
ef120 5f 6d 61 6c 6c 6f 63 28 6c 65 6e 20 2b 20 38 29  _malloc(len + 8)
ef130 3b 0a 20 20 69 66 28 20 63 6f 6e 63 68 50 61 74  ;.  if( conchPat
ef140 68 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  h==0 ){.    retu
ef150 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
ef160 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 63 6f  .  }.  memcpy(co
ef170 6e 63 68 50 61 74 68 2c 20 64 62 50 61 74 68 2c  nchPath, dbPath,
ef180 20 6c 65 6e 2b 31 29 3b 0a 20 20 0a 20 20 2f 2a   len+1);.  .  /*
ef190 20 6e 6f 77 20 69 6e 73 65 72 74 20 61 20 22 2e   now insert a ".
ef1a0 22 20 62 65 66 6f 72 65 20 74 68 65 20 6c 61 73  " before the las
ef1b0 74 20 2f 20 63 68 61 72 61 63 74 65 72 20 2a 2f  t / character */
ef1c0 0a 20 20 66 6f 72 28 20 69 3d 28 6c 65 6e 2d 31  .  for( i=(len-1
ef1d0 29 3b 20 69 3e 3d 30 3b 20 69 2d 2d 20 29 7b 0a  ); i>=0; i-- ){.
ef1e0 20 20 20 20 69 66 28 20 63 6f 6e 63 68 50 61 74      if( conchPat
ef1f0 68 5b 69 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20  h[i]=='/' ){.   
ef200 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 62 72     i++;.      br
ef210 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  eak;.    }.  }. 
ef220 20 63 6f 6e 63 68 50 61 74 68 5b 69 5d 3d 27 2e   conchPath[i]='.
ef230 27 3b 0a 20 20 77 68 69 6c 65 20 28 20 69 3c 6c  ';.  while ( i<l
ef240 65 6e 20 29 7b 0a 20 20 20 20 63 6f 6e 63 68 50  en ){.    conchP
ef250 61 74 68 5b 69 2b 31 5d 3d 64 62 50 61 74 68 5b  ath[i+1]=dbPath[
ef260 69 5d 3b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d  i];.    i++;.  }
ef270 0a 0a 20 20 2f 2a 20 61 70 70 65 6e 64 20 74 68  ..  /* append th
ef280 65 20 22 2d 63 6f 6e 63 68 22 20 73 75 66 66 69  e "-conch" suffi
ef290 78 20 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f  x to the file */
ef2a0 0a 20 20 6d 65 6d 63 70 79 28 26 63 6f 6e 63 68  .  memcpy(&conch
ef2b0 50 61 74 68 5b 69 2b 31 5d 2c 20 22 2d 63 6f 6e  Path[i+1], "-con
ef2c0 63 68 22 2c 20 37 29 3b 0a 20 20 61 73 73 65 72  ch", 7);.  asser
ef2d0 74 28 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 63  t( (int)strlen(c
ef2e0 6f 6e 63 68 50 61 74 68 29 20 3d 3d 20 6c 65 6e  onchPath) == len
ef2f0 2b 37 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  +7 );..  return 
ef300 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f  SQLITE_OK;.}.../
ef310 2a 20 54 61 6b 65 73 20 61 20 66 75 6c 6c 79 20  * Takes a fully 
ef320 63 6f 6e 66 69 67 75 72 65 64 20 70 72 6f 78 79  configured proxy
ef330 20 6c 6f 63 6b 69 6e 67 2d 73 74 79 6c 65 20 75   locking-style u
ef340 6e 69 78 20 66 69 6c 65 20 61 6e 64 20 73 77 69  nix file and swi
ef350 74 63 68 65 73 0a 2a 2a 20 74 68 65 20 6c 6f 63  tches.** the loc
ef360 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 20 70 61 74  al lock file pat
ef370 68 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  h .*/.static int
ef380 20 73 77 69 74 63 68 4c 6f 63 6b 50 72 6f 78 79   switchLockProxy
ef390 50 61 74 68 28 75 6e 69 78 46 69 6c 65 20 2a 70  Path(unixFile *p
ef3a0 46 69 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72  File, const char
ef3b0 20 2a 70 61 74 68 29 20 7b 0a 20 20 70 72 6f 78   *path) {.  prox
ef3c0 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  yLockingContext 
ef3d0 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f  *pCtx = (proxyLo
ef3e0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2a 29 70 46  ckingContext*)pF
ef3f0 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74  ile->lockingCont
ef400 65 78 74 3b 0a 20 20 63 68 61 72 20 2a 6f 6c 64  ext;.  char *old
ef410 50 61 74 68 20 3d 20 70 43 74 78 2d 3e 6c 6f 63  Path = pCtx->loc
ef420 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 69 6e  kProxyPath;.  in
ef430 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
ef440 3b 0a 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  ;..  if( pFile->
ef450 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43  locktype!=NO_LOC
ef460 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  K ){.    return 
ef470 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d  SQLITE_BUSY;.  }
ef480 20 20 0a 0a 20 20 2f 2a 20 6e 6f 74 68 69 6e 67    ..  /* nothing
ef490 20 74 6f 20 64 6f 20 69 66 20 74 68 65 20 70 61   to do if the pa
ef4a0 74 68 20 69 73 20 4e 55 4c 4c 2c 20 3a 61 75 74  th is NULL, :aut
ef4b0 6f 3a 20 6f 72 20 6d 61 74 63 68 65 73 20 74 68  o: or matches th
ef4c0 65 20 65 78 69 73 74 69 6e 67 20 70 61 74 68 20  e existing path 
ef4d0 2a 2f 0a 20 20 69 66 28 20 21 70 61 74 68 20 7c  */.  if( !path |
ef4e0 7c 20 70 61 74 68 5b 30 5d 3d 3d 27 5c 30 27 20  | path[0]=='\0' 
ef4f0 7c 7c 20 21 73 74 72 63 6d 70 28 70 61 74 68 2c  || !strcmp(path,
ef500 20 22 3a 61 75 74 6f 3a 22 29 20 7c 7c 0a 20 20   ":auto:") ||.  
ef510 20 20 28 6f 6c 64 50 61 74 68 20 26 26 20 21 73    (oldPath && !s
ef520 74 72 6e 63 6d 70 28 6f 6c 64 50 61 74 68 2c 20  trncmp(oldPath, 
ef530 70 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e  path, MAXPATHLEN
ef540 29 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  )) ){.    return
ef550 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65   SQLITE_OK;.  }e
ef560 6c 73 65 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c  lse{.    unixFil
ef570 65 20 2a 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 70  e *lockProxy = p
ef580 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a  Ctx->lockProxy;.
ef590 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72      pCtx->lockPr
ef5a0 6f 78 79 3d 4e 55 4c 4c 3b 0a 20 20 20 20 70 43  oxy=NULL;.    pC
ef5b0 74 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 20 3d 20  tx->conchHeld = 
ef5c0 30 3b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 50  0;.    if( lockP
ef5d0 72 6f 78 79 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20  roxy!=NULL ){.  
ef5e0 20 20 20 20 72 63 3d 6c 6f 63 6b 50 72 6f 78 79      rc=lockProxy
ef5f0 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73  ->pMethod->xClos
ef600 65 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  e((sqlite3_file 
ef610 2a 29 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20  *)lockProxy);.  
ef620 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
ef630 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 73 71  urn rc;.      sq
ef640 6c 69 74 65 33 5f 66 72 65 65 28 6c 6f 63 6b 50  lite3_free(lockP
ef650 72 6f 78 79 29 3b 0a 20 20 20 20 7d 0a 20 20 20  roxy);.    }.   
ef660 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6f 6c   sqlite3_free(ol
ef670 64 50 61 74 68 29 3b 0a 20 20 20 20 70 43 74 78  dPath);.    pCtx
ef680 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20  ->lockProxyPath 
ef690 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
ef6a0 70 28 30 2c 20 70 61 74 68 29 3b 0a 20 20 7d 0a  p(0, path);.  }.
ef6b0 20 20 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a    .  return rc;.
ef6c0 7d 0a 0a 2f 2a 0a 2a 2a 20 70 46 69 6c 65 20 69  }../*.** pFile i
ef6d0 73 20 61 20 66 69 6c 65 20 74 68 61 74 20 68 61  s a file that ha
ef6e0 73 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 62 79  s been opened by
ef6f0 20 61 20 70 72 69 6f 72 20 78 4f 70 65 6e 20 63   a prior xOpen c
ef700 61 6c 6c 2e 20 20 64 62 50 61 74 68 0a 2a 2a 20  all.  dbPath.** 
ef710 69 73 20 61 20 73 74 72 69 6e 67 20 62 75 66 66  is a string buff
ef720 65 72 20 61 74 20 6c 65 61 73 74 20 4d 41 58 50  er at least MAXP
ef730 41 54 48 4c 45 4e 2b 31 20 63 68 61 72 61 63 74  ATHLEN+1 charact
ef740 65 72 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 0a  ers in size..**.
ef750 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
ef760 66 69 6e 64 20 74 68 65 20 66 69 6c 65 6e 61 6d  find the filenam
ef770 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  e associated wit
ef780 68 20 70 46 69 6c 65 20 61 6e 64 20 77 72 69 74  h pFile and writ
ef790 65 73 20 69 74 0a 2a 2a 20 69 6e 74 20 64 62 50  es it.** int dbP
ef7a0 61 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ath..*/.static i
ef7b0 6e 74 20 70 72 6f 78 79 47 65 74 44 62 50 61 74  nt proxyGetDbPat
ef7c0 68 46 6f 72 55 6e 69 78 46 69 6c 65 28 75 6e 69  hForUnixFile(uni
ef7d0 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 63 68  xFile *pFile, ch
ef7e0 61 72 20 2a 64 62 50 61 74 68 29 7b 0a 23 69 66  ar *dbPath){.#if
ef7f0 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45   defined(__APPLE
ef800 5f 5f 29 0a 20 20 69 66 28 20 70 46 69 6c 65 2d  __).  if( pFile-
ef810 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26 61 66 70  >pMethod == &afp
ef820 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20  IoMethods ){.   
ef830 20 2f 2a 20 61 66 70 20 73 74 79 6c 65 20 6b 65   /* afp style ke
ef840 65 70 73 20 61 20 72 65 66 65 72 65 6e 63 65 20  eps a reference 
ef850 74 6f 20 74 68 65 20 64 62 20 70 61 74 68 20 69  to the db path i
ef860 6e 20 74 68 65 20 66 69 6c 65 50 61 74 68 20 66  n the filePath f
ef870 69 65 6c 64 20 0a 20 20 20 20 2a 2a 20 6f 66 20  ield .    ** of 
ef880 74 68 65 20 73 74 72 75 63 74 20 2a 2f 0a 20 20  the struct */.  
ef890 20 20 61 73 73 65 72 74 28 20 28 69 6e 74 29 73    assert( (int)s
ef8a0 74 72 6c 65 6e 28 28 63 68 61 72 2a 29 70 46 69  trlen((char*)pFi
ef8b0 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
ef8c0 78 74 29 3c 3d 4d 41 58 50 41 54 48 4c 45 4e 20  xt)<=MAXPATHLEN 
ef8d0 29 3b 0a 20 20 20 20 73 74 72 63 70 79 28 64 62  );.    strcpy(db
ef8e0 50 61 74 68 2c 20 28 28 61 66 70 4c 6f 63 6b 69  Path, ((afpLocki
ef8f0 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c  ngContext *)pFil
ef900 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
ef910 74 29 2d 3e 64 62 50 61 74 68 29 3b 0a 20 20 7d  t)->dbPath);.  }
ef920 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 69 66  else.#endif.  if
ef930 28 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64  ( pFile->pMethod
ef940 20 3d 3d 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65   == &dotlockIoMe
ef950 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20  thods ){.    /* 
ef960 64 6f 74 20 6c 6f 63 6b 20 73 74 79 6c 65 20 75  dot lock style u
ef970 73 65 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20  ses the locking 
ef980 63 6f 6e 74 65 78 74 20 74 6f 20 73 74 6f 72 65  context to store
ef990 20 74 68 65 20 64 6f 74 20 6c 6f 63 6b 0a 20 20   the dot lock.  
ef9a0 20 20 2a 2a 20 66 69 6c 65 20 70 61 74 68 20 2a    ** file path *
ef9b0 2f 0a 20 20 20 20 69 6e 74 20 6c 65 6e 20 3d 20  /.    int len = 
ef9c0 73 74 72 6c 65 6e 28 28 63 68 61 72 20 2a 29 70  strlen((char *)p
ef9d0 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e  File->lockingCon
ef9e0 74 65 78 74 29 20 2d 20 73 74 72 6c 65 6e 28 44  text) - strlen(D
ef9f0 4f 54 4c 4f 43 4b 5f 53 55 46 46 49 58 29 3b 0a  OTLOCK_SUFFIX);.
efa00 20 20 20 20 6d 65 6d 63 70 79 28 64 62 50 61 74      memcpy(dbPat
efa10 68 2c 20 28 63 68 61 72 20 2a 29 70 46 69 6c 65  h, (char *)pFile
efa20 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ->lockingContext
efa30 2c 20 6c 65 6e 20 2b 20 31 29 3b 0a 20 20 7d 65  , len + 1);.  }e
efa40 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 61 6c 6c 20  lse{.    /* all 
efa50 6f 74 68 65 72 20 73 74 79 6c 65 73 20 75 73 65  other styles use
efa60 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e   the locking con
efa70 74 65 78 74 20 74 6f 20 73 74 6f 72 65 20 74 68  text to store th
efa80 65 20 64 62 20 66 69 6c 65 20 70 61 74 68 20 2a  e db file path *
efa90 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 74  /.    assert( st
efaa0 72 6c 65 6e 28 28 63 68 61 72 2a 29 70 46 69 6c  rlen((char*)pFil
efab0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
efac0 74 29 3c 3d 4d 41 58 50 41 54 48 4c 45 4e 20 29  t)<=MAXPATHLEN )
efad0 3b 0a 20 20 20 20 73 74 72 63 70 79 28 64 62 50  ;.    strcpy(dbP
efae0 61 74 68 2c 20 28 63 68 61 72 20 2a 29 70 46 69  ath, (char *)pFi
efaf0 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
efb00 78 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  xt);.  }.  retur
efb10 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
efb20 2f 2a 0a 2a 2a 20 54 61 6b 65 73 20 61 6e 20 61  /*.** Takes an a
efb30 6c 72 65 61 64 79 20 66 69 6c 6c 65 64 20 69 6e  lready filled in
efb40 20 75 6e 69 78 20 66 69 6c 65 20 61 6e 64 20 61   unix file and a
efb50 6c 74 65 72 73 20 69 74 20 73 6f 20 61 6c 6c 20  lters it so all 
efb60 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 0a 2a 2a  file locking .**
efb70 20 77 69 6c 6c 20 62 65 20 70 65 72 66 6f 72 6d   will be perform
efb80 65 64 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20  ed on the local 
efb90 70 72 6f 78 79 20 6c 6f 63 6b 20 66 69 6c 65 2e  proxy lock file.
efba0 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
efbb0 66 69 65 6c 64 73 0a 2a 2a 20 61 72 65 20 70 72  fields.** are pr
efbc0 65 73 65 72 76 65 64 20 69 6e 20 74 68 65 20 6c  eserved in the l
efbd0 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 73  ocking context s
efbe0 6f 20 74 68 61 74 20 74 68 65 79 20 63 61 6e 20  o that they can 
efbf0 62 65 20 72 65 73 74 6f 72 65 64 20 61 6e 64 20  be restored and 
efc00 0a 2a 2a 20 74 68 65 20 75 6e 69 78 20 73 74 72  .** the unix str
efc10 75 63 74 75 72 65 20 70 72 6f 70 65 72 6c 79 20  ucture properly 
efc20 63 6c 65 61 6e 65 64 20 75 70 20 61 74 20 63 6c  cleaned up at cl
efc30 6f 73 65 20 74 69 6d 65 3a 0a 2a 2a 20 20 2d 3e  ose time:.**  ->
efc40 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 0a 2a  lockingContext.*
efc50 2a 20 20 2d 3e 70 4d 65 74 68 6f 64 0a 2a 2f 0a  *  ->pMethod.*/.
efc60 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79  static int proxy
efc70 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c  TransformUnixFil
efc80 65 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c  e(unixFile *pFil
efc90 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  e, const char *p
efca0 61 74 68 29 20 7b 0a 20 20 70 72 6f 78 79 4c 6f  ath) {.  proxyLo
efcb0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43  ckingContext *pC
efcc0 74 78 3b 0a 20 20 63 68 61 72 20 64 62 50 61 74  tx;.  char dbPat
efcd0 68 5b 4d 41 58 50 41 54 48 4c 45 4e 2b 31 5d 3b  h[MAXPATHLEN+1];
efce0 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
efcf0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  f the database f
efd00 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 6c  ile */.  char *l
efd10 6f 63 6b 50 61 74 68 3d 4e 55 4c 4c 3b 0a 20 20  ockPath=NULL;.  
efd20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
efd30 4f 4b 3b 0a 20 20 0a 20 20 69 66 28 20 70 46 69  OK;.  .  if( pFi
efd40 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f  le->locktype!=NO
efd50 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 74  _LOCK ){.    ret
efd60 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  urn SQLITE_BUSY;
efd70 0a 20 20 7d 0a 20 20 70 72 6f 78 79 47 65 74 44  .  }.  proxyGetD
efd80 62 50 61 74 68 46 6f 72 55 6e 69 78 46 69 6c 65  bPathForUnixFile
efd90 28 70 46 69 6c 65 2c 20 64 62 50 61 74 68 29 3b  (pFile, dbPath);
efda0 0a 20 20 69 66 28 20 21 70 61 74 68 20 7c 7c 20  .  if( !path || 
efdb0 70 61 74 68 5b 30 5d 3d 3d 27 5c 30 27 20 7c 7c  path[0]=='\0' ||
efdc0 20 21 73 74 72 63 6d 70 28 70 61 74 68 2c 20 22   !strcmp(path, "
efdd0 3a 61 75 74 6f 3a 22 29 20 29 7b 0a 20 20 20 20  :auto:") ){.    
efde0 6c 6f 63 6b 50 61 74 68 3d 4e 55 4c 4c 3b 0a 20  lockPath=NULL;. 
efdf0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6c 6f 63 6b   }else{.    lock
efe00 50 61 74 68 3d 28 63 68 61 72 20 2a 29 70 61 74  Path=(char *)pat
efe10 68 3b 0a 20 20 7d 0a 20 20 0a 20 20 4f 53 54 52  h;.  }.  .  OSTR
efe20 41 43 45 34 28 22 54 52 41 4e 53 50 52 4f 58 59  ACE4("TRANSPROXY
efe30 20 20 25 64 20 66 6f 72 20 25 73 20 70 69 64 3d    %d for %s pid=
efe40 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %d\n", pFile->h,
efe50 0a 20 20 20 20 20 20 20 20 20 20 20 28 6c 6f 63  .           (loc
efe60 6b 50 61 74 68 20 3f 20 6c 6f 63 6b 50 61 74 68  kPath ? lockPath
efe70 20 3a 20 22 3a 61 75 74 6f 3a 22 29 2c 20 67 65   : ":auto:"), ge
efe80 74 70 69 64 28 29 29 3b 0a 0a 20 20 70 43 74 78  tpid());..  pCtx
efe90 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
efea0 63 28 20 73 69 7a 65 6f 66 28 2a 70 43 74 78 29  c( sizeof(*pCtx)
efeb0 20 29 3b 0a 20 20 69 66 28 20 70 43 74 78 3d 3d   );.  if( pCtx==
efec0 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
efed0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
efee0 7d 0a 20 20 6d 65 6d 73 65 74 28 70 43 74 78 2c  }.  memset(pCtx,
efef0 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 43 74 78   0, sizeof(*pCtx
eff00 29 29 3b 0a 0a 20 20 72 63 20 3d 20 70 72 6f 78  ));..  rc = prox
eff10 79 43 72 65 61 74 65 43 6f 6e 63 68 50 61 74 68  yCreateConchPath
eff20 6e 61 6d 65 28 64 62 50 61 74 68 2c 20 26 70 43  name(dbPath, &pC
eff30 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74  tx->conchFilePat
eff40 68 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  h);.  if( rc==SQ
eff50 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
eff60 63 20 3d 20 70 72 6f 78 79 43 72 65 61 74 65 55  c = proxyCreateU
eff70 6e 69 78 46 69 6c 65 28 70 43 74 78 2d 3e 63 6f  nixFile(pCtx->co
eff80 6e 63 68 46 69 6c 65 50 61 74 68 2c 20 26 70 43  nchFilePath, &pC
eff90 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 29 3b 0a  tx->conchFile);.
effa0 20 20 7d 20 20 0a 20 20 69 66 28 20 72 63 3d 3d    }  .  if( rc==
effb0 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6c 6f 63  SQLITE_OK && loc
effc0 6b 50 61 74 68 20 29 7b 0a 20 20 20 20 70 43 74  kPath ){.    pCt
effd0 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68  x->lockProxyPath
effe0 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44   = sqlite3DbStrD
efff0 75 70 28 30 2c 20 6c 6f 63 6b 50 61 74 68 29 3b  up(0, lockPath);
f0000 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d  .  }..  if( rc==
f0010 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
f0020 20 2f 2a 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 69   /* all memory i
f0030 73 20 61 6c 6c 6f 63 61 74 65 64 2c 20 70 72 6f  s allocated, pro
f0040 78 79 73 20 61 72 65 20 63 72 65 61 74 65 64 20  xys are created 
f0050 61 6e 64 20 61 73 73 69 67 6e 65 64 2c 20 0a 20  and assigned, . 
f0060 20 20 20 2a 2a 20 73 77 69 74 63 68 20 74 68 65     ** switch the
f0070 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74   locking context
f0080 20 61 6e 64 20 70 4d 65 74 68 6f 64 20 74 68 65   and pMethod the
f0090 6e 20 72 65 74 75 72 6e 2e 0a 20 20 20 20 2a 2f  n return..    */
f00a0 0a 20 20 20 20 70 43 74 78 2d 3e 64 62 50 61 74  .    pCtx->dbPat
f00b0 68 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  h = sqlite3DbStr
f00c0 44 75 70 28 30 2c 20 64 62 50 61 74 68 29 3b 0a  Dup(0, dbPath);.
f00d0 20 20 20 20 70 43 74 78 2d 3e 6f 6c 64 4c 6f 63      pCtx->oldLoc
f00e0 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 46  kingContext = pF
f00f0 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74  ile->lockingCont
f0100 65 78 74 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  ext;.    pFile->
f0110 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d  lockingContext =
f0120 20 70 43 74 78 3b 0a 20 20 20 20 70 43 74 78 2d   pCtx;.    pCtx-
f0130 3e 70 4f 6c 64 4d 65 74 68 6f 64 20 3d 20 70 46  >pOldMethod = pF
f0140 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 3b 0a 20 20  ile->pMethod;.  
f0150 20 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64    pFile->pMethod
f0160 20 3d 20 26 70 72 6f 78 79 49 6f 4d 65 74 68 6f   = &proxyIoMetho
f0170 64 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ds;.  }else{.   
f0180 20 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68   if( pCtx->conch
f0190 46 69 6c 65 20 29 7b 20 0a 20 20 20 20 20 20 72  File ){ .      r
f01a0 63 20 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46  c = pCtx->conchF
f01b0 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43  ile->pMethod->xC
f01c0 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f 66 69  lose((sqlite3_fi
f01d0 6c 65 20 2a 29 70 43 74 78 2d 3e 63 6f 6e 63 68  le *)pCtx->conch
f01e0 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 69 66 28  File);.      if(
f01f0 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
f0200 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
f0210 72 65 65 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46  ree(pCtx->conchF
f0220 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ile);.    }.    
f0230 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74  sqlite3_free(pCt
f0240 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74 68  x->conchFilePath
f0250 29 3b 20 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ); .    sqlite3_
f0260 66 72 65 65 28 70 43 74 78 29 3b 0a 20 20 7d 0a  free(pCtx);.  }.
f0270 20 20 4f 53 54 52 41 43 45 33 28 22 54 52 41 4e    OSTRACE3("TRAN
f0280 53 50 52 4f 58 59 20 20 25 64 20 25 73 5c 6e 22  SPROXY  %d %s\n"
f0290 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20  , pFile->h,.    
f02a0 20 20 20 20 20 20 20 28 72 63 3d 3d 53 51 4c 49         (rc==SQLI
f02b0 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22  TE_OK ? "ok" : "
f02c0 66 61 69 6c 65 64 22 29 29 3b 0a 20 20 72 65 74  failed"));.  ret
f02d0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  urn rc;.}.../*.*
f02e0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 68  * This routine h
f02f0 61 6e 64 6c 65 73 20 73 71 6c 69 74 65 33 5f 66  andles sqlite3_f
f0300 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 20 63 61  ile_control() ca
f0310 6c 6c 73 20 74 68 61 74 20 61 72 65 20 73 70 65  lls that are spe
f0320 63 69 66 69 63 0a 2a 2a 20 74 6f 20 70 72 6f 78  cific.** to prox
f0330 79 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74  y locking..*/.st
f0340 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 46 69  atic int proxyFi
f0350 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65  leControl(sqlite
f0360 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
f0370 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b  op, void *pArg){
f0380 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b  .  switch( op ){
f0390 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
f03a0 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49  _GET_LOCKPROXYFI
f03b0 4c 45 3a 20 7b 0a 20 20 20 20 20 20 75 6e 69 78  LE: {.      unix
f03c0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
f03d0 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20  nixFile*)id;.   
f03e0 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4d     if( pFile->pM
f03f0 65 74 68 6f 64 20 3d 3d 20 26 70 72 6f 78 79 49  ethod == &proxyI
f0400 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20  oMethods ){.    
f0410 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67      proxyLocking
f0420 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20  Context *pCtx = 
f0430 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e  (proxyLockingCon
f0440 74 65 78 74 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63  text*)pFile->loc
f0450 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20  kingContext;.   
f0460 20 20 20 20 20 70 72 6f 78 79 54 61 6b 65 43 6f       proxyTakeCo
f0470 6e 63 68 28 70 46 69 6c 65 29 3b 0a 20 20 20 20  nch(pFile);.    
f0480 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e 6c 6f      if( pCtx->lo
f0490 63 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20  ckProxyPath ){. 
f04a0 20 20 20 20 20 20 20 20 20 2a 28 63 6f 6e 73 74           *(const
f04b0 20 63 68 61 72 20 2a 2a 29 70 41 72 67 20 3d 20   char **)pArg = 
f04c0 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50  pCtx->lockProxyP
f04d0 61 74 68 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  ath;.        }el
f04e0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28  se{.          *(
f04f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 29 70 41  const char **)pA
f0500 72 67 20 3d 20 22 3a 61 75 74 6f 3a 20 28 6e 6f  rg = ":auto: (no
f0510 74 20 68 65 6c 64 29 22 3b 0a 20 20 20 20 20 20  t held)";.      
f0520 20 20 7d 0a 20 20 20 20 20 20 7d 20 65 6c 73 65    }.      } else
f0530 20 7b 0a 20 20 20 20 20 20 20 20 2a 28 63 6f 6e   {.        *(con
f0540 73 74 20 63 68 61 72 20 2a 2a 29 70 41 72 67 20  st char **)pArg 
f0550 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 7d 0a  = NULL;.      }.
f0560 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
f0570 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20  ITE_OK;.    }.  
f0580 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 53 45    case SQLITE_SE
f0590 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 3a  T_LOCKPROXYFILE:
f05a0 20 7b 0a 20 20 20 20 20 20 75 6e 69 78 46 69 6c   {.      unixFil
f05b0 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78  e *pFile = (unix
f05c0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 20 20  File*)id;.      
f05d0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
f05e0 4f 4b 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 73  OK;.      int is
f05f0 50 72 6f 78 79 53 74 79 6c 65 20 3d 20 28 70 46  ProxyStyle = (pF
f0600 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20  ile->pMethod == 
f0610 26 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64 73 29  &proxyIoMethods)
f0620 3b 0a 20 20 20 20 20 20 69 66 28 20 70 41 72 67  ;.      if( pArg
f0630 3d 3d 4e 55 4c 4c 20 7c 7c 20 28 63 6f 6e 73 74  ==NULL || (const
f0640 20 63 68 61 72 20 2a 29 70 41 72 67 3d 3d 30 20   char *)pArg==0 
f0650 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  ){.        if( i
f0660 73 50 72 6f 78 79 53 74 79 6c 65 20 29 7b 0a 20  sProxyStyle ){. 
f0670 20 20 20 20 20 20 20 20 20 2f 2a 20 74 75 72 6e           /* turn
f0680 20 6f 66 66 20 70 72 6f 78 79 20 6c 6f 63 6b 69   off proxy locki
f0690 6e 67 20 2d 20 6e 6f 74 20 73 75 70 70 6f 72 74  ng - not support
f06a0 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  ed */.          
f06b0 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
f06c0 52 20 2f 2a 53 51 4c 49 54 45 5f 50 52 4f 54 4f  R /*SQLITE_PROTO
f06d0 43 4f 4c 3f 20 53 51 4c 49 54 45 5f 4d 49 53 55  COL? SQLITE_MISU
f06e0 53 45 3f 2a 2f 3b 0a 20 20 20 20 20 20 20 20 7d  SE?*/;.        }
f06f0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
f0700 2f 2a 20 74 75 72 6e 20 6f 66 66 20 70 72 6f 78  /* turn off prox
f0710 79 20 6c 6f 63 6b 69 6e 67 20 2d 20 61 6c 72 65  y locking - alre
f0720 61 64 79 20 6f 66 66 20 2d 20 4e 4f 4f 50 20 2a  ady off - NOOP *
f0730 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  /.          rc =
f0740 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
f0750 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73      }.      }els
f0760 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74  e{.        const
f0770 20 63 68 61 72 20 2a 70 72 6f 78 79 50 61 74 68   char *proxyPath
f0780 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a   = (const char *
f0790 29 70 41 72 67 3b 0a 20 20 20 20 20 20 20 20 69  )pArg;.        i
f07a0 66 28 20 69 73 50 72 6f 78 79 53 74 79 6c 65 20  f( isProxyStyle 
f07b0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 72 6f  ){.          pro
f07c0 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  xyLockingContext
f07d0 20 2a 70 43 74 78 20 3d 20 0a 20 20 20 20 20 20   *pCtx = .      
f07e0 20 20 20 20 20 20 28 70 72 6f 78 79 4c 6f 63 6b        (proxyLock
f07f0 69 6e 67 43 6f 6e 74 65 78 74 2a 29 70 46 69 6c  ingContext*)pFil
f0800 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
f0810 74 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  t;.          if(
f0820 20 21 73 74 72 63 6d 70 28 70 41 72 67 2c 20 22   !strcmp(pArg, "
f0830 3a 61 75 74 6f 3a 22 29 20 0a 20 20 20 20 20 20  :auto:") .      
f0840 20 20 20 20 20 7c 7c 20 28 70 43 74 78 2d 3e 6c       || (pCtx->l
f0850 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 26 26 0a  ockProxyPath &&.
f0860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 21                 !
f0870 73 74 72 6e 63 6d 70 28 70 43 74 78 2d 3e 6c 6f  strncmp(pCtx->lo
f0880 63 6b 50 72 6f 78 79 50 61 74 68 2c 20 70 72 6f  ckProxyPath, pro
f0890 78 79 50 61 74 68 2c 20 4d 41 58 50 41 54 48 4c  xyPath, MAXPATHL
f08a0 45 4e 29 29 0a 20 20 20 20 20 20 20 20 20 20 29  EN)).          )
f08b0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63  {.            rc
f08c0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
f08d0 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
f08e0 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20             rc = 
f08f0 73 77 69 74 63 68 4c 6f 63 6b 50 72 6f 78 79 50  switchLockProxyP
f0900 61 74 68 28 70 46 69 6c 65 2c 20 70 72 6f 78 79  ath(pFile, proxy
f0910 50 61 74 68 29 3b 0a 20 20 20 20 20 20 20 20 20  Path);.         
f0920 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65   }.        }else
f0930 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74  {.          /* t
f0940 75 72 6e 20 6f 6e 20 70 72 6f 78 79 20 66 69 6c  urn on proxy fil
f0950 65 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 20  e locking */.   
f0960 20 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78         rc = prox
f0970 79 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69  yTransformUnixFi
f0980 6c 65 28 70 46 69 6c 65 2c 20 70 72 6f 78 79 50  le(pFile, proxyP
f0990 61 74 68 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ath);.        }.
f09a0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65        }.      re
f09b0 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
f09c0 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
f09d0 20 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b      assert( 0 );
f09e0 20 20 2f 2a 20 54 68 65 20 63 61 6c 6c 20 61 73    /* The call as
f09f0 73 75 72 65 73 20 74 68 61 74 20 6f 6e 6c 79 20  sures that only 
f0a00 76 61 6c 69 64 20 6f 70 63 6f 64 65 73 20 61 72  valid opcodes ar
f0a10 65 20 73 65 6e 74 20 2a 2f 0a 20 20 20 20 7d 0a  e sent */.    }.
f0a20 20 20 7d 0a 20 20 2f 2a 4e 4f 54 52 45 41 43 48    }.  /*NOTREACH
f0a30 45 44 2a 2f 0a 20 20 72 65 74 75 72 6e 20 53 51  ED*/.  return SQ
f0a40 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f  LITE_ERROR;.}../
f0a50 2a 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68 69 73  *.** Within this
f0a60 20 64 69 76 69 73 69 6f 6e 20 28 74 68 65 20 70   division (the p
f0a70 72 6f 78 79 69 6e 67 20 6c 6f 63 6b 69 6e 67 20  roxying locking 
f0a80 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 29 20  implementation) 
f0a90 74 68 65 20 70 72 6f 63 65 64 75 72 65 73 0a 2a  the procedures.*
f0aa0 2a 20 61 62 6f 76 65 20 74 68 69 73 20 70 6f 69  * above this poi
f0ab0 6e 74 20 61 72 65 20 61 6c 6c 20 75 74 69 6c 69  nt are all utili
f0ac0 74 69 65 73 2e 20 20 54 68 65 20 6c 6f 63 6b 2d  ties.  The lock-
f0ad0 72 65 6c 61 74 65 64 20 6d 65 74 68 6f 64 73 20  related methods 
f0ae0 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 78 79 2d  of the.** proxy-
f0af0 6c 6f 63 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f  locking sqlite3_
f0b00 69 6f 5f 6d 65 74 68 6f 64 20 6f 62 6a 65 63 74  io_method object
f0b10 20 66 6f 6c 6c 6f 77 2e 0a 2a 2f 0a 0a 0a 2f 2a   follow..*/.../*
f0b20 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
f0b30 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65   checks if there
f0b40 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c   is a RESERVED l
f0b50 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20  ock held on the 
f0b60 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c  specified.** fil
f0b70 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79  e by this or any
f0b80 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20   other process. 
f0b90 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69  If such a lock i
f0ba0 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65  s held, set *pRe
f0bb0 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e  sOut.** to a non
f0bc0 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65  -zero value othe
f0bd0 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69  rwise *pResOut i
f0be0 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20  s set to zero.  
f0bf0 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
f0c00 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51  .** is set to SQ
f0c10 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61  LITE_OK unless a
f0c20 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75  n I/O error occu
f0c30 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63  rs during lock c
f0c40 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74  hecking..*/.stat
f0c50 69 63 20 69 6e 74 20 70 72 6f 78 79 43 68 65 63  ic int proxyChec
f0c60 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71  kReservedLock(sq
f0c70 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
f0c80 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 7b 0a  int *pResOut) {.
f0c90 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
f0ca0 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
f0cb0 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 72  d;.  int rc = pr
f0cc0 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69  oxyTakeConch(pFi
f0cd0 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  le);.  if( rc==S
f0ce0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
f0cf0 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74  proxyLockingCont
f0d00 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f  ext *pCtx = (pro
f0d10 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  xyLockingContext
f0d20 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e   *)pFile->lockin
f0d30 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e  gContext;.    un
f0d40 69 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20  ixFile *proxy = 
f0d50 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b  pCtx->lockProxy;
f0d60 0a 20 20 20 20 72 65 74 75 72 6e 20 70 72 6f 78  .    return prox
f0d70 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 68 65  y->pMethod->xChe
f0d80 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 28  ckReservedLock((
f0d90 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 70 72  sqlite3_file*)pr
f0da0 6f 78 79 2c 20 70 52 65 73 4f 75 74 29 3b 0a 20  oxy, pResOut);. 
f0db0 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
f0dc0 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68  }../*.** Lock th
f0dd0 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20  e file with the 
f0de0 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62  lock specified b
f0df0 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b  y parameter lock
f0e00 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66  type - one.** of
f0e10 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
f0e20 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48  **.**     (1) SH
f0e30 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20  ARED_LOCK.**    
f0e40 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f   (2) RESERVED_LO
f0e50 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45  CK.**     (3) PE
f0e60 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  NDING_LOCK.**   
f0e70 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f    (4) EXCLUSIVE_
f0e80 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74  LOCK.**.** Somet
f0e90 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73  imes when reques
f0ea0 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74  ting one lock st
f0eb0 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20  ate, additional 
f0ec0 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61  lock states.** a
f0ed0 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62  re inserted in b
f0ee0 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63  etween.  The loc
f0ef0 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20  king might fail 
f0f00 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61  on one of the la
f0f10 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f  ter.** transitio
f0f20 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c  ns leaving the l
f0f30 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72  ock state differ
f0f40 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74  ent from what it
f0f50 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20   started but.** 
f0f60 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69  still short of i
f0f70 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f  ts goal.  The fo
f0f80 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68  llowing chart sh
f0f90 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a  ows the allowed.
f0fa0 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61  ** transitions a
f0fb0 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20  nd the inserted 
f0fc0 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61  intermediate sta
f0fd0 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e  tes:.**.**    UN
f0fe0 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44  LOCKED -> SHARED
f0ff0 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e  .**    SHARED ->
f1000 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20   RESERVED.**    
f1010 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49  SHARED -> (PENDI
f1020 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45  NG) -> EXCLUSIVE
f1030 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20  .**    RESERVED 
f1040 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20  -> (PENDING) -> 
f1050 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20  EXCLUSIVE.**    
f1060 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55  PENDING -> EXCLU
f1070 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  SIVE.**.** This 
f1080 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c  routine will onl
f1090 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63  y increase a loc
f10a0 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c 69  k.  Use the sqli
f10b0 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a  te3OsUnlock().**
f10c0 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65   routine to lowe
f10d0 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  r a locking leve
f10e0 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  l..*/.static int
f10f0 20 70 72 6f 78 79 4c 6f 63 6b 28 73 71 6c 69 74   proxyLock(sqlit
f1100 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
f1110 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75   locktype) {.  u
f1120 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  nixFile *pFile =
f1130 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a   (unixFile*)id;.
f1140 20 20 69 6e 74 20 72 63 20 3d 20 70 72 6f 78 79    int rc = proxy
f1150 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c 65 29  TakeConch(pFile)
f1160 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
f1170 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 72 6f  TE_OK ){.    pro
f1180 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  xyLockingContext
f1190 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c   *pCtx = (proxyL
f11a0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29  ockingContext *)
f11b0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
f11c0 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e 69 78 46  ntext;.    unixF
f11d0 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20 70 43 74  ile *proxy = pCt
f11e0 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 20  x->lockProxy;.  
f11f0 20 20 72 63 20 3d 20 70 72 6f 78 79 2d 3e 70 4d    rc = proxy->pM
f1200 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28 73 71  ethod->xLock((sq
f1210 6c 69 74 65 33 5f 66 69 6c 65 2a 29 70 72 6f 78  lite3_file*)prox
f1220 79 2c 20 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20  y, locktype);.  
f1230 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
f1240 65 20 3d 20 70 72 6f 78 79 2d 3e 6c 6f 63 6b 74  e = proxy->lockt
f1250 79 70 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ype;.  }.  retur
f1260 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  n rc;.}.../*.** 
f1270 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e  Lower the lockin
f1280 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20  g level on file 
f1290 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65  descriptor pFile
f12a0 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c   to locktype.  l
f12b0 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20  ocktype.** must 
f12c0 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43  be either NO_LOC
f12d0 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b  K or SHARED_LOCK
f12e0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c  ..**.** If the l
f12f0 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20  ocking level of 
f1300 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  the file descrip
f1310 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61  tor is already a
f1320 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68  t or below.** th
f1330 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b  e requested lock
f1340 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20  ing level, this 
f1350 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d  routine is a no-
f1360 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  op..*/.static in
f1370 74 20 70 72 6f 78 79 55 6e 6c 6f 63 6b 28 73 71  t proxyUnlock(sq
f1380 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
f1390 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a  int locktype) {.
f13a0 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
f13b0 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
f13c0 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 72  d;.  int rc = pr
f13d0 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69  oxyTakeConch(pFi
f13e0 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  le);.  if( rc==S
f13f0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
f1400 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74  proxyLockingCont
f1410 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f  ext *pCtx = (pro
f1420 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  xyLockingContext
f1430 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e   *)pFile->lockin
f1440 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e  gContext;.    un
f1450 69 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20  ixFile *proxy = 
f1460 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b  pCtx->lockProxy;
f1470 0a 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 2d  .    rc = proxy-
f1480 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63  >pMethod->xUnloc
f1490 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  k((sqlite3_file*
f14a0 29 70 72 6f 78 79 2c 20 6c 6f 63 6b 74 79 70 65  )proxy, locktype
f14b0 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f  );.    pFile->lo
f14c0 63 6b 74 79 70 65 20 3d 20 70 72 6f 78 79 2d 3e  cktype = proxy->
f14d0 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20  locktype;.  }.  
f14e0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
f14f0 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65  .** Close a file
f1500 20 74 68 61 74 20 75 73 65 73 20 70 72 6f 78 79   that uses proxy
f1510 20 6c 6f 63 6b 73 2e 0a 2a 2f 0a 73 74 61 74 69   locks..*/.stati
f1520 63 20 69 6e 74 20 70 72 6f 78 79 43 6c 6f 73 65  c int proxyClose
f1530 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
f1540 64 29 20 7b 0a 20 20 69 66 28 20 69 64 20 29 7b  d) {.  if( id ){
f1550 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70  .    unixFile *p
f1560 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
f1570 2a 29 69 64 3b 0a 20 20 20 20 70 72 6f 78 79 4c  *)id;.    proxyL
f1580 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70  ockingContext *p
f1590 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b  Ctx = (proxyLock
f15a0 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69  ingContext *)pFi
f15b0 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
f15c0 78 74 3b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65  xt;.    unixFile
f15d0 20 2a 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 70 43   *lockProxy = pC
f15e0 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20  tx->lockProxy;. 
f15f0 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e     unixFile *con
f1600 63 68 46 69 6c 65 20 3d 20 70 43 74 78 2d 3e 63  chFile = pCtx->c
f1610 6f 6e 63 68 46 69 6c 65 3b 0a 20 20 20 20 69 6e  onchFile;.    in
f1620 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
f1630 3b 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20 6c  ;.    .    if( l
f1640 6f 63 6b 50 72 6f 78 79 20 29 7b 0a 20 20 20 20  ockProxy ){.    
f1650 20 20 72 63 20 3d 20 6c 6f 63 6b 50 72 6f 78 79    rc = lockProxy
f1660 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f  ->pMethod->xUnlo
f1670 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ck((sqlite3_file
f1680 2a 29 6c 6f 63 6b 50 72 6f 78 79 2c 20 4e 4f 5f  *)lockProxy, NO_
f1690 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28  LOCK);.      if(
f16a0 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
f16b0 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 63 6b  .      rc = lock
f16c0 50 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e  Proxy->pMethod->
f16d0 78 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f  xClose((sqlite3_
f16e0 66 69 6c 65 2a 29 6c 6f 63 6b 50 72 6f 78 79 29  file*)lockProxy)
f16f0 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29  ;.      if( rc )
f1700 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
f1710 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6c    sqlite3_free(l
f1720 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20 20 20  ockProxy);.     
f1730 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79   pCtx->lockProxy
f1740 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
f1750 69 66 28 20 63 6f 6e 63 68 46 69 6c 65 20 29 7b  if( conchFile ){
f1760 0a 20 20 20 20 20 20 69 66 28 20 70 43 74 78 2d  .      if( pCtx-
f1770 3e 63 6f 6e 63 68 48 65 6c 64 20 29 7b 0a 20 20  >conchHeld ){.  
f1780 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79        rc = proxy
f1790 52 65 6c 65 61 73 65 43 6f 6e 63 68 28 70 46 69  ReleaseConch(pFi
f17a0 6c 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  le);.        if(
f17b0 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
f17c0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
f17d0 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70  c = conchFile->p
f17e0 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 28  Method->xClose((
f17f0 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f  sqlite3_file*)co
f1800 6e 63 68 46 69 6c 65 29 3b 0a 20 20 20 20 20 20  nchFile);.      
f1810 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
f1820 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  rc;.      sqlite
f1830 33 5f 66 72 65 65 28 63 6f 6e 63 68 46 69 6c 65  3_free(conchFile
f1840 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
f1850 69 74 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e  ite3_free(pCtx->
f1860 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 29 3b 0a  lockProxyPath);.
f1870 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
f1880 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65  (pCtx->conchFile
f1890 50 61 74 68 29 3b 0a 20 20 20 20 73 71 6c 69 74  Path);.    sqlit
f18a0 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e 64 62  e3_free(pCtx->db
f18b0 50 61 74 68 29 3b 0a 20 20 20 20 2f 2a 20 72 65  Path);.    /* re
f18c0 73 74 6f 72 65 20 74 68 65 20 6f 72 69 67 69 6e  store the origin
f18d0 61 6c 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65  al locking conte
f18e0 78 74 20 61 6e 64 20 70 4d 65 74 68 6f 64 20 74  xt and pMethod t
f18f0 68 65 6e 20 63 6c 6f 73 65 20 69 74 20 2a 2f 0a  hen close it */.
f1900 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69      pFile->locki
f1910 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 43 74 78  ngContext = pCtx
f1920 2d 3e 6f 6c 64 4c 6f 63 6b 69 6e 67 43 6f 6e 74  ->oldLockingCont
f1930 65 78 74 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  ext;.    pFile->
f1940 70 4d 65 74 68 6f 64 20 3d 20 70 43 74 78 2d 3e  pMethod = pCtx->
f1950 70 4f 6c 64 4d 65 74 68 6f 64 3b 0a 20 20 20 20  pOldMethod;.    
f1960 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74  sqlite3_free(pCt
f1970 78 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70  x);.    return p
f1980 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78  File->pMethod->x
f1990 43 6c 6f 73 65 28 69 64 29 3b 0a 20 20 7d 0a 20  Close(id);.  }. 
f19a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
f19b0 4b 3b 0a 7d 0a 0a 0a 0a 23 65 6e 64 69 66 20 2f  K;.}....#endif /
f19c0 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c  * defined(__APPL
f19d0 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45  E__) && SQLITE_E
f19e0 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
f19f0 59 4c 45 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65  YLE */./*.** The
f1a00 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 73   proxy locking s
f1a10 74 79 6c 65 20 69 73 20 69 6e 74 65 6e 64 65 64  tyle is intended
f1a20 20 66 6f 72 20 75 73 65 20 77 69 74 68 20 41 46   for use with AF
f1a30 50 20 66 69 6c 65 73 79 73 74 65 6d 73 2e 0a 2a  P filesystems..*
f1a40 2a 20 41 6e 64 20 73 69 6e 63 65 20 41 46 50 20  * And since AFP 
f1a50 69 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 65  is only supporte
f1a60 64 20 6f 6e 20 4d 61 63 4f 53 58 2c 20 74 68 65  d on MacOSX, the
f1a70 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69   proxy locking i
f1a80 73 20 61 6c 73 6f 0a 2a 2a 20 72 65 73 74 72 69  s also.** restri
f1a90 63 74 65 64 20 74 6f 20 4d 61 63 4f 53 58 2e 0a  cted to MacOSX..
f1aa0 2a 2a 20 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ** .**.*********
f1ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
f1ac0 66 20 74 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b  f the proxy lock
f1ad0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
f1ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f1af0 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ******.*********
f1b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f1b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f1b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f1b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f1b40 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e  *****/../*.** In
f1b50 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70 65  itialize the ope
f1b60 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e  rating system in
f1b70 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54  terface..**.** T
f1b80 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 67 69  his routine regi
f1b90 73 74 65 72 73 20 61 6c 6c 20 56 46 53 20 69 6d  sters all VFS im
f1ba0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f  plementations fo
f1bb0 72 20 75 6e 69 78 2d 6c 69 6b 65 20 6f 70 65 72  r unix-like oper
f1bc0 61 74 69 6e 67 0a 2a 2a 20 73 79 73 74 65 6d 73  ating.** systems
f1bd0 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 2c  .  This routine,
f1be0 20 61 6e 64 20 74 68 65 20 73 71 6c 69 74 65 33   and the sqlite3
f1bf0 5f 6f 73 5f 65 6e 64 28 29 20 72 6f 75 74 69 6e  _os_end() routin
f1c00 65 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2c 0a  e that follows,.
f1c10 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65  ** should be the
f1c20 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 73 20 69   only routines i
f1c30 6e 20 74 68 69 73 20 66 69 6c 65 20 74 68 61 74  n this file that
f1c40 20 61 72 65 20 76 69 73 69 62 6c 65 20 66 72 6f   are visible fro
f1c50 6d 20 6f 74 68 65 72 0a 2a 2a 20 66 69 6c 65 73  m other.** files
f1c60 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
f1c70 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 6f  tine is called o
f1c80 6e 63 65 20 64 75 72 69 6e 67 20 53 51 4c 69 74  nce during SQLit
f1c90 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  e initialization
f1ca0 20 61 6e 64 20 62 79 20 61 0a 2a 2a 20 73 69 6e   and by a.** sin
f1cb0 67 6c 65 20 74 68 72 65 61 64 2e 20 20 54 68 65  gle thread.  The
f1cc0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
f1cd0 6f 6e 20 61 6e 64 20 6d 75 74 65 78 20 73 75 62  on and mutex sub
f1ce0 73 79 73 74 65 6d 73 20 68 61 76 65 20 6e 6f 74  systems have not
f1cf0 0a 2a 2a 20 6e 65 63 65 73 73 61 72 69 6c 79 20  .** necessarily 
f1d00 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64  been initialized
f1d10 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   when this routi
f1d20 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 61 6e  ne is called, an
f1d30 64 20 73 6f 20 74 68 65 79 0a 2a 2a 20 73 68 6f  d so they.** sho
f1d40 75 6c 64 20 6e 6f 74 20 62 65 20 75 73 65 64 2e  uld not be used.
f1d50 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
f1d60 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e  nt sqlite3_os_in
f1d70 69 74 28 76 6f 69 64 29 7b 20 0a 20 20 2f 2a 20  it(void){ .  /* 
f1d80 0a 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  .  ** The follow
f1d90 69 6e 67 20 6d 61 63 72 6f 20 64 65 66 69 6e 65  ing macro define
f1da0 73 20 61 6e 20 69 6e 69 74 69 61 6c 69 7a 65 72  s an initializer
f1db0 20 66 6f 72 20 61 6e 20 73 71 6c 69 74 65 33 5f   for an sqlite3_
f1dc0 76 66 73 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2a  vfs object..  **
f1dd0 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   The name of the
f1de0 20 56 46 53 20 69 73 20 4e 41 4d 45 2e 20 20 54   VFS is NAME.  T
f1df0 68 65 20 70 41 70 70 44 61 74 61 20 69 73 20 61  he pAppData is a
f1e00 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 6f   pointer to a po
f1e10 69 6e 74 65 72 0a 20 20 2a 2a 20 74 6f 20 74 68  inter.  ** to th
f1e20 65 20 22 66 69 6e 64 65 72 22 20 66 75 6e 63 74  e "finder" funct
f1e30 69 6f 6e 2e 20 20 28 70 41 70 70 44 61 74 61 20  ion.  (pAppData 
f1e40 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
f1e50 61 20 70 6f 69 6e 74 65 72 20 62 65 63 61 75 73  a pointer becaus
f1e60 65 0a 20 20 2a 2a 20 73 69 6c 6c 79 20 43 39 30  e.  ** silly C90
f1e70 20 72 75 6c 65 73 20 70 72 6f 68 69 62 69 74 20   rules prohibit 
f1e80 61 20 76 6f 69 64 2a 20 66 72 6f 6d 20 62 65 69  a void* from bei
f1e90 6e 67 20 63 61 73 74 20 74 6f 20 61 20 66 75 6e  ng cast to a fun
f1ea0 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 0a 20 20  ction pointer.  
f1eb0 2a 2a 20 61 6e 64 20 73 6f 20 77 65 20 68 61 76  ** and so we hav
f1ec0 65 20 74 6f 20 67 6f 20 74 68 72 6f 75 67 68 20  e to go through 
f1ed0 74 68 65 20 69 6e 74 65 72 6d 65 64 69 61 74 65  the intermediate
f1ee0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 76 6f 69   pointer to avoi
f1ef0 64 20 70 72 6f 62 6c 65 6d 73 0a 20 20 2a 2a 20  d problems.  ** 
f1f00 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 77  when compiling w
f1f10 69 74 68 20 2d 70 65 64 61 6e 74 69 63 2d 65 72  ith -pedantic-er
f1f20 72 6f 72 73 20 6f 6e 20 47 43 43 2e 29 0a 20 20  rors on GCC.).  
f1f30 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 46 49 4e 44  **.  ** The FIND
f1f40 45 52 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  ER parameter to 
f1f50 74 68 69 73 20 6d 61 63 72 6f 20 69 73 20 74 68  this macro is th
f1f60 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 70 6f  e name of the po
f1f70 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 20 20 2a  inter to the.  *
f1f80 2a 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f  * finder-functio
f1f90 6e 2e 20 20 54 68 65 20 66 69 6e 64 65 72 2d 66  n.  The finder-f
f1fa0 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
f1fb0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
f1fc0 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f 69 6f 5f  .  ** sqlite_io_
f1fd0 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 74  methods object t
f1fe0 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74  hat implements t
f1ff0 68 65 20 64 65 73 69 72 65 64 20 6c 6f 63 6b 69  he desired locki
f2000 6e 67 0a 20 20 2a 2a 20 62 65 68 61 76 69 6f 72  ng.  ** behavior
f2010 73 2e 20 20 53 65 65 20 74 68 65 20 64 69 76 69  s.  See the divi
f2020 73 69 6f 6e 20 61 62 6f 76 65 20 74 68 61 74 20  sion above that 
f2030 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 49 4f 4d  contains the IOM
f2040 45 54 48 4f 44 53 0a 20 20 2a 2a 20 6d 61 63 72  ETHODS.  ** macr
f2050 6f 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 20 69  o for addition i
f2060 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20 66 69  nformation on fi
f2070 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 73 2e 0a  nder-functions..
f2080 20 20 2a 2a 0a 20 20 2a 2a 20 4d 6f 73 74 20 66    **.  ** Most f
f2090 69 6e 64 65 72 73 20 73 69 6d 70 6c 79 20 72 65  inders simply re
f20a0 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
f20b0 6f 20 61 20 66 69 78 65 64 20 73 71 6c 69 74 65  o a fixed sqlite
f20c0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 20 20 2a  3_io_methods.  *
f20d0 2a 20 6f 62 6a 65 63 74 2e 20 20 42 75 74 20 74  * object.  But t
f20e0 68 65 20 22 61 75 74 6f 6c 6f 63 6b 49 6f 46 69  he "autolockIoFi
f20f0 6e 64 65 72 22 20 61 76 61 69 6c 61 62 6c 65 20  nder" available 
f2100 6f 6e 20 4d 61 63 4f 53 58 20 64 6f 65 73 20 61  on MacOSX does a
f2110 20 6c 69 74 74 6c 65 0a 20 20 2a 2a 20 6d 6f 72   little.  ** mor
f2120 65 20 74 68 61 6e 20 74 68 61 74 3b 20 69 74 20  e than that; it 
f2130 6c 6f 6f 6b 73 20 61 74 20 74 68 65 20 66 69 6c  looks at the fil
f2140 65 73 79 73 74 65 6d 20 74 79 70 65 20 74 68 61  esystem type tha
f2150 74 20 68 6f 73 74 73 20 74 68 65 20 0a 20 20 2a  t hosts the .  *
f2160 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  * database file 
f2170 61 6e 64 20 74 72 69 65 73 20 74 6f 20 63 68 6f  and tries to cho
f2180 6f 73 65 20 61 6e 20 6c 6f 63 6b 69 6e 67 20 6d  ose an locking m
f2190 65 74 68 6f 64 20 61 70 70 72 6f 70 72 69 61 74  ethod appropriat
f21a0 65 20 66 6f 72 0a 20 20 2a 2a 20 74 68 61 74 20  e for.  ** that 
f21b0 66 69 6c 65 73 79 73 74 65 6d 20 74 69 6d 65 2e  filesystem time.
f21c0 0a 20 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20  .  */.  #define 
f21d0 55 4e 49 58 56 46 53 28 56 46 53 4e 41 4d 45 2c  UNIXVFS(VFSNAME,
f21e0 20 46 49 4e 44 45 52 29 20 7b 20 20 20 20 20 20   FINDER) {      
f21f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f2200 20 20 5c 0a 20 20 20 20 31 2c 20 20 20 20 20 20    \.    1,      
f2210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
f2220 20 69 56 65 72 73 69 6f 6e 20 2a 2f 20 20 20 20   iVersion */    
f2230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f2240 5c 0a 20 20 20 20 73 69 7a 65 6f 66 28 75 6e 69  \.    sizeof(uni
f2250 78 46 69 6c 65 29 2c 20 20 20 20 20 2f 2a 20 73  xFile),     /* s
f2260 7a 4f 73 46 69 6c 65 20 2a 2f 20 20 20 20 20 20  zOsFile */      
f2270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
f2280 20 20 20 20 4d 41 58 5f 50 41 54 48 4e 41 4d 45      MAX_PATHNAME
f2290 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 78 50  ,         /* mxP
f22a0 61 74 68 6e 61 6d 65 20 2a 2f 20 20 20 20 20 20  athname */      
f22b0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
f22c0 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
f22d0 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74          /* pNext
f22e0 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
f22f0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
f2300 56 46 53 4e 41 4d 45 2c 20 20 20 20 20 20 20 20  VFSNAME,        
f2310 20 20 20 20 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a        /* zName *
f2320 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
f2330 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 28 76          \.    (v
f2340 6f 69 64 2a 29 26 46 49 4e 44 45 52 2c 20 20 20  oid*)&FINDER,   
f2350 20 20 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20      /* pAppData 
f2360 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
f2370 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78        \.    unix
f2380 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20  Open,           
f2390 20 20 2f 2a 20 78 4f 70 65 6e 20 2a 2f 20 20 20    /* xOpen */   
f23a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f23b0 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 65      \.    unixDe
f23c0 6c 65 74 65 2c 20 20 20 20 20 20 20 20 20 20 20  lete,           
f23d0 2f 2a 20 78 44 65 6c 65 74 65 20 2a 2f 20 20 20  /* xDelete */   
f23e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f23f0 20 20 5c 0a 20 20 20 20 75 6e 69 78 41 63 63 65    \.    unixAcce
f2400 73 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ss,           /*
f2410 20 78 41 63 63 65 73 73 20 2a 2f 20 20 20 20 20   xAccess */     
f2420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f2430 5c 0a 20 20 20 20 75 6e 69 78 46 75 6c 6c 50 61  \.    unixFullPa
f2440 74 68 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 78  thname,     /* x
f2450 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 2a 2f 20  FullPathname */ 
f2460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
f2470 20 20 20 20 75 6e 69 78 44 6c 4f 70 65 6e 2c 20      unixDlOpen, 
f2480 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c            /* xDl
f2490 4f 70 65 6e 20 2a 2f 20 20 20 20 20 20 20 20 20  Open */         
f24a0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
f24b0 20 20 75 6e 69 78 44 6c 45 72 72 6f 72 2c 20 20    unixDlError,  
f24c0 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72          /* xDlEr
f24d0 72 6f 72 20 2a 2f 20 20 20 20 20 20 20 20 20 20  ror */          
f24e0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
f24f0 75 6e 69 78 44 6c 53 79 6d 2c 20 20 20 20 20 20  unixDlSym,      
f2500 20 20 20 20 20 20 2f 2a 20 78 44 6c 53 79 6d 20        /* xDlSym 
f2510 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
f2520 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e          \.    un
f2530 69 78 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 20  ixDlClose,      
f2540 20 20 20 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 20      /* xDlClose 
f2550 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
f2560 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78        \.    unix
f2570 52 61 6e 64 6f 6d 6e 65 73 73 2c 20 20 20 20 20  Randomness,     
f2580 20 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 73 73    /* xRandomness
f2590 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
f25a0 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 53 6c      \.    unixSl
f25b0 65 65 70 2c 20 20 20 20 20 20 20 20 20 20 20 20  eep,            
f25c0 2f 2a 20 78 53 6c 65 65 70 20 2a 2f 20 20 20 20  /* xSleep */    
f25d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f25e0 20 20 5c 0a 20 20 20 20 75 6e 69 78 43 75 72 72    \.    unixCurr
f25f0 65 6e 74 54 69 6d 65 2c 20 20 20 20 20 20 2f 2a  entTime,      /*
f2600 20 78 43 75 72 72 65 6e 74 54 69 6d 65 20 2a 2f   xCurrentTime */
f2610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f2620 5c 0a 20 20 20 20 75 6e 69 78 47 65 74 4c 61 73  \.    unixGetLas
f2630 74 45 72 72 6f 72 20 20 20 20 20 20 2f 2a 20 78  tError      /* x
f2640 47 65 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f 20  GetLastError */ 
f2650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
f2660 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41    }..  /*.  ** A
f2670 6c 6c 20 64 65 66 61 75 6c 74 20 56 46 53 65 73  ll default VFSes
f2680 20 66 6f 72 20 75 6e 69 78 20 61 72 65 20 63 6f   for unix are co
f2690 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 66  ntained in the f
f26a0 6f 6c 6c 6f 77 69 6e 67 20 61 72 72 61 79 2e 0a  ollowing array..
f26b0 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20 74    **.  ** Note t
f26c0 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f  hat the sqlite3_
f26d0 76 66 73 2e 70 4e 65 78 74 20 66 69 65 6c 64 20  vfs.pNext field 
f26e0 6f 66 20 74 68 65 20 56 46 53 20 6f 62 6a 65 63  of the VFS objec
f26f0 74 20 69 73 20 6d 6f 64 69 66 69 65 64 0a 20 20  t is modified.  
f2700 2a 2a 20 62 79 20 74 68 65 20 53 51 4c 69 74 65  ** by the SQLite
f2710 20 63 6f 72 65 20 77 68 65 6e 20 74 68 65 20 56   core when the V
f2720 46 53 20 69 73 20 72 65 67 69 73 74 65 72 65 64  FS is registered
f2730 2e 20 20 53 6f 20 74 68 65 20 66 6f 6c 6c 6f 77  .  So the follow
f2740 69 6e 67 0a 20 20 2a 2a 20 61 72 72 61 79 20 63  ing.  ** array c
f2750 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 73 74 2e 0a  annot be const..
f2760 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 73 71    */.  static sq
f2770 6c 69 74 65 33 5f 76 66 73 20 61 56 66 73 5b 5d  lite3_vfs aVfs[]
f2780 20 3d 20 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f   = {.#if SQLITE_
f2790 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
f27a0 54 59 4c 45 20 26 26 20 28 4f 53 5f 56 58 57 4f  TYLE && (OS_VXWO
f27b0 52 4b 53 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f  RKS || defined(_
f27c0 5f 41 50 50 4c 45 5f 5f 29 29 0a 20 20 20 20 55  _APPLE__)).    U
f27d0 4e 49 58 56 46 53 28 22 75 6e 69 78 22 2c 20 20  NIXVFS("unix",  
f27e0 20 20 20 20 20 20 20 20 61 75 74 6f 6c 6f 63 6b          autolock
f27f0 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65 6c 73  IoFinder ),.#els
f2800 65 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75  e.    UNIXVFS("u
f2810 6e 69 78 22 2c 20 20 20 20 20 20 20 20 20 20 70  nix",          p
f2820 6f 73 69 78 49 6f 46 69 6e 64 65 72 20 29 2c 0a  osixIoFinder ),.
f2830 23 65 6e 64 69 66 0a 20 20 20 20 55 4e 49 58 56  #endif.    UNIXV
f2840 46 53 28 22 75 6e 69 78 2d 6e 6f 6e 65 22 2c 20  FS("unix-none", 
f2850 20 20 20 20 6e 6f 6c 6f 63 6b 49 6f 46 69 6e 64      nolockIoFind
f2860 65 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46  er ),.    UNIXVF
f2870 53 28 22 75 6e 69 78 2d 64 6f 74 66 69 6c 65 22  S("unix-dotfile"
f2880 2c 20 20 64 6f 74 6c 6f 63 6b 49 6f 46 69 6e 64  ,  dotlockIoFind
f2890 65 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46  er ),.    UNIXVF
f28a0 53 28 22 75 6e 69 78 2d 77 66 6c 22 2c 20 20 20  S("unix-wfl",   
f28b0 20 20 20 70 6f 73 69 78 57 66 6c 49 6f 46 69 6e     posixWflIoFin
f28c0 64 65 72 20 29 2c 0a 23 69 66 20 4f 53 5f 56 58  der ),.#if OS_VX
f28d0 57 4f 52 4b 53 0a 20 20 20 20 55 4e 49 58 56 46  WORKS.    UNIXVF
f28e0 53 28 22 75 6e 69 78 2d 6e 61 6d 65 64 73 65 6d  S("unix-namedsem
f28f0 22 2c 20 73 65 6d 49 6f 46 69 6e 64 65 72 20 29  ", semIoFinder )
f2900 2c 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c  ,.#endif.#if SQL
f2910 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
f2920 4e 47 5f 53 54 59 4c 45 0a 20 20 20 20 55 4e 49  NG_STYLE.    UNI
f2930 58 56 46 53 28 22 75 6e 69 78 2d 70 6f 73 69 78  XVFS("unix-posix
f2940 22 2c 20 20 20 20 70 6f 73 69 78 49 6f 46 69 6e  ",    posixIoFin
f2950 64 65 72 20 29 2c 0a 23 69 66 20 21 4f 53 5f 56  der ),.#if !OS_V
f2960 58 57 4f 52 4b 53 0a 20 20 20 20 55 4e 49 58 56  XWORKS.    UNIXV
f2970 46 53 28 22 75 6e 69 78 2d 66 6c 6f 63 6b 22 2c  FS("unix-flock",
f2980 20 20 20 20 66 6c 6f 63 6b 49 6f 46 69 6e 64 65      flockIoFinde
f2990 72 20 29 2c 0a 23 65 6e 64 69 66 0a 23 65 6e 64  r ),.#endif.#end
f29a0 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e  if.#if SQLITE_EN
f29b0 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
f29c0 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f  LE && defined(__
f29d0 41 50 50 4c 45 5f 5f 29 0a 20 20 20 20 55 4e 49  APPLE__).    UNI
f29e0 58 56 46 53 28 22 75 6e 69 78 2d 61 66 70 22 2c  XVFS("unix-afp",
f29f0 20 20 20 20 20 20 61 66 70 49 6f 46 69 6e 64 65        afpIoFinde
f2a00 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46 53  r ),.    UNIXVFS
f2a10 28 22 75 6e 69 78 2d 70 72 6f 78 79 22 2c 20 20  ("unix-proxy",  
f2a20 20 20 70 72 6f 78 79 49 6f 46 69 6e 64 65 72 20    proxyIoFinder 
f2a30 29 2c 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 20  ),.#endif.  };. 
f2a40 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b   unsigned int i;
f2a50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
f2a60 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20  p counter */..  
f2a70 2f 2a 20 52 65 67 69 73 74 65 72 20 61 6c 6c 20  /* Register all 
f2a80 56 46 53 65 73 20 64 65 66 69 6e 65 64 20 69 6e  VFSes defined in
f2a90 20 74 68 65 20 61 56 66 73 5b 5d 20 61 72 72 61   the aVfs[] arra
f2aa0 79 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20  y */.  for(i=0; 
f2ab0 69 3c 28 73 69 7a 65 6f 66 28 61 56 66 73 29 2f  i<(sizeof(aVfs)/
f2ac0 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 76  sizeof(sqlite3_v
f2ad0 66 73 29 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  fs)); i++){.    
f2ae0 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69  sqlite3_vfs_regi
f2af0 73 74 65 72 28 26 61 56 66 73 5b 69 5d 2c 20 69  ster(&aVfs[i], i
f2b00 3d 3d 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ==0);.  }.  retu
f2b10 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d  rn SQLITE_OK; .}
f2b20 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f 77 6e  ../*.** Shutdown
f2b30 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73   the operating s
f2b40 79 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65 2e  ystem interface.
f2b50 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 70 65 72  .**.** Some oper
f2b60 61 74 69 6e 67 20 73 79 73 74 65 6d 73 20 6d 69  ating systems mi
f2b70 67 68 74 20 6e 65 65 64 20 74 6f 20 64 6f 20 73  ght need to do s
f2b80 6f 6d 65 20 63 6c 65 61 6e 75 70 20 69 6e 20 74  ome cleanup in t
f2b90 68 69 73 20 72 6f 75 74 69 6e 65 2c 0a 2a 2a 20  his routine,.** 
f2ba0 74 6f 20 72 65 6c 65 61 73 65 20 64 79 6e 61 6d  to release dynam
f2bb0 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64  ically allocated
f2bc0 20 6f 62 6a 65 63 74 73 2e 20 20 42 75 74 20 6e   objects.  But n
f2bd0 6f 74 20 6f 6e 20 75 6e 69 78 2e 0a 2a 2a 20 54  ot on unix..** T
f2be0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
f2bf0 20 6e 6f 2d 6f 70 20 66 6f 72 20 75 6e 69 78 2e   no-op for unix.
f2c00 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
f2c10 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e  nt sqlite3_os_en
f2c20 64 28 76 6f 69 64 29 7b 20 0a 20 20 72 65 74 75  d(void){ .  retu
f2c30 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d  rn SQLITE_OK; .}
f2c40 0a 20 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  . .#endif /* SQL
f2c50 49 54 45 5f 4f 53 5f 55 4e 49 58 20 2a 2f 0a 0a  ITE_OS_UNIX */..
f2c60 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
f2c70 45 6e 64 20 6f 66 20 6f 73 5f 75 6e 69 78 2e 63  End of os_unix.c
f2c80 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
f2c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
f2cb0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
f2cc0 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 77 69  Begin file os_wi
f2cd0 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.c ************
f2ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
f2d00 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32  /*.** 2004 May 2
f2d10 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  2.**.** The auth
f2d20 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
f2d30 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
f2d40 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
f2d50 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
f2d60 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
f2d70 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
f2d80 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
f2d90 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
f2da0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
f2db0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
f2dc0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
f2dd0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
f2de0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
f2df0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
f2e00 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
f2e10 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
f2e20 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
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 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
f2e80 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
f2e90 6f 64 65 20 74 68 61 74 20 69 73 20 73 70 65 63  ode that is spec
f2ea0 69 66 69 63 20 74 6f 20 77 69 6e 64 6f 77 73 2e  ific to windows.
f2eb0 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f  .*/.#if SQLITE_O
f2ec0 53 5f 57 49 4e 20 20 20 20 20 20 20 20 20 20 20  S_WIN           
f2ed0 20 20 20 20 2f 2a 20 54 68 69 73 20 66 69 6c 65      /* This file
f2ee0 20 69 73 20 75 73 65 64 20 66 6f 72 20 77 69 6e   is used for win
f2ef0 64 6f 77 73 20 6f 6e 6c 79 20 2a 2f 0a 0a 0a 2f  dows only */.../
f2f00 2a 0a 2a 2a 20 41 20 4e 6f 74 65 20 41 62 6f 75  *.** A Note Abou
f2f10 74 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74  t Memory Allocat
f2f20 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ion:.**.** This 
f2f30 64 72 69 76 65 72 20 75 73 65 73 20 6d 61 6c 6c  driver uses mall
f2f40 6f 63 28 29 2f 66 72 65 65 28 29 20 64 69 72 65  oc()/free() dire
f2f50 63 74 6c 79 20 72 61 74 68 65 72 20 74 68 61 6e  ctly rather than
f2f60 20 67 6f 69 6e 67 20 74 68 72 6f 75 67 68 0a 2a   going through.*
f2f70 2a 20 74 68 65 20 53 51 4c 69 74 65 2d 77 72 61  * the SQLite-wra
f2f80 70 70 65 72 73 20 73 71 6c 69 74 65 33 5f 6d 61  ppers sqlite3_ma
f2f90 6c 6c 6f 63 28 29 2f 73 71 6c 69 74 65 33 5f 66  lloc()/sqlite3_f
f2fa0 72 65 65 28 29 2e 20 20 54 68 6f 73 65 20 77 72  ree().  Those wr
f2fb0 61 70 70 65 72 73 0a 2a 2a 20 61 72 65 20 64 65  appers.** are de
f2fc0 73 69 67 6e 65 64 20 66 6f 72 20 75 73 65 20 6f  signed for use o
f2fd0 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65  n embedded syste
f2fe0 6d 73 20 77 68 65 72 65 20 6d 65 6d 6f 72 79 20  ms where memory 
f2ff0 69 73 20 73 63 61 72 63 65 20 61 6e 64 0a 2a 2a  is scarce and.**
f3000 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73   malloc failures
f3010 20 68 61 70 70 65 6e 20 66 72 65 71 75 65 6e 74   happen frequent
f3020 6c 79 2e 20 20 57 69 6e 33 32 20 64 6f 65 73 20  ly.  Win32 does 
f3030 6e 6f 74 20 74 79 70 69 63 61 6c 6c 79 20 72 75  not typically ru
f3040 6e 20 6f 6e 0a 2a 2a 20 65 6d 62 65 64 64 65 64  n on.** embedded
f3050 20 73 79 73 74 65 6d 73 2c 20 61 6e 64 20 77 68   systems, and wh
f3060 65 6e 20 69 74 20 64 6f 65 73 20 74 68 65 20 64  en it does the d
f3070 65 76 65 6c 6f 70 65 72 73 20 6e 6f 72 6d 61 6c  evelopers normal
f3080 6c 79 20 68 61 76 65 20 62 69 67 67 65 72 0a 2a  ly have bigger.*
f3090 2a 20 70 72 6f 62 6c 65 6d 73 20 74 6f 20 77 6f  * problems to wo
f30a0 72 72 79 20 61 62 6f 75 74 20 74 68 61 6e 20 72  rry about than r
f30b0 75 6e 6e 69 6e 67 20 6f 75 74 20 6f 66 20 6d 65  unning out of me
f30c0 6d 6f 72 79 2e 20 20 53 6f 20 74 68 65 72 65 20  mory.  So there 
f30d0 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 63 6f 6d 70  is not.** a comp
f30e0 65 6c 6c 69 6e 67 20 6e 65 65 64 20 74 6f 20 75  elling need to u
f30f0 73 65 20 74 68 65 20 77 72 61 70 70 65 72 73 2e  se the wrappers.
f3100 0a 2a 2a 0a 2a 2a 20 42 75 74 20 74 68 65 72 65  .**.** But there
f3110 20 69 73 20 61 20 67 6f 6f 64 20 72 65 61 73 6f   is a good reaso
f3120 6e 20 74 6f 20 6e 6f 74 20 75 73 65 20 74 68 65  n to not use the
f3130 20 77 72 61 70 70 65 72 73 2e 20 20 49 66 20 77   wrappers.  If w
f3140 65 20 75 73 65 20 74 68 65 0a 2a 2a 20 77 72 61  e use the.** wra
f3150 70 70 65 72 73 20 74 68 65 6e 20 77 65 20 77 69  ppers then we wi
f3160 6c 6c 20 67 65 74 20 73 69 6d 75 6c 61 74 65 64  ll get simulated
f3170 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72   malloc() failur
f3180 65 73 20 77 69 74 68 69 6e 20 74 68 69 73 0a 2a  es within this.*
f3190 2a 20 64 72 69 76 65 72 2e 20 20 41 6e 64 20 74  * driver.  And t
f31a0 68 61 74 20 63 61 75 73 65 73 20 61 6c 6c 20 6b  hat causes all k
f31b0 69 6e 64 73 20 6f 66 20 70 72 6f 62 6c 65 6d 73  inds of problems
f31c0 20 66 6f 72 20 6f 75 72 20 74 65 73 74 73 2e 20   for our tests. 
f31d0 20 57 65 0a 2a 2a 20 63 6f 75 6c 64 20 65 6e 68   We.** could enh
f31e0 61 6e 63 65 20 53 51 4c 69 74 65 20 74 6f 20 64  ance SQLite to d
f31f0 65 61 6c 20 77 69 74 68 20 73 69 6d 75 6c 61 74  eal with simulat
f3200 65 64 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72  ed malloc failur
f3210 65 73 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65  es within.** the
f3220 20 4f 53 20 64 72 69 76 65 72 2c 20 62 75 74 20   OS driver, but 
f3230 74 68 65 20 63 6f 64 65 20 74 6f 20 64 65 61 6c  the code to deal
f3240 20 77 69 74 68 20 74 68 6f 73 65 20 66 61 69 6c   with those fail
f3250 75 72 65 20 77 6f 75 6c 64 20 6e 6f 74 0a 2a 2a  ure would not.**
f3260 20 62 65 20 65 78 65 72 63 69 73 65 64 20 6f 6e   be exercised on
f3270 20 4c 69 6e 75 78 20 28 77 68 69 63 68 20 64 6f   Linux (which do
f3280 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d  es not need to m
f3290 61 6c 6c 6f 63 28 29 20 69 6e 20 74 68 65 20 64  alloc() in the d
f32a0 72 69 76 65 72 29 0a 2a 2a 20 61 6e 64 20 73 6f  river).** and so
f32b0 20 77 65 20 77 6f 75 6c 64 20 68 61 76 65 20 64   we would have d
f32c0 69 66 66 69 63 75 6c 74 79 20 77 72 69 74 69 6e  ifficulty writin
f32d0 67 20 63 6f 76 65 72 61 67 65 20 74 65 73 74 73  g coverage tests
f32e0 20 66 6f 72 20 74 68 61 74 0a 2a 2a 20 63 6f 64   for that.** cod
f32f0 65 2e 20 20 42 65 74 74 65 72 20 74 6f 20 6c 65  e.  Better to le
f3300 61 76 65 20 74 68 65 20 63 6f 64 65 20 6f 75 74  ave the code out
f3310 2c 20 77 65 20 74 68 69 6e 6b 2e 0a 2a 2a 0a 2a  , we think..**.*
f3320 2a 20 54 68 65 20 70 6f 69 6e 74 20 6f 66 20 74  * The point of t
f3330 68 69 73 20 64 69 73 63 75 73 73 69 6f 6e 20 69  his discussion i
f3340 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 20 57  s as follows:  W
f3350 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61 20 6e  hen creating a n
f3360 65 77 0a 2a 2a 20 4f 53 20 6c 61 79 65 72 20 66  ew.** OS layer f
f3370 6f 72 20 61 6e 20 65 6d 62 65 64 64 65 64 20 73  or an embedded s
f3380 79 73 74 65 6d 2c 20 69 66 20 79 6f 75 20 75 73  ystem, if you us
f3390 65 20 74 68 69 73 20 66 69 6c 65 20 61 73 20 61  e this file as a
f33a0 6e 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 61 76  n example,.** av
f33b0 6f 69 64 20 74 68 65 20 75 73 65 20 6f 66 20 6d  oid the use of m
f33c0 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 2e 20  alloc()/free(). 
f33d0 20 54 68 6f 73 65 20 72 6f 75 74 69 6e 65 73 20   Those routines 
f33e0 77 6f 72 6b 20 6f 6b 20 6f 6e 20 77 69 6e 64 6f  work ok on windo
f33f0 77 73 0a 2a 2a 20 64 65 73 6b 74 6f 70 73 20 62  ws.** desktops b
f3400 75 74 20 6e 6f 74 20 73 6f 20 77 65 6c 6c 20 69  ut not so well i
f3410 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65  n embedded syste
f3420 6d 73 2e 0a 2a 2f 0a 0a 23 69 6e 63 6c 75 64 65  ms..*/..#include
f3430 20 3c 77 69 6e 62 61 73 65 2e 68 3e 0a 0a 23 69   <winbase.h>..#i
f3440 66 64 65 66 20 5f 5f 43 59 47 57 49 4e 5f 5f 0a  fdef __CYGWIN__.
f3450 23 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 63  # include <sys/c
f3460 79 67 77 69 6e 2e 68 3e 0a 23 65 6e 64 69 66 0a  ygwin.h>.#endif.
f3470 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 75 73  ./*.** Macros us
f3480 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  ed to determine 
f3490 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  whether or not t
f34a0 6f 20 75 73 65 20 74 68 72 65 61 64 73 2e 0a 2a  o use threads..*
f34b0 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 48  /.#if defined(TH
f34c0 52 45 41 44 53 41 46 45 29 20 26 26 20 54 48 52  READSAFE) && THR
f34d0 45 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e 65  EADSAFE.# define
f34e0 20 53 51 4c 49 54 45 5f 57 33 32 5f 54 48 52 45   SQLITE_W32_THRE
f34f0 41 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ADS 1.#endif../*
f3500 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 63 6f 64 65  .** Include code
f3510 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20   that is common 
f3520 74 6f 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69  to all os_*.c fi
f3530 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  les.*/./********
f3540 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f  ****** Include o
f3550 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 74 68  s_common.h in th
f3560 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 77  e middle of os_w
f3570 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  in.c ***********
f3580 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
f3590 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
f35a0 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a  e os_common.h **
f35b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f35c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f35d0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
f35e0 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54  4 May 22.**.** T
f35f0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
f3600 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
f3610 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
f3620 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
f3630 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
f3640 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
f3650 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
f3660 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
f3670 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
f3680 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
f3690 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
f36a0 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
f36b0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
f36c0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
f36d0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
f36e0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
f36f0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
f3700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
f3750 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
f3760 74 61 69 6e 73 20 6d 61 63 72 6f 73 20 61 6e 64  tains macros and
f3770 20 61 20 6c 69 74 74 6c 65 20 62 69 74 20 6f 66   a little bit of
f3780 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f   code that is co
f3790 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20 61 6c 6c 20 6f  mmon to.** all o
f37a0 66 20 74 68 65 20 70 6c 61 74 66 6f 72 6d 2d 73  f the platform-s
f37b0 70 65 63 69 66 69 63 20 66 69 6c 65 73 20 28 6f  pecific files (o
f37c0 73 5f 2a 2e 63 29 20 61 6e 64 20 69 73 20 23 69  s_*.c) and is #i
f37d0 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20 74 68 6f  ncluded into tho
f37e0 73 65 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a  se.** files..**.
f37f0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 73 68 6f  ** This file sho
f3800 75 6c 64 20 62 65 20 23 69 6e 63 6c 75 64 65 64  uld be #included
f3810 20 62 79 20 74 68 65 20 6f 73 5f 2a 2e 63 20 66   by the os_*.c f
f3820 69 6c 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 69  iles only.  It i
f3830 73 20 6e 6f 74 20 61 0a 2a 2a 20 67 65 6e 65 72  s not a.** gener
f3840 61 6c 20 70 75 72 70 6f 73 65 20 68 65 61 64 65  al purpose heade
f3850 72 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49  r file..**.** $I
f3860 64 3a 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 2c 76  d: os_common.h,v
f3870 20 31 2e 33 38 20 32 30 30 39 2f 30 32 2f 32 34   1.38 2009/02/24
f3880 20 31 38 3a 34 30 3a 35 30 20 64 61 6e 69 65 6c   18:40:50 daniel
f3890 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23  k1977 Exp $.*/.#
f38a0 69 66 6e 64 65 66 20 5f 4f 53 5f 43 4f 4d 4d 4f  ifndef _OS_COMMO
f38b0 4e 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 4f 53  N_H_.#define _OS
f38c0 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a  _COMMON_H_../*.*
f38d0 2a 20 41 74 20 6c 65 61 73 74 20 74 77 6f 20 62  * At least two b
f38e0 75 67 73 20 68 61 76 65 20 73 6c 69 70 70 65 64  ugs have slipped
f38f0 20 69 6e 20 62 65 63 61 75 73 65 20 77 65 20 63   in because we c
f3900 68 61 6e 67 65 64 20 74 68 65 20 4d 45 4d 4f 52  hanged the MEMOR
f3910 59 5f 44 45 42 55 47 0a 2a 2a 20 6d 61 63 72 6f  Y_DEBUG.** macro
f3920 20 74 6f 20 53 51 4c 49 54 45 5f 44 45 42 55 47   to SQLITE_DEBUG
f3930 20 61 6e 64 20 73 6f 6d 65 20 6f 6c 64 65 72 20   and some older 
f3940 6d 61 6b 65 66 69 6c 65 73 20 68 61 76 65 20 6e  makefiles have n
f3950 6f 74 20 79 65 74 20 6d 61 64 65 20 74 68 65 0a  ot yet made the.
f3960 2a 2a 20 73 77 69 74 63 68 2e 20 20 54 68 65 20  ** switch.  The 
f3970 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 73  following code s
f3980 68 6f 75 6c 64 20 63 61 74 63 68 20 74 68 69 73  hould catch this
f3990 20 70 72 6f 62 6c 65 6d 20 61 74 20 63 6f 6d 70   problem at comp
f39a0 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66  ile-time..*/.#if
f39b0 64 65 66 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47  def MEMORY_DEBUG
f39c0 0a 23 20 65 72 72 6f 72 20 22 54 68 65 20 4d 45  .# error "The ME
f39d0 4d 4f 52 59 5f 44 45 42 55 47 20 6d 61 63 72 6f  MORY_DEBUG macro
f39e0 20 69 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 55   is obsolete.  U
f39f0 73 65 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20  se SQLITE_DEBUG 
f3a00 69 6e 73 74 65 61 64 2e 22 0a 23 65 6e 64 69 66  instead.".#endif
f3a10 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
f3a20 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49  DEBUG.SQLITE_PRI
f3a30 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
f3a40 4f 53 54 72 61 63 65 20 3d 20 30 3b 0a 23 64 65  OSTrace = 0;.#de
f3a50 66 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29  fine OSTRACE1(X)
f3a60 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c           if( sql
f3a70 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71  ite3OSTrace ) sq
f3a80 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
f3a90 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  (X).#define OSTR
f3aa0 41 43 45 32 28 58 2c 59 29 20 20 20 20 20 20 20  ACE2(X,Y)       
f3ab0 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61  if( sqlite3OSTra
f3ac0 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75  ce ) sqlite3Debu
f3ad0 67 50 72 69 6e 74 66 28 58 2c 59 29 0a 23 64 65  gPrintf(X,Y).#de
f3ae0 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c  fine OSTRACE3(X,
f3af0 59 2c 5a 29 20 20 20 20 20 69 66 28 20 73 71 6c  Y,Z)     if( sql
f3b00 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71  ite3OSTrace ) sq
f3b10 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
f3b20 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20  (X,Y,Z).#define 
f3b30 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41  OSTRACE4(X,Y,Z,A
f3b40 29 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f  )   if( sqlite3O
f3b50 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33  STrace ) sqlite3
f3b60 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c  DebugPrintf(X,Y,
f3b70 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54  Z,A).#define OST
f3b80 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29  RACE5(X,Y,Z,A,B)
f3b90 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72   if( sqlite3OSTr
f3ba0 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62  ace ) sqlite3Deb
f3bb0 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41  ugPrintf(X,Y,Z,A
f3bc0 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,B).#define OSTR
f3bd0 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43  ACE6(X,Y,Z,A,B,C
f3be0 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74  ) \.    if(sqlit
f3bf0 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74  e3OSTrace) sqlit
f3c00 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c  e3DebugPrintf(X,
f3c10 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69  Y,Z,A,B,C).#defi
f3c20 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c  ne OSTRACE7(X,Y,
f3c30 5a 2c 41 2c 42 2c 43 2c 44 29 20 5c 0a 20 20 20  Z,A,B,C,D) \.   
f3c40 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61   if(sqlite3OSTra
f3c50 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 75 67  ce) sqlite3Debug
f3c60 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42  Printf(X,Y,Z,A,B
f3c70 2c 43 2c 44 29 0a 23 65 6c 73 65 0a 23 64 65 66  ,C,D).#else.#def
f3c80 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 0a  ine OSTRACE1(X).
f3c90 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32  #define OSTRACE2
f3ca0 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53  (X,Y).#define OS
f3cb0 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 0a 23 64  TRACE3(X,Y,Z).#d
f3cc0 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58  efine OSTRACE4(X
f3cd0 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20  ,Y,Z,A).#define 
f3ce0 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41  OSTRACE5(X,Y,Z,A
f3cf0 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,B).#define OSTR
f3d00 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43  ACE6(X,Y,Z,A,B,C
f3d10 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
f3d20 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44  E7(X,Y,Z,A,B,C,D
f3d30 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ).#endif../*.** 
f3d40 4d 61 63 72 6f 73 20 66 6f 72 20 70 65 72 66 6f  Macros for perfo
f3d50 72 6d 61 6e 63 65 20 74 72 61 63 69 6e 67 2e 20  rmance tracing. 
f3d60 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64   Normally turned
f3d70 20 6f 66 66 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b   off.  Only work
f3d80 73 0a 2a 2a 20 6f 6e 20 69 34 38 36 20 68 61 72  s.** on i486 har
f3d90 64 77 61 72 65 2e 0a 2a 2f 0a 23 69 66 64 65 66  dware..*/.#ifdef
f3da0 20 53 51 4c 49 54 45 5f 50 45 52 46 4f 52 4d 41   SQLITE_PERFORMA
f3db0 4e 43 45 5f 54 52 41 43 45 0a 0a 2f 2a 20 0a 2a  NCE_TRACE../* .*
f3dc0 2a 20 68 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61  * hwtime.h conta
f3dd0 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d  ins inline assem
f3de0 62 6c 65 72 20 63 6f 64 65 20 66 6f 72 20 69 6d  bler code for im
f3df0 70 6c 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 68  plementing .** h
f3e00 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20  igh-performance 
f3e10 74 69 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e  timing routines.
f3e20 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .*/./***********
f3e30 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 77 74 69  *** Include hwti
f3e40 6d 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  me.h in the midd
f3e50 6c 65 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e  le of os_common.
f3e60 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
f3e70 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
f3e80 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68  *** Begin file h
f3e90 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  wtime.h ********
f3ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3ec0 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d  **/./*.** 2008 M
f3ed0 61 79 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ay 27.**.** The 
f3ee0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
f3ef0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
f3f00 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
f3f10 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
f3f20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
f3f30 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
f3f40 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
f3f50 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
f3f60 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
f3f70 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
f3f80 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
f3f90 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
f3fa0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
f3fb0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
f3fc0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
f3fd0 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
f3fe0 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
f3ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
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 2a 2a 2a  ****************
f4030 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
f4040 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
f4050 6e 73 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f  ns inline asm co
f4060 64 65 20 66 6f 72 20 72 65 74 72 69 65 76 69 6e  de for retrievin
f4070 67 20 22 68 69 67 68 2d 70 65 72 66 6f 72 6d 61  g "high-performa
f4080 6e 63 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 73  nce".** counters
f4090 20 66 6f 72 20 78 38 36 20 63 6c 61 73 73 20 43   for x86 class C
f40a0 50 55 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  PUs..**.** $Id: 
f40b0 68 77 74 69 6d 65 2e 68 2c 76 20 31 2e 33 20 32  hwtime.h,v 1.3 2
f40c0 30 30 38 2f 30 38 2f 30 31 20 31 34 3a 33 33 3a  008/08/01 14:33:
f40d0 31 35 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a  15 shane Exp $.*
f40e0 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 54 49 4d  /.#ifndef _HWTIM
f40f0 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57  E_H_.#define _HW
f4100 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54  TIME_H_../*.** T
f4110 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75  he following rou
f4120 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20  tine only works 
f4130 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73  on pentium-class
f4140 20 28 6f 72 20 6e 65 77 65 72 29 20 70 72 6f 63   (or newer) proc
f4150 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73  essors..** It us
f4160 65 73 20 74 68 65 20 52 44 54 53 43 20 6f 70 63  es the RDTSC opc
f4170 6f 64 65 20 74 6f 20 72 65 61 64 20 74 68 65 20  ode to read the 
f4180 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75  cycle count valu
f4190 65 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20  e out of the.** 
f41a0 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 65  processor and re
f41b0 74 75 72 6e 73 20 74 68 61 74 20 76 61 6c 75 65  turns that value
f41c0 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 75  .  This can be u
f41d0 73 65 64 20 66 6f 72 20 68 69 67 68 2d 72 65 73  sed for high-res
f41e0 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a  .** profiling..*
f41f0 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 64 28 5f  /.#if (defined(_
f4200 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69  _GNUC__) || defi
f4210 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 26  ned(_MSC_VER)) &
f4220 26 20 5c 0a 20 20 20 20 20 20 28 64 65 66 69 6e  & \.      (defin
f4230 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69  ed(i386) || defi
f4240 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c  ned(__i386__) ||
f4250 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36   defined(_M_IX86
f4260 29 29 0a 0a 20 20 23 69 66 20 64 65 66 69 6e 65  ))..  #if define
f4270 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f  d(__GNUC__)..  _
f4280 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65  _inline__ sqlite
f4290 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48  _uint64 sqlite3H
f42a0 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20  wtime(void){.   
f42b0 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c    unsigned int l
f42c0 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73  o, hi;.     __as
f42d0 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f  m__ __volatile__
f42e0 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 61 22   ("rdtsc" : "=a"
f42f0 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69 29   (lo), "=d" (hi)
f4300 29 3b 0a 20 20 20 20 20 72 65 74 75 72 6e 20 28  );.     return (
f4310 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 68 69  sqlite_uint64)hi
f4320 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d   << 32 | lo;.  }
f4330 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 69 6e 65  ..  #elif define
f4340 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f  d(_MSC_VER)..  _
f4350 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 29  _declspec(naked)
f4360 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65   __inline sqlite
f4370 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20  _uint64 __cdecl 
f4380 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f  sqlite3Hwtime(vo
f4390 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20  id){.     __asm 
f43a0 7b 0a 20 20 20 20 20 20 20 20 72 64 74 73 63 0a  {.        rdtsc.
f43b0 20 20 20 20 20 20 20 20 72 65 74 20 20 20 20 20          ret     
f43c0 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 65    ; return value
f43d0 20 61 74 20 45 44 58 3a 45 41 58 0a 20 20 20 20   at EDX:EAX.    
f43e0 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66   }.  }..  #endif
f43f0 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64  ..#elif (defined
f4400 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65  (__GNUC__) && de
f4410 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f  fined(__x86_64__
f4420 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f  ))..  __inline__
f4430 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73   sqlite_uint64 s
f4440 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69  qlite3Hwtime(voi
f4450 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e  d){.      unsign
f4460 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20  ed long val;.   
f4470 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c     __asm__ __vol
f4480 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22  atile__ ("rdtsc"
f4490 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a   : "=A" (val));.
f44a0 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c        return val
f44b0 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64  ;.  }. .#elif (d
f44c0 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29  efined(__GNUC__)
f44d0 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 70   && defined(__pp
f44e0 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e  c__))..  __inlin
f44f0 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  e__ sqlite_uint6
f4500 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  4 sqlite3Hwtime(
f4510 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73  void){.      uns
f4520 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20  igned long long 
f4530 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e  retval;.      un
f4540 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b  signed long junk
f4550 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20  ;.      __asm__ 
f4560 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c  __volatile__ ("\
f4570 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 31 3a 20  n\.          1: 
f4580 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 31 5c       mftbu   %1\
f4590 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  n\.             
f45a0 20 20 20 20 20 6d 66 74 62 20 20 20 20 25 4c 30       mftb    %L0
f45b0 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20  \n\.            
f45c0 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 30        mftbu   %0
f45d0 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20  \n\.            
f45e0 20 20 20 20 20 20 63 6d 70 77 20 20 20 20 25 30        cmpw    %0
f45f0 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20  ,%1\n\.         
f4600 20 20 20 20 20 20 20 20 20 62 6e 65 20 20 20 20           bne    
f4610 20 31 62 22 0a 20 20 20 20 20 20 20 20 20 20 20   1b".           
f4620 20 20 20 20 20 20 20 3a 20 22 3d 72 22 20 28 72         : "=r" (r
f4630 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75  etval), "=r" (ju
f4640 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75  nk));.      retu
f4650 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a  rn retval;.  }..
f4660 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72 20  #else..  #error 
f4670 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74  Need implementat
f4680 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 77  ion of sqlite3Hw
f4690 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20  time() for your 
f46a0 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a  platform...  /*.
f46b0 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20    ** To compile 
f46c0 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e  without implemen
f46d0 74 69 6e 67 20 73 71 6c 69 74 65 33 48 77 74 69  ting sqlite3Hwti
f46e0 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c  me() for your pl
f46f0 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75  atform,.  ** you
f4700 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 20   can remove the 
f4710 61 62 6f 76 65 20 23 65 72 72 6f 72 20 61 6e 64  above #error and
f4720 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69   use the followi
f4730 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 66 75 6e  ng.  ** stub fun
f4740 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c  ction.  You will
f4750 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73 75 70   lose timing sup
f4760 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20  port for many.  
f4770 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 75 67 67  ** of the debugg
f4780 69 6e 67 20 61 6e 64 20 74 65 73 74 69 6e 67 20  ing and testing 
f4790 75 74 69 6c 69 74 69 65 73 2c 20 62 75 74 20 69  utilities, but i
f47a0 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a  t should at.  **
f47b0 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65 20 61   least compile a
f47c0 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c  nd run..  */.SQL
f47d0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 73 71  ITE_PRIVATE   sq
f47e0 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69  lite_uint64 sqli
f47f0 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b  te3Hwtime(void){
f4800 20 72 65 74 75 72 6e 20 28 28 73 71 6c 69 74 65   return ((sqlite
f4810 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23  _uint64)0); }..#
f4820 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a  endif..#endif /*
f4830 20 21 64 65 66 69 6e 65 64 28 5f 48 57 54 49 4d   !defined(_HWTIM
f4840 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  E_H_) */../*****
f4850 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
f4860 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a   hwtime.h ******
f4870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4890 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
f48a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
f48b0 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
f48c0 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 63 6f 6d  ft off in os_com
f48d0 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  mon.h **********
f48e0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 69  ********/..stati
f48f0 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20  c sqlite_uint64 
f4900 67 5f 73 74 61 72 74 3b 0a 73 74 61 74 69 63 20  g_start;.static 
f4910 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f  sqlite_uint64 g_
f4920 65 6c 61 70 73 65 64 3b 0a 23 64 65 66 69 6e 65  elapsed;.#define
f4930 20 54 49 4d 45 52 5f 53 54 41 52 54 20 20 20 20   TIMER_START    
f4940 20 20 20 67 5f 73 74 61 72 74 3d 73 71 6c 69 74     g_start=sqlit
f4950 65 33 48 77 74 69 6d 65 28 29 0a 23 64 65 66 69  e3Hwtime().#defi
f4960 6e 65 20 54 49 4d 45 52 5f 45 4e 44 20 20 20 20  ne TIMER_END    
f4970 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64 3d 73       g_elapsed=s
f4980 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 2d 67  qlite3Hwtime()-g
f4990 5f 73 74 61 72 74 0a 23 64 65 66 69 6e 65 20 54  _start.#define T
f49a0 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20  IMER_ELAPSED    
f49b0 20 67 5f 65 6c 61 70 73 65 64 0a 23 65 6c 73 65   g_elapsed.#else
f49c0 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53  .#define TIMER_S
f49d0 54 41 52 54 0a 23 64 65 66 69 6e 65 20 54 49 4d  TART.#define TIM
f49e0 45 52 5f 45 4e 44 0a 23 64 65 66 69 6e 65 20 54  ER_END.#define T
f49f0 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20  IMER_ELAPSED    
f4a00 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34   ((sqlite_uint64
f4a10 29 30 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  )0).#endif../*.*
f4a20 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c 65 20  * If we compile 
f4a30 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f  with the SQLITE_
f4a40 54 45 53 54 20 6d 61 63 72 6f 20 73 65 74 2c 20  TEST macro set, 
f4a50 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69  then the followi
f4a60 6e 67 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 63  ng block.** of c
f4a70 6f 64 65 20 77 69 6c 6c 20 67 69 76 65 20 75 73  ode will give us
f4a80 20 74 68 65 20 61 62 69 6c 69 74 79 20 74 6f 20   the ability to 
f4a90 73 69 6d 75 6c 61 74 65 20 61 20 64 69 73 6b 20  simulate a disk 
f4aa0 49 2f 4f 20 65 72 72 6f 72 2e 20 20 54 68 69 73  I/O error.  This
f4ab0 0a 2a 2a 20 69 73 20 75 73 65 64 20 66 6f 72 20  .** is used for 
f4ac0 74 65 73 74 69 6e 67 20 74 68 65 20 49 2f 4f 20  testing the I/O 
f4ad0 72 65 63 6f 76 65 72 79 20 6c 6f 67 69 63 2e 0a  recovery logic..
f4ae0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
f4af0 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49  _TEST.SQLITE_API
f4b00 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f   int sqlite3_io_
f4b10 65 72 72 6f 72 5f 68 69 74 20 3d 20 30 3b 20 20  error_hit = 0;  
f4b20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74            /* Tot
f4b30 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 49 2f 4f  al number of I/O
f4b40 20 45 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54   Errors */.SQLIT
f4b50 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
f4b60 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68  3_io_error_hardh
f4b70 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f  it = 0;        /
f4b80 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d  * Number of non-
f4b90 62 65 6e 69 67 6e 20 65 72 72 6f 72 73 20 2a 2f  benign errors */
f4ba0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
f4bb0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
f4bc0 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 20 20 20  _pending = 0;   
f4bd0 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20 64 6f       /* Count do
f4be0 77 6e 20 74 6f 20 66 69 72 73 74 20 49 2f 4f 20  wn to first I/O 
f4bf0 65 72 72 6f 72 20 2a 2f 0a 53 51 4c 49 54 45 5f  error */.SQLITE_
f4c00 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
f4c10 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74  io_error_persist
f4c20 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20   = 0;        /* 
f4c30 54 72 75 65 20 69 66 20 49 2f 4f 20 65 72 72 6f  True if I/O erro
f4c40 72 73 20 70 65 72 73 69 73 74 20 2a 2f 0a 53 51  rs persist */.SQ
f4c50 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
f4c60 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65  ite3_io_error_be
f4c70 6e 69 67 6e 20 3d 20 30 3b 20 20 20 20 20 20 20  nign = 0;       
f4c80 20 20 2f 2a 20 54 72 75 65 20 69 66 20 65 72 72    /* True if err
f4c90 6f 72 73 20 61 72 65 20 62 65 6e 69 67 6e 20 2a  ors are benign *
f4ca0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
f4cb0 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c   sqlite3_diskful
f4cc0 6c 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 53  l_pending = 0;.S
f4cd0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
f4ce0 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d  lite3_diskfull =
f4cf0 20 30 3b 0a 23 64 65 66 69 6e 65 20 53 69 6d 75   0;.#define Simu
f4d00 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67  lateIOErrorBenig
f4d10 6e 28 58 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f  n(X) sqlite3_io_
f4d20 65 72 72 6f 72 5f 62 65 6e 69 67 6e 3d 28 58 29  error_benign=(X)
f4d30 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74  .#define Simulat
f4d40 65 49 4f 45 72 72 6f 72 28 43 4f 44 45 29 20 20  eIOError(CODE)  
f4d50 5c 0a 20 20 69 66 28 20 28 73 71 6c 69 74 65 33  \.  if( (sqlite3
f4d60 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73  _io_error_persis
f4d70 74 20 26 26 20 73 71 6c 69 74 65 33 5f 69 6f 5f  t && sqlite3_io_
f4d80 65 72 72 6f 72 5f 68 69 74 29 20 5c 0a 20 20 20  error_hit) \.   
f4d90 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f 69      || sqlite3_i
f4da0 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 2d  o_error_pending-
f4db0 2d 20 3d 3d 20 31 20 29 20 20 5c 0a 20 20 20 20  - == 1 )  \.    
f4dc0 20 20 20 20 20 20 20 20 20 20 7b 20 6c 6f 63 61            { loca
f4dd0 6c 5f 69 6f 65 72 72 28 29 3b 20 43 4f 44 45 3b  l_ioerr(); CODE;
f4de0 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c   }.static void l
f4df0 6f 63 61 6c 5f 69 6f 65 72 72 28 29 7b 0a 20 20  ocal_ioerr(){.  
f4e00 49 4f 54 52 41 43 45 28 28 22 49 4f 45 52 52 5c  IOTRACE(("IOERR\
f4e10 6e 22 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  n"));.  sqlite3_
f4e20 69 6f 5f 65 72 72 6f 72 5f 68 69 74 2b 2b 3b 0a  io_error_hit++;.
f4e30 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f 69    if( !sqlite3_i
f4e40 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 29  o_error_benign )
f4e50 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
f4e60 72 5f 68 61 72 64 68 69 74 2b 2b 3b 0a 7d 0a 23  r_hardhit++;.}.#
f4e70 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44  define SimulateD
f4e80 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 43 4f 44  iskfullError(COD
f4e90 45 29 20 5c 0a 20 20 20 69 66 28 20 73 71 6c 69  E) \.   if( sqli
f4ea0 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e  te3_diskfull_pen
f4eb0 64 69 6e 67 20 29 7b 20 5c 0a 20 20 20 20 20 69  ding ){ \.     i
f4ec0 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66  f( sqlite3_diskf
f4ed0 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d 20 31  ull_pending == 1
f4ee0 20 29 7b 20 5c 0a 20 20 20 20 20 20 20 6c 6f 63   ){ \.       loc
f4ef0 61 6c 5f 69 6f 65 72 72 28 29 3b 20 5c 0a 20 20  al_ioerr(); \.  
f4f00 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73       sqlite3_dis
f4f10 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20 20  kfull = 1; \.   
f4f20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65      sqlite3_io_e
f4f30 72 72 6f 72 5f 68 69 74 20 3d 20 31 3b 20 5c 0a  rror_hit = 1; \.
f4f40 20 20 20 20 20 20 20 43 4f 44 45 3b 20 5c 0a 20         CODE; \. 
f4f50 20 20 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 20 20      }else{ \.   
f4f60 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b      sqlite3_disk
f4f70 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20  full_pending--; 
f4f80 5c 0a 20 20 20 20 20 7d 20 5c 0a 20 20 20 7d 0a  \.     } \.   }.
f4f90 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 53 69  #else.#define Si
f4fa0 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e  mulateIOErrorBen
f4fb0 69 67 6e 28 58 29 0a 23 64 65 66 69 6e 65 20 53  ign(X).#define S
f4fc0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 41  imulateIOError(A
f4fd0 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61  ).#define Simula
f4fe0 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28  teDiskfullError(
f4ff0 41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  A).#endif../*.**
f5000 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 6b   When testing, k
f5010 65 65 70 20 61 20 63 6f 75 6e 74 20 6f 66 20 74  eep a count of t
f5020 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 65  he number of ope
f5030 6e 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64  n files..*/.#ifd
f5040 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53  ef SQLITE_TEST.S
f5050 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
f5060 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f  lite3_open_file_
f5070 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 64 65 66 69  count = 0;.#defi
f5080 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58  ne OpenCounter(X
f5090 29 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  )  sqlite3_open_
f50a0 66 69 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a  file_count+=(X).
f50b0 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 70  #else.#define Op
f50c0 65 6e 43 6f 75 6e 74 65 72 28 58 29 0a 23 65 6e  enCounter(X).#en
f50d0 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21  dif..#endif /* !
f50e0 64 65 66 69 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d  defined(_OS_COMM
f50f0 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  ON_H_) */../****
f5100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
f5110 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a  f os_common.h **
f5120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
f5150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
f5160 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
f5170 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 77 69  eft off in os_wi
f5180 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.c ************
f5190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a  *********/../*.*
f51a0 2a 20 53 6f 6d 65 20 6d 69 63 72 6f 73 6f 66 74  * Some microsoft
f51b0 20 63 6f 6d 70 69 6c 65 72 73 20 6c 61 63 6b 20   compilers lack 
f51c0 74 68 69 73 20 64 65 66 69 6e 69 74 69 6f 6e 2e  this definition.
f51d0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 49 4e 56 41  .*/.#ifndef INVA
f51e0 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55  LID_FILE_ATTRIBU
f51f0 54 45 53 0a 23 20 64 65 66 69 6e 65 20 49 4e 56  TES.# define INV
f5200 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42  ALID_FILE_ATTRIB
f5210 55 54 45 53 20 28 28 44 57 4f 52 44 29 2d 31 29  UTES ((DWORD)-1)
f5220 20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20   .#endif../*.** 
f5230 44 65 74 65 72 6d 69 6e 65 20 69 66 20 77 65 20  Determine if we 
f5240 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68  are dealing with
f5250 20 57 69 6e 64 6f 77 73 43 45 20 2d 20 77 68 69   WindowsCE - whi
f5260 63 68 20 68 61 73 20 61 20 6d 75 63 68 0a 2a 2a  ch has a much.**
f5270 20 72 65 64 75 63 65 64 20 41 50 49 2e 0a 2a 2f   reduced API..*/
f5280 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  .#if SQLITE_OS_W
f5290 49 4e 43 45 0a 23 20 64 65 66 69 6e 65 20 41 72  INCE.# define Ar
f52a0 65 46 69 6c 65 41 70 69 73 41 4e 53 49 28 29 20  eFileApisANSI() 
f52b0 31 0a 23 20 64 65 66 69 6e 65 20 47 65 74 44 69  1.# define GetDi
f52c0 73 6b 46 72 65 65 53 70 61 63 65 57 28 29 20 30  skFreeSpaceW() 0
f52d0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57  .#endif../*.** W
f52e0 69 6e 43 45 20 6c 61 63 6b 73 20 6e 61 74 69 76  inCE lacks nativ
f52f0 65 20 73 75 70 70 6f 72 74 20 66 6f 72 20 66 69  e support for fi
f5300 6c 65 20 6c 6f 63 6b 69 6e 67 20 73 6f 20 77 65  le locking so we
f5310 20 68 61 76 65 20 74 6f 20 66 61 6b 65 20 69 74   have to fake it
f5320 0a 2a 2a 20 77 69 74 68 20 73 6f 6d 65 20 63 6f  .** with some co
f5330 64 65 20 6f 66 20 6f 75 72 20 6f 77 6e 2e 0a 2a  de of our own..*
f5340 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  /.#if SQLITE_OS_
f5350 57 49 4e 43 45 0a 74 79 70 65 64 65 66 20 73 74  WINCE.typedef st
f5360 72 75 63 74 20 77 69 6e 63 65 4c 6f 63 6b 20 7b  ruct winceLock {
f5370 0a 20 20 69 6e 74 20 6e 52 65 61 64 65 72 73 3b  .  int nReaders;
f5380 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
f5390 20 6f 66 20 72 65 61 64 65 72 20 6c 6f 63 6b 73   of reader locks
f53a0 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 20 20 42   obtained */.  B
f53b0 4f 4f 4c 20 62 50 65 6e 64 69 6e 67 3b 20 20 20  OOL bPending;   
f53c0 20 20 20 2f 2a 20 49 6e 64 69 63 61 74 65 73 20     /* Indicates 
f53d0 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 68  a pending lock h
f53e0 61 73 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 64  as been obtained
f53f0 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62 52 65 73 65   */.  BOOL bRese
f5400 72 76 65 64 3b 20 20 20 20 20 2f 2a 20 49 6e 64  rved;     /* Ind
f5410 69 63 61 74 65 73 20 61 20 72 65 73 65 72 76 65  icates a reserve
f5420 64 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20  d lock has been 
f5430 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 20 20 42 4f  obtained */.  BO
f5440 4f 4c 20 62 45 78 63 6c 75 73 69 76 65 3b 20 20  OL bExclusive;  
f5450 20 20 2f 2a 20 49 6e 64 69 63 61 74 65 73 20 61    /* Indicates a
f5460 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  n exclusive lock
f5470 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69 6e   has been obtain
f5480 65 64 20 2a 2f 0a 7d 20 77 69 6e 63 65 4c 6f 63  ed */.} winceLoc
f5490 6b 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  k;.#endif../*.**
f54a0 20 54 68 65 20 77 69 6e 46 69 6c 65 20 73 74 72   The winFile str
f54b0 75 63 74 75 72 65 20 69 73 20 61 20 73 75 62 63  ucture is a subc
f54c0 6c 61 73 73 20 6f 66 20 73 71 6c 69 74 65 33 5f  lass of sqlite3_
f54d0 66 69 6c 65 2a 20 73 70 65 63 69 66 69 63 20 74  file* specific t
f54e0 6f 20 74 68 65 20 77 69 6e 33 32 0a 2a 2a 20 70  o the win32.** p
f54f0 6f 72 74 61 62 69 6c 69 74 79 20 6c 61 79 65 72  ortability layer
f5500 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
f5510 75 63 74 20 77 69 6e 46 69 6c 65 20 77 69 6e 46  uct winFile winF
f5520 69 6c 65 3b 0a 73 74 72 75 63 74 20 77 69 6e 46  ile;.struct winF
f5530 69 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71  ile {.  const sq
f5540 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
f5550 20 2a 70 4d 65 74 68 6f 64 3b 2f 2a 20 4d 75 73   *pMethod;/* Mus
f5560 74 20 62 65 20 66 69 72 73 74 20 2a 2f 0a 20 20  t be first */.  
f5570 48 41 4e 44 4c 45 20 68 3b 20 20 20 20 20 20 20  HANDLE h;       
f5580 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c          /* Handl
f5590 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20  e for accessing 
f55a0 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 6e  the file */.  un
f55b0 73 69 67 6e 65 64 20 63 68 61 72 20 6c 6f 63 6b  signed char lock
f55c0 74 79 70 65 3b 20 2f 2a 20 54 79 70 65 20 6f 66  type; /* Type of
f55d0 20 6c 6f 63 6b 20 63 75 72 72 65 6e 74 6c 79 20   lock currently 
f55e0 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 66 69 6c  held on this fil
f55f0 65 20 2a 2f 0a 20 20 73 68 6f 72 74 20 73 68 61  e */.  short sha
f5600 72 65 64 4c 6f 63 6b 42 79 74 65 3b 20 20 20 2f  redLockByte;   /
f5610 2a 20 52 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 65  * Randomly chose
f5620 6e 20 62 79 74 65 20 75 73 65 64 20 61 73 20 61  n byte used as a
f5630 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a   shared lock */.
f5640 20 20 44 57 4f 52 44 20 6c 61 73 74 45 72 72 6e    DWORD lastErrn
f5650 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  o;        /* The
f5660 20 57 69 6e 64 6f 77 73 20 65 72 72 6e 6f 20 66   Windows errno f
f5670 72 6f 6d 20 74 68 65 20 6c 61 73 74 20 49 2f 4f  rom the last I/O
f5680 20 65 72 72 6f 72 20 2a 2f 0a 20 20 44 57 4f 52   error */.  DWOR
f5690 44 20 73 65 63 74 6f 72 53 69 7a 65 3b 20 20 20  D sectorSize;   
f56a0 20 20 20 20 2f 2a 20 53 65 63 74 6f 72 20 73 69      /* Sector si
f56b0 7a 65 20 6f 66 20 74 68 65 20 64 65 76 69 63 65  ze of the device
f56c0 20 66 69 6c 65 20 69 73 20 6f 6e 20 2a 2f 0a 23   file is on */.#
f56d0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
f56e0 43 45 0a 20 20 57 43 48 41 52 20 2a 7a 44 65 6c  CE.  WCHAR *zDel
f56f0 65 74 65 4f 6e 43 6c 6f 73 65 3b 20 20 2f 2a 20  eteOnClose;  /* 
f5700 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20  Name of file to 
f5710 64 65 6c 65 74 65 20 77 68 65 6e 20 63 6c 6f 73  delete when clos
f5720 69 6e 67 20 2a 2f 0a 20 20 48 41 4e 44 4c 45 20  ing */.  HANDLE 
f5730 68 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20  hMutex;         
f5740 20 2f 2a 20 4d 75 74 65 78 20 75 73 65 64 20 74   /* Mutex used t
f5750 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73  o control access
f5760 20 74 6f 20 73 68 61 72 65 64 20 6c 6f 63 6b 20   to shared lock 
f5770 2a 2f 20 20 0a 20 20 48 41 4e 44 4c 45 20 68 53  */  .  HANDLE hS
f5780 68 61 72 65 64 3b 20 20 20 20 20 20 20 20 20 2f  hared;         /
f5790 2a 20 53 68 61 72 65 64 20 6d 65 6d 6f 72 79 20  * Shared memory 
f57a0 73 65 67 6d 65 6e 74 20 75 73 65 64 20 66 6f 72  segment used for
f57b0 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 77 69   locking */.  wi
f57c0 6e 63 65 4c 6f 63 6b 20 6c 6f 63 61 6c 3b 20 20  nceLock local;  
f57d0 20 20 20 20 20 20 2f 2a 20 4c 6f 63 6b 73 20 6f        /* Locks o
f57e0 62 74 61 69 6e 65 64 20 62 79 20 74 68 69 73 20  btained by this 
f57f0 69 6e 73 74 61 6e 63 65 20 6f 66 20 77 69 6e 46  instance of winF
f5800 69 6c 65 20 2a 2f 0a 20 20 77 69 6e 63 65 4c 6f  ile */.  winceLo
f5810 63 6b 20 2a 73 68 61 72 65 64 3b 20 20 20 20 20  ck *shared;     
f5820 20 2f 2a 20 47 6c 6f 62 61 6c 20 73 68 61 72 65   /* Global share
f5830 64 20 6c 6f 63 6b 20 6d 65 6d 6f 72 79 20 66 6f  d lock memory fo
f5840 72 20 74 68 65 20 66 69 6c 65 20 20 2a 2f 0a 23  r the file  */.#
f5850 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  endif.};../*.** 
f5860 46 6f 72 77 61 72 64 20 70 72 6f 74 6f 74 79 70  Forward prototyp
f5870 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  es..*/.static in
f5880 74 20 67 65 74 53 65 63 74 6f 72 53 69 7a 65 28  t getSectorSize(
f5890 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73  .    sqlite3_vfs
f58a0 20 2a 70 56 66 73 2c 0a 20 20 20 20 63 6f 6e 73   *pVfs,.    cons
f58b0 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76  t char *zRelativ
f58c0 65 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 66  e     /* UTF-8 f
f58d0 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a 29 3b 0a 0a  ile name */.);..
f58e0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
f58f0 69 6e 67 20 76 61 72 69 61 62 6c 65 20 69 73 20  ing variable is 
f5900 28 6e 6f 72 6d 61 6c 6c 79 29 20 73 65 74 20 6f  (normally) set o
f5910 6e 63 65 20 61 6e 64 20 6e 65 76 65 72 20 63 68  nce and never ch
f5920 61 6e 67 65 73 0a 2a 2a 20 74 68 65 72 65 61 66  anges.** thereaf
f5930 74 65 72 2e 20 20 49 74 20 72 65 63 6f 72 64 73  ter.  It records
f5940 20 77 68 65 74 68 65 72 20 74 68 65 20 6f 70 65   whether the ope
f5950 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 73  rating system is
f5960 20 57 69 6e 39 35 0a 2a 2a 20 6f 72 20 57 69 6e   Win95.** or Win
f5970 4e 54 2e 0a 2a 2a 0a 2a 2a 20 30 3a 20 20 20 4f  NT..**.** 0:   O
f5980 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20  perating system 
f5990 75 6e 6b 6e 6f 77 6e 2e 0a 2a 2a 20 31 3a 20 20  unknown..** 1:  
f59a0 20 4f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   Operating syste
f59b0 6d 20 69 73 20 57 69 6e 39 35 2e 0a 2a 2a 20 32  m is Win95..** 2
f59c0 3a 20 20 20 4f 70 65 72 61 74 69 6e 67 20 73 79  :   Operating sy
f59d0 73 74 65 6d 20 69 73 20 57 69 6e 4e 54 2e 0a 2a  stem is WinNT..*
f59e0 2a 0a 2a 2a 20 49 6e 20 6f 72 64 65 72 20 74 6f  *.** In order to
f59f0 20 66 61 63 69 6c 69 74 61 74 65 20 74 65 73 74   facilitate test
f5a00 69 6e 67 20 6f 6e 20 61 20 57 69 6e 4e 54 20 73  ing on a WinNT s
f5a10 79 73 74 65 6d 2c 20 74 68 65 20 74 65 73 74 20  ystem, the test 
f5a20 66 69 78 74 75 72 65 0a 2a 2a 20 63 61 6e 20 6d  fixture.** can m
f5a30 61 6e 75 61 6c 6c 79 20 73 65 74 20 74 68 69 73  anually set this
f5a40 20 76 61 6c 75 65 20 74 6f 20 31 20 74 6f 20 65   value to 1 to e
f5a50 6d 75 6c 61 74 65 20 57 69 6e 39 38 20 62 65 68  mulate Win98 beh
f5a60 61 76 69 6f 72 2e 0a 2a 2f 0a 23 69 66 64 65 66  avior..*/.#ifdef
f5a70 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
f5a80 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
f5a90 74 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20 30 3b  te3_os_type = 0;
f5aa0 0a 23 65 6c 73 65 0a 73 74 61 74 69 63 20 69 6e  .#else.static in
f5ab0 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70  t sqlite3_os_typ
f5ac0 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f  e = 0;.#endif../
f5ad0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65  *.** Return true
f5ae0 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 77   (non-zero) if w
f5af0 65 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 75 6e  e are running un
f5b00 64 65 72 20 57 69 6e 4e 54 2c 20 57 69 6e 32 4b  der WinNT, Win2K
f5b10 2c 20 57 69 6e 58 50 2c 0a 2a 2a 20 6f 72 20 57  , WinXP,.** or W
f5b20 69 6e 43 45 2e 20 20 52 65 74 75 72 6e 20 66 61  inCE.  Return fa
f5b30 6c 73 65 20 28 7a 65 72 6f 29 20 66 6f 72 20 57  lse (zero) for W
f5b40 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 6f 72 20  in95, Win98, or 
f5b50 57 69 6e 4d 45 2e 0a 2a 2a 0a 2a 2a 20 48 65 72  WinME..**.** Her
f5b60 65 20 69 73 20 61 6e 20 69 6e 74 65 72 65 73 74  e is an interest
f5b70 69 6e 67 20 6f 62 73 65 72 76 61 74 69 6f 6e 3a  ing observation:
f5b80 20 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20    Win95, Win98, 
f5b90 61 6e 64 20 57 69 6e 4d 45 20 6c 61 63 6b 0a 2a  and WinME lack.*
f5ba0 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78  * the LockFileEx
f5bb0 28 29 20 41 50 49 2e 20 20 42 75 74 20 77 65 20  () API.  But we 
f5bc0 63 61 6e 20 73 74 69 6c 6c 20 73 74 61 74 69 63  can still static
f5bd0 61 6c 6c 79 20 6c 69 6e 6b 20 61 67 61 69 6e 73  ally link agains
f5be0 74 20 74 68 61 74 0a 2a 2a 20 41 50 49 20 61 73  t that.** API as
f5bf0 20 6c 6f 6e 67 20 61 73 20 77 65 20 64 6f 6e 27   long as we don'
f5c00 74 20 63 61 6c 6c 20 69 74 20 77 68 65 6e 20 72  t call it when r
f5c10 75 6e 6e 69 6e 67 20 57 69 6e 39 35 2f 39 38 2f  unning Win95/98/
f5c20 4d 45 2e 20 20 41 20 63 61 6c 6c 20 74 6f 0a 2a  ME.  A call to.*
f5c30 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * this routine i
f5c40 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d  s used to determ
f5c50 69 6e 65 20 69 66 20 74 68 65 20 68 6f 73 74 20  ine if the host 
f5c60 69 73 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20 6f  is Win95/98/ME o
f5c70 72 0a 2a 2a 20 57 69 6e 4e 54 2f 32 4b 2f 58 50  r.** WinNT/2K/XP
f5c80 20 73 6f 20 74 68 61 74 20 77 65 20 77 69 6c 6c   so that we will
f5c90 20 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 6f 72   know whether or
f5ca0 20 6e 6f 74 20 77 65 20 63 61 6e 20 73 61 66 65   not we can safe
f5cb0 6c 79 20 63 61 6c 6c 0a 2a 2a 20 74 68 65 20 4c  ly call.** the L
f5cc0 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e  ockFileEx() API.
f5cd0 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f  .*/.#if SQLITE_O
f5ce0 53 5f 57 49 4e 43 45 0a 23 20 64 65 66 69 6e 65  S_WINCE.# define
f5cf0 20 69 73 4e 54 28 29 20 20 28 31 29 0a 23 65 6c   isNT()  (1).#el
f5d00 73 65 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20  se.  static int 
f5d10 69 73 4e 54 28 76 6f 69 64 29 7b 0a 20 20 20 20  isNT(void){.    
f5d20 69 66 28 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74  if( sqlite3_os_t
f5d30 79 70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ype==0 ){.      
f5d40 4f 53 56 45 52 53 49 4f 4e 49 4e 46 4f 20 73 49  OSVERSIONINFO sI
f5d50 6e 66 6f 3b 0a 20 20 20 20 20 20 73 49 6e 66 6f  nfo;.      sInfo
f5d60 2e 64 77 4f 53 56 65 72 73 69 6f 6e 49 6e 66 6f  .dwOSVersionInfo
f5d70 53 69 7a 65 20 3d 20 73 69 7a 65 6f 66 28 73 49  Size = sizeof(sI
f5d80 6e 66 6f 29 3b 0a 20 20 20 20 20 20 47 65 74 56  nfo);.      GetV
f5d90 65 72 73 69 6f 6e 45 78 28 26 73 49 6e 66 6f 29  ersionEx(&sInfo)
f5da0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
f5db0 6f 73 5f 74 79 70 65 20 3d 20 73 49 6e 66 6f 2e  os_type = sInfo.
f5dc0 64 77 50 6c 61 74 66 6f 72 6d 49 64 3d 3d 56 45  dwPlatformId==VE
f5dd0 52 5f 50 4c 41 54 46 4f 52 4d 5f 57 49 4e 33 32  R_PLATFORM_WIN32
f5de0 5f 4e 54 20 3f 20 32 20 3a 20 31 3b 0a 20 20 20  _NT ? 2 : 1;.   
f5df0 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71   }.    return sq
f5e00 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 3d 3d 32  lite3_os_type==2
f5e10 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ;.  }.#endif /* 
f5e20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20  SQLITE_OS_WINCE 
f5e30 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  */../*.** Conver
f5e40 74 20 61 20 55 54 46 2d 38 20 73 74 72 69 6e 67  t a UTF-8 string
f5e50 20 74 6f 20 6d 69 63 72 6f 73 6f 66 74 20 75 6e   to microsoft un
f5e60 69 63 6f 64 65 20 28 55 54 46 2d 31 36 3f 29 2e  icode (UTF-16?).
f5e70 20 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20 74 6f   .**.** Space to
f5e80 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e   hold the return
f5e90 65 64 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74  ed string is obt
f5ea0 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f  ained from mallo
f5eb0 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 57 43 48  c..*/.static WCH
f5ec0 41 52 20 2a 75 74 66 38 54 6f 55 6e 69 63 6f 64  AR *utf8ToUnicod
f5ed0 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  e(const char *zF
f5ee0 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20  ilename){.  int 
f5ef0 6e 43 68 61 72 3b 0a 20 20 57 43 48 41 52 20 2a  nChar;.  WCHAR *
f5f00 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 3b 0a 0a  zWideFilename;..
f5f10 20 20 6e 43 68 61 72 20 3d 20 4d 75 6c 74 69 42    nChar = MultiB
f5f20 79 74 65 54 6f 57 69 64 65 43 68 61 72 28 43 50  yteToWideChar(CP
f5f30 5f 55 54 46 38 2c 20 30 2c 20 7a 46 69 6c 65 6e  _UTF8, 0, zFilen
f5f40 61 6d 65 2c 20 2d 31 2c 20 4e 55 4c 4c 2c 20 30  ame, -1, NULL, 0
f5f50 29 3b 0a 20 20 7a 57 69 64 65 46 69 6c 65 6e 61  );.  zWideFilena
f5f60 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 43 68  me = malloc( nCh
f5f70 61 72 2a 73 69 7a 65 6f 66 28 7a 57 69 64 65 46  ar*sizeof(zWideF
f5f80 69 6c 65 6e 61 6d 65 5b 30 5d 29 20 29 3b 0a 20  ilename[0]) );. 
f5f90 20 69 66 28 20 7a 57 69 64 65 46 69 6c 65 6e 61   if( zWideFilena
f5fa0 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  me==0 ){.    ret
f5fb0 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 43 68  urn 0;.  }.  nCh
f5fc0 61 72 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f  ar = MultiByteTo
f5fd0 57 69 64 65 43 68 61 72 28 43 50 5f 55 54 46 38  WideChar(CP_UTF8
f5fe0 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20  , 0, zFilename, 
f5ff0 2d 31 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d  -1, zWideFilenam
f6000 65 2c 20 6e 43 68 61 72 29 3b 0a 20 20 69 66 28  e, nChar);.  if(
f6010 20 6e 43 68 61 72 3d 3d 30 20 29 7b 0a 20 20 20   nChar==0 ){.   
f6020 20 66 72 65 65 28 7a 57 69 64 65 46 69 6c 65 6e   free(zWideFilen
f6030 61 6d 65 29 3b 0a 20 20 20 20 7a 57 69 64 65 46  ame);.    zWideF
f6040 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d  ilename = 0;.  }
f6050 0a 20 20 72 65 74 75 72 6e 20 7a 57 69 64 65 46  .  return zWideF
f6060 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  ilename;.}../*.*
f6070 2a 20 43 6f 6e 76 65 72 74 20 6d 69 63 72 6f 73  * Convert micros
f6080 6f 66 74 20 75 6e 69 63 6f 64 65 20 74 6f 20 55  oft unicode to U
f6090 54 46 2d 38 2e 20 20 53 70 61 63 65 20 74 6f 20  TF-8.  Space to 
f60a0 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65  hold the returne
f60b0 64 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 6f  d string is.** o
f60c0 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c  btained from mal
f60d0 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  loc()..*/.static
f60e0 20 63 68 61 72 20 2a 75 6e 69 63 6f 64 65 54 6f   char *unicodeTo
f60f0 55 74 66 38 28 63 6f 6e 73 74 20 57 43 48 41 52  Utf8(const WCHAR
f6100 20 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29   *zWideFilename)
f6110 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20  {.  int nByte;. 
f6120 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
f6130 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 64  ;..  nByte = Wid
f6140 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65  eCharToMultiByte
f6150 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 57 69  (CP_UTF8, 0, zWi
f6160 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20  deFilename, -1, 
f6170 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7a  0, 0, 0, 0);.  z
f6180 46 69 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f  Filename = mallo
f6190 63 28 20 6e 42 79 74 65 20 29 3b 0a 20 20 69 66  c( nByte );.  if
f61a0 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 29  ( zFilename==0 )
f61b0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
f61c0 20 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20 57 69    }.  nByte = Wi
f61d0 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74  deCharToMultiByt
f61e0 65 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 57  e(CP_UTF8, 0, zW
f61f0 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c  ideFilename, -1,
f6200 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74   zFilename, nByt
f6210 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e,.             
f6220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f6230 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 42   0, 0);.  if( nB
f6240 79 74 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20  yte == 0 ){.    
f6250 66 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b  free(zFilename);
f6260 0a 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d  .    zFilename =
f6270 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   0;.  }.  return
f6280 20 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f   zFilename;.}../
f6290 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 6e 20  *.** Convert an 
f62a0 61 6e 73 69 20 73 74 72 69 6e 67 20 74 6f 20 6d  ansi string to m
f62b0 69 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65  icrosoft unicode
f62c0 2c 20 62 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a  , based on the.*
f62d0 2a 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61  * current codepa
f62e0 67 65 20 73 65 74 74 69 6e 67 73 20 66 6f 72 20  ge settings for 
f62f0 66 69 6c 65 20 61 70 69 73 2e 0a 2a 2a 20 0a 2a  file apis..** .*
f6300 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20  * Space to hold 
f6310 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72  the returned str
f6320 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 0a  ing is obtained.
f6330 2a 2a 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a  ** from malloc..
f6340 2a 2f 0a 73 74 61 74 69 63 20 57 43 48 41 52 20  */.static WCHAR 
f6350 2a 6d 62 63 73 54 6f 55 6e 69 63 6f 64 65 28 63  *mbcsToUnicode(c
f6360 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
f6370 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79  name){.  int nBy
f6380 74 65 3b 0a 20 20 57 43 48 41 52 20 2a 7a 4d 62  te;.  WCHAR *zMb
f6390 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 69 6e  csFilename;.  in
f63a0 74 20 63 6f 64 65 70 61 67 65 20 3d 20 41 72 65  t codepage = Are
f63b0 46 69 6c 65 41 70 69 73 41 4e 53 49 28 29 20 3f  FileApisANSI() ?
f63c0 20 43 50 5f 41 43 50 20 3a 20 43 50 5f 4f 45 4d   CP_ACP : CP_OEM
f63d0 43 50 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 4d  CP;..  nByte = M
f63e0 75 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43 68  ultiByteToWideCh
f63f0 61 72 28 63 6f 64 65 70 61 67 65 2c 20 30 2c 20  ar(codepage, 0, 
f6400 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 4e  zFilename, -1, N
f6410 55 4c 4c 2c 30 29 2a 73 69 7a 65 6f 66 28 57 43  ULL,0)*sizeof(WC
f6420 48 41 52 29 3b 0a 20 20 7a 4d 62 63 73 46 69 6c  HAR);.  zMbcsFil
f6430 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20  ename = malloc( 
f6440 6e 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a 4d 62  nByte*sizeof(zMb
f6450 63 73 46 69 6c 65 6e 61 6d 65 5b 30 5d 29 20 29  csFilename[0]) )
f6460 3b 0a 20 20 69 66 28 20 7a 4d 62 63 73 46 69 6c  ;.  if( zMbcsFil
f6470 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20  ename==0 ){.    
f6480 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
f6490 6e 42 79 74 65 20 3d 20 4d 75 6c 74 69 42 79 74  nByte = MultiByt
f64a0 65 54 6f 57 69 64 65 43 68 61 72 28 63 6f 64 65  eToWideChar(code
f64b0 70 61 67 65 2c 20 30 2c 20 7a 46 69 6c 65 6e 61  page, 0, zFilena
f64c0 6d 65 2c 20 2d 31 2c 20 7a 4d 62 63 73 46 69 6c  me, -1, zMbcsFil
f64d0 65 6e 61 6d 65 2c 20 6e 42 79 74 65 29 3b 0a 20  ename, nByte);. 
f64e0 20 69 66 28 20 6e 42 79 74 65 3d 3d 30 20 29 7b   if( nByte==0 ){
f64f0 0a 20 20 20 20 66 72 65 65 28 7a 4d 62 63 73 46  .    free(zMbcsF
f6500 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 4d  ilename);.    zM
f6510 62 63 73 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b  bcsFilename = 0;
f6520 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4d  .  }.  return zM
f6530 62 63 73 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a  bcsFilename;.}..
f6540 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 69  /*.** Convert mi
f6550 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 20  crosoft unicode 
f6560 74 6f 20 6d 75 6c 74 69 62 79 74 65 20 63 68 61  to multibyte cha
f6570 72 61 63 74 65 72 20 73 74 72 69 6e 67 2c 20 62  racter string, b
f6580 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 75  ased on the.** u
f6590 73 65 72 27 73 20 41 6e 73 69 20 63 6f 64 65 70  ser's Ansi codep
f65a0 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65  age..**.** Space
f65b0 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74   to hold the ret
f65c0 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20  urned string is 
f65d0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a  obtained from.**
f65e0 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74   malloc()..*/.st
f65f0 61 74 69 63 20 63 68 61 72 20 2a 75 6e 69 63 6f  atic char *unico
f6600 64 65 54 6f 4d 62 63 73 28 63 6f 6e 73 74 20 57  deToMbcs(const W
f6610 43 48 41 52 20 2a 7a 57 69 64 65 46 69 6c 65 6e  CHAR *zWideFilen
f6620 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74  ame){.  int nByt
f6630 65 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65  e;.  char *zFile
f6640 6e 61 6d 65 3b 0a 20 20 69 6e 74 20 63 6f 64 65  name;.  int code
f6650 70 61 67 65 20 3d 20 41 72 65 46 69 6c 65 41 70  page = AreFileAp
f6660 69 73 41 4e 53 49 28 29 20 3f 20 43 50 5f 41 43  isANSI() ? CP_AC
f6670 50 20 3a 20 43 50 5f 4f 45 4d 43 50 3b 0a 0a 20  P : CP_OEMCP;.. 
f6680 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68 61   nByte = WideCha
f6690 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 63 6f 64  rToMultiByte(cod
f66a0 65 70 61 67 65 2c 20 30 2c 20 7a 57 69 64 65 46  epage, 0, zWideF
f66b0 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 30 2c 20  ilename, -1, 0, 
f66c0 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7a 46 69 6c  0, 0, 0);.  zFil
f66d0 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20  ename = malloc( 
f66e0 6e 42 79 74 65 20 29 3b 0a 20 20 69 66 28 20 7a  nByte );.  if( z
f66f0 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20  Filename==0 ){. 
f6700 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
f6710 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43  .  nByte = WideC
f6720 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 63  harToMultiByte(c
f6730 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a 57 69 64  odepage, 0, zWid
f6740 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a  eFilename, -1, z
f6750 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 65 2c  Filename, nByte,
f6760 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
f6770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
f6780 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 42 79 74  , 0);.  if( nByt
f6790 65 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 66 72  e == 0 ){.    fr
f67a0 65 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  ee(zFilename);. 
f67b0 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 30     zFilename = 0
f67c0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a  ;.  }.  return z
f67d0 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a  Filename;.}../*.
f67e0 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 75 6c 74 69  ** Convert multi
f67f0 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 73  byte character s
f6800 74 72 69 6e 67 20 74 6f 20 55 54 46 2d 38 2e 20  tring to UTF-8. 
f6810 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74   Space to hold t
f6820 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 73  he.** returned s
f6830 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65  tring is obtaine
f6840 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e  d from malloc().
f6850 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
f6860 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 77 69 6e  har *sqlite3_win
f6870 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28  32_mbcs_to_utf8(
f6880 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
f6890 65 6e 61 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a  ename){.  char *
f68a0 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38 3b 0a 20  zFilenameUtf8;. 
f68b0 20 57 43 48 41 52 20 2a 7a 54 6d 70 57 69 64 65   WCHAR *zTmpWide
f68c0 3b 0a 0a 20 20 7a 54 6d 70 57 69 64 65 20 3d 20  ;..  zTmpWide = 
f68d0 6d 62 63 73 54 6f 55 6e 69 63 6f 64 65 28 7a 46  mbcsToUnicode(zF
f68e0 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 69 66 28 20  ilename);.  if( 
f68f0 7a 54 6d 70 57 69 64 65 3d 3d 30 20 29 7b 0a 20  zTmpWide==0 ){. 
f6900 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
f6910 0a 20 20 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38  .  zFilenameUtf8
f6920 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74 66 38   = unicodeToUtf8
f6930 28 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20 66 72  (zTmpWide);.  fr
f6940 65 65 28 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20  ee(zTmpWide);.  
f6950 72 65 74 75 72 6e 20 7a 46 69 6c 65 6e 61 6d 65  return zFilename
f6960 55 74 66 38 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  Utf8;.}../*.** C
f6970 6f 6e 76 65 72 74 20 55 54 46 2d 38 20 74 6f 20  onvert UTF-8 to 
f6980 6d 75 6c 74 69 62 79 74 65 20 63 68 61 72 61 63  multibyte charac
f6990 74 65 72 20 73 74 72 69 6e 67 2e 20 20 53 70 61  ter string.  Spa
f69a0 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 0a  ce to hold the .
f69b0 2a 2a 20 72 65 74 75 72 6e 65 64 20 73 74 72 69  ** returned stri
f69c0 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66  ng is obtained f
f69d0 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f  rom malloc()..*/
f69e0 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 75 74  .static char *ut
f69f0 66 38 54 6f 4d 62 63 73 28 63 6f 6e 73 74 20 63  f8ToMbcs(const c
f6a00 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b  har *zFilename){
f6a10 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  .  char *zFilena
f6a20 6d 65 4d 62 63 73 3b 0a 20 20 57 43 48 41 52 20  meMbcs;.  WCHAR 
f6a30 2a 7a 54 6d 70 57 69 64 65 3b 0a 0a 20 20 7a 54  *zTmpWide;..  zT
f6a40 6d 70 57 69 64 65 20 3d 20 75 74 66 38 54 6f 55  mpWide = utf8ToU
f6a50 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65  nicode(zFilename
f6a60 29 3b 0a 20 20 69 66 28 20 7a 54 6d 70 57 69 64  );.  if( zTmpWid
f6a70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  e==0 ){.    retu
f6a80 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 46 69 6c  rn 0;.  }.  zFil
f6a90 65 6e 61 6d 65 4d 62 63 73 20 3d 20 75 6e 69 63  enameMbcs = unic
f6aa0 6f 64 65 54 6f 4d 62 63 73 28 7a 54 6d 70 57 69  odeToMbcs(zTmpWi
f6ab0 64 65 29 3b 0a 20 20 66 72 65 65 28 7a 54 6d 70  de);.  free(zTmp
f6ac0 57 69 64 65 29 3b 0a 20 20 72 65 74 75 72 6e 20  Wide);.  return 
f6ad0 7a 46 69 6c 65 6e 61 6d 65 4d 62 63 73 3b 0a 7d  zFilenameMbcs;.}
f6ae0 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  ..#if SQLITE_OS_
f6af0 57 49 4e 43 45 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  WINCE./*********
f6b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f6b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f6b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f6b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f6b40 0a 2a 2a 20 54 68 69 73 20 73 65 63 74 69 6f 6e  .** This section
f6b50 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 66   contains code f
f6b60 6f 72 20 57 69 6e 43 45 20 6f 6e 6c 79 2e 0a 2a  or WinCE only..*
f6b70 2f 0a 2f 2a 0a 2a 2a 20 57 69 6e 64 6f 77 73 43  /./*.** WindowsC
f6b80 45 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20  E does not have 
f6b90 61 20 6c 6f 63 61 6c 74 69 6d 65 28 29 20 66 75  a localtime() fu
f6ba0 6e 63 74 69 6f 6e 2e 20 20 53 6f 20 63 72 65 61  nction.  So crea
f6bb0 74 65 20 61 0a 2a 2a 20 73 75 62 73 74 69 74 75  te a.** substitu
f6bc0 74 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 74 6d  te..*/.struct tm
f6bd0 20 2a 5f 5f 63 64 65 63 6c 20 6c 6f 63 61 6c 74   *__cdecl localt
f6be0 69 6d 65 28 63 6f 6e 73 74 20 74 69 6d 65 5f 74  ime(const time_t
f6bf0 20 2a 74 29 0a 7b 0a 20 20 73 74 61 74 69 63 20   *t).{.  static 
f6c00 73 74 72 75 63 74 20 74 6d 20 79 3b 0a 20 20 46  struct tm y;.  F
f6c10 49 4c 45 54 49 4d 45 20 75 54 6d 2c 20 6c 54 6d  ILETIME uTm, lTm
f6c20 3b 0a 20 20 53 59 53 54 45 4d 54 49 4d 45 20 70  ;.  SYSTEMTIME p
f6c30 54 6d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e  Tm;.  sqlite3_in
f6c40 74 36 34 20 74 36 34 3b 0a 20 20 74 36 34 20 3d  t64 t64;.  t64 =
f6c50 20 2a 74 3b 0a 20 20 74 36 34 20 3d 20 28 74 36   *t;.  t64 = (t6
f6c60 34 20 2b 20 31 31 36 34 34 34 37 33 36 30 30 29  4 + 11644473600)
f6c70 2a 31 30 30 30 30 30 30 30 3b 0a 20 20 75 54 6d  *10000000;.  uTm
f6c80 2e 64 77 4c 6f 77 44 61 74 65 54 69 6d 65 20 3d  .dwLowDateTime =
f6c90 20 28 44 57 4f 52 44 29 28 74 36 34 20 26 20 30   (DWORD)(t64 & 0
f6ca0 78 46 46 46 46 46 46 46 46 29 3b 0a 20 20 75 54  xFFFFFFFF);.  uT
f6cb0 6d 2e 64 77 48 69 67 68 44 61 74 65 54 69 6d 65  m.dwHighDateTime
f6cc0 3d 20 28 44 57 4f 52 44 29 28 74 36 34 20 3e 3e  = (DWORD)(t64 >>
f6cd0 20 33 32 29 3b 0a 20 20 46 69 6c 65 54 69 6d 65   32);.  FileTime
f6ce0 54 6f 4c 6f 63 61 6c 46 69 6c 65 54 69 6d 65 28  ToLocalFileTime(
f6cf0 26 75 54 6d 2c 26 6c 54 6d 29 3b 0a 20 20 46 69  &uTm,&lTm);.  Fi
f6d00 6c 65 54 69 6d 65 54 6f 53 79 73 74 65 6d 54 69  leTimeToSystemTi
f6d10 6d 65 28 26 6c 54 6d 2c 26 70 54 6d 29 3b 0a 20  me(&lTm,&pTm);. 
f6d20 20 79 2e 74 6d 5f 79 65 61 72 20 3d 20 70 54 6d   y.tm_year = pTm
f6d30 2e 77 59 65 61 72 20 2d 20 31 39 30 30 3b 0a 20  .wYear - 1900;. 
f6d40 20 79 2e 74 6d 5f 6d 6f 6e 20 3d 20 70 54 6d 2e   y.tm_mon = pTm.
f6d50 77 4d 6f 6e 74 68 20 2d 20 31 3b 0a 20 20 79 2e  wMonth - 1;.  y.
f6d60 74 6d 5f 77 64 61 79 20 3d 20 70 54 6d 2e 77 44  tm_wday = pTm.wD
f6d70 61 79 4f 66 57 65 65 6b 3b 0a 20 20 79 2e 74 6d  ayOfWeek;.  y.tm
f6d80 5f 6d 64 61 79 20 3d 20 70 54 6d 2e 77 44 61 79  _mday = pTm.wDay
f6d90 3b 0a 20 20 79 2e 74 6d 5f 68 6f 75 72 20 3d 20  ;.  y.tm_hour = 
f6da0 70 54 6d 2e 77 48 6f 75 72 3b 0a 20 20 79 2e 74  pTm.wHour;.  y.t
f6db0 6d 5f 6d 69 6e 20 3d 20 70 54 6d 2e 77 4d 69 6e  m_min = pTm.wMin
f6dc0 75 74 65 3b 0a 20 20 79 2e 74 6d 5f 73 65 63 20  ute;.  y.tm_sec 
f6dd0 3d 20 70 54 6d 2e 77 53 65 63 6f 6e 64 3b 0a 20  = pTm.wSecond;. 
f6de0 20 72 65 74 75 72 6e 20 26 79 3b 0a 7d 0a 0a 2f   return &y;.}../
f6df0 2a 20 54 68 69 73 20 77 69 6c 6c 20 6e 65 76 65  * This will neve
f6e00 72 20 62 65 20 63 61 6c 6c 65 64 2c 20 62 75 74  r be called, but
f6e10 20 64 65 66 69 6e 65 64 20 74 6f 20 6d 61 6b 65   defined to make
f6e20 20 74 68 65 20 63 6f 64 65 20 63 6f 6d 70 69 6c   the code compil
f6e30 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 47 65 74  e */.#define Get
f6e40 54 65 6d 70 50 61 74 68 41 28 61 2c 62 29 0a 0a  TempPathA(a,b)..
f6e50 23 64 65 66 69 6e 65 20 4c 6f 63 6b 46 69 6c 65  #define LockFile
f6e60 28 61 2c 62 2c 63 2c 64 2c 65 29 20 20 20 20 20  (a,b,c,d,e)     
f6e70 20 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28    winceLockFile(
f6e80 26 61 2c 20 62 2c 20 63 2c 20 64 2c 20 65 29 0a  &a, b, c, d, e).
f6e90 23 64 65 66 69 6e 65 20 55 6e 6c 6f 63 6b 46 69  #define UnlockFi
f6ea0 6c 65 28 61 2c 62 2c 63 2c 64 2c 65 29 20 20 20  le(a,b,c,d,e)   
f6eb0 20 20 77 69 6e 63 65 55 6e 6c 6f 63 6b 46 69 6c    winceUnlockFil
f6ec0 65 28 26 61 2c 20 62 2c 20 63 2c 20 64 2c 20 65  e(&a, b, c, d, e
f6ed0 29 0a 23 64 65 66 69 6e 65 20 4c 6f 63 6b 46 69  ).#define LockFi
f6ee0 6c 65 45 78 28 61 2c 62 2c 63 2c 64 2c 65 2c 66  leEx(a,b,c,d,e,f
f6ef0 29 20 20 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c  )   winceLockFil
f6f00 65 45 78 28 26 61 2c 20 62 2c 20 63 2c 20 64 2c  eEx(&a, b, c, d,
f6f10 20 65 2c 20 66 29 0a 0a 23 64 65 66 69 6e 65 20   e, f)..#define 
f6f20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 49 4c  HANDLE_TO_WINFIL
f6f30 45 28 61 29 20 28 77 69 6e 46 69 6c 65 2a 29 26  E(a) (winFile*)&
f6f40 28 28 63 68 61 72 2a 29 61 29 5b 2d 28 69 6e 74  ((char*)a)[-(int
f6f50 29 6f 66 66 73 65 74 6f 66 28 77 69 6e 46 69 6c  )offsetof(winFil
f6f60 65 2c 68 29 5d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71  e,h)]../*.** Acq
f6f70 75 69 72 65 20 61 20 6c 6f 63 6b 20 6f 6e 20 74  uire a lock on t
f6f80 68 65 20 68 61 6e 64 6c 65 20 68 0a 2a 2f 0a 73  he handle h.*/.s
f6f90 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 63 65  tatic void wince
f6fa0 4d 75 74 65 78 41 63 71 75 69 72 65 28 48 41 4e  MutexAcquire(HAN
f6fb0 44 4c 45 20 68 29 7b 0a 20 20 20 44 57 4f 52 44  DLE h){.   DWORD
f6fc0 20 64 77 45 72 72 3b 0a 20 20 20 64 6f 20 7b 0a   dwErr;.   do {.
f6fd0 20 20 20 20 20 64 77 45 72 72 20 3d 20 57 61 69       dwErr = Wai
f6fe0 74 46 6f 72 53 69 6e 67 6c 65 4f 62 6a 65 63 74  tForSingleObject
f6ff0 28 68 2c 20 49 4e 46 49 4e 49 54 45 29 3b 0a 20  (h, INFINITE);. 
f7000 20 20 7d 20 77 68 69 6c 65 20 28 64 77 45 72 72    } while (dwErr
f7010 20 21 3d 20 57 41 49 54 5f 4f 42 4a 45 43 54 5f   != WAIT_OBJECT_
f7020 30 20 26 26 20 64 77 45 72 72 20 21 3d 20 57 41  0 && dwErr != WA
f7030 49 54 5f 41 42 41 4e 44 4f 4e 45 44 29 3b 0a 7d  IT_ABANDONED);.}
f7040 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61  ./*.** Release a
f7050 20 6c 6f 63 6b 20 61 63 71 75 69 72 65 64 20 62   lock acquired b
f7060 79 20 77 69 6e 63 65 4d 75 74 65 78 41 63 71 75  y winceMutexAcqu
f7070 69 72 65 28 29 0a 2a 2f 0a 23 64 65 66 69 6e 65  ire().*/.#define
f7080 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61   winceMutexRelea
f7090 73 65 28 68 29 20 52 65 6c 65 61 73 65 4d 75 74  se(h) ReleaseMut
f70a0 65 78 28 68 29 0a 0a 2f 2a 0a 2a 2a 20 43 72 65  ex(h)../*.** Cre
f70b0 61 74 65 20 74 68 65 20 6d 75 74 65 78 20 61 6e  ate the mutex an
f70c0 64 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20  d shared memory 
f70d0 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67  used for locking
f70e0 20 69 6e 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20   in the file.** 
f70f0 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65  descriptor pFile
f7100 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20  .*/.static BOOL 
f7110 77 69 6e 63 65 43 72 65 61 74 65 4c 6f 63 6b 28  winceCreateLock(
f7120 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
f7130 65 6e 61 6d 65 2c 20 77 69 6e 46 69 6c 65 20 2a  ename, winFile *
f7140 70 46 69 6c 65 29 7b 0a 20 20 57 43 48 41 52 20  pFile){.  WCHAR 
f7150 2a 7a 54 6f 6b 3b 0a 20 20 57 43 48 41 52 20 2a  *zTok;.  WCHAR *
f7160 7a 4e 61 6d 65 20 3d 20 75 74 66 38 54 6f 55 6e  zName = utf8ToUn
f7170 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65 29  icode(zFilename)
f7180 3b 0a 20 20 42 4f 4f 4c 20 62 49 6e 69 74 20 3d  ;.  BOOL bInit =
f7190 20 54 52 55 45 3b 0a 0a 20 20 2f 2a 20 49 6e 69   TRUE;..  /* Ini
f71a0 74 69 61 6c 69 7a 65 20 74 68 65 20 6c 6f 63 61  tialize the loca
f71b0 6c 20 6c 6f 63 6b 64 61 74 61 20 2a 2f 0a 20 20  l lockdata */.  
f71c0 5a 65 72 6f 4d 65 6d 6f 72 79 28 26 70 46 69 6c  ZeroMemory(&pFil
f71d0 65 2d 3e 6c 6f 63 61 6c 2c 20 73 69 7a 65 6f 66  e->local, sizeof
f71e0 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 29 29 3b  (pFile->local));
f71f0 0a 0a 20 20 2f 2a 20 52 65 70 6c 61 63 65 20 74  ..  /* Replace t
f7200 68 65 20 62 61 63 6b 73 6c 61 73 68 65 73 20 66  he backslashes f
f7210 72 6f 6d 20 74 68 65 20 66 69 6c 65 6e 61 6d 65  rom the filename
f7220 20 61 6e 64 20 6c 6f 77 65 72 63 61 73 65 20 69   and lowercase i
f7230 74 0a 20 20 2a 2a 20 74 6f 20 64 65 72 69 76 65  t.  ** to derive
f7240 20 61 20 6d 75 74 65 78 20 6e 61 6d 65 2e 20 2a   a mutex name. *
f7250 2f 0a 20 20 7a 54 6f 6b 20 3d 20 43 68 61 72 4c  /.  zTok = CharL
f7260 6f 77 65 72 57 28 7a 4e 61 6d 65 29 3b 0a 20 20  owerW(zName);.  
f7270 66 6f 72 20 28 3b 2a 7a 54 6f 6b 3b 7a 54 6f 6b  for (;*zTok;zTok
f7280 2b 2b 29 7b 0a 20 20 20 20 69 66 20 28 2a 7a 54  ++){.    if (*zT
f7290 6f 6b 20 3d 3d 20 27 5c 5c 27 29 20 2a 7a 54 6f  ok == '\\') *zTo
f72a0 6b 20 3d 20 27 5f 27 3b 0a 20 20 7d 0a 0a 20 20  k = '_';.  }..  
f72b0 2f 2a 20 43 72 65 61 74 65 2f 6f 70 65 6e 20 74  /* Create/open t
f72c0 68 65 20 6e 61 6d 65 64 20 6d 75 74 65 78 20 2a  he named mutex *
f72d0 2f 0a 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74 65  /.  pFile->hMute
f72e0 78 20 3d 20 43 72 65 61 74 65 4d 75 74 65 78 57  x = CreateMutexW
f72f0 28 4e 55 4c 4c 2c 20 46 41 4c 53 45 2c 20 7a 4e  (NULL, FALSE, zN
f7300 61 6d 65 29 3b 0a 20 20 69 66 20 28 21 70 46 69  ame);.  if (!pFi
f7310 6c 65 2d 3e 68 4d 75 74 65 78 29 7b 0a 20 20 20  le->hMutex){.   
f7320 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
f7330 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72  o = GetLastError
f7340 28 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 4e 61  ();.    free(zNa
f7350 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  me);.    return 
f7360 46 41 4c 53 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  FALSE;.  }..  /*
f7370 20 41 63 71 75 69 72 65 20 74 68 65 20 6d 75 74   Acquire the mut
f7380 65 78 20 62 65 66 6f 72 65 20 63 6f 6e 74 69 6e  ex before contin
f7390 75 69 6e 67 20 2a 2f 0a 20 20 77 69 6e 63 65 4d  uing */.  winceM
f73a0 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69 6c  utexAcquire(pFil
f73b0 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 0a 20  e->hMutex);.  . 
f73c0 20 2f 2a 20 53 69 6e 63 65 20 74 68 65 20 6e 61   /* Since the na
f73d0 6d 65 73 20 6f 66 20 6e 61 6d 65 64 20 6d 75 74  mes of named mut
f73e0 65 78 65 73 2c 20 73 65 6d 61 70 68 6f 72 65 73  exes, semaphores
f73f0 2c 20 66 69 6c 65 20 6d 61 70 70 69 6e 67 73 20  , file mappings 
f7400 65 74 63 20 61 72 65 20 0a 20 20 2a 2a 20 63 61  etc are .  ** ca
f7410 73 65 2d 73 65 6e 73 69 74 69 76 65 2c 20 74 61  se-sensitive, ta
f7420 6b 65 20 61 64 76 61 6e 74 61 67 65 20 6f 66 20  ke advantage of 
f7430 74 68 61 74 20 62 79 20 75 70 70 65 72 63 61 73  that by uppercas
f7440 69 6e 67 20 74 68 65 20 6d 75 74 65 78 20 6e 61  ing the mutex na
f7450 6d 65 0a 20 20 2a 2a 20 61 6e 64 20 75 73 69 6e  me.  ** and usin
f7460 67 20 74 68 61 74 20 61 73 20 74 68 65 20 73 68  g that as the sh
f7470 61 72 65 64 20 66 69 6c 65 6d 61 70 70 69 6e 67  ared filemapping
f7480 20 6e 61 6d 65 2e 0a 20 20 2a 2f 0a 20 20 43 68   name..  */.  Ch
f7490 61 72 55 70 70 65 72 57 28 7a 4e 61 6d 65 29 3b  arUpperW(zName);
f74a0 0a 20 20 70 46 69 6c 65 2d 3e 68 53 68 61 72 65  .  pFile->hShare
f74b0 64 20 3d 20 43 72 65 61 74 65 46 69 6c 65 4d 61  d = CreateFileMa
f74c0 70 70 69 6e 67 57 28 49 4e 56 41 4c 49 44 5f 48  ppingW(INVALID_H
f74d0 41 4e 44 4c 45 5f 56 41 4c 55 45 2c 20 4e 55 4c  ANDLE_VALUE, NUL
f74e0 4c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  L,.             
f74f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f7500 20 20 20 20 20 20 20 20 20 20 50 41 47 45 5f 52            PAGE_R
f7510 45 41 44 57 52 49 54 45 2c 20 30 2c 20 73 69 7a  EADWRITE, 0, siz
f7520 65 6f 66 28 77 69 6e 63 65 4c 6f 63 6b 29 2c 0a  eof(winceLock),.
f7530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f7540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f7550 20 20 20 20 20 20 20 7a 4e 61 6d 65 29 3b 20 20         zName);  
f7560 0a 0a 20 20 2f 2a 20 53 65 74 20 61 20 66 6c 61  ..  /* Set a fla
f7570 67 20 74 68 61 74 20 69 6e 64 69 63 61 74 65 73  g that indicates
f7580 20 77 65 27 72 65 20 74 68 65 20 66 69 72 73 74   we're the first
f7590 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 6d   to create the m
f75a0 65 6d 6f 72 79 20 73 6f 20 69 74 20 0a 20 20 2a  emory so it .  *
f75b0 2a 20 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d 69  * must be zero-i
f75c0 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20  nitialized */.  
f75d0 69 66 20 28 47 65 74 4c 61 73 74 45 72 72 6f 72  if (GetLastError
f75e0 28 29 20 3d 3d 20 45 52 52 4f 52 5f 41 4c 52 45  () == ERROR_ALRE
f75f0 41 44 59 5f 45 58 49 53 54 53 29 7b 0a 20 20 20  ADY_EXISTS){.   
f7600 20 62 49 6e 69 74 20 3d 20 46 41 4c 53 45 3b 0a   bInit = FALSE;.
f7610 20 20 7d 0a 0a 20 20 66 72 65 65 28 7a 4e 61 6d    }..  free(zNam
f7620 65 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20  e);..  /* If we 
f7630 73 75 63 63 65 65 64 65 64 20 69 6e 20 6d 61 6b  succeeded in mak
f7640 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6d  ing the shared m
f7650 65 6d 6f 72 79 20 68 61 6e 64 6c 65 2c 20 6d 61  emory handle, ma
f7660 70 20 69 74 2e 20 2a 2f 0a 20 20 69 66 20 28 70  p it. */.  if (p
f7670 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 29 7b 0a  File->hShared){.
f7680 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65      pFile->share
f7690 64 20 3d 20 28 77 69 6e 63 65 4c 6f 63 6b 2a 29  d = (winceLock*)
f76a0 4d 61 70 56 69 65 77 4f 66 46 69 6c 65 28 70 46  MapViewOfFile(pF
f76b0 69 6c 65 2d 3e 68 53 68 61 72 65 64 2c 20 0a 20  ile->hShared, . 
f76c0 20 20 20 20 20 20 20 20 20 20 20 20 46 49 4c 45              FILE
f76d0 5f 4d 41 50 5f 52 45 41 44 7c 46 49 4c 45 5f 4d  _MAP_READ|FILE_M
f76e0 41 50 5f 57 52 49 54 45 2c 20 30 2c 20 30 2c 20  AP_WRITE, 0, 0, 
f76f0 73 69 7a 65 6f 66 28 77 69 6e 63 65 4c 6f 63 6b  sizeof(winceLock
f7700 29 29 3b 0a 20 20 20 20 2f 2a 20 49 66 20 6d 61  ));.    /* If ma
f7710 70 70 69 6e 67 20 66 61 69 6c 65 64 2c 20 63 6c  pping failed, cl
f7720 6f 73 65 20 74 68 65 20 73 68 61 72 65 64 20 6d  ose the shared m
f7730 65 6d 6f 72 79 20 68 61 6e 64 6c 65 20 61 6e 64  emory handle and
f7740 20 65 72 61 73 65 20 69 74 20 2a 2f 0a 20 20 20   erase it */.   
f7750 20 69 66 20 28 21 70 46 69 6c 65 2d 3e 73 68 61   if (!pFile->sha
f7760 72 65 64 29 7b 0a 20 20 20 20 20 20 70 46 69 6c  red){.      pFil
f7770 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47  e->lastErrno = G
f7780 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20  etLastError();. 
f7790 20 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65       CloseHandle
f77a0 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 29  (pFile->hShared)
f77b0 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68  ;.      pFile->h
f77c0 53 68 61 72 65 64 20 3d 20 4e 55 4c 4c 3b 0a 20  Shared = NULL;. 
f77d0 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49     }.  }..  /* I
f77e0 66 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20  f shared memory 
f77f0 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 72 65  could not be cre
f7800 61 74 65 64 2c 20 74 68 65 6e 20 63 6c 6f 73 65  ated, then close
f7810 20 74 68 65 20 6d 75 74 65 78 20 61 6e 64 20 66   the mutex and f
f7820 61 69 6c 20 2a 2f 0a 20 20 69 66 20 28 70 46 69  ail */.  if (pFi
f7830 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d 3d 20 4e  le->hShared == N
f7840 55 4c 4c 29 7b 0a 20 20 20 20 77 69 6e 63 65 4d  ULL){.    winceM
f7850 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c  utexRelease(pFil
f7860 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 20 20  e->hMutex);.    
f7870 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c  CloseHandle(pFil
f7880 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 20 20  e->hMutex);.    
f7890 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 20 3d 20  pFile->hMutex = 
f78a0 4e 55 4c 4c 3b 0a 20 20 20 20 72 65 74 75 72 6e  NULL;.    return
f78b0 20 46 41 4c 53 45 3b 0a 20 20 7d 0a 20 20 0a 20   FALSE;.  }.  . 
f78c0 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74   /* Initialize t
f78d0 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79  he shared memory
f78e0 20 69 66 20 77 65 27 72 65 20 73 75 70 70 6f 73   if we're suppos
f78f0 65 64 20 74 6f 20 2a 2f 0a 20 20 69 66 20 28 62  ed to */.  if (b
f7900 49 6e 69 74 29 20 7b 0a 20 20 20 20 5a 65 72 6f  Init) {.    Zero
f7910 4d 65 6d 6f 72 79 28 70 46 69 6c 65 2d 3e 73 68  Memory(pFile->sh
f7920 61 72 65 64 2c 20 73 69 7a 65 6f 66 28 77 69 6e  ared, sizeof(win
f7930 63 65 4c 6f 63 6b 29 29 3b 0a 20 20 7d 0a 0a 20  ceLock));.  }.. 
f7940 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61   winceMutexRelea
f7950 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78  se(pFile->hMutex
f7960 29 3b 0a 20 20 72 65 74 75 72 6e 20 54 52 55 45  );.  return TRUE
f7970 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 74 72  ;.}../*.** Destr
f7980 6f 79 20 74 68 65 20 70 61 72 74 20 6f 66 20 77  oy the part of w
f7990 69 6e 46 69 6c 65 20 74 68 61 74 20 64 65 61 6c  inFile that deal
f79a0 73 20 77 69 74 68 20 77 69 6e 63 65 20 6c 6f 63  s with wince loc
f79b0 6b 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  ks.*/.static voi
f79c0 64 20 77 69 6e 63 65 44 65 73 74 72 6f 79 4c 6f  d winceDestroyLo
f79d0 63 6b 28 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c  ck(winFile *pFil
f79e0 65 29 7b 0a 20 20 69 66 20 28 70 46 69 6c 65 2d  e){.  if (pFile-
f79f0 3e 68 4d 75 74 65 78 29 7b 0a 20 20 20 20 2f 2a  >hMutex){.    /*
f7a00 20 41 63 71 75 69 72 65 20 74 68 65 20 6d 75 74   Acquire the mut
f7a10 65 78 20 2a 2f 0a 20 20 20 20 77 69 6e 63 65 4d  ex */.    winceM
f7a20 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69 6c  utexAcquire(pFil
f7a30 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 20 20  e->hMutex);..   
f7a40 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e   /* The followin
f7a50 67 20 62 6c 6f 63 6b 73 20 73 68 6f 75 6c 64 20  g blocks should 
f7a60 70 72 6f 62 61 62 6c 79 20 61 73 73 65 72 74 20  probably assert 
f7a70 69 6e 20 64 65 62 75 67 20 6d 6f 64 65 2c 20 62  in debug mode, b
f7a80 75 74 20 74 68 65 79 0a 20 20 20 20 20 20 20 61  ut they.       a
f7a90 72 65 20 74 6f 20 63 6c 65 61 6e 75 70 20 69 6e  re to cleanup in
f7aa0 20 63 61 73 65 20 61 6e 79 20 6c 6f 63 6b 73 20   case any locks 
f7ab0 72 65 6d 61 69 6e 65 64 20 6f 70 65 6e 20 2a 2f  remained open */
f7ac0 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e  .    if (pFile->
f7ad0 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 29 7b  local.nReaders){
f7ae0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68  .      pFile->sh
f7af0 61 72 65 64 2d 3e 6e 52 65 61 64 65 72 73 20 2d  ared->nReaders -
f7b00 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20  -;.    }.    if 
f7b10 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52  (pFile->local.bR
f7b20 65 73 65 72 76 65 64 29 7b 0a 20 20 20 20 20 20  eserved){.      
f7b30 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62  pFile->shared->b
f7b40 52 65 73 65 72 76 65 64 20 3d 20 46 41 4c 53 45  Reserved = FALSE
f7b50 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28  ;.    }.    if (
f7b60 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65  pFile->local.bPe
f7b70 6e 64 69 6e 67 29 7b 0a 20 20 20 20 20 20 70 46  nding){.      pF
f7b80 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65  ile->shared->bPe
f7b90 6e 64 69 6e 67 20 3d 20 46 41 4c 53 45 3b 0a 20  nding = FALSE;. 
f7ba0 20 20 20 7d 0a 20 20 20 20 69 66 20 28 70 46 69     }.    if (pFi
f7bb0 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78 63 6c 75  le->local.bExclu
f7bc0 73 69 76 65 29 7b 0a 20 20 20 20 20 20 70 46 69  sive){.      pFi
f7bd0 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 63  le->shared->bExc
f7be0 6c 75 73 69 76 65 20 3d 20 46 41 4c 53 45 3b 0a  lusive = FALSE;.
f7bf0 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65      }..    /* De
f7c00 2d 72 65 66 65 72 65 6e 63 65 20 61 6e 64 20 63  -reference and c
f7c10 6c 6f 73 65 20 6f 75 72 20 63 6f 70 79 20 6f 66  lose our copy of
f7c20 20 74 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f   the shared memo
f7c30 72 79 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 20  ry handle */.   
f7c40 20 55 6e 6d 61 70 56 69 65 77 4f 66 46 69 6c 65   UnmapViewOfFile
f7c50 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 29 3b  (pFile->shared);
f7c60 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65  .    CloseHandle
f7c70 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 29  (pFile->hShared)
f7c80 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f 6e 65 20 77  ;..    /* Done w
f7c90 69 74 68 20 74 68 65 20 6d 75 74 65 78 20 2a 2f  ith the mutex */
f7ca0 0a 20 20 20 20 77 69 6e 63 65 4d 75 74 65 78 52  .    winceMutexR
f7cb0 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d  elease(pFile->hM
f7cc0 75 74 65 78 29 3b 20 20 20 20 0a 20 20 20 20 43  utex);    .    C
f7cd0 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c 65  loseHandle(pFile
f7ce0 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 20 20 70  ->hMutex);.    p
f7cf0 46 69 6c 65 2d 3e 68 4d 75 74 65 78 20 3d 20 4e  File->hMutex = N
f7d00 55 4c 4c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a  ULL;.  }.}../* .
f7d10 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e 74 61  ** An implementa
f7d20 74 69 6f 6e 20 6f 66 20 74 68 65 20 4c 6f 63 6b  tion of the Lock
f7d30 46 69 6c 65 28 29 20 41 50 49 20 6f 66 20 77 69  File() API of wi
f7d40 6e 64 6f 77 73 20 66 6f 72 20 77 69 6e 63 65 0a  ndows for wince.
f7d50 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20 77  */.static BOOL w
f7d60 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28 0a 20 20  inceLockFile(.  
f7d70 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c 65 2c 0a  HANDLE *phFile,.
f7d80 20 20 44 57 4f 52 44 20 64 77 46 69 6c 65 4f 66    DWORD dwFileOf
f7d90 66 73 65 74 4c 6f 77 2c 0a 20 20 44 57 4f 52 44  fsetLow,.  DWORD
f7da0 20 64 77 46 69 6c 65 4f 66 66 73 65 74 48 69 67   dwFileOffsetHig
f7db0 68 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62  h,.  DWORD nNumb
f7dc0 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c  erOfBytesToLockL
f7dd0 6f 77 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d  ow,.  DWORD nNum
f7de0 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b  berOfBytesToLock
f7df0 48 69 67 68 0a 29 7b 0a 20 20 77 69 6e 46 69 6c  High.){.  winFil
f7e00 65 20 2a 70 46 69 6c 65 20 3d 20 48 41 4e 44 4c  e *pFile = HANDL
f7e10 45 5f 54 4f 5f 57 49 4e 46 49 4c 45 28 70 68 46  E_TO_WINFILE(phF
f7e20 69 6c 65 29 3b 0a 20 20 42 4f 4f 4c 20 62 52 65  ile);.  BOOL bRe
f7e30 74 75 72 6e 20 3d 20 46 41 4c 53 45 3b 0a 0a 20  turn = FALSE;.. 
f7e40 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
f7e50 52 28 64 77 46 69 6c 65 4f 66 66 73 65 74 48 69  R(dwFileOffsetHi
f7e60 67 68 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  gh);.  UNUSED_PA
f7e70 52 41 4d 45 54 45 52 28 6e 4e 75 6d 62 65 72 4f  RAMETER(nNumberO
f7e80 66 42 79 74 65 73 54 6f 4c 6f 63 6b 48 69 67 68  fBytesToLockHigh
f7e90 29 3b 0a 0a 20 20 69 66 20 28 21 70 46 69 6c 65  );..  if (!pFile
f7ea0 2d 3e 68 4d 75 74 65 78 29 20 72 65 74 75 72 6e  ->hMutex) return
f7eb0 20 54 52 55 45 3b 0a 20 20 77 69 6e 63 65 4d 75   TRUE;.  winceMu
f7ec0 74 65 78 41 63 71 75 69 72 65 28 70 46 69 6c 65  texAcquire(pFile
f7ed0 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 20 2f 2a  ->hMutex);..  /*
f7ee0 20 57 61 6e 74 69 6e 67 20 61 6e 20 65 78 63 6c   Wanting an excl
f7ef0 75 73 69 76 65 20 6c 6f 63 6b 3f 20 2a 2f 0a 20  usive lock? */. 
f7f00 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65   if (dwFileOffse
f7f10 74 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 53  tLow == (DWORD)S
f7f20 48 41 52 45 44 5f 46 49 52 53 54 0a 20 20 20 20  HARED_FIRST.    
f7f30 20 20 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42     && nNumberOfB
f7f40 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d  ytesToLockLow ==
f7f50 20 28 44 57 4f 52 44 29 53 48 41 52 45 44 5f 53   (DWORD)SHARED_S
f7f60 49 5a 45 29 7b 0a 20 20 20 20 69 66 20 28 70 46  IZE){.    if (pF
f7f70 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 65  ile->shared->nRe
f7f80 61 64 65 72 73 20 3d 3d 20 30 20 26 26 20 70 46  aders == 0 && pF
f7f90 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78  ile->shared->bEx
f7fa0 63 6c 75 73 69 76 65 20 3d 3d 20 30 29 7b 0a 20  clusive == 0){. 
f7fb0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61        pFile->sha
f7fc0 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 20  red->bExclusive 
f7fd0 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 20 20 70  = TRUE;.       p
f7fe0 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78 63  File->local.bExc
f7ff0 6c 75 73 69 76 65 20 3d 20 54 52 55 45 3b 0a 20  lusive = TRUE;. 
f8000 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20        bReturn = 
f8010 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  TRUE;.    }.  }.
f8020 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20 72 65 61  .  /* Want a rea
f8030 64 2d 6f 6e 6c 79 20 6c 6f 63 6b 3f 20 2a 2f 0a  d-only lock? */.
f8040 20 20 65 6c 73 65 20 69 66 20 28 64 77 46 69 6c    else if (dwFil
f8050 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44  eOffsetLow == (D
f8060 57 4f 52 44 29 53 48 41 52 45 44 5f 46 49 52 53  WORD)SHARED_FIRS
f8070 54 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20  T &&.           
f8080 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f  nNumberOfBytesTo
f8090 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20  LockLow == 1){. 
f80a0 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 73 68     if (pFile->sh
f80b0 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 65  ared->bExclusive
f80c0 20 3d 3d 20 30 29 7b 0a 20 20 20 20 20 20 70 46   == 0){.      pF
f80d0 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64  ile->local.nRead
f80e0 65 72 73 20 2b 2b 3b 0a 20 20 20 20 20 20 69 66  ers ++;.      if
f80f0 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e   (pFile->local.n
f8100 52 65 61 64 65 72 73 20 3d 3d 20 31 29 7b 0a 20  Readers == 1){. 
f8110 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68         pFile->sh
f8120 61 72 65 64 2d 3e 6e 52 65 61 64 65 72 73 20 2b  ared->nReaders +
f8130 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  +;.      }.     
f8140 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b   bReturn = TRUE;
f8150 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
f8160 20 57 61 6e 74 20 61 20 70 65 6e 64 69 6e 67 20   Want a pending 
f8170 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 65 6c 73 65 20  lock? */.  else 
f8180 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74  if (dwFileOffset
f8190 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 50 45  Low == (DWORD)PE
f81a0 4e 44 49 4e 47 5f 42 59 54 45 20 26 26 20 6e 4e  NDING_BYTE && nN
f81b0 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f  umberOfBytesToLo
f81c0 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20  ckLow == 1){.   
f81d0 20 2f 2a 20 49 66 20 6e 6f 20 70 65 6e 64 69 6e   /* If no pendin
f81e0 67 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20  g lock has been 
f81f0 61 63 71 75 69 72 65 64 2c 20 74 68 65 6e 20 61  acquired, then a
f8200 63 71 75 69 72 65 20 69 74 20 2a 2f 0a 20 20 20  cquire it */.   
f8210 20 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 61 72   if (pFile->shar
f8220 65 64 2d 3e 62 50 65 6e 64 69 6e 67 20 3d 3d 20  ed->bPending == 
f8230 30 29 20 7b 0a 20 20 20 20 20 20 70 46 69 6c 65  0) {.      pFile
f8240 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65 6e 64 69  ->shared->bPendi
f8250 6e 67 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 20  ng = TRUE;.     
f8260 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50   pFile->local.bP
f8270 65 6e 64 69 6e 67 20 3d 20 54 52 55 45 3b 0a 20  ending = TRUE;. 
f8280 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54       bReturn = T
f8290 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  RUE;.    }.  }..
f82a0 20 20 2f 2a 20 57 61 6e 74 20 61 20 72 65 73 65    /* Want a rese
f82b0 72 76 65 64 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20  rved lock? */.  
f82c0 65 6c 73 65 20 69 66 20 28 64 77 46 69 6c 65 4f  else if (dwFileO
f82d0 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44 57 4f  ffsetLow == (DWO
f82e0 52 44 29 52 45 53 45 52 56 45 44 5f 42 59 54 45  RD)RESERVED_BYTE
f82f0 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74   && nNumberOfByt
f8300 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31  esToLockLow == 1
f8310 29 7b 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65  ){.    if (pFile
f8320 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65 73 65 72  ->shared->bReser
f8330 76 65 64 20 3d 3d 20 30 29 20 7b 0a 20 20 20 20  ved == 0) {.    
f8340 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d    pFile->shared-
f8350 3e 62 52 65 73 65 72 76 65 64 20 3d 20 54 52 55  >bReserved = TRU
f8360 45 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  E;.      pFile->
f8370 6c 6f 63 61 6c 2e 62 52 65 73 65 72 76 65 64 20  local.bReserved 
f8380 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 20 62 52  = TRUE;.      bR
f8390 65 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20  eturn = TRUE;.  
f83a0 20 20 7d 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 65    }.  }..  wince
f83b0 4d 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 69  MutexRelease(pFi
f83c0 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 72  le->hMutex);.  r
f83d0 65 74 75 72 6e 20 62 52 65 74 75 72 6e 3b 0a 7d  eturn bReturn;.}
f83e0 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65  ../*.** An imple
f83f0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
f8400 20 55 6e 6c 6f 63 6b 46 69 6c 65 20 41 50 49 20   UnlockFile API 
f8410 6f 66 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 77  of windows for w
f8420 69 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42  ince.*/.static B
f8430 4f 4f 4c 20 77 69 6e 63 65 55 6e 6c 6f 63 6b 46  OOL winceUnlockF
f8440 69 6c 65 28 0a 20 20 48 41 4e 44 4c 45 20 2a 70  ile(.  HANDLE *p
f8450 68 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64  hFile,.  DWORD d
f8460 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 2c 0a  wFileOffsetLow,.
f8470 20 20 44 57 4f 52 44 20 64 77 46 69 6c 65 4f 66    DWORD dwFileOf
f8480 66 73 65 74 48 69 67 68 2c 0a 20 20 44 57 4f 52  fsetHigh,.  DWOR
f8490 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73  D nNumberOfBytes
f84a0 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 2c 0a 20 20 44  ToUnlockLow,.  D
f84b0 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79  WORD nNumberOfBy
f84c0 74 65 73 54 6f 55 6e 6c 6f 63 6b 48 69 67 68 0a  tesToUnlockHigh.
f84d0 29 7b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46  ){.  winFile *pF
f84e0 69 6c 65 20 3d 20 48 41 4e 44 4c 45 5f 54 4f 5f  ile = HANDLE_TO_
f84f0 57 49 4e 46 49 4c 45 28 70 68 46 69 6c 65 29 3b  WINFILE(phFile);
f8500 0a 20 20 42 4f 4f 4c 20 62 52 65 74 75 72 6e 20  .  BOOL bReturn 
f8510 3d 20 46 41 4c 53 45 3b 0a 0a 20 20 55 4e 55 53  = FALSE;..  UNUS
f8520 45 44 5f 50 41 52 41 4d 45 54 45 52 28 64 77 46  ED_PARAMETER(dwF
f8530 69 6c 65 4f 66 66 73 65 74 48 69 67 68 29 3b 0a  ileOffsetHigh);.
f8540 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
f8550 45 52 28 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65  ER(nNumberOfByte
f8560 73 54 6f 55 6e 6c 6f 63 6b 48 69 67 68 29 3b 0a  sToUnlockHigh);.
f8570 0a 20 20 69 66 20 28 21 70 46 69 6c 65 2d 3e 68  .  if (!pFile->h
f8580 4d 75 74 65 78 29 20 72 65 74 75 72 6e 20 54 52  Mutex) return TR
f8590 55 45 3b 0a 20 20 77 69 6e 63 65 4d 75 74 65 78  UE;.  winceMutex
f85a0 41 63 71 75 69 72 65 28 70 46 69 6c 65 2d 3e 68  Acquire(pFile->h
f85b0 4d 75 74 65 78 29 3b 0a 0a 20 20 2f 2a 20 52 65  Mutex);..  /* Re
f85c0 6c 65 61 73 69 6e 67 20 61 20 72 65 61 64 65 72  leasing a reader
f85d0 20 6c 6f 63 6b 20 6f 72 20 61 6e 20 65 78 63 6c   lock or an excl
f85e0 75 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20  usive lock */.  
f85f0 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74  if (dwFileOffset
f8600 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 53 48  Low == (DWORD)SH
f8610 41 52 45 44 5f 46 49 52 53 54 29 7b 0a 20 20 20  ARED_FIRST){.   
f8620 20 2f 2a 20 44 69 64 20 77 65 20 68 61 76 65 20   /* Did we have 
f8630 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63  an exclusive loc
f8640 6b 3f 20 2a 2f 0a 20 20 20 20 69 66 20 28 70 46  k? */.    if (pF
f8650 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78 63 6c  ile->local.bExcl
f8660 75 73 69 76 65 29 7b 0a 20 20 20 20 20 20 61 73  usive){.      as
f8670 73 65 72 74 28 6e 4e 75 6d 62 65 72 4f 66 42 79  sert(nNumberOfBy
f8680 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d  tesToUnlockLow =
f8690 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 44 5f  = (DWORD)SHARED_
f86a0 53 49 5a 45 29 3b 0a 20 20 20 20 20 20 70 46 69  SIZE);.      pFi
f86b0 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78 63 6c 75  le->local.bExclu
f86c0 73 69 76 65 20 3d 20 46 41 4c 53 45 3b 0a 20 20  sive = FALSE;.  
f86d0 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65      pFile->share
f86e0 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 20  d->bExclusive = 
f86f0 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 62 52 65  FALSE;.      bRe
f8700 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20  turn = TRUE;.   
f8710 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 69 64 20 77   }..    /* Did w
f8720 65 20 6a 75 73 74 20 68 61 76 65 20 61 20 72 65  e just have a re
f8730 61 64 65 72 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20  ader lock? */.  
f8740 20 20 65 6c 73 65 20 69 66 20 28 70 46 69 6c 65    else if (pFile
f8750 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73  ->local.nReaders
f8760 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
f8770 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f  nNumberOfBytesTo
f8780 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 28 44 57  UnlockLow == (DW
f8790 4f 52 44 29 53 48 41 52 45 44 5f 53 49 5a 45 20  ORD)SHARED_SIZE 
f87a0 7c 7c 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65  || nNumberOfByte
f87b0 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20  sToUnlockLow == 
f87c0 31 29 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d  1);.      pFile-
f87d0 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 20  >local.nReaders 
f87e0 2d 2d 3b 0a 20 20 20 20 20 20 69 66 20 28 70 46  --;.      if (pF
f87f0 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64  ile->local.nRead
f8800 65 72 73 20 3d 3d 20 30 29 0a 20 20 20 20 20 20  ers == 0).      
f8810 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d  {.        pFile-
f8820 3e 73 68 61 72 65 64 2d 3e 6e 52 65 61 64 65 72  >shared->nReader
f8830 73 20 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a 20 20  s --;.      }.  
f8840 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52      bReturn = TR
f8850 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  UE;.    }.  }.. 
f8860 20 2f 2a 20 52 65 6c 65 61 73 69 6e 67 20 61 20   /* Releasing a 
f8870 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 2a 2f 0a  pending lock */.
f8880 20 20 65 6c 73 65 20 69 66 20 28 64 77 46 69 6c    else if (dwFil
f8890 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44  eOffsetLow == (D
f88a0 57 4f 52 44 29 50 45 4e 44 49 4e 47 5f 42 59 54  WORD)PENDING_BYT
f88b0 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79  E && nNumberOfBy
f88c0 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d  tesToUnlockLow =
f88d0 3d 20 31 29 7b 0a 20 20 20 20 69 66 20 28 70 46  = 1){.    if (pF
f88e0 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64  ile->local.bPend
f88f0 69 6e 67 29 7b 0a 20 20 20 20 20 20 70 46 69 6c  ing){.      pFil
f8900 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e  e->local.bPendin
f8910 67 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 20  g = FALSE;.     
f8920 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e   pFile->shared->
f8930 62 50 65 6e 64 69 6e 67 20 3d 20 46 41 4c 53 45  bPending = FALSE
f8940 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e 20  ;.      bReturn 
f8950 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20  = TRUE;.    }.  
f8960 7d 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69 6e 67  }.  /* Releasing
f8970 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b   a reserved lock
f8980 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64   */.  else if (d
f8990 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d  wFileOffsetLow =
f89a0 3d 20 28 44 57 4f 52 44 29 52 45 53 45 52 56 45  = (DWORD)RESERVE
f89b0 44 5f 42 59 54 45 20 26 26 20 6e 4e 75 6d 62 65  D_BYTE && nNumbe
f89c0 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b  rOfBytesToUnlock
f89d0 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 69  Low == 1){.    i
f89e0 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e  f (pFile->local.
f89f0 62 52 65 73 65 72 76 65 64 29 20 7b 0a 20 20 20  bReserved) {.   
f8a00 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e     pFile->local.
f8a10 62 52 65 73 65 72 76 65 64 20 3d 20 46 41 4c 53  bReserved = FALS
f8a20 45 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  E;.      pFile->
f8a30 73 68 61 72 65 64 2d 3e 62 52 65 73 65 72 76 65  shared->bReserve
f8a40 64 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 20  d = FALSE;.     
f8a50 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b   bReturn = TRUE;
f8a60 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 77 69  .    }.  }..  wi
f8a70 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28  nceMutexRelease(
f8a80 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a  pFile->hMutex);.
f8a90 20 20 72 65 74 75 72 6e 20 62 52 65 74 75 72 6e    return bReturn
f8aa0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6d  ;.}../*.** An im
f8ab0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
f8ac0 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29  the LockFileEx()
f8ad0 20 41 50 49 20 6f 66 20 77 69 6e 64 6f 77 73 20   API of windows 
f8ae0 66 6f 72 20 77 69 6e 63 65 0a 2a 2f 0a 73 74 61  for wince.*/.sta
f8af0 74 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65 4c 6f  tic BOOL winceLo
f8b00 63 6b 46 69 6c 65 45 78 28 0a 20 20 48 41 4e 44  ckFileEx(.  HAND
f8b10 4c 45 20 2a 70 68 46 69 6c 65 2c 0a 20 20 44 57  LE *phFile,.  DW
f8b20 4f 52 44 20 64 77 46 6c 61 67 73 2c 0a 20 20 44  ORD dwFlags,.  D
f8b30 57 4f 52 44 20 64 77 52 65 73 65 72 76 65 64 2c  WORD dwReserved,
f8b40 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72  .  DWORD nNumber
f8b50 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77  OfBytesToLockLow
f8b60 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65  ,.  DWORD nNumbe
f8b70 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 48 69  rOfBytesToLockHi
f8b80 67 68 2c 0a 20 20 4c 50 4f 56 45 52 4c 41 50 50  gh,.  LPOVERLAPP
f8b90 45 44 20 6c 70 4f 76 65 72 6c 61 70 70 65 64 0a  ED lpOverlapped.
f8ba0 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ){.  UNUSED_PARA
f8bb0 4d 45 54 45 52 28 64 77 52 65 73 65 72 76 65 64  METER(dwReserved
f8bc0 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
f8bd0 4d 45 54 45 52 28 6e 4e 75 6d 62 65 72 4f 66 42  METER(nNumberOfB
f8be0 79 74 65 73 54 6f 4c 6f 63 6b 48 69 67 68 29 3b  ytesToLockHigh);
f8bf0 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 61  ..  /* If the ca
f8c00 6c 6c 65 72 20 77 61 6e 74 73 20 61 20 73 68 61  ller wants a sha
f8c10 72 65 64 20 72 65 61 64 20 6c 6f 63 6b 2c 20 66  red read lock, f
f8c20 6f 72 77 61 72 64 20 74 68 69 73 20 63 61 6c 6c  orward this call
f8c30 0a 20 20 2a 2a 20 74 6f 20 77 69 6e 63 65 4c 6f  .  ** to winceLo
f8c40 63 6b 46 69 6c 65 20 2a 2f 0a 20 20 69 66 20 28  ckFile */.  if (
f8c50 6c 70 4f 76 65 72 6c 61 70 70 65 64 2d 3e 4f 66  lpOverlapped->Of
f8c60 66 73 65 74 20 3d 3d 20 28 44 57 4f 52 44 29 53  fset == (DWORD)S
f8c70 48 41 52 45 44 5f 46 49 52 53 54 20 26 26 0a 20  HARED_FIRST &&. 
f8c80 20 20 20 20 20 64 77 46 6c 61 67 73 20 3d 3d 20       dwFlags == 
f8c90 31 20 26 26 0a 20 20 20 20 20 20 6e 4e 75 6d 62  1 &&.      nNumb
f8ca0 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c  erOfBytesToLockL
f8cb0 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 53 48 41  ow == (DWORD)SHA
f8cc0 52 45 44 5f 53 49 5a 45 29 7b 0a 20 20 20 20 72  RED_SIZE){.    r
f8cd0 65 74 75 72 6e 20 77 69 6e 63 65 4c 6f 63 6b 46  eturn winceLockF
f8ce0 69 6c 65 28 70 68 46 69 6c 65 2c 20 53 48 41 52  ile(phFile, SHAR
f8cf0 45 44 5f 46 49 52 53 54 2c 20 30 2c 20 31 2c 20  ED_FIRST, 0, 1, 
f8d00 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  0);.  }.  return
f8d10 20 46 41 4c 53 45 3b 0a 7d 0a 2f 2a 0a 2a 2a 20   FALSE;.}./*.** 
f8d20 45 6e 64 20 6f 66 20 74 68 65 20 73 70 65 63 69  End of the speci
f8d30 61 6c 20 63 6f 64 65 20 66 6f 72 20 77 69 6e 63  al code for winc
f8d40 65 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.**************
f8d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f8d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f8d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f8d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
f8d90 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
f8da0 45 5f 4f 53 5f 57 49 4e 43 45 20 2a 2f 0a 0a 2f  E_OS_WINCE */../
f8db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f8dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f8dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f8de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f8df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
f8e00 20 54 68 65 20 6e 65 78 74 20 67 72 6f 75 70 20   The next group 
f8e10 6f 66 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c  of routines impl
f8e20 65 6d 65 6e 74 20 74 68 65 20 49 2f 4f 20 6d 65  ement the I/O me
f8e30 74 68 6f 64 73 20 73 70 65 63 69 66 69 65 64 0a  thods specified.
f8e40 2a 2a 20 62 79 20 74 68 65 20 73 71 6c 69 74 65  ** by the sqlite
f8e50 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a  3_io_methods obj
f8e60 65 63 74 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ect..***********
f8e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f8e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f8e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f8ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f8eb0 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73  ***/../*.** Clos
f8ec0 65 20 61 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  e a file..**.** 
f8ed0 49 74 20 69 73 20 72 65 70 6f 72 74 65 64 20 74  It is reported t
f8ee0 68 61 74 20 61 6e 20 61 74 74 65 6d 70 74 20 74  hat an attempt t
f8ef0 6f 20 63 6c 6f 73 65 20 61 20 68 61 6e 64 6c 65  o close a handle
f8f00 20 6d 69 67 68 74 20 73 6f 6d 65 74 69 6d 65 73   might sometimes
f8f10 0a 2a 2a 20 66 61 69 6c 2e 20 20 54 68 69 73 20  .** fail.  This 
f8f20 69 73 20 61 20 76 65 72 79 20 75 6e 72 65 61 73  is a very unreas
f8f30 6f 6e 61 62 6c 65 20 72 65 73 75 6c 74 2c 20 62  onable result, b
f8f40 75 74 20 77 69 6e 64 6f 77 73 20 69 73 20 6e 6f  ut windows is no
f8f50 74 6f 72 69 6f 75 73 0a 2a 2a 20 66 6f 72 20 62  torious.** for b
f8f60 65 69 6e 67 20 75 6e 72 65 61 73 6f 6e 61 62 6c  eing unreasonabl
f8f70 65 20 73 6f 20 49 20 64 6f 20 6e 6f 74 20 64 6f  e so I do not do
f8f80 75 62 74 20 74 68 61 74 20 69 74 20 6d 69 67 68  ubt that it migh
f8f90 74 20 68 61 70 70 65 6e 2e 20 20 49 66 0a 2a 2a  t happen.  If.**
f8fa0 20 74 68 65 20 63 6c 6f 73 65 20 66 61 69 6c 73   the close fails
f8fb0 2c 20 77 65 20 70 61 75 73 65 20 66 6f 72 20 31  , we pause for 1
f8fc0 30 30 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20  00 milliseconds 
f8fd0 61 6e 64 20 74 72 79 20 61 67 61 69 6e 2e 20 20  and try again.  
f8fe0 41 73 0a 2a 2a 20 6d 61 6e 79 20 61 73 20 4d 58  As.** many as MX
f8ff0 5f 43 4c 4f 53 45 5f 41 54 54 45 4d 50 54 20 61  _CLOSE_ATTEMPT a
f9000 74 74 65 6d 70 74 73 20 74 6f 20 63 6c 6f 73 65  ttempts to close
f9010 20 74 68 65 20 68 61 6e 64 6c 65 20 61 72 65 20   the handle are 
f9020 6d 61 64 65 20 62 65 66 6f 72 65 0a 2a 2a 20 67  made before.** g
f9030 69 76 69 6e 67 20 75 70 20 61 6e 64 20 72 65 74  iving up and ret
f9040 75 72 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 2e  urning an error.
f9050 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f 43  .*/.#define MX_C
f9060 4c 4f 53 45 5f 41 54 54 45 4d 50 54 20 33 0a 73  LOSE_ATTEMPT 3.s
f9070 74 61 74 69 63 20 69 6e 74 20 77 69 6e 43 6c 6f  tatic int winClo
f9080 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  se(sqlite3_file 
f9090 2a 69 64 29 7b 0a 20 20 69 6e 74 20 72 63 2c 20  *id){.  int rc, 
f90a0 63 6e 74 20 3d 20 30 3b 0a 20 20 77 69 6e 46 69  cnt = 0;.  winFi
f90b0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e  le *pFile = (win
f90c0 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73  File*)id;..  ass
f90d0 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20  ert( id!=0 );.  
f90e0 4f 53 54 52 41 43 45 32 28 22 43 4c 4f 53 45 20  OSTRACE2("CLOSE 
f90f0 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 29  %d\n", pFile->h)
f9100 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 72 63 20 3d  ;.  do{.    rc =
f9110 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69   CloseHandle(pFi
f9120 6c 65 2d 3e 68 29 3b 0a 20 20 7d 77 68 69 6c 65  le->h);.  }while
f9130 28 20 72 63 3d 3d 30 20 26 26 20 2b 2b 63 6e 74  ( rc==0 && ++cnt
f9140 20 3c 20 4d 58 5f 43 4c 4f 53 45 5f 41 54 54 45   < MX_CLOSE_ATTE
f9150 4d 50 54 20 26 26 20 28 53 6c 65 65 70 28 31 30  MPT && (Sleep(10
f9160 30 29 2c 20 31 29 20 29 3b 0a 23 69 66 20 53 51  0), 1) );.#if SQ
f9170 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 23 64  LITE_OS_WINCE.#d
f9180 65 66 69 6e 65 20 57 49 4e 43 45 5f 44 45 4c 45  efine WINCE_DELE
f9190 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 20 33 0a  TION_ATTEMPTS 3.
f91a0 20 20 77 69 6e 63 65 44 65 73 74 72 6f 79 4c 6f    winceDestroyLo
f91b0 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 28  ck(pFile);.  if(
f91c0 20 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f   pFile->zDeleteO
f91d0 6e 43 6c 6f 73 65 20 29 7b 0a 20 20 20 20 69 6e  nClose ){.    in
f91e0 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 77  t cnt = 0;.    w
f91f0 68 69 6c 65 28 0a 20 20 20 20 20 20 20 20 20 20  hile(.          
f9200 20 44 65 6c 65 74 65 46 69 6c 65 57 28 70 46 69   DeleteFileW(pFi
f9210 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f  le->zDeleteOnClo
f9220 73 65 29 3d 3d 30 0a 20 20 20 20 20 20 20 20 26  se)==0.        &
f9230 26 20 47 65 74 46 69 6c 65 41 74 74 72 69 62 75  & GetFileAttribu
f9240 74 65 73 57 28 70 46 69 6c 65 2d 3e 7a 44 65 6c  tesW(pFile->zDel
f9250 65 74 65 4f 6e 43 6c 6f 73 65 29 21 3d 30 78 66  eteOnClose)!=0xf
f9260 66 66 66 66 66 66 66 20 0a 20 20 20 20 20 20 20  fffffff .       
f9270 20 26 26 20 63 6e 74 2b 2b 20 3c 20 57 49 4e 43   && cnt++ < WINC
f9280 45 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d  E_DELETION_ATTEM
f9290 50 54 53 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  PTS.    ){.     
f92a0 20 20 53 6c 65 65 70 28 31 30 30 29 3b 20 20 2f    Sleep(100);  /
f92b0 2a 20 57 61 69 74 20 61 20 6c 69 74 74 6c 65 20  * Wait a little 
f92c0 62 65 66 6f 72 65 20 74 72 79 69 6e 67 20 61 67  before trying ag
f92d0 61 69 6e 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20  ain */.    }.   
f92e0 20 66 72 65 65 28 70 46 69 6c 65 2d 3e 7a 44 65   free(pFile->zDe
f92f0 6c 65 74 65 4f 6e 43 6c 6f 73 65 29 3b 0a 20 20  leteOnClose);.  
f9300 7d 0a 23 65 6e 64 69 66 0a 20 20 4f 70 65 6e 43  }.#endif.  OpenC
f9310 6f 75 6e 74 65 72 28 2d 31 29 3b 0a 20 20 72 65  ounter(-1);.  re
f9320 74 75 72 6e 20 72 63 20 3f 20 53 51 4c 49 54 45  turn rc ? SQLITE
f9330 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45  _OK : SQLITE_IOE
f9340 52 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d  RR;.}../*.** Som
f9350 65 20 6d 69 63 72 6f 73 6f 66 74 20 63 6f 6d 70  e microsoft comp
f9360 69 6c 65 72 73 20 6c 61 63 6b 20 74 68 69 73 20  ilers lack this 
f9370 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 23  definition..*/.#
f9380 69 66 6e 64 65 66 20 49 4e 56 41 4c 49 44 5f 53  ifndef INVALID_S
f9390 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 0a  ET_FILE_POINTER.
f93a0 23 20 64 65 66 69 6e 65 20 49 4e 56 41 4c 49 44  # define INVALID
f93b0 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45  _SET_FILE_POINTE
f93c0 52 20 28 28 44 57 4f 52 44 29 2d 31 29 0a 23 65  R ((DWORD)-1).#e
f93d0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64  ndif../*.** Read
f93e0 20 64 61 74 61 20 66 72 6f 6d 20 61 20 66 69 6c   data from a fil
f93f0 65 20 69 6e 74 6f 20 61 20 62 75 66 66 65 72 2e  e into a buffer.
f9400 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    Return SQLITE_
f9410 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74  OK if all.** byt
f9420 65 73 20 77 65 72 65 20 72 65 61 64 20 73 75 63  es were read suc
f9430 63 65 73 73 66 75 6c 6c 79 20 61 6e 64 20 53 51  cessfully and SQ
f9440 4c 49 54 45 5f 49 4f 45 52 52 20 69 66 20 61 6e  LITE_IOERR if an
f9450 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77  ything goes.** w
f9460 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rong..*/.static 
f9470 69 6e 74 20 77 69 6e 52 65 61 64 28 0a 20 20 73  int winRead(.  s
f9480 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
f9490 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c            /* Fil
f94a0 65 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a  e to read from *
f94b0 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 66 2c 20  /.  void *pBuf, 
f94c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
f94d0 2a 20 57 72 69 74 65 20 63 6f 6e 74 65 6e 74 20  * Write content 
f94e0 69 6e 74 6f 20 74 68 69 73 20 62 75 66 66 65 72  into this buffer
f94f0 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20   */.  int amt,  
f9500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f9510 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
f9520 74 65 73 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20  tes to read */. 
f9530 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f   sqlite3_int64 o
f9540 66 66 73 65 74 20 20 20 20 20 20 20 2f 2a 20 42  ffset       /* B
f9550 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61 74 20  egin reading at 
f9560 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f 0a 29  this offset */.)
f9570 7b 0a 20 20 4c 4f 4e 47 20 75 70 70 65 72 42 69  {.  LONG upperBi
f9580 74 73 20 3d 20 28 4c 4f 4e 47 29 28 28 6f 66 66  ts = (LONG)((off
f9590 73 65 74 3e 3e 33 32 29 20 26 20 30 78 37 66 66  set>>32) & 0x7ff
f95a0 66 66 66 66 66 29 3b 0a 20 20 4c 4f 4e 47 20 6c  fffff);.  LONG l
f95b0 6f 77 65 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47  owerBits = (LONG
f95c0 29 28 6f 66 66 73 65 74 20 26 20 30 78 66 66 66  )(offset & 0xfff
f95d0 66 66 66 66 66 29 3b 0a 20 20 44 57 4f 52 44 20  fffff);.  DWORD 
f95e0 72 63 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70  rc;.  winFile *p
f95f0 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a  File = (winFile*
f9600 29 69 64 3b 0a 20 20 44 57 4f 52 44 20 65 72 72  )id;.  DWORD err
f9610 6f 72 3b 0a 20 20 44 57 4f 52 44 20 67 6f 74 3b  or;.  DWORD got;
f9620 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d  ..  assert( id!=
f9630 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49  0 );.  SimulateI
f9640 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51  OError(return SQ
f9650 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 29  LITE_IOERR_READ)
f9660 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 22 52 45  ;.  OSTRACE3("RE
f9670 41 44 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22  AD %d lock=%d\n"
f9680 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c  , pFile->h, pFil
f9690 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20  e->locktype);.  
f96a0 72 63 20 3d 20 53 65 74 46 69 6c 65 50 6f 69 6e  rc = SetFilePoin
f96b0 74 65 72 28 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f  ter(pFile->h, lo
f96c0 77 65 72 42 69 74 73 2c 20 26 75 70 70 65 72 42  werBits, &upperB
f96d0 69 74 73 2c 20 46 49 4c 45 5f 42 45 47 49 4e 29  its, FILE_BEGIN)
f96e0 3b 0a 20 20 69 66 28 20 72 63 3d 3d 49 4e 56 41  ;.  if( rc==INVA
f96f0 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49  LID_SET_FILE_POI
f9700 4e 54 45 52 20 26 26 20 28 65 72 72 6f 72 3d 47  NTER && (error=G
f9710 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 21 3d  etLastError())!=
f9720 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20  NO_ERROR ){.    
f9730 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
f9740 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20 20 72 65   = error;.    re
f9750 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c  turn SQLITE_FULL
f9760 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 52 65 61  ;.  }.  if( !Rea
f9770 64 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20  dFile(pFile->h, 
f9780 70 42 75 66 2c 20 61 6d 74 2c 20 26 67 6f 74 2c  pBuf, amt, &got,
f9790 20 30 29 20 29 7b 0a 20 20 20 20 70 46 69 6c 65   0) ){.    pFile
f97a0 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 65  ->lastErrno = Ge
f97b0 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20  tLastError();.  
f97c0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
f97d0 49 4f 45 52 52 5f 52 45 41 44 3b 0a 20 20 7d 0a  IOERR_READ;.  }.
f97e0 20 20 69 66 28 20 67 6f 74 3d 3d 28 44 57 4f 52    if( got==(DWOR
f97f0 44 29 61 6d 74 20 29 7b 0a 20 20 20 20 72 65 74  D)amt ){.    ret
f9800 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
f9810 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 55   }else{.    /* U
f9820 6e 72 65 61 64 20 70 61 72 74 73 20 6f 66 20 74  nread parts of t
f9830 68 65 20 62 75 66 66 65 72 20 6d 75 73 74 20 62  he buffer must b
f9840 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f  e zero-filled */
f9850 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63  .    memset(&((c
f9860 68 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c  har*)pBuf)[got],
f9870 20 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20   0, amt-got);.  
f9880 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
f9890 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44  IOERR_SHORT_READ
f98a0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57  ;.  }.}../*.** W
f98b0 72 69 74 65 20 64 61 74 61 20 66 72 6f 6d 20 61  rite data from a
f98c0 20 62 75 66 66 65 72 20 69 6e 74 6f 20 61 20 66   buffer into a f
f98d0 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 51 4c  ile.  Return SQL
f98e0 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73  ITE_OK on succes
f98f0 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 68  s.** or some oth
f9900 65 72 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 6e  er error code on
f9910 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61   failure..*/.sta
f9920 74 69 63 20 69 6e 74 20 77 69 6e 57 72 69 74 65  tic int winWrite
f9930 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  (.  sqlite3_file
f9940 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 2f 2a   *id,         /*
f9950 20 46 69 6c 65 20 74 6f 20 77 72 69 74 65 20 69   File to write i
f9960 6e 74 6f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  nto */.  const v
f9970 6f 69 64 20 2a 70 42 75 66 2c 20 20 20 20 20 20  oid *pBuf,      
f9980 20 20 20 2f 2a 20 54 68 65 20 62 79 74 65 73 20     /* The bytes 
f9990 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 2a 2f  to be written */
f99a0 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20  .  int amt,     
f99b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
f99c0 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
f99d0 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 73 71  to write */.  sq
f99e0 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73  lite3_int64 offs
f99f0 65 74 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65  et      /* Offse
f9a00 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 20  t into the file 
f9a10 74 6f 20 62 65 67 69 6e 20 77 72 69 74 69 6e 67  to begin writing
f9a20 20 61 74 20 2a 2f 0a 29 7b 0a 20 20 4c 4f 4e 47   at */.){.  LONG
f9a30 20 75 70 70 65 72 42 69 74 73 20 3d 20 28 4c 4f   upperBits = (LO
f9a40 4e 47 29 28 28 6f 66 66 73 65 74 3e 3e 33 32 29  NG)((offset>>32)
f9a50 20 26 20 30 78 37 66 66 66 66 66 66 66 29 3b 0a   & 0x7fffffff);.
f9a60 20 20 4c 4f 4e 47 20 6c 6f 77 65 72 42 69 74 73    LONG lowerBits
f9a70 20 3d 20 28 4c 4f 4e 47 29 28 6f 66 66 73 65 74   = (LONG)(offset
f9a80 20 26 20 30 78 66 66 66 66 66 66 66 66 29 3b 0a   & 0xffffffff);.
f9a90 20 20 44 57 4f 52 44 20 72 63 3b 0a 20 20 77 69    DWORD rc;.  wi
f9aa0 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  nFile *pFile = (
f9ab0 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44  winFile*)id;.  D
f9ac0 57 4f 52 44 20 65 72 72 6f 72 3b 0a 20 20 44 57  WORD error;.  DW
f9ad0 4f 52 44 20 77 72 6f 74 65 20 3d 20 30 3b 0a 0a  ORD wrote = 0;..
f9ae0 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20    assert( id!=0 
f9af0 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  );.  SimulateIOE
f9b00 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49  rror(return SQLI
f9b10 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 29 3b  TE_IOERR_WRITE);
f9b20 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66  .  SimulateDiskf
f9b30 75 6c 6c 45 72 72 6f 72 28 72 65 74 75 72 6e 20  ullError(return 
f9b40 53 51 4c 49 54 45 5f 46 55 4c 4c 29 3b 0a 20 20  SQLITE_FULL);.  
f9b50 4f 53 54 52 41 43 45 33 28 22 57 52 49 54 45 20  OSTRACE3("WRITE 
f9b60 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70  %d lock=%d\n", p
f9b70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e  File->h, pFile->
f9b80 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 72 63 20  locktype);.  rc 
f9b90 3d 20 53 65 74 46 69 6c 65 50 6f 69 6e 74 65 72  = SetFilePointer
f9ba0 28 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72  (pFile->h, lower
f9bb0 42 69 74 73 2c 20 26 75 70 70 65 72 42 69 74 73  Bits, &upperBits
f9bc0 2c 20 46 49 4c 45 5f 42 45 47 49 4e 29 3b 0a 20  , FILE_BEGIN);. 
f9bd0 20 69 66 28 20 72 63 3d 3d 49 4e 56 41 4c 49 44   if( rc==INVALID
f9be0 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45  _SET_FILE_POINTE
f9bf0 52 20 26 26 20 28 65 72 72 6f 72 3d 47 65 74 4c  R && (error=GetL
f9c00 61 73 74 45 72 72 6f 72 28 29 29 21 3d 4e 4f 5f  astError())!=NO_
f9c10 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 70 46 69  ERROR ){.    pFi
f9c20 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
f9c30 65 72 72 6f 72 3b 0a 20 20 20 20 72 65 74 75 72  error;.    retur
f9c40 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20  n SQLITE_FULL;. 
f9c50 20 7d 0a 20 20 61 73 73 65 72 74 28 20 61 6d 74   }.  assert( amt
f9c60 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 28 0a 20  >0 );.  while(. 
f9c70 20 20 20 20 61 6d 74 3e 30 0a 20 20 20 20 20 26      amt>0.     &
f9c80 26 20 28 72 63 20 3d 20 57 72 69 74 65 46 69 6c  & (rc = WriteFil
f9c90 65 28 70 46 69 6c 65 2d 3e 68 2c 20 70 42 75 66  e(pFile->h, pBuf
f9ca0 2c 20 61 6d 74 2c 20 26 77 72 6f 74 65 2c 20 30  , amt, &wrote, 0
f9cb0 29 29 21 3d 30 0a 20 20 20 20 20 26 26 20 77 72  ))!=0.     && wr
f9cc0 6f 74 65 3e 30 0a 20 20 29 7b 0a 20 20 20 20 61  ote>0.  ){.    a
f9cd0 6d 74 20 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 20  mt -= wrote;.   
f9ce0 20 70 42 75 66 20 3d 20 26 28 28 63 68 61 72 2a   pBuf = &((char*
f9cf0 29 70 42 75 66 29 5b 77 72 6f 74 65 5d 3b 0a 20  )pBuf)[wrote];. 
f9d00 20 7d 0a 20 20 69 66 28 20 21 72 63 20 7c 7c 20   }.  if( !rc || 
f9d10 61 6d 74 3e 28 69 6e 74 29 77 72 6f 74 65 20 29  amt>(int)wrote )
f9d20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73  {.    pFile->las
f9d30 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74  tErrno = GetLast
f9d40 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 72 65 74  Error();.    ret
f9d50 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b  urn SQLITE_FULL;
f9d60 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
f9d70 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
f9d80 2a 20 54 72 75 6e 63 61 74 65 20 61 6e 20 6f 70  * Truncate an op
f9d90 65 6e 20 66 69 6c 65 20 74 6f 20 61 20 73 70 65  en file to a spe
f9da0 63 69 66 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73  cified size.*/.s
f9db0 74 61 74 69 63 20 69 6e 74 20 77 69 6e 54 72 75  tatic int winTru
f9dc0 6e 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69  ncate(sqlite3_fi
f9dd0 6c 65 20 2a 69 64 2c 20 73 71 6c 69 74 65 33 5f  le *id, sqlite3_
f9de0 69 6e 74 36 34 20 6e 42 79 74 65 29 7b 0a 20 20  int64 nByte){.  
f9df0 4c 4f 4e 47 20 75 70 70 65 72 42 69 74 73 20 3d  LONG upperBits =
f9e00 20 28 4c 4f 4e 47 29 28 28 6e 42 79 74 65 3e 3e   (LONG)((nByte>>
f9e10 33 32 29 20 26 20 30 78 37 66 66 66 66 66 66 66  32) & 0x7fffffff
f9e20 29 3b 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65 72 42  );.  LONG lowerB
f9e30 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 6e 42 79  its = (LONG)(nBy
f9e40 74 65 20 26 20 30 78 66 66 66 66 66 66 66 66 29  te & 0xffffffff)
f9e50 3b 0a 20 20 44 57 4f 52 44 20 72 63 3b 0a 20 20  ;.  DWORD rc;.  
f9e60 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  winFile *pFile =
f9e70 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20   (winFile*)id;. 
f9e80 20 44 57 4f 52 44 20 65 72 72 6f 72 3b 0a 0a 20   DWORD error;.. 
f9e90 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29   assert( id!=0 )
f9ea0 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 22 54 52  ;.  OSTRACE3("TR
f9eb0 55 4e 43 41 54 45 20 25 64 20 25 6c 6c 64 5c 6e  UNCATE %d %lld\n
f9ec0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6e 42 79  ", pFile->h, nBy
f9ed0 74 65 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49  te);.  SimulateI
f9ee0 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51  OError(return SQ
f9ef0 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43  LITE_IOERR_TRUNC
f9f00 41 54 45 29 3b 0a 20 20 72 63 20 3d 20 53 65 74  ATE);.  rc = Set
f9f10 46 69 6c 65 50 6f 69 6e 74 65 72 28 70 46 69 6c  FilePointer(pFil
f9f20 65 2d 3e 68 2c 20 6c 6f 77 65 72 42 69 74 73 2c  e->h, lowerBits,
f9f30 20 26 75 70 70 65 72 42 69 74 73 2c 20 46 49 4c   &upperBits, FIL
f9f40 45 5f 42 45 47 49 4e 29 3b 0a 20 20 69 66 28 20  E_BEGIN);.  if( 
f9f50 72 63 3d 3d 49 4e 56 41 4c 49 44 5f 53 45 54 5f  rc==INVALID_SET_
f9f60 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 26 26 20  FILE_POINTER && 
f9f70 28 65 72 72 6f 72 3d 47 65 74 4c 61 73 74 45 72  (error=GetLastEr
f9f80 72 6f 72 28 29 29 21 3d 4e 4f 5f 45 52 52 4f 52  ror())!=NO_ERROR
f9f90 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c   ){.    pFile->l
f9fa0 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6f 72  astErrno = error
f9fb0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
f9fc0 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41  ITE_IOERR_TRUNCA
f9fd0 54 45 3b 0a 20 20 7d 0a 20 20 2f 2a 20 53 65 74  TE;.  }.  /* Set
f9fe0 45 6e 64 4f 66 46 69 6c 65 20 77 69 6c 6c 20 66  EndOfFile will f
f9ff0 61 69 6c 20 69 66 20 6e 42 79 74 65 20 69 73 20  ail if nByte is 
fa000 6e 65 67 61 74 69 76 65 20 2a 2f 0a 20 20 69 66  negative */.  if
fa010 28 20 21 53 65 74 45 6e 64 4f 66 46 69 6c 65 28  ( !SetEndOfFile(
fa020 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a 20 20 20  pFile->h) ){.   
fa030 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
fa040 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72  o = GetLastError
fa050 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  ();.    return S
fa060 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e  QLITE_IOERR_TRUN
fa070 43 41 54 45 3b 0a 20 20 7d 0a 20 20 72 65 74 75  CATE;.  }.  retu
fa080 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
fa090 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
fa0a0 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20  EST./*.** Count 
fa0b0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 75  the number of fu
fa0c0 6c 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d  llsyncs and norm
fa0d0 61 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 73 20  al syncs.  This 
fa0e0 69 73 20 75 73 65 64 20 74 6f 20 74 65 73 74 0a  is used to test.
fa0f0 2a 2a 20 74 68 61 74 20 73 79 6e 63 73 20 61 6e  ** that syncs an
fa100 64 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 20  d fullsyncs are 
fa110 6f 63 63 75 72 69 6e 67 20 61 74 20 74 68 65 20  occuring at the 
fa120 72 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a  right times..*/.
fa130 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
fa140 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e  qlite3_sync_coun
fa150 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50  t = 0;.SQLITE_AP
fa160 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 75  I int sqlite3_fu
fa170 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30  llsync_count = 0
fa180 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ;.#endif../*.** 
fa190 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 77 72  Make sure all wr
fa1a0 69 74 65 73 20 74 6f 20 61 20 70 61 72 74 69 63  ites to a partic
fa1b0 75 6c 61 72 20 66 69 6c 65 20 61 72 65 20 63 6f  ular file are co
fa1c0 6d 6d 69 74 74 65 64 20 74 6f 20 64 69 73 6b 2e  mmitted to disk.
fa1d0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
fa1e0 69 6e 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66  inSync(sqlite3_f
fa1f0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61  ile *id, int fla
fa200 67 73 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c  gs){.#ifndef SQL
fa210 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 77 69  ITE_NO_SYNC.  wi
fa220 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  nFile *pFile = (
fa230 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20  winFile*)id;..  
fa240 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b  assert( id!=0 );
fa250 0a 20 20 4f 53 54 52 41 43 45 33 28 22 53 59 4e  .  OSTRACE3("SYN
fa260 43 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c  C %d lock=%d\n",
fa270 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65   pFile->h, pFile
fa280 2d 3e 6c 6f 63 6b 74 79 70 65 29 3b 0a 23 65 6c  ->locktype);.#el
fa290 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  se.  UNUSED_PARA
fa2a0 4d 45 54 45 52 28 69 64 29 3b 0a 23 65 6e 64 69  METER(id);.#endi
fa2b0 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  f.#ifndef SQLITE
fa2c0 5f 54 45 53 54 0a 20 20 55 4e 55 53 45 44 5f 50  _TEST.  UNUSED_P
fa2d0 41 52 41 4d 45 54 45 52 28 66 6c 61 67 73 29 3b  ARAMETER(flags);
fa2e0 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 66 6c 61  .#else.  if( fla
fa2f0 67 73 20 26 20 53 51 4c 49 54 45 5f 53 59 4e 43  gs & SQLITE_SYNC
fa300 5f 46 55 4c 4c 20 29 7b 0a 20 20 20 20 73 71 6c  _FULL ){.    sql
fa310 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f  ite3_fullsync_co
fa320 75 6e 74 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c  unt++;.  }.  sql
fa330 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b  ite3_sync_count+
fa340 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 2f 2a 20 49  +;.#endif.  /* I
fa350 66 20 77 65 20 63 6f 6d 70 69 6c 65 64 20 77 69  f we compiled wi
fa360 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f  th the SQLITE_NO
fa370 5f 53 59 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e  _SYNC flag, then
fa380 20 73 79 6e 63 69 6e 67 20 69 73 20 61 0a 20 20   syncing is a.  
fa390 2a 2a 20 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69  ** no-op.  */.#i
fa3a0 66 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53  fdef SQLITE_NO_S
fa3b0 59 4e 43 0a 20 20 20 20 72 65 74 75 72 6e 20 53  YNC.    return S
fa3c0 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6c 73 65 0a  QLITE_OK;.#else.
fa3d0 20 20 69 66 28 20 46 6c 75 73 68 46 69 6c 65 42    if( FlushFileB
fa3e0 75 66 66 65 72 73 28 70 46 69 6c 65 2d 3e 68 29  uffers(pFile->h)
fa3f0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
fa400 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73  QLITE_OK;.  }els
fa410 65 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61  e{.    pFile->la
fa420 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73  stErrno = GetLas
fa430 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 72 65  tError();.    re
fa440 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
fa450 52 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a  R;.  }.#endif.}.
fa460 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65  ./*.** Determine
fa470 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a   the current siz
fa480 65 20 6f 66 20 61 20 66 69 6c 65 20 69 6e 20 62  e of a file in b
fa490 79 74 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ytes.*/.static i
fa4a0 6e 74 20 77 69 6e 46 69 6c 65 53 69 7a 65 28 73  nt winFileSize(s
fa4b0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
fa4c0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a   sqlite3_int64 *
fa4d0 70 53 69 7a 65 29 7b 0a 20 20 44 57 4f 52 44 20  pSize){.  DWORD 
fa4e0 75 70 70 65 72 42 69 74 73 3b 0a 20 20 44 57 4f  upperBits;.  DWO
fa4f0 52 44 20 6c 6f 77 65 72 42 69 74 73 3b 0a 20 20  RD lowerBits;.  
fa500 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  winFile *pFile =
fa510 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20   (winFile*)id;. 
fa520 20 44 57 4f 52 44 20 65 72 72 6f 72 3b 0a 0a 20   DWORD error;.. 
fa530 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29   assert( id!=0 )
fa540 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ;.  SimulateIOEr
fa550 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54  ror(return SQLIT
fa560 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 29 3b 0a  E_IOERR_FSTAT);.
fa570 20 20 6c 6f 77 65 72 42 69 74 73 20 3d 20 47 65    lowerBits = Ge
fa580 74 46 69 6c 65 53 69 7a 65 28 70 46 69 6c 65 2d  tFileSize(pFile-
fa590 3e 68 2c 20 26 75 70 70 65 72 42 69 74 73 29 3b  >h, &upperBits);
fa5a0 0a 20 20 69 66 28 20 20 20 28 6c 6f 77 65 72 42  .  if(   (lowerB
fa5b0 69 74 73 20 3d 3d 20 49 4e 56 41 4c 49 44 5f 46  its == INVALID_F
fa5c0 49 4c 45 5f 53 49 5a 45 29 0a 20 20 20 20 20 26  ILE_SIZE).     &
fa5d0 26 20 28 28 65 72 72 6f 72 20 3d 20 47 65 74 4c  & ((error = GetL
fa5e0 61 73 74 45 72 72 6f 72 28 29 29 20 21 3d 20 4e  astError()) != N
fa5f0 4f 5f 45 52 52 4f 52 29 20 29 0a 20 20 7b 0a 20  O_ERROR) ).  {. 
fa600 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72     pFile->lastEr
fa610 72 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20  rno = error;.   
fa620 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
fa630 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20 20 7d 0a  OERR_FSTAT;.  }.
fa640 20 20 2a 70 53 69 7a 65 20 3d 20 28 28 28 73 71    *pSize = (((sq
fa650 6c 69 74 65 33 5f 69 6e 74 36 34 29 75 70 70 65  lite3_int64)uppe
fa660 72 42 69 74 73 29 3c 3c 33 32 29 20 2b 20 6c 6f  rBits)<<32) + lo
fa670 77 65 72 42 69 74 73 3b 0a 20 20 72 65 74 75 72  werBits;.  retur
fa680 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
fa690 2f 2a 0a 2a 2a 20 4c 4f 43 4b 46 49 4c 45 5f 46  /*.** LOCKFILE_F
fa6a0 41 49 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 20  AIL_IMMEDIATELY 
fa6b0 69 73 20 75 6e 64 65 66 69 6e 65 64 20 6f 6e 20  is undefined on 
fa6c0 73 6f 6d 65 20 57 69 6e 64 6f 77 73 20 73 79 73  some Windows sys
fa6d0 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  tems..*/.#ifndef
fa6e0 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49   LOCKFILE_FAIL_I
fa6f0 4d 4d 45 44 49 41 54 45 4c 59 0a 23 20 64 65 66  MMEDIATELY.# def
fa700 69 6e 65 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49  ine LOCKFILE_FAI
fa710 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 20 31 0a  L_IMMEDIATELY 1.
fa720 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 63  #endif../*.** Ac
fa730 71 75 69 72 65 20 61 20 72 65 61 64 65 72 20 6c  quire a reader l
fa740 6f 63 6b 2e 0a 2a 2a 20 44 69 66 66 65 72 65 6e  ock..** Differen
fa750 74 20 41 50 49 20 72 6f 75 74 69 6e 65 73 20 61  t API routines a
fa760 72 65 20 63 61 6c 6c 65 64 20 64 65 70 65 6e 64  re called depend
fa770 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 72 20 6f  ing on whether o
fa780 72 20 6e 6f 74 20 74 68 69 73 0a 2a 2a 20 69 73  r not this.** is
fa790 20 57 69 6e 39 35 20 6f 72 20 57 69 6e 4e 54 2e   Win95 or WinNT.
fa7a0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67  .*/.static int g
fa7b0 65 74 52 65 61 64 4c 6f 63 6b 28 77 69 6e 46 69  etReadLock(winFi
fa7c0 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e  le *pFile){.  in
fa7d0 74 20 72 65 73 3b 0a 20 20 69 66 28 20 69 73 4e  t res;.  if( isN
fa7e0 54 28 29 20 29 7b 0a 20 20 20 20 4f 56 45 52 4c  T() ){.    OVERL
fa7f0 41 50 50 45 44 20 6f 76 6c 70 3b 0a 20 20 20 20  APPED ovlp;.    
fa800 6f 76 6c 70 2e 4f 66 66 73 65 74 20 3d 20 53 48  ovlp.Offset = SH
fa810 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20  ARED_FIRST;.    
fa820 6f 76 6c 70 2e 4f 66 66 73 65 74 48 69 67 68 20  ovlp.OffsetHigh 
fa830 3d 20 30 3b 0a 20 20 20 20 6f 76 6c 70 2e 68 45  = 0;.    ovlp.hE
fa840 76 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20 72 65  vent = 0;.    re
fa850 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 45 78 28 70  s = LockFileEx(p
fa860 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 46 49 4c  File->h, LOCKFIL
fa870 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 41 54 45  E_FAIL_IMMEDIATE
fa880 4c 59 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  LY,.            
fa890 20 20 20 20 20 20 20 20 20 30 2c 20 53 48 41 52           0, SHAR
fa8a0 45 44 5f 53 49 5a 45 2c 20 30 2c 20 26 6f 76 6c  ED_SIZE, 0, &ovl
fa8b0 70 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73  p);./* isNT() is
fa8c0 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f   1 if SQLITE_OS_
fa8d0 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69  WINCE==1, so thi
fa8e0 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20  s else is never 
fa8f0 65 78 65 63 75 74 65 64 2e 20 0a 2a 2f 0a 23 69  executed. .*/.#i
fa900 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
fa910 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  E==0.  }else{.  
fa920 20 20 69 6e 74 20 6c 6b 3b 0a 20 20 20 20 73 71    int lk;.    sq
fa930 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73  lite3_randomness
fa940 28 73 69 7a 65 6f 66 28 6c 6b 29 2c 20 26 6c 6b  (sizeof(lk), &lk
fa950 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 73 68  );.    pFile->sh
fa960 61 72 65 64 4c 6f 63 6b 42 79 74 65 20 3d 20 28  aredLockByte = (
fa970 73 68 6f 72 74 29 28 28 6c 6b 20 26 20 30 78 37  short)((lk & 0x7
fa980 66 66 66 66 66 66 66 29 25 28 53 48 41 52 45 44  fffffff)%(SHARED
fa990 5f 53 49 5a 45 20 2d 20 31 29 29 3b 0a 20 20 20  _SIZE - 1));.   
fa9a0 20 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 28   res = LockFile(
fa9b0 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44  pFile->h, SHARED
fa9c0 5f 46 49 52 53 54 2b 70 46 69 6c 65 2d 3e 73 68  _FIRST+pFile->sh
fa9d0 61 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 30 2c  aredLockByte, 0,
fa9e0 20 31 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20   1, 0);.#endif. 
fa9f0 20 7d 0a 20 20 69 66 28 20 72 65 73 20 3d 3d 20   }.  if( res == 
faa00 30 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  0 ){.    pFile->
faa10 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c  lastErrno = GetL
faa20 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 7d 0a  astError();.  }.
faa30 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a    return res;.}.
faa40 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 61 20 72 65  ./*.** Undo a re
faa50 61 64 6c 6f 63 6b 0a 2a 2f 0a 73 74 61 74 69 63  adlock.*/.static
faa60 20 69 6e 74 20 75 6e 6c 6f 63 6b 52 65 61 64 4c   int unlockReadL
faa70 6f 63 6b 28 77 69 6e 46 69 6c 65 20 2a 70 46 69  ock(winFile *pFi
faa80 6c 65 29 7b 0a 20 20 69 6e 74 20 72 65 73 3b 0a  le){.  int res;.
faa90 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a    if( isNT() ){.
faaa0 20 20 20 20 72 65 73 20 3d 20 55 6e 6c 6f 63 6b      res = Unlock
faab0 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53  File(pFile->h, S
faac0 48 41 52 45 44 5f 46 49 52 53 54 2c 20 30 2c 20  HARED_FIRST, 0, 
faad0 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b  SHARED_SIZE, 0);
faae0 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20  ./* isNT() is 1 
faaf0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
fab00 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65  CE==1, so this e
fab10 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65  lse is never exe
fab20 63 75 74 65 64 2e 20 0a 2a 2f 0a 23 69 66 20 53  cuted. .*/.#if S
fab30 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d  QLITE_OS_WINCE==
fab40 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  0.  }else{.    r
fab50 65 73 20 3d 20 55 6e 6c 6f 63 6b 46 69 6c 65 28  es = UnlockFile(
fab60 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44  pFile->h, SHARED
fab70 5f 46 49 52 53 54 20 2b 20 70 46 69 6c 65 2d 3e  _FIRST + pFile->
fab80 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20  sharedLockByte, 
fab90 30 2c 20 31 2c 20 30 29 3b 0a 23 65 6e 64 69 66  0, 1, 0);.#endif
faba0 0a 20 20 7d 0a 20 20 69 66 28 20 72 65 73 20 3d  .  }.  if( res =
fabb0 3d 20 30 20 29 7b 0a 20 20 20 20 70 46 69 6c 65  = 0 ){.    pFile
fabc0 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 65  ->lastErrno = Ge
fabd0 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20  tLastError();.  
fabe0 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a  }.  return res;.
fabf0 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68  }../*.** Lock th
fac00 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20  e file with the 
fac10 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62  lock specified b
fac20 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b  y parameter lock
fac30 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66  type - one.** of
fac40 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
fac50 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48  **.**     (1) SH
fac60 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20  ARED_LOCK.**    
fac70 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f   (2) RESERVED_LO
fac80 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45  CK.**     (3) PE
fac90 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  NDING_LOCK.**   
faca0 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f    (4) EXCLUSIVE_
facb0 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74  LOCK.**.** Somet
facc0 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73  imes when reques
facd0 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74  ting one lock st
face0 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20  ate, additional 
facf0 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61  lock states.** a
fad00 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62  re inserted in b
fad10 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63  etween.  The loc
fad20 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20  king might fail 
fad30 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61  on one of the la
fad40 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f  ter.** transitio
fad50 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c  ns leaving the l
fad60 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72  ock state differ
fad70 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74  ent from what it
fad80 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20   started but.** 
fad90 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69  still short of i
fada0 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f  ts goal.  The fo
fadb0 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68  llowing chart sh
fadc0 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a  ows the allowed.
fadd0 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61  ** transitions a
fade0 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20  nd the inserted 
fadf0 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61  intermediate sta
fae00 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e  tes:.**.**    UN
fae10 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44  LOCKED -> SHARED
fae20 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e  .**    SHARED ->
fae30 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20   RESERVED.**    
fae40 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49  SHARED -> (PENDI
fae50 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45  NG) -> EXCLUSIVE
fae60 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20  .**    RESERVED 
fae70 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20  -> (PENDING) -> 
fae80 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20  EXCLUSIVE.**    
fae90 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55  PENDING -> EXCLU
faea0 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  SIVE.**.** This 
faeb0 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c  routine will onl
faec0 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63  y increase a loc
faed0 6b 2e 20 20 54 68 65 20 77 69 6e 55 6e 6c 6f 63  k.  The winUnloc
faee0 6b 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 65  k() routine.** e
faef0 72 61 73 65 73 20 61 6c 6c 20 6c 6f 63 6b 73 20  rases all locks 
faf00 61 74 20 6f 6e 63 65 20 61 6e 64 20 72 65 74 75  at once and retu
faf10 72 6e 73 20 75 73 20 69 6d 6d 65 64 69 61 74 65  rns us immediate
faf20 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65  ly to locking le
faf30 76 65 6c 20 30 2e 0a 2a 2a 20 49 74 20 69 73 20  vel 0..** It is 
faf40 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20  not possible to 
faf50 6c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e  lower the lockin
faf60 67 20 6c 65 76 65 6c 20 6f 6e 65 20 73 74 65 70  g level one step
faf70 20 61 74 20 61 20 74 69 6d 65 2e 20 20 59 6f 75   at a time.  You
faf80 0a 2a 2a 20 6d 75 73 74 20 67 6f 20 73 74 72 61  .** must go stra
faf90 69 67 68 74 20 74 6f 20 6c 6f 63 6b 69 6e 67 20  ight to locking 
fafa0 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a 73 74 61 74  level 0..*/.stat
fafb0 69 63 20 69 6e 74 20 77 69 6e 4c 6f 63 6b 28 73  ic int winLock(s
fafc0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
fafd0 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a   int locktype){.
fafe0 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
faff0 45 5f 4f 4b 3b 20 20 20 20 2f 2a 20 52 65 74 75  E_OK;    /* Retu
fb000 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 75 62  rn code from sub
fb010 72 6f 75 74 69 6e 65 73 20 2a 2f 0a 20 20 69 6e  routines */.  in
fb020 74 20 72 65 73 20 3d 20 31 3b 20 20 20 20 20 20  t res = 1;      
fb030 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f       /* Result o
fb040 66 20 61 20 77 69 6e 64 6f 77 73 20 6c 6f 63 6b  f a windows lock
fb050 20 63 61 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e   call */.  int n
fb060 65 77 4c 6f 63 6b 74 79 70 65 3b 20 20 20 20 20  ewLocktype;     
fb070 20 20 2f 2a 20 53 65 74 20 70 46 69 6c 65 2d 3e    /* Set pFile->
fb080 6c 6f 63 6b 74 79 70 65 20 74 6f 20 74 68 69 73  locktype to this
fb090 20 76 61 6c 75 65 20 62 65 66 6f 72 65 20 65 78   value before ex
fb0a0 69 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 67  iting */.  int g
fb0b0 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20  otPendingLock = 
fb0c0 30 3b 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20  0;/* True if we 
fb0d0 61 63 71 75 69 72 65 64 20 61 20 50 45 4e 44 49  acquired a PENDI
fb0e0 4e 47 20 6c 6f 63 6b 20 74 68 69 73 20 74 69 6d  NG lock this tim
fb0f0 65 20 2a 2f 0a 20 20 77 69 6e 46 69 6c 65 20 2a  e */.  winFile *
fb100 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65  pFile = (winFile
fb110 2a 29 69 64 3b 0a 20 20 44 57 4f 52 44 20 65 72  *)id;.  DWORD er
fb120 72 6f 72 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a  ror = NO_ERROR;.
fb130 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30  .  assert( id!=0
fb140 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22   );.  OSTRACE5("
fb150 4c 4f 43 4b 20 25 64 20 25 64 20 77 61 73 20 25  LOCK %d %d was %
fb160 64 28 25 64 29 5c 6e 22 2c 0a 20 20 20 20 20 20  d(%d)\n",.      
fb170 20 20 20 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f      pFile->h, lo
fb180 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 6c  cktype, pFile->l
fb190 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e  ocktype, pFile->
fb1a0 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 29 3b  sharedLockByte);
fb1b0 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20  ..  /* If there 
fb1c0 69 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f 63  is already a loc
fb1d0 6b 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 6f  k of this type o
fb1e0 72 20 6d 6f 72 65 20 72 65 73 74 72 69 63 74 69  r more restricti
fb1f0 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 4f  ve on the.  ** O
fb200 73 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 6e  sFile, do nothin
fb210 67 2e 20 44 6f 6e 27 74 20 75 73 65 20 74 68 65  g. Don't use the
fb220 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 69 74 20   end_lock: exit 
fb230 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a 20 73 71  path, as.  ** sq
fb240 6c 69 74 65 33 4f 73 45 6e 74 65 72 4d 75 74 65  lite3OsEnterMute
fb250 78 28 29 20 68 61 73 6e 27 74 20 62 65 65 6e 20  x() hasn't been 
fb260 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20 20 2a 2f  called yet..  */
fb270 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f  .  if( pFile->lo
fb280 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 70 65  cktype>=locktype
fb290 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
fb2a0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20  QLITE_OK;.  }.. 
fb2b0 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68   /* Make sure th
fb2c0 65 20 6c 6f 63 6b 69 6e 67 20 73 65 71 75 65 6e  e locking sequen
fb2d0 63 65 20 69 73 20 63 6f 72 72 65 63 74 0a 20 20  ce is correct.  
fb2e0 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 46 69  */.  assert( pFi
fb2f0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f  le->locktype!=NO
fb300 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70  _LOCK || locktyp
fb310 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e==SHARED_LOCK )
fb320 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b  ;.  assert( lock
fb330 74 79 70 65 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f  type!=PENDING_LO
fb340 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  CK );.  assert( 
fb350 6c 6f 63 6b 74 79 70 65 21 3d 52 45 53 45 52 56  locktype!=RESERV
fb360 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46 69 6c 65  ED_LOCK || pFile
fb370 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52  ->locktype==SHAR
fb380 45 44 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a  ED_LOCK );..  /*
fb390 20 4c 6f 63 6b 20 74 68 65 20 50 45 4e 44 49 4e   Lock the PENDIN
fb3a0 47 5f 4c 4f 43 4b 20 62 79 74 65 20 69 66 20 77  G_LOCK byte if w
fb3b0 65 20 6e 65 65 64 20 74 6f 20 61 63 71 75 69 72  e need to acquir
fb3c0 65 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b  e a PENDING lock
fb3d0 20 6f 72 0a 20 20 2a 2a 20 61 20 53 48 41 52 45   or.  ** a SHARE
fb3e0 44 20 6c 6f 63 6b 2e 20 20 49 66 20 77 65 20 61  D lock.  If we a
fb3f0 72 65 20 61 63 71 75 69 72 69 6e 67 20 61 20 53  re acquiring a S
fb400 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20  HARED lock, the 
fb410 61 63 71 75 69 73 69 74 69 6f 6e 20 6f 66 0a 20  acquisition of. 
fb420 20 2a 2a 20 74 68 65 20 50 45 4e 44 49 4e 47 5f   ** the PENDING_
fb430 4c 4f 43 4b 20 62 79 74 65 20 69 73 20 74 65 6d  LOCK byte is tem
fb440 70 6f 72 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 6e  porary..  */.  n
fb450 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 70 46 69  ewLocktype = pFi
fb460 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20  le->locktype;.  
fb470 69 66 28 20 20 20 28 70 46 69 6c 65 2d 3e 6c 6f  if(   (pFile->lo
fb480 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 29  cktype==NO_LOCK)
fb490 0a 20 20 20 20 20 7c 7c 20 28 20 20 20 28 6c 6f  .     || (   (lo
fb4a0 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56  cktype==EXCLUSIV
fb4b0 45 5f 4c 4f 43 4b 29 0a 20 20 20 20 20 20 20 20  E_LOCK).        
fb4c0 20 26 26 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b   && (pFile->lock
fb4d0 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c  type==RESERVED_L
fb4e0 4f 43 4b 29 29 0a 20 20 29 7b 0a 20 20 20 20 69  OCK)).  ){.    i
fb4f0 6e 74 20 63 6e 74 20 3d 20 33 3b 0a 20 20 20 20  nt cnt = 3;.    
fb500 77 68 69 6c 65 28 20 63 6e 74 2d 2d 3e 30 20 26  while( cnt-->0 &
fb510 26 20 28 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c  & (res = LockFil
fb520 65 28 70 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44  e(pFile->h, PEND
fb530 49 4e 47 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20  ING_BYTE, 0, 1, 
fb540 30 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  0))==0 ){.      
fb550 2f 2a 20 54 72 79 20 33 20 74 69 6d 65 73 20 74  /* Try 3 times t
fb560 6f 20 67 65 74 20 74 68 65 20 70 65 6e 64 69 6e  o get the pendin
fb570 67 20 6c 6f 63 6b 2e 20 20 54 68 65 20 70 65 6e  g lock.  The pen
fb580 64 69 6e 67 20 6c 6f 63 6b 20 6d 69 67 68 74 20  ding lock might 
fb590 62 65 0a 20 20 20 20 20 20 2a 2a 20 68 65 6c 64  be.      ** held
fb5a0 20 62 79 20 61 6e 6f 74 68 65 72 20 72 65 61 64   by another read
fb5b0 65 72 20 70 72 6f 63 65 73 73 20 77 68 6f 20 77  er process who w
fb5c0 69 6c 6c 20 72 65 6c 65 61 73 65 20 69 74 20 6d  ill release it m
fb5d0 6f 6d 65 6e 74 61 72 69 6c 79 2e 0a 20 20 20 20  omentarily..    
fb5e0 20 20 2a 2f 0a 20 20 20 20 20 20 4f 53 54 52 41    */.      OSTRA
fb5f0 43 45 32 28 22 63 6f 75 6c 64 20 6e 6f 74 20 67  CE2("could not g
fb600 65 74 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63  et a PENDING loc
fb610 6b 2e 20 63 6e 74 3d 25 64 5c 6e 22 2c 20 63 6e  k. cnt=%d\n", cn
fb620 74 29 3b 0a 20 20 20 20 20 20 53 6c 65 65 70 28  t);.      Sleep(
fb630 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f  1);.    }.    go
fb640 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 72  tPendingLock = r
fb650 65 73 3b 0a 20 20 20 20 69 66 28 20 21 72 65 73  es;.    if( !res
fb660 20 29 7b 0a 20 20 20 20 20 20 65 72 72 6f 72 20   ){.      error 
fb670 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29  = GetLastError()
fb680 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
fb690 2a 20 41 63 71 75 69 72 65 20 61 20 73 68 61 72  * Acquire a shar
fb6a0 65 64 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69  ed lock.  */.  i
fb6b0 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  f( locktype==SHA
fb6c0 52 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20  RED_LOCK && res 
fb6d0 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
fb6e0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d  File->locktype==
fb6f0 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72  NO_LOCK );.    r
fb700 65 73 20 3d 20 67 65 74 52 65 61 64 4c 6f 63 6b  es = getReadLock
fb710 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 69 66 28  (pFile);.    if(
fb720 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20 6e 65   res ){.      ne
fb730 77 4c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52  wLocktype = SHAR
fb740 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c  ED_LOCK;.    }el
fb750 73 65 7b 0a 20 20 20 20 20 20 65 72 72 6f 72 20  se{.      error 
fb760 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29  = GetLastError()
fb770 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
fb780 2a 20 41 63 71 75 69 72 65 20 61 20 52 45 53 45  * Acquire a RESE
fb790 52 56 45 44 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20  RVED lock.  */. 
fb7a0 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 52   if( locktype==R
fb7b0 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20  ESERVED_LOCK && 
fb7c0 72 65 73 20 29 7b 0a 20 20 20 20 61 73 73 65 72  res ){.    asser
fb7d0 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  t( pFile->lockty
fb7e0 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe==SHARED_LOCK 
fb7f0 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f 63  );.    res = Loc
fb800 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20  kFile(pFile->h, 
fb810 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 30  RESERVED_BYTE, 0
fb820 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  , 1, 0);.    if(
fb830 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20 6e 65   res ){.      ne
fb840 77 4c 6f 63 6b 74 79 70 65 20 3d 20 52 45 53 45  wLocktype = RESE
fb850 52 56 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d  RVED_LOCK;.    }
fb860 65 6c 73 65 7b 0a 20 20 20 20 20 20 65 72 72 6f  else{.      erro
fb870 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72  r = GetLastError
fb880 28 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  ();.    }.  }.. 
fb890 20 2f 2a 20 41 63 71 75 69 72 65 20 61 20 50 45   /* Acquire a PE
fb8a0 4e 44 49 4e 47 20 6c 6f 63 6b 0a 20 20 2a 2f 0a  NDING lock.  */.
fb8b0 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d    if( locktype==
fb8c0 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26  EXCLUSIVE_LOCK &
fb8d0 26 20 72 65 73 20 29 7b 0a 20 20 20 20 6e 65 77  & res ){.    new
fb8e0 4c 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e 44 49  Locktype = PENDI
fb8f0 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20 67 6f 74  NG_LOCK;.    got
fb900 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b  PendingLock = 0;
fb910 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69  .  }..  /* Acqui
fb920 72 65 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20  re an EXCLUSIVE 
fb930 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20  lock.  */.  if( 
fb940 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53  locktype==EXCLUS
fb950 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20  IVE_LOCK && res 
fb960 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
fb970 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d  File->locktype>=
fb980 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20  SHARED_LOCK );. 
fb990 20 20 20 72 65 73 20 3d 20 75 6e 6c 6f 63 6b 52     res = unlockR
fb9a0 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a  eadLock(pFile);.
fb9b0 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 75 6e      OSTRACE2("un
fb9c0 72 65 61 64 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22  readlock = %d\n"
fb9d0 2c 20 72 65 73 29 3b 0a 20 20 20 20 72 65 73 20  , res);.    res 
fb9e0 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65  = LockFile(pFile
fb9f0 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53  ->h, SHARED_FIRS
fba00 54 2c 20 30 2c 20 53 48 41 52 45 44 5f 53 49 5a  T, 0, SHARED_SIZ
fba10 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72  E, 0);.    if( r
fba20 65 73 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c  es ){.      newL
fba30 6f 63 6b 74 79 70 65 20 3d 20 45 58 43 4c 55 53  ocktype = EXCLUS
fba40 49 56 45 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65  IVE_LOCK;.    }e
fba50 6c 73 65 7b 0a 20 20 20 20 20 20 65 72 72 6f 72  lse{.      error
fba60 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28   = GetLastError(
fba70 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45  );.      OSTRACE
fba80 32 28 22 65 72 72 6f 72 2d 63 6f 64 65 20 3d 20  2("error-code = 
fba90 25 64 5c 6e 22 2c 20 65 72 72 6f 72 29 3b 0a 20  %d\n", error);. 
fbaa0 20 20 20 20 20 67 65 74 52 65 61 64 4c 6f 63 6b       getReadLock
fbab0 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20  (pFile);.    }. 
fbac0 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 61   }..  /* If we a
fbad0 72 65 20 68 6f 6c 64 69 6e 67 20 61 20 50 45 4e  re holding a PEN
fbae0 44 49 4e 47 20 6c 6f 63 6b 20 74 68 61 74 20 6f  DING lock that o
fbaf0 75 67 68 74 20 74 6f 20 62 65 20 72 65 6c 65 61  ught to be relea
fbb00 73 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 72  sed, then.  ** r
fbb10 65 6c 65 61 73 65 20 69 74 20 6e 6f 77 2e 0a 20  elease it now.. 
fbb20 20 2a 2f 0a 20 20 69 66 28 20 67 6f 74 50 65 6e   */.  if( gotPen
fbb30 64 69 6e 67 4c 6f 63 6b 20 26 26 20 6c 6f 63 6b  dingLock && lock
fbb40 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
fbb50 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b 46  K ){.    UnlockF
fbb60 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 50 45  ile(pFile->h, PE
fbb70 4e 44 49 4e 47 5f 42 59 54 45 2c 20 30 2c 20 31  NDING_BYTE, 0, 1
fbb80 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  , 0);.  }..  /* 
fbb90 55 70 64 61 74 65 20 74 68 65 20 73 74 61 74 65  Update the state
fbba0 20 6f 66 20 74 68 65 20 6c 6f 63 6b 20 68 61 73   of the lock has
fbbb0 20 68 65 6c 64 20 69 6e 20 74 68 65 20 66 69 6c   held in the fil
fbbc0 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 68 65  e descriptor the
fbbd0 6e 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 74 68  n.  ** return th
fbbe0 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 72 65  e appropriate re
fbbf0 73 75 6c 74 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a  sult code..  */.
fbc00 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20    if( res ){.   
fbc10 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
fbc20 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4f 53  .  }else{.    OS
fbc30 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 46 41 49  TRACE4("LOCK FAI
fbc40 4c 45 44 20 25 64 20 74 72 79 69 6e 67 20 66 6f  LED %d trying fo
fbc50 72 20 25 64 20 62 75 74 20 67 6f 74 20 25 64 5c  r %d but got %d\
fbc60 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20  n", pFile->h,.  
fbc70 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70           locktyp
fbc80 65 2c 20 6e 65 77 4c 6f 63 6b 74 79 70 65 29 3b  e, newLocktype);
fbc90 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74  .    pFile->last
fbca0 45 72 72 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a 20  Errno = error;. 
fbcb0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42     rc = SQLITE_B
fbcc0 55 53 59 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65  USY;.  }.  pFile
fbcd0 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 28 75 38  ->locktype = (u8
fbce0 29 6e 65 77 4c 6f 63 6b 74 79 70 65 3b 0a 20 20  )newLocktype;.  
fbcf0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
fbd00 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
fbd10 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65   checks if there
fbd20 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c   is a RESERVED l
fbd30 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20  ock held on the 
fbd40 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c  specified.** fil
fbd50 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79  e by this or any
fbd60 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20   other process. 
fbd70 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69  If such a lock i
fbd80 73 20 68 65 6c 64 2c 20 72 65 74 75 72 6e 0a 2a  s held, return.*
fbd90 2a 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 6f 74 68 65  * non-zero, othe
fbda0 72 77 69 73 65 20 7a 65 72 6f 2e 0a 2a 2f 0a 73  rwise zero..*/.s
fbdb0 74 61 74 69 63 20 69 6e 74 20 77 69 6e 43 68 65  tatic int winChe
fbdc0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73  ckReservedLock(s
fbdd0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
fbde0 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a   int *pResOut){.
fbdf0 20 20 69 6e 74 20 72 63 3b 0a 20 20 77 69 6e 46    int rc;.  winF
fbe00 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69  ile *pFile = (wi
fbe10 6e 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73  nFile*)id;..  as
fbe20 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20  sert( id!=0 );. 
fbe30 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b   if( pFile->lock
fbe40 74 79 70 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c  type>=RESERVED_L
fbe50 4f 43 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  OCK ){.    rc = 
fbe60 31 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28  1;.    OSTRACE3(
fbe70 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64  "TEST WR-LOCK %d
fbe80 20 25 64 20 28 6c 6f 63 61 6c 29 5c 6e 22 2c 20   %d (local)\n", 
fbe90 70 46 69 6c 65 2d 3e 68 2c 20 72 63 29 3b 0a 20  pFile->h, rc);. 
fbea0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d   }else{.    rc =
fbeb0 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d   LockFile(pFile-
fbec0 3e 68 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54  >h, RESERVED_BYT
fbed0 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 20  E, 0, 1, 0);.   
fbee0 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20   if( rc ){.     
fbef0 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c   UnlockFile(pFil
fbf00 65 2d 3e 68 2c 20 52 45 53 45 52 56 45 44 5f 42  e->h, RESERVED_B
fbf10 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20  YTE, 0, 1, 0);. 
fbf20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 21 72     }.    rc = !r
fbf30 63 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28  c;.    OSTRACE3(
fbf40 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64  "TEST WR-LOCK %d
fbf50 20 25 64 20 28 72 65 6d 6f 74 65 29 5c 6e 22 2c   %d (remote)\n",
fbf60 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 29 3b 0a   pFile->h, rc);.
fbf70 20 20 7d 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d    }.  *pResOut =
fbf80 20 72 63 3b 0a 20 20 72 65 74 75 72 6e 20 53 51   rc;.  return SQ
fbf90 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
fbfa0 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b  * Lower the lock
fbfb0 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c  ing level on fil
fbfc0 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 64 20  e descriptor id 
fbfd0 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f  to locktype.  lo
fbfe0 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62  cktype.** must b
fbff0 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b  e either NO_LOCK
fc000 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e   or SHARED_LOCK.
fc010 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f  .**.** If the lo
fc020 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74  cking level of t
fc030 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
fc040 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74  or is already at
fc050 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65   or below.** the
fc060 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69   requested locki
fc070 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72  ng level, this r
fc080 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f  outine is a no-o
fc090 70 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 6e  p..**.** It is n
fc0a0 6f 74 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20  ot possible for 
fc0b0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20  this routine to 
fc0c0 66 61 69 6c 20 69 66 20 74 68 65 20 73 65 63 6f  fail if the seco
fc0d0 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69  nd argument.** i
fc0e0 73 20 4e 4f 5f 4c 4f 43 4b 2e 20 20 49 66 20 74  s NO_LOCK.  If t
fc0f0 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
fc100 6e 74 20 69 73 20 53 48 41 52 45 44 5f 4c 4f 43  nt is SHARED_LOC
fc110 4b 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74  K then this rout
fc120 69 6e 65 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74  ine.** might ret
fc130 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
fc140 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ;.*/.static int 
fc150 77 69 6e 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65  winUnlock(sqlite
fc160 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
fc170 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74  locktype){.  int
fc180 20 74 79 70 65 3b 0a 20 20 77 69 6e 46 69 6c 65   type;.  winFile
fc190 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69   *pFile = (winFi
fc1a0 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 72 63  le*)id;.  int rc
fc1b0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
fc1c0 61 73 73 65 72 74 28 20 70 46 69 6c 65 21 3d 30  assert( pFile!=0
fc1d0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f   );.  assert( lo
fc1e0 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c  cktype<=SHARED_L
fc1f0 4f 43 4b 20 29 3b 0a 20 20 4f 53 54 52 41 43 45  OCK );.  OSTRACE
fc200 35 28 22 55 4e 4c 4f 43 4b 20 25 64 20 74 6f 20  5("UNLOCK %d to 
fc210 25 64 20 77 61 73 20 25 64 28 25 64 29 5c 6e 22  %d was %d(%d)\n"
fc220 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b  , pFile->h, lock
fc230 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20 20 20  type,.          
fc240 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c  pFile->locktype,
fc250 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f   pFile->sharedLo
fc260 63 6b 42 79 74 65 29 3b 0a 20 20 74 79 70 65 20  ckByte);.  type 
fc270 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  = pFile->locktyp
fc280 65 3b 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 45  e;.  if( type>=E
fc290 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b  XCLUSIVE_LOCK ){
fc2a0 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28  .    UnlockFile(
fc2b0 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44  pFile->h, SHARED
fc2c0 5f 46 49 52 53 54 2c 20 30 2c 20 53 48 41 52 45  _FIRST, 0, SHARE
fc2d0 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20 20 20  D_SIZE, 0);.    
fc2e0 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48  if( locktype==SH
fc2f0 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 21 67 65  ARED_LOCK && !ge
fc300 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29  tReadLock(pFile)
fc310 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69   ){.      /* Thi
fc320 73 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 68  s should never h
fc330 61 70 70 65 6e 2e 20 20 57 65 20 73 68 6f 75 6c  appen.  We shoul
fc340 64 20 61 6c 77 61 79 73 20 62 65 20 61 62 6c 65  d always be able
fc350 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 72 65 61   to.      ** rea
fc360 63 71 75 69 72 65 20 74 68 65 20 72 65 61 64 20  cquire the read 
fc370 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 72 63  lock */.      rc
fc380 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f   = SQLITE_IOERR_
fc390 55 4e 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20  UNLOCK;.    }.  
fc3a0 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 52 45  }.  if( type>=RE
fc3b0 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20  SERVED_LOCK ){. 
fc3c0 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46     UnlockFile(pF
fc3d0 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56 45 44  ile->h, RESERVED
fc3e0 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b  _BYTE, 0, 1, 0);
fc3f0 0a 20 20 7d 0a 20 20 69 66 28 20 6c 6f 63 6b 74  .  }.  if( lockt
fc400 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 26 26 20  ype==NO_LOCK && 
fc410 74 79 70 65 3e 3d 53 48 41 52 45 44 5f 4c 4f 43  type>=SHARED_LOC
fc420 4b 20 29 7b 0a 20 20 20 20 75 6e 6c 6f 63 6b 52  K ){.    unlockR
fc430 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a  eadLock(pFile);.
fc440 20 20 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d    }.  if( type>=
fc450 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b 0a  PENDING_LOCK ){.
fc460 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70      UnlockFile(p
fc470 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47  File->h, PENDING
fc480 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b  _BYTE, 0, 1, 0);
fc490 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f  .  }.  pFile->lo
fc4a0 63 6b 74 79 70 65 20 3d 20 28 75 38 29 6c 6f 63  cktype = (u8)loc
fc4b0 6b 74 79 70 65 3b 0a 20 20 72 65 74 75 72 6e 20  ktype;.  return 
fc4c0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e  rc;.}../*.** Con
fc4d0 74 72 6f 6c 20 61 6e 64 20 71 75 65 72 79 20 6f  trol and query o
fc4e0 66 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20  f the open file 
fc4f0 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  handle..*/.stati
fc500 63 20 69 6e 74 20 77 69 6e 46 69 6c 65 43 6f 6e  c int winFileCon
fc510 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c  trol(sqlite3_fil
fc520 65 20 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76  e *id, int op, v
fc530 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 73 77  oid *pArg){.  sw
fc540 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20  itch( op ){.    
fc550 63 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54  case SQLITE_FCNT
fc560 4c 5f 4c 4f 43 4b 53 54 41 54 45 3a 20 7b 0a 20  L_LOCKSTATE: {. 
fc570 20 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67       *(int*)pArg
fc580 20 3d 20 28 28 77 69 6e 46 69 6c 65 2a 29 69 64   = ((winFile*)id
fc590 29 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20  )->locktype;.   
fc5a0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
fc5b0 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  _OK;.    }.    c
fc5c0 61 73 65 20 53 51 4c 49 54 45 5f 4c 41 53 54 5f  ase SQLITE_LAST_
fc5d0 45 52 52 4e 4f 3a 20 7b 0a 20 20 20 20 20 20 2a  ERRNO: {.      *
fc5e0 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 28 69 6e  (int*)pArg = (in
fc5f0 74 29 28 28 77 69 6e 46 69 6c 65 2a 29 69 64 29  t)((winFile*)id)
fc600 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20 20  ->lastErrno;.   
fc610 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
fc620 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  _OK;.    }.  }. 
fc630 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
fc640 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  RROR;.}../*.** R
fc650 65 74 75 72 6e 20 74 68 65 20 73 65 63 74 6f 72  eturn the sector
fc660 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f   size in bytes o
fc670 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  f the underlying
fc680 20 62 6c 6f 63 6b 20 64 65 76 69 63 65 20 66 6f   block device fo
fc690 72 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 66 69  r.** the specifi
fc6a0 65 64 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73  ed file. This is
fc6b0 20 61 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 35   almost always 5
fc6c0 31 32 20 62 79 74 65 73 2c 20 62 75 74 20 6d 61  12 bytes, but ma
fc6d0 79 20 62 65 0a 2a 2a 20 6c 61 72 67 65 72 20 66  y be.** larger f
fc6e0 6f 72 20 73 6f 6d 65 20 64 65 76 69 63 65 73 2e  or some devices.
fc6f0 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 6f  .**.** SQLite co
fc700 64 65 20 61 73 73 75 6d 65 73 20 74 68 69 73 20  de assumes this 
fc710 66 75 6e 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20  function cannot 
fc720 66 61 69 6c 2e 20 49 74 20 61 6c 73 6f 20 61 73  fail. It also as
fc730 73 75 6d 65 73 20 74 68 61 74 0a 2a 2a 20 69 66  sumes that.** if
fc740 20 74 77 6f 20 66 69 6c 65 73 20 61 72 65 20 63   two files are c
fc750 72 65 61 74 65 64 20 69 6e 20 74 68 65 20 73 61  reated in the sa
fc760 6d 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 64  me file-system d
fc770 69 72 65 63 74 6f 72 79 20 28 69 2e 65 2e 0a 2a  irectory (i.e..*
fc780 2a 20 61 20 64 61 74 61 62 61 73 65 20 61 6e 64  * a database and
fc790 20 69 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   its journal fil
fc7a0 65 29 20 74 68 61 74 20 74 68 65 20 73 65 63 74  e) that the sect
fc7b0 6f 72 20 73 69 7a 65 20 77 69 6c 6c 20 62 65 20  or size will be 
fc7c0 74 68 65 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 20  the.** same for 
fc7d0 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  both..*/.static 
fc7e0 69 6e 74 20 77 69 6e 53 65 63 74 6f 72 53 69 7a  int winSectorSiz
fc7f0 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
fc800 69 64 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69  id){.  assert( i
fc810 64 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e  d!=0 );.  return
fc820 20 28 69 6e 74 29 28 28 28 77 69 6e 46 69 6c 65   (int)(((winFile
fc830 2a 29 69 64 29 2d 3e 73 65 63 74 6f 72 53 69 7a  *)id)->sectorSiz
fc840 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  e);.}../*.** Ret
fc850 75 72 6e 20 61 20 76 65 63 74 6f 72 20 6f 66 20  urn a vector of 
fc860 64 65 76 69 63 65 20 63 68 61 72 61 63 74 65 72  device character
fc870 69 73 74 69 63 73 2e 0a 2a 2f 0a 73 74 61 74 69  istics..*/.stati
fc880 63 20 69 6e 74 20 77 69 6e 44 65 76 69 63 65 43  c int winDeviceC
fc890 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 73  haracteristics(s
fc8a0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29  qlite3_file *id)
fc8b0 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  {.  UNUSED_PARAM
fc8c0 45 54 45 52 28 69 64 29 3b 0a 20 20 72 65 74 75  ETER(id);.  retu
fc8d0 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  rn 0;.}../*.** T
fc8e0 68 69 73 20 76 65 63 74 6f 72 20 64 65 66 69 6e  his vector defin
fc8f0 65 73 20 61 6c 6c 20 74 68 65 20 6d 65 74 68 6f  es all the metho
fc900 64 73 20 74 68 61 74 20 63 61 6e 20 6f 70 65 72  ds that can oper
fc910 61 74 65 20 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c  ate on an.** sql
fc920 69 74 65 33 5f 66 69 6c 65 20 66 6f 72 20 77 69  ite3_file for wi
fc930 6e 33 32 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  n32..*/.static c
fc940 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f  onst sqlite3_io_
fc950 6d 65 74 68 6f 64 73 20 77 69 6e 49 6f 4d 65 74  methods winIoMet
fc960 68 6f 64 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20  hod = {.  1,    
fc970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fc980 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20      /* iVersion 
fc990 2a 2f 0a 20 20 77 69 6e 43 6c 6f 73 65 2c 0a 20  */.  winClose,. 
fc9a0 20 77 69 6e 52 65 61 64 2c 0a 20 20 77 69 6e 57   winRead,.  winW
fc9b0 72 69 74 65 2c 0a 20 20 77 69 6e 54 72 75 6e 63  rite,.  winTrunc
fc9c0 61 74 65 2c 0a 20 20 77 69 6e 53 79 6e 63 2c 0a  ate,.  winSync,.
fc9d0 20 20 77 69 6e 46 69 6c 65 53 69 7a 65 2c 0a 20    winFileSize,. 
fc9e0 20 77 69 6e 4c 6f 63 6b 2c 0a 20 20 77 69 6e 55   winLock,.  winU
fc9f0 6e 6c 6f 63 6b 2c 0a 20 20 77 69 6e 43 68 65 63  nlock,.  winChec
fca00 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 2c 0a 20  kReservedLock,. 
fca10 20 77 69 6e 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c   winFileControl,
fca20 0a 20 20 77 69 6e 53 65 63 74 6f 72 53 69 7a 65  .  winSectorSize
fca30 2c 0a 20 20 77 69 6e 44 65 76 69 63 65 43 68 61  ,.  winDeviceCha
fca40 72 61 63 74 65 72 69 73 74 69 63 73 0a 7d 3b 0a  racteristics.};.
fca50 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
fca60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fca70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fca80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fca90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
fcaa0 20 48 65 72 65 20 65 6e 64 73 20 74 68 65 20 49   Here ends the I
fcab0 2f 4f 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20  /O methods that 
fcac0 66 6f 72 6d 20 74 68 65 20 73 71 6c 69 74 65 33  form the sqlite3
fcad0 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65  _io_methods obje
fcae0 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65  ct..**.** The ne
fcaf0 78 74 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65  xt block of code
fcb00 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20   implements the 
fcb10 56 46 53 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a 2a  VFS methods..***
fcb20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fcb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fcb40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fcb50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fcb60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a  *********/../*.*
fcb70 2a 20 43 6f 6e 76 65 72 74 20 61 20 55 54 46 2d  * Convert a UTF-
fcb80 38 20 66 69 6c 65 6e 61 6d 65 20 69 6e 74 6f 20  8 filename into 
fcb90 77 68 61 74 65 76 65 72 20 66 6f 72 6d 20 74 68  whatever form th
fcba0 65 20 75 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a 20  e underlying.** 
fcbb0 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  operating system
fcbc0 20 77 61 6e 74 73 20 66 69 6c 65 6e 61 6d 65 73   wants filenames
fcbd0 20 69 6e 2e 20 20 53 70 61 63 65 20 74 6f 20 68   in.  Space to h
fcbe0 6f 6c 64 20 74 68 65 20 72 65 73 75 6c 74 0a 2a  old the result.*
fcbf0 2a 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72  * is obtained fr
fcc00 6f 6d 20 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d 75  om malloc and mu
fcc10 73 74 20 62 65 20 66 72 65 65 64 20 62 79 20 74  st be freed by t
fcc20 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75  he calling.** fu
fcc30 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  nction..*/.stati
fcc40 63 20 76 6f 69 64 20 2a 63 6f 6e 76 65 72 74 55  c void *convertU
fcc50 74 66 38 46 69 6c 65 6e 61 6d 65 28 63 6f 6e 73  tf8Filename(cons
fcc60 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d  t char *zFilenam
fcc70 65 29 7b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e  e){.  void *zCon
fcc80 76 65 72 74 65 64 20 3d 20 30 3b 0a 20 20 69 66  verted = 0;.  if
fcc90 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20  ( isNT() ){.    
fcca0 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 75 74 66  zConverted = utf
fccb0 38 54 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65  8ToUnicode(zFile
fccc0 6e 61 6d 65 29 3b 0a 2f 2a 20 69 73 4e 54 28 29  name);./* isNT()
fccd0 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f   is 1 if SQLITE_
fcce0 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20  OS_WINCE==1, so 
fccf0 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76  this else is nev
fcd00 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2f  er executed. .*/
fcd10 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  .#if SQLITE_OS_W
fcd20 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b  INCE==0.  }else{
fcd30 0a 20 20 20 20 7a 43 6f 6e 76 65 72 74 65 64 20  .    zConverted 
fcd40 3d 20 75 74 66 38 54 6f 4d 62 63 73 28 7a 46 69  = utf8ToMbcs(zFi
fcd50 6c 65 6e 61 6d 65 29 3b 0a 23 65 6e 64 69 66 0a  lename);.#endif.
fcd60 20 20 7d 0a 20 20 2f 2a 20 63 61 6c 6c 65 72 20    }.  /* caller 
fcd70 77 69 6c 6c 20 68 61 6e 64 6c 65 20 6f 75 74 20  will handle out 
fcd80 6f 66 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 72  of memory */.  r
fcd90 65 74 75 72 6e 20 7a 43 6f 6e 76 65 72 74 65 64  eturn zConverted
fcda0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74  ;.}../*.** Creat
fcdb0 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69  e a temporary fi
fcdc0 6c 65 20 6e 61 6d 65 20 69 6e 20 7a 42 75 66 2e  le name in zBuf.
fcdd0 20 20 7a 42 75 66 20 6d 75 73 74 20 62 65 20 62    zBuf must be b
fcde0 69 67 20 65 6e 6f 75 67 68 20 74 6f 0a 2a 2a 20  ig enough to.** 
fcdf0 68 6f 6c 64 20 61 74 20 70 56 66 73 2d 3e 6d 78  hold at pVfs->mx
fce00 50 61 74 68 6e 61 6d 65 20 63 68 61 72 61 63 74  Pathname charact
fce10 65 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ers..*/.static i
fce20 6e 74 20 67 65 74 54 65 6d 70 6e 61 6d 65 28 69  nt getTempname(i
fce30 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a  nt nBuf, char *z
fce40 42 75 66 29 7b 0a 20 20 73 74 61 74 69 63 20 63  Buf){.  static c
fce50 68 61 72 20 7a 43 68 61 72 73 5b 5d 20 3d 0a 20  har zChars[] =. 
fce60 20 20 20 22 61 62 63 64 65 66 67 68 69 6a 6b 6c     "abcdefghijkl
fce70 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a 22 0a  mnopqrstuvwxyz".
fce80 20 20 20 20 22 41 42 43 44 45 46 47 48 49 4a 4b      "ABCDEFGHIJK
fce90 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 22  LMNOPQRSTUVWXYZ"
fcea0 0a 20 20 20 20 22 30 31 32 33 34 35 36 37 38 39  .    "0123456789
fceb0 22 3b 0a 20 20 73 69 7a 65 5f 74 20 69 2c 20 6a  ";.  size_t i, j
fcec0 3b 0a 20 20 63 68 61 72 20 7a 54 65 6d 70 50 61  ;.  char zTempPa
fced0 74 68 5b 4d 41 58 5f 50 41 54 48 2b 31 5d 3b 0a  th[MAX_PATH+1];.
fcee0 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 74 65    if( sqlite3_te
fcef0 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 29 7b 0a  mp_directory ){.
fcf00 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
fcf10 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 2d 33 30  intf(MAX_PATH-30
fcf20 2c 20 7a 54 65 6d 70 50 61 74 68 2c 20 22 25 73  , zTempPath, "%s
fcf30 22 2c 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f  ", sqlite3_temp_
fcf40 64 69 72 65 63 74 6f 72 79 29 3b 0a 20 20 7d 65  directory);.  }e
fcf50 6c 73 65 20 69 66 28 20 69 73 4e 54 28 29 20 29  lse if( isNT() )
fcf60 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d 75 6c  {.    char *zMul
fcf70 74 69 3b 0a 20 20 20 20 57 43 48 41 52 20 7a 57  ti;.    WCHAR zW
fcf80 69 64 65 50 61 74 68 5b 4d 41 58 5f 50 41 54 48  idePath[MAX_PATH
fcf90 5d 3b 0a 20 20 20 20 47 65 74 54 65 6d 70 50 61  ];.    GetTempPa
fcfa0 74 68 57 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c  thW(MAX_PATH-30,
fcfb0 20 7a 57 69 64 65 50 61 74 68 29 3b 0a 20 20 20   zWidePath);.   
fcfc0 20 7a 4d 75 6c 74 69 20 3d 20 75 6e 69 63 6f 64   zMulti = unicod
fcfd0 65 54 6f 55 74 66 38 28 7a 57 69 64 65 50 61 74  eToUtf8(zWidePat
fcfe0 68 29 3b 0a 20 20 20 20 69 66 28 20 7a 4d 75 6c  h);.    if( zMul
fcff0 74 69 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ti ){.      sqli
fd000 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41 58  te3_snprintf(MAX
fd010 5f 50 41 54 48 2d 33 30 2c 20 7a 54 65 6d 70 50  _PATH-30, zTempP
fd020 61 74 68 2c 20 22 25 73 22 2c 20 7a 4d 75 6c 74  ath, "%s", zMult
fd030 69 29 3b 0a 20 20 20 20 20 20 66 72 65 65 28 7a  i);.      free(z
fd040 4d 75 6c 74 69 29 3b 0a 20 20 20 20 7d 65 6c 73  Multi);.    }els
fd050 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  e{.      return 
fd060 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
fd070 20 20 7d 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73    }./* isNT() is
fd080 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f   1 if SQLITE_OS_
fd090 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69  WINCE==1, so thi
fd0a0 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20  s else is never 
fd0b0 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69  executed. .** Si
fd0c0 6e 63 65 20 74 68 65 20 41 53 43 49 49 20 76 65  nce the ASCII ve
fd0d0 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57  rsion of these W
fd0e0 69 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f  indows API do no
fd0f0 74 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43  t exist for WINC
fd100 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72  E,.** it's impor
fd110 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65  tant to not refe
fd120 72 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57  rence them for W
fd130 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a  INCE builds..*/.
fd140 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
fd150 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a  NCE==0.  }else{.
fd160 20 20 20 20 63 68 61 72 20 2a 7a 55 74 66 38 3b      char *zUtf8;
fd170 0a 20 20 20 20 63 68 61 72 20 7a 4d 62 63 73 50  .    char zMbcsP
fd180 61 74 68 5b 4d 41 58 5f 50 41 54 48 5d 3b 0a 20  ath[MAX_PATH];. 
fd190 20 20 20 47 65 74 54 65 6d 70 50 61 74 68 41 28     GetTempPathA(
fd1a0 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 4d 62  MAX_PATH-30, zMb
fd1b0 63 73 50 61 74 68 29 3b 0a 20 20 20 20 7a 55 74  csPath);.    zUt
fd1c0 66 38 20 3d 20 73 71 6c 69 74 65 33 5f 77 69 6e  f8 = sqlite3_win
fd1d0 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28  32_mbcs_to_utf8(
fd1e0 7a 4d 62 63 73 50 61 74 68 29 3b 0a 20 20 20 20  zMbcsPath);.    
fd1f0 69 66 28 20 7a 55 74 66 38 20 29 7b 0a 20 20 20  if( zUtf8 ){.   
fd200 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
fd210 6e 74 66 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c  ntf(MAX_PATH-30,
fd220 20 7a 54 65 6d 70 50 61 74 68 2c 20 22 25 73 22   zTempPath, "%s"
fd230 2c 20 7a 55 74 66 38 29 3b 0a 20 20 20 20 20 20  , zUtf8);.      
fd240 66 72 65 65 28 7a 55 74 66 38 29 3b 0a 20 20 20  free(zUtf8);.   
fd250 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65   }else{.      re
fd260 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
fd270 4d 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  M;.    }.#endif.
fd280 20 20 7d 0a 20 20 66 6f 72 28 69 3d 73 71 6c 69    }.  for(i=sqli
fd290 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 65 6d  te3Strlen30(zTem
fd2a0 70 50 61 74 68 29 3b 20 69 3e 30 20 26 26 20 7a  pPath); i>0 && z
fd2b0 54 65 6d 70 50 61 74 68 5b 69 2d 31 5d 3d 3d 27  TempPath[i-1]=='
fd2c0 5c 5c 27 3b 20 69 2d 2d 29 7b 7d 0a 20 20 7a 54  \\'; i--){}.  zT
fd2d0 65 6d 70 50 61 74 68 5b 69 5d 20 3d 20 30 3b 0a  empPath[i] = 0;.
fd2e0 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
fd2f0 74 66 28 6e 42 75 66 2d 33 30 2c 20 7a 42 75 66  tf(nBuf-30, zBuf
fd300 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
fd310 20 20 20 20 20 22 25 73 5c 5c 22 53 51 4c 49 54       "%s\\"SQLIT
fd320 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46  E_TEMP_FILE_PREF
fd330 49 58 2c 20 7a 54 65 6d 70 50 61 74 68 29 3b 0a  IX, zTempPath);.
fd340 20 20 6a 20 3d 20 73 71 6c 69 74 65 33 53 74 72    j = sqlite3Str
fd350 6c 65 6e 33 30 28 7a 42 75 66 29 3b 0a 20 20 73  len30(zBuf);.  s
fd360 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73  qlite3_randomnes
fd370 73 28 32 30 2c 20 26 7a 42 75 66 5b 6a 5d 29 3b  s(20, &zBuf[j]);
fd380 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 30  .  for(i=0; i<20
fd390 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 20 20  ; i++, j++){.   
fd3a0 20 7a 42 75 66 5b 6a 5d 20 3d 20 28 63 68 61 72   zBuf[j] = (char
fd3b0 29 7a 43 68 61 72 73 5b 20 28 28 75 6e 73 69 67  )zChars[ ((unsig
fd3c0 6e 65 64 20 63 68 61 72 29 7a 42 75 66 5b 6a 5d  ned char)zBuf[j]
fd3d0 29 25 28 73 69 7a 65 6f 66 28 7a 43 68 61 72 73  )%(sizeof(zChars
fd3e0 29 2d 31 29 20 5d 3b 0a 20 20 7d 0a 20 20 7a 42  )-1) ];.  }.  zB
fd3f0 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20 20 4f 53 54  uf[j] = 0;.  OST
fd400 52 41 43 45 32 28 22 54 45 4d 50 20 46 49 4c 45  RACE2("TEMP FILE
fd410 4e 41 4d 45 3a 20 25 73 5c 6e 22 2c 20 7a 42 75  NAME: %s\n", zBu
fd420 66 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  f);.  return SQL
fd430 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 0a 2f 2a 0a 2a  ITE_OK; .}../*.*
fd440 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c  * The return val
fd450 75 65 20 6f 66 20 67 65 74 4c 61 73 74 45 72 72  ue of getLastErr
fd460 6f 72 4d 73 67 0a 2a 2a 20 69 73 20 7a 65 72 6f  orMsg.** is zero
fd470 20 69 66 20 74 68 65 20 65 72 72 6f 72 20 6d 65   if the error me
fd480 73 73 61 67 65 20 66 69 74 73 20 69 6e 20 74 68  ssage fits in th
fd490 65 20 62 75 66 66 65 72 2c 20 6f 72 20 6e 6f 6e  e buffer, or non
fd4a0 2d 7a 65 72 6f 0a 2a 2a 20 6f 74 68 65 72 77 69  -zero.** otherwi
fd4b0 73 65 20 28 69 66 20 74 68 65 20 6d 65 73 73 61  se (if the messa
fd4c0 67 65 20 77 61 73 20 74 72 75 6e 63 61 74 65 64  ge was truncated
fd4d0 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
fd4e0 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73 67   getLastErrorMsg
fd4f0 28 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20  (int nBuf, char 
fd500 2a 7a 42 75 66 29 7b 0a 20 20 44 57 4f 52 44 20  *zBuf){.  DWORD 
fd510 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45  error = GetLastE
fd520 72 72 6f 72 28 29 3b 0a 0a 23 69 66 20 53 51 4c  rror();..#if SQL
fd530 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 73  ITE_OS_WINCE.  s
fd540 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
fd550 6e 42 75 66 2c 20 7a 42 75 66 2c 20 22 4f 73 45  nBuf, zBuf, "OsE
fd560 72 72 6f 72 20 30 78 25 78 20 28 25 75 29 22 2c  rror 0x%x (%u)",
fd570 20 65 72 72 6f 72 2c 20 65 72 72 6f 72 29 3b 0a   error, error);.
fd580 23 65 6c 73 65 0a 20 20 2f 2a 20 46 6f 72 6d 61  #else.  /* Forma
fd590 74 4d 65 73 73 61 67 65 20 72 65 74 75 72 6e 73  tMessage returns
fd5a0 20 30 20 6f 6e 20 66 61 69 6c 75 72 65 2e 20 20   0 on failure.  
fd5b0 4f 74 68 65 72 77 69 73 65 20 69 74 0a 20 20 2a  Otherwise it.  *
fd5c0 2a 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75  * returns the nu
fd5d0 6d 62 65 72 20 6f 66 20 54 43 48 41 52 73 20 77  mber of TCHARs w
fd5e0 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6f 75  ritten to the ou
fd5f0 74 70 75 74 0a 20 20 2a 2a 20 62 75 66 66 65 72  tput.  ** buffer
fd600 2c 20 65 78 63 6c 75 64 69 6e 67 20 74 68 65 20  , excluding the 
fd610 74 65 72 6d 69 6e 61 74 69 6e 67 20 6e 75 6c 6c  terminating null
fd620 20 63 68 61 72 2e 0a 20 20 2a 2f 0a 20 20 69 66   char..  */.  if
fd630 20 28 21 46 6f 72 6d 61 74 4d 65 73 73 61 67 65   (!FormatMessage
fd640 41 28 46 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45  A(FORMAT_MESSAGE
fd650 5f 46 52 4f 4d 5f 53 59 53 54 45 4d 2c 0a 20 20  _FROM_SYSTEM,.  
fd660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fd670 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20      NULL,.      
fd680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fd690 65 72 72 6f 72 2c 0a 20 20 20 20 20 20 20 20 20  error,.         
fd6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 0a               0,.
fd6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fd6c0 20 20 20 20 20 20 7a 42 75 66 2c 0a 20 20 20 20        zBuf,.    
fd6d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fd6e0 20 20 6e 42 75 66 2d 31 2c 0a 20 20 20 20 20 20    nBuf-1,.      
fd6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fd700 30 29 29 0a 20 20 7b 0a 20 20 20 20 73 71 6c 69  0)).  {.    sqli
fd710 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75  te3_snprintf(nBu
fd720 66 2c 20 7a 42 75 66 2c 20 22 4f 73 45 72 72 6f  f, zBuf, "OsErro
fd730 72 20 30 78 25 78 20 28 25 75 29 22 2c 20 65 72  r 0x%x (%u)", er
fd740 72 6f 72 2c 20 65 72 72 6f 72 29 3b 0a 20 20 7d  ror, error);.  }
fd750 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72  .#endif..  retur
fd760 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70  n 0;.}../*.** Op
fd770 65 6e 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74  en a file..*/.st
fd780 61 74 69 63 20 69 6e 74 20 77 69 6e 4f 70 65 6e  atic int winOpen
fd790 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  (.  sqlite3_vfs 
fd7a0 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 2f 2a  *pVfs,        /*
fd7b0 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a 20 20 63   Not used */.  c
fd7c0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
fd7d0 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  ,        /* Name
fd7e0 20 6f 66 20 74 68 65 20 66 69 6c 65 20 28 55 54   of the file (UT
fd7f0 46 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65  F-8) */.  sqlite
fd800 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20  3_file *id,     
fd810 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65      /* Write the
fd820 20 53 51 4c 69 74 65 20 66 69 6c 65 20 68 61 6e   SQLite file han
fd830 64 6c 65 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e  dle here */.  in
fd840 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20  t flags,        
fd850 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20          /* Open 
fd860 6d 6f 64 65 20 66 6c 61 67 73 20 2a 2f 0a 20 20  mode flags */.  
fd870 69 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 20 20  int *pOutFlags  
fd880 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61            /* Sta
fd890 74 75 73 20 72 65 74 75 72 6e 20 66 6c 61 67 73  tus return flags
fd8a0 20 2a 2f 0a 29 7b 0a 20 20 48 41 4e 44 4c 45 20   */.){.  HANDLE 
fd8b0 68 3b 0a 20 20 44 57 4f 52 44 20 64 77 44 65 73  h;.  DWORD dwDes
fd8c0 69 72 65 64 41 63 63 65 73 73 3b 0a 20 20 44 57  iredAccess;.  DW
fd8d0 4f 52 44 20 64 77 53 68 61 72 65 4d 6f 64 65 3b  ORD dwShareMode;
fd8e0 0a 20 20 44 57 4f 52 44 20 64 77 43 72 65 61 74  .  DWORD dwCreat
fd8f0 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e 3b 0a  ionDisposition;.
fd900 20 20 44 57 4f 52 44 20 64 77 46 6c 61 67 73 41    DWORD dwFlagsA
fd910 6e 64 41 74 74 72 69 62 75 74 65 73 20 3d 20 30  ndAttributes = 0
fd920 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  ;.#if SQLITE_OS_
fd930 57 49 4e 43 45 0a 20 20 69 6e 74 20 69 73 54 65  WINCE.  int isTe
fd940 6d 70 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 20  mp = 0;.#endif. 
fd950 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20   winFile *pFile 
fd960 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a  = (winFile*)id;.
fd970 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74    void *zConvert
fd980 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ed;             
fd990 20 20 20 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 20      /* Filename 
fd9a0 69 6e 20 4f 53 20 65 6e 63 6f 64 69 6e 67 20 2a  in OS encoding *
fd9b0 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
fd9c0 7a 55 74 66 38 4e 61 6d 65 20 3d 20 7a 4e 61 6d  zUtf8Name = zNam
fd9d0 65 3b 20 20 20 20 2f 2a 20 46 69 6c 65 6e 61 6d  e;    /* Filenam
fd9e0 65 20 69 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64  e in UTF-8 encod
fd9f0 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20 7a 54  ing */.  char zT
fda00 6d 70 6e 61 6d 65 5b 4d 41 58 5f 50 41 54 48 2b  mpname[MAX_PATH+
fda10 31 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 75  1];        /* Bu
fda20 66 66 65 72 20 75 73 65 64 20 74 6f 20 63 72 65  ffer used to cre
fda30 61 74 65 20 74 65 6d 70 20 66 69 6c 65 6e 61 6d  ate temp filenam
fda40 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  e */..  assert( 
fda50 69 64 21 3d 30 20 29 3b 0a 20 20 55 4e 55 53 45  id!=0 );.  UNUSE
fda60 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73  D_PARAMETER(pVfs
fda70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
fda80 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
fda90 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
fdaa0 20 69 73 20 4e 55 4c 4c 2c 20 67 65 6e 65 72 61   is NULL, genera
fdab0 74 65 20 61 20 0a 20 20 2a 2a 20 74 65 6d 70 6f  te a .  ** tempo
fdac0 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65 20 74  rary file name t
fdad0 6f 20 75 73 65 20 0a 20 20 2a 2f 0a 20 20 69 66  o use .  */.  if
fdae0 28 20 21 7a 55 74 66 38 4e 61 6d 65 20 29 7b 0a  ( !zUtf8Name ){.
fdaf0 20 20 20 20 69 6e 74 20 72 63 20 3d 20 67 65 74      int rc = get
fdb00 54 65 6d 70 6e 61 6d 65 28 4d 41 58 5f 50 41 54  Tempname(MAX_PAT
fdb10 48 2b 31 2c 20 7a 54 6d 70 6e 61 6d 65 29 3b 0a  H+1, zTmpname);.
fdb20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
fdb30 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
fdb40 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
fdb50 20 20 20 20 7a 55 74 66 38 4e 61 6d 65 20 3d 20      zUtf8Name = 
fdb60 7a 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 20  zTmpname;.  }.. 
fdb70 20 2f 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20   /* Convert the 
fdb80 66 69 6c 65 6e 61 6d 65 20 74 6f 20 74 68 65 20  filename to the 
fdb90 73 79 73 74 65 6d 20 65 6e 63 6f 64 69 6e 67 2e  system encoding.
fdba0 20 2a 2f 0a 20 20 7a 43 6f 6e 76 65 72 74 65 64   */.  zConverted
fdbb0 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69   = convertUtf8Fi
fdbc0 6c 65 6e 61 6d 65 28 7a 55 74 66 38 4e 61 6d 65  lename(zUtf8Name
fdbd0 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76 65 72  );.  if( zConver
fdbe0 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  ted==0 ){.    re
fdbf0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
fdc00 4d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c  M;.  }..  if( fl
fdc10 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45  ags & SQLITE_OPE
fdc20 4e 5f 52 45 41 44 57 52 49 54 45 20 29 7b 0a 20  N_READWRITE ){. 
fdc30 20 20 20 64 77 44 65 73 69 72 65 64 41 63 63 65     dwDesiredAcce
fdc40 73 73 20 3d 20 47 45 4e 45 52 49 43 5f 52 45 41  ss = GENERIC_REA
fdc50 44 20 7c 20 47 45 4e 45 52 49 43 5f 57 52 49 54  D | GENERIC_WRIT
fdc60 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  E;.  }else{.    
fdc70 64 77 44 65 73 69 72 65 64 41 63 63 65 73 73 20  dwDesiredAccess 
fdc80 3d 20 47 45 4e 45 52 49 43 5f 52 45 41 44 3b 0a  = GENERIC_READ;.
fdc90 20 20 7d 0a 20 20 2f 2a 20 53 51 4c 49 54 45 5f    }.  /* SQLITE_
fdca0 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 69  OPEN_EXCLUSIVE i
fdcb0 73 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 73  s used to make s
fdcc0 75 72 65 20 74 68 61 74 20 61 20 6e 65 77 20 66  ure that a new f
fdcd0 69 6c 65 20 69 73 20 0a 20 20 2a 2a 20 63 72 65  ile is .  ** cre
fdce0 61 74 65 64 2e 20 53 51 4c 69 74 65 20 64 6f 65  ated. SQLite doe
fdcf0 73 6e 27 74 20 75 73 65 20 69 74 20 74 6f 20 69  sn't use it to i
fdd00 6e 64 69 63 61 74 65 20 22 65 78 63 6c 75 73 69  ndicate "exclusi
fdd10 76 65 20 61 63 63 65 73 73 22 20 0a 20 20 2a 2a  ve access" .  **
fdd20 20 61 73 20 69 74 20 69 73 20 75 73 75 61 6c 6c   as it is usuall
fdd30 79 20 75 6e 64 65 72 73 74 6f 6f 64 2e 0a 20 20  y understood..  
fdd40 2a 2f 0a 20 20 61 73 73 65 72 74 28 21 28 66 6c  */.  assert(!(fl
fdd50 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45  ags & SQLITE_OPE
fdd60 4e 5f 45 58 43 4c 55 53 49 56 45 29 20 7c 7c 20  N_EXCLUSIVE) || 
fdd70 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  (flags & SQLITE_
fdd80 4f 50 45 4e 5f 43 52 45 41 54 45 29 29 3b 0a 20  OPEN_CREATE));. 
fdd90 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c   if( flags & SQL
fdda0 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49  ITE_OPEN_EXCLUSI
fddb0 56 45 20 29 7b 0a 20 20 20 20 2f 2a 20 43 72 65  VE ){.    /* Cre
fddc0 61 74 65 73 20 61 20 6e 65 77 20 66 69 6c 65 2c  ates a new file,
fddd0 20 6f 6e 6c 79 20 69 66 20 69 74 20 64 6f 65 73   only if it does
fdde0 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69   not already exi
fddf0 73 74 2e 20 2a 2f 0a 20 20 20 20 2f 2a 20 49 66  st. */.    /* If
fde00 20 74 68 65 20 66 69 6c 65 20 65 78 69 73 74 73   the file exists
fde10 2c 20 69 74 20 66 61 69 6c 73 2e 20 2a 2f 0a 20  , it fails. */. 
fde20 20 20 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73     dwCreationDis
fde30 70 6f 73 69 74 69 6f 6e 20 3d 20 43 52 45 41 54  position = CREAT
fde40 45 5f 4e 45 57 3b 0a 20 20 7d 65 6c 73 65 20 69  E_NEW;.  }else i
fde50 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54  f( flags & SQLIT
fde60 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 29 7b  E_OPEN_CREATE ){
fde70 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 65 78 69  .    /* Open exi
fde80 73 74 69 6e 67 20 66 69 6c 65 2c 20 6f 72 20 63  sting file, or c
fde90 72 65 61 74 65 20 69 66 20 69 74 20 64 6f 65 73  reate if it does
fdea0 6e 27 74 20 65 78 69 73 74 20 2a 2f 0a 20 20 20  n't exist */.   
fdeb0 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70 6f   dwCreationDispo
fdec0 73 69 74 69 6f 6e 20 3d 20 4f 50 45 4e 5f 41 4c  sition = OPEN_AL
fded0 57 41 59 53 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  WAYS;.  }else{. 
fdee0 20 20 20 2f 2a 20 4f 70 65 6e 73 20 61 20 66 69     /* Opens a fi
fdef0 6c 65 2c 20 6f 6e 6c 79 20 69 66 20 69 74 20 65  le, only if it e
fdf00 78 69 73 74 73 2e 20 2a 2f 0a 20 20 20 20 64 77  xists. */.    dw
fdf10 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74  CreationDisposit
fdf20 69 6f 6e 20 3d 20 4f 50 45 4e 5f 45 58 49 53 54  ion = OPEN_EXIST
fdf30 49 4e 47 3b 0a 20 20 7d 0a 20 20 64 77 53 68 61  ING;.  }.  dwSha
fdf40 72 65 4d 6f 64 65 20 3d 20 46 49 4c 45 5f 53 48  reMode = FILE_SH
fdf50 41 52 45 5f 52 45 41 44 20 7c 20 46 49 4c 45 5f  ARE_READ | FILE_
fdf60 53 48 41 52 45 5f 57 52 49 54 45 3b 0a 20 20 69  SHARE_WRITE;.  i
fdf70 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54  f( flags & SQLIT
fdf80 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43  E_OPEN_DELETEONC
fdf90 4c 4f 53 45 20 29 7b 0a 23 69 66 20 53 51 4c 49  LOSE ){.#if SQLI
fdfa0 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 20 20  TE_OS_WINCE.    
fdfb0 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62  dwFlagsAndAttrib
fdfc0 75 74 65 73 20 3d 20 46 49 4c 45 5f 41 54 54 52  utes = FILE_ATTR
fdfd0 49 42 55 54 45 5f 48 49 44 44 45 4e 3b 0a 20 20  IBUTE_HIDDEN;.  
fdfe0 20 20 69 73 54 65 6d 70 20 3d 20 31 3b 0a 23 65    isTemp = 1;.#e
fdff0 6c 73 65 0a 20 20 20 20 64 77 46 6c 61 67 73 41  lse.    dwFlagsA
fe000 6e 64 41 74 74 72 69 62 75 74 65 73 20 3d 20 46  ndAttributes = F
fe010 49 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 54 45  ILE_ATTRIBUTE_TE
fe020 4d 50 4f 52 41 52 59 0a 20 20 20 20 20 20 20 20  MPORARY.        
fe030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe040 20 20 20 20 20 20 20 7c 20 46 49 4c 45 5f 41 54         | FILE_AT
fe050 54 52 49 42 55 54 45 5f 48 49 44 44 45 4e 0a 20  TRIBUTE_HIDDEN. 
fe060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20                | 
fe080 46 49 4c 45 5f 46 4c 41 47 5f 44 45 4c 45 54 45  FILE_FLAG_DELETE
fe090 5f 4f 4e 5f 43 4c 4f 53 45 3b 0a 23 65 6e 64 69  _ON_CLOSE;.#endi
fe0a0 66 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64  f.  }else{.    d
fe0b0 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75  wFlagsAndAttribu
fe0c0 74 65 73 20 3d 20 46 49 4c 45 5f 41 54 54 52 49  tes = FILE_ATTRI
fe0d0 42 55 54 45 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 7d  BUTE_NORMAL;.  }
fe0e0 0a 20 20 2f 2a 20 52 65 70 6f 72 74 73 20 66 72  .  /* Reports fr
fe0f0 6f 6d 20 74 68 65 20 69 6e 74 65 72 6e 65 74 20  om the internet 
fe100 61 72 65 20 74 68 61 74 20 70 65 72 66 6f 72 6d  are that perform
fe110 61 6e 63 65 20 69 73 20 61 6c 77 61 79 73 0a 20  ance is always. 
fe120 20 2a 2a 20 62 65 74 74 65 72 20 69 66 20 46 49   ** better if FI
fe130 4c 45 5f 46 4c 41 47 5f 52 41 4e 44 4f 4d 5f 41  LE_FLAG_RANDOM_A
fe140 43 43 45 53 53 20 69 73 20 75 73 65 64 2e 20 20  CCESS is used.  
fe150 54 69 63 6b 65 74 20 23 32 36 39 39 2e 20 2a 2f  Ticket #2699. */
fe160 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  .#if SQLITE_OS_W
fe170 49 4e 43 45 0a 20 20 64 77 46 6c 61 67 73 41 6e  INCE.  dwFlagsAn
fe180 64 41 74 74 72 69 62 75 74 65 73 20 7c 3d 20 46  dAttributes |= F
fe190 49 4c 45 5f 46 4c 41 47 5f 52 41 4e 44 4f 4d 5f  ILE_FLAG_RANDOM_
fe1a0 41 43 43 45 53 53 3b 0a 23 65 6e 64 69 66 0a 20  ACCESS;.#endif. 
fe1b0 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20   if( isNT() ){. 
fe1c0 20 20 20 68 20 3d 20 43 72 65 61 74 65 46 69 6c     h = CreateFil
fe1d0 65 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76  eW((WCHAR*)zConv
fe1e0 65 72 74 65 64 2c 0a 20 20 20 20 20 20 20 64 77  erted,.       dw
fe1f0 44 65 73 69 72 65 64 41 63 63 65 73 73 2c 0a 20  DesiredAccess,. 
fe200 20 20 20 20 20 20 64 77 53 68 61 72 65 4d 6f 64        dwShareMod
fe210 65 2c 0a 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a  e,.       NULL,.
fe220 20 20 20 20 20 20 20 64 77 43 72 65 61 74 69 6f         dwCreatio
fe230 6e 44 69 73 70 6f 73 69 74 69 6f 6e 2c 0a 20 20  nDisposition,.  
fe240 20 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41       dwFlagsAndA
fe250 74 74 72 69 62 75 74 65 73 2c 0a 20 20 20 20 20  ttributes,.     
fe260 20 20 4e 55 4c 4c 0a 20 20 20 20 29 3b 0a 2f 2a    NULL.    );./*
fe270 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20   isNT() is 1 if 
fe280 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d  SQLITE_OS_WINCE=
fe290 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65  =1, so this else
fe2a0 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74   is never execut
fe2b0 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68  ed. .** Since th
fe2c0 65 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20  e ASCII version 
fe2d0 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73  of these Windows
fe2e0 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73   API do not exis
fe2f0 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20  t for WINCE,.** 
fe300 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74  it's important t
fe310 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20  o not reference 
fe320 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62  them for WINCE b
fe330 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51  uilds..*/.#if SQ
fe340 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30  LITE_OS_WINCE==0
fe350 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 20  .  }else{.    h 
fe360 3d 20 43 72 65 61 74 65 46 69 6c 65 41 28 28 63  = CreateFileA((c
fe370 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c  har*)zConverted,
fe380 0a 20 20 20 20 20 20 20 64 77 44 65 73 69 72 65  .       dwDesire
fe390 64 41 63 63 65 73 73 2c 0a 20 20 20 20 20 20 20  dAccess,.       
fe3a0 64 77 53 68 61 72 65 4d 6f 64 65 2c 0a 20 20 20  dwShareMode,.   
fe3b0 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20      NULL,.      
fe3c0 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70 6f   dwCreationDispo
fe3d0 73 69 74 69 6f 6e 2c 0a 20 20 20 20 20 20 20 64  sition,.       d
fe3e0 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75  wFlagsAndAttribu
fe3f0 74 65 73 2c 0a 20 20 20 20 20 20 20 4e 55 4c 4c  tes,.       NULL
fe400 0a 20 20 20 20 29 3b 0a 23 65 6e 64 69 66 0a 20  .    );.#endif. 
fe410 20 7d 0a 20 20 69 66 28 20 68 3d 3d 49 4e 56 41   }.  if( h==INVA
fe420 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45  LID_HANDLE_VALUE
fe430 20 29 7b 0a 20 20 20 20 66 72 65 65 28 7a 43 6f   ){.    free(zCo
fe440 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 69 66  nverted);.    if
fe450 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ( flags & SQLITE
fe460 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20  _OPEN_READWRITE 
fe470 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
fe480 77 69 6e 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4e  winOpen(pVfs, zN
fe490 61 6d 65 2c 20 69 64 2c 20 0a 20 20 20 20 20 20  ame, id, .      
fe4a0 20 20 20 20 20 20 20 28 28 66 6c 61 67 73 7c 53         ((flags|S
fe4b0 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f  QLITE_OPEN_READO
fe4c0 4e 4c 59 29 26 7e 53 51 4c 49 54 45 5f 4f 50 45  NLY)&~SQLITE_OPE
fe4d0 4e 5f 52 45 41 44 57 52 49 54 45 29 2c 20 70 4f  N_READWRITE), pO
fe4e0 75 74 46 6c 61 67 73 29 3b 0a 20 20 20 20 7d 65  utFlags);.    }e
fe4f0 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72  lse{.      retur
fe500 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45  n SQLITE_CANTOPE
fe510 4e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  N;.    }.  }.  i
fe520 66 28 20 70 4f 75 74 46 6c 61 67 73 20 29 7b 0a  f( pOutFlags ){.
fe530 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 20      if( flags & 
fe540 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
fe550 57 52 49 54 45 20 29 7b 0a 20 20 20 20 20 20 2a  WRITE ){.      *
fe560 70 4f 75 74 46 6c 61 67 73 20 3d 20 53 51 4c 49  pOutFlags = SQLI
fe570 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54  TE_OPEN_READWRIT
fe580 45 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  E;.    }else{.  
fe590 20 20 20 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d      *pOutFlags =
fe5a0 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41   SQLITE_OPEN_REA
fe5b0 44 4f 4e 4c 59 3b 0a 20 20 20 20 7d 0a 20 20 7d  DONLY;.    }.  }
fe5c0 0a 20 20 6d 65 6d 73 65 74 28 70 46 69 6c 65 2c  .  memset(pFile,
fe5d0 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 46 69 6c   0, sizeof(*pFil
fe5e0 65 29 29 3b 0a 20 20 70 46 69 6c 65 2d 3e 70 4d  e));.  pFile->pM
fe5f0 65 74 68 6f 64 20 3d 20 26 77 69 6e 49 6f 4d 65  ethod = &winIoMe
fe600 74 68 6f 64 3b 0a 20 20 70 46 69 6c 65 2d 3e 68  thod;.  pFile->h
fe610 20 3d 20 68 3b 0a 20 20 70 46 69 6c 65 2d 3e 6c   = h;.  pFile->l
fe620 61 73 74 45 72 72 6e 6f 20 3d 20 4e 4f 5f 45 52  astErrno = NO_ER
fe630 52 4f 52 3b 0a 20 20 70 46 69 6c 65 2d 3e 73 65  ROR;.  pFile->se
fe640 63 74 6f 72 53 69 7a 65 20 3d 20 67 65 74 53 65  ctorSize = getSe
fe650 63 74 6f 72 53 69 7a 65 28 70 56 66 73 2c 20 7a  ctorSize(pVfs, z
fe660 55 74 66 38 4e 61 6d 65 29 3b 0a 23 69 66 20 53  Utf8Name);.#if S
fe670 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20  QLITE_OS_WINCE. 
fe680 20 69 66 28 20 28 66 6c 61 67 73 20 26 20 28 53   if( (flags & (S
fe690 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
fe6a0 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e  RITE|SQLITE_OPEN
fe6b0 5f 4d 41 49 4e 5f 44 42 29 29 20 3d 3d 0a 20 20  _MAIN_DB)) ==.  
fe6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51               (SQ
fe6d0 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
fe6e0 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  ITE|SQLITE_OPEN_
fe6f0 4d 41 49 4e 5f 44 42 29 0a 20 20 20 20 20 20 20  MAIN_DB).       
fe700 26 26 20 21 77 69 6e 63 65 43 72 65 61 74 65 4c  && !winceCreateL
fe710 6f 63 6b 28 7a 4e 61 6d 65 2c 20 70 46 69 6c 65  ock(zName, pFile
fe720 29 0a 20 20 29 7b 0a 20 20 20 20 43 6c 6f 73 65  ).  ){.    Close
fe730 48 61 6e 64 6c 65 28 68 29 3b 0a 20 20 20 20 66  Handle(h);.    f
fe740 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b  ree(zConverted);
fe750 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
fe760 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d  TE_CANTOPEN;.  }
fe770 0a 20 20 69 66 28 20 69 73 54 65 6d 70 20 29 7b  .  if( isTemp ){
fe780 0a 20 20 20 20 70 46 69 6c 65 2d 3e 7a 44 65 6c  .    pFile->zDel
fe790 65 74 65 4f 6e 43 6c 6f 73 65 20 3d 20 7a 43 6f  eteOnClose = zCo
fe7a0 6e 76 65 72 74 65 64 3b 0a 20 20 7d 65 6c 73 65  nverted;.  }else
fe7b0 0a 23 65 6e 64 69 66 0a 20 20 7b 0a 20 20 20 20  .#endif.  {.    
fe7c0 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29  free(zConverted)
fe7d0 3b 0a 20 20 7d 0a 20 20 4f 70 65 6e 43 6f 75 6e  ;.  }.  OpenCoun
fe7e0 74 65 72 28 2b 31 29 3b 0a 20 20 72 65 74 75 72  ter(+1);.  retur
fe7f0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
fe800 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65  /*.** Delete the
fe810 20 6e 61 6d 65 64 20 66 69 6c 65 2e 0a 2a 2a 0a   named file..**.
fe820 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 69 6e  ** Note that win
fe830 64 6f 77 73 20 64 6f 65 73 20 6e 6f 74 20 61 6c  dows does not al
fe840 6c 6f 77 20 61 20 66 69 6c 65 20 74 6f 20 62 65  low a file to be
fe850 20 64 65 6c 65 74 65 64 20 69 66 20 73 6f 6d 65   deleted if some
fe860 20 6f 74 68 65 72 0a 2a 2a 20 70 72 6f 63 65 73   other.** proces
fe870 73 20 68 61 73 20 69 74 20 6f 70 65 6e 2e 20 20  s has it open.  
fe880 53 6f 6d 65 74 69 6d 65 73 20 61 20 76 69 72 75  Sometimes a viru
fe890 73 20 73 63 61 6e 6e 65 72 20 6f 72 20 69 6e 64  s scanner or ind
fe8a0 65 78 69 6e 67 20 70 72 6f 67 72 61 6d 0a 2a 2a  exing program.**
fe8b0 20 77 69 6c 6c 20 6f 70 65 6e 20 61 20 6a 6f 75   will open a jou
fe8c0 72 6e 61 6c 20 66 69 6c 65 20 73 68 6f 72 74 6c  rnal file shortl
fe8d0 79 20 61 66 74 65 72 20 69 74 20 69 73 20 63 72  y after it is cr
fe8e0 65 61 74 65 64 20 69 6e 20 6f 72 64 65 72 20 74  eated in order t
fe8f0 6f 20 64 6f 0a 2a 2a 20 77 68 61 74 65 76 65 72  o do.** whatever
fe900 20 69 74 20 64 6f 65 73 2e 20 20 57 68 69 6c 65   it does.  While
fe910 20 74 68 69 73 20 6f 74 68 65 72 20 70 72 6f 63   this other proc
fe920 65 73 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 74  ess is holding t
fe930 68 65 0a 2a 2a 20 66 69 6c 65 20 6f 70 65 6e 2c  he.** file open,
fe940 20 77 65 20 77 69 6c 6c 20 62 65 20 75 6e 61 62   we will be unab
fe950 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 69 74 2e  le to delete it.
fe960 20 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64    To work around
fe970 20 74 68 69 73 0a 2a 2a 20 70 72 6f 62 6c 65 6d   this.** problem
fe980 2c 20 77 65 20 64 65 6c 61 79 20 31 30 30 20 6d  , we delay 100 m
fe990 69 6c 6c 69 73 65 63 6f 6e 64 73 20 61 6e 64 20  illiseconds and 
fe9a0 74 72 79 20 74 6f 20 64 65 6c 65 74 65 20 61 67  try to delete ag
fe9b0 61 69 6e 2e 20 20 55 70 0a 2a 2a 20 74 6f 20 4d  ain.  Up.** to M
fe9c0 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d  X_DELETION_ATTEM
fe9d0 50 54 73 20 64 65 6c 65 74 69 6f 6e 20 61 74 74  PTs deletion att
fe9e0 65 6d 70 74 73 20 61 72 65 20 72 75 6e 20 62 65  empts are run be
fe9f0 66 6f 72 65 20 67 69 76 69 6e 67 0a 2a 2a 20 75  fore giving.** u
fea00 70 20 61 6e 64 20 72 65 74 75 72 6e 69 6e 67 20  p and returning 
fea10 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 23 64 65  an error..*/.#de
fea20 66 69 6e 65 20 4d 58 5f 44 45 4c 45 54 49 4f 4e  fine MX_DELETION
fea30 5f 41 54 54 45 4d 50 54 53 20 35 0a 73 74 61 74  _ATTEMPTS 5.stat
fea40 69 63 20 69 6e 74 20 77 69 6e 44 65 6c 65 74 65  ic int winDelete
fea50 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  (.  sqlite3_vfs 
fea60 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20  *pVfs,          
fea70 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 77  /* Not used on w
fea80 69 6e 33 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  in32 */.  const 
fea90 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c  char *zFilename,
feaa0 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
feab0 20 66 69 6c 65 20 74 6f 20 64 65 6c 65 74 65 20   file to delete 
feac0 2a 2f 0a 20 20 69 6e 74 20 73 79 6e 63 44 69 72  */.  int syncDir
fead0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
feae0 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20   /* Not used on 
feaf0 77 69 6e 33 32 20 2a 2f 0a 29 7b 0a 20 20 69 6e  win32 */.){.  in
feb00 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 44 57 4f  t cnt = 0;.  DWO
feb10 52 44 20 72 63 3b 0a 20 20 44 57 4f 52 44 20 65  RD rc;.  DWORD e
feb20 72 72 6f 72 20 3d 20 30 3b 0a 20 20 76 6f 69 64  rror = 0;.  void
feb30 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63   *zConverted = c
feb40 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61  onvertUtf8Filena
feb50 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  me(zFilename);. 
feb60 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
feb70 52 28 70 56 66 73 29 3b 0a 20 20 55 4e 55 53 45  R(pVfs);.  UNUSE
feb80 44 5f 50 41 52 41 4d 45 54 45 52 28 73 79 6e 63  D_PARAMETER(sync
feb90 44 69 72 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e  Dir);.  if( zCon
feba0 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20  verted==0 ){.   
febb0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
febc0 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 53 69 6d 75  OMEM;.  }.  Simu
febd0 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75  lateIOError(retu
febe0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
febf0 44 45 4c 45 54 45 29 3b 0a 20 20 69 66 28 20 69  DELETE);.  if( i
fec00 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 64 6f 7b  sNT() ){.    do{
fec10 0a 20 20 20 20 20 20 44 65 6c 65 74 65 46 69 6c  .      DeleteFil
fec20 65 57 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a  eW(zConverted);.
fec30 20 20 20 20 7d 77 68 69 6c 65 28 20 20 20 28 20      }while(   ( 
fec40 20 20 28 28 72 63 20 3d 20 47 65 74 46 69 6c 65    ((rc = GetFile
fec50 41 74 74 72 69 62 75 74 65 73 57 28 7a 43 6f 6e  AttributesW(zCon
fec60 76 65 72 74 65 64 29 29 20 21 3d 20 49 4e 56 41  verted)) != INVA
fec70 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55  LID_FILE_ATTRIBU
fec80 54 45 53 29 0a 20 20 20 20 20 20 20 20 20 20 20  TES).           
fec90 20 20 20 20 7c 7c 20 28 28 65 72 72 6f 72 20 3d      || ((error =
feca0 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29   GetLastError())
fecb0 20 3d 3d 20 45 52 52 4f 52 5f 41 43 43 45 53 53   == ERROR_ACCESS
fecc0 5f 44 45 4e 49 45 44 29 29 0a 20 20 20 20 20 20  _DENIED)).      
fecd0 20 20 20 20 20 26 26 20 28 2b 2b 63 6e 74 20 3c       && (++cnt <
fece0 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54   MX_DELETION_ATT
fecf0 45 4d 50 54 53 29 0a 20 20 20 20 20 20 20 20 20  EMPTS).         
fed00 20 20 26 26 20 28 53 6c 65 65 70 28 31 30 30 29    && (Sleep(100)
fed10 2c 20 31 29 20 29 3b 0a 2f 2a 20 69 73 4e 54 28  , 1) );./* isNT(
fed20 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45  ) is 1 if SQLITE
fed30 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f  _OS_WINCE==1, so
fed40 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65   this else is ne
fed50 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a  ver executed. .*
fed60 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53 43 49  * Since the ASCI
fed70 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65  I version of the
fed80 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64  se Windows API d
fed90 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20  o not exist for 
feda0 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69  WINCE,.** it's i
fedb0 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20  mportant to not 
fedc0 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66  reference them f
fedd0 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e  or WINCE builds.
fede0 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f  .*/.#if SQLITE_O
fedf0 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c  S_WINCE==0.  }el
fee00 73 65 7b 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20  se{.    do{.    
fee10 20 20 44 65 6c 65 74 65 46 69 6c 65 41 28 7a 43    DeleteFileA(zC
fee20 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 7d  onverted);.    }
fee30 77 68 69 6c 65 28 20 20 20 28 20 20 20 28 28 72  while(   (   ((r
fee40 63 20 3d 20 47 65 74 46 69 6c 65 41 74 74 72 69  c = GetFileAttri
fee50 62 75 74 65 73 41 28 7a 43 6f 6e 76 65 72 74 65  butesA(zConverte
fee60 64 29 29 20 21 3d 20 49 4e 56 41 4c 49 44 5f 46  d)) != INVALID_F
fee70 49 4c 45 5f 41 54 54 52 49 42 55 54 45 53 29 0a  ILE_ATTRIBUTES).
fee80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c                 |
fee90 7c 20 28 28 65 72 72 6f 72 20 3d 20 47 65 74 4c  | ((error = GetL
feea0 61 73 74 45 72 72 6f 72 28 29 29 20 3d 3d 20 45  astError()) == E
feeb0 52 52 4f 52 5f 41 43 43 45 53 53 5f 44 45 4e 49  RROR_ACCESS_DENI
feec0 45 44 29 29 0a 20 20 20 20 20 20 20 20 20 20 20  ED)).           
feed0 26 26 20 28 2b 2b 63 6e 74 20 3c 20 4d 58 5f 44  && (++cnt < MX_D
feee0 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53  ELETION_ATTEMPTS
feef0 29 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20  ).           && 
fef00 28 53 6c 65 65 70 28 31 30 30 29 2c 20 31 29 20  (Sleep(100), 1) 
fef10 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20  );.#endif.  }.  
fef20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29  free(zConverted)
fef30 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 22 44 45  ;.  OSTRACE2("DE
fef40 4c 45 54 45 20 5c 22 25 73 5c 22 5c 6e 22 2c 20  LETE \"%s\"\n", 
fef50 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 72 65  zFilename);.  re
fef60 74 75 72 6e 20 28 20 20 20 28 72 63 20 3d 3d 20  turn (   (rc == 
fef70 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54  INVALID_FILE_ATT
fef80 52 49 42 55 54 45 53 29 20 0a 20 20 20 20 20 20  RIBUTES) .      
fef90 20 20 20 20 26 26 20 28 65 72 72 6f 72 20 3d 3d      && (error ==
fefa0 20 45 52 52 4f 52 5f 46 49 4c 45 5f 4e 4f 54 5f   ERROR_FILE_NOT_
fefb0 46 4f 55 4e 44 29 29 20 3f 20 53 51 4c 49 54 45  FOUND)) ? SQLITE
fefc0 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45  _OK : SQLITE_IOE
fefd0 52 52 5f 44 45 4c 45 54 45 3b 0a 7d 0a 0a 2f 2a  RR_DELETE;.}../*
fefe0 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 65 78  .** Check the ex
feff0 69 73 74 61 6e 63 65 20 61 6e 64 20 73 74 61 74  istance and stat
ff000 75 73 20 6f 66 20 61 20 66 69 6c 65 2e 0a 2a 2f  us of a file..*/
ff010 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 41  .static int winA
ff020 63 63 65 73 73 28 0a 20 20 73 71 6c 69 74 65 33  ccess(.  sqlite3
ff030 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20  _vfs *pVfs,     
ff040 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20      /* Not used 
ff050 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 20 20 63 6f  on win32 */.  co
ff060 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  nst char *zFilen
ff070 61 6d 65 2c 20 20 20 20 20 2f 2a 20 4e 61 6d 65  ame,     /* Name
ff080 20 6f 66 20 66 69 6c 65 20 74 6f 20 63 68 65 63   of file to chec
ff090 6b 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73  k */.  int flags
ff0a0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
ff0b0 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 74 65 73    /* Type of tes
ff0c0 74 20 74 6f 20 6d 61 6b 65 20 6f 6e 20 74 68 69  t to make on thi
ff0d0 73 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20  s file */.  int 
ff0e0 2a 70 52 65 73 4f 75 74 20 20 20 20 20 20 20 20  *pResOut        
ff0f0 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 52         /* OUT: R
ff100 65 73 75 6c 74 20 2a 2f 0a 29 7b 0a 20 20 44 57  esult */.){.  DW
ff110 4f 52 44 20 61 74 74 72 3b 0a 20 20 69 6e 74 20  ORD attr;.  int 
ff120 72 63 20 3d 20 30 3b 0a 20 20 76 6f 69 64 20 2a  rc = 0;.  void *
ff130 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e  zConverted = con
ff140 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65  vertUtf8Filename
ff150 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 55  (zFilename);.  U
ff160 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
ff170 70 56 66 73 29 3b 0a 20 20 69 66 28 20 7a 43 6f  pVfs);.  if( zCo
ff180 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20  nverted==0 ){.  
ff190 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
ff1a0 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28  NOMEM;.  }.  if(
ff1b0 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 61   isNT() ){.    a
ff1c0 74 74 72 20 3d 20 47 65 74 46 69 6c 65 41 74 74  ttr = GetFileAtt
ff1d0 72 69 62 75 74 65 73 57 28 28 57 43 48 41 52 2a  ributesW((WCHAR*
ff1e0 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 2f 2a  )zConverted);./*
ff1f0 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20   isNT() is 1 if 
ff200 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d  SQLITE_OS_WINCE=
ff210 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65  =1, so this else
ff220 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74   is never execut
ff230 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68  ed. .** Since th
ff240 65 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20  e ASCII version 
ff250 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73  of these Windows
ff260 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73   API do not exis
ff270 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20  t for WINCE,.** 
ff280 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74  it's important t
ff290 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20  o not reference 
ff2a0 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62  them for WINCE b
ff2b0 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51  uilds..*/.#if SQ
ff2c0 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30  LITE_OS_WINCE==0
ff2d0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 74  .  }else{.    at
ff2e0 74 72 20 3d 20 47 65 74 46 69 6c 65 41 74 74 72  tr = GetFileAttr
ff2f0 69 62 75 74 65 73 41 28 28 63 68 61 72 2a 29 7a  ibutesA((char*)z
ff300 43 6f 6e 76 65 72 74 65 64 29 3b 0a 23 65 6e 64  Converted);.#end
ff310 69 66 0a 20 20 7d 0a 20 20 66 72 65 65 28 7a 43  if.  }.  free(zC
ff320 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 73 77 69  onverted);.  swi
ff330 74 63 68 28 20 66 6c 61 67 73 20 29 7b 0a 20 20  tch( flags ){.  
ff340 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43    case SQLITE_AC
ff350 43 45 53 53 5f 52 45 41 44 3a 0a 20 20 20 20 63  CESS_READ:.    c
ff360 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53  ase SQLITE_ACCES
ff370 53 5f 45 58 49 53 54 53 3a 0a 20 20 20 20 20 20  S_EXISTS:.      
ff380 72 63 20 3d 20 61 74 74 72 21 3d 49 4e 56 41 4c  rc = attr!=INVAL
ff390 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54  ID_FILE_ATTRIBUT
ff3a0 45 53 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ES;.      break;
ff3b0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
ff3c0 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54  _ACCESS_READWRIT
ff3d0 45 3a 0a 20 20 20 20 20 20 72 63 20 3d 20 28 61  E:.      rc = (a
ff3e0 74 74 72 20 26 20 46 49 4c 45 5f 41 54 54 52 49  ttr & FILE_ATTRI
ff3f0 42 55 54 45 5f 52 45 41 44 4f 4e 4c 59 29 3d 3d  BUTE_READONLY)==
ff400 30 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  0;.      break;.
ff410 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20      default:.   
ff420 20 20 20 61 73 73 65 72 74 28 21 22 49 6e 76 61     assert(!"Inva
ff430 6c 69 64 20 66 6c 61 67 73 20 61 72 67 75 6d 65  lid flags argume
ff440 6e 74 22 29 3b 0a 20 20 7d 0a 20 20 2a 70 52 65  nt");.  }.  *pRe
ff450 73 4f 75 74 20 3d 20 72 63 3b 0a 20 20 72 65 74  sOut = rc;.  ret
ff460 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
ff470 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 61 20  .../*.** Turn a 
ff480 72 65 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d  relative pathnam
ff490 65 20 69 6e 74 6f 20 61 20 66 75 6c 6c 20 70 61  e into a full pa
ff4a0 74 68 6e 61 6d 65 2e 20 20 57 72 69 74 65 20 74  thname.  Write t
ff4b0 68 65 20 66 75 6c 6c 0a 2a 2a 20 70 61 74 68 6e  he full.** pathn
ff4c0 61 6d 65 20 69 6e 74 6f 20 7a 4f 75 74 5b 5d 2e  ame into zOut[].
ff4d0 20 20 7a 4f 75 74 5b 5d 20 77 69 6c 6c 20 62 65    zOut[] will be
ff4e0 20 61 74 20 6c 65 61 73 74 20 70 56 66 73 2d 3e   at least pVfs->
ff4f0 6d 78 50 61 74 68 6e 61 6d 65 0a 2a 2a 20 62 79  mxPathname.** by
ff500 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a  tes in size..*/.
ff510 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 46 75  static int winFu
ff520 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 71  llPathname(.  sq
ff530 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
ff540 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
ff550 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 20 6f 62  ointer to vfs ob
ff560 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ject */.  const 
ff570 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76 65 2c  char *zRelative,
ff580 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 69          /* Possi
ff590 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69 6e 70  bly relative inp
ff5a0 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69 6e 74  ut path */.  int
ff5b0 20 6e 46 75 6c 6c 2c 20 20 20 20 20 20 20 20 20   nFull,         
ff5c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
ff5d0 7a 65 20 6f 66 20 6f 75 74 70 75 74 20 62 75 66  ze of output buf
ff5e0 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  fer in bytes */.
ff5f0 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c 20 20 20    char *zFull   
ff600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff610 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 66 65 72  /* Output buffer
ff620 20 2a 2f 0a 29 7b 0a 20 20 0a 23 69 66 20 64 65   */.){.  .#if de
ff630 66 69 6e 65 64 28 5f 5f 43 59 47 57 49 4e 5f 5f  fined(__CYGWIN__
ff640 29 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ).  UNUSED_PARAM
ff650 45 54 45 52 28 6e 46 75 6c 6c 29 3b 0a 20 20 63  ETER(nFull);.  c
ff660 79 67 77 69 6e 5f 63 6f 6e 76 5f 74 6f 5f 66 75  ygwin_conv_to_fu
ff670 6c 6c 5f 77 69 6e 33 32 5f 70 61 74 68 28 7a 52  ll_win32_path(zR
ff680 65 6c 61 74 69 76 65 2c 20 7a 46 75 6c 6c 29 3b  elative, zFull);
ff690 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
ff6a0 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66  _OK;.#endif..#if
ff6b0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
ff6c0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
ff6d0 54 45 52 28 6e 46 75 6c 6c 29 3b 0a 20 20 2f 2a  TER(nFull);.  /*
ff6e0 20 57 69 6e 43 45 20 68 61 73 20 6e 6f 20 63 6f   WinCE has no co
ff6f0 6e 63 65 70 74 20 6f 66 20 61 20 72 65 6c 61 74  ncept of a relat
ff700 69 76 65 20 70 61 74 68 6e 61 6d 65 2c 20 6f 72  ive pathname, or
ff710 20 73 6f 20 49 20 61 6d 20 74 6f 6c 64 2e 20 2a   so I am told. *
ff720 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72  /.  sqlite3_snpr
ff730 69 6e 74 66 28 70 56 66 73 2d 3e 6d 78 50 61 74  intf(pVfs->mxPat
ff740 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c 2c 20 22 25  hname, zFull, "%
ff750 73 22 2c 20 7a 52 65 6c 61 74 69 76 65 29 3b 0a  s", zRelative);.
ff760 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
ff770 4f 4b 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20  OK;.#endif..#if 
ff780 21 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45  !SQLITE_OS_WINCE
ff790 20 26 26 20 21 64 65 66 69 6e 65 64 28 5f 5f 43   && !defined(__C
ff7a0 59 47 57 49 4e 5f 5f 29 0a 20 20 69 6e 74 20 6e  YGWIN__).  int n
ff7b0 42 79 74 65 3b 0a 20 20 76 6f 69 64 20 2a 7a 43  Byte;.  void *zC
ff7c0 6f 6e 76 65 72 74 65 64 3b 0a 20 20 63 68 61 72  onverted;.  char
ff7d0 20 2a 7a 4f 75 74 3b 0a 20 20 55 4e 55 53 45 44   *zOut;.  UNUSED
ff7e0 5f 50 41 52 41 4d 45 54 45 52 28 6e 46 75 6c 6c  _PARAMETER(nFull
ff7f0 29 3b 0a 20 20 7a 43 6f 6e 76 65 72 74 65 64 20  );.  zConverted 
ff800 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c  = convertUtf8Fil
ff810 65 6e 61 6d 65 28 7a 52 65 6c 61 74 69 76 65 29  ename(zRelative)
ff820 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29  ;.  if( isNT() )
ff830 7b 0a 20 20 20 20 57 43 48 41 52 20 2a 7a 54 65  {.    WCHAR *zTe
ff840 6d 70 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20  mp;.    nByte = 
ff850 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 57  GetFullPathNameW
ff860 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72  ((WCHAR*)zConver
ff870 74 65 64 2c 20 30 2c 20 30 2c 20 30 29 20 2b 20  ted, 0, 0, 0) + 
ff880 33 3b 0a 20 20 20 20 7a 54 65 6d 70 20 3d 20 6d  3;.    zTemp = m
ff890 61 6c 6c 6f 63 28 20 6e 42 79 74 65 2a 73 69 7a  alloc( nByte*siz
ff8a0 65 6f 66 28 7a 54 65 6d 70 5b 30 5d 29 20 29 3b  eof(zTemp[0]) );
ff8b0 0a 20 20 20 20 69 66 28 20 7a 54 65 6d 70 3d 3d  .    if( zTemp==
ff8c0 30 20 29 7b 0a 20 20 20 20 20 20 66 72 65 65 28  0 ){.      free(
ff8d0 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20  zConverted);.   
ff8e0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
ff8f0 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20  _NOMEM;.    }.  
ff900 20 20 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d    GetFullPathNam
ff910 65 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76  eW((WCHAR*)zConv
ff920 65 72 74 65 64 2c 20 6e 42 79 74 65 2c 20 7a 54  erted, nByte, zT
ff930 65 6d 70 2c 20 30 29 3b 0a 20 20 20 20 66 72 65  emp, 0);.    fre
ff940 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20  e(zConverted);. 
ff950 20 20 20 7a 4f 75 74 20 3d 20 75 6e 69 63 6f 64     zOut = unicod
ff960 65 54 6f 55 74 66 38 28 7a 54 65 6d 70 29 3b 0a  eToUtf8(zTemp);.
ff970 20 20 20 20 66 72 65 65 28 7a 54 65 6d 70 29 3b      free(zTemp);
ff980 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20  ./* isNT() is 1 
ff990 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
ff9a0 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65  CE==1, so this e
ff9b0 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65  lse is never exe
ff9c0 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65  cuted. .** Since
ff9d0 20 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69   the ASCII versi
ff9e0 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64  on of these Wind
ff9f0 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65  ows API do not e
ffa00 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a  xist for WINCE,.
ffa10 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e  ** it's importan
ffa20 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e  t to not referen
ffa30 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43  ce them for WINC
ffa40 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66  E builds..*/.#if
ffa50 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
ffa60 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ==0.  }else{.   
ffa70 20 63 68 61 72 20 2a 7a 54 65 6d 70 3b 0a 20 20   char *zTemp;.  
ffa80 20 20 6e 42 79 74 65 20 3d 20 47 65 74 46 75 6c    nByte = GetFul
ffa90 6c 50 61 74 68 4e 61 6d 65 41 28 28 63 68 61 72  lPathNameA((char
ffaa0 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20 30 2c  *)zConverted, 0,
ffab0 20 30 2c 20 30 29 20 2b 20 33 3b 0a 20 20 20 20   0, 0) + 3;.    
ffac0 7a 54 65 6d 70 20 3d 20 6d 61 6c 6c 6f 63 28 20  zTemp = malloc( 
ffad0 6e 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a 54 65  nByte*sizeof(zTe
ffae0 6d 70 5b 30 5d 29 20 29 3b 0a 20 20 20 20 69 66  mp[0]) );.    if
ffaf0 28 20 7a 54 65 6d 70 3d 3d 30 20 29 7b 0a 20 20  ( zTemp==0 ){.  
ffb00 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72      free(zConver
ffb10 74 65 64 29 3b 0a 20 20 20 20 20 20 72 65 74 75  ted);.      retu
ffb20 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
ffb30 0a 20 20 20 20 7d 0a 20 20 20 20 47 65 74 46 75  .    }.    GetFu
ffb40 6c 6c 50 61 74 68 4e 61 6d 65 41 28 28 63 68 61  llPathNameA((cha
ffb50 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20 6e  r*)zConverted, n
ffb60 42 79 74 65 2c 20 7a 54 65 6d 70 2c 20 30 29 3b  Byte, zTemp, 0);
ffb70 0a 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65  .    free(zConve
ffb80 72 74 65 64 29 3b 0a 20 20 20 20 7a 4f 75 74 20  rted);.    zOut 
ffb90 3d 20 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f  = sqlite3_win32_
ffba0 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28 7a 54 65  mbcs_to_utf8(zTe
ffbb0 6d 70 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 54  mp);.    free(zT
ffbc0 65 6d 70 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d  emp);.#endif.  }
ffbd0 0a 20 20 69 66 28 20 7a 4f 75 74 20 29 7b 0a 20  .  if( zOut ){. 
ffbe0 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
ffbf0 6e 74 66 28 70 56 66 73 2d 3e 6d 78 50 61 74 68  ntf(pVfs->mxPath
ffc00 6e 61 6d 65 2c 20 7a 46 75 6c 6c 2c 20 22 25 73  name, zFull, "%s
ffc10 22 2c 20 7a 4f 75 74 29 3b 0a 20 20 20 20 66 72  ", zOut);.    fr
ffc20 65 65 28 7a 4f 75 74 29 3b 0a 20 20 20 20 72 65  ee(zOut);.    re
ffc30 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
ffc40 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
ffc50 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
ffc60 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a  ;.  }.#endif.}..
ffc70 2f 2a 0a 2a 2a 20 47 65 74 20 74 68 65 20 73 65  /*.** Get the se
ffc80 63 74 6f 72 20 73 69 7a 65 20 6f 66 20 74 68 65  ctor size of the
ffc90 20 64 65 76 69 63 65 20 75 73 65 64 20 74 6f 20   device used to 
ffca0 73 74 6f 72 65 0a 2a 2a 20 66 69 6c 65 2e 0a 2a  store.** file..*
ffcb0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74  /.static int get
ffcc0 53 65 63 74 6f 72 53 69 7a 65 28 0a 20 20 20 20  SectorSize(.    
ffcd0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
ffce0 73 2c 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  s,.    const cha
ffcf0 72 20 2a 7a 52 65 6c 61 74 69 76 65 20 20 20 20  r *zRelative    
ffd00 20 2f 2a 20 55 54 46 2d 38 20 66 69 6c 65 20 6e   /* UTF-8 file n
ffd10 61 6d 65 20 2a 2f 0a 29 7b 0a 20 20 44 57 4f 52  ame */.){.  DWOR
ffd20 44 20 62 79 74 65 73 50 65 72 53 65 63 74 6f 72  D bytesPerSector
ffd30 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c   = SQLITE_DEFAUL
ffd40 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 20  T_SECTOR_SIZE;. 
ffd50 20 2f 2a 20 47 65 74 44 69 73 6b 46 72 65 65 53   /* GetDiskFreeS
ffd60 70 61 63 65 20 69 73 20 6e 6f 74 20 73 75 70 70  pace is not supp
ffd70 6f 72 74 65 64 20 75 6e 64 65 72 20 57 49 4e 43  orted under WINC
ffd80 45 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f  E */.#if SQLITE_
ffd90 4f 53 5f 57 49 4e 43 45 0a 20 20 55 4e 55 53 45  OS_WINCE.  UNUSE
ffda0 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73  D_PARAMETER(pVfs
ffdb0 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
ffdc0 4d 45 54 45 52 28 7a 52 65 6c 61 74 69 76 65 29  METER(zRelative)
ffdd0 3b 0a 23 65 6c 73 65 0a 20 20 63 68 61 72 20 7a  ;.#else.  char z
ffde0 46 75 6c 6c 70 61 74 68 5b 4d 41 58 5f 50 41 54  Fullpath[MAX_PAT
ffdf0 48 2b 31 5d 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  H+1];.  int rc;.
ffe00 20 20 44 57 4f 52 44 20 64 77 52 65 74 20 3d 20    DWORD dwRet = 
ffe10 30 3b 0a 20 20 44 57 4f 52 44 20 64 77 44 75 6d  0;.  DWORD dwDum
ffe20 6d 79 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 57  my;..  /*.  ** W
ffe30 65 20 6e 65 65 64 20 74 6f 20 67 65 74 20 74 68  e need to get th
ffe40 65 20 66 75 6c 6c 20 70 61 74 68 20 6e 61 6d 65  e full path name
ffe50 20 6f 66 20 74 68 65 20 66 69 6c 65 0a 20 20 2a   of the file.  *
ffe60 2a 20 74 6f 20 67 65 74 20 74 68 65 20 64 72 69  * to get the dri
ffe70 76 65 20 6c 65 74 74 65 72 20 74 6f 20 6c 6f 6f  ve letter to loo
ffe80 6b 20 75 70 20 74 68 65 20 73 65 63 74 6f 72 0a  k up the sector.
ffe90 20 20 2a 2a 20 73 69 7a 65 2e 0a 20 20 2a 2f 0a    ** size..  */.
ffea0 20 20 72 63 20 3d 20 77 69 6e 46 75 6c 6c 50 61    rc = winFullPa
ffeb0 74 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a 52 65  thname(pVfs, zRe
ffec0 6c 61 74 69 76 65 2c 20 4d 41 58 5f 50 41 54 48  lative, MAX_PATH
ffed0 2c 20 7a 46 75 6c 6c 70 61 74 68 29 3b 0a 20 20  , zFullpath);.  
ffee0 69 66 28 20 72 63 20 3d 3d 20 53 51 4c 49 54 45  if( rc == SQLITE
ffef0 5f 4f 4b 20 29 0a 20 20 7b 0a 20 20 20 20 76 6f  _OK ).  {.    vo
fff00 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d  id *zConverted =
fff10 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65   convertUtf8File
fff20 6e 61 6d 65 28 7a 46 75 6c 6c 70 61 74 68 29 3b  name(zFullpath);
fff30 0a 20 20 20 20 69 66 28 20 7a 43 6f 6e 76 65 72  .    if( zConver
fff40 74 65 64 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ted ){.      if(
fff50 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 20   isNT() ){.     
fff60 20 20 20 2f 2a 20 74 72 69 6d 20 70 61 74 68 20     /* trim path 
fff70 74 6f 20 6a 75 73 74 20 64 72 69 76 65 20 72 65  to just drive re
fff80 66 65 72 65 6e 63 65 20 2a 2f 0a 20 20 20 20 20  ference */.     
fff90 20 20 20 57 43 48 41 52 20 2a 70 20 3d 20 7a 43     WCHAR *p = zC
fffa0 6f 6e 76 65 72 74 65 64 3b 0a 20 20 20 20 20 20  onverted;.      
fffb0 20 20 66 6f 72 28 3b 2a 70 3b 70 2b 2b 29 7b 0a    for(;*p;p++){.
fffc0 20 20 20 20 20 20 20 20 20 20 69 66 28 20 2a 70            if( *p
fffd0 20 3d 3d 20 27 5c 5c 27 20 29 7b 0a 20 20 20 20   == '\\' ){.    
fffe0 20 20 20 20 20 20 20 20 2a 70 20 3d 20 27 5c 30          *p = '\0
ffff0 27 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62  ';.            b
10000 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
10001 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
10002 20 20 20 20 64 77 52 65 74 20 3d 20 47 65 74 44      dwRet = GetD
10003 69 73 6b 46 72 65 65 53 70 61 63 65 57 28 28 57  iskFreeSpaceW((W
10004 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64  CHAR*)zConverted
10005 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
10006 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10007 20 20 20 20 26 64 77 44 75 6d 6d 79 2c 0a 20 20      &dwDummy,.  
10008 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10009 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1000a 26 62 79 74 65 73 50 65 72 53 65 63 74 6f 72 2c  &bytesPerSector,
1000b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1000c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1000d 20 20 20 26 64 77 44 75 6d 6d 79 2c 0a 20 20 20     &dwDummy,.   
1000e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1000f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
10010 64 77 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20  dwDummy);.      
10011 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f  }else{.        /
10012 2a 20 74 72 69 6d 20 70 61 74 68 20 74 6f 20 6a  * trim path to j
10013 75 73 74 20 64 72 69 76 65 20 72 65 66 65 72 65  ust drive refere
10014 6e 63 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 43  nce */.        C
10015 48 41 52 20 2a 70 20 3d 20 28 43 48 41 52 20 2a  HAR *p = (CHAR *
10016 29 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 20  )zConverted;.   
10017 20 20 20 20 20 66 6f 72 28 3b 2a 70 3b 70 2b 2b       for(;*p;p++
10018 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
10019 20 2a 70 20 3d 3d 20 27 5c 5c 27 20 29 7b 0a 20   *p == '\\' ){. 
1001a 20 20 20 20 20 20 20 20 20 20 20 2a 70 20 3d 20             *p = 
1001b 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20  '\0';.          
1001c 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
1001d 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
1001e 20 20 20 20 20 20 20 64 77 52 65 74 20 3d 20 47         dwRet = G
1001f 65 74 44 69 73 6b 46 72 65 65 53 70 61 63 65 41  etDiskFreeSpaceA
10020 28 28 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74  ((CHAR*)zConvert
10021 65 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ed,.            
10022 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10023 20 20 20 20 20 20 26 64 77 44 75 6d 6d 79 2c 0a        &dwDummy,.
10024 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10025 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10026 20 20 26 62 79 74 65 73 50 65 72 53 65 63 74 6f    &bytesPerSecto
10027 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r,.             
10028 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10029 20 20 20 20 20 26 64 77 44 75 6d 6d 79 2c 0a 20       &dwDummy,. 
1002a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1002b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1002c 20 26 64 77 44 75 6d 6d 79 29 3b 0a 20 20 20 20   &dwDummy);.    
1002d 20 20 7d 0a 20 20 20 20 20 20 66 72 65 65 28 7a    }.      free(z
1002e 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20  Converted);.    
1002f 7d 0a 20 20 20 20 69 66 28 20 21 64 77 52 65 74  }.    if( !dwRet
10030 20 29 7b 0a 20 20 20 20 20 20 62 79 74 65 73 50   ){.      bytesP
10031 65 72 53 65 63 74 6f 72 20 3d 20 53 51 4c 49 54  erSector = SQLIT
10032 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52  E_DEFAULT_SECTOR
10033 5f 53 49 5a 45 3b 0a 20 20 20 20 7d 0a 20 20 7d  _SIZE;.    }.  }
10034 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e  .#endif.  return
10035 20 28 69 6e 74 29 20 62 79 74 65 73 50 65 72 53   (int) bytesPerS
10036 65 63 74 6f 72 3b 20 0a 7d 0a 0a 23 69 66 6e 64  ector; .}..#ifnd
10037 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c  ef SQLITE_OMIT_L
10038 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a  OAD_EXTENSION./*
10039 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 20 66  .** Interfaces f
1003a 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 61  or opening a sha
1003b 72 65 64 20 6c 69 62 72 61 72 79 2c 20 66 69 6e  red library, fin
1003c 64 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e 74  ding entry point
1003d 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20  s.** within the 
1003e 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20  shared library, 
1003f 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 20  and closing the 
10040 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2e 0a  shared library..
10041 2a 2f 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61  */./*.** Interfa
10042 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20  ces for opening 
10043 61 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79  a shared library
10044 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20  , finding entry 
10045 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e  points.** within
10046 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72   the shared libr
10047 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67  ary, and closing
10048 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72   the shared libr
10049 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ary..*/.static v
1004a 6f 69 64 20 2a 77 69 6e 44 6c 4f 70 65 6e 28 73  oid *winDlOpen(s
1004b 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
1004c 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  , const char *zF
1004d 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 48 41 4e 44  ilename){.  HAND
1004e 4c 45 20 68 3b 0a 20 20 76 6f 69 64 20 2a 7a 43  LE h;.  void *zC
1004f 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e 76 65  onverted = conve
10050 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a  rtUtf8Filename(z
10051 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 55 4e 55  Filename);.  UNU
10052 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56  SED_PARAMETER(pV
10053 66 73 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76  fs);.  if( zConv
10054 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20  erted==0 ){.    
10055 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
10056 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20  if( isNT() ){.  
10057 20 20 68 20 3d 20 4c 6f 61 64 4c 69 62 72 61 72    h = LoadLibrar
10058 79 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76  yW((WCHAR*)zConv
10059 65 72 74 65 64 29 3b 0a 2f 2a 20 69 73 4e 54 28  erted);./* isNT(
1005a 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45  ) is 1 if SQLITE
1005b 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f  _OS_WINCE==1, so
1005c 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65   this else is ne
1005d 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a  ver executed. .*
1005e 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53 43 49  * Since the ASCI
1005f 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65  I version of the
10060 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64  se Windows API d
10061 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20  o not exist for 
10062 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69  WINCE,.** it's i
10063 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20  mportant to not 
10064 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66  reference them f
10065 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e  or WINCE builds.
10066 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f  .*/.#if SQLITE_O
10067 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c  S_WINCE==0.  }el
10068 73 65 7b 0a 20 20 20 20 68 20 3d 20 4c 6f 61 64  se{.    h = Load
10069 4c 69 62 72 61 72 79 41 28 28 63 68 61 72 2a 29  LibraryA((char*)
1006a 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 23 65 6e  zConverted);.#en
1006b 64 69 66 0a 20 20 7d 0a 20 20 66 72 65 65 28 7a  dif.  }.  free(z
1006c 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 72 65  Converted);.  re
1006d 74 75 72 6e 20 28 76 6f 69 64 2a 29 68 3b 0a 7d  turn (void*)h;.}
1006e 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e  .static void win
1006f 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f  DlError(sqlite3_
10070 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e  vfs *pVfs, int n
10071 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f  Buf, char *zBufO
10072 75 74 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41  ut){.  UNUSED_PA
10073 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20  RAMETER(pVfs);. 
10074 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73 67   getLastErrorMsg
10075 28 6e 42 75 66 2c 20 7a 42 75 66 4f 75 74 29 3b  (nBuf, zBufOut);
10076 0a 7d 0a 76 6f 69 64 20 28 2a 77 69 6e 44 6c 53  .}.void (*winDlS
10077 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  ym(sqlite3_vfs *
10078 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e  pVfs, void *pHan
10079 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  dle, const char 
1007a 2a 7a 53 79 6d 62 6f 6c 29 29 28 76 6f 69 64 29  *zSymbol))(void)
1007b 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  {.  UNUSED_PARAM
1007c 45 54 45 52 28 70 56 66 73 29 3b 0a 23 69 66 20  ETER(pVfs);.#if 
1007d 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a  SQLITE_OS_WINCE.
1007e 20 20 2f 2a 20 54 68 65 20 47 65 74 50 72 6f 63    /* The GetProc
1007f 41 64 64 72 65 73 73 41 28 29 20 72 6f 75 74 69  AddressA() routi
10080 6e 65 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c  ne is only avail
10081 61 62 6c 65 20 6f 6e 20 77 69 6e 63 65 2e 20 2a  able on wince. *
10082 2f 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64  /.  return (void
10083 28 2a 29 28 76 6f 69 64 29 29 47 65 74 50 72 6f  (*)(void))GetPro
10084 63 41 64 64 72 65 73 73 41 28 28 48 41 4e 44 4c  cAddressA((HANDL
10085 45 29 70 48 61 6e 64 6c 65 2c 20 7a 53 79 6d 62  E)pHandle, zSymb
10086 6f 6c 29 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a 20  ol);.#else.  /* 
10087 41 6c 6c 20 6f 74 68 65 72 20 77 69 6e 64 6f 77  All other window
10088 73 20 70 6c 61 74 66 6f 72 6d 73 20 65 78 70 65  s platforms expe
10089 63 74 20 47 65 74 50 72 6f 63 41 64 64 72 65 73  ct GetProcAddres
1008a 73 28 29 20 74 6f 20 74 61 6b 65 0a 20 20 2a 2a  s() to take.  **
1008b 20 61 6e 20 41 6e 73 69 20 73 74 72 69 6e 67 20   an Ansi string 
1008c 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68  regardless of th
1008d 65 20 5f 55 4e 49 43 4f 44 45 20 73 65 74 74 69  e _UNICODE setti
1008e 6e 67 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28  ng */.  return (
1008f 76 6f 69 64 28 2a 29 28 76 6f 69 64 29 29 47 65  void(*)(void))Ge
10090 74 50 72 6f 63 41 64 64 72 65 73 73 28 28 48 41  tProcAddress((HA
10091 4e 44 4c 45 29 70 48 61 6e 64 6c 65 2c 20 7a 53  NDLE)pHandle, zS
10092 79 6d 62 6f 6c 29 3b 0a 23 65 6e 64 69 66 0a 7d  ymbol);.#endif.}
10093 0a 76 6f 69 64 20 77 69 6e 44 6c 43 6c 6f 73 65  .void winDlClose
10094 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
10095 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c  fs, void *pHandl
10096 65 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  e){.  UNUSED_PAR
10097 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20  AMETER(pVfs);.  
10098 46 72 65 65 4c 69 62 72 61 72 79 28 28 48 41 4e  FreeLibrary((HAN
10099 44 4c 45 29 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a  DLE)pHandle);.}.
1009a 23 65 6c 73 65 20 2f 2a 20 69 66 20 53 51 4c 49  #else /* if SQLI
1009b 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54  TE_OMIT_LOAD_EXT
1009c 45 4e 53 49 4f 4e 20 69 73 20 64 65 66 69 6e 65  ENSION is define
1009d 64 3a 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20  d: */.  #define 
1009e 77 69 6e 44 6c 4f 70 65 6e 20 20 30 0a 20 20 23  winDlOpen  0.  #
1009f 64 65 66 69 6e 65 20 77 69 6e 44 6c 45 72 72 6f  define winDlErro
100a0 72 20 30 0a 20 20 23 64 65 66 69 6e 65 20 77 69  r 0.  #define wi
100a1 6e 44 6c 53 79 6d 20 20 20 30 0a 20 20 23 64 65  nDlSym   0.  #de
100a2 66 69 6e 65 20 77 69 6e 44 6c 43 6c 6f 73 65 20  fine winDlClose 
100a3 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  0.#endif.../*.**
100a4 20 57 72 69 74 65 20 75 70 20 74 6f 20 6e 42 75   Write up to nBu
100a5 66 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f  f bytes of rando
100a6 6d 6e 65 73 73 20 69 6e 74 6f 20 7a 42 75 66 2e  mness into zBuf.
100a7 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
100a8 69 6e 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c  inRandomness(sql
100a9 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
100aa 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a  int nBuf, char *
100ab 7a 42 75 66 29 7b 0a 20 20 69 6e 74 20 6e 20 3d  zBuf){.  int n =
100ac 20 30 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52   0;.  UNUSED_PAR
100ad 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 23 69  AMETER(pVfs);.#i
100ae 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
100af 5f 54 45 53 54 29 0a 20 20 6e 20 3d 20 6e 42 75  _TEST).  n = nBu
100b0 66 3b 0a 20 20 6d 65 6d 73 65 74 28 7a 42 75 66  f;.  memset(zBuf
100b1 2c 20 30 2c 20 6e 42 75 66 29 3b 0a 23 65 6c 73  , 0, nBuf);.#els
100b2 65 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 28 53  e.  if( sizeof(S
100b3 59 53 54 45 4d 54 49 4d 45 29 3c 3d 6e 42 75 66  YSTEMTIME)<=nBuf
100b4 2d 6e 20 29 7b 0a 20 20 20 20 53 59 53 54 45 4d  -n ){.    SYSTEM
100b5 54 49 4d 45 20 78 3b 0a 20 20 20 20 47 65 74 53  TIME x;.    GetS
100b6 79 73 74 65 6d 54 69 6d 65 28 26 78 29 3b 0a 20  ystemTime(&x);. 
100b7 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b     memcpy(&zBuf[
100b8 6e 5d 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 78  n], &x, sizeof(x
100b9 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a  ));.    n += siz
100ba 65 6f 66 28 78 29 3b 0a 20 20 7d 0a 20 20 69 66  eof(x);.  }.  if
100bb 28 20 73 69 7a 65 6f 66 28 44 57 4f 52 44 29 3c  ( sizeof(DWORD)<
100bc 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20 20 44  =nBuf-n ){.    D
100bd 57 4f 52 44 20 70 69 64 20 3d 20 47 65 74 43 75  WORD pid = GetCu
100be 72 72 65 6e 74 50 72 6f 63 65 73 73 49 64 28 29  rrentProcessId()
100bf 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42  ;.    memcpy(&zB
100c0 75 66 5b 6e 5d 2c 20 26 70 69 64 2c 20 73 69 7a  uf[n], &pid, siz
100c1 65 6f 66 28 70 69 64 29 29 3b 0a 20 20 20 20 6e  eof(pid));.    n
100c2 20 2b 3d 20 73 69 7a 65 6f 66 28 70 69 64 29 3b   += sizeof(pid);
100c3 0a 20 20 7d 0a 20 20 69 66 28 20 73 69 7a 65 6f  .  }.  if( sizeo
100c4 66 28 44 57 4f 52 44 29 3c 3d 6e 42 75 66 2d 6e  f(DWORD)<=nBuf-n
100c5 20 29 7b 0a 20 20 20 20 44 57 4f 52 44 20 63 6e   ){.    DWORD cn
100c6 74 20 3d 20 47 65 74 54 69 63 6b 43 6f 75 6e 74  t = GetTickCount
100c7 28 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26  ();.    memcpy(&
100c8 7a 42 75 66 5b 6e 5d 2c 20 26 63 6e 74 2c 20 73  zBuf[n], &cnt, s
100c9 69 7a 65 6f 66 28 63 6e 74 29 29 3b 0a 20 20 20  izeof(cnt));.   
100ca 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 63 6e 74   n += sizeof(cnt
100cb 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 69 7a  );.  }.  if( siz
100cc 65 6f 66 28 4c 41 52 47 45 5f 49 4e 54 45 47 45  eof(LARGE_INTEGE
100cd 52 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20  R)<=nBuf-n ){.  
100ce 20 20 4c 41 52 47 45 5f 49 4e 54 45 47 45 52 20    LARGE_INTEGER 
100cf 69 3b 0a 20 20 20 20 51 75 65 72 79 50 65 72 66  i;.    QueryPerf
100d0 6f 72 6d 61 6e 63 65 43 6f 75 6e 74 65 72 28 26  ormanceCounter(&
100d1 69 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26  i);.    memcpy(&
100d2 7a 42 75 66 5b 6e 5d 2c 20 26 69 2c 20 73 69 7a  zBuf[n], &i, siz
100d3 65 6f 66 28 69 29 29 3b 0a 20 20 20 20 6e 20 2b  eof(i));.    n +
100d4 3d 20 73 69 7a 65 6f 66 28 69 29 3b 0a 20 20 7d  = sizeof(i);.  }
100d5 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e  .#endif.  return
100d6 20 6e 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 6c   n;.}.../*.** Sl
100d7 65 65 70 20 66 6f 72 20 61 20 6c 69 74 74 6c 65  eep for a little
100d8 20 77 68 69 6c 65 2e 20 20 52 65 74 75 72 6e 20   while.  Return 
100d9 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69  the amount of ti
100da 6d 65 20 73 6c 65 70 74 2e 0a 2a 2f 0a 73 74 61  me slept..*/.sta
100db 74 69 63 20 69 6e 74 20 77 69 6e 53 6c 65 65 70  tic int winSleep
100dc 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
100dd 66 73 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63  fs, int microsec
100de 29 7b 0a 20 20 53 6c 65 65 70 28 28 6d 69 63 72  ){.  Sleep((micr
100df 6f 73 65 63 2b 39 39 39 29 2f 31 30 30 30 29 3b  osec+999)/1000);
100e0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
100e1 54 45 52 28 70 56 66 73 29 3b 0a 20 20 72 65 74  TER(pVfs);.  ret
100e2 75 72 6e 20 28 28 6d 69 63 72 6f 73 65 63 2b 39  urn ((microsec+9
100e3 39 39 29 2f 31 30 30 30 29 2a 31 30 30 30 3b 0a  99)/1000)*1000;.
100e4 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  }../*.** The fol
100e5 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 2c  lowing variable,
100e6 20 69 66 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e   if set to a non
100e7 2d 7a 65 72 6f 20 76 61 6c 75 65 2c 20 62 65 63  -zero value, bec
100e8 6f 6d 65 73 20 74 68 65 20 72 65 73 75 6c 74 0a  omes the result.
100e9 2a 2a 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d  ** returned from
100ea 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e   sqlite3OsCurren
100eb 74 54 69 6d 65 28 29 2e 20 20 54 68 69 73 20 69  tTime().  This i
100ec 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69  s used for testi
100ed 6e 67 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ng..*/.#ifdef SQ
100ee 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
100ef 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
100f0 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20  _current_time = 
100f1 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  0;.#endif../*.**
100f2 20 46 69 6e 64 20 74 68 65 20 63 75 72 72 65 6e   Find the curren
100f3 74 20 74 69 6d 65 20 28 69 6e 20 55 6e 69 76 65  t time (in Unive
100f4 72 73 61 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64  rsal Coordinated
100f5 20 54 69 6d 65 29 2e 20 20 57 72 69 74 65 20 74   Time).  Write t
100f6 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 74 69  he.** current ti
100f7 6d 65 20 61 6e 64 20 64 61 74 65 20 61 73 20 61  me and date as a
100f8 20 4a 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62   Julian Day numb
100f9 65 72 20 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61  er into *prNow a
100fa 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20  nd.** return 0. 
100fb 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65   Return 1 if the
100fc 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 63   time and date c
100fd 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a  annot be found..
100fe 2a 2f 0a 69 6e 74 20 77 69 6e 43 75 72 72 65 6e  */.int winCurren
100ff 74 54 69 6d 65 28 73 71 6c 69 74 65 33 5f 76 66  tTime(sqlite3_vf
10100 73 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65 20  s *pVfs, double 
10101 2a 70 72 4e 6f 77 29 7b 0a 20 20 46 49 4c 45 54  *prNow){.  FILET
10102 49 4d 45 20 66 74 3b 0a 20 20 2f 2a 20 46 49 4c  IME ft;.  /* FIL
10103 45 54 49 4d 45 20 73 74 72 75 63 74 75 72 65 20  ETIME structure 
10104 69 73 20 61 20 36 34 2d 62 69 74 20 76 61 6c 75  is a 64-bit valu
10105 65 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74  e representing t
10106 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 0a 20 20  he number of .  
10107 20 20 20 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e     100-nanosecon
10108 64 20 69 6e 74 65 72 76 61 6c 73 20 73 69 6e 63  d intervals sinc
10109 65 20 4a 61 6e 75 61 72 79 20 31 2c 20 31 36 30  e January 1, 160
1010a 31 20 28 3d 20 4a 44 20 32 33 30 35 38 31 33 2e  1 (= JD 2305813.
1010b 35 29 2e 20 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  5). .  */.  sqli
1010c 74 65 33 5f 69 6e 74 36 34 20 74 69 6d 65 57 3b  te3_int64 timeW;
1010d 20 20 20 2f 2a 20 57 68 6f 6c 65 20 64 61 79 73     /* Whole days
1010e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e   */.  sqlite3_in
1010f 74 36 34 20 74 69 6d 65 46 3b 20 20 20 2f 2a 20  t64 timeF;   /* 
10110 46 72 61 63 74 69 6f 6e 61 6c 20 44 61 79 73 20  Fractional Days 
10111 2a 2f 0a 0a 20 20 2f 2a 20 4e 75 6d 62 65 72 20  */..  /* Number 
10112 6f 66 20 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e  of 100-nanosecon
10113 64 20 69 6e 74 65 72 76 61 6c 73 20 69 6e 20 61  d intervals in a
10114 20 73 69 6e 67 6c 65 20 64 61 79 20 2a 2f 0a 20   single day */. 
10115 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71   static const sq
10116 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e 74 75 50  lite3_int64 ntuP
10117 65 72 44 61 79 20 3d 20 0a 20 20 20 20 20 20 31  erDay = .      1
10118 30 30 30 30 30 30 30 2a 28 73 71 6c 69 74 65 33  0000000*(sqlite3
10119 5f 69 6e 74 36 34 29 38 36 34 30 30 3b 0a 0a 20  _int64)86400;.. 
1011a 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 31 30   /* Number of 10
1011b 30 2d 6e 61 6e 6f 73 65 63 6f 6e 64 20 69 6e 74  0-nanosecond int
1011c 65 72 76 61 6c 73 20 69 6e 20 68 61 6c 66 20 6f  ervals in half o
1011d 66 20 61 20 64 61 79 20 2a 2f 0a 20 20 73 74 61  f a day */.  sta
1011e 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  tic const sqlite
1011f 33 5f 69 6e 74 36 34 20 6e 74 75 50 65 72 48 61  3_int64 ntuPerHa
10120 6c 66 44 61 79 20 3d 20 0a 20 20 20 20 20 20 31  lfDay = .      1
10121 30 30 30 30 30 30 30 2a 28 73 71 6c 69 74 65 33  0000000*(sqlite3
10122 5f 69 6e 74 36 34 29 34 33 32 30 30 3b 0a 0a 20  _int64)43200;.. 
10123 20 2f 2a 20 32 5e 33 32 20 2d 20 74 6f 20 61 76   /* 2^32 - to av
10124 6f 69 64 20 75 73 65 20 6f 66 20 4c 4c 20 61 6e  oid use of LL an
10125 64 20 77 61 72 6e 69 6e 67 73 20 69 6e 20 67 63  d warnings in gc
10126 63 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f  c */.  static co
10127 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  nst sqlite3_int6
10128 34 20 6d 61 78 33 32 42 69 74 56 61 6c 75 65 20  4 max32BitValue 
10129 3d 20 0a 20 20 20 20 20 20 28 73 71 6c 69 74 65  = .      (sqlite
1012a 33 5f 69 6e 74 36 34 29 32 30 30 30 30 30 30 30  3_int64)20000000
1012b 30 30 20 2b 20 28 73 71 6c 69 74 65 33 5f 69 6e  00 + (sqlite3_in
1012c 74 36 34 29 32 30 30 30 30 30 30 30 30 30 20 2b  t64)2000000000 +
1012d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29   (sqlite3_int64)
1012e 32 39 34 39 36 37 32 39 36 3b 0a 0a 23 69 66 20  294967296;..#if 
1012f 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a  SQLITE_OS_WINCE.
10130 20 20 53 59 53 54 45 4d 54 49 4d 45 20 74 69 6d    SYSTEMTIME tim
10131 65 3b 0a 20 20 47 65 74 53 79 73 74 65 6d 54 69  e;.  GetSystemTi
10132 6d 65 28 26 74 69 6d 65 29 3b 0a 20 20 2f 2a 20  me(&time);.  /* 
10133 69 66 20 53 79 73 74 65 6d 54 69 6d 65 54 6f 46  if SystemTimeToF
10134 69 6c 65 54 69 6d 65 28 29 20 66 61 69 6c 73 2c  ileTime() fails,
10135 20 69 74 20 72 65 74 75 72 6e 73 20 7a 65 72 6f   it returns zero
10136 2e 20 2a 2f 0a 20 20 69 66 20 28 21 53 79 73 74  . */.  if (!Syst
10137 65 6d 54 69 6d 65 54 6f 46 69 6c 65 54 69 6d 65  emTimeToFileTime
10138 28 26 74 69 6d 65 2c 26 66 74 29 29 7b 0a 20 20  (&time,&ft)){.  
10139 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
1013a 23 65 6c 73 65 0a 20 20 47 65 74 53 79 73 74 65  #else.  GetSyste
1013b 6d 54 69 6d 65 41 73 46 69 6c 65 54 69 6d 65 28  mTimeAsFileTime(
1013c 20 26 66 74 20 29 3b 0a 23 65 6e 64 69 66 0a 20   &ft );.#endif. 
1013d 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
1013e 52 28 70 56 66 73 29 3b 0a 20 20 74 69 6d 65 57  R(pVfs);.  timeW
1013f 20 3d 20 28 28 28 73 71 6c 69 74 65 33 5f 69 6e   = (((sqlite3_in
10140 74 36 34 29 66 74 2e 64 77 48 69 67 68 44 61 74  t64)ft.dwHighDat
10141 65 54 69 6d 65 29 2a 6d 61 78 33 32 42 69 74 56  eTime)*max32BitV
10142 61 6c 75 65 29 20 2b 20 28 73 71 6c 69 74 65 33  alue) + (sqlite3
10143 5f 69 6e 74 36 34 29 66 74 2e 64 77 4c 6f 77 44  _int64)ft.dwLowD
10144 61 74 65 54 69 6d 65 3b 0a 20 20 74 69 6d 65 46  ateTime;.  timeF
10145 20 3d 20 74 69 6d 65 57 20 25 20 6e 74 75 50 65   = timeW % ntuPe
10146 72 44 61 79 3b 20 20 20 20 20 20 20 20 20 20 2f  rDay;          /
10147 2a 20 66 72 61 63 74 69 6f 6e 61 6c 20 64 61 79  * fractional day
10148 73 20 28 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e  s (100-nanosecon
10149 64 73 29 20 2a 2f 0a 20 20 74 69 6d 65 57 20 3d  ds) */.  timeW =
1014a 20 74 69 6d 65 57 20 2f 20 6e 74 75 50 65 72 44   timeW / ntuPerD
1014b 61 79 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ay;          /* 
1014c 77 68 6f 6c 65 20 64 61 79 73 20 2a 2f 0a 20 20  whole days */.  
1014d 74 69 6d 65 57 20 3d 20 74 69 6d 65 57 20 2b 20  timeW = timeW + 
1014e 32 33 30 35 38 31 33 3b 20 20 20 20 20 20 20 20  2305813;        
1014f 20 20 20 20 2f 2a 20 61 64 64 20 77 68 6f 6c 65      /* add whole
10150 20 64 61 79 73 20 28 66 72 6f 6d 20 32 33 30 35   days (from 2305
10151 38 31 33 2e 35 29 20 2a 2f 0a 20 20 74 69 6d 65  813.5) */.  time
10152 46 20 3d 20 74 69 6d 65 46 20 2b 20 6e 74 75 50  F = timeF + ntuP
10153 65 72 48 61 6c 66 44 61 79 3b 20 20 20 20 20 20  erHalfDay;      
10154 2f 2a 20 61 64 64 20 68 61 6c 66 20 61 20 64 61  /* add half a da
10155 79 20 28 66 72 6f 6d 20 32 33 30 35 38 31 33 2e  y (from 2305813.
10156 35 29 20 2a 2f 0a 20 20 74 69 6d 65 57 20 3d 20  5) */.  timeW = 
10157 74 69 6d 65 57 20 2b 20 28 74 69 6d 65 46 2f 6e  timeW + (timeF/n
10158 74 75 50 65 72 44 61 79 29 3b 20 20 2f 2a 20 61  tuPerDay);  /* a
10159 64 64 20 77 68 6f 6c 65 20 64 61 79 20 69 66 20  dd whole day if 
1015a 68 61 6c 66 20 64 61 79 20 6d 61 64 65 20 6f 6e  half day made on
1015b 65 20 2a 2f 0a 20 20 74 69 6d 65 46 20 3d 20 74  e */.  timeF = t
1015c 69 6d 65 46 20 25 20 6e 74 75 50 65 72 44 61 79  imeF % ntuPerDay
1015d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f  ;          /* co
1015e 6d 70 75 74 65 20 6e 65 77 20 66 72 61 63 74 69  mpute new fracti
1015f 6f 6e 61 6c 20 64 61 79 73 20 2a 2f 0a 20 20 2a  onal days */.  *
10160 70 72 4e 6f 77 20 3d 20 28 64 6f 75 62 6c 65 29  prNow = (double)
10161 74 69 6d 65 57 20 2b 20 28 28 64 6f 75 62 6c 65  timeW + ((double
10162 29 74 69 6d 65 46 20 2f 20 28 64 6f 75 62 6c 65  )timeF / (double
10163 29 6e 74 75 50 65 72 44 61 79 29 3b 0a 23 69 66  )ntuPerDay);.#if
10164 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
10165 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63 75    if( sqlite3_cu
10166 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0a 20 20  rrent_time ){.  
10167 20 20 2a 70 72 4e 6f 77 20 3d 20 28 28 64 6f 75    *prNow = ((dou
10168 62 6c 65 29 73 71 6c 69 74 65 33 5f 63 75 72 72  ble)sqlite3_curr
10169 65 6e 74 5f 74 69 6d 65 20 2b 20 28 64 6f 75 62  ent_time + (doub
1016a 6c 65 29 34 33 32 30 30 29 20 2f 20 28 64 6f 75  le)43200) / (dou
1016b 62 6c 65 29 38 36 34 30 30 20 2b 20 28 64 6f 75  ble)86400 + (dou
1016c 62 6c 65 29 32 34 34 30 35 38 37 3b 0a 20 20 7d  ble)2440587;.  }
1016d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e  .#endif.  return
1016e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65   0;.}../*.** The
1016f 20 69 64 65 61 20 69 73 20 74 68 61 74 20 74 68   idea is that th
10170 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 6f 72 6b  is function work
10171 73 20 6c 69 6b 65 20 61 20 63 6f 6d 62 69 6e 61  s like a combina
10172 74 69 6f 6e 20 6f 66 0a 2a 2a 20 47 65 74 4c 61  tion of.** GetLa
10173 73 74 45 72 72 6f 72 28 29 20 61 6e 64 20 46 6f  stError() and Fo
10174 72 6d 61 74 4d 65 73 73 61 67 65 28 29 20 6f 6e  rmatMessage() on
10175 20 77 69 6e 64 6f 77 73 20 28 6f 72 20 65 72 72   windows (or err
10176 6e 6f 20 61 6e 64 0a 2a 2a 20 73 74 72 65 72 72  no and.** strerr
10177 6f 72 5f 72 28 29 20 6f 6e 20 75 6e 69 78 29 2e  or_r() on unix).
10178 20 41 66 74 65 72 20 61 6e 20 65 72 72 6f 72 20   After an error 
10179 69 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 61  is returned by a
1017a 6e 20 4f 53 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  n OS.** function
1017b 2c 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 20 74  , SQLite calls t
1017c 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 74  his function wit
1017d 68 20 7a 42 75 66 20 70 6f 69 6e 74 69 6e 67 20  h zBuf pointing 
1017e 74 6f 0a 2a 2a 20 61 20 62 75 66 66 65 72 20 6f  to.** a buffer o
1017f 66 20 6e 42 75 66 20 62 79 74 65 73 2e 20 54 68  f nBuf bytes. Th
10180 65 20 4f 53 20 6c 61 79 65 72 20 73 68 6f 75 6c  e OS layer shoul
10181 64 20 70 6f 70 75 6c 61 74 65 20 74 68 65 0a 2a  d populate the.*
10182 2a 20 62 75 66 66 65 72 20 77 69 74 68 20 61 20  * buffer with a 
10183 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 55  nul-terminated U
10184 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 65 72 72  TF-8 encoded err
10185 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 64 65  or message.** de
10186 73 63 72 69 62 69 6e 67 20 74 68 65 20 6c 61 73  scribing the las
10187 74 20 49 4f 20 65 72 72 6f 72 20 74 6f 20 68 61  t IO error to ha
10188 76 65 20 6f 63 63 75 72 72 65 64 20 77 69 74 68  ve occurred with
10189 69 6e 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a  in the calling.*
1018a 2a 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20  * thread..**.** 
1018b 49 66 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73  If the error mes
1018c 73 61 67 65 20 69 73 20 74 6f 6f 20 6c 61 72 67  sage is too larg
1018d 65 20 66 6f 72 20 74 68 65 20 73 75 70 70 6c 69  e for the suppli
1018e 65 64 20 62 75 66 66 65 72 2c 0a 2a 2a 20 69 74  ed buffer,.** it
1018f 20 73 68 6f 75 6c 64 20 62 65 20 74 72 75 6e 63   should be trunc
10190 61 74 65 64 2e 20 54 68 65 20 72 65 74 75 72 6e  ated. The return
10191 20 76 61 6c 75 65 20 6f 66 20 78 47 65 74 4c 61   value of xGetLa
10192 73 74 45 72 72 6f 72 0a 2a 2a 20 69 73 20 7a 65  stError.** is ze
10193 72 6f 20 69 66 20 74 68 65 20 65 72 72 6f 72 20  ro if the error 
10194 6d 65 73 73 61 67 65 20 66 69 74 73 20 69 6e 20  message fits in 
10195 74 68 65 20 62 75 66 66 65 72 2c 20 6f 72 20 6e  the buffer, or n
10196 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 6f 74 68 65 72  on-zero.** other
10197 77 69 73 65 20 28 69 66 20 74 68 65 20 6d 65 73  wise (if the mes
10198 73 61 67 65 20 77 61 73 20 74 72 75 6e 63 61 74  sage was truncat
10199 65 64 29 2e 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f  ed). If non-zero
1019a 20 69 73 20 72 65 74 75 72 6e 65 64 2c 0a 2a 2a   is returned,.**
1019b 20 74 68 65 6e 20 69 74 20 69 73 20 6e 6f 74 20   then it is not 
1019c 6e 65 63 65 73 73 61 72 79 20 74 6f 20 69 6e 63  necessary to inc
1019d 6c 75 64 65 20 74 68 65 20 6e 75 6c 2d 74 65 72  lude the nul-ter
1019e 6d 69 6e 61 74 6f 72 20 63 68 61 72 61 63 74 65  minator characte
1019f 72 0a 2a 2a 20 69 6e 20 74 68 65 20 6f 75 74 70  r.** in the outp
101a0 75 74 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a  ut buffer..**.**
101a1 20 4e 6f 74 20 73 75 70 70 6c 79 69 6e 67 20 61   Not supplying a
101a2 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
101a3 77 69 6c 6c 20 68 61 76 65 20 6e 6f 20 61 64 76  will have no adv
101a4 65 72 73 65 20 65 66 66 65 63 74 0a 2a 2a 20 6f  erse effect.** o
101a5 6e 20 53 51 4c 69 74 65 2e 20 49 74 20 69 73 20  n SQLite. It is 
101a6 66 69 6e 65 20 74 6f 20 68 61 76 65 20 61 6e 20  fine to have an 
101a7 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74  implementation t
101a8 68 61 74 20 6e 65 76 65 72 0a 2a 2a 20 72 65 74  hat never.** ret
101a9 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 6d 65  urns an error me
101aa 73 73 61 67 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 69  ssage:.**.**   i
101ab 6e 74 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72  nt xGetLastError
101ac 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
101ad 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68  fs, int nBuf, ch
101ae 61 72 20 2a 7a 42 75 66 29 7b 0a 2a 2a 20 20 20  ar *zBuf){.**   
101af 20 20 61 73 73 65 72 74 28 7a 42 75 66 5b 30 5d    assert(zBuf[0]
101b0 3d 3d 27 5c 30 27 29 3b 0a 2a 2a 20 20 20 20 20  =='\0');.**     
101b1 72 65 74 75 72 6e 20 30 3b 0a 2a 2a 20 20 20 7d  return 0;.**   }
101b2 0a 2a 2a 0a 2a 2a 20 48 6f 77 65 76 65 72 20 69  .**.** However i
101b3 66 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  f an error messa
101b4 67 65 20 69 73 20 73 75 70 70 6c 69 65 64 2c 20  ge is supplied, 
101b5 69 74 20 77 69 6c 6c 20 62 65 20 69 6e 63 6f 72  it will be incor
101b6 70 6f 72 61 74 65 64 0a 2a 2a 20 62 79 20 73 71  porated.** by sq
101b7 6c 69 74 65 20 69 6e 74 6f 20 74 68 65 20 65 72  lite into the er
101b8 72 6f 72 20 6d 65 73 73 61 67 65 20 61 76 61 69  ror message avai
101b9 6c 61 62 6c 65 20 74 6f 20 74 68 65 20 75 73 65  lable to the use
101ba 72 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74  r using.** sqlit
101bb 65 33 5f 65 72 72 6d 73 67 28 29 2c 20 70 6f 73  e3_errmsg(), pos
101bc 73 69 62 6c 79 20 6d 61 6b 69 6e 67 20 49 4f 20  sibly making IO 
101bd 65 72 72 6f 72 73 20 65 61 73 69 65 72 20 74 6f  errors easier to
101be 20 64 65 62 75 67 2e 0a 2a 2f 0a 73 74 61 74 69   debug..*/.stati
101bf 63 20 69 6e 74 20 77 69 6e 47 65 74 4c 61 73 74  c int winGetLast
101c0 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66  Error(sqlite3_vf
101c1 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75  s *pVfs, int nBu
101c2 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a  f, char *zBuf){.
101c3 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
101c4 45 52 28 70 56 66 73 29 3b 0a 20 20 72 65 74 75  ER(pVfs);.  retu
101c5 72 6e 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d  rn getLastErrorM
101c6 73 67 28 6e 42 75 66 2c 20 7a 42 75 66 29 3b 0a  sg(nBuf, zBuf);.
101c7 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c  }../*.** Initial
101c8 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61  ize and deinitia
101c9 6c 69 7a 65 20 74 68 65 20 6f 70 65 72 61 74 69  lize the operati
101ca 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66  ng system interf
101cb 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ace..*/.SQLITE_A
101cc 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  PI int sqlite3_o
101cd 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20  s_init(void){.  
101ce 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76  static sqlite3_v
101cf 66 73 20 77 69 6e 56 66 73 20 3d 20 7b 0a 20 20  fs winVfs = {.  
101d0 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20    1,            
101d1 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e       /* iVersion
101d2 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28 77   */.    sizeof(w
101d3 69 6e 46 69 6c 65 29 2c 20 20 20 2f 2a 20 73 7a  inFile),   /* sz
101d4 4f 73 46 69 6c 65 20 2a 2f 0a 20 20 20 20 4d 41  OsFile */.    MA
101d5 58 5f 50 41 54 48 2c 20 20 20 20 20 20 20 20 20  X_PATH,         
101d6 20 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20 2a   /* mxPathname *
101d7 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20  /.    0,        
101d8 20 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78           /* pNex
101d9 74 20 2a 2f 0a 20 20 20 20 22 77 69 6e 33 32 22  t */.    "win32"
101da 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a  ,           /* z
101db 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20  Name */.    0,  
101dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
101dd 2a 20 70 41 70 70 44 61 74 61 20 2a 2f 0a 20 0a  * pAppData */. .
101de 20 20 20 20 77 69 6e 4f 70 65 6e 2c 20 20 20 20      winOpen,    
101df 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20         /* xOpen 
101e0 2a 2f 0a 20 20 20 20 77 69 6e 44 65 6c 65 74 65  */.    winDelete
101e1 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65  ,         /* xDe
101e2 6c 65 74 65 20 2a 2f 0a 20 20 20 20 77 69 6e 41  lete */.    winA
101e3 63 63 65 73 73 2c 20 20 20 20 20 20 20 20 20 2f  ccess,         /
101e4 2a 20 78 41 63 63 65 73 73 20 2a 2f 0a 20 20 20  * xAccess */.   
101e5 20 77 69 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65   winFullPathname
101e6 2c 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61 74 68  ,   /* xFullPath
101e7 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 77 69 6e 44  name */.    winD
101e8 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 2f  lOpen,         /
101e9 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a 20 20 20  * xDlOpen */.   
101ea 20 77 69 6e 44 6c 45 72 72 6f 72 2c 20 20 20 20   winDlError,    
101eb 20 20 20 20 2f 2a 20 78 44 6c 45 72 72 6f 72 20      /* xDlError 
101ec 2a 2f 0a 20 20 20 20 77 69 6e 44 6c 53 79 6d 2c  */.    winDlSym,
101ed 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c            /* xDl
101ee 53 79 6d 20 2a 2f 0a 20 20 20 20 77 69 6e 44 6c  Sym */.    winDl
101ef 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 2f 2a  Close,        /*
101f0 20 78 44 6c 43 6c 6f 73 65 20 2a 2f 0a 20 20 20   xDlClose */.   
101f1 20 77 69 6e 52 61 6e 64 6f 6d 6e 65 73 73 2c 20   winRandomness, 
101f2 20 20 20 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65      /* xRandomne
101f3 73 73 20 2a 2f 0a 20 20 20 20 77 69 6e 53 6c 65  ss */.    winSle
101f4 65 70 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ep,          /* 
101f5 78 53 6c 65 65 70 20 2a 2f 0a 20 20 20 20 77 69  xSleep */.    wi
101f6 6e 43 75 72 72 65 6e 74 54 69 6d 65 2c 20 20 20  nCurrentTime,   
101f7 20 2f 2a 20 78 43 75 72 72 65 6e 74 54 69 6d 65   /* xCurrentTime
101f8 20 2a 2f 0a 20 20 20 20 77 69 6e 47 65 74 4c 61   */.    winGetLa
101f9 73 74 45 72 72 6f 72 20 20 20 20 2f 2a 20 78 47  stError    /* xG
101fa 65 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f 0a 20  etLastError */. 
101fb 20 7d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 76   };..  sqlite3_v
101fc 66 73 5f 72 65 67 69 73 74 65 72 28 26 77 69 6e  fs_register(&win
101fd 56 66 73 2c 20 31 29 3b 0a 20 20 72 65 74 75 72  Vfs, 1);.  retur
101fe 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a  n SQLITE_OK; .}.
101ff 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
10200 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f  qlite3_os_end(vo
10201 69 64 29 7b 20 0a 20 20 72 65 74 75 72 6e 20 53  id){ .  return S
10202 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e  QLITE_OK;.}..#en
10203 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53  dif /* SQLITE_OS
10204 5f 57 49 4e 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  _WIN */../******
10205 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
10206 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a  os_win.c *******
10207 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10208 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10209 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
1020a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
1020b 69 6c 65 20 62 69 74 76 65 63 2e 63 20 2a 2a 2a  ile bitvec.c ***
1020c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1020d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1020e 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
1020f 30 30 38 20 46 65 62 72 75 61 72 79 20 31 36 0a  008 February 16.
10210 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
10211 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
10212 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
10213 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
10214 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
10215 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
10216 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
10217 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
10218 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
10219 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
1021a 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
1021b 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
1021c 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
1021d 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
1021e 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
1021f 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
10220 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
10221 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
10222 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10223 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10224 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10225 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
10226 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c  * This file impl
10227 65 6d 65 6e 74 73 20 61 6e 20 6f 62 6a 65 63 74  ements an object
10228 20 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 73   that represents
10229 20 61 20 66 69 78 65 64 2d 6c 65 6e 67 74 68 0a   a fixed-length.
1022a 2a 2a 20 62 69 74 6d 61 70 2e 20 20 42 69 74 73  ** bitmap.  Bits
1022b 20 61 72 65 20 6e 75 6d 62 65 72 65 64 20 73 74   are numbered st
1022c 61 72 74 69 6e 67 20 77 69 74 68 20 31 2e 0a 2a  arting with 1..*
1022d 2a 0a 2a 2a 20 41 20 62 69 74 6d 61 70 20 69 73  *.** A bitmap is
1022e 20 75 73 65 64 20 74 6f 20 72 65 63 6f 72 64 20   used to record 
1022f 77 68 69 63 68 20 70 61 67 65 73 20 6f 66 20 61  which pages of a
10230 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68   database file h
10231 61 76 65 20 62 65 65 6e 0a 2a 2a 20 6a 6f 75 72  ave been.** jour
10232 6e 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 61 20  nalled during a 
10233 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 6f 72 20  transaction, or 
10234 77 68 69 63 68 20 70 61 67 65 73 20 68 61 76 65  which pages have
10235 20 74 68 65 20 22 64 6f 6e 74 2d 77 72 69 74 65   the "dont-write
10236 22 0a 2a 2a 20 70 72 6f 70 65 72 74 79 2e 20 20  ".** property.  
10237 55 73 75 61 6c 6c 79 20 6f 6e 6c 79 20 61 20 66  Usually only a f
10238 65 77 20 70 61 67 65 73 20 61 72 65 20 6d 65 65  ew pages are mee
10239 74 20 65 69 74 68 65 72 20 63 6f 6e 64 69 74 69  t either conditi
1023a 6f 6e 2e 0a 2a 2a 20 53 6f 20 74 68 65 20 62 69  on..** So the bi
1023b 74 6d 61 70 20 69 73 20 75 73 75 61 6c 6c 79 20  tmap is usually 
1023c 73 70 61 72 73 65 20 61 6e 64 20 68 61 73 20 6c  sparse and has l
1023d 6f 77 20 63 61 72 64 69 6e 61 6c 69 74 79 2e 0a  ow cardinality..
1023e 2a 2a 20 42 75 74 20 73 6f 6d 65 74 69 6d 65 73  ** But sometimes
1023f 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 77 68   (for example wh
10240 65 6e 20 64 75 72 69 6e 67 20 61 20 44 52 4f 50  en during a DROP
10241 20 6f 66 20 61 20 6c 61 72 67 65 20 74 61 62 6c   of a large tabl
10242 65 29 20 6d 6f 73 74 0a 2a 2a 20 6f 72 20 61 6c  e) most.** or al
10243 6c 20 6f 66 20 74 68 65 20 70 61 67 65 73 20 69  l of the pages i
10244 6e 20 61 20 64 61 74 61 62 61 73 65 20 63 61 6e  n a database can
10245 20 67 65 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 2e   get journalled.
10246 20 20 49 6e 20 74 68 6f 73 65 20 63 61 73 65 73    In those cases
10247 2c 20 0a 2a 2a 20 74 68 65 20 62 69 74 6d 61 70  , .** the bitmap
10248 20 62 65 63 6f 6d 65 73 20 64 65 6e 73 65 20 77   becomes dense w
10249 69 74 68 20 68 69 67 68 20 63 61 72 64 69 6e 61  ith high cardina
1024a 6c 69 74 79 2e 20 20 54 68 65 20 61 6c 67 6f 72  lity.  The algor
1024b 69 74 68 6d 20 6e 65 65 64 73 20 0a 2a 2a 20 74  ithm needs .** t
1024c 6f 20 68 61 6e 64 6c 65 20 62 6f 74 68 20 63 61  o handle both ca
1024d 73 65 73 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20  ses well..**.** 
1024e 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  The size of the 
1024f 62 69 74 6d 61 70 20 69 73 20 66 69 78 65 64 20  bitmap is fixed 
10250 77 68 65 6e 20 74 68 65 20 6f 62 6a 65 63 74 20  when the object 
10251 69 73 20 63 72 65 61 74 65 64 2e 0a 2a 2a 0a 2a  is created..**.*
10252 2a 20 41 6c 6c 20 62 69 74 73 20 61 72 65 20 63  * All bits are c
10253 6c 65 61 72 20 77 68 65 6e 20 74 68 65 20 62 69  lear when the bi
10254 74 6d 61 70 20 69 73 20 63 72 65 61 74 65 64 2e  tmap is created.
10255 20 20 49 6e 64 69 76 69 64 75 61 6c 20 62 69 74    Individual bit
10256 73 0a 2a 2a 20 6d 61 79 20 62 65 20 73 65 74 20  s.** may be set 
10257 6f 72 20 63 6c 65 61 72 65 64 20 6f 6e 65 20 61  or cleared one a
10258 74 20 61 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20  t a time..**.** 
10259 54 65 73 74 20 6f 70 65 72 61 74 69 6f 6e 73 20  Test operations 
1025a 61 72 65 20 61 62 6f 75 74 20 31 30 30 20 74 69  are about 100 ti
1025b 6d 65 73 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 20  mes more common 
1025c 74 68 61 74 20 73 65 74 20 6f 70 65 72 61 74 69  that set operati
1025d 6f 6e 73 2e 0a 2a 2a 20 43 6c 65 61 72 20 6f 70  ons..** Clear op
1025e 65 72 61 74 69 6f 6e 73 20 61 72 65 20 65 78 63  erations are exc
1025f 65 65 64 69 6e 67 6c 79 20 72 61 72 65 2e 20 20  eedingly rare.  
10260 54 68 65 72 65 20 61 72 65 20 75 73 75 61 6c 6c  There are usuall
10261 79 20 62 65 74 77 65 65 6e 0a 2a 2a 20 35 20 61  y between.** 5 a
10262 6e 64 20 35 30 30 20 73 65 74 20 6f 70 65 72 61  nd 500 set opera
10263 74 69 6f 6e 73 20 70 65 72 20 42 69 74 76 65 63  tions per Bitvec
10264 20 6f 62 6a 65 63 74 2c 20 74 68 6f 75 67 68 20   object, though 
10265 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65  the number of se
10266 74 73 20 63 61 6e 0a 2a 2a 20 73 6f 6d 65 74 69  ts can.** someti
10267 6d 65 73 20 67 72 6f 77 20 69 6e 74 6f 20 74 65  mes grow into te
10268 6e 73 20 6f 66 20 74 68 6f 75 73 61 6e 64 73 20  ns of thousands 
10269 6f 72 20 6c 61 72 67 65 72 2e 20 20 54 68 65 20  or larger.  The 
1026a 73 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a 20 42  size of the.** B
1026b 69 74 76 65 63 20 6f 62 6a 65 63 74 20 69 73 20  itvec object is 
1026c 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  the number of pa
1026d 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62  ges in the datab
1026e 61 73 65 20 66 69 6c 65 20 61 74 20 74 68 65 0a  ase file at the.
1026f 2a 2a 20 73 74 61 72 74 20 6f 66 20 61 20 74 72  ** start of a tr
10270 61 6e 73 61 63 74 69 6f 6e 2c 20 61 6e 64 20 69  ansaction, and i
10271 73 20 74 68 75 73 20 75 73 75 61 6c 6c 79 20 6c  s thus usually l
10272 65 73 73 20 74 68 61 6e 20 61 20 66 65 77 20 74  ess than a few t
10273 68 6f 75 73 61 6e 64 2c 0a 2a 2a 20 62 75 74 20  housand,.** but 
10274 63 61 6e 20 62 65 20 61 73 20 6c 61 72 67 65 20  can be as large 
10275 61 73 20 32 20 62 69 6c 6c 69 6f 6e 20 66 6f 72  as 2 billion for
10276 20 61 20 72 65 61 6c 6c 79 20 62 69 67 20 64 61   a really big da
10277 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 40 28  tabase..**.** @(
10278 23 29 20 24 49 64 3a 20 62 69 74 76 65 63 2e 63  #) $Id: bitvec.c
10279 2c 76 20 31 2e 31 37 20 32 30 30 39 2f 30 37 2f  ,v 1.17 2009/07/
1027a 32 35 20 31 37 3a 33 33 3a 32 36 20 64 72 68 20  25 17:33:26 drh 
1027b 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 53 69 7a  Exp $.*/../* Siz
1027c 65 20 6f 66 20 74 68 65 20 42 69 74 76 65 63 20  e of the Bitvec 
1027d 73 74 72 75 63 74 75 72 65 20 69 6e 20 62 79 74  structure in byt
1027e 65 73 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42  es. */.#define B
1027f 49 54 56 45 43 5f 53 5a 20 20 20 20 20 20 20 20  ITVEC_SZ        
10280 28 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 2a 31  (sizeof(void*)*1
10281 32 38 29 20 20 2f 2a 20 35 31 32 20 6f 6e 20 33  28)  /* 512 on 3
10282 32 62 69 74 2e 20 20 31 30 32 34 20 6f 6e 20 36  2bit.  1024 on 6
10283 34 62 69 74 20 2a 2f 0a 0a 2f 2a 20 52 6f 75 6e  4bit */../* Roun
10284 64 20 74 68 65 20 75 6e 69 6f 6e 20 73 69 7a 65  d the union size
10285 20 64 6f 77 6e 20 74 6f 20 74 68 65 20 6e 65 61   down to the nea
10286 72 65 73 74 20 70 6f 69 6e 74 65 72 20 62 6f 75  rest pointer bou
10287 6e 64 61 72 79 2c 20 73 69 6e 63 65 20 74 68 61  ndary, since tha
10288 74 27 73 20 68 6f 77 20 0a 2a 2a 20 69 74 20 77  t's how .** it w
10289 69 6c 6c 20 62 65 20 61 6c 69 67 6e 65 64 20 77  ill be aligned w
1028a 69 74 68 69 6e 20 74 68 65 20 42 69 74 76 65 63  ithin the Bitvec
1028b 20 73 74 72 75 63 74 2e 20 2a 2f 0a 23 64 65 66   struct. */.#def
1028c 69 6e 65 20 42 49 54 56 45 43 5f 55 53 49 5a 45  ine BITVEC_USIZE
1028d 20 20 20 20 20 28 28 28 42 49 54 56 45 43 5f 53       (((BITVEC_S
1028e 5a 2d 28 33 2a 73 69 7a 65 6f 66 28 75 33 32 29  Z-(3*sizeof(u32)
1028f 29 29 2f 73 69 7a 65 6f 66 28 42 69 74 76 65 63  ))/sizeof(Bitvec
10290 2a 29 29 2a 73 69 7a 65 6f 66 28 42 69 74 76 65  *))*sizeof(Bitve
10291 63 2a 29 29 0a 0a 2f 2a 20 54 79 70 65 20 6f 66  c*))../* Type of
10292 20 74 68 65 20 61 72 72 61 79 20 22 65 6c 65 6d   the array "elem
10293 65 6e 74 22 20 66 6f 72 20 74 68 65 20 62 69 74  ent" for the bit
10294 6d 61 70 20 72 65 70 72 65 73 65 6e 74 61 74 69  map representati
10295 6f 6e 2e 20 0a 2a 2a 20 53 68 6f 75 6c 64 20 62  on. .** Should b
10296 65 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2c 20  e a power of 2, 
10297 61 6e 64 20 69 64 65 61 6c 6c 79 2c 20 65 76 65  and ideally, eve
10298 6e 6c 79 20 64 69 76 69 64 65 20 69 6e 74 6f 20  nly divide into 
10299 42 49 54 56 45 43 5f 55 53 49 5a 45 2e 20 0a 2a  BITVEC_USIZE. .*
1029a 2a 20 53 65 74 74 69 6e 67 20 74 68 69 73 20 74  * Setting this t
1029b 6f 20 74 68 65 20 22 6e 61 74 75 72 61 6c 20 77  o the "natural w
1029c 6f 72 64 22 20 73 69 7a 65 20 6f 66 20 79 6f 75  ord" size of you
1029d 72 20 43 50 55 20 6d 61 79 20 69 6d 70 72 6f 76  r CPU may improv
1029e 65 0a 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65  e.** performance
1029f 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54  . */.#define BIT
102a0 56 45 43 5f 54 45 4c 45 4d 20 20 20 20 20 75 38  VEC_TELEM     u8
102a1 0a 2f 2a 20 53 69 7a 65 2c 20 69 6e 20 62 69 74  ./* Size, in bit
102a2 73 2c 20 6f 66 20 74 68 65 20 62 69 74 6d 61 70  s, of the bitmap
102a3 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f 0a 23 64 65   element. */.#de
102a4 66 69 6e 65 20 42 49 54 56 45 43 5f 53 5a 45 4c  fine BITVEC_SZEL
102a5 45 4d 20 20 20 20 38 0a 2f 2a 20 4e 75 6d 62 65  EM    8./* Numbe
102a6 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e  r of elements in
102a7 20 61 20 62 69 74 6d 61 70 20 61 72 72 61 79 2e   a bitmap array.
102a8 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56   */.#define BITV
102a9 45 43 5f 4e 45 4c 45 4d 20 20 20 20 20 28 42 49  EC_NELEM     (BI
102aa 54 56 45 43 5f 55 53 49 5a 45 2f 73 69 7a 65 6f  TVEC_USIZE/sizeo
102ab 66 28 42 49 54 56 45 43 5f 54 45 4c 45 4d 29 29  f(BITVEC_TELEM))
102ac 0a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 69  ./* Number of bi
102ad 74 73 20 69 6e 20 74 68 65 20 62 69 74 6d 61 70  ts in the bitmap
102ae 20 61 72 72 61 79 2e 20 2a 2f 0a 23 64 65 66 69   array. */.#defi
102af 6e 65 20 42 49 54 56 45 43 5f 4e 42 49 54 20 20  ne BITVEC_NBIT  
102b0 20 20 20 20 28 42 49 54 56 45 43 5f 4e 45 4c 45      (BITVEC_NELE
102b1 4d 2a 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 29  M*BITVEC_SZELEM)
102b2 0a 0a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75  ../* Number of u
102b3 33 32 20 76 61 6c 75 65 73 20 69 6e 20 68 61 73  32 values in has
102b4 68 20 74 61 62 6c 65 2e 20 2a 2f 0a 23 64 65 66  h table. */.#def
102b5 69 6e 65 20 42 49 54 56 45 43 5f 4e 49 4e 54 20  ine BITVEC_NINT 
102b6 20 20 20 20 20 28 42 49 54 56 45 43 5f 55 53 49       (BITVEC_USI
102b7 5a 45 2f 73 69 7a 65 6f 66 28 75 33 32 29 29 0a  ZE/sizeof(u32)).
102b8 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  /* Maximum numbe
102b9 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
102ba 68 61 73 68 20 74 61 62 6c 65 20 62 65 66 6f 72  hash table befor
102bb 65 20 0a 2a 2a 20 73 75 62 2d 64 69 76 69 64 69  e .** sub-dividi
102bc 6e 67 20 61 6e 64 20 72 65 2d 68 61 73 68 69 6e  ng and re-hashin
102bd 67 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49  g. */.#define BI
102be 54 56 45 43 5f 4d 58 48 41 53 48 20 20 20 20 28  TVEC_MXHASH    (
102bf 42 49 54 56 45 43 5f 4e 49 4e 54 2f 32 29 0a 2f  BITVEC_NINT/2)./
102c0 2a 20 48 61 73 68 69 6e 67 20 66 75 6e 63 74 69  * Hashing functi
102c1 6f 6e 20 66 6f 72 20 74 68 65 20 61 48 61 73 68  on for the aHash
102c2 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e   representation.
102c3 0a 2a 2a 20 45 6d 70 69 72 69 63 61 6c 20 74 65  .** Empirical te
102c4 73 74 69 6e 67 20 73 68 6f 77 65 64 20 74 68 61  sting showed tha
102c5 74 20 74 68 65 20 2a 33 37 20 6d 75 6c 74 69 70  t the *37 multip
102c6 6c 69 65 72 20 0a 2a 2a 20 28 61 6e 20 61 72 62  lier .** (an arb
102c7 69 74 72 61 72 79 20 70 72 69 6d 65 29 69 6e 20  itrary prime)in 
102c8 74 68 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f  the hash functio
102c9 6e 20 70 72 6f 76 69 64 65 64 20 0a 2a 2a 20 6e  n provided .** n
102ca 6f 20 66 65 77 65 72 20 63 6f 6c 6c 69 73 69 6f  o fewer collisio
102cb 6e 73 20 74 68 61 6e 20 74 68 65 20 6e 6f 2d 6f  ns than the no-o
102cc 70 20 2a 31 2e 20 2a 2f 0a 23 64 65 66 69 6e 65  p *1. */.#define
102cd 20 42 49 54 56 45 43 5f 48 41 53 48 28 58 29 20   BITVEC_HASH(X) 
102ce 20 20 28 28 28 58 29 2a 31 29 25 42 49 54 56 45    (((X)*1)%BITVE
102cf 43 5f 4e 49 4e 54 29 0a 0a 23 64 65 66 69 6e 65  C_NINT)..#define
102d0 20 42 49 54 56 45 43 5f 4e 50 54 52 20 20 20 20   BITVEC_NPTR    
102d1 20 20 28 42 49 54 56 45 43 5f 55 53 49 5a 45 2f    (BITVEC_USIZE/
102d2 73 69 7a 65 6f 66 28 42 69 74 76 65 63 20 2a 29  sizeof(Bitvec *)
102d3 29 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 62 69 74 6d  ).../*.** A bitm
102d4 61 70 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63  ap is an instanc
102d5 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
102d6 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  ng structure..**
102d7 0a 2a 2a 20 54 68 69 73 20 62 69 74 6d 61 70 20  .** This bitmap 
102d8 72 65 63 6f 72 64 73 20 74 68 65 20 65 78 69 73  records the exis
102d9 74 61 6e 63 65 20 6f 66 20 7a 65 72 6f 20 6f 72  tance of zero or
102da 20 6d 6f 72 65 20 62 69 74 73 0a 2a 2a 20 77 69   more bits.** wi
102db 74 68 20 76 61 6c 75 65 73 20 62 65 74 77 65 65  th values betwee
102dc 6e 20 31 20 61 6e 64 20 69 53 69 7a 65 2c 20 69  n 1 and iSize, i
102dd 6e 63 6c 75 73 69 76 65 2e 0a 2a 2a 0a 2a 2a 20  nclusive..**.** 
102de 54 68 65 72 65 20 61 72 65 20 74 68 72 65 65 20  There are three 
102df 70 6f 73 73 69 62 6c 65 20 72 65 70 72 65 73 65  possible represe
102e0 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20  ntations of the 
102e1 62 69 74 6d 61 70 2e 0a 2a 2a 20 49 66 20 69 53  bitmap..** If iS
102e2 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54  ize<=BITVEC_NBIT
102e3 2c 20 74 68 65 6e 20 42 69 74 76 65 63 2e 75 2e  , then Bitvec.u.
102e4 61 42 69 74 6d 61 70 5b 5d 20 69 73 20 61 20 73  aBitmap[] is a s
102e5 74 72 61 69 67 68 74 0a 2a 2a 20 62 69 74 6d 61  traight.** bitma
102e6 70 2e 20 20 54 68 65 20 6c 65 61 73 74 20 73 69  p.  The least si
102e7 67 6e 69 66 69 63 61 6e 74 20 62 69 74 20 69 73  gnificant bit is
102e8 20 62 69 74 20 31 2e 0a 2a 2a 0a 2a 2a 20 49 66   bit 1..**.** If
102e9 20 69 53 69 7a 65 3e 42 49 54 56 45 43 5f 4e 42   iSize>BITVEC_NB
102ea 49 54 20 61 6e 64 20 69 44 69 76 69 73 6f 72 3d  IT and iDivisor=
102eb 3d 30 20 74 68 65 6e 20 42 69 74 76 65 63 2e 75  =0 then Bitvec.u
102ec 2e 61 48 61 73 68 5b 5d 20 69 73 0a 2a 2a 20 61  .aHash[] is.** a
102ed 20 68 61 73 68 20 74 61 62 6c 65 20 74 68 61 74   hash table that
102ee 20 77 69 6c 6c 20 68 6f 6c 64 20 75 70 20 74 6f   will hold up to
102ef 20 42 49 54 56 45 43 5f 4d 58 48 41 53 48 20 64   BITVEC_MXHASH d
102f0 69 73 74 69 6e 63 74 20 76 61 6c 75 65 73 2e 0a  istinct values..
102f1 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  **.** Otherwise,
102f2 20 74 68 65 20 76 61 6c 75 65 20 69 20 69 73 20   the value i is 
102f3 72 65 64 69 72 65 63 74 65 64 20 69 6e 74 6f 20  redirected into 
102f4 6f 6e 65 20 6f 66 20 42 49 54 56 45 43 5f 4e 50  one of BITVEC_NP
102f5 54 52 0a 2a 2a 20 73 75 62 2d 62 69 74 6d 61 70  TR.** sub-bitmap
102f6 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  s pointed to by 
102f7 42 69 74 76 65 63 2e 75 2e 61 70 53 75 62 5b 5d  Bitvec.u.apSub[]
102f8 2e 20 20 45 61 63 68 20 73 75 62 62 69 74 6d 61  .  Each subbitma
102f9 70 0a 2a 2a 20 68 61 6e 64 6c 65 73 20 75 70 20  p.** handles up 
102fa 74 6f 20 69 44 69 76 69 73 6f 72 20 73 65 70 61  to iDivisor sepa
102fb 72 61 74 65 20 76 61 6c 75 65 73 20 6f 66 20 69  rate values of i
102fc 2e 20 20 61 70 53 75 62 5b 30 5d 20 68 6f 6c 64  .  apSub[0] hold
102fd 73 0a 2a 2a 20 76 61 6c 75 65 73 20 62 65 74 77  s.** values betw
102fe 65 65 6e 20 31 20 61 6e 64 20 69 44 69 76 69 73  een 1 and iDivis
102ff 6f 72 2e 20 20 61 70 53 75 62 5b 31 5d 20 68 6f  or.  apSub[1] ho
10300 6c 64 73 20 76 61 6c 75 65 73 20 62 65 74 77 65  lds values betwe
10301 65 6e 0a 2a 2a 20 69 44 69 76 69 73 6f 72 2b 31  en.** iDivisor+1
10302 20 61 6e 64 20 32 2a 69 44 69 76 69 73 6f 72 2e   and 2*iDivisor.
10303 20 20 61 70 53 75 62 5b 4e 5d 20 68 6f 6c 64 73    apSub[N] holds
10304 20 76 61 6c 75 65 73 20 62 65 74 77 65 65 6e 0a   values between.
10305 2a 2a 20 4e 2a 69 44 69 76 69 73 6f 72 2b 31 20  ** N*iDivisor+1 
10306 61 6e 64 20 28 4e 2b 31 29 2a 69 44 69 76 69 73  and (N+1)*iDivis
10307 6f 72 2e 20 20 45 61 63 68 20 73 75 62 62 69 74  or.  Each subbit
10308 6d 61 70 20 69 73 20 6e 6f 72 6d 61 6c 69 7a 65  map is normalize
10309 64 0a 2a 2a 20 74 6f 20 68 6f 6c 64 20 64 65 61  d.** to hold dea
1030a 6c 20 77 69 74 68 20 76 61 6c 75 65 73 20 62 65  l with values be
1030b 74 77 65 65 6e 20 31 20 61 6e 64 20 69 44 69 76  tween 1 and iDiv
1030c 69 73 6f 72 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  isor..*/.struct 
1030d 42 69 74 76 65 63 20 7b 0a 20 20 75 33 32 20 69  Bitvec {.  u32 i
1030e 53 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20 4d 61  Size;      /* Ma
1030f 78 69 6d 75 6d 20 62 69 74 20 69 6e 64 65 78 2e  ximum bit index.
10310 20 20 4d 61 78 20 69 53 69 7a 65 20 69 73 20 34    Max iSize is 4
10311 2c 32 39 34 2c 39 36 37 2c 32 39 36 2e 20 2a 2f  ,294,967,296. */
10312 0a 20 20 75 33 32 20 6e 53 65 74 3b 20 20 20 20  .  u32 nSet;    
10313 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
10314 62 69 74 73 20 74 68 61 74 20 61 72 65 20 73 65  bits that are se
10315 74 20 2d 20 6f 6e 6c 79 20 76 61 6c 69 64 20 66  t - only valid f
10316 6f 72 20 61 48 61 73 68 0a 20 20 20 20 20 20 20  or aHash.       
10317 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 65 6c             ** el
10318 65 6d 65 6e 74 2e 20 20 4d 61 78 20 69 73 20 42  ement.  Max is B
10319 49 54 56 45 43 5f 4e 49 4e 54 2e 20 20 46 6f 72  ITVEC_NINT.  For
1031a 20 42 49 54 56 45 43 5f 53 5a 20 6f 66 20 35 31   BITVEC_SZ of 51
1031b 32 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  2,.             
1031c 20 20 20 20 20 2a 2a 20 74 68 69 73 20 77 6f 75       ** this wou
1031d 6c 64 20 62 65 20 31 32 35 2e 20 2a 2f 0a 20 20  ld be 125. */.  
1031e 75 33 32 20 69 44 69 76 69 73 6f 72 3b 20 20 20  u32 iDivisor;   
1031f 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 69 74  /* Number of bit
10320 73 20 68 61 6e 64 6c 65 64 20 62 79 20 65 61 63  s handled by eac
10321 68 20 61 70 53 75 62 5b 5d 20 65 6e 74 72 79 2e  h apSub[] entry.
10322 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
10323 20 20 20 20 20 20 2f 2a 20 53 68 6f 75 6c 64 20        /* Should 
10324 3e 3d 30 20 66 6f 72 20 61 70 53 75 62 20 65 6c  >=0 for apSub el
10325 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 20 20  ement. */.      
10326 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
10327 61 78 20 69 44 69 76 69 73 6f 72 20 69 73 20 6d  ax iDivisor is m
10328 61 78 28 75 33 32 29 20 2f 20 42 49 54 56 45 43  ax(u32) / BITVEC
10329 5f 4e 50 54 52 20 2b 20 31 2e 20 20 2a 2f 0a 20  _NPTR + 1.  */. 
1032a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1032b 20 2f 2a 20 46 6f 72 20 61 20 42 49 54 56 45 43   /* For a BITVEC
1032c 5f 53 5a 20 6f 66 20 35 31 32 2c 20 74 68 69 73  _SZ of 512, this
1032d 20 77 6f 75 6c 64 20 62 65 20 33 34 2c 33 35 39   would be 34,359
1032e 2c 37 33 39 2e 20 2a 2f 0a 20 20 75 6e 69 6f 6e  ,739. */.  union
1032f 20 7b 0a 20 20 20 20 42 49 54 56 45 43 5f 54 45   {.    BITVEC_TE
10330 4c 45 4d 20 61 42 69 74 6d 61 70 5b 42 49 54 56  LEM aBitmap[BITV
10331 45 43 5f 4e 45 4c 45 4d 5d 3b 20 20 20 20 2f 2a  EC_NELEM];    /*
10332 20 42 69 74 6d 61 70 20 72 65 70 72 65 73 65 6e   Bitmap represen
10333 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 75 33  tation */.    u3
10334 32 20 61 48 61 73 68 5b 42 49 54 56 45 43 5f 4e  2 aHash[BITVEC_N
10335 49 4e 54 5d 3b 20 20 20 20 20 20 2f 2a 20 48 61  INT];      /* Ha
10336 73 68 20 74 61 62 6c 65 20 72 65 70 72 65 73 65  sh table represe
10337 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 42  ntation */.    B
10338 69 74 76 65 63 20 2a 61 70 53 75 62 5b 42 49 54  itvec *apSub[BIT
10339 56 45 43 5f 4e 50 54 52 5d 3b 20 20 2f 2a 20 52  VEC_NPTR];  /* R
1033a 65 63 75 72 73 69 76 65 20 72 65 70 72 65 73 65  ecursive represe
1033b 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 7d 20 75  ntation */.  } u
1033c 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61  ;.};../*.** Crea
1033d 74 65 20 61 20 6e 65 77 20 62 69 74 6d 61 70 20  te a new bitmap 
1033e 6f 62 6a 65 63 74 20 61 62 6c 65 20 74 6f 20 68  object able to h
1033f 61 6e 64 6c 65 20 62 69 74 73 20 62 65 74 77 65  andle bits betwe
10340 65 6e 20 30 20 61 6e 64 20 69 53 69 7a 65 2c 0a  en 0 and iSize,.
10341 2a 2a 20 69 6e 63 6c 75 73 69 76 65 2e 20 20 52  ** inclusive.  R
10342 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
10343 74 6f 20 74 68 65 20 6e 65 77 20 6f 62 6a 65 63  to the new objec
10344 74 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20  t.  Return NULL 
10345 69 66 20 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61  if .** malloc fa
10346 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ils..*/.SQLITE_P
10347 52 49 56 41 54 45 20 42 69 74 76 65 63 20 2a 73  RIVATE Bitvec *s
10348 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61  qlite3BitvecCrea
10349 74 65 28 75 33 32 20 69 53 69 7a 65 29 7b 0a 20  te(u32 iSize){. 
1034a 20 42 69 74 76 65 63 20 2a 70 3b 0a 20 20 61 73   Bitvec *p;.  as
1034b 73 65 72 74 28 20 73 69 7a 65 6f 66 28 2a 70 29  sert( sizeof(*p)
1034c 3d 3d 42 49 54 56 45 43 5f 53 5a 20 29 3b 0a 20  ==BITVEC_SZ );. 
1034d 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c   p = sqlite3Mall
1034e 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a  ocZero( sizeof(*
1034f 70 29 20 29 3b 0a 20 20 69 66 28 20 70 20 29 7b  p) );.  if( p ){
10350 0a 20 20 20 20 70 2d 3e 69 53 69 7a 65 20 3d 20  .    p->iSize = 
10351 69 53 69 7a 65 3b 0a 20 20 7d 0a 20 20 72 65 74  iSize;.  }.  ret
10352 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn p;.}../*.** 
10353 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  Check to see if 
10354 74 68 65 20 69 2d 74 68 20 62 69 74 20 69 73 20  the i-th bit is 
10355 73 65 74 2e 20 20 52 65 74 75 72 6e 20 74 72 75  set.  Return tru
10356 65 20 6f 72 20 66 61 6c 73 65 2e 0a 2a 2a 20 49  e or false..** I
10357 66 20 70 20 69 73 20 4e 55 4c 4c 20 28 69 66 20  f p is NULL (if 
10358 74 68 65 20 62 69 74 6d 61 70 20 68 61 73 20 6e  the bitmap has n
10359 6f 74 20 62 65 65 6e 20 63 72 65 61 74 65 64 29  ot been created)
1035a 20 6f 72 20 69 66 0a 2a 2a 20 69 20 69 73 20 6f   or if.** i is o
1035b 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 74 68 65  ut of range, the
1035c 6e 20 72 65 74 75 72 6e 20 66 61 6c 73 65 2e 0a  n return false..
1035d 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1035e 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74  E int sqlite3Bit
1035f 76 65 63 54 65 73 74 28 42 69 74 76 65 63 20 2a  vecTest(Bitvec *
10360 70 2c 20 75 33 32 20 69 29 7b 0a 20 20 69 66 28  p, u32 i){.  if(
10361 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30   p==0 ) return 0
10362 3b 0a 20 20 69 66 28 20 69 3e 70 2d 3e 69 53 69  ;.  if( i>p->iSi
10363 7a 65 20 7c 7c 20 69 3d 3d 30 20 29 20 72 65 74  ze || i==0 ) ret
10364 75 72 6e 20 30 3b 0a 20 20 69 2d 2d 3b 0a 20 20  urn 0;.  i--;.  
10365 77 68 69 6c 65 28 20 70 2d 3e 69 44 69 76 69 73  while( p->iDivis
10366 6f 72 20 29 7b 0a 20 20 20 20 75 33 32 20 62 69  or ){.    u32 bi
10367 6e 20 3d 20 69 2f 70 2d 3e 69 44 69 76 69 73 6f  n = i/p->iDiviso
10368 72 3b 0a 20 20 20 20 69 20 3d 20 69 25 70 2d 3e  r;.    i = i%p->
10369 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 70 20  iDivisor;.    p 
1036a 3d 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e  = p->u.apSub[bin
1036b 5d 3b 0a 20 20 20 20 69 66 20 28 21 70 29 20 7b  ];.    if (!p) {
1036c 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  .      return 0;
1036d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
1036e 20 70 2d 3e 69 53 69 7a 65 3c 3d 42 49 54 56 45   p->iSize<=BITVE
1036f 43 5f 4e 42 49 54 20 29 7b 0a 20 20 20 20 72 65  C_NBIT ){.    re
10370 74 75 72 6e 20 28 70 2d 3e 75 2e 61 42 69 74 6d  turn (p->u.aBitm
10371 61 70 5b 69 2f 42 49 54 56 45 43 5f 53 5a 45 4c  ap[i/BITVEC_SZEL
10372 45 4d 5d 20 26 20 28 31 3c 3c 28 69 26 28 42 49  EM] & (1<<(i&(BI
10373 54 56 45 43 5f 53 5a 45 4c 45 4d 2d 31 29 29 29  TVEC_SZELEM-1)))
10374 29 21 3d 30 3b 0a 20 20 7d 20 65 6c 73 65 7b 0a  )!=0;.  } else{.
10375 20 20 20 20 75 33 32 20 68 20 3d 20 42 49 54 56      u32 h = BITV
10376 45 43 5f 48 41 53 48 28 69 2b 2b 29 3b 0a 20 20  EC_HASH(i++);.  
10377 20 20 77 68 69 6c 65 28 20 70 2d 3e 75 2e 61 48    while( p->u.aH
10378 61 73 68 5b 68 5d 20 29 7b 0a 20 20 20 20 20 20  ash[h] ){.      
10379 69 66 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68  if( p->u.aHash[h
1037a 5d 3d 3d 69 20 29 20 72 65 74 75 72 6e 20 31 3b  ]==i ) return 1;
1037b 0a 20 20 20 20 20 20 68 20 3d 20 28 68 2b 31 29  .      h = (h+1)
1037c 20 25 20 42 49 54 56 45 43 5f 4e 49 4e 54 3b 0a   % BITVEC_NINT;.
1037d 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
1037e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a   0;.  }.}../*.**
1037f 20 53 65 74 20 74 68 65 20 69 2d 74 68 20 62 69   Set the i-th bi
10380 74 2e 20 20 52 65 74 75 72 6e 20 30 20 6f 6e 20  t.  Return 0 on 
10381 73 75 63 63 65 73 73 20 61 6e 64 20 61 6e 20 65  success and an e
10382 72 72 6f 72 20 63 6f 64 65 20 69 66 0a 2a 2a 20  rror code if.** 
10383 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20 77 72  anything goes wr
10384 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ong..**.** This 
10385 72 6f 75 74 69 6e 65 20 6d 69 67 68 74 20 63 61  routine might ca
10386 75 73 65 20 73 75 62 2d 62 69 74 6d 61 70 73 20  use sub-bitmaps 
10387 74 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e  to be allocated.
10388 20 20 46 61 69 6c 69 6e 67 0a 2a 2a 20 74 6f 20    Failing.** to 
10389 67 65 74 20 74 68 65 20 6d 65 6d 6f 72 79 20 6e  get the memory n
1038a 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 20 74 68  eeded to hold th
1038b 65 20 73 75 62 2d 62 69 74 6d 61 70 20 69 73 20  e sub-bitmap is 
1038c 74 68 65 20 6f 6e 6c 79 0a 2a 2a 20 74 68 61 74  the only.** that
1038d 20 63 61 6e 20 67 6f 20 77 72 6f 6e 67 20 77 69   can go wrong wi
1038e 74 68 20 61 6e 20 69 6e 73 65 72 74 2c 20 61 73  th an insert, as
1038f 73 75 6d 69 6e 67 20 70 20 61 6e 64 20 69 20 61  suming p and i a
10390 72 65 20 76 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20  re valid..**.** 
10391 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63  The calling func
10392 74 69 6f 6e 20 6d 75 73 74 20 65 6e 73 75 72 65  tion must ensure
10393 20 74 68 61 74 20 70 20 69 73 20 61 20 76 61 6c   that p is a val
10394 69 64 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74  id Bitvec object
10395 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20 74 68 65  .** and that the
10396 20 76 61 6c 75 65 20 66 6f 72 20 22 69 22 20 69   value for "i" i
10397 73 20 77 69 74 68 69 6e 20 72 61 6e 67 65 20 6f  s within range o
10398 66 20 74 68 65 20 42 69 74 76 65 63 20 6f 62 6a  f the Bitvec obj
10399 65 63 74 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73  ect..** Otherwis
1039a 65 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69  e the behavior i
1039b 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a  s undefined..*/.
1039c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1039d 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76 65 63  nt sqlite3Bitvec
1039e 53 65 74 28 42 69 74 76 65 63 20 2a 70 2c 20 75  Set(Bitvec *p, u
1039f 33 32 20 69 29 7b 0a 20 20 75 33 32 20 68 3b 0a  32 i){.  u32 h;.
103a0 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74    if( p==0 ) ret
103a1 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
103a2 20 61 73 73 65 72 74 28 20 69 3e 30 20 29 3b 0a   assert( i>0 );.
103a3 20 20 61 73 73 65 72 74 28 20 69 3c 3d 70 2d 3e    assert( i<=p->
103a4 69 53 69 7a 65 20 29 3b 0a 20 20 69 2d 2d 3b 0a  iSize );.  i--;.
103a5 20 20 77 68 69 6c 65 28 28 70 2d 3e 69 53 69 7a    while((p->iSiz
103a6 65 20 3e 20 42 49 54 56 45 43 5f 4e 42 49 54 29  e > BITVEC_NBIT)
103a7 20 26 26 20 70 2d 3e 69 44 69 76 69 73 6f 72 29   && p->iDivisor)
103a8 20 7b 0a 20 20 20 20 75 33 32 20 62 69 6e 20 3d   {.    u32 bin =
103a9 20 69 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a   i/p->iDivisor;.
103aa 20 20 20 20 69 20 3d 20 69 25 70 2d 3e 69 44 69      i = i%p->iDi
103ab 76 69 73 6f 72 3b 0a 20 20 20 20 69 66 28 20 70  visor;.    if( p
103ac 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3d 3d  ->u.apSub[bin]==
103ad 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 75 2e  0 ){.      p->u.
103ae 61 70 53 75 62 5b 62 69 6e 5d 20 3d 20 73 71 6c  apSub[bin] = sql
103af 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65  ite3BitvecCreate
103b0 28 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 3b  ( p->iDivisor );
103b1 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 75 2e  .      if( p->u.
103b2 61 70 53 75 62 5b 62 69 6e 5d 3d 3d 30 20 29 20  apSub[bin]==0 ) 
103b3 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
103b4 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  MEM;.    }.    p
103b5 20 3d 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69   = p->u.apSub[bi
103b6 6e 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d  n];.  }.  if( p-
103b7 3e 69 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e  >iSize<=BITVEC_N
103b8 42 49 54 20 29 7b 0a 20 20 20 20 70 2d 3e 75 2e  BIT ){.    p->u.
103b9 61 42 69 74 6d 61 70 5b 69 2f 42 49 54 56 45 43  aBitmap[i/BITVEC
103ba 5f 53 5a 45 4c 45 4d 5d 20 7c 3d 20 31 20 3c 3c  _SZELEM] |= 1 <<
103bb 20 28 69 26 28 42 49 54 56 45 43 5f 53 5a 45 4c   (i&(BITVEC_SZEL
103bc 45 4d 2d 31 29 29 3b 0a 20 20 20 20 72 65 74 75  EM-1));.    retu
103bd 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
103be 7d 0a 20 20 68 20 3d 20 42 49 54 56 45 43 5f 48  }.  h = BITVEC_H
103bf 41 53 48 28 69 2b 2b 29 3b 0a 20 20 2f 2a 20 69  ASH(i++);.  /* i
103c0 66 20 74 68 65 72 65 20 77 61 73 6e 27 74 20 61  f there wasn't a
103c1 20 68 61 73 68 20 63 6f 6c 6c 69 73 69 6f 6e 2c   hash collision,
103c2 20 61 6e 64 20 74 68 69 73 20 64 6f 65 73 6e 27   and this doesn'
103c3 74 20 2a 2f 0a 20 20 2f 2a 20 63 6f 6d 70 6c 65  t */.  /* comple
103c4 74 65 6c 79 20 66 69 6c 6c 20 74 68 65 20 68 61  tely fill the ha
103c5 73 68 2c 20 74 68 65 6e 20 6a 75 73 74 20 61 64  sh, then just ad
103c6 64 20 69 74 20 77 69 74 68 6f 75 74 20 2a 2f 0a  d it without */.
103c7 20 20 2f 2a 20 77 6f 72 72 69 6e 67 20 61 62 6f    /* worring abo
103c8 75 74 20 73 75 62 2d 64 69 76 69 64 69 6e 67 20  ut sub-dividing 
103c9 61 6e 64 20 72 65 2d 68 61 73 68 69 6e 67 2e 20  and re-hashing. 
103ca 2a 2f 0a 20 20 69 66 28 20 21 70 2d 3e 75 2e 61  */.  if( !p->u.a
103cb 48 61 73 68 5b 68 5d 20 29 7b 0a 20 20 20 20 69  Hash[h] ){.    i
103cc 66 20 28 70 2d 3e 6e 53 65 74 3c 28 42 49 54 56  f (p->nSet<(BITV
103cd 45 43 5f 4e 49 4e 54 2d 31 29 29 20 7b 0a 20 20  EC_NINT-1)) {.  
103ce 20 20 20 20 67 6f 74 6f 20 62 69 74 76 65 63 5f      goto bitvec_
103cf 73 65 74 5f 65 6e 64 3b 0a 20 20 20 20 7d 20 65  set_end;.    } e
103d0 6c 73 65 20 7b 0a 20 20 20 20 20 20 67 6f 74 6f  lse {.      goto
103d1 20 62 69 74 76 65 63 5f 73 65 74 5f 72 65 68 61   bitvec_set_reha
103d2 73 68 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  sh;.    }.  }.  
103d3 2f 2a 20 74 68 65 72 65 20 77 61 73 20 61 20 63  /* there was a c
103d4 6f 6c 6c 69 73 69 6f 6e 2c 20 63 68 65 63 6b 20  ollision, check 
103d5 74 6f 20 73 65 65 20 69 66 20 69 74 27 73 20 61  to see if it's a
103d6 6c 72 65 61 64 79 20 2a 2f 0a 20 20 2f 2a 20 69  lready */.  /* i
103d7 6e 20 68 61 73 68 2c 20 69 66 20 6e 6f 74 2c 20  n hash, if not, 
103d8 74 72 79 20 74 6f 20 66 69 6e 64 20 61 20 73 70  try to find a sp
103d9 6f 74 20 66 6f 72 20 69 74 20 2a 2f 0a 20 20 64  ot for it */.  d
103da 6f 20 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 75  o {.    if( p->u
103db 2e 61 48 61 73 68 5b 68 5d 3d 3d 69 20 29 20 72  .aHash[h]==i ) r
103dc 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
103dd 0a 20 20 20 20 68 2b 2b 3b 0a 20 20 20 20 69 66  .    h++;.    if
103de 28 20 68 3e 3d 42 49 54 56 45 43 5f 4e 49 4e 54  ( h>=BITVEC_NINT
103df 20 29 20 68 20 3d 20 30 3b 0a 20 20 7d 20 77 68   ) h = 0;.  } wh
103e0 69 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b  ile( p->u.aHash[
103e1 68 5d 20 29 3b 0a 20 20 2f 2a 20 77 65 20 64 69  h] );.  /* we di
103e2 64 6e 27 74 20 66 69 6e 64 20 69 74 20 69 6e 20  dn't find it in 
103e3 74 68 65 20 68 61 73 68 2e 20 20 68 20 70 6f 69  the hash.  h poi
103e4 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72 73 74  nts to the first
103e5 20 2a 2f 0a 20 20 2f 2a 20 61 76 61 69 6c 61 62   */.  /* availab
103e6 6c 65 20 66 72 65 65 20 73 70 6f 74 2e 20 63 68  le free spot. ch
103e7 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68  eck to see if th
103e8 69 73 20 69 73 20 67 6f 69 6e 67 20 74 6f 20 2a  is is going to *
103e9 2f 0a 20 20 2f 2a 20 6d 61 6b 65 20 6f 75 72 20  /.  /* make our 
103ea 68 61 73 68 20 74 6f 6f 20 22 66 75 6c 6c 22 2e  hash too "full".
103eb 20 20 2a 2f 0a 62 69 74 76 65 63 5f 73 65 74 5f    */.bitvec_set_
103ec 72 65 68 61 73 68 3a 0a 20 20 69 66 28 20 70 2d  rehash:.  if( p-
103ed 3e 6e 53 65 74 3e 3d 42 49 54 56 45 43 5f 4d 58  >nSet>=BITVEC_MX
103ee 48 41 53 48 20 29 7b 0a 20 20 20 20 75 6e 73 69  HASH ){.    unsi
103ef 67 6e 65 64 20 69 6e 74 20 6a 3b 0a 20 20 20 20  gned int j;.    
103f0 69 6e 74 20 72 63 3b 0a 20 20 20 20 75 33 32 20  int rc;.    u32 
103f1 2a 61 69 56 61 6c 75 65 73 20 3d 20 73 71 6c 69  *aiValues = sqli
103f2 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 52 61 77  te3StackAllocRaw
103f3 28 30 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 75 2e  (0, sizeof(p->u.
103f4 61 48 61 73 68 29 29 3b 0a 20 20 20 20 69 66 28  aHash));.    if(
103f5 20 61 69 56 61 6c 75 65 73 3d 3d 30 20 29 7b 0a   aiValues==0 ){.
103f6 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
103f7 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
103f8 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 63  else{.      memc
103f9 70 79 28 61 69 56 61 6c 75 65 73 2c 20 70 2d 3e  py(aiValues, p->
103fa 75 2e 61 48 61 73 68 2c 20 73 69 7a 65 6f 66 28  u.aHash, sizeof(
103fb 70 2d 3e 75 2e 61 48 61 73 68 29 29 3b 0a 20 20  p->u.aHash));.  
103fc 20 20 20 20 6d 65 6d 73 65 74 28 70 2d 3e 75 2e      memset(p->u.
103fd 61 70 53 75 62 2c 20 30 2c 20 73 69 7a 65 6f 66  apSub, 0, sizeof
103fe 28 70 2d 3e 75 2e 61 70 53 75 62 29 29 3b 0a 20  (p->u.apSub));. 
103ff 20 20 20 20 20 70 2d 3e 69 44 69 76 69 73 6f 72       p->iDivisor
10400 20 3d 20 28 70 2d 3e 69 53 69 7a 65 20 2b 20 42   = (p->iSize + B
10401 49 54 56 45 43 5f 4e 50 54 52 20 2d 20 31 29 2f  ITVEC_NPTR - 1)/
10402 42 49 54 56 45 43 5f 4e 50 54 52 3b 0a 20 20 20  BITVEC_NPTR;.   
10403 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
10404 69 74 76 65 63 53 65 74 28 70 2c 20 69 29 3b 0a  itvecSet(p, i);.
10405 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
10406 3c 42 49 54 56 45 43 5f 4e 49 4e 54 3b 20 6a 2b  <BITVEC_NINT; j+
10407 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  +){.        if( 
10408 61 69 56 61 6c 75 65 73 5b 6a 5d 20 29 20 72 63  aiValues[j] ) rc
10409 20 7c 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65   |= sqlite3Bitve
1040a 63 53 65 74 28 70 2c 20 61 69 56 61 6c 75 65 73  cSet(p, aiValues
1040b 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  [j]);.      }.  
1040c 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 63 6b      sqlite3Stack
1040d 46 72 65 65 28 30 2c 20 61 69 56 61 6c 75 65 73  Free(0, aiValues
1040e 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
1040f 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 62 69  rc;.    }.  }.bi
10410 74 76 65 63 5f 73 65 74 5f 65 6e 64 3a 0a 20 20  tvec_set_end:.  
10411 70 2d 3e 6e 53 65 74 2b 2b 3b 0a 20 20 70 2d 3e  p->nSet++;.  p->
10412 75 2e 61 48 61 73 68 5b 68 5d 20 3d 20 69 3b 0a  u.aHash[h] = i;.
10413 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
10414 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65  OK;.}../*.** Cle
10415 61 72 20 74 68 65 20 69 2d 74 68 20 62 69 74 2e  ar the i-th bit.
10416 0a 2a 2a 0a 2a 2a 20 70 42 75 66 20 6d 75 73 74  .**.** pBuf must
10417 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   be a pointer to
10418 20 61 74 20 6c 65 61 73 74 20 42 49 54 56 45 43   at least BITVEC
10419 5f 53 5a 20 62 79 74 65 73 20 6f 66 20 74 65 6d  _SZ bytes of tem
1041a 70 6f 72 61 72 79 20 73 74 6f 72 61 67 65 0a 2a  porary storage.*
1041b 2a 20 74 68 61 74 20 42 69 74 76 65 63 43 6c 65  * that BitvecCle
1041c 61 72 20 63 61 6e 20 75 73 65 20 74 6f 20 72 65  ar can use to re
1041d 62 75 69 6c 74 20 69 74 73 20 68 61 73 68 20 74  built its hash t
1041e 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  able..*/.SQLITE_
1041f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
10420 69 74 65 33 42 69 74 76 65 63 43 6c 65 61 72 28  ite3BitvecClear(
10421 42 69 74 76 65 63 20 2a 70 2c 20 75 33 32 20 69  Bitvec *p, u32 i
10422 2c 20 76 6f 69 64 20 2a 70 42 75 66 29 7b 0a 20  , void *pBuf){. 
10423 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75   if( p==0 ) retu
10424 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e  rn;.  assert( i>
10425 30 20 29 3b 0a 20 20 69 2d 2d 3b 0a 20 20 77 68  0 );.  i--;.  wh
10426 69 6c 65 28 20 70 2d 3e 69 44 69 76 69 73 6f 72  ile( p->iDivisor
10427 20 29 7b 0a 20 20 20 20 75 33 32 20 62 69 6e 20   ){.    u32 bin 
10428 3d 20 69 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b  = i/p->iDivisor;
10429 0a 20 20 20 20 69 20 3d 20 69 25 70 2d 3e 69 44  .    i = i%p->iD
1042a 69 76 69 73 6f 72 3b 0a 20 20 20 20 70 20 3d 20  ivisor;.    p = 
1042b 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3b  p->u.apSub[bin];
1042c 0a 20 20 20 20 69 66 20 28 21 70 29 20 7b 0a 20  .    if (!p) {. 
1042d 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
1042e 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e   }.  }.  if( p->
1042f 69 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e 42  iSize<=BITVEC_NB
10430 49 54 20 29 7b 0a 20 20 20 20 70 2d 3e 75 2e 61  IT ){.    p->u.a
10431 42 69 74 6d 61 70 5b 69 2f 42 49 54 56 45 43 5f  Bitmap[i/BITVEC_
10432 53 5a 45 4c 45 4d 5d 20 26 3d 20 7e 28 31 20 3c  SZELEM] &= ~(1 <
10433 3c 20 28 69 26 28 42 49 54 56 45 43 5f 53 5a 45  < (i&(BITVEC_SZE
10434 4c 45 4d 2d 31 29 29 29 3b 0a 20 20 7d 65 6c 73  LEM-1)));.  }els
10435 65 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20  e{.    unsigned 
10436 69 6e 74 20 6a 3b 0a 20 20 20 20 75 33 32 20 2a  int j;.    u32 *
10437 61 69 56 61 6c 75 65 73 20 3d 20 70 42 75 66 3b  aiValues = pBuf;
10438 0a 20 20 20 20 6d 65 6d 63 70 79 28 61 69 56 61  .    memcpy(aiVa
10439 6c 75 65 73 2c 20 70 2d 3e 75 2e 61 48 61 73 68  lues, p->u.aHash
1043a 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 75 2e 61 48  , sizeof(p->u.aH
1043b 61 73 68 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65  ash));.    memse
1043c 74 28 70 2d 3e 75 2e 61 48 61 73 68 2c 20 30 2c  t(p->u.aHash, 0,
1043d 20 73 69 7a 65 6f 66 28 70 2d 3e 75 2e 61 48 61   sizeof(p->u.aHa
1043e 73 68 29 29 3b 0a 20 20 20 20 70 2d 3e 6e 53 65  sh));.    p->nSe
1043f 74 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 6a  t = 0;.    for(j
10440 3d 30 3b 20 6a 3c 42 49 54 56 45 43 5f 4e 49 4e  =0; j<BITVEC_NIN
10441 54 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69  T; j++){.      i
10442 66 28 20 61 69 56 61 6c 75 65 73 5b 6a 5d 20 26  f( aiValues[j] &
10443 26 20 61 69 56 61 6c 75 65 73 5b 6a 5d 21 3d 28  & aiValues[j]!=(
10444 69 2b 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20  i+1) ){.        
10445 75 33 32 20 68 20 3d 20 42 49 54 56 45 43 5f 48  u32 h = BITVEC_H
10446 41 53 48 28 61 69 56 61 6c 75 65 73 5b 6a 5d 2d  ASH(aiValues[j]-
10447 31 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6e  1);.        p->n
10448 53 65 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 77  Set++;.        w
10449 68 69 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73 68  hile( p->u.aHash
1044a 5b 68 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20  [h] ){.         
1044b 20 68 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20   h++;.          
1044c 69 66 28 20 68 3e 3d 42 49 54 56 45 43 5f 4e 49  if( h>=BITVEC_NI
1044d 4e 54 20 29 20 68 20 3d 20 30 3b 0a 20 20 20 20  NT ) h = 0;.    
1044e 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 2d      }.        p-
1044f 3e 75 2e 61 48 61 73 68 5b 68 5d 20 3d 20 61 69  >u.aHash[h] = ai
10450 56 61 6c 75 65 73 5b 6a 5d 3b 0a 20 20 20 20 20  Values[j];.     
10451 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a   }.    }.  }.}..
10452 2f 2a 0a 2a 2a 20 44 65 73 74 72 6f 79 20 61 20  /*.** Destroy a 
10453 62 69 74 6d 61 70 20 6f 62 6a 65 63 74 2e 20 20  bitmap object.  
10454 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f  Reclaim all memo
10455 72 79 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49  ry used..*/.SQLI
10456 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
10457 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73  sqlite3BitvecDes
10458 74 72 6f 79 28 42 69 74 76 65 63 20 2a 70 29 7b  troy(Bitvec *p){
10459 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65  .  if( p==0 ) re
1045a 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 2d 3e 69  turn;.  if( p->i
1045b 44 69 76 69 73 6f 72 20 29 7b 0a 20 20 20 20 75  Divisor ){.    u
1045c 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a 20  nsigned int i;. 
1045d 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 42 49     for(i=0; i<BI
1045e 54 56 45 43 5f 4e 50 54 52 3b 20 69 2b 2b 29 7b  TVEC_NPTR; i++){
1045f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 69  .      sqlite3Bi
10460 74 76 65 63 44 65 73 74 72 6f 79 28 70 2d 3e 75  tvecDestroy(p->u
10461 2e 61 70 53 75 62 5b 69 5d 29 3b 0a 20 20 20 20  .apSub[i]);.    
10462 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  }.  }.  sqlite3_
10463 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  free(p);.}../*.*
10464 2a 20 52 65 74 75 72 6e 20 74 68 65 20 76 61 6c  * Return the val
10465 75 65 20 6f 66 20 74 68 65 20 69 53 69 7a 65 20  ue of the iSize 
10466 70 61 72 61 6d 65 74 65 72 20 73 70 65 63 69 66  parameter specif
10467 69 65 64 20 77 68 65 6e 20 42 69 74 76 65 63 20  ied when Bitvec 
10468 2a 70 0a 2a 2a 20 77 61 73 20 63 72 65 61 74 65  *p.** was create
10469 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
1046a 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33  VATE u32 sqlite3
1046b 42 69 74 76 65 63 53 69 7a 65 28 42 69 74 76 65  BitvecSize(Bitve
1046c 63 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20  c *p){.  return 
1046d 70 2d 3e 69 53 69 7a 65 3b 0a 7d 0a 0a 23 69 66  p->iSize;.}..#if
1046e 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1046f 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a 2f 2a  _BUILTIN_TEST./*
10470 0a 2a 2a 20 4c 65 74 20 56 5b 5d 20 62 65 20 61  .** Let V[] be a
10471 6e 20 61 72 72 61 79 20 6f 66 20 75 6e 73 69 67  n array of unsig
10472 6e 65 64 20 63 68 61 72 61 63 74 65 72 73 20 73  ned characters s
10473 75 66 66 69 63 69 65 6e 74 20 74 6f 20 68 6f 6c  ufficient to hol
10474 64 0a 2a 2a 20 75 70 20 74 6f 20 4e 20 62 69 74  d.** up to N bit
10475 73 2e 20 20 4c 65 74 20 49 20 62 65 20 61 6e 20  s.  Let I be an 
10476 69 6e 74 65 67 65 72 20 62 65 74 77 65 65 6e 20  integer between 
10477 30 20 61 6e 64 20 4e 2e 20 20 30 3c 3d 49 3c 4e  0 and N.  0<=I<N
10478 2e 0a 2a 2a 20 54 68 65 6e 20 74 68 65 20 66 6f  ..** Then the fo
10479 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 63  llowing macros c
1047a 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73 65  an be used to se
1047b 74 2c 20 63 6c 65 61 72 2c 20 6f 72 20 74 65 73  t, clear, or tes
1047c 74 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20  t.** individual 
1047d 62 69 74 73 20 77 69 74 68 69 6e 20 56 2e 0a 2a  bits within V..*
1047e 2f 0a 23 64 65 66 69 6e 65 20 53 45 54 42 49 54  /.#define SETBIT
1047f 28 56 2c 49 29 20 20 20 20 20 20 56 5b 49 3e 3e  (V,I)      V[I>>
10480 33 5d 20 7c 3d 20 28 31 3c 3c 28 49 26 37 29 29  3] |= (1<<(I&7))
10481 0a 23 64 65 66 69 6e 65 20 43 4c 45 41 52 42 49  .#define CLEARBI
10482 54 28 56 2c 49 29 20 20 20 20 56 5b 49 3e 3e 33  T(V,I)    V[I>>3
10483 5d 20 26 3d 20 7e 28 31 3c 3c 28 49 26 37 29 29  ] &= ~(1<<(I&7))
10484 0a 23 64 65 66 69 6e 65 20 54 45 53 54 42 49 54  .#define TESTBIT
10485 28 56 2c 49 29 20 20 20 20 20 28 56 5b 49 3e 3e  (V,I)     (V[I>>
10486 33 5d 26 28 31 3c 3c 28 49 26 37 29 29 29 21 3d  3]&(1<<(I&7)))!=
10487 30 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  0../*.** This ro
10488 75 74 69 6e 65 20 72 75 6e 73 20 61 6e 20 65 78  utine runs an ex
10489 74 65 6e 73 69 76 65 20 74 65 73 74 20 6f 66 20  tensive test of 
1048a 74 68 65 20 42 69 74 76 65 63 20 63 6f 64 65 2e  the Bitvec code.
1048b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74  .**.** The input
1048c 20 69 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20   is an array of 
1048d 69 6e 74 65 67 65 72 73 20 74 68 61 74 20 61 63  integers that ac
1048e 74 73 20 61 73 20 61 20 70 72 6f 67 72 61 6d 0a  ts as a program.
1048f 2a 2a 20 74 6f 20 74 65 73 74 20 74 68 65 20 42  ** to test the B
10490 69 74 76 65 63 2e 20 20 54 68 65 20 69 6e 74 65  itvec.  The inte
10491 67 65 72 73 20 61 72 65 20 6f 70 63 6f 64 65 73  gers are opcodes
10492 20 66 6f 6c 6c 6f 77 65 64 0a 2a 2a 20 62 79 20   followed.** by 
10493 30 2c 20 31 2c 20 6f 72 20 33 20 6f 70 65 72 61  0, 1, or 3 opera
10494 6e 64 73 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f  nds, depending o
10495 6e 20 74 68 65 20 6f 70 63 6f 64 65 2e 20 20 41  n the opcode.  A
10496 6e 6f 74 68 65 72 0a 2a 2a 20 6f 70 63 6f 64 65  nother.** opcode
10497 20 66 6f 6c 6c 6f 77 73 20 69 6d 6d 65 64 69 61   follows immedia
10498 74 65 6c 79 20 61 66 74 65 72 20 74 68 65 20 6c  tely after the l
10499 61 73 74 20 6f 70 65 72 61 6e 64 2e 0a 2a 2a 0a  ast operand..**.
1049a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 36 20 6f  ** There are 6 o
1049b 70 63 6f 64 65 73 20 6e 75 6d 62 65 72 65 64 20  pcodes numbered 
1049c 66 72 6f 6d 20 30 20 74 68 72 6f 75 67 68 20 35  from 0 through 5
1049d 2e 20 20 30 20 69 73 20 74 68 65 0a 2a 2a 20 22  .  0 is the.** "
1049e 68 61 6c 74 22 20 6f 70 63 6f 64 65 20 61 6e 64  halt" opcode and
1049f 20 63 61 75 73 65 73 20 74 68 65 20 74 65 73 74   causes the test
104a0 20 74 6f 20 65 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20   to end..**.**  
104a1 20 20 30 20 20 20 20 20 20 20 20 20 20 48 61 6c    0          Hal
104a2 74 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65  t and return the
104a3 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72   number of error
104a4 73 0a 2a 2a 20 20 20 20 31 20 4e 20 53 20 58 20  s.**    1 N S X 
104a5 20 20 20 53 65 74 20 4e 20 62 69 74 73 20 62 65     Set N bits be
104a6 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 53 20 61  ginning with S a
104a7 6e 64 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20  nd incrementing 
104a8 62 79 20 58 0a 2a 2a 20 20 20 20 32 20 4e 20 53  by X.**    2 N S
104a9 20 58 20 20 20 20 43 6c 65 61 72 20 4e 20 62 69   X    Clear N bi
104aa 74 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74  ts beginning wit
104ab 68 20 53 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e  h S and incremen
104ac 74 69 6e 67 20 62 79 20 58 0a 2a 2a 20 20 20 20  ting by X.**    
104ad 33 20 4e 20 20 20 20 20 20 20 20 53 65 74 20 4e  3 N        Set N
104ae 20 72 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 6e   randomly chosen
104af 20 62 69 74 73 0a 2a 2a 20 20 20 20 34 20 4e 20   bits.**    4 N 
104b0 20 20 20 20 20 20 20 43 6c 65 61 72 20 4e 20 72         Clear N r
104b1 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 6e 20 62  andomly chosen b
104b2 69 74 73 0a 2a 2a 20 20 20 20 35 20 4e 20 53 20  its.**    5 N S 
104b3 58 20 20 20 20 53 65 74 20 4e 20 62 69 74 73 20  X    Set N bits 
104b4 66 72 6f 6d 20 53 20 69 6e 63 72 65 6d 65 6e 74  from S increment
104b5 20 58 20 69 6e 20 61 72 72 61 79 20 6f 6e 6c 79   X in array only
104b6 2c 20 6e 6f 74 20 69 6e 20 62 69 74 76 65 63 0a  , not in bitvec.
104b7 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 63 6f 64 65  **.** The opcode
104b8 73 20 31 20 74 68 72 6f 75 67 68 20 34 20 70 65  s 1 through 4 pe
104b9 72 66 6f 72 6d 20 73 65 74 20 61 6e 64 20 63 6c  rform set and cl
104ba 65 61 72 20 6f 70 65 72 61 74 69 6f 6e 73 20 61  ear operations a
104bb 72 65 20 70 65 72 66 6f 72 6d 65 64 0a 2a 2a 20  re performed.** 
104bc 6f 6e 20 62 6f 74 68 20 61 20 42 69 74 76 65 63  on both a Bitvec
104bd 20 6f 62 6a 65 63 74 20 61 6e 64 20 6f 6e 20 61   object and on a
104be 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20 6f 66   linear array of
104bf 20 62 69 74 73 20 6f 62 74 61 69 6e 65 64 20 66   bits obtained f
104c0 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a 20 4f  rom malloc..** O
104c1 70 63 6f 64 65 20 35 20 77 6f 72 6b 73 20 6f 6e  pcode 5 works on
104c2 20 74 68 65 20 6c 69 6e 65 61 72 20 61 72 72 61   the linear arra
104c3 79 20 6f 6e 6c 79 2c 20 6e 6f 74 20 6f 6e 20 74  y only, not on t
104c4 68 65 20 42 69 74 76 65 63 2e 0a 2a 2a 20 4f 70  he Bitvec..** Op
104c5 63 6f 64 65 20 35 20 69 73 20 75 73 65 64 20 74  code 5 is used t
104c6 6f 20 64 65 6c 69 62 65 72 61 74 65 6c 79 20 69  o deliberately i
104c7 6e 64 75 63 65 20 61 20 66 61 75 6c 74 20 69 6e  nduce a fault in
104c8 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 6f 6e   order to.** con
104c9 66 69 72 6d 20 74 68 61 74 20 65 72 72 6f 72 20  firm that error 
104ca 64 65 74 65 63 74 69 6f 6e 20 77 6f 72 6b 73 2e  detection works.
104cb 0a 2a 2a 0a 2a 2a 20 41 74 20 74 68 65 20 63 6f  .**.** At the co
104cc 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 74 68 65 20  nclusion of the 
104cd 74 65 73 74 20 74 68 65 20 6c 69 6e 65 61 72 20  test the linear 
104ce 61 72 72 61 79 20 69 73 20 63 6f 6d 70 61 72 65  array is compare
104cf 64 0a 2a 2a 20 61 67 61 69 6e 73 74 20 74 68 65  d.** against the
104d0 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 2e 20   Bitvec object. 
104d1 20 49 66 20 74 68 65 72 65 20 61 72 65 20 61 6e   If there are an
104d2 79 20 64 69 66 66 65 72 65 6e 63 65 73 2c 0a 2a  y differences,.*
104d3 2a 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65  * an error is re
104d4 74 75 72 6e 65 64 2e 20 20 49 66 20 74 68 65 79  turned.  If they
104d5 20 61 72 65 20 74 68 65 20 73 61 6d 65 2c 20 7a   are the same, z
104d6 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ero is returned.
104d7 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f  .**.** If a memo
104d8 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72  ry allocation er
104d9 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 65 74 75  ror occurs, retu
104da 72 6e 20 2d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45  rn -1..*/.SQLITE
104db 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
104dc 69 74 65 33 42 69 74 76 65 63 42 75 69 6c 74 69  ite3BitvecBuilti
104dd 6e 54 65 73 74 28 69 6e 74 20 73 7a 2c 20 69 6e  nTest(int sz, in
104de 74 20 2a 61 4f 70 29 7b 0a 20 20 42 69 74 76 65  t *aOp){.  Bitve
104df 63 20 2a 70 42 69 74 76 65 63 20 3d 20 30 3b 0a  c *pBitvec = 0;.
104e0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
104e1 2a 70 56 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72  *pV = 0;.  int r
104e2 63 20 3d 20 2d 31 3b 0a 20 20 69 6e 74 20 69 2c  c = -1;.  int i,
104e3 20 6e 78 2c 20 70 63 2c 20 6f 70 3b 0a 20 20 76   nx, pc, op;.  v
104e4 6f 69 64 20 2a 70 54 6d 70 53 70 61 63 65 3b 0a  oid *pTmpSpace;.
104e5 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74  .  /* Allocate t
104e6 68 65 20 42 69 74 76 65 63 20 74 6f 20 62 65 20  he Bitvec to be 
104e7 74 65 73 74 65 64 20 61 6e 64 20 61 20 6c 69 6e  tested and a lin
104e8 65 61 72 20 61 72 72 61 79 20 6f 66 0a 20 20 2a  ear array of.  *
104e9 2a 20 62 69 74 73 20 74 6f 20 61 63 74 20 61 73  * bits to act as
104ea 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 2a   the reference *
104eb 2f 0a 20 20 70 42 69 74 76 65 63 20 3d 20 73 71  /.  pBitvec = sq
104ec 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74  lite3BitvecCreat
104ed 65 28 20 73 7a 20 29 3b 0a 20 20 70 56 20 3d 20  e( sz );.  pV = 
104ee 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20  sqlite3_malloc( 
104ef 28 73 7a 2b 37 29 2f 38 20 2b 20 31 20 29 3b 0a  (sz+7)/8 + 1 );.
104f0 20 20 70 54 6d 70 53 70 61 63 65 20 3d 20 73 71    pTmpSpace = sq
104f1 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 42 49 54  lite3_malloc(BIT
104f2 56 45 43 5f 53 5a 29 3b 0a 20 20 69 66 28 20 70  VEC_SZ);.  if( p
104f3 42 69 74 76 65 63 3d 3d 30 20 7c 7c 20 70 56 3d  Bitvec==0 || pV=
104f4 3d 30 20 7c 7c 20 70 54 6d 70 53 70 61 63 65 3d  =0 || pTmpSpace=
104f5 3d 30 20 20 29 20 67 6f 74 6f 20 62 69 74 76 65  =0  ) goto bitve
104f6 63 5f 65 6e 64 3b 0a 20 20 6d 65 6d 73 65 74 28  c_end;.  memset(
104f7 70 56 2c 20 30 2c 20 28 73 7a 2b 37 29 2f 38 20  pV, 0, (sz+7)/8 
104f8 2b 20 31 29 3b 0a 0a 20 20 2f 2a 20 4e 55 4c 4c  + 1);..  /* NULL
104f9 20 70 42 69 74 76 65 63 20 74 65 73 74 73 20 2a   pBitvec tests *
104fa 2f 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76 65  /.  sqlite3Bitve
104fb 63 53 65 74 28 30 2c 20 31 29 3b 0a 20 20 73 71  cSet(0, 1);.  sq
104fc 6c 69 74 65 33 42 69 74 76 65 63 43 6c 65 61 72  lite3BitvecClear
104fd 28 30 2c 20 31 2c 20 70 54 6d 70 53 70 61 63 65  (0, 1, pTmpSpace
104fe 29 3b 0a 0a 20 20 2f 2a 20 52 75 6e 20 74 68 65  );..  /* Run the
104ff 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 70 63   program */.  pc
10500 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 28   = 0;.  while( (
10501 6f 70 20 3d 20 61 4f 70 5b 70 63 5d 29 21 3d 30  op = aOp[pc])!=0
10502 20 29 7b 0a 20 20 20 20 73 77 69 74 63 68 28 20   ){.    switch( 
10503 6f 70 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65  op ){.      case
10504 20 31 3a 0a 20 20 20 20 20 20 63 61 73 65 20 32   1:.      case 2
10505 3a 0a 20 20 20 20 20 20 63 61 73 65 20 35 3a 20  :.      case 5: 
10506 7b 0a 20 20 20 20 20 20 20 20 6e 78 20 3d 20 34  {.        nx = 4
10507 3b 0a 20 20 20 20 20 20 20 20 69 20 3d 20 61 4f  ;.        i = aO
10508 70 5b 70 63 2b 32 5d 20 2d 20 31 3b 0a 20 20 20  p[pc+2] - 1;.   
10509 20 20 20 20 20 61 4f 70 5b 70 63 2b 32 5d 20 2b       aOp[pc+2] +
1050a 3d 20 61 4f 70 5b 70 63 2b 33 5d 3b 0a 20 20 20  = aOp[pc+3];.   
1050b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1050c 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 33    }.      case 3
1050d 3a 0a 20 20 20 20 20 20 63 61 73 65 20 34 3a 20  :.      case 4: 
1050e 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20  .      default: 
1050f 7b 0a 20 20 20 20 20 20 20 20 6e 78 20 3d 20 32  {.        nx = 2
10510 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
10511 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a  3_randomness(siz
10512 65 6f 66 28 69 29 2c 20 26 69 29 3b 0a 20 20 20  eof(i), &i);.   
10513 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
10514 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
10515 28 20 28 2d 2d 61 4f 70 5b 70 63 2b 31 5d 29 20  ( (--aOp[pc+1]) 
10516 3e 20 30 20 29 20 6e 78 20 3d 20 30 3b 0a 20 20  > 0 ) nx = 0;.  
10517 20 20 70 63 20 2b 3d 20 6e 78 3b 0a 20 20 20 20    pc += nx;.    
10518 69 20 3d 20 28 69 20 26 20 30 78 37 66 66 66 66  i = (i & 0x7ffff
10519 66 66 66 29 25 73 7a 3b 0a 20 20 20 20 69 66 28  fff)%sz;.    if(
1051a 20 28 6f 70 20 26 20 31 29 21 3d 30 20 29 7b 0a   (op & 1)!=0 ){.
1051b 20 20 20 20 20 20 53 45 54 42 49 54 28 70 56 2c        SETBIT(pV,
1051c 20 28 69 2b 31 29 29 3b 0a 20 20 20 20 20 20 69   (i+1));.      i
1051d 66 28 20 6f 70 21 3d 35 20 29 7b 0a 20 20 20 20  f( op!=5 ){.    
1051e 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 42      if( sqlite3B
1051f 69 74 76 65 63 53 65 74 28 70 42 69 74 76 65 63  itvecSet(pBitvec
10520 2c 20 69 2b 31 29 20 29 20 67 6f 74 6f 20 62 69  , i+1) ) goto bi
10521 74 76 65 63 5f 65 6e 64 3b 0a 20 20 20 20 20 20  tvec_end;.      
10522 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
10523 20 20 20 43 4c 45 41 52 42 49 54 28 70 56 2c 20     CLEARBIT(pV, 
10524 28 69 2b 31 29 29 3b 0a 20 20 20 20 20 20 73 71  (i+1));.      sq
10525 6c 69 74 65 33 42 69 74 76 65 63 43 6c 65 61 72  lite3BitvecClear
10526 28 70 42 69 74 76 65 63 2c 20 69 2b 31 2c 20 70  (pBitvec, i+1, p
10527 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 20 20 7d  TmpSpace);.    }
10528 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73 74 20  .  }..  /* Test 
10529 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65  to make sure the
1052a 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20 65 78   linear array ex
1052b 61 63 74 6c 79 20 6d 61 74 63 68 65 73 20 74 68  actly matches th
1052c 65 0a 20 20 2a 2a 20 42 69 74 76 65 63 20 6f 62  e.  ** Bitvec ob
1052d 6a 65 63 74 2e 20 20 53 74 61 72 74 20 77 69 74  ject.  Start wit
1052e 68 20 74 68 65 20 61 73 73 75 6d 70 74 69 6f 6e  h the assumption
1052f 20 74 68 61 74 20 74 68 65 79 20 64 6f 0a 20 20   that they do.  
10530 2a 2a 20 6d 61 74 63 68 20 28 72 63 3d 3d 30 29  ** match (rc==0)
10531 2e 20 20 43 68 61 6e 67 65 20 72 63 20 74 6f 20  .  Change rc to 
10532 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20 64 69  non-zero if a di
10533 73 63 72 65 70 61 6e 63 79 0a 20 20 2a 2a 20 69  screpancy.  ** i
10534 73 20 66 6f 75 6e 64 2e 0a 20 20 2a 2f 0a 20 20  s found..  */.  
10535 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76  rc = sqlite3Bitv
10536 65 63 54 65 73 74 28 30 2c 30 29 20 2b 20 73 71  ecTest(0,0) + sq
10537 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28  lite3BitvecTest(
10538 70 42 69 74 76 65 63 2c 20 73 7a 2b 31 29 0a 20  pBitvec, sz+1). 
10539 20 20 20 20 20 20 20 20 20 2b 20 73 71 6c 69 74           + sqlit
1053a 65 33 42 69 74 76 65 63 54 65 73 74 28 70 42 69  e3BitvecTest(pBi
1053b 74 76 65 63 2c 20 30 29 0a 20 20 20 20 20 20 20  tvec, 0).       
1053c 20 20 20 2b 20 28 73 71 6c 69 74 65 33 42 69 74     + (sqlite3Bit
1053d 76 65 63 53 69 7a 65 28 70 42 69 74 76 65 63 29  vecSize(pBitvec)
1053e 20 2d 20 73 7a 29 3b 0a 20 20 66 6f 72 28 69 3d   - sz);.  for(i=
1053f 31 3b 20 69 3c 3d 73 7a 3b 20 69 2b 2b 29 7b 0a  1; i<=sz; i++){.
10540 20 20 20 20 69 66 28 20 20 28 54 45 53 54 42 49      if(  (TESTBI
10541 54 28 70 56 2c 69 29 29 21 3d 73 71 6c 69 74 65  T(pV,i))!=sqlite
10542 33 42 69 74 76 65 63 54 65 73 74 28 70 42 69 74  3BitvecTest(pBit
10543 76 65 63 2c 69 29 20 29 7b 0a 20 20 20 20 20 20  vec,i) ){.      
10544 72 63 20 3d 20 69 3b 0a 20 20 20 20 20 20 62 72  rc = i;.      br
10545 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  eak;.    }.  }..
10546 20 20 2f 2a 20 46 72 65 65 20 61 6c 6c 6f 63 61    /* Free alloca
10547 74 65 64 20 73 74 72 75 63 74 75 72 65 20 2a 2f  ted structure */
10548 0a 62 69 74 76 65 63 5f 65 6e 64 3a 0a 20 20 73  .bitvec_end:.  s
10549 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 6d 70  qlite3_free(pTmp
1054a 53 70 61 63 65 29 3b 0a 20 20 73 71 6c 69 74 65  Space);.  sqlite
1054b 33 5f 66 72 65 65 28 70 56 29 3b 0a 20 20 73 71  3_free(pV);.  sq
1054c 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72  lite3BitvecDestr
1054d 6f 79 28 70 42 69 74 76 65 63 29 3b 0a 20 20 72  oy(pBitvec);.  r
1054e 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64  eturn rc;.}.#end
1054f 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
10550 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 2a  T_BUILTIN_TEST *
10551 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
10552 2a 2a 20 45 6e 64 20 6f 66 20 62 69 74 76 65 63  ** End of bitvec
10553 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
10554 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10555 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10556 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
10557 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 63  ** Begin file pc
10558 61 63 68 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  ache.c *********
10559 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1055a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1055b 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 75  */./*.** 2008 Au
1055c 67 75 73 74 20 30 35 0a 2a 2a 0a 2a 2a 20 54 68  gust 05.**.** Th
1055d 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
1055e 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
1055f 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
10560 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
10561 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
10562 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
10563 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
10564 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
10565 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
10566 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
10567 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
10568 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
10569 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
1056a 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
1056b 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
1056c 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
1056d 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
1056e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1056f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10571 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10572 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
10573 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74  ile implements t
10574 68 61 74 20 70 61 67 65 20 63 61 63 68 65 2e 0a  hat page cache..
10575 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20  **.** @(#) $Id: 
10576 70 63 61 63 68 65 2e 63 2c 76 20 31 2e 34 37 20  pcache.c,v 1.47 
10577 32 30 30 39 2f 30 37 2f 32 35 20 31 31 3a 34 36  2009/07/25 11:46
10578 3a 34 39 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :49 danielk1977 
10579 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  Exp $.*/../*.** 
1057a 41 20 63 6f 6d 70 6c 65 74 65 20 70 61 67 65 20  A complete page 
1057b 63 61 63 68 65 20 69 73 20 61 6e 20 69 6e 73 74  cache is an inst
1057c 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72  ance of this str
1057d 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63  ucture..*/.struc
1057e 74 20 50 43 61 63 68 65 20 7b 0a 20 20 50 67 48  t PCache {.  PgH
1057f 64 72 20 2a 70 44 69 72 74 79 2c 20 2a 70 44 69  dr *pDirty, *pDi
10580 72 74 79 54 61 69 6c 3b 20 20 20 20 20 20 20 20  rtyTail;        
10581 20 2f 2a 20 4c 69 73 74 20 6f 66 20 64 69 72 74   /* List of dirt
10582 79 20 70 61 67 65 73 20 69 6e 20 4c 52 55 20 6f  y pages in LRU o
10583 72 64 65 72 20 2a 2f 0a 20 20 50 67 48 64 72 20  rder */.  PgHdr 
10584 2a 70 53 79 6e 63 65 64 3b 20 20 20 20 20 20 20  *pSynced;       
10585 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10586 20 4c 61 73 74 20 73 79 6e 63 65 64 20 70 61 67   Last synced pag
10587 65 20 69 6e 20 64 69 72 74 79 20 70 61 67 65 20  e in dirty page 
10588 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 52  list */.  int nR
10589 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ef;             
1058a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1058b 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72   Number of refer
1058c 65 6e 63 65 64 20 70 61 67 65 73 20 2a 2f 0a 20  enced pages */. 
1058d 20 69 6e 74 20 6e 4d 61 78 3b 20 20 20 20 20 20   int nMax;      
1058e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1058f 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72       /* Configur
10590 65 64 20 63 61 63 68 65 20 73 69 7a 65 20 2a 2f  ed cache size */
10591 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 3b 20 20  .  int szPage;  
10592 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10593 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
10594 66 20 65 76 65 72 79 20 70 61 67 65 20 69 6e 20  f every page in 
10595 74 68 69 73 20 63 61 63 68 65 20 2a 2f 0a 20 20  this cache */.  
10596 69 6e 74 20 73 7a 45 78 74 72 61 3b 20 20 20 20  int szExtra;    
10597 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10598 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65      /* Size of e
10599 78 74 72 61 20 73 70 61 63 65 20 66 6f 72 20 65  xtra space for e
1059a 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e  ach page */.  in
1059b 74 20 62 50 75 72 67 65 61 62 6c 65 3b 20 20 20  t bPurgeable;   
1059c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1059d 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 61 67    /* True if pag
1059e 65 73 20 61 72 65 20 6f 6e 20 62 61 63 6b 69 6e  es are on backin
1059f 67 20 73 74 6f 72 65 20 2a 2f 0a 20 20 69 6e 74  g store */.  int
105a0 20 28 2a 78 53 74 72 65 73 73 29 28 76 6f 69 64   (*xStress)(void
105a1 2a 2c 50 67 48 64 72 2a 29 3b 20 20 20 20 20 20  *,PgHdr*);      
105a2 20 2f 2a 20 43 61 6c 6c 20 74 6f 20 74 72 79 20   /* Call to try 
105a3 6d 61 6b 65 20 61 20 70 61 67 65 20 63 6c 65 61  make a page clea
105a4 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 74  n */.  void *pSt
105a5 72 65 73 73 3b 20 20 20 20 20 20 20 20 20 20 20  ress;           
105a6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72             /* Ar
105a7 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65 73  gument to xStres
105a8 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 70  s */.  sqlite3_p
105a9 63 61 63 68 65 20 2a 70 43 61 63 68 65 3b 20 20  cache *pCache;  
105aa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6c             /* Pl
105ab 75 67 67 61 62 6c 65 20 63 61 63 68 65 20 6d 6f  uggable cache mo
105ac 64 75 6c 65 20 2a 2f 0a 20 20 50 67 48 64 72 20  dule */.  PgHdr 
105ad 2a 70 50 61 67 65 31 3b 20 20 20 20 20 20 20 20  *pPage1;        
105ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
105af 20 52 65 66 65 72 65 6e 63 65 20 74 6f 20 70 61   Reference to pa
105b0 67 65 20 31 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  ge 1 */.};../*.*
105b1 2a 20 53 6f 6d 65 20 6f 66 20 74 68 65 20 61 73  * Some of the as
105b2 73 65 72 74 28 29 20 6d 61 63 72 6f 73 20 69 6e  sert() macros in
105b3 20 74 68 69 73 20 63 6f 64 65 20 61 72 65 20 74   this code are t
105b4 6f 6f 20 65 78 70 65 6e 73 69 76 65 20 74 6f 20  oo expensive to 
105b5 72 75 6e 0a 2a 2a 20 65 76 65 6e 20 64 75 72 69  run.** even duri
105b6 6e 67 20 6e 6f 72 6d 61 6c 20 64 65 62 75 67 67  ng normal debugg
105b7 69 6e 67 2e 20 20 55 73 65 20 74 68 65 6d 20 6f  ing.  Use them o
105b8 6e 6c 79 20 72 61 72 65 6c 79 20 6f 6e 20 6c 6f  nly rarely on lo
105b9 6e 67 2d 72 75 6e 6e 69 6e 67 0a 2a 2a 20 74 65  ng-running.** te
105ba 73 74 73 2e 20 20 45 6e 61 62 6c 65 20 74 68 65  sts.  Enable the
105bb 20 65 78 70 65 6e 73 69 76 65 20 61 73 73 65 72   expensive asser
105bc 74 73 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20  ts using the.** 
105bd 2d 44 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  -DSQLITE_ENABLE_
105be 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 54  EXPENSIVE_ASSERT
105bf 3d 31 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20  =1 compile-time 
105c0 6f 70 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 64 65  option..*/.#ifde
105c1 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
105c2 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 54  EXPENSIVE_ASSERT
105c3 0a 23 20 64 65 66 69 6e 65 20 65 78 70 65 6e 73  .# define expens
105c4 69 76 65 5f 61 73 73 65 72 74 28 58 29 20 20 61  ive_assert(X)  a
105c5 73 73 65 72 74 28 58 29 0a 23 65 6c 73 65 0a 23  ssert(X).#else.#
105c6 20 64 65 66 69 6e 65 20 65 78 70 65 6e 73 69 76   define expensiv
105c7 65 5f 61 73 73 65 72 74 28 58 29 0a 23 65 6e 64  e_assert(X).#end
105c8 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  if../***********
105c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
105ca 2a 2a 2a 2a 2a 2a 2a 20 4c 69 6e 6b 65 64 20 4c  ******* Linked L
105cb 69 73 74 20 4d 61 6e 61 67 65 6d 65 6e 74 20 2a  ist Management *
105cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
105cd 2a 2a 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e  ***/..#if !defin
105ce 65 64 28 4e 44 45 42 55 47 29 20 26 26 20 64 65  ed(NDEBUG) && de
105cf 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
105d0 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53  BLE_EXPENSIVE_AS
105d1 53 45 52 54 29 0a 2f 2a 0a 2a 2a 20 43 68 65 63  SERT)./*.** Chec
105d2 6b 20 74 68 61 74 20 74 68 65 20 70 43 61 63 68  k that the pCach
105d3 65 2d 3e 70 53 79 6e 63 65 64 20 76 61 72 69 61  e->pSynced varia
105d4 62 6c 65 20 69 73 20 73 65 74 20 63 6f 72 72 65  ble is set corre
105d5 63 74 6c 79 2e 20 49 66 20 69 74 0a 2a 2a 20 69  ctly. If it.** i
105d6 73 20 6e 6f 74 2c 20 65 69 74 68 65 72 20 66 61  s not, either fa
105d7 69 6c 20 61 6e 20 61 73 73 65 72 74 20 6f 72 20  il an assert or 
105d8 72 65 74 75 72 6e 20 7a 65 72 6f 2e 20 4f 74 68  return zero. Oth
105d9 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e 0a 2a  erwise, return.*
105da 2a 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 54 68 69 73  * non-zero. This
105db 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 6e   is only used in
105dc 20 64 65 62 75 67 67 69 6e 67 20 62 75 69 6c 64   debugging build
105dd 73 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  s, as follows:.*
105de 2a 0a 2a 2a 20 20 20 65 78 70 65 6e 73 69 76 65  *.**   expensive
105df 5f 61 73 73 65 72 74 28 20 70 63 61 63 68 65 43  _assert( pcacheC
105e0 68 65 63 6b 53 79 6e 63 65 64 28 70 43 61 63 68  heckSynced(pCach
105e1 65 29 20 29 3b 0a 2a 2f 0a 73 74 61 74 69 63 20  e) );.*/.static 
105e2 69 6e 74 20 70 63 61 63 68 65 43 68 65 63 6b 53  int pcacheCheckS
105e3 79 6e 63 65 64 28 50 43 61 63 68 65 20 2a 70 43  ynced(PCache *pC
105e4 61 63 68 65 29 7b 0a 20 20 50 67 48 64 72 20 2a  ache){.  PgHdr *
105e5 70 3b 0a 20 20 66 6f 72 28 70 3d 70 43 61 63 68  p;.  for(p=pCach
105e6 65 2d 3e 70 44 69 72 74 79 54 61 69 6c 3b 20 70  e->pDirtyTail; p
105e7 21 3d 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65  !=pCache->pSynce
105e8 64 3b 20 70 3d 70 2d 3e 70 44 69 72 74 79 50 72  d; p=p->pDirtyPr
105e9 65 76 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  ev){.    assert(
105ea 20 70 2d 3e 6e 52 65 66 20 7c 7c 20 28 70 2d 3e   p->nRef || (p->
105eb 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44  flags&PGHDR_NEED
105ec 5f 53 59 4e 43 29 20 29 3b 0a 20 20 7d 0a 20 20  _SYNC) );.  }.  
105ed 72 65 74 75 72 6e 20 28 70 3d 3d 30 20 7c 7c 20  return (p==0 || 
105ee 70 2d 3e 6e 52 65 66 20 7c 7c 20 28 70 2d 3e 66  p->nRef || (p->f
105ef 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f  lags&PGHDR_NEED_
105f0 53 59 4e 43 29 3d 3d 30 29 3b 0a 7d 0a 23 65 6e  SYNC)==0);.}.#en
105f1 64 69 66 20 2f 2a 20 21 4e 44 45 42 55 47 20 26  dif /* !NDEBUG &
105f2 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  & SQLITE_ENABLE_
105f3 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 54  EXPENSIVE_ASSERT
105f4 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76   */../*.** Remov
105f5 65 20 70 61 67 65 20 70 50 61 67 65 20 66 72 6f  e page pPage fro
105f6 6d 20 74 68 65 20 6c 69 73 74 20 6f 66 20 64 69  m the list of di
105f7 72 74 79 20 70 61 67 65 73 2e 0a 2a 2f 0a 73 74  rty pages..*/.st
105f8 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65  atic void pcache
105f9 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c  RemoveFromDirtyL
105fa 69 73 74 28 50 67 48 64 72 20 2a 70 50 61 67 65  ist(PgHdr *pPage
105fb 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 20 3d  ){.  PCache *p =
105fc 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3b 0a   pPage->pCache;.
105fd 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
105fe 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 7c 7c 20  ->pDirtyNext || 
105ff 70 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74 79  pPage==p->pDirty
10600 54 61 69 6c 20 29 3b 0a 20 20 61 73 73 65 72 74  Tail );.  assert
10601 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50  ( pPage->pDirtyP
10602 72 65 76 20 7c 7c 20 70 50 61 67 65 3d 3d 70 2d  rev || pPage==p-
10603 3e 70 44 69 72 74 79 20 29 3b 0a 0a 20 20 2f 2a  >pDirty );..  /*
10604 20 55 70 64 61 74 65 20 74 68 65 20 50 43 61 63   Update the PCac
10605 68 65 31 2e 70 53 79 6e 63 65 64 20 76 61 72 69  he1.pSynced vari
10606 61 62 6c 65 20 69 66 20 6e 65 63 65 73 73 61 72  able if necessar
10607 79 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70  y. */.  if( p->p
10608 53 79 6e 63 65 64 3d 3d 70 50 61 67 65 20 29 7b  Synced==pPage ){
10609 0a 20 20 20 20 50 67 48 64 72 20 2a 70 53 79 6e  .    PgHdr *pSyn
1060a 63 65 64 20 3d 20 70 50 61 67 65 2d 3e 70 44 69  ced = pPage->pDi
1060b 72 74 79 50 72 65 76 3b 0a 20 20 20 20 77 68 69  rtyPrev;.    whi
1060c 6c 65 28 20 70 53 79 6e 63 65 64 20 26 26 20 28  le( pSynced && (
1060d 70 53 79 6e 63 65 64 2d 3e 66 6c 61 67 73 26 50  pSynced->flags&P
1060e 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 20  GHDR_NEED_SYNC) 
1060f 29 7b 0a 20 20 20 20 20 20 70 53 79 6e 63 65 64  ){.      pSynced
10610 20 3d 20 70 53 79 6e 63 65 64 2d 3e 70 44 69 72   = pSynced->pDir
10611 74 79 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20  tyPrev;.    }.  
10612 20 20 70 2d 3e 70 53 79 6e 63 65 64 20 3d 20 70    p->pSynced = p
10613 53 79 6e 63 65 64 3b 0a 20 20 7d 0a 0a 20 20 69  Synced;.  }..  i
10614 66 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79  f( pPage->pDirty
10615 4e 65 78 74 20 29 7b 0a 20 20 20 20 70 50 61 67  Next ){.    pPag
10616 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d 3e 70  e->pDirtyNext->p
10617 44 69 72 74 79 50 72 65 76 20 3d 20 70 50 61 67  DirtyPrev = pPag
10618 65 2d 3e 70 44 69 72 74 79 50 72 65 76 3b 0a 20  e->pDirtyPrev;. 
10619 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
1061a 72 74 28 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44  rt( pPage==p->pD
1061b 69 72 74 79 54 61 69 6c 20 29 3b 0a 20 20 20 20  irtyTail );.    
1061c 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 3d 20  p->pDirtyTail = 
1061d 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65  pPage->pDirtyPre
1061e 76 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61  v;.  }.  if( pPa
1061f 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 20 29  ge->pDirtyPrev )
10620 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 44 69  {.    pPage->pDi
10621 72 74 79 50 72 65 76 2d 3e 70 44 69 72 74 79 4e  rtyPrev->pDirtyN
10622 65 78 74 20 3d 20 70 50 61 67 65 2d 3e 70 44 69  ext = pPage->pDi
10623 72 74 79 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 65  rtyNext;.  }else
10624 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  {.    assert( pP
10625 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74 79 20 29  age==p->pDirty )
10626 3b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 79 20  ;.    p->pDirty 
10627 3d 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e  = pPage->pDirtyN
10628 65 78 74 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65  ext;.  }.  pPage
10629 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 3d 20 30  ->pDirtyNext = 0
1062a 3b 0a 20 20 70 50 61 67 65 2d 3e 70 44 69 72 74  ;.  pPage->pDirt
1062b 79 50 72 65 76 20 3d 20 30 3b 0a 0a 20 20 65 78  yPrev = 0;..  ex
1062c 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28 20  pensive_assert( 
1062d 70 63 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65  pcacheCheckSynce
1062e 64 28 70 29 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  d(p) );.}../*.**
1062f 20 41 64 64 20 70 61 67 65 20 70 50 61 67 65 20   Add page pPage 
10630 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66 20 74  to the head of t
10631 68 65 20 64 69 72 74 79 20 6c 69 73 74 20 28 50  he dirty list (P
10632 43 61 63 68 65 31 2e 70 44 69 72 74 79 20 69 73  Cache1.pDirty is
10633 20 73 65 74 20 74 6f 0a 2a 2a 20 70 50 61 67 65   set to.** pPage
10634 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  )..*/.static voi
10635 64 20 70 63 61 63 68 65 41 64 64 54 6f 44 69 72  d pcacheAddToDir
10636 74 79 4c 69 73 74 28 50 67 48 64 72 20 2a 70 50  tyList(PgHdr *pP
10637 61 67 65 29 7b 0a 20 20 50 43 61 63 68 65 20 2a  age){.  PCache *
10638 70 20 3d 20 70 50 61 67 65 2d 3e 70 43 61 63 68  p = pPage->pCach
10639 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50  e;..  assert( pP
1063a 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 3d  age->pDirtyNext=
1063b 3d 30 20 26 26 20 70 50 61 67 65 2d 3e 70 44 69  =0 && pPage->pDi
1063c 72 74 79 50 72 65 76 3d 3d 30 20 26 26 20 70 2d  rtyPrev==0 && p-
1063d 3e 70 44 69 72 74 79 21 3d 70 50 61 67 65 20 29  >pDirty!=pPage )
1063e 3b 0a 0a 20 20 70 50 61 67 65 2d 3e 70 44 69 72  ;..  pPage->pDir
1063f 74 79 4e 65 78 74 20 3d 20 70 2d 3e 70 44 69 72  tyNext = p->pDir
10640 74 79 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d  ty;.  if( pPage-
10641 3e 70 44 69 72 74 79 4e 65 78 74 20 29 7b 0a 20  >pDirtyNext ){. 
10642 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
10643 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d 3e 70 44  ->pDirtyNext->pD
10644 69 72 74 79 50 72 65 76 3d 3d 30 20 29 3b 0a 20  irtyPrev==0 );. 
10645 20 20 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79     pPage->pDirty
10646 4e 65 78 74 2d 3e 70 44 69 72 74 79 50 72 65 76  Next->pDirtyPrev
10647 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 0a 20 20   = pPage;.  }.  
10648 70 2d 3e 70 44 69 72 74 79 20 3d 20 70 50 61 67  p->pDirty = pPag
10649 65 3b 0a 20 20 69 66 28 20 21 70 2d 3e 70 44 69  e;.  if( !p->pDi
1064a 72 74 79 54 61 69 6c 20 29 7b 0a 20 20 20 20 70  rtyTail ){.    p
1064b 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 3d 20 70  ->pDirtyTail = p
1064c 50 61 67 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20  Page;.  }.  if( 
1064d 21 70 2d 3e 70 53 79 6e 63 65 64 20 26 26 20 30  !p->pSynced && 0
1064e 3d 3d 28 70 50 61 67 65 2d 3e 66 6c 61 67 73 26  ==(pPage->flags&
1064f 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29  PGHDR_NEED_SYNC)
10650 20 29 7b 0a 20 20 20 20 70 2d 3e 70 53 79 6e 63   ){.    p->pSync
10651 65 64 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 0a  ed = pPage;.  }.
10652 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65    expensive_asse
10653 72 74 28 20 70 63 61 63 68 65 43 68 65 63 6b 53  rt( pcacheCheckS
10654 79 6e 63 65 64 28 70 29 20 29 3b 0a 7d 0a 0a 2f  ynced(p) );.}../
10655 2a 0a 2a 2a 20 57 72 61 70 70 65 72 20 61 72 6f  *.** Wrapper aro
10656 75 6e 64 20 74 68 65 20 70 6c 75 67 67 61 62 6c  und the pluggabl
10657 65 20 63 61 63 68 65 73 20 78 55 6e 70 69 6e 20  e caches xUnpin 
10658 6d 65 74 68 6f 64 2e 20 49 66 20 74 68 65 20 63  method. If the c
10659 61 63 68 65 20 69 73 0a 2a 2a 20 62 65 69 6e 67  ache is.** being
1065a 20 75 73 65 64 20 66 6f 72 20 61 6e 20 69 6e 2d   used for an in-
1065b 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c  memory database,
1065c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
1065d 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74  s a no-op..*/.st
1065e 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65  atic void pcache
1065f 55 6e 70 69 6e 28 50 67 48 64 72 20 2a 70 29 7b  Unpin(PgHdr *p){
10660 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 68  .  PCache *pCach
10661 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20  e = p->pCache;. 
10662 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75   if( pCache->bPu
10663 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 69  rgeable ){.    i
10664 66 28 20 70 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b  f( p->pgno==1 ){
10665 0a 20 20 20 20 20 20 70 43 61 63 68 65 2d 3e 70  .      pCache->p
10666 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 20 20 7d  Page1 = 0;.    }
10667 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
10668 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e  alConfig.pcache.
10669 78 55 6e 70 69 6e 28 70 43 61 63 68 65 2d 3e 70  xUnpin(pCache->p
1066a 43 61 63 68 65 2c 20 70 2c 20 30 29 3b 0a 20 20  Cache, p, 0);.  
1066b 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }.}../**********
1066c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1066d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1066e 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 47 65 6e 65 72 61  ********* Genera
1066f 6c 20 49 6e 74 65 72 66 61 63 65 73 20 2a 2a 2a  l Interfaces ***
10670 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 49 6e 69 74 69 61  ***.**.** Initia
10671 6c 69 7a 65 20 61 6e 64 20 73 68 75 74 64 6f 77  lize and shutdow
10672 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65  n the page cache
10673 20 73 75 62 73 79 73 74 65 6d 2e 20 4e 65 69 74   subsystem. Neit
10674 68 65 72 20 6f 66 20 74 68 65 73 65 20 0a 2a 2a  her of these .**
10675 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 74   functions are t
10676 68 72 65 61 64 73 61 66 65 2e 0a 2a 2f 0a 53 51  hreadsafe..*/.SQ
10677 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
10678 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 6e   sqlite3PcacheIn
10679 69 74 69 61 6c 69 7a 65 28 76 6f 69 64 29 7b 0a  itialize(void){.
1067a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f    if( sqlite3Glo
1067b 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65  balConfig.pcache
1067c 2e 78 49 6e 69 74 3d 3d 30 20 29 7b 0a 20 20 20  .xInit==0 ){.   
1067d 20 73 71 6c 69 74 65 33 50 43 61 63 68 65 53 65   sqlite3PCacheSe
1067e 74 44 65 66 61 75 6c 74 28 29 3b 0a 20 20 7d 0a  tDefault();.  }.
1067f 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
10680 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61  GlobalConfig.pca
10681 63 68 65 2e 78 49 6e 69 74 28 73 71 6c 69 74 65  che.xInit(sqlite
10682 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63  3GlobalConfig.pc
10683 61 63 68 65 2e 70 41 72 67 29 3b 0a 7d 0a 53 51  ache.pArg);.}.SQ
10684 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
10685 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53  d sqlite3PcacheS
10686 68 75 74 64 6f 77 6e 28 76 6f 69 64 29 7b 0a 20  hutdown(void){. 
10687 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
10688 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e  alConfig.pcache.
10689 78 53 68 75 74 64 6f 77 6e 20 29 7b 0a 20 20 20  xShutdown ){.   
1068a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
1068b 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 53 68 75  nfig.pcache.xShu
1068c 74 64 6f 77 6e 28 73 71 6c 69 74 65 33 47 6c 6f  tdown(sqlite3Glo
1068d 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65  balConfig.pcache
1068e 2e 70 41 72 67 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  .pArg);.  }.}../
1068f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
10690 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66  size in bytes of
10691 20 61 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74   a PCache object
10692 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
10693 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
10694 63 61 63 68 65 53 69 7a 65 28 76 6f 69 64 29 7b  cacheSize(void){
10695 20 72 65 74 75 72 6e 20 73 69 7a 65 6f 66 28 50   return sizeof(P
10696 43 61 63 68 65 29 3b 20 7d 0a 0a 2f 2a 0a 2a 2a  Cache); }../*.**
10697 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 50 43   Create a new PC
10698 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 53 74 6f  ache object. Sto
10699 72 61 67 65 20 73 70 61 63 65 20 74 6f 20 68 6f  rage space to ho
1069a 6c 64 20 74 68 65 20 6f 62 6a 65 63 74 0a 2a 2a  ld the object.**
1069b 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65   has already bee
1069c 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20  n allocated and 
1069d 69 73 20 70 61 73 73 65 64 20 69 6e 20 61 73 20  is passed in as 
1069e 74 68 65 20 70 20 70 6f 69 6e 74 65 72 2e 20 0a  the p pointer. .
1069f 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 64 69  ** The caller di
106a0 73 63 6f 76 65 72 73 20 68 6f 77 20 6d 75 63 68  scovers how much
106a1 20 73 70 61 63 65 20 6e 65 65 64 73 20 74 6f 20   space needs to 
106a2 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20  be allocated by 
106a3 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69  .** calling sqli
106a4 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 29 2e  te3PcacheSize().
106a5 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
106a6 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
106a7 63 61 63 68 65 4f 70 65 6e 28 0a 20 20 69 6e 74  cacheOpen(.  int
106a8 20 73 7a 50 61 67 65 2c 20 20 20 20 20 20 20 20   szPage,        
106a9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
106aa 65 20 6f 66 20 65 76 65 72 79 20 70 61 67 65 20  e of every page 
106ab 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 78 74 72 61  */.  int szExtra
106ac 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
106ad 20 20 2f 2a 20 45 78 74 72 61 20 73 70 61 63 65    /* Extra space
106ae 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
106af 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20   each page */.  
106b0 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 2c 20  int bPurgeable, 
106b1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
106b2 54 72 75 65 20 69 66 20 70 61 67 65 73 20 61 72  True if pages ar
106b3 65 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f  e on backing sto
106b4 72 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53  re */.  int (*xS
106b5 74 72 65 73 73 29 28 76 6f 69 64 2a 2c 50 67 48  tress)(void*,PgH
106b6 64 72 2a 29 2c 2f 2a 20 43 61 6c 6c 20 74 6f 20  dr*),/* Call to 
106b7 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 67 65  try to make page
106b8 73 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 6f 69  s clean */.  voi
106b9 64 20 2a 70 53 74 72 65 73 73 2c 20 20 20 20 20  d *pStress,     
106ba 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67            /* Arg
106bb 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65 73 73  ument to xStress
106bc 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a 70 20   */.  PCache *p 
106bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
106be 20 20 20 2f 2a 20 50 72 65 61 6c 6c 6f 63 61 74     /* Preallocat
106bf 65 64 20 73 70 61 63 65 20 66 6f 72 20 74 68 65  ed space for the
106c0 20 50 43 61 63 68 65 20 2a 2f 0a 29 7b 0a 20 20   PCache */.){.  
106c1 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 69 7a  memset(p, 0, siz
106c2 65 6f 66 28 50 43 61 63 68 65 29 29 3b 0a 20 20  eof(PCache));.  
106c3 70 2d 3e 73 7a 50 61 67 65 20 3d 20 73 7a 50 61  p->szPage = szPa
106c4 67 65 3b 0a 20 20 70 2d 3e 73 7a 45 78 74 72 61  ge;.  p->szExtra
106c5 20 3d 20 73 7a 45 78 74 72 61 3b 0a 20 20 70 2d   = szExtra;.  p-
106c6 3e 62 50 75 72 67 65 61 62 6c 65 20 3d 20 62 50  >bPurgeable = bP
106c7 75 72 67 65 61 62 6c 65 3b 0a 20 20 70 2d 3e 78  urgeable;.  p->x
106c8 53 74 72 65 73 73 20 3d 20 78 53 74 72 65 73 73  Stress = xStress
106c9 3b 0a 20 20 70 2d 3e 70 53 74 72 65 73 73 20 3d  ;.  p->pStress =
106ca 20 70 53 74 72 65 73 73 3b 0a 20 20 70 2d 3e 6e   pStress;.  p->n
106cb 4d 61 78 20 3d 20 31 30 30 3b 0a 7d 0a 0a 2f 2a  Max = 100;.}../*
106cc 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 70  .** Change the p
106cd 61 67 65 20 73 69 7a 65 20 66 6f 72 20 50 43 61  age size for PCa
106ce 63 68 65 20 6f 62 6a 65 63 74 2e 20 54 68 65 20  che object. The 
106cf 63 61 6c 6c 65 72 20 6d 75 73 74 20 65 6e 73 75  caller must ensu
106d0 72 65 20 74 68 61 74 20 74 68 65 72 65 0a 2a 2a  re that there.**
106d1 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64   are no outstand
106d2 69 6e 67 20 70 61 67 65 20 72 65 66 65 72 65 6e  ing page referen
106d3 63 65 73 20 77 68 65 6e 20 74 68 69 73 20 66 75  ces when this fu
106d4 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
106d5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
106d6 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
106d7 50 63 61 63 68 65 53 65 74 50 61 67 65 53 69 7a  PcacheSetPageSiz
106d8 65 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65  e(PCache *pCache
106d9 2c 20 69 6e 74 20 73 7a 50 61 67 65 29 7b 0a 20  , int szPage){. 
106da 20 61 73 73 65 72 74 28 20 70 43 61 63 68 65 2d   assert( pCache-
106db 3e 6e 52 65 66 3d 3d 30 20 26 26 20 70 43 61 63  >nRef==0 && pCac
106dc 68 65 2d 3e 70 44 69 72 74 79 3d 3d 30 20 29 3b  he->pDirty==0 );
106dd 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70  .  if( pCache->p
106de 43 61 63 68 65 20 29 7b 0a 20 20 20 20 73 71 6c  Cache ){.    sql
106df 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
106e0 2e 70 63 61 63 68 65 2e 78 44 65 73 74 72 6f 79  .pcache.xDestroy
106e1 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 29  (pCache->pCache)
106e2 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 70 43  ;.    pCache->pC
106e3 61 63 68 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  ache = 0;.  }.  
106e4 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 3d  pCache->szPage =
106e5 20 73 7a 50 61 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a   szPage;.}../*.*
106e6 2a 20 54 72 79 20 74 6f 20 6f 62 74 61 69 6e 20  * Try to obtain 
106e7 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20  a page from the 
106e8 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  cache..*/.SQLITE
106e9 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
106ea 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68 28  ite3PcacheFetch(
106eb 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 68  .  PCache *pCach
106ec 65 2c 20 20 20 20 20 20 20 2f 2a 20 4f 62 74 61  e,       /* Obta
106ed 69 6e 20 74 68 65 20 70 61 67 65 20 66 72 6f 6d  in the page from
106ee 20 74 68 69 73 20 63 61 63 68 65 20 2a 2f 0a 20   this cache */. 
106ef 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20   Pgno pgno,     
106f0 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e         /* Page n
106f1 75 6d 62 65 72 20 74 6f 20 6f 62 74 61 69 6e 20  umber to obtain 
106f2 2a 2f 0a 20 20 69 6e 74 20 63 72 65 61 74 65 46  */.  int createF
106f3 6c 61 67 2c 20 20 20 20 20 20 20 2f 2a 20 49 66  lag,       /* If
106f4 20 74 72 75 65 2c 20 63 72 65 61 74 65 20 70 61   true, create pa
106f5 67 65 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f  ge if it does no
106f6 74 20 65 78 69 73 74 20 61 6c 72 65 61 64 79 20  t exist already 
106f7 2a 2f 0a 20 20 50 67 48 64 72 20 2a 2a 70 70 50  */.  PgHdr **ppP
106f8 61 67 65 20 20 20 20 20 20 20 20 2f 2a 20 57 72  age        /* Wr
106f9 69 74 65 20 74 68 65 20 70 61 67 65 20 68 65 72  ite the page her
106fa 65 20 2a 2f 0a 29 7b 0a 20 20 50 67 48 64 72 20  e */.){.  PgHdr 
106fb 2a 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 69 6e  *pPage = 0;.  in
106fc 74 20 65 43 72 65 61 74 65 3b 0a 0a 20 20 61 73  t eCreate;..  as
106fd 73 65 72 74 28 20 70 43 61 63 68 65 21 3d 30 20  sert( pCache!=0 
106fe 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 72 65  );.  assert( cre
106ff 61 74 65 46 6c 61 67 3d 3d 31 20 7c 7c 20 63 72  ateFlag==1 || cr
10700 65 61 74 65 46 6c 61 67 3d 3d 30 20 29 3b 0a 20  eateFlag==0 );. 
10701 20 61 73 73 65 72 74 28 20 70 67 6e 6f 3e 30 20   assert( pgno>0 
10702 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
10703 70 6c 75 67 67 61 62 6c 65 20 63 61 63 68 65 20  pluggable cache 
10704 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a  (sqlite3_pcache*
10705 29 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 61  ) has not been a
10706 6c 6c 6f 63 61 74 65 64 2c 0a 20 20 2a 2a 20 61  llocated,.  ** a
10707 6c 6c 6f 63 61 74 65 20 69 74 20 6e 6f 77 2e 0a  llocate it now..
10708 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 43 61 63    */.  if( !pCac
10709 68 65 2d 3e 70 43 61 63 68 65 20 26 26 20 63 72  he->pCache && cr
1070a 65 61 74 65 46 6c 61 67 20 29 7b 0a 20 20 20 20  eateFlag ){.    
1070b 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a  sqlite3_pcache *
1070c 70 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 65  p;.    int nByte
1070d 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 70 43  ;.    nByte = pC
1070e 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 2b 20 70  ache->szPage + p
1070f 43 61 63 68 65 2d 3e 73 7a 45 78 74 72 61 20 2b  Cache->szExtra +
10710 20 73 69 7a 65 6f 66 28 50 67 48 64 72 29 3b 0a   sizeof(PgHdr);.
10711 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47      p = sqlite3G
10712 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63  lobalConfig.pcac
10713 68 65 2e 78 43 72 65 61 74 65 28 6e 42 79 74 65  he.xCreate(nByte
10714 2c 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65  , pCache->bPurge
10715 61 62 6c 65 29 3b 0a 20 20 20 20 69 66 28 20 21  able);.    if( !
10716 70 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  p ){.      retur
10717 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
10718 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
10719 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63  3GlobalConfig.pc
1071a 61 63 68 65 2e 78 43 61 63 68 65 73 69 7a 65 28  ache.xCachesize(
1071b 70 2c 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 29  p, pCache->nMax)
1071c 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 70 43  ;.    pCache->pC
1071d 61 63 68 65 20 3d 20 70 3b 0a 20 20 7d 0a 0a 20  ache = p;.  }.. 
1071e 20 65 43 72 65 61 74 65 20 3d 20 63 72 65 61 74   eCreate = creat
1071f 65 46 6c 61 67 20 2a 20 28 31 20 2b 20 28 21 70  eFlag * (1 + (!p
10720 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c  Cache->bPurgeabl
10721 65 20 7c 7c 20 21 70 43 61 63 68 65 2d 3e 70 44  e || !pCache->pD
10722 69 72 74 79 29 29 3b 0a 20 20 69 66 28 20 70 43  irty));.  if( pC
10723 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a  ache->pCache ){.
10724 20 20 20 20 70 50 61 67 65 20 3d 20 73 71 6c 69      pPage = sqli
10725 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
10726 70 63 61 63 68 65 2e 78 46 65 74 63 68 28 70 43  pcache.xFetch(pC
10727 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 67  ache->pCache, pg
10728 6e 6f 2c 20 65 43 72 65 61 74 65 29 3b 0a 20 20  no, eCreate);.  
10729 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 20  }..  if( !pPage 
1072a 26 26 20 65 43 72 65 61 74 65 3d 3d 31 20 29 7b  && eCreate==1 ){
1072b 0a 20 20 20 20 50 67 48 64 72 20 2a 70 50 67 3b  .    PgHdr *pPg;
1072c 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 61 20  ..    /* Find a 
1072d 64 69 72 74 79 20 70 61 67 65 20 74 6f 20 77 72  dirty page to wr
1072e 69 74 65 2d 6f 75 74 20 61 6e 64 20 72 65 63 79  ite-out and recy
1072f 63 6c 65 2e 20 46 69 72 73 74 20 74 72 79 20 74  cle. First try t
10730 6f 20 66 69 6e 64 20 61 20 0a 20 20 20 20 2a 2a  o find a .    **
10731 20 70 61 67 65 20 74 68 61 74 20 64 6f 65 73 20   page that does 
10732 6e 6f 74 20 72 65 71 75 69 72 65 20 61 20 6a 6f  not require a jo
10733 75 72 6e 61 6c 2d 73 79 6e 63 20 28 6f 6e 65 20  urnal-sync (one 
10734 77 69 74 68 20 50 47 48 44 52 5f 4e 45 45 44 5f  with PGHDR_NEED_
10735 53 59 4e 43 0a 20 20 20 20 2a 2a 20 63 6c 65 61  SYNC.    ** clea
10736 72 65 64 29 2c 20 62 75 74 20 69 66 20 74 68 61  red), but if tha
10737 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c  t is not possibl
10738 65 20 73 65 74 74 6c 65 20 66 6f 72 20 61 6e 79  e settle for any
10739 20 6f 74 68 65 72 20 0a 20 20 20 20 2a 2a 20 75   other .    ** u
1073a 6e 72 65 66 65 72 65 6e 63 65 64 20 64 69 72 74  nreferenced dirt
1073b 79 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20  y page..    */. 
1073c 20 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73     expensive_ass
1073d 65 72 74 28 20 70 63 61 63 68 65 43 68 65 63 6b  ert( pcacheCheck
1073e 53 79 6e 63 65 64 28 70 43 61 63 68 65 29 20 29  Synced(pCache) )
1073f 3b 0a 20 20 20 20 66 6f 72 28 70 50 67 3d 70 43  ;.    for(pPg=pC
10740 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 3b 20 0a  ache->pSynced; .
10741 20 20 20 20 20 20 20 20 70 50 67 20 26 26 20 28          pPg && (
10742 70 50 67 2d 3e 6e 52 65 66 20 7c 7c 20 28 70 50  pPg->nRef || (pP
10743 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e  g->flags&PGHDR_N
10744 45 45 44 5f 53 59 4e 43 29 29 3b 20 0a 20 20 20  EED_SYNC)); .   
10745 20 20 20 20 20 70 50 67 3d 70 50 67 2d 3e 70 44       pPg=pPg->pD
10746 69 72 74 79 50 72 65 76 0a 20 20 20 20 29 3b 0a  irtyPrev.    );.
10747 20 20 20 20 69 66 28 20 21 70 50 67 20 29 7b 0a      if( !pPg ){.
10748 20 20 20 20 20 20 66 6f 72 28 70 50 67 3d 70 43        for(pPg=pC
10749 61 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69 6c  ache->pDirtyTail
1074a 3b 20 70 50 67 20 26 26 20 70 50 67 2d 3e 6e 52  ; pPg && pPg->nR
1074b 65 66 3b 20 70 50 67 3d 70 50 67 2d 3e 70 44 69  ef; pPg=pPg->pDi
1074c 72 74 79 50 72 65 76 29 3b 0a 20 20 20 20 7d 0a  rtyPrev);.    }.
1074d 20 20 20 20 69 66 28 20 70 50 67 20 29 7b 0a 20      if( pPg ){. 
1074e 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20       int rc;.   
1074f 20 20 20 72 63 20 3d 20 70 43 61 63 68 65 2d 3e     rc = pCache->
10750 78 53 74 72 65 73 73 28 70 43 61 63 68 65 2d 3e  xStress(pCache->
10751 70 53 74 72 65 73 73 2c 20 70 50 67 29 3b 0a 20  pStress, pPg);. 
10752 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
10753 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51  ITE_OK && rc!=SQ
10754 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20  LITE_BUSY ){.   
10755 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
10756 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
10757 20 20 20 70 50 61 67 65 20 3d 20 73 71 6c 69 74     pPage = sqlit
10758 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
10759 63 61 63 68 65 2e 78 46 65 74 63 68 28 70 43 61  cache.xFetch(pCa
1075a 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 67 6e  che->pCache, pgn
1075b 6f 2c 20 32 29 3b 0a 20 20 7d 0a 0a 20 20 69 66  o, 2);.  }..  if
1075c 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 69  ( pPage ){.    i
1075d 66 28 20 21 70 50 61 67 65 2d 3e 70 44 61 74 61  f( !pPage->pData
1075e 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74   ){.      memset
1075f 28 70 50 61 67 65 2c 20 30 2c 20 73 69 7a 65 6f  (pPage, 0, sizeo
10760 66 28 50 67 48 64 72 29 20 2b 20 70 43 61 63 68  f(PgHdr) + pCach
10761 65 2d 3e 73 7a 45 78 74 72 61 29 3b 0a 20 20 20  e->szExtra);.   
10762 20 20 20 70 50 61 67 65 2d 3e 70 45 78 74 72 61     pPage->pExtra
10763 20 3d 20 28 76 6f 69 64 2a 29 26 70 50 61 67 65   = (void*)&pPage
10764 5b 31 5d 3b 0a 20 20 20 20 20 20 70 50 61 67 65  [1];.      pPage
10765 2d 3e 70 44 61 74 61 20 3d 20 28 76 6f 69 64 20  ->pData = (void 
10766 2a 29 26 28 28 63 68 61 72 20 2a 29 70 50 61 67  *)&((char *)pPag
10767 65 29 5b 73 69 7a 65 6f 66 28 50 67 48 64 72 29  e)[sizeof(PgHdr)
10768 20 2b 20 70 43 61 63 68 65 2d 3e 73 7a 45 78 74   + pCache->szExt
10769 72 61 5d 3b 0a 20 20 20 20 20 20 70 50 61 67 65  ra];.      pPage
1076a 2d 3e 70 43 61 63 68 65 20 3d 20 70 43 61 63 68  ->pCache = pCach
1076b 65 3b 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e  e;.      pPage->
1076c 70 67 6e 6f 20 3d 20 70 67 6e 6f 3b 0a 20 20 20  pgno = pgno;.   
1076d 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70   }.    assert( p
1076e 50 61 67 65 2d 3e 70 43 61 63 68 65 3d 3d 70 43  Page->pCache==pC
1076f 61 63 68 65 20 29 3b 0a 20 20 20 20 61 73 73 65  ache );.    asse
10770 72 74 28 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3d  rt( pPage->pgno=
10771 3d 70 67 6e 6f 20 29 3b 0a 20 20 20 20 61 73 73  =pgno );.    ass
10772 65 72 74 28 20 70 50 61 67 65 2d 3e 70 45 78 74  ert( pPage->pExt
10773 72 61 3d 3d 28 76 6f 69 64 20 2a 29 26 70 50 61  ra==(void *)&pPa
10774 67 65 5b 31 5d 20 29 3b 0a 0a 20 20 20 20 69 66  ge[1] );..    if
10775 28 20 30 3d 3d 70 50 61 67 65 2d 3e 6e 52 65 66  ( 0==pPage->nRef
10776 20 29 7b 0a 20 20 20 20 20 20 70 43 61 63 68 65   ){.      pCache
10777 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 0a  ->nRef++;.    }.
10778 20 20 20 20 70 50 61 67 65 2d 3e 6e 52 65 66 2b      pPage->nRef+
10779 2b 3b 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3d  +;.    if( pgno=
1077a 3d 31 20 29 7b 0a 20 20 20 20 20 20 70 43 61 63  =1 ){.      pCac
1077b 68 65 2d 3e 70 50 61 67 65 31 20 3d 20 70 50 61  he->pPage1 = pPa
1077c 67 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ge;.    }.  }.  
1077d 2a 70 70 50 61 67 65 20 3d 20 70 50 61 67 65 3b  *ppPage = pPage;
1077e 0a 20 20 72 65 74 75 72 6e 20 28 70 50 61 67 65  .  return (pPage
1077f 3d 3d 30 20 26 26 20 65 43 72 65 61 74 65 29 20  ==0 && eCreate) 
10780 3f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 3a  ? SQLITE_NOMEM :
10781 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
10782 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 74  *.** Decrement t
10783 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75  he reference cou
10784 6e 74 20 6f 6e 20 61 20 70 61 67 65 2e 20 49 66  nt on a page. If
10785 20 74 68 65 20 70 61 67 65 20 69 73 20 63 6c 65   the page is cle
10786 61 6e 20 61 6e 64 20 74 68 65 0a 2a 2a 20 72 65  an and the.** re
10787 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 64 72  ference count dr
10788 6f 70 73 20 74 6f 20 30 2c 20 74 68 65 6e 20 69  ops to 0, then i
10789 74 20 69 73 20 6d 61 64 65 20 65 6c 69 62 6c 65  t is made elible
1078a 20 66 6f 72 20 72 65 63 79 63 6c 69 6e 67 2e 0a   for recycling..
1078b 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1078c 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
1078d 61 63 68 65 52 65 6c 65 61 73 65 28 50 67 48 64  acheRelease(PgHd
1078e 72 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28  r *p){.  assert(
1078f 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20   p->nRef>0 );.  
10790 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28  p->nRef--;.  if(
10791 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20   p->nRef==0 ){. 
10792 20 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 68     PCache *pCach
10793 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20  e = p->pCache;. 
10794 20 20 20 70 43 61 63 68 65 2d 3e 6e 52 65 66 2d     pCache->nRef-
10795 2d 3b 0a 20 20 20 20 69 66 28 20 28 70 2d 3e 66  -;.    if( (p->f
10796 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59  lags&PGHDR_DIRTY
10797 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 63  )==0 ){.      pc
10798 61 63 68 65 55 6e 70 69 6e 28 70 29 3b 0a 20 20  acheUnpin(p);.  
10799 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f    }else{.      /
1079a 2a 20 4d 6f 76 65 20 74 68 65 20 70 61 67 65 20  * Move the page 
1079b 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66 20 74  to the head of t
1079c 68 65 20 64 69 72 74 79 20 6c 69 73 74 2e 20 2a  he dirty list. *
1079d 2f 0a 20 20 20 20 20 20 70 63 61 63 68 65 52 65  /.      pcacheRe
1079e 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c 69 73  moveFromDirtyLis
1079f 74 28 70 29 3b 0a 20 20 20 20 20 20 70 63 61 63  t(p);.      pcac
107a0 68 65 41 64 64 54 6f 44 69 72 74 79 4c 69 73 74  heAddToDirtyList
107a1 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  (p);.    }.  }.}
107a2 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 65 61 73 65  ../*.** Increase
107a3 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63   the reference c
107a4 6f 75 6e 74 20 6f 66 20 61 20 73 75 70 70 6c 69  ount of a suppli
107a5 65 64 20 70 61 67 65 20 62 79 20 31 2e 0a 2a 2f  ed page by 1..*/
107a6 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
107a7 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
107a8 68 65 52 65 66 28 50 67 48 64 72 20 2a 70 29 7b  heRef(PgHdr *p){
107a9 0a 20 20 61 73 73 65 72 74 28 70 2d 3e 6e 52 65  .  assert(p->nRe
107aa 66 3e 30 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b  f>0);.  p->nRef+
107ab 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f 70  +;.}../*.** Drop
107ac 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65   a page from the
107ad 20 63 61 63 68 65 2e 20 54 68 65 72 65 20 6d 75   cache. There mu
107ae 73 74 20 62 65 20 65 78 61 63 74 6c 79 20 6f 6e  st be exactly on
107af 65 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74  e reference to t
107b0 68 65 0a 2a 2a 20 70 61 67 65 2e 20 54 68 69 73  he.** page. This
107b1 20 66 75 6e 63 74 69 6f 6e 20 64 65 6c 65 74 65   function delete
107b2 73 20 74 68 61 74 20 72 65 66 65 72 65 6e 63 65  s that reference
107b3 2c 20 73 6f 20 61 66 74 65 72 20 69 74 20 72 65  , so after it re
107b4 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 70 61 67  turns the.** pag
107b5 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  e pointed to by 
107b6 70 20 69 73 20 69 6e 76 61 6c 69 64 2e 0a 2a 2f  p is invalid..*/
107b7 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
107b8 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
107b9 68 65 44 72 6f 70 28 50 67 48 64 72 20 2a 70 29  heDrop(PgHdr *p)
107ba 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 63  {.  PCache *pCac
107bb 68 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  he;.  assert( p-
107bc 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a 20 20 69 66  >nRef==1 );.  if
107bd 28 20 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52  ( p->flags&PGHDR
107be 5f 44 49 52 54 59 20 29 7b 0a 20 20 20 20 70 63  _DIRTY ){.    pc
107bf 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69  acheRemoveFromDi
107c0 72 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 7d 0a  rtyList(p);.  }.
107c1 20 20 70 43 61 63 68 65 20 3d 20 70 2d 3e 70 43    pCache = p->pC
107c2 61 63 68 65 3b 0a 20 20 70 43 61 63 68 65 2d 3e  ache;.  pCache->
107c3 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 2d  nRef--;.  if( p-
107c4 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20  >pgno==1 ){.    
107c5 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31 20 3d  pCache->pPage1 =
107c6 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65   0;.  }.  sqlite
107c7 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63  3GlobalConfig.pc
107c8 61 63 68 65 2e 78 55 6e 70 69 6e 28 70 43 61 63  ache.xUnpin(pCac
107c9 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 2c 20 31  he->pCache, p, 1
107ca 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65  );.}../*.** Make
107cb 20 73 75 72 65 20 74 68 65 20 70 61 67 65 20 69   sure the page i
107cc 73 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74  s marked as dirt
107cd 79 2e 20 49 66 20 69 74 20 69 73 6e 27 74 20 64  y. If it isn't d
107ce 69 72 74 79 20 61 6c 72 65 61 64 79 2c 0a 2a 2a  irty already,.**
107cf 20 6d 61 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f 0a   make it so..*/.
107d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
107d1 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
107d2 65 4d 61 6b 65 44 69 72 74 79 28 50 67 48 64 72  eMakeDirty(PgHdr
107d3 20 2a 70 29 7b 0a 20 20 70 2d 3e 66 6c 61 67 73   *p){.  p->flags
107d4 20 26 3d 20 7e 50 47 48 44 52 5f 44 4f 4e 54 5f   &= ~PGHDR_DONT_
107d5 57 52 49 54 45 3b 0a 20 20 61 73 73 65 72 74 28  WRITE;.  assert(
107d6 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20   p->nRef>0 );.  
107d7 69 66 28 20 30 3d 3d 28 70 2d 3e 66 6c 61 67 73  if( 0==(p->flags
107d8 20 26 20 50 47 48 44 52 5f 44 49 52 54 59 29 20   & PGHDR_DIRTY) 
107d9 29 7b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20  ){.    p->flags 
107da 7c 3d 20 50 47 48 44 52 5f 44 49 52 54 59 3b 0a  |= PGHDR_DIRTY;.
107db 20 20 20 20 70 63 61 63 68 65 41 64 64 54 6f 44      pcacheAddToD
107dc 69 72 74 79 4c 69 73 74 28 20 70 29 3b 0a 20 20  irtyList( p);.  
107dd 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20  }.}../*.** Make 
107de 73 75 72 65 20 74 68 65 20 70 61 67 65 20 69 73  sure the page is
107df 20 6d 61 72 6b 65 64 20 61 73 20 63 6c 65 61 6e   marked as clean
107e0 2e 20 49 66 20 69 74 20 69 73 6e 27 74 20 63 6c  . If it isn't cl
107e1 65 61 6e 20 61 6c 72 65 61 64 79 2c 0a 2a 2a 20  ean already,.** 
107e2 6d 61 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f 0a 53  make it so..*/.S
107e3 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
107e4 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
107e5 4d 61 6b 65 43 6c 65 61 6e 28 50 67 48 64 72 20  MakeClean(PgHdr 
107e6 2a 70 29 7b 0a 20 20 69 66 28 20 28 70 2d 3e 66  *p){.  if( (p->f
107e7 6c 61 67 73 20 26 20 50 47 48 44 52 5f 44 49 52  lags & PGHDR_DIR
107e8 54 59 29 20 29 7b 0a 20 20 20 20 70 63 61 63 68  TY) ){.    pcach
107e9 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79  eRemoveFromDirty
107ea 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 70 2d 3e  List(p);.    p->
107eb 66 6c 61 67 73 20 26 3d 20 7e 28 50 47 48 44 52  flags &= ~(PGHDR
107ec 5f 44 49 52 54 59 7c 50 47 48 44 52 5f 4e 45 45  _DIRTY|PGHDR_NEE
107ed 44 5f 53 59 4e 43 29 3b 0a 20 20 20 20 69 66 28  D_SYNC);.    if(
107ee 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20   p->nRef==0 ){. 
107ef 20 20 20 20 20 70 63 61 63 68 65 55 6e 70 69 6e       pcacheUnpin
107f0 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  (p);.    }.  }.}
107f1 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 65 76 65  ../*.** Make eve
107f2 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 63  ry page in the c
107f3 61 63 68 65 20 63 6c 65 61 6e 2e 0a 2a 2f 0a 53  ache clean..*/.S
107f4 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
107f5 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
107f6 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68 65 20  CleanAll(PCache 
107f7 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48 64  *pCache){.  PgHd
107f8 72 20 2a 70 3b 0a 20 20 77 68 69 6c 65 28 20 28  r *p;.  while( (
107f9 70 20 3d 20 70 43 61 63 68 65 2d 3e 70 44 69 72  p = pCache->pDir
107fa 74 79 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71  ty)!=0 ){.    sq
107fb 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43  lite3PcacheMakeC
107fc 6c 65 61 6e 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a  lean(p);.  }.}..
107fd 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20  /*.** Clear the 
107fe 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20  PGHDR_NEED_SYNC 
107ff 66 6c 61 67 20 66 72 6f 6d 20 61 6c 6c 20 64 69  flag from all di
10800 72 74 79 20 70 61 67 65 73 2e 0a 2a 2f 0a 53 51  rty pages..*/.SQ
10801 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
10802 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43  d sqlite3PcacheC
10803 6c 65 61 72 53 79 6e 63 46 6c 61 67 73 28 50 43  learSyncFlags(PC
10804 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20  ache *pCache){. 
10805 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 66 6f 72   PgHdr *p;.  for
10806 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74  (p=pCache->pDirt
10807 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 69 72 74  y; p; p=p->pDirt
10808 79 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e 66  yNext){.    p->f
10809 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e  lags &= ~PGHDR_N
1080a 45 45 44 5f 53 59 4e 43 3b 0a 20 20 7d 0a 20 20  EED_SYNC;.  }.  
1080b 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 20  pCache->pSynced 
1080c 3d 20 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79  = pCache->pDirty
1080d 54 61 69 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  Tail;.}../*.** C
1080e 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 20 6e  hange the page n
1080f 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20 70 20  umber of page p 
10810 74 6f 20 6e 65 77 50 67 6e 6f 2e 20 0a 2a 2f 0a  to newPgno. .*/.
10811 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
10812 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
10813 65 4d 6f 76 65 28 50 67 48 64 72 20 2a 70 2c 20  eMove(PgHdr *p, 
10814 50 67 6e 6f 20 6e 65 77 50 67 6e 6f 29 7b 0a 20  Pgno newPgno){. 
10815 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 20   PCache *pCache 
10816 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20 20 61  = p->pCache;.  a
10817 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30  ssert( p->nRef>0
10818 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 65   );.  assert( ne
10819 77 50 67 6e 6f 3e 30 20 29 3b 0a 20 20 73 71 6c  wPgno>0 );.  sql
1081a 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
1081b 2e 70 63 61 63 68 65 2e 78 52 65 6b 65 79 28 70  .pcache.xRekey(p
1081c 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70  Cache->pCache, p
1081d 2c 20 70 2d 3e 70 67 6e 6f 2c 20 6e 65 77 50 67  , p->pgno, newPg
1081e 6e 6f 29 3b 0a 20 20 70 2d 3e 70 67 6e 6f 20 3d  no);.  p->pgno =
1081f 20 6e 65 77 50 67 6e 6f 3b 0a 20 20 69 66 28 20   newPgno;.  if( 
10820 28 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f  (p->flags&PGHDR_
10821 44 49 52 54 59 29 20 26 26 20 28 70 2d 3e 66 6c  DIRTY) && (p->fl
10822 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53  ags&PGHDR_NEED_S
10823 59 4e 43 29 20 29 7b 0a 20 20 20 20 70 63 61 63  YNC) ){.    pcac
10824 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74  heRemoveFromDirt
10825 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 70 63  yList(p);.    pc
10826 61 63 68 65 41 64 64 54 6f 44 69 72 74 79 4c 69  acheAddToDirtyLi
10827 73 74 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  st(p);.  }.}../*
10828 0a 2a 2a 20 44 72 6f 70 20 65 76 65 72 79 20 63  .** Drop every c
10829 61 63 68 65 20 65 6e 74 72 79 20 77 68 6f 73 65  ache entry whose
1082a 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20   page number is 
1082b 67 72 65 61 74 65 72 20 74 68 61 6e 20 22 70 67  greater than "pg
1082c 6e 6f 22 2e 20 54 68 65 0a 2a 2a 20 63 61 6c 6c  no". The.** call
1082d 65 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74  er must ensure t
1082e 68 61 74 20 74 68 65 72 65 20 61 72 65 20 6e 6f  hat there are no
1082f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66   outstanding ref
10830 65 72 65 6e 63 65 73 20 74 6f 20 61 6e 79 20 70  erences to any p
10831 61 67 65 73 0a 2a 2a 20 6f 74 68 65 72 20 74 68  ages.** other th
10832 61 6e 20 70 61 67 65 20 31 20 77 69 74 68 20 61  an page 1 with a
10833 20 70 61 67 65 20 6e 75 6d 62 65 72 20 67 72 65   page number gre
10834 61 74 65 72 20 74 68 61 6e 20 70 67 6e 6f 2e 0a  ater than pgno..
10835 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69  **.** If there i
10836 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f  s a reference to
10837 20 70 61 67 65 20 31 20 61 6e 64 20 74 68 65 20   page 1 and the 
10838 70 67 6e 6f 20 70 61 72 61 6d 65 74 65 72 20 70  pgno parameter p
10839 61 73 73 65 64 20 74 6f 20 74 68 69 73 0a 2a 2a  assed to this.**
1083a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 30 2c 20   function is 0, 
1083b 74 68 65 6e 20 74 68 65 20 64 61 74 61 20 61 72  then the data ar
1083c 65 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ea associated wi
1083d 74 68 20 70 61 67 65 20 31 20 69 73 20 7a 65 72  th page 1 is zer
1083e 6f 65 64 2c 20 62 75 74 0a 2a 2a 20 74 68 65 20  oed, but.** the 
1083f 70 61 67 65 20 6f 62 6a 65 63 74 20 69 73 20 6e  page object is n
10840 6f 74 20 64 72 6f 70 70 65 64 2e 0a 2a 2f 0a 53  ot dropped..*/.S
10841 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
10842 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
10843 54 72 75 6e 63 61 74 65 28 50 43 61 63 68 65 20  Truncate(PCache 
10844 2a 70 43 61 63 68 65 2c 20 50 67 6e 6f 20 70 67  *pCache, Pgno pg
10845 6e 6f 29 7b 0a 20 20 69 66 28 20 70 43 61 63 68  no){.  if( pCach
10846 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20  e->pCache ){.   
10847 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 20 20 50   PgHdr *p;.    P
10848 67 48 64 72 20 2a 70 4e 65 78 74 3b 0a 20 20 20  gHdr *pNext;.   
10849 20 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70   for(p=pCache->p
1084a 44 69 72 74 79 3b 20 70 3b 20 70 3d 70 4e 65 78  Dirty; p; p=pNex
1084b 74 29 7b 0a 20 20 20 20 20 20 70 4e 65 78 74 20  t){.      pNext 
1084c 3d 20 70 2d 3e 70 44 69 72 74 79 4e 65 78 74 3b  = p->pDirtyNext;
1084d 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 67  .      if( p->pg
1084e 6e 6f 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 20  no>pgno ){.     
1084f 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 66 6c     assert( p->fl
10850 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 20  ags&PGHDR_DIRTY 
10851 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
10852 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61  e3PcacheMakeClea
10853 6e 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  n(p);.      }.  
10854 20 20 7d 0a 20 20 20 20 69 66 28 20 70 67 6e 6f    }.    if( pgno
10855 3d 3d 30 20 26 26 20 70 43 61 63 68 65 2d 3e 70  ==0 && pCache->p
10856 50 61 67 65 31 20 29 7b 0a 20 20 20 20 20 20 6d  Page1 ){.      m
10857 65 6d 73 65 74 28 70 43 61 63 68 65 2d 3e 70 50  emset(pCache->pP
10858 61 67 65 31 2d 3e 70 44 61 74 61 2c 20 30 2c 20  age1->pData, 0, 
10859 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 29 3b  pCache->szPage);
1085a 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d 20 31 3b  .      pgno = 1;
1085b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
1085c 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
1085d 63 61 63 68 65 2e 78 54 72 75 6e 63 61 74 65 28  cache.xTruncate(
1085e 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20  pCache->pCache, 
1085f 70 67 6e 6f 2b 31 29 3b 0a 20 20 7d 0a 7d 0a 0a  pgno+1);.  }.}..
10860 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 63 61  /*.** Close a ca
10861 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  che..*/.SQLITE_P
10862 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
10863 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65 28 50  te3PcacheClose(P
10864 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a  Cache *pCache){.
10865 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70 43    if( pCache->pC
10866 61 63 68 65 20 29 7b 0a 20 20 20 20 73 71 6c 69  ache ){.    sqli
10867 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
10868 70 63 61 63 68 65 2e 78 44 65 73 74 72 6f 79 28  pcache.xDestroy(
10869 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 29 3b  pCache->pCache);
1086a 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 44  .  }.}../* .** D
1086b 69 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74 65  iscard the conte
1086c 6e 74 73 20 6f 66 20 74 68 65 20 63 61 63 68 65  nts of the cache
1086d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1086e 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1086f 50 63 61 63 68 65 43 6c 65 61 72 28 50 43 61 63  PcacheClear(PCac
10870 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 73  he *pCache){.  s
10871 71 6c 69 74 65 33 50 63 61 63 68 65 54 72 75 6e  qlite3PcacheTrun
10872 63 61 74 65 28 70 43 61 63 68 65 2c 20 30 29 3b  cate(pCache, 0);
10873 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 72 67 65 20  .}../*.** Merge 
10874 74 77 6f 20 6c 69 73 74 73 20 6f 66 20 70 61 67  two lists of pag
10875 65 73 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20  es connected by 
10876 70 44 69 72 74 79 20 61 6e 64 20 69 6e 20 70 67  pDirty and in pg
10877 6e 6f 20 6f 72 64 65 72 2e 0a 2a 2a 20 44 6f 20  no order..** Do 
10878 6e 6f 74 20 62 6f 74 68 20 66 69 78 69 6e 67 20  not both fixing 
10879 74 68 65 20 70 44 69 72 74 79 50 72 65 76 20 70  the pDirtyPrev p
1087a 6f 69 6e 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 74  ointers..*/.stat
1087b 69 63 20 50 67 48 64 72 20 2a 70 63 61 63 68 65  ic PgHdr *pcache
1087c 4d 65 72 67 65 44 69 72 74 79 4c 69 73 74 28 50  MergeDirtyList(P
1087d 67 48 64 72 20 2a 70 41 2c 20 50 67 48 64 72 20  gHdr *pA, PgHdr 
1087e 2a 70 42 29 7b 0a 20 20 50 67 48 64 72 20 72 65  *pB){.  PgHdr re
1087f 73 75 6c 74 2c 20 2a 70 54 61 69 6c 3b 0a 20 20  sult, *pTail;.  
10880 70 54 61 69 6c 20 3d 20 26 72 65 73 75 6c 74 3b  pTail = &result;
10881 0a 20 20 77 68 69 6c 65 28 20 70 41 20 26 26 20  .  while( pA && 
10882 70 42 20 29 7b 0a 20 20 20 20 69 66 28 20 70 41  pB ){.    if( pA
10883 2d 3e 70 67 6e 6f 3c 70 42 2d 3e 70 67 6e 6f 20  ->pgno<pB->pgno 
10884 29 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e  ){.      pTail->
10885 70 44 69 72 74 79 20 3d 20 70 41 3b 0a 20 20 20  pDirty = pA;.   
10886 20 20 20 70 54 61 69 6c 20 3d 20 70 41 3b 0a 20     pTail = pA;. 
10887 20 20 20 20 20 70 41 20 3d 20 70 41 2d 3e 70 44       pA = pA->pD
10888 69 72 74 79 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  irty;.    }else{
10889 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70 44  .      pTail->pD
1088a 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 20 20 20  irty = pB;.     
1088b 20 70 54 61 69 6c 20 3d 20 70 42 3b 0a 20 20 20   pTail = pB;.   
1088c 20 20 20 70 42 20 3d 20 70 42 2d 3e 70 44 69 72     pB = pB->pDir
1088d 74 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ty;.    }.  }.  
1088e 69 66 28 20 70 41 20 29 7b 0a 20 20 20 20 70 54  if( pA ){.    pT
1088f 61 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 41  ail->pDirty = pA
10890 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 42  ;.  }else if( pB
10891 20 29 7b 0a 20 20 20 20 70 54 61 69 6c 2d 3e 70   ){.    pTail->p
10892 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 7d 65  Dirty = pB;.  }e
10893 6c 73 65 7b 0a 20 20 20 20 70 54 61 69 6c 2d 3e  lse{.    pTail->
10894 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 7d 0a  pDirty = 0;.  }.
10895 20 20 72 65 74 75 72 6e 20 72 65 73 75 6c 74 2e    return result.
10896 70 44 69 72 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pDirty;.}../*.**
10897 20 53 6f 72 74 20 74 68 65 20 6c 69 73 74 20 6f   Sort the list o
10898 66 20 70 61 67 65 73 20 69 6e 20 61 63 63 65 6e  f pages in accen
10899 64 69 6e 67 20 6f 72 64 65 72 20 62 79 20 70 67  ding order by pg
1089a 6e 6f 2e 20 20 50 61 67 65 73 20 61 72 65 0a 2a  no.  Pages are.*
1089b 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 70  * connected by p
1089c 44 69 72 74 79 20 70 6f 69 6e 74 65 72 73 2e 20  Dirty pointers. 
1089d 20 54 68 65 20 70 44 69 72 74 79 50 72 65 76 20   The pDirtyPrev 
1089e 70 6f 69 6e 74 65 72 73 20 61 72 65 0a 2a 2a 20  pointers are.** 
1089f 63 6f 72 72 75 70 74 65 64 20 62 79 20 74 68 69  corrupted by thi
108a0 73 20 73 6f 72 74 2e 0a 2a 2a 0a 2a 2a 20 53 69  s sort..**.** Si
108a1 6e 63 65 20 74 68 65 72 65 20 63 61 6e 6e 6f 74  nce there cannot
108a2 20 62 65 20 6d 6f 72 65 20 74 68 61 6e 20 32 5e   be more than 2^
108a3 33 31 20 64 69 73 74 69 6e 63 74 20 70 61 67 65  31 distinct page
108a4 73 20 69 6e 20 61 20 64 61 74 61 62 61 73 65 2c  s in a database,
108a5 0a 2a 2a 20 74 68 65 72 65 20 63 61 6e 6e 6f 74  .** there cannot
108a6 20 62 65 20 6d 6f 72 65 20 74 68 61 6e 20 33 31   be more than 31
108a7 20 62 75 63 6b 65 74 73 20 72 65 71 75 69 72 65   buckets require
108a8 64 20 62 79 20 74 68 65 20 6d 65 72 67 65 20 73  d by the merge s
108a9 6f 72 74 65 72 2e 0a 2a 2a 20 4f 6e 65 20 65 78  orter..** One ex
108aa 74 72 61 20 62 75 63 6b 65 74 20 69 73 20 61 64  tra bucket is ad
108ab 64 65 64 20 74 6f 20 63 61 74 63 68 20 6f 76 65  ded to catch ove
108ac 72 66 6c 6f 77 20 69 6e 20 63 61 73 65 20 73 6f  rflow in case so
108ad 6d 65 74 68 69 6e 67 0a 2a 2a 20 65 76 65 72 20  mething.** ever 
108ae 63 68 61 6e 67 65 73 20 74 6f 20 6d 61 6b 65 20  changes to make 
108af 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 65 6e  the previous sen
108b0 74 65 6e 63 65 20 69 6e 63 6f 72 72 65 63 74 2e  tence incorrect.
108b1 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 5f 53 4f  .*/.#define N_SO
108b2 52 54 5f 42 55 43 4b 45 54 20 20 33 32 0a 73 74  RT_BUCKET  32.st
108b3 61 74 69 63 20 50 67 48 64 72 20 2a 70 63 61 63  atic PgHdr *pcac
108b4 68 65 53 6f 72 74 44 69 72 74 79 4c 69 73 74 28  heSortDirtyList(
108b5 50 67 48 64 72 20 2a 70 49 6e 29 7b 0a 20 20 50  PgHdr *pIn){.  P
108b6 67 48 64 72 20 2a 61 5b 4e 5f 53 4f 52 54 5f 42  gHdr *a[N_SORT_B
108b7 55 43 4b 45 54 5d 2c 20 2a 70 3b 0a 20 20 69 6e  UCKET], *p;.  in
108b8 74 20 69 3b 0a 20 20 6d 65 6d 73 65 74 28 61 2c  t i;.  memset(a,
108b9 20 30 2c 20 73 69 7a 65 6f 66 28 61 29 29 3b 0a   0, sizeof(a));.
108ba 20 20 77 68 69 6c 65 28 20 70 49 6e 20 29 7b 0a    while( pIn ){.
108bb 20 20 20 20 70 20 3d 20 70 49 6e 3b 0a 20 20 20      p = pIn;.   
108bc 20 70 49 6e 20 3d 20 70 2d 3e 70 44 69 72 74 79   pIn = p->pDirty
108bd 3b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 79 20  ;.    p->pDirty 
108be 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  = 0;.    for(i=0
108bf 3b 20 41 4c 57 41 59 53 28 69 3c 4e 5f 53 4f 52  ; ALWAYS(i<N_SOR
108c0 54 5f 42 55 43 4b 45 54 2d 31 29 3b 20 69 2b 2b  T_BUCKET-1); i++
108c1 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 5b 69  ){.      if( a[i
108c2 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  ]==0 ){.        
108c3 61 5b 69 5d 20 3d 20 70 3b 0a 20 20 20 20 20 20  a[i] = p;.      
108c4 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
108c5 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 20  else{.        p 
108c6 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44 69 72  = pcacheMergeDir
108c7 74 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70 29 3b  tyList(a[i], p);
108c8 0a 20 20 20 20 20 20 20 20 61 5b 69 5d 20 3d 20  .        a[i] = 
108c9 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  0;.      }.    }
108ca 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 69  .    if( NEVER(i
108cb 3d 3d 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 2d  ==N_SORT_BUCKET-
108cc 31 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54  1) ){.      /* T
108cd 6f 20 67 65 74 20 68 65 72 65 2c 20 74 68 65 72  o get here, ther
108ce 65 20 6e 65 65 64 20 74 6f 20 62 65 20 32 5e 28  e need to be 2^(
108cf 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 29 20 65  N_SORT_BUCKET) e
108d0 6c 65 6d 65 6e 74 73 20 69 6e 0a 20 20 20 20 20  lements in.     
108d1 20 2a 2a 20 74 68 65 20 69 6e 70 75 74 20 6c 69   ** the input li
108d2 73 74 2e 20 20 42 75 74 20 74 68 61 74 20 69 73  st.  But that is
108d3 20 69 6d 70 6f 73 73 69 62 6c 65 2e 0a 20 20 20   impossible..   
108d4 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 5b 69 5d     */.      a[i]
108d5 20 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44 69   = pcacheMergeDi
108d6 72 74 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70 29  rtyList(a[i], p)
108d7 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 20  ;.    }.  }.  p 
108d8 3d 20 61 5b 30 5d 3b 0a 20 20 66 6f 72 28 69 3d  = a[0];.  for(i=
108d9 31 3b 20 69 3c 4e 5f 53 4f 52 54 5f 42 55 43 4b  1; i<N_SORT_BUCK
108da 45 54 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20  ET; i++){.    p 
108db 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44 69 72  = pcacheMergeDir
108dc 74 79 4c 69 73 74 28 70 2c 20 61 5b 69 5d 29 3b  tyList(p, a[i]);
108dd 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b  .  }.  return p;
108de 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
108df 20 61 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 64   a list of all d
108e0 69 72 74 79 20 70 61 67 65 73 20 69 6e 20 74 68  irty pages in th
108e1 65 20 63 61 63 68 65 2c 20 73 6f 72 74 65 64 20  e cache, sorted 
108e2 62 79 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a  by page number..
108e3 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
108e4 45 20 50 67 48 64 72 20 2a 73 71 6c 69 74 65 33  E PgHdr *sqlite3
108e5 50 63 61 63 68 65 44 69 72 74 79 4c 69 73 74 28  PcacheDirtyList(
108e6 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b  PCache *pCache){
108e7 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 66  .  PgHdr *p;.  f
108e8 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69  or(p=pCache->pDi
108e9 72 74 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 69  rty; p; p=p->pDi
108ea 72 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d  rtyNext){.    p-
108eb 3e 70 44 69 72 74 79 20 3d 20 70 2d 3e 70 44 69  >pDirty = p->pDi
108ec 72 74 79 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 72  rtyNext;.  }.  r
108ed 65 74 75 72 6e 20 70 63 61 63 68 65 53 6f 72 74  eturn pcacheSort
108ee 44 69 72 74 79 4c 69 73 74 28 70 43 61 63 68 65  DirtyList(pCache
108ef 2d 3e 70 44 69 72 74 79 29 3b 0a 7d 0a 0a 2f 2a  ->pDirty);.}../*
108f0 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20   .** Return the 
108f1 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  total number of 
108f2 72 65 66 65 72 65 6e 63 65 64 20 70 61 67 65 73  referenced pages
108f3 20 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 63   held by the cac
108f4 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  he..*/.SQLITE_PR
108f5 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
108f6 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28  3PcacheRefCount(
108f7 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b  PCache *pCache){
108f8 0a 20 20 72 65 74 75 72 6e 20 70 43 61 63 68 65  .  return pCache
108f9 2d 3e 6e 52 65 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ->nRef;.}../*.**
108fa 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
108fb 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73  er of references
108fc 20 74 6f 20 74 68 65 20 70 61 67 65 20 73 75 70   to the page sup
108fd 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72 67 75  plied as an argu
108fe 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ment..*/.SQLITE_
108ff 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
10900 74 65 33 50 63 61 63 68 65 50 61 67 65 52 65 66  te3PcachePageRef
10901 63 6f 75 6e 74 28 50 67 48 64 72 20 2a 70 29 7b  count(PgHdr *p){
10902 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65  .  return p->nRe
10903 66 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74  f;.}../* .** Ret
10904 75 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75  urn the total nu
10905 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e  mber of pages in
10906 20 74 68 65 20 63 61 63 68 65 2e 0a 2a 2f 0a 53   the cache..*/.S
10907 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
10908 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 50  t sqlite3PcacheP
10909 61 67 65 63 6f 75 6e 74 28 50 43 61 63 68 65 20  agecount(PCache 
1090a 2a 70 43 61 63 68 65 29 7b 0a 20 20 69 6e 74 20  *pCache){.  int 
1090b 6e 50 61 67 65 20 3d 20 30 3b 0a 20 20 69 66 28  nPage = 0;.  if(
1090c 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20   pCache->pCache 
1090d 29 7b 0a 20 20 20 20 6e 50 61 67 65 20 3d 20 73  ){.    nPage = s
1090e 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
1090f 69 67 2e 70 63 61 63 68 65 2e 78 50 61 67 65 63  ig.pcache.xPagec
10910 6f 75 6e 74 28 70 43 61 63 68 65 2d 3e 70 43 61  ount(pCache->pCa
10911 63 68 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  che);.  }.  retu
10912 72 6e 20 6e 50 61 67 65 3b 0a 7d 0a 0a 23 69 66  rn nPage;.}..#if
10913 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
10914 2f 2a 0a 2a 2a 20 47 65 74 20 74 68 65 20 73 75  /*.** Get the su
10915 67 67 65 73 74 65 64 20 63 61 63 68 65 2d 73 69  ggested cache-si
10916 7a 65 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c  ze value..*/.SQL
10917 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
10918 73 71 6c 69 74 65 33 50 63 61 63 68 65 47 65 74  sqlite3PcacheGet
10919 43 61 63 68 65 73 69 7a 65 28 50 43 61 63 68 65  Cachesize(PCache
1091a 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 72 65 74   *pCache){.  ret
1091b 75 72 6e 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78  urn pCache->nMax
1091c 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
1091d 2a 20 53 65 74 20 74 68 65 20 73 75 67 67 65 73  * Set the sugges
1091e 74 65 64 20 63 61 63 68 65 2d 73 69 7a 65 20 76  ted cache-size v
1091f 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  alue..*/.SQLITE_
10920 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
10921 69 74 65 33 50 63 61 63 68 65 53 65 74 43 61 63  ite3PcacheSetCac
10922 68 65 73 69 7a 65 28 50 43 61 63 68 65 20 2a 70  hesize(PCache *p
10923 43 61 63 68 65 2c 20 69 6e 74 20 6d 78 50 61 67  Cache, int mxPag
10924 65 29 7b 0a 20 20 70 43 61 63 68 65 2d 3e 6e 4d  e){.  pCache->nM
10925 61 78 20 3d 20 6d 78 50 61 67 65 3b 0a 20 20 69  ax = mxPage;.  i
10926 66 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68  f( pCache->pCach
10927 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  e ){.    sqlite3
10928 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61  GlobalConfig.pca
10929 63 68 65 2e 78 43 61 63 68 65 73 69 7a 65 28 70  che.xCachesize(p
1092a 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 6d  Cache->pCache, m
1092b 78 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 23  xPage);.  }.}..#
1092c 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
1092d 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 29 20 7c  E_CHECK_PAGES) |
1092e 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
1092f 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 46 6f  _DEBUG)./*.** Fo
10930 72 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65  r all dirty page
10931 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74  s currently in t
10932 68 65 20 63 61 63 68 65 2c 20 69 6e 76 6f 6b 65  he cache, invoke
10933 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a   the specified.*
10934 2a 20 63 61 6c 6c 62 61 63 6b 2e 20 54 68 69 73  * callback. This
10935 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66   is only used if
10936 20 74 68 65 20 53 51 4c 49 54 45 5f 43 48 45 43   the SQLITE_CHEC
10937 4b 5f 50 41 47 45 53 20 6d 61 63 72 6f 20 69 73  K_PAGES macro is
10938 0a 2a 2a 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a  .** defined..*/.
10939 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1093a 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
1093b 65 49 74 65 72 61 74 65 44 69 72 74 79 28 50 43  eIterateDirty(PC
1093c 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 76 6f  ache *pCache, vo
1093d 69 64 20 28 2a 78 49 74 65 72 29 28 50 67 48 64  id (*xIter)(PgHd
1093e 72 20 2a 29 29 7b 0a 20 20 50 67 48 64 72 20 2a  r *)){.  PgHdr *
1093f 70 44 69 72 74 79 3b 0a 20 20 66 6f 72 28 70 44  pDirty;.  for(pD
10940 69 72 74 79 3d 70 43 61 63 68 65 2d 3e 70 44 69  irty=pCache->pDi
10941 72 74 79 3b 20 70 44 69 72 74 79 3b 20 70 44 69  rty; pDirty; pDi
10942 72 74 79 3d 70 44 69 72 74 79 2d 3e 70 44 69 72  rty=pDirty->pDir
10943 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 78 49 74  tyNext){.    xIt
10944 65 72 28 70 44 69 72 74 79 29 3b 0a 20 20 7d 0a  er(pDirty);.  }.
10945 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a  }.#endif../*****
10946 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
10947 20 70 63 61 63 68 65 2e 63 20 2a 2a 2a 2a 2a 2a   pcache.c ******
10948 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10949 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1094a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
1094b 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
1094c 66 69 6c 65 20 70 63 61 63 68 65 31 2e 63 20 2a  file pcache1.c *
1094d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1094e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1094f 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
10950 32 30 30 38 20 4e 6f 76 65 6d 62 65 72 20 30 35  2008 November 05
10951 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
10952 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
10953 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
10954 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
10955 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
10956 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
10957 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
10958 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
10959 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
1095a 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
1095b 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
1095c 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
1095d 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
1095e 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
1095f 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
10960 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
10961 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
10962 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
10963 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10964 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10965 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10966 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
10967 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
10968 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 64  implements the d
10969 65 66 61 75 6c 74 20 70 61 67 65 20 63 61 63 68  efault page cach
1096a 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
1096b 20 28 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33   (the.** sqlite3
1096c 5f 70 63 61 63 68 65 20 69 6e 74 65 72 66 61 63  _pcache interfac
1096d 65 29 2e 20 49 74 20 61 6c 73 6f 20 63 6f 6e 74  e). It also cont
1096e 61 69 6e 73 20 70 61 72 74 20 6f 66 20 74 68 65  ains part of the
1096f 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a   implementation.
10970 2a 2a 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45  ** of the SQLITE
10971 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48  _CONFIG_PAGECACH
10972 45 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65  E and sqlite3_re
10973 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 66  lease_memory() f
10974 65 61 74 75 72 65 73 2e 0a 2a 2a 20 49 66 20 74  eatures..** If t
10975 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 20  he default page 
10976 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  cache implementa
10977 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64 65  tion is override
10978 6e 2c 20 74 68 65 6e 20 6e 65 69 74 68 65 72 20  n, then neither 
10979 6f 66 0a 2a 2a 20 74 68 65 73 65 20 74 77 6f 20  of.** these two 
1097a 66 65 61 74 75 72 65 73 20 61 72 65 20 61 76 61  features are ava
1097b 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 40 28  ilable..**.** @(
1097c 23 29 20 24 49 64 3a 20 70 63 61 63 68 65 31 2e  #) $Id: pcache1.
1097d 63 2c 76 20 31 2e 31 39 20 32 30 30 39 2f 30 37  c,v 1.19 2009/07
1097e 2f 31 37 20 31 31 3a 34 34 3a 30 37 20 64 72 68  /17 11:44:07 drh
1097f 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 74 79 70 65   Exp $.*/...type
10980 64 65 66 20 73 74 72 75 63 74 20 50 43 61 63 68  def struct PCach
10981 65 31 20 50 43 61 63 68 65 31 3b 0a 74 79 70 65  e1 PCache1;.type
10982 64 65 66 20 73 74 72 75 63 74 20 50 67 48 64 72  def struct PgHdr
10983 31 20 50 67 48 64 72 31 3b 0a 74 79 70 65 64 65  1 PgHdr1;.typede
10984 66 20 73 74 72 75 63 74 20 50 67 46 72 65 65 73  f struct PgFrees
10985 6c 6f 74 20 50 67 46 72 65 65 73 6c 6f 74 3b 0a  lot PgFreeslot;.
10986 0a 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20  ./* Pointers to 
10987 73 74 72 75 63 74 75 72 65 73 20 6f 66 20 74 68  structures of th
10988 69 73 20 74 79 70 65 20 61 72 65 20 63 61 73 74  is type are cast
10989 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20 61 73   and returned as
1098a 20 0a 2a 2a 20 6f 70 61 71 75 65 20 73 71 6c 69   .** opaque sqli
1098b 74 65 33 5f 70 63 61 63 68 65 2a 20 68 61 6e 64  te3_pcache* hand
1098c 6c 65 73 0a 2a 2f 0a 73 74 72 75 63 74 20 50 43  les.*/.struct PC
1098d 61 63 68 65 31 20 7b 0a 20 20 2f 2a 20 43 61 63  ache1 {.  /* Cac
1098e 68 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e  he configuration
1098f 20 70 61 72 61 6d 65 74 65 72 73 2e 20 50 61 67   parameters. Pag
10990 65 20 73 69 7a 65 20 28 73 7a 50 61 67 65 29 20  e size (szPage) 
10991 61 6e 64 20 74 68 65 20 70 75 72 67 65 61 62 6c  and the purgeabl
10992 65 0a 20 20 2a 2a 20 66 6c 61 67 20 28 62 50 75  e.  ** flag (bPu
10993 72 67 65 61 62 6c 65 29 20 61 72 65 20 73 65 74  rgeable) are set
10994 20 77 68 65 6e 20 74 68 65 20 63 61 63 68 65 20   when the cache 
10995 69 73 20 63 72 65 61 74 65 64 2e 20 6e 4d 61 78  is created. nMax
10996 20 6d 61 79 20 62 65 20 0a 20 20 2a 2a 20 6d 6f   may be .  ** mo
10997 64 69 66 69 65 64 20 61 74 20 61 6e 79 20 74 69  dified at any ti
10998 6d 65 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 20  me by a call to 
10999 74 68 65 20 70 63 61 63 68 65 31 43 61 63 68 65  the pcache1Cache
1099a 53 69 7a 65 28 29 20 6d 65 74 68 6f 64 2e 0a 20  Size() method.. 
1099b 20 2a 2a 20 54 68 65 20 67 6c 6f 62 61 6c 20 6d   ** The global m
1099c 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 65 6c  utex must be hel
1099d 64 20 77 68 65 6e 20 61 63 63 65 73 73 69 6e 67  d when accessing
1099e 20 6e 4d 61 78 2e 0a 20 20 2a 2f 0a 20 20 69 6e   nMax..  */.  in
1099f 74 20 73 7a 50 61 67 65 3b 20 20 20 20 20 20 20  t szPage;       
109a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
109a1 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 6c 6c    /* Size of all
109a2 6f 63 61 74 65 64 20 70 61 67 65 73 20 69 6e 20  ocated pages in 
109a3 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 62  bytes */.  int b
109a4 50 75 72 67 65 61 62 6c 65 3b 20 20 20 20 20 20  Purgeable;      
109a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
109a6 2a 20 54 72 75 65 20 69 66 20 63 61 63 68 65 20  * True if cache 
109a7 69 73 20 70 75 72 67 65 61 62 6c 65 20 2a 2f 0a  is purgeable */.
109a8 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e    unsigned int n
109a9 4d 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  Min;            
109aa 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d        /* Minimum
109ab 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
109ac 20 72 65 73 65 72 76 65 64 20 2a 2f 0a 20 20 75   reserved */.  u
109ad 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 4d 61 78  nsigned int nMax
109ae 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
109af 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 64     /* Configured
109b0 20 22 63 61 63 68 65 5f 73 69 7a 65 22 20 76 61   "cache_size" va
109b1 6c 75 65 20 2a 2f 0a 0a 20 20 2f 2a 20 48 61 73  lue */..  /* Has
109b2 68 20 74 61 62 6c 65 20 6f 66 20 61 6c 6c 20 70  h table of all p
109b3 61 67 65 73 2e 20 54 68 65 20 66 6f 6c 6c 6f 77  ages. The follow
109b4 69 6e 67 20 76 61 72 69 61 62 6c 65 73 20 6d 61  ing variables ma
109b5 79 20 6f 6e 6c 79 20 62 65 20 61 63 63 65 73 73  y only be access
109b6 65 64 0a 20 20 2a 2a 20 77 68 65 6e 20 74 68 65  ed.  ** when the
109b7 20 61 63 63 65 73 73 6f 72 20 69 73 20 68 6f 6c   accessor is hol
109b8 64 69 6e 67 20 74 68 65 20 67 6c 6f 62 61 6c 20  ding the global 
109b9 6d 75 74 65 78 20 28 73 65 65 20 70 63 61 63 68  mutex (see pcach
109ba 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 20 0a  e1EnterMutex() .
109bb 20 20 2a 2a 20 61 6e 64 20 70 63 61 63 68 65 31    ** and pcache1
109bc 4c 65 61 76 65 4d 75 74 65 78 28 29 29 2e 0a 20  LeaveMutex()).. 
109bd 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69   */.  unsigned i
109be 6e 74 20 6e 52 65 63 79 63 6c 61 62 6c 65 3b 20  nt nRecyclable; 
109bf 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
109c0 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
109c1 74 68 65 20 4c 52 55 20 6c 69 73 74 20 2a 2f 0a  the LRU list */.
109c2 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e    unsigned int n
109c3 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20  Page;           
109c4 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e        /* Total n
109c5 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
109c6 6e 20 61 70 48 61 73 68 20 2a 2f 0a 20 20 75 6e  n apHash */.  un
109c7 73 69 67 6e 65 64 20 69 6e 74 20 6e 48 61 73 68  signed int nHash
109c8 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
109c9 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73    /* Number of s
109ca 6c 6f 74 73 20 69 6e 20 61 70 48 61 73 68 5b 5d  lots in apHash[]
109cb 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 2a 2a 61   */.  PgHdr1 **a
109cc 70 48 61 73 68 3b 20 20 20 20 20 20 20 20 20 20  pHash;          
109cd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73            /* Has
109ce 68 20 74 61 62 6c 65 20 66 6f 72 20 66 61 73 74  h table for fast
109cf 20 6c 6f 6f 6b 75 70 20 62 79 20 6b 65 79 20 2a   lookup by key *
109d0 2f 0a 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  /..  unsigned in
109d1 74 20 69 4d 61 78 4b 65 79 3b 20 20 20 20 20 20  t iMaxKey;      
109d2 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 72 67           /* Larg
109d3 65 73 74 20 6b 65 79 20 73 65 65 6e 20 73 69 6e  est key seen sin
109d4 63 65 20 78 54 72 75 6e 63 61 74 65 28 29 20 2a  ce xTruncate() *
109d5 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68  /.};../*.** Each
109d6 20 63 61 63 68 65 20 65 6e 74 72 79 20 69 73 20   cache entry is 
109d7 72 65 70 72 65 73 65 6e 74 65 64 20 62 79 20 61  represented by a
109d8 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
109d9 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20  e following .** 
109da 73 74 72 75 63 74 75 72 65 2e 20 41 20 62 75 66  structure. A buf
109db 66 65 72 20 6f 66 20 50 67 48 64 72 31 2e 70 43  fer of PgHdr1.pC
109dc 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 62 79 74  ache->szPage byt
109dd 65 73 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20  es is allocated 
109de 0a 2a 2a 20 64 69 72 65 63 74 6c 79 20 62 65 66  .** directly bef
109df 6f 72 65 20 74 68 69 73 20 73 74 72 75 63 74 75  ore this structu
109e0 72 65 20 69 6e 20 6d 65 6d 6f 72 79 20 28 73 65  re in memory (se
109e1 65 20 74 68 65 20 50 47 48 44 52 31 5f 54 4f 5f  e the PGHDR1_TO_
109e2 50 41 47 45 28 29 20 0a 2a 2a 20 6d 61 63 72 6f  PAGE() .** macro
109e3 20 62 65 6c 6f 77 29 2e 0a 2a 2f 0a 73 74 72 75   below)..*/.stru
109e4 63 74 20 50 67 48 64 72 31 20 7b 0a 20 20 75 6e  ct PgHdr1 {.  un
109e5 73 69 67 6e 65 64 20 69 6e 74 20 69 4b 65 79 3b  signed int iKey;
109e6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
109e7 4b 65 79 20 76 61 6c 75 65 20 28 70 61 67 65 20  Key value (page 
109e8 6e 75 6d 62 65 72 29 20 2a 2f 0a 20 20 50 67 48  number) */.  PgH
109e9 64 72 31 20 2a 70 4e 65 78 74 3b 20 20 20 20 20  dr1 *pNext;     
109ea 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
109eb 65 78 74 20 69 6e 20 68 61 73 68 20 74 61 62 6c  ext in hash tabl
109ec 65 20 63 68 61 69 6e 20 2a 2f 0a 20 20 50 43 61  e chain */.  PCa
109ed 63 68 65 31 20 2a 70 43 61 63 68 65 3b 20 20 20  che1 *pCache;   
109ee 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
109ef 61 63 68 65 20 74 68 61 74 20 63 75 72 72 65 6e  ache that curren
109f0 74 6c 79 20 6f 77 6e 73 20 74 68 69 73 20 70 61  tly owns this pa
109f1 67 65 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 2a  ge */.  PgHdr1 *
109f2 70 4c 72 75 4e 65 78 74 3b 20 20 20 20 20 20 20  pLruNext;       
109f3 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 69         /* Next i
109f4 6e 20 4c 52 55 20 6c 69 73 74 20 6f 66 20 75 6e  n LRU list of un
109f5 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a 2f 0a  pinned pages */.
109f6 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75 50 72    PgHdr1 *pLruPr
109f7 65 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ev;             
109f8 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 69 6e 20   /* Previous in 
109f9 4c 52 55 20 6c 69 73 74 20 6f 66 20 75 6e 70 69  LRU list of unpi
109fa 6e 6e 65 64 20 70 61 67 65 73 20 2a 2f 0a 7d 3b  nned pages */.};
109fb 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 73 6c 6f  ../*.** Free slo
109fc 74 73 20 69 6e 20 74 68 65 20 61 6c 6c 6f 63 61  ts in the alloca
109fd 74 6f 72 20 75 73 65 64 20 74 6f 20 64 69 76 69  tor used to divi
109fe 64 65 20 75 70 20 74 68 65 20 62 75 66 66 65 72  de up the buffer
109ff 20 70 72 6f 76 69 64 65 64 20 75 73 69 6e 67 0a   provided using.
10a00 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f 43 4f  ** the SQLITE_CO
10a01 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 20 6d  NFIG_PAGECACHE m
10a02 65 63 68 61 6e 69 73 6d 2e 0a 2a 2f 0a 73 74 72  echanism..*/.str
10a03 75 63 74 20 50 67 46 72 65 65 73 6c 6f 74 20 7b  uct PgFreeslot {
10a04 0a 20 20 50 67 46 72 65 65 73 6c 6f 74 20 2a 70  .  PgFreeslot *p
10a05 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 66  Next;  /* Next f
10a06 72 65 65 20 73 6c 6f 74 20 2a 2f 0a 7d 3b 0a 0a  ree slot */.};..
10a07 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 6c 20 64 61 74  /*.** Global dat
10a08 61 20 75 73 65 64 20 62 79 20 74 68 69 73 20 63  a used by this c
10a09 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ache..*/.static 
10a0a 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63  SQLITE_WSD struc
10a0b 74 20 50 43 61 63 68 65 47 6c 6f 62 61 6c 20 7b  t PCacheGlobal {
10a0c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
10a0d 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 20 20 20   *mutex;        
10a0e 20 20 20 20 20 20 20 2f 2a 20 73 74 61 74 69 63         /* static
10a0f 20 6d 75 74 65 78 20 4d 55 54 45 58 5f 53 54 41   mutex MUTEX_STA
10a10 54 49 43 5f 4c 52 55 20 2a 2f 0a 0a 20 20 69 6e  TIC_LRU */..  in
10a11 74 20 6e 4d 61 78 50 61 67 65 3b 20 20 20 20 20  t nMaxPage;     
10a12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10a13 20 20 2f 2a 20 53 75 6d 20 6f 66 20 6e 4d 61 78    /* Sum of nMax
10a14 50 61 67 65 20 66 6f 72 20 70 75 72 67 65 61 62  Page for purgeab
10a15 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a 20 20 69  le caches */.  i
10a16 6e 74 20 6e 4d 69 6e 50 61 67 65 3b 20 20 20 20  nt nMinPage;    
10a17 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10a18 20 20 20 2f 2a 20 53 75 6d 20 6f 66 20 6e 4d 69     /* Sum of nMi
10a19 6e 50 61 67 65 20 66 6f 72 20 70 75 72 67 65 61  nPage for purgea
10a1a 62 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a 20 20  ble caches */.  
10a1b 69 6e 74 20 6e 43 75 72 72 65 6e 74 50 61 67 65  int nCurrentPage
10a1c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
10a1d 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
10a1e 20 70 75 72 67 65 61 62 6c 65 20 70 61 67 65 73   purgeable pages
10a1f 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20   allocated */.  
10a20 50 67 48 64 72 31 20 2a 70 4c 72 75 48 65 61 64  PgHdr1 *pLruHead
10a21 2c 20 2a 70 4c 72 75 54 61 69 6c 3b 20 20 20 20  , *pLruTail;    
10a22 20 20 20 20 2f 2a 20 4c 52 55 20 6c 69 73 74 20      /* LRU list 
10a23 6f 66 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65  of unpinned page
10a24 73 20 2a 2f 0a 0a 20 20 2f 2a 20 56 61 72 69 61  s */..  /* Varia
10a25 62 6c 65 73 20 72 65 6c 61 74 65 64 20 74 6f 20  bles related to 
10a26 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41  SQLITE_CONFIG_PA
10a27 47 45 43 41 43 48 45 20 73 65 74 74 69 6e 67 73  GECACHE settings
10a28 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 6c 6f  . */.  int szSlo
10a29 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
10a2a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
10a2b 7a 65 20 6f 66 20 65 61 63 68 20 66 72 65 65 20  ze of each free 
10a2c 73 6c 6f 74 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  slot */.  void *
10a2d 70 53 74 61 72 74 2c 20 2a 70 45 6e 64 3b 20 20  pStart, *pEnd;  
10a2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10a2f 20 42 6f 75 6e 64 73 20 6f 66 20 70 61 67 65 63   Bounds of pagec
10a30 61 63 68 65 20 6d 61 6c 6c 6f 63 20 72 61 6e 67  ache malloc rang
10a31 65 20 2a 2f 0a 20 20 50 67 46 72 65 65 73 6c 6f  e */.  PgFreeslo
10a32 74 20 2a 70 46 72 65 65 3b 20 20 20 20 20 20 20  t *pFree;       
10a33 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72             /* Fr
10a34 65 65 20 70 61 67 65 20 62 6c 6f 63 6b 73 20 2a  ee page blocks *
10a35 2f 0a 20 20 69 6e 74 20 69 73 49 6e 69 74 3b 20  /.  int isInit; 
10a36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10a37 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
10a38 69 66 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a  if initialized *
10a39 2f 0a 7d 20 70 63 61 63 68 65 31 5f 67 3b 0a 0a  /.} pcache1_g;..
10a3a 2f 2a 0a 2a 2a 20 41 6c 6c 20 63 6f 64 65 20 69  /*.** All code i
10a3b 6e 20 74 68 69 73 20 66 69 6c 65 20 73 68 6f 75  n this file shou
10a3c 6c 64 20 61 63 63 65 73 73 20 74 68 65 20 67 6c  ld access the gl
10a3d 6f 62 61 6c 20 73 74 72 75 63 74 75 72 65 20 61  obal structure a
10a3e 62 6f 76 65 20 76 69 61 20 74 68 65 0a 2a 2a 20  bove via the.** 
10a3f 61 6c 69 61 73 20 22 70 63 61 63 68 65 31 22 2e  alias "pcache1".
10a40 20 54 68 69 73 20 65 6e 73 75 72 65 73 20 74 68   This ensures th
10a41 61 74 20 74 68 65 20 57 53 44 20 65 6d 75 6c 61  at the WSD emula
10a42 74 69 6f 6e 20 69 73 20 75 73 65 64 20 77 68 65  tion is used whe
10a43 6e 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 20 66  n.** compiling f
10a44 6f 72 20 73 79 73 74 65 6d 73 20 74 68 61 74 20  or systems that 
10a45 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 72  do not support r
10a46 65 61 6c 20 57 53 44 2e 0a 2a 2f 0a 23 64 65 66  eal WSD..*/.#def
10a47 69 6e 65 20 70 63 61 63 68 65 31 20 28 47 4c 4f  ine pcache1 (GLO
10a48 42 41 4c 28 73 74 72 75 63 74 20 50 43 61 63 68  BAL(struct PCach
10a49 65 47 6c 6f 62 61 6c 2c 20 70 63 61 63 68 65 31  eGlobal, pcache1
10a4a 5f 67 29 29 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e  _g))../*.** When
10a4b 20 61 20 50 67 48 64 72 31 20 73 74 72 75 63 74   a PgHdr1 struct
10a4c 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64  ure is allocated
10a4d 2c 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64  , the associated
10a4e 20 50 43 61 63 68 65 31 2e 73 7a 50 61 67 65 0a   PCache1.szPage.
10a4f 2a 2a 20 62 79 74 65 73 20 6f 66 20 64 61 74 61  ** bytes of data
10a50 20 61 72 65 20 6c 6f 63 61 74 65 64 20 64 69 72   are located dir
10a51 65 63 74 6c 79 20 62 65 66 6f 72 65 20 69 74 20  ectly before it 
10a52 69 6e 20 6d 65 6d 6f 72 79 20 28 69 2e 65 2e 20  in memory (i.e. 
10a53 74 68 65 20 74 6f 74 61 6c 0a 2a 2a 20 73 69 7a  the total.** siz
10a54 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74  e of the allocat
10a55 69 6f 6e 20 69 73 20 73 69 7a 65 6f 66 28 50 67  ion is sizeof(Pg
10a56 48 64 72 31 29 2b 50 43 61 63 68 65 31 2e 73 7a  Hdr1)+PCache1.sz
10a57 50 61 67 65 20 62 79 74 65 29 2e 20 54 68 65 0a  Page byte). The.
10a58 2a 2a 20 50 47 48 44 52 31 5f 54 4f 5f 50 41 47  ** PGHDR1_TO_PAG
10a59 45 28 29 20 6d 61 63 72 6f 20 74 61 6b 65 73 20  E() macro takes 
10a5a 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 50  a pointer to a P
10a5b 67 48 64 72 31 20 73 74 72 75 63 74 75 72 65 20  gHdr1 structure 
10a5c 61 73 0a 2a 2a 20 61 6e 20 61 72 67 75 6d 65 6e  as.** an argumen
10a5d 74 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20  t and returns a 
10a5e 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61  pointer to the a
10a5f 73 73 6f 63 69 61 74 65 64 20 62 6c 6f 63 6b 20  ssociated block 
10a60 6f 66 20 73 7a 50 61 67 65 0a 2a 2a 20 62 79 74  of szPage.** byt
10a61 65 73 2e 20 54 68 65 20 50 41 47 45 5f 54 4f 5f  es. The PAGE_TO_
10a62 50 47 48 44 52 31 28 29 20 6d 61 63 72 6f 20 64  PGHDR1() macro d
10a63 6f 65 73 20 74 68 65 20 6f 70 70 6f 73 69 74 65  oes the opposite
10a64 3a 20 69 74 73 20 61 72 67 75 6d 65 6e 74 20 69  : its argument i
10a65 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74  s.** a pointer t
10a66 6f 20 61 20 62 6c 6f 63 6b 20 6f 66 20 73 7a 50  o a block of szP
10a67 61 67 65 20 62 79 74 65 73 20 6f 66 20 64 61 74  age bytes of dat
10a68 61 20 61 6e 64 20 74 68 65 20 72 65 74 75 72 6e  a and the return
10a69 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 61 20 70   value is.** a p
10a6a 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 73  ointer to the as
10a6b 73 6f 63 69 61 74 65 64 20 50 67 48 64 72 31 20  sociated PgHdr1 
10a6c 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a  structure..**.**
10a6d 20 20 20 61 73 73 65 72 74 28 20 50 47 48 44 52     assert( PGHDR
10a6e 31 5f 54 4f 5f 50 41 47 45 28 50 41 47 45 5f 54  1_TO_PAGE(PAGE_T
10a6f 4f 5f 50 47 48 44 52 31 28 70 43 61 63 68 65 2c  O_PGHDR1(pCache,
10a70 20 58 29 29 3d 3d 58 20 29 3b 0a 2a 2f 0a 23 64   X))==X );.*/.#d
10a71 65 66 69 6e 65 20 50 47 48 44 52 31 5f 54 4f 5f  efine PGHDR1_TO_
10a72 50 41 47 45 28 70 29 20 20 20 20 28 76 6f 69 64  PAGE(p)    (void
10a73 2a 29 28 28 28 63 68 61 72 2a 29 70 29 20 2d 20  *)(((char*)p) - 
10a74 70 2d 3e 70 43 61 63 68 65 2d 3e 73 7a 50 61 67  p->pCache->szPag
10a75 65 29 0a 23 64 65 66 69 6e 65 20 50 41 47 45 5f  e).#define PAGE_
10a76 54 4f 5f 50 47 48 44 52 31 28 63 2c 20 70 29 20  TO_PGHDR1(c, p) 
10a77 28 50 67 48 64 72 31 2a 29 28 28 28 63 68 61 72  (PgHdr1*)(((char
10a78 2a 29 70 29 20 2b 20 63 2d 3e 73 7a 50 61 67 65  *)p) + c->szPage
10a79 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20  )../*.** Macros 
10a7a 74 6f 20 65 6e 74 65 72 20 61 6e 64 20 6c 65 61  to enter and lea
10a7b 76 65 20 74 68 65 20 67 6c 6f 62 61 6c 20 4c 52  ve the global LR
10a7c 55 20 6d 75 74 65 78 2e 0a 2a 2f 0a 23 64 65 66  U mutex..*/.#def
10a7d 69 6e 65 20 70 63 61 63 68 65 31 45 6e 74 65 72  ine pcache1Enter
10a7e 4d 75 74 65 78 28 29 20 73 71 6c 69 74 65 33 5f  Mutex() sqlite3_
10a7f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 63 61 63  mutex_enter(pcac
10a80 68 65 31 2e 6d 75 74 65 78 29 0a 23 64 65 66 69  he1.mutex).#defi
10a81 6e 65 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d  ne pcache1LeaveM
10a82 75 74 65 78 28 29 20 73 71 6c 69 74 65 33 5f 6d  utex() sqlite3_m
10a83 75 74 65 78 5f 6c 65 61 76 65 28 70 63 61 63 68  utex_leave(pcach
10a84 65 31 2e 6d 75 74 65 78 29 0a 0a 2f 2a 2a 2a 2a  e1.mutex)../****
10a85 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a86 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a87 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a88 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a89 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
10a8a 2a 2a 2a 2a 2a 20 50 61 67 65 20 41 6c 6c 6f 63  ***** Page Alloc
10a8b 61 74 69 6f 6e 2f 53 51 4c 49 54 45 5f 43 4f 4e  ation/SQLITE_CON
10a8c 46 49 47 5f 50 43 41 43 48 45 20 52 65 6c 61 74  FIG_PCACHE Relat
10a8d 65 64 20 46 75 6e 63 74 69 6f 6e 73 20 2a 2a 2a  ed Functions ***
10a8e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a  ***********/../*
10a8f 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
10a90 6e 20 69 73 20 63 61 6c 6c 65 64 20 64 75 72 69  n is called duri
10a91 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  ng initializatio
10a92 6e 20 69 66 20 61 20 73 74 61 74 69 63 20 62 75  n if a static bu
10a93 66 66 65 72 20 69 73 20 0a 2a 2a 20 73 75 70 70  ffer is .** supp
10a94 6c 69 65 64 20 74 6f 20 75 73 65 20 66 6f 72 20  lied to use for 
10a95 74 68 65 20 70 61 67 65 2d 63 61 63 68 65 20 62  the page-cache b
10a96 79 20 70 61 73 73 69 6e 67 20 74 68 65 20 53 51  y passing the SQ
10a97 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45  LITE_CONFIG_PAGE
10a98 43 41 43 48 45 0a 2a 2a 20 76 65 72 62 20 74 6f  CACHE.** verb to
10a99 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28   sqlite3_config(
10a9a 29 2e 20 50 61 72 61 6d 65 74 65 72 20 70 42 75  ). Parameter pBu
10a9b 66 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61  f points to an a
10a9c 6c 6c 6f 63 61 74 69 6f 6e 20 6c 61 72 67 65 0a  llocation large.
10a9d 2a 2a 20 65 6e 6f 75 67 68 20 74 6f 20 63 6f 6e  ** enough to con
10a9e 74 61 69 6e 20 27 6e 27 20 62 75 66 66 65 72 73  tain 'n' buffers
10a9f 20 6f 66 20 27 73 7a 27 20 62 79 74 65 73 20 65   of 'sz' bytes e
10aa0 61 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ach..*/.SQLITE_P
10aa1 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
10aa2 74 65 33 50 43 61 63 68 65 42 75 66 66 65 72 53  te3PCacheBufferS
10aa3 65 74 75 70 28 76 6f 69 64 20 2a 70 42 75 66 2c  etup(void *pBuf,
10aa4 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20 6e 29 7b   int sz, int n){
10aa5 0a 20 20 69 66 28 20 70 63 61 63 68 65 31 2e 69  .  if( pcache1.i
10aa6 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 50 67 46  sInit ){.    PgF
10aa7 72 65 65 73 6c 6f 74 20 2a 70 3b 0a 20 20 20 20  reeslot *p;.    
10aa8 73 7a 20 3d 20 52 4f 55 4e 44 44 4f 57 4e 38 28  sz = ROUNDDOWN8(
10aa9 73 7a 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31  sz);.    pcache1
10aaa 2e 73 7a 53 6c 6f 74 20 3d 20 73 7a 3b 0a 20 20  .szSlot = sz;.  
10aab 20 20 70 63 61 63 68 65 31 2e 70 53 74 61 72 74    pcache1.pStart
10aac 20 3d 20 70 42 75 66 3b 0a 20 20 20 20 70 63 61   = pBuf;.    pca
10aad 63 68 65 31 2e 70 46 72 65 65 20 3d 20 30 3b 0a  che1.pFree = 0;.
10aae 20 20 20 20 77 68 69 6c 65 28 20 6e 2d 2d 20 29      while( n-- )
10aaf 7b 0a 20 20 20 20 20 20 70 20 3d 20 28 50 67 46  {.      p = (PgF
10ab0 72 65 65 73 6c 6f 74 2a 29 70 42 75 66 3b 0a 20  reeslot*)pBuf;. 
10ab1 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20       p->pNext = 
10ab2 70 63 61 63 68 65 31 2e 70 46 72 65 65 3b 0a 20  pcache1.pFree;. 
10ab3 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 46 72       pcache1.pFr
10ab4 65 65 20 3d 20 70 3b 0a 20 20 20 20 20 20 70 42  ee = p;.      pB
10ab5 75 66 20 3d 20 28 76 6f 69 64 2a 29 26 28 28 63  uf = (void*)&((c
10ab6 68 61 72 2a 29 70 42 75 66 29 5b 73 7a 5d 3b 0a  har*)pBuf)[sz];.
10ab7 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63 68 65      }.    pcache
10ab8 31 2e 70 45 6e 64 20 3d 20 70 42 75 66 3b 0a 20  1.pEnd = pBuf;. 
10ab9 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6c 6c   }.}../*.** Mall
10aba 6f 63 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 64  oc function used
10abb 20 77 69 74 68 69 6e 20 74 68 69 73 20 66 69 6c   within this fil
10abc 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 73 70  e to allocate sp
10abd 61 63 65 20 66 72 6f 6d 20 74 68 65 20 62 75 66  ace from the buf
10abe 66 65 72 0a 2a 2a 20 63 6f 6e 66 69 67 75 72 65  fer.** configure
10abf 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f  d using sqlite3_
10ac0 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f  config(SQLITE_CO
10ac1 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 29 20  NFIG_PAGECACHE) 
10ac2 6f 70 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 0a 2a  option. If no .*
10ac3 2a 20 73 75 63 68 20 62 75 66 66 65 72 20 65 78  * such buffer ex
10ac4 69 73 74 73 20 6f 72 20 74 68 65 72 65 20 69 73  ists or there is
10ac5 20 6e 6f 20 73 70 61 63 65 20 6c 65 66 74 20 69   no space left i
10ac6 6e 20 69 74 2c 20 74 68 69 73 20 66 75 6e 63 74  n it, this funct
10ac7 69 6f 6e 20 66 61 6c 6c 73 20 0a 2a 2a 20 62 61  ion falls .** ba
10ac8 63 6b 20 74 6f 20 73 71 6c 69 74 65 33 4d 61 6c  ck to sqlite3Mal
10ac9 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  loc()..*/.static
10aca 20 76 6f 69 64 20 2a 70 63 61 63 68 65 31 41 6c   void *pcache1Al
10acb 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a  loc(int nByte){.
10acc 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61 73 73    void *p;.  ass
10acd 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
10ace 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e  ex_held(pcache1.
10acf 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20  mutex) );.  if( 
10ad0 6e 42 79 74 65 3c 3d 70 63 61 63 68 65 31 2e 73  nByte<=pcache1.s
10ad1 7a 53 6c 6f 74 20 26 26 20 70 63 61 63 68 65 31  zSlot && pcache1
10ad2 2e 70 46 72 65 65 20 29 7b 0a 20 20 20 20 61 73  .pFree ){.    as
10ad3 73 65 72 74 28 20 70 63 61 63 68 65 31 2e 69 73  sert( pcache1.is
10ad4 49 6e 69 74 20 29 3b 0a 20 20 20 20 70 20 3d 20  Init );.    p = 
10ad5 28 50 67 48 64 72 31 20 2a 29 70 63 61 63 68 65  (PgHdr1 *)pcache
10ad6 31 2e 70 46 72 65 65 3b 0a 20 20 20 20 70 63 61  1.pFree;.    pca
10ad7 63 68 65 31 2e 70 46 72 65 65 20 3d 20 70 63 61  che1.pFree = pca
10ad8 63 68 65 31 2e 70 46 72 65 65 2d 3e 70 4e 65 78  che1.pFree->pNex
10ad9 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74  t;.    sqlite3St
10ada 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53  atusSet(SQLITE_S
10adb 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f  TATUS_PAGECACHE_
10adc 53 49 5a 45 2c 20 6e 42 79 74 65 29 3b 0a 20 20  SIZE, nByte);.  
10add 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41    sqlite3StatusA
10ade 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53  dd(SQLITE_STATUS
10adf 5f 50 41 47 45 43 41 43 48 45 5f 55 53 45 44 2c  _PAGECACHE_USED,
10ae0 20 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 0a 20   1);.  }else{.. 
10ae1 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61     /* Allocate a
10ae2 20 6e 65 77 20 62 75 66 66 65 72 20 75 73 69 6e   new buffer usin
10ae3 67 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 2e  g sqlite3Malloc.
10ae4 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 73 6f   Before doing so
10ae5 2c 20 65 78 69 74 20 74 68 65 0a 20 20 20 20 2a  , exit the.    *
10ae6 2a 20 67 6c 6f 62 61 6c 20 70 63 61 63 68 65 20  * global pcache 
10ae7 6d 75 74 65 78 20 61 6e 64 20 75 6e 6c 6f 63 6b  mutex and unlock
10ae8 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 65   the pager-cache
10ae9 20 6f 62 6a 65 63 74 20 70 43 61 63 68 65 2e 20   object pCache. 
10aea 54 68 69 73 20 69 73 20 0a 20 20 20 20 2a 2a 20  This is .    ** 
10aeb 73 6f 20 74 68 61 74 20 69 66 20 74 68 65 20 61  so that if the a
10aec 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61  ttempt to alloca
10aed 74 65 20 61 20 6e 65 77 20 62 75 66 66 65 72 20  te a new buffer 
10aee 63 61 75 73 65 73 20 74 68 65 20 74 68 65 20 0a  causes the the .
10aef 20 20 20 20 2a 2a 20 63 6f 6e 66 69 67 75 72 65      ** configure
10af0 64 20 73 6f 66 74 2d 68 65 61 70 2d 6c 69 6d 69  d soft-heap-limi
10af1 74 20 74 6f 20 62 65 20 62 72 65 61 63 68 65 64  t to be breached
10af2 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 70 6f 73  , it will be pos
10af3 73 69 62 6c 65 20 74 6f 0a 20 20 20 20 2a 2a 20  sible to.    ** 
10af4 72 65 63 6c 61 69 6d 20 6d 65 6d 6f 72 79 20 66  reclaim memory f
10af5 72 6f 6d 20 74 68 69 73 20 70 61 67 65 72 2d 63  rom this pager-c
10af6 61 63 68 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ache..    */.   
10af7 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74   pcache1LeaveMut
10af8 65 78 28 29 3b 0a 20 20 20 20 70 20 3d 20 73 71  ex();.    p = sq
10af9 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 42 79 74  lite3Malloc(nByt
10afa 65 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 45  e);.    pcache1E
10afb 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20  nterMutex();.   
10afc 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20   if( p ){.      
10afd 69 6e 74 20 73 7a 20 3d 20 73 71 6c 69 74 65 33  int sz = sqlite3
10afe 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20  MallocSize(p);. 
10aff 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74       sqlite3Stat
10b00 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41  usAdd(SQLITE_STA
10b01 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56  TUS_PAGECACHE_OV
10b02 45 52 46 4c 4f 57 2c 20 73 7a 29 3b 0a 20 20 20  ERFLOW, sz);.   
10b03 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
10b04 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65  p;.}../*.** Free
10b05 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 75   an allocated bu
10b06 66 66 65 72 20 6f 62 74 61 69 6e 65 64 20 66 72  ffer obtained fr
10b07 6f 6d 20 70 63 61 63 68 65 31 41 6c 6c 6f 63 28  om pcache1Alloc(
10b08 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  )..*/.static voi
10b09 64 20 70 63 61 63 68 65 31 46 72 65 65 28 76 6f  d pcache1Free(vo
10b0a 69 64 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  id *p){.  assert
10b0b 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
10b0c 68 65 6c 64 28 70 63 61 63 68 65 31 2e 6d 75 74  held(pcache1.mut
10b0d 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 3d 3d  ex) );.  if( p==
10b0e 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  0 ) return;.  if
10b0f 28 20 70 3e 3d 70 63 61 63 68 65 31 2e 70 53 74  ( p>=pcache1.pSt
10b10 61 72 74 20 26 26 20 70 3c 70 63 61 63 68 65 31  art && p<pcache1
10b11 2e 70 45 6e 64 20 29 7b 0a 20 20 20 20 50 67 46  .pEnd ){.    PgF
10b12 72 65 65 73 6c 6f 74 20 2a 70 53 6c 6f 74 3b 0a  reeslot *pSlot;.
10b13 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75      sqlite3Statu
10b14 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54  sAdd(SQLITE_STAT
10b15 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55 53 45  US_PAGECACHE_USE
10b16 44 2c 20 2d 31 29 3b 0a 20 20 20 20 70 53 6c 6f  D, -1);.    pSlo
10b17 74 20 3d 20 28 50 67 46 72 65 65 73 6c 6f 74 2a  t = (PgFreeslot*
10b18 29 70 3b 0a 20 20 20 20 70 53 6c 6f 74 2d 3e 70  )p;.    pSlot->p
10b19 4e 65 78 74 20 3d 20 70 63 61 63 68 65 31 2e 70  Next = pcache1.p
10b1a 46 72 65 65 3b 0a 20 20 20 20 70 63 61 63 68 65  Free;.    pcache
10b1b 31 2e 70 46 72 65 65 20 3d 20 70 53 6c 6f 74 3b  1.pFree = pSlot;
10b1c 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e  .  }else{.    in
10b1d 74 20 69 53 69 7a 65 20 3d 20 73 71 6c 69 74 65  t iSize = sqlite
10b1e 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a  3MallocSize(p);.
10b1f 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75      sqlite3Statu
10b20 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54  sAdd(SQLITE_STAT
10b21 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56 45  US_PAGECACHE_OVE
10b22 52 46 4c 4f 57 2c 20 2d 69 53 69 7a 65 29 3b 0a  RFLOW, -iSize);.
10b23 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
10b24 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  (p);.  }.}../*.*
10b25 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77  * Allocate a new
10b26 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69 6e 69   page object ini
10b27 74 69 61 6c 6c 79 20 61 73 73 6f 63 69 61 74 65  tially associate
10b28 64 20 77 69 74 68 20 63 61 63 68 65 20 70 43 61  d with cache pCa
10b29 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 50  che..*/.static P
10b2a 67 48 64 72 31 20 2a 70 63 61 63 68 65 31 41 6c  gHdr1 *pcache1Al
10b2b 6c 6f 63 50 61 67 65 28 50 43 61 63 68 65 31 20  locPage(PCache1 
10b2c 2a 70 43 61 63 68 65 29 7b 0a 20 20 69 6e 74 20  *pCache){.  int 
10b2d 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 50  nByte = sizeof(P
10b2e 67 48 64 72 31 29 20 2b 20 70 43 61 63 68 65 2d  gHdr1) + pCache-
10b2f 3e 73 7a 50 61 67 65 3b 0a 20 20 76 6f 69 64 20  >szPage;.  void 
10b30 2a 70 50 67 20 3d 20 70 63 61 63 68 65 31 41 6c  *pPg = pcache1Al
10b31 6c 6f 63 28 6e 42 79 74 65 29 3b 0a 20 20 50 67  loc(nByte);.  Pg
10b32 48 64 72 31 20 2a 70 3b 0a 20 20 69 66 28 20 70  Hdr1 *p;.  if( p
10b33 50 67 20 29 7b 0a 20 20 20 20 70 20 3d 20 50 41  Pg ){.    p = PA
10b34 47 45 5f 54 4f 5f 50 47 48 44 52 31 28 70 43 61  GE_TO_PGHDR1(pCa
10b35 63 68 65 2c 20 70 50 67 29 3b 0a 20 20 20 20 69  che, pPg);.    i
10b36 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67  f( pCache->bPurg
10b37 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 70  eable ){.      p
10b38 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 50  cache1.nCurrentP
10b39 61 67 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d  age++;.    }.  }
10b3a 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20 30 3b  else{.    p = 0;
10b3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b  .  }.  return p;
10b3c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61  .}../*.** Free a
10b3d 20 70 61 67 65 20 6f 62 6a 65 63 74 20 61 6c 6c   page object all
10b3e 6f 63 61 74 65 64 20 62 79 20 70 63 61 63 68 65  ocated by pcache
10b3f 31 41 6c 6c 6f 63 50 61 67 65 28 29 2e 0a 2a 2a  1AllocPage()..**
10b40 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20  .** The pointer 
10b41 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 65  is allowed to be
10b42 20 4e 55 4c 4c 2c 20 77 68 69 63 68 20 69 73 20   NULL, which is 
10b43 70 72 75 64 65 6e 74 2e 20 20 42 75 74 20 69 74  prudent.  But it
10b44 20 74 75 72 6e 73 20 6f 75 74 0a 2a 2a 20 74 68   turns out.** th
10b45 61 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 69  at the current i
10b46 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 68 61  mplementation ha
10b47 70 70 65 6e 73 20 74 6f 20 6e 65 76 65 72 20 63  ppens to never c
10b48 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  all this routine
10b49 0a 2a 2a 20 77 69 74 68 20 61 20 4e 55 4c 4c 20  .** with a NULL 
10b4a 70 6f 69 6e 74 65 72 2c 20 73 6f 20 77 65 20 6d  pointer, so we m
10b4b 61 72 6b 20 74 68 65 20 4e 55 4c 4c 20 74 65 73  ark the NULL tes
10b4c 74 20 77 69 74 68 20 41 4c 57 41 59 53 28 29 2e  t with ALWAYS().
10b4d 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
10b4e 70 63 61 63 68 65 31 46 72 65 65 50 61 67 65 28  pcache1FreePage(
10b4f 50 67 48 64 72 31 20 2a 70 29 7b 0a 20 20 69 66  PgHdr1 *p){.  if
10b50 28 20 41 4c 57 41 59 53 28 70 29 20 29 7b 0a 20  ( ALWAYS(p) ){. 
10b51 20 20 20 69 66 28 20 70 2d 3e 70 43 61 63 68 65     if( p->pCache
10b52 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 29 7b 0a  ->bPurgeable ){.
10b53 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 6e 43        pcache1.nC
10b54 75 72 72 65 6e 74 50 61 67 65 2d 2d 3b 0a 20 20  urrentPage--;.  
10b55 20 20 7d 0a 20 20 20 20 70 63 61 63 68 65 31 46    }.    pcache1F
10b56 72 65 65 28 50 47 48 44 52 31 5f 54 4f 5f 50 41  ree(PGHDR1_TO_PA
10b57 47 45 28 70 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  GE(p));.  }.}../
10b58 2a 0a 2a 2a 20 4d 61 6c 6c 6f 63 20 66 75 6e 63  *.** Malloc func
10b59 74 69 6f 6e 20 75 73 65 64 20 62 79 20 53 51 4c  tion used by SQL
10b5a 69 74 65 20 74 6f 20 6f 62 74 61 69 6e 20 73 70  ite to obtain sp
10b5b 61 63 65 20 66 72 6f 6d 20 74 68 65 20 62 75 66  ace from the buf
10b5c 66 65 72 20 63 6f 6e 66 69 67 75 72 65 64 0a 2a  fer configured.*
10b5d 2a 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f  * using sqlite3_
10b5e 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f  config(SQLITE_CO
10b5f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 29 20  NFIG_PAGECACHE) 
10b60 6f 70 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 73 75  option. If no su
10b61 63 68 20 62 75 66 66 65 72 0a 2a 2a 20 65 78 69  ch buffer.** exi
10b62 73 74 73 2c 20 74 68 69 73 20 66 75 6e 63 74 69  sts, this functi
10b63 6f 6e 20 66 61 6c 6c 73 20 62 61 63 6b 20 74 6f  on falls back to
10b64 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29   sqlite3Malloc()
10b65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
10b66 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
10b67 33 50 61 67 65 4d 61 6c 6c 6f 63 28 69 6e 74 20  3PageMalloc(int 
10b68 73 7a 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a  sz){.  void *p;.
10b69 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75    pcache1EnterMu
10b6a 74 65 78 28 29 3b 0a 20 20 70 20 3d 20 70 63 61  tex();.  p = pca
10b6b 63 68 65 31 41 6c 6c 6f 63 28 73 7a 29 3b 0a 20  che1Alloc(sz);. 
10b6c 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74   pcache1LeaveMut
10b6d 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  ex();.  return p
10b6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20  ;.}../*.** Free 
10b6f 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66  an allocated buf
10b70 66 65 72 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  fer obtained fro
10b71 6d 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c  m sqlite3PageMal
10b72 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  loc()..*/.SQLITE
10b73 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
10b74 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 76 6f  lite3PageFree(vo
10b75 69 64 20 2a 70 29 7b 0a 20 20 70 63 61 63 68 65  id *p){.  pcache
10b76 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20  1EnterMutex();. 
10b77 20 70 63 61 63 68 65 31 46 72 65 65 28 70 29 3b   pcache1Free(p);
10b78 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d  .  pcache1LeaveM
10b79 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  utex();.}../****
10b7a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10b7b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10b7c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10b7d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10b7e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
10b7f 2a 2a 2a 2a 2a 20 47 65 6e 65 72 61 6c 20 49 6d  ***** General Im
10b80 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 46 75 6e  plementation Fun
10b81 63 74 69 6f 6e 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  ctions *********
10b82 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10b83 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a  ***********/../*
10b84 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
10b85 6e 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 73  n is used to res
10b86 69 7a 65 20 74 68 65 20 68 61 73 68 20 74 61 62  ize the hash tab
10b87 6c 65 20 75 73 65 64 20 62 79 20 74 68 65 20 63  le used by the c
10b88 61 63 68 65 20 70 61 73 73 65 64 0a 2a 2a 20 61  ache passed.** a
10b89 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  s the first argu
10b8a 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ment..**.** The 
10b8b 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 6d 75 73  global mutex mus
10b8c 74 20 62 65 20 68 65 6c 64 20 77 68 65 6e 20 74  t be held when t
10b8d 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
10b8e 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  called..*/.stati
10b8f 63 20 69 6e 74 20 70 63 61 63 68 65 31 52 65 73  c int pcache1Res
10b90 69 7a 65 48 61 73 68 28 50 43 61 63 68 65 31 20  izeHash(PCache1 
10b91 2a 70 29 7b 0a 20 20 50 67 48 64 72 31 20 2a 2a  *p){.  PgHdr1 **
10b92 61 70 4e 65 77 3b 0a 20 20 75 6e 73 69 67 6e 65  apNew;.  unsigne
10b93 64 20 69 6e 74 20 6e 4e 65 77 3b 0a 20 20 75 6e  d int nNew;.  un
10b94 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a 0a 20  signed int i;.. 
10b95 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
10b96 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63  _mutex_held(pcac
10b97 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a 0a 20  he1.mutex) );.. 
10b98 20 6e 4e 65 77 20 3d 20 70 2d 3e 6e 48 61 73 68   nNew = p->nHash
10b99 2a 32 3b 0a 20 20 69 66 28 20 6e 4e 65 77 3c 32  *2;.  if( nNew<2
10b9a 35 36 20 29 7b 0a 20 20 20 20 6e 4e 65 77 20 3d  56 ){.    nNew =
10b9b 20 32 35 36 3b 0a 20 20 7d 0a 0a 20 20 70 63 61   256;.  }..  pca
10b9c 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29  che1LeaveMutex()
10b9d 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 48 61 73 68  ;.  if( p->nHash
10b9e 20 29 7b 20 73 71 6c 69 74 65 33 42 65 67 69 6e   ){ sqlite3Begin
10b9f 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 20  BenignMalloc(); 
10ba0 7d 0a 20 20 61 70 4e 65 77 20 3d 20 28 50 67 48  }.  apNew = (PgH
10ba1 64 72 31 20 2a 2a 29 73 71 6c 69 74 65 33 5f 6d  dr1 **)sqlite3_m
10ba2 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 50 67 48  alloc(sizeof(PgH
10ba3 64 72 31 20 2a 29 2a 6e 4e 65 77 29 3b 0a 20 20  dr1 *)*nNew);.  
10ba4 69 66 28 20 70 2d 3e 6e 48 61 73 68 20 29 7b 20  if( p->nHash ){ 
10ba5 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e  sqlite3EndBenign
10ba6 4d 61 6c 6c 6f 63 28 29 3b 20 7d 0a 20 20 70 63  Malloc(); }.  pc
10ba7 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28  ache1EnterMutex(
10ba8 29 3b 0a 20 20 69 66 28 20 61 70 4e 65 77 20 29  );.  if( apNew )
10ba9 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 61 70 4e  {.    memset(apN
10baa 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28 50 67  ew, 0, sizeof(Pg
10bab 48 64 72 31 20 2a 29 2a 6e 4e 65 77 29 3b 0a 20  Hdr1 *)*nNew);. 
10bac 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d     for(i=0; i<p-
10bad 3e 6e 48 61 73 68 3b 20 69 2b 2b 29 7b 0a 20 20  >nHash; i++){.  
10bae 20 20 20 20 50 67 48 64 72 31 20 2a 70 50 61 67      PgHdr1 *pPag
10baf 65 3b 0a 20 20 20 20 20 20 50 67 48 64 72 31 20  e;.      PgHdr1 
10bb0 2a 70 4e 65 78 74 20 3d 20 70 2d 3e 61 70 48 61  *pNext = p->apHa
10bb1 73 68 5b 69 5d 3b 0a 20 20 20 20 20 20 77 68 69  sh[i];.      whi
10bb2 6c 65 28 20 28 70 50 61 67 65 20 3d 20 70 4e 65  le( (pPage = pNe
10bb3 78 74 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  xt)!=0 ){.      
10bb4 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68    unsigned int h
10bb5 20 3d 20 70 50 61 67 65 2d 3e 69 4b 65 79 20 25   = pPage->iKey %
10bb6 20 6e 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 70   nNew;.        p
10bb7 4e 65 78 74 20 3d 20 70 50 61 67 65 2d 3e 70 4e  Next = pPage->pN
10bb8 65 78 74 3b 0a 20 20 20 20 20 20 20 20 70 50 61  ext;.        pPa
10bb9 67 65 2d 3e 70 4e 65 78 74 20 3d 20 61 70 4e 65  ge->pNext = apNe
10bba 77 5b 68 5d 3b 0a 20 20 20 20 20 20 20 20 61 70  w[h];.        ap
10bbb 4e 65 77 5b 68 5d 20 3d 20 70 50 61 67 65 3b 0a  New[h] = pPage;.
10bbc 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
10bbd 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
10bbe 2d 3e 61 70 48 61 73 68 29 3b 0a 20 20 20 20 70  ->apHash);.    p
10bbf 2d 3e 61 70 48 61 73 68 20 3d 20 61 70 4e 65 77  ->apHash = apNew
10bc0 3b 0a 20 20 20 20 70 2d 3e 6e 48 61 73 68 20 3d  ;.    p->nHash =
10bc1 20 6e 4e 65 77 3b 0a 20 20 7d 0a 0a 20 20 72 65   nNew;.  }..  re
10bc2 74 75 72 6e 20 28 70 2d 3e 61 70 48 61 73 68 20  turn (p->apHash 
10bc3 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51  ? SQLITE_OK : SQ
10bc4 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a  LITE_NOMEM);.}..
10bc5 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
10bc6 69 6f 6e 20 69 73 20 75 73 65 64 20 69 6e 74 65  ion is used inte
10bc7 72 6e 61 6c 6c 79 20 74 6f 20 72 65 6d 6f 76 65  rnally to remove
10bc8 20 74 68 65 20 70 61 67 65 20 70 50 61 67 65 20   the page pPage 
10bc9 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 67 6c 6f  from the .** glo
10bca 62 61 6c 20 4c 52 55 20 6c 69 73 74 2c 20 69 66  bal LRU list, if
10bcb 20 69 73 20 70 61 72 74 20 6f 66 20 69 74 2e 20   is part of it. 
10bcc 49 66 20 70 50 61 67 65 20 69 73 20 6e 6f 74 20  If pPage is not 
10bcd 70 61 72 74 20 6f 66 20 74 68 65 20 67 6c 6f 62  part of the glob
10bce 61 6c 0a 2a 2a 20 4c 52 55 20 6c 69 73 74 2c 20  al.** LRU list, 
10bcf 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  then this functi
10bd0 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  on is a no-op..*
10bd1 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61 6c 20  *.** The global 
10bd2 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 65  mutex must be he
10bd3 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e  ld when this fun
10bd4 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e  ction is called.
10bd5 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
10bd6 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 28 50  pcache1PinPage(P
10bd7 67 48 64 72 31 20 2a 70 50 61 67 65 29 7b 0a 20  gHdr1 *pPage){. 
10bd8 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
10bd9 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63  _mutex_held(pcac
10bda 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20  he1.mutex) );.  
10bdb 69 66 28 20 70 50 61 67 65 20 26 26 20 28 70 50  if( pPage && (pP
10bdc 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 7c 7c  age->pLruNext ||
10bdd 20 70 50 61 67 65 3d 3d 70 63 61 63 68 65 31 2e   pPage==pcache1.
10bde 70 4c 72 75 54 61 69 6c 29 20 29 7b 0a 20 20 20  pLruTail) ){.   
10bdf 20 69 66 28 20 70 50 61 67 65 2d 3e 70 4c 72 75   if( pPage->pLru
10be0 50 72 65 76 20 29 7b 0a 20 20 20 20 20 20 70 50  Prev ){.      pP
10be1 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 2d 3e 70  age->pLruPrev->p
10be2 4c 72 75 4e 65 78 74 20 3d 20 70 50 61 67 65 2d  LruNext = pPage-
10be3 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 20 20 20 7d  >pLruNext;.    }
10be4 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e  .    if( pPage->
10be5 70 4c 72 75 4e 65 78 74 20 29 7b 0a 20 20 20 20  pLruNext ){.    
10be6 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78    pPage->pLruNex
10be7 74 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 70 50  t->pLruPrev = pP
10be8 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 3b 0a 20  age->pLruPrev;. 
10be9 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 63 61     }.    if( pca
10bea 63 68 65 31 2e 70 4c 72 75 48 65 61 64 3d 3d 70  che1.pLruHead==p
10beb 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 70 63  Page ){.      pc
10bec 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 20 3d  ache1.pLruHead =
10bed 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74   pPage->pLruNext
10bee 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
10bef 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c  pcache1.pLruTail
10bf0 3d 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20 20  ==pPage ){.     
10bf1 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69   pcache1.pLruTai
10bf2 6c 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72 75 50  l = pPage->pLruP
10bf3 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  rev;.    }.    p
10bf4 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 3d  Page->pLruNext =
10bf5 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70   0;.    pPage->p
10bf6 4c 72 75 50 72 65 76 20 3d 20 30 3b 0a 20 20 20  LruPrev = 0;.   
10bf7 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 2d 3e   pPage->pCache->
10bf8 6e 52 65 63 79 63 6c 61 62 6c 65 2d 2d 3b 0a 20  nRecyclable--;. 
10bf9 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d   }.}.../*.** Rem
10bfa 6f 76 65 20 74 68 65 20 70 61 67 65 20 73 75 70  ove the page sup
10bfb 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72 67 75  plied as an argu
10bfc 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 68 61  ment from the ha
10bfd 73 68 20 74 61 62 6c 65 20 0a 2a 2a 20 28 50 43  sh table .** (PC
10bfe 61 63 68 65 31 2e 61 70 48 61 73 68 20 73 74 72  ache1.apHash str
10bff 75 63 74 75 72 65 29 20 74 68 61 74 20 69 74 20  ucture) that it 
10c00 69 73 20 63 75 72 72 65 6e 74 6c 79 20 73 74 6f  is currently sto
10c01 72 65 64 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  red in..**.** Th
10c02 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 6d  e global mutex m
10c03 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 65 6e  ust be held when
10c04 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
10c05 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61  s called..*/.sta
10c06 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31  tic void pcache1
10c07 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28 50  RemoveFromHash(P
10c08 67 48 64 72 31 20 2a 70 50 61 67 65 29 7b 0a 20  gHdr1 *pPage){. 
10c09 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 3b   unsigned int h;
10c0a 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63  .  PCache1 *pCac
10c0b 68 65 20 3d 20 70 50 61 67 65 2d 3e 70 43 61 63  he = pPage->pCac
10c0c 68 65 3b 0a 20 20 50 67 48 64 72 31 20 2a 2a 70  he;.  PgHdr1 **p
10c0d 70 3b 0a 0a 20 20 68 20 3d 20 70 50 61 67 65 2d  p;..  h = pPage-
10c0e 3e 69 4b 65 79 20 25 20 70 43 61 63 68 65 2d 3e  >iKey % pCache->
10c0f 6e 48 61 73 68 3b 0a 20 20 66 6f 72 28 70 70 3d  nHash;.  for(pp=
10c10 26 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b  &pCache->apHash[
10c11 68 5d 3b 20 28 2a 70 70 29 21 3d 70 50 61 67 65  h]; (*pp)!=pPage
10c12 3b 20 70 70 3d 26 28 2a 70 70 29 2d 3e 70 4e 65  ; pp=&(*pp)->pNe
10c13 78 74 29 3b 0a 20 20 2a 70 70 20 3d 20 28 2a 70  xt);.  *pp = (*p
10c14 70 29 2d 3e 70 4e 65 78 74 3b 0a 0a 20 20 70 43  p)->pNext;..  pC
10c15 61 63 68 65 2d 3e 6e 50 61 67 65 2d 2d 3b 0a 7d  ache->nPage--;.}
10c16 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65  ../*.** If there
10c17 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 6d   are currently m
10c18 6f 72 65 20 74 68 61 6e 20 70 63 61 63 68 65 2e  ore than pcache.
10c19 6e 4d 61 78 50 61 67 65 20 70 61 67 65 73 20 61  nMaxPage pages a
10c1a 6c 6c 6f 63 61 74 65 64 2c 20 74 72 79 0a 2a 2a  llocated, try.**
10c1b 20 74 6f 20 72 65 63 79 63 6c 65 20 70 61 67 65   to recycle page
10c1c 73 20 74 6f 20 72 65 64 75 63 65 20 74 68 65 20  s to reduce the 
10c1d 6e 75 6d 62 65 72 20 61 6c 6c 6f 63 61 74 65 64  number allocated
10c1e 20 74 6f 20 70 63 61 63 68 65 2e 6e 4d 61 78 50   to pcache.nMaxP
10c1f 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  age..*/.static v
10c20 6f 69 64 20 70 63 61 63 68 65 31 45 6e 66 6f 72  oid pcache1Enfor
10c21 63 65 4d 61 78 50 61 67 65 28 76 6f 69 64 29 7b  ceMaxPage(void){
10c22 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
10c23 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63  e3_mutex_held(pc
10c24 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a  ache1.mutex) );.
10c25 20 20 77 68 69 6c 65 28 20 70 63 61 63 68 65 31    while( pcache1
10c26 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 3e 70 63  .nCurrentPage>pc
10c27 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 20 26  ache1.nMaxPage &
10c28 26 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61  & pcache1.pLruTa
10c29 69 6c 20 29 7b 0a 20 20 20 20 50 67 48 64 72 31  il ){.    PgHdr1
10c2a 20 2a 70 20 3d 20 70 63 61 63 68 65 31 2e 70 4c   *p = pcache1.pL
10c2b 72 75 54 61 69 6c 3b 0a 20 20 20 20 70 63 61 63  ruTail;.    pcac
10c2c 68 65 31 50 69 6e 50 61 67 65 28 70 29 3b 0a 20  he1PinPage(p);. 
10c2d 20 20 20 70 63 61 63 68 65 31 52 65 6d 6f 76 65     pcache1Remove
10c2e 46 72 6f 6d 48 61 73 68 28 70 29 3b 0a 20 20 20  FromHash(p);.   
10c2f 20 70 63 61 63 68 65 31 46 72 65 65 50 61 67 65   pcache1FreePage
10c30 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  (p);.  }.}../*.*
10c31 2a 20 44 69 73 63 61 72 64 20 61 6c 6c 20 70 61  * Discard all pa
10c32 67 65 73 20 66 72 6f 6d 20 63 61 63 68 65 20 70  ges from cache p
10c33 43 61 63 68 65 20 77 69 74 68 20 61 20 70 61 67  Cache with a pag
10c34 65 20 6e 75 6d 62 65 72 20 28 6b 65 79 20 76 61  e number (key va
10c35 6c 75 65 29 20 0a 2a 2a 20 67 72 65 61 74 65 72  lue) .** greater
10c36 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
10c37 6f 20 69 4c 69 6d 69 74 2e 20 41 6e 79 20 70 69  o iLimit. Any pi
10c38 6e 6e 65 64 20 70 61 67 65 73 20 74 68 61 74 20  nned pages that 
10c39 6d 65 65 74 20 74 68 69 73 20 0a 2a 2a 20 63 72  meet this .** cr
10c3a 69 74 65 72 69 61 20 61 72 65 20 75 6e 70 69 6e  iteria are unpin
10c3b 6e 65 64 20 62 65 66 6f 72 65 20 74 68 65 79 20  ned before they 
10c3c 61 72 65 20 64 69 73 63 61 72 64 65 64 2e 0a 2a  are discarded..*
10c3d 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61 6c 20  *.** The global 
10c3e 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 65  mutex must be he
10c3f 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e  ld when this fun
10c40 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e  ction is called.
10c41 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
10c42 70 63 61 63 68 65 31 54 72 75 6e 63 61 74 65 55  pcache1TruncateU
10c43 6e 73 61 66 65 28 0a 20 20 50 43 61 63 68 65 31  nsafe(.  PCache1
10c44 20 2a 70 43 61 63 68 65 2c 20 0a 20 20 75 6e 73   *pCache, .  uns
10c45 69 67 6e 65 64 20 69 6e 74 20 69 4c 69 6d 69 74  igned int iLimit
10c46 20 0a 29 7b 0a 20 20 54 45 53 54 4f 4e 4c 59 28   .){.  TESTONLY(
10c47 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 50   unsigned int nP
10c48 61 67 65 20 3d 20 30 3b 20 29 20 20 20 20 20 20  age = 0; )      
10c49 2f 2a 20 55 73 65 64 20 74 6f 20 61 73 73 65 72  /* Used to asser
10c4a 74 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 20  t pCache->nPage 
10c4b 69 73 20 63 6f 72 72 65 63 74 20 2a 2f 0a 20 20  is correct */.  
10c4c 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 3b 0a  unsigned int h;.
10c4d 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
10c4e 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61  3_mutex_held(pca
10c4f 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20  che1.mutex) );. 
10c50 20 66 6f 72 28 68 3d 30 3b 20 68 3c 70 43 61 63   for(h=0; h<pCac
10c51 68 65 2d 3e 6e 48 61 73 68 3b 20 68 2b 2b 29 7b  he->nHash; h++){
10c52 0a 20 20 20 20 50 67 48 64 72 31 20 2a 2a 70 70  .    PgHdr1 **pp
10c53 20 3d 20 26 70 43 61 63 68 65 2d 3e 61 70 48 61   = &pCache->apHa
10c54 73 68 5b 68 5d 3b 20 0a 20 20 20 20 50 67 48 64  sh[h]; .    PgHd
10c55 72 31 20 2a 70 50 61 67 65 3b 0a 20 20 20 20 77  r1 *pPage;.    w
10c56 68 69 6c 65 28 20 28 70 50 61 67 65 20 3d 20 2a  hile( (pPage = *
10c57 70 70 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  pp)!=0 ){.      
10c58 69 66 28 20 70 50 61 67 65 2d 3e 69 4b 65 79 3e  if( pPage->iKey>
10c59 3d 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 20  =iLimit ){.     
10c5a 20 20 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65     pCache->nPage
10c5b 2d 2d 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 20  --;.        *pp 
10c5c 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 3b 0a  = pPage->pNext;.
10c5d 20 20 20 20 20 20 20 20 70 63 61 63 68 65 31 50          pcache1P
10c5e 69 6e 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20  inPage(pPage);. 
10c5f 20 20 20 20 20 20 20 70 63 61 63 68 65 31 46 72         pcache1Fr
10c60 65 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20  eePage(pPage);. 
10c61 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
10c62 20 20 20 20 70 70 20 3d 20 26 70 50 61 67 65 2d      pp = &pPage-
10c63 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20  >pNext;.        
10c64 54 45 53 54 4f 4e 4c 59 28 20 6e 50 61 67 65 2b  TESTONLY( nPage+
10c65 2b 3b 20 29 0a 20 20 20 20 20 20 7d 0a 20 20 20  +; ).      }.   
10c66 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28   }.  }.  assert(
10c67 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 3d 3d   pCache->nPage==
10c68 6e 50 61 67 65 20 29 3b 0a 7d 0a 0a 2f 2a 2a 2a  nPage );.}../***
10c69 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10c6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10c6b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10c6c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10c6d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
10c6e 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 70  ****** sqlite3_p
10c6f 63 61 63 68 65 20 4d 65 74 68 6f 64 73 20 2a 2a  cache Methods **
10c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10c71 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10c72 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f  ************/../
10c73 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
10c74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74  ion of the sqlit
10c75 65 33 5f 70 63 61 63 68 65 2e 78 49 6e 69 74 20  e3_pcache.xInit 
10c76 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69  method..*/.stati
10c77 63 20 69 6e 74 20 70 63 61 63 68 65 31 49 6e 69  c int pcache1Ini
10c78 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29  t(void *NotUsed)
10c79 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  {.  UNUSED_PARAM
10c7a 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20  ETER(NotUsed);. 
10c7b 20 61 73 73 65 72 74 28 20 70 63 61 63 68 65 31   assert( pcache1
10c7c 2e 69 73 49 6e 69 74 3d 3d 30 20 29 3b 0a 20 20  .isInit==0 );.  
10c7d 6d 65 6d 73 65 74 28 26 70 63 61 63 68 65 31 2c  memset(&pcache1,
10c7e 20 30 2c 20 73 69 7a 65 6f 66 28 70 63 61 63 68   0, sizeof(pcach
10c7f 65 31 29 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  e1));.  if( sqli
10c80 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
10c81 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 20  bCoreMutex ){.  
10c82 20 20 70 63 61 63 68 65 31 2e 6d 75 74 65 78 20    pcache1.mutex 
10c83 3d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  = sqlite3_mutex_
10c84 61 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  alloc(SQLITE_MUT
10c85 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 29 3b 0a  EX_STATIC_LRU);.
10c86 20 20 7d 0a 20 20 70 63 61 63 68 65 31 2e 69 73    }.  pcache1.is
10c87 49 6e 69 74 20 3d 20 31 3b 0a 20 20 72 65 74 75  Init = 1;.  retu
10c88 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
10c89 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
10c8a 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c  ation of the sql
10c8b 69 74 65 33 5f 70 63 61 63 68 65 2e 78 53 68 75  ite3_pcache.xShu
10c8c 74 64 6f 77 6e 20 6d 65 74 68 6f 64 2e 0a 2a 2a  tdown method..**
10c8d 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 73   Note that the s
10c8e 74 61 74 69 63 20 6d 75 74 65 78 20 61 6c 6c 6f  tatic mutex allo
10c8f 63 61 74 65 64 20 69 6e 20 78 49 6e 69 74 20 64  cated in xInit d
10c90 6f 65 73 20 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64  oes .** not need
10c91 20 74 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a 2f   to be freed..*/
10c92 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61  .static void pca
10c93 63 68 65 31 53 68 75 74 64 6f 77 6e 28 76 6f 69  che1Shutdown(voi
10c94 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55  d *NotUsed){.  U
10c95 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
10c96 4e 6f 74 55 73 65 64 29 3b 0a 20 20 61 73 73 65  NotUsed);.  asse
10c97 72 74 28 20 70 63 61 63 68 65 31 2e 69 73 49 6e  rt( pcache1.isIn
10c98 69 74 21 3d 30 20 29 3b 0a 20 20 6d 65 6d 73 65  it!=0 );.  memse
10c99 74 28 26 70 63 61 63 68 65 31 2c 20 30 2c 20 73  t(&pcache1, 0, s
10c9a 69 7a 65 6f 66 28 70 63 61 63 68 65 31 29 29 3b  izeof(pcache1));
10c9b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
10c9c 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
10c9d 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78  sqlite3_pcache.x
10c9e 43 72 65 61 74 65 20 6d 65 74 68 6f 64 2e 0a 2a  Create method..*
10c9f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20  *.** Allocate a 
10ca0 6e 65 77 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74  new cache..*/.st
10ca1 61 74 69 63 20 73 71 6c 69 74 65 33 5f 70 63 61  atic sqlite3_pca
10ca2 63 68 65 20 2a 70 63 61 63 68 65 31 43 72 65 61  che *pcache1Crea
10ca3 74 65 28 69 6e 74 20 73 7a 50 61 67 65 2c 20 69  te(int szPage, i
10ca4 6e 74 20 62 50 75 72 67 65 61 62 6c 65 29 7b 0a  nt bPurgeable){.
10ca5 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68    PCache1 *pCach
10ca6 65 3b 0a 0a 20 20 70 43 61 63 68 65 20 3d 20 28  e;..  pCache = (
10ca7 50 43 61 63 68 65 31 20 2a 29 73 71 6c 69 74 65  PCache1 *)sqlite
10ca8 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28  3_malloc(sizeof(
10ca9 50 43 61 63 68 65 31 29 29 3b 0a 20 20 69 66 28  PCache1));.  if(
10caa 20 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20 6d   pCache ){.    m
10cab 65 6d 73 65 74 28 70 43 61 63 68 65 2c 20 30 2c  emset(pCache, 0,
10cac 20 73 69 7a 65 6f 66 28 50 43 61 63 68 65 31 29   sizeof(PCache1)
10cad 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 73  );.    pCache->s
10cae 7a 50 61 67 65 20 3d 20 73 7a 50 61 67 65 3b 0a  zPage = szPage;.
10caf 20 20 20 20 70 43 61 63 68 65 2d 3e 62 50 75 72      pCache->bPur
10cb0 67 65 61 62 6c 65 20 3d 20 28 62 50 75 72 67 65  geable = (bPurge
10cb1 61 62 6c 65 20 3f 20 31 20 3a 20 30 29 3b 0a 20  able ? 1 : 0);. 
10cb2 20 20 20 69 66 28 20 62 50 75 72 67 65 61 62 6c     if( bPurgeabl
10cb3 65 20 29 7b 0a 20 20 20 20 20 20 70 43 61 63 68  e ){.      pCach
10cb4 65 2d 3e 6e 4d 69 6e 20 3d 20 31 30 3b 0a 20 20  e->nMin = 10;.  
10cb5 20 20 20 20 70 63 61 63 68 65 31 45 6e 74 65 72      pcache1Enter
10cb6 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 20 20 70  Mutex();.      p
10cb7 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67 65 20  cache1.nMinPage 
10cb8 2b 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 69 6e 3b  += pCache->nMin;
10cb9 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 4c 65  .      pcache1Le
10cba 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20  aveMutex();.    
10cbb 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28  }.  }.  return (
10cbc 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a  sqlite3_pcache *
10cbd 29 70 43 61 63 68 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  )pCache;.}../*.*
10cbe 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
10cbf 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   of the sqlite3_
10cc0 70 63 61 63 68 65 2e 78 43 61 63 68 65 73 69 7a  pcache.xCachesiz
10cc1 65 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a  e method. .**.**
10cc2 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 63   Configure the c
10cc3 61 63 68 65 5f 73 69 7a 65 20 6c 69 6d 69 74 20  ache_size limit 
10cc4 66 6f 72 20 61 20 63 61 63 68 65 2e 0a 2a 2f 0a  for a cache..*/.
10cc5 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63  static void pcac
10cc6 68 65 31 43 61 63 68 65 73 69 7a 65 28 73 71 6c  he1Cachesize(sql
10cc7 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 2c 20  ite3_pcache *p, 
10cc8 69 6e 74 20 6e 4d 61 78 29 7b 0a 20 20 50 43 61  int nMax){.  PCa
10cc9 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28  che1 *pCache = (
10cca 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 69  PCache1 *)p;.  i
10ccb 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67  f( pCache->bPurg
10ccc 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 70 63 61  eable ){.    pca
10ccd 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29  che1EnterMutex()
10cce 3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e 6e 4d  ;.    pcache1.nM
10ccf 61 78 50 61 67 65 20 2b 3d 20 28 6e 4d 61 78 20  axPage += (nMax 
10cd0 2d 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 29 3b  - pCache->nMax);
10cd1 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 4d 61  .    pCache->nMa
10cd2 78 20 3d 20 6e 4d 61 78 3b 0a 20 20 20 20 70 63  x = nMax;.    pc
10cd3 61 63 68 65 31 45 6e 66 6f 72 63 65 4d 61 78 50  ache1EnforceMaxP
10cd4 61 67 65 28 29 3b 0a 20 20 20 20 70 63 61 63 68  age();.    pcach
10cd5 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a  e1LeaveMutex();.
10cd6 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70    }.}../*.** Imp
10cd7 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
10cd8 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68  he sqlite3_pcach
10cd9 65 2e 78 50 61 67 65 63 6f 75 6e 74 20 6d 65 74  e.xPagecount met
10cda 68 6f 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20  hod. .*/.static 
10cdb 69 6e 74 20 70 63 61 63 68 65 31 50 61 67 65 63  int pcache1Pagec
10cdc 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 70 63 61  ount(sqlite3_pca
10cdd 63 68 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e  che *p){.  int n
10cde 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72  ;.  pcache1Enter
10cdf 4d 75 74 65 78 28 29 3b 0a 20 20 6e 20 3d 20 28  Mutex();.  n = (
10ce0 28 50 43 61 63 68 65 31 20 2a 29 70 29 2d 3e 6e  (PCache1 *)p)->n
10ce1 50 61 67 65 3b 0a 20 20 70 63 61 63 68 65 31 4c  Page;.  pcache1L
10ce2 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 72  eaveMutex();.  r
10ce3 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn n;.}../*.*
10ce4 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
10ce5 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   of the sqlite3_
10ce6 70 63 61 63 68 65 2e 78 46 65 74 63 68 20 6d 65  pcache.xFetch me
10ce7 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 46 65 74  thod. .**.** Fet
10ce8 63 68 20 61 20 70 61 67 65 20 62 79 20 6b 65 79  ch a page by key
10ce9 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 57 68   value..**.** Wh
10cea 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 20 6e  ether or not a n
10ceb 65 77 20 70 61 67 65 20 6d 61 79 20 62 65 20 61  ew page may be a
10cec 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 69 73  llocated by this
10ced 20 66 75 6e 63 74 69 6f 6e 20 64 65 70 65 6e 64   function depend
10cee 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 76 61 6c 75  s on.** the valu
10cef 65 20 6f 66 20 74 68 65 20 63 72 65 61 74 65 46  e of the createF
10cf0 6c 61 67 20 61 72 67 75 6d 65 6e 74 2e 20 20 30  lag argument.  0
10cf1 20 6d 65 61 6e 73 20 64 6f 20 6e 6f 74 20 61 6c   means do not al
10cf2 6c 6f 63 61 74 65 20 61 20 6e 65 77 0a 2a 2a 20  locate a new.** 
10cf3 70 61 67 65 2e 20 20 31 20 6d 65 61 6e 73 20 61  page.  1 means a
10cf4 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 61  llocate a new pa
10cf5 67 65 20 69 66 20 73 70 61 63 65 20 69 73 20 65  ge if space is e
10cf6 61 73 69 6c 79 20 61 76 61 69 6c 61 62 6c 65 2e  asily available.
10cf7 20 20 32 20 0a 2a 2a 20 6d 65 61 6e 73 20 74 6f    2 .** means to
10cf8 20 74 72 79 20 72 65 61 6c 6c 79 20 68 61 72 64   try really hard
10cf9 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e   to allocate a n
10cfa 65 77 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 46  ew page..**.** F
10cfb 6f 72 20 61 20 6e 6f 6e 2d 70 75 72 67 65 61 62  or a non-purgeab
10cfc 6c 65 20 63 61 63 68 65 20 28 61 20 63 61 63 68  le cache (a cach
10cfd 65 20 75 73 65 64 20 61 73 20 74 68 65 20 73 74  e used as the st
10cfe 6f 72 61 67 65 20 66 6f 72 20 61 6e 20 69 6e 2d  orage for an in-
10cff 6d 65 6d 6f 72 79 0a 2a 2a 20 64 61 74 61 62 61  memory.** databa
10d00 73 65 29 20 74 68 65 72 65 20 69 73 20 72 65 61  se) there is rea
10d01 6c 6c 79 20 6e 6f 20 64 69 66 66 65 72 65 6e 63  lly no differenc
10d02 65 20 62 65 74 77 65 65 6e 20 63 72 65 61 74 65  e between create
10d03 46 6c 61 67 20 31 20 61 6e 64 20 32 2e 20 20 53  Flag 1 and 2.  S
10d04 6f 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 69 6e 67  o.** the calling
10d05 20 66 75 6e 63 74 69 6f 6e 20 28 70 63 61 63 68   function (pcach
10d06 65 2e 63 29 20 77 69 6c 6c 20 6e 65 76 65 72 20  e.c) will never 
10d07 68 61 76 65 20 61 20 63 72 65 61 74 65 46 6c 61  have a createFla
10d08 67 20 6f 66 20 31 20 6f 6e 0a 2a 2a 20 61 20 6e  g of 1 on.** a n
10d09 6f 6e 2d 70 75 72 67 61 62 6c 65 20 63 61 63 68  on-purgable cach
10d0a 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61  e..**.** There a
10d0b 72 65 20 74 68 72 65 65 20 64 69 66 66 65 72 65  re three differe
10d0c 6e 74 20 61 70 70 72 6f 61 63 68 65 73 20 74 6f  nt approaches to
10d0d 20 6f 62 74 61 69 6e 69 6e 67 20 73 70 61 63 65   obtaining space
10d0e 20 66 6f 72 20 61 20 70 61 67 65 2c 0a 2a 2a 20   for a page,.** 
10d0f 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65  depending on the
10d10 20 76 61 6c 75 65 20 6f 66 20 70 61 72 61 6d 65   value of parame
10d11 74 65 72 20 63 72 65 61 74 65 46 6c 61 67 20 28  ter createFlag (
10d12 77 68 69 63 68 20 6d 61 79 20 62 65 20 30 2c 20  which may be 0, 
10d13 31 20 6f 72 20 32 29 2e 0a 2a 2a 0a 2a 2a 20 20  1 or 2)..**.**  
10d14 20 31 2e 20 52 65 67 61 72 64 6c 65 73 73 20 6f   1. Regardless o
10d15 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 63  f the value of c
10d16 72 65 61 74 65 46 6c 61 67 2c 20 74 68 65 20 63  reateFlag, the c
10d17 61 63 68 65 20 69 73 20 73 65 61 72 63 68 65 64  ache is searched
10d18 20 66 6f 72 20 61 20 0a 2a 2a 20 20 20 20 20 20   for a .**      
10d19 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65 71 75  copy of the requ
10d1a 65 73 74 65 64 20 70 61 67 65 2e 20 49 66 20 6f  ested page. If o
10d1b 6e 65 20 69 73 20 66 6f 75 6e 64 2c 20 69 74 20  ne is found, it 
10d1c 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
10d1d 2a 2a 20 20 20 32 2e 20 49 66 20 63 72 65 61 74  **   2. If creat
10d1e 65 46 6c 61 67 3d 3d 30 20 61 6e 64 20 74 68 65  eFlag==0 and the
10d1f 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72   page is not alr
10d20 65 61 64 79 20 69 6e 20 74 68 65 20 63 61 63 68  eady in the cach
10d21 65 2c 20 4e 55 4c 4c 20 69 73 0a 2a 2a 20 20 20  e, NULL is.**   
10d22 20 20 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a     returned..**.
10d23 2a 2a 20 20 20 33 2e 20 49 66 20 63 72 65 61 74  **   3. If creat
10d24 65 46 6c 61 67 20 69 73 20 31 2c 20 61 6e 64 20  eFlag is 1, and 
10d25 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20  the page is not 
10d26 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 63  already in the c
10d27 61 63 68 65 2c 0a 2a 2a 20 20 20 20 20 20 61 6e  ache,.**      an
10d28 64 20 69 66 20 65 69 74 68 65 72 20 6f 66 20 74  d if either of t
10d29 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65  he following are
10d2a 20 74 72 75 65 2c 20 72 65 74 75 72 6e 20 4e 55   true, return NU
10d2b 4c 4c 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  LL:.**.**       
10d2c 28 61 29 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  (a) the number o
10d2d 66 20 70 61 67 65 73 20 70 69 6e 6e 65 64 20 62  f pages pinned b
10d2e 79 20 74 68 65 20 63 61 63 68 65 20 69 73 20 67  y the cache is g
10d2f 72 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a 20 20  reater than.**  
10d30 20 20 20 20 20 20 20 20 20 50 43 61 63 68 65 31           PCache1
10d31 2e 6e 4d 61 78 2c 20 6f 72 0a 2a 2a 20 20 20 20  .nMax, or.**    
10d32 20 20 20 28 62 29 20 74 68 65 20 6e 75 6d 62 65     (b) the numbe
10d33 72 20 6f 66 20 70 61 67 65 73 20 70 69 6e 6e 65  r of pages pinne
10d34 64 20 62 79 20 74 68 65 20 63 61 63 68 65 20 69  d by the cache i
10d35 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 0a 2a  s greater than.*
10d36 2a 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20  *           the 
10d37 73 75 6d 20 6f 66 20 6e 4d 61 78 20 66 6f 72 20  sum of nMax for 
10d38 61 6c 6c 20 70 75 72 67 65 61 62 6c 65 20 63 61  all purgeable ca
10d39 63 68 65 73 2c 20 6c 65 73 73 20 74 68 65 20 73  ches, less the s
10d3a 75 6d 20 6f 66 20 0a 2a 2a 20 20 20 20 20 20 20  um of .**       
10d3b 20 20 20 20 6e 4d 69 6e 20 66 6f 72 20 61 6c 6c      nMin for all
10d3c 20 6f 74 68 65 72 20 70 75 72 67 65 61 62 6c 65   other purgeable
10d3d 20 63 61 63 68 65 73 2e 20 0a 2a 2a 0a 2a 2a 20   caches. .**.** 
10d3e 20 20 34 2e 20 49 66 20 6e 6f 6e 65 20 6f 66 20    4. If none of 
10d3f 74 68 65 20 66 69 72 73 74 20 74 68 72 65 65 20  the first three 
10d40 63 6f 6e 64 69 74 69 6f 6e 73 20 61 70 70 6c 79  conditions apply
10d41 20 61 6e 64 20 74 68 65 20 63 61 63 68 65 20 69   and the cache i
10d42 73 20 6d 61 72 6b 65 64 0a 2a 2a 20 20 20 20 20  s marked.**     
10d43 20 61 73 20 70 75 72 67 65 61 62 6c 65 2c 20 61   as purgeable, a
10d44 6e 64 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65  nd if one of the
10d45 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74 72   following is tr
10d46 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  ue:.**.**       
10d47 28 61 29 20 54 68 65 20 6e 75 6d 62 65 72 20 6f  (a) The number o
10d48 66 20 70 61 67 65 73 20 61 6c 6c 6f 63 61 74 65  f pages allocate
10d49 64 20 66 6f 72 20 74 68 65 20 63 61 63 68 65 20  d for the cache 
10d4a 69 73 20 61 6c 72 65 61 64 79 20 0a 2a 2a 20 20  is already .**  
10d4b 20 20 20 20 20 20 20 20 20 50 43 61 63 68 65 31           PCache1
10d4c 2e 6e 4d 61 78 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20  .nMax, or.**.** 
10d4d 20 20 20 20 20 20 28 62 29 20 54 68 65 20 6e 75        (b) The nu
10d4e 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 61 6c  mber of pages al
10d4f 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 6c 6c 20  located for all 
10d50 70 75 72 67 65 61 62 6c 65 20 63 61 63 68 65 73  purgeable caches
10d51 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   is.**          
10d52 20 61 6c 72 65 61 64 79 20 65 71 75 61 6c 20 74   already equal t
10d53 6f 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 61  o or greater tha
10d54 6e 20 74 68 65 20 73 75 6d 20 6f 66 20 6e 4d 61  n the sum of nMa
10d55 78 20 66 6f 72 20 61 6c 6c 0a 2a 2a 20 20 20 20  x for all.**    
10d56 20 20 20 20 20 20 20 70 75 72 67 65 61 62 6c 65         purgeable
10d57 20 63 61 63 68 65 73 2c 0a 2a 2a 0a 2a 2a 20 20   caches,.**.**  
10d58 20 20 20 20 74 68 65 6e 20 61 74 74 65 6d 70 74      then attempt
10d59 20 74 6f 20 72 65 63 79 63 6c 65 20 61 20 70 61   to recycle a pa
10d5a 67 65 20 66 72 6f 6d 20 74 68 65 20 4c 52 55 20  ge from the LRU 
10d5b 6c 69 73 74 2e 20 49 66 20 69 74 20 69 73 20 74  list. If it is t
10d5c 68 65 20 72 69 67 68 74 0a 2a 2a 20 20 20 20 20  he right.**     
10d5d 20 73 69 7a 65 2c 20 72 65 74 75 72 6e 20 74 68   size, return th
10d5e 65 20 72 65 63 79 63 6c 65 64 20 62 75 66 66 65  e recycled buffe
10d5f 72 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 66 72  r. Otherwise, fr
10d60 65 65 20 74 68 65 20 62 75 66 66 65 72 20 61 6e  ee the buffer an
10d61 64 0a 2a 2a 20 20 20 20 20 20 70 72 6f 63 65 65  d.**      procee
10d62 64 20 74 6f 20 73 74 65 70 20 35 2e 20 0a 2a 2a  d to step 5. .**
10d63 0a 2a 2a 20 20 20 35 2e 20 4f 74 68 65 72 77 69  .**   5. Otherwi
10d64 73 65 2c 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64  se, allocate and
10d65 20 72 65 74 75 72 6e 20 61 20 6e 65 77 20 70 61   return a new pa
10d66 67 65 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 73 74  ge buffer..*/.st
10d67 61 74 69 63 20 76 6f 69 64 20 2a 70 63 61 63 68  atic void *pcach
10d68 65 31 46 65 74 63 68 28 73 71 6c 69 74 65 33 5f  e1Fetch(sqlite3_
10d69 70 63 61 63 68 65 20 2a 70 2c 20 75 6e 73 69 67  pcache *p, unsig
10d6a 6e 65 64 20 69 6e 74 20 69 4b 65 79 2c 20 69 6e  ned int iKey, in
10d6b 74 20 63 72 65 61 74 65 46 6c 61 67 29 7b 0a 20  t createFlag){. 
10d6c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 50   unsigned int nP
10d6d 69 6e 6e 65 64 3b 0a 20 20 50 43 61 63 68 65 31  inned;.  PCache1
10d6e 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 63   *pCache = (PCac
10d6f 68 65 31 20 2a 29 70 3b 0a 20 20 50 67 48 64 72  he1 *)p;.  PgHdr
10d70 31 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 0a 20  1 *pPage = 0;.. 
10d71 20 61 73 73 65 72 74 28 20 70 43 61 63 68 65 2d   assert( pCache-
10d72 3e 62 50 75 72 67 65 61 62 6c 65 20 7c 7c 20 63  >bPurgeable || c
10d73 72 65 61 74 65 46 6c 61 67 21 3d 31 20 29 3b 0a  reateFlag!=1 );.
10d74 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75    pcache1EnterMu
10d75 74 65 78 28 29 3b 0a 20 20 69 66 28 20 63 72 65  tex();.  if( cre
10d76 61 74 65 46 6c 61 67 3d 3d 31 20 29 20 73 71 6c  ateFlag==1 ) sql
10d77 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d  ite3BeginBenignM
10d78 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20 2f 2a 20 53  alloc();..  /* S
10d79 65 61 72 63 68 20 74 68 65 20 68 61 73 68 20 74  earch the hash t
10d7a 61 62 6c 65 20 66 6f 72 20 61 6e 20 65 78 69 73  able for an exis
10d7b 74 69 6e 67 20 65 6e 74 72 79 2e 20 2a 2f 0a 20  ting entry. */. 
10d7c 20 69 66 28 20 70 43 61 63 68 65 2d 3e 6e 48 61   if( pCache->nHa
10d7d 73 68 3e 30 20 29 7b 0a 20 20 20 20 75 6e 73 69  sh>0 ){.    unsi
10d7e 67 6e 65 64 20 69 6e 74 20 68 20 3d 20 69 4b 65  gned int h = iKe
10d7f 79 20 25 20 70 43 61 63 68 65 2d 3e 6e 48 61 73  y % pCache->nHas
10d80 68 3b 0a 20 20 20 20 66 6f 72 28 70 50 61 67 65  h;.    for(pPage
10d81 3d 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b  =pCache->apHash[
10d82 68 5d 3b 20 70 50 61 67 65 26 26 70 50 61 67 65  h]; pPage&&pPage
10d83 2d 3e 69 4b 65 79 21 3d 69 4b 65 79 3b 20 70 50  ->iKey!=iKey; pP
10d84 61 67 65 3d 70 50 61 67 65 2d 3e 70 4e 65 78 74  age=pPage->pNext
10d85 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50  );.  }..  if( pP
10d86 61 67 65 20 7c 7c 20 63 72 65 61 74 65 46 6c 61  age || createFla
10d87 67 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63 61 63  g==0 ){.    pcac
10d88 68 65 31 50 69 6e 50 61 67 65 28 70 50 61 67 65  he1PinPage(pPage
10d89 29 3b 0a 20 20 20 20 67 6f 74 6f 20 66 65 74 63  );.    goto fetc
10d8a 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  h_out;.  }..  /*
10d8b 20 53 74 65 70 20 33 20 6f 66 20 68 65 61 64 65   Step 3 of heade
10d8c 72 20 63 6f 6d 6d 65 6e 74 2e 20 2a 2f 0a 20 20  r comment. */.  
10d8d 6e 50 69 6e 6e 65 64 20 3d 20 70 43 61 63 68 65  nPinned = pCache
10d8e 2d 3e 6e 50 61 67 65 20 2d 20 70 43 61 63 68 65  ->nPage - pCache
10d8f 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 3b 0a 20  ->nRecyclable;. 
10d90 20 69 66 28 20 63 72 65 61 74 65 46 6c 61 67 3d   if( createFlag=
10d91 3d 31 20 26 26 20 28 0a 20 20 20 20 20 20 20 20  =1 && (.        
10d92 6e 50 69 6e 6e 65 64 3e 3d 28 70 63 61 63 68 65  nPinned>=(pcache
10d93 31 2e 6e 4d 61 78 50 61 67 65 2b 70 43 61 63 68  1.nMaxPage+pCach
10d94 65 2d 3e 6e 4d 69 6e 2d 70 63 61 63 68 65 31 2e  e->nMin-pcache1.
10d95 6e 4d 69 6e 50 61 67 65 29 0a 20 20 20 20 20 7c  nMinPage).     |
10d96 7c 20 6e 50 69 6e 6e 65 64 3e 3d 28 70 43 61 63  | nPinned>=(pCac
10d97 68 65 2d 3e 6e 4d 61 78 20 2a 20 39 20 2f 20 31  he->nMax * 9 / 1
10d98 30 29 0a 20 20 29 29 7b 0a 20 20 20 20 67 6f 74  0).  )){.    got
10d99 6f 20 66 65 74 63 68 5f 6f 75 74 3b 0a 20 20 7d  o fetch_out;.  }
10d9a 0a 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e  ..  if( pCache->
10d9b 6e 50 61 67 65 3e 3d 70 43 61 63 68 65 2d 3e 6e  nPage>=pCache->n
10d9c 48 61 73 68 20 26 26 20 70 63 61 63 68 65 31 52  Hash && pcache1R
10d9d 65 73 69 7a 65 48 61 73 68 28 70 43 61 63 68 65  esizeHash(pCache
10d9e 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66 65  ) ){.    goto fe
10d9f 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20  tch_out;.  }..  
10da0 2f 2a 20 53 74 65 70 20 34 2e 20 54 72 79 20 74  /* Step 4. Try t
10da1 6f 20 72 65 63 79 63 6c 65 20 61 20 70 61 67 65  o recycle a page
10da2 20 62 75 66 66 65 72 20 69 66 20 61 70 70 72 6f   buffer if appro
10da3 70 72 69 61 74 65 2e 20 2a 2f 0a 20 20 69 66 28  priate. */.  if(
10da4 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61   pCache->bPurgea
10da5 62 6c 65 20 26 26 20 70 63 61 63 68 65 31 2e 70  ble && pcache1.p
10da6 4c 72 75 54 61 69 6c 20 26 26 20 28 0a 20 20 20  LruTail && (.   
10da7 20 20 28 70 43 61 63 68 65 2d 3e 6e 50 61 67 65    (pCache->nPage
10da8 2b 31 3e 3d 70 43 61 63 68 65 2d 3e 6e 4d 61 78  +1>=pCache->nMax
10da9 29 20 7c 7c 20 70 63 61 63 68 65 31 2e 6e 43 75  ) || pcache1.nCu
10daa 72 72 65 6e 74 50 61 67 65 3e 3d 70 63 61 63 68  rrentPage>=pcach
10dab 65 31 2e 6e 4d 61 78 50 61 67 65 0a 20 20 29 29  e1.nMaxPage.  ))
10dac 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 63  {.    pPage = pc
10dad 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 3b 0a  ache1.pLruTail;.
10dae 20 20 20 20 70 63 61 63 68 65 31 52 65 6d 6f 76      pcache1Remov
10daf 65 46 72 6f 6d 48 61 73 68 28 70 50 61 67 65 29  eFromHash(pPage)
10db0 3b 0a 20 20 20 20 70 63 61 63 68 65 31 50 69 6e  ;.    pcache1Pin
10db1 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20  Page(pPage);.   
10db2 20 69 66 28 20 70 50 61 67 65 2d 3e 70 43 61 63   if( pPage->pCac
10db3 68 65 2d 3e 73 7a 50 61 67 65 21 3d 70 43 61 63  he->szPage!=pCac
10db4 68 65 2d 3e 73 7a 50 61 67 65 20 29 7b 0a 20 20  he->szPage ){.  
10db5 20 20 20 20 70 63 61 63 68 65 31 46 72 65 65 50      pcache1FreeP
10db6 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20  age(pPage);.    
10db7 20 20 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 20    pPage = 0;.   
10db8 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 63   }else{.      pc
10db9 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 50 61  ache1.nCurrentPa
10dba 67 65 20 2d 3d 20 28 70 50 61 67 65 2d 3e 70 43  ge -= (pPage->pC
10dbb 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65  ache->bPurgeable
10dbc 20 2d 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67   - pCache->bPurg
10dbd 65 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20  eable);.    }.  
10dbe 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 35 2e 20  }..  /* Step 5. 
10dbf 49 66 20 61 20 75 73 61 62 6c 65 20 70 61 67 65  If a usable page
10dc0 20 62 75 66 66 65 72 20 68 61 73 20 73 74 69 6c   buffer has stil
10dc1 6c 20 6e 6f 74 20 62 65 65 6e 20 66 6f 75 6e 64  l not been found
10dc2 2c 20 0a 20 20 2a 2a 20 61 74 74 65 6d 70 74 20  , .  ** attempt 
10dc3 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65  to allocate a ne
10dc4 77 20 6f 6e 65 2e 20 0a 20 20 2a 2f 0a 20 20 69  w one. .  */.  i
10dc5 66 28 20 21 70 50 61 67 65 20 29 7b 0a 20 20 20  f( !pPage ){.   
10dc6 20 70 50 61 67 65 20 3d 20 70 63 61 63 68 65 31   pPage = pcache1
10dc7 41 6c 6c 6f 63 50 61 67 65 28 70 43 61 63 68 65  AllocPage(pCache
10dc8 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50  );.  }..  if( pP
10dc9 61 67 65 20 29 7b 0a 20 20 20 20 75 6e 73 69 67  age ){.    unsig
10dca 6e 65 64 20 69 6e 74 20 68 20 3d 20 69 4b 65 79  ned int h = iKey
10dcb 20 25 20 70 43 61 63 68 65 2d 3e 6e 48 61 73 68   % pCache->nHash
10dcc 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 50  ;.    pCache->nP
10dcd 61 67 65 2b 2b 3b 0a 20 20 20 20 70 50 61 67 65  age++;.    pPage
10dce 2d 3e 69 4b 65 79 20 3d 20 69 4b 65 79 3b 0a 20  ->iKey = iKey;. 
10dcf 20 20 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 20     pPage->pNext 
10dd0 3d 20 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68  = pCache->apHash
10dd1 5b 68 5d 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e  [h];.    pPage->
10dd2 70 43 61 63 68 65 20 3d 20 70 43 61 63 68 65 3b  pCache = pCache;
10dd3 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 75  .    pPage->pLru
10dd4 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 70 50  Prev = 0;.    pP
10dd5 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 3d 20  age->pLruNext = 
10dd6 30 3b 0a 20 20 20 20 2a 28 76 6f 69 64 20 2a 2a  0;.    *(void **
10dd7 29 28 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 45  )(PGHDR1_TO_PAGE
10dd8 28 70 50 61 67 65 29 29 20 3d 20 30 3b 0a 20 20  (pPage)) = 0;.  
10dd9 20 20 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68    pCache->apHash
10dda 5b 68 5d 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d  [h] = pPage;.  }
10ddb 0a 0a 66 65 74 63 68 5f 6f 75 74 3a 0a 20 20 69  ..fetch_out:.  i
10ddc 66 28 20 70 50 61 67 65 20 26 26 20 69 4b 65 79  f( pPage && iKey
10ddd 3e 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79  >pCache->iMaxKey
10dde 20 29 7b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e   ){.    pCache->
10ddf 69 4d 61 78 4b 65 79 20 3d 20 69 4b 65 79 3b 0a  iMaxKey = iKey;.
10de0 20 20 7d 0a 20 20 69 66 28 20 63 72 65 61 74 65    }.  if( create
10de1 46 6c 61 67 3d 3d 31 20 29 20 73 71 6c 69 74 65  Flag==1 ) sqlite
10de2 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  3EndBenignMalloc
10de3 28 29 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61  ();.  pcache1Lea
10de4 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 72 65 74  veMutex();.  ret
10de5 75 72 6e 20 28 70 50 61 67 65 20 3f 20 50 47 48  urn (pPage ? PGH
10de6 44 52 31 5f 54 4f 5f 50 41 47 45 28 70 50 61 67  DR1_TO_PAGE(pPag
10de7 65 29 20 3a 20 30 29 3b 0a 7d 0a 0a 0a 2f 2a 0a  e) : 0);.}.../*.
10de8 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
10de9 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  n of the sqlite3
10dea 5f 70 63 61 63 68 65 2e 78 55 6e 70 69 6e 20 6d  _pcache.xUnpin m
10deb 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 4d 61 72  ethod..**.** Mar
10dec 6b 20 61 20 70 61 67 65 20 61 73 20 75 6e 70 69  k a page as unpi
10ded 6e 6e 65 64 20 28 65 6c 69 67 69 62 6c 65 20 66  nned (eligible f
10dee 6f 72 20 61 73 79 6e 63 68 72 6f 6e 6f 75 73 20  or asynchronous 
10def 72 65 63 79 63 6c 69 6e 67 29 2e 0a 2a 2f 0a 73  recycling)..*/.s
10df0 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68  tatic void pcach
10df1 65 31 55 6e 70 69 6e 28 73 71 6c 69 74 65 33 5f  e1Unpin(sqlite3_
10df2 70 63 61 63 68 65 20 2a 70 2c 20 76 6f 69 64 20  pcache *p, void 
10df3 2a 70 50 67 2c 20 69 6e 74 20 72 65 75 73 65 55  *pPg, int reuseU
10df4 6e 6c 69 6b 65 6c 79 29 7b 0a 20 20 50 43 61 63  nlikely){.  PCac
10df5 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50  he1 *pCache = (P
10df6 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 50 67  Cache1 *)p;.  Pg
10df7 48 64 72 31 20 2a 70 50 61 67 65 20 3d 20 50 41  Hdr1 *pPage = PA
10df8 47 45 5f 54 4f 5f 50 47 48 44 52 31 28 70 43 61  GE_TO_PGHDR1(pCa
10df9 63 68 65 2c 20 70 50 67 29 3b 0a 20 0a 20 20 61  che, pPg);. .  a
10dfa 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 43  ssert( pPage->pC
10dfb 61 63 68 65 3d 3d 70 43 61 63 68 65 20 29 3b 0a  ache==pCache );.
10dfc 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75    pcache1EnterMu
10dfd 74 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 49 74 20  tex();..  /* It 
10dfe 69 73 20 61 6e 20 65 72 72 6f 72 20 74 6f 20 63  is an error to c
10dff 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  all this functio
10e00 6e 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73  n if the page is
10e01 20 61 6c 72 65 61 64 79 20 0a 20 20 2a 2a 20 70   already .  ** p
10e02 61 72 74 20 6f 66 20 74 68 65 20 67 6c 6f 62 61  art of the globa
10e03 6c 20 4c 52 55 20 6c 69 73 74 2e 0a 20 20 2a 2f  l LRU list..  */
10e04 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
10e05 2d 3e 70 4c 72 75 50 72 65 76 3d 3d 30 20 26 26  ->pLruPrev==0 &&
10e06 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74   pPage->pLruNext
10e07 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
10e08 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61   pcache1.pLruHea
10e09 64 21 3d 70 50 61 67 65 20 26 26 20 70 63 61 63  d!=pPage && pcac
10e0a 68 65 31 2e 70 4c 72 75 54 61 69 6c 21 3d 70 50  he1.pLruTail!=pP
10e0b 61 67 65 20 29 3b 0a 0a 20 20 69 66 28 20 72 65  age );..  if( re
10e0c 75 73 65 55 6e 6c 69 6b 65 6c 79 20 7c 7c 20 70  useUnlikely || p
10e0d 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 50  cache1.nCurrentP
10e0e 61 67 65 3e 70 63 61 63 68 65 31 2e 6e 4d 61 78  age>pcache1.nMax
10e0f 50 61 67 65 20 29 7b 0a 20 20 20 20 70 63 61 63  Page ){.    pcac
10e10 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73  he1RemoveFromHas
10e11 68 28 70 50 61 67 65 29 3b 0a 20 20 20 20 70 63  h(pPage);.    pc
10e12 61 63 68 65 31 46 72 65 65 50 61 67 65 28 70 50  ache1FreePage(pP
10e13 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  age);.  }else{. 
10e14 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 70 61     /* Add the pa
10e15 67 65 20 74 6f 20 74 68 65 20 67 6c 6f 62 61 6c  ge to the global
10e16 20 4c 52 55 20 6c 69 73 74 2e 20 4e 6f 72 6d 61   LRU list. Norma
10e17 6c 6c 79 2c 20 74 68 65 20 70 61 67 65 20 69 73  lly, the page is
10e18 20 61 64 64 65 64 20 74 6f 0a 20 20 20 20 2a 2a   added to.    **
10e19 20 74 68 65 20 68 65 61 64 20 6f 66 20 74 68 65   the head of the
10e1a 20 6c 69 73 74 20 28 6c 61 73 74 20 70 61 67 65   list (last page
10e1b 20 74 6f 20 62 65 20 72 65 63 79 63 6c 65 64 29   to be recycled)
10e1c 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 20 74 68  . However, if th
10e1d 65 20 0a 20 20 20 20 2a 2a 20 72 65 75 73 65 55  e .    ** reuseU
10e1e 6e 6c 69 6b 65 6c 79 20 66 6c 61 67 20 70 61 73  nlikely flag pas
10e1f 73 65 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63  sed to this func
10e20 74 69 6f 6e 20 69 73 20 74 72 75 65 2c 20 74 68  tion is true, th
10e21 65 20 70 61 67 65 20 69 73 20 61 64 64 65 64 0a  e page is added.
10e22 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 74 61      ** to the ta
10e23 69 6c 20 6f 66 20 74 68 65 20 6c 69 73 74 20 28  il of the list (
10e24 66 69 72 73 74 20 70 61 67 65 20 74 6f 20 62 65  first page to be
10e25 20 72 65 63 79 63 6c 65 64 29 2e 0a 20 20 20 20   recycled)..    
10e26 2a 2f 0a 20 20 20 20 69 66 28 20 70 63 61 63 68  */.    if( pcach
10e27 65 31 2e 70 4c 72 75 48 65 61 64 20 29 7b 0a 20  e1.pLruHead ){. 
10e28 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72       pcache1.pLr
10e29 75 48 65 61 64 2d 3e 70 4c 72 75 50 72 65 76 20  uHead->pLruPrev 
10e2a 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 20 20 70  = pPage;.      p
10e2b 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 3d  Page->pLruNext =
10e2c 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61   pcache1.pLruHea
10e2d 64 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31  d;.      pcache1
10e2e 2e 70 4c 72 75 48 65 61 64 20 3d 20 70 50 61 67  .pLruHead = pPag
10e2f 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  e;.    }else{.  
10e30 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72 75      pcache1.pLru
10e31 54 61 69 6c 20 3d 20 70 50 61 67 65 3b 0a 20 20  Tail = pPage;.  
10e32 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72 75      pcache1.pLru
10e33 48 65 61 64 20 3d 20 70 50 61 67 65 3b 0a 20 20  Head = pPage;.  
10e34 20 20 7d 0a 20 20 20 20 70 43 61 63 68 65 2d 3e    }.    pCache->
10e35 6e 52 65 63 79 63 6c 61 62 6c 65 2b 2b 3b 0a 20  nRecyclable++;. 
10e36 20 7d 0a 0a 20 20 70 63 61 63 68 65 31 4c 65 61   }..  pcache1Lea
10e37 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a  veMutex();.}../*
10e38 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
10e39 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  on of the sqlite
10e3a 33 5f 70 63 61 63 68 65 2e 78 52 65 6b 65 79 20  3_pcache.xRekey 
10e3b 6d 65 74 68 6f 64 2e 20 0a 2a 2f 0a 73 74 61 74  method. .*/.stat
10e3c 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 52  ic void pcache1R
10e3d 65 6b 65 79 28 0a 20 20 73 71 6c 69 74 65 33 5f  ekey(.  sqlite3_
10e3e 70 63 61 63 68 65 20 2a 70 2c 0a 20 20 76 6f 69  pcache *p,.  voi
10e3f 64 20 2a 70 50 67 2c 0a 20 20 75 6e 73 69 67 6e  d *pPg,.  unsign
10e40 65 64 20 69 6e 74 20 69 4f 6c 64 2c 0a 20 20 75  ed int iOld,.  u
10e41 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4e 65 77  nsigned int iNew
10e42 0a 29 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70  .){.  PCache1 *p
10e43 43 61 63 68 65 20 3d 20 28 50 43 61 63 68 65 31  Cache = (PCache1
10e44 20 2a 29 70 3b 0a 20 20 50 67 48 64 72 31 20 2a   *)p;.  PgHdr1 *
10e45 70 50 61 67 65 20 3d 20 50 41 47 45 5f 54 4f 5f  pPage = PAGE_TO_
10e46 50 47 48 44 52 31 28 70 43 61 63 68 65 2c 20 70  PGHDR1(pCache, p
10e47 50 67 29 3b 0a 20 20 50 67 48 64 72 31 20 2a 2a  Pg);.  PgHdr1 **
10e48 70 70 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69  pp;.  unsigned i
10e49 6e 74 20 68 3b 20 0a 20 20 61 73 73 65 72 74 28  nt h; .  assert(
10e4a 20 70 50 61 67 65 2d 3e 69 4b 65 79 3d 3d 69 4f   pPage->iKey==iO
10e4b 6c 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ld );.  assert( 
10e4c 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3d 3d 70  pPage->pCache==p
10e4d 43 61 63 68 65 20 29 3b 0a 0a 20 20 70 63 61 63  Cache );..  pcac
10e4e 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b  he1EnterMutex();
10e4f 0a 0a 20 20 68 20 3d 20 69 4f 6c 64 25 70 43 61  ..  h = iOld%pCa
10e50 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20 70 70  che->nHash;.  pp
10e51 20 3d 20 26 70 43 61 63 68 65 2d 3e 61 70 48 61   = &pCache->apHa
10e52 73 68 5b 68 5d 3b 0a 20 20 77 68 69 6c 65 28 20  sh[h];.  while( 
10e53 28 2a 70 70 29 21 3d 70 50 61 67 65 20 29 7b 0a  (*pp)!=pPage ){.
10e54 20 20 20 20 70 70 20 3d 20 26 28 2a 70 70 29 2d      pp = &(*pp)-
10e55 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 2a 70  >pNext;.  }.  *p
10e56 70 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78 74  p = pPage->pNext
10e57 3b 0a 0a 20 20 68 20 3d 20 69 4e 65 77 25 70 43  ;..  h = iNew%pC
10e58 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20 70  ache->nHash;.  p
10e59 50 61 67 65 2d 3e 69 4b 65 79 20 3d 20 69 4e 65  Page->iKey = iNe
10e5a 77 3b 0a 20 20 70 50 61 67 65 2d 3e 70 4e 65 78  w;.  pPage->pNex
10e5b 74 20 3d 20 70 43 61 63 68 65 2d 3e 61 70 48 61  t = pCache->apHa
10e5c 73 68 5b 68 5d 3b 0a 20 20 70 43 61 63 68 65 2d  sh[h];.  pCache-
10e5d 3e 61 70 48 61 73 68 5b 68 5d 20 3d 20 70 50 61  >apHash[h] = pPa
10e5e 67 65 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 78 52  ge;..  /* The xR
10e5f 65 6b 65 79 28 29 20 69 6e 74 65 72 66 61 63 65  ekey() interface
10e60 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 74 6f   is only used to
10e61 20 6d 6f 76 65 20 70 61 67 65 73 20 65 61 72 6c   move pages earl
10e62 69 65 72 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20  ier in the.  ** 
10e63 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 69  database file (i
10e64 6e 20 6f 72 64 65 72 20 74 6f 20 6d 6f 76 65 20  n order to move 
10e65 61 6c 6c 20 66 72 65 65 20 70 61 67 65 73 20 74  all free pages t
10e66 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  o the end of the
10e67 0a 20 20 2a 2a 20 66 69 6c 65 20 77 68 65 72 65  .  ** file where
10e68 20 74 68 65 79 20 63 61 6e 20 62 65 20 74 72 75   they can be tru
10e69 6e 63 61 74 65 64 20 6f 66 66 2e 29 20 20 48 65  ncated off.)  He
10e6a 6e 63 65 2c 20 69 74 20 69 73 20 6e 6f 74 20 70  nce, it is not p
10e6b 6f 73 73 69 62 6c 65 0a 20 20 2a 2a 20 66 6f 72  ossible.  ** for
10e6c 20 74 68 65 20 6e 65 77 20 70 61 67 65 20 6e 75   the new page nu
10e6d 6d 62 65 72 20 74 6f 20 62 65 20 67 72 65 61 74  mber to be great
10e6e 65 72 20 74 68 61 6e 20 74 68 65 20 6c 61 72 67  er than the larg
10e6f 65 73 74 20 70 72 65 76 69 6f 75 73 6c 79 0a 20  est previously. 
10e70 20 2a 2a 20 66 65 74 63 68 65 64 20 70 61 67 65   ** fetched page
10e71 2e 20 20 42 75 74 20 77 65 20 72 65 74 61 69 6e  .  But we retain
10e72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74   the following t
10e73 65 73 74 20 69 6e 20 63 61 73 65 20 78 52 65 6b  est in case xRek
10e74 65 79 28 29 0a 20 20 2a 2a 20 62 65 67 69 6e 73  ey().  ** begins
10e75 20 74 6f 20 62 65 20 75 73 65 64 20 69 6e 20 64   to be used in d
10e76 69 66 66 65 72 65 6e 74 20 77 61 79 73 20 69 6e  ifferent ways in
10e77 20 74 68 65 20 66 75 74 75 72 65 2e 0a 20 20 2a   the future..  *
10e78 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 69 4e  /.  if( NEVER(iN
10e79 65 77 3e 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b  ew>pCache->iMaxK
10e7a 65 79 29 20 29 7b 0a 20 20 20 20 70 43 61 63 68  ey) ){.    pCach
10e7b 65 2d 3e 69 4d 61 78 4b 65 79 20 3d 20 69 4e 65  e->iMaxKey = iNe
10e7c 77 3b 0a 20 20 7d 0a 0a 20 20 70 63 61 63 68 65  w;.  }..  pcache
10e7d 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d  1LeaveMutex();.}
10e7e 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
10e7f 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71  tation of the sq
10e80 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 54 72  lite3_pcache.xTr
10e81 75 6e 63 61 74 65 20 6d 65 74 68 6f 64 2e 20 0a  uncate method. .
10e82 2a 2a 0a 2a 2a 20 44 69 73 63 61 72 64 20 61 6c  **.** Discard al
10e83 6c 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73  l unpinned pages
10e84 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 77 69   in the cache wi
10e85 74 68 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72  th a page number
10e86 20 65 71 75 61 6c 20 74 6f 0a 2a 2a 20 6f 72 20   equal to.** or 
10e87 67 72 65 61 74 65 72 20 74 68 61 6e 20 70 61 72  greater than par
10e88 61 6d 65 74 65 72 20 69 4c 69 6d 69 74 2e 20 41  ameter iLimit. A
10e89 6e 79 20 70 69 6e 6e 65 64 20 70 61 67 65 73 20  ny pinned pages 
10e8a 77 69 74 68 20 61 20 70 61 67 65 20 6e 75 6d 62  with a page numb
10e8b 65 72 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 20 6f  er.** equal to o
10e8c 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 69  r greater than i
10e8d 4c 69 6d 69 74 20 61 72 65 20 69 6d 70 6c 69 63  Limit are implic
10e8e 69 74 6c 79 20 75 6e 70 69 6e 6e 65 64 2e 0a 2a  itly unpinned..*
10e8f 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63  /.static void pc
10e90 61 63 68 65 31 54 72 75 6e 63 61 74 65 28 73 71  ache1Truncate(sq
10e91 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 2c  lite3_pcache *p,
10e92 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4c   unsigned int iL
10e93 69 6d 69 74 29 7b 0a 20 20 50 43 61 63 68 65 31  imit){.  PCache1
10e94 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 63   *pCache = (PCac
10e95 68 65 31 20 2a 29 70 3b 0a 20 20 70 63 61 63 68  he1 *)p;.  pcach
10e96 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a  e1EnterMutex();.
10e97 20 20 69 66 28 20 69 4c 69 6d 69 74 3c 3d 70 43    if( iLimit<=pC
10e98 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 29 7b  ache->iMaxKey ){
10e99 0a 20 20 20 20 70 63 61 63 68 65 31 54 72 75 6e  .    pcache1Trun
10e9a 63 61 74 65 55 6e 73 61 66 65 28 70 43 61 63 68  cateUnsafe(pCach
10e9b 65 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20  e, iLimit);.    
10e9c 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20  pCache->iMaxKey 
10e9d 3d 20 69 4c 69 6d 69 74 2d 31 3b 0a 20 20 7d 0a  = iLimit-1;.  }.
10e9e 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75    pcache1LeaveMu
10e9f 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  tex();.}../*.** 
10ea0 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
10ea1 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63  f the sqlite3_pc
10ea2 61 63 68 65 2e 78 44 65 73 74 72 6f 79 20 6d 65  ache.xDestroy me
10ea3 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 44 65 73  thod. .**.** Des
10ea4 74 72 6f 79 20 61 20 63 61 63 68 65 20 61 6c 6c  troy a cache all
10ea5 6f 63 61 74 65 64 20 75 73 69 6e 67 20 70 63 61  ocated using pca
10ea6 63 68 65 31 43 72 65 61 74 65 28 29 2e 0a 2a 2f  che1Create()..*/
10ea7 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61  .static void pca
10ea8 63 68 65 31 44 65 73 74 72 6f 79 28 73 71 6c 69  che1Destroy(sqli
10ea9 74 65 33 5f 70 63 61 63 68 65 20 2a 70 29 7b 0a  te3_pcache *p){.
10eaa 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68    PCache1 *pCach
10eab 65 20 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70  e = (PCache1 *)p
10eac 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72  ;.  pcache1Enter
10ead 4d 75 74 65 78 28 29 3b 0a 20 20 70 63 61 63 68  Mutex();.  pcach
10eae 65 31 54 72 75 6e 63 61 74 65 55 6e 73 61 66 65  e1TruncateUnsafe
10eaf 28 70 43 61 63 68 65 2c 20 30 29 3b 0a 20 20 70  (pCache, 0);.  p
10eb0 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 20  cache1.nMaxPage 
10eb1 2d 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 3b  -= pCache->nMax;
10eb2 0a 20 20 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50  .  pcache1.nMinP
10eb3 61 67 65 20 2d 3d 20 70 43 61 63 68 65 2d 3e 6e  age -= pCache->n
10eb4 4d 69 6e 3b 0a 20 20 70 63 61 63 68 65 31 45 6e  Min;.  pcache1En
10eb5 66 6f 72 63 65 4d 61 78 50 61 67 65 28 29 3b 0a  forceMaxPage();.
10eb6 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75    pcache1LeaveMu
10eb7 74 65 78 28 29 3b 0a 20 20 73 71 6c 69 74 65 33  tex();.  sqlite3
10eb8 5f 66 72 65 65 28 70 43 61 63 68 65 2d 3e 61 70  _free(pCache->ap
10eb9 48 61 73 68 29 3b 0a 20 20 73 71 6c 69 74 65 33  Hash);.  sqlite3
10eba 5f 66 72 65 65 28 70 43 61 63 68 65 29 3b 0a 7d  _free(pCache);.}
10ebb 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
10ebc 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
10ebd 64 75 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a  during initializ
10ebe 61 74 69 6f 6e 20 28 73 71 6c 69 74 65 33 5f 69  ation (sqlite3_i
10ebf 6e 69 74 69 61 6c 69 7a 65 28 29 29 20 74 6f 0a  nitialize()) to.
10ec0 2a 2a 20 69 6e 73 74 61 6c 6c 20 74 68 65 20 64  ** install the d
10ec1 65 66 61 75 6c 74 20 70 6c 75 67 67 61 62 6c 65  efault pluggable
10ec2 20 63 61 63 68 65 20 6d 6f 64 75 6c 65 2c 20 61   cache module, a
10ec3 73 73 75 6d 69 6e 67 20 74 68 65 20 75 73 65 72  ssuming the user
10ec4 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65   has not.** alre
10ec5 61 64 79 20 70 72 6f 76 69 64 65 64 20 61 6e 20  ady provided an 
10ec6 61 6c 74 65 72 6e 61 74 69 76 65 2e 0a 2a 2f 0a  alternative..*/.
10ec7 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
10ec8 6f 69 64 20 73 71 6c 69 74 65 33 50 43 61 63 68  oid sqlite3PCach
10ec9 65 53 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64  eSetDefault(void
10eca 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69  ){.  static sqli
10ecb 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f  te3_pcache_metho
10ecc 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68 6f 64  ds defaultMethod
10ecd 73 20 3d 20 7b 0a 20 20 20 20 30 2c 20 20 20 20  s = {.    0,    
10ece 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10ecf 20 20 20 2f 2a 20 70 41 72 67 20 2a 2f 0a 20 20     /* pArg */.  
10ed0 20 20 70 63 61 63 68 65 31 49 6e 69 74 2c 20 20    pcache1Init,  
10ed1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 49             /* xI
10ed2 6e 69 74 20 2a 2f 0a 20 20 20 20 70 63 61 63 68  nit */.    pcach
10ed3 65 31 53 68 75 74 64 6f 77 6e 2c 20 20 20 20 20  e1Shutdown,     
10ed4 20 20 20 20 2f 2a 20 78 53 68 75 74 64 6f 77 6e      /* xShutdown
10ed5 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 43   */.    pcache1C
10ed6 72 65 61 74 65 2c 20 20 20 20 20 20 20 20 20 20  reate,          
10ed7 20 2f 2a 20 78 43 72 65 61 74 65 20 2a 2f 0a 20   /* xCreate */. 
10ed8 20 20 20 70 63 61 63 68 65 31 43 61 63 68 65 73     pcache1Caches
10ed9 69 7a 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 78  ize,        /* x
10eda 43 61 63 68 65 73 69 7a 65 20 2a 2f 0a 20 20 20  Cachesize */.   
10edb 20 70 63 61 63 68 65 31 50 61 67 65 63 6f 75 6e   pcache1Pagecoun
10edc 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 50 61  t,        /* xPa
10edd 67 65 63 6f 75 6e 74 20 2a 2f 0a 20 20 20 20 70  gecount */.    p
10ede 63 61 63 68 65 31 46 65 74 63 68 2c 20 20 20 20  cache1Fetch,    
10edf 20 20 20 20 20 20 20 20 2f 2a 20 78 46 65 74 63          /* xFetc
10ee0 68 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31  h */.    pcache1
10ee1 55 6e 70 69 6e 2c 20 20 20 20 20 20 20 20 20 20  Unpin,          
10ee2 20 20 2f 2a 20 78 55 6e 70 69 6e 20 2a 2f 0a 20    /* xUnpin */. 
10ee3 20 20 20 70 63 61 63 68 65 31 52 65 6b 65 79 2c     pcache1Rekey,
10ee4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
10ee5 52 65 6b 65 79 20 2a 2f 0a 20 20 20 20 70 63 61  Rekey */.    pca
10ee6 63 68 65 31 54 72 75 6e 63 61 74 65 2c 20 20 20  che1Truncate,   
10ee7 20 20 20 20 20 20 2f 2a 20 78 54 72 75 6e 63 61        /* xTrunca
10ee8 74 65 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65  te */.    pcache
10ee9 31 44 65 73 74 72 6f 79 20 20 20 20 20 20 20 20  1Destroy        
10eea 20 20 20 2f 2a 20 78 44 65 73 74 72 6f 79 20 2a     /* xDestroy *
10eeb 2f 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33  /.  };.  sqlite3
10eec 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43  _config(SQLITE_C
10eed 4f 4e 46 49 47 5f 50 43 41 43 48 45 2c 20 26 64  ONFIG_PCACHE, &d
10eee 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 3b 0a  efaultMethods);.
10eef 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  }..#ifdef SQLITE
10ef0 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d  _ENABLE_MEMORY_M
10ef1 41 4e 41 47 45 4d 45 4e 54 0a 2f 2a 0a 2a 2a 20  ANAGEMENT./*.** 
10ef2 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
10ef3 20 63 61 6c 6c 65 64 20 74 6f 20 66 72 65 65 20   called to free 
10ef4 73 75 70 65 72 66 6c 75 6f 75 73 20 64 79 6e 61  superfluous dyna
10ef5 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65  mically allocate
10ef6 64 20 6d 65 6d 6f 72 79 0a 2a 2a 20 68 65 6c 64  d memory.** held
10ef7 20 62 79 20 74 68 65 20 70 61 67 65 72 20 73 79   by the pager sy
10ef8 73 74 65 6d 2e 20 4d 65 6d 6f 72 79 20 69 6e 20  stem. Memory in 
10ef9 75 73 65 20 62 79 20 61 6e 79 20 53 51 4c 69 74  use by any SQLit
10efa 65 20 70 61 67 65 72 20 61 6c 6c 6f 63 61 74 65  e pager allocate
10efb 64 0a 2a 2a 20 62 79 20 74 68 65 20 63 75 72 72  d.** by the curr
10efc 65 6e 74 20 74 68 72 65 61 64 20 6d 61 79 20 62  ent thread may b
10efd 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29  e sqlite3_free()
10efe 65 64 2e 0a 2a 2a 0a 2a 2a 20 6e 52 65 71 20 69  ed..**.** nReq i
10eff 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
10f00 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20  bytes of memory 
10f01 72 65 71 75 69 72 65 64 2e 20 4f 6e 63 65 20 74  required. Once t
10f02 68 69 73 20 6d 75 63 68 20 68 61 73 0a 2a 2a 20  his much has.** 
10f03 62 65 65 6e 20 72 65 6c 65 61 73 65 64 2c 20 74  been released, t
10f04 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  he function retu
10f05 72 6e 73 2e 20 54 68 65 20 72 65 74 75 72 6e 20  rns. The return 
10f06 76 61 6c 75 65 20 69 73 20 74 68 65 20 74 6f 74  value is the tot
10f07 61 6c 20 6e 75 6d 62 65 72 20 0a 2a 2a 20 6f 66  al number .** of
10f08 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79   bytes of memory
10f09 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2f 0a 53 51   released..*/.SQ
10f0a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
10f0b 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65   sqlite3PcacheRe
10f0c 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69 6e 74 20  leaseMemory(int 
10f0d 6e 52 65 71 29 7b 0a 20 20 69 6e 74 20 6e 46 72  nReq){.  int nFr
10f0e 65 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 63  ee = 0;.  if( pc
10f0f 61 63 68 65 31 2e 70 53 74 61 72 74 3d 3d 30 20  ache1.pStart==0 
10f10 29 7b 0a 20 20 20 20 50 67 48 64 72 31 20 2a 70  ){.    PgHdr1 *p
10f11 3b 0a 20 20 20 20 70 63 61 63 68 65 31 45 6e 74  ;.    pcache1Ent
10f12 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 77  erMutex();.    w
10f13 68 69 6c 65 28 20 28 6e 52 65 71 3c 30 20 7c 7c  hile( (nReq<0 ||
10f14 20 6e 46 72 65 65 3c 6e 52 65 71 29 20 26 26 20   nFree<nReq) && 
10f15 28 70 3d 70 63 61 63 68 65 31 2e 70 4c 72 75 54  (p=pcache1.pLruT
10f16 61 69 6c 29 20 29 7b 0a 20 20 20 20 20 20 6e 46  ail) ){.      nF
10f17 72 65 65 20 2b 3d 20 73 71 6c 69 74 65 33 4d 61  ree += sqlite3Ma
10f18 6c 6c 6f 63 53 69 7a 65 28 50 47 48 44 52 31 5f  llocSize(PGHDR1_
10f19 54 4f 5f 50 41 47 45 28 70 29 29 3b 0a 20 20 20  TO_PAGE(p));.   
10f1a 20 20 20 70 63 61 63 68 65 31 50 69 6e 50 61 67     pcache1PinPag
10f1b 65 28 70 29 3b 0a 20 20 20 20 20 20 70 63 61 63  e(p);.      pcac
10f1c 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73  he1RemoveFromHas
10f1d 68 28 70 29 3b 0a 20 20 20 20 20 20 70 63 61 63  h(p);.      pcac
10f1e 68 65 31 46 72 65 65 50 61 67 65 28 70 29 3b 0a  he1FreePage(p);.
10f1f 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63 68 65      }.    pcache
10f20 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20  1LeaveMutex();. 
10f21 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 46 72 65   }.  return nFre
10f22 65 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  e;.}.#endif /* S
10f23 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
10f24 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 20 2a  ORY_MANAGEMENT *
10f25 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  /..#ifdef SQLITE
10f26 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73  _TEST./*.** This
10f27 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
10f28 64 20 62 79 20 74 65 73 74 20 70 72 6f 63 65 64  d by test proced
10f29 75 72 65 73 20 74 6f 20 69 6e 73 70 65 63 74 20  ures to inspect 
10f2a 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 74 61  the internal sta
10f2b 74 65 0a 2a 2a 20 6f 66 20 74 68 65 20 67 6c 6f  te.** of the glo
10f2c 62 61 6c 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51  bal cache..*/.SQ
10f2d 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
10f2e 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53  d sqlite3PcacheS
10f2f 74 61 74 73 28 0a 20 20 69 6e 74 20 2a 70 6e 43  tats(.  int *pnC
10f30 75 72 72 65 6e 74 2c 20 20 20 20 20 20 2f 2a 20  urrent,      /* 
10f31 4f 55 54 3a 20 54 6f 74 61 6c 20 6e 75 6d 62 65  OUT: Total numbe
10f32 72 20 6f 66 20 70 61 67 65 73 20 63 61 63 68 65  r of pages cache
10f33 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 4d 61  d */.  int *pnMa
10f34 78 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  x,          /* O
10f35 55 54 3a 20 47 6c 6f 62 61 6c 20 6d 61 78 69 6d  UT: Global maxim
10f36 75 6d 20 63 61 63 68 65 20 73 69 7a 65 20 2a 2f  um cache size */
10f37 0a 20 20 69 6e 74 20 2a 70 6e 4d 69 6e 2c 20 20  .  int *pnMin,  
10f38 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
10f39 53 75 6d 20 6f 66 20 50 43 61 63 68 65 31 2e 6e  Sum of PCache1.n
10f3a 4d 69 6e 20 66 6f 72 20 70 75 72 67 65 61 62 6c  Min for purgeabl
10f3b 65 20 63 61 63 68 65 73 20 2a 2f 0a 20 20 69 6e  e caches */.  in
10f3c 74 20 2a 70 6e 52 65 63 79 63 6c 61 62 6c 65 20  t *pnRecyclable 
10f3d 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f 74 61 6c     /* OUT: Total
10f3e 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
10f3f 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 72   available for r
10f40 65 63 79 63 6c 69 6e 67 20 2a 2f 0a 29 7b 0a 20  ecycling */.){. 
10f41 20 50 67 48 64 72 31 20 2a 70 3b 0a 20 20 69 6e   PgHdr1 *p;.  in
10f42 74 20 6e 52 65 63 79 63 6c 61 62 6c 65 20 3d 20  t nRecyclable = 
10f43 30 3b 0a 20 20 66 6f 72 28 70 3d 70 63 61 63 68  0;.  for(p=pcach
10f44 65 31 2e 70 4c 72 75 48 65 61 64 3b 20 70 3b 20  e1.pLruHead; p; 
10f45 70 3d 70 2d 3e 70 4c 72 75 4e 65 78 74 29 7b 0a  p=p->pLruNext){.
10f46 20 20 20 20 6e 52 65 63 79 63 6c 61 62 6c 65 2b      nRecyclable+
10f47 2b 3b 0a 20 20 7d 0a 20 20 2a 70 6e 43 75 72 72  +;.  }.  *pnCurr
10f48 65 6e 74 20 3d 20 70 63 61 63 68 65 31 2e 6e 43  ent = pcache1.nC
10f49 75 72 72 65 6e 74 50 61 67 65 3b 0a 20 20 2a 70  urrentPage;.  *p
10f4a 6e 4d 61 78 20 3d 20 70 63 61 63 68 65 31 2e 6e  nMax = pcache1.n
10f4b 4d 61 78 50 61 67 65 3b 0a 20 20 2a 70 6e 4d 69  MaxPage;.  *pnMi
10f4c 6e 20 3d 20 70 63 61 63 68 65 31 2e 6e 4d 69 6e  n = pcache1.nMin
10f4d 50 61 67 65 3b 0a 20 20 2a 70 6e 52 65 63 79 63  Page;.  *pnRecyc
10f4e 6c 61 62 6c 65 20 3d 20 6e 52 65 63 79 63 6c 61  lable = nRecycla
10f4f 62 6c 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  ble;.}.#endif../
10f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
10f51 6e 64 20 6f 66 20 70 63 61 63 68 65 31 2e 63 20  nd of pcache1.c 
10f52 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10f53 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10f54 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
10f55 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
10f56 65 67 69 6e 20 66 69 6c 65 20 72 6f 77 73 65 74  egin file rowset
10f57 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
10f58 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10f59 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
10f5a 2a 0a 2a 2a 20 32 30 30 38 20 44 65 63 65 6d 62  *.** 2008 Decemb
10f5b 65 72 20 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  er 3.**.** The a
10f5c 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
10f5d 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
10f5e 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
10f5f 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
10f60 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
10f61 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
10f62 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
10f63 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
10f64 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
10f65 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
10f66 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
10f67 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
10f68 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
10f69 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
10f6a 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
10f6b 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
10f6c 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
10f6d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10f6e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10f6f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10f71 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d  ***.**.** This m
10f72 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73  odule implements
10f73 20 61 6e 20 6f 62 6a 65 63 74 20 77 65 20 63 61   an object we ca
10f74 6c 6c 20 61 20 22 52 6f 77 53 65 74 22 2e 0a 2a  ll a "RowSet"..*
10f75 2a 0a 2a 2a 20 54 68 65 20 52 6f 77 53 65 74 20  *.** The RowSet 
10f76 6f 62 6a 65 63 74 20 69 73 20 61 20 63 6f 6c 6c  object is a coll
10f77 65 63 74 69 6f 6e 20 6f 66 20 72 6f 77 69 64 73  ection of rowids
10f78 2e 20 20 52 6f 77 69 64 73 0a 2a 2a 20 61 72 65  .  Rowids.** are
10f79 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74   inserted into t
10f7a 68 65 20 52 6f 77 53 65 74 20 69 6e 20 61 6e 20  he RowSet in an 
10f7b 61 72 62 69 74 72 61 72 79 20 6f 72 64 65 72 2e  arbitrary order.
10f7c 20 20 49 6e 73 65 72 74 73 0a 2a 2a 20 63 61 6e    Inserts.** can
10f7d 20 62 65 20 69 6e 74 65 72 6d 69 78 65 64 20 77   be intermixed w
10f7e 69 74 68 20 74 65 73 74 73 20 74 6f 20 73 65 65  ith tests to see
10f7f 20 69 66 20 61 20 67 69 76 65 6e 20 72 6f 77 69   if a given rowi
10f80 64 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 70 72  d has been.** pr
10f81 65 76 69 6f 75 73 6c 79 20 69 6e 73 65 72 74 65  eviously inserte
10f82 64 20 69 6e 74 6f 20 74 68 65 20 52 6f 77 53 65  d into the RowSe
10f83 74 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 61  t..**.** After a
10f84 6c 6c 20 69 6e 73 65 72 74 73 20 61 72 65 20 66  ll inserts are f
10f85 69 6e 69 73 68 65 64 2c 20 69 74 20 69 73 20 70  inished, it is p
10f86 6f 73 73 69 62 6c 65 20 74 6f 20 65 78 74 72 61  ossible to extra
10f87 63 74 20 74 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e  ct the.** elemen
10f88 74 73 20 6f 66 20 74 68 65 20 52 6f 77 53 65 74  ts of the RowSet
10f89 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72   in sorted order
10f8a 2e 20 20 4f 6e 63 65 20 74 68 69 73 20 65 78 74  .  Once this ext
10f8b 72 61 63 74 69 6f 6e 0a 2a 2a 20 70 72 6f 63 65  raction.** proce
10f8c 73 73 20 68 61 73 20 73 74 61 72 74 65 64 2c 20  ss has started, 
10f8d 6e 6f 20 6e 65 77 20 65 6c 65 6d 65 6e 74 73 20  no new elements 
10f8e 6d 61 79 20 62 65 20 69 6e 73 65 72 74 65 64 2e  may be inserted.
10f8f 0a 2a 2a 0a 2a 2a 20 48 65 6e 63 65 2c 20 74 68  .**.** Hence, th
10f90 65 20 70 72 69 6d 69 74 69 76 65 20 6f 70 65 72  e primitive oper
10f91 61 74 69 6f 6e 73 20 66 6f 72 20 61 20 52 6f 77  ations for a Row
10f92 53 65 74 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20  Set are:.**.**  
10f93 20 20 43 52 45 41 54 45 0a 2a 2a 20 20 20 20 49    CREATE.**    I
10f94 4e 53 45 52 54 0a 2a 2a 20 20 20 20 54 45 53 54  NSERT.**    TEST
10f95 0a 2a 2a 20 20 20 20 53 4d 41 4c 4c 45 53 54 0a  .**    SMALLEST.
10f96 2a 2a 20 20 20 20 44 45 53 54 52 4f 59 0a 2a 2a  **    DESTROY.**
10f97 0a 2a 2a 20 54 68 65 20 43 52 45 41 54 45 20 61  .** The CREATE a
10f98 6e 64 20 44 45 53 54 52 4f 59 20 70 72 69 6d 69  nd DESTROY primi
10f99 74 69 76 65 73 20 61 72 65 20 74 68 65 20 63 6f  tives are the co
10f9a 6e 73 74 72 75 63 74 6f 72 20 61 6e 64 20 64 65  nstructor and de
10f9b 73 74 72 75 63 74 6f 72 2c 0a 2a 2a 20 6f 62 76  structor,.** obv
10f9c 69 6f 75 73 6c 79 2e 20 20 54 68 65 20 49 4e 53  iously.  The INS
10f9d 45 52 54 20 70 72 69 6d 69 74 69 76 65 20 61 64  ERT primitive ad
10f9e 64 73 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74  ds a new element
10f9f 20 74 6f 20 74 68 65 20 52 6f 77 53 65 74 2e 0a   to the RowSet..
10fa0 2a 2a 20 54 45 53 54 20 63 68 65 63 6b 73 20 74  ** TEST checks t
10fa1 6f 20 73 65 65 20 69 66 20 61 6e 20 65 6c 65 6d  o see if an elem
10fa2 65 6e 74 20 69 73 20 61 6c 72 65 61 64 79 20 69  ent is already i
10fa3 6e 20 74 68 65 20 52 6f 77 53 65 74 2e 20 20 53  n the RowSet.  S
10fa4 4d 41 4c 4c 45 53 54 0a 2a 2a 20 65 78 74 72 61  MALLEST.** extra
10fa5 63 74 73 20 74 68 65 20 6c 65 61 73 74 20 76 61  cts the least va
10fa6 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 52 6f 77  lue from the Row
10fa7 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49  Set..**.** The I
10fa8 4e 53 45 52 54 20 70 72 69 6d 69 74 69 76 65 20  NSERT primitive 
10fa9 6d 69 67 68 74 20 61 6c 6c 6f 63 61 74 65 20 61  might allocate a
10faa 64 64 69 74 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79  dditional memory
10fab 2e 20 20 4d 65 6d 6f 72 79 20 69 73 0a 2a 2a 20  .  Memory is.** 
10fac 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 63 68 75  allocated in chu
10fad 6e 6b 73 20 73 6f 20 6d 6f 73 74 20 49 4e 53 45  nks so most INSE
10fae 52 54 73 20 64 6f 20 6e 6f 20 61 6c 6c 6f 63 61  RTs do no alloca
10faf 74 69 6f 6e 2e 20 20 54 68 65 72 65 20 69 73 20  tion.  There is 
10fb0 61 6e 20 0a 2a 2a 20 75 70 70 65 72 20 62 6f 75  an .** upper bou
10fb1 6e 64 20 6f 6e 20 74 68 65 20 73 69 7a 65 20 6f  nd on the size o
10fb2 66 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f  f allocated memo
10fb3 72 79 2e 20 20 4e 6f 20 6d 65 6d 6f 72 79 20 69  ry.  No memory i
10fb4 73 20 66 72 65 65 64 0a 2a 2a 20 75 6e 74 69 6c  s freed.** until
10fb5 20 44 45 53 54 52 4f 59 2e 0a 2a 2a 0a 2a 2a 20   DESTROY..**.** 
10fb6 54 68 65 20 54 45 53 54 20 70 72 69 6d 69 74 69  The TEST primiti
10fb7 76 65 20 69 6e 63 6c 75 64 65 73 20 61 20 22 62  ve includes a "b
10fb8 61 74 63 68 22 20 6e 75 6d 62 65 72 2e 20 20 54  atch" number.  T
10fb9 68 65 20 54 45 53 54 20 70 72 69 6d 69 74 69 76  he TEST primitiv
10fba 65 0a 2a 2a 20 77 69 6c 6c 20 6f 6e 6c 79 20 73  e.** will only s
10fbb 65 65 20 65 6c 65 6d 65 6e 74 73 20 74 68 61 74  ee elements that
10fbc 20 77 65 72 65 20 69 6e 73 65 72 74 65 64 20 62   were inserted b
10fbd 65 66 6f 72 65 20 74 68 65 20 6c 61 73 74 20 63  efore the last c
10fbe 68 61 6e 67 65 0a 2a 2a 20 69 6e 20 74 68 65 20  hange.** in the 
10fbf 62 61 74 63 68 20 6e 75 6d 62 65 72 2e 20 20 49  batch number.  I
10fc0 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69  n other words, i
10fc1 66 20 61 6e 20 49 4e 53 45 52 54 20 6f 63 63 75  f an INSERT occu
10fc2 72 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 74 77  rs between.** tw
10fc3 6f 20 54 45 53 54 73 20 77 68 65 72 65 20 74 68  o TESTs where th
10fc4 65 20 54 45 53 54 73 20 68 61 76 65 20 74 68 65  e TESTs have the
10fc5 20 73 61 6d 65 20 62 61 74 63 68 20 6e 75 62 6d   same batch nubm
10fc6 65 72 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20  er, then the.** 
10fc7 76 61 6c 75 65 20 61 64 64 65 64 20 62 79 20 74  value added by t
10fc8 68 65 20 49 4e 53 45 52 54 20 77 69 6c 6c 20 6e  he INSERT will n
10fc9 6f 74 20 62 65 20 76 69 73 69 62 6c 65 20 74 6f  ot be visible to
10fca 20 74 68 65 20 73 65 63 6f 6e 64 20 54 45 53 54   the second TEST
10fcb 2e 0a 2a 2a 20 54 68 65 20 69 6e 69 74 69 61 6c  ..** The initial
10fcc 20 62 61 74 63 68 20 6e 75 6d 62 65 72 20 69 73   batch number is
10fcd 20 7a 65 72 6f 2c 20 73 6f 20 69 66 20 74 68 65   zero, so if the
10fce 20 76 65 72 79 20 66 69 72 73 74 20 54 45 53 54   very first TEST
10fcf 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 6e   contains.** a n
10fd0 6f 6e 2d 7a 65 72 6f 20 62 61 74 63 68 20 6e 75  on-zero batch nu
10fd1 6d 62 65 72 2c 20 69 74 20 77 69 6c 6c 20 73 65  mber, it will se
10fd2 65 20 61 6c 6c 20 70 72 69 6f 72 20 49 4e 53 45  e all prior INSE
10fd3 52 54 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20 49 4e  RTs..**.** No IN
10fd4 53 45 52 54 73 20 6d 61 79 20 6f 63 63 75 72 73  SERTs may occurs
10fd5 20 61 66 74 65 72 20 61 20 53 4d 41 4c 4c 45 53   after a SMALLES
10fd6 54 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e  T.  An assertion
10fd7 20 77 69 6c 6c 20 66 61 69 6c 20 69 66 0a 2a 2a   will fail if.**
10fd8 20 74 68 61 74 20 69 73 20 61 74 74 65 6d 70 74   that is attempt
10fd9 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  ed..**.** The co
10fda 73 74 20 6f 66 20 61 6e 20 49 4e 53 45 52 54 20  st of an INSERT 
10fdb 69 73 20 72 6f 75 67 68 6c 79 20 63 6f 6e 73 74  is roughly const
10fdc 61 6e 74 2e 20 20 28 53 6f 6d 65 74 69 6d 65 20  ant.  (Sometime 
10fdd 6e 65 77 20 6d 65 6d 6f 72 79 0a 2a 2a 20 68 61  new memory.** ha
10fde 73 20 74 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65  s to be allocate
10fdf 64 20 6f 6e 20 61 6e 20 49 4e 53 45 52 54 2e 29  d on an INSERT.)
10fe0 20 20 54 68 65 20 63 6f 73 74 20 6f 66 20 61 20    The cost of a 
10fe1 54 45 53 54 20 77 69 74 68 20 61 20 6e 65 77 0a  TEST with a new.
10fe2 2a 2a 20 62 61 74 63 68 20 6e 75 6d 62 65 72 20  ** batch number 
10fe3 69 73 20 4f 28 4e 6c 6f 67 4e 29 20 77 68 65 72  is O(NlogN) wher
10fe4 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65  e N is the numbe
10fe5 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e  r of elements in
10fe6 20 74 68 65 20 52 6f 77 53 65 74 2e 0a 2a 2a 20   the RowSet..** 
10fe7 54 68 65 20 63 6f 73 74 20 6f 66 20 61 20 54 45  The cost of a TE
10fe8 53 54 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d  ST using the sam
10fe9 65 20 62 61 74 63 68 20 6e 75 6d 62 65 72 20 69  e batch number i
10fea 73 20 4f 28 6c 6f 67 4e 29 2e 20 20 54 68 65 20  s O(logN).  The 
10feb 63 6f 73 74 0a 2a 2a 20 6f 66 20 74 68 65 20 66  cost.** of the f
10fec 69 72 73 74 20 53 4d 41 4c 4c 45 53 54 20 69 73  irst SMALLEST is
10fed 20 4f 28 4e 6c 6f 67 4e 29 2e 20 20 53 65 63 6f   O(NlogN).  Seco
10fee 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e  nd and subsequen
10fef 74 20 53 4d 41 4c 4c 45 53 54 0a 2a 2a 20 70 72  t SMALLEST.** pr
10ff0 69 6d 69 74 69 76 65 73 20 61 72 65 20 63 6f 6e  imitives are con
10ff1 73 74 61 6e 74 20 74 69 6d 65 2e 20 20 54 68 65  stant time.  The
10ff2 20 63 6f 73 74 20 6f 66 20 44 45 53 54 52 4f 59   cost of DESTROY
10ff3 20 69 73 20 4f 28 4e 29 2e 0a 2a 2a 0a 2a 2a 20   is O(N)..**.** 
10ff4 54 68 65 72 65 20 69 73 20 61 6e 20 61 64 64 65  There is an adde
10ff5 64 20 63 6f 73 74 20 6f 66 20 4f 28 4e 29 20 77  d cost of O(N) w
10ff6 68 65 6e 20 73 77 69 74 63 68 69 6e 67 20 62 65  hen switching be
10ff7 74 77 65 65 6e 20 54 45 53 54 20 61 6e 64 0a 2a  tween TEST and.*
10ff8 2a 20 53 4d 41 4c 4c 45 53 54 20 70 72 69 6d 69  * SMALLEST primi
10ff9 74 69 76 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  tives..**.** $Id
10ffa 3a 20 72 6f 77 73 65 74 2e 63 2c 76 20 31 2e 37  : rowset.c,v 1.7
10ffb 20 32 30 30 39 2f 30 35 2f 32 32 20 30 31 3a 30   2009/05/22 01:0
10ffc 30 3a 31 33 20 64 72 68 20 45 78 70 20 24 0a 2a  0:13 drh Exp $.*
10ffd 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 61 72 67 65 74  /.../*.** Target
10ffe 20 73 69 7a 65 20 66 6f 72 20 61 6c 6c 6f 63 61   size for alloca
10fff 74 69 6f 6e 20 63 68 75 6e 6b 73 2e 0a 2a 2f 0a  tion chunks..*/.
11000 23 64 65 66 69 6e 65 20 52 4f 57 53 45 54 5f 41  #define ROWSET_A
11001 4c 4c 4f 43 41 54 49 4f 4e 5f 53 49 5a 45 20 31  LLOCATION_SIZE 1
11002 30 32 34 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e  024../*.** The n
11003 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 65 74 20  umber of rowset 
11004 65 6e 74 72 69 65 73 20 70 65 72 20 61 6c 6c 6f  entries per allo
11005 63 61 74 69 6f 6e 20 63 68 75 6e 6b 2e 0a 2a 2f  cation chunk..*/
11006 0a 23 64 65 66 69 6e 65 20 52 4f 57 53 45 54 5f  .#define ROWSET_
11007 45 4e 54 52 59 5f 50 45 52 5f 43 48 55 4e 4b 20  ENTRY_PER_CHUNK 
11008 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   \.             
11009 20 20 20 20 20 20 20 20 20 20 28 28 52 4f 57 53            ((ROWS
1100a 45 54 5f 41 4c 4c 4f 43 41 54 49 4f 4e 5f 53 49  ET_ALLOCATION_SI
1100b 5a 45 2d 38 29 2f 73 69 7a 65 6f 66 28 73 74 72  ZE-8)/sizeof(str
1100c 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 29  uct RowSetEntry)
1100d 29 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 65 6e  )../*.** Each en
1100e 74 72 79 20 69 6e 20 61 20 52 6f 77 53 65 74 20  try in a RowSet 
1100f 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  is an instance o
11010 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
11011 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 72 75 63  object..*/.struc
11012 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 7b 20  t RowSetEntry { 
11013 20 20 20 20 20 20 20 20 20 20 20 0a 20 20 69 36             .  i6
11014 34 20 76 3b 20 20 20 20 20 20 20 20 20 20 20 20  4 v;            
11015 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
11016 4f 57 49 44 20 76 61 6c 75 65 20 66 6f 72 20 74  OWID value for t
11017 68 69 73 20 65 6e 74 72 79 20 2a 2f 0a 20 20 73  his entry */.  s
11018 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72  truct RowSetEntr
11019 79 20 2a 70 52 69 67 68 74 3b 20 20 20 2f 2a 20  y *pRight;   /* 
1101a 52 69 67 68 74 20 73 75 62 74 72 65 65 20 28 6c  Right subtree (l
1101b 61 72 67 65 72 20 65 6e 74 72 69 65 73 29 20 6f  arger entries) o
1101c 72 20 6c 69 73 74 20 2a 2f 0a 20 20 73 74 72 75  r list */.  stru
1101d 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a  ct RowSetEntry *
1101e 70 4c 65 66 74 3b 20 20 20 20 2f 2a 20 4c 65 66  pLeft;    /* Lef
1101f 74 20 73 75 62 74 72 65 65 20 28 73 6d 61 6c 6c  t subtree (small
11020 65 72 20 65 6e 74 72 69 65 73 29 20 2a 2f 0a 7d  er entries) */.}
11021 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 77 53 65 74 45  ;../*.** RowSetE
11022 6e 74 72 79 20 6f 62 6a 65 63 74 73 20 61 72 65  ntry objects are
11023 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 6c 61   allocated in la
11024 72 67 65 20 63 68 75 6e 6b 73 20 28 69 6e 73 74  rge chunks (inst
11025 61 6e 63 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20  ances of the.** 
11026 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
11027 75 72 65 29 20 74 6f 20 72 65 64 75 63 65 20 6d  ure) to reduce m
11028 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
11029 20 6f 76 65 72 68 65 61 64 2e 20 20 54 68 65 0a   overhead.  The.
1102a 2a 2a 20 63 68 75 6e 6b 73 20 61 72 65 20 6b 65  ** chunks are ke
1102b 70 74 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c  pt on a linked l
1102c 69 73 74 20 73 6f 20 74 68 61 74 20 74 68 65 79  ist so that they
1102d 20 63 61 6e 20 62 65 20 64 65 61 6c 6c 6f 63 61   can be dealloca
1102e 74 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20  ted.** when the 
1102f 52 6f 77 53 65 74 20 69 73 20 64 65 73 74 72 6f  RowSet is destro
11030 79 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52  yed..*/.struct R
11031 6f 77 53 65 74 43 68 75 6e 6b 20 7b 0a 20 20 73  owSetChunk {.  s
11032 74 72 75 63 74 20 52 6f 77 53 65 74 43 68 75 6e  truct RowSetChun
11033 6b 20 2a 70 4e 65 78 74 43 68 75 6e 6b 3b 20 20  k *pNextChunk;  
11034 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 63 68        /* Next ch
11035 75 6e 6b 20 6f 6e 20 6c 69 73 74 20 6f 66 20 74  unk on list of t
11036 68 65 6d 20 61 6c 6c 20 2a 2f 0a 20 20 73 74 72  hem all */.  str
11037 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20  uct RowSetEntry 
11038 61 45 6e 74 72 79 5b 52 4f 57 53 45 54 5f 45 4e  aEntry[ROWSET_EN
11039 54 52 59 5f 50 45 52 5f 43 48 55 4e 4b 5d 3b 20  TRY_PER_CHUNK]; 
1103a 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20 65 6e 74  /* Allocated ent
1103b 72 69 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  ries */.};../*.*
1103c 2a 20 41 20 52 6f 77 53 65 74 20 69 6e 20 61 6e  * A RowSet in an
1103d 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
1103e 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
1103f 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 79  ture..**.** A ty
11040 70 65 64 65 66 20 6f 66 20 74 68 69 73 20 73 74  pedef of this st
11041 72 75 63 74 75 72 65 20 69 66 20 66 6f 75 6e 64  ructure if found
11042 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 2e   in sqliteInt.h.
11043 0a 2a 2f 0a 73 74 72 75 63 74 20 52 6f 77 53 65  .*/.struct RowSe
11044 74 20 7b 0a 20 20 73 74 72 75 63 74 20 52 6f 77  t {.  struct Row
11045 53 65 74 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b  SetChunk *pChunk
11046 3b 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20  ;    /* List of 
11047 61 6c 6c 20 63 68 75 6e 6b 20 61 6c 6c 6f 63 61  all chunk alloca
11048 74 69 6f 6e 73 20 2a 2f 0a 20 20 73 71 6c 69 74  tions */.  sqlit
11049 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20  e3 *db;         
1104a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
1104b 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
1104c 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 72 75 63 74  tion */.  struct
1104d 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 45   RowSetEntry *pE
1104e 6e 74 72 79 3b 20 20 20 20 2f 2a 20 4c 69 73 74  ntry;    /* List
1104f 20 6f 66 20 65 6e 74 72 69 65 73 20 75 73 69 6e   of entries usin
11050 67 20 70 52 69 67 68 74 20 2a 2f 0a 20 20 73 74  g pRight */.  st
11051 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
11052 20 2a 70 4c 61 73 74 3b 20 20 20 20 20 2f 2a 20   *pLast;     /* 
11053 4c 61 73 74 20 65 6e 74 72 79 20 6f 6e 20 74 68  Last entry on th
11054 65 20 70 45 6e 74 72 79 20 6c 69 73 74 20 2a 2f  e pEntry list */
11055 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74  .  struct RowSet
11056 45 6e 74 72 79 20 2a 70 46 72 65 73 68 3b 20 20  Entry *pFresh;  
11057 20 20 2f 2a 20 53 6f 75 72 63 65 20 6f 66 20 6e    /* Source of n
11058 65 77 20 65 6e 74 72 79 20 6f 62 6a 65 63 74 73  ew entry objects
11059 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77   */.  struct Row
1105a 53 65 74 45 6e 74 72 79 20 2a 70 54 72 65 65 3b  SetEntry *pTree;
1105b 20 20 20 20 20 2f 2a 20 42 69 6e 61 72 79 20 74       /* Binary t
1105c 72 65 65 20 6f 66 20 65 6e 74 72 69 65 73 20 2a  ree of entries *
1105d 2f 0a 20 20 75 31 36 20 6e 46 72 65 73 68 3b 20  /.  u16 nFresh; 
1105e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1105f 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
11060 6f 62 6a 65 63 74 73 20 6f 6e 20 70 46 72 65 73  objects on pFres
11061 68 20 2a 2f 0a 20 20 75 38 20 69 73 53 6f 72 74  h */.  u8 isSort
11062 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ed;             
11063 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
11064 20 70 45 6e 74 72 79 20 69 73 20 73 6f 72 74 65   pEntry is sorte
11065 64 20 2a 2f 0a 20 20 75 38 20 69 42 61 74 63 68  d */.  u8 iBatch
11066 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
11067 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74        /* Current
11068 20 69 6e 73 65 72 74 20 62 61 74 63 68 20 2a 2f   insert batch */
11069 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20  .};../*.** Turn 
1106a 62 75 6c 6b 20 6d 65 6d 6f 72 79 20 69 6e 74 6f  bulk memory into
1106b 20 61 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74   a RowSet object
1106c 2e 20 20 4e 20 62 79 74 65 73 20 6f 66 20 6d 65  .  N bytes of me
1106d 6d 6f 72 79 0a 2a 2a 20 61 72 65 20 61 76 61 69  mory.** are avai
1106e 6c 61 62 6c 65 20 61 74 20 70 53 70 61 63 65 2e  lable at pSpace.
1106f 20 20 54 68 65 20 64 62 20 70 6f 69 6e 74 65 72    The db pointer
11070 20 69 73 20 75 73 65 64 20 61 73 20 61 20 6d 65   is used as a me
11071 6d 6f 72 79 20 63 6f 6e 74 65 78 74 0a 2a 2a 20  mory context.** 
11072 66 6f 72 20 61 6e 79 20 73 75 62 73 65 71 75 65  for any subseque
11073 6e 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74  nt allocations t
11074 68 61 74 20 6e 65 65 64 20 74 6f 20 6f 63 63 75  hat need to occu
11075 72 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70  r..** Return a p
11076 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65  ointer to the ne
11077 77 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 2e  w RowSet object.
11078 0a 2a 2a 0a 2a 2a 20 49 74 20 6d 75 73 74 20 62  .**.** It must b
11079 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74 20  e the case that 
1107a 4e 20 69 73 20 73 75 66 66 69 63 69 65 6e 74 20  N is sufficient 
1107b 74 6f 20 6d 61 6b 65 20 61 20 52 6f 77 73 65 74  to make a Rowset
1107c 2e 20 20 49 66 20 6e 6f 74 0a 2a 2a 20 61 6e 20  .  If not.** an 
1107d 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20  assertion fault 
1107e 6f 63 63 75 72 73 2e 0a 2a 2a 20 0a 2a 2a 20 49  occurs..** .** I
1107f 66 20 4e 20 69 73 20 6c 61 72 67 65 72 20 74 68  f N is larger th
11080 61 6e 20 74 68 65 20 6d 69 6e 69 6d 75 6d 2c 20  an the minimum, 
11081 75 73 65 20 74 68 65 20 73 75 72 70 6c 75 73 20  use the surplus 
11082 61 73 20 61 6e 20 69 6e 69 74 69 61 6c 0a 2a 2a  as an initial.**
11083 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 65   allocation of e
11084 6e 74 72 69 65 73 20 61 76 61 69 6c 61 62 6c 65  ntries available
11085 20 74 6f 20 62 65 20 66 69 6c 6c 65 64 2e 0a 2a   to be filled..*
11086 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
11087 20 52 6f 77 53 65 74 20 2a 73 71 6c 69 74 65 33   RowSet *sqlite3
11088 52 6f 77 53 65 74 49 6e 69 74 28 73 71 6c 69 74  RowSetInit(sqlit
11089 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 53  e3 *db, void *pS
1108a 70 61 63 65 2c 20 75 6e 73 69 67 6e 65 64 20 69  pace, unsigned i
1108b 6e 74 20 4e 29 7b 0a 20 20 52 6f 77 53 65 74 20  nt N){.  RowSet 
1108c 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 4e 20  *p;.  assert( N 
1108d 3e 3d 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66  >= ROUND8(sizeof
1108e 28 2a 70 29 29 20 29 3b 0a 20 20 70 20 3d 20 70  (*p)) );.  p = p
1108f 53 70 61 63 65 3b 0a 20 20 70 2d 3e 70 43 68 75  Space;.  p->pChu
11090 6e 6b 20 3d 20 30 3b 0a 20 20 70 2d 3e 64 62 20  nk = 0;.  p->db 
11091 3d 20 64 62 3b 0a 20 20 70 2d 3e 70 45 6e 74 72  = db;.  p->pEntr
11092 79 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 4c 61 73  y = 0;.  p->pLas
11093 74 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 54 72 65  t = 0;.  p->pTre
11094 65 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 46 72 65  e = 0;.  p->pFre
11095 73 68 20 3d 20 28 73 74 72 75 63 74 20 52 6f 77  sh = (struct Row
11096 53 65 74 45 6e 74 72 79 2a 29 28 52 4f 55 4e 44  SetEntry*)(ROUND
11097 38 28 73 69 7a 65 6f 66 28 2a 70 29 29 20 2b 20  8(sizeof(*p)) + 
11098 28 63 68 61 72 2a 29 70 29 3b 0a 20 20 70 2d 3e  (char*)p);.  p->
11099 6e 46 72 65 73 68 20 3d 20 28 75 31 36 29 28 28  nFresh = (u16)((
1109a 4e 20 2d 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f  N - ROUND8(sizeo
1109b 66 28 2a 70 29 29 29 2f 73 69 7a 65 6f 66 28 73  f(*p)))/sizeof(s
1109c 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72  truct RowSetEntr
1109d 79 29 29 3b 0a 20 20 70 2d 3e 69 73 53 6f 72 74  y));.  p->isSort
1109e 65 64 20 3d 20 31 3b 0a 20 20 70 2d 3e 69 42 61  ed = 1;.  p->iBa
1109f 74 63 68 20 3d 20 30 3b 0a 20 20 72 65 74 75 72  tch = 0;.  retur
110a0 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  n p;.}../*.** De
110a1 61 6c 6c 6f 63 61 74 65 20 61 6c 6c 20 63 68 75  allocate all chu
110a2 6e 6b 73 20 66 72 6f 6d 20 61 20 52 6f 77 53 65  nks from a RowSe
110a3 74 2e 20 20 54 68 69 73 20 66 72 65 65 73 20 61  t.  This frees a
110a4 6c 6c 20 6d 65 6d 6f 72 79 20 74 68 61 74 0a 2a  ll memory that.*
110a5 2a 20 74 68 65 20 52 6f 77 53 65 74 20 68 61 73  * the RowSet has
110a6 20 61 6c 6c 6f 63 61 74 65 64 20 6f 76 65 72 20   allocated over 
110a7 69 74 73 20 6c 69 66 65 74 69 6d 65 2e 20 20 54  its lifetime.  T
110a8 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 0a 2a  his routine is.*
110a9 2a 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72  * the destructor
110aa 20 66 6f 72 20 74 68 65 20 52 6f 77 53 65 74 2e   for the RowSet.
110ab 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
110ac 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52  TE void sqlite3R
110ad 6f 77 53 65 74 43 6c 65 61 72 28 52 6f 77 53 65  owSetClear(RowSe
110ae 74 20 2a 70 29 7b 0a 20 20 73 74 72 75 63 74 20  t *p){.  struct 
110af 52 6f 77 53 65 74 43 68 75 6e 6b 20 2a 70 43 68  RowSetChunk *pCh
110b0 75 6e 6b 2c 20 2a 70 4e 65 78 74 43 68 75 6e 6b  unk, *pNextChunk
110b1 3b 0a 20 20 66 6f 72 28 70 43 68 75 6e 6b 3d 70  ;.  for(pChunk=p
110b2 2d 3e 70 43 68 75 6e 6b 3b 20 70 43 68 75 6e 6b  ->pChunk; pChunk
110b3 3b 20 70 43 68 75 6e 6b 20 3d 20 70 4e 65 78 74  ; pChunk = pNext
110b4 43 68 75 6e 6b 29 7b 0a 20 20 20 20 70 4e 65 78  Chunk){.    pNex
110b5 74 43 68 75 6e 6b 20 3d 20 70 43 68 75 6e 6b 2d  tChunk = pChunk-
110b6 3e 70 4e 65 78 74 43 68 75 6e 6b 3b 0a 20 20 20  >pNextChunk;.   
110b7 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70   sqlite3DbFree(p
110b8 2d 3e 64 62 2c 20 70 43 68 75 6e 6b 29 3b 0a 20  ->db, pChunk);. 
110b9 20 7d 0a 20 20 70 2d 3e 70 43 68 75 6e 6b 20 3d   }.  p->pChunk =
110ba 20 30 3b 0a 20 20 70 2d 3e 6e 46 72 65 73 68 20   0;.  p->nFresh 
110bb 3d 20 30 3b 0a 20 20 70 2d 3e 70 45 6e 74 72 79  = 0;.  p->pEntry
110bc 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 4c 61 73 74   = 0;.  p->pLast
110bd 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 54 72 65 65   = 0;.  p->pTree
110be 20 3d 20 30 3b 0a 20 20 70 2d 3e 69 73 53 6f 72   = 0;.  p->isSor
110bf 74 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a  ted = 1;.}../*.*
110c0 2a 20 49 6e 73 65 72 74 20 61 20 6e 65 77 20 76  * Insert a new v
110c1 61 6c 75 65 20 69 6e 74 6f 20 61 20 52 6f 77 53  alue into a RowS
110c2 65 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61  et..**.** The ma
110c3 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20  llocFailed flag 
110c4 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
110c5 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 73 65  connection is se
110c6 74 20 69 66 20 61 0a 2a 2a 20 6d 65 6d 6f 72 79  t if a.** memory
110c7 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c   allocation fail
110c8 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
110c9 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
110ca 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28 52 6f  3RowSetInsert(Ro
110cb 77 53 65 74 20 2a 70 2c 20 69 36 34 20 72 6f 77  wSet *p, i64 row
110cc 69 64 29 7b 0a 20 20 73 74 72 75 63 74 20 52 6f  id){.  struct Ro
110cd 77 53 65 74 45 6e 74 72 79 20 2a 70 45 6e 74 72  wSetEntry *pEntr
110ce 79 3b 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 65  y;  /* The new e
110cf 6e 74 72 79 20 2a 2f 0a 20 20 73 74 72 75 63 74  ntry */.  struct
110d0 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c   RowSetEntry *pL
110d1 61 73 74 3b 20 20 20 2f 2a 20 54 68 65 20 6c 61  ast;   /* The la
110d2 73 74 20 70 72 69 6f 72 20 65 6e 74 72 79 20 2a  st prior entry *
110d3 2f 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30  /.  assert( p!=0
110d4 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 46 72   );.  if( p->nFr
110d5 65 73 68 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74  esh==0 ){.    st
110d6 72 75 63 74 20 52 6f 77 53 65 74 43 68 75 6e 6b  ruct RowSetChunk
110d7 20 2a 70 4e 65 77 3b 0a 20 20 20 20 70 4e 65 77   *pNew;.    pNew
110d8 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
110d9 6f 63 52 61 77 28 70 2d 3e 64 62 2c 20 73 69 7a  ocRaw(p->db, siz
110da 65 6f 66 28 2a 70 4e 65 77 29 29 3b 0a 20 20 20  eof(*pNew));.   
110db 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a   if( pNew==0 ){.
110dc 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
110dd 20 20 7d 0a 20 20 20 20 70 4e 65 77 2d 3e 70 4e    }.    pNew->pN
110de 65 78 74 43 68 75 6e 6b 20 3d 20 70 2d 3e 70 43  extChunk = p->pC
110df 68 75 6e 6b 3b 0a 20 20 20 20 70 2d 3e 70 43 68  hunk;.    p->pCh
110e0 75 6e 6b 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20  unk = pNew;.    
110e1 70 2d 3e 70 46 72 65 73 68 20 3d 20 70 4e 65 77  p->pFresh = pNew
110e2 2d 3e 61 45 6e 74 72 79 3b 0a 20 20 20 20 70 2d  ->aEntry;.    p-
110e3 3e 6e 46 72 65 73 68 20 3d 20 52 4f 57 53 45 54  >nFresh = ROWSET
110e4 5f 45 4e 54 52 59 5f 50 45 52 5f 43 48 55 4e 4b  _ENTRY_PER_CHUNK
110e5 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 20 3d  ;.  }.  pEntry =
110e6 20 70 2d 3e 70 46 72 65 73 68 2b 2b 3b 0a 20 20   p->pFresh++;.  
110e7 70 2d 3e 6e 46 72 65 73 68 2d 2d 3b 0a 20 20 70  p->nFresh--;.  p
110e8 45 6e 74 72 79 2d 3e 76 20 3d 20 72 6f 77 69 64  Entry->v = rowid
110e9 3b 0a 20 20 70 45 6e 74 72 79 2d 3e 70 52 69 67  ;.  pEntry->pRig
110ea 68 74 20 3d 20 30 3b 0a 20 20 70 4c 61 73 74 20  ht = 0;.  pLast 
110eb 3d 20 70 2d 3e 70 4c 61 73 74 3b 0a 20 20 69 66  = p->pLast;.  if
110ec 28 20 70 4c 61 73 74 20 29 7b 0a 20 20 20 20 69  ( pLast ){.    i
110ed 66 28 20 70 2d 3e 69 73 53 6f 72 74 65 64 20 26  f( p->isSorted &
110ee 26 20 72 6f 77 69 64 3c 3d 70 4c 61 73 74 2d 3e  & rowid<=pLast->
110ef 76 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 73  v ){.      p->is
110f0 53 6f 72 74 65 64 20 3d 20 30 3b 0a 20 20 20 20  Sorted = 0;.    
110f1 7d 0a 20 20 20 20 70 4c 61 73 74 2d 3e 70 52 69  }.    pLast->pRi
110f2 67 68 74 20 3d 20 70 45 6e 74 72 79 3b 0a 20 20  ght = pEntry;.  
110f3 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72  }else{.    asser
110f4 74 28 20 70 2d 3e 70 45 6e 74 72 79 3d 3d 30 20  t( p->pEntry==0 
110f5 29 3b 20 2f 2a 20 46 69 72 65 73 20 69 66 20 49  ); /* Fires if I
110f6 4e 53 45 52 54 20 61 66 74 65 72 20 53 4d 41 4c  NSERT after SMAL
110f7 4c 45 53 54 20 2a 2f 0a 20 20 20 20 70 2d 3e 70  LEST */.    p->p
110f8 45 6e 74 72 79 20 3d 20 70 45 6e 74 72 79 3b 0a  Entry = pEntry;.
110f9 20 20 7d 0a 20 20 70 2d 3e 70 4c 61 73 74 20 3d    }.  p->pLast =
110fa 20 70 45 6e 74 72 79 3b 0a 7d 0a 0a 2f 2a 0a 2a   pEntry;.}../*.*
110fb 2a 20 4d 65 72 67 65 20 74 77 6f 20 6c 69 73 74  * Merge two list
110fc 73 20 6f 66 20 52 6f 77 53 65 74 45 6e 74 72 79  s of RowSetEntry
110fd 20 6f 62 6a 65 63 74 73 2e 20 20 52 65 6d 6f 76   objects.  Remov
110fe 65 20 64 75 70 6c 69 63 61 74 65 73 2e 0a 2a 2a  e duplicates..**
110ff 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 6c 69  .** The input li
11100 73 74 73 20 61 72 65 20 63 6f 6e 6e 65 63 74 65  sts are connecte
11101 64 20 76 69 61 20 70 52 69 67 68 74 20 70 6f 69  d via pRight poi
11102 6e 74 65 72 73 20 61 6e 64 20 61 72 65 20 0a 2a  nters and are .*
11103 2a 20 61 73 73 75 6d 65 64 20 74 6f 20 65 61 63  * assumed to eac
11104 68 20 61 6c 72 65 61 64 79 20 62 65 20 69 6e 20  h already be in 
11105 73 6f 72 74 65 64 20 6f 72 64 65 72 2e 0a 2a 2f  sorted order..*/
11106 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 52  .static struct R
11107 6f 77 53 65 74 45 6e 74 72 79 20 2a 72 6f 77 53  owSetEntry *rowS
11108 65 74 4d 65 72 67 65 28 0a 20 20 73 74 72 75 63  etMerge(.  struc
11109 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70  t RowSetEntry *p
1110a 41 2c 20 20 20 20 2f 2a 20 46 69 72 73 74 20 73  A,    /* First s
1110b 6f 72 74 65 64 20 6c 69 73 74 20 74 6f 20 62 65  orted list to be
1110c 20 6d 65 72 67 65 64 20 2a 2f 0a 20 20 73 74 72   merged */.  str
1110d 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20  uct RowSetEntry 
1110e 2a 70 42 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e  *pB     /* Secon
1110f 64 20 73 6f 72 74 65 64 20 6c 69 73 74 20 74 6f  d sorted list to
11110 20 62 65 20 6d 65 72 67 65 64 20 2a 2f 0a 29 7b   be merged */.){
11111 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74  .  struct RowSet
11112 45 6e 74 72 79 20 68 65 61 64 3b 0a 20 20 73 74  Entry head;.  st
11113 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
11114 20 2a 70 54 61 69 6c 3b 0a 0a 20 20 70 54 61 69   *pTail;..  pTai
11115 6c 20 3d 20 26 68 65 61 64 3b 0a 20 20 77 68 69  l = &head;.  whi
11116 6c 65 28 20 70 41 20 26 26 20 70 42 20 29 7b 0a  le( pA && pB ){.
11117 20 20 20 20 61 73 73 65 72 74 28 20 70 41 2d 3e      assert( pA->
11118 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20 70 41 2d  pRight==0 || pA-
11119 3e 76 3c 3d 70 41 2d 3e 70 52 69 67 68 74 2d 3e  >v<=pA->pRight->
1111a 76 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  v );.    assert(
1111b 20 70 42 2d 3e 70 52 69 67 68 74 3d 3d 30 20 7c   pB->pRight==0 |
1111c 7c 20 70 42 2d 3e 76 3c 3d 70 42 2d 3e 70 52 69  | pB->v<=pB->pRi
1111d 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20 69 66  ght->v );.    if
1111e 28 20 70 41 2d 3e 76 3c 70 42 2d 3e 76 20 29 7b  ( pA->v<pB->v ){
1111f 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70 52  .      pTail->pR
11120 69 67 68 74 20 3d 20 70 41 3b 0a 20 20 20 20 20  ight = pA;.     
11121 20 70 41 20 3d 20 70 41 2d 3e 70 52 69 67 68 74   pA = pA->pRight
11122 3b 0a 20 20 20 20 20 20 70 54 61 69 6c 20 3d 20  ;.      pTail = 
11123 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 3b 0a 20  pTail->pRight;. 
11124 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 42 2d     }else if( pB-
11125 3e 76 3c 70 41 2d 3e 76 20 29 7b 0a 20 20 20 20  >v<pA->v ){.    
11126 20 20 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 20    pTail->pRight 
11127 3d 20 70 42 3b 0a 20 20 20 20 20 20 70 42 20 3d  = pB;.      pB =
11128 20 70 42 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20   pB->pRight;.   
11129 20 20 20 70 54 61 69 6c 20 3d 20 70 54 61 69 6c     pTail = pTail
1112a 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 7d 65  ->pRight;.    }e
1112b 6c 73 65 7b 0a 20 20 20 20 20 20 70 41 20 3d 20  lse{.      pA = 
1112c 70 41 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20  pA->pRight;.    
1112d 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 41 20 29  }.  }.  if( pA )
1112e 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 41  {.    assert( pA
1112f 2d 3e 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20 70  ->pRight==0 || p
11130 41 2d 3e 76 3c 3d 70 41 2d 3e 70 52 69 67 68 74  A->v<=pA->pRight
11131 2d 3e 76 20 29 3b 0a 20 20 20 20 70 54 61 69 6c  ->v );.    pTail
11132 2d 3e 70 52 69 67 68 74 20 3d 20 70 41 3b 0a 20  ->pRight = pA;. 
11133 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
11134 72 74 28 20 70 42 3d 3d 30 20 7c 7c 20 70 42 2d  rt( pB==0 || pB-
11135 3e 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20 70 42  >pRight==0 || pB
11136 2d 3e 76 3c 3d 70 42 2d 3e 70 52 69 67 68 74 2d  ->v<=pB->pRight-
11137 3e 76 20 29 3b 0a 20 20 20 20 70 54 61 69 6c 2d  >v );.    pTail-
11138 3e 70 52 69 67 68 74 20 3d 20 70 42 3b 0a 20 20  >pRight = pB;.  
11139 7d 0a 20 20 72 65 74 75 72 6e 20 68 65 61 64 2e  }.  return head.
1113a 70 52 69 67 68 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pRight;.}../*.**
1113b 20 53 6f 72 74 20 61 6c 6c 20 65 6c 65 6d 65 6e   Sort all elemen
1113c 74 73 20 6f 6e 20 74 68 65 20 70 45 6e 74 72 79  ts on the pEntry
1113d 20 6c 69 73 74 20 6f 66 20 74 68 65 20 52 6f 77   list of the Row
1113e 53 65 74 20 69 6e 74 6f 20 61 73 63 65 6e 64 69  Set into ascendi
1113f 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f 20 0a 73 74  ng order..*/ .st
11140 61 74 69 63 20 76 6f 69 64 20 72 6f 77 53 65 74  atic void rowSet
11141 53 6f 72 74 28 52 6f 77 53 65 74 20 2a 70 29 7b  Sort(RowSet *p){
11142 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  .  unsigned int 
11143 69 3b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53  i;.  struct RowS
11144 65 74 45 6e 74 72 79 20 2a 70 45 6e 74 72 79 3b  etEntry *pEntry;
11145 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74  .  struct RowSet
11146 45 6e 74 72 79 20 2a 61 42 75 63 6b 65 74 5b 34  Entry *aBucket[4
11147 30 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  0];..  assert( p
11148 2d 3e 69 73 53 6f 72 74 65 64 3d 3d 30 20 29 3b  ->isSorted==0 );
11149 0a 20 20 6d 65 6d 73 65 74 28 61 42 75 63 6b 65  .  memset(aBucke
1114a 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61 42 75  t, 0, sizeof(aBu
1114b 63 6b 65 74 29 29 3b 0a 20 20 77 68 69 6c 65 28  cket));.  while(
1114c 20 70 2d 3e 70 45 6e 74 72 79 20 29 7b 0a 20 20   p->pEntry ){.  
1114d 20 20 70 45 6e 74 72 79 20 3d 20 70 2d 3e 70 45    pEntry = p->pE
1114e 6e 74 72 79 3b 0a 20 20 20 20 70 2d 3e 70 45 6e  ntry;.    p->pEn
1114f 74 72 79 20 3d 20 70 45 6e 74 72 79 2d 3e 70 52  try = pEntry->pR
11150 69 67 68 74 3b 0a 20 20 20 20 70 45 6e 74 72 79  ight;.    pEntry
11151 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 20  ->pRight = 0;.  
11152 20 20 66 6f 72 28 69 3d 30 3b 20 61 42 75 63 6b    for(i=0; aBuck
11153 65 74 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20  et[i]; i++){.   
11154 20 20 20 70 45 6e 74 72 79 20 3d 20 72 6f 77 53     pEntry = rowS
11155 65 74 4d 65 72 67 65 28 61 42 75 63 6b 65 74 5b  etMerge(aBucket[
11156 69 5d 2c 20 70 45 6e 74 72 79 29 3b 0a 20 20 20  i], pEntry);.   
11157 20 20 20 61 42 75 63 6b 65 74 5b 69 5d 20 3d 20     aBucket[i] = 
11158 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 42 75  0;.    }.    aBu
11159 63 6b 65 74 5b 69 5d 20 3d 20 70 45 6e 74 72 79  cket[i] = pEntry
1115a 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 20 3d  ;.  }.  pEntry =
1115b 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   0;.  for(i=0; i
1115c 3c 73 69 7a 65 6f 66 28 61 42 75 63 6b 65 74 29  <sizeof(aBucket)
1115d 2f 73 69 7a 65 6f 66 28 61 42 75 63 6b 65 74 5b  /sizeof(aBucket[
1115e 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70  0]); i++){.    p
1115f 45 6e 74 72 79 20 3d 20 72 6f 77 53 65 74 4d 65  Entry = rowSetMe
11160 72 67 65 28 70 45 6e 74 72 79 2c 20 61 42 75 63  rge(pEntry, aBuc
11161 6b 65 74 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 70  ket[i]);.  }.  p
11162 2d 3e 70 45 6e 74 72 79 20 3d 20 70 45 6e 74 72  ->pEntry = pEntr
11163 79 3b 0a 20 20 70 2d 3e 70 4c 61 73 74 20 3d 20  y;.  p->pLast = 
11164 30 3b 0a 20 20 70 2d 3e 69 73 53 6f 72 74 65 64  0;.  p->isSorted
11165 20 3d 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20   = 1;.}.../*.** 
11166 54 68 65 20 69 6e 70 75 74 2c 20 70 49 6e 2c 20  The input, pIn, 
11167 69 73 20 61 20 62 69 6e 61 72 79 20 74 72 65 65  is a binary tree
11168 20 28 6f 72 20 73 75 62 74 72 65 65 29 20 6f 66   (or subtree) of
11169 20 52 6f 77 53 65 74 45 6e 74 72 79 20 6f 62 6a   RowSetEntry obj
1116a 65 63 74 73 2e 0a 2a 2a 20 43 6f 6e 76 65 72 74  ects..** Convert
1116b 20 74 68 69 73 20 74 72 65 65 20 69 6e 74 6f 20   this tree into 
1116c 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 63 6f  a linked list co
1116d 6e 6e 65 63 74 65 64 20 62 79 20 74 68 65 20 70  nnected by the p
1116e 52 69 67 68 74 20 70 6f 69 6e 74 65 72 73 0a 2a  Right pointers.*
1116f 2a 20 61 6e 64 20 72 65 74 75 72 6e 20 70 6f 69  * and return poi
11170 6e 74 65 72 73 20 74 6f 20 74 68 65 20 66 69 72  nters to the fir
11171 73 74 20 61 6e 64 20 6c 61 73 74 20 65 6c 65 6d  st and last elem
11172 65 6e 74 73 20 6f 66 20 74 68 65 20 6e 65 77 20  ents of the new 
11173 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  list..*/.static 
11174 76 6f 69 64 20 72 6f 77 53 65 74 54 72 65 65 54  void rowSetTreeT
11175 6f 4c 69 73 74 28 0a 20 20 73 74 72 75 63 74 20  oList(.  struct 
11176 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 49 6e  RowSetEntry *pIn
11177 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f  ,         /* Roo
11178 74 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 74  t of the input t
11179 72 65 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ree */.  struct 
1117a 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 2a 70 70  RowSetEntry **pp
1117b 46 69 72 73 74 2c 20 20 20 20 2f 2a 20 57 72 69  First,    /* Wri
1117c 74 65 20 68 65 61 64 20 6f 66 20 74 68 65 20 6f  te head of the o
1117d 75 74 70 75 74 20 6c 69 73 74 20 68 65 72 65 20  utput list here 
1117e 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53  */.  struct RowS
1117f 65 74 45 6e 74 72 79 20 2a 2a 70 70 4c 61 73 74  etEntry **ppLast
11180 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74        /* Write t
11181 61 69 6c 20 6f 66 20 74 68 65 20 6f 75 74 70 75  ail of the outpu
11182 74 20 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a 29  t list here */.)
11183 7b 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 21  {.  assert( pIn!
11184 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 49 6e 2d  =0 );.  if( pIn-
11185 3e 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 73 74  >pLeft ){.    st
11186 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
11187 20 2a 70 3b 0a 20 20 20 20 72 6f 77 53 65 74 54   *p;.    rowSetT
11188 72 65 65 54 6f 4c 69 73 74 28 70 49 6e 2d 3e 70  reeToList(pIn->p
11189 4c 65 66 74 2c 20 70 70 46 69 72 73 74 2c 20 26  Left, ppFirst, &
1118a 70 29 3b 0a 20 20 20 20 70 2d 3e 70 52 69 67 68  p);.    p->pRigh
1118b 74 20 3d 20 70 49 6e 3b 0a 20 20 7d 65 6c 73 65  t = pIn;.  }else
1118c 7b 0a 20 20 20 20 2a 70 70 46 69 72 73 74 20 3d  {.    *ppFirst =
1118d 20 70 49 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20   pIn;.  }.  if( 
1118e 70 49 6e 2d 3e 70 52 69 67 68 74 20 29 7b 0a 20  pIn->pRight ){. 
1118f 20 20 20 72 6f 77 53 65 74 54 72 65 65 54 6f 4c     rowSetTreeToL
11190 69 73 74 28 70 49 6e 2d 3e 70 52 69 67 68 74 2c  ist(pIn->pRight,
11191 20 26 70 49 6e 2d 3e 70 52 69 67 68 74 2c 20 70   &pIn->pRight, p
11192 70 4c 61 73 74 29 3b 0a 20 20 7d 65 6c 73 65 7b  pLast);.  }else{
11193 0a 20 20 20 20 2a 70 70 4c 61 73 74 20 3d 20 70  .    *ppLast = p
11194 49 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  In;.  }.  assert
11195 28 20 28 2a 70 70 4c 61 73 74 29 2d 3e 70 52 69  ( (*ppLast)->pRi
11196 67 68 74 3d 3d 30 20 29 3b 0a 7d 0a 0a 0a 2f 2a  ght==0 );.}.../*
11197 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 73 6f  .** Convert a so
11198 72 74 65 64 20 6c 69 73 74 20 6f 66 20 65 6c 65  rted list of ele
11199 6d 65 6e 74 73 20 28 63 6f 6e 6e 65 63 74 65 64  ments (connected
1119a 20 62 79 20 70 52 69 67 68 74 29 20 69 6e 74 6f   by pRight) into
1119b 20 61 20 62 69 6e 61 72 79 0a 2a 2a 20 74 72 65   a binary.** tre
1119c 65 20 77 69 74 68 20 64 65 70 74 68 20 6f 66 20  e with depth of 
1119d 69 44 65 70 74 68 2e 20 20 41 20 64 65 70 74 68  iDepth.  A depth
1119e 20 6f 66 20 31 20 6d 65 61 6e 73 20 74 68 65 20   of 1 means the 
1119f 74 72 65 65 20 63 6f 6e 74 61 69 6e 73 20 61 20  tree contains a 
111a0 73 69 6e 67 6c 65 0a 2a 2a 20 6e 6f 64 65 20 74  single.** node t
111a1 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20 68 65  aken from the he
111a2 61 64 20 6f 66 20 2a 70 70 4c 69 73 74 2e 20 20  ad of *ppList.  
111a3 41 20 64 65 70 74 68 20 6f 66 20 32 20 6d 65 61  A depth of 2 mea
111a4 6e 73 20 61 20 74 72 65 65 20 77 69 74 68 0a 2a  ns a tree with.*
111a5 2a 20 74 68 72 65 65 20 6e 6f 64 65 73 2e 20 20  * three nodes.  
111a6 41 6e 64 20 73 6f 20 66 6f 72 74 68 2e 0a 2a 2a  And so forth..**
111a7 0a 2a 2a 20 55 73 65 20 61 73 20 6d 61 6e 79 20  .** Use as many 
111a8 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 74 68 65  entries from the
111a9 20 69 6e 70 75 74 20 6c 69 73 74 20 61 73 20 72   input list as r
111aa 65 71 75 69 72 65 64 20 61 6e 64 20 75 70 64 61  equired and upda
111ab 74 65 20 74 68 65 0a 2a 2a 20 2a 70 70 4c 69 73  te the.** *ppLis
111ac 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68  t to point to th
111ad 65 20 75 6e 75 73 65 64 20 65 6c 65 6d 65 6e 74  e unused element
111ae 73 20 6f 66 20 74 68 65 20 6c 69 73 74 2e 20 20  s of the list.  
111af 49 66 20 74 68 65 20 69 6e 70 75 74 0a 2a 2a 20  If the input.** 
111b0 6c 69 73 74 20 63 6f 6e 74 61 69 6e 73 20 74 6f  list contains to
111b1 6f 20 66 65 77 20 65 6c 65 6d 65 6e 74 73 2c 20  o few elements, 
111b2 74 68 65 6e 20 63 6f 6e 73 74 72 75 63 74 20 61  then construct a
111b3 6e 20 69 6e 63 6f 6d 70 6c 65 74 65 20 74 72 65  n incomplete tre
111b4 65 0a 2a 2a 20 61 6e 64 20 6c 65 61 76 65 20 2a  e.** and leave *
111b5 70 70 4c 69 73 74 20 73 65 74 20 74 6f 20 4e 55  ppList set to NU
111b6 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  LL..**.** Return
111b7 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
111b8 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 63 6f  e root of the co
111b9 6e 73 74 72 75 63 74 65 64 20 62 69 6e 61 72 79  nstructed binary
111ba 20 74 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   tree..*/.static
111bb 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e   struct RowSetEn
111bc 74 72 79 20 2a 72 6f 77 53 65 74 4e 44 65 65 70  try *rowSetNDeep
111bd 54 72 65 65 28 0a 20 20 73 74 72 75 63 74 20 52  Tree(.  struct R
111be 6f 77 53 65 74 45 6e 74 72 79 20 2a 2a 70 70 4c  owSetEntry **ppL
111bf 69 73 74 2c 0a 20 20 69 6e 74 20 69 44 65 70 74  ist,.  int iDept
111c0 68 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 52 6f  h.){.  struct Ro
111c1 77 53 65 74 45 6e 74 72 79 20 2a 70 3b 20 20 20  wSetEntry *p;   
111c2 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 6f 66        /* Root of
111c3 20 74 68 65 20 6e 65 77 20 74 72 65 65 20 2a 2f   the new tree */
111c4 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74  .  struct RowSet
111c5 45 6e 74 72 79 20 2a 70 4c 65 66 74 3b 20 20 20  Entry *pLeft;   
111c6 20 20 2f 2a 20 4c 65 66 74 20 73 75 62 74 72 65    /* Left subtre
111c7 65 20 2a 2f 0a 20 20 69 66 28 20 2a 70 70 4c 69  e */.  if( *ppLi
111c8 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  st==0 ){.    ret
111c9 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28  urn 0;.  }.  if(
111ca 20 69 44 65 70 74 68 3d 3d 31 20 29 7b 0a 20 20   iDepth==1 ){.  
111cb 20 20 70 20 3d 20 2a 70 70 4c 69 73 74 3b 0a 20    p = *ppList;. 
111cc 20 20 20 2a 70 70 4c 69 73 74 20 3d 20 70 2d 3e     *ppList = p->
111cd 70 52 69 67 68 74 3b 0a 20 20 20 20 70 2d 3e 70  pRight;.    p->p
111ce 4c 65 66 74 20 3d 20 70 2d 3e 70 52 69 67 68 74  Left = p->pRight
111cf 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e   = 0;.    return
111d0 20 70 3b 0a 20 20 7d 0a 20 20 70 4c 65 66 74 20   p;.  }.  pLeft 
111d1 3d 20 72 6f 77 53 65 74 4e 44 65 65 70 54 72 65  = rowSetNDeepTre
111d2 65 28 70 70 4c 69 73 74 2c 20 69 44 65 70 74 68  e(ppList, iDepth
111d3 2d 31 29 3b 0a 20 20 70 20 3d 20 2a 70 70 4c 69  -1);.  p = *ppLi
111d4 73 74 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29  st;.  if( p==0 )
111d5 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 4c 65  {.    return pLe
111d6 66 74 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4c 65  ft;.  }.  p->pLe
111d7 66 74 20 3d 20 70 4c 65 66 74 3b 0a 20 20 2a 70  ft = pLeft;.  *p
111d8 70 4c 69 73 74 20 3d 20 70 2d 3e 70 52 69 67 68  pList = p->pRigh
111d9 74 3b 0a 20 20 70 2d 3e 70 52 69 67 68 74 20 3d  t;.  p->pRight =
111da 20 72 6f 77 53 65 74 4e 44 65 65 70 54 72 65 65   rowSetNDeepTree
111db 28 70 70 4c 69 73 74 2c 20 69 44 65 70 74 68 2d  (ppList, iDepth-
111dc 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a  1);.  return p;.
111dd 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74  }../*.** Convert
111de 20 61 20 73 6f 72 74 65 64 20 6c 69 73 74 20 6f   a sorted list o
111df 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 74 6f 20  f elements into 
111e0 61 20 62 69 6e 61 72 79 20 74 72 65 65 2e 20 4d  a binary tree. M
111e1 61 6b 65 20 74 68 65 20 74 72 65 65 0a 2a 2a 20  ake the tree.** 
111e2 61 73 20 64 65 65 70 20 61 73 20 69 74 20 6e 65  as deep as it ne
111e3 65 64 73 20 74 6f 20 62 65 20 69 6e 20 6f 72 64  eds to be in ord
111e4 65 72 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68  er to contain th
111e5 65 20 65 6e 74 69 72 65 20 6c 69 73 74 2e 0a 2a  e entire list..*
111e6 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20  /.static struct 
111e7 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 72 6f 77  RowSetEntry *row
111e8 53 65 74 4c 69 73 74 54 6f 54 72 65 65 28 73 74  SetListToTree(st
111e9 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
111ea 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20   *pList){.  int 
111eb 69 44 65 70 74 68 3b 20 20 20 20 20 20 20 20 20  iDepth;         
111ec 20 20 2f 2a 20 44 65 70 74 68 20 6f 66 20 74 68    /* Depth of th
111ed 65 20 74 72 65 65 20 73 6f 20 66 61 72 20 2a 2f  e tree so far */
111ee 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74  .  struct RowSet
111ef 45 6e 74 72 79 20 2a 70 3b 20 20 20 20 20 20 20  Entry *p;       
111f0 2f 2a 20 43 75 72 72 65 6e 74 20 74 72 65 65 20  /* Current tree 
111f1 72 6f 6f 74 20 2a 2f 0a 20 20 73 74 72 75 63 74  root */.  struct
111f2 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c   RowSetEntry *pL
111f3 65 66 74 3b 20 20 20 2f 2a 20 4c 65 66 74 20 73  eft;   /* Left s
111f4 75 62 74 72 65 65 20 2a 2f 0a 0a 20 20 61 73 73  ubtree */..  ass
111f5 65 72 74 28 20 70 4c 69 73 74 21 3d 30 20 29 3b  ert( pList!=0 );
111f6 0a 20 20 70 20 3d 20 70 4c 69 73 74 3b 0a 20 20  .  p = pList;.  
111f7 70 4c 69 73 74 20 3d 20 70 2d 3e 70 52 69 67 68  pList = p->pRigh
111f8 74 3b 0a 20 20 70 2d 3e 70 4c 65 66 74 20 3d 20  t;.  p->pLeft = 
111f9 70 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a 20  p->pRight = 0;. 
111fa 20 66 6f 72 28 69 44 65 70 74 68 3d 31 3b 20 70   for(iDepth=1; p
111fb 4c 69 73 74 3b 20 69 44 65 70 74 68 2b 2b 29 7b  List; iDepth++){
111fc 0a 20 20 20 20 70 4c 65 66 74 20 3d 20 70 3b 0a  .    pLeft = p;.
111fd 20 20 20 20 70 20 3d 20 70 4c 69 73 74 3b 0a 20      p = pList;. 
111fe 20 20 20 70 4c 69 73 74 20 3d 20 70 2d 3e 70 52     pList = p->pR
111ff 69 67 68 74 3b 0a 20 20 20 20 70 2d 3e 70 4c 65  ight;.    p->pLe
11200 66 74 20 3d 20 70 4c 65 66 74 3b 0a 20 20 20 20  ft = pLeft;.    
11201 70 2d 3e 70 52 69 67 68 74 20 3d 20 72 6f 77 53  p->pRight = rowS
11202 65 74 4e 44 65 65 70 54 72 65 65 28 26 70 4c 69  etNDeepTree(&pLi
11203 73 74 2c 20 69 44 65 70 74 68 29 3b 0a 20 20 7d  st, iDepth);.  }
11204 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
11205 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68  /*.** Convert th
11206 65 20 6c 69 73 74 20 69 6e 20 70 2d 3e 70 45 6e  e list in p->pEn
11207 74 72 79 20 69 6e 74 6f 20 61 20 73 6f 72 74 65  try into a sorte
11208 64 20 6c 69 73 74 20 69 66 20 69 74 20 69 73 20  d list if it is 
11209 6e 6f 74 0a 2a 2a 20 73 6f 72 74 65 64 20 61 6c  not.** sorted al
1120a 72 65 61 64 79 2e 20 20 49 66 20 74 68 65 72 65  ready.  If there
1120b 20 69 73 20 61 20 62 69 6e 61 72 79 20 74 72 65   is a binary tre
1120c 65 20 6f 6e 20 70 2d 3e 70 54 72 65 65 2c 20 74  e on p->pTree, t
1120d 68 65 6e 0a 2a 2a 20 63 6f 6e 76 65 72 74 20 69  hen.** convert i
1120e 74 20 69 6e 74 6f 20 61 20 6c 69 73 74 20 74 6f  t into a list to
1120f 6f 20 61 6e 64 20 6d 65 72 67 65 20 69 74 20 69  o and merge it i
11210 6e 74 6f 20 74 68 65 20 70 2d 3e 70 45 6e 74 72  nto the p->pEntr
11211 79 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69  y list..*/.stati
11212 63 20 76 6f 69 64 20 72 6f 77 53 65 74 54 6f 4c  c void rowSetToL
11213 69 73 74 28 52 6f 77 53 65 74 20 2a 70 29 7b 0a  ist(RowSet *p){.
11214 20 20 69 66 28 20 21 70 2d 3e 69 73 53 6f 72 74    if( !p->isSort
11215 65 64 20 29 7b 0a 20 20 20 20 72 6f 77 53 65 74  ed ){.    rowSet
11216 53 6f 72 74 28 70 29 3b 0a 20 20 7d 0a 20 20 69  Sort(p);.  }.  i
11217 66 28 20 70 2d 3e 70 54 72 65 65 20 29 7b 0a 20  f( p->pTree ){. 
11218 20 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74     struct RowSet
11219 45 6e 74 72 79 20 2a 70 48 65 61 64 2c 20 2a 70  Entry *pHead, *p
1121a 54 61 69 6c 3b 0a 20 20 20 20 72 6f 77 53 65 74  Tail;.    rowSet
1121b 54 72 65 65 54 6f 4c 69 73 74 28 70 2d 3e 70 54  TreeToList(p->pT
1121c 72 65 65 2c 20 26 70 48 65 61 64 2c 20 26 70 54  ree, &pHead, &pT
1121d 61 69 6c 29 3b 0a 20 20 20 20 70 2d 3e 70 54 72  ail);.    p->pTr
1121e 65 65 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 70  ee = 0;.    p->p
1121f 45 6e 74 72 79 20 3d 20 72 6f 77 53 65 74 4d 65  Entry = rowSetMe
11220 72 67 65 28 70 2d 3e 70 45 6e 74 72 79 2c 20 70  rge(p->pEntry, p
11221 48 65 61 64 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  Head);.  }.}../*
11222 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20  .** Extract the 
11223 73 6d 61 6c 6c 65 73 74 20 65 6c 65 6d 65 6e 74  smallest element
11224 20 66 72 6f 6d 20 74 68 65 20 52 6f 77 53 65 74   from the RowSet
11225 2e 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 65  ..** Write the e
11226 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 2a 70 52 6f  lement into *pRo
11227 77 69 64 2e 20 20 52 65 74 75 72 6e 20 31 20 6f  wid.  Return 1 o
11228 6e 20 73 75 63 63 65 73 73 2e 20 20 52 65 74 75  n success.  Retu
11229 72 6e 0a 2a 2a 20 30 20 69 66 20 74 68 65 20 52  rn.** 0 if the R
1122a 6f 77 53 65 74 20 69 73 20 61 6c 72 65 61 64 79  owSet is already
1122b 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a 20 41 66   empty..**.** Af
1122c 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ter this routine
1122d 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64   has been called
1122e 2c 20 74 68 65 20 73 71 6c 69 74 65 33 52 6f 77  , the sqlite3Row
1122f 53 65 74 49 6e 73 65 72 74 28 29 0a 2a 2a 20 72  SetInsert().** r
11230 6f 75 74 69 6e 65 20 6d 61 79 20 6e 6f 74 20 62  outine may not b
11231 65 20 63 61 6c 6c 65 64 20 61 67 61 69 6e 2e 20  e called again. 
11232 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56   .*/.SQLITE_PRIV
11233 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52  ATE int sqlite3R
11234 6f 77 53 65 74 4e 65 78 74 28 52 6f 77 53 65 74  owSetNext(RowSet
11235 20 2a 70 2c 20 69 36 34 20 2a 70 52 6f 77 69 64   *p, i64 *pRowid
11236 29 7b 0a 20 20 72 6f 77 53 65 74 54 6f 4c 69 73  ){.  rowSetToLis
11237 74 28 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70  t(p);.  if( p->p
11238 45 6e 74 72 79 20 29 7b 0a 20 20 20 20 2a 70 52  Entry ){.    *pR
11239 6f 77 69 64 20 3d 20 70 2d 3e 70 45 6e 74 72 79  owid = p->pEntry
1123a 2d 3e 76 3b 0a 20 20 20 20 70 2d 3e 70 45 6e 74  ->v;.    p->pEnt
1123b 72 79 20 3d 20 70 2d 3e 70 45 6e 74 72 79 2d 3e  ry = p->pEntry->
1123c 70 52 69 67 68 74 3b 0a 20 20 20 20 69 66 28 20  pRight;.    if( 
1123d 70 2d 3e 70 45 6e 74 72 79 3d 3d 30 20 29 7b 0a  p->pEntry==0 ){.
1123e 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77        sqlite3Row
1123f 53 65 74 43 6c 65 61 72 28 70 29 3b 0a 20 20 20  SetClear(p);.   
11240 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b   }.    return 1;
11241 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
11242 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f  turn 0;.  }.}../
11243 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65  *.** Check to se
11244 65 20 69 66 20 65 6c 65 6d 65 6e 74 20 69 52 6f  e if element iRo
11245 77 69 64 20 77 61 73 20 69 6e 73 65 72 74 65 64  wid was inserted
11246 20 69 6e 74 6f 20 74 68 65 20 74 68 65 20 72 6f   into the the ro
11247 77 73 65 74 20 61 73 0a 2a 2a 20 70 61 72 74 20  wset as.** part 
11248 6f 66 20 61 6e 79 20 69 6e 73 65 72 74 20 62 61  of any insert ba
11249 74 63 68 20 70 72 69 6f 72 20 74 6f 20 69 42 61  tch prior to iBa
1124a 74 63 68 2e 20 20 52 65 74 75 72 6e 20 31 20 6f  tch.  Return 1 o
1124b 72 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  r 0..*/.SQLITE_P
1124c 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1124d 65 33 52 6f 77 53 65 74 54 65 73 74 28 52 6f 77  e3RowSetTest(Row
1124e 53 65 74 20 2a 70 52 6f 77 53 65 74 2c 20 75 38  Set *pRowSet, u8
1124f 20 69 42 61 74 63 68 2c 20 73 71 6c 69 74 65 33   iBatch, sqlite3
11250 5f 69 6e 74 36 34 20 69 52 6f 77 69 64 29 7b 0a  _int64 iRowid){.
11251 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45    struct RowSetE
11252 6e 74 72 79 20 2a 70 3b 0a 20 20 69 66 28 20 69  ntry *p;.  if( i
11253 42 61 74 63 68 21 3d 70 52 6f 77 53 65 74 2d 3e  Batch!=pRowSet->
11254 69 42 61 74 63 68 20 29 7b 0a 20 20 20 20 69 66  iBatch ){.    if
11255 28 20 70 52 6f 77 53 65 74 2d 3e 70 45 6e 74 72  ( pRowSet->pEntr
11256 79 20 29 7b 0a 20 20 20 20 20 20 72 6f 77 53 65  y ){.      rowSe
11257 74 54 6f 4c 69 73 74 28 70 52 6f 77 53 65 74 29  tToList(pRowSet)
11258 3b 0a 20 20 20 20 20 20 70 52 6f 77 53 65 74 2d  ;.      pRowSet-
11259 3e 70 54 72 65 65 20 3d 20 72 6f 77 53 65 74 4c  >pTree = rowSetL
1125a 69 73 74 54 6f 54 72 65 65 28 70 52 6f 77 53 65  istToTree(pRowSe
1125b 74 2d 3e 70 45 6e 74 72 79 29 3b 0a 20 20 20 20  t->pEntry);.    
1125c 20 20 70 52 6f 77 53 65 74 2d 3e 70 45 6e 74 72    pRowSet->pEntr
1125d 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 52 6f  y = 0;.      pRo
1125e 77 53 65 74 2d 3e 70 4c 61 73 74 20 3d 20 30 3b  wSet->pLast = 0;
1125f 0a 20 20 20 20 7d 0a 20 20 20 20 70 52 6f 77 53  .    }.    pRowS
11260 65 74 2d 3e 69 42 61 74 63 68 20 3d 20 69 42 61  et->iBatch = iBa
11261 74 63 68 3b 0a 20 20 7d 0a 20 20 70 20 3d 20 70  tch;.  }.  p = p
11262 52 6f 77 53 65 74 2d 3e 70 54 72 65 65 3b 0a 20  RowSet->pTree;. 
11263 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20   while( p ){.   
11264 20 69 66 28 20 70 2d 3e 76 3c 69 52 6f 77 69 64   if( p->v<iRowid
11265 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 70 2d   ){.      p = p-
11266 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 7d 65 6c  >pRight;.    }el
11267 73 65 20 69 66 28 20 70 2d 3e 76 3e 69 52 6f 77  se if( p->v>iRow
11268 69 64 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20  id ){.      p = 
11269 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 7d 65  p->pLeft;.    }e
1126a 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72  lse{.      retur
1126b 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  n 1;.    }.  }. 
1126c 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
1126d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
1126e 64 20 6f 66 20 72 6f 77 73 65 74 2e 63 20 2a 2a  d of rowset.c **
1126f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11271 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
11272 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
11273 67 69 6e 20 66 69 6c 65 20 70 61 67 65 72 2e 63  gin file pager.c
11274 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
11275 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11276 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
11277 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
11278 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 15.**.** The 
11279 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
1127a 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
1127b 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
1127c 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
1127d 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
1127e 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
1127f 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
11280 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
11281 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
11282 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
11283 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
11284 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
11285 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
11286 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
11287 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
11288 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
11289 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
1128a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1128b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1128c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1128d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1128e 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  ****.** This is 
1128f 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  the implementati
11290 6f 6e 20 6f 66 20 74 68 65 20 70 61 67 65 20 63  on of the page c
11291 61 63 68 65 20 73 75 62 73 79 73 74 65 6d 20 6f  ache subsystem o
11292 72 20 22 70 61 67 65 72 22 2e 0a 2a 2a 20 0a 2a  r "pager"..** .*
11293 2a 20 54 68 65 20 70 61 67 65 72 20 69 73 20 75  * The pager is u
11294 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 61 20  sed to access a 
11295 64 61 74 61 62 61 73 65 20 64 69 73 6b 20 66 69  database disk fi
11296 6c 65 2e 20 20 49 74 20 69 6d 70 6c 65 6d 65 6e  le.  It implemen
11297 74 73 0a 2a 2a 20 61 74 6f 6d 69 63 20 63 6f 6d  ts.** atomic com
11298 6d 69 74 20 61 6e 64 20 72 6f 6c 6c 62 61 63 6b  mit and rollback
11299 20 74 68 72 6f 75 67 68 20 74 68 65 20 75 73 65   through the use
1129a 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69   of a journal fi
1129b 6c 65 20 74 68 61 74 0a 2a 2a 20 69 73 20 73 65  le that.** is se
1129c 70 61 72 61 74 65 20 66 72 6f 6d 20 74 68 65 20  parate from the 
1129d 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20  database file.  
1129e 54 68 65 20 70 61 67 65 72 20 61 6c 73 6f 20 69  The pager also i
1129f 6d 70 6c 65 6d 65 6e 74 73 20 66 69 6c 65 0a 2a  mplements file.*
112a0 2a 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 70 72 65  * locking to pre
112a1 76 65 6e 74 20 74 77 6f 20 70 72 6f 63 65 73 73  vent two process
112a2 65 73 20 66 72 6f 6d 20 77 72 69 74 69 6e 67 20  es from writing 
112a3 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
112a4 65 0a 2a 2a 20 66 69 6c 65 20 73 69 6d 75 6c 74  e.** file simult
112a5 61 6e 65 6f 75 73 6c 79 2c 20 6f 72 20 6f 6e 65  aneously, or one
112a6 20 70 72 6f 63 65 73 73 20 66 72 6f 6d 20 72 65   process from re
112a7 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 62 61  ading the databa
112a8 73 65 20 77 68 69 6c 65 0a 2a 2a 20 61 6e 6f 74  se while.** anot
112a9 68 65 72 20 69 73 20 77 72 69 74 69 6e 67 2e 0a  her is writing..
112aa 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20  **.** @(#) $Id: 
112ab 70 61 67 65 72 2e 63 2c 76 20 31 2e 36 32 39 20  pager.c,v 1.629 
112ac 32 30 30 39 2f 30 38 2f 31 30 20 31 37 3a 34 38  2009/08/10 17:48
112ad 3a 35 37 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :57 drh Exp $.*/
112ae 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
112af 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a 0a 2f 2a 0a  OMIT_DISKIO../*.
112b0 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 74 72  ** Macros for tr
112b1 6f 75 62 6c 65 73 68 6f 6f 74 69 6e 67 2e 20 20  oubleshooting.  
112b2 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64 20  Normally turned 
112b3 6f 66 66 0a 2a 2f 0a 23 69 66 20 30 0a 69 6e 74  off.*/.#if 0.int
112b4 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 61   sqlite3PagerTra
112b5 63 65 3d 31 3b 20 20 2f 2a 20 54 72 75 65 20 74  ce=1;  /* True t
112b6 6f 20 65 6e 61 62 6c 65 20 74 72 61 63 69 6e 67  o enable tracing
112b7 20 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c 69   */.#define sqli
112b8 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 20 70  te3DebugPrintf p
112b9 72 69 6e 74 66 0a 23 64 65 66 69 6e 65 20 50 41  rintf.#define PA
112ba 47 45 52 54 52 41 43 45 28 58 29 20 20 20 20 20  GERTRACE(X)     
112bb 69 66 28 20 73 71 6c 69 74 65 33 50 61 67 65 72  if( sqlite3Pager
112bc 54 72 61 63 65 20 29 7b 20 73 71 6c 69 74 65 33  Trace ){ sqlite3
112bd 44 65 62 75 67 50 72 69 6e 74 66 20 58 3b 20 7d  DebugPrintf X; }
112be 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 50  .#else.#define P
112bf 41 47 45 52 54 52 41 43 45 28 58 29 0a 23 65 6e  AGERTRACE(X).#en
112c0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  dif../*.** The f
112c1 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 6d 61 63  ollowing two mac
112c2 72 6f 73 20 61 72 65 20 75 73 65 64 20 77 69 74  ros are used wit
112c3 68 69 6e 20 74 68 65 20 50 41 47 45 52 54 52 41  hin the PAGERTRA
112c4 43 45 28 29 20 6d 61 63 72 6f 73 20 61 62 6f 76  CE() macros abov
112c5 65 0a 2a 2a 20 74 6f 20 70 72 69 6e 74 20 6f 75  e.** to print ou
112c6 74 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f  t file-descripto
112c7 72 73 2e 20 0a 2a 2a 0a 2a 2a 20 50 41 47 45 52  rs. .**.** PAGER
112c8 49 44 28 29 20 74 61 6b 65 73 20 61 20 70 6f 69  ID() takes a poi
112c9 6e 74 65 72 20 74 6f 20 61 20 50 61 67 65 72 20  nter to a Pager 
112ca 73 74 72 75 63 74 20 61 73 20 69 74 73 20 61 72  struct as its ar
112cb 67 75 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a 20 61  gument. The.** a
112cc 73 73 6f 63 69 61 74 65 64 20 66 69 6c 65 2d 64  ssociated file-d
112cd 65 73 63 72 69 70 74 6f 72 20 69 73 20 72 65 74  escriptor is ret
112ce 75 72 6e 65 64 2e 20 46 49 4c 45 48 41 4e 44 4c  urned. FILEHANDL
112cf 45 49 44 28 29 20 74 61 6b 65 73 20 61 6e 20 73  EID() takes an s
112d0 71 6c 69 74 65 33 5f 66 69 6c 65 0a 2a 2a 20 73  qlite3_file.** s
112d1 74 72 75 63 74 20 61 73 20 69 74 73 20 61 72 67  truct as its arg
112d2 75 6d 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ument..*/.#defin
112d3 65 20 50 41 47 45 52 49 44 28 70 29 20 28 28 69  e PAGERID(p) ((i
112d4 6e 74 29 28 70 2d 3e 66 64 29 29 0a 23 64 65 66  nt)(p->fd)).#def
112d5 69 6e 65 20 46 49 4c 45 48 41 4e 44 4c 45 49 44  ine FILEHANDLEID
112d6 28 66 64 29 20 28 28 69 6e 74 29 66 64 29 0a 0a  (fd) ((int)fd)..
112d7 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 63  /*.** The page c
112d8 61 63 68 65 20 61 73 20 61 20 77 68 6f 6c 65 20  ache as a whole 
112d9 69 73 20 61 6c 77 61 79 73 20 69 6e 20 6f 6e 65  is always in one
112da 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
112db 67 0a 2a 2a 20 73 74 61 74 65 73 3a 0a 2a 2a 0a  g.** states:.**.
112dc 2a 2a 20 20 20 50 41 47 45 52 5f 55 4e 4c 4f 43  **   PAGER_UNLOC
112dd 4b 20 20 20 20 20 20 20 20 54 68 65 20 70 61 67  K        The pag
112de 65 20 63 61 63 68 65 20 69 73 20 6e 6f 74 20 63  e cache is not c
112df 75 72 72 65 6e 74 6c 79 20 72 65 61 64 69 6e 67  urrently reading
112e0 20 6f 72 20 0a 2a 2a 20 20 20 20 20 20 20 20 20   or .**         
112e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77 72                wr
112e2 69 74 69 6e 67 20 74 68 65 20 64 61 74 61 62 61  iting the databa
112e3 73 65 20 66 69 6c 65 2e 20 20 54 68 65 72 65 20  se file.  There 
112e4 69 73 20 6e 6f 0a 2a 2a 20 20 20 20 20 20 20 20  is no.**        
112e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64                 d
112e6 61 74 61 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f  ata held in memo
112e7 72 79 2e 20 20 54 68 69 73 20 69 73 20 74 68 65  ry.  This is the
112e8 20 69 6e 69 74 69 61 6c 0a 2a 2a 20 20 20 20 20   initial.**     
112e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
112ea 20 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20    state..**.**  
112eb 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 20 20   PAGER_SHARED   
112ec 20 20 20 20 20 54 68 65 20 70 61 67 65 20 63 61       The page ca
112ed 63 68 65 20 69 73 20 72 65 61 64 69 6e 67 20 74  che is reading t
112ee 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20  he database..** 
112ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
112f0 20 20 20 20 20 20 57 72 69 74 69 6e 67 20 69 73        Writing is
112f1 20 6e 6f 74 20 70 65 72 6d 69 74 74 65 64 2e 20   not permitted. 
112f2 20 54 68 65 72 65 20 63 61 6e 20 62 65 0a 2a 2a   There can be.**
112f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
112f4 20 20 20 20 20 20 20 6d 75 6c 74 69 70 6c 65 20         multiple 
112f5 72 65 61 64 65 72 73 20 61 63 63 65 73 73 69 6e  readers accessin
112f6 67 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62  g the same datab
112f7 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ase.**          
112f8 20 20 20 20 20 20 20 20 20 20 20 20 20 66 69 6c               fil
112f9 65 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69  e at the same ti
112fa 6d 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45  me..**.**   PAGE
112fb 52 5f 52 45 53 45 52 56 45 44 20 20 20 20 20 20  R_RESERVED      
112fc 54 68 69 73 20 70 72 6f 63 65 73 73 20 68 61 73  This process has
112fd 20 72 65 73 65 72 76 65 64 20 74 68 65 20 64 61   reserved the da
112fe 74 61 62 61 73 65 20 66 6f 72 20 77 72 69 74 69  tabase for writi
112ff 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ng.**           
11300 20 20 20 20 20 20 20 20 20 20 20 20 62 75 74 20              but 
11301 68 61 73 20 6e 6f 74 20 79 65 74 20 6d 61 64 65  has not yet made
11302 20 61 6e 79 20 63 68 61 6e 67 65 73 2e 20 20 4f   any changes.  O
11303 6e 6c 79 20 6f 6e 65 20 70 72 6f 63 65 73 73 0a  nly one process.
11304 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
11305 20 20 20 20 20 20 20 20 20 61 74 20 61 20 74 69           at a ti
11306 6d 65 20 63 61 6e 20 72 65 73 65 72 76 65 20 74  me can reserve t
11307 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68  he database.  Th
11308 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 20 20  e original.**   
11309 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1130a 20 20 20 20 64 61 74 61 62 61 73 65 20 66 69 6c      database fil
1130b 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 6d  e has not been m
1130c 6f 64 69 66 69 65 64 20 73 6f 20 6f 74 68 65 72  odified so other
1130d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
1130e 20 20 20 20 20 20 20 20 20 20 70 72 6f 63 65 73            proces
1130f 73 65 73 20 6d 61 79 20 73 74 69 6c 6c 20 62 65  ses may still be
11310 20 72 65 61 64 69 6e 67 20 74 68 65 20 6f 6e 2d   reading the on-
11311 64 69 73 6b 0a 2a 2a 20 20 20 20 20 20 20 20 20  disk.**         
11312 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 61                da
11313 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a  tabase file..**.
11314 2a 2a 20 20 20 50 41 47 45 52 5f 45 58 43 4c 55  **   PAGER_EXCLU
11315 53 49 56 45 20 20 20 20 20 54 68 65 20 70 61 67  SIVE     The pag
11316 65 20 63 61 63 68 65 20 69 73 20 77 72 69 74 69  e cache is writi
11317 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  ng the database.
11318 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
11319 20 20 20 20 20 20 20 20 20 20 41 63 63 65 73 73            Access
1131a 20 69 73 20 65 78 63 6c 75 73 69 76 65 2e 20 20   is exclusive.  
1131b 4e 6f 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  No other process
1131c 65 73 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20  es or.**        
1131d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
1131e 68 72 65 61 64 73 20 63 61 6e 20 62 65 20 72 65  hreads can be re
1131f 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67  ading or writing
11320 20 77 68 69 6c 65 20 6f 6e 65 0a 2a 2a 20 20 20   while one.**   
11321 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11322 20 20 20 20 70 72 6f 63 65 73 73 20 69 73 20 77      process is w
11323 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20  riting..**.**   
11324 50 41 47 45 52 5f 53 59 4e 43 45 44 20 20 20 20  PAGER_SYNCED    
11325 20 20 20 20 54 68 65 20 70 61 67 65 72 20 6d 6f      The pager mo
11326 76 65 73 20 74 6f 20 74 68 69 73 20 73 74 61 74  ves to this stat
11327 65 20 66 72 6f 6d 20 50 41 47 45 52 5f 45 58 43  e from PAGER_EXC
11328 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 20 20 20  LUSIVE.**       
11329 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1132a 61 66 74 65 72 20 61 6c 6c 20 64 69 72 74 79 20  after all dirty 
1132b 70 61 67 65 73 20 68 61 76 65 20 62 65 65 6e 20  pages have been 
1132c 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 0a 2a  written to the.*
1132d 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
1132e 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65          database
1132f 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20 66 69   file and the fi
11330 6c 65 20 68 61 73 20 62 65 65 6e 20 73 79 6e 63  le has been sync
11331 65 64 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20  ed to.**        
11332 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64                 d
11333 69 73 6b 2e 20 41 6c 6c 20 74 68 61 74 20 72 65  isk. All that re
11334 6d 61 69 6e 73 20 74 6f 20 64 6f 20 69 73 20 74  mains to do is t
11335 6f 20 72 65 6d 6f 76 65 20 6f 72 0a 2a 2a 20 20  o remove or.**  
11336 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11337 20 20 20 20 20 74 72 75 6e 63 61 74 65 20 74 68       truncate th
11338 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61  e journal file a
11339 6e 64 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  nd the transacti
1133a 6f 6e 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  on .**          
1133b 20 20 20 20 20 20 20 20 20 20 20 20 20 77 69 6c               wil
1133c 6c 20 62 65 20 63 6f 6d 6d 69 74 74 65 64 2e 0a  l be committed..
1133d 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 63  **.** The page c
1133e 61 63 68 65 20 63 6f 6d 65 73 20 75 70 20 69 6e  ache comes up in
1133f 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 2e 20 20   PAGER_UNLOCK.  
11340 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 61  The first time a
11341 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72  .** sqlite3Pager
11342 47 65 74 28 29 20 6f 63 63 75 72 73 2c 20 74 68  Get() occurs, th
11343 65 20 73 74 61 74 65 20 74 72 61 6e 73 69 74 69  e state transiti
11344 6f 6e 73 20 74 6f 20 50 41 47 45 52 5f 53 48 41  ons to PAGER_SHA
11345 52 45 44 2e 0a 2a 2a 20 41 66 74 65 72 20 61 6c  RED..** After al
11346 6c 20 70 61 67 65 73 20 68 61 76 65 20 62 65 65  l pages have bee
11347 6e 20 72 65 6c 65 61 73 65 64 20 75 73 69 6e 67  n released using
11348 20 73 71 6c 69 74 65 5f 70 61 67 65 5f 75 6e 72   sqlite_page_unr
11349 65 66 28 29 2c 0a 2a 2a 20 74 68 65 20 73 74 61  ef(),.** the sta
1134a 74 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 62  te transitions b
1134b 61 63 6b 20 74 6f 20 50 41 47 45 52 5f 55 4e 4c  ack to PAGER_UNL
1134c 4f 43 4b 2e 20 20 54 68 65 20 66 69 72 73 74 20  OCK.  The first 
1134d 74 69 6d 65 0a 2a 2a 20 74 68 61 74 20 73 71 6c  time.** that sql
1134e 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29  ite3PagerWrite()
1134f 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 20   is called, the 
11350 73 74 61 74 65 20 74 72 61 6e 73 69 74 69 6f 6e  state transition
11351 73 20 74 6f 0a 2a 2a 20 50 41 47 45 52 5f 52 45  s to.** PAGER_RE
11352 53 45 52 56 45 44 2e 20 20 28 4e 6f 74 65 20 74  SERVED.  (Note t
11353 68 61 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  hat sqlite3Pager
11354 57 72 69 74 65 28 29 20 63 61 6e 20 6f 6e 6c 79  Write() can only
11355 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64 20 6f 6e   be.** called on
11356 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20   an outstanding 
11357 70 61 67 65 20 77 68 69 63 68 20 6d 65 61 6e 73  page which means
11358 20 74 68 61 74 20 74 68 65 20 70 61 67 65 72 20   that the pager 
11359 6d 75 73 74 0a 2a 2a 20 62 65 20 69 6e 20 50 41  must.** be in PA
1135a 47 45 52 5f 53 48 41 52 45 44 20 62 65 66 6f 72  GER_SHARED befor
1135b 65 20 69 74 20 74 72 61 6e 73 69 74 69 6f 6e 73  e it transitions
1135c 20 74 6f 20 50 41 47 45 52 5f 52 45 53 45 52 56   to PAGER_RESERV
1135d 45 44 2e 29 0a 2a 2a 20 50 41 47 45 52 5f 52 45  ED.).** PAGER_RE
1135e 53 45 52 56 45 44 20 6d 65 61 6e 73 20 74 68 61  SERVED means tha
1135f 74 20 74 68 65 72 65 20 69 73 20 61 6e 20 6f 70  t there is an op
11360 65 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72  en rollback jour
11361 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20 74 72 61 6e  nal..** The tran
11362 73 69 74 69 6f 6e 20 74 6f 20 50 41 47 45 52 5f  sition to PAGER_
11363 45 58 43 4c 55 53 49 56 45 20 6f 63 63 75 72 73  EXCLUSIVE occurs
11364 20 62 65 66 6f 72 65 20 61 6e 79 20 63 68 61 6e   before any chan
11365 67 65 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20  ges.** are made 
11366 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
11367 66 69 6c 65 2c 20 74 68 6f 75 67 68 20 77 72 69  file, though wri
11368 74 65 73 20 74 6f 20 74 68 65 20 72 6f 6c 6c 62  tes to the rollb
11369 61 63 6b 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 6f  ack.** journal o
1136a 63 63 75 72 73 20 77 69 74 68 20 6a 75 73 74 20  ccurs with just 
1136b 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 2e 20  PAGER_RESERVED. 
1136c 20 41 66 74 65 72 20 61 6e 20 73 71 6c 69 74 65   After an sqlite
1136d 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 29  3PagerRollback()
1136e 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 50 61  .** or sqlite3Pa
1136f 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77  gerCommitPhaseTw
11370 6f 28 29 2c 20 74 68 65 20 73 74 61 74 65 20 63  o(), the state c
11371 61 6e 20 67 6f 20 62 61 63 6b 20 74 6f 20 50 41  an go back to PA
11372 47 45 52 5f 53 48 41 52 45 44 2c 0a 2a 2a 20 6f  GER_SHARED,.** o
11373 72 20 69 74 20 63 61 6e 20 73 74 61 79 20 61 74  r it can stay at
11374 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45   PAGER_EXCLUSIVE
11375 20 69 66 20 77 65 20 61 72 65 20 69 6e 20 65 78   if we are in ex
11376 63 6c 75 73 69 76 65 20 61 63 63 65 73 73 20 6d  clusive access m
11377 6f 64 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ode..*/.#define 
11378 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 20 20 20  PAGER_UNLOCK    
11379 20 20 30 0a 23 64 65 66 69 6e 65 20 50 41 47 45    0.#define PAGE
1137a 52 5f 53 48 41 52 45 44 20 20 20 20 20 20 31 20  R_SHARED      1 
1137b 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 53 48 41    /* same as SHA
1137c 52 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 65 66  RED_LOCK */.#def
1137d 69 6e 65 20 50 41 47 45 52 5f 52 45 53 45 52 56  ine PAGER_RESERV
1137e 45 44 20 20 20 20 32 20 20 20 2f 2a 20 73 61 6d  ED    2   /* sam
1137f 65 20 61 73 20 52 45 53 45 52 56 45 44 5f 4c 4f  e as RESERVED_LO
11380 43 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41  CK */.#define PA
11381 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 20 20  GER_EXCLUSIVE   
11382 34 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 45  4   /* same as E
11383 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 2a 2f  XCLUSIVE_LOCK */
11384 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 53  .#define PAGER_S
11385 59 4e 43 45 44 20 20 20 20 20 20 35 0a 0a 2f 2a  YNCED      5../*
11386 0a 2a 2a 20 41 20 6d 61 63 72 6f 20 75 73 65 64  .** A macro used
11387 20 66 6f 72 20 69 6e 76 6f 6b 69 6e 67 20 74 68   for invoking th
11388 65 20 63 6f 64 65 63 20 69 66 20 74 68 65 72 65  e codec if there
11389 20 69 73 20 6f 6e 65 0a 2a 2f 0a 23 69 66 64 65   is one.*/.#ifde
1138a 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44  f SQLITE_HAS_COD
1138b 45 43 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 45  EC.# define CODE
1138c 43 31 28 50 2c 44 2c 4e 2c 58 2c 45 29 20 5c 0a  C1(P,D,N,X,E) \.
1138d 20 20 20 20 69 66 28 20 50 2d 3e 78 43 6f 64 65      if( P->xCode
1138e 63 20 26 26 20 50 2d 3e 78 43 6f 64 65 63 28 50  c && P->xCodec(P
1138f 2d 3e 70 43 6f 64 65 63 2c 44 2c 4e 2c 58 29 3d  ->pCodec,D,N,X)=
11390 3d 30 20 29 7b 20 45 3b 20 7d 0a 23 20 64 65 66  =0 ){ E; }.# def
11391 69 6e 65 20 43 4f 44 45 43 32 28 50 2c 44 2c 4e  ine CODEC2(P,D,N
11392 2c 58 2c 45 2c 4f 29 20 5c 0a 20 20 20 20 69 66  ,X,E,O) \.    if
11393 28 20 50 2d 3e 78 43 6f 64 65 63 3d 3d 30 20 29  ( P->xCodec==0 )
11394 7b 20 4f 3d 28 63 68 61 72 2a 29 44 3b 20 7d 65  { O=(char*)D; }e
11395 6c 73 65 20 5c 0a 20 20 20 20 69 66 28 20 28 4f  lse \.    if( (O
11396 3d 28 63 68 61 72 2a 29 28 50 2d 3e 78 43 6f 64  =(char*)(P->xCod
11397 65 63 28 50 2d 3e 70 43 6f 64 65 63 2c 44 2c 4e  ec(P->pCodec,D,N
11398 2c 58 29 29 29 3d 3d 30 20 29 7b 20 45 3b 20 7d  ,X)))==0 ){ E; }
11399 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
1139a 43 4f 44 45 43 31 28 50 2c 44 2c 4e 2c 58 2c 45  CODEC1(P,D,N,X,E
1139b 29 20 20 20 2f 2a 20 4e 4f 2d 4f 50 20 2a 2f 0a  )   /* NO-OP */.
1139c 23 20 64 65 66 69 6e 65 20 43 4f 44 45 43 32 28  # define CODEC2(
1139d 50 2c 44 2c 4e 2c 58 2c 45 2c 4f 29 20 4f 3d 28  P,D,N,X,E,O) O=(
1139e 63 68 61 72 2a 29 44 0a 23 65 6e 64 69 66 0a 0a  char*)D.#endif..
1139f 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75  /*.** The maximu
113a0 6d 20 61 6c 6c 6f 77 65 64 20 73 65 63 74 6f 72  m allowed sector
113a1 20 73 69 7a 65 2e 20 36 34 4b 69 42 2e 20 49 66   size. 64KiB. If
113a2 20 74 68 65 20 78 53 65 63 74 6f 72 73 69 7a 65   the xSectorsize
113a3 28 29 20 6d 65 74 68 6f 64 20 0a 2a 2a 20 72 65  () method .** re
113a4 74 75 72 6e 73 20 61 20 76 61 6c 75 65 20 6c 61  turns a value la
113a5 72 67 65 72 20 74 68 61 6e 20 74 68 69 73 2c 20  rger than this, 
113a6 74 68 65 6e 20 4d 41 58 5f 53 45 43 54 4f 52 5f  then MAX_SECTOR_
113a7 53 49 5a 45 20 69 73 20 75 73 65 64 20 69 6e 73  SIZE is used ins
113a8 74 65 61 64 2e 0a 2a 2a 20 54 68 69 73 20 63 6f  tead..** This co
113a9 75 6c 64 20 63 6f 6e 63 65 69 76 61 62 6c 79 20  uld conceivably 
113aa 63 61 75 73 65 20 63 6f 72 72 75 70 74 69 6f 6e  cause corruption
113ab 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 70 6f 77   following a pow
113ac 65 72 20 66 61 69 6c 75 72 65 20 6f 6e 0a 2a 2a  er failure on.**
113ad 20 73 75 63 68 20 61 20 73 79 73 74 65 6d 2e 20   such a system. 
113ae 54 68 69 73 20 69 73 20 63 75 72 72 65 6e 74 6c  This is currentl
113af 79 20 61 6e 20 75 6e 64 6f 63 75 6d 65 6e 74 65  y an undocumente
113b0 64 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 23 64 65 66  d limit..*/.#def
113b1 69 6e 65 20 4d 41 58 5f 53 45 43 54 4f 52 5f 53  ine MAX_SECTOR_S
113b2 49 5a 45 20 30 78 31 30 30 30 30 0a 0a 2f 2a 0a  IZE 0x10000../*.
113b3 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
113b4 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
113b5 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c  structure is all
113b6 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63 68 20  ocated for each 
113b7 61 63 74 69 76 65 0a 2a 2a 20 73 61 76 65 70 6f  active.** savepo
113b8 69 6e 74 20 61 6e 64 20 73 74 61 74 65 6d 65 6e  int and statemen
113b9 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e  t transaction in
113ba 20 74 68 65 20 73 79 73 74 65 6d 2e 20 41 6c 6c   the system. All
113bb 20 73 75 63 68 20 73 74 72 75 63 74 75 72 65 73   such structures
113bc 0a 2a 2a 20 61 72 65 20 73 74 6f 72 65 64 20 69  .** are stored i
113bd 6e 20 74 68 65 20 50 61 67 65 72 2e 61 53 61 76  n the Pager.aSav
113be 65 70 6f 69 6e 74 5b 5d 20 61 72 72 61 79 2c 20  epoint[] array, 
113bf 77 68 69 63 68 20 69 73 20 61 6c 6c 6f 63 61 74  which is allocat
113c0 65 64 20 61 6e 64 0a 2a 2a 20 72 65 73 69 7a 65  ed and.** resize
113c1 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 52  d using sqlite3R
113c2 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20  ealloc()..**.** 
113c3 57 68 65 6e 20 61 20 73 61 76 65 70 6f 69 6e 74  When a savepoint
113c4 20 69 73 20 63 72 65 61 74 65 64 2c 20 74 68 65   is created, the
113c5 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e   PagerSavepoint.
113c6 69 48 64 72 4f 66 66 73 65 74 20 66 69 65 6c 64  iHdrOffset field
113c7 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 30 2e   is.** set to 0.
113c8 20 49 66 20 61 20 6a 6f 75 72 6e 61 6c 2d 68 65   If a journal-he
113c9 61 64 65 72 20 69 73 20 77 72 69 74 74 65 6e 20  ader is written 
113ca 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20 6a 6f  into the main jo
113cb 75 72 6e 61 6c 20 77 68 69 6c 65 0a 2a 2a 20 74  urnal while.** t
113cc 68 65 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20  he savepoint is 
113cd 61 63 74 69 76 65 2c 20 74 68 65 6e 20 69 48 64  active, then iHd
113ce 72 4f 66 66 73 65 74 20 69 73 20 73 65 74 20 74  rOffset is set t
113cf 6f 20 74 68 65 20 62 79 74 65 20 6f 66 66 73 65  o the byte offse
113d0 74 20 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c  t .** immediatel
113d1 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20  y following the 
113d2 6c 61 73 74 20 6a 6f 75 72 6e 61 6c 20 72 65 63  last journal rec
113d3 6f 72 64 20 77 72 69 74 74 65 6e 20 69 6e 74 6f  ord written into
113d4 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20 6a 6f 75   the main.** jou
113d5 72 6e 61 6c 20 62 65 66 6f 72 65 20 74 68 65 20  rnal before the 
113d6 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 2e 20  journal-header. 
113d7 54 68 69 73 20 69 73 20 72 65 71 75 69 72 65 64  This is required
113d8 20 64 75 72 69 6e 67 20 73 61 76 65 70 6f 69 6e   during savepoin
113d9 74 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 28 73  t.** rollback (s
113da 65 65 20 70 61 67 65 72 50 6c 61 79 62 61 63 6b  ee pagerPlayback
113db 53 61 76 65 70 6f 69 6e 74 28 29 29 2e 0a 2a 2f  Savepoint())..*/
113dc 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
113dd 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 50  PagerSavepoint P
113de 61 67 65 72 53 61 76 65 70 6f 69 6e 74 3b 0a 73  agerSavepoint;.s
113df 74 72 75 63 74 20 50 61 67 65 72 53 61 76 65 70  truct PagerSavep
113e0 6f 69 6e 74 20 7b 0a 20 20 69 36 34 20 69 4f 66  oint {.  i64 iOf
113e1 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20  fset;           
113e2 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74 69 6e        /* Startin
113e3 67 20 6f 66 66 73 65 74 20 69 6e 20 6d 61 69 6e  g offset in main
113e4 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36   journal */.  i6
113e5 34 20 69 48 64 72 4f 66 66 73 65 74 3b 20 20 20  4 iHdrOffset;   
113e6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65             /* Se
113e7 65 20 61 62 6f 76 65 20 2a 2f 0a 20 20 42 69 74  e above */.  Bit
113e8 76 65 63 20 2a 70 49 6e 53 61 76 65 70 6f 69 6e  vec *pInSavepoin
113e9 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74  t;        /* Set
113ea 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 69   of pages in thi
113eb 73 20 73 61 76 65 70 6f 69 6e 74 20 2a 2f 0a 20  s savepoint */. 
113ec 20 50 67 6e 6f 20 6e 4f 72 69 67 3b 20 20 20 20   Pgno nOrig;    
113ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
113ee 20 4f 72 69 67 69 6e 61 6c 20 6e 75 6d 62 65 72   Original number
113ef 20 6f 66 20 70 61 67 65 73 20 69 6e 20 66 69 6c   of pages in fil
113f0 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 53 75 62  e */.  Pgno iSub
113f1 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  Rec;            
113f2 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
113f3 66 69 72 73 74 20 72 65 63 6f 72 64 20 69 6e 20  first record in 
113f4 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 7d  sub-journal */.}
113f5 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 6f 70 65 6e 20  ;../*.** A open 
113f6 70 61 67 65 20 63 61 63 68 65 20 69 73 20 61 6e  page cache is an
113f7 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
113f8 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
113f9 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 65 72 72 43  ture..**.** errC
113fa 6f 64 65 0a 2a 2a 0a 2a 2a 20 20 20 50 61 67 65  ode.**.**   Page
113fb 72 2e 65 72 72 43 6f 64 65 20 6d 61 79 20 62 65  r.errCode may be
113fc 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 49   set to SQLITE_I
113fd 4f 45 52 52 2c 20 53 51 4c 49 54 45 5f 43 4f 52  OERR, SQLITE_COR
113fe 52 55 50 54 2c 20 6f 72 0a 2a 2a 20 20 20 6f 72  RUPT, or.**   or
113ff 20 53 51 4c 49 54 45 5f 46 55 4c 4c 2e 20 4f 6e   SQLITE_FULL. On
11400 63 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 69  ce one of the fi
11401 72 73 74 20 74 68 72 65 65 20 65 72 72 6f 72 73  rst three errors
11402 20 6f 63 63 75 72 73 2c 20 69 74 20 70 65 72 73   occurs, it pers
11403 69 73 74 73 0a 2a 2a 20 20 20 61 6e 64 20 69 73  ists.**   and is
11404 20 72 65 74 75 72 6e 65 64 20 61 73 20 74 68 65   returned as the
11405 20 72 65 73 75 6c 74 20 6f 66 20 65 76 65 72 79   result of every
11406 20 6d 61 6a 6f 72 20 70 61 67 65 72 20 41 50 49   major pager API
11407 20 63 61 6c 6c 2e 20 20 54 68 65 0a 2a 2a 20 20   call.  The.**  
11408 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 72 65 74   SQLITE_FULL ret
11409 75 72 6e 20 63 6f 64 65 20 69 73 20 73 6c 69 67  urn code is slig
1140a 68 74 6c 79 20 64 69 66 66 65 72 65 6e 74 2e 20  htly different. 
1140b 49 74 20 70 65 72 73 69 73 74 73 20 6f 6e 6c 79  It persists only
1140c 20 75 6e 74 69 6c 20 74 68 65 0a 2a 2a 20 20 20   until the.**   
1140d 6e 65 78 74 20 73 75 63 63 65 73 73 66 75 6c 20  next successful 
1140e 72 6f 6c 6c 62 61 63 6b 20 69 73 20 70 65 72 66  rollback is perf
1140f 6f 72 6d 65 64 20 6f 6e 20 74 68 65 20 70 61 67  ormed on the pag
11410 65 72 20 63 61 63 68 65 2e 20 41 6c 73 6f 2c 0a  er cache. Also,.
11411 2a 2a 20 20 20 53 51 4c 49 54 45 5f 46 55 4c 4c  **   SQLITE_FULL
11412 20 64 6f 65 73 20 6e 6f 74 20 61 66 66 65 63 74   does not affect
11413 20 74 68 65 20 73 71 6c 69 74 65 33 50 61 67 65   the sqlite3Page
11414 72 47 65 74 28 29 20 61 6e 64 20 73 71 6c 69 74  rGet() and sqlit
11415 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 29 0a  e3PagerLookup().
11416 2a 2a 20 20 20 41 50 49 73 2c 20 74 68 65 79 20  **   APIs, they 
11417 6d 61 79 20 73 74 69 6c 6c 20 62 65 20 75 73 65  may still be use
11418 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 0a  d successfully..
11419 2a 2a 0a 2a 2a 20 64 62 53 69 7a 65 56 61 6c 69  **.** dbSizeVali
1141a 64 2c 20 64 62 53 69 7a 65 2c 20 64 62 4f 72 69  d, dbSize, dbOri
1141b 67 53 69 7a 65 2c 20 64 62 46 69 6c 65 53 69 7a  gSize, dbFileSiz
1141c 65 0a 2a 2a 0a 2a 2a 20 20 20 4d 61 6e 61 67 69  e.**.**   Managi
1141d 6e 67 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  ng the size of t
1141e 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1141f 20 69 6e 20 70 61 67 65 73 20 69 73 20 61 20 6c   in pages is a l
11420 69 74 74 6c 65 20 63 6f 6d 70 6c 69 63 61 74 65  ittle complicate
11421 64 2e 0a 2a 2a 20 20 20 54 68 65 20 76 61 72 69  d..**   The vari
11422 61 62 6c 65 20 50 61 67 65 72 2e 64 62 53 69 7a  able Pager.dbSiz
11423 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e  e contains the n
11424 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 74  umber of pages t
11425 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65  hat the database
11426 0a 2a 2a 20 20 20 69 6d 61 67 65 20 63 75 72 72  .**   image curr
11427 65 6e 74 6c 79 20 63 6f 6e 74 61 69 6e 73 2e 20  ently contains. 
11428 41 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  As the database 
11429 69 6d 61 67 65 20 67 72 6f 77 73 20 6f 72 20 73  image grows or s
1142a 68 72 69 6e 6b 73 20 74 68 69 73 0a 2a 2a 20 20  hrinks this.**  
1142b 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 70 64   variable is upd
1142c 61 74 65 64 2e 20 54 68 65 20 76 61 72 69 61 62  ated. The variab
1142d 6c 65 20 50 61 67 65 72 2e 64 62 46 69 6c 65 53  le Pager.dbFileS
1142e 69 7a 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ize contains the
1142f 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 6f 66 20   number.**   of 
11430 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74  pages in the dat
11431 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 69 73  abase file. This
11432 20 6d 61 79 20 62 65 20 64 69 66 66 65 72 65 6e   may be differen
11433 74 20 66 72 6f 6d 20 50 61 67 65 72 2e 64 62 53  t from Pager.dbS
11434 69 7a 65 0a 2a 2a 20 20 20 69 66 20 73 6f 6d 65  ize.**   if some
11435 20 70 61 67 65 73 20 68 61 76 65 20 62 65 65 6e   pages have been
11436 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65   appended to the
11437 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 20   database image 
11438 62 75 74 20 6e 6f 74 20 79 65 74 20 77 72 69 74  but not yet writ
11439 74 65 6e 0a 2a 2a 20 20 20 6f 75 74 20 66 72 6f  ten.**   out fro
1143a 6d 20 74 68 65 20 63 61 63 68 65 20 74 6f 20 74  m the cache to t
1143b 68 65 20 61 63 74 75 61 6c 20 66 69 6c 65 20 6f  he actual file o
1143c 6e 20 64 69 73 6b 2e 20 4f 72 20 69 66 20 74 68  n disk. Or if th
1143d 65 20 69 6d 61 67 65 20 68 61 73 20 62 65 65 6e  e image has been
1143e 0a 2a 2a 20 20 20 74 72 75 6e 63 61 74 65 64 20  .**   truncated 
1143f 62 79 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61  by an incrementa
11440 6c 2d 76 61 63 75 75 6d 20 6f 70 65 72 61 74 69  l-vacuum operati
11441 6f 6e 2e 20 54 68 65 20 50 61 67 65 72 2e 64 62  on. The Pager.db
11442 4f 72 69 67 53 69 7a 65 20 76 61 72 69 61 62 6c  OrigSize variabl
11443 65 0a 2a 2a 20 20 20 63 6f 6e 74 61 69 6e 73 20  e.**   contains 
11444 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  the number of pa
11445 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62  ges in the datab
11446 61 73 65 20 69 6d 61 67 65 20 77 68 65 6e 20 74  ase image when t
11447 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 20 20  he current.**   
11448 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20  transaction was 
11449 6f 70 65 6e 65 64 2e 20 54 68 65 20 63 6f 6e 74  opened. The cont
1144a 65 6e 74 73 20 6f 66 20 61 6c 6c 20 74 68 72 65  ents of all thre
1144b 65 20 6f 66 20 74 68 65 73 65 20 76 61 72 69 61  e of these varia
1144c 62 6c 65 73 20 69 73 0a 2a 2a 20 20 20 6f 6e 6c  bles is.**   onl
1144d 79 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20  y guaranteed to 
1144e 62 65 20 63 6f 72 72 65 63 74 20 69 66 20 74 68  be correct if th
1144f 65 20 62 6f 6f 6c 65 61 6e 20 50 61 67 65 72 2e  e boolean Pager.
11450 64 62 53 69 7a 65 56 61 6c 69 64 20 69 73 20 74  dbSizeValid is t
11451 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 4f 44  rue..**.**   TOD
11452 4f 3a 20 55 6e 64 65 72 20 77 68 61 74 20 63 6f  O: Under what co
11453 6e 64 69 74 69 6f 6e 73 20 69 73 20 64 62 53 69  nditions is dbSi
11454 7a 65 56 61 6c 69 64 20 73 65 74 3f 20 43 6c 65  zeValid set? Cle
11455 61 72 65 64 3f 0a 2a 2a 0a 2a 2a 20 63 68 61 6e  ared?.**.** chan
11456 67 65 43 6f 75 6e 74 44 6f 6e 65 0a 2a 2a 0a 2a  geCountDone.**.*
11457 2a 20 20 20 54 68 69 73 20 62 6f 6f 6c 65 61 6e  *   This boolean
11458 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 73 65   variable is use
11459 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  d to make sure t
1145a 68 61 74 20 74 68 65 20 63 68 61 6e 67 65 2d 63  hat the change-c
1145b 6f 75 6e 74 65 72 20 0a 2a 2a 20 20 20 28 74 68  ounter .**   (th
1145c 65 20 34 2d 62 79 74 65 20 68 65 61 64 65 72 20  e 4-byte header 
1145d 66 69 65 6c 64 20 61 74 20 62 79 74 65 20 6f 66  field at byte of
1145e 66 73 65 74 20 32 34 20 6f 66 20 74 68 65 20 64  fset 24 of the d
1145f 61 74 61 62 61 73 65 20 66 69 6c 65 29 20 69 73  atabase file) is
11460 20 0a 2a 2a 20 20 20 6e 6f 74 20 75 70 64 61 74   .**   not updat
11461 65 64 20 6d 6f 72 65 20 6f 66 74 65 6e 20 74 68  ed more often th
11462 61 6e 20 6e 65 63 65 73 73 61 72 79 2e 20 0a 2a  an necessary. .*
11463 2a 0a 2a 2a 20 20 20 49 74 20 69 73 20 73 65 74  *.**   It is set
11464 20 74 6f 20 74 72 75 65 20 77 68 65 6e 20 74 68   to true when th
11465 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72  e change-counter
11466 20 66 69 65 6c 64 20 69 73 20 75 70 64 61 74 65   field is update
11467 64 2c 20 77 68 69 63 68 20 0a 2a 2a 20 20 20 63  d, which .**   c
11468 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 69  an only happen i
11469 66 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c  f an exclusive l
1146a 6f 63 6b 20 69 73 20 68 65 6c 64 20 6f 6e 20 74  ock is held on t
1146b 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1146c 2e 0a 2a 2a 20 20 20 49 74 20 69 73 20 63 6c 65  ..**   It is cle
1146d 61 72 65 64 20 28 73 65 74 20 74 6f 20 66 61 6c  ared (set to fal
1146e 73 65 29 20 77 68 65 6e 65 76 65 72 20 61 6e 20  se) whenever an 
1146f 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69  exclusive lock i
11470 73 20 0a 2a 2a 20 20 20 72 65 6c 69 6e 71 75 69  s .**   relinqui
11471 73 68 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61  shed on the data
11472 62 61 73 65 20 66 69 6c 65 2e 20 45 61 63 68 20  base file. Each 
11473 74 69 6d 65 20 61 20 74 72 61 6e 73 61 63 74 69  time a transacti
11474 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 2c  on is committed,
11475 0a 2a 2a 20 20 20 54 68 65 20 63 68 61 6e 67 65  .**   The change
11476 43 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67 20 69  CountDone flag i
11477 73 20 69 6e 73 70 65 63 74 65 64 2e 20 49 66 20  s inspected. If 
11478 69 74 20 69 73 20 74 72 75 65 2c 20 74 68 65 20  it is true, the 
11479 77 6f 72 6b 20 6f 66 0a 2a 2a 20 20 20 75 70 64  work of.**   upd
1147a 61 74 69 6e 67 20 74 68 65 20 63 68 61 6e 67 65  ating the change
1147b 2d 63 6f 75 6e 74 65 72 20 69 73 20 6f 6d 69 74  -counter is omit
1147c 74 65 64 20 66 6f 72 20 74 68 65 20 63 75 72 72  ted for the curr
1147d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  ent transaction.
1147e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 6d 65  .**.**   This me
1147f 63 68 61 6e 69 73 6d 20 6d 65 61 6e 73 20 74 68  chanism means th
11480 61 74 20 77 68 65 6e 20 72 75 6e 6e 69 6e 67 20  at when running 
11481 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64  in exclusive mod
11482 65 2c 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  e, a connection 
11483 0a 2a 2a 20 20 20 6e 65 65 64 20 6f 6e 6c 79 20  .**   need only 
11484 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67  update the chang
11485 65 2d 63 6f 75 6e 74 65 72 20 6f 6e 63 65 2c 20  e-counter once, 
11486 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 74 72  for the first tr
11487 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 63  ansaction.**   c
11488 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  ommitted..**.** 
11489 64 62 4d 6f 64 69 66 69 65 64 0a 2a 2a 0a 2a 2a  dbModified.**.**
1148a 20 20 20 54 68 65 20 64 62 4d 6f 64 69 66 69 65     The dbModifie
1148b 64 20 66 6c 61 67 20 69 73 20 73 65 74 20 77 68  d flag is set wh
1148c 65 6e 65 76 65 72 20 61 20 64 61 74 61 62 61 73  enever a databas
1148d 65 20 70 61 67 65 20 69 73 20 64 69 72 74 69 65  e page is dirtie
1148e 64 2e 0a 2a 2a 20 20 20 49 74 20 69 73 20 63 6c  d..**   It is cl
1148f 65 61 72 65 64 20 61 74 20 74 68 65 20 65 6e 64  eared at the end
11490 20 6f 66 20 65 61 63 68 20 74 72 61 6e 73 61 63   of each transac
11491 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 49 74  tion..**.**   It
11492 20 69 73 20 75 73 65 64 20 77 68 65 6e 20 63 6f   is used when co
11493 6d 6d 69 74 74 69 6e 67 20 6f 72 20 6f 74 68 65  mmitting or othe
11494 72 77 69 73 65 20 65 6e 64 69 6e 67 20 61 20 74  rwise ending a t
11495 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66 0a 2a  ransaction. If.*
11496 2a 20 20 20 74 68 65 20 64 62 4d 6f 64 69 66 69  *   the dbModifi
11497 65 64 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72  ed flag is clear
11498 20 74 68 65 6e 20 6c 65 73 73 20 77 6f 72 6b 20   then less work 
11499 68 61 73 20 74 6f 20 62 65 20 64 6f 6e 65 2e 0a  has to be done..
1149a 2a 2a 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 53 74 61  **.** journalSta
1149b 72 74 65 64 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69  rted.**.**   Thi
1149c 73 20 66 6c 61 67 20 69 73 20 73 65 74 20 77 68  s flag is set wh
1149d 65 6e 65 76 65 72 20 74 68 65 20 74 68 65 20 6d  enever the the m
1149e 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73  ain journal is s
1149f 79 6e 63 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 20 20  ynced. .**.**   
114a0 54 68 65 20 70 6f 69 6e 74 20 6f 66 20 74 68 69  The point of thi
114a1 73 20 66 6c 61 67 20 69 73 20 74 68 61 74 20 69  s flag is that i
114a2 74 20 6d 75 73 74 20 62 65 20 73 65 74 20 61 66  t must be set af
114a3 74 65 72 20 74 68 65 20 0a 2a 2a 20 20 20 66 69  ter the .**   fi
114a4 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  rst journal head
114a5 65 72 20 69 6e 20 61 20 6a 6f 75 72 6e 61 6c 20  er in a journal 
114a6 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 73 79  file has been sy
114a7 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a  nced to disk..**
114a8 20 20 20 41 66 74 65 72 20 74 68 69 73 20 68 61     After this ha
114a9 73 20 68 61 70 70 65 6e 65 64 2c 20 6e 65 77 20  s happened, new 
114aa 70 61 67 65 73 20 61 70 70 65 6e 64 65 64 20 74  pages appended t
114ab 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 0a  o the database .
114ac 2a 2a 20 20 20 64 6f 20 6e 6f 74 20 6e 65 65 64  **   do not need
114ad 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f   the PGHDR_NEED_
114ae 53 59 4e 43 20 66 6c 61 67 20 73 65 74 2c 20 61  SYNC flag set, a
114af 73 20 74 68 65 79 20 64 6f 20 6e 6f 74 20 6e 65  s they do not ne
114b0 65 64 0a 2a 2a 20 20 20 74 6f 20 77 61 69 74 20  ed.**   to wait 
114b1 66 6f 72 20 61 20 6a 6f 75 72 6e 61 6c 20 73 79  for a journal sy
114b2 6e 63 20 62 65 66 6f 72 65 20 74 68 65 79 20 63  nc before they c
114b3 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 6f 75  an be written ou
114b4 74 20 74 6f 0a 2a 2a 20 20 20 74 68 65 20 64 61  t to.**   the da
114b5 74 61 62 61 73 65 20 66 69 6c 65 20 28 73 65 65  tabase file (see
114b6 20 66 75 6e 63 74 69 6f 6e 20 70 61 67 65 72 5f   function pager_
114b7 77 72 69 74 65 28 29 29 2e 0a 2a 2a 20 20 20 0a  write())..**   .
114b8 2a 2a 20 73 65 74 4d 61 73 74 65 72 0a 2a 2a 0a  ** setMaster.**.
114b9 2a 2a 20 20 20 54 68 69 73 20 76 61 72 69 61 62  **   This variab
114ba 6c 65 20 69 73 20 75 73 65 64 20 74 6f 20 65 6e  le is used to en
114bb 73 75 72 65 20 74 68 61 74 20 74 68 65 20 6d 61  sure that the ma
114bc 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
114bd 65 20 6e 61 6d 65 0a 2a 2a 20 20 20 28 69 66 20  e name.**   (if 
114be 61 6e 79 29 20 69 73 20 6f 6e 6c 79 20 77 72 69  any) is only wri
114bf 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f  tten into the jo
114c0 75 72 6e 61 6c 20 66 69 6c 65 20 6f 6e 63 65 2e  urnal file once.
114c1 0a 2a 2a 0a 2a 2a 20 20 20 57 68 65 6e 20 63 6f  .**.**   When co
114c2 6d 6d 69 74 74 69 6e 67 20 61 20 74 72 61 6e 73  mmitting a trans
114c3 61 63 74 69 6f 6e 2c 20 74 68 65 20 6d 61 73 74  action, the mast
114c4 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  er journal file 
114c5 6e 61 6d 65 20 28 69 66 20 61 6e 79 29 0a 2a 2a  name (if any).**
114c6 20 20 20 6d 61 79 20 62 65 20 77 72 69 74 74 65     may be writte
114c7 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e  n into the journ
114c8 61 6c 20 66 69 6c 65 20 77 68 69 6c 65 20 74 68  al file while th
114c9 65 20 70 61 67 65 72 20 69 73 20 73 74 69 6c 6c  e pager is still
114ca 20 69 6e 0a 2a 2a 20 20 20 50 41 47 45 52 5f 52   in.**   PAGER_R
114cb 45 53 45 52 56 45 44 20 73 74 61 74 65 20 28 73  ESERVED state (s
114cc 65 65 20 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e  ee CommitPhaseOn
114cd 65 28 29 20 66 6f 72 20 74 68 65 20 61 63 74 69  e() for the acti
114ce 6f 6e 29 2e 20 49 74 0a 2a 2a 20 20 20 74 68 65  on). It.**   the
114cf 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 75 70  n attempts to up
114d0 67 72 61 64 65 20 74 6f 20 61 6e 20 65 78 63 6c  grade to an excl
114d1 75 73 69 76 65 20 6c 6f 63 6b 2e 20 49 66 20 74  usive lock. If t
114d2 68 69 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 20  his attempt.**  
114d3 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 53 51 4c   fails, then SQL
114d4 49 54 45 5f 42 55 53 59 20 6d 61 79 20 62 65 20  ITE_BUSY may be 
114d5 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20  returned to the 
114d6 75 73 65 72 20 61 6e 64 20 74 68 65 20 75 73 65  user and the use
114d7 72 0a 2a 2a 20 20 20 6d 61 79 20 61 74 74 65 6d  r.**   may attem
114d8 70 74 20 74 6f 20 63 6f 6d 6d 69 74 20 74 68 65  pt to commit the
114d9 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 67 61   transaction aga
114da 69 6e 20 6c 61 74 65 72 20 28 63 61 6c 6c 69 6e  in later (callin
114db 67 0a 2a 2a 20 20 20 43 6f 6d 6d 69 74 50 68 61  g.**   CommitPha
114dc 73 65 4f 6e 65 28 29 20 61 67 61 69 6e 29 2e 20  seOne() again). 
114dd 54 68 69 73 20 66 6c 61 67 20 69 73 20 75 73 65  This flag is use
114de 64 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74  d to ensure that
114df 20 74 68 65 20 0a 2a 2a 20 20 20 6d 61 73 74 65   the .**   maste
114e0 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 69  r journal name i
114e1 73 20 6f 6e 6c 79 20 77 72 69 74 74 65 6e 20 74  s only written t
114e2 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  o the journal fi
114e3 6c 65 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20  le the first.** 
114e4 20 20 74 69 6d 65 20 43 6f 6d 6d 69 74 50 68 61    time CommitPha
114e5 73 65 4f 6e 65 28 29 20 69 73 20 63 61 6c 6c 65  seOne() is calle
114e6 64 2e 0a 2a 2a 0a 2a 2a 20 64 6f 4e 6f 74 53 79  d..**.** doNotSy
114e7 6e 63 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20  nc.**.**   This 
114e8 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20  variable is set 
114e9 61 6e 64 20 63 6c 65 61 72 65 64 20 62 79 20 73  and cleared by s
114ea 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
114eb 28 29 2e 0a 2a 2a 0a 2a 2a 20 6e 65 65 64 53 79  ()..**.** needSy
114ec 6e 63 0a 2a 2a 0a 2a 2a 20 20 20 54 4f 44 4f 3a  nc.**.**   TODO:
114ed 20 49 74 20 6d 69 67 68 74 20 62 65 20 65 61 73   It might be eas
114ee 69 65 72 20 74 6f 20 73 65 74 20 74 68 69 73 20  ier to set this 
114ef 76 61 72 69 61 62 6c 65 20 69 6e 20 77 72 69 74  variable in writ
114f0 65 4a 6f 75 72 6e 61 6c 48 64 72 28 29 0a 2a 2a  eJournalHdr().**
114f1 20 20 20 61 6e 64 20 77 72 69 74 65 4d 61 73 74     and writeMast
114f2 65 72 4a 6f 75 72 6e 61 6c 28 29 20 6f 6e 6c 79  erJournal() only
114f3 2e 20 43 68 61 6e 67 65 20 69 74 73 20 6d 65 61  . Change its mea
114f4 6e 69 6e 67 20 74 6f 20 22 75 6e 73 79 6e 63 65  ning to "unsynce
114f5 64 20 64 61 74 61 0a 2a 2a 20 20 20 68 61 73 20  d data.**   has 
114f6 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20  been written to 
114f7 74 68 65 20 6a 6f 75 72 6e 61 6c 22 2e 0a 2a 2a  the journal"..**
114f8 0a 2a 2a 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 79  .** subjInMemory
114f9 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 69 73  .**.**   This is
114fa 20 61 20 62 6f 6f 6c 65 61 6e 20 76 61 72 69 61   a boolean varia
114fb 62 6c 65 2e 20 49 66 20 74 72 75 65 2c 20 74 68  ble. If true, th
114fc 65 6e 20 61 6e 79 20 72 65 71 75 69 72 65 64 20  en any required 
114fd 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20  sub-journal.**  
114fe 20 69 73 20 6f 70 65 6e 65 64 20 61 73 20 61 6e   is opened as an
114ff 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e   in-memory journ
11500 61 6c 20 66 69 6c 65 2e 20 49 66 20 66 61 6c 73  al file. If fals
11501 65 2c 20 74 68 65 6e 20 69 6e 2d 6d 65 6d 6f 72  e, then in-memor
11502 79 0a 2a 2a 20 20 20 73 75 62 2d 6a 6f 75 72 6e  y.**   sub-journ
11503 61 6c 73 20 61 72 65 20 6f 6e 6c 79 20 75 73 65  als are only use
11504 64 20 66 6f 72 20 69 6e 2d 6d 65 6d 6f 72 79 20  d for in-memory 
11505 70 61 67 65 72 20 66 69 6c 65 73 2e 0a 2a 2f 0a  pager files..*/.
11506 73 74 72 75 63 74 20 50 61 67 65 72 20 7b 0a 20  struct Pager {. 
11507 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
11508 66 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  fs;          /* 
11509 4f 53 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20  OS functions to 
1150a 75 73 65 20 66 6f 72 20 49 4f 20 2a 2f 0a 20 20  use for IO */.  
1150b 75 38 20 65 78 63 6c 75 73 69 76 65 4d 6f 64 65  u8 exclusiveMode
1150c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42  ;           /* B
1150d 6f 6f 6c 65 61 6e 2e 20 54 72 75 65 20 69 66 20  oolean. True if 
1150e 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 3d 45 58  locking_mode==EX
1150f 43 4c 55 53 49 56 45 20 2a 2f 0a 20 20 75 38 20  CLUSIVE */.  u8 
11510 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3b 20 20 20 20  journalMode;    
11511 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 20 6f           /* On o
11512 66 20 74 68 65 20 50 41 47 45 52 5f 4a 4f 55 52  f the PAGER_JOUR
11513 4e 41 4c 4d 4f 44 45 5f 2a 20 76 61 6c 75 65 73  NALMODE_* values
11514 20 2a 2f 0a 20 20 75 38 20 75 73 65 4a 6f 75 72   */.  u8 useJour
11515 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  nal;            
11516 20 20 2f 2a 20 55 73 65 20 61 20 72 6f 6c 6c 62    /* Use a rollb
11517 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 6f 6e 20 74  ack journal on t
11518 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38  his file */.  u8
11519 20 6e 6f 52 65 61 64 6c 6f 63 6b 3b 20 20 20 20   noReadlock;    
1151a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20            /* Do 
1151b 6e 6f 74 20 62 6f 74 68 65 72 20 74 6f 20 6f 62  not bother to ob
1151c 74 61 69 6e 20 72 65 61 64 6c 6f 63 6b 73 20 2a  tain readlocks *
1151d 2f 0a 20 20 75 38 20 6e 6f 53 79 6e 63 3b 20 20  /.  u8 noSync;  
1151e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1151f 2f 2a 20 44 6f 20 6e 6f 74 20 73 79 6e 63 20 74  /* Do not sync t
11520 68 65 20 6a 6f 75 72 6e 61 6c 20 69 66 20 74 72  he journal if tr
11521 75 65 20 2a 2f 0a 20 20 75 38 20 66 75 6c 6c 53  ue */.  u8 fullS
11522 79 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  ync;            
11523 20 20 20 20 2f 2a 20 44 6f 20 65 78 74 72 61 20      /* Do extra 
11524 73 79 6e 63 73 20 6f 66 20 74 68 65 20 6a 6f 75  syncs of the jou
11525 72 6e 61 6c 20 66 6f 72 20 72 6f 62 75 73 74 6e  rnal for robustn
11526 65 73 73 20 2a 2f 0a 20 20 75 38 20 73 79 6e 63  ess */.  u8 sync
11527 5f 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20  _flags;         
11528 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 53       /* One of S
11529 59 4e 43 5f 4e 4f 52 4d 41 4c 20 6f 72 20 53 59  YNC_NORMAL or SY
1152a 4e 43 5f 46 55 4c 4c 20 2a 2f 0a 20 20 75 38 20  NC_FULL */.  u8 
1152b 74 65 6d 70 46 69 6c 65 3b 20 20 20 20 20 20 20  tempFile;       
1152c 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 46 69 6c           /* zFil
1152d 65 6e 61 6d 65 20 69 73 20 61 20 74 65 6d 70 6f  ename is a tempo
1152e 72 61 72 79 20 66 69 6c 65 20 2a 2f 0a 20 20 75  rary file */.  u
1152f 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20 20 20 20  8 readOnly;     
11530 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
11531 75 65 20 66 6f 72 20 61 20 72 65 61 64 2d 6f 6e  ue for a read-on
11532 6c 79 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  ly database */. 
11533 20 75 38 20 6d 65 6d 44 62 3b 20 20 20 20 20 20   u8 memDb;      
11534 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11535 54 72 75 65 20 74 6f 20 69 6e 68 69 62 69 74 20  True to inhibit 
11536 61 6c 6c 20 66 69 6c 65 20 49 2f 4f 20 2a 2f 0a  all file I/O */.
11537 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  .  /* The follow
11538 69 6e 67 20 62 6c 6f 63 6b 20 63 6f 6e 74 61 69  ing block contai
11539 6e 73 20 74 68 6f 73 65 20 63 6c 61 73 73 20 6d  ns those class m
1153a 65 6d 62 65 72 73 20 74 68 61 74 20 61 72 65 20  embers that are 
1153b 64 79 6e 61 6d 69 63 61 6c 6c 79 0a 20 20 2a 2a  dynamically.  **
1153c 20 6d 6f 64 69 66 69 65 64 20 64 75 72 69 6e 67   modified during
1153d 20 6e 6f 72 6d 61 6c 20 6f 70 65 72 61 74 69 6f   normal operatio
1153e 6e 73 2e 20 54 68 65 20 6f 74 68 65 72 20 76 61  ns. The other va
1153f 72 69 61 62 6c 65 73 20 69 6e 20 74 68 69 73 20  riables in this 
11540 73 74 72 75 63 74 75 72 65 0a 20 20 2a 2a 20 61  structure.  ** a
11541 72 65 20 65 69 74 68 65 72 20 63 6f 6e 73 74 61  re either consta
11542 6e 74 20 74 68 72 6f 75 67 68 6f 75 74 20 74 68  nt throughout th
11543 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 20 74 68  e lifetime of th
11544 65 20 70 61 67 65 72 2c 20 6f 72 20 65 6c 73 65  e pager, or else
11545 0a 20 20 2a 2a 20 75 73 65 64 20 74 6f 20 73 74  .  ** used to st
11546 6f 72 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  ore configuratio
11547 6e 20 70 61 72 61 6d 65 74 65 72 73 20 74 68 61  n parameters tha
11548 74 20 61 66 66 65 63 74 20 74 68 65 20 77 61 79  t affect the way
11549 20 74 68 65 20 70 61 67 65 72 20 0a 20 20 2a 2a   the pager .  **
1154a 20 6f 70 65 72 61 74 65 73 2e 0a 20 20 2a 2a 0a   operates..  **.
1154b 20 20 2a 2a 20 54 68 65 20 27 73 74 61 74 65 27    ** The 'state'
1154c 20 76 61 72 69 61 62 6c 65 20 69 73 20 64 65 73   variable is des
1154d 63 72 69 62 65 64 20 69 6e 20 6d 6f 72 65 20 64  cribed in more d
1154e 65 74 61 69 6c 20 61 6c 6f 6e 67 20 77 69 74 68  etail along with
1154f 20 74 68 65 0a 20 20 2a 2a 20 64 65 73 63 72 69   the.  ** descri
11550 70 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 76 61  ptions of the va
11551 6c 75 65 73 20 69 74 20 6d 61 79 20 74 61 6b 65  lues it may take
11552 20 2d 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20   - PAGER_UNLOCK 
11553 65 74 63 2e 20 4d 61 6e 79 20 6f 66 20 74 68 65  etc. Many of the
11554 0a 20 20 2a 2a 20 6f 74 68 65 72 20 76 61 72 69  .  ** other vari
11555 61 62 6c 65 73 20 69 6e 20 74 68 69 73 20 62 6c  ables in this bl
11556 6f 63 6b 20 61 72 65 20 64 65 73 63 72 69 62 65  ock are describe
11557 64 20 69 6e 20 74 68 65 20 63 6f 6d 6d 65 6e 74  d in the comment
11558 20 64 69 72 65 63 74 6c 79 20 0a 20 20 2a 2a 20   directly .  ** 
11559 61 62 6f 76 65 20 74 68 69 73 20 63 6c 61 73 73  above this class
1155a 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 20 20 2a   definition..  *
1155b 2f 0a 20 20 75 38 20 73 74 61 74 65 3b 20 20 20  /.  u8 state;   
1155c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1155d 2f 2a 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 2c  /* PAGER_UNLOCK,
1155e 20 5f 53 48 41 52 45 44 2c 20 5f 52 45 53 45 52   _SHARED, _RESER
1155f 56 45 44 2c 20 65 74 63 2e 20 2a 2f 0a 20 20 75  VED, etc. */.  u
11560 38 20 64 62 4d 6f 64 69 66 69 65 64 3b 20 20 20  8 dbModified;   
11561 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
11562 75 65 20 69 66 20 74 68 65 72 65 20 61 72 65 20  ue if there are 
11563 61 6e 79 20 63 68 61 6e 67 65 73 20 74 6f 20 74  any changes to t
11564 68 65 20 44 62 20 2a 2f 0a 20 20 75 38 20 6e 65  he Db */.  u8 ne
11565 65 64 53 79 6e 63 3b 20 20 20 20 20 20 20 20 20  edSync;         
11566 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
11567 66 20 61 6e 20 66 73 79 6e 63 28 29 20 69 73 20  f an fsync() is 
11568 6e 65 65 64 65 64 20 6f 6e 20 74 68 65 20 6a 6f  needed on the jo
11569 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 38 20 6a 6f  urnal */.  u8 jo
1156a 75 72 6e 61 6c 53 74 61 72 74 65 64 3b 20 20 20  urnalStarted;   
1156b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
1156c 66 20 68 65 61 64 65 72 20 6f 66 20 6a 6f 75 72  f header of jour
1156d 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 20 2a 2f  nal is synced */
1156e 0a 20 20 75 38 20 63 68 61 6e 67 65 43 6f 75 6e  .  u8 changeCoun
1156f 74 44 6f 6e 65 3b 20 20 20 20 20 20 20 20 20 2f  tDone;         /
11570 2a 20 53 65 74 20 61 66 74 65 72 20 69 6e 63 72  * Set after incr
11571 65 6d 65 6e 74 69 6e 67 20 74 68 65 20 63 68 61  ementing the cha
11572 6e 67 65 2d 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  nge-counter */. 
11573 20 75 38 20 73 65 74 4d 61 73 74 65 72 3b 20 20   u8 setMaster;  
11574 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11575 54 72 75 65 20 69 66 20 61 20 6d 2d 6a 20 6e 61  True if a m-j na
11576 6d 65 20 68 61 73 20 62 65 65 6e 20 77 72 69 74  me has been writ
11577 74 65 6e 20 74 6f 20 6a 72 6e 6c 20 2a 2f 0a 20  ten to jrnl */. 
11578 20 75 38 20 64 6f 4e 6f 74 53 79 6e 63 3b 20 20   u8 doNotSync;  
11579 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1157a 42 6f 6f 6c 65 61 6e 2e 20 57 68 69 6c 65 20 74  Boolean. While t
1157b 72 75 65 2c 20 64 6f 20 6e 6f 74 20 73 70 69 6c  rue, do not spil
1157c 6c 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a 20  l the cache */. 
1157d 20 75 38 20 64 62 53 69 7a 65 56 61 6c 69 64 3b   u8 dbSizeValid;
1157e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1157f 53 65 74 20 77 68 65 6e 20 64 62 53 69 7a 65 20  Set when dbSize 
11580 69 73 20 63 6f 72 72 65 63 74 20 2a 2f 0a 20 20  is correct */.  
11581 75 38 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 3b  u8 subjInMemory;
11582 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
11583 72 75 65 20 74 6f 20 75 73 65 20 69 6e 2d 6d 65  rue to use in-me
11584 6d 6f 72 79 20 73 75 62 2d 6a 6f 75 72 6e 61 6c  mory sub-journal
11585 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 64 62 53 69  s */.  Pgno dbSi
11586 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ze;             
11587 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
11588 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74  pages in the dat
11589 61 62 61 73 65 20 2a 2f 0a 20 20 50 67 6e 6f 20  abase */.  Pgno 
1158a 64 62 4f 72 69 67 53 69 7a 65 3b 20 20 20 20 20  dbOrigSize;     
1158b 20 20 20 20 20 20 20 2f 2a 20 64 62 53 69 7a 65         /* dbSize
1158c 20 62 65 66 6f 72 65 20 74 68 65 20 63 75 72 72   before the curr
1158d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ent transaction 
1158e 2a 2f 0a 20 20 50 67 6e 6f 20 64 62 46 69 6c 65  */.  Pgno dbFile
1158f 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20  Size;           
11590 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61   /* Number of pa
11591 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62  ges in the datab
11592 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e  ase file */.  in
11593 74 20 65 72 72 43 6f 64 65 3b 20 20 20 20 20 20  t errCode;      
11594 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
11595 20 6f 66 20 73 65 76 65 72 61 6c 20 6b 69 6e 64   of several kind
11596 73 20 6f 66 20 65 72 72 6f 72 73 20 2a 2f 0a 20  s of errors */. 
11597 20 69 6e 74 20 6e 52 65 63 3b 20 20 20 20 20 20   int nRec;      
11598 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11599 50 61 67 65 73 20 6a 6f 75 72 6e 61 6c 6c 65 64  Pages journalled
1159a 20 73 69 6e 63 65 20 6c 61 73 74 20 6a 2d 68 65   since last j-he
1159b 61 64 65 72 20 77 72 69 74 74 65 6e 20 2a 2f 0a  ader written */.
1159c 20 20 75 33 32 20 63 6b 73 75 6d 49 6e 69 74 3b    u32 cksumInit;
1159d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1159e 20 51 75 61 73 69 2d 72 61 6e 64 6f 6d 20 76 61   Quasi-random va
1159f 6c 75 65 20 61 64 64 65 64 20 74 6f 20 65 76 65  lue added to eve
115a0 72 79 20 63 68 65 63 6b 73 75 6d 20 2a 2f 0a 20  ry checksum */. 
115a1 20 75 33 32 20 6e 53 75 62 52 65 63 3b 20 20 20   u32 nSubRec;   
115a2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
115a3 4e 75 6d 62 65 72 20 6f 66 20 72 65 63 6f 72 64  Number of record
115a4 73 20 77 72 69 74 74 65 6e 20 74 6f 20 73 75 62  s written to sub
115a5 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 42 69  -journal */.  Bi
115a6 74 76 65 63 20 2a 70 49 6e 4a 6f 75 72 6e 61 6c  tvec *pInJournal
115a7 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65  ;         /* One
115a8 20 62 69 74 20 66 6f 72 20 65 61 63 68 20 70 61   bit for each pa
115a9 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  ge in the databa
115aa 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 73 71 6c  se file */.  sql
115ab 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 3b 20 20  ite3_file *fd;  
115ac 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65           /* File
115ad 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20   descriptor for 
115ae 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 73 71  database */.  sq
115af 6c 69 74 65 33 5f 66 69 6c 65 20 2a 6a 66 64 3b  lite3_file *jfd;
115b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c            /* Fil
115b1 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72  e descriptor for
115b2 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f   main journal */
115b3 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  .  sqlite3_file 
115b4 2a 73 6a 66 64 3b 20 20 20 20 20 20 20 20 20 2f  *sjfd;         /
115b5 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f  * File descripto
115b6 72 20 66 6f 72 20 73 75 62 2d 6a 6f 75 72 6e 61  r for sub-journa
115b7 6c 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e  l */.  i64 journ
115b8 61 6c 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20  alOff;          
115b9 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 77 72     /* Current wr
115ba 69 74 65 20 6f 66 66 73 65 74 20 69 6e 20 74 68  ite offset in th
115bb 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a  e journal file *
115bc 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61 6c 48  /.  i64 journalH
115bd 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dr;             
115be 2f 2a 20 42 79 74 65 20 6f 66 66 73 65 74 20 74  /* Byte offset t
115bf 6f 20 70 72 65 76 69 6f 75 73 20 6a 6f 75 72 6e  o previous journ
115c0 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 50  al header */.  P
115c1 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 61  agerSavepoint *a
115c2 53 61 76 65 70 6f 69 6e 74 3b 20 2f 2a 20 41 72  Savepoint; /* Ar
115c3 72 61 79 20 6f 66 20 61 63 74 69 76 65 20 73 61  ray of active sa
115c4 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 20 69 6e  vepoints */.  in
115c5 74 20 6e 53 61 76 65 70 6f 69 6e 74 3b 20 20 20  t nSavepoint;   
115c6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
115c7 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20  ber of elements 
115c8 69 6e 20 61 53 61 76 65 70 6f 69 6e 74 5b 5d 20  in aSavepoint[] 
115c9 2a 2f 0a 20 20 63 68 61 72 20 64 62 46 69 6c 65  */.  char dbFile
115ca 56 65 72 73 5b 31 36 5d 3b 20 20 20 20 20 20 20  Vers[16];       
115cb 20 2f 2a 20 43 68 61 6e 67 65 73 20 77 68 65 6e   /* Changes when
115cc 65 76 65 72 20 64 61 74 61 62 61 73 65 20 66 69  ever database fi
115cd 6c 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 20 20  le changes */.  
115ce 75 33 32 20 73 65 63 74 6f 72 53 69 7a 65 3b 20  u32 sectorSize; 
115cf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
115d0 73 73 75 6d 65 64 20 73 65 63 74 6f 72 20 73 69  ssumed sector si
115d1 7a 65 20 64 75 72 69 6e 67 20 72 6f 6c 6c 62 61  ze during rollba
115d2 63 6b 20 2a 2f 0a 0a 20 20 75 31 36 20 6e 45 78  ck */..  u16 nEx
115d3 74 72 61 3b 20 20 20 20 20 20 20 20 20 20 20 20  tra;            
115d4 20 20 20 20 20 2f 2a 20 41 64 64 20 74 68 69 73       /* Add this
115d5 20 6d 61 6e 79 20 62 79 74 65 73 20 74 6f 20 65   many bytes to e
115d6 61 63 68 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61  ach in-memory pa
115d7 67 65 20 2a 2f 0a 20 20 69 31 36 20 6e 52 65 73  ge */.  i16 nRes
115d8 65 72 76 65 3b 20 20 20 20 20 20 20 20 20 20 20  erve;           
115d9 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
115da 20 75 6e 75 73 65 64 20 62 79 74 65 73 20 61 74   unused bytes at
115db 20 65 6e 64 20 6f 66 20 65 61 63 68 20 70 61 67   end of each pag
115dc 65 20 2a 2f 0a 20 20 75 33 32 20 76 66 73 46 6c  e */.  u32 vfsFl
115dd 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  ags;            
115de 20 20 20 2f 2a 20 46 6c 61 67 73 20 66 6f 72 20     /* Flags for 
115df 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65  sqlite3_vfs.xOpe
115e0 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 70 61 67  n() */.  int pag
115e1 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20  eSize;          
115e2 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
115e3 66 20 62 79 74 65 73 20 69 6e 20 61 20 70 61 67  f bytes in a pag
115e4 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 6d 78 50 67  e */.  Pgno mxPg
115e5 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  no;             
115e6 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 6c     /* Maximum al
115e7 6c 6f 77 65 64 20 73 69 7a 65 20 6f 66 20 74 68  lowed size of th
115e8 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  e database */.  
115e9 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 3b  char *zFilename;
115ea 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
115eb 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
115ec 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68  ase file */.  ch
115ed 61 72 20 2a 7a 4a 6f 75 72 6e 61 6c 3b 20 20 20  ar *zJournal;   
115ee 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
115ef 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  e of the journal
115f0 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 28   file */.  int (
115f1 2a 78 42 75 73 79 48 61 6e 64 6c 65 72 29 28 76  *xBusyHandler)(v
115f2 6f 69 64 2a 29 3b 20 2f 2a 20 46 75 6e 63 74 69  oid*); /* Functi
115f3 6f 6e 20 74 6f 20 63 61 6c 6c 20 77 68 65 6e 20  on to call when 
115f4 62 75 73 79 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  busy */.  void *
115f5 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67 3b  pBusyHandlerArg;
115f6 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74        /* Context
115f7 20 61 72 67 75 6d 65 6e 74 20 66 6f 72 20 78 42   argument for xB
115f8 75 73 79 48 61 6e 64 6c 65 72 20 2a 2f 0a 23 69  usyHandler */.#i
115f9 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
115fa 0a 20 20 69 6e 74 20 6e 48 69 74 2c 20 6e 4d 69  .  int nHit, nMi
115fb 73 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ss;            /
115fc 2a 20 43 61 63 68 65 20 68 69 74 73 20 61 6e 64  * Cache hits and
115fd 20 6d 69 73 73 69 6e 67 20 2a 2f 0a 20 20 69 6e   missing */.  in
115fe 74 20 6e 52 65 61 64 2c 20 6e 57 72 69 74 65 3b  t nRead, nWrite;
115ff 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
11600 61 62 61 73 65 20 70 61 67 65 73 20 72 65 61 64  abase pages read
11601 2f 77 72 69 74 74 65 6e 20 2a 2f 0a 23 65 6e 64  /written */.#end
11602 69 66 0a 20 20 76 6f 69 64 20 28 2a 78 52 65 69  if.  void (*xRei
11603 6e 69 74 65 72 29 28 44 62 50 61 67 65 2a 29 3b  niter)(DbPage*);
11604 20 2f 2a 20 43 61 6c 6c 20 74 68 69 73 20 72 6f   /* Call this ro
11605 75 74 69 6e 65 20 77 68 65 6e 20 72 65 6c 6f 61  utine when reloa
11606 64 69 6e 67 20 70 61 67 65 73 20 2a 2f 0a 23 69  ding pages */.#i
11607 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f  fdef SQLITE_HAS_
11608 43 4f 44 45 43 0a 20 20 76 6f 69 64 20 2a 28 2a  CODEC.  void *(*
11609 78 43 6f 64 65 63 29 28 76 6f 69 64 2a 2c 76 6f  xCodec)(void*,vo
1160a 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74 29 3b 20 2f  id*,Pgno,int); /
1160b 2a 20 52 6f 75 74 69 6e 65 20 66 6f 72 20 65 6e  * Routine for en
1160c 2f 64 65 63 6f 64 69 6e 67 20 64 61 74 61 20 2a  /decoding data *
1160d 2f 0a 20 20 76 6f 69 64 20 28 2a 78 43 6f 64 65  /.  void (*xCode
1160e 63 53 69 7a 65 43 68 6e 67 29 28 76 6f 69 64 2a  cSizeChng)(void*
1160f 2c 69 6e 74 2c 69 6e 74 29 3b 20 2f 2a 20 4e 6f  ,int,int); /* No
11610 74 69 66 79 20 6f 66 20 70 61 67 65 20 73 69 7a  tify of page siz
11611 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 20 20 76  e changes */.  v
11612 6f 69 64 20 28 2a 78 43 6f 64 65 63 46 72 65 65  oid (*xCodecFree
11613 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20 20  )(void*);       
11614 20 20 20 20 20 20 2f 2a 20 44 65 73 74 72 75 63        /* Destruc
11615 74 6f 72 20 66 6f 72 20 74 68 65 20 63 6f 64 65  tor for the code
11616 63 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6f  c */.  void *pCo
11617 64 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  dec;            
11618 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 75     /* First argu
11619 6d 65 6e 74 20 74 6f 20 78 43 6f 64 65 63 2e 2e  ment to xCodec..
1161a 2e 20 6d 65 74 68 6f 64 73 20 2a 2f 0a 23 65 6e  . methods */.#en
1161b 64 69 66 0a 20 20 63 68 61 72 20 2a 70 54 6d 70  dif.  char *pTmp
1161c 53 70 61 63 65 3b 20 20 20 20 20 20 20 20 20 20  Space;          
1161d 20 20 2f 2a 20 50 61 67 65 72 2e 70 61 67 65 53    /* Pager.pageS
1161e 69 7a 65 20 62 79 74 65 73 20 6f 66 20 73 70 61  ize bytes of spa
1161f 63 65 20 66 6f 72 20 74 6d 70 20 75 73 65 20 2a  ce for tmp use *
11620 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61 6c 53  /.  i64 journalS
11621 69 7a 65 4c 69 6d 69 74 3b 20 20 20 20 20 20 20  izeLimit;       
11622 2f 2a 20 53 69 7a 65 20 6c 69 6d 69 74 20 66 6f  /* Size limit fo
11623 72 20 70 65 72 73 69 73 74 65 6e 74 20 6a 6f 75  r persistent jou
11624 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f 0a 20 20  rnal files */.  
11625 50 43 61 63 68 65 20 2a 70 50 43 61 63 68 65 3b  PCache *pPCache;
11626 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
11627 6f 69 6e 74 65 72 20 74 6f 20 70 61 67 65 20 63  ointer to page c
11628 61 63 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20  ache object */. 
11629 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20   sqlite3_backup 
1162a 2a 70 42 61 63 6b 75 70 3b 20 20 20 20 2f 2a 20  *pBackup;    /* 
1162b 50 6f 69 6e 74 65 72 20 74 6f 20 6c 69 73 74 20  Pointer to list 
1162c 6f 66 20 6f 6e 67 6f 69 6e 67 20 62 61 63 6b 75  of ongoing backu
1162d 70 20 70 72 6f 63 65 73 73 65 73 20 2a 2f 0a 7d  p processes */.}
1162e 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  ;../*.** The fol
1162f 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61  lowing global va
11630 72 69 61 62 6c 65 73 20 68 6f 6c 64 20 63 6f 75  riables hold cou
11631 6e 74 65 72 73 20 75 73 65 64 20 66 6f 72 0a 2a  nters used for.*
11632 2a 20 74 65 73 74 69 6e 67 20 70 75 72 70 6f 73  * testing purpos
11633 65 73 20 6f 6e 6c 79 2e 20 20 54 68 65 73 65 20  es only.  These 
11634 76 61 72 69 61 62 6c 65 73 20 64 6f 20 6e 6f 74  variables do not
11635 20 65 78 69 73 74 20 69 6e 0a 2a 2a 20 61 20 6e   exist in.** a n
11636 6f 6e 2d 74 65 73 74 69 6e 67 20 62 75 69 6c 64  on-testing build
11637 2e 20 20 54 68 65 73 65 20 76 61 72 69 61 62 6c  .  These variabl
11638 65 73 20 61 72 65 20 6e 6f 74 20 74 68 72 65 61  es are not threa
11639 64 2d 73 61 66 65 2e 0a 2a 2f 0a 23 69 66 64 65  d-safe..*/.#ifde
1163a 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51  f SQLITE_TEST.SQ
1163b 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1163c 69 74 65 33 5f 70 61 67 65 72 5f 72 65 61 64 64  ite3_pager_readd
1163d 62 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 20 20 20  b_count = 0;    
1163e 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 75 6c  /* Number of ful
1163f 6c 20 70 61 67 65 73 20 72 65 61 64 20 66 72 6f  l pages read fro
11640 6d 20 44 42 20 2a 2f 0a 53 51 4c 49 54 45 5f 41  m DB */.SQLITE_A
11641 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70  PI int sqlite3_p
11642 61 67 65 72 5f 77 72 69 74 65 64 62 5f 63 6f 75  ager_writedb_cou
11643 6e 74 20 3d 20 30 3b 20 20 20 2f 2a 20 4e 75 6d  nt = 0;   /* Num
11644 62 65 72 20 6f 66 20 66 75 6c 6c 20 70 61 67 65  ber of full page
11645 73 20 77 72 69 74 74 65 6e 20 74 6f 20 44 42 20  s written to DB 
11646 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
11647 74 20 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f  t sqlite3_pager_
11648 77 72 69 74 65 6a 5f 63 6f 75 6e 74 20 3d 20 30  writej_count = 0
11649 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f  ;    /* Number o
1164a 66 20 70 61 67 65 73 20 77 72 69 74 74 65 6e 20  f pages written 
1164b 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 20  to journal */.# 
1164c 64 65 66 69 6e 65 20 50 41 47 45 52 5f 49 4e 43  define PAGER_INC
1164d 52 28 76 29 20 20 76 2b 2b 0a 23 65 6c 73 65 0a  R(v)  v++.#else.
1164e 23 20 64 65 66 69 6e 65 20 50 41 47 45 52 5f 49  # define PAGER_I
1164f 4e 43 52 28 76 29 0a 23 65 6e 64 69 66 0a 0a 0a  NCR(v).#endif...
11650 0a 2f 2a 0a 2a 2a 20 4a 6f 75 72 6e 61 6c 20 66  ./*.** Journal f
11651 69 6c 65 73 20 62 65 67 69 6e 20 77 69 74 68 20  iles begin with 
11652 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61  the following ma
11653 67 69 63 20 73 74 72 69 6e 67 2e 20 20 54 68 65  gic string.  The
11654 20 64 61 74 61 0a 2a 2a 20 77 61 73 20 6f 62 74   data.** was obt
11655 61 69 6e 65 64 20 66 72 6f 6d 20 2f 64 65 76 2f  ained from /dev/
11656 72 61 6e 64 6f 6d 2e 20 20 49 74 20 69 73 20 75  random.  It is u
11657 73 65 64 20 6f 6e 6c 79 20 61 73 20 61 20 73 61  sed only as a sa
11658 6e 69 74 79 20 63 68 65 63 6b 2e 0a 2a 2a 0a 2a  nity check..**.*
11659 2a 20 53 69 6e 63 65 20 76 65 72 73 69 6f 6e 20  * Since version 
1165a 32 2e 38 2e 30 2c 20 74 68 65 20 6a 6f 75 72 6e  2.8.0, the journ
1165b 61 6c 20 66 6f 72 6d 61 74 20 63 6f 6e 74 61 69  al format contai
1165c 6e 73 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 61  ns additional sa
1165d 6e 69 74 79 0a 2a 2a 20 63 68 65 63 6b 69 6e 67  nity.** checking
1165e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 49   information.  I
1165f 66 20 74 68 65 20 70 6f 77 65 72 20 66 61 69 6c  f the power fail
11660 73 20 77 68 69 6c 65 20 74 68 65 20 6a 6f 75 72  s while the jour
11661 6e 61 6c 20 69 73 20 62 65 69 6e 67 0a 2a 2a 20  nal is being.** 
11662 77 72 69 74 74 65 6e 2c 20 73 65 6d 69 2d 72 61  written, semi-ra
11663 6e 64 6f 6d 20 67 61 72 62 61 67 65 20 64 61 74  ndom garbage dat
11664 61 20 6d 69 67 68 74 20 61 70 70 65 61 72 20 69  a might appear i
11665 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a  n the journal.**
11666 20 66 69 6c 65 20 61 66 74 65 72 20 70 6f 77 65   file after powe
11667 72 20 69 73 20 72 65 73 74 6f 72 65 64 2e 20 20  r is restored.  
11668 49 66 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73  If an attempt is
11669 20 74 68 65 6e 20 6d 61 64 65 0a 2a 2a 20 74 6f   then made.** to
1166a 20 72 6f 6c 6c 20 74 68 65 20 6a 6f 75 72 6e 61   roll the journa
1166b 6c 20 62 61 63 6b 2c 20 74 68 65 20 64 61 74 61  l back, the data
1166c 62 61 73 65 20 63 6f 75 6c 64 20 62 65 20 63 6f  base could be co
1166d 72 72 75 70 74 65 64 2e 20 20 54 68 65 20 61 64  rrupted.  The ad
1166e 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 73 61 6e 69  ditional.** sani
1166f 74 79 20 63 68 65 63 6b 69 6e 67 20 64 61 74 61  ty checking data
11670 20 69 73 20 61 6e 20 61 74 74 65 6d 70 74 20 74   is an attempt t
11671 6f 20 64 69 73 63 6f 76 65 72 20 74 68 65 20 67  o discover the g
11672 61 72 62 61 67 65 20 69 6e 20 74 68 65 0a 2a 2a  arbage in the.**
11673 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 69 67 6e   journal and ign
11674 6f 72 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  ore it..**.** Th
11675 65 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e  e sanity checkin
11676 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f  g information fo
11677 72 20 74 68 65 20 6e 65 77 20 6a 6f 75 72 6e 61  r the new journa
11678 6c 20 66 6f 72 6d 61 74 20 63 6f 6e 73 69 73 74  l format consist
11679 73 0a 2a 2a 20 6f 66 20 61 20 33 32 2d 62 69 74  s.** of a 32-bit
1167a 20 63 68 65 63 6b 73 75 6d 20 6f 6e 20 65 61 63   checksum on eac
1167b 68 20 70 61 67 65 20 6f 66 20 64 61 74 61 2e 20  h page of data. 
1167c 20 54 68 65 20 63 68 65 63 6b 73 75 6d 20 63 6f   The checksum co
1167d 76 65 72 73 20 62 6f 74 68 0a 2a 2a 20 74 68 65  vers both.** the
1167e 20 70 61 67 65 20 6e 75 6d 62 65 72 20 61 6e 64   page number and
1167f 20 74 68 65 20 70 50 61 67 65 72 2d 3e 70 61 67   the pPager->pag
11680 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20 64  eSize bytes of d
11681 61 74 61 20 66 6f 72 20 74 68 65 20 70 61 67 65  ata for the page
11682 2e 0a 2a 2a 20 54 68 69 73 20 63 6b 73 75 6d 20  ..** This cksum 
11683 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74  is initialized t
11684 6f 20 61 20 33 32 2d 62 69 74 20 72 61 6e 64 6f  o a 32-bit rando
11685 6d 20 76 61 6c 75 65 20 74 68 61 74 20 61 70 70  m value that app
11686 65 61 72 73 20 69 6e 20 74 68 65 0a 2a 2a 20 6a  ears in the.** j
11687 6f 75 72 6e 61 6c 20 66 69 6c 65 20 72 69 67 68  ournal file righ
11688 74 20 61 66 74 65 72 20 74 68 65 20 68 65 61 64  t after the head
11689 65 72 2e 20 20 54 68 65 20 72 61 6e 64 6f 6d 20  er.  The random 
1168a 69 6e 69 74 69 61 6c 69 7a 65 72 20 69 73 20 69  initializer is i
1168b 6d 70 6f 72 74 61 6e 74 2c 0a 2a 2a 20 62 65 63  mportant,.** bec
1168c 61 75 73 65 20 67 61 72 62 61 67 65 20 64 61 74  ause garbage dat
1168d 61 20 74 68 61 74 20 61 70 70 65 61 72 73 20 61  a that appears a
1168e 74 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20 6a  t the end of a j
1168f 6f 75 72 6e 61 6c 20 69 73 20 6c 69 6b 65 6c 79  ournal is likely
11690 0a 2a 2a 20 64 61 74 61 20 74 68 61 74 20 77 61  .** data that wa
11691 73 20 6f 6e 63 65 20 69 6e 20 6f 74 68 65 72 20  s once in other 
11692 66 69 6c 65 73 20 74 68 61 74 20 68 61 76 65 20  files that have 
11693 6e 6f 77 20 62 65 65 6e 20 64 65 6c 65 74 65 64  now been deleted
11694 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 67 61 72  .  If the.** gar
11695 62 61 67 65 20 64 61 74 61 20 63 61 6d 65 20 66  bage data came f
11696 72 6f 6d 20 61 6e 20 6f 62 73 6f 6c 65 74 65 20  rom an obsolete 
11697 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68  journal file, th
11698 65 20 63 68 65 63 6b 73 75 6d 73 20 6d 69 67 68  e checksums migh
11699 74 0a 2a 2a 20 62 65 20 63 6f 72 72 65 63 74 2e  t.** be correct.
1169a 20 20 42 75 74 20 62 79 20 69 6e 69 74 69 61 6c    But by initial
1169b 69 7a 69 6e 67 20 74 68 65 20 63 68 65 63 6b 73  izing the checks
1169c 75 6d 20 74 6f 20 72 61 6e 64 6f 6d 20 76 61 6c  um to random val
1169d 75 65 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 64  ue which.** is d
1169e 69 66 66 65 72 65 6e 74 20 66 6f 72 20 65 76 65  ifferent for eve
1169f 72 79 20 6a 6f 75 72 6e 61 6c 2c 20 77 65 20 6d  ry journal, we m
116a0 69 6e 69 6d 69 7a 65 20 74 68 61 74 20 72 69 73  inimize that ris
116a1 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  k..*/.static con
116a2 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
116a3 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 5b 5d   aJournalMagic[]
116a4 20 3d 20 7b 0a 20 20 30 78 64 39 2c 20 30 78 64   = {.  0xd9, 0xd
116a5 35 2c 20 30 78 30 35 2c 20 30 78 66 39 2c 20 30  5, 0x05, 0xf9, 0
116a6 78 32 30 2c 20 30 78 61 31 2c 20 30 78 36 33 2c  x20, 0xa1, 0x63,
116a7 20 30 78 64 37 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a   0xd7,.};../*.**
116a8 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   The size of the
116a9 20 6f 66 20 65 61 63 68 20 70 61 67 65 20 72 65   of each page re
116aa 63 6f 72 64 20 69 6e 20 74 68 65 20 6a 6f 75 72  cord in the jour
116ab 6e 61 6c 20 69 73 20 67 69 76 65 6e 20 62 79 0a  nal is given by.
116ac 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** the following
116ad 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 64 65 66 69   macro..*/.#defi
116ae 6e 65 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a  ne JOURNAL_PG_SZ
116af 28 70 50 61 67 65 72 29 20 20 28 28 70 50 61 67  (pPager)  ((pPag
116b0 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 20 2b 20  er->pageSize) + 
116b1 38 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f  8)../*.** The jo
116b2 75 72 6e 61 6c 20 68 65 61 64 65 72 20 73 69 7a  urnal header siz
116b3 65 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 72  e for this pager
116b4 2e 20 54 68 69 73 20 69 73 20 75 73 75 61 6c 6c  . This is usuall
116b5 79 20 74 68 65 20 73 61 6d 65 20 0a 2a 2a 20 73  y the same .** s
116b6 69 7a 65 20 61 73 20 61 20 73 69 6e 67 6c 65 20  ize as a single 
116b7 64 69 73 6b 20 73 65 63 74 6f 72 2e 20 53 65 65  disk sector. See
116b8 20 61 6c 73 6f 20 73 65 74 53 65 63 74 6f 72 53   also setSectorS
116b9 69 7a 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ize()..*/.#defin
116ba 65 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a  e JOURNAL_HDR_SZ
116bb 28 70 50 61 67 65 72 29 20 28 70 50 61 67 65 72  (pPager) (pPager
116bc 2d 3e 73 65 63 74 6f 72 53 69 7a 65 29 0a 0a 2f  ->sectorSize)../
116bd 2a 0a 2a 2a 20 54 68 65 20 6d 61 63 72 6f 20 4d  *.** The macro M
116be 45 4d 44 42 20 69 73 20 74 72 75 65 20 69 66 20  EMDB is true if 
116bf 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77  we are dealing w
116c0 69 74 68 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79  ith an in-memory
116c1 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20 57 65   database..** We
116c2 20 64 6f 20 74 68 69 73 20 61 73 20 61 20 6d 61   do this as a ma
116c3 63 72 6f 20 73 6f 20 74 68 61 74 20 69 66 20 74  cro so that if t
116c4 68 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d  he SQLITE_OMIT_M
116c5 45 4d 4f 52 59 44 42 20 6d 61 63 72 6f 20 69 73  EMORYDB macro is
116c6 20 73 65 74 2c 0a 2a 2a 20 74 68 65 20 76 61 6c   set,.** the val
116c7 75 65 20 6f 66 20 4d 45 4d 44 42 20 77 69 6c 6c  ue of MEMDB will
116c8 20 62 65 20 61 20 63 6f 6e 73 74 61 6e 74 20 61   be a constant a
116c9 6e 64 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20  nd the compiler 
116ca 77 69 6c 6c 20 6f 70 74 69 6d 69 7a 65 0a 2a 2a  will optimize.**
116cb 20 6f 75 74 20 63 6f 64 65 20 74 68 61 74 20 77   out code that w
116cc 6f 75 6c 64 20 6e 65 76 65 72 20 65 78 65 63 75  ould never execu
116cd 74 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  te..*/.#ifdef SQ
116ce 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59  LITE_OMIT_MEMORY
116cf 44 42 0a 23 20 64 65 66 69 6e 65 20 4d 45 4d 44  DB.# define MEMD
116d0 42 20 30 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  B 0.#else.# defi
116d1 6e 65 20 4d 45 4d 44 42 20 70 50 61 67 65 72 2d  ne MEMDB pPager-
116d2 3e 6d 65 6d 44 62 0a 23 65 6e 64 69 66 0a 0a 2f  >memDb.#endif../
116d3 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d  *.** The maximum
116d4 20 6c 65 67 61 6c 20 70 61 67 65 20 6e 75 6d 62   legal page numb
116d5 65 72 20 69 73 20 28 32 5e 33 31 20 2d 20 31 29  er is (2^31 - 1)
116d6 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47  ..*/.#define PAG
116d7 45 52 5f 4d 41 58 5f 50 47 4e 4f 20 32 31 34 37  ER_MAX_PGNO 2147
116d8 34 38 33 36 34 37 0a 0a 23 69 66 6e 64 65 66 20  483647..#ifndef 
116d9 4e 44 45 42 55 47 20 0a 2f 2a 0a 2a 2a 20 55 73  NDEBUG ./*.** Us
116da 61 67 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 73 73  age:.**.**   ass
116db 65 72 74 28 20 61 73 73 65 72 74 5f 70 61 67 65  ert( assert_page
116dc 72 5f 73 74 61 74 65 28 70 50 61 67 65 72 29 20  r_state(pPager) 
116dd 29 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  );.*/.static int
116de 20 61 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74   assert_pager_st
116df 61 74 65 28 50 61 67 65 72 20 2a 70 50 61 67 65  ate(Pager *pPage
116e0 72 29 7b 0a 0a 20 20 2f 2a 20 41 20 74 65 6d 70  r){..  /* A temp
116e1 2d 66 69 6c 65 20 69 73 20 61 6c 77 61 79 73 20  -file is always 
116e2 69 6e 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49  in PAGER_EXCLUSI
116e3 56 45 20 6f 72 20 50 41 47 45 52 5f 53 59 4e 43  VE or PAGER_SYNC
116e4 45 44 20 73 74 61 74 65 2e 20 2a 2f 0a 20 20 61  ED state. */.  a
116e5 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 74  ssert( pPager->t
116e6 65 6d 70 46 69 6c 65 3d 3d 30 20 7c 7c 20 70 50  empFile==0 || pP
116e7 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47  ager->state>=PAG
116e8 45 52 5f 45 58 43 4c 55 53 49 56 45 20 29 3b 0a  ER_EXCLUSIVE );.
116e9 0a 20 20 2f 2a 20 54 68 65 20 63 68 61 6e 67 65  .  /* The change
116ea 43 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67 20 69  CountDone flag i
116eb 73 20 61 6c 77 61 79 73 20 73 65 74 20 66 6f 72  s always set for
116ec 20 74 65 6d 70 2d 66 69 6c 65 73 20 2a 2f 0a 20   temp-files */. 
116ed 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
116ee 3e 74 65 6d 70 46 69 6c 65 3d 3d 30 20 7c 7c 20  >tempFile==0 || 
116ef 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f  pPager->changeCo
116f0 75 6e 74 44 6f 6e 65 20 29 3b 0a 0a 20 20 72 65  untDone );..  re
116f1 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66  turn 1;.}.#endif
116f2 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
116f3 72 75 65 20 69 66 20 69 74 20 69 73 20 6e 65 63  rue if it is nec
116f4 65 73 73 61 72 79 20 74 6f 20 77 72 69 74 65 20  essary to write 
116f5 70 61 67 65 20 2a 70 50 67 20 69 6e 74 6f 20 74  page *pPg into t
116f6 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 0a  he sub-journal..
116f7 2a 2a 20 41 20 70 61 67 65 20 6e 65 65 64 73 20  ** A page needs 
116f8 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e  to be written in
116f9 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e  to the sub-journ
116fa 61 6c 20 69 66 20 74 68 65 72 65 20 65 78 69 73  al if there exis
116fb 74 73 20 6f 6e 65 0a 2a 2a 20 6f 72 20 6d 6f 72  ts one.** or mor
116fc 65 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74  e open savepoint
116fd 73 20 66 6f 72 20 77 68 69 63 68 3a 0a 2a 2a 0a  s for which:.**.
116fe 2a 2a 20 20 20 2a 20 54 68 65 20 70 61 67 65 2d  **   * The page-
116ff 6e 75 6d 62 65 72 20 69 73 20 6c 65 73 73 20 74  number is less t
11700 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20  han or equal to 
11701 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 6e  PagerSavepoint.n
11702 4f 72 69 67 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a  Orig, and.**   *
11703 20 54 68 65 20 62 69 74 20 63 6f 72 72 65 73 70   The bit corresp
11704 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 70 61  onding to the pa
11705 67 65 2d 6e 75 6d 62 65 72 20 69 73 20 6e 6f 74  ge-number is not
11706 20 73 65 74 20 69 6e 0a 2a 2a 20 20 20 20 20 50   set in.**     P
11707 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 70 49  agerSavepoint.pI
11708 6e 53 61 76 65 70 6f 69 6e 74 2e 0a 2a 2f 0a 73  nSavepoint..*/.s
11709 74 61 74 69 63 20 69 6e 74 20 73 75 62 6a 52 65  tatic int subjRe
1170a 71 75 69 72 65 73 50 61 67 65 28 50 67 48 64 72  quiresPage(PgHdr
1170b 20 2a 70 50 67 29 7b 0a 20 20 50 67 6e 6f 20 70   *pPg){.  Pgno p
1170c 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b  gno = pPg->pgno;
1170d 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
1170e 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a   = pPg->pPager;.
1170f 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69    int i;.  for(i
11710 3d 30 3b 20 69 3c 70 50 61 67 65 72 2d 3e 6e 53  =0; i<pPager->nS
11711 61 76 65 70 6f 69 6e 74 3b 20 69 2b 2b 29 7b 0a  avepoint; i++){.
11712 20 20 20 20 50 61 67 65 72 53 61 76 65 70 6f 69      PagerSavepoi
11713 6e 74 20 2a 70 20 3d 20 26 70 50 61 67 65 72 2d  nt *p = &pPager-
11714 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 5d 3b 0a  >aSavepoint[i];.
11715 20 20 20 20 69 66 28 20 70 2d 3e 6e 4f 72 69 67      if( p->nOrig
11716 3e 3d 70 67 6e 6f 20 26 26 20 30 3d 3d 73 71 6c  >=pgno && 0==sql
11717 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70  ite3BitvecTest(p
11718 2d 3e 70 49 6e 53 61 76 65 70 6f 69 6e 74 2c 20  ->pInSavepoint, 
11719 70 67 6e 6f 29 20 29 7b 0a 20 20 20 20 20 20 72  pgno) ){.      r
1171a 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
1171b 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
1171c 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
1171d 72 75 65 20 69 66 20 74 68 65 20 70 61 67 65 20  rue if the page 
1171e 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68  is already in th
1171f 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a  e journal file..
11720 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61  */.static int pa
11721 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 50 67 48 64  geInJournal(PgHd
11722 72 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 75 72  r *pPg){.  retur
11723 6e 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 54  n sqlite3BitvecT
11724 65 73 74 28 70 50 67 2d 3e 70 50 61 67 65 72 2d  est(pPg->pPager-
11725 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 50 67  >pInJournal, pPg
11726 2d 3e 70 67 6e 6f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  ->pgno);.}../*.*
11727 2a 20 52 65 61 64 20 61 20 33 32 2d 62 69 74 20  * Read a 32-bit 
11728 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 74 68 65  integer from the
11729 20 67 69 76 65 6e 20 66 69 6c 65 20 64 65 73 63   given file desc
1172a 72 69 70 74 6f 72 2e 20 20 53 74 6f 72 65 20 74  riptor.  Store t
1172b 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 74 68  he integer.** th
1172c 61 74 20 69 73 20 72 65 61 64 20 69 6e 20 2a 70  at is read in *p
1172d 52 65 73 2e 20 20 52 65 74 75 72 6e 20 53 51 4c  Res.  Return SQL
1172e 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74  ITE_OK if everyt
1172f 68 69 6e 67 20 77 6f 72 6b 65 64 2c 20 6f 72 20  hing worked, or 
11730 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65  an.** error code
11731 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f   is something go
11732 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20  es wrong..**.** 
11733 41 6c 6c 20 76 61 6c 75 65 73 20 61 72 65 20 73  All values are s
11734 74 6f 72 65 64 20 6f 6e 20 64 69 73 6b 20 61 73  tored on disk as
11735 20 62 69 67 2d 65 6e 64 69 61 6e 2e 0a 2a 2f 0a   big-endian..*/.
11736 73 74 61 74 69 63 20 69 6e 74 20 72 65 61 64 33  static int read3
11737 32 62 69 74 73 28 73 71 6c 69 74 65 33 5f 66 69  2bits(sqlite3_fi
11738 6c 65 20 2a 66 64 2c 20 69 36 34 20 6f 66 66 73  le *fd, i64 offs
11739 65 74 2c 20 75 33 32 20 2a 70 52 65 73 29 7b 0a  et, u32 *pRes){.
1173a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
1173b 61 63 5b 34 5d 3b 0a 20 20 69 6e 74 20 72 63 20  ac[4];.  int rc 
1173c 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28  = sqlite3OsRead(
1173d 66 64 2c 20 61 63 2c 20 73 69 7a 65 6f 66 28 61  fd, ac, sizeof(a
1173e 63 29 2c 20 6f 66 66 73 65 74 29 3b 0a 20 20 69  c), offset);.  i
1173f 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
11740 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20   ){.    *pRes = 
11741 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28  sqlite3Get4byte(
11742 61 63 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ac);.  }.  retur
11743 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57  n rc;.}../*.** W
11744 72 69 74 65 20 61 20 33 32 2d 62 69 74 20 69 6e  rite a 32-bit in
11745 74 65 67 65 72 20 69 6e 74 6f 20 61 20 73 74 72  teger into a str
11746 69 6e 67 20 62 75 66 66 65 72 20 69 6e 20 62 69  ing buffer in bi
11747 67 2d 65 6e 64 69 61 6e 20 62 79 74 65 20 6f 72  g-endian byte or
11748 64 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  der..*/.#define 
11749 70 75 74 33 32 62 69 74 73 28 41 2c 42 29 20 20  put32bits(A,B)  
1174a 73 71 6c 69 74 65 33 50 75 74 34 62 79 74 65 28  sqlite3Put4byte(
1174b 28 75 38 2a 29 41 2c 42 29 0a 0a 2f 2a 0a 2a 2a  (u8*)A,B)../*.**
1174c 20 57 72 69 74 65 20 61 20 33 32 2d 62 69 74 20   Write a 32-bit 
1174d 69 6e 74 65 67 65 72 20 69 6e 74 6f 20 74 68 65  integer into the
1174e 20 67 69 76 65 6e 20 66 69 6c 65 20 64 65 73 63   given file desc
1174f 72 69 70 74 6f 72 2e 20 20 52 65 74 75 72 6e 20  riptor.  Return 
11750 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20  SQLITE_OK.** on 
11751 73 75 63 63 65 73 73 20 6f 72 20 61 6e 20 65 72  success or an er
11752 72 6f 72 20 63 6f 64 65 20 69 73 20 73 6f 6d 65  ror code is some
11753 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67  thing goes wrong
11754 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
11755 77 72 69 74 65 33 32 62 69 74 73 28 73 71 6c 69  write32bits(sqli
11756 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c 20 69 36  te3_file *fd, i6
11757 34 20 6f 66 66 73 65 74 2c 20 75 33 32 20 76 61  4 offset, u32 va
11758 6c 29 7b 0a 20 20 63 68 61 72 20 61 63 5b 34 5d  l){.  char ac[4]
11759 3b 0a 20 20 70 75 74 33 32 62 69 74 73 28 61 63  ;.  put32bits(ac
1175a 2c 20 76 61 6c 29 3b 0a 20 20 72 65 74 75 72 6e  , val);.  return
1175b 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28   sqlite3OsWrite(
1175c 66 64 2c 20 61 63 2c 20 34 2c 20 6f 66 66 73 65  fd, ac, 4, offse
1175d 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  t);.}../*.** The
1175e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69   argument to thi
1175f 73 20 6d 61 63 72 6f 20 69 73 20 61 20 66 69 6c  s macro is a fil
11760 65 20 64 65 73 63 72 69 70 74 6f 72 20 28 74 79  e descriptor (ty
11761 70 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  pe sqlite3_file*
11762 29 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 30 20 69  )..** Return 0 i
11763 66 20 69 74 20 69 73 20 6e 6f 74 20 6f 70 65 6e  f it is not open
11764 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72 6f 20 28 62  , or non-zero (b
11765 75 74 20 6e 6f 74 20 31 29 20 69 66 20 69 74 20  ut not 1) if it 
11766 69 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  is..**.** This i
11767 73 20 73 6f 20 74 68 61 74 20 65 78 70 72 65 73  s so that expres
11768 73 69 6f 6e 73 20 63 61 6e 20 62 65 20 77 72 69  sions can be wri
11769 74 74 65 6e 20 61 73 3a 0a 2a 2a 0a 2a 2a 20 20  tten as:.**.**  
1176a 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67   if( isOpen(pPag
1176b 65 72 2d 3e 6a 66 64 29 20 29 7b 20 2e 2e 2e 0a  er->jfd) ){ ....
1176c 2a 2a 0a 2a 2a 20 69 6e 73 74 65 61 64 20 6f 66  **.** instead of
1176d 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 70 50 61  .**.**   if( pPa
1176e 67 65 72 2d 3e 6a 66 64 2d 3e 70 4d 65 74 68 6f  ger->jfd->pMetho
1176f 64 73 20 29 7b 20 2e 2e 2e 0a 2a 2f 0a 23 64 65  ds ){ ....*/.#de
11770 66 69 6e 65 20 69 73 4f 70 65 6e 28 70 46 64 29  fine isOpen(pFd)
11771 20 28 28 70 46 64 29 2d 3e 70 4d 65 74 68 6f 64   ((pFd)->pMethod
11772 73 29 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 66 69 6c  s)../*.** If fil
11773 65 20 70 46 64 20 69 73 20 6f 70 65 6e 2c 20 63  e pFd is open, c
11774 61 6c 6c 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c  all sqlite3OsUnl
11775 6f 63 6b 28 29 20 6f 6e 20 69 74 2e 0a 2a 2f 0a  ock() on it..*/.
11776 73 74 61 74 69 63 20 69 6e 74 20 6f 73 55 6e 6c  static int osUnl
11777 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
11778 20 2a 70 46 64 2c 20 69 6e 74 20 65 4c 6f 63 6b   *pFd, int eLock
11779 29 7b 0a 20 20 69 66 28 20 21 69 73 4f 70 65 6e  ){.  if( !isOpen
1177a 28 70 46 64 29 20 29 7b 0a 20 20 20 20 72 65 74  (pFd) ){.    ret
1177b 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
1177c 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69   }.  return sqli
1177d 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 70 46 64 2c  te3OsUnlock(pFd,
1177e 20 65 4c 6f 63 6b 29 3b 0a 7d 0a 0a 2f 2a 0a 2a   eLock);.}../*.*
1177f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
11780 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74 68  determines wheth
11781 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 61 74  er or not the at
11782 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d  omic-write optim
11783 69 7a 61 74 69 6f 6e 0a 2a 2a 20 63 61 6e 20 62  ization.** can b
11784 65 20 75 73 65 64 20 77 69 74 68 20 74 68 69 73  e used with this
11785 20 70 61 67 65 72 2e 20 54 68 65 20 6f 70 74 69   pager. The opti
11786 6d 69 7a 61 74 69 6f 6e 20 63 61 6e 20 62 65 20  mization can be 
11787 75 73 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20  used if:.**.**  
11788 28 61 29 20 74 68 65 20 76 61 6c 75 65 20 72 65  (a) the value re
11789 74 75 72 6e 65 64 20 62 79 20 4f 73 44 65 76 69  turned by OsDevi
1178a 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63  ceCharacteristic
1178b 73 28 29 20 69 6e 64 69 63 61 74 65 73 20 74 68  s() indicates th
1178c 61 74 0a 2a 2a 20 20 20 20 20 20 61 20 64 61 74  at.**      a dat
1178d 61 62 61 73 65 20 70 61 67 65 20 6d 61 79 20 62  abase page may b
1178e 65 20 77 72 69 74 74 65 6e 20 61 74 6f 6d 69 63  e written atomic
1178f 61 6c 6c 79 2c 20 61 6e 64 0a 2a 2a 20 20 28 62  ally, and.**  (b
11790 29 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75  ) the value retu
11791 72 6e 65 64 20 62 79 20 4f 73 53 65 63 74 6f 72  rned by OsSector
11792 53 69 7a 65 28 29 20 69 73 20 6c 65 73 73 20 74  Size() is less t
11793 68 61 6e 20 6f 72 20 65 71 75 61 6c 0a 2a 2a 20  han or equal.** 
11794 20 20 20 20 20 74 6f 20 74 68 65 20 70 61 67 65       to the page
11795 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   size..**.** The
11796 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73   optimization is
11797 20 61 6c 73 6f 20 61 6c 77 61 79 73 20 65 6e 61   also always ena
11798 62 6c 65 64 20 66 6f 72 20 74 65 6d 70 6f 72 61  bled for tempora
11799 72 79 20 66 69 6c 65 73 2e 20 49 74 20 69 73 0a  ry files. It is.
1179a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 74 6f 20 63  ** an error to c
1179b 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  all this functio
1179c 6e 20 69 66 20 70 50 61 67 65 72 20 69 73 20 6f  n if pPager is o
1179d 70 65 6e 65 64 20 6f 6e 20 61 6e 20 69 6e 2d 6d  pened on an in-m
1179e 65 6d 6f 72 79 0a 2a 2a 20 64 61 74 61 62 61 73  emory.** databas
1179f 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
117a0 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 63 61 6e  optimization can
117a1 6e 6f 74 20 62 65 20 75 73 65 64 2c 20 30 20 69  not be used, 0 i
117a2 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 69  s returned. If i
117a3 74 20 63 61 6e 20 62 65 20 75 73 65 64 2c 0a 2a  t can be used,.*
117a4 2a 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65  * then the value
117a5 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 65   returned is the
117a6 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75   size of the jou
117a7 72 6e 61 6c 20 66 69 6c 65 20 77 68 65 6e 20 69  rnal file when i
117a8 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 72 6f  t.** contains ro
117a9 6c 6c 62 61 63 6b 20 64 61 74 61 20 66 6f 72 20  llback data for 
117aa 65 78 61 63 74 6c 79 20 6f 6e 65 20 70 61 67 65  exactly one page
117ab 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
117ac 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43  TE_ENABLE_ATOMIC
117ad 5f 57 52 49 54 45 0a 73 74 61 74 69 63 20 69 6e  _WRITE.static in
117ae 74 20 6a 72 6e 6c 42 75 66 66 65 72 53 69 7a 65  t jrnlBufferSize
117af 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
117b0 0a 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44  .  assert( !MEMD
117b1 42 20 29 3b 0a 20 20 69 66 28 20 21 70 50 61 67  B );.  if( !pPag
117b2 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a  er->tempFile ){.
117b3 20 20 20 20 69 6e 74 20 64 63 3b 20 20 20 20 20      int dc;     
117b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
117b5 20 20 20 20 20 20 2f 2a 20 44 65 76 69 63 65 20        /* Device 
117b6 63 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20  characteristics 
117b7 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 53 65 63 74  */.    int nSect
117b8 6f 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  or;             
117b9 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63 74           /* Sect
117ba 6f 72 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 69  or size */.    i
117bb 6e 74 20 73 7a 50 61 67 65 3b 20 20 20 20 20 20  nt szPage;      
117bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
117bd 20 2f 2a 20 50 61 67 65 20 73 69 7a 65 20 2a 2f   /* Page size */
117be 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 73  ..    assert( is
117bf 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29  Open(pPager->fd)
117c0 20 29 3b 0a 20 20 20 20 64 63 20 3d 20 73 71 6c   );.    dc = sql
117c1 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 72  ite3OsDeviceChar
117c2 61 63 74 65 72 69 73 74 69 63 73 28 70 50 61 67  acteristics(pPag
117c3 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 6e 53 65  er->fd);.    nSe
117c4 63 74 6f 72 20 3d 20 70 50 61 67 65 72 2d 3e 73  ctor = pPager->s
117c5 65 63 74 6f 72 53 69 7a 65 3b 0a 20 20 20 20 73  ectorSize;.    s
117c6 7a 50 61 67 65 20 3d 20 70 50 61 67 65 72 2d 3e  zPage = pPager->
117c7 70 61 67 65 53 69 7a 65 3b 0a 0a 20 20 20 20 61  pageSize;..    a
117c8 73 73 65 72 74 28 53 51 4c 49 54 45 5f 49 4f 43  ssert(SQLITE_IOC
117c9 41 50 5f 41 54 4f 4d 49 43 35 31 32 3d 3d 28 35  AP_ATOMIC512==(5
117ca 31 32 3e 3e 38 29 29 3b 0a 20 20 20 20 61 73 73  12>>8));.    ass
117cb 65 72 74 28 53 51 4c 49 54 45 5f 49 4f 43 41 50  ert(SQLITE_IOCAP
117cc 5f 41 54 4f 4d 49 43 36 34 4b 3d 3d 28 36 35 35  _ATOMIC64K==(655
117cd 33 36 3e 3e 38 29 29 3b 0a 20 20 20 20 69 66 28  36>>8));.    if(
117ce 20 30 3d 3d 28 64 63 26 28 53 51 4c 49 54 45 5f   0==(dc&(SQLITE_
117cf 49 4f 43 41 50 5f 41 54 4f 4d 49 43 7c 28 73 7a  IOCAP_ATOMIC|(sz
117d0 50 61 67 65 3e 3e 38 29 29 20 7c 7c 20 6e 53 65  Page>>8)) || nSe
117d1 63 74 6f 72 3e 73 7a 50 61 67 65 29 20 29 7b 0a  ctor>szPage) ){.
117d2 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
117d3 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74      }.  }..  ret
117d4 75 72 6e 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f  urn JOURNAL_HDR_
117d5 53 5a 28 70 50 61 67 65 72 29 20 2b 20 4a 4f 55  SZ(pPager) + JOU
117d6 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65  RNAL_PG_SZ(pPage
117d7 72 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  r);.}.#endif../*
117d8 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 43 48  .** If SQLITE_CH
117d9 45 43 4b 5f 50 41 47 45 53 20 69 73 20 64 65 66  ECK_PAGES is def
117da 69 6e 65 64 20 74 68 65 6e 20 77 65 20 64 6f 20  ined then we do 
117db 73 6f 6d 65 20 73 61 6e 69 74 79 20 63 68 65 63  some sanity chec
117dc 6b 69 6e 67 0a 2a 2a 20 6f 6e 20 74 68 65 20 63  king.** on the c
117dd 61 63 68 65 20 75 73 69 6e 67 20 61 20 68 61 73  ache using a has
117de 68 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69  h function.  Thi
117df 73 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65  s is used for te
117e0 73 74 69 6e 67 0a 2a 2a 20 61 6e 64 20 64 65 62  sting.** and deb
117e1 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a  ugging only..*/.
117e2 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48  #ifdef SQLITE_CH
117e3 45 43 4b 5f 50 41 47 45 53 0a 2f 2a 0a 2a 2a 20  ECK_PAGES./*.** 
117e4 52 65 74 75 72 6e 20 61 20 33 32 2d 62 69 74 20  Return a 32-bit 
117e5 68 61 73 68 20 6f 66 20 74 68 65 20 70 61 67 65  hash of the page
117e6 20 64 61 74 61 20 66 6f 72 20 70 50 61 67 65 2e   data for pPage.
117e7 0a 2a 2f 0a 73 74 61 74 69 63 20 75 33 32 20 70  .*/.static u32 p
117e8 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 69 6e  ager_datahash(in
117e9 74 20 6e 42 79 74 65 2c 20 75 6e 73 69 67 6e 65  t nByte, unsigne
117ea 64 20 63 68 61 72 20 2a 70 44 61 74 61 29 7b 0a  d char *pData){.
117eb 20 20 75 33 32 20 68 61 73 68 20 3d 20 30 3b 0a    u32 hash = 0;.
117ec 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69    int i;.  for(i
117ed 3d 30 3b 20 69 3c 6e 42 79 74 65 3b 20 69 2b 2b  =0; i<nByte; i++
117ee 29 7b 0a 20 20 20 20 68 61 73 68 20 3d 20 28 68  ){.    hash = (h
117ef 61 73 68 2a 31 30 33 39 29 20 2b 20 70 44 61 74  ash*1039) + pDat
117f0 61 5b 69 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75  a[i];.  }.  retu
117f1 72 6e 20 68 61 73 68 3b 0a 7d 0a 73 74 61 74 69  rn hash;.}.stati
117f2 63 20 75 33 32 20 70 61 67 65 72 5f 70 61 67 65  c u32 pager_page
117f3 68 61 73 68 28 50 67 48 64 72 20 2a 70 50 61 67  hash(PgHdr *pPag
117f4 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 61 67  e){.  return pag
117f5 65 72 5f 64 61 74 61 68 61 73 68 28 70 50 61 67  er_datahash(pPag
117f6 65 2d 3e 70 50 61 67 65 72 2d 3e 70 61 67 65 53  e->pPager->pageS
117f7 69 7a 65 2c 20 28 75 6e 73 69 67 6e 65 64 20 63  ize, (unsigned c
117f8 68 61 72 20 2a 29 70 50 61 67 65 2d 3e 70 44 61  har *)pPage->pDa
117f9 74 61 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  ta);.}.static vo
117fa 69 64 20 70 61 67 65 72 5f 73 65 74 5f 70 61 67  id pager_set_pag
117fb 65 68 61 73 68 28 50 67 48 64 72 20 2a 70 50 61  ehash(PgHdr *pPa
117fc 67 65 29 7b 0a 20 20 70 50 61 67 65 2d 3e 70 61  ge){.  pPage->pa
117fd 67 65 48 61 73 68 20 3d 20 70 61 67 65 72 5f 70  geHash = pager_p
117fe 61 67 65 68 61 73 68 28 70 50 61 67 65 29 3b 0a  agehash(pPage);.
117ff 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 43 48 45  }../*.** The CHE
11800 43 4b 5f 50 41 47 45 20 6d 61 63 72 6f 20 74 61  CK_PAGE macro ta
11801 6b 65 73 20 61 20 50 67 48 64 72 2a 20 61 73 20  kes a PgHdr* as 
11802 61 6e 20 61 72 67 75 6d 65 6e 74 2e 20 49 66 20  an argument. If 
11803 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47  SQLITE_CHECK_PAG
11804 45 53 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 64  ES.** is defined
11805 2c 20 61 6e 64 20 4e 44 45 42 55 47 20 69 73 20  , and NDEBUG is 
11806 6e 6f 74 20 64 65 66 69 6e 65 64 2c 20 61 6e 20  not defined, an 
11807 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65  assert() stateme
11808 6e 74 20 63 68 65 63 6b 73 0a 2a 2a 20 74 68 61  nt checks.** tha
11809 74 20 74 68 65 20 70 61 67 65 20 69 73 20 65 69  t the page is ei
1180a 74 68 65 72 20 64 69 72 74 79 20 6f 72 20 73 74  ther dirty or st
1180b 69 6c 6c 20 6d 61 74 63 68 65 73 20 74 68 65 20  ill matches the 
1180c 63 61 6c 63 75 6c 61 74 65 64 20 70 61 67 65 2d  calculated page-
1180d 68 61 73 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  hash..*/.#define
1180e 20 43 48 45 43 4b 5f 50 41 47 45 28 78 29 20 63   CHECK_PAGE(x) c
1180f 68 65 63 6b 50 61 67 65 28 78 29 0a 73 74 61 74  heckPage(x).stat
11810 69 63 20 76 6f 69 64 20 63 68 65 63 6b 50 61 67  ic void checkPag
11811 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20  e(PgHdr *pPg){. 
11812 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d   Pager *pPager =
11813 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20   pPg->pPager;.  
11814 61 73 73 65 72 74 28 20 21 70 50 67 2d 3e 70 61  assert( !pPg->pa
11815 67 65 48 61 73 68 20 7c 7c 20 70 50 61 67 65 72  geHash || pPager
11816 2d 3e 65 72 72 43 6f 64 65 0a 20 20 20 20 20 20  ->errCode.      
11817 7c 7c 20 28 70 50 67 2d 3e 66 6c 61 67 73 26 50  || (pPg->flags&P
11818 47 48 44 52 5f 44 49 52 54 59 29 20 7c 7c 20 70  GHDR_DIRTY) || p
11819 50 67 2d 3e 70 61 67 65 48 61 73 68 3d 3d 70 61  Pg->pageHash==pa
1181a 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67  ger_pagehash(pPg
1181b 29 20 29 3b 0a 7d 0a 0a 23 65 6c 73 65 0a 23 64  ) );.}..#else.#d
1181c 65 66 69 6e 65 20 70 61 67 65 72 5f 64 61 74 61  efine pager_data
1181d 68 61 73 68 28 58 2c 59 29 20 20 30 0a 23 64 65  hash(X,Y)  0.#de
1181e 66 69 6e 65 20 70 61 67 65 72 5f 70 61 67 65 68  fine pager_pageh
1181f 61 73 68 28 58 29 20 20 30 0a 23 64 65 66 69 6e  ash(X)  0.#defin
11820 65 20 43 48 45 43 4b 5f 50 41 47 45 28 78 29 0a  e CHECK_PAGE(x).
11821 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 54  #endif  /* SQLIT
11822 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 20 2a 2f  E_CHECK_PAGES */
11823 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69  ../*.** When thi
11824 73 20 69 73 20 63 61 6c 6c 65 64 20 74 68 65 20  s is called the 
11825 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72  journal file for
11826 20 70 61 67 65 72 20 70 50 61 67 65 72 20 6d 75   pager pPager mu
11827 73 74 20 62 65 20 6f 70 65 6e 2e 0a 2a 2a 20 54  st be open..** T
11828 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 74 74  his function att
11829 65 6d 70 74 73 20 74 6f 20 72 65 61 64 20 61 20  empts to read a 
1182a 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
1182b 69 6c 65 20 6e 61 6d 65 20 66 72 6f 6d 20 74 68  ile name from th
1182c 65 20 0a 2a 2a 20 65 6e 64 20 6f 66 20 74 68 65  e .** end of the
1182d 20 66 69 6c 65 20 61 6e 64 2c 20 69 66 20 73 75   file and, if su
1182e 63 63 65 73 73 66 75 6c 2c 20 63 6f 70 69 65 73  ccessful, copies
1182f 20 69 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20   it into memory 
11830 73 75 70 70 6c 69 65 64 20 0a 2a 2a 20 62 79 20  supplied .** by 
11831 74 68 65 20 63 61 6c 6c 65 72 2e 20 53 65 65 20  the caller. See 
11832 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20 77  comments above w
11833 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61  riteMasterJourna
11834 6c 28 29 20 66 6f 72 20 74 68 65 20 66 6f 72 6d  l() for the form
11835 61 74 0a 2a 2a 20 75 73 65 64 20 74 6f 20 73 74  at.** used to st
11836 6f 72 65 20 61 20 6d 61 73 74 65 72 20 6a 6f 75  ore a master jou
11837 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 61  rnal file name a
11838 74 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20 6a  t the end of a j
11839 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2a 0a  ournal file..**.
1183a 2a 2a 20 7a 4d 61 73 74 65 72 20 6d 75 73 74 20  ** zMaster must 
1183b 70 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65  point to a buffe
1183c 72 20 6f 66 20 61 74 20 6c 65 61 73 74 20 6e 4d  r of at least nM
1183d 61 73 74 65 72 20 62 79 74 65 73 20 61 6c 6c 6f  aster bytes allo
1183e 63 61 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20  cated by.** the 
1183f 63 61 6c 6c 65 72 2e 20 54 68 69 73 20 73 68 6f  caller. This sho
11840 75 6c 64 20 62 65 20 73 71 6c 69 74 65 33 5f 76  uld be sqlite3_v
11841 66 73 2e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 20  fs.mxPathname+1 
11842 28 74 6f 20 65 6e 73 75 72 65 20 74 68 65 72 65  (to ensure there
11843 20 69 73 0a 2a 2a 20 65 6e 6f 75 67 68 20 73 70   is.** enough sp
11844 61 63 65 20 74 6f 20 77 72 69 74 65 20 74 68 65  ace to write the
11845 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
11846 6e 61 6d 65 29 2e 20 49 66 20 74 68 65 20 6d 61  name). If the ma
11847 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20  ster journal.** 
11848 6e 61 6d 65 20 69 6e 20 74 68 65 20 6a 6f 75 72  name in the jour
11849 6e 61 6c 20 69 73 20 6c 6f 6e 67 65 72 20 74 68  nal is longer th
1184a 61 6e 20 6e 4d 61 73 74 65 72 20 62 79 74 65 73  an nMaster bytes
1184b 20 28 69 6e 63 6c 75 64 69 6e 67 20 61 0a 2a 2a   (including a.**
1184c 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 29   nul-terminator)
1184d 2c 20 74 68 65 6e 20 74 68 69 73 20 69 73 20 68  , then this is h
1184e 61 6e 64 6c 65 64 20 61 73 20 69 66 20 6e 6f 20  andled as if no 
1184f 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e  master journal n
11850 61 6d 65 0a 2a 2a 20 77 65 72 65 20 70 72 65 73  ame.** were pres
11851 65 6e 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e  ent in the journ
11852 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d  al..**.** If a m
11853 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
11854 6c 65 20 6e 61 6d 65 20 69 73 20 70 72 65 73 65  le name is prese
11855 6e 74 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66  nt at the end of
11856 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20   the journal.** 
11857 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 69 73  file, then it is
11858 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65   copied into the
11859 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20   buffer pointed 
1185a 74 6f 20 62 79 20 7a 4d 61 73 74 65 72 2e 20 41  to by zMaster. A
1185b 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74  .** nul-terminat
1185c 6f 72 20 62 79 74 65 20 69 73 20 61 70 70 65 6e  or byte is appen
1185d 64 65 64 20 74 6f 20 74 68 65 20 62 75 66 66 65  ded to the buffe
1185e 72 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20  r following the 
1185f 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 6e 61  master.** journa
11860 6c 20 66 69 6c 65 20 6e 61 6d 65 2e 0a 2a 2a 0a  l file name..**.
11861 2a 2a 20 49 66 20 69 74 20 69 73 20 64 65 74 65  ** If it is dete
11862 72 6d 69 6e 65 64 20 74 68 61 74 20 6e 6f 20 6d  rmined that no m
11863 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
11864 6c 65 20 6e 61 6d 65 20 69 73 20 70 72 65 73 65  le name is prese
11865 6e 74 20 0a 2a 2a 20 7a 4d 61 73 74 65 72 5b 30  nt .** zMaster[0
11866 5d 20 69 73 20 73 65 74 20 74 6f 20 30 20 61 6e  ] is set to 0 an
11867 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75  d SQLITE_OK retu
11868 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  rned..**.** If a
11869 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77  n error occurs w
1186a 68 69 6c 65 20 72 65 61 64 69 6e 67 20 66 72 6f  hile reading fro
1186b 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  m the journal fi
1186c 6c 65 2c 20 61 6e 20 53 51 4c 69 74 65 0a 2a 2a  le, an SQLite.**
1186d 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72   error code is r
1186e 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74  eturned..*/.stat
1186f 69 63 20 69 6e 74 20 72 65 61 64 4d 61 73 74 65  ic int readMaste
11870 72 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 74 65 33  rJournal(sqlite3
11871 5f 66 69 6c 65 20 2a 70 4a 72 6e 6c 2c 20 63 68  _file *pJrnl, ch
11872 61 72 20 2a 7a 4d 61 73 74 65 72 2c 20 75 33 32  ar *zMaster, u32
11873 20 6e 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e 74   nMaster){.  int
11874 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
11875 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
11876 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 75 33 32 20  n code */.  u32 
11877 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  len;            
11878 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68         /* Length
11879 20 69 6e 20 62 79 74 65 73 20 6f 66 20 6d 61 73   in bytes of mas
1187a 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65  ter journal name
1187b 20 2a 2f 0a 20 20 69 36 34 20 73 7a 4a 3b 20 20   */.  i64 szJ;  
1187c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1187d 20 2f 2a 20 54 6f 74 61 6c 20 73 69 7a 65 20 69   /* Total size i
1187e 6e 20 62 79 74 65 73 20 6f 66 20 6a 6f 75 72 6e  n bytes of journ
1187f 61 6c 20 66 69 6c 65 20 70 4a 72 6e 6c 20 2a 2f  al file pJrnl */
11880 0a 20 20 75 33 32 20 63 6b 73 75 6d 3b 20 20 20  .  u32 cksum;   
11881 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
11882 20 4d 4a 20 63 68 65 63 6b 73 75 6d 20 76 61 6c   MJ checksum val
11883 75 65 20 72 65 61 64 20 66 72 6f 6d 20 6a 6f 75  ue read from jou
11884 72 6e 61 6c 20 2a 2f 0a 20 20 75 33 32 20 75 3b  rnal */.  u32 u;
11885 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11886 20 20 20 20 20 2f 2a 20 55 6e 73 69 67 6e 65 64       /* Unsigned
11887 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   loop counter */
11888 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
11889 20 61 4d 61 67 69 63 5b 38 5d 3b 20 20 20 2f 2a   aMagic[8];   /*
1188a 20 41 20 62 75 66 66 65 72 20 74 6f 20 68 6f 6c   A buffer to hol
1188b 64 20 74 68 65 20 6d 61 67 69 63 20 68 65 61 64  d the magic head
1188c 65 72 20 2a 2f 0a 20 20 7a 4d 61 73 74 65 72 5b  er */.  zMaster[
1188d 30 5d 20 3d 20 27 5c 30 27 3b 0a 0a 20 20 69 66  0] = '\0';..  if
1188e 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  ( SQLITE_OK!=(rc
1188f 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65   = sqlite3OsFile
11890 53 69 7a 65 28 70 4a 72 6e 6c 2c 20 26 73 7a 4a  Size(pJrnl, &szJ
11891 29 29 0a 20 20 20 7c 7c 20 73 7a 4a 3c 31 36 0a  )).   || szJ<16.
11892 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21     || SQLITE_OK!
11893 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62 69 74  =(rc = read32bit
11894 73 28 70 4a 72 6e 6c 2c 20 73 7a 4a 2d 31 36 2c  s(pJrnl, szJ-16,
11895 20 26 6c 65 6e 29 29 0a 20 20 20 7c 7c 20 6c 65   &len)).   || le
11896 6e 3e 3d 6e 4d 61 73 74 65 72 20 0a 20 20 20 7c  n>=nMaster .   |
11897 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  | SQLITE_OK!=(rc
11898 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 70 4a   = read32bits(pJ
11899 72 6e 6c 2c 20 73 7a 4a 2d 31 32 2c 20 26 63 6b  rnl, szJ-12, &ck
1189a 73 75 6d 29 29 0a 20 20 20 7c 7c 20 53 51 4c 49  sum)).   || SQLI
1189b 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c  TE_OK!=(rc = sql
1189c 69 74 65 33 4f 73 52 65 61 64 28 70 4a 72 6e 6c  ite3OsRead(pJrnl
1189d 2c 20 61 4d 61 67 69 63 2c 20 38 2c 20 73 7a 4a  , aMagic, 8, szJ
1189e 2d 38 29 29 0a 20 20 20 7c 7c 20 6d 65 6d 63 6d  -8)).   || memcm
1189f 70 28 61 4d 61 67 69 63 2c 20 61 4a 6f 75 72 6e  p(aMagic, aJourn
118a0 61 6c 4d 61 67 69 63 2c 20 38 29 0a 20 20 20 7c  alMagic, 8).   |
118a1 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  | SQLITE_OK!=(rc
118a2 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64   = sqlite3OsRead
118a3 28 70 4a 72 6e 6c 2c 20 7a 4d 61 73 74 65 72 2c  (pJrnl, zMaster,
118a4 20 6c 65 6e 2c 20 73 7a 4a 2d 31 36 2d 6c 65 6e   len, szJ-16-len
118a5 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75  )).  ){.    retu
118a6 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  rn rc;.  }..  /*
118a7 20 53 65 65 20 69 66 20 74 68 65 20 63 68 65 63   See if the chec
118a8 6b 73 75 6d 20 6d 61 74 63 68 65 73 20 74 68 65  ksum matches the
118a9 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
118aa 6e 61 6d 65 20 2a 2f 0a 20 20 66 6f 72 28 75 3d  name */.  for(u=
118ab 30 3b 20 75 3c 6c 65 6e 3b 20 75 2b 2b 29 7b 0a  0; u<len; u++){.
118ac 20 20 20 20 63 6b 73 75 6d 20 2d 3d 20 7a 4d 61      cksum -= zMa
118ad 73 74 65 72 5b 75 5d 3b 0a 20 20 7d 0a 20 20 69  ster[u];.  }.  i
118ae 66 28 20 63 6b 73 75 6d 20 29 7b 0a 20 20 20 20  f( cksum ){.    
118af 2f 2a 20 49 66 20 74 68 65 20 63 68 65 63 6b 73  /* If the checks
118b0 75 6d 20 64 6f 65 73 6e 27 74 20 61 64 64 20 75  um doesn't add u
118b1 70 2c 20 74 68 65 6e 20 6f 6e 65 20 6f 72 20 6d  p, then one or m
118b2 6f 72 65 20 6f 66 20 74 68 65 20 64 69 73 6b 20  ore of the disk 
118b3 73 65 63 74 6f 72 73 0a 20 20 20 20 2a 2a 20 63  sectors.    ** c
118b4 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6d 61  ontaining the ma
118b5 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
118b6 65 6e 61 6d 65 20 69 73 20 63 6f 72 72 75 70 74  ename is corrupt
118b7 65 64 2e 20 54 68 69 73 20 6d 65 61 6e 73 0a 20  ed. This means. 
118b8 20 20 20 2a 2a 20 64 65 66 69 6e 69 74 65 6c 79     ** definitely
118b9 20 72 6f 6c 6c 20 62 61 63 6b 2c 20 73 6f 20 6a   roll back, so j
118ba 75 73 74 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ust return SQLIT
118bb 45 5f 4f 4b 20 61 6e 64 20 72 65 70 6f 72 74 20  E_OK and report 
118bc 61 20 28 6e 75 6c 29 0a 20 20 20 20 2a 2a 20 6d  a (nul).    ** m
118bd 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 69  aster-journal fi
118be 6c 65 6e 61 6d 65 2e 0a 20 20 20 20 2a 2f 0a 20  lename..    */. 
118bf 20 20 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 7d 0a     len = 0;.  }.
118c0 20 20 7a 4d 61 73 74 65 72 5b 6c 65 6e 5d 20 3d    zMaster[len] =
118c1 20 27 5c 30 27 3b 0a 20 20 20 0a 20 20 72 65 74   '\0';.   .  ret
118c2 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
118c3 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
118c4 68 65 20 6f 66 66 73 65 74 20 6f 66 20 74 68 65  he offset of the
118c5 20 73 65 63 74 6f 72 20 62 6f 75 6e 64 61 72 79   sector boundary
118c6 20 61 74 20 6f 72 20 69 6d 6d 65 64 69 61 74 65   at or immediate
118c7 6c 79 20 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67  ly .** following
118c8 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 70 50   the value in pP
118c9 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
118ca 2c 20 61 73 73 75 6d 69 6e 67 20 61 20 73 65 63  , assuming a sec
118cb 74 6f 72 20 0a 2a 2a 20 73 69 7a 65 20 6f 66 20  tor .** size of 
118cc 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69  pPager->sectorSi
118cd 7a 65 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20  ze bytes..**.** 
118ce 69 2e 65 20 66 6f 72 20 61 20 73 65 63 74 6f 72  i.e for a sector
118cf 20 73 69 7a 65 20 6f 66 20 35 31 32 3a 0a 2a 2a   size of 512:.**
118d0 0a 2a 2a 20 20 20 50 61 67 65 72 2e 6a 6f 75 72  .**   Pager.jour
118d1 6e 61 6c 4f 66 66 20 20 20 20 20 20 20 20 20 20  nalOff          
118d2 52 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20  Return value.** 
118d3 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    --------------
118d4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
118d5 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 30  ---------.**   0
118d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
118d7 20 20 20 20 20 20 20 20 20 30 0a 2a 2a 20 20 20           0.**   
118d8 35 31 32 20 20 20 20 20 20 20 20 20 20 20 20 20  512             
118d9 20 20 20 20 20 20 20 20 20 20 35 31 32 0a 2a 2a            512.**
118da 20 20 20 31 30 30 20 20 20 20 20 20 20 20 20 20     100          
118db 20 20 20 20 20 20 20 20 20 20 20 20 20 35 31 32               512
118dc 0a 2a 2a 20 20 20 32 30 30 30 20 20 20 20 20 20  .**   2000      
118dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
118de 32 30 34 38 0a 2a 2a 20 0a 2a 2f 0a 73 74 61 74  2048.** .*/.stat
118df 69 63 20 69 36 34 20 6a 6f 75 72 6e 61 6c 48 64  ic i64 journalHd
118e0 72 4f 66 66 73 65 74 28 50 61 67 65 72 20 2a 70  rOffset(Pager *p
118e1 50 61 67 65 72 29 7b 0a 20 20 69 36 34 20 6f 66  Pager){.  i64 of
118e2 66 73 65 74 20 3d 20 30 3b 0a 20 20 69 36 34 20  fset = 0;.  i64 
118e3 63 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  c = pPager->jour
118e4 6e 61 6c 4f 66 66 3b 0a 20 20 69 66 28 20 63 20  nalOff;.  if( c 
118e5 29 7b 0a 20 20 20 20 6f 66 66 73 65 74 20 3d 20  ){.    offset = 
118e6 28 28 63 2d 31 29 2f 4a 4f 55 52 4e 41 4c 5f 48  ((c-1)/JOURNAL_H
118e7 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 2b 20  DR_SZ(pPager) + 
118e8 31 29 20 2a 20 4a 4f 55 52 4e 41 4c 5f 48 44 52  1) * JOURNAL_HDR
118e9 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 7d  _SZ(pPager);.  }
118ea 0a 20 20 61 73 73 65 72 74 28 20 6f 66 66 73 65  .  assert( offse
118eb 74 25 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a  t%JOURNAL_HDR_SZ
118ec 28 70 50 61 67 65 72 29 3d 3d 30 20 29 3b 0a 20  (pPager)==0 );. 
118ed 20 61 73 73 65 72 74 28 20 6f 66 66 73 65 74 3e   assert( offset>
118ee 3d 63 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =c );.  assert( 
118ef 28 6f 66 66 73 65 74 2d 63 29 3c 4a 4f 55 52 4e  (offset-c)<JOURN
118f0 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72  AL_HDR_SZ(pPager
118f1 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 6f 66  ) );.  return of
118f2 66 73 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  fset;.}../*.** T
118f3 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
118f4 6d 75 73 74 20 62 65 20 6f 70 65 6e 20 77 68 65  must be open whe
118f5 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
118f6 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a  is called..**.**
118f7 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
118f8 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65  s a no-op if the
118f9 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61   journal file ha
118fa 73 20 6e 6f 74 20 62 65 65 6e 20 77 72 69 74 74  s not been writt
118fb 65 6e 20 74 6f 0a 2a 2a 20 77 69 74 68 69 6e 20  en to.** within 
118fc 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e  the current tran
118fd 73 61 63 74 69 6f 6e 20 28 69 2e 65 2e 20 69 66  saction (i.e. if
118fe 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66   Pager.journalOf
118ff 66 3d 3d 30 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  f==0)..**.** If 
11900 64 6f 54 72 75 6e 63 61 74 65 20 69 73 20 6e 6f  doTruncate is no
11901 6e 2d 7a 65 72 6f 20 6f 72 20 74 68 65 20 50 61  n-zero or the Pa
11902 67 65 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c  ger.journalSizeL
11903 69 6d 69 74 20 76 61 72 69 61 62 6c 65 20 69 73  imit variable is
11904 0a 2a 2a 20 73 65 74 20 74 6f 20 30 2c 20 74 68  .** set to 0, th
11905 65 6e 20 74 72 75 6e 63 61 74 65 20 74 68 65 20  en truncate the 
11906 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20  journal file to 
11907 7a 65 72 6f 20 62 79 74 65 73 20 69 6e 20 73 69  zero bytes in si
11908 7a 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a 2a  ze. Otherwise,.*
11909 2a 20 7a 65 72 6f 20 74 68 65 20 32 38 2d 62 79  * zero the 28-by
1190a 74 65 20 68 65 61 64 65 72 20 61 74 20 74 68 65  te header at the
1190b 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 6a 6f   start of the jo
1190c 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 6e 20 65  urnal file. In e
1190d 69 74 68 65 72 20 63 61 73 65 2c 20 0a 2a 2a 20  ither case, .** 
1190e 69 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20  if the pager is 
1190f 6e 6f 74 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d  not in no-sync m
11910 6f 64 65 2c 20 73 79 6e 63 20 74 68 65 20 6a 6f  ode, sync the jo
11911 75 72 6e 61 6c 20 66 69 6c 65 20 69 6d 6d 65 64  urnal file immed
11912 69 61 74 65 6c 79 20 0a 2a 2a 20 61 66 74 65 72  iately .** after
11913 20 77 72 69 74 69 6e 67 20 6f 72 20 74 72 75 6e   writing or trun
11914 63 61 74 69 6e 67 20 69 74 2e 0a 2a 2a 0a 2a 2a  cating it..**.**
11915 20 49 66 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61   If Pager.journa
11916 6c 53 69 7a 65 4c 69 6d 69 74 20 69 73 20 73 65  lSizeLimit is se
11917 74 20 74 6f 20 61 20 70 6f 73 69 74 69 76 65 2c  t to a positive,
11918 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2c   non-zero value,
11919 20 61 6e 64 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e   and.** followin
1191a 67 20 74 68 65 20 74 72 75 6e 63 61 74 69 6f 6e  g the truncation
1191b 20 6f 72 20 7a 65 72 6f 69 6e 67 20 64 65 73 63   or zeroing desc
1191c 72 69 62 65 64 20 61 62 6f 76 65 20 74 68 65 20  ribed above the 
1191d 73 69 7a 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20  size of the .** 
1191e 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20  journal file in 
1191f 62 79 74 65 73 20 69 73 20 6c 61 72 67 65 72 20  bytes is larger 
11920 74 68 61 6e 20 74 68 69 73 20 76 61 6c 75 65 2c  than this value,
11921 20 74 68 65 6e 20 74 72 75 6e 63 61 74 65 20 74   then truncate t
11922 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69  he.** journal fi
11923 6c 65 20 74 6f 20 50 61 67 65 72 2e 6a 6f 75 72  le to Pager.jour
11924 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 62 79 74  nalSizeLimit byt
11925 65 73 2e 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20  es. The journal 
11926 66 69 6c 65 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74  file does.** not
11927 20 6e 65 65 64 20 74 6f 20 62 65 20 73 79 6e 63   need to be sync
11928 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 69  ed following thi
11929 73 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a 0a  s operation..**.
1192a 2a 2a 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f  ** If an IO erro
1192b 72 20 6f 63 63 75 72 73 2c 20 61 62 61 6e 64 6f  r occurs, abando
1192c 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 61 6e 64  n processing and
1192d 20 72 65 74 75 72 6e 20 74 68 65 20 49 4f 20 65   return the IO e
1192e 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 20 4f 74  rror code..** Ot
1192f 68 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e 20  herwise, return 
11930 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 73 74  SQLITE_OK..*/.st
11931 61 74 69 63 20 69 6e 74 20 7a 65 72 6f 4a 6f 75  atic int zeroJou
11932 72 6e 61 6c 48 64 72 28 50 61 67 65 72 20 2a 70  rnalHdr(Pager *p
11933 50 61 67 65 72 2c 20 69 6e 74 20 64 6f 54 72 75  Pager, int doTru
11934 6e 63 61 74 65 29 7b 0a 20 20 69 6e 74 20 72 63  ncate){.  int rc
11935 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20   = SQLITE_OK;   
11936 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11937 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
11938 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
11939 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70  assert( isOpen(p
1193a 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 20  Pager->jfd) );. 
1193b 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75   if( pPager->jou
1193c 72 6e 61 6c 4f 66 66 20 29 7b 0a 20 20 20 20 63  rnalOff ){.    c
1193d 6f 6e 73 74 20 69 36 34 20 69 4c 69 6d 69 74 20  onst i64 iLimit 
1193e 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  = pPager->journa
1193f 6c 53 69 7a 65 4c 69 6d 69 74 3b 20 20 20 20 2f  lSizeLimit;    /
11940 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f 66  * Local cache of
11941 20 6a 73 6c 20 2a 2f 0a 0a 20 20 20 20 49 4f 54   jsl */..    IOT
11942 52 41 43 45 28 28 22 4a 5a 45 52 4f 48 44 52 20  RACE(("JZEROHDR 
11943 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0a  %p\n", pPager)).
11944 20 20 20 20 69 66 28 20 64 6f 54 72 75 6e 63 61      if( doTrunca
11945 74 65 20 7c 7c 20 69 4c 69 6d 69 74 3d 3d 30 20  te || iLimit==0 
11946 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ){.      rc = sq
11947 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28  lite3OsTruncate(
11948 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 30 29 3b  pPager->jfd, 0);
11949 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1194a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63    static const c
1194b 68 61 72 20 7a 65 72 6f 48 64 72 5b 32 38 5d 20  har zeroHdr[28] 
1194c 3d 20 7b 30 7d 3b 0a 20 20 20 20 20 20 72 63 20  = {0};.      rc 
1194d 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65  = sqlite3OsWrite
1194e 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 65  (pPager->jfd, ze
1194f 72 6f 48 64 72 2c 20 73 69 7a 65 6f 66 28 7a 65  roHdr, sizeof(ze
11950 72 6f 48 64 72 29 2c 20 30 29 3b 0a 20 20 20 20  roHdr), 0);.    
11951 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  }.    if( rc==SQ
11952 4c 49 54 45 5f 4f 4b 20 26 26 20 21 70 50 61 67  LITE_OK && !pPag
11953 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 20  er->noSync ){.  
11954 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
11955 4f 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 6a  OsSync(pPager->j
11956 66 64 2c 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f  fd, SQLITE_SYNC_
11957 44 41 54 41 4f 4e 4c 59 7c 70 50 61 67 65 72 2d  DATAONLY|pPager-
11958 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a 20 20  >sync_flags);.  
11959 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 74 20 74    }..    /* At t
1195a 68 69 73 20 70 6f 69 6e 74 20 74 68 65 20 74 72  his point the tr
1195b 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d  ansaction is com
1195c 6d 69 74 74 65 64 20 62 75 74 20 74 68 65 20 77  mitted but the w
1195d 72 69 74 65 20 6c 6f 63 6b 20 0a 20 20 20 20 2a  rite lock .    *
1195e 2a 20 69 73 20 73 74 69 6c 6c 20 68 65 6c 64 20  * is still held 
1195f 6f 6e 20 74 68 65 20 66 69 6c 65 2e 20 49 66 20  on the file. If 
11960 74 68 65 72 65 20 69 73 20 61 20 73 69 7a 65 20  there is a size 
11961 6c 69 6d 69 74 20 63 6f 6e 66 69 67 75 72 65 64  limit configured
11962 20 66 6f 72 20 0a 20 20 20 20 2a 2a 20 74 68 65   for .    ** the
11963 20 70 65 72 73 69 73 74 65 6e 74 20 6a 6f 75 72   persistent jour
11964 6e 61 6c 20 61 6e 64 20 74 68 65 20 6a 6f 75 72  nal and the jour
11965 6e 61 6c 20 66 69 6c 65 20 63 75 72 72 65 6e 74  nal file current
11966 6c 79 20 63 6f 6e 73 75 6d 65 73 20 6d 6f 72 65  ly consumes more
11967 0a 20 20 20 20 2a 2a 20 73 70 61 63 65 20 74 68  .    ** space th
11968 61 6e 20 74 68 61 74 20 6c 69 6d 69 74 20 61 6c  an that limit al
11969 6c 6f 77 73 20 66 6f 72 2c 20 74 72 75 6e 63 61  lows for, trunca
1196a 74 65 20 69 74 20 6e 6f 77 2e 20 54 68 65 72 65  te it now. There
1196b 20 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20 20 20   is no need.    
1196c 2a 2a 20 74 6f 20 73 79 6e 63 20 74 68 65 20 66  ** to sync the f
1196d 69 6c 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68  ile following th
1196e 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 20 20  is operation..  
1196f 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d    */.    if( rc=
11970 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 4c  =SQLITE_OK && iL
11971 69 6d 69 74 3e 30 20 29 7b 0a 20 20 20 20 20 20  imit>0 ){.      
11972 69 36 34 20 73 7a 3b 0a 20 20 20 20 20 20 72 63  i64 sz;.      rc
11973 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65   = sqlite3OsFile
11974 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a 66 64  Size(pPager->jfd
11975 2c 20 26 73 7a 29 3b 0a 20 20 20 20 20 20 69 66  , &sz);.      if
11976 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
11977 26 26 20 73 7a 3e 69 4c 69 6d 69 74 20 29 7b 0a  && sz>iLimit ){.
11978 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
11979 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70  ite3OsTruncate(p
1197a 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 4c 69 6d  Pager->jfd, iLim
1197b 69 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  it);.      }.   
1197c 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
1197d 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  rc;.}../*.** The
1197e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75   journal file mu
1197f 73 74 20 62 65 20 6f 70 65 6e 20 77 68 65 6e 20  st be open when 
11980 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
11981 63 61 6c 6c 65 64 2e 20 41 20 6a 6f 75 72 6e 61  called. A journa
11982 6c 0a 2a 2a 20 68 65 61 64 65 72 20 28 4a 4f 55  l.** header (JOU
11983 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 79 74 65  RNAL_HDR_SZ byte
11984 73 29 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e  s) is written in
11985 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  to the journal f
11986 69 6c 65 20 61 74 20 74 68 65 0a 2a 2a 20 63 75  ile at the.** cu
11987 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 2e 0a  rrent location..
11988 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74  **.** The format
11989 20 66 6f 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c   for the journal
1198a 20 68 65 61 64 65 72 20 69 73 20 61 73 20 66 6f   header is as fo
1198b 6c 6c 6f 77 73 3a 0a 2a 2a 20 2d 20 38 20 62 79  llows:.** - 8 by
1198c 74 65 73 3a 20 4d 61 67 69 63 20 69 64 65 6e 74  tes: Magic ident
1198d 69 66 79 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 66  ifying journal f
1198e 6f 72 6d 61 74 2e 0a 2a 2a 20 2d 20 34 20 62 79  ormat..** - 4 by
1198f 74 65 73 3a 20 4e 75 6d 62 65 72 20 6f 66 20 72  tes: Number of r
11990 65 63 6f 72 64 73 20 69 6e 20 6a 6f 75 72 6e 61  ecords in journa
11991 6c 2c 20 6f 72 20 2d 31 20 6e 6f 2d 73 79 6e 63  l, or -1 no-sync
11992 20 6d 6f 64 65 20 69 73 20 6f 6e 2e 0a 2a 2a 20   mode is on..** 
11993 2d 20 34 20 62 79 74 65 73 3a 20 52 61 6e 64 6f  - 4 bytes: Rando
11994 6d 20 6e 75 6d 62 65 72 20 75 73 65 64 20 66 6f  m number used fo
11995 72 20 70 61 67 65 20 68 61 73 68 2e 0a 2a 2a 20  r page hash..** 
11996 2d 20 34 20 62 79 74 65 73 3a 20 49 6e 69 74 69  - 4 bytes: Initi
11997 61 6c 20 64 61 74 61 62 61 73 65 20 70 61 67 65  al database page
11998 20 63 6f 75 6e 74 2e 0a 2a 2a 20 2d 20 34 20 62   count..** - 4 b
11999 79 74 65 73 3a 20 53 65 63 74 6f 72 20 73 69 7a  ytes: Sector siz
1199a 65 20 75 73 65 64 20 62 79 20 74 68 65 20 70 72  e used by the pr
1199b 6f 63 65 73 73 20 74 68 61 74 20 77 72 6f 74 65  ocess that wrote
1199c 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 2e 0a 2a   this journal..*
1199d 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 44 61 74  * - 4 bytes: Dat
1199e 61 62 61 73 65 20 70 61 67 65 20 73 69 7a 65 2e  abase page size.
1199f 0a 2a 2a 20 0a 2a 2a 20 46 6f 6c 6c 6f 77 65 64  .** .** Followed
119a0 20 62 79 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52   by (JOURNAL_HDR
119a1 5f 53 5a 20 2d 20 32 38 29 20 62 79 74 65 73 20  _SZ - 28) bytes 
119a2 6f 66 20 75 6e 75 73 65 64 20 73 70 61 63 65 2e  of unused space.
119a3 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
119a4 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 50  riteJournalHdr(P
119a5 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
119a6 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
119a7 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20  _OK;            
119a8 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63       /* Return c
119a9 6f 64 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ode */.  char *z
119aa 48 65 61 64 65 72 20 3d 20 70 50 61 67 65 72 2d  Header = pPager-
119ab 3e 70 54 6d 70 53 70 61 63 65 3b 20 20 2f 2a 20  >pTmpSpace;  /* 
119ac 54 65 6d 70 6f 72 61 72 79 20 73 70 61 63 65 20  Temporary space 
119ad 75 73 65 64 20 74 6f 20 62 75 69 6c 64 20 68 65  used to build he
119ae 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 6e 48  ader */.  u32 nH
119af 65 61 64 65 72 20 3d 20 70 50 61 67 65 72 2d 3e  eader = pPager->
119b0 70 61 67 65 53 69 7a 65 3b 20 20 20 20 20 2f 2a  pageSize;     /*
119b1 20 53 69 7a 65 20 6f 66 20 62 75 66 66 65 72 20   Size of buffer 
119b2 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 7a 48  pointed to by zH
119b3 65 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 6e  eader */.  u32 n
119b4 57 72 69 74 65 3b 20 20 20 20 20 20 20 20 20 20  Write;          
119b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
119b6 2a 20 42 79 74 65 73 20 6f 66 20 68 65 61 64 65  * Bytes of heade
119b7 72 20 73 65 63 74 6f 72 20 77 72 69 74 74 65 6e  r sector written
119b8 20 2a 2f 0a 20 20 69 6e 74 20 69 69 3b 20 20 20   */.  int ii;   
119b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
119ba 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
119bb 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20  p counter */..  
119bc 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70  assert( isOpen(p
119bd 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 20 20  Pager->jfd) );  
119be 20 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20 66      /* Journal f
119bf 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65 6e  ile must be open
119c0 2e 20 2a 2f 0a 0a 20 20 69 66 28 20 6e 48 65 61  . */..  if( nHea
119c1 64 65 72 3e 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f  der>JOURNAL_HDR_
119c2 53 5a 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20  SZ(pPager) ){.  
119c3 20 20 6e 48 65 61 64 65 72 20 3d 20 4a 4f 55 52    nHeader = JOUR
119c4 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65  NAL_HDR_SZ(pPage
119c5 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66  r);.  }..  /* If
119c6 20 74 68 65 72 65 20 61 72 65 20 61 63 74 69 76   there are activ
119c7 65 20 73 61 76 65 70 6f 69 6e 74 73 20 61 6e 64  e savepoints and
119c8 20 61 6e 79 20 6f 66 20 74 68 65 6d 20 77 65 72   any of them wer
119c9 65 20 63 72 65 61 74 65 64 20 0a 20 20 2a 2a 20  e created .  ** 
119ca 73 69 6e 63 65 20 74 68 65 20 6d 6f 73 74 20 72  since the most r
119cb 65 63 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 68 65  ecent journal he
119cc 61 64 65 72 20 77 61 73 20 77 72 69 74 74 65 6e  ader was written
119cd 2c 20 75 70 64 61 74 65 20 74 68 65 20 0a 20 20  , update the .  
119ce 2a 2a 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e  ** PagerSavepoin
119cf 74 2e 69 48 64 72 4f 66 66 73 65 74 20 66 69 65  t.iHdrOffset fie
119d0 6c 64 73 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20  lds now..  */.  
119d1 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 50 61  for(ii=0; ii<pPa
119d2 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b  ger->nSavepoint;
119d3 20 69 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20   ii++){.    if( 
119d4 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69  pPager->aSavepoi
119d5 6e 74 5b 69 69 5d 2e 69 48 64 72 4f 66 66 73 65  nt[ii].iHdrOffse
119d6 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 50  t==0 ){.      pP
119d7 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74  ager->aSavepoint
119d8 5b 69 69 5d 2e 69 48 64 72 4f 66 66 73 65 74 20  [ii].iHdrOffset 
119d9 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  = pPager->journa
119da 6c 4f 66 66 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  lOff;.    }.  }.
119db 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  .  pPager->journ
119dc 61 6c 48 64 72 20 3d 20 70 50 61 67 65 72 2d 3e  alHdr = pPager->
119dd 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 6a 6f 75  journalOff = jou
119de 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 28 70 50  rnalHdrOffset(pP
119df 61 67 65 72 29 3b 0a 0a 20 20 2f 2a 20 0a 20 20  ager);..  /* .  
119e0 2a 2a 20 57 72 69 74 65 20 74 68 65 20 6e 52 65  ** Write the nRe
119e1 63 20 46 69 65 6c 64 20 2d 20 74 68 65 20 6e 75  c Field - the nu
119e2 6d 62 65 72 20 6f 66 20 70 61 67 65 20 72 65 63  mber of page rec
119e3 6f 72 64 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77  ords that follow
119e4 20 74 68 69 73 0a 20 20 2a 2a 20 6a 6f 75 72 6e   this.  ** journ
119e5 61 6c 20 68 65 61 64 65 72 2e 20 4e 6f 72 6d 61  al header. Norma
119e6 6c 6c 79 2c 20 7a 65 72 6f 20 69 73 20 77 72 69  lly, zero is wri
119e7 74 74 65 6e 20 74 6f 20 74 68 69 73 20 76 61 6c  tten to this val
119e8 75 65 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e  ue at this time.
119e9 0a 20 20 2a 2a 20 41 66 74 65 72 20 74 68 65 20  .  ** After the 
119ea 72 65 63 6f 72 64 73 20 61 72 65 20 61 64 64 65  records are adde
119eb 64 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c  d to the journal
119ec 20 28 61 6e 64 20 74 68 65 20 6a 6f 75 72 6e 61   (and the journa
119ed 6c 20 73 79 6e 63 65 64 2c 20 0a 20 20 2a 2a 20  l synced, .  ** 
119ee 69 66 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 20  if in full-sync 
119ef 6d 6f 64 65 29 2c 20 74 68 65 20 7a 65 72 6f 20  mode), the zero 
119f0 69 73 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77  is overwritten w
119f1 69 74 68 20 74 68 65 20 74 72 75 65 20 6e 75 6d  ith the true num
119f2 62 65 72 0a 20 20 2a 2a 20 6f 66 20 72 65 63 6f  ber.  ** of reco
119f3 72 64 73 20 28 73 65 65 20 73 79 6e 63 4a 6f 75  rds (see syncJou
119f4 72 6e 61 6c 28 29 29 2e 0a 20 20 2a 2a 0a 20 20  rnal())..  **.  
119f5 2a 2a 20 41 20 66 61 73 74 65 72 20 61 6c 74 65  ** A faster alte
119f6 72 6e 61 74 69 76 65 20 69 73 20 74 6f 20 77 72  rnative is to wr
119f7 69 74 65 20 30 78 46 46 46 46 46 46 46 46 20 74  ite 0xFFFFFFFF t
119f8 6f 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64  o the nRec field
119f9 2e 20 57 68 65 6e 0a 20 20 2a 2a 20 72 65 61 64  . When.  ** read
119fa 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ing the journal 
119fb 74 68 69 73 20 76 61 6c 75 65 20 74 65 6c 6c 73  this value tells
119fc 20 53 51 4c 69 74 65 20 74 6f 20 61 73 73 75 6d   SQLite to assum
119fd 65 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a 20  e that the.  ** 
119fe 72 65 73 74 20 6f 66 20 74 68 65 20 6a 6f 75 72  rest of the jour
119ff 6e 61 6c 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  nal file contain
11a00 73 20 76 61 6c 69 64 20 70 61 67 65 20 72 65 63  s valid page rec
11a01 6f 72 64 73 2e 20 54 68 69 73 20 61 73 73 75 6d  ords. This assum
11a02 70 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 64 61  ption.  ** is da
11a03 6e 67 65 72 6f 75 73 2c 20 61 73 20 69 66 20 61  ngerous, as if a
11a04 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 72 65   failure occurre
11a05 64 20 77 68 69 6c 73 74 20 77 72 69 74 69 6e 67  d whilst writing
11a06 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a   to the journal.
11a07 20 20 2a 2a 20 66 69 6c 65 20 69 74 20 6d 61 79    ** file it may
11a08 20 63 6f 6e 74 61 69 6e 20 73 6f 6d 65 20 67 61   contain some ga
11a09 72 62 61 67 65 20 64 61 74 61 2e 20 54 68 65 72  rbage data. Ther
11a0a 65 20 61 72 65 20 74 77 6f 20 73 63 65 6e 61 72  e are two scenar
11a0b 69 6f 73 0a 20 20 2a 2a 20 77 68 65 72 65 20 74  ios.  ** where t
11a0c 68 69 73 20 72 69 73 6b 20 63 61 6e 20 62 65 20  his risk can be 
11a0d 69 67 6e 6f 72 65 64 3a 0a 20 20 2a 2a 0a 20 20  ignored:.  **.  
11a0e 2a 2a 20 20 20 2a 20 57 68 65 6e 20 74 68 65 20  **   * When the 
11a0f 70 61 67 65 72 20 69 73 20 69 6e 20 6e 6f 2d 73  pager is in no-s
11a10 79 6e 63 20 6d 6f 64 65 2e 20 43 6f 72 72 75 70  ync mode. Corrup
11a11 74 69 6f 6e 20 63 61 6e 20 66 6f 6c 6c 6f 77 20  tion can follow 
11a12 61 0a 20 20 2a 2a 20 20 20 20 20 70 6f 77 65 72  a.  **     power
11a13 20 66 61 69 6c 75 72 65 20 69 6e 20 74 68 69 73   failure in this
11a14 20 63 61 73 65 20 61 6e 79 77 61 79 2e 0a 20 20   case anyway..  
11a15 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 68 65 6e  **.  **   * When
11a16 20 74 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41   the SQLITE_IOCA
11a17 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 20 66 6c  P_SAFE_APPEND fl
11a18 61 67 20 69 73 20 73 65 74 2e 20 54 68 69 73 20  ag is set. This 
11a19 67 75 61 72 61 6e 74 65 65 73 0a 20 20 2a 2a 20  guarantees.  ** 
11a1a 20 20 20 20 74 68 61 74 20 67 61 72 62 61 67 65      that garbage
11a1b 20 64 61 74 61 20 69 73 20 6e 65 76 65 72 20 61   data is never a
11a1c 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20 6a  ppended to the j
11a1d 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 2a  ournal file..  *
11a1e 2f 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70  /.  assert( isOp
11a1f 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 7c  en(pPager->fd) |
11a20 7c 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63  | pPager->noSync
11a21 20 29 3b 0a 20 20 69 66 28 20 28 70 50 61 67 65   );.  if( (pPage
11a22 72 2d 3e 6e 6f 53 79 6e 63 29 20 7c 7c 20 28 70  r->noSync) || (p
11a23 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f  Pager->journalMo
11a24 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de==PAGER_JOURNA
11a25 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29 0a 20 20  LMODE_MEMORY).  
11a26 20 7c 7c 20 28 73 71 6c 69 74 65 33 4f 73 44 65   || (sqlite3OsDe
11a27 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74  viceCharacterist
11a28 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 26  ics(pPager->fd)&
11a29 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46  SQLITE_IOCAP_SAF
11a2a 45 5f 41 50 50 45 4e 44 29 20 0a 20 20 29 7b 0a  E_APPEND) .  ){.
11a2b 20 20 20 20 6d 65 6d 63 70 79 28 7a 48 65 61 64      memcpy(zHead
11a2c 65 72 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69  er, aJournalMagi
11a2d 63 2c 20 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e  c, sizeof(aJourn
11a2e 61 6c 4d 61 67 69 63 29 29 3b 0a 20 20 20 20 70  alMagic));.    p
11a2f 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 65  ut32bits(&zHeade
11a30 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61  r[sizeof(aJourna
11a31 6c 4d 61 67 69 63 29 5d 2c 20 30 78 66 66 66 66  lMagic)], 0xffff
11a32 66 66 66 66 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  ffff);.  }else{.
11a33 20 20 20 20 6d 65 6d 73 65 74 28 7a 48 65 61 64      memset(zHead
11a34 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61 4a  er, 0, sizeof(aJ
11a35 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 34 29 3b  ournalMagic)+4);
11a36 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 72  .  }..  /* The r
11a37 61 6e 64 6f 6d 20 63 68 65 63 6b 2d 68 61 73 68  andom check-hash
11a38 20 69 6e 69 74 69 61 6c 69 73 65 72 20 2a 2f 20   initialiser */ 
11a39 0a 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f  .  sqlite3_rando
11a3a 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 70 50 61  mness(sizeof(pPa
11a3b 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 2c  ger->cksumInit),
11a3c 20 26 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49   &pPager->cksumI
11a3d 6e 69 74 29 3b 0a 20 20 70 75 74 33 32 62 69 74  nit);.  put32bit
11a3e 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f  s(&zHeader[sizeo
11a3f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29  f(aJournalMagic)
11a40 2b 34 5d 2c 20 70 50 61 67 65 72 2d 3e 63 6b 73  +4], pPager->cks
11a41 75 6d 49 6e 69 74 29 3b 0a 20 20 2f 2a 20 54 68  umInit);.  /* Th
11a42 65 20 69 6e 69 74 69 61 6c 20 64 61 74 61 62 61  e initial databa
11a43 73 65 20 73 69 7a 65 20 2a 2f 0a 20 20 70 75 74  se size */.  put
11a44 33 32 62 69 74 73 28 26 7a 48 65 61 64 65 72 5b  32bits(&zHeader[
11a45 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d  sizeof(aJournalM
11a46 61 67 69 63 29 2b 38 5d 2c 20 70 50 61 67 65 72  agic)+8], pPager
11a47 2d 3e 64 62 4f 72 69 67 53 69 7a 65 29 3b 0a 20  ->dbOrigSize);. 
11a48 20 2f 2a 20 54 68 65 20 61 73 73 75 6d 65 64 20   /* The assumed 
11a49 73 65 63 74 6f 72 20 73 69 7a 65 20 66 6f 72 20  sector size for 
11a4a 74 68 69 73 20 70 72 6f 63 65 73 73 20 2a 2f 0a  this process */.
11a4b 20 20 70 75 74 33 32 62 69 74 73 28 26 7a 48 65    put32bits(&zHe
11a4c 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75  ader[sizeof(aJou
11a4d 72 6e 61 6c 4d 61 67 69 63 29 2b 31 32 5d 2c 20  rnalMagic)+12], 
11a4e 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69  pPager->sectorSi
11a4f 7a 65 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 70  ze);..  /* The p
11a50 61 67 65 20 73 69 7a 65 20 2a 2f 0a 20 20 70 75  age size */.  pu
11a51 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 65 72  t32bits(&zHeader
11a52 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c  [sizeof(aJournal
11a53 4d 61 67 69 63 29 2b 31 36 5d 2c 20 70 50 61 67  Magic)+16], pPag
11a54 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 0a  er->pageSize);..
11a55 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 69 6e    /* Initializin
11a56 67 20 74 68 65 20 74 61 69 6c 20 6f 66 20 74 68  g the tail of th
11a57 65 20 62 75 66 66 65 72 20 69 73 20 6e 6f 74 20  e buffer is not 
11a58 6e 65 63 65 73 73 61 72 79 2e 20 20 45 76 65 72  necessary.  Ever
11a59 79 74 68 69 6e 67 0a 20 20 2a 2a 20 77 6f 72 6b  ything.  ** work
11a5a 73 20 66 69 6e 64 20 69 66 20 74 68 65 20 66 6f  s find if the fo
11a5b 6c 6c 6f 77 69 6e 67 20 6d 65 6d 73 65 74 28 29  llowing memset()
11a5c 20 69 73 20 6f 6d 69 74 74 65 64 2e 20 20 42 75   is omitted.  Bu
11a5d 74 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 0a 20  t initializing. 
11a5e 20 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 70   ** the memory p
11a5f 72 65 76 65 6e 74 73 20 76 61 6c 67 72 69 6e 64  revents valgrind
11a60 20 66 72 6f 6d 20 63 6f 6d 70 6c 61 69 6e 69 6e   from complainin
11a61 67 2c 20 73 6f 20 77 65 20 61 72 65 20 77 69 6c  g, so we are wil
11a62 6c 69 6e 67 20 74 6f 0a 20 20 2a 2a 20 74 61 6b  ling to.  ** tak
11a63 65 20 74 68 65 20 70 65 72 66 6f 72 6d 61 6e 63  e the performanc
11a64 65 20 68 69 74 2e 0a 20 20 2a 2f 0a 20 20 6d 65  e hit..  */.  me
11a65 6d 73 65 74 28 26 7a 48 65 61 64 65 72 5b 73 69  mset(&zHeader[si
11a66 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67  zeof(aJournalMag
11a67 69 63 29 2b 32 30 5d 2c 20 30 2c 0a 20 20 20 20  ic)+20], 0,.    
11a68 20 20 20 20 20 6e 48 65 61 64 65 72 2d 28 73 69       nHeader-(si
11a69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67  zeof(aJournalMag
11a6a 69 63 29 2b 32 30 29 29 3b 0a 0a 20 20 2f 2a 20  ic)+20));..  /* 
11a6b 49 6e 20 74 68 65 6f 72 79 2c 20 69 74 20 69 73  In theory, it is
11a6c 20 6f 6e 6c 79 20 6e 65 63 65 73 73 61 72 79 20   only necessary 
11a6d 74 6f 20 77 72 69 74 65 20 74 68 65 20 32 38 20  to write the 28 
11a6e 62 79 74 65 73 20 74 68 61 74 20 74 68 65 20 0a  bytes that the .
11a6f 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 68 65 61    ** journal hea
11a70 64 65 72 20 63 6f 6e 73 75 6d 65 73 20 74 6f 20  der consumes to 
11a71 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
11a72 20 68 65 72 65 2e 20 54 68 65 6e 20 69 6e 63 72   here. Then incr
11a73 65 6d 65 6e 74 20 74 68 65 20 0a 20 20 2a 2a 20  ement the .  ** 
11a74 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66  Pager.journalOff
11a75 20 76 61 72 69 61 62 6c 65 20 62 79 20 4a 4f 55   variable by JOU
11a76 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 73 6f 20 74  RNAL_HDR_SZ so t
11a77 68 61 74 20 74 68 65 20 6e 65 78 74 20 0a 20 20  hat the next .  
11a78 2a 2a 20 72 65 63 6f 72 64 20 69 73 20 77 72 69  ** record is wri
11a79 74 74 65 6e 20 74 6f 20 74 68 65 20 66 6f 6c 6c  tten to the foll
11a7a 6f 77 69 6e 67 20 73 65 63 74 6f 72 20 28 6c 65  owing sector (le
11a7b 61 76 69 6e 67 20 61 20 67 61 70 20 69 6e 20 74  aving a gap in t
11a7c 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20 74 68 61  he file.  ** tha
11a7d 74 20 77 69 6c 6c 20 62 65 20 69 6d 70 6c 69 63  t will be implic
11a7e 69 74 6c 79 20 66 69 6c 6c 65 64 20 69 6e 20 62  itly filled in b
11a7f 79 20 74 68 65 20 4f 53 29 2e 0a 20 20 2a 2a 0a  y the OS)..  **.
11a80 20 20 2a 2a 20 48 6f 77 65 76 65 72 20 69 74 20    ** However it 
11a81 68 61 73 20 62 65 65 6e 20 64 69 73 63 6f 76 65  has been discove
11a82 72 65 64 20 74 68 61 74 20 6f 6e 20 73 6f 6d 65  red that on some
11a83 20 73 79 73 74 65 6d 73 20 74 68 69 73 20 70 61   systems this pa
11a84 74 74 65 72 6e 20 63 61 6e 20 0a 20 20 2a 2a 20  ttern can .  ** 
11a85 62 65 20 73 69 67 6e 69 66 69 63 61 6e 74 6c 79  be significantly
11a86 20 73 6c 6f 77 65 72 20 74 68 61 6e 20 63 6f 6e   slower than con
11a87 74 69 67 75 6f 75 73 6c 79 20 77 72 69 74 69 6e  tiguously writin
11a88 67 20 64 61 74 61 20 74 6f 20 74 68 65 20 66 69  g data to the fi
11a89 6c 65 2c 0a 20 20 2a 2a 20 65 76 65 6e 20 69 66  le,.  ** even if
11a8a 20 74 68 61 74 20 6d 65 61 6e 73 20 65 78 70 6c   that means expl
11a8b 69 63 69 74 6c 79 20 77 72 69 74 69 6e 67 20 64  icitly writing d
11a8c 61 74 61 20 74 6f 20 74 68 65 20 62 6c 6f 63 6b  ata to the block
11a8d 20 6f 66 20 0a 20 20 2a 2a 20 28 4a 4f 55 52 4e   of .  ** (JOURN
11a8e 41 4c 5f 48 44 52 5f 53 5a 20 2d 20 32 38 29 20  AL_HDR_SZ - 28) 
11a8f 62 79 74 65 73 20 74 68 61 74 20 77 69 6c 6c 20  bytes that will 
11a90 6e 6f 74 20 62 65 20 75 73 65 64 2e 20 53 6f 20  not be used. So 
11a91 74 68 61 74 20 69 73 20 77 68 61 74 0a 20 20 2a  that is what.  *
11a92 2a 20 69 73 20 64 6f 6e 65 2e 20 0a 20 20 2a 2a  * is done. .  **
11a93 0a 20 20 2a 2a 20 54 68 65 20 6c 6f 6f 70 20 69  .  ** The loop i
11a94 73 20 72 65 71 75 69 72 65 64 20 68 65 72 65 20  s required here 
11a95 69 6e 20 63 61 73 65 20 74 68 65 20 73 65 63 74  in case the sect
11a96 6f 72 2d 73 69 7a 65 20 69 73 20 6c 61 72 67 65  or-size is large
11a97 72 20 74 68 61 6e 20 74 68 65 20 0a 20 20 2a 2a  r than the .  **
11a98 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 73   database page s
11a99 69 7a 65 2e 20 53 69 6e 63 65 20 74 68 65 20 7a  ize. Since the z
11a9a 48 65 61 64 65 72 20 62 75 66 66 65 72 20 69 73  Header buffer is
11a9b 20 6f 6e 6c 79 20 50 61 67 65 72 2e 70 61 67 65   only Pager.page
11a9c 53 69 7a 65 0a 20 20 2a 2a 20 62 79 74 65 73 20  Size.  ** bytes 
11a9d 69 6e 20 73 69 7a 65 2c 20 6d 6f 72 65 20 74 68  in size, more th
11a9e 61 6e 20 6f 6e 65 20 63 61 6c 6c 20 74 6f 20 73  an one call to s
11a9f 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 29 20  qlite3OsWrite() 
11aa0 6d 61 79 20 62 65 20 72 65 71 75 69 72 65 64 0a  may be required.
11aa1 20 20 2a 2a 20 74 6f 20 70 6f 70 75 6c 61 74 65    ** to populate
11aa2 20 74 68 65 20 65 6e 74 69 72 65 20 6a 6f 75 72   the entire jour
11aa3 6e 61 6c 20 68 65 61 64 65 72 20 73 65 63 74 6f  nal header secto
11aa4 72 2e 0a 20 20 2a 2f 20 0a 20 20 66 6f 72 28 6e  r..  */ .  for(n
11aa5 57 72 69 74 65 3d 30 3b 20 72 63 3d 3d 53 51 4c  Write=0; rc==SQL
11aa6 49 54 45 5f 4f 4b 26 26 6e 57 72 69 74 65 3c 4a  ITE_OK&&nWrite<J
11aa7 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50  OURNAL_HDR_SZ(pP
11aa8 61 67 65 72 29 3b 20 6e 57 72 69 74 65 2b 3d 6e  ager); nWrite+=n
11aa9 48 65 61 64 65 72 29 7b 0a 20 20 20 20 49 4f 54  Header){.    IOT
11aaa 52 41 43 45 28 28 22 4a 48 44 52 20 25 70 20 25  RACE(("JHDR %p %
11aab 6c 6c 64 20 25 64 5c 6e 22 2c 20 70 50 61 67 65  lld %d\n", pPage
11aac 72 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  r, pPager->journ
11aad 61 6c 48 64 72 2c 20 6e 48 65 61 64 65 72 29 29  alHdr, nHeader))
11aae 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
11aaf 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d  3OsWrite(pPager-
11ab0 3e 6a 66 64 2c 20 7a 48 65 61 64 65 72 2c 20 6e  >jfd, zHeader, n
11ab1 48 65 61 64 65 72 2c 20 70 50 61 67 65 72 2d 3e  Header, pPager->
11ab2 6a 6f 75 72 6e 61 6c 4f 66 66 29 3b 0a 20 20 20  journalOff);.   
11ab3 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
11ab4 4f 66 66 20 2b 3d 20 6e 48 65 61 64 65 72 3b 0a  Off += nHeader;.
11ab5 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
11ab6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a  ;.}../*.** The j
11ab7 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74  ournal file must
11ab8 20 62 65 20 6f 70 65 6e 20 77 68 65 6e 20 74 68   be open when th
11ab9 69 73 20 69 73 20 63 61 6c 6c 65 64 2e 20 41 20  is is called. A 
11aba 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66  journal header f
11abb 69 6c 65 0a 2a 2a 20 28 4a 4f 55 52 4e 41 4c 5f  ile.** (JOURNAL_
11abc 48 44 52 5f 53 5a 20 62 79 74 65 73 29 20 69 73  HDR_SZ bytes) is
11abd 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 63   read from the c
11abe 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 20  urrent location 
11abf 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a  in the journal.*
11ac0 2a 20 66 69 6c 65 2e 20 54 68 65 20 63 75 72 72  * file. The curr
11ac1 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 20 69 6e 20  ent location in 
11ac2 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
11ac3 20 69 73 20 67 69 76 65 6e 20 62 79 0a 2a 2a 20   is given by.** 
11ac4 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
11ac5 66 66 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73  ff. See comments
11ac6 20 61 62 6f 76 65 20 66 75 6e 63 74 69 6f 6e 20   above function 
11ac7 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28  writeJournalHdr(
11ac8 29 20 66 6f 72 0a 2a 2a 20 61 20 64 65 73 63 72  ) for.** a descr
11ac9 69 70 74 69 6f 6e 20 6f 66 20 74 68 65 20 6a 6f  iption of the jo
11aca 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 6f 72  urnal header for
11acb 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  mat..**.** If th
11acc 65 20 68 65 61 64 65 72 20 69 73 20 72 65 61 64  e header is read
11acd 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 2a   successfully, *
11ace 70 4e 52 65 63 20 69 73 20 73 65 74 20 74 6f 20  pNRec is set to 
11acf 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a  the number of.**
11ad0 20 70 61 67 65 20 72 65 63 6f 72 64 73 20 66 6f   page records fo
11ad1 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20 68 65 61  llowing this hea
11ad2 64 65 72 20 61 6e 64 20 2a 70 44 62 53 69 7a 65  der and *pDbSize
11ad3 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 73   is set to the s
11ad4 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61  ize of the.** da
11ad5 74 61 62 61 73 65 20 62 65 66 6f 72 65 20 74 68  tabase before th
11ad6 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 65  e transaction be
11ad7 67 61 6e 2c 20 69 6e 20 70 61 67 65 73 2e 20 41  gan, in pages. A
11ad8 6c 73 6f 2c 20 70 50 61 67 65 72 2d 3e 63 6b 73  lso, pPager->cks
11ad9 75 6d 49 6e 69 74 0a 2a 2a 20 69 73 20 73 65 74  umInit.** is set
11ada 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 72 65   to the value re
11adb 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72  ad from the jour
11adc 6e 61 6c 20 68 65 61 64 65 72 2e 20 53 51 4c 49  nal header. SQLI
11add 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
11ade 64 0a 2a 2a 20 69 6e 20 74 68 69 73 20 63 61 73  d.** in this cas
11adf 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
11ae0 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66  journal header f
11ae1 69 6c 65 20 61 70 70 65 61 72 73 20 74 6f 20 62  ile appears to b
11ae2 65 20 63 6f 72 72 75 70 74 65 64 2c 20 53 51 4c  e corrupted, SQL
11ae3 49 54 45 5f 44 4f 4e 45 20 69 73 0a 2a 2a 20 72  ITE_DONE is.** r
11ae4 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 4e 52  eturned and *pNR
11ae5 65 63 20 61 6e 64 20 2a 50 44 62 53 69 7a 65 20  ec and *PDbSize 
11ae6 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 20 20  are undefined.  
11ae7 49 66 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53  If JOURNAL_HDR_S
11ae8 5a 20 62 79 74 65 73 0a 2a 2a 20 63 61 6e 6e 6f  Z bytes.** canno
11ae9 74 20 62 65 20 72 65 61 64 20 66 72 6f 6d 20 74  t be read from t
11aea 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
11aeb 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  an error code is
11aec 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74   returned..*/.st
11aed 61 74 69 63 20 69 6e 74 20 72 65 61 64 4a 6f 75  atic int readJou
11aee 72 6e 61 6c 48 64 72 28 0a 20 20 50 61 67 65 72  rnalHdr(.  Pager
11aef 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 20   *pPager,       
11af0 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72          /* Pager
11af1 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74   object */.  int
11af2 20 69 73 48 6f 74 2c 0a 20 20 69 36 34 20 6a 6f   isHot,.  i64 jo
11af3 75 72 6e 61 6c 53 69 7a 65 2c 20 20 20 20 20 20  urnalSize,      
11af4 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
11af5 66 20 74 68 65 20 6f 70 65 6e 20 6a 6f 75 72 6e  f the open journ
11af6 61 6c 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73  al file in bytes
11af7 20 2a 2f 0a 20 20 75 33 32 20 2a 70 4e 52 65 63   */.  u32 *pNRec
11af8 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
11af9 20 20 20 2f 2a 20 4f 55 54 3a 20 56 61 6c 75 65     /* OUT: Value
11afa 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6e   read from the n
11afb 52 65 63 20 66 69 65 6c 64 20 2a 2f 0a 20 20 75  Rec field */.  u
11afc 33 32 20 2a 70 44 62 53 69 7a 65 20 20 20 20 20  32 *pDbSize     
11afd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
11afe 55 54 3a 20 56 61 6c 75 65 20 6f 66 20 6f 72 69  UT: Value of ori
11aff 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20 73  ginal database s
11b00 69 7a 65 20 66 69 65 6c 64 20 2a 2f 0a 29 7b 0a  ize field */.){.
11b01 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
11b02 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
11b03 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f  * Return code */
11b04 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
11b05 20 61 4d 61 67 69 63 5b 38 5d 3b 20 20 20 20 20   aMagic[8];     
11b06 2f 2a 20 41 20 62 75 66 66 65 72 20 74 6f 20 68  /* A buffer to h
11b07 6f 6c 64 20 74 68 65 20 6d 61 67 69 63 20 68 65  old the magic he
11b08 61 64 65 72 20 2a 2f 0a 20 20 69 36 34 20 69 48  ader */.  i64 iH
11b09 64 72 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20  drOff;          
11b0a 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74         /* Offset
11b0b 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64   of journal head
11b0c 65 72 20 62 65 69 6e 67 20 72 65 61 64 20 2a 2f  er being read */
11b0d 0a 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70  ..  assert( isOp
11b0e 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
11b0f 29 3b 20 20 20 20 20 20 2f 2a 20 4a 6f 75 72 6e  );      /* Journ
11b10 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20  al file must be 
11b11 6f 70 65 6e 2e 20 2a 2f 0a 0a 20 20 2f 2a 20 41  open. */..  /* A
11b12 64 76 61 6e 63 65 20 50 61 67 65 72 2e 6a 6f 75  dvance Pager.jou
11b13 72 6e 61 6c 4f 66 66 20 74 6f 20 74 68 65 20 73  rnalOff to the s
11b14 74 61 72 74 20 6f 66 20 74 68 65 20 6e 65 78 74  tart of the next
11b15 20 73 65 63 74 6f 72 2e 20 49 66 20 74 68 65 0a   sector. If the.
11b16 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c    ** journal fil
11b17 65 20 69 73 20 74 6f 6f 20 73 6d 61 6c 6c 20 66  e is too small f
11b18 6f 72 20 74 68 65 72 65 20 74 6f 20 62 65 20 61  or there to be a
11b19 20 68 65 61 64 65 72 20 73 74 6f 72 65 64 20 61   header stored a
11b1a 74 20 74 68 69 73 0a 20 20 2a 2a 20 70 6f 69 6e  t this.  ** poin
11b1b 74 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  t, return SQLITE
11b1c 5f 44 4f 4e 45 2e 0a 20 20 2a 2f 0a 20 20 70 50  _DONE..  */.  pP
11b1d 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
11b1e 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66   = journalHdrOff
11b1f 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 69  set(pPager);.  i
11b20 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  f( pPager->journ
11b21 61 6c 4f 66 66 2b 4a 4f 55 52 4e 41 4c 5f 48 44  alOff+JOURNAL_HD
11b22 52 5f 53 5a 28 70 50 61 67 65 72 29 20 3e 20 6a  R_SZ(pPager) > j
11b23 6f 75 72 6e 61 6c 53 69 7a 65 20 29 7b 0a 20 20  ournalSize ){.  
11b24 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
11b25 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 69 48 64 72  DONE;.  }.  iHdr
11b26 4f 66 66 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f  Off = pPager->jo
11b27 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f 2a 20  urnalOff;..  /* 
11b28 52 65 61 64 20 69 6e 20 74 68 65 20 66 69 72 73  Read in the firs
11b29 74 20 38 20 62 79 74 65 73 20 6f 66 20 74 68 65  t 8 bytes of the
11b2a 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2e   journal header.
11b2b 20 49 66 20 74 68 65 79 20 64 6f 20 6e 6f 74 20   If they do not 
11b2c 6d 61 74 63 68 0a 20 20 2a 2a 20 74 68 65 20 20  match.  ** the  
11b2d 6d 61 67 69 63 20 73 74 72 69 6e 67 20 66 6f 75  magic string fou
11b2e 6e 64 20 61 74 20 74 68 65 20 73 74 61 72 74 20  nd at the start 
11b2f 6f 66 20 65 61 63 68 20 6a 6f 75 72 6e 61 6c 20  of each journal 
11b30 68 65 61 64 65 72 2c 20 72 65 74 75 72 6e 0a 20  header, return. 
11b31 20 2a 2a 20 53 51 4c 49 54 45 5f 44 4f 4e 45 2e   ** SQLITE_DONE.
11b32 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20   If an IO error 
11b33 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20 61  occurs, return a
11b34 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 4f 74  n error code. Ot
11b35 68 65 72 77 69 73 65 2c 0a 20 20 2a 2a 20 70 72  herwise,.  ** pr
11b36 6f 63 65 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66  oceed..  */.  if
11b37 28 20 69 73 48 6f 74 20 7c 7c 20 69 48 64 72 4f  ( isHot || iHdrO
11b38 66 66 21 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72  ff!=pPager->jour
11b39 6e 61 6c 48 64 72 20 29 7b 0a 20 20 20 20 72 63  nalHdr ){.    rc
11b3a 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64   = sqlite3OsRead
11b3b 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 61 4d  (pPager->jfd, aM
11b3c 61 67 69 63 2c 20 73 69 7a 65 6f 66 28 61 4d 61  agic, sizeof(aMa
11b3d 67 69 63 29 2c 20 69 48 64 72 4f 66 66 29 3b 0a  gic), iHdrOff);.
11b3e 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20      if( rc ){.  
11b3f 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
11b40 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 65 6d     }.    if( mem
11b41 63 6d 70 28 61 4d 61 67 69 63 2c 20 61 4a 6f 75  cmp(aMagic, aJou
11b42 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69 7a 65 6f  rnalMagic, sizeo
11b43 66 28 61 4d 61 67 69 63 29 29 21 3d 30 20 29 7b  f(aMagic))!=0 ){
11b44 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
11b45 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d  LITE_DONE;.    }
11b46 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 64 20  .  }..  /* Read 
11b47 74 68 65 20 66 69 72 73 74 20 74 68 72 65 65 20  the first three 
11b48 33 32 2d 62 69 74 20 66 69 65 6c 64 73 20 6f 66  32-bit fields of
11b49 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61   the journal hea
11b4a 64 65 72 3a 20 54 68 65 20 6e 52 65 63 0a 20 20  der: The nRec.  
11b4b 2a 2a 20 66 69 65 6c 64 2c 20 74 68 65 20 63 68  ** field, the ch
11b4c 65 63 6b 73 75 6d 2d 69 6e 69 74 69 61 6c 69 7a  ecksum-initializ
11b4d 65 72 20 61 6e 64 20 74 68 65 20 64 61 74 61 62  er and the datab
11b4e 61 73 65 20 73 69 7a 65 20 61 74 20 74 68 65 20  ase size at the 
11b4f 73 74 61 72 74 0a 20 20 2a 2a 20 6f 66 20 74 68  start.  ** of th
11b50 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 52  e transaction. R
11b51 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63  eturn an error c
11b52 6f 64 65 20 69 66 20 61 6e 79 74 68 69 6e 67 20  ode if anything 
11b53 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 20 20 2a 2f  goes wrong..  */
11b54 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  .  if( SQLITE_OK
11b55 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62 69  !=(rc = read32bi
11b56 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  ts(pPager->jfd, 
11b57 69 48 64 72 4f 66 66 2b 38 2c 20 70 4e 52 65 63  iHdrOff+8, pNRec
11b58 29 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f  )).   || SQLITE_
11b59 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32  OK!=(rc = read32
11b5a 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64  bits(pPager->jfd
11b5b 2c 20 69 48 64 72 4f 66 66 2b 31 32 2c 20 26 70  , iHdrOff+12, &p
11b5c 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74  Pager->cksumInit
11b5d 29 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f  )).   || SQLITE_
11b5e 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32  OK!=(rc = read32
11b5f 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64  bits(pPager->jfd
11b60 2c 20 69 48 64 72 4f 66 66 2b 31 36 2c 20 70 44  , iHdrOff+16, pD
11b61 62 53 69 7a 65 29 29 0a 20 20 29 7b 0a 20 20 20  bSize)).  ){.   
11b62 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
11b63 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a  .  if( pPager->j
11b64 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b 0a  ournalOff==0 ){.
11b65 20 20 20 20 75 33 32 20 69 50 61 67 65 53 69 7a      u32 iPageSiz
11b66 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
11b67 20 2f 2a 20 50 61 67 65 2d 73 69 7a 65 20 66 69   /* Page-size fi
11b68 65 6c 64 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 68  eld of journal h
11b69 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 75 33 32  eader */.    u32
11b6a 20 69 53 65 63 74 6f 72 53 69 7a 65 3b 20 20 20   iSectorSize;   
11b6b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63            /* Sec
11b6c 74 6f 72 2d 73 69 7a 65 20 66 69 65 6c 64 20 6f  tor-size field o
11b6d 66 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  f journal header
11b6e 20 2a 2f 0a 20 20 20 20 75 31 36 20 69 50 61 67   */.    u16 iPag
11b6f 65 53 69 7a 65 31 36 3b 20 20 20 20 20 20 20 20  eSize16;        
11b70 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20       /* Copy of 
11b71 69 50 61 67 65 53 69 7a 65 20 69 6e 20 31 36 2d  iPageSize in 16-
11b72 62 69 74 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a  bit variable */.
11b73 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65  .    /* Read the
11b74 20 70 61 67 65 2d 73 69 7a 65 20 61 6e 64 20 73   page-size and s
11b75 65 63 74 6f 72 2d 73 69 7a 65 20 6a 6f 75 72 6e  ector-size journ
11b76 61 6c 20 68 65 61 64 65 72 20 66 69 65 6c 64 73  al header fields
11b77 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 53 51 4c  . */.    if( SQL
11b78 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65  ITE_OK!=(rc = re
11b79 61 64 33 32 62 69 74 73 28 70 50 61 67 65 72 2d  ad32bits(pPager-
11b7a 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 32 30  >jfd, iHdrOff+20
11b7b 2c 20 26 69 53 65 63 74 6f 72 53 69 7a 65 29 29  , &iSectorSize))
11b7c 0a 20 20 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f  .     || SQLITE_
11b7d 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32  OK!=(rc = read32
11b7e 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64  bits(pPager->jfd
11b7f 2c 20 69 48 64 72 4f 66 66 2b 32 34 2c 20 26 69  , iHdrOff+24, &i
11b80 50 61 67 65 53 69 7a 65 29 29 0a 20 20 20 20 29  PageSize)).    )
11b81 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  {.      return r
11b82 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  c;.    }..    /*
11b83 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20   Check that the 
11b84 76 61 6c 75 65 73 20 72 65 61 64 20 66 72 6f 6d  values read from
11b85 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 61   the page-size a
11b86 6e 64 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 66  nd sector-size f
11b87 69 65 6c 64 73 0a 20 20 20 20 2a 2a 20 61 72 65  ields.    ** are
11b88 20 77 69 74 68 69 6e 20 72 61 6e 67 65 2e 20 54   within range. T
11b89 6f 20 62 65 20 27 69 6e 20 72 61 6e 67 65 27 2c  o be 'in range',
11b8a 20 62 6f 74 68 20 76 61 6c 75 65 73 20 6e 65 65   both values nee
11b8b 64 20 74 6f 20 62 65 20 61 20 70 6f 77 65 72 0a  d to be a power.
11b8c 20 20 20 20 2a 2a 20 6f 66 20 74 77 6f 20 67 72      ** of two gr
11b8d 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71  eater than or eq
11b8e 75 61 6c 20 74 6f 20 35 31 32 2c 20 61 6e 64 20  ual to 512, and 
11b8f 6e 6f 74 20 67 72 65 61 74 65 72 20 74 68 61 6e  not greater than
11b90 20 74 68 65 69 72 20 0a 20 20 20 20 2a 2a 20 72   their .    ** r
11b91 65 73 70 65 63 74 69 76 65 20 63 6f 6d 70 69 6c  espective compil
11b92 65 20 74 69 6d 65 20 6d 61 78 69 6d 75 6d 20 6c  e time maximum l
11b93 69 6d 69 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  imits..    */.  
11b94 20 20 69 66 28 20 69 50 61 67 65 53 69 7a 65 3c    if( iPageSize<
11b95 35 31 32 20 20 20 20 20 20 20 20 20 20 20 20 20  512             
11b96 20 20 20 20 20 7c 7c 20 69 53 65 63 74 6f 72 53       || iSectorS
11b97 69 7a 65 3c 35 31 32 0a 20 20 20 20 20 7c 7c 20  ize<512.     || 
11b98 69 50 61 67 65 53 69 7a 65 3e 53 51 4c 49 54 45  iPageSize>SQLITE
11b99 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 7c  _MAX_PAGE_SIZE |
11b9a 7c 20 69 53 65 63 74 6f 72 53 69 7a 65 3e 4d 41  | iSectorSize>MA
11b9b 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 0a 20 20  X_SECTOR_SIZE.  
11b9c 20 20 20 7c 7c 20 28 28 69 50 61 67 65 53 69 7a     || ((iPageSiz
11b9d 65 2d 31 29 26 69 50 61 67 65 53 69 7a 65 29 21  e-1)&iPageSize)!
11b9e 3d 30 20 20 20 7c 7c 20 28 28 69 53 65 63 74 6f  =0   || ((iSecto
11b9f 72 53 69 7a 65 2d 31 29 26 69 53 65 63 74 6f 72  rSize-1)&iSector
11ba0 53 69 7a 65 29 21 3d 30 20 0a 20 20 20 20 29 7b  Size)!=0 .    ){
11ba1 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65  .      /* If the
11ba2 20 65 69 74 68 65 72 20 74 68 65 20 70 61 67 65   either the page
11ba3 2d 73 69 7a 65 20 6f 72 20 73 65 63 74 6f 72 2d  -size or sector-
11ba4 73 69 7a 65 20 69 6e 20 74 68 65 20 6a 6f 75 72  size in the jour
11ba5 6e 61 6c 2d 68 65 61 64 65 72 20 69 73 20 0a 20  nal-header is . 
11ba6 20 20 20 20 20 2a 2a 20 69 6e 76 61 6c 69 64 2c       ** invalid,
11ba7 20 74 68 65 6e 20 74 68 65 20 70 72 6f 63 65 73   then the proces
11ba8 73 20 74 68 61 74 20 77 72 6f 74 65 20 74 68 65  s that wrote the
11ba9 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20   journal-header 
11baa 6d 75 73 74 20 68 61 76 65 20 0a 20 20 20 20 20  must have .     
11bab 20 2a 2a 20 63 72 61 73 68 65 64 20 62 65 66 6f   ** crashed befo
11bac 72 65 20 74 68 65 20 68 65 61 64 65 72 20 77 61  re the header wa
11bad 73 20 73 79 6e 63 65 64 2e 20 49 6e 20 74 68 69  s synced. In thi
11bae 73 20 63 61 73 65 20 73 74 6f 70 20 72 65 61 64  s case stop read
11baf 69 6e 67 20 0a 20 20 20 20 20 20 2a 2a 20 74 68  ing .      ** th
11bb0 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68  e journal file h
11bb1 65 72 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ere..      */.  
11bb2 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
11bb3 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 0a 20  E_DONE;.    }.. 
11bb4 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65     /* Update the
11bb5 20 70 61 67 65 2d 73 69 7a 65 20 74 6f 20 6d 61   page-size to ma
11bb6 74 63 68 20 74 68 65 20 76 61 6c 75 65 20 72 65  tch the value re
11bb7 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72  ad from the jour
11bb8 6e 61 6c 2e 20 0a 20 20 20 20 2a 2a 20 55 73 65  nal. .    ** Use
11bb9 20 61 20 74 65 73 74 63 61 73 65 28 29 20 6d 61   a testcase() ma
11bba 63 72 6f 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  cro to make sure
11bbb 20 74 68 61 74 20 6d 61 6c 6c 6f 63 20 66 61 69   that malloc fai
11bbc 6c 75 72 65 20 77 69 74 68 69 6e 20 0a 20 20 20  lure within .   
11bbd 20 2a 2a 20 50 61 67 65 72 53 65 74 50 61 67 65   ** PagerSetPage
11bbe 73 69 7a 65 28 29 20 69 73 20 74 65 73 74 65 64  size() is tested
11bbf 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 50 61  ..    */.    iPa
11bc0 67 65 53 69 7a 65 31 36 20 3d 20 28 75 31 36 29  geSize16 = (u16)
11bc1 69 50 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 72  iPageSize;.    r
11bc2 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
11bc3 53 65 74 50 61 67 65 73 69 7a 65 28 70 50 61 67  SetPagesize(pPag
11bc4 65 72 2c 20 26 69 50 61 67 65 53 69 7a 65 31 36  er, &iPageSize16
11bc5 2c 20 2d 31 29 3b 0a 20 20 20 20 74 65 73 74 63  , -1);.    testc
11bc6 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45 5f  ase( rc!=SQLITE_
11bc7 4f 4b 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  OK );.    assert
11bc8 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
11bc9 7c 7c 20 69 50 61 67 65 53 69 7a 65 31 36 3d 3d  || iPageSize16==
11bca 28 75 31 36 29 69 50 61 67 65 53 69 7a 65 20 29  (u16)iPageSize )
11bcb 3b 0a 0a 20 20 20 20 2f 2a 20 55 70 64 61 74 65  ;..    /* Update
11bcc 20 74 68 65 20 61 73 73 75 6d 65 64 20 73 65 63   the assumed sec
11bcd 74 6f 72 2d 73 69 7a 65 20 74 6f 20 6d 61 74 63  tor-size to matc
11bce 68 20 74 68 65 20 76 61 6c 75 65 20 75 73 65 64  h the value used
11bcf 20 62 79 20 0a 20 20 20 20 2a 2a 20 74 68 65 20   by .    ** the 
11bd0 70 72 6f 63 65 73 73 20 74 68 61 74 20 63 72 65  process that cre
11bd1 61 74 65 64 20 74 68 69 73 20 6a 6f 75 72 6e 61  ated this journa
11bd2 6c 2e 20 49 66 20 74 68 69 73 20 6a 6f 75 72 6e  l. If this journ
11bd3 61 6c 20 77 61 73 0a 20 20 20 20 2a 2a 20 63 72  al was.    ** cr
11bd4 65 61 74 65 64 20 62 79 20 61 20 70 72 6f 63 65  eated by a proce
11bd5 73 73 20 6f 74 68 65 72 20 74 68 61 6e 20 74 68  ss other than th
11bd6 69 73 20 6f 6e 65 2c 20 74 68 65 6e 20 74 68 69  is one, then thi
11bd7 73 20 72 6f 75 74 69 6e 65 0a 20 20 20 20 2a 2a  s routine.    **
11bd8 20 69 73 20 62 65 69 6e 67 20 63 61 6c 6c 65 64   is being called
11bd9 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 70 61 67   from within pag
11bda 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 2e 20 54  er_playback(). T
11bdb 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65 0a 20  he local value. 
11bdc 20 20 20 2a 2a 20 6f 66 20 50 61 67 65 72 2e 73     ** of Pager.s
11bdd 65 63 74 6f 72 53 69 7a 65 20 69 73 20 72 65 73  ectorSize is res
11bde 74 6f 72 65 64 20 61 74 20 74 68 65 20 65 6e 64  tored at the end
11bdf 20 6f 66 20 74 68 61 74 20 72 6f 75 74 69 6e 65   of that routine
11be0 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 61  ..    */.    pPa
11be1 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20  ger->sectorSize 
11be2 3d 20 69 53 65 63 74 6f 72 53 69 7a 65 3b 0a 20  = iSectorSize;. 
11be3 20 7d 0a 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f   }..  pPager->jo
11be4 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 4a 4f 55 52  urnalOff += JOUR
11be5 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65  NAL_HDR_SZ(pPage
11be6 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  r);.  return rc;
11be7 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  .}.../*.** Write
11be8 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 6d 61   the supplied ma
11be9 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d  ster journal nam
11bea 65 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e  e into the journ
11beb 61 6c 20 66 69 6c 65 20 66 6f 72 20 70 61 67 65  al file for page
11bec 72 0a 2a 2a 20 70 50 61 67 65 72 20 61 74 20 74  r.** pPager at t
11bed 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 74  he current locat
11bee 69 6f 6e 2e 20 54 68 65 20 6d 61 73 74 65 72 20  ion. The master 
11bef 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 6d 75 73  journal name mus
11bf0 74 20 62 65 20 74 68 65 20 6c 61 73 74 0a 2a 2a  t be the last.**
11bf1 20 74 68 69 6e 67 20 77 72 69 74 74 65 6e 20 74   thing written t
11bf2 6f 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  o a journal file
11bf3 2e 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69  . If the pager i
11bf4 73 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 20 6d  s in full-sync m
11bf5 6f 64 65 2c 20 74 68 65 0a 2a 2a 20 6a 6f 75 72  ode, the.** jour
11bf6 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70  nal file descrip
11bf7 74 6f 72 20 69 73 20 61 64 76 61 6e 63 65 64 20  tor is advanced 
11bf8 74 6f 20 74 68 65 20 6e 65 78 74 20 73 65 63 74  to the next sect
11bf9 6f 72 20 62 6f 75 6e 64 61 72 79 20 62 65 66 6f  or boundary befo
11bfa 72 65 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 69  re.** anything i
11bfb 73 20 77 72 69 74 74 65 6e 2e 20 54 68 65 20 66  s written. The f
11bfc 6f 72 6d 61 74 20 69 73 3a 0a 2a 2a 0a 2a 2a 20  ormat is:.**.** 
11bfd 20 20 2b 20 34 20 62 79 74 65 73 3a 20 50 41 47    + 4 bytes: PAG
11bfe 45 52 5f 4d 4a 5f 50 47 4e 4f 2e 0a 2a 2a 20 20  ER_MJ_PGNO..**  
11bff 20 2b 20 4e 20 62 79 74 65 73 3a 20 4d 61 73 74   + N bytes: Mast
11c00 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e  er journal filen
11c01 61 6d 65 20 69 6e 20 75 74 66 2d 38 2e 0a 2a 2a  ame in utf-8..**
11c02 20 20 20 2b 20 34 20 62 79 74 65 73 3a 20 4e 20     + 4 bytes: N 
11c03 28 6c 65 6e 67 74 68 20 6f 66 20 6d 61 73 74 65  (length of maste
11c04 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 69  r journal name i
11c05 6e 20 62 79 74 65 73 2c 20 6e 6f 20 6e 75 6c 2d  n bytes, no nul-
11c06 74 65 72 6d 69 6e 61 74 6f 72 29 2e 0a 2a 2a 20  terminator)..** 
11c07 20 20 2b 20 34 20 62 79 74 65 73 3a 20 4d 61 73    + 4 bytes: Mas
11c08 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65  ter journal name
11c09 20 63 68 65 63 6b 73 75 6d 2e 0a 2a 2a 20 20 20   checksum..**   
11c0a 2b 20 38 20 62 79 74 65 73 3a 20 61 4a 6f 75 72  + 8 bytes: aJour
11c0b 6e 61 6c 4d 61 67 69 63 5b 5d 2e 0a 2a 2a 0a 2a  nalMagic[]..**.*
11c0c 2a 20 54 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  * The master jou
11c0d 72 6e 61 6c 20 70 61 67 65 20 63 68 65 63 6b 73  rnal page checks
11c0e 75 6d 20 69 73 20 74 68 65 20 73 75 6d 20 6f 66  um is the sum of
11c0f 20 74 68 65 20 62 79 74 65 73 20 69 6e 20 74 68   the bytes in th
11c10 65 20 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f 75 72  e master.** jour
11c11 6e 61 6c 20 6e 61 6d 65 2c 20 77 68 65 72 65 20  nal name, where 
11c12 65 61 63 68 20 62 79 74 65 20 69 73 20 69 6e 74  each byte is int
11c13 65 72 70 72 65 74 65 64 20 61 73 20 61 20 73 69  erpreted as a si
11c14 67 6e 65 64 20 38 2d 62 69 74 20 69 6e 74 65 67  gned 8-bit integ
11c15 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 4d 61  er..**.** If zMa
11c16 73 74 65 72 20 69 73 20 61 20 4e 55 4c 4c 20 70  ster is a NULL p
11c17 6f 69 6e 74 65 72 20 28 6f 63 63 75 72 73 20 66  ointer (occurs f
11c18 6f 72 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61  or a single data
11c19 62 61 73 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  base transaction
11c1a 29 2c 20 0a 2a 2a 20 74 68 69 73 20 63 61 6c 6c  ), .** this call
11c1b 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a   is a no-op..*/.
11c1c 73 74 61 74 69 63 20 69 6e 74 20 77 72 69 74 65  static int write
11c1d 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 50 61  MasterJournal(Pa
11c1e 67 65 72 20 2a 70 50 61 67 65 72 2c 20 63 6f 6e  ger *pPager, con
11c1f 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72  st char *zMaster
11c20 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  ){.  int rc;    
11c21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11c22 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
11c23 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d  code */.  int nM
11c24 61 73 74 65 72 3b 20 20 20 20 20 20 20 20 20 20  aster;          
11c25 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
11c26 6e 67 74 68 20 6f 66 20 73 74 72 69 6e 67 20 7a  ngth of string z
11c27 4d 61 73 74 65 72 20 2a 2f 0a 20 20 69 36 34 20  Master */.  i64 
11c28 69 48 64 72 4f 66 66 3b 20 20 20 20 20 20 20 20  iHdrOff;        
11c29 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11c2a 4f 66 66 73 65 74 20 6f 66 20 68 65 61 64 65 72  Offset of header
11c2b 20 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65   in journal file
11c2c 20 2a 2f 0a 20 20 69 36 34 20 6a 72 6e 6c 53 69   */.  i64 jrnlSi
11c2d 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ze;             
11c2e 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
11c2f 66 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f  f journal file o
11c30 6e 20 64 69 73 6b 20 2a 2f 0a 20 20 75 33 32 20  n disk */.  u32 
11c31 63 6b 73 75 6d 20 3d 20 30 3b 20 20 20 20 20 20  cksum = 0;      
11c32 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11c33 43 68 65 63 6b 73 75 6d 20 6f 66 20 73 74 72 69  Checksum of stri
11c34 6e 67 20 7a 4d 61 73 74 65 72 20 2a 2f 0a 0a 20  ng zMaster */.. 
11c35 20 69 66 28 20 21 7a 4d 61 73 74 65 72 20 7c 7c   if( !zMaster ||
11c36 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74   pPager->setMast
11c37 65 72 0a 20 20 20 7c 7c 20 70 50 61 67 65 72 2d  er.   || pPager-
11c38 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41  >journalMode==PA
11c39 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
11c3a 4d 45 4d 4f 52 59 20 0a 20 20 20 7c 7c 20 70 50  MEMORY .   || pP
11c3b 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64  ager->journalMod
11c3c 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e==PAGER_JOURNAL
11c3d 4d 4f 44 45 5f 4f 46 46 20 0a 20 20 29 7b 0a 20  MODE_OFF .  ){. 
11c3e 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
11c3f 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65  _OK;.  }.  pPage
11c40 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d 20 31  r->setMaster = 1
11c41 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70  ;.  assert( isOp
11c42 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
11c43 29 3b 0a 0a 20 20 2f 2a 20 43 61 6c 63 75 6c 61  );..  /* Calcula
11c44 74 65 20 74 68 65 20 6c 65 6e 67 74 68 20 69 6e  te the length in
11c45 20 62 79 74 65 73 20 61 6e 64 20 74 68 65 20 63   bytes and the c
11c46 68 65 63 6b 73 75 6d 20 6f 66 20 7a 4d 61 73 74  hecksum of zMast
11c47 65 72 20 2a 2f 0a 20 20 66 6f 72 28 6e 4d 61 73  er */.  for(nMas
11c48 74 65 72 3d 30 3b 20 7a 4d 61 73 74 65 72 5b 6e  ter=0; zMaster[n
11c49 4d 61 73 74 65 72 5d 3b 20 6e 4d 61 73 74 65 72  Master]; nMaster
11c4a 2b 2b 29 7b 0a 20 20 20 20 63 6b 73 75 6d 20 2b  ++){.    cksum +
11c4b 3d 20 7a 4d 61 73 74 65 72 5b 6e 4d 61 73 74 65  = zMaster[nMaste
11c4c 72 5d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66  r];.  }..  /* If
11c4d 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f   in full-sync mo
11c4e 64 65 2c 20 61 64 76 61 6e 63 65 20 74 6f 20 74  de, advance to t
11c4f 68 65 20 6e 65 78 74 20 64 69 73 6b 20 73 65 63  he next disk sec
11c50 74 6f 72 20 62 65 66 6f 72 65 20 77 72 69 74 69  tor before writi
11c51 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6d 61 73 74  ng.  ** the mast
11c52 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 2e  er journal name.
11c53 20 54 68 69 73 20 69 73 20 69 6e 20 63 61 73 65   This is in case
11c54 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 70 61   the previous pa
11c55 67 65 20 77 72 69 74 74 65 6e 20 74 6f 0a 20 20  ge written to.  
11c56 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68  ** the journal h
11c57 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
11c58 73 79 6e 63 65 64 2e 0a 20 20 2a 2f 0a 20 20 69  synced..  */.  i
11c59 66 28 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53  f( pPager->fullS
11c5a 79 6e 63 20 29 7b 0a 20 20 20 20 70 50 61 67 65  ync ){.    pPage
11c5b 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20  r->journalOff = 
11c5c 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74  journalHdrOffset
11c5d 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20  (pPager);.  }.  
11c5e 69 48 64 72 4f 66 66 20 3d 20 70 50 61 67 65 72  iHdrOff = pPager
11c5f 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20  ->journalOff;.. 
11c60 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6d 61   /* Write the ma
11c61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 64 61 74  ster journal dat
11c62 61 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20  a to the end of 
11c63 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
11c64 2e 20 49 66 0a 20 20 2a 2a 20 61 6e 20 65 72 72  . If.  ** an err
11c65 6f 72 20 6f 63 63 75 72 73 2c 20 72 65 74 75 72  or occurs, retur
11c66 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65  n the error code
11c67 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a   to the caller..
11c68 20 20 2a 2f 0a 20 20 69 66 28 20 28 30 20 21 3d    */.  if( (0 !=
11c69 20 28 72 63 20 3d 20 77 72 69 74 65 33 32 62 69   (rc = write32bi
11c6a 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  ts(pPager->jfd, 
11c6b 69 48 64 72 4f 66 66 2c 20 50 41 47 45 52 5f 4d  iHdrOff, PAGER_M
11c6c 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 29 29  J_PGNO(pPager)))
11c6d 29 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 28 72  ).   || (0 != (r
11c6e 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69  c = sqlite3OsWri
11c6f 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  te(pPager->jfd, 
11c70 7a 4d 61 73 74 65 72 2c 20 6e 4d 61 73 74 65 72  zMaster, nMaster
11c71 2c 20 69 48 64 72 4f 66 66 2b 34 29 29 29 0a 20  , iHdrOff+4))). 
11c72 20 20 7c 7c 20 28 30 20 21 3d 20 28 72 63 20 3d    || (0 != (rc =
11c73 20 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61   write32bits(pPa
11c74 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66  ger->jfd, iHdrOf
11c75 66 2b 34 2b 6e 4d 61 73 74 65 72 2c 20 6e 4d 61  f+4+nMaster, nMa
11c76 73 74 65 72 29 29 29 0a 20 20 20 7c 7c 20 28 30  ster))).   || (0
11c77 20 21 3d 20 28 72 63 20 3d 20 77 72 69 74 65 33   != (rc = write3
11c78 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66  2bits(pPager->jf
11c79 64 2c 20 69 48 64 72 4f 66 66 2b 34 2b 6e 4d 61  d, iHdrOff+4+nMa
11c7a 73 74 65 72 2b 34 2c 20 63 6b 73 75 6d 29 29 29  ster+4, cksum)))
11c7b 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 28 72 63  .   || (0 != (rc
11c7c 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74   = sqlite3OsWrit
11c7d 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 61  e(pPager->jfd, a
11c7e 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 38 2c  JournalMagic, 8,
11c7f 20 69 48 64 72 4f 66 66 2b 34 2b 6e 4d 61 73 74   iHdrOff+4+nMast
11c80 65 72 2b 38 29 29 29 0a 20 20 29 7b 0a 20 20 20  er+8))).  ){.   
11c81 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
11c82 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
11c83 6c 4f 66 66 20 2b 3d 20 28 6e 4d 61 73 74 65 72  lOff += (nMaster
11c84 2b 32 30 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e  +20);.  pPager->
11c85 6e 65 65 64 53 79 6e 63 20 3d 20 21 70 50 61 67  needSync = !pPag
11c86 65 72 2d 3e 6e 6f 53 79 6e 63 3b 0a 0a 20 20 2f  er->noSync;..  /
11c87 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69  * If the pager i
11c88 73 20 69 6e 20 70 65 72 69 73 74 65 6e 74 2d 6a  s in peristent-j
11c89 6f 75 72 6e 61 6c 20 6d 6f 64 65 2c 20 74 68 65  ournal mode, the
11c8a 6e 20 74 68 65 20 70 68 79 73 69 63 61 6c 20 0a  n the physical .
11c8b 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c    ** journal-fil
11c8c 65 20 6d 61 79 20 65 78 74 65 6e 64 20 70 61 73  e may extend pas
11c8d 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
11c8e 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20   master-journal 
11c8f 6e 61 6d 65 0a 20 20 2a 2a 20 61 6e 64 20 38 20  name.  ** and 8 
11c90 62 79 74 65 73 20 6f 66 20 6d 61 67 69 63 20 64  bytes of magic d
11c91 61 74 61 20 6a 75 73 74 20 77 72 69 74 74 65 6e  ata just written
11c92 20 74 6f 20 74 68 65 20 66 69 6c 65 2e 20 54 68   to the file. Th
11c93 69 73 20 69 73 20 0a 20 20 2a 2a 20 64 61 6e 67  is is .  ** dang
11c94 65 72 6f 75 73 20 62 65 63 61 75 73 65 20 74 68  erous because th
11c95 65 20 63 6f 64 65 20 74 6f 20 72 6f 6c 6c 62 61  e code to rollba
11c96 63 6b 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c  ck a hot-journal
11c97 20 66 69 6c 65 0a 20 20 2a 2a 20 77 69 6c 6c 20   file.  ** will 
11c98 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 6f 20 66  not be able to f
11c99 69 6e 64 20 74 68 65 20 6d 61 73 74 65 72 2d 6a  ind the master-j
11c9a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 74 6f 20 64  ournal name to d
11c9b 65 74 65 72 6d 69 6e 65 20 0a 20 20 2a 2a 20 77  etermine .  ** w
11c9c 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
11c9d 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 68 6f 74  e journal is hot
11c9e 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 61 73  . .  **.  ** Eas
11c9f 69 65 73 74 20 74 68 69 6e 67 20 74 6f 20 64 6f  iest thing to do
11ca0 20 69 6e 20 74 68 69 73 20 73 63 65 6e 61 72 69   in this scenari
11ca1 6f 20 69 73 20 74 6f 20 74 72 75 6e 63 61 74 65  o is to truncate
11ca2 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 0a 20 20   the journal .  
11ca3 2a 2a 20 66 69 6c 65 20 74 6f 20 74 68 65 20 72  ** file to the r
11ca4 65 71 75 69 72 65 64 20 73 69 7a 65 2e 0a 20 20  equired size..  
11ca5 2a 2f 20 0a 20 20 69 66 28 20 53 51 4c 49 54 45  */ .  if( SQLITE
11ca6 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74  _OK==(rc = sqlit
11ca7 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61  e3OsFileSize(pPa
11ca8 67 65 72 2d 3e 6a 66 64 2c 20 26 6a 72 6e 6c 53  ger->jfd, &jrnlS
11ca9 69 7a 65 29 29 0a 20 20 20 26 26 20 6a 72 6e 6c  ize)).   && jrnl
11caa 53 69 7a 65 3e 70 50 61 67 65 72 2d 3e 6a 6f 75  Size>pPager->jou
11cab 72 6e 61 6c 4f 66 66 0a 20 20 29 7b 0a 20 20 20  rnalOff.  ){.   
11cac 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54   rc = sqlite3OsT
11cad 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e  runcate(pPager->
11cae 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75  jfd, pPager->jou
11caf 72 6e 61 6c 4f 66 66 29 3b 0a 20 20 7d 0a 20 20  rnalOff);.  }.  
11cb0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
11cb1 0a 2a 2a 20 46 69 6e 64 20 61 20 70 61 67 65 20  .** Find a page 
11cb2 69 6e 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  in the hash tabl
11cb3 65 20 67 69 76 65 6e 20 69 74 73 20 70 61 67 65  e given its page
11cb4 20 6e 75 6d 62 65 72 2e 20 52 65 74 75 72 6e 0a   number. Return.
11cb5 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** a pointer to 
11cb6 74 68 65 20 70 61 67 65 20 6f 72 20 4e 55 4c 4c  the page or NULL
11cb7 20 69 66 20 74 68 65 20 72 65 71 75 65 73 74 65   if the requeste
11cb8 64 20 70 61 67 65 20 69 73 20 6e 6f 74 20 0a 2a  d page is not .*
11cb9 2a 20 61 6c 72 65 61 64 79 20 69 6e 20 6d 65 6d  * already in mem
11cba 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 50  ory..*/.static P
11cbb 67 48 64 72 20 2a 70 61 67 65 72 5f 6c 6f 6f 6b  gHdr *pager_look
11cbc 75 70 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  up(Pager *pPager
11cbd 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20  , Pgno pgno){.  
11cbe 50 67 48 64 72 20 2a 70 3b 20 20 20 20 20 20 20  PgHdr *p;       
11cbf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11cc0 20 20 2f 2a 20 52 65 74 75 72 6e 20 76 61 6c 75    /* Return valu
11cc1 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49 74 20 69 73  e */..  /* It is
11cc2 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 66 6f   not possible fo
11cc3 72 20 61 20 63 61 6c 6c 20 74 6f 20 50 63 61 63  r a call to Pcac
11cc4 68 65 46 65 74 63 68 28 29 20 77 69 74 68 20 63  heFetch() with c
11cc5 72 65 61 74 65 46 6c 61 67 3d 3d 30 20 74 6f 0a  reateFlag==0 to.
11cc6 20 20 2a 2a 20 66 61 69 6c 2c 20 73 69 6e 63 65    ** fail, since
11cc7 20 6e 6f 20 61 74 74 65 6d 70 74 20 74 6f 20 61   no attempt to a
11cc8 6c 6c 6f 63 61 74 65 20 64 79 6e 61 6d 69 63 20  llocate dynamic 
11cc9 6d 65 6d 6f 72 79 20 77 69 6c 6c 20 62 65 20 6d  memory will be m
11cca 61 64 65 2e 0a 20 20 2a 2f 0a 20 20 28 76 6f 69  ade..  */.  (voi
11ccb 64 29 73 71 6c 69 74 65 33 50 63 61 63 68 65 46  d)sqlite3PcacheF
11ccc 65 74 63 68 28 70 50 61 67 65 72 2d 3e 70 50 43  etch(pPager->pPC
11ccd 61 63 68 65 2c 20 70 67 6e 6f 2c 20 30 2c 20 26  ache, pgno, 0, &
11cce 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a  p);.  return p;.
11ccf 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20  }../*.** Unless 
11cd0 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20  the pager is in 
11cd1 65 72 72 6f 72 2d 73 74 61 74 65 2c 20 64 69 73  error-state, dis
11cd2 63 61 72 64 20 61 6c 6c 20 69 6e 2d 6d 65 6d 6f  card all in-memo
11cd3 72 79 20 70 61 67 65 73 2e 20 49 66 0a 2a 2a 20  ry pages. If.** 
11cd4 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20  the pager is in 
11cd5 65 72 72 6f 72 2d 73 74 61 74 65 2c 20 74 68 65  error-state, the
11cd6 6e 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 61  n this call is a
11cd7 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 4f   no-op..**.** TO
11cd8 44 4f 3a 20 57 68 79 20 63 61 6e 20 77 65 20 6e  DO: Why can we n
11cd9 6f 74 20 72 65 73 65 74 20 74 68 65 20 70 61 67  ot reset the pag
11cda 65 72 20 77 68 69 6c 65 20 69 6e 20 65 72 72 6f  er while in erro
11cdb 72 20 73 74 61 74 65 3f 0a 2a 2f 0a 73 74 61 74  r state?.*/.stat
11cdc 69 63 20 76 6f 69 64 20 70 61 67 65 72 5f 72 65  ic void pager_re
11cdd 73 65 74 28 50 61 67 65 72 20 2a 70 50 61 67 65  set(Pager *pPage
11cde 72 29 7b 0a 20 20 69 66 28 20 53 51 4c 49 54 45  r){.  if( SQLITE
11cdf 5f 4f 4b 3d 3d 70 50 61 67 65 72 2d 3e 65 72 72  _OK==pPager->err
11ce0 43 6f 64 65 20 29 7b 0a 20 20 20 20 73 71 6c 69  Code ){.    sqli
11ce1 74 65 33 42 61 63 6b 75 70 52 65 73 74 61 72 74  te3BackupRestart
11ce2 28 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70  (pPager->pBackup
11ce3 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63  );.    sqlite3Pc
11ce4 61 63 68 65 43 6c 65 61 72 28 70 50 61 67 65 72  acheClear(pPager
11ce5 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 20 20  ->pPCache);.    
11ce6 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61  pPager->dbSizeVa
11ce7 6c 69 64 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a  lid = 0;.  }.}..
11ce8 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6c 6c 20 73  /*.** Free all s
11ce9 74 72 75 63 74 75 72 65 73 20 69 6e 20 74 68 65  tructures in the
11cea 20 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e   Pager.aSavepoin
11ceb 74 5b 5d 20 61 72 72 61 79 20 61 6e 64 20 73 65  t[] array and se
11cec 74 20 62 6f 74 68 0a 2a 2a 20 50 61 67 65 72 2e  t both.** Pager.
11ced 61 53 61 76 65 70 6f 69 6e 74 20 61 6e 64 20 50  aSavepoint and P
11cee 61 67 65 72 2e 6e 53 61 76 65 70 6f 69 6e 74 20  ager.nSavepoint 
11cef 74 6f 20 7a 65 72 6f 2e 20 43 6c 6f 73 65 20 74  to zero. Close t
11cf0 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a  he sub-journal.*
11cf1 2a 20 69 66 20 69 74 20 69 73 20 6f 70 65 6e 20  * if it is open 
11cf2 61 6e 64 20 74 68 65 20 70 61 67 65 72 20 69 73  and the pager is
11cf3 20 6e 6f 74 20 69 6e 20 65 78 63 6c 75 73 69 76   not in exclusiv
11cf4 65 20 6d 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69  e mode..*/.stati
11cf5 63 20 76 6f 69 64 20 72 65 6c 65 61 73 65 41 6c  c void releaseAl
11cf6 6c 53 61 76 65 70 6f 69 6e 74 73 28 50 61 67 65  lSavepoints(Page
11cf7 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e  r *pPager){.  in
11cf8 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20  t ii;           
11cf9 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20      /* Iterator 
11cfa 66 6f 72 20 6c 6f 6f 70 69 6e 67 20 74 68 72 6f  for looping thro
11cfb 75 67 68 20 50 61 67 65 72 2e 61 53 61 76 65 70  ugh Pager.aSavep
11cfc 6f 69 6e 74 20 2a 2f 0a 20 20 66 6f 72 28 69 69  oint */.  for(ii
11cfd 3d 30 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e  =0; ii<pPager->n
11cfe 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29  Savepoint; ii++)
11cff 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 69 74  {.    sqlite3Bit
11d00 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 65  vecDestroy(pPage
11d01 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69  r->aSavepoint[ii
11d02 5d 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 29 3b  ].pInSavepoint);
11d03 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 50 61 67  .  }.  if( !pPag
11d04 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64  er->exclusiveMod
11d05 65 20 7c 7c 20 73 71 6c 69 74 65 33 49 73 4d 65  e || sqlite3IsMe
11d06 6d 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2d  mJournal(pPager-
11d07 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 20 73 71  >sjfd) ){.    sq
11d08 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61  lite3OsClose(pPa
11d09 67 65 72 2d 3e 73 6a 66 64 29 3b 0a 20 20 7d 0a  ger->sjfd);.  }.
11d0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
11d0b 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e  Pager->aSavepoin
11d0c 74 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 61 53  t);.  pPager->aS
11d0d 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 20 20  avepoint = 0;.  
11d0e 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69  pPager->nSavepoi
11d0f 6e 74 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72  nt = 0;.  pPager
11d10 2d 3e 6e 53 75 62 52 65 63 20 3d 20 30 3b 0a 7d  ->nSubRec = 0;.}
11d11 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
11d12 62 69 74 20 6e 75 6d 62 65 72 20 70 67 6e 6f 20  bit number pgno 
11d13 69 6e 20 74 68 65 20 50 61 67 65 72 53 61 76 65  in the PagerSave
11d14 70 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69  point.pInSavepoi
11d15 6e 74 20 0a 2a 2a 20 62 69 74 76 65 63 73 20 6f  nt .** bitvecs o
11d16 66 20 61 6c 6c 20 6f 70 65 6e 20 73 61 76 65 70  f all open savep
11d17 6f 69 6e 74 73 2e 20 52 65 74 75 72 6e 20 53 51  oints. Return SQ
11d18 4c 49 54 45 5f 4f 4b 20 69 66 20 73 75 63 63 65  LITE_OK if succe
11d19 73 73 66 75 6c 0a 2a 2a 20 6f 72 20 53 51 4c 49  ssful.** or SQLI
11d1a 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 61 20 6d 61  TE_NOMEM if a ma
11d1b 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f 63 63  lloc failure occ
11d1c 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  urs..*/.static i
11d1d 6e 74 20 61 64 64 54 6f 53 61 76 65 70 6f 69 6e  nt addToSavepoin
11d1e 74 42 69 74 76 65 63 73 28 50 61 67 65 72 20 2a  tBitvecs(Pager *
11d1f 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e  pPager, Pgno pgn
11d20 6f 29 7b 0a 20 20 69 6e 74 20 69 69 3b 20 20 20  o){.  int ii;   
11d21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11d22 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
11d23 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  */.  int rc = SQ
11d24 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 2f  LITE_OK;       /
11d25 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20 2a 2f  * Result code */
11d26 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ..  for(ii=0; ii
11d27 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f  <pPager->nSavepo
11d28 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  int; ii++){.    
11d29 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a  PagerSavepoint *
11d2a 70 20 3d 20 26 70 50 61 67 65 72 2d 3e 61 53 61  p = &pPager->aSa
11d2b 76 65 70 6f 69 6e 74 5b 69 69 5d 3b 0a 20 20 20  vepoint[ii];.   
11d2c 20 69 66 28 20 70 67 6e 6f 3c 3d 70 2d 3e 6e 4f   if( pgno<=p->nO
11d2d 72 69 67 20 29 7b 0a 20 20 20 20 20 20 72 63 20  rig ){.      rc 
11d2e 7c 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63  |= sqlite3Bitvec
11d2f 53 65 74 28 70 2d 3e 70 49 6e 53 61 76 65 70 6f  Set(p->pInSavepo
11d30 69 6e 74 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20  int, pgno);.    
11d31 20 20 74 65 73 74 63 61 73 65 28 20 72 63 3d 3d    testcase( rc==
11d32 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a  SQLITE_NOMEM );.
11d33 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63        assert( rc
11d34 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72  ==SQLITE_OK || r
11d35 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
11d36 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  );.    }.  }.  r
11d37 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
11d38 2a 2a 20 55 6e 6c 6f 63 6b 20 74 68 65 20 64 61  ** Unlock the da
11d39 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 69  tabase file. Thi
11d3a 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20  s function is a 
11d3b 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 70 61 67  no-op if the pag
11d3c 65 72 0a 2a 2a 20 69 73 20 69 6e 20 65 78 63 6c  er.** is in excl
11d3d 75 73 69 76 65 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a  usive mode..**.*
11d3e 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69  * If the pager i
11d3f 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 65  s currently in e
11d40 72 72 6f 72 20 73 74 61 74 65 2c 20 64 69 73 63  rror state, disc
11d41 61 72 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  ard the contents
11d42 20 6f 66 20 0a 2a 2a 20 74 68 65 20 63 61 63 68   of .** the cach
11d43 65 20 61 6e 64 20 72 65 73 65 74 20 74 68 65 20  e and reset the 
11d44 50 61 67 65 72 20 73 74 72 75 63 74 75 72 65 20  Pager structure 
11d45 69 6e 74 65 72 6e 61 6c 20 73 74 61 74 65 2e 20  internal state. 
11d46 49 66 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 61  If there is.** a
11d47 6e 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 2d 66  n open journal-f
11d48 69 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 6e 65  ile, then the ne
11d49 78 74 20 74 69 6d 65 20 61 20 73 68 61 72 65 64  xt time a shared
11d4a 2d 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65  -lock is obtaine
11d4b 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 70 61 67 65  d.** on the page
11d4c 72 20 66 69 6c 65 20 28 62 79 20 74 68 69 73 20  r file (by this 
11d4d 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f  or any other pro
11d4e 63 65 73 73 29 2c 20 69 74 20 77 69 6c 6c 20 62  cess), it will b
11d4f 65 0a 2a 2a 20 74 72 65 61 74 65 64 20 61 73 20  e.** treated as 
11d50 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 61 6e  a hot-journal an
11d51 64 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a  d rolled back..*
11d52 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61  /.static void pa
11d53 67 65 72 5f 75 6e 6c 6f 63 6b 28 50 61 67 65 72  ger_unlock(Pager
11d54 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28   *pPager){.  if(
11d55 20 21 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73   !pPager->exclus
11d56 69 76 65 4d 6f 64 65 20 29 7b 0a 20 20 20 20 69  iveMode ){.    i
11d57 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
11d58 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
11d59 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20  eturn code */.. 
11d5a 20 20 20 2f 2a 20 41 6c 77 61 79 73 20 63 6c 6f     /* Always clo
11d5b 73 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  se the journal f
11d5c 69 6c 65 20 77 68 65 6e 20 64 72 6f 70 70 69 6e  ile when droppin
11d5d 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6c  g the database l
11d5e 6f 63 6b 2e 0a 20 20 20 20 2a 2a 20 4f 74 68 65  ock..    ** Othe
11d5f 72 77 69 73 65 2c 20 61 6e 6f 74 68 65 72 20 63  rwise, another c
11d60 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 74 68 20 6a  onnection with j
11d61 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 64 65 6c 65  ournal_mode=dele
11d62 74 65 20 6d 69 67 68 74 0a 20 20 20 20 2a 2a 20  te might.    ** 
11d63 64 65 6c 65 74 65 20 74 68 65 20 66 69 6c 65 20  delete the file 
11d64 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 75  out from under u
11d65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  s..    */.    sq
11d66 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61  lite3OsClose(pPa
11d67 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 73  ger->jfd);.    s
11d68 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74  qlite3BitvecDest
11d69 72 6f 79 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a  roy(pPager->pInJ
11d6a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 70 50 61  ournal);.    pPa
11d6b 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20  ger->pInJournal 
11d6c 3d 20 30 3b 0a 20 20 20 20 72 65 6c 65 61 73 65  = 0;.    release
11d6d 41 6c 6c 53 61 76 65 70 6f 69 6e 74 73 28 70 50  AllSavepoints(pP
11d6e 61 67 65 72 29 3b 0a 0a 20 20 20 20 2f 2a 20 49  ager);..    /* I
11d6f 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 75 6e  f the file is un
11d70 6c 6f 63 6b 65 64 2c 20 73 6f 6d 65 62 6f 64 79  locked, somebody
11d71 20 65 6c 73 65 20 6d 69 67 68 74 20 63 68 61 6e   else might chan
11d72 67 65 20 69 74 2e 20 54 68 65 0a 20 20 20 20 2a  ge it. The.    *
11d73 2a 20 76 61 6c 75 65 73 20 73 74 6f 72 65 64 20  * values stored 
11d74 69 6e 20 50 61 67 65 72 2e 64 62 53 69 7a 65 20  in Pager.dbSize 
11d75 65 74 63 2e 20 6d 69 67 68 74 20 62 65 63 6f 6d  etc. might becom
11d76 65 20 69 6e 76 61 6c 69 64 20 69 66 0a 20 20 20  e invalid if.   
11d77 20 2a 2a 20 74 68 69 73 20 68 61 70 70 65 6e 73   ** this happens
11d78 2e 20 54 4f 44 4f 3a 20 52 65 61 6c 6c 79 2c 20  . TODO: Really, 
11d79 74 68 69 73 20 64 6f 65 73 6e 27 74 20 6e 65 65  this doesn't nee
11d7a 64 20 74 6f 20 62 65 20 63 6c 65 61 72 65 64 0a  d to be cleared.
11d7b 20 20 20 20 2a 2a 20 75 6e 74 69 6c 20 74 68 65      ** until the
11d7c 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20   change-counter 
11d7d 63 68 65 63 6b 20 66 61 69 6c 73 20 69 6e 20 50  check fails in P
11d7e 61 67 65 72 53 68 61 72 65 64 4c 6f 63 6b 28 29  agerSharedLock()
11d7f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 61  ..    */.    pPa
11d80 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64  ger->dbSizeValid
11d81 20 3d 20 30 3b 0a 0a 20 20 20 20 72 63 20 3d 20   = 0;..    rc = 
11d82 6f 73 55 6e 6c 6f 63 6b 28 70 50 61 67 65 72 2d  osUnlock(pPager-
11d83 3e 66 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20  >fd, NO_LOCK);. 
11d84 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20     if( rc ){.   
11d85 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f     pPager->errCo
11d86 64 65 20 3d 20 72 63 3b 0a 20 20 20 20 7d 0a 20  de = rc;.    }. 
11d87 20 20 20 49 4f 54 52 41 43 45 28 28 22 55 4e 4c     IOTRACE(("UNL
11d88 4f 43 4b 20 25 70 5c 6e 22 2c 20 70 50 61 67 65  OCK %p\n", pPage
11d89 72 29 29 0a 0a 20 20 20 20 2f 2a 20 49 66 20 50  r))..    /* If P
11d8a 61 67 65 72 2e 65 72 72 43 6f 64 65 20 69 73 20  ager.errCode is 
11d8b 73 65 74 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74  set, the content
11d8c 73 20 6f 66 20 74 68 65 20 70 61 67 65 72 20 63  s of the pager c
11d8d 61 63 68 65 20 63 61 6e 6e 6f 74 20 62 65 0a 20  ache cannot be. 
11d8e 20 20 20 2a 2a 20 74 72 75 73 74 65 64 2e 20 4e     ** trusted. N
11d8f 6f 77 20 74 68 61 74 20 74 68 65 20 70 61 67 65  ow that the page
11d90 72 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b  r file is unlock
11d91 65 64 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  ed, the contents
11d92 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 63   of the.    ** c
11d93 61 63 68 65 20 63 61 6e 20 62 65 20 64 69 73 63  ache can be disc
11d94 61 72 64 65 64 20 61 6e 64 20 74 68 65 20 65 72  arded and the er
11d95 72 6f 72 20 63 6f 64 65 20 73 61 66 65 6c 79 20  ror code safely 
11d96 63 6c 65 61 72 65 64 2e 0a 20 20 20 20 2a 2f 0a  cleared..    */.
11d97 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e      if( pPager->
11d98 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 20  errCode ){.     
11d99 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
11d9a 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50  OK ){.        pP
11d9b 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20  ager->errCode = 
11d9c 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20  SQLITE_OK;.     
11d9d 20 7d 0a 20 20 20 20 20 20 70 61 67 65 72 5f 72   }.      pager_r
11d9e 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20  eset(pPager);.  
11d9f 20 20 7d 0a 0a 20 20 20 20 70 50 61 67 65 72 2d    }..    pPager-
11da0 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65  >changeCountDone
11da1 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72   = 0;.    pPager
11da2 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f  ->state = PAGER_
11da3 55 4e 4c 4f 43 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f  UNLOCK;.  }.}../
11da4 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
11da5 6f 6e 20 73 68 6f 75 6c 64 20 62 65 20 63 61 6c  on should be cal
11da6 6c 65 64 20 77 68 65 6e 20 61 6e 20 49 4f 45 52  led when an IOER
11da7 52 2c 20 43 4f 52 52 55 50 54 20 6f 72 20 46 55  R, CORRUPT or FU
11da8 4c 4c 20 65 72 72 6f 72 0a 2a 2a 20 6d 61 79 20  LL error.** may 
11da9 68 61 76 65 20 6f 63 63 75 72 72 65 64 2e 20 54  have occurred. T
11daa 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
11dab 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  t is a pointer t
11dac 6f 20 74 68 65 20 70 61 67 65 72 20 0a 2a 2a 20  o the pager .** 
11dad 73 74 72 75 63 74 75 72 65 2c 20 74 68 65 20 73  structure, the s
11dae 65 63 6f 6e 64 20 74 68 65 20 65 72 72 6f 72 2d  econd the error-
11daf 63 6f 64 65 20 61 62 6f 75 74 20 74 6f 20 62 65  code about to be
11db0 20 72 65 74 75 72 6e 65 64 20 62 79 20 61 20 70   returned by a p
11db1 61 67 65 72 20 0a 2a 2a 20 41 50 49 20 66 75 6e  ager .** API fun
11db2 63 74 69 6f 6e 2e 20 54 68 65 20 76 61 6c 75 65  ction. The value
11db3 20 72 65 74 75 72 6e 65 64 20 69 73 20 61 20 63   returned is a c
11db4 6f 70 79 20 6f 66 20 74 68 65 20 73 65 63 6f 6e  opy of the secon
11db5 64 20 61 72 67 75 6d 65 6e 74 20 0a 2a 2a 20 74  d argument .** t
11db6 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  o this function.
11db7 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73   .**.** If the s
11db8 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69  econd argument i
11db9 73 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 2c 20  s SQLITE_IOERR, 
11dba 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2c 20  SQLITE_CORRUPT, 
11dbb 6f 72 20 53 51 4c 49 54 45 5f 46 55 4c 4c 0a 2a  or SQLITE_FULL.*
11dbc 2a 20 74 68 65 20 65 72 72 6f 72 20 62 65 63 6f  * the error beco
11dbd 6d 65 73 20 70 65 72 73 69 73 74 65 6e 74 2e 20  mes persistent. 
11dbe 55 6e 74 69 6c 20 74 68 65 20 70 65 72 73 69 73  Until the persis
11dbf 74 65 6e 20 65 72 72 6f 72 20 69 73 20 63 6c 65  ten error is cle
11dc0 61 72 65 64 2c 0a 2a 2a 20 73 75 62 73 65 71 75  ared,.** subsequ
11dc1 65 6e 74 20 41 50 49 20 63 61 6c 6c 73 20 6f 6e  ent API calls on
11dc2 20 74 68 69 73 20 50 61 67 65 72 20 77 69 6c 6c   this Pager will
11dc3 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 72 65 74   immediately ret
11dc4 75 72 6e 20 74 68 65 20 73 61 6d 65 20 0a 2a 2a  urn the same .**
11dc5 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a   error code..**.
11dc6 2a 2a 20 41 20 70 65 72 73 69 73 74 65 6e 74 20  ** A persistent 
11dc7 65 72 72 6f 72 20 69 6e 64 69 63 61 74 65 73 20  error indicates 
11dc8 74 68 61 74 20 74 68 65 20 63 6f 6e 74 65 6e 74  that the content
11dc9 73 20 6f 66 20 74 68 65 20 70 61 67 65 72 2d 63  s of the pager-c
11dca 61 63 68 65 20 0a 2a 2a 20 63 61 6e 6e 6f 74 20  ache .** cannot 
11dcb 62 65 20 74 72 75 73 74 65 64 2e 20 54 68 69 73  be trusted. This
11dcc 20 73 74 61 74 65 20 63 61 6e 20 62 65 20 63 6c   state can be cl
11dcd 65 61 72 65 64 20 62 79 20 63 6f 6d 70 6c 65 74  eared by complet
11dce 65 6c 79 20 64 69 73 63 61 72 64 69 6e 67 20 0a  ely discarding .
11dcf 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  ** the contents 
11dd0 6f 66 20 74 68 65 20 70 61 67 65 72 2d 63 61 63  of the pager-cac
11dd1 68 65 2e 20 49 66 20 61 20 74 72 61 6e 73 61 63  he. If a transac
11dd2 74 69 6f 6e 20 77 61 73 20 61 63 74 69 76 65 20  tion was active 
11dd3 77 68 65 6e 0a 2a 2a 20 74 68 65 20 70 65 72 73  when.** the pers
11dd4 69 73 74 65 6e 74 20 65 72 72 6f 72 20 6f 63 63  istent error occ
11dd5 75 72 72 65 64 2c 20 74 68 65 6e 20 74 68 65 20  urred, then the 
11dd6 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
11dd7 20 6d 61 79 20 6e 65 65 64 0a 2a 2a 20 74 6f 20   may need.** to 
11dd8 62 65 20 72 65 70 6c 61 79 65 64 20 74 6f 20 72  be replayed to r
11dd9 65 73 74 6f 72 65 20 74 68 65 20 63 6f 6e 74 65  estore the conte
11dda 6e 74 73 20 6f 66 20 74 68 65 20 64 61 74 61 62  nts of the datab
11ddb 61 73 65 20 66 69 6c 65 20 28 61 73 20 69 66 0a  ase file (as if.
11ddc 2a 2a 20 69 74 20 77 65 72 65 20 61 20 68 6f 74  ** it were a hot
11ddd 2d 6a 6f 75 72 6e 61 6c 29 2e 0a 2a 2f 0a 73 74  -journal)..*/.st
11dde 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 65  atic int pager_e
11ddf 72 72 6f 72 28 50 61 67 65 72 20 2a 70 50 61 67  rror(Pager *pPag
11de0 65 72 2c 20 69 6e 74 20 72 63 29 7b 0a 20 20 69  er, int rc){.  i
11de1 6e 74 20 72 63 32 20 3d 20 72 63 20 26 20 30 78  nt rc2 = rc & 0x
11de2 66 66 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63  ff;.  assert( rc
11de3 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 21  ==SQLITE_OK || !
11de4 4d 45 4d 44 42 20 29 3b 0a 20 20 61 73 73 65 72  MEMDB );.  asser
11de5 74 28 0a 20 20 20 20 20 20 20 70 50 61 67 65 72  t(.       pPager
11de6 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54  ->errCode==SQLIT
11de7 45 5f 46 55 4c 4c 20 7c 7c 0a 20 20 20 20 20 20  E_FULL ||.      
11de8 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
11de9 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 0a 20  ==SQLITE_OK ||. 
11dea 20 20 20 20 20 20 28 70 50 61 67 65 72 2d 3e 65        (pPager->e
11deb 72 72 43 6f 64 65 20 26 20 30 78 66 66 29 3d 3d  rrCode & 0xff)==
11dec 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a 20 20 29  SQLITE_IOERR.  )
11ded 3b 0a 20 20 69 66 28 20 72 63 32 3d 3d 53 51 4c  ;.  if( rc2==SQL
11dee 49 54 45 5f 46 55 4c 4c 20 7c 7c 20 72 63 32 3d  ITE_FULL || rc2=
11def 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b  =SQLITE_IOERR ){
11df0 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72  .    pPager->err
11df1 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 7d 0a 20  Code = rc;.  }. 
11df2 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
11df3 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 61 20 72  *.** Execute a r
11df4 6f 6c 6c 62 61 63 6b 20 69 66 20 61 20 74 72 61  ollback if a tra
11df5 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69  nsaction is acti
11df6 76 65 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 68  ve and unlock th
11df7 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66  e .** database f
11df8 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ile. .**.** If t
11df9 68 65 20 70 61 67 65 72 20 68 61 73 20 61 6c 72  he pager has alr
11dfa 65 61 64 79 20 65 6e 74 65 72 65 64 20 74 68 65  eady entered the
11dfb 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20 64 6f   error state, do
11dfc 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 0a 2a 2a   not attempt .**
11dfd 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 61 74   the rollback at
11dfe 20 74 68 69 73 20 74 69 6d 65 2e 20 49 6e 73 74   this time. Inst
11dff 65 61 64 2c 20 70 61 67 65 72 5f 75 6e 6c 6f 63  ead, pager_unloc
11e00 6b 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 20 54  k() is called. T
11e01 68 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 70 61  he.** call to pa
11e02 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 77 69 6c  ger_unlock() wil
11e03 6c 20 64 69 73 63 61 72 64 20 61 6c 6c 20 69 6e  l discard all in
11e04 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 2c 20 75  -memory pages, u
11e05 6e 6c 6f 63 6b 0a 2a 2a 20 74 68 65 20 64 61 74  nlock.** the dat
11e06 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 63  abase file and c
11e07 6c 65 61 72 20 74 68 65 20 65 72 72 6f 72 20 73  lear the error s
11e08 74 61 74 65 2e 20 49 66 20 74 68 69 73 20 6d 65  tate. If this me
11e09 61 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 72  ans that.** ther
11e0a 65 20 69 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e  e is a hot-journ
11e0b 61 6c 20 6c 65 66 74 20 69 6e 20 74 68 65 20 66  al left in the f
11e0c 69 6c 65 2d 73 79 73 74 65 6d 2c 20 74 68 65 20  ile-system, the 
11e0d 6e 65 78 74 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a  next connection.
11e0e 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 73  ** to obtain a s
11e0f 68 61 72 65 64 20 6c 6f 63 6b 20 6f 6e 20 74 68  hared lock on th
11e10 65 20 70 61 67 65 72 20 28 77 68 69 63 68 20 6d  e pager (which m
11e11 61 79 20 62 65 20 74 68 69 73 20 6f 6e 65 29 20  ay be this one) 
11e12 77 69 6c 6c 0a 2a 2a 20 72 6f 6c 6c 20 69 74 20  will.** roll it 
11e13 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  back..**.** If t
11e14 68 65 20 70 61 67 65 72 20 68 61 73 20 6e 6f 74  he pager has not
11e15 20 61 6c 72 65 61 64 79 20 65 6e 74 65 72 65 64   already entered
11e16 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65   the error state
11e17 2c 20 62 75 74 20 61 6e 20 49 4f 20 6f 72 0a 2a  , but an IO or.*
11e18 2a 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 20 6f  * malloc error o
11e19 63 63 75 72 73 20 64 75 72 69 6e 67 20 61 20 72  ccurs during a r
11e1a 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 6e 20 74 68  ollback, then th
11e1b 69 73 20 77 69 6c 6c 20 69 74 73 65 6c 66 20 63  is will itself c
11e1c 61 75 73 65 20 0a 2a 2a 20 74 68 65 20 70 61 67  ause .** the pag
11e1d 65 72 20 74 6f 20 65 6e 74 65 72 20 74 68 65 20  er to enter the 
11e1e 65 72 72 6f 72 20 73 74 61 74 65 2e 20 57 68 69  error state. Whi
11e1f 63 68 20 77 69 6c 6c 20 62 65 20 63 6c 65 61 72  ch will be clear
11e20 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 63 61 6c  ed by the.** cal
11e21 6c 20 74 6f 20 70 61 67 65 72 5f 75 6e 6c 6f 63  l to pager_unloc
11e22 6b 28 29 2c 20 61 73 20 64 65 73 63 72 69 62 65  k(), as describe
11e23 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 73 74 61 74  d above..*/.stat
11e24 69 63 20 76 6f 69 64 20 70 61 67 65 72 55 6e 6c  ic void pagerUnl
11e25 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 50  ockAndRollback(P
11e26 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
11e27 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72   if( pPager->err
11e28 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  Code==SQLITE_OK 
11e29 26 26 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  && pPager->state
11e2a 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 44  >=PAGER_RESERVED
11e2b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42   ){.    sqlite3B
11e2c 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  eginBenignMalloc
11e2d 28 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50  ();.    sqlite3P
11e2e 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70 50 61  agerRollback(pPa
11e2f 67 65 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ger);.    sqlite
11e30 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  3EndBenignMalloc
11e31 28 29 3b 0a 20 20 7d 0a 20 20 70 61 67 65 72 5f  ();.  }.  pager_
11e32 75 6e 6c 6f 63 6b 28 70 50 61 67 65 72 29 3b 0a  unlock(pPager);.
11e33 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
11e34 75 74 69 6e 65 20 65 6e 64 73 20 61 20 74 72 61  utine ends a tra
11e35 6e 73 61 63 74 69 6f 6e 2e 20 41 20 74 72 61 6e  nsaction. A tran
11e36 73 61 63 74 69 6f 6e 20 69 73 20 75 73 75 61 6c  saction is usual
11e37 6c 79 20 65 6e 64 65 64 20 62 79 20 0a 2a 2a 20  ly ended by .** 
11e38 65 69 74 68 65 72 20 61 20 43 4f 4d 4d 49 54 20  either a COMMIT 
11e39 6f 72 20 61 20 52 4f 4c 4c 42 41 43 4b 20 6f 70  or a ROLLBACK op
11e3a 65 72 61 74 69 6f 6e 2e 20 54 68 69 73 20 72 6f  eration. This ro
11e3b 75 74 69 6e 65 20 6d 61 79 20 62 65 20 63 61 6c  utine may be cal
11e3c 6c 65 64 20 0a 2a 2a 20 61 66 74 65 72 20 72 6f  led .** after ro
11e3d 6c 6c 62 61 63 6b 20 6f 66 20 61 20 68 6f 74 2d  llback of a hot-
11e3e 6a 6f 75 72 6e 61 6c 2c 20 6f 72 20 69 66 20 61  journal, or if a
11e3f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77  n error occurs w
11e40 68 69 6c 65 20 6f 70 65 6e 69 6e 67 0a 2a 2a 20  hile opening.** 
11e41 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
11e42 20 6f 72 20 77 72 69 74 69 6e 67 20 74 68 65 20   or writing the 
11e43 76 65 72 79 20 66 69 72 73 74 20 6a 6f 75 72 6e  very first journ
11e44 61 6c 2d 68 65 61 64 65 72 20 6f 66 20 61 0a 2a  al-header of a.*
11e45 2a 20 64 61 74 61 62 61 73 65 20 74 72 61 6e 73  * database trans
11e46 61 63 74 69 6f 6e 2e 0a 2a 2a 20 0a 2a 2a 20 49  action..** .** I
11e47 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69  f the pager is i
11e48 6e 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 6f  n PAGER_SHARED o
11e49 72 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 73  r PAGER_UNLOCK s
11e4a 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 0a 2a  tate when this.*
11e4b 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  * routine is cal
11e4c 6c 65 64 2c 20 69 74 20 69 73 20 61 20 6e 6f 2d  led, it is a no-
11e4d 6f 70 20 28 72 65 74 75 72 6e 73 20 53 51 4c 49  op (returns SQLI
11e4e 54 45 5f 4f 4b 29 2e 0a 2a 2a 0a 2a 2a 20 4f 74  TE_OK)..**.** Ot
11e4f 68 65 72 77 69 73 65 2c 20 61 6e 79 20 61 63 74  herwise, any act
11e50 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20 61  ive savepoints a
11e51 72 65 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 0a  re released..**.
11e52 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61  ** If the journa
11e53 6c 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 2c 20  l file is open, 
11e54 74 68 65 6e 20 69 74 20 69 73 20 22 66 69 6e 61  then it is "fina
11e55 6c 69 7a 65 64 22 2e 20 4f 6e 63 65 20 61 20 6a  lized". Once a j
11e56 6f 75 72 6e 61 6c 20 0a 2a 2a 20 66 69 6c 65 20  ournal .** file 
11e57 68 61 73 20 62 65 65 6e 20 66 69 6e 61 6c 69 7a  has been finaliz
11e58 65 64 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73  ed it is not pos
11e59 73 69 62 6c 65 20 74 6f 20 75 73 65 20 69 74 20  sible to use it 
11e5a 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 61 20 0a  to roll back a .
11e5b 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  ** transaction. 
11e5c 4e 6f 72 20 77 69 6c 6c 20 69 74 20 62 65 20 63  Nor will it be c
11e5d 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 62 65 20  onsidered to be 
11e5e 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62 79  a hot-journal by
11e5f 20 74 68 69 73 0a 2a 2a 20 6f 72 20 61 6e 79 20   this.** or any 
11e60 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63  other database c
11e61 6f 6e 6e 65 63 74 69 6f 6e 2e 20 45 78 61 63 74  onnection. Exact
11e62 6c 79 20 68 6f 77 20 61 20 6a 6f 75 72 6e 61 6c  ly how a journal
11e63 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 0a 2a 2a   is finalized.**
11e64 20 64 65 70 65 6e 64 73 20 6f 6e 20 77 68 65 74   depends on whet
11e65 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 70  her or not the p
11e66 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 20  ager is running 
11e67 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64  in exclusive mod
11e68 65 20 61 6e 64 0a 2a 2a 20 74 68 65 20 63 75 72  e and.** the cur
11e69 72 65 6e 74 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64  rent journal-mod
11e6a 65 20 28 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c  e (Pager.journal
11e6b 4d 6f 64 65 20 76 61 6c 75 65 29 2c 20 61 73 20  Mode value), as 
11e6c 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20  follows:.**.**  
11e6d 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 4d 45   journalMode==ME
11e6e 4d 4f 52 59 0a 2a 2a 20 20 20 20 20 4a 6f 75 72  MORY.**     Jour
11e6f 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70  nal file descrip
11e70 74 6f 72 20 69 73 20 73 69 6d 70 6c 79 20 63 6c  tor is simply cl
11e71 6f 73 65 64 2e 20 54 68 69 73 20 64 65 73 74 72  osed. This destr
11e72 6f 79 73 20 61 6e 20 0a 2a 2a 20 20 20 20 20 69  oys an .**     i
11e73 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c  n-memory journal
11e74 2e 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 72 6e 61  ..**.**   journa
11e75 6c 4d 6f 64 65 3d 3d 54 52 55 4e 43 41 54 45 0a  lMode==TRUNCATE.
11e76 2a 2a 20 20 20 20 20 4a 6f 75 72 6e 61 6c 20 66  **     Journal f
11e77 69 6c 65 20 69 73 20 74 72 75 6e 63 61 74 65 64  ile is truncated
11e78 20 74 6f 20 7a 65 72 6f 20 62 79 74 65 73 20 69   to zero bytes i
11e79 6e 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 20 20  n size..**.**   
11e7a 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 45 52  journalMode==PER
11e7b 53 49 53 54 0a 2a 2a 20 20 20 20 20 54 68 65 20  SIST.**     The 
11e7c 66 69 72 73 74 20 32 38 20 62 79 74 65 73 20 6f  first 28 bytes o
11e7d 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  f the journal fi
11e7e 6c 65 20 61 72 65 20 7a 65 72 6f 65 64 2e 20 54  le are zeroed. T
11e7f 68 69 73 20 69 6e 76 61 6c 69 64 61 74 65 73 0a  his invalidates.
11e80 2a 2a 20 20 20 20 20 74 68 65 20 66 69 72 73 74  **     the first
11e81 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20   journal header 
11e82 69 6e 20 74 68 65 20 66 69 6c 65 2c 20 61 6e 64  in the file, and
11e83 20 68 65 6e 63 65 20 74 68 65 20 65 6e 74 69 72   hence the entir
11e84 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20  e journal.**    
11e85 20 66 69 6c 65 2e 20 41 6e 20 69 6e 76 61 6c 69   file. An invali
11e86 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63  d journal file c
11e87 61 6e 6e 6f 74 20 62 65 20 72 6f 6c 6c 65 64 20  annot be rolled 
11e88 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f  back..**.**   jo
11e89 75 72 6e 61 6c 4d 6f 64 65 3d 3d 44 45 4c 45 54  urnalMode==DELET
11e8a 45 0a 2a 2a 20 20 20 20 20 54 68 65 20 6a 6f 75  E.**     The jou
11e8b 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 63 6c 6f  rnal file is clo
11e8c 73 65 64 20 61 6e 64 20 64 65 6c 65 74 65 64 20  sed and deleted 
11e8d 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4f 73 44  using sqlite3OsD
11e8e 65 6c 65 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 20  elete()..**.**  
11e8f 20 20 20 49 66 20 74 68 65 20 70 61 67 65 72 20     If the pager 
11e90 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78  is running in ex
11e91 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20 74 68  clusive mode, th
11e92 69 73 20 6d 65 74 68 6f 64 20 6f 66 20 66 69 6e  is method of fin
11e93 61 6c 69 7a 69 6e 67 0a 2a 2a 20 20 20 20 20 74  alizing.**     t
11e94 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
11e95 69 73 20 6e 65 76 65 72 20 75 73 65 64 2e 20 49  is never used. I
11e96 6e 73 74 65 61 64 2c 20 69 66 20 74 68 65 20 6a  nstead, if the j
11e97 6f 75 72 6e 61 6c 4d 6f 64 65 20 69 73 0a 2a 2a  ournalMode is.**
11e98 20 20 20 20 20 44 45 4c 45 54 45 20 61 6e 64 20       DELETE and 
11e99 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20  the pager is in 
11e9a 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20  exclusive mode, 
11e9b 74 68 65 20 6d 65 74 68 6f 64 20 64 65 73 63 72  the method descr
11e9c 69 62 65 64 20 75 6e 64 65 72 0a 2a 2a 20 20 20  ibed under.**   
11e9d 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50    journalMode==P
11e9e 45 52 53 49 53 54 20 69 73 20 75 73 65 64 20 69  ERSIST is used i
11e9f 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 66  nstead..**.** Af
11ea0 74 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ter the journal 
11ea1 69 73 20 66 69 6e 61 6c 69 7a 65 64 2c 20 69 66  is finalized, if
11ea2 20 72 75 6e 6e 69 6e 67 20 69 6e 20 6e 6f 6e 2d   running in non-
11ea3 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20  exclusive mode, 
11ea4 74 68 65 0a 2a 2a 20 70 61 67 65 72 20 6d 6f 76  the.** pager mov
11ea5 65 73 20 74 6f 20 50 41 47 45 52 5f 53 48 41 52  es to PAGER_SHAR
11ea6 45 44 20 73 74 61 74 65 20 28 61 6e 64 20 64 6f  ED state (and do
11ea7 77 6e 67 72 61 64 65 73 20 74 68 65 20 6c 6f 63  wngrades the loc
11ea8 6b 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 61 74 61  k on the.** data
11ea9 62 61 73 65 20 66 69 6c 65 20 61 63 63 6f 72 64  base file accord
11eaa 69 6e 67 6c 79 29 2e 0a 2a 2a 0a 2a 2a 20 49 66  ingly)..**.** If
11eab 20 74 68 65 20 70 61 67 65 72 20 69 73 20 72 75   the pager is ru
11eac 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 69  nning in exclusi
11ead 76 65 20 6d 6f 64 65 20 61 6e 64 20 69 73 20 69  ve mode and is i
11eae 6e 20 50 41 47 45 52 5f 53 59 4e 43 45 44 20 73  n PAGER_SYNCED s
11eaf 74 61 74 65 2c 0a 2a 2a 20 69 74 20 6d 6f 76 65  tate,.** it move
11eb0 73 20 74 6f 20 50 41 47 45 52 5f 45 58 43 4c 55  s to PAGER_EXCLU
11eb1 53 49 56 45 2e 20 4e 6f 20 6c 6f 63 6b 73 20 61  SIVE. No locks a
11eb2 72 65 20 64 6f 77 6e 67 72 61 64 65 64 20 77 68  re downgraded wh
11eb3 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e 0a 2a 2a  en running in.**
11eb4 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2e   exclusive mode.
11eb5 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b  .**.** SQLITE_OK
11eb6 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
11eb7 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e  no error occurs.
11eb8 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   If an error occ
11eb9 75 72 73 20 64 75 72 69 6e 67 0a 2a 2a 20 61 6e  urs during.** an
11eba 79 20 6f 66 20 74 68 65 20 49 4f 20 6f 70 65 72  y of the IO oper
11ebb 61 74 69 6f 6e 73 20 74 6f 20 66 69 6e 61 6c 69  ations to finali
11ebc 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  ze the journal f
11ebd 69 6c 65 20 6f 72 20 75 6e 6c 6f 63 6b 20 74 68  ile or unlock th
11ebe 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 74 68  e.** database th
11ebf 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20  en the IO error 
11ec0 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
11ec1 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20 49 66   to the user. If
11ec2 20 74 68 65 20 0a 2a 2a 20 6f 70 65 72 61 74 69   the .** operati
11ec3 6f 6e 20 74 6f 20 66 69 6e 61 6c 69 7a 65 20 74  on to finalize t
11ec4 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
11ec5 66 61 69 6c 73 2c 20 74 68 65 6e 20 74 68 65 20  fails, then the 
11ec6 63 6f 64 65 20 73 74 69 6c 6c 0a 2a 2a 20 74 72  code still.** tr
11ec7 69 65 73 20 74 6f 20 75 6e 6c 6f 63 6b 20 74 68  ies to unlock th
11ec8 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
11ec9 69 66 20 6e 6f 74 20 69 6e 20 65 78 63 6c 75 73  if not in exclus
11eca 69 76 65 20 6d 6f 64 65 2e 20 49 66 20 74 68 65  ive mode. If the
11ecb 0a 2a 2a 20 75 6e 6c 6f 63 6b 20 6f 70 65 72 61  .** unlock opera
11ecc 74 69 6f 6e 20 66 61 69 6c 73 20 61 73 20 77 65  tion fails as we
11ecd 6c 6c 2c 20 74 68 65 6e 20 74 68 65 20 66 69 72  ll, then the fir
11ece 73 74 20 65 72 72 6f 72 20 63 6f 64 65 20 72 65  st error code re
11ecf 6c 61 74 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20  lated.** to the 
11ed0 66 69 72 73 74 20 65 72 72 6f 72 20 65 6e 63 6f  first error enco
11ed1 75 6e 74 65 72 65 64 20 28 74 68 65 20 6a 6f 75  untered (the jou
11ed2 72 6e 61 6c 20 66 69 6e 61 6c 69 7a 61 74 69 6f  rnal finalizatio
11ed3 6e 20 6f 6e 65 29 20 69 73 0a 2a 2a 20 72 65 74  n one) is.** ret
11ed4 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  urned..*/.static
11ed5 20 69 6e 74 20 70 61 67 65 72 5f 65 6e 64 5f 74   int pager_end_t
11ed6 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 67 65 72  ransaction(Pager
11ed7 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 68 61   *pPager, int ha
11ed8 73 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e 74 20  sMaster){.  int 
11ed9 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  rc = SQLITE_OK; 
11eda 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 63 6f       /* Error co
11edb 64 65 20 66 72 6f 6d 20 6a 6f 75 72 6e 61 6c 20  de from journal 
11edc 66 69 6e 61 6c 69 7a 61 74 69 6f 6e 20 6f 70 65  finalization ope
11edd 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  ration */.  int 
11ede 72 63 32 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b  rc2 = SQLITE_OK;
11edf 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 63 6f       /* Error co
11ee0 64 65 20 66 72 6f 6d 20 64 62 20 66 69 6c 65 20  de from db file 
11ee1 75 6e 6c 6f 63 6b 20 6f 70 65 72 61 74 69 6f 6e  unlock operation
11ee2 20 2a 2f 0a 0a 20 20 69 66 28 20 70 50 61 67 65   */..  if( pPage
11ee3 72 2d 3e 73 74 61 74 65 3c 50 41 47 45 52 5f 52  r->state<PAGER_R
11ee4 45 53 45 52 56 45 44 20 29 7b 0a 20 20 20 20 72  ESERVED ){.    r
11ee5 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
11ee6 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 41 6c  .  }.  releaseAl
11ee7 6c 53 61 76 65 70 6f 69 6e 74 73 28 70 50 61 67  lSavepoints(pPag
11ee8 65 72 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  er);..  assert( 
11ee9 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a  isOpen(pPager->j
11eea 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 70  fd) || pPager->p
11eeb 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a  InJournal==0 );.
11eec 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61    if( isOpen(pPa
11eed 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 0a 20 20  ger->jfd) ){..  
11eee 20 20 2f 2a 20 46 69 6e 61 6c 69 7a 65 20 74 68    /* Finalize th
11eef 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20  e journal file. 
11ef0 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  */.    if( sqlit
11ef1 65 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 70  e3IsMemJournal(p
11ef2 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20  Pager->jfd) ){. 
11ef3 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61       assert( pPa
11ef4 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65  ger->journalMode
11ef5 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
11ef6 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 3b 0a 20 20  ODE_MEMORY );.  
11ef7 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f      sqlite3OsClo
11ef8 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b  se(pPager->jfd);
11ef9 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70  .    }else if( p
11efa 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f  Pager->journalMo
11efb 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de==PAGER_JOURNA
11efc 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 20 29  LMODE_TRUNCATE )
11efd 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67  {.      if( pPag
11efe 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d  er->journalOff==
11eff 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  0 ){.        rc 
11f00 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
11f01 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
11f02 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
11f03 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d  Truncate(pPager-
11f04 3e 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20 20 20  >jfd, 0);.      
11f05 7d 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e  }.      pPager->
11f06 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a  journalOff = 0;.
11f07 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f        pPager->jo
11f08 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20 30  urnalStarted = 0
11f09 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
11f0a 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76  pPager->exclusiv
11f0b 65 4d 6f 64 65 20 0a 20 20 20 20 20 7c 7c 20 70  eMode .     || p
11f0c 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f  Pager->journalMo
11f0d 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de==PAGER_JOURNA
11f0e 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a 20 20  LMODE_PERSIST.  
11f0f 20 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20    ){.      rc = 
11f10 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 64 72 28 70  zeroJournalHdr(p
11f11 50 61 67 65 72 2c 20 68 61 73 4d 61 73 74 65 72  Pager, hasMaster
11f12 29 3b 0a 20 20 20 20 20 20 70 61 67 65 72 5f 65  );.      pager_e
11f13 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29  rror(pPager, rc)
11f14 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e  ;.      pPager->
11f15 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a  journalOff = 0;.
11f16 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f        pPager->jo
11f17 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20 30  urnalStarted = 0
11f18 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
11f19 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e 63     /* This branc
11f1a 68 20 6d 61 79 20 62 65 20 65 78 65 63 75 74 65  h may be execute
11f1b 64 20 77 69 74 68 20 50 61 67 65 72 2e 6a 6f 75  d with Pager.jou
11f1c 72 6e 61 6c 4d 6f 64 65 3d 3d 4d 45 4d 4f 52 59  rnalMode==MEMORY
11f1d 20 69 66 0a 20 20 20 20 20 20 2a 2a 20 61 20 68   if.      ** a h
11f1e 6f 74 2d 6a 6f 75 72 6e 61 6c 20 77 61 73 20 6a  ot-journal was j
11f1f 75 73 74 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e  ust rolled back.
11f20 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68   In this case th
11f21 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 20  e journal.      
11f22 2a 2a 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62  ** file should b
11f23 65 20 63 6c 6f 73 65 64 20 61 6e 64 20 64 65 6c  e closed and del
11f24 65 74 65 64 2e 20 49 66 20 74 68 69 73 20 63 6f  eted. If this co
11f25 6e 6e 65 63 74 69 6f 6e 20 77 72 69 74 65 73 20  nnection writes 
11f26 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  to.      ** the 
11f27 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 69  database file, i
11f28 74 20 77 69 6c 6c 20 64 6f 20 73 6f 20 75 73 69  t will do so usi
11f29 6e 67 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20  ng an in-memory 
11f2a 6a 6f 75 72 6e 61 6c 2e 20 20 2a 2f 0a 20 20 20  journal.  */.   
11f2b 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
11f2c 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d  r->journalMode==
11f2d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
11f2e 45 5f 44 45 4c 45 54 45 20 0a 20 20 20 20 20 20  E_DELETE .      
11f2f 20 20 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e       || pPager->
11f30 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47  journalMode==PAG
11f31 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d  ER_JOURNALMODE_M
11f32 45 4d 4f 52 59 20 0a 20 20 20 20 20 20 29 3b 0a  EMORY .      );.
11f33 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43        sqlite3OsC
11f34 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64  lose(pPager->jfd
11f35 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 50  );.      if( !pP
11f36 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29  ager->tempFile )
11f37 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  {.        rc = s
11f38 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70  qlite3OsDelete(p
11f39 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 70 50 61  Pager->pVfs, pPa
11f3a 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 30  ger->zJournal, 0
11f3b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
11f3c 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
11f3d 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20  CHECK_PAGES.    
11f3e 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 74 65  sqlite3PcacheIte
11f3f 72 61 74 65 44 69 72 74 79 28 70 50 61 67 65 72  rateDirty(pPager
11f40 2d 3e 70 50 43 61 63 68 65 2c 20 70 61 67 65 72  ->pPCache, pager
11f41 5f 73 65 74 5f 70 61 67 65 68 61 73 68 29 3b 0a  _set_pagehash);.
11f42 23 65 6e 64 69 66 0a 0a 20 20 20 20 73 71 6c 69  #endif..    sqli
11f43 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e 41 6c  te3PcacheCleanAl
11f44 6c 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68  l(pPager->pPCach
11f45 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42  e);.    sqlite3B
11f46 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61  itvecDestroy(pPa
11f47 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29  ger->pInJournal)
11f48 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 49  ;.    pPager->pI
11f49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20  nJournal = 0;.  
11f4a 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d    pPager->nRec =
11f4b 20 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21   0;.  }..  if( !
11f4c 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76  pPager->exclusiv
11f4d 65 4d 6f 64 65 20 29 7b 0a 20 20 20 20 72 63 32  eMode ){.    rc2
11f4e 20 3d 20 6f 73 55 6e 6c 6f 63 6b 28 70 50 61 67   = osUnlock(pPag
11f4f 65 72 2d 3e 66 64 2c 20 53 48 41 52 45 44 5f 4c  er->fd, SHARED_L
11f50 4f 43 4b 29 3b 0a 20 20 20 20 70 50 61 67 65 72  OCK);.    pPager
11f51 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f  ->state = PAGER_
11f52 53 48 41 52 45 44 3b 0a 20 20 20 20 70 50 61 67  SHARED;.    pPag
11f53 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44  er->changeCountD
11f54 6f 6e 65 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  one = 0;.  }else
11f55 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61   if( pPager->sta
11f56 74 65 3d 3d 50 41 47 45 52 5f 53 59 4e 43 45 44  te==PAGER_SYNCED
11f57 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   ){.    pPager->
11f58 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 45 58  state = PAGER_EX
11f59 43 4c 55 53 49 56 45 3b 0a 20 20 7d 0a 20 20 70  CLUSIVE;.  }.  p
11f5a 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72  Pager->setMaster
11f5b 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e   = 0;.  pPager->
11f5c 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20  needSync = 0;.  
11f5d 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69  pPager->dbModifi
11f5e 65 64 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 4f  ed = 0;..  /* TO
11f5f 44 4f 3a 20 49 73 20 74 68 69 73 20 6f 70 74 69  DO: Is this opti
11f60 6d 61 6c 3f 20 57 68 79 20 69 73 20 74 68 65 20  mal? Why is the 
11f61 64 62 20 73 69 7a 65 20 69 6e 76 61 6c 69 64 61  db size invalida
11f62 74 65 64 20 68 65 72 65 20 0a 20 20 2a 2a 20 77  ted here .  ** w
11f63 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  hen the database
11f64 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 75 6e 6c   file is not unl
11f65 6f 63 6b 65 64 3f 20 2a 2f 0a 20 20 70 50 61 67  ocked? */.  pPag
11f66 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 3d  er->dbOrigSize =
11f67 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61   0;.  sqlite3Pca
11f68 63 68 65 54 72 75 6e 63 61 74 65 28 70 50 61 67  cheTruncate(pPag
11f69 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 50 61  er->pPCache, pPa
11f6a 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 20  ger->dbSize);.  
11f6b 69 66 28 20 21 4d 45 4d 44 42 20 29 7b 0a 20 20  if( !MEMDB ){.  
11f6c 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65    pPager->dbSize
11f6d 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 7d 0a 0a  Valid = 0;.  }..
11f6e 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d 53 51    return (rc==SQ
11f6f 4c 49 54 45 5f 4f 4b 3f 72 63 32 3a 72 63 29 3b  LITE_OK?rc2:rc);
11f70 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65  .}../*.** Parame
11f71 74 65 72 20 61 44 61 74 61 20 6d 75 73 74 20 70  ter aData must p
11f72 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 72  oint to a buffer
11f73 20 6f 66 20 70 50 61 67 65 72 2d 3e 70 61 67 65   of pPager->page
11f74 53 69 7a 65 20 62 79 74 65 73 0a 2a 2a 20 6f 66  Size bytes.** of
11f75 20 64 61 74 61 2e 20 43 6f 6d 70 75 74 65 20 61   data. Compute a
11f76 6e 64 20 72 65 74 75 72 6e 20 61 20 63 68 65 63  nd return a chec
11f77 6b 73 75 6d 20 62 61 73 65 64 20 6f 6e 74 20 74  ksum based ont t
11f78 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
11f79 68 65 20 0a 2a 2a 20 70 61 67 65 20 6f 66 20 64  he .** page of d
11f7a 61 74 61 20 61 6e 64 20 74 68 65 20 63 75 72 72  ata and the curr
11f7b 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 70 50 61  ent value of pPa
11f7c 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 2e 0a  ger->cksumInit..
11f7d 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 6e 6f  **.** This is no
11f7e 74 20 61 20 72 65 61 6c 20 63 68 65 63 6b 73 75  t a real checksu
11f7f 6d 2e 20 49 74 20 69 73 20 72 65 61 6c 6c 79 20  m. It is really 
11f80 6a 75 73 74 20 74 68 65 20 73 75 6d 20 6f 66 20  just the sum of 
11f81 74 68 65 20 0a 2a 2a 20 72 61 6e 64 6f 6d 20 69  the .** random i
11f82 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 28 70 50  nitial value (pP
11f83 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29  ager->cksumInit)
11f84 20 61 6e 64 20 65 76 65 72 79 20 32 30 30 74 68   and every 200th
11f85 20 62 79 74 65 0a 2a 2a 20 6f 66 20 74 68 65 20   byte.** of the 
11f86 70 61 67 65 20 64 61 74 61 2c 20 73 74 61 72 74  page data, start
11f87 69 6e 67 20 77 69 74 68 20 62 79 74 65 20 6f 66  ing with byte of
11f88 66 73 65 74 20 28 70 50 61 67 65 72 2d 3e 70 61  fset (pPager->pa
11f89 67 65 53 69 7a 65 25 32 30 30 29 2e 0a 2a 2a 20  geSize%200)..** 
11f8a 45 61 63 68 20 62 79 74 65 20 69 73 20 69 6e 74  Each byte is int
11f8b 65 72 70 72 65 74 65 64 20 61 73 20 61 6e 20 38  erpreted as an 8
11f8c 2d 62 69 74 20 75 6e 73 69 67 6e 65 64 20 69 6e  -bit unsigned in
11f8d 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 68 61  teger..**.** Cha
11f8e 6e 67 69 6e 67 20 74 68 65 20 66 6f 72 6d 75 6c  nging the formul
11f8f 61 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 75 74  a used to comput
11f90 65 20 74 68 69 73 20 63 68 65 63 6b 73 75 6d 20  e this checksum 
11f91 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 0a 2a 2a  results in an.**
11f92 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 6a 6f   incompatible jo
11f93 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 6d 61  urnal file forma
11f94 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6a 6f 75 72  t..**.** If jour
11f95 6e 61 6c 20 63 6f 72 72 75 70 74 69 6f 6e 20 6f  nal corruption o
11f96 63 63 75 72 73 20 64 75 65 20 74 6f 20 61 20 70  ccurs due to a p
11f97 6f 77 65 72 20 66 61 69 6c 75 72 65 2c 20 74 68  ower failure, th
11f98 65 20 6d 6f 73 74 20 6c 69 6b 65 6c 79 20 0a 2a  e most likely .*
11f99 2a 20 73 63 65 6e 61 72 69 6f 20 69 73 20 74 68  * scenario is th
11f9a 61 74 20 6f 6e 65 20 65 6e 64 20 6f 72 20 74 68  at one end or th
11f9b 65 20 6f 74 68 65 72 20 6f 66 20 74 68 65 20 72  e other of the r
11f9c 65 63 6f 72 64 20 77 69 6c 6c 20 62 65 20 63 68  ecord will be ch
11f9d 61 6e 67 65 64 2e 20 0a 2a 2a 20 49 74 20 69 73  anged. .** It is
11f9e 20 6d 75 63 68 20 6c 65 73 73 20 6c 69 6b 65 6c   much less likel
11f9f 79 20 74 68 61 74 20 74 68 65 20 74 77 6f 20 65  y that the two e
11fa0 6e 64 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  nds of the journ
11fa1 61 6c 20 72 65 63 6f 72 64 20 77 69 6c 6c 20 62  al record will b
11fa2 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 61 6e 64  e.** correct and
11fa3 20 74 68 65 20 6d 69 64 64 6c 65 20 62 65 20 63   the middle be c
11fa4 6f 72 72 75 70 74 2e 20 20 54 68 75 73 2c 20 74  orrupt.  Thus, t
11fa5 68 69 73 20 22 63 68 65 63 6b 73 75 6d 22 20 73  his "checksum" s
11fa6 63 68 65 6d 65 2c 0a 2a 2a 20 74 68 6f 75 67 68  cheme,.** though
11fa7 20 66 61 73 74 20 61 6e 64 20 73 69 6d 70 6c 65   fast and simple
11fa8 2c 20 63 61 74 63 68 65 73 20 74 68 65 20 6d 6f  , catches the mo
11fa9 73 74 6c 79 20 6c 69 6b 65 6c 79 20 6b 69 6e 64  stly likely kind
11faa 20 6f 66 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a   of corruption..
11fab 2a 2f 0a 73 74 61 74 69 63 20 75 33 32 20 70 61  */.static u32 pa
11fac 67 65 72 5f 63 6b 73 75 6d 28 50 61 67 65 72 20  ger_cksum(Pager 
11fad 2a 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 75  *pPager, const u
11fae 38 20 2a 61 44 61 74 61 29 7b 0a 20 20 75 33 32  8 *aData){.  u32
11faf 20 63 6b 73 75 6d 20 3d 20 70 50 61 67 65 72 2d   cksum = pPager-
11fb0 3e 63 6b 73 75 6d 49 6e 69 74 3b 20 20 20 20 20  >cksumInit;     
11fb1 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d 20      /* Checksum 
11fb2 76 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20  value to return 
11fb3 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 50 61  */.  int i = pPa
11fb4 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2d 32 30  ger->pageSize-20
11fb5 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  0;          /* L
11fb6 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
11fb7 20 77 68 69 6c 65 28 20 69 3e 30 20 29 7b 0a 20   while( i>0 ){. 
11fb8 20 20 20 63 6b 73 75 6d 20 2b 3d 20 61 44 61 74     cksum += aDat
11fb9 61 5b 69 5d 3b 0a 20 20 20 20 69 20 2d 3d 20 32  a[i];.    i -= 2
11fba 30 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  00;.  }.  return
11fbb 20 63 6b 73 75 6d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   cksum;.}../*.**
11fbc 20 52 65 61 64 20 61 20 73 69 6e 67 6c 65 20 70   Read a single p
11fbd 61 67 65 20 66 72 6f 6d 20 65 69 74 68 65 72 20  age from either 
11fbe 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
11fbf 20 28 69 66 20 69 73 4d 61 69 6e 4a 72 6e 6c 3d   (if isMainJrnl=
11fc0 3d 31 29 20 6f 72 0a 2a 2a 20 66 72 6f 6d 20 74  =1) or.** from t
11fc1 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 28  he sub-journal (
11fc2 69 66 20 69 73 4d 61 69 6e 4a 72 6e 6c 3d 3d 30  if isMainJrnl==0
11fc3 29 20 61 6e 64 20 70 6c 61 79 62 61 63 6b 20 74  ) and playback t
11fc4 68 61 74 20 70 61 67 65 2e 0a 2a 2a 20 54 68 65  hat page..** The
11fc5 20 70 61 67 65 20 62 65 67 69 6e 73 20 61 74 20   page begins at 
11fc6 6f 66 66 73 65 74 20 2a 70 4f 66 66 73 65 74 20  offset *pOffset 
11fc7 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 2e 20 54  into the file. T
11fc8 68 65 20 2a 70 4f 66 66 73 65 74 0a 2a 2a 20 76  he *pOffset.** v
11fc9 61 6c 75 65 20 69 73 20 69 6e 63 72 65 61 73 65  alue is increase
11fca 64 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f  d to the start o
11fcb 66 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20  f the next page 
11fcc 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a  in the journal..
11fcd 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 4d 61 69 6e  **.** The isMain
11fce 4a 72 6e 6c 20 66 6c 61 67 20 69 73 20 74 72 75  Jrnl flag is tru
11fcf 65 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65  e if this is the
11fd0 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a   main rollback j
11fd1 6f 75 72 6e 61 6c 20 61 6e 64 0a 2a 2a 20 66 61  ournal and.** fa
11fd2 6c 73 65 20 66 6f 72 20 74 68 65 20 73 74 61 74  lse for the stat
11fd3 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e 20 20  ement journal.  
11fd4 54 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63  The main rollbac
11fd5 6b 20 6a 6f 75 72 6e 61 6c 20 75 73 65 73 0a 2a  k journal uses.*
11fd6 2a 20 63 68 65 63 6b 73 75 6d 73 20 2d 20 74 68  * checksums - th
11fd7 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72  e statement jour
11fd8 6e 61 6c 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2a  nal does not..**
11fd9 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 20  .** If the page 
11fda 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61  number of the pa
11fdb 67 65 20 72 65 63 6f 72 64 20 72 65 61 64 20 66  ge record read f
11fdc 72 6f 6d 20 74 68 65 20 28 73 75 62 2d 29 6a 6f  rom the (sub-)jo
11fdd 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 69 73  urnal file.** is
11fde 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68   greater than th
11fdf 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20  e current value 
11fe0 6f 66 20 50 61 67 65 72 2e 64 62 53 69 7a 65 2c  of Pager.dbSize,
11fe1 20 74 68 65 6e 20 70 6c 61 79 62 61 63 6b 20 69   then playback i
11fe2 73 0a 2a 2a 20 73 6b 69 70 70 65 64 20 61 6e 64  s.** skipped and
11fe3 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
11fe4 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  turned..**.** If
11fe5 20 70 44 6f 6e 65 20 69 73 20 6e 6f 74 20 4e 55   pDone is not NU
11fe6 4c 4c 2c 20 74 68 65 6e 20 69 74 20 69 73 20 61  LL, then it is a
11fe7 20 72 65 63 6f 72 64 20 6f 66 20 70 61 67 65 73   record of pages
11fe8 20 74 68 61 74 20 68 61 76 65 20 61 6c 72 65 61   that have alrea
11fe9 64 79 0a 2a 2a 20 62 65 65 6e 20 70 6c 61 79 65  dy.** been playe
11fea 64 20 62 61 63 6b 2e 20 20 49 66 20 74 68 65 20  d back.  If the 
11feb 70 61 67 65 20 61 74 20 2a 70 4f 66 66 73 65 74  page at *pOffset
11fec 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65   has already bee
11fed 6e 20 70 6c 61 79 65 64 20 62 61 63 6b 0a 2a 2a  n played back.**
11fee 20 28 69 66 20 74 68 65 20 63 6f 72 72 65 73 70   (if the corresp
11fef 6f 6e 64 69 6e 67 20 70 44 6f 6e 65 20 62 69 74  onding pDone bit
11ff0 20 69 73 20 73 65 74 29 20 74 68 65 6e 20 73 6b   is set) then sk
11ff1 69 70 20 74 68 65 20 70 6c 61 79 62 61 63 6b 2e  ip the playback.
11ff2 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68  .** Make sure th
11ff3 65 20 70 44 6f 6e 65 20 62 69 74 20 63 6f 72 72  e pDone bit corr
11ff4 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65  esponding to the
11ff5 20 2a 70 4f 66 66 73 65 74 20 70 61 67 65 20 69   *pOffset page i
11ff6 73 20 73 65 74 0a 2a 2a 20 70 72 69 6f 72 20 74  s set.** prior t
11ff7 6f 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a  o returning..**.
11ff8 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 72  ** If the page r
11ff9 65 63 6f 72 64 20 69 73 20 73 75 63 63 65 73 73  ecord is success
11ffa 66 75 6c 6c 79 20 72 65 61 64 20 66 72 6f 6d 20  fully read from 
11ffb 74 68 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61  the (sub-)journa
11ffc 6c 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 70 6c  l file.** and pl
11ffd 61 79 65 64 20 62 61 63 6b 2c 20 74 68 65 6e 20  ayed back, then 
11ffe 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
11fff 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20  urned. If an IO 
12000 65 72 72 6f 72 20 6f 63 63 75 72 73 0a 2a 2a 20  error occurs.** 
12001 77 68 69 6c 65 20 72 65 61 64 69 6e 67 20 74 68  while reading th
12002 65 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 74 68  e record from th
12003 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61 6c 20  e (sub-)journal 
12004 66 69 6c 65 20 6f 72 20 77 68 69 6c 65 20 77 72  file or while wr
12005 69 74 69 6e 67 0a 2a 2a 20 74 6f 20 74 68 65 20  iting.** to the 
12006 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74  database file, t
12007 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f 72  hen the IO error
12008 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
12009 64 2e 20 49 66 20 64 61 74 61 0a 2a 2a 20 69 73  d. If data.** is
1200a 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 72 65   successfully re
1200b 61 64 20 66 72 6f 6d 20 74 68 65 20 28 73 75 62  ad from the (sub
1200c 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 62  -)journal file b
1200d 75 74 20 61 70 70 65 61 72 73 20 74 6f 20 62 65  ut appears to be
1200e 0a 2a 2a 20 63 6f 72 72 75 70 74 65 64 2c 20 53  .** corrupted, S
1200f 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 20 72 65  QLITE_DONE is re
12010 74 75 72 6e 65 64 2e 20 44 61 74 61 20 69 73 20  turned. Data is 
12011 63 6f 6e 73 69 64 65 72 65 64 20 63 6f 72 72 75  considered corru
12012 70 74 65 64 20 69 6e 0a 2a 2a 20 74 77 6f 20 63  pted in.** two c
12013 69 72 63 75 6d 73 74 61 6e 63 65 73 3a 0a 2a 2a  ircumstances:.**
12014 20 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65 20   .**   * If the 
12015 72 65 63 6f 72 64 20 70 61 67 65 2d 6e 75 6d 62  record page-numb
12016 65 72 20 69 73 20 69 6c 6c 65 67 61 6c 20 28 30  er is illegal (0
12017 20 6f 72 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e   or PAGER_MJ_PGN
12018 4f 29 2c 20 6f 72 0a 2a 2a 20 20 20 2a 20 49 66  O), or.**   * If
12019 20 74 68 65 20 72 65 63 6f 72 64 20 69 73 20 62   the record is b
1201a 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61 63 6b  eing rolled back
1201b 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 6a   from the main j
1201c 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 20  ournal file.**  
1201d 20 20 20 61 6e 64 20 74 68 65 20 63 68 65 63 6b     and the check
1201e 73 75 6d 20 66 69 65 6c 64 20 64 6f 65 73 20 6e  sum field does n
1201f 6f 74 20 6d 61 74 63 68 20 74 68 65 20 72 65 63  ot match the rec
12020 6f 72 64 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a  ord content..**.
12021 2a 2a 20 4e 65 69 74 68 65 72 20 6f 66 20 74 68  ** Neither of th
12022 65 73 65 20 74 77 6f 20 73 63 65 6e 61 72 69 6f  ese two scenario
12023 73 20 61 72 65 20 70 6f 73 73 69 62 6c 65 20 64  s are possible d
12024 75 72 69 6e 67 20 61 20 73 61 76 65 70 6f 69 6e  uring a savepoin
12025 74 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a  t rollback..**.*
12026 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 73  * If this is a s
12027 61 76 65 70 6f 69 6e 74 20 72 6f 6c 6c 62 61 63  avepoint rollbac
12028 6b 2c 20 74 68 65 6e 20 6d 65 6d 6f 72 79 20 6d  k, then memory m
12029 61 79 20 68 61 76 65 20 74 6f 20 62 65 20 64 79  ay have to be dy
1202a 6e 61 6d 69 63 61 6c 6c 79 0a 2a 2a 20 61 6c 6c  namically.** all
1202b 6f 63 61 74 65 64 20 62 79 20 74 68 69 73 20 66  ocated by this f
1202c 75 6e 63 74 69 6f 6e 2e 20 49 66 20 74 68 69 73  unction. If this
1202d 20 69 73 20 74 68 65 20 63 61 73 65 20 61 6e 64   is the case and
1202e 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66   an allocation f
1202f 61 69 6c 73 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f  ails,.** SQLITE_
12030 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65  NOMEM is returne
12031 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
12032 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f   pager_playback_
12033 6f 6e 65 5f 70 61 67 65 28 0a 20 20 50 61 67 65  one_page(.  Page
12034 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20  r *pPager,      
12035 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
12036 20 70 61 67 65 72 20 62 65 69 6e 67 20 70 6c 61   pager being pla
12037 79 65 64 20 62 61 63 6b 20 2a 2f 0a 20 20 69 6e  yed back */.  in
12038 74 20 69 73 4d 61 69 6e 4a 72 6e 6c 2c 20 20 20  t isMainJrnl,   
12039 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31              /* 1
1203a 20 2d 3e 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c   -> main journal
1203b 2e 20 30 20 2d 3e 20 73 75 62 2d 6a 6f 75 72 6e  . 0 -> sub-journ
1203c 61 6c 2e 20 2a 2f 0a 20 20 69 6e 74 20 69 73 55  al. */.  int isU
1203d 6e 73 79 6e 63 2c 20 20 20 20 20 20 20 20 20 20  nsync,          
1203e 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
1203f 66 20 72 65 61 64 69 6e 67 20 66 72 6f 6d 20 75  f reading from u
12040 6e 73 79 6e 63 65 64 20 6d 61 69 6e 20 6a 6f 75  nsynced main jou
12041 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 2a 70  rnal */.  i64 *p
12042 4f 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20  Offset,         
12043 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65          /* Offse
12044 74 20 6f 66 20 72 65 63 6f 72 64 20 74 6f 20 70  t of record to p
12045 6c 61 79 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74  layback */.  int
12046 20 69 73 53 61 76 65 70 6e 74 2c 20 20 20 20 20   isSavepnt,     
12047 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
12048 75 65 20 66 6f 72 20 61 20 73 61 76 65 70 6f 69  ue for a savepoi
12049 6e 74 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 20  nt rollback */. 
1204a 20 42 69 74 76 65 63 20 2a 70 44 6f 6e 65 20 20   Bitvec *pDone  
1204b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1204c 2a 20 42 69 74 76 65 63 20 6f 66 20 70 61 67 65  * Bitvec of page
1204d 73 20 61 6c 72 65 61 64 79 20 70 6c 61 79 65 64  s already played
1204e 20 62 61 63 6b 20 2a 2f 0a 29 7b 0a 20 20 69 6e   back */.){.  in
1204f 74 20 72 63 3b 0a 20 20 50 67 48 64 72 20 2a 70  t rc;.  PgHdr *p
12050 50 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Pg;             
12051 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 78 69 73        /* An exis
12052 74 69 6e 67 20 70 61 67 65 20 69 6e 20 74 68 65  ting page in the
12053 20 63 61 63 68 65 20 2a 2f 0a 20 20 50 67 6e 6f   cache */.  Pgno
12054 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20   pgno;          
12055 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
12056 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20   page number of 
12057 61 20 70 61 67 65 20 69 6e 20 6a 6f 75 72 6e 61  a page in journa
12058 6c 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d  l */.  u32 cksum
12059 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1205a 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d       /* Checksum
1205b 20 75 73 65 64 20 66 6f 72 20 73 61 6e 69 74 79   used for sanity
1205c 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 75   checking */.  u
1205d 38 20 2a 61 44 61 74 61 3b 20 20 20 20 20 20 20  8 *aData;       
1205e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1205f 54 65 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 67  Temporary storag
12060 65 20 66 6f 72 20 74 68 65 20 70 61 67 65 20 2a  e for the page *
12061 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  /.  sqlite3_file
12062 20 2a 6a 66 64 3b 20 20 20 20 20 20 20 20 20 20   *jfd;          
12063 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65    /* The file de
12064 73 63 72 69 70 74 6f 72 20 66 6f 72 20 74 68 65  scriptor for the
12065 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f   journal file */
12066 0a 0a 20 20 61 73 73 65 72 74 28 20 28 69 73 4d  ..  assert( (isM
12067 61 69 6e 4a 72 6e 6c 26 7e 31 29 3d 3d 30 20 29  ainJrnl&~1)==0 )
12068 3b 20 20 20 20 20 20 2f 2a 20 69 73 4d 61 69 6e  ;      /* isMain
12069 4a 72 6e 6c 20 69 73 20 30 20 6f 72 20 31 20 2a  Jrnl is 0 or 1 *
1206a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 69 73 53  /.  assert( (isS
1206b 61 76 65 70 6e 74 26 7e 31 29 3d 3d 30 20 29 3b  avepnt&~1)==0 );
1206c 20 20 20 20 20 20 20 2f 2a 20 69 73 53 61 76 65         /* isSave
1206d 70 6e 74 20 69 73 20 30 20 6f 72 20 31 20 2a 2f  pnt is 0 or 1 */
1206e 0a 20 20 61 73 73 65 72 74 28 20 69 73 4d 61 69  .  assert( isMai
1206f 6e 4a 72 6e 6c 20 7c 7c 20 70 44 6f 6e 65 20 29  nJrnl || pDone )
12070 3b 20 20 20 20 20 2f 2a 20 70 44 6f 6e 65 20 61  ;     /* pDone a
12071 6c 77 61 79 73 20 75 73 65 64 20 6f 6e 20 73 75  lways used on su
12072 62 2d 6a 6f 75 72 6e 61 6c 73 20 2a 2f 0a 20 20  b-journals */.  
12073 61 73 73 65 72 74 28 20 69 73 53 61 76 65 70 6e  assert( isSavepn
12074 74 20 7c 7c 20 70 44 6f 6e 65 3d 3d 30 20 29 3b  t || pDone==0 );
12075 20 20 20 2f 2a 20 70 44 6f 6e 65 20 6e 65 76 65     /* pDone neve
12076 72 20 75 73 65 64 20 6f 6e 20 6e 6f 6e 2d 73 61  r used on non-sa
12077 76 65 70 6f 69 6e 74 20 2a 2f 0a 0a 20 20 61 44  vepoint */..  aD
12078 61 74 61 20 3d 20 28 75 38 2a 29 70 50 61 67 65  ata = (u8*)pPage
12079 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20  r->pTmpSpace;.  
1207a 61 73 73 65 72 74 28 20 61 44 61 74 61 20 29 3b  assert( aData );
1207b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70           /* Temp
1207c 20 73 74 6f 72 61 67 65 20 6d 75 73 74 20 68 61   storage must ha
1207d 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  ve already been 
1207e 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 0a 20 20  allocated */..  
1207f 2f 2a 20 52 65 61 64 20 74 68 65 20 70 61 67 65  /* Read the page
12080 20 6e 75 6d 62 65 72 20 61 6e 64 20 70 61 67 65   number and page
12081 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 6a   data from the j
12082 6f 75 72 6e 61 6c 20 6f 72 20 73 75 62 2d 6a 6f  ournal or sub-jo
12083 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 2e  urnal.  ** file.
12084 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72   Return an error
12085 20 63 6f 64 65 20 74 6f 20 74 68 65 20 63 61 6c   code to the cal
12086 6c 65 72 20 69 66 20 61 6e 20 49 4f 20 65 72 72  ler if an IO err
12087 6f 72 20 6f 63 63 75 72 73 2e 0a 20 20 2a 2f 0a  or occurs..  */.
12088 20 20 6a 66 64 20 3d 20 69 73 4d 61 69 6e 4a 72    jfd = isMainJr
12089 6e 6c 20 3f 20 70 50 61 67 65 72 2d 3e 6a 66 64  nl ? pPager->jfd
1208a 20 3a 20 70 50 61 67 65 72 2d 3e 73 6a 66 64 3b   : pPager->sjfd;
1208b 0a 20 20 72 63 20 3d 20 72 65 61 64 33 32 62 69  .  rc = read32bi
1208c 74 73 28 6a 66 64 2c 20 2a 70 4f 66 66 73 65 74  ts(jfd, *pOffset
1208d 2c 20 26 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20  , &pgno);.  if( 
1208e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
1208f 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20  return rc;.  rc 
12090 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28  = sqlite3OsRead(
12091 6a 66 64 2c 20 61 44 61 74 61 2c 20 70 50 61 67  jfd, aData, pPag
12092 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 28 2a  er->pageSize, (*
12093 70 4f 66 66 73 65 74 29 2b 34 29 3b 0a 20 20 69  pOffset)+4);.  i
12094 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
12095 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
12096 2a 70 4f 66 66 73 65 74 20 2b 3d 20 70 50 61 67  *pOffset += pPag
12097 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 2b 20 34  er->pageSize + 4
12098 20 2b 20 69 73 4d 61 69 6e 4a 72 6e 6c 2a 34 3b   + isMainJrnl*4;
12099 0a 0a 20 20 2f 2a 20 53 61 6e 69 74 79 20 63 68  ..  /* Sanity ch
1209a 65 63 6b 69 6e 67 20 6f 6e 20 74 68 65 20 70 61  ecking on the pa
1209b 67 65 2e 20 20 54 68 69 73 20 69 73 20 6d 6f 72  ge.  This is mor
1209c 65 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74  e important that
1209d 20 49 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20   I originally.  
1209e 2a 2a 20 74 68 6f 75 67 68 74 2e 20 20 49 66 20  ** thought.  If 
1209f 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20  a power failure 
120a0 6f 63 63 75 72 73 20 77 68 69 6c 65 20 74 68 65  occurs while the
120a1 20 6a 6f 75 72 6e 61 6c 20 69 73 20 62 65 69 6e   journal is bein
120a2 67 20 77 72 69 74 74 65 6e 2c 0a 20 20 2a 2a 20  g written,.  ** 
120a3 69 74 20 63 6f 75 6c 64 20 63 61 75 73 65 20 69  it could cause i
120a4 6e 76 61 6c 69 64 20 64 61 74 61 20 74 6f 20 62  nvalid data to b
120a5 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74  e written into t
120a6 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 57 65 20  he journal.  We 
120a7 6e 65 65 64 20 74 6f 0a 20 20 2a 2a 20 64 65 74  need to.  ** det
120a8 65 63 74 20 74 68 69 73 20 69 6e 76 61 6c 69 64  ect this invalid
120a9 20 64 61 74 61 20 28 77 69 74 68 20 68 69 67 68   data (with high
120aa 20 70 72 6f 62 61 62 69 6c 69 74 79 29 20 61 6e   probability) an
120ab 64 20 69 67 6e 6f 72 65 20 69 74 2e 0a 20 20 2a  d ignore it..  *
120ac 2f 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20  /.  if( pgno==0 
120ad 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47 45 52 5f 4d  || pgno==PAGER_M
120ae 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 29  J_PGNO(pPager) )
120af 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 69  {.    assert( !i
120b0 73 53 61 76 65 70 6e 74 20 29 3b 0a 20 20 20 20  sSavepnt );.    
120b1 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f  return SQLITE_DO
120b2 4e 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 67  NE;.  }.  if( pg
120b3 6e 6f 3e 28 50 67 6e 6f 29 70 50 61 67 65 72 2d  no>(Pgno)pPager-
120b4 3e 64 62 53 69 7a 65 20 7c 7c 20 73 71 6c 69 74  >dbSize || sqlit
120b5 65 33 42 69 74 76 65 63 54 65 73 74 28 70 44 6f  e3BitvecTest(pDo
120b6 6e 65 2c 20 70 67 6e 6f 29 20 29 7b 0a 20 20 20  ne, pgno) ){.   
120b7 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
120b8 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 4d  K;.  }.  if( isM
120b9 61 69 6e 4a 72 6e 6c 20 29 7b 0a 20 20 20 20 72  ainJrnl ){.    r
120ba 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 6a  c = read32bits(j
120bb 66 64 2c 20 28 2a 70 4f 66 66 73 65 74 29 2d 34  fd, (*pOffset)-4
120bc 2c 20 26 63 6b 73 75 6d 29 3b 0a 20 20 20 20 69  , &cksum);.    i
120bd 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
120be 63 3b 0a 20 20 20 20 69 66 28 20 21 69 73 53 61  c;.    if( !isSa
120bf 76 65 70 6e 74 20 26 26 20 70 61 67 65 72 5f 63  vepnt && pager_c
120c0 6b 73 75 6d 28 70 50 61 67 65 72 2c 20 61 44 61  ksum(pPager, aDa
120c1 74 61 29 21 3d 63 6b 73 75 6d 20 29 7b 0a 20 20  ta)!=cksum ){.  
120c2 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
120c3 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20  E_DONE;.    }.  
120c4 7d 0a 0a 20 20 69 66 28 20 70 44 6f 6e 65 20 26  }..  if( pDone &
120c5 26 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 42  & (rc = sqlite3B
120c6 69 74 76 65 63 53 65 74 28 70 44 6f 6e 65 2c 20  itvecSet(pDone, 
120c7 70 67 6e 6f 29 29 21 3d 53 51 4c 49 54 45 5f 4f  pgno))!=SQLITE_O
120c8 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  K ){.    return 
120c9 72 63 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72  rc;.  }..  asser
120ca 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  t( pPager->state
120cb 3d 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 44  ==PAGER_RESERVED
120cc 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 74 61 74   || pPager->stat
120cd 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49  e>=PAGER_EXCLUSI
120ce 56 45 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  VE );..  /* If t
120cf 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 52  he pager is in R
120d0 45 53 45 52 56 45 44 20 73 74 61 74 65 2c 20 74  ESERVED state, t
120d1 68 65 6e 20 74 68 65 72 65 20 6d 75 73 74 20 62  hen there must b
120d2 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 69 73  e a copy of this
120d3 0a 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68  .  ** page in th
120d4 65 20 70 61 67 65 72 20 63 61 63 68 65 2e 20 49  e pager cache. I
120d5 6e 20 74 68 69 73 20 63 61 73 65 20 6a 75 73 74  n this case just
120d6 20 75 70 64 61 74 65 20 74 68 65 20 70 61 67 65   update the page
120d7 72 20 63 61 63 68 65 2c 0a 20 20 2a 2a 20 6e 6f  r cache,.  ** no
120d8 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  t the database f
120d9 69 6c 65 2e 20 54 68 65 20 70 61 67 65 20 69 73  ile. The page is
120da 20 6c 65 66 74 20 6d 61 72 6b 65 64 20 64 69 72   left marked dir
120db 74 79 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e  ty in this case.
120dc 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 65 78  .  **.  ** An ex
120dd 63 65 70 74 69 6f 6e 20 74 6f 20 74 68 65 20 61  ception to the a
120de 62 6f 76 65 20 72 75 6c 65 3a 20 49 66 20 74 68  bove rule: If th
120df 65 20 64 61 74 61 62 61 73 65 20 69 73 20 69 6e  e database is in
120e0 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 0a 20 20   no-sync mode.  
120e1 2a 2a 20 61 6e 64 20 61 20 70 61 67 65 20 69 73  ** and a page is
120e2 20 6d 6f 76 65 64 20 64 75 72 69 6e 67 20 61 6e   moved during an
120e3 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63   incremental vac
120e4 75 75 6d 20 74 68 65 6e 20 74 68 65 20 70 61 67  uum then the pag
120e5 65 20 6d 61 79 0a 20 20 2a 2a 20 6e 6f 74 20 62  e may.  ** not b
120e6 65 20 69 6e 20 74 68 65 20 70 61 67 65 72 20 63  e in the pager c
120e7 61 63 68 65 2e 20 4c 61 74 65 72 3a 20 69 66 20  ache. Later: if 
120e8 61 20 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 49 4f  a malloc() or IO
120e9 20 65 72 72 6f 72 20 6f 63 63 75 72 73 0a 20 20   error occurs.  
120ea 2a 2a 20 64 75 72 69 6e 67 20 61 20 4d 6f 76 65  ** during a Move
120eb 70 61 67 65 28 29 20 63 61 6c 6c 2c 20 74 68 65  page() call, the
120ec 6e 20 74 68 65 20 70 61 67 65 20 6d 61 79 20 6e  n the page may n
120ed 6f 74 20 62 65 20 69 6e 20 74 68 65 20 63 61 63  ot be in the cac
120ee 68 65 0a 20 20 2a 2a 20 65 69 74 68 65 72 2e 20  he.  ** either. 
120ef 53 6f 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e  So the condition
120f0 20 64 65 73 63 72 69 62 65 64 20 69 6e 20 74 68   described in th
120f1 65 20 61 62 6f 76 65 20 70 61 72 61 67 72 61 70  e above paragrap
120f2 68 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 61 73  h is not.  ** as
120f3 73 65 72 74 28 29 61 62 6c 65 2e 0a 20 20 2a 2a  sert()able..  **
120f4 0a 20 20 2a 2a 20 49 66 20 69 6e 20 45 58 43 4c  .  ** If in EXCL
120f5 55 53 49 56 45 20 73 74 61 74 65 2c 20 74 68 65  USIVE state, the
120f6 6e 20 77 65 20 75 70 64 61 74 65 20 74 68 65 20  n we update the 
120f7 70 61 67 65 72 20 63 61 63 68 65 20 69 66 20 69  pager cache if i
120f8 74 20 65 78 69 73 74 73 0a 20 20 2a 2a 20 61 6e  t exists.  ** an
120f9 64 20 74 68 65 20 6d 61 69 6e 20 66 69 6c 65 2e  d the main file.
120fa 20 54 68 65 20 70 61 67 65 20 69 73 20 74 68 65   The page is the
120fb 6e 20 6d 61 72 6b 65 64 20 6e 6f 74 20 64 69 72  n marked not dir
120fc 74 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 69  ty..  **.  ** Ti
120fd 63 6b 65 74 20 23 31 31 37 31 3a 20 20 54 68 65  cket #1171:  The
120fe 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e   statement journ
120ff 61 6c 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e  al might contain
12100 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 74 68   page content th
12101 61 74 20 69 73 0a 20 20 2a 2a 20 64 69 66 66 65  at is.  ** diffe
12102 72 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 70 61  rent from the pa
12103 67 65 20 63 6f 6e 74 65 6e 74 20 61 74 20 74 68  ge content at th
12104 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 74  e start of the t
12105 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2a  ransaction..  **
12106 20 54 68 69 73 20 6f 63 63 75 72 73 20 77 68 65   This occurs whe
12107 6e 20 61 20 70 61 67 65 20 69 73 20 63 68 61 6e  n a page is chan
12108 67 65 64 20 70 72 69 6f 72 20 74 6f 20 74 68 65  ged prior to the
12109 20 73 74 61 72 74 20 6f 66 20 61 20 73 74 61 74   start of a stat
1210a 65 6d 65 6e 74 0a 20 20 2a 2a 20 74 68 65 6e 20  ement.  ** then 
1210b 63 68 61 6e 67 65 64 20 61 67 61 69 6e 20 77 69  changed again wi
1210c 74 68 69 6e 20 74 68 65 20 73 74 61 74 65 6d 65  thin the stateme
1210d 6e 74 2e 20 20 57 68 65 6e 20 72 6f 6c 6c 69 6e  nt.  When rollin
1210e 67 20 62 61 63 6b 20 73 75 63 68 20 61 0a 20 20  g back such a.  
1210f 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 77 65 20  ** statement we 
12110 6d 75 73 74 20 6e 6f 74 20 77 72 69 74 65 20 74  must not write t
12111 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64  o the original d
12112 61 74 61 62 61 73 65 20 75 6e 6c 65 73 73 20 77  atabase unless w
12113 65 20 6b 6e 6f 77 0a 20 20 2a 2a 20 66 6f 72 20  e know.  ** for 
12114 63 65 72 74 61 69 6e 20 74 68 61 74 20 6f 72 69  certain that ori
12115 67 69 6e 61 6c 20 70 61 67 65 20 63 6f 6e 74 65  ginal page conte
12116 6e 74 73 20 61 72 65 20 73 79 6e 63 65 64 20 69  nts are synced i
12117 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20 72 6f 6c  nto the main rol
12118 6c 62 61 63 6b 0a 20 20 2a 2a 20 6a 6f 75 72 6e  lback.  ** journ
12119 61 6c 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20  al.  Otherwise, 
1211a 61 20 70 6f 77 65 72 20 6c 6f 73 73 20 6d 69 67  a power loss mig
1211b 68 74 20 6c 65 61 76 65 20 6d 6f 64 69 66 69 65  ht leave modifie
1211c 64 20 64 61 74 61 20 69 6e 20 74 68 65 0a 20 20  d data in the.  
1211d 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ** database file
1211e 20 77 69 74 68 6f 75 74 20 61 6e 20 65 6e 74 72   without an entr
1211f 79 20 69 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63  y in the rollbac
12120 6b 20 6a 6f 75 72 6e 61 6c 20 74 68 61 74 20 63  k journal that c
12121 61 6e 0a 20 20 2a 2a 20 72 65 73 74 6f 72 65 20  an.  ** restore 
12122 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20  the database to 
12123 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 66 6f 72  its original for
12124 6d 2e 20 20 54 77 6f 20 63 6f 6e 64 69 74 69 6f  m.  Two conditio
12125 6e 73 20 6d 75 73 74 20 62 65 0a 20 20 2a 2a 20  ns must be.  ** 
12126 6d 65 74 20 62 65 66 6f 72 65 20 77 72 69 74 69  met before writi
12127 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ng to the databa
12128 73 65 20 66 69 6c 65 73 2e 20 28 31 29 20 74 68  se files. (1) th
12129 65 20 64 61 74 61 62 61 73 65 20 6d 75 73 74 20  e database must 
1212a 62 65 0a 20 20 2a 2a 20 6c 6f 63 6b 65 64 2e 20  be.  ** locked. 
1212b 20 28 32 29 20 77 65 20 6b 6e 6f 77 20 74 68 61   (2) we know tha
1212c 74 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70  t the original p
1212d 61 67 65 20 63 6f 6e 74 65 6e 74 20 69 73 20 66  age content is f
1212e 75 6c 6c 79 20 73 79 6e 63 65 64 0a 20 20 2a 2a  ully synced.  **
1212f 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75   in the main jou
12130 72 6e 61 6c 20 65 69 74 68 65 72 20 62 65 63 61  rnal either beca
12131 75 73 65 20 74 68 65 20 70 61 67 65 20 69 73 20  use the page is 
12132 6e 6f 74 20 69 6e 20 63 61 63 68 65 20 6f 72 20  not in cache or 
12133 65 6c 73 65 0a 20 20 2a 2a 20 74 68 65 20 70 61  else.  ** the pa
12134 67 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20  ge is marked as 
12135 6e 65 65 64 53 79 6e 63 3d 3d 30 2e 0a 20 20 2a  needSync==0..  *
12136 2a 0a 20 20 2a 2a 20 32 30 30 38 2d 30 34 2d 31  *.  ** 2008-04-1
12137 34 3a 20 20 57 68 65 6e 20 61 74 74 65 6d 70 74  4:  When attempt
12138 69 6e 67 20 74 6f 20 76 61 63 75 75 6d 20 61 20  ing to vacuum a 
12139 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65  corrupt database
1213a 20 66 69 6c 65 2c 20 69 74 0a 20 20 2a 2a 20 69   file, it.  ** i
1213b 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 66 61  s possible to fa
1213c 69 6c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6f  il a statement o
1213d 6e 20 61 20 64 61 74 61 62 61 73 65 20 74 68 61  n a database tha
1213e 74 20 64 6f 65 73 20 6e 6f 74 20 79 65 74 20 65  t does not yet e
1213f 78 69 73 74 2e 0a 20 20 2a 2a 20 44 6f 20 6e 6f  xist..  ** Do no
12140 74 20 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69  t attempt to wri
12141 74 65 20 69 66 20 64 61 74 61 62 61 73 65 20 66  te if database f
12142 69 6c 65 20 68 61 73 20 6e 65 76 65 72 20 62 65  ile has never be
12143 65 6e 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a  en opened..  */.
12144 20 20 70 50 67 20 3d 20 70 61 67 65 72 5f 6c 6f    pPg = pager_lo
12145 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70 67 6e  okup(pPager, pgn
12146 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50  o);.  assert( pP
12147 67 20 7c 7c 20 21 4d 45 4d 44 42 20 29 3b 0a 20  g || !MEMDB );. 
12148 20 50 41 47 45 52 54 52 41 43 45 28 28 22 50 4c   PAGERTRACE(("PL
12149 41 59 42 41 43 4b 20 25 64 20 70 61 67 65 20 25  AYBACK %d page %
1214a 64 20 68 61 73 68 28 25 30 38 78 29 20 25 73 5c  d hash(%08x) %s\
1214b 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
1214c 20 20 20 50 41 47 45 52 49 44 28 70 50 61 67 65     PAGERID(pPage
1214d 72 29 2c 20 70 67 6e 6f 2c 20 70 61 67 65 72 5f  r), pgno, pager_
1214e 64 61 74 61 68 61 73 68 28 70 50 61 67 65 72 2d  datahash(pPager-
1214f 3e 70 61 67 65 53 69 7a 65 2c 20 61 44 61 74 61  >pageSize, aData
12150 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ),.             
12151 20 20 28 69 73 4d 61 69 6e 4a 72 6e 6c 3f 22 6d    (isMainJrnl?"m
12152 61 69 6e 2d 6a 6f 75 72 6e 61 6c 22 3a 22 73 75  ain-journal":"su
12153 62 2d 6a 6f 75 72 6e 61 6c 22 29 0a 20 20 29 29  b-journal").  ))
12154 3b 0a 20 20 69 66 28 20 28 70 50 61 67 65 72 2d  ;.  if( (pPager-
12155 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58  >state>=PAGER_EX
12156 43 4c 55 53 49 56 45 29 0a 20 20 20 26 26 20 28  CLUSIVE).   && (
12157 70 50 67 3d 3d 30 20 7c 7c 20 30 3d 3d 28 70 50  pPg==0 || 0==(pP
12158 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e  g->flags&PGHDR_N
12159 45 45 44 5f 53 59 4e 43 29 29 0a 20 20 20 26 26  EED_SYNC)).   &&
1215a 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e   isOpen(pPager->
1215b 66 64 29 0a 20 20 20 26 26 20 21 69 73 55 6e 73  fd).   && !isUns
1215c 79 6e 63 0a 20 20 29 7b 0a 20 20 20 20 69 36 34  ync.  ){.    i64
1215d 20 6f 66 73 74 20 3d 20 28 70 67 6e 6f 2d 31 29   ofst = (pgno-1)
1215e 2a 28 69 36 34 29 70 50 61 67 65 72 2d 3e 70 61  *(i64)pPager->pa
1215f 67 65 53 69 7a 65 3b 0a 20 20 20 20 72 63 20 3d  geSize;.    rc =
12160 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28   sqlite3OsWrite(
12161 70 50 61 67 65 72 2d 3e 66 64 2c 20 61 44 61 74  pPager->fd, aDat
12162 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53  a, pPager->pageS
12163 69 7a 65 2c 20 6f 66 73 74 29 3b 0a 20 20 20 20  ize, ofst);.    
12164 69 66 28 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d  if( pgno>pPager-
12165 3e 64 62 46 69 6c 65 53 69 7a 65 20 29 7b 0a 20  >dbFileSize ){. 
12166 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 46       pPager->dbF
12167 69 6c 65 53 69 7a 65 20 3d 20 70 67 6e 6f 3b 0a  ileSize = pgno;.
12168 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 50      }.    if( pP
12169 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 20 29 7b  ager->pBackup ){
1216a 0a 20 20 20 20 20 20 43 4f 44 45 43 31 28 70 50  .      CODEC1(pP
1216b 61 67 65 72 2c 20 61 44 61 74 61 2c 20 70 67 6e  ager, aData, pgn
1216c 6f 2c 20 33 2c 20 72 63 3d 53 51 4c 49 54 45 5f  o, 3, rc=SQLITE_
1216d 4e 4f 4d 45 4d 29 3b 0a 20 20 20 20 20 20 73 71  NOMEM);.      sq
1216e 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64 61 74  lite3BackupUpdat
1216f 65 28 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75  e(pPager->pBacku
12170 70 2c 20 70 67 6e 6f 2c 20 61 44 61 74 61 29 3b  p, pgno, aData);
12171 0a 20 20 20 20 20 20 43 4f 44 45 43 31 28 70 50  .      CODEC1(pP
12172 61 67 65 72 2c 20 61 44 61 74 61 2c 20 70 67 6e  ager, aData, pgn
12173 6f 2c 20 30 2c 20 72 63 3d 53 51 4c 49 54 45 5f  o, 0, rc=SQLITE_
12174 4e 4f 4d 45 4d 29 3b 0a 20 20 20 20 7d 0a 20 20  NOMEM);.    }.  
12175 7d 65 6c 73 65 20 69 66 28 20 21 69 73 4d 61 69  }else if( !isMai
12176 6e 4a 72 6e 6c 20 26 26 20 70 50 67 3d 3d 30 20  nJrnl && pPg==0 
12177 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69  ){.    /* If thi
12178 73 20 69 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20  s is a rollback 
12179 6f 66 20 61 20 73 61 76 65 70 6f 69 6e 74 20 61  of a savepoint a
1217a 6e 64 20 64 61 74 61 20 77 61 73 20 6e 6f 74 20  nd data was not 
1217b 77 72 69 74 74 65 6e 20 74 6f 0a 20 20 20 20 2a  written to.    *
1217c 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61  * the database a
1217d 6e 64 20 74 68 65 20 70 61 67 65 20 69 73 20 6e  nd the page is n
1217e 6f 74 20 69 6e 2d 6d 65 6d 6f 72 79 2c 20 74 68  ot in-memory, th
1217f 65 72 65 20 69 73 20 61 20 70 6f 74 65 6e 74 69  ere is a potenti
12180 61 6c 0a 20 20 20 20 2a 2a 20 70 72 6f 62 6c 65  al.    ** proble
12181 6d 2e 20 57 68 65 6e 20 74 68 65 20 70 61 67 65  m. When the page
12182 20 69 73 20 6e 65 78 74 20 66 65 74 63 68 65 64   is next fetched
12183 20 62 79 20 74 68 65 20 62 2d 74 72 65 65 20 6c   by the b-tree l
12184 61 79 65 72 2c 20 69 74 20 0a 20 20 20 20 2a 2a  ayer, it .    **
12185 20 77 69 6c 6c 20 62 65 20 72 65 61 64 20 66 72   will be read fr
12186 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20  om the database 
12187 66 69 6c 65 2c 20 77 68 69 63 68 20 6d 61 79 20  file, which may 
12188 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 0a 20  or may not be . 
12189 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 2e 20 0a     ** current. .
1218a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68      **.    ** Th
1218b 65 72 65 20 61 72 65 20 61 20 63 6f 75 70 6c 65  ere are a couple
1218c 20 6f 66 20 64 69 66 66 65 72 65 6e 74 20 77 61   of different wa
1218d 79 73 20 74 68 69 73 20 63 61 6e 20 68 61 70 70  ys this can happ
1218e 65 6e 2e 20 41 6c 6c 20 61 72 65 20 71 75 69 74  en. All are quit
1218f 65 0a 20 20 20 20 2a 2a 20 6f 62 73 63 75 72 65  e.    ** obscure
12190 2e 20 57 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69  . When running i
12191 6e 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 6d 6f  n synchronous mo
12192 64 65 2c 20 74 68 69 73 20 63 61 6e 20 6f 6e 6c  de, this can onl
12193 79 20 68 61 70 70 65 6e 20 0a 20 20 20 20 2a 2a  y happen .    **
12194 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20   if the page is 
12195 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74  on the free-list
12196 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66   at the start of
12197 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
12198 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 70 6f  , then.    ** po
12199 70 75 6c 61 74 65 64 2c 20 74 68 65 6e 20 6d 6f  pulated, then mo
1219a 76 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65  ved using sqlite
1219b 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65 28 29  3PagerMovepage()
1219c 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
1219d 54 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20  The solution is 
1219e 74 6f 20 61 64 64 20 61 6e 20 69 6e 2d 6d 65 6d  to add an in-mem
1219f 6f 72 79 20 70 61 67 65 20 74 6f 20 74 68 65 20  ory page to the 
121a0 63 61 63 68 65 20 63 6f 6e 74 61 69 6e 69 6e 67  cache containing
121a1 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61  .    ** the data
121a2 20 6a 75 73 74 20 72 65 61 64 20 66 72 6f 6d 20   just read from 
121a3 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e  the sub-journal.
121a4 20 4d 61 72 6b 20 74 68 65 20 70 61 67 65 20 61   Mark the page a
121a5 73 20 64 69 72 74 79 20 0a 20 20 20 20 2a 2a 20  s dirty .    ** 
121a6 61 6e 64 20 69 66 20 74 68 65 20 70 61 67 65 72  and if the pager
121a7 20 72 65 71 75 69 72 65 73 20 61 20 6a 6f 75 72   requires a jour
121a8 6e 61 6c 2d 73 79 6e 63 2c 20 74 68 65 6e 20 6d  nal-sync, then m
121a9 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 73 20  ark the page as 
121aa 0a 20 20 20 20 2a 2a 20 72 65 71 75 69 72 69 6e  .    ** requirin
121ab 67 20 61 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e 63  g a journal-sync
121ac 20 62 65 66 6f 72 65 20 69 74 20 69 73 20 77 72   before it is wr
121ad 69 74 74 65 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20  itten..    */.  
121ae 20 20 61 73 73 65 72 74 28 20 69 73 53 61 76 65    assert( isSave
121af 70 6e 74 20 29 3b 0a 20 20 20 20 69 66 28 20 28  pnt );.    if( (
121b0 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
121b1 72 41 63 71 75 69 72 65 28 70 50 61 67 65 72 2c  rAcquire(pPager,
121b2 20 70 67 6e 6f 2c 20 26 70 50 67 2c 20 31 29 29   pgno, &pPg, 1))
121b3 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
121b4 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
121b5 20 20 20 20 7d 0a 20 20 20 20 70 50 67 2d 3e 66      }.    pPg->f
121b6 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e  lags &= ~PGHDR_N
121b7 45 45 44 5f 52 45 41 44 3b 0a 20 20 20 20 73 71  EED_READ;.    sq
121b8 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44  lite3PcacheMakeD
121b9 69 72 74 79 28 70 50 67 29 3b 0a 20 20 7d 0a 20  irty(pPg);.  }. 
121ba 20 69 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20   if( pPg ){.    
121bb 2f 2a 20 4e 6f 20 70 61 67 65 20 73 68 6f 75 6c  /* No page shoul
121bc 64 20 65 76 65 72 20 62 65 20 65 78 70 6c 69 63  d ever be explic
121bd 69 74 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b  itly rolled back
121be 20 74 68 61 74 20 69 73 20 69 6e 20 75 73 65 2c   that is in use,
121bf 20 65 78 63 65 70 74 0a 20 20 20 20 2a 2a 20 66   except.    ** f
121c0 6f 72 20 70 61 67 65 20 31 20 77 68 69 63 68 20  or page 1 which 
121c1 69 73 20 68 65 6c 64 20 69 6e 20 75 73 65 20 69  is held in use i
121c2 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20  n order to keep 
121c3 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a  the lock on the.
121c4 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20      ** database 
121c5 61 63 74 69 76 65 2e 20 48 6f 77 65 76 65 72 20  active. However 
121c6 73 75 63 68 20 61 20 70 61 67 65 20 6d 61 79 20  such a page may 
121c7 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61  be rolled back a
121c8 73 20 61 20 72 65 73 75 6c 74 0a 20 20 20 20 2a  s a result.    *
121c9 2a 20 6f 66 20 61 6e 20 69 6e 74 65 72 6e 61 6c  * of an internal
121ca 20 65 72 72 6f 72 20 72 65 73 75 6c 74 69 6e 67   error resulting
121cb 20 69 6e 20 61 6e 20 61 75 74 6f 6d 61 74 69 63   in an automatic
121cc 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a 20   call to.    ** 
121cd 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c  sqlite3PagerRoll
121ce 62 61 63 6b 28 29 2e 0a 20 20 20 20 2a 2f 0a 20  back()..    */. 
121cf 20 20 20 76 6f 69 64 20 2a 70 44 61 74 61 3b 0a     void *pData;.
121d0 20 20 20 20 70 44 61 74 61 20 3d 20 70 50 67 2d      pData = pPg-
121d1 3e 70 44 61 74 61 3b 0a 20 20 20 20 6d 65 6d 63  >pData;.    memc
121d2 70 79 28 70 44 61 74 61 2c 20 61 44 61 74 61 2c  py(pData, aData,
121d3 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a   pPager->pageSiz
121d4 65 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e  e);.    pPager->
121d5 78 52 65 69 6e 69 74 65 72 28 70 50 67 29 3b 0a  xReiniter(pPg);.
121d6 20 20 20 20 69 66 28 20 69 73 4d 61 69 6e 4a 72      if( isMainJr
121d7 6e 6c 20 26 26 20 28 21 69 73 53 61 76 65 70 6e  nl && (!isSavepn
121d8 74 20 7c 7c 20 2a 70 4f 66 66 73 65 74 3c 3d 70  t || *pOffset<=p
121d9 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64  Pager->journalHd
121da 72 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49  r) ){.      /* I
121db 66 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  f the contents o
121dc 66 20 74 68 69 73 20 70 61 67 65 20 77 65 72 65  f this page were
121dd 20 6a 75 73 74 20 72 65 73 74 6f 72 65 64 20 66   just restored f
121de 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 0a 20 20  rom the main .  
121df 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66      ** journal f
121e0 69 6c 65 2c 20 74 68 65 6e 20 69 74 73 20 63 6f  ile, then its co
121e1 6e 74 65 6e 74 20 6d 75 73 74 20 62 65 20 61 73  ntent must be as
121e2 20 74 68 65 79 20 77 65 72 65 20 77 68 65 6e 20   they were when 
121e3 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 74 72  the .      ** tr
121e4 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 66 69  ansaction was fi
121e5 72 73 74 20 6f 70 65 6e 65 64 2e 20 49 6e 20 74  rst opened. In t
121e6 68 69 73 20 63 61 73 65 20 77 65 20 63 61 6e 20  his case we can 
121e7 6d 61 72 6b 20 74 68 65 20 70 61 67 65 0a 20 20  mark the page.  
121e8 20 20 20 20 2a 2a 20 61 73 20 63 6c 65 61 6e 2c      ** as clean,
121e9 20 73 69 6e 63 65 20 74 68 65 72 65 20 77 69 6c   since there wil
121ea 6c 20 62 65 20 6e 6f 20 6e 65 65 64 20 74 6f 20  l be no need to 
121eb 77 72 69 74 65 20 69 74 20 6f 75 74 20 74 6f 20  write it out to 
121ec 74 68 65 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20  the..      **.  
121ed 20 20 20 20 2a 2a 20 54 68 65 72 65 20 69 73 20      ** There is 
121ee 6f 6e 65 20 65 78 63 65 70 74 69 6f 6e 20 74 6f  one exception to
121ef 20 74 68 69 73 20 72 75 6c 65 2e 20 49 66 20 74   this rule. If t
121f0 68 65 20 70 61 67 65 20 69 73 20 62 65 69 6e 67  he page is being
121f1 20 72 6f 6c 6c 65 64 0a 20 20 20 20 20 20 2a 2a   rolled.      **
121f2 20 62 61 63 6b 20 61 73 20 70 61 72 74 20 6f 66   back as part of
121f3 20 61 20 73 61 76 65 70 6f 69 6e 74 20 28 6f 72   a savepoint (or
121f4 20 73 74 61 74 65 6d 65 6e 74 29 20 72 6f 6c 6c   statement) roll
121f5 62 61 63 6b 20 66 72 6f 6d 20 61 6e 20 0a 20 20  back from an .  
121f6 20 20 20 20 2a 2a 20 75 6e 73 79 6e 63 65 64 20      ** unsynced 
121f7 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d  portion of the m
121f8 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ain journal file
121f9 2c 20 74 68 65 6e 20 69 74 20 69 73 20 6e 6f 74  , then it is not
121fa 20 73 61 66 65 0a 20 20 20 20 20 20 2a 2a 20 74   safe.      ** t
121fb 6f 20 6d 61 72 6b 20 74 68 65 20 70 61 67 65 20  o mark the page 
121fc 61 73 20 63 6c 65 61 6e 2e 20 54 68 69 73 20 69  as clean. This i
121fd 73 20 62 65 63 61 75 73 65 20 6d 61 72 6b 69 6e  s because markin
121fe 67 20 74 68 65 20 70 61 67 65 20 61 73 0a 20 20  g the page as.  
121ff 20 20 20 20 2a 2a 20 63 6c 65 61 6e 20 77 69 6c      ** clean wil
12200 6c 20 63 6c 65 61 72 20 74 68 65 20 50 47 48 44  l clear the PGHD
12201 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67  R_NEED_SYNC flag
12202 2e 20 53 69 6e 63 65 20 74 68 65 20 70 61 67 65  . Since the page
12203 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 61 6c 72   is.      ** alr
12204 65 61 64 79 20 69 6e 20 74 68 65 20 6a 6f 75 72  eady in the jour
12205 6e 61 6c 20 66 69 6c 65 20 28 72 65 63 6f 72 64  nal file (record
12206 65 64 20 69 6e 20 50 61 67 65 72 2e 70 49 6e 4a  ed in Pager.pInJ
12207 6f 75 72 6e 61 6c 29 20 61 6e 64 0a 20 20 20 20  ournal) and.    
12208 20 20 2a 2a 20 74 68 65 20 50 47 48 44 52 5f 4e    ** the PGHDR_N
12209 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 69 73  EED_SYNC flag is
1220a 20 63 6c 65 61 72 65 64 2c 20 69 66 20 74 68 65   cleared, if the
1220b 20 70 61 67 65 20 69 73 20 77 72 69 74 74 65 6e   page is written
1220c 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 61 67 61   to.      ** aga
1220d 69 6e 20 77 69 74 68 69 6e 20 74 68 69 73 20 74  in within this t
1220e 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 74 20 77  ransaction, it w
1220f 69 6c 6c 20 62 65 20 6d 61 72 6b 65 64 20 61 73  ill be marked as
12210 20 64 69 72 74 79 20 62 75 74 0a 20 20 20 20 20   dirty but.     
12211 20 2a 2a 20 74 68 65 20 50 47 48 44 52 5f 4e 45   ** the PGHDR_NE
12212 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 77 69 6c  ED_SYNC flag wil
12213 6c 20 6e 6f 74 20 62 65 20 73 65 74 2e 20 49 74  l not be set. It
12214 20 63 6f 75 6c 64 20 74 68 65 6e 20 70 6f 74 65   could then pote
12215 6e 74 69 61 6c 6c 79 0a 20 20 20 20 20 20 2a 2a  ntially.      **
12216 20 62 65 20 77 72 69 74 74 65 6e 20 6f 75 74 20   be written out 
12217 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  into the databas
12218 65 20 66 69 6c 65 20 62 65 66 6f 72 65 20 69 74  e file before it
12219 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20  s journal file. 
1221a 20 20 20 20 20 2a 2a 20 73 65 67 6d 65 6e 74 20       ** segment 
1221b 69 73 20 73 79 6e 63 65 64 2e 20 49 66 20 61 20  is synced. If a 
1221c 63 72 61 73 68 20 6f 63 63 75 72 73 20 64 75 72  crash occurs dur
1221d 69 6e 67 20 6f 72 20 66 6f 6c 6c 6f 77 69 6e 67  ing or following
1221e 20 74 68 69 73 2c 0a 20 20 20 20 20 20 2a 2a 20   this,.      ** 
1221f 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74  database corrupt
12220 69 6f 6e 20 6d 61 79 20 65 6e 73 75 65 2e 0a 20  ion may ensue.. 
12221 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71       */.      sq
12222 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43  lite3PcacheMakeC
12223 6c 65 61 6e 28 70 50 67 29 3b 0a 20 20 20 20 7d  lean(pPg);.    }
12224 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43  .#ifdef SQLITE_C
12225 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 70  HECK_PAGES.    p
12226 50 67 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70  Pg->pageHash = p
12227 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50  ager_pagehash(pP
12228 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 2f  g);.#endif.    /
12229 2a 20 49 66 20 74 68 69 73 20 77 61 73 20 70 61  * If this was pa
1222a 67 65 20 31 2c 20 74 68 65 6e 20 72 65 73 74 6f  ge 1, then resto
1222b 72 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  re the value of 
1222c 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72 73  Pager.dbFileVers
1222d 2e 0a 20 20 20 20 2a 2a 20 44 6f 20 74 68 69 73  ..    ** Do this
1222e 20 62 65 66 6f 72 65 20 61 6e 79 20 64 65 63 6f   before any deco
1222f 64 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 69 66 28  ding. */.    if(
12230 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20   pgno==1 ){.    
12231 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 72    memcpy(&pPager
12232 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 26 28  ->dbFileVers, &(
12233 28 75 38 2a 29 70 44 61 74 61 29 5b 32 34 5d 2c  (u8*)pData)[24],
12234 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 64  sizeof(pPager->d
12235 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 20  bFileVers));.   
12236 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 63 6f 64   }..    /* Decod
12237 65 20 74 68 65 20 70 61 67 65 20 6a 75 73 74 20  e the page just 
12238 72 65 61 64 20 66 72 6f 6d 20 64 69 73 6b 20 2a  read from disk *
12239 2f 0a 20 20 20 20 43 4f 44 45 43 31 28 70 50 61  /.    CODEC1(pPa
1223a 67 65 72 2c 20 70 44 61 74 61 2c 20 70 50 67 2d  ger, pData, pPg-
1223b 3e 70 67 6e 6f 2c 20 33 2c 20 72 63 3d 53 51 4c  >pgno, 3, rc=SQL
1223c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 20 20  ITE_NOMEM);.    
1223d 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c  sqlite3PcacheRel
1223e 65 61 73 65 28 70 50 67 29 3b 0a 20 20 7d 0a 20  ease(pPg);.  }. 
1223f 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
12240 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 7a  *.** Parameter z
12241 4d 61 73 74 65 72 20 69 73 20 74 68 65 20 6e 61  Master is the na
12242 6d 65 20 6f 66 20 61 20 6d 61 73 74 65 72 20 6a  me of a master j
12243 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 41 20 73  ournal file. A s
12244 69 6e 67 6c 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a  ingle journal.**
12245 20 66 69 6c 65 20 74 68 61 74 20 72 65 66 65 72   file that refer
12246 72 65 64 20 74 6f 20 74 68 65 20 6d 61 73 74 65  red to the maste
12247 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68  r journal file h
12248 61 73 20 6a 75 73 74 20 62 65 65 6e 20 72 6f 6c  as just been rol
12249 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 20 54 68 69  led back..** Thi
1224a 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73  s routine checks
1224b 20 69 66 20 69 74 20 69 73 20 70 6f 73 73 69 62   if it is possib
1224c 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65  le to delete the
1224d 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
1224e 66 69 6c 65 2c 0a 2a 2a 20 61 6e 64 20 64 6f 65  file,.** and doe
1224f 73 20 73 6f 20 69 66 20 69 74 20 69 73 2e 0a 2a  s so if it is..*
12250 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 7a 4d  *.** Argument zM
12251 61 73 74 65 72 20 6d 61 79 20 70 6f 69 6e 74 20  aster may point 
12252 74 6f 20 50 61 67 65 72 2e 70 54 6d 70 53 70 61  to Pager.pTmpSpa
12253 63 65 2e 20 53 6f 20 74 68 61 74 20 62 75 66 66  ce. So that buff
12254 65 72 20 69 73 20 6e 6f 74 20 0a 2a 2a 20 61 76  er is not .** av
12255 61 69 6c 61 62 6c 65 20 66 6f 72 20 75 73 65 20  ailable for use 
12256 77 69 74 68 69 6e 20 74 68 69 73 20 66 75 6e 63  within this func
12257 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  tion..**.** When
12258 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61   a master journa
12259 6c 20 66 69 6c 65 20 69 73 20 63 72 65 61 74 65  l file is create
1225a 64 2c 20 69 74 20 69 73 20 70 6f 70 75 6c 61 74  d, it is populat
1225b 65 64 20 77 69 74 68 20 74 68 65 20 6e 61 6d 65  ed with the name
1225c 73 20 0a 2a 2a 20 6f 66 20 61 6c 6c 20 6f 66 20  s .** of all of 
1225d 69 74 73 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61  its child journa
1225e 6c 73 2c 20 6f 6e 65 20 61 66 74 65 72 20 61 6e  ls, one after an
1225f 6f 74 68 65 72 2c 20 66 6f 72 6d 61 74 74 65 64  other, formatted
12260 20 61 73 20 75 74 66 2d 38 20 0a 2a 2a 20 65 6e   as utf-8 .** en
12261 63 6f 64 65 64 20 74 65 78 74 2e 20 54 68 65 20  coded text. The 
12262 65 6e 64 20 6f 66 20 65 61 63 68 20 63 68 69 6c  end of each chil
12263 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  d journal file i
12264 73 20 6d 61 72 6b 65 64 20 77 69 74 68 20 61 20  s marked with a 
12265 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74  .** nul-terminat
12266 6f 72 20 62 79 74 65 20 28 30 78 30 30 29 2e 20  or byte (0x00). 
12267 69 2e 65 2e 20 74 68 65 20 65 6e 74 69 72 65 20  i.e. the entire 
12268 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 20 6d 61  contents of a ma
12269 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20  ster journal.** 
1226a 66 69 6c 65 20 66 6f 72 20 61 20 74 72 61 6e 73  file for a trans
1226b 61 63 74 69 6f 6e 20 69 6e 76 6f 6c 76 69 6e 67  action involving
1226c 20 74 77 6f 20 64 61 74 61 62 61 73 65 73 20 6d   two databases m
1226d 69 67 68 74 20 62 65 3a 0a 2a 2a 0a 2a 2a 20 20  ight be:.**.**  
1226e 20 22 2f 68 6f 6d 65 2f 62 69 6c 6c 2f 61 2e 64   "/home/bill/a.d
1226f 62 2d 6a 6f 75 72 6e 61 6c 5c 78 30 30 2f 68 6f  b-journal\x00/ho
12270 6d 65 2f 62 69 6c 6c 2f 62 2e 64 62 2d 6a 6f 75  me/bill/b.db-jou
12271 72 6e 61 6c 5c 78 30 30 22 0a 2a 2a 0a 2a 2a 20  rnal\x00".**.** 
12272 41 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  A master journal
12273 20 66 69 6c 65 20 6d 61 79 20 6f 6e 6c 79 20 62   file may only b
12274 65 20 64 65 6c 65 74 65 64 20 6f 6e 63 65 20 61  e deleted once a
12275 6c 6c 20 6f 66 20 69 74 73 20 63 68 69 6c 64 20  ll of its child 
12276 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 73 20 68 61 76  .** journals hav
12277 65 20 62 65 65 6e 20 72 6f 6c 6c 65 64 20 62 61  e been rolled ba
12278 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ck..**.** This f
12279 75 6e 63 74 69 6f 6e 20 72 65 61 64 73 20 74 68  unction reads th
1227a 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  e contents of th
1227b 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c  e master-journal
1227c 20 66 69 6c 65 20 69 6e 74 6f 20 0a 2a 2a 20 6d   file into .** m
1227d 65 6d 6f 72 79 20 61 6e 64 20 6c 6f 6f 70 73 20  emory and loops 
1227e 74 68 72 6f 75 67 68 20 65 61 63 68 20 6f 66 20  through each of 
1227f 74 68 65 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61  the child journa
12280 6c 20 6e 61 6d 65 73 2e 20 46 6f 72 0a 2a 2a 20  l names. For.** 
12281 65 61 63 68 20 63 68 69 6c 64 20 6a 6f 75 72 6e  each child journ
12282 61 6c 2c 20 69 74 20 63 68 65 63 6b 73 20 69 66  al, it checks if
12283 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 69 66 20 74  :.**.**   * if t
12284 68 65 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c  he child journal
12285 20 65 78 69 73 74 73 2c 20 61 6e 64 20 69 66 20   exists, and if 
12286 73 6f 0a 2a 2a 20 20 20 2a 20 69 66 20 74 68 65  so.**   * if the
12287 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 63   child journal c
12288 6f 6e 74 61 69 6e 73 20 61 20 72 65 66 65 72 65  ontains a refere
12289 6e 63 65 20 74 6f 20 6d 61 73 74 65 72 20 6a 6f  nce to master jo
1228a 75 72 6e 61 6c 20 0a 2a 2a 20 20 20 20 20 66 69  urnal .**     fi
1228b 6c 65 20 7a 4d 61 73 74 65 72 0a 2a 2a 0a 2a 2a  le zMaster.**.**
1228c 20 49 66 20 61 20 63 68 69 6c 64 20 6a 6f 75 72   If a child jour
1228d 6e 61 6c 20 63 61 6e 20 62 65 20 66 6f 75 6e 64  nal can be found
1228e 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 62 6f   that matches bo
1228f 74 68 20 6f 66 20 74 68 65 20 63 72 69 74 65 72  th of the criter
12290 69 61 0a 2a 2a 20 61 62 6f 76 65 2c 20 74 68 69  ia.** above, thi
12291 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
12292 6e 73 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67  ns without doing
12293 20 61 6e 79 74 68 69 6e 67 2e 20 4f 74 68 65 72   anything. Other
12294 77 69 73 65 2c 20 69 66 0a 2a 2a 20 6e 6f 20 73  wise, if.** no s
12295 75 63 68 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61  uch child journa
12296 6c 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 2c 20  l can be found, 
12297 66 69 6c 65 20 7a 4d 61 73 74 65 72 20 69 73 20  file zMaster is 
12298 64 65 6c 65 74 65 64 20 66 72 6f 6d 0a 2a 2a 20  deleted from.** 
12299 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20  the file-system 
1229a 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4f 73 44  using sqlite3OsD
1229b 65 6c 65 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 49  elete()..**.** I
1229c 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 77 69  f an IO error wi
1229d 74 68 69 6e 20 74 68 69 73 20 66 75 6e 63 74 69  thin this functi
1229e 6f 6e 2c 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  on, an error cod
1229f 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 54  e is returned. T
122a0 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  his.** function 
122a1 61 6c 6c 6f 63 61 74 65 73 20 6d 65 6d 6f 72 79  allocates memory
122a2 20 62 79 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69   by calling sqli
122a3 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 20 49 66 20  te3Malloc(). If 
122a4 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a  an allocation.**
122a5 20 66 61 69 6c 73 2c 20 53 51 4c 49 54 45 5f 4e   fails, SQLITE_N
122a6 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64  OMEM is returned
122a7 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20  . Otherwise, if 
122a8 6e 6f 20 49 4f 20 6f 72 20 6d 61 6c 6c 6f 63 20  no IO or malloc 
122a9 65 72 72 6f 72 73 20 0a 2a 2a 20 6f 63 63 75 72  errors .** occur
122aa 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  , SQLITE_OK is r
122ab 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  eturned..**.** T
122ac 4f 44 4f 3a 20 54 68 69 73 20 66 75 6e 63 74 69  ODO: This functi
122ad 6f 6e 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 73  on allocates a s
122ae 69 6e 67 6c 65 20 62 6c 6f 63 6b 20 6f 66 20 6d  ingle block of m
122af 65 6d 6f 72 79 20 74 6f 20 6c 6f 61 64 0a 2a 2a  emory to load.**
122b0 20 74 68 65 20 65 6e 74 69 72 65 20 63 6f 6e 74   the entire cont
122b1 65 6e 74 73 20 6f 66 20 74 68 65 20 6d 61 73 74  ents of the mast
122b2 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  er journal file.
122b3 20 54 68 69 73 20 63 6f 75 6c 64 20 62 65 0a 2a   This could be.*
122b4 2a 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 6b 69  * a couple of ki
122b5 6c 6f 62 79 74 65 73 20 6f 72 20 73 6f 20 2d 20  lobytes or so - 
122b6 70 6f 74 65 6e 74 69 61 6c 6c 79 20 6c 61 72 67  potentially larg
122b7 65 72 20 74 68 61 6e 20 74 68 65 20 70 61 67 65  er than the page
122b8 20 0a 2a 2a 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74   .** size..*/.st
122b9 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 64  atic int pager_d
122ba 65 6c 6d 61 73 74 65 72 28 50 61 67 65 72 20 2a  elmaster(Pager *
122bb 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 63 68  pPager, const ch
122bc 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20  ar *zMaster){.  
122bd 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
122be 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73  s = pPager->pVfs
122bf 3b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  ;.  int rc;     
122c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
122c1 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
122c2 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
122c3 70 4d 61 73 74 65 72 3b 20 20 20 20 2f 2a 20 4d  pMaster;    /* M
122c4 61 6c 6c 6f 63 27 64 20 6d 61 73 74 65 72 2d 6a  alloc'd master-j
122c5 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 63  ournal file desc
122c6 72 69 70 74 6f 72 20 2a 2f 0a 20 20 73 71 6c 69  riptor */.  sqli
122c7 74 65 33 5f 66 69 6c 65 20 2a 70 4a 6f 75 72 6e  te3_file *pJourn
122c8 61 6c 3b 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 27  al;   /* Malloc'
122c9 64 20 63 68 69 6c 64 2d 6a 6f 75 72 6e 61 6c 20  d child-journal 
122ca 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
122cb 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 73 74  */.  char *zMast
122cc 65 72 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 20 2f  erJournal = 0; /
122cd 2a 20 43 6f 6e 74 65 6e 74 73 20 6f 66 20 6d 61  * Contents of ma
122ce 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
122cf 65 20 2a 2f 0a 20 20 69 36 34 20 6e 4d 61 73 74  e */.  i64 nMast
122d0 65 72 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20  erJournal;      
122d1 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6d 61 73 74   /* Size of mast
122d2 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  er journal file 
122d3 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  */..  /* Allocat
122d4 65 20 73 70 61 63 65 20 66 6f 72 20 62 6f 74 68  e space for both
122d5 20 74 68 65 20 70 4a 6f 75 72 6e 61 6c 20 61 6e   the pJournal an
122d6 64 20 70 4d 61 73 74 65 72 20 66 69 6c 65 20 64  d pMaster file d
122d7 65 73 63 72 69 70 74 6f 72 73 2e 0a 20 20 2a 2a  escriptors..  **
122d8 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20   If successful, 
122d9 6f 70 65 6e 20 74 68 65 20 6d 61 73 74 65 72 20  open the master 
122da 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72  journal file for
122db 20 72 65 61 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20   reading..  */. 
122dc 20 70 4d 61 73 74 65 72 20 3d 20 28 73 71 6c 69   pMaster = (sqli
122dd 74 65 33 5f 66 69 6c 65 20 2a 29 73 71 6c 69 74  te3_file *)sqlit
122de 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70 56 66  e3MallocZero(pVf
122df 73 2d 3e 73 7a 4f 73 46 69 6c 65 20 2a 20 32 29  s->szOsFile * 2)
122e0 3b 0a 20 20 70 4a 6f 75 72 6e 61 6c 20 3d 20 28  ;.  pJournal = (
122e1 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 28  sqlite3_file *)(
122e2 28 28 75 38 20 2a 29 70 4d 61 73 74 65 72 29 20  ((u8 *)pMaster) 
122e3 2b 20 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65  + pVfs->szOsFile
122e4 29 3b 0a 20 20 69 66 28 20 21 70 4d 61 73 74 65  );.  if( !pMaste
122e5 72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  r ){.    rc = SQ
122e6 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65  LITE_NOMEM;.  }e
122e7 6c 73 65 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69  lse{.    const i
122e8 6e 74 20 66 6c 61 67 73 20 3d 20 28 53 51 4c 49  nt flags = (SQLI
122e9 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59  TE_OPEN_READONLY
122ea 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53  |SQLITE_OPEN_MAS
122eb 54 45 52 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20  TER_JOURNAL);.  
122ec 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
122ed 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4d 61 73 74  Open(pVfs, zMast
122ee 65 72 2c 20 70 4d 61 73 74 65 72 2c 20 66 6c 61  er, pMaster, fla
122ef 67 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66  gs, 0);.  }.  if
122f0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
122f1 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72  ) goto delmaster
122f2 5f 6f 75 74 3b 0a 0a 20 20 72 63 20 3d 20 73 71  _out;..  rc = sq
122f3 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28  lite3OsFileSize(
122f4 70 4d 61 73 74 65 72 2c 20 26 6e 4d 61 73 74 65  pMaster, &nMaste
122f5 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 69 66 28  rJournal);.  if(
122f6 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
122f7 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f   goto delmaster_
122f8 6f 75 74 3b 0a 0a 20 20 69 66 28 20 6e 4d 61 73  out;..  if( nMas
122f9 74 65 72 4a 6f 75 72 6e 61 6c 3e 30 20 29 7b 0a  terJournal>0 ){.
122fa 20 20 20 20 63 68 61 72 20 2a 7a 4a 6f 75 72 6e      char *zJourn
122fb 61 6c 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d  al;.    char *zM
122fc 61 73 74 65 72 50 74 72 20 3d 20 30 3b 0a 20 20  asterPtr = 0;.  
122fd 20 20 69 6e 74 20 6e 4d 61 73 74 65 72 50 74 72    int nMasterPtr
122fe 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e   = pVfs->mxPathn
122ff 61 6d 65 2b 31 3b 0a 0a 20 20 20 20 2f 2a 20 4c  ame+1;..    /* L
12300 6f 61 64 20 74 68 65 20 65 6e 74 69 72 65 20 6d  oad the entire m
12301 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
12302 6c 65 20 69 6e 74 6f 20 73 70 61 63 65 20 6f 62  le into space ob
12303 74 61 69 6e 65 64 20 66 72 6f 6d 0a 20 20 20 20  tained from.    
12304 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  ** sqlite3_mallo
12305 63 28 29 20 61 6e 64 20 70 6f 69 6e 74 65 64 20  c() and pointed 
12306 74 6f 20 62 79 20 7a 4d 61 73 74 65 72 4a 6f 75  to by zMasterJou
12307 72 6e 61 6c 2e 20 0a 20 20 20 20 2a 2f 0a 20 20  rnal. .    */.  
12308 20 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c    zMasterJournal
12309 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
1230a 28 28 69 6e 74 29 6e 4d 61 73 74 65 72 4a 6f 75  ((int)nMasterJou
1230b 72 6e 61 6c 20 2b 20 6e 4d 61 73 74 65 72 50 74  rnal + nMasterPt
1230c 72 20 2b 20 31 29 3b 0a 20 20 20 20 69 66 28 20  r + 1);.    if( 
1230d 21 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20  !zMasterJournal 
1230e 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
1230f 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
12310 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72    goto delmaster
12311 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  _out;.    }.    
12312 7a 4d 61 73 74 65 72 50 74 72 20 3d 20 26 7a 4d  zMasterPtr = &zM
12313 61 73 74 65 72 4a 6f 75 72 6e 61 6c 5b 6e 4d 61  asterJournal[nMa
12314 73 74 65 72 4a 6f 75 72 6e 61 6c 2b 31 5d 3b 0a  sterJournal+1];.
12315 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
12316 4f 73 52 65 61 64 28 70 4d 61 73 74 65 72 2c 20  OsRead(pMaster, 
12317 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2c 20  zMasterJournal, 
12318 28 69 6e 74 29 6e 4d 61 73 74 65 72 4a 6f 75 72  (int)nMasterJour
12319 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  nal, 0);.    if(
1231a 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1231b 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f   goto delmaster_
1231c 6f 75 74 3b 0a 20 20 20 20 7a 4d 61 73 74 65 72  out;.    zMaster
1231d 4a 6f 75 72 6e 61 6c 5b 6e 4d 61 73 74 65 72 4a  Journal[nMasterJ
1231e 6f 75 72 6e 61 6c 5d 20 3d 20 30 3b 0a 0a 20 20  ournal] = 0;..  
1231f 20 20 7a 4a 6f 75 72 6e 61 6c 20 3d 20 7a 4d 61    zJournal = zMa
12320 73 74 65 72 4a 6f 75 72 6e 61 6c 3b 0a 20 20 20  sterJournal;.   
12321 20 77 68 69 6c 65 28 20 28 7a 4a 6f 75 72 6e 61   while( (zJourna
12322 6c 2d 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  l-zMasterJournal
12323 29 3c 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  )<nMasterJournal
12324 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 78   ){.      int ex
12325 69 73 74 73 3b 0a 20 20 20 20 20 20 72 63 20 3d  ists;.      rc =
12326 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73   sqlite3OsAccess
12327 28 70 56 66 73 2c 20 7a 4a 6f 75 72 6e 61 6c 2c  (pVfs, zJournal,
12328 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45   SQLITE_ACCESS_E
12329 58 49 53 54 53 2c 20 26 65 78 69 73 74 73 29 3b  XISTS, &exists);
1232a 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
1232b 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1232c 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74      goto delmast
1232d 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a  er_out;.      }.
1232e 20 20 20 20 20 20 69 66 28 20 65 78 69 73 74 73        if( exists
1232f 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4f   ){.        /* O
12330 6e 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  ne of the journa
12331 6c 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  ls pointed to by
12332 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
12333 6e 61 6c 20 65 78 69 73 74 73 2e 0a 20 20 20 20  nal exists..    
12334 20 20 20 20 2a 2a 20 4f 70 65 6e 20 69 74 20 61      ** Open it a
12335 6e 64 20 63 68 65 63 6b 20 69 66 20 69 74 20 70  nd check if it p
12336 6f 69 6e 74 73 20 61 74 20 74 68 65 20 6d 61 73  oints at the mas
12337 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 20 49 66 0a  ter journal. If.
12338 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 2c 20 72          ** so, r
12339 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 64 65  eturn without de
1233a 6c 65 74 69 6e 67 20 74 68 65 20 6d 61 73 74 65  leting the maste
1233b 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a  r journal file..
1233c 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
1233d 20 20 20 69 6e 74 20 63 3b 0a 20 20 20 20 20 20     int c;.      
1233e 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 28 53    int flags = (S
1233f 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f  QLITE_OPEN_READO
12340 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  NLY|SQLITE_OPEN_
12341 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20  MAIN_JOURNAL);. 
12342 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
12343 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20  te3OsOpen(pVfs, 
12344 7a 4a 6f 75 72 6e 61 6c 2c 20 70 4a 6f 75 72 6e  zJournal, pJourn
12345 61 6c 2c 20 66 6c 61 67 73 2c 20 30 29 3b 0a 20  al, flags, 0);. 
12346 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
12347 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
12348 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61        goto delma
12349 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20  ster_out;.      
1234a 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 72 63 20    }..        rc 
1234b 3d 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 72  = readMasterJour
1234c 6e 61 6c 28 70 4a 6f 75 72 6e 61 6c 2c 20 7a 4d  nal(pJournal, zM
1234d 61 73 74 65 72 50 74 72 2c 20 6e 4d 61 73 74 65  asterPtr, nMaste
1234e 72 50 74 72 29 3b 0a 20 20 20 20 20 20 20 20 73  rPtr);.        s
1234f 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 4a  qlite3OsClose(pJ
12350 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 20 20 20  ournal);.       
12351 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
12352 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
12353 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f  goto delmaster_o
12354 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20  ut;.        }.. 
12355 20 20 20 20 20 20 20 63 20 3d 20 7a 4d 61 73 74         c = zMast
12356 65 72 50 74 72 5b 30 5d 21 3d 30 20 26 26 20 73  erPtr[0]!=0 && s
12357 74 72 63 6d 70 28 7a 4d 61 73 74 65 72 50 74 72  trcmp(zMasterPtr
12358 2c 20 7a 4d 61 73 74 65 72 29 3d 3d 30 3b 0a 20  , zMaster)==0;. 
12359 20 20 20 20 20 20 20 69 66 28 20 63 20 29 7b 0a         if( c ){.
1235a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20            /* We 
1235b 68 61 76 65 20 61 20 6d 61 74 63 68 2e 20 44 6f  have a match. Do
1235c 20 6e 6f 74 20 64 65 6c 65 74 65 20 74 68 65 20   not delete the 
1235d 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
1235e 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  ile. */.        
1235f 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72    goto delmaster
12360 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  _out;.        }.
12361 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 4a        }.      zJ
12362 6f 75 72 6e 61 6c 20 2b 3d 20 28 73 71 6c 69 74  ournal += (sqlit
12363 65 33 53 74 72 6c 65 6e 33 30 28 7a 4a 6f 75 72  e3Strlen30(zJour
12364 6e 61 6c 29 2b 31 29 3b 0a 20 20 20 20 7d 0a 20  nal)+1);.    }. 
12365 20 7d 0a 20 20 0a 20 20 72 63 20 3d 20 73 71 6c   }.  .  rc = sql
12366 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66  ite3OsDelete(pVf
12367 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a  s, zMaster, 0);.
12368 0a 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3a 0a  .delmaster_out:.
12369 20 20 69 66 28 20 7a 4d 61 73 74 65 72 4a 6f 75    if( zMasterJou
1236a 72 6e 61 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69  rnal ){.    sqli
1236b 74 65 33 5f 66 72 65 65 28 7a 4d 61 73 74 65 72  te3_free(zMaster
1236c 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 7d 20 20 0a  Journal);.  }  .
1236d 20 20 69 66 28 20 70 4d 61 73 74 65 72 20 29 7b    if( pMaster ){
1236e 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c  .    sqlite3OsCl
1236f 6f 73 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20  ose(pMaster);.  
12370 20 20 61 73 73 65 72 74 28 20 21 69 73 4f 70 65    assert( !isOpe
12371 6e 28 70 4a 6f 75 72 6e 61 6c 29 20 29 3b 0a 20  n(pJournal) );. 
12372 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65   }.  sqlite3_fre
12373 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 72 65  e(pMaster);.  re
12374 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a  turn rc;.}.../*.
12375 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
12376 20 69 73 20 75 73 65 64 20 74 6f 20 63 68 61 6e   is used to chan
12377 67 65 20 74 68 65 20 61 63 74 75 61 6c 20 73 69  ge the actual si
12378 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ze of the databa
12379 73 65 20 0a 2a 2a 20 66 69 6c 65 20 69 6e 20 74  se .** file in t
1237a 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 2e 20  he file-system. 
1237b 54 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e  This only happen
1237c 73 20 77 68 65 6e 20 63 6f 6d 6d 69 74 74 69 6e  s when committin
1237d 67 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c  g a transaction,
1237e 0a 2a 2a 20 6f 72 20 72 6f 6c 6c 69 6e 67 20 62  .** or rolling b
1237f 61 63 6b 20 61 20 74 72 61 6e 73 61 63 74 69 6f  ack a transactio
12380 6e 20 28 69 6e 63 6c 75 64 69 6e 67 20 72 6f 6c  n (including rol
12381 6c 69 6e 67 20 62 61 63 6b 20 61 20 68 6f 74 2d  ling back a hot-
12382 6a 6f 75 72 6e 61 6c 29 2e 0a 2a 2a 0a 2a 2a 20  journal)..**.** 
12383 49 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  If the main data
12384 62 61 73 65 20 66 69 6c 65 20 69 73 20 6e 6f 74  base file is not
12385 20 6f 70 65 6e 2c 20 6f 72 20 61 6e 20 65 78 63   open, or an exc
12386 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69 73 20 6e  lusive lock is n
12387 6f 74 0a 2a 2a 20 68 65 6c 64 2c 20 74 68 69 73  ot.** held, this
12388 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e   function is a n
12389 6f 2d 6f 70 2e 20 4f 74 68 65 72 77 69 73 65 2c  o-op. Otherwise,
1238a 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
1238b 20 66 69 6c 65 20 69 73 0a 2a 2a 20 63 68 61 6e   file is.** chan
1238c 67 65 64 20 74 6f 20 6e 50 61 67 65 20 70 61 67  ged to nPage pag
1238d 65 73 20 28 6e 50 61 67 65 2a 70 50 61 67 65 72  es (nPage*pPager
1238e 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73  ->pageSize bytes
1238f 29 2e 20 49 66 20 74 68 65 20 66 69 6c 65 0a 2a  ). If the file.*
12390 2a 20 6f 6e 20 64 69 73 6b 20 69 73 20 63 75 72  * on disk is cur
12391 72 65 6e 74 6c 79 20 6c 61 72 67 65 72 20 74 68  rently larger th
12392 61 6e 20 6e 50 61 67 65 20 70 61 67 65 73 2c 20  an nPage pages, 
12393 74 68 65 6e 20 75 73 65 20 74 68 65 20 56 46 53  then use the VFS
12394 0a 2a 2a 20 78 54 72 75 6e 63 61 74 65 28 29 20  .** xTruncate() 
12395 6d 65 74 68 6f 64 20 74 6f 20 74 72 75 6e 63 61  method to trunca
12396 74 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 4f 72 2c  te it..**.** Or,
12397 20 69 74 20 6d 69 67 68 74 20 6d 69 67 68 74 20   it might might 
12398 62 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74  be the case that
12399 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73   the file on dis
1239a 6b 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61  k is smaller tha
1239b 6e 20 0a 2a 2a 20 6e 50 61 67 65 20 70 61 67 65  n .** nPage page
1239c 73 2e 20 53 6f 6d 65 20 6f 70 65 72 61 74 69 6e  s. Some operatin
1239d 67 20 73 79 73 74 65 6d 20 69 6d 70 6c 65 6d 65  g system impleme
1239e 6e 74 61 74 69 6f 6e 73 20 63 61 6e 20 67 65 74  ntations can get
1239f 20 63 6f 6e 66 75 73 65 64 20 69 66 20 0a 2a 2a   confused if .**
123a0 20 79 6f 75 20 74 72 79 20 74 6f 20 74 72 75 6e   you try to trun
123a1 63 61 74 65 20 61 20 66 69 6c 65 20 74 6f 20 73  cate a file to s
123a2 6f 6d 65 20 73 69 7a 65 20 74 68 61 74 20 69 73  ome size that is
123a3 20 6c 61 72 67 65 72 20 74 68 61 6e 20 69 74 20   larger than it 
123a4 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 69 73  .** currently is
123a5 2c 20 73 6f 20 64 65 74 65 63 74 20 74 68 69 73  , so detect this
123a6 20 63 61 73 65 20 61 6e 64 20 77 72 69 74 65 20   case and write 
123a7 61 20 73 69 6e 67 6c 65 20 7a 65 72 6f 20 62 79  a single zero by
123a8 74 65 20 74 6f 20 0a 2a 2a 20 74 68 65 20 65 6e  te to .** the en
123a9 64 20 6f 66 20 74 68 65 20 6e 65 77 20 66 69 6c  d of the new fil
123aa 65 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a  e instead..**.**
123ab 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20   If successful, 
123ac 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
123ad 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72  . If an IO error
123ae 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 6d 6f   occurs while mo
123af 64 69 66 79 69 6e 67 0a 2a 2a 20 74 68 65 20 64  difying.** the d
123b0 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 72 65  atabase file, re
123b1 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63  turn the error c
123b2 6f 64 65 20 74 6f 20 74 68 65 20 63 61 6c 6c 65  ode to the calle
123b3 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  r..*/.static int
123b4 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28   pager_truncate(
123b5 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50  Pager *pPager, P
123b6 67 6e 6f 20 6e 50 61 67 65 29 7b 0a 20 20 69 6e  gno nPage){.  in
123b7 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
123b8 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  ;.  if( pPager->
123b9 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43  state>=PAGER_EXC
123ba 4c 55 53 49 56 45 20 26 26 20 69 73 4f 70 65 6e  LUSIVE && isOpen
123bb 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a  (pPager->fd) ){.
123bc 20 20 20 20 69 36 34 20 63 75 72 72 65 6e 74 53      i64 currentS
123bd 69 7a 65 2c 20 6e 65 77 53 69 7a 65 3b 0a 20 20  ize, newSize;.  
123be 20 20 2f 2a 20 54 4f 44 4f 3a 20 49 73 20 69 74    /* TODO: Is it
123bf 20 73 61 66 65 20 74 6f 20 75 73 65 20 50 61 67   safe to use Pag
123c0 65 72 2e 64 62 46 69 6c 65 53 69 7a 65 20 68 65  er.dbFileSize he
123c1 72 65 3f 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  re? */.    rc = 
123c2 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a  sqlite3OsFileSiz
123c3 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 63  e(pPager->fd, &c
123c4 75 72 72 65 6e 74 53 69 7a 65 29 3b 0a 20 20 20  urrentSize);.   
123c5 20 6e 65 77 53 69 7a 65 20 3d 20 70 50 61 67 65   newSize = pPage
123c6 72 2d 3e 70 61 67 65 53 69 7a 65 2a 28 69 36 34  r->pageSize*(i64
123c7 29 6e 50 61 67 65 3b 0a 20 20 20 20 69 66 28 20  )nPage;.    if( 
123c8 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
123c9 20 63 75 72 72 65 6e 74 53 69 7a 65 21 3d 6e 65   currentSize!=ne
123ca 77 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 69  wSize ){.      i
123cb 66 28 20 63 75 72 72 65 6e 74 53 69 7a 65 3e 6e  f( currentSize>n
123cc 65 77 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20  ewSize ){.      
123cd 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
123ce 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d  Truncate(pPager-
123cf 3e 66 64 2c 20 6e 65 77 53 69 7a 65 29 3b 0a 20  >fd, newSize);. 
123d0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
123d1 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
123d2 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e  OsWrite(pPager->
123d3 66 64 2c 20 22 22 2c 20 31 2c 20 6e 65 77 53 69  fd, "", 1, newSi
123d4 7a 65 2d 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20  ze-1);.      }. 
123d5 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
123d6 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
123d7 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65    pPager->dbFile
123d8 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20  Size = nPage;.  
123d9 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
123da 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
123db 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 76 61  /*.** Set the va
123dc 6c 75 65 20 6f 66 20 74 68 65 20 50 61 67 65 72  lue of the Pager
123dd 2e 73 65 63 74 6f 72 53 69 7a 65 20 76 61 72 69  .sectorSize vari
123de 61 62 6c 65 20 66 6f 72 20 74 68 65 20 67 69 76  able for the giv
123df 65 6e 0a 2a 2a 20 70 61 67 65 72 20 62 61 73 65  en.** pager base
123e0 64 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 72  d on the value r
123e1 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 78  eturned by the x
123e2 53 65 63 74 6f 72 53 69 7a 65 20 6d 65 74 68 6f  SectorSize metho
123e3 64 0a 2a 2a 20 6f 66 20 74 68 65 20 6f 70 65 6e  d.** of the open
123e4 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
123e5 54 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20  The sector size 
123e6 77 69 6c 6c 20 62 65 20 75 73 65 64 20 75 73 65  will be used use
123e7 64 20 0a 2a 2a 20 74 6f 20 64 65 74 65 72 6d 69  d .** to determi
123e8 6e 65 20 74 68 65 20 73 69 7a 65 20 61 6e 64 20  ne the size and 
123e9 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 6a 6f 75  alignment of jou
123ea 72 6e 61 6c 20 68 65 61 64 65 72 20 61 6e 64 20  rnal header and 
123eb 0a 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  .** master journ
123ec 61 6c 20 70 6f 69 6e 74 65 72 73 20 77 69 74 68  al pointers with
123ed 69 6e 20 63 72 65 61 74 65 64 20 6a 6f 75 72 6e  in created journ
123ee 61 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20  al files..**.** 
123ef 46 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69  For temporary fi
123f0 6c 65 73 20 74 68 65 20 65 66 66 65 63 74 69 76  les the effectiv
123f1 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69 73  e sector size is
123f2 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 74 65   always 512 byte
123f3 73 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69  s..**.** Otherwi
123f4 73 65 2c 20 66 6f 72 20 6e 6f 6e 2d 74 65 6d 70  se, for non-temp
123f5 6f 72 61 72 79 20 66 69 6c 65 73 2c 20 74 68 65  orary files, the
123f6 20 65 66 66 65 63 74 69 76 65 20 73 65 63 74 6f   effective secto
123f7 72 20 73 69 7a 65 20 69 73 0a 2a 2a 20 74 68 65  r size is.** the
123f8 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
123f9 62 79 20 74 68 65 20 78 53 65 63 74 6f 72 53 69  by the xSectorSi
123fa 7a 65 28 29 20 6d 65 74 68 6f 64 20 72 6f 75 6e  ze() method roun
123fb 64 65 64 20 75 70 20 74 6f 20 35 31 32 20 69 66  ded up to 512 if
123fc 0a 2a 2a 20 69 74 20 69 73 20 6c 65 73 73 20 74  .** it is less t
123fd 68 61 6e 20 35 31 32 2c 20 6f 72 20 72 6f 75 6e  han 512, or roun
123fe 64 65 64 20 64 6f 77 6e 20 74 6f 20 4d 41 58 5f  ded down to MAX_
123ff 53 45 43 54 4f 52 5f 53 49 5a 45 20 69 66 20 69  SECTOR_SIZE if i
12400 74 0a 2a 2a 20 69 73 20 67 72 65 61 74 65 72 20  t.** is greater 
12401 74 68 61 6e 20 4d 41 58 5f 53 45 43 54 4f 52 5f  than MAX_SECTOR_
12402 53 49 5a 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  SIZE..*/.static 
12403 76 6f 69 64 20 73 65 74 53 65 63 74 6f 72 53 69  void setSectorSi
12404 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ze(Pager *pPager
12405 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f  ){.  assert( isO
12406 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20  pen(pPager->fd) 
12407 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46  || pPager->tempF
12408 69 6c 65 20 29 3b 0a 0a 20 20 69 66 28 20 21 70  ile );..  if( !p
12409 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20  Pager->tempFile 
1240a 29 7b 0a 20 20 20 20 2f 2a 20 53 65 63 74 6f 72  ){.    /* Sector
1240b 20 73 69 7a 65 20 64 6f 65 73 6e 27 74 20 6d 61   size doesn't ma
1240c 74 74 65 72 20 66 6f 72 20 74 65 6d 70 6f 72 61  tter for tempora
1240d 72 79 20 66 69 6c 65 73 2e 20 41 6c 73 6f 2c 20  ry files. Also, 
1240e 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20  the file.    ** 
1240f 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 62 65 65  may not have bee
12410 6e 20 6f 70 65 6e 65 64 20 79 65 74 2c 20 69 6e  n opened yet, in
12411 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65 20   which case the 
12412 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29 0a 20  OsSectorSize(). 
12413 20 20 20 2a 2a 20 63 61 6c 6c 20 77 69 6c 6c 20     ** call will 
12414 73 65 67 66 61 75 6c 74 2e 0a 20 20 20 20 2a 2f  segfault..    */
12415 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63  .    pPager->sec
12416 74 6f 72 53 69 7a 65 20 3d 20 73 71 6c 69 74 65  torSize = sqlite
12417 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 70 50  3OsSectorSize(pP
12418 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 7d 0a 20  ager->fd);.  }. 
12419 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 65 63   if( pPager->sec
1241a 74 6f 72 53 69 7a 65 3c 35 31 32 20 29 7b 0a 20  torSize<512 ){. 
1241b 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f     pPager->secto
1241c 72 53 69 7a 65 20 3d 20 35 31 32 3b 0a 20 20 7d  rSize = 512;.  }
1241d 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73  .  if( pPager->s
1241e 65 63 74 6f 72 53 69 7a 65 3e 4d 41 58 5f 53 45  ectorSize>MAX_SE
1241f 43 54 4f 52 5f 53 49 5a 45 20 29 7b 0a 20 20 20  CTOR_SIZE ){.   
12420 20 61 73 73 65 72 74 28 20 4d 41 58 5f 53 45 43   assert( MAX_SEC
12421 54 4f 52 5f 53 49 5a 45 3e 3d 35 31 32 20 29 3b  TOR_SIZE>=512 );
12422 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63  .    pPager->sec
12423 74 6f 72 53 69 7a 65 20 3d 20 4d 41 58 5f 53 45  torSize = MAX_SE
12424 43 54 4f 52 5f 53 49 5a 45 3b 0a 20 20 7d 0a 7d  CTOR_SIZE;.  }.}
12425 0a 0a 2f 2a 0a 2a 2a 20 50 6c 61 79 62 61 63 6b  ../*.** Playback
12426 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 6e 64   the journal and
12427 20 74 68 75 73 20 72 65 73 74 6f 72 65 20 74 68   thus restore th
12428 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
12429 74 6f 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 20  to.** the state 
1242a 69 74 20 77 61 73 20 69 6e 20 62 65 66 6f 72 65  it was in before
1242b 20 77 65 20 73 74 61 72 74 65 64 20 6d 61 6b 69   we started maki
1242c 6e 67 20 63 68 61 6e 67 65 73 2e 20 20 0a 2a 2a  ng changes.  .**
1242d 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20  .** The journal 
1242e 66 69 6c 65 20 66 6f 72 6d 61 74 20 69 73 20 61  file format is a
1242f 73 20 66 6f 6c 6c 6f 77 73 3a 20 0a 2a 2a 0a 2a  s follows: .**.*
12430 2a 20 20 28 31 29 20 20 38 20 62 79 74 65 20 70  *  (1)  8 byte p
12431 72 65 66 69 78 2e 20 20 41 20 63 6f 70 79 20 6f  refix.  A copy o
12432 66 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 5b  f aJournalMagic[
12433 5d 2e 0a 2a 2a 20 20 28 32 29 20 20 34 20 62 79  ]..**  (2)  4 by
12434 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e  te big-endian in
12435 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74  teger which is t
12436 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 76 61 6c  he number of val
12437 69 64 20 70 61 67 65 20 72 65 63 6f 72 64 73 0a  id page records.
12438 2a 2a 20 20 20 20 20 20 20 69 6e 20 74 68 65 20  **       in the 
12439 6a 6f 75 72 6e 61 6c 2e 20 20 49 66 20 74 68 69  journal.  If thi
1243a 73 20 76 61 6c 75 65 20 69 73 20 30 78 66 66 66  s value is 0xfff
1243b 66 66 66 66 66 2c 20 74 68 65 6e 20 63 6f 6d 70  fffff, then comp
1243c 75 74 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 20  ute the.**      
1243d 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20   number of page 
1243e 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 74 68 65  records from the
1243f 20 6a 6f 75 72 6e 61 6c 20 73 69 7a 65 2e 0a 2a   journal size..*
12440 2a 20 20 28 33 29 20 20 34 20 62 79 74 65 20 62  *  (3)  4 byte b
12441 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65  ig-endian intege
12442 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 69  r which is the i
12443 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 66 6f 72  nitial value for
12444 20 74 68 65 20 0a 2a 2a 20 20 20 20 20 20 20 73   the .**       s
12445 61 6e 69 74 79 20 63 68 65 63 6b 73 75 6d 2e 0a  anity checksum..
12446 2a 2a 20 20 28 34 29 20 20 34 20 62 79 74 65 20  **  (4)  4 byte 
12447 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73  integer which is
12448 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
12449 61 67 65 73 20 74 6f 20 74 72 75 6e 63 61 74 65  ages to truncate
1244a 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 64 61   the.**       da
1244b 74 61 62 61 73 65 20 74 6f 20 64 75 72 69 6e 67  tabase to during
1244c 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 20   a rollback..** 
1244d 20 28 35 29 20 20 34 20 62 79 74 65 20 62 69 67   (5)  4 byte big
1244e 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20  -endian integer 
1244f 77 68 69 63 68 20 69 73 20 74 68 65 20 73 65 63  which is the sec
12450 74 6f 72 20 73 69 7a 65 2e 20 20 54 68 65 20 68  tor size.  The h
12451 65 61 64 65 72 0a 2a 2a 20 20 20 20 20 20 20 69  eader.**       i
12452 73 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65  s this many byte
12453 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 20 20 28  s in size..**  (
12454 36 29 20 20 34 20 62 79 74 65 20 62 69 67 2d 65  6)  4 byte big-e
12455 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 77 68  ndian integer wh
12456 69 63 68 20 69 73 20 74 68 65 20 70 61 67 65 20  ich is the page 
12457 63 61 73 65 2e 0a 2a 2a 20 20 28 37 29 20 20 34  case..**  (7)  4
12458 20 62 79 74 65 20 69 6e 74 65 67 65 72 20 77 68   byte integer wh
12459 69 63 68 20 69 73 20 74 68 65 20 6e 75 6d 62 65  ich is the numbe
1245a 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68  r of bytes in th
1245b 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
1245c 0a 2a 2a 20 20 20 20 20 20 20 6e 61 6d 65 2e 20  .**       name. 
1245d 20 54 68 65 20 76 61 6c 75 65 20 6d 61 79 20 62   The value may b
1245e 65 20 7a 65 72 6f 20 28 69 6e 64 69 63 61 74 65  e zero (indicate
1245f 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 6e   that there is n
12460 6f 20 6d 61 73 74 65 72 0a 2a 2a 20 20 20 20 20  o master.**     
12461 20 20 6a 6f 75 72 6e 61 6c 2e 29 0a 2a 2a 20 20    journal.).**  
12462 28 38 29 20 20 4e 20 62 79 74 65 73 20 6f 66 20  (8)  N bytes of 
12463 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
12464 61 6c 20 6e 61 6d 65 2e 20 20 54 68 65 20 6e 61  al name.  The na
12465 6d 65 20 77 69 6c 6c 20 62 65 20 6e 75 6c 2d 74  me will be nul-t
12466 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 20 20 20  erminated.**    
12467 20 20 20 61 6e 64 20 6d 69 67 68 74 20 62 65 20     and might be 
12468 73 68 6f 72 74 65 72 20 74 68 61 6e 20 74 68 65  shorter than the
12469 20 76 61 6c 75 65 20 72 65 61 64 20 66 72 6f 6d   value read from
1246a 20 28 35 29 2e 20 20 49 66 20 74 68 65 20 66 69   (5).  If the fi
1246b 72 73 74 20 62 79 74 65 0a 2a 2a 20 20 20 20 20  rst byte.**     
1246c 20 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 69 73    of the name is
1246d 20 5c 30 30 30 20 74 68 65 6e 20 74 68 65 72 65   \000 then there
1246e 20 69 73 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f   is no master jo
1246f 75 72 6e 61 6c 2e 20 20 54 68 65 20 6d 61 73 74  urnal.  The mast
12470 65 72 0a 2a 2a 20 20 20 20 20 20 20 6a 6f 75 72  er.**       jour
12471 6e 61 6c 20 6e 61 6d 65 20 69 73 20 73 74 6f 72  nal name is stor
12472 65 64 20 69 6e 20 55 54 46 2d 38 2e 0a 2a 2a 20  ed in UTF-8..** 
12473 20 28 39 29 20 20 5a 65 72 6f 20 6f 72 20 6d 6f   (9)  Zero or mo
12474 72 65 20 70 61 67 65 73 20 69 6e 73 74 61 6e 63  re pages instanc
12475 65 73 2c 20 65 61 63 68 20 61 73 20 66 6f 6c 6c  es, each as foll
12476 6f 77 73 3a 0a 2a 2a 20 20 20 20 20 20 20 20 2b  ows:.**        +
12477 20 20 34 20 62 79 74 65 20 70 61 67 65 20 6e 75    4 byte page nu
12478 6d 62 65 72 2e 0a 2a 2a 20 20 20 20 20 20 20 20  mber..**        
12479 2b 20 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53  +  pPager->pageS
1247a 69 7a 65 20 62 79 74 65 73 20 6f 66 20 64 61 74  ize bytes of dat
1247b 61 2e 0a 2a 2a 20 20 20 20 20 20 20 20 2b 20 20  a..**        +  
1247c 34 20 62 79 74 65 20 63 68 65 63 6b 73 75 6d 0a  4 byte checksum.
1247d 2a 2a 0a 2a 2a 20 57 68 65 6e 20 77 65 20 73 70  **.** When we sp
1247e 65 61 6b 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  eak of the journ
1247f 61 6c 20 68 65 61 64 65 72 2c 20 77 65 20 6d 65  al header, we me
12480 61 6e 20 74 68 65 20 66 69 72 73 74 20 38 20 69  an the first 8 i
12481 74 65 6d 73 20 61 62 6f 76 65 2e 0a 2a 2a 20 45  tems above..** E
12482 61 63 68 20 65 6e 74 72 79 20 69 6e 20 74 68 65  ach entry in the
12483 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6e 20 69   journal is an i
12484 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 39  nstance of the 9
12485 74 68 20 69 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 43  th item..**.** C
12486 61 6c 6c 20 74 68 65 20 76 61 6c 75 65 20 66 72  all the value fr
12487 6f 6d 20 74 68 65 20 73 65 63 6f 6e 64 20 62 75  om the second bu
12488 6c 6c 65 74 20 22 6e 52 65 63 22 2e 20 20 6e 52  llet "nRec".  nR
12489 65 63 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  ec is the number
1248a 20 6f 66 0a 2a 2a 20 76 61 6c 69 64 20 70 61 67   of.** valid pag
1248b 65 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65  e entries in the
1248c 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 6e 20 6d 6f   journal.  In mo
1248d 73 74 20 63 61 73 65 73 2c 20 79 6f 75 20 63 61  st cases, you ca
1248e 6e 20 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a 2a  n compute the.**
1248f 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 63 20 66   value of nRec f
12490 72 6f 6d 20 74 68 65 20 73 69 7a 65 20 6f 66 20  rom the size of 
12491 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
12492 2e 20 20 42 75 74 20 69 66 20 61 20 70 6f 77 65  .  But if a powe
12493 72 0a 2a 2a 20 66 61 69 6c 75 72 65 20 6f 63 63  r.** failure occ
12494 75 72 72 65 64 20 77 68 69 6c 65 20 74 68 65 20  urred while the 
12495 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62 65 69 6e  journal was bein
12496 67 20 77 72 69 74 74 65 6e 2c 20 69 74 20 63 6f  g written, it co
12497 75 6c 64 20 62 65 20 74 68 65 0a 2a 2a 20 63 61  uld be the.** ca
12498 73 65 20 74 68 61 74 20 74 68 65 20 73 69 7a 65  se that the size
12499 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
1249a 66 69 6c 65 20 68 61 64 20 61 6c 72 65 61 64 79  file had already
1249b 20 62 65 65 6e 20 69 6e 63 72 65 61 73 65 64 20   been increased 
1249c 62 75 74 0a 2a 2a 20 74 68 65 20 65 78 74 72 61  but.** the extra
1249d 20 65 6e 74 72 69 65 73 20 68 61 64 20 6e 6f 74   entries had not
1249e 20 79 65 74 20 6d 61 64 65 20 69 74 20 73 61 66   yet made it saf
1249f 65 6c 79 20 74 6f 20 64 69 73 6b 2e 20 20 49 6e  ely to disk.  In
124a0 20 73 75 63 68 20 61 20 63 61 73 65 2c 0a 2a 2a   such a case,.**
124a1 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6e 52   the value of nR
124a2 65 63 20 63 6f 6d 70 75 74 65 64 20 66 72 6f 6d  ec computed from
124a3 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 77   the file size w
124a4 6f 75 6c 64 20 62 65 20 74 6f 6f 20 6c 61 72 67  ould be too larg
124a5 65 2e 20 20 46 6f 72 0a 2a 2a 20 74 68 61 74 20  e.  For.** that 
124a6 72 65 61 73 6f 6e 2c 20 77 65 20 61 6c 77 61 79  reason, we alway
124a7 73 20 75 73 65 20 74 68 65 20 6e 52 65 63 20 76  s use the nRec v
124a8 61 6c 75 65 20 69 6e 20 74 68 65 20 68 65 61 64  alue in the head
124a9 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  er..**.** If the
124aa 20 6e 52 65 63 20 76 61 6c 75 65 20 69 73 20 30   nRec value is 0
124ab 78 66 66 66 66 66 66 66 66 20 69 74 20 6d 65 61  xffffffff it mea
124ac 6e 73 20 74 68 61 74 20 6e 52 65 63 20 73 68 6f  ns that nRec sho
124ad 75 6c 64 20 62 65 20 63 6f 6d 70 75 74 65 64 0a  uld be computed.
124ae 2a 2a 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65  ** from the file
124af 20 73 69 7a 65 2e 20 20 54 68 69 73 20 76 61 6c   size.  This val
124b0 75 65 20 69 73 20 75 73 65 64 20 77 68 65 6e 20  ue is used when 
124b1 74 68 65 20 75 73 65 72 20 73 65 6c 65 63 74 73  the user selects
124b2 20 74 68 65 0a 2a 2a 20 6e 6f 2d 73 79 6e 63 20   the.** no-sync 
124b3 6f 70 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 6a  option for the j
124b4 6f 75 72 6e 61 6c 2e 20 20 41 20 70 6f 77 65 72  ournal.  A power
124b5 20 66 61 69 6c 75 72 65 20 63 6f 75 6c 64 20 6c   failure could l
124b6 65 61 64 20 74 6f 20 63 6f 72 72 75 70 74 69 6f  ead to corruptio
124b7 6e 0a 2a 2a 20 69 6e 20 74 68 69 73 20 63 61 73  n.** in this cas
124b8 65 2e 20 20 42 75 74 20 66 6f 72 20 74 68 69 6e  e.  But for thin
124b9 67 73 20 6c 69 6b 65 20 74 65 6d 70 6f 72 61 72  gs like temporar
124ba 79 20 74 61 62 6c 65 20 28 77 68 69 63 68 20 77  y table (which w
124bb 69 6c 6c 20 62 65 0a 2a 2a 20 64 65 6c 65 74 65  ill be.** delete
124bc 64 20 77 68 65 6e 20 74 68 65 20 70 6f 77 65 72  d when the power
124bd 20 69 73 20 72 65 73 74 6f 72 65 64 29 20 77 65   is restored) we
124be 20 64 6f 6e 27 74 20 63 61 72 65 2e 20 20 0a 2a   don't care.  .*
124bf 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 6c 65  *.** If the file
124c0 20 6f 70 65 6e 65 64 20 61 73 20 74 68 65 20 6a   opened as the j
124c1 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e  ournal file is n
124c2 6f 74 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 64  ot a well-formed
124c3 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  .** journal file
124c4 20 74 68 65 6e 20 61 6c 6c 20 70 61 67 65 73 20   then all pages 
124c5 75 70 20 74 6f 20 74 68 65 20 66 69 72 73 74 20  up to the first 
124c6 63 6f 72 72 75 70 74 65 64 20 70 61 67 65 20 61  corrupted page a
124c7 72 65 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62 61 63  re rolled.** bac
124c8 6b 20 28 6f 72 20 6e 6f 20 70 61 67 65 73 20 69  k (or no pages i
124c9 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65  f the journal he
124ca 61 64 65 72 20 69 73 20 63 6f 72 72 75 70 74 65  ader is corrupte
124cb 64 29 2e 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20  d). The journal 
124cc 66 69 6c 65 0a 2a 2a 20 69 73 20 74 68 65 6e 20  file.** is then 
124cd 64 65 6c 65 74 65 64 20 61 6e 64 20 53 51 4c 49  deleted and SQLI
124ce 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2c 20  TE_OK returned, 
124cf 6a 75 73 74 20 61 73 20 69 66 20 6e 6f 20 63 6f  just as if no co
124d0 72 72 75 70 74 69 6f 6e 20 68 61 64 0a 2a 2a 20  rruption had.** 
124d1 62 65 65 6e 20 65 6e 63 6f 75 6e 74 65 72 65 64  been encountered
124d2 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 2f  ..**.** If an I/
124d3 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28 29 20 65 72  O or malloc() er
124d4 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65 20  ror occurs, the 
124d5 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 69 73 20  journal-file is 
124d6 6e 6f 74 20 64 65 6c 65 74 65 64 0a 2a 2a 20 61  not deleted.** a
124d7 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  nd an error code
124d8 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
124d9 0a 2a 2a 20 54 68 65 20 69 73 48 6f 74 20 70 61  .** The isHot pa
124da 72 61 6d 65 74 65 72 20 69 6e 64 69 63 61 74 65  rameter indicate
124db 73 20 74 68 61 74 20 77 65 20 61 72 65 20 74 72  s that we are tr
124dc 79 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63 6b  ying to rollback
124dd 20 61 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 74 68   a journal.** th
124de 61 74 20 6d 69 67 68 74 20 62 65 20 61 20 68 6f  at might be a ho
124df 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 4f 72 2c 20  t journal.  Or, 
124e0 69 74 20 63 6f 75 6c 64 20 62 65 20 74 68 61 74  it could be that
124e1 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20   the journal is 
124e2 0a 2a 2a 20 70 72 65 73 65 72 76 65 64 20 62 65  .** preserved be
124e3 63 61 75 73 65 20 6f 66 20 4a 4f 55 52 4e 41 4c  cause of JOURNAL
124e4 4d 4f 44 45 5f 50 45 52 53 49 53 54 20 6f 72 20  MODE_PERSIST or 
124e5 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e  JOURNALMODE_TRUN
124e6 43 41 54 45 2e 0a 2a 2a 20 49 66 20 74 68 65 20  CATE..** If the 
124e7 6a 6f 75 72 6e 61 6c 20 72 65 61 6c 6c 79 20 69  journal really i
124e8 73 20 68 6f 74 2c 20 72 65 73 65 74 20 74 68 65  s hot, reset the
124e9 20 70 61 67 65 72 20 63 61 63 68 65 20 70 72 69   pager cache pri
124ea 6f 72 20 72 6f 6c 6c 69 6e 67 0a 2a 2a 20 62 61  or rolling.** ba
124eb 63 6b 20 61 6e 79 20 63 6f 6e 74 65 6e 74 2e 20  ck any content. 
124ec 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   If the journal 
124ed 69 73 20 6d 65 72 65 6c 79 20 70 65 72 73 69 73  is merely persis
124ee 74 65 6e 74 2c 20 6e 6f 20 72 65 73 65 74 20 69  tent, no reset i
124ef 73 0a 2a 2a 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a  s.** needed..*/.
124f0 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72  static int pager
124f1 5f 70 6c 61 79 62 61 63 6b 28 50 61 67 65 72 20  _playback(Pager 
124f2 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 69 73 48  *pPager, int isH
124f3 6f 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76  ot){.  sqlite3_v
124f4 66 73 20 2a 70 56 66 73 20 3d 20 70 50 61 67 65  fs *pVfs = pPage
124f5 72 2d 3e 70 56 66 73 3b 0a 20 20 69 36 34 20 73  r->pVfs;.  i64 s
124f6 7a 4a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  zJ;             
124f7 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74      /* Size of t
124f8 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
124f9 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 33  in bytes */.  u3
124fa 32 20 6e 52 65 63 3b 20 20 20 20 20 20 20 20 20  2 nRec;         
124fb 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
124fc 20 6f 66 20 52 65 63 6f 72 64 73 20 69 6e 20 74   of Records in t
124fd 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20  he journal */.  
124fe 75 33 32 20 75 3b 20 20 20 20 20 20 20 20 20 20  u32 u;          
124ff 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e 73 69           /* Unsi
12500 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 6e 74 65  gned loop counte
12501 72 20 2a 2f 0a 20 20 50 67 6e 6f 20 6d 78 50 67  r */.  Pgno mxPg
12502 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
12503 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 6f  /* Size of the o
12504 72 69 67 69 6e 61 6c 20 66 69 6c 65 20 69 6e 20  riginal file in 
12505 70 61 67 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72  pages */.  int r
12506 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
12507 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f      /* Result co
12508 64 65 20 6f 66 20 61 20 73 75 62 72 6f 75 74 69  de of a subrouti
12509 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 20  ne */.  int res 
1250a 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 1;            
1250b 20 2f 2a 20 56 61 6c 75 65 20 72 65 74 75 72 6e   /* Value return
1250c 65 64 20 62 79 20 73 71 6c 69 74 65 33 4f 73 41  ed by sqlite3OsA
1250d 63 63 65 73 73 28 29 20 2a 2f 0a 20 20 63 68 61  ccess() */.  cha
1250e 72 20 2a 7a 4d 61 73 74 65 72 20 3d 20 30 3b 20  r *zMaster = 0; 
1250f 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
12510 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
12511 66 69 6c 65 20 69 66 20 61 6e 79 20 2a 2f 0a 20  file if any */. 
12512 20 69 6e 74 20 6e 65 65 64 50 61 67 65 72 52 65   int needPagerRe
12513 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 54 72 75  set;      /* Tru
12514 65 20 74 6f 20 72 65 73 65 74 20 70 61 67 65 20  e to reset page 
12515 70 72 69 6f 72 20 74 6f 20 66 69 72 73 74 20 70  prior to first p
12516 61 67 65 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a  age rollback */.
12517 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74  .  /* Figure out
12518 20 68 6f 77 20 6d 61 6e 79 20 72 65 63 6f 72 64   how many record
12519 73 20 61 72 65 20 69 6e 20 74 68 65 20 6a 6f 75  s are in the jou
1251a 72 6e 61 6c 2e 20 20 41 62 6f 72 74 20 65 61 72  rnal.  Abort ear
1251b 6c 79 20 69 66 0a 20 20 2a 2a 20 74 68 65 20 6a  ly if.  ** the j
1251c 6f 75 72 6e 61 6c 20 69 73 20 65 6d 70 74 79 2e  ournal is empty.
1251d 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
1251e 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a  isOpen(pPager->j
1251f 66 64 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 71  fd) );.  rc = sq
12520 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28  lite3OsFileSize(
12521 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 73 7a  pPager->jfd, &sz
12522 4a 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  J);.  if( rc!=SQ
12523 4c 49 54 45 5f 4f 4b 20 7c 7c 20 73 7a 4a 3d 3d  LITE_OK || szJ==
12524 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 6e  0 ){.    goto en
12525 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 7d 0a  d_playback;.  }.
12526 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 6d  .  /* Read the m
12527 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61  aster journal na
12528 6d 65 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72  me from the jour
12529 6e 61 6c 2c 20 69 66 20 69 74 20 69 73 20 70 72  nal, if it is pr
1252a 65 73 65 6e 74 2e 0a 20 20 2a 2a 20 49 66 20 61  esent..  ** If a
1252b 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
1252c 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 73 70 65  file name is spe
1252d 63 69 66 69 65 64 2c 20 62 75 74 20 74 68 65 20  cified, but the 
1252e 66 69 6c 65 20 69 73 20 6e 6f 74 0a 20 20 2a 2a  file is not.  **
1252f 20 70 72 65 73 65 6e 74 20 6f 6e 20 64 69 73 6b   present on disk
12530 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e  , then the journ
12531 61 6c 20 69 73 20 6e 6f 74 20 68 6f 74 20 61 6e  al is not hot an
12532 64 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20  d does not need 
12533 74 6f 20 62 65 0a 20 20 2a 2a 20 70 6c 61 79 65  to be.  ** playe
12534 64 20 62 61 63 6b 2e 0a 20 20 2a 2a 0a 20 20 2a  d back..  **.  *
12535 2a 20 54 4f 44 4f 3a 20 54 65 63 68 6e 69 63 61  * TODO: Technica
12536 6c 6c 79 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  lly the followin
12537 67 20 69 73 20 61 6e 20 65 72 72 6f 72 20 62 65  g is an error be
12538 63 61 75 73 65 20 69 74 20 61 73 73 75 6d 65 73  cause it assumes
12539 20 74 68 61 74 0a 20 20 2a 2a 20 62 75 66 66 65   that.  ** buffe
1253a 72 20 50 61 67 65 72 2e 70 54 6d 70 53 70 61 63  r Pager.pTmpSpac
1253b 65 20 69 73 20 28 6d 78 50 61 74 68 6e 61 6d 65  e is (mxPathname
1253c 2b 31 29 20 62 79 74 65 73 20 6f 72 20 6c 61 72  +1) bytes or lar
1253d 67 65 72 2e 20 69 2e 65 2e 20 74 68 61 74 0a 20  ger. i.e. that. 
1253e 20 2a 2a 20 28 70 50 61 67 65 72 2d 3e 70 61 67   ** (pPager->pag
1253f 65 53 69 7a 65 20 3e 3d 20 70 50 61 67 65 72 2d  eSize >= pPager-
12540 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d  >pVfs->mxPathnam
12541 65 2b 31 29 2e 20 55 73 69 6e 67 20 6f 73 5f 75  e+1). Using os_u
12542 6e 69 78 2e 63 2c 0a 20 20 2a 2a 20 20 6d 78 50  nix.c,.  **  mxP
12543 61 74 68 6e 61 6d 65 20 69 73 20 35 31 32 2c 20  athname is 512, 
12544 77 68 69 63 68 20 69 73 20 74 68 65 20 73 61 6d  which is the sam
12545 65 20 61 73 20 74 68 65 20 6d 69 6e 69 6d 75 6d  e as the minimum
12546 20 61 6c 6c 6f 77 61 62 6c 65 20 76 61 6c 75 65   allowable value
12547 0a 20 20 2a 2a 20 66 6f 72 20 70 61 67 65 53 69  .  ** for pageSi
12548 7a 65 2e 0a 20 20 2a 2f 0a 20 20 7a 4d 61 73 74  ze..  */.  zMast
12549 65 72 20 3d 20 70 50 61 67 65 72 2d 3e 70 54 6d  er = pPager->pTm
1254a 70 53 70 61 63 65 3b 0a 20 20 72 63 20 3d 20 72  pSpace;.  rc = r
1254b 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  eadMasterJournal
1254c 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d  (pPager->jfd, zM
1254d 61 73 74 65 72 2c 20 70 50 61 67 65 72 2d 3e 70  aster, pPager->p
1254e 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b  Vfs->mxPathname+
1254f 31 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  1);.  if( rc==SQ
12550 4c 49 54 45 5f 4f 4b 20 26 26 20 7a 4d 61 73 74  LITE_OK && zMast
12551 65 72 5b 30 5d 20 29 7b 0a 20 20 20 20 72 63 20  er[0] ){.    rc 
12552 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73  = sqlite3OsAcces
12553 73 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c  s(pVfs, zMaster,
12554 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45   SQLITE_ACCESS_E
12555 58 49 53 54 53 2c 20 26 72 65 73 29 3b 0a 20 20  XISTS, &res);.  
12556 7d 0a 20 20 7a 4d 61 73 74 65 72 20 3d 20 30 3b  }.  zMaster = 0;
12557 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
12558 45 5f 4f 4b 20 7c 7c 20 21 72 65 73 20 29 7b 0a  E_OK || !res ){.
12559 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61      goto end_pla
1255a 79 62 61 63 6b 3b 0a 20 20 7d 0a 20 20 70 50 61  yback;.  }.  pPa
1255b 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20  ger->journalOff 
1255c 3d 20 30 3b 0a 20 20 6e 65 65 64 50 61 67 65 72  = 0;.  needPager
1255d 52 65 73 65 74 20 3d 20 69 73 48 6f 74 3b 0a 0a  Reset = isHot;..
1255e 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20 74    /* This loop t
1255f 65 72 6d 69 6e 61 74 65 73 20 65 69 74 68 65 72  erminates either
12560 20 77 68 65 6e 20 61 20 72 65 61 64 4a 6f 75 72   when a readJour
12561 6e 61 6c 48 64 72 28 29 20 6f 72 20 0a 20 20 2a  nalHdr() or .  *
12562 2a 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b  * pager_playback
12563 5f 6f 6e 65 5f 70 61 67 65 28 29 20 63 61 6c 6c  _one_page() call
12564 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   returns SQLITE_
12565 44 4f 4e 45 20 6f 72 20 61 6e 20 49 4f 20 65 72  DONE or an IO er
12566 72 6f 72 20 0a 20 20 2a 2a 20 6f 63 63 75 72 73  ror .  ** occurs
12567 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28  . .  */.  while(
12568 20 31 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 73   1 ){.    int is
12569 55 6e 73 79 6e 63 20 3d 20 30 3b 0a 0a 20 20 20  Unsync = 0;..   
1256a 20 2f 2a 20 52 65 61 64 20 74 68 65 20 6e 65 78   /* Read the nex
1256b 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  t journal header
1256c 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61   from the journa
1256d 6c 20 66 69 6c 65 2e 20 20 49 66 20 74 68 65 72  l file.  If ther
1256e 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 6e 6f 74  e are.    ** not
1256f 20 65 6e 6f 75 67 68 20 62 79 74 65 73 20 6c 65   enough bytes le
12570 66 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61  ft in the journa
12571 6c 20 66 69 6c 65 20 66 6f 72 20 61 20 63 6f 6d  l file for a com
12572 70 6c 65 74 65 20 68 65 61 64 65 72 2c 20 6f 72  plete header, or
12573 0a 20 20 20 20 2a 2a 20 69 74 20 69 73 20 63 6f  .    ** it is co
12574 72 72 75 70 74 65 64 2c 20 74 68 65 6e 20 61 20  rrupted, then a 
12575 70 72 6f 63 65 73 73 20 6d 75 73 74 20 6f 66 20  process must of 
12576 66 61 69 6c 65 64 20 77 68 69 6c 65 20 77 72 69  failed while wri
12577 74 69 6e 67 20 69 74 2e 0a 20 20 20 20 2a 2a 20  ting it..    ** 
12578 54 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 6e  This indicates n
12579 6f 74 68 69 6e 67 20 6d 6f 72 65 20 6e 65 65 64  othing more need
1257a 73 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62  s to be rolled b
1257b 61 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ack..    */.    
1257c 72 63 20 3d 20 72 65 61 64 4a 6f 75 72 6e 61 6c  rc = readJournal
1257d 48 64 72 28 70 50 61 67 65 72 2c 20 69 73 48 6f  Hdr(pPager, isHo
1257e 74 2c 20 73 7a 4a 2c 20 26 6e 52 65 63 2c 20 26  t, szJ, &nRec, &
1257f 6d 78 50 67 29 3b 0a 20 20 20 20 69 66 28 20 72  mxPg);.    if( r
12580 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 20  c!=SQLITE_OK ){ 
12581 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
12582 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20  QLITE_DONE ){.  
12583 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
12584 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  E_OK;.      }.  
12585 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61      goto end_pla
12586 79 62 61 63 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20  yback;.    }..  
12587 20 20 2f 2a 20 49 66 20 6e 52 65 63 20 69 73 20    /* If nRec is 
12588 30 78 66 66 66 66 66 66 66 66 2c 20 74 68 65 6e  0xffffffff, then
12589 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 77 61   this journal wa
1258a 73 20 63 72 65 61 74 65 64 20 62 79 20 61 20 70  s created by a p
1258b 72 6f 63 65 73 73 0a 20 20 20 20 2a 2a 20 77 6f  rocess.    ** wo
1258c 72 6b 69 6e 67 20 69 6e 20 6e 6f 2d 73 79 6e 63  rking in no-sync
1258d 20 6d 6f 64 65 2e 20 54 68 69 73 20 6d 65 61 6e   mode. This mean
1258e 73 20 74 68 61 74 20 74 68 65 20 72 65 73 74 20  s that the rest 
1258f 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20  of the journal. 
12590 20 20 20 2a 2a 20 66 69 6c 65 20 63 6f 6e 73 69     ** file consi
12591 73 74 73 20 6f 66 20 70 61 67 65 73 2c 20 74 68  sts of pages, th
12592 65 72 65 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20  ere are no more 
12593 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 73 2e  journal headers.
12594 20 43 6f 6d 70 75 74 65 0a 20 20 20 20 2a 2a 20   Compute.    ** 
12595 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6e 52 65  the value of nRe
12596 63 20 62 61 73 65 64 20 6f 6e 20 74 68 69 73 20  c based on this 
12597 61 73 73 75 6d 70 74 69 6f 6e 2e 0a 20 20 20 20  assumption..    
12598 2a 2f 0a 20 20 20 20 69 66 28 20 6e 52 65 63 3d  */.    if( nRec=
12599 3d 30 78 66 66 66 66 66 66 66 66 20 29 7b 0a 20  =0xffffffff ){. 
1259a 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61       assert( pPa
1259b 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d  ger->journalOff=
1259c 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28  =JOURNAL_HDR_SZ(
1259d 70 50 61 67 65 72 29 20 29 3b 0a 20 20 20 20 20  pPager) );.     
1259e 20 6e 52 65 63 20 3d 20 28 69 6e 74 29 28 28 73   nRec = (int)((s
1259f 7a 4a 20 2d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52  zJ - JOURNAL_HDR
125a0 5f 53 5a 28 70 50 61 67 65 72 29 29 2f 4a 4f 55  _SZ(pPager))/JOU
125a1 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65  RNAL_PG_SZ(pPage
125a2 72 29 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  r));.    }..    
125a3 2f 2a 20 49 66 20 6e 52 65 63 20 69 73 20 30 20  /* If nRec is 0 
125a4 61 6e 64 20 74 68 69 73 20 72 6f 6c 6c 62 61 63  and this rollbac
125a5 6b 20 69 73 20 6f 66 20 61 20 74 72 61 6e 73 61  k is of a transa
125a6 63 74 69 6f 6e 20 63 72 65 61 74 65 64 20 62 79  ction created by
125a7 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 70 72 6f   this.    ** pro
125a8 63 65 73 73 20 61 6e 64 20 69 66 20 74 68 69 73  cess and if this
125a9 20 69 73 20 74 68 65 20 66 69 6e 61 6c 20 68 65   is the final he
125aa 61 64 65 72 20 69 6e 20 74 68 65 20 6a 6f 75 72  ader in the jour
125ab 6e 61 6c 2c 20 74 68 65 6e 20 69 74 20 6d 65 61  nal, then it mea
125ac 6e 73 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 74  ns.    ** that t
125ad 68 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 20  his part of the 
125ae 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62 65 69 6e  journal was bein
125af 67 20 66 69 6c 6c 65 64 20 62 75 74 20 68 61 73  g filled but has
125b0 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 0a 20 20   not yet been.  
125b1 20 20 2a 2a 20 73 79 6e 63 65 64 20 74 6f 20 64    ** synced to d
125b2 69 73 6b 2e 20 20 43 6f 6d 70 75 74 65 20 74 68  isk.  Compute th
125b3 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  e number of page
125b4 73 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 72  s based on the r
125b5 65 6d 61 69 6e 69 6e 67 0a 20 20 20 20 2a 2a 20  emaining.    ** 
125b6 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65  size of the file
125b7 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
125b8 54 68 65 20 74 68 69 72 64 20 74 65 72 6d 20 6f  The third term o
125b9 66 20 74 68 65 20 74 65 73 74 20 77 61 73 20 61  f the test was a
125ba 64 64 65 64 20 74 6f 20 66 69 78 20 74 69 63 6b  dded to fix tick
125bb 65 74 20 23 32 35 36 35 2e 0a 20 20 20 20 2a 2a  et #2565..    **
125bc 20 57 68 65 6e 20 72 6f 6c 6c 69 6e 67 20 62 61   When rolling ba
125bd 63 6b 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c  ck a hot journal
125be 2c 20 6e 52 65 63 3d 3d 30 20 61 6c 77 61 79 73  , nRec==0 always
125bf 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
125c0 6e 65 78 74 0a 20 20 20 20 2a 2a 20 63 68 75 6e  next.    ** chun
125c1 6b 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  k of the journal
125c2 20 63 6f 6e 74 61 69 6e 73 20 7a 65 72 6f 20 70   contains zero p
125c3 61 67 65 73 20 74 6f 20 62 65 20 72 6f 6c 6c 65  ages to be rolle
125c4 64 20 62 61 63 6b 2e 20 20 42 75 74 0a 20 20 20  d back.  But.   
125c5 20 2a 2a 20 77 68 65 6e 20 64 6f 69 6e 67 20 61   ** when doing a
125c6 20 52 4f 4c 4c 42 41 43 4b 20 61 6e 64 20 74 68   ROLLBACK and th
125c7 65 20 6e 52 65 63 3d 3d 30 20 63 68 75 6e 6b 20  e nRec==0 chunk 
125c8 69 73 20 74 68 65 20 6c 61 73 74 20 63 68 75 6e  is the last chun
125c9 6b 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20  k in.    ** the 
125ca 6a 6f 75 72 6e 61 6c 2c 20 69 74 20 6d 65 61 6e  journal, it mean
125cb 73 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e  s that the journ
125cc 61 6c 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e  al might contain
125cd 20 61 64 64 69 74 69 6f 6e 61 6c 0a 20 20 20 20   additional.    
125ce 2a 2a 20 70 61 67 65 73 20 74 68 61 74 20 6e 65  ** pages that ne
125cf 65 64 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20  ed to be rolled 
125d0 62 61 63 6b 20 61 6e 64 20 74 68 61 74 20 74 68  back and that th
125d1 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  e number of page
125d2 73 20 0a 20 20 20 20 2a 2a 20 73 68 6f 75 6c 64  s .    ** should
125d3 20 62 65 20 63 6f 6d 70 75 74 65 64 20 62 61 73   be computed bas
125d4 65 64 20 6f 6e 20 74 68 65 20 6a 6f 75 72 6e 61  ed on the journa
125d5 6c 20 66 69 6c 65 20 73 69 7a 65 2e 0a 20 20 20  l file size..   
125d6 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 52 65 63   */.    if( nRec
125d7 3d 3d 30 20 26 26 20 21 69 73 48 6f 74 20 26 26  ==0 && !isHot &&
125d8 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d  .        pPager-
125d9 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52  >journalHdr+JOUR
125da 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65  NAL_HDR_SZ(pPage
125db 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72  r)==pPager->jour
125dc 6e 61 6c 4f 66 66 20 29 7b 0a 20 20 20 20 20 20  nalOff ){.      
125dd 6e 52 65 63 20 3d 20 28 69 6e 74 29 28 28 73 7a  nRec = (int)((sz
125de 4a 20 2d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  J - pPager->jour
125df 6e 61 6c 4f 66 66 29 20 2f 20 4a 4f 55 52 4e 41  nalOff) / JOURNA
125e0 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29 29  L_PG_SZ(pPager))
125e1 3b 0a 20 20 20 20 20 20 69 73 55 6e 73 79 6e 63  ;.      isUnsync
125e2 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20   = 1;.    }..   
125e3 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 74   /* If this is t
125e4 68 65 20 66 69 72 73 74 20 68 65 61 64 65 72 20  he first header 
125e5 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f  read from the jo
125e6 75 72 6e 61 6c 2c 20 74 72 75 6e 63 61 74 65 20  urnal, truncate 
125e7 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62  the.    ** datab
125e8 61 73 65 20 66 69 6c 65 20 62 61 63 6b 20 74 6f  ase file back to
125e9 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 69   its original si
125ea 7a 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ze..    */.    i
125eb 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  f( pPager->journ
125ec 61 6c 4f 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48  alOff==JOURNAL_H
125ed 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 29 7b  DR_SZ(pPager) ){
125ee 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65  .      rc = page
125ef 72 5f 74 72 75 6e 63 61 74 65 28 70 50 61 67 65  r_truncate(pPage
125f0 72 2c 20 6d 78 50 67 29 3b 0a 20 20 20 20 20 20  r, mxPg);.      
125f1 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
125f2 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74  K ){.        got
125f3 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a  o end_playback;.
125f4 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50        }.      pP
125f5 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6d  ager->dbSize = m
125f6 78 50 67 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  xPg;.    }..    
125f7 2f 2a 20 43 6f 70 79 20 6f 72 69 67 69 6e 61 6c  /* Copy original
125f8 20 70 61 67 65 73 20 6f 75 74 20 6f 66 20 74 68   pages out of th
125f9 65 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 62 61  e journal and ba
125fa 63 6b 20 69 6e 74 6f 20 74 68 65 20 0a 20 20 20  ck into the .   
125fb 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c   ** database fil
125fc 65 20 61 6e 64 2f 6f 72 20 70 61 67 65 20 63 61  e and/or page ca
125fd 63 68 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  che..    */.    
125fe 66 6f 72 28 75 3d 30 3b 20 75 3c 6e 52 65 63 3b  for(u=0; u<nRec;
125ff 20 75 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   u++){.      if(
12600 20 6e 65 65 64 50 61 67 65 72 52 65 73 65 74 20   needPagerReset 
12601 29 7b 0a 20 20 20 20 20 20 20 20 70 61 67 65 72  ){.        pager
12602 5f 72 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a  _reset(pPager);.
12603 20 20 20 20 20 20 20 20 6e 65 65 64 50 61 67 65          needPage
12604 72 52 65 73 65 74 20 3d 20 30 3b 0a 20 20 20 20  rReset = 0;.    
12605 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 70    }.      rc = p
12606 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e  ager_playback_on
12607 65 5f 70 61 67 65 28 70 50 61 67 65 72 2c 31 2c  e_page(pPager,1,
12608 69 73 55 6e 73 79 6e 63 2c 26 70 50 61 67 65 72  isUnsync,&pPager
12609 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 30 2c 30  ->journalOff,0,0
1260a 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
1260b 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1260c 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
1260d 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20  LITE_DONE ){.   
1260e 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
1260f 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20  TE_OK;.         
12610 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
12611 4f 66 66 20 3d 20 73 7a 4a 3b 0a 20 20 20 20 20  Off = szJ;.     
12612 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
12613 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
12614 20 20 20 20 20 2f 2a 20 49 66 20 77 65 20 61 72       /* If we ar
12615 65 20 75 6e 61 62 6c 65 20 74 6f 20 72 6f 6c 6c  e unable to roll
12616 62 61 63 6b 2c 20 71 75 69 74 20 61 6e 64 20 72  back, quit and r
12617 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 0a  eturn the error.
12618 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 64            ** cod
12619 65 2e 20 20 54 68 69 73 20 77 69 6c 6c 20 63 61  e.  This will ca
1261a 75 73 65 20 74 68 65 20 70 61 67 65 72 20 74 6f  use the pager to
1261b 20 65 6e 74 65 72 20 74 68 65 20 65 72 72 6f 72   enter the error
1261c 20 73 74 61 74 65 0a 20 20 20 20 20 20 20 20 20   state.         
1261d 20 2a 2a 20 73 6f 20 74 68 61 74 20 6e 6f 20 66   ** so that no f
1261e 75 72 74 68 65 72 20 68 61 72 6d 20 77 69 6c 6c  urther harm will
1261f 20 62 65 20 64 6f 6e 65 2e 20 20 50 65 72 68 61   be done.  Perha
12620 70 73 20 74 68 65 20 6e 65 78 74 0a 20 20 20 20  ps the next.    
12621 20 20 20 20 20 20 2a 2a 20 70 72 6f 63 65 73 73        ** process
12622 20 74 6f 20 63 6f 6d 65 20 61 6c 6f 6e 67 20 77   to come along w
12623 69 6c 6c 20 62 65 20 61 62 6c 65 20 74 6f 20 72  ill be able to r
12624 6f 6c 6c 62 61 63 6b 20 74 68 65 20 64 61 74 61  ollback the data
12625 62 61 73 65 2e 0a 20 20 20 20 20 20 20 20 20 20  base..          
12626 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  */.          got
12627 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a  o end_playback;.
12628 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
12629 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a  }.    }.  }.  /*
1262a 4e 4f 54 52 45 41 43 48 45 44 2a 2f 0a 20 20 61  NOTREACHED*/.  a
1262b 73 73 65 72 74 28 20 30 20 29 3b 0a 0a 65 6e 64  ssert( 0 );..end
1262c 5f 70 6c 61 79 62 61 63 6b 3a 0a 20 20 2f 2a 20  _playback:.  /* 
1262d 46 6f 6c 6c 6f 77 69 6e 67 20 61 20 72 6f 6c 6c  Following a roll
1262e 62 61 63 6b 2c 20 74 68 65 20 64 61 74 61 62 61  back, the databa
1262f 73 65 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62  se file should b
12630 65 20 62 61 63 6b 20 69 6e 20 69 74 73 20 6f 72  e back in its or
12631 69 67 69 6e 61 6c 0a 20 20 2a 2a 20 73 74 61 74  iginal.  ** stat
12632 65 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 73  e prior to the s
12633 74 61 72 74 20 6f 66 20 74 68 65 20 74 72 61 6e  tart of the tran
12634 73 61 63 74 69 6f 6e 2c 20 73 6f 20 69 6e 76 6f  saction, so invo
12635 6b 65 20 74 68 65 0a 20 20 2a 2a 20 53 51 4c 49  ke the.  ** SQLI
12636 54 45 5f 46 43 4e 54 4c 5f 44 42 5f 55 4e 43 48  TE_FCNTL_DB_UNCH
12637 41 4e 47 45 44 20 66 69 6c 65 2d 63 6f 6e 74 72  ANGED file-contr
12638 6f 6c 20 6d 65 74 68 6f 64 20 74 6f 20 64 69 73  ol method to dis
12639 61 62 6c 65 20 74 68 65 0a 20 20 2a 2a 20 61 73  able the.  ** as
1263a 73 65 72 74 69 6f 6e 20 74 68 61 74 20 74 68 65  sertion that the
1263b 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75   transaction cou
1263c 6e 74 65 72 20 77 61 73 20 6d 6f 64 69 66 69 65  nter was modifie
1263d 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  d..  */.  assert
1263e 28 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 66 64  (.    pPager->fd
1263f 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d 30 20 7c 7c  ->pMethods==0 ||
12640 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 46 69  .    sqlite3OsFi
12641 6c 65 43 6f 6e 74 72 6f 6c 28 70 50 61 67 65 72  leControl(pPager
12642 2d 3e 66 64 2c 53 51 4c 49 54 45 5f 46 43 4e 54  ->fd,SQLITE_FCNT
12643 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44 2c 30  L_DB_UNCHANGED,0
12644 29 3e 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 29  )>=SQLITE_OK.  )
12645 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20  ;..  /* If this 
12646 70 6c 61 79 62 61 63 6b 20 69 73 20 68 61 70 70  playback is happ
12647 65 6e 69 6e 67 20 61 75 74 6f 6d 61 74 69 63 61  ening automatica
12648 6c 6c 79 20 61 73 20 61 20 72 65 73 75 6c 74 20  lly as a result 
12649 6f 66 20 61 6e 20 49 4f 20 6f 72 20 0a 20 20 2a  of an IO or .  *
1264a 2a 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 20 74  * malloc error t
1264b 68 61 74 20 6f 63 63 75 72 72 65 64 20 61 66 74  hat occurred aft
1264c 65 72 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f  er the change-co
1264d 75 6e 74 65 72 20 77 61 73 20 75 70 64 61 74 65  unter was update
1264e 64 20 62 75 74 20 0a 20 20 2a 2a 20 62 65 66 6f  d but .  ** befo
1264f 72 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  re the transacti
12650 6f 6e 20 77 61 73 20 63 6f 6d 6d 69 74 74 65 64  on was committed
12651 2c 20 74 68 65 6e 20 74 68 65 20 63 68 61 6e 67  , then the chang
12652 65 2d 63 6f 75 6e 74 65 72 20 0a 20 20 2a 2a 20  e-counter .  ** 
12653 6d 6f 64 69 66 69 63 61 74 69 6f 6e 20 6d 61 79  modification may
12654 20 6a 75 73 74 20 68 61 76 65 20 62 65 65 6e 20   just have been 
12655 72 65 76 65 72 74 65 64 2e 20 49 66 20 74 68 69  reverted. If thi
12656 73 20 68 61 70 70 65 6e 73 20 69 6e 20 65 78 63  s happens in exc
12657 6c 75 73 69 76 65 20 0a 20 20 2a 2a 20 6d 6f 64  lusive .  ** mod
12658 65 2c 20 74 68 65 6e 20 73 75 62 73 65 71 75 65  e, then subseque
12659 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20  nt transactions 
1265a 70 65 72 66 6f 72 6d 65 64 20 62 79 20 74 68 65  performed by the
1265b 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 6c 6c   connection will
1265c 20 6e 6f 74 0a 20 20 2a 2a 20 75 70 64 61 74 65   not.  ** update
1265d 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e   the change-coun
1265e 74 65 72 20 61 74 20 61 6c 6c 2e 20 54 68 69 73  ter at all. This
1265f 20 6d 61 79 20 6c 65 61 64 20 74 6f 20 63 61 63   may lead to cac
12660 68 65 20 69 6e 63 6f 6e 73 69 73 74 65 6e 63 79  he inconsistency
12661 0a 20 20 2a 2a 20 70 72 6f 62 6c 65 6d 73 20 66  .  ** problems f
12662 6f 72 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  or other process
12663 65 73 20 61 74 20 73 6f 6d 65 20 70 6f 69 6e 74  es at some point
12664 20 69 6e 20 74 68 65 20 66 75 74 75 72 65 2e 20   in the future. 
12665 53 6f 2c 20 6a 75 73 74 0a 20 20 2a 2a 20 69 6e  So, just.  ** in
12666 20 63 61 73 65 20 74 68 69 73 20 68 61 73 20 68   case this has h
12667 61 70 70 65 6e 65 64 2c 20 63 6c 65 61 72 20 74  appened, clear t
12668 68 65 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f  he changeCountDo
12669 6e 65 20 66 6c 61 67 20 6e 6f 77 2e 0a 20 20 2a  ne flag now..  *
1266a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e  /.  pPager->chan
1266b 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 70 50  geCountDone = pP
1266c 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a  ager->tempFile;.
1266d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
1266e 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 7a 4d 61 73  E_OK ){.    zMas
1266f 74 65 72 20 3d 20 70 50 61 67 65 72 2d 3e 70 54  ter = pPager->pT
12670 6d 70 53 70 61 63 65 3b 0a 20 20 20 20 72 63 20  mpSpace;.    rc 
12671 3d 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 72  = readMasterJour
12672 6e 61 6c 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  nal(pPager->jfd,
12673 20 7a 4d 61 73 74 65 72 2c 20 70 50 61 67 65 72   zMaster, pPager
12674 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61  ->pVfs->mxPathna
12675 6d 65 2b 31 29 3b 0a 20 20 20 20 74 65 73 74 63  me+1);.    testc
12676 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45 5f  ase( rc!=SQLITE_
12677 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  OK );.  }.  if( 
12678 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
12679 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f  .    rc = pager_
1267a 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28  end_transaction(
1267b 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 5b  pPager, zMaster[
1267c 30 5d 21 3d 27 5c 30 27 29 3b 0a 20 20 20 20 74  0]!='\0');.    t
1267d 65 73 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c  estcase( rc!=SQL
1267e 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20  ITE_OK );.  }.  
1267f 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
12680 4b 20 26 26 20 7a 4d 61 73 74 65 72 5b 30 5d 20  K && zMaster[0] 
12681 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20 2f 2a  && res ){.    /*
12682 20 49 66 20 74 68 65 72 65 20 77 61 73 20 61 20   If there was a 
12683 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 61  master journal a
12684 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  nd this routine 
12685 77 69 6c 6c 20 72 65 74 75 72 6e 20 73 75 63 63  will return succ
12686 65 73 73 2c 0a 20 20 20 20 2a 2a 20 73 65 65 20  ess,.    ** see 
12687 69 66 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c  if it is possibl
12688 65 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 20  e to delete the 
12689 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 0a  master journal..
1268a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20      */.    rc = 
1268b 70 61 67 65 72 5f 64 65 6c 6d 61 73 74 65 72 28  pager_delmaster(
1268c 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 29  pPager, zMaster)
1268d 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
1268e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc!=SQLITE_OK );
1268f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 50  .  }..  /* The P
12690 61 67 65 72 2e 73 65 63 74 6f 72 53 69 7a 65 20  ager.sectorSize 
12691 76 61 72 69 61 62 6c 65 20 6d 61 79 20 68 61 76  variable may hav
12692 65 20 62 65 65 6e 20 75 70 64 61 74 65 64 20 77  e been updated w
12693 68 69 6c 65 20 72 6f 6c 6c 69 6e 67 0a 20 20 2a  hile rolling.  *
12694 2a 20 62 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c  * back a journal
12695 20 63 72 65 61 74 65 64 20 62 79 20 61 20 70 72   created by a pr
12696 6f 63 65 73 73 20 77 69 74 68 20 61 20 64 69 66  ocess with a dif
12697 66 65 72 65 6e 74 20 73 65 63 74 6f 72 20 73 69  ferent sector si
12698 7a 65 0a 20 20 2a 2a 20 76 61 6c 75 65 2e 20 52  ze.  ** value. R
12699 65 73 65 74 20 69 74 20 74 6f 20 74 68 65 20 63  eset it to the c
1269a 6f 72 72 65 63 74 20 76 61 6c 75 65 20 66 6f 72  orrect value for
1269b 20 74 68 69 73 20 70 72 6f 63 65 73 73 2e 0a 20   this process.. 
1269c 20 2a 2f 0a 20 20 73 65 74 53 65 63 74 6f 72 53   */.  setSectorS
1269d 69 7a 65 28 70 50 61 67 65 72 29 3b 0a 20 20 72  ize(pPager);.  r
1269e 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
1269f 2a 2a 20 50 6c 61 79 62 61 63 6b 20 73 61 76 65  ** Playback save
126a0 70 6f 69 6e 74 20 70 53 61 76 65 70 6f 69 6e 74  point pSavepoint
126a1 2e 20 4f 72 2c 20 69 66 20 70 53 61 76 65 70 6f  . Or, if pSavepo
126a2 69 6e 74 3d 3d 4e 55 4c 4c 2c 20 74 68 65 6e 20  int==NULL, then 
126a3 70 6c 61 79 62 61 63 6b 0a 2a 2a 20 74 68 65 20  playback.** the 
126a4 65 6e 74 69 72 65 20 6d 61 73 74 65 72 20 6a 6f  entire master jo
126a5 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 65 20  urnal file. The 
126a6 63 61 73 65 20 70 53 61 76 65 70 6f 69 6e 74 3d  case pSavepoint=
126a7 3d 4e 55 4c 4c 20 6f 63 63 75 72 73 20 77 68 65  =NULL occurs whe
126a8 6e 20 0a 2a 2a 20 61 20 52 4f 4c 4c 42 41 43 4b  n .** a ROLLBACK
126a9 20 54 4f 20 63 6f 6d 6d 61 6e 64 20 69 73 20 69   TO command is i
126aa 6e 76 6f 6b 65 64 20 6f 6e 20 61 20 53 41 56 45  nvoked on a SAVE
126ab 50 4f 49 4e 54 20 74 68 61 74 20 69 73 20 61 20  POINT that is a 
126ac 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20  transaction .** 
126ad 73 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 0a 2a 2a  savepoint..**.**
126ae 20 57 68 65 6e 20 70 53 61 76 65 70 6f 69 6e 74   When pSavepoint
126af 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 28 6d 65   is not NULL (me
126b0 61 6e 69 6e 67 20 61 20 6e 6f 6e 2d 74 72 61 6e  aning a non-tran
126b1 73 61 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e  saction savepoin
126b2 74 20 69 73 20 0a 2a 2a 20 62 65 69 6e 67 20 72  t is .** being r
126b3 6f 6c 6c 65 64 20 62 61 63 6b 29 2c 20 74 68 65  olled back), the
126b4 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 63  n the rollback c
126b5 6f 6e 73 69 73 74 73 20 6f 66 20 75 70 20 74 6f  onsists of up to
126b6 20 74 68 72 65 65 20 73 74 61 67 65 73 2c 0a 2a   three stages,.*
126b7 2a 20 70 65 72 66 6f 72 6d 65 64 20 69 6e 20 74  * performed in t
126b8 68 65 20 6f 72 64 65 72 20 73 70 65 63 69 66 69  he order specifi
126b9 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 50 61  ed:.**.**   * Pa
126ba 67 65 73 20 61 72 65 20 70 6c 61 79 65 64 20 62  ges are played b
126bb 61 63 6b 20 66 72 6f 6d 20 74 68 65 20 6d 61 69  ack from the mai
126bc 6e 20 6a 6f 75 72 6e 61 6c 20 73 74 61 72 74 69  n journal starti
126bd 6e 67 20 61 74 20 62 79 74 65 0a 2a 2a 20 20 20  ng at byte.**   
126be 20 20 6f 66 66 73 65 74 20 50 61 67 65 72 53 61    offset PagerSa
126bf 76 65 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20  vepoint.iOffset 
126c0 61 6e 64 20 63 6f 6e 74 69 6e 75 69 6e 67 20 74  and continuing t
126c1 6f 20 0a 2a 2a 20 20 20 20 20 50 61 67 65 72 53  o .**     PagerS
126c2 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66  avepoint.iHdrOff
126c3 73 65 74 2c 20 6f 72 20 74 6f 20 74 68 65 20 65  set, or to the e
126c4 6e 64 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a  nd of the main j
126c5 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 66 69  ournal.**     fi
126c6 6c 65 20 69 66 20 50 61 67 65 72 53 61 76 65 70  le if PagerSavep
126c7 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 20  oint.iHdrOffset 
126c8 69 73 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 20  is zero..**.**  
126c9 20 2a 20 49 66 20 50 61 67 65 72 53 61 76 65 70   * If PagerSavep
126ca 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 20  oint.iHdrOffset 
126cb 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65  is not zero, the
126cc 6e 20 70 61 67 65 73 20 61 72 65 20 70 6c 61 79  n pages are play
126cd 65 64 0a 2a 2a 20 20 20 20 20 62 61 63 6b 20 73  ed.**     back s
126ce 74 61 72 74 69 6e 67 20 66 72 6f 6d 20 74 68 65  tarting from the
126cf 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20   journal header 
126d0 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c  immediately foll
126d1 6f 77 69 6e 67 20 0a 2a 2a 20 20 20 20 20 50 61  owing .**     Pa
126d2 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64  gerSavepoint.iHd
126d3 72 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 65  rOffset to the e
126d4 6e 64 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a  nd of the main j
126d5 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2a 0a  ournal file..**.
126d6 2a 2a 20 20 20 2a 20 50 61 67 65 73 20 61 72 65  **   * Pages are
126d7 20 74 68 65 6e 20 70 6c 61 79 65 64 20 62 61 63   then played bac
126d8 6b 20 66 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a  k from the sub-j
126d9 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 73 74 61  ournal file, sta
126da 72 74 69 6e 67 0a 2a 2a 20 20 20 20 20 77 69 74  rting.**     wit
126db 68 20 74 68 65 20 50 61 67 65 72 53 61 76 65 70  h the PagerSavep
126dc 6f 69 6e 74 2e 69 53 75 62 52 65 63 20 61 6e 64  oint.iSubRec and
126dd 20 63 6f 6e 74 69 6e 75 69 6e 67 20 74 6f 20 74   continuing to t
126de 68 65 20 65 6e 64 20 6f 66 0a 2a 2a 20 20 20 20  he end of.**    
126df 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
126e0 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 72 6f 75 67 68  e..**.** Through
126e1 6f 75 74 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b  out the rollback
126e2 20 70 72 6f 63 65 73 73 2c 20 65 61 63 68 20 74   process, each t
126e3 69 6d 65 20 61 20 70 61 67 65 20 69 73 20 72 6f  ime a page is ro
126e4 6c 6c 65 64 20 62 61 63 6b 2c 20 74 68 65 0a 2a  lled back, the.*
126e5 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  * corresponding 
126e6 62 69 74 20 69 73 20 73 65 74 20 69 6e 20 61 20  bit is set in a 
126e7 62 69 74 76 65 63 20 73 74 72 75 63 74 75 72 65  bitvec structure
126e8 20 28 76 61 72 69 61 62 6c 65 20 70 44 6f 6e 65   (variable pDone
126e9 20 69 6e 20 74 68 65 0a 2a 2a 20 69 6d 70 6c 65   in the.** imple
126ea 6d 65 6e 74 61 74 69 6f 6e 20 62 65 6c 6f 77 29  mentation below)
126eb 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74  . This is used t
126ec 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 61 20  o ensure that a 
126ed 70 61 67 65 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20  page is only.** 
126ee 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 68 65 20  rolled back the 
126ef 66 69 72 73 74 20 74 69 6d 65 20 69 74 20 69 73  first time it is
126f0 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 69 6e 20   encountered in 
126f1 65 69 74 68 65 72 20 6a 6f 75 72 6e 61 6c 2e 0a  either journal..
126f2 2a 2a 0a 2a 2a 20 49 66 20 70 53 61 76 65 70 6f  **.** If pSavepo
126f3 69 6e 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65  int is NULL, the
126f4 6e 20 70 61 67 65 73 20 61 72 65 20 6f 6e 6c 79  n pages are only
126f5 20 70 6c 61 79 65 64 20 62 61 63 6b 20 66 72 6f   played back fro
126f6 6d 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20 6a 6f  m the main.** jo
126f7 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 65 72  urnal file. Ther
126f8 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 66 6f 72  e is no need for
126f9 20 61 20 62 69 74 76 65 63 20 69 6e 20 74 68 69   a bitvec in thi
126fa 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e  s case..**.** In
126fb 20 65 69 74 68 65 72 20 63 61 73 65 2c 20 62 65   either case, be
126fc 66 6f 72 65 20 70 6c 61 79 62 61 63 6b 20 63 6f  fore playback co
126fd 6d 6d 65 6e 63 65 73 20 74 68 65 20 50 61 67 65  mmences the Page
126fe 72 2e 64 62 53 69 7a 65 20 76 61 72 69 61 62 6c  r.dbSize variabl
126ff 65 0a 2a 2a 20 69 73 20 72 65 73 65 74 20 74 6f  e.** is reset to
12700 20 74 68 65 20 76 61 6c 75 65 20 74 68 61 74 20   the value that 
12701 69 74 20 68 65 6c 64 20 61 74 20 74 68 65 20 73  it held at the s
12702 74 61 72 74 20 6f 66 20 74 68 65 20 73 61 76 65  tart of the save
12703 70 6f 69 6e 74 20 0a 2a 2a 20 28 6f 72 20 74 72  point .** (or tr
12704 61 6e 73 61 63 74 69 6f 6e 29 2e 20 4e 6f 20 70  ansaction). No p
12705 61 67 65 20 77 69 74 68 20 61 20 70 61 67 65 2d  age with a page-
12706 6e 75 6d 62 65 72 20 67 72 65 61 74 65 72 20 74  number greater t
12707 68 61 6e 20 74 68 69 73 20 76 61 6c 75 65 0a 2a  han this value.*
12708 2a 20 69 73 20 70 6c 61 79 65 64 20 62 61 63 6b  * is played back
12709 2e 20 49 66 20 6f 6e 65 20 69 73 20 65 6e 63 6f  . If one is enco
1270a 75 6e 74 65 72 65 64 20 69 74 20 69 73 20 73 69  untered it is si
1270b 6d 70 6c 79 20 73 6b 69 70 70 65 64 2e 0a 2a 2f  mply skipped..*/
1270c 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65  .static int page
1270d 72 50 6c 61 79 62 61 63 6b 53 61 76 65 70 6f 69  rPlaybackSavepoi
1270e 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  nt(Pager *pPager
1270f 2c 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74  , PagerSavepoint
12710 20 2a 70 53 61 76 65 70 6f 69 6e 74 29 7b 0a 20   *pSavepoint){. 
12711 20 69 36 34 20 73 7a 4a 3b 20 20 20 20 20 20 20   i64 szJ;       
12712 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 66 66            /* Eff
12713 65 63 74 69 76 65 20 73 69 7a 65 20 6f 66 20 74  ective size of t
12714 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20  he main journal 
12715 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 4f 66 66  */.  i64 iHdrOff
12716 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
12717 20 45 6e 64 20 6f 66 20 66 69 72 73 74 20 73 65   End of first se
12718 67 6d 65 6e 74 20 6f 66 20 6d 61 69 6e 2d 6a 6f  gment of main-jo
12719 75 72 6e 61 6c 20 72 65 63 6f 72 64 73 20 2a 2f  urnal records */
1271a 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
1271b 54 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a 20 52  TE_OK;      /* R
1271c 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
1271d 42 69 74 76 65 63 20 2a 70 44 6f 6e 65 20 3d 20  Bitvec *pDone = 
1271e 30 3b 20 20 20 20 20 20 20 2f 2a 20 42 69 74 76  0;       /* Bitv
1271f 65 63 20 74 6f 20 65 6e 73 75 72 65 20 70 61 67  ec to ensure pag
12720 65 73 20 70 6c 61 79 65 64 20 62 61 63 6b 20 6f  es played back o
12721 6e 6c 79 20 6f 6e 63 65 20 2a 2f 0a 0a 20 20 61  nly once */..  a
12722 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
12723 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 52  tate>=PAGER_SHAR
12724 45 44 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f  ED );..  /* Allo
12725 63 61 74 65 20 61 20 62 69 74 76 65 63 20 74 6f  cate a bitvec to
12726 20 75 73 65 20 74 6f 20 73 74 6f 72 65 20 74 68   use to store th
12727 65 20 73 65 74 20 6f 66 20 70 61 67 65 73 20 72  e set of pages r
12728 6f 6c 6c 65 64 20 62 61 63 6b 20 2a 2f 0a 20 20  olled back */.  
12729 69 66 28 20 70 53 61 76 65 70 6f 69 6e 74 20 29  if( pSavepoint )
1272a 7b 0a 20 20 20 20 70 44 6f 6e 65 20 3d 20 73 71  {.    pDone = sq
1272b 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74  lite3BitvecCreat
1272c 65 28 70 53 61 76 65 70 6f 69 6e 74 2d 3e 6e 4f  e(pSavepoint->nO
1272d 72 69 67 29 3b 0a 20 20 20 20 69 66 28 20 21 70  rig);.    if( !p
1272e 44 6f 6e 65 20 29 7b 0a 20 20 20 20 20 20 72 65  Done ){.      re
1272f 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
12730 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  M;.    }.  }..  
12731 2f 2a 20 53 65 74 20 74 68 65 20 64 61 74 61 62  /* Set the datab
12732 61 73 65 20 73 69 7a 65 20 62 61 63 6b 20 74 6f  ase size back to
12733 20 74 68 65 20 76 61 6c 75 65 20 69 74 20 77 61   the value it wa
12734 73 20 62 65 66 6f 72 65 20 74 68 65 20 73 61 76  s before the sav
12735 65 70 6f 69 6e 74 20 0a 20 20 2a 2a 20 62 65 69  epoint .  ** bei
12736 6e 67 20 72 65 76 65 72 74 65 64 20 77 61 73 20  ng reverted was 
12737 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 70  opened..  */.  p
12738 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20  Pager->dbSize = 
12739 70 53 61 76 65 70 6f 69 6e 74 20 3f 20 70 53 61  pSavepoint ? pSa
1273a 76 65 70 6f 69 6e 74 2d 3e 6e 4f 72 69 67 20 3a  vepoint->nOrig :
1273b 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53   pPager->dbOrigS
1273c 69 7a 65 3b 0a 0a 20 20 2f 2a 20 55 73 65 20 70  ize;..  /* Use p
1273d 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
1273e 66 20 61 73 20 74 68 65 20 65 66 66 65 63 74 69  f as the effecti
1273f 76 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6d  ve size of the m
12740 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a  ain rollback.  *
12741 2a 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 20  * journal.  The 
12742 61 63 74 75 61 6c 20 66 69 6c 65 20 6d 69 67 68  actual file migh
12743 74 20 62 65 20 6c 61 72 67 65 72 20 74 68 61 6e  t be larger than
12744 20 74 68 69 73 20 69 6e 0a 20 20 2a 2a 20 50 41   this in.  ** PA
12745 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
12746 54 52 55 4e 43 41 54 45 20 6f 72 20 50 41 47 45  TRUNCATE or PAGE
12747 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45  R_JOURNALMODE_PE
12748 52 53 49 53 54 2e 20 20 42 75 74 20 61 6e 79 74  RSIST.  But anyt
12749 68 69 6e 67 0a 20 20 2a 2a 20 70 61 73 74 20 70  hing.  ** past p
1274a 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
1274b 66 20 69 73 20 6f 66 66 2d 6c 69 6d 69 74 73 20  f is off-limits 
1274c 74 6f 20 75 73 2e 0a 20 20 2a 2f 0a 20 20 73 7a  to us..  */.  sz
1274d 4a 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  J = pPager->jour
1274e 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f 2a 20 42 65  nalOff;..  /* Be
1274f 67 69 6e 20 62 79 20 72 6f 6c 6c 69 6e 67 20 62  gin by rolling b
12750 61 63 6b 20 72 65 63 6f 72 64 73 20 66 72 6f 6d  ack records from
12751 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61   the main journa
12752 6c 20 73 74 61 72 74 69 6e 67 20 61 74 0a 20 20  l starting at.  
12753 2a 2a 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e  ** PagerSavepoin
12754 74 2e 69 4f 66 66 73 65 74 20 61 6e 64 20 63 6f  t.iOffset and co
12755 6e 74 69 6e 75 69 6e 67 20 74 6f 20 74 68 65 20  ntinuing to the 
12756 6e 65 78 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61  next journal hea
12757 64 65 72 2e 0a 20 20 2a 2a 20 54 68 65 72 65 20  der..  ** There 
12758 6d 69 67 68 74 20 62 65 20 72 65 63 6f 72 64 73  might be records
12759 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75   in the main jou
1275a 72 6e 61 6c 20 74 68 61 74 20 68 61 76 65 20 61  rnal that have a
1275b 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a   page number.  *
1275c 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74  * greater than t
1275d 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 61 62  he current datab
1275e 61 73 65 20 73 69 7a 65 20 28 70 50 61 67 65 72  ase size (pPager
1275f 2d 3e 64 62 53 69 7a 65 29 20 62 75 74 20 74 68  ->dbSize) but th
12760 6f 73 65 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 65  ose.  ** will be
12761 20 73 6b 69 70 70 65 64 20 61 75 74 6f 6d 61 74   skipped automat
12762 69 63 61 6c 6c 79 2e 20 20 50 61 67 65 73 20 61  ically.  Pages a
12763 72 65 20 61 64 64 65 64 20 74 6f 20 70 44 6f 6e  re added to pDon
12764 65 20 61 73 20 74 68 65 79 0a 20 20 2a 2a 20 61  e as they.  ** a
12765 72 65 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 0a  re played back..
12766 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 61 76 65    */.  if( pSave
12767 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 69 48 64  point ){.    iHd
12768 72 4f 66 66 20 3d 20 70 53 61 76 65 70 6f 69 6e  rOff = pSavepoin
12769 74 2d 3e 69 48 64 72 4f 66 66 73 65 74 20 3f 20  t->iHdrOffset ? 
1276a 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69 48 64 72  pSavepoint->iHdr
1276b 4f 66 66 73 65 74 20 3a 20 73 7a 4a 3b 0a 20 20  Offset : szJ;.  
1276c 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
1276d 6c 4f 66 66 20 3d 20 70 53 61 76 65 70 6f 69 6e  lOff = pSavepoin
1276e 74 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 20 20  t->iOffset;.    
1276f 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54  while( rc==SQLIT
12770 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e  E_OK && pPager->
12771 6a 6f 75 72 6e 61 6c 4f 66 66 3c 69 48 64 72 4f  journalOff<iHdrO
12772 66 66 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  ff ){.      rc =
12773 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f   pager_playback_
12774 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65 72 2c  one_page(pPager,
12775 20 31 2c 20 30 2c 20 26 70 50 61 67 65 72 2d 3e   1, 0, &pPager->
12776 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 31 2c 20 70  journalOff, 1, p
12777 44 6f 6e 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20  Done);.    }.   
12778 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c   assert( rc!=SQL
12779 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d 65  ITE_DONE );.  }e
1277a 6c 73 65 7b 0a 20 20 20 20 70 50 61 67 65 72 2d  lse{.    pPager-
1277b 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b  >journalOff = 0;
1277c 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 74 69  .  }..  /* Conti
1277d 6e 75 65 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b  nue rolling back
1277e 20 72 65 63 6f 72 64 73 20 6f 75 74 20 6f 66 20   records out of 
1277f 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c  the main journal
12780 20 73 74 61 72 74 69 6e 67 20 61 74 0a 20 20 2a   starting at.  *
12781 2a 20 74 68 65 20 66 69 72 73 74 20 6a 6f 75 72  * the first jour
12782 6e 61 6c 20 68 65 61 64 65 72 20 73 65 65 6e 20  nal header seen 
12783 61 6e 64 20 63 6f 6e 74 69 6e 75 69 6e 67 20 75  and continuing u
12784 6e 74 69 6c 20 74 68 65 20 65 66 66 65 63 74 69  ntil the effecti
12785 76 65 20 65 6e 64 0a 20 20 2a 2a 20 6f 66 20 74  ve end.  ** of t
12786 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20  he main journal 
12787 66 69 6c 65 2e 20 20 43 6f 6e 74 69 6e 75 65 20  file.  Continue 
12788 74 6f 20 73 6b 69 70 20 6f 75 74 2d 6f 66 2d 72  to skip out-of-r
12789 61 6e 67 65 20 70 61 67 65 73 20 61 6e 64 0a 20  ange pages and. 
1278a 20 2a 2a 20 63 6f 6e 74 69 6e 75 65 20 61 64 64   ** continue add
1278b 69 6e 67 20 70 61 67 65 73 20 72 6f 6c 6c 65 64  ing pages rolled
1278c 20 62 61 63 6b 20 74 6f 20 70 44 6f 6e 65 2e 0a   back to pDone..
1278d 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 72 63    */.  while( rc
1278e 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70  ==SQLITE_OK && p
1278f 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
12790 66 3c 73 7a 4a 20 29 7b 0a 20 20 20 20 75 33 32  f<szJ ){.    u32
12791 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20   ii;            
12792 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
12793 2a 2f 0a 20 20 20 20 75 33 32 20 6e 4a 52 65 63  */.    u32 nJRec
12794 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4e 75 6d   = 0;     /* Num
12795 62 65 72 20 6f 66 20 4a 6f 75 72 6e 61 6c 20 52  ber of Journal R
12796 65 63 6f 72 64 73 20 2a 2f 0a 20 20 20 20 75 33  ecords */.    u3
12797 32 20 64 75 6d 6d 79 3b 0a 20 20 20 20 72 63 20  2 dummy;.    rc 
12798 3d 20 72 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72  = readJournalHdr
12799 28 70 50 61 67 65 72 2c 20 30 2c 20 73 7a 4a 2c  (pPager, 0, szJ,
1279a 20 26 6e 4a 52 65 63 2c 20 26 64 75 6d 6d 79 29   &nJRec, &dummy)
1279b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63  ;.    assert( rc
1279c 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b  !=SQLITE_DONE );
1279d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20  ..    /*.    ** 
1279e 54 68 65 20 22 70 50 61 67 65 72 2d 3e 6a 6f 75  The "pPager->jou
1279f 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e 41 4c 5f  rnalHdr+JOURNAL_
127a0 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3d 3d  HDR_SZ(pPager)==
127a1 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
127a2 66 66 22 0a 20 20 20 20 2a 2a 20 74 65 73 74 20  ff".    ** test 
127a3 69 73 20 72 65 6c 61 74 65 64 20 74 6f 20 74 69  is related to ti
127a4 63 6b 65 74 20 23 32 35 36 35 2e 20 20 53 65 65  cket #2565.  See
127a5 20 74 68 65 20 64 69 73 63 75 73 73 69 6f 6e 20   the discussion 
127a6 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 70 61  in the.    ** pa
127a7 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 20 66  ger_playback() f
127a8 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 64 64 69  unction for addi
127a9 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69  tional informati
127aa 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  on..    */.    i
127ab 66 28 20 6e 4a 52 65 63 3d 3d 30 20 0a 20 20 20  f( nJRec==0 .   
127ac 20 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75    && pPager->jou
127ad 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e 41 4c 5f  rnalHdr+JOURNAL_
127ae 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3d 3d  HDR_SZ(pPager)==
127af 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
127b0 66 66 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  ff.    ){.      
127b1 6e 4a 52 65 63 20 3d 20 28 75 33 32 29 28 28 73  nJRec = (u32)((s
127b2 7a 4a 20 2d 20 70 50 61 67 65 72 2d 3e 6a 6f 75  zJ - pPager->jou
127b3 72 6e 61 6c 4f 66 66 29 2f 4a 4f 55 52 4e 41 4c  rnalOff)/JOURNAL
127b4 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29 29 3b  _PG_SZ(pPager));
127b5 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69  .    }.    for(i
127b6 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f  i=0; rc==SQLITE_
127b7 4f 4b 20 26 26 20 69 69 3c 6e 4a 52 65 63 20 26  OK && ii<nJRec &
127b8 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  & pPager->journa
127b9 6c 4f 66 66 3c 73 7a 4a 3b 20 69 69 2b 2b 29 7b  lOff<szJ; ii++){
127ba 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65  .      rc = page
127bb 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70  r_playback_one_p
127bc 61 67 65 28 70 50 61 67 65 72 2c 20 31 2c 20 30  age(pPager, 1, 0
127bd 2c 20 26 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  , &pPager->journ
127be 61 6c 4f 66 66 2c 20 31 2c 20 70 44 6f 6e 65 29  alOff, 1, pDone)
127bf 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  ;.    }.    asse
127c0 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44  rt( rc!=SQLITE_D
127c1 4f 4e 45 20 29 3b 0a 20 20 7d 0a 20 20 61 73 73  ONE );.  }.  ass
127c2 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f  ert( rc!=SQLITE_
127c3 4f 4b 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f  OK || pPager->jo
127c4 75 72 6e 61 6c 4f 66 66 3d 3d 73 7a 4a 20 29 3b  urnalOff==szJ );
127c5 0a 0a 20 20 2f 2a 20 46 69 6e 61 6c 6c 79 2c 20  ..  /* Finally, 
127c6 20 72 6f 6c 6c 62 61 63 6b 20 70 61 67 65 73 20   rollback pages 
127c7 66 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75  from the sub-jou
127c8 72 6e 61 6c 2e 20 20 50 61 67 65 20 74 68 61 74  rnal.  Page that
127c9 20 77 65 72 65 0a 20 20 2a 2a 20 70 72 65 76 69   were.  ** previ
127ca 6f 75 73 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63  ously rolled bac
127cb 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 6d 61 69  k out of the mai
127cc 6e 20 6a 6f 75 72 6e 61 6c 20 28 61 6e 64 20 61  n journal (and a
127cd 72 65 20 68 65 6e 63 65 20 69 6e 20 70 44 6f 6e  re hence in pDon
127ce 65 29 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20  e).  ** will be 
127cf 73 6b 69 70 70 65 64 2e 20 20 4f 75 74 2d 6f 66  skipped.  Out-of
127d0 2d 72 61 6e 67 65 20 70 61 67 65 73 20 61 72 65  -range pages are
127d1 20 61 6c 73 6f 20 73 6b 69 70 70 65 64 2e 0a 20   also skipped.. 
127d2 20 2a 2f 0a 20 20 69 66 28 20 70 53 61 76 65 70   */.  if( pSavep
127d3 6f 69 6e 74 20 29 7b 0a 20 20 20 20 75 33 32 20  oint ){.    u32 
127d4 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ii;            /
127d5 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
127d6 2f 0a 20 20 20 20 69 36 34 20 6f 66 66 73 65 74  /.    i64 offset
127d7 20 3d 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69   = pSavepoint->i
127d8 53 75 62 52 65 63 2a 28 34 2b 70 50 61 67 65 72  SubRec*(4+pPager
127d9 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20  ->pageSize);.   
127da 20 66 6f 72 28 69 69 3d 70 53 61 76 65 70 6f 69   for(ii=pSavepoi
127db 6e 74 2d 3e 69 53 75 62 52 65 63 3b 20 72 63 3d  nt->iSubRec; rc=
127dc 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69  =SQLITE_OK && ii
127dd 3c 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63  <pPager->nSubRec
127de 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61  ; ii++){.      a
127df 73 73 65 72 74 28 20 6f 66 66 73 65 74 3d 3d 69  ssert( offset==i
127e0 69 2a 28 34 2b 70 50 61 67 65 72 2d 3e 70 61 67  i*(4+pPager->pag
127e1 65 53 69 7a 65 29 20 29 3b 0a 20 20 20 20 20 20  eSize) );.      
127e2 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62  rc = pager_playb
127e3 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61  ack_one_page(pPa
127e4 67 65 72 2c 20 30 2c 20 30 2c 20 26 6f 66 66 73  ger, 0, 0, &offs
127e5 65 74 2c 20 31 2c 20 70 44 6f 6e 65 29 3b 0a 20  et, 1, pDone);. 
127e6 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
127e7 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45   rc!=SQLITE_DONE
127e8 20 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74   );.  }..  sqlit
127e9 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28  e3BitvecDestroy(
127ea 70 44 6f 6e 65 29 3b 0a 20 20 69 66 28 20 72 63  pDone);.  if( rc
127eb 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
127ec 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e     pPager->journ
127ed 61 6c 4f 66 66 20 3d 20 73 7a 4a 3b 0a 20 20 7d  alOff = szJ;.  }
127ee 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
127ef 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
127f0 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  e maximum number
127f1 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61   of in-memory pa
127f2 67 65 73 20 74 68 61 74 20 61 72 65 20 61 6c 6c  ges that are all
127f3 6f 77 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  owed..*/.SQLITE_
127f4 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
127f5 69 74 65 33 50 61 67 65 72 53 65 74 43 61 63 68  ite3PagerSetCach
127f6 65 73 69 7a 65 28 50 61 67 65 72 20 2a 70 50 61  esize(Pager *pPa
127f7 67 65 72 2c 20 69 6e 74 20 6d 78 50 61 67 65 29  ger, int mxPage)
127f8 7b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68  {.  sqlite3Pcach
127f9 65 53 65 74 43 61 63 68 65 73 69 7a 65 28 70 50  eSetCachesize(pP
127fa 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 6d  ager->pPCache, m
127fb 78 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  xPage);.}../*.**
127fc 20 41 64 6a 75 73 74 20 74 68 65 20 72 6f 62 75   Adjust the robu
127fd 73 74 6e 65 73 73 20 6f 66 20 74 68 65 20 64 61  stness of the da
127fe 74 61 62 61 73 65 20 74 6f 20 64 61 6d 61 67 65  tabase to damage
127ff 20 64 75 65 20 74 6f 20 4f 53 20 63 72 61 73 68   due to OS crash
12800 65 73 0a 2a 2a 20 6f 72 20 70 6f 77 65 72 20 66  es.** or power f
12801 61 69 6c 75 72 65 73 20 62 79 20 63 68 61 6e 67  ailures by chang
12802 69 6e 67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ing the number o
12803 66 20 73 79 6e 63 73 28 29 73 20 77 68 65 6e 20  f syncs()s when 
12804 77 72 69 74 69 6e 67 0a 2a 2a 20 74 68 65 20 72  writing.** the r
12805 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e  ollback journal.
12806 20 20 54 68 65 72 65 20 61 72 65 20 74 68 72 65    There are thre
12807 65 20 6c 65 76 65 6c 73 3a 0a 2a 2a 0a 2a 2a 20  e levels:.**.** 
12808 20 20 20 4f 46 46 20 20 20 20 20 20 20 73 71 6c     OFF       sql
12809 69 74 65 33 4f 73 53 79 6e 63 28 29 20 69 73 20  ite3OsSync() is 
1280a 6e 65 76 65 72 20 63 61 6c 6c 65 64 2e 20 20 54  never called.  T
1280b 68 69 73 20 69 73 20 74 68 65 20 64 65 66 61 75  his is the defau
1280c 6c 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  lt.**           
1280d 20 20 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79     for temporary
1280e 20 61 6e 64 20 74 72 61 6e 73 69 65 6e 74 20 66   and transient f
1280f 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 4e  iles..**.**    N
12810 4f 52 4d 41 4c 20 20 20 20 54 68 65 20 6a 6f 75  ORMAL    The jou
12811 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 20 6f  rnal is synced o
12812 6e 63 65 20 62 65 66 6f 72 65 20 77 72 69 74 65  nce before write
12813 73 20 62 65 67 69 6e 20 6f 6e 20 74 68 65 0a 2a  s begin on the.*
12814 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64  *              d
12815 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 69  atabase.  This i
12816 73 20 6e 6f 72 6d 61 6c 6c 79 20 61 64 65 71 75  s normally adequ
12817 61 74 65 20 70 72 6f 74 65 63 74 69 6f 6e 2c 20  ate protection, 
12818 62 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  but.**          
12819 20 20 20 20 69 74 20 69 73 20 74 68 65 6f 72 65      it is theore
1281a 74 69 63 61 6c 6c 79 20 70 6f 73 73 69 62 6c 65  tically possible
1281b 2c 20 74 68 6f 75 67 68 20 76 65 72 79 20 75 6e  , though very un
1281c 6c 69 6b 65 6c 79 2c 0a 2a 2a 20 20 20 20 20 20  likely,.**      
1281d 20 20 20 20 20 20 20 20 74 68 61 74 20 61 6e 20          that an 
1281e 69 6e 6f 70 65 72 74 75 6e 65 20 70 6f 77 65 72  inopertune power
1281f 20 66 61 69 6c 75 72 65 20 63 6f 75 6c 64 20 6c   failure could l
12820 65 61 76 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c  eave the journal
12821 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
12822 20 69 6e 20 61 20 73 74 61 74 65 20 77 68 69 63   in a state whic
12823 68 20 77 6f 75 6c 64 20 63 61 75 73 65 20 64 61  h would cause da
12824 6d 61 67 65 20 74 6f 20 74 68 65 20 64 61 74 61  mage to the data
12825 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  base.**         
12826 20 20 20 20 20 77 68 65 6e 20 69 74 20 69 73 20       when it is 
12827 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a  rolled back..**.
12828 2a 2a 20 20 20 20 46 55 4c 4c 20 20 20 20 20 20  **    FULL      
12829 54 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73  The journal is s
1282a 79 6e 63 65 64 20 74 77 69 63 65 20 62 65 66 6f  ynced twice befo
1282b 72 65 20 77 72 69 74 65 73 20 62 65 67 69 6e 20  re writes begin 
1282c 6f 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  on the.**       
1282d 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65 20         database 
1282e 28 77 69 74 68 20 73 6f 6d 65 20 61 64 64 69 74  (with some addit
1282f 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
12830 6e 20 2d 20 74 68 65 20 6e 52 65 63 20 66 69 65  n - the nRec fie
12831 6c 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ld.**           
12832 20 20 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61     of the journa
12833 6c 20 68 65 61 64 65 72 20 2d 20 62 65 69 6e 67  l header - being
12834 20 77 72 69 74 74 65 6e 20 69 6e 20 62 65 74 77   written in betw
12835 65 65 6e 20 74 68 65 20 74 77 6f 0a 2a 2a 20 20  een the two.**  
12836 20 20 20 20 20 20 20 20 20 20 20 20 73 79 6e 63              sync
12837 73 29 2e 20 20 49 66 20 77 65 20 61 73 73 75 6d  s).  If we assum
12838 65 20 74 68 61 74 20 77 72 69 74 69 6e 67 20 61  e that writing a
12839 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
1283a 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 65 63   single disk sec
1283b 74 6f 72 20 69 73 20 61 74 6f 6d 69 63 2c 20 74  tor is atomic, t
1283c 68 65 6e 20 74 68 69 73 20 6d 6f 64 65 20 70 72  hen this mode pr
1283d 6f 76 69 64 65 73 0a 2a 2a 20 20 20 20 20 20 20  ovides.**       
1283e 20 20 20 20 20 20 20 61 73 73 75 72 61 6e 63 65         assurance
1283f 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61   that the journa
12840 6c 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 63 6f  l will not be co
12841 72 72 75 70 74 65 64 20 74 6f 20 74 68 65 0a 2a  rrupted to the.*
12842 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  *              p
12843 6f 69 6e 74 20 6f 66 20 63 61 75 73 69 6e 67 20  oint of causing 
12844 64 61 6d 61 67 65 20 74 6f 20 74 68 65 20 64 61  damage to the da
12845 74 61 62 61 73 65 20 64 75 72 69 6e 67 20 72 6f  tabase during ro
12846 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 4e 75  llback..**.** Nu
12847 6d 65 72 69 63 20 76 61 6c 75 65 73 20 61 73 73  meric values ass
12848 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
12849 73 65 20 73 74 61 74 65 73 20 61 72 65 20 4f 46  se states are OF
1284a 46 3d 3d 31 2c 20 4e 4f 52 4d 41 4c 3d 32 2c 0a  F==1, NORMAL=2,.
1284b 2a 2a 20 61 6e 64 20 46 55 4c 4c 3d 33 2e 0a 2a  ** and FULL=3..*
1284c 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
1284d 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47  _OMIT_PAGER_PRAG
1284e 4d 41 53 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  MAS.SQLITE_PRIVA
1284f 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
12850 61 67 65 72 53 65 74 53 61 66 65 74 79 4c 65 76  agerSetSafetyLev
12851 65 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  el(Pager *pPager
12852 2c 20 69 6e 74 20 6c 65 76 65 6c 2c 20 69 6e 74  , int level, int
12853 20 62 46 75 6c 6c 46 73 79 6e 63 29 7b 0a 20 20   bFullFsync){.  
12854 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 3d  pPager->noSync =
12855 20 20 28 6c 65 76 65 6c 3d 3d 31 20 7c 7c 20 70    (level==1 || p
12856 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29  Pager->tempFile)
12857 20 3f 31 3a 30 3b 0a 20 20 70 50 61 67 65 72 2d   ?1:0;.  pPager-
12858 3e 66 75 6c 6c 53 79 6e 63 20 3d 20 28 6c 65 76  >fullSync = (lev
12859 65 6c 3d 3d 33 20 26 26 20 21 70 50 61 67 65 72  el==3 && !pPager
1285a 2d 3e 74 65 6d 70 46 69 6c 65 29 20 3f 31 3a 30  ->tempFile) ?1:0
1285b 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 79 6e 63  ;.  pPager->sync
1285c 5f 66 6c 61 67 73 20 3d 20 28 62 46 75 6c 6c 46  _flags = (bFullF
1285d 73 79 6e 63 3f 53 51 4c 49 54 45 5f 53 59 4e 43  sync?SQLITE_SYNC
1285e 5f 46 55 4c 4c 3a 53 51 4c 49 54 45 5f 53 59 4e  _FULL:SQLITE_SYN
1285f 43 5f 4e 4f 52 4d 41 4c 29 3b 0a 20 20 69 66 28  C_NORMAL);.  if(
12860 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20   pPager->noSync 
12861 29 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79  ) pPager->needSy
12862 6e 63 20 3d 20 30 3b 0a 7d 0a 23 65 6e 64 69 66  nc = 0;.}.#endif
12863 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
12864 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72  owing global var
12865 69 61 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65  iable is increme
12866 6e 74 65 64 20 77 68 65 6e 65 76 65 72 20 74 68  nted whenever th
12867 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 61 74 74  e library.** att
12868 65 6d 70 74 73 20 74 6f 20 6f 70 65 6e 20 61 20  empts to open a 
12869 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e 20  temporary file. 
1286a 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f   This informatio
1286b 6e 20 69 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a  n is used for.**
1286c 20 74 65 73 74 69 6e 67 20 61 6e 64 20 61 6e 61   testing and ana
1286d 6c 79 73 69 73 20 6f 6e 6c 79 2e 20 20 0a 2a 2f  lysis only.  .*/
1286e 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
1286f 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  EST.SQLITE_API i
12870 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 74  nt sqlite3_opent
12871 65 6d 70 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23  emp_count = 0;.#
12872 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65  endif../*.** Ope
12873 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69  n a temporary fi
12874 6c 65 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20  le..**.** Write 
12875 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  the file descrip
12876 74 6f 72 20 69 6e 74 6f 20 2a 70 46 69 6c 65 2e  tor into *pFile.
12877 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
12878 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 0a 2a 2a  K on success .**
12879 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 65   or some other e
1287a 72 72 6f 72 20 63 6f 64 65 20 69 66 20 77 65 20  rror code if we 
1287b 66 61 69 6c 2e 20 54 68 65 20 4f 53 20 77 69 6c  fail. The OS wil
1287c 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  l automatically 
1287d 0a 2a 2a 20 64 65 6c 65 74 65 20 74 68 65 20 74  .** delete the t
1287e 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 77 68  emporary file wh
1287f 65 6e 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e  en it is closed.
12880 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73  .**.** The flags
12881 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 56   passed to the V
12882 46 53 20 6c 61 79 65 72 20 78 4f 70 65 6e 28 29  FS layer xOpen()
12883 20 63 61 6c 6c 20 61 72 65 20 74 68 6f 73 65 20   call are those 
12884 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20  specified.** by 
12885 70 61 72 61 6d 65 74 65 72 20 76 66 73 46 6c 61  parameter vfsFla
12886 67 73 20 4f 52 65 64 20 77 69 74 68 20 74 68 65  gs ORed with the
12887 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a   following:.**.*
12888 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45  *     SQLITE_OPE
12889 4e 5f 52 45 41 44 57 52 49 54 45 0a 2a 2a 20 20  N_READWRITE.**  
1288a 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43     SQLITE_OPEN_C
1288b 52 45 41 54 45 0a 2a 2a 20 20 20 20 20 53 51 4c  REATE.**     SQL
1288c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49  ITE_OPEN_EXCLUSI
1288d 56 45 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45  VE.**     SQLITE
1288e 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c  _OPEN_DELETEONCL
1288f 4f 53 45 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  OSE.*/.static in
12890 74 20 70 61 67 65 72 4f 70 65 6e 74 65 6d 70 28  t pagerOpentemp(
12891 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
12892 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ,        /* The 
12893 70 61 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a  pager object */.
12894 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
12895 70 46 69 6c 65 2c 20 20 2f 2a 20 57 72 69 74 65  pFile,  /* Write
12896 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   the file descri
12897 70 74 6f 72 20 68 65 72 65 20 2a 2f 0a 20 20 69  ptor here */.  i
12898 6e 74 20 76 66 73 46 6c 61 67 73 20 20 20 20 20  nt vfsFlags     
12899 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61       /* Flags pa
1289a 73 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20  ssed through to 
1289b 74 68 65 20 56 46 53 20 2a 2f 0a 29 7b 0a 20 20  the VFS */.){.  
1289c 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
1289d 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
1289e 63 6f 64 65 20 2a 2f 0a 0a 23 69 66 64 65 66 20  code */..#ifdef 
1289f 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 73 71  SQLITE_TEST.  sq
128a0 6c 69 74 65 33 5f 6f 70 65 6e 74 65 6d 70 5f 63  lite3_opentemp_c
128a1 6f 75 6e 74 2b 2b 3b 20 20 2f 2a 20 55 73 65 64  ount++;  /* Used
128a2 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64   for testing and
128a3 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 20 2a   analysis only *
128a4 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 76 66 73 46  /.#endif..  vfsF
128a5 6c 61 67 73 20 7c 3d 20 20 53 51 4c 49 54 45 5f  lags |=  SQLITE_
128a6 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 7c  OPEN_READWRITE |
128a7 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45   SQLITE_OPEN_CRE
128a8 41 54 45 20 7c 0a 20 20 20 20 20 20 20 20 20 20  ATE |.          
128a9 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58    SQLITE_OPEN_EX
128aa 43 4c 55 53 49 56 45 20 7c 20 53 51 4c 49 54 45  CLUSIVE | SQLITE
128ab 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c  _OPEN_DELETEONCL
128ac 4f 53 45 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  OSE;.  rc = sqli
128ad 74 65 33 4f 73 4f 70 65 6e 28 70 50 61 67 65 72  te3OsOpen(pPager
128ae 2d 3e 70 56 66 73 2c 20 30 2c 20 70 46 69 6c 65  ->pVfs, 0, pFile
128af 2c 20 76 66 73 46 6c 61 67 73 2c 20 30 29 3b 0a  , vfsFlags, 0);.
128b0 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51    assert( rc!=SQ
128b1 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70 65  LITE_OK || isOpe
128b2 6e 28 70 46 69 6c 65 29 20 29 3b 0a 20 20 72 65  n(pFile) );.  re
128b3 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
128b4 2a 20 53 65 74 20 74 68 65 20 62 75 73 79 20 68  * Set the busy h
128b5 61 6e 64 6c 65 72 20 66 75 6e 63 74 69 6f 6e 2e  andler function.
128b6 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72  .**.** The pager
128b7 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 62 75 73   invokes the bus
128b8 79 2d 68 61 6e 64 6c 65 72 20 69 66 20 73 71 6c  y-handler if sql
128b9 69 74 65 33 4f 73 4c 6f 63 6b 28 29 20 72 65 74  ite3OsLock() ret
128ba 75 72 6e 73 20 0a 2a 2a 20 53 51 4c 49 54 45 5f  urns .** SQLITE_
128bb 42 55 53 59 20 77 68 65 6e 20 74 72 79 69 6e 67  BUSY when trying
128bc 20 74 6f 20 75 70 67 72 61 64 65 20 66 72 6f 6d   to upgrade from
128bd 20 6e 6f 2d 6c 6f 63 6b 20 74 6f 20 61 20 53 48   no-lock to a SH
128be 41 52 45 44 20 6c 6f 63 6b 2c 0a 2a 2a 20 6f 72  ARED lock,.** or
128bf 20 77 68 65 6e 20 74 72 79 69 6e 67 20 74 6f 20   when trying to 
128c0 75 70 67 72 61 64 65 20 66 72 6f 6d 20 61 20 52  upgrade from a R
128c1 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 74 6f 20  ESERVED lock to 
128c2 61 6e 20 45 58 43 4c 55 53 49 56 45 20 0a 2a 2a  an EXCLUSIVE .**
128c3 20 6c 6f 63 6b 2e 20 49 74 20 64 6f 65 73 20 2a   lock. It does *
128c4 6e 6f 74 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20  not* invoke the 
128c5 62 75 73 79 20 68 61 6e 64 6c 65 72 20 77 68 65  busy handler whe
128c6 6e 20 75 70 67 72 61 64 69 6e 67 20 66 72 6f 6d  n upgrading from
128c7 0a 2a 2a 20 53 48 41 52 45 44 20 74 6f 20 52 45  .** SHARED to RE
128c8 53 45 52 56 45 44 2c 20 6f 72 20 77 68 65 6e 20  SERVED, or when 
128c9 75 70 67 72 61 64 69 6e 67 20 66 72 6f 6d 20 53  upgrading from S
128ca 48 41 52 45 44 20 74 6f 20 45 58 43 4c 55 53 49  HARED to EXCLUSI
128cb 56 45 0a 2a 2a 20 28 77 68 69 63 68 20 6f 63 63  VE.** (which occ
128cc 75 72 73 20 64 75 72 69 6e 67 20 68 6f 74 2d 6a  urs during hot-j
128cd 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 29  ournal rollback)
128ce 2e 20 53 75 6d 6d 61 72 79 3a 0a 2a 2a 0a 2a 2a  . Summary:.**.**
128cf 20 20 20 54 72 61 6e 73 69 74 69 6f 6e 20 20 20     Transition   
128d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
128d1 20 20 20 20 20 7c 20 49 6e 76 6f 6b 65 73 20 78       | Invokes x
128d2 42 75 73 79 48 61 6e 64 6c 65 72 0a 2a 2a 20 20  BusyHandler.**  
128d3 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
128d4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
128d5 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
128d6 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 4e  ---------.**   N
128d7 4f 5f 4c 4f 43 4b 20 20 20 20 20 20 20 2d 3e 20  O_LOCK       -> 
128d8 53 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 20 20  SHARED_LOCK     
128d9 20 7c 20 59 65 73 0a 2a 2a 20 20 20 53 48 41 52   | Yes.**   SHAR
128da 45 44 5f 4c 4f 43 4b 20 20 20 2d 3e 20 52 45 53  ED_LOCK   -> RES
128db 45 52 56 45 44 5f 4c 4f 43 4b 20 20 20 20 7c 20  ERVED_LOCK    | 
128dc 4e 6f 0a 2a 2a 20 20 20 53 48 41 52 45 44 5f 4c  No.**   SHARED_L
128dd 4f 43 4b 20 20 20 2d 3e 20 45 58 43 4c 55 53 49  OCK   -> EXCLUSI
128de 56 45 5f 4c 4f 43 4b 20 20 20 7c 20 4e 6f 0a 2a  VE_LOCK   | No.*
128df 2a 20 20 20 52 45 53 45 52 56 45 44 5f 4c 4f 43  *   RESERVED_LOC
128e0 4b 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 5f 4c  K -> EXCLUSIVE_L
128e1 4f 43 4b 20 20 20 7c 20 59 65 73 0a 2a 2a 0a 2a  OCK   | Yes.**.*
128e2 2a 20 49 66 20 74 68 65 20 62 75 73 79 2d 68 61  * If the busy-ha
128e3 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b 20 72  ndler callback r
128e4 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c  eturns non-zero,
128e5 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 0a 2a 2a   the lock is .**
128e6 20 72 65 74 72 69 65 64 2e 20 49 66 20 69 74 20   retried. If it 
128e7 72 65 74 75 72 6e 73 20 7a 65 72 6f 2c 20 74 68  returns zero, th
128e8 65 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 42 55  en the SQLITE_BU
128e9 53 59 20 65 72 72 6f 72 20 69 73 0a 2a 2a 20 72  SY error is.** r
128ea 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63  eturned to the c
128eb 61 6c 6c 65 72 20 6f 66 20 74 68 65 20 70 61 67  aller of the pag
128ec 65 72 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e  er API function.
128ed 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
128ee 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
128ef 61 67 65 72 53 65 74 42 75 73 79 68 61 6e 64 6c  agerSetBusyhandl
128f0 65 72 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61  er(.  Pager *pPa
128f1 67 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  ger,            
128f2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
128f3 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20  ger object */.  
128f4 69 6e 74 20 28 2a 78 42 75 73 79 48 61 6e 64 6c  int (*xBusyHandl
128f5 65 72 29 28 76 6f 69 64 20 2a 29 2c 20 20 20 20  er)(void *),    
128f6 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
128f7 74 6f 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20  to busy-handler 
128f8 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f  function */.  vo
128f9 69 64 20 2a 70 42 75 73 79 48 61 6e 64 6c 65 72  id *pBusyHandler
128fa 41 72 67 20 20 20 20 20 20 20 20 20 20 20 20 20  Arg             
128fb 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74     /* Argument t
128fc 6f 20 70 61 73 73 20 74 6f 20 78 42 75 73 79 48  o pass to xBusyH
128fd 61 6e 64 6c 65 72 20 2a 2f 0a 29 7b 20 20 0a 20  andler */.){  . 
128fe 20 70 50 61 67 65 72 2d 3e 78 42 75 73 79 48 61   pPager->xBusyHa
128ff 6e 64 6c 65 72 20 3d 20 78 42 75 73 79 48 61 6e  ndler = xBusyHan
12900 64 6c 65 72 3b 0a 20 20 70 50 61 67 65 72 2d 3e  dler;.  pPager->
12901 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67 20  pBusyHandlerArg 
12902 3d 20 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72  = pBusyHandlerAr
12903 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 70 6f  g;.}../*.** Repo
12904 72 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 70  rt the current p
12905 61 67 65 20 73 69 7a 65 20 61 6e 64 20 6e 75 6d  age size and num
12906 62 65 72 20 6f 66 20 72 65 73 65 72 76 65 64 20  ber of reserved 
12907 62 79 74 65 73 20 62 61 63 6b 0a 2a 2a 20 74 6f  bytes back.** to
12908 20 74 68 65 20 63 6f 64 65 63 2e 0a 2a 2f 0a 23   the codec..*/.#
12909 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53  ifdef SQLITE_HAS
1290a 5f 43 4f 44 45 43 0a 73 74 61 74 69 63 20 76 6f  _CODEC.static vo
1290b 69 64 20 70 61 67 65 72 52 65 70 6f 72 74 53 69  id pagerReportSi
1290c 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ze(Pager *pPager
1290d 29 7b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  ){.  if( pPager-
1290e 3e 78 43 6f 64 65 63 53 69 7a 65 43 68 6e 67 20  >xCodecSizeChng 
1290f 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 78  ){.    pPager->x
12910 43 6f 64 65 63 53 69 7a 65 43 68 6e 67 28 70 50  CodecSizeChng(pP
12911 61 67 65 72 2d 3e 70 43 6f 64 65 63 2c 20 70 50  ager->pCodec, pP
12912 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 0a  ager->pageSize,.
12913 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12914 20 20 20 20 20 20 20 20 20 20 20 28 69 6e 74 29             (int)
12915 70 50 61 67 65 72 2d 3e 6e 52 65 73 65 72 76 65  pPager->nReserve
12916 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 23  );.  }.}.#else.#
12917 20 64 65 66 69 6e 65 20 70 61 67 65 72 52 65 70   define pagerRep
12918 6f 72 74 53 69 7a 65 28 58 29 20 20 20 20 20 2f  ortSize(X)     /
12919 2a 20 4e 6f 2d 6f 70 20 69 66 20 77 65 20 64 6f  * No-op if we do
1291a 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 61 20 63   not support a c
1291b 6f 64 65 63 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a  odec */.#endif..
1291c 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  /*.** Change the
1291d 20 70 61 67 65 20 73 69 7a 65 20 75 73 65 64 20   page size used 
1291e 62 79 20 74 68 65 20 50 61 67 65 72 20 6f 62 6a  by the Pager obj
1291f 65 63 74 2e 20 54 68 65 20 6e 65 77 20 70 61 67  ect. The new pag
12920 65 20 73 69 7a 65 20 0a 2a 2a 20 69 73 20 70 61  e size .** is pa
12921 73 73 65 64 20 69 6e 20 2a 70 50 61 67 65 53 69  ssed in *pPageSi
12922 7a 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ze..**.** If the
12923 20 70 61 67 65 72 20 69 73 20 69 6e 20 74 68 65   pager is in the
12924 20 65 72 72 6f 72 20 73 74 61 74 65 20 77 68 65   error state whe
12925 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
12926 69 73 20 63 61 6c 6c 65 64 2c 20 69 74 0a 2a 2a  is called, it.**
12927 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 54 68 65   is a no-op. The
12928 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
12929 69 73 20 74 68 65 20 65 72 72 6f 72 20 73 74 61  is the error sta
1292a 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 28 69  te error code (i
1292b 2e 65 2e 20 0a 2a 2a 20 6f 6e 65 20 6f 66 20 53  .e. .** one of S
1292c 51 4c 49 54 45 5f 49 4f 45 52 52 2c 20 53 51 4c  QLITE_IOERR, SQL
1292d 49 54 45 5f 43 4f 52 52 55 50 54 20 6f 72 20 53  ITE_CORRUPT or S
1292e 51 4c 49 54 45 5f 46 55 4c 4c 29 2e 0a 2a 2a 0a  QLITE_FULL)..**.
1292f 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66  ** Otherwise, if
12930 20 61 6c 6c 20 6f 66 20 74 68 65 20 66 6f 6c 6c   all of the foll
12931 6f 77 69 6e 67 20 61 72 65 20 74 72 75 65 3a 0a  owing are true:.
12932 2a 2a 0a 2a 2a 20 20 20 2a 20 74 68 65 20 6e 65  **.**   * the ne
12933 77 20 70 61 67 65 20 73 69 7a 65 20 28 76 61 6c  w page size (val
12934 75 65 20 6f 66 20 2a 70 50 61 67 65 53 69 7a 65  ue of *pPageSize
12935 29 20 69 73 20 76 61 6c 69 64 20 28 61 20 70 6f  ) is valid (a po
12936 77 65 72 20 0a 2a 2a 20 20 20 20 20 6f 66 20 74  wer .**     of t
12937 77 6f 20 62 65 74 77 65 65 6e 20 35 31 32 20 61  wo between 512 a
12938 6e 64 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41  nd SQLITE_MAX_PA
12939 47 45 5f 53 49 5a 45 2c 20 69 6e 63 6c 75 73 69  GE_SIZE, inclusi
1293a 76 65 29 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20  ve), and.**.**  
1293b 20 2a 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20   * there are no 
1293c 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65  outstanding page
1293d 20 72 65 66 65 72 65 6e 63 65 73 2c 20 61 6e 64   references, and
1293e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 74 68 65 20 64  .**.**   * the d
1293f 61 74 61 62 61 73 65 20 69 73 20 65 69 74 68 65  atabase is eithe
12940 72 20 6e 6f 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f  r not an in-memo
12941 72 79 20 64 61 74 61 62 61 73 65 20 6f 72 20 69  ry database or i
12942 74 20 69 73 0a 2a 2a 20 20 20 20 20 61 6e 20 69  t is.**     an i
12943 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73  n-memory databas
12944 65 20 74 68 61 74 20 63 75 72 72 65 6e 74 6c 79  e that currently
12945 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 7a 65 72   consists of zer
12946 6f 20 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 74  o pages..**.** t
12947 68 65 6e 20 74 68 65 20 70 61 67 65 72 20 6f 62  hen the pager ob
12948 6a 65 63 74 20 70 61 67 65 20 73 69 7a 65 20 69  ject page size i
12949 73 20 73 65 74 20 74 6f 20 2a 70 50 61 67 65 53  s set to *pPageS
1294a 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ize..**.** If th
1294b 65 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 63  e page size is c
1294c 68 61 6e 67 65 64 2c 20 74 68 65 6e 20 74 68 69  hanged, then thi
1294d 73 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 73 20  s function uses 
1294e 73 71 6c 69 74 65 33 50 61 67 65 72 4d 61 6c 6c  sqlite3PagerMall
1294f 6f 63 28 29 20 0a 2a 2a 20 74 6f 20 6f 62 74 61  oc() .** to obta
12950 69 6e 20 61 20 6e 65 77 20 50 61 67 65 72 2e 70  in a new Pager.p
12951 54 6d 70 53 70 61 63 65 20 62 75 66 66 65 72 2e  TmpSpace buffer.
12952 20 49 66 20 74 68 69 73 20 61 6c 6c 6f 63 61 74   If this allocat
12953 69 6f 6e 20 61 74 74 65 6d 70 74 20 0a 2a 2a 20  ion attempt .** 
12954 66 61 69 6c 73 2c 20 53 51 4c 49 54 45 5f 4e 4f  fails, SQLITE_NO
12955 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 20  MEM is returned 
12956 61 6e 64 20 74 68 65 20 70 61 67 65 20 73 69 7a  and the page siz
12957 65 20 72 65 6d 61 69 6e 73 20 75 6e 63 68 61 6e  e remains unchan
12958 67 65 64 2e 20 0a 2a 2a 20 49 6e 20 61 6c 6c 20  ged. .** In all 
12959 6f 74 68 65 72 20 63 61 73 65 73 2c 20 53 51 4c  other cases, SQL
1295a 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
1295b 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ed..**.** If the
1295c 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 6e 6f   page size is no
1295d 74 20 63 68 61 6e 67 65 64 2c 20 65 69 74 68 65  t changed, eithe
1295e 72 20 62 65 63 61 75 73 65 20 6f 6e 65 20 6f 66  r because one of
1295f 20 74 68 65 20 65 6e 75 6d 65 72 61 74 65 64 0a   the enumerated.
12960 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 62  ** conditions ab
12961 6f 76 65 20 69 73 20 6e 6f 74 20 74 72 75 65 2c  ove is not true,
12962 20 74 68 65 20 70 61 67 65 72 20 77 61 73 20 69   the pager was i
12963 6e 20 65 72 72 6f 72 20 73 74 61 74 65 20 77 68  n error state wh
12964 65 6e 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74  en this.** funct
12965 69 6f 6e 20 77 61 73 20 63 61 6c 6c 65 64 2c 20  ion was called, 
12966 6f 72 20 62 65 63 61 75 73 65 20 74 68 65 20 6d  or because the m
12967 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
12968 20 61 74 74 65 6d 70 74 20 66 61 69 6c 65 64 2c   attempt failed,
12969 20 0a 2a 2a 20 74 68 65 6e 20 2a 70 50 61 67 65   .** then *pPage
1296a 53 69 7a 65 20 69 73 20 73 65 74 20 74 6f 20 74  Size is set to t
1296b 68 65 20 6f 6c 64 2c 20 72 65 74 61 69 6e 65 64  he old, retained
1296c 20 70 61 67 65 20 73 69 7a 65 20 62 65 66 6f 72   page size befor
1296d 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a  e returning..*/.
1296e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1296f 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53  nt sqlite3PagerS
12970 65 74 50 61 67 65 73 69 7a 65 28 50 61 67 65 72  etPagesize(Pager
12971 20 2a 70 50 61 67 65 72 2c 20 75 31 36 20 2a 70   *pPager, u16 *p
12972 50 61 67 65 53 69 7a 65 2c 20 69 6e 74 20 6e 52  PageSize, int nR
12973 65 73 65 72 76 65 29 7b 0a 20 20 69 6e 74 20 72  eserve){.  int r
12974 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 43  c = pPager->errC
12975 6f 64 65 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d  ode;..  if( rc==
12976 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
12977 20 75 31 36 20 70 61 67 65 53 69 7a 65 20 3d 20   u16 pageSize = 
12978 2a 70 50 61 67 65 53 69 7a 65 3b 0a 20 20 20 20  *pPageSize;.    
12979 61 73 73 65 72 74 28 20 70 61 67 65 53 69 7a 65  assert( pageSize
1297a 3d 3d 30 20 7c 7c 20 28 70 61 67 65 53 69 7a 65  ==0 || (pageSize
1297b 3e 3d 35 31 32 20 26 26 20 70 61 67 65 53 69 7a  >=512 && pageSiz
1297c 65 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41  e<=SQLITE_MAX_PA
1297d 47 45 5f 53 49 5a 45 29 20 29 3b 0a 20 20 20 20  GE_SIZE) );.    
1297e 69 66 28 20 28 70 50 61 67 65 72 2d 3e 6d 65 6d  if( (pPager->mem
1297f 44 62 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d  Db==0 || pPager-
12980 3e 64 62 53 69 7a 65 3d 3d 30 29 0a 20 20 20 20  >dbSize==0).    
12981 20 26 26 20 73 71 6c 69 74 65 33 50 63 61 63 68   && sqlite3Pcach
12982 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72  eRefCount(pPager
12983 2d 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 0a 20  ->pPCache)==0 . 
12984 20 20 20 20 26 26 20 70 61 67 65 53 69 7a 65 20      && pageSize 
12985 26 26 20 70 61 67 65 53 69 7a 65 21 3d 70 50 61  && pageSize!=pPa
12986 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 0a 20  ger->pageSize . 
12987 20 20 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72     ){.      char
12988 20 2a 70 4e 65 77 20 3d 20 28 63 68 61 72 20 2a   *pNew = (char *
12989 29 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c  )sqlite3PageMall
1298a 6f 63 28 70 61 67 65 53 69 7a 65 29 3b 0a 20 20  oc(pageSize);.  
1298b 20 20 20 20 69 66 28 20 21 70 4e 65 77 20 29 7b      if( !pNew ){
1298c 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
1298d 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
1298e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1298f 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50 61   pager_reset(pPa
12990 67 65 72 29 3b 0a 20 20 20 20 20 20 20 20 70 50  ger);.        pP
12991 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 3d  ager->pageSize =
12992 20 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 20   pageSize;.     
12993 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 46 72     sqlite3PageFr
12994 65 65 28 70 50 61 67 65 72 2d 3e 70 54 6d 70 53  ee(pPager->pTmpS
12995 70 61 63 65 29 3b 0a 20 20 20 20 20 20 20 20 70  pace);.        p
12996 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65  Pager->pTmpSpace
12997 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 20 20   = pNew;.       
12998 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 65   sqlite3PcacheSe
12999 74 50 61 67 65 53 69 7a 65 28 70 50 61 67 65 72  tPageSize(pPager
1299a 2d 3e 70 50 43 61 63 68 65 2c 20 70 61 67 65 53  ->pPCache, pageS
1299b 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ize);.      }.  
1299c 20 20 7d 0a 20 20 20 20 2a 70 50 61 67 65 53 69    }.    *pPageSi
1299d 7a 65 20 3d 20 28 75 31 36 29 70 50 61 67 65 72  ze = (u16)pPager
1299e 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20  ->pageSize;.    
1299f 69 66 28 20 6e 52 65 73 65 72 76 65 3c 30 20 29  if( nReserve<0 )
129a0 20 6e 52 65 73 65 72 76 65 20 3d 20 70 50 61 67   nReserve = pPag
129a1 65 72 2d 3e 6e 52 65 73 65 72 76 65 3b 0a 20 20  er->nReserve;.  
129a2 20 20 61 73 73 65 72 74 28 20 6e 52 65 73 65 72    assert( nReser
129a3 76 65 3e 3d 30 20 26 26 20 6e 52 65 73 65 72 76  ve>=0 && nReserv
129a4 65 3c 31 30 30 30 20 29 3b 0a 20 20 20 20 70 50  e<1000 );.    pP
129a5 61 67 65 72 2d 3e 6e 52 65 73 65 72 76 65 20 3d  ager->nReserve =
129a6 20 28 69 31 36 29 6e 52 65 73 65 72 76 65 3b 0a   (i16)nReserve;.
129a7 20 20 20 20 70 61 67 65 72 52 65 70 6f 72 74 53      pagerReportS
129a8 69 7a 65 28 70 50 61 67 65 72 29 3b 0a 20 20 7d  ize(pPager);.  }
129a9 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
129aa 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  ./*.** Return a 
129ab 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 22  pointer to the "
129ac 74 65 6d 70 6f 72 61 72 79 20 70 61 67 65 22 20  temporary page" 
129ad 62 75 66 66 65 72 20 68 65 6c 64 20 69 6e 74 65  buffer held inte
129ae 72 6e 61 6c 6c 79 0a 2a 2a 20 62 79 20 74 68 65  rnally.** by the
129af 20 70 61 67 65 72 2e 20 20 54 68 69 73 20 69 73   pager.  This is
129b0 20 61 20 62 75 66 66 65 72 20 74 68 61 74 20 69   a buffer that i
129b1 73 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20  s big enough to 
129b2 68 6f 6c 64 20 74 68 65 0a 2a 2a 20 65 6e 74 69  hold the.** enti
129b3 72 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20  re content of a 
129b4 64 61 74 61 62 61 73 65 20 70 61 67 65 2e 20 20  database page.  
129b5 54 68 69 73 20 62 75 66 66 65 72 20 69 73 20 75  This buffer is u
129b6 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a  sed internally.*
129b7 2a 20 64 75 72 69 6e 67 20 72 6f 6c 6c 62 61 63  * during rollbac
129b8 6b 20 61 6e 64 20 77 69 6c 6c 20 62 65 20 6f 76  k and will be ov
129b9 65 72 77 72 69 74 74 65 6e 20 77 68 65 6e 65 76  erwritten whenev
129ba 65 72 20 61 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a  er a rollback.**
129bb 20 6f 63 63 75 72 73 2e 20 20 42 75 74 20 6f 74   occurs.  But ot
129bc 68 65 72 20 6d 6f 64 75 6c 65 73 20 61 72 65 20  her modules are 
129bd 66 72 65 65 20 74 6f 20 75 73 65 20 69 74 20 74  free to use it t
129be 6f 6f 2c 20 61 73 20 6c 6f 6e 67 20 61 73 0a 2a  oo, as long as.*
129bf 2a 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 73 20 61  * no rollbacks a
129c0 72 65 20 68 61 70 70 65 6e 69 6e 67 2e 0a 2a 2f  re happening..*/
129c1 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
129c2 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67  void *sqlite3Pag
129c3 65 72 54 65 6d 70 53 70 61 63 65 28 50 61 67 65  erTempSpace(Page
129c4 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65  r *pPager){.  re
129c5 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 70 54 6d  turn pPager->pTm
129c6 70 53 70 61 63 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pSpace;.}../*.**
129c7 20 41 74 74 65 6d 70 74 20 74 6f 20 73 65 74 20   Attempt to set 
129c8 74 68 65 20 6d 61 78 69 6d 75 6d 20 64 61 74 61  the maximum data
129c9 62 61 73 65 20 70 61 67 65 20 63 6f 75 6e 74 20  base page count 
129ca 69 66 20 6d 78 50 61 67 65 20 69 73 20 70 6f 73  if mxPage is pos
129cb 69 74 69 76 65 2e 20 0a 2a 2a 20 4d 61 6b 65 20  itive. .** Make 
129cc 6e 6f 20 63 68 61 6e 67 65 73 20 69 66 20 6d 78  no changes if mx
129cd 50 61 67 65 20 69 73 20 7a 65 72 6f 20 6f 72 20  Page is zero or 
129ce 6e 65 67 61 74 69 76 65 2e 20 20 41 6e 64 20 6e  negative.  And n
129cf 65 76 65 72 20 72 65 64 75 63 65 20 74 68 65 0a  ever reduce the.
129d0 2a 2a 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20  ** maximum page 
129d1 63 6f 75 6e 74 20 62 65 6c 6f 77 20 74 68 65 20  count below the 
129d2 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20  current size of 
129d3 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a  the database..**
129d4 0a 2a 2a 20 52 65 67 61 72 64 6c 65 73 73 20 6f  .** Regardless o
129d5 66 20 6d 78 50 61 67 65 2c 20 72 65 74 75 72 6e  f mxPage, return
129d6 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 61 78   the current max
129d7 69 6d 75 6d 20 70 61 67 65 20 63 6f 75 6e 74 2e  imum page count.
129d8 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
129d9 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
129da 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 28  gerMaxPageCount(
129db 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69  Pager *pPager, i
129dc 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 69 66  nt mxPage){.  if
129dd 28 20 6d 78 50 61 67 65 3e 30 20 29 7b 0a 20 20  ( mxPage>0 ){.  
129de 20 20 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f    pPager->mxPgno
129df 20 3d 20 6d 78 50 61 67 65 3b 0a 20 20 7d 0a 20   = mxPage;.  }. 
129e0 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
129e1 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 30  ecount(pPager, 0
129e2 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67  );.  return pPag
129e3 65 72 2d 3e 6d 78 50 67 6e 6f 3b 0a 7d 0a 0a 2f  er->mxPgno;.}../
129e4 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
129e5 6e 67 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e  ng set of routin
129e6 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 64  es are used to d
129e7 69 73 61 62 6c 65 20 74 68 65 20 73 69 6d 75 6c  isable the simul
129e8 61 74 65 64 0a 2a 2a 20 49 2f 4f 20 65 72 72 6f  ated.** I/O erro
129e9 72 20 6d 65 63 68 61 6e 69 73 6d 2e 20 20 54 68  r mechanism.  Th
129ea 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65  ese routines are
129eb 20 75 73 65 64 20 74 6f 20 61 76 6f 69 64 20 73   used to avoid s
129ec 69 6d 75 6c 61 74 65 64 0a 2a 2a 20 65 72 72 6f  imulated.** erro
129ed 72 73 20 69 6e 20 70 6c 61 63 65 73 20 77 68 65  rs in places whe
129ee 72 65 20 77 65 20 64 6f 20 6e 6f 74 20 63 61 72  re we do not car
129ef 65 20 61 62 6f 75 74 20 65 72 72 6f 72 73 2e 0a  e about errors..
129f0 2a 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20 2d 44 53  **.** Unless -DS
129f1 51 4c 49 54 45 5f 54 45 53 54 3d 31 20 69 73 20  QLITE_TEST=1 is 
129f2 75 73 65 64 2c 20 74 68 65 73 65 20 72 6f 75 74  used, these rout
129f3 69 6e 65 73 20 61 72 65 20 61 6c 6c 20 6e 6f 2d  ines are all no-
129f4 6f 70 73 0a 2a 2a 20 61 6e 64 20 67 65 6e 65 72  ops.** and gener
129f5 61 74 65 20 6e 6f 20 63 6f 64 65 2e 0a 2a 2f 0a  ate no code..*/.
129f6 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
129f7 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 65 78  ST.SQLITE_API ex
129f8 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33  tern int sqlite3
129f9 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e  _io_error_pendin
129fa 67 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 65 78  g;.SQLITE_API ex
129fb 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33  tern int sqlite3
129fc 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 3b 0a 73  _io_error_hit;.s
129fd 74 61 74 69 63 20 69 6e 74 20 73 61 76 65 64 5f  tatic int saved_
129fe 63 6e 74 3b 0a 76 6f 69 64 20 64 69 73 61 62 6c  cnt;.void disabl
129ff 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65  e_simulated_io_e
12a00 72 72 6f 72 73 28 76 6f 69 64 29 7b 0a 20 20 73  rrors(void){.  s
12a01 61 76 65 64 5f 63 6e 74 20 3d 20 73 71 6c 69 74  aved_cnt = sqlit
12a02 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64  e3_io_error_pend
12a03 69 6e 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69  ing;.  sqlite3_i
12a04 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20  o_error_pending 
12a05 3d 20 2d 31 3b 0a 7d 0a 76 6f 69 64 20 65 6e 61  = -1;.}.void ena
12a06 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f  ble_simulated_io
12a07 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 7b 0a 20  _errors(void){. 
12a08 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
12a09 72 5f 70 65 6e 64 69 6e 67 20 3d 20 73 61 76 65  r_pending = save
12a0a 64 5f 63 6e 74 3b 0a 7d 0a 23 65 6c 73 65 0a 23  d_cnt;.}.#else.#
12a0b 20 64 65 66 69 6e 65 20 64 69 73 61 62 6c 65 5f   define disable_
12a0c 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72  simulated_io_err
12a0d 6f 72 73 28 29 0a 23 20 64 65 66 69 6e 65 20 65  ors().# define e
12a0e 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f  nable_simulated_
12a0f 69 6f 5f 65 72 72 6f 72 73 28 29 0a 23 65 6e 64  io_errors().#end
12a10 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74  if../*.** Read t
12a11 68 65 20 66 69 72 73 74 20 4e 20 62 79 74 65 73  he first N bytes
12a12 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e   from the beginn
12a13 69 6e 67 20 6f 66 20 74 68 65 20 66 69 6c 65 20  ing of the file 
12a14 69 6e 74 6f 20 6d 65 6d 6f 72 79 0a 2a 2a 20 74  into memory.** t
12a15 68 61 74 20 70 44 65 73 74 20 70 6f 69 6e 74 73  hat pDest points
12a16 20 74 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74   to. .**.** If t
12a17 68 65 20 70 61 67 65 72 20 77 61 73 20 6f 70 65  he pager was ope
12a18 6e 65 64 20 6f 6e 20 61 20 74 72 61 6e 73 69 65  ned on a transie
12a19 6e 74 20 66 69 6c 65 20 28 7a 46 69 6c 65 6e 61  nt file (zFilena
12a1a 6d 65 3d 3d 22 22 29 2c 20 6f 72 0a 2a 2a 20 6f  me==""), or.** o
12a1b 70 65 6e 65 64 20 6f 6e 20 61 20 66 69 6c 65 20  pened on a file 
12a1c 6c 65 73 73 20 74 68 61 6e 20 4e 20 62 79 74 65  less than N byte
12a1d 73 20 69 6e 20 73 69 7a 65 2c 20 74 68 65 20 6f  s in size, the o
12a1e 75 74 70 75 74 20 62 75 66 66 65 72 20 69 73 0a  utput buffer is.
12a1f 2a 2a 20 7a 65 72 6f 65 64 20 61 6e 64 20 53 51  ** zeroed and SQ
12a20 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64  LITE_OK returned
12a21 2e 20 54 68 65 20 72 61 74 69 6f 6e 61 6c 65 20  . The rationale 
12a22 66 6f 72 20 74 68 69 73 20 69 73 20 74 68 61 74  for this is that
12a23 20 74 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69   this .** functi
12a24 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 72 65  on is used to re
12a25 61 64 20 64 61 74 61 62 61 73 65 20 68 65 61 64  ad database head
12a26 65 72 73 2c 20 61 6e 64 20 61 20 6e 65 77 20 74  ers, and a new t
12a27 72 61 6e 73 69 65 6e 74 20 6f 72 0a 2a 2a 20 7a  ransient or.** z
12a28 65 72 6f 20 73 69 7a 65 64 20 64 61 74 61 62 61  ero sized databa
12a29 73 65 20 68 61 73 20 61 20 68 65 61 64 65 72 20  se has a header 
12a2a 74 68 61 6e 20 63 6f 6e 73 69 73 74 73 20 65 6e  than consists en
12a2b 74 69 72 65 6c 79 20 6f 66 20 7a 65 72 6f 65 73  tirely of zeroes
12a2c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 20 49  ..**.** If any I
12a2d 4f 20 65 72 72 6f 72 20 61 70 61 72 74 20 66 72  O error apart fr
12a2e 6f 6d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  om SQLITE_IOERR_
12a2f 53 48 4f 52 54 5f 52 45 41 44 20 69 73 20 65 6e  SHORT_READ is en
12a30 63 6f 75 6e 74 65 72 65 64 2c 0a 2a 2a 20 74 68  countered,.** th
12a31 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  e error code is 
12a32 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20  returned to the 
12a33 63 61 6c 6c 65 72 20 61 6e 64 20 74 68 65 20 63  caller and the c
12a34 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 0a 2a  ontents of the.*
12a35 2a 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20  * output buffer 
12a36 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51  undefined..*/.SQ
12a37 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
12a38 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 61   sqlite3PagerRea
12a39 64 46 69 6c 65 68 65 61 64 65 72 28 50 61 67 65  dFileheader(Page
12a3a 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 4e  r *pPager, int N
12a3b 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  , unsigned char 
12a3c 2a 70 44 65 73 74 29 7b 0a 20 20 69 6e 74 20 72  *pDest){.  int r
12a3d 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
12a3e 20 6d 65 6d 73 65 74 28 70 44 65 73 74 2c 20 30   memset(pDest, 0
12a3f 2c 20 4e 29 3b 0a 20 20 61 73 73 65 72 74 28 20  , N);.  assert( 
12a40 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66  isOpen(pPager->f
12a41 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65  d) || pPager->te
12a42 6d 70 46 69 6c 65 20 29 3b 0a 20 20 69 66 28 20  mpFile );.  if( 
12a43 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66  isOpen(pPager->f
12a44 64 29 20 29 7b 0a 20 20 20 20 49 4f 54 52 41 43  d) ){.    IOTRAC
12a45 45 28 28 22 44 42 48 44 52 20 25 70 20 30 20 25  E(("DBHDR %p 0 %
12a46 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 4e 29  d\n", pPager, N)
12a47 29 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ).    rc = sqlit
12a48 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d  e3OsRead(pPager-
12a49 3e 66 64 2c 20 70 44 65 73 74 2c 20 4e 2c 20 30  >fd, pDest, N, 0
12a4a 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
12a4b 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52  QLITE_IOERR_SHOR
12a4c 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 20 20  T_READ ){.      
12a4d 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
12a4e 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
12a4f 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
12a50 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61 6c  Return the total
12a51 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
12a52 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
12a53 20 66 69 6c 65 20 61 73 73 6f 63 69 61 74 65 64   file associated
12a54 20 0a 2a 2a 20 77 69 74 68 20 70 50 61 67 65 72   .** with pPager
12a55 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 74 68 69 73  . Normally, this
12a56 20 69 73 20 63 61 6c 63 75 6c 61 74 65 64 20 61   is calculated a
12a57 73 20 28 3c 64 62 20 66 69 6c 65 20 73 69 7a 65  s (<db file size
12a58 3e 2f 3c 70 61 67 65 2d 73 69 7a 65 3e 29 2e 0a  >/<page-size>)..
12a59 2a 2a 20 48 6f 77 65 76 65 72 2c 20 69 66 20 74  ** However, if t
12a5a 68 65 20 66 69 6c 65 20 69 73 20 62 65 74 77 65  he file is betwe
12a5b 65 6e 20 31 20 61 6e 64 20 3c 70 61 67 65 2d 73  en 1 and <page-s
12a5c 69 7a 65 3e 20 62 79 74 65 73 20 69 6e 20 73 69  ize> bytes in si
12a5d 7a 65 2c 20 74 68 65 6e 20 0a 2a 2a 20 74 68 69  ze, then .** thi
12a5e 73 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20  s is considered 
12a5f 61 20 31 20 70 61 67 65 20 66 69 6c 65 2e 0a 2a  a 1 page file..*
12a60 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65  *.** If the page
12a61 72 20 69 73 20 69 6e 20 65 72 72 6f 72 20 73 74  r is in error st
12a62 61 74 65 20 77 68 65 6e 20 74 68 69 73 20 66 75  ate when this fu
12a63 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
12a64 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 65 72  , then the.** er
12a65 72 6f 72 20 73 74 61 74 65 20 65 72 72 6f 72 20  ror state error 
12a66 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
12a67 20 61 6e 64 20 2a 70 6e 50 61 67 65 20 6c 65 66   and *pnPage lef
12a68 74 20 75 6e 63 68 61 6e 67 65 64 2e 20 4f 72 2c  t unchanged. Or,
12a69 0a 2a 2a 20 69 66 20 74 68 65 20 66 69 6c 65 20  .** if the file 
12a6a 73 79 73 74 65 6d 20 68 61 73 20 74 6f 20 62 65  system has to be
12a6b 20 71 75 65 72 69 65 64 20 66 6f 72 20 74 68 65   queried for the
12a6c 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c   size of the fil
12a6d 65 20 61 6e 64 0a 2a 2a 20 74 68 65 20 71 75 65  e and.** the que
12a6e 72 79 20 61 74 74 65 6d 70 74 20 72 65 74 75 72  ry attempt retur
12a6f 6e 73 20 61 6e 20 49 4f 20 65 72 72 6f 72 2c 20  ns an IO error, 
12a70 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64  the IO error cod
12a71 65 20 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a  e is returned.**
12a72 20 61 6e 64 20 2a 70 6e 50 61 67 65 20 69 73 20   and *pnPage is 
12a73 6c 65 66 74 20 75 6e 63 68 61 6e 67 65 64 2e 0a  left unchanged..
12a74 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  **.** Otherwise,
12a75 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 69   if everything i
12a76 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74 68  s successful, th
12a77 65 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  en SQLITE_OK is 
12a78 72 65 74 75 72 6e 65 64 0a 2a 2a 20 61 6e 64 20  returned.** and 
12a79 2a 70 6e 50 61 67 65 20 69 73 20 73 65 74 20 74  *pnPage is set t
12a7a 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  o the number of 
12a7b 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74  pages in the dat
12a7c 61 62 61 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  abase..*/.SQLITE
12a7d 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
12a7e 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75  ite3PagerPagecou
12a7f 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  nt(Pager *pPager
12a80 2c 20 69 6e 74 20 2a 70 6e 50 61 67 65 29 7b 0a  , int *pnPage){.
12a81 20 20 50 67 6e 6f 20 6e 50 61 67 65 3b 20 20 20    Pgno nPage;   
12a82 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
12a83 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 76  alue to return v
12a84 69 61 20 2a 70 6e 50 61 67 65 20 2a 2f 0a 0a 20  ia *pnPage */.. 
12a85 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 72   /* If the pager
12a86 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74   is already in t
12a87 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20  he error state, 
12a88 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72  return the error
12a89 20 63 6f 64 65 2e 20 2a 2f 0a 20 20 69 66 28 20   code. */.  if( 
12a8a 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20  pPager->errCode 
12a8b 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50  ){.    return pP
12a8c 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20  ager->errCode;. 
12a8d 20 7d 0a 0a 20 20 2f 2a 20 44 65 74 65 72 6d 69   }..  /* Determi
12a8e 6e 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ne the number of
12a8f 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 66 69   pages in the fi
12a90 6c 65 2e 20 53 74 6f 72 65 20 74 68 69 73 20 69  le. Store this i
12a91 6e 20 6e 50 61 67 65 2e 20 2a 2f 0a 20 20 69 66  n nPage. */.  if
12a92 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  ( pPager->dbSize
12a93 56 61 6c 69 64 20 29 7b 0a 20 20 20 20 6e 50 61  Valid ){.    nPa
12a94 67 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53  ge = pPager->dbS
12a95 69 7a 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ize;.  }else{.  
12a96 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
12a97 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72            /* Err
12a98 6f 72 20 72 65 74 75 72 6e 65 64 20 62 79 20 4f  or returned by O
12a99 73 46 69 6c 65 53 69 7a 65 28 29 20 2a 2f 0a 20  sFileSize() */. 
12a9a 20 20 20 69 36 34 20 6e 20 3d 20 30 3b 20 20 20     i64 n = 0;   
12a9b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
12a9c 6c 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73  le size in bytes
12a9d 20 72 65 74 75 72 6e 65 64 20 62 79 20 4f 73 46   returned by OsF
12a9e 69 6c 65 53 69 7a 65 28 29 20 2a 2f 0a 0a 20 20  ileSize() */..  
12a9f 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e    assert( isOpen
12aa0 28 70 50 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20  (pPager->fd) || 
12aa1 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65  pPager->tempFile
12aa2 20 29 3b 0a 20 20 20 20 69 66 28 20 69 73 4f 70   );.    if( isOp
12aa3 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 26  en(pPager->fd) &
12aa4 26 20 28 30 20 21 3d 20 28 72 63 20 3d 20 73 71  & (0 != (rc = sq
12aa5 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28  lite3OsFileSize(
12aa6 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 6e 29 29  pPager->fd, &n))
12aa7 29 20 29 7b 0a 20 20 20 20 20 20 70 61 67 65 72  ) ){.      pager
12aa8 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72  _error(pPager, r
12aa9 63 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  c);.      return
12aaa 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69   rc;.    }.    i
12aab 66 28 20 6e 3e 30 20 26 26 20 6e 3c 70 50 61 67  f( n>0 && n<pPag
12aac 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 29 7b 0a  er->pageSize ){.
12aad 20 20 20 20 20 20 6e 50 61 67 65 20 3d 20 31 3b        nPage = 1;
12aae 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
12aaf 20 20 6e 50 61 67 65 20 3d 20 28 50 67 6e 6f 29    nPage = (Pgno)
12ab0 28 6e 20 2f 20 70 50 61 67 65 72 2d 3e 70 61 67  (n / pPager->pag
12ab1 65 53 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 20 20  eSize);.    }.  
12ab2 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74    if( pPager->st
12ab3 61 74 65 21 3d 50 41 47 45 52 5f 55 4e 4c 4f 43  ate!=PAGER_UNLOC
12ab4 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65  K ){.      pPage
12ab5 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6e 50 61 67  r->dbSize = nPag
12ab6 65 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  e;.      pPager-
12ab7 3e 64 62 46 69 6c 65 53 69 7a 65 20 3d 20 6e 50  >dbFileSize = nP
12ab8 61 67 65 3b 0a 20 20 20 20 20 20 70 50 61 67 65  age;.      pPage
12ab9 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 3d  r->dbSizeValid =
12aba 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20   1;.    }.  }.. 
12abb 20 2f 2a 20 49 66 20 74 68 65 20 63 75 72 72 65   /* If the curre
12abc 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  nt number of pag
12abd 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65 20 69  es in the file i
12abe 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74  s greater than t
12abf 68 65 20 0a 20 20 2a 2a 20 63 6f 6e 66 69 67 75  he .  ** configu
12ac0 72 65 64 20 6d 61 78 69 6d 75 6d 20 70 61 67 65  red maximum page
12ac1 72 20 6e 75 6d 62 65 72 2c 20 69 6e 63 72 65 61  r number, increa
12ac2 73 65 20 74 68 65 20 61 6c 6c 6f 77 65 64 20 6c  se the allowed l
12ac3 69 6d 69 74 20 73 6f 0a 20 20 2a 2a 20 74 68 61  imit so.  ** tha
12ac4 74 20 74 68 65 20 66 69 6c 65 20 63 61 6e 20 62  t the file can b
12ac5 65 20 72 65 61 64 2e 0a 20 20 2a 2f 0a 20 20 69  e read..  */.  i
12ac6 66 28 20 6e 50 61 67 65 3e 70 50 61 67 65 72 2d  f( nPage>pPager-
12ac7 3e 6d 78 50 67 6e 6f 20 29 7b 0a 20 20 20 20 70  >mxPgno ){.    p
12ac8 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20  Pager->mxPgno = 
12ac9 28 50 67 6e 6f 29 6e 50 61 67 65 3b 0a 20 20 7d  (Pgno)nPage;.  }
12aca 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6f  ..  /* Set the o
12acb 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 20 61  utput variable a
12acc 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  nd return SQLITE
12acd 5f 4f 4b 20 2a 2f 0a 20 20 69 66 28 20 70 6e 50  _OK */.  if( pnP
12ace 61 67 65 20 29 7b 0a 20 20 20 20 2a 70 6e 50 61  age ){.    *pnPa
12acf 67 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20 7d 0a  ge = nPage;.  }.
12ad0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
12ad1 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 72  OK;.}.../*.** Tr
12ad2 79 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 6c 6f  y to obtain a lo
12ad3 63 6b 20 6f 66 20 74 79 70 65 20 6c 6f 63 6b 74  ck of type lockt
12ad4 79 70 65 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ype on the datab
12ad5 61 73 65 20 66 69 6c 65 2e 20 49 66 0a 2a 2a 20  ase file. If.** 
12ad6 61 20 73 69 6d 69 6c 61 72 20 6f 72 20 67 72 65  a similar or gre
12ad7 61 74 65 72 20 6c 6f 63 6b 20 69 73 20 61 6c 72  ater lock is alr
12ad8 65 61 64 79 20 68 65 6c 64 2c 20 74 68 69 73 20  eady held, this 
12ad9 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f  function is a no
12ada 2d 6f 70 0a 2a 2a 20 28 72 65 74 75 72 6e 69 6e  -op.** (returnin
12adb 67 20 53 51 4c 49 54 45 5f 4f 4b 20 69 6d 6d 65  g SQLITE_OK imme
12adc 64 69 61 74 65 6c 79 29 2e 0a 2a 2a 0a 2a 2a 20  diately)..**.** 
12add 4f 74 68 65 72 77 69 73 65 2c 20 61 74 74 65 6d  Otherwise, attem
12ade 70 74 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65  pt to obtain the
12adf 20 6c 6f 63 6b 20 75 73 69 6e 67 20 73 71 6c 69   lock using sqli
12ae0 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 20 49 6e 76  te3OsLock(). Inv
12ae1 6f 6b 65 20 0a 2a 2a 20 74 68 65 20 62 75 73 79  oke .** the busy
12ae2 20 63 61 6c 6c 62 61 63 6b 20 69 66 20 74 68 65   callback if the
12ae3 20 6c 6f 63 6b 20 69 73 20 63 75 72 72 65 6e 74   lock is current
12ae4 6c 79 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65  ly not available
12ae5 2e 20 52 65 70 65 61 74 20 0a 2a 2a 20 75 6e 74  . Repeat .** unt
12ae6 69 6c 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c  il the busy call
12ae7 62 61 63 6b 20 72 65 74 75 72 6e 73 20 66 61 6c  back returns fal
12ae8 73 65 20 6f 72 20 75 6e 74 69 6c 20 74 68 65 20  se or until the 
12ae9 61 74 74 65 6d 70 74 20 74 6f 20 0a 2a 2a 20 6f  attempt to .** o
12aea 62 74 61 69 6e 20 74 68 65 20 6c 6f 63 6b 20 73  btain the lock s
12aeb 75 63 63 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 52  ucceeds..**.** R
12aec 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
12aed 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 61  on success and a
12aee 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20  n error code if 
12aef 77 65 20 63 61 6e 6e 6f 74 20 6f 62 74 61 69 6e  we cannot obtain
12af0 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 2e 20 49 66  .** the lock. If
12af1 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 6f 62 74   the lock is obt
12af2 61 69 6e 65 64 20 73 75 63 63 65 73 73 66 75 6c  ained successful
12af3 6c 79 2c 20 73 65 74 20 74 68 65 20 50 61 67 65  ly, set the Page
12af4 72 2e 73 74 61 74 65 20 0a 2a 2a 20 76 61 72 69  r.state .** vari
12af5 61 62 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65  able to locktype
12af6 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e   before returnin
12af7 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  g..*/.static int
12af8 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c   pager_wait_on_l
12af9 6f 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65  ock(Pager *pPage
12afa 72 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29  r, int locktype)
12afb 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  {.  int rc;     
12afc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12afd 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
12afe 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a  rn code */..  /*
12aff 20 54 68 65 20 4f 53 20 6c 6f 63 6b 20 76 61 6c   The OS lock val
12b00 75 65 73 20 6d 75 73 74 20 62 65 20 74 68 65 20  ues must be the 
12b01 73 61 6d 65 20 61 73 20 74 68 65 20 50 61 67 65  same as the Page
12b02 72 20 6c 6f 63 6b 20 76 61 6c 75 65 73 20 2a 2f  r lock values */
12b03 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 45 52  .  assert( PAGER
12b04 5f 53 48 41 52 45 44 3d 3d 53 48 41 52 45 44 5f  _SHARED==SHARED_
12b05 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74  LOCK );.  assert
12b06 28 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 44  ( PAGER_RESERVED
12b07 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20  ==RESERVED_LOCK 
12b08 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47  );.  assert( PAG
12b09 45 52 5f 45 58 43 4c 55 53 49 56 45 3d 3d 45 58  ER_EXCLUSIVE==EX
12b0a 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 3b 0a  CLUSIVE_LOCK );.
12b0b 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c  .  /* If the fil
12b0c 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 75  e is currently u
12b0d 6e 6c 6f 63 6b 65 64 20 74 68 65 6e 20 74 68 65  nlocked then the
12b0e 20 73 69 7a 65 20 6d 75 73 74 20 62 65 20 75 6e   size must be un
12b0f 6b 6e 6f 77 6e 20 2a 2f 0a 20 20 61 73 73 65 72  known */.  asser
12b10 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  t( pPager->state
12b11 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 7c  >=PAGER_SHARED |
12b12 7c 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  | pPager->dbSize
12b13 56 61 6c 69 64 3d 3d 30 20 29 3b 0a 0a 20 20 2f  Valid==0 );..  /
12b14 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 69  * Check that thi
12b15 73 20 69 73 20 65 69 74 68 65 72 20 61 20 6e 6f  s is either a no
12b16 2d 6f 70 20 28 62 65 63 61 75 73 65 20 74 68 65  -op (because the
12b17 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 20   requested lock 
12b18 69 73 20 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79  is .  ** already
12b19 20 68 65 6c 64 2c 20 6f 72 20 6f 6e 65 20 6f 66   held, or one of
12b1a 20 74 68 65 20 74 72 61 6e 73 69 73 74 69 6f 6e   the transistion
12b1b 73 20 74 68 61 74 20 74 68 65 20 62 75 73 79 2d  s that the busy-
12b1c 68 61 6e 64 6c 65 72 0a 20 20 2a 2a 20 6d 61 79  handler.  ** may
12b1d 20 62 65 20 69 6e 76 6f 6b 65 64 20 64 75 72 69   be invoked duri
12b1e 6e 67 2c 20 61 63 63 6f 72 64 69 6e 67 20 74 6f  ng, according to
12b1f 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 61 62 6f   the comment abo
12b20 76 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 50  ve.  ** sqlite3P
12b21 61 67 65 72 53 65 74 42 75 73 79 68 61 6e 64 6c  agerSetBusyhandl
12b22 65 72 28 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  er()..  */.  ass
12b23 65 72 74 28 20 28 70 50 61 67 65 72 2d 3e 73 74  ert( (pPager->st
12b24 61 74 65 3e 3d 6c 6f 63 6b 74 79 70 65 29 0a 20  ate>=locktype). 
12b25 20 20 20 20 20 20 7c 7c 20 28 70 50 61 67 65 72        || (pPager
12b26 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 55  ->state==PAGER_U
12b27 4e 4c 4f 43 4b 20 26 26 20 6c 6f 63 6b 74 79 70  NLOCK && locktyp
12b28 65 3d 3d 50 41 47 45 52 5f 53 48 41 52 45 44 29  e==PAGER_SHARED)
12b29 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 50 61 67  .       || (pPag
12b2a 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52  er->state==PAGER
12b2b 5f 52 45 53 45 52 56 45 44 20 26 26 20 6c 6f 63  _RESERVED && loc
12b2c 6b 74 79 70 65 3d 3d 50 41 47 45 52 5f 45 58 43  ktype==PAGER_EXC
12b2d 4c 55 53 49 56 45 29 0a 20 20 29 3b 0a 0a 20 20  LUSIVE).  );..  
12b2e 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  if( pPager->stat
12b2f 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20  e>=locktype ){. 
12b30 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
12b31 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  K;.  }else{.    
12b32 64 6f 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  do {.      rc = 
12b33 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50  sqlite3OsLock(pP
12b34 61 67 65 72 2d 3e 66 64 2c 20 6c 6f 63 6b 74 79  ager->fd, lockty
12b35 70 65 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28  pe);.    }while(
12b36 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59   rc==SQLITE_BUSY
12b37 20 26 26 20 70 50 61 67 65 72 2d 3e 78 42 75 73   && pPager->xBus
12b38 79 48 61 6e 64 6c 65 72 28 70 50 61 67 65 72 2d  yHandler(pPager-
12b39 3e 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67  >pBusyHandlerArg
12b3a 29 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  ) );.    if( rc=
12b3b 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
12b3c 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74      pPager->stat
12b3d 65 20 3d 20 28 75 38 29 6c 6f 63 6b 74 79 70 65  e = (u8)locktype
12b3e 3b 0a 20 20 20 20 20 20 49 4f 54 52 41 43 45 28  ;.      IOTRACE(
12b3f 28 22 4c 4f 43 4b 20 25 70 20 25 64 5c 6e 22 2c  ("LOCK %p %d\n",
12b40 20 70 50 61 67 65 72 2c 20 6c 6f 63 6b 74 79 70   pPager, locktyp
12b41 65 29 29 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  e)).    }.  }.  
12b42 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
12b43 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 61 73 73  .** Function ass
12b44 65 72 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 74  ertTruncateConst
12b45 72 61 69 6e 74 28 70 50 61 67 65 72 29 20 63 68  raint(pPager) ch
12b46 65 63 6b 73 20 74 68 61 74 20 6f 6e 65 20 6f 66  ecks that one of
12b47 20 74 68 65 20 0a 2a 2a 20 66 6f 6c 6c 6f 77 69   the .** followi
12b48 6e 67 20 69 73 20 74 72 75 65 20 66 6f 72 20 61  ng is true for a
12b49 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 63  ll dirty pages c
12b4a 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 65 20  urrently in the 
12b4b 70 61 67 65 2d 63 61 63 68 65 3a 0a 2a 2a 0a 2a  page-cache:.**.*
12b4c 2a 20 20 20 61 29 20 54 68 65 20 70 61 67 65 20  *   a) The page 
12b4d 6e 75 6d 62 65 72 20 69 73 20 6c 65 73 73 20 74  number is less t
12b4e 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20  han or equal to 
12b4f 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
12b50 0a 2a 2a 20 20 20 20 20 20 63 75 72 72 65 6e 74  .**      current
12b51 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 2c   database image,
12b52 20 69 6e 20 70 61 67 65 73 2c 20 4f 52 0a 2a 2a   in pages, OR.**
12b53 0a 2a 2a 20 20 20 62 29 20 69 66 20 74 68 65 20  .**   b) if the 
12b54 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 77 65 72  page content wer
12b55 65 20 77 72 69 74 74 65 6e 20 61 74 20 74 68 69  e written at thi
12b56 73 20 74 69 6d 65 2c 20 69 74 20 77 6f 75 6c 64  s time, it would
12b57 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 62 65 20   not.**      be 
12b58 6e 65 63 65 73 73 61 72 79 20 74 6f 20 77 72 69  necessary to wri
12b59 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 63  te the current c
12b5a 6f 6e 74 65 6e 74 20 6f 75 74 20 74 6f 20 74 68  ontent out to th
12b5b 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a  e sub-journal.**
12b5c 20 20 20 20 20 20 28 61 73 20 64 65 74 65 72 6d        (as determ
12b5d 69 6e 65 64 20 62 79 20 66 75 6e 63 74 69 6f 6e  ined by function
12b5e 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67   subjRequiresPag
12b5f 65 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  e())..**.** If t
12b60 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 61 73 73  he condition ass
12b61 65 72 74 65 64 20 62 79 20 74 68 69 73 20 66 75  erted by this fu
12b62 6e 63 74 69 6f 6e 20 77 65 72 65 20 6e 6f 74 20  nction were not 
12b63 74 72 75 65 2c 20 61 6e 64 20 74 68 65 0a 2a 2a  true, and the.**
12b64 20 64 69 72 74 79 20 70 61 67 65 20 77 65 72 65   dirty page were
12b65 20 74 6f 20 62 65 20 64 69 73 63 61 72 64 65 64   to be discarded
12b66 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 20   from the cache 
12b67 76 69 61 20 74 68 65 20 70 61 67 65 72 53 74 72  via the pagerStr
12b68 65 73 73 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65  ess().** routine
12b69 2c 20 70 61 67 65 72 53 74 72 65 73 73 28 29 20  , pagerStress() 
12b6a 77 6f 75 6c 64 20 6e 6f 74 20 77 72 69 74 65 20  would not write 
12b6b 74 68 65 20 63 75 72 72 65 6e 74 20 70 61 67 65  the current page
12b6c 20 63 6f 6e 74 65 6e 74 20 74 6f 0a 2a 2a 20 74   content to.** t
12b6d 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
12b6e 2e 20 49 66 20 61 20 73 61 76 65 70 6f 69 6e 74  . If a savepoint
12b6f 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 65 72   transaction wer
12b70 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 66  e rolled back af
12b71 74 65 72 0a 2a 2a 20 74 68 69 73 20 68 61 70 70  ter.** this happ
12b72 65 6e 65 64 2c 20 74 68 65 20 63 6f 72 72 65 63  ened, the correc
12b73 74 20 62 65 68 61 76 69 6f 75 72 20 77 6f 75 6c  t behaviour woul
12b74 64 20 62 65 20 74 6f 20 72 65 73 74 6f 72 65 20  d be to restore 
12b75 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 63  the current.** c
12b76 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61  ontent of the pa
12b77 67 65 2e 20 48 6f 77 65 76 65 72 2c 20 73 69 6e  ge. However, sin
12b78 63 65 20 74 68 69 73 20 63 6f 6e 74 65 6e 74 20  ce this content 
12b79 69 73 20 6e 6f 74 20 70 72 65 73 65 6e 74 20 69  is not present i
12b7a 6e 20 65 69 74 68 65 72 0a 2a 2a 20 74 68 65 20  n either.** the 
12b7b 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f 72  database file or
12b7c 20 74 68 65 20 70 6f 72 74 69 6f 6e 20 6f 66 20   the portion of 
12b7d 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  the rollback jou
12b7e 72 6e 61 6c 20 61 6e 64 20 0a 2a 2a 20 73 75 62  rnal and .** sub
12b7f 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 65 64 20  -journal rolled 
12b80 62 61 63 6b 20 74 68 65 20 63 6f 6e 74 65 6e 74  back the content
12b81 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 72 65   could not be re
12b82 73 74 6f 72 65 64 20 61 6e 64 20 74 68 65 0a 2a  stored and the.*
12b83 2a 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65  * database image
12b84 20 77 6f 75 6c 64 20 62 65 63 6f 6d 65 20 63 6f   would become co
12b85 72 72 75 70 74 2e 20 49 74 20 69 73 20 74 68 65  rrupt. It is the
12b86 72 65 66 6f 72 65 20 66 6f 72 74 75 6e 61 74 65  refore fortunate
12b87 20 74 68 61 74 20 0a 2a 2a 20 74 68 69 73 20 63   that .** this c
12b88 69 72 63 75 6d 73 74 61 6e 63 65 20 63 61 6e 6e  ircumstance cann
12b89 6f 74 20 61 72 69 73 65 2e 0a 2a 2f 0a 23 69 66  ot arise..*/.#if
12b8a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
12b8b 44 45 42 55 47 29 0a 73 74 61 74 69 63 20 76 6f  DEBUG).static vo
12b8c 69 64 20 61 73 73 65 72 74 54 72 75 6e 63 61 74  id assertTruncat
12b8d 65 43 6f 6e 73 74 72 61 69 6e 74 43 62 28 50 67  eConstraintCb(Pg
12b8e 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 61 73 73  Hdr *pPg){.  ass
12b8f 65 72 74 28 20 70 50 67 2d 3e 66 6c 61 67 73 26  ert( pPg->flags&
12b90 50 47 48 44 52 5f 44 49 52 54 59 20 29 3b 0a 20  PGHDR_DIRTY );. 
12b91 20 61 73 73 65 72 74 28 20 21 73 75 62 6a 52 65   assert( !subjRe
12b92 71 75 69 72 65 73 50 61 67 65 28 70 50 67 29 20  quiresPage(pPg) 
12b93 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f 3c 3d 70 50  || pPg->pgno<=pP
12b94 67 2d 3e 70 50 61 67 65 72 2d 3e 64 62 53 69 7a  g->pPager->dbSiz
12b95 65 20 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  e );.}.static vo
12b96 69 64 20 61 73 73 65 72 74 54 72 75 6e 63 61 74  id assertTruncat
12b97 65 43 6f 6e 73 74 72 61 69 6e 74 28 50 61 67 65  eConstraint(Page
12b98 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 73 71  r *pPager){.  sq
12b99 6c 69 74 65 33 50 63 61 63 68 65 49 74 65 72 61  lite3PcacheItera
12b9a 74 65 44 69 72 74 79 28 70 50 61 67 65 72 2d 3e  teDirty(pPager->
12b9b 70 50 43 61 63 68 65 2c 20 61 73 73 65 72 74 54  pPCache, assertT
12b9c 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 6e  runcateConstrain
12b9d 74 43 62 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23 20  tCb);.}.#else.# 
12b9e 64 65 66 69 6e 65 20 61 73 73 65 72 74 54 72 75  define assertTru
12b9f 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 28  ncateConstraint(
12ba0 70 50 61 67 65 72 29 0a 23 65 6e 64 69 66 0a 0a  pPager).#endif..
12ba1 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 74  /*.** Truncate t
12ba2 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74  he in-memory dat
12ba3 61 62 61 73 65 20 66 69 6c 65 20 69 6d 61 67 65  abase file image
12ba4 20 74 6f 20 6e 50 61 67 65 20 70 61 67 65 73 2e   to nPage pages.
12ba5 20 54 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69   This .** functi
12ba6 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 61 63 74 75  on does not actu
12ba7 61 6c 6c 79 20 6d 6f 64 69 66 79 20 74 68 65 20  ally modify the 
12ba8 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f 6e  database file on
12ba9 20 64 69 73 6b 2e 20 49 74 20 0a 2a 2a 20 6a 75   disk. It .** ju
12baa 73 74 20 73 65 74 73 20 74 68 65 20 69 6e 74 65  st sets the inte
12bab 72 6e 61 6c 20 73 74 61 74 65 20 6f 66 20 74 68  rnal state of th
12bac 65 20 70 61 67 65 72 20 6f 62 6a 65 63 74 20 73  e pager object s
12bad 6f 20 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 74  o that the .** t
12bae 72 75 6e 63 61 74 69 6f 6e 20 77 69 6c 6c 20 62  runcation will b
12baf 65 20 64 6f 6e 65 20 77 68 65 6e 20 74 68 65 20  e done when the 
12bb0 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74  current transact
12bb1 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64  ion is committed
12bb2 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
12bb3 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
12bb4 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61  PagerTruncateIma
12bb5 67 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ge(Pager *pPager
12bb6 2c 20 50 67 6e 6f 20 6e 50 61 67 65 29 7b 0a 20  , Pgno nPage){. 
12bb7 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
12bb8 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 29 3b 0a  >dbSizeValid );.
12bb9 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
12bba 2d 3e 64 62 53 69 7a 65 3e 3d 6e 50 61 67 65 20  ->dbSize>=nPage 
12bbb 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
12bbc 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45  ger->state>=PAGE
12bbd 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20 20  R_RESERVED );.  
12bbe 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d  pPager->dbSize =
12bbf 20 6e 50 61 67 65 3b 0a 20 20 61 73 73 65 72 74   nPage;.  assert
12bc0 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 69  TruncateConstrai
12bc1 6e 74 28 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f  nt(pPager);.}../
12bc2 2a 0a 2a 2a 20 53 68 75 74 64 6f 77 6e 20 74 68  *.** Shutdown th
12bc3 65 20 70 61 67 65 20 63 61 63 68 65 2e 20 20 46  e page cache.  F
12bc4 72 65 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61  ree all memory a
12bc5 6e 64 20 63 6c 6f 73 65 20 61 6c 6c 20 66 69 6c  nd close all fil
12bc6 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 74  es..**.** If a t
12bc7 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 69  ransaction was i
12bc8 6e 20 70 72 6f 67 72 65 73 73 20 77 68 65 6e 20  n progress when 
12bc9 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
12bca 63 61 6c 6c 65 64 2c 20 74 68 61 74 0a 2a 2a 20  called, that.** 
12bcb 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72  transaction is r
12bcc 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 20 41 6c 6c  olled back.  All
12bcd 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67   outstanding pag
12bce 65 73 20 61 72 65 20 69 6e 76 61 6c 69 64 61 74  es are invalidat
12bcf 65 64 0a 2a 2a 20 61 6e 64 20 74 68 65 69 72 20  ed.** and their 
12bd0 6d 65 6d 6f 72 79 20 69 73 20 66 72 65 65 64 2e  memory is freed.
12bd1 20 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f    Any attempt to
12bd2 20 75 73 65 20 61 20 70 61 67 65 20 61 73 73 6f   use a page asso
12bd3 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 74  ciated.** with t
12bd4 68 69 73 20 70 61 67 65 20 63 61 63 68 65 20 61  his page cache a
12bd5 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 74 69  fter this functi
12bd6 6f 6e 20 72 65 74 75 72 6e 73 20 77 69 6c 6c 20  on returns will 
12bd7 6c 69 6b 65 6c 79 0a 2a 2a 20 72 65 73 75 6c 74  likely.** result
12bd8 20 69 6e 20 61 20 63 6f 72 65 64 75 6d 70 2e 0a   in a coredump..
12bd9 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  **.** This funct
12bda 69 6f 6e 20 61 6c 77 61 79 73 20 73 75 63 63 65  ion always succe
12bdb 65 64 73 2e 20 49 66 20 61 20 74 72 61 6e 73 61  eds. If a transa
12bdc 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20  ction is active 
12bdd 61 6e 20 61 74 74 65 6d 70 74 0a 2a 2a 20 69 73  an attempt.** is
12bde 20 6d 61 64 65 20 74 6f 20 72 6f 6c 6c 20 69 74   made to roll it
12bdf 20 62 61 63 6b 2e 20 49 66 20 61 6e 20 65 72 72   back. If an err
12be0 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67  or occurs during
12be1 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 0a 2a   the rollback .*
12be2 2a 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20  * a hot journal 
12be3 6d 61 79 20 62 65 20 6c 65 66 74 20 69 6e 20 74  may be left in t
12be4 68 65 20 66 69 6c 65 73 79 73 74 65 6d 20 62 75  he filesystem bu
12be5 74 20 6e 6f 20 65 72 72 6f 72 20 69 73 20 72 65  t no error is re
12be6 74 75 72 6e 65 64 0a 2a 2a 20 74 6f 20 74 68 65  turned.** to the
12be7 20 63 61 6c 6c 65 72 2e 0a 2a 2f 0a 53 51 4c 49   caller..*/.SQLI
12be8 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
12be9 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65  qlite3PagerClose
12bea 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
12beb 0a 20 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c  .  disable_simul
12bec 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29  ated_io_errors()
12bed 3b 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e  ;.  sqlite3Begin
12bee 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a  BenignMalloc();.
12bef 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64    pPager->errCod
12bf0 65 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d  e = 0;.  pPager-
12bf1 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d  >exclusiveMode =
12bf2 20 30 3b 0a 20 20 70 61 67 65 72 5f 72 65 73 65   0;.  pager_rese
12bf3 74 28 70 50 61 67 65 72 29 3b 0a 20 20 69 66 28  t(pPager);.  if(
12bf4 20 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 70 61   MEMDB ){.    pa
12bf5 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 50 61 67 65  ger_unlock(pPage
12bf6 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  r);.  }else{.   
12bf7 20 2f 2a 20 53 65 74 20 50 61 67 65 72 2e 6a 6f   /* Set Pager.jo
12bf8 75 72 6e 61 6c 48 64 72 20 74 6f 20 2d 31 20 66  urnalHdr to -1 f
12bf9 6f 72 20 74 68 65 20 62 65 6e 65 66 69 74 20 6f  or the benefit o
12bfa 66 20 74 68 65 20 70 61 67 65 72 5f 70 6c 61 79  f the pager_play
12bfb 62 61 63 6b 28 29 20 0a 20 20 20 20 2a 2a 20 63  back() .    ** c
12bfc 61 6c 6c 20 77 68 69 63 68 20 6d 61 79 20 62 65  all which may be
12bfd 20 6d 61 64 65 20 66 72 6f 6d 20 77 69 74 68 69   made from withi
12bfe 6e 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64  n pagerUnlockAnd
12bff 52 6f 6c 6c 62 61 63 6b 28 29 2e 20 49 66 20 69  Rollback(). If i
12c00 74 0a 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20  t.    ** is not 
12c01 2d 31 2c 20 74 68 65 6e 20 74 68 65 20 75 6e 73  -1, then the uns
12c02 79 6e 63 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66  ynced portion of
12c03 20 61 6e 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c   an open journal
12c04 20 66 69 6c 65 20 6d 61 79 0a 20 20 20 20 2a 2a   file may.    **
12c05 20 62 65 20 70 6c 61 79 65 64 20 62 61 63 6b 20   be played back 
12c06 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  into the databas
12c07 65 2e 20 49 66 20 61 20 70 6f 77 65 72 20 66 61  e. If a power fa
12c08 69 6c 75 72 65 20 6f 63 63 75 72 73 20 77 68 69  ilure occurs whi
12c09 6c 65 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 69  le.    ** this i
12c0a 73 20 68 61 70 70 65 6e 69 6e 67 2c 20 74 68 65  s happening, the
12c0b 20 64 61 74 61 62 61 73 65 20 6d 61 79 20 62 65   database may be
12c0c 63 6f 6d 65 20 63 6f 72 72 75 70 74 2e 0a 20 20  come corrupt..  
12c0d 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d    */.    pPager-
12c0e 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 2d 31  >journalHdr = -1
12c0f 3b 0a 20 20 20 20 70 61 67 65 72 55 6e 6c 6f 63  ;.    pagerUnloc
12c10 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 70 50 61  kAndRollback(pPa
12c11 67 65 72 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  ger);.  }.  sqli
12c12 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c  te3EndBenignMall
12c13 6f 63 28 29 3b 0a 20 20 65 6e 61 62 6c 65 5f 73  oc();.  enable_s
12c14 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f  imulated_io_erro
12c15 72 73 28 29 3b 0a 20 20 50 41 47 45 52 54 52 41  rs();.  PAGERTRA
12c16 43 45 28 28 22 43 4c 4f 53 45 20 25 64 5c 6e 22  CE(("CLOSE %d\n"
12c17 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72  , PAGERID(pPager
12c18 29 29 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 28  )));.  IOTRACE((
12c19 22 43 4c 4f 53 45 20 25 70 5c 6e 22 2c 20 70 50  "CLOSE %p\n", pP
12c1a 61 67 65 72 29 29 0a 20 20 73 71 6c 69 74 65 33  ager)).  sqlite3
12c1b 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e  OsClose(pPager->
12c1c 66 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 61  fd);.  sqlite3Pa
12c1d 67 65 46 72 65 65 28 70 50 61 67 65 72 2d 3e 70  geFree(pPager->p
12c1e 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 73 71 6c  TmpSpace);.  sql
12c1f 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65 28  ite3PcacheClose(
12c20 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29  pPager->pPCache)
12c21 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ;..#ifdef SQLITE
12c22 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 69 66 28  _HAS_CODEC.  if(
12c23 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 46   pPager->xCodecF
12c24 72 65 65 20 29 20 70 50 61 67 65 72 2d 3e 78 43  ree ) pPager->xC
12c25 6f 64 65 63 46 72 65 65 28 70 50 61 67 65 72 2d  odecFree(pPager-
12c26 3e 70 43 6f 64 65 63 29 3b 0a 23 65 6e 64 69 66  >pCodec);.#endif
12c27 0a 0a 20 20 61 73 73 65 72 74 28 20 21 70 50 61  ..  assert( !pPa
12c28 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 20  ger->aSavepoint 
12c29 26 26 20 21 70 50 61 67 65 72 2d 3e 70 49 6e 4a  && !pPager->pInJ
12c2a 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 61 73 73 65  ournal );.  asse
12c2b 72 74 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67  rt( !isOpen(pPag
12c2c 65 72 2d 3e 6a 66 64 29 20 26 26 20 21 69 73 4f  er->jfd) && !isO
12c2d 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64  pen(pPager->sjfd
12c2e 29 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f  ) );..  sqlite3_
12c2f 66 72 65 65 28 70 50 61 67 65 72 29 3b 0a 20 20  free(pPager);.  
12c30 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
12c31 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65  ;.}..#if !define
12c32 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66  d(NDEBUG) || def
12c33 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54  ined(SQLITE_TEST
12c34 29 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  )./*.** Return t
12c35 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66  he page number f
12c36 6f 72 20 70 61 67 65 20 70 50 67 2e 0a 2a 2f 0a  or page pPg..*/.
12c37 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 50  SQLITE_PRIVATE P
12c38 67 6e 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72  gno sqlite3Pager
12c39 50 61 67 65 6e 75 6d 62 65 72 28 44 62 50 61 67  Pagenumber(DbPag
12c3a 65 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 75 72  e *pPg){.  retur
12c3b 6e 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 7d 0a 23  n pPg->pgno;.}.#
12c3c 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63  endif../*.** Inc
12c3d 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66 65 72  rement the refer
12c3e 65 6e 63 65 20 63 6f 75 6e 74 20 66 6f 72 20 70  ence count for p
12c3f 61 67 65 20 70 50 67 2e 0a 2a 2f 0a 53 51 4c 49  age pPg..*/.SQLI
12c40 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
12c41 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 28  sqlite3PagerRef(
12c42 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20  DbPage *pPg){.  
12c43 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66  sqlite3PcacheRef
12c44 28 70 50 67 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  (pPg);.}../*.** 
12c45 53 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c  Sync the journal
12c46 2e 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73  . In other words
12c47 2c 20 6d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20  , make sure all 
12c48 74 68 65 20 70 61 67 65 73 20 74 68 61 74 20 68  the pages that h
12c49 61 76 65 0a 2a 2a 20 62 65 65 6e 20 77 72 69 74  ave.** been writ
12c4a 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e  ten to the journ
12c4b 61 6c 20 68 61 76 65 20 61 63 74 75 61 6c 6c 79  al have actually
12c4c 20 72 65 61 63 68 65 64 20 74 68 65 20 73 75 72   reached the sur
12c4d 66 61 63 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64  face of the.** d
12c4e 69 73 6b 20 61 6e 64 20 63 61 6e 20 62 65 20 72  isk and can be r
12c4f 65 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 65  estored in the e
12c50 76 65 6e 74 20 6f 66 20 61 20 68 6f 74 2d 6a 6f  vent of a hot-jo
12c51 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 2e 0a  urnal rollback..
12c52 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 61 67  **.** If the Pag
12c53 65 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c 61 67  er.needSync flag
12c54 20 69 73 20 6e 6f 74 20 73 65 74 2c 20 74 68 65   is not set, the
12c55 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
12c56 69 73 20 61 0a 2a 2a 20 6e 6f 2d 6f 70 2e 20 4f  is a.** no-op. O
12c57 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 61 63  therwise, the ac
12c58 74 69 6f 6e 73 20 72 65 71 75 69 72 65 64 20 64  tions required d
12c59 65 70 65 6e 64 20 6f 6e 20 74 68 65 20 6a 6f 75  epend on the jou
12c5a 72 6e 61 6c 2d 6d 6f 64 65 0a 2a 2a 20 61 6e 64  rnal-mode.** and
12c5b 20 74 68 65 20 64 65 76 69 63 65 20 63 68 61 72   the device char
12c5c 61 63 74 65 72 69 73 74 69 63 73 20 6f 66 20 74  acteristics of t
12c5d 68 65 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74  he the file-syst
12c5e 65 6d 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  em, as follows:.
12c5f 2a 2a 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65  **.**   * If the
12c60 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73   journal file is
12c61 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f   an in-memory jo
12c62 75 72 6e 61 6c 20 66 69 6c 65 2c 20 6e 6f 20 61  urnal file, no a
12c63 63 74 69 6f 6e 20 6e 65 65 64 0a 2a 2a 20 20 20  ction need.**   
12c64 20 20 62 65 20 74 61 6b 65 6e 2e 0a 2a 2a 0a 2a    be taken..**.*
12c65 2a 20 20 20 2a 20 4f 74 68 65 72 77 69 73 65 2c  *   * Otherwise,
12c66 20 69 66 20 74 68 65 20 64 65 76 69 63 65 20 64   if the device d
12c67 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 20  oes not support 
12c68 74 68 65 20 53 41 46 45 5f 41 50 50 45 4e 44 20  the SAFE_APPEND 
12c69 70 72 6f 70 65 72 74 79 2c 0a 2a 2a 20 20 20 20  property,.**    
12c6a 20 74 68 65 6e 20 74 68 65 20 6e 52 65 63 20 66   then the nRec f
12c6b 69 65 6c 64 20 6f 66 20 74 68 65 20 6d 6f 73 74  ield of the most
12c6c 20 72 65 63 65 6e 74 6c 79 20 77 72 69 74 74 65   recently writte
12c6d 6e 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  n journal header
12c6e 0a 2a 2a 20 20 20 20 20 69 73 20 75 70 64 61 74  .**     is updat
12c6f 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68  ed to contain th
12c70 65 20 6e 75 6d 62 65 72 20 6f 66 20 6a 6f 75 72  e number of jour
12c71 6e 61 6c 20 72 65 63 6f 72 64 73 20 74 68 61 74  nal records that
12c72 20 68 61 76 65 0a 2a 2a 20 20 20 20 20 62 65 65   have.**     bee
12c73 6e 20 77 72 69 74 74 65 6e 20 66 6f 6c 6c 6f 77  n written follow
12c74 69 6e 67 20 69 74 2e 20 49 66 20 74 68 65 20 70  ing it. If the p
12c75 61 67 65 72 20 69 73 20 6f 70 65 72 61 74 69 6e  ager is operatin
12c76 67 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 0a 2a  g in full-sync.*
12c77 2a 20 20 20 20 20 6d 6f 64 65 2c 20 74 68 65 6e  *     mode, then
12c78 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
12c79 65 20 69 73 20 73 79 6e 63 65 64 20 62 65 66 6f  e is synced befo
12c7a 72 65 20 74 68 69 73 20 66 69 65 6c 64 20 69 73  re this field is
12c7b 20 75 70 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20   updated..**.** 
12c7c 20 20 2a 20 49 66 20 74 68 65 20 64 65 76 69 63    * If the devic
12c7d 65 20 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f  e does not suppo
12c7e 72 74 20 74 68 65 20 53 45 51 55 45 4e 54 49 41  rt the SEQUENTIA
12c7f 4c 20 70 72 6f 70 65 72 74 79 2c 20 74 68 65 6e  L property, then
12c80 20 0a 2a 2a 20 20 20 20 20 6a 6f 75 72 6e 61 6c   .**     journal
12c81 20 66 69 6c 65 20 69 73 20 73 79 6e 63 65 64 2e   file is synced.
12c82 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 69 6e 20 70 73  .**.** Or, in ps
12c83 65 75 64 6f 2d 63 6f 64 65 3a 0a 2a 2a 0a 2a 2a  eudo-code:.**.**
12c84 20 20 20 69 66 28 20 4e 4f 54 20 3c 69 6e 2d 6d     if( NOT <in-m
12c85 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 3e 20 29  emory journal> )
12c86 7b 0a 2a 2a 20 20 20 20 20 69 66 28 20 4e 4f 54  {.**     if( NOT
12c87 20 53 41 46 45 5f 41 50 50 45 4e 44 20 29 7b 0a   SAFE_APPEND ){.
12c88 2a 2a 20 20 20 20 20 20 20 69 66 28 20 3c 66 75  **       if( <fu
12c89 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 3e 20 29 20  ll-sync mode> ) 
12c8a 78 53 79 6e 63 28 3c 6a 6f 75 72 6e 61 6c 20 66  xSync(<journal f
12c8b 69 6c 65 3e 29 3b 0a 2a 2a 20 20 20 20 20 20 20  ile>);.**       
12c8c 3c 75 70 64 61 74 65 20 6e 52 65 63 20 66 69 65  <update nRec fie
12c8d 6c 64 3e 0a 2a 2a 20 20 20 20 20 7d 20 0a 2a 2a  ld>.**     } .**
12c8e 20 20 20 20 20 69 66 28 20 4e 4f 54 20 53 45 51       if( NOT SEQ
12c8f 55 45 4e 54 49 41 4c 20 29 20 78 53 79 6e 63 28  UENTIAL ) xSync(
12c90 3c 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 3e 29 3b  <journal file>);
12c91 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 54 68  .**   }.**.** Th
12c92 65 20 50 61 67 65 72 2e 6e 65 65 64 53 79 6e 63  e Pager.needSync
12c93 20 66 6c 61 67 20 69 73 20 6e 65 76 65 72 20 62   flag is never b
12c94 65 20 73 65 74 20 66 6f 72 20 74 65 6d 70 6f 72  e set for tempor
12c95 61 72 79 20 66 69 6c 65 73 2c 20 6f 72 20 61 6e  ary files, or an
12c96 79 0a 2a 2a 20 66 69 6c 65 20 6f 70 65 72 61 74  y.** file operat
12c97 69 6e 67 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d  ing in no-sync m
12c98 6f 64 65 20 28 50 61 67 65 72 2e 6e 6f 53 79 6e  ode (Pager.noSyn
12c99 63 20 73 65 74 20 74 6f 20 6e 6f 6e 2d 7a 65 72  c set to non-zer
12c9a 6f 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63  o)..**.** If suc
12c9b 63 65 73 73 66 75 6c 2c 20 74 68 69 73 20 72 6f  cessful, this ro
12c9c 75 74 69 6e 65 20 63 6c 65 61 72 73 20 74 68 65  utine clears the
12c9d 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43   PGHDR_NEED_SYNC
12c9e 20 66 6c 61 67 20 6f 66 20 65 76 65 72 79 20 0a   flag of every .
12c9f 2a 2a 20 70 61 67 65 20 63 75 72 72 65 6e 74 6c  ** page currentl
12ca0 79 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79  y held in memory
12ca1 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e   before returnin
12ca2 67 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20  g SQLITE_OK. If 
12ca3 61 6e 20 49 4f 0a 2a 2a 20 65 72 72 6f 72 20 69  an IO.** error i
12ca4 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 74  s encountered, t
12ca5 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f 72  hen the IO error
12ca6 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
12ca7 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e  d to the caller.
12ca8 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
12ca9 79 6e 63 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72  yncJournal(Pager
12caa 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28   *pPager){.  if(
12cab 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e   pPager->needSyn
12cac 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  c ){.    assert(
12cad 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69   !pPager->tempFi
12cae 6c 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50  le );.    if( pP
12caf 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64  ager->journalMod
12cb0 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e!=PAGER_JOURNAL
12cb1 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20  MODE_MEMORY ){. 
12cb2 20 20 20 20 20 69 6e 74 20 72 63 3b 20 20 20 20       int rc;    
12cb3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12cb4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
12cb5 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 20 20  urn code */.    
12cb6 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 44 63 20    const int iDc 
12cb7 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63  = sqlite3OsDevic
12cb8 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73  eCharacteristics
12cb9 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20  (pPager->fd);.  
12cba 20 20 20 20 61 73 73 65 72 74 28 20 69 73 4f 70      assert( isOp
12cbb 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
12cbc 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 30 3d  );..      if( 0=
12cbd 3d 28 69 44 63 26 53 51 4c 49 54 45 5f 49 4f 43  =(iDc&SQLITE_IOC
12cbe 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 29 20  AP_SAFE_APPEND) 
12cbf 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ){.        /* Th
12cc0 69 73 20 62 6c 6f 63 6b 20 64 65 61 6c 73 20 77  is block deals w
12cc1 69 74 68 20 61 6e 20 6f 62 73 63 75 72 65 20 70  ith an obscure p
12cc2 72 6f 62 6c 65 6d 2e 20 49 66 20 74 68 65 20 6c  roblem. If the l
12cc3 61 73 74 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 20  ast connection. 
12cc4 20 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 77         ** that w
12cc5 72 6f 74 65 20 74 6f 20 74 68 69 73 20 64 61 74  rote to this dat
12cc6 61 62 61 73 65 20 77 61 73 20 6f 70 65 72 61 74  abase was operat
12cc7 69 6e 67 20 69 6e 20 70 65 72 73 69 73 74 65 6e  ing in persisten
12cc8 74 2d 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 20  t-journal.      
12cc9 20 20 2a 2a 20 6d 6f 64 65 2c 20 74 68 65 6e 20    ** mode, then 
12cca 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
12ccb 20 6d 61 79 20 61 74 20 74 68 69 73 20 70 6f 69   may at this poi
12ccc 6e 74 20 61 63 74 75 61 6c 6c 79 20 62 65 20 6c  nt actually be l
12ccd 61 72 67 65 72 0a 20 20 20 20 20 20 20 20 2a 2a  arger.        **
12cce 20 74 68 61 6e 20 50 61 67 65 72 2e 6a 6f 75 72   than Pager.jour
12ccf 6e 61 6c 4f 66 66 20 62 79 74 65 73 2e 20 49 66  nalOff bytes. If
12cd0 20 74 68 65 20 6e 65 78 74 20 74 68 69 6e 67 20   the next thing 
12cd1 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20  in the journal. 
12cd2 20 20 20 20 20 20 20 2a 2a 20 66 69 6c 65 20 68         ** file h
12cd3 61 70 70 65 6e 73 20 74 6f 20 62 65 20 61 20 6a  appens to be a j
12cd4 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 28 77  ournal-header (w
12cd5 72 69 74 74 65 6e 20 61 73 20 70 61 72 74 20 6f  ritten as part o
12cd6 66 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a  f the.        **
12cd7 20 70 72 65 76 69 6f 75 73 20 63 6f 6e 6e 65 63   previous connec
12cd8 74 69 6f 6e 73 20 74 72 61 6e 73 61 63 74 69 6f  tions transactio
12cd9 6e 29 2c 20 61 6e 64 20 61 20 63 72 61 73 68 20  n), and a crash 
12cda 6f 72 20 70 6f 77 65 72 2d 66 61 69 6c 75 72 65  or power-failure
12cdb 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 63 63   .        ** occ
12cdc 75 72 73 20 61 66 74 65 72 20 6e 52 65 63 20 69  urs after nRec i
12cdd 73 20 75 70 64 61 74 65 64 20 62 75 74 20 62 65  s updated but be
12cde 66 6f 72 65 20 74 68 69 73 20 63 6f 6e 6e 65 63  fore this connec
12cdf 74 69 6f 6e 20 77 72 69 74 65 73 20 0a 20 20 20  tion writes .   
12ce0 20 20 20 20 20 2a 2a 20 61 6e 79 74 68 69 6e 67       ** anything
12ce1 20 65 6c 73 65 20 74 6f 20 74 68 65 20 6a 6f 75   else to the jou
12ce2 72 6e 61 6c 20 66 69 6c 65 20 28 6f 72 20 63 6f  rnal file (or co
12ce3 6d 6d 69 74 73 2f 72 6f 6c 6c 73 20 62 61 63 6b  mmits/rolls back
12ce4 20 69 74 73 20 0a 20 20 20 20 20 20 20 20 2a 2a   its .        **
12ce5 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2c 20 74   transaction), t
12ce6 68 65 6e 20 53 51 4c 69 74 65 20 6d 61 79 20 62  hen SQLite may b
12ce7 65 63 6f 6d 65 20 63 6f 6e 66 75 73 65 64 20 77  ecome confused w
12ce8 68 65 6e 20 64 6f 69 6e 67 20 74 68 65 20 0a 20  hen doing the . 
12ce9 20 20 20 20 20 20 20 2a 2a 20 68 6f 74 2d 6a 6f         ** hot-jo
12cea 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 20 66  urnal rollback f
12ceb 6f 6c 6c 6f 77 69 6e 67 20 72 65 63 6f 76 65 72  ollowing recover
12cec 79 2e 20 49 74 20 6d 61 79 20 72 6f 6c 6c 20 62  y. It may roll b
12ced 61 63 6b 20 61 6c 6c 0a 20 20 20 20 20 20 20 20  ack all.        
12cee 2a 2a 20 6f 66 20 74 68 69 73 20 63 6f 6e 6e 65  ** of this conne
12cef 63 74 69 6f 6e 73 20 64 61 74 61 2c 20 74 68 65  ctions data, the
12cf0 6e 20 70 72 6f 63 65 65 64 20 74 6f 20 72 6f 6c  n proceed to rol
12cf1 6c 69 6e 67 20 62 61 63 6b 20 74 68 65 20 6f 6c  ling back the ol
12cf2 64 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 75  d,.        ** ou
12cf3 74 2d 6f 66 2d 64 61 74 65 20 64 61 74 61 20 74  t-of-date data t
12cf4 68 61 74 20 66 6f 6c 6c 6f 77 73 20 69 74 2e 20  hat follows it. 
12cf5 44 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74  Database corrupt
12cf6 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a  ion..        **.
12cf7 20 20 20 20 20 20 20 20 2a 2a 20 54 6f 20 77 6f          ** To wo
12cf8 72 6b 20 61 72 6f 75 6e 64 20 74 68 69 73 2c 20  rk around this, 
12cf9 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  if the journal f
12cfa 69 6c 65 20 64 6f 65 73 20 61 70 70 65 61 72 20  ile does appear 
12cfb 74 6f 20 63 6f 6e 74 61 69 6e 0a 20 20 20 20 20  to contain.     
12cfc 20 20 20 2a 2a 20 61 20 76 61 6c 69 64 20 68 65     ** a valid he
12cfd 61 64 65 72 20 66 6f 6c 6c 6f 77 69 6e 67 20 50  ader following P
12cfe 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 2c  ager.journalOff,
12cff 20 74 68 65 6e 20 77 72 69 74 65 20 61 20 30 78   then write a 0x
12d00 30 30 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 79  00.        ** by
12d01 74 65 20 74 6f 20 74 68 65 20 73 74 61 72 74 20  te to the start 
12d02 6f 66 20 69 74 20 74 6f 20 70 72 65 76 65 6e 74  of it to prevent
12d03 20 69 74 20 66 72 6f 6d 20 62 65 69 6e 67 20 72   it from being r
12d04 65 63 6f 67 6e 69 7a 65 64 2e 0a 20 20 20 20 20  ecognized..     
12d05 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
12d06 20 56 61 72 69 61 62 6c 65 20 69 4e 65 78 74 48   Variable iNextH
12d07 64 72 4f 66 66 73 65 74 20 69 73 20 73 65 74 20  drOffset is set 
12d08 74 6f 20 74 68 65 20 6f 66 66 73 65 74 20 61 74  to the offset at
12d09 20 77 68 69 63 68 20 74 68 69 73 0a 20 20 20 20   which this.    
12d0a 20 20 20 20 2a 2a 20 70 72 6f 62 6c 65 6d 61 74      ** problemat
12d0b 69 63 20 68 65 61 64 65 72 20 77 69 6c 6c 20 6f  ic header will o
12d0c 63 63 75 72 2c 20 69 66 20 69 74 20 65 78 69 73  ccur, if it exis
12d0d 74 73 2e 20 61 4d 61 67 69 63 20 69 73 20 75 73  ts. aMagic is us
12d0e 65 64 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 61  ed .        ** a
12d0f 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20 62 75  s a temporary bu
12d10 66 66 65 72 20 74 6f 20 69 6e 73 70 65 63 74 20  ffer to inspect 
12d11 74 68 65 20 66 69 72 73 74 20 63 6f 75 70 6c 65  the first couple
12d12 20 6f 66 20 62 79 74 65 73 20 6f 66 0a 20 20 20   of bytes of.   
12d13 20 20 20 20 20 2a 2a 20 74 68 65 20 70 6f 74 65       ** the pote
12d14 6e 74 69 61 6c 20 6a 6f 75 72 6e 61 6c 20 68 65  ntial journal he
12d15 61 64 65 72 2e 0a 20 20 20 20 20 20 20 20 2a 2f  ader..        */
12d16 0a 20 20 20 20 20 20 20 20 69 36 34 20 69 4e 65  .        i64 iNe
12d17 78 74 48 64 72 4f 66 66 73 65 74 3b 0a 20 20 20  xtHdrOffset;.   
12d18 20 20 20 20 20 75 38 20 61 4d 61 67 69 63 5b 38       u8 aMagic[8
12d19 5d 3b 0a 09 75 38 20 7a 48 65 61 64 65 72 5b 73  ];..u8 zHeader[s
12d1a 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61  izeof(aJournalMa
12d1b 67 69 63 29 2b 34 5d 3b 0a 0a 09 6d 65 6d 63 70  gic)+4];...memcp
12d1c 79 28 7a 48 65 61 64 65 72 2c 20 61 4a 6f 75 72  y(zHeader, aJour
12d1d 6e 61 6c 4d 61 67 69 63 2c 20 73 69 7a 65 6f 66  nalMagic, sizeof
12d1e 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 29  (aJournalMagic))
12d1f 3b 0a 09 70 75 74 33 32 62 69 74 73 28 26 7a 48  ;..put32bits(&zH
12d20 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f  eader[sizeof(aJo
12d21 75 72 6e 61 6c 4d 61 67 69 63 29 5d 2c 20 70 50  urnalMagic)], pP
12d22 61 67 65 72 2d 3e 6e 52 65 63 29 3b 0a 0a 20 20  ager->nRec);..  
12d23 20 20 20 20 20 20 69 4e 65 78 74 48 64 72 4f 66        iNextHdrOf
12d24 66 73 65 74 20 3d 20 6a 6f 75 72 6e 61 6c 48 64  fset = journalHd
12d25 72 4f 66 66 73 65 74 28 70 50 61 67 65 72 29 3b  rOffset(pPager);
12d26 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
12d27 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 67  lite3OsRead(pPag
12d28 65 72 2d 3e 6a 66 64 2c 20 61 4d 61 67 69 63 2c  er->jfd, aMagic,
12d29 20 38 2c 20 69 4e 65 78 74 48 64 72 4f 66 66 73   8, iNextHdrOffs
12d2a 65 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  et);.        if(
12d2b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
12d2c 26 20 30 3d 3d 6d 65 6d 63 6d 70 28 61 4d 61 67  & 0==memcmp(aMag
12d2d 69 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69  ic, aJournalMagi
12d2e 63 2c 20 38 29 20 29 7b 0a 20 20 20 20 20 20 20  c, 8) ){.       
12d2f 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
12d30 75 38 20 7a 65 72 6f 62 79 74 65 20 3d 20 30 3b  u8 zerobyte = 0;
12d31 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
12d32 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70  sqlite3OsWrite(p
12d33 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 7a 65 72  Pager->jfd, &zer
12d34 6f 62 79 74 65 2c 20 31 2c 20 69 4e 65 78 74 48  obyte, 1, iNextH
12d35 64 72 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 20  drOffset);.     
12d36 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
12d37 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc!=SQLITE_OK &
12d38 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f 45  & rc!=SQLITE_IOE
12d39 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29 7b  RR_SHORT_READ ){
12d3a 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
12d3b 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a  n rc;.        }.
12d3c 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74  .        /* Writ
12d3d 65 20 74 68 65 20 6e 52 65 63 20 76 61 6c 75 65  e the nRec value
12d3e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61   into the journa
12d3f 6c 20 66 69 6c 65 20 68 65 61 64 65 72 2e 20 49  l file header. I
12d40 66 20 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20  f in.        ** 
12d41 66 75 6c 6c 2d 73 79 6e 63 68 72 6f 6e 6f 75 73  full-synchronous
12d42 20 6d 6f 64 65 2c 20 73 79 6e 63 20 74 68 65 20   mode, sync the 
12d43 6a 6f 75 72 6e 61 6c 20 66 69 72 73 74 2e 20 54  journal first. T
12d44 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 61 74  his ensures that
12d45 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 6c 20  .        ** all 
12d46 64 61 74 61 20 68 61 73 20 72 65 61 6c 6c 79 20  data has really 
12d47 68 69 74 20 74 68 65 20 64 69 73 6b 20 62 65 66  hit the disk bef
12d48 6f 72 65 20 6e 52 65 63 20 69 73 20 75 70 64 61  ore nRec is upda
12d49 74 65 64 20 74 6f 20 6d 61 72 6b 0a 20 20 20 20  ted to mark.    
12d4a 20 20 20 20 2a 2a 20 69 74 20 61 73 20 61 20 63      ** it as a c
12d4b 61 6e 64 69 64 61 74 65 20 66 6f 72 20 72 6f 6c  andidate for rol
12d4c 6c 62 61 63 6b 2e 0a 20 20 20 20 20 20 20 20 2a  lback..        *
12d4d 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54 68 69  *.        ** Thi
12d4e 73 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65  s is not require
12d4f 64 20 69 66 20 74 68 65 20 70 65 72 73 69 73 74  d if the persist
12d50 65 6e 74 20 6d 65 64 69 61 20 73 75 70 70 6f 72  ent media suppor
12d51 74 73 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a  ts the.        *
12d52 2a 20 53 41 46 45 5f 41 50 50 45 4e 44 20 70 72  * SAFE_APPEND pr
12d53 6f 70 65 72 74 79 2e 20 42 65 63 61 75 73 65 20  operty. Because 
12d54 69 6e 20 74 68 69 73 20 63 61 73 65 20 69 74 20  in this case it 
12d55 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  is not possible 
12d56 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 6f 72 20  .        ** for 
12d57 67 61 72 62 61 67 65 20 64 61 74 61 20 74 6f 20  garbage data to 
12d58 62 65 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74  be appended to t
12d59 68 65 20 66 69 6c 65 2c 20 74 68 65 20 6e 52 65  he file, the nRe
12d5a 63 20 66 69 65 6c 64 0a 20 20 20 20 20 20 20 20  c field.        
12d5b 2a 2a 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20  ** is populated 
12d5c 77 69 74 68 20 30 78 46 46 46 46 46 46 46 46 20  with 0xFFFFFFFF 
12d5d 77 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  when the journal
12d5e 20 68 65 61 64 65 72 20 69 73 20 77 72 69 74 74   header is writt
12d5f 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e  en.        ** an
12d60 64 20 6e 65 76 65 72 20 6e 65 65 64 73 20 74 6f  d never needs to
12d61 20 62 65 20 75 70 64 61 74 65 64 2e 0a 20 20 20   be updated..   
12d62 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
12d63 69 66 28 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c  if( pPager->full
12d64 53 79 6e 63 20 26 26 20 30 3d 3d 28 69 44 63 26  Sync && 0==(iDc&
12d65 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51  SQLITE_IOCAP_SEQ
12d66 55 45 4e 54 49 41 4c 29 20 29 7b 0a 20 20 20 20  UENTIAL) ){.    
12d67 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 45        PAGERTRACE
12d68 28 28 22 53 59 4e 43 20 6a 6f 75 72 6e 61 6c 20  (("SYNC journal 
12d69 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49  of %d\n", PAGERI
12d6a 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 20  D(pPager)));.   
12d6b 20 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28         IOTRACE((
12d6c 22 4a 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50  "JSYNC %p\n", pP
12d6d 61 67 65 72 29 29 0a 20 20 20 20 20 20 20 20 20  ager)).         
12d6e 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53   rc = sqlite3OsS
12d6f 79 6e 63 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  ync(pPager->jfd,
12d70 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c   pPager->sync_fl
12d71 61 67 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ags);.          
12d72 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
12d73 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  K ) return rc;. 
12d74 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
12d75 20 49 4f 54 52 41 43 45 28 28 22 4a 48 44 52 20   IOTRACE(("JHDR 
12d76 25 70 20 25 6c 6c 64 5c 6e 22 2c 20 70 50 61 67  %p %lld\n", pPag
12d77 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  er, pPager->jour
12d78 6e 61 6c 48 64 72 29 29 3b 0a 20 20 20 20 20 20  nalHdr));.      
12d79 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
12d7a 57 72 69 74 65 28 0a 20 20 20 20 20 20 20 20 20  Write(.         
12d7b 20 20 20 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20     pPager->jfd, 
12d7c 7a 48 65 61 64 65 72 2c 20 73 69 7a 65 6f 66 28  zHeader, sizeof(
12d7d 7a 48 65 61 64 65 72 29 2c 20 70 50 61 67 65 72  zHeader), pPager
12d7e 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 0a 09 29 3b  ->journalHdr..);
12d7f 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
12d80 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
12d81 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a  urn rc;.      }.
12d82 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 69 44        if( 0==(iD
12d83 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53  c&SQLITE_IOCAP_S
12d84 45 51 55 45 4e 54 49 41 4c 29 20 29 7b 0a 20 20  EQUENTIAL) ){.  
12d85 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 45        PAGERTRACE
12d86 28 28 22 53 59 4e 43 20 6a 6f 75 72 6e 61 6c 20  (("SYNC journal 
12d87 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49  of %d\n", PAGERI
12d88 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 20  D(pPager)));.   
12d89 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a       IOTRACE(("J
12d8a 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50 61 67  SYNC %p\n", pPag
12d8b 65 72 29 29 0a 20 20 20 20 20 20 20 20 72 63 20  er)).        rc 
12d8c 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28  = sqlite3OsSync(
12d8d 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61  pPager->jfd, pPa
12d8e 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 7c  ger->sync_flags|
12d8f 20 0a 20 20 20 20 20 20 20 20 20 20 28 70 50 61   .          (pPa
12d90 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 3d  ger->sync_flags=
12d91 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c  =SQLITE_SYNC_FUL
12d92 4c 3f 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41  L?SQLITE_SYNC_DA
12d93 54 41 4f 4e 4c 59 3a 30 29 0a 20 20 20 20 20 20  TAONLY:0).      
12d94 20 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28    );.        if(
12d95 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
12d96 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
12d97 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f    }.    }..    /
12d98 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  * The journal fi
12d99 6c 65 20 77 61 73 20 6a 75 73 74 20 73 75 63 63  le was just succ
12d9a 65 73 73 66 75 6c 6c 79 20 73 79 6e 63 65 64 2e  essfully synced.
12d9b 20 53 65 74 20 50 61 67 65 72 2e 6e 65 65 64 53   Set Pager.needS
12d9c 79 6e 63 20 0a 20 20 20 20 2a 2a 20 74 6f 20 7a  ync .    ** to z
12d9d 65 72 6f 20 61 6e 64 20 63 6c 65 61 72 20 74 68  ero and clear th
12d9e 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e  e PGHDR_NEED_SYN
12d9f 43 20 66 6c 61 67 20 6f 6e 20 61 6c 6c 20 70 61  C flag on all pa
12da0 67 65 73 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  gess..    */.   
12da1 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e   pPager->needSyn
12da2 63 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65  c = 0;.    pPage
12da3 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65  r->journalStarte
12da4 64 20 3d 20 31 3b 0a 20 20 20 20 73 71 6c 69 74  d = 1;.    sqlit
12da5 65 33 50 63 61 63 68 65 43 6c 65 61 72 53 79 6e  e3PcacheClearSyn
12da6 63 46 6c 61 67 73 28 70 50 61 67 65 72 2d 3e 70  cFlags(pPager->p
12da7 50 43 61 63 68 65 29 3b 0a 20 20 7d 0a 0a 20 20  PCache);.  }..  
12da8 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
12da9 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61  ;.}../*.** The a
12daa 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 66  rgument is the f
12dab 69 72 73 74 20 69 6e 20 61 20 6c 69 6e 6b 65 64  irst in a linked
12dac 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20 70   list of dirty p
12dad 61 67 65 73 20 63 6f 6e 6e 65 63 74 65 64 0a 2a  ages connected.*
12dae 2a 20 62 79 20 74 68 65 20 50 67 48 64 72 2e 70  * by the PgHdr.p
12daf 44 69 72 74 79 20 70 6f 69 6e 74 65 72 2e 20 54  Dirty pointer. T
12db0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 72 69  his function wri
12db1 74 65 73 20 65 61 63 68 20 6f 6e 65 20 6f 66 20  tes each one of 
12db2 74 68 65 0a 2a 2a 20 69 6e 2d 6d 65 6d 6f 72 79  the.** in-memory
12db3 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 6c 69   pages in the li
12db4 73 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  st to the databa
12db5 73 65 20 66 69 6c 65 2e 20 54 68 65 20 61 72 67  se file. The arg
12db6 75 6d 65 6e 74 20 6d 61 79 0a 2a 2a 20 62 65 20  ument may.** be 
12db7 4e 55 4c 4c 2c 20 72 65 70 72 65 73 65 6e 74 69  NULL, representi
12db8 6e 67 20 61 6e 20 65 6d 70 74 79 20 6c 69 73 74  ng an empty list
12db9 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74  . In this case t
12dba 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a  his function is.
12dbb 2a 2a 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a  ** a no-op..**.*
12dbc 2a 20 54 68 65 20 70 61 67 65 72 20 6d 75 73 74  * The pager must
12dbd 20 68 6f 6c 64 20 61 74 20 6c 65 61 73 74 20 61   hold at least a
12dbe 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 77   RESERVED lock w
12dbf 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
12dc0 6e 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 2e 20  n.** is called. 
12dc1 42 65 66 6f 72 65 20 77 72 69 74 69 6e 67 20 61  Before writing a
12dc2 6e 79 74 68 69 6e 67 20 74 6f 20 74 68 65 20 64  nything to the d
12dc3 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68  atabase file, th
12dc4 69 73 20 6c 6f 63 6b 0a 2a 2a 20 69 73 20 75 70  is lock.** is up
12dc5 67 72 61 64 65 64 20 74 6f 20 61 6e 20 45 58 43  graded to an EXC
12dc6 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 49 66 20  LUSIVE lock. If 
12dc7 74 68 65 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 20  the lock cannot 
12dc8 62 65 20 6f 62 74 61 69 6e 65 64 2c 0a 2a 2a 20  be obtained,.** 
12dc9 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72  SQLITE_BUSY is r
12dca 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64  eturned and no d
12dcb 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 20 74  ata is written t
12dcc 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  o the database f
12dcd 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74  ile..** .** If t
12dce 68 65 20 70 61 67 65 72 20 69 73 20 61 20 74 65  he pager is a te
12dcf 6d 70 2d 66 69 6c 65 20 70 61 67 65 72 20 61 6e  mp-file pager an
12dd0 64 20 74 68 65 20 61 63 74 75 61 6c 20 66 69 6c  d the actual fil
12dd1 65 2d 73 79 73 74 65 6d 20 66 69 6c 65 0a 2a 2a  e-system file.**
12dd2 20 69 73 20 6e 6f 74 20 79 65 74 20 6f 70 65 6e   is not yet open
12dd3 2c 20 69 74 20 69 73 20 63 72 65 61 74 65 64 20  , it is created 
12dd4 61 6e 64 20 6f 70 65 6e 65 64 20 62 65 66 6f 72  and opened befor
12dd5 65 20 61 6e 79 20 64 61 74 61 20 69 73 20 0a 2a  e any data is .*
12dd6 2a 20 77 72 69 74 74 65 6e 20 6f 75 74 2e 0a 2a  * written out..*
12dd7 2a 0a 2a 2a 20 4f 6e 63 65 20 74 68 65 20 6c 6f  *.** Once the lo
12dd8 63 6b 20 68 61 73 20 62 65 65 6e 20 75 70 67 72  ck has been upgr
12dd9 61 64 65 64 20 61 6e 64 2c 20 69 66 20 6e 65 63  aded and, if nec
12dda 65 73 73 61 72 79 2c 20 74 68 65 20 66 69 6c 65  essary, the file
12ddb 20 6f 70 65 6e 65 64 2c 0a 2a 2a 20 74 68 65 20   opened,.** the 
12ddc 70 61 67 65 73 20 61 72 65 20 77 72 69 74 74 65  pages are writte
12ddd 6e 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74  n out to the dat
12dde 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20 6c 69  abase file in li
12ddf 73 74 20 6f 72 64 65 72 2e 20 57 72 69 74 69 6e  st order. Writin
12de0 67 0a 2a 2a 20 61 20 70 61 67 65 20 69 73 20 73  g.** a page is s
12de1 6b 69 70 70 65 64 20 69 66 20 69 74 20 6d 65 65  kipped if it mee
12de2 74 73 20 65 69 74 68 65 72 20 6f 66 20 74 68 65  ts either of the
12de3 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 72 69 74 65   following crite
12de4 72 69 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54  ria:.**.**   * T
12de5 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69  he page number i
12de6 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 50  s greater than P
12de7 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 6f 72 0a  ager.dbSize, or.
12de8 2a 2a 20 20 20 2a 20 54 68 65 20 50 47 48 44 52  **   * The PGHDR
12de9 5f 44 4f 4e 54 5f 57 52 49 54 45 20 66 6c 61 67  _DONT_WRITE flag
12dea 20 69 73 20 73 65 74 20 6f 6e 20 74 68 65 20 70   is set on the p
12deb 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77 72  age..**.** If wr
12dec 69 74 69 6e 67 20 6f 75 74 20 61 20 70 61 67 65  iting out a page
12ded 20 63 61 75 73 65 73 20 74 68 65 20 64 61 74 61   causes the data
12dee 62 61 73 65 20 66 69 6c 65 20 74 6f 20 67 72 6f  base file to gro
12def 77 2c 20 50 61 67 65 72 2e 64 62 46 69 6c 65 53  w, Pager.dbFileS
12df0 69 7a 65 0a 2a 2a 20 69 73 20 75 70 64 61 74 65  ize.** is update
12df1 64 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 20 49  d accordingly. I
12df2 66 20 70 61 67 65 20 31 20 69 73 20 77 72 69 74  f page 1 is writ
12df3 74 65 6e 20 6f 75 74 2c 20 74 68 65 6e 20 74 68  ten out, then th
12df4 65 20 76 61 6c 75 65 20 63 61 63 68 65 64 0a 2a  e value cached.*
12df5 2a 20 69 6e 20 50 61 67 65 72 2e 64 62 46 69 6c  * in Pager.dbFil
12df6 65 56 65 72 73 5b 5d 20 69 73 20 75 70 64 61 74  eVers[] is updat
12df7 65 64 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20  ed to match the 
12df8 6e 65 77 20 76 61 6c 75 65 20 73 74 6f 72 65 64  new value stored
12df9 20 69 6e 0a 2a 2a 20 74 68 65 20 64 61 74 61 62   in.** the datab
12dfa 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  ase file..**.** 
12dfb 49 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 73  If everything is
12dfc 20 73 75 63 63 65 73 73 66 75 6c 2c 20 53 51 4c   successful, SQL
12dfd 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
12dfe 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72  ed. If an IO err
12dff 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 73 2c 20 61  or .** occurs, a
12e00 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20  n IO error code 
12e01 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 72 2c  is returned. Or,
12e02 20 69 66 20 74 68 65 20 45 58 43 4c 55 53 49 56   if the EXCLUSIV
12e03 45 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 0a 2a 2a  E lock cannot.**
12e04 20 62 65 20 6f 62 74 61 69 6e 65 64 2c 20 53 51   be obtained, SQ
12e05 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 65 74  LITE_BUSY is ret
12e06 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  urned..*/.static
12e07 20 69 6e 74 20 70 61 67 65 72 5f 77 72 69 74 65   int pager_write
12e08 5f 70 61 67 65 6c 69 73 74 28 50 67 48 64 72 20  _pagelist(PgHdr 
12e09 2a 70 4c 69 73 74 29 7b 0a 20 20 50 61 67 65 72  *pList){.  Pager
12e0a 20 2a 70 50 61 67 65 72 3b 20 20 20 20 20 20 20   *pPager;       
12e0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12e0c 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20  /* Pager object 
12e0d 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  */.  int rc;    
12e0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12e0f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
12e10 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 69  urn code */..  i
12e11 66 28 20 4e 45 56 45 52 28 70 4c 69 73 74 3d 3d  f( NEVER(pList==
12e12 30 29 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  0) ) return SQLI
12e13 54 45 5f 4f 4b 3b 0a 20 20 70 50 61 67 65 72 20  TE_OK;.  pPager 
12e14 3d 20 70 4c 69 73 74 2d 3e 70 50 61 67 65 72 3b  = pList->pPager;
12e15 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70  ..  /* At this p
12e16 6f 69 6e 74 20 74 68 65 72 65 20 6d 61 79 20 62  oint there may b
12e17 65 20 65 69 74 68 65 72 20 61 20 52 45 53 45 52  e either a RESER
12e18 56 45 44 20 6f 72 20 45 58 43 4c 55 53 49 56 45  VED or EXCLUSIVE
12e19 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20 2a   lock on the.  *
12e1a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  * database file.
12e1b 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6c 72   If there is alr
12e1c 65 61 64 79 20 61 6e 20 45 58 43 4c 55 53 49 56  eady an EXCLUSIV
12e1d 45 20 6c 6f 63 6b 2c 20 74 68 65 20 66 6f 6c 6c  E lock, the foll
12e1e 6f 77 69 6e 67 0a 20 20 2a 2a 20 63 61 6c 6c 20  owing.  ** call 
12e1f 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2a  is a no-op..  **
12e20 0a 20 20 2a 2a 20 4d 6f 76 69 6e 67 20 74 68 65  .  ** Moving the
12e21 20 6c 6f 63 6b 20 66 72 6f 6d 20 52 45 53 45 52   lock from RESER
12e22 56 45 44 20 74 6f 20 45 58 43 4c 55 53 49 56 45  VED to EXCLUSIVE
12e23 20 61 63 74 75 61 6c 6c 79 20 69 6e 76 6f 6c 76   actually involv
12e24 65 73 20 67 6f 69 6e 67 0a 20 20 2a 2a 20 74 68  es going.  ** th
12e25 72 6f 75 67 68 20 61 6e 20 69 6e 74 65 72 6d 65  rough an interme
12e26 64 69 61 74 65 20 73 74 61 74 65 20 50 45 4e 44  diate state PEND
12e27 49 4e 47 2e 20 20 20 41 20 50 45 4e 44 49 4e 47  ING.   A PENDING
12e28 20 6c 6f 63 6b 20 70 72 65 76 65 6e 74 73 20 6e   lock prevents n
12e29 65 77 0a 20 20 2a 2a 20 72 65 61 64 65 72 73 20  ew.  ** readers 
12e2a 66 72 6f 6d 20 61 74 74 61 63 68 69 6e 67 20 74  from attaching t
12e2b 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62  o the database b
12e2c 75 74 20 69 73 20 75 6e 73 75 66 66 69 63 69 65  ut is unsufficie
12e2d 6e 74 20 66 6f 72 20 75 73 20 74 6f 0a 20 20 2a  nt for us to.  *
12e2e 2a 20 77 72 69 74 65 2e 20 20 54 68 65 20 69 64  * write.  The id
12e2f 65 61 20 6f 66 20 61 20 50 45 4e 44 49 4e 47 20  ea of a PENDING 
12e30 6c 6f 63 6b 20 69 73 20 74 6f 20 70 72 65 76 65  lock is to preve
12e31 6e 74 20 6e 65 77 20 72 65 61 64 65 72 73 20 66  nt new readers f
12e32 72 6f 6d 0a 20 20 2a 2a 20 63 6f 6d 69 6e 67 20  rom.  ** coming 
12e33 69 6e 20 77 68 69 6c 65 20 77 65 20 77 61 69 74  in while we wait
12e34 20 66 6f 72 20 65 78 69 73 74 69 6e 67 20 72 65   for existing re
12e35 61 64 65 72 73 20 74 6f 20 63 6c 65 61 72 2e 0a  aders to clear..
12e36 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 69 6c 65 20    **.  ** While 
12e37 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20  the pager is in 
12e38 74 68 65 20 52 45 53 45 52 56 45 44 20 73 74 61  the RESERVED sta
12e39 74 65 2c 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  te, the original
12e3a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 20   database file. 
12e3b 20 2a 2a 20 69 73 20 75 6e 63 68 61 6e 67 65 64   ** is unchanged
12e3c 20 61 6e 64 20 77 65 20 63 61 6e 20 72 6f 6c 6c   and we can roll
12e3d 62 61 63 6b 20 77 69 74 68 6f 75 74 20 68 61 76  back without hav
12e3e 69 6e 67 20 74 6f 20 70 6c 61 79 62 61 63 6b 20  ing to playback 
12e3f 74 68 65 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c  the.  ** journal
12e40 20 69 6e 74 6f 20 74 68 65 20 6f 72 69 67 69 6e   into the origin
12e41 61 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  al database file
12e42 2e 20 20 4f 6e 63 65 20 77 65 20 74 72 61 6e 73  .  Once we trans
12e43 69 74 69 6f 6e 20 74 6f 0a 20 20 2a 2a 20 45 58  ition to.  ** EX
12e44 43 4c 55 53 49 56 45 2c 20 69 74 20 6d 65 61 6e  CLUSIVE, it mean
12e45 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  s the database f
12e46 69 6c 65 20 68 61 73 20 62 65 65 6e 20 63 68 61  ile has been cha
12e47 6e 67 65 64 20 61 6e 64 20 61 6e 79 20 72 6f 6c  nged and any rol
12e48 6c 62 61 63 6b 0a 20 20 2a 2a 20 77 69 6c 6c 20  lback.  ** will 
12e49 72 65 71 75 69 72 65 20 61 20 6a 6f 75 72 6e 61  require a journa
12e4a 6c 20 70 6c 61 79 62 61 63 6b 2e 0a 20 20 2a 2f  l playback..  */
12e4b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
12e4c 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f  r->state>=PAGER_
12e4d 52 45 53 45 52 56 45 44 20 29 3b 0a 20 20 72 63  RESERVED );.  rc
12e4e 20 3d 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e   = pager_wait_on
12e4f 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c 20 45 58  _lock(pPager, EX
12e50 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b 0a 0a  CLUSIVE_LOCK);..
12e51 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65    /* If the file
12e52 20 69 73 20 61 20 74 65 6d 70 2d 66 69 6c 65 20   is a temp-file 
12e53 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e  has not yet been
12e54 20 6f 70 65 6e 65 64 2c 20 6f 70 65 6e 20 69 74   opened, open it
12e55 20 6e 6f 77 2e 20 49 74 0a 20 20 2a 2a 20 69 73   now. It.  ** is
12e56 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 66 6f   not possible fo
12e57 72 20 72 63 20 74 6f 20 62 65 20 6f 74 68 65 72  r rc to be other
12e58 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 4f 4b 20   than SQLITE_OK 
12e59 69 66 20 74 68 69 73 20 62 72 61 6e 63 68 0a 20  if this branch. 
12e5a 20 2a 2a 20 69 73 20 74 61 6b 65 6e 2c 20 61 73   ** is taken, as
12e5b 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c   pager_wait_on_l
12e5c 6f 63 6b 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70  ock() is a no-op
12e5d 20 66 6f 72 20 74 65 6d 70 2d 66 69 6c 65 73 2e   for temp-files.
12e5e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 69 73 4f  .  */.  if( !isO
12e5f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20  pen(pPager->fd) 
12e60 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
12e61 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20  Pager->tempFile 
12e62 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  && rc==SQLITE_OK
12e63 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 70 61 67   );.    rc = pag
12e64 65 72 4f 70 65 6e 74 65 6d 70 28 70 50 61 67 65  erOpentemp(pPage
12e65 72 2c 20 70 50 61 67 65 72 2d 3e 66 64 2c 20 70  r, pPager->fd, p
12e66 50 61 67 65 72 2d 3e 76 66 73 46 6c 61 67 73 29  Pager->vfsFlags)
12e67 3b 0a 20 20 7d 0a 0a 20 20 77 68 69 6c 65 28 20  ;.  }..  while( 
12e68 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
12e69 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 50 67   pList ){.    Pg
12e6a 6e 6f 20 70 67 6e 6f 20 3d 20 70 4c 69 73 74 2d  no pgno = pList-
12e6b 3e 70 67 6e 6f 3b 0a 0a 20 20 20 20 2f 2a 20 49  >pgno;..    /* I
12e6c 66 20 74 68 65 72 65 20 61 72 65 20 64 69 72 74  f there are dirt
12e6d 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 70  y pages in the p
12e6e 61 67 65 20 63 61 63 68 65 20 77 69 74 68 20 70  age cache with p
12e6f 61 67 65 20 6e 75 6d 62 65 72 73 20 67 72 65 61  age numbers grea
12e70 74 65 72 0a 20 20 20 20 2a 2a 20 74 68 61 6e 20  ter.    ** than 
12e71 50 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 74 68  Pager.dbSize, th
12e72 69 73 20 6d 65 61 6e 73 20 73 71 6c 69 74 65 33  is means sqlite3
12e73 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61  PagerTruncateIma
12e74 67 65 28 29 20 77 61 73 20 63 61 6c 6c 65 64 20  ge() was called 
12e75 74 6f 0a 20 20 20 20 2a 2a 20 6d 61 6b 65 20 74  to.    ** make t
12e76 68 65 20 66 69 6c 65 20 73 6d 61 6c 6c 65 72 20  he file smaller 
12e77 28 70 72 65 73 75 6d 61 62 6c 79 20 62 79 20 61  (presumably by a
12e78 75 74 6f 2d 76 61 63 75 75 6d 20 63 6f 64 65 29  uto-vacuum code)
12e79 2e 20 44 6f 20 6e 6f 74 20 77 72 69 74 65 0a 20  . Do not write. 
12e7a 20 20 20 2a 2a 20 61 6e 79 20 73 75 63 68 20 70     ** any such p
12e7b 61 67 65 73 20 74 6f 20 74 68 65 20 66 69 6c 65  ages to the file
12e7c 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
12e7d 41 6c 73 6f 2c 20 64 6f 20 6e 6f 74 20 77 72 69  Also, do not wri
12e7e 74 65 20 6f 75 74 20 61 6e 79 20 70 61 67 65 20  te out any page 
12e7f 74 68 61 74 20 68 61 73 20 74 68 65 20 50 47 48  that has the PGH
12e80 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 20 66 6c  DR_DONT_WRITE fl
12e81 61 67 0a 20 20 20 20 2a 2a 20 73 65 74 20 28 73  ag.    ** set (s
12e82 65 74 20 62 79 20 73 71 6c 69 74 65 33 50 61 67  et by sqlite3Pag
12e83 65 72 44 6f 6e 74 57 72 69 74 65 28 29 29 2e 0a  erDontWrite())..
12e84 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
12e85 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 53  gno<=pPager->dbS
12e86 69 7a 65 20 26 26 20 30 3d 3d 28 70 4c 69 73 74  ize && 0==(pList
12e87 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 4f  ->flags&PGHDR_DO
12e88 4e 54 5f 57 52 49 54 45 29 20 29 7b 0a 20 20 20  NT_WRITE) ){.   
12e89 20 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20     i64 offset = 
12e8a 28 70 67 6e 6f 2d 31 29 2a 28 69 36 34 29 70 50  (pgno-1)*(i64)pP
12e8b 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 20  ager->pageSize; 
12e8c 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 77    /* Offset to w
12e8d 72 69 74 65 20 2a 2f 0a 20 20 20 20 20 20 63 68  rite */.      ch
12e8e 61 72 20 2a 70 44 61 74 61 3b 20 20 20 20 20 20  ar *pData;      
12e8f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12e90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
12e91 44 61 74 61 20 74 6f 20 77 72 69 74 65 20 2a 2f  Data to write */
12e92 20 20 20 20 0a 0a 20 20 20 20 20 20 2f 2a 20 45      ..      /* E
12e93 6e 63 6f 64 65 20 74 68 65 20 64 61 74 61 62 61  ncode the databa
12e94 73 65 20 2a 2f 0a 20 20 20 20 20 20 43 4f 44 45  se */.      CODE
12e95 43 32 28 70 50 61 67 65 72 2c 20 70 4c 69 73 74  C2(pPager, pList
12e96 2d 3e 70 44 61 74 61 2c 20 70 67 6e 6f 2c 20 36  ->pData, pgno, 6
12e97 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  , return SQLITE_
12e98 4e 4f 4d 45 4d 2c 20 70 44 61 74 61 29 3b 0a 0a  NOMEM, pData);..
12e99 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 6f        /* Write o
12e9a 75 74 20 74 68 65 20 70 61 67 65 20 64 61 74 61  ut the page data
12e9b 2e 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20  . */.      rc = 
12e9c 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70  sqlite3OsWrite(p
12e9d 50 61 67 65 72 2d 3e 66 64 2c 20 70 44 61 74 61  Pager->fd, pData
12e9e 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  , pPager->pageSi
12e9f 7a 65 2c 20 6f 66 66 73 65 74 29 3b 0a 0a 20 20  ze, offset);..  
12ea0 20 20 20 20 2f 2a 20 49 66 20 70 61 67 65 20 31      /* If page 1
12ea1 20 77 61 73 20 6a 75 73 74 20 77 72 69 74 74 65   was just writte
12ea2 6e 2c 20 75 70 64 61 74 65 20 50 61 67 65 72 2e  n, update Pager.
12ea3 64 62 46 69 6c 65 56 65 72 73 20 74 6f 20 6d 61  dbFileVers to ma
12ea4 74 63 68 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  tch.      ** the
12ea5 20 76 61 6c 75 65 20 6e 6f 77 20 73 74 6f 72 65   value now store
12ea6 64 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  d in the databas
12ea7 65 20 66 69 6c 65 2e 20 49 66 20 77 72 69 74 69  e file. If writi
12ea8 6e 67 20 74 68 69 73 20 0a 20 20 20 20 20 20 2a  ng this .      *
12ea9 2a 20 70 61 67 65 20 63 61 75 73 65 64 20 74 68  * page caused th
12eaa 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
12eab 74 6f 20 67 72 6f 77 2c 20 75 70 64 61 74 65 20  to grow, update 
12eac 64 62 46 69 6c 65 53 69 7a 65 2e 20 0a 20 20 20  dbFileSize. .   
12ead 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20     */.      if( 
12eae 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 20  pgno==1 ){.     
12eaf 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65     memcpy(&pPage
12eb0 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 26  r->dbFileVers, &
12eb1 70 44 61 74 61 5b 32 34 5d 2c 20 73 69 7a 65 6f  pData[24], sizeo
12eb2 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65  f(pPager->dbFile
12eb3 56 65 72 73 29 29 3b 0a 20 20 20 20 20 20 7d 0a  Vers));.      }.
12eb4 20 20 20 20 20 20 69 66 28 20 70 67 6e 6f 3e 70        if( pgno>p
12eb5 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a  Pager->dbFileSiz
12eb6 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50 61  e ){.        pPa
12eb7 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20  ger->dbFileSize 
12eb8 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a  = pgno;.      }.
12eb9 0a 20 20 20 20 20 20 2f 2a 20 55 70 64 61 74 65  .      /* Update
12eba 20 61 6e 79 20 62 61 63 6b 75 70 20 6f 62 6a 65   any backup obje
12ebb 63 74 73 20 63 6f 70 79 69 6e 67 20 74 68 65 20  cts copying the 
12ebc 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 69 73  contents of this
12ebd 20 70 61 67 65 72 2e 20 2a 2f 0a 20 20 20 20 20   pager. */.     
12ebe 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70   sqlite3BackupUp
12ebf 64 61 74 65 28 70 50 61 67 65 72 2d 3e 70 42 61  date(pPager->pBa
12ec0 63 6b 75 70 2c 20 70 67 6e 6f 2c 20 28 75 38 2a  ckup, pgno, (u8*
12ec1 29 70 4c 69 73 74 2d 3e 70 44 61 74 61 29 3b 0a  )pList->pData);.
12ec2 0a 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43  .      PAGERTRAC
12ec3 45 28 28 22 53 54 4f 52 45 20 25 64 20 70 61 67  E(("STORE %d pag
12ec4 65 20 25 64 20 68 61 73 68 28 25 30 38 78 29 5c  e %d hash(%08x)\
12ec5 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
12ec6 20 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70         PAGERID(p
12ec7 50 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20 70 61  Pager), pgno, pa
12ec8 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 4c 69  ger_pagehash(pLi
12ec9 73 74 29 29 29 3b 0a 20 20 20 20 20 20 49 4f 54  st)));.      IOT
12eca 52 41 43 45 28 28 22 50 47 4f 55 54 20 25 70 20  RACE(("PGOUT %p 
12ecb 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70  %d\n", pPager, p
12ecc 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 50 41 47  gno));.      PAG
12ecd 45 52 5f 49 4e 43 52 28 73 71 6c 69 74 65 33 5f  ER_INCR(sqlite3_
12ece 70 61 67 65 72 5f 77 72 69 74 65 64 62 5f 63 6f  pager_writedb_co
12ecf 75 6e 74 29 3b 0a 20 20 20 20 20 20 50 41 47 45  unt);.      PAGE
12ed0 52 5f 49 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e  R_INCR(pPager->n
12ed1 57 72 69 74 65 29 3b 0a 20 20 20 20 7d 65 6c 73  Write);.    }els
12ed2 65 7b 0a 20 20 20 20 20 20 50 41 47 45 52 54 52  e{.      PAGERTR
12ed3 41 43 45 28 28 22 4e 4f 53 54 4f 52 45 20 25 64  ACE(("NOSTORE %d
12ed4 20 70 61 67 65 20 25 64 5c 6e 22 2c 20 50 41 47   page %d\n", PAG
12ed5 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 67  ERID(pPager), pg
12ed6 6e 6f 29 29 3b 0a 20 20 20 20 7d 0a 23 69 66 64  no));.    }.#ifd
12ed7 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f  ef SQLITE_CHECK_
12ed8 50 41 47 45 53 0a 20 20 20 20 70 4c 69 73 74 2d  PAGES.    pList-
12ed9 3e 70 61 67 65 48 61 73 68 20 3d 20 70 61 67 65  >pageHash = page
12eda 72 5f 70 61 67 65 68 61 73 68 28 70 4c 69 73 74  r_pagehash(pList
12edb 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 4c  );.#endif.    pL
12edc 69 73 74 20 3d 20 70 4c 69 73 74 2d 3e 70 44 69  ist = pList->pDi
12edd 72 74 79 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  rty;.  }..  retu
12ede 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
12edf 41 70 70 65 6e 64 20 61 20 72 65 63 6f 72 64 20  Append a record 
12ee0 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 73  of the current s
12ee1 74 61 74 65 20 6f 66 20 70 61 67 65 20 70 50 67  tate of page pPg
12ee2 20 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72   to the sub-jour
12ee3 6e 61 6c 2e 20 0a 2a 2a 20 49 74 20 69 73 20 74  nal. .** It is t
12ee4 68 65 20 63 61 6c 6c 65 72 73 20 72 65 73 70 6f  he callers respo
12ee5 6e 73 69 62 69 6c 69 74 79 20 74 6f 20 75 73 65  nsibility to use
12ee6 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67   subjRequiresPag
12ee7 65 28 29 20 74 6f 20 63 68 65 63 6b 20 0a 2a 2a  e() to check .**
12ee8 20 74 68 61 74 20 69 74 20 69 73 20 72 65 61 6c   that it is real
12ee9 6c 79 20 72 65 71 75 69 72 65 64 20 62 65 66 6f  ly required befo
12eea 72 65 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20  re calling this 
12eeb 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  function..**.** 
12eec 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 73  If successful, s
12eed 65 74 20 74 68 65 20 62 69 74 20 63 6f 72 72 65  et the bit corre
12eee 73 70 6f 6e 64 69 6e 67 20 74 6f 20 70 50 67 2d  sponding to pPg-
12eef 3e 70 67 6e 6f 20 69 6e 20 74 68 65 20 62 69 74  >pgno in the bit
12ef0 76 65 63 73 0a 2a 2a 20 66 6f 72 20 61 6c 6c 20  vecs.** for all 
12ef1 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 73 20  open savepoints 
12ef2 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  before returning
12ef3 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ..**.** This fun
12ef4 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 53 51  ction returns SQ
12ef5 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79  LITE_OK if every
12ef6 74 68 69 6e 67 20 69 73 20 73 75 63 63 65 73 73  thing is success
12ef7 66 75 6c 2c 20 61 6e 20 49 4f 0a 2a 2a 20 65 72  ful, an IO.** er
12ef8 72 6f 72 20 63 6f 64 65 20 69 66 20 74 68 65 20  ror code if the 
12ef9 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65  attempt to write
12efa 20 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72   to the sub-jour
12efb 6e 61 6c 20 66 61 69 6c 73 2c 20 6f 72 20 0a 2a  nal fails, or .*
12efc 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69  * SQLITE_NOMEM i
12efd 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73  f a malloc fails
12efe 20 77 68 69 6c 65 20 73 65 74 74 69 6e 67 20 61   while setting a
12eff 20 62 69 74 20 69 6e 20 61 20 73 61 76 65 70 6f   bit in a savepo
12f00 69 6e 74 0a 2a 2a 20 62 69 74 76 65 63 2e 0a 2a  int.** bitvec..*
12f01 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 75 62  /.static int sub
12f02 6a 6f 75 72 6e 61 6c 50 61 67 65 28 50 67 48 64  journalPage(PgHd
12f03 72 20 2a 70 50 67 29 7b 0a 20 20 69 6e 74 20 72  r *pPg){.  int r
12f04 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
12f05 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d   Pager *pPager =
12f06 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20   pPg->pPager;.  
12f07 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65  if( isOpen(pPage
12f08 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 20  r->sjfd) ){.    
12f09 76 6f 69 64 20 2a 70 44 61 74 61 20 3d 20 70 50  void *pData = pP
12f0a 67 2d 3e 70 44 61 74 61 3b 0a 20 20 20 20 69 36  g->pData;.    i6
12f0b 34 20 6f 66 66 73 65 74 20 3d 20 70 50 61 67 65  4 offset = pPage
12f0c 72 2d 3e 6e 53 75 62 52 65 63 2a 28 34 2b 70 50  r->nSubRec*(4+pP
12f0d 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b  ager->pageSize);
12f0e 0a 20 20 20 20 63 68 61 72 20 2a 70 44 61 74 61  .    char *pData
12f0f 32 3b 0a 0a 20 20 20 20 43 4f 44 45 43 32 28 70  2;..    CODEC2(p
12f10 50 61 67 65 72 2c 20 70 44 61 74 61 2c 20 70 50  Pager, pData, pP
12f11 67 2d 3e 70 67 6e 6f 2c 20 37 2c 20 72 65 74 75  g->pgno, 7, retu
12f12 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c  rn SQLITE_NOMEM,
12f13 20 70 44 61 74 61 32 29 3b 0a 20 20 20 20 50 41   pData2);.    PA
12f14 47 45 52 54 52 41 43 45 28 28 22 53 54 4d 54 2d  GERTRACE(("STMT-
12f15 4a 4f 55 52 4e 41 4c 20 25 64 20 70 61 67 65 20  JOURNAL %d page 
12f16 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70  %d\n", PAGERID(p
12f17 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e  Pager), pPg->pgn
12f18 6f 29 29 3b 0a 20 20 0a 20 20 20 20 61 73 73 65  o));.  .    asse
12f19 72 74 28 20 70 61 67 65 49 6e 4a 6f 75 72 6e 61  rt( pageInJourna
12f1a 6c 28 70 50 67 29 20 7c 7c 20 70 50 67 2d 3e 70  l(pPg) || pPg->p
12f1b 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62 4f 72  gno>pPager->dbOr
12f1c 69 67 53 69 7a 65 20 29 3b 0a 20 20 20 20 72 63  igSize );.    rc
12f1d 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70   = write32bits(p
12f1e 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 6f 66 66  Pager->sjfd, off
12f1f 73 65 74 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b  set, pPg->pgno);
12f20 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
12f21 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
12f22 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72  rc = sqlite3OsWr
12f23 69 74 65 28 70 50 61 67 65 72 2d 3e 73 6a 66 64  ite(pPager->sjfd
12f24 2c 20 70 44 61 74 61 32 2c 20 70 50 61 67 65 72  , pData2, pPager
12f25 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66 66 73  ->pageSize, offs
12f26 65 74 2b 34 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  et+4);.    }.  }
12f27 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
12f28 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61 67  E_OK ){.    pPag
12f29 65 72 2d 3e 6e 53 75 62 52 65 63 2b 2b 3b 0a 20  er->nSubRec++;. 
12f2a 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
12f2b 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3e 30 20  r->nSavepoint>0 
12f2c 29 3b 0a 20 20 20 20 72 63 20 3d 20 61 64 64 54  );.    rc = addT
12f2d 6f 53 61 76 65 70 6f 69 6e 74 42 69 74 76 65 63  oSavepointBitvec
12f2e 73 28 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70  s(pPager, pPg->p
12f2f 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  gno);.  }.  retu
12f30 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  rn rc;.}.../*.**
12f31 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
12f32 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20  s called by the 
12f33 70 63 61 63 68 65 20 6c 61 79 65 72 20 77 68 65  pcache layer whe
12f34 6e 20 69 74 20 68 61 73 20 72 65 61 63 68 65 64  n it has reached
12f35 20 73 6f 6d 65 0a 2a 2a 20 73 6f 66 74 20 6d 65   some.** soft me
12f36 6d 6f 72 79 20 6c 69 6d 69 74 2e 20 54 68 65 20  mory limit. The 
12f37 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69  first argument i
12f38 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
12f39 20 50 61 67 65 72 20 6f 62 6a 65 63 74 0a 2a 2a   Pager object.**
12f3a 20 28 63 61 73 74 20 61 73 20 61 20 76 6f 69 64   (cast as a void
12f3b 2a 29 2e 20 54 68 65 20 70 61 67 65 72 20 69 73  *). The pager is
12f3c 20 61 6c 77 61 79 73 20 27 70 75 72 67 65 61 62   always 'purgeab
12f3d 6c 65 27 20 28 6e 6f 74 20 61 6e 20 69 6e 2d 6d  le' (not an in-m
12f3e 65 6d 6f 72 79 0a 2a 2a 20 64 61 74 61 62 61 73  emory.** databas
12f3f 65 29 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 61  e). The second a
12f40 72 67 75 6d 65 6e 74 20 69 73 20 61 20 72 65 66  rgument is a ref
12f41 65 72 65 6e 63 65 20 74 6f 20 61 20 70 61 67 65  erence to a page
12f42 20 74 68 61 74 20 69 73 20 0a 2a 2a 20 63 75 72   that is .** cur
12f43 72 65 6e 74 6c 79 20 64 69 72 74 79 20 62 75 74  rently dirty but
12f44 20 68 61 73 20 6e 6f 20 6f 75 74 73 74 61 6e 64   has no outstand
12f45 69 6e 67 20 72 65 66 65 72 65 6e 63 65 73 2e 20  ing references. 
12f46 54 68 65 20 70 61 67 65 0a 2a 2a 20 69 73 20 61  The page.** is a
12f47 6c 77 61 79 73 20 61 73 73 6f 63 69 61 74 65 64  lways associated
12f48 20 77 69 74 68 20 74 68 65 20 50 61 67 65 72 20   with the Pager 
12f49 6f 62 6a 65 63 74 20 70 61 73 73 65 64 20 61 73  object passed as
12f4a 20 74 68 65 20 66 69 72 73 74 20 0a 2a 2a 20 61   the first .** a
12f4b 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54  rgument..**.** T
12f4c 68 65 20 6a 6f 62 20 6f 66 20 74 68 69 73 20 66  he job of this f
12f4d 75 6e 63 74 69 6f 6e 20 69 73 20 74 6f 20 6d 61  unction is to ma
12f4e 6b 65 20 70 50 67 20 63 6c 65 61 6e 20 62 79 20  ke pPg clean by 
12f4f 77 72 69 74 69 6e 67 20 69 74 73 20 63 6f 6e 74  writing its cont
12f50 65 6e 74 73 0a 2a 2a 20 6f 75 74 20 74 6f 20 74  ents.** out to t
12f51 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
12f52 2c 20 69 66 20 70 6f 73 73 69 62 6c 65 2e 20 54  , if possible. T
12f53 68 69 73 20 6d 61 79 20 69 6e 76 6f 6c 76 65 20  his may involve 
12f54 73 79 6e 63 69 6e 67 20 74 68 65 0a 2a 2a 20 6a  syncing the.** j
12f55 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 0a 2a 2a  ournal file. .**
12f56 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75  .** If successfu
12f57 6c 2c 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  l, sqlite3Pcache
12f58 4d 61 6b 65 43 6c 65 61 6e 28 29 20 69 73 20 63  MakeClean() is c
12f59 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 70 61 67  alled on the pag
12f5a 65 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f  e and.** SQLITE_
12f5b 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20  OK returned. If 
12f5c 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75  an IO error occu
12f5d 72 73 20 77 68 69 6c 65 20 74 72 79 69 6e 67 20  rs while trying 
12f5e 74 6f 20 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 70  to make the.** p
12f5f 61 67 65 20 63 6c 65 61 6e 2c 20 74 68 65 20 49  age clean, the I
12f60 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  O error code is 
12f61 72 65 74 75 72 6e 65 64 2e 20 49 66 20 74 68 65  returned. If the
12f62 20 70 61 67 65 20 63 61 6e 6e 6f 74 20 62 65 0a   page cannot be.
12f63 2a 2a 20 6d 61 64 65 20 63 6c 65 61 6e 20 66 6f  ** made clean fo
12f64 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 72 65 61  r some other rea
12f65 73 6f 6e 2c 20 62 75 74 20 6e 6f 20 65 72 72 6f  son, but no erro
12f66 72 20 6f 63 63 75 72 73 2c 20 74 68 65 6e 20 53  r occurs, then S
12f67 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 69 73 20 72  QLITE_OK.** is r
12f68 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74  eturned by sqlit
12f69 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61  e3PcacheMakeClea
12f6a 6e 28 29 20 69 73 20 6e 6f 74 20 63 61 6c 6c 65  n() is not calle
12f6b 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
12f6c 20 70 61 67 65 72 53 74 72 65 73 73 28 76 6f 69   pagerStress(voi
12f6d 64 20 2a 70 2c 20 50 67 48 64 72 20 2a 70 50 67  d *p, PgHdr *pPg
12f6e 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  ){.  Pager *pPag
12f6f 65 72 20 3d 20 28 50 61 67 65 72 20 2a 29 70 3b  er = (Pager *)p;
12f70 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
12f71 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74  TE_OK;..  assert
12f72 28 20 70 50 67 2d 3e 70 50 61 67 65 72 3d 3d 70  ( pPg->pPager==p
12f73 50 61 67 65 72 20 29 3b 0a 20 20 61 73 73 65 72  Pager );.  asser
12f74 74 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47  t( pPg->flags&PG
12f75 48 44 52 5f 44 49 52 54 59 20 29 3b 0a 0a 20 20  HDR_DIRTY );..  
12f76 2f 2a 20 54 68 65 20 64 6f 4e 6f 74 53 79 6e 63  /* The doNotSync
12f77 20 66 6c 61 67 20 69 73 20 73 65 74 20 62 79 20   flag is set by 
12f78 74 68 65 20 73 71 6c 69 74 65 33 50 61 67 65 72  the sqlite3Pager
12f79 57 72 69 74 65 28 29 20 66 75 6e 63 74 69 6f 6e  Write() function
12f7a 20 77 68 69 6c 65 20 69 74 0a 20 20 2a 2a 20 69   while it.  ** i
12f7b 73 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 61 20  s journalling a 
12f7c 73 65 74 20 6f 66 20 74 77 6f 20 6f 72 20 6d 6f  set of two or mo
12f7d 72 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65  re database page
12f7e 73 20 74 68 61 74 20 61 72 65 20 73 74 6f 72 65  s that are store
12f7f 64 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 73 61  d.  ** on the sa
12f80 6d 65 20 64 69 73 6b 20 73 65 63 74 6f 72 2e 20  me disk sector. 
12f81 53 79 6e 63 69 6e 67 20 74 68 65 20 6a 6f 75 72  Syncing the jour
12f82 6e 61 6c 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 77  nal is not allow
12f83 65 64 20 77 68 69 6c 65 0a 20 20 2a 2a 20 74 68  ed while.  ** th
12f84 69 73 20 69 73 20 68 61 70 70 65 6e 69 6e 67 20  is is happening 
12f85 61 73 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61  as it is importa
12f86 6e 74 20 74 68 61 74 20 61 6c 6c 20 6d 65 6d 62  nt that all memb
12f87 65 72 73 20 6f 66 20 73 75 63 68 20 61 0a 20 20  ers of such a.  
12f88 2a 2a 20 73 65 74 20 6f 66 20 70 61 67 65 73 20  ** set of pages 
12f89 61 72 65 20 73 79 6e 63 65 64 20 74 6f 20 64 69  are synced to di
12f8a 73 6b 20 74 6f 67 65 74 68 65 72 2e 20 53 6f 2c  sk together. So,
12f8b 20 69 66 20 74 68 65 20 70 61 67 65 20 74 68 69   if the page thi
12f8c 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20  s function.  ** 
12f8d 69 73 20 74 72 79 69 6e 67 20 74 6f 20 6d 61 6b  is trying to mak
12f8e 65 20 63 6c 65 61 6e 20 77 69 6c 6c 20 72 65 71  e clean will req
12f8f 75 69 72 65 20 61 20 6a 6f 75 72 6e 61 6c 20 73  uire a journal s
12f90 79 6e 63 20 61 6e 64 20 74 68 65 20 64 6f 4e 6f  ync and the doNo
12f91 74 53 79 6e 63 0a 20 20 2a 2a 20 66 6c 61 67 20  tSync.  ** flag 
12f92 69 73 20 73 65 74 2c 20 72 65 74 75 72 6e 20 77  is set, return w
12f93 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79  ithout doing any
12f94 74 68 69 6e 67 2e 20 54 68 65 20 70 63 61 63 68  thing. The pcach
12f95 65 20 6c 61 79 65 72 20 77 69 6c 6c 0a 20 20 2a  e layer will.  *
12f96 2a 20 6a 75 73 74 20 68 61 76 65 20 74 6f 20 67  * just have to g
12f97 6f 20 61 68 65 61 64 20 61 6e 64 20 61 6c 6c 6f  o ahead and allo
12f98 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 20  cate a new page 
12f99 62 75 66 66 65 72 20 69 6e 73 74 65 61 64 20 6f  buffer instead o
12f9a 66 0a 20 20 2a 2a 20 72 65 75 73 69 6e 67 20 70  f.  ** reusing p
12f9b 50 67 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 53 69  Pg..  **.  ** Si
12f9c 6d 69 6c 61 72 6c 79 2c 20 69 66 20 74 68 65 20  milarly, if the 
12f9d 70 61 67 65 72 20 68 61 73 20 61 6c 72 65 61 64  pager has alread
12f9e 79 20 65 6e 74 65 72 65 64 20 74 68 65 20 65 72  y entered the er
12f9f 72 6f 72 20 73 74 61 74 65 2c 20 64 6f 20 6e 6f  ror state, do no
12fa0 74 0a 20 20 2a 2a 20 74 72 79 20 74 6f 20 77 72  t.  ** try to wr
12fa1 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  ite the contents
12fa2 20 6f 66 20 70 50 67 20 74 6f 20 64 69 73 6b 2e   of pPg to disk.
12fa3 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45  .  */.  if( NEVE
12fa4 52 28 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  R(pPager->errCod
12fa5 65 29 0a 20 20 20 7c 7c 20 28 70 50 61 67 65 72  e).   || (pPager
12fa6 2d 3e 64 6f 4e 6f 74 53 79 6e 63 20 26 26 20 70  ->doNotSync && p
12fa7 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f  Pg->flags&PGHDR_
12fa8 4e 45 45 44 5f 53 59 4e 43 29 0a 20 20 29 7b 0a  NEED_SYNC).  ){.
12fa9 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
12faa 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  E_OK;.  }..  /* 
12fab 53 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c  Sync the journal
12fac 20 66 69 6c 65 20 69 66 20 72 65 71 75 69 72 65   file if require
12fad 64 2e 20 2a 2f 0a 20 20 69 66 28 20 70 50 67 2d  d. */.  if( pPg-
12fae 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45  >flags&PGHDR_NEE
12faf 44 5f 53 59 4e 43 20 29 7b 0a 20 20 20 20 72 63  D_SYNC ){.    rc
12fb0 20 3d 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 70   = syncJournal(p
12fb1 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20  Pager);.    if( 
12fb2 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
12fb3 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e   pPager->fullSyn
12fb4 63 20 26 26 20 0a 20 20 20 20 20 20 21 28 70 50  c && .      !(pP
12fb5 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64  ager->journalMod
12fb6 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e==PAGER_JOURNAL
12fb7 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29 20 26 26 0a  MODE_MEMORY) &&.
12fb8 20 20 20 20 20 20 21 28 73 71 6c 69 74 65 33 4f        !(sqlite3O
12fb9 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72  sDeviceCharacter
12fba 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66  istics(pPager->f
12fbb 64 29 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  d)&SQLITE_IOCAP_
12fbc 53 41 46 45 5f 41 50 50 45 4e 44 29 0a 20 20 20  SAFE_APPEND).   
12fbd 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72   ){.      pPager
12fbe 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20 20 20 20  ->nRec = 0;.    
12fbf 20 20 72 63 20 3d 20 77 72 69 74 65 4a 6f 75 72    rc = writeJour
12fc0 6e 61 6c 48 64 72 28 70 50 61 67 65 72 29 3b 0a  nalHdr(pPager);.
12fc1 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
12fc2 49 66 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62  If the page numb
12fc3 65 72 20 6f 66 20 74 68 69 73 20 70 61 67 65 20  er of this page 
12fc4 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74  is larger than t
12fc5 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20  he current size 
12fc6 6f 66 0a 20 20 2a 2a 20 74 68 65 20 64 61 74 61  of.  ** the data
12fc7 62 61 73 65 20 69 6d 61 67 65 2c 20 69 74 20 6d  base image, it m
12fc8 61 79 20 6e 65 65 64 20 74 6f 20 62 65 20 77 72  ay need to be wr
12fc9 69 74 74 65 6e 20 74 6f 20 74 68 65 20 73 75 62  itten to the sub
12fca 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 20 2a 2a 20 54  -journal..  ** T
12fcb 68 69 73 20 69 73 20 62 65 63 61 75 73 65 20 74  his is because t
12fcc 68 65 20 63 61 6c 6c 20 74 6f 20 70 61 67 65 72  he call to pager
12fcd 5f 77 72 69 74 65 5f 70 61 67 65 6c 69 73 74 28  _write_pagelist(
12fce 29 20 62 65 6c 6f 77 20 77 69 6c 6c 20 6e 6f 74  ) below will not
12fcf 0a 20 20 2a 2a 20 61 63 74 75 61 6c 6c 79 20 77  .  ** actually w
12fd0 72 69 74 65 20 64 61 74 61 20 74 6f 20 74 68 65  rite data to the
12fd1 20 66 69 6c 65 20 69 6e 20 74 68 69 73 20 63 61   file in this ca
12fd2 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 43 6f  se..  **.  ** Co
12fd3 6e 73 69 64 65 72 20 74 68 65 20 66 6f 6c 6c 6f  nsider the follo
12fd4 77 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6f 66  wing sequence of
12fd5 20 65 76 65 6e 74 73 3a 0a 20 20 2a 2a 0a 20 20   events:.  **.  
12fd6 2a 2a 20 20 20 42 45 47 49 4e 3b 0a 20 20 2a 2a  **   BEGIN;.  **
12fd7 20 20 20 20 20 3c 6a 6f 75 72 6e 61 6c 20 70 61       <journal pa
12fd8 67 65 20 58 3e 0a 20 20 2a 2a 20 20 20 20 20 3c  ge X>.  **     <
12fd9 6d 6f 64 69 66 79 20 70 61 67 65 20 58 3e 0a 20  modify page X>. 
12fda 20 2a 2a 20 20 20 20 20 53 41 56 45 50 4f 49 4e   **     SAVEPOIN
12fdb 54 20 73 70 3b 0a 20 20 2a 2a 20 20 20 20 20 20  T sp;.  **      
12fdc 20 3c 73 68 72 69 6e 6b 20 64 61 74 61 62 61 73   <shrink databas
12fdd 65 20 66 69 6c 65 20 74 6f 20 59 20 70 61 67 65  e file to Y page
12fde 73 3e 0a 20 20 2a 2a 20 20 20 20 20 20 20 70 61  s>.  **       pa
12fdf 67 65 72 53 74 72 65 73 73 28 70 61 67 65 20 58  gerStress(page X
12fe0 29 0a 20 20 2a 2a 20 20 20 20 20 52 4f 4c 4c 42  ).  **     ROLLB
12fe1 41 43 4b 20 54 4f 20 73 70 3b 0a 20 20 2a 2a 0a  ACK TO sp;.  **.
12fe2 20 20 2a 2a 20 49 66 20 28 58 3e 59 29 2c 20 74    ** If (X>Y), t
12fe3 68 65 6e 20 77 68 65 6e 20 70 61 67 65 72 53 74  hen when pagerSt
12fe4 72 65 73 73 20 69 73 20 63 61 6c 6c 65 64 20 70  ress is called p
12fe5 61 67 65 20 58 20 77 69 6c 6c 20 6e 6f 74 20 62  age X will not b
12fe6 65 20 77 72 69 74 74 65 6e 0a 20 20 2a 2a 20 6f  e written.  ** o
12fe7 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ut to the databa
12fe8 73 65 20 66 69 6c 65 2c 20 62 75 74 20 77 69 6c  se file, but wil
12fe9 6c 20 62 65 20 64 72 6f 70 70 65 64 20 66 72 6f  l be dropped fro
12fea 6d 20 74 68 65 20 63 61 63 68 65 2e 20 54 68 65  m the cache. The
12feb 6e 2c 0a 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e  n,.  ** followin
12fec 67 20 74 68 65 20 22 52 4f 4c 4c 42 41 43 4b 20  g the "ROLLBACK 
12fed 54 4f 20 73 70 22 20 73 74 61 74 65 6d 65 6e 74  TO sp" statement
12fee 2c 20 72 65 61 64 69 6e 67 20 70 61 67 65 20 58  , reading page X
12fef 20 77 69 6c 6c 20 72 65 61 64 0a 20 20 2a 2a 20   will read.  ** 
12ff0 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 64 61  data from the da
12ff1 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 69  tabase file. Thi
12ff2 73 20 77 69 6c 6c 20 62 65 20 74 68 65 20 63 6f  s will be the co
12ff3 70 79 20 6f 66 20 70 61 67 65 20 58 20 61 73 20  py of page X as 
12ff4 69 74 0a 20 20 2a 2a 20 77 61 73 20 77 68 65 6e  it.  ** was when
12ff5 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
12ff6 20 73 74 61 72 74 65 64 2c 20 6e 6f 74 20 61 73   started, not as
12ff7 20 69 74 20 77 61 73 20 77 68 65 6e 20 22 53 41   it was when "SA
12ff8 56 45 50 4f 49 4e 54 20 73 70 22 0a 20 20 2a 2a  VEPOINT sp".  **
12ff9 20 77 61 73 20 65 78 65 63 75 74 65 64 2e 0a 20   was executed.. 
12ffa 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 6f 6c   **.  ** The sol
12ffb 75 74 69 6f 6e 20 69 73 20 74 6f 20 77 72 69 74  ution is to writ
12ffc 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 61  e the current da
12ffd 74 61 20 66 6f 72 20 70 61 67 65 20 58 20 69 6e  ta for page X in
12ffe 74 6f 20 74 68 65 20 0a 20 20 2a 2a 20 73 75 62  to the .  ** sub
12fff 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 6f  -journal file no
13000 77 20 28 69 66 20 69 74 20 69 73 20 6e 6f 74 20  w (if it is not 
13001 61 6c 72 65 61 64 79 20 74 68 65 72 65 29 2c 20  already there), 
13002 73 6f 20 74 68 61 74 20 69 74 20 77 69 6c 6c 0a  so that it will.
13003 20 20 2a 2a 20 62 65 20 72 65 73 74 6f 72 65 64    ** be restored
13004 20 74 6f 20 69 74 73 20 63 75 72 72 65 6e 74 20   to its current 
13005 76 61 6c 75 65 20 77 68 65 6e 20 74 68 65 20 22  value when the "
13006 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 73 70 22 20  ROLLBACK TO sp" 
13007 69 73 20 0a 20 20 2a 2a 20 65 78 65 63 75 74 65  is .  ** execute
13008 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45  d..  */.  if( NE
13009 56 45 52 28 0a 20 20 20 20 20 20 72 63 3d 3d 53  VER(.      rc==S
1300a 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 67 2d  QLITE_OK && pPg-
1300b 3e 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62  >pgno>pPager->db
1300c 53 69 7a 65 20 26 26 20 73 75 62 6a 52 65 71 75  Size && subjRequ
1300d 69 72 65 73 50 61 67 65 28 70 50 67 29 0a 20 20  iresPage(pPg).  
1300e 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 75  ) ){.    rc = su
1300f 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 70 50 67  bjournalPage(pPg
13010 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 72 69  );.  }..  /* Wri
13011 74 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  te the contents 
13012 6f 66 20 74 68 65 20 70 61 67 65 20 6f 75 74 20  of the page out 
13013 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
13014 66 69 6c 65 2e 20 2a 2f 0a 20 20 69 66 28 20 72  file. */.  if( r
13015 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
13016 20 20 20 20 70 50 67 2d 3e 70 44 69 72 74 79 20      pPg->pDirty 
13017 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 70 61  = 0;.    rc = pa
13018 67 65 72 5f 77 72 69 74 65 5f 70 61 67 65 6c 69  ger_write_pageli
13019 73 74 28 70 50 67 29 3b 0a 20 20 7d 0a 0a 20 20  st(pPg);.  }..  
1301a 2f 2a 20 4d 61 72 6b 20 74 68 65 20 70 61 67 65  /* Mark the page
1301b 20 61 73 20 63 6c 65 61 6e 2e 20 2a 2f 0a 20 20   as clean. */.  
1301c 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1301d 4b 20 29 7b 0a 20 20 20 20 50 41 47 45 52 54 52  K ){.    PAGERTR
1301e 41 43 45 28 28 22 53 54 52 45 53 53 20 25 64 20  ACE(("STRESS %d 
1301f 70 61 67 65 20 25 64 5c 6e 22 2c 20 50 41 47 45  page %d\n", PAGE
13020 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67  RID(pPager), pPg
13021 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 73 71  ->pgno));.    sq
13022 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43  lite3PcacheMakeC
13023 6c 65 61 6e 28 70 50 67 29 3b 0a 20 20 7d 0a 0a  lean(pPg);.  }..
13024 20 20 72 65 74 75 72 6e 20 70 61 67 65 72 5f 65    return pager_e
13025 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29  rror(pPager, rc)
13026 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  ;.}.../*.** Allo
13027 63 61 74 65 20 61 6e 64 20 69 6e 69 74 69 61 6c  cate and initial
13028 69 7a 65 20 61 20 6e 65 77 20 50 61 67 65 72 20  ize a new Pager 
13029 6f 62 6a 65 63 74 20 61 6e 64 20 70 75 74 20 61  object and put a
1302a 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 0a 2a   pointer to it.*
1302b 2a 20 69 6e 20 2a 70 70 50 61 67 65 72 2e 20 54  * in *ppPager. T
1302c 68 65 20 70 61 67 65 72 20 73 68 6f 75 6c 64 20  he pager should 
1302d 65 76 65 6e 74 75 61 6c 6c 79 20 62 65 20 66 72  eventually be fr
1302e 65 65 64 20 62 79 20 70 61 73 73 69 6e 67 20 69  eed by passing i
1302f 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 50  t.** to sqlite3P
13030 61 67 65 72 43 6c 6f 73 65 28 29 2e 0a 2a 2a 0a  agerClose()..**.
13031 2a 2a 20 54 68 65 20 7a 46 69 6c 65 6e 61 6d 65  ** The zFilename
13032 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65   argument is the
13033 20 70 61 74 68 20 74 6f 20 74 68 65 20 64 61 74   path to the dat
13034 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 6f 70  abase file to op
13035 65 6e 2e 0a 2a 2a 20 49 66 20 7a 46 69 6c 65 6e  en..** If zFilen
13036 61 6d 65 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e  ame is NULL then
13037 20 61 20 72 61 6e 64 6f 6d 6c 79 2d 6e 61 6d 65   a randomly-name
13038 64 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  d temporary file
13039 20 69 73 20 63 72 65 61 74 65 64 0a 2a 2a 20 61   is created.** a
1303a 6e 64 20 75 73 65 64 20 61 73 20 74 68 65 20 66  nd used as the f
1303b 69 6c 65 20 74 6f 20 62 65 20 63 61 63 68 65 64  ile to be cached
1303c 2e 20 54 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  . Temporary file
1303d 73 20 61 72 65 20 62 65 20 64 65 6c 65 74 65 64  s are be deleted
1303e 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  .** automaticall
1303f 79 20 77 68 65 6e 20 74 68 65 79 20 61 72 65 20  y when they are 
13040 63 6c 6f 73 65 64 2e 20 49 66 20 7a 46 69 6c 65  closed. If zFile
13041 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f 72 79  name is ":memory
13042 3a 22 20 74 68 65 6e 20 0a 2a 2a 20 61 6c 6c 20  :" then .** all 
13043 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 68  information is h
13044 65 6c 64 20 69 6e 20 63 61 63 68 65 2e 20 49 74  eld in cache. It
13045 20 69 73 20 6e 65 76 65 72 20 77 72 69 74 74 65   is never writte
13046 6e 20 74 6f 20 64 69 73 6b 2e 20 0a 2a 2a 20 54  n to disk. .** T
13047 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20  his can be used 
13048 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 6e 20  to implement an 
13049 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61  in-memory databa
1304a 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 45  se..**.** The nE
1304b 78 74 72 61 20 70 61 72 61 6d 65 74 65 72 20 73  xtra parameter s
1304c 70 65 63 69 66 69 65 73 20 74 68 65 20 6e 75 6d  pecifies the num
1304d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20  ber of bytes of 
1304e 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 0a  space allocated.
1304f 2a 2a 20 61 6c 6f 6e 67 20 77 69 74 68 20 65 61  ** along with ea
13050 63 68 20 70 61 67 65 20 72 65 66 65 72 65 6e 63  ch page referenc
13051 65 2e 20 54 68 69 73 20 73 70 61 63 65 20 69 73  e. This space is
13052 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 74 68   available to th
13053 65 20 75 73 65 72 0a 2a 2a 20 76 69 61 20 74 68  e user.** via th
13054 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  e sqlite3PagerGe
13055 74 45 78 74 72 61 28 29 20 41 50 49 2e 0a 2a 2a  tExtra() API..**
13056 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 61 72  .** The flags ar
13057 67 75 6d 65 6e 74 20 69 73 20 75 73 65 64 20 74  gument is used t
13058 6f 20 73 70 65 63 69 66 79 20 70 72 6f 70 65 72  o specify proper
13059 74 69 65 73 20 74 68 61 74 20 61 66 66 65 63 74  ties that affect
1305a 20 74 68 65 0a 2a 2a 20 6f 70 65 72 61 74 69 6f   the.** operatio
1305b 6e 20 6f 66 20 74 68 65 20 70 61 67 65 72 2e 20  n of the pager. 
1305c 49 74 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73  It should be pas
1305d 73 65 64 20 73 6f 6d 65 20 62 69 74 77 69 73 65  sed some bitwise
1305e 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 0a 2a 2a 20   combination.** 
1305f 6f 66 20 74 68 65 20 50 41 47 45 52 5f 4f 4d 49  of the PAGER_OMI
13060 54 5f 4a 4f 55 52 4e 41 4c 20 61 6e 64 20 50 41  T_JOURNAL and PA
13061 47 45 52 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20  GER_NO_READLOCK 
13062 66 6c 61 67 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  flags..**.** The
13063 20 76 66 73 46 6c 61 67 73 20 70 61 72 61 6d 65   vfsFlags parame
13064 74 65 72 20 69 73 20 61 20 62 69 74 6d 61 73 6b  ter is a bitmask
13065 20 74 6f 20 70 61 73 73 20 74 6f 20 74 68 65 20   to pass to the 
13066 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 0a  flags parameter.
13067 2a 2a 20 6f 66 20 74 68 65 20 78 4f 70 65 6e 28  ** of the xOpen(
13068 29 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20  ) method of the 
13069 73 75 70 70 6c 69 65 64 20 56 46 53 20 77 68 65  supplied VFS whe
1306a 6e 20 6f 70 65 6e 69 6e 67 20 66 69 6c 65 73 2e  n opening files.
1306b 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70   .**.** If the p
1306c 61 67 65 72 20 6f 62 6a 65 63 74 20 69 73 20 61  ager object is a
1306d 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65  llocated and the
1306e 20 73 70 65 63 69 66 69 65 64 20 66 69 6c 65 20   specified file 
1306f 6f 70 65 6e 65 64 20 0a 2a 2a 20 73 75 63 63 65  opened .** succe
13070 73 73 66 75 6c 6c 79 2c 20 53 51 4c 49 54 45 5f  ssfully, SQLITE_
13071 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  OK is returned a
13072 6e 64 20 2a 70 70 50 61 67 65 72 20 73 65 74 20  nd *ppPager set 
13073 74 6f 20 70 6f 69 6e 74 20 74 6f 0a 2a 2a 20 74  to point to.** t
13074 68 65 20 6e 65 77 20 70 61 67 65 72 20 6f 62 6a  he new pager obj
13075 65 63 74 2e 20 49 66 20 61 6e 20 65 72 72 6f 72  ect. If an error
13076 20 6f 63 63 75 72 73 2c 20 2a 70 70 50 61 67 65   occurs, *ppPage
13077 72 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c  r is set to NULL
13078 0a 2a 2a 20 61 6e 64 20 65 72 72 6f 72 20 63 6f  .** and error co
13079 64 65 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69  de returned. Thi
1307a 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 72  s function may r
1307b 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
1307c 45 4d 0a 2a 2a 20 28 73 71 6c 69 74 65 33 4d 61  EM.** (sqlite3Ma
1307d 6c 6c 6f 63 28 29 20 69 73 20 75 73 65 64 20 74  lloc() is used t
1307e 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72  o allocate memor
1307f 79 29 2c 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f  y), SQLITE_CANTO
13080 50 45 4e 20 6f 72 20 0a 2a 2a 20 76 61 72 69 6f  PEN or .** vario
13081 75 73 20 53 51 4c 49 54 45 5f 49 4f 5f 58 58 58  us SQLITE_IO_XXX
13082 20 65 72 72 6f 72 73 2e 0a 2a 2f 0a 53 51 4c 49   errors..*/.SQLI
13083 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
13084 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 28  qlite3PagerOpen(
13085 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
13086 70 56 66 73 2c 20 20 20 20 20 20 20 2f 2a 20 54  pVfs,       /* T
13087 68 65 20 76 69 72 74 75 61 6c 20 66 69 6c 65 20  he virtual file 
13088 73 79 73 74 65 6d 20 74 6f 20 75 73 65 20 2a 2f  system to use */
13089 0a 20 20 50 61 67 65 72 20 2a 2a 70 70 50 61 67  .  Pager **ppPag
1308a 65 72 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  er,         /* O
1308b 55 54 3a 20 52 65 74 75 72 6e 20 74 68 65 20 50  UT: Return the P
1308c 61 67 65 72 20 73 74 72 75 63 74 75 72 65 20 68  ager structure h
1308d 65 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ere */.  const c
1308e 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20  har *zFilename, 
1308f 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
13090 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74   database file t
13091 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20  o open */.  int 
13092 6e 45 78 74 72 61 2c 20 20 20 20 20 20 20 20 20  nExtra,         
13093 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 62 79       /* Extra by
13094 74 65 73 20 61 70 70 65 6e 64 20 74 6f 20 65 61  tes append to ea
13095 63 68 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67  ch in-memory pag
13096 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73  e */.  int flags
13097 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
13098 2f 2a 20 66 6c 61 67 73 20 63 6f 6e 74 72 6f 6c  /* flags control
13099 6c 69 6e 67 20 74 68 69 73 20 66 69 6c 65 20 2a  ling this file *
1309a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73  /.  int vfsFlags
1309b 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
1309c 66 6c 61 67 73 20 70 61 73 73 65 64 20 74 68 72  flags passed thr
1309d 6f 75 67 68 20 74 6f 20 73 71 6c 69 74 65 33 5f  ough to sqlite3_
1309e 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20  vfs.xOpen() */. 
1309f 20 76 6f 69 64 20 28 2a 78 52 65 69 6e 69 74 29   void (*xReinit)
130a0 28 44 62 50 61 67 65 2a 29 20 2f 2a 20 46 75 6e  (DbPage*) /* Fun
130a1 63 74 69 6f 6e 20 74 6f 20 72 65 69 6e 69 74 69  ction to reiniti
130a2 61 6c 69 7a 65 20 70 61 67 65 73 20 2a 2f 0a 29  alize pages */.)
130a3 7b 0a 20 20 75 38 20 2a 70 50 74 72 3b 0a 20 20  {.  u8 *pPtr;.  
130a4 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20  Pager *pPager = 
130a5 30 3b 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65  0;       /* Page
130a6 72 20 6f 62 6a 65 63 74 20 74 6f 20 61 6c 6c 6f  r object to allo
130a7 63 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20  cate and return 
130a8 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  */.  int rc = SQ
130a9 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a  LITE_OK;      /*
130aa 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
130ab 20 20 69 6e 74 20 74 65 6d 70 46 69 6c 65 20 3d    int tempFile =
130ac 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72   0;        /* Tr
130ad 75 65 20 66 6f 72 20 74 65 6d 70 20 66 69 6c 65  ue for temp file
130ae 73 20 28 69 6e 63 6c 2e 20 69 6e 2d 6d 65 6d 6f  s (incl. in-memo
130af 72 79 20 66 69 6c 65 73 29 20 2a 2f 0a 20 20 69  ry files) */.  i
130b0 6e 74 20 6d 65 6d 44 62 20 3d 20 30 3b 20 20 20  nt memDb = 0;   
130b1 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
130b2 69 66 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e  if this is an in
130b3 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 20 2a 2f 0a  -memory file */.
130b4 20 20 69 6e 74 20 72 65 61 64 4f 6e 6c 79 20 3d    int readOnly =
130b5 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72   0;        /* Tr
130b6 75 65 20 69 66 20 74 68 69 73 20 69 73 20 61 20  ue if this is a 
130b7 72 65 61 64 2d 6f 6e 6c 79 20 66 69 6c 65 20 2a  read-only file *
130b8 2f 0a 20 20 69 6e 74 20 6a 6f 75 72 6e 61 6c 46  /.  int journalF
130b9 69 6c 65 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20  ileSize;     /* 
130ba 42 79 74 65 73 20 74 6f 20 61 6c 6c 6f 63 61 74  Bytes to allocat
130bb 65 20 66 6f 72 20 65 61 63 68 20 6a 6f 75 72 6e  e for each journ
130bc 61 6c 20 66 64 20 2a 2f 0a 20 20 63 68 61 72 20  al fd */.  char 
130bd 2a 7a 50 61 74 68 6e 61 6d 65 20 3d 20 30 3b 20  *zPathname = 0; 
130be 20 20 20 20 2f 2a 20 46 75 6c 6c 20 70 61 74 68      /* Full path
130bf 20 74 6f 20 64 61 74 61 62 61 73 65 20 66 69 6c   to database fil
130c0 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 74 68  e */.  int nPath
130c1 6e 61 6d 65 20 3d 20 30 3b 20 20 20 20 20 20 20  name = 0;       
130c2 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
130c3 65 73 20 69 6e 20 7a 50 61 74 68 6e 61 6d 65 20  es in zPathname 
130c4 2a 2f 0a 20 20 69 6e 74 20 75 73 65 4a 6f 75 72  */.  int useJour
130c5 6e 61 6c 20 3d 20 28 66 6c 61 67 73 20 26 20 50  nal = (flags & P
130c6 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41  AGER_OMIT_JOURNA
130c7 4c 29 3d 3d 30 3b 20 2f 2a 20 46 61 6c 73 65 20  L)==0; /* False 
130c8 74 6f 20 6f 6d 69 74 20 6a 6f 75 72 6e 61 6c 20  to omit journal 
130c9 2a 2f 0a 20 20 69 6e 74 20 6e 6f 52 65 61 64 6c  */.  int noReadl
130ca 6f 63 6b 20 3d 20 28 66 6c 61 67 73 20 26 20 50  ock = (flags & P
130cb 41 47 45 52 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b  AGER_NO_READLOCK
130cc 29 21 3d 30 3b 20 20 2f 2a 20 54 72 75 65 20 74  )!=0;  /* True t
130cd 6f 20 6f 6d 69 74 20 72 65 61 64 2d 6c 6f 63 6b  o omit read-lock
130ce 20 2a 2f 0a 20 20 69 6e 74 20 70 63 61 63 68 65   */.  int pcache
130cf 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 50 63  Size = sqlite3Pc
130d0 61 63 68 65 53 69 7a 65 28 29 3b 20 20 20 20 20  acheSize();     
130d1 20 20 2f 2a 20 42 79 74 65 73 20 74 6f 20 61 6c    /* Bytes to al
130d2 6c 6f 63 61 74 65 20 66 6f 72 20 50 43 61 63 68  locate for PCach
130d3 65 20 2a 2f 0a 20 20 75 31 36 20 73 7a 50 61 67  e */.  u16 szPag
130d4 65 44 66 6c 74 20 3d 20 53 51 4c 49 54 45 5f 44  eDflt = SQLITE_D
130d5 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45  EFAULT_PAGE_SIZE
130d6 3b 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 70 61  ;  /* Default pa
130d7 67 65 20 73 69 7a 65 20 2a 2f 0a 0a 20 20 2f 2a  ge size */..  /*
130d8 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20   Figure out how 
130d9 6d 75 63 68 20 73 70 61 63 65 20 69 73 20 72 65  much space is re
130da 71 75 69 72 65 64 20 66 6f 72 20 65 61 63 68 20  quired for each 
130db 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2d 68 61 6e  journal file-han
130dc 64 6c 65 0a 20 20 2a 2a 20 28 74 68 65 72 65 20  dle.  ** (there 
130dd 61 72 65 20 74 77 6f 20 6f 66 20 74 68 65 6d 2c  are two of them,
130de 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61   the main journa
130df 6c 20 61 6e 64 20 74 68 65 20 73 75 62 2d 6a 6f  l and the sub-jo
130e0 75 72 6e 61 6c 29 2e 20 54 68 69 73 0a 20 20 2a  urnal). This.  *
130e1 2a 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d  * is the maximum
130e2 20 73 70 61 63 65 20 72 65 71 75 69 72 65 64 20   space required 
130e3 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79  for an in-memory
130e4 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61   journal file ha
130e5 6e 64 6c 65 20 0a 20 20 2a 2a 20 61 6e 64 20 61  ndle .  ** and a
130e6 20 72 65 67 75 6c 61 72 20 6a 6f 75 72 6e 61 6c   regular journal
130e7 20 66 69 6c 65 2d 68 61 6e 64 6c 65 2e 20 4e 6f   file-handle. No
130e8 74 65 20 74 68 61 74 20 61 20 22 72 65 67 75 6c  te that a "regul
130e9 61 72 20 6a 6f 75 72 6e 61 6c 2d 68 61 6e 64 6c  ar journal-handl
130ea 65 22 0a 20 20 2a 2a 20 6d 61 79 20 62 65 20 61  e".  ** may be a
130eb 20 77 72 61 70 70 65 72 20 63 61 70 61 62 6c 65   wrapper capable
130ec 20 6f 66 20 63 61 63 68 69 6e 67 20 74 68 65 20   of caching the 
130ed 66 69 72 73 74 20 70 6f 72 74 69 6f 6e 20 6f 66  first portion of
130ee 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 2a   the journal.  *
130ef 2a 20 66 69 6c 65 20 69 6e 20 6d 65 6d 6f 72 79  * file in memory
130f0 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68   to implement th
130f1 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f  e atomic-write o
130f2 70 74 69 6d 69 7a 61 74 69 6f 6e 20 28 73 65 65  ptimization (see
130f3 20 0a 20 20 2a 2a 20 73 6f 75 72 63 65 20 66 69   .  ** source fi
130f4 6c 65 20 6a 6f 75 72 6e 61 6c 2e 63 29 2e 0a 20  le journal.c).. 
130f5 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
130f6 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 70 56 66  3JournalSize(pVf
130f7 73 29 3e 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75  s)>sqlite3MemJou
130f8 72 6e 61 6c 53 69 7a 65 28 29 20 29 7b 0a 20 20  rnalSize() ){.  
130f9 20 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a    journalFileSiz
130fa 65 20 3d 20 52 4f 55 4e 44 38 28 73 71 6c 69 74  e = ROUND8(sqlit
130fb 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 70 56  e3JournalSize(pV
130fc 66 73 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  fs));.  }else{. 
130fd 20 20 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69     journalFileSi
130fe 7a 65 20 3d 20 52 4f 55 4e 44 38 28 73 71 6c 69  ze = ROUND8(sqli
130ff 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a  te3MemJournalSiz
13100 65 28 29 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  e());.  }..  /* 
13101 53 65 74 20 74 68 65 20 6f 75 74 70 75 74 20 76  Set the output v
13102 61 72 69 61 62 6c 65 20 74 6f 20 4e 55 4c 4c 20  ariable to NULL 
13103 69 6e 20 63 61 73 65 20 61 6e 20 65 72 72 6f 72  in case an error
13104 20 6f 63 63 75 72 73 2e 20 2a 2f 0a 20 20 2a 70   occurs. */.  *p
13105 70 50 61 67 65 72 20 3d 20 30 3b 0a 0a 20 20 2f  pPager = 0;..  /
13106 2a 20 43 6f 6d 70 75 74 65 20 61 6e 64 20 73 74  * Compute and st
13107 6f 72 65 20 74 68 65 20 66 75 6c 6c 20 70 61 74  ore the full pat
13108 68 6e 61 6d 65 20 69 6e 20 61 6e 20 61 6c 6c 6f  hname in an allo
13109 63 61 74 65 64 20 62 75 66 66 65 72 20 70 6f 69  cated buffer poi
1310a 6e 74 65 64 0a 20 20 2a 2a 20 74 6f 20 62 79 20  nted.  ** to by 
1310b 7a 50 61 74 68 6e 61 6d 65 2c 20 6c 65 6e 67 74  zPathname, lengt
1310c 68 20 6e 50 61 74 68 6e 61 6d 65 2e 20 4f 72 2c  h nPathname. Or,
1310d 20 69 66 20 74 68 69 73 20 69 73 20 61 20 74 65   if this is a te
1310e 6d 70 6f 72 61 72 79 20 66 69 6c 65 2c 0a 20 20  mporary file,.  
1310f 2a 2a 20 6c 65 61 76 65 20 62 6f 74 68 20 6e 50  ** leave both nP
13110 61 74 68 6e 61 6d 65 20 61 6e 64 20 7a 50 61 74  athname and zPat
13111 68 6e 61 6d 65 20 73 65 74 20 74 6f 20 30 2e 0a  hname set to 0..
13112 20 20 2a 2f 0a 20 20 69 66 28 20 7a 46 69 6c 65    */.  if( zFile
13113 6e 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61 6d  name && zFilenam
13114 65 5b 30 5d 20 29 7b 0a 20 20 20 20 6e 50 61 74  e[0] ){.    nPat
13115 68 6e 61 6d 65 20 3d 20 70 56 66 73 2d 3e 6d 78  hname = pVfs->mx
13116 50 61 74 68 6e 61 6d 65 2b 31 3b 0a 20 20 20 20  Pathname+1;.    
13117 7a 50 61 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69  zPathname = sqli
13118 74 65 33 4d 61 6c 6c 6f 63 28 6e 50 61 74 68 6e  te3Malloc(nPathn
13119 61 6d 65 2a 32 29 3b 0a 20 20 20 20 69 66 28 20  ame*2);.    if( 
1311a 7a 50 61 74 68 6e 61 6d 65 3d 3d 30 20 29 7b 0a  zPathname==0 ){.
1311b 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
1311c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
1311d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1311e 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 20 20  OMIT_MEMORYDB.  
1311f 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a 46 69    if( strcmp(zFi
13120 6c 65 6e 61 6d 65 2c 22 3a 6d 65 6d 6f 72 79 3a  lename,":memory:
13121 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d  ")==0 ){.      m
13122 65 6d 44 62 20 3d 20 31 3b 0a 20 20 20 20 20 20  emDb = 1;.      
13123 7a 50 61 74 68 6e 61 6d 65 5b 30 5d 20 3d 20 30  zPathname[0] = 0
13124 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64  ;.    }else.#end
13125 69 66 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 7a  if.    {.      z
13126 50 61 74 68 6e 61 6d 65 5b 30 5d 20 3d 20 30 3b  Pathname[0] = 0;
13127 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 69 6e   /* Make sure in
13128 69 74 69 61 6c 69 7a 65 64 20 65 76 65 6e 20 69  itialized even i
13129 66 20 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 29  f FullPathname()
1312a 20 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20 20 20   fails */.      
1312b 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 75  rc = sqlite3OsFu
1312c 6c 6c 50 61 74 68 6e 61 6d 65 28 70 56 66 73 2c  llPathname(pVfs,
1312d 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 50 61 74   zFilename, nPat
1312e 68 6e 61 6d 65 2c 20 7a 50 61 74 68 6e 61 6d 65  hname, zPathname
1312f 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e 50  );.    }..    nP
13130 61 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65  athname = sqlite
13131 33 53 74 72 6c 65 6e 33 30 28 7a 50 61 74 68 6e  3Strlen30(zPathn
13132 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ame);.    if( rc
13133 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6e  ==SQLITE_OK && n
13134 50 61 74 68 6e 61 6d 65 2b 38 3e 70 56 66 73 2d  Pathname+8>pVfs-
13135 3e 6d 78 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20  >mxPathname ){. 
13136 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61       /* This bra
13137 6e 63 68 20 69 73 20 74 61 6b 65 6e 20 77 68 65  nch is taken whe
13138 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 70 61  n the journal pa
13139 74 68 20 72 65 71 75 69 72 65 64 20 62 79 0a 20  th required by. 
1313a 20 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61       ** the data
1313b 62 61 73 65 20 62 65 69 6e 67 20 6f 70 65 6e 65  base being opene
1313c 64 20 77 69 6c 6c 20 62 65 20 6d 6f 72 65 20 74  d will be more t
1313d 68 61 6e 20 70 56 66 73 2d 3e 6d 78 50 61 74 68  han pVfs->mxPath
1313e 6e 61 6d 65 0a 20 20 20 20 20 20 2a 2a 20 62 79  name.      ** by
1313f 74 65 73 20 69 6e 20 6c 65 6e 67 74 68 2e 20 54  tes in length. T
13140 68 69 73 20 6d 65 61 6e 73 20 74 68 65 20 64 61  his means the da
13141 74 61 62 61 73 65 20 63 61 6e 6e 6f 74 20 62 65  tabase cannot be
13142 20 6f 70 65 6e 65 64 2c 0a 20 20 20 20 20 20 2a   opened,.      *
13143 2a 20 61 73 20 69 74 20 77 69 6c 6c 20 6e 6f 74  * as it will not
13144 20 62 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 20   be possible to 
13145 6f 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  open the journal
13146 20 66 69 6c 65 20 6f 72 20 65 76 65 6e 0a 20 20   file or even.  
13147 20 20 20 20 2a 2a 20 63 68 65 63 6b 20 66 6f 72      ** check for
13148 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62   a hot-journal b
13149 65 66 6f 72 65 20 72 65 61 64 69 6e 67 2e 0a 20  efore reading.. 
1314a 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63       */.      rc
1314b 20 3d 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50   = SQLITE_CANTOP
1314c 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  EN;.    }.    if
1314d 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1314e 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1314f 5f 66 72 65 65 28 7a 50 61 74 68 6e 61 6d 65 29  _free(zPathname)
13150 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  ;.      return r
13151 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  c;.    }.  }..  
13152 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f  /* Allocate memo
13153 72 79 20 66 6f 72 20 74 68 65 20 50 61 67 65 72  ry for the Pager
13154 20 73 74 72 75 63 74 75 72 65 2c 20 50 43 61 63   structure, PCac
13155 68 65 20 6f 62 6a 65 63 74 2c 20 74 68 65 0a 20  he object, the. 
13156 20 2a 2a 20 74 68 72 65 65 20 66 69 6c 65 20 64   ** three file d
13157 65 73 63 72 69 70 74 6f 72 73 2c 20 74 68 65 20  escriptors, the 
13158 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6e 61  database file na
13159 6d 65 20 61 6e 64 20 74 68 65 20 6a 6f 75 72 6e  me and the journ
1315a 61 6c 20 0a 20 20 2a 2a 20 66 69 6c 65 20 6e 61  al .  ** file na
1315b 6d 65 2e 20 54 68 65 20 6c 61 79 6f 75 74 20 69  me. The layout i
1315c 6e 20 6d 65 6d 6f 72 79 20 69 73 20 61 73 20 66  n memory is as f
1315d 6f 6c 6c 6f 77 73 3a 0a 20 20 2a 2a 0a 20 20 2a  ollows:.  **.  *
1315e 2a 20 20 20 20 20 50 61 67 65 72 20 6f 62 6a 65  *     Pager obje
1315f 63 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ct              
13160 20 20 20 20 20 20 28 73 69 7a 65 6f 66 28 50 61        (sizeof(Pa
13161 67 65 72 29 20 62 79 74 65 73 29 0a 20 20 2a 2a  ger) bytes).  **
13162 20 20 20 20 20 50 43 61 63 68 65 20 6f 62 6a 65       PCache obje
13163 63 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ct              
13164 20 20 20 20 20 28 73 71 6c 69 74 65 33 50 63 61       (sqlite3Pca
13165 63 68 65 53 69 7a 65 28 29 20 62 79 74 65 73 29  cheSize() bytes)
13166 0a 20 20 2a 2a 20 20 20 20 20 44 61 74 61 62 61  .  **     Databa
13167 73 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 20  se file handle  
13168 20 20 20 20 20 20 20 20 20 20 28 70 56 66 73 2d            (pVfs-
13169 3e 73 7a 4f 73 46 69 6c 65 20 62 79 74 65 73 29  >szOsFile bytes)
1316a 0a 20 20 2a 2a 20 20 20 20 20 53 75 62 2d 6a 6f  .  **     Sub-jo
1316b 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 6e 64 6c  urnal file handl
1316c 65 20 20 20 20 20 20 20 20 20 28 6a 6f 75 72 6e  e         (journ
1316d 61 6c 46 69 6c 65 53 69 7a 65 20 62 79 74 65 73  alFileSize bytes
1316e 29 0a 20 20 2a 2a 20 20 20 20 20 4d 61 69 6e 20  ).  **     Main 
1316f 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 6e  journal file han
13170 64 6c 65 20 20 20 20 20 20 20 20 28 6a 6f 75 72  dle        (jour
13171 6e 61 6c 46 69 6c 65 53 69 7a 65 20 62 79 74 65  nalFileSize byte
13172 73 29 0a 20 20 2a 2a 20 20 20 20 20 44 61 74 61  s).  **     Data
13173 62 61 73 65 20 66 69 6c 65 20 6e 61 6d 65 20 20  base file name  
13174 20 20 20 20 20 20 20 20 20 20 20 20 28 6e 50 61              (nPa
13175 74 68 6e 61 6d 65 2b 31 20 62 79 74 65 73 29 0a  thname+1 bytes).
13176 20 20 2a 2a 20 20 20 20 20 4a 6f 75 72 6e 61 6c    **     Journal
13177 20 66 69 6c 65 20 6e 61 6d 65 20 20 20 20 20 20   file name      
13178 20 20 20 20 20 20 20 20 20 28 6e 50 61 74 68 6e           (nPathn
13179 61 6d 65 2b 38 2b 31 20 62 79 74 65 73 29 0a 20  ame+8+1 bytes). 
1317a 20 2a 2f 0a 20 20 70 50 74 72 20 3d 20 28 75 38   */.  pPtr = (u8
1317b 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   *)sqlite3Malloc
1317c 5a 65 72 6f 28 0a 20 20 20 20 52 4f 55 4e 44 38  Zero(.    ROUND8
1317d 28 73 69 7a 65 6f 66 28 2a 70 50 61 67 65 72 29  (sizeof(*pPager)
1317e 29 20 2b 20 20 20 20 20 20 2f 2a 20 50 61 67 65  ) +      /* Page
1317f 72 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20  r structure */. 
13180 20 20 20 52 4f 55 4e 44 38 28 70 63 61 63 68 65     ROUND8(pcache
13181 53 69 7a 65 29 20 2b 20 20 20 20 20 20 20 20 20  Size) +         
13182 20 20 2f 2a 20 50 43 61 63 68 65 20 6f 62 6a 65    /* PCache obje
13183 63 74 20 2a 2f 0a 20 20 20 20 52 4f 55 4e 44 38  ct */.    ROUND8
13184 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 29  (pVfs->szOsFile)
13185 20 2b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20   +       /* The 
13186 6d 61 69 6e 20 64 62 20 66 69 6c 65 20 2a 2f 0a  main db file */.
13187 20 20 20 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53      journalFileS
13188 69 7a 65 20 2a 20 32 20 2b 20 20 20 20 20 20 20  ize * 2 +       
13189 20 20 20 2f 2a 20 54 68 65 20 74 77 6f 20 6a 6f     /* The two jo
1318a 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f 20 0a  urnal files */ .
1318b 20 20 20 20 6e 50 61 74 68 6e 61 6d 65 20 2b 20      nPathname + 
1318c 31 20 2b 20 20 20 20 20 20 20 20 20 20 20 20 20  1 +             
1318d 20 20 20 2f 2a 20 7a 46 69 6c 65 6e 61 6d 65 20     /* zFilename 
1318e 2a 2f 0a 20 20 20 20 6e 50 61 74 68 6e 61 6d 65  */.    nPathname
1318f 20 2b 20 38 20 2b 20 31 20 20 20 20 20 20 20 20   + 8 + 1        
13190 20 20 20 20 20 20 2f 2a 20 7a 4a 6f 75 72 6e 61        /* zJourna
13191 6c 20 2a 2f 0a 20 20 29 3b 0a 20 20 61 73 73 65  l */.  );.  asse
13192 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41  rt( EIGHT_BYTE_A
13193 4c 49 47 4e 4d 45 4e 54 28 53 51 4c 49 54 45 5f  LIGNMENT(SQLITE_
13194 49 4e 54 5f 54 4f 5f 50 54 52 28 6a 6f 75 72 6e  INT_TO_PTR(journ
13195 61 6c 46 69 6c 65 53 69 7a 65 29 29 20 29 3b 0a  alFileSize)) );.
13196 20 20 69 66 28 20 21 70 50 74 72 20 29 7b 0a 20    if( !pPtr ){. 
13197 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
13198 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20  zPathname);.    
13199 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
1319a 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65  MEM;.  }.  pPage
1319b 72 20 3d 20 20 20 20 20 20 20 20 20 20 20 20 20  r =             
1319c 20 28 50 61 67 65 72 2a 29 28 70 50 74 72 29 3b   (Pager*)(pPtr);
1319d 0a 20 20 70 50 61 67 65 72 2d 3e 70 50 43 61 63  .  pPager->pPCac
1319e 68 65 20 3d 20 20 20 20 28 50 43 61 63 68 65 2a  he =    (PCache*
1319f 29 28 70 50 74 72 20 2b 3d 20 52 4f 55 4e 44 38  )(pPtr += ROUND8
131a0 28 73 69 7a 65 6f 66 28 2a 70 50 61 67 65 72 29  (sizeof(*pPager)
131a1 29 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 66 64  ));.  pPager->fd
131a2 20 3d 20 20 20 28 73 71 6c 69 74 65 33 5f 66 69   =   (sqlite3_fi
131a3 6c 65 2a 29 28 70 50 74 72 20 2b 3d 20 52 4f 55  le*)(pPtr += ROU
131a4 4e 44 38 28 70 63 61 63 68 65 53 69 7a 65 29 29  ND8(pcacheSize))
131a5 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 6a 66 64  ;.  pPager->sjfd
131a6 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65   = (sqlite3_file
131a7 2a 29 28 70 50 74 72 20 2b 3d 20 52 4f 55 4e 44  *)(pPtr += ROUND
131a8 38 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65  8(pVfs->szOsFile
131a9 29 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6a 66  ));.  pPager->jf
131aa 64 20 3d 20 20 28 73 71 6c 69 74 65 33 5f 66 69  d =  (sqlite3_fi
131ab 6c 65 2a 29 28 70 50 74 72 20 2b 3d 20 6a 6f 75  le*)(pPtr += jou
131ac 72 6e 61 6c 46 69 6c 65 53 69 7a 65 29 3b 0a 20  rnalFileSize);. 
131ad 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61   pPager->zFilena
131ae 6d 65 20 3d 20 20 20 20 28 63 68 61 72 2a 29 28  me =    (char*)(
131af 70 50 74 72 20 2b 3d 20 6a 6f 75 72 6e 61 6c 46  pPtr += journalF
131b0 69 6c 65 53 69 7a 65 29 3b 0a 20 20 61 73 73 65  ileSize);.  asse
131b1 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41  rt( EIGHT_BYTE_A
131b2 4c 49 47 4e 4d 45 4e 54 28 70 50 61 67 65 72 2d  LIGNMENT(pPager-
131b3 3e 6a 66 64 29 20 29 3b 0a 0a 20 20 2f 2a 20 46  >jfd) );..  /* F
131b4 69 6c 6c 20 69 6e 20 74 68 65 20 50 61 67 65 72  ill in the Pager
131b5 2e 7a 46 69 6c 65 6e 61 6d 65 20 61 6e 64 20 50  .zFilename and P
131b6 61 67 65 72 2e 7a 4a 6f 75 72 6e 61 6c 20 62 75  ager.zJournal bu
131b7 66 66 65 72 73 2c 20 69 66 20 72 65 71 75 69 72  ffers, if requir
131b8 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20 7a 50 61  ed. */.  if( zPa
131b9 74 68 6e 61 6d 65 20 29 7b 0a 20 20 20 20 70 50  thname ){.    pP
131ba 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 20 3d  ager->zJournal =
131bb 20 20 20 28 63 68 61 72 2a 29 28 70 50 74 72 20     (char*)(pPtr 
131bc 2b 3d 20 6e 50 61 74 68 6e 61 6d 65 20 2b 20 31  += nPathname + 1
131bd 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 50  );.    memcpy(pP
131be 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c  ager->zFilename,
131bf 20 7a 50 61 74 68 6e 61 6d 65 2c 20 6e 50 61 74   zPathname, nPat
131c0 68 6e 61 6d 65 29 3b 0a 20 20 20 20 6d 65 6d 63  hname);.    memc
131c1 70 79 28 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72  py(pPager->zJour
131c2 6e 61 6c 2c 20 7a 50 61 74 68 6e 61 6d 65 2c 20  nal, zPathname, 
131c3 6e 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20  nPathname);.    
131c4 6d 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e  memcpy(&pPager->
131c5 7a 4a 6f 75 72 6e 61 6c 5b 6e 50 61 74 68 6e 61  zJournal[nPathna
131c6 6d 65 5d 2c 20 22 2d 6a 6f 75 72 6e 61 6c 22 2c  me], "-journal",
131c7 20 38 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61   8);.    if( pPa
131c8 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 5b 30  ger->zFilename[0
131c9 5d 3d 3d 30 20 29 20 70 50 61 67 65 72 2d 3e 7a  ]==0 ) pPager->z
131ca 4a 6f 75 72 6e 61 6c 5b 30 5d 20 3d 20 30 3b 0a  Journal[0] = 0;.
131cb 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
131cc 28 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 7d  (zPathname);.  }
131cd 0a 20 20 70 50 61 67 65 72 2d 3e 70 56 66 73 20  .  pPager->pVfs 
131ce 3d 20 70 56 66 73 3b 0a 20 20 70 50 61 67 65 72  = pVfs;.  pPager
131cf 2d 3e 76 66 73 46 6c 61 67 73 20 3d 20 76 66 73  ->vfsFlags = vfs
131d0 46 6c 61 67 73 3b 0a 0a 20 20 2f 2a 20 4f 70 65  Flags;..  /* Ope
131d1 6e 20 74 68 65 20 70 61 67 65 72 20 66 69 6c 65  n the pager file
131d2 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a 46 69  ..  */.  if( zFi
131d3 6c 65 6e 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e  lename && zFilen
131d4 61 6d 65 5b 30 5d 20 26 26 20 21 6d 65 6d 44 62  ame[0] && !memDb
131d5 20 29 7b 0a 20 20 20 20 69 6e 74 20 66 6f 75 74   ){.    int fout
131d6 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
131d7 20 20 20 20 20 20 20 20 20 2f 2a 20 56 46 53 20           /* VFS 
131d8 66 6c 61 67 73 20 72 65 74 75 72 6e 65 64 20 62  flags returned b
131d9 79 20 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 20  y xOpen() */.   
131da 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f   rc = sqlite3OsO
131db 70 65 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72  pen(pVfs, pPager
131dc 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 70 50 61  ->zFilename, pPa
131dd 67 65 72 2d 3e 66 64 2c 20 76 66 73 46 6c 61 67  ger->fd, vfsFlag
131de 73 2c 20 26 66 6f 75 74 29 3b 0a 20 20 20 20 72  s, &fout);.    r
131df 65 61 64 4f 6e 6c 79 20 3d 20 28 66 6f 75 74 26  eadOnly = (fout&
131e0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
131e1 4f 4e 4c 59 29 3b 0a 0a 20 20 20 20 2f 2a 20 49  ONLY);..    /* I
131e2 66 20 74 68 65 20 66 69 6c 65 20 77 61 73 20 73  f the file was s
131e3 75 63 63 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e  uccessfully open
131e4 65 64 20 66 6f 72 20 72 65 61 64 2f 77 72 69 74  ed for read/writ
131e5 65 20 61 63 63 65 73 73 2c 0a 20 20 20 20 2a 2a  e access,.    **
131e6 20 63 68 6f 6f 73 65 20 61 20 64 65 66 61 75 6c   choose a defaul
131e7 74 20 70 61 67 65 20 73 69 7a 65 20 69 6e 20 63  t page size in c
131e8 61 73 65 20 77 65 20 68 61 76 65 20 74 6f 20 63  ase we have to c
131e9 72 65 61 74 65 20 74 68 65 0a 20 20 20 20 2a 2a  reate the.    **
131ea 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
131eb 54 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 65  The default page
131ec 20 73 69 7a 65 20 69 73 20 74 68 65 20 6d 61 78   size is the max
131ed 69 6d 75 6d 20 6f 66 3a 0a 20 20 20 20 2a 2a 0a  imum of:.    **.
131ee 20 20 20 20 2a 2a 20 20 20 20 2b 20 53 51 4c 49      **    + SQLI
131ef 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f  TE_DEFAULT_PAGE_
131f0 53 49 5a 45 2c 0a 20 20 20 20 2a 2a 20 20 20 20  SIZE,.    **    
131f1 2b 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75  + The value retu
131f2 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 4f  rned by sqlite3O
131f3 73 53 65 63 74 6f 72 53 69 7a 65 28 29 0a 20 20  sSectorSize().  
131f4 20 20 2a 2a 20 20 20 20 2b 20 54 68 65 20 6c 61    **    + The la
131f5 72 67 65 73 74 20 70 61 67 65 20 73 69 7a 65 20  rgest page size 
131f6 74 68 61 74 20 63 61 6e 20 62 65 20 77 72 69 74  that can be writ
131f7 74 65 6e 20 61 74 6f 6d 69 63 61 6c 6c 79 2e 0a  ten atomically..
131f8 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72      */.    if( r
131f9 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
131fa 21 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20  !readOnly ){.   
131fb 20 20 20 73 65 74 53 65 63 74 6f 72 53 69 7a 65     setSectorSize
131fc 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20  (pPager);.      
131fd 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f 44 45  assert(SQLITE_DE
131fe 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3c  FAULT_PAGE_SIZE<
131ff 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41  =SQLITE_MAX_DEFA
13200 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 29 3b 0a  ULT_PAGE_SIZE);.
13201 20 20 20 20 20 20 69 66 28 20 73 7a 50 61 67 65        if( szPage
13202 44 66 6c 74 3c 70 50 61 67 65 72 2d 3e 73 65 63  Dflt<pPager->sec
13203 74 6f 72 53 69 7a 65 20 29 7b 0a 20 20 20 20 20  torSize ){.     
13204 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73     if( pPager->s
13205 65 63 74 6f 72 53 69 7a 65 3e 53 51 4c 49 54 45  ectorSize>SQLITE
13206 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47  _MAX_DEFAULT_PAG
13207 45 5f 53 49 5a 45 20 29 7b 0a 20 20 20 20 20 20  E_SIZE ){.      
13208 20 20 20 20 73 7a 50 61 67 65 44 66 6c 74 20 3d      szPageDflt =
13209 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41   SQLITE_MAX_DEFA
1320a 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3b 0a 20  ULT_PAGE_SIZE;. 
1320b 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
1320c 20 20 20 20 20 20 20 20 73 7a 50 61 67 65 44 66          szPageDf
1320d 6c 74 20 3d 20 28 75 31 36 29 70 50 61 67 65 72  lt = (u16)pPager
1320e 2d 3e 73 65 63 74 6f 72 53 69 7a 65 3b 0a 20 20  ->sectorSize;.  
1320f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
13210 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
13211 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54  ABLE_ATOMIC_WRIT
13212 45 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20  E.      {.      
13213 20 20 69 6e 74 20 69 44 63 20 3d 20 73 71 6c 69    int iDc = sqli
13214 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 61  te3OsDeviceChara
13215 63 74 65 72 69 73 74 69 63 73 28 70 50 61 67 65  cteristics(pPage
13216 72 2d 3e 66 64 29 3b 0a 20 20 20 20 20 20 20 20  r->fd);.        
13217 69 6e 74 20 69 69 3b 0a 20 20 20 20 20 20 20 20  int ii;.        
13218 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f 49 4f  assert(SQLITE_IO
13219 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 3d 3d 28  CAP_ATOMIC512==(
1321a 35 31 32 3e 3e 38 29 29 3b 0a 20 20 20 20 20 20  512>>8));.      
1321b 20 20 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f    assert(SQLITE_
1321c 49 4f 43 41 50 5f 41 54 4f 4d 49 43 36 34 4b 3d  IOCAP_ATOMIC64K=
1321d 3d 28 36 35 35 33 36 3e 3e 38 29 29 3b 0a 20 20  =(65536>>8));.  
1321e 20 20 20 20 20 20 61 73 73 65 72 74 28 53 51 4c        assert(SQL
1321f 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f  ITE_MAX_DEFAULT_
13220 50 41 47 45 5f 53 49 5a 45 3c 3d 36 35 35 33 36  PAGE_SIZE<=65536
13221 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  );.        for(i
13222 69 3d 73 7a 50 61 67 65 44 66 6c 74 3b 20 69 69  i=szPageDflt; ii
13223 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46  <=SQLITE_MAX_DEF
13224 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3b 20  AULT_PAGE_SIZE; 
13225 69 69 3d 69 69 2a 32 29 7b 0a 20 20 20 20 20 20  ii=ii*2){.      
13226 20 20 20 20 69 66 28 20 69 44 63 26 28 53 51 4c      if( iDc&(SQL
13227 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43  ITE_IOCAP_ATOMIC
13228 7c 28 69 69 3e 3e 38 29 29 20 29 7b 0a 20 20 20  |(ii>>8)) ){.   
13229 20 20 20 20 20 20 20 20 20 73 7a 50 61 67 65 44           szPageD
1322a 66 6c 74 20 3d 20 69 69 3b 0a 20 20 20 20 20 20  flt = ii;.      
1322b 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
1322c 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20        }.#endif. 
1322d 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
1322e 20 20 2f 2a 20 49 66 20 61 20 74 65 6d 70 6f 72    /* If a tempor
1322f 61 72 79 20 66 69 6c 65 20 69 73 20 72 65 71 75  ary file is requ
13230 65 73 74 65 64 2c 20 69 74 20 69 73 20 6e 6f 74  ested, it is not
13231 20 6f 70 65 6e 65 64 20 69 6d 6d 65 64 69 61 74   opened immediat
13232 65 6c 79 2e 0a 20 20 20 20 2a 2a 20 49 6e 20 74  ely..    ** In t
13233 68 69 73 20 63 61 73 65 20 77 65 20 61 63 63 65  his case we acce
13234 70 74 20 74 68 65 20 64 65 66 61 75 6c 74 20 70  pt the default p
13235 61 67 65 20 73 69 7a 65 20 61 6e 64 20 64 65 6c  age size and del
13236 61 79 20 61 63 74 75 61 6c 6c 79 0a 20 20 20 20  ay actually.    
13237 2a 2a 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 66  ** opening the f
13238 69 6c 65 20 75 6e 74 69 6c 20 74 68 65 20 66 69  ile until the fi
13239 72 73 74 20 63 61 6c 6c 20 74 6f 20 4f 73 57 72  rst call to OsWr
1323a 69 74 65 28 29 2e 0a 20 20 20 20 2a 2a 0a 20 20  ite()..    **.  
1323b 20 20 2a 2a 20 54 68 69 73 20 62 72 61 6e 63 68    ** This branch
1323c 20 69 73 20 61 6c 73 6f 20 72 75 6e 20 66 6f 72   is also run for
1323d 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61   an in-memory da
1323e 74 61 62 61 73 65 2e 20 41 6e 20 69 6e 2d 6d 65  tabase. An in-me
1323f 6d 6f 72 79 0a 20 20 20 20 2a 2a 20 64 61 74 61  mory.    ** data
13240 62 61 73 65 20 69 73 20 74 68 65 20 73 61 6d 65  base is the same
13241 20 61 73 20 61 20 74 65 6d 70 2d 66 69 6c 65 20   as a temp-file 
13242 74 68 61 74 20 69 73 20 6e 65 76 65 72 20 77 72  that is never wr
13243 69 74 74 65 6e 20 6f 75 74 20 74 6f 0a 20 20 20  itten out to.   
13244 20 2a 2a 20 64 69 73 6b 20 61 6e 64 20 75 73 65   ** disk and use
13245 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 72  s an in-memory r
13246 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e  ollback journal.
13247 0a 20 20 20 20 2a 2f 20 0a 20 20 20 20 74 65 6d  .    */ .    tem
13248 70 46 69 6c 65 20 3d 20 31 3b 0a 20 20 20 20 70  pFile = 1;.    p
13249 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50  Pager->state = P
1324a 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 3b 0a  AGER_EXCLUSIVE;.
1324b 20 20 20 20 72 65 61 64 4f 6e 6c 79 20 3d 20 28      readOnly = (
1324c 76 66 73 46 6c 61 67 73 26 53 51 4c 49 54 45 5f  vfsFlags&SQLITE_
1324d 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a  OPEN_READONLY);.
1324e 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f    }..  /* The fo
1324f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 74 6f 20  llowing call to 
13250 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65  PagerSetPagesize
13251 28 29 20 73 65 72 76 65 73 20 74 6f 20 73 65 74  () serves to set
13252 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 0a 20   the value of . 
13253 20 2a 2a 20 50 61 67 65 72 2e 70 61 67 65 53 69   ** Pager.pageSi
13254 7a 65 20 61 6e 64 20 74 6f 20 61 6c 6c 6f 63 61  ze and to alloca
13255 74 65 20 74 68 65 20 50 61 67 65 72 2e 70 54 6d  te the Pager.pTm
13256 70 53 70 61 63 65 20 62 75 66 66 65 72 2e 0a 20  pSpace buffer.. 
13257 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   */.  if( rc==SQ
13258 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61  LITE_OK ){.    a
13259 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6d  ssert( pPager->m
1325a 65 6d 44 62 3d 3d 30 20 29 3b 0a 20 20 20 20 72  emDb==0 );.    r
1325b 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
1325c 53 65 74 50 61 67 65 73 69 7a 65 28 70 50 61 67  SetPagesize(pPag
1325d 65 72 2c 20 26 73 7a 50 61 67 65 44 66 6c 74 2c  er, &szPageDflt,
1325e 20 2d 31 29 3b 0a 20 20 20 20 74 65 73 74 63 61   -1);.    testca
1325f 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  se( rc!=SQLITE_O
13260 4b 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  K );.  }..  /* I
13261 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
13262 72 65 64 20 69 6e 20 65 69 74 68 65 72 20 6f 66  red in either of
13263 20 74 68 65 20 62 6c 6f 63 6b 73 20 61 62 6f 76   the blocks abov
13264 65 2c 20 66 72 65 65 20 74 68 65 20 0a 20 20 2a  e, free the .  *
13265 2a 20 50 61 67 65 72 20 73 74 72 75 63 74 75 72  * Pager structur
13266 65 20 61 6e 64 20 63 6c 6f 73 65 20 74 68 65 20  e and close the 
13267 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  file..  */.  if(
13268 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
13269 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70  {.    assert( !p
1326a 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65  Pager->pTmpSpace
1326b 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f   );.    sqlite3O
1326c 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 66  sClose(pPager->f
1326d 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  d);.    sqlite3_
1326e 66 72 65 65 28 70 50 61 67 65 72 29 3b 0a 20 20  free(pPager);.  
1326f 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
13270 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a  ..  /* Initializ
13271 65 20 74 68 65 20 50 43 61 63 68 65 20 6f 62 6a  e the PCache obj
13272 65 63 74 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74  ect. */.  assert
13273 28 20 6e 45 78 74 72 61 3c 31 30 30 30 20 29 3b  ( nExtra<1000 );
13274 0a 20 20 6e 45 78 74 72 61 20 3d 20 52 4f 55 4e  .  nExtra = ROUN
13275 44 38 28 6e 45 78 74 72 61 29 3b 0a 20 20 73 71  D8(nExtra);.  sq
13276 6c 69 74 65 33 50 63 61 63 68 65 4f 70 65 6e 28  lite3PcacheOpen(
13277 73 7a 50 61 67 65 44 66 6c 74 2c 20 6e 45 78 74  szPageDflt, nExt
13278 72 61 2c 20 21 6d 65 6d 44 62 2c 0a 20 20 20 20  ra, !memDb,.    
13279 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1327a 21 6d 65 6d 44 62 3f 70 61 67 65 72 53 74 72 65  !memDb?pagerStre
1327b 73 73 3a 30 2c 20 28 76 6f 69 64 20 2a 29 70 50  ss:0, (void *)pP
1327c 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 70 50  ager, pPager->pP
1327d 43 61 63 68 65 29 3b 0a 0a 20 20 50 41 47 45 52  Cache);..  PAGER
1327e 54 52 41 43 45 28 28 22 4f 50 45 4e 20 25 64 20  TRACE(("OPEN %d 
1327f 25 73 5c 6e 22 2c 20 46 49 4c 45 48 41 4e 44 4c  %s\n", FILEHANDL
13280 45 49 44 28 70 50 61 67 65 72 2d 3e 66 64 29 2c  EID(pPager->fd),
13281 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61   pPager->zFilena
13282 6d 65 29 29 3b 0a 20 20 49 4f 54 52 41 43 45 28  me));.  IOTRACE(
13283 28 22 4f 50 45 4e 20 25 70 20 25 73 5c 6e 22 2c  ("OPEN %p %s\n",
13284 20 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d   pPager, pPager-
13285 3e 7a 46 69 6c 65 6e 61 6d 65 29 29 0a 0a 20 20  >zFilename))..  
13286 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e  pPager->useJourn
13287 61 6c 20 3d 20 28 75 38 29 75 73 65 4a 6f 75 72  al = (u8)useJour
13288 6e 61 6c 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e  nal;.  pPager->n
13289 6f 52 65 61 64 6c 6f 63 6b 20 3d 20 28 6e 6f 52  oReadlock = (noR
1328a 65 61 64 6c 6f 63 6b 20 26 26 20 72 65 61 64 4f  eadlock && readO
1328b 6e 6c 79 29 20 3f 31 3a 30 3b 0a 20 20 2f 2a 20  nly) ?1:0;.  /* 
1328c 70 50 61 67 65 72 2d 3e 73 74 6d 74 4f 70 65 6e  pPager->stmtOpen
1328d 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50   = 0; */.  /* pP
1328e 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20  ager->stmtInUse 
1328f 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61  = 0; */.  /* pPa
13290 67 65 72 2d 3e 6e 52 65 66 20 3d 20 30 3b 20 2a  ger->nRef = 0; *
13291 2f 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69  /.  pPager->dbSi
13292 7a 65 56 61 6c 69 64 20 3d 20 28 75 38 29 6d 65  zeValid = (u8)me
13293 6d 44 62 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72  mDb;.  /* pPager
13294 2d 3e 73 74 6d 74 53 69 7a 65 20 3d 20 30 3b 20  ->stmtSize = 0; 
13295 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e  */.  /* pPager->
13296 73 74 6d 74 4a 53 69 7a 65 20 3d 20 30 3b 20 2a  stmtJSize = 0; *
13297 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e  /.  /* pPager->n
13298 50 61 67 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 70  Page = 0; */.  p
13299 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20  Pager->mxPgno = 
1329a 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f  SQLITE_MAX_PAGE_
1329b 43 4f 55 4e 54 3b 0a 20 20 2f 2a 20 70 50 61 67  COUNT;.  /* pPag
1329c 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45  er->state = PAGE
1329d 52 5f 55 4e 4c 4f 43 4b 3b 20 2a 2f 0a 20 20 61  R_UNLOCK; */.  a
1329e 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
1329f 74 61 74 65 20 3d 3d 20 28 74 65 6d 70 46 69 6c  tate == (tempFil
132a0 65 20 3f 20 50 41 47 45 52 5f 45 58 43 4c 55 53  e ? PAGER_EXCLUS
132a1 49 56 45 20 3a 20 50 41 47 45 52 5f 55 4e 4c 4f  IVE : PAGER_UNLO
132a2 43 4b 29 20 29 3b 0a 20 20 2f 2a 20 70 50 61 67  CK) );.  /* pPag
132a3 65 72 2d 3e 65 72 72 4d 61 73 6b 20 3d 20 30 3b  er->errMask = 0;
132a4 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 74 65   */.  pPager->te
132a5 6d 70 46 69 6c 65 20 3d 20 28 75 38 29 74 65 6d  mpFile = (u8)tem
132a6 70 46 69 6c 65 3b 0a 20 20 61 73 73 65 72 74 28  pFile;.  assert(
132a7 20 74 65 6d 70 46 69 6c 65 3d 3d 50 41 47 45 52   tempFile==PAGER
132a8 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52  _LOCKINGMODE_NOR
132a9 4d 41 4c 20 0a 20 20 20 20 20 20 20 20 20 20 7c  MAL .          |
132aa 7c 20 74 65 6d 70 46 69 6c 65 3d 3d 50 41 47 45  | tempFile==PAGE
132ab 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58  R_LOCKINGMODE_EX
132ac 43 4c 55 53 49 56 45 20 29 3b 0a 20 20 61 73 73  CLUSIVE );.  ass
132ad 65 72 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49  ert( PAGER_LOCKI
132ae 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45  NGMODE_EXCLUSIVE
132af 3d 3d 31 20 29 3b 0a 20 20 70 50 61 67 65 72 2d  ==1 );.  pPager-
132b0 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d  >exclusiveMode =
132b1 20 28 75 38 29 74 65 6d 70 46 69 6c 65 3b 20 0a   (u8)tempFile; .
132b2 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65    pPager->change
132b3 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 70 50 61 67  CountDone = pPag
132b4 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a 20 20  er->tempFile;.  
132b5 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 20 3d 20  pPager->memDb = 
132b6 28 75 38 29 6d 65 6d 44 62 3b 0a 20 20 70 50 61  (u8)memDb;.  pPa
132b7 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20  ger->readOnly = 
132b8 28 75 38 29 72 65 61 64 4f 6e 6c 79 3b 0a 20 20  (u8)readOnly;.  
132b9 2f 2a 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53  /* pPager->needS
132ba 79 6e 63 20 3d 20 30 3b 20 2a 2f 0a 20 20 61 73  ync = 0; */.  as
132bb 73 65 72 74 28 20 75 73 65 4a 6f 75 72 6e 61 6c  sert( useJournal
132bc 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70   || pPager->temp
132bd 46 69 6c 65 20 29 3b 0a 20 20 70 50 61 67 65 72  File );.  pPager
132be 2d 3e 6e 6f 53 79 6e 63 20 3d 20 70 50 61 67 65  ->noSync = pPage
132bf 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a 20 20 70  r->tempFile;.  p
132c0 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20  Pager->fullSync 
132c1 3d 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63  = pPager->noSync
132c2 20 3f 30 3a 31 3b 0a 20 20 70 50 61 67 65 72 2d   ?0:1;.  pPager-
132c3 3e 73 79 6e 63 5f 66 6c 61 67 73 20 3d 20 53 51  >sync_flags = SQ
132c4 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c  LITE_SYNC_NORMAL
132c5 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70  ;.  /* pPager->p
132c6 46 69 72 73 74 20 3d 20 30 3b 20 2a 2f 0a 20 20  First = 0; */.  
132c7 2f 2a 20 70 50 61 67 65 72 2d 3e 70 46 69 72 73  /* pPager->pFirs
132c8 74 53 79 6e 63 65 64 20 3d 20 30 3b 20 2a 2f 0a  tSynced = 0; */.
132c9 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70 4c 61    /* pPager->pLa
132ca 73 74 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61  st = 0; */.  pPa
132cb 67 65 72 2d 3e 6e 45 78 74 72 61 20 3d 20 28 75  ger->nExtra = (u
132cc 31 36 29 6e 45 78 74 72 61 3b 0a 20 20 70 50 61  16)nExtra;.  pPa
132cd 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65  ger->journalSize
132ce 4c 69 6d 69 74 20 3d 20 53 51 4c 49 54 45 5f 44  Limit = SQLITE_D
132cf 45 46 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53  EFAULT_JOURNAL_S
132d0 49 5a 45 5f 4c 49 4d 49 54 3b 0a 20 20 61 73 73  IZE_LIMIT;.  ass
132d1 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67  ert( isOpen(pPag
132d2 65 72 2d 3e 66 64 29 20 7c 7c 20 74 65 6d 70 46  er->fd) || tempF
132d3 69 6c 65 20 29 3b 0a 20 20 73 65 74 53 65 63 74  ile );.  setSect
132d4 6f 72 53 69 7a 65 28 70 50 61 67 65 72 29 3b 0a  orSize(pPager);.
132d5 20 20 69 66 28 20 21 75 73 65 4a 6f 75 72 6e 61    if( !useJourna
132d6 6c 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d  l ){.    pPager-
132d7 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d 20 50  >journalMode = P
132d8 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
132d9 5f 4f 46 46 3b 0a 20 20 7d 65 6c 73 65 20 69 66  _OFF;.  }else if
132da 28 20 6d 65 6d 44 62 20 29 7b 0a 20 20 20 20 70  ( memDb ){.    p
132db 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f  Pager->journalMo
132dc 64 65 20 3d 20 50 41 47 45 52 5f 4a 4f 55 52 4e  de = PAGER_JOURN
132dd 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 3b 0a 20  ALMODE_MEMORY;. 
132de 20 7d 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e   }.  /* pPager->
132df 78 42 75 73 79 48 61 6e 64 6c 65 72 20 3d 20 30  xBusyHandler = 0
132e0 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72  ; */.  /* pPager
132e1 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72  ->pBusyHandlerAr
132e2 67 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67  g = 0; */.  pPag
132e3 65 72 2d 3e 78 52 65 69 6e 69 74 65 72 20 3d 20  er->xReiniter = 
132e4 78 52 65 69 6e 69 74 3b 0a 20 20 2f 2a 20 6d 65  xReinit;.  /* me
132e5 6d 73 65 74 28 70 50 61 67 65 72 2d 3e 61 48 61  mset(pPager->aHa
132e6 73 68 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 50  sh, 0, sizeof(pP
132e7 61 67 65 72 2d 3e 61 48 61 73 68 29 29 3b 20 2a  ager->aHash)); *
132e8 2f 0a 20 20 2a 70 70 50 61 67 65 72 20 3d 20 70  /.  *ppPager = p
132e9 50 61 67 65 72 3b 0a 20 20 72 65 74 75 72 6e 20  Pager;.  return 
132ea 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 0a  SQLITE_OK;.}....
132eb 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
132ec 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 66  ion is called af
132ed 74 65 72 20 74 72 61 6e 73 69 74 69 6f 6e 69 6e  ter transitionin
132ee 67 20 66 72 6f 6d 20 50 41 47 45 52 5f 55 4e 4c  g from PAGER_UNL
132ef 4f 43 4b 20 74 6f 0a 2a 2a 20 50 41 47 45 52 5f  OCK to.** PAGER_
132f0 53 48 41 52 45 44 20 73 74 61 74 65 2e 20 49 74  SHARED state. It
132f1 20 74 65 73 74 73 20 69 66 20 74 68 65 72 65 20   tests if there 
132f2 69 73 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c  is a hot journal
132f3 20 70 72 65 73 65 6e 74 20 69 6e 0a 2a 2a 20 74   present in.** t
132f4 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 66  he file-system f
132f5 6f 72 20 74 68 65 20 67 69 76 65 6e 20 70 61 67  or the given pag
132f6 65 72 2e 20 41 20 68 6f 74 20 6a 6f 75 72 6e 61  er. A hot journa
132f7 6c 20 69 73 20 6f 6e 65 20 74 68 61 74 20 0a 2a  l is one that .*
132f8 2a 20 6e 65 65 64 73 20 74 6f 20 62 65 20 70 6c  * needs to be pl
132f9 61 79 65 64 20 62 61 63 6b 2e 20 41 63 63 6f 72  ayed back. Accor
132fa 64 69 6e 67 20 74 6f 20 74 68 69 73 20 66 75 6e  ding to this fun
132fb 63 74 69 6f 6e 2c 20 61 20 68 6f 74 2d 6a 6f 75  ction, a hot-jou
132fc 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 65 78 69  rnal.** file exi
132fd 73 74 73 20 69 66 20 74 68 65 20 66 6f 6c 6c 6f  sts if the follo
132fe 77 69 6e 67 20 63 72 69 74 65 72 69 61 20 61 72  wing criteria ar
132ff 65 20 6d 65 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a  e met:.**.**   *
13300 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   The journal fil
13301 65 20 65 78 69 73 74 73 20 69 6e 20 74 68 65 20  e exists in the 
13302 66 69 6c 65 20 73 79 73 74 65 6d 2c 20 61 6e 64  file system, and
13303 0a 2a 2a 20 20 20 2a 20 4e 6f 20 70 72 6f 63 65  .**   * No proce
13304 73 73 20 68 6f 6c 64 73 20 61 20 52 45 53 45 52  ss holds a RESER
13305 56 45 44 20 6f 72 20 67 72 65 61 74 65 72 20 6c  VED or greater l
13306 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ock on the datab
13307 61 73 65 20 66 69 6c 65 2c 20 61 6e 64 0a 2a 2a  ase file, and.**
13308 20 20 20 2a 20 54 68 65 20 64 61 74 61 62 61 73     * The databas
13309 65 20 66 69 6c 65 20 69 74 73 65 6c 66 20 69 73  e file itself is
1330a 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 30 20   greater than 0 
1330b 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 61  bytes in size, a
1330c 6e 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20 66 69  nd.**   * The fi
1330d 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 65 20  rst byte of the 
1330e 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 69  journal file exi
1330f 73 74 73 20 61 6e 64 20 69 73 20 6e 6f 74 20 30  sts and is not 0
13310 78 30 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  x00..**.** If th
13311 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f  e current size o
13312 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  f the database f
13313 69 6c 65 20 69 73 20 30 20 62 75 74 20 61 20 6a  ile is 0 but a j
13314 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 65  ournal file.** e
13315 78 69 73 74 73 2c 20 74 68 61 74 20 69 73 20 70  xists, that is p
13316 72 6f 62 61 62 6c 79 20 61 6e 20 6f 6c 64 20 6a  robably an old j
13317 6f 75 72 6e 61 6c 20 6c 65 66 74 20 6f 76 65 72  ournal left over
13318 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 0a 2a 2a   from a prior.**
13319 20 64 61 74 61 62 61 73 65 20 77 69 74 68 20 74   database with t
1331a 68 65 20 73 61 6d 65 20 6e 61 6d 65 2e 20 49 6e  he same name. In
1331b 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 6a   this case the j
1331c 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 0a 2a  ournal file is.*
1331d 2a 20 6a 75 73 74 20 64 65 6c 65 74 65 64 20 75  * just deleted u
1331e 73 69 6e 67 20 4f 73 44 65 6c 65 74 65 2c 20 2a  sing OsDelete, *
1331f 70 45 78 69 73 74 73 20 69 73 20 73 65 74 20 74  pExists is set t
13320 6f 20 30 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f  o 0 and SQLITE_O
13321 4b 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64  K.** is returned
13322 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
13323 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20 63 68  tine does not ch
13324 65 63 6b 20 69 66 20 74 68 65 72 65 20 69 73 20  eck if there is 
13325 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  a master journal
13326 20 66 69 6c 65 6e 61 6d 65 0a 2a 2a 20 61 74 20   filename.** at 
13327 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66  the end of the f
13328 69 6c 65 2e 20 49 66 20 74 68 65 72 65 20 69 73  ile. If there is
13329 2c 20 61 6e 64 20 74 68 61 74 20 6d 61 73 74 65  , and that maste
1332a 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a  r journal file.*
1332b 2a 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74  * does not exist
1332c 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e  , then the journ
1332d 61 6c 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 72  al file is not r
1332e 65 61 6c 6c 79 20 68 6f 74 2e 20 49 6e 20 74 68  eally hot. In th
1332f 69 73 0a 2a 2a 20 63 61 73 65 20 74 68 69 73 20  is.** case this 
13330 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72 65 74  routine will ret
13331 75 72 6e 20 61 20 66 61 6c 73 65 2d 70 6f 73 69  urn a false-posi
13332 74 69 76 65 2e 20 54 68 65 20 70 61 67 65 72 5f  tive. The pager_
13333 70 6c 61 79 62 61 63 6b 28 29 0a 2a 2a 20 72 6f  playback().** ro
13334 75 74 69 6e 65 20 77 69 6c 6c 20 64 69 73 63 6f  utine will disco
13335 76 65 72 20 74 68 61 74 20 74 68 65 20 6a 6f 75  ver that the jou
13336 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e 6f 74  rnal file is not
13337 20 72 65 61 6c 6c 79 20 68 6f 74 20 61 6e 64 20   really hot and 
13338 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 72 6f 6c  .** will not rol
13339 6c 20 69 74 20 62 61 63 6b 2e 20 0a 2a 2a 0a 2a  l it back. .**.*
1333a 2a 20 49 66 20 61 20 68 6f 74 2d 6a 6f 75 72 6e  * If a hot-journ
1333b 61 6c 20 66 69 6c 65 20 69 73 20 66 6f 75 6e 64  al file is found
1333c 20 74 6f 20 65 78 69 73 74 2c 20 2a 70 45 78 69   to exist, *pExi
1333d 73 74 73 20 69 73 20 73 65 74 20 74 6f 20 31 20  sts is set to 1 
1333e 61 6e 64 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f  and .** SQLITE_O
1333f 4b 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 6e  K returned. If n
13340 6f 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 66 69  o hot-journal fi
13341 6c 65 20 69 73 20 70 72 65 73 65 6e 74 2c 20 2a  le is present, *
13342 70 45 78 69 73 74 73 20 69 73 0a 2a 2a 20 73 65  pExists is.** se
13343 74 20 74 6f 20 30 20 61 6e 64 20 53 51 4c 49 54  t to 0 and SQLIT
13344 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 49  E_OK returned. I
13345 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63  f an IO error oc
13346 63 75 72 73 20 77 68 69 6c 65 20 74 72 79 69 6e  curs while tryin
13347 67 0a 2a 2a 20 74 6f 20 64 65 74 65 72 6d 69 6e  g.** to determin
13348 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  e whether or not
13349 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 66   a hot-journal f
1334a 69 6c 65 20 65 78 69 73 74 73 2c 20 74 68 65 20  ile exists, the 
1334b 49 4f 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65  IO error.** code
1334c 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64   is returned and
1334d 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 2a 70   the value of *p
1334e 45 78 69 73 74 73 20 69 73 20 75 6e 64 65 66 69  Exists is undefi
1334f 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ned..*/.static i
13350 6e 74 20 68 61 73 48 6f 74 4a 6f 75 72 6e 61 6c  nt hasHotJournal
13351 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
13352 69 6e 74 20 2a 70 45 78 69 73 74 73 29 7b 0a 20  int *pExists){. 
13353 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 20 63   sqlite3_vfs * c
13354 6f 6e 73 74 20 70 56 66 73 20 3d 20 70 50 61 67  onst pVfs = pPag
13355 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69 6e 74 20  er->pVfs;.  int 
13356 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
13357 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
13358 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e  urn code */.  in
13359 74 20 65 78 69 73 74 73 3b 20 20 20 20 20 20 20  t exists;       
1335a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1335b 72 75 65 20 69 66 20 61 20 6a 6f 75 72 6e 61 6c  rue if a journal
1335c 20 66 69 6c 65 20 69 73 20 70 72 65 73 65 6e 74   file is present
1335d 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
1335e 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 61 73  Pager!=0 );.  as
1335f 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 75 73  sert( pPager->us
13360 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 61 73  eJournal );.  as
13361 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61  sert( isOpen(pPa
13362 67 65 72 2d 3e 66 64 29 20 29 3b 0a 20 20 61 73  ger->fd) );.  as
13363 73 65 72 74 28 20 21 69 73 4f 70 65 6e 28 70 50  sert( !isOpen(pP
13364 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 20 20  ager->jfd) );.  
13365 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
13366 73 74 61 74 65 20 3c 3d 20 50 41 47 45 52 5f 53  state <= PAGER_S
13367 48 41 52 45 44 20 29 3b 0a 0a 20 20 2a 70 45 78  HARED );..  *pEx
13368 69 73 74 73 20 3d 20 30 3b 0a 20 20 72 63 20 3d  ists = 0;.  rc =
13369 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73   sqlite3OsAccess
1336a 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a  (pVfs, pPager->z
1336b 4a 6f 75 72 6e 61 6c 2c 20 53 51 4c 49 54 45 5f  Journal, SQLITE_
1336c 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20 26  ACCESS_EXISTS, &
1336d 65 78 69 73 74 73 29 3b 0a 20 20 69 66 28 20 72  exists);.  if( r
1336e 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
1336f 65 78 69 73 74 73 20 29 7b 0a 20 20 20 20 69 6e  exists ){.    in
13370 74 20 6c 6f 63 6b 65 64 3b 20 20 20 20 20 20 20  t locked;       
13371 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
13372 65 20 69 66 20 73 6f 6d 65 20 70 72 6f 63 65 73  e if some proces
13373 73 20 68 6f 6c 64 73 20 61 20 52 45 53 45 52 56  s holds a RESERV
13374 45 44 20 6c 6f 63 6b 20 2a 2f 0a 0a 20 20 20 20  ED lock */..    
13375 2f 2a 20 52 61 63 65 20 63 6f 6e 64 69 74 69 6f  /* Race conditio
13376 6e 20 68 65 72 65 3a 20 20 41 6e 6f 74 68 65 72  n here:  Another
13377 20 70 72 6f 63 65 73 73 20 6d 69 67 68 74 20 68   process might h
13378 61 76 65 20 62 65 65 6e 20 68 6f 6c 64 69 6e 67  ave been holding
13379 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 68 65 20   the.    ** the 
1337a 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 61 6e  RESERVED lock an
1337b 64 20 68 61 76 65 20 61 20 6a 6f 75 72 6e 61 6c  d have a journal
1337c 20 6f 70 65 6e 20 61 74 20 74 68 65 20 73 71 6c   open at the sql
1337d 69 74 65 33 4f 73 41 63 63 65 73 73 28 29 20 0a  ite3OsAccess() .
1337e 20 20 20 20 2a 2a 20 63 61 6c 6c 20 61 62 6f 76      ** call abov
1337f 65 2c 20 62 75 74 20 74 68 65 6e 20 64 65 6c 65  e, but then dele
13380 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61  te the journal a
13381 6e 64 20 64 72 6f 70 20 74 68 65 20 6c 6f 63 6b  nd drop the lock
13382 20 62 65 66 6f 72 65 0a 20 20 20 20 2a 2a 20 77   before.    ** w
13383 65 20 67 65 74 20 74 6f 20 74 68 65 20 66 6f 6c  e get to the fol
13384 6c 6f 77 69 6e 67 20 73 71 6c 69 74 65 33 4f 73  lowing sqlite3Os
13385 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
13386 6b 28 29 20 63 61 6c 6c 2e 20 20 49 66 20 74 68  k() call.  If th
13387 61 74 0a 20 20 20 20 2a 2a 20 69 73 20 74 68 65  at.    ** is the
13388 20 63 61 73 65 2c 20 74 68 69 73 20 72 6f 75 74   case, this rout
13389 69 6e 65 20 6d 69 67 68 74 20 74 68 69 6e 6b 20  ine might think 
1338a 74 68 65 72 65 20 69 73 20 61 20 68 6f 74 20 6a  there is a hot j
1338b 6f 75 72 6e 61 6c 20 77 68 65 6e 0a 20 20 20 20  ournal when.    
1338c 2a 2a 20 69 6e 20 66 61 63 74 20 74 68 65 72 65  ** in fact there
1338d 20 69 73 20 6e 6f 6e 65 2e 20 20 54 68 69 73 20   is none.  This 
1338e 72 65 73 75 6c 74 73 20 69 6e 20 61 20 66 61 6c  results in a fal
1338f 73 65 2d 70 6f 73 69 74 69 76 65 20 77 68 69 63  se-positive whic
13390 68 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 62 65  h will.    ** be
13391 20 64 65 61 6c 74 20 77 69 74 68 20 62 79 20 74   dealt with by t
13392 68 65 20 70 6c 61 79 62 61 63 6b 20 72 6f 75 74  he playback rout
13393 69 6e 65 2e 20 20 54 69 63 6b 65 74 20 23 33 38  ine.  Ticket #38
13394 38 33 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72  83..    */.    r
13395 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 43 68 65  c = sqlite3OsChe
13396 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 70  ckReservedLock(p
13397 50 61 67 65 72 2d 3e 66 64 2c 20 26 6c 6f 63 6b  Pager->fd, &lock
13398 65 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  ed);.    if( rc=
13399 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 6c  =SQLITE_OK && !l
1339a 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20 20 20 69  ocked ){.      i
1339b 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 20 20 20 20  nt nPage;..     
1339c 20 2f 2a 20 43 68 65 63 6b 20 74 68 65 20 73 69   /* Check the si
1339d 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ze of the databa
1339e 73 65 20 66 69 6c 65 2e 20 49 66 20 69 74 20 63  se file. If it c
1339f 6f 6e 73 69 73 74 73 20 6f 66 20 30 20 70 61 67  onsists of 0 pag
133a0 65 73 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  es,.      ** the
133a1 6e 20 64 65 6c 65 74 65 20 74 68 65 20 6a 6f 75  n delete the jou
133a2 72 6e 61 6c 20 66 69 6c 65 2e 20 53 65 65 20 74  rnal file. See t
133a3 68 65 20 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e  he header commen
133a4 74 20 61 62 6f 76 65 20 66 6f 72 20 0a 20 20 20  t above for .   
133a5 20 20 20 2a 2a 20 74 68 65 20 72 65 61 73 6f 6e     ** the reason
133a6 69 6e 67 20 68 65 72 65 2e 20 20 44 65 6c 65 74  ing here.  Delet
133a7 65 20 74 68 65 20 6f 62 73 6f 6c 65 74 65 20 6a  e the obsolete j
133a8 6f 75 72 6e 61 6c 20 66 69 6c 65 20 75 6e 64 65  ournal file unde
133a9 72 0a 20 20 20 20 20 20 2a 2a 20 61 20 52 45 53  r.      ** a RES
133aa 45 52 56 45 44 20 6c 6f 63 6b 20 74 6f 20 61 76  ERVED lock to av
133ab 6f 69 64 20 72 61 63 65 20 63 6f 6e 64 69 74 69  oid race conditi
133ac 6f 6e 73 20 61 6e 64 20 74 6f 20 61 76 6f 69 64  ons and to avoid
133ad 20 76 69 6f 6c 61 74 69 6e 67 0a 20 20 20 20 20   violating.     
133ae 20 2a 2a 20 5b 48 33 33 30 32 30 5d 2e 0a 20 20   ** [H33020]..  
133af 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20      */.      rc 
133b0 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61  = sqlite3PagerPa
133b1 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20  gecount(pPager, 
133b2 26 6e 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69  &nPage);.      i
133b3 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
133b4 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
133b5 6e 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20  nPage==0 ){.    
133b6 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 67        sqlite3Beg
133b7 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29  inBenignMalloc()
133b8 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
133b9 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50  sqlite3OsLock(pP
133ba 61 67 65 72 2d 3e 66 64 2c 20 52 45 53 45 52 56  ager->fd, RESERV
133bb 45 44 5f 4c 4f 43 4b 29 3d 3d 53 51 4c 49 54 45  ED_LOCK)==SQLITE
133bc 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
133bd 20 20 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65     sqlite3OsDele
133be 74 65 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d  te(pVfs, pPager-
133bf 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20  >zJournal, 0);. 
133c0 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
133c1 65 33 4f 73 55 6e 6c 6f 63 6b 28 70 50 61 67 65  e3OsUnlock(pPage
133c2 72 2d 3e 66 64 2c 20 53 48 41 52 45 44 5f 4c 4f  r->fd, SHARED_LO
133c3 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  CK);.          }
133c4 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
133c5 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f  e3EndBenignMallo
133c6 63 28 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  c();.        }el
133c7 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a  se{.          /*
133c8 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   The journal fil
133c9 65 20 65 78 69 73 74 73 20 61 6e 64 20 6e 6f 20  e exists and no 
133ca 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e  other connection
133cb 20 68 61 73 20 61 20 72 65 73 65 72 76 65 64 0a   has a reserved.
133cc 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20            ** or 
133cd 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20  greater lock on 
133ce 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
133cf 65 2e 20 4e 6f 77 20 63 68 65 63 6b 20 74 68 61  e. Now check tha
133d0 74 20 74 68 65 72 65 20 69 73 0a 20 20 20 20 20  t there is.     
133d1 20 20 20 20 20 2a 2a 20 61 74 20 6c 65 61 73 74       ** at least
133d2 20 6f 6e 65 20 6e 6f 6e 2d 7a 65 72 6f 20 62 79   one non-zero by
133d3 74 65 73 20 61 74 20 74 68 65 20 73 74 61 72 74  tes at the start
133d4 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
133d5 66 69 6c 65 2e 0a 20 20 20 20 20 20 20 20 20 20  file..          
133d6 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 2c 20  ** If there is, 
133d7 74 68 65 6e 20 77 65 20 63 6f 6e 73 69 64 65 72  then we consider
133d8 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 74 6f   this journal to
133d9 20 62 65 20 68 6f 74 2e 20 49 66 20 6e 6f 74 2c   be hot. If not,
133da 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69   .          ** i
133db 74 20 63 61 6e 20 62 65 20 69 67 6e 6f 72 65 64  t can be ignored
133dc 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  ..          */. 
133dd 20 20 20 20 20 20 20 20 20 69 6e 74 20 66 20 3d           int f =
133de 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41   SQLITE_OPEN_REA
133df 44 4f 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45  DONLY|SQLITE_OPE
133e0 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 3b 0a  N_MAIN_JOURNAL;.
133e1 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73            rc = s
133e2 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66  qlite3OsOpen(pVf
133e3 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72  s, pPager->zJour
133e4 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64  nal, pPager->jfd
133e5 2c 20 66 2c 20 26 66 29 3b 0a 20 20 20 20 20 20  , f, &f);.      
133e6 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
133e7 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
133e8 20 20 20 20 20 75 38 20 66 69 72 73 74 20 3d 20       u8 first = 
133e9 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  0;.            r
133ea 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61  c = sqlite3OsRea
133eb 64 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 28  d(pPager->jfd, (
133ec 76 6f 69 64 20 2a 29 26 66 69 72 73 74 2c 20 31  void *)&first, 1
133ed 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 0);.          
133ee 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
133ef 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41  _IOERR_SHORT_REA
133f0 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  D ){.           
133f1 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
133f2 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  K;.            }
133f3 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
133f4 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67  ite3OsClose(pPag
133f5 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20  er->jfd);.      
133f6 20 20 20 20 20 20 2a 70 45 78 69 73 74 73 20 3d        *pExists =
133f7 20 28 66 69 72 73 74 21 3d 30 29 3b 0a 20 20 20   (first!=0);.   
133f8 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28         }else if(
133f9 20 72 63 3d 3d 53 51 4c 49 54 45 5f 43 41 4e 54   rc==SQLITE_CANT
133fa 4f 50 45 4e 20 29 7b 0a 20 20 20 20 20 20 20 20  OPEN ){.        
133fb 20 20 20 20 2f 2a 20 49 66 20 77 65 20 63 61 6e      /* If we can
133fc 6e 6f 74 20 6f 70 65 6e 20 74 68 65 20 72 6f 6c  not open the rol
133fd 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 66 69  lback journal fi
133fe 6c 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73  le in order to s
133ff 65 65 20 69 66 0a 20 20 20 20 20 20 20 20 20 20  ee if.          
13400 20 20 2a 2a 20 69 74 73 20 68 61 73 20 61 20 7a    ** its has a z
13401 65 72 6f 20 68 65 61 64 65 72 2c 20 74 68 61 74  ero header, that
13402 20 6d 69 67 68 74 20 62 65 20 64 75 65 20 74 6f   might be due to
13403 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 2c 20 6f   an I/O error, o
13404 72 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a  r.            **
13405 20 69 74 20 6d 69 67 68 74 20 62 65 20 64 75 65   it might be due
13406 20 74 6f 20 74 68 65 20 72 61 63 65 20 63 6f 6e   to the race con
13407 64 69 74 69 6f 6e 20 64 65 73 63 72 69 62 65 64  dition described
13408 20 61 62 6f 76 65 20 61 6e 64 20 69 6e 0a 20 20   above and in.  
13409 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 69 63            ** tic
1340a 6b 65 74 20 23 33 38 38 33 2e 20 20 45 69 74 68  ket #3883.  Eith
1340b 65 72 20 77 61 79 2c 20 61 73 73 75 6d 65 20 74  er way, assume t
1340c 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  hat the journal 
1340d 69 73 20 68 6f 74 2e 0a 20 20 20 20 20 20 20 20  is hot..        
1340e 20 20 20 20 2a 2a 20 54 68 69 73 20 6d 69 67 68      ** This migh
1340f 74 20 62 65 20 61 20 66 61 6c 73 65 20 70 6f 73  t be a false pos
13410 69 74 69 76 65 2e 20 20 42 75 74 20 69 66 20 69  itive.  But if i
13411 74 20 69 73 2c 20 74 68 65 6e 20 74 68 65 0a 20  t is, then the. 
13412 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 75             ** au
13413 74 6f 6d 61 74 69 63 20 6a 6f 75 72 6e 61 6c 20  tomatic journal 
13414 70 6c 61 79 62 61 63 6b 20 61 6e 64 20 72 65 63  playback and rec
13415 6f 76 65 72 79 20 6d 65 63 68 61 6e 69 73 6d 20  overy mechanism 
13416 77 69 6c 6c 20 64 65 61 6c 0a 20 20 20 20 20 20  will deal.      
13417 20 20 20 20 20 20 2a 2a 20 77 69 74 68 20 69 74        ** with it
13418 20 75 6e 64 65 72 20 61 6e 20 45 58 43 4c 55 53   under an EXCLUS
13419 49 56 45 20 6c 6f 63 6b 20 77 68 65 72 65 20 77  IVE lock where w
1341a 65 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f  e do not need to
1341b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
1341c 77 6f 72 72 79 20 73 6f 20 6d 75 63 68 20 77 69  worry so much wi
1341d 74 68 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f  th race conditio
1341e 6e 73 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20  ns..            
1341f 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  */.            *
13420 70 45 78 69 73 74 73 20 3d 20 31 3b 0a 20 20 20  pExists = 1;.   
13421 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51           rc = SQ
13422 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20  LITE_OK;.       
13423 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
13424 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
13425 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
13426 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65  ../*.** Read the
13427 20 63 6f 6e 74 65 6e 74 20 66 6f 72 20 70 61 67   content for pag
13428 65 20 70 50 67 20 6f 75 74 20 6f 66 20 74 68 65  e pPg out of the
13429 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
1342a 6e 64 20 69 6e 74 6f 20 0a 2a 2a 20 70 50 67 2d  nd into .** pPg-
1342b 3e 70 44 61 74 61 2e 20 41 20 73 68 61 72 65 64  >pData. A shared
1342c 20 6c 6f 63 6b 20 6f 72 20 67 72 65 61 74 65 72   lock or greater
1342d 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 6f 6e   must be held on
1342e 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a   the database.**
1342f 20 66 69 6c 65 20 62 65 66 6f 72 65 20 74 68 69   file before thi
13430 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
13431 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70  lled..**.** If p
13432 61 67 65 20 31 20 69 73 20 72 65 61 64 2c 20 74  age 1 is read, t
13433 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66  hen the value of
13434 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72   Pager.dbFileVer
13435 73 5b 5d 20 69 73 20 73 65 74 20 74 6f 0a 2a 2a  s[] is set to.**
13436 20 74 68 65 20 76 61 6c 75 65 20 72 65 61 64 20   the value read 
13437 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
13438 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  e file..**.** If
13439 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63   an IO error occ
1343a 75 72 73 2c 20 74 68 65 6e 20 74 68 65 20 49 4f  urs, then the IO
1343b 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e   error is return
1343c 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72  ed to the caller
1343d 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20  ..** Otherwise, 
1343e 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
1343f 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  urned..*/.static
13440 20 69 6e 74 20 72 65 61 64 44 62 50 61 67 65 28   int readDbPage(
13441 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 50  PgHdr *pPg){.  P
13442 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70  ager *pPager = p
13443 50 67 2d 3e 70 50 61 67 65 72 3b 20 2f 2a 20 50  Pg->pPager; /* P
13444 61 67 65 72 20 6f 62 6a 65 63 74 20 61 73 73 6f  ager object asso
13445 63 69 61 74 65 64 20 77 69 74 68 20 70 61 67 65  ciated with page
13446 20 70 50 67 20 2a 2f 0a 20 20 50 67 6e 6f 20 70   pPg */.  Pgno p
13447 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b  gno = pPg->pgno;
13448 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e         /* Page n
13449 75 6d 62 65 72 20 74 6f 20 72 65 61 64 20 2a 2f  umber to read */
1344a 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
1344b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1344c 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
1344d 2f 0a 20 20 69 36 34 20 69 4f 66 66 73 65 74 3b  /.  i64 iOffset;
1344e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1344f 20 2f 2a 20 42 79 74 65 20 6f 66 66 73 65 74 20   /* Byte offset 
13450 6f 66 20 66 69 6c 65 20 74 6f 20 72 65 61 64 20  of file to read 
13451 66 72 6f 6d 20 2a 2f 0a 0a 20 20 61 73 73 65 72  from */..  asser
13452 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  t( pPager->state
13453 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 26  >=PAGER_SHARED &
13454 26 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 61 73  & !MEMDB );.  as
13455 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61  sert( isOpen(pPa
13456 67 65 72 2d 3e 66 64 29 20 29 3b 0a 0a 20 20 69  ger->fd) );..  i
13457 66 28 20 4e 45 56 45 52 28 21 69 73 4f 70 65 6e  f( NEVER(!isOpen
13458 28 70 50 61 67 65 72 2d 3e 66 64 29 29 20 29 7b  (pPager->fd)) ){
13459 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
1345a 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b  ger->tempFile );
1345b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 50 67 2d  .    memset(pPg-
1345c 3e 70 44 61 74 61 2c 20 30 2c 20 70 50 61 67 65  >pData, 0, pPage
1345d 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20  r->pageSize);.  
1345e 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1345f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 4f 66 66 73 65  OK;.  }.  iOffse
13460 74 20 3d 20 28 70 67 6e 6f 2d 31 29 2a 28 69 36  t = (pgno-1)*(i6
13461 34 29 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  4)pPager->pageSi
13462 7a 65 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  ze;.  rc = sqlit
13463 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d  e3OsRead(pPager-
13464 3e 66 64 2c 20 70 50 67 2d 3e 70 44 61 74 61 2c  >fd, pPg->pData,
13465 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a   pPager->pageSiz
13466 65 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20 20 69  e, iOffset);.  i
13467 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f  f( rc==SQLITE_IO
13468 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29  ERR_SHORT_READ )
13469 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
1346a 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20  E_OK;.  }.  if( 
1346b 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 75  pgno==1 ){.    u
1346c 38 20 2a 64 62 46 69 6c 65 56 65 72 73 20 3d 20  8 *dbFileVers = 
1346d 26 28 28 75 38 2a 29 70 50 67 2d 3e 70 44 61 74  &((u8*)pPg->pDat
1346e 61 29 5b 32 34 5d 3b 0a 20 20 20 20 6d 65 6d 63  a)[24];.    memc
1346f 70 79 28 26 70 50 61 67 65 72 2d 3e 64 62 46 69  py(&pPager->dbFi
13470 6c 65 56 65 72 73 2c 20 64 62 46 69 6c 65 56 65  leVers, dbFileVe
13471 72 73 2c 20 73 69 7a 65 6f 66 28 70 50 61 67 65  rs, sizeof(pPage
13472 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 29 3b  r->dbFileVers));
13473 0a 20 20 7d 0a 20 20 43 4f 44 45 43 31 28 70 50  .  }.  CODEC1(pP
13474 61 67 65 72 2c 20 70 50 67 2d 3e 70 44 61 74 61  ager, pPg->pData
13475 2c 20 70 67 6e 6f 2c 20 33 2c 20 72 63 20 3d 20  , pgno, 3, rc = 
13476 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 0a  SQLITE_NOMEM);..
13477 20 20 50 41 47 45 52 5f 49 4e 43 52 28 73 71 6c    PAGER_INCR(sql
13478 69 74 65 33 5f 70 61 67 65 72 5f 72 65 61 64 64  ite3_pager_readd
13479 62 5f 63 6f 75 6e 74 29 3b 0a 20 20 50 41 47 45  b_count);.  PAGE
1347a 52 5f 49 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e  R_INCR(pPager->n
1347b 52 65 61 64 29 3b 0a 20 20 49 4f 54 52 41 43 45  Read);.  IOTRACE
1347c 28 28 22 50 47 49 4e 20 25 70 20 25 64 5c 6e 22  (("PGIN %p %d\n"
1347d 2c 20 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 29  , pPager, pgno))
1347e 3b 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28  ;.  PAGERTRACE((
1347f 22 46 45 54 43 48 20 25 64 20 70 61 67 65 20 25  "FETCH %d page %
13480 64 20 68 61 73 68 28 25 30 38 78 29 5c 6e 22 2c  d hash(%08x)\n",
13481 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
13482 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c  PAGERID(pPager),
13483 20 70 67 6e 6f 2c 20 70 61 67 65 72 5f 70 61 67   pgno, pager_pag
13484 65 68 61 73 68 28 70 50 67 29 29 29 3b 0a 0a 20  ehash(pPg)));.. 
13485 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
13486 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
13487 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20  on is called to 
13488 6f 62 74 61 69 6e 20 61 20 73 68 61 72 65 64 20  obtain a shared 
13489 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61  lock on the data
1348a 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 49 74  base file..** It
1348b 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 63   is illegal to c
1348c 61 6c 6c 20 73 71 6c 69 74 65 33 50 61 67 65 72  all sqlite3Pager
1348d 41 63 71 75 69 72 65 28 29 20 75 6e 74 69 6c 20  Acquire() until 
1348e 61 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 74  after this funct
1348f 69 6f 6e 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20  ion.** has been 
13490 73 75 63 63 65 73 73 66 75 6c 6c 79 20 63 61 6c  successfully cal
13491 6c 65 64 2e 20 49 66 20 61 20 73 68 61 72 65 64  led. If a shared
13492 2d 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 64 79  -lock is already
13493 20 68 65 6c 64 20 77 68 65 6e 0a 2a 2a 20 74 68   held when.** th
13494 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
13495 61 6c 6c 65 64 2c 20 69 74 20 69 73 20 61 20 6e  alled, it is a n
13496 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  o-op..**.** The 
13497 66 6f 6c 6c 6f 77 69 6e 67 20 6f 70 65 72 61 74  following operat
13498 69 6f 6e 73 20 61 72 65 20 61 6c 73 6f 20 70 65  ions are also pe
13499 72 66 6f 72 6d 65 64 20 62 79 20 74 68 69 73 20  rformed by this 
1349a 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  function..**.** 
1349b 20 20 31 29 20 49 66 20 74 68 65 20 70 61 67 65    1) If the page
1349c 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 69  r is currently i
1349d 6e 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 73  n PAGER_UNLOCK s
1349e 74 61 74 65 20 28 6e 6f 20 6c 6f 63 6b 20 68 65  tate (no lock he
1349f 6c 64 0a 2a 2a 20 20 20 20 20 20 6f 6e 20 74 68  ld.**      on th
134a0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 29  e database file)
134a1 2c 20 74 68 65 6e 20 61 6e 20 61 74 74 65 6d 70  , then an attemp
134a2 74 20 69 73 20 6d 61 64 65 20 74 6f 20 6f 62 74  t is made to obt
134a3 61 69 6e 20 61 0a 2a 2a 20 20 20 20 20 20 53 48  ain a.**      SH
134a4 41 52 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  ARED lock on the
134a5 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
134a6 49 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65  Immediately afte
134a7 72 20 6f 62 74 61 69 6e 69 6e 67 0a 2a 2a 20 20  r obtaining.**  
134a8 20 20 20 20 74 68 65 20 53 48 41 52 45 44 20 6c      the SHARED l
134a9 6f 63 6b 2c 20 74 68 65 20 66 69 6c 65 2d 73 79  ock, the file-sy
134aa 73 74 65 6d 20 69 73 20 63 68 65 63 6b 65 64 20  stem is checked 
134ab 66 6f 72 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61  for a hot-journa
134ac 6c 2c 0a 2a 2a 20 20 20 20 20 20 77 68 69 63 68  l,.**      which
134ad 20 69 73 20 70 6c 61 79 65 64 20 62 61 63 6b 20   is played back 
134ae 69 66 20 70 72 65 73 65 6e 74 2e 20 46 6f 6c 6c  if present. Foll
134af 6f 77 69 6e 67 20 61 6e 79 20 68 6f 74 2d 6a 6f  owing any hot-jo
134b0 75 72 6e 61 6c 20 0a 2a 2a 20 20 20 20 20 20 72  urnal .**      r
134b1 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 20 63 6f 6e  ollback, the con
134b2 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63 61 63  tents of the cac
134b3 68 65 20 61 72 65 20 76 61 6c 69 64 61 74 65 64  he are validated
134b4 20 62 79 20 63 68 65 63 6b 69 6e 67 0a 2a 2a 20   by checking.** 
134b5 20 20 20 20 20 74 68 65 20 27 63 68 61 6e 67 65       the 'change
134b6 2d 63 6f 75 6e 74 65 72 27 20 66 69 65 6c 64 20  -counter' field 
134b7 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
134b8 66 69 6c 65 20 68 65 61 64 65 72 20 61 6e 64 0a  file header and.
134b9 2a 2a 20 20 20 20 20 20 64 69 73 63 61 72 64 65  **      discarde
134ba 64 20 69 66 20 74 68 65 79 20 61 72 65 20 66 6f  d if they are fo
134bb 75 6e 64 20 74 6f 20 62 65 20 69 6e 76 61 6c 69  und to be invali
134bc 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20 49 66  d..**.**   2) If
134bd 20 74 68 65 20 70 61 67 65 72 20 69 73 20 72 75   the pager is ru
134be 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 69  nning in exclusi
134bf 76 65 2d 6d 6f 64 65 2c 20 61 6e 64 20 74 68 65  ve-mode, and the
134c0 72 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79  re are currently
134c1 0a 2a 2a 20 20 20 20 20 20 6e 6f 20 6f 75 74 73  .**      no outs
134c2 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63  tanding referenc
134c3 65 73 20 74 6f 20 61 6e 79 20 70 61 67 65 73 2c  es to any pages,
134c4 20 61 6e 64 20 69 73 20 69 6e 20 74 68 65 20 65   and is in the e
134c5 72 72 6f 72 20 73 74 61 74 65 2c 0a 2a 2a 20 20  rror state,.**  
134c6 20 20 20 20 74 68 65 6e 20 61 6e 20 61 74 74 65      then an atte
134c7 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 63  mpt is made to c
134c8 6c 65 61 72 20 74 68 65 20 65 72 72 6f 72 20 73  lear the error s
134c9 74 61 74 65 20 62 79 20 64 69 73 63 61 72 64 69  tate by discardi
134ca 6e 67 0a 2a 2a 20 20 20 20 20 20 74 68 65 20 63  ng.**      the c
134cb 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70  ontents of the p
134cc 61 67 65 20 63 61 63 68 65 20 61 6e 64 20 72 6f  age cache and ro
134cd 6c 6c 69 6e 67 20 62 61 63 6b 20 61 6e 79 20 6f  lling back any o
134ce 70 65 6e 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20  pen journal.**  
134cf 20 20 20 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20      file..**.** 
134d0 49 66 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e  If the operation
134d1 20 64 65 73 63 72 69 62 65 64 20 62 79 20 28 32   described by (2
134d2 29 20 61 62 6f 76 65 20 69 73 20 6e 6f 74 20 61  ) above is not a
134d3 74 74 65 6d 70 74 65 64 2c 20 61 6e 64 20 69 66  ttempted, and if
134d4 20 74 68 65 0a 2a 2a 20 70 61 67 65 72 20 69 73   the.** pager is
134d5 20 69 6e 20 61 6e 20 65 72 72 6f 72 20 73 74 61   in an error sta
134d6 74 65 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51  te other than SQ
134d7 4c 49 54 45 5f 46 55 4c 4c 20 77 68 65 6e 20 74  LITE_FULL when t
134d8 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a  his is called,.*
134d9 2a 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74  * the error stat
134da 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  e error code is 
134db 72 65 74 75 72 6e 65 64 2e 20 49 74 20 69 73 20  returned. It is 
134dc 70 65 72 6d 69 74 74 65 64 20 74 6f 20 72 65 61  permitted to rea
134dd 64 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73  d the.** databas
134de 65 20 77 68 65 6e 20 69 6e 20 53 51 4c 49 54 45  e when in SQLITE
134df 5f 46 55 4c 4c 20 65 72 72 6f 72 20 73 74 61 74  _FULL error stat
134e0 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69  e..**.** Otherwi
134e1 73 65 2c 20 69 66 20 65 76 65 72 79 74 68 69 6e  se, if everythin
134e2 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c  g is successful,
134e3 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
134e4 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 0a 2a 2a  turned. If an.**
134e5 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73   IO error occurs
134e6 20 77 68 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 74   while locking t
134e7 68 65 20 64 61 74 61 62 61 73 65 2c 20 63 68 65  he database, che
134e8 63 6b 69 6e 67 20 66 6f 72 20 61 20 68 6f 74 2d  cking for a hot-
134e9 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20  journal.** file 
134ea 6f 72 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20  or rolling back 
134eb 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20  a journal file, 
134ec 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64  the IO error cod
134ed 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  e is returned..*
134ee 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
134ef 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
134f0 72 53 68 61 72 65 64 4c 6f 63 6b 28 50 61 67 65  rSharedLock(Page
134f1 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e  r *pPager){.  in
134f2 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
134f3 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
134f4 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
134f5 2a 2f 0a 20 20 69 6e 74 20 69 73 45 72 72 6f 72  */.  int isError
134f6 52 65 73 65 74 20 3d 20 30 3b 20 20 20 20 20 20  Reset = 0;      
134f7 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
134f8 69 66 20 72 65 63 6f 76 65 72 69 6e 67 20 66 72  if recovering fr
134f9 6f 6d 20 65 72 72 6f 72 20 73 74 61 74 65 20 2a  om error state *
134fa 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75  /..  /* This rou
134fb 74 69 6e 65 20 69 73 20 6f 6e 6c 79 20 63 61 6c  tine is only cal
134fc 6c 65 64 20 66 72 6f 6d 20 62 2d 74 72 65 65 20  led from b-tree 
134fd 61 6e 64 20 6f 6e 6c 79 20 77 68 65 6e 20 74 68  and only when th
134fe 65 72 65 20 61 72 65 20 6e 6f 0a 20 20 2a 2a 20  ere are no.  ** 
134ff 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65  outstanding page
13500 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73  s */.  assert( s
13501 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43  qlite3PcacheRefC
13502 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43  ount(pPager->pPC
13503 61 63 68 65 29 3d 3d 30 20 29 3b 0a 20 20 69 66  ache)==0 );.  if
13504 28 20 4e 45 56 45 52 28 4d 45 4d 44 42 20 26 26  ( NEVER(MEMDB &&
13505 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
13506 29 20 29 7b 20 72 65 74 75 72 6e 20 70 50 61 67  ) ){ return pPag
13507 65 72 2d 3e 65 72 72 43 6f 64 65 3b 20 7d 0a 0a  er->errCode; }..
13508 20 20 2f 2a 20 49 66 20 74 68 69 73 20 64 61 74    /* If this dat
13509 61 62 61 73 65 20 69 73 20 69 6e 20 61 6e 20 65  abase is in an e
1350a 72 72 6f 72 2d 73 74 61 74 65 2c 20 6e 6f 77 20  rror-state, now 
1350b 69 73 20 61 20 63 68 61 6e 63 65 20 74 6f 20 63  is a chance to c
1350c 6c 65 61 72 0a 20 20 2a 2a 20 74 68 65 20 65 72  lear.  ** the er
1350d 72 6f 72 2e 20 44 69 73 63 61 72 64 20 74 68 65  ror. Discard the
1350e 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
1350f 20 70 61 67 65 72 2d 63 61 63 68 65 20 61 6e 64   pager-cache and
13510 20 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 61   rollback.  ** a
13511 6e 79 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 69  ny hot journal i
13512 6e 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65  n the file-syste
13513 6d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50  m..  */.  if( pP
13514 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b  ager->errCode ){
13515 0a 20 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28  .    if( isOpen(
13516 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c 7c 20  pPager->jfd) || 
13517 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c  pPager->zJournal
13518 20 29 7b 0a 20 20 20 20 20 20 69 73 45 72 72 6f   ){.      isErro
13519 72 52 65 73 65 74 20 3d 20 31 3b 0a 20 20 20 20  rReset = 1;.    
1351a 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72  }.    pPager->er
1351b 72 43 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 4f  rCode = SQLITE_O
1351c 4b 3b 0a 20 20 20 20 70 61 67 65 72 5f 72 65 73  K;.    pager_res
1351d 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a  et(pPager);.  }.
1351e 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73  .  if( pPager->s
1351f 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f  tate==PAGER_UNLO
13520 43 4b 20 7c 7c 20 69 73 45 72 72 6f 72 52 65 73  CK || isErrorRes
13521 65 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  et ){.    sqlite
13522 33 5f 76 66 73 20 2a 20 63 6f 6e 73 74 20 70 56  3_vfs * const pV
13523 66 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66  fs = pPager->pVf
13524 73 3b 0a 20 20 20 20 69 6e 74 20 69 73 48 6f 74  s;.    int isHot
13525 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 20  Journal = 0;.   
13526 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20   assert( !MEMDB 
13527 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  );.    assert( s
13528 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43  qlite3PcacheRefC
13529 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43  ount(pPager->pPC
1352a 61 63 68 65 29 3d 3d 30 20 29 3b 0a 20 20 20 20  ache)==0 );.    
1352b 69 66 28 20 70 50 61 67 65 72 2d 3e 6e 6f 52 65  if( pPager->noRe
1352c 61 64 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20  adlock ){.      
1352d 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
1352e 72 65 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 20 20  readOnly );.    
1352f 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20    pPager->state 
13530 3d 20 50 41 47 45 52 5f 53 48 41 52 45 44 3b 0a  = PAGER_SHARED;.
13531 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
13532 20 72 63 20 3d 20 70 61 67 65 72 5f 77 61 69 74   rc = pager_wait
13533 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c  _on_lock(pPager,
13534 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20   SHARED_LOCK);. 
13535 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
13536 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
13537 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
13538 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 55  ->state==PAGER_U
13539 4e 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 20 20 20  NLOCK );.       
1353a 20 72 65 74 75 72 6e 20 70 61 67 65 72 5f 65 72   return pager_er
1353b 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b  ror(pPager, rc);
1353c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1353d 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
1353e 72 2d 3e 73 74 61 74 65 3e 3d 53 48 41 52 45 44  r->state>=SHARED
1353f 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 20 20 2f 2a  _LOCK );..    /*
13540 20 49 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69   If a journal fi
13541 6c 65 20 65 78 69 73 74 73 2c 20 61 6e 64 20 74  le exists, and t
13542 68 65 72 65 20 69 73 20 6e 6f 20 52 45 53 45 52  here is no RESER
13543 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a  VED lock on the.
13544 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20      ** database 
13545 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 65 69  file, then it ei
13546 74 68 65 72 20 6e 65 65 64 73 20 74 6f 20 62 65  ther needs to be
13547 20 70 6c 61 79 65 64 20 62 61 63 6b 20 6f 72 20   played back or 
13548 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a 2f 0a  deleted..    */.
13549 20 20 20 20 69 66 28 20 21 69 73 45 72 72 6f 72      if( !isError
1354a 52 65 73 65 74 20 29 7b 0a 20 20 20 20 20 20 61  Reset ){.      a
1354b 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
1354c 74 61 74 65 20 3c 3d 20 50 41 47 45 52 5f 53 48  tate <= PAGER_SH
1354d 41 52 45 44 20 29 3b 0a 20 20 20 20 20 20 72 63  ARED );.      rc
1354e 20 3d 20 68 61 73 48 6f 74 4a 6f 75 72 6e 61 6c   = hasHotJournal
1354f 28 70 50 61 67 65 72 2c 20 26 69 73 48 6f 74 4a  (pPager, &isHotJ
13550 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 20 20 69  ournal);.      i
13551 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
13552 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f   ){.        goto
13553 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d   failed;.      }
13554 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69  .    }.    if( i
13555 73 45 72 72 6f 72 52 65 73 65 74 20 7c 7c 20 69  sErrorReset || i
13556 73 48 6f 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20  sHotJournal ){. 
13557 20 20 20 20 20 2f 2a 20 47 65 74 20 61 6e 20 45       /* Get an E
13558 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e  XCLUSIVE lock on
13559 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1355a 6c 65 2e 20 41 74 20 74 68 69 73 20 70 6f 69 6e  le. At this poin
1355b 74 20 69 74 20 69 73 0a 20 20 20 20 20 20 2a 2a  t it is.      **
1355c 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20   important that 
1355d 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20  a RESERVED lock 
1355e 69 73 20 6e 6f 74 20 6f 62 74 61 69 6e 65 64 20  is not obtained 
1355f 6f 6e 20 74 68 65 20 77 61 79 20 74 6f 20 74 68  on the way to th
13560 65 0a 20 20 20 20 20 20 2a 2a 20 45 58 43 4c 55  e.      ** EXCLU
13561 53 49 56 45 20 6c 6f 63 6b 2e 20 49 66 20 69 74  SIVE lock. If it
13562 20 77 65 72 65 2c 20 61 6e 6f 74 68 65 72 20 70   were, another p
13563 72 6f 63 65 73 73 20 6d 69 67 68 74 20 6f 70 65  rocess might ope
13564 6e 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 64  n the.      ** d
13565 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 64 65  atabase file, de
13566 74 65 63 74 20 74 68 65 20 52 45 53 45 52 56 45  tect the RESERVE
13567 44 20 6c 6f 63 6b 2c 20 61 6e 64 20 63 6f 6e 63  D lock, and conc
13568 6c 75 64 65 20 74 68 61 74 20 74 68 65 0a 20 20  lude that the.  
13569 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20      ** database 
1356a 69 73 20 73 61 66 65 20 74 6f 20 72 65 61 64 20  is safe to read 
1356b 77 68 69 6c 65 20 74 68 69 73 20 70 72 6f 63 65  while this proce
1356c 73 73 20 69 73 20 73 74 69 6c 6c 20 72 6f 6c 6c  ss is still roll
1356d 69 6e 67 20 74 68 65 20 0a 20 20 20 20 20 20 2a  ing the .      *
1356e 2a 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62 61  * hot-journal ba
1356f 63 6b 2e 0a 20 20 20 20 20 20 2a 2a 20 0a 20 20  ck..      ** .  
13570 20 20 20 20 2a 2a 20 42 65 63 61 75 73 65 20 74      ** Because t
13571 68 65 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20  he intermediate 
13572 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 73  RESERVED lock is
13573 20 6e 6f 74 20 72 65 71 75 65 73 74 65 64 2c 20   not requested, 
13574 61 6e 79 0a 20 20 20 20 20 20 2a 2a 20 6f 74 68  any.      ** oth
13575 65 72 20 70 72 6f 63 65 73 73 20 61 74 74 65 6d  er process attem
13576 70 74 69 6e 67 20 74 6f 20 61 63 63 65 73 73 20  pting to access 
13577 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
13578 65 20 77 69 6c 6c 20 67 65 74 20 74 6f 20 0a 20  e will get to . 
13579 20 20 20 20 20 2a 2a 20 74 68 69 73 20 70 6f 69       ** this poi
1357a 6e 74 20 69 6e 20 74 68 65 20 63 6f 64 65 20 61  nt in the code a
1357b 6e 64 20 66 61 69 6c 20 74 6f 20 6f 62 74 61 69  nd fail to obtai
1357c 6e 20 69 74 73 20 6f 77 6e 20 45 58 43 4c 55 53  n its own EXCLUS
1357d 49 56 45 20 6c 6f 63 6b 20 0a 20 20 20 20 20 20  IVE lock .      
1357e 2a 2a 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ** on the databa
1357f 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 20 20 2a  se file..      *
13580 2f 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67  /.      if( pPag
13581 65 72 2d 3e 73 74 61 74 65 3c 45 58 43 4c 55 53  er->state<EXCLUS
13582 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  IVE_LOCK ){.    
13583 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
13584 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66  OsLock(pPager->f
13585 64 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  d, EXCLUSIVE_LOC
13586 4b 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  K);.        if( 
13587 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
13588 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
13589 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67  pager_error(pPag
1358a 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 20  er, rc);.       
1358b 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a     goto failed;.
1358c 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1358d 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20    pPager->state 
1358e 3d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56  = PAGER_EXCLUSIV
1358f 45 3b 0a 20 20 20 20 20 20 7d 0a 20 0a 20 20 20  E;.      }. .   
13590 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 6a     /* Open the j
13591 6f 75 72 6e 61 6c 20 66 6f 72 20 72 65 61 64 2f  ournal for read/
13592 77 72 69 74 65 20 61 63 63 65 73 73 2e 20 54 68  write access. Th
13593 69 73 20 69 73 20 62 65 63 61 75 73 65 20 69 6e  is is because in
13594 20 0a 20 20 20 20 20 20 2a 2a 20 65 78 63 6c 75   .      ** exclu
13595 73 69 76 65 2d 61 63 63 65 73 73 20 6d 6f 64 65  sive-access mode
13596 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   the file descri
13597 70 74 6f 72 20 77 69 6c 6c 20 62 65 20 6b 65 70  ptor will be kep
13598 74 20 6f 70 65 6e 20 61 6e 64 0a 20 20 20 20 20  t open and.     
13599 20 2a 2a 20 70 6f 73 73 69 62 6c 79 20 75 73 65   ** possibly use
1359a 64 20 66 6f 72 20 61 20 74 72 61 6e 73 61 63 74  d for a transact
1359b 69 6f 6e 20 6c 61 74 65 72 20 6f 6e 2e 20 4f 6e  ion later on. On
1359c 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2c 20 74   some systems, t
1359d 68 65 0a 20 20 20 20 20 20 2a 2a 20 4f 73 54 72  he.      ** OsTr
1359e 75 6e 63 61 74 65 28 29 20 63 61 6c 6c 20 75 73  uncate() call us
1359f 65 64 20 69 6e 20 65 78 63 6c 75 73 69 76 65 2d  ed in exclusive-
135a0 61 63 63 65 73 73 20 6d 6f 64 65 20 61 6c 73 6f  access mode also
135a1 20 72 65 71 75 69 72 65 73 0a 20 20 20 20 20 20   requires.      
135a2 2a 2a 20 61 20 72 65 61 64 2f 77 72 69 74 65 20  ** a read/write 
135a3 66 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 20 20 20  file handle..   
135a4 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20     */.      if( 
135a5 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e  !isOpen(pPager->
135a6 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20  jfd) ){.        
135a7 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 20 20  int res;.       
135a8 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41   rc = sqlite3OsA
135a9 63 63 65 73 73 28 70 56 66 73 2c 70 50 61 67 65  ccess(pVfs,pPage
135aa 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 53 51 4c 49  r->zJournal,SQLI
135ab 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53  TE_ACCESS_EXISTS
135ac 2c 26 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20  ,&res);.        
135ad 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
135ae 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  K ){.          i
135af 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20  f( res ){.      
135b0 20 20 20 20 20 20 69 6e 74 20 66 6f 75 74 20 3d        int fout =
135b1 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   0;.            
135b2 69 6e 74 20 66 20 3d 20 53 51 4c 49 54 45 5f 4f  int f = SQLITE_O
135b3 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51  PEN_READWRITE|SQ
135b4 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a  LITE_OPEN_MAIN_J
135b5 4f 55 52 4e 41 4c 3b 0a 20 20 20 20 20 20 20 20  OURNAL;.        
135b6 20 20 20 20 61 73 73 65 72 74 28 20 21 70 50 61      assert( !pPa
135b7 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b  ger->tempFile );
135b8 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20  .            rc 
135b9 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28  = sqlite3OsOpen(
135ba 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a  pVfs, pPager->zJ
135bb 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e  ournal, pPager->
135bc 6a 66 64 2c 20 66 2c 20 26 66 6f 75 74 29 3b 0a  jfd, f, &fout);.
135bd 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65              asse
135be 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  rt( rc!=SQLITE_O
135bf 4b 20 7c 7c 20 69 73 4f 70 65 6e 28 70 50 61 67  K || isOpen(pPag
135c0 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 20 20 20 20  er->jfd) );.    
135c1 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
135c2 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 66 6f 75  SQLITE_OK && fou
135c3 74 26 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45  t&SQLITE_OPEN_RE
135c4 41 44 4f 4e 4c 59 20 29 7b 0a 20 20 20 20 20 20  ADONLY ){.      
135c5 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
135c6 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20  ITE_CANTOPEN;.  
135c7 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
135c8 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65  te3OsClose(pPage
135c9 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 20  r->jfd);.       
135ca 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
135cb 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
135cc 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6a 6f      /* If the jo
135cd 75 72 6e 61 6c 20 64 6f 65 73 20 6e 6f 74 20 65  urnal does not e
135ce 78 69 73 74 2c 20 69 74 20 75 73 75 61 6c 6c 79  xist, it usually
135cf 20 6d 65 61 6e 73 20 74 68 61 74 20 73 6f 6d 65   means that some
135d0 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a   .            **
135d1 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f   other connectio
135d2 6e 20 6d 61 6e 61 67 65 64 20 74 6f 20 67 65 74  n managed to get
135d3 20 69 6e 20 61 6e 64 20 72 6f 6c 6c 20 69 74 20   in and roll it 
135d4 62 61 63 6b 20 62 65 66 6f 72 65 20 0a 20 20 20  back before .   
135d5 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 69 73           ** this
135d6 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 74 61   connection obta
135d7 69 6e 65 64 20 74 68 65 20 65 78 63 6c 75 73 69  ined the exclusi
135d8 76 65 20 6c 6f 63 6b 20 61 62 6f 76 65 2e 20 4f  ve lock above. O
135d9 72 2c 20 69 74 20 0a 20 20 20 20 20 20 20 20 20  r, it .         
135da 20 20 20 2a 2a 20 6d 61 79 20 6d 65 61 6e 20 74     ** may mean t
135db 68 61 74 20 74 68 65 20 70 61 67 65 72 20 77 61  hat the pager wa
135dc 73 20 69 6e 20 74 68 65 20 65 72 72 6f 72 2d 73  s in the error-s
135dd 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 0a 20  tate when this. 
135de 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 75             ** fu
135df 6e 63 74 69 6f 6e 20 77 61 73 20 63 61 6c 6c 65  nction was calle
135e0 64 20 61 6e 64 20 74 68 65 20 6a 6f 75 72 6e 61  d and the journa
135e1 6c 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20  l file does not 
135e2 65 78 69 73 74 2e 20 20 2a 2f 0a 20 20 20 20 20  exist.  */.     
135e3 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65         rc = page
135e4 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f  r_end_transactio
135e5 6e 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20  n(pPager, 0);.  
135e6 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
135e7 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
135e8 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
135e9 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67  _OK ){.        g
135ea 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20  oto failed;.    
135eb 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 54 4f    }..      /* TO
135ec 44 4f 3a 20 57 68 79 20 61 72 65 20 74 68 65 73  DO: Why are thes
135ed 65 20 63 6c 65 61 72 65 64 20 68 65 72 65 3f 20  e cleared here? 
135ee 49 73 20 69 74 20 6e 65 63 65 73 73 61 72 79 3f  Is it necessary?
135ef 20 2a 2f 0a 20 20 20 20 20 20 70 50 61 67 65 72   */.      pPager
135f0 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64  ->journalStarted
135f1 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67   = 0;.      pPag
135f2 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d  er->journalOff =
135f3 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72   0;.      pPager
135f4 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d 20 30 3b  ->setMaster = 0;
135f5 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a  .      pPager->j
135f6 6f 75 72 6e 61 6c 48 64 72 20 3d 20 30 3b 0a 20  ournalHdr = 0;. 
135f7 0a 20 20 20 20 20 20 2f 2a 20 50 6c 61 79 62 61  .      /* Playba
135f8 63 6b 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68  ck and delete th
135f9 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 44 72 6f 70  e journal.  Drop
135fa 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77 72   the database wr
135fb 69 74 65 0a 20 20 20 20 20 20 2a 2a 20 6c 6f 63  ite.      ** loc
135fc 6b 20 61 6e 64 20 72 65 61 63 71 75 69 72 65 20  k and reacquire 
135fd 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 20 50  the read lock. P
135fe 75 72 67 65 20 74 68 65 20 63 61 63 68 65 20 62  urge the cache b
135ff 65 66 6f 72 65 0a 20 20 20 20 20 20 2a 2a 20 70  efore.      ** p
13600 6c 61 79 69 6e 67 20 62 61 63 6b 20 74 68 65 20  laying back the 
13601 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 73 6f 20 74  hot-journal so t
13602 68 61 74 20 77 65 20 64 6f 6e 27 74 20 65 6e 64  hat we don't end
13603 20 75 70 20 77 69 74 68 0a 20 20 20 20 20 20 2a   up with.      *
13604 2a 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e  * an inconsisten
13605 74 20 63 61 63 68 65 2e 0a 20 20 20 20 20 20 2a  t cache..      *
13606 2f 0a 20 20 20 20 20 20 69 66 28 20 69 73 4f 70  /.      if( isOp
13607 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
13608 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
13609 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 70  pager_playback(p
1360a 50 61 67 65 72 2c 20 31 29 3b 0a 20 20 20 20 20  Pager, 1);.     
1360b 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
1360c 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
1360d 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 72 72    rc = pager_err
1360e 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a  or(pPager, rc);.
1360f 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66            goto f
13610 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 20 20 7d  ailed;.        }
13611 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61  .      }.      a
13612 73 73 65 72 74 28 20 28 70 50 61 67 65 72 2d 3e  ssert( (pPager->
13613 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 48 41  state==PAGER_SHA
13614 52 45 44 29 0a 20 20 20 20 20 20 20 20 20 20 20  RED).           
13615 7c 7c 20 28 70 50 61 67 65 72 2d 3e 65 78 63 6c  || (pPager->excl
13616 75 73 69 76 65 4d 6f 64 65 20 26 26 20 70 50 61  usiveMode && pPa
13617 67 65 72 2d 3e 73 74 61 74 65 3e 50 41 47 45 52  ger->state>PAGER
13618 5f 53 48 41 52 45 44 29 0a 20 20 20 20 20 20 29  _SHARED).      )
13619 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  ;.    }..    if(
1361a 20 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70   pPager->pBackup
1361b 20 7c 7c 20 73 71 6c 69 74 65 33 50 63 61 63 68   || sqlite3Pcach
1361c 65 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65  ePagecount(pPage
1361d 72 2d 3e 70 50 43 61 63 68 65 29 3e 30 20 29 7b  r->pPCache)>0 ){
1361e 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 68  .      /* The sh
1361f 61 72 65 64 2d 6c 6f 63 6b 20 68 61 73 20 6a 75  ared-lock has ju
13620 73 74 20 62 65 65 6e 20 61 63 71 75 69 72 65 64  st been acquired
13621 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
13622 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 61   file.      ** a
13623 6e 64 20 74 68 65 72 65 20 61 72 65 20 61 6c 72  nd there are alr
13624 65 61 64 79 20 70 61 67 65 73 20 69 6e 20 74 68  eady pages in th
13625 65 20 63 61 63 68 65 20 28 66 72 6f 6d 20 61 20  e cache (from a 
13626 70 72 65 76 69 6f 75 73 0a 20 20 20 20 20 20 2a  previous.      *
13627 2a 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20  * read or write 
13628 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e 20 20 43  transaction).  C
13629 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74  heck to see if t
1362a 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20  he database.    
1362b 20 20 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f    ** has been mo
1362c 64 69 66 69 65 64 2e 20 20 49 66 20 74 68 65 20  dified.  If the 
1362d 64 61 74 61 62 61 73 65 20 68 61 73 20 63 68 61  database has cha
1362e 6e 67 65 64 2c 20 66 6c 75 73 68 20 74 68 65 0a  nged, flush the.
1362f 20 20 20 20 20 20 2a 2a 20 63 61 63 68 65 2e 0a        ** cache..
13630 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
13631 2a 20 44 61 74 61 62 61 73 65 20 63 68 61 6e 67  * Database chang
13632 65 73 20 69 73 20 64 65 74 65 63 74 65 64 20 62  es is detected b
13633 79 20 6c 6f 6f 6b 69 6e 67 20 61 74 20 31 35 20  y looking at 15 
13634 62 79 74 65 73 20 62 65 67 69 6e 6e 69 6e 67 0a  bytes beginning.
13635 20 20 20 20 20 20 2a 2a 20 61 74 20 6f 66 66 73        ** at offs
13636 65 74 20 32 34 20 69 6e 74 6f 20 74 68 65 20 66  et 24 into the f
13637 69 6c 65 2e 20 20 54 68 65 20 66 69 72 73 74 20  ile.  The first 
13638 34 20 6f 66 20 74 68 65 73 65 20 31 36 20 62 79  4 of these 16 by
13639 74 65 73 20 61 72 65 0a 20 20 20 20 20 20 2a 2a  tes are.      **
1363a 20 61 20 33 32 2d 62 69 74 20 63 6f 75 6e 74 65   a 32-bit counte
1363b 72 20 74 68 61 74 20 69 73 20 69 6e 63 72 65 6d  r that is increm
1363c 65 6e 74 65 64 20 77 69 74 68 20 65 61 63 68 20  ented with each 
1363d 63 68 61 6e 67 65 2e 20 20 54 68 65 0a 20 20 20  change.  The.   
1363e 20 20 20 2a 2a 20 6f 74 68 65 72 20 62 79 74 65     ** other byte
1363f 73 20 63 68 61 6e 67 65 20 72 61 6e 64 6f 6d 6c  s change randoml
13640 79 20 77 69 74 68 20 65 61 63 68 20 66 69 6c 65  y with each file
13641 20 63 68 61 6e 67 65 20 77 68 65 6e 0a 20 20 20   change when.   
13642 20 20 20 2a 2a 20 61 20 63 6f 64 65 63 20 69 73     ** a codec is
13643 20 69 6e 20 75 73 65 2e 0a 20 20 20 20 20 20 2a   in use..      *
13644 2a 20 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 72  * .      ** Ther
13645 65 20 69 73 20 61 20 76 61 6e 69 73 68 69 6e 67  e is a vanishing
13646 6c 79 20 73 6d 61 6c 6c 20 63 68 61 6e 63 65 20  ly small chance 
13647 74 68 61 74 20 61 20 63 68 61 6e 67 65 20 77 69  that a change wi
13648 6c 6c 20 6e 6f 74 20 62 65 20 0a 20 20 20 20 20  ll not be .     
13649 20 2a 2a 20 64 65 74 65 63 74 65 64 2e 20 20 54   ** detected.  T
1364a 68 65 20 63 68 61 6e 63 65 20 6f 66 20 61 6e 20  he chance of an 
1364b 75 6e 64 65 74 65 63 74 65 64 20 63 68 61 6e 67  undetected chang
1364c 65 20 69 73 20 73 6f 20 73 6d 61 6c 6c 20 74 68  e is so small th
1364d 61 74 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 63  at.      ** it c
1364e 61 6e 20 62 65 20 6e 65 67 6c 65 63 74 65 64 2e  an be neglected.
1364f 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
13650 63 68 61 72 20 64 62 46 69 6c 65 56 65 72 73 5b  char dbFileVers[
13651 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 64  sizeof(pPager->d
13652 62 46 69 6c 65 56 65 72 73 29 5d 3b 0a 20 20 20  bFileVers)];.   
13653 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 50     sqlite3PagerP
13654 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c  agecount(pPager,
13655 20 30 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20   0);..      if( 
13656 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20  pPager->errCode 
13657 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
13658 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b  pPager->errCode;
13659 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61  .        goto fa
1365a 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  iled;.      }.. 
1365b 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61       assert( pPa
1365c 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64  ger->dbSizeValid
1365d 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50   );.      if( pP
1365e 61 67 65 72 2d 3e 64 62 53 69 7a 65 3e 30 20 29  ager->dbSize>0 )
1365f 7b 0a 20 20 20 20 20 20 20 20 49 4f 54 52 41 43  {.        IOTRAC
13660 45 28 28 22 43 4b 56 45 52 53 20 25 70 20 25 64  E(("CKVERS %p %d
13661 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 73 69 7a  \n", pPager, siz
13662 65 6f 66 28 64 62 46 69 6c 65 56 65 72 73 29 29  eof(dbFileVers))
13663 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  );.        rc = 
13664 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50  sqlite3OsRead(pP
13665 61 67 65 72 2d 3e 66 64 2c 20 26 64 62 46 69 6c  ager->fd, &dbFil
13666 65 56 65 72 73 2c 20 73 69 7a 65 6f 66 28 64 62  eVers, sizeof(db
13667 46 69 6c 65 56 65 72 73 29 2c 20 32 34 29 3b 0a  FileVers), 24);.
13668 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
13669 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1366a 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c         goto fail
1366b 65 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ed;.        }.  
1366c 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1366d 20 20 20 6d 65 6d 73 65 74 28 64 62 46 69 6c 65     memset(dbFile
1366e 56 65 72 73 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Vers, 0, sizeof(
1366f 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20  dbFileVers));.  
13670 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28      }..      if(
13671 20 6d 65 6d 63 6d 70 28 70 50 61 67 65 72 2d 3e   memcmp(pPager->
13672 64 62 46 69 6c 65 56 65 72 73 2c 20 64 62 46 69  dbFileVers, dbFi
13673 6c 65 56 65 72 73 2c 20 73 69 7a 65 6f 66 28 64  leVers, sizeof(d
13674 62 46 69 6c 65 56 65 72 73 29 29 21 3d 30 20 29  bFileVers))!=0 )
13675 7b 0a 20 20 20 20 20 20 20 20 70 61 67 65 72 5f  {.        pager_
13676 72 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20  reset(pPager);. 
13677 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
13678 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
13679 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c  >exclusiveMode |
1367a 7c 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d  | pPager->state=
1367b 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b  =PAGER_SHARED );
1367c 0a 20 20 7d 0a 0a 20 66 61 69 6c 65 64 3a 0a 20  .  }.. failed:. 
1367d 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1367e 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 70 61 67  OK ){.    /* pag
1367f 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 69 73 20 61  er_unlock() is a
13680 20 6e 6f 2d 6f 70 20 66 6f 72 20 65 78 63 6c 75   no-op for exclu
13681 73 69 76 65 20 6d 6f 64 65 20 61 6e 64 20 69 6e  sive mode and in
13682 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65  -memory database
13683 73 2e 20 2a 2f 0a 20 20 20 20 70 61 67 65 72 5f  s. */.    pager_
13684 75 6e 6c 6f 63 6b 28 70 50 61 67 65 72 29 3b 0a  unlock(pPager);.
13685 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
13686 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  .}../*.** If the
13687 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74   reference count
13688 20 68 61 73 20 72 65 61 63 68 65 64 20 7a 65 72   has reached zer
13689 6f 2c 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 79 20  o, rollback any 
1368a 61 63 74 69 76 65 0a 2a 2a 20 74 72 61 6e 73 61  active.** transa
1368b 63 74 69 6f 6e 20 61 6e 64 20 75 6e 6c 6f 63 6b  ction and unlock
1368c 20 74 68 65 20 70 61 67 65 72 2e 0a 2a 2a 0a 2a   the pager..**.*
1368d 2a 20 45 78 63 65 70 74 2c 20 69 6e 20 6c 6f 63  * Except, in loc
1368e 6b 69 6e 67 5f 6d 6f 64 65 3d 45 58 43 4c 55 53  king_mode=EXCLUS
1368f 49 56 45 20 77 68 65 6e 20 74 68 65 72 65 20 69  IVE when there i
13690 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 69 6e 0a  s nothing to in.
13691 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20  ** the rollback 
13692 6a 6f 75 72 6e 61 6c 2c 20 74 68 65 20 75 6e 6c  journal, the unl
13693 6f 63 6b 20 69 73 20 6e 6f 74 20 70 65 72 66 6f  ock is not perfo
13694 72 6d 65 64 20 61 6e 64 20 74 68 65 72 65 20 69  rmed and there i
13695 73 0a 2a 2a 20 6e 6f 74 68 69 6e 67 20 74 6f 20  s.** nothing to 
13696 72 6f 6c 6c 62 61 63 6b 2c 20 73 6f 20 74 68 69  rollback, so thi
13697 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e  s routine is a n
13698 6f 2d 6f 70 2e 0a 2a 2f 20 0a 73 74 61 74 69 63  o-op..*/ .static
13699 20 76 6f 69 64 20 70 61 67 65 72 55 6e 6c 6f 63   void pagerUnloc
1369a 6b 49 66 55 6e 75 73 65 64 28 50 61 67 65 72 20  kIfUnused(Pager 
1369b 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28 20  *pPager){.  if( 
1369c 28 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65  (sqlite3PcacheRe
1369d 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70  fCount(pPager->p
1369e 50 43 61 63 68 65 29 3d 3d 30 29 0a 20 20 20 26  PCache)==0).   &
1369f 26 20 28 21 70 50 61 67 65 72 2d 3e 65 78 63 6c  & (!pPager->excl
136a0 75 73 69 76 65 4d 6f 64 65 20 7c 7c 20 70 50 61  usiveMode || pPa
136a1 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e  ger->journalOff>
136a2 30 29 20 0a 20 20 29 7b 0a 20 20 20 20 70 61 67  0) .  ){.    pag
136a3 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62  erUnlockAndRollb
136a4 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 7d  ack(pPager);.  }
136a5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72  .}../*.** Acquir
136a6 65 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f  e a reference to
136a7 20 70 61 67 65 20 6e 75 6d 62 65 72 20 70 67 6e   page number pgn
136a8 6f 20 69 6e 20 70 61 67 65 72 20 70 50 61 67 65  o in pager pPage
136a9 72 20 28 61 20 70 61 67 65 0a 2a 2a 20 72 65 66  r (a page.** ref
136aa 65 72 65 6e 63 65 20 68 61 73 20 74 79 70 65 20  erence has type 
136ab 44 62 50 61 67 65 2a 29 2e 20 49 66 20 74 68 65  DbPage*). If the
136ac 20 72 65 71 75 65 73 74 65 64 20 72 65 66 65 72   requested refer
136ad 65 6e 63 65 20 69 73 20 0a 2a 2a 20 73 75 63 63  ence is .** succ
136ae 65 73 73 66 75 6c 6c 79 20 6f 62 74 61 69 6e 65  essfully obtaine
136af 64 2c 20 69 74 20 69 73 20 63 6f 70 69 65 64 20  d, it is copied 
136b0 74 6f 20 2a 70 70 50 61 67 65 20 61 6e 64 20 53  to *ppPage and S
136b1 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65  QLITE_OK returne
136b2 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  d..**.** If the 
136b3 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 69  requested page i
136b4 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65  s already in the
136b5 20 63 61 63 68 65 2c 20 69 74 20 69 73 20 72 65   cache, it is re
136b6 74 75 72 6e 65 64 2e 20 0a 2a 2a 20 4f 74 68 65  turned. .** Othe
136b7 72 77 69 73 65 2c 20 61 20 6e 65 77 20 70 61 67  rwise, a new pag
136b8 65 20 6f 62 6a 65 63 74 20 69 73 20 61 6c 6c 6f  e object is allo
136b9 63 61 74 65 64 20 61 6e 64 20 70 6f 70 75 6c 61  cated and popula
136ba 74 65 64 20 77 69 74 68 20 64 61 74 61 0a 2a 2a  ted with data.**
136bb 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 64   read from the d
136bc 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 6e  atabase file. In
136bd 20 73 6f 6d 65 20 63 61 73 65 73 2c 20 74 68 65   some cases, the
136be 20 70 63 61 63 68 65 20 6d 6f 64 75 6c 65 20 6d   pcache module m
136bf 61 79 0a 2a 2a 20 63 68 6f 6f 73 65 20 6e 6f 74  ay.** choose not
136c0 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e   to allocate a n
136c1 65 77 20 70 61 67 65 20 6f 62 6a 65 63 74 20 61  ew page object a
136c2 6e 64 20 6d 61 79 20 72 65 75 73 65 20 61 6e 20  nd may reuse an 
136c3 65 78 69 73 74 69 6e 67 0a 2a 2a 20 6f 62 6a 65  existing.** obje
136c4 63 74 20 77 69 74 68 20 6e 6f 20 6f 75 74 73 74  ct with no outst
136c5 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63 65  anding reference
136c6 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 78 74  s..**.** The ext
136c7 72 61 20 64 61 74 61 20 61 70 70 65 6e 64 65 64  ra data appended
136c8 20 74 6f 20 61 20 70 61 67 65 20 69 73 20 61 6c   to a page is al
136c9 77 61 79 73 20 69 6e 69 74 69 61 6c 69 7a 65 64  ways initialized
136ca 20 74 6f 20 7a 65 72 6f 73 20 74 68 65 20 0a 2a   to zeros the .*
136cb 2a 20 66 69 72 73 74 20 74 69 6d 65 20 61 20 70  * first time a p
136cc 61 67 65 20 69 73 20 6c 6f 61 64 65 64 20 69 6e  age is loaded in
136cd 74 6f 20 6d 65 6d 6f 72 79 2e 20 49 66 20 74 68  to memory. If th
136ce 65 20 70 61 67 65 20 72 65 71 75 65 73 74 65 64  e page requested
136cf 20 69 73 20 0a 2a 2a 20 61 6c 72 65 61 64 79 20   is .** already 
136d0 69 6e 20 74 68 65 20 63 61 63 68 65 20 77 68 65  in the cache whe
136d1 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
136d2 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20  is called, then 
136d3 74 68 65 20 65 78 74 72 61 0a 2a 2a 20 64 61 74  the extra.** dat
136d4 61 20 69 73 20 6c 65 66 74 20 61 73 20 69 74 20  a is left as it 
136d5 77 61 73 20 77 68 65 6e 20 74 68 65 20 70 61 67  was when the pag
136d6 65 20 6f 62 6a 65 63 74 20 77 61 73 20 6c 61 73  e object was las
136d7 74 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  t used..**.** If
136d8 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6d   the database im
136d9 61 67 65 20 69 73 20 73 6d 61 6c 6c 65 72 20 74  age is smaller t
136da 68 61 6e 20 74 68 65 20 72 65 71 75 65 73 74 65  han the requeste
136db 64 20 70 61 67 65 20 6f 72 20 69 66 20 61 20 0a  d page or if a .
136dc 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75  ** non-zero valu
136dd 65 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74  e is passed as t
136de 68 65 20 6e 6f 43 6f 6e 74 65 6e 74 20 70 61 72  he noContent par
136df 61 6d 65 74 65 72 20 61 6e 64 20 74 68 65 20 0a  ameter and the .
136e0 2a 2a 20 72 65 71 75 65 73 74 65 64 20 70 61 67  ** requested pag
136e1 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79  e is not already
136e2 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63   stored in the c
136e3 61 63 68 65 2c 20 74 68 65 6e 20 6e 6f 20 0a 2a  ache, then no .*
136e4 2a 20 61 63 74 75 61 6c 20 64 69 73 6b 20 72 65  * actual disk re
136e5 61 64 20 6f 63 63 75 72 73 2e 20 49 6e 20 74 68  ad occurs. In th
136e6 69 73 20 63 61 73 65 20 74 68 65 20 6d 65 6d 6f  is case the memo
136e7 72 79 20 69 6d 61 67 65 20 6f 66 20 74 68 65 20  ry image of the 
136e8 0a 2a 2a 20 70 61 67 65 20 69 73 20 69 6e 69 74  .** page is init
136e9 69 61 6c 69 7a 65 64 20 74 6f 20 61 6c 6c 20 7a  ialized to all z
136ea 65 72 6f 73 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20  eros. .**.** If 
136eb 6e 6f 43 6f 6e 74 65 6e 74 20 69 73 20 74 72 75  noContent is tru
136ec 65 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74  e, it means that
136ed 20 77 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 20   we do not care 
136ee 61 62 6f 75 74 20 74 68 65 20 63 6f 6e 74 65 6e  about the conten
136ef 74 73 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61 67  ts.** of the pag
136f0 65 2e 20 54 68 69 73 20 6f 63 63 75 72 73 20 69  e. This occurs i
136f1 6e 20 74 77 6f 20 73 65 70 65 72 61 74 65 20 73  n two seperate s
136f2 63 65 6e 61 72 69 6f 73 3a 0a 2a 2a 0a 2a 2a 20  cenarios:.**.** 
136f3 20 20 61 29 20 57 68 65 6e 20 72 65 61 64 69 6e    a) When readin
136f4 67 20 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65  g a free-list le
136f5 61 66 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65  af page from the
136f6 20 64 61 74 61 62 61 73 65 2c 20 61 6e 64 0a 2a   database, and.*
136f7 2a 0a 2a 2a 20 20 20 62 29 20 57 68 65 6e 20 61  *.**   b) When a
136f8 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20 62 65   savepoint is be
136f9 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20  ing rolled back 
136fa 61 6e 64 20 77 65 20 6e 65 65 64 20 74 6f 20 6c  and we need to l
136fb 6f 61 64 0a 2a 2a 20 20 20 20 20 20 61 20 6e 65  oad.**      a ne
136fc 77 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20  w page into the 
136fd 63 61 63 68 65 20 74 6f 20 70 6f 70 75 6c 61 74  cache to populat
136fe 65 20 77 69 74 68 20 74 68 65 20 64 61 74 61 20  e with the data 
136ff 72 65 61 64 0a 2a 2a 20 20 20 20 20 20 66 72 6f  read.**      fro
13700 6d 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20  m the savepoint 
13701 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49  journal..**.** I
13702 66 20 6e 6f 43 6f 6e 74 65 6e 74 20 69 73 20 74  f noContent is t
13703 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 64 61  rue, then the da
13704 74 61 20 72 65 74 75 72 6e 65 64 20 69 73 20 7a  ta returned is z
13705 65 72 6f 65 64 20 69 6e 73 74 65 61 64 20 6f 66  eroed instead of
13706 0a 2a 2a 20 62 65 69 6e 67 20 72 65 61 64 20 66  .** being read f
13707 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65  rom the database
13708 2e 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20  . Additionally, 
13709 74 68 65 20 62 69 74 73 20 63 6f 72 72 65 73 70  the bits corresp
1370a 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20 70 67 6e  onding.** to pgn
1370b 6f 20 69 6e 20 50 61 67 65 72 2e 70 49 6e 4a 6f  o in Pager.pInJo
1370c 75 72 6e 61 6c 20 28 62 69 74 76 65 63 20 6f 66  urnal (bitvec of
1370d 20 70 61 67 65 73 20 61 6c 72 65 61 64 79 20 77   pages already w
1370e 72 69 74 74 65 6e 20 74 6f 20 74 68 65 0a 2a 2a  ritten to the.**
1370f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 61   journal file) a
13710 6e 64 20 74 68 65 20 50 61 67 65 72 53 61 76 65  nd the PagerSave
13711 70 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69  point.pInSavepoi
13712 6e 74 20 62 69 74 76 65 63 73 20 6f 66 20 61 6e  nt bitvecs of an
13713 79 20 6f 70 65 6e 0a 2a 2a 20 73 61 76 65 70 6f  y open.** savepo
13714 69 6e 74 73 20 61 72 65 20 73 65 74 2e 20 54 68  ints are set. Th
13715 69 73 20 6d 65 61 6e 73 20 69 66 20 74 68 65 20  is means if the 
13716 70 61 67 65 20 69 73 20 6d 61 64 65 20 77 72 69  page is made wri
13717 74 61 62 6c 65 20 61 74 20 61 6e 79 0a 2a 2a 20  table at any.** 
13718 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 66 75 74  point in the fut
13719 75 72 65 2c 20 75 73 69 6e 67 20 61 20 63 61 6c  ure, using a cal
1371a 6c 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65  l to sqlite3Page
1371b 72 57 72 69 74 65 28 29 2c 20 69 74 73 20 63 6f  rWrite(), its co
1371c 6e 74 65 6e 74 73 0a 2a 2a 20 77 69 6c 6c 20 6e  ntents.** will n
1371d 6f 74 20 62 65 20 6a 6f 75 72 6e 61 6c 65 64 2e  ot be journaled.
1371e 20 54 68 69 73 20 73 61 76 65 73 20 49 4f 2e 0a   This saves IO..
1371f 2a 2a 0a 2a 2a 20 54 68 65 20 61 63 71 75 69 73  **.** The acquis
13720 69 74 69 6f 6e 20 6d 69 67 68 74 20 66 61 69 6c  ition might fail
13721 20 66 6f 72 20 73 65 76 65 72 61 6c 20 72 65 61   for several rea
13722 73 6f 6e 73 2e 20 20 49 6e 20 61 6c 6c 20 63 61  sons.  In all ca
13723 73 65 73 2c 0a 2a 2a 20 61 6e 20 61 70 70 72 6f  ses,.** an appro
13724 70 72 69 61 74 65 20 65 72 72 6f 72 20 63 6f 64  priate error cod
13725 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  e is returned an
13726 64 20 2a 70 70 50 61 67 65 20 69 73 20 73 65 74  d *ppPage is set
13727 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20   to NULL..**.** 
13728 53 65 65 20 61 6c 73 6f 20 73 71 6c 69 74 65 33  See also sqlite3
13729 50 61 67 65 72 4c 6f 6f 6b 75 70 28 29 2e 20 20  PagerLookup().  
1372a 42 6f 74 68 20 74 68 69 73 20 72 6f 75 74 69 6e  Both this routin
1372b 65 20 61 6e 64 20 4c 6f 6f 6b 75 70 28 29 20 61  e and Lookup() a
1372c 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 66 69 6e  ttempt.** to fin
1372d 64 20 61 20 70 61 67 65 20 69 6e 20 74 68 65 20  d a page in the 
1372e 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 20  in-memory cache 
1372f 66 69 72 73 74 2e 20 20 49 66 20 74 68 65 20 70  first.  If the p
13730 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61  age is not alrea
13731 64 79 0a 2a 2a 20 69 6e 20 6d 65 6d 6f 72 79 2c  dy.** in memory,
13732 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 67 6f   this routine go
13733 65 73 20 74 6f 20 64 69 73 6b 20 74 6f 20 72 65  es to disk to re
13734 61 64 20 69 74 20 69 6e 20 77 68 65 72 65 61 73  ad it in whereas
13735 20 4c 6f 6f 6b 75 70 28 29 0a 2a 2a 20 6a 75 73   Lookup().** jus
13736 74 20 72 65 74 75 72 6e 73 20 30 2e 20 20 54 68  t returns 0.  Th
13737 69 73 20 72 6f 75 74 69 6e 65 20 61 63 71 75 69  is routine acqui
13738 72 65 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20  res a read-lock 
13739 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 69  the first time i
1373a 74 0a 2a 2a 20 68 61 73 20 74 6f 20 67 6f 20 74  t.** has to go t
1373b 6f 20 64 69 73 6b 2c 20 61 6e 64 20 63 6f 75 6c  o disk, and coul
1373c 64 20 61 6c 73 6f 20 70 6c 61 79 62 61 63 6b 20  d also playback 
1373d 61 6e 20 6f 6c 64 20 6a 6f 75 72 6e 61 6c 20 69  an old journal i
1373e 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a 20  f necessary..** 
1373f 53 69 6e 63 65 20 4c 6f 6f 6b 75 70 28 29 20 6e  Since Lookup() n
13740 65 76 65 72 20 67 6f 65 73 20 74 6f 20 64 69 73  ever goes to dis
13741 6b 2c 20 69 74 20 6e 65 76 65 72 20 68 61 73 20  k, it never has 
13742 74 6f 20 64 65 61 6c 20 77 69 74 68 20 6c 6f 63  to deal with loc
13743 6b 73 0a 2a 2a 20 6f 72 20 6a 6f 75 72 6e 61 6c  ks.** or journal
13744 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54   files..*/.SQLIT
13745 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
13746 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 69 72  lite3PagerAcquir
13747 65 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  e(.  Pager *pPag
13748 65 72 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20  er,      /* The 
13749 70 61 67 65 72 20 6f 70 65 6e 20 6f 6e 20 74 68  pager open on th
1374a 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
1374b 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20  */.  Pgno pgno, 
1374c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65           /* Page
1374d 20 6e 75 6d 62 65 72 20 74 6f 20 66 65 74 63 68   number to fetch
1374e 20 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a 2a 70   */.  DbPage **p
1374f 70 50 61 67 65 2c 20 20 20 20 2f 2a 20 57 72 69  pPage,    /* Wri
13750 74 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  te a pointer to 
13751 74 68 65 20 70 61 67 65 20 68 65 72 65 20 2a 2f  the page here */
13752 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e 74 65 6e 74  .  int noContent
13753 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74         /* Do not
13754 20 62 6f 74 68 65 72 20 72 65 61 64 69 6e 67 20   bother reading 
13755 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 64 69 73  content from dis
13756 6b 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a  k if true */.){.
13757 20 20 69 6e 74 20 72 63 3b 0a 20 20 50 67 48 64    int rc;.  PgHd
13758 72 20 2a 70 50 67 3b 0a 0a 20 20 61 73 73 65 72  r *pPg;..  asser
13759 74 28 20 61 73 73 65 72 74 5f 70 61 67 65 72 5f  t( assert_pager_
1375a 73 74 61 74 65 28 70 50 61 67 65 72 29 20 29 3b  state(pPager) );
1375b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
1375c 72 2d 3e 73 74 61 74 65 3e 50 41 47 45 52 5f 55  r->state>PAGER_U
1375d 4e 4c 4f 43 4b 20 29 3b 0a 0a 20 20 69 66 28 20  NLOCK );..  if( 
1375e 70 67 6e 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 72  pgno==0 ){.    r
1375f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
13760 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a  RUPT_BKPT;.  }..
13761 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65    /* If the page
13762 72 20 69 73 20 69 6e 20 74 68 65 20 65 72 72 6f  r is in the erro
13763 72 20 73 74 61 74 65 2c 20 72 65 74 75 72 6e 20  r state, return 
13764 61 6e 20 65 72 72 6f 72 20 69 6d 6d 65 64 69 61  an error immedia
13765 74 65 6c 79 2e 20 0a 20 20 2a 2a 20 4f 74 68 65  tely. .  ** Othe
13766 72 77 69 73 65 2c 20 72 65 71 75 65 73 74 20 74  rwise, request t
13767 68 65 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65  he page from the
13768 20 50 43 61 63 68 65 20 6c 61 79 65 72 2e 20 2a   PCache layer. *
13769 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  /.  if( pPager->
1376a 65 72 72 43 6f 64 65 21 3d 53 51 4c 49 54 45 5f  errCode!=SQLITE_
1376b 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 65 72  OK && pPager->er
1376c 72 43 6f 64 65 21 3d 53 51 4c 49 54 45 5f 46 55  rCode!=SQLITE_FU
1376d 4c 4c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70  LL ){.    rc = p
1376e 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a  Pager->errCode;.
1376f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
13770 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 46  = sqlite3PcacheF
13771 65 74 63 68 28 70 50 61 67 65 72 2d 3e 70 50 43  etch(pPager->pPC
13772 61 63 68 65 2c 20 70 67 6e 6f 2c 20 31 2c 20 70  ache, pgno, 1, p
13773 70 50 61 67 65 29 3b 0a 20 20 7d 0a 0a 20 20 69  pPage);.  }..  i
13774 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
13775 20 29 7b 0a 20 20 20 20 2f 2a 20 45 69 74 68 65   ){.    /* Eithe
13776 72 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71  r the call to sq
13777 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68  lite3PcacheFetch
13778 28 29 20 72 65 74 75 72 6e 65 64 20 61 6e 20 65  () returned an e
13779 72 72 6f 72 20 6f 72 20 74 68 65 0a 20 20 20 20  rror or the.    
1377a 2a 2a 20 70 61 67 65 72 20 77 61 73 20 61 6c 72  ** pager was alr
1377b 65 61 64 79 20 69 6e 20 74 68 65 20 65 72 72 6f  eady in the erro
1377c 72 2d 73 74 61 74 65 20 77 68 65 6e 20 74 68 69  r-state when thi
1377d 73 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 63  s function was c
1377e 61 6c 6c 65 64 2e 0a 20 20 20 20 2a 2a 20 53 65  alled..    ** Se
1377f 74 20 70 50 67 20 74 6f 20 30 20 61 6e 64 20 6a  t pPg to 0 and j
13780 75 6d 70 20 74 6f 20 74 68 65 20 65 78 63 65 70  ump to the excep
13781 74 69 6f 6e 20 68 61 6e 64 6c 65 72 2e 20 20 2a  tion handler.  *
13782 2f 0a 20 20 20 20 70 50 67 20 3d 20 30 3b 0a 20  /.    pPg = 0;. 
13783 20 20 20 67 6f 74 6f 20 70 61 67 65 72 5f 61 63     goto pager_ac
13784 71 75 69 72 65 5f 65 72 72 3b 0a 20 20 7d 0a 20  quire_err;.  }. 
13785 20 61 73 73 65 72 74 28 20 28 2a 70 70 50 61 67   assert( (*ppPag
13786 65 29 2d 3e 70 67 6e 6f 3d 3d 70 67 6e 6f 20 29  e)->pgno==pgno )
13787 3b 0a 20 20 61 73 73 65 72 74 28 20 28 2a 70 70  ;.  assert( (*pp
13788 50 61 67 65 29 2d 3e 70 50 61 67 65 72 3d 3d 70  Page)->pPager==p
13789 50 61 67 65 72 20 7c 7c 20 28 2a 70 70 50 61 67  Pager || (*ppPag
1378a 65 29 2d 3e 70 50 61 67 65 72 3d 3d 30 20 29 3b  e)->pPager==0 );
1378b 0a 0a 20 20 69 66 28 20 28 2a 70 70 50 61 67 65  ..  if( (*ppPage
1378c 29 2d 3e 70 50 61 67 65 72 20 29 7b 0a 20 20 20  )->pPager ){.   
1378d 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65   /* In this case
1378e 20 74 68 65 20 70 63 61 63 68 65 20 61 6c 72 65   the pcache alre
1378f 61 64 79 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20  ady contains an 
13790 69 6e 69 74 69 61 6c 69 7a 65 64 20 63 6f 70 79  initialized copy
13791 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 20 70   of.    ** the p
13792 61 67 65 2e 20 52 65 74 75 72 6e 20 77 69 74 68  age. Return with
13793 6f 75 74 20 66 75 72 74 68 65 72 20 61 64 6f 2e  out further ado.
13794 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28    */.    assert(
13795 20 70 67 6e 6f 3c 3d 50 41 47 45 52 5f 4d 41 58   pgno<=PAGER_MAX
13796 5f 50 47 4e 4f 20 26 26 20 70 67 6e 6f 21 3d 50  _PGNO && pgno!=P
13797 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61  AGER_MJ_PGNO(pPa
13798 67 65 72 29 20 29 3b 0a 20 20 20 20 50 41 47 45  ger) );.    PAGE
13799 52 5f 49 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e  R_INCR(pPager->n
1379a 48 69 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  Hit);.    return
1379b 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 7d   SQLITE_OK;..  }
1379c 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65  else{.    /* The
1379d 20 70 61 67 65 72 20 63 61 63 68 65 20 68 61 73   pager cache has
1379e 20 63 72 65 61 74 65 64 20 61 20 6e 65 77 20 70   created a new p
1379f 61 67 65 2e 20 49 74 73 20 63 6f 6e 74 65 6e 74  age. Its content
137a0 20 6e 65 65 64 73 20 74 6f 20 0a 20 20 20 20 2a   needs to .    *
137a1 2a 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64  * be initialized
137a2 2e 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 4d  .  */.    int nM
137a3 61 78 3b 0a 0a 20 20 20 20 50 41 47 45 52 5f 49  ax;..    PAGER_I
137a4 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e 4d 69 73  NCR(pPager->nMis
137a5 73 29 3b 0a 20 20 20 20 70 50 67 20 3d 20 2a 70  s);.    pPg = *p
137a6 70 50 61 67 65 3b 0a 20 20 20 20 70 50 67 2d 3e  pPage;.    pPg->
137a7 70 50 61 67 65 72 20 3d 20 70 50 61 67 65 72 3b  pPager = pPager;
137a8 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 78  ..    /* The max
137a9 69 6d 75 6d 20 70 61 67 65 20 6e 75 6d 62 65 72  imum page number
137aa 20 69 73 20 32 5e 33 31 2e 20 52 65 74 75 72 6e   is 2^31. Return
137ab 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20   SQLITE_CORRUPT 
137ac 69 66 20 61 20 70 61 67 65 0a 20 20 20 20 2a 2a  if a page.    **
137ad 20 6e 75 6d 62 65 72 20 67 72 65 61 74 65 72 20   number greater 
137ae 74 68 61 6e 20 74 68 69 73 2c 20 6f 72 20 74 68  than this, or th
137af 65 20 75 6e 75 73 65 64 20 6c 6f 63 6b 69 6e 67  e unused locking
137b0 2d 70 61 67 65 2c 20 69 73 20 72 65 71 75 65 73  -page, is reques
137b1 74 65 64 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  ted. */.    if( 
137b2 70 67 6e 6f 3e 50 41 47 45 52 5f 4d 41 58 5f 50  pgno>PAGER_MAX_P
137b3 47 4e 4f 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47  GNO || pgno==PAG
137b4 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65  ER_MJ_PGNO(pPage
137b5 72 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  r) ){.      rc =
137b6 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
137b7 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f  BKPT;.      goto
137b8 20 70 61 67 65 72 5f 61 63 71 75 69 72 65 5f 65   pager_acquire_e
137b9 72 72 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72  rr;.    }..    r
137ba 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
137bb 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72  Pagecount(pPager
137bc 2c 20 26 6e 4d 61 78 29 3b 0a 20 20 20 20 69 66  , &nMax);.    if
137bd 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
137be 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61  ){.      goto pa
137bf 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72 72 3b  ger_acquire_err;
137c0 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20  .    }..    if( 
137c1 6e 4d 61 78 3c 28 69 6e 74 29 70 67 6e 6f 20 7c  nMax<(int)pgno |
137c2 7c 20 4d 45 4d 44 42 20 7c 7c 20 6e 6f 43 6f 6e  | MEMDB || noCon
137c3 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 69 66  tent ){.      if
137c4 28 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 6d  ( pgno>pPager->m
137c5 78 50 67 6e 6f 20 29 7b 0a 09 72 63 20 3d 20 53  xPgno ){..rc = S
137c6 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 09 67 6f 74  QLITE_FULL;..got
137c7 6f 20 70 61 67 65 72 5f 61 63 71 75 69 72 65 5f  o pager_acquire_
137c8 65 72 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  err;.      }.   
137c9 20 20 20 69 66 28 20 6e 6f 43 6f 6e 74 65 6e 74     if( noContent
137ca 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 46   ){.        /* F
137cb 61 69 6c 75 72 65 20 74 6f 20 73 65 74 20 74 68  ailure to set th
137cc 65 20 62 69 74 73 20 69 6e 20 74 68 65 20 49 6e  e bits in the In
137cd 4a 6f 75 72 6e 61 6c 20 62 69 74 2d 76 65 63 74  Journal bit-vect
137ce 6f 72 73 20 69 73 20 62 65 6e 69 67 6e 2e 0a 20  ors is benign.. 
137cf 20 20 20 20 20 20 20 2a 2a 20 49 74 20 6d 65 72         ** It mer
137d0 65 6c 79 20 6d 65 61 6e 73 20 74 68 61 74 20 77  ely means that w
137d1 65 20 6d 69 67 68 74 20 64 6f 20 73 6f 6d 65 20  e might do some 
137d2 65 78 74 72 61 20 77 6f 72 6b 20 74 6f 20 6a 6f  extra work to jo
137d3 75 72 6e 61 6c 20 61 20 0a 20 20 20 20 20 20 20  urnal a .       
137d4 20 2a 2a 20 70 61 67 65 20 74 68 61 74 20 64 6f   ** page that do
137d5 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62  es not need to b
137d6 65 20 6a 6f 75 72 6e 61 6c 65 64 2e 20 20 4e 65  e journaled.  Ne
137d7 76 65 72 74 68 65 6c 65 73 73 2c 20 62 65 20 73  vertheless, be s
137d8 75 72 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  ure .        ** 
137d9 74 6f 20 74 65 73 74 20 74 68 65 20 63 61 73 65  to test the case
137da 20 77 68 65 72 65 20 61 20 6d 61 6c 6c 6f 63 20   where a malloc 
137db 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69  error occurs whi
137dc 6c 65 20 74 72 79 69 6e 67 20 74 6f 20 73 65 74  le trying to set
137dd 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20 62   .        ** a b
137de 69 74 20 69 6e 20 61 20 62 69 74 20 76 65 63 74  it in a bit vect
137df 6f 72 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  or..        */. 
137e0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 65         sqlite3Be
137e1 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  ginBenignMalloc(
137e2 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
137e3 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 4f  gno<=pPager->dbO
137e4 72 69 67 53 69 7a 65 20 29 7b 0a 20 20 20 20 20  rigSize ){.     
137e5 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 72       TESTONLY( r
137e6 63 20 3d 20 29 20 73 71 6c 69 74 65 33 42 69 74  c = ) sqlite3Bit
137e7 76 65 63 53 65 74 28 70 50 61 67 65 72 2d 3e 70  vecSet(pPager->p
137e8 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 67 6e 6f 29  InJournal, pgno)
137e9 3b 0a 20 20 20 20 20 20 20 20 20 20 74 65 73 74  ;.          test
137ea 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 54 45  case( rc==SQLITE
137eb 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20  _NOMEM );.      
137ec 20 20 7d 0a 20 20 20 20 20 20 20 20 54 45 53 54    }.        TEST
137ed 4f 4e 4c 59 28 20 72 63 20 3d 20 29 20 61 64 64  ONLY( rc = ) add
137ee 54 6f 53 61 76 65 70 6f 69 6e 74 42 69 74 76 65  ToSavepointBitve
137ef 63 73 28 70 50 61 67 65 72 2c 20 70 67 6e 6f 29  cs(pPager, pgno)
137f0 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61  ;.        testca
137f1 73 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e  se( rc==SQLITE_N
137f2 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20  OMEM );.        
137f3 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e  sqlite3EndBenign
137f4 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20  Malloc();.      
137f5 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6d  }else{.        m
137f6 65 6d 73 65 74 28 70 50 67 2d 3e 70 44 61 74 61  emset(pPg->pData
137f7 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e 70 61 67  , 0, pPager->pag
137f8 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d 0a  eSize);.      }.
137f9 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22        IOTRACE(("
137fa 5a 45 52 4f 20 25 70 20 25 64 5c 6e 22 2c 20 70  ZERO %p %d\n", p
137fb 50 61 67 65 72 2c 20 70 67 6e 6f 29 29 3b 0a 20  Pager, pgno));. 
137fc 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
137fd 61 73 73 65 72 74 28 20 70 50 67 2d 3e 70 50 61  assert( pPg->pPa
137fe 67 65 72 3d 3d 70 50 61 67 65 72 20 29 3b 0a 20  ger==pPager );. 
137ff 20 20 20 20 20 72 63 20 3d 20 72 65 61 64 44 62       rc = readDb
13800 50 61 67 65 28 70 50 67 29 3b 0a 20 20 20 20 20  Page(pPg);.     
13801 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
13802 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f  OK ){.        go
13803 74 6f 20 70 61 67 65 72 5f 61 63 71 75 69 72 65  to pager_acquire
13804 5f 65 72 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20  _err;.      }.  
13805 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54    }.#ifdef SQLIT
13806 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20  E_CHECK_PAGES.  
13807 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20    pPg->pageHash 
13808 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68  = pager_pagehash
13809 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20  (pPg);.#endif.  
1380a 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  }..  return SQLI
1380b 54 45 5f 4f 4b 3b 0a 0a 70 61 67 65 72 5f 61 63  TE_OK;..pager_ac
1380c 71 75 69 72 65 5f 65 72 72 3a 0a 20 20 61 73 73  quire_err:.  ass
1380d 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f  ert( rc!=SQLITE_
1380e 4f 4b 20 29 3b 0a 20 20 69 66 28 20 70 50 67 20  OK );.  if( pPg 
1380f 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63  ){.    sqlite3Pc
13810 61 63 68 65 44 72 6f 70 28 70 50 67 29 3b 0a 20  acheDrop(pPg);. 
13811 20 7d 0a 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b   }.  pagerUnlock
13812 49 66 55 6e 75 73 65 64 28 70 50 61 67 65 72 29  IfUnused(pPager)
13813 3b 0a 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 30  ;..  *ppPage = 0
13814 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
13815 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20  ../*.** Acquire 
13816 61 20 70 61 67 65 20 69 66 20 69 74 20 69 73 20  a page if it is 
13817 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 69  already in the i
13818 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 2e 20  n-memory cache. 
13819 20 44 6f 0a 2a 2a 20 6e 6f 74 20 72 65 61 64 20   Do.** not read 
1381a 74 68 65 20 70 61 67 65 20 66 72 6f 6d 20 64 69  the page from di
1381b 73 6b 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f  sk.  Return a po
1381c 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61 67  inter to the pag
1381d 65 2c 0a 2a 2a 20 6f 72 20 30 20 69 66 20 74 68  e,.** or 0 if th
1381e 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e  e page is not in
1381f 20 63 61 63 68 65 2e 20 41 6c 73 6f 2c 20 72 65   cache. Also, re
13820 74 75 72 6e 20 30 20 69 66 20 74 68 65 20 0a 2a  turn 0 if the .*
13821 2a 20 70 61 67 65 72 20 69 73 20 69 6e 20 50 41  * pager is in PA
13822 47 45 52 5f 55 4e 4c 4f 43 4b 20 73 74 61 74 65  GER_UNLOCK state
13823 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   when this funct
13824 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a  ion is called,.*
13825 2a 20 6f 72 20 69 66 20 74 68 65 20 70 61 67 65  * or if the page
13826 72 20 69 73 20 69 6e 20 61 6e 20 65 72 72 6f 72  r is in an error
13827 20 73 74 61 74 65 20 6f 74 68 65 72 20 74 68 61   state other tha
13828 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 2e 0a 2a  n SQLITE_FULL..*
13829 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 73 71  *.** See also sq
1382a 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29 2e  lite3PagerGet().
1382b 20 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65    The difference
1382c 20 62 65 74 77 65 65 6e 20 74 68 69 73 20 72 6f   between this ro
1382d 75 74 69 6e 65 0a 2a 2a 20 61 6e 64 20 73 71 6c  utine.** and sql
1382e 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20 69  ite3PagerGet() i
1382f 73 20 74 68 61 74 20 5f 67 65 74 28 29 20 77 69  s that _get() wi
13830 6c 6c 20 67 6f 20 74 6f 20 74 68 65 20 64 69 73  ll go to the dis
13831 6b 20 61 6e 64 20 72 65 61 64 0a 2a 2a 20 69 6e  k and read.** in
13832 20 74 68 65 20 70 61 67 65 20 69 66 20 74 68 65   the page if the
13833 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72   page is not alr
13834 65 61 64 79 20 69 6e 20 63 61 63 68 65 2e 20 20  eady in cache.  
13835 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  This routine.** 
13836 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20 69 66 20  returns NULL if 
13837 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20  the page is not 
13838 69 6e 20 63 61 63 68 65 20 6f 72 20 69 66 20 61  in cache or if a
13839 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 20   disk I/O error 
1383a 0a 2a 2a 20 68 61 73 20 65 76 65 72 20 68 61 70  .** has ever hap
1383b 70 65 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  pened..*/.SQLITE
1383c 5f 50 52 49 56 41 54 45 20 44 62 50 61 67 65 20  _PRIVATE DbPage 
1383d 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f  *sqlite3PagerLoo
1383e 6b 75 70 28 50 61 67 65 72 20 2a 70 50 61 67 65  kup(Pager *pPage
1383f 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20  r, Pgno pgno){. 
13840 20 50 67 48 64 72 20 2a 70 50 67 20 3d 20 30 3b   PgHdr *pPg = 0;
13841 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
13842 72 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  r!=0 );.  assert
13843 28 20 70 67 6e 6f 21 3d 30 20 29 3b 0a 20 20 61  ( pgno!=0 );.  a
13844 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70  ssert( pPager->p
13845 50 43 61 63 68 65 21 3d 30 20 29 3b 0a 20 20 61  PCache!=0 );.  a
13846 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
13847 74 61 74 65 20 3e 20 50 41 47 45 52 5f 55 4e 4c  tate > PAGER_UNL
13848 4f 43 4b 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  OCK );.  sqlite3
13849 50 63 61 63 68 65 46 65 74 63 68 28 70 50 61 67  PcacheFetch(pPag
1384a 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 67 6e  er->pPCache, pgn
1384b 6f 2c 20 30 2c 20 26 70 50 67 29 3b 0a 20 20 72  o, 0, &pPg);.  r
1384c 65 74 75 72 6e 20 70 50 67 3b 0a 7d 0a 0a 2f 2a  eturn pPg;.}../*
1384d 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 70 61  .** Release a pa
1384e 67 65 20 72 65 66 65 72 65 6e 63 65 2e 0a 2a 2a  ge reference..**
1384f 0a 2a 2a 20 49 66 20 74 68 65 20 6e 75 6d 62 65  .** If the numbe
13850 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20  r of references 
13851 74 6f 20 74 68 65 20 70 61 67 65 20 64 72 6f 70  to the page drop
13852 20 74 6f 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74   to zero, then t
13853 68 65 0a 2a 2a 20 70 61 67 65 20 69 73 20 61 64  he.** page is ad
13854 64 65 64 20 74 6f 20 74 68 65 20 4c 52 55 20 6c  ded to the LRU l
13855 69 73 74 2e 20 20 57 68 65 6e 20 61 6c 6c 20 72  ist.  When all r
13856 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61 6c 6c  eferences to all
13857 20 70 61 67 65 73 0a 2a 2a 20 61 72 65 20 72 65   pages.** are re
13858 6c 65 61 73 65 64 2c 20 61 20 72 6f 6c 6c 62 61  leased, a rollba
13859 63 6b 20 6f 63 63 75 72 73 20 61 6e 64 20 74 68  ck occurs and th
1385a 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61  e lock on the da
1385b 74 61 62 61 73 65 20 69 73 0a 2a 2a 20 72 65 6d  tabase is.** rem
1385c 6f 76 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  oved..*/.SQLITE_
1385d 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1385e 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 44  ite3PagerUnref(D
1385f 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 69  bPage *pPg){.  i
13860 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 50 61  f( pPg ){.    Pa
13861 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50  ger *pPager = pP
13862 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 20 20 73  g->pPager;.    s
13863 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c 65  qlite3PcacheRele
13864 61 73 65 28 70 50 67 29 3b 0a 20 20 20 20 70 61  ase(pPg);.    pa
13865 67 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 65  gerUnlockIfUnuse
13866 64 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 7d  d(pPager);.  }.}
13867 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6d  ../*.** If the m
13868 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ain journal file
13869 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65   has already bee
1386a 6e 20 6f 70 65 6e 65 64 2c 20 65 6e 73 75 72 65  n opened, ensure
1386b 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 73 75 62   that the.** sub
1386c 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73  -journal file is
1386d 20 6f 70 65 6e 20 74 6f 6f 2e 20 49 66 20 74 68   open too. If th
1386e 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 69  e main journal i
1386f 73 20 6e 6f 74 20 6f 70 65 6e 2c 0a 2a 2a 20 74  s not open,.** t
13870 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
13871 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 53  a no-op..**.** S
13872 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
13873 72 6e 65 64 20 69 66 20 65 76 65 72 79 74 68 69  rned if everythi
13874 6e 67 20 67 6f 65 73 20 61 63 63 6f 72 64 69 6e  ng goes accordin
13875 67 20 74 6f 20 70 6c 61 6e 2e 20 0a 2a 2a 20 41  g to plan. .** A
13876 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 58  n SQLITE_IOERR_X
13877 58 58 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  XX error code is
13878 20 72 65 74 75 72 6e 65 64 20 69 66 20 61 20 63   returned if a c
13879 61 6c 6c 20 74 6f 20 0a 2a 2a 20 73 71 6c 69 74  all to .** sqlit
1387a 65 33 4f 73 4f 70 65 6e 28 29 20 66 61 69 6c 73  e3OsOpen() fails
1387b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
1387c 6f 70 65 6e 53 75 62 4a 6f 75 72 6e 61 6c 28 50  openSubJournal(P
1387d 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
1387e 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
1387f 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 73 4f 70 65  _OK;.  if( isOpe
13880 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 26  n(pPager->jfd) &
13881 26 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72  & !isOpen(pPager
13882 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 20 69  ->sjfd) ){.    i
13883 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  f( pPager->journ
13884 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f  alMode==PAGER_JO
13885 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59  URNALMODE_MEMORY
13886 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 75 62 6a   || pPager->subj
13887 49 6e 4d 65 6d 6f 72 79 20 29 7b 0a 20 20 20 20  InMemory ){.    
13888 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72    sqlite3MemJour
13889 6e 61 6c 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e  nalOpen(pPager->
1388a 73 6a 66 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65  sjfd);.    }else
1388b 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67  {.      rc = pag
1388c 65 72 4f 70 65 6e 74 65 6d 70 28 70 50 61 67 65  erOpentemp(pPage
1388d 72 2c 20 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c  r, pPager->sjfd,
1388e 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42   SQLITE_OPEN_SUB
1388f 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20 20 20 7d 0a  JOURNAL);.    }.
13890 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
13891 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
13892 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
13893 64 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f  d at the start o
13894 66 20 65 76 65 72 79 20 77 72 69 74 65 20 74 72  f every write tr
13895 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68  ansaction..** Th
13896 65 72 65 20 6d 75 73 74 20 61 6c 72 65 61 64 79  ere must already
13897 20 62 65 20 61 20 52 45 53 45 52 56 45 44 20 6f   be a RESERVED o
13898 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  r EXCLUSIVE lock
13899 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
1389a 20 0a 2a 2a 20 66 69 6c 65 20 77 68 65 6e 20 74   .** file when t
1389b 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
1389c 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 70 65  alled..**.** Ope
1389d 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  n the journal fi
1389e 6c 65 20 66 6f 72 20 70 61 67 65 72 20 70 50 61  le for pager pPa
1389f 67 65 72 20 61 6e 64 20 77 72 69 74 65 20 61 20  ger and write a 
138a0 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 0a 2a  journal header.*
138a1 2a 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f  * to the start o
138a2 66 20 69 74 2e 20 49 66 20 74 68 65 72 65 20 61  f it. If there a
138a3 72 65 20 61 63 74 69 76 65 20 73 61 76 65 70 6f  re active savepo
138a4 69 6e 74 73 2c 20 6f 70 65 6e 20 74 68 65 20 73  ints, open the s
138a5 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 61 73  ub-journal.** as
138a6 20 77 65 6c 6c 2e 20 54 68 69 73 20 66 75 6e 63   well. This func
138a7 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65  tion is only use
138a8 64 20 77 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e  d when the journ
138a9 61 6c 20 66 69 6c 65 20 69 73 20 62 65 69 6e 67  al file is being
138aa 20 0a 2a 2a 20 6f 70 65 6e 65 64 20 74 6f 20 77   .** opened to w
138ab 72 69 74 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20  rite a rollback 
138ac 6c 6f 67 20 66 6f 72 20 61 20 74 72 61 6e 73 61  log for a transa
138ad 63 74 69 6f 6e 2e 20 49 74 20 69 73 20 6e 6f 74  ction. It is not
138ae 20 75 73 65 64 20 0a 2a 2a 20 77 68 65 6e 20 6f   used .** when o
138af 70 65 6e 69 6e 67 20 61 20 68 6f 74 20 6a 6f 75  pening a hot jou
138b0 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 72 6f 6c  rnal file to rol
138b1 6c 20 69 74 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a  l it back..**.**
138b2 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   If the journal 
138b3 66 69 6c 65 20 69 73 20 61 6c 72 65 61 64 79 20  file is already 
138b4 6f 70 65 6e 20 28 61 73 20 69 74 20 6d 61 79 20  open (as it may 
138b5 62 65 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20  be in exclusive 
138b6 6d 6f 64 65 29 2c 0a 2a 2a 20 74 68 65 6e 20 74  mode),.** then t
138b7 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6a 75 73  his function jus
138b8 74 20 77 72 69 74 65 73 20 61 20 6a 6f 75 72 6e  t writes a journ
138b9 61 6c 20 68 65 61 64 65 72 20 74 6f 20 74 68 65  al header to the
138ba 20 73 74 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a   start of the.**
138bb 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 20 66 69   already open fi
138bc 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68  le. .**.** Wheth
138bd 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 6a 6f  er or not the jo
138be 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f 70  urnal file is op
138bf 65 6e 65 64 20 62 79 20 74 68 69 73 20 66 75 6e  ened by this fun
138c0 63 74 69 6f 6e 2c 20 74 68 65 0a 2a 2a 20 50 61  ction, the.** Pa
138c1 67 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 62  ger.pInJournal b
138c2 69 74 76 65 63 20 73 74 72 75 63 74 75 72 65 20  itvec structure 
138c3 69 73 20 61 6c 6c 6f 63 61 74 65 64 2e 0a 2a 2a  is allocated..**
138c4 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54  .** Return SQLIT
138c5 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68 69  E_OK if everythi
138c6 6e 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c  ng is successful
138c7 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74  . Otherwise, ret
138c8 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e  urn .** SQLITE_N
138c9 4f 4d 45 4d 20 69 66 20 74 68 65 20 61 74 74 65  OMEM if the atte
138ca 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  mpt to allocate 
138cb 50 61 67 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c  Pager.pInJournal
138cc 20 66 61 69 6c 73 2c 20 6f 72 20 0a 2a 2a 20 61   fails, or .** a
138cd 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20  n IO error code 
138ce 69 66 20 6f 70 65 6e 69 6e 67 20 6f 72 20 77 72  if opening or wr
138cf 69 74 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61  iting the journa
138d0 6c 20 66 69 6c 65 20 66 61 69 6c 73 2e 0a 2a 2f  l file fails..*/
138d1 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65  .static int page
138d2 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 50  r_open_journal(P
138d3 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
138d4 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
138d5 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20  _OK;            
138d6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
138d7 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
138d8 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 20 63 6f  sqlite3_vfs * co
138d9 6e 73 74 20 70 56 66 73 20 3d 20 70 50 61 67 65  nst pVfs = pPage
138da 72 2d 3e 70 56 66 73 3b 20 20 20 2f 2a 20 4c 6f  r->pVfs;   /* Lo
138db 63 61 6c 20 63 61 63 68 65 20 6f 66 20 76 66 73  cal cache of vfs
138dc 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 0a 20 20 61   pointer */..  a
138dd 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
138de 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45  tate>=PAGER_RESE
138df 52 56 45 44 20 29 3b 0a 20 20 61 73 73 65 72 74  RVED );.  assert
138e0 28 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75  ( pPager->useJou
138e1 72 6e 61 6c 20 29 3b 0a 20 20 61 73 73 65 72 74  rnal );.  assert
138e2 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  ( pPager->journa
138e3 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55  lMode!=PAGER_JOU
138e4 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 3b 0a  RNALMODE_OFF );.
138e5 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
138e6 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20  ->pInJournal==0 
138e7 29 3b 0a 20 20 0a 20 20 2f 2a 20 49 66 20 61 6c  );.  .  /* If al
138e8 72 65 61 64 79 20 69 6e 20 74 68 65 20 65 72 72  ready in the err
138e9 6f 72 20 73 74 61 74 65 2c 20 74 68 69 73 20 66  or state, this f
138ea 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d  unction is a no-
138eb 6f 70 2e 20 20 42 75 74 20 6f 6e 0a 20 20 2a 2a  op.  But on.  **
138ec 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c   the other hand,
138ed 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
138ee 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 20 69 66   never called if
138ef 20 77 65 20 61 72 65 20 61 6c 72 65 61 64 79 20   we are already 
138f0 69 6e 0a 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72  in.  ** an error
138f1 20 73 74 61 74 65 2e 20 2a 2f 0a 20 20 69 66 28   state. */.  if(
138f2 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d 3e 65   NEVER(pPager->e
138f3 72 72 43 6f 64 65 29 20 29 20 72 65 74 75 72 6e  rrCode) ) return
138f4 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
138f5 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 3a 20 49 73  ;..  /* TODO: Is
138f6 20 69 74 20 72 65 61 6c 6c 79 20 70 6f 73 73 69   it really possi
138f7 62 6c 65 20 74 6f 20 67 65 74 20 68 65 72 65 20  ble to get here 
138f8 77 69 74 68 20 64 62 53 69 7a 65 56 61 6c 69 64  with dbSizeValid
138f9 3d 3d 30 3f 20 49 66 20 6e 6f 74 2c 0a 20 20 2a  ==0? If not,.  *
138fa 2a 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 50 61  * the call to Pa
138fb 67 65 72 50 61 67 65 63 6f 75 6e 74 28 29 20 63  gerPagecount() c
138fc 61 6e 20 62 65 20 72 65 6d 6f 76 65 64 2e 0a 20  an be removed.. 
138fd 20 2a 2f 0a 20 20 74 65 73 74 63 61 73 65 28 20   */.  testcase( 
138fe 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61  pPager->dbSizeVa
138ff 6c 69 64 3d 3d 30 20 29 3b 0a 20 20 73 71 6c 69  lid==0 );.  sqli
13900 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e  te3PagerPagecoun
13901 74 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 0a 20  t(pPager, 0);.. 
13902 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72   pPager->pInJour
13903 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 42 69 74  nal = sqlite3Bit
13904 76 65 63 43 72 65 61 74 65 28 70 50 61 67 65 72  vecCreate(pPager
13905 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 20 69 66 28  ->dbSize);.  if(
13906 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72   pPager->pInJour
13907 6e 61 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  nal==0 ){.    re
13908 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
13909 4d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65  M;.  }..  /* Ope
1390a 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  n the journal fi
1390b 6c 65 20 69 66 20 69 74 20 69 73 20 6e 6f 74 20  le if it is not 
1390c 61 6c 72 65 61 64 79 20 6f 70 65 6e 2e 20 2a 2f  already open. */
1390d 0a 20 20 69 66 28 20 21 69 73 4f 70 65 6e 28 70  .  if( !isOpen(p
1390e 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20  Pager->jfd) ){. 
1390f 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a     if( pPager->j
13910 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45  ournalMode==PAGE
13911 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45  R_JOURNALMODE_ME
13912 4d 4f 52 59 20 29 7b 0a 20 20 20 20 20 20 73 71  MORY ){.      sq
13913 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f  lite3MemJournalO
13914 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29  pen(pPager->jfd)
13915 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
13916 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 66 6c 61     const int fla
13917 67 73 20 3d 20 20 20 20 20 20 20 20 20 20 20 20  gs =            
13918 20 20 20 20 20 20 20 2f 2a 20 56 46 53 20 66 6c         /* VFS fl
13919 61 67 73 20 74 6f 20 6f 70 65 6e 20 6a 6f 75 72  ags to open jour
1391a 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20  nal file */.    
1391b 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f      SQLITE_OPEN_
1391c 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45  READWRITE|SQLITE
1391d 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c 0a 20 20  _OPEN_CREATE|.  
1391e 20 20 20 20 20 20 28 70 50 61 67 65 72 2d 3e 74        (pPager->t
1391f 65 6d 70 46 69 6c 65 20 3f 20 0a 20 20 20 20 20  empFile ? .     
13920 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4f 50 45       (SQLITE_OPE
13921 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 7c  N_DELETEONCLOSE|
13922 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50  SQLITE_OPEN_TEMP
13923 5f 4a 4f 55 52 4e 41 4c 29 3a 0a 20 20 20 20 20  _JOURNAL):.     
13924 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4f 50 45       (SQLITE_OPE
13925 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 0a  N_MAIN_JOURNAL).
13926 20 20 20 20 20 20 20 20 29 3b 0a 23 69 66 64 65          );.#ifde
13927 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
13928 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 20  ATOMIC_WRITE.   
13929 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4a     rc = sqlite3J
1392a 6f 75 72 6e 61 6c 4f 70 65 6e 28 0a 20 20 20 20  ournalOpen(.    
1392b 20 20 20 20 20 20 70 56 66 73 2c 20 70 50 61 67        pVfs, pPag
1392c 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50  er->zJournal, pP
1392d 61 67 65 72 2d 3e 6a 66 64 2c 20 66 6c 61 67 73  ager->jfd, flags
1392e 2c 20 6a 72 6e 6c 42 75 66 66 65 72 53 69 7a 65  , jrnlBufferSize
1392f 28 70 50 61 67 65 72 29 0a 20 20 20 20 20 20 29  (pPager).      )
13930 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 72 63  ;.#else.      rc
13931 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e   = sqlite3OsOpen
13932 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a  (pVfs, pPager->z
13933 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d  Journal, pPager-
13934 3e 6a 66 64 2c 20 66 6c 61 67 73 2c 20 30 29 3b  >jfd, flags, 0);
13935 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20  .#endif.    }.  
13936 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51    assert( rc!=SQ
13937 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70 65  LITE_OK || isOpe
13938 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29  n(pPager->jfd) )
13939 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 57 72 69  ;.  }...  /* Wri
1393a 74 65 20 74 68 65 20 66 69 72 73 74 20 6a 6f 75  te the first jou
1393b 72 6e 61 6c 20 68 65 61 64 65 72 20 74 6f 20 74  rnal header to t
1393c 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
1393d 61 6e 64 20 6f 70 65 6e 20 0a 20 20 2a 2a 20 74  and open .  ** t
1393e 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69  he sub-journal i
1393f 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 20 20 2a  f necessary..  *
13940 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  /.  if( rc==SQLI
13941 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20  TE_OK ){.    /* 
13942 54 4f 44 4f 3a 20 43 68 65 63 6b 20 69 66 20 61  TODO: Check if a
13943 6c 6c 20 6f 66 20 74 68 65 73 65 20 61 72 65 20  ll of these are 
13944 72 65 61 6c 6c 79 20 72 65 71 75 69 72 65 64 2e  really required.
13945 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   */.    pPager->
13946 64 62 4f 72 69 67 53 69 7a 65 20 3d 20 70 50 61  dbOrigSize = pPa
13947 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 20  ger->dbSize;.   
13948 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
13949 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 20 20  Started = 0;.   
1394a 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e   pPager->needSyn
1394b 63 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65  c = 0;.    pPage
1394c 72 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20 20 20  r->nRec = 0;.   
1394d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
1394e 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 70 50 61  Off = 0;.    pPa
1394f 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d  ger->setMaster =
13950 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   0;.    pPager->
13951 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 30 3b 0a  journalHdr = 0;.
13952 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 4a 6f      rc = writeJo
13953 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72 29  urnalHdr(pPager)
13954 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d  ;.  }.  if( rc==
13955 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61  SQLITE_OK && pPa
13956 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20  ger->nSavepoint 
13957 29 7b 0a 20 20 20 20 72 63 20 3d 20 6f 70 65 6e  ){.    rc = open
13958 53 75 62 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65  SubJournal(pPage
13959 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72  r);.  }..  if( r
1395a 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
1395b 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 65      sqlite3Bitve
1395c 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d  cDestroy(pPager-
1395d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20  >pInJournal);.  
1395e 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75    pPager->pInJou
1395f 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  rnal = 0;.  }.  
13960 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
13961 0a 2a 2a 20 42 65 67 69 6e 20 61 20 77 72 69 74  .** Begin a writ
13962 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e  e-transaction on
13963 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 70   the specified p
13964 61 67 65 72 20 6f 62 6a 65 63 74 2e 20 49 66 20  ager object. If 
13965 61 20 0a 2a 2a 20 77 72 69 74 65 2d 74 72 61 6e  a .** write-tran
13966 73 61 63 74 69 6f 6e 20 68 61 73 20 61 6c 72 65  saction has alre
13967 61 64 79 20 62 65 65 6e 20 6f 70 65 6e 65 64 2c  ady been opened,
13968 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
13969 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a  s a no-op..**.**
1396a 20 49 66 20 74 68 65 20 65 78 46 6c 61 67 20 61   If the exFlag a
1396b 72 67 75 6d 65 6e 74 20 69 73 20 66 61 6c 73 65  rgument is false
1396c 2c 20 74 68 65 6e 20 61 63 71 75 69 72 65 20 61  , then acquire a
1396d 74 20 6c 65 61 73 74 20 61 20 52 45 53 45 52 56  t least a RESERV
1396e 45 44 0a 2a 2a 20 6c 6f 63 6b 20 6f 6e 20 74 68  ED.** lock on th
1396f 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
13970 20 49 66 20 65 78 46 6c 61 67 20 69 73 20 74 72   If exFlag is tr
13971 75 65 2c 20 74 68 65 6e 20 61 63 71 75 69 72 65  ue, then acquire
13972 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 61 6e 20   at least.** an 
13973 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20  EXCLUSIVE lock. 
13974 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69  If such a lock i
13975 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20  s already held, 
13976 6e 6f 20 6c 6f 63 6b 69 6e 67 20 0a 2a 2a 20 66  no locking .** f
13977 75 6e 63 74 69 6f 6e 73 20 6e 65 65 64 20 62 65  unctions need be
13978 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 49   called..**.** I
13979 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61 20  f this is not a 
1397a 74 65 6d 70 6f 72 61 72 79 20 6f 72 20 69 6e 2d  temporary or in-
1397b 6d 65 6d 6f 72 79 20 66 69 6c 65 20 61 6e 64 2c  memory file and,
1397c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
1397d 65 20 69 73 20 0a 2a 2a 20 6f 70 65 6e 65 64 20  e is .** opened 
1397e 69 66 20 69 74 20 68 61 73 20 6e 6f 74 20 62 65  if it has not be
1397f 65 6e 20 61 6c 72 65 61 64 79 2e 20 46 6f 72 20  en already. For 
13980 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  a temporary file
13981 2c 20 74 68 65 20 6f 70 65 6e 69 6e 67 20 0a 2a  , the opening .*
13982 2a 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  * of the journal
13983 20 66 69 6c 65 20 69 73 20 64 65 66 65 72 72 65   file is deferre
13984 64 20 75 6e 74 69 6c 20 74 68 65 72 65 20 69 73  d until there is
13985 20 61 6e 20 61 63 74 75 61 6c 20 6e 65 65 64 20   an actual need 
13986 74 6f 20 0a 2a 2a 20 77 72 69 74 65 20 74 6f 20  to .** write to 
13987 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 54 4f 44  the journal. TOD
13988 4f 3a 20 57 68 79 20 68 61 6e 64 6c 65 20 74 65  O: Why handle te
13989 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 64 69  mporary files di
1398a 66 66 65 72 65 6e 74 6c 79 3f 0a 2a 2a 0a 2a 2a  fferently?.**.**
1398b 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   If the journal 
1398c 66 69 6c 65 20 69 73 20 6f 70 65 6e 65 64 20 28  file is opened (
1398d 6f 72 20 69 66 20 69 74 20 69 73 20 61 6c 72 65  or if it is alre
1398e 61 64 79 20 6f 70 65 6e 29 2c 20 74 68 65 6e 20  ady open), then 
1398f 61 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 2d 68 65 61  a.** journal-hea
13990 64 65 72 20 69 73 20 77 72 69 74 74 65 6e 20 74  der is written t
13991 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 69  o the start of i
13992 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  t..**.** If the 
13993 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 61 72 67  subjInMemory arg
13994 75 6d 65 6e 74 20 69 73 20 6e 6f 6e 2d 7a 65 72  ument is non-zer
13995 6f 2c 20 74 68 65 6e 20 61 6e 79 20 73 75 62 2d  o, then any sub-
13996 6a 6f 75 72 6e 61 6c 20 6f 70 65 6e 65 64 0a 2a  journal opened.*
13997 2a 20 77 69 74 68 69 6e 20 74 68 69 73 20 74 72  * within this tr
13998 61 6e 73 61 63 74 69 6f 6e 20 77 69 6c 6c 20 62  ansaction will b
13999 65 20 6f 70 65 6e 65 64 20 61 73 20 61 6e 20 69  e opened as an i
1399a 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 2e 20 54  n-memory file. T
1399b 68 69 73 0a 2a 2a 20 68 61 73 20 6e 6f 20 65 66  his.** has no ef
1399c 66 65 63 74 20 69 66 20 74 68 65 20 73 75 62 2d  fect if the sub-
1399d 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6c 72 65 61  journal is alrea
1399e 64 79 20 6f 70 65 6e 65 64 20 28 61 73 20 69 74  dy opened (as it
1399f 20 6d 61 79 20 62 65 20 77 68 65 6e 0a 2a 2a 20   may be when.** 
139a0 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75  running in exclu
139a1 73 69 76 65 20 6d 6f 64 65 29 20 6f 72 20 69 66  sive mode) or if
139a2 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
139a3 20 64 6f 65 73 20 6e 6f 74 20 72 65 71 75 69 72   does not requir
139a4 65 20 61 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e  e a.** sub-journ
139a5 61 6c 2e 20 49 66 20 74 68 65 20 73 75 62 6a 49  al. If the subjI
139a6 6e 4d 65 6d 6f 72 79 20 61 72 67 75 6d 65 6e 74  nMemory argument
139a7 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20 61   is zero, then a
139a8 6e 79 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 73  ny required.** s
139a9 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 20 69 6d  ub-journal is im
139aa 70 6c 65 6d 65 6e 74 65 64 20 69 6e 2d 6d 65 6d  plemented in-mem
139ab 6f 72 79 20 69 66 20 70 50 61 67 65 72 20 69 73  ory if pPager is
139ac 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61   an in-memory da
139ad 74 61 62 61 73 65 2c 20 0a 2a 2a 20 6f 72 20 75  tabase, .** or u
139ae 73 69 6e 67 20 61 20 74 65 6d 70 6f 72 61 72 79  sing a temporary
139af 20 66 69 6c 65 20 6f 74 68 65 72 77 69 73 65 2e   file otherwise.
139b0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
139b1 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
139b2 67 65 72 42 65 67 69 6e 28 50 61 67 65 72 20 2a  gerBegin(Pager *
139b3 70 50 61 67 65 72 2c 20 69 6e 74 20 65 78 46 6c  pPager, int exFl
139b4 61 67 2c 20 69 6e 74 20 73 75 62 6a 49 6e 4d 65  ag, int subjInMe
139b5 6d 6f 72 79 29 7b 0a 20 20 69 6e 74 20 72 63 20  mory){.  int rc 
139b6 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61  = SQLITE_OK;.  a
139b7 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
139b8 74 61 74 65 21 3d 50 41 47 45 52 5f 55 4e 4c 4f  tate!=PAGER_UNLO
139b9 43 4b 20 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e  CK );.  pPager->
139ba 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 3d 20 28  subjInMemory = (
139bb 75 38 29 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 3b  u8)subjInMemory;
139bc 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73  .  if( pPager->s
139bd 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 48 41 52  tate==PAGER_SHAR
139be 45 44 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  ED ){.    assert
139bf 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75  ( pPager->pInJou
139c0 72 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 61  rnal==0 );.    a
139c1 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20 26 26  ssert( !MEMDB &&
139c2 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69   !pPager->tempFi
139c3 6c 65 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 4f 62  le );..    /* Ob
139c4 74 61 69 6e 20 61 20 52 45 53 45 52 56 45 44 20  tain a RESERVED 
139c5 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61  lock on the data
139c6 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20 74 68  base file. If th
139c7 65 20 65 78 46 6c 61 67 20 70 61 72 61 6d 65 74  e exFlag paramet
139c8 65 72 0a 20 20 20 20 2a 2a 20 69 73 20 74 72 75  er.    ** is tru
139c9 65 2c 20 74 68 65 6e 20 69 6d 6d 65 64 69 61 74  e, then immediat
139ca 65 6c 79 20 75 70 67 72 61 64 65 20 74 68 69 73  ely upgrade this
139cb 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 49 56 45   to an EXCLUSIVE
139cc 20 6c 6f 63 6b 2e 20 54 68 65 0a 20 20 20 20 2a   lock. The.    *
139cd 2a 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 63  * busy-handler c
139ce 61 6c 6c 62 61 63 6b 20 63 61 6e 20 62 65 20 75  allback can be u
139cf 73 65 64 20 77 68 65 6e 20 75 70 67 72 61 64 69  sed when upgradi
139d0 6e 67 20 74 6f 20 74 68 65 20 45 58 43 4c 55 53  ng to the EXCLUS
139d1 49 56 45 0a 20 20 20 20 2a 2a 20 6c 6f 63 6b 2c  IVE.    ** lock,
139d2 20 62 75 74 20 6e 6f 74 20 77 68 65 6e 20 6f 62   but not when ob
139d3 74 61 69 6e 69 6e 67 20 74 68 65 20 52 45 53 45  taining the RESE
139d4 52 56 45 44 20 6c 6f 63 6b 2e 0a 20 20 20 20 2a  RVED lock..    *
139d5 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  /.    rc = sqlit
139d6 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d  e3OsLock(pPager-
139d7 3e 66 64 2c 20 52 45 53 45 52 56 45 44 5f 4c 4f  >fd, RESERVED_LO
139d8 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  CK);.    if( rc=
139d9 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
139da 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74      pPager->stat
139db 65 20 3d 20 50 41 47 45 52 5f 52 45 53 45 52 56  e = PAGER_RESERV
139dc 45 44 3b 0a 20 20 20 20 20 20 69 66 28 20 65 78  ED;.      if( ex
139dd 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20 20  Flag ){.        
139de 72 63 20 3d 20 70 61 67 65 72 5f 77 61 69 74 5f  rc = pager_wait_
139df 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c 20  on_lock(pPager, 
139e0 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b  EXCLUSIVE_LOCK);
139e1 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
139e2 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 72 65      /* If the re
139e3 71 75 69 72 65 64 20 6c 6f 63 6b 73 20 77 65 72  quired locks wer
139e4 65 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6f  e successfully o
139e5 62 74 61 69 6e 65 64 2c 20 6f 70 65 6e 20 74 68  btained, open th
139e6 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a  e journal.    **
139e7 20 66 69 6c 65 20 61 6e 64 20 77 72 69 74 65 20   file and write 
139e8 74 68 65 20 66 69 72 73 74 20 6a 6f 75 72 6e 61  the first journa
139e9 6c 2d 68 65 61 64 65 72 20 74 6f 20 69 74 2e 0a  l-header to it..
139ea 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72      */.    if( r
139eb 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
139ec 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
139ed 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode!=PAGER_JOURN
139ee 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 7b 0a 20 20  ALMODE_OFF ){.  
139ef 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 6f      rc = pager_o
139f0 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 70 50 61 67  pen_journal(pPag
139f1 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  er);.    }.  }el
139f2 73 65 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50  se if( isOpen(pP
139f3 61 67 65 72 2d 3e 6a 66 64 29 20 26 26 20 70 50  ager->jfd) && pP
139f4 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
139f5 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  ==0 ){.    /* Th
139f6 69 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20  is happens when 
139f7 74 68 65 20 70 61 67 65 72 20 77 61 73 20 69 6e  the pager was in
139f8 20 65 78 63 6c 75 73 69 76 65 2d 61 63 63 65 73   exclusive-acces
139f9 73 20 6d 6f 64 65 20 74 68 65 20 6c 61 73 74 0a  s mode the last.
139fa 20 20 20 20 2a 2a 20 74 69 6d 65 20 61 20 28 72      ** time a (r
139fb 65 61 64 20 6f 72 20 77 72 69 74 65 29 20 74 72  ead or write) tr
139fc 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 73 75  ansaction was su
139fd 63 63 65 73 73 66 75 6c 6c 79 20 63 6f 6e 63 6c  ccessfully concl
139fe 75 64 65 64 0a 20 20 20 20 2a 2a 20 62 79 20 74  uded.    ** by t
139ff 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20  his connection. 
13a00 49 6e 73 74 65 61 64 20 6f 66 20 64 65 6c 65 74  Instead of delet
13a01 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ing the journal 
13a02 66 69 6c 65 20 69 74 20 77 61 73 20 0a 20 20 20  file it was .   
13a03 20 2a 2a 20 6b 65 70 74 20 6f 70 65 6e 20 61 6e   ** kept open an
13a04 64 20 65 69 74 68 65 72 20 77 61 73 20 74 72 75  d either was tru
13a05 6e 63 61 74 65 64 20 74 6f 20 30 20 62 79 74 65  ncated to 0 byte
13a06 73 20 6f 72 20 69 74 73 20 68 65 61 64 65 72 20  s or its header 
13a07 77 61 73 0a 20 20 20 20 2a 2a 20 6f 76 65 72 77  was.    ** overw
13a08 72 69 74 74 65 6e 20 77 69 74 68 20 7a 65 72 6f  ritten with zero
13a09 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73  s..    */.    as
13a0a 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 52  sert( pPager->nR
13a0b 65 63 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73  ec==0 );.    ass
13a0c 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 4f  ert( pPager->dbO
13a0d 72 69 67 53 69 7a 65 3d 3d 30 20 29 3b 0a 20 20  rigSize==0 );.  
13a0e 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
13a0f 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20  ->pInJournal==0 
13a10 29 3b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65  );.    rc = page
13a11 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 70  r_open_journal(p
13a12 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 50  Pager);.  }..  P
13a13 41 47 45 52 54 52 41 43 45 28 28 22 54 52 41 4e  AGERTRACE(("TRAN
13a14 53 41 43 54 49 4f 4e 20 25 64 5c 6e 22 2c 20 50  SACTION %d\n", P
13a15 41 47 45 52 49 44 28 70 50 61 67 65 72 29 29 29  AGERID(pPager)))
13a16 3b 0a 20 20 61 73 73 65 72 74 28 20 21 69 73 4f  ;.  assert( !isO
13a17 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29  pen(pPager->jfd)
13a18 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72   || pPager->jour
13a19 6e 61 6c 4f 66 66 3e 30 20 7c 7c 20 72 63 21 3d  nalOff>0 || rc!=
13a1a 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69  SQLITE_OK );.  i
13a1b 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
13a1c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
13a1d 21 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66  !pPager->dbModif
13a1e 69 65 64 20 29 3b 0a 20 20 20 20 2f 2a 20 49 67  ied );.    /* Ig
13a1f 6e 6f 72 65 20 61 6e 79 20 49 4f 20 65 72 72 6f  nore any IO erro
13a20 72 20 74 68 61 74 20 6f 63 63 75 72 73 20 77 69  r that occurs wi
13a21 74 68 69 6e 20 70 61 67 65 72 5f 65 6e 64 5f 74  thin pager_end_t
13a22 72 61 6e 73 61 63 74 69 6f 6e 28 29 2e 20 54 68  ransaction(). Th
13a23 65 0a 20 20 20 20 2a 2a 20 70 75 72 70 6f 73 65  e.    ** purpose
13a24 20 6f 66 20 74 68 69 73 20 63 61 6c 6c 20 69 73   of this call is
13a25 20 74 6f 20 72 65 73 65 74 20 74 68 65 20 69 6e   to reset the in
13a26 74 65 72 6e 61 6c 20 73 74 61 74 65 20 6f 66 20  ternal state of 
13a27 74 68 65 20 70 61 67 65 72 0a 20 20 20 20 2a 2a  the pager.    **
13a28 20 73 75 62 2d 73 79 73 74 65 6d 2e 20 49 74 20   sub-system. It 
13a29 64 6f 65 73 6e 27 74 20 6d 61 74 74 65 72 20 69  doesn't matter i
13a2a 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 66 69  f the journal-fi
13a2b 6c 65 20 69 73 20 6e 6f 74 20 70 72 6f 70 65 72  le is not proper
13a2c 6c 79 0a 20 20 20 20 2a 2a 20 66 69 6e 61 6c 69  ly.    ** finali
13a2d 7a 65 64 20 61 74 20 74 68 69 73 20 70 6f 69 6e  zed at this poin
13a2e 74 20 28 73 69 6e 63 65 20 69 74 20 69 73 20 6e  t (since it is n
13a2f 6f 74 20 61 20 76 61 6c 69 64 20 6a 6f 75 72 6e  ot a valid journ
13a30 61 6c 20 66 69 6c 65 20 61 6e 79 77 61 79 29 2e  al file anyway).
13a31 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 61 67 65  .    */.    page
13a32 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f  r_end_transactio
13a33 6e 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20  n(pPager, 0);.  
13a34 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
13a35 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 61 20 73  ../*.** Mark a s
13a36 69 6e 67 6c 65 20 64 61 74 61 20 70 61 67 65 20  ingle data page 
13a37 61 73 20 77 72 69 74 65 61 62 6c 65 2e 20 54 68  as writeable. Th
13a38 65 20 70 61 67 65 20 69 73 20 77 72 69 74 74 65  e page is writte
13a39 6e 20 69 6e 74 6f 20 74 68 65 20 0a 2a 2a 20 6d  n into the .** m
13a3a 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 73  ain journal or s
13a3b 75 62 2d 6a 6f 75 72 6e 61 6c 20 61 73 20 72 65  ub-journal as re
13a3c 71 75 69 72 65 64 2e 20 49 66 20 74 68 65 20 70  quired. If the p
13a3d 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 69  age is written i
13a3e 6e 74 6f 0a 2a 2a 20 6f 6e 65 20 6f 66 20 74 68  nto.** one of th
13a3f 65 20 6a 6f 75 72 6e 61 6c 73 2c 20 74 68 65 20  e journals, the 
13a40 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69  corresponding bi
13a41 74 20 69 73 20 73 65 74 20 69 6e 20 74 68 65 20  t is set in the 
13a42 0a 2a 2a 20 50 61 67 65 72 2e 70 49 6e 4a 6f 75  .** Pager.pInJou
13a43 72 6e 61 6c 20 62 69 74 76 65 63 20 61 6e 64 20  rnal bitvec and 
13a44 74 68 65 20 50 61 67 65 72 53 61 76 65 70 6f 69  the PagerSavepoi
13a45 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 20  nt.pInSavepoint 
13a46 62 69 74 76 65 63 73 0a 2a 2a 20 6f 66 20 61 6e  bitvecs.** of an
13a47 79 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74  y open savepoint
13a48 73 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 65  s as appropriate
13a49 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
13a4a 70 61 67 65 72 5f 77 72 69 74 65 28 50 67 48 64  pager_write(PgHd
13a4b 72 20 2a 70 50 67 29 7b 0a 20 20 76 6f 69 64 20  r *pPg){.  void 
13a4c 2a 70 44 61 74 61 20 3d 20 70 50 67 2d 3e 70 44  *pData = pPg->pD
13a4d 61 74 61 3b 0a 20 20 50 61 67 65 72 20 2a 70 50  ata;.  Pager *pP
13a4e 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67  ager = pPg->pPag
13a4f 65 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  er;.  int rc = S
13a50 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20  QLITE_OK;..  /* 
13a51 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
13a52 6e 6f 74 20 63 61 6c 6c 65 64 20 75 6e 6c 65 73  not called unles
13a53 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  s a transaction 
13a54 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e  has already been
13a55 0a 20 20 2a 2a 20 73 74 61 72 74 65 64 2e 0a 20  .  ** started.. 
13a56 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50   */.  assert( pP
13a57 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47  ager->state>=PAG
13a58 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 0a  ER_RESERVED );..
13a59 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f 72    /* If an error
13a5a 20 68 61 73 20 62 65 65 6e 20 70 72 65 76 69 6f   has been previo
13a5b 75 73 6c 79 20 64 65 74 65 63 74 65 64 2c 20 77  usly detected, w
13a5c 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a  e should not be.
13a5d 20 20 2a 2a 20 63 61 6c 6c 69 6e 67 20 74 68 69    ** calling thi
13a5e 73 20 72 6f 75 74 69 6e 65 2e 20 20 52 65 70 65  s routine.  Repe
13a5f 61 74 20 74 68 65 20 65 72 72 6f 72 20 66 6f 72  at the error for
13a60 20 72 6f 62 75 73 74 6e 65 73 73 2e 0a 20 20 2a   robustness..  *
13a61 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 50  /.  if( NEVER(pP
13a62 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 20 29  ager->errCode) )
13a63 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d    return pPager-
13a64 3e 65 72 72 43 6f 64 65 3b 0a 0a 20 20 2f 2a 20  >errCode;..  /* 
13a65 48 69 67 68 65 72 2d 6c 65 76 65 6c 20 72 6f 75  Higher-level rou
13a66 74 69 6e 65 73 20 6e 65 76 65 72 20 63 61 6c 6c  tines never call
13a67 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
13a68 66 20 64 61 74 61 62 61 73 65 20 69 73 20 6e 6f  f database is no
13a69 74 0a 20 20 2a 2a 20 77 72 69 74 61 62 6c 65 2e  t.  ** writable.
13a6a 20 20 42 75 74 20 63 68 65 63 6b 20 61 6e 79 77    But check anyw
13a6b 61 79 2c 20 6a 75 73 74 20 66 6f 72 20 72 6f 62  ay, just for rob
13a6c 75 73 74 6e 65 73 73 2e 20 2a 2f 0a 20 20 69 66  ustness. */.  if
13a6d 28 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d 3e  ( NEVER(pPager->
13a6e 72 65 61 64 4f 6e 6c 79 29 20 29 20 72 65 74 75  readOnly) ) retu
13a6f 72 6e 20 53 51 4c 49 54 45 5f 50 45 52 4d 3b 0a  rn SQLITE_PERM;.
13a70 0a 20 20 61 73 73 65 72 74 28 20 21 70 50 61 67  .  assert( !pPag
13a71 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 29 3b  er->setMaster );
13a72 0a 0a 20 20 43 48 45 43 4b 5f 50 41 47 45 28 70  ..  CHECK_PAGE(p
13a73 50 67 29 3b 0a 0a 20 20 2f 2a 20 4d 61 72 6b 20  Pg);..  /* Mark 
13a74 74 68 65 20 70 61 67 65 20 61 73 20 64 69 72 74  the page as dirt
13a75 79 2e 20 20 49 66 20 74 68 65 20 70 61 67 65 20  y.  If the page 
13a76 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e  has already been
13a77 20 77 72 69 74 74 65 6e 0a 20 20 2a 2a 20 74 6f   written.  ** to
13a78 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 74 68 65   the journal the
13a79 6e 20 77 65 20 63 61 6e 20 72 65 74 75 72 6e 20  n we can return 
13a7a 72 69 67 68 74 20 61 77 61 79 2e 0a 20 20 2a 2f  right away..  */
13a7b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  .  sqlite3Pcache
13a7c 4d 61 6b 65 44 69 72 74 79 28 70 50 67 29 3b 0a  MakeDirty(pPg);.
13a7d 20 20 69 66 28 20 70 61 67 65 49 6e 4a 6f 75 72    if( pageInJour
13a7e 6e 61 6c 28 70 50 67 29 20 26 26 20 21 73 75 62  nal(pPg) && !sub
13a7f 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 70 50  jRequiresPage(pP
13a80 67 29 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72  g) ){.    pPager
13a81 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 3d 20 31  ->dbModified = 1
13a82 3b 0a 20 20 7d 65 6c 73 65 7b 0a 0a 20 20 20 20  ;.  }else{..    
13a83 2f 2a 20 49 66 20 77 65 20 67 65 74 20 74 68 69  /* If we get thi
13a84 73 20 66 61 72 2c 20 69 74 20 6d 65 61 6e 73 20  s far, it means 
13a85 74 68 61 74 20 74 68 65 20 70 61 67 65 20 6e 65  that the page ne
13a86 65 64 73 20 74 6f 20 62 65 0a 20 20 20 20 2a 2a  eds to be.    **
13a87 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20   written to the 
13a88 74 72 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72  transaction jour
13a89 6e 61 6c 20 6f 72 20 74 68 65 20 63 6b 65 63 6b  nal or the ckeck
13a8a 70 6f 69 6e 74 20 6a 6f 75 72 6e 61 6c 0a 20 20  point journal.  
13a8b 20 20 2a 2a 20 6f 72 20 62 6f 74 68 2e 0a 20 20    ** or both..  
13a8c 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 48 69 67 68    **.    ** High
13a8d 65 72 20 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65  er level routine
13a8e 73 20 73 68 6f 75 6c 64 20 68 61 76 65 20 61 6c  s should have al
13a8f 72 65 61 64 79 20 73 74 61 72 74 65 64 20 61 20  ready started a 
13a90 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 20 20 20  transaction,.   
13a91 20 2a 2a 20 77 68 69 63 68 20 6d 65 61 6e 73 20   ** which means 
13a92 74 68 65 79 20 68 61 76 65 20 61 63 71 75 69 72  they have acquir
13a93 65 64 20 74 68 65 20 6e 65 63 65 73 73 61 72 79  ed the necessary
13a94 20 6c 6f 63 6b 73 20 61 6e 64 20 6f 70 65 6e 65   locks and opene
13a95 64 0a 20 20 20 20 2a 2a 20 61 20 72 6f 6c 6c 62  d.    ** a rollb
13a96 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 20 44 6f  ack journal.  Do
13a97 75 62 6c 65 2d 63 68 65 63 6b 20 74 6f 20 6d 61  uble-check to ma
13a98 6b 65 73 20 73 75 72 65 20 74 68 69 73 20 69 73  kes sure this is
13a99 20 74 68 65 20 63 61 73 65 2e 0a 20 20 20 20 2a   the case..    *
13a9a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  /.    rc = sqlit
13a9b 65 33 50 61 67 65 72 42 65 67 69 6e 28 70 50 61  e3PagerBegin(pPa
13a9c 67 65 72 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e  ger, 0, pPager->
13a9d 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 29 3b 0a 20  subjInMemory);. 
13a9e 20 20 20 69 66 28 20 4e 45 56 45 52 28 72 63 21     if( NEVER(rc!
13a9f 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 7b 0a 20  =SQLITE_OK) ){. 
13aa0 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
13aa1 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 69      }.    if( !i
13aa2 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66  sOpen(pPager->jf
13aa3 64 29 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f  d) && pPager->jo
13aa4 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52  urnalMode!=PAGER
13aa5 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46  _JOURNALMODE_OFF
13aa6 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
13aa7 28 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75  ( pPager->useJou
13aa8 72 6e 61 6c 20 29 3b 0a 20 20 20 20 20 20 72 63  rnal );.      rc
13aa9 20 3d 20 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f   = pager_open_jo
13aaa 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20  urnal(pPager);. 
13aab 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
13aac 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
13aad 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50  rc;.    }.    pP
13aae 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64  ager->dbModified
13aaf 20 3d 20 31 3b 0a 20 20 0a 20 20 20 20 2f 2a 20   = 1;.  .    /* 
13ab0 54 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  The transaction 
13ab1 6a 6f 75 72 6e 61 6c 20 6e 6f 77 20 65 78 69 73  journal now exis
13ab2 74 73 20 61 6e 64 20 77 65 20 68 61 76 65 20 61  ts and we have a
13ab3 20 52 45 53 45 52 56 45 44 20 6f 72 20 61 6e 0a   RESERVED or an.
13ab4 20 20 20 20 2a 2a 20 45 58 43 4c 55 53 49 56 45      ** EXCLUSIVE
13ab5 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 6d 61 69   lock on the mai
13ab6 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  n database file.
13ab7 20 20 57 72 69 74 65 20 74 68 65 20 63 75 72 72    Write the curr
13ab8 65 6e 74 20 70 61 67 65 20 74 6f 0a 20 20 20 20  ent page to.    
13ab9 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  ** the transacti
13aba 6f 6e 20 6a 6f 75 72 6e 61 6c 20 69 66 20 69 74  on journal if it
13abb 20 69 73 20 6e 6f 74 20 74 68 65 72 65 20 61 6c   is not there al
13abc 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a 20 20  ready..    */.  
13abd 20 20 69 66 28 20 21 70 61 67 65 49 6e 4a 6f 75    if( !pageInJou
13abe 72 6e 61 6c 28 70 50 67 29 20 26 26 20 69 73 4f  rnal(pPg) && isO
13abf 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29  pen(pPager->jfd)
13ac0 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50   ){.      if( pP
13ac1 67 2d 3e 70 67 6e 6f 3c 3d 70 50 61 67 65 72 2d  g->pgno<=pPager-
13ac2 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 7b 0a 20  >dbOrigSize ){. 
13ac3 20 20 20 20 20 20 20 75 33 32 20 63 6b 73 75 6d         u32 cksum
13ac4 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a  ;.        char *
13ac5 70 44 61 74 61 32 3b 0a 0a 20 20 20 20 20 20 20  pData2;..       
13ac6 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 6e 65   /* We should ne
13ac7 76 65 72 20 77 72 69 74 65 20 74 6f 20 74 68 65  ver write to the
13ac8 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68   journal file th
13ac9 65 20 70 61 67 65 20 74 68 61 74 0a 20 20 20 20  e page that.    
13aca 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 20      ** contains 
13acb 74 68 65 20 64 61 74 61 62 61 73 65 20 6c 6f 63  the database loc
13acc 6b 73 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ks.  The followi
13acd 6e 67 20 61 73 73 65 72 74 20 76 65 72 69 66 69  ng assert verifi
13ace 65 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68  es.        ** th
13acf 61 74 20 77 65 20 64 6f 20 6e 6f 74 2e 20 2a 2f  at we do not. */
13ad0 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
13ad1 20 70 50 67 2d 3e 70 67 6e 6f 21 3d 50 41 47 45   pPg->pgno!=PAGE
13ad2 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72  R_MJ_PGNO(pPager
13ad3 29 20 29 3b 0a 20 20 20 20 20 20 20 20 43 4f 44  ) );.        COD
13ad4 45 43 32 28 70 50 61 67 65 72 2c 20 70 44 61 74  EC2(pPager, pDat
13ad5 61 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 37 2c  a, pPg->pgno, 7,
13ad6 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
13ad7 4f 4d 45 4d 2c 20 70 44 61 74 61 32 29 3b 0a 20  OMEM, pData2);. 
13ad8 20 20 20 20 20 20 20 63 6b 73 75 6d 20 3d 20 70         cksum = p
13ad9 61 67 65 72 5f 63 6b 73 75 6d 28 70 50 61 67 65  ager_cksum(pPage
13ada 72 2c 20 28 75 38 2a 29 70 44 61 74 61 32 29 3b  r, (u8*)pData2);
13adb 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 77 72  .        rc = wr
13adc 69 74 65 33 32 62 69 74 73 28 70 50 61 67 65 72  ite32bits(pPager
13add 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a  ->jfd, pPager->j
13ade 6f 75 72 6e 61 6c 4f 66 66 2c 20 70 50 67 2d 3e  ournalOff, pPg->
13adf 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 69  pgno);.        i
13ae0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
13ae1 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63   ){.          rc
13ae2 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74   = sqlite3OsWrit
13ae3 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70  e(pPager->jfd, p
13ae4 44 61 74 61 32 2c 20 70 50 61 67 65 72 2d 3e 70  Data2, pPager->p
13ae5 61 67 65 53 69 7a 65 2c 0a 20 20 20 20 20 20 20  ageSize,.       
13ae6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13ae7 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a         pPager->j
13ae8 6f 75 72 6e 61 6c 4f 66 66 20 2b 20 34 29 3b 0a  ournalOff + 4);.
13ae9 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72            pPager
13aea 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20  ->journalOff += 
13aeb 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
13aec 2b 34 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  +4;.        }.  
13aed 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
13aee 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
13aef 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 33       rc = write3
13af0 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66  2bits(pPager->jf
13af1 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  d, pPager->journ
13af2 61 6c 4f 66 66 2c 20 63 6b 73 75 6d 29 3b 0a 20  alOff, cksum);. 
13af3 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d           pPager-
13af4 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 34  >journalOff += 4
13af5 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
13af6 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a 4f      IOTRACE(("JO
13af7 55 54 20 25 70 20 25 64 20 25 6c 6c 64 20 25 64  UT %p %d %lld %d
13af8 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 67  \n", pPager, pPg
13af9 2d 3e 70 67 6e 6f 2c 20 0a 20 20 20 20 20 20 20  ->pgno, .       
13afa 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72            pPager
13afb 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 70 50  ->journalOff, pP
13afc 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 29  ager->pageSize))
13afd 3b 0a 20 20 20 20 20 20 20 20 50 41 47 45 52 5f  ;.        PAGER_
13afe 49 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 61 67  INCR(sqlite3_pag
13aff 65 72 5f 77 72 69 74 65 6a 5f 63 6f 75 6e 74 29  er_writej_count)
13b00 3b 0a 20 20 20 20 20 20 20 20 50 41 47 45 52 54  ;.        PAGERT
13b01 52 41 43 45 28 28 22 4a 4f 55 52 4e 41 4c 20 25  RACE(("JOURNAL %
13b02 64 20 70 61 67 65 20 25 64 20 6e 65 65 64 53 79  d page %d needSy
13b03 6e 63 3d 25 64 20 68 61 73 68 28 25 30 38 78 29  nc=%d hash(%08x)
13b04 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \n",.           
13b05 20 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72    PAGERID(pPager
13b06 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 0a 20  ), pPg->pgno, . 
13b07 20 20 20 20 20 20 20 20 20 20 20 20 28 28 70 50              ((pP
13b08 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e  g->flags&PGHDR_N
13b09 45 45 44 5f 53 59 4e 43 29 3f 31 3a 30 29 2c 20  EED_SYNC)?1:0), 
13b0a 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70  pager_pagehash(p
13b0b 50 67 29 29 29 3b 0a 0a 20 20 20 20 20 20 20 20  Pg)));..        
13b0c 2f 2a 20 45 76 65 6e 20 69 66 20 61 6e 20 49 4f  /* Even if an IO
13b0d 20 6f 72 20 64 69 73 6b 66 75 6c 6c 20 65 72 72   or diskfull err
13b0e 6f 72 20 6f 63 63 75 72 72 65 64 20 77 68 69 6c  or occurred whil
13b0f 65 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 74 68  e journalling th
13b10 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67  e.        ** pag
13b11 65 20 69 6e 20 74 68 65 20 62 6c 6f 63 6b 20 61  e in the block a
13b12 62 6f 76 65 2c 20 73 65 74 20 74 68 65 20 6e 65  bove, set the ne
13b13 65 64 2d 73 79 6e 63 20 66 6c 61 67 20 66 6f 72  ed-sync flag for
13b14 20 74 68 65 20 70 61 67 65 2e 0a 20 20 20 20 20   the page..     
13b15 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c     ** Otherwise,
13b16 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61   when the transa
13b17 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20  ction is rolled 
13b18 62 61 63 6b 2c 20 74 68 65 20 6c 6f 67 69 63 20  back, the logic 
13b19 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 6c  in.        ** pl
13b1a 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28  ayback_one_page(
13b1b 29 20 77 69 6c 6c 20 74 68 69 6e 6b 20 74 68 61  ) will think tha
13b1c 74 20 74 68 65 20 70 61 67 65 20 6e 65 65 64 73  t the page needs
13b1d 20 74 6f 20 62 65 20 72 65 73 74 6f 72 65 64 0a   to be restored.
13b1e 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 20 74 68          ** in th
13b1f 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
13b20 20 41 6e 64 20 69 66 20 61 6e 20 49 4f 20 65 72   And if an IO er
13b21 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65  ror occurs while
13b22 20 64 6f 69 6e 67 20 73 6f 2c 0a 20 20 20 20 20   doing so,.     
13b23 20 20 20 2a 2a 20 74 68 65 6e 20 63 6f 72 72 75     ** then corru
13b24 70 74 69 6f 6e 20 6d 61 79 20 66 6f 6c 6c 6f 77  ption may follow
13b25 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
13b26 20 20 20 20 20 69 66 28 20 21 70 50 61 67 65 72       if( !pPager
13b27 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20  ->noSync ){.    
13b28 20 20 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73        pPg->flags
13b29 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44 5f 53   |= PGHDR_NEED_S
13b2a 59 4e 43 3b 0a 20 20 20 20 20 20 20 20 20 20 70  YNC;.          p
13b2b 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20  Pager->needSync 
13b2c 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a  = 1;.        }..
13b2d 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 72          /* An er
13b2e 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 64  ror has occurred
13b2f 20 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20   writing to the 
13b30 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68  journal file. Th
13b31 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 72  e .        ** tr
13b32 61 6e 73 61 63 74 69 6f 6e 20 77 69 6c 6c 20 62  ansaction will b
13b33 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 62 79  e rolled back by
13b34 20 74 68 65 20 6c 61 79 65 72 20 61 62 6f 76 65   the layer above
13b35 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
13b36 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
13b37 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
13b38 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
13b39 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20         }..      
13b3a 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 2b 2b    pPager->nRec++
13b3b 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
13b3c 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75  ( pPager->pInJou
13b3d 72 6e 61 6c 21 3d 30 20 29 3b 0a 20 20 20 20 20  rnal!=0 );.     
13b3e 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
13b3f 69 74 76 65 63 53 65 74 28 70 50 61 67 65 72 2d  itvecSet(pPager-
13b40 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 50 67  >pInJournal, pPg
13b41 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20  ->pgno);.       
13b42 20 74 65 73 74 63 61 73 65 28 20 72 63 3d 3d 53   testcase( rc==S
13b43 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20  QLITE_NOMEM );. 
13b44 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72         assert( r
13b45 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c==SQLITE_OK || 
13b46 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
13b47 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 7c   );.        rc |
13b48 3d 20 61 64 64 54 6f 53 61 76 65 70 6f 69 6e 74  = addToSavepoint
13b49 42 69 74 76 65 63 73 28 70 50 61 67 65 72 2c 20  Bitvecs(pPager, 
13b4a 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20  pPg->pgno);.    
13b4b 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
13b4c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
13b4d 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53     assert( rc==S
13b4e 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20  QLITE_NOMEM );. 
13b4f 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
13b50 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  rc;.        }.  
13b51 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
13b52 20 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e     if( !pPager->
13b53 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 26  journalStarted &
13b54 26 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e  & !pPager->noSyn
13b55 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  c ){.          p
13b56 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48  Pg->flags |= PGH
13b57 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20  DR_NEED_SYNC;.  
13b58 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e          pPager->
13b59 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20  needSync = 1;.  
13b5a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
13b5b 50 41 47 45 52 54 52 41 43 45 28 28 22 41 50 50  PAGERTRACE(("APP
13b5c 45 4e 44 20 25 64 20 70 61 67 65 20 25 64 20 6e  END %d page %d n
13b5d 65 65 64 53 79 6e 63 3d 25 64 5c 6e 22 2c 0a 20  eedSync=%d\n",. 
13b5e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50                 P
13b5f 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20  AGERID(pPager), 
13b60 70 50 67 2d 3e 70 67 6e 6f 2c 0a 20 20 20 20 20  pPg->pgno,.     
13b61 20 20 20 20 20 20 20 20 20 20 28 28 70 50 67 2d            ((pPg-
13b62 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45  >flags&PGHDR_NEE
13b63 44 5f 53 59 4e 43 29 3f 31 3a 30 29 29 29 3b 0a  D_SYNC)?1:0)));.
13b64 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
13b65 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73  .    /* If the s
13b66 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c  tatement journal
13b67 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 74 68 65   is open and the
13b68 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e 20   page is not in 
13b69 69 74 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20  it,.    ** then 
13b6a 77 72 69 74 65 20 74 68 65 20 63 75 72 72 65 6e  write the curren
13b6b 74 20 70 61 67 65 20 74 6f 20 74 68 65 20 73 74  t page to the st
13b6c 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e  atement journal.
13b6d 20 20 4e 6f 74 65 20 74 68 61 74 0a 20 20 20 20    Note that.    
13b6e 2a 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  ** the statement
13b6f 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 20   journal format 
13b70 64 69 66 66 65 72 73 20 66 72 6f 6d 20 74 68 65  differs from the
13b71 20 73 74 61 6e 64 61 72 64 20 6a 6f 75 72 6e 61   standard journa
13b72 6c 20 66 6f 72 6d 61 74 0a 20 20 20 20 2a 2a 20  l format.    ** 
13b73 69 6e 20 74 68 61 74 20 69 74 20 6f 6d 69 74 73  in that it omits
13b74 20 74 68 65 20 63 68 65 63 6b 73 75 6d 73 20 61   the checksums a
13b75 6e 64 20 74 68 65 20 68 65 61 64 65 72 2e 0a 20  nd the header.. 
13b76 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 75     */.    if( su
13b77 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 70  bjRequiresPage(p
13b78 50 67 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20  Pg) ){.      rc 
13b79 3d 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65  = subjournalPage
13b7a 28 70 50 67 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  (pPg);.    }.  }
13b7b 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68  ..  /* Update th
13b7c 65 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20  e database size 
13b7d 61 6e 64 20 72 65 74 75 72 6e 2e 0a 20 20 2a 2f  and return..  */
13b7e 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
13b7f 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f  r->state>=PAGER_
13b80 53 48 41 52 45 44 20 29 3b 0a 20 20 69 66 28 20  SHARED );.  if( 
13b81 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3c 70  pPager->dbSize<p
13b82 50 67 2d 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20  Pg->pgno ){.    
13b83 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d  pPager->dbSize =
13b84 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20 7d 0a   pPg->pgno;.  }.
13b85 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
13b86 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 61 20 64 61 74  /*.** Mark a dat
13b87 61 20 70 61 67 65 20 61 73 20 77 72 69 74 65 61  a page as writea
13b88 62 6c 65 2e 20 54 68 69 73 20 72 6f 75 74 69 6e  ble. This routin
13b89 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64  e must be called
13b8a 20 62 65 66 6f 72 65 20 0a 2a 2a 20 6d 61 6b 69   before .** maki
13b8b 6e 67 20 63 68 61 6e 67 65 73 20 74 6f 20 61 20  ng changes to a 
13b8c 70 61 67 65 2e 20 54 68 65 20 63 61 6c 6c 65 72  page. The caller
13b8d 20 6d 75 73 74 20 63 68 65 63 6b 20 74 68 65 20   must check the 
13b8e 72 65 74 75 72 6e 20 76 61 6c 75 65 20 0a 2a 2a  return value .**
13b8f 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f   of this functio
13b90 6e 20 61 6e 64 20 62 65 20 63 61 72 65 66 75 6c  n and be careful
13b91 20 6e 6f 74 20 74 6f 20 63 68 61 6e 67 65 20 61   not to change a
13b92 6e 79 20 70 61 67 65 20 64 61 74 61 20 75 6e 6c  ny page data unl
13b93 65 73 73 20 0a 2a 2a 20 74 68 69 73 20 72 6f 75  ess .** this rou
13b94 74 69 6e 65 20 72 65 74 75 72 6e 73 20 53 51 4c  tine returns SQL
13b95 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68  ITE_OK..**.** Th
13b96 65 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74  e difference bet
13b97 77 65 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  ween this functi
13b98 6f 6e 20 61 6e 64 20 70 61 67 65 72 5f 77 72 69  on and pager_wri
13b99 74 65 28 29 20 69 73 20 74 68 61 74 20 74 68 69  te() is that thi
13b9a 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61 6c  s.** function al
13b9b 73 6f 20 64 65 61 6c 73 20 77 69 74 68 20 74 68  so deals with th
13b9c 65 20 73 70 65 63 69 61 6c 20 63 61 73 65 20 77  e special case w
13b9d 68 65 72 65 20 32 20 6f 72 20 6d 6f 72 65 20 70  here 2 or more p
13b9e 61 67 65 73 0a 2a 2a 20 66 69 74 20 6f 6e 20 61  ages.** fit on a
13b9f 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 65 63   single disk sec
13ba0 74 6f 72 2e 20 49 6e 20 74 68 69 73 20 63 61 73  tor. In this cas
13ba1 65 20 61 6c 6c 20 63 6f 2d 72 65 73 69 64 65 6e  e all co-residen
13ba2 74 20 70 61 67 65 73 0a 2a 2a 20 6d 75 73 74 20  t pages.** must 
13ba3 68 61 76 65 20 62 65 65 6e 20 77 72 69 74 74 65  have been writte
13ba4 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c  n to the journal
13ba5 20 66 69 6c 65 20 62 65 66 6f 72 65 20 72 65 74   file before ret
13ba6 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66  urning..**.** If
13ba7 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
13ba8 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 6f  , SQLITE_NOMEM o
13ba9 72 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f  r an IO error co
13baa 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 0a 2a  de is returned.*
13bab 2a 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 65  * as appropriate
13bac 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c  . Otherwise, SQL
13bad 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54  ITE_OK..*/.SQLIT
13bae 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
13baf 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
13bb0 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 29  DbPage *pDbPage)
13bb1 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
13bb2 49 54 45 5f 4f 4b 3b 0a 0a 20 20 50 67 48 64 72  ITE_OK;..  PgHdr
13bb3 20 2a 70 50 67 20 3d 20 70 44 62 50 61 67 65 3b   *pPg = pDbPage;
13bb4 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
13bb5 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a   = pPg->pPager;.
13bb6 20 20 50 67 6e 6f 20 6e 50 61 67 65 50 65 72 53    Pgno nPagePerS
13bb7 65 63 74 6f 72 20 3d 20 28 70 50 61 67 65 72 2d  ector = (pPager-
13bb8 3e 73 65 63 74 6f 72 53 69 7a 65 2f 70 50 61 67  >sectorSize/pPag
13bb9 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 0a  er->pageSize);..
13bba 20 20 69 66 28 20 6e 50 61 67 65 50 65 72 53 65    if( nPagePerSe
13bbb 63 74 6f 72 3e 31 20 29 7b 0a 20 20 20 20 50 67  ctor>1 ){.    Pg
13bbc 6e 6f 20 6e 50 61 67 65 43 6f 75 6e 74 3b 20 20  no nPageCount;  
13bbd 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c          /* Total
13bbe 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
13bbf 20 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c   in database fil
13bc0 65 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 70 67  e */.    Pgno pg
13bc1 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1;              
13bc2 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61 67 65     /* First page
13bc3 20 6f 66 20 74 68 65 20 73 65 63 74 6f 72 20 70   of the sector p
13bc4 50 67 20 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e  Pg is located on
13bc5 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 50 61  . */.    int nPa
13bc6 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ge;             
13bc7 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
13bc8 70 61 67 65 73 20 73 74 61 72 74 69 6e 67 20 61  pages starting a
13bc9 74 20 70 67 31 20 74 6f 20 6a 6f 75 72 6e 61 6c  t pg1 to journal
13bca 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b 20   */.    int ii; 
13bcb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13bcc 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
13bcd 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 65 65  r */.    int nee
13bce 64 53 79 6e 63 20 3d 20 30 3b 20 20 20 20 20 20  dSync = 0;      
13bcf 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e     /* True if an
13bd0 79 20 70 61 67 65 20 68 61 73 20 50 47 48 44 52  y page has PGHDR
13bd1 5f 4e 45 45 44 5f 53 59 4e 43 20 2a 2f 0a 0a 20  _NEED_SYNC */.. 
13bd2 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 64 6f     /* Set the do
13bd3 4e 6f 74 53 79 6e 63 20 66 6c 61 67 20 74 6f 20  NotSync flag to 
13bd4 31 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 75  1. This is becau
13bd5 73 65 20 77 65 20 63 61 6e 6e 6f 74 20 61 6c 6c  se we cannot all
13bd6 6f 77 20 61 20 6a 6f 75 72 6e 61 6c 0a 20 20 20  ow a journal.   
13bd7 20 2a 2a 20 68 65 61 64 65 72 20 74 6f 20 62 65   ** header to be
13bd8 20 77 72 69 74 74 65 6e 20 62 65 74 77 65 65 6e   written between
13bd9 20 74 68 65 20 70 61 67 65 73 20 6a 6f 75 72 6e   the pages journ
13bda 61 6c 65 64 20 62 79 20 74 68 69 73 20 66 75 6e  aled by this fun
13bdb 63 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20  ction..    */.  
13bdc 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42    assert( !MEMDB
13bdd 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
13bde 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e  pPager->doNotSyn
13bdf 63 3d 3d 30 20 29 3b 0a 20 20 20 20 70 50 61 67  c==0 );.    pPag
13be0 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 20 3d 20  er->doNotSync = 
13be1 31 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20  1;..    /* This 
13be2 74 72 69 63 6b 20 61 73 73 75 6d 65 73 20 74 68  trick assumes th
13be3 61 74 20 62 6f 74 68 20 74 68 65 20 70 61 67 65  at both the page
13be4 2d 73 69 7a 65 20 61 6e 64 20 73 65 63 74 6f 72  -size and sector
13be5 2d 73 69 7a 65 20 61 72 65 0a 20 20 20 20 2a 2a  -size are.    **
13be6 20 61 6e 20 69 6e 74 65 67 65 72 20 70 6f 77 65   an integer powe
13be7 72 20 6f 66 20 32 2e 20 49 74 20 73 65 74 73 20  r of 2. It sets 
13be8 76 61 72 69 61 62 6c 65 20 70 67 31 20 74 6f 20  variable pg1 to 
13be9 74 68 65 20 69 64 65 6e 74 69 66 69 65 72 0a 20  the identifier. 
13bea 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 66 69 72     ** of the fir
13beb 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 73  st page of the s
13bec 65 63 74 6f 72 20 70 50 67 20 69 73 20 6c 6f 63  ector pPg is loc
13bed 61 74 65 64 20 6f 6e 2e 0a 20 20 20 20 2a 2f 0a  ated on..    */.
13bee 20 20 20 20 70 67 31 20 3d 20 28 28 70 50 67 2d      pg1 = ((pPg-
13bef 3e 70 67 6e 6f 2d 31 29 20 26 20 7e 28 6e 50 61  >pgno-1) & ~(nPa
13bf0 67 65 50 65 72 53 65 63 74 6f 72 2d 31 29 29 20  gePerSector-1)) 
13bf1 2b 20 31 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65  + 1;..    sqlite
13bf2 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28  3PagerPagecount(
13bf3 70 50 61 67 65 72 2c 20 28 69 6e 74 20 2a 29 26  pPager, (int *)&
13bf4 6e 50 61 67 65 43 6f 75 6e 74 29 3b 0a 20 20 20  nPageCount);.   
13bf5 20 69 66 28 20 70 50 67 2d 3e 70 67 6e 6f 3e 6e   if( pPg->pgno>n
13bf6 50 61 67 65 43 6f 75 6e 74 20 29 7b 0a 20 20 20  PageCount ){.   
13bf7 20 20 20 6e 50 61 67 65 20 3d 20 28 70 50 67 2d     nPage = (pPg-
13bf8 3e 70 67 6e 6f 20 2d 20 70 67 31 29 2b 31 3b 0a  >pgno - pg1)+1;.
13bf9 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70      }else if( (p
13bfa 67 31 2b 6e 50 61 67 65 50 65 72 53 65 63 74 6f  g1+nPagePerSecto
13bfb 72 2d 31 29 3e 6e 50 61 67 65 43 6f 75 6e 74 20  r-1)>nPageCount 
13bfc 29 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 20 3d  ){.      nPage =
13bfd 20 6e 50 61 67 65 43 6f 75 6e 74 2b 31 2d 70 67   nPageCount+1-pg
13bfe 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  1;.    }else{.  
13bff 20 20 20 20 6e 50 61 67 65 20 3d 20 6e 50 61 67      nPage = nPag
13c00 65 50 65 72 53 65 63 74 6f 72 3b 0a 20 20 20 20  ePerSector;.    
13c01 7d 0a 20 20 20 20 61 73 73 65 72 74 28 6e 50 61  }.    assert(nPa
13c02 67 65 3e 30 29 3b 0a 20 20 20 20 61 73 73 65 72  ge>0);.    asser
13c03 74 28 70 67 31 3c 3d 70 50 67 2d 3e 70 67 6e 6f  t(pg1<=pPg->pgno
13c04 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 28 70  );.    assert((p
13c05 67 31 2b 6e 50 61 67 65 29 3e 70 50 67 2d 3e 70  g1+nPage)>pPg->p
13c06 67 6e 6f 29 3b 0a 0a 20 20 20 20 66 6f 72 28 69  gno);..    for(i
13c07 69 3d 30 3b 20 69 69 3c 6e 50 61 67 65 20 26 26  i=0; ii<nPage &&
13c08 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20   rc==SQLITE_OK; 
13c09 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 50 67 6e  ii++){.      Pgn
13c0a 6f 20 70 67 20 3d 20 70 67 31 2b 69 69 3b 0a 20  o pg = pg1+ii;. 
13c0b 20 20 20 20 20 50 67 48 64 72 20 2a 70 50 61 67       PgHdr *pPag
13c0c 65 3b 0a 20 20 20 20 20 20 69 66 28 20 70 67 3d  e;.      if( pg=
13c0d 3d 70 50 67 2d 3e 70 67 6e 6f 20 7c 7c 20 21 73  =pPg->pgno || !s
13c0e 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74  qlite3BitvecTest
13c0f 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72  (pPager->pInJour
13c10 6e 61 6c 2c 20 70 67 29 20 29 7b 0a 20 20 20 20  nal, pg) ){.    
13c11 20 20 20 20 69 66 28 20 70 67 21 3d 50 41 47 45      if( pg!=PAGE
13c12 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72  R_MJ_PGNO(pPager
13c13 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  ) ){.          r
13c14 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
13c15 47 65 74 28 70 50 61 67 65 72 2c 20 70 67 2c 20  Get(pPager, pg, 
13c16 26 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  &pPage);.       
13c17 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
13c18 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
13c19 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77      rc = pager_w
13c1a 72 69 74 65 28 70 50 61 67 65 29 3b 0a 20 20 20  rite(pPage);.   
13c1b 20 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61           if( pPa
13c1c 67 65 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f  ge->flags&PGHDR_
13c1d 4e 45 45 44 5f 53 59 4e 43 20 29 7b 0a 20 20 20  NEED_SYNC ){.   
13c1e 20 20 20 20 20 20 20 20 20 20 20 6e 65 65 64 53             needS
13c1f 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  ync = 1;.       
13c20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 70 50         assert(pP
13c21 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 29 3b  ager->needSync);
13c22 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
13c23 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
13c24 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 61  e3PagerUnref(pPa
13c25 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  ge);.          }
13c26 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
13c27 20 7d 65 6c 73 65 20 69 66 28 20 28 70 50 61 67   }else if( (pPag
13c28 65 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70  e = pager_lookup
13c29 28 70 50 61 67 65 72 2c 20 70 67 29 29 21 3d 30  (pPager, pg))!=0
13c2a 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
13c2b 70 50 61 67 65 2d 3e 66 6c 61 67 73 26 50 47 48  pPage->flags&PGH
13c2c 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b 0a  DR_NEED_SYNC ){.
13c2d 20 20 20 20 20 20 20 20 20 20 6e 65 65 64 53 79            needSy
13c2e 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  nc = 1;.        
13c2f 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  }.        sqlite
13c30 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 61 67  3PagerUnref(pPag
13c31 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  e);.      }.    
13c32 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  }..    /* If the
13c33 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43   PGHDR_NEED_SYNC
13c34 20 66 6c 61 67 20 69 73 20 73 65 74 20 66 6f 72   flag is set for
13c35 20 61 6e 79 20 6f 66 20 74 68 65 20 6e 50 61 67   any of the nPag
13c36 65 20 70 61 67 65 73 20 0a 20 20 20 20 2a 2a 20  e pages .    ** 
13c37 73 74 61 72 74 69 6e 67 20 61 74 20 70 67 31 2c  starting at pg1,
13c38 20 74 68 65 6e 20 69 74 20 6e 65 65 64 73 20 74   then it needs t
13c39 6f 20 62 65 20 73 65 74 20 66 6f 72 20 61 6c 6c  o be set for all
13c3a 20 6f 66 20 74 68 65 6d 2e 20 42 65 63 61 75 73   of them. Becaus
13c3b 65 0a 20 20 20 20 2a 2a 20 77 72 69 74 69 6e 67  e.    ** writing
13c3c 20 74 6f 20 61 6e 79 20 6f 66 20 74 68 65 73 65   to any of these
13c3d 20 6e 50 61 67 65 20 70 61 67 65 73 20 6d 61 79   nPage pages may
13c3e 20 64 61 6d 61 67 65 20 74 68 65 20 6f 74 68 65   damage the othe
13c3f 72 73 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 6a  rs, the.    ** j
13c40 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74  ournal file must
13c41 20 63 6f 6e 74 61 69 6e 20 73 79 6e 63 28 29 65   contain sync()e
13c42 64 20 63 6f 70 69 65 73 20 6f 66 20 61 6c 6c 20  d copies of all 
13c43 6f 66 20 74 68 65 6d 0a 20 20 20 20 2a 2a 20 62  of them.    ** b
13c44 65 66 6f 72 65 20 61 6e 79 20 6f 66 20 74 68 65  efore any of the
13c45 6d 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e  m can be written
13c46 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74 61   out to the data
13c47 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a  base file..    *
13c48 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  /.    if( rc==SQ
13c49 4c 49 54 45 5f 4f 4b 20 26 26 20 6e 65 65 64 53  LITE_OK && needS
13c4a 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 61 73 73  ync ){.      ass
13c4b 65 72 74 28 20 21 4d 45 4d 44 42 20 26 26 20 70  ert( !MEMDB && p
13c4c 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 3d 3d 30  Pager->noSync==0
13c4d 20 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 69   );.      for(ii
13c4e 3d 30 3b 20 69 69 3c 6e 50 61 67 65 3b 20 69 69  =0; ii<nPage; ii
13c4f 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 50 67 48  ++){.        PgH
13c50 64 72 20 2a 70 50 61 67 65 20 3d 20 70 61 67 65  dr *pPage = page
13c51 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c  r_lookup(pPager,
13c52 20 70 67 31 2b 69 69 29 3b 0a 20 20 20 20 20 20   pg1+ii);.      
13c53 20 20 69 66 28 20 70 50 61 67 65 20 29 7b 0a 20    if( pPage ){. 
13c54 20 20 20 20 20 20 20 20 20 70 50 61 67 65 2d 3e           pPage->
13c55 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 4e  flags |= PGHDR_N
13c56 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20 20 20  EED_SYNC;.      
13c57 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
13c58 55 6e 72 65 66 28 70 50 61 67 65 29 3b 0a 20 20  Unref(pPage);.  
13c59 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
13c5a 20 20 20 20 20 20 61 73 73 65 72 74 28 70 50 61        assert(pPa
13c5b 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 29 3b 0a  ger->needSync);.
13c5c 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73 65 72      }..    asser
13c5d 74 28 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74  t( pPager->doNot
13c5e 53 79 6e 63 3d 3d 31 20 29 3b 0a 20 20 20 20 70  Sync==1 );.    p
13c5f 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63  Pager->doNotSync
13c60 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 0;.  }else{. 
13c61 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 72     rc = pager_wr
13c62 69 74 65 28 70 44 62 50 61 67 65 29 3b 0a 20 20  ite(pDbPage);.  
13c63 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
13c64 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54  ../*.** Return T
13c65 52 55 45 20 69 66 20 74 68 65 20 70 61 67 65 20  RUE if the page 
13c66 67 69 76 65 6e 20 69 6e 20 74 68 65 20 61 72 67  given in the arg
13c67 75 6d 65 6e 74 20 77 61 73 20 70 72 65 76 69 6f  ument was previo
13c68 75 73 6c 79 20 70 61 73 73 65 64 0a 2a 2a 20 74  usly passed.** t
13c69 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  o sqlite3PagerWr
13c6a 69 74 65 28 29 2e 20 20 49 6e 20 6f 74 68 65 72  ite().  In other
13c6b 20 77 6f 72 64 73 2c 20 72 65 74 75 72 6e 20 54   words, return T
13c6c 52 55 45 20 69 66 20 69 74 20 69 73 20 6f 6b 0a  RUE if it is ok.
13c6d 2a 2a 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65  ** to change the
13c6e 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20   content of the 
13c6f 70 61 67 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  page..*/.#ifndef
13c70 20 4e 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50   NDEBUG.SQLITE_P
13c71 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
13c72 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
13c73 6c 65 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b  le(DbPage *pPg){
13c74 0a 20 20 72 65 74 75 72 6e 20 70 50 67 2d 3e 66  .  return pPg->f
13c75 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59  lags&PGHDR_DIRTY
13c76 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
13c77 2a 20 41 20 63 61 6c 6c 20 74 6f 20 74 68 69 73  * A call to this
13c78 20 72 6f 75 74 69 6e 65 20 74 65 6c 6c 73 20 74   routine tells t
13c79 68 65 20 70 61 67 65 72 20 74 68 61 74 20 69 74  he pager that it
13c7a 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72   is not necessar
13c7b 79 20 74 6f 0a 2a 2a 20 77 72 69 74 65 20 74 68  y to.** write th
13c7c 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e  e information on
13c7d 20 70 61 67 65 20 70 50 67 20 62 61 63 6b 20 74   page pPg back t
13c7e 6f 20 74 68 65 20 64 69 73 6b 2c 20 65 76 65 6e  o the disk, even
13c7f 20 74 68 6f 75 67 68 0a 2a 2a 20 74 68 61 74 20   though.** that 
13c80 70 61 67 65 20 6d 69 67 68 74 20 62 65 20 6d 61  page might be ma
13c81 72 6b 65 64 20 61 73 20 64 69 72 74 79 2e 20 20  rked as dirty.  
13c82 54 68 69 73 20 68 61 70 70 65 6e 73 2c 20 66 6f  This happens, fo
13c83 72 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 0a  r example, when.
13c84 2a 2a 20 74 68 65 20 70 61 67 65 20 68 61 73 20  ** the page has 
13c85 62 65 65 6e 20 61 64 64 65 64 20 61 73 20 61 20  been added as a 
13c86 6c 65 61 66 20 6f 66 20 74 68 65 20 66 72 65 65  leaf of the free
13c87 6c 69 73 74 20 61 6e 64 20 73 6f 20 69 74 73 0a  list and so its.
13c88 2a 2a 20 63 6f 6e 74 65 6e 74 20 6e 6f 20 6c 6f  ** content no lo
13c89 6e 67 65 72 20 6d 61 74 74 65 72 73 2e 0a 2a 2a  nger matters..**
13c8a 0a 2a 2a 20 54 68 65 20 6f 76 65 72 6c 79 69 6e  .** The overlyin
13c8b 67 20 73 6f 66 74 77 61 72 65 20 6c 61 79 65 72  g software layer
13c8c 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74   calls this rout
13c8d 69 6e 65 20 77 68 65 6e 20 61 6c 6c 20 6f 66 20  ine when all of 
13c8e 74 68 65 20 64 61 74 61 0a 2a 2a 20 6f 6e 20 74  the data.** on t
13c8f 68 65 20 67 69 76 65 6e 20 70 61 67 65 20 69 73  he given page is
13c90 20 75 6e 75 73 65 64 2e 20 54 68 65 20 70 61 67   unused. The pag
13c91 65 72 20 6d 61 72 6b 73 20 74 68 65 20 70 61 67  er marks the pag
13c92 65 20 61 73 20 63 6c 65 61 6e 20 73 6f 0a 2a 2a  e as clean so.**
13c93 20 74 68 61 74 20 69 74 20 64 6f 65 73 20 6e 6f   that it does no
13c94 74 20 67 65 74 20 77 72 69 74 74 65 6e 20 74 6f  t get written to
13c95 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 54 65 73   disk..**.** Tes
13c96 74 73 20 73 68 6f 77 20 74 68 61 74 20 74 68 69  ts show that thi
13c97 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 63  s optimization c
13c98 61 6e 20 71 75 61 64 72 75 70 6c 65 20 74 68 65  an quadruple the
13c99 20 73 70 65 65 64 20 6f 66 20 6c 61 72 67 65 20   speed of large 
13c9a 0a 2a 2a 20 44 45 4c 45 54 45 20 6f 70 65 72 61  .** DELETE opera
13c9b 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  tions..*/.SQLITE
13c9c 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
13c9d 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 57 72  lite3PagerDontWr
13c9e 69 74 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b  ite(PgHdr *pPg){
13c9f 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
13ca0 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a   = pPg->pPager;.
13ca1 20 20 69 66 28 20 28 70 50 67 2d 3e 66 6c 61 67    if( (pPg->flag
13ca2 73 26 50 47 48 44 52 5f 44 49 52 54 59 29 20 26  s&PGHDR_DIRTY) &
13ca3 26 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70  & pPager->nSavep
13ca4 6f 69 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 50  oint==0 ){.    P
13ca5 41 47 45 52 54 52 41 43 45 28 28 22 44 4f 4e 54  AGERTRACE(("DONT
13ca6 5f 57 52 49 54 45 20 70 61 67 65 20 25 64 20 6f  _WRITE page %d o
13ca7 66 20 25 64 5c 6e 22 2c 20 70 50 67 2d 3e 70 67  f %d\n", pPg->pg
13ca8 6e 6f 2c 20 50 41 47 45 52 49 44 28 70 50 61 67  no, PAGERID(pPag
13ca9 65 72 29 29 29 3b 0a 20 20 20 20 49 4f 54 52 41  er)));.    IOTRA
13caa 43 45 28 28 22 43 4c 45 41 4e 20 25 70 20 25 64  CE(("CLEAN %p %d
13cab 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 67  \n", pPager, pPg
13cac 2d 3e 70 67 6e 6f 29 29 0a 20 20 20 20 70 50 67  ->pgno)).    pPg
13cad 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52  ->flags |= PGHDR
13cae 5f 44 4f 4e 54 5f 57 52 49 54 45 3b 0a 23 69 66  _DONT_WRITE;.#if
13caf 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b  def SQLITE_CHECK
13cb0 5f 50 41 47 45 53 0a 20 20 20 20 70 50 67 2d 3e  _PAGES.    pPg->
13cb1 70 61 67 65 48 61 73 68 20 3d 20 70 61 67 65 72  pageHash = pager
13cb2 5f 70 61 67 65 68 61 73 68 28 70 50 67 29 3b 0a  _pagehash(pPg);.
13cb3 23 65 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a 2f 2a  #endif.  }.}../*
13cb4 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
13cb5 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 69 6e   is called to in
13cb6 63 72 65 6d 65 6e 74 20 74 68 65 20 76 61 6c 75  crement the valu
13cb7 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
13cb8 65 20 66 69 6c 65 20 0a 2a 2a 20 63 68 61 6e 67  e file .** chang
13cb9 65 2d 63 6f 75 6e 74 65 72 2c 20 73 74 6f 72 65  e-counter, store
13cba 64 20 61 73 20 61 20 34 2d 62 79 74 65 20 62 69  d as a 4-byte bi
13cbb 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72  g-endian integer
13cbc 20 73 74 61 72 74 69 6e 67 20 61 74 20 0a 2a 2a   starting at .**
13cbd 20 62 79 74 65 20 6f 66 66 73 65 74 20 32 34 20   byte offset 24 
13cbe 6f 66 20 74 68 65 20 70 61 67 65 72 20 66 69 6c  of the pager fil
13cbf 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
13cc0 69 73 44 69 72 65 63 74 4d 6f 64 65 20 66 6c 61  isDirectMode fla
13cc1 67 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20  g is zero, then 
13cc2 74 68 69 73 20 69 73 20 64 6f 6e 65 20 62 79 20  this is done by 
13cc3 63 61 6c 6c 69 6e 67 20 0a 2a 2a 20 73 71 6c 69  calling .** sqli
13cc4 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20  te3PagerWrite() 
13cc5 6f 6e 20 70 61 67 65 20 31 2c 20 74 68 65 6e 20  on page 1, then 
13cc6 6d 6f 64 69 66 79 69 6e 67 20 74 68 65 20 63 6f  modifying the co
13cc7 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 0a 2a 2a  ntents of the.**
13cc8 20 70 61 67 65 20 64 61 74 61 2e 20 49 6e 20 74   page data. In t
13cc9 68 69 73 20 63 61 73 65 20 74 68 65 20 66 69 6c  his case the fil
13cca 65 20 77 69 6c 6c 20 62 65 20 75 70 64 61 74 65  e will be update
13ccb 64 20 77 68 65 6e 20 74 68 65 20 63 75 72 72 65  d when the curre
13ccc 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f  nt.** transactio
13ccd 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 2e 0a  n is committed..
13cce 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 44 69 72 65  **.** The isDire
13ccf 63 74 4d 6f 64 65 20 66 6c 61 67 20 6d 61 79 20  ctMode flag may 
13cd0 6f 6e 6c 79 20 62 65 20 6e 6f 6e 2d 7a 65 72 6f  only be non-zero
13cd1 20 69 66 20 74 68 65 20 6c 69 62 72 61 72 79 20   if the library 
13cd2 77 61 73 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20  was compiled.** 
13cd3 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f  with the SQLITE_
13cd4 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52  ENABLE_ATOMIC_WR
13cd5 49 54 45 20 6d 61 63 72 6f 20 64 65 66 69 6e 65  ITE macro define
13cd6 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c  d. In this case,
13cd7 0a 2a 2a 20 69 66 20 69 73 44 69 72 65 63 74 20  .** if isDirect 
13cd8 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  is non-zero, the
13cd9 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
13cda 69 6c 65 20 69 73 20 75 70 64 61 74 65 64 20 64  ile is updated d
13cdb 69 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 77 72  irectly.** by wr
13cdc 69 74 69 6e 67 20 61 6e 20 75 70 64 61 74 65 64  iting an updated
13cdd 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 61 67 65   version of page
13cde 20 31 20 75 73 69 6e 67 20 61 20 63 61 6c 6c 20   1 using a call 
13cdf 74 6f 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74  to the .** sqlit
13ce0 65 33 4f 73 57 72 69 74 65 28 29 20 66 75 6e 63  e3OsWrite() func
13ce1 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
13ce2 69 6e 74 20 70 61 67 65 72 5f 69 6e 63 72 5f 63  int pager_incr_c
13ce3 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 50 61 67  hangecounter(Pag
13ce4 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20  er *pPager, int 
13ce5 69 73 44 69 72 65 63 74 4d 6f 64 65 29 7b 0a 20  isDirectMode){. 
13ce6 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
13ce7 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 44 65 63 6c 61  _OK;..  /* Decla
13ce8 72 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a  re and initializ
13ce9 65 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65 67  e constant integ
13cea 65 72 20 27 69 73 44 69 72 65 63 74 27 2e 20 49  er 'isDirect'. I
13ceb 66 20 74 68 65 0a 20 20 2a 2a 20 61 74 6f 6d 69  f the.  ** atomi
13cec 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61  c-write optimiza
13ced 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 20  tion is enabled 
13cee 69 6e 20 74 68 69 73 20 62 75 69 6c 64 2c 20 74  in this build, t
13cef 68 65 6e 20 69 73 44 69 72 65 63 74 0a 20 20 2a  hen isDirect.  *
13cf0 2a 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64  * is initialized
13cf1 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 70 61   to the value pa
13cf2 73 73 65 64 20 61 73 20 74 68 65 20 69 73 44 69  ssed as the isDi
13cf3 72 65 63 74 4d 6f 64 65 20 70 61 72 61 6d 65 74  rectMode paramet
13cf4 65 72 0a 20 20 2a 2a 20 74 6f 20 74 68 69 73 20  er.  ** to this 
13cf5 66 75 6e 63 74 69 6f 6e 2e 20 4f 74 68 65 72 77  function. Otherw
13cf6 69 73 65 2c 20 69 74 20 69 73 20 61 6c 77 61 79  ise, it is alway
13cf7 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 0a 20  s set to zero.. 
13cf8 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 69 64 65   **.  ** The ide
13cf9 61 20 69 73 20 74 68 61 74 20 69 66 20 74 68 65  a is that if the
13cfa 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70   atomic-write op
13cfb 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 6e 6f  timization is no
13cfc 74 0a 20 20 2a 2a 20 65 6e 61 62 6c 65 64 20 61  t.  ** enabled a
13cfd 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2c 20  t compile time, 
13cfe 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 61 6e  the compiler can
13cff 20 6f 6d 69 74 20 74 68 65 20 74 65 73 74 73 20   omit the tests 
13d00 6f 66 0a 20 20 2a 2a 20 27 69 73 44 69 72 65 63  of.  ** 'isDirec
13d01 74 27 20 62 65 6c 6f 77 2c 20 61 73 20 77 65 6c  t' below, as wel
13d02 6c 20 61 73 20 74 68 65 20 62 6c 6f 63 6b 20 65  l as the block e
13d03 6e 63 6c 6f 73 65 64 20 69 6e 20 74 68 65 0a 20  nclosed in the. 
13d04 20 2a 2a 20 22 69 66 28 20 69 73 44 69 72 65 63   ** "if( isDirec
13d05 74 20 29 22 20 63 6f 6e 64 69 74 69 6f 6e 2e 0a  t )" condition..
13d06 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c    */.#ifndef SQL
13d07 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49  ITE_ENABLE_ATOMI
13d08 43 5f 57 52 49 54 45 0a 23 20 64 65 66 69 6e 65  C_WRITE.# define
13d09 20 44 49 52 45 43 54 5f 4d 4f 44 45 20 30 0a 20   DIRECT_MODE 0. 
13d0a 20 61 73 73 65 72 74 28 20 69 73 44 69 72 65 63   assert( isDirec
13d0b 74 4d 6f 64 65 3d 3d 30 20 29 3b 0a 20 20 55 4e  tMode==0 );.  UN
13d0c 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69  USED_PARAMETER(i
13d0d 73 44 69 72 65 63 74 4d 6f 64 65 29 3b 0a 23 65  sDirectMode);.#e
13d0e 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 44 49 52  lse.# define DIR
13d0f 45 43 54 5f 4d 4f 44 45 20 69 73 44 69 72 65 63  ECT_MODE isDirec
13d10 74 4d 6f 64 65 0a 23 65 6e 64 69 66 0a 0a 20 20  tMode.#endif..  
13d11 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
13d12 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53  state>=PAGER_RES
13d13 45 52 56 45 44 20 29 3b 0a 20 20 69 66 28 20 21  ERVED );.  if( !
13d14 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f  pPager->changeCo
13d15 75 6e 74 44 6f 6e 65 20 26 26 20 41 4c 57 41 59  untDone && ALWAY
13d16 53 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  S(pPager->dbSize
13d17 3e 30 29 20 29 7b 0a 20 20 20 20 50 67 48 64 72  >0) ){.    PgHdr
13d18 20 2a 70 50 67 48 64 72 3b 20 20 20 20 20 20 20   *pPgHdr;       
13d19 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 66 65           /* Refe
13d1a 72 65 6e 63 65 20 74 6f 20 70 61 67 65 20 31 20  rence to page 1 
13d1b 2a 2f 0a 20 20 20 20 75 33 32 20 63 68 61 6e 67  */.    u32 chang
13d1c 65 5f 63 6f 75 6e 74 65 72 3b 20 20 20 20 20 20  e_counter;      
13d1d 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20       /* Initial 
13d1e 76 61 6c 75 65 20 6f 66 20 63 68 61 6e 67 65 2d  value of change-
13d1f 63 6f 75 6e 74 65 72 20 66 69 65 6c 64 20 2a 2f  counter field */
13d20 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70  ..    assert( !p
13d21 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20  Pager->tempFile 
13d22 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72  && isOpen(pPager
13d23 2d 3e 66 64 29 20 29 3b 0a 0a 20 20 20 20 2f 2a  ->fd) );..    /*
13d24 20 4f 70 65 6e 20 70 61 67 65 20 31 20 6f 66 20   Open page 1 of 
13d25 74 68 65 20 66 69 6c 65 20 66 6f 72 20 77 72 69  the file for wri
13d26 74 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 72 63 20  ting. */.    rc 
13d27 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  = sqlite3PagerGe
13d28 74 28 70 50 61 67 65 72 2c 20 31 2c 20 26 70 50  t(pPager, 1, &pP
13d29 67 48 64 72 29 3b 0a 20 20 20 20 61 73 73 65 72  gHdr);.    asser
13d2a 74 28 20 70 50 67 48 64 72 3d 3d 30 20 7c 7c 20  t( pPgHdr==0 || 
13d2b 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc==SQLITE_OK );
13d2c 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70 61 67 65  ..    /* If page
13d2d 20 6f 6e 65 20 77 61 73 20 66 65 74 63 68 65 64   one was fetched
13d2e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 61   successfully, a
13d2f 6e 64 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  nd this function
13d30 20 69 73 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 6f   is not.    ** o
13d31 70 65 72 61 74 69 6e 67 20 69 6e 20 64 69 72 65  perating in dire
13d32 63 74 2d 6d 6f 64 65 2c 20 6d 61 6b 65 20 70 61  ct-mode, make pa
13d33 67 65 20 31 20 77 72 69 74 61 62 6c 65 2e 20 20  ge 1 writable.  
13d34 57 68 65 6e 20 6e 6f 74 20 69 6e 20 0a 20 20 20  When not in .   
13d35 20 2a 2a 20 64 69 72 65 63 74 20 6d 6f 64 65 2c   ** direct mode,
13d36 20 70 61 67 65 20 31 20 69 73 20 61 6c 77 61 79   page 1 is alway
13d37 73 20 68 65 6c 64 20 69 6e 20 63 61 63 68 65 20  s held in cache 
13d38 61 6e 64 20 68 65 6e 63 65 20 74 68 65 20 50 61  and hence the Pa
13d39 67 65 72 47 65 74 28 29 0a 20 20 20 20 2a 2a 20  gerGet().    ** 
13d3a 61 62 6f 76 65 20 69 73 20 61 6c 77 61 79 73 20  above is always 
13d3b 73 75 63 63 65 73 73 66 75 6c 20 2d 20 68 65 6e  successful - hen
13d3c 63 65 20 74 68 65 20 41 4c 57 41 59 53 20 6f 6e  ce the ALWAYS on
13d3d 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 2e 0a   rc==SQLITE_OK..
13d3e 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21      */.    if( !
13d3f 44 49 52 45 43 54 5f 4d 4f 44 45 20 26 26 20 41  DIRECT_MODE && A
13d40 4c 57 41 59 53 28 72 63 3d 3d 53 51 4c 49 54 45  LWAYS(rc==SQLITE
13d41 5f 4f 4b 29 20 29 7b 0a 20 20 20 20 20 20 72 63  _OK) ){.      rc
13d42 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57   = sqlite3PagerW
13d43 72 69 74 65 28 70 50 67 48 64 72 29 3b 0a 20 20  rite(pPgHdr);.  
13d44 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63 3d    }..    if( rc=
13d45 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
13d46 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74      /* Increment
13d47 20 74 68 65 20 76 61 6c 75 65 20 6a 75 73 74 20   the value just 
13d48 72 65 61 64 20 61 6e 64 20 77 72 69 74 65 20 69  read and write i
13d49 74 20 62 61 63 6b 20 74 6f 20 62 79 74 65 20 32  t back to byte 2
13d4a 34 2e 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 6e  4. */.      chan
13d4b 67 65 5f 63 6f 75 6e 74 65 72 20 3d 20 73 71 6c  ge_counter = sql
13d4c 69 74 65 33 47 65 74 34 62 79 74 65 28 28 75 38  ite3Get4byte((u8
13d4d 2a 29 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65  *)pPager->dbFile
13d4e 56 65 72 73 29 3b 0a 20 20 20 20 20 20 63 68 61  Vers);.      cha
13d4f 6e 67 65 5f 63 6f 75 6e 74 65 72 2b 2b 3b 0a 20  nge_counter++;. 
13d50 20 20 20 20 20 70 75 74 33 32 62 69 74 73 28 28       put32bits((
13d51 28 63 68 61 72 2a 29 70 50 67 48 64 72 2d 3e 70  (char*)pPgHdr->p
13d52 44 61 74 61 29 2b 32 34 2c 20 63 68 61 6e 67 65  Data)+24, change
13d53 5f 63 6f 75 6e 74 65 72 29 3b 0a 0a 20 20 20 20  _counter);..    
13d54 20 20 2f 2a 20 49 66 20 72 75 6e 6e 69 6e 67 20    /* If running 
13d55 69 6e 20 64 69 72 65 63 74 20 6d 6f 64 65 2c 20  in direct mode, 
13d56 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e  write the conten
13d57 74 73 20 6f 66 20 70 61 67 65 20 31 20 74 6f 20  ts of page 1 to 
13d58 74 68 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20  the file. */.   
13d59 20 20 20 69 66 28 20 44 49 52 45 43 54 5f 4d 4f     if( DIRECT_MO
13d5a 44 45 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f  DE ){.        co
13d5b 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66 20 3d  nst void *zBuf =
13d5c 20 70 50 67 48 64 72 2d 3e 70 44 61 74 61 3b 0a   pPgHdr->pData;.
13d5d 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
13d5e 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69  pPager->dbFileSi
13d5f 7a 65 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20  ze>0 );.        
13d60 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72  rc = sqlite3OsWr
13d61 69 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20  ite(pPager->fd, 
13d62 7a 42 75 66 2c 20 70 50 61 67 65 72 2d 3e 70 61  zBuf, pPager->pa
13d63 67 65 53 69 7a 65 2c 20 30 29 3b 0a 20 20 20 20  geSize, 0);.    
13d64 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
13d65 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
13d66 20 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67     pPager->chang
13d67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 31 3b 0a  eCountDone = 1;.
13d68 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
13d69 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70  }else{.        p
13d6a 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75  Pager->changeCou
13d6b 6e 74 44 6f 6e 65 20 3d 20 31 3b 0a 20 20 20 20  ntDone = 1;.    
13d6c 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f    }.    }..    /
13d6d 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20 70 61  * Release the pa
13d6e 67 65 20 72 65 66 65 72 65 6e 63 65 2e 20 2a 2f  ge reference. */
13d6f 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65  .    sqlite3Page
13d70 72 55 6e 72 65 66 28 70 50 67 48 64 72 29 3b 0a  rUnref(pPgHdr);.
13d71 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
13d72 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74  .}../*.** Sync t
13d73 68 65 20 70 61 67 65 72 20 66 69 6c 65 20 74 6f  he pager file to
13d74 20 64 69 73 6b 2e 20 54 68 69 73 20 69 73 20 61   disk. This is a
13d75 20 6e 6f 2d 6f 70 20 66 6f 72 20 69 6e 2d 6d 65   no-op for in-me
13d76 6d 6f 72 79 20 66 69 6c 65 73 0a 2a 2a 20 6f 72  mory files.** or
13d77 20 70 61 67 65 73 20 77 69 74 68 20 74 68 65 20   pages with the 
13d78 50 61 67 65 72 2e 6e 6f 53 79 6e 63 20 66 6c 61  Pager.noSync fla
13d79 67 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  g set..**.** If 
13d7a 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 72 20 63  successful, or c
13d7b 61 6c 6c 65 64 20 6f 6e 20 61 20 70 61 67 65 72  alled on a pager
13d7c 20 66 6f 72 20 77 68 69 63 68 20 69 74 20 69 73   for which it is
13d7d 20 61 20 6e 6f 2d 6f 70 2c 20 74 68 69 73 0a 2a   a no-op, this.*
13d7e 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  * function retur
13d7f 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 4f 74  ns SQLITE_OK. Ot
13d80 68 65 72 77 69 73 65 2c 20 61 6e 20 49 4f 20 65  herwise, an IO e
13d81 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74  rror code is ret
13d82 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  urned..*/.SQLITE
13d83 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
13d84 69 74 65 33 50 61 67 65 72 53 79 6e 63 28 50 61  ite3PagerSync(Pa
13d85 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
13d86 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
13d87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13d88 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63       /* Return c
13d89 6f 64 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ode */.  assert(
13d8a 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 69 66 28   !MEMDB );.  if(
13d8b 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20   pPager->noSync 
13d8c 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
13d8d 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a  TE_OK;.  }else{.
13d8e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
13d8f 4f 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 66  OsSync(pPager->f
13d90 64 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f  d, pPager->sync_
13d91 66 6c 61 67 73 29 3b 0a 20 20 7d 0a 20 20 72 65  flags);.  }.  re
13d92 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
13d93 2a 20 53 79 6e 63 20 74 68 65 20 64 61 74 61 62  * Sync the datab
13d94 61 73 65 20 66 69 6c 65 20 66 6f 72 20 74 68 65  ase file for the
13d95 20 70 61 67 65 72 20 70 50 61 67 65 72 2e 20 7a   pager pPager. z
13d96 4d 61 73 74 65 72 20 70 6f 69 6e 74 73 20 74 6f  Master points to
13d97 20 74 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20   the name.** of 
13d98 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  a master journal
13d99 20 66 69 6c 65 20 74 68 61 74 20 73 68 6f 75 6c   file that shoul
13d9a 64 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74  d be written int
13d9b 6f 20 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c  o the individual
13d9c 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  .** journal file
13d9d 2e 20 7a 4d 61 73 74 65 72 20 6d 61 79 20 62 65  . zMaster may be
13d9e 20 4e 55 4c 4c 2c 20 77 68 69 63 68 20 69 73 20   NULL, which is 
13d9f 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 6e  interpreted as n
13da0 6f 20 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f 75 72  o master.** jour
13da1 6e 61 6c 20 28 61 20 73 69 6e 67 6c 65 20 64 61  nal (a single da
13da2 74 61 62 61 73 65 20 74 72 61 6e 73 61 63 74 69  tabase transacti
13da3 6f 6e 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  on)..**.** This 
13da4 72 6f 75 74 69 6e 65 20 65 6e 73 75 72 65 73 20  routine ensures 
13da5 74 68 61 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20  that:.**.**   * 
13da6 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  The database fil
13da7 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72  e change-counter
13da8 20 69 73 20 75 70 64 61 74 65 64 2c 0a 2a 2a 20   is updated,.** 
13da9 20 20 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20    * the journal 
13daa 69 73 20 73 79 6e 63 65 64 20 28 75 6e 6c 65 73  is synced (unles
13dab 73 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69  s the atomic-wri
13dac 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20  te optimization 
13dad 69 73 20 75 73 65 64 29 2c 0a 2a 2a 20 20 20 2a  is used),.**   *
13dae 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73   all dirty pages
13daf 20 61 72 65 20 77 72 69 74 74 65 6e 20 74 6f 20   are written to 
13db0 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
13db1 65 2c 20 0a 2a 2a 20 20 20 2a 20 74 68 65 20 64  e, .**   * the d
13db2 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20  atabase file is 
13db3 74 72 75 6e 63 61 74 65 64 20 28 69 66 20 72 65  truncated (if re
13db4 71 75 69 72 65 64 29 2c 20 61 6e 64 0a 2a 2a 20  quired), and.** 
13db5 20 20 2a 20 74 68 65 20 64 61 74 61 62 61 73 65    * the database
13db6 20 66 69 6c 65 20 73 79 6e 63 65 64 2e 20 0a 2a   file synced. .*
13db7 2a 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 74 68  *.** The only th
13db8 69 6e 67 20 74 68 61 74 20 72 65 6d 61 69 6e 73  ing that remains
13db9 20 74 6f 20 63 6f 6d 6d 69 74 20 74 68 65 20 74   to commit the t
13dba 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 74 6f  ransaction is to
13dbb 20 66 69 6e 61 6c 69 7a 65 20 0a 2a 2a 20 28 64   finalize .** (d
13dbc 65 6c 65 74 65 2c 20 74 72 75 6e 63 61 74 65 20  elete, truncate 
13dbd 6f 72 20 7a 65 72 6f 20 74 68 65 20 66 69 72 73  or zero the firs
13dbe 74 20 70 61 72 74 20 6f 66 29 20 74 68 65 20 6a  t part of) the j
13dbf 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 6f 72 20  ournal file (or 
13dc0 0a 2a 2a 20 64 65 6c 65 74 65 20 74 68 65 20 6d  .** delete the m
13dc1 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
13dc2 6c 65 20 69 66 20 73 70 65 63 69 66 69 65 64 29  le if specified)
13dc3 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ..**.** Note tha
13dc4 74 20 69 66 20 7a 4d 61 73 74 65 72 3d 3d 4e 55  t if zMaster==NU
13dc5 4c 4c 2c 20 74 68 69 73 20 64 6f 65 73 20 6e 6f  LL, this does no
13dc6 74 20 6f 76 65 72 77 72 69 74 65 20 61 20 70 72  t overwrite a pr
13dc7 65 76 69 6f 75 73 20 76 61 6c 75 65 0a 2a 2a 20  evious value.** 
13dc8 70 61 73 73 65 64 20 74 6f 20 61 6e 20 73 71 6c  passed to an sql
13dc9 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50  ite3PagerCommitP
13dca 68 61 73 65 4f 6e 65 28 29 20 63 61 6c 6c 2e 0a  haseOne() call..
13dcb 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 6e  **.** If the fin
13dcc 61 6c 20 70 61 72 61 6d 65 74 65 72 20 2d 20 6e  al parameter - n
13dcd 6f 53 79 6e 63 20 2d 20 69 73 20 74 72 75 65 2c  oSync - is true,
13dce 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 62 61   then the databa
13dcf 73 65 20 66 69 6c 65 20 69 74 73 65 6c 66 0a 2a  se file itself.*
13dd0 2a 20 69 73 20 6e 6f 74 20 73 79 6e 63 65 64 2e  * is not synced.
13dd1 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74   The caller must
13dd2 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 50 61 67   call sqlite3Pag
13dd3 65 72 53 79 6e 63 28 29 20 64 69 72 65 63 74 6c  erSync() directl
13dd4 79 20 74 6f 0a 2a 2a 20 73 79 6e 63 20 74 68 65  y to.** sync the
13dd5 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 62   database file b
13dd6 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 43 6f  efore calling Co
13dd7 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 74  mmitPhaseTwo() t
13dd8 6f 20 64 65 6c 65 74 65 20 74 68 65 0a 2a 2a 20  o delete the.** 
13dd9 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20  journal file in 
13dda 74 68 69 73 20 63 61 73 65 2e 0a 2a 2f 0a 53 51  this case..*/.SQ
13ddb 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
13ddc 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d   sqlite3PagerCom
13ddd 6d 69 74 50 68 61 73 65 4f 6e 65 28 0a 20 20 50  mitPhaseOne(.  P
13dde 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20  ager *pPager,   
13ddf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
13de0 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 2a  * Pager object *
13de1 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
13de2 7a 4d 61 73 74 65 72 2c 20 20 20 20 20 20 20 20  zMaster,        
13de3 20 20 20 20 2f 2a 20 49 66 20 6e 6f 74 20 4e 55      /* If not NU
13de4 4c 4c 2c 20 74 68 65 20 6d 61 73 74 65 72 20 6a  LL, the master j
13de5 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20  ournal name */. 
13de6 20 69 6e 74 20 6e 6f 53 79 6e 63 20 20 20 20 20   int noSync     
13de7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13de8 20 2f 2a 20 54 72 75 65 20 74 6f 20 6f 6d 69 74   /* True to omit
13de9 20 74 68 65 20 78 53 79 6e 63 20 6f 6e 20 74 68   the xSync on th
13dea 65 20 64 62 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a  e db file */.){.
13deb 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
13dec 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20  E_OK;           
13ded 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
13dee 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 64 62   */..  /* The db
13def 4f 72 69 67 53 69 7a 65 20 69 73 20 6e 65 76 65  OrigSize is neve
13df0 72 20 73 65 74 20 69 66 20 6a 6f 75 72 6e 61 6c  r set if journal
13df1 5f 6d 6f 64 65 3d 4f 46 46 20 2a 2f 0a 20 20 61  _mode=OFF */.  a
13df2 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a  ssert( pPager->j
13df3 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45  ournalMode!=PAGE
13df4 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46  R_JOURNALMODE_OF
13df5 46 20 7c 7c 20 70 50 61 67 65 72 2d 3e 64 62 4f  F || pPager->dbO
13df6 72 69 67 53 69 7a 65 3d 3d 30 20 29 3b 0a 0a 20  rigSize==0 );.. 
13df7 20 2f 2a 20 49 66 20 61 20 70 72 69 6f 72 20 65   /* If a prior e
13df8 72 72 6f 72 20 6f 63 63 75 72 72 65 64 2c 20 74  rror occurred, t
13df9 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75  his routine shou
13dfa 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64  ld not be called
13dfb 2e 20 20 52 4f 4c 4c 42 41 43 4b 0a 20 20 2a 2a  .  ROLLBACK.  **
13dfc 20 69 73 20 74 68 65 20 61 70 70 72 6f 70 72 69   is the appropri
13dfd 61 74 65 20 72 65 73 70 6f 6e 73 65 20 74 6f 20  ate response to 
13dfe 61 6e 20 65 72 72 6f 72 2c 20 6e 6f 74 20 43 4f  an error, not CO
13dff 4d 4d 49 54 2e 20 20 47 75 61 72 64 20 61 67 61  MMIT.  Guard aga
13e00 69 6e 73 74 0a 20 20 2a 2a 20 63 6f 64 69 6e 67  inst.  ** coding
13e01 20 65 72 72 6f 72 73 20 62 79 20 72 65 70 65 61   errors by repea
13e02 74 69 6e 67 20 74 68 65 20 70 72 69 6f 72 20 65  ting the prior e
13e03 72 72 6f 72 2e 20 2a 2f 0a 20 20 69 66 28 20 4e  rror. */.  if( N
13e04 45 56 45 52 28 70 50 61 67 65 72 2d 3e 65 72 72  EVER(pPager->err
13e05 43 6f 64 65 29 20 29 20 72 65 74 75 72 6e 20 70  Code) ) return p
13e06 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a  Pager->errCode;.
13e07 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22  .  PAGERTRACE(("
13e08 44 41 54 41 42 41 53 45 20 53 59 4e 43 3a 20 46  DATABASE SYNC: F
13e09 69 6c 65 3d 25 73 20 7a 4d 61 73 74 65 72 3d 25  ile=%s zMaster=%
13e0a 73 20 6e 53 69 7a 65 3d 25 64 5c 6e 22 2c 20 0a  s nSize=%d\n", .
13e0b 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 7a 46        pPager->zF
13e0c 69 6c 65 6e 61 6d 65 2c 20 7a 4d 61 73 74 65 72  ilename, zMaster
13e0d 2c 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  , pPager->dbSize
13e0e 29 29 3b 0a 0a 20 20 69 66 28 20 4d 45 4d 44 42  ));..  if( MEMDB
13e0f 20 26 26 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f   && pPager->dbMo
13e10 64 69 66 69 65 64 20 29 7b 0a 20 20 20 20 2f 2a  dified ){.    /*
13e11 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 69   If this is an i
13e12 6e 2d 6d 65 6d 6f 72 79 20 64 62 2c 20 6f 72 20  n-memory db, or 
13e13 6e 6f 20 70 61 67 65 73 20 68 61 76 65 20 62 65  no pages have be
13e14 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 2c 20 6f  en written to, o
13e15 72 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 66 75  r this.    ** fu
13e16 6e 63 74 69 6f 6e 20 68 61 73 20 61 6c 72 65 61  nction has alrea
13e17 64 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c 20  dy been called, 
13e18 69 74 20 69 73 20 6d 6f 73 74 6c 79 20 61 20 6e  it is mostly a n
13e19 6f 2d 6f 70 2e 20 20 48 6f 77 65 76 65 72 2c 20  o-op.  However, 
13e1a 61 6e 79 0a 20 20 20 20 2a 2a 20 62 61 63 6b 75  any.    ** backu
13e1b 70 20 69 6e 20 70 72 6f 67 72 65 73 73 20 6e 65  p in progress ne
13e1c 65 64 73 20 74 6f 20 62 65 20 72 65 73 74 61 72  eds to be restar
13e1d 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ted..    */.    
13e1e 73 71 6c 69 74 65 33 42 61 63 6b 75 70 52 65 73  sqlite3BackupRes
13e1f 74 61 72 74 28 70 50 61 67 65 72 2d 3e 70 42 61  tart(pPager->pBa
13e20 63 6b 75 70 29 3b 0a 20 20 7d 65 6c 73 65 20 69  ckup);.  }else i
13e21 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  f( pPager->state
13e22 21 3d 50 41 47 45 52 5f 53 59 4e 43 45 44 20 26  !=PAGER_SYNCED &
13e23 26 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69  & pPager->dbModi
13e24 66 69 65 64 20 29 7b 0a 0a 20 20 20 20 2f 2a 20  fied ){..    /* 
13e25 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c  The following bl
13e26 6f 63 6b 20 75 70 64 61 74 65 73 20 74 68 65 20  ock updates the 
13e27 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 2e 20  change-counter. 
13e28 45 78 61 63 74 6c 79 20 68 6f 77 20 69 74 0a 20  Exactly how it. 
13e29 20 20 20 2a 2a 20 64 6f 65 73 20 74 68 69 73 20     ** does this 
13e2a 64 65 70 65 6e 64 73 20 6f 6e 20 77 68 65 74 68  depends on wheth
13e2b 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 61 74  er or not the at
13e2c 6f 6d 69 63 2d 75 70 64 61 74 65 20 6f 70 74 69  omic-update opti
13e2d 6d 69 7a 61 74 69 6f 6e 0a 20 20 20 20 2a 2a 20  mization.    ** 
13e2e 77 61 73 20 65 6e 61 62 6c 65 64 20 61 74 20 63  was enabled at c
13e2f 6f 6d 70 69 6c 65 20 74 69 6d 65 2c 20 61 6e 64  ompile time, and
13e30 20 69 66 20 74 68 69 73 20 74 72 61 6e 73 61 63   if this transac
13e31 74 69 6f 6e 20 6d 65 65 74 73 20 74 68 65 20 0a  tion meets the .
13e32 20 20 20 20 2a 2a 20 72 75 6e 74 69 6d 65 20 63      ** runtime c
13e33 72 69 74 65 72 69 61 20 74 6f 20 75 73 65 20 74  riteria to use t
13e34 68 65 20 6f 70 65 72 61 74 69 6f 6e 3a 20 0a 20  he operation: . 
13e35 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20     **.    **    
13e36 2a 20 54 68 65 20 66 69 6c 65 2d 73 79 73 74 65  * The file-syste
13e37 6d 20 73 75 70 70 6f 72 74 73 20 74 68 65 20 61  m supports the a
13e38 74 6f 6d 69 63 2d 77 72 69 74 65 20 70 72 6f 70  tomic-write prop
13e39 65 72 74 79 20 66 6f 72 0a 20 20 20 20 2a 2a 20  erty for.    ** 
13e3a 20 20 20 20 20 62 6c 6f 63 6b 73 20 6f 66 20 73       blocks of s
13e3b 69 7a 65 20 70 61 67 65 2d 73 69 7a 65 2c 20 61  ize page-size, a
13e3c 6e 64 20 0a 20 20 20 20 2a 2a 20 20 20 20 2a 20  nd .    **    * 
13e3d 54 68 69 73 20 63 6f 6d 6d 69 74 20 69 73 20 6e  This commit is n
13e3e 6f 74 20 70 61 72 74 20 6f 66 20 61 20 6d 75 6c  ot part of a mul
13e3f 74 69 2d 66 69 6c 65 20 74 72 61 6e 73 61 63 74  ti-file transact
13e40 69 6f 6e 2c 20 61 6e 64 0a 20 20 20 20 2a 2a 20  ion, and.    ** 
13e41 20 20 20 2a 20 45 78 61 63 74 6c 79 20 6f 6e 65     * Exactly one
13e42 20 70 61 67 65 20 68 61 73 20 62 65 65 6e 20 6d   page has been m
13e43 6f 64 69 66 69 65 64 20 61 6e 64 20 73 74 6f 72  odified and stor
13e44 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  e in the journal
13e45 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20   file..    **.  
13e46 20 20 2a 2a 20 49 66 20 74 68 65 20 6f 70 74 69    ** If the opti
13e47 6d 69 7a 61 74 69 6f 6e 20 77 61 73 20 6e 6f 74  mization was not
13e48 20 65 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d 70   enabled at comp
13e49 69 6c 65 20 74 69 6d 65 2c 20 74 68 65 6e 20 74  ile time, then t
13e4a 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 5f  he.    ** pager_
13e4b 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74  incr_changecount
13e4c 65 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73  er() function is
13e4d 20 63 61 6c 6c 65 64 20 74 6f 20 75 70 64 61 74   called to updat
13e4e 65 20 74 68 65 20 63 68 61 6e 67 65 0a 20 20 20  e the change.   
13e4f 20 2a 2a 20 63 6f 75 6e 74 65 72 20 69 6e 20 27   ** counter in '
13e50 69 6e 64 69 72 65 63 74 2d 6d 6f 64 65 27 2e 20  indirect-mode'. 
13e51 49 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74  If the optimizat
13e52 69 6f 6e 20 69 73 20 63 6f 6d 70 69 6c 65 64 20  ion is compiled 
13e53 69 6e 20 62 75 74 0a 20 20 20 20 2a 2a 20 69 73  in but.    ** is
13e54 20 6e 6f 74 20 61 70 70 6c 69 63 61 62 6c 65 20   not applicable 
13e55 74 6f 20 74 68 69 73 20 74 72 61 6e 73 61 63 74  to this transact
13e56 69 6f 6e 2c 20 63 61 6c 6c 20 73 71 6c 69 74 65  ion, call sqlite
13e57 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 29  3JournalCreate()
13e58 0a 20 20 20 20 2a 2a 20 74 6f 20 6d 61 6b 65 20  .    ** to make 
13e59 73 75 72 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c  sure the journal
13e5a 20 66 69 6c 65 20 68 61 73 20 61 63 74 75 61 6c   file has actual
13e5b 6c 79 20 62 65 65 6e 20 63 72 65 61 74 65 64 2c  ly been created,
13e5c 20 74 68 65 6e 20 63 61 6c 6c 0a 20 20 20 20 2a   then call.    *
13e5d 2a 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61  * pager_incr_cha
13e5e 6e 67 65 63 6f 75 6e 74 65 72 28 29 20 74 6f 20  ngecounter() to 
13e5f 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67  update the chang
13e60 65 2d 63 6f 75 6e 74 65 72 20 69 6e 20 69 6e 64  e-counter in ind
13e61 69 72 65 63 74 0a 20 20 20 20 2a 2a 20 6d 6f 64  irect.    ** mod
13e62 65 2e 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  e. .    **.    *
13e63 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20  * Otherwise, if 
13e64 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  the optimization
13e65 20 69 73 20 62 6f 74 68 20 65 6e 61 62 6c 65 64   is both enabled
13e66 20 61 6e 64 20 61 70 70 6c 69 63 61 62 6c 65 2c   and applicable,
13e67 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 63 61 6c  .    ** then cal
13e68 6c 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61  l pager_incr_cha
13e69 6e 67 65 63 6f 75 6e 74 65 72 28 29 20 74 6f 20  ngecounter() to 
13e6a 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67  update the chang
13e6b 65 2d 63 6f 75 6e 74 65 72 0a 20 20 20 20 2a 2a  e-counter.    **
13e6c 20 69 6e 20 27 64 69 72 65 63 74 27 20 6d 6f 64   in 'direct' mod
13e6d 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  e. In this case 
13e6e 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
13e6f 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 0a 20   will never be. 
13e70 20 20 20 2a 2a 20 63 72 65 61 74 65 64 20 66 6f     ** created fo
13e71 72 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69  r this transacti
13e72 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 64 65  on..    */.#ifde
13e73 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
13e74 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 20  ATOMIC_WRITE.   
13e75 20 50 67 48 64 72 20 2a 70 50 67 3b 0a 20 20 20   PgHdr *pPg;.   
13e76 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28   assert( isOpen(
13e77 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c 7c 20  pPager->jfd) || 
13e78 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
13e79 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
13e7a 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 3b 0a 20 20  ALMODE_OFF );.  
13e7b 20 20 69 66 28 20 21 7a 4d 61 73 74 65 72 20 26    if( !zMaster &
13e7c 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  & isOpen(pPager-
13e7d 3e 6a 66 64 29 20 0a 20 20 20 20 20 26 26 20 70  >jfd) .     && p
13e7e 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
13e7f 66 3d 3d 6a 72 6e 6c 42 75 66 66 65 72 53 69 7a  f==jrnlBufferSiz
13e80 65 28 70 50 61 67 65 72 29 20 0a 20 20 20 20 20  e(pPager) .     
13e81 26 26 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a  && pPager->dbSiz
13e82 65 3e 3d 70 50 61 67 65 72 2d 3e 64 62 46 69 6c  e>=pPager->dbFil
13e83 65 53 69 7a 65 0a 20 20 20 20 20 26 26 20 28 30  eSize.     && (0
13e84 3d 3d 28 70 50 67 20 3d 20 73 71 6c 69 74 65 33  ==(pPg = sqlite3
13e85 50 63 61 63 68 65 44 69 72 74 79 4c 69 73 74 28  PcacheDirtyList(
13e86 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29  pPager->pPCache)
13e87 29 20 7c 7c 20 30 3d 3d 70 50 67 2d 3e 70 44 69  ) || 0==pPg->pDi
13e88 72 74 79 29 0a 20 20 20 20 29 7b 0a 20 20 20 20  rty).    ){.    
13e89 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20    /* Update the 
13e8a 64 62 20 66 69 6c 65 20 63 68 61 6e 67 65 20 63  db file change c
13e8b 6f 75 6e 74 65 72 20 76 69 61 20 74 68 65 20 64  ounter via the d
13e8c 69 72 65 63 74 2d 77 72 69 74 65 20 6d 65 74 68  irect-write meth
13e8d 6f 64 2e 20 54 68 65 20 0a 20 20 20 20 20 20 2a  od. The .      *
13e8e 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c  * following call
13e8f 20 77 69 6c 6c 20 6d 6f 64 69 66 79 20 74 68 65   will modify the
13e90 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65   in-memory repre
13e91 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 70 61 67  sentation of pag
13e92 65 20 31 20 0a 20 20 20 20 20 20 2a 2a 20 74 6f  e 1 .      ** to
13e93 20 69 6e 63 6c 75 64 65 20 74 68 65 20 75 70 64   include the upd
13e94 61 74 65 64 20 63 68 61 6e 67 65 20 63 6f 75 6e  ated change coun
13e95 74 65 72 20 61 6e 64 20 74 68 65 6e 20 77 72 69  ter and then wri
13e96 74 65 20 70 61 67 65 20 31 20 0a 20 20 20 20 20  te page 1 .     
13e97 20 2a 2a 20 64 69 72 65 63 74 6c 79 20 74 6f 20   ** directly to 
13e98 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
13e99 65 2e 20 42 65 63 61 75 73 65 20 6f 66 20 74 68  e. Because of th
13e9a 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 0a  e atomic-write .
13e9b 20 20 20 20 20 20 2a 2a 20 70 72 6f 70 65 72 74        ** propert
13e9c 79 20 6f 66 20 74 68 65 20 68 6f 73 74 20 66 69  y of the host fi
13e9d 6c 65 2d 73 79 73 74 65 6d 2c 20 74 68 69 73 20  le-system, this 
13e9e 69 73 20 73 61 66 65 2e 0a 20 20 20 20 20 20 2a  is safe..      *
13e9f 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67  /.      rc = pag
13ea0 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f  er_incr_changeco
13ea1 75 6e 74 65 72 28 70 50 61 67 65 72 2c 20 31 29  unter(pPager, 1)
13ea2 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
13ea3 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4a     rc = sqlite3J
13ea4 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 70 50 61  ournalCreate(pPa
13ea5 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20  ger->jfd);.     
13ea6 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
13ea7 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  OK ){.        rc
13ea8 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68   = pager_incr_ch
13ea9 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50 61 67  angecounter(pPag
13eaa 65 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a  er, 0);.      }.
13eab 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20      }.#else.    
13eac 72 63 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f  rc = pager_incr_
13ead 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50  changecounter(pP
13eae 61 67 65 72 2c 20 30 29 3b 0a 23 65 6e 64 69 66  ager, 0);.#endif
13eaf 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
13eb0 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f  ITE_OK ) goto co
13eb1 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65  mmit_phase_one_e
13eb2 78 69 74 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20  xit;..    /* If 
13eb3 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e  this transaction
13eb4 20 68 61 73 20 6d 61 64 65 20 74 68 65 20 64 61   has made the da
13eb5 74 61 62 61 73 65 20 73 6d 61 6c 6c 65 72 2c 20  tabase smaller, 
13eb6 74 68 65 6e 20 61 6c 6c 20 70 61 67 65 73 0a 20  then all pages. 
13eb7 20 20 20 2a 2a 20 62 65 69 6e 67 20 64 69 73 63     ** being disc
13eb8 61 72 64 65 64 20 62 79 20 74 68 65 20 74 72 75  arded by the tru
13eb9 6e 63 61 74 69 6f 6e 20 6d 75 73 74 20 62 65 20  ncation must be 
13eba 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a  written to the j
13ebb 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 66 69  ournal.    ** fi
13ebc 6c 65 2e 20 54 68 69 73 20 63 61 6e 20 6f 6e 6c  le. This can onl
13ebd 79 20 68 61 70 70 65 6e 20 69 6e 20 61 75 74 6f  y happen in auto
13ebe 2d 76 61 63 75 75 6d 20 6d 6f 64 65 2e 0a 20 20  -vacuum mode..  
13ebf 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 42 65 66 6f    **.    ** Befo
13ec0 72 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 70  re reading the p
13ec1 61 67 65 73 20 77 69 74 68 20 70 61 67 65 20 6e  ages with page n
13ec2 75 6d 62 65 72 73 20 6c 61 72 67 65 72 20 74 68  umbers larger th
13ec3 61 6e 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 63  an the .    ** c
13ec4 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20  urrent value of 
13ec5 50 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 73 65  Pager.dbSize, se
13ec6 74 20 64 62 53 69 7a 65 20 62 61 63 6b 20 74 6f  t dbSize back to
13ec7 20 74 68 65 20 76 61 6c 75 65 0a 20 20 20 20 2a   the value.    *
13ec8 2a 20 74 68 61 74 20 69 74 20 74 6f 6f 6b 20 61  * that it took a
13ec9 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  t the start of t
13eca 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  he transaction. 
13ecb 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 0a 20  Otherwise, the. 
13ecc 20 20 20 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73     ** calls to s
13ecd 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29  qlite3PagerGet()
13ece 20 72 65 74 75 72 6e 20 7a 65 72 6f 65 64 20 70   return zeroed p
13ecf 61 67 65 73 20 69 6e 73 74 65 61 64 20 6f 66 20  ages instead of 
13ed0 0a 20 20 20 20 2a 2a 20 72 65 61 64 69 6e 67 20  .    ** reading 
13ed1 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 64 61  data from the da
13ed2 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20  tabase file..   
13ed3 20 2a 2a 0a 20 20 20 20 2a 2a 20 57 68 65 6e 20   **.    ** When 
13ed4 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 3d 4f 46  journal_mode==OF
13ed5 46 20 74 68 65 20 64 62 4f 72 69 67 53 69 7a 65  F the dbOrigSize
13ed6 20 69 73 20 61 6c 77 61 79 73 20 7a 65 72 6f 2c   is always zero,
13ed7 20 73 6f 20 74 68 69 73 0a 20 20 20 20 2a 2a 20   so this.    ** 
13ed8 62 6c 6f 63 6b 20 6e 65 76 65 72 20 72 75 6e 73  block never runs
13ed9 20 69 66 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65   if journal_mode
13eda 3d 4f 46 46 2e 0a 20 20 20 20 2a 2f 0a 23 69 66  =OFF..    */.#if
13edb 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
13edc 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20  _AUTOVACUUM.    
13edd 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69  if( pPager->dbSi
13ede 7a 65 3c 70 50 61 67 65 72 2d 3e 64 62 4f 72 69  ze<pPager->dbOri
13edf 67 53 69 7a 65 20 0a 20 20 20 20 20 26 26 20 41  gSize .     && A
13ee0 4c 57 41 59 53 28 70 50 61 67 65 72 2d 3e 6a 6f  LWAYS(pPager->jo
13ee1 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52  urnalMode!=PAGER
13ee2 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46  _JOURNALMODE_OFF
13ee3 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 50  ).    ){.      P
13ee4 67 6e 6f 20 69 3b 20 20 20 20 20 20 20 20 20 20  gno i;          
13ee5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13ee6 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72           /* Iter
13ee7 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f  ator variable */
13ee8 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 50 67 6e  .      const Pgn
13ee9 6f 20 69 53 6b 69 70 20 3d 20 50 41 47 45 52 5f  o iSkip = PAGER_
13eea 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 3b  MJ_PGNO(pPager);
13eeb 20 2f 2a 20 50 65 6e 64 69 6e 67 20 6c 6f 63 6b   /* Pending lock
13eec 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 63   page */.      c
13eed 6f 6e 73 74 20 50 67 6e 6f 20 64 62 53 69 7a 65  onst Pgno dbSize
13eee 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a   = pPager->dbSiz
13eef 65 3b 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61  e;       /* Data
13ef0 62 61 73 65 20 69 6d 61 67 65 20 73 69 7a 65 20  base image size 
13ef1 2a 2f 20 0a 20 20 20 20 20 20 70 50 61 67 65 72  */ .      pPager
13ef2 2d 3e 64 62 53 69 7a 65 20 3d 20 70 50 61 67 65  ->dbSize = pPage
13ef3 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3b 0a 20  r->dbOrigSize;. 
13ef4 20 20 20 20 20 66 6f 72 28 20 69 3d 64 62 53 69       for( i=dbSi
13ef5 7a 65 2b 31 3b 20 69 3c 3d 70 50 61 67 65 72 2d  ze+1; i<=pPager-
13ef6 3e 64 62 4f 72 69 67 53 69 7a 65 3b 20 69 2b 2b  >dbOrigSize; i++
13ef7 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
13ef8 21 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65  !sqlite3BitvecTe
13ef9 73 74 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f  st(pPager->pInJo
13efa 75 72 6e 61 6c 2c 20 69 29 20 26 26 20 69 21 3d  urnal, i) && i!=
13efb 69 53 6b 69 70 20 29 7b 0a 20 20 20 20 20 20 20  iSkip ){.       
13efc 20 20 20 50 67 48 64 72 20 2a 70 50 61 67 65 3b     PgHdr *pPage;
13efd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
13efe 50 61 67 65 20 74 6f 20 6a 6f 75 72 6e 61 6c 20  Page to journal 
13eff 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  */.          rc 
13f00 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  = sqlite3PagerGe
13f01 74 28 70 50 61 67 65 72 2c 20 69 2c 20 26 70 50  t(pPager, i, &pP
13f02 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  age);.          
13f03 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
13f04 4b 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f  K ) goto commit_
13f05 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a  phase_one_exit;.
13f06 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73            rc = s
13f07 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
13f08 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  (pPage);.       
13f09 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55     sqlite3PagerU
13f0a 6e 72 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20  nref(pPage);.   
13f0b 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
13f0c 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
13f0d 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65  commit_phase_one
13f0e 5f 65 78 69 74 3b 0a 20 20 20 20 20 20 20 20 7d  _exit;.        }
13f0f 0a 20 20 20 20 20 20 7d 20 0a 20 20 20 20 20 20  .      } .      
13f10 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d  pPager->dbSize =
13f11 20 64 62 53 69 7a 65 3b 0a 20 20 20 20 7d 0a 23   dbSize;.    }.#
13f12 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 57 72  endif..    /* Wr
13f13 69 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a  ite the master j
13f14 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 69 6e 74 6f  ournal name into
13f15 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
13f16 65 2e 20 49 66 20 61 20 6d 61 73 74 65 72 20 0a  e. If a master .
13f17 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66      ** journal f
13f18 69 6c 65 20 6e 61 6d 65 20 68 61 73 20 61 6c 72  ile name has alr
13f19 65 61 64 79 20 62 65 65 6e 20 77 72 69 74 74 65  eady been writte
13f1a 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c  n to the journal
13f1b 20 66 69 6c 65 2c 20 0a 20 20 20 20 2a 2a 20 6f   file, .    ** o
13f1c 72 20 69 66 20 7a 4d 61 73 74 65 72 20 69 73 20  r if zMaster is 
13f1d 4e 55 4c 4c 20 28 6e 6f 20 6d 61 73 74 65 72 20  NULL (no master 
13f1e 6a 6f 75 72 6e 61 6c 29 2c 20 74 68 65 6e 20 74  journal), then t
13f1f 68 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f  his call is a no
13f20 2d 6f 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  -op..    */.    
13f21 72 63 20 3d 20 77 72 69 74 65 4d 61 73 74 65 72  rc = writeMaster
13f22 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2c 20  Journal(pPager, 
13f23 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 69 66  zMaster);.    if
13f24 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
13f25 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68  ) goto commit_ph
13f26 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 0a 20  ase_one_exit;.. 
13f27 20 20 20 2f 2a 20 53 79 6e 63 20 74 68 65 20 6a     /* Sync the j
13f28 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20  ournal file. If 
13f29 74 68 65 20 61 74 6f 6d 69 63 2d 75 70 64 61 74  the atomic-updat
13f2a 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69  e optimization i
13f2b 73 20 62 65 69 6e 67 0a 20 20 20 20 2a 2a 20 75  s being.    ** u
13f2c 73 65 64 2c 20 74 68 69 73 20 63 61 6c 6c 20 77  sed, this call w
13f2d 69 6c 6c 20 6e 6f 74 20 63 72 65 61 74 65 20 74  ill not create t
13f2e 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
13f2f 6f 72 20 70 65 72 66 6f 72 6d 20 61 6e 79 0a 20  or perform any. 
13f30 20 20 20 2a 2a 20 72 65 61 6c 20 49 4f 2e 0a 20     ** real IO.. 
13f31 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73     */.    rc = s
13f32 79 6e 63 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65  yncJournal(pPage
13f33 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  r);.    if( rc!=
13f34 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
13f35 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e   commit_phase_on
13f36 65 5f 65 78 69 74 3b 0a 0a 20 20 20 20 2f 2a 20  e_exit;..    /* 
13f37 57 72 69 74 65 20 61 6c 6c 20 64 69 72 74 79 20  Write all dirty 
13f38 70 61 67 65 73 20 74 6f 20 74 68 65 20 64 61 74  pages to the dat
13f39 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 20  abase file. */. 
13f3a 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 72     rc = pager_wr
13f3b 69 74 65 5f 70 61 67 65 6c 69 73 74 28 73 71 6c  ite_pagelist(sql
13f3c 69 74 65 33 50 63 61 63 68 65 44 69 72 74 79 4c  ite3PcacheDirtyL
13f3d 69 73 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61  ist(pPager->pPCa
13f3e 63 68 65 29 29 3b 0a 20 20 20 20 69 66 28 20 72  che));.    if( r
13f3f 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
13f40 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63        assert( rc
13f41 21 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42  !=SQLITE_IOERR_B
13f42 4c 4f 43 4b 45 44 20 29 3b 0a 20 20 20 20 20 20  LOCKED );.      
13f43 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73  goto commit_phas
13f44 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 20 20 20  e_one_exit;.    
13f45 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61  }.    sqlite3Pca
13f46 63 68 65 43 6c 65 61 6e 41 6c 6c 28 70 50 61 67  cheCleanAll(pPag
13f47 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 0a 20  er->pPCache);.. 
13f48 20 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c     /* If the fil
13f49 65 20 6f 6e 20 64 69 73 6b 20 69 73 20 6e 6f 74  e on disk is not
13f4a 20 74 68 65 20 73 61 6d 65 20 73 69 7a 65 20 61   the same size a
13f4b 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  s the database i
13f4c 6d 61 67 65 2c 0a 20 20 20 20 2a 2a 20 74 68 65  mage,.    ** the
13f4d 6e 20 75 73 65 20 70 61 67 65 72 5f 74 72 75 6e  n use pager_trun
13f4e 63 61 74 65 20 74 6f 20 67 72 6f 77 20 6f 72 20  cate to grow or 
13f4f 73 68 72 69 6e 6b 20 74 68 65 20 66 69 6c 65 20  shrink the file 
13f50 68 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  here..    */.   
13f51 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53   if( pPager->dbS
13f52 69 7a 65 21 3d 70 50 61 67 65 72 2d 3e 64 62 46  ize!=pPager->dbF
13f53 69 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20  ileSize ){.     
13f54 20 50 67 6e 6f 20 6e 4e 65 77 20 3d 20 70 50 61   Pgno nNew = pPa
13f55 67 65 72 2d 3e 64 62 53 69 7a 65 20 2d 20 28 70  ger->dbSize - (p
13f56 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d 50  Pager->dbSize==P
13f57 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61  AGER_MJ_PGNO(pPa
13f58 67 65 72 29 29 3b 0a 20 20 20 20 20 20 61 73 73  ger));.      ass
13f59 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61  ert( pPager->sta
13f5a 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53  te>=PAGER_EXCLUS
13f5b 49 56 45 20 29 3b 0a 20 20 20 20 20 20 72 63 20  IVE );.      rc 
13f5c 3d 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65  = pager_truncate
13f5d 28 70 50 61 67 65 72 2c 20 6e 4e 65 77 29 3b 0a  (pPager, nNew);.
13f5e 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
13f5f 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63  LITE_OK ) goto c
13f60 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f  ommit_phase_one_
13f61 65 78 69 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  exit;.    }..   
13f62 20 2f 2a 20 46 69 6e 61 6c 6c 79 2c 20 73 79 6e   /* Finally, syn
13f63 63 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  c the database f
13f64 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  ile. */.    if( 
13f65 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20  !pPager->noSync 
13f66 26 26 20 21 6e 6f 53 79 6e 63 20 29 7b 0a 20 20  && !noSync ){.  
13f67 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
13f68 4f 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 66  OsSync(pPager->f
13f69 64 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f  d, pPager->sync_
13f6a 66 6c 61 67 73 29 3b 0a 20 20 20 20 7d 0a 20 20  flags);.    }.  
13f6b 20 20 49 4f 54 52 41 43 45 28 28 22 44 42 53 59    IOTRACE(("DBSY
13f6c 4e 43 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72  NC %p\n", pPager
13f6d 29 29 0a 0a 20 20 20 20 70 50 61 67 65 72 2d 3e  ))..    pPager->
13f6e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 53 59  state = PAGER_SY
13f6f 4e 43 45 44 3b 0a 20 20 7d 0a 0a 63 6f 6d 6d 69  NCED;.  }..commi
13f70 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74  t_phase_one_exit
13f71 3a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  :.  return rc;.}
13f72 0a 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68  .../*.** When th
13f73 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
13f74 61 6c 6c 65 64 2c 20 74 68 65 20 64 61 74 61 62  alled, the datab
13f75 61 73 65 20 66 69 6c 65 20 68 61 73 20 62 65 65  ase file has bee
13f76 6e 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a 20  n completely.** 
13f77 75 70 64 61 74 65 64 20 74 6f 20 72 65 66 6c 65  updated to refle
13f78 63 74 20 74 68 65 20 63 68 61 6e 67 65 73 20 6d  ct the changes m
13f79 61 64 65 20 62 79 20 74 68 65 20 63 75 72 72 65  ade by the curre
13f7a 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61  nt transaction a
13f7b 6e 64 0a 2a 2a 20 73 79 6e 63 65 64 20 74 6f 20  nd.** synced to 
13f7c 64 69 73 6b 2e 20 54 68 65 20 6a 6f 75 72 6e 61  disk. The journa
13f7d 6c 20 66 69 6c 65 20 73 74 69 6c 6c 20 65 78 69  l file still exi
13f7e 73 74 73 20 69 6e 20 74 68 65 20 66 69 6c 65 2d  sts in the file-
13f7f 73 79 73 74 65 6d 20 0a 2a 2a 20 74 68 6f 75 67  system .** thoug
13f80 68 2c 20 61 6e 64 20 69 66 20 61 20 66 61 69 6c  h, and if a fail
13f81 75 72 65 20 6f 63 63 75 72 73 20 61 74 20 74 68  ure occurs at th
13f82 69 73 20 70 6f 69 6e 74 20 69 74 20 77 69 6c 6c  is point it will
13f83 20 65 76 65 6e 74 75 61 6c 6c 79 0a 2a 2a 20 62   eventually.** b
13f84 65 20 75 73 65 64 20 61 73 20 61 20 68 6f 74 2d  e used as a hot-
13f85 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74 68 65 20  journal and the 
13f86 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74  current transact
13f87 69 6f 6e 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e  ion rolled back.
13f88 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
13f89 74 69 6f 6e 20 66 69 6e 61 6c 69 7a 65 73 20 74  tion finalizes t
13f8a 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c  he journal file,
13f8b 20 65 69 74 68 65 72 20 62 79 20 64 65 6c 65 74   either by delet
13f8c 69 6e 67 2c 20 0a 2a 2a 20 74 72 75 6e 63 61 74  ing, .** truncat
13f8d 69 6e 67 20 6f 72 20 70 61 72 74 69 61 6c 6c 79  ing or partially
13f8e 20 7a 65 72 6f 69 6e 67 20 69 74 2c 20 73 6f 20   zeroing it, so 
13f8f 74 68 61 74 20 69 74 20 63 61 6e 6e 6f 74 20 62  that it cannot b
13f90 65 20 75 73 65 64 20 0a 2a 2a 20 66 6f 72 20 68  e used .** for h
13f91 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62  ot-journal rollb
13f92 61 63 6b 2e 20 4f 6e 63 65 20 74 68 69 73 20 69  ack. Once this i
13f93 73 20 64 6f 6e 65 20 74 68 65 20 74 72 61 6e 73  s done the trans
13f94 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 69 72 72  action is.** irr
13f95 65 76 6f 63 61 62 6c 79 20 63 6f 6d 6d 69 74 74  evocably committ
13f96 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  ed..**.** If an 
13f97 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 61 6e  error occurs, an
13f98 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69   IO error code i
13f99 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74  s returned and t
13f9a 68 65 20 70 61 67 65 72 0a 2a 2a 20 6d 6f 76 65  he pager.** move
13f9b 73 20 69 6e 74 6f 20 74 68 65 20 65 72 72 6f 72  s into the error
13f9c 20 73 74 61 74 65 2e 20 4f 74 68 65 72 77 69 73   state. Otherwis
13f9d 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  e, SQLITE_OK is 
13f9e 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c  returned..*/.SQL
13f9f 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
13fa0 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d  sqlite3PagerComm
13fa1 69 74 50 68 61 73 65 54 77 6f 28 50 61 67 65 72  itPhaseTwo(Pager
13fa2 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74   *pPager){.  int
13fa3 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
13fa4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13fa5 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
13fa6 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 72   */..  /* This r
13fa7 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 6e 6f  outine should no
13fa8 74 20 62 65 20 63 61 6c 6c 65 64 20 69 66 20 61  t be called if a
13fa9 20 70 72 69 6f 72 20 65 72 72 6f 72 20 68 61 73   prior error has
13faa 20 6f 63 63 75 72 72 65 64 2e 0a 20 20 2a 2a 20   occurred..  ** 
13fab 42 75 74 20 69 66 20 28 64 75 65 20 74 6f 20 61  But if (due to a
13fac 20 63 6f 64 69 6e 67 20 65 72 72 6f 72 20 65 6c   coding error el
13fad 73 65 77 68 65 72 65 20 69 6e 20 74 68 65 20 73  sewhere in the s
13fae 79 73 74 65 6d 29 20 69 74 20 64 6f 65 73 20 67  ystem) it does g
13faf 65 74 0a 20 20 2a 2a 20 63 61 6c 6c 65 64 2c 20  et.  ** called, 
13fb0 6a 75 73 74 20 72 65 74 75 72 6e 20 74 68 65 20  just return the 
13fb1 73 61 6d 65 20 65 72 72 6f 72 20 63 6f 64 65 20  same error code 
13fb2 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e  without doing an
13fb3 79 74 68 69 6e 67 2e 20 2a 2f 0a 20 20 69 66 28  ything. */.  if(
13fb4 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d 3e 65   NEVER(pPager->e
13fb5 72 72 43 6f 64 65 29 20 29 20 72 65 74 75 72 6e  rrCode) ) return
13fb6 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
13fb7 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 66 75 6e  ;..  /* This fun
13fb8 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74  ction should not
13fb9 20 62 65 20 63 61 6c 6c 65 64 20 69 66 20 74 68   be called if th
13fba 65 20 70 61 67 65 72 20 69 73 20 6e 6f 74 20 69  e pager is not i
13fbb 6e 20 61 74 20 6c 65 61 73 74 0a 20 20 2a 2a 20  n at least.  ** 
13fbc 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 73  PAGER_RESERVED s
13fbd 74 61 74 65 2e 20 41 6e 64 20 69 6e 64 65 65 64  tate. And indeed
13fbe 20 53 51 4c 69 74 65 20 6e 65 76 65 72 20 64 6f   SQLite never do
13fbf 65 73 20 74 68 69 73 2e 20 42 75 74 20 69 74 20  es this. But it 
13fc0 69 73 0a 20 20 2a 2a 20 6e 69 63 65 20 74 6f 20  is.  ** nice to 
13fc1 68 61 76 65 20 74 68 69 73 20 64 65 66 65 6e 73  have this defens
13fc2 69 76 65 20 74 65 73 74 20 68 65 72 65 20 61 6e  ive test here an
13fc3 79 77 61 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  yway..  */.  if(
13fc4 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d 3e 73   NEVER(pPager->s
13fc5 74 61 74 65 3c 50 41 47 45 52 5f 52 45 53 45 52  tate<PAGER_RESER
13fc6 56 45 44 29 20 29 20 72 65 74 75 72 6e 20 53 51  VED) ) return SQ
13fc7 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 0a 20 20 2f  LITE_ERROR;..  /
13fc8 2a 20 41 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f  * An optimizatio
13fc9 6e 2e 20 49 66 20 74 68 65 20 64 61 74 61 62 61  n. If the databa
13fca 73 65 20 77 61 73 20 6e 6f 74 20 61 63 74 75 61  se was not actua
13fcb 6c 6c 79 20 6d 6f 64 69 66 69 65 64 20 64 75 72  lly modified dur
13fcc 69 6e 67 0a 20 20 2a 2a 20 74 68 69 73 20 74 72  ing.  ** this tr
13fcd 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 20 70  ansaction, the p
13fce 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 20  ager is running 
13fcf 69 6e 20 65 78 63 6c 75 73 69 76 65 2d 6d 6f 64  in exclusive-mod
13fd0 65 20 61 6e 64 20 69 73 0a 20 20 2a 2a 20 75 73  e and is.  ** us
13fd1 69 6e 67 20 70 65 72 73 69 73 74 65 6e 74 20 6a  ing persistent j
13fd2 6f 75 72 6e 61 6c 73 2c 20 74 68 65 6e 20 74 68  ournals, then th
13fd3 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61  is function is a
13fd4 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2a 0a 20 20 2a   no-op..  **.  *
13fd5 2a 20 54 68 65 20 73 74 61 72 74 20 6f 66 20 74  * The start of t
13fd6 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
13fd7 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61 69  currently contai
13fd8 6e 73 20 61 20 73 69 6e 67 6c 65 20 6a 6f 75 72  ns a single jour
13fd9 6e 61 6c 20 0a 20 20 2a 2a 20 68 65 61 64 65 72  nal .  ** header
13fda 20 77 69 74 68 20 74 68 65 20 6e 52 65 63 20 66   with the nRec f
13fdb 69 65 6c 64 20 73 65 74 20 74 6f 20 30 2e 20 49  ield set to 0. I
13fdc 66 20 73 75 63 68 20 61 20 6a 6f 75 72 6e 61 6c  f such a journal
13fdd 20 69 73 20 75 73 65 64 20 61 73 0a 20 20 2a 2a   is used as.  **
13fde 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 64   a hot-journal d
13fdf 75 72 69 6e 67 20 68 6f 74 2d 6a 6f 75 72 6e 61  uring hot-journa
13fe0 6c 20 72 6f 6c 6c 62 61 63 6b 2c 20 30 20 63 68  l rollback, 0 ch
13fe1 61 6e 67 65 73 20 77 69 6c 6c 20 62 65 20 6d 61  anges will be ma
13fe2 64 65 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 64  de.  ** to the d
13fe3 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 53 6f  atabase file. So
13fe4 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65   there is no nee
13fe5 64 20 74 6f 20 7a 65 72 6f 20 74 68 65 20 6a 6f  d to zero the jo
13fe6 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 68 65 61 64  urnal .  ** head
13fe7 65 72 2e 20 53 69 6e 63 65 20 74 68 65 20 70 61  er. Since the pa
13fe8 67 65 72 20 69 73 20 69 6e 20 65 78 63 6c 75 73  ger is in exclus
13fe9 69 76 65 20 6d 6f 64 65 2c 20 74 68 65 72 65 20  ive mode, there 
13fea 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20 2a 2a 20  is no need.  ** 
13feb 74 6f 20 64 72 6f 70 20 61 6e 79 20 6c 6f 63 6b  to drop any lock
13fec 73 20 65 69 74 68 65 72 2e 0a 20 20 2a 2f 0a 20  s either..  */. 
13fed 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 4d   if( pPager->dbM
13fee 6f 64 69 66 69 65 64 3d 3d 30 20 26 26 20 70 50  odified==0 && pP
13fef 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d  ager->exclusiveM
13ff0 6f 64 65 20 0a 20 20 20 26 26 20 70 50 61 67 65  ode .   && pPage
13ff1 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d  r->journalMode==
13ff2 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
13ff3 45 5f 50 45 52 53 49 53 54 0a 20 20 29 7b 0a 20  E_PERSIST.  ){. 
13ff4 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
13ff5 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 4a  r->journalOff==J
13ff6 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50  OURNAL_HDR_SZ(pP
13ff7 61 67 65 72 29 20 29 3b 0a 20 20 20 20 72 65 74  ager) );.    ret
13ff8 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
13ff9 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45   }..  PAGERTRACE
13ffa 28 28 22 43 4f 4d 4d 49 54 20 25 64 5c 6e 22 2c  (("COMMIT %d\n",
13ffb 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29   PAGERID(pPager)
13ffc 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50  ));.  assert( pP
13ffd 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47  ager->state==PAG
13ffe 45 52 5f 53 59 4e 43 45 44 20 7c 7c 20 4d 45 4d  ER_SYNCED || MEM
13fff 44 42 20 7c 7c 20 21 70 50 61 67 65 72 2d 3e 64  DB || !pPager->d
14000 62 4d 6f 64 69 66 69 65 64 20 29 3b 0a 20 20 72  bModified );.  r
14001 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72  c = pager_end_tr
14002 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72  ansaction(pPager
14003 2c 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73  , pPager->setMas
14004 74 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  ter);.  return p
14005 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65  ager_error(pPage
14006 72 2c 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  r, rc);.}../*.**
14007 20 52 6f 6c 6c 62 61 63 6b 20 61 6c 6c 20 63 68   Rollback all ch
14008 61 6e 67 65 73 2e 20 54 68 65 20 64 61 74 61 62  anges. The datab
14009 61 73 65 20 66 61 6c 6c 73 20 62 61 63 6b 20 74  ase falls back t
1400a 6f 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 6d  o PAGER_SHARED m
1400b 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ode..**.** This 
1400c 66 75 6e 63 74 69 6f 6e 20 70 65 72 66 6f 72 6d  function perform
1400d 73 20 74 77 6f 20 74 61 73 6b 73 3a 0a 2a 2a 0a  s two tasks:.**.
1400e 2a 2a 20 20 20 31 29 20 49 74 20 72 6f 6c 6c 73  **   1) It rolls
1400f 20 62 61 63 6b 20 74 68 65 20 6a 6f 75 72 6e 61   back the journa
14010 6c 20 66 69 6c 65 2c 20 72 65 73 74 6f 72 69 6e  l file, restorin
14011 67 20 61 6c 6c 20 64 61 74 61 62 61 73 65 20 66  g all database f
14012 69 6c 65 20 61 6e 64 20 0a 2a 2a 20 20 20 20 20  ile and .**     
14013 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65   in-memory cache
14014 20 70 61 67 65 73 20 74 6f 20 74 68 65 20 73 74   pages to the st
14015 61 74 65 20 74 68 65 79 20 77 65 72 65 20 69 6e  ate they were in
14016 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61   when the transa
14017 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 77 61  ction.**      wa
14018 73 20 6f 70 65 6e 65 64 2c 20 61 6e 64 0a 2a 2a  s opened, and.**
14019 20 20 20 32 29 20 49 74 20 66 69 6e 61 6c 69 7a     2) It finaliz
1401a 65 73 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  es the journal f
1401b 69 6c 65 2c 20 73 6f 20 74 68 61 74 20 69 74 20  ile, so that it 
1401c 69 73 20 6e 6f 74 20 75 73 65 64 20 66 6f 72 20  is not used for 
1401d 68 6f 74 0a 2a 2a 20 20 20 20 20 20 72 6f 6c 6c  hot.**      roll
1401e 62 61 63 6b 20 61 74 20 61 6e 79 20 70 6f 69 6e  back at any poin
1401f 74 20 69 6e 20 74 68 65 20 66 75 74 75 72 65 2e  t in the future.
14020 0a 2a 2a 0a 2a 2a 20 73 75 62 6a 65 63 74 20 74  .**.** subject t
14021 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  o the following 
14022 71 75 61 6c 69 66 69 63 61 74 69 6f 6e 73 3a 0a  qualifications:.
14023 2a 2a 0a 2a 2a 20 2a 20 49 66 20 74 68 65 20 6a  **.** * If the j
14024 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e  ournal file is n
14025 6f 74 20 79 65 74 20 6f 70 65 6e 20 77 68 65 6e  ot yet open when
14026 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
14027 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 20 20 74  s called,.**   t
14028 68 65 6e 20 6f 6e 6c 79 20 28 32 29 20 69 73 20  hen only (2) is 
14029 70 65 72 66 6f 72 6d 65 64 2e 20 49 6e 20 74 68  performed. In th
1402a 69 73 20 63 61 73 65 20 74 68 65 72 65 20 69 73  is case there is
1402b 20 6e 6f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65   no journal file
1402c 0a 2a 2a 20 20 20 74 6f 20 72 6f 6c 6c 20 62 61  .**   to roll ba
1402d 63 6b 2e 0a 2a 2a 0a 2a 2a 20 2a 20 49 66 20 69  ck..**.** * If i
1402e 6e 20 61 6e 20 65 72 72 6f 72 20 73 74 61 74 65  n an error state
1402f 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49   other than SQLI
14030 54 45 5f 46 55 4c 4c 2c 20 74 68 65 6e 20 74 61  TE_FULL, then ta
14031 73 6b 20 28 31 29 20 69 73 20 0a 2a 2a 20 20 20  sk (1) is .**   
14032 70 65 72 66 6f 72 6d 65 64 2e 20 49 66 20 73 75  performed. If su
14033 63 63 65 73 73 66 75 6c 2c 20 74 61 73 6b 20 28  ccessful, task (
14034 32 29 2e 20 52 65 67 61 72 64 6c 65 73 73 20 6f  2). Regardless o
14035 66 20 74 68 65 20 6f 75 74 63 6f 6d 65 0a 2a 2a  f the outcome.**
14036 20 20 20 6f 66 20 65 69 74 68 65 72 2c 20 74 68     of either, th
14037 65 20 65 72 72 6f 72 20 73 74 61 74 65 20 65 72  e error state er
14038 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75  ror code is retu
14039 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c  rned to the call
1403a 65 72 0a 2a 2a 20 20 20 28 69 2e 65 2e 20 65 69  er.**   (i.e. ei
1403b 74 68 65 72 20 53 51 4c 49 54 45 5f 49 4f 45 52  ther SQLITE_IOER
1403c 52 20 6f 72 20 53 51 4c 49 54 45 5f 43 4f 52 52  R or SQLITE_CORR
1403d 55 50 54 29 2e 0a 2a 2a 0a 2a 2a 20 2a 20 49 66  UPT)..**.** * If
1403e 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e   the pager is in
1403f 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20   PAGER_RESERVED 
14040 73 74 61 74 65 2c 20 74 68 65 6e 20 61 74 74 65  state, then atte
14041 6d 70 74 20 28 31 29 2e 20 57 68 65 74 68 65 72  mpt (1). Whether
14042 0a 2a 2a 20 20 20 6f 72 20 6e 6f 74 20 28 31 29  .**   or not (1)
14043 20 69 73 20 73 75 63 63 75 73 73 66 75 6c 2c 20   is succussful, 
14044 61 6c 73 6f 20 61 74 74 65 6d 70 74 20 28 32 29  also attempt (2)
14045 2e 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c  . If successful,
14046 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 53 51 4c   return.**   SQL
14047 49 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73  ITE_OK. Otherwis
14048 65 2c 20 65 6e 74 65 72 20 74 68 65 20 65 72 72  e, enter the err
14049 6f 72 20 73 74 61 74 65 20 61 6e 64 20 72 65 74  or state and ret
1404a 75 72 6e 20 74 68 65 20 66 69 72 73 74 20 0a 2a  urn the first .*
1404b 2a 20 20 20 65 72 72 6f 72 20 63 6f 64 65 20 65  *   error code e
1404c 6e 63 6f 75 6e 74 65 72 65 64 2e 20 0a 2a 2a 0a  ncountered. .**.
1404d 2a 2a 20 20 20 49 6e 20 74 68 69 73 20 63 61 73  **   In this cas
1404e 65 20 74 68 65 72 65 20 69 73 20 6e 6f 20 63 68  e there is no ch
1404f 61 6e 63 65 20 74 68 61 74 20 74 68 65 20 64 61  ance that the da
14050 74 61 62 61 73 65 20 77 61 73 20 77 72 69 74 74  tabase was writt
14051 65 6e 20 74 6f 2e 20 0a 2a 2a 20 20 20 53 6f 20  en to. .**   So 
14052 69 73 20 73 61 66 65 20 74 6f 20 66 69 6e 61 6c  is safe to final
14053 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ize the journal 
14054 66 69 6c 65 20 65 76 65 6e 20 69 66 20 74 68 65  file even if the
14055 20 70 6c 61 79 62 61 63 6b 20 0a 2a 2a 20 20 20   playback .**   
14056 28 6f 70 65 72 61 74 69 6f 6e 20 31 29 20 66 61  (operation 1) fa
14057 69 6c 65 64 2e 20 48 6f 77 65 76 65 72 20 74 68  iled. However th
14058 65 20 70 61 67 65 72 20 6d 75 73 74 20 65 6e 74  e pager must ent
14059 65 72 20 74 68 65 20 65 72 72 6f 72 20 73 74 61  er the error sta
1405a 74 65 0a 2a 2a 20 20 20 61 73 20 74 68 65 20 63  te.**   as the c
1405b 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 69  ontents of the i
1405c 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 20 61  n-memory cache a
1405d 72 65 20 6e 6f 77 20 73 75 73 70 65 63 74 2e 0a  re now suspect..
1405e 2a 2a 0a 2a 2a 20 2a 20 46 69 6e 61 6c 6c 79 2c  **.** * Finally,
1405f 20 69 66 20 69 6e 20 50 41 47 45 52 5f 45 58 43   if in PAGER_EXC
14060 4c 55 53 49 56 45 20 73 74 61 74 65 2c 20 74 68  LUSIVE state, th
14061 65 6e 20 61 74 74 65 6d 70 74 20 28 31 29 2e 20  en attempt (1). 
14062 4f 6e 6c 79 0a 2a 2a 20 20 20 61 74 74 65 6d 70  Only.**   attemp
14063 74 20 28 32 29 20 69 66 20 28 31 29 20 69 73 20  t (2) if (1) is 
14064 73 75 63 63 65 73 73 66 75 6c 2e 20 52 65 74 75  successful. Retu
14065 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20  rn SQLITE_OK if 
14066 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 20  successful,.**  
14067 20 6f 74 68 65 72 77 69 73 65 20 65 6e 74 65 72   otherwise enter
14068 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65   the error state
14069 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20   and return the 
1406a 65 72 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20  error code from 
1406b 74 68 65 20 0a 2a 2a 20 20 20 66 61 69 6c 69 6e  the .**   failin
1406c 67 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a 0a  g operation..**.
1406d 2a 2a 20 20 20 49 6e 20 74 68 69 73 20 63 61 73  **   In this cas
1406e 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  e the database f
1406f 69 6c 65 20 6d 61 79 20 68 61 76 65 20 62 65 65  ile may have bee
14070 6e 20 77 72 69 74 74 65 6e 20 74 6f 2e 20 53 6f  n written to. So
14071 20 69 66 20 74 68 65 0a 2a 2a 20 20 20 70 6c 61   if the.**   pla
14072 79 62 61 63 6b 20 6f 70 65 72 61 74 69 6f 6e 20  yback operation 
14073 64 69 64 20 6e 6f 74 20 73 75 63 63 65 65 64 20  did not succeed 
14074 69 74 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65 20  it would not be 
14075 73 61 66 65 20 74 6f 20 66 69 6e 61 6c 69 7a 65  safe to finalize
14076 0a 2a 2a 20 20 20 74 68 65 20 6a 6f 75 72 6e 61  .**   the journa
14077 6c 20 66 69 6c 65 2e 20 49 74 20 6e 65 65 64 73  l file. It needs
14078 20 74 6f 20 62 65 20 6c 65 66 74 20 69 6e 20 74   to be left in t
14079 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 73  he file-system s
1407a 6f 20 74 68 61 74 0a 2a 2a 20 20 20 73 6f 6d 65  o that.**   some
1407b 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 63   other process c
1407c 61 6e 20 75 73 65 20 69 74 20 74 6f 20 72 65 73  an use it to res
1407d 74 6f 72 65 20 74 68 65 20 64 61 74 61 62 61 73  tore the databas
1407e 65 20 73 74 61 74 65 20 28 62 79 0a 2a 2a 20 20  e state (by.**  
1407f 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c   hot-journal rol
14080 6c 62 61 63 6b 29 2e 0a 2a 2f 0a 53 51 4c 49 54  lback)..*/.SQLIT
14081 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
14082 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61  lite3PagerRollba
14083 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ck(Pager *pPager
14084 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
14085 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20  LITE_OK;        
14086 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
14087 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 50 41  urn code */.  PA
14088 47 45 52 54 52 41 43 45 28 28 22 52 4f 4c 4c 42  GERTRACE(("ROLLB
14089 41 43 4b 20 25 64 5c 6e 22 2c 20 50 41 47 45 52  ACK %d\n", PAGER
1408a 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20  ID(pPager)));.  
1408b 69 66 28 20 21 70 50 61 67 65 72 2d 3e 64 62 4d  if( !pPager->dbM
1408c 6f 64 69 66 69 65 64 20 7c 7c 20 21 69 73 4f 70  odified || !isOp
1408d 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
1408e 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65  ){.    rc = page
1408f 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f  r_end_transactio
14090 6e 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 72  n(pPager, pPager
14091 2d 3e 73 65 74 4d 61 73 74 65 72 29 3b 0a 20 20  ->setMaster);.  
14092 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67 65 72  }else if( pPager
14093 2d 3e 65 72 72 43 6f 64 65 20 26 26 20 70 50 61  ->errCode && pPa
14094 67 65 72 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51  ger->errCode!=SQ
14095 4c 49 54 45 5f 46 55 4c 4c 20 29 7b 0a 20 20 20  LITE_FULL ){.   
14096 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61   if( pPager->sta
14097 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53  te>=PAGER_EXCLUS
14098 49 56 45 20 29 7b 0a 20 20 20 20 20 20 70 61 67  IVE ){.      pag
14099 65 72 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67  er_playback(pPag
1409a 65 72 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20  er, 0);.    }.  
1409b 20 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65    rc = pPager->e
1409c 72 72 43 6f 64 65 3b 0a 20 20 7d 65 6c 73 65 7b  rrCode;.  }else{
1409d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d  .    if( pPager-
1409e 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 45  >state==PAGER_RE
1409f 53 45 52 56 45 44 20 29 7b 0a 20 20 20 20 20 20  SERVED ){.      
140a0 69 6e 74 20 72 63 32 3b 0a 20 20 20 20 20 20 72  int rc2;.      r
140a1 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61  c = pager_playba
140a2 63 6b 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20  ck(pPager, 0);. 
140a3 20 20 20 20 20 72 63 32 20 3d 20 70 61 67 65 72       rc2 = pager
140a4 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e  _end_transaction
140a5 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d  (pPager, pPager-
140a6 3e 73 65 74 4d 61 73 74 65 72 29 3b 0a 20 20 20  >setMaster);.   
140a7 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
140a8 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
140a9 72 63 20 3d 20 72 63 32 3b 0a 20 20 20 20 20 20  rc = rc2;.      
140aa 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
140ab 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c     rc = pager_pl
140ac 61 79 62 61 63 6b 28 70 50 61 67 65 72 2c 20 30  ayback(pPager, 0
140ad 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  );.    }..    if
140ae 28 20 21 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20  ( !MEMDB ){.    
140af 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65    pPager->dbSize
140b0 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 7d  Valid = 0;.    }
140b1 0a 0a 20 20 20 20 2f 2a 20 49 66 20 61 6e 20 65  ..    /* If an e
140b2 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69  rror occurs duri
140b3 6e 67 20 61 20 52 4f 4c 4c 42 41 43 4b 2c 20 77  ng a ROLLBACK, w
140b4 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20  e can no longer 
140b5 74 72 75 73 74 20 74 68 65 20 70 61 67 65 72 0a  trust the pager.
140b6 20 20 20 20 2a 2a 20 63 61 63 68 65 2e 20 53 6f      ** cache. So
140b7 20 63 61 6c 6c 20 70 61 67 65 72 5f 65 72 72 6f   call pager_erro
140b8 72 28 29 20 6f 6e 20 74 68 65 20 77 61 79 20 6f  r() on the way o
140b9 75 74 20 74 6f 20 6d 61 6b 65 20 61 6e 79 20 65  ut to make any e
140ba 72 72 6f 72 20 0a 20 20 20 20 2a 2a 20 70 65 72  rror .    ** per
140bb 73 69 73 74 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a  sistent..    */.
140bc 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65      rc = pager_e
140bd 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29  rror(pPager, rc)
140be 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
140bf 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  c;.}../*.** Retu
140c0 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 64  rn TRUE if the d
140c1 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20  atabase file is 
140c2 6f 70 65 6e 65 64 20 72 65 61 64 2d 6f 6e 6c 79  opened read-only
140c3 2e 20 20 52 65 74 75 72 6e 20 46 41 4c 53 45 0a  .  Return FALSE.
140c4 2a 2a 20 69 66 20 74 68 65 20 64 61 74 61 62 61  ** if the databa
140c5 73 65 20 69 73 20 28 69 6e 20 74 68 65 6f 72 79  se is (in theory
140c6 29 20 77 72 69 74 61 62 6c 65 2e 0a 2a 2f 0a 53  ) writable..*/.S
140c7 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 38  QLITE_PRIVATE u8
140c8 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 72   sqlite3PagerIsr
140c9 65 61 64 6f 6e 6c 79 28 50 61 67 65 72 20 2a 70  eadonly(Pager *p
140ca 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e  Pager){.  return
140cb 20 70 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c   pPager->readOnl
140cc 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  y;.}../*.** Retu
140cd 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
140ce 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74   references to t
140cf 68 65 20 70 61 67 65 72 2e 0a 2a 2f 0a 53 51 4c  he pager..*/.SQL
140d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
140d1 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63  sqlite3PagerRefc
140d2 6f 75 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67  ount(Pager *pPag
140d3 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71  er){.  return sq
140d4 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f  lite3PcacheRefCo
140d5 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61  unt(pPager->pPCa
140d6 63 68 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  che);.}../*.** R
140d7 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
140d8 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 74   of references t
140d9 6f 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  o the specified 
140da 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  page..*/.SQLITE_
140db 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
140dc 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66 63  te3PagerPageRefc
140dd 6f 75 6e 74 28 44 62 50 61 67 65 20 2a 70 50 61  ount(DbPage *pPa
140de 67 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71  ge){.  return sq
140df 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65 52  lite3PcachePageR
140e0 65 66 63 6f 75 6e 74 28 70 50 61 67 65 29 3b 0a  efcount(pPage);.
140e1 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  }..#ifdef SQLITE
140e2 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73  _TEST./*.** This
140e3 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64   routine is used
140e4 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64   for testing and
140e5 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 2e 0a   analysis only..
140e6 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
140e7 45 20 69 6e 74 20 2a 73 71 6c 69 74 65 33 50 61  E int *sqlite3Pa
140e8 67 65 72 53 74 61 74 73 28 50 61 67 65 72 20 2a  gerStats(Pager *
140e9 70 50 61 67 65 72 29 7b 0a 20 20 73 74 61 74 69  pPager){.  stati
140ea 63 20 69 6e 74 20 61 5b 31 31 5d 3b 0a 20 20 61  c int a[11];.  a
140eb 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33 50 63 61  [0] = sqlite3Pca
140ec 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67  cheRefCount(pPag
140ed 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20  er->pPCache);.  
140ee 61 5b 31 5d 20 3d 20 73 71 6c 69 74 65 33 50 63  a[1] = sqlite3Pc
140ef 61 63 68 65 50 61 67 65 63 6f 75 6e 74 28 70 50  achePagecount(pP
140f0 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a  ager->pPCache);.
140f1 20 20 61 5b 32 5d 20 3d 20 73 71 6c 69 74 65 33    a[2] = sqlite3
140f2 50 63 61 63 68 65 47 65 74 43 61 63 68 65 73 69  PcacheGetCachesi
140f3 7a 65 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63  ze(pPager->pPCac
140f4 68 65 29 3b 0a 20 20 61 5b 33 5d 20 3d 20 70 50  he);.  a[3] = pP
140f5 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69  ager->dbSizeVali
140f6 64 20 3f 20 28 69 6e 74 29 20 70 50 61 67 65 72  d ? (int) pPager
140f7 2d 3e 64 62 53 69 7a 65 20 3a 20 2d 31 3b 0a 20  ->dbSize : -1;. 
140f8 20 61 5b 34 5d 20 3d 20 70 50 61 67 65 72 2d 3e   a[4] = pPager->
140f9 73 74 61 74 65 3b 0a 20 20 61 5b 35 5d 20 3d 20  state;.  a[5] = 
140fa 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b  pPager->errCode;
140fb 0a 20 20 61 5b 36 5d 20 3d 20 70 50 61 67 65 72  .  a[6] = pPager
140fc 2d 3e 6e 48 69 74 3b 0a 20 20 61 5b 37 5d 20 3d  ->nHit;.  a[7] =
140fd 20 70 50 61 67 65 72 2d 3e 6e 4d 69 73 73 3b 0a   pPager->nMiss;.
140fe 20 20 61 5b 38 5d 20 3d 20 30 3b 20 20 2f 2a 20    a[8] = 0;  /* 
140ff 55 73 65 64 20 74 6f 20 62 65 20 70 50 61 67 65  Used to be pPage
14100 72 2d 3e 6e 4f 76 66 6c 20 2a 2f 0a 20 20 61 5b  r->nOvfl */.  a[
14101 39 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e 52 65  9] = pPager->nRe
14102 61 64 3b 0a 20 20 61 5b 31 30 5d 20 3d 20 70 50  ad;.  a[10] = pP
14103 61 67 65 72 2d 3e 6e 57 72 69 74 65 3b 0a 20 20  ager->nWrite;.  
14104 72 65 74 75 72 6e 20 61 3b 0a 7d 0a 23 65 6e 64  return a;.}.#end
14105 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  if../*.** Return
14106 20 74 72 75 65 20 69 66 20 74 68 69 73 20 69 73   true if this is
14107 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61   an in-memory pa
14108 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ger..*/.SQLITE_P
14109 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1410a 65 33 50 61 67 65 72 49 73 4d 65 6d 64 62 28 50  e3PagerIsMemdb(P
1410b 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
1410c 20 72 65 74 75 72 6e 20 4d 45 4d 44 42 3b 0a 7d   return MEMDB;.}
1410d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68  ../*.** Check th
1410e 61 74 20 74 68 65 72 65 20 61 72 65 20 61 74 20  at there are at 
1410f 6c 65 61 73 74 20 6e 53 61 76 65 70 6f 69 6e 74  least nSavepoint
14110 20 73 61 76 65 70 6f 69 6e 74 73 20 6f 70 65 6e   savepoints open
14111 2e 20 49 66 20 74 68 65 72 65 20 61 72 65 0a 2a  . If there are.*
14112 2a 20 63 75 72 72 65 6e 74 6c 79 20 6c 65 73 73  * currently less
14113 20 74 68 61 6e 20 6e 53 61 76 65 70 6f 69 6e 74   than nSavepoint
14114 73 20 6f 70 65 6e 2c 20 74 68 65 6e 20 6f 70 65  s open, then ope
14115 6e 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 61  n one or more sa
14116 76 65 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 6d  vepoints.** to m
14117 61 6b 65 20 75 70 20 74 68 65 20 64 69 66 66 65  ake up the diffe
14118 72 65 6e 63 65 2e 20 49 66 20 74 68 65 20 6e 75  rence. If the nu
14119 6d 62 65 72 20 6f 66 20 73 61 76 65 70 6f 69 6e  mber of savepoin
1411a 74 73 20 69 73 20 61 6c 72 65 61 64 79 0a 2a 2a  ts is already.**
1411b 20 65 71 75 61 6c 20 74 6f 20 6e 53 61 76 65 70   equal to nSavep
1411c 6f 69 6e 74 2c 20 74 68 65 6e 20 74 68 69 73 20  oint, then this 
1411d 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f  function is a no
1411e 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20  -op..**.** If a 
1411f 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
14120 6e 20 66 61 69 6c 73 2c 20 53 51 4c 49 54 45 5f  n fails, SQLITE_
14121 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65  NOMEM is returne
14122 64 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 0a  d. If an error .
14123 2a 2a 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20  ** occurs while 
14124 6f 70 65 6e 69 6e 67 20 74 68 65 20 73 75 62 2d  opening the sub-
14125 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68  journal file, th
14126 65 6e 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63  en an IO error c
14127 6f 64 65 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e  ode is.** return
14128 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53  ed. Otherwise, S
14129 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c  QLITE_OK..*/.SQL
1412a 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1412b 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e  sqlite3PagerOpen
1412c 53 61 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20  Savepoint(Pager 
1412d 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6e 53 61  *pPager, int nSa
1412e 76 65 70 6f 69 6e 74 29 7b 0a 20 20 69 6e 74 20  vepoint){.  int 
1412f 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  rc = SQLITE_OK; 
14130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14131 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
14132 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  code */.  int nC
14133 75 72 72 65 6e 74 20 3d 20 70 50 61 67 65 72 2d  urrent = pPager-
14134 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 20  >nSavepoint;    
14135 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e      /* Current n
14136 75 6d 62 65 72 20 6f 66 20 73 61 76 65 70 6f 69  umber of savepoi
14137 6e 74 73 20 2a 2f 0a 0a 20 20 69 66 28 20 6e 53  nts */..  if( nS
14138 61 76 65 70 6f 69 6e 74 3e 6e 43 75 72 72 65 6e  avepoint>nCurren
14139 74 20 26 26 20 70 50 61 67 65 72 2d 3e 75 73 65  t && pPager->use
1413a 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 69  Journal ){.    i
1413b 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20  nt ii;          
1413c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1413d 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74         /* Iterat
1413e 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20  or variable */. 
1413f 20 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e     PagerSavepoin
14140 74 20 2a 61 4e 65 77 3b 20 20 20 20 20 20 20 20  t *aNew;        
14141 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65             /* Ne
14142 77 20 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69  w Pager.aSavepoi
14143 6e 74 20 61 72 72 61 79 20 2a 2f 0a 0a 20 20 20  nt array */..   
14144 20 2f 2a 20 45 69 74 68 65 72 20 74 68 65 72 65   /* Either there
14145 20 69 73 20 6e 6f 20 61 63 74 69 76 65 20 6a 6f   is no active jo
14146 75 72 6e 61 6c 20 6f 72 20 74 68 65 20 73 75 62  urnal or the sub
14147 2d 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 70 65 6e  -journal is open
14148 20 6f 72 20 0a 20 20 20 20 2a 2a 20 74 68 65 20   or .    ** the 
14149 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6c 77 61 79  journal is alway
1414a 73 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f  s stored in memo
1414b 72 79 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74  ry */.    assert
1414c 28 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70  ( pPager->nSavep
1414d 6f 69 6e 74 3d 3d 30 20 7c 7c 20 69 73 4f 70 65  oint==0 || isOpe
1414e 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 20  n(pPager->sjfd) 
1414f 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  ||.            p
14150 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f  Pager->journalMo
14151 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de==PAGER_JOURNA
14152 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 3b 0a  LMODE_MEMORY );.
14153 0a 20 20 20 20 2f 2a 20 47 72 6f 77 20 74 68 65  .    /* Grow the
14154 20 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e   Pager.aSavepoin
14155 74 20 61 72 72 61 79 20 75 73 69 6e 67 20 72 65  t array using re
14156 61 6c 6c 6f 63 28 29 2e 20 52 65 74 75 72 6e 20  alloc(). Return 
14157 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 20 20 20  SQLITE_NOMEM.   
14158 20 2a 2a 20 69 66 20 74 68 65 20 61 6c 6c 6f 63   ** if the alloc
14159 61 74 69 6f 6e 20 66 61 69 6c 73 2e 20 4f 74 68  ation fails. Oth
1415a 65 72 77 69 73 65 2c 20 7a 65 72 6f 20 74 68 65  erwise, zero the
1415b 20 6e 65 77 20 70 6f 72 74 69 6f 6e 20 69 6e 20   new portion in 
1415c 63 61 73 65 20 61 20 0a 20 20 20 20 2a 2a 20 6d  case a .    ** m
1415d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f 63  alloc failure oc
1415e 63 75 72 73 20 77 68 69 6c 65 20 70 6f 70 75 6c  curs while popul
1415f 61 74 69 6e 67 20 69 74 20 69 6e 20 74 68 65 20  ating it in the 
14160 66 6f 72 28 2e 2e 2e 29 20 6c 6f 6f 70 20 62 65  for(...) loop be
14161 6c 6f 77 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  low..    */.    
14162 61 4e 65 77 20 3d 20 28 50 61 67 65 72 53 61 76  aNew = (PagerSav
14163 65 70 6f 69 6e 74 20 2a 29 73 71 6c 69 74 65 33  epoint *)sqlite3
14164 52 65 61 6c 6c 6f 63 28 0a 20 20 20 20 20 20 20  Realloc(.       
14165 20 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f   pPager->aSavepo
14166 69 6e 74 2c 20 73 69 7a 65 6f 66 28 50 61 67 65  int, sizeof(Page
14167 72 53 61 76 65 70 6f 69 6e 74 29 2a 6e 53 61 76  rSavepoint)*nSav
14168 65 70 6f 69 6e 74 0a 20 20 20 20 29 3b 0a 20 20  epoint.    );.  
14169 20 20 69 66 28 20 21 61 4e 65 77 20 29 7b 0a 20    if( !aNew ){. 
1416a 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
1416b 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a  TE_NOMEM;.    }.
1416c 20 20 20 20 6d 65 6d 73 65 74 28 26 61 4e 65 77      memset(&aNew
1416d 5b 6e 43 75 72 72 65 6e 74 5d 2c 20 30 2c 20 28  [nCurrent], 0, (
1416e 6e 53 61 76 65 70 6f 69 6e 74 2d 6e 43 75 72 72  nSavepoint-nCurr
1416f 65 6e 74 29 20 2a 20 73 69 7a 65 6f 66 28 50 61  ent) * sizeof(Pa
14170 67 65 72 53 61 76 65 70 6f 69 6e 74 29 29 3b 0a  gerSavepoint));.
14171 20 20 20 20 70 50 61 67 65 72 2d 3e 61 53 61 76      pPager->aSav
14172 65 70 6f 69 6e 74 20 3d 20 61 4e 65 77 3b 0a 20  epoint = aNew;. 
14173 20 20 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65     pPager->nSave
14174 70 6f 69 6e 74 20 3d 20 6e 53 61 76 65 70 6f 69  point = nSavepoi
14175 6e 74 3b 0a 0a 20 20 20 20 2f 2a 20 50 6f 70 75  nt;..    /* Popu
14176 6c 61 74 65 20 74 68 65 20 50 61 67 65 72 53 61  late the PagerSa
14177 76 65 70 6f 69 6e 74 20 73 74 72 75 63 74 75 72  vepoint structur
14178 65 73 20 6a 75 73 74 20 61 6c 6c 6f 63 61 74 65  es just allocate
14179 64 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 69  d. */.    for(ii
1417a 3d 6e 43 75 72 72 65 6e 74 3b 20 69 69 3c 6e 53  =nCurrent; ii<nS
1417b 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b  avepoint; ii++){
1417c 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1417d 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c  Pager->dbSizeVal
1417e 69 64 20 29 3b 0a 20 20 20 20 20 20 61 4e 65 77  id );.      aNew
1417f 5b 69 69 5d 2e 6e 4f 72 69 67 20 3d 20 70 50 61  [ii].nOrig = pPa
14180 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 20  ger->dbSize;.   
14181 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50     if( isOpen(pP
14182 61 67 65 72 2d 3e 6a 66 64 29 20 26 26 20 41 4c  ager->jfd) && AL
14183 57 41 59 53 28 70 50 61 67 65 72 2d 3e 6a 6f 75  WAYS(pPager->jou
14184 72 6e 61 6c 4f 66 66 3e 30 29 20 29 7b 0a 20 20  rnalOff>0) ){.  
14185 20 20 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 69        aNew[ii].i
14186 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 72 2d  Offset = pPager-
14187 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 20  >journalOff;.   
14188 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
14189 20 20 61 4e 65 77 5b 69 69 5d 2e 69 4f 66 66 73    aNew[ii].iOffs
1418a 65 74 20 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52  et = JOURNAL_HDR
1418b 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 20  _SZ(pPager);.   
1418c 20 20 20 7d 0a 20 20 20 20 20 20 61 4e 65 77 5b     }.      aNew[
1418d 69 69 5d 2e 69 53 75 62 52 65 63 20 3d 20 70 50  ii].iSubRec = pP
1418e 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 3b 0a 20  ager->nSubRec;. 
1418f 20 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 70 49       aNew[ii].pI
14190 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 73 71 6c  nSavepoint = sql
14191 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65  ite3BitvecCreate
14192 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29  (pPager->dbSize)
14193 3b 0a 20 20 20 20 20 20 69 66 28 20 21 61 4e 65  ;.      if( !aNe
14194 77 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69  w[ii].pInSavepoi
14195 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  nt ){.        re
14196 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
14197 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  M;.      }.    }
14198 0a 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68  ..    /* Open th
14199 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2c 20 69  e sub-journal, i
1419a 66 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 65  f it is not alre
1419b 61 64 79 20 6f 70 65 6e 65 64 2e 20 2a 2f 0a 20  ady opened. */. 
1419c 20 20 20 72 63 20 3d 20 6f 70 65 6e 53 75 62 4a     rc = openSubJ
1419d 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a  ournal(pPager);.
1419e 20 20 20 20 61 73 73 65 72 74 54 72 75 6e 63 61      assertTrunca
1419f 74 65 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61  teConstraint(pPa
141a0 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  ger);.  }..  ret
141a1 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
141a2 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
141a3 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 6f 6c 6c  s called to roll
141a4 62 61 63 6b 20 6f 72 20 72 65 6c 65 61 73 65 20  back or release 
141a5 28 63 6f 6d 6d 69 74 29 20 61 20 73 61 76 65 70  (commit) a savep
141a6 6f 69 6e 74 2e 0a 2a 2a 20 54 68 65 20 73 61 76  oint..** The sav
141a7 65 70 6f 69 6e 74 20 74 6f 20 72 65 6c 65 61 73  epoint to releas
141a8 65 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6e 65  e or rollback ne
141a9 65 64 20 6e 6f 74 20 62 65 20 74 68 65 20 6d 6f  ed not be the mo
141aa 73 74 20 72 65 63 65 6e 74 6c 79 20 0a 2a 2a 20  st recently .** 
141ab 63 72 65 61 74 65 64 20 73 61 76 65 70 6f 69 6e  created savepoin
141ac 74 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 74  t..**.** Paramet
141ad 65 72 20 6f 70 20 69 73 20 61 6c 77 61 79 73 20  er op is always 
141ae 65 69 74 68 65 72 20 53 41 56 45 50 4f 49 4e 54  either SAVEPOINT
141af 5f 52 4f 4c 4c 42 41 43 4b 20 6f 72 20 53 41 56  _ROLLBACK or SAV
141b0 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e 0a  EPOINT_RELEASE..
141b1 2a 2a 20 49 66 20 69 74 20 69 73 20 53 41 56 45  ** If it is SAVE
141b2 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20 74  POINT_RELEASE, t
141b3 68 65 6e 20 72 65 6c 65 61 73 65 20 61 6e 64 20  hen release and 
141b4 64 65 73 74 72 6f 79 20 74 68 65 20 73 61 76 65  destroy the save
141b5 70 6f 69 6e 74 20 77 69 74 68 0a 2a 2a 20 69 6e  point with.** in
141b6 64 65 78 20 69 53 61 76 65 70 6f 69 6e 74 2e 20  dex iSavepoint. 
141b7 49 66 20 69 74 20 69 73 20 53 41 56 45 50 4f 49  If it is SAVEPOI
141b8 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 74 68 65  NT_ROLLBACK, the
141b9 6e 20 72 6f 6c 6c 62 61 63 6b 20 61 6c 6c 20 63  n rollback all c
141ba 68 61 6e 67 65 73 0a 2a 2a 20 74 68 61 74 20 68  hanges.** that h
141bb 61 76 65 20 6f 63 63 75 72 72 65 64 20 73 69 6e  ave occurred sin
141bc 63 65 20 74 68 65 20 73 70 65 63 69 66 69 65 64  ce the specified
141bd 20 73 61 76 65 70 6f 69 6e 74 20 77 61 73 20 63   savepoint was c
141be 72 65 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  reated..**.** Th
141bf 65 20 73 61 76 65 70 6f 69 6e 74 20 74 6f 20 72  e savepoint to r
141c0 6f 6c 6c 62 61 63 6b 20 6f 72 20 72 65 6c 65 61  ollback or relea
141c1 73 65 20 69 73 20 69 64 65 6e 74 69 66 69 65 64  se is identified
141c2 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 0a 2a   by parameter .*
141c3 2a 20 69 53 61 76 65 70 6f 69 6e 74 2e 20 41 20  * iSavepoint. A 
141c4 76 61 6c 75 65 20 6f 66 20 30 20 6d 65 61 6e 73  value of 0 means
141c5 20 74 6f 20 6f 70 65 72 61 74 65 20 6f 6e 20 74   to operate on t
141c6 68 65 20 6f 75 74 65 72 6d 6f 73 74 20 73 61 76  he outermost sav
141c7 65 70 6f 69 6e 74 0a 2a 2a 20 28 74 68 65 20 66  epoint.** (the f
141c8 69 72 73 74 20 63 72 65 61 74 65 64 29 2e 20 41  irst created). A
141c9 20 76 61 6c 75 65 20 6f 66 20 28 50 61 67 65 72   value of (Pager
141ca 2e 6e 53 61 76 65 70 6f 69 6e 74 2d 31 29 20 6d  .nSavepoint-1) m
141cb 65 61 6e 73 20 6f 70 65 72 61 74 65 0a 2a 2a 20  eans operate.** 
141cc 6f 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  on the most rece
141cd 6e 74 6c 79 20 63 72 65 61 74 65 64 20 73 61 76  ntly created sav
141ce 65 70 6f 69 6e 74 2e 20 49 66 20 69 53 61 76 65  epoint. If iSave
141cf 70 6f 69 6e 74 20 69 73 20 67 72 65 61 74 65 72  point is greater
141d0 20 74 68 61 6e 0a 2a 2a 20 28 50 61 67 65 72 2e   than.** (Pager.
141d1 6e 53 61 76 65 70 6f 69 6e 74 2d 31 29 2c 20 74  nSavepoint-1), t
141d2 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
141d3 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a  n is a no-op..**
141d4 0a 2a 2a 20 49 66 20 61 20 6e 65 67 61 74 69 76  .** If a negativ
141d5 65 20 76 61 6c 75 65 20 69 73 20 70 61 73 73 65  e value is passe
141d6 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69  d to this functi
141d7 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72  on, then the cur
141d8 72 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74  rent.** transact
141d9 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61  ion is rolled ba
141da 63 6b 2e 20 54 68 69 73 20 69 73 20 64 69 66 66  ck. This is diff
141db 65 72 65 6e 74 20 74 6f 20 63 61 6c 6c 69 6e 67  erent to calling
141dc 20 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65   .** sqlite3Page
141dd 72 52 6f 6c 6c 62 61 63 6b 28 29 20 62 65 63 61  rRollback() beca
141de 75 73 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f  use this functio
141df 6e 20 64 6f 65 73 20 6e 6f 74 20 74 65 72 6d 69  n does not termi
141e0 6e 61 74 65 0a 2a 2a 20 74 68 65 20 74 72 61 6e  nate.** the tran
141e1 73 61 63 74 69 6f 6e 20 6f 72 20 75 6e 6c 6f 63  saction or unloc
141e2 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20  k the database, 
141e3 69 74 20 6a 75 73 74 20 72 65 73 74 6f 72 65 73  it just restores
141e4 20 74 68 65 20 0a 2a 2a 20 63 6f 6e 74 65 6e 74   the .** content
141e5 73 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  s of the databas
141e6 65 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61  e to its origina
141e7 6c 20 73 74 61 74 65 2e 20 0a 2a 2a 0a 2a 2a 20  l state. .**.** 
141e8 49 6e 20 61 6e 79 20 63 61 73 65 2c 20 61 6c 6c  In any case, all
141e9 20 73 61 76 65 70 6f 69 6e 74 73 20 77 69 74 68   savepoints with
141ea 20 61 6e 20 69 6e 64 65 78 20 67 72 65 61 74 65   an index greate
141eb 72 20 74 68 61 6e 20 69 53 61 76 65 70 6f 69 6e  r than iSavepoin
141ec 74 20 0a 2a 2a 20 61 72 65 20 64 65 73 74 72 6f  t .** are destro
141ed 79 65 64 2e 20 49 66 20 74 68 69 73 20 69 73 20  yed. If this is 
141ee 61 20 72 65 6c 65 61 73 65 20 6f 70 65 72 61 74  a release operat
141ef 69 6f 6e 20 28 6f 70 3d 3d 53 41 56 45 50 4f 49  ion (op==SAVEPOI
141f0 4e 54 5f 52 45 4c 45 41 53 45 29 2c 0a 2a 2a 20  NT_RELEASE),.** 
141f1 74 68 65 6e 20 73 61 76 65 70 6f 69 6e 74 20 69  then savepoint i
141f2 53 61 76 65 70 6f 69 6e 74 20 69 73 20 61 6c 73  Savepoint is als
141f3 6f 20 64 65 73 74 72 6f 79 65 64 2e 0a 2a 2a 0a  o destroyed..**.
141f4 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
141f5 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49   may return SQLI
141f6 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 61 20 6d 65  TE_NOMEM if a me
141f7 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
141f8 66 61 69 6c 73 2c 0a 2a 2a 20 6f 72 20 61 6e 20  fails,.** or an 
141f9 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66  IO error code if
141fa 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63   an IO error occ
141fb 75 72 73 20 77 68 69 6c 65 20 72 6f 6c 6c 69 6e  urs while rollin
141fc 67 20 62 61 63 6b 20 61 20 0a 2a 2a 20 73 61 76  g back a .** sav
141fd 65 70 6f 69 6e 74 2e 20 49 66 20 6e 6f 20 65 72  epoint. If no er
141fe 72 6f 72 73 20 6f 63 63 75 72 2c 20 53 51 4c 49  rors occur, SQLI
141ff 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
14200 64 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52  d..*/ .SQLITE_PR
14201 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
14202 33 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 28  3PagerSavepoint(
14203 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69  Pager *pPager, i
14204 6e 74 20 6f 70 2c 20 69 6e 74 20 69 53 61 76 65  nt op, int iSave
14205 70 6f 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63  point){.  int rc
14206 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20   = SQLITE_OK;.. 
14207 20 61 73 73 65 72 74 28 20 6f 70 3d 3d 53 41 56   assert( op==SAV
14208 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 7c  EPOINT_RELEASE |
14209 7c 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f  | op==SAVEPOINT_
1420a 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a 20 20 61 73  ROLLBACK );.  as
1420b 73 65 72 74 28 20 69 53 61 76 65 70 6f 69 6e 74  sert( iSavepoint
1420c 3e 3d 30 20 7c 7c 20 6f 70 3d 3d 53 41 56 45 50  >=0 || op==SAVEP
1420d 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b  OINT_ROLLBACK );
1420e 0a 0a 20 20 69 66 28 20 69 53 61 76 65 70 6f 69  ..  if( iSavepoi
1420f 6e 74 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65  nt<pPager->nSave
14210 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74  point ){.    int
14211 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20   ii;            
14212 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69  /* Iterator vari
14213 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20  able */.    int 
14214 6e 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20 2f  nNew;          /
14215 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 6d 61  * Number of rema
14216 69 6e 69 6e 67 20 73 61 76 65 70 6f 69 6e 74 73  ining savepoints
14217 20 61 66 74 65 72 20 74 68 69 73 20 6f 70 2e 20   after this op. 
14218 2a 2f 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75 72  */..    /* Figur
14219 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 73  e out how many s
1421a 61 76 65 70 6f 69 6e 74 73 20 77 69 6c 6c 20 73  avepoints will s
1421b 74 69 6c 6c 20 62 65 20 61 63 74 69 76 65 20 61  till be active a
1421c 66 74 65 72 20 74 68 69 73 0a 20 20 20 20 2a 2a  fter this.    **
1421d 20 6f 70 65 72 61 74 69 6f 6e 2e 20 53 74 6f 72   operation. Stor
1421e 65 20 74 68 69 73 20 76 61 6c 75 65 20 69 6e 20  e this value in 
1421f 6e 4e 65 77 2e 20 54 68 65 6e 20 66 72 65 65 20  nNew. Then free 
14220 72 65 73 6f 75 72 63 65 73 20 61 73 73 6f 63 69  resources associ
14221 61 74 65 64 20 0a 20 20 20 20 2a 2a 20 77 69 74  ated .    ** wit
14222 68 20 61 6e 79 20 73 61 76 65 70 6f 69 6e 74 73  h any savepoints
14223 20 74 68 61 74 20 61 72 65 20 64 65 73 74 72 6f   that are destro
14224 79 65 64 20 62 79 20 74 68 69 73 20 6f 70 65 72  yed by this oper
14225 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20  ation..    */.  
14226 20 20 6e 4e 65 77 20 3d 20 69 53 61 76 65 70 6f    nNew = iSavepo
14227 69 6e 74 20 2b 20 28 6f 70 3d 3d 53 41 56 45 50  int + (op==SAVEP
14228 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 29 3b 0a  OINT_ROLLBACK);.
14229 20 20 20 20 66 6f 72 28 69 69 3d 6e 4e 65 77 3b      for(ii=nNew;
1422a 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76   ii<pPager->nSav
1422b 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20  epoint; ii++){. 
1422c 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76       sqlite3Bitv
1422d 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 72  ecDestroy(pPager
1422e 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d  ->aSavepoint[ii]
1422f 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 29 3b 0a  .pInSavepoint);.
14230 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72      }.    pPager
14231 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 6e  ->nSavepoint = n
14232 4e 65 77 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20  New;..    /* If 
14233 74 68 69 73 20 69 73 20 61 20 72 6f 6c 6c 62 61  this is a rollba
14234 63 6b 20 6f 70 65 72 61 74 69 6f 6e 2c 20 70 6c  ck operation, pl
14235 61 79 62 61 63 6b 20 74 68 65 20 73 70 65 63 69  ayback the speci
14236 66 69 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 0a  fied savepoint..
14237 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69      ** If this i
14238 73 20 61 20 74 65 6d 70 2d 66 69 6c 65 2c 20 69  s a temp-file, i
14239 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 68  t is possible th
1423a 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  at the journal f
1423b 69 6c 65 20 68 61 73 0a 20 20 20 20 2a 2a 20 6e  ile has.    ** n
1423c 6f 74 20 79 65 74 20 62 65 65 6e 20 6f 70 65 6e  ot yet been open
1423d 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  ed. In this case
1423e 20 74 68 65 72 65 20 68 61 76 65 20 62 65 65 6e   there have been
1423f 20 6e 6f 20 63 68 61 6e 67 65 73 20 74 6f 0a 20   no changes to. 
14240 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61     ** the databa
14241 73 65 20 66 69 6c 65 2c 20 73 6f 20 74 68 65 20  se file, so the 
14242 70 6c 61 79 62 61 63 6b 20 6f 70 65 72 61 74 69  playback operati
14243 6f 6e 20 63 61 6e 20 62 65 20 73 6b 69 70 70 65  on can be skippe
14244 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  d..    */.    if
14245 28 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f  ( op==SAVEPOINT_
14246 52 4f 4c 4c 42 41 43 4b 20 26 26 20 69 73 4f 70  ROLLBACK && isOp
14247 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
14248 29 7b 0a 20 20 20 20 20 20 50 61 67 65 72 53 61  ){.      PagerSa
14249 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 70 6f  vepoint *pSavepo
1424a 69 6e 74 20 3d 20 28 6e 4e 65 77 3d 3d 30 29 3f  int = (nNew==0)?
1424b 30 3a 26 70 50 61 67 65 72 2d 3e 61 53 61 76 65  0:&pPager->aSave
1424c 70 6f 69 6e 74 5b 6e 4e 65 77 2d 31 5d 3b 0a 20  point[nNew-1];. 
1424d 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 50       rc = pagerP
1424e 6c 61 79 62 61 63 6b 53 61 76 65 70 6f 69 6e 74  laybackSavepoint
1424f 28 70 50 61 67 65 72 2c 20 70 53 61 76 65 70 6f  (pPager, pSavepo
14250 69 6e 74 29 3b 0a 20 20 20 20 20 20 61 73 73 65  int);.      asse
14251 72 74 28 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f  rt(rc!=SQLITE_DO
14252 4e 45 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20  NE);.    }.  .  
14253 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
14254 61 20 72 65 6c 65 61 73 65 20 6f 66 20 74 68 65  a release of the
14255 20 6f 75 74 65 72 6d 6f 73 74 20 73 61 76 65 70   outermost savep
14256 6f 69 6e 74 2c 20 74 72 75 6e 63 61 74 65 20 0a  oint, truncate .
14257 20 20 20 20 2a 2a 20 74 68 65 20 73 75 62 2d 6a      ** the sub-j
14258 6f 75 72 6e 61 6c 20 74 6f 20 7a 65 72 6f 20 62  ournal to zero b
14259 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20 2a 2f  ytes in size. */
1425a 0a 20 20 20 20 69 66 28 20 6e 4e 65 77 3d 3d 30  .    if( nNew==0
1425b 20 26 26 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e   && op==SAVEPOIN
1425c 54 5f 52 45 4c 45 41 53 45 20 26 26 20 69 73 4f  T_RELEASE && isO
1425d 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64  pen(pPager->sjfd
1425e 29 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  ) ){.      asser
1425f 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
14260 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   );.      rc = s
14261 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65  qlite3OsTruncate
14262 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 30  (pPager->sjfd, 0
14263 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  );.      pPager-
14264 3e 6e 53 75 62 52 65 63 20 3d 20 30 3b 0a 20 20  >nSubRec = 0;.  
14265 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
14266 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
14267 74 75 72 6e 20 74 68 65 20 66 75 6c 6c 20 70 61  turn the full pa
14268 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61  thname of the da
14269 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a  tabase file..*/.
1426a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
1426b 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
1426c 65 33 50 61 67 65 72 46 69 6c 65 6e 61 6d 65 28  e3PagerFilename(
1426d 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
1426e 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d    return pPager-
1426f 3e 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f  >zFilename;.}../
14270 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
14271 56 46 53 20 73 74 72 75 63 74 75 72 65 20 66 6f  VFS structure fo
14272 72 20 74 68 65 20 70 61 67 65 72 2e 0a 2a 2f 0a  r the pager..*/.
14273 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
14274 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 66 73  onst sqlite3_vfs
14275 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 56 66   *sqlite3PagerVf
14276 73 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  s(Pager *pPager)
14277 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65  {.  return pPage
14278 72 2d 3e 70 56 66 73 3b 0a 7d 0a 0a 2f 2a 0a 2a  r->pVfs;.}../*.*
14279 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66 69 6c  * Return the fil
1427a 65 20 68 61 6e 64 6c 65 20 66 6f 72 20 74 68 65  e handle for the
1427b 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
1427c 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74  ssociated.** wit
1427d 68 20 74 68 65 20 70 61 67 65 72 2e 20 20 54 68  h the pager.  Th
1427e 69 73 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20  is might return 
1427f 4e 55 4c 4c 20 69 66 20 74 68 65 20 66 69 6c 65  NULL if the file
14280 20 68 61 73 0a 2a 2a 20 6e 6f 74 20 79 65 74 20   has.** not yet 
14281 62 65 65 6e 20 6f 70 65 6e 65 64 2e 0a 2a 2f 0a  been opened..*/.
14282 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73  SQLITE_PRIVATE s
14283 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 73 71 6c  qlite3_file *sql
14284 69 74 65 33 50 61 67 65 72 46 69 6c 65 28 50 61  ite3PagerFile(Pa
14285 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
14286 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 66  return pPager->f
14287 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  d;.}../*.** Retu
14288 72 6e 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68  rn the full path
14289 6e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72  name of the jour
1428a 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c  nal file..*/.SQL
1428b 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
1428c 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 50  t char *sqlite3P
1428d 61 67 65 72 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28  agerJournalname(
1428e 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
1428f 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d    return pPager-
14290 3e 7a 4a 6f 75 72 6e 61 6c 3b 0a 7d 0a 0a 2f 2a  >zJournal;.}../*
14291 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  .** Return true 
14292 69 66 20 66 73 79 6e 63 28 29 20 63 61 6c 6c 73  if fsync() calls
14293 20 61 72 65 20 64 69 73 61 62 6c 65 64 20 66 6f   are disabled fo
14294 72 20 74 68 69 73 20 70 61 67 65 72 2e 20 20 52  r this pager.  R
14295 65 74 75 72 6e 20 46 41 4c 53 45 0a 2a 2a 20 69  eturn FALSE.** i
14296 66 20 66 73 79 6e 63 28 29 73 20 61 72 65 20 65  f fsync()s are e
14297 78 65 63 75 74 65 64 20 6e 6f 72 6d 61 6c 6c 79  xecuted normally
14298 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
14299 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
1429a 61 67 65 72 4e 6f 73 79 6e 63 28 50 61 67 65 72  agerNosync(Pager
1429b 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74   *pPager){.  ret
1429c 75 72 6e 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79  urn pPager->noSy
1429d 6e 63 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51  nc;.}..#ifdef SQ
1429e 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 2f  LITE_HAS_CODEC./
1429f 2a 0a 2a 2a 20 53 65 74 20 6f 72 20 72 65 74 72  *.** Set or retr
142a0 69 65 76 65 20 74 68 65 20 63 6f 64 65 63 20 66  ieve the codec f
142a1 6f 72 20 74 68 69 73 20 70 61 67 65 72 0a 2a 2f  or this pager.*/
142a2 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c  .static void sql
142a3 69 74 65 33 50 61 67 65 72 53 65 74 43 6f 64 65  ite3PagerSetCode
142a4 63 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  c(.  Pager *pPag
142a5 65 72 2c 0a 20 20 76 6f 69 64 20 2a 28 2a 78 43  er,.  void *(*xC
142a6 6f 64 65 63 29 28 76 6f 69 64 2a 2c 76 6f 69 64  odec)(void*,void
142a7 2a 2c 50 67 6e 6f 2c 69 6e 74 29 2c 0a 20 20 76  *,Pgno,int),.  v
142a8 6f 69 64 20 28 2a 78 43 6f 64 65 63 53 69 7a 65  oid (*xCodecSize
142a9 43 68 6e 67 29 28 76 6f 69 64 2a 2c 69 6e 74 2c  Chng)(void*,int,
142aa 69 6e 74 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78  int),.  void (*x
142ab 43 6f 64 65 63 46 72 65 65 29 28 76 6f 69 64 2a  CodecFree)(void*
142ac 29 2c 0a 20 20 76 6f 69 64 20 2a 70 43 6f 64 65  ),.  void *pCode
142ad 63 0a 29 7b 0a 20 20 69 66 28 20 70 50 61 67 65  c.){.  if( pPage
142ae 72 2d 3e 78 43 6f 64 65 63 46 72 65 65 20 29 20  r->xCodecFree ) 
142af 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 46 72  pPager->xCodecFr
142b0 65 65 28 70 50 61 67 65 72 2d 3e 70 43 6f 64 65  ee(pPager->pCode
142b1 63 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 78 43  c);.  pPager->xC
142b2 6f 64 65 63 20 3d 20 78 43 6f 64 65 63 3b 0a 20  odec = xCodec;. 
142b3 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 53   pPager->xCodecS
142b4 69 7a 65 43 68 6e 67 20 3d 20 78 43 6f 64 65 63  izeChng = xCodec
142b5 53 69 7a 65 43 68 6e 67 3b 0a 20 20 70 50 61 67  SizeChng;.  pPag
142b6 65 72 2d 3e 78 43 6f 64 65 63 46 72 65 65 20 3d  er->xCodecFree =
142b7 20 78 43 6f 64 65 63 46 72 65 65 3b 0a 20 20 70   xCodecFree;.  p
142b8 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 20 3d 20  Pager->pCodec = 
142b9 70 43 6f 64 65 63 3b 0a 20 20 70 61 67 65 72 52  pCodec;.  pagerR
142ba 65 70 6f 72 74 53 69 7a 65 28 70 50 61 67 65 72  eportSize(pPager
142bb 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  );.}.static void
142bc 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 47 65   *sqlite3PagerGe
142bd 74 43 6f 64 65 63 28 50 61 67 65 72 20 2a 70 50  tCodec(Pager *pP
142be 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20  ager){.  return 
142bf 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 3b 0a  pPager->pCodec;.
142c0 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65  }.#endif..#ifnde
142c1 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
142c2 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 4d  TOVACUUM./*.** M
142c3 6f 76 65 20 74 68 65 20 70 61 67 65 20 70 50 67  ove the page pPg
142c4 20 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 70 67 6e   to location pgn
142c5 6f 20 69 6e 20 74 68 65 20 66 69 6c 65 2e 0a 2a  o in the file..*
142c6 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 20  *.** There must 
142c7 62 65 20 6e 6f 20 72 65 66 65 72 65 6e 63 65 73  be no references
142c8 20 74 6f 20 74 68 65 20 70 61 67 65 20 70 72 65   to the page pre
142c9 76 69 6f 75 73 6c 79 20 6c 6f 63 61 74 65 64 20  viously located 
142ca 61 74 0a 2a 2a 20 70 67 6e 6f 20 28 77 68 69 63  at.** pgno (whic
142cb 68 20 77 65 20 63 61 6c 6c 20 70 50 67 4f 6c 64  h we call pPgOld
142cc 29 20 74 68 6f 75 67 68 20 74 68 61 74 20 70 61  ) though that pa
142cd 67 65 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f  ge is allowed to
142ce 20 62 65 0a 2a 2a 20 69 6e 20 63 61 63 68 65 2e   be.** in cache.
142cf 20 20 49 66 20 74 68 65 20 70 61 67 65 20 70 72    If the page pr
142d0 65 76 69 6f 75 73 6c 79 20 6c 6f 63 61 74 65 64  eviously located
142d1 20 61 74 20 70 67 6e 6f 20 69 73 20 6e 6f 74 20   at pgno is not 
142d2 61 6c 72 65 61 64 79 0a 2a 2a 20 69 6e 20 74 68  already.** in th
142d3 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  e rollback journ
142d4 61 6c 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 75  al, it is not pu
142d5 74 20 74 68 65 72 65 20 62 79 20 62 79 20 74 68  t there by by th
142d6 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a  is routine..**.*
142d7 2a 20 52 65 66 65 72 65 6e 63 65 73 20 74 6f 20  * References to 
142d8 74 68 65 20 70 61 67 65 20 70 50 67 20 72 65 6d  the page pPg rem
142d9 61 69 6e 20 76 61 6c 69 64 2e 20 55 70 64 61 74  ain valid. Updat
142da 69 6e 67 20 61 6e 79 0a 2a 2a 20 6d 65 74 61 2d  ing any.** meta-
142db 64 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20  data associated 
142dc 77 69 74 68 20 70 50 67 20 28 69 2e 65 2e 20 64  with pPg (i.e. d
142dd 61 74 61 20 73 74 6f 72 65 64 20 69 6e 20 74 68  ata stored in th
142de 65 20 6e 45 78 74 72 61 20 62 79 74 65 73 0a 2a  e nExtra bytes.*
142df 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 6c 6f 6e  * allocated alon
142e0 67 20 77 69 74 68 20 74 68 65 20 70 61 67 65 29  g with the page)
142e1 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e 73 69   is the responsi
142e2 62 69 6c 69 74 79 20 6f 66 20 74 68 65 20 63 61  bility of the ca
142e3 6c 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 72  ller..**.** A tr
142e4 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62  ansaction must b
142e5 65 20 61 63 74 69 76 65 20 77 68 65 6e 20 74 68  e active when th
142e6 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
142e7 6c 6c 65 64 2e 20 49 74 20 75 73 65 64 20 74 6f  lled. It used to
142e8 20 62 65 0a 2a 2a 20 72 65 71 75 69 72 65 64 20   be.** required 
142e9 74 68 61 74 20 61 20 73 74 61 74 65 6d 65 6e 74  that a statement
142ea 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73   transaction was
142eb 20 6e 6f 74 20 61 63 74 69 76 65 2c 20 62 75 74   not active, but
142ec 20 74 68 69 73 20 72 65 73 74 72 69 63 74 69 6f   this restrictio
142ed 6e 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 72 65  n.** has been re
142ee 6d 6f 76 65 64 20 28 43 52 45 41 54 45 20 49 4e  moved (CREATE IN
142ef 44 45 58 20 6e 65 65 64 73 20 74 6f 20 6d 6f 76  DEX needs to mov
142f0 65 20 61 20 70 61 67 65 20 77 68 65 6e 20 61 20  e a page when a 
142f1 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61  statement.** tra
142f2 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69  nsaction is acti
142f3 76 65 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ve)..**.** If th
142f4 65 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e  e fourth argumen
142f5 74 2c 20 69 73 43 6f 6d 6d 69 74 2c 20 69 73 20  t, isCommit, is 
142f6 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74  non-zero, then t
142f7 68 69 73 20 70 61 67 65 20 69 73 20 62 65 69 6e  his page is bein
142f8 67 0a 2a 2a 20 6d 6f 76 65 64 20 61 73 20 70 61  g.** moved as pa
142f9 72 74 20 6f 66 20 61 20 64 61 74 61 62 61 73 65  rt of a database
142fa 20 72 65 6f 72 67 61 6e 69 7a 61 74 69 6f 6e 20   reorganization 
142fb 6a 75 73 74 20 62 65 66 6f 72 65 20 74 68 65 20  just before the 
142fc 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20  transaction .** 
142fd 69 73 20 62 65 69 6e 67 20 63 6f 6d 6d 69 74 74  is being committ
142fe 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  ed. In this case
142ff 2c 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 65  , it is guarante
14300 65 64 20 74 68 61 74 20 74 68 65 20 64 61 74 61  ed that the data
14301 62 61 73 65 20 70 61 67 65 20 0a 2a 2a 20 70 50  base page .** pP
14302 67 20 72 65 66 65 72 73 20 74 6f 20 77 69 6c 6c  g refers to will
14303 20 6e 6f 74 20 62 65 20 77 72 69 74 74 65 6e 20   not be written 
14304 74 6f 20 61 67 61 69 6e 20 77 69 74 68 69 6e 20  to again within 
14305 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e  this transaction
14306 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ..**.** This fun
14307 63 74 69 6f 6e 20 6d 61 79 20 72 65 74 75 72 6e  ction may return
14308 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 6f 72   SQLITE_NOMEM or
14309 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64   an IO error cod
1430a 65 20 69 66 20 61 6e 20 65 72 72 6f 72 0a 2a 2a  e if an error.**
1430b 20 6f 63 63 75 72 73 2e 20 4f 74 68 65 72 77 69   occurs. Otherwi
1430c 73 65 2c 20 69 74 20 72 65 74 75 72 6e 73 20 53  se, it returns S
1430d 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c  QLITE_OK..*/.SQL
1430e 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1430f 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65  sqlite3PagerMove
14310 70 61 67 65 28 50 61 67 65 72 20 2a 70 50 61 67  page(Pager *pPag
14311 65 72 2c 20 44 62 50 61 67 65 20 2a 70 50 67 2c  er, DbPage *pPg,
14312 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 69 6e 74 20   Pgno pgno, int 
14313 69 73 43 6f 6d 6d 69 74 29 7b 0a 20 20 50 67 48  isCommit){.  PgH
14314 64 72 20 2a 70 50 67 4f 6c 64 3b 20 20 20 20 20  dr *pPgOld;     
14315 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
14316 20 70 61 67 65 20 62 65 69 6e 67 20 6f 76 65 72   page being over
14317 77 72 69 74 74 65 6e 2e 20 2a 2f 0a 20 20 50 67  written. */.  Pg
14318 6e 6f 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 20  no needSyncPgno 
14319 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4f 6c  = 0;       /* Ol
1431a 64 20 76 61 6c 75 65 20 6f 66 20 70 50 67 2d 3e  d value of pPg->
1431b 70 67 6e 6f 2c 20 69 66 20 73 79 6e 63 20 69 73  pgno, if sync is
1431c 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 69   required */.  i
1431d 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
1431e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
1431f 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
14320 50 67 6e 6f 20 6f 72 69 67 50 67 6e 6f 3b 20 20  Pgno origPgno;  
14321 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
14322 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67  The original pag
14323 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 0a 20 20 61  e number */..  a
14324 73 73 65 72 74 28 20 70 50 67 2d 3e 6e 52 65 66  ssert( pPg->nRef
14325 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  >0 );..  /* If t
14326 68 65 20 70 61 67 65 20 62 65 69 6e 67 20 6d 6f  he page being mo
14327 76 65 64 20 69 73 20 64 69 72 74 79 20 61 6e 64  ved is dirty and
14328 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 73 61   has not been sa
14329 76 65 64 20 62 79 20 74 68 65 20 6c 61 74 65 73  ved by the lates
1432a 74 0a 20 20 2a 2a 20 73 61 76 65 70 6f 69 6e 74  t.  ** savepoint
1432b 2c 20 74 68 65 6e 20 73 61 76 65 20 74 68 65 20  , then save the 
1432c 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73  current contents
1432d 20 6f 66 20 74 68 65 20 70 61 67 65 20 69 6e 74   of the page int
1432e 6f 20 74 68 65 20 0a 20 20 2a 2a 20 73 75 62 2d  o the .  ** sub-
1432f 6a 6f 75 72 6e 61 6c 20 6e 6f 77 2e 20 54 68 69  journal now. Thi
14330 73 20 69 73 20 72 65 71 75 69 72 65 64 20 74 6f  s is required to
14331 20 68 61 6e 64 6c 65 20 74 68 65 20 66 6f 6c 6c   handle the foll
14332 6f 77 69 6e 67 20 73 63 65 6e 61 72 69 6f 3a 0a  owing scenario:.
14333 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 42 45 47 49    **.  **   BEGI
14334 4e 3b 0a 20 20 2a 2a 20 20 20 20 20 3c 6a 6f 75  N;.  **     <jou
14335 72 6e 61 6c 20 70 61 67 65 20 58 2c 20 74 68 65  rnal page X, the
14336 6e 20 6d 6f 64 69 66 79 20 69 74 20 69 6e 20 6d  n modify it in m
14337 65 6d 6f 72 79 3e 0a 20 20 2a 2a 20 20 20 20 20  emory>.  **     
14338 53 41 56 45 50 4f 49 4e 54 20 6f 6e 65 3b 0a 20  SAVEPOINT one;. 
14339 20 2a 2a 20 20 20 20 20 20 20 3c 4d 6f 76 65 20   **       <Move 
1433a 70 61 67 65 20 58 20 74 6f 20 6c 6f 63 61 74 69  page X to locati
1433b 6f 6e 20 59 3e 0a 20 20 2a 2a 20 20 20 20 20 52  on Y>.  **     R
1433c 4f 4c 4c 42 41 43 4b 20 54 4f 20 6f 6e 65 3b 0a  OLLBACK TO one;.
1433d 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 61 67    **.  ** If pag
1433e 65 20 58 20 77 65 72 65 20 6e 6f 74 20 77 72 69  e X were not wri
1433f 74 74 65 6e 20 74 6f 20 74 68 65 20 73 75 62 2d  tten to the sub-
14340 6a 6f 75 72 6e 61 6c 20 68 65 72 65 2c 20 69 74  journal here, it
14341 20 77 6f 75 6c 64 20 6e 6f 74 0a 20 20 2a 2a 20   would not.  ** 
14342 62 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 72  be possible to r
14343 65 73 74 6f 72 65 20 69 74 73 20 63 6f 6e 74 65  estore its conte
14344 6e 74 73 20 77 68 65 6e 20 74 68 65 20 22 52 4f  nts when the "RO
14345 4c 4c 42 41 43 4b 20 54 4f 20 6f 6e 65 22 0a 20  LLBACK TO one". 
14346 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 77 65   ** statement we
14347 72 65 20 69 73 20 70 72 6f 63 65 73 73 65 64 2e  re is processed.
14348 0a 20 20 2a 2a 0a 20 20 2a 2a 20 73 75 62 6a 6f  .  **.  ** subjo
14349 75 72 6e 61 6c 50 61 67 65 28 29 20 6d 61 79 20  urnalPage() may 
1434a 6e 65 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65  need to allocate
1434b 20 73 70 61 63 65 20 74 6f 20 73 74 6f 72 65 20   space to store 
1434c 70 50 67 2d 3e 70 67 6e 6f 20 69 6e 74 6f 0a 20  pPg->pgno into. 
1434d 20 2a 2a 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20   ** one or more 
1434e 73 61 76 65 70 6f 69 6e 74 20 62 69 74 76 65 63  savepoint bitvec
1434f 73 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 72  s. This is the r
14350 65 61 73 6f 6e 20 74 68 69 73 20 66 75 6e 63 74  eason this funct
14351 69 6f 6e 0a 20 20 2a 2a 20 6d 61 79 20 72 65 74  ion.  ** may ret
14352 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
14353 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 67  ..  */.  if( pPg
14354 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49  ->flags&PGHDR_DI
14355 52 54 59 20 0a 20 20 20 26 26 20 73 75 62 6a 52  RTY .   && subjR
14356 65 71 75 69 72 65 73 50 61 67 65 28 70 50 67 29  equiresPage(pPg)
14357 0a 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b  .   && SQLITE_OK
14358 21 3d 28 72 63 20 3d 20 73 75 62 6a 6f 75 72 6e  !=(rc = subjourn
14359 61 6c 50 61 67 65 28 70 50 67 29 29 0a 20 20 29  alPage(pPg)).  )
1435a 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  {.    return rc;
1435b 0a 20 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 41  .  }..  PAGERTRA
1435c 43 45 28 28 22 4d 4f 56 45 20 25 64 20 70 61 67  CE(("MOVE %d pag
1435d 65 20 25 64 20 28 6e 65 65 64 53 79 6e 63 3d 25  e %d (needSync=%
1435e 64 29 20 6d 6f 76 65 73 20 74 6f 20 25 64 5c 6e  d) moves to %d\n
1435f 22 2c 20 0a 20 20 20 20 20 20 50 41 47 45 52 49  ", .      PAGERI
14360 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e  D(pPager), pPg->
14361 70 67 6e 6f 2c 20 28 70 50 67 2d 3e 66 6c 61 67  pgno, (pPg->flag
14362 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e  s&PGHDR_NEED_SYN
14363 43 29 3f 31 3a 30 2c 20 70 67 6e 6f 29 29 3b 0a  C)?1:0, pgno));.
14364 20 20 49 4f 54 52 41 43 45 28 28 22 4d 4f 56 45    IOTRACE(("MOVE
14365 20 25 70 20 25 64 20 25 64 5c 6e 22 2c 20 70 50   %p %d %d\n", pP
14366 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c  ager, pPg->pgno,
14367 20 70 67 6e 6f 29 29 0a 0a 20 20 2f 2a 20 49 66   pgno))..  /* If
14368 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6e 65 65   the journal nee
14369 64 73 20 74 6f 20 62 65 20 73 79 6e 63 28 29 65  ds to be sync()e
1436a 64 20 62 65 66 6f 72 65 20 70 61 67 65 20 70 50  d before page pP
1436b 67 2d 3e 70 67 6e 6f 20 63 61 6e 0a 20 20 2a 2a  g->pgno can.  **
1436c 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 2c 20   be written to, 
1436d 73 74 6f 72 65 20 70 50 67 2d 3e 70 67 6e 6f 20  store pPg->pgno 
1436e 69 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  in local variabl
1436f 65 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2e 0a  e needSyncPgno..
14370 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65    **.  ** If the
14371 20 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67 20 69   isCommit flag i
14372 73 20 73 65 74 2c 20 74 68 65 72 65 20 69 73 20  s set, there is 
14373 6e 6f 20 6e 65 65 64 20 74 6f 20 72 65 6d 65 6d  no need to remem
14374 62 65 72 20 74 68 61 74 0a 20 20 2a 2a 20 74 68  ber that.  ** th
14375 65 20 6a 6f 75 72 6e 61 6c 20 6e 65 65 64 73 20  e journal needs 
14376 74 6f 20 62 65 20 73 79 6e 63 28 29 65 64 20 62  to be sync()ed b
14377 65 66 6f 72 65 20 64 61 74 61 62 61 73 65 20 70  efore database p
14378 61 67 65 20 70 50 67 2d 3e 70 67 6e 6f 20 0a 20  age pPg->pgno . 
14379 20 2a 2a 20 63 61 6e 20 62 65 20 77 72 69 74 74   ** can be writt
1437a 65 6e 20 74 6f 2e 20 54 68 65 20 63 61 6c 6c 65  en to. The calle
1437b 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 70 72  r has already pr
1437c 6f 6d 69 73 65 64 20 6e 6f 74 20 74 6f 20 77 72  omised not to wr
1437d 69 74 65 20 74 6f 20 69 74 2e 0a 20 20 2a 2f 0a  ite to it..  */.
1437e 20 20 69 66 28 20 28 70 50 67 2d 3e 66 6c 61 67    if( (pPg->flag
1437f 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e  s&PGHDR_NEED_SYN
14380 43 29 20 26 26 20 21 69 73 43 6f 6d 6d 69 74 20  C) && !isCommit 
14381 29 7b 0a 20 20 20 20 6e 65 65 64 53 79 6e 63 50  ){.    needSyncP
14382 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b  gno = pPg->pgno;
14383 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 61 67  .    assert( pag
14384 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20  eInJournal(pPg) 
14385 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61  || pPg->pgno>pPa
14386 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20  ger->dbOrigSize 
14387 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
14388 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f  Pg->flags&PGHDR_
14389 44 49 52 54 59 20 29 3b 0a 20 20 20 20 61 73 73  DIRTY );.    ass
1438a 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 65 65  ert( pPager->nee
1438b 64 53 79 6e 63 20 29 3b 0a 20 20 7d 0a 0a 20 20  dSync );.  }..  
1438c 2f 2a 20 49 66 20 74 68 65 20 63 61 63 68 65 20  /* If the cache 
1438d 63 6f 6e 74 61 69 6e 73 20 61 20 70 61 67 65 20  contains a page 
1438e 77 69 74 68 20 70 61 67 65 2d 6e 75 6d 62 65 72  with page-number
1438f 20 70 67 6e 6f 2c 20 72 65 6d 6f 76 65 20 69 74   pgno, remove it
14390 0a 20 20 2a 2a 20 66 72 6f 6d 20 69 74 73 20 68  .  ** from its h
14391 61 73 68 20 63 68 61 69 6e 2e 20 41 6c 73 6f 2c  ash chain. Also,
14392 20 69 66 20 74 68 65 20 50 67 48 64 72 2e 6e 65   if the PgHdr.ne
14393 65 64 53 79 6e 63 20 77 61 73 20 73 65 74 20 66  edSync was set f
14394 6f 72 20 0a 20 20 2a 2a 20 70 61 67 65 20 70 67  or .  ** page pg
14395 6e 6f 20 62 65 66 6f 72 65 20 74 68 65 20 27 6d  no before the 'm
14396 6f 76 65 27 20 6f 70 65 72 61 74 69 6f 6e 2c 20  ove' operation, 
14397 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72  it needs to be r
14398 65 74 61 69 6e 65 64 20 0a 20 20 2a 2a 20 66 6f  etained .  ** fo
14399 72 20 74 68 65 20 70 61 67 65 20 6d 6f 76 65 64  r the page moved
1439a 20 74 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 70   there..  */.  p
1439b 50 67 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 50 47  Pg->flags &= ~PG
1439c 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20  HDR_NEED_SYNC;. 
1439d 20 70 50 67 4f 6c 64 20 3d 20 70 61 67 65 72 5f   pPgOld = pager_
1439e 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70  lookup(pPager, p
1439f 67 6e 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20  gno);.  assert( 
143a0 21 70 50 67 4f 6c 64 20 7c 7c 20 70 50 67 4f 6c  !pPgOld || pPgOl
143a1 64 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a 20 20  d->nRef==1 );.  
143a2 69 66 28 20 70 50 67 4f 6c 64 20 29 7b 0a 20 20  if( pPgOld ){.  
143a3 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20    pPg->flags |= 
143a4 28 70 50 67 4f 6c 64 2d 3e 66 6c 61 67 73 26 50  (pPgOld->flags&P
143a5 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3b  GHDR_NEED_SYNC);
143a6 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63  .    sqlite3Pcac
143a7 68 65 44 72 6f 70 28 70 50 67 4f 6c 64 29 3b 0a  heDrop(pPgOld);.
143a8 20 20 7d 0a 0a 20 20 6f 72 69 67 50 67 6e 6f 20    }..  origPgno 
143a9 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20 73  = pPg->pgno;.  s
143aa 71 6c 69 74 65 33 50 63 61 63 68 65 4d 6f 76 65  qlite3PcacheMove
143ab 28 70 50 67 2c 20 70 67 6e 6f 29 3b 0a 20 20 73  (pPg, pgno);.  s
143ac 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65  qlite3PcacheMake
143ad 44 69 72 74 79 28 70 50 67 29 3b 0a 20 20 70 50  Dirty(pPg);.  pP
143ae 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64  ager->dbModified
143af 20 3d 20 31 3b 0a 0a 20 20 69 66 28 20 6e 65 65   = 1;..  if( nee
143b0 64 53 79 6e 63 50 67 6e 6f 20 29 7b 0a 20 20 20  dSyncPgno ){.   
143b1 20 2f 2a 20 49 66 20 6e 65 65 64 53 79 6e 63 50   /* If needSyncP
143b2 67 6e 6f 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c  gno is non-zero,
143b3 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61   then the journa
143b4 6c 20 66 69 6c 65 20 6e 65 65 64 73 20 74 6f 20  l file needs to 
143b5 62 65 20 0a 20 20 20 20 2a 2a 20 73 79 6e 63 28  be .    ** sync(
143b6 29 65 64 20 62 65 66 6f 72 65 20 61 6e 79 20 64  )ed before any d
143b7 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 20 74  ata is written t
143b8 6f 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  o database file 
143b9 70 61 67 65 20 6e 65 65 64 53 79 6e 63 50 67 6e  page needSyncPgn
143ba 6f 2e 0a 20 20 20 20 2a 2a 20 43 75 72 72 65 6e  o..    ** Curren
143bb 74 6c 79 2c 20 6e 6f 20 73 75 63 68 20 70 61 67  tly, no such pag
143bc 65 20 65 78 69 73 74 73 20 69 6e 20 74 68 65 20  e exists in the 
143bd 70 61 67 65 2d 63 61 63 68 65 20 61 6e 64 20 74  page-cache and t
143be 68 65 20 0a 20 20 20 20 2a 2a 20 22 69 73 20 6a  he .    ** "is j
143bf 6f 75 72 6e 61 6c 65 64 22 20 62 69 74 76 65 63  ournaled" bitvec
143c0 20 66 6c 61 67 20 68 61 73 20 62 65 65 6e 20 73   flag has been s
143c1 65 74 2e 20 54 68 69 73 20 6e 65 65 64 73 20 74  et. This needs t
143c2 6f 20 62 65 20 72 65 6d 65 64 69 65 64 20 62 79  o be remedied by
143c3 0a 20 20 20 20 2a 2a 20 6c 6f 61 64 69 6e 67 20  .    ** loading 
143c4 74 68 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68  the page into th
143c5 65 20 70 61 67 65 72 2d 63 61 63 68 65 20 61 6e  e pager-cache an
143c6 64 20 73 65 74 74 69 6e 67 20 74 68 65 20 50 67  d setting the Pg
143c7 48 64 72 2e 6e 65 65 64 53 79 6e 63 20 0a 20 20  Hdr.needSync .  
143c8 20 20 2a 2a 20 66 6c 61 67 2e 0a 20 20 20 20 2a    ** flag..    *
143c9 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20  *.    ** If the 
143ca 61 74 74 65 6d 70 74 20 74 6f 20 6c 6f 61 64 20  attempt to load 
143cb 74 68 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68  the page into th
143cc 65 20 70 61 67 65 2d 63 61 63 68 65 20 66 61 69  e page-cache fai
143cd 6c 73 2c 20 28 64 75 65 0a 20 20 20 20 2a 2a 20  ls, (due.    ** 
143ce 74 6f 20 61 20 6d 61 6c 6c 6f 63 28 29 20 6f 72  to a malloc() or
143cf 20 49 4f 20 66 61 69 6c 75 72 65 29 2c 20 63 6c   IO failure), cl
143d0 65 61 72 20 74 68 65 20 62 69 74 20 69 6e 20 74  ear the bit in t
143d1 68 65 20 70 49 6e 4a 6f 75 72 6e 61 6c 5b 5d 0a  he pInJournal[].
143d2 20 20 20 20 2a 2a 20 61 72 72 61 79 2e 20 4f 74      ** array. Ot
143d3 68 65 72 77 69 73 65 2c 20 69 66 20 74 68 65 20  herwise, if the 
143d4 70 61 67 65 20 69 73 20 6c 6f 61 64 65 64 20 61  page is loaded a
143d5 6e 64 20 77 72 69 74 74 65 6e 20 61 67 61 69 6e  nd written again
143d6 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 69 73 20   in.    ** this 
143d7 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 74 20  transaction, it 
143d8 6d 61 79 20 62 65 20 77 72 69 74 74 65 6e 20 74  may be written t
143d9 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  o the database f
143da 69 6c 65 20 62 65 66 6f 72 65 0a 20 20 20 20 2a  ile before.    *
143db 2a 20 69 74 20 69 73 20 73 79 6e 63 65 64 20 69  * it is synced i
143dc 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  nto the journal 
143dd 66 69 6c 65 2e 20 54 68 69 73 20 77 61 79 2c 20  file. This way, 
143de 69 74 20 6d 61 79 20 65 6e 64 20 75 70 20 69 6e  it may end up in
143df 0a 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72  .    ** the jour
143e0 6e 61 6c 20 66 69 6c 65 20 74 77 69 63 65 2c 20  nal file twice, 
143e1 62 75 74 20 74 68 61 74 20 69 73 20 6e 6f 74 20  but that is not 
143e2 61 20 70 72 6f 62 6c 65 6d 2e 0a 20 20 20 20 2a  a problem..    *
143e3 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 71 6c  *.    ** The sql
143e4 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20 63  ite3PagerGet() c
143e5 61 6c 6c 20 6d 61 79 20 63 61 75 73 65 20 74 68  all may cause th
143e6 65 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 73 79 6e  e journal to syn
143e7 63 2e 20 53 6f 20 6d 61 6b 65 0a 20 20 20 20 2a  c. So make.    *
143e8 2a 20 73 75 72 65 20 74 68 65 20 50 61 67 65 72  * sure the Pager
143e9 2e 6e 65 65 64 53 79 6e 63 20 66 6c 61 67 20 69  .needSync flag i
143ea 73 20 73 65 74 20 74 6f 6f 2e 0a 20 20 20 20 2a  s set too..    *
143eb 2f 0a 20 20 20 20 50 67 48 64 72 20 2a 70 50 67  /.    PgHdr *pPg
143ec 48 64 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28  Hdr;.    assert(
143ed 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e   pPager->needSyn
143ee 63 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  c );.    rc = sq
143ef 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 50  lite3PagerGet(pP
143f0 61 67 65 72 2c 20 6e 65 65 64 53 79 6e 63 50 67  ager, needSyncPg
143f1 6e 6f 2c 20 26 70 50 67 48 64 72 29 3b 0a 20 20  no, &pPgHdr);.  
143f2 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
143f3 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 66 28  _OK ){.      if(
143f4 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 3c 3d 70   needSyncPgno<=p
143f5 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a  Pager->dbOrigSiz
143f6 65 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  e ){.        ass
143f7 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 54 6d  ert( pPager->pTm
143f8 70 53 70 61 63 65 21 3d 30 20 29 3b 0a 20 20 20  pSpace!=0 );.   
143f9 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76       sqlite3Bitv
143fa 65 63 43 6c 65 61 72 28 70 50 61 67 65 72 2d 3e  ecClear(pPager->
143fb 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 6e 65 65 64  pInJournal, need
143fc 53 79 6e 63 50 67 6e 6f 2c 20 70 50 61 67 65 72  SyncPgno, pPager
143fd 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20  ->pTmpSpace);.  
143fe 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75      }.      retu
143ff 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20  rn rc;.    }.   
14400 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e   pPager->needSyn
14401 63 20 3d 20 31 3b 0a 20 20 20 20 61 73 73 65 72  c = 1;.    asser
14402 74 28 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e  t( pPager->noSyn
14403 63 3d 3d 30 20 26 26 20 21 4d 45 4d 44 42 20 29  c==0 && !MEMDB )
14404 3b 0a 20 20 20 20 70 50 67 48 64 72 2d 3e 66 6c  ;.    pPgHdr->fl
14405 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45  ags |= PGHDR_NEE
14406 44 5f 53 59 4e 43 3b 0a 20 20 20 20 73 71 6c 69  D_SYNC;.    sqli
14407 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72  te3PcacheMakeDir
14408 74 79 28 70 50 67 48 64 72 29 3b 0a 20 20 20 20  ty(pPgHdr);.    
14409 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65  sqlite3PagerUnre
1440a 66 28 70 50 67 48 64 72 29 3b 0a 20 20 7d 0a 0a  f(pPgHdr);.  }..
1440b 20 20 2f 2a 0a 20 20 2a 2a 20 46 6f 72 20 61 6e    /*.  ** For an
1440c 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62   in-memory datab
1440d 61 73 65 2c 20 6d 61 6b 65 20 73 75 72 65 20 74  ase, make sure t
1440e 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65  he original page
1440f 20 63 6f 6e 74 69 6e 75 65 73 0a 20 20 2a 2a 20   continues.  ** 
14410 74 6f 20 65 78 69 73 74 2c 20 69 6e 20 63 61 73  to exist, in cas
14411 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  e the transactio
14412 6e 20 6e 65 65 64 73 20 74 6f 20 72 6f 6c 6c 20  n needs to roll 
14413 62 61 63 6b 2e 20 20 57 65 20 61 6c 6c 6f 63 61  back.  We alloca
14414 74 65 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 65  te.  ** the page
14415 20 6e 6f 77 2c 20 69 6e 73 74 65 61 64 20 6f 66   now, instead of
14416 20 61 74 20 72 6f 6c 6c 62 61 63 6b 2c 20 62 65   at rollback, be
14417 63 61 75 73 65 20 77 65 20 63 61 6e 20 62 65 74  cause we can bet
14418 74 65 72 20 64 65 61 6c 0a 20 20 2a 2a 20 77 69  ter deal.  ** wi
14419 74 68 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d 65 6d  th an out-of-mem
1441a 6f 72 79 20 65 72 72 6f 72 20 6e 6f 77 2e 20 20  ory error now.  
1441b 54 69 63 6b 65 74 20 23 33 37 36 31 2e 0a 20 20  Ticket #3761..  
1441c 2a 2f 0a 20 20 69 66 28 20 4d 45 4d 44 42 20 29  */.  if( MEMDB )
1441d 7b 0a 20 20 20 20 44 62 50 61 67 65 20 2a 70 4e  {.    DbPage *pN
1441e 65 77 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  ew;.    rc = sql
1441f 69 74 65 33 50 61 67 65 72 41 63 71 75 69 72 65  ite3PagerAcquire
14420 28 70 50 61 67 65 72 2c 20 6f 72 69 67 50 67 6e  (pPager, origPgn
14421 6f 2c 20 26 70 4e 65 77 2c 20 31 29 3b 0a 20 20  o, &pNew, 1);.  
14422 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
14423 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  _OK ){.      sql
14424 69 74 65 33 50 63 61 63 68 65 4d 6f 76 65 28 70  ite3PcacheMove(p
14425 50 67 2c 20 6f 72 69 67 50 67 6e 6f 29 3b 0a 20  Pg, origPgno);. 
14426 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
14427 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
14428 33 50 61 67 65 72 55 6e 72 65 66 28 70 4e 65 77  3PagerUnref(pNew
14429 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  );.  }..  return
1442a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65   SQLITE_OK;.}.#e
1442b 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ndif../*.** Retu
1442c 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
1442d 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 65  the data for the
1442e 20 73 70 65 63 69 66 69 65 64 20 70 61 67 65 2e   specified page.
1442f 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
14430 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
14431 50 61 67 65 72 47 65 74 44 61 74 61 28 44 62 50  PagerGetData(DbP
14432 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 61 73 73  age *pPg){.  ass
14433 65 72 74 28 20 70 50 67 2d 3e 6e 52 65 66 3e 30  ert( pPg->nRef>0
14434 20 7c 7c 20 70 50 67 2d 3e 70 50 61 67 65 72 2d   || pPg->pPager-
14435 3e 6d 65 6d 44 62 20 29 3b 0a 20 20 72 65 74 75  >memDb );.  retu
14436 72 6e 20 70 50 67 2d 3e 70 44 61 74 61 3b 0a 7d  rn pPg->pData;.}
14437 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ../*.** Return a
14438 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
14439 50 61 67 65 72 2e 6e 45 78 74 72 61 20 62 79 74  Pager.nExtra byt
1443a 65 73 20 6f 66 20 22 65 78 74 72 61 22 20 73 70  es of "extra" sp
1443b 61 63 65 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65  ace .** allocate
1443c 64 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65  d along with the
1443d 20 73 70 65 63 69 66 69 65 64 20 70 61 67 65 2e   specified page.
1443e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1443f 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
14440 50 61 67 65 72 47 65 74 45 78 74 72 61 28 44 62  PagerGetExtra(Db
14441 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 72 65  Page *pPg){.  re
14442 74 75 72 6e 20 70 50 67 2d 3e 70 45 78 74 72 61  turn pPg->pExtra
14443 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73  ;.}../*.** Get/s
14444 65 74 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d  et the locking-m
14445 6f 64 65 20 66 6f 72 20 74 68 69 73 20 70 61 67  ode for this pag
14446 65 72 2e 20 50 61 72 61 6d 65 74 65 72 20 65 4d  er. Parameter eM
14447 6f 64 65 20 6d 75 73 74 20 62 65 20 6f 6e 65 0a  ode must be one.
14448 2a 2a 20 6f 66 20 50 41 47 45 52 5f 4c 4f 43 4b  ** of PAGER_LOCK
14449 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 2c 20 50  INGMODE_QUERY, P
1444a 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
1444b 5f 4e 4f 52 4d 41 4c 20 6f 72 20 0a 2a 2a 20 50  _NORMAL or .** P
1444c 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
1444d 5f 45 58 43 4c 55 53 49 56 45 2e 20 49 66 20 74  _EXCLUSIVE. If t
1444e 68 65 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  he parameter is 
1444f 6e 6f 74 20 5f 51 55 45 52 59 2c 20 74 68 65 6e  not _QUERY, then
14450 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d  .** the locking-
14451 6d 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20 74  mode is set to t
14452 68 65 20 76 61 6c 75 65 20 73 70 65 63 69 66 69  he value specifi
14453 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65  ed..**.** The re
14454 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20  turned value is 
14455 65 69 74 68 65 72 20 50 41 47 45 52 5f 4c 4f 43  either PAGER_LOC
14456 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20  KINGMODE_NORMAL 
14457 6f 72 0a 2a 2a 20 50 41 47 45 52 5f 4c 4f 43 4b  or.** PAGER_LOCK
14458 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56  INGMODE_EXCLUSIV
14459 45 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68  E, indicating th
1445a 65 20 63 75 72 72 65 6e 74 20 28 70 6f 73 73 69  e current (possi
1445b 62 6c 79 20 75 70 64 61 74 65 64 29 0a 2a 2a 20  bly updated).** 
1445c 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 2e 0a 2a 2f  locking-mode..*/
1445d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1445e 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
1445f 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 50 61 67 65  LockingMode(Page
14460 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 65  r *pPager, int e
14461 4d 6f 64 65 29 7b 0a 20 20 61 73 73 65 72 74 28  Mode){.  assert(
14462 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f   eMode==PAGER_LO
14463 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 0a  CKINGMODE_QUERY.
14464 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65              || e
14465 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b  Mode==PAGER_LOCK
14466 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 0a 20  INGMODE_NORMAL. 
14467 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d             || eM
14468 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49  ode==PAGER_LOCKI
14469 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45  NGMODE_EXCLUSIVE
1446a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41   );.  assert( PA
1446b 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
1446c 51 55 45 52 59 3c 30 20 29 3b 0a 20 20 61 73 73  QUERY<0 );.  ass
1446d 65 72 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49  ert( PAGER_LOCKI
1446e 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 3e 3d 30  NGMODE_NORMAL>=0
1446f 20 26 26 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e   && PAGER_LOCKIN
14470 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 3e  GMODE_EXCLUSIVE>
14471 3d 30 20 29 3b 0a 20 20 69 66 28 20 65 4d 6f 64  =0 );.  if( eMod
14472 65 3e 3d 30 20 26 26 20 21 70 50 61 67 65 72 2d  e>=0 && !pPager-
14473 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20  >tempFile ){.   
14474 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69   pPager->exclusi
14475 76 65 4d 6f 64 65 20 3d 20 28 75 38 29 65 4d 6f  veMode = (u8)eMo
14476 64 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  de;.  }.  return
14477 20 28 69 6e 74 29 70 50 61 67 65 72 2d 3e 65 78   (int)pPager->ex
14478 63 6c 75 73 69 76 65 4d 6f 64 65 3b 0a 7d 0a 0a  clusiveMode;.}..
14479 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65 74 20 74 68  /*.** Get/set th
1447a 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 66  e journal-mode f
1447b 6f 72 20 74 68 69 73 20 70 61 67 65 72 2e 20 50  or this pager. P
1447c 61 72 61 6d 65 74 65 72 20 65 4d 6f 64 65 20 6d  arameter eMode m
1447d 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 3a 0a 2a  ust be one of:.*
1447e 2a 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f  *.**    PAGER_JO
1447f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 0a  URNALMODE_QUERY.
14480 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52  **    PAGER_JOUR
14481 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 0a 2a  NALMODE_DELETE.*
14482 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e  *    PAGER_JOURN
14483 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 0a  ALMODE_TRUNCATE.
14484 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52  **    PAGER_JOUR
14485 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a  NALMODE_PERSIST.
14486 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52  **    PAGER_JOUR
14487 4e 41 4c 4d 4f 44 45 5f 4f 46 46 0a 2a 2a 20 20  NALMODE_OFF.**  
14488 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d    PAGER_JOURNALM
14489 4f 44 45 5f 4d 45 4d 4f 52 59 0a 2a 2a 0a 2a 2a  ODE_MEMORY.**.**
1448a 20 49 66 20 74 68 65 20 70 61 72 61 6d 65 74 65   If the paramete
1448b 72 20 69 73 20 6e 6f 74 20 5f 51 55 45 52 59 2c  r is not _QUERY,
1448c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61   then the journa
1448d 6c 5f 6d 6f 64 65 20 69 73 20 73 65 74 20 74 6f  l_mode is set to
1448e 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 73 70   the.** value sp
1448f 65 63 69 66 69 65 64 20 69 66 20 74 68 65 20 63  ecified if the c
14490 68 61 6e 67 65 20 69 73 20 61 6c 6c 6f 77 65 64  hange is allowed
14491 2e 20 20 54 68 65 20 63 68 61 6e 67 65 20 69 73  .  The change is
14492 20 64 69 73 61 6c 6c 6f 77 65 64 0a 2a 2a 20 66   disallowed.** f
14493 6f 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  or the following
14494 20 72 65 61 73 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20   reasons:.**.** 
14495 20 20 2a 20 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72    *  An in-memor
14496 79 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 6f  y database can o
14497 6e 6c 79 20 68 61 76 65 20 69 74 73 20 6a 6f 75  nly have its jou
14498 72 6e 61 6c 5f 6d 6f 64 65 20 73 65 74 20 74 6f  rnal_mode set to
14499 20 5f 4f 46 46 0a 2a 2a 20 20 20 20 20 20 6f 72   _OFF.**      or
1449a 20 5f 4d 45 4d 4f 52 59 2e 0a 2a 2a 0a 2a 2a 20   _MEMORY..**.** 
1449b 20 20 2a 20 20 54 68 65 20 6a 6f 75 72 6e 61 6c    *  The journal
1449c 20 6d 6f 64 65 20 6d 61 79 20 6e 6f 74 20 62 65   mode may not be
1449d 20 63 68 61 6e 67 65 64 20 77 68 69 6c 65 20 61   changed while a
1449e 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
1449f 61 63 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  active..**.** Th
144a0 65 20 72 65 74 75 72 6e 65 64 20 69 6e 64 69 63  e returned indic
144a1 61 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ate the current 
144a2 28 70 6f 73 73 69 62 6c 79 20 75 70 64 61 74 65  (possibly update
144a3 64 29 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 2e  d) journal-mode.
144a4 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
144a5 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
144a6 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50  gerJournalMode(P
144a7 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e  ager *pPager, in
144a8 74 20 65 4d 6f 64 65 29 7b 0a 20 20 61 73 73 65  t eMode){.  asse
144a9 72 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52  rt( eMode==PAGER
144aa 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45  _JOURNALMODE_QUE
144ab 52 59 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c  RY.            |
144ac 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a  | eMode==PAGER_J
144ad 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54  OURNALMODE_DELET
144ae 45 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c  E.            ||
144af 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f   eMode==PAGER_JO
144b0 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41  URNALMODE_TRUNCA
144b1 54 45 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c  TE.            |
144b2 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a  | eMode==PAGER_J
144b3 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49  OURNALMODE_PERSI
144b4 53 54 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c  ST.            |
144b5 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a  | eMode==PAGER_J
144b6 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a  OURNALMODE_OFF .
144b7 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65              || e
144b8 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode==PAGER_JOUR
144b9 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29  NALMODE_MEMORY )
144ba 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 45  ;.  assert( PAGE
144bb 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55  R_JOURNALMODE_QU
144bc 45 52 59 3c 30 20 29 3b 0a 20 20 69 66 28 20 65  ERY<0 );.  if( e
144bd 4d 6f 64 65 3e 3d 30 0a 20 20 20 26 26 20 28 21  Mode>=0.   && (!
144be 4d 45 4d 44 42 20 7c 7c 20 65 4d 6f 64 65 3d 3d  MEMDB || eMode==
144bf 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
144c0 45 5f 4d 45 4d 4f 52 59 20 0a 20 20 20 20 20 20  E_MEMORY .      
144c1 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65          || eMode
144c2 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
144c3 4f 44 45 5f 4f 46 46 29 0a 20 20 20 26 26 20 21  ODE_OFF).   && !
144c4 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69  pPager->dbModifi
144c5 65 64 0a 20 20 20 26 26 20 28 21 69 73 4f 70 65  ed.   && (!isOpe
144c6 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c  n(pPager->jfd) |
144c7 7c 20 30 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75  | 0==pPager->jou
144c8 72 6e 61 6c 4f 66 66 29 0a 20 20 29 7b 0a 20 20  rnalOff).  ){.  
144c9 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61    if( isOpen(pPa
144ca 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20  ger->jfd) ){.   
144cb 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73     sqlite3OsClos
144cc 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a  e(pPager->jfd);.
144cd 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72      }.    pPager
144ce 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d 20  ->journalMode = 
144cf 28 75 38 29 65 4d 6f 64 65 3b 0a 20 20 7d 0a 20  (u8)eMode;.  }. 
144d0 20 72 65 74 75 72 6e 20 28 69 6e 74 29 70 50 61   return (int)pPa
144d1 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65  ger->journalMode
144d2 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73  ;.}../*.** Get/s
144d3 65 74 20 74 68 65 20 73 69 7a 65 2d 6c 69 6d 69  et the size-limi
144d4 74 20 75 73 65 64 20 66 6f 72 20 70 65 72 73 69  t used for persi
144d5 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69  stent journal fi
144d6 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 74 74 69  les..**.** Setti
144d7 6e 67 20 74 68 65 20 73 69 7a 65 20 6c 69 6d 69  ng the size limi
144d8 74 20 74 6f 20 2d 31 20 6d 65 61 6e 73 20 6e 6f  t to -1 means no
144d9 20 6c 69 6d 69 74 20 69 73 20 65 6e 66 6f 72 63   limit is enforc
144da 65 64 2e 0a 2a 2a 20 41 6e 20 61 74 74 65 6d 70  ed..** An attemp
144db 74 20 74 6f 20 73 65 74 20 61 20 6c 69 6d 69 74  t to set a limit
144dc 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 2d 31   smaller than -1
144dd 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a   is a no-op..*/.
144de 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
144df 36 34 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a  64 sqlite3PagerJ
144e0 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28  ournalSizeLimit(
144e1 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69  Pager *pPager, i
144e2 36 34 20 69 4c 69 6d 69 74 29 7b 0a 20 20 69 66  64 iLimit){.  if
144e3 28 20 69 4c 69 6d 69 74 3e 3d 2d 31 20 29 7b 0a  ( iLimit>=-1 ){.
144e4 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72      pPager->jour
144e5 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 3d 20 69  nalSizeLimit = i
144e6 4c 69 6d 69 74 3b 0a 20 20 7d 0a 20 20 72 65 74  Limit;.  }.  ret
144e7 75 72 6e 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  urn pPager->jour
144e8 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 3b 0a 7d 0a  nalSizeLimit;.}.
144e9 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  ./*.** Return a 
144ea 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70  pointer to the p
144eb 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 20 76  Pager->pBackup v
144ec 61 72 69 61 62 6c 65 2e 20 54 68 65 20 62 61 63  ariable. The bac
144ed 6b 75 70 20 6d 6f 64 75 6c 65 0a 2a 2a 20 69 6e  kup module.** in
144ee 20 62 61 63 6b 75 70 2e 63 20 6d 61 69 6e 74 61   backup.c mainta
144ef 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  ins the content 
144f0 6f 66 20 74 68 69 73 20 76 61 72 69 61 62 6c 65  of this variable
144f1 2e 20 54 68 69 73 20 6d 6f 64 75 6c 65 0a 2a 2a  . This module.**
144f2 20 75 73 65 73 20 69 74 20 6f 70 61 71 75 65 6c   uses it opaquel
144f3 79 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74  y as an argument
144f4 20 74 6f 20 73 71 6c 69 74 65 33 42 61 63 6b 75   to sqlite3Backu
144f5 70 52 65 73 74 61 72 74 28 29 20 61 6e 64 0a 2a  pRestart() and.*
144f6 2a 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55  * sqlite3BackupU
144f7 70 64 61 74 65 28 29 20 6f 6e 6c 79 2e 0a 2a 2f  pdate() only..*/
144f8 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
144f9 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a  sqlite3_backup *
144fa 2a 73 71 6c 69 74 65 33 50 61 67 65 72 42 61 63  *sqlite3PagerBac
144fb 6b 75 70 50 74 72 28 50 61 67 65 72 20 2a 70 50  kupPtr(Pager *pP
144fc 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20  ager){.  return 
144fd 26 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70  &pPager->pBackup
144fe 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53  ;.}..#endif /* S
144ff 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49  QLITE_OMIT_DISKI
14500 4f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  O */../*********
14501 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 67  ***** End of pag
14502 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  er.c ***********
14503 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14504 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14505 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
14506 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
14507 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a   btmutex.c *****
14508 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14509 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1450a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37  ****/./*.** 2007
1450b 20 41 75 67 75 73 74 20 32 37 0a 2a 2a 0a 2a 2a   August 27.**.**
1450c 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
1450d 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
1450e 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
1450f 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
14510 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
14511 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
14512 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
14513 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
14514 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
14515 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
14516 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
14517 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
14518 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
14519 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
1451a 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
1451b 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
1451c 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
1451d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1451e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1451f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14521 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
14522 24 49 64 3a 20 62 74 6d 75 74 65 78 2e 63 2c 76  $Id: btmutex.c,v
14523 20 31 2e 31 37 20 32 30 30 39 2f 30 37 2f 32 30   1.17 2009/07/20
14524 20 31 32 3a 33 33 3a 33 33 20 64 72 68 20 45 78   12:33:33 drh Ex
14525 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  p $.**.** This f
14526 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64  ile contains cod
14527 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d  e used to implem
14528 65 6e 74 20 6d 75 74 65 78 65 73 20 6f 6e 20 42  ent mutexes on B
14529 74 72 65 65 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a  tree objects..**
1452a 20 54 68 69 73 20 63 6f 64 65 20 72 65 61 6c 6c   This code reall
1452b 79 20 62 65 6c 6f 6e 67 73 20 69 6e 20 62 74 72  y belongs in btr
1452c 65 65 2e 63 2e 20 20 42 75 74 20 62 74 72 65 65  ee.c.  But btree
1452d 2e 63 20 69 73 20 67 65 74 74 69 6e 67 20 74 6f  .c is getting to
1452e 6f 0a 2a 2a 20 62 69 67 20 61 6e 64 20 77 65 20  o.** big and we 
1452f 77 61 6e 74 20 74 6f 20 62 72 65 61 6b 20 69 74  want to break it
14530 20 64 6f 77 6e 20 73 6f 6d 65 2e 20 20 54 68 69   down some.  Thi
14531 73 20 70 61 63 6b 61 67 65 64 20 73 65 65 6d 65  s packaged seeme
14532 64 20 6c 69 6b 65 0a 2a 2a 20 61 20 67 6f 6f 64  d like.** a good
14533 20 62 72 65 61 6b 6f 75 74 2e 0a 2a 2f 0a 2f 2a   breakout..*/./*
14534 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
14535 63 6c 75 64 65 20 62 74 72 65 65 49 6e 74 2e 68  clude btreeInt.h
14536 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
14537 66 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a  f btmutex.c ****
14538 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
14539 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
1453a 67 69 6e 20 66 69 6c 65 20 62 74 72 65 65 49 6e  gin file btreeIn
1453b 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
1453c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1453d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
1453e 0a 2a 2a 20 32 30 30 34 20 41 70 72 69 6c 20 36  .** 2004 April 6
1453f 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
14540 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
14541 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
14542 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
14543 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
14544 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
14545 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
14546 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
14547 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
14548 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
14549 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
1454a 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
1454b 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
1454c 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
1454d 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
1454e 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
1454f 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
14550 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
14551 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14552 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14553 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14554 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
14555 2a 2a 20 24 49 64 3a 20 62 74 72 65 65 49 6e 74  ** $Id: btreeInt
14556 2e 68 2c 76 20 31 2e 35 32 20 32 30 30 39 2f 30  .h,v 1.52 2009/0
14557 37 2f 31 35 20 31 37 3a 32 35 3a 34 36 20 64 72  7/15 17:25:46 dr
14558 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68  h Exp $.**.** Th
14559 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e  is file implemen
1455a 74 73 20 61 20 65 78 74 65 72 6e 61 6c 20 28 64  ts a external (d
1455b 69 73 6b 2d 62 61 73 65 64 29 20 64 61 74 61 62  isk-based) datab
1455c 61 73 65 20 75 73 69 6e 67 20 42 54 72 65 65 73  ase using BTrees
1455d 2e 0a 2a 2a 20 46 6f 72 20 61 20 64 65 74 61 69  ..** For a detai
1455e 6c 65 64 20 64 69 73 63 75 73 73 69 6f 6e 20 6f  led discussion o
1455f 66 20 42 54 72 65 65 73 2c 20 72 65 66 65 72 20  f BTrees, refer 
14560 74 6f 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 6f 6e  to.**.**     Don
14561 61 6c 64 20 45 2e 20 4b 6e 75 74 68 2c 20 54 48  ald E. Knuth, TH
14562 45 20 41 52 54 20 4f 46 20 43 4f 4d 50 55 54 45  E ART OF COMPUTE
14563 52 20 50 52 4f 47 52 41 4d 4d 49 4e 47 2c 20 56  R PROGRAMMING, V
14564 6f 6c 75 6d 65 20 33 3a 0a 2a 2a 20 20 20 20 20  olume 3:.**     
14565 22 53 6f 72 74 69 6e 67 20 41 6e 64 20 53 65 61  "Sorting And Sea
14566 72 63 68 69 6e 67 22 2c 20 70 61 67 65 73 20 34  rching", pages 4
14567 37 33 2d 34 38 30 2e 20 41 64 64 69 73 6f 6e 2d  73-480. Addison-
14568 57 65 73 6c 65 79 0a 2a 2a 20 20 20 20 20 50 75  Wesley.**     Pu
14569 62 6c 69 73 68 69 6e 67 20 43 6f 6d 70 61 6e 79  blishing Company
1456a 2c 20 52 65 61 64 69 6e 67 2c 20 4d 61 73 73 61  , Reading, Massa
1456b 63 68 75 73 65 74 74 73 2e 0a 2a 2a 0a 2a 2a 20  chusetts..**.** 
1456c 54 68 65 20 62 61 73 69 63 20 69 64 65 61 20 69  The basic idea i
1456d 73 20 74 68 61 74 20 65 61 63 68 20 70 61 67 65  s that each page
1456e 20 6f 66 20 74 68 65 20 66 69 6c 65 20 63 6f 6e   of the file con
1456f 74 61 69 6e 73 20 4e 20 64 61 74 61 62 61 73 65  tains N database
14570 0a 2a 2a 20 65 6e 74 72 69 65 73 20 61 6e 64 20  .** entries and 
14571 4e 2b 31 20 70 6f 69 6e 74 65 72 73 20 74 6f 20  N+1 pointers to 
14572 73 75 62 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20  subpages..**.** 
14573 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    --------------
14574 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
14575 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
14576 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
14577 2d 2d 0a 2a 2a 20 20 20 7c 20 20 50 74 72 28 30  --.**   |  Ptr(0
14578 29 20 7c 20 4b 65 79 28 30 29 20 7c 20 50 74 72  ) | Key(0) | Ptr
14579 28 31 29 20 7c 20 4b 65 79 28 31 29 20 7c 20 2e  (1) | Key(1) | .
1457a 2e 2e 20 7c 20 4b 65 79 28 4e 2d 31 29 20 7c 20  .. | Key(N-1) | 
1457b 50 74 72 28 4e 29 20 7c 0a 2a 2a 20 20 20 2d 2d  Ptr(N) |.**   --
1457c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1457d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1457e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1457f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a  --------------.*
14580 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20  *.** All of the 
14581 6b 65 79 73 20 6f 6e 20 74 68 65 20 70 61 67 65  keys on the page
14582 20 74 68 61 74 20 50 74 72 28 30 29 20 70 6f 69   that Ptr(0) poi
14583 6e 74 73 20 74 6f 20 68 61 76 65 20 76 61 6c 75  nts to have valu
14584 65 73 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20  es less.** than 
14585 4b 65 79 28 30 29 2e 20 20 41 6c 6c 20 6f 66 20  Key(0).  All of 
14586 74 68 65 20 6b 65 79 73 20 6f 6e 20 70 61 67 65  the keys on page
14587 20 50 74 72 28 31 29 20 61 6e 64 20 69 74 73 20   Ptr(1) and its 
14588 73 75 62 70 61 67 65 73 20 68 61 76 65 0a 2a 2a  subpages have.**
14589 20 76 61 6c 75 65 73 20 67 72 65 61 74 65 72 20   values greater 
1458a 74 68 61 6e 20 4b 65 79 28 30 29 20 61 6e 64 20  than Key(0) and 
1458b 6c 65 73 73 20 74 68 61 6e 20 4b 65 79 28 31 29  less than Key(1)
1458c 2e 20 20 41 6c 6c 20 6f 66 20 74 68 65 20 6b 65  .  All of the ke
1458d 79 73 0a 2a 2a 20 6f 6e 20 50 74 72 28 4e 29 20  ys.** on Ptr(N) 
1458e 61 6e 64 20 69 74 73 20 73 75 62 70 61 67 65 73  and its subpages
1458f 20 68 61 76 65 20 76 61 6c 75 65 73 20 67 72 65   have values gre
14590 61 74 65 72 20 74 68 61 6e 20 4b 65 79 28 4e 2d  ater than Key(N-
14591 31 29 2e 20 20 41 6e 64 0a 2a 2a 20 73 6f 20 66  1).  And.** so f
14592 6f 72 74 68 2e 0a 2a 2a 0a 2a 2a 20 46 69 6e 64  orth..**.** Find
14593 69 6e 67 20 61 20 70 61 72 74 69 63 75 6c 61 72  ing a particular
14594 20 6b 65 79 20 72 65 71 75 69 72 65 73 20 72 65   key requires re
14595 61 64 69 6e 67 20 4f 28 6c 6f 67 28 4d 29 29 20  ading O(log(M)) 
14596 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 0a  pages from the .
14597 2a 2a 20 64 69 73 6b 20 77 68 65 72 65 20 4d 20  ** disk where M 
14598 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
14599 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20   entries in the 
1459a 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74  tree..**.** In t
1459b 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  his implementati
1459c 6f 6e 2c 20 61 20 73 69 6e 67 6c 65 20 66 69 6c  on, a single fil
1459d 65 20 63 61 6e 20 68 6f 6c 64 20 6f 6e 65 20 6f  e can hold one o
1459e 72 20 6d 6f 72 65 20 73 65 70 61 72 61 74 65 20  r more separate 
1459f 0a 2a 2a 20 42 54 72 65 65 73 2e 20 20 45 61 63  .** BTrees.  Eac
145a0 68 20 42 54 72 65 65 20 69 73 20 69 64 65 6e 74  h BTree is ident
145a1 69 66 69 65 64 20 62 79 20 74 68 65 20 69 6e 64  ified by the ind
145a2 65 78 20 6f 66 20 69 74 73 20 72 6f 6f 74 20 70  ex of its root p
145a3 61 67 65 2e 20 20 54 68 65 0a 2a 2a 20 6b 65 79  age.  The.** key
145a4 20 61 6e 64 20 64 61 74 61 20 66 6f 72 20 61 6e   and data for an
145a5 79 20 65 6e 74 72 79 20 61 72 65 20 63 6f 6d 62  y entry are comb
145a6 69 6e 65 64 20 74 6f 20 66 6f 72 6d 20 74 68 65  ined to form the
145a7 20 22 70 61 79 6c 6f 61 64 22 2e 20 20 41 0a 2a   "payload".  A.*
145a8 2a 20 66 69 78 65 64 20 61 6d 6f 75 6e 74 20 6f  * fixed amount o
145a9 66 20 70 61 79 6c 6f 61 64 20 63 61 6e 20 62 65  f payload can be
145aa 20 63 61 72 72 69 65 64 20 64 69 72 65 63 74 6c   carried directl
145ab 79 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  y on the databas
145ac 65 0a 2a 2a 20 70 61 67 65 2e 20 20 49 66 20 74  e.** page.  If t
145ad 68 65 20 70 61 79 6c 6f 61 64 20 69 73 20 6c 61  he payload is la
145ae 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 70 72  rger than the pr
145af 65 73 65 74 20 61 6d 6f 75 6e 74 20 74 68 65 6e  eset amount then
145b0 20 73 75 72 70 6c 75 73 0a 2a 2a 20 62 79 74 65   surplus.** byte
145b1 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e 20  s are stored on 
145b2 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20  overflow pages. 
145b3 20 54 68 65 20 70 61 79 6c 6f 61 64 20 66 6f 72   The payload for
145b4 20 61 6e 20 65 6e 74 72 79 0a 2a 2a 20 61 6e 64   an entry.** and
145b5 20 74 68 65 20 70 72 65 63 65 64 69 6e 67 20 70   the preceding p
145b6 6f 69 6e 74 65 72 20 61 72 65 20 63 6f 6d 62 69  ointer are combi
145b7 6e 65 64 20 74 6f 20 66 6f 72 6d 20 61 20 22 43  ned to form a "C
145b8 65 6c 6c 22 2e 20 20 45 61 63 68 20 0a 2a 2a 20  ell".  Each .** 
145b9 70 61 67 65 20 68 61 73 20 61 20 73 6d 61 6c 6c  page has a small
145ba 20 68 65 61 64 65 72 20 77 68 69 63 68 20 63 6f   header which co
145bb 6e 74 61 69 6e 73 20 74 68 65 20 50 74 72 28 4e  ntains the Ptr(N
145bc 29 20 70 6f 69 6e 74 65 72 20 61 6e 64 20 6f 74  ) pointer and ot
145bd 68 65 72 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69  her.** informati
145be 6f 6e 20 73 75 63 68 20 61 73 20 74 68 65 20 73  on such as the s
145bf 69 7a 65 20 6f 66 20 6b 65 79 20 61 6e 64 20 64  ize of key and d
145c0 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 46 4f 52 4d 41  ata..**.** FORMA
145c1 54 20 44 45 54 41 49 4c 53 0a 2a 2a 0a 2a 2a 20  T DETAILS.**.** 
145c2 54 68 65 20 66 69 6c 65 20 69 73 20 64 69 76 69  The file is divi
145c3 64 65 64 20 69 6e 74 6f 20 70 61 67 65 73 2e 20  ded into pages. 
145c4 20 54 68 65 20 66 69 72 73 74 20 70 61 67 65 20   The first page 
145c5 69 73 20 63 61 6c 6c 65 64 20 70 61 67 65 20 31  is called page 1
145c6 2c 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 20  ,.** the second 
145c7 69 73 20 70 61 67 65 20 32 2c 20 61 6e 64 20 73  is page 2, and s
145c8 6f 20 66 6f 72 74 68 2e 20 20 41 20 70 61 67 65  o forth.  A page
145c9 20 6e 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f 20   number of zero 
145ca 69 6e 64 69 63 61 74 65 73 0a 2a 2a 20 22 6e 6f  indicates.** "no
145cb 20 73 75 63 68 20 70 61 67 65 22 2e 20 20 54 68   such page".  Th
145cc 65 20 70 61 67 65 20 73 69 7a 65 20 63 61 6e 20  e page size can 
145cd 62 65 20 61 6e 79 74 68 69 6e 67 20 62 65 74 77  be anything betw
145ce 65 65 6e 20 35 31 32 20 61 6e 64 20 36 35 35 33  een 512 and 6553
145cf 36 2e 0a 2a 2a 20 45 61 63 68 20 70 61 67 65 20  6..** Each page 
145d0 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 61 20  can be either a 
145d1 62 74 72 65 65 20 70 61 67 65 2c 20 61 20 66 72  btree page, a fr
145d2 65 65 6c 69 73 74 20 70 61 67 65 20 6f 72 20 61  eelist page or a
145d3 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 70 61  n overflow.** pa
145d4 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  ge..**.** The fi
145d5 72 73 74 20 70 61 67 65 20 69 73 20 61 6c 77 61  rst page is alwa
145d6 79 73 20 61 20 62 74 72 65 65 20 70 61 67 65 2e  ys a btree page.
145d7 20 20 54 68 65 20 66 69 72 73 74 20 31 30 30 20    The first 100 
145d8 62 79 74 65 73 20 6f 66 20 74 68 65 20 66 69 72  bytes of the fir
145d9 73 74 0a 2a 2a 20 70 61 67 65 20 63 6f 6e 74 61  st.** page conta
145da 69 6e 20 61 20 73 70 65 63 69 61 6c 20 68 65 61  in a special hea
145db 64 65 72 20 28 74 68 65 20 22 66 69 6c 65 20 68  der (the "file h
145dc 65 61 64 65 72 22 29 20 74 68 61 74 20 64 65 73  eader") that des
145dd 63 72 69 62 65 73 20 74 68 65 20 66 69 6c 65 2e  cribes the file.
145de 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74 20 6f  .** The format o
145df 66 20 74 68 65 20 66 69 6c 65 20 68 65 61 64 65  f the file heade
145e0 72 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  r is as follows:
145e1 0a 2a 2a 0a 2a 2a 20 20 20 4f 46 46 53 45 54 20  .**.**   OFFSET 
145e2 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 49    SIZE    DESCRI
145e3 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 30 20  PTION.**      0 
145e4 20 20 20 20 20 31 36 20 20 20 20 20 48 65 61 64       16     Head
145e5 65 72 20 73 74 72 69 6e 67 3a 20 22 53 51 4c 69  er string: "SQLi
145e6 74 65 20 66 6f 72 6d 61 74 20 33 5c 30 30 30 22  te format 3\000"
145e7 0a 2a 2a 20 20 20 20 20 31 36 20 20 20 20 20 20  .**     16      
145e8 20 32 20 20 20 20 20 50 61 67 65 20 73 69 7a 65   2     Page size
145e9 20 69 6e 20 62 79 74 65 73 2e 20 20 0a 2a 2a 20   in bytes.  .** 
145ea 20 20 20 20 31 38 20 20 20 20 20 20 20 31 20 20      18       1  
145eb 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 77     File format w
145ec 72 69 74 65 20 76 65 72 73 69 6f 6e 0a 2a 2a 20  rite version.** 
145ed 20 20 20 20 31 39 20 20 20 20 20 20 20 31 20 20      19       1  
145ee 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 72     File format r
145ef 65 61 64 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 20  ead version.**  
145f0 20 20 20 32 30 20 20 20 20 20 20 20 31 20 20 20     20       1   
145f1 20 20 42 79 74 65 73 20 6f 66 20 75 6e 75 73 65    Bytes of unuse
145f2 64 20 73 70 61 63 65 20 61 74 20 74 68 65 20 65  d space at the e
145f3 6e 64 20 6f 66 20 65 61 63 68 20 70 61 67 65 0a  nd of each page.
145f4 2a 2a 20 20 20 20 20 32 31 20 20 20 20 20 20 20  **     21       
145f5 31 20 20 20 20 20 4d 61 78 20 65 6d 62 65 64 64  1     Max embedd
145f6 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74  ed payload fract
145f7 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 32 20 20 20  ion.**     22   
145f8 20 20 20 20 31 20 20 20 20 20 4d 69 6e 20 65 6d      1     Min em
145f9 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66  bedded payload f
145fa 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 32  raction.**     2
145fb 33 20 20 20 20 20 20 20 31 20 20 20 20 20 4d 69  3       1     Mi
145fc 6e 20 6c 65 61 66 20 70 61 79 6c 6f 61 64 20 66  n leaf payload f
145fd 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 32  raction.**     2
145fe 34 20 20 20 20 20 20 20 34 20 20 20 20 20 46 69  4       4     Fi
145ff 6c 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65  le change counte
14600 72 0a 2a 2a 20 20 20 20 20 32 38 20 20 20 20 20  r.**     28     
14601 20 20 34 20 20 20 20 20 52 65 73 65 72 76 65 64    4     Reserved
14602 20 66 6f 72 20 66 75 74 75 72 65 20 75 73 65 0a   for future use.
14603 2a 2a 20 20 20 20 20 33 32 20 20 20 20 20 20 20  **     32       
14604 34 20 20 20 20 20 46 69 72 73 74 20 66 72 65 65  4     First free
14605 6c 69 73 74 20 70 61 67 65 0a 2a 2a 20 20 20 20  list page.**    
14606 20 33 36 20 20 20 20 20 20 20 34 20 20 20 20 20   36       4     
14607 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 6c 69  Number of freeli
14608 73 74 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  st pages in the 
14609 66 69 6c 65 0a 2a 2a 20 20 20 20 20 34 30 20 20  file.**     40  
1460a 20 20 20 20 36 30 20 20 20 20 20 31 35 20 34 2d      60     15 4-
1460b 62 79 74 65 20 6d 65 74 61 20 76 61 6c 75 65 73  byte meta values
1460c 20 70 61 73 73 65 64 20 74 6f 20 68 69 67 68 65   passed to highe
1460d 72 20 6c 61 79 65 72 73 0a 2a 2a 0a 2a 2a 20 20  r layers.**.**  
1460e 20 20 20 34 30 20 20 20 20 20 20 20 34 20 20 20     40       4   
1460f 20 20 53 63 68 65 6d 61 20 63 6f 6f 6b 69 65 0a    Schema cookie.
14610 2a 2a 20 20 20 20 20 34 34 20 20 20 20 20 20 20  **     44       
14611 34 20 20 20 20 20 46 69 6c 65 20 66 6f 72 6d 61  4     File forma
14612 74 20 6f 66 20 73 63 68 65 6d 61 20 6c 61 79 65  t of schema laye
14613 72 0a 2a 2a 20 20 20 20 20 34 38 20 20 20 20 20  r.**     48     
14614 20 20 34 20 20 20 20 20 53 69 7a 65 20 6f 66 20    4     Size of 
14615 70 61 67 65 20 63 61 63 68 65 0a 2a 2a 20 20 20  page cache.**   
14616 20 20 35 32 20 20 20 20 20 20 20 34 20 20 20 20    52       4    
14617 20 4c 61 72 67 65 73 74 20 72 6f 6f 74 2d 70 61   Largest root-pa
14618 67 65 20 28 61 75 74 6f 2f 69 6e 63 72 5f 76 61  ge (auto/incr_va
14619 63 75 75 6d 29 0a 2a 2a 20 20 20 20 20 35 36 20  cuum).**     56 
1461a 20 20 20 20 20 20 34 20 20 20 20 20 31 3d 55 54        4     1=UT
1461b 46 2d 38 20 32 3d 55 54 46 31 36 6c 65 20 33 3d  F-8 2=UTF16le 3=
1461c 55 54 46 31 36 62 65 0a 2a 2a 20 20 20 20 20 36  UTF16be.**     6
1461d 30 20 20 20 20 20 20 20 34 20 20 20 20 20 55 73  0       4     Us
1461e 65 72 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 20 20  er version.**   
1461f 20 20 36 34 20 20 20 20 20 20 20 34 20 20 20 20    64       4    
14620 20 49 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63   Incremental vac
14621 75 75 6d 20 6d 6f 64 65 0a 2a 2a 20 20 20 20 20  uum mode.**     
14622 36 38 20 20 20 20 20 20 20 34 20 20 20 20 20 75  68       4     u
14623 6e 75 73 65 64 0a 2a 2a 20 20 20 20 20 37 32 20  nused.**     72 
14624 20 20 20 20 20 20 34 20 20 20 20 20 75 6e 75 73        4     unus
14625 65 64 0a 2a 2a 20 20 20 20 20 37 36 20 20 20 20  ed.**     76    
14626 20 20 20 34 20 20 20 20 20 75 6e 75 73 65 64 0a     4     unused.
14627 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65  **.** All of the
14628 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 20   integer values 
14629 61 72 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 28  are big-endian (
1462a 6d 6f 73 74 20 73 69 67 6e 69 66 69 63 61 6e 74  most significant
1462b 20 62 79 74 65 20 66 69 72 73 74 29 2e 0a 2a 2a   byte first)..**
1462c 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 63 68 61  .** The file cha
1462d 6e 67 65 20 63 6f 75 6e 74 65 72 20 69 73 20 69  nge counter is i
1462e 6e 63 72 65 6d 65 6e 74 65 64 20 77 68 65 6e 20  ncremented when 
1462f 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20  the database is 
14630 63 68 61 6e 67 65 64 0a 2a 2a 20 54 68 69 73 20  changed.** This 
14631 63 6f 75 6e 74 65 72 20 61 6c 6c 6f 77 73 20 6f  counter allows o
14632 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 74  ther processes t
14633 6f 20 6b 6e 6f 77 20 77 68 65 6e 20 74 68 65 20  o know when the 
14634 66 69 6c 65 20 68 61 73 20 63 68 61 6e 67 65 64  file has changed
14635 0a 2a 2a 20 61 6e 64 20 74 68 75 73 20 77 68 65  .** and thus whe
14636 6e 20 74 68 65 79 20 6e 65 65 64 20 74 6f 20 66  n they need to f
14637 6c 75 73 68 20 74 68 65 69 72 20 63 61 63 68 65  lush their cache
14638 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 20  ..**.** The max 
14639 65 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64  embedded payload
1463a 20 66 72 61 63 74 69 6f 6e 20 69 73 20 74 68 65   fraction is the
1463b 20 61 6d 6f 75 6e 74 20 6f 66 20 74 68 65 20 74   amount of the t
1463c 6f 74 61 6c 20 75 73 61 62 6c 65 0a 2a 2a 20 73  otal usable.** s
1463d 70 61 63 65 20 69 6e 20 61 20 70 61 67 65 20 74  pace in a page t
1463e 68 61 74 20 63 61 6e 20 62 65 20 63 6f 6e 73 75  hat can be consu
1463f 6d 65 64 20 62 79 20 61 20 73 69 6e 67 6c 65 20  med by a single 
14640 63 65 6c 6c 20 66 6f 72 20 73 74 61 6e 64 61 72  cell for standar
14641 64 0a 2a 2a 20 42 2d 74 72 65 65 20 28 6e 6f 6e  d.** B-tree (non
14642 2d 4c 45 41 46 44 41 54 41 29 20 74 61 62 6c 65  -LEAFDATA) table
14643 73 2e 20 20 41 20 76 61 6c 75 65 20 6f 66 20 32  s.  A value of 2
14644 35 35 20 6d 65 61 6e 73 20 31 30 30 25 2e 20 20  55 means 100%.  
14645 54 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 69  The default.** i
14646 73 20 74 6f 20 6c 69 6d 69 74 20 74 68 65 20 6d  s to limit the m
14647 61 78 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65  aximum cell size
14648 20 73 6f 20 74 68 61 74 20 61 74 20 6c 65 61 73   so that at leas
14649 74 20 34 20 63 65 6c 6c 73 20 77 69 6c 6c 20 66  t 4 cells will f
1464a 69 74 0a 2a 2a 20 6f 6e 20 6f 6e 65 20 70 61 67  it.** on one pag
1464b 65 2e 20 20 54 68 75 73 20 74 68 65 20 64 65 66  e.  Thus the def
1464c 61 75 6c 74 20 6d 61 78 20 65 6d 62 65 64 64 65  ault max embedde
1464d 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69  d payload fracti
1464e 6f 6e 20 69 73 20 36 34 2e 0a 2a 2a 0a 2a 2a 20  on is 64..**.** 
1464f 49 66 20 74 68 65 20 70 61 79 6c 6f 61 64 20 66  If the payload f
14650 6f 72 20 61 20 63 65 6c 6c 20 69 73 20 6c 61 72  or a cell is lar
14651 67 65 72 20 74 68 61 6e 20 74 68 65 20 6d 61 78  ger than the max
14652 20 70 61 79 6c 6f 61 64 2c 20 74 68 65 6e 20 65   payload, then e
14653 78 74 72 61 0a 2a 2a 20 70 61 79 6c 6f 61 64 20  xtra.** payload 
14654 69 73 20 73 70 69 6c 6c 65 64 20 74 6f 20 6f 76  is spilled to ov
14655 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 20 4f  erflow pages.  O
14656 6e 63 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20  nce an overflow 
14657 70 61 67 65 20 69 73 20 61 6c 6c 6f 63 61 74 65  page is allocate
14658 64 2c 0a 2a 2a 20 61 73 20 6d 61 6e 79 20 62 79  d,.** as many by
14659 74 65 73 20 61 73 20 70 6f 73 73 69 62 6c 65 20  tes as possible 
1465a 61 72 65 20 6d 6f 76 65 64 20 69 6e 74 6f 20 74  are moved into t
1465b 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  he overflow page
1465c 73 20 77 69 74 68 6f 75 74 20 6c 65 74 74 69 6e  s without lettin
1465d 67 0a 2a 2a 20 74 68 65 20 63 65 6c 6c 20 73 69  g.** the cell si
1465e 7a 65 20 64 72 6f 70 20 62 65 6c 6f 77 20 74 68  ze drop below th
1465f 65 20 6d 69 6e 20 65 6d 62 65 64 64 65 64 20 70  e min embedded p
14660 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 2e  ayload fraction.
14661 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 69 6e 20 6c  .**.** The min l
14662 65 61 66 20 70 61 79 6c 6f 61 64 20 66 72 61 63  eaf payload frac
14663 74 69 6f 6e 20 69 73 20 6c 69 6b 65 20 74 68 65  tion is like the
14664 20 6d 69 6e 20 65 6d 62 65 64 64 65 64 20 70 61   min embedded pa
14665 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a  yload fraction.*
14666 2a 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74  * except that it
14667 20 61 70 70 6c 69 65 73 20 74 6f 20 6c 65 61 66   applies to leaf
14668 20 6e 6f 64 65 73 20 69 6e 20 61 20 4c 45 41 46   nodes in a LEAF
14669 44 41 54 41 20 74 72 65 65 2e 20 20 54 68 65 20  DATA tree.  The 
1466a 6d 61 78 69 6d 75 6d 0a 2a 2a 20 70 61 79 6c 6f  maximum.** paylo
1466b 61 64 20 66 72 61 63 74 69 6f 6e 20 66 6f 72 20  ad fraction for 
1466c 61 20 4c 45 41 46 44 41 54 41 20 74 72 65 65 20  a LEAFDATA tree 
1466d 69 73 20 61 6c 77 61 79 73 20 31 30 30 25 20 28  is always 100% (
1466e 6f 72 20 32 35 35 29 20 61 6e 64 20 69 74 0a 2a  or 255) and it.*
1466f 2a 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64 20  * not specified 
14670 69 6e 20 74 68 65 20 68 65 61 64 65 72 2e 0a 2a  in the header..*
14671 2a 0a 2a 2a 20 45 61 63 68 20 62 74 72 65 65 20  *.** Each btree 
14672 70 61 67 65 73 20 69 73 20 64 69 76 69 64 65 64  pages is divided
14673 20 69 6e 74 6f 20 74 68 72 65 65 20 73 65 63 74   into three sect
14674 69 6f 6e 73 3a 20 20 54 68 65 20 68 65 61 64 65  ions:  The heade
14675 72 2c 20 74 68 65 0a 2a 2a 20 63 65 6c 6c 20 70  r, the.** cell p
14676 6f 69 6e 74 65 72 20 61 72 72 61 79 2c 20 61 6e  ointer array, an
14677 64 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65  d the cell conte
14678 6e 74 20 61 72 65 61 2e 20 20 50 61 67 65 20 31  nt area.  Page 1
14679 20 61 6c 73 6f 20 68 61 73 20 61 20 31 30 30 2d   also has a 100-
1467a 62 79 74 65 0a 2a 2a 20 66 69 6c 65 20 68 65 61  byte.** file hea
1467b 64 65 72 20 74 68 61 74 20 6f 63 63 75 72 73 20  der that occurs 
1467c 62 65 66 6f 72 65 20 74 68 65 20 70 61 67 65 20  before the page 
1467d 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20  header..**.**   
1467e 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     |------------
1467f 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20  ----|.**      | 
14680 66 69 6c 65 20 68 65 61 64 65 72 20 20 20 20 7c  file header    |
14681 20 20 20 31 30 30 20 62 79 74 65 73 2e 20 20 50     100 bytes.  P
14682 61 67 65 20 31 20 6f 6e 6c 79 2e 0a 2a 2a 20 20  age 1 only..**  
14683 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d      |-----------
14684 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c  -----|.**      |
14685 20 70 61 67 65 20 68 65 61 64 65 72 20 20 20 20   page header    
14686 7c 20 20 20 38 20 62 79 74 65 73 20 66 6f 72 20  |   8 bytes for 
14687 6c 65 61 76 65 73 2e 20 20 31 32 20 62 79 74 65  leaves.  12 byte
14688 73 20 66 6f 72 20 69 6e 74 65 72 69 6f 72 20 6e  s for interior n
14689 6f 64 65 73 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d  odes.**      |--
1468a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a  --------------|.
1468b 2a 2a 20 20 20 20 20 20 7c 20 63 65 6c 6c 20 70  **      | cell p
1468c 6f 69 6e 74 65 72 20 20 20 7c 20 20 20 7c 20 20  ointer   |   |  
1468d 32 20 62 79 74 65 73 20 70 65 72 20 63 65 6c 6c  2 bytes per cell
1468e 2e 20 20 53 6f 72 74 65 64 20 6f 72 64 65 72 2e  .  Sorted order.
1468f 0a 2a 2a 20 20 20 20 20 20 7c 20 61 72 72 61 79  .**      | array
14690 20 20 20 20 20 20 20 20 20 20 7c 20 20 20 7c 20            |   | 
14691 20 47 72 6f 77 73 20 64 6f 77 6e 77 61 72 64 0a   Grows downward.
14692 2a 2a 20 20 20 20 20 20 7c 20 20 20 20 20 20 20  **      |       
14693 20 20 20 20 20 20 20 20 20 7c 20 20 20 76 0a 2a           |   v.*
14694 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d  *      |--------
14695 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20  --------|.**    
14696 20 20 7c 20 75 6e 61 6c 6c 6f 63 61 74 65 64 20    | unallocated 
14697 20 20 20 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 73     |.**      | s
14698 70 61 63 65 20 20 20 20 20 20 20 20 20 20 7c 0a  pace          |.
14699 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d  **      |-------
1469a 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 20 20 20 5e 20 20  ---------|   ^  
1469b 47 72 6f 77 73 20 75 70 77 61 72 64 73 0a 2a 2a  Grows upwards.**
1469c 20 20 20 20 20 20 7c 20 63 65 6c 6c 20 63 6f 6e        | cell con
1469d 74 65 6e 74 20 20 20 7c 20 20 20 7c 20 20 41 72  tent   |   |  Ar
1469e 62 69 74 72 61 72 79 20 6f 72 64 65 72 20 69 6e  bitrary order in
1469f 74 65 72 73 70 65 72 73 65 64 20 77 69 74 68 20  terspersed with 
146a0 66 72 65 65 62 6c 6f 63 6b 73 2e 0a 2a 2a 20 20  freeblocks..**  
146a1 20 20 20 20 7c 20 61 72 65 61 20 20 20 20 20 20      | area      
146a2 20 20 20 20 20 7c 20 20 20 7c 20 20 61 6e 64 20       |   |  and 
146a3 66 72 65 65 20 73 70 61 63 65 20 66 72 61 67 6d  free space fragm
146a4 65 6e 74 73 2e 0a 2a 2a 20 20 20 20 20 20 7c 2d  ents..**      |-
146a5 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c  ---------------|
146a6 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20  .**.** The page 
146a7 68 65 61 64 65 72 73 20 6c 6f 6f 6b 73 20 6c 69  headers looks li
146a8 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20  ke this:.**.**  
146a9 20 4f 46 46 53 45 54 20 20 20 53 49 5a 45 20 20   OFFSET   SIZE  
146aa 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a     DESCRIPTION.*
146ab 2a 20 20 20 20 20 20 30 20 20 20 20 20 20 20 31  *      0       1
146ac 20 20 20 20 20 20 46 6c 61 67 73 2e 20 31 3a 20        Flags. 1: 
146ad 69 6e 74 6b 65 79 2c 20 32 3a 20 7a 65 72 6f 64  intkey, 2: zerod
146ae 61 74 61 2c 20 34 3a 20 6c 65 61 66 64 61 74 61  ata, 4: leafdata
146af 2c 20 38 3a 20 6c 65 61 66 0a 2a 2a 20 20 20 20  , 8: leaf.**    
146b0 20 20 31 20 20 20 20 20 20 20 32 20 20 20 20 20    1       2     
146b1 20 62 79 74 65 20 6f 66 66 73 65 74 20 74 6f 20   byte offset to 
146b2 74 68 65 20 66 69 72 73 74 20 66 72 65 65 62 6c  the first freebl
146b3 6f 63 6b 0a 2a 2a 20 20 20 20 20 20 33 20 20 20  ock.**      3   
146b4 20 20 20 20 32 20 20 20 20 20 20 6e 75 6d 62 65      2      numbe
146b5 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 74 68  r of cells on th
146b6 69 73 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20  is page.**      
146b7 35 20 20 20 20 20 20 20 32 20 20 20 20 20 20 66  5       2      f
146b8 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 65  irst byte of the
146b9 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72   cell content ar
146ba 65 61 0a 2a 2a 20 20 20 20 20 20 37 20 20 20 20  ea.**      7    
146bb 20 20 20 31 20 20 20 20 20 20 6e 75 6d 62 65 72     1      number
146bc 20 6f 66 20 66 72 61 67 6d 65 6e 74 65 64 20 66   of fragmented f
146bd 72 65 65 20 62 79 74 65 73 0a 2a 2a 20 20 20 20  ree bytes.**    
146be 20 20 38 20 20 20 20 20 20 20 34 20 20 20 20 20    8       4     
146bf 20 52 69 67 68 74 20 63 68 69 6c 64 20 28 74 68   Right child (th
146c0 65 20 50 74 72 28 4e 29 20 76 61 6c 75 65 29 2e  e Ptr(N) value).
146c1 20 20 4f 6d 69 74 74 65 64 20 6f 6e 20 6c 65 61    Omitted on lea
146c2 76 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  ves..**.** The f
146c3 6c 61 67 73 20 64 65 66 69 6e 65 20 74 68 65 20  lags define the 
146c4 66 6f 72 6d 61 74 20 6f 66 20 74 68 69 73 20 62  format of this b
146c5 74 72 65 65 20 70 61 67 65 2e 20 20 54 68 65 20  tree page.  The 
146c6 6c 65 61 66 20 66 6c 61 67 20 6d 65 61 6e 73 20  leaf flag means 
146c7 74 68 61 74 0a 2a 2a 20 74 68 69 73 20 70 61 67  that.** this pag
146c8 65 20 68 61 73 20 6e 6f 20 63 68 69 6c 64 72 65  e has no childre
146c9 6e 2e 20 20 54 68 65 20 7a 65 72 6f 64 61 74 61  n.  The zerodata
146ca 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 68 61 74   flag means that
146cb 20 74 68 69 73 20 70 61 67 65 20 63 61 72 72 69   this page carri
146cc 65 73 0a 2a 2a 20 6f 6e 6c 79 20 6b 65 79 73 20  es.** only keys 
146cd 61 6e 64 20 6e 6f 20 64 61 74 61 2e 20 20 54 68  and no data.  Th
146ce 65 20 69 6e 74 6b 65 79 20 66 6c 61 67 20 6d 65  e intkey flag me
146cf 61 6e 73 20 74 68 61 74 20 74 68 65 20 6b 65 79  ans that the key
146d0 20 69 73 20 61 20 69 6e 74 65 67 65 72 0a 2a 2a   is a integer.**
146d1 20 77 68 69 63 68 20 69 73 20 73 74 6f 72 65 64   which is stored
146d2 20 69 6e 20 74 68 65 20 6b 65 79 20 73 69 7a 65   in the key size
146d3 20 65 6e 74 72 79 20 6f 66 20 74 68 65 20 63 65   entry of the ce
146d4 6c 6c 20 68 65 61 64 65 72 20 72 61 74 68 65 72  ll header rather
146d5 20 74 68 61 6e 20 69 6e 0a 2a 2a 20 74 68 65 20   than in.** the 
146d6 70 61 79 6c 6f 61 64 20 61 72 65 61 2e 0a 2a 2a  payload area..**
146d7 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 20 70 6f 69  .** The cell poi
146d8 6e 74 65 72 20 61 72 72 61 79 20 62 65 67 69 6e  nter array begin
146d9 73 20 6f 6e 20 74 68 65 20 66 69 72 73 74 20 62  s on the first b
146da 79 74 65 20 61 66 74 65 72 20 74 68 65 20 70 61  yte after the pa
146db 67 65 20 68 65 61 64 65 72 2e 0a 2a 2a 20 54 68  ge header..** Th
146dc 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61  e cell pointer a
146dd 72 72 61 79 20 63 6f 6e 74 61 69 6e 73 20 7a 65  rray contains ze
146de 72 6f 20 6f 72 20 6d 6f 72 65 20 32 2d 62 79 74  ro or more 2-byt
146df 65 20 6e 75 6d 62 65 72 73 20 77 68 69 63 68 20  e numbers which 
146e0 61 72 65 0a 2a 2a 20 6f 66 66 73 65 74 73 20 66  are.** offsets f
146e1 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e  rom the beginnin
146e2 67 20 6f 66 20 74 68 65 20 70 61 67 65 20 74 6f  g of the page to
146e3 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e   the cell conten
146e4 74 20 69 6e 20 74 68 65 20 63 65 6c 6c 0a 2a 2a  t in the cell.**
146e5 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 20   content area.  
146e6 54 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72  The cell pointer
146e7 73 20 6f 63 63 75 72 20 69 6e 20 73 6f 72 74 65  s occur in sorte
146e8 64 20 6f 72 64 65 72 2e 20 20 54 68 65 20 73 79  d order.  The sy
146e9 73 74 65 6d 20 73 74 72 69 76 65 73 0a 2a 2a 20  stem strives.** 
146ea 74 6f 20 6b 65 65 70 20 66 72 65 65 20 73 70 61  to keep free spa
146eb 63 65 20 61 66 74 65 72 20 74 68 65 20 6c 61 73  ce after the las
146ec 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 73  t cell pointer s
146ed 6f 20 74 68 61 74 20 6e 65 77 20 63 65 6c 6c 73  o that new cells
146ee 20 63 61 6e 0a 2a 2a 20 62 65 20 65 61 73 69 6c   can.** be easil
146ef 79 20 61 64 64 65 64 20 77 69 74 68 6f 75 74 20  y added without 
146f0 68 61 76 69 6e 67 20 74 6f 20 64 65 66 72 61 67  having to defrag
146f1 6d 65 6e 74 20 74 68 65 20 70 61 67 65 2e 0a 2a  ment the page..*
146f2 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f 6e 74 65 6e  *.** Cell conten
146f3 74 20 69 73 20 73 74 6f 72 65 64 20 61 74 20 74  t is stored at t
146f4 68 65 20 76 65 72 79 20 65 6e 64 20 6f 66 20 74  he very end of t
146f5 68 65 20 70 61 67 65 20 61 6e 64 20 67 72 6f 77  he page and grow
146f6 73 20 74 6f 77 61 72 64 20 74 68 65 0a 2a 2a 20  s toward the.** 
146f7 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65  beginning of the
146f8 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 55 6e 75   page..**.** Unu
146f9 73 65 64 20 73 70 61 63 65 20 77 69 74 68 69 6e  sed space within
146fa 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e   the cell conten
146fb 74 20 61 72 65 61 20 69 73 20 63 6f 6c 6c 65 63  t area is collec
146fc 74 65 64 20 69 6e 74 6f 20 61 20 6c 69 6e 6b 65  ted into a linke
146fd 64 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 66 72 65  d list of.** fre
146fe 65 62 6c 6f 63 6b 73 2e 20 20 45 61 63 68 20 66  eblocks.  Each f
146ff 72 65 65 62 6c 6f 63 6b 20 69 73 20 61 74 20 6c  reeblock is at l
14700 65 61 73 74 20 34 20 62 79 74 65 73 20 69 6e 20  east 4 bytes in 
14701 73 69 7a 65 2e 20 20 54 68 65 20 62 79 74 65 20  size.  The byte 
14702 6f 66 66 73 65 74 0a 2a 2a 20 74 6f 20 74 68 65  offset.** to the
14703 20 66 69 72 73 74 20 66 72 65 65 62 6c 6f 63 6b   first freeblock
14704 20 69 73 20 67 69 76 65 6e 20 69 6e 20 74 68 65   is given in the
14705 20 68 65 61 64 65 72 2e 20 20 46 72 65 65 62 6c   header.  Freebl
14706 6f 63 6b 73 20 6f 63 63 75 72 20 69 6e 0a 2a 2a  ocks occur in.**
14707 20 69 6e 63 72 65 61 73 69 6e 67 20 6f 72 64 65   increasing orde
14708 72 2e 20 20 42 65 63 61 75 73 65 20 61 20 66 72  r.  Because a fr
14709 65 65 62 6c 6f 63 6b 20 6d 75 73 74 20 62 65 20  eeblock must be 
1470a 61 74 20 6c 65 61 73 74 20 34 20 62 79 74 65 73  at least 4 bytes
1470b 20 69 6e 20 73 69 7a 65 2c 0a 2a 2a 20 61 6e 79   in size,.** any
1470c 20 67 72 6f 75 70 20 6f 66 20 33 20 6f 72 20 66   group of 3 or f
1470d 65 77 65 72 20 75 6e 75 73 65 64 20 62 79 74 65  ewer unused byte
1470e 73 20 69 6e 20 74 68 65 20 63 65 6c 6c 20 63 6f  s in the cell co
1470f 6e 74 65 6e 74 20 61 72 65 61 20 63 61 6e 6e 6f  ntent area canno
14710 74 0a 2a 2a 20 65 78 69 73 74 20 6f 6e 20 74 68  t.** exist on th
14711 65 20 66 72 65 65 62 6c 6f 63 6b 20 63 68 61 69  e freeblock chai
14712 6e 2e 20 20 41 20 67 72 6f 75 70 20 6f 66 20 33  n.  A group of 3
14713 20 6f 72 20 66 65 77 65 72 20 66 72 65 65 20 62   or fewer free b
14714 79 74 65 73 20 69 73 20 63 61 6c 6c 65 64 0a 2a  ytes is called.*
14715 2a 20 61 20 66 72 61 67 6d 65 6e 74 2e 20 20 54  * a fragment.  T
14716 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20  he total number 
14717 6f 66 20 62 79 74 65 73 20 69 6e 20 61 6c 6c 20  of bytes in all 
14718 66 72 61 67 6d 65 6e 74 73 20 69 73 20 72 65 63  fragments is rec
14719 6f 72 64 65 64 2e 0a 2a 2a 20 69 6e 20 74 68 65  orded..** in the
1471a 20 70 61 67 65 20 68 65 61 64 65 72 20 61 74 20   page header at 
1471b 6f 66 66 73 65 74 20 37 2e 0a 2a 2a 0a 2a 2a 20  offset 7..**.** 
1471c 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52     SIZE    DESCR
1471d 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 32  IPTION.**      2
1471e 20 20 20 20 20 42 79 74 65 20 6f 66 66 73 65 74       Byte offset
1471f 20 6f 66 20 74 68 65 20 6e 65 78 74 20 66 72 65   of the next fre
14720 65 62 6c 6f 63 6b 0a 2a 2a 20 20 20 20 20 20 32  eblock.**      2
14721 20 20 20 20 20 42 79 74 65 73 20 69 6e 20 74 68       Bytes in th
14722 69 73 20 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a 0a  is freeblock.**.
14723 2a 2a 20 43 65 6c 6c 73 20 61 72 65 20 6f 66 20  ** Cells are of 
14724 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 2e  variable length.
14725 20 20 43 65 6c 6c 73 20 61 72 65 20 73 74 6f 72    Cells are stor
14726 65 64 20 69 6e 20 74 68 65 20 63 65 6c 6c 20 63  ed in the cell c
14727 6f 6e 74 65 6e 74 20 61 72 65 61 20 61 74 0a 2a  ontent area at.*
14728 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  * the end of the
14729 20 70 61 67 65 2e 20 20 50 6f 69 6e 74 65 72 73   page.  Pointers
1472a 20 74 6f 20 74 68 65 20 63 65 6c 6c 73 20 61 72   to the cells ar
1472b 65 20 69 6e 20 74 68 65 20 63 65 6c 6c 20 70 6f  e in the cell po
1472c 69 6e 74 65 72 20 61 72 72 61 79 0a 2a 2a 20 74  inter array.** t
1472d 68 61 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  hat immediately 
1472e 66 6f 6c 6c 6f 77 73 20 74 68 65 20 70 61 67 65  follows the page
1472f 20 68 65 61 64 65 72 2e 20 20 43 65 6c 6c 73 20   header.  Cells 
14730 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69  is not necessari
14731 6c 79 0a 2a 2a 20 63 6f 6e 74 69 67 75 6f 75 73  ly.** contiguous
14732 20 6f 72 20 69 6e 20 6f 72 64 65 72 2c 20 62 75   or in order, bu
14733 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 73 20  t cell pointers 
14734 61 72 65 20 63 6f 6e 74 69 67 75 6f 75 73 20 61  are contiguous a
14735 6e 64 20 69 6e 20 6f 72 64 65 72 2e 0a 2a 2a 0a  nd in order..**.
14736 2a 2a 20 43 65 6c 6c 20 63 6f 6e 74 65 6e 74 20  ** Cell content 
14737 6d 61 6b 65 73 20 75 73 65 20 6f 66 20 76 61 72  makes use of var
14738 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e 74  iable length int
14739 65 67 65 72 73 2e 20 20 41 20 76 61 72 69 61 62  egers.  A variab
1473a 6c 65 0a 2a 2a 20 6c 65 6e 67 74 68 20 69 6e 74  le.** length int
1473b 65 67 65 72 20 69 73 20 31 20 74 6f 20 39 20 62  eger is 1 to 9 b
1473c 79 74 65 73 20 77 68 65 72 65 20 74 68 65 20 6c  ytes where the l
1473d 6f 77 65 72 20 37 20 62 69 74 73 20 6f 66 20 65  ower 7 bits of e
1473e 61 63 68 20 0a 2a 2a 20 62 79 74 65 20 61 72 65  ach .** byte are
1473f 20 75 73 65 64 2e 20 20 54 68 65 20 69 6e 74 65   used.  The inte
14740 67 65 72 20 63 6f 6e 73 69 73 74 73 20 6f 66 20  ger consists of 
14741 61 6c 6c 20 62 79 74 65 73 20 74 68 61 74 20 68  all bytes that h
14742 61 76 65 20 62 69 74 20 38 20 73 65 74 20 61 6e  ave bit 8 set an
14743 64 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 62  d.** the first b
14744 79 74 65 20 77 69 74 68 20 62 69 74 20 38 20 63  yte with bit 8 c
14745 6c 65 61 72 2e 20 20 54 68 65 20 6d 6f 73 74 20  lear.  The most 
14746 73 69 67 6e 69 66 69 63 61 6e 74 20 62 79 74 65  significant byte
14747 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a   of the integer.
14748 2a 2a 20 61 70 70 65 61 72 73 20 66 69 72 73 74  ** appears first
14749 2e 20 20 41 20 76 61 72 69 61 62 6c 65 2d 6c 65  .  A variable-le
1474a 6e 67 74 68 20 69 6e 74 65 67 65 72 20 6d 61 79  ngth integer may
1474b 20 6e 6f 74 20 62 65 20 6d 6f 72 65 20 74 68 61   not be more tha
1474c 6e 20 39 20 62 79 74 65 73 20 6c 6f 6e 67 2e 0a  n 9 bytes long..
1474d 2a 2a 20 41 73 20 61 20 73 70 65 63 69 61 6c 20  ** As a special 
1474e 63 61 73 65 2c 20 61 6c 6c 20 38 20 62 79 74 65  case, all 8 byte
1474f 73 20 6f 66 20 74 68 65 20 39 74 68 20 62 79 74  s of the 9th byt
14750 65 20 61 72 65 20 75 73 65 64 20 61 73 20 64 61  e are used as da
14751 74 61 2e 20 20 54 68 69 73 0a 2a 2a 20 61 6c 6c  ta.  This.** all
14752 6f 77 73 20 61 20 36 34 2d 62 69 74 20 69 6e 74  ows a 64-bit int
14753 65 67 65 72 20 74 6f 20 62 65 20 65 6e 63 6f 64  eger to be encod
14754 65 64 20 69 6e 20 39 20 62 79 74 65 73 2e 0a 2a  ed in 9 bytes..*
14755 2a 0a 2a 2a 20 20 20 20 30 78 30 30 20 20 20 20  *.**    0x00    
14756 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14757 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30    becomes  0x000
14758 30 30 30 30 30 0a 2a 2a 20 20 20 20 30 78 37 66  00000.**    0x7f
14759 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1475a 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30        becomes  0
1475b 78 30 30 30 30 30 30 37 66 0a 2a 2a 20 20 20 20  x0000007f.**    
1475c 30 78 38 31 20 30 78 30 30 20 20 20 20 20 20 20  0x81 0x00       
1475d 20 20 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65            become
1475e 73 20 20 30 78 30 30 30 30 30 30 38 30 0a 2a 2a  s  0x00000080.**
1475f 20 20 20 20 30 78 38 32 20 30 78 30 30 20 20 20      0x82 0x00   
14760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 65                be
14761 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30 31 30  comes  0x0000010
14762 30 0a 2a 2a 20 20 20 20 30 78 38 30 20 30 78 37  0.**    0x80 0x7
14763 66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f               
14764 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30    becomes  0x000
14765 30 30 30 37 66 0a 2a 2a 20 20 20 20 30 78 38 61  0007f.**    0x8a
14766 20 30 78 39 31 20 30 78 64 31 20 30 78 61 63 20   0x91 0xd1 0xac 
14767 30 78 37 38 20 20 62 65 63 6f 6d 65 73 20 20 30  0x78  becomes  0
14768 78 31 32 33 34 35 36 37 38 0a 2a 2a 20 20 20 20  x12345678.**    
14769 30 78 38 31 20 30 78 38 31 20 30 78 38 31 20 30  0x81 0x81 0x81 0
1476a 78 38 31 20 30 78 30 31 20 20 62 65 63 6f 6d 65  x81 0x01  become
1476b 73 20 20 30 78 31 30 32 30 34 30 38 31 0a 2a 2a  s  0x10204081.**
1476c 0a 2a 2a 20 56 61 72 69 61 62 6c 65 20 6c 65 6e  .** Variable len
1476d 67 74 68 20 69 6e 74 65 67 65 72 73 20 61 72 65  gth integers are
1476e 20 75 73 65 64 20 66 6f 72 20 72 6f 77 69 64 73   used for rowids
1476f 20 61 6e 64 20 74 6f 20 68 6f 6c 64 20 74 68 65   and to hold the
14770 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 79   number of.** by
14771 74 65 73 20 6f 66 20 6b 65 79 20 61 6e 64 20 64  tes of key and d
14772 61 74 61 20 69 6e 20 61 20 62 74 72 65 65 20 63  ata in a btree c
14773 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  ell..**.** The c
14774 6f 6e 74 65 6e 74 20 6f 66 20 61 20 63 65 6c 6c  ontent of a cell
14775 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73   looks like this
14776 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 45 20  :.**.**    SIZE 
14777 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a     DESCRIPTION.*
14778 2a 20 20 20 20 20 20 34 20 20 20 20 20 50 61 67  *      4     Pag
14779 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
1477a 6c 65 66 74 20 63 68 69 6c 64 2e 20 4f 6d 69 74  left child. Omit
1477b 74 65 64 20 69 66 20 6c 65 61 66 20 66 6c 61 67  ted if leaf flag
1477c 20 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20   is set..**     
1477d 76 61 72 20 20 20 20 4e 75 6d 62 65 72 20 6f 66  var    Number of
1477e 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 2e 20   bytes of data. 
1477f 4f 6d 69 74 74 65 64 20 69 66 20 74 68 65 20 7a  Omitted if the z
14780 65 72 6f 64 61 74 61 20 66 6c 61 67 20 69 73 20  erodata flag is 
14781 73 65 74 2e 0a 2a 2a 20 20 20 20 20 76 61 72 20  set..**     var 
14782 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74     Number of byt
14783 65 73 20 6f 66 20 6b 65 79 2e 20 4f 72 20 74 68  es of key. Or th
14784 65 20 6b 65 79 20 69 74 73 65 6c 66 20 69 66 20  e key itself if 
14785 69 6e 74 6b 65 79 20 66 6c 61 67 20 69 73 20 73  intkey flag is s
14786 65 74 2e 0a 2a 2a 20 20 20 20 20 20 2a 20 20 20  et..**      *   
14787 20 20 50 61 79 6c 6f 61 64 0a 2a 2a 20 20 20 20    Payload.**    
14788 20 20 34 20 20 20 20 20 46 69 72 73 74 20 70 61    4     First pa
14789 67 65 20 6f 66 20 74 68 65 20 6f 76 65 72 66 6c  ge of the overfl
1478a 6f 77 20 63 68 61 69 6e 2e 20 20 4f 6d 69 74 74  ow chain.  Omitt
1478b 65 64 20 69 66 20 6e 6f 20 6f 76 65 72 66 6c 6f  ed if no overflo
1478c 77 0a 2a 2a 0a 2a 2a 20 4f 76 65 72 66 6c 6f 77  w.**.** Overflow
1478d 20 70 61 67 65 73 20 66 6f 72 6d 20 61 20 6c 69   pages form a li
1478e 6e 6b 65 64 20 6c 69 73 74 2e 20 20 45 61 63 68  nked list.  Each
1478f 20 70 61 67 65 20 65 78 63 65 70 74 20 74 68 65   page except the
14790 20 6c 61 73 74 20 69 73 20 63 6f 6d 70 6c 65 74   last is complet
14791 65 6c 79 0a 2a 2a 20 66 69 6c 6c 65 64 20 77 69  ely.** filled wi
14792 74 68 20 64 61 74 61 20 28 70 61 67 65 73 69 7a  th data (pagesiz
14793 65 20 2d 20 34 20 62 79 74 65 73 29 2e 20 20 54  e - 4 bytes).  T
14794 68 65 20 6c 61 73 74 20 70 61 67 65 20 63 61 6e  he last page can
14795 20 68 61 76 65 20 61 73 20 6c 69 74 74 6c 65 0a   have as little.
14796 2a 2a 20 61 73 20 31 20 62 79 74 65 20 6f 66 20  ** as 1 byte of 
14797 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 53  data..**.**    S
14798 49 5a 45 20 20 20 20 44 45 53 43 52 49 50 54 49  IZE    DESCRIPTI
14799 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20  ON.**      4    
1479a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20   Page number of 
1479b 6e 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 70 61  next overflow pa
1479c 67 65 0a 2a 2a 20 20 20 20 20 20 2a 20 20 20 20  ge.**      *    
1479d 20 44 61 74 61 0a 2a 2a 0a 2a 2a 20 46 72 65 65   Data.**.** Free
1479e 6c 69 73 74 20 70 61 67 65 73 20 63 6f 6d 65 20  list pages come 
1479f 69 6e 20 74 77 6f 20 73 75 62 74 79 70 65 73 3a  in two subtypes:
147a0 20 74 72 75 6e 6b 20 70 61 67 65 73 20 61 6e 64   trunk pages and
147a1 20 6c 65 61 66 20 70 61 67 65 73 2e 20 20 54 68   leaf pages.  Th
147a2 65 0a 2a 2a 20 66 69 6c 65 20 68 65 61 64 65 72  e.** file header
147a3 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66   points to the f
147a4 69 72 73 74 20 69 6e 20 61 20 6c 69 6e 6b 65 64  irst in a linked
147a5 20 6c 69 73 74 20 6f 66 20 74 72 75 6e 6b 20 70   list of trunk p
147a6 61 67 65 2e 20 20 45 61 63 68 20 74 72 75 6e 6b  age.  Each trunk
147a7 0a 2a 2a 20 70 61 67 65 20 70 6f 69 6e 74 73 20  .** page points 
147a8 74 6f 20 6d 75 6c 74 69 70 6c 65 20 6c 65 61 66  to multiple leaf
147a9 20 70 61 67 65 73 2e 20 20 54 68 65 20 63 6f 6e   pages.  The con
147aa 74 65 6e 74 20 6f 66 20 61 20 6c 65 61 66 20 70  tent of a leaf p
147ab 61 67 65 20 69 73 0a 2a 2a 20 75 6e 73 70 65 63  age is.** unspec
147ac 69 66 69 65 64 2e 20 20 41 20 74 72 75 6e 6b 20  ified.  A trunk 
147ad 70 61 67 65 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20  page looks like 
147ae 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 53  this:.**.**    S
147af 49 5a 45 20 20 20 20 44 45 53 43 52 49 50 54 49  IZE    DESCRIPTI
147b0 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20  ON.**      4    
147b1 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20   Page number of 
147b2 6e 65 78 74 20 74 72 75 6e 6b 20 70 61 67 65 0a  next trunk page.
147b3 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20 4e 75  **      4     Nu
147b4 6d 62 65 72 20 6f 66 20 6c 65 61 66 20 70 6f 69  mber of leaf poi
147b5 6e 74 65 72 73 20 6f 6e 20 74 68 69 73 20 70 61  nters on this pa
147b6 67 65 0a 2a 2a 20 20 20 20 20 20 2a 20 20 20 20  ge.**      *    
147b7 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 70 61   zero or more pa
147b8 67 65 73 20 6e 75 6d 62 65 72 73 20 6f 66 20 6c  ges numbers of l
147b9 65 61 76 65 73 0a 2a 2f 0a 0a 0a 2f 2a 20 54 68  eaves.*/.../* Th
147ba 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75  e following valu
147bb 65 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d  e is the maximum
147bc 20 63 65 6c 6c 20 73 69 7a 65 20 61 73 73 75 6d   cell size assum
147bd 69 6e 67 20 61 20 6d 61 78 69 6d 75 6d 20 70 61  ing a maximum pa
147be 67 65 0a 2a 2a 20 73 69 7a 65 20 67 69 76 65 20  ge.** size give 
147bf 61 62 6f 76 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e  above..*/.#defin
147c0 65 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70  e MX_CELL_SIZE(p
147c1 42 74 29 20 20 28 70 42 74 2d 3e 70 61 67 65 53  Bt)  (pBt->pageS
147c2 69 7a 65 2d 38 29 0a 0a 2f 2a 20 54 68 65 20 6d  ize-8)../* The m
147c3 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66  aximum number of
147c4 20 63 65 6c 6c 73 20 6f 6e 20 61 20 73 69 6e 67   cells on a sing
147c5 6c 65 20 70 61 67 65 20 6f 66 20 74 68 65 20 64  le page of the d
147c6 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 0a 2a  atabase.  This.*
147c7 2a 20 61 73 73 75 6d 65 73 20 61 20 6d 69 6e 69  * assumes a mini
147c8 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 6f 66  mum cell size of
147c9 20 36 20 62 79 74 65 73 20 20 28 34 20 62 79 74   6 bytes  (4 byt
147ca 65 73 20 66 6f 72 20 74 68 65 20 63 65 6c 6c 20  es for the cell 
147cb 69 74 73 65 6c 66 0a 2a 2a 20 70 6c 75 73 20 32  itself.** plus 2
147cc 20 62 79 74 65 73 20 66 6f 72 20 74 68 65 20 69   bytes for the i
147cd 6e 64 65 78 20 74 6f 20 74 68 65 20 63 65 6c 6c  ndex to the cell
147ce 20 69 6e 20 74 68 65 20 70 61 67 65 20 68 65 61   in the page hea
147cf 64 65 72 29 2e 20 20 53 75 63 68 0a 2a 2a 20 73  der).  Such.** s
147d0 6d 61 6c 6c 20 63 65 6c 6c 73 20 77 69 6c 6c 20  mall cells will 
147d1 62 65 20 72 61 72 65 2c 20 62 75 74 20 74 68 65  be rare, but the
147d2 79 20 61 72 65 20 70 6f 73 73 69 62 6c 65 2e 0a  y are possible..
147d3 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f 43 45  */.#define MX_CE
147d4 4c 4c 28 70 42 74 29 20 28 28 70 42 74 2d 3e 70  LL(pBt) ((pBt->p
147d5 61 67 65 53 69 7a 65 2d 38 29 2f 36 29 0a 0a 2f  ageSize-8)/6)../
147d6 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72  * Forward declar
147d7 61 74 69 6f 6e 73 20 2a 2f 0a 74 79 70 65 64 65  ations */.typede
147d8 66 20 73 74 72 75 63 74 20 4d 65 6d 50 61 67 65  f struct MemPage
147d9 20 4d 65 6d 50 61 67 65 3b 0a 74 79 70 65 64 65   MemPage;.typede
147da 66 20 73 74 72 75 63 74 20 42 74 4c 6f 63 6b 20  f struct BtLock 
147db 42 74 4c 6f 63 6b 3b 0a 0a 2f 2a 0a 2a 2a 20 54  BtLock;../*.** T
147dc 68 69 73 20 69 73 20 61 20 6d 61 67 69 63 20 73  his is a magic s
147dd 74 72 69 6e 67 20 74 68 61 74 20 61 70 70 65 61  tring that appea
147de 72 73 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e  rs at the beginn
147df 69 6e 67 20 6f 66 20 65 76 65 72 79 0a 2a 2a 20  ing of every.** 
147e0 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 20  SQLite database 
147e1 69 6e 20 6f 72 64 65 72 20 74 6f 20 69 64 65 6e  in order to iden
147e2 74 69 66 79 20 74 68 65 20 66 69 6c 65 20 61 73  tify the file as
147e3 20 61 20 72 65 61 6c 20 64 61 74 61 62 61 73 65   a real database
147e4 2e 0a 2a 2a 0a 2a 2a 20 59 6f 75 20 63 61 6e 20  ..**.** You can 
147e5 63 68 61 6e 67 65 20 74 68 69 73 20 76 61 6c 75  change this valu
147e6 65 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  e at compile-tim
147e7 65 20 62 79 20 73 70 65 63 69 66 79 69 6e 67 20  e by specifying 
147e8 61 0a 2a 2a 20 2d 44 53 51 4c 49 54 45 5f 46 49  a.** -DSQLITE_FI
147e9 4c 45 5f 48 45 41 44 45 52 3d 22 2e 2e 2e 22 20  LE_HEADER="..." 
147ea 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20  on the compiler 
147eb 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 2e 20 20 54  command-line.  T
147ec 68 65 0a 2a 2a 20 68 65 61 64 65 72 20 6d 75 73  he.** header mus
147ed 74 20 62 65 20 65 78 61 63 74 6c 79 20 31 36 20  t be exactly 16 
147ee 62 79 74 65 73 20 69 6e 63 6c 75 64 69 6e 67 20  bytes including 
147ef 74 68 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61  the zero-termina
147f0 74 6f 72 20 73 6f 0a 2a 2a 20 74 68 65 20 73 74  tor so.** the st
147f1 72 69 6e 67 20 69 74 73 65 6c 66 20 73 68 6f 75  ring itself shou
147f2 6c 64 20 62 65 20 31 35 20 63 68 61 72 61 63 74  ld be 15 charact
147f3 65 72 73 20 6c 6f 6e 67 2e 20 20 49 66 20 79 6f  ers long.  If yo
147f4 75 20 63 68 61 6e 67 65 0a 2a 2a 20 74 68 65 20  u change.** the 
147f5 68 65 61 64 65 72 2c 20 74 68 65 6e 20 79 6f 75  header, then you
147f6 72 20 63 75 73 74 6f 6d 20 6c 69 62 72 61 72 79  r custom library
147f7 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c   will not be abl
147f8 65 20 74 6f 20 72 65 61 64 20 0a 2a 2a 20 64 61  e to read .** da
147f9 74 61 62 61 73 65 73 20 67 65 6e 65 72 61 74 65  tabases generate
147fa 64 20 62 79 20 74 68 65 20 73 74 61 6e 64 61 72  d by the standar
147fb 64 20 74 6f 6f 6c 73 20 61 6e 64 20 74 68 65 20  d tools and the 
147fc 73 74 61 6e 64 61 72 64 20 74 6f 6f 6c 73 0a 2a  standard tools.*
147fd 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62  * will not be ab
147fe 6c 65 20 74 6f 20 72 65 61 64 20 64 61 74 61 62  le to read datab
147ff 61 73 65 73 20 63 72 65 61 74 65 64 20 62 79 20  ases created by 
14800 79 6f 75 72 20 63 75 73 74 6f 6d 20 6c 69 62 72  your custom libr
14801 61 72 79 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ary..*/.#ifndef 
14802 53 51 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41 44  SQLITE_FILE_HEAD
14803 45 52 20 2f 2a 20 31 32 33 34 35 36 37 38 39 20  ER /* 123456789 
14804 31 32 33 34 35 36 20 2a 2f 0a 23 20 20 64 65 66  123456 */.#  def
14805 69 6e 65 20 53 51 4c 49 54 45 5f 46 49 4c 45 5f  ine SQLITE_FILE_
14806 48 45 41 44 45 52 20 22 53 51 4c 69 74 65 20 66  HEADER "SQLite f
14807 6f 72 6d 61 74 20 33 22 0a 23 65 6e 64 69 66 0a  ormat 3".#endif.
14808 0a 2f 2a 0a 2a 2a 20 50 61 67 65 20 74 79 70 65  ./*.** Page type
14809 20 66 6c 61 67 73 2e 20 20 41 6e 20 4f 52 65 64   flags.  An ORed
1480a 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20   combination of 
1480b 74 68 65 73 65 20 66 6c 61 67 73 20 61 70 70 65  these flags appe
1480c 61 72 20 61 73 20 74 68 65 0a 2a 2a 20 66 69 72  ar as the.** fir
1480d 73 74 20 62 79 74 65 20 6f 66 20 6f 6e 2d 64 69  st byte of on-di
1480e 73 6b 20 69 6d 61 67 65 20 6f 66 20 65 76 65 72  sk image of ever
1480f 79 20 42 54 72 65 65 20 70 61 67 65 2e 0a 2a 2f  y BTree page..*/
14810 0a 23 64 65 66 69 6e 65 20 50 54 46 5f 49 4e 54  .#define PTF_INT
14811 4b 45 59 20 20 20 20 30 78 30 31 0a 23 64 65 66  KEY    0x01.#def
14812 69 6e 65 20 50 54 46 5f 5a 45 52 4f 44 41 54 41  ine PTF_ZERODATA
14813 20 20 30 78 30 32 0a 23 64 65 66 69 6e 65 20 50    0x02.#define P
14814 54 46 5f 4c 45 41 46 44 41 54 41 20 20 30 78 30  TF_LEAFDATA  0x0
14815 34 0a 23 64 65 66 69 6e 65 20 50 54 46 5f 4c 45  4.#define PTF_LE
14816 41 46 20 20 20 20 20 20 30 78 30 38 0a 0a 2f 2a  AF      0x08../*
14817 0a 2a 2a 20 41 73 20 65 61 63 68 20 70 61 67 65  .** As each page
14818 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 73 20   of the file is 
14819 6c 6f 61 64 65 64 20 69 6e 74 6f 20 6d 65 6d 6f  loaded into memo
1481a 72 79 2c 20 61 6e 20 69 6e 73 74 61 6e 63 65 20  ry, an instance 
1481b 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
1481c 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 69 73  .** structure is
1481d 20 61 70 70 65 6e 64 65 64 20 61 6e 64 20 69 6e   appended and in
1481e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72  itialized to zer
1481f 6f 2e 20 20 54 68 69 73 20 73 74 72 75 63 74 75  o.  This structu
14820 72 65 20 73 74 6f 72 65 73 0a 2a 2a 20 69 6e 66  re stores.** inf
14821 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74  ormation about t
14822 68 65 20 70 61 67 65 20 74 68 61 74 20 69 73 20  he page that is 
14823 64 65 63 6f 64 65 64 20 66 72 6f 6d 20 74 68 65  decoded from the
14824 20 72 61 77 20 66 69 6c 65 20 70 61 67 65 2e 0a   raw file page..
14825 2a 2a 0a 2a 2a 20 54 68 65 20 70 50 61 72 65 6e  **.** The pParen
14826 74 20 66 69 65 6c 64 20 70 6f 69 6e 74 73 20 62  t field points b
14827 61 63 6b 20 74 6f 20 74 68 65 20 70 61 72 65 6e  ack to the paren
14828 74 20 70 61 67 65 2e 20 20 54 68 69 73 20 61 6c  t page.  This al
14829 6c 6f 77 73 20 75 73 20 74 6f 0a 2a 2a 20 77 61  lows us to.** wa
1482a 6c 6b 20 75 70 20 74 68 65 20 42 54 72 65 65 20  lk up the BTree 
1482b 66 72 6f 6d 20 61 6e 79 20 6c 65 61 66 20 74 6f  from any leaf to
1482c 20 74 68 65 20 72 6f 6f 74 2e 20 20 43 61 72 65   the root.  Care
1482d 20 6d 75 73 74 20 62 65 20 74 61 6b 65 6e 20 74   must be taken t
1482e 6f 0a 2a 2a 20 75 6e 72 65 66 28 29 20 74 68 65  o.** unref() the
1482f 20 70 61 72 65 6e 74 20 70 61 67 65 20 70 6f 69   parent page poi
14830 6e 74 65 72 20 77 68 65 6e 20 74 68 69 73 20 70  nter when this p
14831 61 67 65 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72  age is no longer
14832 20 72 65 66 65 72 65 6e 63 65 64 2e 0a 2a 2a 20   referenced..** 
14833 54 68 65 20 70 61 67 65 44 65 73 74 72 75 63 74  The pageDestruct
14834 6f 72 28 29 20 72 6f 75 74 69 6e 65 20 68 61 6e  or() routine han
14835 64 6c 65 73 20 74 68 61 74 20 63 68 6f 72 65 2e  dles that chore.
14836 0a 2a 2a 0a 2a 2a 20 41 63 63 65 73 73 20 74 6f  .**.** Access to
14837 20 61 6c 6c 20 66 69 65 6c 64 73 20 6f 66 20 74   all fields of t
14838 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73  his structure is
14839 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 74   controlled by t
1483a 68 65 20 6d 75 74 65 78 0a 2a 2a 20 73 74 6f 72  he mutex.** stor
1483b 65 64 20 69 6e 20 4d 65 6d 50 61 67 65 2e 70 42  ed in MemPage.pB
1483c 74 2d 3e 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 72  t->mutex..*/.str
1483d 75 63 74 20 4d 65 6d 50 61 67 65 20 7b 0a 20 20  uct MemPage {.  
1483e 75 38 20 69 73 49 6e 69 74 3b 20 20 20 20 20 20  u8 isInit;      
1483f 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
14840 70 72 65 76 69 6f 75 73 6c 79 20 69 6e 69 74 69  previously initi
14841 61 6c 69 7a 65 64 2e 20 4d 55 53 54 20 42 45 20  alized. MUST BE 
14842 46 49 52 53 54 21 20 2a 2f 0a 20 20 75 38 20 6e  FIRST! */.  u8 n
14843 4f 76 65 72 66 6c 6f 77 3b 20 20 20 20 20 20 20  Overflow;       
14844 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 76   /* Number of ov
14845 65 72 66 6c 6f 77 20 63 65 6c 6c 20 62 6f 64 69  erflow cell bodi
14846 65 73 20 69 6e 20 61 43 65 6c 6c 5b 5d 20 2a 2f  es in aCell[] */
14847 0a 20 20 75 38 20 69 6e 74 4b 65 79 3b 20 20 20  .  u8 intKey;   
14848 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
14849 69 66 20 69 6e 74 6b 65 79 20 66 6c 61 67 20 69  if intkey flag i
1484a 73 20 73 65 74 20 2a 2f 0a 20 20 75 38 20 6c 65  s set */.  u8 le
1484b 61 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  af;             
1484c 2f 2a 20 54 72 75 65 20 69 66 20 6c 65 61 66 20  /* True if leaf 
1484d 66 6c 61 67 20 69 73 20 73 65 74 20 2a 2f 0a 20  flag is set */. 
1484e 20 75 38 20 68 61 73 44 61 74 61 3b 20 20 20 20   u8 hasData;    
1484f 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
14850 20 74 68 69 73 20 70 61 67 65 20 73 74 6f 72 65   this page store
14851 73 20 64 61 74 61 20 2a 2f 0a 20 20 75 38 20 68  s data */.  u8 h
14852 64 72 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20  drOffset;       
14853 20 2f 2a 20 31 30 30 20 66 6f 72 20 70 61 67 65   /* 100 for page
14854 20 31 2e 20 20 30 20 6f 74 68 65 72 77 69 73 65   1.  0 otherwise
14855 20 2a 2f 0a 20 20 75 38 20 63 68 69 6c 64 50 74   */.  u8 childPt
14856 72 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20 30 20  rSize;     /* 0 
14857 69 66 20 6c 65 61 66 3d 3d 31 2e 20 20 34 20 69  if leaf==1.  4 i
14858 66 20 6c 65 61 66 3d 3d 30 20 2a 2f 0a 20 20 75  f leaf==0 */.  u
14859 31 36 20 6d 61 78 4c 6f 63 61 6c 3b 20 20 20 20  16 maxLocal;    
1485a 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 42      /* Copy of B
1485b 74 53 68 61 72 65 64 2e 6d 61 78 4c 6f 63 61 6c  tShared.maxLocal
1485c 20 6f 72 20 42 74 53 68 61 72 65 64 2e 6d 61 78   or BtShared.max
1485d 4c 65 61 66 20 2a 2f 0a 20 20 75 31 36 20 6d 69  Leaf */.  u16 mi
1485e 6e 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20 20 2f  nLocal;        /
1485f 2a 20 43 6f 70 79 20 6f 66 20 42 74 53 68 61 72  * Copy of BtShar
14860 65 64 2e 6d 69 6e 4c 6f 63 61 6c 20 6f 72 20 42  ed.minLocal or B
14861 74 53 68 61 72 65 64 2e 6d 69 6e 4c 65 61 66 20  tShared.minLeaf 
14862 2a 2f 0a 20 20 75 31 36 20 63 65 6c 6c 4f 66 66  */.  u16 cellOff
14863 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 49 6e 64  set;      /* Ind
14864 65 78 20 69 6e 20 61 44 61 74 61 20 6f 66 20 66  ex in aData of f
14865 69 72 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65  irst cell pointe
14866 72 20 2a 2f 0a 20 20 75 31 36 20 6e 46 72 65 65  r */.  u16 nFree
14867 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ;           /* N
14868 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20 62 79  umber of free by
14869 74 65 73 20 6f 6e 20 74 68 65 20 70 61 67 65 20  tes on the page 
1486a 2a 2f 0a 20 20 75 31 36 20 6e 43 65 6c 6c 3b 20  */.  u16 nCell; 
1486b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
1486c 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20  ber of cells on 
1486d 74 68 69 73 20 70 61 67 65 2c 20 6c 6f 63 61 6c  this page, local
1486e 20 61 6e 64 20 6f 76 66 6c 20 2a 2f 0a 20 20 75   and ovfl */.  u
1486f 31 36 20 6d 61 73 6b 50 61 67 65 3b 20 20 20 20  16 maskPage;    
14870 20 20 20 20 2f 2a 20 4d 61 73 6b 20 66 6f 72 20      /* Mask for 
14871 70 61 67 65 20 6f 66 66 73 65 74 20 2a 2f 0a 20  page offset */. 
14872 20 73 74 72 75 63 74 20 5f 4f 76 66 6c 43 65 6c   struct _OvflCel
14873 6c 20 7b 20 20 20 2f 2a 20 43 65 6c 6c 73 20 74  l {   /* Cells t
14874 68 61 74 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74  hat will not fit
14875 20 6f 6e 20 61 44 61 74 61 5b 5d 20 2a 2f 0a 20   on aData[] */. 
14876 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20     u8 *pCell;   
14877 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
14878 72 73 20 74 6f 20 74 68 65 20 62 6f 64 79 20 6f  rs to the body o
14879 66 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63  f the overflow c
1487a 65 6c 6c 20 2a 2f 0a 20 20 20 20 75 31 36 20 69  ell */.    u16 i
1487b 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  dx;            /
1487c 2a 20 49 6e 73 65 72 74 20 74 68 69 73 20 63 65  * Insert this ce
1487d 6c 6c 20 62 65 66 6f 72 65 20 69 64 78 2d 74 68  ll before idx-th
1487e 20 6e 6f 6e 2d 6f 76 65 72 66 6c 6f 77 20 63 65   non-overflow ce
1487f 6c 6c 20 2a 2f 0a 20 20 7d 20 61 4f 76 66 6c 5b  ll */.  } aOvfl[
14880 35 5d 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  5];.  BtShared *
14881 70 42 74 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f  pBt;       /* Po
14882 69 6e 74 65 72 20 74 6f 20 42 74 53 68 61 72 65  inter to BtShare
14883 64 20 74 68 61 74 20 74 68 69 73 20 70 61 67 65  d that this page
14884 20 69 73 20 70 61 72 74 20 6f 66 20 2a 2f 0a 20   is part of */. 
14885 20 75 38 20 2a 61 44 61 74 61 3b 20 20 20 20 20   u8 *aData;     
14886 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
14887 20 74 6f 20 64 69 73 6b 20 69 6d 61 67 65 20 6f   to disk image o
14888 66 20 74 68 65 20 70 61 67 65 20 64 61 74 61 20  f the page data 
14889 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62  */.  DbPage *pDb
1488a 50 61 67 65 3b 20 20 20 20 20 2f 2a 20 50 61 67  Page;     /* Pag
1488b 65 72 20 70 61 67 65 20 68 61 6e 64 6c 65 20 2a  er page handle *
1488c 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 20 20  /.  Pgno pgno;  
1488d 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65           /* Page
1488e 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 69 73   number for this
1488f 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a   page */.};../*.
14890 2a 2a 20 54 68 65 20 69 6e 2d 6d 65 6d 6f 72 79  ** The in-memory
14891 20 69 6d 61 67 65 20 6f 66 20 61 20 64 69 73 6b   image of a disk
14892 20 70 61 67 65 20 68 61 73 20 74 68 65 20 61 75   page has the au
14893 78 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d 61 74  xiliary informat
14894 69 6f 6e 20 61 70 70 65 6e 64 65 64 0a 2a 2a 20  ion appended.** 
14895 74 6f 20 74 68 65 20 65 6e 64 2e 20 20 45 58 54  to the end.  EXT
14896 52 41 5f 53 49 5a 45 20 69 73 20 74 68 65 20 6e  RA_SIZE is the n
14897 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
14898 66 20 73 70 61 63 65 20 6e 65 65 64 65 64 20 74  f space needed t
14899 6f 20 68 6f 6c 64 0a 2a 2a 20 74 68 61 74 20 65  o hold.** that e
1489a 78 74 72 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  xtra information
1489b 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 54  ..*/.#define EXT
1489c 52 41 5f 53 49 5a 45 20 73 69 7a 65 6f 66 28 4d  RA_SIZE sizeof(M
1489d 65 6d 50 61 67 65 29 0a 0a 2f 2a 0a 2a 2a 20 41  emPage)../*.** A
1489e 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20   linked list of 
1489f 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
148a0 72 75 63 74 75 72 65 73 20 69 73 20 73 74 6f 72  ructures is stor
148a1 65 64 20 61 74 20 42 74 53 68 61 72 65 64 2e 70  ed at BtShared.p
148a2 4c 6f 63 6b 2e 0a 2a 2a 20 4c 6f 63 6b 73 20 61  Lock..** Locks a
148a3 72 65 20 61 64 64 65 64 20 28 6f 72 20 75 70 67  re added (or upg
148a4 72 61 64 65 64 20 66 72 6f 6d 20 52 45 41 44 5f  raded from READ_
148a5 4c 4f 43 4b 20 74 6f 20 57 52 49 54 45 5f 4c 4f  LOCK to WRITE_LO
148a6 43 4b 29 20 77 68 65 6e 20 61 20 63 75 72 73 6f  CK) when a curso
148a7 72 20 0a 2a 2a 20 69 73 20 6f 70 65 6e 65 64 20  r .** is opened 
148a8 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69 74  on the table wit
148a9 68 20 72 6f 6f 74 20 70 61 67 65 20 42 74 53 68  h root page BtSh
148aa 61 72 65 64 2e 69 54 61 62 6c 65 2e 20 4c 6f 63  ared.iTable. Loc
148ab 6b 73 20 61 72 65 20 72 65 6d 6f 76 65 64 0a 2a  ks are removed.*
148ac 2a 20 66 72 6f 6d 20 74 68 69 73 20 6c 69 73 74  * from this list
148ad 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61 63 74   when a transact
148ae 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64  ion is committed
148af 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2c   or rolled back,
148b0 20 6f 72 20 77 68 65 6e 0a 2a 2a 20 61 20 62 74   or when.** a bt
148b1 72 65 65 20 68 61 6e 64 6c 65 20 69 73 20 63 6c  ree handle is cl
148b2 6f 73 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  osed..*/.struct 
148b3 42 74 4c 6f 63 6b 20 7b 0a 20 20 42 74 72 65 65  BtLock {.  Btree
148b4 20 2a 70 42 74 72 65 65 3b 20 20 20 20 20 20 20   *pBtree;       
148b5 20 2f 2a 20 42 74 72 65 65 20 68 61 6e 64 6c 65   /* Btree handle
148b6 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 6c 6f   holding this lo
148b7 63 6b 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 54 61  ck */.  Pgno iTa
148b8 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ble;          /*
148b9 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 61   Root page of ta
148ba 62 6c 65 20 2a 2f 0a 20 20 75 38 20 65 4c 6f 63  ble */.  u8 eLoc
148bb 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  k;             /
148bc 2a 20 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 57  * READ_LOCK or W
148bd 52 49 54 45 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 42  RITE_LOCK */.  B
148be 74 4c 6f 63 6b 20 2a 70 4e 65 78 74 3b 20 20 20  tLock *pNext;   
148bf 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20       /* Next in 
148c0 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 20 6c  BtShared.pLock l
148c1 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 43 61  ist */.};../* Ca
148c2 6e 64 69 64 61 74 65 20 76 61 6c 75 65 73 20 66  ndidate values f
148c3 6f 72 20 42 74 4c 6f 63 6b 2e 65 4c 6f 63 6b 20  or BtLock.eLock 
148c4 2a 2f 0a 23 64 65 66 69 6e 65 20 52 45 41 44 5f  */.#define READ_
148c5 4c 4f 43 4b 20 20 20 20 20 31 0a 23 64 65 66 69  LOCK     1.#defi
148c6 6e 65 20 57 52 49 54 45 5f 4c 4f 43 4b 20 20 20  ne WRITE_LOCK   
148c7 20 32 0a 0a 2f 2a 20 41 20 42 74 72 65 65 20 68   2../* A Btree h
148c8 61 6e 64 6c 65 0a 2a 2a 0a 2a 2a 20 41 20 64 61  andle.**.** A da
148c9 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
148ca 6e 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69  n contains a poi
148cb 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61  nter to an insta
148cc 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20 6f  nce of.** this o
148cd 62 6a 65 63 74 20 66 6f 72 20 65 76 65 72 79 20  bject for every 
148ce 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 68  database file th
148cf 61 74 20 69 74 20 68 61 73 20 6f 70 65 6e 2e 20  at it has open. 
148d0 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 0a   This structure.
148d1 2a 2a 20 69 73 20 6f 70 61 71 75 65 20 74 6f 20  ** is opaque to 
148d2 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  the database con
148d3 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 64 61  nection.  The da
148d4 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
148d5 6e 20 63 61 6e 6e 6f 74 0a 2a 2a 20 73 65 65 20  n cannot.** see 
148d6 74 68 65 20 69 6e 74 65 72 6e 61 6c 73 20 6f 66  the internals of
148d7 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
148d8 61 6e 64 20 6f 6e 6c 79 20 64 65 61 6c 73 20 77  and only deals w
148d9 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a  ith pointers to.
148da 2a 2a 20 74 68 69 73 20 73 74 72 75 63 74 75 72  ** this structur
148db 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 73 6f 6d  e..**.** For som
148dc 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73  e database files
148dd 2c 20 74 68 65 20 73 61 6d 65 20 75 6e 64 65 72  , the same under
148de 6c 79 69 6e 67 20 64 61 74 61 62 61 73 65 20 63  lying database c
148df 61 63 68 65 20 6d 69 67 68 74 20 62 65 20 0a 2a  ache might be .*
148e0 2a 20 73 68 61 72 65 64 20 62 65 74 77 65 65 6e  * shared between
148e1 20 6d 75 6c 74 69 70 6c 65 20 63 6f 6e 6e 65 63   multiple connec
148e2 74 69 6f 6e 73 2e 20 20 49 6e 20 74 68 61 74 20  tions.  In that 
148e3 63 61 73 65 2c 20 65 61 63 68 20 63 6f 6e 74 65  case, each conte
148e4 63 74 69 6f 6e 0a 2a 2a 20 68 61 73 20 69 74 20  ction.** has it 
148e5 6f 77 6e 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  own pointer to t
148e6 68 69 73 20 6f 62 6a 65 63 74 2e 20 20 42 75 74  his object.  But
148e7 20 65 61 63 68 20 69 6e 73 74 61 6e 63 65 20 6f   each instance o
148e8 66 20 74 68 69 73 20 6f 62 6a 65 63 74 0a 2a 2a  f this object.**
148e9 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 73   points to the s
148ea 61 6d 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a  ame BtShared obj
148eb 65 63 74 2e 20 20 54 68 65 20 64 61 74 61 62 61  ect.  The databa
148ec 73 65 20 63 61 63 68 65 20 61 6e 64 20 74 68 65  se cache and the
148ed 0a 2a 2a 20 73 63 68 65 6d 61 20 61 73 73 6f 63  .** schema assoc
148ee 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 64  iated with the d
148ef 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 72 65  atabase file are
148f0 20 61 6c 6c 20 63 6f 6e 74 61 69 6e 65 64 20 77   all contained w
148f1 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 42 74 53  ithin.** the BtS
148f2 68 61 72 65 64 20 6f 62 6a 65 63 74 2e 0a 2a 2a  hared object..**
148f3 0a 2a 2a 20 41 6c 6c 20 66 69 65 6c 64 73 20 69  .** All fields i
148f4 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  n this structure
148f5 20 61 72 65 20 61 63 63 65 73 73 65 64 20 75 6e   are accessed un
148f6 64 65 72 20 73 71 6c 69 74 65 33 2e 6d 75 74 65  der sqlite3.mute
148f7 78 2e 0a 2a 2a 20 54 68 65 20 70 42 74 20 70 6f  x..** The pBt po
148f8 69 6e 74 65 72 20 69 74 73 65 6c 66 20 6d 61 79  inter itself may
148f9 20 6e 6f 74 20 62 65 20 63 68 61 6e 67 65 64 20   not be changed 
148fa 77 68 69 6c 65 20 74 68 65 72 65 20 65 78 69 73  while there exis
148fb 74 73 20 63 75 72 73 6f 72 73 20 0a 2a 2a 20 69  ts cursors .** i
148fc 6e 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 64  n the referenced
148fd 20 42 74 53 68 61 72 65 64 20 74 68 61 74 20 70   BtShared that p
148fe 6f 69 6e 74 20 62 61 63 6b 20 74 6f 20 74 68 69  oint back to thi
148ff 73 20 42 74 72 65 65 20 73 69 6e 63 65 20 74 68  s Btree since th
14900 6f 73 65 0a 2a 2a 20 63 75 72 73 6f 72 73 20 68  ose.** cursors h
14901 61 76 65 20 74 6f 20 64 6f 20 67 6f 20 74 68 72  ave to do go thr
14902 6f 75 67 68 20 74 68 69 73 20 42 74 72 65 65 20  ough this Btree 
14903 74 6f 20 66 69 6e 64 20 74 68 65 69 72 20 42 74  to find their Bt
14904 53 68 61 72 65 64 20 61 6e 64 0a 2a 2a 20 74 68  Shared and.** th
14905 65 79 20 6f 66 74 65 6e 20 64 6f 20 73 6f 20 77  ey often do so w
14906 69 74 68 6f 75 74 20 68 6f 6c 64 69 6e 67 20 73  ithout holding s
14907 71 6c 69 74 65 33 2e 6d 75 74 65 78 2e 0a 2a 2f  qlite3.mutex..*/
14908 0a 73 74 72 75 63 74 20 42 74 72 65 65 20 7b 0a  .struct Btree {.
14909 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
1490a 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
1490b 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
1490c 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 62 74 72  holding this btr
1490d 65 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64  ee */.  BtShared
1490e 20 2a 70 42 74 3b 20 20 20 20 20 2f 2a 20 53 68   *pBt;     /* Sh
1490f 61 72 61 62 6c 65 20 63 6f 6e 74 65 6e 74 20 6f  arable content o
14910 66 20 74 68 69 73 20 62 74 72 65 65 20 2a 2f 0a  f this btree */.
14911 20 20 75 38 20 69 6e 54 72 61 6e 73 3b 20 20 20    u8 inTrans;   
14912 20 20 20 20 20 2f 2a 20 54 52 41 4e 53 5f 4e 4f       /* TRANS_NO
14913 4e 45 2c 20 54 52 41 4e 53 5f 52 45 41 44 20 6f  NE, TRANS_READ o
14914 72 20 54 52 41 4e 53 5f 57 52 49 54 45 20 2a 2f  r TRANS_WRITE */
14915 0a 20 20 75 38 20 73 68 61 72 61 62 6c 65 3b 20  .  u8 sharable; 
14916 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
14917 20 77 65 20 63 61 6e 20 73 68 61 72 65 20 70 42   we can share pB
14918 74 20 77 69 74 68 20 61 6e 6f 74 68 65 72 20 64  t with another d
14919 62 20 2a 2f 0a 20 20 75 38 20 6c 6f 63 6b 65 64  b */.  u8 locked
1491a 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  ;         /* Tru
1491b 65 20 69 66 20 64 62 20 63 75 72 72 65 6e 74 6c  e if db currentl
1491c 79 20 68 61 73 20 70 42 74 20 6c 6f 63 6b 65 64  y has pBt locked
1491d 20 2a 2f 0a 20 20 69 6e 74 20 77 61 6e 74 54 6f   */.  int wantTo
1491e 4c 6f 63 6b 3b 20 20 20 20 2f 2a 20 4e 75 6d 62  Lock;    /* Numb
1491f 65 72 20 6f 66 20 6e 65 73 74 65 64 20 63 61 6c  er of nested cal
14920 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72  ls to sqlite3Btr
14921 65 65 45 6e 74 65 72 28 29 20 2a 2f 0a 20 20 69  eeEnter() */.  i
14922 6e 74 20 6e 42 61 63 6b 75 70 3b 20 20 20 20 20  nt nBackup;     
14923 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
14924 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 73  ackup operations
14925 20 72 65 61 64 69 6e 67 20 74 68 69 73 20 62 74   reading this bt
14926 72 65 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a  ree */.  Btree *
14927 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4c  pNext;      /* L
14928 69 73 74 20 6f 66 20 6f 74 68 65 72 20 73 68 61  ist of other sha
14929 72 61 62 6c 65 20 42 74 72 65 65 73 20 66 72 6f  rable Btrees fro
1492a 6d 20 74 68 65 20 73 61 6d 65 20 64 62 20 2a 2f  m the same db */
1492b 0a 20 20 42 74 72 65 65 20 2a 70 50 72 65 76 3b  .  Btree *pPrev;
1492c 20 20 20 20 20 20 2f 2a 20 42 61 63 6b 20 70 6f        /* Back po
1492d 69 6e 74 65 72 20 6f 66 20 74 68 65 20 73 61 6d  inter of the sam
1492e 65 20 6c 69 73 74 20 2a 2f 0a 23 69 66 6e 64 65  e list */.#ifnde
1492f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
14930 41 52 45 44 5f 43 41 43 48 45 0a 20 20 42 74 4c  ARED_CACHE.  BtL
14931 6f 63 6b 20 6c 6f 63 6b 3b 20 20 20 20 20 20 20  ock lock;       
14932 2f 2a 20 4f 62 6a 65 63 74 20 75 73 65 64 20 74  /* Object used t
14933 6f 20 6c 6f 63 6b 20 70 61 67 65 20 31 20 2a 2f  o lock page 1 */
14934 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a  .#endif.};../*.*
14935 2a 20 42 74 72 65 65 2e 69 6e 54 72 61 6e 73 20  * Btree.inTrans 
14936 6d 61 79 20 74 61 6b 65 20 6f 6e 65 20 6f 66 20  may take one of 
14937 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61  the following va
14938 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  lues..**.** If t
14939 68 65 20 73 68 61 72 65 64 2d 64 61 74 61 20 65  he shared-data e
1493a 78 74 65 6e 73 69 6f 6e 20 69 73 20 65 6e 61 62  xtension is enab
1493b 6c 65 64 2c 20 74 68 65 72 65 20 6d 61 79 20 62  led, there may b
1493c 65 20 6d 75 6c 74 69 70 6c 65 20 75 73 65 72 73  e multiple users
1493d 0a 2a 2a 20 6f 66 20 74 68 65 20 42 74 72 65 65  .** of the Btree
1493e 20 73 74 72 75 63 74 75 72 65 2e 20 41 74 20 6d   structure. At m
1493f 6f 73 74 20 6f 6e 65 20 6f 66 20 74 68 65 73 65  ost one of these
14940 20 6d 61 79 20 6f 70 65 6e 20 61 20 77 72 69 74   may open a writ
14941 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 2a  e transaction,.*
14942 2a 20 62 75 74 20 61 6e 79 20 6e 75 6d 62 65 72  * but any number
14943 20 6d 61 79 20 68 61 76 65 20 61 63 74 69 76 65   may have active
14944 20 72 65 61 64 20 74 72 61 6e 73 61 63 74 69 6f   read transactio
14945 6e 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54  ns..*/.#define T
14946 52 41 4e 53 5f 4e 4f 4e 45 20 20 30 0a 23 64 65  RANS_NONE  0.#de
14947 66 69 6e 65 20 54 52 41 4e 53 5f 52 45 41 44 20  fine TRANS_READ 
14948 20 31 0a 23 64 65 66 69 6e 65 20 54 52 41 4e 53   1.#define TRANS
14949 5f 57 52 49 54 45 20 32 0a 0a 2f 2a 0a 2a 2a 20  _WRITE 2../*.** 
1494a 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  An instance of t
1494b 68 69 73 20 6f 62 6a 65 63 74 20 72 65 70 72 65  his object repre
1494c 73 65 6e 74 73 20 61 20 73 69 6e 67 6c 65 20 64  sents a single d
1494d 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a  atabase file..**
1494e 20 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 64 61   .** A single da
1494f 74 61 62 61 73 65 20 66 69 6c 65 20 63 61 6e 20  tabase file can 
14950 62 65 20 69 6e 20 75 73 65 20 61 73 20 74 68 65  be in use as the
14951 20 73 61 6d 65 20 74 69 6d 65 20 62 79 20 74 77   same time by tw
14952 6f 0a 2a 2a 20 6f 72 20 6d 6f 72 65 20 64 61 74  o.** or more dat
14953 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
14954 73 2e 20 20 57 68 65 6e 20 74 77 6f 20 6f 72 20  s.  When two or 
14955 6d 6f 72 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  more connections
14956 20 61 72 65 0a 2a 2a 20 73 68 61 72 69 6e 67 20   are.** sharing 
14957 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
14958 65 20 66 69 6c 65 2c 20 65 61 63 68 20 63 6f 6e  e file, each con
14959 6e 65 63 74 69 6f 6e 20 68 61 73 20 69 74 20 6f  nection has it o
1495a 77 6e 0a 2a 2a 20 70 72 69 76 61 74 65 20 42 74  wn.** private Bt
1495b 72 65 65 20 6f 62 6a 65 63 74 20 66 6f 72 20 74  ree object for t
1495c 68 65 20 66 69 6c 65 20 61 6e 64 20 65 61 63 68  he file and each
1495d 20 6f 66 20 74 68 6f 73 65 20 42 74 72 65 65 73   of those Btrees
1495e 20 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 74 68   points.** to th
1495f 69 73 20 6f 6e 65 20 42 74 53 68 61 72 65 64 20  is one BtShared 
14960 6f 62 6a 65 63 74 2e 20 20 42 74 53 68 61 72 65  object.  BtShare
14961 64 2e 6e 52 65 66 20 69 73 20 74 68 65 20 6e 75  d.nRef is the nu
14962 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 6f 6e 6e 65  mber of.** conne
14963 63 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 6c 79  ctions currently
14964 20 73 68 61 72 69 6e 67 20 74 68 69 73 20 64 61   sharing this da
14965 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a  tabase file..**.
14966 2a 2a 20 46 69 65 6c 64 73 20 69 6e 20 74 68 69  ** Fields in thi
14967 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20  s structure are 
14968 61 63 63 65 73 73 65 64 20 75 6e 64 65 72 20 74  accessed under t
14969 68 65 20 42 74 53 68 61 72 65 64 2e 6d 75 74 65  he BtShared.mute
1496a 78 0a 2a 2a 20 6d 75 74 65 78 2c 20 65 78 63 65  x.** mutex, exce
1496b 70 74 20 66 6f 72 20 6e 52 65 66 20 61 6e 64 20  pt for nRef and 
1496c 70 4e 65 78 74 20 77 68 69 63 68 20 61 72 65 20  pNext which are 
1496d 61 63 63 65 73 73 65 64 20 75 6e 64 65 72 20 74  accessed under t
1496e 68 65 0a 2a 2a 20 67 6c 6f 62 61 6c 20 53 51 4c  he.** global SQL
1496f 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
14970 5f 4d 41 53 54 45 52 20 6d 75 74 65 78 2e 20 20  _MASTER mutex.  
14971 54 68 65 20 70 50 61 67 65 72 20 66 69 65 6c 64  The pPager field
14972 0a 2a 2a 20 6d 61 79 20 6e 6f 74 20 62 65 20 6d  .** may not be m
14973 6f 64 69 66 69 65 64 20 6f 6e 63 65 20 69 74 20  odified once it 
14974 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 73 65 74  is initially set
14975 20 61 73 20 6c 6f 6e 67 20 61 73 20 6e 52 65 66   as long as nRef
14976 3e 30 2e 0a 2a 2a 20 54 68 65 20 70 53 63 68 65  >0..** The pSche
14977 6d 61 20 66 69 65 6c 64 20 6d 61 79 20 62 65 20  ma field may be 
14978 73 65 74 20 6f 6e 63 65 20 75 6e 64 65 72 20 42  set once under B
14979 74 53 68 61 72 65 64 2e 6d 75 74 65 78 20 61 6e  tShared.mutex an
1497a 64 0a 2a 2a 20 74 68 65 72 65 61 66 74 65 72 20  d.** thereafter 
1497b 69 73 20 75 6e 63 68 61 6e 67 65 64 20 61 73 20  is unchanged as 
1497c 6c 6f 6e 67 20 61 73 20 6e 52 65 66 3e 30 2e 0a  long as nRef>0..
1497d 2a 2a 0a 2a 2a 20 69 73 50 65 6e 64 69 6e 67 3a  **.** isPending:
1497e 0a 2a 2a 0a 2a 2a 20 20 20 49 66 20 61 20 42 74  .**.**   If a Bt
1497f 53 68 61 72 65 64 20 63 6c 69 65 6e 74 20 66 61  Shared client fa
14980 69 6c 73 20 74 6f 20 6f 62 74 61 69 6e 20 61 20  ils to obtain a 
14981 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 61 20  write-lock on a 
14982 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 74 61  database.**   ta
14983 62 6c 65 20 28 62 65 63 61 75 73 65 20 74 68 65  ble (because the
14984 72 65 20 65 78 69 73 74 73 20 6f 6e 65 20 6f 72  re exists one or
14985 20 6d 6f 72 65 20 72 65 61 64 2d 6c 6f 63 6b 73   more read-locks
14986 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 29 2c 0a   on the table),.
14987 2a 2a 20 20 20 74 68 65 20 73 68 61 72 65 64 2d  **   the shared-
14988 63 61 63 68 65 20 65 6e 74 65 72 73 20 27 70 65  cache enters 'pe
14989 6e 64 69 6e 67 2d 6c 6f 63 6b 27 20 73 74 61 74  nding-lock' stat
1498a 65 20 61 6e 64 20 69 73 50 65 6e 64 69 6e 67 20  e and isPending 
1498b 69 73 0a 2a 2a 20 20 20 73 65 74 20 74 6f 20 74  is.**   set to t
1498c 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65  rue..**.**   The
1498d 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6c 65   shared-cache le
1498e 61 76 65 73 20 74 68 65 20 27 70 65 6e 64 69 6e  aves the 'pendin
1498f 67 20 6c 6f 63 6b 27 20 73 74 61 74 65 20 77 68  g lock' state wh
14990 65 6e 20 65 69 74 68 65 72 20 6f 66 0a 2a 2a 20  en either of.** 
14991 20 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    the following 
14992 6f 63 63 75 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  occur:.**.**    
14993 20 31 29 20 54 68 65 20 63 75 72 72 65 6e 74 20   1) The current 
14994 77 72 69 74 65 72 20 28 42 74 53 68 61 72 65 64  writer (BtShared
14995 2e 70 57 72 69 74 65 72 29 20 63 6f 6e 63 6c 75  .pWriter) conclu
14996 64 65 73 20 69 74 73 20 74 72 61 6e 73 61 63 74  des its transact
14997 69 6f 6e 2c 20 4f 52 0a 2a 2a 20 20 20 20 20 32  ion, OR.**     2
14998 29 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  ) The number of 
14999 6c 6f 63 6b 73 20 68 65 6c 64 20 62 79 20 6f 74  locks held by ot
1499a 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  her connections 
1499b 64 72 6f 70 73 20 74 6f 20 7a 65 72 6f 2e 0a 2a  drops to zero..*
1499c 2a 0a 2a 2a 20 20 20 77 68 69 6c 65 20 69 6e 20  *.**   while in 
1499d 74 68 65 20 27 70 65 6e 64 69 6e 67 2d 6c 6f 63  the 'pending-loc
1499e 6b 27 20 73 74 61 74 65 2c 20 6e 6f 20 63 6f 6e  k' state, no con
1499f 6e 65 63 74 69 6f 6e 20 6d 61 79 20 73 74 61 72  nection may star
149a0 74 20 61 20 6e 65 77 0a 2a 2a 20 20 20 74 72 61  t a new.**   tra
149a1 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  nsaction..**.** 
149a2 20 20 54 68 69 73 20 66 65 61 74 75 72 65 20 69    This feature i
149a3 73 20 69 6e 63 6c 75 64 65 64 20 74 6f 20 68 65  s included to he
149a4 6c 70 20 70 72 65 76 65 6e 74 20 77 72 69 74 65  lp prevent write
149a5 72 2d 73 74 61 72 76 61 74 69 6f 6e 2e 0a 2a 2f  r-starvation..*/
149a6 0a 73 74 72 75 63 74 20 42 74 53 68 61 72 65 64  .struct BtShared
149a7 20 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67   {.  Pager *pPag
149a8 65 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68  er;        /* Th
149a9 65 20 70 61 67 65 20 63 61 63 68 65 20 2a 2f 0a  e page cache */.
149aa 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
149ab 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
149ac 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63  ase connection c
149ad 75 72 72 65 6e 74 6c 79 20 75 73 69 6e 67 20 74  urrently using t
149ae 68 69 73 20 42 74 72 65 65 20 2a 2f 0a 20 20 42  his Btree */.  B
149af 74 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  tCursor *pCursor
149b0 3b 20 20 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f  ;    /* A list o
149b1 66 20 61 6c 6c 20 6f 70 65 6e 20 63 75 72 73 6f  f all open curso
149b2 72 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20  rs */.  MemPage 
149b3 2a 70 50 61 67 65 31 3b 20 20 20 20 20 20 2f 2a  *pPage1;      /*
149b4 20 46 69 72 73 74 20 70 61 67 65 20 6f 66 20 74   First page of t
149b5 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  he database */. 
149b6 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20 20   u8 readOnly;   
149b7 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
149b8 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  f the underlying
149b9 20 66 69 6c 65 20 69 73 20 72 65 61 64 6f 6e 6c   file is readonl
149ba 79 20 2a 2f 0a 20 20 75 38 20 70 61 67 65 53 69  y */.  u8 pageSi
149bb 7a 65 46 69 78 65 64 3b 20 20 20 20 20 2f 2a 20  zeFixed;     /* 
149bc 54 72 75 65 20 69 66 20 74 68 65 20 70 61 67 65  True if the page
149bd 20 73 69 7a 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e   size can no lon
149be 67 65 72 20 62 65 20 63 68 61 6e 67 65 64 20 2a  ger be changed *
149bf 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
149c0 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
149c1 0a 20 20 75 38 20 61 75 74 6f 56 61 63 75 75 6d  .  u8 autoVacuum
149c2 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65  ;        /* True
149c3 20 69 66 20 61 75 74 6f 2d 76 61 63 75 75 6d 20   if auto-vacuum 
149c4 69 73 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20  is enabled */.  
149c5 75 38 20 69 6e 63 72 56 61 63 75 75 6d 3b 20 20  u8 incrVacuum;  
149c6 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
149c7 20 69 6e 63 72 2d 76 61 63 75 75 6d 20 69 73 20   incr-vacuum is 
149c8 65 6e 61 62 6c 65 64 20 2a 2f 0a 23 65 6e 64 69  enabled */.#endi
149c9 66 0a 20 20 75 31 36 20 70 61 67 65 53 69 7a 65  f.  u16 pageSize
149ca 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74  ;         /* Tot
149cb 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  al number of byt
149cc 65 73 20 6f 6e 20 61 20 70 61 67 65 20 2a 2f 0a  es on a page */.
149cd 20 20 75 31 36 20 75 73 61 62 6c 65 53 69 7a 65    u16 usableSize
149ce 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  ;       /* Numbe
149cf 72 20 6f 66 20 75 73 61 62 6c 65 20 62 79 74 65  r of usable byte
149d0 73 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 2a  s on each page *
149d1 2f 0a 20 20 75 31 36 20 6d 61 78 4c 6f 63 61 6c  /.  u16 maxLocal
149d2 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78  ;         /* Max
149d3 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f  imum local paylo
149d4 61 64 20 69 6e 20 6e 6f 6e 2d 4c 45 41 46 44 41  ad in non-LEAFDA
149d5 54 41 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 75  TA tables */.  u
149d6 31 36 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 20 20  16 minLocal;    
149d7 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20       /* Minimum 
149d8 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e  local payload in
149d9 20 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 20 74 61   non-LEAFDATA ta
149da 62 6c 65 73 20 2a 2f 0a 20 20 75 31 36 20 6d 61  bles */.  u16 ma
149db 78 4c 65 61 66 3b 20 20 20 20 20 20 20 20 20 20  xLeaf;          
149dc 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 6f 63 61 6c  /* Maximum local
149dd 20 70 61 79 6c 6f 61 64 20 69 6e 20 61 20 4c 45   payload in a LE
149de 41 46 44 41 54 41 20 74 61 62 6c 65 20 2a 2f 0a  AFDATA table */.
149df 20 20 75 31 36 20 6d 69 6e 4c 65 61 66 3b 20 20    u16 minLeaf;  
149e0 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d          /* Minim
149e1 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64  um local payload
149e2 20 69 6e 20 61 20 4c 45 41 46 44 41 54 41 20 74   in a LEAFDATA t
149e3 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20 69 6e 54  able */.  u8 inT
149e4 72 61 6e 73 61 63 74 69 6f 6e 3b 20 20 20 20 20  ransaction;     
149e5 2f 2a 20 54 72 61 6e 73 61 63 74 69 6f 6e 20 73  /* Transaction s
149e6 74 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 54  tate */.  int nT
149e7 72 61 6e 73 61 63 74 69 6f 6e 3b 20 20 20 20 20  ransaction;     
149e8 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 70 65  /* Number of ope
149e9 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 28  n transactions (
149ea 72 65 61 64 20 2b 20 77 72 69 74 65 29 20 2a 2f  read + write) */
149eb 0a 20 20 76 6f 69 64 20 2a 70 53 63 68 65 6d 61  .  void *pSchema
149ec 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e  ;        /* Poin
149ed 74 65 72 20 74 6f 20 73 70 61 63 65 20 61 6c 6c  ter to space all
149ee 6f 63 61 74 65 64 20 62 79 20 73 71 6c 69 74 65  ocated by sqlite
149ef 33 42 74 72 65 65 53 63 68 65 6d 61 28 29 20 2a  3BtreeSchema() *
149f0 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46 72 65 65  /.  void (*xFree
149f1 53 63 68 65 6d 61 29 28 76 6f 69 64 2a 29 3b 20  Schema)(void*); 
149f2 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66   /* Destructor f
149f3 6f 72 20 42 74 53 68 61 72 65 64 2e 70 53 63 68  or BtShared.pSch
149f4 65 6d 61 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ema */.  sqlite3
149f5 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 2f  _mutex *mutex; /
149f6 2a 20 4e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20  * Non-recursive 
149f7 6d 75 74 65 78 20 72 65 71 75 69 72 65 64 20 74  mutex required t
149f8 6f 20 61 63 63 65 73 73 20 74 68 69 73 20 73 74  o access this st
149f9 72 75 63 74 20 2a 2f 0a 20 20 42 69 74 76 65 63  ruct */.  Bitvec
149fa 20 2a 70 48 61 73 43 6f 6e 74 65 6e 74 3b 20 20   *pHasContent;  
149fb 2f 2a 20 53 65 74 20 6f 66 20 70 61 67 65 73 20  /* Set of pages 
149fc 6d 6f 76 65 64 20 74 6f 20 66 72 65 65 2d 6c 69  moved to free-li
149fd 73 74 20 74 68 69 73 20 74 72 61 6e 73 61 63 74  st this transact
149fe 69 6f 6e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ion */.#ifndef S
149ff 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
14a00 44 5f 43 41 43 48 45 0a 20 20 69 6e 74 20 6e 52  D_CACHE.  int nR
14a01 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ef;             
14a02 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66  /* Number of ref
14a03 65 72 65 6e 63 65 73 20 74 6f 20 74 68 69 73 20  erences to this 
14a04 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 42  structure */.  B
14a05 74 53 68 61 72 65 64 20 2a 70 4e 65 78 74 3b 20  tShared *pNext; 
14a06 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 6f 6e 20       /* Next on 
14a07 61 20 6c 69 73 74 20 6f 66 20 73 68 61 72 61 62  a list of sharab
14a08 6c 65 20 42 74 53 68 61 72 65 64 20 73 74 72 75  le BtShared stru
14a09 63 74 73 20 2a 2f 0a 20 20 42 74 4c 6f 63 6b 20  cts */.  BtLock 
14a0a 2a 70 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f  *pLock;        /
14a0b 2a 20 4c 69 73 74 20 6f 66 20 6c 6f 63 6b 73 20  * List of locks 
14a0c 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 73 68 61  held on this sha
14a0d 72 65 64 2d 62 74 72 65 65 20 73 74 72 75 63 74  red-btree struct
14a0e 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 57 72   */.  Btree *pWr
14a0f 69 74 65 72 3b 20 20 20 20 20 20 20 2f 2a 20 42  iter;       /* B
14a10 74 72 65 65 20 77 69 74 68 20 63 75 72 72 65 6e  tree with curren
14a11 74 6c 79 20 6f 70 65 6e 20 77 72 69 74 65 20 74  tly open write t
14a12 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20  ransaction */.  
14a13 75 38 20 69 73 45 78 63 6c 75 73 69 76 65 3b 20  u8 isExclusive; 
14a14 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
14a15 20 70 57 72 69 74 65 72 20 68 61 73 20 61 6e 20   pWriter has an 
14a16 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f  EXCLUSIVE lock o
14a17 6e 20 74 68 65 20 64 62 20 2a 2f 0a 20 20 75 38  n the db */.  u8
14a18 20 69 73 50 65 6e 64 69 6e 67 3b 20 20 20 20 20   isPending;     
14a19 20 20 20 20 2f 2a 20 49 66 20 77 61 69 74 69 6e      /* If waitin
14a1a 67 20 66 6f 72 20 72 65 61 64 2d 6c 6f 63 6b 73  g for read-locks
14a1b 20 74 6f 20 63 6c 65 61 72 20 2a 2f 0a 23 65 6e   to clear */.#en
14a1c 64 69 66 0a 20 20 75 38 20 2a 70 54 6d 70 53 70  dif.  u8 *pTmpSp
14a1d 61 63 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 42  ace;        /* B
14a1e 74 53 68 61 72 65 64 2e 70 61 67 65 53 69 7a 65  tShared.pageSize
14a1f 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20   bytes of space 
14a20 66 6f 72 20 74 6d 70 20 75 73 65 20 2a 2f 0a 7d  for tmp use */.}
14a21 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74  ;../*.** An inst
14a22 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
14a23 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
14a24 69 73 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20  is used to hold 
14a25 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61  information.** a
14a26 62 6f 75 74 20 61 20 63 65 6c 6c 2e 20 20 54 68  bout a cell.  Th
14a27 65 20 70 61 72 73 65 43 65 6c 6c 50 74 72 28 29  e parseCellPtr()
14a28 20 66 75 6e 63 74 69 6f 6e 20 66 69 6c 6c 73 20   function fills 
14a29 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72  in this structur
14a2a 65 0a 2a 2a 20 62 61 73 65 64 20 6f 6e 20 69 6e  e.** based on in
14a2b 66 6f 72 6d 61 74 69 6f 6e 20 65 78 74 72 61 63  formation extrac
14a2c 74 20 66 72 6f 6d 20 74 68 65 20 72 61 77 20 64  t from the raw d
14a2d 69 73 6b 20 70 61 67 65 2e 0a 2a 2f 0a 74 79 70  isk page..*/.typ
14a2e 65 64 65 66 20 73 74 72 75 63 74 20 43 65 6c 6c  edef struct Cell
14a2f 49 6e 66 6f 20 43 65 6c 6c 49 6e 66 6f 3b 0a 73  Info CellInfo;.s
14a30 74 72 75 63 74 20 43 65 6c 6c 49 6e 66 6f 20 7b  truct CellInfo {
14a31 0a 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20  .  u8 *pCell;   
14a32 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
14a33 74 68 65 20 73 74 61 72 74 20 6f 66 20 63 65 6c  the start of cel
14a34 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 69  l content */.  i
14a35 36 34 20 6e 4b 65 79 3b 20 20 20 20 20 20 2f 2a  64 nKey;      /*
14a36 20 54 68 65 20 6b 65 79 20 66 6f 72 20 49 4e 54   The key for INT
14a37 4b 45 59 20 74 61 62 6c 65 73 2c 20 6f 72 20 6e  KEY tables, or n
14a38 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
14a39 6e 20 6b 65 79 20 2a 2f 0a 20 20 75 33 32 20 6e  n key */.  u32 n
14a3a 44 61 74 61 3b 20 20 20 20 20 2f 2a 20 4e 75 6d  Data;     /* Num
14a3b 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20  ber of bytes of 
14a3c 64 61 74 61 20 2a 2f 0a 20 20 75 33 32 20 6e 50  data */.  u32 nP
14a3d 61 79 6c 6f 61 64 3b 20 20 2f 2a 20 54 6f 74 61  ayload;  /* Tota
14a3e 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c  l amount of payl
14a3f 6f 61 64 20 2a 2f 0a 20 20 75 31 36 20 6e 48 65  oad */.  u16 nHe
14a40 61 64 65 72 3b 20 20 20 2f 2a 20 53 69 7a 65 20  ader;   /* Size 
14a41 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  of the cell cont
14a42 65 6e 74 20 68 65 61 64 65 72 20 69 6e 20 62 79  ent header in by
14a43 74 65 73 20 2a 2f 0a 20 20 75 31 36 20 6e 4c 6f  tes */.  u16 nLo
14a44 63 61 6c 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e  cal;    /* Amoun
14a45 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c  t of payload hel
14a46 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 75  d locally */.  u
14a47 31 36 20 69 4f 76 65 72 66 6c 6f 77 3b 20 2f 2a  16 iOverflow; /*
14a48 20 4f 66 66 73 65 74 20 74 6f 20 6f 76 65 72 66   Offset to overf
14a49 6c 6f 77 20 70 61 67 65 20 6e 75 6d 62 65 72 2e  low page number.
14a4a 20 20 5a 65 72 6f 20 69 66 20 6e 6f 20 6f 76 65    Zero if no ove
14a4b 72 66 6c 6f 77 20 2a 2f 0a 20 20 75 31 36 20 6e  rflow */.  u16 n
14a4c 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20 53 69 7a  Size;     /* Siz
14a4d 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f  e of the cell co
14a4e 6e 74 65 6e 74 20 6f 6e 20 74 68 65 20 6d 61 69  ntent on the mai
14a4f 6e 20 62 2d 74 72 65 65 20 70 61 67 65 20 2a 2f  n b-tree page */
14a50 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d  .};../*.** Maxim
14a51 75 6d 20 64 65 70 74 68 20 6f 66 20 61 6e 20 53  um depth of an S
14a52 51 4c 69 74 65 20 42 2d 54 72 65 65 20 73 74 72  QLite B-Tree str
14a53 75 63 74 75 72 65 2e 20 41 6e 79 20 42 2d 54 72  ucture. Any B-Tr
14a54 65 65 20 64 65 65 70 65 72 20 74 68 61 6e 0a 2a  ee deeper than.*
14a55 2a 20 74 68 69 73 20 77 69 6c 6c 20 62 65 20 64  * this will be d
14a56 65 63 6c 61 72 65 64 20 63 6f 72 72 75 70 74 2e  eclared corrupt.
14a57 20 54 68 69 73 20 76 61 6c 75 65 20 69 73 20 63   This value is c
14a58 61 6c 63 75 6c 61 74 65 64 20 62 61 73 65 64 20  alculated based 
14a59 6f 6e 20 61 0a 2a 2a 20 6d 61 78 69 6d 75 6d 20  on a.** maximum 
14a5a 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 6f 66  database size of
14a5b 20 32 5e 33 31 20 70 61 67 65 73 20 61 20 6d 69   2^31 pages a mi
14a5c 6e 69 6d 75 6d 20 66 61 6e 6f 75 74 20 6f 66 20  nimum fanout of 
14a5d 32 20 66 6f 72 20 61 0a 2a 2a 20 72 6f 6f 74 2d  2 for a.** root-
14a5e 6e 6f 64 65 20 61 6e 64 20 33 20 66 6f 72 20 61  node and 3 for a
14a5f 6c 6c 20 6f 74 68 65 72 20 69 6e 74 65 72 6e 61  ll other interna
14a60 6c 20 6e 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 49  l nodes..**.** I
14a61 66 20 61 20 74 72 65 65 20 74 68 61 74 20 61 70  f a tree that ap
14a62 70 65 61 72 73 20 74 6f 20 62 65 20 74 61 6c 6c  pears to be tall
14a63 65 72 20 74 68 61 6e 20 74 68 69 73 20 69 73 20  er than this is 
14a64 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 69 74 20  encountered, it 
14a65 69 73 0a 2a 2a 20 61 73 73 75 6d 65 64 20 74 68  is.** assumed th
14a66 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20  at the database 
14a67 69 73 20 63 6f 72 72 75 70 74 2e 0a 2a 2f 0a 23  is corrupt..*/.#
14a68 64 65 66 69 6e 65 20 42 54 43 55 52 53 4f 52 5f  define BTCURSOR_
14a69 4d 41 58 5f 44 45 50 54 48 20 32 30 0a 0a 2f 2a  MAX_DEPTH 20../*
14a6a 0a 2a 2a 20 41 20 63 75 72 73 6f 72 20 69 73 20  .** A cursor is 
14a6b 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70  a pointer to a p
14a6c 61 72 74 69 63 75 6c 61 72 20 65 6e 74 72 79 20  articular entry 
14a6d 77 69 74 68 69 6e 20 61 20 70 61 72 74 69 63 75  within a particu
14a6e 6c 61 72 0a 2a 2a 20 62 2d 74 72 65 65 20 77 69  lar.** b-tree wi
14a6f 74 68 69 6e 20 61 20 64 61 74 61 62 61 73 65 20  thin a database 
14a70 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  file..**.** The 
14a71 65 6e 74 72 79 20 69 73 20 69 64 65 6e 74 69 66  entry is identif
14a72 69 65 64 20 62 79 20 69 74 73 20 4d 65 6d 50 61  ied by its MemPa
14a73 67 65 20 61 6e 64 20 74 68 65 20 69 6e 64 65 78  ge and the index
14a74 20 69 6e 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61   in.** MemPage.a
14a75 43 65 6c 6c 5b 5d 20 6f 66 20 74 68 65 20 65 6e  Cell[] of the en
14a76 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  try..**.** When 
14a77 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73  a single databas
14a78 65 20 66 69 6c 65 20 63 61 6e 20 73 68 61 72 65  e file can share
14a79 64 20 62 79 20 74 77 6f 20 6d 6f 72 65 20 64 61  d by two more da
14a7a 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
14a7b 6e 73 2c 0a 2a 2a 20 62 75 74 20 63 75 72 73 6f  ns,.** but curso
14a7c 72 73 20 63 61 6e 6e 6f 74 20 62 65 20 73 68 61  rs cannot be sha
14a7d 72 65 64 2e 20 20 45 61 63 68 20 63 75 72 73 6f  red.  Each curso
14a7e 72 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 20  r is associated 
14a7f 77 69 74 68 20 61 0a 2a 2a 20 70 61 72 74 69 63  with a.** partic
14a80 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 6f  ular database co
14a81 6e 6e 65 63 74 69 6f 6e 20 69 64 65 6e 74 69 66  nnection identif
14a82 69 65 64 20 42 74 43 75 72 73 6f 72 2e 70 42 74  ied BtCursor.pBt
14a83 72 65 65 2e 64 62 2e 0a 2a 2a 0a 2a 2a 20 46 69  ree.db..**.** Fi
14a84 65 6c 64 73 20 69 6e 20 74 68 69 73 20 73 74 72  elds in this str
14a85 75 63 74 75 72 65 20 61 72 65 20 61 63 63 65 73  ucture are acces
14a86 73 65 64 20 75 6e 64 65 72 20 74 68 65 20 42 74  sed under the Bt
14a87 53 68 61 72 65 64 2e 6d 75 74 65 78 0a 2a 2a 20  Shared.mutex.** 
14a88 66 6f 75 6e 64 20 61 74 20 73 65 6c 66 2d 3e 70  found at self->p
14a89 42 74 2d 3e 6d 75 74 65 78 2e 20 0a 2a 2f 0a 73  Bt->mutex. .*/.s
14a8a 74 72 75 63 74 20 42 74 43 75 72 73 6f 72 20 7b  truct BtCursor {
14a8b 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72 65 65  .  Btree *pBtree
14a8c 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
14a8d 54 68 65 20 42 74 72 65 65 20 74 6f 20 77 68 69  The Btree to whi
14a8e 63 68 20 74 68 69 73 20 63 75 72 73 6f 72 20 62  ch this cursor b
14a8f 65 6c 6f 6e 67 73 20 2a 2f 0a 20 20 42 74 53 68  elongs */.  BtSh
14a90 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20  ared *pBt;      
14a91 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42 74 53        /* The BtS
14a92 68 61 72 65 64 20 74 68 69 73 20 63 75 72 73 6f  hared this curso
14a93 72 20 70 6f 69 6e 74 73 20 74 6f 20 2a 2f 0a 20  r points to */. 
14a94 20 42 74 43 75 72 73 6f 72 20 2a 70 4e 65 78 74   BtCursor *pNext
14a95 2c 20 2a 70 50 72 65 76 3b 20 20 2f 2a 20 46 6f  , *pPrev;  /* Fo
14a96 72 6d 73 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73  rms a linked lis
14a97 74 20 6f 66 20 61 6c 6c 20 63 75 72 73 6f 72 73  t of all cursors
14a98 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4b 65 79   */.  struct Key
14a99 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20  Info *pKeyInfo; 
14a9a 2f 2a 20 41 72 67 75 6d 65 6e 74 20 70 61 73 73  /* Argument pass
14a9b 65 64 20 74 6f 20 63 6f 6d 70 61 72 69 73 6f 6e  ed to comparison
14a9c 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 50   function */.  P
14a9d 67 6e 6f 20 70 67 6e 6f 52 6f 6f 74 3b 20 20 20  gno pgnoRoot;   
14a9e 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
14a9f 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 69  root page of thi
14aa0 73 20 74 72 65 65 20 2a 2f 0a 20 20 73 71 6c 69  s tree */.  sqli
14aa1 74 65 33 5f 69 6e 74 36 34 20 63 61 63 68 65 64  te3_int64 cached
14aa2 52 6f 77 69 64 3b 20 2f 2a 20 4e 65 78 74 20 72  Rowid; /* Next r
14aa3 6f 77 69 64 20 63 61 63 68 65 2e 20 20 30 20 6d  owid cache.  0 m
14aa4 65 61 6e 73 20 6e 6f 74 20 76 61 6c 69 64 20 2a  eans not valid *
14aa5 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66  /.  CellInfo inf
14aa6 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  o;            /*
14aa7 20 41 20 70 61 72 73 65 20 6f 66 20 74 68 65 20   A parse of the 
14aa8 63 65 6c 6c 20 77 65 20 61 72 65 20 70 6f 69 6e  cell we are poin
14aa9 74 69 6e 67 20 61 74 20 2a 2f 0a 20 20 75 38 20  ting at */.  u8 
14aaa 77 72 46 6c 61 67 3b 20 20 20 20 20 20 20 20 20  wrFlag;         
14aab 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
14aac 66 20 77 72 69 74 61 62 6c 65 20 2a 2f 0a 20 20  f writable */.  
14aad 75 38 20 61 74 4c 61 73 74 3b 20 20 20 20 20 20  u8 atLast;      
14aae 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
14aaf 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20  sor pointing to 
14ab0 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 2a  the last entry *
14ab1 2f 0a 20 20 75 38 20 76 61 6c 69 64 4e 4b 65 79  /.  u8 validNKey
14ab2 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
14ab3 20 54 72 75 65 20 69 66 20 69 6e 66 6f 2e 6e 4b   True if info.nK
14ab4 65 79 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20  ey is valid */. 
14ab5 20 75 38 20 65 53 74 61 74 65 3b 20 20 20 20 20   u8 eState;     
14ab6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e             /* On
14ab7 65 20 6f 66 20 74 68 65 20 43 55 52 53 4f 52 5f  e of the CURSOR_
14ab8 58 58 58 20 63 6f 6e 73 74 61 6e 74 73 20 28 73  XXX constants (s
14ab9 65 65 20 62 65 6c 6f 77 29 20 2a 2f 0a 20 20 76  ee below) */.  v
14aba 6f 69 64 20 2a 70 4b 65 79 3b 20 20 20 20 20 20  oid *pKey;      
14abb 2f 2a 20 53 61 76 65 64 20 6b 65 79 20 74 68 61  /* Saved key tha
14abc 74 20 77 61 73 20 63 75 72 73 6f 72 27 73 20 6c  t was cursor's l
14abd 61 73 74 20 6b 6e 6f 77 6e 20 70 6f 73 69 74 69  ast known positi
14abe 6f 6e 20 2a 2f 0a 20 20 69 36 34 20 6e 4b 65 79  on */.  i64 nKey
14abf 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65  ;        /* Size
14ac0 20 6f 66 20 70 4b 65 79 2c 20 6f 72 20 6c 61 73   of pKey, or las
14ac1 74 20 69 6e 74 65 67 65 72 20 6b 65 79 20 2a 2f  t integer key */
14ac2 0a 20 20 69 6e 74 20 73 6b 69 70 4e 65 78 74 3b  .  int skipNext;
14ac3 20 20 20 20 2f 2a 20 50 72 65 76 28 29 20 69 73      /* Prev() is
14ac4 20 6e 6f 6f 70 20 69 66 20 6e 65 67 61 74 69 76   noop if negativ
14ac5 65 2e 20 4e 65 78 74 28 29 20 69 73 20 6e 6f 6f  e. Next() is noo
14ac6 70 20 69 66 20 70 6f 73 69 74 69 76 65 20 2a 2f  p if positive */
14ac7 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
14ac8 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20  OMIT_INCRBLOB.  
14ac9 75 38 20 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e  u8 isIncrblobHan
14aca 64 6c 65 3b 20 20 20 20 20 20 2f 2a 20 54 72 75  dle;      /* Tru
14acb 65 20 69 66 20 74 68 69 73 20 63 75 72 73 6f 72  e if this cursor
14acc 20 69 73 20 61 6e 20 69 6e 63 72 2e 20 69 6f 20   is an incr. io 
14acd 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f  handle */.  Pgno
14ace 20 2a 61 4f 76 65 72 66 6c 6f 77 3b 20 20 20 20   *aOverflow;    
14acf 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 20 6f        /* Cache o
14ad0 66 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20  f overflow page 
14ad1 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 23 65 6e  locations */.#en
14ad2 64 69 66 0a 20 20 69 31 36 20 69 50 61 67 65 3b  dif.  i16 iPage;
14ad3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14ad4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
14ad5 6e 64 65 78 20 6f 66 20 63 75 72 72 65 6e 74 20  ndex of current 
14ad6 70 61 67 65 20 69 6e 20 61 70 50 61 67 65 20 2a  page in apPage *
14ad7 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61 70 50  /.  MemPage *apP
14ad8 61 67 65 5b 42 54 43 55 52 53 4f 52 5f 4d 41 58  age[BTCURSOR_MAX
14ad9 5f 44 45 50 54 48 5d 3b 20 20 2f 2a 20 50 61 67  _DEPTH];  /* Pag
14ada 65 73 20 66 72 6f 6d 20 72 6f 6f 74 20 74 6f 20  es from root to 
14adb 63 75 72 72 65 6e 74 20 70 61 67 65 20 2a 2f 0a  current page */.
14adc 20 20 75 31 36 20 61 69 49 64 78 5b 42 54 43 55    u16 aiIdx[BTCU
14add 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 5d 3b  RSOR_MAX_DEPTH];
14ade 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65          /* Curre
14adf 6e 74 20 69 6e 64 65 78 20 69 6e 20 61 70 50 61  nt index in apPa
14ae0 67 65 5b 69 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  ge[i] */.};../*.
14ae1 2a 2a 20 50 6f 74 65 6e 74 69 61 6c 20 76 61 6c  ** Potential val
14ae2 75 65 73 20 66 6f 72 20 42 74 43 75 72 73 6f 72  ues for BtCursor
14ae3 2e 65 53 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 43  .eState..**.** C
14ae4 55 52 53 4f 52 5f 56 41 4c 49 44 3a 0a 2a 2a 20  URSOR_VALID:.** 
14ae5 20 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 73 20    Cursor points 
14ae6 74 6f 20 61 20 76 61 6c 69 64 20 65 6e 74 72 79  to a valid entry
14ae7 2e 20 67 65 74 50 61 79 6c 6f 61 64 28 29 20 65  . getPayload() e
14ae8 74 63 2e 20 6d 61 79 20 62 65 20 63 61 6c 6c 65  tc. may be calle
14ae9 64 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f  d..**.** CURSOR_
14aea 49 4e 56 41 4c 49 44 3a 0a 2a 2a 20 20 20 43 75  INVALID:.**   Cu
14aeb 72 73 6f 72 20 64 6f 65 73 20 6e 6f 74 20 70 6f  rsor does not po
14aec 69 6e 74 20 74 6f 20 61 20 76 61 6c 69 64 20 65  int to a valid e
14aed 6e 74 72 79 2e 20 54 68 69 73 20 63 61 6e 20 68  ntry. This can h
14aee 61 70 70 65 6e 20 28 66 6f 72 20 65 78 61 6d 70  appen (for examp
14aef 6c 65 29 20 0a 2a 2a 20 20 20 62 65 63 61 75 73  le) .**   becaus
14af0 65 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65  e the table is e
14af1 6d 70 74 79 20 6f 72 20 62 65 63 61 75 73 65 20  mpty or because 
14af2 42 74 72 65 65 43 75 72 73 6f 72 46 69 72 73 74  BtreeCursorFirst
14af3 28 29 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 0a  () has not been.
14af4 2a 2a 20 20 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a  **   called..**.
14af5 2a 2a 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52  ** CURSOR_REQUIR
14af6 45 53 45 45 4b 3a 0a 2a 2a 20 20 20 54 68 65 20  ESEEK:.**   The 
14af7 74 61 62 6c 65 20 74 68 61 74 20 74 68 69 73 20  table that this 
14af8 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65 6e 65  cursor was opene
14af9 64 20 6f 6e 20 73 74 69 6c 6c 20 65 78 69 73 74  d on still exist
14afa 73 2c 20 62 75 74 20 68 61 73 20 62 65 65 6e 20  s, but has been 
14afb 0a 2a 2a 20 20 20 6d 6f 64 69 66 69 65 64 20 73  .**   modified s
14afc 69 6e 63 65 20 74 68 65 20 63 75 72 73 6f 72 20  ince the cursor 
14afd 77 61 73 20 6c 61 73 74 20 75 73 65 64 2e 20 54  was last used. T
14afe 68 65 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69  he cursor positi
14aff 6f 6e 20 69 73 20 73 61 76 65 64 0a 2a 2a 20 20  on is saved.**  
14b00 20 69 6e 20 76 61 72 69 61 62 6c 65 73 20 42 74   in variables Bt
14b01 43 75 72 73 6f 72 2e 70 4b 65 79 20 61 6e 64 20  Cursor.pKey and 
14b02 42 74 43 75 72 73 6f 72 2e 6e 4b 65 79 2e 20 57  BtCursor.nKey. W
14b03 68 65 6e 20 61 20 63 75 72 73 6f 72 20 69 73 20  hen a cursor is 
14b04 69 6e 20 0a 2a 2a 20 20 20 74 68 69 73 20 73 74  in .**   this st
14b05 61 74 65 2c 20 72 65 73 74 6f 72 65 43 75 72 73  ate, restoreCurs
14b06 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 63 61 6e  orPosition() can
14b07 20 62 65 20 63 61 6c 6c 65 64 20 74 6f 20 61 74   be called to at
14b08 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 20 20 73 65  tempt to.**   se
14b09 65 6b 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f  ek the cursor to
14b0a 20 74 68 65 20 73 61 76 65 64 20 70 6f 73 69 74   the saved posit
14b0b 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f  ion..**.** CURSO
14b0c 52 5f 46 41 55 4c 54 3a 0a 2a 2a 20 20 20 41 20  R_FAULT:.**   A 
14b0d 75 6e 72 65 63 6f 76 65 72 61 62 6c 65 20 65 72  unrecoverable er
14b0e 72 6f 72 20 28 61 6e 20 49 2f 4f 20 65 72 72 6f  ror (an I/O erro
14b0f 72 20 6f 72 20 61 20 6d 61 6c 6c 6f 63 20 66 61  r or a malloc fa
14b10 69 6c 75 72 65 29 20 68 61 73 20 6f 63 63 75 72  ilure) has occur
14b11 72 65 64 0a 2a 2a 20 20 20 6f 6e 20 61 20 64 69  red.**   on a di
14b12 66 66 65 72 65 6e 74 20 63 6f 6e 6e 65 63 74 69  fferent connecti
14b13 6f 6e 20 74 68 61 74 20 73 68 61 72 65 73 20 74  on that shares t
14b14 68 65 20 42 74 53 68 61 72 65 64 20 63 61 63 68  he BtShared cach
14b15 65 20 77 69 74 68 20 74 68 69 73 0a 2a 2a 20 20  e with this.**  
14b16 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 65 72   cursor.  The er
14b17 72 6f 72 20 68 61 73 20 6c 65 66 74 20 74 68 65  ror has left the
14b18 20 63 61 63 68 65 20 69 6e 20 61 6e 20 69 6e 63   cache in an inc
14b19 6f 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65 2e  onsistent state.
14b1a 0a 2a 2a 20 20 20 44 6f 20 6e 6f 74 68 69 6e 67  .**   Do nothing
14b1b 20 65 6c 73 65 20 77 69 74 68 20 74 68 69 73 20   else with this 
14b1c 63 75 72 73 6f 72 2e 20 20 41 6e 79 20 61 74 74  cursor.  Any att
14b1d 65 6d 70 74 20 74 6f 20 75 73 65 20 74 68 65 20  empt to use the 
14b1e 63 75 72 73 6f 72 0a 2a 2a 20 20 20 73 68 6f 75  cursor.**   shou
14b1f 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 72  ld return the er
14b20 72 6f 72 20 63 6f 64 65 20 73 74 6f 72 65 64 20  ror code stored 
14b21 69 6e 20 42 74 43 75 72 73 6f 72 2e 73 6b 69 70  in BtCursor.skip
14b22 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 55 52 53  .*/.#define CURS
14b23 4f 52 5f 49 4e 56 41 4c 49 44 20 20 20 20 20 20  OR_INVALID      
14b24 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 43       0.#define C
14b25 55 52 53 4f 52 5f 56 41 4c 49 44 20 20 20 20 20  URSOR_VALID     
14b26 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e          1.#defin
14b27 65 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45  e CURSOR_REQUIRE
14b28 53 45 45 4b 20 20 20 20 20 20 20 32 0a 23 64 65  SEEK       2.#de
14b29 66 69 6e 65 20 43 55 52 53 4f 52 5f 46 41 55 4c  fine CURSOR_FAUL
14b2a 54 20 20 20 20 20 20 20 20 20 20 20 20 20 33 0a  T             3.
14b2b 0a 2f 2a 20 0a 2a 2a 20 54 68 65 20 64 61 74 61  ./* .** The data
14b2c 62 61 73 65 20 70 61 67 65 20 74 68 65 20 50 45  base page the PE
14b2d 4e 44 49 4e 47 5f 42 59 54 45 20 6f 63 63 75 70  NDING_BYTE occup
14b2e 69 65 73 2e 20 54 68 69 73 20 70 61 67 65 20 69  ies. This page i
14b2f 73 20 6e 65 76 65 72 20 75 73 65 64 2e 0a 2a 2f  s never used..*/
14b30 0a 23 20 64 65 66 69 6e 65 20 50 45 4e 44 49 4e  .# define PENDIN
14b31 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29  G_BYTE_PAGE(pBt)
14b32 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70   PAGER_MJ_PGNO(p
14b33 42 74 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65  Bt)../*.** These
14b34 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 20 74   macros define t
14b35 68 65 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 74  he location of t
14b36 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65  he pointer-map e
14b37 6e 74 72 79 20 66 6f 72 20 61 20 0a 2a 2a 20 64  ntry for a .** d
14b38 61 74 61 62 61 73 65 20 70 61 67 65 2e 20 54 68  atabase page. Th
14b39 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
14b3a 20 74 6f 20 65 61 63 68 20 69 73 20 74 68 65 20   to each is the 
14b3b 6e 75 6d 62 65 72 20 6f 66 20 75 73 61 62 6c 65  number of usable
14b3c 0a 2a 2a 20 62 79 74 65 73 20 6f 6e 20 65 61 63  .** bytes on eac
14b3d 68 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61  h page of the da
14b3e 74 61 62 61 73 65 20 28 6f 66 74 65 6e 20 31 30  tabase (often 10
14b3f 32 34 29 2e 20 54 68 65 20 73 65 63 6f 6e 64 20  24). The second 
14b40 69 73 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 6e  is the.** page n
14b41 75 6d 62 65 72 20 74 6f 20 6c 6f 6f 6b 20 75 70  umber to look up
14b42 20 69 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 20   in the pointer 
14b43 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41  map..**.** PTRMA
14b44 50 5f 50 41 47 45 4e 4f 20 72 65 74 75 72 6e 73  P_PAGENO returns
14b45 20 74 68 65 20 64 61 74 61 62 61 73 65 20 70 61   the database pa
14b46 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  ge number of the
14b47 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 2a 2a 20   pointer-map.** 
14b48 70 61 67 65 20 74 68 61 74 20 73 74 6f 72 65 73  page that stores
14b49 20 74 68 65 20 72 65 71 75 69 72 65 64 20 70 6f   the required po
14b4a 69 6e 74 65 72 2e 20 50 54 52 4d 41 50 5f 50 54  inter. PTRMAP_PT
14b4b 52 4f 46 46 53 45 54 20 72 65 74 75 72 6e 73 0a  ROFFSET returns.
14b4c 2a 2a 20 74 68 65 20 6f 66 66 73 65 74 20 6f 66  ** the offset of
14b4d 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6d   the requested m
14b4e 61 70 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20  ap entry..**.** 
14b4f 49 66 20 74 68 65 20 70 67 6e 6f 20 61 72 67 75  If the pgno argu
14b50 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f 20 50  ment passed to P
14b51 54 52 4d 41 50 5f 50 41 47 45 4e 4f 20 69 73 20  TRMAP_PAGENO is 
14b52 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61  a pointer-map pa
14b53 67 65 2c 0a 2a 2a 20 74 68 65 6e 20 70 67 6e 6f  ge,.** then pgno
14b54 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 53 6f   is returned. So
14b55 20 28 70 67 6e 6f 3d 3d 50 54 52 4d 41 50 5f 50   (pgno==PTRMAP_P
14b56 41 47 45 4e 4f 28 70 67 73 7a 2c 20 70 67 6e 6f  AGENO(pgsz, pgno
14b57 29 29 20 63 61 6e 20 62 65 0a 2a 2a 20 75 73 65  )) can be.** use
14b58 64 20 74 6f 20 74 65 73 74 20 69 66 20 70 67 6e  d to test if pgn
14b59 6f 20 69 73 20 61 20 70 6f 69 6e 74 65 72 2d 6d  o is a pointer-m
14b5a 61 70 20 70 61 67 65 2e 20 50 54 52 4d 41 50 5f  ap page. PTRMAP_
14b5b 49 53 50 41 47 45 20 69 6d 70 6c 65 6d 65 6e 74  ISPAGE implement
14b5c 73 0a 2a 2a 20 74 68 69 73 20 74 65 73 74 2e 0a  s.** this test..
14b5d 2a 2f 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41  */.#define PTRMA
14b5e 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 70 67  P_PAGENO(pBt, pg
14b5f 6e 6f 29 20 70 74 72 6d 61 70 50 61 67 65 6e 6f  no) ptrmapPageno
14b60 28 70 42 74 2c 20 70 67 6e 6f 29 0a 23 64 65 66  (pBt, pgno).#def
14b61 69 6e 65 20 50 54 52 4d 41 50 5f 50 54 52 4f 46  ine PTRMAP_PTROF
14b62 46 53 45 54 28 70 67 70 74 72 6d 61 70 2c 20 70  FSET(pgptrmap, p
14b63 67 6e 6f 29 20 28 35 2a 28 70 67 6e 6f 2d 70 67  gno) (5*(pgno-pg
14b64 70 74 72 6d 61 70 2d 31 29 29 0a 23 64 65 66 69  ptrmap-1)).#defi
14b65 6e 65 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45  ne PTRMAP_ISPAGE
14b66 28 70 42 74 2c 20 70 67 6e 6f 29 20 28 50 54 52  (pBt, pgno) (PTR
14b67 4d 41 50 5f 50 41 47 45 4e 4f 28 28 70 42 74 29  MAP_PAGENO((pBt)
14b68 2c 28 70 67 6e 6f 29 29 3d 3d 28 70 67 6e 6f 29  ,(pgno))==(pgno)
14b69 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 6f 69  )../*.** The poi
14b6a 6e 74 65 72 20 6d 61 70 20 69 73 20 61 20 6c 6f  nter map is a lo
14b6b 6f 6b 75 70 20 74 61 62 6c 65 20 74 68 61 74 20  okup table that 
14b6c 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 70  identifies the p
14b6d 61 72 65 6e 74 20 70 61 67 65 20 66 6f 72 0a 2a  arent page for.*
14b6e 2a 20 65 61 63 68 20 63 68 69 6c 64 20 70 61 67  * each child pag
14b6f 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  e in the databas
14b70 65 20 66 69 6c 65 2e 20 20 54 68 65 20 70 61 72  e file.  The par
14b71 65 6e 74 20 70 61 67 65 20 69 73 20 74 68 65 20  ent page is the 
14b72 70 61 67 65 20 74 68 61 74 0a 2a 2a 20 63 6f 6e  page that.** con
14b73 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  tains a pointer 
14b74 74 6f 20 74 68 65 20 63 68 69 6c 64 2e 20 20 45  to the child.  E
14b75 76 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65  very page in the
14b76 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69   database contai
14b77 6e 73 0a 2a 2a 20 30 20 6f 72 20 31 20 70 61 72  ns.** 0 or 1 par
14b78 65 6e 74 20 70 61 67 65 73 2e 20 20 28 49 6e 20  ent pages.  (In 
14b79 74 68 69 73 20 63 6f 6e 74 65 78 74 20 27 64 61  this context 'da
14b7a 74 61 62 61 73 65 20 70 61 67 65 27 20 72 65 66  tabase page' ref
14b7b 65 72 73 0a 2a 2a 20 74 6f 20 61 6e 79 20 70 61  ers.** to any pa
14b7c 67 65 20 74 68 61 74 20 69 73 20 6e 6f 74 20 70  ge that is not p
14b7d 61 72 74 20 6f 66 20 74 68 65 20 70 6f 69 6e 74  art of the point
14b7e 65 72 20 6d 61 70 20 69 74 73 65 6c 66 2e 29 20  er map itself.) 
14b7f 20 45 61 63 68 20 70 6f 69 6e 74 65 72 20 6d 61   Each pointer ma
14b80 70 0a 2a 2a 20 65 6e 74 72 79 20 63 6f 6e 73 69  p.** entry consi
14b81 73 74 73 20 6f 66 20 61 20 73 69 6e 67 6c 65 20  sts of a single 
14b82 62 79 74 65 20 27 74 79 70 65 27 20 61 6e 64 20  byte 'type' and 
14b83 61 20 34 20 62 79 74 65 20 70 61 72 65 6e 74 20  a 4 byte parent 
14b84 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20  page number..** 
14b85 54 68 65 20 50 54 52 4d 41 50 5f 58 58 58 20 69  The PTRMAP_XXX i
14b86 64 65 6e 74 69 66 69 65 72 73 20 62 65 6c 6f 77  dentifiers below
14b87 20 61 72 65 20 74 68 65 20 76 61 6c 69 64 20 74   are the valid t
14b88 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ypes..**.** The 
14b89 70 75 72 70 6f 73 65 20 6f 66 20 74 68 65 20 70  purpose of the p
14b8a 6f 69 6e 74 65 72 20 6d 61 70 20 69 73 20 74 6f  ointer map is to
14b8b 20 66 61 63 69 6c 69 74 79 20 6d 6f 76 69 6e 67   facility moving
14b8c 20 70 61 67 65 73 20 66 72 6f 6d 20 6f 6e 65 0a   pages from one.
14b8d 2a 2a 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74  ** position in t
14b8e 68 65 20 66 69 6c 65 20 74 6f 20 61 6e 6f 74 68  he file to anoth
14b8f 65 72 20 61 73 20 70 61 72 74 20 6f 66 20 61 75  er as part of au
14b90 74 6f 76 61 63 75 75 6d 2e 20 20 57 68 65 6e 20  tovacuum.  When 
14b91 61 20 70 61 67 65 0a 2a 2a 20 69 73 20 6d 6f 76  a page.** is mov
14b92 65 64 2c 20 74 68 65 20 70 6f 69 6e 74 65 72 20  ed, the pointer 
14b93 69 6e 20 69 74 73 20 70 61 72 65 6e 74 20 6d 75  in its parent mu
14b94 73 74 20 62 65 20 75 70 64 61 74 65 64 20 74 6f  st be updated to
14b95 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a 2a   point to the.**
14b96 20 6e 65 77 20 6c 6f 63 61 74 69 6f 6e 2e 20 20   new location.  
14b97 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20  The pointer map 
14b98 69 73 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74  is used to locat
14b99 65 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67  e the parent pag
14b9a 65 20 71 75 69 63 6b 6c 79 2e 0a 2a 2a 0a 2a 2a  e quickly..**.**
14b9b 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45   PTRMAP_ROOTPAGE
14b9c 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70  : The database p
14b9d 61 67 65 20 69 73 20 61 20 72 6f 6f 74 2d 70 61  age is a root-pa
14b9e 67 65 2e 20 54 68 65 20 70 61 67 65 2d 6e 75 6d  ge. The page-num
14b9f 62 65 72 20 69 73 20 6e 6f 74 0a 2a 2a 20 20 20  ber is not.**   
14ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75                 u
14ba1 73 65 64 20 69 6e 20 74 68 69 73 20 63 61 73 65  sed in this case
14ba2 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 46  ..**.** PTRMAP_F
14ba3 52 45 45 50 41 47 45 3a 20 54 68 65 20 64 61 74  REEPAGE: The dat
14ba4 61 62 61 73 65 20 70 61 67 65 20 69 73 20 61 6e  abase page is an
14ba5 20 75 6e 75 73 65 64 20 28 66 72 65 65 29 20 70   unused (free) p
14ba6 61 67 65 2e 20 54 68 65 20 70 61 67 65 2d 6e 75  age. The page-nu
14ba7 6d 62 65 72 20 0a 2a 2a 20 20 20 20 20 20 20 20  mber .**        
14ba8 20 20 20 20 20 20 20 20 20 20 69 73 20 6e 6f 74            is not
14ba9 20 75 73 65 64 20 69 6e 20 74 68 69 73 20 63 61   used in this ca
14baa 73 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50  se..**.** PTRMAP
14bab 5f 4f 56 45 52 46 4c 4f 57 31 3a 20 54 68 65 20  _OVERFLOW1: The 
14bac 64 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73  database page is
14bad 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20   the first page 
14bae 69 6e 20 61 20 6c 69 73 74 20 6f 66 20 0a 2a 2a  in a list of .**
14baf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14bb0 20 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65     overflow page
14bb1 73 2e 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62  s. The page numb
14bb2 65 72 20 69 64 65 6e 74 69 66 69 65 73 20 74 68  er identifies th
14bb3 65 20 70 61 67 65 20 74 68 61 74 0a 2a 2a 20 20  e page that.**  
14bb4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14bb5 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 65   contains the ce
14bb6 6c 6c 20 77 69 74 68 20 61 20 70 6f 69 6e 74 65  ll with a pointe
14bb7 72 20 74 6f 20 74 68 69 73 20 6f 76 65 72 66 6c  r to this overfl
14bb8 6f 77 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50  ow page..**.** P
14bb9 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 3a  TRMAP_OVERFLOW2:
14bba 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61   The database pa
14bbb 67 65 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64  ge is the second
14bbc 20 6f 72 20 6c 61 74 65 72 20 70 61 67 65 20 69   or later page i
14bbd 6e 20 61 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 20  n a list of.**  
14bbe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14bbf 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e   overflow pages.
14bc0 20 54 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72   The page-number
14bc1 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20   identifies the 
14bc2 70 72 65 76 69 6f 75 73 0a 2a 2a 20 20 20 20 20  previous.**     
14bc3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61                pa
14bc4 67 65 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c  ge in the overfl
14bc5 6f 77 20 70 61 67 65 20 6c 69 73 74 2e 0a 2a 2a  ow page list..**
14bc6 0a 2a 2a 20 50 54 52 4d 41 50 5f 42 54 52 45 45  .** PTRMAP_BTREE
14bc7 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70  : The database p
14bc8 61 67 65 20 69 73 20 61 20 6e 6f 6e 2d 72 6f 6f  age is a non-roo
14bc9 74 20 62 74 72 65 65 20 70 61 67 65 2e 20 54 68  t btree page. Th
14bca 65 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 2a 2a  e page number.**
14bcb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
14bcc 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 70 61  dentifies the pa
14bcd 72 65 6e 74 20 70 61 67 65 20 69 6e 20 74 68 65  rent page in the
14bce 20 62 74 72 65 65 2e 0a 2a 2f 0a 23 64 65 66 69   btree..*/.#defi
14bcf 6e 65 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41  ne PTRMAP_ROOTPA
14bd0 47 45 20 31 0a 23 64 65 66 69 6e 65 20 50 54 52  GE 1.#define PTR
14bd1 4d 41 50 5f 46 52 45 45 50 41 47 45 20 32 0a 23  MAP_FREEPAGE 2.#
14bd2 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 4f 56  define PTRMAP_OV
14bd3 45 52 46 4c 4f 57 31 20 33 0a 23 64 65 66 69 6e  ERFLOW1 3.#defin
14bd4 65 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f  e PTRMAP_OVERFLO
14bd5 57 32 20 34 0a 23 64 65 66 69 6e 65 20 50 54 52  W2 4.#define PTR
14bd6 4d 41 50 5f 42 54 52 45 45 20 35 0a 0a 2f 2a 20  MAP_BTREE 5../* 
14bd7 41 20 62 75 6e 63 68 20 6f 66 20 61 73 73 65 72  A bunch of asser
14bd8 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 74  t() statements t
14bd9 6f 20 63 68 65 63 6b 20 74 68 65 20 74 72 61 6e  o check the tran
14bda 73 61 63 74 69 6f 6e 20 73 74 61 74 65 20 76 61  saction state va
14bdb 72 69 61 62 6c 65 73 0a 2a 2a 20 6f 66 20 68 61  riables.** of ha
14bdc 6e 64 6c 65 20 70 20 28 74 79 70 65 20 42 74 72  ndle p (type Btr
14bdd 65 65 2a 29 20 61 72 65 20 69 6e 74 65 72 6e 61  ee*) are interna
14bde 6c 6c 79 20 63 6f 6e 73 69 73 74 65 6e 74 2e 0a  lly consistent..
14bdf 2a 2f 0a 23 64 65 66 69 6e 65 20 62 74 72 65 65  */.#define btree
14be0 49 6e 74 65 67 72 69 74 79 28 70 29 20 5c 0a 20  Integrity(p) \. 
14be1 20 61 73 73 65 72 74 28 20 70 2d 3e 70 42 74 2d   assert( p->pBt-
14be2 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 21 3d  >inTransaction!=
14be3 54 52 41 4e 53 5f 4e 4f 4e 45 20 7c 7c 20 70 2d  TRANS_NONE || p-
14be4 3e 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69  >pBt->nTransacti
14be5 6f 6e 3d 3d 30 20 29 3b 20 5c 0a 20 20 61 73 73  on==0 ); \.  ass
14be6 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 69 6e 54  ert( p->pBt->inT
14be7 72 61 6e 73 61 63 74 69 6f 6e 3e 3d 70 2d 3e 69  ransaction>=p->i
14be8 6e 54 72 61 6e 73 20 29 3b 20 0a 0a 0a 2f 2a 0a  nTrans ); .../*.
14be9 2a 2a 20 54 68 65 20 49 53 41 55 54 4f 56 41 43  ** The ISAUTOVAC
14bea 55 55 4d 20 6d 61 63 72 6f 20 69 73 20 75 73 65  UUM macro is use
14beb 64 20 77 69 74 68 69 6e 20 62 61 6c 61 6e 63 65  d within balance
14bec 5f 6e 6f 6e 72 6f 6f 74 28 29 20 74 6f 20 64 65  _nonroot() to de
14bed 74 65 72 6d 69 6e 65 0a 2a 2a 20 69 66 20 74 68  termine.** if th
14bee 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f  e database suppo
14bef 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 20  rts auto-vacuum 
14bf0 6f 72 20 6e 6f 74 2e 20 42 65 63 61 75 73 65 20  or not. Because 
14bf1 69 74 20 69 73 20 75 73 65 64 0a 2a 2a 20 77 69  it is used.** wi
14bf2 74 68 69 6e 20 61 6e 20 65 78 70 72 65 73 73 69  thin an expressi
14bf3 6f 6e 20 74 68 61 74 20 69 73 20 61 6e 20 61 72  on that is an ar
14bf4 67 75 6d 65 6e 74 20 74 6f 20 61 6e 6f 74 68 65  gument to anothe
14bf5 72 20 6d 61 63 72 6f 20 0a 2a 2a 20 28 73 71 6c  r macro .** (sql
14bf6 69 74 65 4d 61 6c 6c 6f 63 52 61 77 29 2c 20 69  iteMallocRaw), i
14bf7 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c  t is not possibl
14bf8 65 20 74 6f 20 75 73 65 20 63 6f 6e 64 69 74 69  e to use conditi
14bf9 6f 6e 61 6c 20 63 6f 6d 70 69 6c 61 74 69 6f 6e  onal compilation
14bfa 2e 0a 2a 2a 20 53 6f 2c 20 74 68 69 73 20 6d 61  ..** So, this ma
14bfb 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64 20 69  cro is defined i
14bfc 6e 73 74 65 61 64 2e 0a 2a 2f 0a 23 69 66 6e 64  nstead..*/.#ifnd
14bfd 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
14bfe 55 54 4f 56 41 43 55 55 4d 0a 23 64 65 66 69 6e  UTOVACUUM.#defin
14bff 65 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 28  e ISAUTOVACUUM (
14c00 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29  pBt->autoVacuum)
14c01 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 49  .#else.#define I
14c02 53 41 55 54 4f 56 41 43 55 55 4d 20 30 0a 23 65  SAUTOVACUUM 0.#e
14c03 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  ndif.../*.** Thi
14c04 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 70  s structure is p
14c05 61 73 73 65 64 20 61 72 6f 75 6e 64 20 74 68 72  assed around thr
14c06 6f 75 67 68 20 61 6c 6c 20 74 68 65 20 73 61 6e  ough all the san
14c07 69 74 79 20 63 68 65 63 6b 69 6e 67 20 72 6f 75  ity checking rou
14c08 74 69 6e 65 73 0a 2a 2a 20 69 6e 20 6f 72 64 65  tines.** in orde
14c09 72 20 74 6f 20 6b 65 65 70 20 74 72 61 63 6b 20  r to keep track 
14c0a 6f 66 20 73 6f 6d 65 20 67 6c 6f 62 61 6c 20 73  of some global s
14c0b 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  tate information
14c0c 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
14c0d 75 63 74 20 49 6e 74 65 67 72 69 74 79 43 6b 20  uct IntegrityCk 
14c0e 49 6e 74 65 67 72 69 74 79 43 6b 3b 0a 73 74 72  IntegrityCk;.str
14c0f 75 63 74 20 49 6e 74 65 67 72 69 74 79 43 6b 20  uct IntegrityCk 
14c10 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  {.  BtShared *pB
14c11 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 74 72 65  t;    /* The tre
14c12 65 20 62 65 69 6e 67 20 63 68 65 63 6b 65 64 20  e being checked 
14c13 6f 75 74 20 2a 2f 0a 20 20 50 61 67 65 72 20 2a  out */.  Pager *
14c14 70 50 61 67 65 72 3b 20 20 20 20 2f 2a 20 54 68  pPager;    /* Th
14c15 65 20 61 73 73 6f 63 69 61 74 65 64 20 70 61 67  e associated pag
14c16 65 72 2e 20 20 41 6c 73 6f 20 61 63 63 65 73 73  er.  Also access
14c17 69 62 6c 65 20 62 79 20 70 42 74 2d 3e 70 50 61  ible by pBt->pPa
14c18 67 65 72 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 50  ger */.  Pgno nP
14c19 61 67 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75  age;       /* Nu
14c1a 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e  mber of pages in
14c1b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f   the database */
14c1c 0a 20 20 69 6e 74 20 2a 61 6e 52 65 66 3b 20 20  .  int *anRef;  
14c1d 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
14c1e 66 20 74 69 6d 65 73 20 65 61 63 68 20 70 61 67  f times each pag
14c1f 65 20 69 73 20 72 65 66 65 72 65 6e 63 65 64 20  e is referenced 
14c20 2a 2f 0a 20 20 69 6e 74 20 6d 78 45 72 72 3b 20  */.  int mxErr; 
14c21 20 20 20 20 20 20 20 2f 2a 20 53 74 6f 70 20 61         /* Stop a
14c22 63 63 75 6d 75 6c 61 74 69 6e 67 20 65 72 72 6f  ccumulating erro
14c23 72 73 20 77 68 65 6e 20 74 68 69 73 20 72 65 61  rs when this rea
14c24 63 68 65 73 20 7a 65 72 6f 20 2a 2f 0a 20 20 69  ches zero */.  i
14c25 6e 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 20  nt nErr;        
14c26 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65   /* Number of me
14c27 73 73 61 67 65 73 20 77 72 69 74 74 65 6e 20 74  ssages written t
14c28 6f 20 7a 45 72 72 4d 73 67 20 73 6f 20 66 61 72  o zErrMsg so far
14c29 20 2a 2f 0a 20 20 69 6e 74 20 6d 61 6c 6c 6f 63   */.  int malloc
14c2a 46 61 69 6c 65 64 3b 20 2f 2a 20 41 20 6d 65 6d  Failed; /* A mem
14c2b 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65  ory allocation e
14c2c 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65  rror has occurre
14c2d 64 20 2a 2f 0a 20 20 53 74 72 41 63 63 75 6d 20  d */.  StrAccum 
14c2e 65 72 72 4d 73 67 3b 20 20 2f 2a 20 41 63 63 75  errMsg;  /* Accu
14c2f 6d 75 6c 61 74 65 20 74 68 65 20 65 72 72 6f 72  mulate the error
14c30 20 6d 65 73 73 61 67 65 20 74 65 78 74 20 68 65   message text he
14c31 72 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  re */.};../*.** 
14c32 52 65 61 64 20 6f 72 20 77 72 69 74 65 20 61 20  Read or write a 
14c33 74 77 6f 2d 20 61 6e 64 20 66 6f 75 72 2d 62 79  two- and four-by
14c34 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e  te big-endian in
14c35 74 65 67 65 72 20 76 61 6c 75 65 73 2e 0a 2a 2f  teger values..*/
14c36 0a 23 64 65 66 69 6e 65 20 67 65 74 32 62 79 74  .#define get2byt
14c37 65 28 78 29 20 20 20 28 28 78 29 5b 30 5d 3c 3c  e(x)   ((x)[0]<<
14c38 38 20 7c 20 28 78 29 5b 31 5d 29 0a 23 64 65 66  8 | (x)[1]).#def
14c39 69 6e 65 20 70 75 74 32 62 79 74 65 28 70 2c 76  ine put2byte(p,v
14c3a 29 20 28 28 70 29 5b 30 5d 20 3d 20 28 75 38 29  ) ((p)[0] = (u8)
14c3b 28 28 76 29 3e 3e 38 29 2c 20 28 70 29 5b 31 5d  ((v)>>8), (p)[1]
14c3c 20 3d 20 28 75 38 29 28 76 29 29 0a 23 64 65 66   = (u8)(v)).#def
14c3d 69 6e 65 20 67 65 74 34 62 79 74 65 20 73 71 6c  ine get4byte sql
14c3e 69 74 65 33 47 65 74 34 62 79 74 65 0a 23 64 65  ite3Get4byte.#de
14c3f 66 69 6e 65 20 70 75 74 34 62 79 74 65 20 73 71  fine put4byte sq
14c40 6c 69 74 65 33 50 75 74 34 62 79 74 65 0a 0a 2f  lite3Put4byte../
14c41 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
14c42 6e 64 20 6f 66 20 62 74 72 65 65 49 6e 74 2e 68  nd of btreeInt.h
14c43 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
14c44 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14c45 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
14c46 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43  ************** C
14c47 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20  ontinuing where 
14c48 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 62  we left off in b
14c49 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a  tmutex.c *******
14c4a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23  *************/.#
14c4b 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
14c4c 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a  IT_SHARED_CACHE.
14c4d 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
14c4e 44 53 41 46 45 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74  DSAFE../*.** Obt
14c4f 61 69 6e 20 74 68 65 20 42 74 53 68 61 72 65 64  ain the BtShared
14c50 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61 74 65   mutex associate
14c51 64 20 77 69 74 68 20 42 2d 54 72 65 65 20 68 61  d with B-Tree ha
14c52 6e 64 6c 65 20 70 2e 20 41 6c 73 6f 2c 0a 2a 2a  ndle p. Also,.**
14c53 20 73 65 74 20 42 74 53 68 61 72 65 64 2e 64 62   set BtShared.db
14c54 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
14c55 20 68 61 6e 64 6c 65 20 61 73 73 6f 63 69 61 74   handle associat
14c56 65 64 20 77 69 74 68 20 70 20 61 6e 64 20 74 68  ed with p and th
14c57 65 0a 2a 2a 20 70 2d 3e 6c 6f 63 6b 65 64 20 62  e.** p->locked b
14c58 6f 6f 6c 65 61 6e 20 74 6f 20 74 72 75 65 2e 0a  oolean to true..
14c59 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c  */.static void l
14c5a 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 42 74  ockBtreeMutex(Bt
14c5b 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72  ree *p){.  asser
14c5c 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20  t( p->locked==0 
14c5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
14c5e 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65  ite3_mutex_nothe
14c5f 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  ld(p->pBt->mutex
14c60 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  ) );.  assert( s
14c61 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
14c62 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  d(p->db->mutex) 
14c63 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  );..  sqlite3_mu
14c64 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 42 74  tex_enter(p->pBt
14c65 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 2d 3e 70  ->mutex);.  p->p
14c66 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a  Bt->db = p->db;.
14c67 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31 3b    p->locked = 1;
14c68 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73  .}../*.** Releas
14c69 65 20 74 68 65 20 42 74 53 68 61 72 65 64 20 6d  e the BtShared m
14c6a 75 74 65 78 20 61 73 73 6f 63 69 61 74 65 64 20  utex associated 
14c6b 77 69 74 68 20 42 2d 54 72 65 65 20 68 61 6e 64  with B-Tree hand
14c6c 6c 65 20 70 20 61 6e 64 0a 2a 2a 20 63 6c 65 61  le p and.** clea
14c6d 72 20 74 68 65 20 70 2d 3e 6c 6f 63 6b 65 64 20  r the p->locked 
14c6e 62 6f 6f 6c 65 61 6e 2e 0a 2a 2f 0a 73 74 61 74  boolean..*/.stat
14c6f 69 63 20 76 6f 69 64 20 75 6e 6c 6f 63 6b 42 74  ic void unlockBt
14c70 72 65 65 4d 75 74 65 78 28 42 74 72 65 65 20 2a  reeMutex(Btree *
14c71 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  p){.  assert( p-
14c72 3e 6c 6f 63 6b 65 64 3d 3d 31 20 29 3b 0a 20 20  >locked==1 );.  
14c73 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
14c74 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 42  mutex_held(p->pB
14c75 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  t->mutex) );.  a
14c76 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
14c77 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d  utex_held(p->db-
14c78 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73  >mutex) );.  ass
14c79 65 72 74 28 20 70 2d 3e 64 62 3d 3d 70 2d 3e 70  ert( p->db==p->p
14c7a 42 74 2d 3e 64 62 20 29 3b 0a 0a 20 20 73 71 6c  Bt->db );..  sql
14c7b 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
14c7c 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b  (p->pBt->mutex);
14c7d 0a 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 30  .  p->locked = 0
14c7e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72  ;.}../*.** Enter
14c7f 20 61 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 20   a mutex on the 
14c80 67 69 76 65 6e 20 42 54 72 65 65 20 6f 62 6a 65  given BTree obje
14c81 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ct..**.** If the
14c82 20 6f 62 6a 65 63 74 20 69 73 20 6e 6f 74 20 73   object is not s
14c83 68 61 72 61 62 6c 65 2c 20 74 68 65 6e 20 6e 6f  harable, then no
14c84 20 6d 75 74 65 78 20 69 73 20 65 76 65 72 20 72   mutex is ever r
14c85 65 71 75 69 72 65 64 0a 2a 2a 20 61 6e 64 20 74  equired.** and t
14c86 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
14c87 20 6e 6f 2d 6f 70 2e 20 20 54 68 65 20 75 6e 64   no-op.  The und
14c88 65 72 6c 79 69 6e 67 20 6d 75 74 65 78 20 69 73  erlying mutex is
14c89 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 2e 0a   non-recursive..
14c8a 2a 2a 20 42 75 74 20 77 65 20 6b 65 65 70 20 61  ** But we keep a
14c8b 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74   reference count
14c8c 20 69 6e 20 42 74 72 65 65 2e 77 61 6e 74 54 6f   in Btree.wantTo
14c8d 4c 6f 63 6b 20 73 6f 20 74 68 65 20 62 65 68 61  Lock so the beha
14c8e 76 69 6f 72 0a 2a 2a 20 6f 66 20 74 68 69 73 20  vior.** of this 
14c8f 69 6e 74 65 72 66 61 63 65 20 69 73 20 72 65 63  interface is rec
14c90 75 72 73 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 6f  ursive..**.** To
14c91 20 61 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b 73   avoid deadlocks
14c92 2c 20 6d 75 6c 74 69 70 6c 65 20 42 74 72 65 65  , multiple Btree
14c93 73 20 61 72 65 20 6c 6f 63 6b 65 64 20 69 6e 20  s are locked in 
14c94 74 68 65 20 73 61 6d 65 20 6f 72 64 65 72 0a 2a  the same order.*
14c95 2a 20 62 79 20 61 6c 6c 20 64 61 74 61 62 61 73  * by all databas
14c96 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20  e connections.  
14c97 54 68 65 20 70 2d 3e 70 4e 65 78 74 20 69 73 20  The p->pNext is 
14c98 61 20 6c 69 73 74 20 6f 66 20 6f 74 68 65 72 0a  a list of other.
14c99 2a 2a 20 42 74 72 65 65 73 20 62 65 6c 6f 6e 67  ** Btrees belong
14c9a 69 6e 67 20 74 6f 20 74 68 65 20 73 61 6d 65 20  ing to the same 
14c9b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
14c9c 69 6f 6e 20 61 73 20 74 68 65 20 70 20 42 74 72  ion as the p Btr
14c9d 65 65 0a 2a 2a 20 77 68 69 63 68 20 6e 65 65 64  ee.** which need
14c9e 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 61 66   to be locked af
14c9f 74 65 72 20 70 2e 20 20 49 66 20 77 65 20 63 61  ter p.  If we ca
14ca0 6e 6e 6f 74 20 67 65 74 20 61 20 6c 6f 63 6b 20  nnot get a lock 
14ca1 6f 6e 0a 2a 2a 20 70 2c 20 74 68 65 6e 20 66 69  on.** p, then fi
14ca2 72 73 74 20 75 6e 6c 6f 63 6b 20 61 6c 6c 20 6f  rst unlock all o
14ca3 66 20 74 68 65 20 6f 74 68 65 72 73 20 6f 6e 20  f the others on 
14ca4 70 2d 3e 70 4e 65 78 74 2c 20 74 68 65 6e 20 77  p->pNext, then w
14ca5 61 69 74 0a 2a 2a 20 66 6f 72 20 74 68 65 20 6c  ait.** for the l
14ca6 6f 63 6b 20 74 6f 20 62 65 63 6f 6d 65 20 61 76  ock to become av
14ca7 61 69 6c 61 62 6c 65 20 6f 6e 20 70 2c 20 74 68  ailable on p, th
14ca8 65 6e 20 72 65 6c 6f 63 6b 20 61 6c 6c 20 6f 66  en relock all of
14ca9 20 74 68 65 0a 2a 2a 20 73 75 62 73 65 71 75 65   the.** subseque
14caa 6e 74 20 42 74 72 65 65 73 20 74 68 61 74 20 64  nt Btrees that d
14cab 65 73 69 72 65 20 61 20 6c 6f 63 6b 2e 0a 2a 2f  esire a lock..*/
14cac 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
14cad 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
14cae 65 45 6e 74 65 72 28 42 74 72 65 65 20 2a 70 29  eEnter(Btree *p)
14caf 7b 0a 20 20 42 74 72 65 65 20 2a 70 4c 61 74 65  {.  Btree *pLate
14cb0 72 3b 0a 0a 20 20 2f 2a 20 53 6f 6d 65 20 62 61  r;..  /* Some ba
14cb1 73 69 63 20 73 61 6e 69 74 79 20 63 68 65 63 6b  sic sanity check
14cb2 69 6e 67 20 6f 6e 20 74 68 65 20 42 74 72 65 65  ing on the Btree
14cb3 2e 20 20 54 68 65 20 6c 69 73 74 20 6f 66 20 42  .  The list of B
14cb4 74 72 65 65 73 0a 20 20 2a 2a 20 63 6f 6e 6e 65  trees.  ** conne
14cb5 63 74 65 64 20 62 79 20 70 4e 65 78 74 20 61 6e  cted by pNext an
14cb6 64 20 70 50 72 65 76 20 73 68 6f 75 6c 64 20 62  d pPrev should b
14cb7 65 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65  e in sorted orde
14cb8 72 20 62 79 0a 20 20 2a 2a 20 42 74 72 65 65 2e  r by.  ** Btree.
14cb9 70 42 74 20 76 61 6c 75 65 2e 20 41 6c 6c 20 65  pBt value. All e
14cba 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 6c  lements of the l
14cbb 69 73 74 20 73 68 6f 75 6c 64 20 62 65 6c 6f 6e  ist should belon
14cbc 67 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20 73 61  g to.  ** the sa
14cbd 6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 4f  me connection. O
14cbe 6e 6c 79 20 73 68 61 72 65 64 20 42 74 72 65 65  nly shared Btree
14cbf 73 20 61 72 65 20 6f 6e 20 74 68 65 20 6c 69 73  s are on the lis
14cc0 74 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  t. */.  assert( 
14cc1 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c 20 70  p->pNext==0 || p
14cc2 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 3e 70 2d 3e  ->pNext->pBt>p->
14cc3 70 42 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28  pBt );.  assert(
14cc4 20 70 2d 3e 70 50 72 65 76 3d 3d 30 20 7c 7c 20   p->pPrev==0 || 
14cc5 70 2d 3e 70 50 72 65 76 2d 3e 70 42 74 3c 70 2d  p->pPrev->pBt<p-
14cc6 3e 70 42 74 20 29 3b 0a 20 20 61 73 73 65 72 74  >pBt );.  assert
14cc7 28 20 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c  ( p->pNext==0 ||
14cc8 20 70 2d 3e 70 4e 65 78 74 2d 3e 64 62 3d 3d 70   p->pNext->db==p
14cc9 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74  ->db );.  assert
14cca 28 20 70 2d 3e 70 50 72 65 76 3d 3d 30 20 7c 7c  ( p->pPrev==0 ||
14ccb 20 70 2d 3e 70 50 72 65 76 2d 3e 64 62 3d 3d 70   p->pPrev->db==p
14ccc 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74  ->db );.  assert
14ccd 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c  ( p->sharable ||
14cce 20 28 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 26 26   (p->pNext==0 &&
14ccf 20 70 2d 3e 70 50 72 65 76 3d 3d 30 29 20 29 3b   p->pPrev==0) );
14cd0 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72  ..  /* Check for
14cd1 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 73 69 73 74   locking consist
14cd2 65 6e 63 79 20 2a 2f 0a 20 20 61 73 73 65 72 74  ency */.  assert
14cd3 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20  ( !p->locked || 
14cd4 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20  p->wantToLock>0 
14cd5 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
14cd6 73 68 61 72 61 62 6c 65 20 7c 7c 20 70 2d 3e 77  sharable || p->w
14cd7 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 3b 0a  antToLock==0 );.
14cd8 0a 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20  .  /* We should 
14cd9 61 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 20 6c  already hold a l
14cda 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ock on the datab
14cdb 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
14cdc 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  /.  assert( sqli
14cdd 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
14cde 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
14cdf 0a 20 20 2f 2a 20 55 6e 6c 65 73 73 20 74 68 65  .  /* Unless the
14ce0 20 64 61 74 61 62 61 73 65 20 69 73 20 73 68 61   database is sha
14ce1 72 61 62 6c 65 20 61 6e 64 20 75 6e 6c 6f 63 6b  rable and unlock
14ce2 65 64 2c 20 74 68 65 6e 20 42 74 53 68 61 72 65  ed, then BtShare
14ce3 64 2e 64 62 0a 20 20 2a 2a 20 73 68 6f 75 6c 64  d.db.  ** should
14ce4 20 61 6c 72 65 61 64 79 20 62 65 20 73 65 74 20   already be set 
14ce5 63 6f 72 72 65 63 74 6c 79 2e 20 2a 2f 0a 20 20  correctly. */.  
14ce6 61 73 73 65 72 74 28 20 28 70 2d 3e 6c 6f 63 6b  assert( (p->lock
14ce7 65 64 3d 3d 30 20 26 26 20 70 2d 3e 73 68 61 72  ed==0 && p->shar
14ce8 61 62 6c 65 29 20 7c 7c 20 70 2d 3e 70 42 74 2d  able) || p->pBt-
14ce9 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a 0a 20  >db==p->db );.. 
14cea 20 69 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c   if( !p->sharabl
14ceb 65 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 2d  e ) return;.  p-
14cec 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20  >wantToLock++;. 
14ced 20 69 66 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 29   if( p->locked )
14cee 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 49   return;..  /* I
14cef 6e 20 6d 6f 73 74 20 63 61 73 65 73 2c 20 77 65  n most cases, we
14cf0 20 73 68 6f 75 6c 64 20 62 65 20 61 62 6c 65 20   should be able 
14cf1 74 6f 20 61 63 71 75 69 72 65 20 74 68 65 20 6c  to acquire the l
14cf2 6f 63 6b 20 77 65 0a 20 20 2a 2a 20 77 61 6e 74  ock we.  ** want
14cf3 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20   without having 
14cf4 74 6f 20 67 6f 20 74 68 72 6f 75 67 68 74 20 74  to go throught t
14cf5 68 65 20 61 73 63 65 6e 64 69 6e 67 20 6c 6f 63  he ascending loc
14cf6 6b 0a 20 20 2a 2a 20 70 72 6f 63 65 64 75 72 65  k.  ** procedure
14cf7 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2e 20 20   that follows.  
14cf8 4a 75 73 74 20 62 65 20 73 75 72 65 20 6e 6f 74  Just be sure not
14cf9 20 74 6f 20 62 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a   to block..  */.
14cfa 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 6d 75    if( sqlite3_mu
14cfb 74 65 78 5f 74 72 79 28 70 2d 3e 70 42 74 2d 3e  tex_try(p->pBt->
14cfc 6d 75 74 65 78 29 3d 3d 53 51 4c 49 54 45 5f 4f  mutex)==SQLITE_O
14cfd 4b 20 29 7b 0a 20 20 20 20 70 2d 3e 70 42 74 2d  K ){.    p->pBt-
14cfe 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20  >db = p->db;.   
14cff 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31 3b 0a   p->locked = 1;.
14d00 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
14d01 0a 20 20 2f 2a 20 54 6f 20 61 76 6f 69 64 20 64  .  /* To avoid d
14d02 65 61 64 6c 6f 63 6b 2c 20 66 69 72 73 74 20 72  eadlock, first r
14d03 65 6c 65 61 73 65 20 61 6c 6c 20 6c 6f 63 6b 73  elease all locks
14d04 20 77 69 74 68 20 61 20 6c 61 72 67 65 72 0a 20   with a larger. 
14d05 20 2a 2a 20 42 74 53 68 61 72 65 64 20 61 64 64   ** BtShared add
14d06 72 65 73 73 2e 20 20 54 68 65 6e 20 61 63 71 75  ress.  Then acqu
14d07 69 72 65 20 6f 75 72 20 6c 6f 63 6b 2e 20 20 54  ire our lock.  T
14d08 68 65 6e 20 72 65 61 63 71 75 69 72 65 0a 20 20  hen reacquire.  
14d09 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 42 74 53  ** the other BtS
14d0a 68 61 72 65 64 20 6c 6f 63 6b 73 20 74 68 61 74  hared locks that
14d0b 20 77 65 20 75 73 65 64 20 74 6f 20 68 6f 6c 64   we used to hold
14d0c 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 0a 20 20   in ascending.  
14d0d 2a 2a 20 6f 72 64 65 72 2e 0a 20 20 2a 2f 0a 20  ** order..  */. 
14d0e 20 66 6f 72 28 70 4c 61 74 65 72 3d 70 2d 3e 70   for(pLater=p->p
14d0f 4e 65 78 74 3b 20 70 4c 61 74 65 72 3b 20 70 4c  Next; pLater; pL
14d10 61 74 65 72 3d 70 4c 61 74 65 72 2d 3e 70 4e 65  ater=pLater->pNe
14d11 78 74 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  xt){.    assert(
14d12 20 70 4c 61 74 65 72 2d 3e 73 68 61 72 61 62 6c   pLater->sharabl
14d13 65 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  e );.    assert(
14d14 20 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 3d 3d   pLater->pNext==
14d15 30 20 7c 7c 20 70 4c 61 74 65 72 2d 3e 70 4e 65  0 || pLater->pNe
14d16 78 74 2d 3e 70 42 74 3e 70 4c 61 74 65 72 2d 3e  xt->pBt>pLater->
14d17 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72  pBt );.    asser
14d18 74 28 20 21 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b  t( !pLater->lock
14d19 65 64 20 7c 7c 20 70 4c 61 74 65 72 2d 3e 77 61  ed || pLater->wa
14d1a 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20  ntToLock>0 );.  
14d1b 20 20 69 66 28 20 70 4c 61 74 65 72 2d 3e 6c 6f    if( pLater->lo
14d1c 63 6b 65 64 20 29 7b 0a 20 20 20 20 20 20 75 6e  cked ){.      un
14d1d 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 70  lockBtreeMutex(p
14d1e 4c 61 74 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20  Later);.    }.  
14d1f 7d 0a 20 20 6c 6f 63 6b 42 74 72 65 65 4d 75 74  }.  lockBtreeMut
14d20 65 78 28 70 29 3b 0a 20 20 66 6f 72 28 70 4c 61  ex(p);.  for(pLa
14d21 74 65 72 3d 70 2d 3e 70 4e 65 78 74 3b 20 70 4c  ter=p->pNext; pL
14d22 61 74 65 72 3b 20 70 4c 61 74 65 72 3d 70 4c 61  ater; pLater=pLa
14d23 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  ter->pNext){.   
14d24 20 69 66 28 20 70 4c 61 74 65 72 2d 3e 77 61 6e   if( pLater->wan
14d25 74 54 6f 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20  tToLock ){.     
14d26 20 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28   lockBtreeMutex(
14d27 70 4c 61 74 65 72 29 3b 0a 20 20 20 20 7d 0a 20  pLater);.    }. 
14d28 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 69 74   }.}../*.** Exit
14d29 20 74 68 65 20 72 65 63 75 72 73 69 76 65 20 6d   the recursive m
14d2a 75 74 65 78 20 6f 6e 20 61 20 42 74 72 65 65 2e  utex on a Btree.
14d2b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
14d2c 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
14d2d 74 72 65 65 4c 65 61 76 65 28 42 74 72 65 65 20  treeLeave(Btree 
14d2e 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 73 68  *p){.  if( p->sh
14d2f 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 61 73  arable ){.    as
14d30 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c  sert( p->wantToL
14d31 6f 63 6b 3e 30 20 29 3b 0a 20 20 20 20 70 2d 3e  ock>0 );.    p->
14d32 77 61 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b 0a 20 20  wantToLock--;.  
14d33 20 20 69 66 28 20 70 2d 3e 77 61 6e 74 54 6f 4c    if( p->wantToL
14d34 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ock==0 ){.      
14d35 75 6e 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78  unlockBtreeMutex
14d36 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  (p);.    }.  }.}
14d37 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
14d38 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72  ./*.** Return tr
14d39 75 65 20 69 66 20 74 68 65 20 42 74 53 68 61 72  ue if the BtShar
14d3a 65 64 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64  ed mutex is held
14d3b 20 6f 6e 20 74 68 65 20 62 74 72 65 65 2c 20 6f   on the btree, o
14d3c 72 20 69 66 20 74 68 65 0a 2a 2a 20 42 2d 54 72  r if the.** B-Tr
14d3d 65 65 20 69 73 20 6e 6f 74 20 6d 61 72 6b 65 64  ee is not marked
14d3e 20 61 73 20 73 68 61 72 61 62 6c 65 2e 0a 2a 2a   as sharable..**
14d3f 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
14d40 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 66 72   is used only fr
14d41 6f 6d 20 77 69 74 68 69 6e 20 61 73 73 65 72 74  om within assert
14d42 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a  () statements..*
14d43 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
14d44 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
14d45 65 48 6f 6c 64 73 4d 75 74 65 78 28 42 74 72 65  eHoldsMutex(Btre
14d46 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28  e *p){.  assert(
14d47 20 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20   p->sharable==0 
14d48 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20  || p->locked==0 
14d49 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b  || p->wantToLock
14d4a 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  >0 );.  assert( 
14d4b 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20 7c  p->sharable==0 |
14d4c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 7c  | p->locked==0 |
14d4d 7c 20 70 2d 3e 64 62 3d 3d 70 2d 3e 70 42 74 2d  | p->db==p->pBt-
14d4e 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28  >db );.  assert(
14d4f 20 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20   p->sharable==0 
14d50 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20  || p->locked==0 
14d51 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  || sqlite3_mutex
14d52 5f 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75  _held(p->pBt->mu
14d53 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
14d54 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30  ( p->sharable==0
14d55 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30   || p->locked==0
14d56 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   || sqlite3_mute
14d57 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75  x_held(p->db->mu
14d58 74 65 78 29 20 29 3b 0a 0a 20 20 72 65 74 75 72  tex) );..  retur
14d59 6e 20 28 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d  n (p->sharable==
14d5a 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 29 3b  0 || p->locked);
14d5b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e  .}.#endif...#ifn
14d5c 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
14d5d 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 45  INCRBLOB./*.** E
14d5e 6e 74 65 72 20 61 6e 64 20 6c 65 61 76 65 20 61  nter and leave a
14d5f 20 6d 75 74 65 78 20 6f 6e 20 61 20 42 74 72 65   mutex on a Btre
14d60 65 20 67 69 76 65 6e 20 61 20 63 75 72 73 6f 72  e given a cursor
14d61 20 6f 77 6e 65 64 20 62 79 20 74 68 61 74 0a 2a   owned by that.*
14d62 2a 20 42 74 72 65 65 2e 20 20 54 68 65 73 65 20  * Btree.  These 
14d63 65 6e 74 72 79 20 70 6f 69 6e 74 73 20 61 72 65  entry points are
14d64 20 75 73 65 64 20 62 79 20 69 6e 63 72 65 6d 65   used by increme
14d65 6e 74 61 6c 20 49 2f 4f 20 61 6e 64 20 63 61 6e  ntal I/O and can
14d66 20 62 65 0a 2a 2a 20 6f 6d 69 74 74 65 64 20 69   be.** omitted i
14d67 66 20 74 68 61 74 20 6d 6f 64 75 6c 65 20 69 73  f that module is
14d68 20 6e 6f 74 20 75 73 65 64 2e 0a 2a 2f 0a 53 51   not used..*/.SQ
14d69 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
14d6a 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e  d sqlite3BtreeEn
14d6b 74 65 72 43 75 72 73 6f 72 28 42 74 43 75 72 73  terCursor(BtCurs
14d6c 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 73 71 6c  or *pCur){.  sql
14d6d 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
14d6e 43 75 72 2d 3e 70 42 74 72 65 65 29 3b 0a 7d 0a  Cur->pBtree);.}.
14d6f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
14d70 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
14d71 4c 65 61 76 65 43 75 72 73 6f 72 28 42 74 43 75  LeaveCursor(BtCu
14d72 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 73  rsor *pCur){.  s
14d73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
14d74 28 70 43 75 72 2d 3e 70 42 74 72 65 65 29 3b 0a  (pCur->pBtree);.
14d75 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
14d76 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42  TE_OMIT_INCRBLOB
14d77 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65   */.../*.** Ente
14d78 72 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 20 65  r the mutex on e
14d79 76 65 72 79 20 42 74 72 65 65 20 61 73 73 6f 63  very Btree assoc
14d7a 69 61 74 65 64 20 77 69 74 68 20 61 20 64 61 74  iated with a dat
14d7b 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74  abase.** connect
14d7c 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 6e 65  ion.  This is ne
14d7d 65 64 65 64 20 28 66 6f 72 20 65 78 61 6d 70 6c  eded (for exampl
14d7e 65 29 20 70 72 69 6f 72 20 74 6f 20 70 61 72 73  e) prior to pars
14d7f 69 6e 67 0a 2a 2a 20 61 20 73 74 61 74 65 6d 65  ing.** a stateme
14d80 6e 74 20 73 69 6e 63 65 20 77 65 20 77 69 6c 6c  nt since we will
14d81 20 62 65 20 63 6f 6d 70 61 72 69 6e 67 20 74 61   be comparing ta
14d82 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 6e  ble and column n
14d83 61 6d 65 73 0a 2a 2a 20 61 67 61 69 6e 73 74 20  ames.** against 
14d84 61 6c 6c 20 73 63 68 65 6d 61 73 20 61 6e 64 20  all schemas and 
14d85 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74  we do not want t
14d86 68 6f 73 65 20 73 63 68 65 6d 61 73 20 62 65 69  hose schemas bei
14d87 6e 67 0a 2a 2a 20 72 65 73 65 74 20 6f 75 74 20  ng.** reset out 
14d88 66 72 6f 6d 20 75 6e 64 65 72 20 75 73 2e 0a 2a  from under us..*
14d89 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20  *.** There is a 
14d8a 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 6c 65  corresponding le
14d8b 61 76 65 2d 61 6c 6c 20 70 72 6f 63 65 64 75 72  ave-all procedur
14d8c 65 73 2e 0a 2a 2a 0a 2a 2a 20 45 6e 74 65 72 20  es..**.** Enter 
14d8d 74 68 65 20 6d 75 74 65 78 65 73 20 69 6e 20 61  the mutexes in a
14d8e 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62  ccending order b
14d8f 79 20 42 74 53 68 61 72 65 64 20 70 6f 69 6e 74  y BtShared point
14d90 65 72 20 61 64 64 72 65 73 73 0a 2a 2a 20 74 6f  er address.** to
14d91 20 61 76 6f 69 64 20 74 68 65 20 70 6f 73 73 69   avoid the possi
14d92 62 69 6c 69 74 79 20 6f 66 20 64 65 61 64 6c 6f  bility of deadlo
14d93 63 6b 20 77 68 65 6e 20 74 77 6f 20 74 68 72 65  ck when two thre
14d94 61 64 73 20 77 69 74 68 0a 2a 2a 20 74 77 6f 20  ads with.** two 
14d95 6f 72 20 6d 6f 72 65 20 62 74 72 65 65 73 20 69  or more btrees i
14d96 6e 20 63 6f 6d 6d 6f 6e 20 62 6f 74 68 20 74 72  n common both tr
14d97 79 20 74 6f 20 6c 6f 63 6b 20 61 6c 6c 20 74 68  y to lock all th
14d98 65 69 72 20 62 74 72 65 65 73 0a 2a 2a 20 61 74  eir btrees.** at
14d99 20 74 68 65 20 73 61 6d 65 20 69 6e 73 74 61 6e   the same instan
14d9a 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
14d9b 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
14d9c 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 73  3BtreeEnterAll(s
14d9d 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69  qlite3 *db){.  i
14d9e 6e 74 20 69 3b 0a 20 20 42 74 72 65 65 20 2a 70  nt i;.  Btree *p
14d9f 2c 20 2a 70 4c 61 74 65 72 3b 0a 20 20 61 73 73  , *pLater;.  ass
14da0 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
14da1 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
14da2 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  x) );.  for(i=0;
14da3 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29   i<db->nDb; i++)
14da4 7b 0a 20 20 20 20 70 20 3d 20 64 62 2d 3e 61 44  {.    p = db->aD
14da5 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 61 73  b[i].pBt;.    as
14da6 73 65 72 74 28 20 21 70 20 7c 7c 20 28 70 2d 3e  sert( !p || (p->
14da7 6c 6f 63 6b 65 64 3d 3d 30 20 26 26 20 70 2d 3e  locked==0 && p->
14da8 73 68 61 72 61 62 6c 65 29 20 7c 7c 20 70 2d 3e  sharable) || p->
14da9 70 42 74 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29  pBt->db==p->db )
14daa 3b 0a 20 20 20 20 69 66 28 20 70 20 26 26 20 70  ;.    if( p && p
14dab 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20  ->sharable ){.  
14dac 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63      p->wantToLoc
14dad 6b 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 21  k++;.      if( !
14dae 70 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20  p->locked ){.   
14daf 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
14db0 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 31 20 29 3b  wantToLock==1 );
14db1 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
14db2 70 2d 3e 70 50 72 65 76 20 29 20 70 20 3d 20 70  p->pPrev ) p = p
14db3 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 20 20 20  ->pPrev;.       
14db4 20 2f 2a 20 52 65 61 73 6f 6e 20 66 6f 72 20 41   /* Reason for A
14db5 4c 57 41 59 53 3a 20 20 54 68 65 72 65 20 6d 75  LWAYS:  There mu
14db6 73 74 20 62 65 20 61 74 20 6c 65 61 73 74 20 6f  st be at least o
14db7 6e 20 75 6e 6c 6f 63 6b 65 64 20 42 74 72 65 65  n unlocked Btree
14db8 20 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74   in.        ** t
14db9 68 65 20 63 68 61 69 6e 2e 20 20 4f 74 68 65 72  he chain.  Other
14dba 77 69 73 65 20 74 68 65 20 21 70 2d 3e 6c 6f 63  wise the !p->loc
14dbb 6b 65 64 20 74 65 73 74 20 61 62 6f 76 65 20 77  ked test above w
14dbc 6f 75 6c 64 20 68 61 76 65 20 66 61 69 6c 65 64  ould have failed
14dbd 20 2a 2f 0a 20 20 20 20 20 20 20 20 77 68 69 6c   */.        whil
14dbe 65 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 26 26 20  e( p->locked && 
14dbf 41 4c 57 41 59 53 28 70 2d 3e 70 4e 65 78 74 29  ALWAYS(p->pNext)
14dc0 20 29 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b   ) p = p->pNext;
14dc1 0a 20 20 20 20 20 20 20 20 66 6f 72 28 70 4c 61  .        for(pLa
14dc2 74 65 72 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 20  ter = p->pNext; 
14dc3 70 4c 61 74 65 72 3b 20 70 4c 61 74 65 72 3d 70  pLater; pLater=p
14dc4 4c 61 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20  Later->pNext){. 
14dc5 20 20 20 20 20 20 20 20 20 69 66 28 20 70 4c 61           if( pLa
14dc6 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20  ter->locked ){. 
14dc7 20 20 20 20 20 20 20 20 20 20 20 75 6e 6c 6f 63             unloc
14dc8 6b 42 74 72 65 65 4d 75 74 65 78 28 70 4c 61 74  kBtreeMutex(pLat
14dc9 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  er);.          }
14dca 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
14dcb 20 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20     while( p ){. 
14dcc 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 42 74 72           lockBtr
14dcd 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20  eeMutex(p);.    
14dce 20 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 4e 65        p = p->pNe
14dcf 78 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  xt;.        }.  
14dd0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
14dd1 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
14dd2 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
14dd3 65 65 4c 65 61 76 65 41 6c 6c 28 73 71 6c 69 74  eeLeaveAll(sqlit
14dd4 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69  e3 *db){.  int i
14dd5 3b 0a 20 20 42 74 72 65 65 20 2a 70 3b 0a 20 20  ;.  Btree *p;.  
14dd6 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
14dd7 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d  mutex_held(db->m
14dd8 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69  utex) );.  for(i
14dd9 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69  =0; i<db->nDb; i
14dda 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 64 62 2d  ++){.    p = db-
14ddb 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20  >aDb[i].pBt;.   
14ddc 20 69 66 28 20 70 20 26 26 20 70 2d 3e 73 68 61   if( p && p->sha
14ddd 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 61  rable ){.      a
14dde 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f  ssert( p->wantTo
14ddf 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 20 20 20 20  Lock>0 );.      
14de0 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b  p->wantToLock--;
14de1 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 77 61  .      if( p->wa
14de2 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20  ntToLock==0 ){. 
14de3 20 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72         unlockBtr
14de4 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20  eeMutex(p);.    
14de5 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a    }.    }.  }.}.
14de6 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
14de7 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75  /*.** Return tru
14de8 65 20 69 66 20 74 68 65 20 63 75 72 72 65 6e 74  e if the current
14de9 20 74 68 72 65 61 64 20 68 6f 6c 64 73 20 74 68   thread holds th
14dea 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
14deb 63 74 69 6f 6e 0a 2a 2a 20 6d 75 74 65 78 20 61  ction.** mutex a
14dec 6e 64 20 61 6c 6c 20 72 65 71 75 69 72 65 64 20  nd all required 
14ded 42 74 53 68 61 72 65 64 20 6d 75 74 65 78 65 73  BtShared mutexes
14dee 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
14def 74 69 6e 65 20 69 73 20 75 73 65 64 20 69 6e 73  tine is used ins
14df0 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61  ide assert() sta
14df1 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 0a 2a 2f  tements only..*/
14df2 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
14df3 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
14df4 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28  HoldsAllMutexes(
14df5 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
14df6 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 21 73 71  int i;.  if( !sq
14df7 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
14df8 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 7b 0a 20  (db->mutex) ){. 
14df9 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
14dfa 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62  .  for(i=0; i<db
14dfb 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nDb; i++){.   
14dfc 20 42 74 72 65 65 20 2a 70 3b 0a 20 20 20 20 70   Btree *p;.    p
14dfd 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42   = db->aDb[i].pB
14dfe 74 3b 0a 20 20 20 20 69 66 28 20 70 20 26 26 20  t;.    if( p && 
14dff 70 2d 3e 73 68 61 72 61 62 6c 65 20 26 26 0a 20  p->sharable &&. 
14e00 20 20 20 20 20 20 20 20 28 70 2d 3e 77 61 6e 74          (p->want
14e01 54 6f 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 21 73 71  ToLock==0 || !sq
14e02 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
14e03 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 29  (p->pBt->mutex))
14e04 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
14e05 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   0;.    }.  }.  
14e06 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64  return 1;.}.#end
14e07 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a  if /* NDEBUG */.
14e08 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77  ./*.** Add a new
14e09 20 42 74 72 65 65 20 70 6f 69 6e 74 65 72 20 74   Btree pointer t
14e0a 6f 20 61 20 42 74 72 65 65 4d 75 74 65 78 41 72  o a BtreeMutexAr
14e0b 72 61 79 2e 20 0a 2a 2a 20 69 66 20 74 68 65 20  ray. .** if the 
14e0c 70 6f 69 6e 74 65 72 20 63 61 6e 20 70 6f 73 73  pointer can poss
14e0d 69 62 6c 79 20 62 65 20 73 68 61 72 65 64 20 77  ibly be shared w
14e0e 69 74 68 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 64  ith.** another d
14e0f 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
14e10 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f  on..**.** The po
14e11 69 6e 74 65 72 73 20 61 72 65 20 6b 65 70 74 20  inters are kept 
14e12 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 20  in sorted order 
14e13 62 79 20 70 42 74 72 65 65 2d 3e 70 42 74 2e 20  by pBtree->pBt. 
14e14 20 54 68 61 74 0a 2a 2a 20 77 61 79 20 77 68 65   That.** way whe
14e15 6e 20 77 65 20 67 6f 20 74 6f 20 65 6e 74 65 72  n we go to enter
14e16 20 61 6c 6c 20 74 68 65 20 6d 75 74 65 78 65 73   all the mutexes
14e17 2c 20 77 65 20 63 61 6e 20 65 6e 74 65 72 20 74  , we can enter t
14e18 68 65 6d 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20  hem.** in order 
14e19 77 69 74 68 6f 75 74 20 65 76 65 72 79 20 68 61  without every ha
14e1a 76 69 6e 67 20 74 6f 20 62 61 63 6b 75 70 20 61  ving to backup a
14e1b 6e 64 20 72 65 74 72 79 20 61 6e 64 20 77 69 74  nd retry and wit
14e1c 68 6f 75 74 0a 2a 2a 20 77 6f 72 72 79 69 6e 67  hout.** worrying
14e1d 20 61 62 6f 75 74 20 64 65 61 64 6c 6f 63 6b 2e   about deadlock.
14e1e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65  .**.** The numbe
14e1f 72 20 6f 66 20 73 68 61 72 65 64 20 62 74 72 65  r of shared btre
14e20 65 73 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62  es will always b
14e21 65 20 73 6d 61 6c 6c 20 28 75 73 75 61 6c 6c 79  e small (usually
14e22 20 30 20 6f 72 20 31 29 0a 2a 2a 20 73 6f 20 61   0 or 1).** so a
14e23 6e 20 69 6e 73 65 72 74 69 6f 6e 20 73 6f 72 74  n insertion sort
14e24 20 69 73 20 61 6e 20 61 64 65 71 75 61 74 65 20   is an adequate 
14e25 61 6c 67 6f 72 69 74 68 6d 20 68 65 72 65 2e 0a  algorithm here..
14e26 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
14e27 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74  E void sqlite3Bt
14e28 72 65 65 4d 75 74 65 78 41 72 72 61 79 49 6e 73  reeMutexArrayIns
14e29 65 72 74 28 42 74 72 65 65 4d 75 74 65 78 41 72  ert(BtreeMutexAr
14e2a 72 61 79 20 2a 70 41 72 72 61 79 2c 20 42 74 72  ray *pArray, Btr
14e2b 65 65 20 2a 70 42 74 72 65 65 29 7b 0a 20 20 69  ee *pBtree){.  i
14e2c 6e 74 20 69 2c 20 6a 3b 0a 20 20 42 74 53 68 61  nt i, j;.  BtSha
14e2d 72 65 64 20 2a 70 42 74 3b 0a 20 20 69 66 28 20  red *pBt;.  if( 
14e2e 70 42 74 72 65 65 3d 3d 30 20 7c 7c 20 70 42 74  pBtree==0 || pBt
14e2f 72 65 65 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30  ree->sharable==0
14e30 20 29 20 72 65 74 75 72 6e 3b 0a 23 69 66 6e 64   ) return;.#ifnd
14e31 65 66 20 4e 44 45 42 55 47 0a 20 20 7b 0a 20 20  ef NDEBUG.  {.  
14e32 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 72    for(i=0; i<pAr
14e33 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69 2b 2b  ray->nMutex; i++
14e34 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
14e35 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b   pArray->aBtree[
14e36 69 5d 21 3d 70 42 74 72 65 65 20 29 3b 0a 20 20  i]!=pBtree );.  
14e37 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20    }.  }.#endif. 
14e38 20 61 73 73 65 72 74 28 20 70 41 72 72 61 79 2d   assert( pArray-
14e39 3e 6e 4d 75 74 65 78 3e 3d 30 20 29 3b 0a 20 20  >nMutex>=0 );.  
14e3a 61 73 73 65 72 74 28 20 70 41 72 72 61 79 2d 3e  assert( pArray->
14e3b 6e 4d 75 74 65 78 3c 41 72 72 61 79 53 69 7a 65  nMutex<ArraySize
14e3c 28 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 29  (pArray->aBtree)
14e3d 2d 31 20 29 3b 0a 20 20 70 42 74 20 3d 20 70 42  -1 );.  pBt = pB
14e3e 74 72 65 65 2d 3e 70 42 74 3b 0a 20 20 66 6f 72  tree->pBt;.  for
14e3f 28 69 3d 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e  (i=0; i<pArray->
14e40 6e 4d 75 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20  nMutex; i++){.  
14e41 20 20 61 73 73 65 72 74 28 20 70 41 72 72 61 79    assert( pArray
14e42 2d 3e 61 42 74 72 65 65 5b 69 5d 21 3d 70 42 74  ->aBtree[i]!=pBt
14e43 72 65 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70  ree );.    if( p
14e44 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d  Array->aBtree[i]
14e45 2d 3e 70 42 74 3e 70 42 74 20 29 7b 0a 20 20 20  ->pBt>pBt ){.   
14e46 20 20 20 66 6f 72 28 6a 3d 70 41 72 72 61 79 2d     for(j=pArray-
14e47 3e 6e 4d 75 74 65 78 3b 20 6a 3e 69 3b 20 6a 2d  >nMutex; j>i; j-
14e48 2d 29 7b 0a 20 20 20 20 20 20 20 20 70 41 72 72  -){.        pArr
14e49 61 79 2d 3e 61 42 74 72 65 65 5b 6a 5d 20 3d 20  ay->aBtree[j] = 
14e4a 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 6a  pArray->aBtree[j
14e4b 2d 31 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  -1];.      }.   
14e4c 20 20 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65     pArray->aBtre
14e4d 65 5b 69 5d 20 3d 20 70 42 74 72 65 65 3b 0a 20  e[i] = pBtree;. 
14e4e 20 20 20 20 20 70 41 72 72 61 79 2d 3e 6e 4d 75       pArray->nMu
14e4f 74 65 78 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74  tex++;.      ret
14e50 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  urn;.    }.  }. 
14e51 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b   pArray->aBtree[
14e52 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 2b 2b  pArray->nMutex++
14e53 5d 20 3d 20 70 42 74 72 65 65 3b 0a 7d 0a 0a 2f  ] = pBtree;.}../
14e54 2a 0a 2a 2a 20 45 6e 74 65 72 20 74 68 65 20 6d  *.** Enter the m
14e55 75 74 65 78 20 6f 66 20 65 76 65 72 79 20 62 74  utex of every bt
14e56 72 65 65 20 69 6e 20 74 68 65 20 61 72 72 61 79  ree in the array
14e57 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
14e58 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 61 74 20  is.** called at 
14e59 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
14e5a 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63   sqlite3VdbeExec
14e5b 28 29 2e 20 20 54 68 65 20 6d 75 74 65 78 65 73  ().  The mutexes
14e5c 20 61 72 65 0a 2a 2a 20 65 78 69 74 65 64 20 61   are.** exited a
14e5d 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
14e5e 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 2e 0a   same function..
14e5f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
14e60 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74  E void sqlite3Bt
14e61 72 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74  reeMutexArrayEnt
14e62 65 72 28 42 74 72 65 65 4d 75 74 65 78 41 72 72  er(BtreeMutexArr
14e63 61 79 20 2a 70 41 72 72 61 79 29 7b 0a 20 20 69  ay *pArray){.  i
14e64 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  nt i;.  for(i=0;
14e65 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65   i<pArray->nMute
14e66 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72  x; i++){.    Btr
14e67 65 65 20 2a 70 20 3d 20 70 41 72 72 61 79 2d 3e  ee *p = pArray->
14e68 61 42 74 72 65 65 5b 69 5d 3b 0a 20 20 20 20 2f  aBtree[i];.    /
14e69 2a 20 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e  * Some basic san
14e6a 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a  ity checking */.
14e6b 20 20 20 20 61 73 73 65 72 74 28 20 69 3d 3d 30      assert( i==0
14e6c 20 7c 7c 20 70 41 72 72 61 79 2d 3e 61 42 74 72   || pArray->aBtr
14e6d 65 65 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e  ee[i-1]->pBt<p->
14e6e 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72  pBt );.    asser
14e6f 74 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c  t( !p->locked ||
14e70 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30   p->wantToLock>0
14e71 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 57 65 20 73   );..    /* We s
14e72 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 68 6f  hould already ho
14e73 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  ld a lock on the
14e74 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
14e75 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 61 73 73 65  tion */.    asse
14e76 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
14e77 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75  x_held(p->db->mu
14e78 74 65 78 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20  tex) );..    /* 
14e79 54 68 65 20 42 74 72 65 65 20 69 73 20 73 68 61  The Btree is sha
14e7a 72 61 62 6c 65 20 62 65 63 61 75 73 65 20 6f 6e  rable because on
14e7b 6c 79 20 73 68 61 72 61 62 6c 65 20 42 74 72 65  ly sharable Btre
14e7c 65 73 20 61 72 65 20 65 6e 74 65 72 65 64 0a 20  es are entered. 
14e7d 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 61     ** into the a
14e7e 72 72 61 79 20 69 6e 20 74 68 65 20 66 69 72 73  rray in the firs
14e7f 74 20 70 6c 61 63 65 2e 20 2a 2f 0a 20 20 20 20  t place. */.    
14e80 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61  assert( p->shara
14e81 62 6c 65 20 29 3b 0a 0a 20 20 20 20 70 2d 3e 77  ble );..    p->w
14e82 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 20  antToLock++;.   
14e83 20 69 66 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20   if( !p->locked 
14e84 29 7b 0a 20 20 20 20 20 20 6c 6f 63 6b 42 74 72  ){.      lockBtr
14e85 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20  eeMutex(p);.    
14e86 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c  }.  }.}../*.** L
14e87 65 61 76 65 20 74 68 65 20 6d 75 74 65 78 20 6f  eave the mutex o
14e88 66 20 65 76 65 72 79 20 62 74 72 65 65 20 69 6e  f every btree in
14e89 20 74 68 65 20 67 72 6f 75 70 2e 0a 2a 2f 0a 53   the group..*/.S
14e8a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
14e8b 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d  id sqlite3BtreeM
14e8c 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 28 42  utexArrayLeave(B
14e8d 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 20 2a  treeMutexArray *
14e8e 70 41 72 72 61 79 29 7b 0a 20 20 69 6e 74 20 69  pArray){.  int i
14e8f 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
14e90 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69  Array->nMutex; i
14e91 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a  ++){.    Btree *
14e92 70 20 3d 20 70 41 72 72 61 79 2d 3e 61 42 74 72  p = pArray->aBtr
14e93 65 65 5b 69 5d 3b 0a 20 20 20 20 2f 2a 20 53 6f  ee[i];.    /* So
14e94 6d 65 20 62 61 73 69 63 20 73 61 6e 69 74 79 20  me basic sanity 
14e95 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20  checking */.    
14e96 61 73 73 65 72 74 28 20 69 3d 3d 30 20 7c 7c 20  assert( i==0 || 
14e97 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69  pArray->aBtree[i
14e98 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 20  -1]->pBt<p->pBt 
14e99 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
14e9a 2d 3e 6c 6f 63 6b 65 64 20 29 3b 0a 20 20 20 20  ->locked );.    
14e9b 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54  assert( p->wantT
14e9c 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 0a 20 20 20 20  oLock>0 );..    
14e9d 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 61 6c 72  /* We should alr
14e9e 65 61 64 79 20 68 6f 6c 64 20 61 20 6c 6f 63 6b  eady hold a lock
14e9f 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
14ea0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
14ea1 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
14ea2 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d  e3_mutex_held(p-
14ea3 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a  >db->mutex) );..
14ea4 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63      p->wantToLoc
14ea5 6b 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  k--;.    if( p->
14ea6 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 7b  wantToLock==0 ){
14ea7 0a 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72  .      unlockBtr
14ea8 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20  eeMutex(p);.    
14ea9 7d 0a 20 20 7d 0a 7d 0a 0a 23 65 6c 73 65 0a 53  }.  }.}..#else.S
14eaa 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
14eab 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  id sqlite3BtreeE
14eac 6e 74 65 72 28 42 74 72 65 65 20 2a 70 29 7b 0a  nter(Btree *p){.
14ead 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70    p->pBt->db = p
14eae 2d 3e 64 62 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  ->db;.}.SQLITE_P
14eaf 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
14eb0 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c  te3BtreeEnterAll
14eb1 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
14eb2 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d   int i;.  for(i=
14eb3 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  0; i<db->nDb; i+
14eb4 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70  +){.    Btree *p
14eb5 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42   = db->aDb[i].pB
14eb6 74 3b 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a  t;.    if( p ){.
14eb7 20 20 20 20 20 20 70 2d 3e 70 42 74 2d 3e 64 62        p->pBt->db
14eb8 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 7d 0a   = p->db;.    }.
14eb9 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20    }.}.#endif /* 
14eba 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  if SQLITE_THREAD
14ebb 53 41 46 45 20 2a 2f 0a 23 65 6e 64 69 66 20 2f  SAFE */.#endif /
14ebc 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  * ifndef SQLITE_
14ebd 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
14ebe 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  E */../*********
14ebf 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 6d  ***** End of btm
14ec0 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  utex.c *********
14ec1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14ec2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14ec3 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
14ec4 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
14ec5 20 62 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a   btree.c *******
14ec6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14ec7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14ec8 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34  ****/./*.** 2004
14ec9 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54   April 6.**.** T
14eca 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
14ecb 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
14ecc 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
14ecd 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
14ece 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
14ecf 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
14ed0 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
14ed1 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
14ed2 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
14ed3 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
14ed4 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
14ed5 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
14ed6 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
14ed7 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
14ed8 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
14ed9 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
14eda 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
14edb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14edc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14edd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14ede 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14edf 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  *******.** $Id: 
14ee0 62 74 72 65 65 2e 63 2c 76 20 31 2e 37 30 35 20  btree.c,v 1.705 
14ee1 32 30 30 39 2f 30 38 2f 31 30 20 30 33 3a 35 37  2009/08/10 03:57
14ee2 3a 35 38 20 73 68 61 6e 65 20 45 78 70 20 24 0a  :58 shane Exp $.
14ee3 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
14ee4 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 65 78 74  implements a ext
14ee5 65 72 6e 61 6c 20 28 64 69 73 6b 2d 62 61 73 65  ernal (disk-base
14ee6 64 29 20 64 61 74 61 62 61 73 65 20 75 73 69 6e  d) database usin
14ee7 67 20 42 54 72 65 65 73 2e 0a 2a 2a 20 53 65 65  g BTrees..** See
14ee8 20 74 68 65 20 68 65 61 64 65 72 20 63 6f 6d 6d   the header comm
14ee9 65 6e 74 20 6f 6e 20 22 62 74 72 65 65 49 6e 74  ent on "btreeInt
14eea 2e 68 22 20 66 6f 72 20 61 64 64 69 74 69 6f 6e  .h" for addition
14eeb 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  al information..
14eec 2a 2a 20 49 6e 63 6c 75 64 69 6e 67 20 61 20 64  ** Including a d
14eed 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 66 69  escription of fi
14eee 6c 65 20 66 6f 72 6d 61 74 20 61 6e 64 20 61 6e  le format and an
14eef 20 6f 76 65 72 76 69 65 77 20 6f 66 20 6f 70 65   overview of ope
14ef0 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a  ration..*/../*.*
14ef1 2a 20 54 68 65 20 68 65 61 64 65 72 20 73 74 72  * The header str
14ef2 69 6e 67 20 74 68 61 74 20 61 70 70 65 61 72 73  ing that appears
14ef3 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e   at the beginnin
14ef4 67 20 6f 66 20 65 76 65 72 79 0a 2a 2a 20 53 51  g of every.** SQ
14ef5 4c 69 74 65 20 64 61 74 61 62 61 73 65 2e 0a 2a  Lite database..*
14ef6 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  /.static const c
14ef7 68 61 72 20 7a 4d 61 67 69 63 48 65 61 64 65 72  har zMagicHeader
14ef8 5b 5d 20 3d 20 53 51 4c 49 54 45 5f 46 49 4c 45  [] = SQLITE_FILE
14ef9 5f 48 45 41 44 45 52 3b 0a 0a 2f 2a 0a 2a 2a 20  _HEADER;../*.** 
14efa 53 65 74 20 74 68 69 73 20 67 6c 6f 62 61 6c 20  Set this global 
14efb 76 61 72 69 61 62 6c 65 20 74 6f 20 31 20 74 6f  variable to 1 to
14efc 20 65 6e 61 62 6c 65 20 74 72 61 63 69 6e 67 20   enable tracing 
14efd 75 73 69 6e 67 20 74 68 65 20 54 52 41 43 45 0a  using the TRACE.
14efe 2a 2a 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66  ** macro..*/.#if
14eff 20 30 0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74   0.int sqlite3Bt
14f00 72 65 65 54 72 61 63 65 3d 31 3b 20 20 2f 2a 20  reeTrace=1;  /* 
14f01 54 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 74  True to enable t
14f02 72 61 63 69 6e 67 20 2a 2f 0a 23 20 64 65 66 69  racing */.# defi
14f03 6e 65 20 54 52 41 43 45 28 58 29 20 20 69 66 28  ne TRACE(X)  if(
14f04 73 71 6c 69 74 65 33 42 74 72 65 65 54 72 61 63  sqlite3BtreeTrac
14f05 65 29 7b 70 72 69 6e 74 66 20 58 3b 66 66 6c 75  e){printf X;fflu
14f06 73 68 28 73 74 64 6f 75 74 29 3b 7d 0a 23 65 6c  sh(stdout);}.#el
14f07 73 65 0a 23 20 64 65 66 69 6e 65 20 54 52 41 43  se.# define TRAC
14f08 45 28 58 29 0a 23 65 6e 64 69 66 0a 0a 0a 0a 23  E(X).#endif....#
14f09 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
14f0a 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a  IT_SHARED_CACHE.
14f0b 2f 2a 0a 2a 2a 20 41 20 6c 69 73 74 20 6f 66 20  /*.** A list of 
14f0c 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 73  BtShared objects
14f0d 20 74 68 61 74 20 61 72 65 20 65 6c 69 67 69 62   that are eligib
14f0e 6c 65 20 66 6f 72 20 70 61 72 74 69 63 69 70 61  le for participa
14f0f 74 69 6f 6e 0a 2a 2a 20 69 6e 20 73 68 61 72 65  tion.** in share
14f10 64 20 63 61 63 68 65 2e 20 20 54 68 69 73 20 76  d cache.  This v
14f11 61 72 69 61 62 6c 65 20 68 61 73 20 66 69 6c 65  ariable has file
14f12 20 73 63 6f 70 65 20 64 75 72 69 6e 67 20 6e 6f   scope during no
14f13 72 6d 61 6c 20 62 75 69 6c 64 73 2c 0a 2a 2a 20  rmal builds,.** 
14f14 62 75 74 20 74 68 65 20 74 65 73 74 20 68 61 72  but the test har
14f15 6e 65 73 73 20 6e 65 65 64 73 20 74 6f 20 61 63  ness needs to ac
14f16 63 65 73 73 20 69 74 20 73 6f 20 77 65 20 6d 61  cess it so we ma
14f17 6b 65 20 69 74 20 67 6c 6f 62 61 6c 20 66 6f 72  ke it global for
14f18 20 0a 2a 2a 20 74 65 73 74 20 62 75 69 6c 64 73   .** test builds
14f19 2e 0a 2a 2a 0a 2a 2a 20 41 63 63 65 73 73 20 74  ..**.** Access t
14f1a 6f 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20  o this variable 
14f1b 69 73 20 70 72 6f 74 65 63 74 65 64 20 62 79 20  is protected by 
14f1c 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
14f1d 54 49 43 5f 4d 41 53 54 45 52 2e 0a 2a 2f 0a 23  TIC_MASTER..*/.#
14f1e 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
14f1f 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  T.SQLITE_PRIVATE
14f20 20 42 74 53 68 61 72 65 64 20 2a 53 51 4c 49 54   BtShared *SQLIT
14f21 45 5f 57 53 44 20 73 71 6c 69 74 65 33 53 68 61  E_WSD sqlite3Sha
14f22 72 65 64 43 61 63 68 65 4c 69 73 74 20 3d 20 30  redCacheList = 0
14f23 3b 0a 23 65 6c 73 65 0a 73 74 61 74 69 63 20 42  ;.#else.static B
14f24 74 53 68 61 72 65 64 20 2a 53 51 4c 49 54 45 5f  tShared *SQLITE_
14f25 57 53 44 20 73 71 6c 69 74 65 33 53 68 61 72 65  WSD sqlite3Share
14f26 64 43 61 63 68 65 4c 69 73 74 20 3d 20 30 3b 0a  dCacheList = 0;.
14f27 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a  #endif.#endif /*
14f28 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
14f29 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69  RED_CACHE */..#i
14f2a 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
14f2b 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f  T_SHARED_CACHE./
14f2c 2a 0a 2a 2a 20 45 6e 61 62 6c 65 20 6f 72 20 64  *.** Enable or d
14f2d 69 73 61 62 6c 65 20 74 68 65 20 73 68 61 72 65  isable the share
14f2e 64 20 70 61 67 65 72 20 61 6e 64 20 73 63 68 65  d pager and sche
14f2f 6d 61 20 66 65 61 74 75 72 65 73 2e 0a 2a 2a 0a  ma features..**.
14f30 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
14f31 68 61 73 20 6e 6f 20 65 66 66 65 63 74 20 6f 6e  has no effect on
14f32 20 65 78 69 73 74 69 6e 67 20 64 61 74 61 62 61   existing databa
14f33 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 0a  se connections..
14f34 2a 2a 20 54 68 65 20 73 68 61 72 65 64 20 63 61  ** The shared ca
14f35 63 68 65 20 73 65 74 74 69 6e 67 20 65 66 66 65  che setting effe
14f36 63 74 73 20 6f 6e 6c 79 20 66 75 74 75 72 65 20  cts only future 
14f37 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69  calls to.** sqli
14f38 74 65 33 5f 6f 70 65 6e 28 29 2c 20 73 71 6c 69  te3_open(), sqli
14f39 74 65 33 5f 6f 70 65 6e 31 36 28 29 2c 20 6f 72  te3_open16(), or
14f3a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32   sqlite3_open_v2
14f3b 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ()..*/.SQLITE_AP
14f3c 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e  I int sqlite3_en
14f3d 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68  able_shared_cach
14f3e 65 28 69 6e 74 20 65 6e 61 62 6c 65 29 7b 0a 20  e(int enable){. 
14f3f 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
14f40 6e 66 69 67 2e 73 68 61 72 65 64 43 61 63 68 65  nfig.sharedCache
14f41 45 6e 61 62 6c 65 64 20 3d 20 65 6e 61 62 6c 65  Enabled = enable
14f42 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
14f43 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  E_OK;.}.#endif..
14f44 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
14f45 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
14f46 45 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20  E.  /*.  ** The 
14f47 66 75 6e 63 74 69 6f 6e 73 20 71 75 65 72 79 53  functions queryS
14f48 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c  haredCacheTableL
14f49 6f 63 6b 28 29 2c 20 73 65 74 53 68 61 72 65 64  ock(), setShared
14f4a 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29  CacheTableLock()
14f4b 2c 0a 20 20 2a 2a 20 61 6e 64 20 63 6c 65 61 72  ,.  ** and clear
14f4c 41 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 61  AllSharedCacheTa
14f4d 62 6c 65 4c 6f 63 6b 73 28 29 0a 20 20 2a 2a 20  bleLocks().  ** 
14f4e 6d 61 6e 69 70 75 6c 61 74 65 20 65 6e 74 72 69  manipulate entri
14f4f 65 73 20 69 6e 20 74 68 65 20 42 74 53 68 61 72  es in the BtShar
14f50 65 64 2e 70 4c 6f 63 6b 20 6c 69 6e 6b 65 64 20  ed.pLock linked 
14f51 6c 69 73 74 20 75 73 65 64 20 74 6f 20 73 74 6f  list used to sto
14f52 72 65 0a 20 20 2a 2a 20 73 68 61 72 65 64 2d 63  re.  ** shared-c
14f53 61 63 68 65 20 74 61 62 6c 65 20 6c 65 76 65 6c  ache table level
14f54 20 6c 6f 63 6b 73 2e 20 49 66 20 74 68 65 20 6c   locks. If the l
14f55 69 62 72 61 72 79 20 69 73 20 63 6f 6d 70 69 6c  ibrary is compil
14f56 65 64 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a  ed with the.  **
14f57 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 66 65   shared-cache fe
14f58 61 74 75 72 65 20 64 69 73 61 62 6c 65 64 2c 20  ature disabled, 
14f59 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 6f 6e  then there is on
14f5a 6c 79 20 65 76 65 72 20 6f 6e 65 20 75 73 65 72  ly ever one user
14f5b 0a 20 20 2a 2a 20 6f 66 20 65 61 63 68 20 42 74  .  ** of each Bt
14f5c 53 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65  Shared structure
14f5d 20 61 6e 64 20 73 6f 20 74 68 69 73 20 6c 6f 63   and so this loc
14f5e 6b 69 6e 67 20 69 73 20 6e 6f 74 20 6e 65 63 65  king is not nece
14f5f 73 73 61 72 79 2e 20 0a 20 20 2a 2a 20 53 6f 20  ssary. .  ** So 
14f60 64 65 66 69 6e 65 20 74 68 65 20 6c 6f 63 6b 20  define the lock 
14f61 72 65 6c 61 74 65 64 20 66 75 6e 63 74 69 6f 6e  related function
14f62 73 20 61 73 20 6e 6f 2d 6f 70 73 2e 0a 20 20 2a  s as no-ops..  *
14f63 2f 0a 20 20 23 64 65 66 69 6e 65 20 71 75 65 72  /.  #define quer
14f64 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  ySharedCacheTabl
14f65 65 4c 6f 63 6b 28 61 2c 62 2c 63 29 20 53 51 4c  eLock(a,b,c) SQL
14f66 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65  ITE_OK.  #define
14f67 20 73 65 74 53 68 61 72 65 64 43 61 63 68 65 54   setSharedCacheT
14f68 61 62 6c 65 4c 6f 63 6b 28 61 2c 62 2c 63 29 20  ableLock(a,b,c) 
14f69 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66  SQLITE_OK.  #def
14f6a 69 6e 65 20 63 6c 65 61 72 41 6c 6c 53 68 61 72  ine clearAllShar
14f6b 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b  edCacheTableLock
14f6c 73 28 61 29 0a 20 20 23 64 65 66 69 6e 65 20 64  s(a).  #define d
14f6d 6f 77 6e 67 72 61 64 65 41 6c 6c 53 68 61 72 65  owngradeAllShare
14f6e 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73  dCacheTableLocks
14f6f 28 61 29 0a 20 20 23 64 65 66 69 6e 65 20 68 61  (a).  #define ha
14f70 73 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  sSharedCacheTabl
14f71 65 4c 6f 63 6b 28 61 2c 62 2c 63 2c 64 29 20 31  eLock(a,b,c,d) 1
14f72 0a 20 20 23 64 65 66 69 6e 65 20 68 61 73 52 65  .  #define hasRe
14f73 61 64 43 6f 6e 66 6c 69 63 74 73 28 61 2c 20 62  adConflicts(a, b
14f74 29 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e  ) 0.#endif..#ifn
14f75 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
14f76 53 48 41 52 45 44 5f 43 41 43 48 45 0a 0a 23 69  SHARED_CACHE..#i
14f77 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
14f78 47 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  G./*.** This fun
14f79 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73  ction is only us
14f7a 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61 6e  ed as part of an
14f7b 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d   assert() statem
14f7c 65 6e 74 2e 20 49 74 20 63 68 65 63 6b 73 0a 2a  ent. It checks.*
14f7d 2a 20 74 68 61 74 20 63 6f 6e 6e 65 63 74 69 6f  * that connectio
14f7e 6e 20 70 20 68 6f 6c 64 73 20 74 68 65 20 72 65  n p holds the re
14f7f 71 75 69 72 65 64 20 6c 6f 63 6b 73 20 74 6f 20  quired locks to 
14f80 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 6f  read or write to
14f81 20 74 68 65 20 0a 2a 2a 20 62 2d 74 72 65 65 20   the .** b-tree 
14f82 77 69 74 68 20 72 6f 6f 74 20 70 61 67 65 20 69  with root page i
14f83 52 6f 6f 74 2e 20 49 66 20 73 6f 2c 20 74 72 75  Root. If so, tru
14f84 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f  e is returned. O
14f85 74 68 65 72 77 69 73 65 2c 20 66 61 6c 73 65 2e  therwise, false.
14f86 20 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65   .** For example
14f87 2c 20 77 68 65 6e 20 77 72 69 74 69 6e 67 20 74  , when writing t
14f88 6f 20 61 20 74 61 62 6c 65 20 62 2d 74 72 65 65  o a table b-tree
14f89 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20   with root-page 
14f8a 69 52 6f 6f 74 20 76 69 61 20 0a 2a 2a 20 42 74  iRoot via .** Bt
14f8b 72 65 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70  ree connection p
14f8c 42 74 72 65 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  Btree:.**.**    
14f8d 61 73 73 65 72 74 28 20 68 61 73 53 68 61 72 65  assert( hasShare
14f8e 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28  dCacheTableLock(
14f8f 70 42 74 72 65 65 2c 20 69 52 6f 6f 74 2c 20 30  pBtree, iRoot, 0
14f90 2c 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 29 3b  , WRITE_LOCK) );
14f91 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 77 72 69 74  .**.** When writ
14f92 69 6e 67 20 74 6f 20 61 6e 20 69 6e 64 65 78 20  ing to an index 
14f93 62 2d 74 72 65 65 20 74 68 61 74 20 72 65 73 69  b-tree that resi
14f94 64 65 73 20 69 6e 20 61 20 73 68 61 72 61 62 6c  des in a sharabl
14f95 65 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 20  e database, the 
14f96 0a 2a 2a 20 63 61 6c 6c 65 72 20 73 68 6f 75 6c  .** caller shoul
14f97 64 20 68 61 76 65 20 66 69 72 73 74 20 6f 62 74  d have first obt
14f98 61 69 6e 65 64 20 61 20 6c 6f 63 6b 20 73 70 65  ained a lock spe
14f99 63 69 66 79 69 6e 67 20 74 68 65 20 72 6f 6f 74  cifying the root
14f9a 20 70 61 67 65 20 6f 66 0a 2a 2a 20 74 68 65 20   page of.** the 
14f9b 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 61  corresponding ta
14f9c 62 6c 65 20 62 2d 74 72 65 65 2e 20 54 68 69 73  ble b-tree. This
14f9d 20 6d 61 6b 65 73 20 74 68 69 6e 67 73 20 61 20   makes things a 
14f9e 62 69 74 20 6d 6f 72 65 20 63 6f 6d 70 6c 69 63  bit more complic
14f9f 61 74 65 64 2c 0a 2a 2a 20 61 73 20 74 68 69 73  ated,.** as this
14fa0 20 6d 6f 64 75 6c 65 20 74 72 65 61 74 73 20 65   module treats e
14fa1 61 63 68 20 62 2d 74 72 65 65 20 61 73 20 61 20  ach b-tree as a 
14fa2 73 65 70 61 72 61 74 65 20 73 74 72 75 63 74 75  separate structu
14fa3 72 65 2e 20 54 6f 20 64 65 74 65 72 6d 69 6e 65  re. To determine
14fa4 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 62 2d  .** the table b-
14fa5 74 72 65 65 20 63 6f 72 72 65 73 70 6f 6e 64 69  tree correspondi
14fa6 6e 67 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20  ng to the index 
14fa7 62 2d 74 72 65 65 20 62 65 69 6e 67 20 77 72 69  b-tree being wri
14fa8 74 74 65 6e 2c 20 74 68 69 73 0a 2a 2a 20 66 75  tten, this.** fu
14fa9 6e 63 74 69 6f 6e 20 68 61 73 20 74 6f 20 73 65  nction has to se
14faa 61 72 63 68 20 74 68 72 6f 75 67 68 20 74 68 65  arch through the
14fab 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
14fac 2e 0a 2a 2a 0a 2a 2a 20 49 6e 73 74 65 61 64 20  ..**.** Instead 
14fad 6f 66 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  of a lock on the
14fae 20 62 2d 74 72 65 65 20 72 6f 6f 74 65 64 20 61   b-tree rooted a
14faf 74 20 70 61 67 65 20 69 52 6f 6f 74 2c 20 74 68  t page iRoot, th
14fb0 65 20 63 61 6c 6c 65 72 20 6d 61 79 0a 2a 2a 20  e caller may.** 
14fb1 68 6f 6c 64 20 61 20 77 72 69 74 65 2d 6c 6f 63  hold a write-loc
14fb2 6b 20 6f 6e 20 74 68 65 20 73 63 68 65 6d 61 20  k on the schema 
14fb3 74 61 62 6c 65 20 28 72 6f 6f 74 20 70 61 67 65  table (root page
14fb4 20 31 29 2e 20 54 68 69 73 20 69 73 20 61 6c 73   1). This is als
14fb5 6f 0a 2a 2a 20 61 63 63 65 70 74 61 62 6c 65 2e  o.** acceptable.
14fb6 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 68  .*/.static int h
14fb7 61 73 53 68 61 72 65 64 43 61 63 68 65 54 61 62  asSharedCacheTab
14fb8 6c 65 4c 6f 63 6b 28 0a 20 20 42 74 72 65 65 20  leLock(.  Btree 
14fb9 2a 70 42 74 72 65 65 2c 20 20 20 20 20 20 20 20  *pBtree,        
14fba 20 2f 2a 20 48 61 6e 64 6c 65 20 74 68 61 74 20   /* Handle that 
14fbb 6d 75 73 74 20 68 6f 6c 64 20 6c 6f 63 6b 20 2a  must hold lock *
14fbc 2f 0a 20 20 50 67 6e 6f 20 69 52 6f 6f 74 2c 20  /.  Pgno iRoot, 
14fbd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f             /* Ro
14fbe 6f 74 20 70 61 67 65 20 6f 66 20 62 2d 74 72 65  ot page of b-tre
14fbf 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 49 6e 64  e */.  int isInd
14fc0 65 78 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ex,           /*
14fc1 20 54 72 75 65 20 69 66 20 69 52 6f 6f 74 20 69   True if iRoot i
14fc2 73 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 61 6e  s the root of an
14fc3 20 69 6e 64 65 78 20 62 2d 74 72 65 65 20 2a 2f   index b-tree */
14fc4 0a 20 20 69 6e 74 20 65 4c 6f 63 6b 54 79 70 65  .  int eLockType
14fc5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 71            /* Req
14fc6 75 69 72 65 64 20 6c 6f 63 6b 20 74 79 70 65 20  uired lock type 
14fc7 28 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 57 52  (READ_LOCK or WR
14fc8 49 54 45 5f 4c 4f 43 4b 29 20 2a 2f 0a 29 7b 0a  ITE_LOCK) */.){.
14fc9 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d    Schema *pSchem
14fca 61 20 3d 20 28 53 63 68 65 6d 61 20 2a 29 70 42  a = (Schema *)pB
14fcb 74 72 65 65 2d 3e 70 42 74 2d 3e 70 53 63 68 65  tree->pBt->pSche
14fcc 6d 61 3b 0a 20 20 50 67 6e 6f 20 69 54 61 62 20  ma;.  Pgno iTab 
14fcd 3d 20 30 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 70  = 0;.  BtLock *p
14fce 4c 6f 63 6b 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  Lock;..  /* If t
14fcf 68 69 73 20 62 2d 74 72 65 65 20 64 61 74 61 62  his b-tree datab
14fd0 61 73 65 20 69 73 20 6e 6f 74 20 73 68 61 72 65  ase is not share
14fd1 61 62 6c 65 2c 20 6f 72 20 69 66 20 74 68 65 20  able, or if the 
14fd2 63 6c 69 65 6e 74 20 69 73 20 72 65 61 64 69 6e  client is readin
14fd3 67 0a 20 20 2a 2a 20 61 6e 64 20 68 61 73 20 74  g.  ** and has t
14fd4 68 65 20 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74  he read-uncommit
14fd5 74 65 64 20 66 6c 61 67 20 73 65 74 2c 20 74 68  ted flag set, th
14fd6 65 6e 20 6e 6f 20 6c 6f 63 6b 20 69 73 20 72 65  en no lock is re
14fd7 71 75 69 72 65 64 2e 20 0a 20 20 2a 2a 20 49 6e  quired. .  ** In
14fd8 20 74 68 65 73 65 20 63 61 73 65 73 20 72 65 74   these cases ret
14fd9 75 72 6e 20 74 72 75 65 20 69 6d 6d 65 64 69 61  urn true immedia
14fda 74 65 6c 79 2e 20 20 49 66 20 74 68 65 20 63 6c  tely.  If the cl
14fdb 69 65 6e 74 20 69 73 20 72 65 61 64 69 6e 67 20  ient is reading 
14fdc 0a 20 20 2a 2a 20 6f 72 20 77 72 69 74 69 6e 67  .  ** or writing
14fdd 20 61 6e 20 69 6e 64 65 78 20 62 2d 74 72 65 65   an index b-tree
14fde 2c 20 62 75 74 20 74 68 65 20 73 63 68 65 6d 61  , but the schema
14fdf 20 69 73 20 6e 6f 74 20 6c 6f 61 64 65 64 2c 20   is not loaded, 
14fe0 74 68 65 6e 20 72 65 74 75 72 6e 0a 20 20 2a 2a  then return.  **
14fe1 20 74 72 75 65 20 61 6c 73 6f 2e 20 49 6e 20 74   true also. In t
14fe2 68 69 73 20 63 61 73 65 20 74 68 65 20 6c 6f 63  his case the loc
14fe3 6b 20 69 73 20 72 65 71 75 69 72 65 64 2c 20 62  k is required, b
14fe4 75 74 20 69 74 20 69 73 20 74 6f 6f 20 64 69 66  ut it is too dif
14fe5 66 69 63 75 6c 74 0a 20 20 2a 2a 20 74 6f 20 63  ficult.  ** to c
14fe6 68 65 63 6b 20 69 66 20 74 68 65 20 63 6c 69 65  heck if the clie
14fe7 6e 74 20 61 63 74 75 61 6c 6c 79 20 68 6f 6c 64  nt actually hold
14fe8 73 20 69 74 2e 20 54 68 69 73 20 64 6f 65 73 6e  s it. This doesn
14fe9 27 74 20 68 61 70 70 65 6e 20 76 65 72 79 0a 20  't happen very. 
14fea 20 2a 2a 20 6f 66 74 65 6e 2e 20 20 2a 2f 0a 20   ** often.  */. 
14feb 20 69 66 28 20 28 70 42 74 72 65 65 2d 3e 73 68   if( (pBtree->sh
14fec 61 72 61 62 6c 65 3d 3d 30 29 0a 20 20 20 7c 7c  arable==0).   ||
14fed 20 28 65 4c 6f 63 6b 54 79 70 65 3d 3d 52 45 41   (eLockType==REA
14fee 44 5f 4c 4f 43 4b 20 26 26 20 28 70 42 74 72 65  D_LOCK && (pBtre
14fef 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53  e->db->flags & S
14ff0 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d  QLITE_ReadUncomm
14ff1 69 74 74 65 64 29 29 0a 20 20 20 7c 7c 20 28 69  itted)).   || (i
14ff2 73 49 6e 64 65 78 20 26 26 20 28 21 70 53 63 68  sIndex && (!pSch
14ff3 65 6d 61 20 7c 7c 20 28 70 53 63 68 65 6d 61 2d  ema || (pSchema-
14ff4 3e 66 6c 61 67 73 26 44 42 5f 53 63 68 65 6d 61  >flags&DB_Schema
14ff5 4c 6f 61 64 65 64 29 3d 3d 30 20 29 29 0a 20 20  Loaded)==0 )).  
14ff6 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  ){.    return 1;
14ff7 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72  .  }..  /* Figur
14ff8 65 20 6f 75 74 20 74 68 65 20 72 6f 6f 74 2d 70  e out the root-p
14ff9 61 67 65 20 74 68 61 74 20 74 68 65 20 6c 6f 63  age that the loc
14ffa 6b 20 73 68 6f 75 6c 64 20 62 65 20 68 65 6c 64  k should be held
14ffb 20 6f 6e 2e 20 46 6f 72 20 74 61 62 6c 65 0a 20   on. For table. 
14ffc 20 2a 2a 20 62 2d 74 72 65 65 73 2c 20 74 68 69   ** b-trees, thi
14ffd 73 20 69 73 20 6a 75 73 74 20 74 68 65 20 72 6f  s is just the ro
14ffe 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 62  ot page of the b
14fff 2d 74 72 65 65 20 62 65 69 6e 67 20 72 65 61 64  -tree being read
15000 20 6f 72 0a 20 20 2a 2a 20 77 72 69 74 74 65 6e   or.  ** written
15001 2e 20 46 6f 72 20 69 6e 64 65 78 20 62 2d 74 72  . For index b-tr
15002 65 65 73 2c 20 69 74 20 69 73 20 74 68 65 20 72  ees, it is the r
15003 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20  oot page of the 
15004 61 73 73 6f 63 69 61 74 65 64 0a 20 20 2a 2a 20  associated.  ** 
15005 74 61 62 6c 65 2e 20 20 2a 2f 0a 20 20 69 66 28  table.  */.  if(
15006 20 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 20   isIndex ){.    
15007 48 61 73 68 45 6c 65 6d 20 2a 70 3b 0a 20 20 20  HashElem *p;.   
15008 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 48 61 73   for(p=sqliteHas
15009 68 46 69 72 73 74 28 26 70 53 63 68 65 6d 61 2d  hFirst(&pSchema-
1500a 3e 69 64 78 48 61 73 68 29 3b 20 70 3b 20 70 3d  >idxHash); p; p=
1500b 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70  sqliteHashNext(p
1500c 29 29 7b 0a 20 20 20 20 20 20 49 6e 64 65 78 20  )){.      Index 
1500d 2a 70 49 64 78 20 3d 20 28 49 6e 64 65 78 20 2a  *pIdx = (Index *
1500e 29 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28  )sqliteHashData(
1500f 70 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49  p);.      if( pI
15010 64 78 2d 3e 74 6e 75 6d 3d 3d 28 69 6e 74 29 69  dx->tnum==(int)i
15011 52 6f 6f 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Root ){.        
15012 69 54 61 62 20 3d 20 70 49 64 78 2d 3e 70 54 61  iTab = pIdx->pTa
15013 62 6c 65 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20 20  ble->tnum;.     
15014 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65   }.    }.  }else
15015 7b 0a 20 20 20 20 69 54 61 62 20 3d 20 69 52 6f  {.    iTab = iRo
15016 6f 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65  ot;.  }..  /* Se
15017 61 72 63 68 20 66 6f 72 20 74 68 65 20 72 65 71  arch for the req
15018 75 69 72 65 64 20 6c 6f 63 6b 2e 20 45 69 74 68  uired lock. Eith
15019 65 72 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20  er a write-lock 
1501a 6f 6e 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61  on root-page iTa
1501b 62 2c 20 61 20 0a 20 20 2a 2a 20 77 72 69 74 65  b, a .  ** write
1501c 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 73 63 68  -lock on the sch
1501d 65 6d 61 20 74 61 62 6c 65 2c 20 6f 72 20 28 69  ema table, or (i
1501e 66 20 74 68 65 20 63 6c 69 65 6e 74 20 69 73 20  f the client is 
1501f 72 65 61 64 69 6e 67 29 20 61 0a 20 20 2a 2a 20  reading) a.  ** 
15020 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 69 54 61  read-lock on iTa
15021 62 20 77 69 6c 6c 20 73 75 66 66 69 63 65 2e 20  b will suffice. 
15022 52 65 74 75 72 6e 20 31 20 69 66 20 61 6e 79 20  Return 1 if any 
15023 6f 66 20 74 68 65 73 65 20 61 72 65 20 66 6f 75  of these are fou
15024 6e 64 2e 20 20 2a 2f 0a 20 20 66 6f 72 28 70 4c  nd.  */.  for(pL
15025 6f 63 6b 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d  ock=pBtree->pBt-
15026 3e 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b 3b 20 70  >pLock; pLock; p
15027 4c 6f 63 6b 3d 70 4c 6f 63 6b 2d 3e 70 4e 65 78  Lock=pLock->pNex
15028 74 29 7b 0a 20 20 20 20 69 66 28 20 70 4c 6f 63  t){.    if( pLoc
15029 6b 2d 3e 70 42 74 72 65 65 3d 3d 70 42 74 72 65  k->pBtree==pBtre
1502a 65 20 0a 20 20 20 20 20 26 26 20 28 70 4c 6f 63  e .     && (pLoc
1502b 6b 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 20  k->iTable==iTab 
1502c 7c 7c 20 28 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b  || (pLock->eLock
1502d 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 26 26 20  ==WRITE_LOCK && 
1502e 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65 3d 3d 31  pLock->iTable==1
1502f 29 29 0a 20 20 20 20 20 26 26 20 70 4c 6f 63 6b  )).     && pLock
15030 2d 3e 65 4c 6f 63 6b 3e 3d 65 4c 6f 63 6b 54 79  ->eLock>=eLockTy
15031 70 65 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  pe .    ){.     
15032 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
15033 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 61 69 6c 65  .  }..  /* Faile
15034 64 20 74 6f 20 66 69 6e 64 20 74 68 65 20 72 65  d to find the re
15035 71 75 69 72 65 64 20 6c 6f 63 6b 2e 20 2a 2f 0a  quired lock. */.
15036 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
15037 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
15038 6f 6e 20 69 73 20 61 6c 73 6f 20 75 73 65 64 20  on is also used 
15039 61 73 20 70 61 72 74 20 6f 66 20 61 73 73 65 72  as part of asser
1503a 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 6f  t() statements o
1503b 6e 6c 79 2e 20 49 74 20 0a 2a 2a 20 72 65 74 75  nly. It .** retu
1503c 72 6e 73 20 74 72 75 65 20 69 66 20 74 68 65 72  rns true if ther
1503d 65 20 65 78 69 73 74 20 6f 6e 65 20 6f 72 20 6d  e exist one or m
1503e 6f 72 65 20 63 75 72 73 6f 72 73 20 6f 70 65 6e  ore cursors open
1503f 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 0a 2a   on the table .*
15040 2a 20 77 69 74 68 20 72 6f 6f 74 20 70 61 67 65  * with root page
15041 20 69 52 6f 6f 74 20 74 68 61 74 20 64 6f 20 6e   iRoot that do n
15042 6f 74 20 62 65 6c 6f 6e 67 20 74 6f 20 65 69 74  ot belong to eit
15043 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70  her connection p
15044 42 74 72 65 65 20 0a 2a 2a 20 6f 72 20 73 6f 6d  Btree .** or som
15045 65 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69  e other connecti
15046 6f 6e 20 74 68 61 74 20 68 61 73 20 74 68 65 20  on that has the 
15047 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74 74 65 64  read-uncommitted
15048 20 66 6c 61 67 20 73 65 74 2e 0a 2a 2a 0a 2a 2a   flag set..**.**
15049 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 62 65   For example, be
1504a 66 6f 72 65 20 77 72 69 74 69 6e 67 20 74 6f 20  fore writing to 
1504b 70 61 67 65 20 69 52 6f 6f 74 3a 0a 2a 2a 0a 2a  page iRoot:.**.*
1504c 2a 20 20 20 20 61 73 73 65 72 74 28 20 21 68 61  *    assert( !ha
1504d 73 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 70  sReadConflicts(p
1504e 42 74 72 65 65 2c 20 69 52 6f 6f 74 29 20 29 3b  Btree, iRoot) );
1504f 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 68  .*/.static int h
15050 61 73 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28  asReadConflicts(
15051 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20 50  Btree *pBtree, P
15052 67 6e 6f 20 69 52 6f 6f 74 29 7b 0a 20 20 42 74  gno iRoot){.  Bt
15053 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 66 6f 72  Cursor *p;.  for
15054 28 70 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d 3e  (p=pBtree->pBt->
15055 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d  pCursor; p; p=p-
15056 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28  >pNext){.    if(
15057 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 69 52   p->pgnoRoot==iR
15058 6f 6f 74 20 0a 20 20 20 20 20 26 26 20 70 2d 3e  oot .     && p->
15059 70 42 74 72 65 65 21 3d 70 42 74 72 65 65 0a 20  pBtree!=pBtree. 
1505a 20 20 20 20 26 26 20 30 3d 3d 28 70 2d 3e 70 42      && 0==(p->pB
1505b 74 72 65 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20  tree->db->flags 
1505c 26 20 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63  & SQLITE_ReadUnc
1505d 6f 6d 6d 69 74 74 65 64 29 0a 20 20 20 20 29 7b  ommitted).    ){
1505e 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
1505f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
15060 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20  urn 0;.}.#endif 
15061 20 20 20 2f 2a 20 23 69 66 64 65 66 20 53 51 4c     /* #ifdef SQL
15062 49 54 45 5f 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a  ITE_DEBUG */../*
15063 0a 2a 2a 20 51 75 65 72 79 20 74 6f 20 73 65 65  .** Query to see
15064 20 69 66 20 62 74 72 65 65 20 68 61 6e 64 6c 65   if btree handle
15065 20 70 20 6d 61 79 20 6f 62 74 61 69 6e 20 61 20   p may obtain a 
15066 6c 6f 63 6b 20 6f 66 20 74 79 70 65 20 65 4c 6f  lock of type eLo
15067 63 6b 20 0a 2a 2a 20 28 52 45 41 44 5f 4c 4f 43  ck .** (READ_LOC
15068 4b 20 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b 29  K or WRITE_LOCK)
15069 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69   on the table wi
1506a 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61  th root-page iTa
1506b 62 2e 20 52 65 74 75 72 6e 0a 2a 2a 20 53 51 4c  b. Return.** SQL
1506c 49 54 45 5f 4f 4b 20 69 66 20 74 68 65 20 6c 6f  ITE_OK if the lo
1506d 63 6b 20 6d 61 79 20 62 65 20 6f 62 74 61 69 6e  ck may be obtain
1506e 65 64 20 28 62 79 20 63 61 6c 6c 69 6e 67 0a 2a  ed (by calling.*
1506f 2a 20 73 65 74 53 68 61 72 65 64 43 61 63 68 65  * setSharedCache
15070 54 61 62 6c 65 4c 6f 63 6b 28 29 29 2c 20 6f 72  TableLock()), or
15071 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69   SQLITE_LOCKED i
15072 66 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  f not..*/.static
15073 20 69 6e 74 20 71 75 65 72 79 53 68 61 72 65 64   int queryShared
15074 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 42  CacheTableLock(B
15075 74 72 65 65 20 2a 70 2c 20 50 67 6e 6f 20 69 54  tree *p, Pgno iT
15076 61 62 2c 20 75 38 20 65 4c 6f 63 6b 29 7b 0a 20  ab, u8 eLock){. 
15077 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
15078 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74 4c 6f 63   p->pBt;.  BtLoc
15079 6b 20 2a 70 49 74 65 72 3b 0a 0a 20 20 61 73 73  k *pIter;..  ass
1507a 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
1507b 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29  eHoldsMutex(p) )
1507c 3b 0a 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63  ;.  assert( eLoc
1507d 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20  k==READ_LOCK || 
1507e 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43  eLock==WRITE_LOC
1507f 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  K );.  assert( p
15080 2d 3e 64 62 21 3d 30 20 29 3b 0a 20 20 61 73 73  ->db!=0 );.  ass
15081 65 72 74 28 20 21 28 70 2d 3e 64 62 2d 3e 66 6c  ert( !(p->db->fl
15082 61 67 73 26 53 51 4c 49 54 45 5f 52 65 61 64 55  ags&SQLITE_ReadU
15083 6e 63 6f 6d 6d 69 74 74 65 64 29 7c 7c 65 4c 6f  ncommitted)||eLo
15084 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 7c 7c  ck==WRITE_LOCK||
15085 69 54 61 62 3d 3d 31 20 29 3b 0a 20 20 0a 20 20  iTab==1 );.  .  
15086 2f 2a 20 49 66 20 72 65 71 75 65 73 74 69 6e 67  /* If requesting
15087 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 2c 20 74   a write-lock, t
15088 68 65 6e 20 74 68 65 20 42 74 72 65 65 20 6d 75  hen the Btree mu
15089 73 74 20 68 61 76 65 20 61 6e 20 6f 70 65 6e 20  st have an open 
1508a 77 72 69 74 65 0a 20 20 2a 2a 20 74 72 61 6e 73  write.  ** trans
1508b 61 63 74 69 6f 6e 20 6f 6e 20 74 68 69 73 20 66  action on this f
1508c 69 6c 65 2e 20 41 6e 64 2c 20 6f 62 76 69 6f 75  ile. And, obviou
1508d 73 6c 79 2c 20 66 6f 72 20 74 68 69 73 20 74 6f  sly, for this to
1508e 20 62 65 20 73 6f 20 74 68 65 72 65 20 0a 20 20   be so there .  
1508f 2a 2a 20 6d 75 73 74 20 62 65 20 61 6e 20 6f 70  ** must be an op
15090 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73 61 63  en write transac
15091 74 69 6f 6e 20 6f 6e 20 74 68 65 20 66 69 6c 65  tion on the file
15092 20 69 74 73 65 6c 66 2e 0a 20 20 2a 2f 0a 20 20   itself..  */.  
15093 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52  assert( eLock==R
15094 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 28 70 3d 3d  EAD_LOCK || (p==
15095 70 42 74 2d 3e 70 57 72 69 74 65 72 20 26 26 20  pBt->pWriter && 
15096 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e  p->inTrans==TRAN
15097 53 5f 57 52 49 54 45 29 20 29 3b 0a 20 20 61 73  S_WRITE) );.  as
15098 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 41  sert( eLock==REA
15099 44 5f 4c 4f 43 4b 20 7c 7c 20 70 42 74 2d 3e 69  D_LOCK || pBt->i
1509a 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52  nTransaction==TR
1509b 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 0a  ANS_WRITE );.  .
1509c 20 20 2f 2a 20 54 68 69 73 20 69 73 20 61 20 6e    /* This is a n
1509d 6f 2d 6f 70 20 69 66 20 74 68 65 20 73 68 61 72  o-op if the shar
1509e 65 64 2d 63 61 63 68 65 20 69 73 20 6e 6f 74 20  ed-cache is not 
1509f 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 69 66 28  enabled */.  if(
150a0 20 21 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b   !p->sharable ){
150a1 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
150a2 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  TE_OK;.  }..  /*
150a3 20 49 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 63   If some other c
150a4 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 68 6f 6c  onnection is hol
150a5 64 69 6e 67 20 61 6e 20 65 78 63 6c 75 73 69 76  ding an exclusiv
150a6 65 20 6c 6f 63 6b 2c 20 74 68 65 0a 20 20 2a 2a  e lock, the.  **
150a7 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 20   requested lock 
150a8 6d 61 79 20 6e 6f 74 20 62 65 20 6f 62 74 61 69  may not be obtai
150a9 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ned..  */.  if( 
150aa 70 42 74 2d 3e 70 57 72 69 74 65 72 21 3d 70 20  pBt->pWriter!=p 
150ab 26 26 20 70 42 74 2d 3e 69 73 45 78 63 6c 75 73  && pBt->isExclus
150ac 69 76 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ive ){.    sqlit
150ad 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63  e3ConnectionBloc
150ae 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42 74 2d 3e  ked(p->db, pBt->
150af 70 57 72 69 74 65 72 2d 3e 64 62 29 3b 0a 20 20  pWriter->db);.  
150b0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
150b1 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43  LOCKED_SHAREDCAC
150b2 48 45 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 70  HE;.  }..  for(p
150b3 49 74 65 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b  Iter=pBt->pLock;
150b4 20 70 49 74 65 72 3b 20 70 49 74 65 72 3d 70 49   pIter; pIter=pI
150b5 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  ter->pNext){.   
150b6 20 2f 2a 20 54 68 65 20 63 6f 6e 64 69 74 69 6f   /* The conditio
150b7 6e 20 28 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 21  n (pIter->eLock!
150b8 3d 65 4c 6f 63 6b 29 20 69 6e 20 74 68 65 20 66  =eLock) in the f
150b9 6f 6c 6c 6f 77 69 6e 67 20 69 66 28 2e 2e 2e 29  ollowing if(...)
150ba 20 0a 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65   .    ** stateme
150bb 6e 74 20 69 73 20 61 20 73 69 6d 70 6c 69 66 69  nt is a simplifi
150bc 63 61 74 69 6f 6e 20 6f 66 3a 0a 20 20 20 20 2a  cation of:.    *
150bd 2a 0a 20 20 20 20 2a 2a 20 20 20 28 65 4c 6f 63  *.    **   (eLoc
150be 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 7c 7c  k==WRITE_LOCK ||
150bf 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 57   pIter->eLock==W
150c0 52 49 54 45 5f 4c 4f 43 4b 29 0a 20 20 20 20 2a  RITE_LOCK).    *
150c1 2a 0a 20 20 20 20 2a 2a 20 73 69 6e 63 65 20 77  *.    ** since w
150c2 65 20 6b 6e 6f 77 20 74 68 61 74 20 69 66 20 65  e know that if e
150c3 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b  Lock==WRITE_LOCK
150c4 2c 20 74 68 65 6e 20 6e 6f 20 6f 74 68 65 72 20  , then no other 
150c5 63 6f 6e 6e 65 63 74 69 6f 6e 0a 20 20 20 20 2a  connection.    *
150c6 2a 20 6d 61 79 20 68 6f 6c 64 20 61 20 57 52 49  * may hold a WRI
150c7 54 45 5f 4c 4f 43 4b 20 6f 6e 20 61 6e 79 20 74  TE_LOCK on any t
150c8 61 62 6c 65 20 69 6e 20 74 68 69 73 20 66 69 6c  able in this fil
150c9 65 20 28 73 69 6e 63 65 20 74 68 65 72 65 20 63  e (since there c
150ca 61 6e 0a 20 20 20 20 2a 2a 20 6f 6e 6c 79 20 62  an.    ** only b
150cb 65 20 61 20 73 69 6e 67 6c 65 20 77 72 69 74 65  e a single write
150cc 72 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61  r)..    */.    a
150cd 73 73 65 72 74 28 20 70 49 74 65 72 2d 3e 65 4c  ssert( pIter->eL
150ce 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c  ock==READ_LOCK |
150cf 7c 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d  | pIter->eLock==
150d0 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20  WRITE_LOCK );.  
150d1 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d    assert( eLock=
150d2 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 49  =READ_LOCK || pI
150d3 74 65 72 2d 3e 70 42 74 72 65 65 3d 3d 70 20 7c  ter->pBtree==p |
150d4 7c 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d 3d  | pIter->eLock==
150d5 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20  READ_LOCK);.    
150d6 69 66 28 20 70 49 74 65 72 2d 3e 70 42 74 72 65  if( pIter->pBtre
150d7 65 21 3d 70 20 26 26 20 70 49 74 65 72 2d 3e 69  e!=p && pIter->i
150d8 54 61 62 6c 65 3d 3d 69 54 61 62 20 26 26 20 70  Table==iTab && p
150d9 49 74 65 72 2d 3e 65 4c 6f 63 6b 21 3d 65 4c 6f  Iter->eLock!=eLo
150da 63 6b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ck ){.      sqli
150db 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f  te3ConnectionBlo
150dc 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 49 74 65  cked(p->db, pIte
150dd 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 29 3b 0a  r->pBtree->db);.
150de 20 20 20 20 20 20 69 66 28 20 65 4c 6f 63 6b 3d        if( eLock=
150df 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 7b 0a 20  =WRITE_LOCK ){. 
150e0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
150e1 3d 3d 70 42 74 2d 3e 70 57 72 69 74 65 72 20 29  ==pBt->pWriter )
150e2 3b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e 69  ;.        pBt->i
150e3 73 50 65 6e 64 69 6e 67 20 3d 20 31 3b 0a 20 20  sPending = 1;.  
150e4 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75      }.      retu
150e5 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44  rn SQLITE_LOCKED
150e6 5f 53 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20  _SHAREDCACHE;.  
150e7 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
150e8 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65   SQLITE_OK;.}.#e
150e9 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f  ndif /* !SQLITE_
150ea 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
150eb 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  E */..#ifndef SQ
150ec 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
150ed 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 41 64 64  _CACHE./*.** Add
150ee 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74   a lock on the t
150ef 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d 70  able with root-p
150f0 61 67 65 20 69 54 61 62 6c 65 20 74 6f 20 74 68  age iTable to th
150f1 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 20 75  e shared-btree u
150f2 73 65 64 0a 2a 2a 20 62 79 20 42 74 72 65 65 20  sed.** by Btree 
150f3 68 61 6e 64 6c 65 20 70 2e 20 50 61 72 61 6d 65  handle p. Parame
150f4 74 65 72 20 65 4c 6f 63 6b 20 6d 75 73 74 20 62  ter eLock must b
150f5 65 20 65 69 74 68 65 72 20 52 45 41 44 5f 4c 4f  e either READ_LO
150f6 43 4b 20 6f 72 20 0a 2a 2a 20 57 52 49 54 45 5f  CK or .** WRITE_
150f7 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  LOCK..**.** This
150f8 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65   function assume
150f9 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  s the following:
150fa 0a 2a 2a 0a 2a 2a 20 20 20 28 61 29 20 54 68 65  .**.**   (a) The
150fb 20 73 70 65 63 69 66 69 65 64 20 62 2d 74 72 65   specified b-tre
150fc 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e  e connection han
150fd 64 6c 65 20 69 73 20 63 6f 6e 6e 65 63 74 65 64  dle is connected
150fe 20 74 6f 20 61 20 73 68 61 72 61 62 6c 65 0a 2a   to a sharable.*
150ff 2a 20 20 20 20 20 20 20 62 2d 74 72 65 65 20 64  *       b-tree d
15100 61 74 61 62 61 73 65 20 28 6f 6e 65 20 77 69 74  atabase (one wit
15101 68 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 73  h the BtShared.s
15102 68 61 72 61 62 6c 65 29 20 66 6c 61 67 20 73 65  harable) flag se
15103 74 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 28  t, and.**.**   (
15104 62 29 20 4e 6f 20 6f 74 68 65 72 20 62 2d 74 72  b) No other b-tr
15105 65 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61  ee connection ha
15106 6e 64 6c 65 20 68 6f 6c 64 73 20 61 20 6c 6f 63  ndle holds a loc
15107 6b 20 74 68 61 74 20 63 6f 6e 66 6c 69 63 74 73  k that conflicts
15108 0a 2a 2a 20 20 20 20 20 20 20 77 69 74 68 20 74  .**       with t
15109 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63  he requested loc
1510a 6b 20 28 69 2e 65 2e 20 71 75 65 72 79 53 68 61  k (i.e. querySha
1510b 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63  redCacheTableLoc
1510c 6b 28 29 20 68 61 73 0a 2a 2a 20 20 20 20 20 20  k() has.**      
1510d 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 61   already been ca
1510e 6c 6c 65 64 20 61 6e 64 20 72 65 74 75 72 6e 65  lled and returne
1510f 64 20 53 51 4c 49 54 45 5f 4f 4b 29 2e 0a 2a 2a  d SQLITE_OK)..**
15110 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  .** SQLITE_OK is
15111 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65   returned if the
15112 20 6c 6f 63 6b 20 69 73 20 61 64 64 65 64 20 73   lock is added s
15113 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 53 51 4c  uccessfully. SQL
15114 49 54 45 5f 4e 4f 4d 45 4d 20 0a 2a 2a 20 69 73  ITE_NOMEM .** is
15115 20 72 65 74 75 72 6e 65 64 20 69 66 20 61 20 6d   returned if a m
15116 61 6c 6c 6f 63 20 61 74 74 65 6d 70 74 20 66 61  alloc attempt fa
15117 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ils..*/.static i
15118 6e 74 20 73 65 74 53 68 61 72 65 64 43 61 63 68  nt setSharedCach
15119 65 54 61 62 6c 65 4c 6f 63 6b 28 42 74 72 65 65  eTableLock(Btree
1511a 20 2a 70 2c 20 50 67 6e 6f 20 69 54 61 62 6c 65   *p, Pgno iTable
1511b 2c 20 75 38 20 65 4c 6f 63 6b 29 7b 0a 20 20 42  , u8 eLock){.  B
1511c 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
1511d 2d 3e 70 42 74 3b 0a 20 20 42 74 4c 6f 63 6b 20  ->pBt;.  BtLock 
1511e 2a 70 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 42 74  *pLock = 0;.  Bt
1511f 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a 0a 20 20  Lock *pIter;..  
15120 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
15121 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70  treeHoldsMutex(p
15122 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 65  ) );.  assert( e
15123 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20  Lock==READ_LOCK 
15124 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f  || eLock==WRITE_
15125 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74  LOCK );.  assert
15126 28 20 70 2d 3e 64 62 21 3d 30 20 29 3b 0a 0a 20  ( p->db!=0 );.. 
15127 20 2f 2a 20 41 20 63 6f 6e 6e 65 63 74 69 6f 6e   /* A connection
15128 20 77 69 74 68 20 74 68 65 20 72 65 61 64 2d 75   with the read-u
15129 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 20  ncommitted flag 
1512a 73 65 74 20 77 69 6c 6c 20 6e 65 76 65 72 20 74  set will never t
1512b 72 79 20 74 6f 0a 20 20 2a 2a 20 6f 62 74 61 69  ry to.  ** obtai
1512c 6e 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 75 73  n a read-lock us
1512d 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ing this functio
1512e 6e 2e 20 54 68 65 20 6f 6e 6c 79 20 72 65 61 64  n. The only read
1512f 2d 6c 6f 63 6b 20 6f 62 74 61 69 6e 65 64 0a 20  -lock obtained. 
15130 20 2a 2a 20 62 79 20 61 20 63 6f 6e 6e 65 63 74   ** by a connect
15131 69 6f 6e 20 69 6e 20 72 65 61 64 2d 75 6e 63 6f  ion in read-unco
15132 6d 6d 69 74 74 65 64 20 6d 6f 64 65 20 69 73 20  mmitted mode is 
15133 6f 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61  on the sqlite_ma
15134 73 74 65 72 20 0a 20 20 2a 2a 20 74 61 62 6c 65  ster .  ** table
15135 2c 20 61 6e 64 20 74 68 61 74 20 6c 6f 63 6b 20  , and that lock 
15136 69 73 20 6f 62 74 61 69 6e 65 64 20 69 6e 20 42  is obtained in B
15137 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 29  treeBeginTrans()
15138 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
15139 30 3d 3d 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 73  0==(p->db->flags
1513a 26 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f  &SQLITE_ReadUnco
1513b 6d 6d 69 74 74 65 64 29 20 7c 7c 20 65 4c 6f 63  mmitted) || eLoc
1513c 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b  k==WRITE_LOCK );
1513d 0a 0a 20 20 2f 2a 20 54 68 69 73 20 66 75 6e 63  ..  /* This func
1513e 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6f 6e 6c 79  tion should only
1513f 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20   be called on a 
15140 73 68 61 72 61 62 6c 65 20 62 2d 74 72 65 65 20  sharable b-tree 
15141 61 66 74 65 72 20 69 74 20 0a 20 20 2a 2a 20 68  after it .  ** h
15142 61 73 20 62 65 65 6e 20 64 65 74 65 72 6d 69 6e  as been determin
15143 65 64 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72  ed that no other
15144 20 62 2d 74 72 65 65 20 68 6f 6c 64 73 20 61 20   b-tree holds a 
15145 63 6f 6e 66 6c 69 63 74 69 6e 67 20 6c 6f 63 6b  conflicting lock
15146 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
15147 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 3b 0a 20  p->sharable );. 
15148 20 61 73 73 65 72 74 28 20 53 51 4c 49 54 45 5f   assert( SQLITE_
15149 4f 4b 3d 3d 71 75 65 72 79 53 68 61 72 65 64 43  OK==querySharedC
1514a 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c  acheTableLock(p,
1514b 20 69 54 61 62 6c 65 2c 20 65 4c 6f 63 6b 29 20   iTable, eLock) 
1514c 29 3b 0a 0a 20 20 2f 2a 20 46 69 72 73 74 20 73  );..  /* First s
1514d 65 61 72 63 68 20 74 68 65 20 6c 69 73 74 20 66  earch the list f
1514e 6f 72 20 61 6e 20 65 78 69 73 74 69 6e 67 20 6c  or an existing l
1514f 6f 63 6b 20 6f 6e 20 74 68 69 73 20 74 61 62 6c  ock on this tabl
15150 65 2e 20 2a 2f 0a 20 20 66 6f 72 28 70 49 74 65  e. */.  for(pIte
15151 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49  r=pBt->pLock; pI
15152 74 65 72 3b 20 70 49 74 65 72 3d 70 49 74 65 72  ter; pIter=pIter
15153 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66  ->pNext){.    if
15154 28 20 70 49 74 65 72 2d 3e 69 54 61 62 6c 65 3d  ( pIter->iTable=
15155 3d 69 54 61 62 6c 65 20 26 26 20 70 49 74 65 72  =iTable && pIter
15156 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 7b 0a 20  ->pBtree==p ){. 
15157 20 20 20 20 20 70 4c 6f 63 6b 20 3d 20 70 49 74       pLock = pIt
15158 65 72 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  er;.      break;
15159 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
1515a 20 49 66 20 74 68 65 20 61 62 6f 76 65 20 73 65   If the above se
1515b 61 72 63 68 20 64 69 64 20 6e 6f 74 20 66 69 6e  arch did not fin
1515c 64 20 61 20 42 74 4c 6f 63 6b 20 73 74 72 75 63  d a BtLock struc
1515d 74 20 61 73 73 6f 63 69 61 74 69 6e 67 20 42 74  t associating Bt
1515e 72 65 65 20 70 0a 20 20 2a 2a 20 77 69 74 68 20  ree p.  ** with 
1515f 74 61 62 6c 65 20 69 54 61 62 6c 65 2c 20 61 6c  table iTable, al
15160 6c 6f 63 61 74 65 20 6f 6e 65 20 61 6e 64 20 6c  locate one and l
15161 69 6e 6b 20 69 74 20 69 6e 74 6f 20 74 68 65 20  ink it into the 
15162 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  list..  */.  if(
15163 20 21 70 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70   !pLock ){.    p
15164 4c 6f 63 6b 20 3d 20 28 42 74 4c 6f 63 6b 20 2a  Lock = (BtLock *
15165 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65  )sqlite3MallocZe
15166 72 6f 28 73 69 7a 65 6f 66 28 42 74 4c 6f 63 6b  ro(sizeof(BtLock
15167 29 29 3b 0a 20 20 20 20 69 66 28 20 21 70 4c 6f  ));.    if( !pLo
15168 63 6b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  ck ){.      retu
15169 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
1516a 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 6f 63 6b  .    }.    pLock
1516b 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54 61 62 6c  ->iTable = iTabl
1516c 65 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 42  e;.    pLock->pB
1516d 74 72 65 65 20 3d 20 70 3b 0a 20 20 20 20 70 4c  tree = p;.    pL
1516e 6f 63 6b 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74  ock->pNext = pBt
1516f 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 20 20 70 42 74  ->pLock;.    pBt
15170 2d 3e 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b  ->pLock = pLock;
15171 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74  .  }..  /* Set t
15172 68 65 20 42 74 4c 6f 63 6b 2e 65 4c 6f 63 6b 20  he BtLock.eLock 
15173 76 61 72 69 61 62 6c 65 20 74 6f 20 74 68 65 20  variable to the 
15174 6d 61 78 69 6d 75 6d 20 6f 66 20 74 68 65 20 63  maximum of the c
15175 75 72 72 65 6e 74 20 6c 6f 63 6b 0a 20 20 2a 2a  urrent lock.  **
15176 20 61 6e 64 20 74 68 65 20 72 65 71 75 65 73 74   and the request
15177 65 64 20 6c 6f 63 6b 2e 20 54 68 69 73 20 6d 65  ed lock. This me
15178 61 6e 73 20 69 66 20 61 20 77 72 69 74 65 2d 6c  ans if a write-l
15179 6f 63 6b 20 77 61 73 20 61 6c 72 65 61 64 79 20  ock was already 
1517a 68 65 6c 64 0a 20 20 2a 2a 20 61 6e 64 20 61 20  held.  ** and a 
1517b 72 65 61 64 2d 6c 6f 63 6b 20 72 65 71 75 65 73  read-lock reques
1517c 74 65 64 2c 20 77 65 20 64 6f 6e 27 74 20 69 6e  ted, we don't in
1517d 63 6f 72 72 65 63 74 6c 79 20 64 6f 77 6e 67 72  correctly downgr
1517e 61 64 65 20 74 68 65 20 6c 6f 63 6b 2e 0a 20 20  ade the lock..  
1517f 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 57 52 49  */.  assert( WRI
15180 54 45 5f 4c 4f 43 4b 3e 52 45 41 44 5f 4c 4f 43  TE_LOCK>READ_LOC
15181 4b 20 29 3b 0a 20 20 69 66 28 20 65 4c 6f 63 6b  K );.  if( eLock
15182 3e 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 29 7b  >pLock->eLock ){
15183 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63  .    pLock->eLoc
15184 6b 20 3d 20 65 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a  k = eLock;.  }..
15185 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
15186 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  OK;.}.#endif /* 
15187 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41  !SQLITE_OMIT_SHA
15188 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69  RED_CACHE */..#i
15189 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1518a 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f  T_SHARED_CACHE./
1518b 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 6c 6c  *.** Release all
1518c 20 74 68 65 20 74 61 62 6c 65 20 6c 6f 63 6b 73   the table locks
1518d 20 28 6c 6f 63 6b 73 20 6f 62 74 61 69 6e 65 64   (locks obtained
1518e 20 76 69 61 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a   via calls to.**
1518f 20 74 68 65 20 73 65 74 53 68 61 72 65 64 43 61   the setSharedCa
15190 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 20 70  cheTableLock() p
15191 72 6f 63 65 64 75 72 65 29 20 68 65 6c 64 20 62  rocedure) held b
15192 79 20 42 74 72 65 65 20 68 61 6e 64 6c 65 20 70  y Btree handle p
15193 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ..**.** This fun
15194 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68  ction assumes th
15195 61 74 20 68 61 6e 64 6c 65 20 70 20 68 61 73 20  at handle p has 
15196 61 6e 20 6f 70 65 6e 20 72 65 61 64 20 6f 72 20  an open read or 
15197 77 72 69 74 65 20 0a 2a 2a 20 74 72 61 6e 73 61  write .** transa
15198 63 74 69 6f 6e 2e 20 49 66 20 69 74 20 64 6f 65  ction. If it doe
15199 73 20 6e 6f 74 2c 20 74 68 65 6e 20 74 68 65 20  s not, then the 
1519a 42 74 53 68 61 72 65 64 2e 69 73 50 65 6e 64 69  BtShared.isPendi
1519b 6e 67 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d  ng variable.** m
1519c 61 79 20 62 65 20 69 6e 63 6f 72 72 65 63 74 6c  ay be incorrectl
1519d 79 20 63 6c 65 61 72 65 64 2e 0a 2a 2f 0a 73 74  y cleared..*/.st
1519e 61 74 69 63 20 76 6f 69 64 20 63 6c 65 61 72 41  atic void clearA
1519f 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 62  llSharedCacheTab
151a0 6c 65 4c 6f 63 6b 73 28 42 74 72 65 65 20 2a 70  leLocks(Btree *p
151a1 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  ){.  BtShared *p
151a2 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42  Bt = p->pBt;.  B
151a3 74 4c 6f 63 6b 20 2a 2a 70 70 49 74 65 72 20 3d  tLock **ppIter =
151a4 20 26 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a 0a 20   &pBt->pLock;.. 
151a5 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
151a6 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28  BtreeHoldsMutex(
151a7 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  p) );.  assert( 
151a8 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 30  p->sharable || 0
151a9 3d 3d 2a 70 70 49 74 65 72 20 29 3b 0a 20 20 61  ==*ppIter );.  a
151aa 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e  ssert( p->inTran
151ab 73 3e 30 20 29 3b 0a 0a 20 20 77 68 69 6c 65 28  s>0 );..  while(
151ac 20 2a 70 70 49 74 65 72 20 29 7b 0a 20 20 20 20   *ppIter ){.    
151ad 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 20 3d 20  BtLock *pLock = 
151ae 2a 70 70 49 74 65 72 3b 0a 20 20 20 20 61 73 73  *ppIter;.    ass
151af 65 72 74 28 20 70 42 74 2d 3e 69 73 45 78 63 6c  ert( pBt->isExcl
151b0 75 73 69 76 65 3d 3d 30 20 7c 7c 20 70 42 74 2d  usive==0 || pBt-
151b1 3e 70 57 72 69 74 65 72 3d 3d 70 4c 6f 63 6b 2d  >pWriter==pLock-
151b2 3e 70 42 74 72 65 65 20 29 3b 0a 20 20 20 20 61  >pBtree );.    a
151b3 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 70 42  ssert( pLock->pB
151b4 74 72 65 65 2d 3e 69 6e 54 72 61 6e 73 3e 3d 70  tree->inTrans>=p
151b5 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 29 3b 0a 20  Lock->eLock );. 
151b6 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 42     if( pLock->pB
151b7 74 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20  tree==p ){.     
151b8 20 2a 70 70 49 74 65 72 20 3d 20 70 4c 6f 63 6b   *ppIter = pLock
151b9 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 61  ->pNext;.      a
151ba 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 69 54  ssert( pLock->iT
151bb 61 62 6c 65 21 3d 31 20 7c 7c 20 70 4c 6f 63 6b  able!=1 || pLock
151bc 3d 3d 26 70 2d 3e 6c 6f 63 6b 20 29 3b 0a 20 20  ==&p->lock );.  
151bd 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 69      if( pLock->i
151be 54 61 62 6c 65 21 3d 31 20 29 7b 0a 20 20 20 20  Table!=1 ){.    
151bf 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
151c0 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d  (pLock);.      }
151c1 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
151c2 20 20 70 70 49 74 65 72 20 3d 20 26 70 4c 6f 63    ppIter = &pLoc
151c3 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a  k->pNext;.    }.
151c4 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70    }..  assert( p
151c5 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 3d 3d 30  Bt->isPending==0
151c6 20 7c 7c 20 70 42 74 2d 3e 70 57 72 69 74 65 72   || pBt->pWriter
151c7 20 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 70   );.  if( pBt->p
151c8 57 72 69 74 65 72 3d 3d 70 20 29 7b 0a 20 20 20  Writer==p ){.   
151c9 20 70 42 74 2d 3e 70 57 72 69 74 65 72 20 3d 20   pBt->pWriter = 
151ca 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69 73 45 78  0;.    pBt->isEx
151cb 63 6c 75 73 69 76 65 20 3d 20 30 3b 0a 20 20 20  clusive = 0;.   
151cc 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20   pBt->isPending 
151cd 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  = 0;.  }else if(
151ce 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69   pBt->nTransacti
151cf 6f 6e 3d 3d 32 20 29 7b 0a 20 20 20 20 2f 2a 20  on==2 ){.    /* 
151d0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
151d1 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 63 6f 6e   called when con
151d2 6e 65 63 74 69 6f 6e 20 70 20 69 73 20 63 6f 6e  nection p is con
151d3 63 6c 75 64 69 6e 67 20 69 74 73 20 0a 20 20 20  cluding its .   
151d4 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e   ** transaction.
151d5 20 49 66 20 74 68 65 72 65 20 63 75 72 72 65 6e   If there curren
151d6 74 6c 79 20 65 78 69 73 74 73 20 61 20 77 72 69  tly exists a wri
151d7 74 65 72 2c 20 61 6e 64 20 70 20 69 73 20 6e 6f  ter, and p is no
151d8 74 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 77 72  t.    ** that wr
151d9 69 74 65 72 2c 20 74 68 65 6e 20 74 68 65 20 6e  iter, then the n
151da 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 68  umber of locks h
151db 65 6c 64 20 62 79 20 63 6f 6e 6e 65 63 74 69 6f  eld by connectio
151dc 6e 73 20 6f 74 68 65 72 0a 20 20 20 20 2a 2a 20  ns other.    ** 
151dd 74 68 61 6e 20 74 68 65 20 77 72 69 74 65 72 20  than the writer 
151de 6d 75 73 74 20 62 65 20 61 62 6f 75 74 20 74 6f  must be about to
151df 20 64 72 6f 70 20 74 6f 20 7a 65 72 6f 2e 20 49   drop to zero. I
151e0 6e 20 74 68 69 73 20 63 61 73 65 0a 20 20 20 20  n this case.    
151e1 2a 2a 20 73 65 74 20 74 68 65 20 69 73 50 65 6e  ** set the isPen
151e2 64 69 6e 67 20 66 6c 61 67 20 74 6f 20 30 2e 0a  ding flag to 0..
151e3 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66      **.    ** If
151e4 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20 63 75   there is not cu
151e5 72 72 65 6e 74 6c 79 20 61 20 77 72 69 74 65 72  rrently a writer
151e6 2c 20 74 68 65 6e 20 42 74 53 68 61 72 65 64 2e  , then BtShared.
151e7 69 73 50 65 6e 64 69 6e 67 20 6d 75 73 74 0a 20  isPending must. 
151e8 20 20 20 2a 2a 20 62 65 20 7a 65 72 6f 20 61 6c     ** be zero al
151e9 72 65 61 64 79 2e 20 53 6f 20 74 68 69 73 20 6e  ready. So this n
151ea 65 78 74 20 6c 69 6e 65 20 69 73 20 68 61 72 6d  ext line is harm
151eb 6c 65 73 73 20 69 6e 20 74 68 61 74 20 63 61 73  less in that cas
151ec 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 42  e..    */.    pB
151ed 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20 3d 20 30  t->isPending = 0
151ee 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
151ef 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 68 61  his function cha
151f0 6e 67 65 73 20 61 6c 6c 20 77 72 69 74 65 2d 6c  nges all write-l
151f1 6f 63 6b 73 20 68 65 6c 64 20 62 79 20 63 6f 6e  ocks held by con
151f2 6e 65 63 74 69 6f 6e 20 70 20 74 6f 20 72 65 61  nection p to rea
151f3 64 2d 6c 6f 63 6b 73 2e 0a 2a 2f 0a 73 74 61 74  d-locks..*/.stat
151f4 69 63 20 76 6f 69 64 20 64 6f 77 6e 67 72 61 64  ic void downgrad
151f5 65 41 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54  eAllSharedCacheT
151f6 61 62 6c 65 4c 6f 63 6b 73 28 42 74 72 65 65 20  ableLocks(Btree 
151f7 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20  *p){.  BtShared 
151f8 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20  *pBt = p->pBt;. 
151f9 20 69 66 28 20 70 42 74 2d 3e 70 57 72 69 74 65   if( pBt->pWrite
151fa 72 3d 3d 70 20 29 7b 0a 20 20 20 20 42 74 4c 6f  r==p ){.    BtLo
151fb 63 6b 20 2a 70 4c 6f 63 6b 3b 0a 20 20 20 20 70  ck *pLock;.    p
151fc 42 74 2d 3e 70 57 72 69 74 65 72 20 3d 20 30 3b  Bt->pWriter = 0;
151fd 0a 20 20 20 20 70 42 74 2d 3e 69 73 45 78 63 6c  .    pBt->isExcl
151fe 75 73 69 76 65 20 3d 20 30 3b 0a 20 20 20 20 70  usive = 0;.    p
151ff 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20 3d 20  Bt->isPending = 
15200 30 3b 0a 20 20 20 20 66 6f 72 28 70 4c 6f 63 6b  0;.    for(pLock
15201 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 4c 6f  =pBt->pLock; pLo
15202 63 6b 3b 20 70 4c 6f 63 6b 3d 70 4c 6f 63 6b 2d  ck; pLock=pLock-
15203 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 61  >pNext){.      a
15204 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 65 4c  ssert( pLock->eL
15205 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c  ock==READ_LOCK |
15206 7c 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 3d  | pLock->pBtree=
15207 3d 70 20 29 3b 0a 20 20 20 20 20 20 70 4c 6f 63  =p );.      pLoc
15208 6b 2d 3e 65 4c 6f 63 6b 20 3d 20 52 45 41 44 5f  k->eLock = READ_
15209 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  LOCK;.    }.  }.
1520a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  }..#endif /* SQL
1520b 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
1520c 43 41 43 48 45 20 2a 2f 0a 0a 73 74 61 74 69 63  CACHE */..static
1520d 20 76 6f 69 64 20 72 65 6c 65 61 73 65 50 61 67   void releasePag
1520e 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65  e(MemPage *pPage
1520f 29 3b 20 20 2f 2a 20 46 6f 72 77 61 72 64 20 72  );  /* Forward r
15210 65 66 65 72 65 6e 63 65 20 2a 2f 0a 0a 2f 2a 0a  eference */../*.
15211 2a 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74  ** Verify that t
15212 68 65 20 63 75 72 73 6f 72 20 68 6f 6c 64 73 20  he cursor holds 
15213 61 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 20 42  a mutex on the B
15214 74 53 68 61 72 65 64 0a 2a 2f 0a 23 69 66 6e 64  tShared.*/.#ifnd
15215 65 66 20 4e 44 45 42 55 47 0a 73 74 61 74 69 63  ef NDEBUG.static
15216 20 69 6e 74 20 63 75 72 73 6f 72 48 6f 6c 64 73   int cursorHolds
15217 4d 75 74 65 78 28 42 74 43 75 72 73 6f 72 20 2a  Mutex(BtCursor *
15218 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  p){.  return sql
15219 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1521a 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a  p->pBt->mutex);.
1521b 7d 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64  }.#endif...#ifnd
1521c 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49  ef SQLITE_OMIT_I
1521d 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 49 6e  NCRBLOB./*.** In
1521e 76 61 6c 69 64 61 74 65 20 74 68 65 20 6f 76 65  validate the ove
1521f 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20  rflow page-list 
15220 63 61 63 68 65 20 66 6f 72 20 63 75 72 73 6f 72  cache for cursor
15221 20 70 43 75 72 2c 20 69 66 20 61 6e 79 2e 0a 2a   pCur, if any..*
15222 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e  /.static void in
15223 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77  validateOverflow
15224 43 61 63 68 65 28 42 74 43 75 72 73 6f 72 20 2a  Cache(BtCursor *
15225 70 43 75 72 29 7b 0a 20 20 61 73 73 65 72 74 28  pCur){.  assert(
15226 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
15227 78 28 70 43 75 72 29 20 29 3b 0a 20 20 73 71 6c  x(pCur) );.  sql
15228 69 74 65 33 5f 66 72 65 65 28 70 43 75 72 2d 3e  ite3_free(pCur->
15229 61 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20 70 43  aOverflow);.  pC
1522a 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 3d 20  ur->aOverflow = 
1522b 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 61  0;.}../*.** Inva
1522c 6c 69 64 61 74 65 20 74 68 65 20 6f 76 65 72 66  lidate the overf
1522d 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61  low page-list ca
1522e 63 68 65 20 66 6f 72 20 61 6c 6c 20 63 75 72 73  che for all curs
1522f 6f 72 73 20 6f 70 65 6e 65 64 0a 2a 2a 20 6f 6e  ors opened.** on
15230 20 74 68 65 20 73 68 61 72 65 64 20 62 74 72 65   the shared btre
15231 65 20 73 74 72 75 63 74 75 72 65 20 70 42 74 2e  e structure pBt.
15232 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
15233 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65  invalidateAllOve
15234 72 66 6c 6f 77 43 61 63 68 65 28 42 74 53 68 61  rflowCache(BtSha
15235 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 42 74 43  red *pBt){.  BtC
15236 75 72 73 6f 72 20 2a 70 3b 0a 20 20 61 73 73 65  ursor *p;.  asse
15237 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
15238 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65  x_held(pBt->mute
15239 78 29 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42  x) );.  for(p=pB
1523a 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70  t->pCursor; p; p
1523b 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  =p->pNext){.    
1523c 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c  invalidateOverfl
1523d 6f 77 43 61 63 68 65 28 70 29 3b 0a 20 20 7d 0a  owCache(p);.  }.
1523e 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
1523f 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
15240 20 62 65 66 6f 72 65 20 6d 6f 64 69 66 79 69 6e   before modifyin
15241 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  g the contents o
15242 66 20 61 20 74 61 62 6c 65 0a 2a 2a 20 62 2d 74  f a table.** b-t
15243 72 65 65 20 74 6f 20 69 6e 76 61 6c 69 64 61 74  ree to invalidat
15244 65 20 61 6e 79 20 69 6e 63 72 62 6c 6f 62 20 63  e any incrblob c
15245 75 72 73 6f 72 73 20 74 68 61 74 20 61 72 65 20  ursors that are 
15246 6f 70 65 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 72  open on the.** r
15247 6f 77 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65  ow or one of the
15248 20 72 6f 77 73 20 62 65 69 6e 67 20 6d 6f 64 69   rows being modi
15249 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  fied..**.** If a
1524a 72 67 75 6d 65 6e 74 20 69 73 43 6c 65 61 72 54  rgument isClearT
1524b 61 62 6c 65 20 69 73 20 74 72 75 65 2c 20 74 68  able is true, th
1524c 65 6e 20 74 68 65 20 65 6e 74 69 72 65 20 63 6f  en the entire co
1524d 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 0a 2a 2a  ntents of the.**
1524e 20 74 61 62 6c 65 20 69 73 20 61 62 6f 75 74 20   table is about 
1524f 74 6f 20 62 65 20 64 65 6c 65 74 65 64 2e 20 49  to be deleted. I
15250 6e 20 74 68 69 73 20 63 61 73 65 20 69 6e 76 61  n this case inva
15251 6c 69 64 61 74 65 20 61 6c 6c 20 69 6e 63 72 62  lidate all incrb
15252 6c 6f 62 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f  lob.** cursors o
15253 70 65 6e 20 6f 6e 20 61 6e 79 20 72 6f 77 20 77  pen on any row w
15254 69 74 68 69 6e 20 74 68 65 20 74 61 62 6c 65 20  ithin the table 
15255 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 70  with root-page p
15256 67 6e 6f 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 4f  gnoRoot..**.** O
15257 74 68 65 72 77 69 73 65 2c 20 69 66 20 61 72 67  therwise, if arg
15258 75 6d 65 6e 74 20 69 73 43 6c 65 61 72 54 61 62  ument isClearTab
15259 6c 65 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65  le is false, the
1525a 6e 20 74 68 65 20 72 6f 77 20 77 69 74 68 0a 2a  n the row with.*
1525b 2a 20 72 6f 77 69 64 20 69 52 6f 77 20 69 73 20  * rowid iRow is 
1525c 62 65 69 6e 67 20 72 65 70 6c 61 63 65 64 20 6f  being replaced o
1525d 72 20 64 65 6c 65 74 65 64 2e 20 49 6e 20 74 68  r deleted. In th
1525e 69 73 20 63 61 73 65 20 69 6e 76 61 6c 69 64 61  is case invalida
1525f 74 65 0a 2a 2a 20 6f 6e 6c 79 20 74 68 6f 73 65  te.** only those
15260 20 69 6e 63 72 62 6c 6f 62 20 63 75 72 73 6f 72   incrblob cursor
15261 73 20 6f 70 65 6e 20 6f 6e 20 74 68 69 73 20 73  s open on this s
15262 70 65 63 69 66 69 63 20 72 6f 77 2e 0a 2a 2f 0a  pecific row..*/.
15263 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 76 61  static void inva
15264 6c 69 64 61 74 65 49 6e 63 72 62 6c 6f 62 43 75  lidateIncrblobCu
15265 72 73 6f 72 73 28 0a 20 20 42 74 72 65 65 20 2a  rsors(.  Btree *
15266 70 42 74 72 65 65 2c 20 20 20 20 20 20 20 20 20  pBtree,         
15267 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
15268 20 66 69 6c 65 20 74 6f 20 63 68 65 63 6b 20 2a   file to check *
15269 2f 0a 20 20 69 36 34 20 69 52 6f 77 2c 20 20 20  /.  i64 iRow,   
1526a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1526b 68 65 20 72 6f 77 69 64 20 74 68 61 74 20 6d 69  he rowid that mi
1526c 67 68 74 20 62 65 20 63 68 61 6e 67 69 6e 67 20  ght be changing 
1526d 2a 2f 0a 20 20 69 6e 74 20 69 73 43 6c 65 61 72  */.  int isClear
1526e 54 61 62 6c 65 20 20 20 20 20 20 20 20 2f 2a 20  Table        /* 
1526f 54 72 75 65 20 69 66 20 61 6c 6c 20 72 6f 77 73  True if all rows
15270 20 61 72 65 20 62 65 69 6e 67 20 64 65 6c 65 74   are being delet
15271 65 64 20 2a 2f 0a 29 7b 0a 20 20 42 74 43 75 72  ed */.){.  BtCur
15272 73 6f 72 20 2a 70 3b 0a 20 20 42 74 53 68 61 72  sor *p;.  BtShar
15273 65 64 20 2a 70 42 74 20 3d 20 70 42 74 72 65 65  ed *pBt = pBtree
15274 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28  ->pBt;.  assert(
15275 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c   sqlite3BtreeHol
15276 64 73 4d 75 74 65 78 28 70 42 74 72 65 65 29 20  dsMutex(pBtree) 
15277 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 2d 3e  );.  for(p=pBt->
15278 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d  pCursor; p; p=p-
15279 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28  >pNext){.    if(
1527a 20 70 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61   p->isIncrblobHa
1527b 6e 64 6c 65 20 26 26 20 28 69 73 43 6c 65 61 72  ndle && (isClear
1527c 54 61 62 6c 65 20 7c 7c 20 70 2d 3e 69 6e 66 6f  Table || p->info
1527d 2e 6e 4b 65 79 3d 3d 69 52 6f 77 29 20 29 7b 0a  .nKey==iRow) ){.
1527e 20 20 20 20 20 20 70 2d 3e 65 53 74 61 74 65 20        p->eState 
1527f 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  = CURSOR_INVALID
15280 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23  ;.    }.  }.}..#
15281 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 69  else.  #define i
15282 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f  nvalidateOverflo
15283 77 43 61 63 68 65 28 78 29 0a 20 20 23 64 65 66  wCache(x).  #def
15284 69 6e 65 20 69 6e 76 61 6c 69 64 61 74 65 41 6c  ine invalidateAl
15285 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 78  lOverflowCache(x
15286 29 0a 20 20 23 64 65 66 69 6e 65 20 69 6e 76 61  ).  #define inva
15287 6c 69 64 61 74 65 49 6e 63 72 62 6c 6f 62 43 75  lidateIncrblobCu
15288 72 73 6f 72 73 28 78 2c 79 2c 7a 29 0a 23 65 6e  rsors(x,y,z).#en
15289 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 62  dif../*.** Set b
1528a 69 74 20 70 67 6e 6f 20 6f 66 20 74 68 65 20 42  it pgno of the B
1528b 74 53 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74  tShared.pHasCont
1528c 65 6e 74 20 62 69 74 76 65 63 2e 20 54 68 69 73  ent bitvec. This
1528d 20 69 73 20 63 61 6c 6c 65 64 20 0a 2a 2a 20 77   is called .** w
1528e 68 65 6e 20 61 20 70 61 67 65 20 74 68 61 74 20  hen a page that 
1528f 70 72 65 76 69 6f 75 73 6c 79 20 63 6f 6e 74 61  previously conta
15290 69 6e 65 64 20 64 61 74 61 20 62 65 63 6f 6d 65  ined data become
15291 73 20 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65  s a free-list le
15292 61 66 20 0a 2a 2a 20 70 61 67 65 2e 0a 2a 2a 0a  af .** page..**.
15293 2a 2a 20 54 68 65 20 42 74 53 68 61 72 65 64 2e  ** The BtShared.
15294 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 76  pHasContent bitv
15295 65 63 20 65 78 69 73 74 73 20 74 6f 20 77 6f 72  ec exists to wor
15296 6b 20 61 72 6f 75 6e 64 20 61 6e 20 6f 62 73 63  k around an obsc
15297 75 72 65 0a 2a 2a 20 62 75 67 20 63 61 75 73 65  ure.** bug cause
15298 64 20 62 79 20 74 68 65 20 69 6e 74 65 72 61 63  d by the interac
15299 74 69 6f 6e 20 6f 66 20 74 77 6f 20 75 73 65 66  tion of two usef
1529a 75 6c 20 49 4f 20 6f 70 74 69 6d 69 7a 61 74 69  ul IO optimizati
1529b 6f 6e 73 20 73 75 72 72 6f 75 6e 64 69 6e 67 0a  ons surrounding.
1529c 2a 2a 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61  ** free-list lea
1529d 66 20 70 61 67 65 73 3a 0a 2a 2a 0a 2a 2a 20 20  f pages:.**.**  
1529e 20 31 29 20 57 68 65 6e 20 61 6c 6c 20 64 61 74   1) When all dat
1529f 61 20 69 73 20 64 65 6c 65 74 65 64 20 66 72 6f  a is deleted fro
152a0 6d 20 61 20 70 61 67 65 20 61 6e 64 20 74 68 65  m a page and the
152a1 20 70 61 67 65 20 62 65 63 6f 6d 65 73 0a 2a 2a   page becomes.**
152a2 20 20 20 20 20 20 61 20 66 72 65 65 2d 6c 69 73        a free-lis
152a3 74 20 6c 65 61 66 20 70 61 67 65 2c 20 74 68 65  t leaf page, the
152a4 20 70 61 67 65 20 69 73 20 6e 6f 74 20 77 72 69   page is not wri
152a5 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61  tten to the data
152a6 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 28 61 73  base.**      (as
152a7 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20   free-list leaf 
152a8 70 61 67 65 73 20 63 6f 6e 74 61 69 6e 20 6e 6f  pages contain no
152a9 20 6d 65 61 6e 69 6e 67 66 75 6c 20 64 61 74 61   meaningful data
152aa 29 2e 20 53 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20  ). Sometimes.** 
152ab 20 20 20 20 20 73 75 63 68 20 61 20 70 61 67 65       such a page
152ac 20 69 73 20 6e 6f 74 20 65 76 65 6e 20 6a 6f 75   is not even jou
152ad 72 6e 61 6c 6c 65 64 20 28 61 73 20 69 74 20 77  rnalled (as it w
152ae 69 6c 6c 20 6e 6f 74 20 62 65 20 6d 6f 64 69 66  ill not be modif
152af 69 65 64 2c 0a 2a 2a 20 20 20 20 20 20 77 68 79  ied,.**      why
152b0 20 62 6f 74 68 65 72 20 6a 6f 75 72 6e 61 6c 6c   bother journall
152b1 69 6e 67 20 69 74 3f 29 2e 0a 2a 2a 0a 2a 2a 20  ing it?)..**.** 
152b2 20 20 32 29 20 57 68 65 6e 20 61 20 66 72 65 65    2) When a free
152b3 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 20  -list leaf page 
152b4 69 73 20 72 65 75 73 65 64 2c 20 69 74 73 20 63  is reused, its c
152b5 6f 6e 74 65 6e 74 20 69 73 20 6e 6f 74 20 72 65  ontent is not re
152b6 61 64 0a 2a 2a 20 20 20 20 20 20 66 72 6f 6d 20  ad.**      from 
152b7 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 72 20  the database or 
152b8 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a  written to the j
152b9 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 77 68 79  ournal file (why
152ba 20 73 68 6f 75 6c 64 20 69 74 0a 2a 2a 20 20 20   should it.**   
152bb 20 20 20 62 65 2c 20 69 66 20 69 74 20 69 73 20     be, if it is 
152bc 6e 6f 74 20 61 74 20 61 6c 6c 20 6d 65 61 6e 69  not at all meani
152bd 6e 67 66 75 6c 3f 29 2e 0a 2a 2a 0a 2a 2a 20 42  ngful?)..**.** B
152be 79 20 74 68 65 6d 73 65 6c 76 65 73 2c 20 74 68  y themselves, th
152bf 65 73 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  ese optimization
152c0 73 20 77 6f 72 6b 20 66 69 6e 65 20 61 6e 64 20  s work fine and 
152c1 70 72 6f 76 69 64 65 20 61 20 68 61 6e 64 79 0a  provide a handy.
152c2 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 62  ** performance b
152c3 6f 6f 73 74 20 74 6f 20 62 75 6c 6b 20 64 65 6c  oost to bulk del
152c4 65 74 65 20 6f 72 20 69 6e 73 65 72 74 20 6f 70  ete or insert op
152c5 65 72 61 74 69 6f 6e 73 2e 20 48 6f 77 65 76 65  erations. Howeve
152c6 72 2c 20 69 66 0a 2a 2a 20 61 20 70 61 67 65 20  r, if.** a page 
152c7 69 73 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20  is moved to the 
152c8 66 72 65 65 2d 6c 69 73 74 20 61 6e 64 20 74 68  free-list and th
152c9 65 6e 20 72 65 75 73 65 64 20 77 69 74 68 69 6e  en reused within
152ca 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74 72 61   the same.** tra
152cb 6e 73 61 63 74 69 6f 6e 2c 20 61 20 70 72 6f 62  nsaction, a prob
152cc 6c 65 6d 20 63 6f 6d 65 73 20 75 70 2e 20 49 66  lem comes up. If
152cd 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74   the page is not
152ce 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 77 68 65 6e   journalled when
152cf 0a 2a 2a 20 69 74 20 69 73 20 6d 6f 76 65 64 20  .** it is moved 
152d0 74 6f 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74  to the free-list
152d1 20 61 6e 64 20 69 74 20 69 73 20 61 6c 73 6f 20   and it is also 
152d2 6e 6f 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 77  not journalled w
152d3 68 65 6e 20 69 74 0a 2a 2a 20 69 73 20 65 78 74  hen it.** is ext
152d4 72 61 63 74 65 64 20 66 72 6f 6d 20 74 68 65 20  racted from the 
152d5 66 72 65 65 2d 6c 69 73 74 20 61 6e 64 20 72 65  free-list and re
152d6 75 73 65 64 2c 20 74 68 65 6e 20 74 68 65 20 6f  used, then the o
152d7 72 69 67 69 6e 61 6c 20 64 61 74 61 0a 2a 2a 20  riginal data.** 
152d8 6d 61 79 20 62 65 20 6c 6f 73 74 2e 20 49 6e 20  may be lost. In 
152d9 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 20 72  the event of a r
152da 6f 6c 6c 62 61 63 6b 2c 20 69 74 20 6d 61 79 20  ollback, it may 
152db 6e 6f 74 20 62 65 20 70 6f 73 73 69 62 6c 65 0a  not be possible.
152dc 2a 2a 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68  ** to restore th
152dd 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 69 74  e database to it
152de 73 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6e 66 69  s original confi
152df 67 75 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  guration..**.** 
152e0 54 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20  The solution is 
152e1 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 48 61  the BtShared.pHa
152e2 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 65 63 2e  sContent bitvec.
152e3 20 57 68 65 6e 65 76 65 72 20 61 20 70 61 67 65   Whenever a page
152e4 20 69 73 20 0a 2a 2a 20 6d 6f 76 65 64 20 74 6f   is .** moved to
152e5 20 62 65 63 6f 6d 65 20 61 20 66 72 65 65 2d 6c   become a free-l
152e6 69 73 74 20 6c 65 61 66 20 70 61 67 65 2c 20 74  ist leaf page, t
152e7 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
152e8 20 62 69 74 20 69 73 0a 2a 2a 20 73 65 74 20 69   bit is.** set i
152e9 6e 20 74 68 65 20 62 69 74 76 65 63 2e 20 57 68  n the bitvec. Wh
152ea 65 6e 65 76 65 72 20 61 20 6c 65 61 66 20 70 61  enever a leaf pa
152eb 67 65 20 69 73 20 65 78 74 72 61 63 74 65 64 20  ge is extracted 
152ec 66 72 6f 6d 20 74 68 65 20 66 72 65 65 2d 6c 69  from the free-li
152ed 73 74 2c 0a 2a 2a 20 6f 70 74 69 6d 69 7a 61 74  st,.** optimizat
152ee 69 6f 6e 20 32 20 61 62 6f 76 65 20 69 73 20 6f  ion 2 above is o
152ef 6d 6d 69 74 74 65 64 20 69 66 20 74 68 65 20 63  mmitted if the c
152f0 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 74  orresponding bit
152f1 20 69 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 73   is already.** s
152f2 65 74 20 69 6e 20 42 74 53 68 61 72 65 64 2e 70  et in BtShared.p
152f3 48 61 73 43 6f 6e 74 65 6e 74 2e 20 54 68 65 20  HasContent. The 
152f4 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
152f5 62 69 74 76 65 63 20 61 72 65 20 63 6c 65 61 72  bitvec are clear
152f6 65 64 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e 64  ed.** at the end
152f7 20 6f 66 20 65 76 65 72 79 20 74 72 61 6e 73 61   of every transa
152f8 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ction..*/.static
152f9 20 69 6e 74 20 62 74 72 65 65 53 65 74 48 61 73   int btreeSetHas
152fa 43 6f 6e 74 65 6e 74 28 42 74 53 68 61 72 65 64  Content(BtShared
152fb 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f   *pBt, Pgno pgno
152fc 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
152fd 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 21  LITE_OK;.  if( !
152fe 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74  pBt->pHasContent
152ff 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 50 61 67   ){.    int nPag
15300 65 20 3d 20 31 30 30 3b 0a 20 20 20 20 73 71 6c  e = 100;.    sql
15301 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75  ite3PagerPagecou
15302 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20  nt(pBt->pPager, 
15303 26 6e 50 61 67 65 29 3b 0a 20 20 20 20 2f 2a 20  &nPage);.    /* 
15304 49 66 20 73 71 6c 69 74 65 33 50 61 67 65 72 50  If sqlite3PagerP
15305 61 67 65 63 6f 75 6e 74 28 29 20 66 61 69 6c 73  agecount() fails
15306 20 74 68 65 72 65 20 69 73 20 6e 6f 20 68 61 72   there is no har
15307 6d 20 62 65 63 61 75 73 65 20 74 68 65 0a 20 20  m because the.  
15308 20 20 2a 2a 20 6e 50 61 67 65 20 76 61 72 69 61    ** nPage varia
15309 62 6c 65 20 69 73 20 75 6e 63 68 61 6e 67 65 64  ble is unchanged
1530a 20 66 72 6f 6d 20 69 74 73 20 64 65 66 61 75 6c   from its defaul
1530b 74 20 76 61 6c 75 65 20 6f 66 20 31 30 30 20 2a  t value of 100 *
1530c 2f 0a 20 20 20 20 70 42 74 2d 3e 70 48 61 73 43  /.    pBt->pHasC
1530d 6f 6e 74 65 6e 74 20 3d 20 73 71 6c 69 74 65 33  ontent = sqlite3
1530e 42 69 74 76 65 63 43 72 65 61 74 65 28 28 75 33  BitvecCreate((u3
1530f 32 29 6e 50 61 67 65 29 3b 0a 20 20 20 20 69 66  2)nPage);.    if
15310 28 20 21 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74  ( !pBt->pHasCont
15311 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 72 63 20  ent ){.      rc 
15312 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
15313 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
15314 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
15315 20 70 67 6e 6f 3c 3d 73 71 6c 69 74 65 33 42 69   pgno<=sqlite3Bi
15316 74 76 65 63 53 69 7a 65 28 70 42 74 2d 3e 70 48  tvecSize(pBt->pH
15317 61 73 43 6f 6e 74 65 6e 74 29 20 29 7b 0a 20 20  asContent) ){.  
15318 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69    rc = sqlite3Bi
15319 74 76 65 63 53 65 74 28 70 42 74 2d 3e 70 48 61  tvecSet(pBt->pHa
1531a 73 43 6f 6e 74 65 6e 74 2c 20 70 67 6e 6f 29 3b  sContent, pgno);
1531b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
1531c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79  ;.}../*.** Query
1531d 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 48   the BtShared.pH
1531e 61 73 43 6f 6e 74 65 6e 74 20 76 65 63 74 6f 72  asContent vector
1531f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ..**.** This fun
15320 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
15321 77 68 65 6e 20 61 20 66 72 65 65 2d 6c 69 73 74  when a free-list
15322 20 6c 65 61 66 20 70 61 67 65 20 69 73 20 72 65   leaf page is re
15323 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 0a 2a  moved from the.*
15324 2a 20 66 72 65 65 2d 6c 69 73 74 20 66 6f 72 20  * free-list for 
15325 72 65 75 73 65 2e 20 49 74 20 72 65 74 75 72 6e  reuse. It return
15326 73 20 66 61 6c 73 65 20 69 66 20 69 74 20 69 73  s false if it is
15327 20 73 61 66 65 20 74 6f 20 72 65 74 72 69 65 76   safe to retriev
15328 65 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 66 72  e the.** page fr
15329 6f 6d 20 74 68 65 20 70 61 67 65 72 20 6c 61 79  om the pager lay
1532a 65 72 20 77 69 74 68 20 74 68 65 20 27 6e 6f 2d  er with the 'no-
1532b 63 6f 6e 74 65 6e 74 27 20 66 6c 61 67 20 73 65  content' flag se
1532c 74 2e 20 54 72 75 65 20 6f 74 68 65 72 77 69 73  t. True otherwis
1532d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
1532e 20 62 74 72 65 65 47 65 74 48 61 73 43 6f 6e 74   btreeGetHasCont
1532f 65 6e 74 28 42 74 53 68 61 72 65 64 20 2a 70 42  ent(BtShared *pB
15330 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20  t, Pgno pgno){. 
15331 20 42 69 74 76 65 63 20 2a 70 20 3d 20 70 42 74   Bitvec *p = pBt
15332 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 3b 0a 20  ->pHasContent;. 
15333 20 72 65 74 75 72 6e 20 28 70 20 26 26 20 28 70   return (p && (p
15334 67 6e 6f 3e 73 71 6c 69 74 65 33 42 69 74 76 65  gno>sqlite3Bitve
15335 63 53 69 7a 65 28 70 29 20 7c 7c 20 73 71 6c 69  cSize(p) || sqli
15336 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 2c  te3BitvecTest(p,
15337 20 70 67 6e 6f 29 29 29 3b 0a 7d 0a 0a 2f 2a 0a   pgno)));.}../*.
15338 2a 2a 20 43 6c 65 61 72 20 28 64 65 73 74 72 6f  ** Clear (destro
15339 79 29 20 74 68 65 20 42 74 53 68 61 72 65 64 2e  y) the BtShared.
1533a 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 76  pHasContent bitv
1533b 65 63 2e 20 54 68 69 73 20 73 68 6f 75 6c 64 20  ec. This should 
1533c 62 65 0a 2a 2a 20 69 6e 76 6f 6b 65 64 20 61 74  be.** invoked at
1533d 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20   the conclusion 
1533e 6f 66 20 65 61 63 68 20 77 72 69 74 65 2d 74 72  of each write-tr
1533f 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74  ansaction..*/.st
15340 61 74 69 63 20 76 6f 69 64 20 62 74 72 65 65 43  atic void btreeC
15341 6c 65 61 72 48 61 73 43 6f 6e 74 65 6e 74 28 42  learHasContent(B
15342 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20  tShared *pBt){. 
15343 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65   sqlite3BitvecDe
15344 73 74 72 6f 79 28 70 42 74 2d 3e 70 48 61 73 43  stroy(pBt->pHasC
15345 6f 6e 74 65 6e 74 29 3b 0a 20 20 70 42 74 2d 3e  ontent);.  pBt->
15346 70 48 61 73 43 6f 6e 74 65 6e 74 20 3d 20 30 3b  pHasContent = 0;
15347 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 61 76 65 20 74  .}../*.** Save t
15348 68 65 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f  he current curso
15349 72 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74 68  r position in th
1534a 65 20 76 61 72 69 61 62 6c 65 73 20 42 74 43 75  e variables BtCu
1534b 72 73 6f 72 2e 6e 4b 65 79 20 0a 2a 2a 20 61 6e  rsor.nKey .** an
1534c 64 20 42 74 43 75 72 73 6f 72 2e 70 4b 65 79 2e  d BtCursor.pKey.
1534d 20 54 68 65 20 63 75 72 73 6f 72 27 73 20 73 74   The cursor's st
1534e 61 74 65 20 69 73 20 73 65 74 20 74 6f 20 43 55  ate is set to CU
1534f 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b  RSOR_REQUIRESEEK
15350 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c  ..**.** The call
15351 65 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74  er must ensure t
15352 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 69  hat the cursor i
15353 73 20 76 61 6c 69 64 20 28 68 61 73 20 65 53 74  s valid (has eSt
15354 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49  ate==CURSOR_VALI
15355 44 29 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 63  D).** prior to c
15356 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74  alling this rout
15357 69 6e 65 2e 20 20 0a 2a 2f 0a 73 74 61 74 69 63  ine.  .*/.static
15358 20 69 6e 74 20 73 61 76 65 43 75 72 73 6f 72 50   int saveCursorP
15359 6f 73 69 74 69 6f 6e 28 42 74 43 75 72 73 6f 72  osition(BtCursor
1535a 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 72   *pCur){.  int r
1535b 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 43 55  c;..  assert( CU
1535c 52 53 4f 52 5f 56 41 4c 49 44 3d 3d 70 43 75 72  RSOR_VALID==pCur
1535d 2d 3e 65 53 74 61 74 65 20 29 3b 0a 20 20 61 73  ->eState );.  as
1535e 73 65 72 74 28 20 30 3d 3d 70 43 75 72 2d 3e 70  sert( 0==pCur->p
1535f 4b 65 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28  Key );.  assert(
15360 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
15361 78 28 70 43 75 72 29 20 29 3b 0a 0a 20 20 72 63  x(pCur) );..  rc
15362 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b   = sqlite3BtreeK
15363 65 79 53 69 7a 65 28 70 43 75 72 2c 20 26 70 43  eySize(pCur, &pC
15364 75 72 2d 3e 6e 4b 65 79 29 3b 0a 20 20 61 73 73  ur->nKey);.  ass
15365 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
15366 4f 4b 20 29 3b 20 20 2f 2a 20 4b 65 79 53 69 7a  OK );  /* KeySiz
15367 65 28 29 20 63 61 6e 6e 6f 74 20 66 61 69 6c 20  e() cannot fail 
15368 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73  */..  /* If this
15369 20 69 73 20 61 6e 20 69 6e 74 4b 65 79 20 74 61   is an intKey ta
1536a 62 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 61 62  ble, then the ab
1536b 6f 76 65 20 63 61 6c 6c 20 74 6f 20 42 74 72 65  ove call to Btre
1536c 65 4b 65 79 53 69 7a 65 28 29 0a 20 20 2a 2a 20  eKeySize().  ** 
1536d 73 74 6f 72 65 73 20 74 68 65 20 69 6e 74 65 67  stores the integ
1536e 65 72 20 6b 65 79 20 69 6e 20 70 43 75 72 2d 3e  er key in pCur->
1536f 6e 4b 65 79 2e 20 49 6e 20 74 68 69 73 20 63 61  nKey. In this ca
15370 73 65 20 74 68 69 73 20 76 61 6c 75 65 20 69 73  se this value is
15371 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 61 74 20 69  .  ** all that i
15372 73 20 72 65 71 75 69 72 65 64 2e 20 4f 74 68 65  s required. Othe
15373 72 77 69 73 65 2c 20 69 66 20 70 43 75 72 20 69  rwise, if pCur i
15374 73 20 6e 6f 74 20 6f 70 65 6e 20 6f 6e 20 61 6e  s not open on an
15375 20 69 6e 74 4b 65 79 0a 20 20 2a 2a 20 74 61 62   intKey.  ** tab
15376 6c 65 2c 20 74 68 65 6e 20 6d 61 6c 6c 6f 63 20  le, then malloc 
15377 73 70 61 63 65 20 66 6f 72 20 61 6e 64 20 73 74  space for and st
15378 6f 72 65 20 74 68 65 20 70 43 75 72 2d 3e 6e 4b  ore the pCur->nK
15379 65 79 20 62 79 74 65 73 20 6f 66 20 6b 65 79 20  ey bytes of key 
1537a 0a 20 20 2a 2a 20 64 61 74 61 2e 0a 20 20 2a 2f  .  ** data..  */
1537b 0a 20 20 69 66 28 20 30 3d 3d 70 43 75 72 2d 3e  .  if( 0==pCur->
1537c 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65  apPage[0]->intKe
1537d 79 20 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 70  y ){.    void *p
1537e 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  Key = sqlite3Mal
1537f 6c 6f 63 28 20 28 69 6e 74 29 70 43 75 72 2d 3e  loc( (int)pCur->
15380 6e 4b 65 79 20 29 3b 0a 20 20 20 20 69 66 28 20  nKey );.    if( 
15381 70 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63  pKey ){.      rc
15382 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b   = sqlite3BtreeK
15383 65 79 28 70 43 75 72 2c 20 30 2c 20 28 69 6e 74  ey(pCur, 0, (int
15384 29 70 43 75 72 2d 3e 6e 4b 65 79 2c 20 70 4b 65  )pCur->nKey, pKe
15385 79 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  y);.      if( rc
15386 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
15387 20 20 20 20 20 20 20 70 43 75 72 2d 3e 70 4b 65         pCur->pKe
15388 79 20 3d 20 70 4b 65 79 3b 0a 20 20 20 20 20 20  y = pKey;.      
15389 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73  }else{.        s
1538a 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4b 65 79  qlite3_free(pKey
1538b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
1538c 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d  else{.      rc =
1538d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
1538e 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72     }.  }.  asser
1538f 74 28 20 21 70 43 75 72 2d 3e 61 70 50 61 67 65  t( !pCur->apPage
15390 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 21  [0]->intKey || !
15391 70 43 75 72 2d 3e 70 4b 65 79 20 29 3b 0a 0a 20  pCur->pKey );.. 
15392 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
15393 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  OK ){.    int i;
15394 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
15395 3d 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 2b  =pCur->iPage; i+
15396 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73  +){.      releas
15397 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61  ePage(pCur->apPa
15398 67 65 5b 69 5d 29 3b 0a 20 20 20 20 20 20 70 43  ge[i]);.      pC
15399 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 20 3d 20  ur->apPage[i] = 
1539a 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 75  0;.    }.    pCu
1539b 72 2d 3e 69 50 61 67 65 20 3d 20 2d 31 3b 0a 20  r->iPage = -1;. 
1539c 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20     pCur->eState 
1539d 3d 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45  = CURSOR_REQUIRE
1539e 53 45 45 4b 3b 0a 20 20 7d 0a 0a 20 20 69 6e 76  SEEK;.  }..  inv
1539f 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43  alidateOverflowC
153a0 61 63 68 65 28 70 43 75 72 29 3b 0a 20 20 72 65  ache(pCur);.  re
153a1 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
153a2 2a 20 53 61 76 65 20 74 68 65 20 70 6f 73 69 74  * Save the posit
153a3 69 6f 6e 73 20 6f 66 20 61 6c 6c 20 63 75 72 73  ions of all curs
153a4 6f 72 73 20 65 78 63 65 70 74 20 70 45 78 63 65  ors except pExce
153a5 70 74 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 74  pt open on the t
153a6 61 62 6c 65 20 0a 2a 2a 20 77 69 74 68 20 72 6f  able .** with ro
153a7 6f 74 2d 70 61 67 65 20 69 52 6f 6f 74 2e 20 55  ot-page iRoot. U
153a8 73 75 61 6c 6c 79 2c 20 74 68 69 73 20 69 73 20  sually, this is 
153a9 63 61 6c 6c 65 64 20 6a 75 73 74 20 62 65 66 6f  called just befo
153aa 72 65 20 63 75 72 73 6f 72 0a 2a 2a 20 70 45 78  re cursor.** pEx
153ab 63 65 70 74 20 69 73 20 75 73 65 64 20 74 6f 20  cept is used to 
153ac 6d 6f 64 69 66 79 20 74 68 65 20 74 61 62 6c 65  modify the table
153ad 20 28 42 74 72 65 65 44 65 6c 65 74 65 28 29 20   (BtreeDelete() 
153ae 6f 72 20 42 74 72 65 65 49 6e 73 65 72 74 28 29  or BtreeInsert()
153af 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
153b0 20 73 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28   saveAllCursors(
153b1 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50  BtShared *pBt, P
153b2 67 6e 6f 20 69 52 6f 6f 74 2c 20 42 74 43 75 72  gno iRoot, BtCur
153b3 73 6f 72 20 2a 70 45 78 63 65 70 74 29 7b 0a 20  sor *pExcept){. 
153b4 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20   BtCursor *p;.  
153b5 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
153b6 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e  mutex_held(pBt->
153b7 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
153b8 72 74 28 20 70 45 78 63 65 70 74 3d 3d 30 20 7c  rt( pExcept==0 |
153b9 7c 20 70 45 78 63 65 70 74 2d 3e 70 42 74 3d 3d  | pExcept->pBt==
153ba 70 42 74 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70  pBt );.  for(p=p
153bb 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20  Bt->pCursor; p; 
153bc 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  p=p->pNext){.   
153bd 20 69 66 28 20 70 21 3d 70 45 78 63 65 70 74 20   if( p!=pExcept 
153be 26 26 20 28 30 3d 3d 69 52 6f 6f 74 20 7c 7c 20  && (0==iRoot || 
153bf 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 69 52 6f  p->pgnoRoot==iRo
153c0 6f 74 29 20 26 26 20 0a 20 20 20 20 20 20 20 20  ot) && .        
153c1 70 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  p->eState==CURSO
153c2 52 5f 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 20  R_VALID ){.     
153c3 20 69 6e 74 20 72 63 20 3d 20 73 61 76 65 43 75   int rc = saveCu
153c4 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 29 3b  rsorPosition(p);
153c5 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54  .      if( SQLIT
153c6 45 5f 4f 4b 21 3d 72 63 20 29 7b 0a 20 20 20 20  E_OK!=rc ){.    
153c7 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
153c8 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
153c9 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
153ca 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c  _OK;.}../*.** Cl
153cb 65 61 72 20 74 68 65 20 63 75 72 72 65 6e 74 20  ear the current 
153cc 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 2e  cursor position.
153cd 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
153ce 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
153cf 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28  treeClearCursor(
153d0 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b  BtCursor *pCur){
153d1 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
153d2 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72  rHoldsMutex(pCur
153d3 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  ) );.  sqlite3_f
153d4 72 65 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b  ree(pCur->pKey);
153d5 0a 20 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20  .  pCur->pKey = 
153d6 30 3b 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 74  0;.  pCur->eStat
153d7 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c  e = CURSOR_INVAL
153d8 49 44 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 20  ID;.}../*.** In 
153d9 74 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20  this version of 
153da 42 74 72 65 65 4d 6f 76 65 74 6f 2c 20 70 4b 65  BtreeMoveto, pKe
153db 79 20 69 73 20 61 20 70 61 63 6b 65 64 20 69 6e  y is a packed in
153dc 64 65 78 20 72 65 63 6f 72 64 0a 2a 2a 20 73 75  dex record.** su
153dd 63 68 20 61 73 20 69 73 20 67 65 6e 65 72 61 74  ch as is generat
153de 65 64 20 62 79 20 74 68 65 20 4f 50 5f 4d 61 6b  ed by the OP_Mak
153df 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 2e 20  eRecord opcode. 
153e0 20 55 6e 70 61 63 6b 20 74 68 65 0a 2a 2a 20 72   Unpack the.** r
153e1 65 63 6f 72 64 20 61 6e 64 20 74 68 65 6e 20 63  ecord and then c
153e2 61 6c 6c 20 42 74 72 65 65 4d 6f 76 65 74 6f 55  all BtreeMovetoU
153e3 6e 70 61 63 6b 65 64 28 29 20 74 6f 20 64 6f 20  npacked() to do 
153e4 74 68 65 20 77 6f 72 6b 2e 0a 2a 2f 0a 73 74 61  the work..*/.sta
153e5 74 69 63 20 69 6e 74 20 62 74 72 65 65 4d 6f 76  tic int btreeMov
153e6 65 74 6f 28 0a 20 20 42 74 43 75 72 73 6f 72 20  eto(.  BtCursor 
153e7 2a 70 43 75 72 2c 20 20 20 20 20 2f 2a 20 43 75  *pCur,     /* Cu
153e8 72 73 6f 72 20 6f 70 65 6e 20 6f 6e 20 74 68 65  rsor open on the
153e9 20 62 74 72 65 65 20 74 6f 20 62 65 20 73 65 61   btree to be sea
153ea 72 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74  rched */.  const
153eb 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20 2f   void *pKey,   /
153ec 2a 20 50 61 63 6b 65 64 20 6b 65 79 20 69 66 20  * Packed key if 
153ed 74 68 65 20 62 74 72 65 65 20 69 73 20 61 6e 20  the btree is an 
153ee 69 6e 64 65 78 20 2a 2f 0a 20 20 69 36 34 20 6e  index */.  i64 n
153ef 4b 65 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f  Key,           /
153f0 2a 20 49 6e 74 65 67 65 72 20 6b 65 79 20 66 6f  * Integer key fo
153f1 72 20 74 61 62 6c 65 73 2e 20 20 53 69 7a 65 20  r tables.  Size 
153f2 6f 66 20 70 4b 65 79 20 66 6f 72 20 69 6e 64 69  of pKey for indi
153f3 63 65 73 20 2a 2f 0a 20 20 69 6e 74 20 62 69 61  ces */.  int bia
153f4 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  s,           /* 
153f5 42 69 61 73 20 73 65 61 72 63 68 20 74 6f 20 74  Bias search to t
153f6 68 65 20 68 69 67 68 20 65 6e 64 20 2a 2f 0a 20  he high end */. 
153f7 20 69 6e 74 20 2a 70 52 65 73 20 20 20 20 20 20   int *pRes      
153f8 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 73 65       /* Write se
153f9 61 72 63 68 20 72 65 73 75 6c 74 73 20 68 65 72  arch results her
153fa 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  e */.){.  int rc
153fb 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
153fc 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20 63       /* Status c
153fd 6f 64 65 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65  ode */.  Unpacke
153fe 64 52 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79  dRecord *pIdxKey
153ff 3b 20 20 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20  ;   /* Unpacked 
15400 69 6e 64 65 78 20 6b 65 79 20 2a 2f 0a 20 20 63  index key */.  c
15401 68 61 72 20 61 53 70 61 63 65 5b 31 35 30 5d 3b  har aSpace[150];
15402 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d            /* Tem
15403 70 20 73 70 61 63 65 20 66 6f 72 20 70 49 64 78  p space for pIdx
15404 4b 65 79 20 2d 20 74 6f 20 61 76 6f 69 64 20 61  Key - to avoid a
15405 20 6d 61 6c 6c 6f 63 20 2a 2f 0a 0a 20 20 69 66   malloc */..  if
15406 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 61 73  ( pKey ){.    as
15407 73 65 72 74 28 20 6e 4b 65 79 3d 3d 28 69 36 34  sert( nKey==(i64
15408 29 28 69 6e 74 29 6e 4b 65 79 20 29 3b 0a 20 20  )(int)nKey );.  
15409 20 20 70 49 64 78 4b 65 79 20 3d 20 73 71 6c 69    pIdxKey = sqli
1540a 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70  te3VdbeRecordUnp
1540b 61 63 6b 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e  ack(pCur->pKeyIn
1540c 66 6f 2c 20 28 69 6e 74 29 6e 4b 65 79 2c 20 70  fo, (int)nKey, p
1540d 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Key,.           
1540e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1540f 20 20 20 20 20 20 20 20 20 20 20 61 53 70 61 63             aSpac
15410 65 2c 20 73 69 7a 65 6f 66 28 61 53 70 61 63 65  e, sizeof(aSpace
15411 29 29 3b 0a 20 20 20 20 69 66 28 20 70 49 64 78  ));.    if( pIdx
15412 4b 65 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  Key==0 ) return 
15413 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
15414 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 64 78 4b  }else{.    pIdxK
15415 65 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 63  ey = 0;.  }.  rc
15416 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d   = sqlite3BtreeM
15417 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43  ovetoUnpacked(pC
15418 75 72 2c 20 70 49 64 78 4b 65 79 2c 20 6e 4b 65  ur, pIdxKey, nKe
15419 79 2c 20 62 69 61 73 2c 20 70 52 65 73 29 3b 0a  y, bias, pRes);.
1541a 20 20 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 20    if( pKey ){.  
1541b 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c    sqlite3VdbeDel
1541c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  eteUnpackedRecor
1541d 64 28 70 49 64 78 4b 65 79 29 3b 0a 20 20 7d 0a  d(pIdxKey);.  }.
1541e 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1541f 2f 2a 0a 2a 2a 20 52 65 73 74 6f 72 65 20 74 68  /*.** Restore th
15420 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20  e cursor to the 
15421 70 6f 73 69 74 69 6f 6e 20 69 74 20 77 61 73 20  position it was 
15422 69 6e 20 28 6f 72 20 61 73 20 63 6c 6f 73 65 20  in (or as close 
15423 74 6f 20 61 73 20 70 6f 73 73 69 62 6c 65 29 0a  to as possible).
15424 2a 2a 20 77 68 65 6e 20 73 61 76 65 43 75 72 73  ** when saveCurs
15425 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 77 61 73  orPosition() was
15426 20 63 61 6c 6c 65 64 2e 20 4e 6f 74 65 20 74 68   called. Note th
15427 61 74 20 74 68 69 73 20 63 61 6c 6c 20 64 65 6c  at this call del
15428 65 74 65 73 20 74 68 65 20 0a 2a 2a 20 73 61 76  etes the .** sav
15429 65 64 20 70 6f 73 69 74 69 6f 6e 20 69 6e 66 6f  ed position info
1542a 20 73 74 6f 72 65 64 20 62 79 20 73 61 76 65 43   stored by saveC
1542b 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 2c  ursorPosition(),
1542c 20 73 6f 20 74 68 65 72 65 20 63 61 6e 20 62 65   so there can be
1542d 0a 2a 2a 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20  .** at most one 
1542e 65 66 66 65 63 74 69 76 65 20 72 65 73 74 6f 72  effective restor
1542f 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28  eCursorPosition(
15430 29 20 63 61 6c 6c 20 61 66 74 65 72 20 65 61 63  ) call after eac
15431 68 20 0a 2a 2a 20 73 61 76 65 43 75 72 73 6f 72  h .** saveCursor
15432 50 6f 73 69 74 69 6f 6e 28 29 2e 0a 2a 2f 0a 73  Position()..*/.s
15433 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 52  tatic int btreeR
15434 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69  estoreCursorPosi
15435 74 69 6f 6e 28 42 74 43 75 72 73 6f 72 20 2a 70  tion(BtCursor *p
15436 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  Cur){.  int rc;.
15437 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
15438 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
15439 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43   );.  assert( pC
1543a 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 53  ur->eState>=CURS
1543b 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29  OR_REQUIRESEEK )
1543c 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53  ;.  if( pCur->eS
1543d 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55  tate==CURSOR_FAU
1543e 4c 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  LT ){.    return
1543f 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b   pCur->skipNext;
15440 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 65 53 74  .  }.  pCur->eSt
15441 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56  ate = CURSOR_INV
15442 41 4c 49 44 3b 0a 20 20 72 63 20 3d 20 62 74 72  ALID;.  rc = btr
15443 65 65 4d 6f 76 65 74 6f 28 70 43 75 72 2c 20 70  eeMoveto(pCur, p
15444 43 75 72 2d 3e 70 4b 65 79 2c 20 70 43 75 72 2d  Cur->pKey, pCur-
15445 3e 6e 4b 65 79 2c 20 30 2c 20 26 70 43 75 72 2d  >nKey, 0, &pCur-
15446 3e 73 6b 69 70 4e 65 78 74 29 3b 0a 20 20 69 66  >skipNext);.  if
15447 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
15448 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  ){.    sqlite3_f
15449 72 65 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b  ree(pCur->pKey);
1544a 0a 20 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 20  .    pCur->pKey 
1544b 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28  = 0;.    assert(
1544c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
1544d 55 52 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70  URSOR_VALID || p
1544e 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52  Cur->eState==CUR
1544f 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 3b 0a 20  SOR_INVALID );. 
15450 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
15451 7d 0a 0a 23 64 65 66 69 6e 65 20 72 65 73 74 6f  }..#define resto
15452 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e  reCursorPosition
15453 28 70 29 20 5c 0a 20 20 28 70 2d 3e 65 53 74 61  (p) \.  (p->eSta
15454 74 65 3e 3d 43 55 52 53 4f 52 5f 52 45 51 55 49  te>=CURSOR_REQUI
15455 52 45 53 45 45 4b 20 3f 20 5c 0a 20 20 20 20 20  RESEEK ? \.     
15456 20 20 20 20 62 74 72 65 65 52 65 73 74 6f 72 65      btreeRestore
15457 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70  CursorPosition(p
15458 29 20 3a 20 5c 0a 20 20 20 20 20 20 20 20 20 53  ) : \.         S
15459 51 4c 49 54 45 5f 4f 4b 29 0a 0a 2f 2a 0a 2a 2a  QLITE_OK)../*.**
1545a 20 44 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68   Determine wheth
1545b 65 72 20 6f 72 20 6e 6f 74 20 61 20 63 75 72 73  er or not a curs
1545c 6f 72 20 68 61 73 20 6d 6f 76 65 64 20 66 72 6f  or has moved fro
1545d 6d 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 20 69  m the position i
1545e 74 0a 2a 2a 20 77 61 73 20 6c 61 73 74 20 70 6c  t.** was last pl
1545f 61 63 65 64 20 61 74 2e 20 20 43 75 72 73 6f 72  aced at.  Cursor
15460 73 20 63 61 6e 20 6d 6f 76 65 20 77 68 65 6e 20  s can move when 
15461 74 68 65 20 72 6f 77 20 74 68 65 79 20 61 72 65  the row they are
15462 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20 61 74 20   pointing.** at 
15463 69 73 20 64 65 6c 65 74 65 64 20 6f 75 74 20 66  is deleted out f
15464 72 6f 6d 20 75 6e 64 65 72 20 74 68 65 6d 2e 0a  rom under them..
15465 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
15466 6e 65 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72  ne returns an er
15467 72 6f 72 20 63 6f 64 65 20 69 66 20 73 6f 6d 65  ror code if some
15468 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67  thing goes wrong
15469 2e 20 20 54 68 65 0a 2a 2a 20 69 6e 74 65 67 65  .  The.** intege
1546a 72 20 2a 70 48 61 73 4d 6f 76 65 64 20 69 73 20  r *pHasMoved is 
1546b 73 65 74 20 74 6f 20 6f 6e 65 20 69 66 20 74 68  set to one if th
1546c 65 20 63 75 72 73 6f 72 20 68 61 73 20 6d 6f 76  e cursor has mov
1546d 65 64 20 61 6e 64 20 30 20 69 66 20 6e 6f 74 2e  ed and 0 if not.
1546e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1546f 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
15470 72 65 65 43 75 72 73 6f 72 48 61 73 4d 6f 76 65  reeCursorHasMove
15471 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  d(BtCursor *pCur
15472 2c 20 69 6e 74 20 2a 70 48 61 73 4d 6f 76 65 64  , int *pHasMoved
15473 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20  ){.  int rc;..  
15474 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73  rc = restoreCurs
15475 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29  orPosition(pCur)
15476 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20  ;.  if( rc ){.  
15477 20 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 31    *pHasMoved = 1
15478 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  ;.    return rc;
15479 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 75 72 2d  .  }.  if( pCur-
1547a 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f  >eState!=CURSOR_
1547b 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d 3e 73  VALID || pCur->s
1547c 6b 69 70 4e 65 78 74 21 3d 30 20 29 7b 0a 20 20  kipNext!=0 ){.  
1547d 20 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 31    *pHasMoved = 1
1547e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a  ;.  }else{.    *
1547f 70 48 61 73 4d 6f 76 65 64 20 3d 20 30 3b 0a 20  pHasMoved = 0;. 
15480 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
15481 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65  TE_OK;.}..#ifnde
15482 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
15483 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 47  TOVACUUM./*.** G
15484 69 76 65 6e 20 61 20 70 61 67 65 20 6e 75 6d 62  iven a page numb
15485 65 72 20 6f 66 20 61 20 72 65 67 75 6c 61 72 20  er of a regular 
15486 64 61 74 61 62 61 73 65 20 70 61 67 65 2c 20 72  database page, r
15487 65 74 75 72 6e 20 74 68 65 20 70 61 67 65 0a 2a  eturn the page.*
15488 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65  * number for the
15489 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67   pointer-map pag
1548a 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20  e that contains 
1548b 74 68 65 20 65 6e 74 72 79 20 66 6f 72 20 74 68  the entry for th
1548c 65 0a 2a 2a 20 69 6e 70 75 74 20 70 61 67 65 20  e.** input page 
1548d 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  number..*/.stati
1548e 63 20 50 67 6e 6f 20 70 74 72 6d 61 70 50 61 67  c Pgno ptrmapPag
1548f 65 6e 6f 28 42 74 53 68 61 72 65 64 20 2a 70 42  eno(BtShared *pB
15490 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20  t, Pgno pgno){. 
15491 20 69 6e 74 20 6e 50 61 67 65 73 50 65 72 4d 61   int nPagesPerMa
15492 70 50 61 67 65 3b 0a 20 20 50 67 6e 6f 20 69 50  pPage;.  Pgno iP
15493 74 72 4d 61 70 2c 20 72 65 74 3b 0a 20 20 61 73  trMap, ret;.  as
15494 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
15495 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75  tex_held(pBt->mu
15496 74 65 78 29 20 29 3b 0a 20 20 6e 50 61 67 65 73  tex) );.  nPages
15497 50 65 72 4d 61 70 50 61 67 65 20 3d 20 28 70 42  PerMapPage = (pB
15498 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 35 29  t->usableSize/5)
15499 2b 31 3b 0a 20 20 69 50 74 72 4d 61 70 20 3d 20  +1;.  iPtrMap = 
1549a 28 70 67 6e 6f 2d 32 29 2f 6e 50 61 67 65 73 50  (pgno-2)/nPagesP
1549b 65 72 4d 61 70 50 61 67 65 3b 0a 20 20 72 65 74  erMapPage;.  ret
1549c 20 3d 20 28 69 50 74 72 4d 61 70 2a 6e 50 61 67   = (iPtrMap*nPag
1549d 65 73 50 65 72 4d 61 70 50 61 67 65 29 20 2b 20  esPerMapPage) + 
1549e 32 3b 20 0a 20 20 69 66 28 20 72 65 74 3d 3d 50  2; .  if( ret==P
1549f 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45  ENDING_BYTE_PAGE
154a0 28 70 42 74 29 20 29 7b 0a 20 20 20 20 72 65 74  (pBt) ){.    ret
154a1 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ++;.  }.  return
154a2 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57   ret;.}../*.** W
154a3 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e  rite an entry in
154a4 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d  to the pointer m
154a5 61 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ap..**.** This r
154a6 6f 75 74 69 6e 65 20 75 70 64 61 74 65 73 20 74  outine updates t
154a7 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65  he pointer map e
154a8 6e 74 72 79 20 66 6f 72 20 70 61 67 65 20 6e 75  ntry for page nu
154a9 6d 62 65 72 20 27 6b 65 79 27 0a 2a 2a 20 73 6f  mber 'key'.** so
154aa 20 74 68 61 74 20 69 74 20 6d 61 70 73 20 74 6f   that it maps to
154ab 20 74 79 70 65 20 27 65 54 79 70 65 27 20 61 6e   type 'eType' an
154ac 64 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e 75  d parent page nu
154ad 6d 62 65 72 20 27 70 67 6e 6f 27 2e 0a 2a 2a 0a  mber 'pgno'..**.
154ae 2a 2a 20 49 66 20 2a 70 52 43 20 69 73 20 69 6e  ** If *pRC is in
154af 69 74 69 61 6c 6c 79 20 6e 6f 6e 2d 7a 65 72 6f  itially non-zero
154b0 20 28 6e 6f 6e 2d 53 51 4c 49 54 45 5f 4f 4b 29   (non-SQLITE_OK)
154b1 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   then this routi
154b2 6e 65 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f 70  ne is.** a no-op
154b3 2e 20 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  .  If an error o
154b4 63 63 75 72 73 2c 20 74 68 65 20 61 70 70 72 6f  ccurs, the appro
154b5 70 72 69 61 74 65 20 65 72 72 6f 72 20 63 6f 64  priate error cod
154b6 65 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20  e is written.** 
154b7 69 6e 74 6f 20 2a 70 52 43 2e 0a 2a 2f 0a 73 74  into *pRC..*/.st
154b8 61 74 69 63 20 76 6f 69 64 20 70 74 72 6d 61 70  atic void ptrmap
154b9 50 75 74 28 42 74 53 68 61 72 65 64 20 2a 70 42  Put(BtShared *pB
154ba 74 2c 20 50 67 6e 6f 20 6b 65 79 2c 20 75 38 20  t, Pgno key, u8 
154bb 65 54 79 70 65 2c 20 50 67 6e 6f 20 70 61 72 65  eType, Pgno pare
154bc 6e 74 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a 20  nt, int *pRC){. 
154bd 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65   DbPage *pDbPage
154be 3b 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65  ;  /* The pointe
154bf 72 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 20  r map page */.  
154c0 75 38 20 2a 70 50 74 72 6d 61 70 3b 20 20 20 20  u8 *pPtrmap;    
154c1 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72    /* The pointer
154c2 20 6d 61 70 20 64 61 74 61 20 2a 2f 0a 20 20 50   map data */.  P
154c3 67 6e 6f 20 69 50 74 72 6d 61 70 3b 20 20 20 20  gno iPtrmap;    
154c4 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20   /* The pointer 
154c5 6d 61 70 20 70 61 67 65 20 6e 75 6d 62 65 72 20  map page number 
154c6 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 73 65 74 3b  */.  int offset;
154c7 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74         /* Offset
154c8 20 69 6e 20 70 6f 69 6e 74 65 72 20 6d 61 70 20   in pointer map 
154c9 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63  page */.  int rc
154ca 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52  ;           /* R
154cb 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20  eturn code from 
154cc 73 75 62 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a  subfunctions */.
154cd 0a 20 20 69 66 28 20 2a 70 52 43 20 29 20 72 65  .  if( *pRC ) re
154ce 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28  turn;..  assert(
154cf 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
154d0 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20  eld(pBt->mutex) 
154d1 29 3b 0a 20 20 2f 2a 20 54 68 65 20 6d 61 73 74  );.  /* The mast
154d2 65 72 2d 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20  er-journal page 
154d3 6e 75 6d 62 65 72 20 6d 75 73 74 20 6e 65 76 65  number must neve
154d4 72 20 62 65 20 75 73 65 64 20 61 73 20 61 20 70  r be used as a p
154d5 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20  ointer map page 
154d6 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 30 3d 3d  */.  assert( 0==
154d7 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42  PTRMAP_ISPAGE(pB
154d8 74 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  t, PENDING_BYTE_
154d9 50 41 47 45 28 70 42 74 29 29 20 29 3b 0a 0a 20  PAGE(pBt)) );.. 
154da 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75   assert( pBt->au
154db 74 6f 56 61 63 75 75 6d 20 29 3b 0a 20 20 69 66  toVacuum );.  if
154dc 28 20 6b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20  ( key==0 ){.    
154dd 2a 70 52 43 20 3d 20 53 51 4c 49 54 45 5f 43 4f  *pRC = SQLITE_CO
154de 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
154df 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 50  return;.  }.  iP
154e0 74 72 6d 61 70 20 3d 20 50 54 52 4d 41 50 5f 50  trmap = PTRMAP_P
154e1 41 47 45 4e 4f 28 70 42 74 2c 20 6b 65 79 29 3b  AGENO(pBt, key);
154e2 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50  .  rc = sqlite3P
154e3 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70 50 61  agerGet(pBt->pPa
154e4 67 65 72 2c 20 69 50 74 72 6d 61 70 2c 20 26 70  ger, iPtrmap, &p
154e5 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72  DbPage);.  if( r
154e6 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
154e7 20 20 20 20 2a 70 52 43 20 3d 20 72 63 3b 0a 20      *pRC = rc;. 
154e8 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
154e9 20 6f 66 66 73 65 74 20 3d 20 50 54 52 4d 41 50   offset = PTRMAP
154ea 5f 50 54 52 4f 46 46 53 45 54 28 69 50 74 72 6d  _PTROFFSET(iPtrm
154eb 61 70 2c 20 6b 65 79 29 3b 0a 20 20 69 66 28 20  ap, key);.  if( 
154ec 6f 66 66 73 65 74 3c 30 20 29 7b 0a 20 20 20 20  offset<0 ){.    
154ed 2a 70 52 43 20 3d 20 53 51 4c 49 54 45 5f 43 4f  *pRC = SQLITE_CO
154ee 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
154ef 67 6f 74 6f 20 70 74 72 6d 61 70 5f 65 78 69 74  goto ptrmap_exit
154f0 3b 0a 20 20 7d 0a 20 20 70 50 74 72 6d 61 70 20  ;.  }.  pPtrmap 
154f1 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 50  = (u8 *)sqlite3P
154f2 61 67 65 72 47 65 74 44 61 74 61 28 70 44 62 50  agerGetData(pDbP
154f3 61 67 65 29 3b 0a 0a 20 20 69 66 28 20 65 54 79  age);..  if( eTy
154f4 70 65 21 3d 70 50 74 72 6d 61 70 5b 6f 66 66 73  pe!=pPtrmap[offs
154f5 65 74 5d 20 7c 7c 20 67 65 74 34 62 79 74 65 28  et] || get4byte(
154f6 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b  &pPtrmap[offset+
154f7 31 5d 29 21 3d 70 61 72 65 6e 74 20 29 7b 0a 20  1])!=parent ){. 
154f8 20 20 20 54 52 41 43 45 28 28 22 50 54 52 4d 41     TRACE(("PTRMA
154f9 50 5f 55 50 44 41 54 45 3a 20 25 64 2d 3e 28 25  P_UPDATE: %d->(%
154fa 64 2c 25 64 29 5c 6e 22 2c 20 6b 65 79 2c 20 65  d,%d)\n", key, e
154fb 54 79 70 65 2c 20 70 61 72 65 6e 74 29 29 3b 0a  Type, parent));.
154fc 20 20 20 20 2a 70 52 43 3d 20 72 63 20 3d 20 73      *pRC= rc = s
154fd 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
154fe 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69  (pDbPage);.    i
154ff 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
15500 20 29 7b 0a 20 20 20 20 20 20 70 50 74 72 6d 61   ){.      pPtrma
15501 70 5b 6f 66 66 73 65 74 5d 20 3d 20 65 54 79 70  p[offset] = eTyp
15502 65 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74  e;.      put4byt
15503 65 28 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65  e(&pPtrmap[offse
15504 74 2b 31 5d 2c 20 70 61 72 65 6e 74 29 3b 0a 20  t+1], parent);. 
15505 20 20 20 7d 0a 20 20 7d 0a 0a 70 74 72 6d 61 70     }.  }..ptrmap
15506 5f 65 78 69 74 3a 0a 20 20 73 71 6c 69 74 65 33  _exit:.  sqlite3
15507 50 61 67 65 72 55 6e 72 65 66 28 70 44 62 50 61  PagerUnref(pDbPa
15508 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  ge);.}../*.** Re
15509 61 64 20 61 6e 20 65 6e 74 72 79 20 66 72 6f 6d  ad an entry from
1550a 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70   the pointer map
1550b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
1550c 74 69 6e 65 20 72 65 74 72 69 65 76 65 73 20 74  tine retrieves t
1550d 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65  he pointer map e
1550e 6e 74 72 79 20 66 6f 72 20 70 61 67 65 20 27 6b  ntry for page 'k
1550f 65 79 27 2c 20 77 72 69 74 69 6e 67 0a 2a 2a 20  ey', writing.** 
15510 74 68 65 20 74 79 70 65 20 61 6e 64 20 70 61 72  the type and par
15511 65 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20  ent page number 
15512 74 6f 20 2a 70 45 54 79 70 65 20 61 6e 64 20 2a  to *pEType and *
15513 70 50 67 6e 6f 20 72 65 73 70 65 63 74 69 76 65  pPgno respective
15514 6c 79 2e 0a 2a 2a 20 41 6e 20 65 72 72 6f 72 20  ly..** An error 
15515 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
15516 20 69 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f   if something go
15517 65 73 20 77 72 6f 6e 67 2c 20 6f 74 68 65 72 77  es wrong, otherw
15518 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a  ise SQLITE_OK..*
15519 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 72  /.static int ptr
1551a 6d 61 70 47 65 74 28 42 74 53 68 61 72 65 64 20  mapGet(BtShared 
1551b 2a 70 42 74 2c 20 50 67 6e 6f 20 6b 65 79 2c 20  *pBt, Pgno key, 
1551c 75 38 20 2a 70 45 54 79 70 65 2c 20 50 67 6e 6f  u8 *pEType, Pgno
1551d 20 2a 70 50 67 6e 6f 29 7b 0a 20 20 44 62 50 61   *pPgno){.  DbPa
1551e 67 65 20 2a 70 44 62 50 61 67 65 3b 20 20 20 2f  ge *pDbPage;   /
1551f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61  * The pointer ma
15520 70 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20  p page */.  int 
15521 69 50 74 72 6d 61 70 3b 20 20 20 20 20 20 20 2f  iPtrmap;       /
15522 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 70 61  * Pointer map pa
15523 67 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 75 38  ge index */.  u8
15524 20 2a 70 50 74 72 6d 61 70 3b 20 20 20 20 20 20   *pPtrmap;      
15525 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 20   /* Pointer map 
15526 70 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 69  page data */.  i
15527 6e 74 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20  nt offset;      
15528 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 65    /* Offset of e
15529 6e 74 72 79 20 69 6e 20 70 6f 69 6e 74 65 72 20  ntry in pointer 
1552a 6d 61 70 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b  map */.  int rc;
1552b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
1552c 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1552d 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20  Bt->mutex) );.. 
1552e 20 69 50 74 72 6d 61 70 20 3d 20 50 54 52 4d 41   iPtrmap = PTRMA
1552f 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 6b 65  P_PAGENO(pBt, ke
15530 79 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  y);.  rc = sqlit
15531 65 33 50 61 67 65 72 47 65 74 28 70 42 74 2d 3e  e3PagerGet(pBt->
15532 70 50 61 67 65 72 2c 20 69 50 74 72 6d 61 70 2c  pPager, iPtrmap,
15533 20 26 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66   &pDbPage);.  if
15534 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 72  ( rc!=0 ){.    r
15535 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20  eturn rc;.  }.  
15536 70 50 74 72 6d 61 70 20 3d 20 28 75 38 20 2a 29  pPtrmap = (u8 *)
15537 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44  sqlite3PagerGetD
15538 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a 0a 20  ata(pDbPage);.. 
15539 20 6f 66 66 73 65 74 20 3d 20 50 54 52 4d 41 50   offset = PTRMAP
1553a 5f 50 54 52 4f 46 46 53 45 54 28 69 50 74 72 6d  _PTROFFSET(iPtrm
1553b 61 70 2c 20 6b 65 79 29 3b 0a 20 20 61 73 73 65  ap, key);.  asse
1553c 72 74 28 20 70 45 54 79 70 65 21 3d 30 20 29 3b  rt( pEType!=0 );
1553d 0a 20 20 2a 70 45 54 79 70 65 20 3d 20 70 50 74  .  *pEType = pPt
1553e 72 6d 61 70 5b 6f 66 66 73 65 74 5d 3b 0a 20 20  rmap[offset];.  
1553f 69 66 28 20 70 50 67 6e 6f 20 29 20 2a 70 50 67  if( pPgno ) *pPg
15540 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  no = get4byte(&p
15541 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d  Ptrmap[offset+1]
15542 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 50 61 67  );..  sqlite3Pag
15543 65 72 55 6e 72 65 66 28 70 44 62 50 61 67 65 29  erUnref(pDbPage)
15544 3b 0a 20 20 69 66 28 20 2a 70 45 54 79 70 65 3c  ;.  if( *pEType<
15545 31 20 7c 7c 20 2a 70 45 54 79 70 65 3e 35 20 29  1 || *pEType>5 )
15546 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
15547 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 72  ORRUPT_BKPT;.  r
15548 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
15549 0a 7d 0a 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20  .}..#else /* if 
1554a 64 65 66 69 6e 65 64 20 53 51 4c 49 54 45 5f 4f  defined SQLITE_O
1554b 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 2a  MIT_AUTOVACUUM *
1554c 2f 0a 20 20 23 64 65 66 69 6e 65 20 70 74 72 6d  /.  #define ptrm
1554d 61 70 50 75 74 28 77 2c 78 2c 79 2c 7a 2c 72 63  apPut(w,x,y,z,rc
1554e 29 0a 20 20 23 64 65 66 69 6e 65 20 70 74 72 6d  ).  #define ptrm
1554f 61 70 47 65 74 28 77 2c 78 2c 79 2c 7a 29 20 53  apGet(w,x,y,z) S
15550 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69  QLITE_OK.  #defi
15551 6e 65 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c  ne ptrmapPutOvfl
15552 50 74 72 28 78 2c 20 79 2c 20 72 63 29 0a 23 65  Ptr(x, y, rc).#e
15553 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65  ndif../*.** Give
15554 6e 20 61 20 62 74 72 65 65 20 70 61 67 65 20 61  n a btree page a
15555 6e 64 20 61 20 63 65 6c 6c 20 69 6e 64 65 78 20  nd a cell index 
15556 28 30 20 6d 65 61 6e 73 20 74 68 65 20 66 69 72  (0 means the fir
15557 73 74 20 63 65 6c 6c 20 6f 6e 0a 2a 2a 20 74 68  st cell on.** th
15558 65 20 70 61 67 65 2c 20 31 20 6d 65 61 6e 73 20  e page, 1 means 
15559 74 68 65 20 73 65 63 6f 6e 64 20 63 65 6c 6c 2c  the second cell,
1555a 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 29 20 72   and so forth) r
1555b 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a  eturn a pointer.
1555c 2a 2a 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63  ** to the cell c
1555d 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  ontent..**.** Th
1555e 69 73 20 72 6f 75 74 69 6e 65 20 77 6f 72 6b 73  is routine works
1555f 20 6f 6e 6c 79 20 66 6f 72 20 70 61 67 65 73 20   only for pages 
15560 74 68 61 74 20 64 6f 20 6e 6f 74 20 63 6f 6e 74  that do not cont
15561 61 69 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c  ain overflow cel
15562 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 66  ls..*/.#define f
15563 69 6e 64 43 65 6c 6c 28 50 2c 49 29 20 5c 0a 20  indCell(P,I) \. 
15564 20 28 28 50 29 2d 3e 61 44 61 74 61 20 2b 20 28   ((P)->aData + (
15565 28 50 29 2d 3e 6d 61 73 6b 50 61 67 65 20 26 20  (P)->maskPage & 
15566 67 65 74 32 62 79 74 65 28 26 28 50 29 2d 3e 61  get2byte(&(P)->a
15567 44 61 74 61 5b 28 50 29 2d 3e 63 65 6c 6c 4f 66  Data[(P)->cellOf
15568 66 73 65 74 2b 32 2a 28 49 29 5d 29 29 29 0a 0a  fset+2*(I)])))..
15569 2f 2a 0a 2a 2a 20 54 68 69 73 20 61 20 6d 6f 72  /*.** This a mor
1556a 65 20 63 6f 6d 70 6c 65 78 20 76 65 72 73 69 6f  e complex versio
1556b 6e 20 6f 66 20 66 69 6e 64 43 65 6c 6c 28 29 20  n of findCell() 
1556c 74 68 61 74 20 77 6f 72 6b 73 20 66 6f 72 0a 2a  that works for.*
1556d 2a 20 70 61 67 65 73 20 74 68 61 74 20 64 6f 20  * pages that do 
1556e 63 6f 6e 74 61 69 6e 20 6f 76 65 72 66 6c 6f 77  contain overflow
1556f 20 63 65 6c 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69   cells..*/.stati
15570 63 20 75 38 20 2a 66 69 6e 64 4f 76 65 72 66 6c  c u8 *findOverfl
15571 6f 77 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20 2a  owCell(MemPage *
15572 70 50 61 67 65 2c 20 69 6e 74 20 69 43 65 6c 6c  pPage, int iCell
15573 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73  ){.  int i;.  as
15574 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
15575 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e  tex_held(pPage->
15576 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
15577 20 66 6f 72 28 69 3d 70 50 61 67 65 2d 3e 6e 4f   for(i=pPage->nO
15578 76 65 72 66 6c 6f 77 2d 31 3b 20 69 3e 3d 30 3b  verflow-1; i>=0;
15579 20 69 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 20 6b   i--){.    int k
1557a 3b 0a 20 20 20 20 73 74 72 75 63 74 20 5f 4f 76  ;.    struct _Ov
1557b 66 6c 43 65 6c 6c 20 2a 70 4f 76 66 6c 3b 0a 20  flCell *pOvfl;. 
1557c 20 20 20 70 4f 76 66 6c 20 3d 20 26 70 50 61 67     pOvfl = &pPag
1557d 65 2d 3e 61 4f 76 66 6c 5b 69 5d 3b 0a 20 20 20  e->aOvfl[i];.   
1557e 20 6b 20 3d 20 70 4f 76 66 6c 2d 3e 69 64 78 3b   k = pOvfl->idx;
1557f 0a 20 20 20 20 69 66 28 20 6b 3c 3d 69 43 65 6c  .    if( k<=iCel
15580 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6b  l ){.      if( k
15581 3d 3d 69 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20  ==iCell ){.     
15582 20 20 20 72 65 74 75 72 6e 20 70 4f 76 66 6c 2d     return pOvfl-
15583 3e 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a  >pCell;.      }.
15584 20 20 20 20 20 20 69 43 65 6c 6c 2d 2d 3b 0a 20        iCell--;. 
15585 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
15586 6e 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65  n findCell(pPage
15587 2c 20 69 43 65 6c 6c 29 3b 0a 7d 0a 0a 2f 2a 0a  , iCell);.}../*.
15588 2a 2a 20 50 61 72 73 65 20 61 20 63 65 6c 6c 20  ** Parse a cell 
15589 63 6f 6e 74 65 6e 74 20 62 6c 6f 63 6b 20 61 6e  content block an
1558a 64 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 43 65  d fill in the Ce
1558b 6c 6c 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  llInfo structure
1558c 2e 20 20 54 68 65 72 65 0a 2a 2a 20 61 72 65 20  .  There.** are 
1558d 74 77 6f 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  two versions of 
1558e 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20  this function.  
1558f 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 29  btreeParseCell()
15590 20 74 61 6b 65 73 20 61 20 0a 2a 2a 20 63 65 6c   takes a .** cel
15591 6c 20 69 6e 64 65 78 20 61 73 20 74 68 65 20 73  l index as the s
15592 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 61  econd argument a
15593 6e 64 20 62 74 72 65 65 50 61 72 73 65 43 65 6c  nd btreeParseCel
15594 6c 50 74 72 28 29 20 0a 2a 2a 20 74 61 6b 65 73  lPtr() .** takes
15595 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
15596 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20 63 65  e body of the ce
15597 6c 6c 20 61 73 20 69 74 73 20 73 65 63 6f 6e 64  ll as its second
15598 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a   argument..**.**
15599 20 57 69 74 68 69 6e 20 74 68 69 73 20 66 69 6c   Within this fil
1559a 65 2c 20 74 68 65 20 70 61 72 73 65 43 65 6c 6c  e, the parseCell
1559b 28 29 20 6d 61 63 72 6f 20 63 61 6e 20 62 65 20  () macro can be 
1559c 63 61 6c 6c 65 64 20 69 6e 73 74 65 61 64 20 6f  called instead o
1559d 66 0a 2a 2a 20 62 74 72 65 65 50 61 72 73 65 43  f.** btreeParseC
1559e 65 6c 6c 50 74 72 28 29 2e 20 55 73 69 6e 67 20  ellPtr(). Using 
1559f 73 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 73 2c 20  some compilers, 
155a0 74 68 69 73 20 77 69 6c 6c 20 62 65 20 66 61 73  this will be fas
155a1 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ter..*/.static v
155a2 6f 69 64 20 62 74 72 65 65 50 61 72 73 65 43 65  oid btreeParseCe
155a3 6c 6c 50 74 72 28 0a 20 20 4d 65 6d 50 61 67 65  llPtr(.  MemPage
155a4 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20 20   *pPage,        
155a5 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e   /* Page contain
155a6 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a  ing the cell */.
155a7 20 20 75 38 20 2a 70 43 65 6c 6c 2c 20 20 20 20    u8 *pCell,    
155a8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
155a9 6e 74 65 72 20 74 6f 20 74 68 65 20 63 65 6c 6c  nter to the cell
155aa 20 74 65 78 74 2e 20 2a 2f 0a 20 20 43 65 6c 6c   text. */.  Cell
155ab 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 20 20 20 20  Info *pInfo     
155ac 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74      /* Fill in t
155ad 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f  his structure */
155ae 0a 29 7b 0a 20 20 75 31 36 20 6e 3b 20 20 20 20  .){.  u16 n;    
155af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
155b0 20 4e 75 6d 62 65 72 20 62 79 74 65 73 20 69 6e   Number bytes in
155b1 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 65   cell content he
155b2 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 6e 50  ader */.  u32 nP
155b3 61 79 6c 6f 61 64 3b 20 20 20 20 20 20 20 20 20  ayload;         
155b4 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
155b5 79 74 65 73 20 6f 66 20 63 65 6c 6c 20 70 61 79  ytes of cell pay
155b6 6c 6f 61 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72  load */..  asser
155b7 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
155b8 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74  _held(pPage->pBt
155b9 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 70  ->mutex) );..  p
155ba 49 6e 66 6f 2d 3e 70 43 65 6c 6c 20 3d 20 70 43  Info->pCell = pC
155bb 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ell;.  assert( p
155bc 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 30 20 7c 7c  Page->leaf==0 ||
155bd 20 70 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 31 20   pPage->leaf==1 
155be 29 3b 0a 20 20 6e 20 3d 20 70 50 61 67 65 2d 3e  );.  n = pPage->
155bf 63 68 69 6c 64 50 74 72 53 69 7a 65 3b 0a 20 20  childPtrSize;.  
155c0 61 73 73 65 72 74 28 20 6e 3d 3d 34 2d 34 2a 70  assert( n==4-4*p
155c1 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20  Page->leaf );.  
155c2 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65  if( pPage->intKe
155c3 79 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61  y ){.    if( pPa
155c4 67 65 2d 3e 68 61 73 44 61 74 61 20 29 7b 0a 20  ge->hasData ){. 
155c5 20 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72       n += getVar
155c6 69 6e 74 33 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c  int32(&pCell[n],
155c7 20 6e 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20   nPayload);.    
155c8 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50 61  }else{.      nPa
155c9 79 6c 6f 61 64 20 3d 20 30 3b 0a 20 20 20 20 7d  yload = 0;.    }
155ca 0a 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72  .    n += getVar
155cb 69 6e 74 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 28  int(&pCell[n], (
155cc 75 36 34 2a 29 26 70 49 6e 66 6f 2d 3e 6e 4b 65  u64*)&pInfo->nKe
155cd 79 29 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e  y);.    pInfo->n
155ce 44 61 74 61 20 3d 20 6e 50 61 79 6c 6f 61 64 3b  Data = nPayload;
155cf 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49  .  }else{.    pI
155d0 6e 66 6f 2d 3e 6e 44 61 74 61 20 3d 20 30 3b 0a  nfo->nData = 0;.
155d1 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 69      n += getVari
155d2 6e 74 33 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20  nt32(&pCell[n], 
155d3 6e 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 70  nPayload);.    p
155d4 49 6e 66 6f 2d 3e 6e 4b 65 79 20 3d 20 6e 50 61  Info->nKey = nPa
155d5 79 6c 6f 61 64 3b 0a 20 20 7d 0a 20 20 70 49 6e  yload;.  }.  pIn
155d6 66 6f 2d 3e 6e 50 61 79 6c 6f 61 64 20 3d 20 6e  fo->nPayload = n
155d7 50 61 79 6c 6f 61 64 3b 0a 20 20 70 49 6e 66 6f  Payload;.  pInfo
155d8 2d 3e 6e 48 65 61 64 65 72 20 3d 20 6e 3b 0a 20  ->nHeader = n;. 
155d9 20 74 65 73 74 63 61 73 65 28 20 6e 50 61 79 6c   testcase( nPayl
155da 6f 61 64 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c  oad==pPage->maxL
155db 6f 63 61 6c 20 29 3b 0a 20 20 74 65 73 74 63 61  ocal );.  testca
155dc 73 65 28 20 6e 50 61 79 6c 6f 61 64 3d 3d 70 50  se( nPayload==pP
155dd 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 2b 31 20  age->maxLocal+1 
155de 29 3b 0a 20 20 69 66 28 20 6c 69 6b 65 6c 79 28  );.  if( likely(
155df 6e 50 61 79 6c 6f 61 64 3c 3d 70 50 61 67 65 2d  nPayload<=pPage-
155e0 3e 6d 61 78 4c 6f 63 61 6c 29 20 29 7b 0a 20 20  >maxLocal) ){.  
155e1 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65    /* This is the
155e2 20 28 65 61 73 79 29 20 63 6f 6d 6d 6f 6e 20 63   (easy) common c
155e3 61 73 65 20 77 68 65 72 65 20 74 68 65 20 65 6e  ase where the en
155e4 74 69 72 65 20 70 61 79 6c 6f 61 64 20 66 69 74  tire payload fit
155e5 73 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20  s.    ** on the 
155e6 6c 6f 63 61 6c 20 70 61 67 65 2e 20 20 4e 6f 20  local page.  No 
155e7 6f 76 65 72 66 6c 6f 77 20 69 73 20 72 65 71 75  overflow is requ
155e8 69 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ired..    */.   
155e9 20 69 6e 74 20 6e 53 69 7a 65 3b 20 20 20 20 20   int nSize;     
155ea 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73 69       /* Total si
155eb 7a 65 20 6f 66 20 63 65 6c 6c 20 63 6f 6e 74 65  ze of cell conte
155ec 6e 74 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20  nt in bytes */. 
155ed 20 20 20 6e 53 69 7a 65 20 3d 20 6e 50 61 79 6c     nSize = nPayl
155ee 6f 61 64 20 2b 20 6e 3b 0a 20 20 20 20 70 49 6e  oad + n;.    pIn
155ef 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31  fo->nLocal = (u1
155f0 36 29 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 20 20  6)nPayload;.    
155f1 70 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77  pInfo->iOverflow
155f2 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 28 6e   = 0;.    if( (n
155f3 53 69 7a 65 20 26 20 7e 33 29 3d 3d 30 20 29 7b  Size & ~3)==0 ){
155f4 0a 20 20 20 20 20 20 6e 53 69 7a 65 20 3d 20 34  .      nSize = 4
155f5 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69  ;        /* Mini
155f6 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 69 73  mum cell size is
155f7 20 34 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20   4 */.    }.    
155f8 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20 28  pInfo->nSize = (
155f9 75 31 36 29 6e 53 69 7a 65 3b 0a 20 20 7d 65 6c  u16)nSize;.  }el
155fa 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  se{.    /* If th
155fb 65 20 70 61 79 6c 6f 61 64 20 77 69 6c 6c 20 6e  e payload will n
155fc 6f 74 20 66 69 74 20 63 6f 6d 70 6c 65 74 65 6c  ot fit completel
155fd 79 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70  y on the local p
155fe 61 67 65 2c 20 77 65 20 68 61 76 65 0a 20 20 20  age, we have.   
155ff 20 2a 2a 20 74 6f 20 64 65 63 69 64 65 20 68 6f   ** to decide ho
15600 77 20 6d 75 63 68 20 74 6f 20 73 74 6f 72 65 20  w much to store 
15601 6c 6f 63 61 6c 6c 79 20 61 6e 64 20 68 6f 77 20  locally and how 
15602 6d 75 63 68 20 74 6f 20 73 70 69 6c 6c 20 6f 6e  much to spill on
15603 74 6f 0a 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c  to.    ** overfl
15604 6f 77 20 70 61 67 65 73 2e 20 20 54 68 65 20 73  ow pages.  The s
15605 74 72 61 74 65 67 79 20 69 73 20 74 6f 20 6d 69  trategy is to mi
15606 6e 69 6d 69 7a 65 20 74 68 65 20 61 6d 6f 75 6e  nimize the amoun
15607 74 20 6f 66 20 75 6e 75 73 65 64 0a 20 20 20 20  t of unused.    
15608 2a 2a 20 73 70 61 63 65 20 6f 6e 20 6f 76 65 72  ** space on over
15609 66 6c 6f 77 20 70 61 67 65 73 20 77 68 69 6c 65  flow pages while
1560a 20 6b 65 65 70 69 6e 67 20 74 68 65 20 61 6d 6f   keeping the amo
1560b 75 6e 74 20 6f 66 20 6c 6f 63 61 6c 20 73 74 6f  unt of local sto
1560c 72 61 67 65 0a 20 20 20 20 2a 2a 20 69 6e 20 62  rage.    ** in b
1560d 65 74 77 65 65 6e 20 6d 69 6e 4c 6f 63 61 6c 20  etween minLocal 
1560e 61 6e 64 20 6d 61 78 4c 6f 63 61 6c 2e 0a 20 20  and maxLocal..  
1560f 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 57 61 72 6e    **.    ** Warn
15610 69 6e 67 3a 20 20 63 68 61 6e 67 69 6e 67 20 74  ing:  changing t
15611 68 65 20 77 61 79 20 6f 76 65 72 66 6c 6f 77 20  he way overflow 
15612 70 61 79 6c 6f 61 64 20 69 73 20 64 69 73 74 72  payload is distr
15613 69 62 75 74 65 64 20 69 6e 20 61 6e 79 0a 20 20  ibuted in any.  
15614 20 20 2a 2a 20 77 61 79 20 77 69 6c 6c 20 72 65    ** way will re
15615 73 75 6c 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6d  sult in an incom
15616 70 61 74 69 62 6c 65 20 66 69 6c 65 20 66 6f 72  patible file for
15617 6d 61 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  mat..    */.    
15618 69 6e 74 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 2f  int minLocal;  /
15619 2a 20 4d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e 74  * Minimum amount
1561a 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64   of payload held
1561b 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 20 20   locally */.    
1561c 69 6e 74 20 6d 61 78 4c 6f 63 61 6c 3b 20 20 2f  int maxLocal;  /
1561d 2a 20 4d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74  * Maximum amount
1561e 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64   of payload held
1561f 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 20 20   locally */.    
15620 69 6e 74 20 73 75 72 70 6c 75 73 3b 20 20 20 2f  int surplus;   /
15621 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 79 6c 6f  * Overflow paylo
15622 61 64 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72  ad available for
15623 20 6c 6f 63 61 6c 20 73 74 6f 72 61 67 65 20 2a   local storage *
15624 2f 0a 0a 20 20 20 20 6d 69 6e 4c 6f 63 61 6c 20  /..    minLocal 
15625 3d 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61  = pPage->minLoca
15626 6c 3b 0a 20 20 20 20 6d 61 78 4c 6f 63 61 6c 20  l;.    maxLocal 
15627 3d 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61  = pPage->maxLoca
15628 6c 3b 0a 20 20 20 20 73 75 72 70 6c 75 73 20 3d  l;.    surplus =
15629 20 6d 69 6e 4c 6f 63 61 6c 20 2b 20 28 6e 50 61   minLocal + (nPa
1562a 79 6c 6f 61 64 20 2d 20 6d 69 6e 4c 6f 63 61 6c  yload - minLocal
1562b 29 25 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75  )%(pPage->pBt->u
1562c 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 29 3b 0a  sableSize - 4);.
1562d 20 20 20 20 74 65 73 74 63 61 73 65 28 20 73 75      testcase( su
1562e 72 70 6c 75 73 3d 3d 6d 61 78 4c 6f 63 61 6c 20  rplus==maxLocal 
1562f 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
15630 20 73 75 72 70 6c 75 73 3d 3d 6d 61 78 4c 6f 63   surplus==maxLoc
15631 61 6c 2b 31 20 29 3b 0a 20 20 20 20 69 66 28 20  al+1 );.    if( 
15632 73 75 72 70 6c 75 73 20 3c 3d 20 6d 61 78 4c 6f  surplus <= maxLo
15633 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 70 49 6e  cal ){.      pIn
15634 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31  fo->nLocal = (u1
15635 36 29 73 75 72 70 6c 75 73 3b 0a 20 20 20 20 7d  6)surplus;.    }
15636 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 49 6e 66  else{.      pInf
15637 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 36  o->nLocal = (u16
15638 29 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 7d  )minLocal;.    }
15639 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 69 4f 76 65  .    pInfo->iOve
1563a 72 66 6c 6f 77 20 3d 20 28 75 31 36 29 28 70 49  rflow = (u16)(pI
1563b 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 2b 20 6e 29  nfo->nLocal + n)
1563c 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 53 69  ;.    pInfo->nSi
1563d 7a 65 20 3d 20 70 49 6e 66 6f 2d 3e 69 4f 76 65  ze = pInfo->iOve
1563e 72 66 6c 6f 77 20 2b 20 34 3b 0a 20 20 7d 0a 7d  rflow + 4;.  }.}
1563f 0a 23 64 65 66 69 6e 65 20 70 61 72 73 65 43 65  .#define parseCe
15640 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 2c  ll(pPage, iCell,
15641 20 70 49 6e 66 6f 29 20 5c 0a 20 20 62 74 72 65   pInfo) \.  btre
15642 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 28 70  eParseCellPtr((p
15643 50 61 67 65 29 2c 20 66 69 6e 64 43 65 6c 6c 28  Page), findCell(
15644 28 70 50 61 67 65 29 2c 20 28 69 43 65 6c 6c 29  (pPage), (iCell)
15645 29 2c 20 28 70 49 6e 66 6f 29 29 0a 73 74 61 74  ), (pInfo)).stat
15646 69 63 20 76 6f 69 64 20 62 74 72 65 65 50 61 72  ic void btreePar
15647 73 65 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67  seCell(.  MemPag
15648 65 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20  e *pPage,       
15649 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69    /* Page contai
1564a 6e 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f  ning the cell */
1564b 0a 20 20 69 6e 74 20 69 43 65 6c 6c 2c 20 20 20  .  int iCell,   
1564c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1564d 65 20 63 65 6c 6c 20 69 6e 64 65 78 2e 20 20 46  e cell index.  F
1564e 69 72 73 74 20 63 65 6c 6c 20 69 73 20 30 20 2a  irst cell is 0 *
1564f 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 2a 70 49  /.  CellInfo *pI
15650 6e 66 6f 20 20 20 20 20 20 20 20 20 2f 2a 20 46  nfo         /* F
15651 69 6c 6c 20 69 6e 20 74 68 69 73 20 73 74 72 75  ill in this stru
15652 63 74 75 72 65 20 2a 2f 0a 29 7b 0a 20 20 70 61  cture */.){.  pa
15653 72 73 65 43 65 6c 6c 28 70 50 61 67 65 2c 20 69  rseCell(pPage, i
15654 43 65 6c 6c 2c 20 70 49 6e 66 6f 29 3b 0a 7d 0a  Cell, pInfo);.}.
15655 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74  ./*.** Compute t
15656 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20  he total number 
15657 6f 66 20 62 79 74 65 73 20 74 68 61 74 20 61 20  of bytes that a 
15658 43 65 6c 6c 20 6e 65 65 64 73 20 69 6e 20 74 68  Cell needs in th
15659 65 20 63 65 6c 6c 0a 2a 2a 20 64 61 74 61 20 61  e cell.** data a
1565a 72 65 61 20 6f 66 20 74 68 65 20 62 74 72 65 65  rea of the btree
1565b 2d 70 61 67 65 2e 20 20 54 68 65 20 72 65 74 75  -page.  The retu
1565c 72 6e 20 6e 75 6d 62 65 72 20 69 6e 63 6c 75 64  rn number includ
1565d 65 73 20 74 68 65 20 63 65 6c 6c 0a 2a 2a 20 64  es the cell.** d
1565e 61 74 61 20 68 65 61 64 65 72 20 61 6e 64 20 74  ata header and t
1565f 68 65 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64  he local payload
15660 2c 20 62 75 74 20 6e 6f 74 20 61 6e 79 20 6f 76  , but not any ov
15661 65 72 66 6c 6f 77 20 70 61 67 65 20 6f 72 0a 2a  erflow page or.*
15662 2a 20 74 68 65 20 73 70 61 63 65 20 75 73 65 64  * the space used
15663 20 62 79 20 74 68 65 20 63 65 6c 6c 20 70 6f 69   by the cell poi
15664 6e 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  nter..*/.static 
15665 75 31 36 20 63 65 6c 6c 53 69 7a 65 50 74 72 28  u16 cellSizePtr(
15666 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20  MemPage *pPage, 
15667 75 38 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 75 38  u8 *pCell){.  u8
15668 20 2a 70 49 74 65 72 20 3d 20 26 70 43 65 6c 6c   *pIter = &pCell
15669 5b 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72  [pPage->childPtr
1566a 53 69 7a 65 5d 3b 0a 20 20 75 33 32 20 6e 53 69  Size];.  u32 nSi
1566b 7a 65 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  ze;..#ifdef SQLI
1566c 54 45 5f 44 45 42 55 47 0a 20 20 2f 2a 20 54 68  TE_DEBUG.  /* Th
1566d 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  e value returned
1566e 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f   by this functio
1566f 6e 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20  n should always 
15670 62 65 20 74 68 65 20 73 61 6d 65 20 61 73 0a 20  be the same as. 
15671 20 2a 2a 20 74 68 65 20 28 43 65 6c 6c 49 6e 66   ** the (CellInf
15672 6f 2e 6e 53 69 7a 65 29 20 76 61 6c 75 65 20 66  o.nSize) value f
15673 6f 75 6e 64 20 62 79 20 64 6f 69 6e 67 20 61 20  ound by doing a 
15674 66 75 6c 6c 20 70 61 72 73 65 20 6f 66 20 74 68  full parse of th
15675 65 0a 20 20 2a 2a 20 63 65 6c 6c 2e 20 49 66 20  e.  ** cell. If 
15676 53 51 4c 49 54 45 5f 44 45 42 55 47 20 69 73 20  SQLITE_DEBUG is 
15677 64 65 66 69 6e 65 64 2c 20 61 6e 20 61 73 73 65  defined, an asse
15678 72 74 28 29 20 61 74 20 74 68 65 20 62 6f 74 74  rt() at the bott
15679 6f 6d 20 6f 66 0a 20 20 2a 2a 20 74 68 69 73 20  om of.  ** this 
1567a 66 75 6e 63 74 69 6f 6e 20 76 65 72 69 66 69 65  function verifie
1567b 73 20 74 68 61 74 20 74 68 69 73 20 69 6e 76 61  s that this inva
1567c 72 69 61 6e 74 20 69 73 20 6e 6f 74 20 76 69 6f  riant is not vio
1567d 6c 61 74 65 64 2e 20 2a 2f 0a 20 20 43 65 6c 6c  lated. */.  Cell
1567e 49 6e 66 6f 20 64 65 62 75 67 69 6e 66 6f 3b 0a  Info debuginfo;.
1567f 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c    btreeParseCell
15680 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c  Ptr(pPage, pCell
15681 2c 20 26 64 65 62 75 67 69 6e 66 6f 29 3b 0a 23  , &debuginfo);.#
15682 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 70 50 61  endif..  if( pPa
15683 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20  ge->intKey ){.  
15684 20 20 75 38 20 2a 70 45 6e 64 3b 0a 20 20 20 20    u8 *pEnd;.    
15685 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61  if( pPage->hasDa
15686 74 61 20 29 7b 0a 20 20 20 20 20 20 70 49 74 65  ta ){.      pIte
15687 72 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32  r += getVarint32
15688 28 70 49 74 65 72 2c 20 6e 53 69 7a 65 29 3b 0a  (pIter, nSize);.
15689 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1568a 20 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20   nSize = 0;.    
1568b 7d 0a 0a 20 20 20 20 2f 2a 20 70 49 74 65 72 20  }..    /* pIter 
1568c 6e 6f 77 20 70 6f 69 6e 74 73 20 61 74 20 74 68  now points at th
1568d 65 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72  e 64-bit integer
1568e 20 6b 65 79 20 76 61 6c 75 65 2c 20 61 20 76 61   key value, a va
1568f 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 0a 20  riable length . 
15690 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 2e 20 54     ** integer. T
15691 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f  he following blo
15692 63 6b 20 6d 6f 76 65 73 20 70 49 74 65 72 20 74  ck moves pIter t
15693 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 66  o point at the f
15694 69 72 73 74 20 62 79 74 65 0a 20 20 20 20 2a 2a  irst byte.    **
15695 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66   past the end of
15696 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e 20   the key value. 
15697 2a 2f 0a 20 20 20 20 70 45 6e 64 20 3d 20 26 70  */.    pEnd = &p
15698 49 74 65 72 5b 39 5d 3b 0a 20 20 20 20 77 68 69  Iter[9];.    whi
15699 6c 65 28 20 28 2a 70 49 74 65 72 2b 2b 29 26 30  le( (*pIter++)&0
1569a 78 38 30 20 26 26 20 70 49 74 65 72 3c 70 45 6e  x80 && pIter<pEn
1569b 64 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  d );.  }else{.  
1569c 20 20 70 49 74 65 72 20 2b 3d 20 67 65 74 56 61    pIter += getVa
1569d 72 69 6e 74 33 32 28 70 49 74 65 72 2c 20 6e 53  rint32(pIter, nS
1569e 69 7a 65 29 3b 0a 20 20 7d 0a 0a 20 20 74 65 73  ize);.  }..  tes
1569f 74 63 61 73 65 28 20 6e 53 69 7a 65 3d 3d 70 50  tcase( nSize==pP
156a0 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b  age->maxLocal );
156a1 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 53 69  .  testcase( nSi
156a2 7a 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f  ze==pPage->maxLo
156a3 63 61 6c 2b 31 20 29 3b 0a 20 20 69 66 28 20 6e  cal+1 );.  if( n
156a4 53 69 7a 65 3e 70 50 61 67 65 2d 3e 6d 61 78 4c  Size>pPage->maxL
156a5 6f 63 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74 20  ocal ){.    int 
156a6 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 50 61 67 65  minLocal = pPage
156a7 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20  ->minLocal;.    
156a8 6e 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f 63 61 6c  nSize = minLocal
156a9 20 2b 20 28 6e 53 69 7a 65 20 2d 20 6d 69 6e 4c   + (nSize - minL
156aa 6f 63 61 6c 29 20 25 20 28 70 50 61 67 65 2d 3e  ocal) % (pPage->
156ab 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20  pBt->usableSize 
156ac 2d 20 34 29 3b 0a 20 20 20 20 74 65 73 74 63 61  - 4);.    testca
156ad 73 65 28 20 6e 53 69 7a 65 3d 3d 70 50 61 67 65  se( nSize==pPage
156ae 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 20  ->maxLocal );.  
156af 20 20 74 65 73 74 63 61 73 65 28 20 6e 53 69 7a    testcase( nSiz
156b0 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63  e==pPage->maxLoc
156b1 61 6c 2b 31 20 29 3b 0a 20 20 20 20 69 66 28 20  al+1 );.    if( 
156b2 6e 53 69 7a 65 3e 70 50 61 67 65 2d 3e 6d 61 78  nSize>pPage->max
156b3 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 6e  Local ){.      n
156b4 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f 63 61 6c 3b  Size = minLocal;
156b5 0a 20 20 20 20 7d 0a 20 20 20 20 6e 53 69 7a 65  .    }.    nSize
156b6 20 2b 3d 20 34 3b 0a 20 20 7d 0a 20 20 6e 53 69   += 4;.  }.  nSi
156b7 7a 65 20 2b 3d 20 28 75 33 32 29 28 70 49 74 65  ze += (u32)(pIte
156b8 72 20 2d 20 70 43 65 6c 6c 29 3b 0a 0a 20 20 2f  r - pCell);..  /
156b9 2a 20 54 68 65 20 6d 69 6e 69 6d 75 6d 20 73 69  * The minimum si
156ba 7a 65 20 6f 66 20 61 6e 79 20 63 65 6c 6c 20 69  ze of any cell i
156bb 73 20 34 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20  s 4 bytes. */.  
156bc 69 66 28 20 6e 53 69 7a 65 3c 34 20 29 7b 0a 20  if( nSize<4 ){. 
156bd 20 20 20 6e 53 69 7a 65 20 3d 20 34 3b 0a 20 20     nSize = 4;.  
156be 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 53 69  }..  assert( nSi
156bf 7a 65 3d 3d 64 65 62 75 67 69 6e 66 6f 2e 6e 53  ze==debuginfo.nS
156c0 69 7a 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  ize );.  return 
156c1 28 75 31 36 29 6e 53 69 7a 65 3b 0a 7d 0a 23 69  (u16)nSize;.}.#i
156c2 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 74 61  fndef NDEBUG.sta
156c3 74 69 63 20 75 31 36 20 63 65 6c 6c 53 69 7a 65  tic u16 cellSize
156c4 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c  (MemPage *pPage,
156c5 20 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20 72   int iCell){.  r
156c6 65 74 75 72 6e 20 63 65 6c 6c 53 69 7a 65 50 74  eturn cellSizePt
156c7 72 28 70 50 61 67 65 2c 20 66 69 6e 64 43 65 6c  r(pPage, findCel
156c8 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 29 29  l(pPage, iCell))
156c9 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e  ;.}.#endif..#ifn
156ca 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
156cb 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a  AUTOVACUUM./*.**
156cc 20 49 66 20 74 68 65 20 63 65 6c 6c 20 70 43 65   If the cell pCe
156cd 6c 6c 2c 20 70 61 72 74 20 6f 66 20 70 61 67 65  ll, part of page
156ce 20 70 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 20   pPage contains 
156cf 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20  a pointer.** to 
156d0 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  an overflow page
156d1 2c 20 69 6e 73 65 72 74 20 61 6e 20 65 6e 74 72  , insert an entr
156d2 79 20 69 6e 74 6f 20 74 68 65 20 70 6f 69 6e 74  y into the point
156d3 65 72 2d 6d 61 70 0a 2a 2a 20 66 6f 72 20 74 68  er-map.** for th
156d4 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e  e overflow page.
156d5 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
156d6 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72  ptrmapPutOvflPtr
156d7 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c  (MemPage *pPage,
156d8 20 75 38 20 2a 70 43 65 6c 6c 2c 20 69 6e 74 20   u8 *pCell, int 
156d9 2a 70 52 43 29 7b 0a 20 20 43 65 6c 6c 49 6e 66  *pRC){.  CellInf
156da 6f 20 69 6e 66 6f 3b 0a 20 20 69 66 28 20 2a 70  o info;.  if( *p
156db 52 43 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61  RC ) return;.  a
156dc 73 73 65 72 74 28 20 70 43 65 6c 6c 21 3d 30 20  ssert( pCell!=0 
156dd 29 3b 0a 20 20 62 74 72 65 65 50 61 72 73 65 43  );.  btreeParseC
156de 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43  ellPtr(pPage, pC
156df 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 61  ell, &info);.  a
156e0 73 73 65 72 74 28 20 28 69 6e 66 6f 2e 6e 44 61  ssert( (info.nDa
156e1 74 61 2b 28 70 50 61 67 65 2d 3e 69 6e 74 4b 65  ta+(pPage->intKe
156e2 79 3f 30 3a 69 6e 66 6f 2e 6e 4b 65 79 29 29 3d  y?0:info.nKey))=
156e3 3d 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 29  =info.nPayload )
156e4 3b 0a 20 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76  ;.  if( info.iOv
156e5 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 50 67  erflow ){.    Pg
156e6 6e 6f 20 6f 76 66 6c 20 3d 20 67 65 74 34 62 79  no ovfl = get4by
156e7 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69  te(&pCell[info.i
156e8 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 20 20  Overflow]);.    
156e9 70 74 72 6d 61 70 50 75 74 28 70 50 61 67 65 2d  ptrmapPut(pPage-
156ea 3e 70 42 74 2c 20 6f 76 66 6c 2c 20 50 54 52 4d  >pBt, ovfl, PTRM
156eb 41 50 5f 4f 56 45 52 46 4c 4f 57 31 2c 20 70 50  AP_OVERFLOW1, pP
156ec 61 67 65 2d 3e 70 67 6e 6f 2c 20 70 52 43 29 3b  age->pgno, pRC);
156ed 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a  .  }.}.#endif...
156ee 2f 2a 0a 2a 2a 20 44 65 66 72 61 67 6d 65 6e 74  /*.** Defragment
156ef 20 74 68 65 20 70 61 67 65 20 67 69 76 65 6e 2e   the page given.
156f0 20 20 41 6c 6c 20 43 65 6c 6c 73 20 61 72 65 20    All Cells are 
156f1 6d 6f 76 65 64 20 74 6f 20 74 68 65 0a 2a 2a 20  moved to the.** 
156f2 65 6e 64 20 6f 66 20 74 68 65 20 70 61 67 65 20  end of the page 
156f3 61 6e 64 20 61 6c 6c 20 66 72 65 65 20 73 70 61  and all free spa
156f4 63 65 20 69 73 20 63 6f 6c 6c 65 63 74 65 64 20  ce is collected 
156f5 69 6e 74 6f 20 6f 6e 65 0a 2a 2a 20 62 69 67 20  into one.** big 
156f6 46 72 65 65 42 6c 6b 20 74 68 61 74 20 6f 63 63  FreeBlk that occ
156f7 75 72 73 20 69 6e 20 62 65 74 77 65 65 6e 20 74  urs in between t
156f8 68 65 20 68 65 61 64 65 72 20 61 6e 64 20 63 65  he header and ce
156f9 6c 6c 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 61 72  ll.** pointer ar
156fa 72 61 79 20 61 6e 64 20 74 68 65 20 63 65 6c 6c  ray and the cell
156fb 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e 0a 2a   content area..*
156fc 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 66  /.static int def
156fd 72 61 67 6d 65 6e 74 50 61 67 65 28 4d 65 6d 50  ragmentPage(MemP
156fe 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 69  age *pPage){.  i
156ff 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
15700 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
15701 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69  p counter */.  i
15702 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20 20 20  nt pc;          
15703 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64            /* Add
15704 72 65 73 73 20 6f 66 20 61 20 69 2d 74 68 20 63  ress of a i-th c
15705 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 68 64 72  ell */.  int hdr
15706 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
15707 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f      /* Offset to
15708 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72   the page header
15709 20 2a 2f 0a 20 20 69 6e 74 20 73 69 7a 65 3b 20   */.  int size; 
1570a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1570b 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 20 63 65   /* Size of a ce
1570c 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 75 73 61 62  ll */.  int usab
1570d 6c 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20  leSize;         
1570e 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1570f 75 73 61 62 6c 65 20 62 79 74 65 73 20 6f 6e 20  usable bytes on 
15710 61 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20  a page */.  int 
15711 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20 20 20 20  cellOffset;     
15712 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74         /* Offset
15713 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 70 6f 69   to the cell poi
15714 6e 74 65 72 20 61 72 72 61 79 20 2a 2f 0a 20 20  nter array */.  
15715 69 6e 74 20 63 62 72 6b 3b 20 20 20 20 20 20 20  int cbrk;       
15716 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66             /* Of
15717 66 73 65 74 20 74 6f 20 74 68 65 20 63 65 6c 6c  fset to the cell
15718 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f   content area */
15719 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 20 20 20  .  int nCell;   
1571a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1571b 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73   Number of cells
1571c 20 6f 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0a   on the page */.
1571d 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
1571e 2a 64 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20  *data;       /* 
1571f 54 68 65 20 70 61 67 65 20 64 61 74 61 20 2a 2f  The page data */
15720 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
15721 20 2a 74 65 6d 70 3b 20 20 20 20 20 20 20 2f 2a   *temp;       /*
15722 20 54 65 6d 70 20 61 72 65 61 20 66 6f 72 20 63   Temp area for c
15723 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20  ell content */. 
15724 20 69 6e 74 20 69 43 65 6c 6c 46 69 72 73 74 3b   int iCellFirst;
15725 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
15726 69 72 73 74 20 61 6c 6c 6f 77 61 62 6c 65 20 63  irst allowable c
15727 65 6c 6c 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69  ell index */.  i
15728 6e 74 20 69 43 65 6c 6c 4c 61 73 74 3b 20 20 20  nt iCellLast;   
15729 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73            /* Las
1572a 74 20 70 6f 73 73 69 62 6c 65 20 63 65 6c 6c 20  t possible cell 
1572b 69 6e 64 65 78 20 2a 2f 0a 0a 0a 20 20 61 73 73  index */...  ass
1572c 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65  ert( sqlite3Page
1572d 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61  rIswriteable(pPa
1572e 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a  ge->pDbPage) );.
1572f 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
15730 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73  >pBt!=0 );.  ass
15731 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 2d  ert( pPage->pBt-
15732 3e 75 73 61 62 6c 65 53 69 7a 65 20 3c 3d 20 53  >usableSize <= S
15733 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53  QLITE_MAX_PAGE_S
15734 49 5a 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28  IZE );.  assert(
15735 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f   pPage->nOverflo
15736 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  w==0 );.  assert
15737 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
15738 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d  held(pPage->pBt-
15739 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 74 65 6d  >mutex) );.  tem
1573a 70 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  p = sqlite3Pager
1573b 54 65 6d 70 53 70 61 63 65 28 70 50 61 67 65 2d  TempSpace(pPage-
1573c 3e 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20  >pBt->pPager);. 
1573d 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61   data = pPage->a
1573e 44 61 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50  Data;.  hdr = pP
1573f 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a  age->hdrOffset;.
15740 20 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 70    cellOffset = p
15741 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74  Page->cellOffset
15742 3b 0a 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67  ;.  nCell = pPag
15743 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 61 73 73 65  e->nCell;.  asse
15744 72 74 28 20 6e 43 65 6c 6c 3d 3d 67 65 74 32 62  rt( nCell==get2b
15745 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d  yte(&data[hdr+3]
15746 29 20 29 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a  ) );.  usableSiz
15747 65 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e  e = pPage->pBt->
15748 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 63 62  usableSize;.  cb
15749 72 6b 20 3d 20 67 65 74 32 62 79 74 65 28 26 64  rk = get2byte(&d
1574a 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 6d  ata[hdr+5]);.  m
1574b 65 6d 63 70 79 28 26 74 65 6d 70 5b 63 62 72 6b  emcpy(&temp[cbrk
1574c 5d 2c 20 26 64 61 74 61 5b 63 62 72 6b 5d 2c 20  ], &data[cbrk], 
1574d 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 63 62 72  usableSize - cbr
1574e 6b 29 3b 0a 20 20 63 62 72 6b 20 3d 20 75 73 61  k);.  cbrk = usa
1574f 62 6c 65 53 69 7a 65 3b 0a 20 20 69 43 65 6c 6c  bleSize;.  iCell
15750 46 69 72 73 74 20 3d 20 63 65 6c 6c 4f 66 66 73  First = cellOffs
15751 65 74 20 2b 20 32 2a 6e 43 65 6c 6c 3b 0a 20 20  et + 2*nCell;.  
15752 69 43 65 6c 6c 4c 61 73 74 20 3d 20 75 73 61 62  iCellLast = usab
15753 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20 66 6f  leSize - 4;.  fo
15754 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20  r(i=0; i<nCell; 
15755 69 2b 2b 29 7b 0a 20 20 20 20 75 38 20 2a 70 41  i++){.    u8 *pA
15756 64 64 72 3b 20 20 20 20 20 2f 2a 20 54 68 65 20  ddr;     /* The 
15757 69 2d 74 68 20 63 65 6c 6c 20 70 6f 69 6e 74 65  i-th cell pointe
15758 72 20 2a 2f 0a 20 20 20 20 70 41 64 64 72 20 3d  r */.    pAddr =
15759 20 26 64 61 74 61 5b 63 65 6c 6c 4f 66 66 73 65   &data[cellOffse
1575a 74 20 2b 20 69 2a 32 5d 3b 0a 20 20 20 20 70 63  t + i*2];.    pc
1575b 20 3d 20 67 65 74 32 62 79 74 65 28 70 41 64 64   = get2byte(pAdd
1575c 72 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65  r);.    testcase
1575d 28 20 70 63 3d 3d 69 43 65 6c 6c 46 69 72 73 74  ( pc==iCellFirst
1575e 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65   );.    testcase
1575f 28 20 70 63 3d 3d 69 43 65 6c 6c 4c 61 73 74 20  ( pc==iCellLast 
15760 29 3b 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  );.#if !defined(
15761 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4f 56  SQLITE_ENABLE_OV
15762 45 52 53 49 5a 45 5f 43 45 4c 4c 5f 43 48 45 43  ERSIZE_CELL_CHEC
15763 4b 29 0a 20 20 20 20 2f 2a 20 54 68 65 73 65 20  K).    /* These 
15764 63 6f 6e 64 69 74 69 6f 6e 73 20 68 61 76 65 20  conditions have 
15765 61 6c 72 65 61 64 79 20 62 65 65 6e 20 76 65 72  already been ver
15766 69 66 69 65 64 20 69 6e 20 62 74 72 65 65 49 6e  ified in btreeIn
15767 69 74 50 61 67 65 28 29 0a 20 20 20 20 2a 2a 20  itPage().    ** 
15768 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  if SQLITE_ENABLE
15769 5f 4f 56 45 52 53 49 5a 45 5f 43 45 4c 4c 5f 43  _OVERSIZE_CELL_C
1576a 48 45 43 4b 20 69 73 20 64 65 66 69 6e 65 64 20  HECK is defined 
1576b 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
1576c 70 63 3c 69 43 65 6c 6c 46 69 72 73 74 20 7c 7c  pc<iCellFirst ||
1576d 20 70 63 3e 69 43 65 6c 6c 4c 61 73 74 20 29 7b   pc>iCellLast ){
1576e 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
1576f 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
15770 54 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  T;.    }.#endif.
15771 20 20 20 20 61 73 73 65 72 74 28 20 70 63 3e 3d      assert( pc>=
15772 69 43 65 6c 6c 46 69 72 73 74 20 26 26 20 70 63  iCellFirst && pc
15773 3c 3d 69 43 65 6c 6c 4c 61 73 74 20 29 3b 0a 20  <=iCellLast );. 
15774 20 20 20 73 69 7a 65 20 3d 20 63 65 6c 6c 53 69     size = cellSi
15775 7a 65 50 74 72 28 70 50 61 67 65 2c 20 26 74 65  zePtr(pPage, &te
15776 6d 70 5b 70 63 5d 29 3b 0a 20 20 20 20 63 62 72  mp[pc]);.    cbr
15777 6b 20 2d 3d 20 73 69 7a 65 3b 0a 23 69 66 20 64  k -= size;.#if d
15778 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
15779 41 42 4c 45 5f 4f 56 45 52 53 49 5a 45 5f 43 45  ABLE_OVERSIZE_CE
1577a 4c 4c 5f 43 48 45 43 4b 29 0a 20 20 20 20 69 66  LL_CHECK).    if
1577b 28 20 63 62 72 6b 3c 69 43 65 6c 6c 46 69 72 73  ( cbrk<iCellFirs
1577c 74 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  t ){.      retur
1577d 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
1577e 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 23 65 6c  _BKPT;.    }.#el
1577f 73 65 0a 20 20 20 20 69 66 28 20 63 62 72 6b 3c  se.    if( cbrk<
15780 69 43 65 6c 6c 46 69 72 73 74 20 7c 7c 20 70 63  iCellFirst || pc
15781 2b 73 69 7a 65 3e 75 73 61 62 6c 65 53 69 7a 65  +size>usableSize
15782 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
15783 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
15784 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  BKPT;.    }.#end
15785 69 66 0a 20 20 20 20 61 73 73 65 72 74 28 20 63  if.    assert( c
15786 62 72 6b 2b 73 69 7a 65 3c 3d 75 73 61 62 6c 65  brk+size<=usable
15787 53 69 7a 65 20 26 26 20 63 62 72 6b 3e 3d 69 43  Size && cbrk>=iC
15788 65 6c 6c 46 69 72 73 74 20 29 3b 0a 20 20 20 20  ellFirst );.    
15789 74 65 73 74 63 61 73 65 28 20 63 62 72 6b 2b 73  testcase( cbrk+s
1578a 69 7a 65 3d 3d 75 73 61 62 6c 65 53 69 7a 65 20  ize==usableSize 
1578b 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
1578c 20 70 63 2b 73 69 7a 65 3d 3d 75 73 61 62 6c 65   pc+size==usable
1578d 53 69 7a 65 20 29 3b 0a 20 20 20 20 6d 65 6d 63  Size );.    memc
1578e 70 79 28 26 64 61 74 61 5b 63 62 72 6b 5d 2c 20  py(&data[cbrk], 
1578f 26 74 65 6d 70 5b 70 63 5d 2c 20 73 69 7a 65 29  &temp[pc], size)
15790 3b 0a 20 20 20 20 70 75 74 32 62 79 74 65 28 70  ;.    put2byte(p
15791 41 64 64 72 2c 20 63 62 72 6b 29 3b 0a 20 20 7d  Addr, cbrk);.  }
15792 0a 20 20 61 73 73 65 72 74 28 20 63 62 72 6b 3e  .  assert( cbrk>
15793 3d 69 43 65 6c 6c 46 69 72 73 74 20 29 3b 0a 20  =iCellFirst );. 
15794 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b   put2byte(&data[
15795 68 64 72 2b 35 5d 2c 20 63 62 72 6b 29 3b 0a 20  hdr+5], cbrk);. 
15796 20 64 61 74 61 5b 68 64 72 2b 31 5d 20 3d 20 30   data[hdr+1] = 0
15797 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 32 5d 20  ;.  data[hdr+2] 
15798 3d 20 30 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b  = 0;.  data[hdr+
15799 37 5d 20 3d 20 30 3b 0a 20 20 6d 65 6d 73 65 74  7] = 0;.  memset
1579a 28 26 64 61 74 61 5b 69 43 65 6c 6c 46 69 72 73  (&data[iCellFirs
1579b 74 5d 2c 20 30 2c 20 63 62 72 6b 2d 69 43 65 6c  t], 0, cbrk-iCel
1579c 6c 46 69 72 73 74 29 3b 0a 20 20 61 73 73 65 72  lFirst);.  asser
1579d 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  t( sqlite3PagerI
1579e 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65  swriteable(pPage
1579f 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20  ->pDbPage) );.  
157a0 69 66 28 20 63 62 72 6b 2d 69 43 65 6c 6c 46 69  if( cbrk-iCellFi
157a1 72 73 74 21 3d 70 50 61 67 65 2d 3e 6e 46 72 65  rst!=pPage->nFre
157a2 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  e ){.    return 
157a3 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
157a4 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  KPT;.  }.  retur
157a5 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
157a6 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e  /*.** Allocate n
157a7 42 79 74 65 20 62 79 74 65 73 20 6f 66 20 73 70  Byte bytes of sp
157a8 61 63 65 20 66 72 6f 6d 20 77 69 74 68 69 6e 20  ace from within 
157a9 74 68 65 20 42 2d 54 72 65 65 20 70 61 67 65 20  the B-Tree page 
157aa 70 61 73 73 65 64 0a 2a 2a 20 61 73 20 74 68 65  passed.** as the
157ab 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e   first argument.
157ac 20 57 72 69 74 65 20 69 6e 74 6f 20 2a 70 49 64   Write into *pId
157ad 78 20 74 68 65 20 69 6e 64 65 78 20 69 6e 74 6f  x the index into
157ae 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d 0a   pPage->aData[].
157af 2a 2a 20 6f 66 20 74 68 65 20 66 69 72 73 74 20  ** of the first 
157b0 62 79 74 65 20 6f 66 20 61 6c 6c 6f 63 61 74 65  byte of allocate
157b1 64 20 73 70 61 63 65 2e 20 52 65 74 75 72 6e 20  d space. Return 
157b2 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4f 4b  either SQLITE_OK
157b3 20 6f 72 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20   or.** an error 
157b4 63 6f 64 65 20 28 75 73 75 61 6c 6c 79 20 53 51  code (usually SQ
157b5 4c 49 54 45 5f 43 4f 52 52 55 50 54 29 2e 0a 2a  LITE_CORRUPT)..*
157b6 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20  *.** The caller 
157b7 67 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 20  guarantees that 
157b8 74 68 65 72 65 20 69 73 20 73 75 66 66 69 63 69  there is suffici
157b9 65 6e 74 20 73 70 61 63 65 20 74 6f 20 6d 61 6b  ent space to mak
157ba 65 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74  e the.** allocat
157bb 69 6f 6e 2e 20 20 54 68 69 73 20 72 6f 75 74 69  ion.  This routi
157bc 6e 65 20 6d 69 67 68 74 20 6e 65 65 64 20 74 6f  ne might need to
157bd 20 64 65 66 72 61 67 6d 65 6e 74 20 69 6e 20 6f   defragment in o
157be 72 64 65 72 20 74 6f 20 62 72 69 6e 67 0a 2a 2a  rder to bring.**
157bf 20 61 6c 6c 20 74 68 65 20 73 70 61 63 65 20 74   all the space t
157c0 6f 67 65 74 68 65 72 2c 20 68 6f 77 65 76 65 72  ogether, however
157c1 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
157c2 77 69 6c 6c 20 61 76 6f 69 64 20 75 73 69 6e 67  will avoid using
157c3 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 74 77  .** the first tw
157c4 6f 20 62 79 74 65 73 20 70 61 73 74 20 74 68 65  o bytes past the
157c5 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72   cell pointer ar
157c6 65 61 20 73 69 6e 63 65 20 70 72 65 73 75 6d 61  ea since presuma
157c7 62 6c 79 20 74 68 69 73 0a 2a 2a 20 61 6c 6c 6f  bly this.** allo
157c8 63 61 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20  cation is being 
157c9 6d 61 64 65 20 69 6e 20 6f 72 64 65 72 20 74 6f  made in order to
157ca 20 69 6e 73 65 72 74 20 61 20 6e 65 77 20 63 65   insert a new ce
157cb 6c 6c 2c 20 73 6f 20 77 65 20 77 69 6c 6c 0a 2a  ll, so we will.*
157cc 2a 20 61 6c 73 6f 20 65 6e 64 20 75 70 20 6e 65  * also end up ne
157cd 65 64 69 6e 67 20 61 20 6e 65 77 20 63 65 6c 6c  eding a new cell
157ce 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 73 74 61   pointer..*/.sta
157cf 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65  tic int allocate
157d0 53 70 61 63 65 28 4d 65 6d 50 61 67 65 20 2a 70  Space(MemPage *p
157d1 50 61 67 65 2c 20 69 6e 74 20 6e 42 79 74 65 2c  Page, int nByte,
157d2 20 69 6e 74 20 2a 70 49 64 78 29 7b 0a 20 20 63   int *pIdx){.  c
157d3 6f 6e 73 74 20 69 6e 74 20 68 64 72 20 3d 20 70  onst int hdr = p
157d4 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b  Page->hdrOffset;
157d5 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 63 61 63      /* Local cac
157d6 68 65 20 6f 66 20 70 50 61 67 65 2d 3e 68 64 72  he of pPage->hdr
157d7 4f 66 66 73 65 74 20 2a 2f 0a 20 20 75 38 20 2a  Offset */.  u8 *
157d8 20 63 6f 6e 73 74 20 64 61 74 61 20 3d 20 70 50   const data = pP
157d9 61 67 65 2d 3e 61 44 61 74 61 3b 20 20 20 20 20  age->aData;     
157da 20 2f 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65 20   /* Local cache 
157db 6f 66 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20  of pPage->aData 
157dc 2a 2f 0a 20 20 69 6e 74 20 6e 46 72 61 67 3b 20  */.  int nFrag; 
157dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
157de 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
157df 62 65 72 20 6f 66 20 66 72 61 67 6d 65 6e 74 65  ber of fragmente
157e0 64 20 62 79 74 65 73 20 6f 6e 20 70 50 61 67 65  d bytes on pPage
157e1 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 70 3b 20 20   */.  int top;  
157e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
157e3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
157e4 72 73 74 20 62 79 74 65 20 6f 66 20 63 65 6c 6c  rst byte of cell
157e5 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f   content area */
157e6 0a 20 20 69 6e 74 20 67 61 70 3b 20 20 20 20 20  .  int gap;     
157e7 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65     /* First byte
157e8 20 6f 66 20 67 61 70 20 62 65 74 77 65 65 6e 20   of gap between 
157e9 63 65 6c 6c 20 70 6f 69 6e 74 65 72 73 20 61 6e  cell pointers an
157ea 64 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 2a  d cell content *
157eb 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  /.  int rc;     
157ec 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 72      /* Integer r
157ed 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
157ee 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
157ef 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
157f0 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  le(pPage->pDbPag
157f1 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  e) );.  assert( 
157f2 70 50 61 67 65 2d 3e 70 42 74 20 29 3b 0a 20 20  pPage->pBt );.  
157f3 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
157f4 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65  mutex_held(pPage
157f5 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  ->pBt->mutex) );
157f6 0a 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 65  .  assert( nByte
157f7 3e 3d 30 20 29 3b 20 20 2f 2a 20 4d 69 6e 69 6d  >=0 );  /* Minim
157f8 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 69 73 20  um cell size is 
157f9 34 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  4 */.  assert( p
157fa 50 61 67 65 2d 3e 6e 46 72 65 65 3e 3d 6e 42 79  Page->nFree>=nBy
157fb 74 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  te );.  assert( 
157fc 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77  pPage->nOverflow
157fd 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
157fe 20 6e 42 79 74 65 3c 70 50 61 67 65 2d 3e 70 42   nByte<pPage->pB
157ff 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 38 20  t->usableSize-8 
15800 29 3b 0a 0a 20 20 6e 46 72 61 67 20 3d 20 64 61  );..  nFrag = da
15801 74 61 5b 68 64 72 2b 37 5d 3b 0a 20 20 61 73 73  ta[hdr+7];.  ass
15802 65 72 74 28 20 70 50 61 67 65 2d 3e 63 65 6c 6c  ert( pPage->cell
15803 4f 66 66 73 65 74 20 3d 3d 20 68 64 72 20 2b 20  Offset == hdr + 
15804 31 32 20 2d 20 34 2a 70 50 61 67 65 2d 3e 6c 65  12 - 4*pPage->le
15805 61 66 20 29 3b 0a 20 20 67 61 70 20 3d 20 70 50  af );.  gap = pP
15806 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20  age->cellOffset 
15807 2b 20 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  + 2*pPage->nCell
15808 3b 0a 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79  ;.  top = get2by
15809 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29  te(&data[hdr+5])
1580a 3b 0a 20 20 69 66 28 20 67 61 70 3e 74 6f 70 20  ;.  if( gap>top 
1580b 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
1580c 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
1580d 74 65 73 74 63 61 73 65 28 20 67 61 70 2b 32 3d  testcase( gap+2=
1580e 3d 74 6f 70 20 29 3b 0a 20 20 74 65 73 74 63 61  =top );.  testca
1580f 73 65 28 20 67 61 70 2b 31 3d 3d 74 6f 70 20 29  se( gap+1==top )
15810 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 67 61  ;.  testcase( ga
15811 70 3d 3d 74 6f 70 20 29 3b 0a 0a 20 20 69 66 28  p==top );..  if(
15812 20 6e 46 72 61 67 3e 3d 36 30 20 29 7b 0a 20 20   nFrag>=60 ){.  
15813 20 20 2f 2a 20 41 6c 77 61 79 73 20 64 65 66 72    /* Always defr
15814 61 67 6d 65 6e 74 20 68 69 67 68 6c 79 20 66 72  agment highly fr
15815 61 67 6d 65 6e 74 65 64 20 70 61 67 65 73 20 2a  agmented pages *
15816 2f 0a 20 20 20 20 72 63 20 3d 20 64 65 66 72 61  /.    rc = defra
15817 67 6d 65 6e 74 50 61 67 65 28 70 50 61 67 65 29  gmentPage(pPage)
15818 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72  ;.    if( rc ) r
15819 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 74 6f  eturn rc;.    to
1581a 70 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61  p = get2byte(&da
1581b 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 7d 65  ta[hdr+5]);.  }e
1581c 6c 73 65 20 69 66 28 20 67 61 70 2b 32 3c 3d 74  lse if( gap+2<=t
1581d 6f 70 20 29 7b 0a 20 20 20 20 2f 2a 20 53 65 61  op ){.    /* Sea
1581e 72 63 68 20 74 68 65 20 66 72 65 65 6c 69 73 74  rch the freelist
1581f 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 66   looking for a f
15820 72 65 65 20 73 6c 6f 74 20 62 69 67 20 65 6e 6f  ree slot big eno
15821 75 67 68 20 74 6f 20 73 61 74 69 73 66 79 20 0a  ugh to satisfy .
15822 20 20 20 20 2a 2a 20 74 68 65 20 72 65 71 75 65      ** the reque
15823 73 74 2e 20 54 68 65 20 61 6c 6c 6f 63 61 74 69  st. The allocati
15824 6f 6e 20 69 73 20 6d 61 64 65 20 66 72 6f 6d 20  on is made from 
15825 74 68 65 20 66 69 72 73 74 20 66 72 65 65 20 73  the first free s
15826 6c 6f 74 20 69 6e 20 0a 20 20 20 20 2a 2a 20 74  lot in .    ** t
15827 68 65 20 6c 69 73 74 20 74 68 61 74 20 69 73 20  he list that is 
15828 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20  large enough to 
15829 61 63 63 6f 6d 61 64 61 74 65 20 69 74 2e 0a 20  accomadate it.. 
1582a 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 70 63     */.    int pc
1582b 2c 20 61 64 64 72 3b 0a 20 20 20 20 66 6f 72 28  , addr;.    for(
1582c 61 64 64 72 3d 68 64 72 2b 31 3b 20 28 70 63 20  addr=hdr+1; (pc 
1582d 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  = get2byte(&data
1582e 5b 61 64 64 72 5d 29 29 3e 30 3b 20 61 64 64 72  [addr]))>0; addr
1582f 3d 70 63 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  =pc){.      int 
15830 73 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28  size = get2byte(
15831 26 64 61 74 61 5b 70 63 2b 32 5d 29 3b 20 20 20  &data[pc+2]);   
15832 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 66 72 65    /* Size of fre
15833 65 20 73 6c 6f 74 20 2a 2f 0a 20 20 20 20 20 20  e slot */.      
15834 69 66 28 20 73 69 7a 65 3e 3d 6e 42 79 74 65 20  if( size>=nByte 
15835 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 78  ){.        int x
15836 20 3d 20 73 69 7a 65 20 2d 20 6e 42 79 74 65 3b   = size - nByte;
15837 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73  .        testcas
15838 65 28 20 78 3d 3d 34 20 29 3b 0a 20 20 20 20 20  e( x==4 );.     
15839 20 20 20 74 65 73 74 63 61 73 65 28 20 78 3d 3d     testcase( x==
1583a 33 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  3 );.        if(
1583b 20 78 3c 34 20 29 7b 0a 20 20 20 20 20 20 20 20   x<4 ){.        
1583c 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20    /* Remove the 
1583d 73 6c 6f 74 20 66 72 6f 6d 20 74 68 65 20 66 72  slot from the fr
1583e 65 65 2d 6c 69 73 74 2e 20 55 70 64 61 74 65 20  ee-list. Update 
1583f 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20  the number of.  
15840 20 20 20 20 20 20 20 20 2a 2a 20 66 72 61 67 6d          ** fragm
15841 65 6e 74 65 64 20 62 79 74 65 73 20 77 69 74 68  ented bytes with
15842 69 6e 20 74 68 65 20 70 61 67 65 2e 20 2a 2f 0a  in the page. */.
15843 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79            memcpy
15844 28 26 64 61 74 61 5b 61 64 64 72 5d 2c 20 26 64  (&data[addr], &d
15845 61 74 61 5b 70 63 5d 2c 20 32 29 3b 0a 20 20 20  ata[pc], 2);.   
15846 20 20 20 20 20 20 20 64 61 74 61 5b 68 64 72 2b         data[hdr+
15847 37 5d 20 3d 20 28 75 38 29 28 6e 46 72 61 67 20  7] = (u8)(nFrag 
15848 2b 20 78 29 3b 0a 20 20 20 20 20 20 20 20 7d 65  + x);.        }e
15849 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f  lse{.          /
1584a 2a 20 54 68 65 20 73 6c 6f 74 20 72 65 6d 61 69  * The slot remai
1584b 6e 73 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c  ns on the free-l
1584c 69 73 74 2e 20 52 65 64 75 63 65 20 69 74 73 20  ist. Reduce its 
1584d 73 69 7a 65 20 74 6f 20 61 63 63 6f 75 6e 74 0a  size to account.
1584e 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 6f 72            ** for
1584f 20 74 68 65 20 70 6f 72 74 69 6f 6e 20 75 73 65   the portion use
15850 64 20 62 79 20 74 68 65 20 6e 65 77 20 61 6c 6c  d by the new all
15851 6f 63 61 74 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20  ocation. */.    
15852 20 20 20 20 20 20 70 75 74 32 62 79 74 65 28 26        put2byte(&
15853 64 61 74 61 5b 70 63 2b 32 5d 2c 20 78 29 3b 0a  data[pc+2], x);.
15854 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
15855 20 20 2a 70 49 64 78 20 3d 20 70 63 20 2b 20 78    *pIdx = pc + x
15856 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
15857 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
15858 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20    }.    }.  }.. 
15859 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b   /* Check to mak
1585a 65 20 73 75 72 65 20 74 68 65 72 65 20 69 73 20  e sure there is 
1585b 65 6e 6f 75 67 68 20 73 70 61 63 65 20 69 6e 20  enough space in 
1585c 74 68 65 20 67 61 70 20 74 6f 20 73 61 74 69 73  the gap to satis
1585d 66 79 0a 20 20 2a 2a 20 74 68 65 20 61 6c 6c 6f  fy.  ** the allo
1585e 63 61 74 69 6f 6e 2e 20 20 49 66 20 6e 6f 74 2c  cation.  If not,
1585f 20 64 65 66 72 61 67 6d 65 6e 74 2e 0a 20 20 2a   defragment..  *
15860 2f 0a 20 20 74 65 73 74 63 61 73 65 28 20 67 61  /.  testcase( ga
15861 70 2b 32 2b 6e 42 79 74 65 3d 3d 74 6f 70 20 29  p+2+nByte==top )
15862 3b 0a 20 20 69 66 28 20 67 61 70 2b 32 2b 6e 42  ;.  if( gap+2+nB
15863 79 74 65 3e 74 6f 70 20 29 7b 0a 20 20 20 20 72  yte>top ){.    r
15864 63 20 3d 20 64 65 66 72 61 67 6d 65 6e 74 50 61  c = defragmentPa
15865 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 69  ge(pPage);.    i
15866 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
15867 63 3b 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74  c;.    top = get
15868 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b  2byte(&data[hdr+
15869 35 5d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  5]);.    assert(
1586a 20 67 61 70 2b 6e 42 79 74 65 3c 3d 74 6f 70 20   gap+nByte<=top 
1586b 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 41 6c  );.  }...  /* Al
1586c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 66 72  locate memory fr
1586d 6f 6d 20 74 68 65 20 67 61 70 20 69 6e 20 62 65  om the gap in be
1586e 74 77 65 65 6e 20 74 68 65 20 63 65 6c 6c 20 70  tween the cell p
1586f 6f 69 6e 74 65 72 20 61 72 72 61 79 0a 20 20 2a  ointer array.  *
15870 2a 20 61 6e 64 20 74 68 65 20 63 65 6c 6c 20 63  * and the cell c
15871 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 20 54 68  ontent area.  Th
15872 65 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28  e btreeInitPage(
15873 29 20 63 61 6c 6c 20 68 61 73 20 61 6c 72 65 61  ) call has alrea
15874 64 79 0a 20 20 2a 2a 20 76 61 6c 69 64 61 74 65  dy.  ** validate
15875 64 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20  d the freelist. 
15876 20 47 69 76 65 6e 20 74 68 61 74 20 74 68 65 20   Given that the 
15877 66 72 65 65 6c 69 73 74 20 69 73 20 76 61 6c 69  freelist is vali
15878 64 2c 20 74 68 65 72 65 0a 20 20 2a 2a 20 69 73  d, there.  ** is
15879 20 6e 6f 20 77 61 79 20 74 68 61 74 20 74 68 65   no way that the
1587a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 63 61 6e 20   allocation can 
1587b 65 78 74 65 6e 64 20 6f 66 66 20 74 68 65 20 65  extend off the e
1587c 6e 64 20 6f 66 20 74 68 65 20 70 61 67 65 2e 0a  nd of the page..
1587d 20 20 2a 2a 20 54 68 65 20 61 73 73 65 72 74 28    ** The assert(
1587e 29 20 62 65 6c 6f 77 20 76 65 72 69 66 69 65 73  ) below verifies
1587f 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 65   the previous se
15880 6e 74 65 6e 63 65 2e 0a 20 20 2a 2f 0a 20 20 74  ntence..  */.  t
15881 6f 70 20 2d 3d 20 6e 42 79 74 65 3b 0a 20 20 70  op -= nByte;.  p
15882 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64  ut2byte(&data[hd
15883 72 2b 35 5d 2c 20 74 6f 70 29 3b 0a 20 20 61 73  r+5], top);.  as
15884 73 65 72 74 28 20 74 6f 70 2b 6e 42 79 74 65 20  sert( top+nByte 
15885 3c 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75  <= pPage->pBt->u
15886 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 2a  sableSize );.  *
15887 70 49 64 78 20 3d 20 74 6f 70 3b 0a 20 20 72 65  pIdx = top;.  re
15888 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
15889 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
1588a 61 20 73 65 63 74 69 6f 6e 20 6f 66 20 74 68 65  a section of the
1588b 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 74 6f   pPage->aData to
1588c 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 0a 2a   the freelist..*
1588d 2a 20 54 68 65 20 66 69 72 73 74 20 62 79 74 65  * The first byte
1588e 20 6f 66 20 74 68 65 20 6e 65 77 20 66 72 65 65   of the new free
1588f 20 62 6c 6f 63 6b 20 69 73 20 70 50 61 67 65 2d   block is pPage-
15890 3e 61 44 69 73 6b 5b 73 74 61 72 74 5d 0a 2a 2a  >aDisk[start].**
15891 20 61 6e 64 20 74 68 65 20 73 69 7a 65 20 6f 66   and the size of
15892 20 74 68 65 20 62 6c 6f 63 6b 20 69 73 20 22 73   the block is "s
15893 69 7a 65 22 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a  ize" bytes..**.*
15894 2a 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 65 66  * Most of the ef
15895 66 6f 72 74 20 68 65 72 65 20 69 73 20 69 6e 76  fort here is inv
15896 6f 6c 76 65 64 20 69 6e 20 63 6f 61 6c 65 73 69  olved in coalesi
15897 6e 67 20 61 64 6a 61 63 65 6e 74 0a 2a 2a 20 66  ng adjacent.** f
15898 72 65 65 20 62 6c 6f 63 6b 73 20 69 6e 74 6f 20  ree blocks into 
15899 61 20 73 69 6e 67 6c 65 20 62 69 67 20 66 72 65  a single big fre
1589a 65 20 62 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74  e block..*/.stat
1589b 69 63 20 69 6e 74 20 66 72 65 65 53 70 61 63 65  ic int freeSpace
1589c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c  (MemPage *pPage,
1589d 20 69 6e 74 20 73 74 61 72 74 2c 20 69 6e 74 20   int start, int 
1589e 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 61 64 64  size){.  int add
1589f 72 2c 20 70 62 65 67 69 6e 2c 20 68 64 72 3b 0a  r, pbegin, hdr;.
158a0 20 20 69 6e 74 20 69 4c 61 73 74 3b 20 20 20 20    int iLast;    
158a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
158a2 20 20 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 70      /* Largest p
158a3 6f 73 73 69 62 6c 65 20 66 72 65 65 62 6c 6f 63  ossible freebloc
158a4 6b 20 6f 66 66 73 65 74 20 2a 2f 0a 20 20 75 6e  k offset */.  un
158a5 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74  signed char *dat
158a6 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61  a = pPage->aData
158a7 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  ;..  assert( pPa
158a8 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20  ge->pBt!=0 );.  
158a9 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50  assert( sqlite3P
158aa 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28  agerIswriteable(
158ab 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20  pPage->pDbPage) 
158ac 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 74 61  );.  assert( sta
158ad 72 74 3e 3d 70 50 61 67 65 2d 3e 68 64 72 4f 66  rt>=pPage->hdrOf
158ae 66 73 65 74 2b 36 2b 70 50 61 67 65 2d 3e 63 68  fset+6+pPage->ch
158af 69 6c 64 50 74 72 53 69 7a 65 20 29 3b 0a 20 20  ildPtrSize );.  
158b0 61 73 73 65 72 74 28 20 28 73 74 61 72 74 20 2b  assert( (start +
158b1 20 73 69 7a 65 29 3c 3d 70 50 61 67 65 2d 3e 70   size)<=pPage->p
158b2 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29  Bt->usableSize )
158b3 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
158b4 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
158b5 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  Page->pBt->mutex
158b6 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  ) );.  assert( s
158b7 69 7a 65 3e 3d 30 20 29 3b 20 20 20 2f 2a 20 4d  ize>=0 );   /* M
158b8 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65  inimum cell size
158b9 20 69 73 20 34 20 2a 2f 0a 0a 23 69 66 64 65 66   is 4 */..#ifdef
158ba 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44   SQLITE_SECURE_D
158bb 45 4c 45 54 45 0a 20 20 2f 2a 20 4f 76 65 72 77  ELETE.  /* Overw
158bc 72 69 74 65 20 64 65 6c 65 74 65 64 20 69 6e 66  rite deleted inf
158bd 6f 72 6d 61 74 69 6f 6e 20 77 69 74 68 20 7a 65  ormation with ze
158be 72 6f 73 20 77 68 65 6e 20 74 68 65 20 53 45 43  ros when the SEC
158bf 55 52 45 5f 44 45 4c 45 54 45 20 0a 20 20 2a 2a  URE_DELETE .  **
158c0 20 6f 70 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c   option is enabl
158c1 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69  ed at compile-ti
158c2 6d 65 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26  me */.  memset(&
158c3 64 61 74 61 5b 73 74 61 72 74 5d 2c 20 30 2c 20  data[start], 0, 
158c4 73 69 7a 65 29 3b 0a 23 65 6e 64 69 66 0a 0a 20  size);.#endif.. 
158c5 20 2f 2a 20 41 64 64 20 74 68 65 20 73 70 61 63   /* Add the spac
158c6 65 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20  e back into the 
158c7 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 66  linked list of f
158c8 72 65 65 62 6c 6f 63 6b 73 2e 20 20 4e 6f 74 65  reeblocks.  Note
158c9 20 74 68 61 74 0a 20 20 2a 2a 20 65 76 65 6e 20   that.  ** even 
158ca 74 68 6f 75 67 68 20 74 68 65 20 66 72 65 65 62  though the freeb
158cb 6c 6f 63 6b 20 6c 69 73 74 20 77 61 73 20 63 68  lock list was ch
158cc 65 63 6b 65 64 20 62 79 20 62 74 72 65 65 49 6e  ecked by btreeIn
158cd 69 74 50 61 67 65 28 29 2c 0a 20 20 2a 2a 20 62  itPage(),.  ** b
158ce 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 64  treeInitPage() d
158cf 69 64 20 6e 6f 74 20 64 65 74 65 63 74 20 6f 76  id not detect ov
158d0 65 72 6c 61 70 70 69 6e 67 20 63 65 6c 6c 73 20  erlapping cells 
158d1 6f 72 0a 20 20 2a 2a 20 66 72 65 65 62 6c 6f 63  or.  ** freebloc
158d2 6b 73 20 74 68 61 74 20 6f 76 65 72 6c 61 70 70  ks that overlapp
158d3 65 64 20 63 65 6c 6c 73 2e 20 20 20 4e 6f 72 20  ed cells.   Nor 
158d4 64 6f 65 73 20 69 74 20 64 65 74 65 63 74 20 77  does it detect w
158d5 68 65 6e 20 74 68 65 0a 20 20 2a 2a 20 63 65 6c  hen the.  ** cel
158d6 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 65  l content area e
158d7 78 63 65 65 64 73 20 74 68 65 20 76 61 6c 75 65  xceeds the value
158d8 20 69 6e 20 74 68 65 20 70 61 67 65 20 68 65 61   in the page hea
158d9 64 65 72 2e 20 20 49 66 20 74 68 65 73 65 0a 20  der.  If these. 
158da 20 2a 2a 20 73 69 74 75 61 74 69 6f 6e 73 20 61   ** situations a
158db 72 69 73 65 2c 20 74 68 65 6e 20 73 75 62 73 65  rise, then subse
158dc 71 75 65 6e 74 20 69 6e 73 65 72 74 20 6f 70 65  quent insert ope
158dd 72 61 74 69 6f 6e 73 20 6d 69 67 68 74 20 63 6f  rations might co
158de 72 72 75 70 74 0a 20 20 2a 2a 20 74 68 65 20 66  rrupt.  ** the f
158df 72 65 65 6c 69 73 74 2e 20 20 53 6f 20 77 65 20  reelist.  So we 
158e0 64 6f 20 6e 65 65 64 20 74 6f 20 63 68 65 63 6b  do need to check
158e1 20 66 6f 72 20 63 6f 72 72 75 70 74 69 6f 6e 20   for corruption 
158e2 77 68 69 6c 65 20 73 63 61 6e 6e 69 6e 67 0a 20  while scanning. 
158e3 20 2a 2a 20 74 68 65 20 66 72 65 65 6c 69 73 74   ** the freelist
158e4 2e 0a 20 20 2a 2f 0a 20 20 68 64 72 20 3d 20 70  ..  */.  hdr = p
158e5 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b  Page->hdrOffset;
158e6 0a 20 20 61 64 64 72 20 3d 20 68 64 72 20 2b 20  .  addr = hdr + 
158e7 31 3b 0a 20 20 69 4c 61 73 74 20 3d 20 70 50 61  1;.  iLast = pPa
158e8 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53  ge->pBt->usableS
158e9 69 7a 65 20 2d 20 34 3b 0a 20 20 61 73 73 65 72  ize - 4;.  asser
158ea 74 28 20 73 74 61 72 74 3c 3d 69 4c 61 73 74 20  t( start<=iLast 
158eb 29 3b 0a 20 20 77 68 69 6c 65 28 20 28 70 62 65  );.  while( (pbe
158ec 67 69 6e 20 3d 20 67 65 74 32 62 79 74 65 28 26  gin = get2byte(&
158ed 64 61 74 61 5b 61 64 64 72 5d 29 29 3c 73 74 61  data[addr]))<sta
158ee 72 74 20 26 26 20 70 62 65 67 69 6e 3e 30 20 29  rt && pbegin>0 )
158ef 7b 0a 20 20 20 20 69 66 28 20 70 62 65 67 69 6e  {.    if( pbegin
158f0 3c 61 64 64 72 2b 34 20 29 7b 0a 20 20 20 20 20  <addr+4 ){.     
158f1 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
158f2 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
158f3 20 7d 0a 20 20 20 20 61 64 64 72 20 3d 20 70 62   }.    addr = pb
158f4 65 67 69 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20  egin;.  }.  if( 
158f5 70 62 65 67 69 6e 3e 69 4c 61 73 74 20 29 7b 0a  pbegin>iLast ){.
158f6 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
158f7 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
158f8 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 62    }.  assert( pb
158f9 65 67 69 6e 3e 61 64 64 72 20 7c 7c 20 70 62 65  egin>addr || pbe
158fa 67 69 6e 3d 3d 30 20 29 3b 0a 20 20 70 75 74 32  gin==0 );.  put2
158fb 62 79 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d  byte(&data[addr]
158fc 2c 20 73 74 61 72 74 29 3b 0a 20 20 70 75 74 32  , start);.  put2
158fd 62 79 74 65 28 26 64 61 74 61 5b 73 74 61 72 74  byte(&data[start
158fe 5d 2c 20 70 62 65 67 69 6e 29 3b 0a 20 20 70 75  ], pbegin);.  pu
158ff 74 32 62 79 74 65 28 26 64 61 74 61 5b 73 74 61  t2byte(&data[sta
15900 72 74 2b 32 5d 2c 20 73 69 7a 65 29 3b 0a 20 20  rt+2], size);.  
15901 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20 70  pPage->nFree = p
15902 50 61 67 65 2d 3e 6e 46 72 65 65 20 2b 20 28 75  Page->nFree + (u
15903 31 36 29 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20 43  16)size;..  /* C
15904 6f 61 6c 65 73 63 65 20 61 64 6a 61 63 65 6e 74  oalesce adjacent
15905 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 2a 2f 0a   free blocks */.
15906 20 20 61 64 64 72 20 3d 20 68 64 72 20 2b 20 31    addr = hdr + 1
15907 3b 0a 20 20 77 68 69 6c 65 28 20 28 70 62 65 67  ;.  while( (pbeg
15908 69 6e 20 3d 20 67 65 74 32 62 79 74 65 28 26 64  in = get2byte(&d
15909 61 74 61 5b 61 64 64 72 5d 29 29 3e 30 20 29 7b  ata[addr]))>0 ){
1590a 0a 20 20 20 20 69 6e 74 20 70 6e 65 78 74 2c 20  .    int pnext, 
1590b 70 73 69 7a 65 2c 20 78 3b 0a 20 20 20 20 61 73  psize, x;.    as
1590c 73 65 72 74 28 20 70 62 65 67 69 6e 3e 61 64 64  sert( pbegin>add
1590d 72 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  r );.    assert(
1590e 20 70 62 65 67 69 6e 3c 3d 70 50 61 67 65 2d 3e   pbegin<=pPage->
1590f 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d  pBt->usableSize-
15910 34 20 29 3b 0a 20 20 20 20 70 6e 65 78 74 20 3d  4 );.    pnext =
15911 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
15912 70 62 65 67 69 6e 5d 29 3b 0a 20 20 20 20 70 73  pbegin]);.    ps
15913 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28 26  ize = get2byte(&
15914 64 61 74 61 5b 70 62 65 67 69 6e 2b 32 5d 29 3b  data[pbegin+2]);
15915 0a 20 20 20 20 69 66 28 20 70 62 65 67 69 6e 20  .    if( pbegin 
15916 2b 20 70 73 69 7a 65 20 2b 20 33 20 3e 3d 20 70  + psize + 3 >= p
15917 6e 65 78 74 20 26 26 20 70 6e 65 78 74 3e 30 20  next && pnext>0 
15918 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 66 72 61  ){.      int fra
15919 67 20 3d 20 70 6e 65 78 74 20 2d 20 28 70 62 65  g = pnext - (pbe
1591a 67 69 6e 2b 70 73 69 7a 65 29 3b 0a 20 20 20 20  gin+psize);.    
1591b 20 20 69 66 28 20 28 66 72 61 67 3c 30 29 20 7c    if( (frag<0) |
1591c 7c 20 28 66 72 61 67 3e 28 69 6e 74 29 64 61 74  | (frag>(int)dat
1591d 61 5b 68 64 72 2b 37 5d 29 20 29 7b 0a 20 20 20  a[hdr+7]) ){.   
1591e 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
1591f 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
15920 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64  .      }.      d
15921 61 74 61 5b 68 64 72 2b 37 5d 20 2d 3d 20 28 75  ata[hdr+7] -= (u
15922 38 29 66 72 61 67 3b 0a 20 20 20 20 20 20 78 20  8)frag;.      x 
15923 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  = get2byte(&data
15924 5b 70 6e 65 78 74 5d 29 3b 0a 20 20 20 20 20 20  [pnext]);.      
15925 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 70  put2byte(&data[p
15926 62 65 67 69 6e 5d 2c 20 78 29 3b 0a 20 20 20 20  begin], x);.    
15927 20 20 78 20 3d 20 70 6e 65 78 74 20 2b 20 67 65    x = pnext + ge
15928 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 6e 65  t2byte(&data[pne
15929 78 74 2b 32 5d 29 20 2d 20 70 62 65 67 69 6e 3b  xt+2]) - pbegin;
1592a 0a 20 20 20 20 20 20 70 75 74 32 62 79 74 65 28  .      put2byte(
1592b 26 64 61 74 61 5b 70 62 65 67 69 6e 2b 32 5d 2c  &data[pbegin+2],
1592c 20 78 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a   x);.    }else{.
1592d 20 20 20 20 20 20 61 64 64 72 20 3d 20 70 62 65        addr = pbe
1592e 67 69 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  gin;.    }.  }..
1592f 20 20 2f 2a 20 49 66 20 74 68 65 20 63 65 6c 6c    /* If the cell
15930 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 62 65   content area be
15931 67 69 6e 73 20 77 69 74 68 20 61 20 66 72 65 65  gins with a free
15932 62 6c 6f 63 6b 2c 20 72 65 6d 6f 76 65 20 69 74  block, remove it
15933 2e 20 2a 2f 0a 20 20 69 66 28 20 64 61 74 61 5b  . */.  if( data[
15934 68 64 72 2b 31 5d 3d 3d 64 61 74 61 5b 68 64 72  hdr+1]==data[hdr
15935 2b 35 5d 20 26 26 20 64 61 74 61 5b 68 64 72 2b  +5] && data[hdr+
15936 32 5d 3d 3d 64 61 74 61 5b 68 64 72 2b 36 5d 20  2]==data[hdr+6] 
15937 29 7b 0a 20 20 20 20 69 6e 74 20 74 6f 70 3b 0a  ){.    int top;.
15938 20 20 20 20 70 62 65 67 69 6e 20 3d 20 67 65 74      pbegin = get
15939 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b  2byte(&data[hdr+
1593a 31 5d 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  1]);.    memcpy(
1593b 26 64 61 74 61 5b 68 64 72 2b 31 5d 2c 20 26 64  &data[hdr+1], &d
1593c 61 74 61 5b 70 62 65 67 69 6e 5d 2c 20 32 29 3b  ata[pbegin], 2);
1593d 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62  .    top = get2b
1593e 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d  yte(&data[hdr+5]
1593f 29 20 2b 20 67 65 74 32 62 79 74 65 28 26 64 61  ) + get2byte(&da
15940 74 61 5b 70 62 65 67 69 6e 2b 32 5d 29 3b 0a 20  ta[pbegin+2]);. 
15941 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74     put2byte(&dat
15942 61 5b 68 64 72 2b 35 5d 2c 20 74 6f 70 29 3b 0a  a[hdr+5], top);.
15943 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 73 71    }.  assert( sq
15944 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74  lite3PagerIswrit
15945 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62  eable(pPage->pDb
15946 50 61 67 65 29 20 29 3b 0a 20 20 72 65 74 75 72  Page) );.  retur
15947 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
15948 2f 2a 0a 2a 2a 20 44 65 63 6f 64 65 20 74 68 65  /*.** Decode the
15949 20 66 6c 61 67 73 20 62 79 74 65 20 28 74 68 65   flags byte (the
1594a 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 74   first byte of t
1594b 68 65 20 68 65 61 64 65 72 29 20 66 6f 72 20 61  he header) for a
1594c 20 70 61 67 65 0a 2a 2a 20 61 6e 64 20 69 6e 69   page.** and ini
1594d 74 69 61 6c 69 7a 65 20 66 69 65 6c 64 73 20 6f  tialize fields o
1594e 66 20 74 68 65 20 4d 65 6d 50 61 67 65 20 73 74  f the MemPage st
1594f 72 75 63 74 75 72 65 20 61 63 63 6f 72 64 69 6e  ructure accordin
15950 67 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20  gly..**.** Only 
15951 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f  the following co
15952 6d 62 69 6e 61 74 69 6f 6e 73 20 61 72 65 20 73  mbinations are s
15953 75 70 70 6f 72 74 65 64 2e 20 20 41 6e 79 74 68  upported.  Anyth
15954 69 6e 67 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a  ing different.**
15955 20 69 6e 64 69 63 61 74 65 73 20 61 20 63 6f 72   indicates a cor
15956 72 75 70 74 20 64 61 74 61 62 61 73 65 20 66 69  rupt database fi
15957 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  les:.**.**      
15958 20 20 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 0a     PTF_ZERODATA.
15959 2a 2a 20 20 20 20 20 20 20 20 20 50 54 46 5f 5a  **         PTF_Z
1595a 45 52 4f 44 41 54 41 20 7c 20 50 54 46 5f 4c 45  ERODATA | PTF_LE
1595b 41 46 0a 2a 2a 20 20 20 20 20 20 20 20 20 50 54  AF.**         PT
1595c 46 5f 4c 45 41 46 44 41 54 41 20 7c 20 50 54 46  F_LEAFDATA | PTF
1595d 5f 49 4e 54 4b 45 59 0a 2a 2a 20 20 20 20 20 20  _INTKEY.**      
1595e 20 20 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20     PTF_LEAFDATA 
1595f 7c 20 50 54 46 5f 49 4e 54 4b 45 59 20 7c 20 50  | PTF_INTKEY | P
15960 54 46 5f 4c 45 41 46 0a 2a 2f 0a 73 74 61 74 69  TF_LEAF.*/.stati
15961 63 20 69 6e 74 20 64 65 63 6f 64 65 46 6c 61 67  c int decodeFlag
15962 73 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65  s(MemPage *pPage
15963 2c 20 69 6e 74 20 66 6c 61 67 42 79 74 65 29 7b  , int flagByte){
15964 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
15965 3b 20 20 20 20 20 2f 2a 20 41 20 63 6f 70 79 20  ;     /* A copy 
15966 6f 66 20 70 50 61 67 65 2d 3e 70 42 74 20 2a 2f  of pPage->pBt */
15967 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ..  assert( pPag
15968 65 2d 3e 68 64 72 4f 66 66 73 65 74 3d 3d 28 70  e->hdrOffset==(p
15969 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d 31 20 3f 20  Page->pgno==1 ? 
1596a 31 30 30 20 3a 20 30 29 20 29 3b 0a 20 20 61 73  100 : 0) );.  as
1596b 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1596c 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e  tex_held(pPage->
1596d 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
1596e 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 3d 20 28   pPage->leaf = (
1596f 75 38 29 28 66 6c 61 67 42 79 74 65 3e 3e 33 29  u8)(flagByte>>3)
15970 3b 20 20 61 73 73 65 72 74 28 20 50 54 46 5f 4c  ;  assert( PTF_L
15971 45 41 46 20 3d 3d 20 31 3c 3c 33 20 29 3b 0a 20  EAF == 1<<3 );. 
15972 20 66 6c 61 67 42 79 74 65 20 26 3d 20 7e 50 54   flagByte &= ~PT
15973 46 5f 4c 45 41 46 3b 0a 20 20 70 50 61 67 65 2d  F_LEAF;.  pPage-
15974 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 20 3d 20  >childPtrSize = 
15975 34 2d 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 3b  4-4*pPage->leaf;
15976 0a 20 20 70 42 74 20 3d 20 70 50 61 67 65 2d 3e  .  pBt = pPage->
15977 70 42 74 3b 0a 20 20 69 66 28 20 66 6c 61 67 42  pBt;.  if( flagB
15978 79 74 65 3d 3d 28 50 54 46 5f 4c 45 41 46 44 41  yte==(PTF_LEAFDA
15979 54 41 20 7c 20 50 54 46 5f 49 4e 54 4b 45 59 29  TA | PTF_INTKEY)
1597a 20 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69   ){.    pPage->i
1597b 6e 74 4b 65 79 20 3d 20 31 3b 0a 20 20 20 20 70  ntKey = 1;.    p
1597c 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 3d 20  Page->hasData = 
1597d 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 20  pPage->leaf;.   
1597e 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c   pPage->maxLocal
1597f 20 3d 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66 3b   = pBt->maxLeaf;
15980 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 69 6e 4c  .    pPage->minL
15981 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 69 6e 4c  ocal = pBt->minL
15982 65 61 66 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  eaf;.  }else if(
15983 20 66 6c 61 67 42 79 74 65 3d 3d 50 54 46 5f 5a   flagByte==PTF_Z
15984 45 52 4f 44 41 54 41 20 29 7b 0a 20 20 20 20 70  ERODATA ){.    p
15985 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 3d 20 30  Page->intKey = 0
15986 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 68 61 73  ;.    pPage->has
15987 44 61 74 61 20 3d 20 30 3b 0a 20 20 20 20 70 50  Data = 0;.    pP
15988 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20  age->maxLocal = 
15989 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c 3b 0a 20  pBt->maxLocal;. 
1598a 20 20 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63     pPage->minLoc
1598b 61 6c 20 3d 20 70 42 74 2d 3e 6d 69 6e 4c 6f 63  al = pBt->minLoc
1598c 61 6c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  al;.  }else{.   
1598d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
1598e 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d  ORRUPT_BKPT;.  }
1598f 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
15990 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  _OK;.}../*.** In
15991 69 74 69 61 6c 69 7a 65 20 74 68 65 20 61 75 78  itialize the aux
15992 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d 61 74 69  iliary informati
15993 6f 6e 20 66 6f 72 20 61 20 64 69 73 6b 20 62 6c  on for a disk bl
15994 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  ock..**.** Retur
15995 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73  n SQLITE_OK on s
15996 75 63 63 65 73 73 2e 20 20 49 66 20 77 65 20 73  uccess.  If we s
15997 65 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65  ee that the page
15998 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e   does.** not con
15999 74 61 69 6e 20 61 20 77 65 6c 6c 2d 66 6f 72 6d  tain a well-form
1599a 65 64 20 64 61 74 61 62 61 73 65 20 70 61 67 65  ed database page
1599b 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20 0a 2a  , then return .*
1599c 2a 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  * SQLITE_CORRUPT
1599d 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 61 20 72  .  Note that a r
1599e 65 74 75 72 6e 20 6f 66 20 53 51 4c 49 54 45 5f  eturn of SQLITE_
1599f 4f 4b 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 67  OK does not.** g
159a0 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 74 68  uarantee that th
159a1 65 20 70 61 67 65 20 69 73 20 77 65 6c 6c 2d 66  e page is well-f
159a2 6f 72 6d 65 64 2e 20 20 49 74 20 6f 6e 6c 79 20  ormed.  It only 
159a3 73 68 6f 77 73 20 74 68 61 74 0a 2a 2a 20 77 65  shows that.** we
159a4 20 66 61 69 6c 65 64 20 74 6f 20 64 65 74 65 63   failed to detec
159a5 74 20 61 6e 79 20 63 6f 72 72 75 70 74 69 6f 6e  t any corruption
159a6 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
159a7 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 4d 65  btreeInitPage(Me
159a8 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 0a  mPage *pPage){..
159a9 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
159aa 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73  >pBt!=0 );.  ass
159ab 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
159ac 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70  ex_held(pPage->p
159ad 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
159ae 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70  assert( pPage->p
159af 67 6e 6f 3d 3d 73 71 6c 69 74 65 33 50 61 67 65  gno==sqlite3Page
159b0 72 50 61 67 65 6e 75 6d 62 65 72 28 70 50 61 67  rPagenumber(pPag
159b1 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20  e->pDbPage) );. 
159b2 20 61 73 73 65 72 74 28 20 70 50 61 67 65 20 3d   assert( pPage =
159b3 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  = sqlite3PagerGe
159b4 74 45 78 74 72 61 28 70 50 61 67 65 2d 3e 70 44  tExtra(pPage->pD
159b5 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65  bPage) );.  asse
159b6 72 74 28 20 70 50 61 67 65 2d 3e 61 44 61 74 61  rt( pPage->aData
159b7 20 3d 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72   == sqlite3Pager
159b8 47 65 74 44 61 74 61 28 70 50 61 67 65 2d 3e 70  GetData(pPage->p
159b9 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 69 66  DbPage) );..  if
159ba 28 20 21 70 50 61 67 65 2d 3e 69 73 49 6e 69 74  ( !pPage->isInit
159bb 20 29 7b 0a 20 20 20 20 75 31 36 20 70 63 3b 20   ){.    u16 pc; 
159bc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64             /* Ad
159bd 64 72 65 73 73 20 6f 66 20 61 20 66 72 65 65 62  dress of a freeb
159be 6c 6f 63 6b 20 77 69 74 68 69 6e 20 70 50 61 67  lock within pPag
159bf 65 2d 3e 61 44 61 74 61 5b 5d 20 2a 2f 0a 20 20  e->aData[] */.  
159c0 20 20 75 38 20 68 64 72 3b 20 20 20 20 20 20 20    u8 hdr;       
159c1 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74       /* Offset t
159c2 6f 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 70  o beginning of p
159c3 61 67 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20  age header */.  
159c4 20 20 75 38 20 2a 64 61 74 61 3b 20 20 20 20 20    u8 *data;     
159c5 20 20 20 20 20 2f 2a 20 45 71 75 61 6c 20 74 6f       /* Equal to
159c6 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 2a 2f   pPage->aData */
159c7 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 2a 70  .    BtShared *p
159c8 42 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68  Bt;        /* Th
159c9 65 20 6d 61 69 6e 20 62 74 72 65 65 20 73 74 72  e main btree str
159ca 75 63 74 75 72 65 20 2a 2f 0a 20 20 20 20 75 31  ucture */.    u1
159cb 36 20 75 73 61 62 6c 65 53 69 7a 65 3b 20 20 20  6 usableSize;   
159cc 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 75 73   /* Amount of us
159cd 61 62 6c 65 20 73 70 61 63 65 20 6f 6e 20 65 61  able space on ea
159ce 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 75  ch page */.    u
159cf 31 36 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20  16 cellOffset;  
159d0 20 20 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f 6d    /* Offset from
159d1 20 73 74 61 72 74 20 6f 66 20 70 61 67 65 20 74   start of page t
159d2 6f 20 66 69 72 73 74 20 63 65 6c 6c 20 70 6f 69  o first cell poi
159d3 6e 74 65 72 20 2a 2f 0a 20 20 20 20 75 31 36 20  nter */.    u16 
159d4 6e 46 72 65 65 3b 20 20 20 20 20 20 20 20 20 2f  nFree;         /
159d5 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 6e 75 73  * Number of unus
159d6 65 64 20 62 79 74 65 73 20 6f 6e 20 74 68 65 20  ed bytes on the 
159d7 70 61 67 65 20 2a 2f 0a 20 20 20 20 75 31 36 20  page */.    u16 
159d8 74 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 2f  top;           /
159d9 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 20  * First byte of 
159da 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74  the cell content
159db 20 61 72 65 61 20 2a 2f 0a 20 20 20 20 69 6e 74   area */.    int
159dc 20 69 43 65 6c 6c 46 69 72 73 74 3b 20 20 20 20   iCellFirst;    
159dd 2f 2a 20 46 69 72 73 74 20 61 6c 6c 6f 77 61 62  /* First allowab
159de 6c 65 20 63 65 6c 6c 20 6f 72 20 66 72 65 65 62  le cell or freeb
159df 6c 6f 63 6b 20 6f 66 66 73 65 74 20 2a 2f 0a 20  lock offset */. 
159e0 20 20 20 69 6e 74 20 69 43 65 6c 6c 4c 61 73 74     int iCellLast
159e1 3b 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 70 6f  ;     /* Last po
159e2 73 73 69 62 6c 65 20 63 65 6c 6c 20 6f 72 20 66  ssible cell or f
159e3 72 65 65 62 6c 6f 63 6b 20 6f 66 66 73 65 74 20  reeblock offset 
159e4 2a 2f 0a 0a 20 20 20 20 70 42 74 20 3d 20 70 50  */..    pBt = pP
159e5 61 67 65 2d 3e 70 42 74 3b 0a 0a 20 20 20 20 68  age->pBt;..    h
159e6 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f  dr = pPage->hdrO
159e7 66 66 73 65 74 3b 0a 20 20 20 20 64 61 74 61 20  ffset;.    data 
159e8 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a  = pPage->aData;.
159e9 20 20 20 20 69 66 28 20 64 65 63 6f 64 65 46 6c      if( decodeFl
159ea 61 67 73 28 70 50 61 67 65 2c 20 64 61 74 61 5b  ags(pPage, data[
159eb 68 64 72 5d 29 20 29 20 72 65 74 75 72 6e 20 53  hdr]) ) return S
159ec 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
159ed 50 54 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  PT;.    assert( 
159ee 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3d 35  pBt->pageSize>=5
159ef 31 32 20 26 26 20 70 42 74 2d 3e 70 61 67 65 53  12 && pBt->pageS
159f0 69 7a 65 3c 3d 33 32 37 36 38 20 29 3b 0a 20 20  ize<=32768 );.  
159f1 20 20 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67    pPage->maskPag
159f2 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a  e = pBt->pageSiz
159f3 65 20 2d 20 31 3b 0a 20 20 20 20 70 50 61 67 65  e - 1;.    pPage
159f4 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b  ->nOverflow = 0;
159f5 0a 20 20 20 20 75 73 61 62 6c 65 53 69 7a 65 20  .    usableSize 
159f6 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  = pBt->usableSiz
159f7 65 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 63 65  e;.    pPage->ce
159f8 6c 6c 4f 66 66 73 65 74 20 3d 20 63 65 6c 6c 4f  llOffset = cellO
159f9 66 66 73 65 74 20 3d 20 68 64 72 20 2b 20 31 32  ffset = hdr + 12
159fa 20 2d 20 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66   - 4*pPage->leaf
159fb 3b 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32  ;.    top = get2
159fc 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35  byte(&data[hdr+5
159fd 5d 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e  ]);.    pPage->n
159fe 43 65 6c 6c 20 3d 20 67 65 74 32 62 79 74 65 28  Cell = get2byte(
159ff 26 64 61 74 61 5b 68 64 72 2b 33 5d 29 3b 0a 20  &data[hdr+3]);. 
15a00 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 43     if( pPage->nC
15a01 65 6c 6c 3e 4d 58 5f 43 45 4c 4c 28 70 42 74 29  ell>MX_CELL(pBt)
15a02 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 6f 20   ){.      /* To 
15a03 6d 61 6e 79 20 63 65 6c 6c 73 20 66 6f 72 20 61  many cells for a
15a04 20 73 69 6e 67 6c 65 20 70 61 67 65 2e 20 20 54   single page.  T
15a05 68 65 20 70 61 67 65 20 6d 75 73 74 20 62 65 20  he page must be 
15a06 63 6f 72 72 75 70 74 20 2a 2f 0a 20 20 20 20 20  corrupt */.     
15a07 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
15a08 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
15a09 20 7d 0a 20 20 20 20 74 65 73 74 63 61 73 65 28   }.    testcase(
15a0a 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 4d   pPage->nCell==M
15a0b 58 5f 43 45 4c 4c 28 70 42 74 29 20 29 3b 0a 0a  X_CELL(pBt) );..
15a0c 20 20 20 20 2f 2a 20 41 20 6d 61 6c 66 6f 72 6d      /* A malform
15a0d 65 64 20 64 61 74 61 62 61 73 65 20 70 61 67 65  ed database page
15a0e 20 6d 69 67 68 74 20 63 61 75 73 65 20 75 73 20   might cause us 
15a0f 74 6f 20 72 65 61 64 20 70 61 73 74 20 74 68 65  to read past the
15a10 20 65 6e 64 0a 20 20 20 20 2a 2a 20 6f 66 20 70   end.    ** of p
15a11 61 67 65 20 77 68 65 6e 20 70 61 72 73 69 6e 67  age when parsing
15a12 20 61 20 63 65 6c 6c 2e 20 20 0a 20 20 20 20 2a   a cell.  .    *
15a13 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 66 6f 6c  *.    ** The fol
15a14 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 6f 66 20  lowing block of 
15a15 63 6f 64 65 20 63 68 65 63 6b 73 20 65 61 72 6c  code checks earl
15a16 79 20 74 6f 20 73 65 65 20 69 66 20 61 20 63 65  y to see if a ce
15a17 6c 6c 20 65 78 74 65 6e 64 73 0a 20 20 20 20 2a  ll extends.    *
15a18 2a 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f  * past the end o
15a19 66 20 61 20 70 61 67 65 20 62 6f 75 6e 64 61 72  f a page boundar
15a1a 79 20 61 6e 64 20 63 61 75 73 65 73 20 53 51 4c  y and causes SQL
15a1b 49 54 45 5f 43 4f 52 52 55 50 54 20 74 6f 20 62  ITE_CORRUPT to b
15a1c 65 20 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 6e  e .    ** return
15a1d 65 64 20 69 66 20 69 74 20 64 6f 65 73 2e 0a 20  ed if it does.. 
15a1e 20 20 20 2a 2f 0a 20 20 20 20 69 43 65 6c 6c 46     */.    iCellF
15a1f 69 72 73 74 20 3d 20 63 65 6c 6c 4f 66 66 73 65  irst = cellOffse
15a20 74 20 2b 20 32 2a 70 50 61 67 65 2d 3e 6e 43 65  t + 2*pPage->nCe
15a21 6c 6c 3b 0a 20 20 20 20 69 43 65 6c 6c 4c 61 73  ll;.    iCellLas
15a22 74 20 3d 20 75 73 61 62 6c 65 53 69 7a 65 20 2d  t = usableSize -
15a23 20 34 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28   4;.#if defined(
15a24 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4f 56  SQLITE_ENABLE_OV
15a25 45 52 53 49 5a 45 5f 43 45 4c 4c 5f 43 48 45 43  ERSIZE_CELL_CHEC
15a26 4b 29 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 69  K).    {.      i
15a27 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
15a28 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 74   /* Index into t
15a29 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20  he cell pointer 
15a2a 61 72 72 61 79 20 2a 2f 0a 20 20 20 20 20 20 69  array */.      i
15a2b 6e 74 20 73 7a 3b 20 20 20 20 20 20 20 20 20 20  nt sz;          
15a2c 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 20 63 65   /* Size of a ce
15a2d 6c 6c 20 2a 2f 0a 0a 20 20 20 20 20 20 69 66 28  ll */..      if(
15a2e 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 20   !pPage->leaf ) 
15a2f 69 43 65 6c 6c 4c 61 73 74 2d 2d 3b 0a 20 20 20  iCellLast--;.   
15a30 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50     for(i=0; i<pP
15a31 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29  age->nCell; i++)
15a32 7b 0a 20 20 20 20 20 20 20 20 70 63 20 3d 20 67  {.        pc = g
15a33 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 63 65  et2byte(&data[ce
15a34 6c 6c 4f 66 66 73 65 74 2b 69 2a 32 5d 29 3b 0a  llOffset+i*2]);.
15a35 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
15a36 28 20 70 63 3d 3d 69 43 65 6c 6c 46 69 72 73 74  ( pc==iCellFirst
15a37 20 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74   );.        test
15a38 63 61 73 65 28 20 70 63 3d 3d 69 43 65 6c 6c 4c  case( pc==iCellL
15a39 61 73 74 20 29 3b 0a 20 20 20 20 20 20 20 20 69  ast );.        i
15a3a 66 28 20 70 63 3c 69 43 65 6c 6c 46 69 72 73 74  f( pc<iCellFirst
15a3b 20 7c 7c 20 70 63 3e 69 43 65 6c 6c 4c 61 73 74   || pc>iCellLast
15a3c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65   ){.          re
15a3d 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
15a3e 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20  UPT_BKPT;.      
15a3f 20 20 7d 0a 20 20 20 20 20 20 20 20 73 7a 20 3d    }.        sz =
15a40 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61   cellSizePtr(pPa
15a41 67 65 2c 20 26 64 61 74 61 5b 70 63 5d 29 3b 0a  ge, &data[pc]);.
15a42 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
15a43 28 20 70 63 2b 73 7a 3d 3d 75 73 61 62 6c 65 53  ( pc+sz==usableS
15a44 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 20 20 69  ize );.        i
15a45 66 28 20 70 63 2b 73 7a 3e 75 73 61 62 6c 65 53  f( pc+sz>usableS
15a46 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ize ){.         
15a47 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
15a48 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
15a49 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
15a4a 20 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d       if( !pPage-
15a4b 3e 6c 65 61 66 20 29 20 69 43 65 6c 6c 4c 61 73  >leaf ) iCellLas
15a4c 74 2b 2b 3b 0a 20 20 20 20 7d 20 20 0a 23 65 6e  t++;.    }  .#en
15a4d 64 69 66 0a 0a 20 20 20 20 2f 2a 20 43 6f 6d 70  dif..    /* Comp
15a4e 75 74 65 20 74 68 65 20 74 6f 74 61 6c 20 66 72  ute the total fr
15a4f 65 65 20 73 70 61 63 65 20 6f 6e 20 74 68 65 20  ee space on the 
15a50 70 61 67 65 20 2a 2f 0a 20 20 20 20 70 63 20 3d  page */.    pc =
15a51 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
15a52 68 64 72 2b 31 5d 29 3b 0a 20 20 20 20 6e 46 72  hdr+1]);.    nFr
15a53 65 65 20 3d 20 64 61 74 61 5b 68 64 72 2b 37 5d  ee = data[hdr+7]
15a54 20 2b 20 74 6f 70 3b 0a 20 20 20 20 77 68 69 6c   + top;.    whil
15a55 65 28 20 70 63 3e 30 20 29 7b 0a 20 20 20 20 20  e( pc>0 ){.     
15a56 20 75 31 36 20 6e 65 78 74 2c 20 73 69 7a 65 3b   u16 next, size;
15a57 0a 20 20 20 20 20 20 69 66 28 20 70 63 3c 69 43  .      if( pc<iC
15a58 65 6c 6c 46 69 72 73 74 20 7c 7c 20 70 63 3e 69  ellFirst || pc>i
15a59 43 65 6c 6c 4c 61 73 74 20 29 7b 0a 20 20 20 20  CellLast ){.    
15a5a 20 20 20 20 2f 2a 20 53 74 61 72 74 20 6f 66 20      /* Start of 
15a5b 66 72 65 65 20 62 6c 6f 63 6b 20 69 73 20 6f 66  free block is of
15a5c 66 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20  f the page */.  
15a5d 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
15a5e 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
15a5f 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  ; .      }.     
15a60 20 6e 65 78 74 20 3d 20 67 65 74 32 62 79 74 65   next = get2byte
15a61 28 26 64 61 74 61 5b 70 63 5d 29 3b 0a 20 20 20  (&data[pc]);.   
15a62 20 20 20 73 69 7a 65 20 3d 20 67 65 74 32 62 79     size = get2by
15a63 74 65 28 26 64 61 74 61 5b 70 63 2b 32 5d 29 3b  te(&data[pc+2]);
15a64 0a 20 20 20 20 20 20 69 66 28 20 28 6e 65 78 74  .      if( (next
15a65 3e 30 20 26 26 20 6e 65 78 74 3c 3d 70 63 2b 73  >0 && next<=pc+s
15a66 69 7a 65 2b 33 29 20 7c 7c 20 70 63 2b 73 69 7a  ize+3) || pc+siz
15a67 65 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a  e>usableSize ){.
15a68 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20          /* Free 
15a69 62 6c 6f 63 6b 73 20 6d 75 73 74 20 62 65 20 69  blocks must be i
15a6a 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65  n ascending orde
15a6b 72 2e 20 41 6e 64 20 74 68 65 20 6c 61 73 74 20  r. And the last 
15a6c 62 79 74 65 20 6f 66 0a 09 2a 2a 20 74 68 65 20  byte of..** the 
15a6d 66 72 65 65 2d 62 6c 6f 63 6b 20 6d 75 73 74 20  free-block must 
15a6e 6c 69 65 20 6f 6e 20 74 68 65 20 64 61 74 61 62  lie on the datab
15a6f 61 73 65 20 70 61 67 65 2e 20 20 2a 2f 0a 20 20  ase page.  */.  
15a70 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
15a71 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
15a72 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  ; .      }.     
15a73 20 6e 46 72 65 65 20 3d 20 6e 46 72 65 65 20 2b   nFree = nFree +
15a74 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 70 63 20   size;.      pc 
15a75 3d 20 6e 65 78 74 3b 0a 20 20 20 20 7d 0a 0a 20  = next;.    }.. 
15a76 20 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f     /* At this po
15a77 69 6e 74 2c 20 6e 46 72 65 65 20 63 6f 6e 74 61  int, nFree conta
15a78 69 6e 73 20 74 68 65 20 73 75 6d 20 6f 66 20 74  ins the sum of t
15a79 68 65 20 6f 66 66 73 65 74 20 74 6f 20 74 68 65  he offset to the
15a7a 20 73 74 61 72 74 0a 20 20 20 20 2a 2a 20 6f 66   start.    ** of
15a7b 20 74 68 65 20 63 65 6c 6c 2d 63 6f 6e 74 65 6e   the cell-conten
15a7c 74 20 61 72 65 61 20 70 6c 75 73 20 74 68 65 20  t area plus the 
15a7d 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20 62  number of free b
15a7e 79 74 65 73 20 77 69 74 68 69 6e 0a 20 20 20 20  ytes within.    
15a7f 2a 2a 20 74 68 65 20 63 65 6c 6c 2d 63 6f 6e 74  ** the cell-cont
15a80 65 6e 74 20 61 72 65 61 2e 20 49 66 20 74 68 69  ent area. If thi
15a81 73 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  s is greater tha
15a82 6e 20 74 68 65 20 75 73 61 62 6c 65 2d 73 69 7a  n the usable-siz
15a83 65 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20  e.    ** of the 
15a84 70 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20 70  page, then the p
15a85 61 67 65 20 6d 75 73 74 20 62 65 20 63 6f 72 72  age must be corr
15a86 75 70 74 65 64 2e 20 54 68 69 73 20 63 68 65 63  upted. This chec
15a87 6b 20 61 6c 73 6f 0a 20 20 20 20 2a 2a 20 73 65  k also.    ** se
15a88 72 76 65 73 20 74 6f 20 76 65 72 69 66 79 20 74  rves to verify t
15a89 68 61 74 20 74 68 65 20 6f 66 66 73 65 74 20 74  hat the offset t
15a8a 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  o the start of t
15a8b 68 65 20 63 65 6c 6c 2d 63 6f 6e 74 65 6e 74 0a  he cell-content.
15a8c 20 20 20 20 2a 2a 20 61 72 65 61 2c 20 61 63 63      ** area, acc
15a8d 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 70 61  ording to the pa
15a8e 67 65 20 68 65 61 64 65 72 2c 20 6c 69 65 73 20  ge header, lies 
15a8f 77 69 74 68 69 6e 20 74 68 65 20 70 61 67 65 2e  within the page.
15a90 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
15a91 6e 46 72 65 65 3e 75 73 61 62 6c 65 53 69 7a 65  nFree>usableSize
15a92 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
15a93 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
15a94 42 4b 50 54 3b 20 0a 20 20 20 20 7d 0a 20 20 20  BKPT; .    }.   
15a95 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20   pPage->nFree = 
15a96 28 75 31 36 29 28 6e 46 72 65 65 20 2d 20 69 43  (u16)(nFree - iC
15a97 65 6c 6c 46 69 72 73 74 29 3b 0a 20 20 20 20 70  ellFirst);.    p
15a98 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 31  Page->isInit = 1
15a99 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
15a9a 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
15a9b 2a 2a 20 53 65 74 20 75 70 20 61 20 72 61 77 20  ** Set up a raw 
15a9c 70 61 67 65 20 73 6f 20 74 68 61 74 20 69 74 20  page so that it 
15a9d 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 64 61 74  looks like a dat
15a9e 61 62 61 73 65 20 70 61 67 65 20 68 6f 6c 64 69  abase page holdi
15a9f 6e 67 0a 2a 2a 20 6e 6f 20 65 6e 74 72 69 65 73  ng.** no entries
15aa0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
15aa1 20 7a 65 72 6f 50 61 67 65 28 4d 65 6d 50 61 67   zeroPage(MemPag
15aa2 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 66 6c  e *pPage, int fl
15aa3 61 67 73 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64  ags){.  unsigned
15aa4 20 63 68 61 72 20 2a 64 61 74 61 20 3d 20 70 50   char *data = pP
15aa5 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 42 74  age->aData;.  Bt
15aa6 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50  Shared *pBt = pP
15aa7 61 67 65 2d 3e 70 42 74 3b 0a 20 20 75 38 20 68  age->pBt;.  u8 h
15aa8 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f  dr = pPage->hdrO
15aa9 66 66 73 65 74 3b 0a 20 20 75 31 36 20 66 69 72  ffset;.  u16 fir
15aaa 73 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73  st;..  assert( s
15aab 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 6e  qlite3PagerPagen
15aac 75 6d 62 65 72 28 70 50 61 67 65 2d 3e 70 44 62  umber(pPage->pDb
15aad 50 61 67 65 29 3d 3d 70 50 61 67 65 2d 3e 70 67  Page)==pPage->pg
15aae 6e 6f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  no );.  assert( 
15aaf 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45  sqlite3PagerGetE
15ab0 78 74 72 61 28 70 50 61 67 65 2d 3e 70 44 62 50  xtra(pPage->pDbP
15ab1 61 67 65 29 20 3d 3d 20 28 76 6f 69 64 2a 29 70  age) == (void*)p
15ab2 50 61 67 65 20 29 3b 0a 20 20 61 73 73 65 72 74  Page );.  assert
15ab3 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  ( sqlite3PagerGe
15ab4 74 44 61 74 61 28 70 50 61 67 65 2d 3e 70 44 62  tData(pPage->pDb
15ab5 50 61 67 65 29 20 3d 3d 20 64 61 74 61 20 29 3b  Page) == data );
15ab6 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
15ab7 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
15ab8 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  le(pPage->pDbPag
15ab9 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  e) );.  assert( 
15aba 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
15abb 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  ld(pBt->mutex) )
15abc 3b 0a 20 20 2f 2a 6d 65 6d 73 65 74 28 26 64 61  ;.  /*memset(&da
15abd 74 61 5b 68 64 72 5d 2c 20 30 2c 20 70 42 74 2d  ta[hdr], 0, pBt-
15abe 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 68 64  >usableSize - hd
15abf 72 29 3b 2a 2f 0a 20 20 64 61 74 61 5b 68 64 72  r);*/.  data[hdr
15ac0 5d 20 3d 20 28 63 68 61 72 29 66 6c 61 67 73 3b  ] = (char)flags;
15ac1 0a 20 20 66 69 72 73 74 20 3d 20 68 64 72 20 2b  .  first = hdr +
15ac2 20 38 20 2b 20 34 2a 28 28 66 6c 61 67 73 26 50   8 + 4*((flags&P
15ac3 54 46 5f 4c 45 41 46 29 3d 3d 30 20 3f 31 3a 30  TF_LEAF)==0 ?1:0
15ac4 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 74  );.  memset(&dat
15ac5 61 5b 68 64 72 2b 31 5d 2c 20 30 2c 20 34 29 3b  a[hdr+1], 0, 4);
15ac6 0a 20 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 3d  .  data[hdr+7] =
15ac7 20 30 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26   0;.  put2byte(&
15ac8 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 70 42 74  data[hdr+5], pBt
15ac9 2d 3e 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a 20  ->usableSize);. 
15aca 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20   pPage->nFree = 
15acb 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20  pBt->usableSize 
15acc 2d 20 66 69 72 73 74 3b 0a 20 20 64 65 63 6f 64  - first;.  decod
15acd 65 46 6c 61 67 73 28 70 50 61 67 65 2c 20 66 6c  eFlags(pPage, fl
15ace 61 67 73 29 3b 0a 20 20 70 50 61 67 65 2d 3e 68  ags);.  pPage->h
15acf 64 72 4f 66 66 73 65 74 20 3d 20 68 64 72 3b 0a  drOffset = hdr;.
15ad0 20 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66    pPage->cellOff
15ad1 73 65 74 20 3d 20 66 69 72 73 74 3b 0a 20 20 70  set = first;.  p
15ad2 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20  Page->nOverflow 
15ad3 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70  = 0;.  assert( p
15ad4 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3d 35 31  Bt->pageSize>=51
15ad5 32 20 26 26 20 70 42 74 2d 3e 70 61 67 65 53 69  2 && pBt->pageSi
15ad6 7a 65 3c 3d 33 32 37 36 38 20 29 3b 0a 20 20 70  ze<=32768 );.  p
15ad7 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65 20 3d  Page->maskPage =
15ad8 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d   pBt->pageSize -
15ad9 20 31 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 43 65   1;.  pPage->nCe
15ada 6c 6c 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 2d  ll = 0;.  pPage-
15adb 3e 69 73 49 6e 69 74 20 3d 20 31 3b 0a 7d 0a 0a  >isInit = 1;.}..
15adc 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61  ./*.** Convert a
15add 20 44 62 50 61 67 65 20 6f 62 74 61 69 6e 65 64   DbPage obtained
15ade 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72 20   from the pager 
15adf 69 6e 74 6f 20 61 20 4d 65 6d 50 61 67 65 20 75  into a MemPage u
15ae0 73 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 62 74  sed by.** the bt
15ae1 72 65 65 20 6c 61 79 65 72 2e 0a 2a 2f 0a 73 74  ree layer..*/.st
15ae2 61 74 69 63 20 4d 65 6d 50 61 67 65 20 2a 62 74  atic MemPage *bt
15ae3 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67  reePageFromDbPag
15ae4 65 28 44 62 50 61 67 65 20 2a 70 44 62 50 61 67  e(DbPage *pDbPag
15ae5 65 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 42 74  e, Pgno pgno, Bt
15ae6 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20  Shared *pBt){.  
15ae7 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d  MemPage *pPage =
15ae8 20 28 4d 65 6d 50 61 67 65 2a 29 73 71 6c 69 74   (MemPage*)sqlit
15ae9 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28  e3PagerGetExtra(
15aea 70 44 62 50 61 67 65 29 3b 0a 20 20 70 50 61 67  pDbPage);.  pPag
15aeb 65 2d 3e 61 44 61 74 61 20 3d 20 73 71 6c 69 74  e->aData = sqlit
15aec 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70  e3PagerGetData(p
15aed 44 62 50 61 67 65 29 3b 0a 20 20 70 50 61 67 65  DbPage);.  pPage
15aee 2d 3e 70 44 62 50 61 67 65 20 3d 20 70 44 62 50  ->pDbPage = pDbP
15aef 61 67 65 3b 0a 20 20 70 50 61 67 65 2d 3e 70 42  age;.  pPage->pB
15af0 74 20 3d 20 70 42 74 3b 0a 20 20 70 50 61 67 65  t = pBt;.  pPage
15af1 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e 6f 3b 0a 20  ->pgno = pgno;. 
15af2 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65   pPage->hdrOffse
15af3 74 20 3d 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3d  t = pPage->pgno=
15af4 3d 31 20 3f 20 31 30 30 20 3a 20 30 3b 0a 20 20  =1 ? 100 : 0;.  
15af5 72 65 74 75 72 6e 20 70 50 61 67 65 3b 20 0a 7d  return pPage; .}
15af6 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 70 61  ../*.** Get a pa
15af7 67 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65  ge from the page
15af8 72 2e 20 20 49 6e 69 74 69 61 6c 69 7a 65 20 74  r.  Initialize t
15af9 68 65 20 4d 65 6d 50 61 67 65 2e 70 42 74 20 61  he MemPage.pBt a
15afa 6e 64 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 44  nd.** MemPage.aD
15afb 61 74 61 20 65 6c 65 6d 65 6e 74 73 20 69 66 20  ata elements if 
15afc 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  needed..**.** If
15afd 20 74 68 65 20 6e 6f 43 6f 6e 74 65 6e 74 20 66   the noContent f
15afe 6c 61 67 20 69 73 20 73 65 74 2c 20 69 74 20 6d  lag is set, it m
15aff 65 61 6e 73 20 74 68 61 74 20 77 65 20 64 6f 20  eans that we do 
15b00 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 74 0a 2a  not care about.*
15b01 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  * the content of
15b02 20 74 68 65 20 70 61 67 65 20 61 74 20 74 68 69   the page at thi
15b03 73 20 74 69 6d 65 2e 20 20 53 6f 20 64 6f 20 6e  s time.  So do n
15b04 6f 74 20 67 6f 20 74 6f 20 74 68 65 20 64 69 73  ot go to the dis
15b05 6b 0a 2a 2a 20 74 6f 20 66 65 74 63 68 20 74 68  k.** to fetch th
15b06 65 20 63 6f 6e 74 65 6e 74 2e 20 20 4a 75 73 74  e content.  Just
15b07 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 63 6f 6e   fill in the con
15b08 74 65 6e 74 20 77 69 74 68 20 7a 65 72 6f 73 20  tent with zeros 
15b09 66 6f 72 20 6e 6f 77 2e 0a 2a 2a 20 49 66 20 69  for now..** If i
15b0a 6e 20 74 68 65 20 66 75 74 75 72 65 20 77 65 20  n the future we 
15b0b 63 61 6c 6c 20 73 71 6c 69 74 65 33 50 61 67 65  call sqlite3Page
15b0c 72 57 72 69 74 65 28 29 20 6f 6e 20 74 68 69 73  rWrite() on this
15b0d 20 70 61 67 65 2c 20 74 68 61 74 0a 2a 2a 20 6d   page, that.** m
15b0e 65 61 6e 73 20 77 65 20 68 61 76 65 20 73 74 61  eans we have sta
15b0f 72 74 65 64 20 74 6f 20 62 65 20 63 6f 6e 63 65  rted to be conce
15b10 72 6e 65 64 20 61 62 6f 75 74 20 63 6f 6e 74 65  rned about conte
15b11 6e 74 20 61 6e 64 20 74 68 65 20 64 69 73 6b 0a  nt and the disk.
15b12 2a 2a 20 72 65 61 64 20 73 68 6f 75 6c 64 20 6f  ** read should o
15b13 63 63 75 72 20 61 74 20 74 68 61 74 20 70 6f 69  ccur at that poi
15b14 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  nt..*/.static in
15b15 74 20 62 74 72 65 65 47 65 74 50 61 67 65 28 0a  t btreeGetPage(.
15b16 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c    BtShared *pBt,
15b17 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74         /* The bt
15b18 72 65 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67  ree */.  Pgno pg
15b19 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  no,           /*
15b1a 20 4e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70   Number of the p
15b1b 61 67 65 20 74 6f 20 66 65 74 63 68 20 2a 2f 0a  age to fetch */.
15b1c 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50 61    MemPage **ppPa
15b1d 67 65 2c 20 20 20 20 2f 2a 20 52 65 74 75 72 6e  ge,    /* Return
15b1e 20 74 68 65 20 70 61 67 65 20 69 6e 20 74 68 69   the page in thi
15b1f 73 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20  s parameter */. 
15b20 20 69 6e 74 20 6e 6f 43 6f 6e 74 65 6e 74 20 20   int noContent  
15b21 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20        /* Do not 
15b22 6c 6f 61 64 20 70 61 67 65 20 63 6f 6e 74 65 6e  load page conten
15b23 74 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a  t if true */.){.
15b24 20 20 69 6e 74 20 72 63 3b 0a 20 20 44 62 50 61    int rc;.  DbPa
15b25 67 65 20 2a 70 44 62 50 61 67 65 3b 0a 0a 20 20  ge *pDbPage;..  
15b26 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
15b27 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e  mutex_held(pBt->
15b28 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d  mutex) );.  rc =
15b29 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 71   sqlite3PagerAcq
15b2a 75 69 72 65 28 70 42 74 2d 3e 70 50 61 67 65 72  uire(pBt->pPager
15b2b 2c 20 70 67 6e 6f 2c 20 28 44 62 50 61 67 65 2a  , pgno, (DbPage*
15b2c 2a 29 26 70 44 62 50 61 67 65 2c 20 6e 6f 43 6f  *)&pDbPage, noCo
15b2d 6e 74 65 6e 74 29 3b 0a 20 20 69 66 28 20 72 63  ntent);.  if( rc
15b2e 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
15b2f 2a 70 70 50 61 67 65 20 3d 20 62 74 72 65 65 50  *ppPage = btreeP
15b30 61 67 65 46 72 6f 6d 44 62 50 61 67 65 28 70 44  ageFromDbPage(pD
15b31 62 50 61 67 65 2c 20 70 67 6e 6f 2c 20 70 42 74  bPage, pgno, pBt
15b32 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
15b33 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
15b34 52 65 74 72 69 65 76 65 20 61 20 70 61 67 65 20  Retrieve a page 
15b35 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72 20 63  from the pager c
15b36 61 63 68 65 2e 20 49 66 20 74 68 65 20 72 65 71  ache. If the req
15b37 75 65 73 74 65 64 20 70 61 67 65 20 69 73 20 6e  uested page is n
15b38 6f 74 0a 2a 2a 20 61 6c 72 65 61 64 79 20 69 6e  ot.** already in
15b39 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65   the pager cache
15b3a 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 49 6e   return NULL. In
15b3b 69 74 69 61 6c 69 7a 65 20 74 68 65 20 4d 65 6d  itialize the Mem
15b3c 50 61 67 65 2e 70 42 74 20 61 6e 64 0a 2a 2a 20  Page.pBt and.** 
15b3d 4d 65 6d 50 61 67 65 2e 61 44 61 74 61 20 65 6c  MemPage.aData el
15b3e 65 6d 65 6e 74 73 20 69 66 20 6e 65 65 64 65 64  ements if needed
15b3f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 4d 65 6d 50  ..*/.static MemP
15b40 61 67 65 20 2a 62 74 72 65 65 50 61 67 65 4c 6f  age *btreePageLo
15b41 6f 6b 75 70 28 42 74 53 68 61 72 65 64 20 2a 70  okup(BtShared *p
15b42 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a  Bt, Pgno pgno){.
15b43 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67    DbPage *pDbPag
15b44 65 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  e;.  assert( sql
15b45 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
15b46 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
15b47 20 70 44 62 50 61 67 65 20 3d 20 73 71 6c 69 74   pDbPage = sqlit
15b48 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 70 42  e3PagerLookup(pB
15b49 74 2d 3e 70 50 61 67 65 72 2c 20 70 67 6e 6f 29  t->pPager, pgno)
15b4a 3b 0a 20 20 69 66 28 20 70 44 62 50 61 67 65 20  ;.  if( pDbPage 
15b4b 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 62 74  ){.    return bt
15b4c 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67  reePageFromDbPag
15b4d 65 28 70 44 62 50 61 67 65 2c 20 70 67 6e 6f 2c  e(pDbPage, pgno,
15b4e 20 70 42 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74   pBt);.  }.  ret
15b4f 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
15b50 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20  Return the size 
15b51 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
15b52 66 69 6c 65 20 69 6e 20 70 61 67 65 73 2e 20 49  file in pages. I
15b53 66 20 74 68 65 72 65 20 69 73 20 61 6e 79 20 6b  f there is any k
15b54 69 6e 64 20 6f 66 0a 2a 2a 20 65 72 72 6f 72 2c  ind of.** error,
15b55 20 72 65 74 75 72 6e 20 28 28 75 6e 73 69 67 6e   return ((unsign
15b56 65 64 20 69 6e 74 29 2d 31 29 2e 0a 2a 2f 0a 73  ed int)-1)..*/.s
15b57 74 61 74 69 63 20 50 67 6e 6f 20 70 61 67 65 72  tatic Pgno pager
15b58 50 61 67 65 63 6f 75 6e 74 28 42 74 53 68 61 72  Pagecount(BtShar
15b59 65 64 20 2a 70 42 74 29 7b 0a 20 20 69 6e 74 20  ed *pBt){.  int 
15b5a 6e 50 61 67 65 20 3d 20 2d 31 3b 0a 20 20 69 6e  nPage = -1;.  in
15b5b 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20  t rc;.  assert( 
15b5c 70 42 74 2d 3e 70 50 61 67 65 31 20 29 3b 0a 20  pBt->pPage1 );. 
15b5d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
15b5e 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d  erPagecount(pBt-
15b5f 3e 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29  >pPager, &nPage)
15b60 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d  ;.  assert( rc==
15b61 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 6e 50 61  SQLITE_OK || nPa
15b62 67 65 3d 3d 2d 31 20 29 3b 0a 20 20 72 65 74 75  ge==-1 );.  retu
15b63 72 6e 20 28 50 67 6e 6f 29 6e 50 61 67 65 3b 0a  rn (Pgno)nPage;.
15b64 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 70  }../*.** Get a p
15b65 61 67 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67  age from the pag
15b66 65 72 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a  er and initializ
15b67 65 20 69 74 2e 20 20 54 68 69 73 20 72 6f 75 74  e it.  This rout
15b68 69 6e 65 20 69 73 20 6a 75 73 74 20 61 0a 2a 2a  ine is just a.**
15b69 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 77 72 61   convenience wra
15b6a 70 70 65 72 20 61 72 6f 75 6e 64 20 73 65 70 61  pper around sepa
15b6b 72 61 74 65 20 63 61 6c 6c 73 20 74 6f 20 62 74  rate calls to bt
15b6c 72 65 65 47 65 74 50 61 67 65 28 29 20 61 6e 64  reeGetPage() and
15b6d 20 0a 2a 2a 20 62 74 72 65 65 49 6e 69 74 50 61   .** btreeInitPa
15b6e 67 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  ge()..**.** If a
15b6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
15b70 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 2a  then the value *
15b71 70 70 50 61 67 65 20 69 73 20 73 65 74 20 74 6f  ppPage is set to
15b72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 20 49   is undefined. I
15b73 74 0a 2a 2a 20 6d 61 79 20 72 65 6d 61 69 6e 20  t.** may remain 
15b74 75 6e 63 68 61 6e 67 65 64 2c 20 6f 72 20 69 74  unchanged, or it
15b75 20 6d 61 79 20 62 65 20 73 65 74 20 74 6f 20 61   may be set to a
15b76 6e 20 69 6e 76 61 6c 69 64 20 76 61 6c 75 65 2e  n invalid value.
15b77 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67  .*/.static int g
15b78 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 0a 20  etAndInitPage(. 
15b79 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20   BtShared *pBt, 
15b7a 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
15b7b 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f  database file */
15b7c 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20  .  Pgno pgno,   
15b7d 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
15b7e 72 20 6f 66 20 74 68 65 20 70 61 67 65 20 74 6f  r of the page to
15b7f 20 67 65 74 20 2a 2f 0a 20 20 4d 65 6d 50 61 67   get */.  MemPag
15b80 65 20 2a 2a 70 70 50 61 67 65 20 20 20 20 20 2f  e **ppPage     /
15b81 2a 20 57 72 69 74 65 20 74 68 65 20 70 61 67 65  * Write the page
15b82 20 70 6f 69 6e 74 65 72 20 68 65 72 65 20 2a 2f   pointer here */
15b83 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
15b84 54 45 53 54 4f 4e 4c 59 28 20 50 67 6e 6f 20 69  TESTONLY( Pgno i
15b85 4c 61 73 74 50 67 20 3d 20 70 61 67 65 72 50 61  LastPg = pagerPa
15b86 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 20 29 0a  gecount(pBt); ).
15b87 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
15b88 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74  3_mutex_held(pBt
15b89 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 72  ->mutex) );..  r
15b8a 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65  c = btreeGetPage
15b8b 28 70 42 74 2c 20 70 67 6e 6f 2c 20 70 70 50 61  (pBt, pgno, ppPa
15b8c 67 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63  ge, 0);.  if( rc
15b8d 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
15b8e 20 20 20 72 63 20 3d 20 62 74 72 65 65 49 6e 69     rc = btreeIni
15b8f 74 50 61 67 65 28 2a 70 70 50 61 67 65 29 3b 0a  tPage(*ppPage);.
15b90 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
15b91 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
15b92 65 6c 65 61 73 65 50 61 67 65 28 2a 70 70 50 61  eleasePage(*ppPa
15b93 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ge);.    }.  }..
15b94 20 20 2f 2a 20 49 66 20 74 68 65 20 72 65 71 75    /* If the requ
15b95 65 73 74 65 64 20 70 61 67 65 20 6e 75 6d 62 65  ested page numbe
15b96 72 20 77 61 73 20 65 69 74 68 65 72 20 30 20 6f  r was either 0 o
15b97 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74  r greater than t
15b98 68 65 20 70 61 67 65 0a 20 20 2a 2a 20 6e 75 6d  he page.  ** num
15b99 62 65 72 20 6f 66 20 74 68 65 20 6c 61 73 74 20  ber of the last 
15b9a 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61  page in the data
15b9b 62 61 73 65 2c 20 74 68 69 73 20 66 75 6e 63 74  base, this funct
15b9c 69 6f 6e 20 73 68 6f 75 6c 64 20 72 65 74 75 72  ion should retur
15b9d 6e 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 43 4f  n.  ** SQLITE_CO
15b9e 52 52 55 50 54 20 6f 72 20 73 6f 6d 65 20 6f 74  RRUPT or some ot
15b9f 68 65 72 20 65 72 72 6f 72 20 28 69 2e 65 2e 20  her error (i.e. 
15ba0 53 51 4c 49 54 45 5f 46 55 4c 4c 29 2e 20 43 68  SQLITE_FULL). Ch
15ba1 65 63 6b 20 74 68 61 74 20 74 68 69 73 0a 20 20  eck that this.  
15ba2 2a 2a 20 69 73 20 74 68 65 20 63 61 73 65 2e 20  ** is the case. 
15ba3 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70   */.  assert( (p
15ba4 67 6e 6f 3e 30 20 26 26 20 70 67 6e 6f 3c 3d 69  gno>0 && pgno<=i
15ba5 4c 61 73 74 50 67 29 20 7c 7c 20 72 63 21 3d 53  LastPg) || rc!=S
15ba6 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 74 65  QLITE_OK );.  te
15ba7 73 74 63 61 73 65 28 20 70 67 6e 6f 3d 3d 30 20  stcase( pgno==0 
15ba8 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70  );.  testcase( p
15ba9 67 6e 6f 3d 3d 69 4c 61 73 74 50 67 20 29 3b 0a  gno==iLastPg );.
15baa 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
15bab 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61  ./*.** Release a
15bac 20 4d 65 6d 50 61 67 65 2e 20 20 54 68 69 73 20   MemPage.  This 
15bad 73 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64  should be called
15bae 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 70   once for each p
15baf 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20  rior.** call to 
15bb0 62 74 72 65 65 47 65 74 50 61 67 65 2e 0a 2a 2f  btreeGetPage..*/
15bb1 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c  .static void rel
15bb2 65 61 73 65 50 61 67 65 28 4d 65 6d 50 61 67 65  easePage(MemPage
15bb3 20 2a 70 50 61 67 65 29 7b 0a 20 20 69 66 28 20   *pPage){.  if( 
15bb4 70 50 61 67 65 20 29 7b 0a 20 20 20 20 61 73 73  pPage ){.    ass
15bb5 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65  ert( pPage->nOve
15bb6 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 73 71 6c 69  rflow==0 || sqli
15bb7 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66 63  te3PagerPageRefc
15bb8 6f 75 6e 74 28 70 50 61 67 65 2d 3e 70 44 62 50  ount(pPage->pDbP
15bb9 61 67 65 29 3e 31 20 29 3b 0a 20 20 20 20 61 73  age)>1 );.    as
15bba 73 65 72 74 28 20 70 50 61 67 65 2d 3e 61 44 61  sert( pPage->aDa
15bbb 74 61 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  ta );.    assert
15bbc 28 20 70 50 61 67 65 2d 3e 70 42 74 20 29 3b 0a  ( pPage->pBt );.
15bbd 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
15bbe 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61  te3PagerGetExtra
15bbf 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
15bc0 20 3d 3d 20 28 76 6f 69 64 2a 29 70 50 61 67 65   == (void*)pPage
15bc1 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
15bc2 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44  sqlite3PagerGetD
15bc3 61 74 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ata(pPage->pDbPa
15bc4 67 65 29 3d 3d 70 50 61 67 65 2d 3e 61 44 61 74  ge)==pPage->aDat
15bc5 61 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  a );.    assert(
15bc6 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
15bc7 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e  eld(pPage->pBt->
15bc8 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 73 71  mutex) );.    sq
15bc9 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28  lite3PagerUnref(
15bca 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b  pPage->pDbPage);
15bcb 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 75  .  }.}../*.** Du
15bcc 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2c  ring a rollback,
15bcd 20 77 68 65 6e 20 74 68 65 20 70 61 67 65 72 20   when the pager 
15bce 72 65 6c 6f 61 64 73 20 69 6e 66 6f 72 6d 61 74  reloads informat
15bcf 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 63 61 63  ion into the cac
15bd0 68 65 0a 2a 2a 20 73 6f 20 74 68 61 74 20 74 68  he.** so that th
15bd1 65 20 63 61 63 68 65 20 69 73 20 72 65 73 74 6f  e cache is resto
15bd2 72 65 64 20 74 6f 20 69 74 73 20 6f 72 69 67 69  red to its origi
15bd3 6e 61 6c 20 73 74 61 74 65 20 61 74 20 74 68 65  nal state at the
15bd4 20 73 74 61 72 74 20 6f 66 0a 2a 2a 20 74 68 65   start of.** the
15bd5 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 66 6f   transaction, fo
15bd6 72 20 65 61 63 68 20 70 61 67 65 20 72 65 73 74  r each page rest
15bd7 6f 72 65 64 20 74 68 69 73 20 72 6f 75 74 69 6e  ored this routin
15bd8 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a  e is called..**.
15bd9 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
15bda 6e 65 65 64 73 20 74 6f 20 72 65 73 65 74 20 74  needs to reset t
15bdb 68 65 20 65 78 74 72 61 20 64 61 74 61 20 73 65  he extra data se
15bdc 63 74 69 6f 6e 20 61 74 20 74 68 65 20 65 6e 64  ction at the end
15bdd 20 6f 66 20 74 68 65 0a 2a 2a 20 70 61 67 65 20   of the.** page 
15bde 74 6f 20 61 67 72 65 65 20 77 69 74 68 20 74 68  to agree with th
15bdf 65 20 72 65 73 74 6f 72 65 64 20 64 61 74 61 2e  e restored data.
15be0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
15be1 70 61 67 65 52 65 69 6e 69 74 28 44 62 50 61 67  pageReinit(DbPag
15be2 65 20 2a 70 44 61 74 61 29 7b 0a 20 20 4d 65 6d  e *pData){.  Mem
15be3 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 70  Page *pPage;.  p
15be4 50 61 67 65 20 3d 20 28 4d 65 6d 50 61 67 65 20  Page = (MemPage 
15be5 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  *)sqlite3PagerGe
15be6 74 45 78 74 72 61 28 70 44 61 74 61 29 3b 0a 20  tExtra(pData);. 
15be7 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
15be8 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e  PagerPageRefcoun
15be9 74 28 70 44 61 74 61 29 3e 30 20 29 3b 0a 20 20  t(pData)>0 );.  
15bea 69 66 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69  if( pPage->isIni
15beb 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  t ){.    assert(
15bec 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
15bed 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e  eld(pPage->pBt->
15bee 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 70 50  mutex) );.    pP
15bef 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b  age->isInit = 0;
15bf0 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
15bf1 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e  PagerPageRefcoun
15bf2 74 28 70 44 61 74 61 29 3e 31 20 29 7b 0a 20 20  t(pData)>1 ){.  
15bf3 20 20 20 20 2f 2a 20 70 50 61 67 65 20 6d 69 67      /* pPage mig
15bf4 68 74 20 6e 6f 74 20 62 65 20 61 20 62 74 72 65  ht not be a btre
15bf5 65 20 70 61 67 65 3b 20 20 69 74 20 6d 69 67 68  e page;  it migh
15bf6 74 20 62 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77  t be an overflow
15bf7 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 6f   page.      ** o
15bf8 72 20 70 74 72 6d 61 70 20 70 61 67 65 20 6f 72  r ptrmap page or
15bf9 20 61 20 66 72 65 65 20 70 61 67 65 2e 20 20 49   a free page.  I
15bfa 6e 20 74 68 6f 73 65 20 63 61 73 65 73 2c 20 74  n those cases, t
15bfb 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 20  he following.   
15bfc 20 20 20 2a 2a 20 63 61 6c 6c 20 74 6f 20 62 74     ** call to bt
15bfd 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 77 69  reeInitPage() wi
15bfe 6c 6c 20 6c 69 6b 65 6c 79 20 72 65 74 75 72 6e  ll likely return
15bff 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2e   SQLITE_CORRUPT.
15c00 0a 20 20 20 20 20 20 2a 2a 20 42 75 74 20 6e 6f  .      ** But no
15c01 20 68 61 72 6d 20 69 73 20 64 6f 6e 65 20 62 79   harm is done by
15c02 20 74 68 69 73 2e 20 20 41 6e 64 20 69 74 20 69   this.  And it i
15c03 73 20 76 65 72 79 20 69 6d 70 6f 72 74 61 6e 74  s very important
15c04 20 74 68 61 74 0a 20 20 20 20 20 20 2a 2a 20 62   that.      ** b
15c05 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 62  treeInitPage() b
15c06 65 20 63 61 6c 6c 65 64 20 6f 6e 20 65 76 65 72  e called on ever
15c07 79 20 62 74 72 65 65 20 70 61 67 65 20 73 6f 20  y btree page so 
15c08 77 65 20 6d 61 6b 65 0a 20 20 20 20 20 20 2a 2a  we make.      **
15c09 20 74 68 65 20 63 61 6c 6c 20 66 6f 72 20 65 76   the call for ev
15c0a 65 72 79 20 70 61 67 65 20 74 68 61 74 20 63 6f  ery page that co
15c0b 6d 65 73 20 69 6e 20 66 6f 72 20 72 65 2d 69 6e  mes in for re-in
15c0c 69 74 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 20 20  iting. */.      
15c0d 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 50  btreeInitPage(pP
15c0e 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  age);.    }.  }.
15c0f 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20  }../*.** Invoke 
15c10 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72  the busy handler
15c11 20 66 6f 72 20 61 20 62 74 72 65 65 2e 0a 2a 2f   for a btree..*/
15c12 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65  .static int btre
15c13 65 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c  eInvokeBusyHandl
15c14 65 72 28 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a  er(void *pArg){.
15c15 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
15c16 3d 20 28 42 74 53 68 61 72 65 64 2a 29 70 41 72  = (BtShared*)pAr
15c17 67 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74  g;.  assert( pBt
15c18 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74  ->db );.  assert
15c19 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
15c1a 68 65 6c 64 28 70 42 74 2d 3e 64 62 2d 3e 6d 75  held(pBt->db->mu
15c1b 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e  tex) );.  return
15c1c 20 73 71 6c 69 74 65 33 49 6e 76 6f 6b 65 42 75   sqlite3InvokeBu
15c1d 73 79 48 61 6e 64 6c 65 72 28 26 70 42 74 2d 3e  syHandler(&pBt->
15c1e 64 62 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72 29  db->busyHandler)
15c1f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20  ;.}../*.** Open 
15c20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  a database file.
15c21 0a 2a 2a 20 0a 2a 2a 20 7a 46 69 6c 65 6e 61 6d  .** .** zFilenam
15c22 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  e is the name of
15c23 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
15c24 6c 65 2e 20 20 49 66 20 7a 46 69 6c 65 6e 61 6d  le.  If zFilenam
15c25 65 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 61 20 6e  e is NULL.** a n
15c26 65 77 20 64 61 74 61 62 61 73 65 20 77 69 74 68  ew database with
15c27 20 61 20 72 61 6e 64 6f 6d 20 6e 61 6d 65 20 69   a random name i
15c28 73 20 63 72 65 61 74 65 64 2e 20 20 54 68 69 73  s created.  This
15c29 20 72 61 6e 64 6f 6d 6c 79 20 6e 61 6d 65 64 0a   randomly named.
15c2a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ** database file
15c2b 20 77 69 6c 6c 20 62 65 20 64 65 6c 65 74 65 64   will be deleted
15c2c 20 77 68 65 6e 20 73 71 6c 69 74 65 33 42 74 72   when sqlite3Btr
15c2d 65 65 43 6c 6f 73 65 28 29 20 69 73 20 63 61 6c  eeClose() is cal
15c2e 6c 65 64 2e 0a 2a 2a 20 49 66 20 7a 46 69 6c 65  led..** If zFile
15c2f 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f 72 79  name is ":memory
15c30 3a 22 20 74 68 65 6e 20 61 6e 20 69 6e 2d 6d 65  :" then an in-me
15c31 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 69 73  mory database is
15c32 20 63 72 65 61 74 65 64 0a 2a 2a 20 74 68 61 74   created.** that
15c33 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c   is automaticall
15c34 79 20 64 65 73 74 72 6f 79 65 64 20 77 68 65 6e  y destroyed when
15c35 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a   it is closed..*
15c36 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61  *.** If the data
15c37 62 61 73 65 20 69 73 20 61 6c 72 65 61 64 79 20  base is already 
15c38 6f 70 65 6e 65 64 20 69 6e 20 74 68 65 20 73 61  opened in the sa
15c39 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  me database conn
15c3a 65 63 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 77 65  ection.** and we
15c3b 20 61 72 65 20 69 6e 20 73 68 61 72 65 64 20 63   are in shared c
15c3c 61 63 68 65 20 6d 6f 64 65 2c 20 74 68 65 6e 20  ache mode, then 
15c3d 74 68 65 20 6f 70 65 6e 20 77 69 6c 6c 20 66 61  the open will fa
15c3e 69 6c 20 77 69 74 68 20 61 6e 0a 2a 2a 20 53 51  il with an.** SQ
15c3f 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20  LITE_CONSTRAINT 
15c40 65 72 72 6f 72 2e 20 20 57 65 20 63 61 6e 6e 6f  error.  We canno
15c41 74 20 61 6c 6c 6f 77 20 74 77 6f 20 6f 72 20 6d  t allow two or m
15c42 6f 72 65 20 42 74 53 68 61 72 65 64 0a 2a 2a 20  ore BtShared.** 
15c43 6f 62 6a 65 63 74 73 20 69 6e 20 74 68 65 20 73  objects in the s
15c44 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ame database con
15c45 6e 65 63 74 69 6f 6e 20 73 69 6e 63 65 20 64 6f  nection since do
15c46 69 6e 67 20 73 6f 20 77 69 6c 6c 20 6c 65 61 64  ing so will lead
15c47 0a 2a 2a 20 74 6f 20 70 72 6f 62 6c 65 6d 73 20  .** to problems 
15c48 77 69 74 68 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f  with locking..*/
15c49 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
15c4a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
15c4b 4f 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68  Open(.  const ch
15c4c 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20  ar *zFilename,  
15c4d 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66  /* Name of the f
15c4e 69 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ile containing t
15c4f 68 65 20 42 54 72 65 65 20 64 61 74 61 62 61 73  he BTree databas
15c50 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  e */.  sqlite3 *
15c51 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  db,            /
15c52 2a 20 41 73 73 6f 63 69 61 74 65 64 20 64 61 74  * Associated dat
15c53 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a  abase handle */.
15c54 20 20 42 74 72 65 65 20 2a 2a 70 70 42 74 72 65    Btree **ppBtre
15c55 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69  e,        /* Poi
15c56 6e 74 65 72 20 74 6f 20 6e 65 77 20 42 74 72 65  nter to new Btre
15c57 65 20 6f 62 6a 65 63 74 20 77 72 69 74 74 65 6e  e object written
15c58 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66   here */.  int f
15c59 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20  lags,           
15c5a 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 73 20 2a 2f     /* Options */
15c5b 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73 20  .  int vfsFlags 
15c5c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c             /* Fl
15c5d 61 67 73 20 70 61 73 73 65 64 20 74 68 72 6f 75  ags passed throu
15c5e 67 68 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 66  gh to sqlite3_vf
15c5f 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 29 7b 0a  s.xOpen() */.){.
15c60 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
15c61 56 66 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  Vfs;            
15c62 20 2f 2a 20 54 68 65 20 56 46 53 20 74 6f 20 75   /* The VFS to u
15c63 73 65 20 66 6f 72 20 74 68 69 73 20 62 74 72 65  se for this btre
15c64 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20  e */.  BtShared 
15c65 2a 70 42 74 20 3d 20 30 3b 20 20 20 20 20 20 20  *pBt = 0;       
15c66 20 20 20 20 20 20 2f 2a 20 53 68 61 72 65 64 20        /* Shared 
15c67 70 61 72 74 20 6f 66 20 62 74 72 65 65 20 73 74  part of btree st
15c68 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 42 74 72  ructure */.  Btr
15c69 65 65 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20  ee *p;          
15c6a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48              /* H
15c6b 61 6e 64 6c 65 20 74 6f 20 72 65 74 75 72 6e 20  andle to return 
15c6c 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  */.  sqlite3_mut
15c6d 65 78 20 2a 6d 75 74 65 78 4f 70 65 6e 20 3d 20  ex *mutexOpen = 
15c6e 30 3b 20 20 2f 2a 20 50 72 65 76 65 6e 74 73 20  0;  /* Prevents 
15c6f 61 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e  a race condition
15c70 2e 20 54 69 63 6b 65 74 20 23 33 35 33 37 20 2a  . Ticket #3537 *
15c71 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  /.  int rc = SQL
15c72 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20  ITE_OK;         
15c73 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64     /* Result cod
15c74 65 20 66 72 6f 6d 20 74 68 69 73 20 66 75 6e 63  e from this func
15c75 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 20 6e 52 65  tion */.  u8 nRe
15c76 73 65 72 76 65 3b 20 20 20 20 20 20 20 20 20 20  serve;          
15c77 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65           /* Byte
15c78 20 6f 66 20 75 6e 75 73 65 64 20 73 70 61 63 65   of unused space
15c79 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 2a 2f   on each page */
15c7a 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
15c7b 20 7a 44 62 48 65 61 64 65 72 5b 31 30 30 5d 3b   zDbHeader[100];
15c7c 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 65    /* Database he
15c7d 61 64 65 72 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a  ader content */.
15c7e 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 76 61  .  /* Set the va
15c7f 72 69 61 62 6c 65 20 69 73 4d 65 6d 64 62 20 74  riable isMemdb t
15c80 6f 20 74 72 75 65 20 66 6f 72 20 61 6e 20 69 6e  o true for an in
15c81 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65  -memory database
15c82 2c 20 6f 72 20 0a 20 20 2a 2a 20 66 61 6c 73 65  , or .  ** false
15c83 20 66 6f 72 20 61 20 66 69 6c 65 2d 62 61 73 65   for a file-base
15c84 64 20 64 61 74 61 62 61 73 65 2e 20 54 68 69 73  d database. This
15c85 20 73 79 6d 62 6f 6c 20 69 73 20 6f 6e 6c 79 20   symbol is only 
15c86 72 65 71 75 69 72 65 64 20 69 66 0a 20 20 2a 2a  required if.  **
15c87 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 20 73   either of the s
15c88 68 61 72 65 64 2d 64 61 74 61 20 6f 72 20 61 75  hared-data or au
15c89 74 6f 76 61 63 75 75 6d 20 66 65 61 74 75 72 65  tovacuum feature
15c8a 73 20 61 72 65 20 63 6f 6d 70 69 6c 65 64 20 0a  s are compiled .
15c8b 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 6c 69    ** into the li
15c8c 62 72 61 72 79 2e 0a 20 20 2a 2f 0a 23 69 66 20  brary..  */.#if 
15c8d 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
15c8e 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
15c8f 45 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53  E) || !defined(S
15c90 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
15c91 41 43 55 55 4d 29 0a 20 20 23 69 66 64 65 66 20  ACUUM).  #ifdef 
15c92 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f  SQLITE_OMIT_MEMO
15c93 52 59 44 42 0a 20 20 20 20 63 6f 6e 73 74 20 69  RYDB.    const i
15c94 6e 74 20 69 73 4d 65 6d 64 62 20 3d 20 30 3b 0a  nt isMemdb = 0;.
15c95 20 20 23 65 6c 73 65 0a 20 20 20 20 63 6f 6e 73    #else.    cons
15c96 74 20 69 6e 74 20 69 73 4d 65 6d 64 62 20 3d 20  t int isMemdb = 
15c97 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 21 73 74  zFilename && !st
15c98 72 63 6d 70 28 7a 46 69 6c 65 6e 61 6d 65 2c 20  rcmp(zFilename, 
15c99 22 3a 6d 65 6d 6f 72 79 3a 22 29 3b 0a 20 20 23  ":memory:");.  #
15c9a 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 20 20  endif.#endif..  
15c9b 61 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b  assert( db!=0 );
15c9c 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
15c9d 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62  e3_mutex_held(db
15c9e 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 70  ->mutex) );..  p
15c9f 56 66 73 20 3d 20 64 62 2d 3e 70 56 66 73 3b 0a  Vfs = db->pVfs;.
15ca0 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c    p = sqlite3Mal
15ca1 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 6f 66 28 42  locZero(sizeof(B
15ca2 74 72 65 65 29 29 3b 0a 20 20 69 66 28 20 21 70  tree));.  if( !p
15ca3 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
15ca4 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
15ca5 0a 20 20 70 2d 3e 69 6e 54 72 61 6e 73 20 3d 20  .  p->inTrans = 
15ca6 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 70 2d  TRANS_NONE;.  p-
15ca7 3e 64 62 20 3d 20 64 62 3b 0a 23 69 66 6e 64 65  >db = db;.#ifnde
15ca8 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
15ca9 41 52 45 44 5f 43 41 43 48 45 0a 20 20 70 2d 3e  ARED_CACHE.  p->
15caa 6c 6f 63 6b 2e 70 42 74 72 65 65 20 3d 20 70 3b  lock.pBtree = p;
15cab 0a 20 20 70 2d 3e 6c 6f 63 6b 2e 69 54 61 62 6c  .  p->lock.iTabl
15cac 65 20 3d 20 31 3b 0a 23 65 6e 64 69 66 0a 0a 23  e = 1;.#endif..#
15cad 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
15cae 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
15caf 41 43 48 45 29 20 26 26 20 21 64 65 66 69 6e 65  ACHE) && !define
15cb0 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49  d(SQLITE_OMIT_DI
15cb1 53 4b 49 4f 29 0a 20 20 2f 2a 0a 20 20 2a 2a 20  SKIO).  /*.  ** 
15cb2 49 66 20 74 68 69 73 20 42 74 72 65 65 20 69 73  If this Btree is
15cb3 20 61 20 63 61 6e 64 69 64 61 74 65 20 66 6f 72   a candidate for
15cb4 20 73 68 61 72 65 64 20 63 61 63 68 65 2c 20 74   shared cache, t
15cb5 72 79 20 74 6f 20 66 69 6e 64 20 61 6e 0a 20 20  ry to find an.  
15cb6 2a 2a 20 65 78 69 73 74 69 6e 67 20 42 74 53 68  ** existing BtSh
15cb7 61 72 65 64 20 6f 62 6a 65 63 74 20 74 68 61 74  ared object that
15cb8 20 77 65 20 63 61 6e 20 73 68 61 72 65 20 77 69   we can share wi
15cb9 74 68 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73  th.  */.  if( is
15cba 4d 65 6d 64 62 3d 3d 30 20 26 26 20 7a 46 69 6c  Memdb==0 && zFil
15cbb 65 6e 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61  ename && zFilena
15cbc 6d 65 5b 30 5d 20 29 7b 0a 20 20 20 20 69 66 28  me[0] ){.    if(
15cbd 20 76 66 73 46 6c 61 67 73 20 26 20 53 51 4c 49   vfsFlags & SQLI
15cbe 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41  TE_OPEN_SHAREDCA
15cbf 43 48 45 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  CHE ){.      int
15cc0 20 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 3d   nFullPathname =
15cc1 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d   pVfs->mxPathnam
15cc2 65 2b 31 3b 0a 20 20 20 20 20 20 63 68 61 72 20  e+1;.      char 
15cc3 2a 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 3d  *zFullPathname =
15cc4 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e   sqlite3Malloc(n
15cc5 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20  FullPathname);. 
15cc6 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74       sqlite3_mut
15cc7 65 78 20 2a 6d 75 74 65 78 53 68 61 72 65 64 3b  ex *mutexShared;
15cc8 0a 20 20 20 20 20 20 70 2d 3e 73 68 61 72 61 62  .      p->sharab
15cc9 6c 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 69 66  le = 1;.      if
15cca 28 20 21 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65  ( !zFullPathname
15ccb 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
15ccc 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 20  te3_free(p);.   
15ccd 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
15cce 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
15ccf 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f  }.      sqlite3O
15cd0 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 70 56  sFullPathname(pV
15cd1 66 73 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e  fs, zFilename, n
15cd2 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 7a 46  FullPathname, zF
15cd3 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20  ullPathname);.  
15cd4 20 20 20 20 6d 75 74 65 78 4f 70 65 6e 20 3d 20      mutexOpen = 
15cd5 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
15cd6 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
15cd7 54 41 54 49 43 5f 4f 50 45 4e 29 3b 0a 20 20 20  TATIC_OPEN);.   
15cd8 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
15cd9 5f 65 6e 74 65 72 28 6d 75 74 65 78 4f 70 65 6e  _enter(mutexOpen
15cda 29 3b 0a 20 20 20 20 20 20 6d 75 74 65 78 53 68  );.      mutexSh
15cdb 61 72 65 64 20 3d 20 73 71 6c 69 74 65 33 4d 75  ared = sqlite3Mu
15cdc 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  texAlloc(SQLITE_
15cdd 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53  MUTEX_STATIC_MAS
15cde 54 45 52 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  TER);.      sqli
15cdf 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
15ce0 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20  mutexShared);.  
15ce1 20 20 20 20 66 6f 72 28 70 42 74 3d 47 4c 4f 42      for(pBt=GLOB
15ce2 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c  AL(BtShared*,sql
15ce3 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c  ite3SharedCacheL
15ce4 69 73 74 29 3b 20 70 42 74 3b 20 70 42 74 3d 70  ist); pBt; pBt=p
15ce5 42 74 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  Bt->pNext){.    
15ce6 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d      assert( pBt-
15ce7 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 20 20 20  >nRef>0 );.     
15ce8 20 20 20 69 66 28 20 30 3d 3d 73 74 72 63 6d 70     if( 0==strcmp
15ce9 28 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20  (zFullPathname, 
15cea 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65  sqlite3PagerFile
15ceb 6e 61 6d 65 28 70 42 74 2d 3e 70 50 61 67 65 72  name(pBt->pPager
15cec 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  )).             
15ced 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 50 61      && sqlite3Pa
15cee 67 65 72 56 66 73 28 70 42 74 2d 3e 70 50 61 67  gerVfs(pBt->pPag
15cef 65 72 29 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20  er)==pVfs ){.   
15cf0 20 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a         int iDb;.
15cf1 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 44            for(iD
15cf2 62 3d 64 62 2d 3e 6e 44 62 2d 31 3b 20 69 44 62  b=db->nDb-1; iDb
15cf3 3e 3d 30 3b 20 69 44 62 2d 2d 29 7b 0a 20 20 20  >=0; iDb--){.   
15cf4 20 20 20 20 20 20 20 20 20 42 74 72 65 65 20 2a           Btree *
15cf5 70 45 78 69 73 74 69 6e 67 20 3d 20 64 62 2d 3e  pExisting = db->
15cf6 61 44 62 5b 69 44 62 5d 2e 70 42 74 3b 0a 20 20  aDb[iDb].pBt;.  
15cf7 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 45            if( pE
15cf8 78 69 73 74 69 6e 67 20 26 26 20 70 45 78 69 73  xisting && pExis
15cf9 74 69 6e 67 2d 3e 70 42 74 3d 3d 70 42 74 20 29  ting->pBt==pBt )
15cfa 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
15cfb 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
15cfc 61 76 65 28 6d 75 74 65 78 53 68 61 72 65 64 29  ave(mutexShared)
15cfd 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
15cfe 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
15cff 61 76 65 28 6d 75 74 65 78 4f 70 65 6e 29 3b 0a  ave(mutexOpen);.
15d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
15d01 6c 69 74 65 33 5f 66 72 65 65 28 7a 46 75 6c 6c  lite3_free(zFull
15d02 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20  Pathname);.     
15d03 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
15d04 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20  _free(p);.      
15d05 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
15d06 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
15d07 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
15d08 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
15d09 20 20 20 20 20 20 70 2d 3e 70 42 74 20 3d 20 70        p->pBt = p
15d0a 42 74 3b 0a 20 20 20 20 20 20 20 20 20 20 70 42  Bt;.          pB
15d0b 74 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 20  t->nRef++;.     
15d0c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
15d0d 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
15d0e 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
15d0f 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 53 68 61  x_leave(mutexSha
15d10 72 65 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  red);.      sqli
15d11 74 65 33 5f 66 72 65 65 28 7a 46 75 6c 6c 50 61  te3_free(zFullPa
15d12 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 23  thname);.    }.#
15d13 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
15d14 55 47 0a 20 20 20 20 65 6c 73 65 7b 0a 20 20 20  UG.    else{.   
15d15 20 20 20 2f 2a 20 49 6e 20 64 65 62 75 67 20 6d     /* In debug m
15d16 6f 64 65 2c 20 77 65 20 6d 61 72 6b 20 61 6c 6c  ode, we mark all
15d17 20 70 65 72 73 69 73 74 65 6e 74 20 64 61 74 61   persistent data
15d18 62 61 73 65 73 20 61 73 20 73 68 61 72 61 62 6c  bases as sharabl
15d19 65 0a 20 20 20 20 20 20 2a 2a 20 65 76 65 6e 20  e.      ** even 
15d1a 77 68 65 6e 20 74 68 65 79 20 61 72 65 20 6e 6f  when they are no
15d1b 74 2e 20 20 54 68 69 73 20 65 78 65 72 63 69 73  t.  This exercis
15d1c 65 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63  es the locking c
15d1d 6f 64 65 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a  ode and.      **
15d1e 20 67 69 76 65 73 20 6d 6f 72 65 20 6f 70 70 6f   gives more oppo
15d1f 72 74 75 6e 69 74 79 20 66 6f 72 20 61 73 73 65  rtunity for asse
15d20 72 74 73 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  rts(sqlite3_mute
15d21 78 5f 68 65 6c 64 28 29 29 0a 20 20 20 20 20 20  x_held()).      
15d22 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f  ** statements to
15d23 20 66 69 6e 64 20 6c 6f 63 6b 69 6e 67 20 70 72   find locking pr
15d24 6f 62 6c 65 6d 73 2e 0a 20 20 20 20 20 20 2a 2f  oblems..      */
15d25 0a 20 20 20 20 20 20 70 2d 3e 73 68 61 72 61 62  .      p->sharab
15d26 6c 65 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 23 65  le = 1;.    }.#e
15d27 6e 64 69 66 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  ndif.  }.#endif.
15d28 20 20 69 66 28 20 70 42 74 3d 3d 30 20 29 7b 0a    if( pBt==0 ){.
15d29 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68      /*.    ** Th
15d2a 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65  e following asse
15d2b 72 74 73 20 6d 61 6b 65 20 73 75 72 65 20 74 68  rts make sure th
15d2c 61 74 20 73 74 72 75 63 74 75 72 65 73 20 75 73  at structures us
15d2d 65 64 20 62 79 20 74 68 65 20 62 74 72 65 65 20  ed by the btree 
15d2e 61 72 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 72  are.    ** the r
15d2f 69 67 68 74 20 73 69 7a 65 2e 20 20 54 68 69 73  ight size.  This
15d30 20 69 73 20 74 6f 20 67 75 61 72 64 20 61 67 61   is to guard aga
15d31 69 6e 73 74 20 73 69 7a 65 20 63 68 61 6e 67 65  inst size change
15d32 73 20 74 68 61 74 20 72 65 73 75 6c 74 0a 20 20  s that result.  
15d33 20 20 2a 2a 20 77 68 65 6e 20 63 6f 6d 70 69 6c    ** when compil
15d34 69 6e 67 20 6f 6e 20 61 20 64 69 66 66 65 72 65  ing on a differe
15d35 6e 74 20 61 72 63 68 69 74 65 63 74 75 72 65 2e  nt architecture.
15d36 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65  .    */.    asse
15d37 72 74 28 20 73 69 7a 65 6f 66 28 69 36 34 29 3d  rt( sizeof(i64)=
15d38 3d 38 20 7c 7c 20 73 69 7a 65 6f 66 28 69 36 34  =8 || sizeof(i64
15d39 29 3d 3d 34 20 29 3b 0a 20 20 20 20 61 73 73 65  )==4 );.    asse
15d3a 72 74 28 20 73 69 7a 65 6f 66 28 75 36 34 29 3d  rt( sizeof(u64)=
15d3b 3d 38 20 7c 7c 20 73 69 7a 65 6f 66 28 75 36 34  =8 || sizeof(u64
15d3c 29 3d 3d 34 20 29 3b 0a 20 20 20 20 61 73 73 65  )==4 );.    asse
15d3d 72 74 28 20 73 69 7a 65 6f 66 28 75 33 32 29 3d  rt( sizeof(u32)=
15d3e 3d 34 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =4 );.    assert
15d3f 28 20 73 69 7a 65 6f 66 28 75 31 36 29 3d 3d 32  ( sizeof(u16)==2
15d40 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
15d41 73 69 7a 65 6f 66 28 50 67 6e 6f 29 3d 3d 34 20  sizeof(Pgno)==4 
15d42 29 3b 0a 20 20 0a 20 20 20 20 70 42 74 20 3d 20  );.  .    pBt = 
15d43 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72  sqlite3MallocZer
15d44 6f 28 20 73 69 7a 65 6f 66 28 2a 70 42 74 29 20  o( sizeof(*pBt) 
15d45 29 3b 0a 20 20 20 20 69 66 28 20 70 42 74 3d 3d  );.    if( pBt==
15d46 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  0 ){.      rc = 
15d47 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
15d48 20 20 20 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f      goto btree_o
15d49 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20  pen_out;.    }. 
15d4a 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
15d4b 61 67 65 72 4f 70 65 6e 28 70 56 66 73 2c 20 26  agerOpen(pVfs, &
15d4c 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 7a 46 69  pBt->pPager, zFi
15d4d 6c 65 6e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20  lename,.        
15d4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d4f 20 20 45 58 54 52 41 5f 53 49 5a 45 2c 20 66 6c    EXTRA_SIZE, fl
15d50 61 67 73 2c 20 76 66 73 46 6c 61 67 73 2c 20 70  ags, vfsFlags, p
15d51 61 67 65 52 65 69 6e 69 74 29 3b 0a 20 20 20 20  ageReinit);.    
15d52 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
15d53 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  K ){.      rc = 
15d54 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 61 64  sqlite3PagerRead
15d55 46 69 6c 65 68 65 61 64 65 72 28 70 42 74 2d 3e  Fileheader(pBt->
15d56 70 50 61 67 65 72 2c 73 69 7a 65 6f 66 28 7a 44  pPager,sizeof(zD
15d57 62 48 65 61 64 65 72 29 2c 7a 44 62 48 65 61 64  bHeader),zDbHead
15d58 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  er);.    }.    i
15d59 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
15d5a 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62   ){.      goto b
15d5b 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20  tree_open_out;. 
15d5c 20 20 20 7d 0a 20 20 20 20 70 42 74 2d 3e 64 62     }.    pBt->db
15d5d 20 3d 20 64 62 3b 0a 20 20 20 20 73 71 6c 69 74   = db;.    sqlit
15d5e 65 33 50 61 67 65 72 53 65 74 42 75 73 79 68 61  e3PagerSetBusyha
15d5f 6e 64 6c 65 72 28 70 42 74 2d 3e 70 50 61 67 65  ndler(pBt->pPage
15d60 72 2c 20 62 74 72 65 65 49 6e 76 6f 6b 65 42 75  r, btreeInvokeBu
15d61 73 79 48 61 6e 64 6c 65 72 2c 20 70 42 74 29 3b  syHandler, pBt);
15d62 0a 20 20 20 20 70 2d 3e 70 42 74 20 3d 20 70 42  .    p->pBt = pB
15d63 74 3b 0a 20 20 0a 20 20 20 20 70 42 74 2d 3e 70  t;.  .    pBt->p
15d64 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 20 20  Cursor = 0;.    
15d65 70 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b  pBt->pPage1 = 0;
15d66 0a 20 20 20 20 70 42 74 2d 3e 72 65 61 64 4f 6e  .    pBt->readOn
15d67 6c 79 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  ly = sqlite3Page
15d68 72 49 73 72 65 61 64 6f 6e 6c 79 28 70 42 74 2d  rIsreadonly(pBt-
15d69 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 70 42  >pPager);.    pB
15d6a 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 67 65  t->pageSize = ge
15d6b 74 32 62 79 74 65 28 26 7a 44 62 48 65 61 64 65  t2byte(&zDbHeade
15d6c 72 5b 31 36 5d 29 3b 0a 20 20 20 20 69 66 28 20  r[16]);.    if( 
15d6d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3c 35 31  pBt->pageSize<51
15d6e 32 20 7c 7c 20 70 42 74 2d 3e 70 61 67 65 53 69  2 || pBt->pageSi
15d6f 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41  ze>SQLITE_MAX_PA
15d70 47 45 5f 53 49 5a 45 0a 20 20 20 20 20 20 20 20  GE_SIZE.        
15d71 20 7c 7c 20 28 28 70 42 74 2d 3e 70 61 67 65 53   || ((pBt->pageS
15d72 69 7a 65 2d 31 29 26 70 42 74 2d 3e 70 61 67 65  ize-1)&pBt->page
15d73 53 69 7a 65 29 21 3d 30 20 29 7b 0a 20 20 20 20  Size)!=0 ){.    
15d74 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20    pBt->pageSize 
15d75 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  = 0;.#ifndef SQL
15d76 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
15d77 55 55 4d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20  UUM.      /* If 
15d78 74 68 65 20 6d 61 67 69 63 20 6e 61 6d 65 20 22  the magic name "
15d79 3a 6d 65 6d 6f 72 79 3a 22 20 77 69 6c 6c 20 63  :memory:" will c
15d7a 72 65 61 74 65 20 61 6e 20 69 6e 2d 6d 65 6d 6f  reate an in-memo
15d7b 72 79 20 64 61 74 61 62 61 73 65 2c 20 74 68 65  ry database, the
15d7c 6e 0a 20 20 20 20 20 20 2a 2a 20 6c 65 61 76 65  n.      ** leave
15d7d 20 74 68 65 20 61 75 74 6f 56 61 63 75 75 6d 20   the autoVacuum 
15d7e 6d 6f 64 65 20 61 74 20 30 20 28 64 6f 20 6e 6f  mode at 0 (do no
15d7f 74 20 61 75 74 6f 2d 76 61 63 75 75 6d 29 2c 20  t auto-vacuum), 
15d80 65 76 65 6e 20 69 66 0a 20 20 20 20 20 20 2a 2a  even if.      **
15d81 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
15d82 41 55 54 4f 56 41 43 55 55 4d 20 69 73 20 74 72  AUTOVACUUM is tr
15d83 75 65 2e 20 4f 6e 20 74 68 65 20 6f 74 68 65 72  ue. On the other
15d84 20 68 61 6e 64 2c 20 69 66 0a 20 20 20 20 20 20   hand, if.      
15d85 2a 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d  ** SQLITE_OMIT_M
15d86 45 4d 4f 52 59 44 42 20 68 61 73 20 62 65 65 6e  EMORYDB has been
15d87 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 22   defined, then "
15d88 3a 6d 65 6d 6f 72 79 3a 22 20 69 73 20 6a 75 73  :memory:" is jus
15d89 74 20 61 0a 20 20 20 20 20 20 2a 2a 20 72 65 67  t a.      ** reg
15d8a 75 6c 61 72 20 66 69 6c 65 2d 6e 61 6d 65 2e 20  ular file-name. 
15d8b 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65  In this case the
15d8c 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 61 70 70   auto-vacuum app
15d8d 6c 69 65 73 20 61 73 20 70 65 72 20 6e 6f 72 6d  lies as per norm
15d8e 61 6c 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  al..      */.   
15d8f 20 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65     if( zFilename
15d90 20 26 26 20 21 69 73 4d 65 6d 64 62 20 29 7b 0a   && !isMemdb ){.
15d91 20 20 20 20 20 20 20 20 70 42 74 2d 3e 61 75 74          pBt->aut
15d92 6f 56 61 63 75 75 6d 20 3d 20 28 53 51 4c 49 54  oVacuum = (SQLIT
15d93 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41  E_DEFAULT_AUTOVA
15d94 43 55 55 4d 20 3f 20 31 20 3a 20 30 29 3b 0a 20  CUUM ? 1 : 0);. 
15d95 20 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 63 72         pBt->incr
15d96 56 61 63 75 75 6d 20 3d 20 28 53 51 4c 49 54 45  Vacuum = (SQLITE
15d97 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43  _DEFAULT_AUTOVAC
15d98 55 55 4d 3d 3d 32 20 3f 20 31 20 3a 20 30 29 3b  UUM==2 ? 1 : 0);
15d99 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  .      }.#endif.
15d9a 20 20 20 20 20 20 6e 52 65 73 65 72 76 65 20 3d        nReserve =
15d9b 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   0;.    }else{. 
15d9c 20 20 20 20 20 6e 52 65 73 65 72 76 65 20 3d 20       nReserve = 
15d9d 7a 44 62 48 65 61 64 65 72 5b 32 30 5d 3b 0a 20  zDbHeader[20];. 
15d9e 20 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69       pBt->pageSi
15d9f 7a 65 46 69 78 65 64 20 3d 20 31 3b 0a 23 69 66  zeFixed = 1;.#if
15da0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
15da1 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20  _AUTOVACUUM.    
15da2 20 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75    pBt->autoVacuu
15da3 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28 26 7a  m = (get4byte(&z
15da4 44 62 48 65 61 64 65 72 5b 33 36 20 2b 20 34 2a  DbHeader[36 + 4*
15da5 34 5d 29 3f 31 3a 30 29 3b 0a 20 20 20 20 20 20  4])?1:0);.      
15da6 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20  pBt->incrVacuum 
15da7 3d 20 28 67 65 74 34 62 79 74 65 28 26 7a 44 62  = (get4byte(&zDb
15da8 48 65 61 64 65 72 5b 33 36 20 2b 20 37 2a 34 5d  Header[36 + 7*4]
15da9 29 3f 31 3a 30 29 3b 0a 23 65 6e 64 69 66 0a 20  )?1:0);.#endif. 
15daa 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71     }.    rc = sq
15dab 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61 67  lite3PagerSetPag
15dac 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65  esize(pBt->pPage
15dad 72 2c 20 26 70 42 74 2d 3e 70 61 67 65 53 69 7a  r, &pBt->pageSiz
15dae 65 2c 20 6e 52 65 73 65 72 76 65 29 3b 0a 20 20  e, nReserve);.  
15daf 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20    if( rc ) goto 
15db0 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a  btree_open_out;.
15db1 20 20 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 53      pBt->usableS
15db2 69 7a 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53  ize = pBt->pageS
15db3 69 7a 65 20 2d 20 6e 52 65 73 65 72 76 65 3b 0a  ize - nReserve;.
15db4 20 20 20 20 61 73 73 65 72 74 28 20 28 70 42 74      assert( (pBt
15db5 2d 3e 70 61 67 65 53 69 7a 65 20 26 20 37 29 3d  ->pageSize & 7)=
15db6 3d 30 20 29 3b 20 20 2f 2a 20 38 2d 62 79 74 65  =0 );  /* 8-byte
15db7 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 70 61   alignment of pa
15db8 67 65 53 69 7a 65 20 2a 2f 0a 20 20 20 0a 23 69  geSize */.   .#i
15db9 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
15dba 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
15dbb 43 48 45 29 20 26 26 20 21 64 65 66 69 6e 65 64  CHE) && !defined
15dbc 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53  (SQLITE_OMIT_DIS
15dbd 4b 49 4f 29 0a 20 20 20 20 2f 2a 20 41 64 64 20  KIO).    /* Add 
15dbe 74 68 65 20 6e 65 77 20 42 74 53 68 61 72 65 64  the new BtShared
15dbf 20 6f 62 6a 65 63 74 20 74 6f 20 74 68 65 20 6c   object to the l
15dc0 69 6e 6b 65 64 20 6c 69 73 74 20 73 68 61 72 61  inked list shara
15dc1 62 6c 65 20 42 74 53 68 61 72 65 64 73 2e 0a 20  ble BtShareds.. 
15dc2 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d     */.    if( p-
15dc3 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20  >sharable ){.   
15dc4 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
15dc5 20 2a 6d 75 74 65 78 53 68 61 72 65 64 3b 0a 20   *mutexShared;. 
15dc6 20 20 20 20 20 70 42 74 2d 3e 6e 52 65 66 20 3d       pBt->nRef =
15dc7 20 31 3b 0a 20 20 20 20 20 20 6d 75 74 65 78 53   1;.      mutexS
15dc8 68 61 72 65 64 20 3d 20 73 71 6c 69 74 65 33 4d  hared = sqlite3M
15dc9 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
15dca 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41  _MUTEX_STATIC_MA
15dcb 53 54 45 52 29 3b 0a 20 20 20 20 20 20 69 66 28  STER);.      if(
15dcc 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
15dcd 46 45 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f  FE && sqlite3Glo
15dce 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d  balConfig.bCoreM
15dcf 75 74 65 78 20 29 7b 0a 20 20 20 20 20 20 20 20  utex ){.        
15dd0 70 42 74 2d 3e 6d 75 74 65 78 20 3d 20 73 71 6c  pBt->mutex = sql
15dd1 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53  ite3MutexAlloc(S
15dd2 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54  QLITE_MUTEX_FAST
15dd3 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
15dd4 42 74 2d 3e 6d 75 74 65 78 3d 3d 30 20 29 7b 0a  Bt->mutex==0 ){.
15dd5 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
15dd6 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
15dd7 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f         db->mallo
15dd8 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20  cFailed = 0;.   
15dd9 20 20 20 20 20 20 20 67 6f 74 6f 20 62 74 72 65         goto btre
15dda 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20  e_open_out;.    
15ddb 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
15ddc 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
15ddd 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 53 68 61  x_enter(mutexSha
15dde 72 65 64 29 3b 0a 20 20 20 20 20 20 70 42 74 2d  red);.      pBt-
15ddf 3e 70 4e 65 78 74 20 3d 20 47 4c 4f 42 41 4c 28  >pNext = GLOBAL(
15de0 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65  BtShared*,sqlite
15de1 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74  3SharedCacheList
15de2 29 3b 0a 20 20 20 20 20 20 47 4c 4f 42 41 4c 28  );.      GLOBAL(
15de3 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65  BtShared*,sqlite
15de4 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74  3SharedCacheList
15de5 29 20 3d 20 70 42 74 3b 0a 20 20 20 20 20 20 73  ) = pBt;.      s
15de6 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
15de7 76 65 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b  ve(mutexShared);
15de8 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
15de9 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  }..#if !defined(
15dea 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
15deb 45 44 5f 43 41 43 48 45 29 20 26 26 20 21 64 65  ED_CACHE) && !de
15dec 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
15ded 54 5f 44 49 53 4b 49 4f 29 0a 20 20 2f 2a 20 49  T_DISKIO).  /* I
15dee 66 20 74 68 65 20 6e 65 77 20 42 74 72 65 65 20  f the new Btree 
15def 75 73 65 73 20 61 20 73 68 61 72 61 62 6c 65 20  uses a sharable 
15df0 70 42 74 53 68 61 72 65 64 2c 20 74 68 65 6e 20  pBtShared, then 
15df1 6c 69 6e 6b 20 74 68 65 20 6e 65 77 0a 20 20 2a  link the new.  *
15df2 2a 20 42 74 72 65 65 20 69 6e 74 6f 20 74 68 65  * Btree into the
15df3 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 73 68 61   list of all sha
15df4 72 61 62 6c 65 20 42 74 72 65 65 73 20 66 6f 72  rable Btrees for
15df5 20 74 68 65 20 73 61 6d 65 20 63 6f 6e 6e 65 63   the same connec
15df6 74 69 6f 6e 2e 0a 20 20 2a 2a 20 54 68 65 20 6c  tion..  ** The l
15df7 69 73 74 20 69 73 20 6b 65 70 74 20 69 6e 20 61  ist is kept in a
15df8 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62  scending order b
15df9 79 20 70 42 74 20 61 64 64 72 65 73 73 2e 0a 20  y pBt address.. 
15dfa 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 73 68 61   */.  if( p->sha
15dfb 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 69 6e 74  rable ){.    int
15dfc 20 69 3b 0a 20 20 20 20 42 74 72 65 65 20 2a 70   i;.    Btree *p
15dfd 53 69 62 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  Sib;.    for(i=0
15dfe 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
15dff 29 7b 0a 20 20 20 20 20 20 69 66 28 20 28 70 53  ){.      if( (pS
15e00 69 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e  ib = db->aDb[i].
15e01 70 42 74 29 21 3d 30 20 26 26 20 70 53 69 62 2d  pBt)!=0 && pSib-
15e02 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20  >sharable ){.   
15e03 20 20 20 20 20 77 68 69 6c 65 28 20 70 53 69 62       while( pSib
15e04 2d 3e 70 50 72 65 76 20 29 7b 20 70 53 69 62 20  ->pPrev ){ pSib 
15e05 3d 20 70 53 69 62 2d 3e 70 50 72 65 76 3b 20 7d  = pSib->pPrev; }
15e06 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e  .        if( p->
15e07 70 42 74 3c 70 53 69 62 2d 3e 70 42 74 20 29 7b  pBt<pSib->pBt ){
15e08 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 4e  .          p->pN
15e09 65 78 74 20 3d 20 70 53 69 62 3b 0a 20 20 20 20  ext = pSib;.    
15e0a 20 20 20 20 20 20 70 2d 3e 70 50 72 65 76 20 3d        p->pPrev =
15e0b 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 70 53   0;.          pS
15e0c 69 62 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20  ib->pPrev = p;. 
15e0d 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
15e0e 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70          while( p
15e0f 53 69 62 2d 3e 70 4e 65 78 74 20 26 26 20 70 53  Sib->pNext && pS
15e10 69 62 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 3c 70  ib->pNext->pBt<p
15e11 2d 3e 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20  ->pBt ){.       
15e12 20 20 20 20 20 70 53 69 62 20 3d 20 70 53 69 62       pSib = pSib
15e13 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20  ->pNext;.       
15e14 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70     }.          p
15e15 2d 3e 70 4e 65 78 74 20 3d 20 70 53 69 62 2d 3e  ->pNext = pSib->
15e16 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 20  pNext;.         
15e17 20 70 2d 3e 70 50 72 65 76 20 3d 20 70 53 69 62   p->pPrev = pSib
15e18 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
15e19 70 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20  p->pNext ){.    
15e1a 20 20 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74          p->pNext
15e1b 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20 20 20  ->pPrev = p;.   
15e1c 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
15e1d 20 20 20 70 53 69 62 2d 3e 70 4e 65 78 74 20 3d     pSib->pNext =
15e1e 20 70 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   p;.        }.  
15e1f 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
15e20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23     }.    }.  }.#
15e21 65 6e 64 69 66 0a 20 20 2a 70 70 42 74 72 65 65  endif.  *ppBtree
15e22 20 3d 20 70 3b 0a 0a 62 74 72 65 65 5f 6f 70 65   = p;..btree_ope
15e23 6e 5f 6f 75 74 3a 0a 20 20 69 66 28 20 72 63 21  n_out:.  if( rc!
15e24 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
15e25 20 20 69 66 28 20 70 42 74 20 26 26 20 70 42 74    if( pBt && pBt
15e26 2d 3e 70 50 61 67 65 72 20 29 7b 0a 20 20 20 20  ->pPager ){.    
15e27 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c    sqlite3PagerCl
15e28 6f 73 65 28 70 42 74 2d 3e 70 50 61 67 65 72 29  ose(pBt->pPager)
15e29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
15e2a 74 65 33 5f 66 72 65 65 28 70 42 74 29 3b 0a 20  te3_free(pBt);. 
15e2b 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
15e2c 70 29 3b 0a 20 20 20 20 2a 70 70 42 74 72 65 65  p);.    *ppBtree
15e2d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20   = 0;.  }.  if( 
15e2e 6d 75 74 65 78 4f 70 65 6e 20 29 7b 0a 20 20 20  mutexOpen ){.   
15e2f 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
15e30 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 75 74 65  _mutex_held(mute
15e31 78 4f 70 65 6e 29 20 29 3b 0a 20 20 20 20 73 71  xOpen) );.    sq
15e32 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
15e33 65 28 6d 75 74 65 78 4f 70 65 6e 29 3b 0a 20 20  e(mutexOpen);.  
15e34 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
15e35 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e  ../*.** Decremen
15e36 74 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 6e  t the BtShared.n
15e37 52 65 66 20 63 6f 75 6e 74 65 72 2e 20 20 57 68  Ref counter.  Wh
15e38 65 6e 20 69 74 20 72 65 61 63 68 65 73 20 7a 65  en it reaches ze
15e39 72 6f 2c 0a 2a 2a 20 72 65 6d 6f 76 65 20 74 68  ro,.** remove th
15e3a 65 20 42 74 53 68 61 72 65 64 20 73 74 72 75 63  e BtShared struc
15e3b 74 75 72 65 20 66 72 6f 6d 20 74 68 65 20 73 68  ture from the sh
15e3c 61 72 69 6e 67 20 6c 69 73 74 2e 20 20 52 65 74  aring list.  Ret
15e3d 75 72 6e 0a 2a 2a 20 74 72 75 65 20 69 66 20 74  urn.** true if t
15e3e 68 65 20 42 74 53 68 61 72 65 64 2e 6e 52 65 66  he BtShared.nRef
15e3f 20 63 6f 75 6e 74 65 72 20 72 65 61 63 68 65 73   counter reaches
15e40 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 75 72 6e   zero and return
15e41 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20 69 74 20  .** false if it 
15e42 69 73 20 73 74 69 6c 6c 20 70 6f 73 69 74 69 76  is still positiv
15e43 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
15e44 20 72 65 6d 6f 76 65 46 72 6f 6d 53 68 61 72 69   removeFromShari
15e45 6e 67 4c 69 73 74 28 42 74 53 68 61 72 65 64 20  ngList(BtShared 
15e46 2a 70 42 74 29 7b 0a 23 69 66 6e 64 65 66 20 53  *pBt){.#ifndef S
15e47 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
15e48 44 5f 43 41 43 48 45 0a 20 20 73 71 6c 69 74 65  D_CACHE.  sqlite
15e49 33 5f 6d 75 74 65 78 20 2a 70 4d 61 73 74 65 72  3_mutex *pMaster
15e4a 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 4c  ;.  BtShared *pL
15e4b 69 73 74 3b 0a 20 20 69 6e 74 20 72 65 6d 6f 76  ist;.  int remov
15e4c 65 64 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72  ed = 0;..  asser
15e4d 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
15e4e 5f 6e 6f 74 68 65 6c 64 28 70 42 74 2d 3e 6d 75  _notheld(pBt->mu
15e4f 74 65 78 29 20 29 3b 0a 20 20 70 4d 61 73 74 65  tex) );.  pMaste
15e50 72 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78  r = sqlite3Mutex
15e51 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
15e52 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
15e53 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
15e54 65 78 5f 65 6e 74 65 72 28 70 4d 61 73 74 65 72  ex_enter(pMaster
15e55 29 3b 0a 20 20 70 42 74 2d 3e 6e 52 65 66 2d 2d  );.  pBt->nRef--
15e56 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 6e 52 65  ;.  if( pBt->nRe
15e57 66 3c 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  f<=0 ){.    if( 
15e58 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a  GLOBAL(BtShared*
15e59 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61  ,sqlite3SharedCa
15e5a 63 68 65 4c 69 73 74 29 3d 3d 70 42 74 20 29 7b  cheList)==pBt ){
15e5b 0a 20 20 20 20 20 20 47 4c 4f 42 41 4c 28 42 74  .      GLOBAL(Bt
15e5c 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53  Shared*,sqlite3S
15e5d 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 20  haredCacheList) 
15e5e 3d 20 70 42 74 2d 3e 70 4e 65 78 74 3b 0a 20 20  = pBt->pNext;.  
15e5f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
15e60 4c 69 73 74 20 3d 20 47 4c 4f 42 41 4c 28 42 74  List = GLOBAL(Bt
15e61 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53  Shared*,sqlite3S
15e62 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 3b  haredCacheList);
15e63 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 41 4c  .      while( AL
15e64 57 41 59 53 28 70 4c 69 73 74 29 20 26 26 20 70  WAYS(pList) && p
15e65 4c 69 73 74 2d 3e 70 4e 65 78 74 21 3d 70 42 74  List->pNext!=pBt
15e66 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4c 69 73   ){.        pLis
15e67 74 3d 70 4c 69 73 74 2d 3e 70 4e 65 78 74 3b 0a  t=pList->pNext;.
15e68 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
15e69 28 20 41 4c 57 41 59 53 28 70 4c 69 73 74 29 20  ( ALWAYS(pList) 
15e6a 29 7b 0a 20 20 20 20 20 20 20 20 70 4c 69 73 74  ){.        pList
15e6b 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70  ->pNext = pBt->p
15e6c 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Next;.      }.  
15e6d 20 20 7d 0a 20 20 20 20 69 66 28 20 53 51 4c 49    }.    if( SQLI
15e6e 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 29 7b  TE_THREADSAFE ){
15e6f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  .      sqlite3_m
15e70 75 74 65 78 5f 66 72 65 65 28 70 42 74 2d 3e 6d  utex_free(pBt->m
15e71 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20  utex);.    }.   
15e72 20 72 65 6d 6f 76 65 64 20 3d 20 31 3b 0a 20 20   removed = 1;.  
15e73 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
15e74 78 5f 6c 65 61 76 65 28 70 4d 61 73 74 65 72 29  x_leave(pMaster)
15e75 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 6d 6f 76  ;.  return remov
15e76 65 64 3b 0a 23 65 6c 73 65 0a 20 20 72 65 74 75  ed;.#else.  retu
15e77 72 6e 20 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a  rn 1;.#endif.}..
15e78 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20  /*.** Make sure 
15e79 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 70  pBt->pTmpSpace p
15e7a 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61 6c 6c 6f  oints to an allo
15e7b 63 61 74 69 6f 6e 20 6f 66 20 0a 2a 2a 20 4d 58  cation of .** MX
15e7c 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20  _CELL_SIZE(pBt) 
15e7d 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  bytes..*/.static
15e7e 20 76 6f 69 64 20 61 6c 6c 6f 63 61 74 65 54 65   void allocateTe
15e7f 6d 70 53 70 61 63 65 28 42 74 53 68 61 72 65 64  mpSpace(BtShared
15e80 20 2a 70 42 74 29 7b 0a 20 20 69 66 28 20 21 70   *pBt){.  if( !p
15e81 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 29 7b  Bt->pTmpSpace ){
15e82 0a 20 20 20 20 70 42 74 2d 3e 70 54 6d 70 53 70  .    pBt->pTmpSp
15e83 61 63 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67  ace = sqlite3Pag
15e84 65 4d 61 6c 6c 6f 63 28 20 70 42 74 2d 3e 70 61  eMalloc( pBt->pa
15e85 67 65 53 69 7a 65 20 29 3b 0a 20 20 7d 0a 7d 0a  geSize );.  }.}.
15e86 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 74 68 65 20  ./*.** Free the 
15e87 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 61  pBt->pTmpSpace a
15e88 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61  llocation.*/.sta
15e89 74 69 63 20 76 6f 69 64 20 66 72 65 65 54 65 6d  tic void freeTem
15e8a 70 53 70 61 63 65 28 42 74 53 68 61 72 65 64 20  pSpace(BtShared 
15e8b 2a 70 42 74 29 7b 0a 20 20 73 71 6c 69 74 65 33  *pBt){.  sqlite3
15e8c 50 61 67 65 46 72 65 65 28 20 70 42 74 2d 3e 70  PageFree( pBt->p
15e8d 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 70 42 74  TmpSpace);.  pBt
15e8e 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d 20 30 3b  ->pTmpSpace = 0;
15e8f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20  .}../*.** Close 
15e90 61 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65  an open database
15e91 20 61 6e 64 20 69 6e 76 61 6c 69 64 61 74 65 20   and invalidate 
15e92 61 6c 6c 20 63 75 72 73 6f 72 73 2e 0a 2a 2f 0a  all cursors..*/.
15e93 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
15e94 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
15e95 6c 6f 73 65 28 42 74 72 65 65 20 2a 70 29 7b 0a  lose(Btree *p){.
15e96 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
15e97 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74 43 75  = p->pBt;.  BtCu
15e98 72 73 6f 72 20 2a 70 43 75 72 3b 0a 0a 20 20 2f  rsor *pCur;..  /
15e99 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 63 75 72 73  * Close all curs
15e9a 6f 72 73 20 6f 70 65 6e 65 64 20 76 69 61 20 74  ors opened via t
15e9b 68 69 73 20 68 61 6e 64 6c 65 2e 20 20 2a 2f 0a  his handle.  */.
15e9c 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
15e9d 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e  3_mutex_held(p->
15e9e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
15e9f 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
15ea0 72 28 70 29 3b 0a 20 20 70 43 75 72 20 3d 20 70  r(p);.  pCur = p
15ea1 42 74 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 77  Bt->pCursor;.  w
15ea2 68 69 6c 65 28 20 70 43 75 72 20 29 7b 0a 20 20  hile( pCur ){.  
15ea3 20 20 42 74 43 75 72 73 6f 72 20 2a 70 54 6d 70    BtCursor *pTmp
15ea4 20 3d 20 70 43 75 72 3b 0a 20 20 20 20 70 43 75   = pCur;.    pCu
15ea5 72 20 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b  r = pCur->pNext;
15ea6 0a 20 20 20 20 69 66 28 20 70 54 6d 70 2d 3e 70  .    if( pTmp->p
15ea7 42 74 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20  Btree==p ){.    
15ea8 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c    sqlite3BtreeCl
15ea9 6f 73 65 43 75 72 73 6f 72 28 70 54 6d 70 29 3b  oseCursor(pTmp);
15eaa 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
15eab 20 52 6f 6c 6c 62 61 63 6b 20 61 6e 79 20 61 63   Rollback any ac
15eac 74 69 76 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  tive transaction
15ead 20 61 6e 64 20 66 72 65 65 20 74 68 65 20 68 61   and free the ha
15eae 6e 64 6c 65 20 73 74 72 75 63 74 75 72 65 2e 0a  ndle structure..
15eaf 20 20 2a 2a 20 54 68 65 20 63 61 6c 6c 20 74 6f    ** The call to
15eb0 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c   sqlite3BtreeRol
15eb1 6c 62 61 63 6b 28 29 20 64 72 6f 70 73 20 61 6e  lback() drops an
15eb2 79 20 74 61 62 6c 65 2d 6c 6f 63 6b 73 20 68 65  y table-locks he
15eb3 6c 64 20 62 79 0a 20 20 2a 2a 20 74 68 69 73 20  ld by.  ** this 
15eb4 68 61 6e 64 6c 65 2e 0a 20 20 2a 2f 0a 20 20 73  handle..  */.  s
15eb5 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62  qlite3BtreeRollb
15eb6 61 63 6b 28 70 29 3b 0a 20 20 73 71 6c 69 74 65  ack(p);.  sqlite
15eb7 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
15eb8 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61  .  /* If there a
15eb9 72 65 20 73 74 69 6c 6c 20 6f 74 68 65 72 20 6f  re still other o
15eba 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72  utstanding refer
15ebb 65 6e 63 65 73 20 74 6f 20 74 68 65 20 73 68 61  ences to the sha
15ebc 72 65 64 2d 62 74 72 65 65 0a 20 20 2a 2a 20 73  red-btree.  ** s
15ebd 74 72 75 63 74 75 72 65 2c 20 72 65 74 75 72 6e  tructure, return
15ebe 20 6e 6f 77 2e 20 54 68 65 20 72 65 6d 61 69 6e   now. The remain
15ebf 64 65 72 20 6f 66 20 74 68 69 73 20 70 72 6f 63  der of this proc
15ec0 65 64 75 72 65 20 63 6c 65 61 6e 73 20 0a 20 20  edure cleans .  
15ec1 2a 2a 20 75 70 20 74 68 65 20 73 68 61 72 65 64  ** up the shared
15ec2 2d 62 74 72 65 65 2e 0a 20 20 2a 2f 0a 20 20 61  -btree..  */.  a
15ec3 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f  ssert( p->wantTo
15ec4 4c 6f 63 6b 3d 3d 30 20 26 26 20 70 2d 3e 6c 6f  Lock==0 && p->lo
15ec5 63 6b 65 64 3d 3d 30 20 29 3b 0a 20 20 69 66 28  cked==0 );.  if(
15ec6 20 21 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c   !p->sharable ||
15ec7 20 72 65 6d 6f 76 65 46 72 6f 6d 53 68 61 72 69   removeFromShari
15ec8 6e 67 4c 69 73 74 28 70 42 74 29 20 29 7b 0a 20  ngList(pBt) ){. 
15ec9 20 20 20 2f 2a 20 54 68 65 20 70 42 74 20 69 73     /* The pBt is
15eca 20 6e 6f 20 6c 6f 6e 67 65 72 20 6f 6e 20 74 68   no longer on th
15ecb 65 20 73 68 61 72 69 6e 67 20 6c 69 73 74 2c 20  e sharing list, 
15ecc 73 6f 20 77 65 20 63 61 6e 20 61 63 63 65 73 73  so we can access
15ecd 0a 20 20 20 20 2a 2a 20 69 74 20 77 69 74 68 6f  .    ** it witho
15ece 75 74 20 68 61 76 69 6e 67 20 74 6f 20 68 6f 6c  ut having to hol
15ecf 64 20 74 68 65 20 6d 75 74 65 78 2e 0a 20 20 20  d the mutex..   
15ed0 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 6c 65 61 6e   **.    ** Clean
15ed1 20 6f 75 74 20 61 6e 64 20 64 65 6c 65 74 65 20   out and delete 
15ed2 74 68 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a  the BtShared obj
15ed3 65 63 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ect..    */.    
15ed4 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e 70 43  assert( !pBt->pC
15ed5 75 72 73 6f 72 20 29 3b 0a 20 20 20 20 73 71 6c  ursor );.    sql
15ed6 69 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 70  ite3PagerClose(p
15ed7 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20  Bt->pPager);.   
15ed8 20 69 66 28 20 70 42 74 2d 3e 78 46 72 65 65 53   if( pBt->xFreeS
15ed9 63 68 65 6d 61 20 26 26 20 70 42 74 2d 3e 70 53  chema && pBt->pS
15eda 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 70  chema ){.      p
15edb 42 74 2d 3e 78 46 72 65 65 53 63 68 65 6d 61 28  Bt->xFreeSchema(
15edc 70 42 74 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  pBt->pSchema);. 
15edd 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
15ede 5f 66 72 65 65 28 70 42 74 2d 3e 70 53 63 68 65  _free(pBt->pSche
15edf 6d 61 29 3b 0a 20 20 20 20 66 72 65 65 54 65 6d  ma);.    freeTem
15ee0 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20  pSpace(pBt);.   
15ee1 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42   sqlite3_free(pB
15ee2 74 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66  t);.  }..#ifndef
15ee3 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
15ee4 52 45 44 5f 43 41 43 48 45 0a 20 20 61 73 73 65  RED_CACHE.  asse
15ee5 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63  rt( p->wantToLoc
15ee6 6b 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  k==0 );.  assert
15ee7 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29  ( p->locked==0 )
15ee8 3b 0a 20 20 69 66 28 20 70 2d 3e 70 50 72 65 76  ;.  if( p->pPrev
15ee9 20 29 20 70 2d 3e 70 50 72 65 76 2d 3e 70 4e 65   ) p->pPrev->pNe
15eea 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20  xt = p->pNext;. 
15eeb 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 20   if( p->pNext ) 
15eec 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20  p->pNext->pPrev 
15eed 3d 20 70 2d 3e 70 50 72 65 76 3b 0a 23 65 6e 64  = p->pPrev;.#end
15eee 69 66 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  if..  sqlite3_fr
15eef 65 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  ee(p);.  return 
15ef0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
15ef1 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 6c  .** Change the l
15ef2 69 6d 69 74 20 6f 6e 20 74 68 65 20 6e 75 6d 62  imit on the numb
15ef3 65 72 20 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f  er of pages allo
15ef4 77 65 64 20 69 6e 20 74 68 65 20 63 61 63 68 65  wed in the cache
15ef5 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69  ..**.** The maxi
15ef6 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 61  mum number of ca
15ef7 63 68 65 20 70 61 67 65 73 20 69 73 20 73 65 74  che pages is set
15ef8 20 74 6f 20 74 68 65 20 61 62 73 6f 6c 75 74 65   to the absolute
15ef9 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 6d 78 50  .** value of mxP
15efa 61 67 65 2e 20 20 49 66 20 6d 78 50 61 67 65 20  age.  If mxPage 
15efb 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65  is negative, the
15efc 20 70 61 67 65 72 20 77 69 6c 6c 0a 2a 2a 20 6f   pager will.** o
15efd 70 65 72 61 74 65 20 61 73 79 6e 63 68 72 6f 6e  perate asynchron
15efe 6f 75 73 6c 79 20 2d 20 69 74 20 77 69 6c 6c 20  ously - it will 
15eff 6e 6f 74 20 73 74 6f 70 20 74 6f 20 64 6f 20 66  not stop to do f
15f00 73 79 6e 63 28 29 73 0a 2a 2a 20 74 6f 20 69 6e  sync()s.** to in
15f01 73 75 72 65 20 64 61 74 61 20 69 73 20 77 72 69  sure data is wri
15f02 74 74 65 6e 20 74 6f 20 74 68 65 20 64 69 73 6b  tten to the disk
15f03 20 73 75 72 66 61 63 65 20 62 65 66 6f 72 65 0a   surface before.
15f04 2a 2a 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 20 20  ** continuing.  
15f05 54 72 61 6e 73 61 63 74 69 6f 6e 73 20 73 74 69  Transactions sti
15f06 6c 6c 20 77 6f 72 6b 20 69 66 20 73 79 6e 63 68  ll work if synch
15f07 72 6f 6e 6f 75 73 20 69 73 20 6f 66 66 2c 0a 2a  ronous is off,.*
15f08 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61  * and the databa
15f09 73 65 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 72  se cannot be cor
15f0a 72 75 70 74 65 64 20 69 66 20 74 68 69 73 20 70  rupted if this p
15f0b 72 6f 67 72 61 6d 0a 2a 2a 20 63 72 61 73 68 65  rogram.** crashe
15f0c 73 2e 20 20 42 75 74 20 69 66 20 74 68 65 20 6f  s.  But if the o
15f0d 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20  perating system 
15f0e 63 72 61 73 68 65 73 20 6f 72 20 74 68 65 72 65  crashes or there
15f0f 20 69 73 0a 2a 2a 20 61 6e 20 61 62 72 75 70 74   is.** an abrupt
15f10 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 77   power failure w
15f11 68 65 6e 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20  hen synchronous 
15f12 69 73 20 6f 66 66 2c 20 74 68 65 20 64 61 74 61  is off, the data
15f13 62 61 73 65 0a 2a 2a 20 63 6f 75 6c 64 20 62 65  base.** could be
15f14 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f   left in an inco
15f15 6e 73 69 73 74 65 6e 74 20 61 6e 64 20 75 6e 72  nsistent and unr
15f16 65 63 6f 76 65 72 61 62 6c 65 20 73 74 61 74 65  ecoverable state
15f17 2e 0a 2a 2a 20 53 79 6e 63 68 72 6f 6e 6f 75 73  ..** Synchronous
15f18 20 69 73 20 6f 6e 20 62 79 20 64 65 66 61 75 6c   is on by defaul
15f19 74 20 73 6f 20 64 61 74 61 62 61 73 65 20 63 6f  t so database co
15f1a 72 72 75 70 74 69 6f 6e 20 69 73 20 6e 6f 74 0a  rruption is not.
15f1b 2a 2a 20 6e 6f 72 6d 61 6c 6c 79 20 61 20 77 6f  ** normally a wo
15f1c 72 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  rry..*/.SQLITE_P
15f1d 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
15f1e 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65 53  e3BtreeSetCacheS
15f1f 69 7a 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e  ize(Btree *p, in
15f20 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 42 74 53  t mxPage){.  BtS
15f21 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
15f22 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73  pBt;.  assert( s
15f23 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
15f24 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  d(p->db->mutex) 
15f25 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
15f26 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 73 71 6c  eEnter(p);.  sql
15f27 69 74 65 33 50 61 67 65 72 53 65 74 43 61 63 68  ite3PagerSetCach
15f28 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65  esize(pBt->pPage
15f29 72 2c 20 6d 78 50 61 67 65 29 3b 0a 20 20 73 71  r, mxPage);.  sq
15f2a 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
15f2b 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  p);.  return SQL
15f2c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
15f2d 20 43 68 61 6e 67 65 20 74 68 65 20 77 61 79 20   Change the way 
15f2e 64 61 74 61 20 69 73 20 73 79 6e 63 65 64 20 74  data is synced t
15f2f 6f 20 64 69 73 6b 20 69 6e 20 6f 72 64 65 72 20  o disk in order 
15f30 74 6f 20 69 6e 63 72 65 61 73 65 20 6f 72 20 64  to increase or d
15f31 65 63 72 65 61 73 65 0a 2a 2a 20 68 6f 77 20 77  ecrease.** how w
15f32 65 6c 6c 20 74 68 65 20 64 61 74 61 62 61 73 65  ell the database
15f33 20 72 65 73 69 73 74 73 20 64 61 6d 61 67 65 20   resists damage 
15f34 64 75 65 20 74 6f 20 4f 53 20 63 72 61 73 68 65  due to OS crashe
15f35 73 20 61 6e 64 20 70 6f 77 65 72 0a 2a 2a 20 66  s and power.** f
15f36 61 69 6c 75 72 65 73 2e 20 20 4c 65 76 65 6c 20  ailures.  Level 
15f37 31 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73  1 is the same as
15f38 20 61 73 79 6e 63 68 72 6f 6e 6f 75 73 20 28 6e   asynchronous (n
15f39 6f 20 73 79 6e 63 73 28 29 20 6f 63 63 75 72 20  o syncs() occur 
15f3a 61 6e 64 0a 2a 2a 20 74 68 65 72 65 20 69 73 20  and.** there is 
15f3b 61 20 68 69 67 68 20 70 72 6f 62 61 62 69 6c 69  a high probabili
15f3c 74 79 20 6f 66 20 64 61 6d 61 67 65 29 20 20 4c  ty of damage)  L
15f3d 65 76 65 6c 20 32 20 69 73 20 74 68 65 20 64 65  evel 2 is the de
15f3e 66 61 75 6c 74 2e 20 20 54 68 65 72 65 0a 2a 2a  fault.  There.**
15f3f 20 69 73 20 61 20 76 65 72 79 20 6c 6f 77 20 62   is a very low b
15f40 75 74 20 6e 6f 6e 2d 7a 65 72 6f 20 70 72 6f 62  ut non-zero prob
15f41 61 62 69 6c 69 74 79 20 6f 66 20 64 61 6d 61 67  ability of damag
15f42 65 2e 20 20 4c 65 76 65 6c 20 33 20 72 65 64 75  e.  Level 3 redu
15f43 63 65 73 20 74 68 65 0a 2a 2a 20 70 72 6f 62 61  ces the.** proba
15f44 62 69 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65  bility of damage
15f45 20 74 6f 20 6e 65 61 72 20 7a 65 72 6f 20 62 75   to near zero bu
15f46 74 20 77 69 74 68 20 61 20 77 72 69 74 65 20 70  t with a write p
15f47 65 72 66 6f 72 6d 61 6e 63 65 20 72 65 64 75 63  erformance reduc
15f48 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  tion..*/.#ifndef
15f49 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47   SQLITE_OMIT_PAG
15f4a 45 52 5f 50 52 41 47 4d 41 53 0a 53 51 4c 49 54  ER_PRAGMAS.SQLIT
15f4b 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
15f4c 6c 69 74 65 33 42 74 72 65 65 53 65 74 53 61 66  lite3BtreeSetSaf
15f4d 65 74 79 4c 65 76 65 6c 28 42 74 72 65 65 20 2a  etyLevel(Btree *
15f4e 70 2c 20 69 6e 74 20 6c 65 76 65 6c 2c 20 69 6e  p, int level, in
15f4f 74 20 66 75 6c 6c 53 79 6e 63 29 7b 0a 20 20 42  t fullSync){.  B
15f50 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
15f51 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28  ->pBt;.  assert(
15f52 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
15f53 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78  eld(p->db->mutex
15f54 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  ) );.  sqlite3Bt
15f55 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 73  reeEnter(p);.  s
15f56 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 53 61  qlite3PagerSetSa
15f57 66 65 74 79 4c 65 76 65 6c 28 70 42 74 2d 3e 70  fetyLevel(pBt->p
15f58 50 61 67 65 72 2c 20 6c 65 76 65 6c 2c 20 66 75  Pager, level, fu
15f59 6c 6c 53 79 6e 63 29 3b 0a 20 20 73 71 6c 69 74  llSync);.  sqlit
15f5a 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
15f5b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
15f5c 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  _OK;.}.#endif../
15f5d 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45  *.** Return TRUE
15f5e 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 62 74   if the given bt
15f5f 72 65 65 20 69 73 20 73 65 74 20 74 6f 20 73 61  ree is set to sa
15f60 66 65 74 79 20 6c 65 76 65 6c 20 31 2e 20 20 49  fety level 1.  I
15f61 6e 20 6f 74 68 65 72 0a 2a 2a 20 77 6f 72 64 73  n other.** words
15f62 2c 20 72 65 74 75 72 6e 20 54 52 55 45 20 69 66  , return TRUE if
15f63 20 6e 6f 20 73 79 6e 63 28 29 20 6f 63 63 75 72   no sync() occur
15f64 73 20 6f 6e 20 74 68 65 20 64 69 73 6b 20 66 69  s on the disk fi
15f65 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  les..*/.SQLITE_P
15f66 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
15f67 65 33 42 74 72 65 65 53 79 6e 63 44 69 73 61 62  e3BtreeSyncDisab
15f68 6c 65 64 28 42 74 72 65 65 20 2a 70 29 7b 0a 20  led(Btree *p){. 
15f69 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
15f6a 20 70 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 20 72   p->pBt;.  int r
15f6b 63 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  c;.  assert( sql
15f6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
15f6d 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  p->db->mutex) );
15f6e 20 20 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65    .  sqlite3Btre
15f6f 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73  eEnter(p);.  ass
15f70 65 72 74 28 20 70 42 74 20 26 26 20 70 42 74 2d  ert( pBt && pBt-
15f71 3e 70 50 61 67 65 72 20 29 3b 0a 20 20 72 63 20  >pPager );.  rc 
15f72 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4e 6f  = sqlite3PagerNo
15f73 73 79 6e 63 28 70 42 74 2d 3e 70 50 61 67 65 72  sync(pBt->pPager
15f74 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
15f75 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74  eLeave(p);.  ret
15f76 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 21  urn rc;.}..#if !
15f77 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
15f78 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41  MIT_PAGER_PRAGMA
15f79 53 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53  S) || !defined(S
15f7a 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55  QLITE_OMIT_VACUU
15f7b 4d 29 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20  M)./*.** Change 
15f7c 74 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 65  the default page
15f7d 73 20 73 69 7a 65 20 61 6e 64 20 74 68 65 20 6e  s size and the n
15f7e 75 6d 62 65 72 20 6f 66 20 72 65 73 65 72 76 65  umber of reserve
15f7f 64 20 62 79 74 65 73 20 70 65 72 20 70 61 67 65  d bytes per page
15f80 2e 0a 2a 2a 20 4f 72 2c 20 69 66 20 74 68 65 20  ..** Or, if the 
15f81 70 61 67 65 20 73 69 7a 65 20 68 61 73 20 61 6c  page size has al
15f82 72 65 61 64 79 20 62 65 65 6e 20 66 69 78 65 64  ready been fixed
15f83 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  , return SQLITE_
15f84 52 45 41 44 4f 4e 4c 59 20 0a 2a 2a 20 77 69 74  READONLY .** wit
15f85 68 6f 75 74 20 63 68 61 6e 67 69 6e 67 20 61 6e  hout changing an
15f86 79 74 68 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68  ything..**.** Th
15f87 65 20 70 61 67 65 20 73 69 7a 65 20 6d 75 73 74  e page size must
15f88 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 32   be a power of 2
15f89 20 62 65 74 77 65 65 6e 20 35 31 32 20 61 6e 64   between 512 and
15f8a 20 36 35 35 33 36 2e 20 20 49 66 20 74 68 65 20   65536.  If the 
15f8b 70 61 67 65 0a 2a 2a 20 73 69 7a 65 20 73 75 70  page.** size sup
15f8c 70 6c 69 65 64 20 64 6f 65 73 20 6e 6f 74 20 6d  plied does not m
15f8d 65 65 74 20 74 68 69 73 20 63 6f 6e 73 74 72 61  eet this constra
15f8e 69 6e 74 20 74 68 65 6e 20 74 68 65 20 70 61 67  int then the pag
15f8f 65 20 73 69 7a 65 20 69 73 20 6e 6f 74 0a 2a 2a  e size is not.**
15f90 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20   changed..**.** 
15f91 50 61 67 65 20 73 69 7a 65 73 20 61 72 65 20 63  Page sizes are c
15f92 6f 6e 73 74 72 61 69 6e 65 64 20 74 6f 20 62 65  onstrained to be
15f93 20 61 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 20   a power of two 
15f94 73 6f 20 74 68 61 74 20 74 68 65 20 72 65 67 69  so that the regi
15f95 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 64 61 74  on.** of the dat
15f96 61 62 61 73 65 20 66 69 6c 65 20 75 73 65 64 20  abase file used 
15f97 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 28 62 65 67  for locking (beg
15f98 69 6e 6e 69 6e 67 20 61 74 20 50 45 4e 44 49 4e  inning at PENDIN
15f99 47 5f 42 59 54 45 2c 0a 2a 2a 20 74 68 65 20 66  G_BYTE,.** the f
15f9a 69 72 73 74 20 62 79 74 65 20 70 61 73 74 20 74  irst byte past t
15f9b 68 65 20 31 47 42 20 62 6f 75 6e 64 61 72 79 2c  he 1GB boundary,
15f9c 20 30 78 34 30 30 30 30 30 30 30 29 20 6e 65 65   0x40000000) nee
15f9d 64 73 20 74 6f 20 6f 63 63 75 72 0a 2a 2a 20 61  ds to occur.** a
15f9e 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  t the beginning 
15f9f 6f 66 20 61 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a  of a page..**.**
15fa0 20 49 66 20 70 61 72 61 6d 65 74 65 72 20 6e 52   If parameter nR
15fa1 65 73 65 72 76 65 20 69 73 20 6c 65 73 73 20 74  eserve is less t
15fa2 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74  han zero, then t
15fa3 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73  he number of res
15fa4 65 72 76 65 64 0a 2a 2a 20 62 79 74 65 73 20 70  erved.** bytes p
15fa5 65 72 20 70 61 67 65 20 69 73 20 6c 65 66 74 20  er page is left 
15fa6 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a  unchanged..**.**
15fa7 20 49 66 20 74 68 65 20 69 46 69 78 21 3d 30 20   If the iFix!=0 
15fa8 74 68 65 6e 20 74 68 65 20 70 61 67 65 53 69 7a  then the pageSiz
15fa9 65 46 69 78 65 64 20 66 6c 61 67 20 69 73 20 73  eFixed flag is s
15faa 65 74 20 73 6f 20 74 68 61 74 20 74 68 65 20 70  et so that the p
15fab 61 67 65 20 73 69 7a 65 0a 2a 2a 20 61 6e 64 20  age size.** and 
15fac 61 75 74 6f 76 61 63 75 75 6d 20 6d 6f 64 65 20  autovacuum mode 
15fad 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65  can no longer be
15fae 20 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c   changed..*/.SQL
15faf 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
15fb0 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 50  sqlite3BtreeSetP
15fb1 61 67 65 53 69 7a 65 28 42 74 72 65 65 20 2a 70  ageSize(Btree *p
15fb2 2c 20 69 6e 74 20 70 61 67 65 53 69 7a 65 2c 20  , int pageSize, 
15fb3 69 6e 74 20 6e 52 65 73 65 72 76 65 2c 20 69 6e  int nReserve, in
15fb4 74 20 69 46 69 78 29 7b 0a 20 20 69 6e 74 20 72  t iFix){.  int r
15fb5 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
15fb6 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
15fb7 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72   p->pBt;.  asser
15fb8 74 28 20 6e 52 65 73 65 72 76 65 3e 3d 2d 31 20  t( nReserve>=-1 
15fb9 26 26 20 6e 52 65 73 65 72 76 65 3c 3d 32 35 35  && nReserve<=255
15fba 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72   );.  sqlite3Btr
15fbb 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66  eeEnter(p);.  if
15fbc 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46  ( pBt->pageSizeF
15fbd 69 78 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69  ixed ){.    sqli
15fbe 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
15fbf 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
15fc0 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20  ITE_READONLY;.  
15fc1 7d 0a 20 20 69 66 28 20 6e 52 65 73 65 72 76 65  }.  if( nReserve
15fc2 3c 30 20 29 7b 0a 20 20 20 20 6e 52 65 73 65 72  <0 ){.    nReser
15fc3 76 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69  ve = pBt->pageSi
15fc4 7a 65 20 2d 20 70 42 74 2d 3e 75 73 61 62 6c 65  ze - pBt->usable
15fc5 53 69 7a 65 3b 0a 20 20 7d 0a 20 20 61 73 73 65  Size;.  }.  asse
15fc6 72 74 28 20 6e 52 65 73 65 72 76 65 3e 3d 30 20  rt( nReserve>=0 
15fc7 26 26 20 6e 52 65 73 65 72 76 65 3c 3d 32 35 35  && nReserve<=255
15fc8 20 29 3b 0a 20 20 69 66 28 20 70 61 67 65 53 69   );.  if( pageSi
15fc9 7a 65 3e 3d 35 31 32 20 26 26 20 70 61 67 65 53  ze>=512 && pageS
15fca 69 7a 65 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f  ize<=SQLITE_MAX_
15fcb 50 41 47 45 5f 53 49 5a 45 20 26 26 0a 20 20 20  PAGE_SIZE &&.   
15fcc 20 20 20 20 20 28 28 70 61 67 65 53 69 7a 65 2d       ((pageSize-
15fcd 31 29 26 70 61 67 65 53 69 7a 65 29 3d 3d 30 20  1)&pageSize)==0 
15fce 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  ){.    assert( (
15fcf 70 61 67 65 53 69 7a 65 20 26 20 37 29 3d 3d 30  pageSize & 7)==0
15fd0 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
15fd1 21 70 42 74 2d 3e 70 50 61 67 65 31 20 26 26 20  !pBt->pPage1 && 
15fd2 21 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 29 3b  !pBt->pCursor );
15fd3 0a 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69  .    pBt->pageSi
15fd4 7a 65 20 3d 20 28 75 31 36 29 70 61 67 65 53 69  ze = (u16)pageSi
15fd5 7a 65 3b 0a 20 20 20 20 66 72 65 65 54 65 6d 70  ze;.    freeTemp
15fd6 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 7d 0a  Space(pBt);.  }.
15fd7 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
15fd8 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 70  gerSetPagesize(p
15fd9 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 70 42 74  Bt->pPager, &pBt
15fda 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6e 52 65 73  ->pageSize, nRes
15fdb 65 72 76 65 29 3b 0a 20 20 70 42 74 2d 3e 75 73  erve);.  pBt->us
15fdc 61 62 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e  ableSize = pBt->
15fdd 70 61 67 65 53 69 7a 65 20 2d 20 28 75 31 36 29  pageSize - (u16)
15fde 6e 52 65 73 65 72 76 65 3b 0a 20 20 69 66 28 20  nReserve;.  if( 
15fdf 69 46 69 78 20 29 20 70 42 74 2d 3e 70 61 67 65  iFix ) pBt->page
15fe0 53 69 7a 65 46 69 78 65 64 20 3d 20 31 3b 0a 20  SizeFixed = 1;. 
15fe1 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
15fe2 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  ve(p);.  return 
15fe3 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  rc;.}../*.** Ret
15fe4 75 72 6e 20 74 68 65 20 63 75 72 72 65 6e 74 6c  urn the currentl
15fe5 79 20 64 65 66 69 6e 65 64 20 70 61 67 65 20 73  y defined page s
15fe6 69 7a 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ize.*/.SQLITE_PR
15fe7 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
15fe8 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69 7a  3BtreeGetPageSiz
15fe9 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 72  e(Btree *p){.  r
15fea 65 74 75 72 6e 20 70 2d 3e 70 42 74 2d 3e 70 61  eturn p->pBt->pa
15feb 67 65 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  geSize;.}../*.**
15fec 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
15fed 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73  er of bytes of s
15fee 70 61 63 65 20 61 74 20 74 68 65 20 65 6e 64 20  pace at the end 
15fef 6f 66 20 65 76 65 72 79 20 70 61 67 65 20 74 68  of every page th
15ff0 61 74 0a 2a 2a 20 61 72 65 20 69 6e 74 65 6e 74  at.** are intent
15ff1 75 61 6c 6c 79 20 6c 65 66 74 20 75 6e 75 73 65  ually left unuse
15ff2 64 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20  d.  This is the 
15ff3 22 72 65 73 65 72 76 65 64 22 20 73 70 61 63 65  "reserved" space
15ff4 20 74 68 61 74 20 69 73 0a 2a 2a 20 73 6f 6d 65   that is.** some
15ff5 74 69 6d 65 73 20 75 73 65 64 20 62 79 20 65 78  times used by ex
15ff6 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c  tensions..*/.SQL
15ff7 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
15ff8 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 52  sqlite3BtreeGetR
15ff9 65 73 65 72 76 65 28 42 74 72 65 65 20 2a 70 29  eserve(Btree *p)
15ffa 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c  {.  int n;.  sql
15ffb 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
15ffc 29 3b 0a 20 20 6e 20 3d 20 70 2d 3e 70 42 74 2d  );.  n = p->pBt-
15ffd 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 2d 3e 70  >pageSize - p->p
15ffe 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a  Bt->usableSize;.
15fff 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
16000 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
16001 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74   n;.}../*.** Set
16002 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 70 61 67   the maximum pag
16003 65 20 63 6f 75 6e 74 20 66 6f 72 20 61 20 64 61  e count for a da
16004 74 61 62 61 73 65 20 69 66 20 6d 78 50 61 67 65  tabase if mxPage
16005 20 69 73 20 70 6f 73 69 74 69 76 65 2e 0a 2a 2a   is positive..**
16006 20 4e 6f 20 63 68 61 6e 67 65 73 20 61 72 65 20   No changes are 
16007 6d 61 64 65 20 69 66 20 6d 78 50 61 67 65 20 69  made if mxPage i
16008 73 20 30 20 6f 72 20 6e 65 67 61 74 69 76 65 2e  s 0 or negative.
16009 0a 2a 2a 20 52 65 67 61 72 64 6c 65 73 73 20 6f  .** Regardless o
1600a 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6d  f the value of m
1600b 78 50 61 67 65 2c 20 72 65 74 75 72 6e 20 74 68  xPage, return th
1600c 65 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 63  e maximum page c
1600d 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ount..*/.SQLITE_
1600e 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1600f 74 65 33 42 74 72 65 65 4d 61 78 50 61 67 65 43  te3BtreeMaxPageC
16010 6f 75 6e 74 28 42 74 72 65 65 20 2a 70 2c 20 69  ount(Btree *p, i
16011 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 69 6e  nt mxPage){.  in
16012 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  t n;.  sqlite3Bt
16013 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 6e  reeEnter(p);.  n
16014 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d   = sqlite3PagerM
16015 61 78 50 61 67 65 43 6f 75 6e 74 28 70 2d 3e 70  axPageCount(p->p
16016 42 74 2d 3e 70 50 61 67 65 72 2c 20 6d 78 50 61  Bt->pPager, mxPa
16017 67 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  ge);.  sqlite3Bt
16018 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72  reeLeave(p);.  r
16019 65 74 75 72 6e 20 6e 3b 0a 7d 0a 23 65 6e 64 69  eturn n;.}.#endi
1601a 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51  f /* !defined(SQ
1601b 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f  LITE_OMIT_PAGER_
1601c 50 52 41 47 4d 41 53 29 20 7c 7c 20 21 64 65 66  PRAGMAS) || !def
1601d 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
1601e 5f 56 41 43 55 55 4d 29 20 2a 2f 0a 0a 2f 2a 0a  _VACUUM) */../*.
1601f 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 27 61  ** Change the 'a
16020 75 74 6f 2d 76 61 63 75 75 6d 27 20 70 72 6f 70  uto-vacuum' prop
16021 65 72 74 79 20 6f 66 20 74 68 65 20 64 61 74 61  erty of the data
16022 62 61 73 65 2e 20 49 66 20 74 68 65 20 27 61 75  base. If the 'au
16023 74 6f 56 61 63 75 75 6d 27 0a 2a 2a 20 70 61 72  toVacuum'.** par
16024 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65  ameter is non-ze
16025 72 6f 2c 20 74 68 65 6e 20 61 75 74 6f 2d 76 61  ro, then auto-va
16026 63 75 75 6d 20 6d 6f 64 65 20 69 73 20 65 6e 61  cuum mode is ena
16027 62 6c 65 64 2e 20 49 66 20 7a 65 72 6f 2c 20 69  bled. If zero, i
16028 74 0a 2a 2a 20 69 73 20 64 69 73 61 62 6c 65 64  t.** is disabled
16029 2e 20 54 68 65 20 64 65 66 61 75 6c 74 20 76 61  . The default va
1602a 6c 75 65 20 66 6f 72 20 74 68 65 20 61 75 74 6f  lue for the auto
1602b 2d 76 61 63 75 75 6d 20 70 72 6f 70 65 72 74 79  -vacuum property
1602c 20 69 73 20 0a 2a 2a 20 64 65 74 65 72 6d 69 6e   is .** determin
1602d 65 64 20 62 79 20 74 68 65 20 53 51 4c 49 54 45  ed by the SQLITE
1602e 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43  _DEFAULT_AUTOVAC
1602f 55 55 4d 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 53 51  UUM macro..*/.SQ
16030 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
16031 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74   sqlite3BtreeSet
16032 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65 65  AutoVacuum(Btree
16033 20 2a 70 2c 20 69 6e 74 20 61 75 74 6f 56 61 63   *p, int autoVac
16034 75 75 6d 29 7b 0a 23 69 66 64 65 66 20 53 51 4c  uum){.#ifdef SQL
16035 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
16036 55 55 4d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  UUM.  return SQL
16037 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 23 65  ITE_READONLY;.#e
16038 6c 73 65 0a 20 20 42 74 53 68 61 72 65 64 20 2a  lse.  BtShared *
16039 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
1603a 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
1603b 4f 4b 3b 0a 20 20 75 38 20 61 76 20 3d 20 28 75  OK;.  u8 av = (u
1603c 38 29 61 75 74 6f 56 61 63 75 75 6d 3b 0a 0a 20  8)autoVacuum;.. 
1603d 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
1603e 65 72 28 70 29 3b 0a 20 20 69 66 28 20 70 42 74  er(p);.  if( pBt
1603f 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64 20  ->pageSizeFixed 
16040 26 26 20 28 61 76 20 3f 31 3a 30 29 21 3d 70 42  && (av ?1:0)!=pB
16041 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b  t->autoVacuum ){
16042 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
16043 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 65 6c  _READONLY;.  }el
16044 73 65 7b 0a 20 20 20 20 70 42 74 2d 3e 61 75 74  se{.    pBt->aut
16045 6f 56 61 63 75 75 6d 20 3d 20 61 76 20 3f 31 3a  oVacuum = av ?1:
16046 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e 63 72  0;.    pBt->incr
16047 56 61 63 75 75 6d 20 3d 20 61 76 3d 3d 32 20 3f  Vacuum = av==2 ?
16048 31 3a 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  1:0;.  }.  sqlit
16049 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
1604a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65  .  return rc;.#e
1604b 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  ndif.}../*.** Re
1604c 74 75 72 6e 20 74 68 65 20 76 61 6c 75 65 20 6f  turn the value o
1604d 66 20 74 68 65 20 27 61 75 74 6f 2d 76 61 63 75  f the 'auto-vacu
1604e 75 6d 27 20 70 72 6f 70 65 72 74 79 2e 20 49 66  um' property. If
1604f 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 69 73 20   auto-vacuum is 
16050 0a 2a 2a 20 65 6e 61 62 6c 65 64 20 31 20 69 73  .** enabled 1 is
16051 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72   returned. Other
16052 77 69 73 65 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54  wise 0..*/.SQLIT
16053 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
16054 6c 69 74 65 33 42 74 72 65 65 47 65 74 41 75 74  lite3BtreeGetAut
16055 6f 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a 70  oVacuum(Btree *p
16056 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ){.#ifdef SQLITE
16057 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
16058 0a 20 20 72 65 74 75 72 6e 20 42 54 52 45 45 5f  .  return BTREE_
16059 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e 45 3b  AUTOVACUUM_NONE;
1605a 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20 72 63 3b  .#else.  int rc;
1605b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
1605c 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20  nter(p);.  rc = 
1605d 28 0a 20 20 20 20 28 21 70 2d 3e 70 42 74 2d 3e  (.    (!p->pBt->
1605e 61 75 74 6f 56 61 63 75 75 6d 29 3f 42 54 52 45  autoVacuum)?BTRE
1605f 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e  E_AUTOVACUUM_NON
16060 45 3a 0a 20 20 20 20 28 21 70 2d 3e 70 42 74 2d  E:.    (!p->pBt-
16061 3e 69 6e 63 72 56 61 63 75 75 6d 29 3f 42 54 52  >incrVacuum)?BTR
16062 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 46 55  EE_AUTOVACUUM_FU
16063 4c 4c 3a 0a 20 20 20 20 42 54 52 45 45 5f 41 55  LL:.    BTREE_AU
16064 54 4f 56 41 43 55 55 4d 5f 49 4e 43 52 0a 20 20  TOVACUUM_INCR.  
16065 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
16066 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74  eLeave(p);.  ret
16067 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 7d  urn rc;.#endif.}
16068 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 72  .../*.** Get a r
16069 65 66 65 72 65 6e 63 65 20 74 6f 20 70 50 61 67  eference to pPag
1606a 65 31 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  e1 of the databa
1606b 73 65 20 66 69 6c 65 2e 20 20 54 68 69 73 20 77  se file.  This w
1606c 69 6c 6c 0a 2a 2a 20 61 6c 73 6f 20 61 63 71 75  ill.** also acqu
1606d 69 72 65 20 61 20 72 65 61 64 6c 6f 63 6b 20 6f  ire a readlock o
1606e 6e 20 74 68 61 74 20 66 69 6c 65 2e 0a 2a 2a 0a  n that file..**.
1606f 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  ** SQLITE_OK is 
16070 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 75 63 63  returned on succ
16071 65 73 73 2e 20 20 49 66 20 74 68 65 20 66 69 6c  ess.  If the fil
16072 65 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 77 65  e is not a.** we
16073 6c 6c 2d 66 6f 72 6d 65 64 20 64 61 74 61 62 61  ll-formed databa
16074 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 53 51  se file, then SQ
16075 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 69 73 20  LITE_CORRUPT is 
16076 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 53 51 4c  returned..** SQL
16077 49 54 45 5f 42 55 53 59 20 69 73 20 72 65 74 75  ITE_BUSY is retu
16078 72 6e 65 64 20 69 66 20 74 68 65 20 64 61 74 61  rned if the data
16079 62 61 73 65 20 69 73 20 6c 6f 63 6b 65 64 2e 20  base is locked. 
1607a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a   SQLITE_NOMEM.**
1607b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
1607c 77 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 6d 65  we run out of me
1607d 6d 6f 72 79 2e 20 0a 2a 2f 0a 73 74 61 74 69 63  mory. .*/.static
1607e 20 69 6e 74 20 6c 6f 63 6b 42 74 72 65 65 28 42   int lockBtree(B
1607f 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20  tShared *pBt){. 
16080 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61   int rc;.  MemPa
16081 67 65 20 2a 70 50 61 67 65 31 3b 0a 20 20 69 6e  ge *pPage1;.  in
16082 74 20 6e 50 61 67 65 3b 0a 0a 20 20 61 73 73 65  t nPage;..  asse
16083 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
16084 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65  x_held(pBt->mute
16085 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
16086 70 42 74 2d 3e 70 50 61 67 65 31 3d 3d 30 20 29  pBt->pPage1==0 )
16087 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
16088 50 61 67 65 72 53 68 61 72 65 64 4c 6f 63 6b 28  PagerSharedLock(
16089 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20  pBt->pPager);.  
1608a 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1608b 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  K ) return rc;. 
1608c 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61   rc = btreeGetPa
1608d 67 65 28 70 42 74 2c 20 31 2c 20 26 70 50 61 67  ge(pBt, 1, &pPag
1608e 65 31 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63  e1, 0);.  if( rc
1608f 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
16090 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 44  turn rc;..  /* D
16091 6f 20 73 6f 6d 65 20 63 68 65 63 6b 69 6e 67 20  o some checking 
16092 74 6f 20 68 65 6c 70 20 69 6e 73 75 72 65 20 74  to help insure t
16093 68 65 20 66 69 6c 65 20 77 65 20 6f 70 65 6e 65  he file we opene
16094 64 20 72 65 61 6c 6c 79 20 69 73 0a 20 20 2a 2a  d really is.  **
16095 20 61 20 76 61 6c 69 64 20 64 61 74 61 62 61 73   a valid databas
16096 65 20 66 69 6c 65 2e 20 0a 20 20 2a 2f 0a 20 20  e file. .  */.  
16097 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
16098 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e  rPagecount(pBt->
16099 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 3b  pPager, &nPage);
1609a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
1609b 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f  E_OK ){.    goto
1609c 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c   page1_init_fail
1609d 65 64 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  ed;.  }else if( 
1609e 6e 50 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 69  nPage>0 ){.    i
1609f 6e 74 20 70 61 67 65 53 69 7a 65 3b 0a 20 20 20  nt pageSize;.   
160a0 20 69 6e 74 20 75 73 61 62 6c 65 53 69 7a 65 3b   int usableSize;
160a1 0a 20 20 20 20 75 38 20 2a 70 61 67 65 31 20 3d  .    u8 *page1 =
160a2 20 70 50 61 67 65 31 2d 3e 61 44 61 74 61 3b 0a   pPage1->aData;.
160a3 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
160a4 4e 4f 54 41 44 42 3b 0a 20 20 20 20 69 66 28 20  NOTADB;.    if( 
160a5 6d 65 6d 63 6d 70 28 70 61 67 65 31 2c 20 7a 4d  memcmp(page1, zM
160a6 61 67 69 63 48 65 61 64 65 72 2c 20 31 36 29 21  agicHeader, 16)!
160a7 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  =0 ){.      goto
160a8 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c   page1_init_fail
160a9 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ed;.    }.    if
160aa 28 20 70 61 67 65 31 5b 31 38 5d 3e 31 20 29 7b  ( page1[18]>1 ){
160ab 0a 20 20 20 20 20 20 70 42 74 2d 3e 72 65 61 64  .      pBt->read
160ac 4f 6e 6c 79 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  Only = 1;.    }.
160ad 20 20 20 20 69 66 28 20 70 61 67 65 31 5b 31 39      if( page1[19
160ae 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  ]>1 ){.      got
160af 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69  o page1_init_fai
160b0 6c 65 64 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  led;.    }..    
160b1 2f 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 65  /* The maximum e
160b2 6d 62 65 64 64 65 64 20 66 72 61 63 74 69 6f 6e  mbedded fraction
160b3 20 6d 75 73 74 20 62 65 20 65 78 61 63 74 6c 79   must be exactly
160b4 20 32 35 25 2e 20 20 41 6e 64 20 74 68 65 20 6d   25%.  And the m
160b5 69 6e 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 65 6d  inimum.    ** em
160b6 62 65 64 64 65 64 20 66 72 61 63 74 69 6f 6e 20  bedded fraction 
160b7 6d 75 73 74 20 62 65 20 31 32 2e 35 25 20 66 6f  must be 12.5% fo
160b8 72 20 62 6f 74 68 20 6c 65 61 66 2d 64 61 74 61  r both leaf-data
160b9 20 61 6e 64 20 6e 6f 6e 2d 6c 65 61 66 2d 64 61   and non-leaf-da
160ba 74 61 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 6f  ta..    ** The o
160bb 72 69 67 69 6e 61 6c 20 64 65 73 69 67 6e 20 61  riginal design a
160bc 6c 6c 6f 77 65 64 20 74 68 65 73 65 20 61 6d 6f  llowed these amo
160bd 75 6e 74 73 20 74 6f 20 76 61 72 79 2c 20 62 75  unts to vary, bu
160be 74 20 61 73 20 6f 66 0a 20 20 20 20 2a 2a 20 76  t as of.    ** v
160bf 65 72 73 69 6f 6e 20 33 2e 36 2e 30 2c 20 77 65  ersion 3.6.0, we
160c0 20 72 65 71 75 69 72 65 20 74 68 65 6d 20 74 6f   require them to
160c1 20 62 65 20 66 69 78 65 64 2e 0a 20 20 20 20 2a   be fixed..    *
160c2 2f 0a 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70  /.    if( memcmp
160c3 28 26 70 61 67 65 31 5b 32 31 5d 2c 20 22 5c 31  (&page1[21], "\1
160c4 30 30 5c 30 34 30 5c 30 34 30 22 2c 33 29 21 3d  00\040\040",3)!=
160c5 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  0 ){.      goto 
160c6 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65  page1_init_faile
160c7 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 61 67  d;.    }.    pag
160c8 65 53 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65  eSize = get2byte
160c9 28 26 70 61 67 65 31 5b 31 36 5d 29 3b 0a 20 20  (&page1[16]);.  
160ca 20 20 69 66 28 20 28 28 70 61 67 65 53 69 7a 65    if( ((pageSize
160cb 2d 31 29 26 70 61 67 65 53 69 7a 65 29 21 3d 30  -1)&pageSize)!=0
160cc 20 7c 7c 20 70 61 67 65 53 69 7a 65 3c 35 31 32   || pageSize<512
160cd 20 7c 7c 0a 20 20 20 20 20 20 20 20 28 53 51 4c   ||.        (SQL
160ce 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a  ITE_MAX_PAGE_SIZ
160cf 45 3c 33 32 37 36 38 20 26 26 20 70 61 67 65 53  E<32768 && pageS
160d0 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50  ize>SQLITE_MAX_P
160d1 41 47 45 5f 53 49 5a 45 29 0a 20 20 20 20 29 7b  AGE_SIZE).    ){
160d2 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65  .      goto page
160d3 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20  1_init_failed;. 
160d4 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
160d5 20 28 70 61 67 65 53 69 7a 65 20 26 20 37 29 3d   (pageSize & 7)=
160d6 3d 30 20 29 3b 0a 20 20 20 20 75 73 61 62 6c 65  =0 );.    usable
160d7 53 69 7a 65 20 3d 20 70 61 67 65 53 69 7a 65 20  Size = pageSize 
160d8 2d 20 70 61 67 65 31 5b 32 30 5d 3b 0a 20 20 20  - page1[20];.   
160d9 20 69 66 28 20 70 61 67 65 53 69 7a 65 21 3d 70   if( pageSize!=p
160da 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 29 7b 0a  Bt->pageSize ){.
160db 20 20 20 20 20 20 2f 2a 20 41 66 74 65 72 20 72        /* After r
160dc 65 61 64 69 6e 67 20 74 68 65 20 66 69 72 73 74  eading the first
160dd 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74   page of the dat
160de 61 62 61 73 65 20 61 73 73 75 6d 69 6e 67 20 61  abase assuming a
160df 20 70 61 67 65 20 73 69 7a 65 0a 20 20 20 20 20   page size.     
160e0 20 2a 2a 20 6f 66 20 42 74 53 68 61 72 65 64 2e   ** of BtShared.
160e1 70 61 67 65 53 69 7a 65 2c 20 77 65 20 68 61 76  pageSize, we hav
160e2 65 20 64 69 73 63 6f 76 65 72 65 64 20 74 68 61  e discovered tha
160e3 74 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20  t the page-size 
160e4 69 73 0a 20 20 20 20 20 20 2a 2a 20 61 63 74 75  is.      ** actu
160e5 61 6c 6c 79 20 70 61 67 65 53 69 7a 65 2e 20 55  ally pageSize. U
160e6 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61  nlock the databa
160e7 73 65 2c 20 6c 65 61 76 65 20 70 42 74 2d 3e 70  se, leave pBt->p
160e8 50 61 67 65 31 20 61 74 0a 20 20 20 20 20 20 2a  Page1 at.      *
160e9 2a 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 75 72  * zero and retur
160ea 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 54 68 65  n SQLITE_OK. The
160eb 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 63 61 6c   caller will cal
160ec 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a  l this function.
160ed 20 20 20 20 20 20 2a 2a 20 61 67 61 69 6e 20 77        ** again w
160ee 69 74 68 20 74 68 65 20 63 6f 72 72 65 63 74 20  ith the correct 
160ef 70 61 67 65 2d 73 69 7a 65 2e 0a 20 20 20 20 20  page-size..     
160f0 20 2a 2f 0a 20 20 20 20 20 20 72 65 6c 65 61 73   */.      releas
160f1 65 50 61 67 65 28 70 50 61 67 65 31 29 3b 0a 20  ePage(pPage1);. 
160f2 20 20 20 20 20 70 42 74 2d 3e 75 73 61 62 6c 65       pBt->usable
160f3 53 69 7a 65 20 3d 20 28 75 31 36 29 75 73 61 62  Size = (u16)usab
160f4 6c 65 53 69 7a 65 3b 0a 20 20 20 20 20 20 70 42  leSize;.      pB
160f5 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 28 75  t->pageSize = (u
160f6 31 36 29 70 61 67 65 53 69 7a 65 3b 0a 20 20 20  16)pageSize;.   
160f7 20 20 20 66 72 65 65 54 65 6d 70 53 70 61 63 65     freeTempSpace
160f8 28 70 42 74 29 3b 0a 20 20 20 20 20 20 72 63 20  (pBt);.      rc 
160f9 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65  = sqlite3PagerSe
160fa 74 50 61 67 65 73 69 7a 65 28 70 42 74 2d 3e 70  tPagesize(pBt->p
160fb 50 61 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 67  Pager, &pBt->pag
160fc 65 53 69 7a 65 2c 0a 20 20 20 20 20 20 20 20 20  eSize,.         
160fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
160fe 20 20 20 20 20 20 20 20 20 20 70 61 67 65 53 69            pageSi
160ff 7a 65 2d 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a  ze-usableSize);.
16100 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
16101 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 75  .    }.    if( u
16102 73 61 62 6c 65 53 69 7a 65 3c 34 38 30 20 29 7b  sableSize<480 ){
16103 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65  .      goto page
16104 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20  1_init_failed;. 
16105 20 20 20 7d 0a 20 20 20 20 70 42 74 2d 3e 70 61     }.    pBt->pa
16106 67 65 53 69 7a 65 20 3d 20 28 75 31 36 29 70 61  geSize = (u16)pa
16107 67 65 53 69 7a 65 3b 0a 20 20 20 20 70 42 74 2d  geSize;.    pBt-
16108 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 28 75  >usableSize = (u
16109 31 36 29 75 73 61 62 6c 65 53 69 7a 65 3b 0a 23  16)usableSize;.#
1610a 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1610b 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
1610c 20 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75    pBt->autoVacuu
1610d 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28 26 70  m = (get4byte(&p
1610e 61 67 65 31 5b 33 36 20 2b 20 34 2a 34 5d 29 3f  age1[36 + 4*4])?
1610f 31 3a 30 29 3b 0a 20 20 20 20 70 42 74 2d 3e 69  1:0);.    pBt->i
16110 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 67 65 74  ncrVacuum = (get
16111 34 62 79 74 65 28 26 70 61 67 65 31 5b 33 36 20  4byte(&page1[36 
16112 2b 20 37 2a 34 5d 29 3f 31 3a 30 29 3b 0a 23 65  + 7*4])?1:0);.#e
16113 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 2f 2a 20 6d  ndif.  }..  /* m
16114 61 78 4c 6f 63 61 6c 20 69 73 20 74 68 65 20 6d  axLocal is the m
16115 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66  aximum amount of
16116 20 70 61 79 6c 6f 61 64 20 74 6f 20 73 74 6f 72   payload to stor
16117 65 20 6c 6f 63 61 6c 6c 79 20 66 6f 72 0a 20 20  e locally for.  
16118 2a 2a 20 61 20 63 65 6c 6c 2e 20 20 4d 61 6b 65  ** a cell.  Make
16119 20 73 75 72 65 20 69 74 20 69 73 20 73 6d 61 6c   sure it is smal
1611a 6c 20 65 6e 6f 75 67 68 20 73 6f 20 74 68 61 74  l enough so that
1611b 20 61 74 20 6c 65 61 73 74 20 6d 69 6e 46 61 6e   at least minFan
1611c 6f 75 74 0a 20 20 2a 2a 20 63 65 6c 6c 73 20 63  out.  ** cells c
1611d 61 6e 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 6f  an will fit on o
1611e 6e 65 20 70 61 67 65 2e 20 20 57 65 20 61 73 73  ne page.  We ass
1611f 75 6d 65 20 61 20 31 30 2d 62 79 74 65 20 70 61  ume a 10-byte pa
16120 67 65 20 68 65 61 64 65 72 2e 0a 20 20 2a 2a 20  ge header..  ** 
16121 42 65 73 69 64 65 73 20 74 68 65 20 70 61 79 6c  Besides the payl
16122 6f 61 64 2c 20 74 68 65 20 63 65 6c 6c 20 6d 75  oad, the cell mu
16123 73 74 20 73 74 6f 72 65 3a 0a 20 20 2a 2a 20 20  st store:.  **  
16124 20 20 20 32 2d 62 79 74 65 20 70 6f 69 6e 74 65     2-byte pointe
16125 72 20 74 6f 20 74 68 65 20 63 65 6c 6c 0a 20 20  r to the cell.  
16126 2a 2a 20 20 20 20 20 34 2d 62 79 74 65 20 63 68  **     4-byte ch
16127 69 6c 64 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a  ild pointer.  **
16128 20 20 20 20 20 39 2d 62 79 74 65 20 6e 4b 65 79       9-byte nKey
16129 20 76 61 6c 75 65 0a 20 20 2a 2a 20 20 20 20 20   value.  **     
1612a 34 2d 62 79 74 65 20 6e 44 61 74 61 20 76 61 6c  4-byte nData val
1612b 75 65 0a 20 20 2a 2a 20 20 20 20 20 34 2d 62 79  ue.  **     4-by
1612c 74 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  te overflow page
1612d 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 53 6f   pointer.  ** So
1612e 20 61 20 63 65 6c 6c 20 63 6f 6e 73 69 73 74 73   a cell consists
1612f 20 6f 66 20 61 20 32 2d 62 79 74 65 20 70 6f 69   of a 2-byte poi
16130 6e 65 72 2c 20 61 20 68 65 61 64 65 72 20 77 68  ner, a header wh
16131 69 63 68 20 69 73 20 61 73 20 6d 75 63 68 20 61  ich is as much a
16132 73 0a 20 20 2a 2a 20 31 37 20 62 79 74 65 73 20  s.  ** 17 bytes 
16133 6c 6f 6e 67 2c 20 30 20 74 6f 20 4e 20 62 79 74  long, 0 to N byt
16134 65 73 20 6f 66 20 70 61 79 6c 6f 61 64 2c 20 61  es of payload, a
16135 6e 64 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 34  nd an optional 4
16136 20 62 79 74 65 20 6f 76 65 72 66 6c 6f 77 0a 20   byte overflow. 
16137 20 2a 2a 20 70 61 67 65 20 70 6f 69 6e 74 65 72   ** page pointer
16138 2e 0a 20 20 2a 2f 0a 20 20 70 42 74 2d 3e 6d 61  ..  */.  pBt->ma
16139 78 4c 6f 63 61 6c 20 3d 20 28 70 42 74 2d 3e 75  xLocal = (pBt->u
1613a 73 61 62 6c 65 53 69 7a 65 2d 31 32 29 2a 36 34  sableSize-12)*64
1613b 2f 32 35 35 20 2d 20 32 33 3b 0a 20 20 70 42 74  /255 - 23;.  pBt
1613c 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 28 70 42  ->minLocal = (pB
1613d 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32  t->usableSize-12
1613e 29 2a 33 32 2f 32 35 35 20 2d 20 32 33 3b 0a 20  )*32/255 - 23;. 
1613f 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66 20 3d 20   pBt->maxLeaf = 
16140 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20  pBt->usableSize 
16141 2d 20 33 35 3b 0a 20 20 70 42 74 2d 3e 6d 69 6e  - 35;.  pBt->min
16142 4c 65 61 66 20 3d 20 28 70 42 74 2d 3e 75 73 61  Leaf = (pBt->usa
16143 62 6c 65 53 69 7a 65 2d 31 32 29 2a 33 32 2f 32  bleSize-12)*32/2
16144 35 35 20 2d 20 32 33 3b 0a 20 20 61 73 73 65 72  55 - 23;.  asser
16145 74 28 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66 20  t( pBt->maxLeaf 
16146 2b 20 32 33 20 3c 3d 20 4d 58 5f 43 45 4c 4c 5f  + 23 <= MX_CELL_
16147 53 49 5a 45 28 70 42 74 29 20 29 3b 0a 20 20 70  SIZE(pBt) );.  p
16148 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 70 50 61  Bt->pPage1 = pPa
16149 67 65 31 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  ge1;.  return SQ
1614a 4c 49 54 45 5f 4f 4b 3b 0a 0a 70 61 67 65 31 5f  LITE_OK;..page1_
1614b 69 6e 69 74 5f 66 61 69 6c 65 64 3a 0a 20 20 72  init_failed:.  r
1614c 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65  eleasePage(pPage
1614d 31 29 3b 0a 20 20 70 42 74 2d 3e 70 50 61 67 65  1);.  pBt->pPage
1614e 31 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20  1 = 0;.  return 
1614f 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  rc;.}../*.** If 
16150 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6f 75 74  there are no out
16151 73 74 61 6e 64 69 6e 67 20 63 75 72 73 6f 72 73  standing cursors
16152 20 61 6e 64 20 77 65 20 61 72 65 20 6e 6f 74 20   and we are not 
16153 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 0a 2a 2a  in the middle.**
16154 20 6f 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f   of a transactio
16155 6e 20 62 75 74 20 74 68 65 72 65 20 69 73 20 61  n but there is a
16156 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68   read lock on th
16157 65 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e  e database, then
16158 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65  .** this routine
16159 20 75 6e 72 65 66 73 20 74 68 65 20 66 69 72 73   unrefs the firs
1615a 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61  t page of the da
1615b 74 61 62 61 73 65 20 66 69 6c 65 20 77 68 69 63  tabase file whic
1615c 68 20 0a 2a 2a 20 68 61 73 20 74 68 65 20 65 66  h .** has the ef
1615d 66 65 63 74 20 6f 66 20 72 65 6c 65 61 73 69 6e  fect of releasin
1615e 67 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e  g the read lock.
1615f 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20  .**.** If there 
16160 69 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  is a transaction
16161 20 69 6e 20 70 72 6f 67 72 65 73 73 2c 20 74 68   in progress, th
16162 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
16163 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63  no-op..*/.static
16164 20 76 6f 69 64 20 75 6e 6c 6f 63 6b 42 74 72 65   void unlockBtre
16165 65 49 66 55 6e 75 73 65 64 28 42 74 53 68 61 72  eIfUnused(BtShar
16166 65 64 20 2a 70 42 74 29 7b 0a 20 20 61 73 73 65  ed *pBt){.  asse
16167 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
16168 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65  x_held(pBt->mute
16169 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
1616a 70 42 74 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 20  pBt->pCursor==0 
1616b 7c 7c 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61  || pBt->inTransa
1616c 63 74 69 6f 6e 3e 54 52 41 4e 53 5f 4e 4f 4e 45  ction>TRANS_NONE
1616d 20 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 69   );.  if( pBt->i
1616e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52  nTransaction==TR
1616f 41 4e 53 5f 4e 4f 4e 45 20 26 26 20 70 42 74 2d  ANS_NONE && pBt-
16170 3e 70 50 61 67 65 31 21 3d 30 20 29 7b 0a 20 20  >pPage1!=0 ){.  
16171 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70    assert( pBt->p
16172 50 61 67 65 31 2d 3e 61 44 61 74 61 20 29 3b 0a  Page1->aData );.
16173 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
16174 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74  te3PagerRefcount
16175 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3d 3d 31  (pBt->pPager)==1
16176 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
16177 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61  pBt->pPage1->aDa
16178 74 61 20 29 3b 0a 20 20 20 20 72 65 6c 65 61 73  ta );.    releas
16179 65 50 61 67 65 28 70 42 74 2d 3e 70 50 61 67 65  ePage(pBt->pPage
1617a 31 29 3b 0a 20 20 20 20 70 42 74 2d 3e 70 50 61  1);.    pBt->pPa
1617b 67 65 31 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a  ge1 = 0;.  }.}..
1617c 2f 2a 0a 2a 2a 20 49 66 20 70 42 74 20 70 6f 69  /*.** If pBt poi
1617d 6e 74 73 20 74 6f 20 61 6e 20 65 6d 70 74 79 20  nts to an empty 
1617e 66 69 6c 65 20 74 68 65 6e 20 63 6f 6e 76 65 72  file then conver
1617f 74 20 74 68 61 74 20 65 6d 70 74 79 20 66 69 6c  t that empty fil
16180 65 0a 2a 2a 20 69 6e 74 6f 20 61 20 6e 65 77 20  e.** into a new 
16181 65 6d 70 74 79 20 64 61 74 61 62 61 73 65 20 62  empty database b
16182 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74  y initializing t
16183 68 65 20 66 69 72 73 74 20 70 61 67 65 20 6f 66  he first page of
16184 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65  .** the database
16185 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
16186 6e 65 77 44 61 74 61 62 61 73 65 28 42 74 53 68  newDatabase(BtSh
16187 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 4d 65  ared *pBt){.  Me
16188 6d 50 61 67 65 20 2a 70 50 31 3b 0a 20 20 75 6e  mPage *pP1;.  un
16189 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74  signed char *dat
1618a 61 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69  a;.  int rc;.  i
1618b 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 20 61 73 73  nt nPage;..  ass
1618c 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1618d 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74  ex_held(pBt->mut
1618e 65 78 29 20 29 3b 0a 20 20 2f 2a 20 54 68 65 20  ex) );.  /* The 
1618f 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 68 61  database size ha
16190 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6d  s already been m
16191 65 61 73 75 72 65 64 20 61 6e 64 20 63 61 63 68  easured and cach
16192 65 64 2c 20 73 6f 20 66 61 69 6c 75 72 65 0a 20  ed, so failure. 
16193 20 2a 2a 20 69 73 20 69 6d 70 6f 73 73 69 62 6c   ** is impossibl
16194 65 20 68 65 72 65 2e 20 20 49 66 20 74 68 65 20  e here.  If the 
16195 6f 72 69 67 69 6e 61 6c 20 73 69 7a 65 20 6d 65  original size me
16196 61 73 75 72 65 6d 65 6e 74 20 66 61 69 6c 65 64  asurement failed
16197 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 70 72 6f 63  , then.  ** proc
16198 65 73 73 69 6e 67 20 61 62 6f 72 74 73 20 62 65  essing aborts be
16199 66 6f 72 65 20 65 6e 74 65 72 69 6e 67 20 74 68  fore entering th
1619a 69 73 20 72 6f 75 74 69 6e 65 2e 20 2a 2f 0a 20  is routine. */. 
1619b 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
1619c 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d  erPagecount(pBt-
1619d 3e 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29  >pPager, &nPage)
1619e 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 72 63  ;.  if( NEVER(rc
1619f 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 7c 7c 20  !=SQLITE_OK) || 
161a0 6e 50 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 72  nPage>0 ){.    r
161a1 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20  eturn rc;.  }.  
161a2 70 50 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65  pP1 = pBt->pPage
161a3 31 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 31  1;.  assert( pP1
161a4 21 3d 30 20 29 3b 0a 20 20 64 61 74 61 20 3d 20  !=0 );.  data = 
161a5 70 50 31 2d 3e 61 44 61 74 61 3b 0a 20 20 72 63  pP1->aData;.  rc
161a6 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57   = sqlite3PagerW
161a7 72 69 74 65 28 70 50 31 2d 3e 70 44 62 50 61 67  rite(pP1->pDbPag
161a8 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72  e);.  if( rc ) r
161a9 65 74 75 72 6e 20 72 63 3b 0a 20 20 6d 65 6d 63  eturn rc;.  memc
161aa 70 79 28 64 61 74 61 2c 20 7a 4d 61 67 69 63 48  py(data, zMagicH
161ab 65 61 64 65 72 2c 20 73 69 7a 65 6f 66 28 7a 4d  eader, sizeof(zM
161ac 61 67 69 63 48 65 61 64 65 72 29 29 3b 0a 20 20  agicHeader));.  
161ad 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 7a  assert( sizeof(z
161ae 4d 61 67 69 63 48 65 61 64 65 72 29 3d 3d 31 36  MagicHeader)==16
161af 20 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26   );.  put2byte(&
161b0 64 61 74 61 5b 31 36 5d 2c 20 70 42 74 2d 3e 70  data[16], pBt->p
161b1 61 67 65 53 69 7a 65 29 3b 0a 20 20 64 61 74 61  ageSize);.  data
161b2 5b 31 38 5d 20 3d 20 31 3b 0a 20 20 64 61 74 61  [18] = 1;.  data
161b3 5b 31 39 5d 20 3d 20 31 3b 0a 20 20 61 73 73 65  [19] = 1;.  asse
161b4 72 74 28 20 70 42 74 2d 3e 75 73 61 62 6c 65 53  rt( pBt->usableS
161b5 69 7a 65 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69  ize<=pBt->pageSi
161b6 7a 65 20 26 26 20 70 42 74 2d 3e 75 73 61 62 6c  ze && pBt->usabl
161b7 65 53 69 7a 65 2b 32 35 35 3e 3d 70 42 74 2d 3e  eSize+255>=pBt->
161b8 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 64 61 74  pageSize);.  dat
161b9 61 5b 32 30 5d 20 3d 20 28 75 38 29 28 70 42 74  a[20] = (u8)(pBt
161ba 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 42 74  ->pageSize - pBt
161bb 2d 3e 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a 20  ->usableSize);. 
161bc 20 64 61 74 61 5b 32 31 5d 20 3d 20 36 34 3b 0a   data[21] = 64;.
161bd 20 20 64 61 74 61 5b 32 32 5d 20 3d 20 33 32 3b    data[22] = 32;
161be 0a 20 20 64 61 74 61 5b 32 33 5d 20 3d 20 33 32  .  data[23] = 32
161bf 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 74 61  ;.  memset(&data
161c0 5b 32 34 5d 2c 20 30 2c 20 31 30 30 2d 32 34 29  [24], 0, 100-24)
161c1 3b 0a 20 20 7a 65 72 6f 50 61 67 65 28 70 50 31  ;.  zeroPage(pP1
161c2 2c 20 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46  , PTF_INTKEY|PTF
161c3 5f 4c 45 41 46 7c 50 54 46 5f 4c 45 41 46 44 41  _LEAF|PTF_LEAFDA
161c4 54 41 20 29 3b 0a 20 20 70 42 74 2d 3e 70 61 67  TA );.  pBt->pag
161c5 65 53 69 7a 65 46 69 78 65 64 20 3d 20 31 3b 0a  eSizeFixed = 1;.
161c6 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
161c7 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
161c8 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75   assert( pBt->au
161c9 74 6f 56 61 63 75 75 6d 3d 3d 31 20 7c 7c 20 70  toVacuum==1 || p
161ca 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 3d 3d  Bt->autoVacuum==
161cb 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
161cc 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 3d 3d  Bt->incrVacuum==
161cd 31 20 7c 7c 20 70 42 74 2d 3e 69 6e 63 72 56 61  1 || pBt->incrVa
161ce 63 75 75 6d 3d 3d 30 20 29 3b 0a 20 20 70 75 74  cuum==0 );.  put
161cf 34 62 79 74 65 28 26 64 61 74 61 5b 33 36 20 2b  4byte(&data[36 +
161d0 20 34 2a 34 5d 2c 20 70 42 74 2d 3e 61 75 74 6f   4*4], pBt->auto
161d1 56 61 63 75 75 6d 29 3b 0a 20 20 70 75 74 34 62  Vacuum);.  put4b
161d2 79 74 65 28 26 64 61 74 61 5b 33 36 20 2b 20 37  yte(&data[36 + 7
161d3 2a 34 5d 2c 20 70 42 74 2d 3e 69 6e 63 72 56 61  *4], pBt->incrVa
161d4 63 75 75 6d 29 3b 0a 23 65 6e 64 69 66 0a 20 20  cuum);.#endif.  
161d5 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
161d6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d  ;.}../*.** Attem
161d7 70 74 20 74 6f 20 73 74 61 72 74 20 61 20 6e 65  pt to start a ne
161d8 77 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 41  w transaction. A
161d9 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69   write-transacti
161da 6f 6e 0a 2a 2a 20 69 73 20 73 74 61 72 74 65 64  on.** is started
161db 20 69 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61   if the second a
161dc 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 6e 7a 65  rgument is nonze
161dd 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 20 61 20  ro, otherwise a 
161de 72 65 61 64 2d 0a 2a 2a 20 74 72 61 6e 73 61 63  read-.** transac
161df 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 20 73 65  tion.  If the se
161e0 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73  cond argument is
161e1 20 32 20 6f 72 20 6d 6f 72 65 20 61 6e 64 20 65   2 or more and e
161e2 78 63 6c 75 73 69 76 65 0a 2a 2a 20 74 72 61 6e  xclusive.** tran
161e3 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74  saction is start
161e4 65 64 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61 74  ed, meaning that
161e5 20 6e 6f 20 6f 74 68 65 72 20 70 72 6f 63 65 73   no other proces
161e6 73 20 69 73 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20  s is allowed.** 
161e7 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 64 61  to access the da
161e8 74 61 62 61 73 65 2e 20 20 41 20 70 72 65 65 78  tabase.  A preex
161e9 69 73 74 69 6e 67 20 74 72 61 6e 73 61 63 74 69  isting transacti
161ea 6f 6e 20 6d 61 79 20 6e 6f 74 20 62 65 0a 2a 2a  on may not be.**
161eb 20 75 70 67 72 61 64 65 64 20 74 6f 20 65 78 63   upgraded to exc
161ec 6c 75 73 69 76 65 20 62 79 20 63 61 6c 6c 69 6e  lusive by callin
161ed 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61  g this routine a
161ee 20 73 65 63 6f 6e 64 20 74 69 6d 65 20 2d 20 74   second time - t
161ef 68 65 0a 2a 2a 20 65 78 63 6c 75 73 69 76 69 74  he.** exclusivit
161f0 79 20 66 6c 61 67 20 6f 6e 6c 79 20 77 6f 72 6b  y flag only work
161f1 73 20 66 6f 72 20 61 20 6e 65 77 20 74 72 61 6e  s for a new tran
161f2 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41  saction..**.** A
161f3 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69   write-transacti
161f4 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 61 72 74  on must be start
161f5 65 64 20 62 65 66 6f 72 65 20 61 74 74 65 6d 70  ed before attemp
161f6 74 69 6e 67 20 61 6e 79 20 0a 2a 2a 20 63 68 61  ting any .** cha
161f7 6e 67 65 73 20 74 6f 20 74 68 65 20 64 61 74 61  nges to the data
161f8 62 61 73 65 2e 20 20 4e 6f 6e 65 20 6f 66 20 74  base.  None of t
161f9 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75  he following rou
161fa 74 69 6e 65 73 20 0a 2a 2a 20 77 69 6c 6c 20 77  tines .** will w
161fb 6f 72 6b 20 75 6e 6c 65 73 73 20 61 20 74 72 61  ork unless a tra
161fc 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72  nsaction is star
161fd 74 65 64 20 66 69 72 73 74 3a 0a 2a 2a 0a 2a 2a  ted first:.**.**
161fe 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
161ff 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 29 0a  eeCreateTable().
16200 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  **      sqlite3B
16201 74 72 65 65 43 72 65 61 74 65 49 6e 64 65 78 28  treeCreateIndex(
16202 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65  ).**      sqlite
16203 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65  3BtreeClearTable
16204 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74  ().**      sqlit
16205 65 33 42 74 72 65 65 44 72 6f 70 54 61 62 6c 65  e3BtreeDropTable
16206 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74  ().**      sqlit
16207 65 33 42 74 72 65 65 49 6e 73 65 72 74 28 29 0a  e3BtreeInsert().
16208 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  **      sqlite3B
16209 74 72 65 65 44 65 6c 65 74 65 28 29 0a 2a 2a 20  treeDelete().** 
1620a 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
1620b 65 55 70 64 61 74 65 4d 65 74 61 28 29 0a 2a 2a  eUpdateMeta().**
1620c 0a 2a 2a 20 49 66 20 61 6e 20 69 6e 69 74 69 61  .** If an initia
1620d 6c 20 61 74 74 65 6d 70 74 20 74 6f 20 61 63 71  l attempt to acq
1620e 75 69 72 65 20 74 68 65 20 6c 6f 63 6b 20 66 61  uire the lock fa
1620f 69 6c 73 20 62 65 63 61 75 73 65 20 6f 66 20 6c  ils because of l
16210 6f 63 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 0a 2a  ock contention.*
16211 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61  * and the databa
16212 73 65 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c  se was previousl
16213 79 20 75 6e 6c 6f 63 6b 65 64 2c 20 74 68 65 6e  y unlocked, then
16214 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79   invoke the busy
16215 20 68 61 6e 64 6c 65 72 0a 2a 2a 20 69 66 20 74   handler.** if t
16216 68 65 72 65 20 69 73 20 6f 6e 65 2e 20 20 42 75  here is one.  Bu
16217 74 20 69 66 20 74 68 65 72 65 20 77 61 73 20 70  t if there was p
16218 72 65 76 69 6f 75 73 6c 79 20 61 20 72 65 61 64  reviously a read
16219 2d 6c 6f 63 6b 2c 20 64 6f 20 6e 6f 74 0a 2a 2a  -lock, do not.**
1621a 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79   invoke the busy
1621b 20 68 61 6e 64 6c 65 72 20 2d 20 6a 75 73 74 20   handler - just 
1621c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55  return SQLITE_BU
1621d 53 59 2e 20 20 53 51 4c 49 54 45 5f 42 55 53 59  SY.  SQLITE_BUSY
1621e 20 69 73 20 0a 2a 2a 20 72 65 74 75 72 6e 65 64   is .** returned
1621f 20 77 68 65 6e 20 74 68 65 72 65 20 69 73 20 61   when there is a
16220 6c 72 65 61 64 79 20 61 20 72 65 61 64 2d 6c 6f  lready a read-lo
16221 63 6b 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 61  ck in order to a
16222 76 6f 69 64 20 61 20 64 65 61 64 6c 6f 63 6b 2e  void a deadlock.
16223 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73 65 20 74  .**.** Suppose t
16224 68 65 72 65 20 61 72 65 20 74 77 6f 20 70 72 6f  here are two pro
16225 63 65 73 73 65 73 20 41 20 61 6e 64 20 42 2e 20  cesses A and B. 
16226 20 41 20 68 61 73 20 61 20 72 65 61 64 20 6c 6f   A has a read lo
16227 63 6b 20 61 6e 64 20 42 20 68 61 73 0a 2a 2a 20  ck and B has.** 
16228 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 2e  a reserved lock.
16229 20 20 42 20 74 72 69 65 73 20 74 6f 20 70 72 6f    B tries to pro
1622a 6d 6f 74 65 20 74 6f 20 65 78 63 6c 75 73 69 76  mote to exclusiv
1622b 65 20 62 75 74 20 69 73 20 62 6c 6f 63 6b 65 64  e but is blocked
1622c 20 62 65 63 61 75 73 65 0a 2a 2a 20 6f 66 20 41   because.** of A
1622d 27 73 20 72 65 61 64 20 6c 6f 63 6b 2e 20 20 41  's read lock.  A
1622e 20 74 72 69 65 73 20 74 6f 20 70 72 6f 6d 6f 74   tries to promot
1622f 65 20 74 6f 20 72 65 73 65 72 76 65 64 20 62 75  e to reserved bu
16230 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 20  t is blocked by 
16231 42 2e 0a 2a 2a 20 4f 6e 65 20 6f 72 20 74 68 65  B..** One or the
16232 20 6f 74 68 65 72 20 6f 66 20 74 68 65 20 74 77   other of the tw
16233 6f 20 70 72 6f 63 65 73 73 65 73 20 6d 75 73 74  o processes must
16234 20 67 69 76 65 20 77 61 79 20 6f 72 20 74 68 65   give way or the
16235 72 65 20 63 61 6e 20 62 65 0a 2a 2a 20 6e 6f 20  re can be.** no 
16236 70 72 6f 67 72 65 73 73 2e 20 20 42 79 20 72 65  progress.  By re
16237 74 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f 42  turning SQLITE_B
16238 55 53 59 20 61 6e 64 20 6e 6f 74 20 69 6e 76 6f  USY and not invo
16239 6b 69 6e 67 20 74 68 65 20 62 75 73 79 20 63 61  king the busy ca
1623a 6c 6c 62 61 63 6b 0a 2a 2a 20 77 68 65 6e 20 41  llback.** when A
1623b 20 61 6c 72 65 61 64 79 20 68 61 73 20 61 20 72   already has a r
1623c 65 61 64 20 6c 6f 63 6b 2c 20 77 65 20 65 6e 63  ead lock, we enc
1623d 6f 75 72 61 67 65 20 41 20 74 6f 20 67 69 76 65  ourage A to give
1623e 20 75 70 20 61 6e 64 20 6c 65 74 20 42 0a 2a 2a   up and let B.**
1623f 20 70 72 6f 63 65 65 64 2e 0a 2a 2f 0a 53 51 4c   proceed..*/.SQL
16240 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
16241 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69  sqlite3BtreeBegi
16242 6e 54 72 61 6e 73 28 42 74 72 65 65 20 2a 70 2c  nTrans(Btree *p,
16243 20 69 6e 74 20 77 72 66 6c 61 67 29 7b 0a 20 20   int wrflag){.  
16244 73 71 6c 69 74 65 33 20 2a 70 42 6c 6f 63 6b 20  sqlite3 *pBlock 
16245 3d 20 30 3b 0a 20 20 42 74 53 68 61 72 65 64 20  = 0;.  BtShared 
16246 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20  *pBt = p->pBt;. 
16247 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
16248 5f 4f 4b 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42  _OK;..  sqlite3B
16249 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20  treeEnter(p);.  
1624a 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70  btreeIntegrity(p
1624b 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
1624c 62 74 72 65 65 20 69 73 20 61 6c 72 65 61 64 79  btree is already
1624d 20 69 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e   in a write-tran
1624e 73 61 63 74 69 6f 6e 2c 20 6f 72 20 69 74 0a 20  saction, or it. 
1624f 20 2a 2a 20 69 73 20 61 6c 72 65 61 64 79 20 69   ** is already i
16250 6e 20 61 20 72 65 61 64 2d 74 72 61 6e 73 61 63  n a read-transac
16251 74 69 6f 6e 20 61 6e 64 20 61 20 72 65 61 64 2d  tion and a read-
16252 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a  transaction.  **
16253 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 74   is requested, t
16254 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  his is a no-op..
16255 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 6e    */.  if( p->in
16256 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49  Trans==TRANS_WRI
16257 54 45 20 7c 7c 20 28 70 2d 3e 69 6e 54 72 61 6e  TE || (p->inTran
16258 73 3d 3d 54 52 41 4e 53 5f 52 45 41 44 20 26 26  s==TRANS_READ &&
16259 20 21 77 72 66 6c 61 67 29 20 29 7b 0a 20 20 20   !wrflag) ){.   
1625a 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75   goto trans_begu
1625b 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 72 69  n;.  }..  /* Wri
1625c 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20  te transactions 
1625d 61 72 65 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65  are not possible
1625e 20 6f 6e 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20   on a read-only 
1625f 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 66  database */.  if
16260 28 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20  ( pBt->readOnly 
16261 26 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20  && wrflag ){.   
16262 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 41   rc = SQLITE_REA
16263 44 4f 4e 4c 59 3b 0a 20 20 20 20 67 6f 74 6f 20  DONLY;.    goto 
16264 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 7d  trans_begun;.  }
16265 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
16266 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
16267 48 45 0a 20 20 2f 2a 20 49 66 20 61 6e 6f 74 68  HE.  /* If anoth
16268 65 72 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  er database hand
16269 6c 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 6f  le has already o
1626a 70 65 6e 65 64 20 61 20 77 72 69 74 65 20 74 72  pened a write tr
1626b 61 6e 73 61 63 74 69 6f 6e 20 0a 20 20 2a 2a 20  ansaction .  ** 
1626c 6f 6e 20 74 68 69 73 20 73 68 61 72 65 64 2d 62  on this shared-b
1626d 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 61  tree structure a
1626e 6e 64 20 61 20 73 65 63 6f 6e 64 20 77 72 69 74  nd a second writ
1626f 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  e transaction is
16270 0a 20 20 2a 2a 20 72 65 71 75 65 73 74 65 64 2c  .  ** requested,
16271 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c   return SQLITE_L
16272 4f 43 4b 45 44 2e 0a 20 20 2a 2f 0a 20 20 69 66  OCKED..  */.  if
16273 28 20 28 77 72 66 6c 61 67 20 26 26 20 70 42 74  ( (wrflag && pBt
16274 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d  ->inTransaction=
16275 3d 54 52 41 4e 53 5f 57 52 49 54 45 29 20 7c 7c  =TRANS_WRITE) ||
16276 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20   pBt->isPending 
16277 29 7b 0a 20 20 20 20 70 42 6c 6f 63 6b 20 3d 20  ){.    pBlock = 
16278 70 42 74 2d 3e 70 57 72 69 74 65 72 2d 3e 64 62  pBt->pWriter->db
16279 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 77 72  ;.  }else if( wr
1627a 66 6c 61 67 3e 31 20 29 7b 0a 20 20 20 20 42 74  flag>1 ){.    Bt
1627b 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a 20 20 20  Lock *pIter;.   
1627c 20 66 6f 72 28 70 49 74 65 72 3d 70 42 74 2d 3e   for(pIter=pBt->
1627d 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70 49  pLock; pIter; pI
1627e 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78 74  ter=pIter->pNext
1627f 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 49 74  ){.      if( pIt
16280 65 72 2d 3e 70 42 74 72 65 65 21 3d 70 20 29 7b  er->pBtree!=p ){
16281 0a 20 20 20 20 20 20 20 20 70 42 6c 6f 63 6b 20  .        pBlock 
16282 3d 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 2d  = pIter->pBtree-
16283 3e 64 62 3b 0a 20 20 20 20 20 20 20 20 62 72 65  >db;.        bre
16284 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
16285 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 42 6c 6f  }.  }.  if( pBlo
16286 63 6b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ck ){.    sqlite
16287 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b  3ConnectionBlock
16288 65 64 28 70 2d 3e 64 62 2c 20 70 42 6c 6f 63 6b  ed(p->db, pBlock
16289 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  );.    rc = SQLI
1628a 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44  TE_LOCKED_SHARED
1628b 43 41 43 48 45 3b 0a 20 20 20 20 67 6f 74 6f 20  CACHE;.    goto 
1628c 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 7d  trans_begun;.  }
1628d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 6e  .#endif..  /* An
1628e 79 20 72 65 61 64 2d 6f 6e 6c 79 20 6f 72 20 72  y read-only or r
1628f 65 61 64 2d 77 72 69 74 65 20 74 72 61 6e 73 61  ead-write transa
16290 63 74 69 6f 6e 20 69 6d 70 6c 69 65 73 20 61 20  ction implies a 
16291 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 0a 20 20  read-lock on .  
16292 2a 2a 20 70 61 67 65 20 31 2e 20 53 6f 20 69 66  ** page 1. So if
16293 20 73 6f 6d 65 20 6f 74 68 65 72 20 73 68 61 72   some other shar
16294 65 64 2d 63 61 63 68 65 20 63 6c 69 65 6e 74 20  ed-cache client 
16295 61 6c 72 65 61 64 79 20 68 61 73 20 61 20 77 72  already has a wr
16296 69 74 65 2d 6c 6f 63 6b 20 0a 20 20 2a 2a 20 6f  ite-lock .  ** o
16297 6e 20 70 61 67 65 20 31 2c 20 74 68 65 20 74 72  n page 1, the tr
16298 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e 6e 6f 74  ansaction cannot
16299 20 62 65 20 6f 70 65 6e 65 64 2e 20 2a 2f 0a 20   be opened. */. 
1629a 20 72 63 20 3d 20 71 75 65 72 79 53 68 61 72 65   rc = queryShare
1629b 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28  dCacheTableLock(
1629c 70 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20  p, MASTER_ROOT, 
1629d 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 69 66  READ_LOCK);.  if
1629e 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 20  ( SQLITE_OK!=rc 
1629f 29 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67  ) goto trans_beg
162a0 75 6e 3b 0a 0a 20 20 64 6f 20 7b 0a 20 20 20 20  un;..  do {.    
162a1 2f 2a 20 43 61 6c 6c 20 6c 6f 63 6b 42 74 72 65  /* Call lockBtre
162a2 65 28 29 20 75 6e 74 69 6c 20 65 69 74 68 65 72  e() until either
162a3 20 70 42 74 2d 3e 70 50 61 67 65 31 20 69 73 20   pBt->pPage1 is 
162a4 70 6f 70 75 6c 61 74 65 64 20 6f 72 0a 20 20 20  populated or.   
162a5 20 2a 2a 20 6c 6f 63 6b 42 74 72 65 65 28 29 20   ** lockBtree() 
162a6 72 65 74 75 72 6e 73 20 73 6f 6d 65 74 68 69 6e  returns somethin
162a7 67 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c  g other than SQL
162a8 49 54 45 5f 4f 4b 2e 20 6c 6f 63 6b 42 74 72 65  ITE_OK. lockBtre
162a9 65 28 29 0a 20 20 20 20 2a 2a 20 6d 61 79 20 72  e().    ** may r
162aa 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
162ab 62 75 74 20 6c 65 61 76 65 20 70 42 74 2d 3e 70  but leave pBt->p
162ac 50 61 67 65 31 20 73 65 74 20 74 6f 20 30 20 69  Page1 set to 0 i
162ad 66 20 61 66 74 65 72 0a 20 20 20 20 2a 2a 20 72  f after.    ** r
162ae 65 61 64 69 6e 67 20 70 61 67 65 20 31 20 69 74  eading page 1 it
162af 20 64 69 73 63 6f 76 65 72 73 20 74 68 61 74 20   discovers that 
162b0 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 6f 66  the page-size of
162b1 20 74 68 65 20 64 61 74 61 62 61 73 65 20 0a 20   the database . 
162b2 20 20 20 2a 2a 20 66 69 6c 65 20 69 73 20 6e 6f     ** file is no
162b3 74 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2e  t pBt->pageSize.
162b4 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 6c 6f   In this case lo
162b5 63 6b 42 74 72 65 65 28 29 20 77 69 6c 6c 20 75  ckBtree() will u
162b6 70 64 61 74 65 0a 20 20 20 20 2a 2a 20 70 42 74  pdate.    ** pBt
162b7 2d 3e 70 61 67 65 53 69 7a 65 20 74 6f 20 74 68  ->pageSize to th
162b8 65 20 70 61 67 65 2d 73 69 7a 65 20 6f 66 20 74  e page-size of t
162b9 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e  he file on disk.
162ba 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77 68 69 6c  .    */.    whil
162bb 65 28 20 70 42 74 2d 3e 70 50 61 67 65 31 3d 3d  e( pBt->pPage1==
162bc 30 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d  0 && SQLITE_OK==
162bd 28 72 63 20 3d 20 6c 6f 63 6b 42 74 72 65 65 28  (rc = lockBtree(
162be 70 42 74 29 29 20 29 3b 0a 0a 20 20 20 20 69 66  pBt)) );..    if
162bf 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
162c0 26 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20  && wrflag ){.   
162c1 20 20 20 69 66 28 20 70 42 74 2d 3e 72 65 61 64     if( pBt->read
162c2 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20 20 20 20  Only ){.        
162c3 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 41 44  rc = SQLITE_READ
162c4 4f 4e 4c 59 3b 0a 20 20 20 20 20 20 7d 65 6c 73  ONLY;.      }els
162c5 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  e{.        rc = 
162c6 73 71 6c 69 74 65 33 50 61 67 65 72 42 65 67 69  sqlite3PagerBegi
162c7 6e 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 77 72  n(pBt->pPager,wr
162c8 66 6c 61 67 3e 31 2c 73 71 6c 69 74 65 33 54 65  flag>1,sqlite3Te
162c9 6d 70 49 6e 4d 65 6d 6f 72 79 28 70 2d 3e 64 62  mpInMemory(p->db
162ca 29 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ));.        if( 
162cb 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
162cc 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
162cd 6e 65 77 44 61 74 61 62 61 73 65 28 70 42 74 29  newDatabase(pBt)
162ce 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
162cf 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20    }.    }.  .   
162d0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
162d1 4f 4b 20 29 7b 0a 20 20 20 20 20 20 75 6e 6c 6f  OK ){.      unlo
162d2 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64 28  ckBtreeIfUnused(
162d3 70 42 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 77  pBt);.    }.  }w
162d4 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45  hile( rc==SQLITE
162d5 5f 42 55 53 59 20 26 26 20 70 42 74 2d 3e 69 6e  _BUSY && pBt->in
162d6 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41  Transaction==TRA
162d7 4e 53 5f 4e 4f 4e 45 20 26 26 0a 20 20 20 20 20  NS_NONE &&.     
162d8 20 20 20 20 20 62 74 72 65 65 49 6e 76 6f 6b 65       btreeInvoke
162d9 42 75 73 79 48 61 6e 64 6c 65 72 28 70 42 74 29  BusyHandler(pBt)
162da 20 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53   );..  if( rc==S
162db 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
162dc 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d  if( p->inTrans==
162dd 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20  TRANS_NONE ){.  
162de 20 20 20 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61      pBt->nTransa
162df 63 74 69 6f 6e 2b 2b 3b 0a 23 69 66 6e 64 65 66  ction++;.#ifndef
162e0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
162e1 52 45 44 5f 43 41 43 48 45 0a 20 20 20 20 20 20  RED_CACHE.      
162e2 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20  if( p->sharable 
162e3 29 7b 0a 09 61 73 73 65 72 74 28 20 70 2d 3e 6c  ){..assert( p->l
162e4 6f 63 6b 2e 70 42 74 72 65 65 3d 3d 70 20 26 26  ock.pBtree==p &&
162e5 20 70 2d 3e 6c 6f 63 6b 2e 69 54 61 62 6c 65 3d   p->lock.iTable=
162e6 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 70 2d  =1 );.        p-
162e7 3e 6c 6f 63 6b 2e 65 4c 6f 63 6b 20 3d 20 52 45  >lock.eLock = RE
162e8 41 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 20  AD_LOCK;.       
162e9 20 70 2d 3e 6c 6f 63 6b 2e 70 4e 65 78 74 20 3d   p->lock.pNext =
162ea 20 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 20   pBt->pLock;.   
162eb 20 20 20 20 20 70 42 74 2d 3e 70 4c 6f 63 6b 20       pBt->pLock 
162ec 3d 20 26 70 2d 3e 6c 6f 63 6b 3b 0a 20 20 20 20  = &p->lock;.    
162ed 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d    }.#endif.    }
162ee 0a 20 20 20 20 70 2d 3e 69 6e 54 72 61 6e 73 20  .    p->inTrans 
162ef 3d 20 28 77 72 66 6c 61 67 3f 54 52 41 4e 53 5f  = (wrflag?TRANS_
162f0 57 52 49 54 45 3a 54 52 41 4e 53 5f 52 45 41 44  WRITE:TRANS_READ
162f1 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 6e  );.    if( p->in
162f2 54 72 61 6e 73 3e 70 42 74 2d 3e 69 6e 54 72 61  Trans>pBt->inTra
162f3 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20  nsaction ){.    
162f4 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63    pBt->inTransac
162f5 74 69 6f 6e 20 3d 20 70 2d 3e 69 6e 54 72 61 6e  tion = p->inTran
162f6 73 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66  s;.    }.#ifndef
162f7 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
162f8 52 45 44 5f 43 41 43 48 45 0a 20 20 20 20 69 66  RED_CACHE.    if
162f9 28 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 20  ( wrflag ){.    
162fa 20 20 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e    assert( !pBt->
162fb 70 57 72 69 74 65 72 20 29 3b 0a 20 20 20 20 20  pWriter );.     
162fc 20 70 42 74 2d 3e 70 57 72 69 74 65 72 20 3d 20   pBt->pWriter = 
162fd 70 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 73  p;.      pBt->is
162fe 45 78 63 6c 75 73 69 76 65 20 3d 20 28 75 38 29  Exclusive = (u8)
162ff 28 77 72 66 6c 61 67 3e 31 29 3b 0a 20 20 20 20  (wrflag>1);.    
16300 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 0a 74  }.#endif.  }...t
16301 72 61 6e 73 5f 62 65 67 75 6e 3a 0a 20 20 69 66  rans_begun:.  if
16302 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
16303 26 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 20 20  && wrflag ){.   
16304 20 2f 2a 20 54 68 69 73 20 63 61 6c 6c 20 6d 61   /* This call ma
16305 6b 65 73 20 73 75 72 65 20 74 68 61 74 20 74 68  kes sure that th
16306 65 20 70 61 67 65 72 20 68 61 73 20 74 68 65 20  e pager has the 
16307 63 6f 72 72 65 63 74 20 6e 75 6d 62 65 72 20 6f  correct number o
16308 66 0a 20 20 20 20 2a 2a 20 6f 70 65 6e 20 73 61  f.    ** open sa
16309 76 65 70 6f 69 6e 74 73 2e 20 49 66 20 74 68 65  vepoints. If the
1630a 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   second paramete
1630b 72 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  r is greater tha
1630c 6e 20 30 20 61 6e 64 0a 20 20 20 20 2a 2a 20 74  n 0 and.    ** t
1630d 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69  he sub-journal i
1630e 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f 70  s not already op
1630f 65 6e 2c 20 74 68 65 6e 20 69 74 20 77 69 6c 6c  en, then it will
16310 20 62 65 20 6f 70 65 6e 65 64 20 68 65 72 65 2e   be opened here.
16311 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d  .    */.    rc =
16312 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65   sqlite3PagerOpe
16313 6e 53 61 76 65 70 6f 69 6e 74 28 70 42 74 2d 3e  nSavepoint(pBt->
16314 70 50 61 67 65 72 2c 20 70 2d 3e 64 62 2d 3e 6e  pPager, p->db->n
16315 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 7d 0a  Savepoint);.  }.
16316 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 74  .  btreeIntegrit
16317 79 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  y(p);.  sqlite3B
16318 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20  treeLeave(p);.  
16319 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69  return rc;.}..#i
1631a 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1631b 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 0a 2f 2a  T_AUTOVACUUM../*
1631c 0a 2a 2a 20 53 65 74 20 74 68 65 20 70 6f 69 6e  .** Set the poin
1631d 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 20  ter-map entries 
1631e 66 6f 72 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e  for all children
1631f 20 6f 66 20 70 61 67 65 20 70 50 61 67 65 2e 20   of page pPage. 
16320 41 6c 73 6f 2c 20 69 66 0a 2a 2a 20 70 50 61 67  Also, if.** pPag
16321 65 20 63 6f 6e 74 61 69 6e 73 20 63 65 6c 6c 73  e contains cells
16322 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f   that point to o
16323 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2c 20 73  verflow pages, s
16324 65 74 20 74 68 65 20 70 6f 69 6e 74 65 72 0a 2a  et the pointer.*
16325 2a 20 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f  * map entries fo
16326 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70  r the overflow p
16327 61 67 65 73 20 61 73 20 77 65 6c 6c 2e 0a 2a 2f  ages as well..*/
16328 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 74 43  .static int setC
16329 68 69 6c 64 50 74 72 6d 61 70 73 28 4d 65 6d 50  hildPtrmaps(MemP
1632a 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 69  age *pPage){.  i
1632b 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
1632c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1632d 20 20 2f 2a 20 43 6f 75 6e 74 65 72 20 76 61 72    /* Counter var
1632e 69 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  iable */.  int n
1632f 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20  Cell;           
16330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
16331 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73   Number of cells
16332 20 69 6e 20 70 61 67 65 20 70 50 61 67 65 20 2a   in page pPage *
16333 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  /.  int rc;     
16334 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16335 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
16336 20 63 6f 64 65 20 2a 2f 0a 20 20 42 74 53 68 61   code */.  BtSha
16337 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65  red *pBt = pPage
16338 2d 3e 70 42 74 3b 0a 20 20 75 38 20 69 73 49 6e  ->pBt;.  u8 isIn
16339 69 74 4f 72 69 67 20 3d 20 70 50 61 67 65 2d 3e  itOrig = pPage->
1633a 69 73 49 6e 69 74 3b 0a 20 20 50 67 6e 6f 20 70  isInit;.  Pgno p
1633b 67 6e 6f 20 3d 20 70 50 61 67 65 2d 3e 70 67 6e  gno = pPage->pgn
1633c 6f 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  o;..  assert( sq
1633d 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1633e 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74  (pPage->pBt->mut
1633f 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 62 74  ex) );.  rc = bt
16340 72 65 65 49 6e 69 74 50 61 67 65 28 70 50 61 67  reeInitPage(pPag
16341 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  e);.  if( rc!=SQ
16342 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67  LITE_OK ){.    g
16343 6f 74 6f 20 73 65 74 5f 63 68 69 6c 64 5f 70 74  oto set_child_pt
16344 72 6d 61 70 73 5f 6f 75 74 3b 0a 20 20 7d 0a 20  rmaps_out;.  }. 
16345 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e   nCell = pPage->
16346 6e 43 65 6c 6c 3b 0a 0a 20 20 66 6f 72 28 69 3d  nCell;..  for(i=
16347 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29  0; i<nCell; i++)
16348 7b 0a 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 20  {.    u8 *pCell 
16349 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65  = findCell(pPage
1634a 2c 20 69 29 3b 0a 0a 20 20 20 20 70 74 72 6d 61  , i);..    ptrma
1634b 70 50 75 74 4f 76 66 6c 50 74 72 28 70 50 61 67  pPutOvflPtr(pPag
1634c 65 2c 20 70 43 65 6c 6c 2c 20 26 72 63 29 3b 0a  e, pCell, &rc);.
1634d 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d  .    if( !pPage-
1634e 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 50  >leaf ){.      P
1634f 67 6e 6f 20 63 68 69 6c 64 50 67 6e 6f 20 3d 20  gno childPgno = 
16350 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c 29 3b  get4byte(pCell);
16351 0a 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 74  .      ptrmapPut
16352 28 70 42 74 2c 20 63 68 69 6c 64 50 67 6e 6f 2c  (pBt, childPgno,
16353 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70   PTRMAP_BTREE, p
16354 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 7d  gno, &rc);.    }
16355 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 61  .  }..  if( !pPa
16356 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20  ge->leaf ){.    
16357 50 67 6e 6f 20 63 68 69 6c 64 50 67 6e 6f 20 3d  Pgno childPgno =
16358 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65   get4byte(&pPage
16359 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68  ->aData[pPage->h
1635a 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20  drOffset+8]);.  
1635b 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c    ptrmapPut(pBt,
1635c 20 63 68 69 6c 64 50 67 6e 6f 2c 20 50 54 52 4d   childPgno, PTRM
1635d 41 50 5f 42 54 52 45 45 2c 20 70 67 6e 6f 2c 20  AP_BTREE, pgno, 
1635e 26 72 63 29 3b 0a 20 20 7d 0a 0a 73 65 74 5f 63  &rc);.  }..set_c
1635f 68 69 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74  hild_ptrmaps_out
16360 3a 0a 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69  :.  pPage->isIni
16361 74 20 3d 20 69 73 49 6e 69 74 4f 72 69 67 3b 0a  t = isInitOrig;.
16362 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
16363 2f 2a 0a 2a 2a 20 53 6f 6d 65 77 68 65 72 65 20  /*.** Somewhere 
16364 6f 6e 20 70 50 61 67 65 20 69 73 20 61 20 70 6f  on pPage is a po
16365 69 6e 74 65 72 20 74 6f 20 70 61 67 65 20 69 46  inter to page iF
16366 72 6f 6d 2e 20 20 4d 6f 64 69 66 79 20 74 68 69  rom.  Modify thi
16367 73 20 70 6f 69 6e 74 65 72 20 73 6f 0a 2a 2a 20  s pointer so.** 
16368 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74  that it points t
16369 6f 20 69 54 6f 2e 20 50 61 72 61 6d 65 74 65 72  o iTo. Parameter
1636a 20 65 54 79 70 65 20 64 65 73 63 72 69 62 65 73   eType describes
1636b 20 74 68 65 20 74 79 70 65 20 6f 66 20 70 6f 69   the type of poi
1636c 6e 74 65 72 20 74 6f 0a 2a 2a 20 62 65 20 6d 6f  nter to.** be mo
1636d 64 69 66 69 65 64 2c 20 61 73 20 20 66 6f 6c 6c  dified, as  foll
1636e 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41  ows:.**.** PTRMA
1636f 50 5f 42 54 52 45 45 3a 20 20 20 20 20 70 50 61  P_BTREE:     pPa
16370 67 65 20 69 73 20 61 20 62 74 72 65 65 2d 70 61  ge is a btree-pa
16371 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20  ge. The pointer 
16372 70 6f 69 6e 74 73 20 61 74 20 61 20 63 68 69 6c  points at a chil
16373 64 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  d .**           
16374 20 20 20 20 20 20 20 20 70 61 67 65 20 6f 66 20          page of 
16375 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52  pPage..**.** PTR
16376 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 3a 20 70  MAP_OVERFLOW1: p
16377 50 61 67 65 20 69 73 20 61 20 62 74 72 65 65 2d  Page is a btree-
16378 70 61 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 65  page. The pointe
16379 72 20 70 6f 69 6e 74 73 20 61 74 20 61 6e 20 6f  r points at an o
1637a 76 65 72 66 6c 6f 77 0a 2a 2a 20 20 20 20 20 20  verflow.**      
1637b 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67               pag
1637c 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  e pointed to by 
1637d 6f 6e 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 73  one of the cells
1637e 20 6f 6e 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a   on pPage..**.**
1637f 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57   PTRMAP_OVERFLOW
16380 32 3a 20 70 50 61 67 65 20 69 73 20 61 6e 20 6f  2: pPage is an o
16381 76 65 72 66 6c 6f 77 2d 70 61 67 65 2e 20 54 68  verflow-page. Th
16382 65 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73  e pointer points
16383 20 61 74 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20   at the next.** 
16384 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16385 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20    overflow page 
16386 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2f 0a  in the list..*/.
16387 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 64 69 66  static int modif
16388 79 50 61 67 65 50 6f 69 6e 74 65 72 28 4d 65 6d  yPagePointer(Mem
16389 50 61 67 65 20 2a 70 50 61 67 65 2c 20 50 67 6e  Page *pPage, Pgn
1638a 6f 20 69 46 72 6f 6d 2c 20 50 67 6e 6f 20 69 54  o iFrom, Pgno iT
1638b 6f 2c 20 75 38 20 65 54 79 70 65 29 7b 0a 20 20  o, u8 eType){.  
1638c 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
1638d 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65  mutex_held(pPage
1638e 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  ->pBt->mutex) );
1638f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
16390 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
16391 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  le(pPage->pDbPag
16392 65 29 20 29 3b 0a 20 20 69 66 28 20 65 54 79 70  e) );.  if( eTyp
16393 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c  e==PTRMAP_OVERFL
16394 4f 57 32 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  OW2 ){.    /* Th
16395 65 20 70 6f 69 6e 74 65 72 20 69 73 20 61 6c 77  e pointer is alw
16396 61 79 73 20 74 68 65 20 66 69 72 73 74 20 34 20  ays the first 4 
16397 62 79 74 65 73 20 6f 66 20 74 68 65 20 70 61 67  bytes of the pag
16398 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 20  e in this case. 
16399 20 2a 2f 0a 20 20 20 20 69 66 28 20 67 65 74 34   */.    if( get4
1639a 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74  byte(pPage->aDat
1639b 61 29 21 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20  a)!=iFrom ){.   
1639c 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1639d 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
1639e 20 20 20 7d 0a 20 20 20 20 70 75 74 34 62 79 74     }.    put4byt
1639f 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20  e(pPage->aData, 
163a0 69 54 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  iTo);.  }else{. 
163a1 20 20 20 75 38 20 69 73 49 6e 69 74 4f 72 69 67     u8 isInitOrig
163a2 20 3d 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74   = pPage->isInit
163a3 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20  ;.    int i;.   
163a4 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 0a 20 20 20   int nCell;..   
163a5 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 70   btreeInitPage(p
163a6 50 61 67 65 29 3b 0a 20 20 20 20 6e 43 65 6c 6c  Page);.    nCell
163a7 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b   = pPage->nCell;
163a8 0a 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ..    for(i=0; i
163a9 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20  <nCell; i++){.  
163aa 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 20 3d 20      u8 *pCell = 
163ab 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20  findCell(pPage, 
163ac 69 29 3b 0a 20 20 20 20 20 20 69 66 28 20 65 54  i);.      if( eT
163ad 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52  ype==PTRMAP_OVER
163ae 46 4c 4f 57 31 20 29 7b 0a 20 20 20 20 20 20 20  FLOW1 ){.       
163af 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a   CellInfo info;.
163b0 20 20 20 20 20 20 20 20 62 74 72 65 65 50 61 72          btreePar
163b1 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c  seCellPtr(pPage,
163b2 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a   pCell, &info);.
163b3 20 20 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f          if( info
163b4 2e 69 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20  .iOverflow ){.  
163b5 20 20 20 20 20 20 20 20 69 66 28 20 69 46 72 6f          if( iFro
163b6 6d 3d 3d 67 65 74 34 62 79 74 65 28 26 70 43 65  m==get4byte(&pCe
163b7 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f  ll[info.iOverflo
163b8 77 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  w]) ){.         
163b9 20 20 20 70 75 74 34 62 79 74 65 28 26 70 43 65     put4byte(&pCe
163ba 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f  ll[info.iOverflo
163bb 77 5d 2c 20 69 54 6f 29 3b 0a 20 20 20 20 20 20  w], iTo);.      
163bc 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
163bd 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
163be 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a   }.      }else{.
163bf 20 20 20 20 20 20 20 20 69 66 28 20 67 65 74 34          if( get4
163c0 62 79 74 65 28 70 43 65 6c 6c 29 3d 3d 69 46 72  byte(pCell)==iFr
163c1 6f 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  om ){.          
163c2 70 75 74 34 62 79 74 65 28 70 43 65 6c 6c 2c 20  put4byte(pCell, 
163c3 69 54 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20  iTo);.          
163c4 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
163c5 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
163c6 20 0a 20 20 20 20 69 66 28 20 69 3d 3d 6e 43 65   .    if( i==nCe
163c7 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ll ){.      if( 
163c8 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f 42 54  eType!=PTRMAP_BT
163c9 52 45 45 20 7c 7c 20 0a 20 20 20 20 20 20 20 20  REE || .        
163ca 20 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67    get4byte(&pPag
163cb 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e  e->aData[pPage->
163cc 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 21 3d 69  hdrOffset+8])!=i
163cd 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20 20 20  From ){.        
163ce 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
163cf 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
163d0 20 20 7d 0a 20 20 20 20 20 20 70 75 74 34 62 79    }.      put4by
163d1 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61  te(&pPage->aData
163d2 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65  [pPage->hdrOffse
163d3 74 2b 38 5d 2c 20 69 54 6f 29 3b 0a 20 20 20 20  t+8], iTo);.    
163d4 7d 0a 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 73  }..    pPage->is
163d5 49 6e 69 74 20 3d 20 69 73 49 6e 69 74 4f 72 69  Init = isInitOri
163d6 67 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  g;.  }.  return 
163d7 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f  SQLITE_OK;.}.../
163d8 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 6f 70  *.** Move the op
163d9 65 6e 20 64 61 74 61 62 61 73 65 20 70 61 67 65  en database page
163da 20 70 44 62 50 61 67 65 20 74 6f 20 6c 6f 63 61   pDbPage to loca
163db 74 69 6f 6e 20 69 46 72 65 65 50 61 67 65 20 69  tion iFreePage i
163dc 6e 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61  n the .** databa
163dd 73 65 2e 20 54 68 65 20 70 44 62 50 61 67 65 20  se. The pDbPage 
163de 72 65 66 65 72 65 6e 63 65 20 72 65 6d 61 69 6e  reference remain
163df 73 20 76 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20 54  s valid..**.** T
163e0 68 65 20 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67  he isCommit flag
163e1 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20   indicates that 
163e2 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64  there is no need
163e3 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 74 68 61   to remember tha
163e4 74 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c  t.** the journal
163e5 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73 79 6e   needs to be syn
163e6 63 28 29 65 64 20 62 65 66 6f 72 65 20 64 61 74  c()ed before dat
163e7 61 62 61 73 65 20 70 61 67 65 20 70 44 62 50 61  abase page pDbPa
163e8 67 65 2d 3e 70 67 6e 6f 20 0a 2a 2a 20 63 61 6e  ge->pgno .** can
163e9 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 2e 20   be written to. 
163ea 54 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20 61  The caller has a
163eb 6c 72 65 61 64 79 20 70 72 6f 6d 69 73 65 64 20  lready promised 
163ec 6e 6f 74 20 74 6f 20 77 72 69 74 65 20 74 6f 20  not to write to 
163ed 74 68 61 74 0a 2a 2a 20 70 61 67 65 2e 0a 2a 2f  that.** page..*/
163ee 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 6c 6f  .static int relo
163ef 63 61 74 65 50 61 67 65 28 0a 20 20 42 74 53 68  catePage(.  BtSh
163f0 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20  ared *pBt,      
163f1 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20 2a 2f       /* Btree */
163f2 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 44 62 50  .  MemPage *pDbP
163f3 61 67 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 4f  age,        /* O
163f4 70 65 6e 20 70 61 67 65 20 74 6f 20 6d 6f 76 65  pen page to move
163f5 20 2a 2f 0a 20 20 75 38 20 65 54 79 70 65 2c 20   */.  u8 eType, 
163f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
163f7 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 27 74  * Pointer map 't
163f8 79 70 65 27 20 65 6e 74 72 79 20 66 6f 72 20 70  ype' entry for p
163f9 44 62 50 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f  DbPage */.  Pgno
163fa 20 69 50 74 72 50 61 67 65 2c 20 20 20 20 20 20   iPtrPage,      
163fb 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
163fc 6d 61 70 20 27 70 61 67 65 2d 6e 6f 27 20 65 6e  map 'page-no' en
163fd 74 72 79 20 66 6f 72 20 70 44 62 50 61 67 65 20  try for pDbPage 
163fe 2a 2f 0a 20 20 50 67 6e 6f 20 69 46 72 65 65 50  */.  Pgno iFreeP
163ff 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  age,          /*
16400 20 54 68 65 20 6c 6f 63 61 74 69 6f 6e 20 74 6f   The location to
16401 20 6d 6f 76 65 20 70 44 62 50 61 67 65 20 74 6f   move pDbPage to
16402 20 2a 2f 0a 20 20 69 6e 74 20 69 73 43 6f 6d 6d   */.  int isComm
16403 69 74 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  it             /
16404 2a 20 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67 20  * isCommit flag 
16405 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65  passed to sqlite
16406 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65 20 2a  3PagerMovepage *
16407 2f 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  /.){.  MemPage *
16408 70 50 74 72 50 61 67 65 3b 20 20 20 2f 2a 20 54  pPtrPage;   /* T
16409 68 65 20 70 61 67 65 20 74 68 61 74 20 63 6f 6e  he page that con
1640a 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  tains a pointer 
1640b 74 6f 20 70 44 62 50 61 67 65 20 2a 2f 0a 20 20  to pDbPage */.  
1640c 50 67 6e 6f 20 69 44 62 50 61 67 65 20 3d 20 70  Pgno iDbPage = p
1640d 44 62 50 61 67 65 2d 3e 70 67 6e 6f 3b 0a 20 20  DbPage->pgno;.  
1640e 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20  Pager *pPager = 
1640f 70 42 74 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69  pBt->pPager;.  i
16410 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74  nt rc;..  assert
16411 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f  ( eType==PTRMAP_
16412 4f 56 45 52 46 4c 4f 57 32 20 7c 7c 20 65 54 79  OVERFLOW2 || eTy
16413 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46  pe==PTRMAP_OVERF
16414 4c 4f 57 31 20 7c 7c 20 0a 20 20 20 20 20 20 65  LOW1 || .      e
16415 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 42 54 52  Type==PTRMAP_BTR
16416 45 45 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52  EE || eType==PTR
16417 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 3b 0a  MAP_ROOTPAGE );.
16418 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
16419 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74  3_mutex_held(pBt
1641a 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
1641b 73 65 72 74 28 20 70 44 62 50 61 67 65 2d 3e 70  sert( pDbPage->p
1641c 42 74 3d 3d 70 42 74 20 29 3b 0a 0a 20 20 2f 2a  Bt==pBt );..  /*
1641d 20 4d 6f 76 65 20 70 61 67 65 20 69 44 62 50 61   Move page iDbPa
1641e 67 65 20 66 72 6f 6d 20 69 74 73 20 63 75 72 72  ge from its curr
1641f 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 20 74 6f 20  ent location to 
16420 70 61 67 65 20 6e 75 6d 62 65 72 20 69 46 72 65  page number iFre
16421 65 50 61 67 65 20 2a 2f 0a 20 20 54 52 41 43 45  ePage */.  TRACE
16422 28 28 22 41 55 54 4f 56 41 43 55 55 4d 3a 20 4d  (("AUTOVACUUM: M
16423 6f 76 69 6e 67 20 25 64 20 74 6f 20 66 72 65 65  oving %d to free
16424 20 70 61 67 65 20 25 64 20 28 70 74 72 20 70 61   page %d (ptr pa
16425 67 65 20 25 64 20 74 79 70 65 20 25 64 29 5c 6e  ge %d type %d)\n
16426 22 2c 20 0a 20 20 20 20 20 20 69 44 62 50 61 67  ", .      iDbPag
16427 65 2c 20 69 46 72 65 65 50 61 67 65 2c 20 69 50  e, iFreePage, iP
16428 74 72 50 61 67 65 2c 20 65 54 79 70 65 29 29 3b  trPage, eType));
16429 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50  .  rc = sqlite3P
1642a 61 67 65 72 4d 6f 76 65 70 61 67 65 28 70 50 61  agerMovepage(pPa
1642b 67 65 72 2c 20 70 44 62 50 61 67 65 2d 3e 70 44  ger, pDbPage->pD
1642c 62 50 61 67 65 2c 20 69 46 72 65 65 50 61 67 65  bPage, iFreePage
1642d 2c 20 69 73 43 6f 6d 6d 69 74 29 3b 0a 20 20 69  , isCommit);.  i
1642e 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1642f 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   ){.    return r
16430 63 3b 0a 20 20 7d 0a 20 20 70 44 62 50 61 67 65  c;.  }.  pDbPage
16431 2d 3e 70 67 6e 6f 20 3d 20 69 46 72 65 65 50 61  ->pgno = iFreePa
16432 67 65 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 44 62  ge;..  /* If pDb
16433 50 61 67 65 20 77 61 73 20 61 20 62 74 72 65 65  Page was a btree
16434 2d 70 61 67 65 2c 20 74 68 65 6e 20 69 74 20 6d  -page, then it m
16435 61 79 20 68 61 76 65 20 63 68 69 6c 64 20 70 61  ay have child pa
16436 67 65 73 20 61 6e 64 2f 6f 72 20 63 65 6c 6c 73  ges and/or cells
16437 0a 20 20 2a 2a 20 74 68 61 74 20 70 6f 69 6e 74  .  ** that point
16438 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   to overflow pag
16439 65 73 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20  es. The pointer 
1643a 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f 72 20  map entries for 
1643b 61 6c 6c 20 74 68 65 73 65 0a 20 20 2a 2a 20 70  all these.  ** p
1643c 61 67 65 73 20 6e 65 65 64 20 74 6f 20 62 65 20  ages need to be 
1643d 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2a 0a 20 20  changed..  **.  
1643e 2a 2a 20 49 66 20 70 44 62 50 61 67 65 20 69 73  ** If pDbPage is
1643f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   an overflow pag
16440 65 2c 20 74 68 65 6e 20 74 68 65 20 66 69 72 73  e, then the firs
16441 74 20 34 20 62 79 74 65 73 20 6d 61 79 20 73 74  t 4 bytes may st
16442 6f 72 65 20 61 0a 20 20 2a 2a 20 70 6f 69 6e 74  ore a.  ** point
16443 65 72 20 74 6f 20 61 20 73 75 62 73 65 71 75 65  er to a subseque
16444 6e 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  nt overflow page
16445 2e 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65  . If this is the
16446 20 63 61 73 65 2c 20 74 68 65 6e 0a 20 20 2a 2a   case, then.  **
16447 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70   the pointer map
16448 20 6e 65 65 64 73 20 74 6f 20 62 65 20 75 70 64   needs to be upd
16449 61 74 65 64 20 66 6f 72 20 74 68 65 20 73 75 62  ated for the sub
1644a 73 65 71 75 65 6e 74 20 6f 76 65 72 66 6c 6f 77  sequent overflow
1644b 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66   page..  */.  if
1644c 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f  ( eType==PTRMAP_
1644d 42 54 52 45 45 20 7c 7c 20 65 54 79 70 65 3d 3d  BTREE || eType==
1644e 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20  PTRMAP_ROOTPAGE 
1644f 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 65 74 43  ){.    rc = setC
16450 68 69 6c 64 50 74 72 6d 61 70 73 28 70 44 62 50  hildPtrmaps(pDbP
16451 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63  age);.    if( rc
16452 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
16453 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
16454 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
16455 20 20 20 50 67 6e 6f 20 6e 65 78 74 4f 76 66 6c     Pgno nextOvfl
16456 20 3d 20 67 65 74 34 62 79 74 65 28 70 44 62 50   = get4byte(pDbP
16457 61 67 65 2d 3e 61 44 61 74 61 29 3b 0a 20 20 20  age->aData);.   
16458 20 69 66 28 20 6e 65 78 74 4f 76 66 6c 21 3d 30   if( nextOvfl!=0
16459 20 29 7b 0a 20 20 20 20 20 20 70 74 72 6d 61 70   ){.      ptrmap
1645a 50 75 74 28 70 42 74 2c 20 6e 65 78 74 4f 76 66  Put(pBt, nextOvf
1645b 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c  l, PTRMAP_OVERFL
1645c 4f 57 32 2c 20 69 46 72 65 65 50 61 67 65 2c 20  OW2, iFreePage, 
1645d 26 72 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20  &rc);.      if( 
1645e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1645f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
16460 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rc;.      }.    
16461 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 78 20  }.  }..  /* Fix 
16462 74 68 65 20 64 61 74 61 62 61 73 65 20 70 6f 69  the database poi
16463 6e 74 65 72 20 6f 6e 20 70 61 67 65 20 69 50 74  nter on page iPt
16464 72 50 61 67 65 20 74 68 61 74 20 70 6f 69 6e 74  rPage that point
16465 65 64 20 61 74 20 69 44 62 50 61 67 65 20 73 6f  ed at iDbPage so
16466 0a 20 20 2a 2a 20 74 68 61 74 20 69 74 20 70 6f  .  ** that it po
16467 69 6e 74 73 20 61 74 20 69 46 72 65 65 50 61 67  ints at iFreePag
16468 65 2e 20 41 6c 73 6f 20 66 69 78 20 74 68 65 20  e. Also fix the 
16469 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72  pointer map entr
1646a 79 20 66 6f 72 0a 20 20 2a 2a 20 69 50 74 72 50  y for.  ** iPtrP
1646b 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  age..  */.  if( 
1646c 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f 52 4f  eType!=PTRMAP_RO
1646d 4f 54 50 41 47 45 20 29 7b 0a 20 20 20 20 72 63  OTPAGE ){.    rc
1646e 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28   = btreeGetPage(
1646f 70 42 74 2c 20 69 50 74 72 50 61 67 65 2c 20 26  pBt, iPtrPage, &
16470 70 50 74 72 50 61 67 65 2c 20 30 29 3b 0a 20 20  pPtrPage, 0);.  
16471 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
16472 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74  _OK ){.      ret
16473 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
16474 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
16475 67 65 72 57 72 69 74 65 28 70 50 74 72 50 61 67  gerWrite(pPtrPag
16476 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  e->pDbPage);.   
16477 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
16478 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65  OK ){.      rele
16479 61 73 65 50 61 67 65 28 70 50 74 72 50 61 67 65  asePage(pPtrPage
1647a 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
1647b 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63  rc;.    }.    rc
1647c 20 3d 20 6d 6f 64 69 66 79 50 61 67 65 50 6f 69   = modifyPagePoi
1647d 6e 74 65 72 28 70 50 74 72 50 61 67 65 2c 20 69  nter(pPtrPage, i
1647e 44 62 50 61 67 65 2c 20 69 46 72 65 65 50 61 67  DbPage, iFreePag
1647f 65 2c 20 65 54 79 70 65 29 3b 0a 20 20 20 20 72  e, eType);.    r
16480 65 6c 65 61 73 65 50 61 67 65 28 70 50 74 72 50  eleasePage(pPtrP
16481 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63  age);.    if( rc
16482 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
16483 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70       ptrmapPut(p
16484 42 74 2c 20 69 46 72 65 65 50 61 67 65 2c 20 65  Bt, iFreePage, e
16485 54 79 70 65 2c 20 69 50 74 72 50 61 67 65 2c 20  Type, iPtrPage, 
16486 26 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  &rc);.    }.  }.
16487 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
16488 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61  /* Forward decla
16489 72 61 74 69 6f 6e 20 72 65 71 75 69 72 65 64 20  ration required 
1648a 62 79 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65  by incrVacuumSte
1648b 70 28 29 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69  p(). */.static i
1648c 6e 74 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65  nt allocateBtree
1648d 50 61 67 65 28 42 74 53 68 61 72 65 64 20 2a 2c  Page(BtShared *,
1648e 20 4d 65 6d 50 61 67 65 20 2a 2a 2c 20 50 67 6e   MemPage **, Pgn
1648f 6f 20 2a 2c 20 50 67 6e 6f 2c 20 75 38 29 3b 0a  o *, Pgno, u8);.
16490 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61  ./*.** Perform a
16491 20 73 69 6e 67 6c 65 20 73 74 65 70 20 6f 66 20   single step of 
16492 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 2d 76  an incremental-v
16493 61 63 75 75 6d 2e 20 49 66 20 73 75 63 63 65 73  acuum. If succes
16494 73 66 75 6c 2c 0a 2a 2a 20 72 65 74 75 72 6e 20  sful,.** return 
16495 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 74 68  SQLITE_OK. If th
16496 65 72 65 20 69 73 20 6e 6f 20 77 6f 72 6b 20 74  ere is no work t
16497 6f 20 64 6f 20 28 61 6e 64 20 74 68 65 72 65 66  o do (and theref
16498 6f 72 65 20 6e 6f 0a 2a 2a 20 70 6f 69 6e 74 20  ore no.** point 
16499 69 6e 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20  in calling this 
1649a 66 75 6e 63 74 69 6f 6e 20 61 67 61 69 6e 29 2c  function again),
1649b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44   return SQLITE_D
1649c 4f 4e 45 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 72 65 20  ONE..**.** More 
1649d 73 70 65 63 69 66 69 63 6c 79 2c 20 74 68 69 73  specificly, this
1649e 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70   function attemp
1649f 74 73 20 74 6f 20 72 65 2d 6f 72 67 61 6e 69 7a  ts to re-organiz
164a0 65 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61  e the .** databa
164a1 73 65 20 73 6f 20 74 68 61 74 20 74 68 65 20 6c  se so that the l
164a2 61 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20  ast page of the 
164a3 66 69 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 69  file currently i
164a4 6e 20 75 73 65 0a 2a 2a 20 69 73 20 6e 6f 20 6c  n use.** is no l
164a5 6f 6e 67 65 72 20 69 6e 20 75 73 65 2e 0a 2a 2a  onger in use..**
164a6 0a 2a 2a 20 49 66 20 74 68 65 20 6e 46 69 6e 20  .** If the nFin 
164a7 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e  parameter is non
164a8 2d 7a 65 72 6f 2c 20 74 68 69 73 20 66 75 6e 63  -zero, this func
164a9 74 69 6f 6e 20 61 73 73 75 6d 65 73 0a 2a 2a 20  tion assumes.** 
164aa 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65 72 20  that the caller 
164ab 77 69 6c 6c 20 6b 65 65 70 20 63 61 6c 6c 69 6e  will keep callin
164ac 67 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70  g incrVacuumStep
164ad 28 29 20 75 6e 74 69 6c 0a 2a 2a 20 69 74 20 72  () until.** it r
164ae 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 44 4f  eturns SQLITE_DO
164af 4e 45 20 6f 72 20 61 6e 20 65 72 72 6f 72 2c 20  NE or an error, 
164b0 61 6e 64 20 74 68 61 74 20 6e 46 69 6e 20 69 73  and that nFin is
164b1 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f   the.** number o
164b2 66 20 70 61 67 65 73 20 74 68 65 20 64 61 74 61  f pages the data
164b3 62 61 73 65 20 66 69 6c 65 20 77 69 6c 6c 20 63  base file will c
164b4 6f 6e 74 61 69 6e 20 61 66 74 65 72 20 74 68 69  ontain after thi
164b5 73 20 0a 2a 2a 20 70 72 6f 63 65 73 73 20 69 73  s .** process is
164b6 20 63 6f 6d 70 6c 65 74 65 2e 20 20 49 66 20 6e   complete.  If n
164b7 46 69 6e 20 69 73 20 7a 65 72 6f 2c 20 69 74 20  Fin is zero, it 
164b8 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 0a  is assumed that.
164b9 2a 2a 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65  ** incrVacuumSte
164ba 70 28 29 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c  p() will be call
164bb 65 64 20 61 20 66 69 6e 69 74 65 20 61 6d 6f 75  ed a finite amou
164bc 6e 74 20 6f 66 20 74 69 6d 65 73 0a 2a 2a 20 77  nt of times.** w
164bd 68 69 63 68 20 6d 61 79 20 6f 72 20 6d 61 79 20  hich may or may 
164be 6e 6f 74 20 65 6d 70 74 79 20 74 68 65 20 66 72  not empty the fr
164bf 65 65 6c 69 73 74 2e 20 20 41 20 66 75 6c 6c 20  eelist.  A full 
164c0 61 75 74 6f 76 61 63 75 75 6d 0a 2a 2a 20 68 61  autovacuum.** ha
164c1 73 20 6e 46 69 6e 3e 30 2e 20 20 41 20 22 50 52  s nFin>0.  A "PR
164c2 41 47 4d 41 20 69 6e 63 72 65 6d 65 6e 74 61 6c  AGMA incremental
164c3 5f 76 61 63 75 75 6d 22 20 68 61 73 20 6e 46 69  _vacuum" has nFi
164c4 6e 3d 3d 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  n==0..*/.static 
164c5 69 6e 74 20 69 6e 63 72 56 61 63 75 75 6d 53 74  int incrVacuumSt
164c6 65 70 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  ep(BtShared *pBt
164c7 2c 20 50 67 6e 6f 20 6e 46 69 6e 2c 20 50 67 6e  , Pgno nFin, Pgn
164c8 6f 20 69 4c 61 73 74 50 67 29 7b 0a 20 20 50 67  o iLastPg){.  Pg
164c9 6e 6f 20 6e 46 72 65 65 4c 69 73 74 3b 20 20 20  no nFreeList;   
164ca 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
164cb 72 20 6f 66 20 70 61 67 65 73 20 73 74 69 6c 6c  r of pages still
164cc 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73   on the free-lis
164cd 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  t */..  assert( 
164ce 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
164cf 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  ld(pBt->mutex) )
164d0 3b 0a 20 20 61 73 73 65 72 74 28 20 69 4c 61 73  ;.  assert( iLas
164d1 74 50 67 3e 6e 46 69 6e 20 29 3b 0a 0a 20 20 69  tPg>nFin );..  i
164d2 66 28 20 21 50 54 52 4d 41 50 5f 49 53 50 41 47  f( !PTRMAP_ISPAG
164d3 45 28 70 42 74 2c 20 69 4c 61 73 74 50 67 29 20  E(pBt, iLastPg) 
164d4 26 26 20 69 4c 61 73 74 50 67 21 3d 50 45 4e 44  && iLastPg!=PEND
164d5 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
164d6 74 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63  t) ){.    int rc
164d7 3b 0a 20 20 20 20 75 38 20 65 54 79 70 65 3b 0a  ;.    u8 eType;.
164d8 20 20 20 20 50 67 6e 6f 20 69 50 74 72 50 61 67      Pgno iPtrPag
164d9 65 3b 0a 0a 20 20 20 20 6e 46 72 65 65 4c 69 73  e;..    nFreeLis
164da 74 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 42  t = get4byte(&pB
164db 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61  t->pPage1->aData
164dc 5b 33 36 5d 29 3b 0a 20 20 20 20 69 66 28 20 6e  [36]);.    if( n
164dd 46 72 65 65 4c 69 73 74 3d 3d 30 20 29 7b 0a 20  FreeList==0 ){. 
164de 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
164df 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 0a  TE_DONE;.    }..
164e0 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47      rc = ptrmapG
164e1 65 74 28 70 42 74 2c 20 69 4c 61 73 74 50 67 2c  et(pBt, iLastPg,
164e2 20 26 65 54 79 70 65 2c 20 26 69 50 74 72 50 61   &eType, &iPtrPa
164e3 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  ge);.    if( rc!
164e4 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
164e5 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
164e6 20 20 20 7d 0a 20 20 20 20 69 66 28 20 65 54 79     }.    if( eTy
164e7 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50  pe==PTRMAP_ROOTP
164e8 41 47 45 20 29 7b 0a 20 20 20 20 20 20 72 65 74  AGE ){.      ret
164e9 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
164ea 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 0a  PT_BKPT;.    }..
164eb 20 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50      if( eType==P
164ec 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29  TRMAP_FREEPAGE )
164ed 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 46 69 6e  {.      if( nFin
164ee 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ==0 ){.        /
164ef 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 70 61 67  * Remove the pag
164f0 65 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 73  e from the files
164f1 20 66 72 65 65 2d 6c 69 73 74 2e 20 54 68 69 73   free-list. This
164f2 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64   is not required
164f3 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 66 20 6e  .        ** if n
164f4 46 69 6e 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e  Fin is non-zero.
164f5 20 49 6e 20 74 68 61 74 20 63 61 73 65 2c 20 74   In that case, t
164f6 68 65 20 66 72 65 65 2d 6c 69 73 74 20 77 69 6c  he free-list wil
164f7 6c 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  l be.        ** 
164f8 74 72 75 6e 63 61 74 65 64 20 74 6f 20 7a 65 72  truncated to zer
164f9 6f 20 61 66 74 65 72 20 74 68 69 73 20 66 75 6e  o after this fun
164fa 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2c 20 73  ction returns, s
164fb 6f 20 69 74 20 64 6f 65 73 6e 27 74 20 0a 20 20  o it doesn't .  
164fc 20 20 20 20 20 20 2a 2a 20 6d 61 74 74 65 72 20        ** matter 
164fd 69 66 20 69 74 20 73 74 69 6c 6c 20 63 6f 6e 74  if it still cont
164fe 61 69 6e 73 20 73 6f 6d 65 20 67 61 72 62 61 67  ains some garbag
164ff 65 20 65 6e 74 72 69 65 73 2e 0a 20 20 20 20 20  e entries..     
16500 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 50 67     */.        Pg
16501 6e 6f 20 69 46 72 65 65 50 67 3b 0a 20 20 20 20  no iFreePg;.    
16502 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 46 72      MemPage *pFr
16503 65 65 50 67 3b 0a 20 20 20 20 20 20 20 20 72 63  eePg;.        rc
16504 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65   = allocateBtree
16505 50 61 67 65 28 70 42 74 2c 20 26 70 46 72 65 65  Page(pBt, &pFree
16506 50 67 2c 20 26 69 46 72 65 65 50 67 2c 20 69 4c  Pg, &iFreePg, iL
16507 61 73 74 50 67 2c 20 31 29 3b 0a 20 20 20 20 20  astPg, 1);.     
16508 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
16509 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
1650a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
1650b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61       }.        a
1650c 73 73 65 72 74 28 20 69 46 72 65 65 50 67 3d 3d  ssert( iFreePg==
1650d 69 4c 61 73 74 50 67 20 29 3b 0a 20 20 20 20 20  iLastPg );.     
1650e 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
1650f 46 72 65 65 50 67 29 3b 0a 20 20 20 20 20 20 7d  FreePg);.      }
16510 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20  .    } else {.  
16511 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65 50 67      Pgno iFreePg
16512 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
16513 20 49 6e 64 65 78 20 6f 66 20 66 72 65 65 20 70   Index of free p
16514 61 67 65 20 74 6f 20 6d 6f 76 65 20 70 4c 61 73  age to move pLas
16515 74 50 67 20 74 6f 20 2a 2f 0a 20 20 20 20 20 20  tPg to */.      
16516 4d 65 6d 50 61 67 65 20 2a 70 4c 61 73 74 50 67  MemPage *pLastPg
16517 3b 0a 0a 20 20 20 20 20 20 72 63 20 3d 20 62 74  ;..      rc = bt
16518 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20  reeGetPage(pBt, 
16519 69 4c 61 73 74 50 67 2c 20 26 70 4c 61 73 74 50  iLastPg, &pLastP
1651a 67 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28  g, 0);.      if(
1651b 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1651c 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
1651d 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20   rc;.      }..  
1651e 20 20 20 20 2f 2a 20 49 66 20 6e 46 69 6e 20 69      /* If nFin i
1651f 73 20 7a 65 72 6f 2c 20 74 68 69 73 20 6c 6f 6f  s zero, this loo
16520 70 20 72 75 6e 73 20 65 78 61 63 74 6c 79 20 6f  p runs exactly o
16521 6e 63 65 20 61 6e 64 20 70 61 67 65 20 70 4c 61  nce and page pLa
16522 73 74 50 67 0a 20 20 20 20 20 20 2a 2a 20 69 73  stPg.      ** is
16523 20 73 77 61 70 70 65 64 20 77 69 74 68 20 74 68   swapped with th
16524 65 20 66 69 72 73 74 20 66 72 65 65 20 70 61 67  e first free pag
16525 65 20 70 75 6c 6c 65 64 20 6f 66 66 20 74 68 65  e pulled off the
16526 20 66 72 65 65 20 6c 69 73 74 2e 0a 20 20 20 20   free list..    
16527 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4f 6e    **.      ** On
16528 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c   the other hand,
16529 20 69 66 20 6e 46 69 6e 20 69 73 20 67 72 65 61   if nFin is grea
1652a 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74  ter than zero, t
1652b 68 65 6e 20 6b 65 65 70 0a 20 20 20 20 20 20 2a  hen keep.      *
1652c 2a 20 6c 6f 6f 70 69 6e 67 20 75 6e 74 69 6c 20  * looping until 
1652d 61 20 66 72 65 65 2d 70 61 67 65 20 6c 6f 63 61  a free-page loca
1652e 74 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 66  ted within the f
1652f 69 72 73 74 20 6e 46 69 6e 20 70 61 67 65 73 0a  irst nFin pages.
16530 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20        ** of the 
16531 66 69 6c 65 20 69 73 20 66 6f 75 6e 64 2e 0a 20  file is found.. 
16532 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 6f       */.      do
16533 20 7b 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61   {.        MemPa
16534 67 65 20 2a 70 46 72 65 65 50 67 3b 0a 20 20 20  ge *pFreePg;.   
16535 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61       rc = alloca
16536 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c  teBtreePage(pBt,
16537 20 26 70 46 72 65 65 50 67 2c 20 26 69 46 72 65   &pFreePg, &iFre
16538 65 50 67 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  ePg, 0, 0);.    
16539 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1653a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1653b 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
1653c 4c 61 73 74 50 67 29 3b 0a 20 20 20 20 20 20 20  LastPg);.       
1653d 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
1653e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1653f 72 65 6c 65 61 73 65 50 61 67 65 28 70 46 72 65  releasePage(pFre
16540 65 50 67 29 3b 0a 20 20 20 20 20 20 7d 77 68 69  ePg);.      }whi
16541 6c 65 28 20 6e 46 69 6e 21 3d 30 20 26 26 20 69  le( nFin!=0 && i
16542 46 72 65 65 50 67 3e 6e 46 69 6e 20 29 3b 0a 20  FreePg>nFin );. 
16543 20 20 20 20 20 61 73 73 65 72 74 28 20 69 46 72       assert( iFr
16544 65 65 50 67 3c 69 4c 61 73 74 50 67 20 29 3b 0a  eePg<iLastPg );.
16545 20 20 20 20 20 20 0a 20 20 20 20 20 20 72 63 20        .      rc 
16546 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
16547 69 74 65 28 70 4c 61 73 74 50 67 2d 3e 70 44 62  ite(pLastPg->pDb
16548 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28  Page);.      if(
16549 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1654a 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72  {.        rc = r
1654b 65 6c 6f 63 61 74 65 50 61 67 65 28 70 42 74 2c  elocatePage(pBt,
1654c 20 70 4c 61 73 74 50 67 2c 20 65 54 79 70 65 2c   pLastPg, eType,
1654d 20 69 50 74 72 50 61 67 65 2c 20 69 46 72 65 65   iPtrPage, iFree
1654e 50 67 2c 20 6e 46 69 6e 21 3d 30 29 3b 0a 20 20  Pg, nFin!=0);.  
1654f 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 6c 65      }.      rele
16550 61 73 65 50 61 67 65 28 70 4c 61 73 74 50 67 29  asePage(pLastPg)
16551 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
16552 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
16553 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
16554 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
16555 7d 0a 0a 20 20 69 66 28 20 6e 46 69 6e 3d 3d 30  }..  if( nFin==0
16556 20 29 7b 0a 20 20 20 20 69 4c 61 73 74 50 67 2d   ){.    iLastPg-
16557 2d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 69 4c  -;.    while( iL
16558 61 73 74 50 67 3d 3d 50 45 4e 44 49 4e 47 5f 42  astPg==PENDING_B
16559 59 54 45 5f 50 41 47 45 28 70 42 74 29 7c 7c 50  YTE_PAGE(pBt)||P
1655a 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74  TRMAP_ISPAGE(pBt
1655b 2c 20 69 4c 61 73 74 50 67 29 20 29 7b 0a 20 20  , iLastPg) ){.  
1655c 20 20 20 20 69 66 28 20 50 54 52 4d 41 50 5f 49      if( PTRMAP_I
1655d 53 50 41 47 45 28 70 42 74 2c 20 69 4c 61 73 74  SPAGE(pBt, iLast
1655e 50 67 29 20 29 7b 0a 20 20 20 20 20 20 20 20 4d  Pg) ){.        M
1655f 65 6d 50 61 67 65 20 2a 70 50 67 3b 0a 20 20 20  emPage *pPg;.   
16560 20 20 20 20 20 69 6e 74 20 72 63 20 3d 20 62 74       int rc = bt
16561 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20  reeGetPage(pBt, 
16562 69 4c 61 73 74 50 67 2c 20 26 70 50 67 2c 20 30  iLastPg, &pPg, 0
16563 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
16564 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
16565 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
16566 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20   rc;.        }. 
16567 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
16568 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50  te3PagerWrite(pP
16569 67 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  g->pDbPage);.   
1656a 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
1656b 28 70 50 67 29 3b 0a 20 20 20 20 20 20 20 20 69  (pPg);.        i
1656c 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1656d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65   ){.          re
1656e 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20  turn rc;.       
1656f 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
16570 20 69 4c 61 73 74 50 67 2d 2d 3b 0a 20 20 20 20   iLastPg--;.    
16571 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67  }.    sqlite3Pag
16572 65 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28  erTruncateImage(
16573 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69 4c 61  pBt->pPager, iLa
16574 73 74 50 67 29 3b 0a 20 20 7d 0a 20 20 72 65 74  stPg);.  }.  ret
16575 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
16576 0a 0a 2f 2a 0a 2a 2a 20 41 20 77 72 69 74 65 2d  ../*.** A write-
16577 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74  transaction must
16578 20 62 65 20 6f 70 65 6e 65 64 20 62 65 66 6f 72   be opened befor
16579 65 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 66  e calling this f
1657a 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 49 74 20 70  unction..** It p
1657b 65 72 66 6f 72 6d 73 20 61 20 73 69 6e 67 6c 65  erforms a single
1657c 20 75 6e 69 74 20 6f 66 20 77 6f 72 6b 20 74 6f   unit of work to
1657d 77 61 72 64 73 20 61 6e 20 69 6e 63 72 65 6d 65  wards an increme
1657e 6e 74 61 6c 20 76 61 63 75 75 6d 2e 0a 2a 2a 0a  ntal vacuum..**.
1657f 2a 2a 20 49 66 20 74 68 65 20 69 6e 63 72 65 6d  ** If the increm
16580 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 69 73 20  ental vacuum is 
16581 66 69 6e 69 73 68 65 64 20 61 66 74 65 72 20 74  finished after t
16582 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 68 61 73  his function has
16583 20 72 75 6e 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f   run,.** SQLITE_
16584 44 4f 4e 45 20 69 73 20 72 65 74 75 72 6e 65 64  DONE is returned
16585 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20 66  . If it is not f
16586 69 6e 69 73 68 65 64 2c 20 62 75 74 20 6e 6f 20  inished, but no 
16587 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 2c 0a  error occurred,.
16588 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  ** SQLITE_OK is 
16589 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77  returned. Otherw
1658a 69 73 65 20 61 6e 20 53 51 4c 69 74 65 20 65 72  ise an SQLite er
1658b 72 6f 72 20 63 6f 64 65 2e 20 0a 2a 2f 0a 53 51  ror code. .*/.SQ
1658c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1658d 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 63   sqlite3BtreeInc
1658e 72 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a 70  rVacuum(Btree *p
1658f 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42  ){.  int rc;.  B
16590 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
16591 2d 3e 70 42 74 3b 0a 0a 20 20 73 71 6c 69 74 65  ->pBt;..  sqlite
16592 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a  3BtreeEnter(p);.
16593 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69    assert( pBt->i
16594 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52  nTransaction==TR
16595 41 4e 53 5f 57 52 49 54 45 20 26 26 20 70 2d 3e  ANS_WRITE && p->
16596 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57  inTrans==TRANS_W
16597 52 49 54 45 20 29 3b 0a 20 20 69 66 28 20 21 70  RITE );.  if( !p
16598 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29  Bt->autoVacuum )
16599 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
1659a 45 5f 44 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65 7b  E_DONE;.  }else{
1659b 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 41  .    invalidateA
1659c 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28  llOverflowCache(
1659d 70 42 74 29 3b 0a 20 20 20 20 72 63 20 3d 20 69  pBt);.    rc = i
1659e 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28 70 42  ncrVacuumStep(pB
1659f 74 2c 20 30 2c 20 70 61 67 65 72 50 61 67 65 63  t, 0, pagerPagec
165a0 6f 75 6e 74 28 70 42 74 29 29 3b 0a 20 20 7d 0a  ount(pBt));.  }.
165a1 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
165a2 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
165a3 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   rc;.}../*.** Th
165a4 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
165a5 6c 6c 65 64 20 70 72 69 6f 72 20 74 6f 20 73 71  lled prior to sq
165a6 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74  lite3PagerCommit
165a7 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61 63 74   when a transact
165a8 69 6f 6e 0a 2a 2a 20 69 73 20 63 6f 6d 6d 69 74  ion.** is commit
165a9 65 64 20 66 6f 72 20 61 6e 20 61 75 74 6f 2d 76  ed for an auto-v
165aa 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 2e 0a  acuum database..
165ab 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f  **.** If SQLITE_
165ac 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20  OK is returned, 
165ad 74 68 65 6e 20 2a 70 6e 54 72 75 6e 63 20 69 73  then *pnTrunc is
165ae 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62   set to the numb
165af 65 72 20 6f 66 20 70 61 67 65 73 0a 2a 2a 20 74  er of pages.** t
165b0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
165b1 20 73 68 6f 75 6c 64 20 62 65 20 74 72 75 6e 63   should be trunc
165b2 61 74 65 64 20 74 6f 20 64 75 72 69 6e 67 20 74  ated to during t
165b3 68 65 20 63 6f 6d 6d 69 74 20 70 72 6f 63 65 73  he commit proces
165b4 73 2e 20 0a 2a 2a 20 69 2e 65 2e 20 74 68 65 20  s. .** i.e. the 
165b5 64 61 74 61 62 61 73 65 20 68 61 73 20 62 65 65  database has bee
165b6 6e 20 72 65 6f 72 67 61 6e 69 7a 65 64 20 73 6f  n reorganized so
165b7 20 74 68 61 74 20 6f 6e 6c 79 20 74 68 65 20 66   that only the f
165b8 69 72 73 74 20 2a 70 6e 54 72 75 6e 63 0a 2a 2a  irst *pnTrunc.**
165b9 20 70 61 67 65 73 20 61 72 65 20 69 6e 20 75 73   pages are in us
165ba 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
165bb 20 61 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d 69   autoVacuumCommi
165bc 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29  t(BtShared *pBt)
165bd 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
165be 49 54 45 5f 4f 4b 3b 0a 20 20 50 61 67 65 72 20  ITE_OK;.  Pager 
165bf 2a 70 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70  *pPager = pBt->p
165c0 50 61 67 65 72 3b 0a 20 20 56 56 41 5f 4f 4e 4c  Pager;.  VVA_ONL
165c1 59 28 20 69 6e 74 20 6e 52 65 66 20 3d 20 73 71  Y( int nRef = sq
165c2 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75  lite3PagerRefcou
165c3 6e 74 28 70 50 61 67 65 72 29 20 29 3b 0a 0a 20  nt(pPager) );.. 
165c4 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
165c5 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d  _mutex_held(pBt-
165c6 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 6e 76  >mutex) );.  inv
165c7 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c  alidateAllOverfl
165c8 6f 77 43 61 63 68 65 28 70 42 74 29 3b 0a 20 20  owCache(pBt);.  
165c9 61 73 73 65 72 74 28 70 42 74 2d 3e 61 75 74 6f  assert(pBt->auto
165ca 56 61 63 75 75 6d 29 3b 0a 20 20 69 66 28 20 21  Vacuum);.  if( !
165cb 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20  pBt->incrVacuum 
165cc 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 46 69 6e  ){.    Pgno nFin
165cd 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  ;         /* Num
165ce 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
165cf 64 61 74 61 62 61 73 65 20 61 66 74 65 72 20 61  database after a
165d0 75 74 6f 76 61 63 75 75 6d 69 6e 67 20 2a 2f 0a  utovacuuming */.
165d1 20 20 20 20 50 67 6e 6f 20 6e 46 72 65 65 3b 20      Pgno nFree; 
165d2 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
165d3 20 6f 66 20 70 61 67 65 73 20 6f 6e 20 74 68 65   of pages on the
165d4 20 66 72 65 65 6c 69 73 74 20 69 6e 69 74 69 61   freelist initia
165d5 6c 6c 79 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20  lly */.    Pgno 
165d6 6e 50 74 72 6d 61 70 3b 20 20 20 20 20 20 2f 2a  nPtrmap;      /*
165d7 20 4e 75 6d 62 65 72 20 6f 66 20 50 74 72 4d 61   Number of PtrMa
165d8 70 20 70 61 67 65 73 20 74 6f 20 62 65 20 66 72  p pages to be fr
165d9 65 65 64 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20  eed */.    Pgno 
165da 69 46 72 65 65 3b 20 20 20 20 20 20 20 20 2f 2a  iFree;        /*
165db 20 54 68 65 20 6e 65 78 74 20 70 61 67 65 20 74   The next page t
165dc 6f 20 62 65 20 66 72 65 65 64 20 2a 2f 0a 20 20  o be freed */.  
165dd 20 20 69 6e 74 20 6e 45 6e 74 72 79 3b 20 20 20    int nEntry;   
165de 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
165df 66 20 65 6e 74 72 69 65 73 20 6f 6e 20 6f 6e 65  f entries on one
165e0 20 70 74 72 6d 61 70 20 70 61 67 65 20 2a 2f 0a   ptrmap page */.
165e1 20 20 20 20 50 67 6e 6f 20 6e 4f 72 69 67 3b 20      Pgno nOrig; 
165e2 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
165e3 73 65 20 73 69 7a 65 20 62 65 66 6f 72 65 20 66  se size before f
165e4 72 65 65 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 6e  reeing */..    n
165e5 4f 72 69 67 20 3d 20 70 61 67 65 72 50 61 67 65  Orig = pagerPage
165e6 63 6f 75 6e 74 28 70 42 74 29 3b 0a 20 20 20 20  count(pBt);.    
165e7 69 66 28 20 50 54 52 4d 41 50 5f 49 53 50 41 47  if( PTRMAP_ISPAG
165e8 45 28 70 42 74 2c 20 6e 4f 72 69 67 29 20 7c 7c  E(pBt, nOrig) ||
165e9 20 6e 4f 72 69 67 3d 3d 50 45 4e 44 49 4e 47 5f   nOrig==PENDING_
165ea 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29  BYTE_PAGE(pBt) )
165eb 7b 0a 20 20 20 20 20 20 2f 2a 20 49 74 20 69 73  {.      /* It is
165ec 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f   not possible to
165ed 20 63 72 65 61 74 65 20 61 20 64 61 74 61 62 61   create a databa
165ee 73 65 20 66 6f 72 20 77 68 69 63 68 20 74 68 65  se for which the
165ef 20 66 69 6e 61 6c 20 70 61 67 65 0a 20 20 20 20   final page.    
165f0 20 20 2a 2a 20 69 73 20 65 69 74 68 65 72 20 61    ** is either a
165f1 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67   pointer-map pag
165f2 65 20 6f 72 20 74 68 65 20 70 65 6e 64 69 6e 67  e or the pending
165f3 2d 62 79 74 65 20 70 61 67 65 2e 20 49 66 20 6f  -byte page. If o
165f4 6e 65 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 65  ne.      ** is e
165f5 6e 63 6f 75 6e 74 65 72 65 64 2c 20 74 68 69 73  ncountered, this
165f6 20 69 6e 64 69 63 61 74 65 73 20 63 6f 72 72 75   indicates corru
165f7 70 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a  ption..      */.
165f8 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
165f9 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
165fa 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e 46 72  ;.    }..    nFr
165fb 65 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  ee = get4byte(&p
165fc 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74  Bt->pPage1->aDat
165fd 61 5b 33 36 5d 29 3b 0a 20 20 20 20 6e 45 6e 74  a[36]);.    nEnt
165fe 72 79 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65  ry = pBt->usable
165ff 53 69 7a 65 2f 35 3b 0a 20 20 20 20 6e 50 74 72  Size/5;.    nPtr
16600 6d 61 70 20 3d 20 28 6e 46 72 65 65 2d 6e 4f 72  map = (nFree-nOr
16601 69 67 2b 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f  ig+PTRMAP_PAGENO
16602 28 70 42 74 2c 20 6e 4f 72 69 67 29 2b 6e 45 6e  (pBt, nOrig)+nEn
16603 74 72 79 29 2f 6e 45 6e 74 72 79 3b 0a 20 20 20  try)/nEntry;.   
16604 20 6e 46 69 6e 20 3d 20 6e 4f 72 69 67 20 2d 20   nFin = nOrig - 
16605 6e 46 72 65 65 20 2d 20 6e 50 74 72 6d 61 70 3b  nFree - nPtrmap;
16606 0a 20 20 20 20 69 66 28 20 6e 4f 72 69 67 3e 50  .    if( nOrig>P
16607 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45  ENDING_BYTE_PAGE
16608 28 70 42 74 29 20 26 26 20 6e 46 69 6e 3c 50 45  (pBt) && nFin<PE
16609 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
1660a 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 6e 46  pBt) ){.      nF
1660b 69 6e 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  in--;.    }.    
1660c 77 68 69 6c 65 28 20 50 54 52 4d 41 50 5f 49 53  while( PTRMAP_IS
1660d 50 41 47 45 28 70 42 74 2c 20 6e 46 69 6e 29 20  PAGE(pBt, nFin) 
1660e 7c 7c 20 6e 46 69 6e 3d 3d 50 45 4e 44 49 4e 47  || nFin==PENDING
1660f 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20  _BYTE_PAGE(pBt) 
16610 29 7b 0a 20 20 20 20 20 20 6e 46 69 6e 2d 2d 3b  ){.      nFin--;
16611 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e  .    }.    if( n
16612 46 69 6e 3e 6e 4f 72 69 67 20 29 20 72 65 74 75  Fin>nOrig ) retu
16613 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
16614 54 5f 42 4b 50 54 3b 0a 0a 20 20 20 20 66 6f 72  T_BKPT;..    for
16615 28 69 46 72 65 65 3d 6e 4f 72 69 67 3b 20 69 46  (iFree=nOrig; iF
16616 72 65 65 3e 6e 46 69 6e 20 26 26 20 72 63 3d 3d  ree>nFin && rc==
16617 53 51 4c 49 54 45 5f 4f 4b 3b 20 69 46 72 65 65  SQLITE_OK; iFree
16618 2d 2d 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  --){.      rc = 
16619 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28 70  incrVacuumStep(p
1661a 42 74 2c 20 6e 46 69 6e 2c 20 69 46 72 65 65 29  Bt, nFin, iFree)
1661b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1661c 28 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45  (rc==SQLITE_DONE
1661d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f   || rc==SQLITE_O
1661e 4b 29 20 26 26 20 6e 46 72 65 65 3e 30 20 29 7b  K) && nFree>0 ){
1661f 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
16620 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 72 63 20  TE_OK;.      rc 
16621 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
16622 69 74 65 28 70 42 74 2d 3e 70 50 61 67 65 31 2d  ite(pBt->pPage1-
16623 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20  >pDbPage);.     
16624 20 70 75 74 34 62 79 74 65 28 26 70 42 74 2d 3e   put4byte(&pBt->
16625 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32  pPage1->aData[32
16626 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 75 74  ], 0);.      put
16627 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67  4byte(&pBt->pPag
16628 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 2c 20 30  e1->aData[36], 0
16629 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1662a 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61  PagerTruncateIma
1662b 67 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20  ge(pBt->pPager, 
1662c 6e 46 69 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20  nFin);.    }.   
1662d 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1662e 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  OK ){.      sqli
1662f 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b  te3PagerRollback
16630 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a  (pPager);.    }.
16631 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 6e    }..  assert( n
16632 52 65 66 3d 3d 73 71 6c 69 74 65 33 50 61 67 65  Ref==sqlite3Page
16633 72 52 65 66 63 6f 75 6e 74 28 70 50 61 67 65 72  rRefcount(pPager
16634 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ) );.  return rc
16635 3b 0a 7d 0a 0a 23 65 6c 73 65 20 2f 2a 20 69 66  ;.}..#else /* if
16636 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
16637 5f 41 55 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 23  _AUTOVACUUM */.#
16638 20 64 65 66 69 6e 65 20 73 65 74 43 68 69 6c 64   define setChild
16639 50 74 72 6d 61 70 73 28 78 29 20 53 51 4c 49 54  Ptrmaps(x) SQLIT
1663a 45 5f 4f 4b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  E_OK.#endif../*.
1663b 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
1663c 64 6f 65 73 20 74 68 65 20 66 69 72 73 74 20 70  does the first p
1663d 68 61 73 65 20 6f 66 20 61 20 74 77 6f 2d 70 68  hase of a two-ph
1663e 61 73 65 20 63 6f 6d 6d 69 74 2e 20 20 54 68 69  ase commit.  Thi
1663f 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 63 61 75  s routine.** cau
16640 73 65 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a  ses a rollback j
16641 6f 75 72 6e 61 6c 20 74 6f 20 62 65 20 63 72 65  ournal to be cre
16642 61 74 65 64 20 28 69 66 20 69 74 20 64 6f 65 73  ated (if it does
16643 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69   not already exi
16644 73 74 29 0a 2a 2a 20 61 6e 64 20 70 6f 70 75 6c  st).** and popul
16645 61 74 65 64 20 77 69 74 68 20 65 6e 6f 75 67 68  ated with enough
16646 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 6f 20   information so 
16647 74 68 61 74 20 69 66 20 61 20 70 6f 77 65 72 20  that if a power 
16648 6c 6f 73 73 20 6f 63 63 75 72 73 0a 2a 2a 20 74  loss occurs.** t
16649 68 65 20 64 61 74 61 62 61 73 65 20 63 61 6e 20  he database can 
1664a 62 65 20 72 65 73 74 6f 72 65 64 20 74 6f 20 69  be restored to i
1664b 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74  ts original stat
1664c 65 20 62 79 20 70 6c 61 79 69 6e 67 20 62 61 63  e by playing bac
1664d 6b 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c  k.** the journal
1664e 2e 20 20 54 68 65 6e 20 74 68 65 20 63 6f 6e 74  .  Then the cont
1664f 65 6e 74 73 20 6f 66 20 74 68 65 20 6a 6f 75 72  ents of the jour
16650 6e 61 6c 20 61 72 65 20 66 6c 75 73 68 65 64 20  nal are flushed 
16651 6f 75 74 20 74 6f 0a 2a 2a 20 74 68 65 20 64 69  out to.** the di
16652 73 6b 2e 20 20 41 66 74 65 72 20 74 68 65 20 6a  sk.  After the j
16653 6f 75 72 6e 61 6c 20 69 73 20 73 61 66 65 6c 79  ournal is safely
16654 20 6f 6e 20 6f 78 69 64 65 2c 20 74 68 65 20 63   on oxide, the c
16655 68 61 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a 2a  hanges to the.**
16656 20 64 61 74 61 62 61 73 65 20 61 72 65 20 77 72   database are wr
16657 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 64  itten into the d
16658 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64  atabase file and
16659 20 66 6c 75 73 68 65 64 20 74 6f 20 6f 78 69 64   flushed to oxid
1665a 65 2e 0a 2a 2a 20 41 74 20 74 68 65 20 65 6e 64  e..** At the end
1665b 20 6f 66 20 74 68 69 73 20 63 61 6c 6c 2c 20 74   of this call, t
1665c 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72  he rollback jour
1665d 6e 61 6c 20 73 74 69 6c 6c 20 65 78 69 73 74 73  nal still exists
1665e 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 69 73 6b 20   on the.** disk 
1665f 61 6e 64 20 77 65 20 61 72 65 20 73 74 69 6c 6c  and we are still
16660 20 68 6f 6c 64 69 6e 67 20 61 6c 6c 20 6c 6f 63   holding all loc
16661 6b 73 2c 20 73 6f 20 74 68 65 20 74 72 61 6e 73  ks, so the trans
16662 61 63 74 69 6f 6e 20 68 61 73 20 6e 6f 74 0a 2a  action has not.*
16663 2a 20 63 6f 6d 6d 69 74 74 65 64 2e 20 20 53 65  * committed.  Se
16664 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  e sqlite3BtreeCo
16665 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 66  mmitPhaseTwo() f
16666 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 70 68  or the second ph
16667 61 73 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f  ase of the.** co
16668 6d 6d 69 74 20 70 72 6f 63 65 73 73 2e 0a 2a 2a  mmit process..**
16669 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 69 73  .** This call is
1666a 20 61 20 6e 6f 2d 6f 70 20 69 66 20 6e 6f 20 77   a no-op if no w
1666b 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e  rite-transaction
1666c 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 63   is currently ac
1666d 74 69 76 65 20 6f 6e 20 70 42 74 2e 0a 2a 2a 0a  tive on pBt..**.
1666e 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 73 79  ** Otherwise, sy
1666f 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 65 20  nc the database 
16670 66 69 6c 65 20 66 6f 72 20 74 68 65 20 62 74 72  file for the btr
16671 65 65 20 70 42 74 2e 20 7a 4d 61 73 74 65 72 20  ee pBt. zMaster 
16672 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68 65  points to.** the
16673 20 6e 61 6d 65 20 6f 66 20 61 20 6d 61 73 74 65   name of a maste
16674 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74  r journal file t
16675 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 77 72  hat should be wr
16676 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 0a 2a  itten into the.*
16677 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f 75  * individual jou
16678 72 6e 61 6c 20 66 69 6c 65 2c 20 6f 72 20 69 73  rnal file, or is
16679 20 4e 55 4c 4c 2c 20 69 6e 64 69 63 61 74 69 6e   NULL, indicatin
1667a 67 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72  g no master jour
1667b 6e 61 6c 20 66 69 6c 65 20 0a 2a 2a 20 28 73 69  nal file .** (si
1667c 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 74 72  ngle database tr
1667d 61 6e 73 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a 2a  ansaction)..**.*
1667e 2a 20 57 68 65 6e 20 74 68 69 73 20 69 73 20 63  * When this is c
1667f 61 6c 6c 65 64 2c 20 74 68 65 20 6d 61 73 74 65  alled, the maste
16680 72 20 6a 6f 75 72 6e 61 6c 20 73 68 6f 75 6c 64  r journal should
16681 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 62 65   already have be
16682 65 6e 0a 2a 2a 20 63 72 65 61 74 65 64 2c 20 70  en.** created, p
16683 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 74 68  opulated with th
16684 69 73 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74  is journal point
16685 65 72 20 61 6e 64 20 73 79 6e 63 65 64 20 74 6f  er and synced to
16686 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63   disk..**.** Onc
16687 65 20 74 68 69 73 20 69 73 20 72 6f 75 74 69 6e  e this is routin
16688 65 20 68 61 73 20 72 65 74 75 72 6e 65 64 2c 20  e has returned, 
16689 74 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 72  the only thing r
1668a 65 71 75 69 72 65 64 20 74 6f 20 63 6f 6d 6d 69  equired to commi
1668b 74 0a 2a 2a 20 74 68 65 20 77 72 69 74 65 2d 74  t.** the write-t
1668c 72 61 6e 73 61 63 74 69 6f 6e 20 66 6f 72 20 74  ransaction for t
1668d 68 69 73 20 64 61 74 61 62 61 73 65 20 66 69 6c  his database fil
1668e 65 20 69 73 20 74 6f 20 64 65 6c 65 74 65 20 74  e is to delete t
1668f 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2f 0a 53  he journal..*/.S
16690 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
16691 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  t sqlite3BtreeCo
16692 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 42 74 72  mmitPhaseOne(Btr
16693 65 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61  ee *p, const cha
16694 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20 69  r *zMaster){.  i
16695 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
16696 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72  K;.  if( p->inTr
16697 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ans==TRANS_WRITE
16698 20 29 7b 0a 20 20 20 20 42 74 53 68 61 72 65 64   ){.    BtShared
16699 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
1669a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
1669b 45 6e 74 65 72 28 70 29 3b 0a 23 69 66 6e 64 65  Enter(p);.#ifnde
1669c 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
1669d 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28  TOVACUUM.    if(
1669e 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d   pBt->autoVacuum
1669f 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61   ){.      rc = a
166a0 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d 69 74 28  utoVacuumCommit(
166a1 70 42 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20  pBt);.      if( 
166a2 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
166a3 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
166a4 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
166a5 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
166a6 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
166a7 23 65 6e 64 69 66 0a 20 20 20 20 72 63 20 3d 20  #endif.    rc = 
166a8 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d  sqlite3PagerComm
166a9 69 74 50 68 61 73 65 4f 6e 65 28 70 42 74 2d 3e  itPhaseOne(pBt->
166aa 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 72 2c  pPager, zMaster,
166ab 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
166ac 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
166ad 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
166ae 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
166af 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
166b0 20 66 72 6f 6d 20 62 6f 74 68 20 42 74 72 65 65   from both Btree
166b1 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29  CommitPhaseTwo()
166b2 20 61 6e 64 20 42 74 72 65 65 52 6f 6c 6c 62 61   and BtreeRollba
166b3 63 6b 28 29 0a 2a 2a 20 61 74 20 74 68 65 20 63  ck().** at the c
166b4 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 61 20 74  onclusion of a t
166b5 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73  ransaction..*/.s
166b6 74 61 74 69 63 20 76 6f 69 64 20 62 74 72 65 65  tatic void btree
166b7 45 6e 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 42  EndTransaction(B
166b8 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68  tree *p){.  BtSh
166b9 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70  ared *pBt = p->p
166ba 42 74 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a  Bt;.  BtCursor *
166bb 70 43 73 72 3b 0a 20 20 61 73 73 65 72 74 28 20  pCsr;.  assert( 
166bc 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
166bd 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 0a 20 20  sMutex(p) );..  
166be 2f 2a 20 53 65 61 72 63 68 20 66 6f 72 20 61 20  /* Search for a 
166bf 63 75 72 73 6f 72 20 68 65 6c 64 20 6f 70 65 6e  cursor held open
166c0 20 62 79 20 74 68 69 73 20 62 2d 74 72 65 65 20   by this b-tree 
166c1 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 49 66 20 6f  connection. If o
166c2 6e 65 20 65 78 69 73 74 73 2c 0a 20 20 2a 2a 20  ne exists,.  ** 
166c3 74 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63  then the transac
166c4 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 64 6f 77  tion will be dow
166c5 6e 67 72 61 64 65 64 20 74 6f 20 61 20 72 65 61  ngraded to a rea
166c6 64 2d 6f 6e 6c 79 20 74 72 61 6e 73 61 63 74 69  d-only transacti
166c7 6f 6e 0a 20 20 2a 2a 20 69 6e 73 74 65 61 64 20  on.  ** instead 
166c8 6f 66 20 61 63 74 75 61 6c 6c 79 20 63 6f 6e 63  of actually conc
166c9 6c 75 64 65 64 2e 20 41 20 73 75 62 73 65 71 75  luded. A subsequ
166ca 65 6e 74 20 63 61 6c 6c 20 74 6f 20 43 6f 6d 6d  ent call to Comm
166cb 69 74 50 68 61 73 65 54 77 6f 28 29 20 0a 20 20  itPhaseTwo() .  
166cc 2a 2a 20 6f 72 20 52 6f 6c 6c 62 61 63 6b 28 29  ** or Rollback()
166cd 20 77 69 6c 6c 20 66 69 6e 69 73 68 20 74 68 65   will finish the
166ce 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64   transaction and
166cf 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 61   unlock the data
166d0 62 61 73 65 2e 20 20 2a 2f 0a 20 20 66 6f 72 28  base.  */.  for(
166d1 70 43 73 72 3d 70 42 74 2d 3e 70 43 75 72 73 6f  pCsr=pBt->pCurso
166d2 72 3b 20 70 43 73 72 20 26 26 20 70 43 73 72 2d  r; pCsr && pCsr-
166d3 3e 70 42 74 72 65 65 21 3d 70 3b 20 70 43 73 72  >pBtree!=p; pCsr
166d4 3d 70 43 73 72 2d 3e 70 4e 65 78 74 29 3b 0a 20  =pCsr->pNext);. 
166d5 20 61 73 73 65 72 74 28 20 70 43 73 72 3d 3d 30   assert( pCsr==0
166d6 20 7c 7c 20 70 2d 3e 69 6e 54 72 61 6e 73 3e 54   || p->inTrans>T
166d7 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b 0a 0a 20 20  RANS_NONE );..  
166d8 62 74 72 65 65 43 6c 65 61 72 48 61 73 43 6f 6e  btreeClearHasCon
166d9 74 65 6e 74 28 70 42 74 29 3b 0a 20 20 69 66 28  tent(pBt);.  if(
166da 20 70 43 73 72 20 29 7b 0a 20 20 20 20 64 6f 77   pCsr ){.    dow
166db 6e 67 72 61 64 65 41 6c 6c 53 68 61 72 65 64 43  ngradeAllSharedC
166dc 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 70  acheTableLocks(p
166dd 29 3b 0a 20 20 20 20 70 2d 3e 69 6e 54 72 61 6e  );.    p->inTran
166de 73 20 3d 20 54 52 41 4e 53 5f 52 45 41 44 3b 0a  s = TRANS_READ;.
166df 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
166e0 49 66 20 74 68 65 20 68 61 6e 64 6c 65 20 68 61  If the handle ha
166e1 64 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20 74 72  d any kind of tr
166e2 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 2c 20  ansaction open, 
166e3 64 65 63 72 65 6d 65 6e 74 20 74 68 65 20 0a 20  decrement the . 
166e4 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f     ** transactio
166e5 6e 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 73  n count of the s
166e6 68 61 72 65 64 20 62 74 72 65 65 2e 20 49 66 20  hared btree. If 
166e7 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
166e8 63 6f 75 6e 74 20 0a 20 20 20 20 2a 2a 20 72 65  count .    ** re
166e9 61 63 68 65 73 20 30 2c 20 73 65 74 20 74 68 65  aches 0, set the
166ea 20 73 68 61 72 65 64 20 73 74 61 74 65 20 74 6f   shared state to
166eb 20 54 52 41 4e 53 5f 4e 4f 4e 45 2e 20 54 68 65   TRANS_NONE. The
166ec 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e   unlockBtreeIfUn
166ed 75 73 65 64 28 29 0a 20 20 20 20 2a 2a 20 63 61  used().    ** ca
166ee 6c 6c 20 62 65 6c 6f 77 20 77 69 6c 6c 20 75 6e  ll below will un
166ef 6c 6f 63 6b 20 74 68 65 20 70 61 67 65 72 2e 20  lock the pager. 
166f0 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 69   */.    if( p->i
166f1 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f  nTrans!=TRANS_NO
166f2 4e 45 20 29 7b 0a 20 20 20 20 20 20 63 6c 65 61  NE ){.      clea
166f3 72 41 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54  rAllSharedCacheT
166f4 61 62 6c 65 4c 6f 63 6b 73 28 70 29 3b 0a 20 20  ableLocks(p);.  
166f5 20 20 20 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61      pBt->nTransa
166f6 63 74 69 6f 6e 2d 2d 3b 0a 20 20 20 20 20 20 69  ction--;.      i
166f7 66 28 20 30 3d 3d 70 42 74 2d 3e 6e 54 72 61 6e  f( 0==pBt->nTran
166f8 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20  saction ){.     
166f9 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61     pBt->inTransa
166fa 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 4e 4f  ction = TRANS_NO
166fb 4e 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  NE;.      }.    
166fc 7d 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68  }..    /* Set th
166fd 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61  e current transa
166fe 63 74 69 6f 6e 20 73 74 61 74 65 20 74 6f 20 54  ction state to T
166ff 52 41 4e 53 5f 4e 4f 4e 45 20 61 6e 64 20 75 6e  RANS_NONE and un
16700 6c 6f 63 6b 20 74 68 65 20 0a 20 20 20 20 2a 2a  lock the .    **
16701 20 70 61 67 65 72 20 69 66 20 74 68 69 73 20 63   pager if this c
16702 61 6c 6c 20 63 6c 6f 73 65 64 20 74 68 65 20 6f  all closed the o
16703 6e 6c 79 20 72 65 61 64 20 6f 72 20 77 72 69 74  nly read or writ
16704 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20  e transaction.  
16705 2a 2f 0a 20 20 20 20 70 2d 3e 69 6e 54 72 61 6e  */.    p->inTran
16706 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a  s = TRANS_NONE;.
16707 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49      unlockBtreeI
16708 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 20  fUnused(pBt);.  
16709 7d 0a 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72  }..  btreeIntegr
1670a 69 74 79 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ity(p);.}../*.**
1670b 20 43 6f 6d 6d 69 74 20 74 68 65 20 74 72 61 6e   Commit the tran
1670c 73 61 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c  saction currentl
1670d 79 20 69 6e 20 70 72 6f 67 72 65 73 73 2e 0a 2a  y in progress..*
1670e 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1670f 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  e implements the
16710 20 73 65 63 6f 6e 64 20 70 68 61 73 65 20 6f 66   second phase of
16711 20 61 20 32 2d 70 68 61 73 65 20 63 6f 6d 6d 69   a 2-phase commi
16712 74 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74  t.  The.** sqlit
16713 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61  e3BtreeCommitPha
16714 73 65 4f 6e 65 28 29 20 72 6f 75 74 69 6e 65 20  seOne() routine 
16715 64 6f 65 73 20 74 68 65 20 66 69 72 73 74 20 70  does the first p
16716 68 61 73 65 20 61 6e 64 20 73 68 6f 75 6c 64 0a  hase and should.
16717 2a 2a 20 62 65 20 69 6e 76 6f 6b 65 64 20 70 72  ** be invoked pr
16718 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 20 74  ior to calling t
16719 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 54 68  his routine.  Th
1671a 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  e sqlite3BtreeCo
1671b 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 0a 2a  mmitPhaseOne().*
1671c 2a 20 72 6f 75 74 69 6e 65 20 64 69 64 20 61 6c  * routine did al
1671d 6c 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 77 72  l the work of wr
1671e 69 74 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f  iting informatio
1671f 6e 20 6f 75 74 20 74 6f 20 64 69 73 6b 20 61 6e  n out to disk an
16720 64 20 66 6c 75 73 68 69 6e 67 20 74 68 65 0a 2a  d flushing the.*
16721 2a 20 63 6f 6e 74 65 6e 74 73 20 73 6f 20 74 68  * contents so th
16722 61 74 20 74 68 65 79 20 61 72 65 20 77 72 69 74  at they are writ
16723 74 65 6e 20 6f 6e 74 6f 20 74 68 65 20 64 69 73  ten onto the dis
16724 6b 20 70 6c 61 74 74 65 72 2e 20 20 41 6c 6c 20  k platter.  All 
16725 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  this.** routine 
16726 68 61 73 20 74 6f 20 64 6f 20 69 73 20 64 65 6c  has to do is del
16727 65 74 65 20 6f 72 20 74 72 75 6e 63 61 74 65 20  ete or truncate 
16728 6f 72 20 7a 65 72 6f 20 74 68 65 20 68 65 61 64  or zero the head
16729 65 72 20 69 6e 20 74 68 65 0a 2a 2a 20 74 68 65  er in the.** the
1672a 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61   rollback journa
1672b 6c 20 28 77 68 69 63 68 20 63 61 75 73 65 73 20  l (which causes 
1672c 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
1672d 74 6f 20 63 6f 6d 6d 69 74 29 20 61 6e 64 0a 2a  to commit) and.*
1672e 2a 20 64 72 6f 70 20 6c 6f 63 6b 73 2e 0a 2a 2a  * drop locks..**
1672f 0a 2a 2a 20 54 68 69 73 20 77 69 6c 6c 20 72 65  .** This will re
16730 6c 65 61 73 65 20 74 68 65 20 77 72 69 74 65 20  lease the write 
16731 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61  lock on the data
16732 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 74  base file.  If t
16733 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61  here.** are no a
16734 63 74 69 76 65 20 63 75 72 73 6f 72 73 2c 20 69  ctive cursors, i
16735 74 20 61 6c 73 6f 20 72 65 6c 65 61 73 65 73 20  t also releases 
16736 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a  the read lock..*
16737 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
16738 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
16739 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28  eCommitPhaseTwo(
1673a 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53  Btree *p){.  BtS
1673b 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
1673c 70 42 74 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42  pBt;..  sqlite3B
1673d 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20  treeEnter(p);.  
1673e 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70  btreeIntegrity(p
1673f 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
16740 68 61 6e 64 6c 65 20 68 61 73 20 61 20 77 72 69  handle has a wri
16741 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f  te-transaction o
16742 70 65 6e 2c 20 63 6f 6d 6d 69 74 20 74 68 65 20  pen, commit the 
16743 73 68 61 72 65 64 2d 62 74 72 65 65 73 20 0a 20  shared-btrees . 
16744 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20   ** transaction 
16745 61 6e 64 20 73 65 74 20 74 68 65 20 73 68 61 72  and set the shar
16746 65 64 20 73 74 61 74 65 20 74 6f 20 54 52 41 4e  ed state to TRAN
16747 53 5f 52 45 41 44 2e 0a 20 20 2a 2f 0a 20 20 69  S_READ..  */.  i
16748 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54  f( p->inTrans==T
16749 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20  RANS_WRITE ){.  
1674a 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 61 73    int rc;.    as
1674b 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61  sert( pBt->inTra
1674c 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f  nsaction==TRANS_
1674d 57 52 49 54 45 20 29 3b 0a 20 20 20 20 61 73 73  WRITE );.    ass
1674e 65 72 74 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73  ert( pBt->nTrans
1674f 61 63 74 69 6f 6e 3e 30 20 29 3b 0a 20 20 20 20  action>0 );.    
16750 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
16751 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28  rCommitPhaseTwo(
16752 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20  pBt->pPager);.  
16753 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
16754 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  _OK ){.      sql
16755 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
16756 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
16757 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42  rc;.    }.    pB
16758 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  t->inTransaction
16759 20 3d 20 54 52 41 4e 53 5f 52 45 41 44 3b 0a 20   = TRANS_READ;. 
1675a 20 7d 0a 0a 20 20 62 74 72 65 65 45 6e 64 54 72   }..  btreeEndTr
1675b 61 6e 73 61 63 74 69 6f 6e 28 70 29 3b 0a 20 20  ansaction(p);.  
1675c 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
1675d 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  e(p);.  return S
1675e 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
1675f 2a 2a 20 44 6f 20 62 6f 74 68 20 70 68 61 73 65  ** Do both phase
16760 73 20 6f 66 20 61 20 63 6f 6d 6d 69 74 2e 0a 2a  s of a commit..*
16761 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
16762 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
16763 65 43 6f 6d 6d 69 74 28 42 74 72 65 65 20 2a 70  eCommit(Btree *p
16764 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73  ){.  int rc;.  s
16765 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
16766 28 70 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  (p);.  rc = sqli
16767 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68  te3BtreeCommitPh
16768 61 73 65 4f 6e 65 28 70 2c 20 30 29 3b 0a 20 20  aseOne(p, 0);.  
16769 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1676a 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  K ){.    rc = sq
1676b 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74  lite3BtreeCommit
1676c 50 68 61 73 65 54 77 6f 28 70 29 3b 0a 20 20 7d  PhaseTwo(p);.  }
1676d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
1676e 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72  eave(p);.  retur
1676f 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66  n rc;.}..#ifndef
16770 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65   NDEBUG./*.** Re
16771 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
16772 6f 66 20 77 72 69 74 65 2d 63 75 72 73 6f 72 73  of write-cursors
16773 20 6f 70 65 6e 20 6f 6e 20 74 68 69 73 20 68 61   open on this ha
16774 6e 64 6c 65 2e 20 54 68 69 73 20 69 73 20 66 6f  ndle. This is fo
16775 72 20 75 73 65 0a 2a 2a 20 69 6e 20 61 73 73 65  r use.** in asse
16776 72 74 28 29 20 65 78 70 72 65 73 73 69 6f 6e 73  rt() expressions
16777 2c 20 73 6f 20 69 74 20 69 73 20 6f 6e 6c 79 20  , so it is only 
16778 63 6f 6d 70 69 6c 65 64 20 69 66 20 4e 44 45 42  compiled if NDEB
16779 55 47 20 69 73 20 6e 6f 74 0a 2a 2a 20 64 65 66  UG is not.** def
1677a 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  ined..**.** For 
1677b 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20  the purposes of 
1677c 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 61 20  this routine, a 
1677d 77 72 69 74 65 2d 63 75 72 73 6f 72 20 69 73 20  write-cursor is 
1677e 61 6e 79 20 63 75 72 73 6f 72 20 74 68 61 74 0a  any cursor that.
1677f 2a 2a 20 69 73 20 63 61 70 61 62 6c 65 20 6f 66  ** is capable of
16780 20 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20   writing to the 
16781 64 61 74 61 62 73 65 2e 20 20 54 68 61 74 20 6d  databse.  That m
16782 65 61 6e 73 20 74 68 65 20 63 75 72 73 6f 72 20  eans the cursor 
16783 77 61 73 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 6c  was.** originall
16784 79 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 72 69  y opened for wri
16785 74 69 6e 67 20 61 6e 64 20 74 68 65 20 63 75 72  ting and the cur
16786 73 6f 72 20 68 61 73 20 6e 6f 74 20 62 65 20 64  sor has not be d
16787 69 73 61 62 6c 65 64 0a 2a 2a 20 62 79 20 68 61  isabled.** by ha
16788 76 69 6e 67 20 69 74 73 20 73 74 61 74 65 20 63  ving its state c
16789 68 61 6e 67 65 64 20 74 6f 20 43 55 52 53 4f 52  hanged to CURSOR
1678a 5f 46 41 55 4c 54 2e 0a 2a 2f 0a 73 74 61 74 69  _FAULT..*/.stati
1678b 63 20 69 6e 74 20 63 6f 75 6e 74 57 72 69 74 65  c int countWrite
1678c 43 75 72 73 6f 72 73 28 42 74 53 68 61 72 65 64  Cursors(BtShared
1678d 20 2a 70 42 74 29 7b 0a 20 20 42 74 43 75 72 73   *pBt){.  BtCurs
1678e 6f 72 20 2a 70 43 75 72 3b 0a 20 20 69 6e 74 20  or *pCur;.  int 
1678f 72 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70 43 75  r = 0;.  for(pCu
16790 72 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20  r=pBt->pCursor; 
16791 70 43 75 72 3b 20 70 43 75 72 3d 70 43 75 72 2d  pCur; pCur=pCur-
16792 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28  >pNext){.    if(
16793 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 26 26   pCur->wrFlag &&
16794 20 70 43 75 72 2d 3e 65 53 74 61 74 65 21 3d 43   pCur->eState!=C
16795 55 52 53 4f 52 5f 46 41 55 4c 54 20 29 20 72 2b  URSOR_FAULT ) r+
16796 2b 3b 20 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  +; .  }.  return
16797 20 72 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a   r;.}.#endif../*
16798 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
16799 20 73 65 74 73 20 74 68 65 20 73 74 61 74 65 20   sets the state 
1679a 74 6f 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 20  to CURSOR_FAULT 
1679b 61 6e 64 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a  and the error.**
1679c 20 63 6f 64 65 20 74 6f 20 65 72 72 43 6f 64 65   code to errCode
1679d 20 66 6f 72 20 65 76 65 72 79 20 63 75 72 73 6f   for every curso
1679e 72 20 6f 6e 20 42 74 53 68 61 72 65 64 20 74 68  r on BtShared th
1679f 61 74 20 70 42 74 72 65 65 0a 2a 2a 20 72 65 66  at pBtree.** ref
167a0 65 72 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 45  erences..**.** E
167a1 76 65 72 79 20 63 75 72 73 6f 72 20 69 73 20 74  very cursor is t
167a2 72 69 70 70 65 64 2c 20 69 6e 63 6c 75 64 69 6e  ripped, includin
167a3 67 20 63 75 72 73 6f 72 73 20 74 68 61 74 20 62  g cursors that b
167a4 65 6c 6f 6e 67 0a 2a 2a 20 74 6f 20 6f 74 68 65  elong.** to othe
167a5 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  r database conne
167a6 63 74 69 6f 6e 73 20 74 68 61 74 20 68 61 70 70  ctions that happ
167a7 65 6e 20 74 6f 20 62 65 20 73 68 61 72 69 6e 67  en to be sharing
167a8 0a 2a 2a 20 74 68 65 20 63 61 63 68 65 20 77 69  .** the cache wi
167a9 74 68 20 70 42 74 72 65 65 2e 0a 2a 2a 0a 2a 2a  th pBtree..**.**
167aa 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65   This routine ge
167ab 74 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61  ts called when a
167ac 20 72 6f 6c 6c 62 61 63 6b 20 6f 63 63 75 72 73   rollback occurs
167ad 2e 0a 2a 2a 20 41 6c 6c 20 63 75 72 73 6f 72 73  ..** All cursors
167ae 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20   using the same 
167af 63 61 63 68 65 20 6d 75 73 74 20 62 65 20 74 72  cache must be tr
167b0 69 70 70 65 64 0a 2a 2a 20 74 6f 20 70 72 65 76  ipped.** to prev
167b1 65 6e 74 20 74 68 65 6d 20 66 72 6f 6d 20 74 72  ent them from tr
167b2 79 69 6e 67 20 74 6f 20 75 73 65 20 74 68 65 20  ying to use the 
167b3 62 74 72 65 65 20 61 66 74 65 72 0a 2a 2a 20 74  btree after.** t
167b4 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 20 20 54 68  he rollback.  Th
167b5 65 20 72 6f 6c 6c 62 61 63 6b 20 6d 61 79 20 68  e rollback may h
167b6 61 76 65 20 64 65 6c 65 74 65 64 20 74 61 62 6c  ave deleted tabl
167b7 65 73 0a 2a 2a 20 6f 72 20 6d 6f 76 65 64 20 72  es.** or moved r
167b8 6f 6f 74 20 70 61 67 65 73 2c 20 73 6f 20 69 74  oot pages, so it
167b9 20 69 73 20 6e 6f 74 20 73 75 66 66 69 63 69 65   is not sufficie
167ba 6e 74 20 74 6f 0a 2a 2a 20 73 61 76 65 20 74 68  nt to.** save th
167bb 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 63  e state of the c
167bc 75 72 73 6f 72 2e 20 20 54 68 65 20 63 75 72 73  ursor.  The curs
167bd 6f 72 20 6d 75 73 74 20 62 65 0a 2a 2a 20 69 6e  or must be.** in
167be 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2f 0a 53 51  validated..*/.SQ
167bf 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
167c0 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 54 72  d sqlite3BtreeTr
167c1 69 70 41 6c 6c 43 75 72 73 6f 72 73 28 42 74 72  ipAllCursors(Btr
167c2 65 65 20 2a 70 42 74 72 65 65 2c 20 69 6e 74 20  ee *pBtree, int 
167c3 65 72 72 43 6f 64 65 29 7b 0a 20 20 42 74 43 75  errCode){.  BtCu
167c4 72 73 6f 72 20 2a 70 3b 0a 20 20 73 71 6c 69 74  rsor *p;.  sqlit
167c5 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 42 74  e3BtreeEnter(pBt
167c6 72 65 65 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42  ree);.  for(p=pB
167c7 74 72 65 65 2d 3e 70 42 74 2d 3e 70 43 75 72 73  tree->pBt->pCurs
167c8 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78  or; p; p=p->pNex
167c9 74 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20  t){.    int i;. 
167ca 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43     sqlite3BtreeC
167cb 6c 65 61 72 43 75 72 73 6f 72 28 70 29 3b 0a 20  learCursor(p);. 
167cc 20 20 20 70 2d 3e 65 53 74 61 74 65 20 3d 20 43     p->eState = C
167cd 55 52 53 4f 52 5f 46 41 55 4c 54 3b 0a 20 20 20  URSOR_FAULT;.   
167ce 20 70 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 65   p->skipNext = e
167cf 72 72 43 6f 64 65 3b 0a 20 20 20 20 66 6f 72 28  rrCode;.    for(
167d0 69 3d 30 3b 20 69 3c 3d 70 2d 3e 69 50 61 67 65  i=0; i<=p->iPage
167d1 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65  ; i++){.      re
167d2 6c 65 61 73 65 50 61 67 65 28 70 2d 3e 61 70 50  leasePage(p->apP
167d3 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 20 20 70  age[i]);.      p
167d4 2d 3e 61 70 50 61 67 65 5b 69 5d 20 3d 20 30 3b  ->apPage[i] = 0;
167d5 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c  .    }.  }.  sql
167d6 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
167d7 42 74 72 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Btree);.}../*.**
167d8 20 52 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72   Rollback the tr
167d9 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20 70 72 6f  ansaction in pro
167da 67 72 65 73 73 2e 20 20 41 6c 6c 20 63 75 72 73  gress.  All curs
167db 6f 72 73 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 69  ors will be.** i
167dc 6e 76 61 6c 69 64 65 64 20 62 79 20 74 68 69 73  nvalided by this
167dd 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 41 6e 79   operation.  Any
167de 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20   attempt to use 
167df 61 20 63 75 72 73 6f 72 0a 2a 2a 20 74 68 61 74  a cursor.** that
167e0 20 77 61 73 20 6f 70 65 6e 20 61 74 20 74 68 65   was open at the
167e1 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68   beginning of th
167e2 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 77 69 6c  is operation wil
167e3 6c 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 61  l result.** in a
167e4 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54  n error..**.** T
167e5 68 69 73 20 77 69 6c 6c 20 72 65 6c 65 61 73 65  his will release
167e6 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63 6b 20   the write lock 
167e7 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
167e8 66 69 6c 65 2e 20 20 49 66 20 74 68 65 72 65 0a  file.  If there.
167e9 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65  ** are no active
167ea 20 63 75 72 73 6f 72 73 2c 20 69 74 20 61 6c 73   cursors, it als
167eb 6f 20 72 65 6c 65 61 73 65 73 20 74 68 65 20 72  o releases the r
167ec 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c  ead lock..*/.SQL
167ed 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
167ee 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c  sqlite3BtreeRoll
167ef 62 61 63 6b 28 42 74 72 65 65 20 2a 70 29 7b 0a  back(Btree *p){.
167f0 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68    int rc;.  BtSh
167f1 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70  ared *pBt = p->p
167f2 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  Bt;.  MemPage *p
167f3 50 61 67 65 31 3b 0a 0a 20 20 73 71 6c 69 74 65  Page1;..  sqlite
167f4 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a  3BtreeEnter(p);.
167f5 20 20 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75    rc = saveAllCu
167f6 72 73 6f 72 73 28 70 42 74 2c 20 30 2c 20 30 29  rsors(pBt, 0, 0)
167f7 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
167f8 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
167f9 48 45 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  HE.  if( rc!=SQL
167fa 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a  ITE_OK ){.    /*
167fb 20 54 68 69 73 20 69 73 20 61 20 68 6f 72 72 69   This is a horri
167fc 62 6c 65 20 73 69 74 75 61 74 69 6f 6e 2e 20 41  ble situation. A
167fd 6e 20 49 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28 29  n IO or malloc()
167fe 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 20   error occurred 
167ff 77 68 69 6c 73 74 0a 20 20 20 20 2a 2a 20 74 72  whilst.    ** tr
16800 79 69 6e 67 20 74 6f 20 73 61 76 65 20 63 75 72  ying to save cur
16801 73 6f 72 20 70 6f 73 69 74 69 6f 6e 73 2e 20 49  sor positions. I
16802 66 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 74  f this is an aut
16803 6f 6d 61 74 69 63 20 72 6f 6c 6c 62 61 63 6b 20  omatic rollback 
16804 28 61 73 0a 20 20 20 20 2a 2a 20 74 68 65 20 72  (as.    ** the r
16805 65 73 75 6c 74 20 6f 66 20 61 20 63 6f 6e 73 74  esult of a const
16806 72 61 69 6e 74 2c 20 6d 61 6c 6c 6f 63 28 29 20  raint, malloc() 
16807 66 61 69 6c 75 72 65 20 6f 72 20 49 4f 20 65 72  failure or IO er
16808 72 6f 72 29 20 74 68 65 6e 20 0a 20 20 20 20 2a  ror) then .    *
16809 2a 20 74 68 65 20 63 61 63 68 65 20 6d 61 79 20  * the cache may 
1680a 62 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20 69 6e  be internally in
1680b 63 6f 6e 73 69 73 74 65 6e 74 20 28 6e 6f 74 20  consistent (not 
1680c 63 6f 6e 74 61 69 6e 20 76 61 6c 69 64 20 74 72  contain valid tr
1680d 65 65 73 29 20 73 6f 0a 20 20 20 20 2a 2a 20 77  ees) so.    ** w
1680e 65 20 63 61 6e 6e 6f 74 20 73 69 6d 70 6c 79 20  e cannot simply 
1680f 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72  return the error
16810 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 20   to the caller. 
16811 49 6e 73 74 65 61 64 2c 20 61 62 6f 72 74 20 0a  Instead, abort .
16812 20 20 20 20 2a 2a 20 61 6c 6c 20 71 75 65 72 69      ** all queri
16813 65 73 20 74 68 61 74 20 6d 61 79 20 62 65 20 75  es that may be u
16814 73 69 6e 67 20 61 6e 79 20 6f 66 20 74 68 65 20  sing any of the 
16815 63 75 72 73 6f 72 73 20 74 68 61 74 20 66 61 69  cursors that fai
16816 6c 65 64 20 74 6f 20 73 61 76 65 2e 0a 20 20 20  led to save..   
16817 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42   */.    sqlite3B
16818 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72 73 6f  treeTripAllCurso
16819 72 73 28 70 2c 20 72 63 29 3b 0a 20 20 7d 0a 23  rs(p, rc);.  }.#
1681a 65 6e 64 69 66 0a 20 20 62 74 72 65 65 49 6e 74  endif.  btreeInt
1681b 65 67 72 69 74 79 28 70 29 3b 0a 0a 20 20 69 66  egrity(p);..  if
1681c 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52  ( p->inTrans==TR
1681d 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20  ANS_WRITE ){.   
1681e 20 69 6e 74 20 72 63 32 3b 0a 0a 20 20 20 20 61   int rc2;..    a
1681f 73 73 65 72 74 28 20 54 52 41 4e 53 5f 57 52 49  ssert( TRANS_WRI
16820 54 45 3d 3d 70 42 74 2d 3e 69 6e 54 72 61 6e 73  TE==pBt->inTrans
16821 61 63 74 69 6f 6e 20 29 3b 0a 20 20 20 20 72 63  action );.    rc
16822 32 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  2 = sqlite3Pager
16823 52 6f 6c 6c 62 61 63 6b 28 70 42 74 2d 3e 70 50  Rollback(pBt->pP
16824 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72  ager);.    if( r
16825 63 32 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  c2!=SQLITE_OK ){
16826 0a 20 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b  .      rc = rc2;
16827 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54  .    }..    /* T
16828 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6d 61 79 20  he rollback may 
16829 68 61 76 65 20 64 65 73 74 72 6f 79 65 64 20 74  have destroyed t
1682a 68 65 20 70 50 61 67 65 31 2d 3e 61 44 61 74 61  he pPage1->aData
1682b 20 76 61 6c 75 65 2e 20 20 53 6f 0a 20 20 20 20   value.  So.    
1682c 2a 2a 20 63 61 6c 6c 20 62 74 72 65 65 47 65 74  ** call btreeGet
1682d 50 61 67 65 28 29 20 6f 6e 20 70 61 67 65 20 31  Page() on page 1
1682e 20 61 67 61 69 6e 20 74 6f 20 6d 61 6b 65 0a 20   again to make. 
1682f 20 20 20 2a 2a 20 73 75 72 65 20 70 50 61 67 65     ** sure pPage
16830 31 2d 3e 61 44 61 74 61 20 69 73 20 73 65 74 20  1->aData is set 
16831 63 6f 72 72 65 63 74 6c 79 2e 20 2a 2f 0a 20 20  correctly. */.  
16832 20 20 69 66 28 20 62 74 72 65 65 47 65 74 50 61    if( btreeGetPa
16833 67 65 28 70 42 74 2c 20 31 2c 20 26 70 50 61 67  ge(pBt, 1, &pPag
16834 65 31 2c 20 30 29 3d 3d 53 51 4c 49 54 45 5f 4f  e1, 0)==SQLITE_O
16835 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61  K ){.      relea
16836 73 65 50 61 67 65 28 70 50 61 67 65 31 29 3b 0a  sePage(pPage1);.
16837 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
16838 28 20 63 6f 75 6e 74 57 72 69 74 65 43 75 72 73  ( countWriteCurs
16839 6f 72 73 28 70 42 74 29 3d 3d 30 20 29 3b 0a 20  ors(pBt)==0 );. 
1683a 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61     pBt->inTransa
1683b 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 52 45  ction = TRANS_RE
1683c 41 44 3b 0a 20 20 7d 0a 0a 20 20 62 74 72 65 65  AD;.  }..  btree
1683d 45 6e 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 70  EndTransaction(p
1683e 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
1683f 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74  eLeave(p);.  ret
16840 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
16841 20 53 74 61 72 74 20 61 20 73 74 61 74 65 6d 65   Start a stateme
16842 6e 74 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f  nt subtransactio
16843 6e 2e 20 54 68 65 20 73 75 62 74 72 61 6e 73 61  n. The subtransa
16844 63 74 69 6f 6e 20 63 61 6e 20 63 61 6e 20 62 65  ction can can be
16845 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62 61 63 6b 20   rolled.** back 
16846 69 6e 64 65 70 65 6e 64 65 6e 74 6c 79 20 6f 66  independently of
16847 20 74 68 65 20 6d 61 69 6e 20 74 72 61 6e 73 61   the main transa
16848 63 74 69 6f 6e 2e 20 59 6f 75 20 6d 75 73 74 20  ction. You must 
16849 73 74 61 72 74 20 61 20 74 72 61 6e 73 61 63 74  start a transact
1684a 69 6f 6e 20 0a 2a 2a 20 62 65 66 6f 72 65 20 73  ion .** before s
1684b 74 61 72 74 69 6e 67 20 61 20 73 75 62 74 72 61  tarting a subtra
1684c 6e 73 61 63 74 69 6f 6e 2e 20 54 68 65 20 73 75  nsaction. The su
1684d 62 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20  btransaction is 
1684e 65 6e 64 65 64 20 61 75 74 6f 6d 61 74 69 63 61  ended automatica
1684f 6c 6c 79 20 0a 2a 2a 20 69 66 20 74 68 65 20 6d  lly .** if the m
16850 61 69 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ain transaction 
16851 63 6f 6d 6d 69 74 73 20 6f 72 20 72 6f 6c 6c 73  commits or rolls
16852 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 74 61   back..**.** Sta
16853 74 65 6d 65 6e 74 20 73 75 62 74 72 61 6e 73 61  tement subtransa
16854 63 74 69 6f 6e 73 20 61 72 65 20 75 73 65 64 20  ctions are used 
16855 61 72 6f 75 6e 64 20 69 6e 64 69 76 69 64 75 61  around individua
16856 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73  l SQL statements
16857 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 63 6f 6e  .** that are con
16858 74 61 69 6e 65 64 20 77 69 74 68 69 6e 20 61 20  tained within a 
16859 42 45 47 49 4e 2e 2e 2e 43 4f 4d 4d 49 54 20 62  BEGIN...COMMIT b
1685a 6c 6f 63 6b 2e 20 20 49 66 20 61 20 63 6f 6e 73  lock.  If a cons
1685b 74 72 61 69 6e 74 0a 2a 2a 20 65 72 72 6f 72 20  traint.** error 
1685c 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20 74 68  occurs within th
1685d 65 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65  e statement, the
1685e 20 65 66 66 65 63 74 20 6f 66 20 74 68 61 74 20   effect of that 
1685f 6f 6e 65 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a  one statement.**
16860 20 63 61 6e 20 62 65 20 72 6f 6c 6c 65 64 20 62   can be rolled b
16861 61 63 6b 20 77 69 74 68 6f 75 74 20 68 61 76 69  ack without havi
16862 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74  ng to rollback t
16863 68 65 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61  he entire transa
16864 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20 73  ction..**.** A s
16865 74 61 74 65 6d 65 6e 74 20 73 75 62 2d 74 72 61  tatement sub-tra
16866 6e 73 61 63 74 69 6f 6e 20 69 73 20 69 6d 70 6c  nsaction is impl
16867 65 6d 65 6e 74 65 64 20 61 73 20 61 6e 20 61 6e  emented as an an
16868 6f 6e 79 6d 6f 75 73 20 73 61 76 65 70 6f 69 6e  onymous savepoin
16869 74 2e 20 54 68 65 0a 2a 2a 20 76 61 6c 75 65 20  t. The.** value 
1686a 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65  passed as the se
1686b 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69  cond parameter i
1686c 73 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62  s the total numb
1686d 65 72 20 6f 66 20 73 61 76 65 70 6f 69 6e 74 73  er of savepoints
1686e 2c 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 74  ,.** including t
1686f 68 65 20 6e 65 77 20 61 6e 6f 6e 79 6d 6f 75 73  he new anonymous
16870 20 73 61 76 65 70 6f 69 6e 74 2c 20 6f 70 65 6e   savepoint, open
16871 20 6f 6e 20 74 68 65 20 42 2d 54 72 65 65 2e 20   on the B-Tree. 
16872 69 2e 65 2e 20 69 66 20 74 68 65 72 65 0a 2a 2a  i.e. if there.**
16873 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20 73   are no active s
16874 61 76 65 70 6f 69 6e 74 73 20 61 6e 64 20 6e 6f  avepoints and no
16875 20 6f 74 68 65 72 20 73 74 61 74 65 6d 65 6e 74   other statement
16876 2d 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 6f 70  -transactions op
16877 65 6e 2c 0a 2a 2a 20 69 53 74 61 74 65 6d 65 6e  en,.** iStatemen
16878 74 20 69 73 20 31 2e 20 54 68 69 73 20 61 6e 6f  t is 1. This ano
16879 6e 79 6d 6f 75 73 20 73 61 76 65 70 6f 69 6e 74  nymous savepoint
1687a 20 63 61 6e 20 62 65 20 72 65 6c 65 61 73 65 64   can be released
1687b 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61 63 6b 0a   or rolled back.
1687c 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c  ** using the sql
1687d 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69  ite3BtreeSavepoi
1687e 6e 74 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  nt() function..*
1687f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
16880 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
16881 65 42 65 67 69 6e 53 74 6d 74 28 42 74 72 65 65  eBeginStmt(Btree
16882 20 2a 70 2c 20 69 6e 74 20 69 53 74 61 74 65 6d   *p, int iStatem
16883 65 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  ent){.  int rc;.
16884 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
16885 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69  = p->pBt;.  sqli
16886 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29  te3BtreeEnter(p)
16887 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69  ;.  assert( p->i
16888 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52  nTrans==TRANS_WR
16889 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ITE );.  assert(
1688a 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 3d 3d   pBt->readOnly==
1688b 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  0 );.  assert( i
1688c 53 74 61 74 65 6d 65 6e 74 3e 30 20 29 3b 0a 20  Statement>0 );. 
1688d 20 61 73 73 65 72 74 28 20 69 53 74 61 74 65 6d   assert( iStatem
1688e 65 6e 74 3e 70 2d 3e 64 62 2d 3e 6e 53 61 76 65  ent>p->db->nSave
1688f 70 6f 69 6e 74 20 29 3b 0a 20 20 69 66 28 20 4e  point );.  if( N
16890 45 56 45 52 28 70 2d 3e 69 6e 54 72 61 6e 73 21  EVER(p->inTrans!
16891 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 7c 7c 20  =TRANS_WRITE || 
16892 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 29 20 29  pBt->readOnly) )
16893 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
16894 45 5f 49 4e 54 45 52 4e 41 4c 3b 0a 20 20 7d 65  E_INTERNAL;.  }e
16895 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
16896 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74   pBt->inTransact
16897 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ion==TRANS_WRITE
16898 20 29 3b 0a 20 20 20 20 2f 2a 20 41 74 20 74 68   );.    /* At th
16899 65 20 70 61 67 65 72 20 6c 65 76 65 6c 2c 20 61  e pager level, a
1689a 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73   statement trans
1689b 61 63 74 69 6f 6e 20 69 73 20 61 20 73 61 76 65  action is a save
1689c 70 6f 69 6e 74 20 77 69 74 68 0a 20 20 20 20 2a  point with.    *
1689d 2a 20 61 6e 20 69 6e 64 65 78 20 67 72 65 61 74  * an index great
1689e 65 72 20 74 68 61 6e 20 61 6c 6c 20 73 61 76 65  er than all save
1689f 70 6f 69 6e 74 73 20 63 72 65 61 74 65 64 20 65  points created e
168a0 78 70 6c 69 63 69 74 6c 79 20 75 73 69 6e 67 0a  xplicitly using.
168a1 20 20 20 20 2a 2a 20 53 51 4c 20 73 74 61 74 65      ** SQL state
168a2 6d 65 6e 74 73 2e 20 49 74 20 69 73 20 69 6c 6c  ments. It is ill
168a3 65 67 61 6c 20 74 6f 20 6f 70 65 6e 2c 20 72 65  egal to open, re
168a4 6c 65 61 73 65 20 6f 72 20 72 6f 6c 6c 62 61 63  lease or rollbac
168a5 6b 20 61 6e 79 0a 20 20 20 20 2a 2a 20 73 75 63  k any.    ** suc
168a6 68 20 73 61 76 65 70 6f 69 6e 74 73 20 77 68 69  h savepoints whi
168a7 6c 65 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  le the statement
168a8 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 61 76   transaction sav
168a9 65 70 6f 69 6e 74 20 69 73 20 61 63 74 69 76 65  epoint is active
168aa 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20  ..    */.    rc 
168ab 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70  = sqlite3PagerOp
168ac 65 6e 53 61 76 65 70 6f 69 6e 74 28 70 42 74 2d  enSavepoint(pBt-
168ad 3e 70 50 61 67 65 72 2c 20 69 53 74 61 74 65 6d  >pPager, iStatem
168ae 65 6e 74 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  ent);.  }.  sqli
168af 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
168b0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
168b1 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f  ../*.** The seco
168b2 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  nd argument to t
168b3 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 6f 70  his function, op
168b4 2c 20 69 73 20 61 6c 77 61 79 73 20 53 41 56 45  , is always SAVE
168b5 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 0a 2a  POINT_ROLLBACK.*
168b6 2a 20 6f 72 20 53 41 56 45 50 4f 49 4e 54 5f 52  * or SAVEPOINT_R
168b7 45 4c 45 41 53 45 2e 20 54 68 69 73 20 66 75 6e  ELEASE. This fun
168b8 63 74 69 6f 6e 20 65 69 74 68 65 72 20 72 65 6c  ction either rel
168b9 65 61 73 65 73 20 6f 72 20 72 6f 6c 6c 73 20 62  eases or rolls b
168ba 61 63 6b 20 74 68 65 0a 2a 2a 20 73 61 76 65 70  ack the.** savep
168bb 6f 69 6e 74 20 69 64 65 6e 74 69 66 69 65 64 20  oint identified 
168bc 62 79 20 70 61 72 61 6d 65 74 65 72 20 69 53 61  by parameter iSa
168bd 76 65 70 6f 69 6e 74 2c 20 64 65 70 65 6e 64 69  vepoint, dependi
168be 6e 67 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20  ng on the value 
168bf 0a 2a 2a 20 6f 66 20 6f 70 2e 0a 2a 2a 0a 2a 2a  .** of op..**.**
168c0 20 4e 6f 72 6d 61 6c 6c 79 2c 20 69 53 61 76 65   Normally, iSave
168c1 70 6f 69 6e 74 20 69 73 20 67 72 65 61 74 65 72  point is greater
168c2 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
168c3 6f 20 7a 65 72 6f 2e 20 48 6f 77 65 76 65 72 2c  o zero. However,
168c4 20 69 66 20 6f 70 20 69 73 0a 2a 2a 20 53 41 56   if op is.** SAV
168c5 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c  EPOINT_ROLLBACK,
168c6 20 74 68 65 6e 20 69 53 61 76 65 70 6f 69 6e 74   then iSavepoint
168c7 20 6d 61 79 20 61 6c 73 6f 20 62 65 20 2d 31 2e   may also be -1.
168c8 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68   In this case th
168c9 65 20 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f  e .** contents o
168ca 66 20 74 68 65 20 65 6e 74 69 72 65 20 74 72 61  f the entire tra
168cb 6e 73 61 63 74 69 6f 6e 20 61 72 65 20 72 6f 6c  nsaction are rol
168cc 6c 65 64 20 62 61 63 6b 2e 20 54 68 69 73 20 69  led back. This i
168cd 73 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66  s different.** f
168ce 72 6f 6d 20 61 20 6e 6f 72 6d 61 6c 20 74 72 61  rom a normal tra
168cf 6e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c 62 61 63  nsaction rollbac
168d0 6b 2c 20 61 73 20 6e 6f 20 6c 6f 63 6b 73 20 61  k, as no locks a
168d1 72 65 20 72 65 6c 65 61 73 65 64 20 61 6e 64 20  re released and 
168d2 74 68 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69  the.** transacti
168d3 6f 6e 20 72 65 6d 61 69 6e 73 20 6f 70 65 6e 2e  on remains open.
168d4 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
168d5 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
168d6 72 65 65 53 61 76 65 70 6f 69 6e 74 28 42 74 72  reeSavepoint(Btr
168d7 65 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69  ee *p, int op, i
168d8 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 29 7b 0a  nt iSavepoint){.
168d9 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
168da 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 20 26 26  E_OK;.  if( p &&
168db 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41   p->inTrans==TRA
168dc 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20  NS_WRITE ){.    
168dd 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
168de 70 2d 3e 70 42 74 3b 0a 20 20 20 20 61 73 73 65  p->pBt;.    asse
168df 72 74 28 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e  rt( op==SAVEPOIN
168e0 54 5f 52 45 4c 45 41 53 45 20 7c 7c 20 6f 70 3d  T_RELEASE || op=
168e1 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42  =SAVEPOINT_ROLLB
168e2 41 43 4b 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ACK );.    asser
168e3 74 28 20 69 53 61 76 65 70 6f 69 6e 74 3e 3d 30  t( iSavepoint>=0
168e4 20 7c 7c 20 28 69 53 61 76 65 70 6f 69 6e 74 3d   || (iSavepoint=
168e5 3d 2d 31 20 26 26 20 6f 70 3d 3d 53 41 56 45 50  =-1 && op==SAVEP
168e6 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 29 20 29  OINT_ROLLBACK) )
168e7 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ;.    sqlite3Btr
168e8 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 20 20  eeEnter(p);.    
168e9 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
168ea 72 53 61 76 65 70 6f 69 6e 74 28 70 42 74 2d 3e  rSavepoint(pBt->
168eb 70 50 61 67 65 72 2c 20 6f 70 2c 20 69 53 61 76  pPager, op, iSav
168ec 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 69 66 28  epoint);.    if(
168ed 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
168ee 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6e 65 77  {.      rc = new
168ef 44 61 74 61 62 61 73 65 28 70 42 74 29 3b 0a 20  Database(pBt);. 
168f0 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
168f1 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
168f2 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
168f3 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20  }../*.** Create 
168f4 61 20 6e 65 77 20 63 75 72 73 6f 72 20 66 6f 72  a new cursor for
168f5 20 74 68 65 20 42 54 72 65 65 20 77 68 6f 73 65   the BTree whose
168f6 20 72 6f 6f 74 20 69 73 20 6f 6e 20 74 68 65 20   root is on the 
168f7 70 61 67 65 0a 2a 2a 20 69 54 61 62 6c 65 2e 20  page.** iTable. 
168f8 49 66 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 63  If a read-only c
168f9 75 72 73 6f 72 20 69 73 20 72 65 71 75 65 73 74  ursor is request
168fa 65 64 2c 20 69 74 20 69 73 20 61 73 73 75 6d 65  ed, it is assume
168fb 64 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 63 61  d that.** the ca
168fc 6c 6c 65 72 20 61 6c 72 65 61 64 79 20 68 61 73  ller already has
168fd 20 61 74 20 6c 65 61 73 74 20 61 20 72 65 61 64   at least a read
168fe 2d 6f 6e 6c 79 20 74 72 61 6e 73 61 63 74 69 6f  -only transactio
168ff 6e 20 6f 70 65 6e 0a 2a 2a 20 6f 6e 20 74 68 65  n open.** on the
16900 20 64 61 74 61 62 61 73 65 20 61 6c 72 65 61 64   database alread
16901 79 2e 20 49 66 20 61 20 77 72 69 74 65 2d 63 75  y. If a write-cu
16902 72 73 6f 72 20 69 73 20 72 65 71 75 65 73 74 65  rsor is requeste
16903 64 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 63  d, then.** the c
16904 61 6c 6c 65 72 20 69 73 20 61 73 73 75 6d 65 64  aller is assumed
16905 20 74 6f 20 68 61 76 65 20 61 6e 20 6f 70 65 6e   to have an open
16906 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
16907 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77 72 46  on..**.** If wrF
16908 6c 61 67 3d 3d 30 2c 20 74 68 65 6e 20 74 68 65  lag==0, then the
16909 20 63 75 72 73 6f 72 20 63 61 6e 20 6f 6e 6c 79   cursor can only
1690a 20 62 65 20 75 73 65 64 20 66 6f 72 20 72 65 61   be used for rea
1690b 64 69 6e 67 2e 0a 2a 2a 20 49 66 20 77 72 46 6c  ding..** If wrFl
1690c 61 67 3d 3d 31 2c 20 74 68 65 6e 20 74 68 65 20  ag==1, then the 
1690d 63 75 72 73 6f 72 20 63 61 6e 20 62 65 20 75 73  cursor can be us
1690e 65 64 20 66 6f 72 20 72 65 61 64 69 6e 67 20 6f  ed for reading o
1690f 72 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e 67  r for.** writing
16910 20 69 66 20 6f 74 68 65 72 20 63 6f 6e 64 69 74   if other condit
16911 69 6f 6e 73 20 66 6f 72 20 77 72 69 74 69 6e 67  ions for writing
16912 20 61 72 65 20 61 6c 73 6f 20 6d 65 74 2e 20 20   are also met.  
16913 54 68 65 73 65 0a 2a 2a 20 61 72 65 20 74 68 65  These.** are the
16914 20 63 6f 6e 64 69 74 69 6f 6e 73 20 74 68 61 74   conditions that
16915 20 6d 75 73 74 20 62 65 20 6d 65 74 20 69 6e 20   must be met in 
16916 6f 72 64 65 72 20 66 6f 72 20 77 72 69 74 69 6e  order for writin
16917 67 20 74 6f 0a 2a 2a 20 62 65 20 61 6c 6c 6f 77  g to.** be allow
16918 65 64 3a 0a 2a 2a 0a 2a 2a 20 31 3a 20 20 54 68  ed:.**.** 1:  Th
16919 65 20 63 75 72 73 6f 72 20 6d 75 73 74 20 68 61  e cursor must ha
1691a 76 65 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 77  ve been opened w
1691b 69 74 68 20 77 72 46 6c 61 67 3d 3d 31 0a 2a 2a  ith wrFlag==1.**
1691c 0a 2a 2a 20 32 3a 20 20 4f 74 68 65 72 20 64 61  .** 2:  Other da
1691d 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1691e 6e 73 20 74 68 61 74 20 73 68 61 72 65 20 74 68  ns that share th
1691f 65 20 73 61 6d 65 20 70 61 67 65 72 20 63 61 63  e same pager cac
16920 68 65 0a 2a 2a 20 20 20 20 20 62 75 74 20 77 68  he.**     but wh
16921 69 63 68 20 61 72 65 20 6e 6f 74 20 69 6e 20 74  ich are not in t
16922 68 65 20 52 45 41 44 5f 55 4e 43 4f 4d 4d 49 54  he READ_UNCOMMIT
16923 54 45 44 20 73 74 61 74 65 20 6d 61 79 20 6e 6f  TED state may no
16924 74 20 68 61 76 65 0a 2a 2a 20 20 20 20 20 63 75  t have.**     cu
16925 72 73 6f 72 73 20 6f 70 65 6e 20 77 69 74 68 20  rsors open with 
16926 77 72 46 6c 61 67 3d 3d 30 20 6f 6e 20 74 68 65  wrFlag==0 on the
16927 20 73 61 6d 65 20 74 61 62 6c 65 2e 20 20 4f 74   same table.  Ot
16928 68 65 72 77 69 73 65 0a 2a 2a 20 20 20 20 20 74  herwise.**     t
16929 68 65 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20  he changes made 
1692a 62 79 20 74 68 69 73 20 77 72 69 74 65 20 63 75  by this write cu
1692b 72 73 6f 72 20 77 6f 75 6c 64 20 62 65 20 76 69  rsor would be vi
1692c 73 69 62 6c 65 20 74 6f 0a 2a 2a 20 20 20 20 20  sible to.**     
1692d 74 68 65 20 72 65 61 64 20 63 75 72 73 6f 72 73  the read cursors
1692e 20 69 6e 20 74 68 65 20 6f 74 68 65 72 20 64 61   in the other da
1692f 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
16930 6e 2e 0a 2a 2a 0a 2a 2a 20 33 3a 20 20 54 68 65  n..**.** 3:  The
16931 20 64 61 74 61 62 61 73 65 20 6d 75 73 74 20 62   database must b
16932 65 20 77 72 69 74 61 62 6c 65 20 28 6e 6f 74 20  e writable (not 
16933 6f 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 65 64  on read-only med
16934 69 61 29 0a 2a 2a 0a 2a 2a 20 34 3a 20 20 54 68  ia).**.** 4:  Th
16935 65 72 65 20 6d 75 73 74 20 62 65 20 61 6e 20 61  ere must be an a
16936 63 74 69 76 65 20 74 72 61 6e 73 61 63 74 69 6f  ctive transactio
16937 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20 63 68 65 63  n..**.** No chec
16938 6b 69 6e 67 20 69 73 20 64 6f 6e 65 20 74 6f 20  king is done to 
16939 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 70  make sure that p
1693a 61 67 65 20 69 54 61 62 6c 65 20 72 65 61 6c 6c  age iTable reall
1693b 79 20 69 73 20 74 68 65 0a 2a 2a 20 72 6f 6f 74  y is the.** root
1693c 20 70 61 67 65 20 6f 66 20 61 20 62 2d 74 72 65   page of a b-tre
1693d 65 2e 20 20 49 66 20 69 74 20 69 73 20 6e 6f 74  e.  If it is not
1693e 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f  , then the curso
1693f 72 20 61 63 71 75 69 72 65 64 0a 2a 2a 20 77 69  r acquired.** wi
16940 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20 63 6f 72 72  ll not work corr
16941 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 74 20  ectly..**.** It 
16942 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 20  is assumed that 
16943 74 68 65 20 73 71 6c 69 74 65 33 42 74 72 65 65  the sqlite3Btree
16944 43 75 72 73 6f 72 53 69 7a 65 28 29 20 62 79 74  CursorSize() byt
16945 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 0a 2a 2a  es of memory .**
16946 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70   pointed to by p
16947 43 75 72 20 68 61 76 65 20 62 65 65 6e 20 7a 65  Cur have been ze
16948 72 6f 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c  roed by the call
16949 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  er..*/.static in
1694a 74 20 62 74 72 65 65 43 75 72 73 6f 72 28 0a 20  t btreeCursor(. 
1694b 20 42 74 72 65 65 20 2a 70 2c 20 20 20 20 20 20   Btree *p,      
1694c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1694d 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62          /* The b
1694e 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 69 54  tree */.  int iT
1694f 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20  able,           
16950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16951 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66   /* Root page of
16952 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a   table to open *
16953 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 2c 20  /.  int wrFlag, 
16954 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16955 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20             /* 1 
16956 74 6f 20 77 72 69 74 65 2e 20 30 20 72 65 61 64  to write. 0 read
16957 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 75 63  -only */.  struc
16958 74 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49  t KeyInfo *pKeyI
16959 6e 66 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20  nfo,            
1695a 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 20 74    /* First arg t
1695b 6f 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e  o comparison fun
1695c 63 74 69 6f 6e 20 2a 2f 0a 20 20 42 74 43 75 72  ction */.  BtCur
1695d 73 6f 72 20 2a 70 43 75 72 20 20 20 20 20 20 20  sor *pCur       
1695e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1695f 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 6e    /* Space for n
16960 65 77 20 63 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a  ew cursor */.){.
16961 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
16962 3d 20 70 2d 3e 70 42 74 3b 20 20 20 20 20 20 20  = p->pBt;       
16963 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68 61 72           /* Shar
16964 65 64 20 62 2d 74 72 65 65 20 68 61 6e 64 6c 65  ed b-tree handle
16965 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73   */..  assert( s
16966 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73  qlite3BtreeHolds
16967 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73  Mutex(p) );.  as
16968 73 65 72 74 28 20 77 72 46 6c 61 67 3d 3d 30 20  sert( wrFlag==0 
16969 7c 7c 20 77 72 46 6c 61 67 3d 3d 31 20 29 3b 0a  || wrFlag==1 );.
1696a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  .  /* The follow
1696b 69 6e 67 20 61 73 73 65 72 74 20 73 74 61 74 65  ing assert state
1696c 6d 65 6e 74 73 20 76 65 72 69 66 79 20 74 68 61  ments verify tha
1696d 74 20 69 66 20 74 68 69 73 20 69 73 20 61 20 73  t if this is a s
1696e 68 61 72 61 62 6c 65 20 0a 20 20 2a 2a 20 62 2d  harable .  ** b-
1696f 74 72 65 65 20 64 61 74 61 62 61 73 65 2c 20 74  tree database, t
16970 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73  he connection is
16971 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 72 65 71   holding the req
16972 75 69 72 65 64 20 74 61 62 6c 65 20 6c 6f 63 6b  uired table lock
16973 73 2c 20 0a 20 20 2a 2a 20 61 6e 64 20 74 68 61  s, .  ** and tha
16974 74 20 6e 6f 20 6f 74 68 65 72 20 63 6f 6e 6e 65  t no other conne
16975 63 74 69 6f 6e 20 68 61 73 20 61 6e 79 20 6f 70  ction has any op
16976 65 6e 20 63 75 72 73 6f 72 20 74 68 61 74 20 63  en cursor that c
16977 6f 6e 66 6c 69 63 74 73 20 77 69 74 68 20 0a 20  onflicts with . 
16978 20 2a 2a 20 74 68 69 73 20 6c 6f 63 6b 2e 20 20   ** this lock.  
16979 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 68 61 73  */.  assert( has
1697a 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65  SharedCacheTable
1697b 4c 6f 63 6b 28 70 2c 20 69 54 61 62 6c 65 2c 20  Lock(p, iTable, 
1697c 70 4b 65 79 49 6e 66 6f 21 3d 30 2c 20 77 72 46  pKeyInfo!=0, wrF
1697d 6c 61 67 2b 31 29 20 29 3b 0a 20 20 61 73 73 65  lag+1) );.  asse
1697e 72 74 28 20 77 72 46 6c 61 67 3d 3d 30 20 7c 7c  rt( wrFlag==0 ||
1697f 20 21 68 61 73 52 65 61 64 43 6f 6e 66 6c 69 63   !hasReadConflic
16980 74 73 28 70 2c 20 69 54 61 62 6c 65 29 20 29 3b  ts(p, iTable) );
16981 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68  ..  /* Assert th
16982 61 74 20 74 68 65 20 63 61 6c 6c 65 72 20 68 61  at the caller ha
16983 73 20 6f 70 65 6e 65 64 20 74 68 65 20 72 65 71  s opened the req
16984 75 69 72 65 64 20 74 72 61 6e 73 61 63 74 69 6f  uired transactio
16985 6e 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  n. */.  assert( 
16986 70 2d 3e 69 6e 54 72 61 6e 73 3e 54 52 41 4e 53  p->inTrans>TRANS
16987 5f 4e 4f 4e 45 20 29 3b 0a 20 20 61 73 73 65 72  _NONE );.  asser
16988 74 28 20 77 72 46 6c 61 67 3d 3d 30 20 7c 7c 20  t( wrFlag==0 || 
16989 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e  p->inTrans==TRAN
1698a 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73  S_WRITE );.  ass
1698b 65 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31  ert( pBt->pPage1
1698c 20 26 26 20 70 42 74 2d 3e 70 50 61 67 65 31 2d   && pBt->pPage1-
1698d 3e 61 44 61 74 61 20 29 3b 0a 0a 20 20 69 66 28  >aData );..  if(
1698e 20 4e 45 56 45 52 28 77 72 46 6c 61 67 20 26 26   NEVER(wrFlag &&
1698f 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 29 20   pBt->readOnly) 
16990 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
16991 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20  LITE_READONLY;. 
16992 20 7d 0a 20 20 69 66 28 20 69 54 61 62 6c 65 3d   }.  if( iTable=
16993 3d 31 20 26 26 20 70 61 67 65 72 50 61 67 65 63  =1 && pagerPagec
16994 6f 75 6e 74 28 70 42 74 29 3d 3d 30 20 29 7b 0a  ount(pBt)==0 ){.
16995 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
16996 45 5f 45 4d 50 54 59 3b 0a 20 20 7d 0a 0a 20 20  E_EMPTY;.  }..  
16997 2f 2a 20 4e 6f 77 20 74 68 61 74 20 6e 6f 20 6f  /* Now that no o
16998 74 68 65 72 20 65 72 72 6f 72 73 20 63 61 6e 20  ther errors can 
16999 6f 63 63 75 72 2c 20 66 69 6e 69 73 68 20 66 69  occur, finish fi
1699a 6c 6c 69 6e 67 20 69 6e 20 74 68 65 20 42 74 43  lling in the BtC
1699b 75 72 73 6f 72 0a 20 20 2a 2a 20 76 61 72 69 61  ursor.  ** varia
1699c 62 6c 65 73 20 61 6e 64 20 6c 69 6e 6b 20 74 68  bles and link th
1699d 65 20 63 75 72 73 6f 72 20 69 6e 74 6f 20 74 68  e cursor into th
1699e 65 20 42 74 53 68 61 72 65 64 20 6c 69 73 74 2e  e BtShared list.
1699f 20 20 2a 2f 0a 20 20 70 43 75 72 2d 3e 70 67 6e    */.  pCur->pgn
169a0 6f 52 6f 6f 74 20 3d 20 28 50 67 6e 6f 29 69 54  oRoot = (Pgno)iT
169a1 61 62 6c 65 3b 0a 20 20 70 43 75 72 2d 3e 69 50  able;.  pCur->iP
169a2 61 67 65 20 3d 20 2d 31 3b 0a 20 20 70 43 75 72  age = -1;.  pCur
169a3 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4b 65  ->pKeyInfo = pKe
169a4 79 49 6e 66 6f 3b 0a 20 20 70 43 75 72 2d 3e 70  yInfo;.  pCur->p
169a5 42 74 72 65 65 20 3d 20 70 3b 0a 20 20 70 43 75  Btree = p;.  pCu
169a6 72 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20  r->pBt = pBt;.  
169a7 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 3d 20 28  pCur->wrFlag = (
169a8 75 38 29 77 72 46 6c 61 67 3b 0a 20 20 70 43 75  u8)wrFlag;.  pCu
169a9 72 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e  r->pNext = pBt->
169aa 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 70  pCursor;.  if( p
169ab 43 75 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20  Cur->pNext ){.  
169ac 20 20 70 43 75 72 2d 3e 70 4e 65 78 74 2d 3e 70    pCur->pNext->p
169ad 50 72 65 76 20 3d 20 70 43 75 72 3b 0a 20 20 7d  Prev = pCur;.  }
169ae 0a 20 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 20  .  pBt->pCursor 
169af 3d 20 70 43 75 72 3b 0a 20 20 70 43 75 72 2d 3e  = pCur;.  pCur->
169b0 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f  eState = CURSOR_
169b1 49 4e 56 41 4c 49 44 3b 0a 20 20 70 43 75 72 2d  INVALID;.  pCur-
169b2 3e 63 61 63 68 65 64 52 6f 77 69 64 20 3d 20 30  >cachedRowid = 0
169b3 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
169b4 45 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  E_OK;.}.SQLITE_P
169b5 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
169b6 65 33 42 74 72 65 65 43 75 72 73 6f 72 28 0a 20  e3BtreeCursor(. 
169b7 20 42 74 72 65 65 20 2a 70 2c 20 20 20 20 20 20   Btree *p,      
169b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
169b9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
169ba 54 68 65 20 62 74 72 65 65 20 2a 2f 0a 20 20 69  The btree */.  i
169bb 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20  nt iTable,      
169bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
169bd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f             /* Ro
169be 6f 74 20 70 61 67 65 20 6f 66 20 74 61 62 6c 65  ot page of table
169bf 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e   to open */.  in
169c0 74 20 77 72 46 6c 61 67 2c 20 20 20 20 20 20 20  t wrFlag,       
169c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
169c2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 74            /* 1 t
169c3 6f 20 77 72 69 74 65 2e 20 30 20 72 65 61 64 2d  o write. 0 read-
169c4 6f 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 75 63 74  only */.  struct
169c5 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e   KeyInfo *pKeyIn
169c6 66 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  fo,             
169c7 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61        /* First a
169c8 72 67 20 74 6f 20 78 43 6f 6d 70 61 72 65 28 29  rg to xCompare()
169c9 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a   */.  BtCursor *
169ca 70 43 75 72 20 20 20 20 20 20 20 20 20 20 20 20  pCur            
169cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
169cc 20 20 2f 2a 20 57 72 69 74 65 20 6e 65 77 20 63    /* Write new c
169cd 75 72 73 6f 72 20 68 65 72 65 20 2a 2f 0a 29 7b  ursor here */.){
169ce 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c  .  int rc;.  sql
169cf 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
169d0 29 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65 43  );.  rc = btreeC
169d1 75 72 73 6f 72 28 70 2c 20 69 54 61 62 6c 65 2c  ursor(p, iTable,
169d2 20 77 72 46 6c 61 67 2c 20 70 4b 65 79 49 6e 66   wrFlag, pKeyInf
169d3 6f 2c 20 70 43 75 72 29 3b 0a 20 20 73 71 6c 69  o, pCur);.  sqli
169d4 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
169d5 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
169d6 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
169d7 68 65 20 73 69 7a 65 20 6f 66 20 61 20 42 74 43  he size of a BtC
169d8 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 69 6e 20  ursor object in 
169d9 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  bytes..**.** Thi
169da 73 20 69 6e 74 65 72 66 61 63 65 73 20 69 73 20  s interfaces is 
169db 6e 65 65 64 65 64 20 73 6f 20 74 68 61 74 20 75  needed so that u
169dc 73 65 72 73 20 6f 66 20 63 75 72 73 6f 72 73 20  sers of cursors 
169dd 63 61 6e 20 70 72 65 61 6c 6c 6f 63 61 74 65 0a  can preallocate.
169de 2a 2a 20 73 75 66 66 69 63 69 65 6e 74 20 73 74  ** sufficient st
169df 6f 72 61 67 65 20 74 6f 20 68 6f 6c 64 20 61 20  orage to hold a 
169e0 63 75 72 73 6f 72 2e 20 20 54 68 65 20 42 74 43  cursor.  The BtC
169e1 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 69 73 20  ursor object is 
169e2 6f 70 61 71 75 65 0a 2a 2a 20 74 6f 20 75 73 65  opaque.** to use
169e3 72 73 20 73 6f 20 74 68 65 79 20 63 61 6e 6e 6f  rs so they canno
169e4 74 20 64 6f 20 74 68 65 20 73 69 7a 65 6f 66 28  t do the sizeof(
169e5 29 20 74 68 65 6d 73 65 6c 76 65 73 20 2d 20 74  ) themselves - t
169e6 68 65 79 20 6d 75 73 74 20 63 61 6c 6c 0a 2a 2a  hey must call.**
169e7 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a   this routine..*
169e8 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
169e9 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
169ea 65 43 75 72 73 6f 72 53 69 7a 65 28 76 6f 69 64  eCursorSize(void
169eb 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 69 7a 65  ){.  return size
169ec 6f 66 28 42 74 43 75 72 73 6f 72 29 3b 0a 7d 0a  of(BtCursor);.}.
169ed 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 63  ./*.** Set the c
169ee 61 63 68 65 64 20 72 6f 77 69 64 20 76 61 6c 75  ached rowid valu
169ef 65 20 6f 66 20 65 76 65 72 79 20 63 75 72 73 6f  e of every curso
169f0 72 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 61  r in the same da
169f1 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 61  tabase file.** a
169f2 73 20 70 43 75 72 20 61 6e 64 20 68 61 76 69 6e  s pCur and havin
169f3 67 20 74 68 65 20 73 61 6d 65 20 72 6f 6f 74 20  g the same root 
169f4 70 61 67 65 20 6e 75 6d 62 65 72 20 61 73 20 70  page number as p
169f5 43 75 72 2e 20 20 54 68 65 20 76 61 6c 75 65 20  Cur.  The value 
169f6 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 69 52 6f  is.** set to iRo
169f7 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20  wid..**.** Only 
169f8 70 6f 73 69 74 69 76 65 20 72 6f 77 69 64 20 76  positive rowid v
169f9 61 6c 75 65 73 20 61 72 65 20 63 6f 6e 73 69 64  alues are consid
169fa 65 72 65 64 20 76 61 6c 69 64 20 66 6f 72 20 74  ered valid for t
169fb 68 69 73 20 63 61 63 68 65 2e 0a 2a 2a 20 54 68  his cache..** Th
169fc 65 20 63 61 63 68 65 20 69 73 20 69 6e 69 74 69  e cache is initi
169fd 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 6f 2c 20  alized to zero, 
169fe 69 6e 64 69 63 61 74 69 6e 67 20 61 6e 20 69 6e  indicating an in
169ff 76 61 6c 69 64 20 63 61 63 68 65 2e 0a 2a 2a 20  valid cache..** 
16a00 41 20 62 74 72 65 65 20 77 69 6c 6c 20 77 6f 72  A btree will wor
16a01 6b 20 66 69 6e 65 20 77 69 74 68 20 7a 65 72 6f  k fine with zero
16a02 20 6f 72 20 6e 65 67 61 74 69 76 65 20 72 6f 77   or negative row
16a03 69 64 73 2e 20 20 57 65 20 6a 75 73 74 20 63 61  ids.  We just ca
16a04 6e 6e 6f 74 0a 2a 2a 20 63 61 63 68 65 20 7a 65  nnot.** cache ze
16a05 72 6f 20 6f 72 20 6e 65 67 61 74 69 76 65 20 72  ro or negative r
16a06 6f 77 69 64 73 2c 20 77 68 69 63 68 20 6d 65 61  owids, which mea
16a07 6e 73 20 74 61 62 6c 65 73 20 74 68 61 74 20 75  ns tables that u
16a08 73 65 20 7a 65 72 6f 20 6f 72 0a 2a 2a 20 6e 65  se zero or.** ne
16a09 67 61 74 69 76 65 20 72 6f 77 69 64 73 20 6d 69  gative rowids mi
16a0a 67 68 74 20 72 75 6e 20 61 20 6c 69 74 74 6c 65  ght run a little
16a0b 20 73 6c 6f 77 65 72 2e 20 20 42 75 74 20 69 6e   slower.  But in
16a0c 20 70 72 61 63 74 69 63 65 2c 20 7a 65 72 6f 0a   practice, zero.
16a0d 2a 2a 20 6f 72 20 6e 65 67 61 74 69 76 65 20 72  ** or negative r
16a0e 6f 77 69 64 73 20 61 72 65 20 76 65 72 79 20 75  owids are very u
16a0f 6e 63 6f 6d 6d 6f 6e 20 73 6f 20 74 68 69 73 20  ncommon so this 
16a10 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 20  should not be a 
16a11 70 72 6f 62 6c 65 6d 2e 0a 2a 2f 0a 53 51 4c 49  problem..*/.SQLI
16a12 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
16a13 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43  sqlite3BtreeSetC
16a14 61 63 68 65 64 52 6f 77 69 64 28 42 74 43 75 72  achedRowid(BtCur
16a15 73 6f 72 20 2a 70 43 75 72 2c 20 73 71 6c 69 74  sor *pCur, sqlit
16a16 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 69 64 29  e3_int64 iRowid)
16a17 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b  {.  BtCursor *p;
16a18 0a 20 20 66 6f 72 28 70 3d 70 43 75 72 2d 3e 70  .  for(p=pCur->p
16a19 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20  Bt->pCursor; p; 
16a1a 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  p=p->pNext){.   
16a1b 20 69 66 28 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74   if( p->pgnoRoot
16a1c 3d 3d 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74  ==pCur->pgnoRoot
16a1d 20 29 20 70 2d 3e 63 61 63 68 65 64 52 6f 77 69   ) p->cachedRowi
16a1e 64 20 3d 20 69 52 6f 77 69 64 3b 0a 20 20 7d 0a  d = iRowid;.  }.
16a1f 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
16a20 63 61 63 68 65 64 52 6f 77 69 64 3d 3d 69 52 6f  cachedRowid==iRo
16a21 77 69 64 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  wid );.}../*.** 
16a22 52 65 74 75 72 6e 20 74 68 65 20 63 61 63 68 65  Return the cache
16a23 64 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20  d rowid for the 
16a24 67 69 76 65 6e 20 63 75 72 73 6f 72 2e 20 20 41  given cursor.  A
16a25 20 6e 65 67 61 74 69 76 65 20 6f 72 20 7a 65 72   negative or zer
16a26 6f 0a 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c 75  o.** return valu
16a27 65 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74  e indicates that
16a28 20 74 68 65 20 72 6f 77 69 64 20 63 61 63 68 65   the rowid cache
16a29 20 69 73 20 69 6e 76 61 6c 69 64 20 61 6e 64 20   is invalid and 
16a2a 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 69 67 6e  should be.** ign
16a2b 6f 72 65 64 2e 20 20 49 66 20 74 68 65 20 72 6f  ored.  If the ro
16a2c 77 69 64 20 63 61 63 68 65 20 68 61 73 20 6e 65  wid cache has ne
16a2d 76 65 72 20 62 65 66 6f 72 65 20 62 65 65 6e 20  ver before been 
16a2e 73 65 74 2c 20 74 68 65 6e 20 61 0a 2a 2a 20 7a  set, then a.** z
16a2f 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ero is returned.
16a30 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
16a31 54 45 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  TE sqlite3_int64
16a32 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
16a33 43 61 63 68 65 64 52 6f 77 69 64 28 42 74 43 75  CachedRowid(BtCu
16a34 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 72  rsor *pCur){.  r
16a35 65 74 75 72 6e 20 70 43 75 72 2d 3e 63 61 63 68  eturn pCur->cach
16a36 65 64 52 6f 77 69 64 3b 0a 7d 0a 0a 2f 2a 0a 2a  edRowid;.}../*.*
16a37 2a 20 43 6c 6f 73 65 20 61 20 63 75 72 73 6f 72  * Close a cursor
16a38 2e 20 20 54 68 65 20 72 65 61 64 20 6c 6f 63 6b  .  The read lock
16a39 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
16a3a 20 66 69 6c 65 20 69 73 20 72 65 6c 65 61 73 65   file is release
16a3b 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 6c 61  d.** when the la
16a3c 73 74 20 63 75 72 73 6f 72 20 69 73 20 63 6c 6f  st cursor is clo
16a3d 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  sed..*/.SQLITE_P
16a3e 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
16a3f 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72 73  e3BtreeCloseCurs
16a40 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  or(BtCursor *pCu
16a41 72 29 7b 0a 20 20 42 74 72 65 65 20 2a 70 42 74  r){.  Btree *pBt
16a42 72 65 65 20 3d 20 70 43 75 72 2d 3e 70 42 74 72  ree = pCur->pBtr
16a43 65 65 3b 0a 20 20 69 66 28 20 70 42 74 72 65 65  ee;.  if( pBtree
16a44 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   ){.    int i;. 
16a45 20 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74     BtShared *pBt
16a46 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 0a 20 20   = pCur->pBt;.  
16a47 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
16a48 74 65 72 28 70 42 74 72 65 65 29 3b 0a 20 20 20  ter(pBtree);.   
16a49 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65   sqlite3BtreeCle
16a4a 61 72 43 75 72 73 6f 72 28 70 43 75 72 29 3b 0a  arCursor(pCur);.
16a4b 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 70 50      if( pCur->pP
16a4c 72 65 76 20 29 7b 0a 20 20 20 20 20 20 70 43 75  rev ){.      pCu
16a4d 72 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20  r->pPrev->pNext 
16a4e 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b 0a 20  = pCur->pNext;. 
16a4f 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
16a50 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20 70  pBt->pCursor = p
16a51 43 75 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  Cur->pNext;.    
16a52 7d 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e  }.    if( pCur->
16a53 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 70  pNext ){.      p
16a54 43 75 72 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65  Cur->pNext->pPre
16a55 76 20 3d 20 70 43 75 72 2d 3e 70 50 72 65 76 3b  v = pCur->pPrev;
16a56 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69  .    }.    for(i
16a57 3d 30 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61  =0; i<=pCur->iPa
16a58 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ge; i++){.      
16a59 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 75 72  releasePage(pCur
16a5a 2d 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20  ->apPage[i]);.  
16a5b 20 20 7d 0a 20 20 20 20 75 6e 6c 6f 63 6b 42 74    }.    unlockBt
16a5c 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29  reeIfUnused(pBt)
16a5d 3b 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65  ;.    invalidate
16a5e 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 43  OverflowCache(pC
16a5f 75 72 29 3b 0a 20 20 20 20 2f 2a 20 73 71 6c 69  ur);.    /* sqli
16a60 74 65 33 5f 66 72 65 65 28 70 43 75 72 29 3b 20  te3_free(pCur); 
16a61 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  */.    sqlite3Bt
16a62 72 65 65 4c 65 61 76 65 28 70 42 74 72 65 65 29  reeLeave(pBtree)
16a63 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
16a64 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
16a65 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65  ** Make sure the
16a66 20 42 74 43 75 72 73 6f 72 2a 20 67 69 76 65 6e   BtCursor* given
16a67 20 69 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74   in the argument
16a68 20 68 61 73 20 61 20 76 61 6c 69 64 0a 2a 2a 20   has a valid.** 
16a69 42 74 43 75 72 73 6f 72 2e 69 6e 66 6f 20 73 74  BtCursor.info st
16a6a 72 75 63 74 75 72 65 2e 20 20 49 66 20 69 74 20  ructure.  If it 
16a6b 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 76  is not already v
16a6c 61 6c 69 64 2c 20 63 61 6c 6c 0a 2a 2a 20 62 74  alid, call.** bt
16a6d 72 65 65 50 61 72 73 65 43 65 6c 6c 28 29 20 74  reeParseCell() t
16a6e 6f 20 66 69 6c 6c 20 69 74 20 69 6e 2e 0a 2a 2a  o fill it in..**
16a6f 0a 2a 2a 20 42 74 43 75 72 73 6f 72 2e 69 6e 66  .** BtCursor.inf
16a70 6f 20 69 73 20 61 20 63 61 63 68 65 20 6f 66 20  o is a cache of 
16a71 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  the information 
16a72 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 63  in the current c
16a73 65 6c 6c 2e 0a 2a 2a 20 55 73 69 6e 67 20 74 68  ell..** Using th
16a74 69 73 20 63 61 63 68 65 20 72 65 64 75 63 65 73  is cache reduces
16a75 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
16a76 61 6c 6c 73 20 74 6f 20 62 74 72 65 65 50 61 72  alls to btreePar
16a77 73 65 43 65 6c 6c 28 29 2e 0a 2a 2a 0a 2a 2a 20  seCell()..**.** 
16a78 32 30 30 37 2d 30 36 2d 32 35 3a 20 20 54 68 65  2007-06-25:  The
16a79 72 65 20 69 73 20 61 20 62 75 67 20 69 6e 20 73  re is a bug in s
16a7a 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  ome versions of 
16a7b 4d 53 56 43 20 74 68 61 74 20 63 61 75 73 65 20  MSVC that cause 
16a7c 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 20  the.** compiler 
16a7d 74 6f 20 63 72 61 73 68 20 77 68 65 6e 20 67 65  to crash when ge
16a7e 74 43 65 6c 6c 49 6e 66 6f 28 29 20 69 73 20 69  tCellInfo() is i
16a7f 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 20  mplemented as a 
16a80 6d 61 63 72 6f 2e 0a 2a 2a 20 42 75 74 20 74 68  macro..** But th
16a81 65 72 65 20 69 73 20 61 20 6d 65 61 73 75 72 65  ere is a measure
16a82 61 62 6c 65 20 73 70 65 65 64 20 61 64 76 61 6e  able speed advan
16a83 74 61 67 65 20 74 6f 20 75 73 69 6e 67 20 74 68  tage to using th
16a84 65 20 6d 61 63 72 6f 20 6f 6e 20 67 63 63 0a 2a  e macro on gcc.*
16a85 2a 20 28 77 68 65 6e 20 6c 65 73 73 20 63 6f 6d  * (when less com
16a86 70 69 6c 65 72 20 6f 70 74 69 6d 69 7a 61 74 69  piler optimizati
16a87 6f 6e 73 20 6c 69 6b 65 20 2d 4f 73 20 6f 72 20  ons like -Os or 
16a88 2d 4f 30 20 61 72 65 20 75 73 65 64 20 61 6e 64  -O0 are used and
16a89 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72   the.** compiler
16a8a 20 69 73 20 6e 6f 74 20 64 6f 69 6e 67 20 61 67   is not doing ag
16a8b 72 65 73 73 69 76 65 20 69 6e 6c 69 6e 69 6e 67  ressive inlining
16a8c 2e 29 20 20 53 6f 20 77 65 20 75 73 65 20 61 20  .)  So we use a 
16a8d 72 65 61 6c 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a  real function.**
16a8e 20 66 6f 72 20 4d 53 56 43 20 61 6e 64 20 61 20   for MSVC and a 
16a8f 6d 61 63 72 6f 20 66 6f 72 20 65 76 65 72 79 74  macro for everyt
16a90 68 69 6e 67 20 65 6c 73 65 2e 20 20 54 69 63 6b  hing else.  Tick
16a91 65 74 20 23 32 34 35 37 2e 0a 2a 2f 0a 23 69 66  et #2457..*/.#if
16a92 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 73 74  ndef NDEBUG.  st
16a93 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 72 74  atic void assert
16a94 43 65 6c 6c 49 6e 66 6f 28 42 74 43 75 72 73 6f  CellInfo(BtCurso
16a95 72 20 2a 70 43 75 72 29 7b 0a 20 20 20 20 43 65  r *pCur){.    Ce
16a96 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 20  llInfo info;.   
16a97 20 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75   int iPage = pCu
16a98 72 2d 3e 69 50 61 67 65 3b 0a 20 20 20 20 6d 65  r->iPage;.    me
16a99 6d 73 65 74 28 26 69 6e 66 6f 2c 20 30 2c 20 73  mset(&info, 0, s
16a9a 69 7a 65 6f 66 28 69 6e 66 6f 29 29 3b 0a 20 20  izeof(info));.  
16a9b 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c    btreeParseCell
16a9c 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50  (pCur->apPage[iP
16a9d 61 67 65 5d 2c 20 70 43 75 72 2d 3e 61 69 49 64  age], pCur->aiId
16a9e 78 5b 69 50 61 67 65 5d 2c 20 26 69 6e 66 6f 29  x[iPage], &info)
16a9f 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d 65  ;.    assert( me
16aa0 6d 63 6d 70 28 26 69 6e 66 6f 2c 20 26 70 43 75  mcmp(&info, &pCu
16aa1 72 2d 3e 69 6e 66 6f 2c 20 73 69 7a 65 6f 66 28  r->info, sizeof(
16aa2 69 6e 66 6f 29 29 3d 3d 30 20 29 3b 0a 20 20 7d  info))==0 );.  }
16aa3 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65  .#else.  #define
16aa4 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28   assertCellInfo(
16aa5 78 29 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66  x).#endif.#ifdef
16aa6 20 5f 4d 53 43 5f 56 45 52 0a 20 20 2f 2a 20 55   _MSC_VER.  /* U
16aa7 73 65 20 61 20 72 65 61 6c 20 66 75 6e 63 74 69  se a real functi
16aa8 6f 6e 20 69 6e 20 4d 53 56 43 20 74 6f 20 77 6f  on in MSVC to wo
16aa9 72 6b 20 61 72 6f 75 6e 64 20 62 75 67 73 20 69  rk around bugs i
16aaa 6e 20 74 68 61 74 20 63 6f 6d 70 69 6c 65 72 2e  n that compiler.
16aab 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 76 6f 69   */.  static voi
16aac 64 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 42 74  d getCellInfo(Bt
16aad 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20  Cursor *pCur){. 
16aae 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 6e 66     if( pCur->inf
16aaf 6f 2e 6e 53 69 7a 65 3d 3d 30 20 29 7b 0a 20 20  o.nSize==0 ){.  
16ab0 20 20 20 20 69 6e 74 20 69 50 61 67 65 20 3d 20      int iPage = 
16ab1 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 20  pCur->iPage;.   
16ab2 20 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c     btreeParseCel
16ab3 6c 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69  l(pCur->apPage[i
16ab4 50 61 67 65 5d 2c 70 43 75 72 2d 3e 61 69 49 64  Page],pCur->aiId
16ab5 78 5b 69 50 61 67 65 5d 2c 26 70 43 75 72 2d 3e  x[iPage],&pCur->
16ab6 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20 70 43 75  info);.      pCu
16ab7 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31  r->validNKey = 1
16ab8 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
16ab9 20 20 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66     assertCellInf
16aba 6f 28 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20  o(pCur);.    }. 
16abb 20 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 6e   }.#else /* if n
16abc 6f 74 20 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a 20  ot _MSC_VER */. 
16abd 20 2f 2a 20 55 73 65 20 61 20 6d 61 63 72 6f 20   /* Use a macro 
16abe 69 6e 20 61 6c 6c 20 6f 74 68 65 72 20 63 6f 6d  in all other com
16abf 70 69 6c 65 72 73 20 73 6f 20 74 68 61 74 20 74  pilers so that t
16ac0 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69  he function is i
16ac1 6e 6c 69 6e 65 64 20 2a 2f 0a 23 64 65 66 69 6e  nlined */.#defin
16ac2 65 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43  e getCellInfo(pC
16ac3 75 72 29 20 20 20 20 20 20 20 20 20 20 20 20 20  ur)             
16ac4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16ac5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16ac6 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28           \.  if(
16ac7 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a   pCur->info.nSiz
16ac8 65 3d 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20  e==0 ){         
16ac9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16aca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16acb 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
16acc 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75 72  int iPage = pCur
16acd 2d 3e 69 50 61 67 65 3b 20 20 20 20 20 20 20 20  ->iPage;        
16ace 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16acf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16ad0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
16ad1 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28   btreeParseCell(
16ad2 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61  pCur->apPage[iPa
16ad3 67 65 5d 2c 70 43 75 72 2d 3e 61 69 49 64 78 5b  ge],pCur->aiIdx[
16ad4 69 50 61 67 65 5d 2c 26 70 43 75 72 2d 3e 69 6e  iPage],&pCur->in
16ad5 66 6f 29 3b 20 5c 0a 20 20 20 20 70 43 75 72 2d  fo); \.    pCur-
16ad6 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 20  >validNKey = 1; 
16ad7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16ad8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16ad9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16ada 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b        \.  }else{
16adb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16adc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16add 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16ade 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16adf 20 20 20 20 20 20 20 5c 0a 20 20 20 20 61 73 73         \.    ass
16ae0 65 72 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72  ertCellInfo(pCur
16ae1 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
16ae2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16ae3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16ae4 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 23 65          \.  }.#e
16ae5 6e 64 69 66 20 2f 2a 20 5f 4d 53 43 5f 56 45 52  ndif /* _MSC_VER
16ae6 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45   */..#ifndef NDE
16ae7 42 55 47 20 20 2f 2a 20 54 68 65 20 6e 65 78 74  BUG  /* The next
16ae8 20 72 6f 75 74 69 6e 65 20 75 73 65 64 20 6f 6e   routine used on
16ae9 6c 79 20 77 69 74 68 69 6e 20 61 73 73 65 72 74  ly within assert
16aea 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f  () statements */
16aeb 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72  ./*.** Return tr
16aec 75 65 20 69 66 20 74 68 65 20 67 69 76 65 6e 20  ue if the given 
16aed 42 74 43 75 72 73 6f 72 20 69 73 20 76 61 6c 69  BtCursor is vali
16aee 64 2e 20 20 41 20 76 61 6c 69 64 20 63 75 72 73  d.  A valid curs
16aef 6f 72 20 69 73 20 6f 6e 65 0a 2a 2a 20 74 68 61  or is one.** tha
16af0 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70  t is currently p
16af1 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 72 6f 77  ointing to a row
16af2 20 69 6e 20 61 20 28 6e 6f 6e 2d 65 6d 70 74 79   in a (non-empty
16af3 29 20 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 69 73  ) table..** This
16af4 20 69 73 20 61 20 76 65 72 69 66 69 63 61 74 69   is a verificati
16af5 6f 6e 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73  on routine is us
16af6 65 64 20 6f 6e 6c 79 20 77 69 74 68 69 6e 20 61  ed only within a
16af7 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e  ssert() statemen
16af8 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ts..*/.SQLITE_PR
16af9 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
16afa 33 42 74 72 65 65 43 75 72 73 6f 72 49 73 56 61  3BtreeCursorIsVa
16afb 6c 69 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43  lid(BtCursor *pC
16afc 75 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 43  ur){.  return pC
16afd 75 72 20 26 26 20 70 43 75 72 2d 3e 65 53 74 61  ur && pCur->eSta
16afe 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44  te==CURSOR_VALID
16aff 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44  ;.}.#endif /* ND
16b00 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53  EBUG */../*.** S
16b01 65 74 20 2a 70 53 69 7a 65 20 74 6f 20 74 68 65  et *pSize to the
16b02 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 75 66   size of the buf
16b03 66 65 72 20 6e 65 65 64 65 64 20 74 6f 20 68 6f  fer needed to ho
16b04 6c 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 0a  ld the value of.
16b05 2a 2a 20 74 68 65 20 6b 65 79 20 66 6f 72 20 74  ** the key for t
16b06 68 65 20 63 75 72 72 65 6e 74 20 65 6e 74 72 79  he current entry
16b07 2e 20 20 49 66 20 74 68 65 20 63 75 72 73 6f 72  .  If the cursor
16b08 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67   is not pointing
16b09 0a 2a 2a 20 74 6f 20 61 20 76 61 6c 69 64 20 65  .** to a valid e
16b0a 6e 74 72 79 2c 20 2a 70 53 69 7a 65 20 69 73 20  ntry, *pSize is 
16b0b 73 65 74 20 74 6f 20 30 2e 20 0a 2a 2a 0a 2a 2a  set to 0. .**.**
16b0c 20 46 6f 72 20 61 20 74 61 62 6c 65 20 77 69 74   For a table wit
16b0d 68 20 74 68 65 20 49 4e 54 4b 45 59 20 66 6c 61  h the INTKEY fla
16b0e 67 20 73 65 74 2c 20 74 68 69 73 20 72 6f 75 74  g set, this rout
16b0f 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20  ine returns the 
16b10 6b 65 79 0a 2a 2a 20 69 74 73 65 6c 66 2c 20 6e  key.** itself, n
16b11 6f 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ot the number of
16b12 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 6b 65   bytes in the ke
16b13 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c  y..**.** The cal
16b14 6c 65 72 20 6d 75 73 74 20 70 6f 73 69 74 69 6f  ler must positio
16b15 6e 20 74 68 65 20 63 75 72 73 6f 72 20 70 72 69  n the cursor pri
16b16 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74  or to invoking t
16b17 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 20  his routine..** 
16b18 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
16b19 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 20 49   cannot fail.  I
16b1a 74 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73  t always returns
16b1b 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 20 0a 2a 2f   SQLITE_OK.  .*/
16b1c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
16b1d 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
16b1e 4b 65 79 53 69 7a 65 28 42 74 43 75 72 73 6f 72  KeySize(BtCursor
16b1f 20 2a 70 43 75 72 2c 20 69 36 34 20 2a 70 53 69   *pCur, i64 *pSi
16b20 7a 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63  ze){.  assert( c
16b21 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
16b22 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72  pCur) );.  asser
16b23 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  t( pCur->eState=
16b24 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20  =CURSOR_INVALID 
16b25 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  || pCur->eState=
16b26 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b  =CURSOR_VALID );
16b27 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74  .  if( pCur->eSt
16b28 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49  ate!=CURSOR_VALI
16b29 44 20 29 7b 0a 20 20 20 20 2a 70 53 69 7a 65 20  D ){.    *pSize 
16b2a 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  = 0;.  }else{.  
16b2b 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43    getCellInfo(pC
16b2c 75 72 29 3b 0a 20 20 20 20 2a 70 53 69 7a 65 20  ur);.    *pSize 
16b2d 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65  = pCur->info.nKe
16b2e 79 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  y;.  }.  return 
16b2f 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
16b30 0a 2a 2a 20 53 65 74 20 2a 70 53 69 7a 65 20 74  .** Set *pSize t
16b31 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  o the number of 
16b32 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 69 6e  bytes of data in
16b33 20 74 68 65 20 65 6e 74 72 79 20 74 68 65 0a 2a   the entry the.*
16b34 2a 20 63 75 72 73 6f 72 20 63 75 72 72 65 6e 74  * cursor current
16b35 6c 79 20 70 6f 69 6e 74 73 20 74 6f 2e 0a 2a 2a  ly points to..**
16b36 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 6d  .** The caller m
16b37 75 73 74 20 67 75 61 72 61 6e 74 65 65 20 74 68  ust guarantee th
16b38 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  at the cursor is
16b39 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 6e   pointing to a n
16b3a 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20 76 61 6c 69 64  on-NULL.** valid
16b3b 20 65 6e 74 72 79 2e 20 20 49 6e 20 6f 74 68 65   entry.  In othe
16b3c 72 20 77 6f 72 64 73 2c 20 74 68 65 20 63 61 6c  r words, the cal
16b3d 6c 69 6e 67 20 70 72 6f 63 65 64 75 72 65 20 6d  ling procedure m
16b3e 75 73 74 20 67 75 61 72 61 6e 74 65 65 0a 2a 2a  ust guarantee.**
16b3f 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72   that the cursor
16b40 20 68 61 73 20 43 75 72 73 6f 72 2e 65 53 74 61   has Cursor.eSta
16b41 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44  te==CURSOR_VALID
16b42 2e 0a 2a 2a 0a 2a 2a 20 46 61 69 6c 75 72 65 20  ..**.** Failure 
16b43 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2e  is not possible.
16b44 20 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20    This function 
16b45 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 53  always returns S
16b46 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 20 49 74 20  QLITE_OK..** It 
16b47 6d 69 67 68 74 20 6a 75 73 74 20 61 73 20 77 65  might just as we
16b48 6c 6c 20 62 65 20 61 20 70 72 6f 63 65 64 75 72  ll be a procedur
16b49 65 20 28 72 65 74 75 72 6e 69 6e 67 20 76 6f 69  e (returning voi
16b4a 64 29 20 62 75 74 20 77 65 20 63 6f 6e 74 69 6e  d) but we contin
16b4b 75 65 0a 2a 2a 20 74 6f 20 72 65 74 75 72 6e 20  ue.** to return 
16b4c 61 6e 20 69 6e 74 65 67 65 72 20 72 65 73 75 6c  an integer resul
16b4d 74 20 63 6f 64 65 20 66 6f 72 20 68 69 73 74 6f  t code for histo
16b4e 72 69 63 61 6c 20 72 65 61 73 6f 6e 73 2e 0a 2a  rical reasons..*
16b4f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
16b50 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
16b51 65 44 61 74 61 53 69 7a 65 28 42 74 43 75 72 73  eDataSize(BtCurs
16b52 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 20 2a 70  or *pCur, u32 *p
16b53 53 69 7a 65 29 7b 0a 20 20 61 73 73 65 72 74 28  Size){.  assert(
16b54 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
16b55 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73  x(pCur) );.  ass
16b56 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74  ert( pCur->eStat
16b57 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20  e==CURSOR_VALID 
16b58 29 3b 0a 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f  );.  getCellInfo
16b59 28 70 43 75 72 29 3b 0a 20 20 2a 70 53 69 7a 65  (pCur);.  *pSize
16b5a 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44   = pCur->info.nD
16b5b 61 74 61 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  ata;.  return SQ
16b5c 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
16b5d 2a 20 47 69 76 65 6e 20 74 68 65 20 70 61 67 65  * Given the page
16b5e 20 6e 75 6d 62 65 72 20 6f 66 20 61 6e 20 6f 76   number of an ov
16b5f 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 20 74  erflow page in t
16b60 68 65 20 64 61 74 61 62 61 73 65 20 28 70 61 72  he database (par
16b61 61 6d 65 74 65 72 0a 2a 2a 20 6f 76 66 6c 29 2c  ameter.** ovfl),
16b62 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66   this function f
16b63 69 6e 64 73 20 74 68 65 20 70 61 67 65 20 6e 75  inds the page nu
16b64 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 78 74  mber of the next
16b65 20 70 61 67 65 20 69 6e 20 74 68 65 20 0a 2a 2a   page in the .**
16b66 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20   linked list of 
16b67 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20  overflow pages. 
16b68 49 66 20 70 6f 73 73 69 62 6c 65 2c 20 69 74 20  If possible, it 
16b69 75 73 65 73 20 74 68 65 20 61 75 74 6f 2d 76 61  uses the auto-va
16b6a 63 75 75 6d 0a 2a 2a 20 70 6f 69 6e 74 65 72 2d  cuum.** pointer-
16b6b 6d 61 70 20 64 61 74 61 20 69 6e 73 74 65 61 64  map data instead
16b6c 20 6f 66 20 72 65 61 64 69 6e 67 20 74 68 65 20   of reading the 
16b6d 63 6f 6e 74 65 6e 74 20 6f 66 20 70 61 67 65 20  content of page 
16b6e 6f 76 66 6c 20 74 6f 20 64 6f 20 73 6f 2e 20 0a  ovfl to do so. .
16b6f 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f  **.** If an erro
16b70 72 20 6f 63 63 75 72 73 20 61 6e 20 53 51 4c 69  r occurs an SQLi
16b71 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  te error code is
16b72 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72   returned. Other
16b73 77 69 73 65 3a 0a 2a 2a 0a 2a 2a 20 54 68 65 20  wise:.**.** The 
16b74 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74  page number of t
16b75 68 65 20 6e 65 78 74 20 6f 76 65 72 66 6c 6f 77  he next overflow
16b76 20 70 61 67 65 20 69 6e 20 74 68 65 20 6c 69 6e   page in the lin
16b77 6b 65 64 20 6c 69 73 74 20 69 73 20 0a 2a 2a 20  ked list is .** 
16b78 77 72 69 74 74 65 6e 20 74 6f 20 2a 70 50 67 6e  written to *pPgn
16b79 6f 4e 65 78 74 2e 20 49 66 20 70 61 67 65 20 6f  oNext. If page o
16b7a 76 66 6c 20 69 73 20 74 68 65 20 6c 61 73 74 20  vfl is the last 
16b7b 70 61 67 65 20 69 6e 20 69 74 73 20 6c 69 6e 6b  page in its link
16b7c 65 64 20 0a 2a 2a 20 6c 69 73 74 2c 20 2a 70 50  ed .** list, *pP
16b7d 67 6e 6f 4e 65 78 74 20 69 73 20 73 65 74 20 74  gnoNext is set t
16b7e 6f 20 7a 65 72 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49  o zero. .**.** I
16b7f 66 20 70 70 50 61 67 65 20 69 73 20 6e 6f 74 20  f ppPage is not 
16b80 4e 55 4c 4c 2c 20 61 6e 64 20 61 20 72 65 66 65  NULL, and a refe
16b81 72 65 6e 63 65 20 74 6f 20 74 68 65 20 4d 65 6d  rence to the Mem
16b82 50 61 67 65 20 6f 62 6a 65 63 74 20 63 6f 72 72  Page object corr
16b83 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20  esponding.** to 
16b84 70 61 67 65 20 6e 75 6d 62 65 72 20 70 4f 76 66  page number pOvf
16b85 6c 20 77 61 73 20 6f 62 74 61 69 6e 65 64 2c 20  l was obtained, 
16b86 74 68 65 6e 20 2a 70 70 50 61 67 65 20 69 73 20  then *ppPage is 
16b87 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20  set to point to 
16b88 74 68 61 74 0a 2a 2a 20 72 65 66 65 72 65 6e 63  that.** referenc
16b89 65 2e 20 49 74 20 69 73 20 74 68 65 20 72 65 73  e. It is the res
16b8a 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 74  ponsibility of t
16b8b 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 63 61 6c  he caller to cal
16b8c 6c 20 72 65 6c 65 61 73 65 50 61 67 65 28 29 0a  l releasePage().
16b8d 2a 2a 20 6f 6e 20 2a 70 70 50 61 67 65 20 74 6f  ** on *ppPage to
16b8e 20 66 72 65 65 20 74 68 65 20 72 65 66 65 72 65   free the refere
16b8f 6e 63 65 2e 20 49 6e 20 6e 6f 20 72 65 66 65 72  nce. In no refer
16b90 65 6e 63 65 20 77 61 73 20 6f 62 74 61 69 6e 65  ence was obtaine
16b91 64 20 28 62 65 63 61 75 73 65 0a 2a 2a 20 74 68  d (because.** th
16b92 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 77 61  e pointer-map wa
16b93 73 20 75 73 65 64 20 74 6f 20 6f 62 74 61 69 6e  s used to obtain
16b94 20 74 68 65 20 76 61 6c 75 65 20 66 6f 72 20 2a   the value for *
16b95 70 50 67 6e 6f 4e 65 78 74 29 2c 20 74 68 65 6e  pPgnoNext), then
16b96 0a 2a 2a 20 2a 70 70 50 61 67 65 20 69 73 20 73  .** *ppPage is s
16b97 65 74 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2f 0a 73  et to zero..*/.s
16b98 74 61 74 69 63 20 69 6e 74 20 67 65 74 4f 76 65  tatic int getOve
16b99 72 66 6c 6f 77 50 61 67 65 28 0a 20 20 42 74 53  rflowPage(.  BtS
16b9a 68 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20  hared *pBt,     
16b9b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
16b9c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a   database file *
16b9d 2f 0a 20 20 50 67 6e 6f 20 6f 76 66 6c 2c 20 20  /.  Pgno ovfl,  
16b9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16b9f 20 2f 2a 20 43 75 72 72 65 6e 74 20 6f 76 65 72   /* Current over
16ba0 66 6c 6f 77 20 70 61 67 65 20 6e 75 6d 62 65 72  flow page number
16ba1 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a   */.  MemPage **
16ba2 70 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 20  ppPage,         
16ba3 20 20 20 2f 2a 20 4f 55 54 3a 20 4d 65 6d 50 61     /* OUT: MemPa
16ba4 67 65 20 68 61 6e 64 6c 65 20 28 6d 61 79 20 62  ge handle (may b
16ba5 65 20 4e 55 4c 4c 29 20 2a 2f 0a 20 20 50 67 6e  e NULL) */.  Pgn
16ba6 6f 20 2a 70 50 67 6e 6f 4e 65 78 74 20 20 20 20  o *pPgnoNext    
16ba7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
16ba8 3a 20 4e 65 78 74 20 6f 76 65 72 66 6c 6f 77 20  : Next overflow 
16ba9 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 29  page number */.)
16baa 7b 0a 20 20 50 67 6e 6f 20 6e 65 78 74 20 3d 20  {.  Pgno next = 
16bab 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  0;.  MemPage *pP
16bac 61 67 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72  age = 0;.  int r
16bad 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a  c = SQLITE_OK;..
16bae 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
16baf 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74  3_mutex_held(pBt
16bb0 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
16bb1 73 65 72 74 28 70 50 67 6e 6f 4e 65 78 74 29 3b  sert(pPgnoNext);
16bb2 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
16bb3 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
16bb4 0a 20 20 2f 2a 20 54 72 79 20 74 6f 20 66 69 6e  .  /* Try to fin
16bb5 64 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20  d the next page 
16bb6 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  in the overflow 
16bb7 6c 69 73 74 20 75 73 69 6e 67 20 74 68 65 0a 20  list using the. 
16bb8 20 2a 2a 20 61 75 74 6f 76 61 63 75 75 6d 20 70   ** autovacuum p
16bb9 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 73  ointer-map pages
16bba 2e 20 47 75 65 73 73 20 74 68 61 74 20 74 68 65  . Guess that the
16bbb 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20 0a 20   next page in . 
16bbc 20 2a 2a 20 74 68 65 20 6f 76 65 72 66 6c 6f 77   ** the overflow
16bbd 20 6c 69 73 74 20 69 73 20 70 61 67 65 20 6e 75   list is page nu
16bbe 6d 62 65 72 20 28 6f 76 66 6c 2b 31 29 2e 20 49  mber (ovfl+1). I
16bbf 66 20 74 68 61 74 20 67 75 65 73 73 20 74 75 72  f that guess tur
16bc0 6e 73 20 0a 20 20 2a 2a 20 6f 75 74 20 74 6f 20  ns .  ** out to 
16bc1 62 65 20 77 72 6f 6e 67 2c 20 66 61 6c 6c 20 62  be wrong, fall b
16bc2 61 63 6b 20 74 6f 20 6c 6f 61 64 69 6e 67 20 74  ack to loading t
16bc3 68 65 20 64 61 74 61 20 6f 66 20 70 61 67 65 20  he data of page 
16bc4 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 76 66  .  ** number ovf
16bc5 6c 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74  l to determine t
16bc6 68 65 20 6e 65 78 74 20 70 61 67 65 20 6e 75 6d  he next page num
16bc7 62 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ber..  */.  if( 
16bc8 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20  pBt->autoVacuum 
16bc9 29 7b 0a 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f  ){.    Pgno pgno
16bca 3b 0a 20 20 20 20 50 67 6e 6f 20 69 47 75 65 73  ;.    Pgno iGues
16bcb 73 20 3d 20 6f 76 66 6c 2b 31 3b 0a 20 20 20 20  s = ovfl+1;.    
16bcc 75 38 20 65 54 79 70 65 3b 0a 0a 20 20 20 20 77  u8 eType;..    w
16bcd 68 69 6c 65 28 20 50 54 52 4d 41 50 5f 49 53 50  hile( PTRMAP_ISP
16bce 41 47 45 28 70 42 74 2c 20 69 47 75 65 73 73 29  AGE(pBt, iGuess)
16bcf 20 7c 7c 20 69 47 75 65 73 73 3d 3d 50 45 4e 44   || iGuess==PEND
16bd0 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
16bd1 74 29 20 29 7b 0a 20 20 20 20 20 20 69 47 75 65  t) ){.      iGue
16bd2 73 73 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  ss++;.    }..   
16bd3 20 69 66 28 20 69 47 75 65 73 73 3c 3d 70 61 67   if( iGuess<=pag
16bd4 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29  erPagecount(pBt)
16bd5 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70   ){.      rc = p
16bd6 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 69 47  trmapGet(pBt, iG
16bd7 75 65 73 73 2c 20 26 65 54 79 70 65 2c 20 26 70  uess, &eType, &p
16bd8 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20  gno);.      if( 
16bd9 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
16bda 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f   eType==PTRMAP_O
16bdb 56 45 52 46 4c 4f 57 32 20 26 26 20 70 67 6e 6f  VERFLOW2 && pgno
16bdc 3d 3d 6f 76 66 6c 20 29 7b 0a 20 20 20 20 20 20  ==ovfl ){.      
16bdd 20 20 6e 65 78 74 20 3d 20 69 47 75 65 73 73 3b    next = iGuess;
16bde 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
16bdf 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20  LITE_DONE;.     
16be0 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e   }.    }.  }.#en
16be1 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 6e  dif..  assert( n
16be2 65 78 74 3d 3d 30 20 7c 7c 20 72 63 3d 3d 53 51  ext==0 || rc==SQ
16be3 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 69  LITE_DONE );.  i
16be4 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
16be5 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 62 74 72   ){.    rc = btr
16be6 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 6f  eeGetPage(pBt, o
16be7 76 66 6c 2c 20 26 70 50 61 67 65 2c 20 30 29 3b  vfl, &pPage, 0);
16be8 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d  .    assert( rc=
16be9 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50  =SQLITE_OK || pP
16bea 61 67 65 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66  age==0 );.    if
16beb 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
16bec 29 7b 0a 20 20 20 20 20 20 6e 65 78 74 20 3d 20  ){.      next = 
16bed 67 65 74 34 62 79 74 65 28 70 50 61 67 65 2d 3e  get4byte(pPage->
16bee 61 44 61 74 61 29 3b 0a 20 20 20 20 7d 0a 20 20  aData);.    }.  
16bef 7d 0a 0a 20 20 2a 70 50 67 6e 6f 4e 65 78 74 20  }..  *pPgnoNext 
16bf0 3d 20 6e 65 78 74 3b 0a 20 20 69 66 28 20 70 70  = next;.  if( pp
16bf1 50 61 67 65 20 29 7b 0a 20 20 20 20 2a 70 70 50  Page ){.    *ppP
16bf2 61 67 65 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d  age = pPage;.  }
16bf3 65 6c 73 65 7b 0a 20 20 20 20 72 65 6c 65 61 73  else{.    releas
16bf4 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20  ePage(pPage);.  
16bf5 7d 0a 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d  }.  return (rc==
16bf6 53 51 4c 49 54 45 5f 44 4f 4e 45 20 3f 20 53 51  SQLITE_DONE ? SQ
16bf7 4c 49 54 45 5f 4f 4b 20 3a 20 72 63 29 3b 0a 7d  LITE_OK : rc);.}
16bf8 0a 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 64 61 74  ../*.** Copy dat
16bf9 61 20 66 72 6f 6d 20 61 20 62 75 66 66 65 72 20  a from a buffer 
16bfa 74 6f 20 61 20 70 61 67 65 2c 20 6f 72 20 66 72  to a page, or fr
16bfb 6f 6d 20 61 20 70 61 67 65 20 74 6f 20 61 20 62  om a page to a b
16bfc 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 70 50 61  uffer..**.** pPa
16bfd 79 6c 6f 61 64 20 69 73 20 61 20 70 6f 69 6e 74  yload is a point
16bfe 65 72 20 74 6f 20 64 61 74 61 20 73 74 6f 72 65  er to data store
16bff 64 20 6f 6e 20 64 61 74 61 62 61 73 65 20 70 61  d on database pa
16c00 67 65 20 70 44 62 50 61 67 65 2e 0a 2a 2a 20 49  ge pDbPage..** I
16c01 66 20 61 72 67 75 6d 65 6e 74 20 65 4f 70 20 69  f argument eOp i
16c02 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 6e 42  s false, then nB
16c03 79 74 65 20 62 79 74 65 73 20 6f 66 20 64 61 74  yte bytes of dat
16c04 61 20 61 72 65 20 63 6f 70 69 65 64 0a 2a 2a 20  a are copied.** 
16c05 66 72 6f 6d 20 70 50 61 79 6c 6f 61 64 20 74 6f  from pPayload to
16c06 20 74 68 65 20 62 75 66 66 65 72 20 70 6f 69 6e   the buffer poin
16c07 74 65 64 20 61 74 20 62 79 20 70 42 75 66 2e 20  ted at by pBuf. 
16c08 49 66 20 65 4f 70 20 69 73 20 74 72 75 65 2c 0a  If eOp is true,.
16c09 2a 2a 20 74 68 65 6e 20 73 71 6c 69 74 65 33 50  ** then sqlite3P
16c0a 61 67 65 72 57 72 69 74 65 28 29 20 69 73 20 63  agerWrite() is c
16c0b 61 6c 6c 65 64 20 6f 6e 20 70 44 62 50 61 67 65  alled on pDbPage
16c0c 20 61 6e 64 20 6e 42 79 74 65 20 62 79 74 65 73   and nByte bytes
16c0d 0a 2a 2a 20 6f 66 20 64 61 74 61 20 61 72 65 20  .** of data are 
16c0e 63 6f 70 69 65 64 20 66 72 6f 6d 20 74 68 65 20  copied from the 
16c0f 62 75 66 66 65 72 20 70 42 75 66 20 74 6f 20 70  buffer pBuf to p
16c10 50 61 79 6c 6f 61 64 2e 0a 2a 2a 0a 2a 2a 20 53  Payload..**.** S
16c11 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
16c12 72 6e 65 64 20 6f 6e 20 73 75 63 63 65 73 73 2c  rned on success,
16c13 20 6f 74 68 65 72 77 69 73 65 20 61 6e 20 65 72   otherwise an er
16c14 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61  ror code..*/.sta
16c15 74 69 63 20 69 6e 74 20 63 6f 70 79 50 61 79 6c  tic int copyPayl
16c16 6f 61 64 28 0a 20 20 76 6f 69 64 20 2a 70 50 61  oad(.  void *pPa
16c17 79 6c 6f 61 64 2c 20 20 20 20 20 20 20 20 20 20  yload,          
16c18 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 70   /* Pointer to p
16c19 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 76 6f  age data */.  vo
16c1a 69 64 20 2a 70 42 75 66 2c 20 20 20 20 20 20 20  id *pBuf,       
16c1b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
16c1c 65 72 20 74 6f 20 62 75 66 66 65 72 20 2a 2f 0a  er to buffer */.
16c1d 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20    int nByte,    
16c1e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
16c1f 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74  umber of bytes t
16c20 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 69 6e 74 20  o copy */.  int 
16c21 65 4f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20  eOp,            
16c22 20 20 20 20 20 20 2f 2a 20 30 20 2d 3e 20 63 6f        /* 0 -> co
16c23 70 79 20 66 72 6f 6d 20 70 61 67 65 2c 20 31 20  py from page, 1 
16c24 2d 3e 20 63 6f 70 79 20 74 6f 20 70 61 67 65 20  -> copy to page 
16c25 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62  */.  DbPage *pDb
16c26 50 61 67 65 20 20 20 20 20 20 20 20 20 20 20 2f  Page           /
16c27 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e  * Page containin
16c28 67 20 70 50 61 79 6c 6f 61 64 20 2a 2f 0a 29 7b  g pPayload */.){
16c29 0a 20 20 69 66 28 20 65 4f 70 20 29 7b 0a 20 20  .  if( eOp ){.  
16c2a 20 20 2f 2a 20 43 6f 70 79 20 64 61 74 61 20 66    /* Copy data f
16c2b 72 6f 6d 20 62 75 66 66 65 72 20 74 6f 20 70 61  rom buffer to pa
16c2c 67 65 20 28 61 20 77 72 69 74 65 20 6f 70 65 72  ge (a write oper
16c2d 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 20 20 69 6e  ation) */.    in
16c2e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61  t rc = sqlite3Pa
16c2f 67 65 72 57 72 69 74 65 28 70 44 62 50 61 67 65  gerWrite(pDbPage
16c30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
16c31 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
16c32 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
16c33 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 50   }.    memcpy(pP
16c34 61 79 6c 6f 61 64 2c 20 70 42 75 66 2c 20 6e 42  ayload, pBuf, nB
16c35 79 74 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  yte);.  }else{. 
16c36 20 20 20 2f 2a 20 43 6f 70 79 20 64 61 74 61 20     /* Copy data 
16c37 66 72 6f 6d 20 70 61 67 65 20 74 6f 20 62 75 66  from page to buf
16c38 66 65 72 20 28 61 20 72 65 61 64 20 6f 70 65 72  fer (a read oper
16c39 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 20 20 6d 65  ation) */.    me
16c3a 6d 63 70 79 28 70 42 75 66 2c 20 70 50 61 79 6c  mcpy(pBuf, pPayl
16c3b 6f 61 64 2c 20 6e 42 79 74 65 29 3b 0a 20 20 7d  oad, nByte);.  }
16c3c 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
16c3d 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  _OK;.}../*.** Th
16c3e 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75  is function is u
16c3f 73 65 64 20 74 6f 20 72 65 61 64 20 6f 72 20 6f  sed to read or o
16c40 76 65 72 77 72 69 74 65 20 70 61 79 6c 6f 61 64  verwrite payload
16c41 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20   information.** 
16c42 66 6f 72 20 74 68 65 20 65 6e 74 72 79 20 74 68  for the entry th
16c43 61 74 20 74 68 65 20 70 43 75 72 20 63 75 72 73  at the pCur curs
16c44 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74  or is pointing t
16c45 6f 2e 20 49 66 20 74 68 65 20 65 4f 70 0a 2a 2a  o. If the eOp.**
16c46 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 30 2c   parameter is 0,
16c47 20 74 68 69 73 20 69 73 20 61 20 72 65 61 64 20   this is a read 
16c48 6f 70 65 72 61 74 69 6f 6e 20 28 64 61 74 61 20  operation (data 
16c49 63 6f 70 69 65 64 20 69 6e 74 6f 0a 2a 2a 20 62  copied into.** b
16c4a 75 66 66 65 72 20 70 42 75 66 29 2e 20 49 66 20  uffer pBuf). If 
16c4b 69 74 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  it is non-zero, 
16c4c 61 20 77 72 69 74 65 20 28 64 61 74 61 20 63 6f  a write (data co
16c4d 70 69 65 64 20 66 72 6f 6d 0a 2a 2a 20 62 75 66  pied from.** buf
16c4e 66 65 72 20 70 42 75 66 29 2e 0a 2a 2a 0a 2a 2a  fer pBuf)..**.**
16c4f 20 41 20 74 6f 74 61 6c 20 6f 66 20 22 61 6d 74   A total of "amt
16c50 22 20 62 79 74 65 73 20 61 72 65 20 72 65 61 64  " bytes are read
16c51 20 6f 72 20 77 72 69 74 74 65 6e 20 62 65 67 69   or written begi
16c52 6e 6e 69 6e 67 20 61 74 20 22 6f 66 66 73 65 74  nning at "offset
16c53 22 2e 0a 2a 2a 20 44 61 74 61 20 69 73 20 72 65  "..** Data is re
16c54 61 64 20 74 6f 20 6f 72 20 66 72 6f 6d 20 74 68  ad to or from th
16c55 65 20 62 75 66 66 65 72 20 70 42 75 66 2e 0a 2a  e buffer pBuf..*
16c56 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74  *.** The content
16c57 20 62 65 69 6e 67 20 72 65 61 64 20 6f 72 20 77   being read or w
16c58 72 69 74 74 65 6e 20 6d 69 67 68 74 20 61 70 70  ritten might app
16c59 65 61 72 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20  ear on the main 
16c5a 70 61 67 65 0a 2a 2a 20 6f 72 20 62 65 20 73 63  page.** or be sc
16c5b 61 74 74 65 72 65 64 20 6f 75 74 20 6f 6e 20 6d  attered out on m
16c5c 75 6c 74 69 70 6c 65 20 6f 76 65 72 66 6c 6f 77  ultiple overflow
16c5d 20 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66   pages..**.** If
16c5e 20 74 68 65 20 42 74 43 75 72 73 6f 72 2e 69 73   the BtCursor.is
16c5f 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 66  IncrblobHandle f
16c60 6c 61 67 20 69 73 20 73 65 74 2c 20 61 6e 64 20  lag is set, and 
16c61 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 63  the current.** c
16c62 75 72 73 6f 72 20 65 6e 74 72 79 20 75 73 65 73  ursor entry uses
16c63 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 76 65   one or more ove
16c64 72 66 6c 6f 77 20 70 61 67 65 73 2c 20 74 68 69  rflow pages, thi
16c65 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 61 6c  s function.** al
16c66 6c 6f 63 61 74 65 73 20 73 70 61 63 65 20 66 6f  locates space fo
16c67 72 20 61 6e 64 20 6c 61 7a 69 6c 79 20 70 6f 70  r and lazily pop
16c68 6c 75 61 74 65 73 20 74 68 65 20 6f 76 65 72 66  luates the overf
16c69 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 0a 2a  low page-list .*
16c6a 2a 20 63 61 63 68 65 20 61 72 72 61 79 20 28 42  * cache array (B
16c6b 74 43 75 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f  tCursor.aOverflo
16c6c 77 29 2e 20 53 75 62 73 65 71 75 65 6e 74 20 63  w). Subsequent c
16c6d 61 6c 6c 73 20 75 73 65 20 74 68 69 73 0a 2a 2a  alls use this.**
16c6e 20 63 61 63 68 65 20 74 6f 20 6d 61 6b 65 20 73   cache to make s
16c6f 65 65 6b 69 6e 67 20 74 6f 20 74 68 65 20 73 75  eeking to the su
16c70 70 70 6c 69 65 64 20 6f 66 66 73 65 74 20 6d 6f  pplied offset mo
16c71 72 65 20 65 66 66 69 63 69 65 6e 74 2e 0a 2a 2a  re efficient..**
16c72 0a 2a 2a 20 4f 6e 63 65 20 61 6e 20 6f 76 65 72  .** Once an over
16c73 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63  flow page-list c
16c74 61 63 68 65 20 68 61 73 20 62 65 65 6e 20 61 6c  ache has been al
16c75 6c 6f 63 61 74 65 64 2c 20 69 74 20 6d 61 79 20  located, it may 
16c76 62 65 0a 2a 2a 20 69 6e 76 61 6c 69 64 61 74 65  be.** invalidate
16c77 64 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20  d if some other 
16c78 63 75 72 73 6f 72 20 77 72 69 74 65 73 20 74 6f  cursor writes to
16c79 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 2c   the same table,
16c7a 20 6f 72 20 69 66 0a 2a 2a 20 74 68 65 20 63 75   or if.** the cu
16c7b 72 73 6f 72 20 69 73 20 6d 6f 76 65 64 20 74 6f  rsor is moved to
16c7c 20 61 20 64 69 66 66 65 72 65 6e 74 20 72 6f 77   a different row
16c7d 2e 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20  . Additionally, 
16c7e 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 0a 2a  in auto-vacuum.*
16c7f 2a 20 6d 6f 64 65 2c 20 74 68 65 20 66 6f 6c 6c  * mode, the foll
16c80 6f 77 69 6e 67 20 65 76 65 6e 74 73 20 6d 61 79  owing events may
16c81 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6e 20 6f   invalidate an o
16c82 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73  verflow page-lis
16c83 74 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 20  t cache..**.**  
16c84 20 2a 20 41 6e 20 69 6e 63 72 65 6d 65 6e 74 61   * An incrementa
16c85 6c 20 76 61 63 75 75 6d 2c 0a 2a 2a 20 20 20 2a  l vacuum,.**   *
16c86 20 41 20 63 6f 6d 6d 69 74 20 69 6e 20 61 75 74   A commit in aut
16c87 6f 5f 76 61 63 75 75 6d 3d 22 66 75 6c 6c 22 20  o_vacuum="full" 
16c88 6d 6f 64 65 2c 0a 2a 2a 20 20 20 2a 20 43 72 65  mode,.**   * Cre
16c89 61 74 69 6e 67 20 61 20 74 61 62 6c 65 20 28 6d  ating a table (m
16c8a 61 79 20 72 65 71 75 69 72 65 20 6d 6f 76 69 6e  ay require movin
16c8b 67 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61  g an overflow pa
16c8c 67 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ge)..*/.static i
16c8d 6e 74 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64  nt accessPayload
16c8e 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43  (.  BtCursor *pC
16c8f 75 72 2c 20 20 20 20 20 20 2f 2a 20 43 75 72 73  ur,      /* Curs
16c90 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 65  or pointing to e
16c91 6e 74 72 79 20 74 6f 20 72 65 61 64 20 66 72 6f  ntry to read fro
16c92 6d 20 2a 2f 0a 20 20 75 33 32 20 6f 66 66 73 65  m */.  u32 offse
16c93 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42  t,          /* B
16c94 65 67 69 6e 20 72 65 61 64 69 6e 67 20 74 68 69  egin reading thi
16c95 73 20 66 61 72 20 69 6e 74 6f 20 70 61 79 6c 6f  s far into paylo
16c96 61 64 20 2a 2f 0a 20 20 75 33 32 20 61 6d 74 2c  ad */.  u32 amt,
16c97 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
16c98 52 65 61 64 20 74 68 69 73 20 6d 61 6e 79 20 62  Read this many b
16c99 79 74 65 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  ytes */.  unsign
16c9a 65 64 20 63 68 61 72 20 2a 70 42 75 66 2c 20 2f  ed char *pBuf, /
16c9b 2a 20 57 72 69 74 65 20 74 68 65 20 62 79 74 65  * Write the byte
16c9c 73 20 69 6e 74 6f 20 74 68 69 73 20 62 75 66 66  s into this buff
16c9d 65 72 20 2a 2f 20 0a 20 20 69 6e 74 20 65 4f 70  er */ .  int eOp
16c9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
16c9f 20 7a 65 72 6f 20 74 6f 20 72 65 61 64 2e 20 6e   zero to read. n
16ca0 6f 6e 2d 7a 65 72 6f 20 74 6f 20 77 72 69 74 65  on-zero to write
16ca1 2e 20 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e  . */.){.  unsign
16ca2 65 64 20 63 68 61 72 20 2a 61 50 61 79 6c 6f 61  ed char *aPayloa
16ca3 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  d;.  int rc = SQ
16ca4 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 33 32 20 6e  LITE_OK;.  u32 n
16ca5 4b 65 79 3b 0a 20 20 69 6e 74 20 69 49 64 78 20  Key;.  int iIdx 
16ca6 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  = 0;.  MemPage *
16ca7 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70  pPage = pCur->ap
16ca8 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
16ca9 5d 3b 20 2f 2a 20 42 74 72 65 65 20 70 61 67 65  ]; /* Btree page
16caa 20 6f 66 20 63 75 72 72 65 6e 74 20 65 6e 74 72   of current entr
16cab 79 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20  y */.  BtShared 
16cac 2a 70 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 74  *pBt = pCur->pBt
16cad 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
16cae 20 20 20 2f 2a 20 42 74 72 65 65 20 74 68 69 73     /* Btree this
16caf 20 63 75 72 73 6f 72 20 62 65 6c 6f 6e 67 73 20   cursor belongs 
16cb0 74 6f 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  to */..  assert(
16cb1 20 70 50 61 67 65 20 29 3b 0a 20 20 61 73 73 65   pPage );.  asse
16cb2 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65  rt( pCur->eState
16cb3 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29  ==CURSOR_VALID )
16cb4 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  ;.  assert( pCur
16cb5 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50  ->aiIdx[pCur->iP
16cb6 61 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 6c  age]<pPage->nCel
16cb7 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63  l );.  assert( c
16cb8 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
16cb9 70 43 75 72 29 20 29 3b 0a 0a 20 20 67 65 74 43  pCur) );..  getC
16cba 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20  ellInfo(pCur);. 
16cbb 20 61 50 61 79 6c 6f 61 64 20 3d 20 70 43 75 72   aPayload = pCur
16cbc 2d 3e 69 6e 66 6f 2e 70 43 65 6c 6c 20 2b 20 70  ->info.pCell + p
16cbd 43 75 72 2d 3e 69 6e 66 6f 2e 6e 48 65 61 64 65  Cur->info.nHeade
16cbe 72 3b 0a 20 20 6e 4b 65 79 20 3d 20 28 70 50 61  r;.  nKey = (pPa
16cbf 67 65 2d 3e 69 6e 74 4b 65 79 20 3f 20 30 20 3a  ge->intKey ? 0 :
16cc0 20 28 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f   (int)pCur->info
16cc1 2e 6e 4b 65 79 29 3b 0a 0a 20 20 69 66 28 20 4e  .nKey);..  if( N
16cc2 45 56 45 52 28 6f 66 66 73 65 74 2b 61 6d 74 20  EVER(offset+amt 
16cc3 3e 20 6e 4b 65 79 2b 70 43 75 72 2d 3e 69 6e 66  > nKey+pCur->inf
16cc4 6f 2e 6e 44 61 74 61 29 20 0a 20 20 20 7c 7c 20  o.nData) .   || 
16cc5 26 61 50 61 79 6c 6f 61 64 5b 70 43 75 72 2d 3e  &aPayload[pCur->
16cc6 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 5d 20 3e 20 26  info.nLocal] > &
16cc7 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74  pPage->aData[pBt
16cc8 2d 3e 75 73 61 62 6c 65 53 69 7a 65 5d 0a 20 20  ->usableSize].  
16cc9 29 7b 0a 20 20 20 20 2f 2a 20 54 72 79 69 6e 67  ){.    /* Trying
16cca 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69 74   to read or writ
16ccb 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f  e past the end o
16ccc 66 20 74 68 65 20 64 61 74 61 20 69 73 20 61 6e  f the data is an
16ccd 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 72 65   error */.    re
16cce 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
16ccf 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20  UPT_BKPT;.  }.. 
16cd0 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 64 61 74   /* Check if dat
16cd1 61 20 6d 75 73 74 20 62 65 20 72 65 61 64 2f 77  a must be read/w
16cd2 72 69 74 74 65 6e 20 74 6f 2f 66 72 6f 6d 20 74  ritten to/from t
16cd3 68 65 20 62 74 72 65 65 20 70 61 67 65 20 69 74  he btree page it
16cd4 73 65 6c 66 2e 20 2a 2f 0a 20 20 69 66 28 20 6f  self. */.  if( o
16cd5 66 66 73 65 74 3c 70 43 75 72 2d 3e 69 6e 66 6f  ffset<pCur->info
16cd6 2e 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 69  .nLocal ){.    i
16cd7 6e 74 20 61 20 3d 20 61 6d 74 3b 0a 20 20 20 20  nt a = amt;.    
16cd8 69 66 28 20 61 2b 6f 66 66 73 65 74 3e 70 43 75  if( a+offset>pCu
16cd9 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29  r->info.nLocal )
16cda 7b 0a 20 20 20 20 20 20 61 20 3d 20 70 43 75 72  {.      a = pCur
16cdb 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2d 20  ->info.nLocal - 
16cdc 6f 66 66 73 65 74 3b 0a 20 20 20 20 7d 0a 20 20  offset;.    }.  
16cdd 20 20 72 63 20 3d 20 63 6f 70 79 50 61 79 6c 6f    rc = copyPaylo
16cde 61 64 28 26 61 50 61 79 6c 6f 61 64 5b 6f 66 66  ad(&aPayload[off
16cdf 73 65 74 5d 2c 20 70 42 75 66 2c 20 61 2c 20 65  set], pBuf, a, e
16ce0 4f 70 2c 20 70 50 61 67 65 2d 3e 70 44 62 50 61  Op, pPage->pDbPa
16ce1 67 65 29 3b 0a 20 20 20 20 6f 66 66 73 65 74 20  ge);.    offset 
16ce2 3d 20 30 3b 0a 20 20 20 20 70 42 75 66 20 2b 3d  = 0;.    pBuf +=
16ce3 20 61 3b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 61   a;.    amt -= a
16ce4 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f  ;.  }else{.    o
16ce5 66 66 73 65 74 20 2d 3d 20 70 43 75 72 2d 3e 69  ffset -= pCur->i
16ce6 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 0a  nfo.nLocal;.  }.
16ce7 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
16ce8 45 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 29 7b  E_OK && amt>0 ){
16ce9 0a 20 20 20 20 63 6f 6e 73 74 20 75 33 32 20 6f  .    const u32 o
16cea 76 66 6c 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75  vflSize = pBt->u
16ceb 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 20 20  sableSize - 4;  
16cec 2f 2a 20 42 79 74 65 73 20 63 6f 6e 74 65 6e 74  /* Bytes content
16ced 20 70 65 72 20 6f 76 66 6c 20 70 61 67 65 20 2a   per ovfl page *
16cee 2f 0a 20 20 20 20 50 67 6e 6f 20 6e 65 78 74 50  /.    Pgno nextP
16cef 61 67 65 3b 0a 0a 20 20 20 20 6e 65 78 74 50 61  age;..    nextPa
16cf0 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 61  ge = get4byte(&a
16cf1 50 61 79 6c 6f 61 64 5b 70 43 75 72 2d 3e 69 6e  Payload[pCur->in
16cf2 66 6f 2e 6e 4c 6f 63 61 6c 5d 29 3b 0a 0a 23 69  fo.nLocal]);..#i
16cf3 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
16cf4 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20 2f  T_INCRBLOB.    /
16cf5 2a 20 49 66 20 74 68 65 20 69 73 49 6e 63 72 62  * If the isIncrb
16cf6 6c 6f 62 48 61 6e 64 6c 65 20 66 6c 61 67 20 69  lobHandle flag i
16cf7 73 20 73 65 74 20 61 6e 64 20 74 68 65 20 42 74  s set and the Bt
16cf8 43 75 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77  Cursor.aOverflow
16cf9 5b 5d 0a 20 20 20 20 2a 2a 20 68 61 73 20 6e 6f  [].    ** has no
16cfa 74 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64  t been allocated
16cfb 2c 20 61 6c 6c 6f 63 61 74 65 20 69 74 20 6e 6f  , allocate it no
16cfc 77 2e 20 54 68 65 20 61 72 72 61 79 20 69 73 20  w. The array is 
16cfd 73 69 7a 65 64 20 61 74 0a 20 20 20 20 2a 2a 20  sized at.    ** 
16cfe 6f 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 65 61  one entry for ea
16cff 63 68 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  ch overflow page
16d00 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77   in the overflow
16d01 20 63 68 61 69 6e 2e 20 54 68 65 0a 20 20 20 20   chain. The.    
16d02 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f  ** page number o
16d03 66 20 74 68 65 20 66 69 72 73 74 20 6f 76 65 72  f the first over
16d04 66 6c 6f 77 20 70 61 67 65 20 69 73 20 73 74 6f  flow page is sto
16d05 72 65 64 20 69 6e 20 61 4f 76 65 72 66 6c 6f 77  red in aOverflow
16d06 5b 30 5d 2c 0a 20 20 20 20 2a 2a 20 65 74 63 2e  [0],.    ** etc.
16d07 20 41 20 76 61 6c 75 65 20 6f 66 20 30 20 69 6e   A value of 0 in
16d08 20 74 68 65 20 61 4f 76 65 72 66 6c 6f 77 5b 5d   the aOverflow[]
16d09 20 61 72 72 61 79 20 6d 65 61 6e 73 20 22 6e 6f   array means "no
16d0a 74 20 79 65 74 20 6b 6e 6f 77 6e 22 0a 20 20 20  t yet known".   
16d0b 20 2a 2a 20 28 74 68 65 20 63 61 63 68 65 20 69   ** (the cache i
16d0c 73 20 6c 61 7a 69 6c 79 20 70 6f 70 75 6c 61 74  s lazily populat
16d0d 65 64 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ed)..    */.    
16d0e 69 66 28 20 70 43 75 72 2d 3e 69 73 49 6e 63 72  if( pCur->isIncr
16d0f 62 6c 6f 62 48 61 6e 64 6c 65 20 26 26 20 21 70  blobHandle && !p
16d10 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 29  Cur->aOverflow )
16d11 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 4f 76 66  {.      int nOvf
16d12 6c 20 3d 20 28 70 43 75 72 2d 3e 69 6e 66 6f 2e  l = (pCur->info.
16d13 6e 50 61 79 6c 6f 61 64 2d 70 43 75 72 2d 3e 69  nPayload-pCur->i
16d14 6e 66 6f 2e 6e 4c 6f 63 61 6c 2b 6f 76 66 6c 53  nfo.nLocal+ovflS
16d15 69 7a 65 2d 31 29 2f 6f 76 66 6c 53 69 7a 65 3b  ize-1)/ovflSize;
16d16 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 4f 76  .      pCur->aOv
16d17 65 72 66 6c 6f 77 20 3d 20 28 50 67 6e 6f 20 2a  erflow = (Pgno *
16d18 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65  )sqlite3MallocZe
16d19 72 6f 28 73 69 7a 65 6f 66 28 50 67 6e 6f 29 2a  ro(sizeof(Pgno)*
16d1a 6e 4f 76 66 6c 29 3b 0a 20 20 20 20 20 20 2f 2a  nOvfl);.      /*
16d1b 20 6e 4f 76 66 6c 20 69 73 20 61 6c 77 61 79 73   nOvfl is always
16d1c 20 70 6f 73 69 74 69 76 65 2e 20 20 49 66 20 69   positive.  If i
16d1d 74 20 77 65 72 65 20 7a 65 72 6f 2c 20 66 65 74  t were zero, fet
16d1e 63 68 50 61 79 6c 6f 61 64 20 77 6f 75 6c 64 20  chPayload would 
16d1f 68 61 76 65 0a 20 20 20 20 20 20 2a 2a 20 62 65  have.      ** be
16d20 65 6e 20 75 73 65 64 20 69 6e 73 74 65 61 64 20  en used instead 
16d21 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e  of this routine.
16d22 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 41 4c   */.      if( AL
16d23 57 41 59 53 28 6e 4f 76 66 6c 29 20 26 26 20 21  WAYS(nOvfl) && !
16d24 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20  pCur->aOverflow 
16d25 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
16d26 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
16d27 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
16d28 20 2f 2a 20 49 66 20 74 68 65 20 6f 76 65 72 66   /* If the overf
16d29 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61  low page-list ca
16d2a 63 68 65 20 68 61 73 20 62 65 65 6e 20 61 6c 6c  che has been all
16d2b 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65 0a 20  ocated and the. 
16d2c 20 20 20 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20     ** entry for 
16d2d 74 68 65 20 66 69 72 73 74 20 72 65 71 75 69 72  the first requir
16d2e 65 64 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  ed overflow page
16d2f 20 69 73 20 76 61 6c 69 64 2c 20 73 6b 69 70 0a   is valid, skip.
16d30 20 20 20 20 2a 2a 20 64 69 72 65 63 74 6c 79 20      ** directly 
16d31 74 6f 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20  to it..    */.  
16d32 20 20 69 66 28 20 70 43 75 72 2d 3e 61 4f 76 65    if( pCur->aOve
16d33 72 66 6c 6f 77 20 26 26 20 70 43 75 72 2d 3e 61  rflow && pCur->a
16d34 4f 76 65 72 66 6c 6f 77 5b 6f 66 66 73 65 74 2f  Overflow[offset/
16d35 6f 76 66 6c 53 69 7a 65 5d 20 29 7b 0a 20 20 20  ovflSize] ){.   
16d36 20 20 20 69 49 64 78 20 3d 20 28 6f 66 66 73 65     iIdx = (offse
16d37 74 2f 6f 76 66 6c 53 69 7a 65 29 3b 0a 20 20 20  t/ovflSize);.   
16d38 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20 70 43     nextPage = pC
16d39 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49  ur->aOverflow[iI
16d3a 64 78 5d 3b 0a 20 20 20 20 20 20 6f 66 66 73 65  dx];.      offse
16d3b 74 20 3d 20 28 6f 66 66 73 65 74 25 6f 76 66 6c  t = (offset%ovfl
16d3c 53 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e  Size);.    }.#en
16d3d 64 69 66 0a 0a 20 20 20 20 66 6f 72 28 20 3b 20  dif..    for( ; 
16d3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
16d3f 20 61 6d 74 3e 30 20 26 26 20 6e 65 78 74 50 61   amt>0 && nextPa
16d40 67 65 3b 20 69 49 64 78 2b 2b 29 7b 0a 0a 23 69  ge; iIdx++){..#i
16d41 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
16d42 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20 20  T_INCRBLOB.     
16d43 20 2f 2a 20 49 66 20 72 65 71 75 69 72 65 64 2c   /* If required,
16d44 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6f 76   populate the ov
16d45 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74  erflow page-list
16d46 20 63 61 63 68 65 2e 20 2a 2f 0a 20 20 20 20 20   cache. */.     
16d47 20 69 66 28 20 70 43 75 72 2d 3e 61 4f 76 65 72   if( pCur->aOver
16d48 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 20  flow ){.        
16d49 61 73 73 65 72 74 28 21 70 43 75 72 2d 3e 61 4f  assert(!pCur->aO
16d4a 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d 20 7c 7c  verflow[iIdx] ||
16d4b 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77   pCur->aOverflow
16d4c 5b 69 49 64 78 5d 3d 3d 6e 65 78 74 50 61 67 65  [iIdx]==nextPage
16d4d 29 3b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d  );.        pCur-
16d4e 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d  >aOverflow[iIdx]
16d4f 20 3d 20 6e 65 78 74 50 61 67 65 3b 0a 20 20 20   = nextPage;.   
16d50 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20     }.#endif..   
16d51 20 20 20 69 66 28 20 6f 66 66 73 65 74 3e 3d 6f     if( offset>=o
16d52 76 66 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 20  vflSize ){.     
16d53 20 20 20 2f 2a 20 54 68 65 20 6f 6e 6c 79 20 72     /* The only r
16d54 65 61 73 6f 6e 20 74 6f 20 72 65 61 64 20 74 68  eason to read th
16d55 69 73 20 70 61 67 65 20 69 73 20 74 6f 20 6f 62  is page is to ob
16d56 74 61 69 6e 20 74 68 65 20 70 61 67 65 0a 20 20  tain the page.  
16d57 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20        ** number 
16d58 66 6f 72 20 74 68 65 20 6e 65 78 74 20 70 61 67  for the next pag
16d59 65 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f  e in the overflo
16d5a 77 20 63 68 61 69 6e 2e 20 54 68 65 20 70 61 67  w chain. The pag
16d5b 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 64 61 74  e.        ** dat
16d5c 61 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65  a is not require
16d5d 64 2e 20 53 6f 20 66 69 72 73 74 20 74 72 79 20  d. So first try 
16d5e 74 6f 20 6c 6f 6f 6b 75 70 20 74 68 65 20 6f 76  to lookup the ov
16d5f 65 72 66 6c 6f 77 0a 20 20 20 20 20 20 20 20 2a  erflow.        *
16d60 2a 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68  * page-list cach
16d61 65 2c 20 69 66 20 61 6e 79 2c 20 74 68 65 6e 20  e, if any, then 
16d62 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 74 68 65  fall back to the
16d63 20 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 67 65   getOverflowPage
16d64 28 29 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 75  ().        ** fu
16d65 6e 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20  nction..        
16d66 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
16d67 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a  E_OMIT_INCRBLOB.
16d68 20 20 20 20 20 20 20 20 69 66 28 20 70 43 75 72          if( pCur
16d69 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 26 26 20 70  ->aOverflow && p
16d6a 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69  Cur->aOverflow[i
16d6b 49 64 78 2b 31 5d 20 29 7b 0a 20 20 20 20 20 20  Idx+1] ){.      
16d6c 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20 70      nextPage = p
16d6d 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69  Cur->aOverflow[i
16d6e 49 64 78 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20  Idx+1];.        
16d6f 7d 20 65 6c 73 65 20 0a 23 65 6e 64 69 66 0a 20  } else .#endif. 
16d70 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 67 65           rc = ge
16d71 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 70 42  tOverflowPage(pB
16d72 74 2c 20 6e 65 78 74 50 61 67 65 2c 20 30 2c 20  t, nextPage, 0, 
16d73 26 6e 65 78 74 50 61 67 65 29 3b 0a 20 20 20 20  &nextPage);.    
16d74 20 20 20 20 6f 66 66 73 65 74 20 2d 3d 20 6f 76      offset -= ov
16d75 66 6c 53 69 7a 65 3b 0a 20 20 20 20 20 20 7d 65  flSize;.      }e
16d76 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  lse{.        /* 
16d77 4e 65 65 64 20 74 6f 20 72 65 61 64 20 74 68 69  Need to read thi
16d78 73 20 70 61 67 65 20 70 72 6f 70 65 72 6c 79 2e  s page properly.
16d79 20 49 74 20 63 6f 6e 74 61 69 6e 73 20 73 6f 6d   It contains som
16d7a 65 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20  e of the.       
16d7b 20 2a 2a 20 72 61 6e 67 65 20 6f 66 20 64 61 74   ** range of dat
16d7c 61 20 74 68 61 74 20 69 73 20 62 65 69 6e 67 20  a that is being 
16d7d 72 65 61 64 20 28 65 4f 70 3d 3d 30 29 20 6f 72  read (eOp==0) or
16d7e 20 77 72 69 74 74 65 6e 20 28 65 4f 70 21 3d 30   written (eOp!=0
16d7f 29 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  )..        */.  
16d80 20 20 20 20 20 20 44 62 50 61 67 65 20 2a 70 44        DbPage *pD
16d81 62 50 61 67 65 3b 0a 20 20 20 20 20 20 20 20 69  bPage;.        i
16d82 6e 74 20 61 20 3d 20 61 6d 74 3b 0a 20 20 20 20  nt a = amt;.    
16d83 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
16d84 50 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70 50  PagerGet(pBt->pP
16d85 61 67 65 72 2c 20 6e 65 78 74 50 61 67 65 2c 20  ager, nextPage, 
16d86 26 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20  &pDbPage);.     
16d87 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
16d88 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
16d89 20 20 61 50 61 79 6c 6f 61 64 20 3d 20 73 71 6c    aPayload = sql
16d8a 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61  ite3PagerGetData
16d8b 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20  (pDbPage);.     
16d8c 20 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20       nextPage = 
16d8d 67 65 74 34 62 79 74 65 28 61 50 61 79 6c 6f 61  get4byte(aPayloa
16d8e 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  d);.          if
16d8f 28 20 61 20 2b 20 6f 66 66 73 65 74 20 3e 20 6f  ( a + offset > o
16d90 76 66 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 20  vflSize ){.     
16d91 20 20 20 20 20 20 20 61 20 3d 20 6f 76 66 6c 53         a = ovflS
16d92 69 7a 65 20 2d 20 6f 66 66 73 65 74 3b 0a 20 20  ize - offset;.  
16d93 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
16d94 20 20 20 20 72 63 20 3d 20 63 6f 70 79 50 61 79      rc = copyPay
16d95 6c 6f 61 64 28 26 61 50 61 79 6c 6f 61 64 5b 6f  load(&aPayload[o
16d96 66 66 73 65 74 2b 34 5d 2c 20 70 42 75 66 2c 20  ffset+4], pBuf, 
16d97 61 2c 20 65 4f 70 2c 20 70 44 62 50 61 67 65 29  a, eOp, pDbPage)
16d98 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
16d99 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 44  te3PagerUnref(pD
16d9a 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20  bPage);.        
16d9b 20 20 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20    offset = 0;.  
16d9c 20 20 20 20 20 20 20 20 61 6d 74 20 2d 3d 20 61          amt -= a
16d9d 3b 0a 20 20 20 20 20 20 20 20 20 20 70 42 75 66  ;.          pBuf
16d9e 20 2b 3d 20 61 3b 0a 20 20 20 20 20 20 20 20 7d   += a;.        }
16d9f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
16da0 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   }..  if( rc==SQ
16da1 4c 49 54 45 5f 4f 4b 20 26 26 20 61 6d 74 3e 30  LITE_OK && amt>0
16da2 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
16da3 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
16da4 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  PT;.  }.  return
16da5 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
16da6 61 64 20 70 61 72 74 20 6f 66 20 74 68 65 20 6b  ad part of the k
16da7 65 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ey associated wi
16da8 74 68 20 63 75 72 73 6f 72 20 70 43 75 72 2e 20  th cursor pCur. 
16da9 20 45 78 61 63 74 6c 79 0a 2a 2a 20 22 61 6d 74   Exactly.** "amt
16daa 22 20 62 79 74 65 73 20 77 69 6c 6c 20 62 65 20  " bytes will be 
16dab 74 72 61 6e 73 66 65 72 65 64 20 69 6e 74 6f 20  transfered into 
16dac 70 42 75 66 5b 5d 2e 20 20 54 68 65 20 74 72 61  pBuf[].  The tra
16dad 6e 73 66 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20  nsfer.** begins 
16dae 61 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a  at "offset"..**.
16daf 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75  ** The caller mu
16db0 73 74 20 65 6e 73 75 72 65 20 74 68 61 74 20 70  st ensure that p
16db1 43 75 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20  Cur is pointing 
16db2 74 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 0a 2a  to a valid row.*
16db3 2a 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a  * in the table..
16db4 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c  **.** Return SQL
16db5 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73  ITE_OK on succes
16db6 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f  s or an error co
16db7 64 65 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67  de if anything g
16db8 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20 41  oes.** wrong.  A
16db9 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72  n error is retur
16dba 6e 65 64 20 69 66 20 22 6f 66 66 73 65 74 2b 61  ned if "offset+a
16dbb 6d 74 22 20 69 73 20 6c 61 72 67 65 72 20 74 68  mt" is larger th
16dbc 61 6e 0a 2a 2a 20 74 68 65 20 61 76 61 69 6c 61  an.** the availa
16dbd 62 6c 65 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a  ble payload..*/.
16dbe 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
16dbf 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b  nt sqlite3BtreeK
16dc0 65 79 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  ey(BtCursor *pCu
16dc1 72 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75  r, u32 offset, u
16dc2 33 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42  32 amt, void *pB
16dc3 75 66 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63  uf){.  assert( c
16dc4 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
16dc5 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72  pCur) );.  asser
16dc6 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  t( pCur->eState=
16dc7 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b  =CURSOR_VALID );
16dc8 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
16dc9 3e 69 50 61 67 65 3e 3d 30 20 26 26 20 70 43 75  >iPage>=0 && pCu
16dca 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
16dcb 69 50 61 67 65 5d 20 29 3b 0a 20 20 61 73 73 65  iPage] );.  asse
16dcc 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b  rt( pCur->aiIdx[
16dcd 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43 75  pCur->iPage]<pCu
16dce 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
16dcf 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b  iPage]->nCell );
16dd0 0a 20 20 72 65 74 75 72 6e 20 61 63 63 65 73 73  .  return access
16dd1 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 6f 66  Payload(pCur, of
16dd2 66 73 65 74 2c 20 61 6d 74 2c 20 28 75 6e 73 69  fset, amt, (unsi
16dd3 67 6e 65 64 20 63 68 61 72 2a 29 70 42 75 66 2c  gned char*)pBuf,
16dd4 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   0);.}../*.** Re
16dd5 61 64 20 70 61 72 74 20 6f 66 20 74 68 65 20 64  ad part of the d
16dd6 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 77  ata associated w
16dd7 69 74 68 20 63 75 72 73 6f 72 20 70 43 75 72 2e  ith cursor pCur.
16dd8 20 20 45 78 61 63 74 6c 79 0a 2a 2a 20 22 61 6d    Exactly.** "am
16dd9 74 22 20 62 79 74 65 73 20 77 69 6c 6c 20 62 65  t" bytes will be
16dda 20 74 72 61 6e 73 66 65 72 65 64 20 69 6e 74 6f   transfered into
16ddb 20 70 42 75 66 5b 5d 2e 20 20 54 68 65 20 74 72   pBuf[].  The tr
16ddc 61 6e 73 66 65 72 0a 2a 2a 20 62 65 67 69 6e 73  ansfer.** begins
16ddd 20 61 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a   at "offset"..**
16dde 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54  .** Return SQLIT
16ddf 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20  E_OK on success 
16de0 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  or an error code
16de1 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65   if anything goe
16de2 73 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20 41 6e 20  s.** wrong.  An 
16de3 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65  error is returne
16de4 64 20 69 66 20 22 6f 66 66 73 65 74 2b 61 6d 74  d if "offset+amt
16de5 22 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e  " is larger than
16de6 0a 2a 2a 20 74 68 65 20 61 76 61 69 6c 61 62 6c  .** the availabl
16de7 65 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 51  e payload..*/.SQ
16de8 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
16de9 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74   sqlite3BtreeDat
16dea 61 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  a(BtCursor *pCur
16deb 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33  , u32 offset, u3
16dec 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42 75  2 amt, void *pBu
16ded 66 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 23  f){.  int rc;..#
16dee 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
16def 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 69 66  IT_INCRBLOB.  if
16df0 20 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d   ( pCur->eState=
16df1 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20  =CURSOR_INVALID 
16df2 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
16df3 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a  LITE_ABORT;.  }.
16df4 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74  #endif..  assert
16df5 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
16df6 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63  ex(pCur) );.  rc
16df7 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72   = restoreCursor
16df8 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a  Position(pCur);.
16df9 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
16dfa 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72  _OK ){.    asser
16dfb 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  t( pCur->eState=
16dfc 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b  =CURSOR_VALID );
16dfd 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75  .    assert( pCu
16dfe 72 2d 3e 69 50 61 67 65 3e 3d 30 20 26 26 20 70  r->iPage>=0 && p
16dff 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
16e00 2d 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 20 20  ->iPage] );.    
16e01 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69  assert( pCur->ai
16e02 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  Idx[pCur->iPage]
16e03 3c 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43  <pCur->apPage[pC
16e04 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c  ur->iPage]->nCel
16e05 6c 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 61 63  l );.    rc = ac
16e06 63 65 73 73 50 61 79 6c 6f 61 64 28 70 43 75 72  cessPayload(pCur
16e07 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 70  , offset, amt, p
16e08 42 75 66 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72  Buf, 0);.  }.  r
16e09 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
16e0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  ** Return a poin
16e0b 74 65 72 20 74 6f 20 70 61 79 6c 6f 61 64 20 69  ter to payload i
16e0c 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20  nformation from 
16e0d 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 74  the entry that t
16e0e 68 65 20 0a 2a 2a 20 70 43 75 72 20 63 75 72 73  he .** pCur curs
16e0f 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74  or is pointing t
16e10 6f 2e 20 20 54 68 65 20 70 6f 69 6e 74 65 72 20  o.  The pointer 
16e11 69 73 20 74 6f 20 74 68 65 20 62 65 67 69 6e 6e  is to the beginn
16e12 69 6e 67 20 6f 66 0a 2a 2a 20 74 68 65 20 6b 65  ing of.** the ke
16e13 79 20 69 66 20 73 6b 69 70 4b 65 79 3d 3d 30 20  y if skipKey==0 
16e14 61 6e 64 20 69 74 20 70 6f 69 6e 74 73 20 74 6f  and it points to
16e15 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f   the beginning o
16e16 66 20 64 61 74 61 20 69 66 0a 2a 2a 20 73 6b 69  f data if.** ski
16e17 70 4b 65 79 3d 3d 31 2e 20 20 54 68 65 20 6e 75  pKey==1.  The nu
16e18 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
16e19 20 61 76 61 69 6c 61 62 6c 65 20 6b 65 79 2f 64   available key/d
16e1a 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 0a 2a  ata is written.*
16e1b 2a 20 69 6e 74 6f 20 2a 70 41 6d 74 2e 20 20 49  * into *pAmt.  I
16e1c 66 20 2a 70 41 6d 74 3d 3d 30 2c 20 74 68 65 6e  f *pAmt==0, then
16e1d 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   the value retur
16e1e 6e 65 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65 0a  ned will not be.
16e1f 2a 2a 20 61 20 76 61 6c 69 64 20 70 6f 69 6e 74  ** a valid point
16e20 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  er..**.** This r
16e21 6f 75 74 69 6e 65 20 69 73 20 61 6e 20 6f 70 74  outine is an opt
16e22 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 49 74 20 69  imization.  It i
16e23 73 20 63 6f 6d 6d 6f 6e 20 66 6f 72 20 74 68 65  s common for the
16e24 20 65 6e 74 69 72 65 20 6b 65 79 0a 2a 2a 20 61   entire key.** a
16e25 6e 64 20 64 61 74 61 20 74 6f 20 66 69 74 20 6f  nd data to fit o
16e26 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 67 65  n the local page
16e27 20 61 6e 64 20 66 6f 72 20 74 68 65 72 65 20 74   and for there t
16e28 6f 20 62 65 20 6e 6f 20 6f 76 65 72 66 6c 6f 77  o be no overflow
16e29 0a 2a 2a 20 70 61 67 65 73 2e 20 20 57 68 65 6e  .** pages.  When
16e2a 20 74 68 61 74 20 69 73 20 73 6f 2c 20 74 68 69   that is so, thi
16e2b 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 65  s routine can be
16e2c 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20   used to access 
16e2d 74 68 65 0a 2a 2a 20 6b 65 79 20 61 6e 64 20 64  the.** key and d
16e2e 61 74 61 20 77 69 74 68 6f 75 74 20 6d 61 6b 69  ata without maki
16e2f 6e 67 20 61 20 63 6f 70 79 2e 20 20 49 66 20 74  ng a copy.  If t
16e30 68 65 20 6b 65 79 20 61 6e 64 2f 6f 72 20 64 61  he key and/or da
16e31 74 61 20 73 70 69 6c 6c 73 0a 2a 2a 20 6f 6e 74  ta spills.** ont
16e32 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  o overflow pages
16e33 2c 20 74 68 65 6e 20 61 63 63 65 73 73 50 61 79  , then accessPay
16e34 6c 6f 61 64 28 29 20 6d 75 73 74 20 62 65 20 75  load() must be u
16e35 73 65 64 20 74 6f 20 72 65 61 73 73 65 6d 62 6c  sed to reassembl
16e36 65 0a 2a 2a 20 74 68 65 20 6b 65 79 2f 64 61 74  e.** the key/dat
16e37 61 20 61 6e 64 20 63 6f 70 79 20 69 74 20 69 6e  a and copy it in
16e38 74 6f 20 61 20 70 72 65 61 6c 6c 6f 63 61 74 65  to a preallocate
16e39 64 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20  d buffer..**.** 
16e3a 54 68 65 20 70 6f 69 6e 74 65 72 20 72 65 74 75  The pointer retu
16e3b 72 6e 65 64 20 62 79 20 74 68 69 73 20 72 6f 75  rned by this rou
16e3c 74 69 6e 65 20 6c 6f 6f 6b 73 20 64 69 72 65 63  tine looks direc
16e3d 74 6c 79 20 69 6e 74 6f 20 74 68 65 20 63 61 63  tly into the cac
16e3e 68 65 64 0a 2a 2a 20 70 61 67 65 20 6f 66 20 74  hed.** page of t
16e3f 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68  he database.  Th
16e40 65 20 64 61 74 61 20 6d 69 67 68 74 20 63 68 61  e data might cha
16e41 6e 67 65 20 6f 72 20 6d 6f 76 65 20 74 68 65 20  nge or move the 
16e42 6e 65 78 74 20 74 69 6d 65 0a 2a 2a 20 61 6e 79  next time.** any
16e43 20 62 74 72 65 65 20 72 6f 75 74 69 6e 65 20 69   btree routine i
16e44 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61  s called..*/.sta
16e45 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  tic const unsign
16e46 65 64 20 63 68 61 72 20 2a 66 65 74 63 68 50 61  ed char *fetchPa
16e47 79 6c 6f 61 64 28 0a 20 20 42 74 43 75 72 73 6f  yload(.  BtCurso
16e48 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 2f 2a  r *pCur,      /*
16e49 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67   Cursor pointing
16e4a 20 74 6f 20 65 6e 74 72 79 20 74 6f 20 72 65 61   to entry to rea
16e4b 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20  d from */.  int 
16e4c 2a 70 41 6d 74 2c 20 20 20 20 20 20 20 20 20 20  *pAmt,          
16e4d 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 75   /* Write the nu
16e4e 6d 62 65 72 20 6f 66 20 61 76 61 69 6c 61 62 6c  mber of availabl
16e4f 65 20 62 79 74 65 73 20 68 65 72 65 20 2a 2f 0a  e bytes here */.
16e50 20 20 69 6e 74 20 73 6b 69 70 4b 65 79 20 20 20    int skipKey   
16e51 20 20 20 20 20 20 20 2f 2a 20 72 65 61 64 20 62         /* read b
16e52 65 67 69 6e 6e 69 6e 67 20 61 74 20 64 61 74 61  eginning at data
16e53 20 69 66 20 74 68 69 73 20 69 73 20 74 72 75 65   if this is true
16e54 20 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65   */.){.  unsigne
16e55 64 20 63 68 61 72 20 2a 61 50 61 79 6c 6f 61 64  d char *aPayload
16e56 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  ;.  MemPage *pPa
16e57 67 65 3b 0a 20 20 75 33 32 20 6e 4b 65 79 3b 0a  ge;.  u32 nKey;.
16e58 20 20 75 33 32 20 6e 4c 6f 63 61 6c 3b 0a 0a 20    u32 nLocal;.. 
16e59 20 61 73 73 65 72 74 28 20 70 43 75 72 21 3d 30   assert( pCur!=0
16e5a 20 26 26 20 70 43 75 72 2d 3e 69 50 61 67 65 3e   && pCur->iPage>
16e5b 3d 30 20 26 26 20 70 43 75 72 2d 3e 61 70 50 61  =0 && pCur->apPa
16e5c 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29  ge[pCur->iPage])
16e5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  ;.  assert( pCur
16e5e 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
16e5f 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65  _VALID );.  asse
16e60 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d  rt( cursorHoldsM
16e61 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20  utex(pCur) );.  
16e62 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70  pPage = pCur->ap
16e63 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
16e64 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75  ];.  assert( pCu
16e65 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
16e66 50 61 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 65  Page]<pPage->nCe
16e67 6c 6c 20 29 3b 0a 20 20 69 66 28 20 4e 45 56 45  ll );.  if( NEVE
16e68 52 28 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69  R(pCur->info.nSi
16e69 7a 65 3d 3d 30 29 20 29 7b 0a 20 20 20 20 62 74  ze==0) ){.    bt
16e6a 72 65 65 50 61 72 73 65 43 65 6c 6c 28 70 43 75  reeParseCell(pCu
16e6b 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
16e6c 69 50 61 67 65 5d 2c 20 70 43 75 72 2d 3e 61 69  iPage], pCur->ai
16e6d 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  Idx[pCur->iPage]
16e6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
16e6f 20 20 20 20 20 26 70 43 75 72 2d 3e 69 6e 66 6f       &pCur->info
16e70 29 3b 0a 20 20 7d 0a 20 20 61 50 61 79 6c 6f 61  );.  }.  aPayloa
16e71 64 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 70  d = pCur->info.p
16e72 43 65 6c 6c 3b 0a 20 20 61 50 61 79 6c 6f 61 64  Cell;.  aPayload
16e73 20 2b 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e   += pCur->info.n
16e74 48 65 61 64 65 72 3b 0a 20 20 69 66 28 20 70 50  Header;.  if( pP
16e75 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20  age->intKey ){. 
16e76 20 20 20 6e 4b 65 79 20 3d 20 30 3b 0a 20 20 7d     nKey = 0;.  }
16e77 65 6c 73 65 7b 0a 20 20 20 20 6e 4b 65 79 20 3d  else{.    nKey =
16e78 20 28 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f   (int)pCur->info
16e79 2e 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28  .nKey;.  }.  if(
16e7a 20 73 6b 69 70 4b 65 79 20 29 7b 0a 20 20 20 20   skipKey ){.    
16e7b 61 50 61 79 6c 6f 61 64 20 2b 3d 20 6e 4b 65 79  aPayload += nKey
16e7c 3b 0a 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70  ;.    nLocal = p
16e7d 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c  Cur->info.nLocal
16e7e 20 2d 20 6e 4b 65 79 3b 0a 20 20 7d 65 6c 73 65   - nKey;.  }else
16e7f 7b 0a 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70  {.    nLocal = p
16e80 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c  Cur->info.nLocal
16e81 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 4c  ;.    assert( nL
16e82 6f 63 61 6c 3c 3d 6e 4b 65 79 20 29 3b 0a 20 20  ocal<=nKey );.  
16e83 7d 0a 20 20 2a 70 41 6d 74 20 3d 20 6e 4c 6f 63  }.  *pAmt = nLoc
16e84 61 6c 3b 0a 20 20 72 65 74 75 72 6e 20 61 50 61  al;.  return aPa
16e85 79 6c 6f 61 64 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  yload;.}.../*.**
16e86 20 46 6f 72 20 74 68 65 20 65 6e 74 72 79 20 74   For the entry t
16e87 68 61 74 20 63 75 72 73 6f 72 20 70 43 75 72 20  hat cursor pCur 
16e88 69 73 20 70 6f 69 6e 74 20 74 6f 2c 20 72 65 74  is point to, ret
16e89 75 72 6e 20 61 73 0a 2a 2a 20 6d 61 6e 79 20 62  urn as.** many b
16e8a 79 74 65 73 20 6f 66 20 74 68 65 20 6b 65 79 20  ytes of the key 
16e8b 6f 72 20 64 61 74 61 20 61 73 20 61 72 65 20 61  or data as are a
16e8c 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20  vailable on the 
16e8d 6c 6f 63 61 6c 0a 2a 2a 20 62 2d 74 72 65 65 20  local.** b-tree 
16e8e 70 61 67 65 2e 20 20 57 72 69 74 65 20 74 68 65  page.  Write the
16e8f 20 6e 75 6d 62 65 72 20 6f 66 20 61 76 61 69 6c   number of avail
16e90 61 62 6c 65 20 62 79 74 65 73 20 69 6e 74 6f 20  able bytes into 
16e91 2a 70 41 6d 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  *pAmt..**.** The
16e92 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e 65   pointer returne
16e93 64 20 69 73 20 65 70 68 65 6d 65 72 61 6c 2e 20  d is ephemeral. 
16e94 20 54 68 65 20 6b 65 79 2f 64 61 74 61 20 6d 61   The key/data ma
16e95 79 20 6d 6f 76 65 0a 2a 2a 20 6f 72 20 62 65 20  y move.** or be 
16e96 64 65 73 74 72 6f 79 65 64 20 6f 6e 20 74 68 65  destroyed on the
16e97 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20 61 6e   next call to an
16e98 79 20 42 74 72 65 65 20 72 6f 75 74 69 6e 65 2c  y Btree routine,
16e99 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 63 61  .** including ca
16e9a 6c 6c 73 20 66 72 6f 6d 20 6f 74 68 65 72 20 74  lls from other t
16e9b 68 72 65 61 64 73 20 61 67 61 69 6e 73 74 20 74  hreads against t
16e9c 68 65 20 73 61 6d 65 20 63 61 63 68 65 2e 0a 2a  he same cache..*
16e9d 2a 20 48 65 6e 63 65 2c 20 61 20 6d 75 74 65 78  * Hence, a mutex
16e9e 20 6f 6e 20 74 68 65 20 42 74 53 68 61 72 65 64   on the BtShared
16e9f 20 73 68 6f 75 6c 64 20 62 65 20 68 65 6c 64 20   should be held 
16ea0 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67  prior to calling
16ea1 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65  .** this routine
16ea2 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f  ..**.** These ro
16ea3 75 74 69 6e 65 73 20 69 73 20 75 73 65 64 20 74  utines is used t
16ea4 6f 20 67 65 74 20 71 75 69 63 6b 20 61 63 63 65  o get quick acce
16ea5 73 73 20 74 6f 20 6b 65 79 20 61 6e 64 20 64 61  ss to key and da
16ea6 74 61 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f 6d  ta.** in the com
16ea7 6d 6f 6e 20 63 61 73 65 20 77 68 65 72 65 20 6e  mon case where n
16ea8 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  o overflow pages
16ea9 20 61 72 65 20 75 73 65 64 2e 0a 2a 2f 0a 53 51   are used..*/.SQ
16eaa 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
16eab 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
16eac 42 74 72 65 65 4b 65 79 46 65 74 63 68 28 42 74  BtreeKeyFetch(Bt
16ead 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e  Cursor *pCur, in
16eae 74 20 2a 70 41 6d 74 29 7b 0a 20 20 63 6f 6e 73  t *pAmt){.  cons
16eaf 74 20 76 6f 69 64 20 2a 70 20 3d 20 30 3b 0a 20  t void *p = 0;. 
16eb0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
16eb1 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72  _mutex_held(pCur
16eb2 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75  ->pBtree->db->mu
16eb3 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
16eb4 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
16eb5 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 69 66  ex(pCur) );.  if
16eb6 28 20 41 4c 57 41 59 53 28 70 43 75 72 2d 3e 65  ( ALWAYS(pCur->e
16eb7 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41  State==CURSOR_VA
16eb8 4c 49 44 29 20 29 7b 0a 20 20 20 20 70 20 3d 20  LID) ){.    p = 
16eb9 28 63 6f 6e 73 74 20 76 6f 69 64 2a 29 66 65 74  (const void*)fet
16eba 63 68 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20  chPayload(pCur, 
16ebb 70 41 6d 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  pAmt, 0);.  }.  
16ebc 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 53 51 4c 49  return p;.}.SQLI
16ebd 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
16ebe 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74   void *sqlite3Bt
16ebf 72 65 65 44 61 74 61 46 65 74 63 68 28 42 74 43  reeDataFetch(BtC
16ec0 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74  ursor *pCur, int
16ec1 20 2a 70 41 6d 74 29 7b 0a 20 20 63 6f 6e 73 74   *pAmt){.  const
16ec2 20 76 6f 69 64 20 2a 70 20 3d 20 30 3b 0a 20 20   void *p = 0;.  
16ec3 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
16ec4 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d  mutex_held(pCur-
16ec5 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74  >pBtree->db->mut
16ec6 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ex) );.  assert(
16ec7 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
16ec8 78 28 70 43 75 72 29 20 29 3b 0a 20 20 69 66 28  x(pCur) );.  if(
16ec9 20 41 4c 57 41 59 53 28 70 43 75 72 2d 3e 65 53   ALWAYS(pCur->eS
16eca 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c  tate==CURSOR_VAL
16ecb 49 44 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 28  ID) ){.    p = (
16ecc 63 6f 6e 73 74 20 76 6f 69 64 2a 29 66 65 74 63  const void*)fetc
16ecd 68 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 70  hPayload(pCur, p
16ece 41 6d 74 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 72  Amt, 1);.  }.  r
16ecf 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a  eturn p;.}.../*.
16ed0 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73  ** Move the curs
16ed1 6f 72 20 64 6f 77 6e 20 74 6f 20 61 20 6e 65 77  or down to a new
16ed2 20 63 68 69 6c 64 20 70 61 67 65 2e 20 20 54 68   child page.  Th
16ed3 65 20 6e 65 77 50 67 6e 6f 20 61 72 67 75 6d 65  e newPgno argume
16ed4 6e 74 20 69 73 20 74 68 65 0a 2a 2a 20 70 61 67  nt is the.** pag
16ed5 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
16ed6 63 68 69 6c 64 20 70 61 67 65 20 74 6f 20 6d 6f  child page to mo
16ed7 76 65 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ve to..**.** Thi
16ed8 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
16ed9 6e 73 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  ns SQLITE_CORRUP
16eda 54 20 69 66 20 74 68 65 20 70 61 67 65 2d 68 65  T if the page-he
16edb 61 64 65 72 20 66 6c 61 67 73 20 66 69 65 6c 64  ader flags field
16edc 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20 63   of.** the new c
16edd 68 69 6c 64 20 70 61 67 65 20 64 6f 65 73 20 6e  hild page does n
16ede 6f 74 20 6d 61 74 63 68 20 74 68 65 20 66 6c 61  ot match the fla
16edf 67 73 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20  gs field of the 
16ee0 70 61 72 65 6e 74 20 28 69 2e 65 2e 0a 2a 2a 20  parent (i.e..** 
16ee1 69 66 20 61 6e 20 69 6e 74 6b 65 79 20 70 61 67  if an intkey pag
16ee2 65 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 20  e appears to be 
16ee3 74 68 65 20 70 61 72 65 6e 74 20 6f 66 20 61 20  the parent of a 
16ee4 6e 6f 6e 2d 69 6e 74 6b 65 79 20 70 61 67 65 2c  non-intkey page,
16ee5 20 6f 72 0a 2a 2a 20 76 69 63 65 2d 76 65 72 73   or.** vice-vers
16ee6 61 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  a)..*/.static in
16ee7 74 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 42 74  t moveToChild(Bt
16ee8 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 75 33  Cursor *pCur, u3
16ee9 32 20 6e 65 77 50 67 6e 6f 29 7b 0a 20 20 69 6e  2 newPgno){.  in
16eea 74 20 72 63 3b 0a 20 20 69 6e 74 20 69 20 3d 20  t rc;.  int i = 
16eeb 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 4d  pCur->iPage;.  M
16eec 65 6d 50 61 67 65 20 2a 70 4e 65 77 50 61 67 65  emPage *pNewPage
16eed 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  ;.  BtShared *pB
16eee 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 0a 0a  t = pCur->pBt;..
16eef 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
16ef0 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
16ef1 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43   );.  assert( pC
16ef2 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53  ur->eState==CURS
16ef3 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73  OR_VALID );.  as
16ef4 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61 67  sert( pCur->iPag
16ef5 65 3c 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44  e<BTCURSOR_MAX_D
16ef6 45 50 54 48 20 29 3b 0a 20 20 69 66 28 20 70 43  EPTH );.  if( pC
16ef7 75 72 2d 3e 69 50 61 67 65 3e 3d 28 42 54 43 55  ur->iPage>=(BTCU
16ef8 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 2d 31  RSOR_MAX_DEPTH-1
16ef9 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
16efa 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
16efb 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20  KPT;.  }.  rc = 
16efc 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 70  getAndInitPage(p
16efd 42 74 2c 20 6e 65 77 50 67 6e 6f 2c 20 26 70 4e  Bt, newPgno, &pN
16efe 65 77 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72  ewPage);.  if( r
16eff 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  c ) return rc;. 
16f00 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 2b   pCur->apPage[i+
16f01 31 5d 20 3d 20 70 4e 65 77 50 61 67 65 3b 0a 20  1] = pNewPage;. 
16f02 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 2b 31   pCur->aiIdx[i+1
16f03 5d 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 69  ] = 0;.  pCur->i
16f04 50 61 67 65 2b 2b 3b 0a 0a 20 20 70 43 75 72 2d  Page++;..  pCur-
16f05 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b  >info.nSize = 0;
16f06 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b  .  pCur->validNK
16f07 65 79 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 4e  ey = 0;.  if( pN
16f08 65 77 50 61 67 65 2d 3e 6e 43 65 6c 6c 3c 31 20  ewPage->nCell<1 
16f09 7c 7c 20 70 4e 65 77 50 61 67 65 2d 3e 69 6e 74  || pNewPage->int
16f0a 4b 65 79 21 3d 70 43 75 72 2d 3e 61 70 50 61 67  Key!=pCur->apPag
16f0b 65 5b 69 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a  e[i]->intKey ){.
16f0c 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
16f0d 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
16f0e 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
16f0f 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64  ITE_OK;.}..#ifnd
16f10 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20  ef NDEBUG./*.** 
16f11 50 61 67 65 20 70 50 61 72 65 6e 74 20 69 73 20  Page pParent is 
16f12 61 6e 20 69 6e 74 65 72 6e 61 6c 20 28 6e 6f 6e  an internal (non
16f13 2d 6c 65 61 66 29 20 74 72 65 65 20 70 61 67 65  -leaf) tree page
16f14 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
16f15 0a 2a 2a 20 61 73 73 65 72 74 73 20 74 68 61 74  .** asserts that
16f16 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 43 68   page number iCh
16f17 69 6c 64 20 69 73 20 74 68 65 20 6c 65 66 74 2d  ild is the left-
16f18 63 68 69 6c 64 20 69 66 20 74 68 65 20 69 49 64  child if the iId
16f19 78 27 74 68 0a 2a 2a 20 63 65 6c 6c 20 69 6e 20  x'th.** cell in 
16f1a 70 61 67 65 20 70 50 61 72 65 6e 74 2e 20 4f 72  page pParent. Or
16f1b 2c 20 69 66 20 69 49 64 78 20 69 73 20 65 71 75  , if iIdx is equ
16f1c 61 6c 20 74 6f 20 74 68 65 20 74 6f 74 61 6c 20  al to the total 
16f1d 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 65 6c  number of.** cel
16f1e 6c 73 20 69 6e 20 70 50 61 72 65 6e 74 2c 20 74  ls in pParent, t
16f1f 68 61 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20  hat page number 
16f20 69 43 68 69 6c 64 20 69 73 20 74 68 65 20 72 69  iChild is the ri
16f21 67 68 74 2d 63 68 69 6c 64 20 6f 66 0a 2a 2a 20  ght-child of.** 
16f22 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61  the page..*/.sta
16f23 74 69 63 20 76 6f 69 64 20 61 73 73 65 72 74 50  tic void assertP
16f24 61 72 65 6e 74 49 6e 64 65 78 28 4d 65 6d 50 61  arentIndex(MemPa
16f25 67 65 20 2a 70 50 61 72 65 6e 74 2c 20 69 6e 74  ge *pParent, int
16f26 20 69 49 64 78 2c 20 50 67 6e 6f 20 69 43 68 69   iIdx, Pgno iChi
16f27 6c 64 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69  ld){.  assert( i
16f28 49 64 78 3c 3d 70 50 61 72 65 6e 74 2d 3e 6e 43  Idx<=pParent->nC
16f29 65 6c 6c 20 29 3b 0a 20 20 69 66 28 20 69 49 64  ell );.  if( iId
16f2a 78 3d 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c  x==pParent->nCel
16f2b 6c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  l ){.    assert(
16f2c 20 67 65 74 34 62 79 74 65 28 26 70 50 61 72 65   get4byte(&pPare
16f2d 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e  nt->aData[pParen
16f2e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29  t->hdrOffset+8])
16f2f 3d 3d 69 43 68 69 6c 64 20 29 3b 0a 20 20 7d 65  ==iChild );.  }e
16f30 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
16f31 20 67 65 74 34 62 79 74 65 28 66 69 6e 64 43 65   get4byte(findCe
16f32 6c 6c 28 70 50 61 72 65 6e 74 2c 20 69 49 64 78  ll(pParent, iIdx
16f33 29 29 3d 3d 69 43 68 69 6c 64 20 29 3b 0a 20 20  ))==iChild );.  
16f34 7d 0a 7d 0a 23 65 6c 73 65 0a 23 20 20 64 65 66  }.}.#else.#  def
16f35 69 6e 65 20 61 73 73 65 72 74 50 61 72 65 6e 74  ine assertParent
16f36 49 6e 64 65 78 28 78 2c 79 2c 7a 29 20 0a 23 65  Index(x,y,z) .#e
16f37 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65  ndif../*.** Move
16f38 20 74 68 65 20 63 75 72 73 6f 72 20 75 70 20 74   the cursor up t
16f39 6f 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67  o the parent pag
16f3a 65 2e 0a 2a 2a 0a 2a 2a 20 70 43 75 72 2d 3e 69  e..**.** pCur->i
16f3b 64 78 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  dx is set to the
16f3c 20 63 65 6c 6c 20 69 6e 64 65 78 20 74 68 61 74   cell index that
16f3d 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 70 6f   contains the po
16f3e 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20  inter.** to the 
16f3f 70 61 67 65 20 77 65 20 61 72 65 20 63 6f 6d 69  page we are comi
16f40 6e 67 20 66 72 6f 6d 2e 20 20 49 66 20 77 65 20  ng from.  If we 
16f41 61 72 65 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20  are coming from 
16f42 74 68 65 0a 2a 2a 20 72 69 67 68 74 2d 6d 6f 73  the.** right-mos
16f43 74 20 63 68 69 6c 64 20 70 61 67 65 20 74 68 65  t child page the
16f44 6e 20 70 43 75 72 2d 3e 69 64 78 20 69 73 20 73  n pCur->idx is s
16f45 65 74 20 74 6f 20 6f 6e 65 20 6d 6f 72 65 20 74  et to one more t
16f46 68 61 6e 0a 2a 2a 20 74 68 65 20 6c 61 72 67 65  han.** the large
16f47 73 74 20 63 65 6c 6c 20 69 6e 64 65 78 2e 0a 2a  st cell index..*
16f48 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 6f  /.static void mo
16f49 76 65 54 6f 50 61 72 65 6e 74 28 42 74 43 75 72  veToParent(BtCur
16f4a 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73  sor *pCur){.  as
16f4b 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64  sert( cursorHold
16f4c 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a  sMutex(pCur) );.
16f4d 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
16f4e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56  eState==CURSOR_V
16f4f 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74  ALID );.  assert
16f50 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 30 20  ( pCur->iPage>0 
16f51 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75  );.  assert( pCu
16f52 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
16f53 69 50 61 67 65 5d 20 29 3b 0a 20 20 61 73 73 65  iPage] );.  asse
16f54 72 74 50 61 72 65 6e 74 49 6e 64 65 78 28 0a 20  rtParentIndex(. 
16f55 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b     pCur->apPage[
16f56 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 2c 20  pCur->iPage-1], 
16f57 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78  .    pCur->aiIdx
16f58 5b 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 2c  [pCur->iPage-1],
16f59 20 0a 20 20 20 20 70 43 75 72 2d 3e 61 70 50 61   .    pCur->apPa
16f5a 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d  ge[pCur->iPage]-
16f5b 3e 70 67 6e 6f 0a 20 20 29 3b 0a 20 20 72 65 6c  >pgno.  );.  rel
16f5c 65 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61  easePage(pCur->a
16f5d 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
16f5e 65 5d 29 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61  e]);.  pCur->iPa
16f5f 67 65 2d 2d 3b 0a 20 20 70 43 75 72 2d 3e 69 6e  ge--;.  pCur->in
16f60 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20  fo.nSize = 0;.  
16f61 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20  pCur->validNKey 
16f62 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f  = 0;.}../*.** Mo
16f63 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f  ve the cursor to
16f64 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 72 6f   point to the ro
16f65 6f 74 20 70 61 67 65 20 6f 66 20 69 74 73 20 62  ot page of its b
16f66 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 65 2e  -tree structure.
16f67 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 74 61  .**.** If the ta
16f68 62 6c 65 20 68 61 73 20 61 20 76 69 72 74 75 61  ble has a virtua
16f69 6c 20 72 6f 6f 74 20 70 61 67 65 2c 20 74 68 65  l root page, the
16f6a 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  n the cursor is 
16f6b 6d 6f 76 65 64 20 74 6f 20 70 6f 69 6e 74 0a 2a  moved to point.*
16f6c 2a 20 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c  * to the virtual
16f6d 20 72 6f 6f 74 20 70 61 67 65 20 69 6e 73 74 65   root page inste
16f6e 61 64 20 6f 66 20 74 68 65 20 61 63 74 75 61 6c  ad of the actual
16f6f 20 72 6f 6f 74 20 70 61 67 65 2e 20 41 20 74 61   root page. A ta
16f70 62 6c 65 20 68 61 73 20 61 0a 2a 2a 20 76 69 72  ble has a.** vir
16f71 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 20 77  tual root page w
16f72 68 65 6e 20 74 68 65 20 61 63 74 75 61 6c 20 72  hen the actual r
16f73 6f 6f 74 20 70 61 67 65 20 63 6f 6e 74 61 69 6e  oot page contain
16f74 73 20 6e 6f 20 63 65 6c 6c 73 20 61 6e 64 20 61  s no cells and a
16f75 20 0a 2a 2a 20 73 69 6e 67 6c 65 20 63 68 69 6c   .** single chil
16f76 64 20 70 61 67 65 2e 20 54 68 69 73 20 63 61 6e  d page. This can
16f77 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 77 69 74   only happen wit
16f78 68 20 74 68 65 20 74 61 62 6c 65 20 72 6f 6f 74  h the table root
16f79 65 64 20 61 74 20 70 61 67 65 20 31 2e 0a 2a 2a  ed at page 1..**
16f7a 0a 2a 2a 20 49 66 20 74 68 65 20 62 2d 74 72 65  .** If the b-tre
16f7b 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20 65  e structure is e
16f7c 6d 70 74 79 2c 20 74 68 65 20 63 75 72 73 6f 72  mpty, the cursor
16f7d 20 73 74 61 74 65 20 69 73 20 73 65 74 20 74 6f   state is set to
16f7e 20 0a 2a 2a 20 43 55 52 53 4f 52 5f 49 4e 56 41   .** CURSOR_INVA
16f7f 4c 49 44 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  LID. Otherwise, 
16f80 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 73 65  the cursor is se
16f81 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68  t to point to th
16f82 65 20 66 69 72 73 74 0a 2a 2a 20 63 65 6c 6c 20  e first.** cell 
16f83 6c 6f 63 61 74 65 64 20 6f 6e 20 74 68 65 20 72  located on the r
16f84 6f 6f 74 20 28 6f 72 20 76 69 72 74 75 61 6c 20  oot (or virtual 
16f85 72 6f 6f 74 29 20 70 61 67 65 20 61 6e 64 20 74  root) page and t
16f86 68 65 20 63 75 72 73 6f 72 20 73 74 61 74 65 0a  he cursor state.
16f87 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 43 55 52  ** is set to CUR
16f88 53 4f 52 5f 56 41 4c 49 44 2e 0a 2a 2a 0a 2a 2a  SOR_VALID..**.**
16f89 20 49 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f   If this functio
16f8a 6e 20 72 65 74 75 72 6e 73 20 73 75 63 63 65 73  n returns succes
16f8b 73 66 75 6c 6c 79 2c 20 69 74 20 6d 61 79 20 62  sfully, it may b
16f8c 65 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74  e assumed that t
16f8d 68 65 0a 2a 2a 20 70 61 67 65 2d 68 65 61 64 65  he.** page-heade
16f8e 72 20 66 6c 61 67 73 20 69 6e 64 69 63 61 74 65  r flags indicate
16f8f 20 74 68 61 74 20 74 68 65 20 5b 76 69 72 74 75   that the [virtu
16f90 61 6c 5d 20 72 6f 6f 74 2d 70 61 67 65 20 69 73  al] root-page is
16f91 20 74 68 65 20 65 78 70 65 63 74 65 64 20 0a 2a   the expected .*
16f92 2a 20 6b 69 6e 64 20 6f 66 20 62 2d 74 72 65 65  * kind of b-tree
16f93 20 70 61 67 65 20 28 69 2e 65 2e 20 69 66 20 77   page (i.e. if w
16f94 68 65 6e 20 6f 70 65 6e 69 6e 67 20 74 68 65 20  hen opening the 
16f95 63 75 72 73 6f 72 20 74 68 65 20 63 61 6c 6c 65  cursor the calle
16f96 72 20 64 69 64 20 6e 6f 74 0a 2a 2a 20 73 70 65  r did not.** spe
16f97 63 69 66 79 20 61 20 4b 65 79 49 6e 66 6f 20 73  cify a KeyInfo s
16f98 74 72 75 63 74 75 72 65 20 74 68 65 20 66 6c 61  tructure the fla
16f99 67 73 20 62 79 74 65 20 69 73 20 73 65 74 20 74  gs byte is set t
16f9a 6f 20 30 78 30 35 20 6f 72 20 30 78 30 44 2c 0a  o 0x05 or 0x0D,.
16f9b 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 61 20  ** indicating a 
16f9c 74 61 62 6c 65 20 62 2d 74 72 65 65 2c 20 6f 72  table b-tree, or
16f9d 20 69 66 20 74 68 65 20 63 61 6c 6c 65 72 20 64   if the caller d
16f9e 69 64 20 73 70 65 63 69 66 79 20 61 20 4b 65 79  id specify a Key
16f9f 49 6e 66 6f 20 0a 2a 2a 20 73 74 72 75 63 74 75  Info .** structu
16fa0 72 65 20 74 68 65 20 66 6c 61 67 73 20 62 79 74  re the flags byt
16fa1 65 20 69 73 20 73 65 74 20 74 6f 20 30 78 30 32  e is set to 0x02
16fa2 20 6f 72 20 30 78 30 41 2c 20 69 6e 64 69 63 61   or 0x0A, indica
16fa3 74 69 6e 67 20 61 6e 20 69 6e 64 65 78 0a 2a 2a  ting an index.**
16fa4 20 62 2d 74 72 65 65 29 2e 0a 2a 2f 0a 73 74 61   b-tree)..*/.sta
16fa5 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f 52 6f  tic int moveToRo
16fa6 6f 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  ot(BtCursor *pCu
16fa7 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  r){.  MemPage *p
16fa8 52 6f 6f 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d  Root;.  int rc =
16fa9 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 42 74   SQLITE_OK;.  Bt
16faa 72 65 65 20 2a 70 20 3d 20 70 43 75 72 2d 3e 70  ree *p = pCur->p
16fab 42 74 72 65 65 3b 0a 20 20 42 74 53 68 61 72 65  Btree;.  BtShare
16fac 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
16fad 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73  ..  assert( curs
16fae 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75  orHoldsMutex(pCu
16faf 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  r) );.  assert( 
16fb0 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 3c  CURSOR_INVALID <
16fb1 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53   CURSOR_REQUIRES
16fb2 45 45 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28  EEK );.  assert(
16fb3 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 20 20   CURSOR_VALID   
16fb4 3c 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45  < CURSOR_REQUIRE
16fb5 53 45 45 4b 20 29 3b 0a 20 20 61 73 73 65 72 74  SEEK );.  assert
16fb6 28 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 20  ( CURSOR_FAULT  
16fb7 20 3e 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52   > CURSOR_REQUIR
16fb8 45 53 45 45 4b 20 29 3b 0a 20 20 69 66 28 20 70  ESEEK );.  if( p
16fb9 43 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52  Cur->eState>=CUR
16fba 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20  SOR_REQUIRESEEK 
16fbb 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d  ){.    if( pCur-
16fbc 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
16fbd 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 20 20 61  FAULT ){.      a
16fbe 73 73 65 72 74 28 20 70 43 75 72 2d 3e 73 6b 69  ssert( pCur->ski
16fbf 70 4e 65 78 74 21 3d 53 51 4c 49 54 45 5f 4f 4b  pNext!=SQLITE_OK
16fc0 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   );.      return
16fc1 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b   pCur->skipNext;
16fc2 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
16fc3 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 73  e3BtreeClearCurs
16fc4 6f 72 28 70 43 75 72 29 3b 0a 20 20 7d 0a 0a 20  or(pCur);.  }.. 
16fc5 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65   if( pCur->iPage
16fc6 3e 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  >=0 ){.    int i
16fc7 3b 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69  ;.    for(i=1; i
16fc8 3c 3d 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69  <=pCur->iPage; i
16fc9 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61  ++){.      relea
16fca 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50  sePage(pCur->apP
16fcb 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20  age[i]);.    }. 
16fcc 20 20 20 70 43 75 72 2d 3e 69 50 61 67 65 20 3d     pCur->iPage =
16fcd 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
16fce 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e 69 74   rc = getAndInit
16fcf 50 61 67 65 28 70 42 74 2c 20 70 43 75 72 2d 3e  Page(pBt, pCur->
16fd0 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 43 75 72 2d  pgnoRoot, &pCur-
16fd1 3e 61 70 50 61 67 65 5b 30 5d 29 3b 0a 20 20 20  >apPage[0]);.   
16fd2 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
16fd3 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72  OK ){.      pCur
16fd4 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f  ->eState = CURSO
16fd5 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 20  R_INVALID;.     
16fd6 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
16fd7 7d 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67  }.    pCur->iPag
16fd8 65 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 49  e = 0;..    /* I
16fd9 66 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f  f pCur->pKeyInfo
16fda 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68   is not NULL, th
16fdb 65 6e 20 74 68 65 20 63 61 6c 6c 65 72 20 74 68  en the caller th
16fdc 61 74 20 6f 70 65 6e 65 64 20 74 68 69 73 20 63  at opened this c
16fdd 75 72 73 6f 72 0a 20 20 20 20 2a 2a 20 65 78 70  ursor.    ** exp
16fde 65 63 74 65 64 20 74 6f 20 6f 70 65 6e 20 69 74  ected to open it
16fdf 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20 62 2d 74   on an index b-t
16fe0 72 65 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ree. Otherwise, 
16fe1 69 66 20 70 4b 65 79 49 6e 66 6f 20 69 73 0a 20  if pKeyInfo is. 
16fe2 20 20 20 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 20     ** NULL, the 
16fe3 63 61 6c 6c 65 72 20 65 78 70 65 63 74 73 20 61  caller expects a
16fe4 20 74 61 62 6c 65 20 62 2d 74 72 65 65 2e 20 49   table b-tree. I
16fe5 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 74 68  f this is not th
16fe6 65 20 63 61 73 65 2c 0a 20 20 20 20 2a 2a 20 72  e case,.    ** r
16fe7 65 74 75 72 6e 20 61 6e 20 53 51 4c 49 54 45 5f  eturn an SQLITE_
16fe8 43 4f 52 52 55 50 54 20 65 72 72 6f 72 2e 20 20  CORRUPT error.  
16fe9 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  */.    assert( p
16fea 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e  Cur->apPage[0]->
16feb 69 6e 74 4b 65 79 3d 3d 31 20 7c 7c 20 70 43 75  intKey==1 || pCu
16fec 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e  r->apPage[0]->in
16fed 74 4b 65 79 3d 3d 30 20 29 3b 0a 20 20 20 20 69  tKey==0 );.    i
16fee 66 28 20 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e  f( (pCur->pKeyIn
16fef 66 6f 3d 3d 30 29 21 3d 70 43 75 72 2d 3e 61 70  fo==0)!=pCur->ap
16ff0 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20  Page[0]->intKey 
16ff1 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
16ff2 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
16ff3 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  KPT;.    }.  }..
16ff4 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74    /* Assert that
16ff5 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 69   the root page i
16ff6 73 20 6f 66 20 74 68 65 20 63 6f 72 72 65 63 74  s of the correct
16ff7 20 74 79 70 65 2e 20 54 68 69 73 20 6d 75 73 74   type. This must
16ff8 20 62 65 20 74 68 65 0a 20 20 2a 2a 20 63 61 73   be the.  ** cas
16ff9 65 20 61 73 20 74 68 65 20 63 61 6c 6c 20 74 6f  e as the call to
16ffa 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74   this function t
16ffb 68 61 74 20 6c 6f 61 64 65 64 20 74 68 65 20 72  hat loaded the r
16ffc 6f 6f 74 2d 70 61 67 65 20 28 65 69 74 68 65 72  oot-page (either
16ffd 0a 20 20 2a 2a 20 74 68 69 73 20 63 61 6c 6c 20  .  ** this call 
16ffe 6f 72 20 61 20 70 72 65 76 69 6f 75 73 20 69 6e  or a previous in
16fff 76 6f 63 61 74 69 6f 6e 29 20 77 6f 75 6c 64 20  vocation) would 
17000 68 61 76 65 20 64 65 74 65 63 74 65 64 20 63 6f  have detected co
17001 72 72 75 70 74 69 6f 6e 20 0a 20 20 2a 2a 20 69  rruption .  ** i
17002 66 20 74 68 65 20 61 73 73 75 6d 70 74 69 6f 6e  f the assumption
17003 20 77 65 72 65 20 6e 6f 74 20 74 72 75 65 2c 20   were not true, 
17004 61 6e 64 20 69 74 20 69 73 20 6e 6f 74 20 70 6f  and it is not po
17005 73 73 69 62 6c 65 20 66 6f 72 20 74 68 65 20 66  ssible for the f
17006 6c 61 67 73 20 0a 20 20 2a 2a 20 62 79 74 65 20  lags .  ** byte 
17007 74 6f 20 68 61 76 65 20 62 65 65 6e 20 6d 6f 64  to have been mod
17008 69 66 69 65 64 20 77 68 69 6c 65 20 74 68 69 73  ified while this
17009 20 63 75 72 73 6f 72 20 69 73 20 68 6f 6c 64 69   cursor is holdi
1700a 6e 67 20 61 20 72 65 66 65 72 65 6e 63 65 0a 20  ng a reference. 
1700b 20 2a 2a 20 74 6f 20 74 68 65 20 70 61 67 65 2e   ** to the page.
1700c 20 20 2a 2f 0a 20 20 70 52 6f 6f 74 20 3d 20 70    */.  pRoot = p
1700d 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 3b 0a  Cur->apPage[0];.
1700e 20 20 61 73 73 65 72 74 28 20 70 52 6f 6f 74 2d    assert( pRoot-
1700f 3e 70 67 6e 6f 3d 3d 70 43 75 72 2d 3e 70 67 6e  >pgno==pCur->pgn
17010 6f 52 6f 6f 74 20 29 3b 0a 20 20 61 73 73 65 72  oRoot );.  asser
17011 74 28 20 70 52 6f 6f 74 2d 3e 69 73 49 6e 69 74  t( pRoot->isInit
17012 20 26 26 20 28 70 43 75 72 2d 3e 70 4b 65 79 49   && (pCur->pKeyI
17013 6e 66 6f 3d 3d 30 29 3d 3d 70 52 6f 6f 74 2d 3e  nfo==0)==pRoot->
17014 69 6e 74 4b 65 79 20 29 3b 0a 0a 20 20 70 43 75  intKey );..  pCu
17015 72 2d 3e 61 69 49 64 78 5b 30 5d 20 3d 20 30 3b  r->aiIdx[0] = 0;
17016 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53  .  pCur->info.nS
17017 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d  ize = 0;.  pCur-
17018 3e 61 74 4c 61 73 74 20 3d 20 30 3b 0a 20 20 70  >atLast = 0;.  p
17019 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d  Cur->validNKey =
1701a 20 30 3b 0a 0a 20 20 69 66 28 20 70 52 6f 6f 74   0;..  if( pRoot
1701b 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 26 26 20 21 70  ->nCell==0 && !p
1701c 52 6f 6f 74 2d 3e 6c 65 61 66 20 29 7b 0a 20 20  Root->leaf ){.  
1701d 20 20 50 67 6e 6f 20 73 75 62 70 61 67 65 3b 0a    Pgno subpage;.
1701e 20 20 20 20 69 66 28 20 70 52 6f 6f 74 2d 3e 70      if( pRoot->p
1701f 67 6e 6f 21 3d 31 20 29 20 72 65 74 75 72 6e 20  gno!=1 ) return 
17020 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
17021 4b 50 54 3b 0a 20 20 20 20 73 75 62 70 61 67 65  KPT;.    subpage
17022 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 52 6f   = get4byte(&pRo
17023 6f 74 2d 3e 61 44 61 74 61 5b 70 52 6f 6f 74 2d  ot->aData[pRoot-
17024 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a  >hdrOffset+8]);.
17025 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65      pCur->eState
17026 20 3d 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3b   = CURSOR_VALID;
17027 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f  .    rc = moveTo
17028 43 68 69 6c 64 28 70 43 75 72 2c 20 73 75 62 70  Child(pCur, subp
17029 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  age);.  }else{. 
1702a 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20     pCur->eState 
1702b 3d 20 28 28 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c  = ((pRoot->nCell
1702c 3e 30 29 3f 43 55 52 53 4f 52 5f 56 41 4c 49 44  >0)?CURSOR_VALID
1702d 3a 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 29  :CURSOR_INVALID)
1702e 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
1702f 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65  c;.}../*.** Move
17030 20 74 68 65 20 63 75 72 73 6f 72 20 64 6f 77 6e   the cursor down
17031 20 74 6f 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73   to the left-mos
17032 74 20 6c 65 61 66 20 65 6e 74 72 79 20 62 65 6e  t leaf entry ben
17033 65 61 74 68 20 74 68 65 0a 2a 2a 20 65 6e 74 72  eath the.** entr
17034 79 20 74 6f 20 77 68 69 63 68 20 69 74 20 69 73  y to which it is
17035 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74   currently point
17036 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c  ing..**.** The l
17037 65 66 74 2d 6d 6f 73 74 20 6c 65 61 66 20 69 73  eft-most leaf is
17038 20 74 68 65 20 6f 6e 65 20 77 69 74 68 20 74 68   the one with th
17039 65 20 73 6d 61 6c 6c 65 73 74 20 6b 65 79 20 2d  e smallest key -
1703a 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 69 6e   the first.** in
1703b 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72   ascending order
1703c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
1703d 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 42  moveToLeftmost(B
1703e 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a  tCursor *pCur){.
1703f 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69    Pgno pgno;.  i
17040 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
17041 4b 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  K;.  MemPage *pP
17042 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  age;..  assert( 
17043 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
17044 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65  (pCur) );.  asse
17045 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65  rt( pCur->eState
17046 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29  ==CURSOR_VALID )
17047 3b 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53  ;.  while( rc==S
17048 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28 70 50  QLITE_OK && !(pP
17049 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61  age = pCur->apPa
1704a 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29  ge[pCur->iPage])
1704b 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 61 73  ->leaf ){.    as
1704c 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64  sert( pCur->aiId
1704d 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70  x[pCur->iPage]<p
1704e 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20  Page->nCell );. 
1704f 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79     pgno = get4by
17050 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 67  te(findCell(pPag
17051 65 2c 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70  e, pCur->aiIdx[p
17052 43 75 72 2d 3e 69 50 61 67 65 5d 29 29 3b 0a 20  Cur->iPage]));. 
17053 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68     rc = moveToCh
17054 69 6c 64 28 70 43 75 72 2c 20 70 67 6e 6f 29 3b  ild(pCur, pgno);
17055 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
17056 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20  ;.}../*.** Move 
17057 74 68 65 20 63 75 72 73 6f 72 20 64 6f 77 6e 20  the cursor down 
17058 74 6f 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73  to the right-mos
17059 74 20 6c 65 61 66 20 65 6e 74 72 79 20 62 65 6e  t leaf entry ben
1705a 65 61 74 68 20 74 68 65 0a 2a 2a 20 70 61 67 65  eath the.** page
1705b 20 74 6f 20 77 68 69 63 68 20 69 74 20 69 73 20   to which it is 
1705c 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69  currently pointi
1705d 6e 67 2e 20 20 4e 6f 74 69 63 65 20 74 68 65 20  ng.  Notice the 
1705e 64 69 66 66 65 72 65 6e 63 65 0a 2a 2a 20 62 65  difference.** be
1705f 74 77 65 65 6e 20 6d 6f 76 65 54 6f 4c 65 66 74  tween moveToLeft
17060 6d 6f 73 74 28 29 20 61 6e 64 20 6d 6f 76 65 54  most() and moveT
17061 6f 52 69 67 68 74 6d 6f 73 74 28 29 2e 20 20 6d  oRightmost().  m
17062 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 29 0a  oveToLeftmost().
17063 2a 2a 20 66 69 6e 64 73 20 74 68 65 20 6c 65 66  ** finds the lef
17064 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 62 65 6e  t-most entry ben
17065 65 61 74 68 20 74 68 65 20 2a 65 6e 74 72 79 2a  eath the *entry*
17066 20 77 68 65 72 65 61 73 20 6d 6f 76 65 54 6f 52   whereas moveToR
17067 69 67 68 74 6d 6f 73 74 28 29 0a 2a 2a 20 66 69  ightmost().** fi
17068 6e 64 73 20 74 68 65 20 72 69 67 68 74 2d 6d 6f  nds the right-mo
17069 73 74 20 65 6e 74 72 79 20 62 65 6e 65 61 74 68  st entry beneath
1706a 20 74 68 65 20 2a 70 61 67 65 2a 2e 0a 2a 2a 0a   the *page*..**.
1706b 2a 2a 20 54 68 65 20 72 69 67 68 74 2d 6d 6f 73  ** The right-mos
1706c 74 20 65 6e 74 72 79 20 69 73 20 74 68 65 20 6f  t entry is the o
1706d 6e 65 20 77 69 74 68 20 74 68 65 20 6c 61 72 67  ne with the larg
1706e 65 73 74 20 6b 65 79 20 2d 20 74 68 65 20 6c 61  est key - the la
1706f 73 74 0a 2a 2a 20 6b 65 79 20 69 6e 20 61 73 63  st.** key in asc
17070 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f  ending order..*/
17071 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65  .static int move
17072 54 6f 52 69 67 68 74 6d 6f 73 74 28 42 74 43 75  ToRightmost(BtCu
17073 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 50  rsor *pCur){.  P
17074 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69 6e 74 20  gno pgno;.  int 
17075 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
17076 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
17077 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28   = 0;..  assert(
17078 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
17079 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73  x(pCur) );.  ass
1707a 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74  ert( pCur->eStat
1707b 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20  e==CURSOR_VALID 
1707c 29 3b 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d  );.  while( rc==
1707d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28 70  SQLITE_OK && !(p
1707e 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50  Page = pCur->apP
1707f 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
17080 29 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 70  )->leaf ){.    p
17081 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26  gno = get4byte(&
17082 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61  pPage->aData[pPa
17083 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d  ge->hdrOffset+8]
17084 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49  );.    pCur->aiI
17085 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20  dx[pCur->iPage] 
17086 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a  = pPage->nCell;.
17087 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43      rc = moveToC
17088 68 69 6c 64 28 70 43 75 72 2c 20 70 67 6e 6f 29  hild(pCur, pgno)
17089 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d  ;.  }.  if( rc==
1708a 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1708b 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75   pCur->aiIdx[pCu
1708c 72 2d 3e 69 50 61 67 65 5d 20 3d 20 70 50 61 67  r->iPage] = pPag
1708d 65 2d 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 20 20 20  e->nCell-1;.    
1708e 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65  pCur->info.nSize
1708f 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e   = 0;.    pCur->
17090 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20  validNKey = 0;. 
17091 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
17092 7d 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 20 63  }../* Move the c
17093 75 72 73 6f 72 20 74 6f 20 74 68 65 20 66 69 72  ursor to the fir
17094 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  st entry in the 
17095 74 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 53  table.  Return S
17096 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73  QLITE_OK.** on s
17097 75 63 63 65 73 73 2e 20 20 53 65 74 20 2a 70 52  uccess.  Set *pR
17098 65 73 20 74 6f 20 30 20 69 66 20 74 68 65 20 63  es to 0 if the c
17099 75 72 73 6f 72 20 61 63 74 75 61 6c 6c 79 20 70  ursor actually p
1709a 6f 69 6e 74 73 20 74 6f 20 73 6f 6d 65 74 68 69  oints to somethi
1709b 6e 67 0a 2a 2a 20 6f 72 20 73 65 74 20 2a 70 52  ng.** or set *pR
1709c 65 73 20 74 6f 20 31 20 69 66 20 74 68 65 20 74  es to 1 if the t
1709d 61 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a 2a  able is empty..*
1709e 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1709f 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
170a0 65 46 69 72 73 74 28 42 74 43 75 72 73 6f 72 20  eFirst(BtCursor 
170a1 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 73  *pCur, int *pRes
170a2 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20  ){.  int rc;..  
170a3 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f  assert( cursorHo
170a4 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29  ldsMutex(pCur) )
170a5 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
170a6 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
170a7 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d  Cur->pBtree->db-
170a8 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20  >mutex) );.  rc 
170a9 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75  = moveToRoot(pCu
170aa 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  r);.  if( rc==SQ
170ab 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69  LITE_OK ){.    i
170ac 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  f( pCur->eState=
170ad 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20  =CURSOR_INVALID 
170ae 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
170af 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43   pCur->apPage[pC
170b0 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c  ur->iPage]->nCel
170b1 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 2a 70  l==0 );.      *p
170b2 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 72  Res = 1;.      r
170b3 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
170b4 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
170b5 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70  assert( pCur->ap
170b6 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
170b7 5d 2d 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20 20  ]->nCell>0 );.  
170b8 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20      *pRes = 0;. 
170b9 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f       rc = moveTo
170ba 4c 65 66 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a  Leftmost(pCur);.
170bb 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
170bc 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76  rn rc;.}../* Mov
170bd 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20  e the cursor to 
170be 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69  the last entry i
170bf 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 52 65  n the table.  Re
170c0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a  turn SQLITE_OK.*
170c1 2a 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 53  * on success.  S
170c2 65 74 20 2a 70 52 65 73 20 74 6f 20 30 20 69 66  et *pRes to 0 if
170c3 20 74 68 65 20 63 75 72 73 6f 72 20 61 63 74 75   the cursor actu
170c4 61 6c 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 73  ally points to s
170c5 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 6f 72 20 73  omething.** or s
170c6 65 74 20 2a 70 52 65 73 20 74 6f 20 31 20 69 66  et *pRes to 1 if
170c7 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d   the table is em
170c8 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  pty..*/.SQLITE_P
170c9 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
170ca 65 33 42 74 72 65 65 4c 61 73 74 28 42 74 43 75  e3BtreeLast(BtCu
170cb 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20  rsor *pCur, int 
170cc 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63  *pRes){.  int rc
170cd 3b 0a 20 0a 20 20 61 73 73 65 72 74 28 20 63 75  ;. .  assert( cu
170ce 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
170cf 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74  Cur) );.  assert
170d0 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
170d1 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 65  held(pCur->pBtre
170d2 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  e->db->mutex) );
170d3 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 75  ..  /* If the cu
170d4 72 73 6f 72 20 61 6c 72 65 61 64 79 20 70 6f 69  rsor already poi
170d5 6e 74 73 20 74 6f 20 74 68 65 20 6c 61 73 74 20  nts to the last 
170d6 65 6e 74 72 79 2c 20 74 68 69 73 20 69 73 20 61  entry, this is a
170d7 20 6e 6f 2d 6f 70 2e 20 2a 2f 0a 20 20 69 66 28   no-op. */.  if(
170d8 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3d 3d 70   CURSOR_VALID==p
170d9 43 75 72 2d 3e 65 53 74 61 74 65 20 26 26 20 70  Cur->eState && p
170da 43 75 72 2d 3e 61 74 4c 61 73 74 20 29 7b 0a 23  Cur->atLast ){.#
170db 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
170dc 55 47 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 62  UG.    /* This b
170dd 6c 6f 63 6b 20 73 65 72 76 65 73 20 74 6f 20 61  lock serves to a
170de 73 73 65 72 74 28 29 20 74 68 61 74 20 74 68 65  ssert() that the
170df 20 63 75 72 73 6f 72 20 72 65 61 6c 6c 79 20 64   cursor really d
170e0 6f 65 73 20 70 6f 69 6e 74 20 0a 20 20 20 20 2a  oes point .    *
170e1 2a 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e  * to the last en
170e2 74 72 79 20 69 6e 20 74 68 65 20 62 2d 74 72 65  try in the b-tre
170e3 65 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 69  e. */.    int ii
170e4 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20  ;.    for(ii=0; 
170e5 69 69 3c 70 43 75 72 2d 3e 69 50 61 67 65 3b 20  ii<pCur->iPage; 
170e6 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73  ii++){.      ass
170e7 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78  ert( pCur->aiIdx
170e8 5b 69 69 5d 3d 3d 70 43 75 72 2d 3e 61 70 50 61  [ii]==pCur->apPa
170e9 67 65 5b 69 69 5d 2d 3e 6e 43 65 6c 6c 20 29 3b  ge[ii]->nCell );
170ea 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
170eb 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70  t( pCur->aiIdx[p
170ec 43 75 72 2d 3e 69 50 61 67 65 5d 3d 3d 70 43 75  Cur->iPage]==pCu
170ed 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
170ee 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 2d 31 20  iPage]->nCell-1 
170ef 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
170f0 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
170f1 2d 3e 69 50 61 67 65 5d 2d 3e 6c 65 61 66 20 29  ->iPage]->leaf )
170f2 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 65 74  ;.#endif.    ret
170f3 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
170f4 20 7d 0a 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54   }..  rc = moveT
170f5 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 69  oRoot(pCur);.  i
170f6 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
170f7 20 29 7b 0a 20 20 20 20 69 66 28 20 43 55 52 53   ){.    if( CURS
170f8 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 75 72  OR_INVALID==pCur
170f9 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20 20 20 20  ->eState ){.    
170fa 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
170fb 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
170fc 67 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b  ge]->nCell==0 );
170fd 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31  .      *pRes = 1
170fe 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
170ff 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d     assert( pCur-
17100 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
17101 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 20 20 2a  VALID );.      *
17102 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20  pRes = 0;.      
17103 72 63 20 3d 20 6d 6f 76 65 54 6f 52 69 67 68 74  rc = moveToRight
17104 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20  most(pCur);.    
17105 20 20 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 3d    pCur->atLast =
17106 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f   rc==SQLITE_OK ?
17107 31 3a 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  1:0;.    }.  }. 
17108 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
17109 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f  * Move the curso
1710a 72 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69  r so that it poi
1710b 6e 74 73 20 74 6f 20 61 6e 20 65 6e 74 72 79 20  nts to an entry 
1710c 6e 65 61 72 20 74 68 65 20 6b 65 79 20 0a 2a 2a  near the key .**
1710d 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 49   specified by pI
1710e 64 78 4b 65 79 20 6f 72 20 69 6e 74 4b 65 79 2e  dxKey or intKey.
1710f 20 20 20 52 65 74 75 72 6e 20 61 20 73 75 63 63     Return a succ
17110 65 73 73 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20  ess code..**.** 
17111 46 6f 72 20 49 4e 54 4b 45 59 20 74 61 62 6c 65  For INTKEY table
17112 73 2c 20 74 68 65 20 69 6e 74 4b 65 79 20 70 61  s, the intKey pa
17113 72 61 6d 65 74 65 72 20 69 73 20 75 73 65 64 2e  rameter is used.
17114 20 20 70 49 64 78 4b 65 79 20 0a 2a 2a 20 6d 75    pIdxKey .** mu
17115 73 74 20 62 65 20 4e 55 4c 4c 2e 20 20 46 6f 72  st be NULL.  For
17116 20 69 6e 64 65 78 20 74 61 62 6c 65 73 2c 20 70   index tables, p
17117 49 64 78 4b 65 79 20 69 73 20 75 73 65 64 20 61  IdxKey is used a
17118 6e 64 20 69 6e 74 4b 65 79 0a 2a 2a 20 69 73 20  nd intKey.** is 
17119 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  ignored..**.** I
1711a 66 20 61 6e 20 65 78 61 63 74 20 6d 61 74 63 68  f an exact match
1711b 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2c 20 74   is not found, t
1711c 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69  hen the cursor i
1711d 73 20 61 6c 77 61 79 73 0a 2a 2a 20 6c 65 66 74  s always.** left
1711e 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 6c   pointing at a l
1711f 65 61 66 20 70 61 67 65 20 77 68 69 63 68 20 77  eaf page which w
17120 6f 75 6c 64 20 68 6f 6c 64 20 74 68 65 20 65 6e  ould hold the en
17121 74 72 79 20 69 66 20 69 74 0a 2a 2a 20 77 65 72  try if it.** wer
17122 65 20 70 72 65 73 65 6e 74 2e 20 20 54 68 65 20  e present.  The 
17123 63 75 72 73 6f 72 20 6d 69 67 68 74 20 70 6f 69  cursor might poi
17124 6e 74 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 74  nt to an entry t
17125 68 61 74 20 63 6f 6d 65 73 0a 2a 2a 20 62 65 66  hat comes.** bef
17126 6f 72 65 20 6f 72 20 61 66 74 65 72 20 74 68 65  ore or after the
17127 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 69   key..**.** An i
17128 6e 74 65 67 65 72 20 69 73 20 77 72 69 74 74 65  nteger is writte
17129 6e 20 69 6e 74 6f 20 2a 70 52 65 73 20 77 68 69  n into *pRes whi
1712a 63 68 20 69 73 20 74 68 65 20 72 65 73 75 6c 74  ch is the result
1712b 20 6f 66 0a 2a 2a 20 63 6f 6d 70 61 72 69 6e 67   of.** comparing
1712c 20 74 68 65 20 6b 65 79 20 77 69 74 68 20 74 68   the key with th
1712d 65 20 65 6e 74 72 79 20 74 6f 20 77 68 69 63 68  e entry to which
1712e 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 0a   the cursor is .
1712f 2a 2a 20 70 6f 69 6e 74 69 6e 67 2e 20 20 54 68  ** pointing.  Th
17130 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 74 68 65  e meaning of the
17131 20 69 6e 74 65 67 65 72 20 77 72 69 74 74 65 6e   integer written
17132 20 69 6e 74 6f 0a 2a 2a 20 2a 70 52 65 73 20 69   into.** *pRes i
17133 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  s as follows:.**
17134 0a 2a 2a 20 20 20 20 20 2a 70 52 65 73 3c 30 20  .**     *pRes<0 
17135 20 20 20 20 20 54 68 65 20 63 75 72 73 6f 72 20       The cursor 
17136 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67  is left pointing
17137 20 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61   at an entry tha
17138 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
17139 20 20 20 20 20 20 69 73 20 73 6d 61 6c 6c 65 72        is smaller
1713a 20 74 68 61 6e 20 69 6e 74 4b 65 79 2f 70 49 64   than intKey/pId
1713b 78 4b 65 79 20 6f 72 20 69 66 20 74 68 65 20 74  xKey or if the t
1713c 61 62 6c 65 20 69 73 20 65 6d 70 74 79 0a 2a 2a  able is empty.**
1713d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1713e 20 20 61 6e 64 20 74 68 65 20 63 75 72 73 6f 72    and the cursor
1713f 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 6c 65   is therefore le
17140 66 74 20 70 6f 69 6e 74 20 74 6f 20 6e 6f 74 68  ft point to noth
17141 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a  ing..**.**     *
17142 70 52 65 73 3d 3d 30 20 20 20 20 20 54 68 65 20  pRes==0     The 
17143 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20 70  cursor is left p
17144 6f 69 6e 74 69 6e 67 20 61 74 20 61 6e 20 65 6e  ointing at an en
17145 74 72 79 20 74 68 61 74 0a 2a 2a 20 20 20 20 20  try that.**     
17146 20 20 20 20 20 20 20 20 20 20 20 20 20 65 78 61               exa
17147 63 74 6c 79 20 6d 61 74 63 68 65 73 20 69 6e 74  ctly matches int
17148 4b 65 79 2f 70 49 64 78 4b 65 79 2e 0a 2a 2a 0a  Key/pIdxKey..**.
17149 2a 2a 20 20 20 20 20 2a 70 52 65 73 3e 30 20 20  **     *pRes>0  
1714a 20 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 69      The cursor i
1714b 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20  s left pointing 
1714c 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74  at an entry that
1714d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
1714e 20 20 20 20 20 69 73 20 6c 61 72 67 65 72 20 74       is larger t
1714f 68 61 6e 20 69 6e 74 4b 65 79 2f 70 49 64 78 4b  han intKey/pIdxK
17150 65 79 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45  ey..**.*/.SQLITE
17151 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
17152 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55  ite3BtreeMovetoU
17153 6e 70 61 63 6b 65 64 28 0a 20 20 42 74 43 75 72  npacked(.  BtCur
17154 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20  sor *pCur,      
17155 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f      /* The curso
17156 72 20 74 6f 20 62 65 20 6d 6f 76 65 64 20 2a 2f  r to be moved */
17157 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  .  UnpackedRecor
17158 64 20 2a 70 49 64 78 4b 65 79 2c 20 2f 2a 20 55  d *pIdxKey, /* U
17159 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65  npacked index ke
1715a 79 20 2a 2f 0a 20 20 69 36 34 20 69 6e 74 4b 65  y */.  i64 intKe
1715b 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y,              
1715c 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 6b 65 79  /* The table key
1715d 20 2a 2f 0a 20 20 69 6e 74 20 62 69 61 73 52 69   */.  int biasRi
1715e 67 68 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f  ght,           /
1715f 2a 20 49 66 20 74 72 75 65 2c 20 62 69 61 73 20  * If true, bias 
17160 74 68 65 20 73 65 61 72 63 68 20 74 6f 20 74 68  the search to th
17161 65 20 68 69 67 68 20 65 6e 64 20 2a 2f 0a 20 20  e high end */.  
17162 69 6e 74 20 2a 70 52 65 73 20 20 20 20 20 20 20  int *pRes       
17163 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
17164 65 20 73 65 61 72 63 68 20 72 65 73 75 6c 74 73  e search results
17165 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e   here */.){.  in
17166 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28  t rc;..  assert(
17167 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
17168 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73  x(pCur) );.  ass
17169 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1716a 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42  ex_held(pCur->pB
1716b 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  tree->db->mutex)
1716c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 52   );.  assert( pR
1716d 65 73 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  es );.  assert( 
1716e 28 70 49 64 78 4b 65 79 3d 3d 30 29 3d 3d 28 70  (pIdxKey==0)==(p
1716f 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30  Cur->pKeyInfo==0
17170 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  ) );..  /* If th
17171 65 20 63 75 72 73 6f 72 20 69 73 20 61 6c 72 65  e cursor is alre
17172 61 64 79 20 70 6f 73 69 74 69 6f 6e 65 64 20 61  ady positioned a
17173 74 20 74 68 65 20 70 6f 69 6e 74 20 77 65 20 61  t the point we a
17174 72 65 20 74 72 79 69 6e 67 0a 20 20 2a 2a 20 74  re trying.  ** t
17175 6f 20 6d 6f 76 65 20 74 6f 2c 20 74 68 65 6e 20  o move to, then 
17176 6a 75 73 74 20 72 65 74 75 72 6e 20 77 69 74 68  just return with
17177 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79 20 77 6f  out doing any wo
17178 72 6b 20 2a 2f 0a 20 20 69 66 28 20 70 43 75 72  rk */.  if( pCur
17179 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
1717a 5f 56 41 4c 49 44 20 26 26 20 70 43 75 72 2d 3e  _VALID && pCur->
1717b 76 61 6c 69 64 4e 4b 65 79 20 0a 20 20 20 26 26  validNKey .   &&
1717c 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d   pCur->apPage[0]
1717d 2d 3e 69 6e 74 4b 65 79 20 0a 20 20 29 7b 0a 20  ->intKey .  ){. 
1717e 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 6e 66     if( pCur->inf
1717f 6f 2e 6e 4b 65 79 3d 3d 69 6e 74 4b 65 79 20 29  o.nKey==intKey )
17180 7b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20  {.      *pRes = 
17181 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  0;.      return 
17182 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d  SQLITE_OK;.    }
17183 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 61  .    if( pCur->a
17184 74 4c 61 73 74 20 26 26 20 70 43 75 72 2d 3e 69  tLast && pCur->i
17185 6e 66 6f 2e 6e 4b 65 79 3c 69 6e 74 4b 65 79 20  nfo.nKey<intKey 
17186 29 7b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d  ){.      *pRes =
17187 20 2d 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72   -1;.      retur
17188 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  n SQLITE_OK;.   
17189 20 7d 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 6d   }.  }..  rc = m
1718a 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b  oveToRoot(pCur);
1718b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20  .  if( rc ){.   
1718c 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
1718d 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
1718e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
1718f 67 65 5d 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ge] );.  assert(
17190 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43   pCur->apPage[pC
17191 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 69 73 49 6e  ur->iPage]->isIn
17192 69 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  it );.  assert( 
17193 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
17194 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c  r->iPage]->nCell
17195 3e 30 20 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61  >0 || pCur->eSta
17196 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c  te==CURSOR_INVAL
17197 49 44 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72  ID );.  if( pCur
17198 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
17199 5f 49 4e 56 41 4c 49 44 20 29 7b 0a 20 20 20 20  _INVALID ){.    
1719a 2a 70 52 65 73 20 3d 20 2d 31 3b 0a 20 20 20 20  *pRes = -1;.    
1719b 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70  assert( pCur->ap
1719c 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
1719d 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20  ]->nCell==0 );. 
1719e 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1719f 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  _OK;.  }.  asser
171a0 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b  t( pCur->apPage[
171a1 30 5d 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 70 49  0]->intKey || pI
171a2 64 78 4b 65 79 20 29 3b 0a 20 20 66 6f 72 28 3b  dxKey );.  for(;
171a3 3b 29 7b 0a 20 20 20 20 69 6e 74 20 6c 77 72 2c  ;){.    int lwr,
171a4 20 75 70 72 3b 0a 20 20 20 20 50 67 6e 6f 20 63   upr;.    Pgno c
171a5 68 6c 64 50 67 3b 0a 20 20 20 20 4d 65 6d 50 61  hldPg;.    MemPa
171a6 67 65 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72  ge *pPage = pCur
171a7 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
171a8 50 61 67 65 5d 3b 0a 20 20 20 20 69 6e 74 20 63  Page];.    int c
171a9 3b 0a 0a 20 20 20 20 2f 2a 20 70 50 61 67 65 2d  ;..    /* pPage-
171aa 3e 6e 43 65 6c 6c 20 6d 75 73 74 20 62 65 20 67  >nCell must be g
171ab 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f  reater than zero
171ac 2e 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65  . If this is the
171ad 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 2a   root-page.    *
171ae 2a 20 74 68 65 20 63 75 72 73 6f 72 20 77 6f 75  * the cursor wou
171af 6c 64 20 68 61 76 65 20 62 65 65 6e 20 49 4e 56  ld have been INV
171b0 41 4c 49 44 20 61 62 6f 76 65 20 61 6e 64 20 74  ALID above and t
171b1 68 69 73 20 66 6f 72 28 3b 3b 29 20 6c 6f 6f 70  his for(;;) loop
171b2 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 72 75 6e 2e  .    ** not run.
171b3 20 49 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20   If this is not 
171b4 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 2c 20 74  the root-page, t
171b5 68 65 6e 20 74 68 65 20 6d 6f 76 65 54 6f 43 68  hen the moveToCh
171b6 69 6c 64 28 29 20 72 6f 75 74 69 6e 65 0a 20 20  ild() routine.  
171b7 20 20 2a 2a 20 77 6f 75 6c 64 20 68 61 76 65 20    ** would have 
171b8 61 6c 72 65 61 64 79 20 64 65 74 65 63 74 65 64  already detected
171b9 20 64 62 20 63 6f 72 72 75 70 74 69 6f 6e 2e 20   db corruption. 
171ba 53 69 6d 69 6c 61 72 6c 79 2c 20 70 50 61 67 65  Similarly, pPage
171bb 20 6d 75 73 74 0a 20 20 20 20 2a 2a 20 62 65 20   must.    ** be 
171bc 74 68 65 20 72 69 67 68 74 20 6b 69 6e 64 20 28  the right kind (
171bd 69 6e 64 65 78 20 6f 72 20 74 61 62 6c 65 29 20  index or table) 
171be 6f 66 20 62 2d 74 72 65 65 20 70 61 67 65 2e 20  of b-tree page. 
171bf 4f 74 68 65 72 77 69 73 65 0a 20 20 20 20 2a 2a  Otherwise.    **
171c0 20 61 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 29   a moveToChild()
171c1 20 6f 72 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 29   or moveToRoot()
171c2 20 63 61 6c 6c 20 77 6f 75 6c 64 20 68 61 76 65   call would have
171c3 20 64 65 74 65 63 74 65 64 20 63 6f 72 72 75 70   detected corrup
171c4 74 69 6f 6e 2e 20 20 2a 2f 0a 20 20 20 20 61 73  tion.  */.    as
171c5 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 65  sert( pPage->nCe
171c6 6c 6c 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65  ll>0 );.    asse
171c7 72 74 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65  rt( pPage->intKe
171c8 79 3d 3d 28 70 49 64 78 4b 65 79 3d 3d 30 29 20  y==(pIdxKey==0) 
171c9 29 3b 0a 20 20 20 20 6c 77 72 20 3d 20 30 3b 0a  );.    lwr = 0;.
171ca 20 20 20 20 75 70 72 20 3d 20 70 50 61 67 65 2d      upr = pPage-
171cb 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 20 20 20 69 66  >nCell-1;.    if
171cc 28 20 62 69 61 73 52 69 67 68 74 20 29 7b 0a 20  ( biasRight ){. 
171cd 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78       pCur->aiIdx
171ce 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20  [pCur->iPage] = 
171cf 28 75 31 36 29 75 70 72 3b 0a 20 20 20 20 7d 65  (u16)upr;.    }e
171d0 6c 73 65 7b 0a 20 20 20 20 20 20 70 43 75 72 2d  lse{.      pCur-
171d1 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
171d2 67 65 5d 20 3d 20 28 75 31 36 29 28 28 75 70 72  ge] = (u16)((upr
171d3 2b 6c 77 72 29 2f 32 29 3b 0a 20 20 20 20 7d 0a  +lwr)/2);.    }.
171d4 20 20 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20      for(;;){.   
171d5 20 20 20 69 6e 74 20 69 64 78 20 3d 20 70 43 75     int idx = pCu
171d6 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
171d7 50 61 67 65 5d 3b 20 2f 2a 20 49 6e 64 65 78 20  Page]; /* Index 
171d8 6f 66 20 63 75 72 72 65 6e 74 20 63 65 6c 6c 20  of current cell 
171d9 69 6e 20 70 50 61 67 65 20 2a 2f 0a 20 20 20 20  in pPage */.    
171da 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20 20    u8 *pCell;    
171db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
171dc 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
171dd 20 74 6f 20 63 75 72 72 65 6e 74 20 63 65 6c 6c   to current cell
171de 20 69 6e 20 70 50 61 67 65 20 2a 2f 0a 0a 20 20   in pPage */..  
171df 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e      pCur->info.n
171e0 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 20 20  Size = 0;.      
171e1 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c  pCell = findCell
171e2 28 70 50 61 67 65 2c 20 69 64 78 29 20 2b 20 70  (pPage, idx) + p
171e3 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69  Page->childPtrSi
171e4 7a 65 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50  ze;.      if( pP
171e5 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20  age->intKey ){. 
171e6 20 20 20 20 20 20 20 69 36 34 20 6e 43 65 6c 6c         i64 nCell
171e7 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 69 66 28  Key;.        if(
171e8 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20   pPage->hasData 
171e9 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75 33 32  ){.          u32
171ea 20 64 75 6d 6d 79 3b 0a 20 20 20 20 20 20 20 20   dummy;.        
171eb 20 20 70 43 65 6c 6c 20 2b 3d 20 67 65 74 56 61    pCell += getVa
171ec 72 69 6e 74 33 32 28 70 43 65 6c 6c 2c 20 64 75  rint32(pCell, du
171ed 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  mmy);.        }.
171ee 20 20 20 20 20 20 20 20 67 65 74 56 61 72 69 6e          getVarin
171ef 74 28 70 43 65 6c 6c 2c 20 28 75 36 34 2a 29 26  t(pCell, (u64*)&
171f0 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 20  nCellKey);.     
171f1 20 20 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3d     if( nCellKey=
171f2 3d 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20  =intKey ){.     
171f3 20 20 20 20 20 63 20 3d 20 30 3b 0a 20 20 20 20       c = 0;.    
171f4 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 43      }else if( nC
171f5 65 6c 6c 4b 65 79 3c 69 6e 74 4b 65 79 20 29 7b  ellKey<intKey ){
171f6 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 2d  .          c = -
171f7 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  1;.        }else
171f8 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  {.          asse
171f9 72 74 28 20 6e 43 65 6c 6c 4b 65 79 3e 69 6e 74  rt( nCellKey>int
171fa 4b 65 79 20 29 3b 0a 20 20 20 20 20 20 20 20 20  Key );.         
171fb 20 63 20 3d 20 2b 31 3b 0a 20 20 20 20 20 20 20   c = +1;.       
171fc 20 7d 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d   }.        pCur-
171fd 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 0a  >validNKey = 1;.
171fe 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 69 6e          pCur->in
171ff 66 6f 2e 6e 4b 65 79 20 3d 20 6e 43 65 6c 6c 4b  fo.nKey = nCellK
17200 65 79 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ey;.      }else{
17201 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  .        /* The 
17202 6d 61 78 69 6d 75 6d 20 73 75 70 70 6f 72 74 65  maximum supporte
17203 64 20 70 61 67 65 2d 73 69 7a 65 20 69 73 20 33  d page-size is 3
17204 32 37 36 38 20 62 79 74 65 73 2e 20 54 68 69 73  2768 bytes. This
17205 20 6d 65 61 6e 73 20 74 68 61 74 0a 20 20 20 20   means that.    
17206 20 20 20 20 2a 2a 20 74 68 65 20 6d 61 78 69 6d      ** the maxim
17207 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 63  um number of rec
17208 6f 72 64 20 62 79 74 65 73 20 73 74 6f 72 65 64  ord bytes stored
17209 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20 42 2d 54   on an index B-T
1720a 72 65 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 70  ree.        ** p
1720b 61 67 65 20 69 73 20 61 74 20 6d 6f 73 74 20 38  age is at most 8
1720c 31 39 38 20 62 79 74 65 73 2c 20 77 68 69 63 68  198 bytes, which
1720d 20 6d 61 79 20 62 65 20 73 74 6f 72 65 64 20 61   may be stored a
1720e 73 20 61 20 32 2d 62 79 74 65 0a 20 20 20 20 20  s a 2-byte.     
1720f 20 20 20 2a 2a 20 76 61 72 69 6e 74 2e 20 54 68     ** varint. Th
17210 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  is information i
17211 73 20 75 73 65 64 20 74 6f 20 61 74 74 65 6d 70  s used to attemp
17212 74 20 74 6f 20 61 76 6f 69 64 20 70 61 72 73 69  t to avoid parsi
17213 6e 67 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74  ng .        ** t
17214 68 65 20 65 6e 74 69 72 65 20 63 65 6c 6c 20 62  he entire cell b
17215 79 20 63 68 65 63 6b 69 6e 67 20 66 6f 72 20 74  y checking for t
17216 68 65 20 63 61 73 65 73 20 77 68 65 72 65 20 74  he cases where t
17217 68 65 20 72 65 63 6f 72 64 20 69 73 20 0a 20 20  he record is .  
17218 20 20 20 20 20 20 2a 2a 20 73 74 6f 72 65 64 20        ** stored 
17219 65 6e 74 69 72 65 6c 79 20 77 69 74 68 69 6e 20  entirely within 
1721a 74 68 65 20 62 2d 74 72 65 65 20 70 61 67 65 20  the b-tree page 
1721b 62 79 20 69 6e 73 70 65 63 74 69 6e 67 20 74 68  by inspecting th
1721c 65 20 66 69 72 73 74 20 0a 20 20 20 20 20 20 20  e first .       
1721d 20 2a 2a 20 32 20 62 79 74 65 73 20 6f 66 20 74   ** 2 bytes of t
1721e 68 65 20 63 65 6c 6c 2e 0a 20 20 20 20 20 20 20  he cell..       
1721f 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20   */.        int 
17220 6e 43 65 6c 6c 20 3d 20 70 43 65 6c 6c 5b 30 5d  nCell = pCell[0]
17221 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 28  ;.        if( !(
17222 6e 43 65 6c 6c 20 26 20 30 78 38 30 29 20 26 26  nCell & 0x80) &&
17223 20 6e 43 65 6c 6c 3c 3d 70 50 61 67 65 2d 3e 6d   nCell<=pPage->m
17224 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20  axLocal ){.     
17225 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61       /* This bra
17226 6e 63 68 20 72 75 6e 73 20 69 66 20 74 68 65 20  nch runs if the 
17227 72 65 63 6f 72 64 2d 73 69 7a 65 20 66 69 65 6c  record-size fiel
17228 64 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 69 73  d of the cell is
17229 20 61 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20   a.          ** 
1722a 73 69 6e 67 6c 65 20 62 79 74 65 20 76 61 72 69  single byte vari
1722b 6e 74 20 61 6e 64 20 74 68 65 20 72 65 63 6f 72  nt and the recor
1722c 64 20 66 69 74 73 20 65 6e 74 69 72 65 6c 79 20  d fits entirely 
1722d 6f 6e 20 74 68 65 20 6d 61 69 6e 0a 20 20 20 20  on the main.    
1722e 20 20 20 20 20 20 2a 2a 20 62 2d 74 72 65 65 20        ** b-tree 
1722f 70 61 67 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20  page.  */.      
17230 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56      c = sqlite3V
17231 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65  dbeRecordCompare
17232 28 6e 43 65 6c 6c 2c 20 28 76 6f 69 64 2a 29 26  (nCell, (void*)&
17233 70 43 65 6c 6c 5b 31 5d 2c 20 70 49 64 78 4b 65  pCell[1], pIdxKe
17234 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  y);.        }els
17235 65 20 69 66 28 20 21 28 70 43 65 6c 6c 5b 31 5d  e if( !(pCell[1]
17236 20 26 20 30 78 38 30 29 20 0a 20 20 20 20 20 20   & 0x80) .      
17237 20 20 20 20 26 26 20 28 6e 43 65 6c 6c 20 3d 20      && (nCell = 
17238 28 28 6e 43 65 6c 6c 26 30 78 37 66 29 3c 3c 37  ((nCell&0x7f)<<7
17239 29 20 2b 20 70 43 65 6c 6c 5b 31 5d 29 3c 3d 70  ) + pCell[1])<=p
1723a 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 0a 20  Page->maxLocal. 
1723b 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20         ){.      
1723c 20 20 20 20 2f 2a 20 54 68 65 20 72 65 63 6f 72      /* The recor
1723d 64 2d 73 69 7a 65 20 66 69 65 6c 64 20 69 73 20  d-size field is 
1723e 61 20 32 20 62 79 74 65 20 76 61 72 69 6e 74 20  a 2 byte varint 
1723f 61 6e 64 20 74 68 65 20 72 65 63 6f 72 64 20 0a  and the record .
17240 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 69 74            ** fit
17241 73 20 65 6e 74 69 72 65 6c 79 20 6f 6e 20 74 68  s entirely on th
17242 65 20 6d 61 69 6e 20 62 2d 74 72 65 65 20 70 61  e main b-tree pa
17243 67 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20  ge.  */.        
17244 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62    c = sqlite3Vdb
17245 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 6e  eRecordCompare(n
17246 43 65 6c 6c 2c 20 28 76 6f 69 64 2a 29 26 70 43  Cell, (void*)&pC
17247 65 6c 6c 5b 32 5d 2c 20 70 49 64 78 4b 65 79 29  ell[2], pIdxKey)
17248 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
17249 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  .          /* Th
1724a 65 20 72 65 63 6f 72 64 20 66 6c 6f 77 73 20 6f  e record flows o
1724b 76 65 72 20 6f 6e 74 6f 20 6f 6e 65 20 6f 72 20  ver onto one or 
1724c 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 61  more overflow pa
1724d 67 65 73 2e 20 49 6e 0a 20 20 20 20 20 20 20 20  ges. In.        
1724e 20 20 2a 2a 20 74 68 69 73 20 63 61 73 65 20 74    ** this case t
1724f 68 65 20 77 68 6f 6c 65 20 63 65 6c 6c 20 6e 65  he whole cell ne
17250 65 64 73 20 74 6f 20 62 65 20 70 61 72 73 65 64  eds to be parsed
17251 2c 20 61 20 62 75 66 66 65 72 20 61 6c 6c 6f 63  , a buffer alloc
17252 61 74 65 64 0a 20 20 20 20 20 20 20 20 20 20 2a  ated.          *
17253 2a 20 61 6e 64 20 61 63 63 65 73 73 50 61 79 6c  * and accessPayl
17254 6f 61 64 28 29 20 75 73 65 64 20 74 6f 20 72 65  oad() used to re
17255 74 72 69 65 76 65 20 74 68 65 20 72 65 63 6f 72  trieve the recor
17256 64 20 69 6e 74 6f 20 74 68 65 0a 20 20 20 20 20  d into the.     
17257 20 20 20 20 20 2a 2a 20 62 75 66 66 65 72 20 62       ** buffer b
17258 65 66 6f 72 65 20 56 64 62 65 52 65 63 6f 72 64  efore VdbeRecord
17259 43 6f 6d 70 61 72 65 28 29 20 63 61 6e 20 62 65  Compare() can be
1725a 20 63 61 6c 6c 65 64 2e 20 2a 2f 0a 20 20 20 20   called. */.    
1725b 20 20 20 20 20 20 76 6f 69 64 20 2a 70 43 65 6c        void *pCel
1725c 6c 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 20 20  lKey;.          
1725d 75 38 20 2a 20 63 6f 6e 73 74 20 70 43 65 6c 6c  u8 * const pCell
1725e 42 6f 64 79 20 3d 20 70 43 65 6c 6c 20 2d 20 70  Body = pCell - p
1725f 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69  Page->childPtrSi
17260 7a 65 3b 0a 20 20 20 20 20 20 20 20 20 20 62 74  ze;.          bt
17261 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28  reeParseCellPtr(
17262 70 50 61 67 65 2c 20 70 43 65 6c 6c 42 6f 64 79  pPage, pCellBody
17263 2c 20 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 0a  , &pCur->info);.
17264 20 20 20 20 20 20 20 20 20 20 6e 43 65 6c 6c 20            nCell 
17265 3d 20 28 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66  = (int)pCur->inf
17266 6f 2e 6e 4b 65 79 3b 0a 20 20 20 20 20 20 20 20  o.nKey;.        
17267 20 20 70 43 65 6c 6c 4b 65 79 20 3d 20 73 71 6c    pCellKey = sql
17268 69 74 65 33 4d 61 6c 6c 6f 63 28 20 6e 43 65 6c  ite3Malloc( nCel
17269 6c 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  l );.          i
1726a 66 28 20 70 43 65 6c 6c 4b 65 79 3d 3d 30 20 29  f( pCellKey==0 )
1726b 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63  {.            rc
1726c 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
1726d 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74  .            got
1726e 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b  o moveto_finish;
1726f 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
17270 20 20 20 20 20 20 20 72 63 20 3d 20 61 63 63 65         rc = acce
17271 73 73 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20  ssPayload(pCur, 
17272 30 2c 20 6e 43 65 6c 6c 2c 20 28 75 6e 73 69 67  0, nCell, (unsig
17273 6e 65 64 20 63 68 61 72 2a 29 70 43 65 6c 6c 4b  ned char*)pCellK
17274 65 79 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  ey, 0);.        
17275 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
17276 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
17277 66 72 65 65 28 70 43 65 6c 6c 4b 65 79 29 3b 0a  free(pCellKey);.
17278 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f              goto
17279 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a   moveto_finish;.
1727a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
1727b 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65        c = sqlite
1727c 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61  3VdbeRecordCompa
1727d 72 65 28 6e 43 65 6c 6c 2c 20 70 43 65 6c 6c 4b  re(nCell, pCellK
1727e 65 79 2c 20 70 49 64 78 4b 65 79 29 3b 0a 20 20  ey, pIdxKey);.  
1727f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
17280 66 72 65 65 28 70 43 65 6c 6c 4b 65 79 29 3b 0a  free(pCellKey);.
17281 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
17282 7d 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 30  }.      if( c==0
17283 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
17284 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26  pPage->intKey &&
17285 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b   !pPage->leaf ){
17286 0a 20 20 20 20 20 20 20 20 20 20 6c 77 72 20 3d  .          lwr =
17287 20 69 64 78 3b 0a 20 20 20 20 20 20 20 20 20 20   idx;.          
17288 75 70 72 20 3d 20 6c 77 72 20 2d 20 31 3b 0a 20  upr = lwr - 1;. 
17289 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
1728a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
1728b 20 20 20 20 20 20 20 20 20 2a 70 52 65 73 20 3d           *pRes =
1728c 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63   0;.          rc
1728d 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
1728e 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76          goto mov
1728f 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20  eto_finish;.    
17290 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
17291 20 20 20 20 69 66 28 20 63 3c 30 20 29 7b 0a 20      if( c<0 ){. 
17292 20 20 20 20 20 20 20 6c 77 72 20 3d 20 69 64 78         lwr = idx
17293 2b 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  +1;.      }else{
17294 0a 20 20 20 20 20 20 20 20 75 70 72 20 3d 20 69  .        upr = i
17295 64 78 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20  dx-1;.      }.  
17296 20 20 20 20 69 66 28 20 6c 77 72 3e 75 70 72 20      if( lwr>upr 
17297 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ){.        break
17298 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
17299 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72  pCur->aiIdx[pCur
1729a 2d 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 36 29  ->iPage] = (u16)
1729b 28 28 6c 77 72 2b 75 70 72 29 2f 32 29 3b 0a 20  ((lwr+upr)/2);. 
1729c 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
1729d 20 6c 77 72 3d 3d 75 70 72 2b 31 20 29 3b 0a 20   lwr==upr+1 );. 
1729e 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
1729f 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 20 20  ->isInit );.    
172a0 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20  if( pPage->leaf 
172a1 29 7b 0a 20 20 20 20 20 20 63 68 6c 64 50 67 20  ){.      chldPg 
172a2 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  = 0;.    }else i
172a3 66 28 20 6c 77 72 3e 3d 70 50 61 67 65 2d 3e 6e  f( lwr>=pPage->n
172a4 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 63 68  Cell ){.      ch
172a5 6c 64 50 67 20 3d 20 67 65 74 34 62 79 74 65 28  ldPg = get4byte(
172a6 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50  &pPage->aData[pP
172a7 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38  age->hdrOffset+8
172a8 5d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ]);.    }else{. 
172a9 20 20 20 20 20 63 68 6c 64 50 67 20 3d 20 67 65       chldPg = ge
172aa 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28  t4byte(findCell(
172ab 70 50 61 67 65 2c 20 6c 77 72 29 29 3b 0a 20 20  pPage, lwr));.  
172ac 20 20 7d 0a 20 20 20 20 69 66 28 20 63 68 6c 64    }.    if( chld
172ad 50 67 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61  Pg==0 ){.      a
172ae 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49  ssert( pCur->aiI
172af 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c  dx[pCur->iPage]<
172b0 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
172b1 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c  r->iPage]->nCell
172b2 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20   );.      *pRes 
172b3 3d 20 63 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  = c;.      rc = 
172b4 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20  SQLITE_OK;.     
172b5 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e   goto moveto_fin
172b6 69 73 68 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  ish;.    }.    p
172b7 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d  Cur->aiIdx[pCur-
172b8 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 36 29 6c  >iPage] = (u16)l
172b9 77 72 3b 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e  wr;.    pCur->in
172ba 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20  fo.nSize = 0;.  
172bb 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65    pCur->validNKe
172bc 79 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20  y = 0;.    rc = 
172bd 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72  moveToChild(pCur
172be 2c 20 63 68 6c 64 50 67 29 3b 0a 20 20 20 20 69  , chldPg);.    i
172bf 66 28 20 72 63 20 29 20 67 6f 74 6f 20 6d 6f 76  f( rc ) goto mov
172c0 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 7d 0a  eto_finish;.  }.
172c1 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3a 0a 20  moveto_finish:. 
172c2 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a   return rc;.}...
172c3 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55  /*.** Return TRU
172c4 45 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 20  E if the cursor 
172c5 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 20  is not pointing 
172c6 61 74 20 61 6e 20 65 6e 74 72 79 20 6f 66 20 74  at an entry of t
172c7 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  he table..**.** 
172c8 54 52 55 45 20 77 69 6c 6c 20 62 65 20 72 65 74  TRUE will be ret
172c9 75 72 6e 65 64 20 61 66 74 65 72 20 61 20 63 61  urned after a ca
172ca 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72  ll to sqlite3Btr
172cb 65 65 4e 65 78 74 28 29 20 6d 6f 76 65 73 0a 2a  eeNext() moves.*
172cc 2a 20 70 61 73 74 20 74 68 65 20 6c 61 73 74 20  * past the last 
172cd 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61 62  entry in the tab
172ce 6c 65 20 6f 72 20 73 71 6c 69 74 65 33 42 74 72  le or sqlite3Btr
172cf 65 65 50 72 65 76 28 29 20 6d 6f 76 65 73 20 70  eePrev() moves p
172d0 61 73 74 0a 2a 2a 20 74 68 65 20 66 69 72 73 74  ast.** the first
172d1 20 65 6e 74 72 79 2e 20 20 54 52 55 45 20 69 73   entry.  TRUE is
172d2 20 61 6c 73 6f 20 72 65 74 75 72 6e 65 64 20 69   also returned i
172d3 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65  f the table is e
172d4 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  mpty..*/.SQLITE_
172d5 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
172d6 74 65 33 42 74 72 65 65 45 6f 66 28 42 74 43 75  te3BtreeEof(BtCu
172d7 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 2f  rsor *pCur){.  /
172d8 2a 20 54 4f 44 4f 3a 20 57 68 61 74 20 69 66 20  * TODO: What if 
172d9 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 69 6e  the cursor is in
172da 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53   CURSOR_REQUIRES
172db 45 45 4b 20 62 75 74 20 61 6c 6c 20 74 61 62 6c  EEK but all tabl
172dc 65 20 65 6e 74 72 69 65 73 0a 20 20 2a 2a 20 68  e entries.  ** h
172dd 61 76 65 20 62 65 65 6e 20 64 65 6c 65 74 65 64  ave been deleted
172de 3f 20 54 68 69 73 20 41 50 49 20 77 69 6c 6c 20  ? This API will 
172df 6e 65 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74  need to change t
172e0 6f 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f  o return an erro
172e1 72 20 63 6f 64 65 0a 20 20 2a 2a 20 61 73 20 77  r code.  ** as w
172e2 65 6c 6c 20 61 73 20 74 68 65 20 62 6f 6f 6c 65  ell as the boole
172e3 61 6e 20 72 65 73 75 6c 74 20 76 61 6c 75 65 2e  an result value.
172e4 0a 20 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28  .  */.  return (
172e5 43 55 52 53 4f 52 5f 56 41 4c 49 44 21 3d 70 43  CURSOR_VALID!=pC
172e6 75 72 2d 3e 65 53 74 61 74 65 29 3b 0a 7d 0a 0a  ur->eState);.}..
172e7 2f 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 74 68  /*.** Advance th
172e8 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20  e cursor to the 
172e9 6e 65 78 74 20 65 6e 74 72 79 20 69 6e 20 74 68  next entry in th
172ea 65 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 0a  e database.  If.
172eb 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 74 68  ** successful th
172ec 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 30 2e 20  en set *pRes=0. 
172ed 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 0a 2a   If the cursor.*
172ee 2a 20 77 61 73 20 61 6c 72 65 61 64 79 20 70 6f  * was already po
172ef 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 6c 61  inting to the la
172f0 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  st entry in the 
172f1 64 61 74 61 62 61 73 65 20 62 65 66 6f 72 65 0a  database before.
172f2 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ** this routine 
172f3 77 61 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e  was called, then
172f4 20 73 65 74 20 2a 70 52 65 73 3d 31 2e 0a 2a 2f   set *pRes=1..*/
172f5 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
172f6 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
172f7 4e 65 78 74 28 42 74 43 75 72 73 6f 72 20 2a 70  Next(BtCursor *p
172f8 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b  Cur, int *pRes){
172f9 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74  .  int rc;.  int
172fa 20 69 64 78 3b 0a 20 20 4d 65 6d 50 61 67 65 20   idx;.  MemPage 
172fb 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72  *pPage;..  asser
172fc 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75  t( cursorHoldsMu
172fd 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72  tex(pCur) );.  r
172fe 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f  c = restoreCurso
172ff 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b  rPosition(pCur);
17300 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
17301 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75  E_OK ){.    retu
17302 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73  rn rc;.  }.  ass
17303 65 72 74 28 20 70 52 65 73 21 3d 30 20 29 3b 0a  ert( pRes!=0 );.
17304 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49 4e 56    if( CURSOR_INV
17305 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61  ALID==pCur->eSta
17306 74 65 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20  te ){.    *pRes 
17307 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = 1;.    return 
17308 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
17309 20 69 66 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e   if( pCur->skipN
1730a 65 78 74 3e 30 20 29 7b 0a 20 20 20 20 70 43 75  ext>0 ){.    pCu
1730b 72 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b  r->skipNext = 0;
1730c 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a  .    *pRes = 0;.
1730d 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1730e 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 43 75 72  E_OK;.  }.  pCur
1730f 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0a  ->skipNext = 0;.
17310 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d  .  pPage = pCur-
17311 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
17312 61 67 65 5d 3b 0a 20 20 69 64 78 20 3d 20 2b 2b  age];.  idx = ++
17313 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72  pCur->aiIdx[pCur
17314 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65  ->iPage];.  asse
17315 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69  rt( pPage->isIni
17316 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  t );.  assert( i
17317 64 78 3c 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  dx<=pPage->nCell
17318 20 29 3b 0a 0a 20 20 70 43 75 72 2d 3e 69 6e 66   );..  pCur->inf
17319 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70  o.nSize = 0;.  p
1731a 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d  Cur->validNKey =
1731b 20 30 3b 0a 20 20 69 66 28 20 69 64 78 3e 3d 70   0;.  if( idx>=p
1731c 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20  Page->nCell ){. 
1731d 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c     if( !pPage->l
1731e 65 61 66 20 29 7b 0a 20 20 20 20 20 20 72 63 20  eaf ){.      rc 
1731f 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43  = moveToChild(pC
17320 75 72 2c 20 67 65 74 34 62 79 74 65 28 26 70 50  ur, get4byte(&pP
17321 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65  age->aData[pPage
17322 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 29  ->hdrOffset+8]))
17323 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29  ;.      if( rc )
17324 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
17325 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66    rc = moveToLef
17326 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 20  tmost(pCur);.   
17327 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20     *pRes = 0;.  
17328 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
17329 20 20 20 7d 0a 20 20 20 20 64 6f 7b 0a 20 20 20     }.    do{.   
1732a 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61     if( pCur->iPa
1732b 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ge==0 ){.       
1732c 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20   *pRes = 1;.    
1732d 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65      pCur->eState
1732e 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49   = CURSOR_INVALI
1732f 44 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  D;.        retur
17330 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  n SQLITE_OK;.   
17331 20 20 20 7d 0a 20 20 20 20 20 20 6d 6f 76 65 54     }.      moveT
17332 6f 50 61 72 65 6e 74 28 70 43 75 72 29 3b 0a 20  oParent(pCur);. 
17333 20 20 20 20 20 70 50 61 67 65 20 3d 20 70 43 75       pPage = pCu
17334 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
17335 69 50 61 67 65 5d 3b 0a 20 20 20 20 7d 77 68 69  iPage];.    }whi
17336 6c 65 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b  le( pCur->aiIdx[
17337 70 43 75 72 2d 3e 69 50 61 67 65 5d 3e 3d 70 50  pCur->iPage]>=pP
17338 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20  age->nCell );.  
17339 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20    *pRes = 0;.   
1733a 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b   if( pPage->intK
1733b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  ey ){.      rc =
1733c 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78   sqlite3BtreeNex
1733d 74 28 70 43 75 72 2c 20 70 52 65 73 29 3b 0a 20  t(pCur, pRes);. 
1733e 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1733f 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
17340 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
17341 20 72 63 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 73   rc;.  }.  *pRes
17342 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 50 61 67   = 0;.  if( pPag
17343 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 72  e->leaf ){.    r
17344 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
17345 0a 20 20 7d 0a 20 20 72 63 20 3d 20 6d 6f 76 65  .  }.  rc = move
17346 54 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75 72 29  ToLeftmost(pCur)
17347 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
17348 0a 0a 0a 2f 2a 0a 2a 2a 20 53 74 65 70 20 74 68  .../*.** Step th
17349 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20  e cursor to the 
1734a 62 61 63 6b 20 74 6f 20 74 68 65 20 70 72 65 76  back to the prev
1734b 69 6f 75 73 20 65 6e 74 72 79 20 69 6e 20 74 68  ious entry in th
1734c 65 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 0a  e database.  If.
1734d 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 74 68  ** successful th
1734e 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 30 2e 20  en set *pRes=0. 
1734f 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 0a 2a   If the cursor.*
17350 2a 20 77 61 73 20 61 6c 72 65 61 64 79 20 70 6f  * was already po
17351 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 66 69  inting to the fi
17352 72 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65  rst entry in the
17353 20 64 61 74 61 62 61 73 65 20 62 65 66 6f 72 65   database before
17354 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65  .** this routine
17355 20 77 61 73 20 63 61 6c 6c 65 64 2c 20 74 68 65   was called, the
17356 6e 20 73 65 74 20 2a 70 52 65 73 3d 31 2e 0a 2a  n set *pRes=1..*
17357 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
17358 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
17359 65 50 72 65 76 69 6f 75 73 28 42 74 43 75 72 73  ePrevious(BtCurs
1735a 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70  or *pCur, int *p
1735b 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  Res){.  int rc;.
1735c 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
1735d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72  ;..  assert( cur
1735e 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43  sorHoldsMutex(pC
1735f 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65  ur) );.  rc = re
17360 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74  storeCursorPosit
17361 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 28  ion(pCur);.  if(
17362 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
17363 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  {.    return rc;
17364 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 61 74 4c  .  }.  pCur->atL
17365 61 73 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 43  ast = 0;.  if( C
17366 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d 70  URSOR_INVALID==p
17367 43 75 72 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20  Cur->eState ){. 
17368 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20     *pRes = 1;.  
17369 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1736a 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43  OK;.  }.  if( pC
1736b 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3c 30 20 29  ur->skipNext<0 )
1736c 7b 0a 20 20 20 20 70 43 75 72 2d 3e 73 6b 69 70  {.    pCur->skip
1736d 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 2a 70  Next = 0;.    *p
1736e 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 65 74  Res = 0;.    ret
1736f 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
17370 20 7d 0a 20 20 70 43 75 72 2d 3e 73 6b 69 70 4e   }.  pCur->skipN
17371 65 78 74 20 3d 20 30 3b 0a 0a 20 20 70 50 61 67  ext = 0;..  pPag
17372 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65  e = pCur->apPage
17373 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20  [pCur->iPage];. 
17374 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
17375 69 73 49 6e 69 74 20 29 3b 0a 20 20 69 66 28 20  isInit );.  if( 
17376 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a  !pPage->leaf ){.
17377 20 20 20 20 69 6e 74 20 69 64 78 20 3d 20 70 43      int idx = pC
17378 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e  ur->aiIdx[pCur->
17379 69 50 61 67 65 5d 3b 0a 20 20 20 20 72 63 20 3d  iPage];.    rc =
1737a 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75   moveToChild(pCu
1737b 72 2c 20 67 65 74 34 62 79 74 65 28 66 69 6e 64  r, get4byte(find
1737c 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 29  Cell(pPage, idx)
1737d 29 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  ));.    if( rc )
1737e 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  {.      return r
1737f 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20  c;.    }.    rc 
17380 3d 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73  = moveToRightmos
17381 74 28 70 43 75 72 29 3b 0a 20 20 7d 65 6c 73 65  t(pCur);.  }else
17382 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 43 75  {.    while( pCu
17383 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
17384 50 61 67 65 5d 3d 3d 30 20 29 7b 0a 20 20 20 20  Page]==0 ){.    
17385 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67    if( pCur->iPag
17386 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  e==0 ){.        
17387 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43  pCur->eState = C
17388 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20  URSOR_INVALID;. 
17389 20 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31         *pRes = 1
1738a 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
1738b 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
1738c 20 20 7d 0a 20 20 20 20 20 20 6d 6f 76 65 54 6f    }.      moveTo
1738d 50 61 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 20  Parent(pCur);.  
1738e 20 20 7d 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e    }.    pCur->in
1738f 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20  fo.nSize = 0;.  
17390 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65    pCur->validNKe
17391 79 20 3d 20 30 3b 0a 0a 20 20 20 20 70 43 75 72  y = 0;..    pCur
17392 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50  ->aiIdx[pCur->iP
17393 61 67 65 5d 2d 2d 3b 0a 20 20 20 20 70 50 61 67  age]--;.    pPag
17394 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65  e = pCur->apPage
17395 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20  [pCur->iPage];. 
17396 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e     if( pPage->in
17397 74 4b 65 79 20 26 26 20 21 70 50 61 67 65 2d 3e  tKey && !pPage->
17398 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 72 63  leaf ){.      rc
17399 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50   = sqlite3BtreeP
1739a 72 65 76 69 6f 75 73 28 70 43 75 72 2c 20 70 52  revious(pCur, pR
1739b 65 73 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  es);.    }else{.
1739c 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
1739d 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  E_OK;.    }.  }.
1739e 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 72    *pRes = 0;.  r
1739f 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
173a0 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65  ** Allocate a ne
173a1 77 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20  w page from the 
173a2 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a  database file..*
173a3 2a 0a 2a 2a 20 54 68 65 20 6e 65 77 20 70 61 67  *.** The new pag
173a4 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 64  e is marked as d
173a5 69 72 74 79 2e 20 20 28 49 6e 20 6f 74 68 65 72  irty.  (In other
173a6 20 77 6f 72 64 73 2c 20 73 71 6c 69 74 65 33 50   words, sqlite3P
173a7 61 67 65 72 57 72 69 74 65 28 29 0a 2a 2a 20 68  agerWrite().** h
173a8 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
173a9 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 6e 65  called on the ne
173aa 77 20 70 61 67 65 2e 29 20 20 54 68 65 20 6e 65  w page.)  The ne
173ab 77 20 70 61 67 65 20 68 61 73 20 61 6c 73 6f 0a  w page has also.
173ac 2a 2a 20 62 65 65 6e 20 72 65 66 65 72 65 6e 63  ** been referenc
173ad 65 64 20 61 6e 64 20 74 68 65 20 63 61 6c 6c 69  ed and the calli
173ae 6e 67 20 72 6f 75 74 69 6e 65 20 69 73 20 72 65  ng routine is re
173af 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 63 61  sponsible for ca
173b0 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33  lling.** sqlite3
173b1 50 61 67 65 72 55 6e 72 65 66 28 29 20 6f 6e 20  PagerUnref() on 
173b2 74 68 65 20 6e 65 77 20 70 61 67 65 20 77 68 65  the new page whe
173b3 6e 20 69 74 20 69 73 20 64 6f 6e 65 2e 0a 2a 2a  n it is done..**
173b4 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  .** SQLITE_OK is
173b5 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 75 63   returned on suc
173b6 63 65 73 73 2e 20 20 41 6e 79 20 6f 74 68 65 72  cess.  Any other
173b7 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 6e   return value in
173b8 64 69 63 61 74 65 73 0a 2a 2a 20 61 6e 20 65 72  dicates.** an er
173b9 72 6f 72 2e 20 20 2a 70 70 50 61 67 65 20 61 6e  ror.  *ppPage an
173ba 64 20 2a 70 50 67 6e 6f 20 61 72 65 20 75 6e 64  d *pPgno are und
173bb 65 66 69 6e 65 64 20 69 6e 20 74 68 65 20 65 76  efined in the ev
173bc 65 6e 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 2e  ent of an error.
173bd 0a 2a 2a 20 44 6f 20 6e 6f 74 20 69 6e 76 6f 6b  .** Do not invok
173be 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e  e sqlite3PagerUn
173bf 72 65 66 28 29 20 6f 6e 20 2a 70 70 50 61 67 65  ref() on *ppPage
173c0 20 69 66 20 61 6e 20 65 72 72 6f 72 20 69 73 20   if an error is 
173c1 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
173c2 49 66 20 74 68 65 20 22 6e 65 61 72 62 79 22 20  If the "nearby" 
173c3 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74  parameter is not
173c4 20 30 2c 20 74 68 65 6e 20 61 20 28 66 65 65 62   0, then a (feeb
173c5 6c 65 29 20 65 66 66 6f 72 74 20 69 73 20 6d 61  le) effort is ma
173c6 64 65 20 74 6f 20 0a 2a 2a 20 6c 6f 63 61 74 65  de to .** locate
173c7 20 61 20 70 61 67 65 20 63 6c 6f 73 65 20 74 6f   a page close to
173c8 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72   the page number
173c9 20 22 6e 65 61 72 62 79 22 2e 20 20 54 68 69 73   "nearby".  This
173ca 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 6e 20   can be used in 
173cb 61 6e 0a 2a 2a 20 61 74 74 65 6d 70 74 20 74 6f  an.** attempt to
173cc 20 6b 65 65 70 20 72 65 6c 61 74 65 64 20 70 61   keep related pa
173cd 67 65 73 20 63 6c 6f 73 65 20 74 6f 20 65 61 63  ges close to eac
173ce 68 20 6f 74 68 65 72 20 69 6e 20 74 68 65 20 64  h other in the d
173cf 61 74 61 62 61 73 65 20 66 69 6c 65 2c 0a 2a 2a  atabase file,.**
173d0 20 77 68 69 63 68 20 69 6e 20 74 75 72 6e 20 63   which in turn c
173d1 61 6e 20 6d 61 6b 65 20 64 61 74 61 62 61 73 65  an make database
173d2 20 61 63 63 65 73 73 20 66 61 73 74 65 72 2e 0a   access faster..
173d3 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 65 78  **.** If the "ex
173d4 61 63 74 22 20 70 61 72 61 6d 65 74 65 72 20 69  act" parameter i
173d5 73 20 6e 6f 74 20 30 2c 20 61 6e 64 20 74 68 65  s not 0, and the
173d6 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 6e 65 61   page-number nea
173d7 72 62 79 20 65 78 69 73 74 73 20 0a 2a 2a 20 61  rby exists .** a
173d8 6e 79 77 68 65 72 65 20 6f 6e 20 74 68 65 20 66  nywhere on the f
173d9 72 65 65 2d 6c 69 73 74 2c 20 74 68 65 6e 20 69  ree-list, then i
173da 74 20 69 73 20 67 75 61 72 65 6e 74 65 65 64 20  t is guarenteed 
173db 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20  to be returned. 
173dc 54 68 69 73 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20  This.** is only 
173dd 75 73 65 64 20 62 79 20 61 75 74 6f 2d 76 61 63  used by auto-vac
173de 75 75 6d 20 64 61 74 61 62 61 73 65 73 20 77 68  uum databases wh
173df 65 6e 20 61 6c 6c 6f 63 61 74 69 6e 67 20 61 20  en allocating a 
173e0 6e 65 77 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74  new table..*/.st
173e1 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74  atic int allocat
173e2 65 42 74 72 65 65 50 61 67 65 28 0a 20 20 42 74  eBtreePage(.  Bt
173e3 53 68 61 72 65 64 20 2a 70 42 74 2c 20 0a 20 20  Shared *pBt, .  
173e4 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65  MemPage **ppPage
173e5 2c 20 0a 20 20 50 67 6e 6f 20 2a 70 50 67 6e 6f  , .  Pgno *pPgno
173e6 2c 20 0a 20 20 50 67 6e 6f 20 6e 65 61 72 62 79  , .  Pgno nearby
173e7 2c 0a 20 20 75 38 20 65 78 61 63 74 0a 29 7b 0a  ,.  u8 exact.){.
173e8 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
173e9 31 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 75  1;.  int rc;.  u
173ea 33 32 20 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d  32 n;     /* Num
173eb 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f 6e 20  ber of pages on 
173ec 74 68 65 20 66 72 65 65 6c 69 73 74 20 2a 2f 0a  the freelist */.
173ed 20 20 75 33 32 20 6b 3b 20 20 20 20 20 2f 2a 20    u32 k;     /* 
173ee 4e 75 6d 62 65 72 20 6f 66 20 6c 65 61 76 65 73  Number of leaves
173ef 20 6f 6e 20 74 68 65 20 74 72 75 6e 6b 20 6f 66   on the trunk of
173f0 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 2a 2f   the freelist */
173f1 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 72 75  .  MemPage *pTru
173f2 6e 6b 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67  nk = 0;.  MemPag
173f3 65 20 2a 70 50 72 65 76 54 72 75 6e 6b 20 3d 20  e *pPrevTrunk = 
173f4 30 3b 0a 20 20 50 67 6e 6f 20 6d 78 50 61 67 65  0;.  Pgno mxPage
173f5 3b 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73  ;     /* Total s
173f6 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ize of the datab
173f7 61 73 65 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 61  ase file */..  a
173f8 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
173f9 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d  utex_held(pBt->m
173fa 75 74 65 78 29 20 29 3b 0a 20 20 70 50 61 67 65  utex) );.  pPage
173fb 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b  1 = pBt->pPage1;
173fc 0a 20 20 6d 78 50 61 67 65 20 3d 20 70 61 67 65  .  mxPage = page
173fd 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b  rPagecount(pBt);
173fe 0a 20 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28  .  n = get4byte(
173ff 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  &pPage1->aData[3
17400 36 5d 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28  6]);.  testcase(
17401 20 6e 3d 3d 6d 78 50 61 67 65 2d 31 20 29 3b 0a   n==mxPage-1 );.
17402 20 20 69 66 28 20 6e 3e 3d 6d 78 50 61 67 65 20    if( n>=mxPage 
17403 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
17404 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
17405 54 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 3e 30  T;.  }.  if( n>0
17406 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 72 65   ){.    /* There
17407 20 61 72 65 20 70 61 67 65 73 20 6f 6e 20 74 68   are pages on th
17408 65 20 66 72 65 65 6c 69 73 74 2e 20 20 52 65 75  e freelist.  Reu
17409 73 65 20 6f 6e 65 20 6f 66 20 74 68 6f 73 65 20  se one of those 
1740a 70 61 67 65 73 2e 20 2a 2f 0a 20 20 20 20 50 67  pages. */.    Pg
1740b 6e 6f 20 69 54 72 75 6e 6b 3b 0a 20 20 20 20 75  no iTrunk;.    u
1740c 38 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30  8 searchList = 0
1740d 3b 20 2f 2a 20 49 66 20 74 68 65 20 66 72 65 65  ; /* If the free
1740e 2d 6c 69 73 74 20 6d 75 73 74 20 62 65 20 73 65  -list must be se
1740f 61 72 63 68 65 64 20 66 6f 72 20 27 6e 65 61 72  arched for 'near
17410 62 79 27 20 2a 2f 0a 20 20 20 20 0a 20 20 20 20  by' */.    .    
17411 2f 2a 20 49 66 20 74 68 65 20 27 65 78 61 63 74  /* If the 'exact
17412 27 20 70 61 72 61 6d 65 74 65 72 20 77 61 73 20  ' parameter was 
17413 74 72 75 65 20 61 6e 64 20 61 20 71 75 65 72 79  true and a query
17414 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2d   of the pointer-
17415 6d 61 70 0a 20 20 20 20 2a 2a 20 73 68 6f 77 73  map.    ** shows
17416 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 27   that the page '
17417 6e 65 61 72 62 79 27 20 69 73 20 73 6f 6d 65 77  nearby' is somew
17418 68 65 72 65 20 6f 6e 20 74 68 65 20 66 72 65 65  here on the free
17419 2d 6c 69 73 74 2c 20 74 68 65 6e 0a 20 20 20 20  -list, then.    
1741a 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 2d 6c 69  ** the entire-li
1741b 73 74 20 77 69 6c 6c 20 62 65 20 73 65 61 72 63  st will be searc
1741c 68 65 64 20 66 6f 72 20 74 68 61 74 20 70 61 67  hed for that pag
1741d 65 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65  e..    */.#ifnde
1741e 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
1741f 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28  TOVACUUM.    if(
17420 20 65 78 61 63 74 20 26 26 20 6e 65 61 72 62 79   exact && nearby
17421 3c 3d 6d 78 50 61 67 65 20 29 7b 0a 20 20 20 20  <=mxPage ){.    
17422 20 20 75 38 20 65 54 79 70 65 3b 0a 20 20 20 20    u8 eType;.    
17423 20 20 61 73 73 65 72 74 28 20 6e 65 61 72 62 79    assert( nearby
17424 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  >0 );.      asse
17425 72 74 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63  rt( pBt->autoVac
17426 75 75 6d 20 29 3b 0a 20 20 20 20 20 20 72 63 20  uum );.      rc 
17427 3d 20 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c  = ptrmapGet(pBt,
17428 20 6e 65 61 72 62 79 2c 20 26 65 54 79 70 65 2c   nearby, &eType,
17429 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72   0);.      if( r
1742a 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  c ) return rc;. 
1742b 20 20 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d       if( eType==
1742c 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20  PTRMAP_FREEPAGE 
1742d 29 7b 0a 20 20 20 20 20 20 20 20 73 65 61 72 63  ){.        searc
1742e 68 4c 69 73 74 20 3d 20 31 3b 0a 20 20 20 20 20  hList = 1;.     
1742f 20 7d 0a 20 20 20 20 20 20 2a 70 50 67 6e 6f 20   }.      *pPgno 
17430 3d 20 6e 65 61 72 62 79 3b 0a 20 20 20 20 7d 0a  = nearby;.    }.
17431 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 44  #endif..    /* D
17432 65 63 72 65 6d 65 6e 74 20 74 68 65 20 66 72 65  ecrement the fre
17433 65 2d 6c 69 73 74 20 63 6f 75 6e 74 20 62 79 20  e-list count by 
17434 31 2e 20 53 65 74 20 69 54 72 75 6e 6b 20 74 6f  1. Set iTrunk to
17435 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   the index of th
17436 65 0a 20 20 20 20 2a 2a 20 66 69 72 73 74 20 66  e.    ** first f
17437 72 65 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70  ree-list trunk p
17438 61 67 65 2e 20 69 50 72 65 76 54 72 75 6e 6b 20  age. iPrevTrunk 
17439 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 31 2e 0a  is initially 1..
1743a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20      */.    rc = 
1743b 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
1743c 65 28 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67  e(pPage1->pDbPag
1743d 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  e);.    if( rc )
1743e 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
1743f 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65 31  put4byte(&pPage1
17440 2d 3e 61 44 61 74 61 5b 33 36 5d 2c 20 6e 2d 31  ->aData[36], n-1
17441 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 63  );..    /* The c
17442 6f 64 65 20 77 69 74 68 69 6e 20 74 68 69 73 20  ode within this 
17443 6c 6f 6f 70 20 69 73 20 72 75 6e 20 6f 6e 6c 79  loop is run only
17444 20 6f 6e 63 65 20 69 66 20 74 68 65 20 27 73 65   once if the 'se
17445 61 72 63 68 4c 69 73 74 27 20 76 61 72 69 61 62  archList' variab
17446 6c 65 0a 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74  le.    ** is not
17447 20 74 72 75 65 2e 20 4f 74 68 65 72 77 69 73 65   true. Otherwise
17448 2c 20 69 74 20 72 75 6e 73 20 6f 6e 63 65 20 66  , it runs once f
17449 6f 72 20 65 61 63 68 20 74 72 75 6e 6b 2d 70 61  or each trunk-pa
1744a 67 65 20 6f 6e 20 74 68 65 0a 20 20 20 20 2a 2a  ge on the.    **
1744b 20 66 72 65 65 2d 6c 69 73 74 20 75 6e 74 69 6c   free-list until
1744c 20 74 68 65 20 70 61 67 65 20 27 6e 65 61 72 62   the page 'nearb
1744d 79 27 20 69 73 20 6c 6f 63 61 74 65 64 2e 0a 20  y' is located.. 
1744e 20 20 20 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a 20     */.    do {. 
1744f 20 20 20 20 20 70 50 72 65 76 54 72 75 6e 6b 20       pPrevTrunk 
17450 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20  = pTrunk;.      
17451 69 66 28 20 70 50 72 65 76 54 72 75 6e 6b 20 29  if( pPrevTrunk )
17452 7b 0a 20 20 20 20 20 20 20 20 69 54 72 75 6e 6b  {.        iTrunk
17453 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 72   = get4byte(&pPr
17454 65 76 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30  evTrunk->aData[0
17455 5d 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ]);.      }else{
17456 0a 20 20 20 20 20 20 20 20 69 54 72 75 6e 6b 20  .        iTrunk 
17457 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67  = get4byte(&pPag
17458 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29 3b 0a  e1->aData[32]);.
17459 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 74 65        }.      te
1745a 73 74 63 61 73 65 28 20 69 54 72 75 6e 6b 3d 3d  stcase( iTrunk==
1745b 6d 78 50 61 67 65 20 29 3b 0a 20 20 20 20 20 20  mxPage );.      
1745c 69 66 28 20 69 54 72 75 6e 6b 3e 6d 78 50 61 67  if( iTrunk>mxPag
1745d 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  e ){.        rc 
1745e 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  = SQLITE_CORRUPT
1745f 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d 65 6c  _BKPT;.      }el
17460 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  se{.        rc =
17461 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42   btreeGetPage(pB
17462 74 2c 20 69 54 72 75 6e 6b 2c 20 26 70 54 72 75  t, iTrunk, &pTru
17463 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a  nk, 0);.      }.
17464 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a        if( rc ){.
17465 20 20 20 20 20 20 20 20 70 54 72 75 6e 6b 20 3d          pTrunk =
17466 20 30 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f   0;.        goto
17467 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61   end_allocate_pa
17468 67 65 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  ge;.      }..   
17469 20 20 20 6b 20 3d 20 67 65 74 34 62 79 74 65 28     k = get4byte(
1746a 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34  &pTrunk->aData[4
1746b 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6b 3d  ]);.      if( k=
1746c 3d 30 20 26 26 20 21 73 65 61 72 63 68 4c 69 73  =0 && !searchLis
1746d 74 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  t ){.        /* 
1746e 54 68 65 20 74 72 75 6e 6b 20 68 61 73 20 6e 6f  The trunk has no
1746f 20 6c 65 61 76 65 73 20 61 6e 64 20 74 68 65 20   leaves and the 
17470 6c 69 73 74 20 69 73 20 6e 6f 74 20 62 65 69 6e  list is not bein
17471 67 20 73 65 61 72 63 68 65 64 2e 20 0a 20 20 20  g searched. .   
17472 20 20 20 20 20 2a 2a 20 53 6f 20 65 78 74 72 61       ** So extra
17473 63 74 20 74 68 65 20 74 72 75 6e 6b 20 70 61 67  ct the trunk pag
17474 65 20 69 74 73 65 6c 66 20 61 6e 64 20 75 73 65  e itself and use
17475 20 69 74 20 61 73 20 74 68 65 20 6e 65 77 6c 79   it as the newly
17476 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 6c   .        ** all
17477 6f 63 61 74 65 64 20 70 61 67 65 20 2a 2f 0a 20  ocated page */. 
17478 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
17479 50 72 65 76 54 72 75 6e 6b 3d 3d 30 20 29 3b 0a  PrevTrunk==0 );.
1747a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
1747b 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
1747c 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b  Trunk->pDbPage);
1747d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20  .        if( rc 
1747e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  ){.          got
1747f 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70  o end_allocate_p
17480 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  age;.        }. 
17481 20 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20         *pPgno = 
17482 69 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20  iTrunk;.        
17483 6d 65 6d 63 70 79 28 26 70 50 61 67 65 31 2d 3e  memcpy(&pPage1->
17484 61 44 61 74 61 5b 33 32 5d 2c 20 26 70 54 72 75  aData[32], &pTru
17485 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29  nk->aData[0], 4)
17486 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 50 61 67  ;.        *ppPag
17487 65 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20 20  e = pTrunk;.    
17488 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a      pTrunk = 0;.
17489 20 20 20 20 20 20 20 20 54 52 41 43 45 28 28 22          TRACE(("
1748a 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 74 72 75  ALLOCATE: %d tru
1748b 6e 6b 20 2d 20 25 64 20 66 72 65 65 20 70 61 67  nk - %d free pag
1748c 65 73 20 6c 65 66 74 5c 6e 22 2c 20 2a 70 50 67  es left\n", *pPg
1748d 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 20 20 20 20 20  no, n-1));.     
1748e 20 7d 65 6c 73 65 20 69 66 28 20 6b 3e 28 75 33   }else if( k>(u3
1748f 32 29 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  2)(pBt->usableSi
17490 7a 65 2f 34 20 2d 20 32 29 20 29 7b 0a 20 20 20  ze/4 - 2) ){.   
17491 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66       /* Value of
17492 20 6b 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e   k is out of ran
17493 67 65 2e 20 20 44 61 74 61 62 61 73 65 20 63 6f  ge.  Database co
17494 72 72 75 70 74 69 6f 6e 20 2a 2f 0a 20 20 20 20  rruption */.    
17495 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
17496 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
17497 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61        goto end_a
17498 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 23 69  llocate_page;.#i
17499 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1749a 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20  T_AUTOVACUUM.   
1749b 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 65 61     }else if( sea
1749c 72 63 68 4c 69 73 74 20 26 26 20 6e 65 61 72 62  rchList && nearb
1749d 79 3d 3d 69 54 72 75 6e 6b 20 29 7b 0a 20 20 20  y==iTrunk ){.   
1749e 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 69 73 74       /* The list
1749f 20 69 73 20 62 65 69 6e 67 20 73 65 61 72 63 68   is being search
174a0 65 64 20 61 6e 64 20 74 68 69 73 20 74 72 75 6e  ed and this trun
174a1 6b 20 70 61 67 65 20 69 73 20 74 68 65 20 70 61  k page is the pa
174a2 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f  ge.        ** to
174a3 20 61 6c 6c 6f 63 61 74 65 2c 20 72 65 67 61 72   allocate, regar
174a4 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 72  dless of whether
174a5 20 69 74 20 68 61 73 20 6c 65 61 76 65 73 2e 0a   it has leaves..
174a6 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
174a7 20 20 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e     assert( *pPgn
174a8 6f 3d 3d 69 54 72 75 6e 6b 20 29 3b 0a 20 20 20  o==iTrunk );.   
174a9 20 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20 70       *ppPage = p
174aa 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 73  Trunk;.        s
174ab 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 0a 20  earchList = 0;. 
174ac 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
174ad 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 54  te3PagerWrite(pT
174ae 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a  runk->pDbPage);.
174af 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29          if( rc )
174b0 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  {.          goto
174b1 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61   end_allocate_pa
174b2 67 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ge;.        }.  
174b3 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 30 20 29        if( k==0 )
174b4 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
174b5 21 70 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20  !pPrevTrunk ){. 
174b6 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70             memcp
174b7 79 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61  y(&pPage1->aData
174b8 5b 33 32 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61  [32], &pTrunk->a
174b9 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20  Data[0], 4);.   
174ba 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
174bb 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79            memcpy
174bc 28 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61 44  (&pPrevTrunk->aD
174bd 61 74 61 5b 30 5d 2c 20 26 70 54 72 75 6e 6b 2d  ata[0], &pTrunk-
174be 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20  >aData[0], 4);. 
174bf 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
174c0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
174c1 20 20 20 20 2f 2a 20 54 68 65 20 74 72 75 6e 6b      /* The trunk
174c2 20 70 61 67 65 20 69 73 20 72 65 71 75 69 72 65   page is require
174c3 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 20  d by the caller 
174c4 62 75 74 20 69 74 20 63 6f 6e 74 61 69 6e 73 20  but it contains 
174c5 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 6f  .          ** po
174c6 69 6e 74 65 72 73 20 74 6f 20 66 72 65 65 2d 6c  inters to free-l
174c7 69 73 74 20 6c 65 61 76 65 73 2e 20 54 68 65 20  ist leaves. The 
174c8 66 69 72 73 74 20 6c 65 61 66 20 62 65 63 6f 6d  first leaf becom
174c9 65 73 20 61 20 74 72 75 6e 6b 0a 20 20 20 20 20  es a trunk.     
174ca 20 20 20 20 20 2a 2a 20 70 61 67 65 20 69 6e 20       ** page in 
174cb 74 68 69 73 20 63 61 73 65 2e 0a 20 20 20 20 20  this case..     
174cc 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
174cd 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 54    MemPage *pNewT
174ce 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  runk;.          
174cf 50 67 6e 6f 20 69 4e 65 77 54 72 75 6e 6b 20 3d  Pgno iNewTrunk =
174d0 20 67 65 74 34 62 79 74 65 28 26 70 54 72 75 6e   get4byte(&pTrun
174d1 6b 2d 3e 61 44 61 74 61 5b 38 5d 29 3b 0a 20 20  k->aData[8]);.  
174d2 20 20 20 20 20 20 20 20 69 66 28 20 69 4e 65 77          if( iNew
174d3 54 72 75 6e 6b 3e 6d 78 50 61 67 65 20 29 7b 20  Trunk>mxPage ){ 
174d4 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20  .            rc 
174d5 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  = SQLITE_CORRUPT
174d6 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 20  _BKPT;.         
174d7 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f     goto end_allo
174d8 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20  cate_page;.     
174d9 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
174da 20 74 65 73 74 63 61 73 65 28 20 69 4e 65 77 54   testcase( iNewT
174db 72 75 6e 6b 3d 3d 6d 78 50 61 67 65 20 29 3b 0a  runk==mxPage );.
174dc 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 62            rc = b
174dd 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c  treeGetPage(pBt,
174de 20 69 4e 65 77 54 72 75 6e 6b 2c 20 26 70 4e 65   iNewTrunk, &pNe
174df 77 54 72 75 6e 6b 2c 20 30 29 3b 0a 20 20 20 20  wTrunk, 0);.    
174e0 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
174e1 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
174e2 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f         goto end_
174e3 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20  allocate_page;. 
174e4 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
174e5 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
174e6 33 50 61 67 65 72 57 72 69 74 65 28 70 4e 65 77  3PagerWrite(pNew
174e7 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b  Trunk->pDbPage);
174e8 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72  .          if( r
174e9 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
174ea 20 20 20 20 20 20 20 20 20 20 20 20 72 65 6c 65              rele
174eb 61 73 65 50 61 67 65 28 70 4e 65 77 54 72 75 6e  asePage(pNewTrun
174ec 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  k);.            
174ed 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74  goto end_allocat
174ee 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20  e_page;.        
174ef 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 6d 65    }.          me
174f0 6d 63 70 79 28 26 70 4e 65 77 54 72 75 6e 6b 2d  mcpy(&pNewTrunk-
174f1 3e 61 44 61 74 61 5b 30 5d 2c 20 26 70 54 72 75  >aData[0], &pTru
174f2 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29  nk->aData[0], 4)
174f3 3b 0a 20 20 20 20 20 20 20 20 20 20 70 75 74 34  ;.          put4
174f4 62 79 74 65 28 26 70 4e 65 77 54 72 75 6e 6b 2d  byte(&pNewTrunk-
174f5 3e 61 44 61 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b  >aData[4], k-1);
174f6 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70  .          memcp
174f7 79 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44  y(&pNewTrunk->aD
174f8 61 74 61 5b 38 5d 2c 20 26 70 54 72 75 6e 6b 2d  ata[8], &pTrunk-
174f9 3e 61 44 61 74 61 5b 31 32 5d 2c 20 28 6b 2d 31  >aData[12], (k-1
174fa 29 2a 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20  )*4);.          
174fb 72 65 6c 65 61 73 65 50 61 67 65 28 70 4e 65 77  releasePage(pNew
174fc 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20  Trunk);.        
174fd 20 20 69 66 28 20 21 70 50 72 65 76 54 72 75 6e    if( !pPrevTrun
174fe 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  k ){.           
174ff 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
17500 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65  PagerIswriteable
17501 28 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65  (pPage1->pDbPage
17502 29 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ) );.           
17503 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65   put4byte(&pPage
17504 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 69 4e  1->aData[32], iN
17505 65 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20  ewTrunk);.      
17506 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
17507 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
17508 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50  te3PagerWrite(pP
17509 72 65 76 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67  revTrunk->pDbPag
1750a 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  e);.            
1750b 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
1750c 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64          goto end
1750d 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a  _allocate_page;.
1750e 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
1750f 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62 79            put4by
17510 74 65 28 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e  te(&pPrevTrunk->
17511 61 44 61 74 61 5b 30 5d 2c 20 69 4e 65 77 54 72  aData[0], iNewTr
17512 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20  unk);.          
17513 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
17514 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a      pTrunk = 0;.
17515 20 20 20 20 20 20 20 20 54 52 41 43 45 28 28 22          TRACE(("
17516 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 74 72 75  ALLOCATE: %d tru
17517 6e 6b 20 2d 20 25 64 20 66 72 65 65 20 70 61 67  nk - %d free pag
17518 65 73 20 6c 65 66 74 5c 6e 22 2c 20 2a 70 50 67  es left\n", *pPg
17519 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 23 65 6e 64 69  no, n-1));.#endi
1751a 66 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  f.      }else if
1751b 28 20 6b 3e 30 20 29 7b 0a 20 20 20 20 20 20 20  ( k>0 ){.       
1751c 20 2f 2a 20 45 78 74 72 61 63 74 20 61 20 6c 65   /* Extract a le
1751d 61 66 20 66 72 6f 6d 20 74 68 65 20 74 72 75 6e  af from the trun
1751e 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 33 32  k */.        u32
1751f 20 63 6c 6f 73 65 73 74 3b 0a 20 20 20 20 20 20   closest;.      
17520 20 20 50 67 6e 6f 20 69 50 61 67 65 3b 0a 20 20    Pgno iPage;.  
17521 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63        unsigned c
17522 68 61 72 20 2a 61 44 61 74 61 20 3d 20 70 54 72  har *aData = pTr
17523 75 6e 6b 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20  unk->aData;.    
17524 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
17525 50 61 67 65 72 57 72 69 74 65 28 70 54 72 75 6e  PagerWrite(pTrun
17526 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  k->pDbPage);.   
17527 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20       if( rc ){. 
17528 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e           goto en
17529 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b  d_allocate_page;
1752a 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1752b 20 20 20 69 66 28 20 6e 65 61 72 62 79 3e 30 20     if( nearby>0 
1752c 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75 33 32  ){.          u32
1752d 20 69 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e   i;.          in
1752e 74 20 64 69 73 74 3b 0a 20 20 20 20 20 20 20 20  t dist;.        
1752f 20 20 63 6c 6f 73 65 73 74 20 3d 20 30 3b 0a 20    closest = 0;. 
17530 20 20 20 20 20 20 20 20 20 64 69 73 74 20 3d 20           dist = 
17531 67 65 74 34 62 79 74 65 28 26 61 44 61 74 61 5b  get4byte(&aData[
17532 38 5d 29 20 2d 20 6e 65 61 72 62 79 3b 0a 20 20  8]) - nearby;.  
17533 20 20 20 20 20 20 20 20 69 66 28 20 64 69 73 74          if( dist
17534 3c 30 20 29 20 64 69 73 74 20 3d 20 2d 64 69 73  <0 ) dist = -dis
17535 74 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72  t;.          for
17536 28 69 3d 31 3b 20 69 3c 6b 3b 20 69 2b 2b 29 7b  (i=1; i<k; i++){
17537 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74  .            int
17538 20 64 32 20 3d 20 67 65 74 34 62 79 74 65 28 26   d2 = get4byte(&
17539 61 44 61 74 61 5b 38 2b 69 2a 34 5d 29 20 2d 20  aData[8+i*4]) - 
1753a 6e 65 61 72 62 79 3b 0a 20 20 20 20 20 20 20 20  nearby;.        
1753b 20 20 20 20 69 66 28 20 64 32 3c 30 20 29 20 64      if( d2<0 ) d
1753c 32 20 3d 20 2d 64 32 3b 0a 20 20 20 20 20 20 20  2 = -d2;.       
1753d 20 20 20 20 20 69 66 28 20 64 32 3c 64 69 73 74       if( d2<dist
1753e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
1753f 20 20 63 6c 6f 73 65 73 74 20 3d 20 69 3b 0a 20    closest = i;. 
17540 20 20 20 20 20 20 20 20 20 20 20 20 20 64 69 73               dis
17541 74 20 3d 20 64 32 3b 0a 20 20 20 20 20 20 20 20  t = d2;.        
17542 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
17543 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  }.        }else{
17544 0a 20 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65  .          close
17545 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  st = 0;.        
17546 7d 0a 0a 20 20 20 20 20 20 20 20 69 50 61 67 65  }..        iPage
17547 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 44 61   = get4byte(&aDa
17548 74 61 5b 38 2b 63 6c 6f 73 65 73 74 2a 34 5d 29  ta[8+closest*4])
17549 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61  ;.        testca
1754a 73 65 28 20 69 50 61 67 65 3d 3d 6d 78 50 61 67  se( iPage==mxPag
1754b 65 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  e );.        if(
1754c 20 69 50 61 67 65 3e 6d 78 50 61 67 65 20 29 7b   iPage>mxPage ){
1754d 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
1754e 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
1754f 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 67  KPT;.          g
17550 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65  oto end_allocate
17551 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d  _page;.        }
17552 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73  .        testcas
17553 65 28 20 69 50 61 67 65 3d 3d 6d 78 50 61 67 65  e( iPage==mxPage
17554 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   );.        if( 
17555 21 73 65 61 72 63 68 4c 69 73 74 20 7c 7c 20 69  !searchList || i
17556 50 61 67 65 3d 3d 6e 65 61 72 62 79 20 29 7b 0a  Page==nearby ){.
17557 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 6f            int no
17558 43 6f 6e 74 65 6e 74 3b 0a 20 20 20 20 20 20 20  Content;.       
17559 20 20 20 2a 70 50 67 6e 6f 20 3d 20 69 50 61 67     *pPgno = iPag
1755a 65 3b 0a 20 20 20 20 20 20 20 20 20 20 54 52 41  e;.          TRA
1755b 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25  CE(("ALLOCATE: %
1755c 64 20 77 61 73 20 6c 65 61 66 20 25 64 20 6f 66  d was leaf %d of
1755d 20 25 64 20 6f 6e 20 74 72 75 6e 6b 20 25 64 22   %d on trunk %d"
1755e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1755f 20 20 22 3a 20 25 64 20 6d 6f 72 65 20 66 72 65    ": %d more fre
17560 65 20 70 61 67 65 73 5c 6e 22 2c 0a 20 20 20 20  e pages\n",.    
17561 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 50               *pP
17562 67 6e 6f 2c 20 63 6c 6f 73 65 73 74 2b 31 2c 20  gno, closest+1, 
17563 6b 2c 20 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f 2c  k, pTrunk->pgno,
17564 20 6e 2d 31 29 29 3b 0a 20 20 20 20 20 20 20 20   n-1));.        
17565 20 20 69 66 28 20 63 6c 6f 73 65 73 74 3c 6b 2d    if( closest<k-
17566 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  1 ){.           
17567 20 6d 65 6d 63 70 79 28 26 61 44 61 74 61 5b 38   memcpy(&aData[8
17568 2b 63 6c 6f 73 65 73 74 2a 34 5d 2c 20 26 61 44  +closest*4], &aD
17569 61 74 61 5b 34 2b 6b 2a 34 5d 2c 20 34 29 3b 0a  ata[4+k*4], 4);.
1756a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
1756b 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26        put4byte(&
1756c 61 44 61 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b 0a  aData[4], k-1);.
1756d 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
1756e 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  ( sqlite3PagerIs
1756f 77 72 69 74 65 61 62 6c 65 28 70 54 72 75 6e 6b  writeable(pTrunk
17570 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20  ->pDbPage) );.  
17571 20 20 20 20 20 20 20 20 6e 6f 43 6f 6e 74 65 6e          noConten
17572 74 20 3d 20 21 62 74 72 65 65 47 65 74 48 61 73  t = !btreeGetHas
17573 43 6f 6e 74 65 6e 74 28 70 42 74 2c 20 2a 70 50  Content(pBt, *pP
17574 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20  gno);.          
17575 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67  rc = btreeGetPag
17576 65 28 70 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 70  e(pBt, *pPgno, p
17577 70 50 61 67 65 2c 20 6e 6f 43 6f 6e 74 65 6e 74  pPage, noContent
17578 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
17579 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1757a 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63  {.            rc
1757b 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57   = sqlite3PagerW
1757c 72 69 74 65 28 28 2a 70 70 50 61 67 65 29 2d 3e  rite((*ppPage)->
1757d 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20  pDbPage);.      
1757e 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
1757f 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
17580 20 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65           release
17581 50 61 67 65 28 2a 70 70 50 61 67 65 29 3b 0a 20  Page(*ppPage);. 
17582 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
17583 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
17584 20 20 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20     searchList = 
17585 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  0;.        }.   
17586 20 20 20 7d 0a 20 20 20 20 20 20 72 65 6c 65 61     }.      relea
17587 73 65 50 61 67 65 28 70 50 72 65 76 54 72 75 6e  sePage(pPrevTrun
17588 6b 29 3b 0a 20 20 20 20 20 20 70 50 72 65 76 54  k);.      pPrevT
17589 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 7d 77  runk = 0;.    }w
1758a 68 69 6c 65 28 20 73 65 61 72 63 68 4c 69 73 74  hile( searchList
1758b 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   );.  }else{.   
1758c 20 2f 2a 20 54 68 65 72 65 20 61 72 65 20 6e 6f   /* There are no
1758d 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 66 72   pages on the fr
1758e 65 65 6c 69 73 74 2c 20 73 6f 20 63 72 65 61 74  eelist, so creat
1758f 65 20 61 20 6e 65 77 20 70 61 67 65 20 61 74 20  e a new page at 
17590 74 68 65 0a 20 20 20 20 2a 2a 20 65 6e 64 20 6f  the.    ** end o
17591 66 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20  f the file */.  
17592 20 20 69 6e 74 20 6e 50 61 67 65 20 3d 20 70 61    int nPage = pa
17593 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74  gerPagecount(pBt
17594 29 3b 0a 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20  );.    *pPgno = 
17595 6e 50 61 67 65 20 2b 20 31 3b 0a 0a 20 20 20 20  nPage + 1;..    
17596 69 66 28 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44  if( *pPgno==PEND
17597 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
17598 74 29 20 29 7b 0a 20 20 20 20 20 20 28 2a 70 50  t) ){.      (*pP
17599 67 6e 6f 29 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 23  gno)++;.    }..#
1759a 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1759b 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
1759c 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56    if( pBt->autoV
1759d 61 63 75 75 6d 20 26 26 20 50 54 52 4d 41 50 5f  acuum && PTRMAP_
1759e 49 53 50 41 47 45 28 70 42 74 2c 20 2a 70 50 67  ISPAGE(pBt, *pPg
1759f 6e 6f 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  no) ){.      /* 
175a0 49 66 20 2a 70 50 67 6e 6f 20 72 65 66 65 72 73  If *pPgno refers
175a1 20 74 6f 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61   to a pointer-ma
175a2 70 20 70 61 67 65 2c 20 61 6c 6c 6f 63 61 74 65  p page, allocate
175a3 20 74 77 6f 20 6e 65 77 20 70 61 67 65 73 0a 20   two new pages. 
175a4 20 20 20 20 20 2a 2a 20 61 74 20 74 68 65 20 65       ** at the e
175a5 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69  nd of the file i
175a6 6e 73 74 65 61 64 20 6f 66 20 6f 6e 65 2e 20 54  nstead of one. T
175a7 68 65 20 66 69 72 73 74 20 61 6c 6c 6f 63 61 74  he first allocat
175a8 65 64 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a  ed page.      **
175a9 20 62 65 63 6f 6d 65 73 20 61 20 6e 65 77 20 70   becomes a new p
175aa 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2c  ointer-map page,
175ab 20 74 68 65 20 73 65 63 6f 6e 64 20 69 73 20 75   the second is u
175ac 73 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65  sed by the calle
175ad 72 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  r..      */.    
175ae 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 67 20 3d    MemPage *pPg =
175af 20 30 3b 0a 20 20 20 20 20 20 54 52 41 43 45 28   0;.      TRACE(
175b0 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 66  ("ALLOCATE: %d f
175b1 72 6f 6d 20 65 6e 64 20 6f 66 20 66 69 6c 65 20  rom end of file 
175b2 28 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67  (pointer-map pag
175b3 65 29 5c 6e 22 2c 20 2a 70 50 67 6e 6f 29 29 3b  e)\n", *pPgno));
175b4 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 2a  .      assert( *
175b5 70 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42  pPgno!=PENDING_B
175b6 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b  YTE_PAGE(pBt) );
175b7 0a 20 20 20 20 20 20 72 63 20 3d 20 62 74 72 65  .      rc = btre
175b8 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 2a 70  eGetPage(pBt, *p
175b9 50 67 6e 6f 2c 20 26 70 50 67 2c 20 30 29 3b 0a  Pgno, &pPg, 0);.
175ba 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
175bb 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
175bc 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
175bd 61 67 65 72 57 72 69 74 65 28 70 50 67 2d 3e 70  agerWrite(pPg->p
175be 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  DbPage);.       
175bf 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 67   releasePage(pPg
175c0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
175c1 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
175c2 20 72 63 3b 0a 20 20 20 20 20 20 28 2a 70 50 67   rc;.      (*pPg
175c3 6e 6f 29 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28  no)++;.      if(
175c4 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e 47   *pPgno==PENDING
175c5 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20  _BYTE_PAGE(pBt) 
175c6 29 7b 20 28 2a 70 50 67 6e 6f 29 2b 2b 3b 20 7d  ){ (*pPgno)++; }
175c7 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  .    }.#endif.. 
175c8 20 20 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e     assert( *pPgn
175c9 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  o!=PENDING_BYTE_
175ca 50 41 47 45 28 70 42 74 29 20 29 3b 0a 20 20 20  PAGE(pBt) );.   
175cb 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61   rc = btreeGetPa
175cc 67 65 28 70 42 74 2c 20 2a 70 50 67 6e 6f 2c 20  ge(pBt, *pPgno, 
175cd 70 70 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 20  ppPage, 0);.    
175ce 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
175cf 72 63 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  rc;.    rc = sql
175d0 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 28  ite3PagerWrite((
175d1 2a 70 70 50 61 67 65 29 2d 3e 70 44 62 50 61 67  *ppPage)->pDbPag
175d2 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  e);.    if( rc!=
175d3 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
175d4 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a     releasePage(*
175d5 70 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20  ppPage);.    }. 
175d6 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43     TRACE(("ALLOC
175d7 41 54 45 3a 20 25 64 20 66 72 6f 6d 20 65 6e 64  ATE: %d from end
175d8 20 6f 66 20 66 69 6c 65 5c 6e 22 2c 20 2a 70 50   of file\n", *pP
175d9 67 6e 6f 29 29 3b 0a 20 20 7d 0a 0a 20 20 61 73  gno));.  }..  as
175da 73 65 72 74 28 20 2a 70 50 67 6e 6f 21 3d 50 45  sert( *pPgno!=PE
175db 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
175dc 70 42 74 29 20 29 3b 0a 0a 65 6e 64 5f 61 6c 6c  pBt) );..end_all
175dd 6f 63 61 74 65 5f 70 61 67 65 3a 0a 20 20 72 65  ocate_page:.  re
175de 6c 65 61 73 65 50 61 67 65 28 70 54 72 75 6e 6b  leasePage(pTrunk
175df 29 3b 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65  );.  releasePage
175e0 28 70 50 72 65 76 54 72 75 6e 6b 29 3b 0a 20 20  (pPrevTrunk);.  
175e1 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
175e2 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c  K ){.    if( sql
175e3 69 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66  ite3PagerPageRef
175e4 63 6f 75 6e 74 28 28 2a 70 70 50 61 67 65 29 2d  count((*ppPage)-
175e5 3e 70 44 62 50 61 67 65 29 3e 31 20 29 7b 0a 20  >pDbPage)>1 ){. 
175e6 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
175e7 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20 20  (*ppPage);.     
175e8 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
175e9 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
175ea 20 7d 0a 20 20 20 20 28 2a 70 70 50 61 67 65 29   }.    (*ppPage)
175eb 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20  ->isInit = 0;.  
175ec 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 50 61  }else{.    *ppPa
175ed 67 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65  ge = 0;.  }.  re
175ee 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
175ef 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
175f0 69 73 20 75 73 65 64 20 74 6f 20 61 64 64 20 70  is used to add p
175f1 61 67 65 20 69 50 61 67 65 20 74 6f 20 74 68 65  age iPage to the
175f2 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66   database file f
175f3 72 65 65 2d 6c 69 73 74 2e 20 0a 2a 2a 20 49 74  ree-list. .** It
175f4 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74   is assumed that
175f5 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74   the page is not
175f6 20 61 6c 72 65 61 64 79 20 61 20 70 61 72 74 20   already a part 
175f7 6f 66 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74  of the free-list
175f8 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75  ..**.** The valu
175f9 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  e passed as the 
175fa 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
175fb 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
175fc 20 69 73 20 6f 70 74 69 6f 6e 61 6c 2e 0a 2a 2a   is optional..**
175fd 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20 68   If the caller h
175fe 61 70 70 65 6e 73 20 74 6f 20 68 61 76 65 20 61  appens to have a
175ff 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
17600 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 74 20 0a  MemPage object .
17601 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  ** corresponding
17602 20 74 6f 20 70 61 67 65 20 69 50 61 67 65 20 68   to page iPage h
17603 61 6e 64 79 2c 20 69 74 20 6d 61 79 20 70 61 73  andy, it may pas
17604 73 20 69 74 20 61 73 20 74 68 65 20 73 65 63 6f  s it as the seco
17605 6e 64 20 76 61 6c 75 65 2e 20 0a 2a 2a 20 4f 74  nd value. .** Ot
17606 68 65 72 77 69 73 65 2c 20 69 74 20 6d 61 79 20  herwise, it may 
17607 70 61 73 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a  pass NULL..**.**
17608 20 49 66 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   If a pointer to
17609 20 61 20 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63   a MemPage objec
1760a 74 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74  t is passed as t
1760b 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
1760c 6e 74 2c 0a 2a 2a 20 69 74 73 20 72 65 66 65 72  nt,.** its refer
1760d 65 6e 63 65 20 63 6f 75 6e 74 20 69 73 20 6e 6f  ence count is no
1760e 74 20 61 6c 74 65 72 65 64 20 62 79 20 74 68 69  t altered by thi
1760f 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73  s function..*/.s
17610 74 61 74 69 63 20 69 6e 74 20 66 72 65 65 50 61  tatic int freePa
17611 67 65 32 28 42 74 53 68 61 72 65 64 20 2a 70 42  ge2(BtShared *pB
17612 74 2c 20 4d 65 6d 50 61 67 65 20 2a 70 4d 65 6d  t, MemPage *pMem
17613 50 61 67 65 2c 20 50 67 6e 6f 20 69 50 61 67 65  Page, Pgno iPage
17614 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54  ){.  MemPage *pT
17615 72 75 6e 6b 20 3d 20 30 3b 20 20 20 20 20 20 20  runk = 0;       
17616 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65           /* Free
17617 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65  -list trunk page
17618 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 54 72 75 6e   */.  Pgno iTrun
17619 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  k = 0;          
1761a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67            /* Pag
1761b 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 65  e number of free
1761c 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65  -list trunk page
1761d 20 2a 2f 20 0a 20 20 4d 65 6d 50 61 67 65 20 2a   */ .  MemPage *
1761e 70 50 61 67 65 31 20 3d 20 70 42 74 2d 3e 70 50  pPage1 = pBt->pP
1761f 61 67 65 31 3b 20 20 20 20 20 20 2f 2a 20 4c 6f  age1;      /* Lo
17620 63 61 6c 20 72 65 66 65 72 65 6e 63 65 20 74 6f  cal reference to
17621 20 70 61 67 65 20 31 20 2a 2f 0a 20 20 4d 65 6d   page 1 */.  Mem
17622 50 61 67 65 20 2a 70 50 61 67 65 3b 20 20 20 20  Page *pPage;    
17623 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17624 20 2f 2a 20 50 61 67 65 20 62 65 69 6e 67 20 66   /* Page being f
17625 72 65 65 64 2e 20 4d 61 79 20 62 65 20 4e 55 4c  reed. May be NUL
17626 4c 2e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20  L. */.  int rc; 
17627 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17628 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
17629 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20  eturn Code */.  
1762a 69 6e 74 20 6e 46 72 65 65 3b 20 20 20 20 20 20  int nFree;      
1762b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1762c 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 6e      /* Initial n
1762d 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f  umber of pages o
1762e 6e 20 66 72 65 65 2d 6c 69 73 74 20 2a 2f 0a 0a  n free-list */..
1762f 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
17630 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74  3_mutex_held(pBt
17631 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
17632 73 65 72 74 28 20 69 50 61 67 65 3e 31 20 29 3b  sert( iPage>1 );
17633 0a 20 20 61 73 73 65 72 74 28 20 21 70 4d 65 6d  .  assert( !pMem
17634 50 61 67 65 20 7c 7c 20 70 4d 65 6d 50 61 67 65  Page || pMemPage
17635 2d 3e 70 67 6e 6f 3d 3d 69 50 61 67 65 20 29 3b  ->pgno==iPage );
17636 0a 0a 20 20 69 66 28 20 70 4d 65 6d 50 61 67 65  ..  if( pMemPage
17637 20 29 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20   ){.    pPage = 
17638 70 4d 65 6d 50 61 67 65 3b 0a 20 20 20 20 73 71  pMemPage;.    sq
17639 6c 69 74 65 33 50 61 67 65 72 52 65 66 28 70 50  lite3PagerRef(pP
1763a 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  age->pDbPage);. 
1763b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 67   }else{.    pPag
1763c 65 20 3d 20 62 74 72 65 65 50 61 67 65 4c 6f 6f  e = btreePageLoo
1763d 6b 75 70 28 70 42 74 2c 20 69 50 61 67 65 29 3b  kup(pBt, iPage);
1763e 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 63 72 65  .  }..  /* Incre
1763f 6d 65 6e 74 20 74 68 65 20 66 72 65 65 20 70 61  ment the free pa
17640 67 65 20 63 6f 75 6e 74 20 6f 6e 20 70 50 61 67  ge count on pPag
17641 65 31 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c  e1 */.  rc = sql
17642 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
17643 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b  Page1->pDbPage);
17644 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f  .  if( rc ) goto
17645 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20   freepage_out;. 
17646 20 6e 46 72 65 65 20 3d 20 67 65 74 34 62 79 74   nFree = get4byt
17647 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61  e(&pPage1->aData
17648 5b 33 36 5d 29 3b 0a 20 20 70 75 74 34 62 79 74  [36]);.  put4byt
17649 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61  e(&pPage1->aData
1764a 5b 33 36 5d 2c 20 6e 46 72 65 65 2b 31 29 3b 0a  [36], nFree+1);.
1764b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53  .#ifdef SQLITE_S
1764c 45 43 55 52 45 5f 44 45 4c 45 54 45 0a 20 20 2f  ECURE_DELETE.  /
1764d 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f  * If the SQLITE_
1764e 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20 63 6f  SECURE_DELETE co
1764f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f  mpile-time optio
17650 6e 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68  n is enabled, th
17651 65 6e 0a 20 20 2a 2a 20 61 6c 77 61 79 73 20 66  en.  ** always f
17652 75 6c 6c 79 20 6f 76 65 72 77 72 69 74 65 20 64  ully overwrite d
17653 65 6c 65 74 65 64 20 69 6e 66 6f 72 6d 61 74 69  eleted informati
17654 6f 6e 20 77 69 74 68 20 7a 65 72 6f 73 2e 0a 20  on with zeros.. 
17655 20 2a 2f 0a 20 20 69 66 28 20 28 21 70 50 61 67   */.  if( (!pPag
17656 65 20 26 26 20 28 72 63 20 3d 20 62 74 72 65 65  e && (rc = btree
17657 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 50 61  GetPage(pBt, iPa
17658 67 65 2c 20 26 70 50 61 67 65 2c 20 30 29 29 29  ge, &pPage, 0)))
17659 0a 20 20 20 7c 7c 20 20 20 20 20 20 20 20 20 20  .   ||          
1765a 20 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50    (rc = sqlite3P
1765b 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d  agerWrite(pPage-
1765c 3e 70 44 62 50 61 67 65 29 29 0a 20 20 29 7b 0a  >pDbPage)).  ){.
1765d 20 20 20 20 67 6f 74 6f 20 66 72 65 65 70 61 67      goto freepag
1765e 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 6d 65 6d  e_out;.  }.  mem
1765f 73 65 74 28 70 50 61 67 65 2d 3e 61 44 61 74 61  set(pPage->aData
17660 2c 20 30 2c 20 70 50 61 67 65 2d 3e 70 42 74 2d  , 0, pPage->pBt-
17661 3e 70 61 67 65 53 69 7a 65 29 3b 0a 23 65 6e 64  >pageSize);.#end
17662 69 66 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  if..  /* If the 
17663 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 74  database support
17664 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 2c 20 77  s auto-vacuum, w
17665 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e  rite an entry in
17666 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70   the pointer-map
17667 0a 20 20 2a 2a 20 74 6f 20 69 6e 64 69 63 61 74  .  ** to indicat
17668 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20  e that the page 
17669 69 73 20 66 72 65 65 2e 0a 20 20 2a 2f 0a 20 20  is free..  */.  
1766a 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d  if( ISAUTOVACUUM
1766b 20 29 7b 0a 20 20 20 20 70 74 72 6d 61 70 50 75   ){.    ptrmapPu
1766c 74 28 70 42 74 2c 20 69 50 61 67 65 2c 20 50 54  t(pBt, iPage, PT
1766d 52 4d 41 50 5f 46 52 45 45 50 41 47 45 2c 20 30  RMAP_FREEPAGE, 0
1766e 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 66 28 20  , &rc);.    if( 
1766f 72 63 20 29 20 67 6f 74 6f 20 66 72 65 65 70 61  rc ) goto freepa
17670 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f  ge_out;.  }..  /
17671 2a 20 4e 6f 77 20 6d 61 6e 69 70 75 6c 61 74 65  * Now manipulate
17672 20 74 68 65 20 61 63 74 75 61 6c 20 64 61 74 61   the actual data
17673 62 61 73 65 20 66 72 65 65 2d 6c 69 73 74 20 73  base free-list s
17674 74 72 75 63 74 75 72 65 2e 20 54 68 65 72 65 20  tructure. There 
17675 61 72 65 20 74 77 6f 0a 20 20 2a 2a 20 70 6f 73  are two.  ** pos
17676 73 69 62 69 6c 69 74 69 65 73 2e 20 49 66 20 74  sibilities. If t
17677 68 65 20 66 72 65 65 2d 6c 69 73 74 20 69 73 20  he free-list is 
17678 63 75 72 72 65 6e 74 6c 79 20 65 6d 70 74 79 2c  currently empty,
17679 20 6f 72 20 69 66 20 74 68 65 20 66 69 72 73 74   or if the first
1767a 0a 20 20 2a 2a 20 74 72 75 6e 6b 20 70 61 67 65  .  ** trunk page
1767b 20 69 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73   in the free-lis
1767c 74 20 69 73 20 66 75 6c 6c 2c 20 74 68 65 6e 20  t is full, then 
1767d 74 68 69 73 20 70 61 67 65 20 77 69 6c 6c 20 62  this page will b
1767e 65 63 6f 6d 65 20 61 0a 20 20 2a 2a 20 6e 65 77  ecome a.  ** new
1767f 20 66 72 65 65 2d 6c 69 73 74 20 74 72 75 6e 6b   free-list trunk
17680 20 70 61 67 65 2e 20 4f 74 68 65 72 77 69 73 65   page. Otherwise
17681 2c 20 69 74 20 77 69 6c 6c 20 62 65 63 6f 6d 65  , it will become
17682 20 61 20 6c 65 61 66 20 6f 66 20 74 68 65 0a 20   a leaf of the. 
17683 20 2a 2a 20 66 69 72 73 74 20 74 72 75 6e 6b 20   ** first trunk 
17684 70 61 67 65 20 69 6e 20 74 68 65 20 63 75 72 72  page in the curr
17685 65 6e 74 20 66 72 65 65 2d 6c 69 73 74 2e 20 54  ent free-list. T
17686 68 69 73 20 62 6c 6f 63 6b 20 74 65 73 74 73 20  his block tests 
17687 69 66 20 69 74 0a 20 20 2a 2a 20 69 73 20 70 6f  if it.  ** is po
17688 73 73 69 62 6c 65 20 74 6f 20 61 64 64 20 74 68  ssible to add th
17689 65 20 70 61 67 65 20 61 73 20 61 20 6e 65 77 20  e page as a new 
1768a 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 2e 0a  free-list leaf..
1768b 20 20 2a 2f 0a 20 20 69 66 28 20 6e 46 72 65 65    */.  if( nFree
1768c 21 3d 30 20 29 7b 0a 20 20 20 20 75 33 32 20 6e  !=0 ){.    u32 n
1768d 4c 65 61 66 3b 20 20 20 20 20 20 20 20 20 20 20  Leaf;           
1768e 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20       /* Initial 
1768f 6e 75 6d 62 65 72 20 6f 66 20 6c 65 61 66 20 63  number of leaf c
17690 65 6c 6c 73 20 6f 6e 20 74 72 75 6e 6b 20 70 61  ells on trunk pa
17691 67 65 20 2a 2f 0a 0a 20 20 20 20 69 54 72 75 6e  ge */..    iTrun
17692 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50  k = get4byte(&pP
17693 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29  age1->aData[32])
17694 3b 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65 65  ;.    rc = btree
17695 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 54 72  GetPage(pBt, iTr
17696 75 6e 6b 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29  unk, &pTrunk, 0)
17697 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
17698 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
17699 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f   goto freepage_o
1769a 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e  ut;.    }..    n
1769b 4c 65 61 66 20 3d 20 67 65 74 34 62 79 74 65 28  Leaf = get4byte(
1769c 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34  &pTrunk->aData[4
1769d 5d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ]);.    assert( 
1769e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3e  pBt->usableSize>
1769f 33 32 20 29 3b 0a 20 20 20 20 69 66 28 20 6e 4c  32 );.    if( nL
176a0 65 61 66 20 3e 20 28 75 33 32 29 70 42 74 2d 3e  eaf > (u32)pBt->
176a1 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 32  usableSize/4 - 2
176a2 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   ){.      rc = S
176a3 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
176a4 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 66  PT;.      goto f
176a5 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20  reepage_out;.   
176a6 20 7d 0a 20 20 20 20 69 66 28 20 6e 4c 65 61 66   }.    if( nLeaf
176a7 20 3c 20 28 75 33 32 29 70 42 74 2d 3e 75 73 61   < (u32)pBt->usa
176a8 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 29 7b  bleSize/4 - 8 ){
176a9 0a 20 20 20 20 20 20 2f 2a 20 49 6e 20 74 68 69  .      /* In thi
176aa 73 20 63 61 73 65 20 74 68 65 72 65 20 69 73 20  s case there is 
176ab 72 6f 6f 6d 20 6f 6e 20 74 68 65 20 74 72 75 6e  room on the trun
176ac 6b 20 70 61 67 65 20 74 6f 20 69 6e 73 65 72 74  k page to insert
176ad 20 74 68 65 20 70 61 67 65 0a 20 20 20 20 20 20   the page.      
176ae 2a 2a 20 62 65 69 6e 67 20 66 72 65 65 64 20 61  ** being freed a
176af 73 20 61 20 6e 65 77 20 6c 65 61 66 2e 0a 20 20  s a new leaf..  
176b0 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
176b1 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 74 72  Note that the tr
176b2 75 6e 6b 20 70 61 67 65 20 69 73 20 6e 6f 74 20  unk page is not 
176b3 72 65 61 6c 6c 79 20 66 75 6c 6c 20 75 6e 74 69  really full unti
176b4 6c 20 69 74 20 63 6f 6e 74 61 69 6e 73 0a 20 20  l it contains.  
176b5 20 20 20 20 2a 2a 20 75 73 61 62 6c 65 53 69 7a      ** usableSiz
176b6 65 2f 34 20 2d 20 32 20 65 6e 74 72 69 65 73 2c  e/4 - 2 entries,
176b7 20 6e 6f 74 20 75 73 61 62 6c 65 53 69 7a 65 2f   not usableSize/
176b8 34 20 2d 20 38 20 65 6e 74 72 69 65 73 20 61 73  4 - 8 entries as
176b9 20 77 65 20 68 61 76 65 0a 20 20 20 20 20 20 2a   we have.      *
176ba 2a 20 63 6f 64 65 64 2e 20 20 42 75 74 20 64 75  * coded.  But du
176bb 65 20 74 6f 20 61 20 63 6f 64 69 6e 67 20 65 72  e to a coding er
176bc 72 6f 72 20 69 6e 20 76 65 72 73 69 6f 6e 73 20  ror in versions 
176bd 6f 66 20 53 51 4c 69 74 65 20 70 72 69 6f 72 20  of SQLite prior 
176be 74 6f 0a 20 20 20 20 20 20 2a 2a 20 33 2e 36 2e  to.      ** 3.6.
176bf 30 2c 20 64 61 74 61 62 61 73 65 73 20 77 69 74  0, databases wit
176c0 68 20 66 72 65 65 6c 69 73 74 20 74 72 75 6e 6b  h freelist trunk
176c1 20 70 61 67 65 73 20 68 6f 6c 64 69 6e 67 20 6d   pages holding m
176c2 6f 72 65 20 74 68 61 6e 0a 20 20 20 20 20 20 2a  ore than.      *
176c3 2a 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d  * usableSize/4 -
176c4 20 38 20 65 6e 74 72 69 65 73 20 77 69 6c 6c 20   8 entries will 
176c5 62 65 20 72 65 70 6f 72 74 65 64 20 61 73 20 63  be reported as c
176c6 6f 72 72 75 70 74 2e 20 20 49 6e 20 6f 72 64 65  orrupt.  In orde
176c7 72 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 6d 61  r.      ** to ma
176c8 69 6e 74 61 69 6e 20 62 61 63 6b 77 61 72 64 73  intain backwards
176c9 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 77   compatibility w
176ca 69 74 68 20 6f 6c 64 65 72 20 76 65 72 73 69 6f  ith older versio
176cb 6e 73 20 6f 66 20 53 51 4c 69 74 65 2c 0a 20 20  ns of SQLite,.  
176cc 20 20 20 20 2a 2a 20 77 65 20 77 69 6c 6c 20 63      ** we will c
176cd 6f 6e 74 69 6e 75 65 20 74 6f 20 72 65 73 74 72  ontinue to restr
176ce 69 63 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ict the number o
176cf 66 20 65 6e 74 72 69 65 73 20 74 6f 20 75 73 61  f entries to usa
176d0 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 0a 20 20  bleSize/4 - 8.  
176d1 20 20 20 20 2a 2a 20 66 6f 72 20 6e 6f 77 2e 20      ** for now. 
176d2 20 41 74 20 73 6f 6d 65 20 70 6f 69 6e 74 20 69   At some point i
176d3 6e 20 74 68 65 20 66 75 74 75 72 65 20 28 6f 6e  n the future (on
176d4 63 65 20 65 76 65 72 79 6f 6e 65 20 68 61 73 20  ce everyone has 
176d5 75 70 67 72 61 64 65 64 0a 20 20 20 20 20 20 2a  upgraded.      *
176d6 2a 20 74 6f 20 33 2e 36 2e 30 20 6f 72 20 6c 61  * to 3.6.0 or la
176d7 74 65 72 29 20 77 65 20 73 68 6f 75 6c 64 20 63  ter) we should c
176d8 6f 6e 73 69 64 65 72 20 66 69 78 69 6e 67 20 74  onsider fixing t
176d9 68 65 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 61  he conditional a
176da 62 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20 74 6f  bove.      ** to
176db 20 72 65 61 64 20 22 75 73 61 62 6c 65 53 69 7a   read "usableSiz
176dc 65 2f 34 2d 32 22 20 69 6e 73 74 65 61 64 20 6f  e/4-2" instead o
176dd 66 20 22 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d  f "usableSize/4-
176de 38 22 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  8"..      */.   
176df 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
176e0 61 67 65 72 57 72 69 74 65 28 70 54 72 75 6e 6b  agerWrite(pTrunk
176e1 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  ->pDbPage);.    
176e2 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
176e3 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70  _OK ){.        p
176e4 75 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d  ut4byte(&pTrunk-
176e5 3e 61 44 61 74 61 5b 34 5d 2c 20 6e 4c 65 61 66  >aData[4], nLeaf
176e6 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 70 75 74  +1);.        put
176e7 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61  4byte(&pTrunk->a
176e8 44 61 74 61 5b 38 2b 6e 4c 65 61 66 2a 34 5d 2c  Data[8+nLeaf*4],
176e9 20 69 50 61 67 65 29 3b 0a 23 69 66 6e 64 65 66   iPage);.#ifndef
176ea 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44   SQLITE_SECURE_D
176eb 45 4c 45 54 45 0a 20 20 20 20 20 20 20 20 69 66  ELETE.        if
176ec 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 20  ( pPage ){.     
176ed 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65       sqlite3Page
176ee 72 44 6f 6e 74 57 72 69 74 65 28 70 50 61 67 65  rDontWrite(pPage
176ef 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  ->pDbPage);.    
176f0 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20      }.#endif.   
176f1 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 53       rc = btreeS
176f2 65 74 48 61 73 43 6f 6e 74 65 6e 74 28 70 42 74  etHasContent(pBt
176f3 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20  , iPage);.      
176f4 7d 0a 20 20 20 20 20 20 54 52 41 43 45 28 28 22  }.      TRACE(("
176f5 46 52 45 45 2d 50 41 47 45 3a 20 25 64 20 6c 65  FREE-PAGE: %d le
176f6 61 66 20 6f 6e 20 74 72 75 6e 6b 20 70 61 67 65  af on trunk page
176f7 20 25 64 5c 6e 22 2c 70 50 61 67 65 2d 3e 70 67   %d\n",pPage->pg
176f8 6e 6f 2c 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f 29  no,pTrunk->pgno)
176f9 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 72  );.      goto fr
176fa 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20  eepage_out;.    
176fb 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 63  }.  }..  /* If c
176fc 6f 6e 74 72 6f 6c 20 66 6c 6f 77 73 20 74 6f 20  ontrol flows to 
176fd 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e  this point, then
176fe 20 69 74 20 77 61 73 20 6e 6f 74 20 70 6f 73 73   it was not poss
176ff 69 62 6c 65 20 74 6f 20 61 64 64 20 74 68 65 0a  ible to add the.
17700 20 20 2a 2a 20 74 68 65 20 70 61 67 65 20 62 65    ** the page be
17701 69 6e 67 20 66 72 65 65 64 20 61 73 20 61 20 6c  ing freed as a l
17702 65 61 66 20 70 61 67 65 20 6f 66 20 74 68 65 20  eaf page of the 
17703 66 69 72 73 74 20 74 72 75 6e 6b 20 69 6e 20 74  first trunk in t
17704 68 65 20 66 72 65 65 2d 6c 69 73 74 2e 0a 20 20  he free-list..  
17705 2a 2a 20 50 6f 73 73 69 62 6c 79 20 62 65 63 61  ** Possibly beca
17706 75 73 65 20 74 68 65 20 66 72 65 65 2d 6c 69 73  use the free-lis
17707 74 20 69 73 20 65 6d 70 74 79 2c 20 6f 72 20 70  t is empty, or p
17708 6f 73 73 69 62 6c 79 20 62 65 63 61 75 73 65 20  ossibly because 
17709 74 68 65 20 0a 20 20 2a 2a 20 66 69 72 73 74 20  the .  ** first 
1770a 74 72 75 6e 6b 20 69 6e 20 74 68 65 20 66 72 65  trunk in the fre
1770b 65 2d 6c 69 73 74 20 69 73 20 66 75 6c 6c 2e 20  e-list is full. 
1770c 45 69 74 68 65 72 20 77 61 79 2c 20 74 68 65 20  Either way, the 
1770d 70 61 67 65 20 62 65 69 6e 67 20 66 72 65 65 64  page being freed
1770e 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 65 63 6f 6d  .  ** will becom
1770f 65 20 74 68 65 20 6e 65 77 20 66 69 72 73 74 20  e the new first 
17710 74 72 75 6e 6b 20 70 61 67 65 20 69 6e 20 74 68  trunk page in th
17711 65 20 66 72 65 65 2d 6c 69 73 74 2e 0a 20 20 2a  e free-list..  *
17712 2f 0a 20 20 69 66 28 20 70 50 61 67 65 3d 3d 30  /.  if( pPage==0
17713 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28   && SQLITE_OK!=(
17714 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67  rc = btreeGetPag
17715 65 28 70 42 74 2c 20 69 50 61 67 65 2c 20 26 70  e(pBt, iPage, &p
17716 50 61 67 65 2c 20 30 29 29 20 29 7b 0a 20 20 20  Page, 0)) ){.   
17717 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f   goto freepage_o
17718 75 74 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73  ut;.  }.  rc = s
17719 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
1771a 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
1771b 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
1771c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74  TE_OK ){.    got
1771d 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a  o freepage_out;.
1771e 20 20 7d 0a 20 20 70 75 74 34 62 79 74 65 28 70    }.  put4byte(p
1771f 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 69 54 72  Page->aData, iTr
17720 75 6e 6b 29 3b 0a 20 20 70 75 74 34 62 79 74 65  unk);.  put4byte
17721 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 34  (&pPage->aData[4
17722 5d 2c 20 30 29 3b 0a 20 20 70 75 74 34 62 79 74  ], 0);.  put4byt
17723 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61  e(&pPage1->aData
17724 5b 33 32 5d 2c 20 69 50 61 67 65 29 3b 0a 20 20  [32], iPage);.  
17725 54 52 41 43 45 28 28 22 46 52 45 45 2d 50 41 47  TRACE(("FREE-PAG
17726 45 3a 20 25 64 20 6e 65 77 20 74 72 75 6e 6b 20  E: %d new trunk 
17727 70 61 67 65 20 72 65 70 6c 61 63 69 6e 67 20 25  page replacing %
17728 64 5c 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e  d\n", pPage->pgn
17729 6f 2c 20 69 54 72 75 6e 6b 29 29 3b 0a 0a 66 72  o, iTrunk));..fr
1772a 65 65 70 61 67 65 5f 6f 75 74 3a 0a 20 20 69 66  eepage_out:.  if
1772b 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 70  ( pPage ){.    p
1772c 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30  Page->isInit = 0
1772d 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 50  ;.  }.  releaseP
1772e 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 72 65  age(pPage);.  re
1772f 6c 65 61 73 65 50 61 67 65 28 70 54 72 75 6e 6b  leasePage(pTrunk
17730 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
17731 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 72  }.static void fr
17732 65 65 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a  eePage(MemPage *
17733 70 50 61 67 65 2c 20 69 6e 74 20 2a 70 52 43 29  pPage, int *pRC)
17734 7b 0a 20 20 69 66 28 20 28 2a 70 52 43 29 3d 3d  {.  if( (*pRC)==
17735 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
17736 20 2a 70 52 43 20 3d 20 66 72 65 65 50 61 67 65   *pRC = freePage
17737 32 28 70 50 61 67 65 2d 3e 70 42 74 2c 20 70 50  2(pPage->pBt, pP
17738 61 67 65 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f  age, pPage->pgno
17739 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
1773a 46 72 65 65 20 61 6e 79 20 6f 76 65 72 66 6c 6f  Free any overflo
1773b 77 20 70 61 67 65 73 20 61 73 73 6f 63 69 61 74  w pages associat
1773c 65 64 20 77 69 74 68 20 74 68 65 20 67 69 76 65  ed with the give
1773d 6e 20 43 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69  n Cell..*/.stati
1773e 63 20 69 6e 74 20 63 6c 65 61 72 43 65 6c 6c 28  c int clearCell(
1773f 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20  MemPage *pPage, 
17740 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
17741 43 65 6c 6c 29 7b 0a 20 20 42 74 53 68 61 72 65  Cell){.  BtShare
17742 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e  d *pBt = pPage->
17743 70 42 74 3b 0a 20 20 43 65 6c 6c 49 6e 66 6f 20  pBt;.  CellInfo 
17744 69 6e 66 6f 3b 0a 20 20 50 67 6e 6f 20 6f 76 66  info;.  Pgno ovf
17745 6c 50 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 3b  lPgno;.  int rc;
17746 0a 20 20 69 6e 74 20 6e 4f 76 66 6c 3b 0a 20 20  .  int nOvfl;.  
17747 75 31 36 20 6f 76 66 6c 50 61 67 65 53 69 7a 65  u16 ovflPageSize
17748 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
17749 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1774a 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65  pPage->pBt->mute
1774b 78 29 20 29 3b 0a 20 20 62 74 72 65 65 50 61 72  x) );.  btreePar
1774c 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c  seCellPtr(pPage,
1774d 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a   pCell, &info);.
1774e 20 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76 65 72    if( info.iOver
1774f 66 6c 6f 77 3d 3d 30 20 29 7b 0a 20 20 20 20 72  flow==0 ){.    r
17750 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
17751 20 20 2f 2a 20 4e 6f 20 6f 76 65 72 66 6c 6f 77    /* No overflow
17752 20 70 61 67 65 73 2e 20 52 65 74 75 72 6e 20 77   pages. Return w
17753 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79  ithout doing any
17754 74 68 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20 20 6f  thing */.  }.  o
17755 76 66 6c 50 67 6e 6f 20 3d 20 67 65 74 34 62 79  vflPgno = get4by
17756 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69  te(&pCell[info.i
17757 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 61 73  Overflow]);.  as
17758 73 65 72 74 28 20 70 42 74 2d 3e 75 73 61 62 6c  sert( pBt->usabl
17759 65 53 69 7a 65 20 3e 20 34 20 29 3b 0a 20 20 6f  eSize > 4 );.  o
1775a 76 66 6c 50 61 67 65 53 69 7a 65 20 3d 20 70 42  vflPageSize = pB
1775b 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20  t->usableSize - 
1775c 34 3b 0a 20 20 6e 4f 76 66 6c 20 3d 20 28 69 6e  4;.  nOvfl = (in
1775d 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 2d 20 69 6e  fo.nPayload - in
1775e 66 6f 2e 6e 4c 6f 63 61 6c 20 2b 20 6f 76 66 6c  fo.nLocal + ovfl
1775f 50 61 67 65 53 69 7a 65 20 2d 20 31 29 2f 6f 76  PageSize - 1)/ov
17760 66 6c 50 61 67 65 53 69 7a 65 3b 0a 20 20 61 73  flPageSize;.  as
17761 73 65 72 74 28 20 6f 76 66 6c 50 67 6e 6f 3d 3d  sert( ovflPgno==
17762 30 20 7c 7c 20 6e 4f 76 66 6c 3e 30 20 29 3b 0a  0 || nOvfl>0 );.
17763 20 20 77 68 69 6c 65 28 20 6e 4f 76 66 6c 2d 2d    while( nOvfl--
17764 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 69 4e 65   ){.    Pgno iNe
17765 78 74 20 3d 20 30 3b 0a 20 20 20 20 4d 65 6d 50  xt = 0;.    MemP
17766 61 67 65 20 2a 70 4f 76 66 6c 20 3d 20 30 3b 0a  age *pOvfl = 0;.
17767 20 20 20 20 69 66 28 20 6f 76 66 6c 50 67 6e 6f      if( ovflPgno
17768 3c 32 20 7c 7c 20 6f 76 66 6c 50 67 6e 6f 3e 70  <2 || ovflPgno>p
17769 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42  agerPagecount(pB
1776a 74 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 30  t) ){.      /* 0
1776b 20 69 73 20 6e 6f 74 20 61 20 6c 65 67 61 6c 20   is not a legal 
1776c 70 61 67 65 20 6e 75 6d 62 65 72 20 61 6e 64 20  page number and 
1776d 70 61 67 65 20 31 20 63 61 6e 6e 6f 74 20 62 65  page 1 cannot be
1776e 20 61 6e 20 0a 20 20 20 20 20 20 2a 2a 20 6f 76   an .      ** ov
1776f 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 54 68 65  erflow page. The
17770 72 65 66 6f 72 65 20 69 66 20 6f 76 66 6c 50 67  refore if ovflPg
17771 6e 6f 3c 32 20 6f 72 20 70 61 73 74 20 74 68 65  no<2 or past the
17772 20 65 6e 64 20 6f 66 20 74 68 65 20 0a 20 20 20   end of the .   
17773 20 20 20 2a 2a 20 66 69 6c 65 20 74 68 65 20 64     ** file the d
17774 61 74 61 62 61 73 65 20 6d 75 73 74 20 62 65 20  atabase must be 
17775 63 6f 72 72 75 70 74 2e 20 2a 2f 0a 20 20 20 20  corrupt. */.    
17776 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
17777 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
17778 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 4f 76 66    }.    if( nOvf
17779 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  l ){.      rc = 
1777a 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28  getOverflowPage(
1777b 70 42 74 2c 20 6f 76 66 6c 50 67 6e 6f 2c 20 26  pBt, ovflPgno, &
1777c 70 4f 76 66 6c 2c 20 26 69 4e 65 78 74 29 3b 0a  pOvfl, &iNext);.
1777d 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72        if( rc ) r
1777e 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
1777f 20 20 20 20 72 63 20 3d 20 66 72 65 65 50 61 67      rc = freePag
17780 65 32 28 70 42 74 2c 20 70 4f 76 66 6c 2c 20 6f  e2(pBt, pOvfl, o
17781 76 66 6c 50 67 6e 6f 29 3b 0a 20 20 20 20 69 66  vflPgno);.    if
17782 28 20 70 4f 76 66 6c 20 29 7b 0a 20 20 20 20 20  ( pOvfl ){.     
17783 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72   sqlite3PagerUnr
17784 65 66 28 70 4f 76 66 6c 2d 3e 70 44 62 50 61 67  ef(pOvfl->pDbPag
17785 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  e);.    }.    if
17786 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
17787 3b 0a 20 20 20 20 6f 76 66 6c 50 67 6e 6f 20 3d  ;.    ovflPgno =
17788 20 69 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65   iNext;.  }.  re
17789 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1778a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20  }../*.** Create 
1778b 74 68 65 20 62 79 74 65 20 73 65 71 75 65 6e 63  the byte sequenc
1778c 65 20 75 73 65 64 20 74 6f 20 72 65 70 72 65 73  e used to repres
1778d 65 6e 74 20 61 20 63 65 6c 6c 20 6f 6e 20 70 61  ent a cell on pa
1778e 67 65 20 70 50 61 67 65 0a 2a 2a 20 61 6e 64 20  ge pPage.** and 
1778f 77 72 69 74 65 20 74 68 61 74 20 62 79 74 65 20  write that byte 
17790 73 65 71 75 65 6e 63 65 20 69 6e 74 6f 20 70 43  sequence into pC
17791 65 6c 6c 5b 5d 2e 20 20 4f 76 65 72 66 6c 6f 77  ell[].  Overflow
17792 20 70 61 67 65 73 20 61 72 65 0a 2a 2a 20 61 6c   pages are.** al
17793 6c 6f 63 61 74 65 64 20 61 6e 64 20 66 69 6c 6c  located and fill
17794 65 64 20 69 6e 20 61 73 20 6e 65 63 65 73 73 61  ed in as necessa
17795 72 79 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67  ry.  The calling
17796 20 70 72 6f 63 65 64 75 72 65 0a 2a 2a 20 69 73   procedure.** is
17797 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72   responsible for
17798 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 73 75 66   making sure suf
17799 66 69 63 69 65 6e 74 20 73 70 61 63 65 20 68 61  ficient space ha
1779a 73 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64  s been allocated
1779b 0a 2a 2a 20 66 6f 72 20 70 43 65 6c 6c 5b 5d 2e  .** for pCell[].
1779c 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74  .**.** Note that
1779d 20 70 43 65 6c 6c 20 64 6f 65 73 20 6e 6f 74 20   pCell does not 
1779e 6e 65 63 65 73 73 61 72 79 20 6e 65 65 64 20 74  necessary need t
1779f 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 70  o point to the p
177a0 50 61 67 65 2d 3e 61 44 61 74 61 0a 2a 2a 20 61  Page->aData.** a
177a1 72 65 61 2e 20 20 70 43 65 6c 6c 20 6d 69 67 68  rea.  pCell migh
177a2 74 20 70 6f 69 6e 74 20 74 6f 20 73 6f 6d 65 20  t point to some 
177a3 74 65 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 67  temporary storag
177a4 65 2e 20 20 54 68 65 20 63 65 6c 6c 20 77 69 6c  e.  The cell wil
177a5 6c 0a 2a 2a 20 62 65 20 63 6f 6e 73 74 72 75 63  l.** be construc
177a6 74 65 64 20 69 6e 20 74 68 69 73 20 74 65 6d 70  ted in this temp
177a7 6f 72 61 72 79 20 61 72 65 61 20 74 68 65 6e 20  orary area then 
177a8 63 6f 70 69 65 64 20 69 6e 74 6f 20 70 50 61 67  copied into pPag
177a9 65 2d 3e 61 44 61 74 61 0a 2a 2a 20 6c 61 74 65  e->aData.** late
177aa 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  r..*/.static int
177ab 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 0a 20 20 4d   fillInCell(.  M
177ac 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20  emPage *pPage,  
177ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
177ae 20 54 68 65 20 70 61 67 65 20 74 68 61 74 20 63   The page that c
177af 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 65 6c 6c  ontains the cell
177b0 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   */.  unsigned c
177b1 68 61 72 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20  har *pCell,     
177b2 20 20 20 20 20 2f 2a 20 43 6f 6d 70 6c 65 74 65       /* Complete
177b3 20 74 65 78 74 20 6f 66 20 74 68 65 20 63 65 6c   text of the cel
177b4 6c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69  l */.  const voi
177b5 64 20 2a 70 4b 65 79 2c 20 69 36 34 20 6e 4b 65  d *pKey, i64 nKe
177b6 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79  y,    /* The key
177b7 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
177b8 20 2a 70 44 61 74 61 2c 69 6e 74 20 6e 44 61 74   *pData,int nDat
177b9 61 2c 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61  a,   /* The data
177ba 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65 72 6f 2c   */.  int nZero,
177bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
177bc 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 7a 65       /* Extra ze
177bd 72 6f 20 62 79 74 65 73 20 74 6f 20 61 70 70 65  ro bytes to appe
177be 6e 64 20 74 6f 20 70 44 61 74 61 20 2a 2f 0a 20  nd to pData */. 
177bf 20 69 6e 74 20 2a 70 6e 53 69 7a 65 20 20 20 20   int *pnSize    
177c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
177c1 2f 2a 20 57 72 69 74 65 20 63 65 6c 6c 20 73 69  /* Write cell si
177c2 7a 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20  ze here */.){.  
177c3 69 6e 74 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20  int nPayload;.  
177c4 63 6f 6e 73 74 20 75 38 20 2a 70 53 72 63 3b 0a  const u8 *pSrc;.
177c5 20 20 69 6e 74 20 6e 53 72 63 2c 20 6e 2c 20 72    int nSrc, n, r
177c6 63 3b 0a 20 20 69 6e 74 20 73 70 61 63 65 4c 65  c;.  int spaceLe
177c7 66 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  ft;.  MemPage *p
177c8 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 4d 65 6d 50  Ovfl = 0;.  MemP
177c9 61 67 65 20 2a 70 54 6f 52 65 6c 65 61 73 65 20  age *pToRelease 
177ca 3d 20 30 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20  = 0;.  unsigned 
177cb 63 68 61 72 20 2a 70 50 72 69 6f 72 3b 0a 20 20  char *pPrior;.  
177cc 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
177cd 50 61 79 6c 6f 61 64 3b 0a 20 20 42 74 53 68 61  Payload;.  BtSha
177ce 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65  red *pBt = pPage
177cf 2d 3e 70 42 74 3b 0a 20 20 50 67 6e 6f 20 70 67  ->pBt;.  Pgno pg
177d0 6e 6f 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 69 6e  noOvfl = 0;.  in
177d1 74 20 6e 48 65 61 64 65 72 3b 0a 20 20 43 65 6c  t nHeader;.  Cel
177d2 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20 61  lInfo info;..  a
177d3 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
177d4 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d  utex_held(pPage-
177d5 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  >pBt->mutex) );.
177d6 0a 20 20 2f 2a 20 70 50 61 67 65 20 69 73 20 6e  .  /* pPage is n
177d7 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 77  ot necessarily w
177d8 72 69 74 65 61 62 6c 65 20 73 69 6e 63 65 20 70  riteable since p
177d9 43 65 6c 6c 20 6d 69 67 68 74 20 62 65 20 61 75  Cell might be au
177da 78 69 6c 69 61 72 79 0a 20 20 2a 2a 20 62 75 66  xiliary.  ** buf
177db 66 65 72 20 73 70 61 63 65 20 74 68 61 74 20 69  fer space that i
177dc 73 20 73 65 70 61 72 61 74 65 20 66 72 6f 6d 20  s separate from 
177dd 74 68 65 20 70 50 61 67 65 20 62 75 66 66 65 72  the pPage buffer
177de 20 61 72 65 61 20 2a 2f 0a 20 20 61 73 73 65 72   area */.  asser
177df 74 28 20 70 43 65 6c 6c 3c 70 50 61 67 65 2d 3e  t( pCell<pPage->
177e0 61 44 61 74 61 20 7c 7c 20 70 43 65 6c 6c 3e 3d  aData || pCell>=
177e1 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42  &pPage->aData[pB
177e2 74 2d 3e 70 61 67 65 53 69 7a 65 5d 0a 20 20 20  t->pageSize].   
177e3 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69           || sqli
177e4 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61  te3PagerIswritea
177e5 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ble(pPage->pDbPa
177e6 67 65 29 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6c  ge) );..  /* Fil
177e7 6c 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 2e  l in the header.
177e8 20 2a 2f 0a 20 20 6e 48 65 61 64 65 72 20 3d 20   */.  nHeader = 
177e9 30 3b 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d  0;.  if( !pPage-
177ea 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 6e 48 65  >leaf ){.    nHe
177eb 61 64 65 72 20 2b 3d 20 34 3b 0a 20 20 7d 0a 20  ader += 4;.  }. 
177ec 20 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44   if( pPage->hasD
177ed 61 74 61 20 29 7b 0a 20 20 20 20 6e 48 65 61 64  ata ){.    nHead
177ee 65 72 20 2b 3d 20 70 75 74 56 61 72 69 6e 74 28  er += putVarint(
177ef 26 70 43 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 2c  &pCell[nHeader],
177f0 20 6e 44 61 74 61 2b 6e 5a 65 72 6f 29 3b 0a 20   nData+nZero);. 
177f1 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 44 61 74   }else{.    nDat
177f2 61 20 3d 20 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20  a = nZero = 0;. 
177f3 20 7d 0a 20 20 6e 48 65 61 64 65 72 20 2b 3d 20   }.  nHeader += 
177f4 70 75 74 56 61 72 69 6e 74 28 26 70 43 65 6c 6c  putVarint(&pCell
177f5 5b 6e 48 65 61 64 65 72 5d 2c 20 2a 28 75 36 34  [nHeader], *(u64
177f6 2a 29 26 6e 4b 65 79 29 3b 0a 20 20 62 74 72 65  *)&nKey);.  btre
177f7 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50  eParseCellPtr(pP
177f8 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66  age, pCell, &inf
177f9 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e  o);.  assert( in
177fa 66 6f 2e 6e 48 65 61 64 65 72 3d 3d 6e 48 65 61  fo.nHeader==nHea
177fb 64 65 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28  der );.  assert(
177fc 20 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 6e 4b 65 79   info.nKey==nKey
177fd 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e   );.  assert( in
177fe 66 6f 2e 6e 44 61 74 61 3d 3d 28 75 33 32 29 28  fo.nData==(u32)(
177ff 6e 44 61 74 61 2b 6e 5a 65 72 6f 29 20 29 3b 0a  nData+nZero) );.
17800 20 20 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20    .  /* Fill in 
17801 74 68 65 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20  the payload */. 
17802 20 6e 50 61 79 6c 6f 61 64 20 3d 20 6e 44 61 74   nPayload = nDat
17803 61 20 2b 20 6e 5a 65 72 6f 3b 0a 20 20 69 66 28  a + nZero;.  if(
17804 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29   pPage->intKey )
17805 7b 0a 20 20 20 20 70 53 72 63 20 3d 20 70 44 61  {.    pSrc = pDa
17806 74 61 3b 0a 20 20 20 20 6e 53 72 63 20 3d 20 6e  ta;.    nSrc = n
17807 44 61 74 61 3b 0a 20 20 20 20 6e 44 61 74 61 20  Data;.    nData 
17808 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 20 0a 20  = 0;.  }else{ . 
17809 20 20 20 69 66 28 20 4e 45 56 45 52 28 6e 4b 65     if( NEVER(nKe
1780a 79 3e 30 78 37 66 66 66 66 66 66 66 20 7c 7c 20  y>0x7fffffff || 
1780b 70 4b 65 79 3d 3d 30 29 20 29 7b 0a 20 20 20 20  pKey==0) ){.    
1780c 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1780d 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
1780e 20 20 7d 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64    }.    nPayload
1780f 20 2b 3d 20 28 69 6e 74 29 6e 4b 65 79 3b 0a 20   += (int)nKey;. 
17810 20 20 20 70 53 72 63 20 3d 20 70 4b 65 79 3b 0a     pSrc = pKey;.
17811 20 20 20 20 6e 53 72 63 20 3d 20 28 69 6e 74 29      nSrc = (int)
17812 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 2a 70 6e 53  nKey;.  }.  *pnS
17813 69 7a 65 20 3d 20 69 6e 66 6f 2e 6e 53 69 7a 65  ize = info.nSize
17814 3b 0a 20 20 73 70 61 63 65 4c 65 66 74 20 3d 20  ;.  spaceLeft = 
17815 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 70  info.nLocal;.  p
17816 50 61 79 6c 6f 61 64 20 3d 20 26 70 43 65 6c 6c  Payload = &pCell
17817 5b 6e 48 65 61 64 65 72 5d 3b 0a 20 20 70 50 72  [nHeader];.  pPr
17818 69 6f 72 20 3d 20 26 70 43 65 6c 6c 5b 69 6e 66  ior = &pCell[inf
17819 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 3b 0a 0a 20  o.iOverflow];.. 
1781a 20 77 68 69 6c 65 28 20 6e 50 61 79 6c 6f 61 64   while( nPayload
1781b 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 73 70  >0 ){.    if( sp
1781c 61 63 65 4c 65 66 74 3d 3d 30 20 29 7b 0a 23 69  aceLeft==0 ){.#i
1781d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1781e 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20  T_AUTOVACUUM.   
1781f 20 20 20 50 67 6e 6f 20 70 67 6e 6f 50 74 72 6d     Pgno pgnoPtrm
17820 61 70 20 3d 20 70 67 6e 6f 4f 76 66 6c 3b 20 2f  ap = pgnoOvfl; /
17821 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 20  * Overflow page 
17822 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72  pointer-map entr
17823 79 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 20  y page */.      
17824 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63  if( pBt->autoVac
17825 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 64  uum ){.        d
17826 6f 7b 0a 20 20 20 20 20 20 20 20 20 20 70 67 6e  o{.          pgn
17827 6f 4f 76 66 6c 2b 2b 3b 0a 20 20 20 20 20 20 20  oOvfl++;.       
17828 20 7d 20 77 68 69 6c 65 28 20 0a 20 20 20 20 20   } while( .     
17829 20 20 20 20 20 50 54 52 4d 41 50 5f 49 53 50 41       PTRMAP_ISPA
1782a 47 45 28 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c  GE(pBt, pgnoOvfl
1782b 29 20 7c 7c 20 70 67 6e 6f 4f 76 66 6c 3d 3d 50  ) || pgnoOvfl==P
1782c 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45  ENDING_BYTE_PAGE
1782d 28 70 42 74 29 20 0a 20 20 20 20 20 20 20 20 29  (pBt) .        )
1782e 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66  ;.      }.#endif
1782f 0a 20 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f  .      rc = allo
17830 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42  cateBtreePage(pB
17831 74 2c 20 26 70 4f 76 66 6c 2c 20 26 70 67 6e 6f  t, &pOvfl, &pgno
17832 4f 76 66 6c 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20  Ovfl, pgnoOvfl, 
17833 30 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  0);.#ifndef SQLI
17834 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
17835 55 4d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74  UM.      /* If t
17836 68 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70  he database supp
17837 6f 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d  orts auto-vacuum
17838 2c 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64  , and the second
17839 20 6f 72 20 73 75 62 73 65 71 75 65 6e 74 0a 20   or subsequent. 
1783a 20 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77       ** overflow
1783b 20 70 61 67 65 20 69 73 20 62 65 69 6e 67 20 61   page is being a
1783c 6c 6c 6f 63 61 74 65 64 2c 20 61 64 64 20 61 6e  llocated, add an
1783d 20 65 6e 74 72 79 20 74 6f 20 74 68 65 20 70 6f   entry to the po
1783e 69 6e 74 65 72 2d 6d 61 70 0a 20 20 20 20 20 20  inter-map.      
1783f 2a 2a 20 66 6f 72 20 74 68 61 74 20 70 61 67 65  ** for that page
17840 20 6e 6f 77 2e 20 0a 20 20 20 20 20 20 2a 2a 0a   now. .      **.
17841 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73        ** If this
17842 20 69 73 20 74 68 65 20 66 69 72 73 74 20 6f 76   is the first ov
17843 65 72 66 6c 6f 77 20 70 61 67 65 2c 20 74 68 65  erflow page, the
17844 6e 20 77 72 69 74 65 20 61 20 70 61 72 74 69 61  n write a partia
17845 6c 20 65 6e 74 72 79 20 0a 20 20 20 20 20 20 2a  l entry .      *
17846 2a 20 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72  * to the pointer
17847 2d 6d 61 70 2e 20 49 66 20 77 65 20 77 72 69 74  -map. If we writ
17848 65 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 69  e nothing to thi
17849 73 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 73 6c  s pointer-map sl
1784a 6f 74 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  ot,.      ** the
1784b 6e 20 74 68 65 20 6f 70 74 69 6d 69 73 74 69 63  n the optimistic
1784c 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 20   overflow chain 
1784d 70 72 6f 63 65 73 73 69 6e 67 20 69 6e 20 63 6c  processing in cl
1784e 65 61 72 43 65 6c 6c 28 29 0a 20 20 20 20 20 20  earCell().      
1784f 2a 2a 20 6d 61 79 20 6d 69 73 69 6e 74 65 72 70  ** may misinterp
17850 72 65 74 20 74 68 65 20 75 6e 69 6e 69 74 69 61  ret the uninitia
17851 6c 69 73 65 64 20 76 61 6c 75 65 73 20 61 6e 64  lised values and
17852 20 64 65 6c 65 74 65 20 74 68 65 0a 20 20 20 20   delete the.    
17853 20 20 2a 2a 20 77 72 6f 6e 67 20 70 61 67 65 73    ** wrong pages
17854 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61   from the databa
17855 73 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  se..      */.   
17856 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f     if( pBt->auto
17857 56 61 63 75 75 6d 20 26 26 20 72 63 3d 3d 53 51  Vacuum && rc==SQ
17858 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
17859 20 20 20 75 38 20 65 54 79 70 65 20 3d 20 28 70     u8 eType = (p
1785a 67 6e 6f 50 74 72 6d 61 70 3f 50 54 52 4d 41 50  gnoPtrmap?PTRMAP
1785b 5f 4f 56 45 52 46 4c 4f 57 32 3a 50 54 52 4d 41  _OVERFLOW2:PTRMA
1785c 50 5f 4f 56 45 52 46 4c 4f 57 31 29 3b 0a 20 20  P_OVERFLOW1);.  
1785d 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 28        ptrmapPut(
1785e 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 65  pBt, pgnoOvfl, e
1785f 54 79 70 65 2c 20 70 67 6e 6f 50 74 72 6d 61 70  Type, pgnoPtrmap
17860 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 20 20  , &rc);.        
17861 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
17862 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
17863 70 4f 76 66 6c 29 3b 0a 20 20 20 20 20 20 20 20  pOvfl);.        
17864 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66  }.      }.#endif
17865 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b  .      if( rc ){
17866 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65  .        release
17867 50 61 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29  Page(pToRelease)
17868 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
17869 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20   rc;.      }..  
1786a 20 20 20 20 2f 2a 20 49 66 20 70 54 6f 52 65 6c      /* If pToRel
1786b 65 61 73 65 20 69 73 20 6e 6f 74 20 7a 65 72 6f  ease is not zero
1786c 20 74 68 61 6e 20 70 50 72 69 6f 72 20 70 6f 69   than pPrior poi
1786d 6e 74 73 20 69 6e 74 6f 20 74 68 65 20 64 61 74  nts into the dat
1786e 61 20 61 72 65 61 0a 20 20 20 20 20 20 2a 2a 20  a area.      ** 
1786f 6f 66 20 70 54 6f 52 65 6c 65 61 73 65 2e 20 20  of pToRelease.  
17870 4d 61 6b 65 20 73 75 72 65 20 70 54 6f 52 65 6c  Make sure pToRel
17871 65 61 73 65 20 69 73 20 73 74 69 6c 6c 20 77 72  ease is still wr
17872 69 74 65 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20  iteable. */.    
17873 20 20 61 73 73 65 72 74 28 20 70 54 6f 52 65 6c    assert( pToRel
17874 65 61 73 65 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  ease==0 || sqlit
17875 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
17876 6c 65 28 70 54 6f 52 65 6c 65 61 73 65 2d 3e 70  le(pToRelease->p
17877 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 20  DbPage) );..    
17878 20 20 2f 2a 20 49 66 20 70 50 72 69 6f 72 20 69    /* If pPrior i
17879 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61  s part of the da
1787a 74 61 20 61 72 65 61 20 6f 66 20 70 50 61 67 65  ta area of pPage
1787b 2c 20 74 68 65 6e 20 6d 61 6b 65 20 73 75 72 65  , then make sure
1787c 20 70 50 61 67 65 0a 20 20 20 20 20 20 2a 2a 20   pPage.      ** 
1787d 69 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61 62  is still writeab
1787e 6c 65 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65  le */.      asse
1787f 72 74 28 20 70 50 72 69 6f 72 3c 70 50 61 67 65  rt( pPrior<pPage
17880 2d 3e 61 44 61 74 61 20 7c 7c 20 70 50 72 69 6f  ->aData || pPrio
17881 72 3e 3d 26 70 50 61 67 65 2d 3e 61 44 61 74 61  r>=&pPage->aData
17882 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d 0a  [pBt->pageSize].
17883 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73              || s
17884 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69  qlite3PagerIswri
17885 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44  teable(pPage->pD
17886 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 20  bPage) );..     
17887 20 70 75 74 34 62 79 74 65 28 70 50 72 69 6f 72   put4byte(pPrior
17888 2c 20 70 67 6e 6f 4f 76 66 6c 29 3b 0a 20 20 20  , pgnoOvfl);.   
17889 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
1788a 54 6f 52 65 6c 65 61 73 65 29 3b 0a 20 20 20 20  ToRelease);.    
1788b 20 20 70 54 6f 52 65 6c 65 61 73 65 20 3d 20 70    pToRelease = p
1788c 4f 76 66 6c 3b 0a 20 20 20 20 20 20 70 50 72 69  Ovfl;.      pPri
1788d 6f 72 20 3d 20 70 4f 76 66 6c 2d 3e 61 44 61 74  or = pOvfl->aDat
1788e 61 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74  a;.      put4byt
1788f 65 28 70 50 72 69 6f 72 2c 20 30 29 3b 0a 20 20  e(pPrior, 0);.  
17890 20 20 20 20 70 50 61 79 6c 6f 61 64 20 3d 20 26      pPayload = &
17891 70 4f 76 66 6c 2d 3e 61 44 61 74 61 5b 34 5d 3b  pOvfl->aData[4];
17892 0a 20 20 20 20 20 20 73 70 61 63 65 4c 65 66 74  .      spaceLeft
17893 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69   = pBt->usableSi
17894 7a 65 20 2d 20 34 3b 0a 20 20 20 20 7d 0a 20 20  ze - 4;.    }.  
17895 20 20 6e 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a    n = nPayload;.
17896 20 20 20 20 69 66 28 20 6e 3e 73 70 61 63 65 4c      if( n>spaceL
17897 65 66 74 20 29 20 6e 20 3d 20 73 70 61 63 65 4c  eft ) n = spaceL
17898 65 66 74 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20  eft;..    /* If 
17899 70 54 6f 52 65 6c 65 61 73 65 20 69 73 20 6e 6f  pToRelease is no
1789a 74 20 7a 65 72 6f 20 74 68 61 6e 20 70 50 61 79  t zero than pPay
1789b 6c 6f 61 64 20 70 6f 69 6e 74 73 20 69 6e 74 6f  load points into
1789c 20 74 68 65 20 64 61 74 61 20 61 72 65 61 0a 20   the data area. 
1789d 20 20 20 2a 2a 20 6f 66 20 70 54 6f 52 65 6c 65     ** of pToRele
1789e 61 73 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 20  ase.  Make sure 
1789f 70 54 6f 52 65 6c 65 61 73 65 20 69 73 20 73 74  pToRelease is st
178a0 69 6c 6c 20 77 72 69 74 65 61 62 6c 65 2e 20 2a  ill writeable. *
178a1 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54  /.    assert( pT
178a2 6f 52 65 6c 65 61 73 65 3d 3d 30 20 7c 7c 20 73  oRelease==0 || s
178a3 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69  qlite3PagerIswri
178a4 74 65 61 62 6c 65 28 70 54 6f 52 65 6c 65 61 73  teable(pToReleas
178a5 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a  e->pDbPage) );..
178a6 20 20 20 20 2f 2a 20 49 66 20 70 50 61 79 6c 6f      /* If pPaylo
178a7 61 64 20 69 73 20 70 61 72 74 20 6f 66 20 74 68  ad is part of th
178a8 65 20 64 61 74 61 20 61 72 65 61 20 6f 66 20 70  e data area of p
178a9 50 61 67 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20  Page, then make 
178aa 73 75 72 65 20 70 50 61 67 65 0a 20 20 20 20 2a  sure pPage.    *
178ab 2a 20 69 73 20 73 74 69 6c 6c 20 77 72 69 74 65  * is still write
178ac 61 62 6c 65 20 2a 2f 0a 20 20 20 20 61 73 73 65  able */.    asse
178ad 72 74 28 20 70 50 61 79 6c 6f 61 64 3c 70 50 61  rt( pPayload<pPa
178ae 67 65 2d 3e 61 44 61 74 61 20 7c 7c 20 70 50 61  ge->aData || pPa
178af 79 6c 6f 61 64 3e 3d 26 70 50 61 67 65 2d 3e 61  yload>=&pPage->a
178b0 44 61 74 61 5b 70 42 74 2d 3e 70 61 67 65 53 69  Data[pBt->pageSi
178b1 7a 65 5d 0a 20 20 20 20 20 20 20 20 20 20 20 20  ze].            
178b2 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  || sqlite3PagerI
178b3 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65  swriteable(pPage
178b4 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20  ->pDbPage) );.. 
178b5 20 20 20 69 66 28 20 6e 53 72 63 3e 30 20 29 7b     if( nSrc>0 ){
178b6 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 6e 53 72  .      if( n>nSr
178b7 63 20 29 20 6e 20 3d 20 6e 53 72 63 3b 0a 20 20  c ) n = nSrc;.  
178b8 20 20 20 20 61 73 73 65 72 74 28 20 70 53 72 63      assert( pSrc
178b9 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79   );.      memcpy
178ba 28 70 50 61 79 6c 6f 61 64 2c 20 70 53 72 63 2c  (pPayload, pSrc,
178bb 20 6e 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a   n);.    }else{.
178bc 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61        memset(pPa
178bd 79 6c 6f 61 64 2c 20 30 2c 20 6e 29 3b 0a 20 20  yload, 0, n);.  
178be 20 20 7d 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64    }.    nPayload
178bf 20 2d 3d 20 6e 3b 0a 20 20 20 20 70 50 61 79 6c   -= n;.    pPayl
178c0 6f 61 64 20 2b 3d 20 6e 3b 0a 20 20 20 20 70 53  oad += n;.    pS
178c1 72 63 20 2b 3d 20 6e 3b 0a 20 20 20 20 6e 53 72  rc += n;.    nSr
178c2 63 20 2d 3d 20 6e 3b 0a 20 20 20 20 73 70 61 63  c -= n;.    spac
178c3 65 4c 65 66 74 20 2d 3d 20 6e 3b 0a 20 20 20 20  eLeft -= n;.    
178c4 69 66 28 20 6e 53 72 63 3d 3d 30 20 29 7b 0a 20  if( nSrc==0 ){. 
178c5 20 20 20 20 20 6e 53 72 63 20 3d 20 6e 44 61 74       nSrc = nDat
178c6 61 3b 0a 20 20 20 20 20 20 70 53 72 63 20 3d 20  a;.      pSrc = 
178c7 70 44 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d  pData;.    }.  }
178c8 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70  .  releasePage(p
178c9 54 6f 52 65 6c 65 61 73 65 29 3b 0a 20 20 72 65  ToRelease);.  re
178ca 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
178cb 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20  }../*.** Remove 
178cc 74 68 65 20 69 2d 74 68 20 63 65 6c 6c 20 66 72  the i-th cell fr
178cd 6f 6d 20 70 50 61 67 65 2e 20 20 54 68 69 73 20  om pPage.  This 
178ce 72 6f 75 74 69 6e 65 20 65 66 66 65 63 74 73 20  routine effects 
178cf 70 50 61 67 65 20 6f 6e 6c 79 2e 0a 2a 2a 20 54  pPage only..** T
178d0 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20  he cell content 
178d1 69 73 20 6e 6f 74 20 66 72 65 65 64 20 6f 72 20  is not freed or 
178d2 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20 20 49 74  deallocated.  It
178d3 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74   is assumed that
178d4 0a 2a 2a 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e  .** the cell con
178d5 74 65 6e 74 20 68 61 73 20 62 65 65 6e 20 63 6f  tent has been co
178d6 70 69 65 64 20 73 6f 6d 65 70 6c 61 63 65 20 65  pied someplace e
178d7 6c 73 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69  lse.  This routi
178d8 6e 65 20 6a 75 73 74 0a 2a 2a 20 72 65 6d 6f 76  ne just.** remov
178d9 65 73 20 74 68 65 20 72 65 66 65 72 65 6e 63 65  es the reference
178da 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 66 72 6f   to the cell fro
178db 6d 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 22  m pPage..**.** "
178dc 73 7a 22 20 6d 75 73 74 20 62 65 20 74 68 65 20  sz" must be the 
178dd 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
178de 69 6e 20 74 68 65 20 63 65 6c 6c 2e 0a 2a 2f 0a  in the cell..*/.
178df 73 74 61 74 69 63 20 76 6f 69 64 20 64 72 6f 70  static void drop
178e0 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50  Cell(MemPage *pP
178e1 61 67 65 2c 20 69 6e 74 20 69 64 78 2c 20 69 6e  age, int idx, in
178e2 74 20 73 7a 2c 20 69 6e 74 20 2a 70 52 43 29 7b  t sz, int *pRC){
178e3 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
178e4 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
178e5 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20  er */.  int pc; 
178e6 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65          /* Offse
178e7 74 20 74 6f 20 63 65 6c 6c 20 63 6f 6e 74 65 6e  t to cell conten
178e8 74 20 6f 66 20 63 65 6c 6c 20 62 65 69 6e 67 20  t of cell being 
178e9 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 75 38 20  deleted */.  u8 
178ea 2a 64 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20  *data;       /* 
178eb 70 50 61 67 65 2d 3e 61 44 61 74 61 20 2a 2f 0a  pPage->aData */.
178ec 20 20 75 38 20 2a 70 74 72 3b 20 20 20 20 20 20    u8 *ptr;      
178ed 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6d 6f 76    /* Used to mov
178ee 65 20 62 79 74 65 73 20 61 72 6f 75 6e 64 20 77  e bytes around w
178ef 69 74 68 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a  ithin data[] */.
178f0 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
178f1 20 20 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 20    /* The return 
178f2 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 68 64  code */.  int hd
178f3 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 65 67  r;        /* Beg
178f4 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 68 65  inning of the he
178f5 61 64 65 72 2e 20 20 30 20 6d 6f 73 74 20 70 61  ader.  0 most pa
178f6 67 65 73 2e 20 20 31 30 30 20 70 61 67 65 20 31  ges.  100 page 1
178f7 20 2a 2f 0a 0a 20 20 69 66 28 20 2a 70 52 43 20   */..  if( *pRC 
178f8 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73  ) return;..  ass
178f9 65 72 74 28 20 69 64 78 3e 3d 30 20 26 26 20 69  ert( idx>=0 && i
178fa 64 78 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20  dx<pPage->nCell 
178fb 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a 3d  );.  assert( sz=
178fc 3d 63 65 6c 6c 53 69 7a 65 28 70 50 61 67 65 2c  =cellSize(pPage,
178fd 20 69 64 78 29 20 29 3b 0a 20 20 61 73 73 65 72   idx) );.  asser
178fe 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  t( sqlite3PagerI
178ff 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65  swriteable(pPage
17900 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20  ->pDbPage) );.  
17901 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
17902 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65  mutex_held(pPage
17903 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  ->pBt->mutex) );
17904 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d  .  data = pPage-
17905 3e 61 44 61 74 61 3b 0a 20 20 70 74 72 20 3d 20  >aData;.  ptr = 
17906 26 64 61 74 61 5b 70 50 61 67 65 2d 3e 63 65 6c  &data[pPage->cel
17907 6c 4f 66 66 73 65 74 20 2b 20 32 2a 69 64 78 5d  lOffset + 2*idx]
17908 3b 0a 20 20 70 63 20 3d 20 67 65 74 32 62 79 74  ;.  pc = get2byt
17909 65 28 70 74 72 29 3b 0a 20 20 68 64 72 20 3d 20  e(ptr);.  hdr = 
1790a 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74  pPage->hdrOffset
1790b 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 63  ;.  testcase( pc
1790c 3d 3d 67 65 74 32 62 79 74 65 28 26 64 61 74 61  ==get2byte(&data
1790d 5b 68 64 72 2b 35 5d 29 20 29 3b 0a 20 20 74 65  [hdr+5]) );.  te
1790e 73 74 63 61 73 65 28 20 70 63 2b 73 7a 3d 3d 70  stcase( pc+sz==p
1790f 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c  Page->pBt->usabl
17910 65 53 69 7a 65 20 29 3b 0a 20 20 69 66 28 20 70  eSize );.  if( p
17911 63 20 3c 20 67 65 74 32 62 79 74 65 28 26 64 61  c < get2byte(&da
17912 74 61 5b 68 64 72 2b 35 5d 29 20 7c 7c 20 70 63  ta[hdr+5]) || pc
17913 2b 73 7a 20 3e 20 70 50 61 67 65 2d 3e 70 42 74  +sz > pPage->pBt
17914 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a  ->usableSize ){.
17915 20 20 20 20 2a 70 52 43 20 3d 20 53 51 4c 49 54      *pRC = SQLIT
17916 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
17917 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
17918 20 20 72 63 20 3d 20 66 72 65 65 53 70 61 63 65    rc = freeSpace
17919 28 70 50 61 67 65 2c 20 70 63 2c 20 73 7a 29 3b  (pPage, pc, sz);
1791a 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20  .  if( rc ){.   
1791b 20 2a 70 52 43 20 3d 20 72 63 3b 0a 20 20 20 20   *pRC = rc;.    
1791c 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f  return;.  }.  fo
1791d 72 28 69 3d 69 64 78 2b 31 3b 20 69 3c 70 50 61  r(i=idx+1; i<pPa
1791e 67 65 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 2c 20  ge->nCell; i++, 
1791f 70 74 72 2b 3d 32 29 7b 0a 20 20 20 20 70 74 72  ptr+=2){.    ptr
17920 5b 30 5d 20 3d 20 70 74 72 5b 32 5d 3b 0a 20 20  [0] = ptr[2];.  
17921 20 20 70 74 72 5b 31 5d 20 3d 20 70 74 72 5b 33    ptr[1] = ptr[3
17922 5d 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 2d 3e  ];.  }.  pPage->
17923 6e 43 65 6c 6c 2d 2d 3b 0a 20 20 70 75 74 32 62  nCell--;.  put2b
17924 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d  yte(&data[hdr+3]
17925 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 3b  , pPage->nCell);
17926 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20  .  pPage->nFree 
17927 2b 3d 20 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  += 2;.}../*.** I
17928 6e 73 65 72 74 20 61 20 6e 65 77 20 63 65 6c 6c  nsert a new cell
17929 20 6f 6e 20 70 50 61 67 65 20 61 74 20 63 65 6c   on pPage at cel
1792a 6c 20 69 6e 64 65 78 20 22 69 22 2e 20 20 70 43  l index "i".  pC
1792b 65 6c 6c 20 70 6f 69 6e 74 73 20 74 6f 20 74 68  ell points to th
1792c 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20  e.** content of 
1792d 74 68 65 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20  the cell..**.** 
1792e 49 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  If the cell cont
1792f 65 6e 74 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20  ent will fit on 
17930 74 68 65 20 70 61 67 65 2c 20 74 68 65 6e 20 70  the page, then p
17931 75 74 20 69 74 20 74 68 65 72 65 2e 20 20 49 66  ut it there.  If
17932 20 69 74 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20   it.** will not 
17933 66 69 74 2c 20 74 68 65 6e 20 6d 61 6b 65 20 61  fit, then make a
17934 20 63 6f 70 79 20 6f 66 20 74 68 65 20 63 65 6c   copy of the cel
17935 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 70  l content into p
17936 54 65 6d 70 20 69 66 0a 2a 2a 20 70 54 65 6d 70  Temp if.** pTemp
17937 20 69 73 20 6e 6f 74 20 6e 75 6c 6c 2e 20 20 52   is not null.  R
17938 65 67 61 72 64 6c 65 73 73 20 6f 66 20 70 54 65  egardless of pTe
17939 6d 70 2c 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e  mp, allocate a n
1793a 65 77 20 65 6e 74 72 79 0a 2a 2a 20 69 6e 20 70  ew entry.** in p
1793b 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 5d 20 61 6e  Page->aOvfl[] an
1793c 64 20 6d 61 6b 65 20 69 74 20 70 6f 69 6e 74 20  d make it point 
1793d 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  to the cell cont
1793e 65 6e 74 20 28 65 69 74 68 65 72 0a 2a 2a 20 69  ent (either.** i
1793f 6e 20 70 54 65 6d 70 20 6f 72 20 74 68 65 20 6f  n pTemp or the o
17940 72 69 67 69 6e 61 6c 20 70 43 65 6c 6c 29 20 61  riginal pCell) a
17941 6e 64 20 61 6c 73 6f 20 72 65 63 6f 72 64 20 69  nd also record i
17942 74 73 20 69 6e 64 65 78 2e 20 0a 2a 2a 20 41 6c  ts index. .** Al
17943 6c 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20 65  locating a new e
17944 6e 74 72 79 20 69 6e 20 70 50 61 67 65 2d 3e 61  ntry in pPage->a
17945 43 65 6c 6c 5b 5d 20 69 6d 70 6c 69 65 73 20 74  Cell[] implies t
17946 68 61 74 20 0a 2a 2a 20 70 50 61 67 65 2d 3e 6e  hat .** pPage->n
17947 4f 76 65 72 66 6c 6f 77 20 69 73 20 69 6e 63 72  Overflow is incr
17948 65 6d 65 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  emented..**.** I
17949 66 20 6e 53 6b 69 70 20 69 73 20 6e 6f 6e 2d 7a  f nSkip is non-z
1794a 65 72 6f 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74  ero, then do not
1794b 20 63 6f 70 79 20 74 68 65 20 66 69 72 73 74 20   copy the first 
1794c 6e 53 6b 69 70 20 62 79 74 65 73 20 6f 66 20 74  nSkip bytes of t
1794d 68 65 0a 2a 2a 20 63 65 6c 6c 2e 20 54 68 65 20  he.** cell. The 
1794e 63 61 6c 6c 65 72 20 77 69 6c 6c 20 6f 76 65 72  caller will over
1794f 77 72 69 74 65 20 74 68 65 6d 20 61 66 74 65 72  write them after
17950 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   this function r
17951 65 74 75 72 6e 73 2e 20 49 66 0a 2a 2a 20 6e 53  eturns. If.** nS
17952 6b 69 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c  kip is non-zero,
17953 20 74 68 65 6e 20 70 43 65 6c 6c 20 6d 61 79 20   then pCell may 
17954 6e 6f 74 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20  not point to an 
17955 69 6e 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20 6c  invalid memory l
17956 6f 63 61 74 69 6f 6e 20 0a 2a 2a 20 28 62 75 74  ocation .** (but
17957 20 70 43 65 6c 6c 2b 6e 53 6b 69 70 20 69 73 20   pCell+nSkip is 
17958 61 6c 77 61 79 73 20 76 61 6c 69 64 29 2e 0a 2a  always valid)..*
17959 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e  /.static void in
1795a 73 65 72 74 43 65 6c 6c 28 0a 20 20 4d 65 6d 50  sertCell(.  MemP
1795b 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 2f 2a  age *pPage,   /*
1795c 20 50 61 67 65 20 69 6e 74 6f 20 77 68 69 63 68   Page into which
1795d 20 77 65 20 61 72 65 20 63 6f 70 79 69 6e 67 20   we are copying 
1795e 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 20 20 20 20  */.  int i,     
1795f 20 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 63 65         /* New ce
17960 6c 6c 20 62 65 63 6f 6d 65 73 20 74 68 65 20 69  ll becomes the i
17961 2d 74 68 20 63 65 6c 6c 20 6f 66 20 74 68 65 20  -th cell of the 
17962 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70 43  page */.  u8 *pC
17963 65 6c 6c 2c 20 20 20 20 20 20 20 20 2f 2a 20 43  ell,        /* C
17964 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 6e 65  ontent of the ne
17965 77 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20  w cell */.  int 
17966 73 7a 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  sz,           /*
17967 20 42 79 74 65 73 20 6f 66 20 63 6f 6e 74 65 6e   Bytes of conten
17968 74 20 69 6e 20 70 43 65 6c 6c 20 2a 2f 0a 20 20  t in pCell */.  
17969 75 38 20 2a 70 54 65 6d 70 2c 20 20 20 20 20 20  u8 *pTemp,      
1796a 20 20 2f 2a 20 54 65 6d 70 20 73 74 6f 72 61 67    /* Temp storag
1796b 65 20 73 70 61 63 65 20 66 6f 72 20 70 43 65 6c  e space for pCel
1796c 6c 2c 20 69 66 20 6e 65 65 64 65 64 20 2a 2f 0a  l, if needed */.
1796d 20 20 50 67 6e 6f 20 69 43 68 69 6c 64 2c 20 20    Pgno iChild,  
1796e 20 20 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65      /* If non-ze
1796f 72 6f 2c 20 72 65 70 6c 61 63 65 20 66 69 72 73  ro, replace firs
17970 74 20 34 20 62 79 74 65 73 20 77 69 74 68 20 74  t 4 bytes with t
17971 68 69 73 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69  his value */.  i
17972 6e 74 20 2a 70 52 43 20 20 20 20 20 20 20 20 20  nt *pRC         
17973 20 2f 2a 20 52 65 61 64 20 61 6e 64 20 77 72 69   /* Read and wri
17974 74 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 66  te return code f
17975 72 6f 6d 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  rom here */.){. 
17976 20 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20 20   int idx;       
17977 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 77     /* Where to w
17978 72 69 74 65 20 6e 65 77 20 63 65 6c 6c 20 63 6f  rite new cell co
17979 6e 74 65 6e 74 20 69 6e 20 64 61 74 61 5b 5d 20  ntent in data[] 
1797a 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20 20 20 20 20  */.  int j;     
1797b 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
1797c 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  ounter */.  int 
1797d 65 6e 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  end;          /*
1797e 20 46 69 72 73 74 20 62 79 74 65 20 70 61 73 74   First byte past
1797f 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c 20 70   the last cell p
17980 6f 69 6e 74 65 72 20 69 6e 20 64 61 74 61 5b 5d  ointer in data[]
17981 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 73 3b 20 20   */.  int ins;  
17982 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
17983 20 69 6e 20 64 61 74 61 5b 5d 20 77 68 65 72 65   in data[] where
17984 20 6e 65 77 20 63 65 6c 6c 20 70 6f 69 6e 74 65   new cell pointe
17985 72 20 69 73 20 69 6e 73 65 72 74 65 64 20 2a 2f  r is inserted */
17986 0a 20 20 69 6e 74 20 63 65 6c 6c 4f 66 66 73 65  .  int cellOffse
17987 74 3b 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20  t;   /* Address 
17988 6f 66 20 66 69 72 73 74 20 63 65 6c 6c 20 70 6f  of first cell po
17989 69 6e 74 65 72 20 69 6e 20 64 61 74 61 5b 5d 20  inter in data[] 
1798a 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 20  */.  u8 *data;  
1798b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f         /* The co
1798c 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 77 68 6f  ntent of the who
1798d 6c 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20  le page */.  u8 
1798e 2a 70 74 72 3b 20 20 20 20 20 20 20 20 20 20 2f  *ptr;          /
1798f 2a 20 55 73 65 64 20 66 6f 72 20 6d 6f 76 69 6e  * Used for movin
17990 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 72  g information ar
17991 6f 75 6e 64 20 69 6e 20 64 61 74 61 5b 5d 20 2a  ound in data[] *
17992 2f 0a 0a 20 20 69 6e 74 20 6e 53 6b 69 70 20 3d  /..  int nSkip =
17993 20 28 69 43 68 69 6c 64 20 3f 20 34 20 3a 20 30   (iChild ? 4 : 0
17994 29 3b 0a 0a 20 20 69 66 28 20 2a 70 52 43 20 29  );..  if( *pRC )
17995 20 72 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65   return;..  asse
17996 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 3d 70  rt( i>=0 && i<=p
17997 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b 70 50 61 67  Page->nCell+pPag
17998 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 3b 0a  e->nOverflow );.
17999 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
1799a 3e 6e 43 65 6c 6c 3c 3d 4d 58 5f 43 45 4c 4c 28  >nCell<=MX_CELL(
1799b 70 50 61 67 65 2d 3e 70 42 74 29 20 26 26 20 4d  pPage->pBt) && M
1799c 58 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e 70 42  X_CELL(pPage->pB
1799d 74 29 3c 3d 35 34 36 30 20 29 3b 0a 20 20 61 73  t)<=5460 );.  as
1799e 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76  sert( pPage->nOv
1799f 65 72 66 6c 6f 77 3c 3d 41 72 72 61 79 53 69 7a  erflow<=ArraySiz
179a0 65 28 70 50 61 67 65 2d 3e 61 4f 76 66 6c 29 20  e(pPage->aOvfl) 
179a1 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a 3d  );.  assert( sz=
179a2 3d 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61  =cellSizePtr(pPa
179a3 67 65 2c 20 70 43 65 6c 6c 29 20 29 3b 0a 20 20  ge, pCell) );.  
179a4 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
179a5 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65  mutex_held(pPage
179a6 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  ->pBt->mutex) );
179a7 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 4f  .  if( pPage->nO
179a8 76 65 72 66 6c 6f 77 20 7c 7c 20 73 7a 2b 32 3e  verflow || sz+2>
179a9 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 29 7b 0a  pPage->nFree ){.
179aa 20 20 20 20 69 66 28 20 70 54 65 6d 70 20 29 7b      if( pTemp ){
179ab 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 54  .      memcpy(pT
179ac 65 6d 70 2b 6e 53 6b 69 70 2c 20 70 43 65 6c 6c  emp+nSkip, pCell
179ad 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e 53 6b 69 70  +nSkip, sz-nSkip
179ae 29 3b 0a 20 20 20 20 20 20 70 43 65 6c 6c 20 3d  );.      pCell =
179af 20 70 54 65 6d 70 3b 0a 20 20 20 20 7d 0a 20 20   pTemp;.    }.  
179b0 20 20 69 66 28 20 69 43 68 69 6c 64 20 29 7b 0a    if( iChild ){.
179b1 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 70        put4byte(p
179b2 43 65 6c 6c 2c 20 69 43 68 69 6c 64 29 3b 0a 20  Cell, iChild);. 
179b3 20 20 20 7d 0a 20 20 20 20 6a 20 3d 20 70 50 61     }.    j = pPa
179b4 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2b 2b 3b  ge->nOverflow++;
179b5 0a 20 20 20 20 61 73 73 65 72 74 28 20 6a 3c 28  .    assert( j<(
179b6 69 6e 74 29 28 73 69 7a 65 6f 66 28 70 50 61 67  int)(sizeof(pPag
179b7 65 2d 3e 61 4f 76 66 6c 29 2f 73 69 7a 65 6f 66  e->aOvfl)/sizeof
179b8 28 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 30 5d  (pPage->aOvfl[0]
179b9 29 29 20 29 3b 0a 20 20 20 20 70 50 61 67 65 2d  )) );.    pPage-
179ba 3e 61 4f 76 66 6c 5b 6a 5d 2e 70 43 65 6c 6c 20  >aOvfl[j].pCell 
179bb 3d 20 70 43 65 6c 6c 3b 0a 20 20 20 20 70 50 61  = pCell;.    pPa
179bc 67 65 2d 3e 61 4f 76 66 6c 5b 6a 5d 2e 69 64 78  ge->aOvfl[j].idx
179bd 20 3d 20 28 75 31 36 29 69 3b 0a 20 20 7d 65 6c   = (u16)i;.  }el
179be 73 65 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d  se{.    int rc =
179bf 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
179c0 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  te(pPage->pDbPag
179c1 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  e);.    if( rc!=
179c2 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
179c3 20 20 20 2a 70 52 43 20 3d 20 72 63 3b 0a 20 20     *pRC = rc;.  
179c4 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
179c5 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71  }.    assert( sq
179c6 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74  lite3PagerIswrit
179c7 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62  eable(pPage->pDb
179c8 50 61 67 65 29 20 29 3b 0a 20 20 20 20 64 61 74  Page) );.    dat
179c9 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61  a = pPage->aData
179ca 3b 0a 20 20 20 20 63 65 6c 6c 4f 66 66 73 65 74  ;.    cellOffset
179cb 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66   = pPage->cellOf
179cc 66 73 65 74 3b 0a 20 20 20 20 65 6e 64 20 3d 20  fset;.    end = 
179cd 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70  cellOffset + 2*p
179ce 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20  Page->nCell;.   
179cf 20 69 6e 73 20 3d 20 63 65 6c 6c 4f 66 66 73 65   ins = cellOffse
179d0 74 20 2b 20 32 2a 69 3b 0a 20 20 20 20 72 63 20  t + 2*i;.    rc 
179d1 3d 20 61 6c 6c 6f 63 61 74 65 53 70 61 63 65 28  = allocateSpace(
179d2 70 50 61 67 65 2c 20 73 7a 2c 20 26 69 64 78 29  pPage, sz, &idx)
179d3 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 20  ;.    if( rc ){ 
179d4 2a 70 52 43 20 3d 20 72 63 3b 20 72 65 74 75 72  *pRC = rc; retur
179d5 6e 3b 20 7d 0a 20 20 20 20 2f 2a 20 54 68 65 20  n; }.    /* The 
179d6 61 6c 6c 6f 63 61 74 65 53 70 61 63 65 28 29 20  allocateSpace() 
179d7 72 6f 75 74 69 6e 65 20 67 75 61 72 61 6e 74 65  routine guarante
179d8 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  es the following
179d9 20 74 77 6f 20 70 72 6f 70 65 72 74 69 65 73 0a   two properties.
179da 20 20 20 20 2a 2a 20 69 66 20 69 74 20 72 65 74      ** if it ret
179db 75 72 6e 73 20 73 75 63 63 65 73 73 20 2a 2f 0a  urns success */.
179dc 20 20 20 20 61 73 73 65 72 74 28 20 69 64 78 20      assert( idx 
179dd 3e 3d 20 65 6e 64 2b 32 20 29 3b 0a 20 20 20 20  >= end+2 );.    
179de 61 73 73 65 72 74 28 20 69 64 78 2b 73 7a 20 3c  assert( idx+sz <
179df 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73  = pPage->pBt->us
179e0 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20 20  ableSize );.    
179e1 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b 2b 3b 0a  pPage->nCell++;.
179e2 20 20 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65      pPage->nFree
179e3 20 2d 3d 20 28 75 31 36 29 28 32 20 2b 20 73 7a   -= (u16)(2 + sz
179e4 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 64  );.    memcpy(&d
179e5 61 74 61 5b 69 64 78 2b 6e 53 6b 69 70 5d 2c 20  ata[idx+nSkip], 
179e6 70 43 65 6c 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d  pCell+nSkip, sz-
179e7 6e 53 6b 69 70 29 3b 0a 20 20 20 20 69 66 28 20  nSkip);.    if( 
179e8 69 43 68 69 6c 64 20 29 7b 0a 20 20 20 20 20 20  iChild ){.      
179e9 70 75 74 34 62 79 74 65 28 26 64 61 74 61 5b 69  put4byte(&data[i
179ea 64 78 5d 2c 20 69 43 68 69 6c 64 29 3b 0a 20 20  dx], iChild);.  
179eb 20 20 7d 0a 20 20 20 20 66 6f 72 28 6a 3d 65 6e    }.    for(j=en
179ec 64 2c 20 70 74 72 3d 26 64 61 74 61 5b 6a 5d 3b  d, ptr=&data[j];
179ed 20 6a 3e 69 6e 73 3b 20 6a 2d 3d 32 2c 20 70 74   j>ins; j-=2, pt
179ee 72 2d 3d 32 29 7b 0a 20 20 20 20 20 20 70 74 72  r-=2){.      ptr
179ef 5b 30 5d 20 3d 20 70 74 72 5b 2d 32 5d 3b 0a 20  [0] = ptr[-2];. 
179f0 20 20 20 20 20 70 74 72 5b 31 5d 20 3d 20 70 74       ptr[1] = pt
179f1 72 5b 2d 31 5d 3b 0a 20 20 20 20 7d 0a 20 20 20  r[-1];.    }.   
179f2 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b   put2byte(&data[
179f3 69 6e 73 5d 2c 20 69 64 78 29 3b 0a 20 20 20 20  ins], idx);.    
179f4 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 70  put2byte(&data[p
179f5 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b  Page->hdrOffset+
179f6 33 5d 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  3], pPage->nCell
179f7 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
179f8 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
179f9 4d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d  M.    if( pPage-
179fa 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d  >pBt->autoVacuum
179fb 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65   ){.      /* The
179fc 20 63 65 6c 6c 20 6d 61 79 20 63 6f 6e 74 61 69   cell may contai
179fd 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  n a pointer to a
179fe 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e  n overflow page.
179ff 20 49 66 20 73 6f 2c 20 77 72 69 74 65 0a 20 20   If so, write.  
17a00 20 20 20 20 2a 2a 20 74 68 65 20 65 6e 74 72 79      ** the entry
17a01 20 66 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f   for the overflo
17a02 77 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20  w page into the 
17a03 70 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 20 20 20  pointer map..   
17a04 20 20 20 2a 2f 0a 20 20 20 20 20 20 70 74 72 6d     */.      ptrm
17a05 61 70 50 75 74 4f 76 66 6c 50 74 72 28 70 50 61  apPutOvflPtr(pPa
17a06 67 65 2c 20 70 43 65 6c 6c 2c 20 70 52 43 29 3b  ge, pCell, pRC);
17a07 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
17a08 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61  }.}../*.** Add a
17a09 20 6c 69 73 74 20 6f 66 20 63 65 6c 6c 73 20 74   list of cells t
17a0a 6f 20 61 20 70 61 67 65 2e 20 20 54 68 65 20 70  o a page.  The p
17a0b 61 67 65 20 73 68 6f 75 6c 64 20 62 65 20 69 6e  age should be in
17a0c 69 74 69 61 6c 6c 79 20 65 6d 70 74 79 2e 0a 2a  itially empty..*
17a0d 2a 20 54 68 65 20 63 65 6c 6c 73 20 61 72 65 20  * The cells are 
17a0e 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 66 69  guaranteed to fi
17a0f 74 20 6f 6e 20 74 68 65 20 70 61 67 65 2e 0a 2a  t on the page..*
17a10 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 73  /.static void as
17a11 73 65 6d 62 6c 65 50 61 67 65 28 0a 20 20 4d 65  semblePage(.  Me
17a12 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20  mPage *pPage,   
17a13 2f 2a 20 54 68 65 20 70 61 67 65 20 74 6f 20 62  /* The page to b
17a14 65 20 61 73 73 65 6d 62 6c 69 65 64 20 2a 2f 0a  e assemblied */.
17a15 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 20 20 20 20    int nCell,    
17a16 20 20 20 20 2f 2a 20 54 68 65 20 6e 75 6d 62 65      /* The numbe
17a17 72 20 6f 66 20 63 65 6c 6c 73 20 74 6f 20 61 64  r of cells to ad
17a18 64 20 74 6f 20 74 68 69 73 20 70 61 67 65 20 2a  d to this page *
17a19 2f 0a 20 20 75 38 20 2a 2a 61 70 43 65 6c 6c 2c  /.  u8 **apCell,
17a1a 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
17a1b 73 20 74 6f 20 63 65 6c 6c 20 62 6f 64 69 65 73  s to cell bodies
17a1c 20 2a 2f 0a 20 20 75 31 36 20 2a 61 53 69 7a 65   */.  u16 *aSize
17a1d 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 73          /* Sizes
17a1e 20 6f 66 20 74 68 65 20 63 65 6c 6c 73 20 2a 2f   of the cells */
17a1f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20  .){.  int i;    
17a20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
17a21 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 38 20  counter */.  u8 
17a22 2a 70 43 65 6c 6c 70 74 72 3b 20 20 20 20 20 2f  *pCellptr;     /
17a23 2a 20 41 64 64 72 65 73 73 20 6f 66 20 6e 65 78  * Address of nex
17a24 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a  t cell pointer *
17a25 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 62 6f 64 79  /.  int cellbody
17a26 3b 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73  ;     /* Address
17a27 20 6f 66 20 6e 65 78 74 20 63 65 6c 6c 20 62 6f   of next cell bo
17a28 64 79 20 2a 2f 0a 20 20 75 38 20 2a 20 63 6f 6e  dy */.  u8 * con
17a29 73 74 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d  st data = pPage-
17a2a 3e 61 44 61 74 61 3b 20 20 20 20 20 20 20 20 20  >aData;         
17a2b 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
17a2c 6f 20 64 61 74 61 20 66 6f 72 20 70 50 61 67 65  o data for pPage
17a2d 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20   */.  const int 
17a2e 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72  hdr = pPage->hdr
17a2f 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20  Offset;         
17a30 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 68    /* Offset of h
17a31 65 61 64 65 72 20 6f 6e 20 70 50 61 67 65 20 2a  eader on pPage *
17a32 2f 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 55  /.  const int nU
17a33 73 61 62 6c 65 20 3d 20 70 50 61 67 65 2d 3e 70  sable = pPage->p
17a34 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 20  Bt->usableSize; 
17a35 2f 2a 20 55 73 61 62 6c 65 20 73 69 7a 65 20 6f  /* Usable size o
17a36 66 20 70 61 67 65 20 2a 2f 0a 0a 20 20 61 73 73  f page */..  ass
17a37 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65  ert( pPage->nOve
17a38 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73  rflow==0 );.  as
17a39 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
17a3a 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e  tex_held(pPage->
17a3b 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
17a3c 20 61 73 73 65 72 74 28 20 6e 43 65 6c 6c 3e 3d   assert( nCell>=
17a3d 30 20 26 26 20 6e 43 65 6c 6c 3c 3d 4d 58 5f 43  0 && nCell<=MX_C
17a3e 45 4c 4c 28 70 50 61 67 65 2d 3e 70 42 74 29 20  ELL(pPage->pBt) 
17a3f 26 26 20 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65  && MX_CELL(pPage
17a40 2d 3e 70 42 74 29 3c 3d 35 34 36 30 20 29 3b 0a  ->pBt)<=5460 );.
17a41 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
17a42 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c  3PagerIswriteabl
17a43 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  e(pPage->pDbPage
17a44 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  ) );..  /* Check
17a45 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 68   that the page h
17a46 61 73 20 6a 75 73 74 20 62 65 65 6e 20 7a 65 72  as just been zer
17a47 6f 65 64 20 62 79 20 7a 65 72 6f 50 61 67 65 28  oed by zeroPage(
17a48 29 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  ) */.  assert( p
17a49 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29  Page->nCell==0 )
17a4a 3b 0a 20 20 61 73 73 65 72 74 28 20 67 65 74 32  ;.  assert( get2
17a4b 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35  byte(&data[hdr+5
17a4c 5d 29 3d 3d 6e 55 73 61 62 6c 65 20 29 3b 0a 0a  ])==nUsable );..
17a4d 20 20 70 43 65 6c 6c 70 74 72 20 3d 20 26 64 61    pCellptr = &da
17a4e 74 61 5b 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66  ta[pPage->cellOf
17a4f 66 73 65 74 20 2b 20 6e 43 65 6c 6c 2a 32 5d 3b  fset + nCell*2];
17a50 0a 20 20 63 65 6c 6c 62 6f 64 79 20 3d 20 6e 55  .  cellbody = nU
17a51 73 61 62 6c 65 3b 0a 20 20 66 6f 72 28 69 3d 6e  sable;.  for(i=n
17a52 43 65 6c 6c 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d  Cell-1; i>=0; i-
17a53 2d 29 7b 0a 20 20 20 20 70 43 65 6c 6c 70 74 72  -){.    pCellptr
17a54 20 2d 3d 20 32 3b 0a 20 20 20 20 63 65 6c 6c 62   -= 2;.    cellb
17a55 6f 64 79 20 2d 3d 20 61 53 69 7a 65 5b 69 5d 3b  ody -= aSize[i];
17a56 0a 20 20 20 20 70 75 74 32 62 79 74 65 28 70 43  .    put2byte(pC
17a57 65 6c 6c 70 74 72 2c 20 63 65 6c 6c 62 6f 64 79  ellptr, cellbody
17a58 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 64  );.    memcpy(&d
17a59 61 74 61 5b 63 65 6c 6c 62 6f 64 79 5d 2c 20 61  ata[cellbody], a
17a5a 70 43 65 6c 6c 5b 69 5d 2c 20 61 53 69 7a 65 5b  pCell[i], aSize[
17a5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 70 75 74 32 62  i]);.  }.  put2b
17a5c 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d  yte(&data[hdr+3]
17a5d 2c 20 6e 43 65 6c 6c 29 3b 0a 20 20 70 75 74 32  , nCell);.  put2
17a5e 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35  byte(&data[hdr+5
17a5f 5d 2c 20 63 65 6c 6c 62 6f 64 79 29 3b 0a 20 20  ], cellbody);.  
17a60 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 2d 3d 20  pPage->nFree -= 
17a61 28 6e 43 65 6c 6c 2a 32 20 2b 20 6e 55 73 61 62  (nCell*2 + nUsab
17a62 6c 65 20 2d 20 63 65 6c 6c 62 6f 64 79 29 3b 0a  le - cellbody);.
17a63 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d    pPage->nCell =
17a64 20 28 75 31 36 29 6e 43 65 6c 6c 3b 0a 7d 0a 0a   (u16)nCell;.}..
17a65 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
17a66 69 6e 67 20 70 61 72 61 6d 65 74 65 72 73 20 64  ing parameters d
17a67 65 74 65 72 6d 69 6e 65 20 68 6f 77 20 6d 61 6e  etermine how man
17a68 79 20 61 64 6a 61 63 65 6e 74 20 70 61 67 65 73  y adjacent pages
17a69 20 67 65 74 20 69 6e 76 6f 6c 76 65 64 0a 2a 2a   get involved.**
17a6a 20 69 6e 20 61 20 62 61 6c 61 6e 63 69 6e 67 20   in a balancing 
17a6b 6f 70 65 72 61 74 69 6f 6e 2e 20 20 4e 4e 20 69  operation.  NN i
17a6c 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
17a6d 6e 65 69 67 68 62 6f 72 73 20 6f 6e 20 65 69 74  neighbors on eit
17a6e 68 65 72 20 73 69 64 65 0a 2a 2a 20 6f 66 20 74  her side.** of t
17a6f 68 65 20 70 61 67 65 20 74 68 61 74 20 70 61 72  he page that par
17a70 74 69 63 69 70 61 74 65 20 69 6e 20 74 68 65 20  ticipate in the 
17a71 62 61 6c 61 6e 63 69 6e 67 20 6f 70 65 72 61 74  balancing operat
17a72 69 6f 6e 2e 20 20 4e 42 20 69 73 20 74 68 65 0a  ion.  NB is the.
17a73 2a 2a 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20  ** total number 
17a74 6f 66 20 70 61 67 65 73 20 74 68 61 74 20 70 61  of pages that pa
17a75 72 74 69 63 69 70 61 74 65 2c 20 69 6e 63 6c 75  rticipate, inclu
17a76 64 69 6e 67 20 74 68 65 20 74 61 72 67 65 74 20  ding the target 
17a77 70 61 67 65 20 61 6e 64 0a 2a 2a 20 4e 4e 20 6e  page and.** NN n
17a78 65 69 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 68  eighbors on eith
17a79 65 72 20 73 69 64 65 2e 0a 2a 2a 0a 2a 2a 20 54  er side..**.** T
17a7a 68 65 20 6d 69 6e 69 6d 75 6d 20 76 61 6c 75 65  he minimum value
17a7b 20 6f 66 20 4e 4e 20 69 73 20 31 20 28 6f 66 20   of NN is 1 (of 
17a7c 63 6f 75 72 73 65 29 2e 20 20 49 6e 63 72 65 61  course).  Increa
17a7d 73 69 6e 67 20 4e 4e 20 61 62 6f 76 65 20 31 0a  sing NN above 1.
17a7e 2a 2a 20 28 74 6f 20 32 20 6f 72 20 33 29 20 67  ** (to 2 or 3) g
17a7f 69 76 65 73 20 61 20 6d 6f 64 65 73 74 20 69 6d  ives a modest im
17a80 70 72 6f 76 65 6d 65 6e 74 20 69 6e 20 53 45 4c  provement in SEL
17a81 45 43 54 20 61 6e 64 20 44 45 4c 45 54 45 20 70  ECT and DELETE p
17a82 65 72 66 6f 72 6d 61 6e 63 65 0a 2a 2a 20 69 6e  erformance.** in
17a83 20 65 78 63 68 61 6e 67 65 20 66 6f 72 20 61 20   exchange for a 
17a84 6c 61 72 67 65 72 20 64 65 67 72 61 64 61 74 69  larger degradati
17a85 6f 6e 20 69 6e 20 49 4e 53 45 52 54 20 61 6e 64  on in INSERT and
17a86 20 55 50 44 41 54 45 20 70 65 72 66 6f 72 6d 61   UPDATE performa
17a87 6e 63 65 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75  nce..** The valu
17a88 65 20 6f 66 20 4e 4e 20 61 70 70 65 61 72 73 20  e of NN appears 
17a89 74 6f 20 67 69 76 65 20 74 68 65 20 62 65 73 74  to give the best
17a8a 20 72 65 73 75 6c 74 73 20 6f 76 65 72 61 6c 6c   results overall
17a8b 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 4e 20  ..*/.#define NN 
17a8c 31 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  1             /*
17a8d 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 69 67 68   Number of neigh
17a8e 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 20 73  bors on either s
17a8f 69 64 65 20 6f 66 20 70 50 61 67 65 20 2a 2f 0a  ide of pPage */.
17a90 23 64 65 66 69 6e 65 20 4e 42 20 28 4e 4e 2a 32  #define NB (NN*2
17a91 2b 31 29 20 20 20 20 20 20 2f 2a 20 54 6f 74 61  +1)      /* Tota
17a92 6c 20 70 61 67 65 73 20 69 6e 76 6f 6c 76 65 64  l pages involved
17a93 20 69 6e 20 74 68 65 20 62 61 6c 61 6e 63 65 20   in the balance 
17a94 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  */...#ifndef SQL
17a95 49 54 45 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 41  ITE_OMIT_QUICKBA
17a96 4c 41 4e 43 45 0a 2f 2a 0a 2a 2a 20 54 68 69 73  LANCE./*.** This
17a97 20 76 65 72 73 69 6f 6e 20 6f 66 20 62 61 6c 61   version of bala
17a98 6e 63 65 28 29 20 68 61 6e 64 6c 65 73 20 74 68  nce() handles th
17a99 65 20 63 6f 6d 6d 6f 6e 20 73 70 65 63 69 61 6c  e common special
17a9a 20 63 61 73 65 20 77 68 65 72 65 0a 2a 2a 20 61   case where.** a
17a9b 20 6e 65 77 20 65 6e 74 72 79 20 69 73 20 62 65   new entry is be
17a9c 69 6e 67 20 69 6e 73 65 72 74 65 64 20 6f 6e 20  ing inserted on 
17a9d 74 68 65 20 65 78 74 72 65 6d 65 20 72 69 67 68  the extreme righ
17a9e 74 2d 65 6e 64 20 6f 66 20 74 68 65 0a 2a 2a 20  t-end of the.** 
17a9f 74 72 65 65 2c 20 69 6e 20 6f 74 68 65 72 20 77  tree, in other w
17aa0 6f 72 64 73 2c 20 77 68 65 6e 20 74 68 65 20 6e  ords, when the n
17aa1 65 77 20 65 6e 74 72 79 20 77 69 6c 6c 20 62 65  ew entry will be
17aa2 63 6f 6d 65 20 74 68 65 20 6c 61 72 67 65 73 74  come the largest
17aa3 0a 2a 2a 20 65 6e 74 72 79 20 69 6e 20 74 68 65  .** entry in the
17aa4 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 73   tree..**.** Ins
17aa5 74 65 61 64 20 6f 66 20 74 72 79 69 6e 67 20 74  tead of trying t
17aa6 6f 20 62 61 6c 61 6e 63 65 20 74 68 65 20 33 20  o balance the 3 
17aa7 72 69 67 68 74 2d 6d 6f 73 74 20 6c 65 61 66 20  right-most leaf 
17aa8 70 61 67 65 73 2c 20 6a 75 73 74 20 61 64 64 0a  pages, just add.
17aa9 2a 2a 20 61 20 6e 65 77 20 70 61 67 65 20 74 6f  ** a new page to
17aaa 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20   the right-hand 
17aab 73 69 64 65 20 61 6e 64 20 70 75 74 20 74 68 65  side and put the
17aac 20 6f 6e 65 20 6e 65 77 20 65 6e 74 72 79 20 69   one new entry i
17aad 6e 0a 2a 2a 20 74 68 61 74 20 70 61 67 65 2e 20  n.** that page. 
17aae 20 54 68 69 73 20 6c 65 61 76 65 73 20 74 68 65   This leaves the
17aaf 20 72 69 67 68 74 20 73 69 64 65 20 6f 66 20 74   right side of t
17ab0 68 65 20 74 72 65 65 20 73 6f 6d 65 77 68 61 74  he tree somewhat
17ab1 0a 2a 2a 20 75 6e 62 61 6c 61 6e 63 65 64 2e 20  .** unbalanced. 
17ab2 20 42 75 74 20 6f 64 64 73 20 61 72 65 20 74 68   But odds are th
17ab3 61 74 20 77 65 20 77 69 6c 6c 20 62 65 20 69 6e  at we will be in
17ab4 73 65 72 74 69 6e 67 20 6e 65 77 20 65 6e 74 72  serting new entr
17ab5 69 65 73 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e  ies.** at the en
17ab6 64 20 73 6f 6f 6e 20 61 66 74 65 72 77 61 72 64  d soon afterward
17ab7 73 20 73 6f 20 74 68 65 20 6e 65 61 72 6c 79 20  s so the nearly 
17ab8 65 6d 70 74 79 20 70 61 67 65 20 77 69 6c 6c 20  empty page will 
17ab9 71 75 69 63 6b 6c 79 0a 2a 2a 20 66 69 6c 6c 20  quickly.** fill 
17aba 75 70 2e 20 20 4f 6e 20 61 76 65 72 61 67 65 2e  up.  On average.
17abb 0a 2a 2a 0a 2a 2a 20 70 50 61 67 65 20 69 73 20  .**.** pPage is 
17abc 74 68 65 20 6c 65 61 66 20 70 61 67 65 20 77 68  the leaf page wh
17abd 69 63 68 20 69 73 20 74 68 65 20 72 69 67 68 74  ich is the right
17abe 2d 6d 6f 73 74 20 70 61 67 65 20 69 6e 20 74 68  -most page in th
17abf 65 20 74 72 65 65 2e 0a 2a 2a 20 70 50 61 72 65  e tree..** pPare
17ac0 6e 74 20 69 73 20 69 74 73 20 70 61 72 65 6e 74  nt is its parent
17ac1 2e 20 20 70 50 61 67 65 20 6d 75 73 74 20 68 61  .  pPage must ha
17ac2 76 65 20 61 20 73 69 6e 67 6c 65 20 6f 76 65 72  ve a single over
17ac3 66 6c 6f 77 20 65 6e 74 72 79 0a 2a 2a 20 77 68  flow entry.** wh
17ac4 69 63 68 20 69 73 20 61 6c 73 6f 20 74 68 65 20  ich is also the 
17ac5 72 69 67 68 74 2d 6d 6f 73 74 20 65 6e 74 72 79  right-most entry
17ac6 20 6f 6e 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a   on the page..**
17ac7 0a 2a 2a 20 54 68 65 20 70 53 70 61 63 65 20 62  .** The pSpace b
17ac8 75 66 66 65 72 20 69 73 20 75 73 65 64 20 74 6f  uffer is used to
17ac9 20 73 74 6f 72 65 20 61 20 74 65 6d 70 6f 72 61   store a tempora
17aca 72 79 20 63 6f 70 79 20 6f 66 20 74 68 65 20 64  ry copy of the d
17acb 69 76 69 64 65 72 0a 2a 2a 20 63 65 6c 6c 20 74  ivider.** cell t
17acc 68 61 74 20 77 69 6c 6c 20 62 65 20 69 6e 73 65  hat will be inse
17acd 72 74 65 64 20 69 6e 74 6f 20 70 50 61 72 65 6e  rted into pParen
17ace 74 2e 20 53 75 63 68 20 61 20 63 65 6c 6c 20 63  t. Such a cell c
17acf 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 34 0a 2a  onsists of a 4.*
17ad0 2a 20 62 79 74 65 20 70 61 67 65 20 6e 75 6d 62  * byte page numb
17ad1 65 72 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61  er followed by a
17ad2 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68   variable length
17ad3 20 69 6e 74 65 67 65 72 2e 20 49 6e 20 6f 74 68   integer. In oth
17ad4 65 72 0a 2a 2a 20 77 6f 72 64 73 2c 20 61 74 20  er.** words, at 
17ad5 6d 6f 73 74 20 31 33 20 62 79 74 65 73 2e 20 48  most 13 bytes. H
17ad6 65 6e 63 65 20 74 68 65 20 70 53 70 61 63 65 20  ence the pSpace 
17ad7 62 75 66 66 65 72 20 6d 75 73 74 20 62 65 20 61  buffer must be a
17ad8 74 0a 2a 2a 20 6c 65 61 73 74 20 31 33 20 62 79  t.** least 13 by
17ad9 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a  tes in size..*/.
17ada 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e  static int balan
17adb 63 65 5f 71 75 69 63 6b 28 4d 65 6d 50 61 67 65  ce_quick(MemPage
17adc 20 2a 70 50 61 72 65 6e 74 2c 20 4d 65 6d 50 61   *pParent, MemPa
17add 67 65 20 2a 70 50 61 67 65 2c 20 75 38 20 2a 70  ge *pPage, u8 *p
17ade 53 70 61 63 65 29 7b 0a 20 20 42 74 53 68 61 72  Space){.  BtShar
17adf 65 64 20 2a 63 6f 6e 73 74 20 70 42 74 20 3d 20  ed *const pBt = 
17ae0 70 50 61 67 65 2d 3e 70 42 74 3b 20 20 20 20 2f  pPage->pBt;    /
17ae1 2a 20 42 2d 54 72 65 65 20 44 61 74 61 62 61 73  * B-Tree Databas
17ae2 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a  e */.  MemPage *
17ae3 70 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20 20  pNew;           
17ae4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
17ae5 65 77 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 70  ewly allocated p
17ae6 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b  age */.  int rc;
17ae7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17ae8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
17ae9 20 52 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a   Return Code */.
17aea 20 20 50 67 6e 6f 20 70 67 6e 6f 4e 65 77 3b 20    Pgno pgnoNew; 
17aeb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17aec 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e         /* Page n
17aed 75 6d 62 65 72 20 6f 66 20 70 4e 65 77 20 2a 2f  umber of pNew */
17aee 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
17aef 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
17af0 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  Page->pBt->mutex
17af1 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  ) );.  assert( s
17af2 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69  qlite3PagerIswri
17af3 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e  teable(pParent->
17af4 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73  pDbPage) );.  as
17af5 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76  sert( pPage->nOv
17af6 65 72 66 6c 6f 77 3d 3d 31 20 29 3b 0a 0a 20 20  erflow==1 );..  
17af7 69 66 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  if( pPage->nCell
17af8 3c 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c  <=0 ) return SQL
17af9 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
17afa 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  ;..  /* Allocate
17afb 20 61 20 6e 65 77 20 70 61 67 65 2e 20 54 68 69   a new page. Thi
17afc 73 20 70 61 67 65 20 77 69 6c 6c 20 62 65 63 6f  s page will beco
17afd 6d 65 20 74 68 65 20 72 69 67 68 74 2d 73 69 62  me the right-sib
17afe 6c 69 6e 67 20 6f 66 20 0a 20 20 2a 2a 20 70 50  ling of .  ** pP
17aff 61 67 65 2e 20 4d 61 6b 65 20 74 68 65 20 70 61  age. Make the pa
17b00 72 65 6e 74 20 70 61 67 65 20 77 72 69 74 61 62  rent page writab
17b01 6c 65 2c 20 73 6f 20 74 68 61 74 20 74 68 65 20  le, so that the 
17b02 6e 65 77 20 64 69 76 69 64 65 72 20 63 65 6c 6c  new divider cell
17b03 0a 20 20 2a 2a 20 6d 61 79 20 62 65 20 69 6e 73  .  ** may be ins
17b04 65 72 74 65 64 2e 20 49 66 20 62 6f 74 68 20 74  erted. If both t
17b05 68 65 73 65 20 6f 70 65 72 61 74 69 6f 6e 73 20  hese operations 
17b06 61 72 65 20 73 75 63 63 65 73 73 66 75 6c 2c 20  are successful, 
17b07 70 72 6f 63 65 65 64 2e 0a 20 20 2a 2f 0a 20 20  proceed..  */.  
17b08 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72  rc = allocateBtr
17b09 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 4e 65  eePage(pBt, &pNe
17b0a 77 2c 20 26 70 67 6e 6f 4e 65 77 2c 20 30 2c 20  w, &pgnoNew, 0, 
17b0b 30 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53  0);..  if( rc==S
17b0c 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 0a 20 20 20  QLITE_OK ){..   
17b0d 20 75 38 20 2a 70 4f 75 74 20 3d 20 26 70 53 70   u8 *pOut = &pSp
17b0e 61 63 65 5b 34 5d 3b 0a 20 20 20 20 75 38 20 2a  ace[4];.    u8 *
17b0f 70 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 61  pCell = pPage->a
17b10 4f 76 66 6c 5b 30 5d 2e 70 43 65 6c 6c 3b 0a 20  Ovfl[0].pCell;. 
17b11 20 20 20 75 31 36 20 73 7a 43 65 6c 6c 20 3d 20     u16 szCell = 
17b12 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67  cellSizePtr(pPag
17b13 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 75  e, pCell);.    u
17b14 38 20 2a 70 53 74 6f 70 3b 0a 0a 20 20 20 20 61  8 *pStop;..    a
17b15 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
17b16 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
17b17 4e 65 77 2d 3e 70 44 62 50 61 67 65 29 20 29 3b  New->pDbPage) );
17b18 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
17b19 67 65 2d 3e 61 44 61 74 61 5b 30 5d 3d 3d 28 50  ge->aData[0]==(P
17b1a 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 5f 4c 45  TF_INTKEY|PTF_LE
17b1b 41 46 44 41 54 41 7c 50 54 46 5f 4c 45 41 46 29  AFDATA|PTF_LEAF)
17b1c 20 29 3b 0a 20 20 20 20 7a 65 72 6f 50 61 67 65   );.    zeroPage
17b1d 28 70 4e 65 77 2c 20 50 54 46 5f 49 4e 54 4b 45  (pNew, PTF_INTKE
17b1e 59 7c 50 54 46 5f 4c 45 41 46 44 41 54 41 7c 50  Y|PTF_LEAFDATA|P
17b1f 54 46 5f 4c 45 41 46 29 3b 0a 20 20 20 20 61 73  TF_LEAF);.    as
17b20 73 65 6d 62 6c 65 50 61 67 65 28 70 4e 65 77 2c  semblePage(pNew,
17b21 20 31 2c 20 26 70 43 65 6c 6c 2c 20 26 73 7a 43   1, &pCell, &szC
17b22 65 6c 6c 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66  ell);..    /* If
17b23 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 74 6f   this is an auto
17b24 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65  -vacuum database
17b25 2c 20 75 70 64 61 74 65 20 74 68 65 20 70 6f 69  , update the poi
17b26 6e 74 65 72 20 6d 61 70 0a 20 20 20 20 2a 2a 20  nter map.    ** 
17b27 77 69 74 68 20 65 6e 74 72 69 65 73 20 66 6f 72  with entries for
17b28 20 74 68 65 20 6e 65 77 20 70 61 67 65 2c 20 61   the new page, a
17b29 6e 64 20 61 6e 79 20 70 6f 69 6e 74 65 72 20 66  nd any pointer f
17b2a 72 6f 6d 20 74 68 65 20 0a 20 20 20 20 2a 2a 20  rom the .    ** 
17b2b 63 65 6c 6c 20 6f 6e 20 74 68 65 20 70 61 67 65  cell on the page
17b2c 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20   to an overflow 
17b2d 70 61 67 65 2e 20 49 66 20 65 69 74 68 65 72 20  page. If either 
17b2e 6f 66 20 74 68 65 73 65 0a 20 20 20 20 2a 2a 20  of these.    ** 
17b2f 6f 70 65 72 61 74 69 6f 6e 73 20 66 61 69 6c 73  operations fails
17b30 2c 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64  , the return cod
17b31 65 20 69 73 20 73 65 74 2c 20 62 75 74 20 74 68  e is set, but th
17b32 65 20 63 6f 6e 74 65 6e 74 73 0a 20 20 20 20 2a  e contents.    *
17b33 2a 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74 20  * of the parent 
17b34 70 61 67 65 20 61 72 65 20 73 74 69 6c 6c 20 6d  page are still m
17b35 61 6e 69 70 75 6c 61 74 65 64 20 62 79 20 74 68  anipulated by th
17b36 68 20 63 6f 64 65 20 62 65 6c 6f 77 2e 0a 20 20  h code below..  
17b37 20 20 2a 2a 20 54 68 61 74 20 69 73 20 4f 6b 2c    ** That is Ok,
17b38 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74   at this point t
17b39 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 69  he parent page i
17b3a 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 0a  s guaranteed to.
17b3b 20 20 20 20 2a 2a 20 62 65 20 6d 61 72 6b 65 64      ** be marked
17b3c 20 61 73 20 64 69 72 74 79 2e 20 52 65 74 75 72   as dirty. Retur
17b3d 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 63 6f  ning an error co
17b3e 64 65 20 77 69 6c 6c 20 63 61 75 73 65 20 61 0a  de will cause a.
17b3f 20 20 20 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b 2c      ** rollback,
17b40 20 75 6e 64 6f 69 6e 67 20 61 6e 79 20 63 68 61   undoing any cha
17b41 6e 67 65 73 20 6d 61 64 65 20 74 6f 20 74 68 65  nges made to the
17b42 20 70 61 72 65 6e 74 20 70 61 67 65 2e 0a 20 20   parent page..  
17b43 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 49 53 41    */.    if( ISA
17b44 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20  UTOVACUUM ){.   
17b45 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74     ptrmapPut(pBt
17b46 2c 20 70 67 6e 6f 4e 65 77 2c 20 50 54 52 4d 41  , pgnoNew, PTRMA
17b47 50 5f 42 54 52 45 45 2c 20 70 50 61 72 65 6e 74  P_BTREE, pParent
17b48 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20  ->pgno, &rc);.  
17b49 20 20 20 20 69 66 28 20 73 7a 43 65 6c 6c 3e 70      if( szCell>p
17b4a 4e 65 77 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 29 7b  New->minLocal ){
17b4b 0a 20 20 20 20 20 20 20 20 70 74 72 6d 61 70 50  .        ptrmapP
17b4c 75 74 4f 76 66 6c 50 74 72 28 70 4e 65 77 2c 20  utOvflPtr(pNew, 
17b4d 70 43 65 6c 6c 2c 20 26 72 63 29 3b 0a 20 20 20  pCell, &rc);.   
17b4e 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20     }.    }.  .  
17b4f 20 20 2f 2a 20 43 72 65 61 74 65 20 61 20 64 69    /* Create a di
17b50 76 69 64 65 72 20 63 65 6c 6c 20 74 6f 20 69 6e  vider cell to in
17b51 73 65 72 74 20 69 6e 74 6f 20 70 50 61 72 65 6e  sert into pParen
17b52 74 2e 20 54 68 65 20 64 69 76 69 64 65 72 20 63  t. The divider c
17b53 65 6c 6c 0a 20 20 20 20 2a 2a 20 63 6f 6e 73 69  ell.    ** consi
17b54 73 74 73 20 6f 66 20 61 20 34 2d 62 79 74 65 20  sts of a 4-byte 
17b55 70 61 67 65 20 6e 75 6d 62 65 72 20 28 74 68 65  page number (the
17b56 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20   page number of 
17b57 70 50 61 67 65 29 20 61 6e 64 0a 20 20 20 20 2a  pPage) and.    *
17b58 2a 20 61 20 76 61 72 69 61 62 6c 65 20 6c 65 6e  * a variable len
17b59 67 74 68 20 6b 65 79 20 76 61 6c 75 65 20 28 77  gth key value (w
17b5a 68 69 63 68 20 6d 75 73 74 20 62 65 20 74 68 65  hich must be the
17b5b 20 73 61 6d 65 20 76 61 6c 75 65 20 61 73 20 74   same value as t
17b5c 68 65 0a 20 20 20 20 2a 2a 20 6c 61 72 67 65 73  he.    ** larges
17b5d 74 20 6b 65 79 20 6f 6e 20 70 50 61 67 65 29 2e  t key on pPage).
17b5e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54  .    **.    ** T
17b5f 6f 20 66 69 6e 64 20 74 68 65 20 6c 61 72 67 65  o find the large
17b60 73 74 20 6b 65 79 20 76 61 6c 75 65 20 6f 6e 20  st key value on 
17b61 70 50 61 67 65 2c 20 66 69 72 73 74 20 66 69 6e  pPage, first fin
17b62 64 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74  d the right-most
17b63 20 0a 20 20 20 20 2a 2a 20 63 65 6c 6c 20 6f 6e   .    ** cell on
17b64 20 70 50 61 67 65 2e 20 54 68 65 20 66 69 72 73   pPage. The firs
17b65 74 20 74 77 6f 20 66 69 65 6c 64 73 20 6f 66 20  t two fields of 
17b66 74 68 69 73 20 63 65 6c 6c 20 61 72 65 20 74 68  this cell are th
17b67 65 20 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 64  e .    ** record
17b68 2d 6c 65 6e 67 74 68 20 28 61 20 76 61 72 69 61  -length (a varia
17b69 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e 74 65 67  ble length integ
17b6a 65 72 20 61 74 20 6d 6f 73 74 20 33 32 2d 62 69  er at most 32-bi
17b6b 74 73 20 69 6e 20 73 69 7a 65 29 0a 20 20 20 20  ts in size).    
17b6c 2a 2a 20 61 6e 64 20 74 68 65 20 6b 65 79 20 76  ** and the key v
17b6d 61 6c 75 65 20 28 61 20 76 61 72 69 61 62 6c 65  alue (a variable
17b6e 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 2c   length integer,
17b6f 20 6d 61 79 20 68 61 76 65 20 61 6e 79 20 76 61   may have any va
17b70 6c 75 65 29 2e 0a 20 20 20 20 2a 2a 20 54 68 65  lue)..    ** The
17b71 20 66 69 72 73 74 20 6f 66 20 74 68 65 20 77 68   first of the wh
17b72 69 6c 65 28 2e 2e 2e 29 20 6c 6f 6f 70 73 20 62  ile(...) loops b
17b73 65 6c 6f 77 20 73 6b 69 70 73 20 6f 76 65 72 20  elow skips over 
17b74 74 68 65 20 72 65 63 6f 72 64 2d 6c 65 6e 67 74  the record-lengt
17b75 68 0a 20 20 20 20 2a 2a 20 66 69 65 6c 64 2e 20  h.    ** field. 
17b76 54 68 65 20 73 65 63 6f 6e 64 20 77 68 69 6c 65  The second while
17b77 28 2e 2e 2e 29 20 6c 6f 6f 70 20 63 6f 70 69 65  (...) loop copie
17b78 73 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 20  s the key value 
17b79 66 72 6f 6d 20 74 68 65 0a 20 20 20 20 2a 2a 20  from the.    ** 
17b7a 63 65 6c 6c 20 6f 6e 20 70 50 61 67 65 20 69 6e  cell on pPage in
17b7b 74 6f 20 74 68 65 20 70 53 70 61 63 65 20 62 75  to the pSpace bu
17b7c 66 66 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ffer..    */.   
17b7d 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c   pCell = findCel
17b7e 6c 28 70 50 61 67 65 2c 20 70 50 61 67 65 2d 3e  l(pPage, pPage->
17b7f 6e 43 65 6c 6c 2d 31 29 3b 0a 20 20 20 20 70 53  nCell-1);.    pS
17b80 74 6f 70 20 3d 20 26 70 43 65 6c 6c 5b 39 5d 3b  top = &pCell[9];
17b81 0a 20 20 20 20 77 68 69 6c 65 28 20 28 2a 28 70  .    while( (*(p
17b82 43 65 6c 6c 2b 2b 29 26 30 78 38 30 29 20 26 26  Cell++)&0x80) &&
17b83 20 70 43 65 6c 6c 3c 70 53 74 6f 70 20 29 3b 0a   pCell<pStop );.
17b84 20 20 20 20 70 53 74 6f 70 20 3d 20 26 70 43 65      pStop = &pCe
17b85 6c 6c 5b 39 5d 3b 0a 20 20 20 20 77 68 69 6c 65  ll[9];.    while
17b86 28 20 28 28 2a 28 70 4f 75 74 2b 2b 29 20 3d 20  ( ((*(pOut++) = 
17b87 2a 28 70 43 65 6c 6c 2b 2b 29 29 26 30 78 38 30  *(pCell++))&0x80
17b88 29 20 26 26 20 70 43 65 6c 6c 3c 70 53 74 6f 70  ) && pCell<pStop
17b89 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 73 65   );..    /* Inse
17b8a 72 74 20 74 68 65 20 6e 65 77 20 64 69 76 69 64  rt the new divid
17b8b 65 72 20 63 65 6c 6c 20 69 6e 74 6f 20 70 50 61  er cell into pPa
17b8c 72 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 69 6e 73  rent. */.    ins
17b8d 65 72 74 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c  ertCell(pParent,
17b8e 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 2c   pParent->nCell,
17b8f 20 70 53 70 61 63 65 2c 20 28 69 6e 74 29 28 70   pSpace, (int)(p
17b90 4f 75 74 2d 70 53 70 61 63 65 29 2c 0a 20 20 20  Out-pSpace),.   
17b91 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 70              0, p
17b92 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 26 72 63 29  Page->pgno, &rc)
17b93 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68  ;..    /* Set th
17b94 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 70 6f  e right-child po
17b95 69 6e 74 65 72 20 6f 66 20 70 50 61 72 65 6e 74  inter of pParent
17b96 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65   to point to the
17b97 20 6e 65 77 20 70 61 67 65 2e 20 2a 2f 0a 20 20   new page. */.  
17b98 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61 72    put4byte(&pPar
17b99 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65  ent->aData[pPare
17b9a 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d  nt->hdrOffset+8]
17b9b 2c 20 70 67 6e 6f 4e 65 77 29 3b 0a 20 20 0a 20  , pgnoNew);.  . 
17b9c 20 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68     /* Release th
17b9d 65 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74  e reference to t
17b9e 68 65 20 6e 65 77 20 70 61 67 65 2e 20 2a 2f 0a  he new page. */.
17b9f 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
17ba0 70 4e 65 77 29 3b 0a 20 20 7d 0a 0a 20 20 72 65  pNew);.  }..  re
17ba1 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69  turn rc;.}.#endi
17ba2 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
17ba3 5f 51 55 49 43 4b 42 41 4c 41 4e 43 45 20 2a 2f  _QUICKBALANCE */
17ba4 0a 0a 23 69 66 20 30 0a 2f 2a 0a 2a 2a 20 54 68  ..#if 0./*.** Th
17ba5 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73  is function does
17ba6 20 6e 6f 74 20 63 6f 6e 74 72 69 62 75 74 65 20   not contribute 
17ba7 61 6e 79 74 68 69 6e 67 20 74 6f 20 74 68 65 20  anything to the 
17ba8 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 53 51 4c  operation of SQL
17ba9 69 74 65 2e 0a 2a 2a 20 69 74 20 69 73 20 73 6f  ite..** it is so
17baa 6d 65 74 69 6d 65 73 20 61 63 74 69 76 61 74 65  metimes activate
17bab 64 20 74 65 6d 70 6f 72 61 72 69 6c 79 20 77 68  d temporarily wh
17bac 69 6c 65 20 64 65 62 75 67 67 69 6e 67 20 63 6f  ile debugging co
17bad 64 65 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 0a  de responsible .
17bae 2a 2a 20 66 6f 72 20 73 65 74 74 69 6e 67 20 70  ** for setting p
17baf 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69  ointer-map entri
17bb0 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  es..*/.static in
17bb1 74 20 70 74 72 6d 61 70 43 68 65 63 6b 50 61 67  t ptrmapCheckPag
17bb2 65 73 28 4d 65 6d 50 61 67 65 20 2a 2a 61 70 50  es(MemPage **apP
17bb3 61 67 65 2c 20 69 6e 74 20 6e 50 61 67 65 29 7b  age, int nPage){
17bb4 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 66  .  int i, j;.  f
17bb5 6f 72 28 69 3d 30 3b 20 69 3c 6e 50 61 67 65 3b  or(i=0; i<nPage;
17bb6 20 69 2b 2b 29 7b 0a 20 20 20 20 50 67 6e 6f 20   i++){.    Pgno 
17bb7 6e 3b 0a 20 20 20 20 75 38 20 65 3b 0a 20 20 20  n;.    u8 e;.   
17bb8 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20   MemPage *pPage 
17bb9 3d 20 61 70 50 61 67 65 5b 69 5d 3b 0a 20 20 20  = apPage[i];.   
17bba 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
17bbb 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 20   pPage->pBt;.   
17bbc 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
17bbd 69 73 49 6e 69 74 20 29 3b 0a 0a 20 20 20 20 66  isInit );..    f
17bbe 6f 72 28 6a 3d 30 3b 20 6a 3c 70 50 61 67 65 2d  or(j=0; j<pPage-
17bbf 3e 6e 43 65 6c 6c 3b 20 6a 2b 2b 29 7b 0a 20 20  >nCell; j++){.  
17bc0 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66      CellInfo inf
17bc1 6f 3b 0a 20 20 20 20 20 20 75 38 20 2a 7a 3b 0a  o;.      u8 *z;.
17bc2 20 20 20 20 20 0a 20 20 20 20 20 20 7a 20 3d 20       .      z = 
17bc3 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20  findCell(pPage, 
17bc4 6a 29 3b 0a 20 20 20 20 20 20 62 74 72 65 65 50  j);.      btreeP
17bc5 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67  arseCellPtr(pPag
17bc6 65 2c 20 7a 2c 20 26 69 6e 66 6f 29 3b 0a 20 20  e, z, &info);.  
17bc7 20 20 20 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76      if( info.iOv
17bc8 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20  erflow ){.      
17bc9 20 20 50 67 6e 6f 20 6f 76 66 6c 20 3d 20 67 65    Pgno ovfl = ge
17bca 74 34 62 79 74 65 28 26 7a 5b 69 6e 66 6f 2e 69  t4byte(&z[info.i
17bcb 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 20 20  Overflow]);.    
17bcc 20 20 20 20 70 74 72 6d 61 70 47 65 74 28 70 42      ptrmapGet(pB
17bcd 74 2c 20 6f 76 66 6c 2c 20 26 65 2c 20 26 6e 29  t, ovfl, &e, &n)
17bce 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
17bcf 28 20 6e 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f  ( n==pPage->pgno
17bd0 20 26 26 20 65 3d 3d 50 54 52 4d 41 50 5f 4f 56   && e==PTRMAP_OV
17bd1 45 52 46 4c 4f 57 31 20 29 3b 0a 20 20 20 20 20  ERFLOW1 );.     
17bd2 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21 70 50   }.      if( !pP
17bd3 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  age->leaf ){.   
17bd4 20 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 20       Pgno child 
17bd5 3d 20 67 65 74 34 62 79 74 65 28 7a 29 3b 0a 20  = get4byte(z);. 
17bd6 20 20 20 20 20 20 20 70 74 72 6d 61 70 47 65 74         ptrmapGet
17bd7 28 70 42 74 2c 20 63 68 69 6c 64 2c 20 26 65 2c  (pBt, child, &e,
17bd8 20 26 6e 29 3b 0a 20 20 20 20 20 20 20 20 61 73   &n);.        as
17bd9 73 65 72 74 28 20 6e 3d 3d 70 50 61 67 65 2d 3e  sert( n==pPage->
17bda 70 67 6e 6f 20 26 26 20 65 3d 3d 50 54 52 4d 41  pgno && e==PTRMA
17bdb 50 5f 42 54 52 45 45 20 29 3b 0a 20 20 20 20 20  P_BTREE );.     
17bdc 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
17bdd 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b   !pPage->leaf ){
17bde 0a 20 20 20 20 20 20 50 67 6e 6f 20 63 68 69 6c  .      Pgno chil
17bdf 64 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50  d = get4byte(&pP
17be0 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65  age->aData[pPage
17be1 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b  ->hdrOffset+8]);
17be2 0a 20 20 20 20 20 20 70 74 72 6d 61 70 47 65 74  .      ptrmapGet
17be3 28 70 42 74 2c 20 63 68 69 6c 64 2c 20 26 65 2c  (pBt, child, &e,
17be4 20 26 6e 29 3b 0a 20 20 20 20 20 20 61 73 73 65   &n);.      asse
17be5 72 74 28 20 6e 3d 3d 70 50 61 67 65 2d 3e 70 67  rt( n==pPage->pg
17be6 6e 6f 20 26 26 20 65 3d 3d 50 54 52 4d 41 50 5f  no && e==PTRMAP_
17be7 42 54 52 45 45 20 29 3b 0a 20 20 20 20 7d 0a 20  BTREE );.    }. 
17be8 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d   }.  return 1;.}
17be9 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
17bea 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
17beb 75 73 65 64 20 74 6f 20 63 6f 70 79 20 74 68 65  used to copy the
17bec 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
17bed 20 62 2d 74 72 65 65 20 6e 6f 64 65 20 73 74 6f   b-tree node sto
17bee 72 65 64 20 0a 2a 2a 20 6f 6e 20 70 61 67 65 20  red .** on page 
17bef 70 46 72 6f 6d 20 74 6f 20 70 61 67 65 20 70 54  pFrom to page pT
17bf0 6f 2e 20 49 66 20 70 61 67 65 20 70 46 72 6f 6d  o. If page pFrom
17bf1 20 77 61 73 20 6e 6f 74 20 61 20 6c 65 61 66 20   was not a leaf 
17bf2 70 61 67 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68  page, then.** th
17bf3 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e  e pointer-map en
17bf4 74 72 69 65 73 20 66 6f 72 20 65 61 63 68 20 63  tries for each c
17bf5 68 69 6c 64 20 70 61 67 65 20 61 72 65 20 75 70  hild page are up
17bf6 64 61 74 65 64 20 73 6f 20 74 68 61 74 20 74 68  dated so that th
17bf7 65 0a 2a 2a 20 70 61 72 65 6e 74 20 70 61 67 65  e.** parent page
17bf8 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 70   stored in the p
17bf9 6f 69 6e 74 65 72 20 6d 61 70 20 69 73 20 70 61  ointer map is pa
17bfa 67 65 20 70 54 6f 2e 20 49 66 20 70 46 72 6f 6d  ge pTo. If pFrom
17bfb 20 63 6f 6e 74 61 69 6e 65 64 0a 2a 2a 20 61 6e   contained.** an
17bfc 79 20 63 65 6c 6c 73 20 77 69 74 68 20 6f 76 65  y cells with ove
17bfd 72 66 6c 6f 77 20 70 61 67 65 20 70 6f 69 6e 74  rflow page point
17bfe 65 72 73 2c 20 74 68 65 6e 20 74 68 65 20 63 6f  ers, then the co
17bff 72 72 65 73 70 6f 6e 64 69 6e 67 20 70 6f 69 6e  rresponding poin
17c00 74 65 72 0a 2a 2a 20 6d 61 70 20 65 6e 74 72 69  ter.** map entri
17c01 65 73 20 61 72 65 20 61 6c 73 6f 20 75 70 64 61  es are also upda
17c02 74 65 64 20 73 6f 20 74 68 61 74 20 74 68 65 20  ted so that the 
17c03 70 61 72 65 6e 74 20 70 61 67 65 20 69 73 20 70  parent page is p
17c04 61 67 65 20 70 54 6f 2e 0a 2a 2a 0a 2a 2a 20 49  age pTo..**.** I
17c05 66 20 70 46 72 6f 6d 20 69 73 20 63 75 72 72 65  f pFrom is curre
17c06 6e 74 6c 79 20 63 61 72 72 79 69 6e 67 20 61 6e  ntly carrying an
17c07 79 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73  y overflow cells
17c08 20 28 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65   (entries in the
17c09 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 4f 76 66  .** MemPage.aOvf
17c0a 6c 5b 5d 20 61 72 72 61 79 29 2c 20 74 68 65 79  l[] array), they
17c0b 20 61 72 65 20 6e 6f 74 20 63 6f 70 69 65 64 20   are not copied 
17c0c 74 6f 20 70 54 6f 2e 20 0a 2a 2a 0a 2a 2a 20 42  to pTo. .**.** B
17c0d 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2c  efore returning,
17c0e 20 70 61 67 65 20 70 54 6f 20 69 73 20 72 65 69   page pTo is rei
17c0f 6e 69 74 69 61 6c 69 7a 65 64 20 75 73 69 6e 67  nitialized using
17c10 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 29   btreeInitPage()
17c11 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 65 72 66  ..**.** The perf
17c12 6f 72 6d 61 6e 63 65 20 6f 66 20 74 68 69 73 20  ormance of this 
17c13 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20  function is not 
17c14 63 72 69 74 69 63 61 6c 2e 20 49 74 20 69 73 20  critical. It is 
17c15 6f 6e 6c 79 20 75 73 65 64 20 62 79 20 0a 2a 2a  only used by .**
17c16 20 74 68 65 20 62 61 6c 61 6e 63 65 5f 73 68 61   the balance_sha
17c17 6c 6c 6f 77 65 72 28 29 20 61 6e 64 20 62 61 6c  llower() and bal
17c18 61 6e 63 65 5f 64 65 65 70 65 72 28 29 20 70 72  ance_deeper() pr
17c19 6f 63 65 64 75 72 65 73 2c 20 6e 65 69 74 68 65  ocedures, neithe
17c1a 72 20 6f 66 0a 2a 2a 20 77 68 69 63 68 20 61 72  r of.** which ar
17c1b 65 20 63 61 6c 6c 65 64 20 6f 66 74 65 6e 20 75  e called often u
17c1c 6e 64 65 72 20 6e 6f 72 6d 61 6c 20 63 69 72 63  nder normal circ
17c1d 75 6d 73 74 61 6e 63 65 73 2e 0a 2a 2f 0a 73 74  umstances..*/.st
17c1e 61 74 69 63 20 76 6f 69 64 20 63 6f 70 79 4e 6f  atic void copyNo
17c1f 64 65 43 6f 6e 74 65 6e 74 28 4d 65 6d 50 61 67  deContent(MemPag
17c20 65 20 2a 70 46 72 6f 6d 2c 20 4d 65 6d 50 61 67  e *pFrom, MemPag
17c21 65 20 2a 70 54 6f 2c 20 69 6e 74 20 2a 70 52 43  e *pTo, int *pRC
17c22 29 7b 0a 20 20 69 66 28 20 28 2a 70 52 43 29 3d  ){.  if( (*pRC)=
17c23 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
17c24 20 20 42 74 53 68 61 72 65 64 20 2a 20 63 6f 6e    BtShared * con
17c25 73 74 20 70 42 74 20 3d 20 70 46 72 6f 6d 2d 3e  st pBt = pFrom->
17c26 70 42 74 3b 0a 20 20 20 20 75 38 20 2a 20 63 6f  pBt;.    u8 * co
17c27 6e 73 74 20 61 46 72 6f 6d 20 3d 20 70 46 72 6f  nst aFrom = pFro
17c28 6d 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 75 38  m->aData;.    u8
17c29 20 2a 20 63 6f 6e 73 74 20 61 54 6f 20 3d 20 70   * const aTo = p
17c2a 54 6f 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 69  To->aData;.    i
17c2b 6e 74 20 63 6f 6e 73 74 20 69 46 72 6f 6d 48 64  nt const iFromHd
17c2c 72 20 3d 20 70 46 72 6f 6d 2d 3e 68 64 72 4f 66  r = pFrom->hdrOf
17c2d 66 73 65 74 3b 0a 20 20 20 20 69 6e 74 20 63 6f  fset;.    int co
17c2e 6e 73 74 20 69 54 6f 48 64 72 20 3d 20 28 28 70  nst iToHdr = ((p
17c2f 54 6f 2d 3e 70 67 6e 6f 3d 3d 31 29 20 3f 20 31  To->pgno==1) ? 1
17c30 30 30 20 3a 20 30 29 3b 0a 20 20 20 20 54 45 53  00 : 0);.    TES
17c31 54 4f 4e 4c 59 28 69 6e 74 20 72 63 3b 29 0a 20  TONLY(int rc;). 
17c32 20 20 20 69 6e 74 20 69 44 61 74 61 3b 0a 20 20     int iData;.  
17c33 0a 20 20 0a 20 20 20 20 61 73 73 65 72 74 28 20  .  .    assert( 
17c34 70 46 72 6f 6d 2d 3e 69 73 49 6e 69 74 20 29 3b  pFrom->isInit );
17c35 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 72  .    assert( pFr
17c36 6f 6d 2d 3e 6e 46 72 65 65 3e 3d 69 54 6f 48 64  om->nFree>=iToHd
17c37 72 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  r );.    assert(
17c38 20 67 65 74 32 62 79 74 65 28 26 61 46 72 6f 6d   get2byte(&aFrom
17c39 5b 69 46 72 6f 6d 48 64 72 2b 35 5d 29 3c 3d 70  [iFromHdr+5])<=p
17c3a 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29  Bt->usableSize )
17c3b 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 43 6f 70 79  ;.  .    /* Copy
17c3c 20 74 68 65 20 62 2d 74 72 65 65 20 6e 6f 64 65   the b-tree node
17c3d 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 70 61   content from pa
17c3e 67 65 20 70 46 72 6f 6d 20 74 6f 20 70 61 67 65  ge pFrom to page
17c3f 20 70 54 6f 2e 20 2a 2f 0a 20 20 20 20 69 44 61   pTo. */.    iDa
17c40 74 61 20 3d 20 67 65 74 32 62 79 74 65 28 26 61  ta = get2byte(&a
17c41 46 72 6f 6d 5b 69 46 72 6f 6d 48 64 72 2b 35 5d  From[iFromHdr+5]
17c42 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 61  );.    memcpy(&a
17c43 54 6f 5b 69 44 61 74 61 5d 2c 20 26 61 46 72 6f  To[iData], &aFro
17c44 6d 5b 69 44 61 74 61 5d 2c 20 70 42 74 2d 3e 75  m[iData], pBt->u
17c45 73 61 62 6c 65 53 69 7a 65 2d 69 44 61 74 61 29  sableSize-iData)
17c46 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 61 54  ;.    memcpy(&aT
17c47 6f 5b 69 54 6f 48 64 72 5d 2c 20 26 61 46 72 6f  o[iToHdr], &aFro
17c48 6d 5b 69 46 72 6f 6d 48 64 72 5d 2c 20 70 46 72  m[iFromHdr], pFr
17c49 6f 6d 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 2b  om->cellOffset +
17c4a 20 32 2a 70 46 72 6f 6d 2d 3e 6e 43 65 6c 6c 29   2*pFrom->nCell)
17c4b 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 52 65 69 6e  ;.  .    /* Rein
17c4c 69 74 69 61 6c 69 7a 65 20 70 61 67 65 20 70 54  itialize page pT
17c4d 6f 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 6f  o so that the co
17c4e 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 4d 65  ntents of the Me
17c4f 6d 50 61 67 65 20 73 74 72 75 63 74 75 72 65 0a  mPage structure.
17c50 20 20 20 20 2a 2a 20 6d 61 74 63 68 20 74 68 65      ** match the
17c51 20 6e 65 77 20 64 61 74 61 2e 20 54 68 65 20 69   new data. The i
17c52 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66  nitialization of
17c53 20 70 54 6f 20 22 63 61 6e 6e 6f 74 22 20 66 61   pTo "cannot" fa
17c54 69 6c 2c 20 61 73 20 74 68 65 0a 20 20 20 20 2a  il, as the.    *
17c55 2a 20 64 61 74 61 20 63 6f 70 69 65 64 20 66 72  * data copied fr
17c56 6f 6d 20 70 46 72 6f 6d 20 69 73 20 6b 6e 6f 77  om pFrom is know
17c57 6e 20 74 6f 20 62 65 20 76 61 6c 69 64 2e 20 20  n to be valid.  
17c58 2a 2f 0a 20 20 20 20 70 54 6f 2d 3e 69 73 49 6e  */.    pTo->isIn
17c59 69 74 20 3d 20 30 3b 0a 20 20 20 20 54 45 53 54  it = 0;.    TEST
17c5a 4f 4e 4c 59 28 72 63 20 3d 20 29 20 62 74 72 65  ONLY(rc = ) btre
17c5b 65 49 6e 69 74 50 61 67 65 28 70 54 6f 29 3b 0a  eInitPage(pTo);.
17c5c 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d      assert( rc==
17c5d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 0a  SQLITE_OK );.  .
17c5e 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69      /* If this i
17c5f 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d  s an auto-vacuum
17c60 20 64 61 74 61 62 61 73 65 2c 20 75 70 64 61 74   database, updat
17c61 65 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61  e the pointer-ma
17c62 70 20 65 6e 74 72 69 65 73 0a 20 20 20 20 2a 2a  p entries.    **
17c63 20 66 6f 72 20 61 6e 79 20 62 2d 74 72 65 65 20   for any b-tree 
17c64 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  or overflow page
17c65 73 20 74 68 61 74 20 70 54 6f 20 6e 6f 77 20 63  s that pTo now c
17c66 6f 6e 74 61 69 6e 73 20 74 68 65 20 70 6f 69 6e  ontains the poin
17c67 74 65 72 73 20 74 6f 2e 0a 20 20 20 20 2a 2f 0a  ters to..    */.
17c68 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41      if( ISAUTOVA
17c69 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 2a 70  CUUM ){.      *p
17c6a 52 43 20 3d 20 73 65 74 43 68 69 6c 64 50 74 72  RC = setChildPtr
17c6b 6d 61 70 73 28 70 54 6f 29 3b 0a 20 20 20 20 7d  maps(pTo);.    }
17c6c 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
17c6d 69 73 20 72 6f 75 74 69 6e 65 20 72 65 64 69 73  is routine redis
17c6e 74 72 69 62 75 74 65 73 20 63 65 6c 6c 73 20 6f  tributes cells o
17c6f 6e 20 74 68 65 20 69 50 61 72 65 6e 74 49 64 78  n the iParentIdx
17c70 27 74 68 20 63 68 69 6c 64 20 6f 66 20 70 50 61  'th child of pPa
17c71 72 65 6e 74 0a 2a 2a 20 28 68 65 72 65 61 66 74  rent.** (hereaft
17c72 65 72 20 22 74 68 65 20 70 61 67 65 22 29 20 61  er "the page") a
17c73 6e 64 20 75 70 20 74 6f 20 32 20 73 69 62 6c 69  nd up to 2 sibli
17c74 6e 67 73 20 73 6f 20 74 68 61 74 20 61 6c 6c 20  ngs so that all 
17c75 70 61 67 65 73 20 68 61 76 65 20 61 62 6f 75 74  pages have about
17c76 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 61 6d 6f   the.** same amo
17c77 75 6e 74 20 6f 66 20 66 72 65 65 20 73 70 61 63  unt of free spac
17c78 65 2e 20 55 73 75 61 6c 6c 79 20 61 20 73 69 6e  e. Usually a sin
17c79 67 6c 65 20 73 69 62 6c 69 6e 67 20 6f 6e 20 65  gle sibling on e
17c7a 69 74 68 65 72 20 73 69 64 65 20 6f 66 20 74 68  ither side of th
17c7b 65 0a 2a 2a 20 70 61 67 65 20 61 72 65 20 75 73  e.** page are us
17c7c 65 64 20 69 6e 20 74 68 65 20 62 61 6c 61 6e 63  ed in the balanc
17c7d 69 6e 67 2c 20 74 68 6f 75 67 68 20 62 6f 74 68  ing, though both
17c7e 20 73 69 62 6c 69 6e 67 73 20 6d 69 67 68 74 20   siblings might 
17c7f 63 6f 6d 65 20 66 72 6f 6d 20 6f 6e 65 0a 2a 2a  come from one.**
17c80 20 73 69 64 65 20 69 66 20 74 68 65 20 70 61 67   side if the pag
17c81 65 20 69 73 20 74 68 65 20 66 69 72 73 74 20 6f  e is the first o
17c82 72 20 6c 61 73 74 20 63 68 69 6c 64 20 6f 66 20  r last child of 
17c83 69 74 73 20 70 61 72 65 6e 74 2e 20 49 66 20 74  its parent. If t
17c84 68 65 20 70 61 67 65 20 0a 2a 2a 20 68 61 73 20  he page .** has 
17c85 66 65 77 65 72 20 74 68 61 6e 20 32 20 73 69 62  fewer than 2 sib
17c86 6c 69 6e 67 73 20 28 73 6f 6d 65 74 68 69 6e 67  lings (something
17c87 20 77 68 69 63 68 20 63 61 6e 20 6f 6e 6c 79 20   which can only 
17c88 68 61 70 70 65 6e 20 69 66 20 74 68 65 20 70 61  happen if the pa
17c89 67 65 0a 2a 2a 20 69 73 20 61 20 72 6f 6f 74 20  ge.** is a root 
17c8a 70 61 67 65 20 6f 72 20 61 20 63 68 69 6c 64 20  page or a child 
17c8b 6f 66 20 61 20 72 6f 6f 74 20 70 61 67 65 29 20  of a root page) 
17c8c 74 68 65 6e 20 61 6c 6c 20 61 76 61 69 6c 61 62  then all availab
17c8d 6c 65 20 73 69 62 6c 69 6e 67 73 0a 2a 2a 20 70  le siblings.** p
17c8e 61 72 74 69 63 69 70 61 74 65 20 69 6e 20 74 68  articipate in th
17c8f 65 20 62 61 6c 61 6e 63 69 6e 67 2e 0a 2a 2a 0a  e balancing..**.
17c90 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66  ** The number of
17c91 20 73 69 62 6c 69 6e 67 73 20 6f 66 20 74 68 65   siblings of the
17c92 20 70 61 67 65 20 6d 69 67 68 74 20 62 65 20 69   page might be i
17c93 6e 63 72 65 61 73 65 64 20 6f 72 20 64 65 63 72  ncreased or decr
17c94 65 61 73 65 64 20 62 79 20 0a 2a 2a 20 6f 6e 65  eased by .** one
17c95 20 6f 72 20 74 77 6f 20 69 6e 20 61 6e 20 65 66   or two in an ef
17c96 66 6f 72 74 20 74 6f 20 6b 65 65 70 20 70 61 67  fort to keep pag
17c97 65 73 20 6e 65 61 72 6c 79 20 66 75 6c 6c 20 62  es nearly full b
17c98 75 74 20 6e 6f 74 20 6f 76 65 72 20 66 75 6c 6c  ut not over full
17c99 2e 20 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68  . .**.** Note th
17c9a 61 74 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75  at when this rou
17c9b 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20  tine is called, 
17c9c 73 6f 6d 65 20 6f 66 20 74 68 65 20 63 65 6c 6c  some of the cell
17c9d 73 20 6f 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a  s on the page.**
17c9e 20 6d 69 67 68 74 20 6e 6f 74 20 61 63 74 75 61   might not actua
17c9f 6c 6c 79 20 62 65 20 73 74 6f 72 65 64 20 69 6e  lly be stored in
17ca0 20 4d 65 6d 50 61 67 65 2e 61 44 61 74 61 5b 5d   MemPage.aData[]
17ca1 2e 20 54 68 69 73 20 63 61 6e 20 68 61 70 70 65  . This can happe
17ca2 6e 0a 2a 2a 20 69 66 20 74 68 65 20 70 61 67 65  n.** if the page
17ca3 20 69 73 20 6f 76 65 72 66 75 6c 6c 2e 20 54 68   is overfull. Th
17ca4 69 73 20 72 6f 75 74 69 6e 65 20 65 6e 73 75 72  is routine ensur
17ca5 65 73 20 74 68 61 74 20 61 6c 6c 20 63 65 6c 6c  es that all cell
17ca6 73 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 74  s allocated.** t
17ca7 6f 20 74 68 65 20 70 61 67 65 20 61 6e 64 20 69  o the page and i
17ca8 74 73 20 73 69 62 6c 69 6e 67 73 20 66 69 74 20  ts siblings fit 
17ca9 69 6e 74 6f 20 4d 65 6d 50 61 67 65 2e 61 44 61  into MemPage.aDa
17caa 74 61 5b 5d 20 62 65 66 6f 72 65 20 72 65 74 75  ta[] before retu
17cab 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20  rning..**.** In 
17cac 74 68 65 20 63 6f 75 72 73 65 20 6f 66 20 62 61  the course of ba
17cad 6c 61 6e 63 69 6e 67 20 74 68 65 20 70 61 67 65  lancing the page
17cae 20 61 6e 64 20 69 74 73 20 73 69 62 6c 69 6e 67   and its sibling
17caf 73 2c 20 63 65 6c 6c 73 20 6d 61 79 20 62 65 0a  s, cells may be.
17cb0 2a 2a 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f  ** inserted into
17cb1 20 6f 72 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d   or removed from
17cb2 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65   the parent page
17cb3 20 28 70 50 61 72 65 6e 74 29 2e 20 44 6f 69 6e   (pParent). Doin
17cb4 67 20 73 6f 0a 2a 2a 20 6d 61 79 20 63 61 75 73  g so.** may caus
17cb5 65 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67  e the parent pag
17cb6 65 20 74 6f 20 62 65 63 6f 6d 65 20 6f 76 65 72  e to become over
17cb7 66 75 6c 6c 20 6f 72 20 75 6e 64 65 72 66 75 6c  full or underful
17cb8 6c 2e 20 49 66 20 74 68 69 73 0a 2a 2a 20 68 61  l. If this.** ha
17cb9 70 70 65 6e 73 2c 20 69 74 20 69 73 20 74 68 65  ppens, it is the
17cba 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20   responsibility 
17cbb 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f  of the caller to
17cbc 20 69 6e 76 6f 6b 65 20 74 68 65 20 63 6f 72 72   invoke the corr
17cbd 65 63 74 0a 2a 2a 20 62 61 6c 61 6e 63 69 6e 67  ect.** balancing
17cbe 20 72 6f 75 74 69 6e 65 20 74 6f 20 66 69 78 20   routine to fix 
17cbf 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 28 73 65  this problem (se
17cc0 65 20 74 68 65 20 62 61 6c 61 6e 63 65 28 29 20  e the balance() 
17cc1 72 6f 75 74 69 6e 65 29 2e 20 0a 2a 2a 0a 2a 2a  routine). .**.**
17cc2 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   If this routine
17cc3 20 66 61 69 6c 73 20 66 6f 72 20 61 6e 79 20 72   fails for any r
17cc4 65 61 73 6f 6e 2c 20 69 74 20 6d 69 67 68 74 20  eason, it might 
17cc5 6c 65 61 76 65 20 74 68 65 20 64 61 74 61 62 61  leave the databa
17cc6 73 65 0a 2a 2a 20 69 6e 20 61 20 63 6f 72 72 75  se.** in a corru
17cc7 70 74 65 64 20 73 74 61 74 65 2e 20 53 6f 20 69  pted state. So i
17cc8 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66  f this routine f
17cc9 61 69 6c 73 2c 20 74 68 65 20 64 61 74 61 62 61  ails, the databa
17cca 73 65 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20  se should.** be 
17ccb 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a  rolled back..**.
17ccc 2a 2a 20 54 68 65 20 74 68 69 72 64 20 61 72 67  ** The third arg
17ccd 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75  ument to this fu
17cce 6e 63 74 69 6f 6e 2c 20 61 4f 76 66 6c 53 70 61  nction, aOvflSpa
17ccf 63 65 2c 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ce, is a pointer
17cd0 20 74 6f 20 61 0a 2a 2a 20 62 75 66 66 65 72 20   to a.** buffer 
17cd1 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f  big enough to ho
17cd2 6c 64 20 6f 6e 65 20 70 61 67 65 2e 20 49 66 20  ld one page. If 
17cd3 77 68 69 6c 65 20 69 6e 73 65 72 74 69 6e 67 20  while inserting 
17cd4 63 65 6c 6c 73 20 69 6e 74 6f 20 74 68 65 20 70  cells into the p
17cd5 61 72 65 6e 74 0a 2a 2a 20 70 61 67 65 20 28 70  arent.** page (p
17cd6 50 61 72 65 6e 74 29 20 74 68 65 20 70 61 72 65  Parent) the pare
17cd7 6e 74 20 70 61 67 65 20 62 65 63 6f 6d 65 73 20  nt page becomes 
17cd8 6f 76 65 72 66 75 6c 6c 2c 20 74 68 69 73 20 62  overfull, this b
17cd9 75 66 66 65 72 20 69 73 0a 2a 2a 20 75 73 65 64  uffer is.** used
17cda 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 70 61   to store the pa
17cdb 72 65 6e 74 27 73 20 6f 76 65 72 66 6c 6f 77 20  rent's overflow 
17cdc 63 65 6c 6c 73 2e 20 42 65 63 61 75 73 65 20 74  cells. Because t
17cdd 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e 73  his function ins
17cde 65 72 74 73 0a 2a 2a 20 61 20 6d 61 78 69 6d 75  erts.** a maximu
17cdf 6d 20 6f 66 20 66 6f 75 72 20 64 69 76 69 64 65  m of four divide
17ce0 72 20 63 65 6c 6c 73 20 69 6e 74 6f 20 74 68 65  r cells into the
17ce1 20 70 61 72 65 6e 74 20 70 61 67 65 2c 20 61 6e   parent page, an
17ce2 64 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a  d the maximum.**
17ce3 20 73 69 7a 65 20 6f 66 20 61 20 63 65 6c 6c 20   size of a cell 
17ce4 73 74 6f 72 65 64 20 77 69 74 68 69 6e 20 61 6e  stored within an
17ce5 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 69   internal node i
17ce6 73 20 61 6c 77 61 79 73 20 6c 65 73 73 20 74 68  s always less th
17ce7 61 6e 20 31 2f 34 0a 2a 2a 20 6f 66 20 74 68 65  an 1/4.** of the
17ce8 20 70 61 67 65 2d 73 69 7a 65 2c 20 74 68 65 20   page-size, the 
17ce9 61 4f 76 66 6c 53 70 61 63 65 5b 5d 20 62 75 66  aOvflSpace[] buf
17cea 66 65 72 20 69 73 20 67 75 61 72 61 6e 74 65 65  fer is guarantee
17ceb 64 20 74 6f 20 62 65 20 6c 61 72 67 65 0a 2a 2a  d to be large.**
17cec 20 65 6e 6f 75 67 68 20 66 6f 72 20 61 6c 6c 20   enough for all 
17ced 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2e 0a  overflow cells..
17cee 2a 2a 0a 2a 2a 20 49 66 20 61 4f 76 66 6c 53 70  **.** If aOvflSp
17cef 61 63 65 20 69 73 20 73 65 74 20 74 6f 20 61 20  ace is set to a 
17cf0 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 2c 20 74 68  null pointer, th
17cf1 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  is function retu
17cf2 72 6e 73 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e  rns .** SQLITE_N
17cf3 4f 4d 45 4d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  OMEM..*/.static 
17cf4 69 6e 74 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72  int balance_nonr
17cf5 6f 6f 74 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a  oot(.  MemPage *
17cf6 70 50 61 72 65 6e 74 2c 20 20 20 20 20 20 20 20  pParent,        
17cf7 20 20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74         /* Parent
17cf8 20 70 61 67 65 20 6f 66 20 73 69 62 6c 69 6e 67   page of sibling
17cf9 73 20 62 65 69 6e 67 20 62 61 6c 61 6e 63 65 64  s being balanced
17cfa 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 72 65 6e   */.  int iParen
17cfb 74 49 64 78 2c 20 20 20 20 20 20 20 20 20 20 20  tIdx,           
17cfc 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
17cfd 66 20 22 74 68 65 20 70 61 67 65 22 20 69 6e 20  f "the page" in 
17cfe 70 50 61 72 65 6e 74 20 2a 2f 0a 20 20 75 38 20  pParent */.  u8 
17cff 2a 61 4f 76 66 6c 53 70 61 63 65 2c 20 20 20 20  *aOvflSpace,    
17d00 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
17d01 70 61 67 65 2d 73 69 7a 65 20 62 79 74 65 73 20  page-size bytes 
17d02 6f 66 20 73 70 61 63 65 20 66 6f 72 20 70 61 72  of space for par
17d03 65 6e 74 20 6f 76 66 6c 20 2a 2f 0a 20 20 69 6e  ent ovfl */.  in
17d04 74 20 69 73 52 6f 6f 74 20 20 20 20 20 20 20 20  t isRoot        
17d05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
17d06 20 54 72 75 65 20 69 66 20 70 50 61 72 65 6e 74   True if pParent
17d07 20 69 73 20 61 20 72 6f 6f 74 2d 70 61 67 65 20   is a root-page 
17d08 2a 2f 0a 29 7b 0a 20 20 42 74 53 68 61 72 65 64  */.){.  BtShared
17d09 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20 20 20   *pBt;          
17d0a 20 20 20 20 20 2f 2a 20 54 68 65 20 77 68 6f 6c       /* The whol
17d0b 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  e database */.  
17d0c 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 30 3b 20 20  int nCell = 0;  
17d0d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
17d0e 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20  Number of cells 
17d0f 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 2a 2f 0a 20  in apCell[] */. 
17d10 20 69 6e 74 20 6e 4d 61 78 43 65 6c 6c 73 20 3d   int nMaxCells =
17d11 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a   0;           /*
17d12 20 41 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20   Allocated size 
17d13 6f 66 20 61 70 43 65 6c 6c 2c 20 73 7a 43 65 6c  of apCell, szCel
17d14 6c 2c 20 61 46 72 6f 6d 2e 20 2a 2f 0a 20 20 69  l, aFrom. */.  i
17d15 6e 74 20 6e 4e 65 77 20 3d 20 30 3b 20 20 20 20  nt nNew = 0;    
17d16 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
17d17 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
17d18 6e 20 61 70 4e 65 77 5b 5d 20 2a 2f 0a 20 20 69  n apNew[] */.  i
17d19 6e 74 20 6e 4f 6c 64 3b 20 20 20 20 20 20 20 20  nt nOld;        
17d1a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
17d1b 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
17d1c 6e 20 61 70 4f 6c 64 5b 5d 20 2a 2f 0a 20 20 69  n apOld[] */.  i
17d1d 6e 74 20 69 2c 20 6a 2c 20 6b 3b 20 20 20 20 20  nt i, j, k;     
17d1e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
17d1f 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a  oop counters */.
17d20 20 20 69 6e 74 20 6e 78 44 69 76 3b 20 20 20 20    int nxDiv;    
17d21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
17d22 2a 20 4e 65 78 74 20 64 69 76 69 64 65 72 20 73  * Next divider s
17d23 6c 6f 74 20 69 6e 20 70 50 61 72 65 6e 74 2d 3e  lot in pParent->
17d24 61 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74  aCell[] */.  int
17d25 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
17d26 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
17d27 20 72 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   return code */.
17d28 20 20 75 31 36 20 6c 65 61 66 43 6f 72 72 65 63    u16 leafCorrec
17d29 74 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 2f  tion;          /
17d2a 2a 20 34 20 69 66 20 70 50 61 67 65 20 69 73 20  * 4 if pPage is 
17d2b 61 20 6c 65 61 66 2e 20 20 30 20 69 66 20 6e 6f  a leaf.  0 if no
17d2c 74 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 61 66 44  t */.  int leafD
17d2d 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20  ata;            
17d2e 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70      /* True if p
17d2f 50 61 67 65 20 69 73 20 61 20 6c 65 61 66 20 6f  Page is a leaf o
17d30 66 20 61 20 4c 45 41 46 44 41 54 41 20 74 72 65  f a LEAFDATA tre
17d31 65 20 2a 2f 0a 20 20 69 6e 74 20 75 73 61 62 6c  e */.  int usabl
17d32 65 53 70 61 63 65 3b 20 20 20 20 20 20 20 20 20  eSpace;         
17d33 20 20 20 20 2f 2a 20 42 79 74 65 73 20 69 6e 20      /* Bytes in 
17d34 70 50 61 67 65 20 62 65 79 6f 6e 64 20 74 68 65  pPage beyond the
17d35 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e 74   header */.  int
17d36 20 70 61 67 65 46 6c 61 67 73 3b 20 20 20 20 20   pageFlags;     
17d37 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c            /* Val
17d38 75 65 20 6f 66 20 70 50 61 67 65 2d 3e 61 44 61  ue of pPage->aDa
17d39 74 61 5b 30 5d 20 2a 2f 0a 20 20 69 6e 74 20 73  ta[0] */.  int s
17d3a 75 62 74 6f 74 61 6c 3b 20 20 20 20 20 20 20 20  ubtotal;        
17d3b 20 20 20 20 20 20 20 20 2f 2a 20 53 75 62 74 6f          /* Subto
17d3c 74 61 6c 20 6f 66 20 62 79 74 65 73 20 69 6e 20  tal of bytes in 
17d3d 63 65 6c 6c 73 20 6f 6e 20 6f 6e 65 20 70 61 67  cells on one pag
17d3e 65 20 2a 2f 0a 20 20 69 6e 74 20 69 53 70 61 63  e */.  int iSpac
17d3f 65 31 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  e1 = 0;         
17d40 20 20 20 20 2f 2a 20 46 69 72 73 74 20 75 6e 75      /* First unu
17d41 73 65 64 20 62 79 74 65 20 6f 66 20 61 53 70 61  sed byte of aSpa
17d42 63 65 31 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69  ce1[] */.  int i
17d43 4f 76 66 6c 53 70 61 63 65 20 3d 20 30 3b 20 20  OvflSpace = 0;  
17d44 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
17d45 20 75 6e 75 73 65 64 20 62 79 74 65 20 6f 66 20   unused byte of 
17d46 61 4f 76 66 6c 53 70 61 63 65 5b 5d 20 2a 2f 0a  aOvflSpace[] */.
17d47 20 20 69 6e 74 20 73 7a 53 63 72 61 74 63 68 3b    int szScratch;
17d48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
17d49 2a 20 53 69 7a 65 20 6f 66 20 73 63 72 61 74 63  * Size of scratc
17d4a 68 20 6d 65 6d 6f 72 79 20 72 65 71 75 65 73 74  h memory request
17d4b 65 64 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20  ed */.  MemPage 
17d4c 2a 61 70 4f 6c 64 5b 4e 42 5d 3b 20 20 20 20 20  *apOld[NB];     
17d4d 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20 61 6e       /* pPage an
17d4e 64 20 75 70 20 74 6f 20 74 77 6f 20 73 69 62 6c  d up to two sibl
17d4f 69 6e 67 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67  ings */.  MemPag
17d50 65 20 2a 61 70 43 6f 70 79 5b 4e 42 5d 3b 20 20  e *apCopy[NB];  
17d51 20 20 20 20 20 20 20 2f 2a 20 50 72 69 76 61 74         /* Privat
17d52 65 20 63 6f 70 69 65 73 20 6f 66 20 61 70 4f 6c  e copies of apOl
17d53 64 5b 5d 20 70 61 67 65 73 20 2a 2f 0a 20 20 4d  d[] pages */.  M
17d54 65 6d 50 61 67 65 20 2a 61 70 4e 65 77 5b 4e 42  emPage *apNew[NB
17d55 2b 32 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 70  +2];        /* p
17d56 50 61 67 65 20 61 6e 64 20 75 70 20 74 6f 20 4e  Page and up to N
17d57 42 20 73 69 62 6c 69 6e 67 73 20 61 66 74 65 72  B siblings after
17d58 20 62 61 6c 61 6e 63 69 6e 67 20 2a 2f 0a 20 20   balancing */.  
17d59 75 38 20 2a 70 52 69 67 68 74 3b 20 20 20 20 20  u8 *pRight;     
17d5a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
17d5b 4c 6f 63 61 74 69 6f 6e 20 69 6e 20 70 61 72 65  Location in pare
17d5c 6e 74 20 6f 66 20 72 69 67 68 74 2d 73 69 62 6c  nt of right-sibl
17d5d 69 6e 67 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20  ing pointer */. 
17d5e 20 75 38 20 2a 61 70 44 69 76 5b 4e 42 2d 31 5d   u8 *apDiv[NB-1]
17d5f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
17d60 20 44 69 76 69 64 65 72 20 63 65 6c 6c 73 20 69   Divider cells i
17d61 6e 20 70 50 61 72 65 6e 74 20 2a 2f 0a 20 20 69  n pParent */.  i
17d62 6e 74 20 63 6e 74 4e 65 77 5b 4e 42 2b 32 5d 3b  nt cntNew[NB+2];
17d63 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
17d64 6e 64 65 78 20 69 6e 20 61 43 65 6c 6c 5b 5d 20  ndex in aCell[] 
17d65 6f 66 20 63 65 6c 6c 20 61 66 74 65 72 20 69 2d  of cell after i-
17d66 74 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74  th page */.  int
17d67 20 73 7a 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20 20   szNew[NB+2];   
17d68 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d            /* Com
17d69 62 69 6e 65 64 20 73 69 7a 65 20 6f 66 20 63 65  bined size of ce
17d6a 6c 6c 73 20 70 6c 61 63 65 20 6f 6e 20 69 2d 74  lls place on i-t
17d6b 68 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a  h page */.  u8 *
17d6c 2a 61 70 43 65 6c 6c 20 3d 20 30 3b 20 20 20 20  *apCell = 0;    
17d6d 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20           /* All 
17d6e 63 65 6c 6c 73 20 62 65 67 69 6e 20 62 61 6c 61  cells begin bala
17d6f 6e 63 65 64 20 2a 2f 0a 20 20 75 31 36 20 2a 73  nced */.  u16 *s
17d70 7a 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20  zCell;          
17d71 20 20 20 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20         /* Local 
17d72 73 69 7a 65 20 6f 66 20 61 6c 6c 20 63 65 6c 6c  size of all cell
17d73 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 2a 2f  s in apCell[] */
17d74 0a 20 20 75 38 20 2a 61 53 70 61 63 65 31 3b 20  .  u8 *aSpace1; 
17d75 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17d76 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 63 6f 70  /* Space for cop
17d77 69 65 73 20 6f 66 20 64 69 76 69 64 65 72 73 20  ies of dividers 
17d78 63 65 6c 6c 73 20 2a 2f 0a 20 20 50 67 6e 6f 20  cells */.  Pgno 
17d79 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20  pgno;           
17d7a 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20          /* Temp 
17d7b 76 61 72 20 74 6f 20 73 74 6f 72 65 20 61 20 70  var to store a p
17d7c 61 67 65 20 6e 75 6d 62 65 72 20 69 6e 20 2a 2f  age number in */
17d7d 0a 0a 20 20 70 42 74 20 3d 20 70 50 61 72 65 6e  ..  pBt = pParen
17d7e 74 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74  t->pBt;.  assert
17d7f 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
17d80 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29  held(pBt->mutex)
17d81 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
17d82 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74  lite3PagerIswrit
17d83 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e 70  eable(pParent->p
17d84 44 62 50 61 67 65 29 20 29 3b 0a 0a 23 69 66 20  DbPage) );..#if 
17d85 30 0a 20 20 54 52 41 43 45 28 28 22 42 41 4c 41  0.  TRACE(("BALA
17d86 4e 43 45 3a 20 62 65 67 69 6e 20 70 61 67 65 20  NCE: begin page 
17d87 25 64 20 63 68 69 6c 64 20 6f 66 20 25 64 5c 6e  %d child of %d\n
17d88 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20  ", pPage->pgno, 
17d89 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 29 29 3b  pParent->pgno));
17d8a 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 74  .#endif..  /* At
17d8b 20 74 68 69 73 20 70 6f 69 6e 74 20 70 50 61 72   this point pPar
17d8c 65 6e 74 20 6d 61 79 20 68 61 76 65 20 61 74 20  ent may have at 
17d8d 6d 6f 73 74 20 6f 6e 65 20 6f 76 65 72 66 6c 6f  most one overflo
17d8e 77 20 63 65 6c 6c 2e 20 41 6e 64 20 69 66 0a 20  w cell. And if. 
17d8f 20 2a 2a 20 74 68 69 73 20 6f 76 65 72 66 6c 6f   ** this overflo
17d90 77 20 63 65 6c 6c 20 69 73 20 70 72 65 73 65 6e  w cell is presen
17d91 74 2c 20 69 74 20 6d 75 73 74 20 62 65 20 74 68  t, it must be th
17d92 65 20 63 65 6c 6c 20 77 69 74 68 20 0a 20 20 2a  e cell with .  *
17d93 2a 20 69 6e 64 65 78 20 69 50 61 72 65 6e 74 49  * index iParentI
17d94 64 78 2e 20 54 68 69 73 20 73 63 65 6e 61 72 69  dx. This scenari
17d95 6f 20 63 6f 6d 65 73 20 61 62 6f 75 74 20 77 68  o comes about wh
17d96 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
17d97 0a 20 20 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20  .  ** is called 
17d98 28 69 6e 64 69 72 65 63 74 6c 79 29 20 66 72 6f  (indirectly) fro
17d99 6d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65  m sqlite3BtreeDe
17d9a 6c 65 74 65 28 29 2e 0a 20 20 2a 2f 0a 20 20 61  lete()..  */.  a
17d9b 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 2d 3e  ssert( pParent->
17d9c 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20  nOverflow==0 || 
17d9d 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c  pParent->nOverfl
17d9e 6f 77 3d 3d 31 20 29 3b 0a 20 20 61 73 73 65 72  ow==1 );.  asser
17d9f 74 28 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65  t( pParent->nOve
17da0 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 70 50 61 72  rflow==0 || pPar
17da1 65 6e 74 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64  ent->aOvfl[0].id
17da2 78 3d 3d 69 50 61 72 65 6e 74 49 64 78 20 29 3b  x==iParentIdx );
17da3 0a 0a 20 20 69 66 28 20 21 61 4f 76 66 6c 53 70  ..  if( !aOvflSp
17da4 61 63 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ace ){.    retur
17da5 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
17da6 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 74    }..  /* Find t
17da7 68 65 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73  he sibling pages
17da8 20 74 6f 20 62 61 6c 61 6e 63 65 2e 20 41 6c 73   to balance. Als
17da9 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 63 65 6c  o locate the cel
17daa 6c 73 20 69 6e 20 70 50 61 72 65 6e 74 20 0a 20  ls in pParent . 
17dab 20 2a 2a 20 74 68 61 74 20 64 69 76 69 64 65 20   ** that divide 
17dac 74 68 65 20 73 69 62 6c 69 6e 67 73 2e 20 41 6e  the siblings. An
17dad 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65   attempt is made
17dae 20 74 6f 20 66 69 6e 64 20 4e 4e 20 73 69 62 6c   to find NN sibl
17daf 69 6e 67 73 20 6f 6e 20 0a 20 20 2a 2a 20 65 69  ings on .  ** ei
17db0 74 68 65 72 20 73 69 64 65 20 6f 66 20 70 50 61  ther side of pPa
17db1 67 65 2e 20 4d 6f 72 65 20 73 69 62 6c 69 6e 67  ge. More sibling
17db2 73 20 61 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d  s are taken from
17db3 20 6f 6e 65 20 73 69 64 65 2c 20 68 6f 77 65 76   one side, howev
17db4 65 72 2c 20 0a 20 20 2a 2a 20 69 66 20 74 68 65  er, .  ** if the
17db5 72 65 20 61 72 65 20 66 65 77 65 72 20 74 68 61  re are fewer tha
17db6 6e 20 4e 4e 20 73 69 62 6c 69 6e 67 73 20 6f 6e  n NN siblings on
17db7 20 74 68 65 20 6f 74 68 65 72 20 73 69 64 65 2e   the other side.
17db8 20 49 66 20 70 50 61 72 65 6e 74 0a 20 20 2a 2a   If pParent.  **
17db9 20 68 61 73 20 4e 42 20 6f 72 20 66 65 77 65 72   has NB or fewer
17dba 20 63 68 69 6c 64 72 65 6e 20 74 68 65 6e 20 61   children then a
17dbb 6c 6c 20 63 68 69 6c 64 72 65 6e 20 6f 66 20 70  ll children of p
17dbc 50 61 72 65 6e 74 20 61 72 65 20 74 61 6b 65 6e  Parent are taken
17dbd 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  .  .  **.  ** Th
17dbe 69 73 20 6c 6f 6f 70 20 61 6c 73 6f 20 64 72 6f  is loop also dro
17dbf 70 73 20 74 68 65 20 64 69 76 69 64 65 72 20 63  ps the divider c
17dc0 65 6c 6c 73 20 66 72 6f 6d 20 74 68 65 20 70 61  ells from the pa
17dc1 72 65 6e 74 20 70 61 67 65 2e 20 54 68 69 73 0a  rent page. This.
17dc2 20 20 2a 2a 20 77 61 79 2c 20 74 68 65 20 72 65    ** way, the re
17dc3 6d 61 69 6e 64 65 72 20 6f 66 20 74 68 65 20 66  mainder of the f
17dc4 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74  unction does not
17dc5 20 68 61 76 65 20 74 6f 20 64 65 61 6c 20 77 69   have to deal wi
17dc6 74 68 20 61 6e 79 0a 20 20 2a 2a 20 6f 76 65 72  th any.  ** over
17dc7 66 6c 6f 77 20 63 65 6c 6c 73 20 69 6e 20 74 68  flow cells in th
17dc8 65 20 70 61 72 65 6e 74 20 70 61 67 65 2c 20 73  e parent page, s
17dc9 69 6e 63 65 20 69 66 20 61 6e 79 20 65 78 69 73  ince if any exis
17dca 74 65 64 20 74 68 65 79 20 77 69 6c 6c 0a 20 20  ted they will.  
17dcb 2a 2a 20 68 61 76 65 20 61 6c 72 65 61 64 79 20  ** have already 
17dcc 62 65 65 6e 20 72 65 6d 6f 76 65 64 2e 0a 20 20  been removed..  
17dcd 2a 2f 0a 20 20 69 20 3d 20 70 50 61 72 65 6e 74  */.  i = pParent
17dce 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 2b 20 70 50  ->nOverflow + pP
17dcf 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3b 0a 20 20  arent->nCell;.  
17dd0 69 66 28 20 69 3c 32 20 29 7b 0a 20 20 20 20 6e  if( i<2 ){.    n
17dd1 78 44 69 76 20 3d 20 30 3b 0a 20 20 20 20 6e 4f  xDiv = 0;.    nO
17dd2 6c 64 20 3d 20 69 2b 31 3b 0a 20 20 7d 65 6c 73  ld = i+1;.  }els
17dd3 65 7b 0a 20 20 20 20 6e 4f 6c 64 20 3d 20 33 3b  e{.    nOld = 3;
17dd4 0a 20 20 20 20 69 66 28 20 69 50 61 72 65 6e 74  .    if( iParent
17dd5 49 64 78 3d 3d 30 20 29 7b 20 20 20 20 20 20 20  Idx==0 ){       
17dd6 20 20 20 20 20 20 20 20 20 20 0a 20 20 20 20 20            .     
17dd7 20 6e 78 44 69 76 20 3d 20 30 3b 0a 20 20 20 20   nxDiv = 0;.    
17dd8 7d 65 6c 73 65 20 69 66 28 20 69 50 61 72 65 6e  }else if( iParen
17dd9 74 49 64 78 3d 3d 69 20 29 7b 0a 20 20 20 20 20  tIdx==i ){.     
17dda 20 6e 78 44 69 76 20 3d 20 69 2d 32 3b 0a 20 20   nxDiv = i-2;.  
17ddb 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e    }else{.      n
17ddc 78 44 69 76 20 3d 20 69 50 61 72 65 6e 74 49 64  xDiv = iParentId
17ddd 78 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  x-1;.    }.    i
17dde 20 3d 20 32 3b 0a 20 20 7d 0a 20 20 69 66 28 20   = 2;.  }.  if( 
17ddf 28 69 2b 6e 78 44 69 76 2d 70 50 61 72 65 6e 74  (i+nxDiv-pParent
17de0 2d 3e 6e 4f 76 65 72 66 6c 6f 77 29 3d 3d 70 50  ->nOverflow)==pP
17de1 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a  arent->nCell ){.
17de2 20 20 20 20 70 52 69 67 68 74 20 3d 20 26 70 50      pRight = &pP
17de3 61 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61  arent->aData[pPa
17de4 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b  rent->hdrOffset+
17de5 38 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  8];.  }else{.   
17de6 20 70 52 69 67 68 74 20 3d 20 66 69 6e 64 43 65   pRight = findCe
17de7 6c 6c 28 70 50 61 72 65 6e 74 2c 20 69 2b 6e 78  ll(pParent, i+nx
17de8 44 69 76 2d 70 50 61 72 65 6e 74 2d 3e 6e 4f 76  Div-pParent->nOv
17de9 65 72 66 6c 6f 77 29 3b 0a 20 20 7d 0a 20 20 70  erflow);.  }.  p
17dea 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 70  gno = get4byte(p
17deb 52 69 67 68 74 29 3b 0a 20 20 77 68 69 6c 65 28  Right);.  while(
17dec 20 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 67   1 ){.    rc = g
17ded 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42  etAndInitPage(pB
17dee 74 2c 20 70 67 6e 6f 2c 20 26 61 70 4f 6c 64 5b  t, pgno, &apOld[
17def 69 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20  i]);.    if( rc 
17df0 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28  ){.      memset(
17df1 61 70 4f 6c 64 2c 20 30 2c 20 28 69 2b 31 29 2a  apOld, 0, (i+1)*
17df2 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 2a 29  sizeof(MemPage*)
17df3 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 61  );.      goto ba
17df4 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20  lance_cleanup;. 
17df5 20 20 20 7d 0a 20 20 20 20 6e 4d 61 78 43 65 6c     }.    nMaxCel
17df6 6c 73 20 2b 3d 20 31 2b 61 70 4f 6c 64 5b 69 5d  ls += 1+apOld[i]
17df7 2d 3e 6e 43 65 6c 6c 2b 61 70 4f 6c 64 5b 69 5d  ->nCell+apOld[i]
17df8 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20  ->nOverflow;.   
17df9 20 69 66 28 20 28 69 2d 2d 29 3d 3d 30 20 29 20   if( (i--)==0 ) 
17dfa 62 72 65 61 6b 3b 0a 0a 20 20 20 20 69 66 28 20  break;..    if( 
17dfb 69 2b 6e 78 44 69 76 3d 3d 70 50 61 72 65 6e 74  i+nxDiv==pParent
17dfc 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 20 26  ->aOvfl[0].idx &
17dfd 26 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72  & pParent->nOver
17dfe 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 61 70  flow ){.      ap
17dff 44 69 76 5b 69 5d 20 3d 20 70 50 61 72 65 6e 74  Div[i] = pParent
17e00 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 70 43 65 6c 6c  ->aOvfl[0].pCell
17e01 3b 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d 20 67  ;.      pgno = g
17e02 65 74 34 62 79 74 65 28 61 70 44 69 76 5b 69 5d  et4byte(apDiv[i]
17e03 29 3b 0a 20 20 20 20 20 20 73 7a 4e 65 77 5b 69  );.      szNew[i
17e04 5d 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28  ] = cellSizePtr(
17e05 70 50 61 72 65 6e 74 2c 20 61 70 44 69 76 5b 69  pParent, apDiv[i
17e06 5d 29 3b 0a 20 20 20 20 20 20 70 50 61 72 65 6e  ]);.      pParen
17e07 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30  t->nOverflow = 0
17e08 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
17e09 20 20 20 61 70 44 69 76 5b 69 5d 20 3d 20 66 69     apDiv[i] = fi
17e0a 6e 64 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20  ndCell(pParent, 
17e0b 69 2b 6e 78 44 69 76 2d 70 50 61 72 65 6e 74 2d  i+nxDiv-pParent-
17e0c 3e 6e 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20 20  >nOverflow);.   
17e0d 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79     pgno = get4by
17e0e 74 65 28 61 70 44 69 76 5b 69 5d 29 3b 0a 20 20  te(apDiv[i]);.  
17e0f 20 20 20 20 73 7a 4e 65 77 5b 69 5d 20 3d 20 63      szNew[i] = c
17e10 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 72 65  ellSizePtr(pPare
17e11 6e 74 2c 20 61 70 44 69 76 5b 69 5d 29 3b 0a 0a  nt, apDiv[i]);..
17e12 20 20 20 20 20 20 2f 2a 20 44 72 6f 70 20 74 68        /* Drop th
17e13 65 20 63 65 6c 6c 20 66 72 6f 6d 20 74 68 65 20  e cell from the 
17e14 70 61 72 65 6e 74 20 70 61 67 65 2e 20 61 70 44  parent page. apD
17e15 69 76 5b 69 5d 20 73 74 69 6c 6c 20 70 6f 69 6e  iv[i] still poin
17e16 74 73 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74  ts to.      ** t
17e17 68 65 20 63 65 6c 6c 20 77 69 74 68 69 6e 20 74  he cell within t
17e18 68 65 20 70 61 72 65 6e 74 2c 20 65 76 65 6e 20  he parent, even 
17e19 74 68 6f 75 67 68 20 69 74 20 68 61 73 20 62 65  though it has be
17e1a 65 6e 20 64 72 6f 70 70 65 64 2e 0a 20 20 20 20  en dropped..    
17e1b 20 20 2a 2a 20 54 68 69 73 20 69 73 20 73 61 66    ** This is saf
17e1c 65 20 62 65 63 61 75 73 65 20 64 72 6f 70 70 69  e because droppi
17e1d 6e 67 20 61 20 63 65 6c 6c 20 6f 6e 6c 79 20 6f  ng a cell only o
17e1e 76 65 72 77 72 69 74 65 73 20 74 68 65 20 66 69  verwrites the fi
17e1f 72 73 74 0a 20 20 20 20 20 20 2a 2a 20 66 6f 75  rst.      ** fou
17e20 72 20 62 79 74 65 73 20 6f 66 20 69 74 2c 20 61  r bytes of it, a
17e21 6e 64 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  nd this function
17e22 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74   does not need t
17e23 68 65 20 66 69 72 73 74 0a 20 20 20 20 20 20 2a  he first.      *
17e24 2a 20 66 6f 75 72 20 62 79 74 65 73 20 6f 66 20  * four bytes of 
17e25 74 68 65 20 64 69 76 69 64 65 72 20 63 65 6c 6c  the divider cell
17e26 2e 20 53 6f 20 74 68 65 20 70 6f 69 6e 74 65 72  . So the pointer
17e27 20 69 73 20 73 61 66 65 20 74 6f 20 75 73 65 0a   is safe to use.
17e28 20 20 20 20 20 20 2a 2a 20 6c 61 74 65 72 20 6f        ** later o
17e29 6e 2e 20 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20  n.  .      **.  
17e2a 20 20 20 20 2a 2a 20 55 6e 6c 65 73 73 20 53 51      ** Unless SQ
17e2b 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64  Lite is compiled
17e2c 20 69 6e 20 73 65 63 75 72 65 2d 64 65 6c 65 74   in secure-delet
17e2d 65 20 6d 6f 64 65 2e 20 49 6e 20 74 68 69 73 20  e mode. In this 
17e2e 63 61 73 65 2c 0a 20 20 20 20 20 20 2a 2a 20 74  case,.      ** t
17e2f 68 65 20 64 72 6f 70 43 65 6c 6c 28 29 20 72 6f  he dropCell() ro
17e30 75 74 69 6e 65 20 77 69 6c 6c 20 6f 76 65 72 77  utine will overw
17e31 72 69 74 65 20 74 68 65 20 65 6e 74 69 72 65 20  rite the entire 
17e32 63 65 6c 6c 20 77 69 74 68 20 7a 65 72 6f 65 73  cell with zeroes
17e33 2e 0a 20 20 20 20 20 20 2a 2a 20 49 6e 20 74 68  ..      ** In th
17e34 69 73 20 63 61 73 65 2c 20 74 65 6d 70 6f 72 61  is case, tempora
17e35 72 69 6c 79 20 63 6f 70 79 20 74 68 65 20 63 65  rily copy the ce
17e36 6c 6c 20 69 6e 74 6f 20 74 68 65 20 61 4f 76 66  ll into the aOvf
17e37 6c 53 70 61 63 65 5b 5d 0a 20 20 20 20 20 20 2a  lSpace[].      *
17e38 2a 20 62 75 66 66 65 72 2e 20 49 74 20 77 69 6c  * buffer. It wil
17e39 6c 20 62 65 20 63 6f 70 69 65 64 20 6f 75 74 20  l be copied out 
17e3a 61 67 61 69 6e 20 61 73 20 73 6f 6f 6e 20 61 73  again as soon as
17e3b 20 74 68 65 20 61 53 70 61 63 65 5b 5d 20 62 75   the aSpace[] bu
17e3c 66 66 65 72 0a 20 20 20 20 20 20 2a 2a 20 69 73  ffer.      ** is
17e3d 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 2a 2f 0a   allocated.  */.
17e3e 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 45  #ifdef SQLITE_SE
17e3f 43 55 52 45 5f 44 45 4c 45 54 45 0a 20 20 20 20  CURE_DELETE.    
17e40 20 20 6d 65 6d 63 70 79 28 26 61 4f 76 66 6c 53    memcpy(&aOvflS
17e41 70 61 63 65 5b 61 70 44 69 76 5b 69 5d 2d 70 50  pace[apDiv[i]-pP
17e42 61 72 65 6e 74 2d 3e 61 44 61 74 61 5d 2c 20 61  arent->aData], a
17e43 70 44 69 76 5b 69 5d 2c 20 73 7a 4e 65 77 5b 69  pDiv[i], szNew[i
17e44 5d 29 3b 0a 20 20 20 20 20 20 61 70 44 69 76 5b  ]);.      apDiv[
17e45 69 5d 20 3d 20 26 61 4f 76 66 6c 53 70 61 63 65  i] = &aOvflSpace
17e46 5b 61 70 44 69 76 5b 69 5d 2d 70 50 61 72 65 6e  [apDiv[i]-pParen
17e47 74 2d 3e 61 44 61 74 61 5d 3b 0a 23 65 6e 64 69  t->aData];.#endi
17e48 66 0a 20 20 20 20 20 20 64 72 6f 70 43 65 6c 6c  f.      dropCell
17e49 28 70 50 61 72 65 6e 74 2c 20 69 2b 6e 78 44 69  (pParent, i+nxDi
17e4a 76 2d 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72  v-pParent->nOver
17e4b 66 6c 6f 77 2c 20 73 7a 4e 65 77 5b 69 5d 2c 20  flow, szNew[i], 
17e4c 26 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  &rc);.    }.  }.
17e4d 0a 20 20 2f 2a 20 4d 61 6b 65 20 6e 4d 61 78 43  .  /* Make nMaxC
17e4e 65 6c 6c 73 20 61 20 6d 75 6c 74 69 70 6c 65 20  ells a multiple 
17e4f 6f 66 20 34 20 69 6e 20 6f 72 64 65 72 20 74 6f  of 4 in order to
17e50 20 70 72 65 73 65 72 76 65 20 38 2d 62 79 74 65   preserve 8-byte
17e51 0a 20 20 2a 2a 20 61 6c 69 67 6e 6d 65 6e 74 20  .  ** alignment 
17e52 2a 2f 0a 20 20 6e 4d 61 78 43 65 6c 6c 73 20 3d  */.  nMaxCells =
17e53 20 28 6e 4d 61 78 43 65 6c 6c 73 20 2b 20 33 29   (nMaxCells + 3)
17e54 26 7e 33 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  &~3;..  /*.  ** 
17e55 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66  Allocate space f
17e56 6f 72 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 74  or memory struct
17e57 75 72 65 73 0a 20 20 2a 2f 0a 20 20 6b 20 3d 20  ures.  */.  k = 
17e58 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2b 20  pBt->pageSize + 
17e59 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 4d 65  ROUND8(sizeof(Me
17e5a 6d 50 61 67 65 29 29 3b 0a 20 20 73 7a 53 63 72  mPage));.  szScr
17e5b 61 74 63 68 20 3d 0a 20 20 20 20 20 20 20 6e 4d  atch =.       nM
17e5c 61 78 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 75  axCells*sizeof(u
17e5d 38 2a 29 20 20 20 20 20 20 20 20 20 20 20 20 20  8*)             
17e5e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 61 70 43            /* apC
17e5f 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 6e 4d  ell */.     + nM
17e60 61 78 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 75  axCells*sizeof(u
17e61 31 36 29 20 20 20 20 20 20 20 20 20 20 20 20 20  16)             
17e62 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 7a 43            /* szC
17e63 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 70 42  ell */.     + pB
17e64 74 2d 3e 70 61 67 65 53 69 7a 65 20 20 20 20 20  t->pageSize     
17e65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17e66 20 20 20 20 20 20 20 20 20 20 2f 2a 20 61 53 70            /* aSp
17e67 61 63 65 31 20 2a 2f 0a 20 20 20 20 20 2b 20 6b  ace1 */.     + k
17e68 2a 6e 4f 6c 64 3b 20 20 20 20 20 20 20 20 20 20  *nOld;          
17e69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17e6a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
17e6b 67 65 20 63 6f 70 69 65 73 20 28 61 70 43 6f 70  ge copies (apCop
17e6c 79 29 20 2a 2f 0a 20 20 61 70 43 65 6c 6c 20 3d  y) */.  apCell =
17e6d 20 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 4d   sqlite3ScratchM
17e6e 61 6c 6c 6f 63 28 20 73 7a 53 63 72 61 74 63 68  alloc( szScratch
17e6f 20 29 3b 20 0a 20 20 69 66 28 20 61 70 43 65 6c   ); .  if( apCel
17e70 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d  l==0 ){.    rc =
17e71 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
17e72 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f     goto balance_
17e73 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 73  cleanup;.  }.  s
17e74 7a 43 65 6c 6c 20 3d 20 28 75 31 36 2a 29 26 61  zCell = (u16*)&a
17e75 70 43 65 6c 6c 5b 6e 4d 61 78 43 65 6c 6c 73 5d  pCell[nMaxCells]
17e76 3b 0a 20 20 61 53 70 61 63 65 31 20 3d 20 28 75  ;.  aSpace1 = (u
17e77 38 2a 29 26 73 7a 43 65 6c 6c 5b 6e 4d 61 78 43  8*)&szCell[nMaxC
17e78 65 6c 6c 73 5d 3b 0a 20 20 61 73 73 65 72 74 28  ells];.  assert(
17e79 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47   EIGHT_BYTE_ALIG
17e7a 4e 4d 45 4e 54 28 61 53 70 61 63 65 31 29 20 29  NMENT(aSpace1) )
17e7b 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4c 6f 61  ;..  /*.  ** Loa
17e7c 64 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 61 6c  d pointers to al
17e7d 6c 20 63 65 6c 6c 73 20 6f 6e 20 73 69 62 6c 69  l cells on sibli
17e7e 6e 67 20 70 61 67 65 73 20 61 6e 64 20 74 68 65  ng pages and the
17e7f 20 64 69 76 69 64 65 72 20 63 65 6c 6c 73 0a 20   divider cells. 
17e80 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 6c 6f 63   ** into the loc
17e81 61 6c 20 61 70 43 65 6c 6c 5b 5d 20 61 72 72 61  al apCell[] arra
17e82 79 2e 20 20 4d 61 6b 65 20 63 6f 70 69 65 73 20  y.  Make copies 
17e83 6f 66 20 74 68 65 20 64 69 76 69 64 65 72 20 63  of the divider c
17e84 65 6c 6c 73 0a 20 20 2a 2a 20 69 6e 74 6f 20 73  ells.  ** into s
17e85 70 61 63 65 20 6f 62 74 61 69 6e 65 64 20 66 72  pace obtained fr
17e86 6f 6d 20 61 53 70 61 63 65 31 5b 5d 20 61 6e 64  om aSpace1[] and
17e87 20 72 65 6d 6f 76 65 20 74 68 65 20 74 68 65 20   remove the the 
17e88 64 69 76 69 64 65 72 20 43 65 6c 6c 73 0a 20 20  divider Cells.  
17e89 2a 2a 20 66 72 6f 6d 20 70 50 61 72 65 6e 74 2e  ** from pParent.
17e8a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68  .  **.  ** If th
17e8b 65 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6f  e siblings are o
17e8c 6e 20 6c 65 61 66 20 70 61 67 65 73 2c 20 74 68  n leaf pages, th
17e8d 65 6e 20 74 68 65 20 63 68 69 6c 64 20 70 6f 69  en the child poi
17e8e 6e 74 65 72 73 20 6f 66 20 74 68 65 0a 20 20 2a  nters of the.  *
17e8f 2a 20 64 69 76 69 64 65 72 20 63 65 6c 6c 73 20  * divider cells 
17e90 61 72 65 20 73 74 72 69 70 70 65 64 20 66 72 6f  are stripped fro
17e91 6d 20 74 68 65 20 63 65 6c 6c 73 20 62 65 66 6f  m the cells befo
17e92 72 65 20 74 68 65 79 20 61 72 65 20 63 6f 70 69  re they are copi
17e93 65 64 0a 20 20 2a 2a 20 69 6e 74 6f 20 61 53 70  ed.  ** into aSp
17e94 61 63 65 31 5b 5d 2e 20 20 49 6e 20 74 68 69 73  ace1[].  In this
17e95 20 77 61 79 2c 20 61 6c 6c 20 63 65 6c 6c 73 20   way, all cells 
17e96 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 61 72 65 20  in apCell[] are 
17e97 77 69 74 68 6f 75 74 0a 20 20 2a 2a 20 63 68 69  without.  ** chi
17e98 6c 64 20 70 6f 69 6e 74 65 72 73 2e 20 20 49 66  ld pointers.  If
17e99 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6e 6f   siblings are no
17e9a 74 20 6c 65 61 76 65 73 2c 20 74 68 65 6e 20 61  t leaves, then a
17e9b 6c 6c 20 63 65 6c 6c 20 69 6e 0a 20 20 2a 2a 20  ll cell in.  ** 
17e9c 61 70 43 65 6c 6c 5b 5d 20 69 6e 63 6c 75 64 65  apCell[] include
17e9d 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 73 2e   child pointers.
17e9e 20 20 45 69 74 68 65 72 20 77 61 79 2c 20 61 6c    Either way, al
17e9f 6c 20 63 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c  l cells in apCel
17ea0 6c 5b 5d 0a 20 20 2a 2a 20 61 72 65 20 61 6c 69  l[].  ** are ali
17ea1 6b 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 6c 65  ke..  **.  ** le
17ea2 61 66 43 6f 72 72 65 63 74 69 6f 6e 3a 20 20 34  afCorrection:  4
17ea3 20 69 66 20 70 50 61 67 65 20 69 73 20 61 20 6c   if pPage is a l
17ea4 65 61 66 2e 20 20 30 20 69 66 20 70 50 61 67 65  eaf.  0 if pPage
17ea5 20 69 73 20 6e 6f 74 20 61 20 6c 65 61 66 2e 0a   is not a leaf..
17ea6 20 20 2a 2a 20 20 20 20 20 20 20 6c 65 61 66 44    **       leafD
17ea7 61 74 61 3a 20 20 31 20 69 66 20 70 50 61 67 65  ata:  1 if pPage
17ea8 20 68 6f 6c 64 73 20 6b 65 79 2b 64 61 74 61 20   holds key+data 
17ea9 61 6e 64 20 70 50 61 72 65 6e 74 20 68 6f 6c 64  and pParent hold
17eaa 73 20 6f 6e 6c 79 20 6b 65 79 73 2e 0a 20 20 2a  s only keys..  *
17eab 2f 0a 20 20 6c 65 61 66 43 6f 72 72 65 63 74 69  /.  leafCorrecti
17eac 6f 6e 20 3d 20 61 70 4f 6c 64 5b 30 5d 2d 3e 6c  on = apOld[0]->l
17ead 65 61 66 2a 34 3b 0a 20 20 6c 65 61 66 44 61 74  eaf*4;.  leafDat
17eae 61 20 3d 20 61 70 4f 6c 64 5b 30 5d 2d 3e 68 61  a = apOld[0]->ha
17eaf 73 44 61 74 61 3b 0a 20 20 66 6f 72 28 69 3d 30  sData;.  for(i=0
17eb0 3b 20 69 3c 6e 4f 6c 64 3b 20 69 2b 2b 29 7b 0a  ; i<nOld; i++){.
17eb1 20 20 20 20 69 6e 74 20 6c 69 6d 69 74 3b 0a 20      int limit;. 
17eb2 20 20 20 0a 20 20 20 20 2f 2a 20 42 65 66 6f 72     .    /* Befor
17eb3 65 20 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67  e doing anything
17eb4 20 65 6c 73 65 2c 20 74 61 6b 65 20 61 20 63 6f   else, take a co
17eb5 70 79 20 6f 66 20 74 68 65 20 69 27 74 68 20 6f  py of the i'th o
17eb6 72 69 67 69 6e 61 6c 20 73 69 62 6c 69 6e 67 0a  riginal sibling.
17eb7 20 20 20 20 2a 2a 20 54 68 65 20 72 65 73 74 20      ** The rest 
17eb8 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  of this function
17eb9 20 77 69 6c 6c 20 75 73 65 20 64 61 74 61 20 66   will use data f
17eba 72 6f 6d 20 74 68 65 20 63 6f 70 69 65 73 20 72  rom the copies r
17ebb 61 74 68 65 72 0a 20 20 20 20 2a 2a 20 74 68 61  ather.    ** tha
17ebc 74 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70  t the original p
17ebd 61 67 65 73 20 73 69 6e 63 65 20 74 68 65 20 6f  ages since the o
17ebe 72 69 67 69 6e 61 6c 20 70 61 67 65 73 20 77 69  riginal pages wi
17ebf 6c 6c 20 62 65 20 69 6e 20 74 68 65 0a 20 20 20  ll be in the.   
17ec0 20 2a 2a 20 70 72 6f 63 65 73 73 20 6f 66 20 62   ** process of b
17ec1 65 69 6e 67 20 6f 76 65 72 77 72 69 74 74 65 6e  eing overwritten
17ec2 2e 20 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67  .  */.    MemPag
17ec3 65 20 2a 70 4f 6c 64 20 3d 20 61 70 43 6f 70 79  e *pOld = apCopy
17ec4 5b 69 5d 20 3d 20 28 4d 65 6d 50 61 67 65 2a 29  [i] = (MemPage*)
17ec5 26 61 53 70 61 63 65 31 5b 70 42 74 2d 3e 70 61  &aSpace1[pBt->pa
17ec6 67 65 53 69 7a 65 20 2b 20 6b 2a 69 5d 3b 0a 20  geSize + k*i];. 
17ec7 20 20 20 6d 65 6d 63 70 79 28 70 4f 6c 64 2c 20     memcpy(pOld, 
17ec8 61 70 4f 6c 64 5b 69 5d 2c 20 73 69 7a 65 6f 66  apOld[i], sizeof
17ec9 28 4d 65 6d 50 61 67 65 29 29 3b 0a 20 20 20 20  (MemPage));.    
17eca 70 4f 6c 64 2d 3e 61 44 61 74 61 20 3d 20 28 76  pOld->aData = (v
17ecb 6f 69 64 2a 29 26 70 4f 6c 64 5b 31 5d 3b 0a 20  oid*)&pOld[1];. 
17ecc 20 20 20 6d 65 6d 63 70 79 28 70 4f 6c 64 2d 3e     memcpy(pOld->
17ecd 61 44 61 74 61 2c 20 61 70 4f 6c 64 5b 69 5d 2d  aData, apOld[i]-
17ece 3e 61 44 61 74 61 2c 20 70 42 74 2d 3e 70 61 67  >aData, pBt->pag
17ecf 65 53 69 7a 65 29 3b 0a 0a 20 20 20 20 6c 69 6d  eSize);..    lim
17ed0 69 74 20 3d 20 70 4f 6c 64 2d 3e 6e 43 65 6c 6c  it = pOld->nCell
17ed1 2b 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77  +pOld->nOverflow
17ed2 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a  ;.    for(j=0; j
17ed3 3c 6c 69 6d 69 74 3b 20 6a 2b 2b 29 7b 0a 20 20  <limit; j++){.  
17ed4 20 20 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c      assert( nCel
17ed5 6c 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20  l<nMaxCells );. 
17ed6 20 20 20 20 20 61 70 43 65 6c 6c 5b 6e 43 65 6c       apCell[nCel
17ed7 6c 5d 20 3d 20 66 69 6e 64 4f 76 65 72 66 6c 6f  l] = findOverflo
17ed8 77 43 65 6c 6c 28 70 4f 6c 64 2c 20 6a 29 3b 0a  wCell(pOld, j);.
17ed9 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65        szCell[nCe
17eda 6c 6c 5d 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74  ll] = cellSizePt
17edb 72 28 70 4f 6c 64 2c 20 61 70 43 65 6c 6c 5b 6e  r(pOld, apCell[n
17edc 43 65 6c 6c 5d 29 3b 0a 20 20 20 20 20 20 6e 43  Cell]);.      nC
17edd 65 6c 6c 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20  ell++;.    }.   
17ede 20 69 66 28 20 69 3c 6e 4f 6c 64 2d 31 20 26 26   if( i<nOld-1 &&
17edf 20 21 6c 65 61 66 44 61 74 61 29 7b 0a 20 20 20   !leafData){.   
17ee0 20 20 20 75 31 36 20 73 7a 20 3d 20 28 75 31 36     u16 sz = (u16
17ee1 29 73 7a 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 20  )szNew[i];.     
17ee2 20 75 38 20 2a 70 54 65 6d 70 3b 0a 20 20 20 20   u8 *pTemp;.    
17ee3 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c 6c 3c    assert( nCell<
17ee4 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20  nMaxCells );.   
17ee5 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d     szCell[nCell]
17ee6 20 3d 20 73 7a 3b 0a 20 20 20 20 20 20 70 54 65   = sz;.      pTe
17ee7 6d 70 20 3d 20 26 61 53 70 61 63 65 31 5b 69 53  mp = &aSpace1[iS
17ee8 70 61 63 65 31 5d 3b 0a 20 20 20 20 20 20 69 53  pace1];.      iS
17ee9 70 61 63 65 31 20 2b 3d 20 73 7a 3b 0a 20 20 20  pace1 += sz;.   
17eea 20 20 20 61 73 73 65 72 74 28 20 73 7a 3c 3d 70     assert( sz<=p
17eeb 42 74 2d 3e 70 61 67 65 53 69 7a 65 2f 34 20 29  Bt->pageSize/4 )
17eec 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
17eed 69 53 70 61 63 65 31 3c 3d 70 42 74 2d 3e 70 61  iSpace1<=pBt->pa
17eee 67 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 20  geSize );.      
17eef 6d 65 6d 63 70 79 28 70 54 65 6d 70 2c 20 61 70  memcpy(pTemp, ap
17ef0 44 69 76 5b 69 5d 2c 20 73 7a 29 3b 0a 20 20 20  Div[i], sz);.   
17ef1 20 20 20 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d     apCell[nCell]
17ef2 20 3d 20 70 54 65 6d 70 2b 6c 65 61 66 43 6f 72   = pTemp+leafCor
17ef3 72 65 63 74 69 6f 6e 3b 0a 20 20 20 20 20 20 61  rection;.      a
17ef4 73 73 65 72 74 28 20 6c 65 61 66 43 6f 72 72 65  ssert( leafCorre
17ef5 63 74 69 6f 6e 3d 3d 30 20 7c 7c 20 6c 65 61 66  ction==0 || leaf
17ef6 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 20 29 3b  Correction==4 );
17ef7 0a 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43  .      szCell[nC
17ef8 65 6c 6c 5d 20 3d 20 73 7a 43 65 6c 6c 5b 6e 43  ell] = szCell[nC
17ef9 65 6c 6c 5d 20 2d 20 6c 65 61 66 43 6f 72 72 65  ell] - leafCorre
17efa 63 74 69 6f 6e 3b 0a 20 20 20 20 20 20 69 66 28  ction;.      if(
17efb 20 21 70 4f 6c 64 2d 3e 6c 65 61 66 20 29 7b 0a   !pOld->leaf ){.
17efc 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
17efd 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d  leafCorrection==
17efe 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73  0 );.        ass
17eff 65 72 74 28 20 70 4f 6c 64 2d 3e 68 64 72 4f 66  ert( pOld->hdrOf
17f00 66 73 65 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20  fset==0 );.     
17f01 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68 74 20     /* The right 
17f02 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 65 20 63  pointer of the c
17f03 68 69 6c 64 20 70 61 67 65 20 70 4f 6c 64 20 62  hild page pOld b
17f04 65 63 6f 6d 65 73 20 74 68 65 20 6c 65 66 74 0a  ecomes the left.
17f05 20 20 20 20 20 20 20 20 2a 2a 20 70 6f 69 6e 74          ** point
17f06 65 72 20 6f 66 20 74 68 65 20 64 69 76 69 64 65  er of the divide
17f07 72 20 63 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 20  r cell */.      
17f08 20 20 6d 65 6d 63 70 79 28 61 70 43 65 6c 6c 5b    memcpy(apCell[
17f09 6e 43 65 6c 6c 5d 2c 20 26 70 4f 6c 64 2d 3e 61  nCell], &pOld->a
17f0a 44 61 74 61 5b 38 5d 2c 20 34 29 3b 0a 20 20 20  Data[8], 4);.   
17f0b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
17f0c 20 20 61 73 73 65 72 74 28 20 6c 65 61 66 43 6f    assert( leafCo
17f0d 72 72 65 63 74 69 6f 6e 3d 3d 34 20 29 3b 0a 20  rrection==4 );. 
17f0e 20 20 20 20 20 20 20 69 66 28 20 73 7a 43 65 6c         if( szCel
17f0f 6c 5b 6e 43 65 6c 6c 5d 3c 34 20 29 7b 0a 20 20  l[nCell]<4 ){.  
17f10 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f          /* Do no
17f11 74 20 61 6c 6c 6f 77 20 61 6e 79 20 63 65 6c 6c  t allow any cell
17f12 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 34  s smaller than 4
17f13 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 20 20 20   bytes. */.     
17f14 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c       szCell[nCel
17f15 6c 5d 20 3d 20 34 3b 0a 20 20 20 20 20 20 20 20  l] = 4;.        
17f16 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
17f17 6e 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 7d 0a 20  nCell++;.    }. 
17f18 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46 69   }..  /*.  ** Fi
17f19 67 75 72 65 20 6f 75 74 20 74 68 65 20 6e 75 6d  gure out the num
17f1a 62 65 72 20 6f 66 20 70 61 67 65 73 20 6e 65 65  ber of pages nee
17f1b 64 65 64 20 74 6f 20 68 6f 6c 64 20 61 6c 6c 20  ded to hold all 
17f1c 6e 43 65 6c 6c 20 63 65 6c 6c 73 2e 0a 20 20 2a  nCell cells..  *
17f1d 2a 20 53 74 6f 72 65 20 74 68 69 73 20 6e 75 6d  * Store this num
17f1e 62 65 72 20 69 6e 20 22 6b 22 2e 20 20 41 6c 73  ber in "k".  Als
17f1f 6f 20 63 6f 6d 70 75 74 65 20 73 7a 4e 65 77 5b  o compute szNew[
17f20 5d 20 77 68 69 63 68 20 69 73 20 74 68 65 20 74  ] which is the t
17f21 6f 74 61 6c 0a 20 20 2a 2a 20 73 69 7a 65 20 6f  otal.  ** size o
17f22 66 20 61 6c 6c 20 63 65 6c 6c 73 20 6f 6e 20 74  f all cells on t
17f23 68 65 20 69 2d 74 68 20 70 61 67 65 20 61 6e 64  he i-th page and
17f24 20 63 6e 74 4e 65 77 5b 5d 20 77 68 69 63 68 20   cntNew[] which 
17f25 69 73 20 74 68 65 20 69 6e 64 65 78 0a 20 20 2a  is the index.  *
17f26 2a 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 6f 66  * in apCell[] of
17f27 20 74 68 65 20 63 65 6c 6c 20 74 68 61 74 20 64   the cell that d
17f28 69 76 69 64 65 73 20 70 61 67 65 20 69 20 66 72  ivides page i fr
17f29 6f 6d 20 70 61 67 65 20 69 2b 31 2e 20 20 0a 20  om page i+1.  . 
17f2a 20 2a 2a 20 63 6e 74 4e 65 77 5b 6b 5d 20 73 68   ** cntNew[k] sh
17f2b 6f 75 6c 64 20 65 71 75 61 6c 20 6e 43 65 6c 6c  ould equal nCell
17f2c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 6c 75  ..  **.  ** Valu
17f2d 65 73 20 63 6f 6d 70 75 74 65 64 20 62 79 20 74  es computed by t
17f2e 68 69 73 20 62 6c 6f 63 6b 3a 0a 20 20 2a 2a 0a  his block:.  **.
17f2f 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6b    **           k
17f30 3a 20 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62  : The total numb
17f31 65 72 20 6f 66 20 73 69 62 6c 69 6e 67 20 70 61  er of sibling pa
17f32 67 65 73 0a 20 20 2a 2a 20 20 20 20 73 7a 4e 65  ges.  **    szNe
17f33 77 5b 69 5d 3a 20 53 70 61 63 65 64 20 75 73 65  w[i]: Spaced use
17f34 64 20 6f 6e 20 74 68 65 20 69 2d 74 68 20 73 69  d on the i-th si
17f35 62 6c 69 6e 67 20 70 61 67 65 2e 0a 20 20 2a 2a  bling page..  **
17f36 20 20 20 63 6e 74 4e 65 77 5b 69 5d 3a 20 49 6e     cntNew[i]: In
17f37 64 65 78 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20  dex in apCell[] 
17f38 61 6e 64 20 73 7a 43 65 6c 6c 5b 5d 20 66 6f 72  and szCell[] for
17f39 20 74 68 65 20 66 69 72 73 74 20 63 65 6c 6c 20   the first cell 
17f3a 74 6f 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20  to.  **         
17f3b 20 20 20 20 20 74 68 65 20 72 69 67 68 74 20 6f       the right o
17f3c 66 20 74 68 65 20 69 2d 74 68 20 73 69 62 6c 69  f the i-th sibli
17f3d 6e 67 20 70 61 67 65 2e 0a 20 20 2a 2a 20 75 73  ng page..  ** us
17f3e 61 62 6c 65 53 70 61 63 65 3a 20 4e 75 6d 62 65  ableSpace: Numbe
17f3f 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70  r of bytes of sp
17f40 61 63 65 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e  ace available on
17f41 20 65 61 63 68 20 73 69 62 6c 69 6e 67 2e 0a 20   each sibling.. 
17f42 20 2a 2a 20 0a 20 20 2a 2f 0a 20 20 75 73 61 62   ** .  */.  usab
17f43 6c 65 53 70 61 63 65 20 3d 20 70 42 74 2d 3e 75  leSpace = pBt->u
17f44 73 61 62 6c 65 53 69 7a 65 20 2d 20 31 32 20 2b  sableSize - 12 +
17f45 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b   leafCorrection;
17f46 0a 20 20 66 6f 72 28 73 75 62 74 6f 74 61 6c 3d  .  for(subtotal=
17f47 6b 3d 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20  k=i=0; i<nCell; 
17f48 69 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74  i++){.    assert
17f49 28 20 69 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b  ( i<nMaxCells );
17f4a 0a 20 20 20 20 73 75 62 74 6f 74 61 6c 20 2b 3d  .    subtotal +=
17f4b 20 73 7a 43 65 6c 6c 5b 69 5d 20 2b 20 32 3b 0a   szCell[i] + 2;.
17f4c 20 20 20 20 69 66 28 20 73 75 62 74 6f 74 61 6c      if( subtotal
17f4d 20 3e 20 75 73 61 62 6c 65 53 70 61 63 65 20 29   > usableSpace )
17f4e 7b 0a 20 20 20 20 20 20 73 7a 4e 65 77 5b 6b 5d  {.      szNew[k]
17f4f 20 3d 20 73 75 62 74 6f 74 61 6c 20 2d 20 73 7a   = subtotal - sz
17f50 43 65 6c 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 63  Cell[i];.      c
17f51 6e 74 4e 65 77 5b 6b 5d 20 3d 20 69 3b 0a 20 20  ntNew[k] = i;.  
17f52 20 20 20 20 69 66 28 20 6c 65 61 66 44 61 74 61      if( leafData
17f53 20 29 7b 20 69 2d 2d 3b 20 7d 0a 20 20 20 20 20   ){ i--; }.     
17f54 20 73 75 62 74 6f 74 61 6c 20 3d 20 30 3b 0a 20   subtotal = 0;. 
17f55 20 20 20 20 20 6b 2b 2b 3b 0a 20 20 20 20 20 20       k++;.      
17f56 69 66 28 20 6b 3e 4e 42 2b 31 20 29 7b 20 72 63  if( k>NB+1 ){ rc
17f57 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50   = SQLITE_CORRUP
17f58 54 3b 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f  T; goto balance_
17f59 63 6c 65 61 6e 75 70 3b 20 7d 0a 20 20 20 20 7d  cleanup; }.    }
17f5a 0a 20 20 7d 0a 20 20 73 7a 4e 65 77 5b 6b 5d 20  .  }.  szNew[k] 
17f5b 3d 20 73 75 62 74 6f 74 61 6c 3b 0a 20 20 63 6e  = subtotal;.  cn
17f5c 74 4e 65 77 5b 6b 5d 20 3d 20 6e 43 65 6c 6c 3b  tNew[k] = nCell;
17f5d 0a 20 20 6b 2b 2b 3b 0a 0a 20 20 2f 2a 0a 20 20  .  k++;..  /*.  
17f5e 2a 2a 20 54 68 65 20 70 61 63 6b 69 6e 67 20 63  ** The packing c
17f5f 6f 6d 70 75 74 65 64 20 62 79 20 74 68 65 20 70  omputed by the p
17f60 72 65 76 69 6f 75 73 20 62 6c 6f 63 6b 20 69 73  revious block is
17f61 20 62 69 61 73 65 64 20 74 6f 77 61 72 64 20 74   biased toward t
17f62 68 65 20 73 69 62 6c 69 6e 67 73 0a 20 20 2a 2a  he siblings.  **
17f63 20 6f 6e 20 74 68 65 20 6c 65 66 74 20 73 69 64   on the left sid
17f64 65 2e 20 20 54 68 65 20 6c 65 66 74 20 73 69 62  e.  The left sib
17f65 6c 69 6e 67 73 20 61 72 65 20 61 6c 77 61 79 73  lings are always
17f66 20 6e 65 61 72 6c 79 20 66 75 6c 6c 2c 20 77 68   nearly full, wh
17f67 69 6c 65 20 74 68 65 0a 20 20 2a 2a 20 72 69 67  ile the.  ** rig
17f68 68 74 2d 6d 6f 73 74 20 73 69 62 6c 69 6e 67 20  ht-most sibling 
17f69 6d 69 67 68 74 20 62 65 20 6e 65 61 72 6c 79 20  might be nearly 
17f6a 65 6d 70 74 79 2e 20 20 54 68 69 73 20 62 6c 6f  empty.  This blo
17f6b 63 6b 20 6f 66 20 63 6f 64 65 20 61 74 74 65 6d  ck of code attem
17f6c 70 74 73 0a 20 20 2a 2a 20 74 6f 20 61 64 6a 75  pts.  ** to adju
17f6d 73 74 20 74 68 65 20 70 61 63 6b 69 6e 67 20 6f  st the packing o
17f6e 66 20 73 69 62 6c 69 6e 67 73 20 74 6f 20 67 65  f siblings to ge
17f6f 74 20 61 20 62 65 74 74 65 72 20 62 61 6c 61 6e  t a better balan
17f70 63 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  ce..  **.  ** Th
17f71 69 73 20 61 64 6a 75 73 74 6d 65 6e 74 20 69 73  is adjustment is
17f72 20 6d 6f 72 65 20 74 68 61 6e 20 61 6e 20 6f 70   more than an op
17f73 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 54 68 65  timization.  The
17f74 20 70 61 63 6b 69 6e 67 20 61 62 6f 76 65 20 6d   packing above m
17f75 69 67 68 74 0a 20 20 2a 2a 20 62 65 20 73 6f 20  ight.  ** be so 
17f76 6f 75 74 20 6f 66 20 62 61 6c 61 6e 63 65 20 61  out of balance a
17f77 73 20 74 6f 20 62 65 20 69 6c 6c 65 67 61 6c 2e  s to be illegal.
17f78 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74    For example, t
17f79 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 0a 20 20  he right-most.  
17f7a 2a 2a 20 73 69 62 6c 69 6e 67 20 6d 69 67 68 74  ** sibling might
17f7b 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65   be completely e
17f7c 6d 70 74 79 2e 20 20 54 68 69 73 20 61 64 6a 75  mpty.  This adju
17f7d 73 74 6d 65 6e 74 20 69 73 20 6e 6f 74 20 6f 70  stment is not op
17f7e 74 69 6f 6e 61 6c 2e 0a 20 20 2a 2f 0a 20 20 66  tional..  */.  f
17f7f 6f 72 28 69 3d 6b 2d 31 3b 20 69 3e 30 3b 20 69  or(i=k-1; i>0; i
17f80 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 20 73 7a 52  --){.    int szR
17f81 69 67 68 74 20 3d 20 73 7a 4e 65 77 5b 69 5d 3b  ight = szNew[i];
17f82 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 69 62    /* Size of sib
17f83 6c 69 6e 67 20 6f 6e 20 74 68 65 20 72 69 67 68  ling on the righ
17f84 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 7a 4c  t */.    int szL
17f85 65 66 74 20 3d 20 73 7a 4e 65 77 5b 69 2d 31 5d  eft = szNew[i-1]
17f86 3b 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 69 62  ; /* Size of sib
17f87 6c 69 6e 67 20 6f 6e 20 74 68 65 20 6c 65 66 74  ling on the left
17f88 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 3b 20 20   */.    int r;  
17f89 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
17f8a 6e 64 65 78 20 6f 66 20 72 69 67 68 74 2d 6d 6f  ndex of right-mo
17f8b 73 74 20 63 65 6c 6c 20 69 6e 20 6c 65 66 74 20  st cell in left 
17f8c 73 69 62 6c 69 6e 67 20 2a 2f 0a 20 20 20 20 69  sibling */.    i
17f8d 6e 74 20 64 3b 20 20 20 20 20 20 20 20 20 20 20  nt d;           
17f8e 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 66     /* Index of f
17f8f 69 72 73 74 20 63 65 6c 6c 20 74 6f 20 74 68 65  irst cell to the
17f90 20 6c 65 66 74 20 6f 66 20 72 69 67 68 74 20 73   left of right s
17f91 69 62 6c 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 72  ibling */..    r
17f92 20 3d 20 63 6e 74 4e 65 77 5b 69 2d 31 5d 20 2d   = cntNew[i-1] -
17f93 20 31 3b 0a 20 20 20 20 64 20 3d 20 72 20 2b 20   1;.    d = r + 
17f94 31 20 2d 20 6c 65 61 66 44 61 74 61 3b 0a 20 20  1 - leafData;.  
17f95 20 20 61 73 73 65 72 74 28 20 64 3c 6e 4d 61 78    assert( d<nMax
17f96 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 61 73 73  Cells );.    ass
17f97 65 72 74 28 20 72 3c 6e 4d 61 78 43 65 6c 6c 73  ert( r<nMaxCells
17f98 20 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 73   );.    while( s
17f99 7a 52 69 67 68 74 3d 3d 30 20 7c 7c 20 73 7a 52  zRight==0 || szR
17f9a 69 67 68 74 2b 73 7a 43 65 6c 6c 5b 64 5d 2b 32  ight+szCell[d]+2
17f9b 3c 3d 73 7a 4c 65 66 74 2d 28 73 7a 43 65 6c 6c  <=szLeft-(szCell
17f9c 5b 72 5d 2b 32 29 20 29 7b 0a 20 20 20 20 20 20  [r]+2) ){.      
17f9d 73 7a 52 69 67 68 74 20 2b 3d 20 73 7a 43 65 6c  szRight += szCel
17f9e 6c 5b 64 5d 20 2b 20 32 3b 0a 20 20 20 20 20 20  l[d] + 2;.      
17f9f 73 7a 4c 65 66 74 20 2d 3d 20 73 7a 43 65 6c 6c  szLeft -= szCell
17fa0 5b 72 5d 20 2b 20 32 3b 0a 20 20 20 20 20 20 63  [r] + 2;.      c
17fa1 6e 74 4e 65 77 5b 69 2d 31 5d 2d 2d 3b 0a 20 20  ntNew[i-1]--;.  
17fa2 20 20 20 20 72 20 3d 20 63 6e 74 4e 65 77 5b 69      r = cntNew[i
17fa3 2d 31 5d 20 2d 20 31 3b 0a 20 20 20 20 20 20 64  -1] - 1;.      d
17fa4 20 3d 20 72 20 2b 20 31 20 2d 20 6c 65 61 66 44   = r + 1 - leafD
17fa5 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  ata;.    }.    s
17fa6 7a 4e 65 77 5b 69 5d 20 3d 20 73 7a 52 69 67 68  zNew[i] = szRigh
17fa7 74 3b 0a 20 20 20 20 73 7a 4e 65 77 5b 69 2d 31  t;.    szNew[i-1
17fa8 5d 20 3d 20 73 7a 4c 65 66 74 3b 0a 20 20 7d 0a  ] = szLeft;.  }.
17fa9 0a 20 20 2f 2a 20 45 69 74 68 65 72 20 77 65 20  .  /* Either we 
17faa 66 6f 75 6e 64 20 6f 6e 65 20 6f 72 20 6d 6f 72  found one or mor
17fab 65 20 63 65 6c 6c 73 20 28 63 6e 74 6e 65 77 5b  e cells (cntnew[
17fac 30 5d 29 3e 30 29 20 6f 72 20 70 50 61 67 65 20  0])>0) or pPage 
17fad 69 73 0a 20 20 2a 2a 20 61 20 76 69 72 74 75 61  is.  ** a virtua
17fae 6c 20 72 6f 6f 74 20 70 61 67 65 2e 20 20 41 20  l root page.  A 
17faf 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 67  virtual root pag
17fb0 65 20 69 73 20 77 68 65 6e 20 74 68 65 20 72 65  e is when the re
17fb1 61 6c 20 72 6f 6f 74 0a 20 20 2a 2a 20 70 61 67  al root.  ** pag
17fb2 65 20 69 73 20 70 61 67 65 20 31 20 61 6e 64 20  e is page 1 and 
17fb3 77 65 20 61 72 65 20 74 68 65 20 6f 6e 6c 79 20  we are the only 
17fb4 63 68 69 6c 64 20 6f 66 20 74 68 61 74 20 70 61  child of that pa
17fb5 67 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  ge..  */.  asser
17fb6 74 28 20 63 6e 74 4e 65 77 5b 30 5d 3e 30 20 7c  t( cntNew[0]>0 |
17fb7 7c 20 28 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f  | (pParent->pgno
17fb8 3d 3d 31 20 26 26 20 70 50 61 72 65 6e 74 2d 3e  ==1 && pParent->
17fb9 6e 43 65 6c 6c 3d 3d 30 29 20 29 3b 0a 0a 20 20  nCell==0) );..  
17fba 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a  TRACE(("BALANCE:
17fbb 20 6f 6c 64 3a 20 25 64 20 25 64 20 25 64 20 20   old: %d %d %d  
17fbc 22 2c 0a 20 20 20 20 61 70 4f 6c 64 5b 30 5d 2d  ",.    apOld[0]-
17fbd 3e 70 67 6e 6f 2c 20 0a 20 20 20 20 6e 4f 6c 64  >pgno, .    nOld
17fbe 3e 3d 32 20 3f 20 61 70 4f 6c 64 5b 31 5d 2d 3e  >=2 ? apOld[1]->
17fbf 70 67 6e 6f 20 3a 20 30 2c 0a 20 20 20 20 6e 4f  pgno : 0,.    nO
17fc0 6c 64 3e 3d 33 20 3f 20 61 70 4f 6c 64 5b 32 5d  ld>=3 ? apOld[2]
17fc1 2d 3e 70 67 6e 6f 20 3a 20 30 0a 20 20 29 29 3b  ->pgno : 0.  ));
17fc2 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c 6f  ..  /*.  ** Allo
17fc3 63 61 74 65 20 6b 20 6e 65 77 20 70 61 67 65 73  cate k new pages
17fc4 2e 20 20 52 65 75 73 65 20 6f 6c 64 20 70 61 67  .  Reuse old pag
17fc5 65 73 20 77 68 65 72 65 20 70 6f 73 73 69 62 6c  es where possibl
17fc6 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 61 70  e..  */.  if( ap
17fc7 4f 6c 64 5b 30 5d 2d 3e 70 67 6e 6f 3c 3d 31 20  Old[0]->pgno<=1 
17fc8 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
17fc9 54 45 5f 43 4f 52 52 55 50 54 3b 0a 20 20 20 20  TE_CORRUPT;.    
17fca 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65  goto balance_cle
17fcb 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 70 61 67 65  anup;.  }.  page
17fcc 46 6c 61 67 73 20 3d 20 61 70 4f 6c 64 5b 30 5d  Flags = apOld[0]
17fcd 2d 3e 61 44 61 74 61 5b 30 5d 3b 0a 20 20 66 6f  ->aData[0];.  fo
17fce 72 28 69 3d 30 3b 20 69 3c 6b 3b 20 69 2b 2b 29  r(i=0; i<k; i++)
17fcf 7b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70  {.    MemPage *p
17fd0 4e 65 77 3b 0a 20 20 20 20 69 66 28 20 69 3c 6e  New;.    if( i<n
17fd1 4f 6c 64 20 29 7b 0a 20 20 20 20 20 20 70 4e 65  Old ){.      pNe
17fd2 77 20 3d 20 61 70 4e 65 77 5b 69 5d 20 3d 20 61  w = apNew[i] = a
17fd3 70 4f 6c 64 5b 69 5d 3b 0a 20 20 20 20 20 20 61  pOld[i];.      a
17fd4 70 4f 6c 64 5b 69 5d 20 3d 20 30 3b 0a 20 20 20  pOld[i] = 0;.   
17fd5 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
17fd6 61 67 65 72 57 72 69 74 65 28 70 4e 65 77 2d 3e  agerWrite(pNew->
17fd7 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20  pDbPage);.      
17fd8 6e 4e 65 77 2b 2b 3b 0a 20 20 20 20 20 20 69 66  nNew++;.      if
17fd9 28 20 72 63 20 29 20 67 6f 74 6f 20 62 61 6c 61  ( rc ) goto bala
17fda 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20  nce_cleanup;.   
17fdb 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73   }else{.      as
17fdc 73 65 72 74 28 20 69 3e 30 20 29 3b 0a 20 20 20  sert( i>0 );.   
17fdd 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65     rc = allocate
17fde 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26  BtreePage(pBt, &
17fdf 70 4e 65 77 2c 20 26 70 67 6e 6f 2c 20 70 67 6e  pNew, &pgno, pgn
17fe0 6f 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28  o, 0);.      if(
17fe1 20 72 63 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e   rc ) goto balan
17fe2 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20  ce_cleanup;.    
17fe3 20 20 61 70 4e 65 77 5b 69 5d 20 3d 20 70 4e 65    apNew[i] = pNe
17fe4 77 3b 0a 20 20 20 20 20 20 6e 4e 65 77 2b 2b 3b  w;.      nNew++;
17fe5 0a 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 20 74  ..      /* Set t
17fe6 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65  he pointer-map e
17fe7 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6e 65 77  ntry for the new
17fe8 20 73 69 62 6c 69 6e 67 20 70 61 67 65 2e 20 2a   sibling page. *
17fe9 2f 0a 20 20 20 20 20 20 69 66 28 20 49 53 41 55  /.      if( ISAU
17fea 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20  TOVACUUM ){.    
17feb 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42      ptrmapPut(pB
17fec 74 2c 20 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 50  t, pNew->pgno, P
17fed 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 50 61  TRMAP_BTREE, pPa
17fee 72 65 6e 74 2d 3e 70 67 6e 6f 2c 20 26 72 63 29  rent->pgno, &rc)
17fef 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
17ff0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
17ff1 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62 61           goto ba
17ff2 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20  lance_cleanup;. 
17ff3 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
17ff4 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
17ff5 20 46 72 65 65 20 61 6e 79 20 6f 6c 64 20 70 61   Free any old pa
17ff6 67 65 73 20 74 68 61 74 20 77 65 72 65 20 6e 6f  ges that were no
17ff7 74 20 72 65 75 73 65 64 20 61 73 20 6e 65 77 20  t reused as new 
17ff8 70 61 67 65 73 2e 0a 20 20 2a 2f 0a 20 20 77 68  pages..  */.  wh
17ff9 69 6c 65 28 20 69 3c 6e 4f 6c 64 20 29 7b 0a 20  ile( i<nOld ){. 
17ffa 20 20 20 66 72 65 65 50 61 67 65 28 61 70 4f 6c     freePage(apOl
17ffb 64 5b 69 5d 2c 20 26 72 63 29 3b 0a 20 20 20 20  d[i], &rc);.    
17ffc 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 62 61  if( rc ) goto ba
17ffd 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20  lance_cleanup;. 
17ffe 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 61     releasePage(a
17fff 70 4f 6c 64 5b 69 5d 29 3b 0a 20 20 20 20 61 70  pOld[i]);.    ap
18000 4f 6c 64 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20  Old[i] = 0;.    
18001 69 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20  i++;.  }..  /*. 
18002 20 2a 2a 20 50 75 74 20 74 68 65 20 6e 65 77 20   ** Put the new 
18003 70 61 67 65 73 20 69 6e 20 61 63 63 65 6e 64 69  pages in accendi
18004 6e 67 20 6f 72 64 65 72 2e 20 20 54 68 69 73 20  ng order.  This 
18005 68 65 6c 70 73 20 74 6f 0a 20 20 2a 2a 20 6b 65  helps to.  ** ke
18006 65 70 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68  ep entries in th
18007 65 20 64 69 73 6b 20 66 69 6c 65 20 69 6e 20 6f  e disk file in o
18008 72 64 65 72 20 73 6f 20 74 68 61 74 20 61 20 73  rder so that a s
18009 63 61 6e 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20  can.  ** of the 
1800a 74 61 62 6c 65 20 69 73 20 61 20 6c 69 6e 65 61  table is a linea
1800b 72 20 73 63 61 6e 20 74 68 72 6f 75 67 68 20 74  r scan through t
1800c 68 65 20 66 69 6c 65 2e 20 20 54 68 61 74 0a 20  he file.  That. 
1800d 20 2a 2a 20 69 6e 20 74 75 72 6e 20 68 65 6c 70   ** in turn help
1800e 73 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20  s the operating 
1800f 73 79 73 74 65 6d 20 74 6f 20 64 65 6c 69 76 65  system to delive
18010 72 20 70 61 67 65 73 0a 20 20 2a 2a 20 66 72 6f  r pages.  ** fro
18011 6d 20 74 68 65 20 64 69 73 6b 20 6d 6f 72 65 20  m the disk more 
18012 72 61 70 69 64 6c 79 2e 0a 20 20 2a 2a 0a 20 20  rapidly..  **.  
18013 2a 2a 20 41 6e 20 4f 28 6e 5e 32 29 20 69 6e 73  ** An O(n^2) ins
18014 65 72 74 69 6f 6e 20 73 6f 72 74 20 61 6c 67 6f  ertion sort algo
18015 72 69 74 68 6d 20 69 73 20 75 73 65 64 2c 20 62  rithm is used, b
18016 75 74 20 73 69 6e 63 65 0a 20 20 2a 2a 20 6e 20  ut since.  ** n 
18017 69 73 20 6e 65 76 65 72 20 6d 6f 72 65 20 74 68  is never more th
18018 61 6e 20 4e 42 20 28 61 20 73 6d 61 6c 6c 20 63  an NB (a small c
18019 6f 6e 73 74 61 6e 74 29 2c 20 74 68 61 74 20 73  onstant), that s
1801a 68 6f 75 6c 64 0a 20 20 2a 2a 20 6e 6f 74 20 62  hould.  ** not b
1801b 65 20 61 20 70 72 6f 62 6c 65 6d 2e 0a 20 20 2a  e a problem..  *
1801c 2a 0a 20 20 2a 2a 20 57 68 65 6e 20 4e 42 3d 3d  *.  ** When NB==
1801d 33 2c 20 74 68 69 73 20 6f 6e 65 20 6f 70 74 69  3, this one opti
1801e 6d 69 7a 61 74 69 6f 6e 20 6d 61 6b 65 73 20 74  mization makes t
1801f 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a  he database.  **
18020 20 61 62 6f 75 74 20 32 35 25 20 66 61 73 74 65   about 25% faste
18021 72 20 66 6f 72 20 6c 61 72 67 65 20 69 6e 73 65  r for large inse
18022 72 74 69 6f 6e 73 20 61 6e 64 20 64 65 6c 65 74  rtions and delet
18023 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  ions..  */.  for
18024 28 69 3d 30 3b 20 69 3c 6b 2d 31 3b 20 69 2b 2b  (i=0; i<k-1; i++
18025 29 7b 0a 20 20 20 20 69 6e 74 20 6d 69 6e 56 20  ){.    int minV 
18026 3d 20 61 70 4e 65 77 5b 69 5d 2d 3e 70 67 6e 6f  = apNew[i]->pgno
18027 3b 0a 20 20 20 20 69 6e 74 20 6d 69 6e 49 20 3d  ;.    int minI =
18028 20 69 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 69 2b   i;.    for(j=i+
18029 31 3b 20 6a 3c 6b 3b 20 6a 2b 2b 29 7b 0a 20 20  1; j<k; j++){.  
1802a 20 20 20 20 69 66 28 20 61 70 4e 65 77 5b 6a 5d      if( apNew[j]
1802b 2d 3e 70 67 6e 6f 3c 28 75 6e 73 69 67 6e 65 64  ->pgno<(unsigned
1802c 29 6d 69 6e 56 20 29 7b 0a 20 20 20 20 20 20 20  )minV ){.       
1802d 20 6d 69 6e 49 20 3d 20 6a 3b 0a 20 20 20 20 20   minI = j;.     
1802e 20 20 20 6d 69 6e 56 20 3d 20 61 70 4e 65 77 5b     minV = apNew[
1802f 6a 5d 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 20 20  j]->pgno;.      
18030 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
18031 6d 69 6e 49 3e 69 20 29 7b 0a 20 20 20 20 20 20  minI>i ){.      
18032 69 6e 74 20 74 3b 0a 20 20 20 20 20 20 4d 65 6d  int t;.      Mem
18033 50 61 67 65 20 2a 70 54 3b 0a 20 20 20 20 20 20  Page *pT;.      
18034 74 20 3d 20 61 70 4e 65 77 5b 69 5d 2d 3e 70 67  t = apNew[i]->pg
18035 6e 6f 3b 0a 20 20 20 20 20 20 70 54 20 3d 20 61  no;.      pT = a
18036 70 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 20 20 61  pNew[i];.      a
18037 70 4e 65 77 5b 69 5d 20 3d 20 61 70 4e 65 77 5b  pNew[i] = apNew[
18038 6d 69 6e 49 5d 3b 0a 20 20 20 20 20 20 61 70 4e  minI];.      apN
18039 65 77 5b 6d 69 6e 49 5d 20 3d 20 70 54 3b 0a 20  ew[minI] = pT;. 
1803a 20 20 20 7d 0a 20 20 7d 0a 20 20 54 52 41 43 45     }.  }.  TRACE
1803b 28 28 22 6e 65 77 3a 20 25 64 28 25 64 29 20 25  (("new: %d(%d) %
1803c 64 28 25 64 29 20 25 64 28 25 64 29 20 25 64 28  d(%d) %d(%d) %d(
1803d 25 64 29 20 25 64 28 25 64 29 5c 6e 22 2c 0a 20  %d) %d(%d)\n",. 
1803e 20 20 20 61 70 4e 65 77 5b 30 5d 2d 3e 70 67 6e     apNew[0]->pgn
1803f 6f 2c 20 73 7a 4e 65 77 5b 30 5d 2c 0a 20 20 20  o, szNew[0],.   
18040 20 6e 4e 65 77 3e 3d 32 20 3f 20 61 70 4e 65 77   nNew>=2 ? apNew
18041 5b 31 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c 20 6e  [1]->pgno : 0, n
18042 4e 65 77 3e 3d 32 20 3f 20 73 7a 4e 65 77 5b 31  New>=2 ? szNew[1
18043 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4e 65 77 3e  ] : 0,.    nNew>
18044 3d 33 20 3f 20 61 70 4e 65 77 5b 32 5d 2d 3e 70  =3 ? apNew[2]->p
18045 67 6e 6f 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d 33  gno : 0, nNew>=3
18046 20 3f 20 73 7a 4e 65 77 5b 32 5d 20 3a 20 30 2c   ? szNew[2] : 0,
18047 0a 20 20 20 20 6e 4e 65 77 3e 3d 34 20 3f 20 61  .    nNew>=4 ? a
18048 70 4e 65 77 5b 33 5d 2d 3e 70 67 6e 6f 20 3a 20  pNew[3]->pgno : 
18049 30 2c 20 6e 4e 65 77 3e 3d 34 20 3f 20 73 7a 4e  0, nNew>=4 ? szN
1804a 65 77 5b 33 5d 20 3a 20 30 2c 0a 20 20 20 20 6e  ew[3] : 0,.    n
1804b 4e 65 77 3e 3d 35 20 3f 20 61 70 4e 65 77 5b 34  New>=5 ? apNew[4
1804c 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c 20 6e 4e 65  ]->pgno : 0, nNe
1804d 77 3e 3d 35 20 3f 20 73 7a 4e 65 77 5b 34 5d 20  w>=5 ? szNew[4] 
1804e 3a 20 30 29 29 3b 0a 0a 20 20 61 73 73 65 72 74  : 0));..  assert
1804f 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  ( sqlite3PagerIs
18050 77 72 69 74 65 61 62 6c 65 28 70 50 61 72 65 6e  writeable(pParen
18051 74 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20  t->pDbPage) );. 
18052 20 70 75 74 34 62 79 74 65 28 70 52 69 67 68 74   put4byte(pRight
18053 2c 20 61 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d 2d  , apNew[nNew-1]-
18054 3e 70 67 6e 6f 29 3b 0a 0a 20 20 2f 2a 0a 20 20  >pgno);..  /*.  
18055 2a 2a 20 45 76 65 6e 6c 79 20 64 69 73 74 72 69  ** Evenly distri
18056 62 75 74 65 20 74 68 65 20 64 61 74 61 20 69 6e  bute the data in
18057 20 61 70 43 65 6c 6c 5b 5d 20 61 63 72 6f 73 73   apCell[] across
18058 20 74 68 65 20 6e 65 77 20 70 61 67 65 73 2e 0a   the new pages..
18059 20 20 2a 2a 20 49 6e 73 65 72 74 20 64 69 76 69    ** Insert divi
1805a 64 65 72 20 63 65 6c 6c 73 20 69 6e 74 6f 20 70  der cells into p
1805b 50 61 72 65 6e 74 20 61 73 20 6e 65 63 65 73 73  Parent as necess
1805c 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 6a 20 3d 20  ary..  */.  j = 
1805d 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  0;.  for(i=0; i<
1805e 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nNew; i++){.    
1805f 2f 2a 20 41 73 73 65 6d 62 6c 65 20 74 68 65 20  /* Assemble the 
18060 6e 65 77 20 73 69 62 6c 69 6e 67 20 70 61 67 65  new sibling page
18061 2e 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65  . */.    MemPage
18062 20 2a 70 4e 65 77 20 3d 20 61 70 4e 65 77 5b 69   *pNew = apNew[i
18063 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6a  ];.    assert( j
18064 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20  <nMaxCells );.  
18065 20 20 7a 65 72 6f 50 61 67 65 28 70 4e 65 77 2c    zeroPage(pNew,
18066 20 70 61 67 65 46 6c 61 67 73 29 3b 0a 20 20 20   pageFlags);.   
18067 20 61 73 73 65 6d 62 6c 65 50 61 67 65 28 70 4e   assemblePage(pN
18068 65 77 2c 20 63 6e 74 4e 65 77 5b 69 5d 2d 6a 2c  ew, cntNew[i]-j,
18069 20 26 61 70 43 65 6c 6c 5b 6a 5d 2c 20 26 73 7a   &apCell[j], &sz
1806a 43 65 6c 6c 5b 6a 5d 29 3b 0a 20 20 20 20 61 73  Cell[j]);.    as
1806b 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e 43 65 6c  sert( pNew->nCel
1806c 6c 3e 30 20 7c 7c 20 28 6e 4e 65 77 3d 3d 31 20  l>0 || (nNew==1 
1806d 26 26 20 63 6e 74 4e 65 77 5b 30 5d 3d 3d 30 29  && cntNew[0]==0)
1806e 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
1806f 70 4e 65 77 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d  pNew->nOverflow=
18070 3d 30 20 29 3b 0a 0a 20 20 20 20 6a 20 3d 20 63  =0 );..    j = c
18071 6e 74 4e 65 77 5b 69 5d 3b 0a 0a 20 20 20 20 2f  ntNew[i];..    /
18072 2a 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67  * If the sibling
18073 20 70 61 67 65 20 61 73 73 65 6d 62 6c 65 64 20   page assembled 
18074 61 62 6f 76 65 20 77 61 73 20 6e 6f 74 20 74 68  above was not th
18075 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 73 69 62  e right-most sib
18076 6c 69 6e 67 2c 0a 20 20 20 20 2a 2a 20 69 6e 73  ling,.    ** ins
18077 65 72 74 20 61 20 64 69 76 69 64 65 72 20 63 65  ert a divider ce
18078 6c 6c 20 69 6e 74 6f 20 74 68 65 20 70 61 72 65  ll into the pare
18079 6e 74 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a  nt page..    */.
1807a 20 20 20 20 61 73 73 65 72 74 28 20 69 3c 6e 4e      assert( i<nN
1807b 65 77 2d 31 20 7c 7c 20 6a 3d 3d 6e 43 65 6c 6c  ew-1 || j==nCell
1807c 20 29 3b 0a 20 20 20 20 69 66 28 20 6a 3c 6e 43   );.    if( j<nC
1807d 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 75 38 20  ell ){.      u8 
1807e 2a 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20 75 38  *pCell;.      u8
1807f 20 2a 70 54 65 6d 70 3b 0a 20 20 20 20 20 20 69   *pTemp;.      i
18080 6e 74 20 73 7a 3b 0a 0a 20 20 20 20 20 20 61 73  nt sz;..      as
18081 73 65 72 74 28 20 6a 3c 6e 4d 61 78 43 65 6c 6c  sert( j<nMaxCell
18082 73 20 29 3b 0a 20 20 20 20 20 20 70 43 65 6c 6c  s );.      pCell
18083 20 3d 20 61 70 43 65 6c 6c 5b 6a 5d 3b 0a 20 20   = apCell[j];.  
18084 20 20 20 20 73 7a 20 3d 20 73 7a 43 65 6c 6c 5b      sz = szCell[
18085 6a 5d 20 2b 20 6c 65 61 66 43 6f 72 72 65 63 74  j] + leafCorrect
18086 69 6f 6e 3b 0a 20 20 20 20 20 20 70 54 65 6d 70  ion;.      pTemp
18087 20 3d 20 26 61 4f 76 66 6c 53 70 61 63 65 5b 69   = &aOvflSpace[i
18088 4f 76 66 6c 53 70 61 63 65 5d 3b 0a 20 20 20 20  OvflSpace];.    
18089 20 20 69 66 28 20 21 70 4e 65 77 2d 3e 6c 65 61    if( !pNew->lea
1808a 66 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d  f ){.        mem
1808b 63 70 79 28 26 70 4e 65 77 2d 3e 61 44 61 74 61  cpy(&pNew->aData
1808c 5b 38 5d 2c 20 70 43 65 6c 6c 2c 20 34 29 3b 0a  [8], pCell, 4);.
1808d 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
1808e 6c 65 61 66 44 61 74 61 20 29 7b 0a 20 20 20 20  leafData ){.    
1808f 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 74 72      /* If the tr
18090 65 65 20 69 73 20 61 20 6c 65 61 66 2d 64 61 74  ee is a leaf-dat
18091 61 20 74 72 65 65 2c 20 61 6e 64 20 74 68 65 20  a tree, and the 
18092 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6c 65 61  siblings are lea
18093 76 65 73 2c 20 0a 20 20 20 20 20 20 20 20 2a 2a  ves, .        **
18094 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 6e   then there is n
18095 6f 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 69  o divider cell i
18096 6e 20 61 70 43 65 6c 6c 5b 5d 2e 20 49 6e 73 74  n apCell[]. Inst
18097 65 61 64 2c 20 74 68 65 20 64 69 76 69 64 65 72  ead, the divider
18098 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 65 6c   .        ** cel
18099 6c 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 68  l consists of th
1809a 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20 66 6f  e integer key fo
1809b 72 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74  r the right-most
1809c 20 63 65 6c 6c 20 6f 66 20 0a 20 20 20 20 20 20   cell of .      
1809d 20 20 2a 2a 20 74 68 65 20 73 69 62 6c 69 6e 67    ** the sibling
1809e 2d 70 61 67 65 20 61 73 73 65 6d 62 6c 65 64 20  -page assembled 
1809f 61 62 6f 76 65 20 6f 6e 6c 79 2e 0a 20 20 20 20  above only..    
180a0 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 43      */.        C
180a1 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20  ellInfo info;.  
180a2 20 20 20 20 20 20 6a 2d 2d 3b 0a 20 20 20 20 20        j--;.     
180a3 20 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c     btreeParseCel
180a4 6c 50 74 72 28 70 4e 65 77 2c 20 61 70 43 65 6c  lPtr(pNew, apCel
180a5 6c 5b 6a 5d 2c 20 26 69 6e 66 6f 29 3b 0a 20 20  l[j], &info);.  
180a6 20 20 20 20 20 20 70 43 65 6c 6c 20 3d 20 70 54        pCell = pT
180a7 65 6d 70 3b 0a 20 20 20 20 20 20 20 20 73 7a 20  emp;.        sz 
180a8 3d 20 34 20 2b 20 70 75 74 56 61 72 69 6e 74 28  = 4 + putVarint(
180a9 26 70 43 65 6c 6c 5b 34 5d 2c 20 69 6e 66 6f 2e  &pCell[4], info.
180aa 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 70  nKey);.        p
180ab 54 65 6d 70 20 3d 20 30 3b 0a 20 20 20 20 20 20  Temp = 0;.      
180ac 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70  }else{.        p
180ad 43 65 6c 6c 20 2d 3d 20 34 3b 0a 20 20 20 20 20  Cell -= 4;.     
180ae 20 20 20 2f 2a 20 4f 62 73 63 75 72 65 20 63 61     /* Obscure ca
180af 73 65 20 66 6f 72 20 6e 6f 6e 2d 6c 65 61 66 2d  se for non-leaf-
180b0 64 61 74 61 20 74 72 65 65 73 3a 20 49 66 20 74  data trees: If t
180b1 68 65 20 63 65 6c 6c 20 61 74 20 70 43 65 6c 6c  he cell at pCell
180b2 20 77 61 73 0a 20 20 20 20 20 20 20 20 2a 2a 20   was.        ** 
180b3 70 72 65 76 69 6f 75 73 6c 79 20 73 74 6f 72 65  previously store
180b4 64 20 6f 6e 20 61 20 6c 65 61 66 20 6e 6f 64 65  d on a leaf node
180b5 2c 20 61 6e 64 20 69 74 73 20 72 65 70 6f 72 74  , and its report
180b6 65 64 20 73 69 7a 65 20 77 61 73 20 34 0a 20 20  ed size was 4.  
180b7 20 20 20 20 20 20 2a 2a 20 62 79 74 65 73 2c 20        ** bytes, 
180b8 74 68 65 6e 20 69 74 20 6d 61 79 20 61 63 74 75  then it may actu
180b9 61 6c 6c 79 20 62 65 20 73 6d 61 6c 6c 65 72 20  ally be smaller 
180ba 74 68 61 6e 20 74 68 69 73 20 0a 20 20 20 20 20  than this .     
180bb 20 20 20 2a 2a 20 28 73 65 65 20 62 74 72 65 65     ** (see btree
180bc 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29 2c 20  ParseCellPtr(), 
180bd 34 20 62 79 74 65 73 20 69 73 20 74 68 65 20 6d  4 bytes is the m
180be 69 6e 69 6d 75 6d 20 73 69 7a 65 20 6f 66 0a 20  inimum size of. 
180bf 20 20 20 20 20 20 20 2a 2a 20 61 6e 79 20 63 65         ** any ce
180c0 6c 6c 29 2e 20 42 75 74 20 69 74 20 69 73 20 69  ll). But it is i
180c1 6d 70 6f 72 74 61 6e 74 20 74 6f 20 70 61 73 73  mportant to pass
180c2 20 74 68 65 20 63 6f 72 72 65 63 74 20 73 69 7a   the correct siz
180c3 65 20 74 6f 20 0a 20 20 20 20 20 20 20 20 2a 2a  e to .        **
180c4 20 69 6e 73 65 72 74 43 65 6c 6c 28 29 2c 20 73   insertCell(), s
180c5 6f 20 72 65 70 61 72 73 65 20 74 68 65 20 63 65  o reparse the ce
180c6 6c 6c 20 6e 6f 77 2e 0a 20 20 20 20 20 20 20 20  ll now..        
180c7 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 4e 6f  **.        ** No
180c8 74 65 20 74 68 61 74 20 74 68 69 73 20 63 61 6e  te that this can
180c9 20 6e 65 76 65 72 20 68 61 70 70 65 6e 20 69 6e   never happen in
180ca 20 61 6e 20 53 51 4c 69 74 65 20 64 61 74 61 20   an SQLite data 
180cb 66 69 6c 65 2c 20 61 73 20 61 6c 6c 0a 20 20 20  file, as all.   
180cc 20 20 20 20 20 2a 2a 20 63 65 6c 6c 73 20 61 72       ** cells ar
180cd 65 20 61 74 20 6c 65 61 73 74 20 34 20 62 79 74  e at least 4 byt
180ce 65 73 2e 20 49 74 20 6f 6e 6c 79 20 68 61 70 70  es. It only happ
180cf 65 6e 73 20 69 6e 20 62 2d 74 72 65 65 73 20 75  ens in b-trees u
180d0 73 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 74  sed.        ** t
180d1 6f 20 65 76 61 6c 75 61 74 65 20 22 49 4e 20 28  o evaluate "IN (
180d2 53 45 4c 45 43 54 20 2e 2e 2e 29 22 20 61 6e 64  SELECT ...)" and
180d3 20 73 69 6d 69 6c 61 72 20 63 6c 61 75 73 65 73   similar clauses
180d4 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
180d5 20 20 20 20 20 69 66 28 20 73 7a 43 65 6c 6c 5b       if( szCell[
180d6 6a 5d 3d 3d 34 20 29 7b 0a 20 20 20 20 20 20 20  j]==4 ){.       
180d7 20 20 20 61 73 73 65 72 74 28 6c 65 61 66 43 6f     assert(leafCo
180d8 72 72 65 63 74 69 6f 6e 3d 3d 34 29 3b 0a 20 20  rrection==4);.  
180d9 20 20 20 20 20 20 20 20 73 7a 20 3d 20 63 65 6c          sz = cel
180da 6c 53 69 7a 65 50 74 72 28 70 50 61 72 65 6e 74  lSizePtr(pParent
180db 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20  , pCell);.      
180dc 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
180dd 20 20 69 4f 76 66 6c 53 70 61 63 65 20 2b 3d 20    iOvflSpace += 
180de 73 7a 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  sz;.      assert
180df 28 20 73 7a 3c 3d 70 42 74 2d 3e 70 61 67 65 53  ( sz<=pBt->pageS
180e0 69 7a 65 2f 34 20 29 3b 0a 20 20 20 20 20 20 61  ize/4 );.      a
180e1 73 73 65 72 74 28 20 69 4f 76 66 6c 53 70 61 63  ssert( iOvflSpac
180e2 65 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  e<=pBt->pageSize
180e3 20 29 3b 0a 20 20 20 20 20 20 69 6e 73 65 72 74   );.      insert
180e4 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 6e 78  Cell(pParent, nx
180e5 44 69 76 2c 20 70 43 65 6c 6c 2c 20 73 7a 2c 20  Div, pCell, sz, 
180e6 70 54 65 6d 70 2c 20 70 4e 65 77 2d 3e 70 67 6e  pTemp, pNew->pgn
180e7 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 69  o, &rc);.      i
180e8 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
180e9 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f   ) goto balance_
180ea 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 61  cleanup;.      a
180eb 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
180ec 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
180ed 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29  Parent->pDbPage)
180ee 20 29 3b 0a 0a 20 20 20 20 20 20 6a 2b 2b 3b 0a   );..      j++;.
180ef 20 20 20 20 20 20 6e 78 44 69 76 2b 2b 3b 0a 20        nxDiv++;. 
180f0 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72     }.  }.  asser
180f1 74 28 20 6a 3d 3d 6e 43 65 6c 6c 20 29 3b 0a 20  t( j==nCell );. 
180f2 20 61 73 73 65 72 74 28 20 6e 4f 6c 64 3e 30 20   assert( nOld>0 
180f3 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 4e 65  );.  assert( nNe
180f4 77 3e 30 20 29 3b 0a 20 20 69 66 28 20 28 70 61  w>0 );.  if( (pa
180f5 67 65 46 6c 61 67 73 20 26 20 50 54 46 5f 4c 45  geFlags & PTF_LE
180f6 41 46 29 3d 3d 30 20 29 7b 0a 20 20 20 20 75 38  AF)==0 ){.    u8
180f7 20 2a 7a 43 68 69 6c 64 20 3d 20 26 61 70 43 6f   *zChild = &apCo
180f8 70 79 5b 6e 4f 6c 64 2d 31 5d 2d 3e 61 44 61 74  py[nOld-1]->aDat
180f9 61 5b 38 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79  a[8];.    memcpy
180fa 28 26 61 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d 2d  (&apNew[nNew-1]-
180fb 3e 61 44 61 74 61 5b 38 5d 2c 20 7a 43 68 69 6c  >aData[8], zChil
180fc 64 2c 20 34 29 3b 0a 20 20 7d 0a 0a 20 20 69 66  d, 4);.  }..  if
180fd 28 20 69 73 52 6f 6f 74 20 26 26 20 70 50 61 72  ( isRoot && pPar
180fe 65 6e 74 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 26 26  ent->nCell==0 &&
180ff 20 70 50 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66   pParent->hdrOff
18100 73 65 74 3c 3d 61 70 4e 65 77 5b 30 5d 2d 3e 6e  set<=apNew[0]->n
18101 46 72 65 65 20 29 7b 0a 20 20 20 20 2f 2a 20 54  Free ){.    /* T
18102 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20  he root page of 
18103 74 68 65 20 62 2d 74 72 65 65 20 6e 6f 77 20 63  the b-tree now c
18104 6f 6e 74 61 69 6e 73 20 6e 6f 20 63 65 6c 6c 73  ontains no cells
18105 2e 20 54 68 65 20 6f 6e 6c 79 20 73 69 62 6c 69  . The only sibli
18106 6e 67 0a 20 20 20 20 2a 2a 20 70 61 67 65 20 69  ng.    ** page i
18107 73 20 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c  s the right-chil
18108 64 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74 2e  d of the parent.
18109 20 43 6f 70 79 20 74 68 65 20 63 6f 6e 74 65 6e   Copy the conten
1810a 74 73 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a  ts of the.    **
1810b 20 63 68 69 6c 64 20 70 61 67 65 20 69 6e 74 6f   child page into
1810c 20 74 68 65 20 70 61 72 65 6e 74 2c 20 64 65 63   the parent, dec
1810d 72 65 61 73 69 6e 67 20 74 68 65 20 6f 76 65 72  reasing the over
1810e 61 6c 6c 20 68 65 69 67 68 74 20 6f 66 20 74 68  all height of th
1810f 65 0a 20 20 20 20 2a 2a 20 62 2d 74 72 65 65 20  e.    ** b-tree 
18110 73 74 72 75 63 74 75 72 65 20 62 79 20 6f 6e 65  structure by one
18111 2e 20 54 68 69 73 20 69 73 20 64 65 73 63 72 69  . This is descri
18112 62 65 64 20 61 73 20 74 68 65 20 22 62 61 6c 61  bed as the "bala
18113 6e 63 65 2d 73 68 61 6c 6c 6f 77 65 72 22 0a 20  nce-shallower". 
18114 20 20 20 2a 2a 20 73 75 62 2d 61 6c 67 6f 72 69     ** sub-algori
18115 74 68 6d 20 69 6e 20 73 6f 6d 65 20 64 6f 63 75  thm in some docu
18116 6d 65 6e 74 61 74 69 6f 6e 2e 0a 20 20 20 20 2a  mentation..    *
18117 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73  *.    ** If this
18118 20 69 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 75   is an auto-vacu
18119 75 6d 20 64 61 74 61 62 61 73 65 2c 20 74 68 65  um database, the
1811a 20 63 61 6c 6c 20 74 6f 20 63 6f 70 79 4e 6f 64   call to copyNod
1811b 65 43 6f 6e 74 65 6e 74 28 29 20 0a 20 20 20 20  eContent() .    
1811c 2a 2a 20 73 65 74 73 20 61 6c 6c 20 70 6f 69 6e  ** sets all poin
1811d 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 20  ter-map entries 
1811e 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f  corresponding to
1811f 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 20   database image 
18120 70 61 67 65 73 20 0a 20 20 20 20 2a 2a 20 66 6f  pages .    ** fo
18121 72 20 77 68 69 63 68 20 74 68 65 20 70 6f 69 6e  r which the poin
18122 74 65 72 20 69 73 20 73 74 6f 72 65 64 20 77 69  ter is stored wi
18123 74 68 69 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74  thin the content
18124 20 62 65 69 6e 67 20 63 6f 70 69 65 64 2e 0a 20   being copied.. 
18125 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65     **.    ** The
18126 20 73 65 63 6f 6e 64 20 61 73 73 65 72 74 20 62   second assert b
18127 65 6c 6f 77 20 76 65 72 69 66 69 65 73 20 74 68  elow verifies th
18128 61 74 20 74 68 65 20 63 68 69 6c 64 20 70 61 67  at the child pag
18129 65 20 69 73 20 64 65 66 72 61 67 6d 65 6e 74 65  e is defragmente
1812a 64 0a 20 20 20 20 2a 2a 20 28 69 74 20 6d 75 73  d.    ** (it mus
1812b 74 20 62 65 2c 20 61 73 20 69 74 20 77 61 73 20  t be, as it was 
1812c 6a 75 73 74 20 72 65 63 6f 6e 73 74 72 75 63 74  just reconstruct
1812d 65 64 20 75 73 69 6e 67 20 61 73 73 65 6d 62 6c  ed using assembl
1812e 65 50 61 67 65 28 29 29 2e 20 54 68 69 73 0a 20  ePage()). This. 
1812f 20 20 20 2a 2a 20 69 73 20 69 6d 70 6f 72 74 61     ** is importa
18130 6e 74 20 69 66 20 74 68 65 20 70 61 72 65 6e 74  nt if the parent
18131 20 70 61 67 65 20 68 61 70 70 65 6e 73 20 74 6f   page happens to
18132 20 62 65 20 70 61 67 65 20 31 20 6f 66 20 74 68   be page 1 of th
18133 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 2a  e database.    *
18134 2a 20 69 6d 61 67 65 2e 20 20 2a 2f 0a 20 20 20  * image.  */.   
18135 20 61 73 73 65 72 74 28 20 6e 4e 65 77 3d 3d 31   assert( nNew==1
18136 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
18137 61 70 4e 65 77 5b 30 5d 2d 3e 6e 46 72 65 65 20  apNew[0]->nFree 
18138 3d 3d 20 0a 20 20 20 20 20 20 20 20 28 67 65 74  == .        (get
18139 32 62 79 74 65 28 26 61 70 4e 65 77 5b 30 5d 2d  2byte(&apNew[0]-
1813a 3e 61 44 61 74 61 5b 35 5d 29 2d 61 70 4e 65 77  >aData[5])-apNew
1813b 5b 30 5d 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 2d  [0]->cellOffset-
1813c 61 70 4e 65 77 5b 30 5d 2d 3e 6e 43 65 6c 6c 2a  apNew[0]->nCell*
1813d 32 29 20 0a 20 20 20 20 29 3b 0a 20 20 20 20 63  2) .    );.    c
1813e 6f 70 79 4e 6f 64 65 43 6f 6e 74 65 6e 74 28 61  opyNodeContent(a
1813f 70 4e 65 77 5b 30 5d 2c 20 70 50 61 72 65 6e 74  pNew[0], pParent
18140 2c 20 26 72 63 29 3b 0a 20 20 20 20 66 72 65 65  , &rc);.    free
18141 50 61 67 65 28 61 70 4e 65 77 5b 30 5d 2c 20 26  Page(apNew[0], &
18142 72 63 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  rc);.  }else if(
18143 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29 7b   ISAUTOVACUUM ){
18144 0a 20 20 20 20 2f 2a 20 46 69 78 20 74 68 65 20  .    /* Fix the 
18145 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72  pointer-map entr
18146 69 65 73 20 66 6f 72 20 61 6c 6c 20 74 68 65 20  ies for all the 
18147 63 65 6c 6c 73 20 74 68 61 74 20 77 65 72 65 20  cells that were 
18148 73 68 69 66 74 65 64 20 61 72 6f 75 6e 64 2e 20  shifted around. 
18149 0a 20 20 20 20 2a 2a 20 54 68 65 72 65 20 61 72  .    ** There ar
1814a 65 20 73 65 76 65 72 61 6c 20 64 69 66 66 65 72  e several differ
1814b 65 6e 74 20 74 79 70 65 73 20 6f 66 20 70 6f 69  ent types of poi
1814c 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73  nter-map entries
1814d 20 74 68 61 74 20 6e 65 65 64 20 74 6f 0a 20 20   that need to.  
1814e 20 20 2a 2a 20 62 65 20 64 65 61 6c 74 20 77 69    ** be dealt wi
1814f 74 68 20 62 79 20 74 68 69 73 20 72 6f 75 74 69  th by this routi
18150 6e 65 2e 20 53 6f 6d 65 20 6f 66 20 74 68 65 73  ne. Some of thes
18151 65 20 68 61 76 65 20 62 65 65 6e 20 73 65 74 20  e have been set 
18152 61 6c 72 65 61 64 79 2c 20 62 75 74 0a 20 20 20  already, but.   
18153 20 2a 2a 20 6d 61 6e 79 20 68 61 76 65 20 6e 6f   ** many have no
18154 74 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  t. The following
18155 20 69 73 20 61 20 73 75 6d 6d 61 72 79 3a 0a 20   is a summary:. 
18156 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 31     **.    **   1
18157 29 20 54 68 65 20 65 6e 74 72 69 65 73 20 61 73  ) The entries as
18158 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 6e 65  sociated with ne
18159 77 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20  w sibling pages 
1815a 74 68 61 74 20 77 65 72 65 20 6e 6f 74 0a 20 20  that were not.  
1815b 20 20 2a 2a 20 20 20 20 20 20 73 69 62 6c 69 6e    **      siblin
1815c 67 73 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e  gs when this fun
1815d 63 74 69 6f 6e 20 77 61 73 20 63 61 6c 6c 65 64  ction was called
1815e 2e 20 54 68 65 73 65 20 68 61 76 65 20 61 6c 72  . These have alr
1815f 65 61 64 79 0a 20 20 20 20 2a 2a 20 20 20 20 20  eady.    **     
18160 20 62 65 65 6e 20 73 65 74 2e 20 57 65 20 64 6f   been set. We do
18161 6e 27 74 20 6e 65 65 64 20 74 6f 20 77 6f 72 72  n't need to worr
18162 79 20 61 62 6f 75 74 20 6f 6c 64 20 73 69 62 6c  y about old sibl
18163 69 6e 67 73 20 74 68 61 74 20 77 65 72 65 0a 20  ings that were. 
18164 20 20 20 2a 2a 20 20 20 20 20 20 6d 6f 76 65 64     **      moved
18165 20 74 6f 20 74 68 65 20 66 72 65 65 2d 6c 69 73   to the free-lis
18166 74 20 2d 20 74 68 65 20 66 72 65 65 50 61 67 65  t - the freePage
18167 28 29 20 63 6f 64 65 20 68 61 73 20 74 61 6b 65  () code has take
18168 6e 20 63 61 72 65 0a 20 20 20 20 2a 2a 20 20 20  n care.    **   
18169 20 20 20 6f 66 20 74 68 6f 73 65 2e 0a 20 20 20     of those..   
1816a 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 32 29 20   **.    **   2) 
1816b 54 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20  The pointer-map 
1816c 65 6e 74 72 69 65 73 20 61 73 73 6f 63 69 61 74  entries associat
1816d 65 64 20 77 69 74 68 20 74 68 65 20 66 69 72 73  ed with the firs
1816e 74 20 6f 76 65 72 66 6c 6f 77 0a 20 20 20 20 2a  t overflow.    *
1816f 2a 20 20 20 20 20 20 70 61 67 65 20 69 6e 20 61  *      page in a
18170 6e 79 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69  ny overflow chai
18171 6e 73 20 75 73 65 64 20 62 79 20 6e 65 77 20 64  ns used by new d
18172 69 76 69 64 65 72 20 63 65 6c 6c 73 2e 20 54 68  ivider cells. Th
18173 65 73 65 20 0a 20 20 20 20 2a 2a 20 20 20 20 20  ese .    **     
18174 20 68 61 76 65 20 61 6c 73 6f 20 61 6c 72 65 61   have also alrea
18175 64 79 20 62 65 65 6e 20 74 61 6b 65 6e 20 63 61  dy been taken ca
18176 72 65 20 6f 66 20 62 79 20 74 68 65 20 69 6e 73  re of by the ins
18177 65 72 74 43 65 6c 6c 28 29 20 63 6f 64 65 2e 0a  ertCell() code..
18178 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
18179 33 29 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e  3) If the siblin
1817a 67 20 70 61 67 65 73 20 61 72 65 20 6e 6f 74 20  g pages are not 
1817b 6c 65 61 76 65 73 2c 20 74 68 65 6e 20 74 68 65  leaves, then the
1817c 20 63 68 69 6c 64 20 70 61 67 65 73 20 6f 66 0a   child pages of.
1817d 20 20 20 20 2a 2a 20 20 20 20 20 20 63 65 6c 6c      **      cell
1817e 73 20 73 74 6f 72 65 64 20 6f 6e 20 74 68 65 20  s stored on the 
1817f 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 6d 61  sibling pages ma
18180 79 20 6e 65 65 64 20 74 6f 20 62 65 20 75 70 64  y need to be upd
18181 61 74 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  ated..    **.   
18182 20 2a 2a 20 20 20 34 29 20 49 66 20 74 68 65 20   **   4) If the 
18183 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 61 72  sibling pages ar
18184 65 20 6e 6f 74 20 69 6e 74 65 72 6e 61 6c 20 69  e not internal i
18185 6e 74 6b 65 79 20 6e 6f 64 65 73 2c 20 74 68 65  ntkey nodes, the
18186 6e 20 61 6e 79 0a 20 20 20 20 2a 2a 20 20 20 20  n any.    **    
18187 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73    overflow pages
18188 20 75 73 65 64 20 62 79 20 74 68 65 73 65 20 63   used by these c
18189 65 6c 6c 73 20 6d 61 79 20 6e 65 65 64 20 74 6f  ells may need to
1818a 20 62 65 20 75 70 64 61 74 65 64 0a 20 20 20 20   be updated.    
1818b 2a 2a 20 20 20 20 20 20 28 69 6e 74 65 72 6e 61  **      (interna
1818c 6c 20 69 6e 74 6b 65 79 20 6e 6f 64 65 73 20 6e  l intkey nodes n
1818d 65 76 65 72 20 63 6f 6e 74 61 69 6e 20 70 6f 69  ever contain poi
1818e 6e 74 65 72 73 20 74 6f 20 6f 76 65 72 66 6c 6f  nters to overflo
1818f 77 20 70 61 67 65 73 29 2e 0a 20 20 20 20 2a 2a  w pages)..    **
18190 0a 20 20 20 20 2a 2a 20 20 20 35 29 20 49 66 20  .    **   5) If 
18191 74 68 65 20 73 69 62 6c 69 6e 67 20 70 61 67 65  the sibling page
18192 73 20 61 72 65 20 6e 6f 74 20 6c 65 61 76 65 73  s are not leaves
18193 2c 20 74 68 65 6e 20 74 68 65 20 70 6f 69 6e 74  , then the point
18194 65 72 2d 6d 61 70 0a 20 20 20 20 2a 2a 20 20 20  er-map.    **   
18195 20 20 20 65 6e 74 72 69 65 73 20 66 6f 72 20 74     entries for t
18196 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 70  he right-child p
18197 61 67 65 73 20 6f 66 20 65 61 63 68 20 73 69 62  ages of each sib
18198 6c 69 6e 67 20 6d 61 79 20 6e 65 65 64 0a 20 20  ling may need.  
18199 20 20 2a 2a 20 20 20 20 20 20 74 6f 20 62 65 20    **      to be 
1819a 75 70 64 61 74 65 64 2e 0a 20 20 20 20 2a 2a 0a  updated..    **.
1819b 20 20 20 20 2a 2a 20 43 61 73 65 73 20 31 20 61      ** Cases 1 a
1819c 6e 64 20 32 20 61 72 65 20 64 65 61 6c 74 20 77  nd 2 are dealt w
1819d 69 74 68 20 61 62 6f 76 65 20 62 79 20 6f 74 68  ith above by oth
1819e 65 72 20 63 6f 64 65 2e 20 54 68 65 20 6e 65 78  er code. The nex
1819f 74 0a 20 20 20 20 2a 2a 20 62 6c 6f 63 6b 20 64  t.    ** block d
181a0 65 61 6c 73 20 77 69 74 68 20 63 61 73 65 73 20  eals with cases 
181a1 33 20 61 6e 64 20 34 20 61 6e 64 20 74 68 65 20  3 and 4 and the 
181a2 6f 6e 65 20 61 66 74 65 72 20 74 68 61 74 2c 20  one after that, 
181a3 63 61 73 65 20 35 2e 20 53 69 6e 63 65 0a 20 20  case 5. Since.  
181a4 20 20 2a 2a 20 73 65 74 74 69 6e 67 20 61 20 70    ** setting a p
181a5 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79  ointer map entry
181a6 20 69 73 20 61 20 72 65 6c 61 74 69 76 65 6c 79   is a relatively
181a7 20 65 78 70 65 6e 73 69 76 65 20 6f 70 65 72 61   expensive opera
181a8 74 69 6f 6e 2c 20 74 68 69 73 0a 20 20 20 20 2a  tion, this.    *
181a9 2a 20 63 6f 64 65 20 6f 6e 6c 79 20 73 65 74 73  * code only sets
181aa 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74   pointer map ent
181ab 72 69 65 73 20 66 6f 72 20 63 68 69 6c 64 20 6f  ries for child o
181ac 72 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  r overflow pages
181ad 20 74 68 61 74 20 68 61 76 65 0a 20 20 20 20 2a   that have.    *
181ae 2a 20 61 63 74 75 61 6c 6c 79 20 6d 6f 76 65 64  * actually moved
181af 20 62 65 74 77 65 65 6e 20 70 61 67 65 73 2e 20   between pages. 
181b0 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 20   */.    MemPage 
181b1 2a 70 4e 65 77 20 3d 20 61 70 4e 65 77 5b 30 5d  *pNew = apNew[0]
181b2 3b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70  ;.    MemPage *p
181b3 4f 6c 64 20 3d 20 61 70 43 6f 70 79 5b 30 5d 3b  Old = apCopy[0];
181b4 0a 20 20 20 20 69 6e 74 20 6e 4f 76 65 72 66 6c  .    int nOverfl
181b5 6f 77 20 3d 20 70 4f 6c 64 2d 3e 6e 4f 76 65 72  ow = pOld->nOver
181b6 66 6c 6f 77 3b 0a 20 20 20 20 69 6e 74 20 69 4e  flow;.    int iN
181b7 65 78 74 4f 6c 64 20 3d 20 70 4f 6c 64 2d 3e 6e  extOld = pOld->n
181b8 43 65 6c 6c 20 2b 20 6e 4f 76 65 72 66 6c 6f 77  Cell + nOverflow
181b9 3b 0a 20 20 20 20 69 6e 74 20 69 4f 76 65 72 66  ;.    int iOverf
181ba 6c 6f 77 20 3d 20 28 6e 4f 76 65 72 66 6c 6f 77  low = (nOverflow
181bb 20 3f 20 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 30   ? pOld->aOvfl[0
181bc 5d 2e 69 64 78 20 3a 20 2d 31 29 3b 0a 20 20 20  ].idx : -1);.   
181bd 20 6a 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20   j = 0;         
181be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
181bf 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 27      /* Current '
181c0 6f 6c 64 27 20 73 69 62 6c 69 6e 67 20 70 61 67  old' sibling pag
181c1 65 20 2a 2f 0a 20 20 20 20 6b 20 3d 20 30 3b 20  e */.    k = 0; 
181c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
181c3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
181c4 75 72 72 65 6e 74 20 27 6e 65 77 27 20 73 69 62  urrent 'new' sib
181c5 6c 69 6e 67 20 70 61 67 65 20 2a 2f 0a 20 20 20  ling page */.   
181c6 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c   for(i=0; i<nCel
181c7 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  l; i++){.      i
181c8 6e 74 20 69 73 44 69 76 69 64 65 72 20 3d 20 30  nt isDivider = 0
181c9 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 69  ;.      while( i
181ca 3d 3d 69 4e 65 78 74 4f 6c 64 20 29 7b 0a 20 20  ==iNextOld ){.  
181cb 20 20 20 20 20 20 2f 2a 20 43 65 6c 6c 20 69 20        /* Cell i 
181cc 69 73 20 74 68 65 20 63 65 6c 6c 20 69 6d 6d 65  is the cell imme
181cd 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e  diately followin
181ce 67 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c 20  g the last cell 
181cf 6f 6e 20 6f 6c 64 0a 20 20 20 20 20 20 20 20 2a  on old.        *
181d0 2a 20 73 69 62 6c 69 6e 67 20 70 61 67 65 20 6a  * sibling page j
181d1 2e 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67  . If the sibling
181d2 73 20 61 72 65 20 6e 6f 74 20 6c 65 61 66 20 70  s are not leaf p
181d3 61 67 65 73 20 6f 66 20 61 6e 0a 20 20 20 20 20  ages of an.     
181d4 20 20 20 2a 2a 20 69 6e 74 6b 65 79 20 62 2d 74     ** intkey b-t
181d5 72 65 65 2c 20 74 68 65 6e 20 63 65 6c 6c 20 69  ree, then cell i
181d6 20 77 61 73 20 61 20 64 69 76 69 64 65 72 20 63   was a divider c
181d7 65 6c 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  ell. */.        
181d8 70 4f 6c 64 20 3d 20 61 70 43 6f 70 79 5b 2b 2b  pOld = apCopy[++
181d9 6a 5d 3b 0a 20 20 20 20 20 20 20 20 69 4e 65 78  j];.        iNex
181da 74 4f 6c 64 20 3d 20 69 20 2b 20 21 6c 65 61 66  tOld = i + !leaf
181db 44 61 74 61 20 2b 20 70 4f 6c 64 2d 3e 6e 43 65  Data + pOld->nCe
181dc 6c 6c 20 2b 20 70 4f 6c 64 2d 3e 6e 4f 76 65 72  ll + pOld->nOver
181dd 66 6c 6f 77 3b 0a 20 20 20 20 20 20 20 20 69 66  flow;.        if
181de 28 20 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f  ( pOld->nOverflo
181df 77 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e  w ){.          n
181e0 4f 76 65 72 66 6c 6f 77 20 3d 20 70 4f 6c 64 2d  Overflow = pOld-
181e1 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20  >nOverflow;.    
181e2 20 20 20 20 20 20 69 4f 76 65 72 66 6c 6f 77 20        iOverflow 
181e3 3d 20 69 20 2b 20 21 6c 65 61 66 44 61 74 61 20  = i + !leafData 
181e4 2b 20 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 30 5d  + pOld->aOvfl[0]
181e5 2e 69 64 78 3b 0a 20 20 20 20 20 20 20 20 7d 0a  .idx;.        }.
181e6 20 20 20 20 20 20 20 20 69 73 44 69 76 69 64 65          isDivide
181e7 72 20 3d 20 21 6c 65 61 66 44 61 74 61 3b 20 20  r = !leafData;  
181e8 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
181e9 61 73 73 65 72 74 28 6e 4f 76 65 72 66 6c 6f 77  assert(nOverflow
181ea 3e 30 20 7c 7c 20 69 4f 76 65 72 66 6c 6f 77 3c  >0 || iOverflow<
181eb 69 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  i );.      asser
181ec 74 28 6e 4f 76 65 72 66 6c 6f 77 3c 32 20 7c 7c  t(nOverflow<2 ||
181ed 20 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 30 5d 2e   pOld->aOvfl[0].
181ee 69 64 78 3d 3d 70 4f 6c 64 2d 3e 61 4f 76 66 6c  idx==pOld->aOvfl
181ef 5b 31 5d 2e 69 64 78 2d 31 29 3b 0a 20 20 20 20  [1].idx-1);.    
181f0 20 20 61 73 73 65 72 74 28 6e 4f 76 65 72 66 6c    assert(nOverfl
181f1 6f 77 3c 33 20 7c 7c 20 70 4f 6c 64 2d 3e 61 4f  ow<3 || pOld->aO
181f2 76 66 6c 5b 31 5d 2e 69 64 78 3d 3d 70 4f 6c 64  vfl[1].idx==pOld
181f3 2d 3e 61 4f 76 66 6c 5b 32 5d 2e 69 64 78 2d 31  ->aOvfl[2].idx-1
181f4 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d  );.      if( i==
181f5 69 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20  iOverflow ){.   
181f6 20 20 20 20 20 69 73 44 69 76 69 64 65 72 20 3d       isDivider =
181f7 20 31 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   1;.        if( 
181f8 28 2d 2d 6e 4f 76 65 72 66 6c 6f 77 29 3e 30 20  (--nOverflow)>0 
181f9 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 4f 76  ){.          iOv
181fa 65 72 66 6c 6f 77 2b 2b 3b 0a 20 20 20 20 20 20  erflow++;.      
181fb 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20    }.      }..   
181fc 20 20 20 69 66 28 20 69 3d 3d 63 6e 74 4e 65 77     if( i==cntNew
181fd 5b 6b 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  [k] ){.        /
181fe 2a 20 43 65 6c 6c 20 69 20 69 73 20 74 68 65 20  * Cell i is the 
181ff 63 65 6c 6c 20 69 6d 6d 65 64 69 61 74 65 6c 79  cell immediately
18200 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 6c   following the l
18201 61 73 74 20 63 65 6c 6c 20 6f 6e 20 6e 65 77 0a  ast cell on new.
18202 20 20 20 20 20 20 20 20 2a 2a 20 73 69 62 6c 69          ** sibli
18203 6e 67 20 70 61 67 65 20 6b 2e 20 49 66 20 74 68  ng page k. If th
18204 65 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6e  e siblings are n
18205 6f 74 20 6c 65 61 66 20 70 61 67 65 73 20 6f 66  ot leaf pages of
18206 20 61 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 69   an.        ** i
18207 6e 74 6b 65 79 20 62 2d 74 72 65 65 2c 20 74 68  ntkey b-tree, th
18208 65 6e 20 63 65 6c 6c 20 69 20 69 73 20 61 20 64  en cell i is a d
18209 69 76 69 64 65 72 20 63 65 6c 6c 2e 20 20 2a 2f  ivider cell.  */
1820a 0a 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d 20  .        pNew = 
1820b 61 70 4e 65 77 5b 2b 2b 6b 5d 3b 0a 20 20 20 20  apNew[++k];.    
1820c 20 20 20 20 69 66 28 20 21 6c 65 61 66 44 61 74      if( !leafDat
1820d 61 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  a ) continue;.  
1820e 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65      }.      asse
1820f 72 74 28 20 6a 3c 6e 4f 6c 64 20 29 3b 0a 20 20  rt( j<nOld );.  
18210 20 20 20 20 61 73 73 65 72 74 28 20 6b 3c 6e 4e      assert( k<nN
18211 65 77 20 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  ew );..      /* 
18212 49 66 20 74 68 65 20 63 65 6c 6c 20 77 61 73 20  If the cell was 
18213 6f 72 69 67 69 6e 61 6c 6c 79 20 64 69 76 69 64  originally divid
18214 65 72 20 63 65 6c 6c 20 28 61 6e 64 20 69 73 20  er cell (and is 
18215 6e 6f 74 20 6e 6f 77 29 20 6f 72 0a 20 20 20 20  not now) or.    
18216 20 20 2a 2a 20 61 6e 20 6f 76 65 72 66 6c 6f 77    ** an overflow
18217 20 63 65 6c 6c 2c 20 6f 72 20 69 66 20 74 68 65   cell, or if the
18218 20 63 65 6c 6c 20 77 61 73 20 6c 6f 63 61 74 65   cell was locate
18219 64 20 6f 6e 20 61 20 64 69 66 66 65 72 65 6e 74  d on a different
1821a 20 73 69 62 6c 69 6e 67 0a 20 20 20 20 20 20 2a   sibling.      *
1821b 2a 20 70 61 67 65 20 62 65 66 6f 72 65 20 74 68  * page before th
1821c 65 20 62 61 6c 61 6e 63 69 6e 67 2c 20 74 68 65  e balancing, the
1821d 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61  n the pointer ma
1821e 70 20 65 6e 74 72 69 65 73 20 61 73 73 6f 63 69  p entries associ
1821f 61 74 65 64 0a 20 20 20 20 20 20 2a 2a 20 77 69  ated.      ** wi
18220 74 68 20 61 6e 79 20 63 68 69 6c 64 20 6f 72 20  th any child or 
18221 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 6e  overflow pages n
18222 65 65 64 20 74 6f 20 62 65 20 75 70 64 61 74 65  eed to be update
18223 64 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  d.  */.      if(
18224 20 69 73 44 69 76 69 64 65 72 20 7c 7c 20 70 4f   isDivider || pO
18225 6c 64 2d 3e 70 67 6e 6f 21 3d 70 4e 65 77 2d 3e  ld->pgno!=pNew->
18226 70 67 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 20  pgno ){.        
18227 69 66 28 20 21 6c 65 61 66 43 6f 72 72 65 63 74  if( !leafCorrect
18228 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ion ){.         
18229 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20   ptrmapPut(pBt, 
1822a 67 65 74 34 62 79 74 65 28 61 70 43 65 6c 6c 5b  get4byte(apCell[
1822b 69 5d 29 2c 20 50 54 52 4d 41 50 5f 42 54 52 45  i]), PTRMAP_BTRE
1822c 45 2c 20 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 26  E, pNew->pgno, &
1822d 72 63 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  rc);.        }. 
1822e 20 20 20 20 20 20 20 69 66 28 20 73 7a 43 65 6c         if( szCel
1822f 6c 5b 69 5d 3e 70 4e 65 77 2d 3e 6d 69 6e 4c 6f  l[i]>pNew->minLo
18230 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20  cal ){.         
18231 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74   ptrmapPutOvflPt
18232 72 28 70 4e 65 77 2c 20 61 70 43 65 6c 6c 5b 69  r(pNew, apCell[i
18233 5d 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 20  ], &rc);.       
18234 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
18235 0a 0a 20 20 20 20 69 66 28 20 21 6c 65 61 66 43  ..    if( !leafC
18236 6f 72 72 65 63 74 69 6f 6e 20 29 7b 0a 20 20 20  orrection ){.   
18237 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e     for(i=0; i<nN
18238 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ew; i++){.      
18239 20 20 75 33 32 20 6b 65 79 20 3d 20 67 65 74 34    u32 key = get4
1823a 62 79 74 65 28 26 61 70 4e 65 77 5b 69 5d 2d 3e  byte(&apNew[i]->
1823b 61 44 61 74 61 5b 38 5d 29 3b 0a 20 20 20 20 20  aData[8]);.     
1823c 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74     ptrmapPut(pBt
1823d 2c 20 6b 65 79 2c 20 50 54 52 4d 41 50 5f 42 54  , key, PTRMAP_BT
1823e 52 45 45 2c 20 61 70 4e 65 77 5b 69 5d 2d 3e 70  REE, apNew[i]->p
1823f 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 20  gno, &rc);.     
18240 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 66 20 30 0a   }.    }..#if 0.
18241 20 20 20 20 2f 2a 20 54 68 65 20 70 74 72 6d 61      /* The ptrma
18242 70 43 68 65 63 6b 50 61 67 65 73 28 29 20 63 6f  pCheckPages() co
18243 6e 74 61 69 6e 73 20 61 73 73 65 72 74 28 29 20  ntains assert() 
18244 73 74 61 74 65 6d 65 6e 74 73 20 74 68 61 74 20  statements that 
18245 76 65 72 69 66 79 20 74 68 61 74 0a 20 20 20 20  verify that.    
18246 2a 2a 20 61 6c 6c 20 70 6f 69 6e 74 65 72 20 6d  ** all pointer m
18247 61 70 20 70 61 67 65 73 20 61 72 65 20 73 65 74  ap pages are set
18248 20 63 6f 72 72 65 63 74 6c 79 2e 20 54 68 69 73   correctly. This
18249 20 69 73 20 68 65 6c 70 66 75 6c 20 77 68 69 6c   is helpful whil
1824a 65 20 0a 20 20 20 20 2a 2a 20 64 65 62 75 67 67  e .    ** debugg
1824b 69 6e 67 2e 20 54 68 69 73 20 69 73 20 75 73 75  ing. This is usu
1824c 61 6c 6c 79 20 64 69 73 61 62 6c 65 64 20 62 65  ally disabled be
1824d 63 61 75 73 65 20 61 20 63 6f 72 72 75 70 74 20  cause a corrupt 
1824e 64 61 74 61 62 61 73 65 20 6d 61 79 0a 20 20 20  database may.   
1824f 20 2a 2a 20 63 61 75 73 65 20 61 6e 20 61 73 73   ** cause an ass
18250 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 20  ert() statement 
18251 74 6f 20 66 61 69 6c 2e 20 20 2a 2f 0a 20 20 20  to fail.  */.   
18252 20 70 74 72 6d 61 70 43 68 65 63 6b 50 61 67 65   ptrmapCheckPage
18253 73 28 61 70 4e 65 77 2c 20 6e 4e 65 77 29 3b 0a  s(apNew, nNew);.
18254 20 20 20 20 70 74 72 6d 61 70 43 68 65 63 6b 50      ptrmapCheckP
18255 61 67 65 73 28 26 70 50 61 72 65 6e 74 2c 20 31  ages(&pParent, 1
18256 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20  );.#endif.  }.. 
18257 20 61 73 73 65 72 74 28 20 70 50 61 72 65 6e 74   assert( pParent
18258 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 54 52  ->isInit );.  TR
18259 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 66  ACE(("BALANCE: f
1825a 69 6e 69 73 68 65 64 3a 20 6f 6c 64 3d 25 64 20  inished: old=%d 
1825b 6e 65 77 3d 25 64 20 63 65 6c 6c 73 3d 25 64 5c  new=%d cells=%d\
1825c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 6e 4f  n",.          nO
1825d 6c 64 2c 20 6e 4e 65 77 2c 20 6e 43 65 6c 6c 29  ld, nNew, nCell)
1825e 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 6c  );..  /*.  ** Cl
1825f 65 61 6e 75 70 20 62 65 66 6f 72 65 20 72 65 74  eanup before ret
18260 75 72 6e 69 6e 67 2e 0a 20 20 2a 2f 0a 62 61 6c  urning..  */.bal
18261 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3a 0a 20 20  ance_cleanup:.  
18262 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 46 72  sqlite3ScratchFr
18263 65 65 28 61 70 43 65 6c 6c 29 3b 0a 20 20 66 6f  ee(apCell);.  fo
18264 72 28 69 3d 30 3b 20 69 3c 6e 4f 6c 64 3b 20 69  r(i=0; i<nOld; i
18265 2b 2b 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65  ++){.    release
18266 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d 29 3b 0a  Page(apOld[i]);.
18267 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69    }.  for(i=0; i
18268 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20  <nNew; i++){.   
18269 20 72 65 6c 65 61 73 65 50 61 67 65 28 61 70 4e   releasePage(apN
1826a 65 77 5b 69 5d 29 3b 0a 20 20 7d 0a 0a 20 20 72  ew[i]);.  }..  r
1826b 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a  eturn rc;.}.../*
1826c 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
1826d 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e  n is called when
1826e 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f   the root page o
1826f 66 20 61 20 62 2d 74 72 65 65 20 73 74 72 75 63  f a b-tree struc
18270 74 75 72 65 20 69 73 0a 2a 2a 20 6f 76 65 72 66  ture is.** overf
18271 75 6c 6c 20 28 68 61 73 20 6f 6e 65 20 6f 72 20  ull (has one or 
18272 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 61  more overflow pa
18273 67 65 73 29 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65  ges)..**.** A ne
18274 77 20 63 68 69 6c 64 20 70 61 67 65 20 69 73 20  w child page is 
18275 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 68  allocated and th
18276 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  e contents of th
18277 65 20 63 75 72 72 65 6e 74 20 72 6f 6f 74 0a 2a  e current root.*
18278 2a 20 70 61 67 65 2c 20 69 6e 63 6c 75 64 69 6e  * page, includin
18279 67 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73  g overflow cells
1827a 2c 20 61 72 65 20 63 6f 70 69 65 64 20 69 6e 74  , are copied int
1827b 6f 20 74 68 65 20 63 68 69 6c 64 2e 20 54 68 65  o the child. The
1827c 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 69 73   root.** page is
1827d 20 74 68 65 6e 20 6f 76 65 72 77 72 69 74 74 65   then overwritte
1827e 6e 20 74 6f 20 6d 61 6b 65 20 69 74 20 61 6e 20  n to make it an 
1827f 65 6d 70 74 79 20 70 61 67 65 20 77 69 74 68 20  empty page with 
18280 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20  the right-child 
18281 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e  .** pointer poin
18282 74 69 6e 67 20 74 6f 20 74 68 65 20 6e 65 77 20  ting to the new 
18283 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 42 65 66 6f  page..**.** Befo
18284 72 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 61 6c  re returning, al
18285 6c 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e  l pointer-map en
18286 74 72 69 65 73 20 63 6f 72 72 65 73 70 6f 6e 64  tries correspond
18287 69 6e 67 20 74 6f 20 70 61 67 65 73 20 0a 2a 2a  ing to pages .**
18288 20 74 68 61 74 20 74 68 65 20 6e 65 77 20 63 68   that the new ch
18289 69 6c 64 2d 70 61 67 65 20 6e 6f 77 20 63 6f 6e  ild-page now con
1828a 74 61 69 6e 73 20 70 6f 69 6e 74 65 72 73 20 74  tains pointers t
1828b 6f 20 61 72 65 20 75 70 64 61 74 65 64 2e 20 54  o are updated. T
1828c 68 65 0a 2a 2a 20 65 6e 74 72 79 20 63 6f 72 72  he.** entry corr
1828d 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65  esponding to the
1828e 20 6e 65 77 20 72 69 67 68 74 2d 63 68 69 6c 64   new right-child
1828f 20 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 65 20   pointer of the 
18290 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 69 73 20  root.** page is 
18291 61 6c 73 6f 20 75 70 64 61 74 65 64 2e 0a 2a 2a  also updated..**
18292 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75  .** If successfu
18293 6c 2c 20 2a 70 70 43 68 69 6c 64 20 69 73 20 73  l, *ppChild is s
18294 65 74 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20  et to contain a 
18295 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65  reference to the
18296 20 63 68 69 6c 64 20 0a 2a 2a 20 70 61 67 65 20   child .** page 
18297 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  and SQLITE_OK is
18298 20 72 65 74 75 72 6e 65 64 2e 20 49 6e 20 74 68   returned. In th
18299 69 73 20 63 61 73 65 20 74 68 65 20 63 61 6c 6c  is case the call
1829a 65 72 20 69 73 20 72 65 71 75 69 72 65 64 0a 2a  er is required.*
1829b 2a 20 74 6f 20 63 61 6c 6c 20 72 65 6c 65 61 73  * to call releas
1829c 65 50 61 67 65 28 29 20 6f 6e 20 2a 70 70 43 68  ePage() on *ppCh
1829d 69 6c 64 20 65 78 61 63 74 6c 79 20 6f 6e 63 65  ild exactly once
1829e 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  . If an error oc
1829f 63 75 72 73 2c 0a 2a 2a 20 61 6e 20 65 72 72 6f  curs,.** an erro
182a0 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
182a1 65 64 20 61 6e 64 20 2a 70 70 43 68 69 6c 64 20  ed and *ppChild 
182a2 69 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2f 0a  is set to 0..*/.
182a3 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e  static int balan
182a4 63 65 5f 64 65 65 70 65 72 28 4d 65 6d 50 61 67  ce_deeper(MemPag
182a5 65 20 2a 70 52 6f 6f 74 2c 20 4d 65 6d 50 61 67  e *pRoot, MemPag
182a6 65 20 2a 2a 70 70 43 68 69 6c 64 29 7b 0a 20 20  e **ppChild){.  
182a7 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
182a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
182a9 2a 20 52 65 74 75 72 6e 20 76 61 6c 75 65 20 66  * Return value f
182aa 72 6f 6d 20 73 75 62 70 72 6f 63 65 64 75 72 65  rom subprocedure
182ab 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a  s */.  MemPage *
182ac 70 43 68 69 6c 64 20 3d 20 30 3b 20 20 20 20 20  pChild = 0;     
182ad 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
182ae 20 74 6f 20 61 20 6e 65 77 20 63 68 69 6c 64 20   to a new child 
182af 70 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70  page */.  Pgno p
182b0 67 6e 6f 43 68 69 6c 64 20 3d 20 30 3b 20 20 20  gnoChild = 0;   
182b1 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65           /* Page
182b2 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e   number of the n
182b3 65 77 20 63 68 69 6c 64 20 70 61 67 65 20 2a 2f  ew child page */
182b4 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
182b5 20 3d 20 70 52 6f 6f 74 2d 3e 70 42 74 3b 20 20   = pRoot->pBt;  
182b6 20 20 2f 2a 20 54 68 65 20 42 54 72 65 65 20 2a    /* The BTree *
182b7 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 52 6f  /..  assert( pRo
182b8 6f 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20  ot->nOverflow>0 
182b9 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
182ba 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
182bb 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a  pBt->mutex) );..
182bc 20 20 2f 2a 20 4d 61 6b 65 20 70 52 6f 6f 74 2c    /* Make pRoot,
182bd 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f   the root page o
182be 66 20 74 68 65 20 62 2d 74 72 65 65 2c 20 77 72  f the b-tree, wr
182bf 69 74 61 62 6c 65 2e 20 41 6c 6c 6f 63 61 74 65  itable. Allocate
182c0 20 61 20 6e 65 77 20 0a 20 20 2a 2a 20 70 61 67   a new .  ** pag
182c1 65 20 74 68 61 74 20 77 69 6c 6c 20 62 65 63 6f  e that will beco
182c2 6d 65 20 74 68 65 20 6e 65 77 20 72 69 67 68 74  me the new right
182c3 2d 63 68 69 6c 64 20 6f 66 20 70 50 61 67 65 2e  -child of pPage.
182c4 20 43 6f 70 79 20 74 68 65 20 63 6f 6e 74 65 6e   Copy the conten
182c5 74 73 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 6e  ts.  ** of the n
182c6 6f 64 65 20 73 74 6f 72 65 64 20 6f 6e 20 70 52  ode stored on pR
182c7 6f 6f 74 20 69 6e 74 6f 20 74 68 65 20 6e 65 77  oot into the new
182c8 20 63 68 69 6c 64 20 70 61 67 65 2e 0a 20 20 2a   child page..  *
182c9 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  /.  rc = sqlite3
182ca 50 61 67 65 72 57 72 69 74 65 28 70 52 6f 6f 74  PagerWrite(pRoot
182cb 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66  ->pDbPage);.  if
182cc 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
182cd 29 7b 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f  ){.    rc = allo
182ce 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42  cateBtreePage(pB
182cf 74 2c 26 70 43 68 69 6c 64 2c 26 70 67 6e 6f 43  t,&pChild,&pgnoC
182d0 68 69 6c 64 2c 70 52 6f 6f 74 2d 3e 70 67 6e 6f  hild,pRoot->pgno
182d1 2c 30 29 3b 0a 20 20 20 20 63 6f 70 79 4e 6f 64  ,0);.    copyNod
182d2 65 43 6f 6e 74 65 6e 74 28 70 52 6f 6f 74 2c 20  eContent(pRoot, 
182d3 70 43 68 69 6c 64 2c 20 26 72 63 29 3b 0a 20 20  pChild, &rc);.  
182d4 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55    if( ISAUTOVACU
182d5 55 4d 20 29 7b 0a 20 20 20 20 20 20 70 74 72 6d  UM ){.      ptrm
182d6 61 70 50 75 74 28 70 42 74 2c 20 70 67 6e 6f 43  apPut(pBt, pgnoC
182d7 68 69 6c 64 2c 20 50 54 52 4d 41 50 5f 42 54 52  hild, PTRMAP_BTR
182d8 45 45 2c 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 2c  EE, pRoot->pgno,
182d9 20 26 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d   &rc);.    }.  }
182da 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20  .  if( rc ){.   
182db 20 2a 70 70 43 68 69 6c 64 20 3d 20 30 3b 0a 20   *ppChild = 0;. 
182dc 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
182dd 43 68 69 6c 64 29 3b 0a 20 20 20 20 72 65 74 75  Child);.    retu
182de 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73  rn rc;.  }.  ass
182df 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65  ert( sqlite3Page
182e0 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 43 68  rIswriteable(pCh
182e1 69 6c 64 2d 3e 70 44 62 50 61 67 65 29 20 29 3b  ild->pDbPage) );
182e2 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
182e3 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
182e4 6c 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67  le(pRoot->pDbPag
182e5 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  e) );.  assert( 
182e6 70 43 68 69 6c 64 2d 3e 6e 43 65 6c 6c 3d 3d 70  pChild->nCell==p
182e7 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a  Root->nCell );..
182e8 20 20 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43    TRACE(("BALANC
182e9 45 3a 20 63 6f 70 79 20 72 6f 6f 74 20 25 64 20  E: copy root %d 
182ea 69 6e 74 6f 20 25 64 5c 6e 22 2c 20 70 52 6f 6f  into %d\n", pRoo
182eb 74 2d 3e 70 67 6e 6f 2c 20 70 43 68 69 6c 64 2d  t->pgno, pChild-
182ec 3e 70 67 6e 6f 29 29 3b 0a 0a 20 20 2f 2a 20 43  >pgno));..  /* C
182ed 6f 70 79 20 74 68 65 20 6f 76 65 72 66 6c 6f 77  opy the overflow
182ee 20 63 65 6c 6c 73 20 66 72 6f 6d 20 70 52 6f 6f   cells from pRoo
182ef 74 20 74 6f 20 70 43 68 69 6c 64 20 2a 2f 0a 20  t to pChild */. 
182f0 20 6d 65 6d 63 70 79 28 70 43 68 69 6c 64 2d 3e   memcpy(pChild->
182f1 61 4f 76 66 6c 2c 20 70 52 6f 6f 74 2d 3e 61 4f  aOvfl, pRoot->aO
182f2 76 66 6c 2c 20 70 52 6f 6f 74 2d 3e 6e 4f 76 65  vfl, pRoot->nOve
182f3 72 66 6c 6f 77 2a 73 69 7a 65 6f 66 28 70 52 6f  rflow*sizeof(pRo
182f4 6f 74 2d 3e 61 4f 76 66 6c 5b 30 5d 29 29 3b 0a  ot->aOvfl[0]));.
182f5 20 20 70 43 68 69 6c 64 2d 3e 6e 4f 76 65 72 66    pChild->nOverf
182f6 6c 6f 77 20 3d 20 70 52 6f 6f 74 2d 3e 6e 4f 76  low = pRoot->nOv
182f7 65 72 66 6c 6f 77 3b 0a 0a 20 20 2f 2a 20 5a 65  erflow;..  /* Ze
182f8 72 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  ro the contents 
182f9 6f 66 20 70 52 6f 6f 74 2e 20 54 68 65 6e 20 69  of pRoot. Then i
182fa 6e 73 74 61 6c 6c 20 70 43 68 69 6c 64 20 61 73  nstall pChild as
182fb 20 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64   the right-child
182fc 2e 20 2a 2f 0a 20 20 7a 65 72 6f 50 61 67 65 28  . */.  zeroPage(
182fd 70 52 6f 6f 74 2c 20 70 43 68 69 6c 64 2d 3e 61  pRoot, pChild->a
182fe 44 61 74 61 5b 30 5d 20 26 20 7e 50 54 46 5f 4c  Data[0] & ~PTF_L
182ff 45 41 46 29 3b 0a 20 20 70 75 74 34 62 79 74 65  EAF);.  put4byte
18300 28 26 70 52 6f 6f 74 2d 3e 61 44 61 74 61 5b 70  (&pRoot->aData[p
18301 52 6f 6f 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b  Root->hdrOffset+
18302 38 5d 2c 20 70 67 6e 6f 43 68 69 6c 64 29 3b 0a  8], pgnoChild);.
18303 0a 20 20 2a 70 70 43 68 69 6c 64 20 3d 20 70 43  .  *ppChild = pC
18304 68 69 6c 64 3b 0a 20 20 72 65 74 75 72 6e 20 53  hild;.  return S
18305 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
18306 2a 2a 20 54 68 65 20 70 61 67 65 20 74 68 61 74  ** The page that
18307 20 70 43 75 72 20 63 75 72 72 65 6e 74 6c 79 20   pCur currently 
18308 70 6f 69 6e 74 73 20 74 6f 20 68 61 73 20 6a 75  points to has ju
18309 73 74 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64  st been modified
1830a 20 69 6e 0a 2a 2a 20 73 6f 6d 65 20 77 61 79 2e   in.** some way.
1830b 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66   This function f
1830c 69 67 75 72 65 73 20 6f 75 74 20 69 66 20 74 68  igures out if th
1830d 69 73 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 20  is modification 
1830e 6d 65 61 6e 73 20 74 68 65 0a 2a 2a 20 74 72 65  means the.** tre
1830f 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 62 61  e needs to be ba
18310 6c 61 6e 63 65 64 2c 20 61 6e 64 20 69 66 20 73  lanced, and if s
18311 6f 20 63 61 6c 6c 73 20 74 68 65 20 61 70 70 72  o calls the appr
18312 6f 70 72 69 61 74 65 20 62 61 6c 61 6e 63 69 6e  opriate balancin
18313 67 20 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e 20 42  g .** routine. B
18314 61 6c 61 6e 63 69 6e 67 20 72 6f 75 74 69 6e 65  alancing routine
18315 73 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 62  s are:.**.**   b
18316 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29 0a 2a  alance_quick().*
18317 2a 20 20 20 62 61 6c 61 6e 63 65 5f 64 65 65 70  *   balance_deep
18318 65 72 28 29 0a 2a 2a 20 20 20 62 61 6c 61 6e 63  er().**   balanc
18319 65 5f 6e 6f 6e 72 6f 6f 74 28 29 0a 2a 2f 0a 73  e_nonroot().*/.s
1831a 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63  tatic int balanc
1831b 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  e(BtCursor *pCur
1831c 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
1831d 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63 6f 6e 73 74  LITE_OK;.  const
1831e 20 69 6e 74 20 6e 4d 69 6e 20 3d 20 70 43 75 72   int nMin = pCur
1831f 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  ->pBt->usableSiz
18320 65 20 2a 20 32 20 2f 20 33 3b 0a 20 20 75 38 20  e * 2 / 3;.  u8 
18321 61 42 61 6c 61 6e 63 65 51 75 69 63 6b 53 70 61  aBalanceQuickSpa
18322 63 65 5b 31 33 5d 3b 0a 20 20 75 38 20 2a 70 46  ce[13];.  u8 *pF
18323 72 65 65 20 3d 20 30 3b 0a 0a 20 20 54 45 53 54  ree = 0;..  TEST
18324 4f 4e 4c 59 28 20 69 6e 74 20 62 61 6c 61 6e 63  ONLY( int balanc
18325 65 5f 71 75 69 63 6b 5f 63 61 6c 6c 65 64 20 3d  e_quick_called =
18326 20 30 20 29 3b 0a 20 20 54 45 53 54 4f 4e 4c 59   0 );.  TESTONLY
18327 28 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f 64 65  ( int balance_de
18328 65 70 65 72 5f 63 61 6c 6c 65 64 20 3d 20 30 20  eper_called = 0 
18329 29 3b 0a 0a 20 20 64 6f 20 7b 0a 20 20 20 20 69  );..  do {.    i
1832a 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75 72 2d  nt iPage = pCur-
1832b 3e 69 50 61 67 65 3b 0a 20 20 20 20 4d 65 6d 50  >iPage;.    MemP
1832c 61 67 65 20 2a 70 50 61 67 65 20 3d 20 70 43 75  age *pPage = pCu
1832d 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 67 65 5d  r->apPage[iPage]
1832e 3b 0a 0a 20 20 20 20 69 66 28 20 69 50 61 67 65  ;..    if( iPage
1832f 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ==0 ){.      if(
18330 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f   pPage->nOverflo
18331 77 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  w ){.        /* 
18332 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66  The root page of
18333 20 74 68 65 20 62 2d 74 72 65 65 20 69 73 20 6f   the b-tree is o
18334 76 65 72 66 75 6c 6c 2e 20 49 6e 20 74 68 69 73  verfull. In this
18335 20 63 61 73 65 20 63 61 6c 6c 20 74 68 65 0a 20   case call the. 
18336 20 20 20 20 20 20 20 2a 2a 20 62 61 6c 61 6e 63         ** balanc
18337 65 5f 64 65 65 70 65 72 28 29 20 66 75 6e 63 74  e_deeper() funct
18338 69 6f 6e 20 74 6f 20 63 72 65 61 74 65 20 61 20  ion to create a 
18339 6e 65 77 20 63 68 69 6c 64 20 66 6f 72 20 74 68  new child for th
1833a 65 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20  e root-page.    
1833b 20 20 20 20 2a 2a 20 61 6e 64 20 63 6f 70 79 20      ** and copy 
1833c 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f 6e 74  the current cont
1833d 65 6e 74 73 20 6f 66 20 74 68 65 20 72 6f 6f 74  ents of the root
1833e 2d 70 61 67 65 20 74 6f 20 69 74 2e 20 54 68 65  -page to it. The
1833f 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 65 78 74  .        ** next
18340 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68   iteration of th
18341 65 20 64 6f 2d 6c 6f 6f 70 20 77 69 6c 6c 20 62  e do-loop will b
18342 61 6c 61 6e 63 65 20 74 68 65 20 63 68 69 6c 64  alance the child
18343 20 70 61 67 65 2e 0a 20 20 20 20 20 20 20 20 2a   page..        *
18344 2f 20 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  / .        asser
18345 74 28 20 28 62 61 6c 61 6e 63 65 5f 64 65 65 70  t( (balance_deep
18346 65 72 5f 63 61 6c 6c 65 64 2b 2b 29 3d 3d 30 20  er_called++)==0 
18347 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  );.        rc = 
18348 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 70  balance_deeper(p
18349 50 61 67 65 2c 20 26 70 43 75 72 2d 3e 61 70 50  Page, &pCur->apP
1834a 61 67 65 5b 31 5d 29 3b 0a 20 20 20 20 20 20 20  age[1]);.       
1834b 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1834c 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
1834d 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 31 3b  pCur->iPage = 1;
1834e 0a 20 20 20 20 20 20 20 20 20 20 70 43 75 72 2d  .          pCur-
1834f 3e 61 69 49 64 78 5b 30 5d 20 3d 20 30 3b 0a 20  >aiIdx[0] = 0;. 
18350 20 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 61           pCur->a
18351 69 49 64 78 5b 31 5d 20 3d 20 30 3b 0a 20 20 20  iIdx[1] = 0;.   
18352 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
18353 43 75 72 2d 3e 61 70 50 61 67 65 5b 31 5d 2d 3e  Cur->apPage[1]->
18354 6e 4f 76 65 72 66 6c 6f 77 20 29 3b 0a 20 20 20  nOverflow );.   
18355 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
18356 73 65 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61  se{.        brea
18357 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
18358 65 6c 73 65 20 69 66 28 20 70 50 61 67 65 2d 3e  else if( pPage->
18359 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 26 26 20  nOverflow==0 && 
1835a 70 50 61 67 65 2d 3e 6e 46 72 65 65 3c 3d 6e 4d  pPage->nFree<=nM
1835b 69 6e 20 29 7b 0a 20 20 20 20 20 20 62 72 65 61  in ){.      brea
1835c 6b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  k;.    }else{.  
1835d 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 20 63 6f      MemPage * co
1835e 6e 73 74 20 70 50 61 72 65 6e 74 20 3d 20 70 43  nst pParent = pC
1835f 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 67 65  ur->apPage[iPage
18360 2d 31 5d 3b 0a 20 20 20 20 20 20 69 6e 74 20 63  -1];.      int c
18361 6f 6e 73 74 20 69 49 64 78 20 3d 20 70 43 75 72  onst iIdx = pCur
18362 2d 3e 61 69 49 64 78 5b 69 50 61 67 65 2d 31 5d  ->aiIdx[iPage-1]
18363 3b 0a 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ;..      rc = sq
18364 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
18365 70 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65  pParent->pDbPage
18366 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
18367 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 23 69  =SQLITE_OK ){.#i
18368 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
18369 54 5f 51 55 49 43 4b 42 41 4c 41 4e 43 45 0a 20  T_QUICKBALANCE. 
1836a 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65         if( pPage
1836b 2d 3e 68 61 73 44 61 74 61 0a 20 20 20 20 20 20  ->hasData.      
1836c 20 20 20 26 26 20 70 50 61 67 65 2d 3e 6e 4f 76     && pPage->nOv
1836d 65 72 66 6c 6f 77 3d 3d 31 0a 20 20 20 20 20 20  erflow==1.      
1836e 20 20 20 26 26 20 70 50 61 67 65 2d 3e 61 4f 76     && pPage->aOv
1836f 66 6c 5b 30 5d 2e 69 64 78 3d 3d 70 50 61 67 65  fl[0].idx==pPage
18370 2d 3e 6e 43 65 6c 6c 0a 20 20 20 20 20 20 20 20  ->nCell.        
18371 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 70 67 6e   && pParent->pgn
18372 6f 21 3d 31 0a 20 20 20 20 20 20 20 20 20 26 26  o!=1.         &&
18373 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3d   pParent->nCell=
18374 3d 69 49 64 78 0a 20 20 20 20 20 20 20 20 29 7b  =iIdx.        ){
18375 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61  .          /* Ca
18376 6c 6c 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b  ll balance_quick
18377 28 29 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e  () to create a n
18378 65 77 20 73 69 62 6c 69 6e 67 20 6f 66 20 70 50  ew sibling of pP
18379 61 67 65 20 6f 6e 20 77 68 69 63 68 0a 20 20 20  age on which.   
1837a 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 73 74 6f         ** to sto
1837b 72 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  re the overflow 
1837c 63 65 6c 6c 2e 20 62 61 6c 61 6e 63 65 5f 71 75  cell. balance_qu
1837d 69 63 6b 28 29 20 69 6e 73 65 72 74 73 20 61 20  ick() inserts a 
1837e 6e 65 77 20 63 65 6c 6c 0a 20 20 20 20 20 20 20  new cell.       
1837f 20 20 20 2a 2a 20 69 6e 74 6f 20 70 50 61 72 65     ** into pPare
18380 6e 74 2c 20 77 68 69 63 68 20 6d 61 79 20 63 61  nt, which may ca
18381 75 73 65 20 70 50 61 72 65 6e 74 20 6f 76 65 72  use pParent over
18382 66 6c 6f 77 2e 20 49 66 20 74 68 69 73 0a 20 20  flow. If this.  
18383 20 20 20 20 20 20 20 20 2a 2a 20 68 61 70 70 65          ** happe
18384 6e 73 2c 20 74 68 65 20 6e 65 78 74 20 69 6e 74  ns, the next int
18385 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64  eration of the d
18386 6f 2d 6c 6f 6f 70 20 77 69 6c 6c 20 62 61 6c 61  o-loop will bala
18387 6e 63 65 20 70 50 61 72 65 6e 74 20 0a 20 20 20  nce pParent .   
18388 20 20 20 20 20 20 20 2a 2a 20 75 73 65 20 65 69         ** use ei
18389 74 68 65 72 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e  ther balance_non
1838a 72 6f 6f 74 28 29 20 6f 72 20 62 61 6c 61 6e 63  root() or balanc
1838b 65 5f 64 65 65 70 65 72 28 29 2e 20 55 6e 74 69  e_deeper(). Unti
1838c 6c 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 20  l this.         
1838d 20 2a 2a 20 68 61 70 70 65 6e 73 2c 20 74 68 65   ** happens, the
1838e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 69   overflow cell i
1838f 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  s stored in the 
18390 61 42 61 6c 61 6e 63 65 51 75 69 63 6b 53 70 61  aBalanceQuickSpa
18391 63 65 5b 5d 0a 20 20 20 20 20 20 20 20 20 20 2a  ce[].          *
18392 2a 20 62 75 66 66 65 72 2e 20 0a 20 20 20 20 20  * buffer. .     
18393 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20       **.        
18394 20 20 2a 2a 20 54 68 65 20 70 75 72 70 6f 73 65    ** The purpose
18395 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
18396 67 20 61 73 73 65 72 74 28 29 20 69 73 20 74 6f  g assert() is to
18397 20 63 68 65 63 6b 20 74 68 61 74 20 6f 6e 6c 79   check that only
18398 20 61 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20   a.          ** 
18399 73 69 6e 67 6c 65 20 63 61 6c 6c 20 74 6f 20 62  single call to b
1839a 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29 20 69  alance_quick() i
1839b 73 20 6d 61 64 65 20 66 6f 72 20 65 61 63 68 20  s made for each 
1839c 63 61 6c 6c 20 74 6f 20 74 68 69 73 0a 20 20 20  call to this.   
1839d 20 20 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 69         ** functi
1839e 6f 6e 2e 20 49 66 20 74 68 69 73 20 77 65 72 65  on. If this were
1839f 20 6e 6f 74 20 76 65 72 69 66 69 65 64 2c 20 61   not verified, a
183a0 20 73 75 62 74 6c 65 20 62 75 67 20 69 6e 76 6f   subtle bug invo
183a1 6c 76 69 6e 67 20 72 65 75 73 65 0a 20 20 20 20  lving reuse.    
183a2 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20        ** of the 
183a3 61 42 61 6c 61 6e 63 65 51 75 69 63 6b 53 70 61  aBalanceQuickSpa
183a4 63 65 5b 5d 20 6d 69 67 68 74 20 73 6e 65 61 6b  ce[] might sneak
183a5 20 69 6e 2e 0a 20 20 20 20 20 20 20 20 20 20 2a   in..          *
183a6 2f 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  /.          asse
183a7 72 74 28 20 28 62 61 6c 61 6e 63 65 5f 71 75 69  rt( (balance_qui
183a8 63 6b 5f 63 61 6c 6c 65 64 2b 2b 29 3d 3d 30 20  ck_called++)==0 
183a9 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  );.          rc 
183aa 3d 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28  = balance_quick(
183ab 70 50 61 72 65 6e 74 2c 20 70 50 61 67 65 2c 20  pParent, pPage, 
183ac 61 42 61 6c 61 6e 63 65 51 75 69 63 6b 53 70 61  aBalanceQuickSpa
183ad 63 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  ce);.        }el
183ae 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20  se.#endif.      
183af 20 20 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a    {.          /*
183b0 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 63   In this case, c
183b1 61 6c 6c 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72  all balance_nonr
183b2 6f 6f 74 28 29 20 74 6f 20 72 65 64 69 73 74 72  oot() to redistr
183b3 69 62 75 74 65 20 63 65 6c 6c 73 0a 20 20 20 20  ibute cells.    
183b4 20 20 20 20 20 20 2a 2a 20 62 65 74 77 65 65 6e        ** between
183b5 20 70 50 61 67 65 20 61 6e 64 20 75 70 20 74 6f   pPage and up to
183b6 20 32 20 6f 66 20 69 74 73 20 73 69 62 6c 69 6e   2 of its siblin
183b7 67 20 70 61 67 65 73 2e 20 54 68 69 73 20 69 6e  g pages. This in
183b8 76 6f 6c 76 65 73 0a 20 20 20 20 20 20 20 20 20  volves.         
183b9 20 2a 2a 20 6d 6f 64 69 66 79 69 6e 67 20 74 68   ** modifying th
183ba 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 50  e contents of pP
183bb 61 72 65 6e 74 2c 20 77 68 69 63 68 20 6d 61 79  arent, which may
183bc 20 63 61 75 73 65 20 70 50 61 72 65 6e 74 20 74   cause pParent t
183bd 6f 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62  o.          ** b
183be 65 63 6f 6d 65 20 6f 76 65 72 66 75 6c 6c 20 6f  ecome overfull o
183bf 72 20 75 6e 64 65 72 66 75 6c 6c 2e 20 54 68 65  r underfull. The
183c0 20 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20   next iteration 
183c1 6f 66 20 74 68 65 20 64 6f 2d 6c 6f 6f 70 0a 20  of the do-loop. 
183c2 20 20 20 20 20 20 20 20 20 2a 2a 20 77 69 6c 6c           ** will
183c3 20 62 61 6c 61 6e 63 65 20 74 68 65 20 70 61 72   balance the par
183c4 65 6e 74 20 70 61 67 65 20 74 6f 20 63 6f 72 72  ent page to corr
183c5 65 63 74 20 74 68 69 73 2e 0a 20 20 20 20 20 20  ect this..      
183c6 20 20 20 20 2a 2a 20 0a 20 20 20 20 20 20 20 20      ** .        
183c7 20 20 2a 2a 20 49 66 20 74 68 65 20 70 61 72 65    ** If the pare
183c8 6e 74 20 70 61 67 65 20 62 65 63 6f 6d 65 73 20  nt page becomes 
183c9 6f 76 65 72 66 75 6c 6c 2c 20 74 68 65 20 6f 76  overfull, the ov
183ca 65 72 66 6c 6f 77 20 63 65 6c 6c 20 6f 72 20 63  erflow cell or c
183cb 65 6c 6c 73 0a 20 20 20 20 20 20 20 20 20 20 2a  ells.          *
183cc 2a 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20  * are stored in 
183cd 74 68 65 20 70 53 70 61 63 65 20 62 75 66 66 65  the pSpace buffe
183ce 72 20 61 6c 6c 6f 63 61 74 65 64 20 69 6d 6d 65  r allocated imme
183cf 64 69 61 74 65 6c 79 20 62 65 6c 6f 77 2e 20 0a  diately below. .
183d0 20 20 20 20 20 20 20 20 20 20 2a 2a 20 41 20 73            ** A s
183d1 75 62 73 65 71 75 65 6e 74 20 69 74 65 72 61 74  ubsequent iterat
183d2 69 6f 6e 20 6f 66 20 74 68 65 20 64 6f 2d 6c 6f  ion of the do-lo
183d3 6f 70 20 77 69 6c 6c 20 64 65 61 6c 20 77 69 74  op will deal wit
183d4 68 20 74 68 69 73 20 62 79 0a 20 20 20 20 20 20  h this by.      
183d5 20 20 20 20 2a 2a 20 63 61 6c 6c 69 6e 67 20 62      ** calling b
183d6 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29  alance_nonroot()
183d7 20 28 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72   (balance_deeper
183d8 28 29 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64  () may be called
183d9 20 66 69 72 73 74 2c 0a 20 20 20 20 20 20 20 20   first,.        
183da 20 20 2a 2a 20 62 75 74 20 69 74 20 64 6f 65 73    ** but it does
183db 6e 27 74 20 64 65 61 6c 20 77 69 74 68 20 6f 76  n't deal with ov
183dc 65 72 66 6c 6f 77 20 63 65 6c 6c 73 20 2d 20 6a  erflow cells - j
183dd 75 73 74 20 6d 6f 76 65 73 20 74 68 65 6d 20 74  ust moves them t
183de 6f 20 61 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  o a.          **
183df 20 64 69 66 66 65 72 65 6e 74 20 70 61 67 65 29   different page)
183e0 2e 20 4f 6e 63 65 20 74 68 69 73 20 73 75 62 73  . Once this subs
183e1 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f 20 62  equent call to b
183e2 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29  alance_nonroot()
183e3 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 68   .          ** h
183e4 61 73 20 63 6f 6d 70 6c 65 74 65 64 2c 20 69 74  as completed, it
183e5 20 69 73 20 73 61 66 65 20 74 6f 20 72 65 6c 65   is safe to rele
183e6 61 73 65 20 74 68 65 20 70 53 70 61 63 65 20 62  ase the pSpace b
183e7 75 66 66 65 72 20 75 73 65 64 20 62 79 0a 20 20  uffer used by.  
183e8 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 70          ** the p
183e9 72 65 76 69 6f 75 73 20 63 61 6c 6c 2c 20 61 73  revious call, as
183ea 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 65   the overflow ce
183eb 6c 6c 20 64 61 74 61 20 77 69 6c 6c 20 68 61 76  ll data will hav
183ec 65 20 62 65 65 6e 20 0a 20 20 20 20 20 20 20 20  e been .        
183ed 20 20 2a 2a 20 63 6f 70 69 65 64 20 65 69 74 68    ** copied eith
183ee 65 72 20 69 6e 74 6f 20 74 68 65 20 62 6f 64 79  er into the body
183ef 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 70   of a database p
183f0 61 67 65 20 6f 72 20 69 6e 74 6f 20 74 68 65 20  age or into the 
183f1 6e 65 77 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  new.          **
183f2 20 70 53 70 61 63 65 20 62 75 66 66 65 72 20 70   pSpace buffer p
183f3 61 73 73 65 64 20 74 6f 20 74 68 65 20 6c 61 74  assed to the lat
183f4 74 65 72 20 63 61 6c 6c 20 74 6f 20 62 61 6c 61  ter call to bala
183f5 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 2e 0a 20  nce_nonroot().. 
183f6 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20           */.    
183f7 20 20 20 20 20 20 75 38 20 2a 70 53 70 61 63 65        u8 *pSpace
183f8 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61   = sqlite3PageMa
183f9 6c 6c 6f 63 28 70 43 75 72 2d 3e 70 42 74 2d 3e  lloc(pCur->pBt->
183fa 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20  pageSize);.     
183fb 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63       rc = balanc
183fc 65 5f 6e 6f 6e 72 6f 6f 74 28 70 50 61 72 65 6e  e_nonroot(pParen
183fd 74 2c 20 69 49 64 78 2c 20 70 53 70 61 63 65 2c  t, iIdx, pSpace,
183fe 20 69 50 61 67 65 3d 3d 31 29 3b 0a 20 20 20 20   iPage==1);.    
183ff 20 20 20 20 20 20 69 66 28 20 70 46 72 65 65 20        if( pFree 
18400 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f  ){.            /
18401 2a 20 49 66 20 70 46 72 65 65 20 69 73 20 6e 6f  * If pFree is no
18402 74 20 4e 55 4c 4c 2c 20 69 74 20 70 6f 69 6e 74  t NULL, it point
18403 73 20 74 6f 20 74 68 65 20 70 53 70 61 63 65 20  s to the pSpace 
18404 62 75 66 66 65 72 20 75 73 65 64 20 0a 20 20 20  buffer used .   
18405 20 20 20 20 20 20 20 20 20 2a 2a 20 62 79 20 61           ** by a
18406 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 20 74   previous call t
18407 6f 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f  o balance_nonroo
18408 74 28 29 2e 20 49 74 73 20 63 6f 6e 74 65 6e 74  t(). Its content
18409 73 20 61 72 65 0a 20 20 20 20 20 20 20 20 20 20  s are.          
1840a 20 20 2a 2a 20 6e 6f 77 20 73 74 6f 72 65 64 20    ** now stored 
1840b 65 69 74 68 65 72 20 6f 6e 20 72 65 61 6c 20 64  either on real d
1840c 61 74 61 62 61 73 65 20 70 61 67 65 73 20 6f 72  atabase pages or
1840d 20 77 69 74 68 69 6e 20 74 68 65 20 0a 20 20 20   within the .   
1840e 20 20 20 20 20 20 20 20 20 2a 2a 20 6e 65 77 20           ** new 
1840f 70 53 70 61 63 65 20 62 75 66 66 65 72 2c 20 73  pSpace buffer, s
18410 6f 20 69 74 20 6d 61 79 20 62 65 20 73 61 66 65  o it may be safe
18411 6c 79 20 66 72 65 65 64 20 68 65 72 65 2e 20 2a  ly freed here. *
18412 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71  /.            sq
18413 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 70 46  lite3PageFree(pF
18414 72 65 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ree);.          
18415 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  }..          /* 
18416 54 68 65 20 70 53 70 61 63 65 20 62 75 66 66 65  The pSpace buffe
18417 72 20 77 69 6c 6c 20 62 65 20 66 72 65 65 64 20  r will be freed 
18418 61 66 74 65 72 20 74 68 65 20 6e 65 78 74 20 63  after the next c
18419 61 6c 6c 20 74 6f 0a 20 20 20 20 20 20 20 20 20  all to.         
1841a 20 2a 2a 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72   ** balance_nonr
1841b 6f 6f 74 28 29 2c 20 6f 72 20 6a 75 73 74 20 62  oot(), or just b
1841c 65 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74  efore this funct
1841d 69 6f 6e 20 72 65 74 75 72 6e 73 2c 20 77 68 69  ion returns, whi
1841e 63 68 65 76 65 72 0a 20 20 20 20 20 20 20 20 20  chever.         
1841f 20 2a 2a 20 63 6f 6d 65 73 20 66 69 72 73 74 2e   ** comes first.
18420 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 70 46   */.          pF
18421 72 65 65 20 3d 20 70 53 70 61 63 65 3b 0a 20 20  ree = pSpace;.  
18422 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
18423 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e 6e 4f  .      pPage->nO
18424 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 0a 20 20  verflow = 0;..  
18425 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20      /* The next 
18426 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65  iteration of the
18427 20 64 6f 2d 6c 6f 6f 70 20 62 61 6c 61 6e 63 65   do-loop balance
18428 73 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67  s the parent pag
18429 65 2e 20 2a 2f 0a 20 20 20 20 20 20 72 65 6c 65  e. */.      rele
1842a 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a  asePage(pPage);.
1842b 20 20 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67        pCur->iPag
1842c 65 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 7d 77 68  e--;.    }.  }wh
1842d 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ile( rc==SQLITE_
1842e 4f 4b 20 29 3b 0a 0a 20 20 69 66 28 20 70 46 72  OK );..  if( pFr
1842f 65 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ee ){.    sqlite
18430 33 50 61 67 65 46 72 65 65 28 70 46 72 65 65 29  3PageFree(pFree)
18431 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
18432 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73  c;.}.../*.** Ins
18433 65 72 74 20 61 20 6e 65 77 20 72 65 63 6f 72 64  ert a new record
18434 20 69 6e 74 6f 20 74 68 65 20 42 54 72 65 65 2e   into the BTree.
18435 20 20 54 68 65 20 6b 65 79 20 69 73 20 67 69 76    The key is giv
18436 65 6e 20 62 79 20 28 70 4b 65 79 2c 6e 4b 65 79  en by (pKey,nKey
18437 29 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74  ).** and the dat
18438 61 20 69 73 20 67 69 76 65 6e 20 62 79 20 28 70  a is given by (p
18439 44 61 74 61 2c 6e 44 61 74 61 29 2e 20 20 54 68  Data,nData).  Th
1843a 65 20 63 75 72 73 6f 72 20 69 73 20 75 73 65 64  e cursor is used
1843b 20 6f 6e 6c 79 20 74 6f 0a 2a 2a 20 64 65 66 69   only to.** defi
1843c 6e 65 20 77 68 61 74 20 74 61 62 6c 65 20 74 68  ne what table th
1843d 65 20 72 65 63 6f 72 64 20 73 68 6f 75 6c 64 20  e record should 
1843e 62 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f  be inserted into
1843f 2e 20 20 54 68 65 20 63 75 72 73 6f 72 0a 2a 2a  .  The cursor.**
18440 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e   is left pointin
18441 67 20 61 74 20 61 20 72 61 6e 64 6f 6d 20 6c 6f  g at a random lo
18442 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f  cation..**.** Fo
18443 72 20 61 6e 20 49 4e 54 4b 45 59 20 74 61 62 6c  r an INTKEY tabl
18444 65 2c 20 6f 6e 6c 79 20 74 68 65 20 6e 4b 65 79  e, only the nKey
18445 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6b 65   value of the ke
18446 79 20 69 73 20 75 73 65 64 2e 20 20 70 4b 65 79  y is used.  pKey
18447 20 69 73 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e 20   is.** ignored. 
18448 20 46 6f 72 20 61 20 5a 45 52 4f 44 41 54 41 20   For a ZERODATA 
18449 74 61 62 6c 65 2c 20 74 68 65 20 70 44 61 74 61  table, the pData
1844a 20 61 6e 64 20 6e 44 61 74 61 20 61 72 65 20 62   and nData are b
1844b 6f 74 68 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a  oth ignored..**.
1844c 2a 2a 20 49 66 20 74 68 65 20 73 65 65 6b 52 65  ** If the seekRe
1844d 73 75 6c 74 20 70 61 72 61 6d 65 74 65 72 20 69  sult parameter i
1844e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e  s non-zero, then
1844f 20 61 20 73 75 63 63 65 73 73 66 75 6c 20 63 61   a successful ca
18450 6c 6c 20 74 6f 0a 2a 2a 20 4d 6f 76 65 74 6f 55  ll to.** MovetoU
18451 6e 70 61 63 6b 65 64 28 29 20 74 6f 20 73 65 65  npacked() to see
18452 6b 20 63 75 72 73 6f 72 20 70 43 75 72 20 74 6f  k cursor pCur to
18453 20 28 70 4b 65 79 2c 20 6e 4b 65 79 29 20 68 61   (pKey, nKey) ha
18454 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 62 65 65  s already.** bee
18455 6e 20 70 65 72 66 6f 72 6d 65 64 2e 20 73 65 65  n performed. see
18456 6b 52 65 73 75 6c 74 20 69 73 20 74 68 65 20 73  kResult is the s
18457 65 61 72 63 68 20 72 65 73 75 6c 74 20 72 65 74  earch result ret
18458 75 72 6e 65 64 20 28 61 20 6e 65 67 61 74 69 76  urned (a negativ
18459 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 69 66 20 70  e.** number if p
1845a 43 75 72 20 70 6f 69 6e 74 73 20 61 74 20 61 6e  Cur points at an
1845b 20 65 6e 74 72 79 20 74 68 61 74 20 69 73 20 73   entry that is s
1845c 6d 61 6c 6c 65 72 20 74 68 61 6e 20 28 70 4b 65  maller than (pKe
1845d 79 2c 20 6e 4b 65 79 29 2c 20 6f 72 0a 2a 2a 20  y, nKey), or.** 
1845e 61 20 70 6f 73 69 74 69 76 65 20 76 61 6c 75 65  a positive value
1845f 20 69 66 20 70 43 75 72 20 70 6f 69 6e 74 73 20   if pCur points 
18460 61 74 20 61 6e 20 65 74 72 79 20 74 68 61 74 20  at an etry that 
18461 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 0a  is larger than .
18462 2a 2a 20 28 70 4b 65 79 2c 20 6e 4b 65 79 29 29  ** (pKey, nKey))
18463 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  . .**.** If the 
18464 73 65 65 6b 52 65 73 75 6c 74 20 70 61 72 61 6d  seekResult param
18465 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  eter is non-zero
18466 2c 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c 65  , then the calle
18467 72 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 61  r guarantees tha
18468 74 0a 2a 2a 20 63 75 72 73 6f 72 20 70 43 75 72  t.** cursor pCur
18469 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 61 74 20   is pointing at 
1846a 74 68 65 20 65 78 69 73 74 69 6e 67 20 63 6f 70  the existing cop
1846b 79 20 6f 66 20 61 20 72 6f 77 20 74 68 61 74 20  y of a row that 
1846c 69 73 20 74 6f 20 62 65 0a 2a 2a 20 6f 76 65 72  is to be.** over
1846d 77 72 69 74 74 65 6e 2e 20 20 49 66 20 74 68 65  written.  If the
1846e 20 73 65 65 6b 52 65 73 75 6c 74 20 70 61 72 61   seekResult para
1846f 6d 65 74 65 72 20 69 73 20 30 2c 20 74 68 65 6e  meter is 0, then
18470 20 63 75 72 73 6f 72 20 70 43 75 72 20 6d 61 79   cursor pCur may
18471 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 61 6e 79  .** point to any
18472 20 65 6e 74 72 79 20 6f 72 20 74 6f 20 6e 6f 20   entry or to no 
18473 65 6e 74 72 79 20 61 74 20 61 6c 6c 20 61 6e 64  entry at all and
18474 20 73 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f   so this functio
18475 6e 20 68 61 73 20 74 6f 20 73 65 65 6b 0a 2a 2a  n has to seek.**
18476 20 74 68 65 20 63 75 72 73 6f 72 20 62 65 66 6f   the cursor befo
18477 72 65 20 74 68 65 20 6e 65 77 20 6b 65 79 20 63  re the new key c
18478 61 6e 20 62 65 20 69 6e 73 65 72 74 65 64 2e 0a  an be inserted..
18479 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1847a 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
1847b 65 65 49 6e 73 65 72 74 28 0a 20 20 42 74 43 75  eeInsert(.  BtCu
1847c 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20  rsor *pCur,     
1847d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
1847e 73 65 72 74 20 64 61 74 61 20 69 6e 74 6f 20 74  sert data into t
1847f 68 65 20 74 61 62 6c 65 20 6f 66 20 74 68 69 73  he table of this
18480 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 63 6f 6e   cursor */.  con
18481 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69  st void *pKey, i
18482 36 34 20 6e 4b 65 79 2c 20 20 20 20 2f 2a 20 54  64 nKey,    /* T
18483 68 65 20 6b 65 79 20 6f 66 20 74 68 65 20 6e 65  he key of the ne
18484 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 6f  w record */.  co
18485 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 74 61 2c  nst void *pData,
18486 20 69 6e 74 20 6e 44 61 74 61 2c 20 20 2f 2a 20   int nData,  /* 
18487 54 68 65 20 64 61 74 61 20 6f 66 20 74 68 65 20  The data of the 
18488 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  new record */.  
18489 69 6e 74 20 6e 5a 65 72 6f 2c 20 20 20 20 20 20  int nZero,      
1848a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1848b 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 78 74 72  * Number of extr
1848c 61 20 30 20 62 79 74 65 73 20 74 6f 20 61 70 70  a 0 bytes to app
1848d 65 6e 64 20 74 6f 20 64 61 74 61 20 2a 2f 0a 20  end to data */. 
1848e 20 69 6e 74 20 61 70 70 65 6e 64 42 69 61 73 2c   int appendBias,
1848f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18490 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20  /* True if this 
18491 69 73 20 6c 69 6b 65 6c 79 20 61 6e 20 61 70 70  is likely an app
18492 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 73 65 65  end */.  int see
18493 6b 52 65 73 75 6c 74 20 20 20 20 20 20 20 20 20  kResult         
18494 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c          /* Resul
18495 74 20 6f 66 20 70 72 69 6f 72 20 4d 6f 76 65 74  t of prior Movet
18496 6f 55 6e 70 61 63 6b 65 64 28 29 20 63 61 6c 6c  oUnpacked() call
18497 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b   */.){.  int rc;
18498 0a 20 20 69 6e 74 20 6c 6f 63 20 3d 20 73 65 65  .  int loc = see
18499 6b 52 65 73 75 6c 74 3b 20 20 20 20 20 20 20 20  kResult;        
1849a 20 20 2f 2a 20 2d 31 3a 20 62 65 66 6f 72 65 20    /* -1: before 
1849b 64 65 73 69 72 65 64 20 6c 6f 63 61 74 69 6f 6e  desired location
1849c 20 20 2b 31 3a 20 61 66 74 65 72 20 2a 2f 0a 20    +1: after */. 
1849d 20 69 6e 74 20 73 7a 4e 65 77 3b 0a 20 20 69 6e   int szNew;.  in
1849e 74 20 69 64 78 3b 0a 20 20 4d 65 6d 50 61 67 65  t idx;.  MemPage
1849f 20 2a 70 50 61 67 65 3b 0a 20 20 42 74 72 65 65   *pPage;.  Btree
184a0 20 2a 70 20 3d 20 70 43 75 72 2d 3e 70 42 74 72   *p = pCur->pBtr
184a1 65 65 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  ee;.  BtShared *
184a2 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
184a3 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 6f  unsigned char *o
184a4 6c 64 43 65 6c 6c 3b 0a 20 20 75 6e 73 69 67 6e  ldCell;.  unsign
184a5 65 64 20 63 68 61 72 20 2a 6e 65 77 43 65 6c 6c  ed char *newCell
184a6 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 70 43 75   = 0;..  if( pCu
184a7 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  r->eState==CURSO
184a8 52 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 61  R_FAULT ){.    a
184a9 73 73 65 72 74 28 20 70 43 75 72 2d 3e 73 6b 69  ssert( pCur->ski
184aa 70 4e 65 78 74 21 3d 53 51 4c 49 54 45 5f 4f 4b  pNext!=SQLITE_OK
184ab 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70   );.    return p
184ac 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b 0a 20  Cur->skipNext;. 
184ad 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75   }..  assert( cu
184ae 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
184af 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74  Cur) );.  assert
184b0 28 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 26  ( pCur->wrFlag &
184b1 26 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63  & pBt->inTransac
184b2 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54  tion==TRANS_WRIT
184b3 45 20 26 26 20 21 70 42 74 2d 3e 72 65 61 64 4f  E && !pBt->readO
184b4 6e 6c 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28  nly );.  assert(
184b5 20 68 61 73 53 68 61 72 65 64 43 61 63 68 65 54   hasSharedCacheT
184b6 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 70 43 75 72  ableLock(p, pCur
184b7 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72  ->pgnoRoot, pCur
184b8 2d 3e 70 4b 65 79 49 6e 66 6f 21 3d 30 2c 20 32  ->pKeyInfo!=0, 2
184b9 29 20 29 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 72  ) );..  /* Asser
184ba 74 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65  t that the calle
184bb 72 20 68 61 73 20 62 65 65 6e 20 63 6f 6e 73 69  r has been consi
184bc 73 74 65 6e 74 2e 20 49 66 20 74 68 69 73 20 63  stent. If this c
184bd 75 72 73 6f 72 20 77 61 73 20 6f 70 65 6e 65 64  ursor was opened
184be 0a 20 20 2a 2a 20 65 78 70 65 63 74 69 6e 67 20  .  ** expecting 
184bf 61 6e 20 69 6e 64 65 78 20 62 2d 74 72 65 65 2c  an index b-tree,
184c0 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c 65 72   then the caller
184c1 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 73 65 72   should be inser
184c2 74 69 6e 67 20 62 6c 6f 62 0a 20 20 2a 2a 20 6b  ting blob.  ** k
184c3 65 79 73 20 77 69 74 68 20 6e 6f 20 61 73 73 6f  eys with no asso
184c4 63 69 61 74 65 64 20 64 61 74 61 2e 20 49 66 20  ciated data. If 
184c5 74 68 65 20 63 75 72 73 6f 72 20 77 61 73 20 6f  the cursor was o
184c6 70 65 6e 65 64 20 65 78 70 65 63 74 69 6e 67 20  pened expecting 
184c7 61 6e 0a 20 20 2a 2a 20 69 6e 74 6b 65 79 20 74  an.  ** intkey t
184c8 61 62 6c 65 2c 20 74 68 65 20 63 61 6c 6c 65 72  able, the caller
184c9 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 73 65 72   should be inser
184ca 74 69 6e 67 20 69 6e 74 65 67 65 72 20 6b 65 79  ting integer key
184cb 73 20 77 69 74 68 20 61 0a 20 20 2a 2a 20 62 6c  s with a.  ** bl
184cc 6f 62 20 6f 66 20 61 73 73 6f 63 69 61 74 65 64  ob of associated
184cd 20 64 61 74 61 2e 20 20 2a 2f 0a 20 20 61 73 73   data.  */.  ass
184ce 65 72 74 28 20 28 70 4b 65 79 3d 3d 30 29 3d 3d  ert( (pKey==0)==
184cf 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d  (pCur->pKeyInfo=
184d0 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  =0) );..  /* If 
184d1 74 68 69 73 20 69 73 20 61 6e 20 69 6e 73 65 72  this is an inser
184d2 74 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 20 62  t into a table b
184d3 2d 74 72 65 65 2c 20 69 6e 76 61 6c 69 64 61 74  -tree, invalidat
184d4 65 20 61 6e 79 20 69 6e 63 72 62 6c 6f 62 20 0a  e any incrblob .
184d5 20 20 2a 2a 20 63 75 72 73 6f 72 73 20 6f 70 65    ** cursors ope
184d6 6e 20 6f 6e 20 74 68 65 20 72 6f 77 20 62 65 69  n on the row bei
184d7 6e 67 20 72 65 70 6c 61 63 65 64 20 28 61 73 73  ng replaced (ass
184d8 75 6d 69 6e 67 20 74 68 69 73 20 69 73 20 61 20  uming this is a 
184d9 72 65 70 6c 61 63 65 0a 20 20 2a 2a 20 6f 70 65  replace.  ** ope
184da 72 61 74 69 6f 6e 20 2d 20 69 66 20 69 74 20 69  ration - if it i
184db 73 20 6e 6f 74 2c 20 74 68 65 20 66 6f 6c 6c 6f  s not, the follo
184dc 77 69 6e 67 20 69 73 20 61 20 6e 6f 2d 6f 70 29  wing is a no-op)
184dd 2e 20 20 2a 2f 0a 20 20 69 66 28 20 70 43 75 72  .  */.  if( pCur
184de 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 20 29 7b  ->pKeyInfo==0 ){
184df 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 49  .    invalidateI
184e0 6e 63 72 62 6c 6f 62 43 75 72 73 6f 72 73 28 70  ncrblobCursors(p
184e1 2c 20 6e 4b 65 79 2c 20 30 29 3b 0a 20 20 7d 0a  , nKey, 0);.  }.
184e2 0a 20 20 2f 2a 20 53 61 76 65 20 74 68 65 20 70  .  /* Save the p
184e3 6f 73 69 74 69 6f 6e 73 20 6f 66 20 61 6e 79 20  ositions of any 
184e4 6f 74 68 65 72 20 63 75 72 73 6f 72 73 20 6f 70  other cursors op
184e5 65 6e 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65  en on this table
184e6 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 73  ..  **.  ** In s
184e7 6f 6d 65 20 63 61 73 65 73 2c 20 74 68 65 20 63  ome cases, the c
184e8 61 6c 6c 20 74 6f 20 62 74 72 65 65 4d 6f 76 65  all to btreeMove
184e9 74 6f 28 29 20 62 65 6c 6f 77 20 69 73 20 61 20  to() below is a 
184ea 6e 6f 2d 6f 70 2e 20 46 6f 72 0a 20 20 2a 2a 20  no-op. For.  ** 
184eb 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 20 69 6e  example, when in
184ec 73 65 72 74 69 6e 67 20 64 61 74 61 20 69 6e 74  serting data int
184ed 6f 20 61 20 74 61 62 6c 65 20 77 69 74 68 20 61  o a table with a
184ee 75 74 6f 2d 67 65 6e 65 72 61 74 65 64 20 69 6e  uto-generated in
184ef 74 65 67 65 72 0a 20 20 2a 2a 20 6b 65 79 73 2c  teger.  ** keys,
184f0 20 74 68 65 20 56 44 42 45 20 6c 61 79 65 72 20   the VDBE layer 
184f1 69 6e 76 6f 6b 65 73 20 73 71 6c 69 74 65 33 42  invokes sqlite3B
184f2 74 72 65 65 4c 61 73 74 28 29 20 74 6f 20 66 69  treeLast() to fi
184f3 67 75 72 65 20 6f 75 74 20 74 68 65 20 0a 20 20  gure out the .  
184f4 2a 2a 20 69 6e 74 65 67 65 72 20 6b 65 79 20 74  ** integer key t
184f5 6f 20 75 73 65 2e 20 49 74 20 74 68 65 6e 20 63  o use. It then c
184f6 61 6c 6c 73 20 74 68 69 73 20 66 75 6e 63 74 69  alls this functi
184f7 6f 6e 20 74 6f 20 61 63 74 75 61 6c 6c 79 20 69  on to actually i
184f8 6e 73 65 72 74 20 74 68 65 20 0a 20 20 2a 2a 20  nsert the .  ** 
184f9 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20 69 6e  data into the in
184fa 74 6b 65 79 20 42 2d 54 72 65 65 2e 20 49 6e 20  tkey B-Tree. In 
184fb 74 68 69 73 20 63 61 73 65 20 62 74 72 65 65 4d  this case btreeM
184fc 6f 76 65 74 6f 28 29 20 72 65 63 6f 67 6e 69 7a  oveto() recogniz
184fd 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65  es.  ** that the
184fe 20 63 75 72 73 6f 72 20 69 73 20 61 6c 72 65 61   cursor is alrea
184ff 64 79 20 77 68 65 72 65 20 69 74 20 6e 65 65 64  dy where it need
18500 73 20 74 6f 20 62 65 20 61 6e 64 20 72 65 74 75  s to be and retu
18501 72 6e 73 20 77 69 74 68 6f 75 74 0a 20 20 2a 2a  rns without.  **
18502 20 64 6f 69 6e 67 20 61 6e 79 20 77 6f 72 6b 2e   doing any work.
18503 20 54 6f 20 61 76 6f 69 64 20 74 68 77 61 72 74   To avoid thwart
18504 69 6e 67 20 74 68 65 73 65 20 6f 70 74 69 6d 69  ing these optimi
18505 7a 61 74 69 6f 6e 73 2c 20 69 74 20 69 73 20 69  zations, it is i
18506 6d 70 6f 72 74 61 6e 74 0a 20 20 2a 2a 20 6e 6f  mportant.  ** no
18507 74 20 74 6f 20 63 6c 65 61 72 20 74 68 65 20 63  t to clear the c
18508 75 72 73 6f 72 20 68 65 72 65 2e 0a 20 20 2a 2f  ursor here..  */
18509 0a 20 20 72 63 20 3d 20 73 61 76 65 41 6c 6c 43  .  rc = saveAllC
1850a 75 72 73 6f 72 73 28 70 42 74 2c 20 70 43 75 72  ursors(pBt, pCur
1850b 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72  ->pgnoRoot, pCur
1850c 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65  );.  if( rc ) re
1850d 74 75 72 6e 20 72 63 3b 0a 20 20 69 66 28 20 21  turn rc;.  if( !
1850e 6c 6f 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  loc ){.    rc = 
1850f 62 74 72 65 65 4d 6f 76 65 74 6f 28 70 43 75 72  btreeMoveto(pCur
18510 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 61 70  , pKey, nKey, ap
18511 70 65 6e 64 42 69 61 73 2c 20 26 6c 6f 63 29 3b  pendBias, &loc);
18512 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65  .    if( rc ) re
18513 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61  turn rc;.  }.  a
18514 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74  ssert( pCur->eSt
18515 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49  ate==CURSOR_VALI
18516 44 20 7c 7c 20 28 70 43 75 72 2d 3e 65 53 74 61  D || (pCur->eSta
18517 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c  te==CURSOR_INVAL
18518 49 44 20 26 26 20 6c 6f 63 29 20 29 3b 0a 0a 20  ID && loc) );.. 
18519 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61   pPage = pCur->a
1851a 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
1851b 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50  e];.  assert( pP
1851c 61 67 65 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 6e  age->intKey || n
1851d 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65  Key>=0 );.  asse
1851e 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20  rt( pPage->leaf 
1851f 7c 7c 20 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65  || !pPage->intKe
18520 79 20 29 3b 0a 0a 20 20 54 52 41 43 45 28 28 22  y );..  TRACE(("
18521 49 4e 53 45 52 54 3a 20 74 61 62 6c 65 3d 25 64  INSERT: table=%d
18522 20 6e 6b 65 79 3d 25 6c 6c 64 20 6e 64 61 74 61   nkey=%lld ndata
18523 3d 25 64 20 70 61 67 65 3d 25 64 20 25 73 5c 6e  =%d page=%d %s\n
18524 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70 43 75  ",.          pCu
18525 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 6e 4b 65  r->pgnoRoot, nKe
18526 79 2c 20 6e 44 61 74 61 2c 20 70 50 61 67 65 2d  y, nData, pPage-
18527 3e 70 67 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20  >pgno,.         
18528 20 6c 6f 63 3d 3d 30 20 3f 20 22 6f 76 65 72 77   loc==0 ? "overw
18529 72 69 74 65 22 20 3a 20 22 6e 65 77 20 65 6e 74  rite" : "new ent
1852a 72 79 22 29 29 3b 0a 20 20 61 73 73 65 72 74 28  ry"));.  assert(
1852b 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29   pPage->isInit )
1852c 3b 0a 20 20 61 6c 6c 6f 63 61 74 65 54 65 6d 70  ;.  allocateTemp
1852d 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 6e 65  Space(pBt);.  ne
1852e 77 43 65 6c 6c 20 3d 20 70 42 74 2d 3e 70 54 6d  wCell = pBt->pTm
1852f 70 53 70 61 63 65 3b 0a 20 20 69 66 28 20 6e 65  pSpace;.  if( ne
18530 77 43 65 6c 6c 3d 3d 30 20 29 20 72 65 74 75 72  wCell==0 ) retur
18531 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
18532 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e 43 65 6c    rc = fillInCel
18533 6c 28 70 50 61 67 65 2c 20 6e 65 77 43 65 6c 6c  l(pPage, newCell
18534 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 70 44  , pKey, nKey, pD
18535 61 74 61 2c 20 6e 44 61 74 61 2c 20 6e 5a 65 72  ata, nData, nZer
18536 6f 2c 20 26 73 7a 4e 65 77 29 3b 0a 20 20 69 66  o, &szNew);.  if
18537 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64 5f  ( rc ) goto end_
18538 69 6e 73 65 72 74 3b 0a 20 20 61 73 73 65 72 74  insert;.  assert
18539 28 20 73 7a 4e 65 77 3d 3d 63 65 6c 6c 53 69 7a  ( szNew==cellSiz
1853a 65 50 74 72 28 70 50 61 67 65 2c 20 6e 65 77 43  ePtr(pPage, newC
1853b 65 6c 6c 29 20 29 3b 0a 20 20 61 73 73 65 72 74  ell) );.  assert
1853c 28 20 73 7a 4e 65 77 3c 3d 4d 58 5f 43 45 4c 4c  ( szNew<=MX_CELL
1853d 5f 53 49 5a 45 28 70 42 74 29 20 29 3b 0a 20 20  _SIZE(pBt) );.  
1853e 69 64 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 64  idx = pCur->aiId
1853f 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a  x[pCur->iPage];.
18540 20 20 69 66 28 20 6c 6f 63 3d 3d 30 20 29 7b 0a    if( loc==0 ){.
18541 20 20 20 20 75 31 36 20 73 7a 4f 6c 64 3b 0a 20      u16 szOld;. 
18542 20 20 20 61 73 73 65 72 74 28 20 69 64 78 3c 70     assert( idx<p
18543 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20  Page->nCell );. 
18544 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
18545 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d  agerWrite(pPage-
18546 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69  >pDbPage);.    i
18547 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 67  f( rc ){.      g
18548 6f 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a  oto end_insert;.
18549 20 20 20 20 7d 0a 20 20 20 20 6f 6c 64 43 65 6c      }.    oldCel
1854a 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61  l = findCell(pPa
1854b 67 65 2c 20 69 64 78 29 3b 0a 20 20 20 20 69 66  ge, idx);.    if
1854c 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  ( !pPage->leaf )
1854d 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 6e  {.      memcpy(n
1854e 65 77 43 65 6c 6c 2c 20 6f 6c 64 43 65 6c 6c 2c  ewCell, oldCell,
1854f 20 34 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73   4);.    }.    s
18550 7a 4f 6c 64 20 3d 20 63 65 6c 6c 53 69 7a 65 50  zOld = cellSizeP
18551 74 72 28 70 50 61 67 65 2c 20 6f 6c 64 43 65 6c  tr(pPage, oldCel
18552 6c 29 3b 0a 20 20 20 20 72 63 20 3d 20 63 6c 65  l);.    rc = cle
18553 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 6f 6c  arCell(pPage, ol
18554 64 43 65 6c 6c 29 3b 0a 20 20 20 20 64 72 6f 70  dCell);.    drop
18555 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 2c  Cell(pPage, idx,
18556 20 73 7a 4f 6c 64 2c 20 26 72 63 29 3b 0a 20 20   szOld, &rc);.  
18557 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20    if( rc ) goto 
18558 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 20 7d 65  end_insert;.  }e
18559 6c 73 65 20 69 66 28 20 6c 6f 63 3c 30 20 26 26  lse if( loc<0 &&
1855a 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 30 20   pPage->nCell>0 
1855b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
1855c 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20  Page->leaf );.  
1855d 20 20 69 64 78 20 3d 20 2b 2b 70 43 75 72 2d 3e    idx = ++pCur->
1855e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67  aiIdx[pCur->iPag
1855f 65 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  e];.  }else{.   
18560 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
18561 6c 65 61 66 20 29 3b 0a 20 20 7d 0a 20 20 69 6e  leaf );.  }.  in
18562 73 65 72 74 43 65 6c 6c 28 70 50 61 67 65 2c 20  sertCell(pPage, 
18563 69 64 78 2c 20 6e 65 77 43 65 6c 6c 2c 20 73 7a  idx, newCell, sz
18564 4e 65 77 2c 20 30 2c 20 30 2c 20 26 72 63 29 3b  New, 0, 0, &rc);
18565 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53  .  assert( rc!=S
18566 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67  QLITE_OK || pPag
18567 65 2d 3e 6e 43 65 6c 6c 3e 30 20 7c 7c 20 70 50  e->nCell>0 || pP
18568 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30  age->nOverflow>0
18569 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 20   );..  /* If no 
1856a 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 65  error has occure
1856b 64 20 61 6e 64 20 70 50 61 67 65 20 68 61 73 20  d and pPage has 
1856c 61 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c  an overflow cell
1856d 2c 20 63 61 6c 6c 20 62 61 6c 61 6e 63 65 28 29  , call balance()
1856e 20 0a 20 20 2a 2a 20 74 6f 20 72 65 64 69 73 74   .  ** to redist
1856f 72 69 62 75 74 65 20 74 68 65 20 63 65 6c 6c 73  ribute the cells
18570 20 77 69 74 68 69 6e 20 74 68 65 20 74 72 65 65   within the tree
18571 2e 20 53 69 6e 63 65 20 62 61 6c 61 6e 63 65 28  . Since balance(
18572 29 20 6d 61 79 20 6d 6f 76 65 0a 20 20 2a 2a 20  ) may move.  ** 
18573 74 68 65 20 63 75 72 73 6f 72 2c 20 7a 65 72 6f  the cursor, zero
18574 20 74 68 65 20 42 74 43 75 72 73 6f 72 2e 69 6e   the BtCursor.in
18575 66 6f 2e 6e 53 69 7a 65 20 61 6e 64 20 42 74 43  fo.nSize and BtC
18576 75 72 73 6f 72 2e 76 61 6c 69 64 4e 4b 65 79 0a  ursor.validNKey.
18577 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 73 2e 0a    ** variables..
18578 20 20 2a 2a 0a 20 20 2a 2a 20 50 72 65 76 69 6f    **.  ** Previo
18579 75 73 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53  us versions of S
1857a 51 4c 69 74 65 20 63 61 6c 6c 65 64 20 6d 6f 76  QLite called mov
1857b 65 54 6f 52 6f 6f 74 28 29 20 74 6f 20 6d 6f 76  eToRoot() to mov
1857c 65 20 74 68 65 20 63 75 72 73 6f 72 0a 20 20 2a  e the cursor.  *
1857d 2a 20 62 61 63 6b 20 74 6f 20 74 68 65 20 72 6f  * back to the ro
1857e 6f 74 20 70 61 67 65 20 61 73 20 62 61 6c 61 6e  ot page as balan
1857f 63 65 28 29 20 75 73 65 64 20 74 6f 20 69 6e 76  ce() used to inv
18580 61 6c 69 64 61 74 65 20 74 68 65 20 63 6f 6e 74  alidate the cont
18581 65 6e 74 73 0a 20 20 2a 2a 20 6f 66 20 42 74 43  ents.  ** of BtC
18582 75 72 73 6f 72 2e 61 70 50 61 67 65 5b 5d 20 61  ursor.apPage[] a
18583 6e 64 20 42 74 43 75 72 73 6f 72 2e 61 69 49 64  nd BtCursor.aiId
18584 78 5b 5d 2e 20 49 6e 73 74 65 61 64 20 6f 66 20  x[]. Instead of 
18585 64 6f 69 6e 67 20 74 68 61 74 2c 0a 20 20 2a 2a  doing that,.  **
18586 20 73 65 74 20 74 68 65 20 63 75 72 73 6f 72 20   set the cursor 
18587 73 74 61 74 65 20 74 6f 20 22 69 6e 76 61 6c 69  state to "invali
18588 64 22 2e 20 54 68 69 73 20 6d 61 6b 65 73 20 63  d". This makes c
18589 6f 6d 6d 6f 6e 20 69 6e 73 65 72 74 20 6f 70 65  ommon insert ope
1858a 72 61 74 69 6f 6e 73 0a 20 20 2a 2a 20 73 6c 69  rations.  ** sli
1858b 67 68 74 6c 79 20 66 61 73 74 65 72 2e 0a 20 20  ghtly faster..  
1858c 2a 2a 0a 20 20 2a 2a 20 54 68 65 72 65 20 69 73  **.  ** There is
1858d 20 61 20 73 75 62 74 6c 65 20 62 75 74 20 69 6d   a subtle but im
1858e 70 6f 72 74 61 6e 74 20 6f 70 74 69 6d 69 7a 61  portant optimiza
1858f 74 69 6f 6e 20 68 65 72 65 20 74 6f 6f 2e 20 57  tion here too. W
18590 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 0a 20 20  hen inserting.  
18591 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 72 65 63 6f  ** multiple reco
18592 72 64 73 20 69 6e 74 6f 20 61 6e 20 69 6e 74 6b  rds into an intk
18593 65 79 20 62 2d 74 72 65 65 20 75 73 69 6e 67 20  ey b-tree using 
18594 61 20 73 69 6e 67 6c 65 20 63 75 72 73 6f 72 20  a single cursor 
18595 28 61 73 20 63 61 6e 0a 20 20 2a 2a 20 68 61 70  (as can.  ** hap
18596 70 65 6e 20 77 68 69 6c 65 20 70 72 6f 63 65 73  pen while proces
18597 73 69 6e 67 20 61 6e 20 22 49 4e 53 45 52 54 20  sing an "INSERT 
18598 49 4e 54 4f 20 2e 2e 2e 20 53 45 4c 45 43 54 22  INTO ... SELECT"
18599 20 73 74 61 74 65 6d 65 6e 74 29 2c 20 69 74 0a   statement), it.
1859a 20 20 2a 2a 20 69 73 20 61 64 76 61 6e 74 61 67    ** is advantag
1859b 65 6f 75 73 20 74 6f 20 6c 65 61 76 65 20 74 68  eous to leave th
1859c 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e  e cursor pointin
1859d 67 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e  g to the last en
1859e 74 72 79 20 69 6e 0a 20 20 2a 2a 20 74 68 65 20  try in.  ** the 
1859f 62 2d 74 72 65 65 20 69 66 20 70 6f 73 73 69 62  b-tree if possib
185a0 6c 65 2e 20 49 66 20 74 68 65 20 63 75 72 73 6f  le. If the curso
185a1 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69  r is left pointi
185a2 6e 67 20 74 6f 20 74 68 65 20 6c 61 73 74 0a 20  ng to the last. 
185a3 20 2a 2a 20 65 6e 74 72 79 20 69 6e 20 74 68 65   ** entry in the
185a4 20 74 61 62 6c 65 2c 20 61 6e 64 20 74 68 65 20   table, and the 
185a5 6e 65 78 74 20 72 6f 77 20 69 6e 73 65 72 74 65  next row inserte
185a6 64 20 68 61 73 20 61 6e 20 69 6e 74 65 67 65 72  d has an integer
185a7 20 6b 65 79 0a 20 20 2a 2a 20 6c 61 72 67 65 72   key.  ** larger
185a8 20 74 68 61 6e 20 74 68 65 20 6c 61 72 67 65 73   than the larges
185a9 74 20 65 78 69 73 74 69 6e 67 20 6b 65 79 2c 20  t existing key, 
185aa 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74  it is possible t
185ab 6f 20 69 6e 73 65 72 74 20 74 68 65 0a 20 20 2a  o insert the.  *
185ac 2a 20 72 6f 77 20 77 69 74 68 6f 75 74 20 73 65  * row without se
185ad 65 6b 69 6e 67 20 74 68 65 20 63 75 72 73 6f 72  eking the cursor
185ae 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20 61 20  . This can be a 
185af 62 69 67 20 70 65 72 66 6f 72 6d 61 6e 63 65 20  big performance 
185b0 62 6f 6f 73 74 2e 0a 20 20 2a 2f 0a 20 20 70 43  boost..  */.  pC
185b1 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d  ur->info.nSize =
185b2 20 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69   0;.  pCur->vali
185b3 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 69 66 28  dNKey = 0;.  if(
185b4 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
185b5 26 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c  & pPage->nOverfl
185b6 6f 77 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 62  ow ){.    rc = b
185b7 61 6c 61 6e 63 65 28 70 43 75 72 29 3b 0a 0a 20  alance(pCur);.. 
185b8 20 20 20 2f 2a 20 4d 75 73 74 20 6d 61 6b 65 20     /* Must make 
185b9 73 75 72 65 20 6e 4f 76 65 72 66 6c 6f 77 20 69  sure nOverflow i
185ba 73 20 72 65 73 65 74 20 74 6f 20 7a 65 72 6f 20  s reset to zero 
185bb 65 76 65 6e 20 69 66 20 74 68 65 20 62 61 6c 61  even if the bala
185bc 6e 63 65 28 29 0a 20 20 20 20 2a 2a 20 66 61 69  nce().    ** fai
185bd 6c 73 2e 20 49 6e 74 65 72 6e 61 6c 20 64 61 74  ls. Internal dat
185be 61 20 73 74 72 75 63 74 75 72 65 20 63 6f 72 72  a structure corr
185bf 75 70 74 69 6f 6e 20 77 69 6c 6c 20 72 65 73 75  uption will resu
185c0 6c 74 20 6f 74 68 65 72 77 69 73 65 2e 20 0a 20  lt otherwise. . 
185c1 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 73 65 74 20     ** Also, set 
185c2 74 68 65 20 63 75 72 73 6f 72 20 73 74 61 74 65  the cursor state
185c3 20 74 6f 20 69 6e 76 61 6c 69 64 2e 20 54 68 69   to invalid. Thi
185c4 73 20 73 74 6f 70 73 20 73 61 76 65 43 75 72 73  s stops saveCurs
185c5 6f 72 50 6f 73 69 74 69 6f 6e 28 29 0a 20 20 20  orPosition().   
185c6 20 2a 2a 20 66 72 6f 6d 20 74 72 79 69 6e 67 20   ** from trying 
185c7 74 6f 20 73 61 76 65 20 74 68 65 20 63 75 72 72  to save the curr
185c8 65 6e 74 20 70 6f 73 69 74 69 6f 6e 20 6f 66 20  ent position of 
185c9 74 68 65 20 63 75 72 73 6f 72 2e 20 20 2a 2f 0a  the cursor.  */.
185ca 20 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65      pCur->apPage
185cb 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e  [pCur->iPage]->n
185cc 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20  Overflow = 0;.  
185cd 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d    pCur->eState =
185ce 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b   CURSOR_INVALID;
185cf 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
185d0 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
185d1 2d 3e 69 50 61 67 65 5d 2d 3e 6e 4f 76 65 72 66  ->iPage]->nOverf
185d2 6c 6f 77 3d 3d 30 20 29 3b 0a 0a 65 6e 64 5f 69  low==0 );..end_i
185d3 6e 73 65 72 74 3a 0a 20 20 72 65 74 75 72 6e 20  nsert:.  return 
185d4 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c  rc;.}../*.** Del
185d5 65 74 65 20 74 68 65 20 65 6e 74 72 79 20 74 68  ete the entry th
185d6 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  at the cursor is
185d7 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 54   pointing to.  T
185d8 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 73 20  he cursor.** is 
185d9 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74  left pointing at
185da 20 61 20 61 72 62 69 74 72 61 72 79 20 6c 6f 63   a arbitrary loc
185db 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ation..*/.SQLITE
185dc 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
185dd 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 28  ite3BtreeDelete(
185de 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b  BtCursor *pCur){
185df 0a 20 20 42 74 72 65 65 20 2a 70 20 3d 20 70 43  .  Btree *p = pC
185e0 75 72 2d 3e 70 42 74 72 65 65 3b 0a 20 20 42 74  ur->pBtree;.  Bt
185e1 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d  Shared *pBt = p-
185e2 3e 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 20  >pBt;           
185e3 20 20 20 0a 20 20 69 6e 74 20 72 63 3b 20 20 20     .  int rc;   
185e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
185e5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
185e6 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 4d  turn code */.  M
185e7 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 20 20  emPage *pPage;  
185e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
185e9 20 20 20 20 2f 2a 20 50 61 67 65 20 74 6f 20 64      /* Page to d
185ea 65 6c 65 74 65 20 63 65 6c 6c 20 66 72 6f 6d 20  elete cell from 
185eb 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  */.  unsigned ch
185ec 61 72 20 2a 70 43 65 6c 6c 3b 20 20 20 20 20 20  ar *pCell;      
185ed 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
185ee 6e 74 65 72 20 74 6f 20 63 65 6c 6c 20 74 6f 20  nter to cell to 
185ef 64 65 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20  delete */.  int 
185f0 69 43 65 6c 6c 49 64 78 3b 20 20 20 20 20 20 20  iCellIdx;       
185f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
185f2 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 65 6c   /* Index of cel
185f3 6c 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20  l to delete */. 
185f4 20 69 6e 74 20 69 43 65 6c 6c 44 65 70 74 68 3b   int iCellDepth;
185f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
185f6 20 20 20 20 20 20 2f 2a 20 44 65 70 74 68 20 6f        /* Depth o
185f7 66 20 6e 6f 64 65 20 63 6f 6e 74 61 69 6e 69 6e  f node containin
185f8 67 20 70 43 65 6c 6c 20 2a 2f 20 0a 0a 20 20 61  g pCell */ ..  a
185f9 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c  ssert( cursorHol
185fa 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b  dsMutex(pCur) );
185fb 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e  .  assert( pBt->
185fc 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54  inTransaction==T
185fd 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20  RANS_WRITE );.  
185fe 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e 72 65  assert( !pBt->re
185ff 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 61 73 73 65  adOnly );.  asse
18600 72 74 28 20 70 43 75 72 2d 3e 77 72 46 6c 61 67  rt( pCur->wrFlag
18601 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 68 61   );.  assert( ha
18602 73 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  sSharedCacheTabl
18603 65 4c 6f 63 6b 28 70 2c 20 70 43 75 72 2d 3e 70  eLock(p, pCur->p
18604 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 2d 3e 70  gnoRoot, pCur->p
18605 4b 65 79 49 6e 66 6f 21 3d 30 2c 20 32 29 20 29  KeyInfo!=0, 2) )
18606 3b 0a 20 20 61 73 73 65 72 74 28 20 21 68 61 73  ;.  assert( !has
18607 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 2c  ReadConflicts(p,
18608 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 29   pCur->pgnoRoot)
18609 20 29 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45 52   );..  if( NEVER
1860a 28 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75  (pCur->aiIdx[pCu
1860b 72 2d 3e 69 50 61 67 65 5d 3e 3d 70 43 75 72 2d  r->iPage]>=pCur-
1860c 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
1860d 61 67 65 5d 2d 3e 6e 43 65 6c 6c 29 20 0a 20 20  age]->nCell) .  
1860e 20 7c 7c 20 4e 45 56 45 52 28 70 43 75 72 2d 3e   || NEVER(pCur->
1860f 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56  eState!=CURSOR_V
18610 41 4c 49 44 29 0a 20 20 29 7b 0a 20 20 20 20 72  ALID).  ){.    r
18611 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
18612 4f 52 3b 20 20 2f 2a 20 53 6f 6d 65 74 68 69 6e  OR;  /* Somethin
18613 67 20 68 61 73 20 67 6f 6e 65 20 61 77 72 79 2e  g has gone awry.
18614 20 2a 2f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66   */.  }..  /* If
18615 20 74 68 69 73 20 69 73 20 61 20 64 65 6c 65 74   this is a delet
18616 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 72  e operation to r
18617 65 6d 6f 76 65 20 61 20 72 6f 77 20 66 72 6f 6d  emove a row from
18618 20 61 20 74 61 62 6c 65 20 62 2d 74 72 65 65 2c   a table b-tree,
18619 0a 20 20 2a 2a 20 69 6e 76 61 6c 69 64 61 74 65  .  ** invalidate
1861a 20 61 6e 79 20 69 6e 63 72 62 6c 6f 62 20 63 75   any incrblob cu
1861b 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68  rsors open on th
1861c 65 20 72 6f 77 20 62 65 69 6e 67 20 64 65 6c 65  e row being dele
1861d 74 65 64 2e 20 20 2a 2f 0a 20 20 69 66 28 20 70  ted.  */.  if( p
1861e 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30  Cur->pKeyInfo==0
1861f 20 29 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64 61   ){.    invalida
18620 74 65 49 6e 63 72 62 6c 6f 62 43 75 72 73 6f 72  teIncrblobCursor
18621 73 28 70 2c 20 70 43 75 72 2d 3e 69 6e 66 6f 2e  s(p, pCur->info.
18622 6e 4b 65 79 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20  nKey, 0);.  }.. 
18623 20 69 43 65 6c 6c 44 65 70 74 68 20 3d 20 70 43   iCellDepth = pC
18624 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 69 43 65  ur->iPage;.  iCe
18625 6c 6c 49 64 78 20 3d 20 70 43 75 72 2d 3e 61 69  llIdx = pCur->ai
18626 49 64 78 5b 69 43 65 6c 6c 44 65 70 74 68 5d 3b  Idx[iCellDepth];
18627 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d  .  pPage = pCur-
18628 3e 61 70 50 61 67 65 5b 69 43 65 6c 6c 44 65 70  >apPage[iCellDep
18629 74 68 5d 3b 0a 20 20 70 43 65 6c 6c 20 3d 20 66  th];.  pCell = f
1862a 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69  indCell(pPage, i
1862b 43 65 6c 6c 49 64 78 29 3b 0a 0a 20 20 2f 2a 20  CellIdx);..  /* 
1862c 49 66 20 74 68 65 20 70 61 67 65 20 63 6f 6e 74  If the page cont
1862d 61 69 6e 69 6e 67 20 74 68 65 20 65 6e 74 72 79  aining the entry
1862e 20 74 6f 20 64 65 6c 65 74 65 20 69 73 20 6e 6f   to delete is no
1862f 74 20 61 20 6c 65 61 66 20 70 61 67 65 2c 20 6d  t a leaf page, m
18630 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20 63 75 72  ove.  ** the cur
18631 73 6f 72 20 74 6f 20 74 68 65 20 6c 61 72 67 65  sor to the large
18632 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  st entry in the 
18633 74 72 65 65 20 74 68 61 74 20 69 73 20 73 6d 61  tree that is sma
18634 6c 6c 65 72 20 74 68 61 6e 0a 20 20 2a 2a 20 74  ller than.  ** t
18635 68 65 20 65 6e 74 72 79 20 62 65 69 6e 67 20 64  he entry being d
18636 65 6c 65 74 65 64 2e 20 54 68 69 73 20 63 65 6c  eleted. This cel
18637 6c 20 77 69 6c 6c 20 72 65 70 6c 61 63 65 20 74  l will replace t
18638 68 65 20 63 65 6c 6c 20 62 65 69 6e 67 20 64 65  he cell being de
18639 6c 65 74 65 64 0a 20 20 2a 2a 20 66 72 6f 6d 20  leted.  ** from 
1863a 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64  the internal nod
1863b 65 2e 20 54 68 65 20 27 70 72 65 76 69 6f 75 73  e. The 'previous
1863c 27 20 65 6e 74 72 79 20 69 73 20 75 73 65 64 20  ' entry is used 
1863d 66 6f 72 20 74 68 69 73 20 69 6e 73 74 65 61 64  for this instead
1863e 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 27 6e 65  .  ** of the 'ne
1863f 78 74 27 20 65 6e 74 72 79 2c 20 61 73 20 74 68  xt' entry, as th
18640 65 20 70 72 65 76 69 6f 75 73 20 65 6e 74 72 79  e previous entry
18641 20 69 73 20 61 6c 77 61 79 73 20 61 20 70 61 72   is always a par
18642 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 73 75  t of the.  ** su
18643 62 2d 74 72 65 65 20 68 65 61 64 65 64 20 62 79  b-tree headed by
18644 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 20   the child page 
18645 6f 66 20 74 68 65 20 63 65 6c 6c 20 62 65 69 6e  of the cell bein
18646 67 20 64 65 6c 65 74 65 64 2e 20 54 68 69 73 20  g deleted. This 
18647 6d 61 6b 65 73 0a 20 20 2a 2a 20 62 61 6c 61 6e  makes.  ** balan
18648 63 69 6e 67 20 74 68 65 20 74 72 65 65 20 66 6f  cing the tree fo
18649 6c 6c 6f 77 69 6e 67 20 74 68 65 20 64 65 6c 65  llowing the dele
1864a 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 65 61 73  te operation eas
1864b 69 65 72 2e 20 20 2a 2f 0a 20 20 69 66 28 20 21  ier.  */.  if( !
1864c 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20  pPage->leaf ){. 
1864d 20 20 20 69 6e 74 20 6e 6f 74 55 73 65 64 3b 0a     int notUsed;.
1864e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1864f 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 70 43  BtreePrevious(pC
18650 75 72 2c 20 26 6e 6f 74 55 73 65 64 29 3b 0a 20  ur, &notUsed);. 
18651 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75     if( rc ) retu
18652 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  rn rc;.  }..  /*
18653 20 53 61 76 65 20 74 68 65 20 70 6f 73 69 74 69   Save the positi
18654 6f 6e 73 20 6f 66 20 61 6e 79 20 6f 74 68 65 72  ons of any other
18655 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e   cursors open on
18656 20 74 68 69 73 20 74 61 62 6c 65 20 62 65 66 6f   this table befo
18657 72 65 0a 20 20 2a 2a 20 6d 61 6b 69 6e 67 20 61  re.  ** making a
18658 6e 79 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 73  ny modifications
18659 2e 20 4d 61 6b 65 20 74 68 65 20 70 61 67 65 20  . Make the page 
1865a 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 65  containing the e
1865b 6e 74 72 79 20 74 6f 20 62 65 20 0a 20 20 2a 2a  ntry to be .  **
1865c 20 64 65 6c 65 74 65 64 20 77 72 69 74 61 62 6c   deleted writabl
1865d 65 2e 20 54 68 65 6e 20 66 72 65 65 20 61 6e 79  e. Then free any
1865e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20   overflow pages 
1865f 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
18660 74 68 65 20 0a 20 20 2a 2a 20 65 6e 74 72 79 20  the .  ** entry 
18661 61 6e 64 20 66 69 6e 61 6c 6c 79 20 72 65 6d 6f  and finally remo
18662 76 65 20 74 68 65 20 63 65 6c 6c 20 69 74 73 65  ve the cell itse
18663 6c 66 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74  lf from within t
18664 68 65 20 70 61 67 65 2e 20 20 0a 20 20 2a 2f 0a  he page.  .  */.
18665 20 20 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75    rc = saveAllCu
18666 72 73 6f 72 73 28 70 42 74 2c 20 70 43 75 72 2d  rsors(pBt, pCur-
18667 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 29  >pgnoRoot, pCur)
18668 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74  ;.  if( rc ) ret
18669 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 73  urn rc;.  rc = s
1866a 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
1866b 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
1866c 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74  ;.  if( rc ) ret
1866d 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 63  urn rc;.  rc = c
1866e 6c 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20  learCell(pPage, 
1866f 70 43 65 6c 6c 29 3b 0a 20 20 64 72 6f 70 43 65  pCell);.  dropCe
18670 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 49  ll(pPage, iCellI
18671 64 78 2c 20 63 65 6c 6c 53 69 7a 65 50 74 72 28  dx, cellSizePtr(
18672 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 2c 20 26  pPage, pCell), &
18673 72 63 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20  rc);.  if( rc ) 
18674 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a  return rc;..  /*
18675 20 49 66 20 74 68 65 20 63 65 6c 6c 20 64 65 6c   If the cell del
18676 65 74 65 64 20 77 61 73 20 6e 6f 74 20 6c 6f 63  eted was not loc
18677 61 74 65 64 20 6f 6e 20 61 20 6c 65 61 66 20 70  ated on a leaf p
18678 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20 63 75  age, then the cu
18679 72 73 6f 72 0a 20 20 2a 2a 20 69 73 20 63 75 72  rsor.  ** is cur
1867a 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20  rently pointing 
1867b 74 6f 20 74 68 65 20 6c 61 72 67 65 73 74 20 65  to the largest e
1867c 6e 74 72 79 20 69 6e 20 74 68 65 20 73 75 62 2d  ntry in the sub-
1867d 74 72 65 65 20 68 65 61 64 65 64 0a 20 20 2a 2a  tree headed.  **
1867e 20 62 79 20 74 68 65 20 63 68 69 6c 64 2d 70 61   by the child-pa
1867f 67 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 74  ge of the cell t
18680 68 61 74 20 77 61 73 20 6a 75 73 74 20 64 65 6c  hat was just del
18681 65 74 65 64 20 66 72 6f 6d 20 61 6e 20 69 6e 74  eted from an int
18682 65 72 6e 61 6c 0a 20 20 2a 2a 20 6e 6f 64 65 2e  ernal.  ** node.
18683 20 54 68 65 20 63 65 6c 6c 20 66 72 6f 6d 20 74   The cell from t
18684 68 65 20 6c 65 61 66 20 6e 6f 64 65 20 6e 65 65  he leaf node nee
18685 64 73 20 74 6f 20 62 65 20 6d 6f 76 65 64 20 74  ds to be moved t
18686 6f 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 20  o the internal. 
18687 20 2a 2a 20 6e 6f 64 65 20 74 6f 20 72 65 70 6c   ** node to repl
18688 61 63 65 20 74 68 65 20 64 65 6c 65 74 65 64 20  ace the deleted 
18689 63 65 6c 6c 2e 20 20 2a 2f 0a 20 20 69 66 28 20  cell.  */.  if( 
1868a 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a  !pPage->leaf ){.
1868b 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4c 65      MemPage *pLe
1868c 61 66 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67  af = pCur->apPag
1868d 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a  e[pCur->iPage];.
1868e 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20      int nCell;. 
1868f 20 20 20 50 67 6e 6f 20 6e 20 3d 20 70 43 75 72     Pgno n = pCur
18690 2d 3e 61 70 50 61 67 65 5b 69 43 65 6c 6c 44 65  ->apPage[iCellDe
18691 70 74 68 2b 31 5d 2d 3e 70 67 6e 6f 3b 0a 20 20  pth+1]->pgno;.  
18692 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
18693 2a 70 54 6d 70 3b 0a 0a 20 20 20 20 70 43 65 6c  *pTmp;..    pCel
18694 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 4c 65  l = findCell(pLe
18695 61 66 2c 20 70 4c 65 61 66 2d 3e 6e 43 65 6c 6c  af, pLeaf->nCell
18696 2d 31 29 3b 0a 20 20 20 20 6e 43 65 6c 6c 20 3d  -1);.    nCell =
18697 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 4c 65   cellSizePtr(pLe
18698 61 66 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20  af, pCell);.    
18699 61 73 73 65 72 74 28 20 4d 58 5f 43 45 4c 4c 5f  assert( MX_CELL_
1869a 53 49 5a 45 28 70 42 74 29 3e 3d 6e 43 65 6c 6c  SIZE(pBt)>=nCell
1869b 20 29 3b 0a 0a 20 20 20 20 61 6c 6c 6f 63 61 74   );..    allocat
1869c 65 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b  eTempSpace(pBt);
1869d 0a 20 20 20 20 70 54 6d 70 20 3d 20 70 42 74 2d  .    pTmp = pBt-
1869e 3e 70 54 6d 70 53 70 61 63 65 3b 0a 0a 20 20 20  >pTmpSpace;..   
1869f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
186a0 65 72 57 72 69 74 65 28 70 4c 65 61 66 2d 3e 70  erWrite(pLeaf->p
186a1 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 6e 73  DbPage);.    ins
186a2 65 72 74 43 65 6c 6c 28 70 50 61 67 65 2c 20 69  ertCell(pPage, i
186a3 43 65 6c 6c 49 64 78 2c 20 70 43 65 6c 6c 2d 34  CellIdx, pCell-4
186a4 2c 20 6e 43 65 6c 6c 2b 34 2c 20 70 54 6d 70 2c  , nCell+4, pTmp,
186a5 20 6e 2c 20 26 72 63 29 3b 0a 20 20 20 20 64 72   n, &rc);.    dr
186a6 6f 70 43 65 6c 6c 28 70 4c 65 61 66 2c 20 70 4c  opCell(pLeaf, pL
186a7 65 61 66 2d 3e 6e 43 65 6c 6c 2d 31 2c 20 6e 43  eaf->nCell-1, nC
186a8 65 6c 6c 2c 20 26 72 63 29 3b 0a 20 20 20 20 69  ell, &rc);.    i
186a9 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
186aa 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 61 6c  c;.  }..  /* Bal
186ab 61 6e 63 65 20 74 68 65 20 74 72 65 65 2e 20 49  ance the tree. I
186ac 66 20 74 68 65 20 65 6e 74 72 79 20 64 65 6c 65  f the entry dele
186ad 74 65 64 20 77 61 73 20 6c 6f 63 61 74 65 64 20  ted was located 
186ae 6f 6e 20 61 20 6c 65 61 66 20 70 61 67 65 2c 0a  on a leaf page,.
186af 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 75    ** then the cu
186b0 72 73 6f 72 20 73 74 69 6c 6c 20 70 6f 69 6e 74  rsor still point
186b1 73 20 74 6f 20 74 68 61 74 20 70 61 67 65 2e 20  s to that page. 
186b2 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65  In this case the
186b3 20 66 69 72 73 74 0a 20 20 2a 2a 20 63 61 6c 6c   first.  ** call
186b4 20 74 6f 20 62 61 6c 61 6e 63 65 28 29 20 72 65   to balance() re
186b5 70 61 69 72 73 20 74 68 65 20 74 72 65 65 2c 20  pairs the tree, 
186b6 61 6e 64 20 74 68 65 20 69 66 28 2e 2e 2e 29 20  and the if(...) 
186b7 63 6f 6e 64 69 74 69 6f 6e 20 69 73 0a 20 20 2a  condition is.  *
186b8 2a 20 6e 65 76 65 72 20 74 72 75 65 2e 0a 20 20  * never true..  
186b9 2a 2a 0a 20 20 2a 2a 20 4f 74 68 65 72 77 69 73  **.  ** Otherwis
186ba 65 2c 20 69 66 20 74 68 65 20 65 6e 74 72 79 20  e, if the entry 
186bb 64 65 6c 65 74 65 64 20 77 61 73 20 6f 6e 20 61  deleted was on a
186bc 6e 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20  n internal node 
186bd 70 61 67 65 2c 20 74 68 65 6e 0a 20 20 2a 2a 20  page, then.  ** 
186be 70 43 75 72 20 69 73 20 70 6f 69 6e 74 69 6e 67  pCur is pointing
186bf 20 74 6f 20 74 68 65 20 6c 65 61 66 20 70 61 67   to the leaf pag
186c0 65 20 66 72 6f 6d 20 77 68 69 63 68 20 61 20 63  e from which a c
186c1 65 6c 6c 20 77 61 73 20 72 65 6d 6f 76 65 64 20  ell was removed 
186c2 74 6f 0a 20 20 2a 2a 20 72 65 70 6c 61 63 65 20  to.  ** replace 
186c3 74 68 65 20 63 65 6c 6c 20 64 65 6c 65 74 65 64  the cell deleted
186c4 20 66 72 6f 6d 20 74 68 65 20 69 6e 74 65 72 6e   from the intern
186c5 61 6c 20 6e 6f 64 65 2e 20 54 68 69 73 20 69 73  al node. This is
186c6 20 73 6c 69 67 68 74 6c 79 0a 20 20 2a 2a 20 74   slightly.  ** t
186c7 72 69 63 6b 79 20 61 73 20 74 68 65 20 6c 65 61  ricky as the lea
186c8 66 20 6e 6f 64 65 20 6d 61 79 20 62 65 20 75 6e  f node may be un
186c9 64 65 72 66 75 6c 6c 2c 20 61 6e 64 20 74 68 65  derfull, and the
186ca 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 6d   internal node m
186cb 61 79 0a 20 20 2a 2a 20 62 65 20 65 69 74 68 65  ay.  ** be eithe
186cc 72 20 75 6e 64 65 72 20 6f 72 20 6f 76 65 72 66  r under or overf
186cd 75 6c 6c 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ull. In this cas
186ce 65 20 72 75 6e 20 74 68 65 20 62 61 6c 61 6e 63  e run the balanc
186cf 69 6e 67 20 61 6c 67 6f 72 69 74 68 6d 0a 20 20  ing algorithm.  
186d0 2a 2a 20 6f 6e 20 74 68 65 20 6c 65 61 66 20 6e  ** on the leaf n
186d1 6f 64 65 20 66 69 72 73 74 2e 20 49 66 20 74 68  ode first. If th
186d2 65 20 62 61 6c 61 6e 63 65 20 70 72 6f 63 65 65  e balance procee
186d3 64 73 20 66 61 72 20 65 6e 6f 75 67 68 20 75 70  ds far enough up
186d4 20 74 68 65 0a 20 20 2a 2a 20 74 72 65 65 20 74   the.  ** tree t
186d5 68 61 74 20 77 65 20 63 61 6e 20 62 65 20 73 75  hat we can be su
186d6 72 65 20 74 68 61 74 20 61 6e 79 20 70 72 6f 62  re that any prob
186d7 6c 65 6d 20 69 6e 20 74 68 65 20 69 6e 74 65 72  lem in the inter
186d8 6e 61 6c 20 6e 6f 64 65 20 68 61 73 0a 20 20 2a  nal node has.  *
186d9 2a 20 62 65 65 6e 20 63 6f 72 72 65 63 74 65 64  * been corrected
186da 2c 20 73 6f 20 62 65 20 69 74 2e 20 4f 74 68 65  , so be it. Othe
186db 72 77 69 73 65 2c 20 61 66 74 65 72 20 62 61 6c  rwise, after bal
186dc 61 6e 63 69 6e 67 20 74 68 65 20 6c 65 61 66 20  ancing the leaf 
186dd 6e 6f 64 65 2c 0a 20 20 2a 2a 20 77 61 6c 6b 20  node,.  ** walk 
186de 74 68 65 20 63 75 72 73 6f 72 20 75 70 20 74 68  the cursor up th
186df 65 20 74 72 65 65 20 74 6f 20 74 68 65 20 69 6e  e tree to the in
186e0 74 65 72 6e 61 6c 20 6e 6f 64 65 20 61 6e 64 20  ternal node and 
186e1 62 61 6c 61 6e 63 65 20 69 74 20 61 73 20 0a 20  balance it as . 
186e2 20 2a 2a 20 77 65 6c 6c 2e 20 20 2a 2f 0a 20 20   ** well.  */.  
186e3 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 75  rc = balance(pCu
186e4 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  r);.  if( rc==SQ
186e5 4c 49 54 45 5f 4f 4b 20 26 26 20 70 43 75 72 2d  LITE_OK && pCur-
186e6 3e 69 50 61 67 65 3e 69 43 65 6c 6c 44 65 70 74  >iPage>iCellDept
186e7 68 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 20  h ){.    while( 
186e8 70 43 75 72 2d 3e 69 50 61 67 65 3e 69 43 65 6c  pCur->iPage>iCel
186e9 6c 44 65 70 74 68 20 29 7b 0a 20 20 20 20 20 20  lDepth ){.      
186ea 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 75 72  releasePage(pCur
186eb 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
186ec 50 61 67 65 2d 2d 5d 29 3b 0a 20 20 20 20 7d 0a  Page--]);.    }.
186ed 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65      rc = balance
186ee 28 70 43 75 72 29 3b 0a 20 20 7d 0a 0a 20 20 69  (pCur);.  }..  i
186ef 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
186f0 20 29 7b 0a 20 20 20 20 6d 6f 76 65 54 6f 52 6f   ){.    moveToRo
186f1 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d 0a 20 20  ot(pCur);.  }.  
186f2 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
186f3 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77  .** Create a new
186f4 20 42 54 72 65 65 20 74 61 62 6c 65 2e 20 20 57   BTree table.  W
186f5 72 69 74 65 20 69 6e 74 6f 20 2a 70 69 54 61 62  rite into *piTab
186f6 6c 65 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 6e  le the page.** n
186f7 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 72 6f  umber for the ro
186f8 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 6e  ot page of the n
186f9 65 77 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  ew table..**.** 
186fa 54 68 65 20 74 79 70 65 20 6f 66 20 74 79 70 65  The type of type
186fb 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62   is determined b
186fc 79 20 74 68 65 20 66 6c 61 67 73 20 70 61 72 61  y the flags para
186fd 6d 65 74 65 72 2e 20 20 4f 6e 6c 79 20 74 68 65  meter.  Only the
186fe 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61  .** following va
186ff 6c 75 65 73 20 6f 66 20 66 6c 61 67 73 20 61 72  lues of flags ar
18700 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75  e currently in u
18701 73 65 2e 20 20 4f 74 68 65 72 20 76 61 6c 75 65  se.  Other value
18702 73 20 66 6f 72 0a 2a 2a 20 66 6c 61 67 73 20 6d  s for.** flags m
18703 69 67 68 74 20 6e 6f 74 20 77 6f 72 6b 3a 0a 2a  ight not work:.*
18704 2a 0a 2a 2a 20 20 20 20 20 42 54 52 45 45 5f 49  *.**     BTREE_I
18705 4e 54 4b 45 59 7c 42 54 52 45 45 5f 4c 45 41 46  NTKEY|BTREE_LEAF
18706 44 41 54 41 20 20 20 20 20 55 73 65 64 20 66 6f  DATA     Used fo
18707 72 20 53 51 4c 20 74 61 62 6c 65 73 20 77 69 74  r SQL tables wit
18708 68 20 72 6f 77 69 64 20 6b 65 79 73 0a 2a 2a 20  h rowid keys.** 
18709 20 20 20 20 42 54 52 45 45 5f 5a 45 52 4f 44 41      BTREE_ZERODA
1870a 54 41 20 20 20 20 20 20 20 20 20 20 20 20 20 20  TA              
1870b 20 20 20 20 55 73 65 64 20 66 6f 72 20 53 51 4c      Used for SQL
1870c 20 69 6e 64 69 63 65 73 0a 2a 2f 0a 73 74 61 74   indices.*/.stat
1870d 69 63 20 69 6e 74 20 62 74 72 65 65 43 72 65 61  ic int btreeCrea
1870e 74 65 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70  teTable(Btree *p
1870f 2c 20 69 6e 74 20 2a 70 69 54 61 62 6c 65 2c 20  , int *piTable, 
18710 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 42 74  int flags){.  Bt
18711 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d  Shared *pBt = p-
18712 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20  >pBt;.  MemPage 
18713 2a 70 52 6f 6f 74 3b 0a 20 20 50 67 6e 6f 20 70  *pRoot;.  Pgno p
18714 67 6e 6f 52 6f 6f 74 3b 0a 20 20 69 6e 74 20 72  gnoRoot;.  int r
18715 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  c;..  assert( sq
18716 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d  lite3BtreeHoldsM
18717 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73  utex(p) );.  ass
18718 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e  ert( pBt->inTran
18719 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57  saction==TRANS_W
1871a 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74  RITE );.  assert
1871b 28 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79  ( !pBt->readOnly
1871c 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49   );..#ifdef SQLI
1871d 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
1871e 55 4d 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61  UM.  rc = alloca
1871f 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c  teBtreePage(pBt,
18720 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f 52 6f   &pRoot, &pgnoRo
18721 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20 69 66 28  ot, 1, 0);.  if(
18722 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72   rc ){.    retur
18723 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a  n rc;.  }.#else.
18724 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56    if( pBt->autoV
18725 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 50 67 6e  acuum ){.    Pgn
18726 6f 20 70 67 6e 6f 4d 6f 76 65 3b 20 20 20 20 20  o pgnoMove;     
18727 20 2f 2a 20 4d 6f 76 65 20 61 20 70 61 67 65 20   /* Move a page 
18728 68 65 72 65 20 74 6f 20 6d 61 6b 65 20 72 6f 6f  here to make roo
18729 6d 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 2d 70  m for the root-p
1872a 61 67 65 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61  age */.    MemPa
1872b 67 65 20 2a 70 50 61 67 65 4d 6f 76 65 3b 20 2f  ge *pPageMove; /
1872c 2a 20 54 68 65 20 70 61 67 65 20 74 6f 20 6d 6f  * The page to mo
1872d 76 65 20 74 6f 2e 20 2a 2f 0a 0a 20 20 20 20 2f  ve to. */..    /
1872e 2a 20 43 72 65 61 74 69 6e 67 20 61 20 6e 65 77  * Creating a new
1872f 20 74 61 62 6c 65 20 6d 61 79 20 70 72 6f 62 61   table may proba
18730 62 6c 79 20 72 65 71 75 69 72 65 20 6d 6f 76 69  bly require movi
18731 6e 67 20 61 6e 20 65 78 69 73 74 69 6e 67 20 64  ng an existing d
18732 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 74  atabase.    ** t
18733 6f 20 6d 61 6b 65 20 72 6f 6f 6d 20 66 6f 72 20  o make room for 
18734 74 68 65 20 6e 65 77 20 74 61 62 6c 65 73 20 72  the new tables r
18735 6f 6f 74 20 70 61 67 65 2e 20 49 6e 20 63 61 73  oot page. In cas
18736 65 20 74 68 69 73 20 70 61 67 65 20 74 75 72 6e  e this page turn
18737 73 0a 20 20 20 20 2a 2a 20 6f 75 74 20 74 6f 20  s.    ** out to 
18738 62 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70  be an overflow p
18739 61 67 65 2c 20 64 65 6c 65 74 65 20 61 6c 6c 20  age, delete all 
1873a 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6d 61  overflow page-ma
1873b 70 20 63 61 63 68 65 73 0a 20 20 20 20 2a 2a 20  p caches.    ** 
1873c 68 65 6c 64 20 62 79 20 6f 70 65 6e 20 63 75 72  held by open cur
1873d 73 6f 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  sors..    */.   
1873e 20 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76   invalidateAllOv
1873f 65 72 66 6c 6f 77 43 61 63 68 65 28 70 42 74 29  erflowCache(pBt)
18740 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74  ;..    /* Read t
18741 68 65 20 76 61 6c 75 65 20 6f 66 20 6d 65 74 61  he value of meta
18742 5b 33 5d 20 66 72 6f 6d 20 74 68 65 20 64 61 74  [3] from the dat
18743 61 62 61 73 65 20 74 6f 20 64 65 74 65 72 6d 69  abase to determi
18744 6e 65 20 77 68 65 72 65 20 74 68 65 0a 20 20 20  ne where the.   
18745 20 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 6f 66   ** root page of
18746 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 73   the new table s
18747 68 6f 75 6c 64 20 67 6f 2e 20 6d 65 74 61 5b 33  hould go. meta[3
18748 5d 20 69 73 20 74 68 65 20 6c 61 72 67 65 73 74  ] is the largest
18749 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 2a   root-page.    *
1874a 2a 20 63 72 65 61 74 65 64 20 73 6f 20 66 61 72  * created so far
1874b 2c 20 73 6f 20 74 68 65 20 6e 65 77 20 72 6f 6f  , so the new roo
1874c 74 2d 70 61 67 65 20 69 73 20 28 6d 65 74 61 5b  t-page is (meta[
1874d 33 5d 2b 31 29 2e 0a 20 20 20 20 2a 2f 0a 20 20  3]+1)..    */.  
1874e 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65    sqlite3BtreeGe
1874f 74 4d 65 74 61 28 70 2c 20 42 54 52 45 45 5f 4c  tMeta(p, BTREE_L
18750 41 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45  ARGEST_ROOT_PAGE
18751 2c 20 26 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20  , &pgnoRoot);.  
18752 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 0a 20    pgnoRoot++;.. 
18753 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 6f     /* The new ro
18754 6f 74 2d 70 61 67 65 20 6d 61 79 20 6e 6f 74 20  ot-page may not 
18755 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 6f 6e 20  be allocated on 
18756 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61  a pointer-map pa
18757 67 65 2c 20 6f 72 20 74 68 65 0a 20 20 20 20 2a  ge, or the.    *
18758 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 70  * PENDING_BYTE p
18759 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  age..    */.    
1875a 77 68 69 6c 65 28 20 70 67 6e 6f 52 6f 6f 74 3d  while( pgnoRoot=
1875b 3d 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70  =PTRMAP_PAGENO(p
1875c 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 29 20 7c 7c  Bt, pgnoRoot) ||
1875d 0a 20 20 20 20 20 20 20 20 70 67 6e 6f 52 6f 6f  .        pgnoRoo
1875e 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  t==PENDING_BYTE_
1875f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20  PAGE(pBt) ){.   
18760 20 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 20     pgnoRoot++;. 
18761 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
18762 20 70 67 6e 6f 52 6f 6f 74 3e 3d 33 20 29 3b 0a   pgnoRoot>=3 );.
18763 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  .    /* Allocate
18764 20 61 20 70 61 67 65 2e 20 54 68 65 20 70 61 67   a page. The pag
18765 65 20 74 68 61 74 20 63 75 72 72 65 6e 74 6c 79  e that currently
18766 20 72 65 73 69 64 65 73 20 61 74 20 70 67 6e 6f   resides at pgno
18767 52 6f 6f 74 20 77 69 6c 6c 0a 20 20 20 20 2a 2a  Root will.    **
18768 20 62 65 20 6d 6f 76 65 64 20 74 6f 20 74 68 65   be moved to the
18769 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 20   allocated page 
1876a 28 75 6e 6c 65 73 73 20 74 68 65 20 61 6c 6c 6f  (unless the allo
1876b 63 61 74 65 64 20 70 61 67 65 20 68 61 70 70 65  cated page happe
1876c 6e 73 0a 20 20 20 20 2a 2a 20 74 6f 20 72 65 73  ns.    ** to res
1876d 69 64 65 20 61 74 20 70 67 6e 6f 52 6f 6f 74 29  ide at pgnoRoot)
1876e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20  ..    */.    rc 
1876f 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50  = allocateBtreeP
18770 61 67 65 28 70 42 74 2c 20 26 70 50 61 67 65 4d  age(pBt, &pPageM
18771 6f 76 65 2c 20 26 70 67 6e 6f 4d 6f 76 65 2c 20  ove, &pgnoMove, 
18772 70 67 6e 6f 52 6f 6f 74 2c 20 31 29 3b 0a 20 20  pgnoRoot, 1);.  
18773 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
18774 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74  _OK ){.      ret
18775 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20  urn rc;.    }.. 
18776 20 20 20 69 66 28 20 70 67 6e 6f 4d 6f 76 65 21     if( pgnoMove!
18777 3d 70 67 6e 6f 52 6f 6f 74 20 29 7b 0a 20 20 20  =pgnoRoot ){.   
18778 20 20 20 2f 2a 20 70 67 6e 6f 52 6f 6f 74 20 69     /* pgnoRoot i
18779 73 20 74 68 65 20 70 61 67 65 20 74 68 61 74 20  s the page that 
1877a 77 69 6c 6c 20 62 65 20 75 73 65 64 20 66 6f 72  will be used for
1877b 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 6f   the root-page o
1877c 66 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6e  f.      ** the n
1877d 65 77 20 74 61 62 6c 65 20 28 61 73 73 75 6d 69  ew table (assumi
1877e 6e 67 20 61 6e 20 65 72 72 6f 72 20 64 69 64 20  ng an error did 
1877f 6e 6f 74 20 6f 63 63 75 72 29 2e 20 42 75 74 20  not occur). But 
18780 77 65 20 77 65 72 65 0a 20 20 20 20 20 20 2a 2a  we were.      **
18781 20 61 6c 6c 6f 63 61 74 65 64 20 70 67 6e 6f 4d   allocated pgnoM
18782 6f 76 65 2e 20 49 66 20 72 65 71 75 69 72 65 64  ove. If required
18783 20 28 69 2e 65 2e 20 69 66 20 69 74 20 77 61 73   (i.e. if it was
18784 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 64 0a 20   not allocated. 
18785 20 20 20 20 20 2a 2a 20 62 79 20 65 78 74 65 6e       ** by exten
18786 64 69 6e 67 20 74 68 65 20 66 69 6c 65 29 2c 20  ding the file), 
18787 74 68 65 20 63 75 72 72 65 6e 74 20 70 61 67 65  the current page
18788 20 61 74 20 70 6f 73 69 74 69 6f 6e 20 70 67 6e   at position pgn
18789 6f 4d 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20 69  oMove.      ** i
1878a 73 20 61 6c 72 65 61 64 79 20 6a 6f 75 72 6e 61  s already journa
1878b 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  led..      */.  
1878c 20 20 20 20 75 38 20 65 54 79 70 65 20 3d 20 30      u8 eType = 0
1878d 3b 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 50 74  ;.      Pgno iPt
1878e 72 50 61 67 65 20 3d 20 30 3b 0a 0a 20 20 20 20  rPage = 0;..    
1878f 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50    releasePage(pP
18790 61 67 65 4d 6f 76 65 29 3b 0a 0a 20 20 20 20 20  ageMove);..     
18791 20 2f 2a 20 4d 6f 76 65 20 74 68 65 20 70 61 67   /* Move the pag
18792 65 20 63 75 72 72 65 6e 74 6c 79 20 61 74 20 70  e currently at p
18793 67 6e 6f 52 6f 6f 74 20 74 6f 20 70 67 6e 6f 4d  gnoRoot to pgnoM
18794 6f 76 65 2e 20 2a 2f 0a 20 20 20 20 20 20 72 63  ove. */.      rc
18795 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28   = btreeGetPage(
18796 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 26  pBt, pgnoRoot, &
18797 70 52 6f 6f 74 2c 20 30 29 3b 0a 20 20 20 20 20  pRoot, 0);.     
18798 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
18799 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  OK ){.        re
1879a 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d  turn rc;.      }
1879b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d  .      rc = ptrm
1879c 61 70 47 65 74 28 70 42 74 2c 20 70 67 6e 6f 52  apGet(pBt, pgnoR
1879d 6f 6f 74 2c 20 26 65 54 79 70 65 2c 20 26 69 50  oot, &eType, &iP
1879e 74 72 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69  trPage);.      i
1879f 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50  f( eType==PTRMAP
187a0 5f 52 4f 4f 54 50 41 47 45 20 7c 7c 20 65 54 79  _ROOTPAGE || eTy
187a1 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45 50  pe==PTRMAP_FREEP
187a2 41 47 45 20 29 7b 0a 20 20 20 20 20 20 20 20 72  AGE ){.        r
187a3 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  c = SQLITE_CORRU
187a4 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d  PT_BKPT;.      }
187a5 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
187a6 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
187a7 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
187a8 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20 20 20  pRoot);.        
187a9 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
187aa 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   }.      assert(
187ab 20 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f 52   eType!=PTRMAP_R
187ac 4f 4f 54 50 41 47 45 20 29 3b 0a 20 20 20 20 20  OOTPAGE );.     
187ad 20 61 73 73 65 72 74 28 20 65 54 79 70 65 21 3d   assert( eType!=
187ae 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20  PTRMAP_FREEPAGE 
187af 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 72 65  );.      rc = re
187b0 6c 6f 63 61 74 65 50 61 67 65 28 70 42 74 2c 20  locatePage(pBt, 
187b1 70 52 6f 6f 74 2c 20 65 54 79 70 65 2c 20 69 50  pRoot, eType, iP
187b2 74 72 50 61 67 65 2c 20 70 67 6e 6f 4d 6f 76 65  trPage, pgnoMove
187b3 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 65 6c 65  , 0);.      rele
187b4 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a  asePage(pRoot);.
187b5 0a 20 20 20 20 20 20 2f 2a 20 4f 62 74 61 69 6e  .      /* Obtain
187b6 20 74 68 65 20 70 61 67 65 20 61 74 20 70 67 6e   the page at pgn
187b7 6f 52 6f 6f 74 20 2a 2f 0a 20 20 20 20 20 20 69  oRoot */.      i
187b8 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
187b9 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
187ba 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20  rn rc;.      }. 
187bb 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47       rc = btreeG
187bc 65 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f  etPage(pBt, pgno
187bd 52 6f 6f 74 2c 20 26 70 52 6f 6f 74 2c 20 30 29  Root, &pRoot, 0)
187be 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
187bf 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
187c0 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
187c1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63        }.      rc
187c2 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57   = sqlite3PagerW
187c3 72 69 74 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50  rite(pRoot->pDbP
187c4 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  age);.      if( 
187c5 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
187c6 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65  .        release
187c7 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a 20 20 20  Page(pRoot);.   
187c8 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
187c9 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
187ca 65 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74 20 3d  e{.      pRoot =
187cb 20 70 50 61 67 65 4d 6f 76 65 3b 0a 20 20 20 20   pPageMove;.    
187cc 7d 20 0a 0a 20 20 20 20 2f 2a 20 55 70 64 61 74  } ..    /* Updat
187cd 65 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61  e the pointer-ma
187ce 70 20 61 6e 64 20 6d 65 74 61 2d 64 61 74 61 20  p and meta-data 
187cf 77 69 74 68 20 74 68 65 20 6e 65 77 20 72 6f 6f  with the new roo
187d0 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 2a  t-page number. *
187d1 2f 0a 20 20 20 20 70 74 72 6d 61 70 50 75 74 28  /.    ptrmapPut(
187d2 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 50  pBt, pgnoRoot, P
187d3 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 2c 20  TRMAP_ROOTPAGE, 
187d4 30 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 66 28  0, &rc);.    if(
187d5 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 6c   rc ){.      rel
187d6 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b  easePage(pRoot);
187d7 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
187d8 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d  ;.    }.    rc =
187d9 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64   sqlite3BtreeUpd
187da 61 74 65 4d 65 74 61 28 70 2c 20 34 2c 20 70 67  ateMeta(p, 4, pg
187db 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 20 69 66 28  noRoot);.    if(
187dc 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 6c   rc ){.      rel
187dd 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b  easePage(pRoot);
187de 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
187df 3b 0a 20 20 20 20 7d 0a 0a 20 20 7d 65 6c 73 65  ;.    }..  }else
187e0 7b 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63  {.    rc = alloc
187e1 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74  ateBtreePage(pBt
187e2 2c 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f 52  , &pRoot, &pgnoR
187e3 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20  oot, 1, 0);.    
187e4 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
187e5 72 63 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20  rc;.  }.#endif. 
187e6 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
187e7 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65  PagerIswriteable
187e8 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29  (pRoot->pDbPage)
187e9 20 29 3b 0a 20 20 7a 65 72 6f 50 61 67 65 28 70   );.  zeroPage(p
187ea 52 6f 6f 74 2c 20 66 6c 61 67 73 20 7c 20 50 54  Root, flags | PT
187eb 46 5f 4c 45 41 46 29 3b 0a 20 20 73 71 6c 69 74  F_LEAF);.  sqlit
187ec 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 52 6f  e3PagerUnref(pRo
187ed 6f 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  ot->pDbPage);.  
187ee 2a 70 69 54 61 62 6c 65 20 3d 20 28 69 6e 74 29  *piTable = (int)
187ef 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 72 65 74 75  pgnoRoot;.  retu
187f0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
187f1 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
187f2 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
187f3 72 65 61 74 65 54 61 62 6c 65 28 42 74 72 65 65  reateTable(Btree
187f4 20 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61 62 6c   *p, int *piTabl
187f5 65 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20  e, int flags){. 
187f6 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74   int rc;.  sqlit
187f7 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
187f8 0a 20 20 72 63 20 3d 20 62 74 72 65 65 43 72 65  .  rc = btreeCre
187f9 61 74 65 54 61 62 6c 65 28 70 2c 20 70 69 54 61  ateTable(p, piTa
187fa 62 6c 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20 73  ble, flags);.  s
187fb 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
187fc 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  (p);.  return rc
187fd 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72 61 73 65  ;.}../*.** Erase
187fe 20 74 68 65 20 67 69 76 65 6e 20 64 61 74 61 62   the given datab
187ff 61 73 65 20 70 61 67 65 20 61 6e 64 20 61 6c 6c  ase page and all
18800 20 69 74 73 20 63 68 69 6c 64 72 65 6e 2e 20 20   its children.  
18801 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 70 61  Return.** the pa
18802 67 65 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69  ge to the freeli
18803 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  st..*/.static in
18804 74 20 63 6c 65 61 72 44 61 74 61 62 61 73 65 50  t clearDatabaseP
18805 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 20  age(.  BtShared 
18806 2a 70 42 74 2c 20 20 20 20 20 20 20 20 20 20 20  *pBt,           
18807 2f 2a 20 54 68 65 20 42 54 72 65 65 20 74 68 61  /* The BTree tha
18808 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74  t contains the t
18809 61 62 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70  able */.  Pgno p
1880a 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20  gno,            
1880b 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 74  /* Page number t
1880c 6f 20 63 6c 65 61 72 20 2a 2f 0a 20 20 69 6e 74  o clear */.  int
1880d 20 66 72 65 65 50 61 67 65 46 6c 61 67 2c 20 20   freePageFlag,  
1880e 20 20 20 2f 2a 20 44 65 61 6c 6c 6f 63 61 74 65     /* Deallocate
1880f 20 70 61 67 65 20 69 66 20 74 72 75 65 20 2a 2f   page if true */
18810 0a 20 20 69 6e 74 20 2a 70 6e 43 68 61 6e 67 65  .  int *pnChange
18811 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  .){.  MemPage *p
18812 50 61 67 65 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  Page;.  int rc;.
18813 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
18814 2a 70 43 65 6c 6c 3b 0a 20 20 69 6e 74 20 69 3b  *pCell;.  int i;
18815 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
18816 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
18817 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
18818 69 66 28 20 70 67 6e 6f 3e 70 61 67 65 72 50 61  if( pgno>pagerPa
18819 67 65 63 6f 75 6e 74 28 70 42 74 29 20 29 7b 0a  gecount(pBt) ){.
1881a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1881b 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
1881c 20 20 7d 0a 0a 20 20 72 63 20 3d 20 67 65 74 41    }..  rc = getA
1881d 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74 2c 20  ndInitPage(pBt, 
1881e 70 67 6e 6f 2c 20 26 70 50 61 67 65 29 3b 0a 20  pgno, &pPage);. 
1881f 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
18820 20 72 63 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20   rc;.  for(i=0; 
18821 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20  i<pPage->nCell; 
18822 69 2b 2b 29 7b 0a 20 20 20 20 70 43 65 6c 6c 20  i++){.    pCell 
18823 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65  = findCell(pPage
18824 2c 20 69 29 3b 0a 20 20 20 20 69 66 28 20 21 70  , i);.    if( !p
18825 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20  Page->leaf ){.  
18826 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 44 61      rc = clearDa
18827 74 61 62 61 73 65 50 61 67 65 28 70 42 74 2c 20  tabasePage(pBt, 
18828 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c 29 2c  get4byte(pCell),
18829 20 31 2c 20 70 6e 43 68 61 6e 67 65 29 3b 0a 20   1, pnChange);. 
1882a 20 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f       if( rc ) go
1882b 74 6f 20 63 6c 65 61 72 64 61 74 61 62 61 73 65  to cleardatabase
1882c 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a  page_out;.    }.
1882d 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 43 65      rc = clearCe
1882e 6c 6c 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 29  ll(pPage, pCell)
1882f 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67  ;.    if( rc ) g
18830 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 62 61 73  oto cleardatabas
18831 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20  epage_out;.  }. 
18832 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61   if( !pPage->lea
18833 66 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 63 6c  f ){.    rc = cl
18834 65 61 72 44 61 74 61 62 61 73 65 50 61 67 65 28  earDatabasePage(
18835 70 42 74 2c 20 67 65 74 34 62 79 74 65 28 26 70  pBt, get4byte(&p
18836 50 61 67 65 2d 3e 61 44 61 74 61 5b 38 5d 29 2c  Page->aData[8]),
18837 20 31 2c 20 70 6e 43 68 61 6e 67 65 29 3b 0a 20   1, pnChange);. 
18838 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f     if( rc ) goto
18839 20 63 6c 65 61 72 64 61 74 61 62 61 73 65 70 61   cleardatabasepa
1883a 67 65 5f 6f 75 74 3b 0a 20 20 7d 65 6c 73 65 20  ge_out;.  }else 
1883b 69 66 28 20 70 6e 43 68 61 6e 67 65 20 29 7b 0a  if( pnChange ){.
1883c 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
1883d 65 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a 20 20 20  e->intKey );.   
1883e 20 2a 70 6e 43 68 61 6e 67 65 20 2b 3d 20 70 50   *pnChange += pP
1883f 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 7d 0a  age->nCell;.  }.
18840 20 20 69 66 28 20 66 72 65 65 50 61 67 65 46 6c    if( freePageFl
18841 61 67 20 29 7b 0a 20 20 20 20 66 72 65 65 50 61  ag ){.    freePa
18842 67 65 28 70 50 61 67 65 2c 20 26 72 63 29 3b 0a  ge(pPage, &rc);.
18843 20 20 7d 65 6c 73 65 20 69 66 28 20 28 72 63 20    }else if( (rc 
18844 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
18845 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ite(pPage->pDbPa
18846 67 65 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 7a  ge))==0 ){.    z
18847 65 72 6f 50 61 67 65 28 70 50 61 67 65 2c 20 70  eroPage(pPage, p
18848 50 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d 20 7c  Page->aData[0] |
18849 20 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20 7d 0a   PTF_LEAF);.  }.
1884a 0a 63 6c 65 61 72 64 61 74 61 62 61 73 65 70 61  .cleardatabasepa
1884b 67 65 5f 6f 75 74 3a 0a 20 20 72 65 6c 65 61 73  ge_out:.  releas
1884c 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20  ePage(pPage);.  
1884d 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1884e 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20 69  .** Delete all i
1884f 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20  nformation from 
18850 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 69  a single table i
18851 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  n the database. 
18852 20 69 54 61 62 6c 65 20 69 73 0a 2a 2a 20 74 68   iTable is.** th
18853 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66  e page number of
18854 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65   the root of the
18855 20 74 61 62 6c 65 2e 20 20 41 66 74 65 72 20 74   table.  After t
18856 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
18857 72 6e 73 2c 0a 2a 2a 20 74 68 65 20 72 6f 6f 74  rns,.** the root
18858 20 70 61 67 65 20 69 73 20 65 6d 70 74 79 2c 20   page is empty, 
18859 62 75 74 20 73 74 69 6c 6c 20 65 78 69 73 74 73  but still exists
1885a 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
1885b 74 69 6e 65 20 77 69 6c 6c 20 66 61 69 6c 20 77  tine will fail w
1885c 69 74 68 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  ith SQLITE_LOCKE
1885d 44 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61  D if there are a
1885e 6e 79 20 6f 70 65 6e 0a 2a 2a 20 72 65 61 64 20  ny open.** read 
1885f 63 75 72 73 6f 72 73 20 6f 6e 20 74 68 65 20 74  cursors on the t
18860 61 62 6c 65 2e 20 20 4f 70 65 6e 20 77 72 69 74  able.  Open writ
18861 65 20 63 75 72 73 6f 72 73 20 61 72 65 20 6d 6f  e cursors are mo
18862 76 65 64 20 74 6f 20 74 68 65 0a 2a 2a 20 72 6f  ved to the.** ro
18863 6f 74 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e  ot of the table.
18864 0a 2a 2a 0a 2a 2a 20 49 66 20 70 6e 43 68 61 6e  .**.** If pnChan
18865 67 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20  ge is not NULL, 
18866 74 68 65 6e 20 74 61 62 6c 65 20 69 54 61 62 6c  then table iTabl
18867 65 20 6d 75 73 74 20 62 65 20 61 6e 20 69 6e 74  e must be an int
18868 6b 65 79 20 74 61 62 6c 65 2e 20 54 68 65 0a 2a  key table. The.*
18869 2a 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20  * integer value 
1886a 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 6e  pointed to by pn
1886b 43 68 61 6e 67 65 20 69 73 20 69 6e 63 72 65 6d  Change is increm
1886c 65 6e 74 65 64 20 62 79 20 74 68 65 20 6e 75 6d  ented by the num
1886d 62 65 72 20 6f 66 0a 2a 2a 20 65 6e 74 72 69 65  ber of.** entrie
1886e 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a  s in the table..
1886f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
18870 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
18871 65 65 43 6c 65 61 72 54 61 62 6c 65 28 42 74 72  eeClearTable(Btr
18872 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 61 62 6c  ee *p, int iTabl
18873 65 2c 20 69 6e 74 20 2a 70 6e 43 68 61 6e 67 65  e, int *pnChange
18874 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42  ){.  int rc;.  B
18875 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
18876 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 33  ->pBt;.  sqlite3
18877 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20  BtreeEnter(p);. 
18878 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72   assert( p->inTr
18879 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ans==TRANS_WRITE
1887a 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e 76 61 6c 69   );..  /* Invali
1887b 64 61 74 65 20 61 6c 6c 20 69 6e 63 72 62 6c 6f  date all incrblo
1887c 62 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f  b cursors open o
1887d 6e 20 74 61 62 6c 65 20 69 54 61 62 6c 65 20 28  n table iTable (
1887e 61 73 73 75 6d 69 6e 67 20 69 54 61 62 6c 65 0a  assuming iTable.
1887f 20 20 2a 2a 20 69 73 20 74 68 65 20 72 6f 6f 74    ** is the root
18880 20 6f 66 20 61 20 74 61 62 6c 65 20 62 2d 74 72   of a table b-tr
18881 65 65 20 2d 20 69 66 20 69 74 20 69 73 20 6e 6f  ee - if it is no
18882 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  t, the following
18883 20 63 61 6c 6c 20 69 73 0a 20 20 2a 2a 20 61 20   call is.  ** a 
18884 6e 6f 2d 6f 70 29 2e 20 20 2a 2f 0a 20 20 69 6e  no-op).  */.  in
18885 76 61 6c 69 64 61 74 65 49 6e 63 72 62 6c 6f 62  validateIncrblob
18886 43 75 72 73 6f 72 73 28 70 2c 20 30 2c 20 31 29  Cursors(p, 0, 1)
18887 3b 0a 0a 20 20 72 63 20 3d 20 73 61 76 65 41 6c  ;..  rc = saveAl
18888 6c 43 75 72 73 6f 72 73 28 70 42 74 2c 20 28 50  lCursors(pBt, (P
18889 67 6e 6f 29 69 54 61 62 6c 65 2c 20 30 29 3b 0a  gno)iTable, 0);.
1888a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d    if( SQLITE_OK=
1888b 3d 72 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  =rc ){.    rc = 
1888c 63 6c 65 61 72 44 61 74 61 62 61 73 65 50 61 67  clearDatabasePag
1888d 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 69 54 61  e(pBt, (Pgno)iTa
1888e 62 6c 65 2c 20 30 2c 20 70 6e 43 68 61 6e 67 65  ble, 0, pnChange
1888f 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
18890 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
18891 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
18892 2a 0a 2a 2a 20 45 72 61 73 65 20 61 6c 6c 20 69  *.** Erase all i
18893 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 61 20  nformation in a 
18894 74 61 62 6c 65 20 61 6e 64 20 61 64 64 20 74 68  table and add th
18895 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 61  e root of the ta
18896 62 6c 65 20 74 6f 0a 2a 2a 20 74 68 65 20 66 72  ble to.** the fr
18897 65 65 6c 69 73 74 2e 20 20 45 78 63 65 70 74 2c  eelist.  Except,
18898 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65   the root of the
18899 20 70 72 69 6e 63 69 70 6c 65 20 74 61 62 6c 65   principle table
1889a 20 28 74 68 65 20 6f 6e 65 20 6f 6e 0a 2a 2a 20   (the one on.** 
1889b 70 61 67 65 20 31 29 20 69 73 20 6e 65 76 65 72  page 1) is never
1889c 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 66 72   added to the fr
1889d 65 65 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  eelist..**.** Th
1889e 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  is routine will 
1889f 66 61 69 6c 20 77 69 74 68 20 53 51 4c 49 54 45  fail with SQLITE
188a0 5f 4c 4f 43 4b 45 44 20 69 66 20 74 68 65 72 65  _LOCKED if there
188a1 20 61 72 65 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a   are any open.**
188a2 20 63 75 72 73 6f 72 73 20 6f 6e 20 74 68 65 20   cursors on the 
188a3 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  table..**.** If 
188a4 41 55 54 4f 56 41 43 55 55 4d 20 69 73 20 65 6e  AUTOVACUUM is en
188a5 61 62 6c 65 64 20 61 6e 64 20 74 68 65 20 70 61  abled and the pa
188a6 67 65 20 61 74 20 69 54 61 62 6c 65 20 69 73 20  ge at iTable is 
188a7 6e 6f 74 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20  not the last.** 
188a8 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 74 68 65  root page in the
188a9 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20   database file, 
188aa 74 68 65 6e 20 74 68 65 20 6c 61 73 74 20 72 6f  then the last ro
188ab 6f 74 20 70 61 67 65 20 0a 2a 2a 20 69 6e 20 74  ot page .** in t
188ac 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
188ad 20 69 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 74   is moved into t
188ae 68 65 20 73 6c 6f 74 20 66 6f 72 6d 65 72 6c 79  he slot formerly
188af 20 6f 63 63 75 70 69 65 64 20 62 79 0a 2a 2a 20   occupied by.** 
188b0 69 54 61 62 6c 65 20 61 6e 64 20 74 68 61 74 20  iTable and that 
188b1 6c 61 73 74 20 73 6c 6f 74 20 66 6f 72 6d 65 72  last slot former
188b2 6c 79 20 6f 63 63 75 70 69 65 64 20 62 79 20 74  ly occupied by t
188b3 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67  he last root pag
188b4 65 0a 2a 2a 20 69 73 20 61 64 64 65 64 20 74 6f  e.** is added to
188b5 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 6e   the freelist in
188b6 73 74 65 61 64 20 6f 66 20 69 54 61 62 6c 65 2e  stead of iTable.
188b7 20 20 49 6e 20 74 68 69 73 20 73 61 79 2c 20 61    In this say, a
188b8 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 73  ll.** root pages
188b9 20 61 72 65 20 6b 65 70 74 20 61 74 20 74 68 65   are kept at the
188ba 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68   beginning of th
188bb 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  e database file,
188bc 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 6e 65 63   which.** is nec
188bd 65 73 73 61 72 79 20 66 6f 72 20 41 55 54 4f 56  essary for AUTOV
188be 41 43 55 55 4d 20 74 6f 20 77 6f 72 6b 20 72 69  ACUUM to work ri
188bf 67 68 74 2e 20 20 2a 70 69 4d 6f 76 65 64 20 69  ght.  *piMoved i
188c0 73 20 73 65 74 20 74 6f 20 74 68 65 20 0a 2a 2a  s set to the .**
188c1 20 70 61 67 65 20 6e 75 6d 62 65 72 20 74 68 61   page number tha
188c2 74 20 75 73 65 64 20 74 6f 20 62 65 20 74 68 65  t used to be the
188c3 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67 65 20   last root page 
188c4 69 6e 20 74 68 65 20 66 69 6c 65 20 62 65 66 6f  in the file befo
188c5 72 65 0a 2a 2a 20 74 68 65 20 6d 6f 76 65 2e 20  re.** the move. 
188c6 20 49 66 20 6e 6f 20 70 61 67 65 20 67 65 74 73   If no page gets
188c7 20 6d 6f 76 65 64 2c 20 2a 70 69 4d 6f 76 65 64   moved, *piMoved
188c8 20 69 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2a   is set to 0..**
188c9 20 54 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70   The last root p
188ca 61 67 65 20 69 73 20 72 65 63 6f 72 64 65 64 20  age is recorded 
188cb 69 6e 20 6d 65 74 61 5b 33 5d 20 61 6e 64 20 74  in meta[3] and t
188cc 68 65 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 6d  he value of.** m
188cd 65 74 61 5b 33 5d 20 69 73 20 75 70 64 61 74 65  eta[3] is update
188ce 64 20 62 79 20 74 68 69 73 20 70 72 6f 63 65 64  d by this proced
188cf 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ure..*/.static i
188d0 6e 74 20 62 74 72 65 65 44 72 6f 70 54 61 62 6c  nt btreeDropTabl
188d1 65 28 42 74 72 65 65 20 2a 70 2c 20 50 67 6e 6f  e(Btree *p, Pgno
188d2 20 69 54 61 62 6c 65 2c 20 69 6e 74 20 2a 70 69   iTable, int *pi
188d3 4d 6f 76 65 64 29 7b 0a 20 20 69 6e 74 20 72 63  Moved){.  int rc
188d4 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  ;.  MemPage *pPa
188d5 67 65 20 3d 20 30 3b 0a 20 20 42 74 53 68 61 72  ge = 0;.  BtShar
188d6 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
188d7 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
188d8 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75  ite3BtreeHoldsMu
188d9 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65  tex(p) );.  asse
188da 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d  rt( p->inTrans==
188db 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 0a  TRANS_WRITE );..
188dc 20 20 2f 2a 20 49 74 20 69 73 20 69 6c 6c 65 67    /* It is illeg
188dd 61 6c 20 74 6f 20 64 72 6f 70 20 61 20 74 61 62  al to drop a tab
188de 6c 65 20 69 66 20 61 6e 79 20 63 75 72 73 6f 72  le if any cursor
188df 73 20 61 72 65 20 6f 70 65 6e 20 6f 6e 20 74 68  s are open on th
188e0 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e  e.  ** database.
188e1 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73 65   This is because
188e2 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20   in auto-vacuum 
188e3 6d 6f 64 65 20 74 68 65 20 62 61 63 6b 65 6e 64  mode the backend
188e4 20 6d 61 79 0a 20 20 2a 2a 20 6e 65 65 64 20 74   may.  ** need t
188e5 6f 20 6d 6f 76 65 20 61 6e 6f 74 68 65 72 20 72  o move another r
188e6 6f 6f 74 2d 70 61 67 65 20 74 6f 20 66 69 6c 6c  oot-page to fill
188e7 20 61 20 67 61 70 20 6c 65 66 74 20 62 79 20 74   a gap left by t
188e8 68 65 20 64 65 6c 65 74 65 64 0a 20 20 2a 2a 20  he deleted.  ** 
188e9 72 6f 6f 74 20 70 61 67 65 2e 20 49 66 20 61 6e  root page. If an
188ea 20 6f 70 65 6e 20 63 75 72 73 6f 72 20 77 61 73   open cursor was
188eb 20 75 73 69 6e 67 20 74 68 69 73 20 70 61 67 65   using this page
188ec 20 61 20 70 72 6f 62 6c 65 6d 20 77 6f 75 6c 64   a problem would
188ed 20 0a 20 20 2a 2a 20 6f 63 63 75 72 2e 0a 20 20   .  ** occur..  
188ee 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 65 72 72  **.  ** This err
188ef 6f 72 20 69 73 20 63 61 75 67 68 74 20 6c 6f 6e  or is caught lon
188f0 67 20 62 65 66 6f 72 65 20 63 6f 6e 74 72 6f 6c  g before control
188f1 20 72 65 61 63 68 65 73 20 74 68 69 73 20 70 6f   reaches this po
188f2 69 6e 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  int..  */.  if( 
188f3 4e 45 56 45 52 28 70 42 74 2d 3e 70 43 75 72 73  NEVER(pBt->pCurs
188f4 6f 72 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  or) ){.    sqlit
188f5 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63  e3ConnectionBloc
188f6 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42 74 2d 3e  ked(p->db, pBt->
188f7 70 43 75 72 73 6f 72 2d 3e 70 42 74 72 65 65 2d  pCursor->pBtree-
188f8 3e 64 62 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  >db);.    return
188f9 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53   SQLITE_LOCKED_S
188fa 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20 7d 0a  HAREDCACHE;.  }.
188fb 0a 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74  .  rc = btreeGet
188fc 50 61 67 65 28 70 42 74 2c 20 28 50 67 6e 6f 29  Page(pBt, (Pgno)
188fd 69 54 61 62 6c 65 2c 20 26 70 50 61 67 65 2c 20  iTable, &pPage, 
188fe 30 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72  0);.  if( rc ) r
188ff 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d  eturn rc;.  rc =
18900 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65   sqlite3BtreeCle
18901 61 72 54 61 62 6c 65 28 70 2c 20 69 54 61 62 6c  arTable(p, iTabl
18902 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 20  e, 0);.  if( rc 
18903 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61  ){.    releasePa
18904 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 72  ge(pPage);.    r
18905 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20  eturn rc;.  }.. 
18906 20 2a 70 69 4d 6f 76 65 64 20 3d 20 30 3b 0a 0a   *piMoved = 0;..
18907 20 20 69 66 28 20 69 54 61 62 6c 65 3e 31 20 29    if( iTable>1 )
18908 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  {.#ifdef SQLITE_
18909 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
1890a 20 20 20 20 66 72 65 65 50 61 67 65 28 70 50 61      freePage(pPa
1890b 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 72 65  ge, &rc);.    re
1890c 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29  leasePage(pPage)
1890d 3b 0a 23 65 6c 73 65 0a 20 20 20 20 69 66 28 20  ;.#else.    if( 
1890e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20  pBt->autoVacuum 
1890f 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 6d 61  ){.      Pgno ma
18910 78 52 6f 6f 74 50 67 6e 6f 3b 0a 20 20 20 20 20  xRootPgno;.     
18911 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
18912 4d 65 74 61 28 70 2c 20 42 54 52 45 45 5f 4c 41  Meta(p, BTREE_LA
18913 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45 2c  RGEST_ROOT_PAGE,
18914 20 26 6d 61 78 52 6f 6f 74 50 67 6e 6f 29 3b 0a   &maxRootPgno);.
18915 0a 20 20 20 20 20 20 69 66 28 20 69 54 61 62 6c  .      if( iTabl
18916 65 3d 3d 6d 61 78 52 6f 6f 74 50 67 6e 6f 20 29  e==maxRootPgno )
18917 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20  {.        /* If 
18918 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20  the table being 
18919 64 72 6f 70 70 65 64 20 69 73 20 74 68 65 20 74  dropped is the t
1891a 61 62 6c 65 20 77 69 74 68 20 74 68 65 20 6c 61  able with the la
1891b 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a  rgest root-page.
1891c 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65          ** numbe
1891d 72 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  r in the databas
1891e 65 2c 20 70 75 74 20 74 68 65 20 72 6f 6f 74 20  e, put the root 
1891f 70 61 67 65 20 6f 6e 20 74 68 65 20 66 72 65 65  page on the free
18920 20 6c 69 73 74 2e 20 0a 20 20 20 20 20 20 20 20   list. .        
18921 2a 2f 0a 20 20 20 20 20 20 20 20 66 72 65 65 50  */.        freeP
18922 61 67 65 28 70 50 61 67 65 2c 20 26 72 63 29 3b  age(pPage, &rc);
18923 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65  .        release
18924 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20  Page(pPage);.   
18925 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
18926 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
18927 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
18928 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
18929 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a  else{.        /*
1892a 20 54 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   The table being
1892b 20 64 72 6f 70 70 65 64 20 64 6f 65 73 20 6e 6f   dropped does no
1892c 74 20 68 61 76 65 20 74 68 65 20 6c 61 72 67 65  t have the large
1892d 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20  st root-page.   
1892e 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 69       ** number i
1892f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  n the database. 
18930 53 6f 20 6d 6f 76 65 20 74 68 65 20 70 61 67 65  So move the page
18931 20 74 68 61 74 20 64 6f 65 73 20 69 6e 74 6f 20   that does into 
18932 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  the .        ** 
18933 67 61 70 20 6c 65 66 74 20 62 79 20 74 68 65 20  gap left by the 
18934 64 65 6c 65 74 65 64 20 72 6f 6f 74 2d 70 61 67  deleted root-pag
18935 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  e..        */.  
18936 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70        MemPage *p
18937 4d 6f 76 65 3b 0a 20 20 20 20 20 20 20 20 72 65  Move;.        re
18938 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29  leasePage(pPage)
18939 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 62  ;.        rc = b
1893a 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c  treeGetPage(pBt,
1893b 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2c 20 26 70   maxRootPgno, &p
1893c 4d 6f 76 65 2c 20 30 29 3b 0a 20 20 20 20 20 20  Move, 0);.      
1893d 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1893e 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
1893f 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
18940 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63      }.        rc
18941 20 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28   = relocatePage(
18942 70 42 74 2c 20 70 4d 6f 76 65 2c 20 50 54 52 4d  pBt, pMove, PTRM
18943 41 50 5f 52 4f 4f 54 50 41 47 45 2c 20 30 2c 20  AP_ROOTPAGE, 0, 
18944 69 54 61 62 6c 65 2c 20 30 29 3b 0a 20 20 20 20  iTable, 0);.    
18945 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
18946 70 4d 6f 76 65 29 3b 0a 20 20 20 20 20 20 20 20  pMove);.        
18947 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
18948 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  K ){.          r
18949 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
1894a 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4d 6f 76    }.        pMov
1894b 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 72  e = 0;.        r
1894c 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65  c = btreeGetPage
1894d 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50 67 6e  (pBt, maxRootPgn
1894e 6f 2c 20 26 70 4d 6f 76 65 2c 20 30 29 3b 0a 20  o, &pMove, 0);. 
1894f 20 20 20 20 20 20 20 66 72 65 65 50 61 67 65 28         freePage(
18950 70 4d 6f 76 65 2c 20 26 72 63 29 3b 0a 20 20 20  pMove, &rc);.   
18951 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
18952 28 70 4d 6f 76 65 29 3b 0a 20 20 20 20 20 20 20  (pMove);.       
18953 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
18954 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
18955 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
18956 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2a 70 69     }.        *pi
18957 4d 6f 76 65 64 20 3d 20 6d 61 78 52 6f 6f 74 50  Moved = maxRootP
18958 67 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  gno;.      }..  
18959 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6e      /* Set the n
1895a 65 77 20 27 6d 61 78 2d 72 6f 6f 74 2d 70 61 67  ew 'max-root-pag
1895b 65 27 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20  e' value in the 
1895c 64 61 74 61 62 61 73 65 20 68 65 61 64 65 72 2e  database header.
1895d 20 54 68 69 73 0a 20 20 20 20 20 20 2a 2a 20 69   This.      ** i
1895e 73 20 74 68 65 20 6f 6c 64 20 76 61 6c 75 65 20  s the old value 
1895f 6c 65 73 73 20 6f 6e 65 2c 20 6c 65 73 73 20 6f  less one, less o
18960 6e 65 20 6d 6f 72 65 20 69 66 20 74 68 61 74 20  ne more if that 
18961 68 61 70 70 65 6e 73 20 74 6f 0a 20 20 20 20 20  happens to.     
18962 20 2a 2a 20 62 65 20 61 20 72 6f 6f 74 2d 70 61   ** be a root-pa
18963 67 65 20 6e 75 6d 62 65 72 2c 20 6c 65 73 73 20  ge number, less 
18964 6f 6e 65 20 61 67 61 69 6e 20 69 66 20 74 68 61  one again if tha
18965 74 20 69 73 20 74 68 65 0a 20 20 20 20 20 20 2a  t is the.      *
18966 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  * PENDING_BYTE_P
18967 41 47 45 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  AGE..      */.  
18968 20 20 20 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d      maxRootPgno-
18969 2d 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  -;.      while( 
1896a 6d 61 78 52 6f 6f 74 50 67 6e 6f 3d 3d 50 45 4e  maxRootPgno==PEN
1896b 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
1896c 42 74 29 0a 20 20 20 20 20 20 20 20 20 20 20 20  Bt).            
1896d 20 7c 7c 20 50 54 52 4d 41 50 5f 49 53 50 41 47   || PTRMAP_ISPAG
1896e 45 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50 67  E(pBt, maxRootPg
1896f 6e 6f 29 20 29 7b 0a 20 20 20 20 20 20 20 20 6d  no) ){.        m
18970 61 78 52 6f 6f 74 50 67 6e 6f 2d 2d 3b 0a 20 20  axRootPgno--;.  
18971 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65      }.      asse
18972 72 74 28 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 21  rt( maxRootPgno!
18973 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41  =PENDING_BYTE_PA
18974 47 45 28 70 42 74 29 20 29 3b 0a 0a 20 20 20 20  GE(pBt) );..    
18975 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
18976 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 70 2c  reeUpdateMeta(p,
18977 20 34 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 29   4, maxRootPgno)
18978 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
18979 20 20 20 66 72 65 65 50 61 67 65 28 70 50 61 67     freePage(pPag
1897a 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 72  e, &rc);.      r
1897b 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65  eleasePage(pPage
1897c 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  );.    }.#endif.
1897d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
1897e 49 66 20 73 71 6c 69 74 65 33 42 74 72 65 65 44  If sqlite3BtreeD
1897f 72 6f 70 54 61 62 6c 65 20 77 61 73 20 63 61 6c  ropTable was cal
18980 6c 65 64 20 6f 6e 20 70 61 67 65 20 31 2e 0a 20  led on page 1.. 
18981 20 20 20 2a 2a 20 54 68 69 73 20 72 65 61 6c 6c     ** This reall
18982 79 20 6e 65 76 65 72 20 73 68 6f 75 6c 64 20 68  y never should h
18983 61 70 70 65 6e 20 65 78 63 65 70 74 20 69 6e 20  appen except in 
18984 61 20 63 6f 72 72 75 70 74 0a 20 20 20 20 2a 2a  a corrupt.    **
18985 20 64 61 74 61 62 61 73 65 2e 20 0a 20 20 20 20   database. .    
18986 2a 2f 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 28  */.    zeroPage(
18987 70 50 61 67 65 2c 20 50 54 46 5f 49 4e 54 4b 45  pPage, PTF_INTKE
18988 59 7c 50 54 46 5f 4c 45 41 46 20 29 3b 0a 20 20  Y|PTF_LEAF );.  
18989 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50    releasePage(pP
1898a 61 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  age);.  }.  retu
1898b 72 6e 20 72 63 3b 20 20 0a 7d 0a 53 51 4c 49 54  rn rc;  .}.SQLIT
1898c 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1898d 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 61  lite3BtreeDropTa
1898e 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e  ble(Btree *p, in
1898f 74 20 69 54 61 62 6c 65 2c 20 69 6e 74 20 2a 70  t iTable, int *p
18990 69 4d 6f 76 65 64 29 7b 0a 20 20 69 6e 74 20 72  iMoved){.  int r
18991 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  c;.  sqlite3Btre
18992 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20  eEnter(p);.  rc 
18993 3d 20 62 74 72 65 65 44 72 6f 70 54 61 62 6c 65  = btreeDropTable
18994 28 70 2c 20 69 54 61 62 6c 65 2c 20 70 69 4d 6f  (p, iTable, piMo
18995 76 65 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  ved);.  sqlite3B
18996 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20  treeLeave(p);.  
18997 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f  return rc;.}.../
18998 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
18999 6f 6e 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 63  on may only be c
1899a 61 6c 6c 65 64 20 69 66 20 74 68 65 20 62 2d 74  alled if the b-t
1899b 72 65 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61  ree connection a
1899c 6c 72 65 61 64 79 0a 2a 2a 20 68 61 73 20 61 20  lready.** has a 
1899d 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 72  read or write tr
1899e 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 20 6f  ansaction open o
1899f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a  n the database..
189a0 2a 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 6d  **.** Read the m
189a1 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  eta-information 
189a2 6f 75 74 20 6f 66 20 61 20 64 61 74 61 62 61 73  out of a databas
189a3 65 20 66 69 6c 65 2e 20 20 4d 65 74 61 5b 30 5d  e file.  Meta[0]
189a4 0a 2a 2a 20 69 73 20 74 68 65 20 6e 75 6d 62 65  .** is the numbe
189a5 72 20 6f 66 20 66 72 65 65 20 70 61 67 65 73 20  r of free pages 
189a6 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 65  currently in the
189a7 20 64 61 74 61 62 61 73 65 2e 20 20 4d 65 74 61   database.  Meta
189a8 5b 31 5d 0a 2a 2a 20 74 68 72 6f 75 67 68 20 6d  [1].** through m
189a9 65 74 61 5b 31 35 5d 20 61 72 65 20 61 76 61 69  eta[15] are avai
189aa 6c 61 62 6c 65 20 66 6f 72 20 75 73 65 20 62 79  lable for use by
189ab 20 68 69 67 68 65 72 20 6c 61 79 65 72 73 2e 20   higher layers. 
189ac 20 4d 65 74 61 5b 30 5d 0a 2a 2a 20 69 73 20 72   Meta[0].** is r
189ad 65 61 64 2d 6f 6e 6c 79 2c 20 74 68 65 20 6f 74  ead-only, the ot
189ae 68 65 72 73 20 61 72 65 20 72 65 61 64 2f 77 72  hers are read/wr
189af 69 74 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20  ite..** .** The 
189b0 73 63 68 65 6d 61 20 6c 61 79 65 72 20 6e 75 6d  schema layer num
189b1 62 65 72 73 20 6d 65 74 61 20 76 61 6c 75 65 73  bers meta values
189b2 20 64 69 66 66 65 72 65 6e 74 6c 79 2e 20 20 41   differently.  A
189b3 74 20 74 68 65 20 73 63 68 65 6d 61 0a 2a 2a 20  t the schema.** 
189b4 6c 61 79 65 72 20 28 61 6e 64 20 74 68 65 20 53  layer (and the S
189b5 65 74 43 6f 6f 6b 69 65 20 61 6e 64 20 52 65 61  etCookie and Rea
189b6 64 43 6f 6f 6b 69 65 20 6f 70 63 6f 64 65 73 29  dCookie opcodes)
189b7 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a   the number of.*
189b8 2a 20 66 72 65 65 20 70 61 67 65 73 20 69 73 20  * free pages is 
189b9 6e 6f 74 20 76 69 73 69 62 6c 65 2e 20 20 53 6f  not visible.  So
189ba 20 43 6f 6f 6b 69 65 5b 30 5d 20 69 73 20 74 68   Cookie[0] is th
189bb 65 20 73 61 6d 65 20 61 73 20 4d 65 74 61 5b 31  e same as Meta[1
189bc 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  ]..*/.SQLITE_PRI
189bd 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
189be 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 42 74  3BtreeGetMeta(Bt
189bf 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 64 78 2c  ree *p, int idx,
189c0 20 75 33 32 20 2a 70 4d 65 74 61 29 7b 0a 20 20   u32 *pMeta){.  
189c1 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
189c2 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 71 6c 69 74  p->pBt;..  sqlit
189c3 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
189c4 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e  .  assert( p->in
189c5 54 72 61 6e 73 3e 54 52 41 4e 53 5f 4e 4f 4e 45  Trans>TRANS_NONE
189c6 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 53 51   );.  assert( SQ
189c7 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65 72 79 53 68  LITE_OK==querySh
189c8 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f  aredCacheTableLo
189c9 63 6b 28 70 2c 20 4d 41 53 54 45 52 5f 52 4f 4f  ck(p, MASTER_ROO
189ca 54 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 20 29 3b  T, READ_LOCK) );
189cb 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e  .  assert( pBt->
189cc 70 50 61 67 65 31 20 29 3b 0a 20 20 61 73 73 65  pPage1 );.  asse
189cd 72 74 28 20 69 64 78 3e 3d 30 20 26 26 20 69 64  rt( idx>=0 && id
189ce 78 3c 3d 31 35 20 29 3b 0a 0a 20 20 2a 70 4d 65  x<=15 );..  *pMe
189cf 74 61 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  ta = get4byte(&p
189d0 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74  Bt->pPage1->aDat
189d1 61 5b 33 36 20 2b 20 69 64 78 2a 34 5d 29 3b 0a  a[36 + idx*4]);.
189d2 0a 20 20 2f 2a 20 49 66 20 61 75 74 6f 2d 76 61  .  /* If auto-va
189d3 63 75 75 6d 20 69 73 20 64 69 73 61 62 6c 65 64  cuum is disabled
189d4 20 69 6e 20 74 68 69 73 20 62 75 69 6c 64 20 61   in this build a
189d5 6e 64 20 74 68 69 73 20 69 73 20 61 6e 20 61 75  nd this is an au
189d6 74 6f 2d 76 61 63 75 75 6d 0a 20 20 2a 2a 20 64  to-vacuum.  ** d
189d7 61 74 61 62 61 73 65 2c 20 6d 61 72 6b 20 74 68  atabase, mark th
189d8 65 20 64 61 74 61 62 61 73 65 20 61 73 20 72 65  e database as re
189d9 61 64 2d 6f 6e 6c 79 2e 20 20 2a 2f 0a 23 69 66  ad-only.  */.#if
189da 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
189db 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 69 66 28  AUTOVACUUM.  if(
189dc 20 69 64 78 3d 3d 42 54 52 45 45 5f 4c 41 52 47   idx==BTREE_LARG
189dd 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45 20 26 26  EST_ROOT_PAGE &&
189de 20 2a 70 4d 65 74 61 3e 30 20 29 20 70 42 74 2d   *pMeta>0 ) pBt-
189df 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a 23  >readOnly = 1;.#
189e0 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33  endif..  sqlite3
189e1 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 7d  BtreeLeave(p);.}
189e2 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 6d 65  ../*.** Write me
189e3 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 62  ta-information b
189e4 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 64 61 74  ack into the dat
189e5 61 62 61 73 65 2e 20 20 4d 65 74 61 5b 30 5d 20  abase.  Meta[0] 
189e6 69 73 0a 2a 2a 20 72 65 61 64 2d 6f 6e 6c 79 20  is.** read-only 
189e7 61 6e 64 20 6d 61 79 20 6e 6f 74 20 62 65 20 77  and may not be w
189e8 72 69 74 74 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  ritten..*/.SQLIT
189e9 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
189ea 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65  lite3BtreeUpdate
189eb 4d 65 74 61 28 42 74 72 65 65 20 2a 70 2c 20 69  Meta(Btree *p, i
189ec 6e 74 20 69 64 78 2c 20 75 33 32 20 69 4d 65 74  nt idx, u32 iMet
189ed 61 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  a){.  BtShared *
189ee 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
189ef 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
189f0 50 31 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  P1;.  int rc;.  
189f1 61 73 73 65 72 74 28 20 69 64 78 3e 3d 31 20 26  assert( idx>=1 &
189f2 26 20 69 64 78 3c 3d 31 35 20 29 3b 0a 20 20 73  & idx<=15 );.  s
189f3 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
189f4 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  (p);.  assert( p
189f5 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53  ->inTrans==TRANS
189f6 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65  _WRITE );.  asse
189f7 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 21  rt( pBt->pPage1!
189f8 3d 30 20 29 3b 0a 20 20 70 50 31 20 3d 20 70 42  =0 );.  pP1 = pB
189f9 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61  t->pPage1->aData
189fa 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
189fb 50 61 67 65 72 57 72 69 74 65 28 70 42 74 2d 3e  PagerWrite(pBt->
189fc 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29  pPage1->pDbPage)
189fd 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
189fe 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 75 74  TE_OK ){.    put
189ff 34 62 79 74 65 28 26 70 50 31 5b 33 36 20 2b 20  4byte(&pP1[36 + 
18a00 69 64 78 2a 34 5d 2c 20 69 4d 65 74 61 29 3b 0a  idx*4], iMeta);.
18a01 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
18a02 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
18a03 20 20 20 69 66 28 20 69 64 78 3d 3d 42 54 52 45     if( idx==BTRE
18a04 45 5f 49 4e 43 52 5f 56 41 43 55 55 4d 20 29 7b  E_INCR_VACUUM ){
18a05 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
18a06 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 7c  Bt->autoVacuum |
18a07 7c 20 69 4d 65 74 61 3d 3d 30 20 29 3b 0a 20 20  | iMeta==0 );.  
18a08 20 20 20 20 61 73 73 65 72 74 28 20 69 4d 65 74      assert( iMet
18a09 61 3d 3d 30 20 7c 7c 20 69 4d 65 74 61 3d 3d 31  a==0 || iMeta==1
18a0a 20 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69   );.      pBt->i
18a0b 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 75 38 29  ncrVacuum = (u8)
18a0c 69 4d 65 74 61 3b 0a 20 20 20 20 7d 0a 23 65 6e  iMeta;.    }.#en
18a0d 64 69 66 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  dif.  }.  sqlite
18a0e 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
18a0f 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
18a10 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
18a11 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 0a 2f  MIT_BTREECOUNT./
18a12 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61  *.** The first a
18a13 72 67 75 6d 65 6e 74 2c 20 70 43 75 72 2c 20 69  rgument, pCur, i
18a14 73 20 61 20 63 75 72 73 6f 72 20 6f 70 65 6e 65  s a cursor opene
18a15 64 20 6f 6e 20 73 6f 6d 65 20 62 2d 74 72 65 65  d on some b-tree
18a16 2e 20 43 6f 75 6e 74 20 74 68 65 0a 2a 2a 20 6e  . Count the.** n
18a17 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
18a18 20 69 6e 20 74 68 65 20 62 2d 74 72 65 65 20 61   in the b-tree a
18a19 6e 64 20 77 72 69 74 65 20 74 68 65 20 72 65 73  nd write the res
18a1a 75 6c 74 20 74 6f 20 2a 70 6e 45 6e 74 72 79 2e  ult to *pnEntry.
18a1b 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b  .**.** SQLITE_OK
18a1c 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
18a1d 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 73  the operation is
18a1e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 65 78   successfully ex
18a1f 65 63 75 74 65 64 2e 20 0a 2a 2a 20 4f 74 68 65  ecuted. .** Othe
18a20 72 77 69 73 65 2c 20 69 66 20 61 6e 20 65 72 72  rwise, if an err
18a21 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65  or is encountere
18a22 64 20 28 69 2e 65 2e 20 61 6e 20 49 4f 20 65 72  d (i.e. an IO er
18a23 72 6f 72 20 6f 72 20 64 61 74 61 62 61 73 65 0a  ror or database.
18a24 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e 29 20 61  ** corruption) a
18a25 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  n SQLite error c
18a26 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ode is returned.
18a27 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
18a28 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
18a29 72 65 65 43 6f 75 6e 74 28 42 74 43 75 72 73 6f  reeCount(BtCurso
18a2a 72 20 2a 70 43 75 72 2c 20 69 36 34 20 2a 70 6e  r *pCur, i64 *pn
18a2b 45 6e 74 72 79 29 7b 0a 20 20 69 36 34 20 6e 45  Entry){.  i64 nE
18a2c 6e 74 72 79 20 3d 20 30 3b 20 20 20 20 20 20 20  ntry = 0;       
18a2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
18a2e 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72  * Value to retur
18a2f 6e 20 69 6e 20 2a 70 6e 45 6e 74 72 79 20 2a 2f  n in *pnEntry */
18a30 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
18a31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18a32 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
18a33 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 72 63 20 3d  n code */.  rc =
18a34 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72   moveToRoot(pCur
18a35 29 3b 0a 0a 20 20 2f 2a 20 55 6e 6c 65 73 73 20  );..  /* Unless 
18a36 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
18a37 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6c   the following l
18a38 6f 6f 70 20 72 75 6e 73 20 6f 6e 65 20 69 74 65  oop runs one ite
18a39 72 61 74 69 6f 6e 20 66 6f 72 20 65 61 63 68 0a  ration for each.
18a3a 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 65    ** page in the
18a3b 20 42 2d 54 72 65 65 20 73 74 72 75 63 74 75 72   B-Tree structur
18a3c 65 20 28 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67  e (not including
18a3d 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 29   overflow pages)
18a3e 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28  . .  */.  while(
18a3f 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
18a40 7b 0a 20 20 20 20 69 6e 74 20 69 49 64 78 3b 20  {.    int iIdx; 
18a41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18a42 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
18a43 78 20 6f 66 20 63 68 69 6c 64 20 6e 6f 64 65 20  x of child node 
18a44 69 6e 20 70 61 72 65 6e 74 20 2a 2f 0a 20 20 20  in parent */.   
18a45 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b   MemPage *pPage;
18a46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18a47 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 70      /* Current p
18a48 61 67 65 20 6f 66 20 74 68 65 20 62 2d 74 72 65  age of the b-tre
18a49 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 49 66 20  e */..    /* If 
18a4a 74 68 69 73 20 69 73 20 61 20 6c 65 61 66 20 70  this is a leaf p
18a4b 61 67 65 20 6f 72 20 74 68 65 20 74 72 65 65 20  age or the tree 
18a4c 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 2d 6b 65  is not an int-ke
18a4d 79 20 74 72 65 65 2c 20 74 68 65 6e 20 0a 20 20  y tree, then .  
18a4e 20 20 2a 2a 20 74 68 69 73 20 70 61 67 65 20 63    ** this page c
18a4f 6f 6e 74 61 69 6e 73 20 63 6f 75 6e 74 61 62 6c  ontains countabl
18a50 65 20 65 6e 74 72 69 65 73 2e 20 49 6e 63 72 65  e entries. Incre
18a51 6d 65 6e 74 20 74 68 65 20 65 6e 74 72 79 20 63  ment the entry c
18a52 6f 75 6e 74 65 72 0a 20 20 20 20 2a 2a 20 61 63  ounter.    ** ac
18a53 63 6f 72 64 69 6e 67 6c 79 2e 0a 20 20 20 20 2a  cordingly..    *
18a54 2f 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 43  /.    pPage = pC
18a55 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
18a56 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 69 66 28  >iPage];.    if(
18a57 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 7c 7c 20   pPage->leaf || 
18a58 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29  !pPage->intKey )
18a59 7b 0a 20 20 20 20 20 20 6e 45 6e 74 72 79 20 2b  {.      nEntry +
18a5a 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a  = pPage->nCell;.
18a5b 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 70 50      }..    /* pP
18a5c 61 67 65 20 69 73 20 61 20 6c 65 61 66 20 6e 6f  age is a leaf no
18a5d 64 65 2e 20 54 68 69 73 20 6c 6f 6f 70 20 6e 61  de. This loop na
18a5e 76 69 67 61 74 65 73 20 74 68 65 20 63 75 72 73  vigates the curs
18a5f 6f 72 20 73 6f 20 74 68 61 74 20 69 74 20 0a 20  or so that it . 
18a60 20 20 20 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20     ** points to 
18a61 74 68 65 20 66 69 72 73 74 20 69 6e 74 65 72 69  the first interi
18a62 6f 72 20 63 65 6c 6c 20 74 68 61 74 20 69 74 20  or cell that it 
18a63 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 70 61  points to the pa
18a64 72 65 6e 74 20 6f 66 0a 20 20 20 20 2a 2a 20 74  rent of.    ** t
18a65 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20  he next page in 
18a66 74 68 65 20 74 72 65 65 20 74 68 61 74 20 68 61  the tree that ha
18a67 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 76  s not yet been v
18a68 69 73 69 74 65 64 2e 20 54 68 65 0a 20 20 20 20  isited. The.    
18a69 2a 2a 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70  ** pCur->aiIdx[p
18a6a 43 75 72 2d 3e 69 50 61 67 65 5d 20 76 61 6c 75  Cur->iPage] valu
18a6b 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  e is set to the 
18a6c 69 6e 64 65 78 20 6f 66 20 74 68 65 20 70 61 72  index of the par
18a6d 65 6e 74 20 63 65 6c 6c 0a 20 20 20 20 2a 2a 20  ent cell.    ** 
18a6e 6f 66 20 74 68 65 20 70 61 67 65 2c 20 6f 72 20  of the page, or 
18a6f 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  to the number of
18a70 20 63 65 6c 6c 73 20 69 6e 20 74 68 65 20 70 61   cells in the pa
18a71 67 65 20 69 66 20 74 68 65 20 6e 65 78 74 20 70  ge if the next p
18a72 61 67 65 0a 20 20 20 20 2a 2a 20 74 6f 20 76 69  age.    ** to vi
18a73 73 69 74 20 69 73 20 74 68 65 20 72 69 67 68 74  sit is the right
18a74 2d 63 68 69 6c 64 20 6f 66 20 69 74 73 20 70 61  -child of its pa
18a75 72 65 6e 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  rent..    **.   
18a76 20 2a 2a 20 49 66 20 61 6c 6c 20 70 61 67 65 73   ** If all pages
18a77 20 69 6e 20 74 68 65 20 74 72 65 65 20 68 61 76   in the tree hav
18a78 65 20 62 65 65 6e 20 76 69 73 69 74 65 64 2c 20  e been visited, 
18a79 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
18a7a 20 74 6f 20 74 68 65 0a 20 20 20 20 2a 2a 20 63   to the.    ** c
18a7b 61 6c 6c 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20  aller..    */.  
18a7c 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61    if( pPage->lea
18a7d 66 20 29 7b 0a 20 20 20 20 20 20 64 6f 20 7b 0a  f ){.      do {.
18a7e 20 20 20 20 20 20 20 20 69 66 28 20 70 43 75 72          if( pCur
18a7f 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20  ->iPage==0 ){.  
18a80 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 70          /* All p
18a81 61 67 65 73 20 6f 66 20 74 68 65 20 62 2d 74 72  ages of the b-tr
18a82 65 65 20 68 61 76 65 20 62 65 65 6e 20 76 69 73  ee have been vis
18a83 69 74 65 64 2e 20 52 65 74 75 72 6e 20 73 75 63  ited. Return suc
18a84 63 65 73 73 66 75 6c 6c 79 2e 20 2a 2f 0a 20 20  cessfully. */.  
18a85 20 20 20 20 20 20 20 20 2a 70 6e 45 6e 74 72 79          *pnEntry
18a86 20 3d 20 6e 45 6e 74 72 79 3b 0a 20 20 20 20 20   = nEntry;.     
18a87 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
18a88 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 7d  TE_OK;.        }
18a89 0a 20 20 20 20 20 20 20 20 6d 6f 76 65 54 6f 50  .        moveToP
18a8a 61 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 20 20  arent(pCur);.   
18a8b 20 20 20 7d 77 68 69 6c 65 20 28 20 70 43 75 72     }while ( pCur
18a8c 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50  ->aiIdx[pCur->iP
18a8d 61 67 65 5d 3e 3d 70 43 75 72 2d 3e 61 70 50 61  age]>=pCur->apPa
18a8e 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d  ge[pCur->iPage]-
18a8f 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 20 20 20  >nCell );..     
18a90 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75   pCur->aiIdx[pCu
18a91 72 2d 3e 69 50 61 67 65 5d 2b 2b 3b 0a 20 20 20  r->iPage]++;.   
18a92 20 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d     pPage = pCur-
18a93 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
18a94 61 67 65 5d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  age];.    }..   
18a95 20 2f 2a 20 44 65 73 63 65 6e 64 20 74 6f 20 74   /* Descend to t
18a96 68 65 20 63 68 69 6c 64 20 6e 6f 64 65 20 6f 66  he child node of
18a97 20 74 68 65 20 63 65 6c 6c 20 74 68 61 74 20 74   the cell that t
18a98 68 65 20 63 75 72 73 6f 72 20 63 75 72 72 65 6e  he cursor curren
18a99 74 6c 79 20 0a 20 20 20 20 2a 2a 20 70 6f 69 6e  tly .    ** poin
18a9a 74 73 20 61 74 2e 20 54 68 69 73 20 69 73 20 74  ts at. This is t
18a9b 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 69  he right-child i
18a9c 66 20 28 69 49 64 78 3d 3d 70 50 61 67 65 2d 3e  f (iIdx==pPage->
18a9d 6e 43 65 6c 6c 29 2e 0a 20 20 20 20 2a 2f 0a 20  nCell)..    */. 
18a9e 20 20 20 69 49 64 78 20 3d 20 70 43 75 72 2d 3e     iIdx = pCur->
18a9f 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67  aiIdx[pCur->iPag
18aa0 65 5d 3b 0a 20 20 20 20 69 66 28 20 69 49 64 78  e];.    if( iIdx
18aa1 3d 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29  ==pPage->nCell )
18aa2 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76  {.      rc = mov
18aa3 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 67  eToChild(pCur, g
18aa4 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e  et4byte(&pPage->
18aa5 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72  aData[pPage->hdr
18aa6 4f 66 66 73 65 74 2b 38 5d 29 29 3b 0a 20 20 20  Offset+8]));.   
18aa7 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63   }else{.      rc
18aa8 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70   = moveToChild(p
18aa9 43 75 72 2c 20 67 65 74 34 62 79 74 65 28 66 69  Cur, get4byte(fi
18aaa 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 49  ndCell(pPage, iI
18aab 64 78 29 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  dx)));.    }.  }
18aac 0a 0a 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20  ..  /* An error 
18aad 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20 52 65  has occurred. Re
18aae 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f  turn an error co
18aaf 64 65 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20  de. */.  return 
18ab0 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  rc;.}.#endif../*
18ab1 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70  .** Return the p
18ab2 61 67 65 72 20 61 73 73 6f 63 69 61 74 65 64 20  ager associated 
18ab3 77 69 74 68 20 61 20 42 54 72 65 65 2e 20 20 54  with a BTree.  T
18ab4 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75  his routine is u
18ab5 73 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74 69  sed for.** testi
18ab6 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67  ng and debugging
18ab7 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45   only..*/.SQLITE
18ab8 5f 50 52 49 56 41 54 45 20 50 61 67 65 72 20 2a  _PRIVATE Pager *
18ab9 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65  sqlite3BtreePage
18aba 72 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 72  r(Btree *p){.  r
18abb 65 74 75 72 6e 20 70 2d 3e 70 42 74 2d 3e 70 50  eturn p->pBt->pP
18abc 61 67 65 72 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66  ager;.}..#ifndef
18abd 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54   SQLITE_OMIT_INT
18abe 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a  EGRITY_CHECK./*.
18abf 2a 2a 20 41 70 70 65 6e 64 20 61 20 6d 65 73 73  ** Append a mess
18ac0 61 67 65 20 74 6f 20 74 68 65 20 65 72 72 6f 72  age to the error
18ac1 20 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 2e   message string.
18ac2 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
18ac3 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 0a  checkAppendMsg(.
18ac4 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a 70    IntegrityCk *p
18ac5 43 68 65 63 6b 2c 0a 20 20 63 68 61 72 20 2a 7a  Check,.  char *z
18ac6 4d 73 67 31 2c 0a 20 20 63 6f 6e 73 74 20 63 68  Msg1,.  const ch
18ac7 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 0a 20 20 2e  ar *zFormat,.  .
18ac8 2e 2e 0a 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20  ...){.  va_list 
18ac9 61 70 3b 0a 20 20 69 66 28 20 21 70 43 68 65 63  ap;.  if( !pChec
18aca 6b 2d 3e 6d 78 45 72 72 20 29 20 72 65 74 75 72  k->mxErr ) retur
18acb 6e 3b 0a 20 20 70 43 68 65 63 6b 2d 3e 6d 78 45  n;.  pCheck->mxE
18acc 72 72 2d 2d 3b 0a 20 20 70 43 68 65 63 6b 2d 3e  rr--;.  pCheck->
18acd 6e 45 72 72 2b 2b 3b 0a 20 20 76 61 5f 73 74 61  nErr++;.  va_sta
18ace 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b  rt(ap, zFormat);
18acf 0a 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 65  .  if( pCheck->e
18ad0 72 72 4d 73 67 2e 6e 43 68 61 72 20 29 7b 0a 20  rrMsg.nChar ){. 
18ad1 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63     sqlite3StrAcc
18ad2 75 6d 41 70 70 65 6e 64 28 26 70 43 68 65 63 6b  umAppend(&pCheck
18ad3 2d 3e 65 72 72 4d 73 67 2c 20 22 5c 6e 22 2c 20  ->errMsg, "\n", 
18ad4 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 4d  1);.  }.  if( zM
18ad5 73 67 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  sg1 ){.    sqlit
18ad6 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64  e3StrAccumAppend
18ad7 28 26 70 43 68 65 63 6b 2d 3e 65 72 72 4d 73 67  (&pCheck->errMsg
18ad8 2c 20 7a 4d 73 67 31 2c 20 2d 31 29 3b 0a 20 20  , zMsg1, -1);.  
18ad9 7d 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69  }.  sqlite3VXPri
18ada 6e 74 66 28 26 70 43 68 65 63 6b 2d 3e 65 72 72  ntf(&pCheck->err
18adb 4d 73 67 2c 20 31 2c 20 7a 46 6f 72 6d 61 74 2c  Msg, 1, zFormat,
18adc 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61   ap);.  va_end(a
18add 70 29 3b 0a 20 20 69 66 28 20 70 43 68 65 63 6b  p);.  if( pCheck
18ade 2d 3e 65 72 72 4d 73 67 2e 6d 61 6c 6c 6f 63 46  ->errMsg.mallocF
18adf 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 43 68  ailed ){.    pCh
18ae0 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  eck->mallocFaile
18ae1 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65 6e  d = 1;.  }.}.#en
18ae2 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
18ae3 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45  IT_INTEGRITY_CHE
18ae4 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  CK */..#ifndef S
18ae5 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47  QLITE_OMIT_INTEG
18ae6 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a  RITY_CHECK./*.**
18ae7 20 41 64 64 20 31 20 74 6f 20 74 68 65 20 72 65   Add 1 to the re
18ae8 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 66 6f  ference count fo
18ae9 72 20 70 61 67 65 20 69 50 61 67 65 2e 20 20 49  r page iPage.  I
18aea 66 20 74 68 69 73 20 69 73 20 74 68 65 20 73 65  f this is the se
18aeb 63 6f 6e 64 0a 2a 2a 20 72 65 66 65 72 65 6e 63  cond.** referenc
18aec 65 20 74 6f 20 74 68 65 20 70 61 67 65 2c 20 61  e to the page, a
18aed 64 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  dd an error mess
18aee 61 67 65 20 74 6f 20 70 43 68 65 63 6b 2d 3e 7a  age to pCheck->z
18aef 45 72 72 4d 73 67 2e 0a 2a 2a 20 52 65 74 75 72  ErrMsg..** Retur
18af0 6e 20 31 20 69 66 20 74 68 65 72 65 20 61 72 65  n 1 if there are
18af1 20 32 20 6f 72 65 20 6d 6f 72 65 20 72 65 66 65   2 ore more refe
18af2 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61  rences to the pa
18af3 67 65 20 61 6e 64 20 30 20 69 66 0a 2a 2a 20 69  ge and 0 if.** i
18af4 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66 69  f this is the fi
18af5 72 73 74 20 72 65 66 65 72 65 6e 63 65 20 74 6f  rst reference to
18af6 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a   the page..**.**
18af7 20 41 6c 73 6f 20 63 68 65 63 6b 20 74 68 61 74   Also check that
18af8 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72   the page number
18af9 20 69 73 20 69 6e 20 62 6f 75 6e 64 73 2e 0a 2a   is in bounds..*
18afa 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 65  /.static int che
18afb 63 6b 52 65 66 28 49 6e 74 65 67 72 69 74 79 43  ckRef(IntegrityC
18afc 6b 20 2a 70 43 68 65 63 6b 2c 20 50 67 6e 6f 20  k *pCheck, Pgno 
18afd 69 50 61 67 65 2c 20 63 68 61 72 20 2a 7a 43 6f  iPage, char *zCo
18afe 6e 74 65 78 74 29 7b 0a 20 20 69 66 28 20 69 50  ntext){.  if( iP
18aff 61 67 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  age==0 ) return 
18b00 31 3b 0a 20 20 69 66 28 20 69 50 61 67 65 3e 70  1;.  if( iPage>p
18b01 43 68 65 63 6b 2d 3e 6e 50 61 67 65 20 29 7b 0a  Check->nPage ){.
18b02 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d      checkAppendM
18b03 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74  sg(pCheck, zCont
18b04 65 78 74 2c 20 22 69 6e 76 61 6c 69 64 20 70 61  ext, "invalid pa
18b05 67 65 20 6e 75 6d 62 65 72 20 25 64 22 2c 20 69  ge number %d", i
18b06 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75 72  Page);.    retur
18b07 6e 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  n 1;.  }.  if( p
18b08 43 68 65 63 6b 2d 3e 61 6e 52 65 66 5b 69 50 61  Check->anRef[iPa
18b09 67 65 5d 3d 3d 31 20 29 7b 0a 20 20 20 20 63 68  ge]==1 ){.    ch
18b0a 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68  eckAppendMsg(pCh
18b0b 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22  eck, zContext, "
18b0c 32 6e 64 20 72 65 66 65 72 65 6e 63 65 20 74 6f  2nd reference to
18b0d 20 70 61 67 65 20 25 64 22 2c 20 69 50 61 67 65   page %d", iPage
18b0e 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  );.    return 1;
18b0f 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 20 28  .  }.  return  (
18b10 70 43 68 65 63 6b 2d 3e 61 6e 52 65 66 5b 69 50  pCheck->anRef[iP
18b11 61 67 65 5d 2b 2b 29 3e 31 3b 0a 7d 0a 0a 23 69  age]++)>1;.}..#i
18b12 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
18b13 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a  T_AUTOVACUUM./*.
18b14 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68  ** Check that th
18b15 65 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 70  e entry in the p
18b16 6f 69 6e 74 65 72 2d 6d 61 70 20 66 6f 72 20 70  ointer-map for p
18b17 61 67 65 20 69 43 68 69 6c 64 20 6d 61 70 73 20  age iChild maps 
18b18 74 6f 20 0a 2a 2a 20 70 61 67 65 20 69 50 61 72  to .** page iPar
18b19 65 6e 74 2c 20 70 6f 69 6e 74 65 72 20 74 79 70  ent, pointer typ
18b1a 65 20 70 74 72 54 79 70 65 2e 20 49 66 20 6e 6f  e ptrType. If no
18b1b 74 2c 20 61 70 70 65 6e 64 20 61 6e 20 65 72 72  t, append an err
18b1c 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 74 6f  or message.** to
18b1d 20 70 43 68 65 63 6b 2e 0a 2a 2f 0a 73 74 61 74   pCheck..*/.stat
18b1e 69 63 20 76 6f 69 64 20 63 68 65 63 6b 50 74 72  ic void checkPtr
18b1f 6d 61 70 28 0a 20 20 49 6e 74 65 67 72 69 74 79  map(.  Integrity
18b20 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20 20 2f 2a  Ck *pCheck,   /*
18b21 20 49 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b   Integrity check
18b22 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 50 67   context */.  Pg
18b23 6e 6f 20 69 43 68 69 6c 64 2c 20 20 20 20 20 20  no iChild,      
18b24 20 20 20 20 20 2f 2a 20 43 68 69 6c 64 20 70 61       /* Child pa
18b25 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 75  ge number */.  u
18b26 38 20 65 54 79 70 65 2c 20 20 20 20 20 20 20 20  8 eType,        
18b27 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63 74 65        /* Expecte
18b28 64 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 74 79  d pointer map ty
18b29 70 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 50 61  pe */.  Pgno iPa
18b2a 72 65 6e 74 2c 20 20 20 20 20 20 20 20 20 20 2f  rent,          /
18b2b 2a 20 45 78 70 65 63 74 65 64 20 70 6f 69 6e 74  * Expected point
18b2c 65 72 20 6d 61 70 20 70 61 72 65 6e 74 20 70 61  er map parent pa
18b2d 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 63  ge number */.  c
18b2e 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74 20 20 20  har *zContext   
18b2f 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74        /* Context
18b30 20 64 65 73 63 72 69 70 74 69 6f 6e 20 28 75 73   description (us
18b31 65 64 20 66 6f 72 20 65 72 72 6f 72 20 6d 73 67  ed for error msg
18b32 29 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  ) */.){.  int rc
18b33 3b 0a 20 20 75 38 20 65 50 74 72 6d 61 70 54 79  ;.  u8 ePtrmapTy
18b34 70 65 3b 0a 20 20 50 67 6e 6f 20 69 50 74 72 6d  pe;.  Pgno iPtrm
18b35 61 70 50 61 72 65 6e 74 3b 0a 0a 20 20 72 63 20  apParent;..  rc 
18b36 3d 20 70 74 72 6d 61 70 47 65 74 28 70 43 68 65  = ptrmapGet(pChe
18b37 63 6b 2d 3e 70 42 74 2c 20 69 43 68 69 6c 64 2c  ck->pBt, iChild,
18b38 20 26 65 50 74 72 6d 61 70 54 79 70 65 2c 20 26   &ePtrmapType, &
18b39 69 50 74 72 6d 61 70 50 61 72 65 6e 74 29 3b 0a  iPtrmapParent);.
18b3a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
18b3b 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 72  _OK ){.    if( r
18b3c 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
18b3d 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f  || rc==SQLITE_IO
18b3e 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 70 43 68 65  ERR_NOMEM ) pChe
18b3f 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  ck->mallocFailed
18b40 20 3d 20 31 3b 0a 20 20 20 20 63 68 65 63 6b 41   = 1;.    checkA
18b41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c  ppendMsg(pCheck,
18b42 20 7a 43 6f 6e 74 65 78 74 2c 20 22 46 61 69 6c   zContext, "Fail
18b43 65 64 20 74 6f 20 72 65 61 64 20 70 74 72 6d 61  ed to read ptrma
18b44 70 20 6b 65 79 3d 25 64 22 2c 20 69 43 68 69 6c  p key=%d", iChil
18b45 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  d);.    return;.
18b46 20 20 7d 0a 0a 20 20 69 66 28 20 65 50 74 72 6d    }..  if( ePtrm
18b47 61 70 54 79 70 65 21 3d 65 54 79 70 65 20 7c 7c  apType!=eType ||
18b48 20 69 50 74 72 6d 61 70 50 61 72 65 6e 74 21 3d   iPtrmapParent!=
18b49 69 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 63  iParent ){.    c
18b4a 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43  heckAppendMsg(pC
18b4b 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20  heck, zContext, 
18b4c 0a 20 20 20 20 20 20 22 42 61 64 20 70 74 72 20  .      "Bad ptr 
18b4d 6d 61 70 20 65 6e 74 72 79 20 6b 65 79 3d 25 64  map entry key=%d
18b4e 20 65 78 70 65 63 74 65 64 3d 28 25 64 2c 25 64   expected=(%d,%d
18b4f 29 20 67 6f 74 3d 28 25 64 2c 25 64 29 22 2c 20  ) got=(%d,%d)", 
18b50 0a 20 20 20 20 20 20 69 43 68 69 6c 64 2c 20 65  .      iChild, e
18b51 54 79 70 65 2c 20 69 50 61 72 65 6e 74 2c 20 65  Type, iParent, e
18b52 50 74 72 6d 61 70 54 79 70 65 2c 20 69 50 74 72  PtrmapType, iPtr
18b53 6d 61 70 50 61 72 65 6e 74 29 3b 0a 20 20 7d 0a  mapParent);.  }.
18b54 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
18b55 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67 72  Check the integr
18b56 69 74 79 20 6f 66 20 74 68 65 20 66 72 65 65 6c  ity of the freel
18b57 69 73 74 20 6f 72 20 6f 66 20 61 6e 20 6f 76 65  ist or of an ove
18b58 72 66 6c 6f 77 20 70 61 67 65 20 6c 69 73 74 2e  rflow page list.
18b59 0a 2a 2a 20 56 65 72 69 66 79 20 74 68 61 74 20  .** Verify that 
18b5a 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  the number of pa
18b5b 67 65 73 20 6f 6e 20 74 68 65 20 6c 69 73 74 20  ges on the list 
18b5c 69 73 20 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  is N..*/.static 
18b5d 76 6f 69 64 20 63 68 65 63 6b 4c 69 73 74 28 0a  void checkList(.
18b5e 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a 70    IntegrityCk *p
18b5f 43 68 65 63 6b 2c 20 20 2f 2a 20 49 6e 74 65 67  Check,  /* Integ
18b60 72 69 74 79 20 63 68 65 63 6b 69 6e 67 20 63 6f  rity checking co
18b61 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69  ntext */.  int i
18b62 73 46 72 65 65 4c 69 73 74 2c 20 20 20 20 20 20  sFreeList,      
18b63 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 66   /* True for a f
18b64 72 65 65 6c 69 73 74 2e 20 20 46 61 6c 73 65 20  reelist.  False 
18b65 66 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  for overflow pag
18b66 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20  e list */.  int 
18b67 69 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20  iPage,          
18b68 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72    /* Page number
18b69 20 66 6f 72 20 66 69 72 73 74 20 70 61 67 65 20   for first page 
18b6a 69 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20  in the list */. 
18b6b 20 69 6e 74 20 4e 2c 20 20 20 20 20 20 20 20 20   int N,         
18b6c 20 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63 74         /* Expect
18b6d 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  ed number of pag
18b6e 65 73 20 69 6e 20 74 68 65 20 6c 69 73 74 20 2a  es in the list *
18b6f 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65  /.  char *zConte
18b70 78 74 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e  xt        /* Con
18b71 74 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 6d  text for error m
18b72 65 73 73 61 67 65 73 20 2a 2f 0a 29 7b 0a 20 20  essages */.){.  
18b73 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 65 78 70  int i;.  int exp
18b74 65 63 74 65 64 20 3d 20 4e 3b 0a 20 20 69 6e 74  ected = N;.  int
18b75 20 69 46 69 72 73 74 20 3d 20 69 50 61 67 65 3b   iFirst = iPage;
18b76 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 3e 20  .  while( N-- > 
18b77 30 20 26 26 20 70 43 68 65 63 6b 2d 3e 6d 78 45  0 && pCheck->mxE
18b78 72 72 20 29 7b 0a 20 20 20 20 44 62 50 61 67 65  rr ){.    DbPage
18b79 20 2a 70 4f 76 66 6c 50 61 67 65 3b 0a 20 20 20   *pOvflPage;.   
18b7a 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
18b7b 70 4f 76 66 6c 44 61 74 61 3b 0a 20 20 20 20 69  pOvflData;.    i
18b7c 66 28 20 69 50 61 67 65 3c 31 20 29 7b 0a 20 20  f( iPage<1 ){.  
18b7d 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d      checkAppendM
18b7e 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74  sg(pCheck, zCont
18b7f 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20 22 25  ext,.         "%
18b80 64 20 6f 66 20 25 64 20 70 61 67 65 73 20 6d 69  d of %d pages mi
18b81 73 73 69 6e 67 20 66 72 6f 6d 20 6f 76 65 72 66  ssing from overf
18b82 6c 6f 77 20 6c 69 73 74 20 73 74 61 72 74 69 6e  low list startin
18b83 67 20 61 74 20 25 64 22 2c 0a 20 20 20 20 20 20  g at %d",.      
18b84 20 20 20 20 4e 2b 31 2c 20 65 78 70 65 63 74 65      N+1, expecte
18b85 64 2c 20 69 46 69 72 73 74 29 3b 0a 20 20 20 20  d, iFirst);.    
18b86 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
18b87 20 20 20 69 66 28 20 63 68 65 63 6b 52 65 66 28     if( checkRef(
18b88 70 43 68 65 63 6b 2c 20 69 50 61 67 65 2c 20 7a  pCheck, iPage, z
18b89 43 6f 6e 74 65 78 74 29 20 29 20 62 72 65 61 6b  Context) ) break
18b8a 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
18b8b 33 50 61 67 65 72 47 65 74 28 70 43 68 65 63 6b  3PagerGet(pCheck
18b8c 2d 3e 70 50 61 67 65 72 2c 20 28 50 67 6e 6f 29  ->pPager, (Pgno)
18b8d 69 50 61 67 65 2c 20 26 70 4f 76 66 6c 50 61 67  iPage, &pOvflPag
18b8e 65 29 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63  e) ){.      chec
18b8f 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63  kAppendMsg(pChec
18b90 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 66 61  k, zContext, "fa
18b91 69 6c 65 64 20 74 6f 20 67 65 74 20 70 61 67 65  iled to get page
18b92 20 25 64 22 2c 20 69 50 61 67 65 29 3b 0a 20 20   %d", iPage);.  
18b93 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
18b94 0a 20 20 20 20 70 4f 76 66 6c 44 61 74 61 20 3d  .    pOvflData =
18b95 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
18b96 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  *)sqlite3PagerGe
18b97 74 44 61 74 61 28 70 4f 76 66 6c 50 61 67 65 29  tData(pOvflPage)
18b98 3b 0a 20 20 20 20 69 66 28 20 69 73 46 72 65 65  ;.    if( isFree
18b99 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 69 6e  List ){.      in
18b9a 74 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28 26  t n = get4byte(&
18b9b 70 4f 76 66 6c 44 61 74 61 5b 34 5d 29 3b 0a 23  pOvflData[4]);.#
18b9c 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
18b9d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
18b9e 20 20 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e      if( pCheck->
18b9f 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20  pBt->autoVacuum 
18ba0 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b  ){.        check
18ba1 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 69  Ptrmap(pCheck, i
18ba2 50 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52 45  Page, PTRMAP_FRE
18ba3 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74 65  EPAGE, 0, zConte
18ba4 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  xt);.      }.#en
18ba5 64 69 66 0a 20 20 20 20 20 20 69 66 28 20 6e 3e  dif.      if( n>
18ba6 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 75 73 61  pCheck->pBt->usa
18ba7 62 6c 65 53 69 7a 65 2f 34 2d 32 20 29 7b 0a 20  bleSize/4-2 ){. 
18ba8 20 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65         checkAppe
18ba9 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43  ndMsg(pCheck, zC
18baa 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20  ontext,.        
18bab 20 20 20 22 66 72 65 65 6c 69 73 74 20 6c 65 61     "freelist lea
18bac 66 20 63 6f 75 6e 74 20 74 6f 6f 20 62 69 67 20  f count too big 
18bad 6f 6e 20 70 61 67 65 20 25 64 22 2c 20 69 50 61  on page %d", iPa
18bae 67 65 29 3b 0a 20 20 20 20 20 20 20 20 4e 2d 2d  ge);.        N--
18baf 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
18bb0 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20         for(i=0; 
18bb1 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  i<n; i++){.     
18bb2 20 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65 50       Pgno iFreeP
18bb3 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 26  age = get4byte(&
18bb4 70 4f 76 66 6c 44 61 74 61 5b 38 2b 69 2a 34 5d  pOvflData[8+i*4]
18bb5 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
18bb6 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
18bb7 4d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  M.          if( 
18bb8 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 61 75 74  pCheck->pBt->aut
18bb9 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20  oVacuum ){.     
18bba 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d         checkPtrm
18bbb 61 70 28 70 43 68 65 63 6b 2c 20 69 46 72 65 65  ap(pCheck, iFree
18bbc 50 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52 45  Page, PTRMAP_FRE
18bbd 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74 65  EPAGE, 0, zConte
18bbe 78 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  xt);.          }
18bbf 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20  .#endif.        
18bc0 20 20 63 68 65 63 6b 52 65 66 28 70 43 68 65 63    checkRef(pChec
18bc1 6b 2c 20 69 46 72 65 65 50 61 67 65 2c 20 7a 43  k, iFreePage, zC
18bc2 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 20  ontext);.       
18bc3 20 7d 0a 20 20 20 20 20 20 20 20 4e 20 2d 3d 20   }.        N -= 
18bc4 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  n;.      }.    }
18bc5 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
18bc6 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
18bc7 20 20 20 20 65 6c 73 65 7b 0a 20 20 20 20 20 20      else{.      
18bc8 2f 2a 20 49 66 20 74 68 69 73 20 64 61 74 61 62  /* If this datab
18bc9 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 74  ase supports aut
18bca 6f 2d 76 61 63 75 75 6d 20 61 6e 64 20 69 50 61  o-vacuum and iPa
18bcb 67 65 20 69 73 20 6e 6f 74 20 74 68 65 20 6c 61  ge is not the la
18bcc 73 74 0a 20 20 20 20 20 20 2a 2a 20 70 61 67 65  st.      ** page
18bcd 20 69 6e 20 74 68 69 73 20 6f 76 65 72 66 6c 6f   in this overflo
18bce 77 20 6c 69 73 74 2c 20 63 68 65 63 6b 20 74 68  w list, check th
18bcf 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d  at the pointer-m
18bd0 61 70 20 65 6e 74 72 79 20 66 6f 72 0a 20 20 20  ap entry for.   
18bd1 20 20 20 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77     ** the follow
18bd2 69 6e 67 20 70 61 67 65 20 6d 61 74 63 68 65 73  ing page matches
18bd3 20 69 50 61 67 65 2e 0a 20 20 20 20 20 20 2a 2f   iPage..      */
18bd4 0a 20 20 20 20 20 20 69 66 28 20 70 43 68 65 63  .      if( pChec
18bd5 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  k->pBt->autoVacu
18bd6 75 6d 20 26 26 20 4e 3e 30 20 29 7b 0a 20 20 20  um && N>0 ){.   
18bd7 20 20 20 20 20 69 20 3d 20 67 65 74 34 62 79 74       i = get4byt
18bd8 65 28 70 4f 76 66 6c 44 61 74 61 29 3b 0a 20 20  e(pOvflData);.  
18bd9 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61        checkPtrma
18bda 70 28 70 43 68 65 63 6b 2c 20 69 2c 20 50 54 52  p(pCheck, i, PTR
18bdb 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 2c 20 69  MAP_OVERFLOW2, i
18bdc 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b  Page, zContext);
18bdd 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23  .      }.    }.#
18bde 65 6e 64 69 66 0a 20 20 20 20 69 50 61 67 65 20  endif.    iPage 
18bdf 3d 20 67 65 74 34 62 79 74 65 28 70 4f 76 66 6c  = get4byte(pOvfl
18be0 44 61 74 61 29 3b 0a 20 20 20 20 73 71 6c 69 74  Data);.    sqlit
18be1 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 4f 76  e3PagerUnref(pOv
18be2 66 6c 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 23  flPage);.  }.}.#
18be3 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
18be4 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43  OMIT_INTEGRITY_C
18be5 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  HECK */..#ifndef
18be6 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54   SQLITE_OMIT_INT
18be7 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a  EGRITY_CHECK./*.
18be8 2a 2a 20 44 6f 20 76 61 72 69 6f 75 73 20 73 61  ** Do various sa
18be9 6e 69 74 79 20 63 68 65 63 6b 73 20 6f 6e 20 61  nity checks on a
18bea 20 73 69 6e 67 6c 65 20 70 61 67 65 20 6f 66 20   single page of 
18beb 61 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e 0a  a tree.  Return.
18bec 2a 2a 20 74 68 65 20 74 72 65 65 20 64 65 70 74  ** the tree dept
18bed 68 2e 20 20 52 6f 6f 74 20 70 61 67 65 73 20 72  h.  Root pages r
18bee 65 74 75 72 6e 20 30 2e 20 20 50 61 72 65 6e 74  eturn 0.  Parent
18bef 73 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 0a  s of root pages.
18bf0 2a 2a 20 72 65 74 75 72 6e 20 31 2c 20 61 6e 64  ** return 1, and
18bf1 20 73 6f 20 66 6f 72 74 68 2e 0a 2a 2a 20 0a 2a   so forth..** .*
18bf2 2a 20 54 68 65 73 65 20 63 68 65 63 6b 73 20 61  * These checks a
18bf3 72 65 20 64 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20  re done:.**.**  
18bf4 20 20 20 20 31 2e 20 20 4d 61 6b 65 20 73 75 72      1.  Make sur
18bf5 65 20 74 68 61 74 20 63 65 6c 6c 73 20 61 6e 64  e that cells and
18bf6 20 66 72 65 65 62 6c 6f 63 6b 73 20 64 6f 20 6e   freeblocks do n
18bf7 6f 74 20 6f 76 65 72 6c 61 70 0a 2a 2a 20 20 20  ot overlap.**   
18bf8 20 20 20 20 20 20 20 62 75 74 20 63 6f 6d 62 69         but combi
18bf9 6e 65 20 74 6f 20 63 6f 6d 70 6c 65 74 65 6c 79  ne to completely
18bfa 20 63 6f 76 65 72 20 74 68 65 20 70 61 67 65 2e   cover the page.
18bfb 0a 2a 2a 20 20 4e 4f 20 20 32 2e 20 20 4d 61 6b  .**  NO  2.  Mak
18bfc 65 20 73 75 72 65 20 63 65 6c 6c 20 6b 65 79 73  e sure cell keys
18bfd 20 61 72 65 20 69 6e 20 6f 72 64 65 72 2e 0a 2a   are in order..*
18bfe 2a 20 20 4e 4f 20 20 33 2e 20 20 4d 61 6b 65 20  *  NO  3.  Make 
18bff 73 75 72 65 20 6e 6f 20 6b 65 79 20 69 73 20 6c  sure no key is l
18c00 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61  ess than or equa
18c01 6c 20 74 6f 20 7a 4c 6f 77 65 72 42 6f 75 6e 64  l to zLowerBound
18c02 2e 0a 2a 2a 20 20 4e 4f 20 20 34 2e 20 20 4d 61  ..**  NO  4.  Ma
18c03 6b 65 20 73 75 72 65 20 6e 6f 20 6b 65 79 20 69  ke sure no key i
18c04 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f  s greater than o
18c05 72 20 65 71 75 61 6c 20 74 6f 20 7a 55 70 70 65  r equal to zUppe
18c06 72 42 6f 75 6e 64 2e 0a 2a 2a 20 20 20 20 20 20  rBound..**      
18c07 35 2e 20 20 43 68 65 63 6b 20 74 68 65 20 69 6e  5.  Check the in
18c08 74 65 67 72 69 74 79 20 6f 66 20 6f 76 65 72 66  tegrity of overf
18c09 6c 6f 77 20 70 61 67 65 73 2e 0a 2a 2a 20 20 20  low pages..**   
18c0a 20 20 20 36 2e 20 20 52 65 63 75 72 73 69 76 65     6.  Recursive
18c0b 6c 79 20 63 61 6c 6c 20 63 68 65 63 6b 54 72 65  ly call checkTre
18c0c 65 50 61 67 65 20 6f 6e 20 61 6c 6c 20 63 68 69  ePage on all chi
18c0d 6c 64 72 65 6e 2e 0a 2a 2a 20 20 20 20 20 20 37  ldren..**      7
18c0e 2e 20 20 56 65 72 69 66 79 20 74 68 61 74 20 74  .  Verify that t
18c0f 68 65 20 64 65 70 74 68 20 6f 66 20 61 6c 6c 20  he depth of all 
18c10 63 68 69 6c 64 72 65 6e 20 69 73 20 74 68 65 20  children is the 
18c11 73 61 6d 65 2e 0a 2a 2a 20 20 20 20 20 20 38 2e  same..**      8.
18c12 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 69 73    Make sure this
18c13 20 70 61 67 65 20 69 73 20 61 74 20 6c 65 61 73   page is at leas
18c14 74 20 33 33 25 20 66 75 6c 6c 20 6f 72 20 65 6c  t 33% full or el
18c15 73 65 20 69 74 20 69 73 0a 2a 2a 20 20 20 20 20  se it is.**     
18c16 20 20 20 20 20 74 68 65 20 72 6f 6f 74 20 6f 66       the root of
18c17 20 74 68 65 20 74 72 65 65 2e 0a 2a 2f 0a 73 74   the tree..*/.st
18c18 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b 54 72  atic int checkTr
18c19 65 65 50 61 67 65 28 0a 20 20 49 6e 74 65 67 72  eePage(.  Integr
18c1a 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20  ityCk *pCheck,  
18c1b 2f 2a 20 43 6f 6e 74 65 78 74 20 66 6f 72 20 74  /* Context for t
18c1c 68 65 20 73 61 6e 69 74 79 20 63 68 65 63 6b 20  he sanity check 
18c1d 2a 2f 0a 20 20 69 6e 74 20 69 50 61 67 65 2c 20  */.  int iPage, 
18c1e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
18c1f 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  ge number of the
18c20 20 70 61 67 65 20 74 6f 20 63 68 65 63 6b 20 2a   page to check *
18c21 2f 0a 20 20 63 68 61 72 20 2a 7a 50 61 72 65 6e  /.  char *zParen
18c22 74 43 6f 6e 74 65 78 74 20 20 2f 2a 20 50 61 72  tContext  /* Par
18c23 65 6e 74 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 29  ent context */.)
18c24 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  {.  MemPage *pPa
18c25 67 65 3b 0a 20 20 69 6e 74 20 69 2c 20 72 63 2c  ge;.  int i, rc,
18c26 20 64 65 70 74 68 2c 20 64 32 2c 20 70 67 6e 6f   depth, d2, pgno
18c27 2c 20 63 6e 74 3b 0a 20 20 69 6e 74 20 68 64 72  , cnt;.  int hdr
18c28 2c 20 63 65 6c 6c 53 74 61 72 74 3b 0a 20 20 69  , cellStart;.  i
18c29 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20 75 38 20 2a  nt nCell;.  u8 *
18c2a 64 61 74 61 3b 0a 20 20 42 74 53 68 61 72 65 64  data;.  BtShared
18c2b 20 2a 70 42 74 3b 0a 20 20 69 6e 74 20 75 73 61   *pBt;.  int usa
18c2c 62 6c 65 53 69 7a 65 3b 0a 20 20 63 68 61 72 20  bleSize;.  char 
18c2d 7a 43 6f 6e 74 65 78 74 5b 31 30 30 5d 3b 0a 20  zContext[100];. 
18c2e 20 63 68 61 72 20 2a 68 69 74 20 3d 20 30 3b 0a   char *hit = 0;.
18c2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  .  sqlite3_snpri
18c30 6e 74 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74  ntf(sizeof(zCont
18c31 65 78 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 20  ext), zContext, 
18c32 22 50 61 67 65 20 25 64 3a 20 22 2c 20 69 50 61  "Page %d: ", iPa
18c33 67 65 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  ge);..  /* Check
18c34 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 65   that the page e
18c35 78 69 73 74 73 0a 20 20 2a 2f 0a 20 20 70 42 74  xists.  */.  pBt
18c36 20 3d 20 70 43 68 65 63 6b 2d 3e 70 42 74 3b 0a   = pCheck->pBt;.
18c37 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70    usableSize = p
18c38 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a  Bt->usableSize;.
18c39 20 20 69 66 28 20 69 50 61 67 65 3d 3d 30 20 29    if( iPage==0 )
18c3a 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28   return 0;.  if(
18c3b 20 63 68 65 63 6b 52 65 66 28 70 43 68 65 63 6b   checkRef(pCheck
18c3c 2c 20 69 50 61 67 65 2c 20 7a 50 61 72 65 6e 74  , iPage, zParent
18c3d 43 6f 6e 74 65 78 74 29 20 29 20 72 65 74 75 72  Context) ) retur
18c3e 6e 20 30 3b 0a 20 20 69 66 28 20 28 72 63 20 3d  n 0;.  if( (rc =
18c3f 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42   btreeGetPage(pB
18c40 74 2c 20 28 50 67 6e 6f 29 69 50 61 67 65 2c 20  t, (Pgno)iPage, 
18c41 26 70 50 61 67 65 2c 20 30 29 29 21 3d 30 20 29  &pPage, 0))!=0 )
18c42 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e  {.    checkAppen
18c43 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f  dMsg(pCheck, zCo
18c44 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 22 75  ntext,.       "u
18c45 6e 61 62 6c 65 20 74 6f 20 67 65 74 20 74 68 65  nable to get the
18c46 20 70 61 67 65 2e 20 65 72 72 6f 72 20 63 6f 64   page. error cod
18c47 65 3d 25 64 22 2c 20 72 63 29 3b 0a 20 20 20 20  e=%d", rc);.    
18c48 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20  return 0;.  }.. 
18c49 20 2f 2a 20 43 6c 65 61 72 20 4d 65 6d 50 61 67   /* Clear MemPag
18c4a 65 2e 69 73 49 6e 69 74 20 74 6f 20 6d 61 6b 65  e.isInit to make
18c4b 20 73 75 72 65 20 74 68 65 20 63 6f 72 72 75 70   sure the corrup
18c4c 74 69 6f 6e 20 64 65 74 65 63 74 69 6f 6e 20 63  tion detection c
18c4d 6f 64 65 20 69 6e 0a 20 20 2a 2a 20 62 74 72 65  ode in.  ** btre
18c4e 65 49 6e 69 74 50 61 67 65 28 29 20 69 73 20 65  eInitPage() is e
18c4f 78 65 63 75 74 65 64 2e 20 20 2a 2f 0a 20 20 70  xecuted.  */.  p
18c50 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30  Page->isInit = 0
18c51 3b 0a 20 20 69 66 28 20 28 72 63 20 3d 20 62 74  ;.  if( (rc = bt
18c52 72 65 65 49 6e 69 74 50 61 67 65 28 70 50 61 67  reeInitPage(pPag
18c53 65 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 61 73  e))!=0 ){.    as
18c54 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
18c55 5f 43 4f 52 52 55 50 54 20 29 3b 20 20 2f 2a 20  _CORRUPT );  /* 
18c56 54 68 65 20 6f 6e 6c 79 20 70 6f 73 73 69 62 6c  The only possibl
18c57 65 20 65 72 72 6f 72 20 66 72 6f 6d 20 49 6e 69  e error from Ini
18c58 74 50 61 67 65 20 2a 2f 0a 20 20 20 20 63 68 65  tPage */.    che
18c59 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65  ckAppendMsg(pChe
18c5a 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a 20  ck, zContext, . 
18c5b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18c5c 20 20 22 62 74 72 65 65 49 6e 69 74 50 61 67 65    "btreeInitPage
18c5d 28 29 20 72 65 74 75 72 6e 73 20 65 72 72 6f 72  () returns error
18c5e 20 63 6f 64 65 20 25 64 22 2c 20 72 63 29 3b 0a   code %d", rc);.
18c5f 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
18c60 70 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75  pPage);.    retu
18c61 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  rn 0;.  }..  /* 
18c62 43 68 65 63 6b 20 6f 75 74 20 61 6c 6c 20 74 68  Check out all th
18c63 65 20 63 65 6c 6c 73 2e 0a 20 20 2a 2f 0a 20 20  e cells..  */.  
18c64 64 65 70 74 68 20 3d 20 30 3b 0a 20 20 66 6f 72  depth = 0;.  for
18c65 28 69 3d 30 3b 20 69 3c 70 50 61 67 65 2d 3e 6e  (i=0; i<pPage->n
18c66 43 65 6c 6c 20 26 26 20 70 43 68 65 63 6b 2d 3e  Cell && pCheck->
18c67 6d 78 45 72 72 3b 20 69 2b 2b 29 7b 0a 20 20 20  mxErr; i++){.   
18c68 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 20 20   u8 *pCell;.    
18c69 75 33 32 20 73 7a 3b 0a 20 20 20 20 43 65 6c 6c  u32 sz;.    Cell
18c6a 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20 20 20  Info info;..    
18c6b 2f 2a 20 43 68 65 63 6b 20 70 61 79 6c 6f 61 64  /* Check payload
18c6c 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 0a   overflow pages.
18c6d 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74      */.    sqlit
18c6e 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65  e3_snprintf(size
18c6f 6f 66 28 7a 43 6f 6e 74 65 78 74 29 2c 20 7a 43  of(zContext), zC
18c70 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20  ontext,.        
18c71 20 20 20 20 20 22 4f 6e 20 74 72 65 65 20 70 61       "On tree pa
18c72 67 65 20 25 64 20 63 65 6c 6c 20 25 64 3a 20 22  ge %d cell %d: "
18c73 2c 20 69 50 61 67 65 2c 20 69 29 3b 0a 20 20 20  , iPage, i);.   
18c74 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c   pCell = findCel
18c75 6c 28 70 50 61 67 65 2c 69 29 3b 0a 20 20 20 20  l(pPage,i);.    
18c76 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74  btreeParseCellPt
18c77 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20  r(pPage, pCell, 
18c78 26 69 6e 66 6f 29 3b 0a 20 20 20 20 73 7a 20 3d  &info);.    sz =
18c79 20 69 6e 66 6f 2e 6e 44 61 74 61 3b 0a 20 20 20   info.nData;.   
18c7a 20 69 66 28 20 21 70 50 61 67 65 2d 3e 69 6e 74   if( !pPage->int
18c7b 4b 65 79 20 29 20 73 7a 20 2b 3d 20 28 69 6e 74  Key ) sz += (int
18c7c 29 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 20 20  )info.nKey;.    
18c7d 61 73 73 65 72 74 28 20 73 7a 3d 3d 69 6e 66 6f  assert( sz==info
18c7e 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a 20 20 20  .nPayload );.   
18c7f 20 69 66 28 20 28 73 7a 3e 69 6e 66 6f 2e 6e 4c   if( (sz>info.nL
18c80 6f 63 61 6c 29 20 0a 20 20 20 20 20 26 26 20 28  ocal) .     && (
18c81 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65  &pCell[info.iOve
18c82 72 66 6c 6f 77 5d 3c 3d 26 70 50 61 67 65 2d 3e  rflow]<=&pPage->
18c83 61 44 61 74 61 5b 70 42 74 2d 3e 75 73 61 62 6c  aData[pBt->usabl
18c84 65 53 69 7a 65 5d 29 0a 20 20 20 20 29 7b 0a 20  eSize]).    ){. 
18c85 20 20 20 20 20 69 6e 74 20 6e 50 61 67 65 20 3d       int nPage =
18c86 20 28 73 7a 20 2d 20 69 6e 66 6f 2e 6e 4c 6f 63   (sz - info.nLoc
18c87 61 6c 20 2b 20 75 73 61 62 6c 65 53 69 7a 65 20  al + usableSize 
18c88 2d 20 35 29 2f 28 75 73 61 62 6c 65 53 69 7a 65  - 5)/(usableSize
18c89 20 2d 20 34 29 3b 0a 20 20 20 20 20 20 50 67 6e   - 4);.      Pgn
18c8a 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d 20 67 65 74  o pgnoOvfl = get
18c8b 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66  4byte(&pCell[inf
18c8c 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 23  o.iOverflow]);.#
18c8d 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
18c8e 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
18c8f 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74      if( pBt->aut
18c90 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20  oVacuum ){.     
18c91 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 70     checkPtrmap(p
18c92 43 68 65 63 6b 2c 20 70 67 6e 6f 4f 76 66 6c 2c  Check, pgnoOvfl,
18c93 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57   PTRMAP_OVERFLOW
18c94 31 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65  1, iPage, zConte
18c95 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  xt);.      }.#en
18c96 64 69 66 0a 20 20 20 20 20 20 63 68 65 63 6b 4c  dif.      checkL
18c97 69 73 74 28 70 43 68 65 63 6b 2c 20 30 2c 20 70  ist(pCheck, 0, p
18c98 67 6e 6f 4f 76 66 6c 2c 20 6e 50 61 67 65 2c 20  gnoOvfl, nPage, 
18c99 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 7d  zContext);.    }
18c9a 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 73  ..    /* Check s
18c9b 61 6e 69 74 79 20 6f 66 20 6c 65 66 74 20 63 68  anity of left ch
18c9c 69 6c 64 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f  ild page..    */
18c9d 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d  .    if( !pPage-
18c9e 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 70  >leaf ){.      p
18c9f 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 70  gno = get4byte(p
18ca0 43 65 6c 6c 29 3b 0a 23 69 66 6e 64 65 66 20 53  Cell);.#ifndef S
18ca1 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
18ca2 41 43 55 55 4d 0a 20 20 20 20 20 20 69 66 28 20  ACUUM.      if( 
18ca3 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20  pBt->autoVacuum 
18ca4 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b  ){.        check
18ca5 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 70  Ptrmap(pCheck, p
18ca6 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45  gno, PTRMAP_BTRE
18ca7 45 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65  E, iPage, zConte
18ca8 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  xt);.      }.#en
18ca9 64 69 66 0a 20 20 20 20 20 20 64 32 20 3d 20 63  dif.      d2 = c
18caa 68 65 63 6b 54 72 65 65 50 61 67 65 28 70 43 68  heckTreePage(pCh
18cab 65 63 6b 2c 20 70 67 6e 6f 2c 20 7a 43 6f 6e 74  eck, pgno, zCont
18cac 65 78 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ext);.      if( 
18cad 69 3e 30 20 26 26 20 64 32 21 3d 64 65 70 74 68  i>0 && d2!=depth
18cae 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63   ){.        chec
18caf 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63  kAppendMsg(pChec
18cb0 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 43 68  k, zContext, "Ch
18cb1 69 6c 64 20 70 61 67 65 20 64 65 70 74 68 20 64  ild page depth d
18cb2 69 66 66 65 72 73 22 29 3b 0a 20 20 20 20 20 20  iffers");.      
18cb3 7d 0a 20 20 20 20 20 20 64 65 70 74 68 20 3d 20  }.      depth = 
18cb4 64 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  d2;.    }.  }.  
18cb5 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66  if( !pPage->leaf
18cb6 20 29 7b 0a 20 20 20 20 70 67 6e 6f 20 3d 20 67   ){.    pgno = g
18cb7 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e  et4byte(&pPage->
18cb8 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72  aData[pPage->hdr
18cb9 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20  Offset+8]);.    
18cba 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
18cbb 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 78 74  (sizeof(zContext
18cbc 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a 20 20  ), zContext, .  
18cbd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18cbe 20 20 20 22 4f 6e 20 70 61 67 65 20 25 64 20 61     "On page %d a
18cbf 74 20 72 69 67 68 74 20 63 68 69 6c 64 3a 20 22  t right child: "
18cc0 2c 20 69 50 61 67 65 29 3b 0a 23 69 66 6e 64 65  , iPage);.#ifnde
18cc1 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
18cc2 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28  TOVACUUM.    if(
18cc3 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d   pBt->autoVacuum
18cc4 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 50   ){.      checkP
18cc5 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 70 67  trmap(pCheck, pg
18cc6 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45  no, PTRMAP_BTREE
18cc7 2c 20 69 50 61 67 65 2c 20 30 29 3b 0a 20 20 20  , iPage, 0);.   
18cc8 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 68   }.#endif.    ch
18cc9 65 63 6b 54 72 65 65 50 61 67 65 28 70 43 68 65  eckTreePage(pChe
18cca 63 6b 2c 20 70 67 6e 6f 2c 20 7a 43 6f 6e 74 65  ck, pgno, zConte
18ccb 78 74 29 3b 0a 20 20 7d 0a 20 0a 20 20 2f 2a 20  xt);.  }. .  /* 
18ccc 43 68 65 63 6b 20 66 6f 72 20 63 6f 6d 70 6c 65  Check for comple
18ccd 74 65 20 63 6f 76 65 72 61 67 65 20 6f 66 20 74  te coverage of t
18cce 68 65 20 70 61 67 65 0a 20 20 2a 2f 0a 20 20 64  he page.  */.  d
18ccf 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61  ata = pPage->aDa
18cd0 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50 61 67  ta;.  hdr = pPag
18cd1 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20  e->hdrOffset;.  
18cd2 68 69 74 20 3d 20 73 71 6c 69 74 65 33 50 61 67  hit = sqlite3Pag
18cd3 65 4d 61 6c 6c 6f 63 28 20 70 42 74 2d 3e 70 61  eMalloc( pBt->pa
18cd4 67 65 53 69 7a 65 20 29 3b 0a 20 20 69 66 28 20  geSize );.  if( 
18cd5 68 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 43  hit==0 ){.    pC
18cd6 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  heck->mallocFail
18cd7 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b  ed = 1;.  }else{
18cd8 0a 20 20 20 20 75 31 36 20 63 6f 6e 74 65 6e 74  .    u16 content
18cd9 4f 66 66 73 65 74 20 3d 20 67 65 74 32 62 79 74  Offset = get2byt
18cda 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b  e(&data[hdr+5]);
18cdb 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 6f 6e  .    assert( con
18cdc 74 65 6e 74 4f 66 66 73 65 74 3c 3d 75 73 61 62  tentOffset<=usab
18cdd 6c 65 53 69 7a 65 20 29 3b 20 20 2f 2a 20 45 6e  leSize );  /* En
18cde 66 6f 72 63 65 64 20 62 79 20 62 74 72 65 65 49  forced by btreeI
18cdf 6e 69 74 50 61 67 65 28 29 20 2a 2f 0a 20 20 20  nitPage() */.   
18ce0 20 6d 65 6d 73 65 74 28 68 69 74 2b 63 6f 6e 74   memset(hit+cont
18ce1 65 6e 74 4f 66 66 73 65 74 2c 20 30 2c 20 75 73  entOffset, 0, us
18ce2 61 62 6c 65 53 69 7a 65 2d 63 6f 6e 74 65 6e 74  ableSize-content
18ce3 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 6d 65 6d  Offset);.    mem
18ce4 73 65 74 28 68 69 74 2c 20 31 2c 20 63 6f 6e 74  set(hit, 1, cont
18ce5 65 6e 74 4f 66 66 73 65 74 29 3b 0a 20 20 20 20  entOffset);.    
18ce6 6e 43 65 6c 6c 20 3d 20 67 65 74 32 62 79 74 65  nCell = get2byte
18ce7 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 29 3b 0a  (&data[hdr+3]);.
18ce8 20 20 20 20 63 65 6c 6c 53 74 61 72 74 20 3d 20      cellStart = 
18ce9 68 64 72 20 2b 20 31 32 20 2d 20 34 2a 70 50 61  hdr + 12 - 4*pPa
18cea 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 20 20 66 6f  ge->leaf;.    fo
18ceb 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20  r(i=0; i<nCell; 
18cec 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  i++){.      int 
18ced 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 26 64  pc = get2byte(&d
18cee 61 74 61 5b 63 65 6c 6c 53 74 61 72 74 2b 69 2a  ata[cellStart+i*
18cef 32 5d 29 3b 0a 20 20 20 20 20 20 75 31 36 20 73  2]);.      u16 s
18cf0 69 7a 65 20 3d 20 31 30 32 34 3b 0a 20 20 20 20  ize = 1024;.    
18cf1 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 69    int j;.      i
18cf2 66 28 20 70 63 3c 3d 75 73 61 62 6c 65 53 69 7a  f( pc<=usableSiz
18cf3 65 2d 34 20 29 7b 0a 20 20 20 20 20 20 20 20 73  e-4 ){.        s
18cf4 69 7a 65 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74  ize = cellSizePt
18cf5 72 28 70 50 61 67 65 2c 20 26 64 61 74 61 5b 70  r(pPage, &data[p
18cf6 63 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  c]);.      }.   
18cf7 20 20 20 69 66 28 20 28 70 63 2b 73 69 7a 65 2d     if( (pc+size-
18cf8 31 29 3e 3d 75 73 61 62 6c 65 53 69 7a 65 20 29  1)>=usableSize )
18cf9 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 41  {.        checkA
18cfa 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c  ppendMsg(pCheck,
18cfb 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20   0, .           
18cfc 20 22 43 6f 72 72 75 70 74 69 6f 6e 20 64 65 74   "Corruption det
18cfd 65 63 74 65 64 20 69 6e 20 63 65 6c 6c 20 25 64  ected in cell %d
18cfe 20 6f 6e 20 70 61 67 65 20 25 64 22 2c 69 2c 69   on page %d",i,i
18cff 50 61 67 65 2c 30 29 3b 0a 20 20 20 20 20 20 7d  Page,0);.      }
18d00 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 66 6f  else{.        fo
18d01 72 28 6a 3d 70 63 2b 73 69 7a 65 2d 31 3b 20 6a  r(j=pc+size-1; j
18d02 3e 3d 70 63 3b 20 6a 2d 2d 29 20 68 69 74 5b 6a  >=pc; j--) hit[j
18d03 5d 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ]++;.      }.   
18d04 20 7d 0a 20 20 20 20 69 20 3d 20 67 65 74 32 62   }.    i = get2b
18d05 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 31 5d  yte(&data[hdr+1]
18d06 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 69 3e  );.    while( i>
18d07 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 73  0 ){.      int s
18d08 69 7a 65 2c 20 6a 3b 0a 20 20 20 20 20 20 61 73  ize, j;.      as
18d09 73 65 72 74 28 20 69 3c 3d 75 73 61 62 6c 65 53  sert( i<=usableS
18d0a 69 7a 65 2d 34 20 29 3b 20 20 20 20 20 2f 2a 20  ize-4 );     /* 
18d0b 45 6e 66 6f 72 63 65 64 20 62 79 20 62 74 72 65  Enforced by btre
18d0c 65 49 6e 69 74 50 61 67 65 28 29 20 2a 2f 0a 20  eInitPage() */. 
18d0d 20 20 20 20 20 73 69 7a 65 20 3d 20 67 65 74 32       size = get2
18d0e 62 79 74 65 28 26 64 61 74 61 5b 69 2b 32 5d 29  byte(&data[i+2])
18d0f 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
18d10 69 2b 73 69 7a 65 3c 3d 75 73 61 62 6c 65 53 69  i+size<=usableSi
18d11 7a 65 20 29 3b 20 20 2f 2a 20 45 6e 66 6f 72 63  ze );  /* Enforc
18d12 65 64 20 62 79 20 62 74 72 65 65 49 6e 69 74 50  ed by btreeInitP
18d13 61 67 65 28 29 20 2a 2f 0a 20 20 20 20 20 20 66  age() */.      f
18d14 6f 72 28 6a 3d 69 2b 73 69 7a 65 2d 31 3b 20 6a  or(j=i+size-1; j
18d15 3e 3d 69 3b 20 6a 2d 2d 29 20 68 69 74 5b 6a 5d  >=i; j--) hit[j]
18d16 2b 2b 3b 0a 20 20 20 20 20 20 6a 20 3d 20 67 65  ++;.      j = ge
18d17 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 5d 29  t2byte(&data[i])
18d18 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
18d19 6a 3d 3d 30 20 7c 7c 20 6a 3e 69 2b 73 69 7a 65  j==0 || j>i+size
18d1a 20 29 3b 20 20 2f 2a 20 45 6e 66 6f 72 63 65 64   );  /* Enforced
18d1b 20 62 79 20 62 74 72 65 65 49 6e 69 74 50 61 67   by btreeInitPag
18d1c 65 28 29 20 2a 2f 0a 20 20 20 20 20 20 61 73 73  e() */.      ass
18d1d 65 72 74 28 20 6a 3c 3d 75 73 61 62 6c 65 53 69  ert( j<=usableSi
18d1e 7a 65 2d 34 20 29 3b 20 20 20 2f 2a 20 45 6e 66  ze-4 );   /* Enf
18d1f 6f 72 63 65 64 20 62 79 20 62 74 72 65 65 49 6e  orced by btreeIn
18d20 69 74 50 61 67 65 28 29 20 2a 2f 0a 20 20 20 20  itPage() */.    
18d21 20 20 69 20 3d 20 6a 3b 0a 20 20 20 20 7d 0a 20    i = j;.    }. 
18d22 20 20 20 66 6f 72 28 69 3d 63 6e 74 3d 30 3b 20     for(i=cnt=0; 
18d23 69 3c 75 73 61 62 6c 65 53 69 7a 65 3b 20 69 2b  i<usableSize; i+
18d24 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 68 69  +){.      if( hi
18d25 74 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20  t[i]==0 ){.     
18d26 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20     cnt++;.      
18d27 7d 65 6c 73 65 20 69 66 28 20 68 69 74 5b 69 5d  }else if( hit[i]
18d28 3e 31 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68  >1 ){.        ch
18d29 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68  eckAppendMsg(pCh
18d2a 65 63 6b 2c 20 30 2c 0a 20 20 20 20 20 20 20 20  eck, 0,.        
18d2b 20 20 22 4d 75 6c 74 69 70 6c 65 20 75 73 65 73    "Multiple uses
18d2c 20 66 6f 72 20 62 79 74 65 20 25 64 20 6f 66 20   for byte %d of 
18d2d 70 61 67 65 20 25 64 22 2c 20 69 2c 20 69 50 61  page %d", i, iPa
18d2e 67 65 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65  ge);.        bre
18d2f 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
18d30 7d 0a 20 20 20 20 69 66 28 20 63 6e 74 21 3d 64  }.    if( cnt!=d
18d31 61 74 61 5b 68 64 72 2b 37 5d 20 29 7b 0a 20 20  ata[hdr+7] ){.  
18d32 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d      checkAppendM
18d33 73 67 28 70 43 68 65 63 6b 2c 20 30 2c 20 0a 20  sg(pCheck, 0, . 
18d34 20 20 20 20 20 20 20 20 20 22 46 72 61 67 6d 65           "Fragme
18d35 6e 74 61 74 69 6f 6e 20 6f 66 20 25 64 20 62 79  ntation of %d by
18d36 74 65 73 20 72 65 70 6f 72 74 65 64 20 61 73 20  tes reported as 
18d37 25 64 20 6f 6e 20 70 61 67 65 20 25 64 22 2c 0a  %d on page %d",.
18d38 20 20 20 20 20 20 20 20 20 20 63 6e 74 2c 20 64            cnt, d
18d39 61 74 61 5b 68 64 72 2b 37 5d 2c 20 69 50 61 67  ata[hdr+7], iPag
18d3a 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  e);.    }.  }.  
18d3b 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28  sqlite3PageFree(
18d3c 68 69 74 29 3b 0a 20 20 72 65 6c 65 61 73 65 50  hit);.  releaseP
18d3d 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 72 65  age(pPage);.  re
18d3e 74 75 72 6e 20 64 65 70 74 68 2b 31 3b 0a 7d 0a  turn depth+1;.}.
18d3f 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
18d40 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f  _OMIT_INTEGRITY_
18d41 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65  CHECK */..#ifnde
18d42 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  f SQLITE_OMIT_IN
18d43 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a  TEGRITY_CHECK./*
18d44 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
18d45 20 64 6f 65 73 20 61 20 63 6f 6d 70 6c 65 74 65   does a complete
18d46 20 63 68 65 63 6b 20 6f 66 20 74 68 65 20 67 69   check of the gi
18d47 76 65 6e 20 42 54 72 65 65 20 66 69 6c 65 2e 20  ven BTree file. 
18d48 20 61 52 6f 6f 74 5b 5d 20 69 73 0a 2a 2a 20 61   aRoot[] is.** a
18d49 6e 20 61 72 72 61 79 20 6f 66 20 70 61 67 65 73  n array of pages
18d4a 20 6e 75 6d 62 65 72 73 20 77 65 72 65 20 65 61   numbers were ea
18d4b 63 68 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69  ch page number i
18d4c 73 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20  s the root page 
18d4d 6f 66 0a 2a 2a 20 61 20 74 61 62 6c 65 2e 20 20  of.** a table.  
18d4e 6e 52 6f 6f 74 20 69 73 20 74 68 65 20 6e 75 6d  nRoot is the num
18d4f 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
18d50 6e 20 61 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 41  n aRoot..**.** A
18d51 20 72 65 61 64 2d 6f 6e 6c 79 20 6f 72 20 72 65   read-only or re
18d52 61 64 2d 77 72 69 74 65 20 74 72 61 6e 73 61 63  ad-write transac
18d53 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 6f 70 65  tion must be ope
18d54 6e 65 64 20 62 65 66 6f 72 65 20 63 61 6c 6c 69  ned before calli
18d55 6e 67 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74  ng.** this funct
18d56 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65  ion..**.** Write
18d57 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
18d58 72 72 6f 72 20 73 65 65 6e 20 69 6e 20 2a 70 6e  rror seen in *pn
18d59 45 72 72 2e 20 20 45 78 63 65 70 74 20 66 6f 72  Err.  Except for
18d5a 20 73 6f 6d 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20   some memory.** 
18d5b 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72  allocation error
18d5c 73 2c 20 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  s,  an error mes
18d5d 73 61 67 65 20 68 65 6c 64 20 69 6e 20 6d 65 6d  sage held in mem
18d5e 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ory obtained fro
18d5f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 69 73 20 72  m.** malloc is r
18d60 65 74 75 72 6e 65 64 20 69 66 20 2a 70 6e 45 72  eturned if *pnEr
18d61 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20  r is non-zero.  
18d62 49 66 20 2a 70 6e 45 72 72 3d 3d 30 20 74 68 65  If *pnErr==0 the
18d63 6e 20 4e 55 4c 4c 20 69 73 0a 2a 2a 20 72 65 74  n NULL is.** ret
18d64 75 72 6e 65 64 2e 20 20 49 66 20 61 20 6d 65 6d  urned.  If a mem
18d65 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65  ory allocation e
18d66 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 4e 55 4c  rror occurs, NUL
18d67 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  L is returned..*
18d68 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
18d69 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74   char *sqlite3Bt
18d6a 72 65 65 49 6e 74 65 67 72 69 74 79 43 68 65 63  reeIntegrityChec
18d6b 6b 28 0a 20 20 42 74 72 65 65 20 2a 70 2c 20 20  k(.  Btree *p,  
18d6c 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20     /* The btree 
18d6d 74 6f 20 62 65 20 63 68 65 63 6b 65 64 20 2a 2f  to be checked */
18d6e 0a 20 20 69 6e 74 20 2a 61 52 6f 6f 74 2c 20 20  .  int *aRoot,  
18d6f 20 2f 2a 20 41 6e 20 61 72 72 61 79 20 6f 66 20   /* An array of 
18d70 72 6f 6f 74 20 70 61 67 65 73 20 6e 75 6d 62 65  root pages numbe
18d71 72 73 20 66 6f 72 20 69 6e 64 69 76 69 64 75 61  rs for individua
18d72 6c 20 74 72 65 65 73 20 2a 2f 0a 20 20 69 6e 74  l trees */.  int
18d73 20 6e 52 6f 6f 74 2c 20 20 20 20 2f 2a 20 4e 75   nRoot,    /* Nu
18d74 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
18d75 69 6e 20 61 52 6f 6f 74 5b 5d 20 2a 2f 0a 20 20  in aRoot[] */.  
18d76 69 6e 74 20 6d 78 45 72 72 2c 20 20 20 20 2f 2a  int mxErr,    /*
18d77 20 53 74 6f 70 20 72 65 70 6f 72 74 69 6e 67 20   Stop reporting 
18d78 65 72 72 6f 72 73 20 61 66 74 65 72 20 74 68 69  errors after thi
18d79 73 20 6d 61 6e 79 20 2a 2f 0a 20 20 69 6e 74 20  s many */.  int 
18d7a 2a 70 6e 45 72 72 20 20 20 20 2f 2a 20 57 72 69  *pnErr    /* Wri
18d7b 74 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72  te number of err
18d7c 6f 72 73 20 73 65 65 6e 20 74 6f 20 74 68 69 73  ors seen to this
18d7d 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 29 7b 0a   variable */.){.
18d7e 20 20 50 67 6e 6f 20 69 3b 0a 20 20 69 6e 74 20    Pgno i;.  int 
18d7f 6e 52 65 66 3b 0a 20 20 49 6e 74 65 67 72 69 74  nRef;.  Integrit
18d80 79 43 6b 20 73 43 68 65 63 6b 3b 0a 20 20 42 74  yCk sCheck;.  Bt
18d81 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d  Shared *pBt = p-
18d82 3e 70 42 74 3b 0a 20 20 63 68 61 72 20 7a 45 72  >pBt;.  char zEr
18d83 72 5b 31 30 30 5d 3b 0a 0a 20 20 73 71 6c 69 74  r[100];..  sqlit
18d84 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
18d85 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e  .  assert( p->in
18d86 54 72 61 6e 73 3e 54 52 41 4e 53 5f 4e 4f 4e 45  Trans>TRANS_NONE
18d87 20 26 26 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73   && pBt->inTrans
18d88 61 63 74 69 6f 6e 3e 54 52 41 4e 53 5f 4e 4f 4e  action>TRANS_NON
18d89 45 20 29 3b 0a 20 20 6e 52 65 66 20 3d 20 73 71  E );.  nRef = sq
18d8a 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75  lite3PagerRefcou
18d8b 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b  nt(pBt->pPager);
18d8c 0a 20 20 73 43 68 65 63 6b 2e 70 42 74 20 3d 20  .  sCheck.pBt = 
18d8d 70 42 74 3b 0a 20 20 73 43 68 65 63 6b 2e 70 50  pBt;.  sCheck.pP
18d8e 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50 61 67  ager = pBt->pPag
18d8f 65 72 3b 0a 20 20 73 43 68 65 63 6b 2e 6e 50 61  er;.  sCheck.nPa
18d90 67 65 20 3d 20 70 61 67 65 72 50 61 67 65 63 6f  ge = pagerPageco
18d91 75 6e 74 28 73 43 68 65 63 6b 2e 70 42 74 29 3b  unt(sCheck.pBt);
18d92 0a 20 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 20  .  sCheck.mxErr 
18d93 3d 20 6d 78 45 72 72 3b 0a 20 20 73 43 68 65 63  = mxErr;.  sChec
18d94 6b 2e 6e 45 72 72 20 3d 20 30 3b 0a 20 20 73 43  k.nErr = 0;.  sC
18d95 68 65 63 6b 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65  heck.mallocFaile
18d96 64 20 3d 20 30 3b 0a 20 20 2a 70 6e 45 72 72 20  d = 0;.  *pnErr 
18d97 3d 20 30 3b 0a 20 20 69 66 28 20 73 43 68 65 63  = 0;.  if( sChec
18d98 6b 2e 6e 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20  k.nPage==0 ){.  
18d99 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
18d9a 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75  ave(p);.    retu
18d9b 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 73 43 68 65  rn 0;.  }.  sChe
18d9c 63 6b 2e 61 6e 52 65 66 20 3d 20 73 71 6c 69 74  ck.anRef = sqlit
18d9d 65 33 4d 61 6c 6c 6f 63 28 20 28 73 43 68 65 63  e3Malloc( (sChec
18d9e 6b 2e 6e 50 61 67 65 2b 31 29 2a 73 69 7a 65 6f  k.nPage+1)*sizeo
18d9f 66 28 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 30  f(sCheck.anRef[0
18da0 5d 29 20 29 3b 0a 20 20 69 66 28 20 21 73 43 68  ]) );.  if( !sCh
18da1 65 63 6b 2e 61 6e 52 65 66 20 29 7b 0a 20 20 20  eck.anRef ){.   
18da2 20 2a 70 6e 45 72 72 20 3d 20 31 3b 0a 20 20 20   *pnErr = 1;.   
18da3 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
18da4 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75 72  ve(p);.    retur
18da5 6e 20 30 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69  n 0;.  }.  for(i
18da6 3d 30 3b 20 69 3c 3d 73 43 68 65 63 6b 2e 6e 50  =0; i<=sCheck.nP
18da7 61 67 65 3b 20 69 2b 2b 29 7b 20 73 43 68 65 63  age; i++){ sChec
18da8 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d 20 30 3b 20  k.anRef[i] = 0; 
18da9 7d 0a 20 20 69 20 3d 20 50 45 4e 44 49 4e 47 5f  }.  i = PENDING_
18daa 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 3b 0a  BYTE_PAGE(pBt);.
18dab 20 20 69 66 28 20 69 3c 3d 73 43 68 65 63 6b 2e    if( i<=sCheck.
18dac 6e 50 61 67 65 20 29 7b 0a 20 20 20 20 73 43 68  nPage ){.    sCh
18dad 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d 20 31  eck.anRef[i] = 1
18dae 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 53  ;.  }.  sqlite3S
18daf 74 72 41 63 63 75 6d 49 6e 69 74 28 26 73 43 68  trAccumInit(&sCh
18db0 65 63 6b 2e 65 72 72 4d 73 67 2c 20 7a 45 72 72  eck.errMsg, zErr
18db1 2c 20 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20  , sizeof(zErr), 
18db2 32 30 30 30 30 29 3b 0a 0a 20 20 2f 2a 20 43 68  20000);..  /* Ch
18db3 65 63 6b 20 74 68 65 20 69 6e 74 65 67 72 69 74  eck the integrit
18db4 79 20 6f 66 20 74 68 65 20 66 72 65 65 6c 69 73  y of the freelis
18db5 74 0a 20 20 2a 2f 0a 20 20 63 68 65 63 6b 4c 69  t.  */.  checkLi
18db6 73 74 28 26 73 43 68 65 63 6b 2c 20 31 2c 20 67  st(&sCheck, 1, g
18db7 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50  et4byte(&pBt->pP
18db8 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29  age1->aData[32])
18db9 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 65  ,.            ge
18dba 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61  t4byte(&pBt->pPa
18dbb 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 2c  ge1->aData[36]),
18dbc 20 22 4d 61 69 6e 20 66 72 65 65 6c 69 73 74 3a   "Main freelist:
18dbd 20 22 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b   ");..  /* Check
18dbe 20 61 6c 6c 20 74 68 65 20 74 61 62 6c 65 73 2e   all the tables.
18dbf 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b  .  */.  for(i=0;
18dc0 20 28 69 6e 74 29 69 3c 6e 52 6f 6f 74 20 26 26   (int)i<nRoot &&
18dc1 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 3b 20 69   sCheck.mxErr; i
18dc2 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 52 6f  ++){.    if( aRo
18dc3 6f 74 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69  ot[i]==0 ) conti
18dc4 6e 75 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  nue;.#ifndef SQL
18dc5 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
18dc6 55 55 4d 0a 20 20 20 20 69 66 28 20 70 42 74 2d  UUM.    if( pBt-
18dc7 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 26 20 61  >autoVacuum && a
18dc8 52 6f 6f 74 5b 69 5d 3e 31 20 29 7b 0a 20 20 20  Root[i]>1 ){.   
18dc9 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 26     checkPtrmap(&
18dca 73 43 68 65 63 6b 2c 20 61 52 6f 6f 74 5b 69 5d  sCheck, aRoot[i]
18dcb 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47  , PTRMAP_ROOTPAG
18dcc 45 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  E, 0, 0);.    }.
18dcd 23 65 6e 64 69 66 0a 20 20 20 20 63 68 65 63 6b  #endif.    check
18dce 54 72 65 65 50 61 67 65 28 26 73 43 68 65 63 6b  TreePage(&sCheck
18dcf 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 22 4c 69 73  , aRoot[i], "Lis
18dd0 74 20 6f 66 20 74 72 65 65 20 72 6f 6f 74 73 3a  t of tree roots:
18dd1 20 22 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d   ");.  }..  /* M
18dd2 61 6b 65 20 73 75 72 65 20 65 76 65 72 79 20 70  ake sure every p
18dd3 61 67 65 20 69 6e 20 74 68 65 20 66 69 6c 65 20  age in the file 
18dd4 69 73 20 72 65 66 65 72 65 6e 63 65 64 0a 20 20  is referenced.  
18dd5 2a 2f 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c  */.  for(i=1; i<
18dd6 3d 73 43 68 65 63 6b 2e 6e 50 61 67 65 20 26 26  =sCheck.nPage &&
18dd7 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 3b 20 69   sCheck.mxErr; i
18dd8 2b 2b 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49  ++){.#ifdef SQLI
18dd9 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
18dda 55 4d 0a 20 20 20 20 69 66 28 20 73 43 68 65 63  UM.    if( sChec
18ddb 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 20 29 7b  k.anRef[i]==0 ){
18ddc 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65  .      checkAppe
18ddd 6e 64 4d 73 67 28 26 73 43 68 65 63 6b 2c 20 30  ndMsg(&sCheck, 0
18dde 2c 20 22 50 61 67 65 20 25 64 20 69 73 20 6e 65  , "Page %d is ne
18ddf 76 65 72 20 75 73 65 64 22 2c 20 69 29 3b 0a 20  ver used", i);. 
18de0 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 2f     }.#else.    /
18de1 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73  * If the databas
18de2 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d  e supports auto-
18de3 76 61 63 75 75 6d 2c 20 6d 61 6b 65 20 73 75 72  vacuum, make sur
18de4 65 20 6e 6f 20 74 61 62 6c 65 73 20 63 6f 6e 74  e no tables cont
18de5 61 69 6e 0a 20 20 20 20 2a 2a 20 72 65 66 65 72  ain.    ** refer
18de6 65 6e 63 65 73 20 74 6f 20 70 6f 69 6e 74 65 72  ences to pointer
18de7 2d 6d 61 70 20 70 61 67 65 73 2e 0a 20 20 20 20  -map pages..    
18de8 2a 2f 0a 20 20 20 20 69 66 28 20 73 43 68 65 63  */.    if( sChec
18de9 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 20 26 26  k.anRef[i]==0 &&
18dea 20 0a 20 20 20 20 20 20 20 28 50 54 52 4d 41 50   .       (PTRMAP
18deb 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 69 29 21  _PAGENO(pBt, i)!
18dec 3d 69 20 7c 7c 20 21 70 42 74 2d 3e 61 75 74 6f  =i || !pBt->auto
18ded 56 61 63 75 75 6d 29 20 29 7b 0a 20 20 20 20 20  Vacuum) ){.     
18dee 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28   checkAppendMsg(
18def 26 73 43 68 65 63 6b 2c 20 30 2c 20 22 50 61 67  &sCheck, 0, "Pag
18df0 65 20 25 64 20 69 73 20 6e 65 76 65 72 20 75 73  e %d is never us
18df1 65 64 22 2c 20 69 29 3b 0a 20 20 20 20 7d 0a 20  ed", i);.    }. 
18df2 20 20 20 69 66 28 20 73 43 68 65 63 6b 2e 61 6e     if( sCheck.an
18df3 52 65 66 5b 69 5d 21 3d 30 20 26 26 20 0a 20 20  Ref[i]!=0 && .  
18df4 20 20 20 20 20 28 50 54 52 4d 41 50 5f 50 41 47       (PTRMAP_PAG
18df5 45 4e 4f 28 70 42 74 2c 20 69 29 3d 3d 69 20 26  ENO(pBt, i)==i &
18df6 26 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  & pBt->autoVacuu
18df7 6d 29 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63  m) ){.      chec
18df8 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 43 68 65  kAppendMsg(&sChe
18df9 63 6b 2c 20 30 2c 20 22 50 6f 69 6e 74 65 72 20  ck, 0, "Pointer 
18dfa 6d 61 70 20 70 61 67 65 20 25 64 20 69 73 20 72  map page %d is r
18dfb 65 66 65 72 65 6e 63 65 64 22 2c 20 69 29 3b 0a  eferenced", i);.
18dfc 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d      }.#endif.  }
18dfd 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
18dfe 20 74 68 69 73 20 61 6e 61 6c 79 73 69 73 20 64   this analysis d
18dff 69 64 20 6e 6f 74 20 6c 65 61 76 65 20 61 6e 79  id not leave any
18e00 20 75 6e 72 65 66 28 29 20 70 61 67 65 73 2e 0a   unref() pages..
18e01 20 20 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20    ** This is an 
18e02 69 6e 74 65 72 6e 61 6c 20 63 6f 6e 73 69 73 74  internal consist
18e03 65 6e 63 79 20 63 68 65 63 6b 3b 20 61 6e 20 69  ency check; an i
18e04 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 0a 20  ntegrity check. 
18e05 20 2a 2a 20 6f 66 20 74 68 65 20 69 6e 74 65 67   ** of the integ
18e06 72 69 74 79 20 63 68 65 63 6b 2e 0a 20 20 2a 2f  rity check..  */
18e07 0a 20 20 69 66 28 20 4e 45 56 45 52 28 6e 52 65  .  if( NEVER(nRe
18e08 66 20 21 3d 20 73 71 6c 69 74 65 33 50 61 67 65  f != sqlite3Page
18e09 72 52 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70  rRefcount(pBt->p
18e0a 50 61 67 65 72 29 29 20 29 7b 0a 20 20 20 20 63  Pager)) ){.    c
18e0b 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 26 73  heckAppendMsg(&s
18e0c 43 68 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20 20  Check, 0, .     
18e0d 20 22 4f 75 74 73 74 61 6e 64 69 6e 67 20 70 61   "Outstanding pa
18e0e 67 65 20 63 6f 75 6e 74 20 67 6f 65 73 20 66 72  ge count goes fr
18e0f 6f 6d 20 25 64 20 74 6f 20 25 64 20 64 75 72 69  om %d to %d duri
18e10 6e 67 20 74 68 69 73 20 61 6e 61 6c 79 73 69 73  ng this analysis
18e11 22 2c 0a 20 20 20 20 20 20 6e 52 65 66 2c 20 73  ",.      nRef, s
18e12 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f  qlite3PagerRefco
18e13 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29  unt(pBt->pPager)
18e14 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f  .    );.  }..  /
18e15 2a 20 43 6c 65 61 6e 20 20 75 70 20 61 6e 64 20  * Clean  up and 
18e16 72 65 70 6f 72 74 20 65 72 72 6f 72 73 2e 0a 20  report errors.. 
18e17 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 74 72   */.  sqlite3Btr
18e18 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 73 71  eeLeave(p);.  sq
18e19 6c 69 74 65 33 5f 66 72 65 65 28 73 43 68 65 63  lite3_free(sChec
18e1a 6b 2e 61 6e 52 65 66 29 3b 0a 20 20 69 66 28 20  k.anRef);.  if( 
18e1b 73 43 68 65 63 6b 2e 6d 61 6c 6c 6f 63 46 61 69  sCheck.mallocFai
18e1c 6c 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  led ){.    sqlit
18e1d 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28  e3StrAccumReset(
18e1e 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 29 3b  &sCheck.errMsg);
18e1f 0a 20 20 20 20 2a 70 6e 45 72 72 20 3d 20 73 43  .    *pnErr = sC
18e20 68 65 63 6b 2e 6e 45 72 72 2b 31 3b 0a 20 20 20  heck.nErr+1;.   
18e21 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
18e22 20 2a 70 6e 45 72 72 20 3d 20 73 43 68 65 63 6b   *pnErr = sCheck
18e23 2e 6e 45 72 72 3b 0a 20 20 69 66 28 20 73 43 68  .nErr;.  if( sCh
18e24 65 63 6b 2e 6e 45 72 72 3d 3d 30 20 29 20 73 71  eck.nErr==0 ) sq
18e25 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 65 73  lite3StrAccumRes
18e26 65 74 28 26 73 43 68 65 63 6b 2e 65 72 72 4d 73  et(&sCheck.errMs
18e27 67 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  g);.  return sql
18e28 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69  ite3StrAccumFini
18e29 73 68 28 26 73 43 68 65 63 6b 2e 65 72 72 4d 73  sh(&sCheck.errMs
18e2a 67 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  g);.}.#endif /* 
18e2b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45  SQLITE_OMIT_INTE
18e2c 47 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a  GRITY_CHECK */..
18e2d 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
18e2e 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f   full pathname o
18e2f 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  f the underlying
18e30 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a   database file..
18e31 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20  **.** The pager 
18e32 66 69 6c 65 6e 61 6d 65 20 69 73 20 69 6e 76 61  filename is inva
18e33 72 69 61 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73  riant as long as
18e34 20 74 68 65 20 70 61 67 65 72 20 69 73 0a 2a 2a   the pager is.**
18e35 20 6f 70 65 6e 20 73 6f 20 69 74 20 69 73 20 73   open so it is s
18e36 61 66 65 20 74 6f 20 61 63 63 65 73 73 20 77 69  afe to access wi
18e37 74 68 6f 75 74 20 74 68 65 20 42 74 53 68 61 72  thout the BtShar
18e38 65 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51 4c  ed mutex..*/.SQL
18e39 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
18e3a 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42  t char *sqlite3B
18e3b 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28  treeGetFilename(
18e3c 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73  Btree *p){.  ass
18e3d 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 70 50 61  ert( p->pBt->pPa
18e3e 67 65 72 21 3d 30 20 29 3b 0a 20 20 72 65 74 75  ger!=0 );.  retu
18e3f 72 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 46  rn sqlite3PagerF
18e40 69 6c 65 6e 61 6d 65 28 70 2d 3e 70 42 74 2d 3e  ilename(p->pBt->
18e41 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  pPager);.}../*.*
18e42 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70 61 74  * Return the pat
18e43 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75  hname of the jou
18e44 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 74 68  rnal file for th
18e45 69 73 20 64 61 74 61 62 61 73 65 2e 20 54 68 65  is database. The
18e46 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65   return.** value
18e47 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   of this routine
18e48 20 69 73 20 74 68 65 20 73 61 6d 65 20 72 65 67   is the same reg
18e49 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68  ardless of wheth
18e4a 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  er the journal f
18e4b 69 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20  ile.** has been 
18e4c 63 72 65 61 74 65 64 20 6f 72 20 6e 6f 74 2e 0a  created or not..
18e4d 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20  **.** The pager 
18e4e 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65  journal filename
18e4f 20 69 73 20 69 6e 76 61 72 69 61 6e 74 20 61 73   is invariant as
18e50 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 70 61 67   long as the pag
18e51 65 72 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 73 6f  er is.** open so
18e52 20 69 74 20 69 73 20 73 61 66 65 20 74 6f 20 61   it is safe to a
18e53 63 63 65 73 73 20 77 69 74 68 6f 75 74 20 74 68  ccess without th
18e54 65 20 42 74 53 68 61 72 65 64 20 6d 75 74 65 78  e BtShared mutex
18e55 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
18e56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ATE const char *
18e57 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4a  sqlite3BtreeGetJ
18e58 6f 75 72 6e 61 6c 6e 61 6d 65 28 42 74 72 65 65  ournalname(Btree
18e59 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20   *p){.  assert( 
18e5a 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 21 3d  p->pBt->pPager!=
18e5b 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71  0 );.  return sq
18e5c 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61  lite3PagerJourna
18e5d 6c 6e 61 6d 65 28 70 2d 3e 70 42 74 2d 3e 70 50  lname(p->pBt->pP
18e5e 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ager);.}../*.** 
18e5f 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20  Return non-zero 
18e60 69 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  if a transaction
18e61 20 69 73 20 61 63 74 69 76 65 2e 0a 2a 2f 0a 53   is active..*/.S
18e62 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
18e63 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73  t sqlite3BtreeIs
18e64 49 6e 54 72 61 6e 73 28 42 74 72 65 65 20 2a 70  InTrans(Btree *p
18e65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 3d 3d  ){.  assert( p==
18e66 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74  0 || sqlite3_mut
18e67 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d  ex_held(p->db->m
18e68 75 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72  utex) );.  retur
18e69 6e 20 28 70 20 26 26 20 28 70 2d 3e 69 6e 54 72  n (p && (p->inTr
18e6a 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ans==TRANS_WRITE
18e6b 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  ));.}../*.** Ret
18e6c 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20  urn non-zero if 
18e6d 61 20 72 65 61 64 20 28 6f 72 20 77 72 69 74 65  a read (or write
18e6e 29 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  ) transaction is
18e6f 20 61 63 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49   active..*/.SQLI
18e70 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
18e71 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 52  qlite3BtreeIsInR
18e72 65 61 64 54 72 61 6e 73 28 42 74 72 65 65 20 2a  eadTrans(Btree *
18e73 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20  p){.  assert( p 
18e74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
18e75 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
18e76 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  p->db->mutex) );
18e77 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 69 6e 54  .  return p->inT
18e78 72 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45  rans!=TRANS_NONE
18e79 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  ;.}..SQLITE_PRIV
18e7a 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
18e7b 74 72 65 65 49 73 49 6e 42 61 63 6b 75 70 28 42  treeIsInBackup(B
18e7c 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65  tree *p){.  asse
18e7d 72 74 28 20 70 20 29 3b 0a 20 20 61 73 73 65 72  rt( p );.  asser
18e7e 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
18e7f 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74  _held(p->db->mut
18e80 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  ex) );.  return 
18e81 70 2d 3e 6e 42 61 63 6b 75 70 21 3d 30 3b 0a 7d  p->nBackup!=0;.}
18e82 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
18e83 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 20  ction returns a 
18e84 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 62 6c 6f  pointer to a blo
18e85 62 20 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f  b of memory asso
18e86 63 69 61 74 65 64 20 77 69 74 68 0a 2a 2a 20 61  ciated with.** a
18e87 20 73 69 6e 67 6c 65 20 73 68 61 72 65 64 2d 62   single shared-b
18e88 74 72 65 65 2e 20 54 68 65 20 6d 65 6d 6f 72 79  tree. The memory
18e89 20 69 73 20 75 73 65 64 20 62 79 20 63 6c 69 65   is used by clie
18e8a 6e 74 20 63 6f 64 65 20 66 6f 72 20 69 74 73 20  nt code for its 
18e8b 6f 77 6e 0a 2a 2a 20 70 75 72 70 6f 73 65 73 20  own.** purposes 
18e8c 28 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 6f  (for example, to
18e8d 20 73 74 6f 72 65 20 61 20 68 69 67 68 2d 6c 65   store a high-le
18e8e 76 65 6c 20 73 63 68 65 6d 61 20 61 73 73 6f 63  vel schema assoc
18e8f 69 61 74 65 64 20 77 69 74 68 20 0a 2a 2a 20 74  iated with .** t
18e90 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 29  he shared-btree)
18e91 2e 20 54 68 65 20 62 74 72 65 65 20 6c 61 79 65  . The btree laye
18e92 72 20 6d 61 6e 61 67 65 73 20 72 65 66 65 72 65  r manages refere
18e93 6e 63 65 20 63 6f 75 6e 74 69 6e 67 20 69 73 73  nce counting iss
18e94 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  ues..**.** The f
18e95 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 69  irst time this i
18e96 73 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 73 68  s called on a sh
18e97 61 72 65 64 2d 62 74 72 65 65 2c 20 6e 42 79 74  ared-btree, nByt
18e98 65 73 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f  es bytes of memo
18e99 72 79 0a 2a 2a 20 61 72 65 20 61 6c 6c 6f 63 61  ry.** are alloca
18e9a 74 65 64 2c 20 7a 65 72 6f 65 64 2c 20 61 6e 64  ted, zeroed, and
18e9b 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65   returned to the
18e9c 20 63 61 6c 6c 65 72 2e 20 46 6f 72 20 65 61 63   caller. For eac
18e9d 68 20 73 75 62 73 65 71 75 65 6e 74 20 0a 2a 2a  h subsequent .**
18e9e 20 63 61 6c 6c 20 74 68 65 20 6e 42 79 74 65 73   call the nBytes
18e9f 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 69 67   parameter is ig
18ea0 6e 6f 72 65 64 20 61 6e 64 20 61 20 70 6f 69 6e  nored and a poin
18ea1 74 65 72 20 74 6f 20 74 68 65 20 73 61 6d 65 20  ter to the same 
18ea2 62 6c 6f 62 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72  blob.** of memor
18ea3 79 20 72 65 74 75 72 6e 65 64 2e 20 0a 2a 2a 0a  y returned. .**.
18ea4 2a 2a 20 49 66 20 74 68 65 20 6e 42 79 74 65 73  ** If the nBytes
18ea5 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 30 20   parameter is 0 
18ea6 61 6e 64 20 74 68 65 20 62 6c 6f 62 20 6f 66 20  and the blob of 
18ea7 6d 65 6d 6f 72 79 20 68 61 73 20 6e 6f 74 20 79  memory has not y
18ea8 65 74 20 62 65 65 6e 0a 2a 2a 20 61 6c 6c 6f 63  et been.** alloc
18ea9 61 74 65 64 2c 20 61 20 6e 75 6c 6c 20 70 6f 69  ated, a null poi
18eaa 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64  nter is returned
18eab 2e 20 49 66 20 74 68 65 20 62 6c 6f 62 20 68 61  . If the blob ha
18eac 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 0a 2a  s already been.*
18ead 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20 69 74 20  * allocated, it 
18eae 69 73 20 72 65 74 75 72 6e 65 64 20 61 73 20 6e  is returned as n
18eaf 6f 72 6d 61 6c 2e 0a 2a 2a 0a 2a 2a 20 4a 75 73  ormal..**.** Jus
18eb0 74 20 62 65 66 6f 72 65 20 74 68 65 20 73 68 61  t before the sha
18eb1 72 65 64 2d 62 74 72 65 65 20 69 73 20 63 6c 6f  red-btree is clo
18eb2 73 65 64 2c 20 74 68 65 20 66 75 6e 63 74 69 6f  sed, the functio
18eb3 6e 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  n passed as the 
18eb4 0a 2a 2a 20 78 46 72 65 65 20 61 72 67 75 6d 65  .** xFree argume
18eb5 6e 74 20 77 68 65 6e 20 74 68 65 20 6d 65 6d 6f  nt when the memo
18eb6 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61  ry allocation wa
18eb7 73 20 6d 61 64 65 20 69 73 20 69 6e 76 6f 6b 65  s made is invoke
18eb8 64 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 62 6c 6f  d on the .** blo
18eb9 62 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 6d  b of allocated m
18eba 65 6d 6f 72 79 2e 20 54 68 69 73 20 66 75 6e 63  emory. This func
18ebb 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74 20  tion should not 
18ebc 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 66 72 65  call sqlite3_fre
18ebd 65 28 29 0a 2a 2a 20 6f 6e 20 74 68 65 20 6d 65  e().** on the me
18ebe 6d 6f 72 79 2c 20 74 68 65 20 62 74 72 65 65 20  mory, the btree 
18ebf 6c 61 79 65 72 20 64 6f 65 73 20 74 68 61 74 2e  layer does that.
18ec0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
18ec1 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
18ec2 42 74 72 65 65 53 63 68 65 6d 61 28 42 74 72 65  BtreeSchema(Btre
18ec3 65 20 2a 70 2c 20 69 6e 74 20 6e 42 79 74 65 73  e *p, int nBytes
18ec4 2c 20 76 6f 69 64 28 2a 78 46 72 65 65 29 28 76  , void(*xFree)(v
18ec5 6f 69 64 20 2a 29 29 7b 0a 20 20 42 74 53 68 61  oid *)){.  BtSha
18ec6 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
18ec7 74 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  t;.  sqlite3Btre
18ec8 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28  eEnter(p);.  if(
18ec9 20 21 70 42 74 2d 3e 70 53 63 68 65 6d 61 20 26   !pBt->pSchema &
18eca 26 20 6e 42 79 74 65 73 20 29 7b 0a 20 20 20 20  & nBytes ){.    
18ecb 70 42 74 2d 3e 70 53 63 68 65 6d 61 20 3d 20 73  pBt->pSchema = s
18ecc 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f  qlite3MallocZero
18ecd 28 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 70 42  (nBytes);.    pB
18ece 74 2d 3e 78 46 72 65 65 53 63 68 65 6d 61 20 3d  t->xFreeSchema =
18ecf 20 78 46 72 65 65 3b 0a 20 20 7d 0a 20 20 73 71   xFree;.  }.  sq
18ed0 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
18ed1 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 42 74  p);.  return pBt
18ed2 2d 3e 70 53 63 68 65 6d 61 3b 0a 7d 0a 0a 2f 2a  ->pSchema;.}../*
18ed3 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54  .** Return SQLIT
18ed4 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43  E_LOCKED_SHAREDC
18ed5 41 43 48 45 20 69 66 20 61 6e 6f 74 68 65 72 20  ACHE if another 
18ed6 75 73 65 72 20 6f 66 20 74 68 65 20 73 61 6d 65  user of the same
18ed7 20 73 68 61 72 65 64 20 0a 2a 2a 20 62 74 72 65   shared .** btre
18ed8 65 20 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e  e as the argumen
18ed9 74 20 68 61 6e 64 6c 65 20 68 6f 6c 64 73 20 61  t handle holds a
18eda 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  n exclusive lock
18edb 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69   on the .** sqli
18edc 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e  te_master table.
18edd 20 4f 74 68 65 72 77 69 73 65 20 53 51 4c 49 54   Otherwise SQLIT
18ede 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  E_OK..*/.SQLITE_
18edf 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
18ee0 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 4c 6f  te3BtreeSchemaLo
18ee1 63 6b 65 64 28 42 74 72 65 65 20 2a 70 29 7b 0a  cked(Btree *p){.
18ee2 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65    int rc;.  asse
18ee3 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
18ee4 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75  x_held(p->db->mu
18ee5 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65  tex) );.  sqlite
18ee6 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a  3BtreeEnter(p);.
18ee7 20 20 72 63 20 3d 20 71 75 65 72 79 53 68 61 72    rc = queryShar
18ee8 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b  edCacheTableLock
18ee9 28 70 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c  (p, MASTER_ROOT,
18eea 20 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 61   READ_LOCK);.  a
18eeb 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
18eec 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c 49  E_OK || rc==SQLI
18eed 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44  TE_LOCKED_SHARED
18eee 43 41 43 48 45 20 29 3b 0a 20 20 73 71 6c 69 74  CACHE );.  sqlit
18eef 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
18ef0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
18ef1 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
18ef2 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
18ef3 48 45 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20  HE./*.** Obtain 
18ef4 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61  a lock on the ta
18ef5 62 6c 65 20 77 68 6f 73 65 20 72 6f 6f 74 20 70  ble whose root p
18ef6 61 67 65 20 69 73 20 69 54 61 62 2e 20 20 54 68  age is iTab.  Th
18ef7 65 0a 2a 2a 20 6c 6f 63 6b 20 69 73 20 61 20 77  e.** lock is a w
18ef8 72 69 74 65 20 6c 6f 63 6b 20 69 66 20 69 73 57  rite lock if isW
18ef9 72 69 74 65 6c 6f 63 6b 20 69 73 20 74 72 75 65  ritelock is true
18efa 20 6f 72 20 61 20 72 65 61 64 20 6c 6f 63 6b 0a   or a read lock.
18efb 2a 2a 20 69 66 20 69 74 20 69 73 20 66 61 6c 73  ** if it is fals
18efc 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
18efd 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
18efe 42 74 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28 42  BtreeLockTable(B
18eff 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 61  tree *p, int iTa
18f00 62 2c 20 75 38 20 69 73 57 72 69 74 65 4c 6f 63  b, u8 isWriteLoc
18f01 6b 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  k){.  int rc = S
18f02 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65  QLITE_OK;.  asse
18f03 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 21 3d  rt( p->inTrans!=
18f04 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b 0a 20 20  TRANS_NONE );.  
18f05 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20  if( p->sharable 
18f06 29 7b 0a 20 20 20 20 75 38 20 6c 6f 63 6b 54 79  ){.    u8 lockTy
18f07 70 65 20 3d 20 52 45 41 44 5f 4c 4f 43 4b 20 2b  pe = READ_LOCK +
18f08 20 69 73 57 72 69 74 65 4c 6f 63 6b 3b 0a 20 20   isWriteLock;.  
18f09 20 20 61 73 73 65 72 74 28 20 52 45 41 44 5f 4c    assert( READ_L
18f0a 4f 43 4b 2b 31 3d 3d 57 52 49 54 45 5f 4c 4f 43  OCK+1==WRITE_LOC
18f0b 4b 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  K );.    assert(
18f0c 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 30 20   isWriteLock==0 
18f0d 7c 7c 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d  || isWriteLock==
18f0e 31 20 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65  1 );..    sqlite
18f0f 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a  3BtreeEnter(p);.
18f10 20 20 20 20 72 63 20 3d 20 71 75 65 72 79 53 68      rc = querySh
18f11 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f  aredCacheTableLo
18f12 63 6b 28 70 2c 20 69 54 61 62 2c 20 6c 6f 63 6b  ck(p, iTab, lock
18f13 54 79 70 65 29 3b 0a 20 20 20 20 69 66 28 20 72  Type);.    if( r
18f14 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
18f15 20 20 20 20 20 20 72 63 20 3d 20 73 65 74 53 68        rc = setSh
18f16 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f  aredCacheTableLo
18f17 63 6b 28 70 2c 20 69 54 61 62 2c 20 6c 6f 63 6b  ck(p, iTab, lock
18f18 54 79 70 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20  Type);.    }.   
18f19 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
18f1a 76 65 28 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74  ve(p);.  }.  ret
18f1b 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66  urn rc;.}.#endif
18f1c 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
18f1d 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f  _OMIT_INCRBLOB./
18f1e 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 70 43  *.** Argument pC
18f1f 73 72 20 6d 75 73 74 20 62 65 20 61 20 63 75 72  sr must be a cur
18f20 73 6f 72 20 6f 70 65 6e 65 64 20 66 6f 72 20 77  sor opened for w
18f21 72 69 74 69 6e 67 20 6f 6e 20 61 6e 20 0a 2a 2a  riting on an .**
18f22 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 20 63 75   INTKEY table cu
18f23 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67  rrently pointing
18f24 20 61 74 20 61 20 76 61 6c 69 64 20 74 61 62 6c   at a valid tabl
18f25 65 20 65 6e 74 72 79 2e 20 0a 2a 2a 20 54 68 69  e entry. .** Thi
18f26 73 20 66 75 6e 63 74 69 6f 6e 20 6d 6f 64 69 66  s function modif
18f27 69 65 73 20 74 68 65 20 64 61 74 61 20 73 74 6f  ies the data sto
18f28 72 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74  red as part of t
18f29 68 61 74 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a  hat entry..**.**
18f2a 20 4f 6e 6c 79 20 74 68 65 20 64 61 74 61 20 63   Only the data c
18f2b 6f 6e 74 65 6e 74 20 6d 61 79 20 6f 6e 6c 79 20  ontent may only 
18f2c 62 65 20 6d 6f 64 69 66 69 65 64 2c 20 69 74 20  be modified, it 
18f2d 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  is not possible 
18f2e 74 6f 20 0a 2a 2a 20 63 68 61 6e 67 65 20 74 68  to .** change th
18f2f 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20  e length of the 
18f30 64 61 74 61 20 73 74 6f 72 65 64 2e 20 49 66 20  data stored. If 
18f31 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
18f32 20 63 61 6c 6c 65 64 20 77 69 74 68 0a 2a 2a 20   called with.** 
18f33 70 61 72 61 6d 65 74 65 72 73 20 74 68 61 74 20  parameters that 
18f34 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65  attempt to write
18f35 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66   past the end of
18f36 20 74 68 65 20 65 78 69 73 74 69 6e 67 20 64 61   the existing da
18f37 74 61 2c 0a 2a 2a 20 6e 6f 20 6d 6f 64 69 66 69  ta,.** no modifi
18f38 63 61 74 69 6f 6e 73 20 61 72 65 20 6d 61 64 65  cations are made
18f39 20 61 6e 64 20 53 51 4c 49 54 45 5f 43 4f 52 52   and SQLITE_CORR
18f3a 55 50 54 20 69 73 20 72 65 74 75 72 6e 65 64 2e  UPT is returned.
18f3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
18f3c 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
18f3d 72 65 65 50 75 74 44 61 74 61 28 42 74 43 75 72  reePutData(BtCur
18f3e 73 6f 72 20 2a 70 43 73 72 2c 20 75 33 32 20 6f  sor *pCsr, u32 o
18f3f 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20  ffset, u32 amt, 
18f40 76 6f 69 64 20 2a 7a 29 7b 0a 20 20 69 6e 74 20  void *z){.  int 
18f41 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75  rc;.  assert( cu
18f42 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
18f43 43 73 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74  Csr) );.  assert
18f44 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
18f45 68 65 6c 64 28 70 43 73 72 2d 3e 70 42 74 72 65  held(pCsr->pBtre
18f46 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  e->db->mutex) );
18f47 0a 20 20 61 73 73 65 72 74 28 20 70 43 73 72 2d  .  assert( pCsr-
18f48 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c  >isIncrblobHandl
18f49 65 20 29 3b 0a 0a 20 20 72 63 20 3d 20 72 65 73  e );..  rc = res
18f4a 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69  toreCursorPositi
18f4b 6f 6e 28 70 43 73 72 29 3b 0a 20 20 69 66 28 20  on(pCsr);.  if( 
18f4c 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
18f4d 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
18f4e 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 43    }.  assert( pC
18f4f 73 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53  sr->eState!=CURS
18f50 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29  OR_REQUIRESEEK )
18f51 3b 0a 20 20 69 66 28 20 70 43 73 72 2d 3e 65 53  ;.  if( pCsr->eS
18f52 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c  tate!=CURSOR_VAL
18f53 49 44 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ID ){.    return
18f54 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20   SQLITE_ABORT;. 
18f55 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 73   }..  /* Check s
18f56 6f 6d 65 20 61 73 73 75 6d 70 74 69 6f 6e 73 3a  ome assumptions:
18f57 20 0a 20 20 2a 2a 20 20 20 28 61 29 20 74 68 65   .  **   (a) the
18f58 20 63 75 72 73 6f 72 20 69 73 20 6f 70 65 6e 20   cursor is open 
18f59 66 6f 72 20 77 72 69 74 69 6e 67 2c 0a 20 20 2a  for writing,.  *
18f5a 2a 20 20 20 28 62 29 20 74 68 65 72 65 20 69 73  *   (b) there is
18f5b 20 61 20 72 65 61 64 2f 77 72 69 74 65 20 74 72   a read/write tr
18f5c 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 2c 0a  ansaction open,.
18f5d 20 20 2a 2a 20 20 20 28 63 29 20 74 68 65 20 63    **   (c) the c
18f5e 6f 6e 6e 65 63 74 69 6f 6e 20 68 6f 6c 64 73 20  onnection holds 
18f5f 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20  a write-lock on 
18f60 74 68 65 20 74 61 62 6c 65 20 28 69 66 20 72 65  the table (if re
18f61 71 75 69 72 65 64 29 2c 0a 20 20 2a 2a 20 20 20  quired),.  **   
18f62 28 64 29 20 74 68 65 72 65 20 61 72 65 20 6e 6f  (d) there are no
18f63 20 63 6f 6e 66 6c 69 63 74 69 6e 67 20 72 65 61   conflicting rea
18f64 64 2d 6c 6f 63 6b 73 2c 20 61 6e 64 0a 20 20 2a  d-locks, and.  *
18f65 2a 20 20 20 28 65 29 20 74 68 65 20 63 75 72 73  *   (e) the curs
18f66 6f 72 20 70 6f 69 6e 74 73 20 61 74 20 61 20 76  or points at a v
18f67 61 6c 69 64 20 72 6f 77 20 6f 66 20 61 6e 20 69  alid row of an i
18f68 6e 74 4b 65 79 20 74 61 62 6c 65 2e 0a 20 20 2a  ntKey table..  *
18f69 2f 0a 20 20 69 66 28 20 21 70 43 73 72 2d 3e 77  /.  if( !pCsr->w
18f6a 72 46 6c 61 67 20 29 7b 0a 20 20 20 20 72 65 74  rFlag ){.    ret
18f6b 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f  urn SQLITE_READO
18f6c 4e 4c 59 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  NLY;.  }.  asser
18f6d 74 28 20 21 70 43 73 72 2d 3e 70 42 74 2d 3e 72  t( !pCsr->pBt->r
18f6e 65 61 64 4f 6e 6c 79 20 26 26 20 70 43 73 72 2d  eadOnly && pCsr-
18f6f 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74  >pBt->inTransact
18f70 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ion==TRANS_WRITE
18f71 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 68 61   );.  assert( ha
18f72 73 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  sSharedCacheTabl
18f73 65 4c 6f 63 6b 28 70 43 73 72 2d 3e 70 42 74 72  eLock(pCsr->pBtr
18f74 65 65 2c 20 70 43 73 72 2d 3e 70 67 6e 6f 52 6f  ee, pCsr->pgnoRo
18f75 6f 74 2c 20 30 2c 20 32 29 20 29 3b 0a 20 20 61  ot, 0, 2) );.  a
18f76 73 73 65 72 74 28 20 21 68 61 73 52 65 61 64 43  ssert( !hasReadC
18f77 6f 6e 66 6c 69 63 74 73 28 70 43 73 72 2d 3e 70  onflicts(pCsr->p
18f78 42 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 67 6e  Btree, pCsr->pgn
18f79 6f 52 6f 6f 74 29 20 29 3b 0a 20 20 61 73 73 65  oRoot) );.  asse
18f7a 72 74 28 20 70 43 73 72 2d 3e 61 70 50 61 67 65  rt( pCsr->apPage
18f7b 5b 70 43 73 72 2d 3e 69 50 61 67 65 5d 2d 3e 69  [pCsr->iPage]->i
18f7c 6e 74 4b 65 79 20 29 3b 0a 0a 20 20 72 65 74 75  ntKey );..  retu
18f7d 72 6e 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64  rn accessPayload
18f7e 28 70 43 73 72 2c 20 6f 66 66 73 65 74 2c 20 61  (pCsr, offset, a
18f7f 6d 74 2c 20 28 75 6e 73 69 67 6e 65 64 20 63 68  mt, (unsigned ch
18f80 61 72 20 2a 29 7a 2c 20 31 29 3b 0a 7d 0a 0a 2f  ar *)z, 1);.}../
18f81 2a 20 0a 2a 2a 20 53 65 74 20 61 20 66 6c 61 67  * .** Set a flag
18f82 20 6f 6e 20 74 68 69 73 20 63 75 72 73 6f 72 20   on this cursor 
18f83 74 6f 20 63 61 63 68 65 20 74 68 65 20 6c 6f 63  to cache the loc
18f84 61 74 69 6f 6e 73 20 6f 66 20 70 61 67 65 73 20  ations of pages 
18f85 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 6f 76 65  from the .** ove
18f86 72 66 6c 6f 77 20 6c 69 73 74 20 66 6f 72 20 74  rflow list for t
18f87 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 2e 20  he current row. 
18f88 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20  This is used by 
18f89 63 75 72 73 6f 72 73 20 6f 70 65 6e 65 64 0a 2a  cursors opened.*
18f8a 2a 20 66 6f 72 20 69 6e 63 72 65 6d 65 6e 74 61  * for incrementa
18f8b 6c 20 62 6c 6f 62 20 49 4f 20 6f 6e 6c 79 2e 0a  l blob IO only..
18f8c 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  **.** This funct
18f8d 69 6f 6e 20 73 65 74 73 20 61 20 66 6c 61 67 20  ion sets a flag 
18f8e 6f 6e 6c 79 2e 20 54 68 65 20 61 63 74 75 61 6c  only. The actual
18f8f 20 70 61 67 65 20 6c 6f 63 61 74 69 6f 6e 20 63   page location c
18f90 61 63 68 65 0a 2a 2a 20 28 73 74 6f 72 65 64 20  ache.** (stored 
18f91 69 6e 20 42 74 43 75 72 73 6f 72 2e 61 4f 76 65  in BtCursor.aOve
18f92 72 66 6c 6f 77 5b 5d 29 20 69 73 20 61 6c 6c 6f  rflow[]) is allo
18f93 63 61 74 65 64 20 61 6e 64 20 75 73 65 64 20 62  cated and used b
18f94 79 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 61 63  y function.** ac
18f95 63 65 73 73 50 61 79 6c 6f 61 64 28 29 20 28 74  cessPayload() (t
18f96 68 65 20 77 6f 72 6b 65 72 20 66 75 6e 63 74 69  he worker functi
18f97 6f 6e 20 66 6f 72 20 73 71 6c 69 74 65 33 42 74  on for sqlite3Bt
18f98 72 65 65 44 61 74 61 28 29 20 61 6e 64 0a 2a 2a  reeData() and.**
18f99 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75 74   sqlite3BtreePut
18f9a 44 61 74 61 28 29 29 2e 0a 2a 2f 0a 53 51 4c 49  Data())..*/.SQLI
18f9b 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
18f9c 73 71 6c 69 74 65 33 42 74 72 65 65 43 61 63 68  sqlite3BtreeCach
18f9d 65 4f 76 65 72 66 6c 6f 77 28 42 74 43 75 72 73  eOverflow(BtCurs
18f9e 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73  or *pCur){.  ass
18f9f 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
18fa0 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20  Mutex(pCur) );. 
18fa1 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
18fa2 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72  _mutex_held(pCur
18fa3 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75  ->pBtree->db->mu
18fa4 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
18fa5 28 21 70 43 75 72 2d 3e 69 73 49 6e 63 72 62 6c  (!pCur->isIncrbl
18fa6 6f 62 48 61 6e 64 6c 65 29 3b 0a 20 20 61 73 73  obHandle);.  ass
18fa7 65 72 74 28 21 70 43 75 72 2d 3e 61 4f 76 65 72  ert(!pCur->aOver
18fa8 66 6c 6f 77 29 3b 0a 20 20 70 43 75 72 2d 3e 69  flow);.  pCur->i
18fa9 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20  sIncrblobHandle 
18faa 3d 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  = 1;.}.#endif../
18fab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
18fac 6e 64 20 6f 66 20 62 74 72 65 65 2e 63 20 2a 2a  nd of btree.c **
18fad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18fae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18faf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
18fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
18fb1 65 67 69 6e 20 66 69 6c 65 20 62 61 63 6b 75 70  egin file backup
18fb2 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
18fb3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18fb4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
18fb5 2a 0a 2a 2a 20 32 30 30 39 20 4a 61 6e 75 61 72  *.** 2009 Januar
18fb6 79 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  y 28.**.** The a
18fb7 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
18fb8 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
18fb9 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
18fba 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
18fbb 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
18fbc 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
18fbd 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
18fbe 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
18fbf 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
18fc0 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
18fc1 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
18fc2 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
18fc3 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
18fc4 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
18fc5 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
18fc6 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
18fc7 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
18fc8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18fc9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18fca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18fcb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18fcc 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
18fcd 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 69 6d   contains the im
18fce 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
18fcf 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  the sqlite3_back
18fd0 75 70 5f 58 58 58 28 29 20 0a 2a 2a 20 41 50 49  up_XXX() .** API
18fd1 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 74   functions and t
18fd2 68 65 20 72 65 6c 61 74 65 64 20 66 65 61 74 75  he related featu
18fd3 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  res..**.** $Id: 
18fd4 62 61 63 6b 75 70 2e 63 2c 76 20 31 2e 31 39 20  backup.c,v 1.19 
18fd5 32 30 30 39 2f 30 37 2f 30 36 20 31 39 3a 30 33  2009/07/06 19:03
18fd6 3a 31 33 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :13 drh Exp $.*/
18fd7 0a 0a 2f 2a 20 4d 61 63 72 6f 20 74 6f 20 66 69  ../* Macro to fi
18fd8 6e 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 6f  nd the minimum o
18fd9 66 20 74 77 6f 20 6e 75 6d 65 72 69 63 20 76 61  f two numeric va
18fda 6c 75 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  lues..*/.#ifndef
18fdb 20 4d 49 4e 0a 23 20 64 65 66 69 6e 65 20 4d 49   MIN.# define MI
18fdc 4e 28 78 2c 79 29 20 28 28 78 29 3c 28 79 29 3f  N(x,y) ((x)<(y)?
18fdd 28 78 29 3a 28 79 29 29 0a 23 65 6e 64 69 66 0a  (x):(y)).#endif.
18fde 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 74 75 72 65  ./*.** Structure
18fdf 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 65   allocated for e
18fe0 61 63 68 20 62 61 63 6b 75 70 20 6f 70 65 72 61  ach backup opera
18fe1 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  tion..*/.struct 
18fe2 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 7b  sqlite3_backup {
18fe3 0a 20 20 73 71 6c 69 74 65 33 2a 20 70 44 65 73  .  sqlite3* pDes
18fe4 74 44 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 44  tDb;        /* D
18fe5 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62  estination datab
18fe6 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20  ase handle */.  
18fe7 42 74 72 65 65 20 2a 70 44 65 73 74 3b 20 20 20  Btree *pDest;   
18fe8 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74           /* Dest
18fe9 69 6e 61 74 69 6f 6e 20 62 2d 74 72 65 65 20 66  ination b-tree f
18fea 69 6c 65 20 2a 2f 0a 20 20 75 33 32 20 69 44 65  ile */.  u32 iDe
18feb 73 74 53 63 68 65 6d 61 3b 20 20 20 20 20 20 20  stSchema;       
18fec 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 73 63    /* Original sc
18fed 68 65 6d 61 20 63 6f 6f 6b 69 65 20 69 6e 20 64  hema cookie in d
18fee 65 73 74 69 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20  estination */.  
18fef 69 6e 74 20 62 44 65 73 74 4c 6f 63 6b 65 64 3b  int bDestLocked;
18ff0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
18ff1 20 6f 6e 63 65 20 61 20 77 72 69 74 65 2d 74 72   once a write-tr
18ff2 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65  ansaction is ope
18ff3 6e 20 6f 6e 20 70 44 65 73 74 20 2a 2f 0a 0a 20  n on pDest */.. 
18ff4 20 50 67 6e 6f 20 69 4e 65 78 74 3b 20 20 20 20   Pgno iNext;    
18ff5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67            /* Pag
18ff6 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
18ff7 6e 65 78 74 20 73 6f 75 72 63 65 20 70 61 67 65  next source page
18ff8 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 73 71   to copy */.  sq
18ff9 6c 69 74 65 33 2a 20 70 53 72 63 44 62 3b 20 20  lite3* pSrcDb;  
18ffa 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65         /* Source
18ffb 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
18ffc 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 53 72   */.  Btree *pSr
18ffd 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  c;             /
18ffe 2a 20 53 6f 75 72 63 65 20 62 2d 74 72 65 65 20  * Source b-tree 
18fff 66 69 6c 65 20 2a 2f 0a 0a 20 20 69 6e 74 20 72  file */..  int r
19000 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
19001 20 20 20 20 2f 2a 20 42 61 63 6b 75 70 20 70 72      /* Backup pr
19002 6f 63 65 73 73 20 65 72 72 6f 72 20 63 6f 64 65  ocess error code
19003 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 73 65 20   */..  /* These 
19004 74 77 6f 20 76 61 72 69 61 62 6c 65 73 20 61 72  two variables ar
19005 65 20 73 65 74 20 62 79 20 65 76 65 72 79 20 63  e set by every c
19006 61 6c 6c 20 74 6f 20 62 61 63 6b 75 70 5f 73 74  all to backup_st
19007 65 70 28 29 2e 20 54 68 65 79 20 61 72 65 0a 20  ep(). They are. 
19008 20 2a 2a 20 72 65 61 64 20 62 79 20 63 61 6c 6c   ** read by call
19009 73 20 74 6f 20 62 61 63 6b 75 70 5f 72 65 6d 61  s to backup_rema
1900a 69 6e 69 6e 67 28 29 20 61 6e 64 20 62 61 63 6b  ining() and back
1900b 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 2e 0a  up_pagecount()..
1900c 20 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 52 65 6d    */.  Pgno nRem
1900d 61 69 6e 69 6e 67 3b 20 20 20 20 20 20 20 20 20  aining;         
1900e 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67  /* Number of pag
1900f 65 73 20 6c 65 66 74 20 74 6f 20 63 6f 70 79 20  es left to copy 
19010 2a 2f 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65 63  */.  Pgno nPagec
19011 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a  ount;         /*
19012 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   Total number of
19013 20 70 61 67 65 73 20 74 6f 20 63 6f 70 79 20 2a   pages to copy *
19014 2f 0a 0a 20 20 69 6e 74 20 69 73 41 74 74 61 63  /..  int isAttac
19015 68 65 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  hed;          /*
19016 20 54 72 75 65 20 6f 6e 63 65 20 62 61 63 6b 75   True once backu
19017 70 20 68 61 73 20 62 65 65 6e 20 72 65 67 69 73  p has been regis
19018 74 65 72 65 64 20 77 69 74 68 20 70 61 67 65 72  tered with pager
19019 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62 61   */.  sqlite3_ba
1901a 63 6b 75 70 20 2a 70 4e 65 78 74 3b 20 20 20 2f  ckup *pNext;   /
1901b 2a 20 4e 65 78 74 20 62 61 63 6b 75 70 20 61 73  * Next backup as
1901c 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 73 6f  sociated with so
1901d 75 72 63 65 20 70 61 67 65 72 20 2a 2f 0a 7d 3b  urce pager */.};
1901e 0a 0a 2f 2a 0a 2a 2a 20 54 48 52 45 41 44 20 53  ../*.** THREAD S
1901f 41 46 45 54 59 20 4e 4f 54 45 53 3a 0a 2a 2a 0a  AFETY NOTES:.**.
19020 2a 2a 20 20 20 4f 6e 63 65 20 69 74 20 68 61 73  **   Once it has
19021 20 62 65 65 6e 20 63 72 65 61 74 65 64 20 75 73   been created us
19022 69 6e 67 20 62 61 63 6b 75 70 5f 69 6e 69 74 28  ing backup_init(
19023 29 2c 20 61 20 73 69 6e 67 6c 65 20 73 71 6c 69  ), a single sqli
19024 74 65 33 5f 62 61 63 6b 75 70 0a 2a 2a 20 20 20  te3_backup.**   
19025 73 74 72 75 63 74 75 72 65 20 6d 61 79 20 62 65  structure may be
19026 20 61 63 63 65 73 73 65 64 20 76 69 61 20 74 77   accessed via tw
19027 6f 20 67 72 6f 75 70 73 20 6f 66 20 74 68 72 65  o groups of thre
19028 61 64 2d 73 61 66 65 20 65 6e 74 72 79 20 70 6f  ad-safe entry po
19029 69 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ints:.**.**     
1902a 2a 20 56 69 61 20 74 68 65 20 73 71 6c 69 74 65  * Via the sqlite
1902b 33 5f 62 61 63 6b 75 70 5f 58 58 58 28 29 20 41  3_backup_XXX() A
1902c 50 49 20 66 75 6e 63 74 69 6f 6e 20 62 61 63 6b  PI function back
1902d 75 70 5f 73 74 65 70 28 29 20 61 6e 64 20 0a 2a  up_step() and .*
1902e 2a 20 20 20 20 20 20 20 62 61 63 6b 75 70 5f 66  *       backup_f
1902f 69 6e 69 73 68 28 29 2e 20 42 6f 74 68 20 74 68  inish(). Both th
19030 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 6f 62  ese functions ob
19031 74 61 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20  tain the source 
19032 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 20 20  database.**     
19033 20 20 68 61 6e 64 6c 65 20 6d 75 74 65 78 20 61    handle mutex a
19034 6e 64 20 74 68 65 20 6d 75 74 65 78 20 61 73 73  nd the mutex ass
19035 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
19036 20 73 6f 75 72 63 65 20 42 74 53 68 61 72 65 64   source BtShared
19037 20 0a 2a 2a 20 20 20 20 20 20 20 73 74 72 75 63   .**       struc
19038 74 75 72 65 2c 20 69 6e 20 74 68 61 74 20 6f 72  ture, in that or
19039 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a  der..**.**     *
1903a 20 56 69 61 20 74 68 65 20 42 61 63 6b 75 70 55   Via the BackupU
1903b 70 64 61 74 65 28 29 20 61 6e 64 20 42 61 63 6b  pdate() and Back
1903c 75 70 52 65 73 74 61 72 74 28 29 20 66 75 6e 63  upRestart() func
1903d 74 69 6f 6e 73 2c 20 77 68 69 63 68 20 61 72 65  tions, which are
1903e 0a 2a 2a 20 20 20 20 20 20 20 69 6e 76 6f 6b 65  .**       invoke
1903f 64 20 62 79 20 74 68 65 20 70 61 67 65 72 20 6c  d by the pager l
19040 61 79 65 72 20 74 6f 20 72 65 70 6f 72 74 20 76  ayer to report v
19041 61 72 69 6f 75 73 20 73 74 61 74 65 20 63 68 61  arious state cha
19042 6e 67 65 73 20 69 6e 0a 2a 2a 20 20 20 20 20 20  nges in.**      
19043 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 20   the page cache 
19044 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
19045 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62  the source datab
19046 61 73 65 2e 20 54 68 65 20 6d 75 74 65 78 0a 2a  ase. The mutex.*
19047 2a 20 20 20 20 20 20 20 61 73 73 6f 63 69 61 74  *       associat
19048 65 64 20 77 69 74 68 20 74 68 65 20 73 6f 75 72  ed with the sour
19049 63 65 20 64 61 74 61 62 61 73 65 20 42 74 53 68  ce database BtSh
1904a 61 72 65 64 20 73 74 72 75 63 74 75 72 65 20 77  ared structure w
1904b 69 6c 6c 20 61 6c 77 61 79 73 20 0a 2a 2a 20 20  ill always .**  
1904c 20 20 20 20 20 62 65 20 68 65 6c 64 20 77 68 65       be held whe
1904d 6e 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 73  n either of thes
1904e 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20  e functions are 
1904f 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 20  invoked..**.**  
19050 20 54 68 65 20 6f 74 68 65 72 20 73 71 6c 69 74   The other sqlit
19051 65 33 5f 62 61 63 6b 75 70 5f 58 58 58 28 29 20  e3_backup_XXX() 
19052 41 50 49 20 66 75 6e 63 74 69 6f 6e 73 2c 20 62  API functions, b
19053 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28  ackup_remaining(
19054 29 20 61 6e 64 0a 2a 2a 20 20 20 62 61 63 6b 75  ) and.**   backu
19055 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 20 61 72  p_pagecount() ar
19056 65 20 6e 6f 74 20 74 68 72 65 61 64 2d 73 61 66  e not thread-saf
19057 65 20 66 75 6e 63 74 69 6f 6e 73 2e 20 49 66 20  e functions. If 
19058 74 68 65 79 20 61 72 65 20 63 61 6c 6c 65 64 0a  they are called.
19059 2a 2a 20 20 20 77 68 69 6c 65 20 73 6f 6d 65 20  **   while some 
1905a 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 73 20  other thread is 
1905b 63 61 6c 6c 69 6e 67 20 62 61 63 6b 75 70 5f 73  calling backup_s
1905c 74 65 70 28 29 20 6f 72 20 62 61 63 6b 75 70 5f  tep() or backup_
1905d 66 69 6e 69 73 68 28 29 2c 0a 2a 2a 20 20 20 74  finish(),.**   t
1905e 68 65 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e  he values return
1905f 65 64 20 6d 61 79 20 62 65 20 69 6e 76 61 6c 69  ed may be invali
19060 64 2e 20 54 68 65 72 65 20 69 73 20 6e 6f 20 77  d. There is no w
19061 61 79 20 66 6f 72 20 61 20 63 61 6c 6c 20 74 6f  ay for a call to
19062 0a 2a 2a 20 20 20 42 61 63 6b 75 70 55 70 64 61  .**   BackupUpda
19063 74 65 28 29 20 6f 72 20 42 61 63 6b 75 70 52 65  te() or BackupRe
19064 73 74 61 72 74 28 29 20 74 6f 20 69 6e 74 65 72  start() to inter
19065 66 65 72 65 20 77 69 74 68 20 62 61 63 6b 75 70  fere with backup
19066 5f 72 65 6d 61 69 6e 69 6e 67 28 29 0a 2a 2a 20  _remaining().** 
19067 20 20 6f 72 20 62 61 63 6b 75 70 5f 70 61 67 65    or backup_page
19068 63 6f 75 6e 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 20  count()..**.**  
19069 20 44 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68   Depending on th
1906a 65 20 53 51 4c 69 74 65 20 63 6f 6e 66 69 67 75  e SQLite configu
1906b 72 61 74 69 6f 6e 2c 20 74 68 65 20 64 61 74 61  ration, the data
1906c 62 61 73 65 20 68 61 6e 64 6c 65 73 20 61 6e 64  base handles and
1906d 2f 6f 72 0a 2a 2a 20 20 20 74 68 65 20 42 74 72  /or.**   the Btr
1906e 65 65 20 6f 62 6a 65 63 74 73 20 6d 61 79 20 68  ee objects may h
1906f 61 76 65 20 74 68 65 69 72 20 6f 77 6e 20 6d 75  ave their own mu
19070 74 65 78 65 73 20 74 68 61 74 20 72 65 71 75 69  texes that requi
19071 72 65 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 20  re locking..**  
19072 20 4e 6f 6e 2d 73 68 61 72 61 62 6c 65 20 42 74   Non-sharable Bt
19073 72 65 65 73 20 28 69 6e 2d 6d 65 6d 6f 72 79 20  rees (in-memory 
19074 64 61 74 61 62 61 73 65 73 20 66 6f 72 20 65 78  databases for ex
19075 61 6d 70 6c 65 29 2c 20 64 6f 20 6e 6f 74 20 68  ample), do not h
19076 61 76 65 0a 2a 2a 20 20 20 61 73 73 6f 63 69 61  ave.**   associa
19077 74 65 64 20 6d 75 74 65 78 65 73 2e 0a 2a 2f 0a  ted mutexes..*/.
19078 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  ./*.** Return a 
19079 70 6f 69 6e 74 65 72 20 63 6f 72 72 65 73 70 6f  pointer correspo
1907a 6e 64 69 6e 67 20 74 6f 20 64 61 74 61 62 61 73  nding to databas
1907b 65 20 7a 44 62 20 28 69 2e 65 2e 20 22 6d 61 69  e zDb (i.e. "mai
1907c 6e 22 2c 20 22 74 65 6d 70 22 29 0a 2a 2a 20 69  n", "temp").** i
1907d 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e  n connection han
1907e 64 6c 65 20 70 44 62 2e 20 49 66 20 73 75 63 68  dle pDb. If such
1907f 20 61 20 64 61 74 61 62 61 73 65 20 63 61 6e 6e   a database cann
19080 6f 74 20 62 65 20 66 6f 75 6e 64 2c 20 72 65 74  ot be found, ret
19081 75 72 6e 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f  urn.** a NULL po
19082 69 6e 74 65 72 20 61 6e 64 20 77 72 69 74 65 20  inter and write 
19083 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
19084 20 74 6f 20 70 45 72 72 6f 72 44 62 2e 0a 2a 2a   to pErrorDb..**
19085 0a 2a 2a 20 49 66 20 74 68 65 20 22 74 65 6d 70  .** If the "temp
19086 22 20 64 61 74 61 62 61 73 65 20 69 73 20 72 65  " database is re
19087 71 75 65 73 74 65 64 2c 20 69 74 20 6d 61 79 20  quested, it may 
19088 6e 65 65 64 20 74 6f 20 62 65 20 6f 70 65 6e 65  need to be opene
19089 64 20 62 79 20 74 68 69 73 20 0a 2a 2a 20 66 75  d by this .** fu
1908a 6e 63 74 69 6f 6e 2e 20 49 66 20 61 6e 20 65 72  nction. If an er
1908b 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65  ror occurs while
1908c 20 64 6f 69 6e 67 20 73 6f 2c 20 72 65 74 75 72   doing so, retur
1908d 6e 20 30 20 61 6e 64 20 77 72 69 74 65 20 61 6e  n 0 and write an
1908e 20 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73 61   .** error messa
1908f 67 65 20 74 6f 20 70 45 72 72 6f 72 44 62 2e 0a  ge to pErrorDb..
19090 2a 2f 0a 73 74 61 74 69 63 20 42 74 72 65 65 20  */.static Btree 
19091 2a 66 69 6e 64 42 74 72 65 65 28 73 71 6c 69 74  *findBtree(sqlit
19092 65 33 20 2a 70 45 72 72 6f 72 44 62 2c 20 73 71  e3 *pErrorDb, sq
19093 6c 69 74 65 33 20 2a 70 44 62 2c 20 63 6f 6e 73  lite3 *pDb, cons
19094 74 20 63 68 61 72 20 2a 7a 44 62 29 7b 0a 20 20  t char *zDb){.  
19095 69 6e 74 20 69 20 3d 20 73 71 6c 69 74 65 33 46  int i = sqlite3F
19096 69 6e 64 44 62 4e 61 6d 65 28 70 44 62 2c 20 7a  indDbName(pDb, z
19097 44 62 29 3b 0a 0a 20 20 69 66 28 20 69 3d 3d 31  Db);..  if( i==1
19098 20 29 7b 0a 20 20 20 20 50 61 72 73 65 20 2a 70   ){.    Parse *p
19099 50 61 72 73 65 3b 0a 20 20 20 20 69 6e 74 20 72  Parse;.    int r
1909a 63 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 72 73  c = 0;.    pPars
1909b 65 20 3d 20 73 71 6c 69 74 65 33 53 74 61 63 6b  e = sqlite3Stack
1909c 41 6c 6c 6f 63 5a 65 72 6f 28 70 45 72 72 6f 72  AllocZero(pError
1909d 44 62 2c 20 73 69 7a 65 6f 66 28 2a 70 50 61 72  Db, sizeof(*pPar
1909e 73 65 29 29 3b 0a 20 20 20 20 69 66 28 20 70 50  se));.    if( pP
1909f 61 72 73 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20  arse==0 ){.     
190a0 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 45   sqlite3Error(pE
190a1 72 72 6f 72 44 62 2c 20 53 51 4c 49 54 45 5f 4e  rrorDb, SQLITE_N
190a2 4f 4d 45 4d 2c 20 22 6f 75 74 20 6f 66 20 6d 65  OMEM, "out of me
190a3 6d 6f 72 79 22 29 3b 0a 20 20 20 20 20 20 72 63  mory");.      rc
190a4 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
190a5 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
190a6 20 20 70 50 61 72 73 65 2d 3e 64 62 20 3d 20 70    pParse->db = p
190a7 44 62 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71  Db;.      if( sq
190a8 6c 69 74 65 33 4f 70 65 6e 54 65 6d 70 44 61 74  lite3OpenTempDat
190a9 61 62 61 73 65 28 70 50 61 72 73 65 29 20 29 7b  abase(pParse) ){
190aa 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
190ab 45 72 72 6f 72 43 6c 65 61 72 28 70 50 61 72 73  ErrorClear(pPars
190ac 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  e);.        sqli
190ad 74 65 33 45 72 72 6f 72 28 70 45 72 72 6f 72 44  te3Error(pErrorD
190ae 62 2c 20 70 50 61 72 73 65 2d 3e 72 63 2c 20 22  b, pParse->rc, "
190af 25 73 22 2c 20 70 50 61 72 73 65 2d 3e 7a 45 72  %s", pParse->zEr
190b0 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 20 20 72  rMsg);.        r
190b1 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
190b2 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
190b3 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65  sqlite3StackFree
190b4 28 70 45 72 72 6f 72 44 62 2c 20 70 50 61 72 73  (pErrorDb, pPars
190b5 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  e);.    }.    if
190b6 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65  ( rc ){.      re
190b7 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20  turn 0;.    }.  
190b8 7d 0a 0a 20 20 69 66 28 20 69 3c 30 20 29 7b 0a  }..  if( i<0 ){.
190b9 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
190ba 28 70 45 72 72 6f 72 44 62 2c 20 53 51 4c 49 54  (pErrorDb, SQLIT
190bb 45 5f 45 52 52 4f 52 2c 20 22 75 6e 6b 6e 6f 77  E_ERROR, "unknow
190bc 6e 20 64 61 74 61 62 61 73 65 20 25 73 22 2c 20  n database %s", 
190bd 7a 44 62 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  zDb);.    return
190be 20 30 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72   0;.  }..  retur
190bf 6e 20 70 44 62 2d 3e 61 44 62 5b 69 5d 2e 70 42  n pDb->aDb[i].pB
190c0 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61  t;.}../*.** Crea
190c1 74 65 20 61 6e 20 73 71 6c 69 74 65 33 5f 62 61  te an sqlite3_ba
190c2 63 6b 75 70 20 70 72 6f 63 65 73 73 20 74 6f 20  ckup process to 
190c3 63 6f 70 79 20 74 68 65 20 63 6f 6e 74 65 6e 74  copy the content
190c4 73 20 6f 66 20 7a 53 72 63 44 62 20 66 72 6f 6d  s of zSrcDb from
190c5 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68  .** connection h
190c6 61 6e 64 6c 65 20 70 53 72 63 44 62 20 74 6f 20  andle pSrcDb to 
190c7 7a 44 65 73 74 44 62 20 69 6e 20 70 44 65 73 74  zDestDb in pDest
190c8 44 62 2e 20 49 66 20 73 75 63 63 65 73 73 66 75  Db. If successfu
190c9 6c 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70  l, return.** a p
190ca 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65  ointer to the ne
190cb 77 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  w sqlite3_backup
190cc 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49   object..**.** I
190cd 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
190ce 73 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72  s, NULL is retur
190cf 6e 65 64 20 61 6e 64 20 61 6e 20 65 72 72 6f 72  ned and an error
190d0 20 63 6f 64 65 20 61 6e 64 20 65 72 72 6f 72 20   code and error 
190d1 6d 65 73 73 61 67 65 0a 2a 2a 20 73 74 6f 72 65  message.** store
190d2 64 20 69 6e 20 64 61 74 61 62 61 73 65 20 68 61  d in database ha
190d3 6e 64 6c 65 20 70 44 65 73 74 44 62 2e 0a 2a 2f  ndle pDestDb..*/
190d4 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
190d5 74 65 33 5f 62 61 63 6b 75 70 20 2a 73 71 6c 69  te3_backup *sqli
190d6 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28  te3_backup_init(
190d7 0a 20 20 73 71 6c 69 74 65 33 2a 20 70 44 65 73  .  sqlite3* pDes
190d8 74 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  tDb,            
190d9 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
190da 62 61 73 65 20 74 6f 20 77 72 69 74 65 20 74 6f  base to write to
190db 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
190dc 20 2a 7a 44 65 73 74 44 62 2c 20 20 20 20 20 20   *zDestDb,      
190dd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
190de 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 20  ame of database 
190df 77 69 74 68 69 6e 20 70 44 65 73 74 44 62 20 2a  within pDestDb *
190e0 2f 0a 20 20 73 71 6c 69 74 65 33 2a 20 70 53 72  /.  sqlite3* pSr
190e1 63 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  cDb,            
190e2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
190e3 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
190e4 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f   to read from */
190e5 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
190e6 53 72 63 44 62 20 20 20 20 20 20 20 20 20 20 20  SrcDb           
190e7 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
190e8 20 6f 66 20 64 61 74 61 62 61 73 65 20 77 69 74   of database wit
190e9 68 69 6e 20 70 53 72 63 44 62 20 2a 2f 0a 29 7b  hin pSrcDb */.){
190ea 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  .  sqlite3_backu
190eb 70 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20  p *p;           
190ec 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75           /* Valu
190ed 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 0a  e to return */..
190ee 20 20 2f 2a 20 4c 6f 63 6b 20 74 68 65 20 73 6f    /* Lock the so
190ef 75 72 63 65 20 64 61 74 61 62 61 73 65 20 68 61  urce database ha
190f0 6e 64 6c 65 2e 20 54 68 65 20 64 65 73 74 69 6e  ndle. The destin
190f1 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 0a 20  ation database. 
190f2 20 2a 2a 20 68 61 6e 64 6c 65 20 69 73 20 6e 6f   ** handle is no
190f3 74 20 6c 6f 63 6b 65 64 20 69 6e 20 74 68 69 73  t locked in this
190f4 20 72 6f 75 74 69 6e 65 2c 20 62 75 74 20 69 74   routine, but it
190f5 20 69 73 20 6c 6f 63 6b 65 64 20 69 6e 0a 20 20   is locked in.  
190f6 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  ** sqlite3_backu
190f7 70 5f 73 74 65 70 28 29 2e 20 54 68 65 20 75 73  p_step(). The us
190f8 65 72 20 69 73 20 72 65 71 75 69 72 65 64 20 74  er is required t
190f9 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 6e 6f  o ensure that no
190fa 0a 20 20 2a 2a 20 6f 74 68 65 72 20 74 68 72 65  .  ** other thre
190fb 61 64 20 61 63 63 65 73 73 65 73 20 74 68 65 20  ad accesses the 
190fc 64 65 73 74 69 6e 61 74 69 6f 6e 20 68 61 6e 64  destination hand
190fd 6c 65 20 66 6f 72 20 74 68 65 20 64 75 72 61 74  le for the durat
190fe 69 6f 6e 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20  ion.  ** of the 
190ff 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e  backup operation
19100 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74  .  Any attempt t
19101 6f 20 75 73 65 20 74 68 65 20 64 65 73 74 69 6e  o use the destin
19102 61 74 69 6f 6e 0a 20 20 2a 2a 20 64 61 74 61 62  ation.  ** datab
19103 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77  ase connection w
19104 68 69 6c 65 20 61 20 62 61 63 6b 75 70 20 69 73  hile a backup is
19105 20 69 6e 20 70 72 6f 67 72 65 73 73 20 6d 61 79   in progress may
19106 20 63 61 75 73 65 0a 20 20 2a 2a 20 61 20 6d 61   cause.  ** a ma
19107 6c 66 75 6e 63 74 69 6f 6e 20 6f 72 20 61 20 64  lfunction or a d
19108 65 61 64 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20  eadlock..  */.  
19109 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
1910a 74 65 72 28 70 53 72 63 44 62 2d 3e 6d 75 74 65  ter(pSrcDb->mute
1910b 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  x);.  sqlite3_mu
1910c 74 65 78 5f 65 6e 74 65 72 28 70 44 65 73 74 44  tex_enter(pDestD
1910d 62 2d 3e 6d 75 74 65 78 29 3b 0a 0a 20 20 69 66  b->mutex);..  if
1910e 28 20 70 53 72 63 44 62 3d 3d 70 44 65 73 74 44  ( pSrcDb==pDestD
1910f 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  b ){.    sqlite3
19110 45 72 72 6f 72 28 0a 20 20 20 20 20 20 20 20 70  Error(.        p
19111 44 65 73 74 44 62 2c 20 53 51 4c 49 54 45 5f 45  DestDb, SQLITE_E
19112 52 52 4f 52 2c 20 22 73 6f 75 72 63 65 20 61 6e  RROR, "source an
19113 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 6d 75  d destination mu
19114 73 74 20 62 65 20 64 69 73 74 69 6e 63 74 22 0a  st be distinct".
19115 20 20 20 20 29 3b 0a 20 20 20 20 70 20 3d 20 30      );.    p = 0
19116 3b 0a 20 20 7d 65 6c 73 65 20 7b 0a 20 20 20 20  ;.  }else {.    
19117 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63  /* Allocate spac
19118 65 20 66 6f 72 20 61 20 6e 65 77 20 73 71 6c 69  e for a new sqli
19119 74 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a 65 63  te3_backup objec
1911a 74 20 2a 2f 0a 20 20 20 20 70 20 3d 20 28 73 71  t */.    p = (sq
1911b 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 29 73  lite3_backup *)s
1911c 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
1911d 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 62 61 63  zeof(sqlite3_bac
1911e 6b 75 70 29 29 3b 0a 20 20 20 20 69 66 28 20 21  kup));.    if( !
1911f 70 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  p ){.      sqlit
19120 65 33 45 72 72 6f 72 28 70 44 65 73 74 44 62 2c  e3Error(pDestDb,
19121 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 30   SQLITE_NOMEM, 0
19122 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
19123 2f 2a 20 49 66 20 74 68 65 20 61 6c 6c 6f 63 61  /* If the alloca
19124 74 69 6f 6e 20 73 75 63 63 65 65 64 65 64 2c 20  tion succeeded, 
19125 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6e 65 77  populate the new
19126 20 6f 62 6a 65 63 74 2e 20 2a 2f 0a 20 20 69 66   object. */.  if
19127 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65  ( p ){.    memse
19128 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73  t(p, 0, sizeof(s
19129 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 29 29 3b  qlite3_backup));
1912a 0a 20 20 20 20 70 2d 3e 70 53 72 63 20 3d 20 66  .    p->pSrc = f
1912b 69 6e 64 42 74 72 65 65 28 70 44 65 73 74 44 62  indBtree(pDestDb
1912c 2c 20 70 53 72 63 44 62 2c 20 7a 53 72 63 44 62  , pSrcDb, zSrcDb
1912d 29 3b 0a 20 20 20 20 70 2d 3e 70 44 65 73 74 20  );.    p->pDest 
1912e 3d 20 66 69 6e 64 42 74 72 65 65 28 70 44 65 73  = findBtree(pDes
1912f 74 44 62 2c 20 70 44 65 73 74 44 62 2c 20 7a 44  tDb, pDestDb, zD
19130 65 73 74 44 62 29 3b 0a 20 20 20 20 70 2d 3e 70  estDb);.    p->p
19131 44 65 73 74 44 62 20 3d 20 70 44 65 73 74 44 62  DestDb = pDestDb
19132 3b 0a 20 20 20 20 70 2d 3e 70 53 72 63 44 62 20  ;.    p->pSrcDb 
19133 3d 20 70 53 72 63 44 62 3b 0a 20 20 20 20 70 2d  = pSrcDb;.    p-
19134 3e 69 4e 65 78 74 20 3d 20 31 3b 0a 20 20 20 20  >iNext = 1;.    
19135 70 2d 3e 69 73 41 74 74 61 63 68 65 64 20 3d 20  p->isAttached = 
19136 30 3b 0a 0a 20 20 20 20 69 66 28 20 30 3d 3d 70  0;..    if( 0==p
19137 2d 3e 70 53 72 63 20 7c 7c 20 30 3d 3d 70 2d 3e  ->pSrc || 0==p->
19138 70 44 65 73 74 20 29 7b 0a 20 20 20 20 20 20 2f  pDest ){.      /
19139 2a 20 4f 6e 65 20 28 6f 72 20 62 6f 74 68 29 20  * One (or both) 
1913a 6f 66 20 74 68 65 20 6e 61 6d 65 64 20 64 61 74  of the named dat
1913b 61 62 61 73 65 73 20 64 69 64 20 6e 6f 74 20 65  abases did not e
1913c 78 69 73 74 2e 20 41 6e 20 65 72 72 6f 72 20 68  xist. An error h
1913d 61 73 0a 20 20 20 20 20 20 2a 2a 20 61 6c 72 65  as.      ** alre
1913e 61 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e  ady been written
1913f 20 69 6e 74 6f 20 74 68 65 20 70 44 65 73 74 44   into the pDestD
19140 62 20 68 61 6e 64 6c 65 2e 20 41 6c 6c 20 74 68  b handle. All th
19141 61 74 20 69 73 20 6c 65 66 74 0a 20 20 20 20 20  at is left.     
19142 20 2a 2a 20 74 6f 20 64 6f 20 68 65 72 65 20 69   ** to do here i
19143 73 20 66 72 65 65 20 74 68 65 20 73 71 6c 69 74  s free the sqlit
19144 65 33 5f 62 61 63 6b 75 70 20 73 74 72 75 63 74  e3_backup struct
19145 75 72 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ure..      */.  
19146 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
19147 28 70 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 30  (p);.      p = 0
19148 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
19149 28 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e 70 53  ( p ){.    p->pS
1914a 72 63 2d 3e 6e 42 61 63 6b 75 70 2b 2b 3b 0a 20  rc->nBackup++;. 
1914b 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75   }..  sqlite3_mu
1914c 74 65 78 5f 6c 65 61 76 65 28 70 44 65 73 74 44  tex_leave(pDestD
1914d 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c  b->mutex);.  sql
1914e 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
1914f 28 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b  (pSrcDb->mutex);
19150 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
19151 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 72  /*.** Argument r
19152 63 20 69 73 20 61 6e 20 53 51 4c 69 74 65 20 65  c is an SQLite e
19153 72 72 6f 72 20 63 6f 64 65 2e 20 52 65 74 75 72  rror code. Retur
19154 6e 20 74 72 75 65 20 69 66 20 74 68 69 73 20 65  n true if this e
19155 72 72 6f 72 20 69 73 20 0a 2a 2a 20 63 6f 6e 73  rror is .** cons
19156 69 64 65 72 65 64 20 66 61 74 61 6c 20 69 66 20  idered fatal if 
19157 65 6e 63 6f 75 6e 74 65 72 65 64 20 64 75 72 69  encountered duri
19158 6e 67 20 61 20 62 61 63 6b 75 70 20 6f 70 65 72  ng a backup oper
19159 61 74 69 6f 6e 2e 20 41 6c 6c 20 65 72 72 6f 72  ation. All error
1915a 73 0a 2a 2a 20 61 72 65 20 63 6f 6e 73 69 64 65  s.** are conside
1915b 72 65 64 20 66 61 74 61 6c 20 65 78 63 65 70 74  red fatal except
1915c 20 66 6f 72 20 53 51 4c 49 54 45 5f 42 55 53 59   for SQLITE_BUSY
1915d 20 61 6e 64 20 53 51 4c 49 54 45 5f 4c 4f 43 4b   and SQLITE_LOCK
1915e 45 44 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ED..*/.static in
1915f 74 20 69 73 46 61 74 61 6c 45 72 72 6f 72 28 69  t isFatalError(i
19160 6e 74 20 72 63 29 7b 0a 20 20 72 65 74 75 72 6e  nt rc){.  return
19161 20 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20   (rc!=SQLITE_OK 
19162 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 42 55  && rc!=SQLITE_BU
19163 53 59 20 26 26 20 41 4c 57 41 59 53 28 72 63 21  SY && ALWAYS(rc!
19164 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 29 29  =SQLITE_LOCKED))
19165 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d  ;.}../*.** Param
19166 65 74 65 72 20 7a 53 72 63 44 61 74 61 20 70 6f  eter zSrcData po
19167 69 6e 74 73 20 74 6f 20 61 20 62 75 66 66 65 72  ints to a buffer
19168 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
19169 64 61 74 61 20 66 6f 72 20 0a 2a 2a 20 70 61 67  data for .** pag
1916a 65 20 69 53 72 63 50 67 20 66 72 6f 6d 20 74 68  e iSrcPg from th
1916b 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73  e source databas
1916c 65 2e 20 43 6f 70 79 20 74 68 69 73 20 64 61 74  e. Copy this dat
1916d 61 20 69 6e 74 6f 20 74 68 65 20 0a 2a 2a 20 64  a into the .** d
1916e 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62  estination datab
1916f 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ase..*/.static i
19170 6e 74 20 62 61 63 6b 75 70 4f 6e 65 50 61 67 65  nt backupOnePage
19171 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20  (sqlite3_backup 
19172 2a 70 2c 20 50 67 6e 6f 20 69 53 72 63 50 67 2c  *p, Pgno iSrcPg,
19173 20 63 6f 6e 73 74 20 75 38 20 2a 7a 53 72 63 44   const u8 *zSrcD
19174 61 74 61 29 7b 0a 20 20 50 61 67 65 72 20 2a 20  ata){.  Pager * 
19175 63 6f 6e 73 74 20 70 44 65 73 74 50 61 67 65 72  const pDestPager
19176 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50   = sqlite3BtreeP
19177 61 67 65 72 28 70 2d 3e 70 44 65 73 74 29 3b 0a  ager(p->pDest);.
19178 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 53 72 63    const int nSrc
19179 50 67 73 7a 20 3d 20 73 71 6c 69 74 65 33 42 74  Pgsz = sqlite3Bt
1917a 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 70  reeGetPageSize(p
1917b 2d 3e 70 53 72 63 29 3b 0a 20 20 69 6e 74 20 6e  ->pSrc);.  int n
1917c 44 65 73 74 50 67 73 7a 20 3d 20 73 71 6c 69 74  DestPgsz = sqlit
1917d 65 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69  e3BtreeGetPageSi
1917e 7a 65 28 70 2d 3e 70 44 65 73 74 29 3b 0a 20 20  ze(p->pDest);.  
1917f 63 6f 6e 73 74 20 69 6e 74 20 6e 43 6f 70 79 20  const int nCopy 
19180 3d 20 4d 49 4e 28 6e 53 72 63 50 67 73 7a 2c 20  = MIN(nSrcPgsz, 
19181 6e 44 65 73 74 50 67 73 7a 29 3b 0a 20 20 63 6f  nDestPgsz);.  co
19182 6e 73 74 20 69 36 34 20 69 45 6e 64 20 3d 20 28  nst i64 iEnd = (
19183 69 36 34 29 69 53 72 63 50 67 2a 28 69 36 34 29  i64)iSrcPg*(i64)
19184 6e 53 72 63 50 67 73 7a 3b 0a 0a 20 20 69 6e 74  nSrcPgsz;..  int
19185 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
19186 0a 20 20 69 36 34 20 69 4f 66 66 3b 0a 0a 20 20  .  i64 iOff;..  
19187 61 73 73 65 72 74 28 20 70 2d 3e 62 44 65 73 74  assert( p->bDest
19188 4c 6f 63 6b 65 64 20 29 3b 0a 20 20 61 73 73 65  Locked );.  asse
19189 72 74 28 20 21 69 73 46 61 74 61 6c 45 72 72 6f  rt( !isFatalErro
1918a 72 28 70 2d 3e 72 63 29 20 29 3b 0a 20 20 61 73  r(p->rc) );.  as
1918b 73 65 72 74 28 20 69 53 72 63 50 67 21 3d 50 45  sert( iSrcPg!=PE
1918c 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
1918d 70 2d 3e 70 53 72 63 2d 3e 70 42 74 29 20 29 3b  p->pSrc->pBt) );
1918e 0a 20 20 61 73 73 65 72 74 28 20 7a 53 72 63 44  .  assert( zSrcD
1918f 61 74 61 20 29 3b 0a 0a 20 20 2f 2a 20 43 61 74  ata );..  /* Cat
19190 63 68 20 74 68 65 20 63 61 73 65 20 77 68 65 72  ch the case wher
19191 65 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  e the destinatio
19192 6e 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72  n is an in-memor
19193 79 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 74  y database and t
19194 68 65 0a 20 20 2a 2a 20 70 61 67 65 20 73 69 7a  he.  ** page siz
19195 65 73 20 6f 66 20 74 68 65 20 73 6f 75 72 63 65  es of the source
19196 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69 6f 6e   and destination
19197 20 64 69 66 66 65 72 2e 20 0a 20 20 2a 2f 0a 20   differ. .  */. 
19198 20 69 66 28 20 6e 53 72 63 50 67 73 7a 21 3d 6e   if( nSrcPgsz!=n
19199 44 65 73 74 50 67 73 7a 20 26 26 20 73 71 6c 69  DestPgsz && sqli
1919a 74 65 33 50 61 67 65 72 49 73 4d 65 6d 64 62 28  te3PagerIsMemdb(
1919b 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65  sqlite3BtreePage
1919c 72 28 70 2d 3e 70 44 65 73 74 29 29 20 29 7b 0a  r(p->pDest)) ){.
1919d 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1919e 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 0a 20  READONLY;.  }.. 
1919f 20 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20 72 75   /* This loop ru
191a0 6e 73 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68  ns once for each
191a1 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 67   destination pag
191a2 65 20 73 70 61 6e 6e 65 64 20 62 79 20 74 68 65  e spanned by the
191a3 20 73 6f 75 72 63 65 20 0a 20 20 2a 2a 20 70 61   source .  ** pa
191a4 67 65 2e 20 46 6f 72 20 65 61 63 68 20 69 74 65  ge. For each ite
191a5 72 61 74 69 6f 6e 2c 20 76 61 72 69 61 62 6c 65  ration, variable
191a6 20 69 4f 66 66 20 69 73 20 73 65 74 20 74 6f 20   iOff is set to 
191a7 74 68 65 20 62 79 74 65 20 6f 66 66 73 65 74 0a  the byte offset.
191a8 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 73 74    ** of the dest
191a9 69 6e 61 74 69 6f 6e 20 70 61 67 65 2e 0a 20 20  ination page..  
191aa 2a 2f 0a 20 20 66 6f 72 28 69 4f 66 66 3d 69 45  */.  for(iOff=iE
191ab 6e 64 2d 28 69 36 34 29 6e 53 72 63 50 67 73 7a  nd-(i64)nSrcPgsz
191ac 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ; rc==SQLITE_OK 
191ad 26 26 20 69 4f 66 66 3c 69 45 6e 64 3b 20 69 4f  && iOff<iEnd; iO
191ae 66 66 2b 3d 6e 44 65 73 74 50 67 73 7a 29 7b 0a  ff+=nDestPgsz){.
191af 20 20 20 20 44 62 50 61 67 65 20 2a 70 44 65 73      DbPage *pDes
191b0 74 50 67 20 3d 20 30 3b 0a 20 20 20 20 50 67 6e  tPg = 0;.    Pgn
191b1 6f 20 69 44 65 73 74 20 3d 20 28 50 67 6e 6f 29  o iDest = (Pgno)
191b2 28 69 4f 66 66 2f 6e 44 65 73 74 50 67 73 7a 29  (iOff/nDestPgsz)
191b3 2b 31 3b 0a 20 20 20 20 69 66 28 20 69 44 65 73  +1;.    if( iDes
191b4 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  t==PENDING_BYTE_
191b5 50 41 47 45 28 70 2d 3e 70 44 65 73 74 2d 3e 70  PAGE(p->pDest->p
191b6 42 74 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  Bt) ) continue;.
191b7 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f      if( SQLITE_O
191b8 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33  K==(rc = sqlite3
191b9 50 61 67 65 72 47 65 74 28 70 44 65 73 74 50 61  PagerGet(pDestPa
191ba 67 65 72 2c 20 69 44 65 73 74 2c 20 26 70 44 65  ger, iDest, &pDe
191bb 73 74 50 67 29 29 0a 20 20 20 20 20 26 26 20 53  stPg)).     && S
191bc 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20  QLITE_OK==(rc = 
191bd 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
191be 65 28 70 44 65 73 74 50 67 29 29 0a 20 20 20 20  e(pDestPg)).    
191bf 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 75  ){.      const u
191c0 38 20 2a 7a 49 6e 20 3d 20 26 7a 53 72 63 44 61  8 *zIn = &zSrcDa
191c1 74 61 5b 69 4f 66 66 25 6e 53 72 63 50 67 73 7a  ta[iOff%nSrcPgsz
191c2 5d 3b 0a 20 20 20 20 20 20 75 38 20 2a 7a 44 65  ];.      u8 *zDe
191c3 73 74 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33  stData = sqlite3
191c4 50 61 67 65 72 47 65 74 44 61 74 61 28 70 44 65  PagerGetData(pDe
191c5 73 74 50 67 29 3b 0a 20 20 20 20 20 20 75 38 20  stPg);.      u8 
191c6 2a 7a 4f 75 74 20 3d 20 26 7a 44 65 73 74 44 61  *zOut = &zDestDa
191c7 74 61 5b 69 4f 66 66 25 6e 44 65 73 74 50 67 73  ta[iOff%nDestPgs
191c8 7a 5d 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f  z];..      /* Co
191c9 70 79 20 74 68 65 20 64 61 74 61 20 66 72 6f 6d  py the data from
191ca 20 74 68 65 20 73 6f 75 72 63 65 20 70 61 67 65   the source page
191cb 20 69 6e 74 6f 20 74 68 65 20 64 65 73 74 69 6e   into the destin
191cc 61 74 69 6f 6e 20 70 61 67 65 2e 0a 20 20 20 20  ation page..    
191cd 20 20 2a 2a 20 54 68 65 6e 20 63 6c 65 61 72 20    ** Then clear 
191ce 74 68 65 20 42 74 72 65 65 20 6c 61 79 65 72 20  the Btree layer 
191cf 4d 65 6d 50 61 67 65 2e 69 73 49 6e 69 74 20 66  MemPage.isInit f
191d0 6c 61 67 2e 20 42 6f 74 68 20 74 68 69 73 20 6d  lag. Both this m
191d1 6f 64 75 6c 65 0a 20 20 20 20 20 20 2a 2a 20 61  odule.      ** a
191d2 6e 64 20 74 68 65 20 70 61 67 65 72 20 63 6f 64  nd the pager cod
191d3 65 20 75 73 65 20 74 68 69 73 20 74 72 69 63 6b  e use this trick
191d4 20 28 63 6c 65 61 72 69 6e 67 20 74 68 65 20 66   (clearing the f
191d5 69 72 73 74 20 62 79 74 65 0a 20 20 20 20 20 20  irst byte.      
191d6 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 20 27  ** of the page '
191d7 65 78 74 72 61 27 20 73 70 61 63 65 20 74 6f 20  extra' space to 
191d8 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 42  invalidate the B
191d9 74 72 65 65 20 6c 61 79 65 72 73 0a 20 20 20 20  tree layers.    
191da 20 20 2a 2a 20 63 61 63 68 65 64 20 70 61 72 73    ** cached pars
191db 65 20 6f 66 20 74 68 65 20 70 61 67 65 29 2e 20  e of the page). 
191dc 4d 65 6d 50 61 67 65 2e 69 73 49 6e 69 74 20 69  MemPage.isInit i
191dd 73 20 6d 61 72 6b 65 64 20 0a 20 20 20 20 20 20  s marked .      
191de 2a 2a 20 22 4d 55 53 54 20 42 45 20 46 49 52 53  ** "MUST BE FIRS
191df 54 22 20 66 6f 72 20 74 68 69 73 20 70 75 72 70  T" for this purp
191e0 6f 73 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ose..      */.  
191e1 20 20 20 20 6d 65 6d 63 70 79 28 7a 4f 75 74 2c      memcpy(zOut,
191e2 20 7a 49 6e 2c 20 6e 43 6f 70 79 29 3b 0a 20 20   zIn, nCopy);.  
191e3 20 20 20 20 28 28 75 38 20 2a 29 73 71 6c 69 74      ((u8 *)sqlit
191e4 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28  e3PagerGetExtra(
191e5 70 44 65 73 74 50 67 29 29 5b 30 5d 20 3d 20 30  pDestPg))[0] = 0
191e6 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
191e7 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 44  te3PagerUnref(pD
191e8 65 73 74 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 72  estPg);.  }..  r
191e9 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
191ea 2a 2a 20 49 66 20 70 46 69 6c 65 20 69 73 20 63  ** If pFile is c
191eb 75 72 72 65 6e 74 6c 79 20 6c 61 72 67 65 72 20  urrently larger 
191ec 74 68 61 6e 20 69 53 69 7a 65 20 62 79 74 65 73  than iSize bytes
191ed 2c 20 74 68 65 6e 20 74 72 75 6e 63 61 74 65 20  , then truncate 
191ee 69 74 20 74 6f 0a 2a 2a 20 65 78 61 63 74 6c 79  it to.** exactly
191ef 20 69 53 69 7a 65 20 62 79 74 65 73 2e 20 49 66   iSize bytes. If
191f0 20 70 46 69 6c 65 20 69 73 20 6e 6f 74 20 6c 61   pFile is not la
191f1 72 67 65 72 20 74 68 61 6e 20 69 53 69 7a 65 20  rger than iSize 
191f2 62 79 74 65 73 2c 20 74 68 65 6e 0a 2a 2a 20 74  bytes, then.** t
191f3 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
191f4 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 52  a no-op..**.** R
191f5 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
191f6 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 73  if everything is
191f7 20 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 72 20   successful, or 
191f8 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20  an SQLite error 
191f9 0a 2a 2a 20 63 6f 64 65 20 69 66 20 61 6e 20 65  .** code if an e
191fa 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a  rror occurs..*/.
191fb 73 74 61 74 69 63 20 69 6e 74 20 62 61 63 6b 75  static int backu
191fc 70 54 72 75 6e 63 61 74 65 46 69 6c 65 28 73 71  pTruncateFile(sq
191fd 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c  lite3_file *pFil
191fe 65 2c 20 69 36 34 20 69 53 69 7a 65 29 7b 0a 20  e, i64 iSize){. 
191ff 20 69 36 34 20 69 43 75 72 72 65 6e 74 3b 0a 20   i64 iCurrent;. 
19200 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65   int rc = sqlite
19201 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 46 69 6c  3OsFileSize(pFil
19202 65 2c 20 26 69 43 75 72 72 65 6e 74 29 3b 0a 20  e, &iCurrent);. 
19203 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
19204 4f 4b 20 26 26 20 69 43 75 72 72 65 6e 74 3e 69  OK && iCurrent>i
19205 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 63 20 3d  Size ){.    rc =
19206 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61   sqlite3OsTrunca
19207 74 65 28 70 46 69 6c 65 2c 20 69 53 69 7a 65 29  te(pFile, iSize)
19208 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
19209 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69  c;.}../*.** Regi
1920a 73 74 65 72 20 74 68 69 73 20 62 61 63 6b 75 70  ster this backup
1920b 20 6f 62 6a 65 63 74 20 77 69 74 68 20 74 68 65   object with the
1920c 20 61 73 73 6f 63 69 61 74 65 64 20 73 6f 75 72   associated sour
1920d 63 65 20 70 61 67 65 72 20 66 6f 72 0a 2a 2a 20  ce pager for.** 
1920e 63 61 6c 6c 62 61 63 6b 73 20 77 68 65 6e 20 70  callbacks when p
1920f 61 67 65 73 20 61 72 65 20 63 68 61 6e 67 65 64  ages are changed
19210 20 6f 72 20 74 68 65 20 63 61 63 68 65 20 69 6e   or the cache in
19211 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2f 0a 73 74  validated..*/.st
19212 61 74 69 63 20 76 6f 69 64 20 61 74 74 61 63 68  atic void attach
19213 42 61 63 6b 75 70 4f 62 6a 65 63 74 28 73 71 6c  BackupObject(sql
19214 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 7b  ite3_backup *p){
19215 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  .  sqlite3_backu
19216 70 20 2a 2a 70 70 3b 0a 20 20 61 73 73 65 72 74  p **pp;.  assert
19217 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f  ( sqlite3BtreeHo
19218 6c 64 73 4d 75 74 65 78 28 70 2d 3e 70 53 72 63  ldsMutex(p->pSrc
19219 29 20 29 3b 0a 20 20 70 70 20 3d 20 73 71 6c 69  ) );.  pp = sqli
1921a 74 65 33 50 61 67 65 72 42 61 63 6b 75 70 50 74  te3PagerBackupPt
1921b 72 28 73 71 6c 69 74 65 33 42 74 72 65 65 50 61  r(sqlite3BtreePa
1921c 67 65 72 28 70 2d 3e 70 53 72 63 29 29 3b 0a 20  ger(p->pSrc));. 
1921d 20 70 2d 3e 70 4e 65 78 74 20 3d 20 2a 70 70 3b   p->pNext = *pp;
1921e 0a 20 20 2a 70 70 20 3d 20 70 3b 0a 20 20 70 2d  .  *pp = p;.  p-
1921f 3e 69 73 41 74 74 61 63 68 65 64 20 3d 20 31 3b  >isAttached = 1;
19220 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 6e  .}../*.** Copy n
19221 50 61 67 65 20 70 61 67 65 73 20 66 72 6f 6d 20  Page pages from 
19222 74 68 65 20 73 6f 75 72 63 65 20 62 2d 74 72 65  the source b-tre
19223 65 20 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61  e to the destina
19224 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
19225 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
19226 62 61 63 6b 75 70 5f 73 74 65 70 28 73 71 6c 69  backup_step(sqli
19227 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 2c 20 69  te3_backup *p, i
19228 6e 74 20 6e 50 61 67 65 29 7b 0a 20 20 69 6e 74  nt nPage){.  int
19229 20 72 63 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f   rc;..  sqlite3_
1922a 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70  mutex_enter(p->p
1922b 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  SrcDb->mutex);. 
1922c 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
1922d 65 72 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 69  er(p->pSrc);.  i
1922e 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b  f( p->pDestDb ){
1922f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
19230 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 44 65 73  ex_enter(p->pDes
19231 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d  tDb->mutex);.  }
19232 0a 0a 20 20 72 63 20 3d 20 70 2d 3e 72 63 3b 0a  ..  rc = p->rc;.
19233 20 20 69 66 28 20 21 69 73 46 61 74 61 6c 45 72    if( !isFatalEr
19234 72 6f 72 28 72 63 29 20 29 7b 0a 20 20 20 20 50  ror(rc) ){.    P
19235 61 67 65 72 20 2a 20 63 6f 6e 73 74 20 70 53 72  ager * const pSr
19236 63 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33  cPager = sqlite3
19237 42 74 72 65 65 50 61 67 65 72 28 70 2d 3e 70 53  BtreePager(p->pS
19238 72 63 29 3b 20 20 20 20 20 2f 2a 20 53 6f 75 72  rc);     /* Sour
19239 63 65 20 70 61 67 65 72 20 2a 2f 0a 20 20 20 20  ce pager */.    
1923a 50 61 67 65 72 20 2a 20 63 6f 6e 73 74 20 70 44  Pager * const pD
1923b 65 73 74 50 61 67 65 72 20 3d 20 73 71 6c 69 74  estPager = sqlit
1923c 65 33 42 74 72 65 65 50 61 67 65 72 28 70 2d 3e  e3BtreePager(p->
1923d 70 44 65 73 74 29 3b 20 20 20 2f 2a 20 44 65 73  pDest);   /* Des
1923e 74 20 70 61 67 65 72 20 2a 2f 0a 20 20 20 20 69  t pager */.    i
1923f 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20  nt ii;          
19240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19241 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61    /* Iterator va
19242 72 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e  riable */.    in
19243 74 20 6e 53 72 63 50 61 67 65 20 3d 20 2d 31 3b  t nSrcPage = -1;
19244 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19245 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 6f 75 72   /* Size of sour
19246 63 65 20 64 62 20 69 6e 20 70 61 67 65 73 20 2a  ce db in pages *
19247 2f 0a 20 20 20 20 69 6e 74 20 62 43 6c 6f 73 65  /.    int bClose
19248 54 72 61 6e 73 20 3d 20 30 3b 20 20 20 20 20 20  Trans = 0;      
19249 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
1924a 20 69 66 20 73 72 63 20 64 62 20 72 65 71 75 69   if src db requi
1924b 72 65 73 20 75 6e 6c 6f 63 6b 69 6e 67 20 2a 2f  res unlocking */
1924c 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20  ..    /* If the 
1924d 73 6f 75 72 63 65 20 70 61 67 65 72 20 69 73 20  source pager is 
1924e 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 61 20 77  currently in a w
1924f 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e  rite-transaction
19250 2c 20 72 65 74 75 72 6e 0a 20 20 20 20 2a 2a 20  , return.    ** 
19251 53 51 4c 49 54 45 5f 42 55 53 59 20 69 6d 6d 65  SQLITE_BUSY imme
19252 64 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2f 0a  diately..    */.
19253 20 20 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74      if( p->pDest
19254 44 62 20 26 26 20 70 2d 3e 70 53 72 63 2d 3e 70  Db && p->pSrc->p
19255 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f  Bt->inTransactio
19256 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29  n==TRANS_WRITE )
19257 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
19258 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 65  ITE_BUSY;.    }e
19259 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  lse{.      rc = 
1925a 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d  SQLITE_OK;.    }
1925b 0a 0a 20 20 20 20 2f 2a 20 4c 6f 63 6b 20 74 68  ..    /* Lock th
1925c 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61  e destination da
1925d 74 61 62 61 73 65 2c 20 69 66 20 69 74 20 69 73  tabase, if it is
1925e 20 6e 6f 74 20 6c 6f 63 6b 65 64 20 61 6c 72 65   not locked alre
1925f 61 64 79 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  ady. */.    if( 
19260 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20 26 26  SQLITE_OK==rc &&
19261 20 70 2d 3e 62 44 65 73 74 4c 6f 63 6b 65 64 3d   p->bDestLocked=
19262 3d 30 0a 20 20 20 20 20 26 26 20 53 51 4c 49 54  =0.     && SQLIT
19263 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69  E_OK==(rc = sqli
19264 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 61  te3BtreeBeginTra
19265 6e 73 28 70 2d 3e 70 44 65 73 74 2c 20 32 29 29  ns(p->pDest, 2))
19266 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 70   .    ){.      p
19267 2d 3e 62 44 65 73 74 4c 6f 63 6b 65 64 20 3d 20  ->bDestLocked = 
19268 31 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  1;.      sqlite3
19269 42 74 72 65 65 47 65 74 4d 65 74 61 28 70 2d 3e  BtreeGetMeta(p->
1926a 70 44 65 73 74 2c 20 42 54 52 45 45 5f 53 43 48  pDest, BTREE_SCH
1926b 45 4d 41 5f 56 45 52 53 49 4f 4e 2c 20 26 70 2d  EMA_VERSION, &p-
1926c 3e 69 44 65 73 74 53 63 68 65 6d 61 29 3b 0a 20  >iDestSchema);. 
1926d 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20     }..    /* If 
1926e 74 68 65 72 65 20 69 73 20 6e 6f 20 6f 70 65 6e  there is no open
1926f 20 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69 6f   read-transactio
19270 6e 20 6f 6e 20 74 68 65 20 73 6f 75 72 63 65 20  n on the source 
19271 64 61 74 61 62 61 73 65 2c 20 6f 70 65 6e 0a 20  database, open. 
19272 20 20 20 2a 2a 20 6f 6e 65 20 6e 6f 77 2e 20 49     ** one now. I
19273 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  f a transaction 
19274 69 73 20 6f 70 65 6e 65 64 20 68 65 72 65 2c 20  is opened here, 
19275 74 68 65 6e 20 69 74 20 77 69 6c 6c 20 62 65 20  then it will be 
19276 63 6c 6f 73 65 64 0a 20 20 20 20 2a 2a 20 62 65  closed.    ** be
19277 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 69  fore this functi
19278 6f 6e 20 65 78 69 74 73 2e 0a 20 20 20 20 2a 2f  on exits..    */
19279 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
1927a 49 54 45 5f 4f 4b 20 26 26 20 30 3d 3d 73 71 6c  ITE_OK && 0==sql
1927b 69 74 65 33 42 74 72 65 65 49 73 49 6e 52 65 61  ite3BtreeIsInRea
1927c 64 54 72 61 6e 73 28 70 2d 3e 70 53 72 63 29 20  dTrans(p->pSrc) 
1927d 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ){.      rc = sq
1927e 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54  lite3BtreeBeginT
1927f 72 61 6e 73 28 70 2d 3e 70 53 72 63 2c 20 30 29  rans(p->pSrc, 0)
19280 3b 0a 20 20 20 20 20 20 62 43 6c 6f 73 65 54 72  ;.      bCloseTr
19281 61 6e 73 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  ans = 1;.    }. 
19282 20 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 74 68 61   .    /* Now tha
19283 74 20 74 68 65 72 65 20 69 73 20 61 20 72 65 61  t there is a rea
19284 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 73 6f  d-lock on the so
19285 75 72 63 65 20 64 61 74 61 62 61 73 65 2c 20 71  urce database, q
19286 75 65 72 79 20 74 68 65 0a 20 20 20 20 2a 2a 20  uery the.    ** 
19287 73 6f 75 72 63 65 20 70 61 67 65 72 20 66 6f 72  source pager for
19288 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
19289 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61  ages in the data
1928a 62 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  base..    */.   
1928b 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1928c 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  OK ){.      rc =
1928d 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
1928e 65 63 6f 75 6e 74 28 70 53 72 63 50 61 67 65 72  ecount(pSrcPager
1928f 2c 20 26 6e 53 72 63 50 61 67 65 29 3b 0a 20 20  , &nSrcPage);.  
19290 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 69 3d 30    }.    for(ii=0
19291 3b 20 28 6e 50 61 67 65 3c 30 20 7c 7c 20 69 69  ; (nPage<0 || ii
19292 3c 6e 50 61 67 65 29 20 26 26 20 70 2d 3e 69 4e  <nPage) && p->iN
19293 65 78 74 3c 3d 28 50 67 6e 6f 29 6e 53 72 63 50  ext<=(Pgno)nSrcP
19294 61 67 65 20 26 26 20 21 72 63 3b 20 69 69 2b 2b  age && !rc; ii++
19295 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 50  ){.      const P
19296 67 6e 6f 20 69 53 72 63 50 67 20 3d 20 70 2d 3e  gno iSrcPg = p->
19297 69 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20  iNext;          
19298 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65         /* Source
19299 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a   page number */.
1929a 20 20 20 20 20 20 69 66 28 20 69 53 72 63 50 67        if( iSrcPg
1929b 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  !=PENDING_BYTE_P
1929c 41 47 45 28 70 2d 3e 70 53 72 63 2d 3e 70 42 74  AGE(p->pSrc->pBt
1929d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 44 62 50  ) ){.        DbP
1929e 61 67 65 20 2a 70 53 72 63 50 67 3b 20 20 20 20  age *pSrcPg;    
1929f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
192a0 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72           /* Sour
192a1 63 65 20 70 61 67 65 20 6f 62 6a 65 63 74 20 2a  ce page object *
192a2 2f 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  /.        rc = s
192a3 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70  qlite3PagerGet(p
192a4 53 72 63 50 61 67 65 72 2c 20 69 53 72 63 50 67  SrcPager, iSrcPg
192a5 2c 20 26 70 53 72 63 50 67 29 3b 0a 20 20 20 20  , &pSrcPg);.    
192a6 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
192a7 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
192a8 20 20 20 72 63 20 3d 20 62 61 63 6b 75 70 4f 6e     rc = backupOn
192a9 65 50 61 67 65 28 70 2c 20 69 53 72 63 50 67 2c  ePage(p, iSrcPg,
192aa 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
192ab 44 61 74 61 28 70 53 72 63 50 67 29 29 3b 0a 20  Data(pSrcPg));. 
192ac 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
192ad 50 61 67 65 72 55 6e 72 65 66 28 70 53 72 63 50  PagerUnref(pSrcP
192ae 67 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  g);.        }.  
192af 20 20 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e 69      }.      p->i
192b0 4e 65 78 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20  Next++;.    }.  
192b1 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
192b2 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  _OK ){.      p->
192b3 6e 50 61 67 65 63 6f 75 6e 74 20 3d 20 6e 53 72  nPagecount = nSr
192b4 63 50 61 67 65 3b 0a 20 20 20 20 20 20 70 2d 3e  cPage;.      p->
192b5 6e 52 65 6d 61 69 6e 69 6e 67 20 3d 20 6e 53 72  nRemaining = nSr
192b6 63 50 61 67 65 2b 31 2d 70 2d 3e 69 4e 65 78 74  cPage+1-p->iNext
192b7 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69  ;.      if( p->i
192b8 4e 65 78 74 3e 28 50 67 6e 6f 29 6e 53 72 63 50  Next>(Pgno)nSrcP
192b9 61 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72  age ){.        r
192ba 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b  c = SQLITE_DONE;
192bb 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
192bc 20 21 70 2d 3e 69 73 41 74 74 61 63 68 65 64 20   !p->isAttached 
192bd 29 7b 0a 20 20 20 20 20 20 20 20 61 74 74 61 63  ){.        attac
192be 68 42 61 63 6b 75 70 4f 62 6a 65 63 74 28 70 29  hBackupObject(p)
192bf 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
192c0 20 20 0a 20 20 20 20 2f 2a 20 55 70 64 61 74 65    .    /* Update
192c1 20 74 68 65 20 73 63 68 65 6d 61 20 76 65 72 73   the schema vers
192c2 69 6f 6e 20 66 69 65 6c 64 20 69 6e 20 74 68 65  ion field in the
192c3 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74   destination dat
192c4 61 62 61 73 65 2e 20 54 68 69 73 0a 20 20 20 20  abase. This.    
192c5 2a 2a 20 69 73 20 74 6f 20 6d 61 6b 65 20 73 75  ** is to make su
192c6 72 65 20 74 68 61 74 20 74 68 65 20 73 63 68 65  re that the sche
192c7 6d 61 2d 76 65 72 73 69 6f 6e 20 72 65 61 6c 6c  ma-version reall
192c8 79 20 64 6f 65 73 20 63 68 61 6e 67 65 20 69 6e  y does change in
192c9 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 61 73 65  .    ** the case
192ca 20 77 68 65 72 65 20 74 68 65 20 73 6f 75 72 63   where the sourc
192cb 65 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69 6f  e and destinatio
192cc 6e 20 64 61 74 61 62 61 73 65 73 20 68 61 76 65  n databases have
192cd 20 74 68 65 0a 20 20 20 20 2a 2a 20 73 61 6d 65   the.    ** same
192ce 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e   schema version.
192cf 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
192d0 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc==SQLITE_DONE 
192d1 0a 20 20 20 20 20 26 26 20 28 72 63 20 3d 20 73  .     && (rc = s
192d2 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74  qlite3BtreeUpdat
192d3 65 4d 65 74 61 28 70 2d 3e 70 44 65 73 74 2c 31  eMeta(p->pDest,1
192d4 2c 70 2d 3e 69 44 65 73 74 53 63 68 65 6d 61 2b  ,p->iDestSchema+
192d5 31 29 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20  1))==SQLITE_OK. 
192d6 20 20 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73     ){.      cons
192d7 74 20 69 6e 74 20 6e 53 72 63 50 61 67 65 73 69  t int nSrcPagesi
192d8 7a 65 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  ze = sqlite3Btre
192d9 65 47 65 74 50 61 67 65 53 69 7a 65 28 70 2d 3e  eGetPageSize(p->
192da 70 53 72 63 29 3b 0a 20 20 20 20 20 20 63 6f 6e  pSrc);.      con
192db 73 74 20 69 6e 74 20 6e 44 65 73 74 50 61 67 65  st int nDestPage
192dc 73 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 42 74  size = sqlite3Bt
192dd 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 70  reeGetPageSize(p
192de 2d 3e 70 44 65 73 74 29 3b 0a 20 20 20 20 20 20  ->pDest);.      
192df 69 6e 74 20 6e 44 65 73 74 54 72 75 6e 63 61 74  int nDestTruncat
192e0 65 3b 0a 20 20 0a 20 20 20 20 20 20 69 66 28 20  e;.  .      if( 
192e1 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 20  p->pDestDb ){.  
192e2 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 73        sqlite3Res
192e3 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61  etInternalSchema
192e4 28 70 2d 3e 70 44 65 73 74 44 62 2c 20 30 29 3b  (p->pDestDb, 0);
192e5 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
192e6 2f 2a 20 53 65 74 20 6e 44 65 73 74 54 72 75 6e  /* Set nDestTrun
192e7 63 61 74 65 20 74 6f 20 74 68 65 20 66 69 6e 61  cate to the fina
192e8 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  l number of page
192e9 73 20 69 6e 20 74 68 65 20 64 65 73 74 69 6e 61  s in the destina
192ea 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 64 61  tion.      ** da
192eb 74 61 62 61 73 65 2e 20 54 68 65 20 63 6f 6d 70  tabase. The comp
192ec 6c 69 63 61 74 69 6f 6e 20 68 65 72 65 20 69 73  lication here is
192ed 20 74 68 61 74 20 74 68 65 20 64 65 73 74 69 6e   that the destin
192ee 61 74 69 6f 6e 20 70 61 67 65 0a 20 20 20 20 20  ation page.     
192ef 20 2a 2a 20 73 69 7a 65 20 6d 61 79 20 62 65 20   ** size may be 
192f0 64 69 66 66 65 72 65 6e 74 20 74 6f 20 74 68 65  different to the
192f1 20 73 6f 75 72 63 65 20 70 61 67 65 20 73 69 7a   source page siz
192f2 65 2e 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  e. .      **.   
192f3 20 20 20 2a 2a 20 49 66 20 74 68 65 20 73 6f 75     ** If the sou
192f4 72 63 65 20 70 61 67 65 20 73 69 7a 65 20 69 73  rce page size is
192f5 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74 68   smaller than th
192f6 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61  e destination pa
192f7 67 65 20 73 69 7a 65 2c 20 0a 20 20 20 20 20 20  ge size, .      
192f8 2a 2a 20 72 6f 75 6e 64 20 75 70 2e 20 49 6e 20  ** round up. In 
192f9 74 68 69 73 20 63 61 73 65 20 74 68 65 20 63 61  this case the ca
192fa 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 54  ll to sqlite3OsT
192fb 72 75 6e 63 61 74 65 28 29 20 62 65 6c 6f 77 20  runcate() below 
192fc 77 69 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 66 69  will.      ** fi
192fd 78 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  x the size of th
192fe 65 20 66 69 6c 65 2e 20 48 6f 77 65 76 65 72 20  e file. However 
192ff 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20  it is important 
19300 74 6f 20 63 61 6c 6c 0a 20 20 20 20 20 20 2a 2a  to call.      **
19301 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 75   sqlite3PagerTru
19302 6e 63 61 74 65 49 6d 61 67 65 28 29 20 68 65 72  ncateImage() her
19303 65 20 73 6f 20 74 68 61 74 20 61 6e 79 20 70 61  e so that any pa
19304 67 65 73 20 69 6e 20 74 68 65 20 0a 20 20 20 20  ges in the .    
19305 20 20 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e    ** destination
19306 20 66 69 6c 65 20 74 68 61 74 20 6c 69 65 20 62   file that lie b
19307 65 79 6f 6e 64 20 74 68 65 20 6e 44 65 73 74 54  eyond the nDestT
19308 72 75 6e 63 61 74 65 20 70 61 67 65 20 6d 61 72  runcate page mar
19309 6b 20 61 72 65 0a 20 20 20 20 20 20 2a 2a 20 6a  k are.      ** j
1930a 6f 75 72 6e 61 6c 6c 65 64 20 62 79 20 50 61 67  ournalled by Pag
1930b 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65  erCommitPhaseOne
1930c 28 29 20 62 65 66 6f 72 65 20 74 68 65 79 20 61  () before they a
1930d 72 65 20 64 65 73 74 72 6f 79 65 64 0a 20 20 20  re destroyed.   
1930e 20 20 20 2a 2a 20 62 79 20 74 68 65 20 66 69 6c     ** by the fil
1930f 65 20 74 72 75 6e 63 61 74 69 6f 6e 2e 0a 20 20  e truncation..  
19310 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
19311 20 6e 53 72 63 50 61 67 65 73 69 7a 65 3c 6e 44   nSrcPagesize<nD
19312 65 73 74 50 61 67 65 73 69 7a 65 20 29 7b 0a 20  estPagesize ){. 
19313 20 20 20 20 20 20 20 69 6e 74 20 72 61 74 69 6f         int ratio
19314 20 3d 20 6e 44 65 73 74 50 61 67 65 73 69 7a 65   = nDestPagesize
19315 2f 6e 53 72 63 50 61 67 65 73 69 7a 65 3b 0a 20  /nSrcPagesize;. 
19316 20 20 20 20 20 20 20 6e 44 65 73 74 54 72 75 6e         nDestTrun
19317 63 61 74 65 20 3d 20 28 6e 53 72 63 50 61 67 65  cate = (nSrcPage
19318 2b 72 61 74 69 6f 2d 31 29 2f 72 61 74 69 6f 3b  +ratio-1)/ratio;
19319 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 44 65  .        if( nDe
1931a 73 74 54 72 75 6e 63 61 74 65 3d 3d 28 69 6e 74  stTruncate==(int
1931b 29 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41  )PENDING_BYTE_PA
1931c 47 45 28 70 2d 3e 70 44 65 73 74 2d 3e 70 42 74  GE(p->pDest->pBt
1931d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e  ) ){.          n
1931e 44 65 73 74 54 72 75 6e 63 61 74 65 2d 2d 3b 0a  DestTruncate--;.
1931f 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
19320 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6e  }else{.        n
19321 44 65 73 74 54 72 75 6e 63 61 74 65 20 3d 20 6e  DestTruncate = n
19322 53 72 63 50 61 67 65 20 2a 20 28 6e 53 72 63 50  SrcPage * (nSrcP
19323 61 67 65 73 69 7a 65 2f 6e 44 65 73 74 50 61 67  agesize/nDestPag
19324 65 73 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d 0a  esize);.      }.
19325 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67        sqlite3Pag
19326 65 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28  erTruncateImage(
19327 70 44 65 73 74 50 61 67 65 72 2c 20 6e 44 65 73  pDestPager, nDes
19328 74 54 72 75 6e 63 61 74 65 29 3b 0a 0a 20 20 20  tTruncate);..   
19329 20 20 20 69 66 28 20 6e 53 72 63 50 61 67 65 73     if( nSrcPages
1932a 69 7a 65 3c 6e 44 65 73 74 50 61 67 65 73 69 7a  ize<nDestPagesiz
1932b 65 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  e ){.        /* 
1932c 49 66 20 74 68 65 20 73 6f 75 72 63 65 20 70 61  If the source pa
1932d 67 65 2d 73 69 7a 65 20 69 73 20 73 6d 61 6c 6c  ge-size is small
1932e 65 72 20 74 68 61 6e 20 74 68 65 20 64 65 73 74  er than the dest
1932f 69 6e 61 74 69 6f 6e 20 70 61 67 65 2d 73 69 7a  ination page-siz
19330 65 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 77  e,.        ** tw
19331 6f 20 65 78 74 72 61 20 74 68 69 6e 67 73 20 6d  o extra things m
19332 61 79 20 6e 65 65 64 20 74 6f 20 68 61 70 70 65  ay need to happe
19333 6e 3a 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20  n:.        **.  
19334 20 20 20 20 20 20 2a 2a 20 20 20 2a 20 54 68 65        **   * The
19335 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 6d 61 79   destination may
19336 20 6e 65 65 64 20 74 6f 20 62 65 20 74 72 75 6e   need to be trun
19337 63 61 74 65 64 2c 20 61 6e 64 0a 20 20 20 20 20  cated, and.     
19338 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
19339 20 20 20 2a 20 44 61 74 61 20 73 74 6f 72 65 64     * Data stored
1933a 20 6f 6e 20 74 68 65 20 70 61 67 65 73 20 69 6d   on the pages im
1933b 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77  mediately follow
1933c 69 6e 67 20 74 68 65 20 0a 20 20 20 20 20 20 20  ing the .       
1933d 20 2a 2a 20 20 20 20 20 70 65 6e 64 69 6e 67 2d   **     pending-
1933e 62 79 74 65 20 70 61 67 65 20 69 6e 20 74 68 65  byte page in the
1933f 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65   source database
19340 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 62 65 0a   may need to be.
19341 20 20 20 20 20 20 20 20 2a 2a 20 20 20 20 20 63          **     c
19342 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20 64  opied into the d
19343 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62  estination datab
19344 61 73 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  ase..        */.
19345 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 69 36          const i6
19346 34 20 69 53 69 7a 65 20 3d 20 28 69 36 34 29 6e  4 iSize = (i64)n
19347 53 72 63 50 61 67 65 73 69 7a 65 20 2a 20 28 69  SrcPagesize * (i
19348 36 34 29 6e 53 72 63 50 61 67 65 3b 0a 20 20 20  64)nSrcPage;.   
19349 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c       sqlite3_fil
1934a 65 20 2a 20 63 6f 6e 73 74 20 70 46 69 6c 65 20  e * const pFile 
1934b 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 46 69  = sqlite3PagerFi
1934c 6c 65 28 70 44 65 73 74 50 61 67 65 72 29 3b 0a  le(pDestPager);.
1934d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1934e 20 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 20 20   pFile );.      
1934f 20 20 61 73 73 65 72 74 28 20 28 69 36 34 29 6e    assert( (i64)n
19350 44 65 73 74 54 72 75 6e 63 61 74 65 2a 28 69 36  DestTruncate*(i6
19351 34 29 6e 44 65 73 74 50 61 67 65 73 69 7a 65 20  4)nDestPagesize 
19352 3e 3d 20 69 53 69 7a 65 20 7c 7c 20 28 0a 20 20  >= iSize || (.  
19353 20 20 20 20 20 20 20 20 20 20 20 20 6e 44 65 73              nDes
19354 74 54 72 75 6e 63 61 74 65 3d 3d 28 69 6e 74 29  tTruncate==(int)
19355 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41  (PENDING_BYTE_PA
19356 47 45 28 70 2d 3e 70 44 65 73 74 2d 3e 70 42 74  GE(p->pDest->pBt
19357 29 2d 31 29 0a 20 20 20 20 20 20 20 20 20 20 20  )-1).           
19358 26 26 20 69 53 69 7a 65 3e 3d 50 45 4e 44 49 4e  && iSize>=PENDIN
19359 47 5f 42 59 54 45 20 26 26 20 69 53 69 7a 65 3c  G_BYTE && iSize<
1935a 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 6e 44  =PENDING_BYTE+nD
1935b 65 73 74 50 61 67 65 73 69 7a 65 0a 20 20 20 20  estPagesize.    
1935c 20 20 20 20 29 29 3b 0a 20 20 20 20 20 20 20 20      ));.        
1935d 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28  if( SQLITE_OK==(
1935e 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
1935f 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28  rCommitPhaseOne(
19360 70 44 65 73 74 50 61 67 65 72 2c 20 30 2c 20 31  pDestPager, 0, 1
19361 29 29 0a 20 20 20 20 20 20 20 20 20 26 26 20 53  )).         && S
19362 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20  QLITE_OK==(rc = 
19363 62 61 63 6b 75 70 54 72 75 6e 63 61 74 65 46 69  backupTruncateFi
19364 6c 65 28 70 46 69 6c 65 2c 20 69 53 69 7a 65 29  le(pFile, iSize)
19365 29 0a 20 20 20 20 20 20 20 20 20 26 26 20 53 51  ).         && SQ
19366 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73  LITE_OK==(rc = s
19367 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63 28  qlite3PagerSync(
19368 70 44 65 73 74 50 61 67 65 72 29 29 0a 20 20 20  pDestPager)).   
19369 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20       ){.        
1936a 20 20 69 36 34 20 69 4f 66 66 3b 0a 20 20 20 20    i64 iOff;.    
1936b 20 20 20 20 20 20 69 36 34 20 69 45 6e 64 20 3d        i64 iEnd =
1936c 20 4d 49 4e 28 50 45 4e 44 49 4e 47 5f 42 59 54   MIN(PENDING_BYT
1936d 45 20 2b 20 6e 44 65 73 74 50 61 67 65 73 69 7a  E + nDestPagesiz
1936e 65 2c 20 69 53 69 7a 65 29 3b 0a 20 20 20 20 20  e, iSize);.     
1936f 20 20 20 20 20 66 6f 72 28 0a 20 20 20 20 20 20       for(.      
19370 20 20 20 20 20 20 69 4f 66 66 3d 50 45 4e 44 49        iOff=PENDI
19371 4e 47 5f 42 59 54 45 2b 6e 53 72 63 50 61 67 65  NG_BYTE+nSrcPage
19372 73 69 7a 65 3b 20 0a 20 20 20 20 20 20 20 20 20  size; .         
19373 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b     rc==SQLITE_OK
19374 20 26 26 20 69 4f 66 66 3c 69 45 6e 64 3b 20 0a   && iOff<iEnd; .
19375 20 20 20 20 20 20 20 20 20 20 20 20 69 4f 66 66              iOff
19376 2b 3d 6e 53 72 63 50 61 67 65 73 69 7a 65 0a 20  +=nSrcPagesize. 
19377 20 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20           ){.    
19378 20 20 20 20 20 20 20 20 50 67 48 64 72 20 2a 70          PgHdr *p
19379 53 72 63 50 67 20 3d 20 30 3b 0a 20 20 20 20 20  SrcPg = 0;.     
1937a 20 20 20 20 20 20 20 63 6f 6e 73 74 20 50 67 6e         const Pgn
1937b 6f 20 69 53 72 63 50 67 20 3d 20 28 50 67 6e 6f  o iSrcPg = (Pgno
1937c 29 28 28 69 4f 66 66 2f 6e 53 72 63 50 61 67 65  )((iOff/nSrcPage
1937d 73 69 7a 65 29 2b 31 29 3b 0a 20 20 20 20 20 20  size)+1);.      
1937e 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1937f 65 33 50 61 67 65 72 47 65 74 28 70 53 72 63 50  e3PagerGet(pSrcP
19380 61 67 65 72 2c 20 69 53 72 63 50 67 2c 20 26 70  ager, iSrcPg, &p
19381 53 72 63 50 67 29 3b 0a 20 20 20 20 20 20 20 20  SrcPg);.        
19382 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
19383 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
19384 20 20 20 20 20 20 20 75 38 20 2a 7a 44 61 74 61         u8 *zData
19385 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47   = sqlite3PagerG
19386 65 74 44 61 74 61 28 70 53 72 63 50 67 29 3b 0a  etData(pSrcPg);.
19387 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 63                rc
19388 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74   = sqlite3OsWrit
19389 65 28 70 46 69 6c 65 2c 20 7a 44 61 74 61 2c 20  e(pFile, zData, 
1938a 6e 53 72 63 50 61 67 65 73 69 7a 65 2c 20 69 4f  nSrcPagesize, iO
1938b 66 66 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ff);.           
1938c 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 73   }.            s
1938d 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
1938e 28 70 53 72 63 50 67 29 3b 0a 20 20 20 20 20 20  (pSrcPg);.      
1938f 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
19390 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
19391 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
19392 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73  3PagerCommitPhas
19393 65 4f 6e 65 28 70 44 65 73 74 50 61 67 65 72 2c  eOne(pDestPager,
19394 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a   0, 0);.      }.
19395 20 20 0a 20 20 20 20 20 20 2f 2a 20 46 69 6e 69    .      /* Fini
19396 73 68 20 63 6f 6d 6d 69 74 74 69 6e 67 20 74 68  sh committing th
19397 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f  e transaction to
19398 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
19399 20 64 61 74 61 62 61 73 65 2e 20 2a 2f 0a 20 20   database. */.  
1939a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f      if( SQLITE_O
1939b 4b 3d 3d 72 63 0a 20 20 20 20 20 20 20 26 26 20  K==rc.       && 
1939c 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d  SQLITE_OK==(rc =
1939d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d   sqlite3BtreeCom
1939e 6d 69 74 50 68 61 73 65 54 77 6f 28 70 2d 3e 70  mitPhaseTwo(p->p
1939f 44 65 73 74 29 29 0a 20 20 20 20 20 20 29 7b 0a  Dest)).      ){.
193a0 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
193a1 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20 20  ITE_DONE;.      
193a2 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f  }.    }.  .    /
193a3 2a 20 49 66 20 62 43 6c 6f 73 65 54 72 61 6e 73  * If bCloseTrans
193a4 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74   is true, then t
193a5 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6f 70 65  his function ope
193a6 6e 65 64 20 61 20 72 65 61 64 20 74 72 61 6e 73  ned a read trans
193a7 61 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 6f 6e  action.    ** on
193a8 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61   the source data
193a9 62 61 73 65 2e 20 43 6c 6f 73 65 20 74 68 65 20  base. Close the 
193aa 72 65 61 64 20 74 72 61 6e 73 61 63 74 69 6f 6e  read transaction
193ab 20 68 65 72 65 2e 20 54 68 65 72 65 20 69 73 0a   here. There is.
193ac 20 20 20 20 2a 2a 20 6e 6f 20 6e 65 65 64 20 74      ** no need t
193ad 6f 20 63 68 65 63 6b 20 74 68 65 20 72 65 74 75  o check the retu
193ae 72 6e 20 76 61 6c 75 65 73 20 6f 66 20 74 68 65  rn values of the
193af 20 62 74 72 65 65 20 6d 65 74 68 6f 64 73 20 68   btree methods h
193b0 65 72 65 2c 20 61 73 0a 20 20 20 20 2a 2a 20 22  ere, as.    ** "
193b1 63 6f 6d 6d 69 74 74 69 6e 67 22 20 61 20 72 65  committing" a re
193b2 61 64 2d 6f 6e 6c 79 20 74 72 61 6e 73 61 63 74  ad-only transact
193b3 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e  ion cannot fail.
193b4 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
193b5 62 43 6c 6f 73 65 54 72 61 6e 73 20 29 7b 0a 20  bCloseTrans ){. 
193b6 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 69       TESTONLY( i
193b7 6e 74 20 72 63 32 20 29 3b 0a 20 20 20 20 20 20  nt rc2 );.      
193b8 54 45 53 54 4f 4e 4c 59 28 20 72 63 32 20 20 3d  TESTONLY( rc2  =
193b9 20 29 20 73 71 6c 69 74 65 33 42 74 72 65 65 43   ) sqlite3BtreeC
193ba 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 2d  ommitPhaseOne(p-
193bb 3e 70 53 72 63 2c 20 30 29 3b 0a 20 20 20 20 20  >pSrc, 0);.     
193bc 20 54 45 53 54 4f 4e 4c 59 28 20 72 63 32 20 7c   TESTONLY( rc2 |
193bd 3d 20 29 20 73 71 6c 69 74 65 33 42 74 72 65 65  = ) sqlite3Btree
193be 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70  CommitPhaseTwo(p
193bf 2d 3e 70 53 72 63 29 3b 0a 20 20 20 20 20 20 61  ->pSrc);.      a
193c0 73 73 65 72 74 28 20 72 63 32 3d 3d 53 51 4c 49  ssert( rc2==SQLI
193c1 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 7d 0a 20  TE_OK );.    }. 
193c2 20 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63   .    p->rc = rc
193c3 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70  ;.  }.  if( p->p
193c4 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 73 71  DestDb ){.    sq
193c5 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
193c6 65 28 70 2d 3e 70 44 65 73 74 44 62 2d 3e 6d 75  e(p->pDestDb->mu
193c7 74 65 78 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  tex);.  }.  sqli
193c8 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 2d  te3BtreeLeave(p-
193c9 3e 70 53 72 63 29 3b 0a 20 20 73 71 6c 69 74 65  >pSrc);.  sqlite
193ca 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d  3_mutex_leave(p-
193cb 3e 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b  >pSrcDb->mutex);
193cc 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
193cd 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61  ./*.** Release a
193ce 6c 6c 20 72 65 73 6f 75 72 63 65 73 20 61 73 73  ll resources ass
193cf 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 6e 20  ociated with an 
193d0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 2a 20  sqlite3_backup* 
193d1 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54  handle..*/.SQLIT
193d2 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
193d3 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28  3_backup_finish(
193d4 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a  sqlite3_backup *
193d5 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62 61  p){.  sqlite3_ba
193d6 63 6b 75 70 20 2a 2a 70 70 3b 20 20 20 20 20 20  ckup **pp;      
193d7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 74             /* Pt
193d8 72 20 74 6f 20 68 65 61 64 20 6f 66 20 70 61 67  r to head of pag
193d9 65 72 73 20 62 61 63 6b 75 70 20 6c 69 73 74 20  ers backup list 
193da 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  */.  sqlite3_mut
193db 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 20  ex *mutex;      
193dc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74            /* Mut
193dd 65 78 20 74 6f 20 70 72 6f 74 65 63 74 20 73 6f  ex to protect so
193de 75 72 63 65 20 64 61 74 61 62 61 73 65 20 2a 2f  urce database */
193df 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
193e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
193e1 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65          /* Value
193e2 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 0a 20   to return */.. 
193e3 20 2f 2a 20 45 6e 74 65 72 20 74 68 65 20 6d 75   /* Enter the mu
193e4 74 65 78 65 73 20 2a 2f 0a 20 20 69 66 28 20 70  texes */.  if( p
193e5 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c  ==0 ) return SQL
193e6 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c 69 74 65  ITE_OK;.  sqlite
193e7 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d  3_mutex_enter(p-
193e8 3e 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b  >pSrcDb->mutex);
193e9 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
193ea 6e 74 65 72 28 70 2d 3e 70 53 72 63 29 3b 0a 20  nter(p->pSrc);. 
193eb 20 6d 75 74 65 78 20 3d 20 70 2d 3e 70 53 72 63   mutex = p->pSrc
193ec 44 62 2d 3e 6d 75 74 65 78 3b 0a 20 20 69 66 28  Db->mutex;.  if(
193ed 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20   p->pDestDb ){. 
193ee 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
193ef 5f 65 6e 74 65 72 28 70 2d 3e 70 44 65 73 74 44  _enter(p->pDestD
193f0 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 0a  b->mutex);.  }..
193f1 20 20 2f 2a 20 44 65 74 61 63 68 20 74 68 69 73    /* Detach this
193f2 20 62 61 63 6b 75 70 20 66 72 6f 6d 20 74 68 65   backup from the
193f3 20 73 6f 75 72 63 65 20 70 61 67 65 72 2e 20 2a   source pager. *
193f4 2f 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74  /.  if( p->pDest
193f5 44 62 20 29 7b 0a 20 20 20 20 70 2d 3e 70 53 72  Db ){.    p->pSr
193f6 63 2d 3e 6e 42 61 63 6b 75 70 2d 2d 3b 0a 20 20  c->nBackup--;.  
193f7 7d 0a 20 20 69 66 28 20 70 2d 3e 69 73 41 74 74  }.  if( p->isAtt
193f8 61 63 68 65 64 20 29 7b 0a 20 20 20 20 70 70 20  ached ){.    pp 
193f9 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 42 61  = sqlite3PagerBa
193fa 63 6b 75 70 50 74 72 28 73 71 6c 69 74 65 33 42  ckupPtr(sqlite3B
193fb 74 72 65 65 50 61 67 65 72 28 70 2d 3e 70 53 72  treePager(p->pSr
193fc 63 29 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  c));.    while( 
193fd 2a 70 70 21 3d 70 20 29 7b 0a 20 20 20 20 20 20  *pp!=p ){.      
193fe 70 70 20 3d 20 26 28 2a 70 70 29 2d 3e 70 4e 65  pp = &(*pp)->pNe
193ff 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70  xt;.    }.    *p
19400 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20  p = p->pNext;.  
19401 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20 74 72 61  }..  /* If a tra
19402 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 69 6c  nsaction is stil
19403 6c 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 42 74  l open on the Bt
19404 72 65 65 2c 20 72 6f 6c 6c 20 69 74 20 62 61 63  ree, roll it bac
19405 6b 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42  k. */.  sqlite3B
19406 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 70 2d 3e  treeRollback(p->
19407 70 44 65 73 74 29 3b 0a 0a 20 20 2f 2a 20 53 65  pDest);..  /* Se
19408 74 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65  t the error code
19409 20 6f 66 20 74 68 65 20 64 65 73 74 69 6e 61 74   of the destinat
1940a 69 6f 6e 20 64 61 74 61 62 61 73 65 20 68 61 6e  ion database han
1940b 64 6c 65 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 28  dle. */.  rc = (
1940c 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f  p->rc==SQLITE_DO
1940d 4e 45 29 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20  NE) ? SQLITE_OK 
1940e 3a 20 70 2d 3e 72 63 3b 0a 20 20 73 71 6c 69 74  : p->rc;.  sqlit
1940f 65 33 45 72 72 6f 72 28 70 2d 3e 70 44 65 73 74  e3Error(p->pDest
19410 44 62 2c 20 72 63 2c 20 30 29 3b 0a 0a 20 20 2f  Db, rc, 0);..  /
19411 2a 20 45 78 69 74 20 74 68 65 20 6d 75 74 65 78  * Exit the mutex
19412 65 73 20 61 6e 64 20 66 72 65 65 20 74 68 65 20  es and free the 
19413 62 61 63 6b 75 70 20 63 6f 6e 74 65 78 74 20 73  backup context s
19414 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a 20 20 69  tructure. */.  i
19415 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b  f( p->pDestDb ){
19416 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
19417 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70 44 65 73  ex_leave(p->pDes
19418 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d  tDb->mutex);.  }
19419 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
1941a 65 61 76 65 28 70 2d 3e 70 53 72 63 29 3b 0a 20  eave(p->pSrc);. 
1941b 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20   if( p->pDestDb 
1941c 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  ){.    sqlite3_f
1941d 72 65 65 28 70 29 3b 0a 20 20 7d 0a 20 20 73 71  ree(p);.  }.  sq
1941e 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
1941f 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  e(mutex);.  retu
19420 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
19421 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
19422 72 20 6f 66 20 70 61 67 65 73 20 73 74 69 6c 6c  r of pages still
19423 20 74 6f 20 62 65 20 62 61 63 6b 65 64 20 75 70   to be backed up
19424 20 61 73 20 6f 66 20 74 68 65 20 6d 6f 73 74 20   as of the most 
19425 72 65 63 65 6e 74 0a 2a 2a 20 63 61 6c 6c 20 74  recent.** call t
19426 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  o sqlite3_backup
19427 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49  _step()..*/.SQLI
19428 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
19429 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e  e3_backup_remain
1942a 69 6e 67 28 73 71 6c 69 74 65 33 5f 62 61 63 6b  ing(sqlite3_back
1942b 75 70 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e  up *p){.  return
1942c 20 70 2d 3e 6e 52 65 6d 61 69 6e 69 6e 67 3b 0a   p->nRemaining;.
1942d 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
1942e 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72  the total number
1942f 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65   of pages in the
19430 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65   source database
19431 20 61 73 20 6f 66 20 74 68 65 20 6d 6f 73 74 20   as of the most 
19432 0a 2a 2a 20 72 65 63 65 6e 74 20 63 61 6c 6c 20  .** recent call 
19433 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  to sqlite3_backu
19434 70 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c  p_step()..*/.SQL
19435 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
19436 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 63  te3_backup_pagec
19437 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 62 61 63  ount(sqlite3_bac
19438 6b 75 70 20 2a 70 29 7b 0a 20 20 72 65 74 75 72  kup *p){.  retur
19439 6e 20 70 2d 3e 6e 50 61 67 65 63 6f 75 6e 74 3b  n p->nPagecount;
1943a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
1943b 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
1943c 64 20 61 66 74 65 72 20 74 68 65 20 63 6f 6e 74  d after the cont
1943d 65 6e 74 73 20 6f 66 20 70 61 67 65 20 69 50 61  ents of page iPa
1943e 67 65 20 6f 66 20 74 68 65 0a 2a 2a 20 73 6f 75  ge of the.** sou
1943f 72 63 65 20 64 61 74 61 62 61 73 65 20 68 61 76  rce database hav
19440 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 2e  e been modified.
19441 20 49 66 20 70 61 67 65 20 69 50 61 67 65 20 68   If page iPage h
19442 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
19443 0a 2a 2a 20 63 6f 70 69 65 64 20 69 6e 74 6f 20  .** copied into 
19444 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  the destination 
19445 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20 74  database, then t
19446 68 65 20 64 61 74 61 20 77 72 69 74 74 65 6e 20  he data written 
19447 74 6f 20 74 68 65 0a 2a 2a 20 64 65 73 74 69 6e  to the.** destin
19448 61 74 69 6f 6e 20 69 73 20 6e 6f 77 20 69 6e 76  ation is now inv
19449 61 6c 69 64 61 74 65 64 2e 20 54 68 65 20 64 65  alidated. The de
1944a 73 74 69 6e 61 74 69 6f 6e 20 63 6f 70 79 20 6f  stination copy o
1944b 66 20 69 50 61 67 65 20 6e 65 65 64 73 0a 2a 2a  f iPage needs.**
1944c 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 20 77   to be updated w
1944d 69 74 68 20 74 68 65 20 6e 65 77 20 64 61 74 61  ith the new data
1944e 20 62 65 66 6f 72 65 20 74 68 65 20 62 61 63 6b   before the back
1944f 75 70 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 0a  up operation is.
19450 2a 2a 20 63 6f 6d 70 6c 65 74 65 2e 0a 2a 2a 0a  ** complete..**.
19451 2a 2a 20 49 74 20 69 73 20 61 73 73 75 6d 65 64  ** It is assumed
19452 20 74 68 61 74 20 74 68 65 20 6d 75 74 65 78 20   that the mutex 
19453 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
19454 74 68 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a  the BtShared obj
19455 65 63 74 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e  ect.** correspon
19456 64 69 6e 67 20 74 6f 20 74 68 65 20 73 6f 75 72  ding to the sour
19457 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20 68  ce database is h
19458 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 75  eld when this fu
19459 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 63 61 6c  nction is.** cal
1945a 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  led..*/.SQLITE_P
1945b 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1945c 74 65 33 42 61 63 6b 75 70 55 70 64 61 74 65 28  te3BackupUpdate(
1945d 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a  sqlite3_backup *
1945e 70 42 61 63 6b 75 70 2c 20 50 67 6e 6f 20 69 50  pBackup, Pgno iP
1945f 61 67 65 2c 20 63 6f 6e 73 74 20 75 38 20 2a 61  age, const u8 *a
19460 44 61 74 61 29 7b 0a 20 20 73 71 6c 69 74 65 33  Data){.  sqlite3
19461 5f 62 61 63 6b 75 70 20 2a 70 3b 20 20 20 20 20  _backup *p;     
19462 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
19463 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62   Iterator variab
19464 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 70 3d 70 42  le */.  for(p=pB
19465 61 63 6b 75 70 3b 20 70 3b 20 70 3d 70 2d 3e 70  ackup; p; p=p->p
19466 4e 65 78 74 29 7b 0a 20 20 20 20 61 73 73 65 72  Next){.    asser
19467 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
19468 5f 68 65 6c 64 28 70 2d 3e 70 53 72 63 2d 3e 70  _held(p->pSrc->p
19469 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
1946a 20 20 69 66 28 20 21 69 73 46 61 74 61 6c 45 72    if( !isFatalEr
1946b 72 6f 72 28 70 2d 3e 72 63 29 20 26 26 20 69 50  ror(p->rc) && iP
1946c 61 67 65 3c 70 2d 3e 69 4e 65 78 74 20 29 7b 0a  age<p->iNext ){.
1946d 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 61 63        /* The bac
1946e 6b 75 70 20 70 72 6f 63 65 73 73 20 70 20 68 61  kup process p ha
1946f 73 20 61 6c 72 65 61 64 79 20 63 6f 70 69 65 64  s already copied
19470 20 70 61 67 65 20 69 50 61 67 65 2e 20 42 75 74   page iPage. But
19471 20 6e 6f 77 20 69 74 0a 20 20 20 20 20 20 2a 2a   now it.      **
19472 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 69   has been modifi
19473 65 64 20 62 79 20 61 20 74 72 61 6e 73 61 63 74  ed by a transact
19474 69 6f 6e 20 6f 6e 20 74 68 65 20 73 6f 75 72 63  ion on the sourc
19475 65 20 70 61 67 65 72 2e 20 43 6f 70 79 0a 20 20  e pager. Copy.  
19476 20 20 20 20 2a 2a 20 74 68 65 20 6e 65 77 20 64      ** the new d
19477 61 74 61 20 69 6e 74 6f 20 74 68 65 20 62 61 63  ata into the bac
19478 6b 75 70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  kup..      */.  
19479 20 20 20 20 69 6e 74 20 72 63 20 3d 20 62 61 63      int rc = bac
1947a 6b 75 70 4f 6e 65 50 61 67 65 28 70 2c 20 69 50  kupOnePage(p, iP
1947b 61 67 65 2c 20 61 44 61 74 61 29 3b 0a 20 20 20  age, aData);.   
1947c 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53     assert( rc!=S
1947d 51 4c 49 54 45 5f 42 55 53 59 20 26 26 20 72 63  QLITE_BUSY && rc
1947e 21 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20  !=SQLITE_LOCKED 
1947f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
19480 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
19481 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63        p->rc = rc
19482 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
19483 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73    }.}../*.** Res
19484 74 61 72 74 20 74 68 65 20 62 61 63 6b 75 70 20  tart the backup 
19485 70 72 6f 63 65 73 73 2e 20 54 68 69 73 20 69 73  process. This is
19486 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65   called when the
19487 20 70 61 67 65 72 20 6c 61 79 65 72 0a 2a 2a 20   pager layer.** 
19488 64 65 74 65 63 74 73 20 74 68 61 74 20 74 68 65  detects that the
19489 20 64 61 74 61 62 61 73 65 20 68 61 73 20 62 65   database has be
1948a 65 6e 20 6d 6f 64 69 66 69 65 64 20 62 79 20 61  en modified by a
1948b 6e 20 65 78 74 65 72 6e 61 6c 20 64 61 74 61 62  n external datab
1948c 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f  ase.** connectio
1948d 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  n. In this case 
1948e 74 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20  there is no way 
1948f 6f 66 20 6b 6e 6f 77 69 6e 67 20 77 68 69 63 68  of knowing which
19490 20 6f 66 20 74 68 65 0a 2a 2a 20 70 61 67 65 73   of the.** pages
19491 20 74 68 61 74 20 68 61 76 65 20 62 65 65 6e 20   that have been 
19492 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20  copied into the 
19493 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61  destination data
19494 62 61 73 65 20 61 72 65 20 73 74 69 6c 6c 20 0a  base are still .
19495 2a 2a 20 76 61 6c 69 64 20 61 6e 64 20 77 68 69  ** valid and whi
19496 63 68 20 61 72 65 20 6e 6f 74 2c 20 73 6f 20 74  ch are not, so t
19497 68 65 20 65 6e 74 69 72 65 20 70 72 6f 63 65 73  he entire proces
19498 73 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65  s needs to be re
19499 73 74 61 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  started..**.** I
1949a 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61  t is assumed tha
1949b 74 20 74 68 65 20 6d 75 74 65 78 20 61 73 73 6f  t the mutex asso
1949c 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
1949d 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 0a  BtShared object.
1949e 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  ** corresponding
1949f 20 74 6f 20 74 68 65 20 73 6f 75 72 63 65 20 64   to the source d
194a0 61 74 61 62 61 73 65 20 69 73 20 68 65 6c 64 20  atabase is held 
194a1 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  when this functi
194a2 6f 6e 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 2e  on is.** called.
194a3 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
194a4 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
194a5 61 63 6b 75 70 52 65 73 74 61 72 74 28 73 71 6c  ackupRestart(sql
194a6 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 42 61  ite3_backup *pBa
194a7 63 6b 75 70 29 7b 0a 20 20 73 71 6c 69 74 65 33  ckup){.  sqlite3
194a8 5f 62 61 63 6b 75 70 20 2a 70 3b 20 20 20 20 20  _backup *p;     
194a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
194aa 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62   Iterator variab
194ab 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 70 3d 70 42  le */.  for(p=pB
194ac 61 63 6b 75 70 3b 20 70 3b 20 70 3d 70 2d 3e 70  ackup; p; p=p->p
194ad 4e 65 78 74 29 7b 0a 20 20 20 20 61 73 73 65 72  Next){.    asser
194ae 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
194af 5f 68 65 6c 64 28 70 2d 3e 70 53 72 63 2d 3e 70  _held(p->pSrc->p
194b0 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
194b1 20 20 70 2d 3e 69 4e 65 78 74 20 3d 20 31 3b 0a    p->iNext = 1;.
194b2 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53    }.}..#ifndef S
194b3 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55  QLITE_OMIT_VACUU
194b4 4d 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 74 68 65  M./*.** Copy the
194b5 20 63 6f 6d 70 6c 65 74 65 20 63 6f 6e 74 65 6e   complete conten
194b6 74 20 6f 66 20 70 42 74 46 72 6f 6d 20 69 6e 74  t of pBtFrom int
194b7 6f 20 70 42 74 54 6f 2e 20 20 41 20 74 72 61 6e  o pBtTo.  A tran
194b8 73 61 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20  saction.** must 
194b9 62 65 20 61 63 74 69 76 65 20 66 6f 72 20 62 6f  be active for bo
194ba 74 68 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20  th files..**.** 
194bb 54 68 65 20 73 69 7a 65 20 6f 66 20 66 69 6c 65  The size of file
194bc 20 70 54 6f 20 6d 61 79 20 62 65 20 72 65 64 75   pTo may be redu
194bd 63 65 64 20 62 79 20 74 68 69 73 20 6f 70 65 72  ced by this oper
194be 61 74 69 6f 6e 2e 20 49 66 20 61 6e 79 74 68 69  ation. If anythi
194bf 6e 67 20 0a 2a 2a 20 67 6f 65 73 20 77 72 6f 6e  ng .** goes wron
194c0 67 2c 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  g, the transacti
194c1 6f 6e 20 6f 6e 20 70 54 6f 20 69 73 20 72 6f 6c  on on pTo is rol
194c2 6c 65 64 20 62 61 63 6b 2e 20 49 66 20 73 75 63  led back. If suc
194c3 63 65 73 73 66 75 6c 2c 20 74 68 65 20 0a 2a 2a  cessful, the .**
194c4 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
194c5 63 6f 6d 6d 69 74 74 65 64 20 62 65 66 6f 72 65  committed before
194c6 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53   returning..*/.S
194c7 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
194c8 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  t sqlite3BtreeCo
194c9 70 79 46 69 6c 65 28 42 74 72 65 65 20 2a 70 54  pyFile(Btree *pT
194ca 6f 2c 20 42 74 72 65 65 20 2a 70 46 72 6f 6d 29  o, Btree *pFrom)
194cb 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71  {.  int rc;.  sq
194cc 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 62 3b 0a  lite3_backup b;.
194cd 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
194ce 74 65 72 28 70 54 6f 29 3b 0a 20 20 73 71 6c 69  ter(pTo);.  sqli
194cf 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 46  te3BtreeEnter(pF
194d0 72 6f 6d 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20  rom);..  /* Set 
194d1 75 70 20 61 6e 20 73 71 6c 69 74 65 33 5f 62 61  up an sqlite3_ba
194d2 63 6b 75 70 20 6f 62 6a 65 63 74 2e 20 73 71 6c  ckup object. sql
194d3 69 74 65 33 5f 62 61 63 6b 75 70 2e 70 44 65 73  ite3_backup.pDes
194d4 74 44 62 20 6d 75 73 74 20 62 65 20 73 65 74 0a  tDb must be set.
194d5 20 20 2a 2a 20 74 6f 20 30 2e 20 54 68 69 73 20    ** to 0. This 
194d6 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 69  is used by the i
194d7 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f  mplementations o
194d8 66 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  f sqlite3_backup
194d9 5f 73 74 65 70 28 29 0a 20 20 2a 2a 20 61 6e 64  _step().  ** and
194da 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
194db 66 69 6e 69 73 68 28 29 20 74 6f 20 64 65 74 65  finish() to dete
194dc 63 74 20 74 68 61 74 20 74 68 65 79 20 61 72 65  ct that they are
194dd 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 0a 20 20   being called.  
194de 2a 2a 20 66 72 6f 6d 20 74 68 69 73 20 66 75 6e  ** from this fun
194df 63 74 69 6f 6e 2c 20 6e 6f 74 20 64 69 72 65 63  ction, not direc
194e0 74 6c 79 20 62 79 20 74 68 65 20 75 73 65 72 2e  tly by the user.
194e1 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26  .  */.  memset(&
194e2 62 2c 20 30 2c 20 73 69 7a 65 6f 66 28 62 29 29  b, 0, sizeof(b))
194e3 3b 0a 20 20 62 2e 70 53 72 63 44 62 20 3d 20 70  ;.  b.pSrcDb = p
194e4 46 72 6f 6d 2d 3e 64 62 3b 0a 20 20 62 2e 70 53  From->db;.  b.pS
194e5 72 63 20 3d 20 70 46 72 6f 6d 3b 0a 20 20 62 2e  rc = pFrom;.  b.
194e6 70 44 65 73 74 20 3d 20 70 54 6f 3b 0a 20 20 62  pDest = pTo;.  b
194e7 2e 69 4e 65 78 74 20 3d 20 31 3b 0a 0a 20 20 2f  .iNext = 1;..  /
194e8 2a 20 30 78 37 46 46 46 46 46 46 46 20 69 73 20  * 0x7FFFFFFF is 
194e9 74 68 65 20 68 61 72 64 20 6c 69 6d 69 74 20 66  the hard limit f
194ea 6f 72 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  or the number of
194eb 20 70 61 67 65 73 20 69 6e 20 61 20 64 61 74 61   pages in a data
194ec 62 61 73 65 0a 20 20 2a 2a 20 66 69 6c 65 2e 20  base.  ** file. 
194ed 42 79 20 70 61 73 73 69 6e 67 20 74 68 69 73 20  By passing this 
194ee 61 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  as the number of
194ef 20 70 61 67 65 73 20 74 6f 20 63 6f 70 79 20 74   pages to copy t
194f0 6f 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 62  o.  ** sqlite3_b
194f1 61 63 6b 75 70 5f 73 74 65 70 28 29 2c 20 77 65  ackup_step(), we
194f2 20 63 61 6e 20 67 75 61 72 61 6e 74 65 65 20 74   can guarantee t
194f3 68 61 74 20 74 68 65 20 63 6f 70 79 20 66 69 6e  hat the copy fin
194f4 69 73 68 65 73 20 0a 20 20 2a 2a 20 77 69 74 68  ishes .  ** with
194f5 69 6e 20 61 20 73 69 6e 67 6c 65 20 63 61 6c 6c  in a single call
194f6 20 28 75 6e 6c 65 73 73 20 61 6e 20 65 72 72 6f   (unless an erro
194f7 72 20 6f 63 63 75 72 73 29 2e 20 54 68 65 20 61  r occurs). The a
194f8 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e  ssert() statemen
194f9 74 0a 20 20 2a 2a 20 63 68 65 63 6b 73 20 74 68  t.  ** checks th
194fa 69 73 20 61 73 73 75 6d 70 74 69 6f 6e 20 2d 20  is assumption - 
194fb 28 70 2d 3e 72 63 29 20 73 68 6f 75 6c 64 20 62  (p->rc) should b
194fc 65 20 73 65 74 20 74 6f 20 65 69 74 68 65 72 20  e set to either 
194fd 53 51 4c 49 54 45 5f 44 4f 4e 45 20 0a 20 20 2a  SQLITE_DONE .  *
194fe 2a 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f  * or an error co
194ff 64 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  de..  */.  sqlit
19500 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 26  e3_backup_step(&
19501 62 2c 20 30 78 37 46 46 46 46 46 46 46 29 3b 0a  b, 0x7FFFFFFF);.
19502 20 20 61 73 73 65 72 74 28 20 62 2e 72 63 21 3d    assert( b.rc!=
19503 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 72  SQLITE_OK );.  r
19504 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  c = sqlite3_back
19505 75 70 5f 66 69 6e 69 73 68 28 26 62 29 3b 0a 20  up_finish(&b);. 
19506 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
19507 4f 4b 20 29 7b 0a 20 20 20 20 70 54 6f 2d 3e 70  OK ){.    pTo->p
19508 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65  Bt->pageSizeFixe
19509 64 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 73 71  d = 0;.  }..  sq
1950a 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
1950b 70 46 72 6f 6d 29 3b 0a 20 20 73 71 6c 69 74 65  pFrom);.  sqlite
1950c 33 42 74 72 65 65 4c 65 61 76 65 28 70 54 6f 29  3BtreeLeave(pTo)
1950d 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
1950e 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1950f 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 20 2a 2f  E_OMIT_VACUUM */
19510 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
19511 2a 20 45 6e 64 20 6f 66 20 62 61 63 6b 75 70 2e  * End of backup.
19512 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
19513 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19514 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19515 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
19516 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62  * Begin file vdb
19517 65 6d 65 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  emem.c *********
19518 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19519 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1951a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79  /./*.** 2004 May
1951b 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   26.**.** The au
1951c 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
1951d 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
1951e 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
1951f 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
19520 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
19521 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
19522 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
19523 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
19524 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
19525 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
19526 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
19527 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
19528 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
19529 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
1952a 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
1952b 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
1952c 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
1952d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1952e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1952f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19531 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  **.**.** This fi
19532 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65  le contains code
19533 20 75 73 65 20 74 6f 20 6d 61 6e 69 70 75 6c 61   use to manipula
19534 74 65 20 22 4d 65 6d 22 20 73 74 72 75 63 74 75  te "Mem" structu
19535 72 65 2e 20 20 41 20 22 4d 65 6d 22 0a 2a 2a 20  re.  A "Mem".** 
19536 73 74 6f 72 65 73 20 61 20 73 69 6e 67 6c 65 20  stores a single 
19537 76 61 6c 75 65 20 69 6e 20 74 68 65 20 56 44 42  value in the VDB
19538 45 2e 20 20 4d 65 6d 20 69 73 20 61 6e 20 6f 70  E.  Mem is an op
19539 61 71 75 65 20 73 74 72 75 63 74 75 72 65 20 76  aque structure v
1953a 69 73 69 62 6c 65 0a 2a 2a 20 6f 6e 6c 79 20 77  isible.** only w
1953b 69 74 68 69 6e 20 74 68 65 20 56 44 42 45 2e 20  ithin the VDBE. 
1953c 20 49 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69   Interface routi
1953d 6e 65 73 20 72 65 66 65 72 20 74 6f 20 61 20 4d  nes refer to a M
1953e 65 6d 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20  em using the.** 
1953f 6e 61 6d 65 20 73 71 6c 69 74 65 5f 76 61 6c 75  name sqlite_valu
19540 65 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62  e.**.** $Id: vdb
19541 65 6d 65 6d 2e 63 2c 76 20 31 2e 31 35 32 20 32  emem.c,v 1.152 2
19542 30 30 39 2f 30 37 2f 32 32 20 31 38 3a 30 37 3a  009/07/22 18:07:
19543 34 31 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a  41 drh Exp $.*/.
19544 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69  ./*.** Call sqli
19545 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64  te3VdbeMemExpand
19546 42 6c 6f 62 28 29 20 6f 6e 20 74 68 65 20 73 75  Blob() on the su
19547 70 70 6c 69 65 64 20 76 61 6c 75 65 20 28 74 79  pplied value (ty
19548 70 65 20 4d 65 6d 2a 29 0a 2a 2a 20 50 20 69 66  pe Mem*).** P if
19549 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 23 64   required..*/.#d
1954a 65 66 69 6e 65 20 65 78 70 61 6e 64 42 6c 6f 62  efine expandBlob
1954b 28 50 29 20 28 28 28 50 29 2d 3e 66 6c 61 67 73  (P) (((P)->flags
1954c 26 4d 45 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69 74  &MEM_Zero)?sqlit
1954d 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42  e3VdbeMemExpandB
1954e 6c 6f 62 28 50 29 3a 30 29 0a 0a 2f 2a 0a 2a 2a  lob(P):0)../*.**
1954f 20 49 66 20 70 4d 65 6d 20 69 73 20 61 6e 20 6f   If pMem is an o
19550 62 6a 65 63 74 20 77 69 74 68 20 61 20 76 61 6c  bject with a val
19551 69 64 20 73 74 72 69 6e 67 20 72 65 70 72 65 73  id string repres
19552 65 6e 74 61 74 69 6f 6e 2c 20 74 68 69 73 20 72  entation, this r
19553 6f 75 74 69 6e 65 0a 2a 2a 20 65 6e 73 75 72 65  outine.** ensure
19554 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 65  s the internal e
19555 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 68 65 20  ncoding for the 
19556 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74  string represent
19557 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 27 64 65 73  ation is.** 'des
19558 69 72 65 64 45 6e 63 27 2c 20 6f 6e 65 20 6f 66  iredEnc', one of
19559 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51   SQLITE_UTF8, SQ
1955a 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72 20  LITE_UTF16LE or 
1955b 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2e 0a  SQLITE_UTF16BE..
1955c 2a 2a 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73  **.** If pMem is
1955d 20 6e 6f 74 20 61 20 73 74 72 69 6e 67 20 6f 62   not a string ob
1955e 6a 65 63 74 2c 20 6f 72 20 74 68 65 20 65 6e 63  ject, or the enc
1955f 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 73 74 72  oding of the str
19560 69 6e 67 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74  ing.** represent
19561 61 74 69 6f 6e 20 69 73 20 61 6c 72 65 61 64 79  ation is already
19562 20 73 74 6f 72 65 64 20 75 73 69 6e 67 20 74 68   stored using th
19563 65 20 72 65 71 75 65 73 74 65 64 20 65 6e 63 6f  e requested enco
19564 64 69 6e 67 2c 20 74 68 65 6e 20 74 68 69 73 0a  ding, then this.
19565 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  ** routine is a 
19566 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c  no-op..**.** SQL
19567 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
19568 65 64 20 69 66 20 74 68 65 20 63 6f 6e 76 65 72  ed if the conver
19569 73 69 6f 6e 20 69 73 20 73 75 63 63 65 73 73 66  sion is successf
1956a 75 6c 20 28 6f 72 20 6e 6f 74 20 72 65 71 75 69  ul (or not requi
1956b 72 65 64 29 2e 0a 2a 2a 20 53 51 4c 49 54 45 5f  red)..** SQLITE_
1956c 4e 4f 4d 45 4d 20 6d 61 79 20 62 65 20 72 65 74  NOMEM may be ret
1956d 75 72 6e 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f  urned if a mallo
1956e 63 28 29 20 66 61 69 6c 73 20 64 75 72 69 6e 67  c() fails during
1956f 20 63 6f 6e 76 65 72 73 69 6f 6e 0a 2a 2a 20 62   conversion.** b
19570 65 74 77 65 65 6e 20 66 6f 72 6d 61 74 73 2e 0a  etween formats..
19571 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
19572 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
19573 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28  eChangeEncoding(
19574 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 64  Mem *pMem, int d
19575 65 73 69 72 65 64 45 6e 63 29 7b 0a 20 20 69 6e  esiredEnc){.  in
19576 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20  t rc;.  assert( 
19577 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d  (pMem->flags&MEM
19578 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20  _RowSet)==0 );. 
19579 20 61 73 73 65 72 74 28 20 64 65 73 69 72 65 64   assert( desired
1957a 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38  Enc==SQLITE_UTF8
1957b 20 7c 7c 20 64 65 73 69 72 65 64 45 6e 63 3d 3d   || desiredEnc==
1957c 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 0a 20  SQLITE_UTF16LE. 
1957d 20 20 20 20 20 20 20 20 20 20 7c 7c 20 64 65 73            || des
1957e 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f  iredEnc==SQLITE_
1957f 55 54 46 31 36 42 45 20 29 3b 0a 20 20 69 66 28  UTF16BE );.  if(
19580 20 21 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d   !(pMem->flags&M
19581 45 4d 5f 53 74 72 29 20 7c 7c 20 70 4d 65 6d 2d  EM_Str) || pMem-
19582 3e 65 6e 63 3d 3d 64 65 73 69 72 65 64 45 6e 63  >enc==desiredEnc
19583 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
19584 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
19585 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62  assert( pMem->db
19586 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d  ==0 || sqlite3_m
19587 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e  utex_held(pMem->
19588 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 23 69  db->mutex) );.#i
19589 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
1958a 5f 55 54 46 31 36 0a 20 20 72 65 74 75 72 6e 20  _UTF16.  return 
1958b 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 23 65  SQLITE_ERROR;.#e
1958c 6c 73 65 0a 0a 20 20 2f 2a 20 4d 65 6d 54 72 61  lse..  /* MemTra
1958d 6e 73 6c 61 74 65 28 29 20 6d 61 79 20 72 65 74  nslate() may ret
1958e 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 72  urn SQLITE_OK or
1958f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 49   SQLITE_NOMEM. I
19590 66 20 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72  f NOMEM is retur
19591 6e 65 64 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 74  ned,.  ** then t
19592 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74  he encoding of t
19593 68 65 20 76 61 6c 75 65 20 6d 61 79 20 6e 6f 74  he value may not
19594 20 68 61 76 65 20 63 68 61 6e 67 65 64 2e 0a 20   have changed.. 
19595 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   */.  rc = sqlit
19596 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61  e3VdbeMemTransla
19597 74 65 28 70 4d 65 6d 2c 20 28 75 38 29 64 65 73  te(pMem, (u8)des
19598 69 72 65 64 45 6e 63 29 3b 0a 20 20 61 73 73 65  iredEnc);.  asse
19599 72 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  rt(rc==SQLITE_OK
1959a 20 20 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54      || rc==SQLIT
1959b 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 61 73 73 65  E_NOMEM);.  asse
1959c 72 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  rt(rc==SQLITE_OK
1959d 20 20 20 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e 63      || pMem->enc
1959e 21 3d 64 65 73 69 72 65 64 45 6e 63 29 3b 0a 20  !=desiredEnc);. 
1959f 20 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c 49   assert(rc==SQLI
195a0 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 70 4d 65 6d  TE_NOMEM || pMem
195a1 2d 3e 65 6e 63 3d 3d 64 65 73 69 72 65 64 45 6e  ->enc==desiredEn
195a2 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  c);.  return rc;
195a3 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a  .#endif.}../*.**
195a4 20 4d 61 6b 65 20 73 75 72 65 20 70 4d 65 6d 2d   Make sure pMem-
195a5 3e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 77  >z points to a w
195a6 72 69 74 61 62 6c 65 20 61 6c 6c 6f 63 61 74 69  ritable allocati
195a7 6f 6e 20 6f 66 20 61 74 20 6c 65 61 73 74 20 0a  on of at least .
195a8 2a 2a 20 6e 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a  ** n bytes..**.*
195a9 2a 20 49 66 20 74 68 65 20 6d 65 6d 6f 72 79 20  * If the memory 
195aa 63 65 6c 6c 20 63 75 72 72 65 6e 74 6c 79 20 63  cell currently c
195ab 6f 6e 74 61 69 6e 73 20 73 74 72 69 6e 67 20 6f  ontains string o
195ac 72 20 62 6c 6f 62 20 64 61 74 61 0a 2a 2a 20 61  r blob data.** a
195ad 6e 64 20 74 68 65 20 74 68 69 72 64 20 61 72 67  nd the third arg
195ae 75 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f 20  ument passed to 
195af 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
195b0 20 74 72 75 65 2c 20 74 68 65 20 0a 2a 2a 20 63   true, the .** c
195b1 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 20 6f  urrent content o
195b2 66 20 74 68 65 20 63 65 6c 6c 20 69 73 20 70 72  f the cell is pr
195b3 65 73 65 72 76 65 64 2e 20 4f 74 68 65 72 77 69  eserved. Otherwi
195b4 73 65 2c 20 69 74 20 6d 61 79 0a 2a 2a 20 62 65  se, it may.** be
195b5 20 64 69 73 63 61 72 64 65 64 2e 20 20 0a 2a 2a   discarded.  .**
195b6 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
195b7 6e 20 73 65 74 73 20 74 68 65 20 4d 45 4d 5f 44  n sets the MEM_D
195b8 79 6e 20 66 6c 61 67 20 61 6e 64 20 63 6c 65 61  yn flag and clea
195b9 72 73 20 61 6e 79 20 78 44 65 6c 20 63 61 6c 6c  rs any xDel call
195ba 62 61 63 6b 2e 0a 2a 2a 20 49 74 20 61 6c 73 6f  back..** It also
195bb 20 63 6c 65 61 72 73 20 4d 45 4d 5f 45 70 68 65   clears MEM_Ephe
195bc 6d 20 61 6e 64 20 4d 45 4d 5f 53 74 61 74 69 63  m and MEM_Static
195bd 2e 20 49 66 20 74 68 65 20 70 72 65 73 65 72 76  . If the preserv
195be 65 20 66 6c 61 67 20 69 73 20 0a 2a 2a 20 6e 6f  e flag is .** no
195bf 74 20 73 65 74 2c 20 4d 65 6d 2e 6e 20 69 73 20  t set, Mem.n is 
195c0 7a 65 72 6f 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  zeroed..*/.SQLIT
195c1 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
195c2 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77  lite3VdbeMemGrow
195c3 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20  (Mem *pMem, int 
195c4 6e 2c 20 69 6e 74 20 70 72 65 73 65 72 76 65 29  n, int preserve)
195c5 7b 0a 20 20 61 73 73 65 72 74 28 20 31 20 3e 3d  {.  assert( 1 >=
195c6 0a 20 20 20 20 28 28 70 4d 65 6d 2d 3e 7a 4d 61  .    ((pMem->zMa
195c7 6c 6c 6f 63 20 26 26 20 70 4d 65 6d 2d 3e 7a 4d  lloc && pMem->zM
195c8 61 6c 6c 6f 63 3d 3d 70 4d 65 6d 2d 3e 7a 29 20  alloc==pMem->z) 
195c9 3f 20 31 20 3a 20 30 29 20 2b 0a 20 20 20 20 28  ? 1 : 0) +.    (
195ca 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45  ((pMem->flags&ME
195cb 4d 5f 44 79 6e 29 26 26 70 4d 65 6d 2d 3e 78 44  M_Dyn)&&pMem->xD
195cc 65 6c 29 20 3f 20 31 20 3a 20 30 29 20 2b 20 0a  el) ? 1 : 0) + .
195cd 20 20 20 20 28 28 70 4d 65 6d 2d 3e 66 6c 61 67      ((pMem->flag
195ce 73 26 4d 45 4d 5f 45 70 68 65 6d 29 20 3f 20 31  s&MEM_Ephem) ? 1
195cf 20 3a 20 30 29 20 2b 20 0a 20 20 20 20 28 28 70   : 0) + .    ((p
195d0 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53  Mem->flags&MEM_S
195d1 74 61 74 69 63 29 20 3f 20 31 20 3a 20 30 29 0a  tatic) ? 1 : 0).
195d2 20 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28    );.  assert( (
195d3 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  pMem->flags&MEM_
195d4 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 0a 20  RowSet)==0 );.. 
195d5 20 69 66 28 20 6e 3c 33 32 20 29 20 6e 20 3d 20   if( n<32 ) n = 
195d6 33 32 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  32;.  if( sqlite
195d7 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 4d  3DbMallocSize(pM
195d8 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d  em->db, pMem->zM
195d9 61 6c 6c 6f 63 29 3c 6e 20 29 7b 0a 20 20 20 20  alloc)<n ){.    
195da 69 66 28 20 70 72 65 73 65 72 76 65 20 26 26 20  if( preserve && 
195db 70 4d 65 6d 2d 3e 7a 3d 3d 70 4d 65 6d 2d 3e 7a  pMem->z==pMem->z
195dc 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20  Malloc ){.      
195dd 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d 3e  pMem->z = pMem->
195de 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65  zMalloc = sqlite
195df 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65  3DbReallocOrFree
195e0 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d  (pMem->db, pMem-
195e1 3e 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20 70 72  >z, n);.      pr
195e2 65 73 65 72 76 65 20 3d 20 30 3b 0a 20 20 20 20  eserve = 0;.    
195e3 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
195e4 69 74 65 33 44 62 46 72 65 65 28 70 4d 65 6d 2d  ite3DbFree(pMem-
195e5 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c  >db, pMem->zMall
195e6 6f 63 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d  oc);.      pMem-
195e7 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 74  >zMalloc = sqlit
195e8 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 4d  e3DbMallocRaw(pM
195e9 65 6d 2d 3e 64 62 2c 20 6e 29 3b 0a 20 20 20 20  em->db, n);.    
195ea 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4d 65  }.  }..  if( pMe
195eb 6d 2d 3e 7a 20 26 26 20 70 72 65 73 65 72 76 65  m->z && preserve
195ec 20 26 26 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f   && pMem->zMallo
195ed 63 20 26 26 20 70 4d 65 6d 2d 3e 7a 21 3d 70 4d  c && pMem->z!=pM
195ee 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20  em->zMalloc ){. 
195ef 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d 2d 3e     memcpy(pMem->
195f0 7a 4d 61 6c 6c 6f 63 2c 20 70 4d 65 6d 2d 3e 7a  zMalloc, pMem->z
195f1 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 7d 0a  , pMem->n);.  }.
195f2 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67    if( pMem->flag
195f3 73 26 4d 45 4d 5f 44 79 6e 20 26 26 20 70 4d 65  s&MEM_Dyn && pMe
195f4 6d 2d 3e 78 44 65 6c 20 29 7b 0a 20 20 20 20 70  m->xDel ){.    p
195f5 4d 65 6d 2d 3e 78 44 65 6c 28 28 76 6f 69 64 20  Mem->xDel((void 
195f6 2a 29 28 70 4d 65 6d 2d 3e 7a 29 29 3b 0a 20 20  *)(pMem->z));.  
195f7 7d 0a 0a 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70  }..  pMem->z = p
195f8 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20  Mem->zMalloc;.  
195f9 69 66 28 20 70 4d 65 6d 2d 3e 7a 3d 3d 30 20 29  if( pMem->z==0 )
195fa 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67  {.    pMem->flag
195fb 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20  s = MEM_Null;.  
195fc 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4d 65 6d 2d  }else{.    pMem-
195fd 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f  >flags &= ~(MEM_
195fe 45 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 69 63  Ephem|MEM_Static
195ff 29 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 78  );.  }.  pMem->x
19600 44 65 6c 20 3d 20 30 3b 0a 20 20 72 65 74 75 72  Del = 0;.  retur
19601 6e 20 28 70 4d 65 6d 2d 3e 7a 20 3f 20 53 51 4c  n (pMem->z ? SQL
19602 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f  ITE_OK : SQLITE_
19603 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  NOMEM);.}../*.**
19604 20 4d 61 6b 65 20 74 68 65 20 67 69 76 65 6e 20   Make the given 
19605 4d 65 6d 20 6f 62 6a 65 63 74 20 4d 45 4d 5f 44  Mem object MEM_D
19606 79 6e 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f  yn.  In other wo
19607 72 64 73 2c 20 6d 61 6b 65 20 69 74 20 73 6f 0a  rds, make it so.
19608 2a 2a 20 74 68 61 74 20 61 6e 79 20 54 45 58 54  ** that any TEXT
19609 20 6f 72 20 42 4c 4f 42 20 63 6f 6e 74 65 6e 74   or BLOB content
1960a 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 6d 65   is stored in me
1960b 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72  mory obtained fr
1960c 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 2e 20  om.** malloc(). 
1960d 20 49 6e 20 74 68 69 73 20 77 61 79 2c 20 77 65   In this way, we
1960e 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 20 6d   know that the m
1960f 65 6d 6f 72 79 20 69 73 20 73 61 66 65 20 74 6f  emory is safe to
19610 20 62 65 0a 2a 2a 20 6f 76 65 72 77 72 69 74 74   be.** overwritt
19611 65 6e 20 6f 72 20 61 6c 74 65 72 65 64 2e 0a 2a  en or altered..*
19612 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49  *.** Return SQLI
19613 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73  TE_OK on success
19614 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d   or SQLITE_NOMEM
19615 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73   if malloc fails
19616 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
19617 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
19618 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61  dbeMemMakeWritea
19619 62 6c 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a  ble(Mem *pMem){.
1961a 20 20 69 6e 74 20 66 3b 0a 20 20 61 73 73 65 72    int f;.  asser
1961b 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c  t( pMem->db==0 |
1961c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
1961d 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d  held(pMem->db->m
1961e 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
1961f 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26  t( (pMem->flags&
19620 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29  MEM_RowSet)==0 )
19621 3b 0a 20 20 65 78 70 61 6e 64 42 6c 6f 62 28 70  ;.  expandBlob(p
19622 4d 65 6d 29 3b 0a 20 20 66 20 3d 20 70 4d 65 6d  Mem);.  f = pMem
19623 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 28 20 28  ->flags;.  if( (
19624 66 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42  f&(MEM_Str|MEM_B
19625 6c 6f 62 29 29 20 26 26 20 70 4d 65 6d 2d 3e 7a  lob)) && pMem->z
19626 21 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20  !=pMem->zMalloc 
19627 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ){.    if( sqlit
19628 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d  e3VdbeMemGrow(pM
19629 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 20 2b 20 32 2c  em, pMem->n + 2,
1962a 20 31 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74   1) ){.      ret
1962b 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
1962c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d  ;.    }.    pMem
1962d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 30  ->z[pMem->n] = 0
1962e 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d  ;.    pMem->z[pM
1962f 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 30 3b 0a 20 20  em->n+1] = 0;.  
19630 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d    pMem->flags |=
19631 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 7d 0a 0a   MEM_Term;.  }..
19632 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
19633 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  OK;.}../*.** If 
19634 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 2a 20 68  the given Mem* h
19635 61 73 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64  as a zero-filled
19636 20 74 61 69 6c 2c 20 74 75 72 6e 20 69 74 20 69   tail, turn it i
19637 6e 74 6f 20 61 6e 20 6f 72 64 69 6e 61 72 79 0a  nto an ordinary.
19638 2a 2a 20 62 6c 6f 62 20 73 74 6f 72 65 64 20 69  ** blob stored i
19639 6e 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c  n dynamically al
1963a 6c 6f 63 61 74 65 64 20 73 70 61 63 65 2e 0a 2a  located space..*
1963b 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
1963c 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 53  _OMIT_INCRBLOB.S
1963d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1963e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
1963f 45 78 70 61 6e 64 42 6c 6f 62 28 4d 65 6d 20 2a  ExpandBlob(Mem *
19640 70 4d 65 6d 29 7b 0a 20 20 69 66 28 20 70 4d 65  pMem){.  if( pMe
19641 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a  m->flags & MEM_Z
19642 65 72 6f 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e  ero ){.    int n
19643 42 79 74 65 3b 0a 20 20 20 20 61 73 73 65 72 74  Byte;.    assert
19644 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45  ( pMem->flags&ME
19645 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 61 73  M_Blob );.    as
19646 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61  sert( (pMem->fla
19647 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d  gs&MEM_RowSet)==
19648 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
19649 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20   pMem->db==0 || 
1964a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
1964b 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74  ld(pMem->db->mut
1964c 65 78 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 53  ex) );..    /* S
1964d 65 74 20 6e 42 79 74 65 20 74 6f 20 74 68 65 20  et nByte to the 
1964e 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
1964f 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f 72  required to stor
19650 65 20 74 68 65 20 65 78 70 61 6e 64 65 64 20 62  e the expanded b
19651 6c 6f 62 2e 20 2a 2f 0a 20 20 20 20 6e 42 79 74  lob. */.    nByt
19652 65 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2b 20 70 4d  e = pMem->n + pM
19653 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20  em->u.nZero;.   
19654 20 69 66 28 20 6e 42 79 74 65 3c 3d 30 20 29 7b   if( nByte<=0 ){
19655 0a 20 20 20 20 20 20 6e 42 79 74 65 20 3d 20 31  .      nByte = 1
19656 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
19657 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72  sqlite3VdbeMemGr
19658 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20  ow(pMem, nByte, 
19659 31 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  1) ){.      retu
1965a 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
1965b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6d 65 6d 73  .    }..    mems
1965c 65 74 28 26 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d  et(&pMem->z[pMem
1965d 2d 3e 6e 5d 2c 20 30 2c 20 70 4d 65 6d 2d 3e 75  ->n], 0, pMem->u
1965e 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20 20 70 4d 65  .nZero);.    pMe
1965f 6d 2d 3e 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75 2e  m->n += pMem->u.
19660 6e 5a 65 72 6f 3b 0a 20 20 20 20 70 4d 65 6d 2d  nZero;.    pMem-
19661 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f  >flags &= ~(MEM_
19662 5a 65 72 6f 7c 4d 45 4d 5f 54 65 72 6d 29 3b 0a  Zero|MEM_Term);.
19663 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
19664 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66  ITE_OK;.}.#endif
19665 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75  .../*.** Make su
19666 72 65 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d  re the given Mem
19667 20 69 73 20 5c 75 30 30 30 30 20 74 65 72 6d 69   is \u0000 termi
19668 6e 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  nated..*/.SQLITE
19669 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1966a 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65  ite3VdbeMemNulTe
1966b 72 6d 69 6e 61 74 65 28 4d 65 6d 20 2a 70 4d 65  rminate(Mem *pMe
1966c 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d  m){.  assert( pM
1966d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  em->db==0 || sql
1966e 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1966f 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  pMem->db->mutex)
19670 20 29 3b 0a 20 20 69 66 28 20 28 70 4d 65 6d 2d   );.  if( (pMem-
19671 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 54 65 72  >flags & MEM_Ter
19672 6d 29 21 3d 30 20 7c 7c 20 28 70 4d 65 6d 2d 3e  m)!=0 || (pMem->
19673 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 29  flags & MEM_Str)
19674 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
19675 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 2f  n SQLITE_OK;   /
19676 2a 20 4e 6f 74 68 69 6e 67 20 74 6f 20 64 6f 20  * Nothing to do 
19677 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c  */.  }.  if( sql
19678 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28  ite3VdbeMemGrow(
19679 70 4d 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 2b 32 2c  pMem, pMem->n+2,
1967a 20 31 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72   1) ){.    retur
1967b 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
1967c 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d    }.  pMem->z[pM
1967d 65 6d 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 20 70 4d  em->n] = 0;.  pM
1967e 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d  em->z[pMem->n+1]
1967f 20 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c   = 0;.  pMem->fl
19680 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b  ags |= MEM_Term;
19681 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
19682 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  _OK;.}../*.** Ad
19683 64 20 4d 45 4d 5f 53 74 72 20 74 6f 20 74 68 65  d MEM_Str to the
19684 20 73 65 74 20 6f 66 20 72 65 70 72 65 73 65 6e   set of represen
19685 74 61 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 20  tations for the 
19686 67 69 76 65 6e 20 4d 65 6d 2e 20 20 4e 75 6d 62  given Mem.  Numb
19687 65 72 73 0a 2a 2a 20 61 72 65 20 63 6f 6e 76 65  ers.** are conve
19688 72 74 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74  rted using sqlit
19689 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 2e 20 20  e3_snprintf().  
1968a 43 6f 6e 76 65 72 74 69 6e 67 20 61 20 42 4c 4f  Converting a BLO
1968b 42 20 74 6f 20 61 20 73 74 72 69 6e 67 0a 2a 2a  B to a string.**
1968c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a   is a no-op..**.
1968d 2a 2a 20 45 78 69 73 74 69 6e 67 20 72 65 70 72  ** Existing repr
1968e 65 73 65 6e 74 61 74 69 6f 6e 73 20 4d 45 4d 5f  esentations MEM_
1968f 49 6e 74 20 61 6e 64 20 4d 45 4d 5f 52 65 61 6c  Int and MEM_Real
19690 20 61 72 65 20 2a 6e 6f 74 2a 20 69 6e 76 61 6c   are *not* inval
19691 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20  idated..**.** A 
19692 4d 45 4d 5f 4e 75 6c 6c 20 76 61 6c 75 65 20 77  MEM_Null value w
19693 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 70 61 73  ill never be pas
19694 73 65 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63  sed to this func
19695 74 69 6f 6e 2e 20 54 68 69 73 20 66 75 6e 63 74  tion. This funct
19696 69 6f 6e 20 69 73 0a 2a 2a 20 75 73 65 64 20 66  ion is.** used f
19697 6f 72 20 63 6f 6e 76 65 72 74 69 6e 67 20 76 61  or converting va
19698 6c 75 65 73 20 74 6f 20 74 65 78 74 20 66 6f 72  lues to text for
19699 20 72 65 74 75 72 6e 69 6e 67 20 74 6f 20 74 68   returning to th
1969a 65 20 75 73 65 72 20 28 69 2e 65 2e 20 76 69 61  e user (i.e. via
1969b 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  .** sqlite3_valu
1969c 65 5f 74 65 78 74 28 29 29 2c 20 6f 72 20 66 6f  e_text()), or fo
1969d 72 20 65 6e 73 75 72 69 6e 67 20 74 68 61 74 20  r ensuring that 
1969e 76 61 6c 75 65 73 20 74 6f 20 62 65 20 75 73 65  values to be use
1969f 64 20 61 73 20 62 74 72 65 65 0a 2a 2a 20 6b 65  d as btree.** ke
196a0 79 73 20 61 72 65 20 73 74 72 69 6e 67 73 2e 20  ys are strings. 
196a1 49 6e 20 74 68 65 20 66 6f 72 6d 65 72 20 63 61  In the former ca
196a2 73 65 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  se a NULL pointe
196a3 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 68  r is returned th
196a4 65 0a 2a 2a 20 75 73 65 72 20 61 6e 64 20 74 68  e.** user and th
196a5 65 20 6c 61 74 65 72 20 69 73 20 61 6e 20 69 6e  e later is an in
196a6 74 65 72 6e 61 6c 20 70 72 6f 67 72 61 6d 6d 69  ternal programmi
196a7 6e 67 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c  ng error..*/.SQL
196a8 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
196a9 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74  sqlite3VdbeMemSt
196aa 72 69 6e 67 69 66 79 28 4d 65 6d 20 2a 70 4d 65  ringify(Mem *pMe
196ab 6d 2c 20 69 6e 74 20 65 6e 63 29 7b 0a 20 20 69  m, int enc){.  i
196ac 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
196ad 4b 3b 0a 20 20 69 6e 74 20 66 67 20 3d 20 70 4d  K;.  int fg = pM
196ae 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 63 6f 6e  em->flags;.  con
196af 73 74 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 33  st int nByte = 3
196b0 32 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4d  2;..  assert( pM
196b1 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  em->db==0 || sql
196b2 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
196b3 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  pMem->db->mutex)
196b4 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 28   );.  assert( !(
196b5 66 67 26 4d 45 4d 5f 5a 65 72 6f 29 20 29 3b 0a  fg&MEM_Zero) );.
196b6 20 20 61 73 73 65 72 74 28 20 21 28 66 67 26 28    assert( !(fg&(
196b7 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62  MEM_Str|MEM_Blob
196b8 29 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  )) );.  assert( 
196b9 66 67 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f  fg&(MEM_Int|MEM_
196ba 52 65 61 6c 29 20 29 3b 0a 20 20 61 73 73 65 72  Real) );.  asser
196bb 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26  t( (pMem->flags&
196bc 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29  MEM_RowSet)==0 )
196bd 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48  ;.  assert( EIGH
196be 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54  T_BYTE_ALIGNMENT
196bf 28 70 4d 65 6d 29 20 29 3b 0a 0a 0a 20 20 69 66  (pMem) );...  if
196c0 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  ( sqlite3VdbeMem
196c1 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65  Grow(pMem, nByte
196c2 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75  , 0) ){.    retu
196c3 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
196c4 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 6f 72 20 61  .  }..  /* For a
196c5 20 52 65 61 6c 20 6f 72 20 49 6e 74 65 67 65 72   Real or Integer
196c6 2c 20 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 70  , use sqlite3_mp
196c7 72 69 6e 74 66 28 29 20 74 6f 20 70 72 6f 64 75  rintf() to produ
196c8 63 65 20 74 68 65 20 55 54 46 2d 38 0a 20 20 2a  ce the UTF-8.  *
196c9 2a 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65  * string represe
196ca 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 76  ntation of the v
196cb 61 6c 75 65 2e 20 54 68 65 6e 2c 20 69 66 20 74  alue. Then, if t
196cc 68 65 20 72 65 71 75 69 72 65 64 20 65 6e 63 6f  he required enco
196cd 64 69 6e 67 0a 20 20 2a 2a 20 69 73 20 55 54 46  ding.  ** is UTF
196ce 2d 31 36 6c 65 20 6f 72 20 55 54 46 2d 31 36 62  -16le or UTF-16b
196cf 65 20 64 6f 20 61 20 74 72 61 6e 73 6c 61 74 69  e do a translati
196d0 6f 6e 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 46  on..  ** .  ** F
196d1 49 58 20 4d 45 3a 20 49 74 20 77 6f 75 6c 64 20  IX ME: It would 
196d2 62 65 20 62 65 74 74 65 72 20 69 66 20 73 71 6c  be better if sql
196d3 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 20  ite3_snprintf() 
196d4 63 6f 75 6c 64 20 64 6f 20 55 54 46 2d 31 36 2e  could do UTF-16.
196d5 0a 20 20 2a 2f 0a 20 20 69 66 28 20 66 67 20 26  .  */.  if( fg &
196d6 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20   MEM_Int ){.    
196d7 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
196d8 28 6e 42 79 74 65 2c 20 70 4d 65 6d 2d 3e 7a 2c  (nByte, pMem->z,
196d9 20 22 25 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75   "%lld", pMem->u
196da 2e 69 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  .i);.  }else{.  
196db 20 20 61 73 73 65 72 74 28 20 66 67 20 26 20 4d    assert( fg & M
196dc 45 4d 5f 52 65 61 6c 20 29 3b 0a 20 20 20 20 73  EM_Real );.    s
196dd 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
196de 6e 42 79 74 65 2c 20 70 4d 65 6d 2d 3e 7a 2c 20  nByte, pMem->z, 
196df 22 25 21 2e 31 35 67 22 2c 20 70 4d 65 6d 2d 3e  "%!.15g", pMem->
196e0 72 29 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e  r);.  }.  pMem->
196e1 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  n = sqlite3Strle
196e2 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20  n30(pMem->z);.  
196e3 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49  pMem->enc = SQLI
196e4 54 45 5f 55 54 46 38 3b 0a 20 20 70 4d 65 6d 2d  TE_UTF8;.  pMem-
196e5 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74  >flags |= MEM_St
196e6 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 73 71  r|MEM_Term;.  sq
196e7 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45  lite3VdbeChangeE
196e8 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 65 6e  ncoding(pMem, en
196e9 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  c);.  return rc;
196ea 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 6d 6f 72 79  .}../*.** Memory
196eb 20 63 65 6c 6c 20 70 4d 65 6d 20 63 6f 6e 74 61   cell pMem conta
196ec 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 78 74 20  ins the context 
196ed 6f 66 20 61 6e 20 61 67 67 72 65 67 61 74 65 20  of an aggregate 
196ee 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69  function..** Thi
196ef 73 20 72 6f 75 74 69 6e 65 20 63 61 6c 6c 73 20  s routine calls 
196f0 74 68 65 20 66 69 6e 61 6c 69 7a 65 20 6d 65 74  the finalize met
196f1 68 6f 64 20 66 6f 72 20 74 68 61 74 20 66 75 6e  hod for that fun
196f2 63 74 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20 72  ction.  The.** r
196f3 65 73 75 6c 74 20 6f 66 20 74 68 65 20 61 67 67  esult of the agg
196f4 72 65 67 61 74 65 20 69 73 20 73 74 6f 72 65 64  regate is stored
196f5 20 62 61 63 6b 20 69 6e 74 6f 20 70 4d 65 6d 2e   back into pMem.
196f6 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51  .**.** Return SQ
196f7 4c 49 54 45 5f 45 52 52 4f 52 20 69 66 20 74 68  LITE_ERROR if th
196f8 65 20 66 69 6e 61 6c 69 7a 65 72 20 72 65 70 6f  e finalizer repo
196f9 72 74 73 20 61 6e 20 65 72 72 6f 72 2e 20 20 53  rts an error.  S
196fa 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 74 68 65  QLITE_OK.** othe
196fb 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  rwise..*/.SQLITE
196fc 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
196fd 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e 61 6c  ite3VdbeMemFinal
196fe 69 7a 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 46  ize(Mem *pMem, F
196ff 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 29 7b 0a  uncDef *pFunc){.
19700 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
19701 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 41 4c 57 41  E_OK;.  if( ALWA
19702 59 53 28 70 46 75 6e 63 20 26 26 20 70 46 75 6e  YS(pFunc && pFun
19703 63 2d 3e 78 46 69 6e 61 6c 69 7a 65 29 20 29 7b  c->xFinalize) ){
19704 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  .    sqlite3_con
19705 74 65 78 74 20 63 74 78 3b 0a 20 20 20 20 61 73  text ctx;.    as
19706 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61  sert( (pMem->fla
19707 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d  gs & MEM_Null)!=
19708 30 20 7c 7c 20 70 46 75 6e 63 3d 3d 70 4d 65 6d  0 || pFunc==pMem
19709 2d 3e 75 2e 70 44 65 66 20 29 3b 0a 20 20 20 20  ->u.pDef );.    
1970a 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62  assert( pMem->db
1970b 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d  ==0 || sqlite3_m
1970c 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e  utex_held(pMem->
1970d 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
1970e 20 20 6d 65 6d 73 65 74 28 26 63 74 78 2c 20 30    memset(&ctx, 0
1970f 2c 20 73 69 7a 65 6f 66 28 63 74 78 29 29 3b 0a  , sizeof(ctx));.
19710 20 20 20 20 63 74 78 2e 73 2e 66 6c 61 67 73 20      ctx.s.flags 
19711 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20  = MEM_Null;.    
19712 63 74 78 2e 73 2e 64 62 20 3d 20 70 4d 65 6d 2d  ctx.s.db = pMem-
19713 3e 64 62 3b 0a 20 20 20 20 63 74 78 2e 70 4d 65  >db;.    ctx.pMe
19714 6d 20 3d 20 70 4d 65 6d 3b 0a 20 20 20 20 63 74  m = pMem;.    ct
19715 78 2e 70 46 75 6e 63 20 3d 20 70 46 75 6e 63 3b  x.pFunc = pFunc;
19716 0a 20 20 20 20 70 46 75 6e 63 2d 3e 78 46 69 6e  .    pFunc->xFin
19717 61 6c 69 7a 65 28 26 63 74 78 29 3b 0a 20 20 20  alize(&ctx);.   
19718 20 61 73 73 65 72 74 28 20 30 3d 3d 28 70 4d 65   assert( 0==(pMe
19719 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e  m->flags&MEM_Dyn
1971a 29 20 26 26 20 21 70 4d 65 6d 2d 3e 78 44 65 6c  ) && !pMem->xDel
1971b 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44   );.    sqlite3D
1971c 62 46 72 65 65 28 70 4d 65 6d 2d 3e 64 62 2c 20  bFree(pMem->db, 
1971d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a  pMem->zMalloc);.
1971e 20 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d 2c      memcpy(pMem,
1971f 20 26 63 74 78 2e 73 2c 20 73 69 7a 65 6f 66 28   &ctx.s, sizeof(
19720 63 74 78 2e 73 29 29 3b 0a 20 20 20 20 72 63 20  ctx.s));.    rc 
19721 3d 20 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a 20  = ctx.isError;. 
19722 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
19723 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20  }../*.** If the 
19724 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 63 6f 6e 74  memory cell cont
19725 61 69 6e 73 20 61 20 73 74 72 69 6e 67 20 76 61  ains a string va
19726 6c 75 65 20 74 68 61 74 20 6d 75 73 74 20 62 65  lue that must be
19727 20 66 72 65 65 64 20 62 79 0a 2a 2a 20 69 6e 76   freed by.** inv
19728 6f 6b 69 6e 67 20 61 6e 20 65 78 74 65 72 6e 61  oking an externa
19729 6c 20 63 61 6c 6c 62 61 63 6b 2c 20 66 72 65 65  l callback, free
1972a 20 69 74 20 6e 6f 77 2e 20 43 61 6c 6c 69 6e 67   it now. Calling
1972b 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a   this function.*
1972c 2a 20 64 6f 65 73 20 6e 6f 74 20 66 72 65 65 20  * does not free 
1972d 61 6e 79 20 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20  any Mem.zMalloc 
1972e 62 75 66 66 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54  buffer..*/.SQLIT
1972f 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
19730 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
19731 65 61 73 65 45 78 74 65 72 6e 61 6c 28 4d 65 6d  easeExternal(Mem
19732 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20   *p){.  assert( 
19733 70 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69  p->db==0 || sqli
19734 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
19735 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
19736 20 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e 66    testcase( p->f
19737 6c 61 67 73 20 26 20 4d 45 4d 5f 41 67 67 20 29  lags & MEM_Agg )
19738 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 2d  ;.  testcase( p-
19739 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e  >flags & MEM_Dyn
1973a 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20   );.  testcase( 
1973b 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52  p->flags & MEM_R
1973c 6f 77 53 65 74 20 29 3b 0a 20 20 74 65 73 74 63  owSet );.  testc
1973d 61 73 65 28 20 70 2d 3e 66 6c 61 67 73 20 26 20  ase( p->flags & 
1973e 4d 45 4d 5f 46 72 61 6d 65 20 29 3b 0a 20 20 69  MEM_Frame );.  i
1973f 66 28 20 70 2d 3e 66 6c 61 67 73 26 28 4d 45 4d  f( p->flags&(MEM
19740 5f 41 67 67 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d  _Agg|MEM_Dyn|MEM
19741 5f 52 6f 77 53 65 74 7c 4d 45 4d 5f 46 72 61 6d  _RowSet|MEM_Fram
19742 65 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d  e) ){.    if( p-
19743 3e 66 6c 61 67 73 26 4d 45 4d 5f 41 67 67 20 29  >flags&MEM_Agg )
19744 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
19745 64 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 70  dbeMemFinalize(p
19746 2c 20 70 2d 3e 75 2e 70 44 65 66 29 3b 0a 20 20  , p->u.pDef);.  
19747 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e      assert( (p->
19748 66 6c 61 67 73 20 26 20 4d 45 4d 5f 41 67 67 29  flags & MEM_Agg)
19749 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ==0 );.      sql
1974a 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
1974b 73 65 28 70 29 3b 0a 20 20 20 20 7d 65 6c 73 65  se(p);.    }else
1974c 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 4d 45   if( p->flags&ME
1974d 4d 5f 44 79 6e 20 26 26 20 70 2d 3e 78 44 65 6c  M_Dyn && p->xDel
1974e 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
1974f 28 20 28 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  ( (p->flags&MEM_
19750 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20  RowSet)==0 );.  
19751 20 20 20 20 70 2d 3e 78 44 65 6c 28 28 76 6f 69      p->xDel((voi
19752 64 20 2a 29 70 2d 3e 7a 29 3b 0a 20 20 20 20 20  d *)p->z);.     
19753 20 70 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20   p->xDel = 0;.  
19754 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66    }else if( p->f
19755 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 20  lags&MEM_RowSet 
19756 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
19757 52 6f 77 53 65 74 43 6c 65 61 72 28 70 2d 3e 75  RowSetClear(p->u
19758 2e 70 52 6f 77 53 65 74 29 3b 0a 20 20 20 20 7d  .pRowSet);.    }
19759 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67  else if( p->flag
1975a 73 26 4d 45 4d 5f 46 72 61 6d 65 20 29 7b 0a 20  s&MEM_Frame ){. 
1975b 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1975c 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 29 3b 0a 20  MemSetNull(p);. 
1975d 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
1975e 2a 20 52 65 6c 65 61 73 65 20 61 6e 79 20 6d 65  * Release any me
1975f 6d 6f 72 79 20 68 65 6c 64 20 62 79 20 74 68 65  mory held by the
19760 20 4d 65 6d 2e 20 54 68 69 73 20 6d 61 79 20 6c   Mem. This may l
19761 65 61 76 65 20 74 68 65 20 4d 65 6d 20 69 6e 20  eave the Mem in 
19762 61 6e 0a 2a 2a 20 69 6e 63 6f 6e 73 69 73 74 65  an.** inconsiste
19763 6e 74 20 73 74 61 74 65 2c 20 66 6f 72 20 65 78  nt state, for ex
19764 61 6d 70 6c 65 20 77 69 74 68 20 28 4d 65 6d 2e  ample with (Mem.
19765 7a 3d 3d 30 29 20 61 6e 64 0a 2a 2a 20 28 4d 65  z==0) and.** (Me
19766 6d 2e 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f 54  m.type==SQLITE_T
19767 45 58 54 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  EXT)..*/.SQLITE_
19768 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
19769 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
1976a 73 65 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 73 71  se(Mem *p){.  sq
1976b 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
1976c 61 73 65 45 78 74 65 72 6e 61 6c 28 70 29 3b 0a  aseExternal(p);.
1976d 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1976e 70 2d 3e 64 62 2c 20 70 2d 3e 7a 4d 61 6c 6c 6f  p->db, p->zMallo
1976f 63 29 3b 0a 20 20 70 2d 3e 7a 20 3d 20 30 3b 0a  c);.  p->z = 0;.
19770 20 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30    p->zMalloc = 0
19771 3b 0a 20 20 70 2d 3e 78 44 65 6c 20 3d 20 30 3b  ;.  p->xDel = 0;
19772 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  .}../*.** Conver
19773 74 20 61 20 36 34 2d 62 69 74 20 49 45 45 45 20  t a 64-bit IEEE 
19774 64 6f 75 62 6c 65 20 69 6e 74 6f 20 61 20 36 34  double into a 64
19775 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65  -bit signed inte
19776 67 65 72 2e 0a 2a 2a 20 49 66 20 74 68 65 20 64  ger..** If the d
19777 6f 75 62 6c 65 20 69 73 20 74 6f 6f 20 6c 61 72  ouble is too lar
19778 67 65 2c 20 72 65 74 75 72 6e 20 30 78 38 30 30  ge, return 0x800
19779 30 30 30 30 30 30 30 30 30 30 30 30 30 2e 0a 2a  0000000000000..*
1977a 2a 0a 2a 2a 20 4d 6f 73 74 20 73 79 73 74 65 6d  *.** Most system
1977b 73 20 61 70 70 65 61 72 20 74 6f 20 64 6f 20 74  s appear to do t
1977c 68 69 73 20 73 69 6d 70 6c 79 20 62 79 20 61 73  his simply by as
1977d 73 69 67 6e 69 6e 67 0a 2a 2a 20 76 61 72 69 61  signing.** varia
1977e 62 6c 65 73 20 61 6e 64 20 77 69 74 68 6f 75 74  bles and without
1977f 20 74 68 65 20 65 78 74 72 61 20 72 61 6e 67 65   the extra range
19780 20 74 65 73 74 73 2e 20 20 42 75 74 0a 2a 2a 20   tests.  But.** 
19781 74 68 65 72 65 20 61 72 65 20 72 65 70 6f 72 74  there are report
19782 73 20 74 68 61 74 20 77 69 6e 64 6f 77 73 20 74  s that windows t
19783 68 72 6f 77 73 20 61 6e 20 65 78 70 65 63 74 69  hrows an expecti
19784 6f 6e 0a 2a 2a 20 69 66 20 74 68 65 20 66 6c 6f  on.** if the flo
19785 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75  ating point valu
19786 65 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67  e is out of rang
19787 65 2e 20 28 53 65 65 20 74 69 63 6b 65 74 20 23  e. (See ticket #
19788 32 38 38 30 2e 29 0a 2a 2a 20 42 65 63 61 75 73  2880.).** Becaus
19789 65 20 77 65 20 64 6f 20 6e 6f 74 20 63 6f 6d 70  e we do not comp
1978a 6c 65 74 65 6c 79 20 75 6e 64 65 72 73 74 61 6e  letely understan
1978b 64 20 74 68 65 20 70 72 6f 62 6c 65 6d 2c 20 77  d the problem, w
1978c 65 20 77 69 6c 6c 0a 2a 2a 20 74 61 6b 65 20 74  e will.** take t
1978d 68 65 20 63 6f 6e 73 65 72 76 61 74 69 76 65 20  he conservative 
1978e 61 70 70 72 6f 61 63 68 20 61 6e 64 20 61 6c 77  approach and alw
1978f 61 79 73 20 64 6f 20 72 61 6e 67 65 20 74 65 73  ays do range tes
19790 74 73 0a 2a 2a 20 62 65 66 6f 72 65 20 61 74 74  ts.** before att
19791 65 6d 70 74 69 6e 67 20 74 68 65 20 63 6f 6e 76  empting the conv
19792 65 72 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  ersion..*/.stati
19793 63 20 69 36 34 20 64 6f 75 62 6c 65 54 6f 49 6e  c i64 doubleToIn
19794 74 36 34 28 64 6f 75 62 6c 65 20 72 29 7b 0a 20  t64(double r){. 
19795 20 2f 2a 0a 20 20 2a 2a 20 4d 61 6e 79 20 63 6f   /*.  ** Many co
19796 6d 70 69 6c 65 72 73 20 77 65 20 65 6e 63 6f 75  mpilers we encou
19797 6e 74 65 72 20 64 6f 20 6e 6f 74 20 64 65 66 69  nter do not defi
19798 6e 65 20 63 6f 6e 73 74 61 6e 74 73 20 66 6f 72  ne constants for
19799 20 74 68 65 0a 20 20 2a 2a 20 6d 69 6e 69 6d 75   the.  ** minimu
1979a 6d 20 61 6e 64 20 6d 61 78 69 6d 75 6d 20 36 34  m and maximum 64
1979b 2d 62 69 74 20 69 6e 74 65 67 65 72 73 2c 20 6f  -bit integers, o
1979c 72 20 74 68 65 79 20 64 65 66 69 6e 65 20 74 68  r they define th
1979d 65 6d 0a 20 20 2a 2a 20 69 6e 63 6f 6e 73 69 73  em.  ** inconsis
1979e 74 65 6e 74 6c 79 2e 20 20 41 6e 64 20 6d 61 6e  tently.  And man
1979f 79 20 64 6f 20 6e 6f 74 20 75 6e 64 65 72 73 74  y do not underst
197a0 61 6e 64 20 74 68 65 20 22 4c 4c 22 20 6e 6f 74  and the "LL" not
197a1 61 74 69 6f 6e 2e 0a 20 20 2a 2a 20 53 6f 20 77  ation..  ** So w
197a2 65 20 64 65 66 69 6e 65 20 6f 75 72 20 6f 77 6e  e define our own
197a3 20 73 74 61 74 69 63 20 63 6f 6e 73 74 61 6e 74   static constant
197a4 73 20 68 65 72 65 20 75 73 69 6e 67 20 6e 6f 74  s here using not
197a5 68 69 6e 67 0a 20 20 2a 2a 20 6c 61 72 67 65 72  hing.  ** larger
197a6 20 74 68 61 6e 20 61 20 33 32 2d 62 69 74 20 69   than a 32-bit i
197a7 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 2e  nteger constant.
197a8 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63  .  */.  static c
197a9 6f 6e 73 74 20 69 36 34 20 6d 61 78 49 6e 74 20  onst i64 maxInt 
197aa 3d 20 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 3b  = LARGEST_INT64;
197ab 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
197ac 69 36 34 20 6d 69 6e 49 6e 74 20 3d 20 53 4d 41  i64 minInt = SMA
197ad 4c 4c 45 53 54 5f 49 4e 54 36 34 3b 0a 0a 20 20  LLEST_INT64;..  
197ae 69 66 28 20 72 3c 28 64 6f 75 62 6c 65 29 6d 69  if( r<(double)mi
197af 6e 49 6e 74 20 29 7b 0a 20 20 20 20 72 65 74 75  nInt ){.    retu
197b0 72 6e 20 6d 69 6e 49 6e 74 3b 0a 20 20 7d 65 6c  rn minInt;.  }el
197b1 73 65 20 69 66 28 20 72 3e 28 64 6f 75 62 6c 65  se if( r>(double
197b2 29 6d 61 78 49 6e 74 20 29 7b 0a 20 20 20 20 2f  )maxInt ){.    /
197b3 2a 20 6d 69 6e 49 6e 74 20 69 73 20 63 6f 72 72  * minInt is corr
197b4 65 63 74 20 68 65 72 65 20 2d 20 6e 6f 74 20 6d  ect here - not m
197b5 61 78 49 6e 74 2e 20 20 49 74 20 74 75 72 6e 73  axInt.  It turns
197b6 20 6f 75 74 20 74 68 61 74 20 61 73 73 69 67 6e   out that assign
197b7 69 6e 67 0a 20 20 20 20 2a 2a 20 61 20 76 65 72  ing.    ** a ver
197b8 79 20 6c 61 72 67 65 20 70 6f 73 69 74 69 76 65  y large positive
197b9 20 6e 75 6d 62 65 72 20 74 6f 20 61 6e 20 69 6e   number to an in
197ba 74 65 67 65 72 20 72 65 73 75 6c 74 73 20 69 6e  teger results in
197bb 20 61 20 76 65 72 79 20 6c 61 72 67 65 0a 20 20   a very large.  
197bc 20 20 2a 2a 20 6e 65 67 61 74 69 76 65 20 69 6e    ** negative in
197bd 74 65 67 65 72 2e 20 20 54 68 69 73 20 6d 61 6b  teger.  This mak
197be 65 73 20 6e 6f 20 73 65 6e 73 65 2c 20 62 75 74  es no sense, but
197bf 20 69 74 20 69 73 20 77 68 61 74 20 78 38 36 20   it is what x86 
197c0 68 61 72 64 77 61 72 65 0a 20 20 20 20 2a 2a 20  hardware.    ** 
197c1 64 6f 65 73 20 73 6f 20 66 6f 72 20 63 6f 6d 70  does so for comp
197c2 61 74 69 62 69 6c 69 74 79 20 77 65 20 77 69 6c  atibility we wil
197c3 6c 20 64 6f 20 74 68 65 20 73 61 6d 65 20 69 6e  l do the same in
197c4 20 73 6f 66 74 77 61 72 65 2e 20 2a 2f 0a 20 20   software. */.  
197c5 20 20 72 65 74 75 72 6e 20 6d 69 6e 49 6e 74 3b    return minInt;
197c6 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
197c7 74 75 72 6e 20 28 69 36 34 29 72 3b 0a 20 20 7d  turn (i64)r;.  }
197c8 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
197c9 20 73 6f 6d 65 20 6b 69 6e 64 20 6f 66 20 69 6e   some kind of in
197ca 74 65 67 65 72 20 76 61 6c 75 65 20 77 68 69 63  teger value whic
197cb 68 20 69 73 20 74 68 65 20 62 65 73 74 20 77 65  h is the best we
197cc 20 63 61 6e 20 64 6f 0a 2a 2a 20 61 74 20 72 65   can do.** at re
197cd 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 76  presenting the v
197ce 61 6c 75 65 20 74 68 61 74 20 2a 70 4d 65 6d 20  alue that *pMem 
197cf 64 65 73 63 72 69 62 65 73 20 61 73 20 61 6e 20  describes as an 
197d0 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 49 66 20 70  integer..** If p
197d1 4d 65 6d 20 69 73 20 61 6e 20 69 6e 74 65 67 65  Mem is an intege
197d2 72 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75  r, then the valu
197d3 65 20 69 73 20 65 78 61 63 74 2e 20 20 49 66 20  e is exact.  If 
197d4 70 4d 65 6d 20 69 73 0a 2a 2a 20 61 20 66 6c 6f  pMem is.** a flo
197d5 61 74 69 6e 67 2d 70 6f 69 6e 74 20 74 68 65 6e  ating-point then
197d6 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   the value retur
197d7 6e 65 64 20 69 73 20 74 68 65 20 69 6e 74 65 67  ned is the integ
197d8 65 72 20 70 61 72 74 2e 0a 2a 2a 20 49 66 20 70  er part..** If p
197d9 4d 65 6d 20 69 73 20 61 20 73 74 72 69 6e 67 20  Mem is a string 
197da 6f 72 20 62 6c 6f 62 2c 20 74 68 65 6e 20 77 65  or blob, then we
197db 20 6d 61 6b 65 20 61 6e 20 61 74 74 65 6d 70 74   make an attempt
197dc 20 74 6f 20 63 6f 6e 76 65 72 74 0a 2a 2a 20 69   to convert.** i
197dd 74 20 69 6e 74 6f 20 61 20 69 6e 74 65 67 65 72  t into a integer
197de 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 61 74   and return that
197df 2e 20 20 49 66 20 70 4d 65 6d 20 72 65 70 72 65  .  If pMem repre
197e0 73 65 6e 74 73 20 61 6e 0a 2a 2a 20 61 6e 20 53  sents an.** an S
197e1 51 4c 2d 4e 55 4c 4c 20 76 61 6c 75 65 2c 20 72  QL-NULL value, r
197e2 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 49  eturn 0..**.** I
197e3 66 20 70 4d 65 6d 20 72 65 70 72 65 73 65 6e 74  f pMem represent
197e4 73 20 61 20 73 74 72 69 6e 67 20 76 61 6c 75 65  s a string value
197e5 2c 20 69 74 73 20 65 6e 63 6f 64 69 6e 67 20 6d  , its encoding m
197e6 69 67 68 74 20 62 65 20 63 68 61 6e 67 65 64 2e  ight be changed.
197e7 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
197e8 54 45 20 69 36 34 20 73 71 6c 69 74 65 33 56 64  TE i64 sqlite3Vd
197e9 62 65 49 6e 74 56 61 6c 75 65 28 4d 65 6d 20 2a  beIntValue(Mem *
197ea 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 66 6c 61  pMem){.  int fla
197eb 67 73 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d  gs;.  assert( pM
197ec 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  em->db==0 || sql
197ed 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
197ee 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  pMem->db->mutex)
197ef 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49   );.  assert( EI
197f0 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45  GHT_BYTE_ALIGNME
197f1 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 20 20 66 6c  NT(pMem) );.  fl
197f2 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67  ags = pMem->flag
197f3 73 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26  s;.  if( flags &
197f4 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20   MEM_Int ){.    
197f5 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 75 2e 69  return pMem->u.i
197f6 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c  ;.  }else if( fl
197f7 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29  ags & MEM_Real )
197f8 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 64 6f 75  {.    return dou
197f9 62 6c 65 54 6f 49 6e 74 36 34 28 70 4d 65 6d 2d  bleToInt64(pMem-
197fa 3e 72 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  >r);.  }else if(
197fb 20 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74   flags & (MEM_St
197fc 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20  r|MEM_Blob) ){. 
197fd 20 20 20 69 36 34 20 76 61 6c 75 65 3b 0a 20 20     i64 value;.  
197fe 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d    pMem->flags |=
197ff 20 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20 69 66   MEM_Str;.    if
19800 28 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61  ( sqlite3VdbeCha
19801 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d  ngeEncoding(pMem
19802 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 0a 20  , SQLITE_UTF8). 
19803 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33        || sqlite3
19804 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e  VdbeMemNulTermin
19805 61 74 65 28 70 4d 65 6d 29 20 29 7b 0a 20 20 20  ate(pMem) ){.   
19806 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20     return 0;.   
19807 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70   }.    assert( p
19808 4d 65 6d 2d 3e 7a 20 29 3b 0a 20 20 20 20 73 71  Mem->z );.    sq
19809 6c 69 74 65 33 41 74 6f 69 36 34 28 70 4d 65 6d  lite3Atoi64(pMem
1980a 2d 3e 7a 2c 20 26 76 61 6c 75 65 29 3b 0a 20 20  ->z, &value);.  
1980b 20 20 72 65 74 75 72 6e 20 76 61 6c 75 65 3b 0a    return value;.
1980c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
1980d 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  urn 0;.  }.}../*
1980e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 62  .** Return the b
1980f 65 73 74 20 72 65 70 72 65 73 65 6e 74 61 74 69  est representati
19810 6f 6e 20 6f 66 20 70 4d 65 6d 20 74 68 61 74 20  on of pMem that 
19811 77 65 20 63 61 6e 20 67 65 74 20 69 6e 74 6f 20  we can get into 
19812 61 0a 2a 2a 20 64 6f 75 62 6c 65 2e 20 20 49 66  a.** double.  If
19813 20 70 4d 65 6d 20 69 73 20 61 6c 72 65 61 64 79   pMem is already
19814 20 61 20 64 6f 75 62 6c 65 20 6f 72 20 61 6e 20   a double or an 
19815 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e 20  integer, return 
19816 69 74 73 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 49  its.** value.  I
19817 66 20 69 74 20 69 73 20 61 20 73 74 72 69 6e 67  f it is a string
19818 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f   or blob, try to
19819 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61   convert it to a
1981a 20 64 6f 75 62 6c 65 2e 0a 2a 2a 20 49 66 20 69   double..** If i
1981b 74 20 69 73 20 61 20 4e 55 4c 4c 2c 20 72 65 74  t is a NULL, ret
1981c 75 72 6e 20 30 2e 30 2e 0a 2a 2f 0a 53 51 4c 49  urn 0.0..*/.SQLI
1981d 54 45 5f 50 52 49 56 41 54 45 20 64 6f 75 62 6c  TE_PRIVATE doubl
1981e 65 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61  e sqlite3VdbeRea
1981f 6c 56 61 6c 75 65 28 4d 65 6d 20 2a 70 4d 65 6d  lValue(Mem *pMem
19820 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65  ){.  assert( pMe
19821 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69  m->db==0 || sqli
19822 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
19823 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  Mem->db->mutex) 
19824 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47  );.  assert( EIG
19825 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e  HT_BYTE_ALIGNMEN
19826 54 28 70 4d 65 6d 29 20 29 3b 0a 20 20 69 66 28  T(pMem) );.  if(
19827 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d   pMem->flags & M
19828 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 72  EM_Real ){.    r
19829 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 72 3b 0a 20  eturn pMem->r;. 
1982a 20 7d 65 6c 73 65 20 69 66 28 20 70 4d 65 6d 2d   }else if( pMem-
1982b 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74  >flags & MEM_Int
1982c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28   ){.    return (
1982d 64 6f 75 62 6c 65 29 70 4d 65 6d 2d 3e 75 2e 69  double)pMem->u.i
1982e 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4d  ;.  }else if( pM
1982f 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d  em->flags & (MEM
19830 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29  _Str|MEM_Blob) )
19831 7b 0a 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65  {.    /* (double
19832 29 30 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51  )0 In case of SQ
19833 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
19834 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20  NG_POINT... */. 
19835 20 20 20 64 6f 75 62 6c 65 20 76 61 6c 20 3d 20     double val = 
19836 28 64 6f 75 62 6c 65 29 30 3b 0a 20 20 20 20 70  (double)0;.    p
19837 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45  Mem->flags |= ME
19838 4d 5f 53 74 72 3b 0a 20 20 20 20 69 66 28 20 73  M_Str;.    if( s
19839 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1983a 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 53  Encoding(pMem, S
1983b 51 4c 49 54 45 5f 55 54 46 38 29 0a 20 20 20 20  QLITE_UTF8).    
1983c 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 56 64 62     || sqlite3Vdb
1983d 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65  eMemNulTerminate
1983e 28 70 4d 65 6d 29 20 29 7b 0a 20 20 20 20 20 20  (pMem) ){.      
1983f 2f 2a 20 28 64 6f 75 62 6c 65 29 30 20 49 6e 20  /* (double)0 In 
19840 63 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f  case of SQLITE_O
19841 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49  MIT_FLOATING_POI
19842 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 72  NT... */.      r
19843 65 74 75 72 6e 20 28 64 6f 75 62 6c 65 29 30 3b  eturn (double)0;
19844 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
19845 74 28 20 70 4d 65 6d 2d 3e 7a 20 29 3b 0a 20 20  t( pMem->z );.  
19846 20 20 73 71 6c 69 74 65 33 41 74 6f 46 28 70 4d    sqlite3AtoF(pM
19847 65 6d 2d 3e 7a 2c 20 26 76 61 6c 29 3b 0a 20 20  em->z, &val);.  
19848 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20    return val;.  
19849 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 28 64  }else{.    /* (d
1984a 6f 75 62 6c 65 29 30 20 49 6e 20 63 61 73 65 20  ouble)0 In case 
1984b 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  of SQLITE_OMIT_F
1984c 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e  LOATING_POINT...
1984d 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 28   */.    return (
1984e 64 6f 75 62 6c 65 29 30 3b 0a 20 20 7d 0a 7d 0a  double)0;.  }.}.
1984f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 4d 45 4d 20 73  ./*.** The MEM s
19850 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 72 65  tructure is alre
19851 61 64 79 20 61 20 4d 45 4d 5f 52 65 61 6c 2e 20  ady a MEM_Real. 
19852 20 54 72 79 20 74 6f 20 61 6c 73 6f 20 6d 61 6b   Try to also mak
19853 65 20 69 74 20 61 0a 2a 2a 20 4d 45 4d 5f 49 6e  e it a.** MEM_In
19854 74 20 69 66 20 77 65 20 63 61 6e 2e 0a 2a 2f 0a  t if we can..*/.
19855 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
19856 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 49  oid sqlite3VdbeI
19857 6e 74 65 67 65 72 41 66 66 69 6e 69 74 79 28 4d  ntegerAffinity(M
19858 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73  em *pMem){.  ass
19859 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  ert( pMem->flags
1985a 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 3b 0a 20   & MEM_Real );. 
1985b 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e   assert( (pMem->
1985c 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53  flags & MEM_RowS
1985d 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  et)==0 );.  asse
1985e 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20  rt( pMem->db==0 
1985f 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  || sqlite3_mutex
19860 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e  _held(pMem->db->
19861 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
19862 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41  rt( EIGHT_BYTE_A
19863 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29  LIGNMENT(pMem) )
19864 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d  ;..  pMem->u.i =
19865 20 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28 70   doubleToInt64(p
19866 4d 65 6d 2d 3e 72 29 3b 0a 0a 20 20 2f 2a 20 4f  Mem->r);..  /* O
19867 6e 6c 79 20 6d 61 72 6b 20 74 68 65 20 76 61 6c  nly mark the val
19868 75 65 20 61 73 20 61 6e 20 69 6e 74 65 67 65 72  ue as an integer
19869 20 69 66 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20   if.  **.  **   
1986a 20 28 31 29 20 74 68 65 20 72 6f 75 6e 64 2d 74   (1) the round-t
1986b 72 69 70 20 63 6f 6e 76 65 72 73 69 6f 6e 20 72  rip conversion r
1986c 65 61 6c 2d 3e 69 6e 74 2d 3e 72 65 61 6c 20 69  eal->int->real i
1986d 73 20 61 20 6e 6f 2d 6f 70 2c 20 61 6e 64 0a 20  s a no-op, and. 
1986e 20 2a 2a 20 20 20 20 28 32 29 20 54 68 65 20 69   **    (2) The i
1986f 6e 74 65 67 65 72 20 69 73 20 6e 65 69 74 68 65  nteger is neithe
19870 72 20 74 68 65 20 6c 61 72 67 65 73 74 20 6e 6f  r the largest no
19871 72 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 0a 20  r the smallest. 
19872 20 2a 2a 20 20 20 20 20 20 20 20 70 6f 73 73 69   **        possi
19873 62 6c 65 20 69 6e 74 65 67 65 72 20 28 74 69 63  ble integer (tic
19874 6b 65 74 20 23 33 39 32 32 29 0a 20 20 2a 2a 0a  ket #3922).  **.
19875 20 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20    ** The second 
19876 61 6e 64 20 74 68 69 72 64 20 74 65 72 6d 73 20  and third terms 
19877 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  in the following
19878 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 65 6e 66   conditional enf
19879 6f 72 63 65 73 0a 20 20 2a 2a 20 74 68 65 20 73  orces.  ** the s
1987a 65 63 6f 6e 64 20 63 6f 6e 64 69 74 69 6f 6e 20  econd condition 
1987b 75 6e 64 65 72 20 74 68 65 20 61 73 73 75 6d 70  under the assump
1987c 74 69 6f 6e 20 74 68 61 74 20 61 64 64 69 74 69  tion that additi
1987d 6f 6e 20 6f 76 65 72 66 6c 6f 77 20 63 61 75 73  on overflow caus
1987e 65 73 0a 20 20 2a 2a 20 76 61 6c 75 65 73 20 74  es.  ** values t
1987f 6f 20 77 72 61 70 20 61 72 6f 75 6e 64 2e 20 20  o wrap around.  
19880 4f 6e 20 78 38 36 20 68 61 72 64 77 61 72 65 2c  On x86 hardware,
19881 20 74 68 65 20 74 68 69 72 64 20 74 65 72 6d 20   the third term 
19882 69 73 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 74  is always.  ** t
19883 72 75 65 20 61 6e 64 20 63 6f 75 6c 64 20 62 65  rue and could be
19884 20 6f 6d 69 74 74 65 64 2e 20 20 42 75 74 20 77   omitted.  But w
19885 65 20 6c 65 61 76 65 20 69 74 20 69 6e 20 62 65  e leave it in be
19886 63 61 75 73 65 20 6f 74 68 65 72 0a 20 20 2a 2a  cause other.  **
19887 20 61 72 63 68 69 74 65 63 74 75 72 65 73 20 6d   architectures m
19888 69 67 68 74 20 62 65 68 61 76 65 20 64 69 66 66  ight behave diff
19889 65 72 65 6e 74 6c 79 2e 0a 20 20 2a 2f 0a 20 20  erently..  */.  
1988a 69 66 28 20 70 4d 65 6d 2d 3e 72 3d 3d 28 64 6f  if( pMem->r==(do
1988b 75 62 6c 65 29 70 4d 65 6d 2d 3e 75 2e 69 20 26  uble)pMem->u.i &
1988c 26 20 70 4d 65 6d 2d 3e 75 2e 69 3e 53 4d 41 4c  & pMem->u.i>SMAL
1988d 4c 45 53 54 5f 49 4e 54 36 34 0a 20 20 20 20 20  LEST_INT64.     
1988e 20 26 26 20 41 4c 57 41 59 53 28 70 4d 65 6d 2d   && ALWAYS(pMem-
1988f 3e 75 2e 69 3c 4c 41 52 47 45 53 54 5f 49 4e 54  >u.i<LARGEST_INT
19890 36 34 29 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d  64) ){.    pMem-
19891 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 49 6e  >flags |= MEM_In
19892 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  t;.  }.}../*.** 
19893 43 6f 6e 76 65 72 74 20 70 4d 65 6d 20 74 6f 20  Convert pMem to 
19894 74 79 70 65 20 69 6e 74 65 67 65 72 2e 20 20 49  type integer.  I
19895 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70 72  nvalidate any pr
19896 69 6f 72 20 72 65 70 72 65 73 65 6e 74 61 74 69  ior representati
19897 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ons..*/.SQLITE_P
19898 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
19899 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72  e3VdbeMemInteger
1989a 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a  ify(Mem *pMem){.
1989b 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e    assert( pMem->
1989c 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  db==0 || sqlite3
1989d 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d  _mutex_held(pMem
1989e 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
1989f 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d    assert( (pMem-
198a0 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77  >flags & MEM_Row
198a1 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73  Set)==0 );.  ass
198a2 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f  ert( EIGHT_BYTE_
198a3 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20  ALIGNMENT(pMem) 
198a4 29 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20  );..  pMem->u.i 
198a5 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74  = sqlite3VdbeInt
198a6 56 61 6c 75 65 28 70 4d 65 6d 29 3b 0a 20 20 4d  Value(pMem);.  M
198a7 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4d  emSetTypeFlag(pM
198a8 65 6d 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20  em, MEM_Int);.  
198a9 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
198aa 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  ;.}../*.** Conve
198ab 72 74 20 70 4d 65 6d 20 73 6f 20 74 68 61 74 20  rt pMem so that 
198ac 69 74 20 69 73 20 6f 66 20 74 79 70 65 20 4d 45  it is of type ME
198ad 4d 5f 52 65 61 6c 2e 0a 2a 2a 20 49 6e 76 61 6c  M_Real..** Inval
198ae 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20  idate any prior 
198af 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e  representations.
198b0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
198b1 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
198b2 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 4d 65 6d  beMemRealify(Mem
198b3 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72   *pMem){.  asser
198b4 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c  t( pMem->db==0 |
198b5 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
198b6 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d  held(pMem->db->m
198b7 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
198b8 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c  t( EIGHT_BYTE_AL
198b9 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b  IGNMENT(pMem) );
198ba 0a 0a 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 73 71  ..  pMem->r = sq
198bb 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c  lite3VdbeRealVal
198bc 75 65 28 70 4d 65 6d 29 3b 0a 20 20 4d 65 6d 53  ue(pMem);.  MemS
198bd 65 74 54 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c  etTypeFlag(pMem,
198be 20 4d 45 4d 5f 52 65 61 6c 29 3b 0a 20 20 72 65   MEM_Real);.  re
198bf 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
198c0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74  }../*.** Convert
198c1 20 70 4d 65 6d 20 73 6f 20 74 68 61 74 20 69 74   pMem so that it
198c2 20 68 61 73 20 74 79 70 65 73 20 4d 45 4d 5f 52   has types MEM_R
198c3 65 61 6c 20 6f 72 20 4d 45 4d 5f 49 6e 74 20 6f  eal or MEM_Int o
198c4 72 20 62 6f 74 68 2e 0a 2a 2a 20 49 6e 76 61 6c  r both..** Inval
198c5 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20  idate any prior 
198c6 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e  representations.
198c7 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
198c8 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
198c9 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28 4d 65  beMemNumerify(Me
198ca 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 64 6f 75 62  m *pMem){.  doub
198cb 6c 65 20 72 31 2c 20 72 32 3b 0a 20 20 69 36 34  le r1, r2;.  i64
198cc 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70   i;.  assert( (p
198cd 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45  Mem->flags & (ME
198ce 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d  M_Int|MEM_Real|M
198cf 45 4d 5f 4e 75 6c 6c 29 29 3d 3d 30 20 29 3b 0a  EM_Null))==0 );.
198d0 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d    assert( (pMem-
198d1 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c  >flags & (MEM_Bl
198d2 6f 62 7c 4d 45 4d 5f 53 74 72 29 29 21 3d 30 20  ob|MEM_Str))!=0 
198d3 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65  );.  assert( pMe
198d4 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69  m->db==0 || sqli
198d5 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
198d6 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  Mem->db->mutex) 
198d7 29 3b 0a 20 20 72 31 20 3d 20 73 71 6c 69 74 65  );.  r1 = sqlite
198d8 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70  3VdbeRealValue(p
198d9 4d 65 6d 29 3b 0a 20 20 69 20 3d 20 64 6f 75 62  Mem);.  i = doub
198da 6c 65 54 6f 49 6e 74 36 34 28 72 31 29 3b 0a 20  leToInt64(r1);. 
198db 20 72 32 20 3d 20 28 64 6f 75 62 6c 65 29 69 3b   r2 = (double)i;
198dc 0a 20 20 69 66 28 20 72 31 3d 3d 72 32 20 29 7b  .  if( r1==r2 ){
198dd 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
198de 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 4d  MemIntegerify(pM
198df 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  em);.  }else{.  
198e0 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 72 31 3b 0a    pMem->r = r1;.
198e1 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c      MemSetTypeFl
198e2 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 52 65 61  ag(pMem, MEM_Rea
198e3 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  l);.  }.  return
198e4 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
198e5 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20  *.** Delete any 
198e6 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61  previous value a
198e7 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 65  nd set the value
198e8 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d   stored in *pMem
198e9 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c   to NULL..*/.SQL
198ea 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
198eb 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
198ec 65 74 4e 75 6c 6c 28 4d 65 6d 20 2a 70 4d 65 6d  etNull(Mem *pMem
198ed 29 7b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66  ){.  if( pMem->f
198ee 6c 61 67 73 20 26 20 4d 45 4d 5f 46 72 61 6d 65  lags & MEM_Frame
198ef 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
198f0 64 62 65 46 72 61 6d 65 44 65 6c 65 74 65 28 70  dbeFrameDelete(p
198f1 4d 65 6d 2d 3e 75 2e 70 46 72 61 6d 65 29 3b 0a  Mem->u.pFrame);.
198f2 20 20 7d 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e    }.  if( pMem->
198f3 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53  flags & MEM_RowS
198f4 65 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  et ){.    sqlite
198f5 33 52 6f 77 53 65 74 43 6c 65 61 72 28 70 4d 65  3RowSetClear(pMe
198f6 6d 2d 3e 75 2e 70 52 6f 77 53 65 74 29 3b 0a 20  m->u.pRowSet);. 
198f7 20 7d 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46   }.  MemSetTypeF
198f8 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 4e 75  lag(pMem, MEM_Nu
198f9 6c 6c 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70  ll);.  pMem->typ
198fa 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b  e = SQLITE_NULL;
198fb 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65  .}../*.** Delete
198fc 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61   any previous va
198fd 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68 65 20  lue and set the 
198fe 76 61 6c 75 65 20 74 6f 20 62 65 20 61 20 42 4c  value to be a BL
198ff 4f 42 20 6f 66 20 6c 65 6e 67 74 68 0a 2a 2a 20  OB of length.** 
19900 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6c 6c  n containing all
19901 20 7a 65 72 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 54   zeros..*/.SQLIT
19902 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
19903 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
19904 5a 65 72 6f 42 6c 6f 62 28 4d 65 6d 20 2a 70 4d  ZeroBlob(Mem *pM
19905 65 6d 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 73 71  em, int n){.  sq
19906 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
19907 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65  ase(pMem);.  pMe
19908 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42  m->flags = MEM_B
19909 6c 6f 62 7c 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 20  lob|MEM_Zero;.  
1990a 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
1990b 49 54 45 5f 42 4c 4f 42 3b 0a 20 20 70 4d 65 6d  ITE_BLOB;.  pMem
1990c 2d 3e 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e  ->n = 0;.  if( n
1990d 3c 30 20 29 20 6e 20 3d 20 30 3b 0a 20 20 70 4d  <0 ) n = 0;.  pM
1990e 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 20 3d 20 6e 3b  em->u.nZero = n;
1990f 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53  .  pMem->enc = S
19910 51 4c 49 54 45 5f 55 54 46 38 3b 0a 0a 23 69 66  QLITE_UTF8;..#if
19911 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
19912 49 4e 43 52 42 4c 4f 42 0a 20 20 73 71 6c 69 74  INCRBLOB.  sqlit
19913 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d  e3VdbeMemGrow(pM
19914 65 6d 2c 20 6e 2c 20 30 29 3b 0a 20 20 69 66 28  em, n, 0);.  if(
19915 20 70 4d 65 6d 2d 3e 7a 20 29 7b 0a 20 20 20 20   pMem->z ){.    
19916 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 3b 0a 20 20 20  pMem->n = n;.   
19917 20 6d 65 6d 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c   memset(pMem->z,
19918 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a 23 65 6e 64   0, n);.  }.#end
19919 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65  if.}../*.** Dele
1991a 74 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20  te any previous 
1991b 76 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68  value and set th
1991c 65 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69  e value stored i
1991d 6e 20 2a 70 4d 65 6d 20 74 6f 20 76 61 6c 2c 0a  n *pMem to val,.
1991e 2a 2a 20 6d 61 6e 69 66 65 73 74 20 74 79 70 65  ** manifest type
1991f 20 49 4e 54 45 47 45 52 2e 0a 2a 2f 0a 53 51 4c   INTEGER..*/.SQL
19920 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
19921 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
19922 65 74 49 6e 74 36 34 28 4d 65 6d 20 2a 70 4d 65  etInt64(Mem *pMe
19923 6d 2c 20 69 36 34 20 76 61 6c 29 7b 0a 20 20 73  m, i64 val){.  s
19924 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
19925 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d  ease(pMem);.  pM
19926 65 6d 2d 3e 75 2e 69 20 3d 20 76 61 6c 3b 0a 20  em->u.i = val;. 
19927 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
19928 45 4d 5f 49 6e 74 3b 0a 20 20 70 4d 65 6d 2d 3e  EM_Int;.  pMem->
19929 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e  type = SQLITE_IN
1992a 54 45 47 45 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TEGER;.}../*.** 
1992b 44 65 6c 65 74 65 20 61 6e 79 20 70 72 65 76 69  Delete any previ
1992c 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 20 73 65  ous value and se
1992d 74 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72  t the value stor
1992e 65 64 20 69 6e 20 2a 70 4d 65 6d 20 74 6f 20 76  ed in *pMem to v
1992f 61 6c 2c 0a 2a 2a 20 6d 61 6e 69 66 65 73 74 20  al,.** manifest 
19930 74 79 70 65 20 52 45 41 4c 2e 0a 2a 2f 0a 53 51  type REAL..*/.SQ
19931 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
19932 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  d sqlite3VdbeMem
19933 53 65 74 44 6f 75 62 6c 65 28 4d 65 6d 20 2a 70  SetDouble(Mem *p
19934 4d 65 6d 2c 20 64 6f 75 62 6c 65 20 76 61 6c 29  Mem, double val)
19935 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 49  {.  if( sqlite3I
19936 73 4e 61 4e 28 76 61 6c 29 20 29 7b 0a 20 20 20  sNaN(val) ){.   
19937 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
19938 65 74 4e 75 6c 6c 28 70 4d 65 6d 29 3b 0a 20 20  etNull(pMem);.  
19939 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
1993a 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
1993b 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d  (pMem);.    pMem
1993c 2d 3e 72 20 3d 20 76 61 6c 3b 0a 20 20 20 20 70  ->r = val;.    p
1993d 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
1993e 5f 52 65 61 6c 3b 0a 20 20 20 20 70 4d 65 6d 2d  _Real;.    pMem-
1993f 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 46  >type = SQLITE_F
19940 4c 4f 41 54 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  LOAT;.  }.}../*.
19941 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72  ** Delete any pr
19942 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64  evious value and
19943 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f   set the value o
19944 66 20 70 4d 65 6d 20 74 6f 20 62 65 20 61 6e 0a  f pMem to be an.
19945 2a 2a 20 65 6d 70 74 79 20 62 6f 6f 6c 65 61 6e  ** empty boolean
19946 20 69 6e 64 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54   index..*/.SQLIT
19947 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
19948 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
19949 52 6f 77 53 65 74 28 4d 65 6d 20 2a 70 4d 65 6d  RowSet(Mem *pMem
1994a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
1994b 20 3d 20 70 4d 65 6d 2d 3e 64 62 3b 0a 20 20 61   = pMem->db;.  a
1994c 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a  ssert( db!=0 );.
1994d 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d    assert( (pMem-
1994e 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77  >flags & MEM_Row
1994f 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 73 71 6c  Set)==0 );.  sql
19950 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
19951 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d  se(pMem);.  pMem
19952 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69  ->zMalloc = sqli
19953 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
19954 62 2c 20 36 34 29 3b 0a 20 20 69 66 28 20 64 62  b, 64);.  if( db
19955 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
19956 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67  {.    pMem->flag
19957 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20  s = MEM_Null;.  
19958 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72  }else{.    asser
19959 74 28 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63  t( pMem->zMalloc
1995a 20 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 75 2e   );.    pMem->u.
1995b 70 52 6f 77 53 65 74 20 3d 20 73 71 6c 69 74 65  pRowSet = sqlite
1995c 33 52 6f 77 53 65 74 49 6e 69 74 28 64 62 2c 20  3RowSetInit(db, 
1995d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 2c 20 0a  pMem->zMalloc, .
1995e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1995f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19960 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62         sqlite3Db
19961 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20 70  MallocSize(db, p
19962 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 29 3b 0a  Mem->zMalloc));.
19963 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d      assert( pMem
19964 2d 3e 75 2e 70 52 6f 77 53 65 74 21 3d 30 20 29  ->u.pRowSet!=0 )
19965 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67  ;.    pMem->flag
19966 73 20 3d 20 4d 45 4d 5f 52 6f 77 53 65 74 3b 0a  s = MEM_RowSet;.
19967 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74    }.}../*.** Ret
19968 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20  urn true if the 
19969 4d 65 6d 20 6f 62 6a 65 63 74 20 63 6f 6e 74 61  Mem object conta
1996a 69 6e 73 20 61 20 54 45 58 54 20 6f 72 20 42 4c  ins a TEXT or BL
1996b 4f 42 20 74 68 61 74 20 69 73 0a 2a 2a 20 74 6f  OB that is.** to
1996c 6f 20 6c 61 72 67 65 20 2d 20 77 68 6f 73 65 20  o large - whose 
1996d 73 69 7a 65 20 65 78 63 65 65 64 73 20 53 51 4c  size exceeds SQL
1996e 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 2e 0a  ITE_MAX_LENGTH..
1996f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
19970 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
19971 65 4d 65 6d 54 6f 6f 42 69 67 28 4d 65 6d 20 2a  eMemTooBig(Mem *
19972 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  p){.  assert( p-
19973 3e 64 62 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  >db!=0 );.  if( 
19974 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f  p->flags & (MEM_
19975 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b  Str|MEM_Blob) ){
19976 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 2d 3e  .    int n = p->
19977 6e 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 6c  n;.    if( p->fl
19978 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29  ags & MEM_Zero )
19979 7b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 70 2d 3e  {.      n += p->
1997a 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 7d 0a 20  u.nZero;.    }. 
1997b 20 20 20 72 65 74 75 72 6e 20 6e 3e 70 2d 3e 64     return n>p->d
1997c 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
1997d 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a  _LIMIT_LENGTH];.
1997e 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 20    }.  return 0; 
1997f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 69 7a 65 20 6f  .}../*.** Size o
19980 66 20 73 74 72 75 63 74 20 4d 65 6d 20 6e 6f 74  f struct Mem not
19981 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 4d   including the M
19982 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 6d 65 6d 62 65  em.zMalloc membe
19983 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45  r..*/.#define ME
19984 4d 43 45 4c 4c 53 49 5a 45 20 28 73 69 7a 65 5f  MCELLSIZE (size_
19985 74 29 28 26 28 28 28 4d 65 6d 20 2a 29 30 29 2d  t)(&(((Mem *)0)-
19986 3e 7a 4d 61 6c 6c 6f 63 29 29 0a 0a 2f 2a 0a 2a  >zMalloc))../*.*
19987 2a 20 4d 61 6b 65 20 61 6e 20 73 68 61 6c 6c 6f  * Make an shallo
19988 77 20 63 6f 70 79 20 6f 66 20 70 46 72 6f 6d 20  w copy of pFrom 
19989 69 6e 74 6f 20 70 54 6f 2e 20 20 50 72 69 6f 72  into pTo.  Prior
1998a 20 63 6f 6e 74 65 6e 74 73 20 6f 66 0a 2a 2a 20   contents of.** 
1998b 70 54 6f 20 61 72 65 20 66 72 65 65 64 2e 20 20  pTo are freed.  
1998c 54 68 65 20 70 46 72 6f 6d 2d 3e 7a 20 66 69 65  The pFrom->z fie
1998d 6c 64 20 69 73 20 6e 6f 74 20 64 75 70 6c 69 63  ld is not duplic
1998e 61 74 65 64 2e 20 20 49 66 0a 2a 2a 20 70 46 72  ated.  If.** pFr
1998f 6f 6d 2d 3e 7a 20 69 73 20 75 73 65 64 2c 20 74  om->z is used, t
19990 68 65 6e 20 70 54 6f 2d 3e 7a 20 70 6f 69 6e 74  hen pTo->z point
19991 73 20 74 6f 20 74 68 65 20 73 61 6d 65 20 74 68  s to the same th
19992 69 6e 67 20 61 73 20 70 46 72 6f 6d 2d 3e 7a 0a  ing as pFrom->z.
19993 2a 2a 20 61 6e 64 20 66 6c 61 67 73 20 67 65 74  ** and flags get
19994 73 20 73 72 63 54 79 70 65 20 28 65 69 74 68 65  s srcType (eithe
19995 72 20 4d 45 4d 5f 45 70 68 65 6d 20 6f 72 20 4d  r MEM_Ephem or M
19996 45 4d 5f 53 74 61 74 69 63 29 2e 0a 2a 2f 0a 53  EM_Static)..*/.S
19997 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
19998 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  id sqlite3VdbeMe
19999 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 4d 65 6d  mShallowCopy(Mem
1999a 20 2a 70 54 6f 2c 20 63 6f 6e 73 74 20 4d 65 6d   *pTo, const Mem
1999b 20 2a 70 46 72 6f 6d 2c 20 69 6e 74 20 73 72 63   *pFrom, int src
1999c 54 79 70 65 29 7b 0a 20 20 61 73 73 65 72 74 28  Type){.  assert(
1999d 20 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 20 26   (pFrom->flags &
1999e 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20   MEM_RowSet)==0 
1999f 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
199a0 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e  MemReleaseExtern
199a1 61 6c 28 70 54 6f 29 3b 0a 20 20 6d 65 6d 63 70  al(pTo);.  memcp
199a2 79 28 70 54 6f 2c 20 70 46 72 6f 6d 2c 20 4d 45  y(pTo, pFrom, ME
199a3 4d 43 45 4c 4c 53 49 5a 45 29 3b 0a 20 20 70 54  MCELLSIZE);.  pT
199a4 6f 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 69  o->xDel = 0;.  i
199a5 66 28 20 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73  f( (pFrom->flags
199a6 26 4d 45 4d 5f 44 79 6e 29 21 3d 30 20 7c 7c 20  &MEM_Dyn)!=0 || 
199a7 70 46 72 6f 6d 2d 3e 7a 3d 3d 70 46 72 6f 6d 2d  pFrom->z==pFrom-
199a8 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20  >zMalloc ){.    
199a9 70 54 6f 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28  pTo->flags &= ~(
199aa 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 53 74 61 74  MEM_Dyn|MEM_Stat
199ab 69 63 7c 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20  ic|MEM_Ephem);. 
199ac 20 20 20 61 73 73 65 72 74 28 20 73 72 63 54 79     assert( srcTy
199ad 70 65 3d 3d 4d 45 4d 5f 45 70 68 65 6d 20 7c 7c  pe==MEM_Ephem ||
199ae 20 73 72 63 54 79 70 65 3d 3d 4d 45 4d 5f 53 74   srcType==MEM_St
199af 61 74 69 63 20 29 3b 0a 20 20 20 20 70 54 6f 2d  atic );.    pTo-
199b0 3e 66 6c 61 67 73 20 7c 3d 20 73 72 63 54 79 70  >flags |= srcTyp
199b1 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  e;.  }.}../*.** 
199b2 4d 61 6b 65 20 61 20 66 75 6c 6c 20 63 6f 70 79  Make a full copy
199b3 20 6f 66 20 70 46 72 6f 6d 20 69 6e 74 6f 20 70   of pFrom into p
199b4 54 6f 2e 20 20 50 72 69 6f 72 20 63 6f 6e 74 65  To.  Prior conte
199b5 6e 74 73 20 6f 66 20 70 54 6f 20 61 72 65 0a 2a  nts of pTo are.*
199b6 2a 20 66 72 65 65 64 20 62 65 66 6f 72 65 20 74  * freed before t
199b7 68 65 20 63 6f 70 79 20 69 73 20 6d 61 64 65 2e  he copy is made.
199b8 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
199b9 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
199ba 62 65 4d 65 6d 43 6f 70 79 28 4d 65 6d 20 2a 70  beMemCopy(Mem *p
199bb 54 6f 2c 20 63 6f 6e 73 74 20 4d 65 6d 20 2a 70  To, const Mem *p
199bc 46 72 6f 6d 29 7b 0a 20 20 69 6e 74 20 72 63 20  From){.  int rc 
199bd 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20  = SQLITE_OK;..  
199be 61 73 73 65 72 74 28 20 28 70 46 72 6f 6d 2d 3e  assert( (pFrom->
199bf 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53  flags & MEM_RowS
199c0 65 74 29 3d 3d 30 20 29 3b 0a 20 20 73 71 6c 69  et)==0 );.  sqli
199c1 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
199c2 65 45 78 74 65 72 6e 61 6c 28 70 54 6f 29 3b 0a  eExternal(pTo);.
199c3 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70 46    memcpy(pTo, pF
199c4 72 6f 6d 2c 20 4d 45 4d 43 45 4c 4c 53 49 5a 45  rom, MEMCELLSIZE
199c5 29 3b 0a 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20  );.  pTo->flags 
199c6 26 3d 20 7e 4d 45 4d 5f 44 79 6e 3b 0a 0a 20 20  &= ~MEM_Dyn;..  
199c7 69 66 28 20 70 54 6f 2d 3e 66 6c 61 67 73 26 28  if( pTo->flags&(
199c8 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62  MEM_Str|MEM_Blob
199c9 29 20 29 7b 0a 20 20 20 20 69 66 28 20 30 3d 3d  ) ){.    if( 0==
199ca 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 26 4d 45  (pFrom->flags&ME
199cb 4d 5f 53 74 61 74 69 63 29 20 29 7b 0a 20 20 20  M_Static) ){.   
199cc 20 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20 7c 3d     pTo->flags |=
199cd 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20   MEM_Ephem;.    
199ce 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
199cf 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62  beMemMakeWriteab
199d0 6c 65 28 70 54 6f 29 3b 0a 20 20 20 20 7d 0a 20  le(pTo);.    }. 
199d1 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
199d2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 66  .}../*.** Transf
199d3 65 72 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  er the contents 
199d4 6f 66 20 70 46 72 6f 6d 20 74 6f 20 70 54 6f 2e  of pFrom to pTo.
199d5 20 41 6e 79 20 65 78 69 73 74 69 6e 67 20 76 61   Any existing va
199d6 6c 75 65 20 69 6e 20 70 54 6f 20 69 73 0a 2a 2a  lue in pTo is.**
199d7 20 66 72 65 65 64 2e 20 49 66 20 70 46 72 6f 6d   freed. If pFrom
199d8 20 63 6f 6e 74 61 69 6e 73 20 65 70 68 65 6d 65   contains epheme
199d9 72 61 6c 20 64 61 74 61 2c 20 61 20 63 6f 70 79  ral data, a copy
199da 20 69 73 20 6d 61 64 65 2e 0a 2a 2a 0a 2a 2a 20   is made..**.** 
199db 70 46 72 6f 6d 20 63 6f 6e 74 61 69 6e 73 20 61  pFrom contains a
199dc 6e 20 53 51 4c 20 4e 55 4c 4c 20 77 68 65 6e 20  n SQL NULL when 
199dd 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  this routine ret
199de 75 72 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  urns..*/.SQLITE_
199df 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
199e0 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28  ite3VdbeMemMove(
199e1 4d 65 6d 20 2a 70 54 6f 2c 20 4d 65 6d 20 2a 70  Mem *pTo, Mem *p
199e2 46 72 6f 6d 29 7b 0a 20 20 61 73 73 65 72 74 28  From){.  assert(
199e3 20 70 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c 7c   pFrom->db==0 ||
199e4 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
199e5 65 6c 64 28 70 46 72 6f 6d 2d 3e 64 62 2d 3e 6d  eld(pFrom->db->m
199e6 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
199e7 74 28 20 70 54 6f 2d 3e 64 62 3d 3d 30 20 7c 7c  t( pTo->db==0 ||
199e8 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
199e9 65 6c 64 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74  eld(pTo->db->mut
199ea 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ex) );.  assert(
199eb 20 70 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c 7c   pFrom->db==0 ||
199ec 20 70 54 6f 2d 3e 64 62 3d 3d 30 20 7c 7c 20 70   pTo->db==0 || p
199ed 46 72 6f 6d 2d 3e 64 62 3d 3d 70 54 6f 2d 3e 64  From->db==pTo->d
199ee 62 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 56  b );..  sqlite3V
199ef 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 54  dbeMemRelease(pT
199f0 6f 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 54 6f  o);.  memcpy(pTo
199f1 2c 20 70 46 72 6f 6d 2c 20 73 69 7a 65 6f 66 28  , pFrom, sizeof(
199f2 4d 65 6d 29 29 3b 0a 20 20 70 46 72 6f 6d 2d 3e  Mem));.  pFrom->
199f3 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
199f4 3b 0a 20 20 70 46 72 6f 6d 2d 3e 78 44 65 6c 20  ;.  pFrom->xDel 
199f5 3d 20 30 3b 0a 20 20 70 46 72 6f 6d 2d 3e 7a 4d  = 0;.  pFrom->zM
199f6 61 6c 6c 6f 63 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a  alloc = 0;.}../*
199f7 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76  .** Change the v
199f8 61 6c 75 65 20 6f 66 20 61 20 4d 65 6d 20 74 6f  alue of a Mem to
199f9 20 62 65 20 61 20 73 74 72 69 6e 67 20 6f 72 20   be a string or 
199fa 61 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 54 68  a BLOB..**.** Th
199fb 65 20 6d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 6d  e memory managem
199fc 65 6e 74 20 73 74 72 61 74 65 67 79 20 64 65 70  ent strategy dep
199fd 65 6e 64 73 20 6f 6e 20 74 68 65 20 76 61 6c 75  ends on the valu
199fe 65 20 6f 66 20 74 68 65 20 78 44 65 6c 0a 2a 2a  e of the xDel.**
199ff 20 70 61 72 61 6d 65 74 65 72 2e 20 49 66 20 74   parameter. If t
19a00 68 65 20 76 61 6c 75 65 20 70 61 73 73 65 64 20  he value passed 
19a01 69 73 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  is SQLITE_TRANSI
19a02 45 4e 54 2c 20 74 68 65 6e 20 74 68 65 20 0a 2a  ENT, then the .*
19a03 2a 20 73 74 72 69 6e 67 20 69 73 20 63 6f 70 69  * string is copi
19a04 65 64 20 69 6e 74 6f 20 61 20 28 70 6f 73 73 69  ed into a (possi
19a05 62 6c 79 20 65 78 69 73 74 69 6e 67 29 20 62 75  bly existing) bu
19a06 66 66 65 72 20 6d 61 6e 61 67 65 64 20 62 79 20  ffer managed by 
19a07 74 68 65 20 0a 2a 2a 20 4d 65 6d 20 73 74 72 75  the .** Mem stru
19a08 63 74 75 72 65 2e 20 4f 74 68 65 72 77 69 73 65  cture. Otherwise
19a09 2c 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20 62  , any existing b
19a0a 75 66 66 65 72 20 69 73 20 66 72 65 65 64 20 61  uffer is freed a
19a0b 6e 64 20 74 68 65 0a 2a 2a 20 70 6f 69 6e 74 65  nd the.** pointe
19a0c 72 20 63 6f 70 69 65 64 2e 0a 2a 2a 0a 2a 2a 20  r copied..**.** 
19a0d 49 66 20 74 68 65 20 73 74 72 69 6e 67 20 69 73  If the string is
19a0e 20 74 6f 6f 20 6c 61 72 67 65 20 28 69 66 20 69   too large (if i
19a0f 74 20 65 78 63 65 65 64 73 20 74 68 65 20 53 51  t exceeds the SQ
19a10 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
19a11 48 0a 2a 2a 20 73 69 7a 65 20 6c 69 6d 69 74 29  H.** size limit)
19a12 20 74 68 65 6e 20 6e 6f 20 6d 65 6d 6f 72 79 20   then no memory 
19a13 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 63 63 75 72  allocation occur
19a14 73 2e 20 20 49 66 20 74 68 65 20 73 74 72 69 6e  s.  If the strin
19a15 67 20 63 61 6e 20 62 65 0a 2a 2a 20 73 74 6f 72  g can be.** stor
19a16 65 64 20 77 69 74 68 6f 75 74 20 61 6c 6c 6f 63  ed without alloc
19a17 61 74 69 6e 67 20 6d 65 6d 6f 72 79 2c 20 74 68  ating memory, th
19a18 65 6e 20 69 74 20 69 73 2e 20 20 49 66 20 61 20  en it is.  If a 
19a19 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
19a1a 6e 0a 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64  n.** is required
19a1b 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73 74   to store the st
19a1c 72 69 6e 67 2c 20 74 68 65 6e 20 76 61 6c 75 65  ring, then value
19a1d 20 6f 66 20 70 4d 65 6d 20 69 73 20 75 6e 63 68   of pMem is unch
19a1e 61 6e 67 65 64 2e 20 20 49 6e 0a 2a 2a 20 65 69  anged.  In.** ei
19a1f 74 68 65 72 20 63 61 73 65 2c 20 53 51 4c 49 54  ther case, SQLIT
19a20 45 5f 54 4f 4f 42 49 47 20 69 73 20 72 65 74 75  E_TOOBIG is retu
19a21 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rned..*/.SQLITE_
19a22 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
19a23 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72  te3VdbeMemSetStr
19a24 28 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 20  (.  Mem *pMem,  
19a25 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72          /* Memor
19a26 79 20 63 65 6c 6c 20 74 6f 20 73 65 74 20 74 6f  y cell to set to
19a27 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 2a 2f   string value */
19a28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
19a29 2c 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67  ,      /* String
19a2a 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 69 6e   pointer */.  in
19a2b 74 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20  t n,            
19a2c 20 20 2f 2a 20 42 79 74 65 73 20 69 6e 20 73 74    /* Bytes in st
19a2d 72 69 6e 67 2c 20 6f 72 20 6e 65 67 61 74 69 76  ring, or negativ
19a2e 65 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20  e */.  u8 enc,  
19a2f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e             /* En
19a30 63 6f 64 69 6e 67 20 6f 66 20 7a 2e 20 20 30 20  coding of z.  0 
19a31 66 6f 72 20 42 4c 4f 42 73 20 2a 2f 0a 20 20 76  for BLOBs */.  v
19a32 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64  oid (*xDel)(void
19a33 2a 29 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72  *) /* Destructor
19a34 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a   function */.){.
19a35 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 6e 3b    int nByte = n;
19a36 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 61 6c        /* New val
19a37 75 65 20 66 6f 72 20 70 4d 65 6d 2d 3e 6e 20 2a  ue for pMem->n *
19a38 2f 0a 20 20 69 6e 74 20 69 4c 69 6d 69 74 3b 20  /.  int iLimit; 
19a39 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d          /* Maxim
19a3a 75 6d 20 61 6c 6c 6f 77 65 64 20 73 74 72 69 6e  um allowed strin
19a3b 67 20 6f 72 20 62 6c 6f 62 20 73 69 7a 65 20 2a  g or blob size *
19a3c 2f 0a 20 20 75 31 36 20 66 6c 61 67 73 20 3d 20  /.  u16 flags = 
19a3d 30 3b 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 76  0;      /* New v
19a3e 61 6c 75 65 20 66 6f 72 20 70 4d 65 6d 2d 3e 66  alue for pMem->f
19a3f 6c 61 67 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72  lags */..  asser
19a40 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c  t( pMem->db==0 |
19a41 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
19a42 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d  held(pMem->db->m
19a43 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
19a44 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  t( (pMem->flags 
19a45 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30  & MEM_RowSet)==0
19a46 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 7a 20 69   );..  /* If z i
19a47 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  s a NULL pointer
19a48 2c 20 73 65 74 20 70 4d 65 6d 20 74 6f 20 63 6f  , set pMem to co
19a49 6e 74 61 69 6e 20 61 6e 20 53 51 4c 20 4e 55 4c  ntain an SQL NUL
19a4a 4c 2e 20 2a 2f 0a 20 20 69 66 28 20 21 7a 20 29  L. */.  if( !z )
19a4b 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
19a4c 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4d 65 6d  eMemSetNull(pMem
19a4d 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
19a4e 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20  LITE_OK;.  }..  
19a4f 69 66 28 20 70 4d 65 6d 2d 3e 64 62 20 29 7b 0a  if( pMem->db ){.
19a50 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 70 4d 65      iLimit = pMe
19a51 6d 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  m->db->aLimit[SQ
19a52 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
19a53 48 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  H];.  }else{.   
19a54 20 69 4c 69 6d 69 74 20 3d 20 53 51 4c 49 54 45   iLimit = SQLITE
19a55 5f 4d 41 58 5f 4c 45 4e 47 54 48 3b 0a 20 20 7d  _MAX_LENGTH;.  }
19a56 0a 20 20 66 6c 61 67 73 20 3d 20 28 65 6e 63 3d  .  flags = (enc=
19a57 3d 30 3f 4d 45 4d 5f 42 6c 6f 62 3a 4d 45 4d 5f  =0?MEM_Blob:MEM_
19a58 53 74 72 29 3b 0a 20 20 69 66 28 20 6e 42 79 74  Str);.  if( nByt
19a59 65 3c 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72  e<0 ){.    asser
19a5a 74 28 20 65 6e 63 21 3d 30 20 29 3b 0a 20 20 20  t( enc!=0 );.   
19a5b 20 69 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45   if( enc==SQLITE
19a5c 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 20 20 66  _UTF8 ){.      f
19a5d 6f 72 28 6e 42 79 74 65 3d 30 3b 20 6e 42 79 74  or(nByte=0; nByt
19a5e 65 3c 3d 69 4c 69 6d 69 74 20 26 26 20 7a 5b 6e  e<=iLimit && z[n
19a5f 42 79 74 65 5d 3b 20 6e 42 79 74 65 2b 2b 29 7b  Byte]; nByte++){
19a60 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
19a61 20 20 20 66 6f 72 28 6e 42 79 74 65 3d 30 3b 20     for(nByte=0; 
19a62 6e 42 79 74 65 3c 3d 69 4c 69 6d 69 74 20 26 26  nByte<=iLimit &&
19a63 20 28 7a 5b 6e 42 79 74 65 5d 20 7c 20 7a 5b 6e   (z[nByte] | z[n
19a64 42 79 74 65 2b 31 5d 29 3b 20 6e 42 79 74 65 2b  Byte+1]); nByte+
19a65 3d 32 29 7b 7d 0a 20 20 20 20 7d 0a 20 20 20 20  =2){}.    }.    
19a66 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72  flags |= MEM_Ter
19a67 6d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65  m;.  }..  /* The
19a68 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b   following block
19a69 20 73 65 74 73 20 74 68 65 20 6e 65 77 20 76 61   sets the new va
19a6a 6c 75 65 73 20 6f 66 20 4d 65 6d 2e 7a 20 61 6e  lues of Mem.z an
19a6b 64 20 4d 65 6d 2e 78 44 65 6c 2e 20 49 74 0a 20  d Mem.xDel. It. 
19a6c 20 2a 2a 20 61 6c 73 6f 20 73 65 74 73 20 61 20   ** also sets a 
19a6d 66 6c 61 67 20 69 6e 20 6c 6f 63 61 6c 20 76 61  flag in local va
19a6e 72 69 61 62 6c 65 20 22 66 6c 61 67 73 22 20 74  riable "flags" t
19a6f 6f 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 6d  o indicate the m
19a70 65 6d 6f 72 79 0a 20 20 2a 2a 20 6d 61 6e 61 67  emory.  ** manag
19a71 65 6d 65 6e 74 20 28 6f 6e 65 20 6f 66 20 4d 45  ement (one of ME
19a72 4d 5f 44 79 6e 20 6f 72 20 4d 45 4d 5f 53 74 61  M_Dyn or MEM_Sta
19a73 74 69 63 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  tic)..  */.  if(
19a74 20 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 54 52   xDel==SQLITE_TR
19a75 41 4e 53 49 45 4e 54 20 29 7b 0a 20 20 20 20 69  ANSIENT ){.    i
19a76 6e 74 20 6e 41 6c 6c 6f 63 20 3d 20 6e 42 79 74  nt nAlloc = nByt
19a77 65 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73  e;.    if( flags
19a78 26 4d 45 4d 5f 54 65 72 6d 20 29 7b 0a 20 20 20  &MEM_Term ){.   
19a79 20 20 20 6e 41 6c 6c 6f 63 20 2b 3d 20 28 65 6e     nAlloc += (en
19a7a 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f 31  c==SQLITE_UTF8?1
19a7b 3a 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  :2);.    }.    i
19a7c 66 28 20 6e 42 79 74 65 3e 69 4c 69 6d 69 74 20  f( nByte>iLimit 
19a7d 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
19a7e 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20  SQLITE_TOOBIG;. 
19a7f 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c     }.    if( sql
19a80 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28  ite3VdbeMemGrow(
19a81 70 4d 65 6d 2c 20 6e 41 6c 6c 6f 63 2c 20 30 29  pMem, nAlloc, 0)
19a82 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
19a83 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
19a84 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28     }.    memcpy(
19a85 70 4d 65 6d 2d 3e 7a 2c 20 7a 2c 20 6e 41 6c 6c  pMem->z, z, nAll
19a86 6f 63 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  oc);.  }else if(
19a87 20 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 44 59   xDel==SQLITE_DY
19a88 4e 41 4d 49 43 20 29 7b 0a 20 20 20 20 73 71 6c  NAMIC ){.    sql
19a89 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
19a8a 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d  se(pMem);.    pM
19a8b 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d  em->zMalloc = pM
19a8c 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 20 2a 29  em->z = (char *)
19a8d 7a 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 78 44 65  z;.    pMem->xDe
19a8e 6c 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a  l = 0;.  }else{.
19a8f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
19a90 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b  emRelease(pMem);
19a91 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28  .    pMem->z = (
19a92 63 68 61 72 20 2a 29 7a 3b 0a 20 20 20 20 70 4d  char *)z;.    pM
19a93 65 6d 2d 3e 78 44 65 6c 20 3d 20 78 44 65 6c 3b  em->xDel = xDel;
19a94 0a 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 28 28  .    flags |= ((
19a95 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 53 54 41  xDel==SQLITE_STA
19a96 54 49 43 29 3f 4d 45 4d 5f 53 74 61 74 69 63 3a  TIC)?MEM_Static:
19a97 4d 45 4d 5f 44 79 6e 29 3b 0a 20 20 7d 0a 0a 20  MEM_Dyn);.  }.. 
19a98 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 42 79 74 65   pMem->n = nByte
19a99 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  ;.  pMem->flags 
19a9a 3d 20 66 6c 61 67 73 3b 0a 20 20 70 4d 65 6d 2d  = flags;.  pMem-
19a9b 3e 65 6e 63 20 3d 20 28 65 6e 63 3d 3d 30 20 3f  >enc = (enc==0 ?
19a9c 20 53 51 4c 49 54 45 5f 55 54 46 38 20 3a 20 65   SQLITE_UTF8 : e
19a9d 6e 63 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70  nc);.  pMem->typ
19a9e 65 20 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 53 51  e = (enc==0 ? SQ
19a9f 4c 49 54 45 5f 42 4c 4f 42 20 3a 20 53 51 4c 49  LITE_BLOB : SQLI
19aa0 54 45 5f 54 45 58 54 29 3b 0a 0a 23 69 66 6e 64  TE_TEXT);..#ifnd
19aa1 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  ef SQLITE_OMIT_U
19aa2 54 46 31 36 0a 20 20 69 66 28 20 70 4d 65 6d 2d  TF16.  if( pMem-
19aa3 3e 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46  >enc!=SQLITE_UTF
19aa4 38 20 26 26 20 73 71 6c 69 74 65 33 56 64 62 65  8 && sqlite3Vdbe
19aa5 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 70 4d 65  MemHandleBom(pMe
19aa6 6d 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  m) ){.    return
19aa7 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
19aa8 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28   }.#endif..  if(
19aa9 20 6e 42 79 74 65 3e 69 4c 69 6d 69 74 20 29 7b   nByte>iLimit ){
19aaa 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
19aab 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 7d 0a 0a  TE_TOOBIG;.  }..
19aac 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
19aad 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d  OK;.}../*.** Com
19aae 70 61 72 65 20 74 68 65 20 76 61 6c 75 65 73 20  pare the values 
19aaf 63 6f 6e 74 61 69 6e 65 64 20 62 79 20 74 68 65  contained by the
19ab0 20 74 77 6f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c   two memory cell
19ab1 73 2c 20 72 65 74 75 72 6e 69 6e 67 0a 2a 2a 20  s, returning.** 
19ab2 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 20 6f  negative, zero o
19ab3 72 20 70 6f 73 69 74 69 76 65 20 69 66 20 70 4d  r positive if pM
19ab4 65 6d 31 20 69 73 20 6c 65 73 73 20 74 68 61 6e  em1 is less than
19ab5 2c 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67  , equal to, or g
19ab6 72 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 70  reater.** than p
19ab7 4d 65 6d 32 2e 20 53 6f 72 74 69 6e 67 20 6f 72  Mem2. Sorting or
19ab8 64 65 72 20 69 73 20 4e 55 4c 4c 27 73 20 66 69  der is NULL's fi
19ab9 72 73 74 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79  rst, followed by
19aba 20 6e 75 6d 62 65 72 73 20 28 69 6e 74 65 67 65   numbers (intege
19abb 72 73 0a 2a 2a 20 61 6e 64 20 72 65 61 6c 73 29  rs.** and reals)
19abc 20 73 6f 72 74 65 64 20 6e 75 6d 65 72 69 63 61   sorted numerica
19abd 6c 6c 79 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79  lly, followed by
19abe 20 74 65 78 74 20 6f 72 64 65 72 65 64 20 62 79   text ordered by
19abf 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a   the collating.*
19ac0 2a 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c  * sequence pColl
19ac1 20 61 6e 64 20 66 69 6e 61 6c 6c 79 20 62 6c 6f   and finally blo
19ac2 62 27 73 20 6f 72 64 65 72 65 64 20 62 79 20 6d  b's ordered by m
19ac3 65 6d 63 6d 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 54  emcmp()..**.** T
19ac4 77 6f 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20 61  wo NULL values a
19ac5 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 65 71  re considered eq
19ac6 75 61 6c 20 62 79 20 74 68 69 73 20 66 75 6e 63  ual by this func
19ac7 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
19ac8 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
19ac9 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 63 6f  te3MemCompare(co
19aca 6e 73 74 20 4d 65 6d 20 2a 70 4d 65 6d 31 2c 20  nst Mem *pMem1, 
19acb 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 4d 65 6d 32  const Mem *pMem2
19acc 2c 20 63 6f 6e 73 74 20 43 6f 6c 6c 53 65 71 20  , const CollSeq 
19acd 2a 70 43 6f 6c 6c 29 7b 0a 20 20 69 6e 74 20 72  *pColl){.  int r
19ace 63 3b 0a 20 20 69 6e 74 20 66 31 2c 20 66 32 3b  c;.  int f1, f2;
19acf 0a 20 20 69 6e 74 20 63 6f 6d 62 69 6e 65 64 5f  .  int combined_
19ad0 66 6c 61 67 73 3b 0a 0a 20 20 2f 2a 20 49 6e 74  flags;..  /* Int
19ad1 65 72 63 68 61 6e 67 65 20 70 4d 65 6d 31 20 61  erchange pMem1 a
19ad2 6e 64 20 70 4d 65 6d 32 20 69 66 20 74 68 65 20  nd pMem2 if the 
19ad3 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
19ad4 63 65 20 73 70 65 63 69 66 69 65 73 0a 20 20 2a  ce specifies.  *
19ad5 2a 20 44 45 53 43 20 6f 72 64 65 72 2e 0a 20 20  * DESC order..  
19ad6 2a 2f 0a 20 20 66 31 20 3d 20 70 4d 65 6d 31 2d  */.  f1 = pMem1-
19ad7 3e 66 6c 61 67 73 3b 0a 20 20 66 32 20 3d 20 70  >flags;.  f2 = p
19ad8 4d 65 6d 32 2d 3e 66 6c 61 67 73 3b 0a 20 20 63  Mem2->flags;.  c
19ad9 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 20 3d 20  ombined_flags = 
19ada 66 31 7c 66 32 3b 0a 20 20 61 73 73 65 72 74 28  f1|f2;.  assert(
19adb 20 28 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73   (combined_flags
19adc 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d   & MEM_RowSet)==
19add 30 20 29 3b 0a 20 0a 20 20 2f 2a 20 49 66 20 6f  0 );. .  /* If o
19ade 6e 65 20 76 61 6c 75 65 20 69 73 20 4e 55 4c 4c  ne value is NULL
19adf 2c 20 69 74 20 69 73 20 6c 65 73 73 20 74 68 61  , it is less tha
19ae0 6e 20 74 68 65 20 6f 74 68 65 72 2e 20 49 66 20  n the other. If 
19ae1 62 6f 74 68 20 76 61 6c 75 65 73 0a 20 20 2a 2a  both values.  **
19ae2 20 61 72 65 20 4e 55 4c 4c 2c 20 72 65 74 75 72   are NULL, retur
19ae3 6e 20 30 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  n 0..  */.  if( 
19ae4 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26 4d  combined_flags&M
19ae5 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 72  EM_Null ){.    r
19ae6 65 74 75 72 6e 20 28 66 32 26 4d 45 4d 5f 4e 75  eturn (f2&MEM_Nu
19ae7 6c 6c 29 20 2d 20 28 66 31 26 4d 45 4d 5f 4e 75  ll) - (f1&MEM_Nu
19ae8 6c 6c 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  ll);.  }..  /* I
19ae9 66 20 6f 6e 65 20 76 61 6c 75 65 20 69 73 20 61  f one value is a
19aea 20 6e 75 6d 62 65 72 20 61 6e 64 20 74 68 65 20   number and the 
19aeb 6f 74 68 65 72 20 69 73 20 6e 6f 74 2c 20 74 68  other is not, th
19aec 65 20 6e 75 6d 62 65 72 20 69 73 20 6c 65 73 73  e number is less
19aed 2e 0a 20 20 2a 2a 20 49 66 20 62 6f 74 68 20 61  ..  ** If both a
19aee 72 65 20 6e 75 6d 62 65 72 73 2c 20 63 6f 6d 70  re numbers, comp
19aef 61 72 65 20 61 73 20 72 65 61 6c 73 20 69 66 20  are as reals if 
19af0 6f 6e 65 20 69 73 20 61 20 72 65 61 6c 2c 20 6f  one is a real, o
19af1 72 20 61 73 20 69 6e 74 65 67 65 72 73 0a 20 20  r as integers.  
19af2 2a 2a 20 69 66 20 62 6f 74 68 20 76 61 6c 75 65  ** if both value
19af3 73 20 61 72 65 20 69 6e 74 65 67 65 72 73 2e 0a  s are integers..
19af4 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f 6d 62 69    */.  if( combi
19af5 6e 65 64 5f 66 6c 61 67 73 26 28 4d 45 4d 5f 49  ned_flags&(MEM_I
19af6 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 20 29 7b 0a  nt|MEM_Real) ){.
19af7 20 20 20 20 69 66 28 20 21 28 66 31 26 28 4d 45      if( !(f1&(ME
19af8 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 29  M_Int|MEM_Real))
19af9 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
19afa 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   1;.    }.    if
19afb 28 20 21 28 66 32 26 28 4d 45 4d 5f 49 6e 74 7c  ( !(f2&(MEM_Int|
19afc 4d 45 4d 5f 52 65 61 6c 29 29 20 29 7b 0a 20 20  MEM_Real)) ){.  
19afd 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20      return -1;. 
19afe 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 66 31     }.    if( (f1
19aff 20 26 20 66 32 20 26 20 4d 45 4d 5f 49 6e 74 29   & f2 & MEM_Int)
19b00 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 6f 75  ==0 ){.      dou
19b01 62 6c 65 20 72 31 2c 20 72 32 3b 0a 20 20 20 20  ble r1, r2;.    
19b02 20 20 69 66 28 20 28 66 31 26 4d 45 4d 5f 52 65    if( (f1&MEM_Re
19b03 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  al)==0 ){.      
19b04 20 20 72 31 20 3d 20 28 64 6f 75 62 6c 65 29 70    r1 = (double)p
19b05 4d 65 6d 31 2d 3e 75 2e 69 3b 0a 20 20 20 20 20  Mem1->u.i;.     
19b06 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
19b07 72 31 20 3d 20 70 4d 65 6d 31 2d 3e 72 3b 0a 20  r1 = pMem1->r;. 
19b08 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
19b09 20 28 66 32 26 4d 45 4d 5f 52 65 61 6c 29 3d 3d   (f2&MEM_Real)==
19b0a 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 32 20  0 ){.        r2 
19b0b 3d 20 28 64 6f 75 62 6c 65 29 70 4d 65 6d 32 2d  = (double)pMem2-
19b0c 3e 75 2e 69 3b 0a 20 20 20 20 20 20 7d 65 6c 73  >u.i;.      }els
19b0d 65 7b 0a 20 20 20 20 20 20 20 20 72 32 20 3d 20  e{.        r2 = 
19b0e 70 4d 65 6d 32 2d 3e 72 3b 0a 20 20 20 20 20 20  pMem2->r;.      
19b0f 7d 0a 20 20 20 20 20 20 69 66 28 20 72 31 3c 72  }.      if( r1<r
19b10 32 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20  2 ) return -1;. 
19b11 20 20 20 20 20 69 66 28 20 72 31 3e 72 32 20 29       if( r1>r2 )
19b12 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
19b13 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d   return 0;.    }
19b14 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65  else{.      asse
19b15 72 74 28 20 66 31 26 4d 45 4d 5f 49 6e 74 20 29  rt( f1&MEM_Int )
19b16 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
19b17 66 32 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20  f2&MEM_Int );.  
19b18 20 20 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e 75      if( pMem1->u
19b19 2e 69 20 3c 20 70 4d 65 6d 32 2d 3e 75 2e 69 20  .i < pMem2->u.i 
19b1a 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20  ) return -1;.   
19b1b 20 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e 75 2e     if( pMem1->u.
19b1c 69 20 3e 20 70 4d 65 6d 32 2d 3e 75 2e 69 20 29  i > pMem2->u.i )
19b1d 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
19b1e 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d   return 0;.    }
19b1f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e  .  }..  /* If on
19b20 65 20 76 61 6c 75 65 20 69 73 20 61 20 73 74 72  e value is a str
19b21 69 6e 67 20 61 6e 64 20 74 68 65 20 6f 74 68 65  ing and the othe
19b22 72 20 69 73 20 61 20 62 6c 6f 62 2c 20 74 68 65  r is a blob, the
19b23 20 73 74 72 69 6e 67 20 69 73 20 6c 65 73 73 2e   string is less.
19b24 0a 20 20 2a 2a 20 49 66 20 62 6f 74 68 20 61 72  .  ** If both ar
19b25 65 20 73 74 72 69 6e 67 73 2c 20 63 6f 6d 70 61  e strings, compa
19b26 72 65 20 75 73 69 6e 67 20 74 68 65 20 63 6f 6c  re using the col
19b27 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 73  lating functions
19b28 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f 6d  ..  */.  if( com
19b29 62 69 6e 65 64 5f 66 6c 61 67 73 26 4d 45 4d 5f  bined_flags&MEM_
19b2a 53 74 72 20 29 7b 0a 20 20 20 20 69 66 28 20 28  Str ){.    if( (
19b2b 66 31 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d 30  f1 & MEM_Str)==0
19b2c 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
19b2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   1;.    }.    if
19b2e 28 20 28 66 32 20 26 20 4d 45 4d 5f 53 74 72 29  ( (f2 & MEM_Str)
19b2f 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ==0 ){.      ret
19b30 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 0a 20  urn -1;.    }.. 
19b31 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 31     assert( pMem1
19b32 2d 3e 65 6e 63 3d 3d 70 4d 65 6d 32 2d 3e 65 6e  ->enc==pMem2->en
19b33 63 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  c );.    assert(
19b34 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 53 51 4c   pMem1->enc==SQL
19b35 49 54 45 5f 55 54 46 38 20 7c 7c 20 0a 20 20 20  ITE_UTF8 || .   
19b36 20 20 20 20 20 20 20 20 20 70 4d 65 6d 31 2d 3e           pMem1->
19b37 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31  enc==SQLITE_UTF1
19b38 36 4c 45 20 7c 7c 20 70 4d 65 6d 31 2d 3e 65 6e  6LE || pMem1->en
19b39 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42  c==SQLITE_UTF16B
19b3a 45 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65  E );..    /* The
19b3b 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
19b3c 6e 63 65 20 6d 75 73 74 20 62 65 20 64 65 66 69  nce must be defi
19b3d 6e 65 64 20 61 74 20 74 68 69 73 20 70 6f 69 6e  ned at this poin
19b3e 74 2c 20 65 76 65 6e 20 69 66 0a 20 20 20 20 2a  t, even if.    *
19b3f 2a 20 74 68 65 20 75 73 65 72 20 64 65 6c 65 74  * the user delet
19b40 65 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  es the collation
19b41 20 73 65 71 75 65 6e 63 65 20 61 66 74 65 72 20   sequence after 
19b42 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d  the vdbe program
19b43 20 69 73 0a 20 20 20 20 2a 2a 20 63 6f 6d 70 69   is.    ** compi
19b44 6c 65 64 20 28 74 68 69 73 20 77 61 73 20 6e 6f  led (this was no
19b45 74 20 61 6c 77 61 79 73 20 74 68 65 20 63 61 73  t always the cas
19b46 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61  e)..    */.    a
19b47 73 73 65 72 74 28 20 21 70 43 6f 6c 6c 20 7c 7c  ssert( !pColl ||
19b48 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 20 29 3b 0a   pColl->xCmp );.
19b49 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29  .    if( pColl )
19b4a 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d  {.      if( pMem
19b4b 31 2d 3e 65 6e 63 3d 3d 70 43 6f 6c 6c 2d 3e 65  1->enc==pColl->e
19b4c 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  nc ){.        /*
19b4d 20 54 68 65 20 73 74 72 69 6e 67 73 20 61 72 65   The strings are
19b4e 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20   already in the 
19b4f 63 6f 72 72 65 63 74 20 65 6e 63 6f 64 69 6e 67  correct encoding
19b50 2e 20 20 43 61 6c 6c 20 74 68 65 0a 20 20 20 20  .  Call the.    
19b51 20 20 20 20 2a 2a 20 63 6f 6d 70 61 72 69 73 6f      ** compariso
19b52 6e 20 66 75 6e 63 74 69 6f 6e 20 64 69 72 65 63  n function direc
19b53 74 6c 79 20 2a 2f 0a 20 20 20 20 20 20 20 20 72  tly */.        r
19b54 65 74 75 72 6e 20 70 43 6f 6c 6c 2d 3e 78 43 6d  eturn pColl->xCm
19b55 70 28 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c 70  p(pColl->pUser,p
19b56 4d 65 6d 31 2d 3e 6e 2c 70 4d 65 6d 31 2d 3e 7a  Mem1->n,pMem1->z
19b57 2c 70 4d 65 6d 32 2d 3e 6e 2c 70 4d 65 6d 32 2d  ,pMem2->n,pMem2-
19b58 3e 7a 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  >z);.      }else
19b59 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20  {.        const 
19b5a 76 6f 69 64 20 2a 76 31 2c 20 2a 76 32 3b 0a 20  void *v1, *v2;. 
19b5b 20 20 20 20 20 20 20 69 6e 74 20 6e 31 2c 20 6e         int n1, n
19b5c 32 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d 20 63  2;.        Mem c
19b5d 31 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d 20 63  1;.        Mem c
19b5e 32 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65  2;.        memse
19b5f 74 28 26 63 31 2c 20 30 2c 20 73 69 7a 65 6f 66  t(&c1, 0, sizeof
19b60 28 63 31 29 29 3b 0a 20 20 20 20 20 20 20 20 6d  (c1));.        m
19b61 65 6d 73 65 74 28 26 63 32 2c 20 30 2c 20 73 69  emset(&c2, 0, si
19b62 7a 65 6f 66 28 63 32 29 29 3b 0a 20 20 20 20 20  zeof(c2));.     
19b63 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
19b64 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 26 63 31  mShallowCopy(&c1
19b65 2c 20 70 4d 65 6d 31 2c 20 4d 45 4d 5f 45 70 68  , pMem1, MEM_Eph
19b66 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  em);.        sql
19b67 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c  ite3VdbeMemShall
19b68 6f 77 43 6f 70 79 28 26 63 32 2c 20 70 4d 65 6d  owCopy(&c2, pMem
19b69 32 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20  2, MEM_Ephem);. 
19b6a 20 20 20 20 20 20 20 76 31 20 3d 20 73 71 6c 69         v1 = sqli
19b6b 74 65 33 56 61 6c 75 65 54 65 78 74 28 28 73 71  te3ValueText((sq
19b6c 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 26 63 31  lite3_value*)&c1
19b6d 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 20  , pColl->enc);. 
19b6e 20 20 20 20 20 20 20 6e 31 20 3d 20 76 31 3d 3d         n1 = v1==
19b6f 30 20 3f 20 30 20 3a 20 63 31 2e 6e 3b 0a 20 20  0 ? 0 : c1.n;.  
19b70 20 20 20 20 20 20 76 32 20 3d 20 73 71 6c 69 74        v2 = sqlit
19b71 65 33 56 61 6c 75 65 54 65 78 74 28 28 73 71 6c  e3ValueText((sql
19b72 69 74 65 33 5f 76 61 6c 75 65 2a 29 26 63 32 2c  ite3_value*)&c2,
19b73 20 70 43 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 20 20   pColl->enc);.  
19b74 20 20 20 20 20 20 6e 32 20 3d 20 76 32 3d 3d 30        n2 = v2==0
19b75 20 3f 20 30 20 3a 20 63 32 2e 6e 3b 0a 20 20 20   ? 0 : c2.n;.   
19b76 20 20 20 20 20 72 63 20 3d 20 70 43 6f 6c 6c 2d       rc = pColl-
19b77 3e 78 43 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55 73  >xCmp(pColl->pUs
19b78 65 72 2c 20 6e 31 2c 20 76 31 2c 20 6e 32 2c 20  er, n1, v1, n2, 
19b79 76 32 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  v2);.        sql
19b7a 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
19b7b 73 65 28 26 63 31 29 3b 0a 20 20 20 20 20 20 20  se(&c1);.       
19b7c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
19b7d 65 6c 65 61 73 65 28 26 63 32 29 3b 0a 20 20 20  elease(&c2);.   
19b7e 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
19b7f 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
19b80 20 20 2f 2a 20 49 66 20 61 20 4e 55 4c 4c 20 70    /* If a NULL p
19b81 6f 69 6e 74 65 72 20 77 61 73 20 70 61 73 73 65  ointer was passe
19b82 64 20 61 73 20 74 68 65 20 63 6f 6c 6c 61 74 65  d as the collate
19b83 20 66 75 6e 63 74 69 6f 6e 2c 20 66 61 6c 6c 20   function, fall 
19b84 74 68 72 6f 75 67 68 0a 20 20 20 20 2a 2a 20 74  through.    ** t
19b85 6f 20 74 68 65 20 62 6c 6f 62 20 63 61 73 65 20  o the blob case 
19b86 61 6e 64 20 75 73 65 20 6d 65 6d 63 6d 70 28 29  and use memcmp()
19b87 2e 20 20 2a 2f 0a 20 20 7d 0a 20 0a 20 20 2f 2a  .  */.  }. .  /*
19b88 20 42 6f 74 68 20 76 61 6c 75 65 73 20 6d 75 73   Both values mus
19b89 74 20 62 65 20 62 6c 6f 62 73 2e 20 20 43 6f 6d  t be blobs.  Com
19b8a 70 61 72 65 20 75 73 69 6e 67 20 6d 65 6d 63 6d  pare using memcm
19b8b 70 28 29 2e 20 20 2a 2f 0a 20 20 72 63 20 3d 20  p().  */.  rc = 
19b8c 6d 65 6d 63 6d 70 28 70 4d 65 6d 31 2d 3e 7a 2c  memcmp(pMem1->z,
19b8d 20 70 4d 65 6d 32 2d 3e 7a 2c 20 28 70 4d 65 6d   pMem2->z, (pMem
19b8e 31 2d 3e 6e 3e 70 4d 65 6d 32 2d 3e 6e 29 3f 70  1->n>pMem2->n)?p
19b8f 4d 65 6d 32 2d 3e 6e 3a 70 4d 65 6d 31 2d 3e 6e  Mem2->n:pMem1->n
19b90 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 30 20 29  );.  if( rc==0 )
19b91 7b 0a 20 20 20 20 72 63 20 3d 20 70 4d 65 6d 31  {.    rc = pMem1
19b92 2d 3e 6e 20 2d 20 70 4d 65 6d 32 2d 3e 6e 3b 0a  ->n - pMem2->n;.
19b93 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
19b94 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 64  .}../*.** Move d
19b95 61 74 61 20 6f 75 74 20 6f 66 20 61 20 62 74 72  ata out of a btr
19b96 65 65 20 6b 65 79 20 6f 72 20 64 61 74 61 20 66  ee key or data f
19b97 69 65 6c 64 20 61 6e 64 20 69 6e 74 6f 20 61 20  ield and into a 
19b98 4d 65 6d 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  Mem structure..*
19b99 2a 20 54 68 65 20 64 61 74 61 20 6f 72 20 6b 65  * The data or ke
19b9a 79 20 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20  y is taken from 
19b9b 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 70  the entry that p
19b9c 43 75 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79  Cur is currently
19b9d 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20 74 6f 2e   pointing.** to.
19b9e 20 20 6f 66 66 73 65 74 20 61 6e 64 20 61 6d 74    offset and amt
19b9f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 61 74 20   determine what 
19ba0 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 64  portion of the d
19ba1 61 74 61 20 6f 72 20 6b 65 79 20 74 6f 20 72 65  ata or key to re
19ba2 74 72 69 65 76 65 2e 0a 2a 2a 20 6b 65 79 20 69  trieve..** key i
19ba3 73 20 74 72 75 65 20 74 6f 20 67 65 74 20 74 68  s true to get th
19ba4 65 20 6b 65 79 20 6f 72 20 66 61 6c 73 65 20 74  e key or false t
19ba5 6f 20 67 65 74 20 64 61 74 61 2e 20 20 54 68 65  o get data.  The
19ba6 20 72 65 73 75 6c 74 20 69 73 20 77 72 69 74 74   result is writt
19ba7 65 6e 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 70  en.** into the p
19ba8 4d 65 6d 20 65 6c 65 6d 65 6e 74 2e 0a 2a 2a 0a  Mem element..**.
19ba9 2a 2a 20 54 68 65 20 70 4d 65 6d 20 73 74 72 75  ** The pMem stru
19baa 63 74 75 72 65 20 69 73 20 61 73 73 75 6d 65 64  cture is assumed
19bab 20 74 6f 20 62 65 20 75 6e 69 6e 69 74 69 61 6c   to be uninitial
19bac 69 7a 65 64 2e 20 20 41 6e 79 20 70 72 69 6f 72  ized.  Any prior
19bad 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 69 73 20 6f   content.** is o
19bae 76 65 72 77 72 69 74 74 65 6e 20 77 69 74 68 6f  verwritten witho
19baf 75 74 20 62 65 69 6e 67 20 66 72 65 65 64 2e 0a  ut being freed..
19bb0 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 72 6f  **.** If this ro
19bb1 75 74 69 6e 65 20 66 61 69 6c 73 20 66 6f 72 20  utine fails for 
19bb2 61 6e 79 20 72 65 61 73 6f 6e 20 28 6d 61 6c 6c  any reason (mall
19bb3 6f 63 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20  oc returns NULL 
19bb4 6f 72 20 75 6e 61 62 6c 65 0a 2a 2a 20 74 6f 20  or unable.** to 
19bb5 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 64 69  read from the di
19bb6 73 6b 29 20 74 68 65 6e 20 74 68 65 20 70 4d 65  sk) then the pMe
19bb7 6d 20 69 73 20 6c 65 66 74 20 69 6e 20 61 6e 20  m is left in an 
19bb8 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73 74 61  inconsistent sta
19bb9 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  te..*/.SQLITE_PR
19bba 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
19bbb 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65  3VdbeMemFromBtre
19bbc 65 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  e(.  BtCursor *p
19bbd 43 75 72 2c 20 20 20 2f 2a 20 43 75 72 73 6f 72  Cur,   /* Cursor
19bbe 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 72 65 63   pointing at rec
19bbf 6f 72 64 20 74 6f 20 72 65 74 72 69 65 76 65 2e  ord to retrieve.
19bc0 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 73 65 74   */.  int offset
19bc1 2c 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65  ,       /* Offse
19bc2 74 20 66 72 6f 6d 20 74 68 65 20 73 74 61 72 74  t from the start
19bc3 20 6f 66 20 64 61 74 61 20 74 6f 20 72 65 74 75   of data to retu
19bc4 72 6e 20 62 79 74 65 73 20 66 72 6f 6d 2e 20 2a  rn bytes from. *
19bc5 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 20  /.  int amt,    
19bc6 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
19bc7 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 74 75  of bytes to retu
19bc8 72 6e 2e 20 2a 2f 0a 20 20 69 6e 74 20 6b 65 79  rn. */.  int key
19bc9 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66  ,          /* If
19bca 20 74 72 75 65 2c 20 72 65 74 72 69 65 76 65 20   true, retrieve 
19bcb 66 72 6f 6d 20 74 68 65 20 62 74 72 65 65 20 6b  from the btree k
19bcc 65 79 2c 20 6e 6f 74 20 64 61 74 61 2e 20 2a 2f  ey, not data. */
19bcd 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 20 20 20  .  Mem *pMem    
19bce 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 52 65 74       /* OUT: Ret
19bcf 75 72 6e 20 64 61 74 61 20 69 6e 20 74 68 69 73  urn data in this
19bd0 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65 2e 20   Mem structure. 
19bd1 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 44  */.){.  char *zD
19bd2 61 74 61 3b 20 20 20 20 20 20 20 20 2f 2a 20 44  ata;        /* D
19bd3 61 74 61 20 66 72 6f 6d 20 74 68 65 20 62 74 72  ata from the btr
19bd4 65 65 20 6c 61 79 65 72 20 2a 2f 0a 20 20 69 6e  ee layer */.  in
19bd5 74 20 61 76 61 69 6c 61 62 6c 65 20 3d 20 30 3b  t available = 0;
19bd6 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
19bd7 79 74 65 73 20 61 76 61 69 6c 61 62 6c 65 20 6f  ytes available o
19bd8 6e 20 74 68 65 20 6c 6f 63 61 6c 20 62 74 72 65  n the local btre
19bd9 65 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20  e page */.  int 
19bda 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  rc = SQLITE_OK; 
19bdb 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
19bdc 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  /..  assert( sql
19bdd 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49  ite3BtreeCursorI
19bde 73 56 61 6c 69 64 28 70 43 75 72 29 20 29 3b 0a  sValid(pCur) );.
19bdf 0a 20 20 2f 2a 20 4e 6f 74 65 3a 20 74 68 65 20  .  /* Note: the 
19be0 63 61 6c 6c 73 20 74 6f 20 42 74 72 65 65 4b 65  calls to BtreeKe
19be1 79 46 65 74 63 68 28 29 20 61 6e 64 20 44 61 74  yFetch() and Dat
19be2 61 46 65 74 63 68 28 29 20 62 65 6c 6f 77 20 61  aFetch() below a
19be3 73 73 65 72 74 28 29 20 0a 20 20 2a 2a 20 74 68  ssert() .  ** th
19be4 61 74 20 62 6f 74 68 20 74 68 65 20 42 74 53 68  at both the BtSh
19be5 61 72 65 64 20 61 6e 64 20 64 61 74 61 62 61 73  ared and databas
19be6 65 20 68 61 6e 64 6c 65 20 6d 75 74 65 78 65 73  e handle mutexes
19be7 20 61 72 65 20 68 65 6c 64 2e 20 2a 2f 0a 20 20   are held. */.  
19be8 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66  assert( (pMem->f
19be9 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65  lags & MEM_RowSe
19bea 74 29 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 6b  t)==0 );.  if( k
19beb 65 79 20 29 7b 0a 20 20 20 20 7a 44 61 74 61 20  ey ){.    zData 
19bec 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65  = (char *)sqlite
19bed 33 42 74 72 65 65 4b 65 79 46 65 74 63 68 28 70  3BtreeKeyFetch(p
19bee 43 75 72 2c 20 26 61 76 61 69 6c 61 62 6c 65 29  Cur, &available)
19bef 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a  ;.  }else{.    z
19bf0 44 61 74 61 20 3d 20 28 63 68 61 72 20 2a 29 73  Data = (char *)s
19bf1 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 46  qlite3BtreeDataF
19bf2 65 74 63 68 28 70 43 75 72 2c 20 26 61 76 61 69  etch(pCur, &avai
19bf3 6c 61 62 6c 65 29 3b 0a 20 20 7d 0a 20 20 61 73  lable);.  }.  as
19bf4 73 65 72 74 28 20 7a 44 61 74 61 21 3d 30 20 29  sert( zData!=0 )
19bf5 3b 0a 0a 20 20 69 66 28 20 6f 66 66 73 65 74 2b  ;..  if( offset+
19bf6 61 6d 74 3c 3d 61 76 61 69 6c 61 62 6c 65 20 26  amt<=available &
19bf7 26 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d  & (pMem->flags&M
19bf8 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 7b 0a 20 20  EM_Dyn)==0 ){.  
19bf9 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
19bfa 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20  Release(pMem);. 
19bfb 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 26 7a 44     pMem->z = &zD
19bfc 61 74 61 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 20  ata[offset];.   
19bfd 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
19bfe 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 45 70 68 65  EM_Blob|MEM_Ephe
19bff 6d 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 53  m;.  }else if( S
19c00 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20  QLITE_OK==(rc = 
19c01 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72  sqlite3VdbeMemGr
19c02 6f 77 28 70 4d 65 6d 2c 20 61 6d 74 2b 32 2c 20  ow(pMem, amt+2, 
19c03 30 29 29 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d  0)) ){.    pMem-
19c04 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f  >flags = MEM_Blo
19c05 62 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 54 65  b|MEM_Dyn|MEM_Te
19c06 72 6d 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e  rm;.    pMem->en
19c07 63 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d  c = 0;.    pMem-
19c08 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 42  >type = SQLITE_B
19c09 4c 4f 42 3b 0a 20 20 20 20 69 66 28 20 6b 65 79  LOB;.    if( key
19c0a 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   ){.      rc = s
19c0b 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 28 70  qlite3BtreeKey(p
19c0c 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74  Cur, offset, amt
19c0d 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20  , pMem->z);.    
19c0e 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20  }else{.      rc 
19c0f 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61  = sqlite3BtreeDa
19c10 74 61 28 70 43 75 72 2c 20 6f 66 66 73 65 74 2c  ta(pCur, offset,
19c11 20 61 6d 74 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a   amt, pMem->z);.
19c12 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e      }.    pMem->
19c13 7a 5b 61 6d 74 5d 20 3d 20 30 3b 0a 20 20 20 20  z[amt] = 0;.    
19c14 70 4d 65 6d 2d 3e 7a 5b 61 6d 74 2b 31 5d 20 3d  pMem->z[amt+1] =
19c15 20 30 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d   0;.    if( rc!=
19c16 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
19c17 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
19c18 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a  mRelease(pMem);.
19c19 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 4d 65 6d      }.  }.  pMem
19c1a 2d 3e 6e 20 3d 20 61 6d 74 3b 0a 0a 20 20 72 65  ->n = amt;..  re
19c1b 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54  turn rc;.}../* T
19c1c 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
19c1d 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69  only available i
19c1e 6e 74 65 72 6e 61 6c 6c 79 2c 20 69 74 20 69 73  nternally, it is
19c1f 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65   not part of the
19c20 0a 2a 2a 20 65 78 74 65 72 6e 61 6c 20 41 50 49  .** external API
19c21 2e 20 49 74 20 77 6f 72 6b 73 20 69 6e 20 61 20  . It works in a 
19c22 73 69 6d 69 6c 61 72 20 77 61 79 20 74 6f 20 73  similar way to s
19c23 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
19c24 74 28 29 2c 0a 2a 2a 20 65 78 63 65 70 74 20 74  t(),.** except t
19c25 68 65 20 64 61 74 61 20 72 65 74 75 72 6e 65 64  he data returned
19c26 20 69 73 20 69 6e 20 74 68 65 20 65 6e 63 6f 64   is in the encod
19c27 69 6e 67 20 73 70 65 63 69 66 69 65 64 20 62 79  ing specified by
19c28 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 70   the second.** p
19c29 61 72 61 6d 65 74 65 72 2c 20 77 68 69 63 68 20  arameter, which 
19c2a 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 20 53  must be one of S
19c2b 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20 53  QLITE_UTF16BE, S
19c2c 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72  QLITE_UTF16LE or
19c2d 0a 2a 2a 20 53 51 4c 49 54 45 5f 55 54 46 38 2e  .** SQLITE_UTF8.
19c2e 0a 2a 2a 0a 2a 2a 20 28 32 30 30 36 2d 30 32 2d  .**.** (2006-02-
19c2f 31 36 3a 29 20 20 54 68 65 20 65 6e 63 20 76 61  16:)  The enc va
19c30 6c 75 65 20 63 61 6e 20 62 65 20 6f 72 2d 65 64  lue can be or-ed
19c31 20 77 69 74 68 20 53 51 4c 49 54 45 5f 55 54 46   with SQLITE_UTF
19c32 31 36 5f 41 4c 49 47 4e 45 44 2e 0a 2a 2a 20 49  16_ALIGNED..** I
19c33 66 20 74 68 61 74 20 69 73 20 74 68 65 20 63 61  f that is the ca
19c34 73 65 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73  se, then the res
19c35 75 6c 74 20 6d 75 73 74 20 62 65 20 61 6c 69 67  ult must be alig
19c36 6e 65 64 20 6f 6e 20 61 6e 20 65 76 65 6e 20 62  ned on an even b
19c37 79 74 65 0a 2a 2a 20 62 6f 75 6e 64 61 72 79 2e  yte.** boundary.
19c38 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
19c39 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  TE const void *s
19c3a 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28  qlite3ValueText(
19c3b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20 70  sqlite3_value* p
19c3c 56 61 6c 2c 20 75 38 20 65 6e 63 29 7b 0a 20 20  Val, u8 enc){.  
19c3d 69 66 28 20 21 70 56 61 6c 20 29 20 72 65 74 75  if( !pVal ) retu
19c3e 72 6e 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28  rn 0;..  assert(
19c3f 20 70 56 61 6c 2d 3e 64 62 3d 3d 30 20 7c 7c 20   pVal->db==0 || 
19c40 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
19c41 6c 64 28 70 56 61 6c 2d 3e 64 62 2d 3e 6d 75 74  ld(pVal->db->mut
19c42 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ex) );.  assert(
19c43 20 28 65 6e 63 26 33 29 3d 3d 28 65 6e 63 26 7e   (enc&3)==(enc&~
19c44 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49  SQLITE_UTF16_ALI
19c45 47 4e 45 44 29 20 29 3b 0a 20 20 61 73 73 65 72  GNED) );.  asser
19c46 74 28 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73 20  t( (pVal->flags 
19c47 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30  & MEM_RowSet)==0
19c48 20 29 3b 0a 0a 20 20 69 66 28 20 70 56 61 6c 2d   );..  if( pVal-
19c49 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20  >flags&MEM_Null 
19c4a 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
19c4b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 28  .  }.  assert( (
19c4c 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 29 20 3d 3d 20  MEM_Blob>>3) == 
19c4d 4d 45 4d 5f 53 74 72 20 29 3b 0a 20 20 70 56 61  MEM_Str );.  pVa
19c4e 6c 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 70 56 61  l->flags |= (pVa
19c4f 6c 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42  l->flags & MEM_B
19c50 6c 6f 62 29 3e 3e 33 3b 0a 20 20 65 78 70 61 6e  lob)>>3;.  expan
19c51 64 42 6c 6f 62 28 70 56 61 6c 29 3b 0a 20 20 69  dBlob(pVal);.  i
19c52 66 28 20 70 56 61 6c 2d 3e 66 6c 61 67 73 26 4d  f( pVal->flags&M
19c53 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 73 71  EM_Str ){.    sq
19c54 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45  lite3VdbeChangeE
19c55 6e 63 6f 64 69 6e 67 28 70 56 61 6c 2c 20 65 6e  ncoding(pVal, en
19c56 63 20 26 20 7e 53 51 4c 49 54 45 5f 55 54 46 31  c & ~SQLITE_UTF1
19c57 36 5f 41 4c 49 47 4e 45 44 29 3b 0a 20 20 20 20  6_ALIGNED);.    
19c58 69 66 28 20 28 65 6e 63 20 26 20 53 51 4c 49 54  if( (enc & SQLIT
19c59 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29  E_UTF16_ALIGNED)
19c5a 21 3d 30 20 26 26 20 31 3d 3d 28 31 26 53 51 4c  !=0 && 1==(1&SQL
19c5b 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 70  ITE_PTR_TO_INT(p
19c5c 56 61 6c 2d 3e 7a 29 29 20 29 7b 0a 20 20 20 20  Val->z)) ){.    
19c5d 20 20 61 73 73 65 72 74 28 20 28 70 56 61 6c 2d    assert( (pVal-
19c5e 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 45 70  >flags & (MEM_Ep
19c5f 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 69 63 29 29  hem|MEM_Static))
19c60 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28  !=0 );.      if(
19c61 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d   sqlite3VdbeMemM
19c62 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 56 61  akeWriteable(pVa
19c63 6c 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  l)!=SQLITE_OK ){
19c64 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
19c65 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  0;.      }.    }
19c66 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
19c67 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28  MemNulTerminate(
19c68 70 56 61 6c 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  pVal);.  }else{.
19c69 20 20 20 20 61 73 73 65 72 74 28 20 28 70 56 61      assert( (pVa
19c6a 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 6c 6f  l->flags&MEM_Blo
19c6b 62 29 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c  b)==0 );.    sql
19c6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 6e  ite3VdbeMemStrin
19c6d 67 69 66 79 28 70 56 61 6c 2c 20 65 6e 63 29 3b  gify(pVal, enc);
19c6e 0a 20 20 20 20 61 73 73 65 72 74 28 20 30 3d 3d  .    assert( 0==
19c6f 28 31 26 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f  (1&SQLITE_PTR_TO
19c70 5f 49 4e 54 28 70 56 61 6c 2d 3e 7a 29 29 20 29  _INT(pVal->z)) )
19c71 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 70  ;.  }.  assert(p
19c72 56 61 6c 2d 3e 65 6e 63 3d 3d 28 65 6e 63 20 26  Val->enc==(enc &
19c73 20 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41   ~SQLITE_UTF16_A
19c74 4c 49 47 4e 45 44 29 20 7c 7c 20 70 56 61 6c 2d  LIGNED) || pVal-
19c75 3e 64 62 3d 3d 30 0a 20 20 20 20 20 20 20 20 20  >db==0.         
19c76 20 20 20 20 20 7c 7c 20 70 56 61 6c 2d 3e 64 62       || pVal->db
19c77 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
19c78 3b 0a 20 20 69 66 28 20 70 56 61 6c 2d 3e 65 6e  ;.  if( pVal->en
19c79 63 3d 3d 28 65 6e 63 20 26 20 7e 53 51 4c 49 54  c==(enc & ~SQLIT
19c7a 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29  E_UTF16_ALIGNED)
19c7b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70   ){.    return p
19c7c 56 61 6c 2d 3e 7a 3b 0a 20 20 7d 65 6c 73 65 7b  Val->z;.  }else{
19c7d 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
19c7e 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61   }.}../*.** Crea
19c7f 74 65 20 61 20 6e 65 77 20 73 71 6c 69 74 65 33  te a new sqlite3
19c80 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 2e 0a 2a  _value object..*
19c81 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
19c82 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
19c83 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28  sqlite3ValueNew(
19c84 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
19c85 4d 65 6d 20 2a 70 20 3d 20 73 71 6c 69 74 65 33  Mem *p = sqlite3
19c86 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
19c87 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 20   sizeof(*p));.  
19c88 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e  if( p ){.    p->
19c89 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
19c8a 3b 0a 20 20 20 20 70 2d 3e 74 79 70 65 20 3d 20  ;.    p->type = 
19c8b 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20 20  SQLITE_NULL;.   
19c8c 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 7d   p->db = db;.  }
19c8d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
19c8e 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e  /*.** Create a n
19c8f 65 77 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ew sqlite3_value
19c90 20 6f 62 6a 65 63 74 2c 20 63 6f 6e 74 61 69 6e   object, contain
19c91 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 6f 66  ing the value of
19c92 20 70 45 78 70 72 2e 0a 2a 2a 0a 2a 2a 20 54 68   pExpr..**.** Th
19c93 69 73 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 66 6f  is only works fo
19c94 72 20 76 65 72 79 20 73 69 6d 70 6c 65 20 65 78  r very simple ex
19c95 70 72 65 73 73 69 6f 6e 73 20 74 68 61 74 20 63  pressions that c
19c96 6f 6e 73 69 73 74 20 6f 66 20 6f 6e 65 20 63 6f  onsist of one co
19c97 6e 73 74 61 6e 74 0a 2a 2a 20 74 6f 6b 65 6e 20  nstant.** token 
19c98 28 69 2e 65 2e 20 22 35 22 2c 20 22 35 2e 31 22  (i.e. "5", "5.1"
19c99 2c 20 22 27 61 20 73 74 72 69 6e 67 27 22 29 2e  , "'a string'").
19c9a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69   If the expressi
19c9b 6f 6e 20 63 61 6e 0a 2a 2a 20 62 65 20 63 6f 6e  on can.** be con
19c9c 76 65 72 74 65 64 20 64 69 72 65 63 74 6c 79 20  verted directly 
19c9d 69 6e 74 6f 20 61 20 76 61 6c 75 65 2c 20 74 68  into a value, th
19c9e 65 6e 20 74 68 65 20 76 61 6c 75 65 20 69 73 20  en the value is 
19c9f 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 0a 2a 2a  allocated and.**
19ca0 20 61 20 70 6f 69 6e 74 65 72 20 77 72 69 74 74   a pointer writt
19ca1 65 6e 20 74 6f 20 2a 70 70 56 61 6c 2e 20 54 68  en to *ppVal. Th
19ca2 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65 73 70  e caller is resp
19ca3 6f 6e 73 69 62 6c 65 20 66 6f 72 20 64 65 61 6c  onsible for deal
19ca4 6c 6f 63 61 74 69 6e 67 0a 2a 2a 20 74 68 65 20  locating.** the 
19ca5 76 61 6c 75 65 20 62 79 20 70 61 73 73 69 6e 67  value by passing
19ca6 20 69 74 20 74 6f 20 73 71 6c 69 74 65 33 56 61   it to sqlite3Va
19ca7 6c 75 65 46 72 65 65 28 29 20 6c 61 74 65 72 20  lueFree() later 
19ca8 6f 6e 2e 20 49 66 20 74 68 65 20 65 78 70 72 65  on. If the expre
19ca9 73 73 69 6f 6e 0a 2a 2a 20 63 61 6e 6e 6f 74 20  ssion.** cannot 
19caa 62 65 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 20  be converted to 
19cab 61 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 2a 70  a value, then *p
19cac 70 56 61 6c 20 69 73 20 73 65 74 20 74 6f 20 4e  pVal is set to N
19cad 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ULL..*/.SQLITE_P
19cae 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
19caf 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28  e3ValueFromExpr(
19cb0 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
19cb1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
19cb2 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  The database con
19cb3 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70  nection */.  Exp
19cb4 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20  r *pExpr,       
19cb5 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78         /* The ex
19cb6 70 72 65 73 73 69 6f 6e 20 74 6f 20 65 76 61 6c  pression to eval
19cb7 75 61 74 65 20 2a 2f 0a 20 20 75 38 20 65 6e 63  uate */.  u8 enc
19cb8 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
19cb9 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 20      /* Encoding 
19cba 74 6f 20 75 73 65 20 2a 2f 0a 20 20 75 38 20 61  to use */.  u8 a
19cbb 66 66 69 6e 69 74 79 2c 20 20 20 20 20 20 20 20  ffinity,        
19cbc 20 20 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 74        /* Affinit
19cbd 79 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 73 71  y to use */.  sq
19cbe 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70 70  lite3_value **pp
19cbf 56 61 6c 20 20 20 20 20 2f 2a 20 57 72 69 74 65  Val     /* Write
19cc0 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 20 68   the new value h
19cc1 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ere */.){.  int 
19cc2 6f 70 3b 0a 20 20 63 68 61 72 20 2a 7a 56 61 6c  op;.  char *zVal
19cc3 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f   = 0;.  sqlite3_
19cc4 76 61 6c 75 65 20 2a 70 56 61 6c 20 3d 20 30 3b  value *pVal = 0;
19cc5 0a 0a 20 20 69 66 28 20 21 70 45 78 70 72 20 29  ..  if( !pExpr )
19cc6 7b 0a 20 20 20 20 2a 70 70 56 61 6c 20 3d 20 30  {.    *ppVal = 0
19cc7 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
19cc8 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 6f 70  ITE_OK;.  }.  op
19cc9 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 20   = pExpr->op;.  
19cca 69 66 28 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53  if( op==TK_REGIS
19ccb 54 45 52 20 29 7b 0a 20 20 20 20 6f 70 20 3d 20  TER ){.    op = 
19ccc 70 45 78 70 72 2d 3e 6f 70 32 3b 0a 20 20 7d 0a  pExpr->op2;.  }.
19ccd 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 53 54  .  if( op==TK_ST
19cce 52 49 4e 47 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 46  RING || op==TK_F
19ccf 4c 4f 41 54 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 49  LOAT || op==TK_I
19cd0 4e 54 45 47 45 52 20 29 7b 0a 20 20 20 20 70 56  NTEGER ){.    pV
19cd1 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75  al = sqlite3Valu
19cd2 65 4e 65 77 28 64 62 29 3b 0a 20 20 20 20 69 66  eNew(db);.    if
19cd3 28 20 70 56 61 6c 3d 3d 30 20 29 20 67 6f 74 6f  ( pVal==0 ) goto
19cd4 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 69 66 28   no_mem;.    if(
19cd5 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79   ExprHasProperty
19cd6 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61  (pExpr, EP_IntVa
19cd7 6c 75 65 29 20 29 7b 0a 20 20 20 20 20 20 73 71  lue) ){.      sq
19cd8 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49  lite3VdbeMemSetI
19cd9 6e 74 36 34 28 70 56 61 6c 2c 20 28 69 36 34 29  nt64(pVal, (i64)
19cda 70 45 78 70 72 2d 3e 75 2e 69 56 61 6c 75 65 29  pExpr->u.iValue)
19cdb 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
19cdc 20 20 20 7a 56 61 6c 20 3d 20 73 71 6c 69 74 65     zVal = sqlite
19cdd 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 45  3DbStrDup(db, pE
19cde 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a  xpr->u.zToken);.
19cdf 20 20 20 20 20 20 69 66 28 20 7a 56 61 6c 3d 3d        if( zVal==
19ce0 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b  0 ) goto no_mem;
19ce1 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61  .      sqlite3Va
19ce2 6c 75 65 53 65 74 53 74 72 28 70 56 61 6c 2c 20  lueSetStr(pVal, 
19ce3 2d 31 2c 20 7a 56 61 6c 2c 20 53 51 4c 49 54 45  -1, zVal, SQLITE
19ce4 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 44 59  _UTF8, SQLITE_DY
19ce5 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 69 66  NAMIC);.      if
19ce6 28 20 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29  ( op==TK_FLOAT )
19ce7 20 70 56 61 6c 2d 3e 74 79 70 65 20 3d 20 53 51   pVal->type = SQ
19ce8 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 20 20  LITE_FLOAT;.    
19ce9 7d 0a 20 20 20 20 69 66 28 20 28 6f 70 3d 3d 54  }.    if( (op==T
19cea 4b 5f 49 4e 54 45 47 45 52 20 7c 7c 20 6f 70 3d  K_INTEGER || op=
19ceb 3d 54 4b 5f 46 4c 4f 41 54 20 29 20 26 26 20 61  =TK_FLOAT ) && a
19cec 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f  ffinity==SQLITE_
19ced 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20  AFF_NONE ){.    
19cee 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 41 70    sqlite3ValueAp
19cef 70 6c 79 41 66 66 69 6e 69 74 79 28 70 56 61 6c  plyAffinity(pVal
19cf0 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d  , SQLITE_AFF_NUM
19cf1 45 52 49 43 2c 20 53 51 4c 49 54 45 5f 55 54 46  ERIC, SQLITE_UTF
19cf2 38 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  8);.    }else{. 
19cf3 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75       sqlite3Valu
19cf4 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70  eApplyAffinity(p
19cf5 56 61 6c 2c 20 61 66 66 69 6e 69 74 79 2c 20 53  Val, affinity, S
19cf6 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 20  QLITE_UTF8);.   
19cf7 20 7d 0a 20 20 20 20 69 66 28 20 65 6e 63 21 3d   }.    if( enc!=
19cf8 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20  SQLITE_UTF8 ){. 
19cf9 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
19cfa 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70  ChangeEncoding(p
19cfb 56 61 6c 2c 20 65 6e 63 29 3b 0a 20 20 20 20 7d  Val, enc);.    }
19cfc 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d  .  }else if( op=
19cfd 3d 54 4b 5f 55 4d 49 4e 55 53 20 29 20 7b 0a 20  =TK_UMINUS ) {. 
19cfe 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b     if( SQLITE_OK
19cff 3d 3d 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72  ==sqlite3ValueFr
19d00 6f 6d 45 78 70 72 28 64 62 2c 70 45 78 70 72 2d  omExpr(db,pExpr-
19d01 3e 70 4c 65 66 74 2c 65 6e 63 2c 61 66 66 69 6e  >pLeft,enc,affin
19d02 69 74 79 2c 26 70 56 61 6c 29 20 29 7b 0a 20 20  ity,&pVal) ){.  
19d03 20 20 20 20 70 56 61 6c 2d 3e 75 2e 69 20 3d 20      pVal->u.i = 
19d04 2d 31 20 2a 20 70 56 61 6c 2d 3e 75 2e 69 3b 0a  -1 * pVal->u.i;.
19d05 20 20 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65        /* (double
19d06 29 2d 31 20 49 6e 20 63 61 73 65 20 6f 66 20 53  )-1 In case of S
19d07 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
19d08 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a  ING_POINT... */.
19d09 20 20 20 20 20 20 70 56 61 6c 2d 3e 72 20 3d 20        pVal->r = 
19d0a 28 64 6f 75 62 6c 65 29 2d 31 20 2a 20 70 56 61  (double)-1 * pVa
19d0b 6c 2d 3e 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  l->r;.    }.  }.
19d0c 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
19d0d 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c  MIT_BLOB_LITERAL
19d0e 0a 20 20 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d  .  else if( op==
19d0f 54 4b 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20 69  TK_BLOB ){.    i
19d10 6e 74 20 6e 56 61 6c 3b 0a 20 20 20 20 61 73 73  nt nVal;.    ass
19d11 65 72 74 28 20 70 45 78 70 72 2d 3e 75 2e 7a 54  ert( pExpr->u.zT
19d12 6f 6b 65 6e 5b 30 5d 3d 3d 27 78 27 20 7c 7c 20  oken[0]=='x' || 
19d13 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b  pExpr->u.zToken[
19d14 30 5d 3d 3d 27 58 27 20 29 3b 0a 20 20 20 20 61  0]=='X' );.    a
19d15 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75 2e  ssert( pExpr->u.
19d16 7a 54 6f 6b 65 6e 5b 31 5d 3d 3d 27 5c 27 27 20  zToken[1]=='\'' 
19d17 29 3b 0a 20 20 20 20 70 56 61 6c 20 3d 20 73 71  );.    pVal = sq
19d18 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 64 62  lite3ValueNew(db
19d19 29 3b 0a 20 20 20 20 69 66 28 20 21 70 56 61 6c  );.    if( !pVal
19d1a 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a   ) goto no_mem;.
19d1b 20 20 20 20 7a 56 61 6c 20 3d 20 26 70 45 78 70      zVal = &pExp
19d1c 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 32 5d 3b 0a  r->u.zToken[2];.
19d1d 20 20 20 20 6e 56 61 6c 20 3d 20 73 71 6c 69 74      nVal = sqlit
19d1e 65 33 53 74 72 6c 65 6e 33 30 28 7a 56 61 6c 29  e3Strlen30(zVal)
19d1f 2d 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  -1;.    assert( 
19d20 7a 56 61 6c 5b 6e 56 61 6c 5d 3d 3d 27 5c 27 27  zVal[nVal]=='\''
19d21 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   );.    sqlite3V
19d22 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70 56 61  dbeMemSetStr(pVa
19d23 6c 2c 20 73 71 6c 69 74 65 33 48 65 78 54 6f 42  l, sqlite3HexToB
19d24 6c 6f 62 28 64 62 2c 20 7a 56 61 6c 2c 20 6e 56  lob(db, zVal, nV
19d25 61 6c 29 2c 20 6e 56 61 6c 2f 32 2c 0a 20 20 20  al), nVal/2,.   
19d26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19d27 20 20 20 20 20 20 30 2c 20 53 51 4c 49 54 45 5f        0, SQLITE_
19d28 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 23 65  DYNAMIC);.  }.#e
19d29 6e 64 69 66 0a 0a 20 20 2a 70 70 56 61 6c 20 3d  ndif..  *ppVal =
19d2a 20 70 56 61 6c 3b 0a 20 20 72 65 74 75 72 6e 20   pVal;.  return 
19d2b 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 6e 6f 5f 6d  SQLITE_OK;..no_m
19d2c 65 6d 3a 0a 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63  em:.  db->malloc
19d2d 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 73 71  Failed = 1;.  sq
19d2e 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
19d2f 7a 56 61 6c 29 3b 0a 20 20 73 71 6c 69 74 65 33  zVal);.  sqlite3
19d30 56 61 6c 75 65 46 72 65 65 28 70 56 61 6c 29 3b  ValueFree(pVal);
19d31 0a 20 20 2a 70 70 56 61 6c 20 3d 20 30 3b 0a 20  .  *ppVal = 0;. 
19d32 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
19d33 4f 4d 45 4d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  OMEM;.}../*.** C
19d34 68 61 6e 67 65 20 74 68 65 20 73 74 72 69 6e 67  hange the string
19d35 20 76 61 6c 75 65 20 6f 66 20 61 6e 20 73 71 6c   value of an sql
19d36 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63  ite3_value objec
19d37 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  t.*/.SQLITE_PRIV
19d38 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
19d39 56 61 6c 75 65 53 65 74 53 74 72 28 0a 20 20 73  ValueSetStr(.  s
19d3a 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 76 2c  qlite3_value *v,
19d3b 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f       /* Value to
19d3c 20 62 65 20 73 65 74 20 2a 2f 0a 20 20 69 6e 74   be set */.  int
19d3d 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   n,             
19d3e 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20     /* Length of 
19d3f 73 74 72 69 6e 67 20 7a 20 2a 2f 0a 20 20 63 6f  string z */.  co
19d40 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 20 20 20  nst void *z,    
19d41 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 74      /* Text of t
19d42 68 65 20 6e 65 77 20 73 74 72 69 6e 67 20 2a 2f  he new string */
19d43 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20 20  .  u8 enc,      
19d44 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f           /* Enco
19d45 64 69 6e 67 20 74 6f 20 75 73 65 20 2a 2f 0a 20  ding to use */. 
19d46 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f   void (*xDel)(vo
19d47 69 64 2a 29 20 20 20 2f 2a 20 44 65 73 74 72 75  id*)   /* Destru
19d48 63 74 6f 72 20 66 6f 72 20 74 68 65 20 73 74 72  ctor for the str
19d49 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20  ing */.){.  if( 
19d4a 76 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 4d  v ) sqlite3VdbeM
19d4b 65 6d 53 65 74 53 74 72 28 28 4d 65 6d 20 2a 29  emSetStr((Mem *)
19d4c 76 2c 20 7a 2c 20 6e 2c 20 65 6e 63 2c 20 78 44  v, z, n, enc, xD
19d4d 65 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72  el);.}../*.** Fr
19d4e 65 65 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61  ee an sqlite3_va
19d4f 6c 75 65 20 6f 62 6a 65 63 74 0a 2a 2f 0a 53 51  lue object.*/.SQ
19d50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
19d51 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72  d sqlite3ValueFr
19d52 65 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ee(sqlite3_value
19d53 20 2a 76 29 7b 0a 20 20 69 66 28 20 21 76 20 29   *v){.  if( !v )
19d54 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74   return;.  sqlit
19d55 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
19d56 28 28 4d 65 6d 20 2a 29 76 29 3b 0a 20 20 73 71  ((Mem *)v);.  sq
19d57 6c 69 74 65 33 44 62 46 72 65 65 28 28 28 4d 65  lite3DbFree(((Me
19d58 6d 2a 29 76 29 2d 3e 64 62 2c 20 76 29 3b 0a 7d  m*)v)->db, v);.}
19d59 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
19d5a 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
19d5b 65 73 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65  es in the sqlite
19d5c 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 20 61  3_value object a
19d5d 73 73 75 6d 69 6e 67 0a 2a 2a 20 74 68 61 74 20  ssuming.** that 
19d5e 69 74 20 75 73 65 73 20 74 68 65 20 65 6e 63 6f  it uses the enco
19d5f 64 69 6e 67 20 22 65 6e 63 22 0a 2a 2f 0a 53 51  ding "enc".*/.SQ
19d60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
19d61 20 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79 74   sqlite3ValueByt
19d62 65 73 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  es(sqlite3_value
19d63 20 2a 70 56 61 6c 2c 20 75 38 20 65 6e 63 29 7b   *pVal, u8 enc){
19d64 0a 20 20 4d 65 6d 20 2a 70 20 3d 20 28 4d 65 6d  .  Mem *p = (Mem
19d65 2a 29 70 56 61 6c 3b 0a 20 20 69 66 28 20 28 70  *)pVal;.  if( (p
19d66 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c  ->flags & MEM_Bl
19d67 6f 62 29 21 3d 30 20 7c 7c 20 73 71 6c 69 74 65  ob)!=0 || sqlite
19d68 33 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c  3ValueText(pVal,
19d69 20 65 6e 63 29 20 29 7b 0a 20 20 20 20 69 66 28   enc) ){.    if(
19d6a 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f   p->flags & MEM_
19d6b 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 72 65  Zero ){.      re
19d6c 74 75 72 6e 20 70 2d 3e 6e 20 2b 20 70 2d 3e 75  turn p->n + p->u
19d6d 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 7d 65 6c 73  .nZero;.    }els
19d6e 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  e{.      return 
19d6f 70 2d 3e 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  p->n;.    }.  }.
19d70 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
19d71 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
19d72 6e 64 20 6f 66 20 76 64 62 65 6d 65 6d 2e 63 20  nd of vdbemem.c 
19d73 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19d74 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19d75 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
19d76 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
19d77 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 61 75  egin file vdbeau
19d78 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  x.c ************
19d79 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19d7a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
19d7b 2a 0a 2a 2a 20 32 30 30 33 20 53 65 70 74 65 6d  *.** 2003 Septem
19d7c 62 65 72 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ber 6.**.** The 
19d7d 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
19d7e 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
19d7f 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
19d80 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
19d81 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
19d82 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
19d83 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
19d84 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
19d85 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
19d86 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
19d87 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
19d88 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
19d89 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
19d8a 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
19d8b 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
19d8c 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
19d8d 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
19d8e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19d8f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19d91 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19d92 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
19d93 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20  e contains code 
19d94 75 73 65 64 20 66 6f 72 20 63 72 65 61 74 69 6e  used for creatin
19d95 67 2c 20 64 65 73 74 72 6f 79 69 6e 67 2c 20 61  g, destroying, a
19d96 6e 64 20 70 6f 70 75 6c 61 74 69 6e 67 0a 2a 2a  nd populating.**
19d97 20 61 20 56 44 42 45 20 28 6f 72 20 61 6e 20 22   a VDBE (or an "
19d98 73 71 6c 69 74 65 33 5f 73 74 6d 74 22 20 61 73  sqlite3_stmt" as
19d99 20 69 74 20 69 73 20 6b 6e 6f 77 6e 20 74 6f 20   it is known to 
19d9a 74 68 65 20 6f 75 74 73 69 64 65 20 77 6f 72 6c  the outside worl
19d9b 64 2e 29 20 20 50 72 69 6f 72 0a 2a 2a 20 74 6f  d.)  Prior.** to
19d9c 20 76 65 72 73 69 6f 6e 20 32 2e 38 2e 37 2c 20   version 2.8.7, 
19d9d 61 6c 6c 20 74 68 69 73 20 63 6f 64 65 20 77 61  all this code wa
19d9e 73 20 63 6f 6d 62 69 6e 65 64 20 69 6e 74 6f 20  s combined into 
19d9f 74 68 65 20 76 64 62 65 2e 63 20 73 6f 75 72 63  the vdbe.c sourc
19da0 65 20 66 69 6c 65 2e 0a 2a 2a 20 42 75 74 20 74  e file..** But t
19da1 68 61 74 20 66 69 6c 65 20 77 61 73 20 67 65 74  hat file was get
19da2 74 69 6e 67 20 74 6f 6f 20 62 69 67 20 73 6f 20  ting too big so 
19da3 74 68 69 73 20 73 75 62 72 6f 75 74 69 6e 65 73  this subroutines
19da4 20 77 65 72 65 20 73 70 6c 69 74 20 6f 75 74 2e   were split out.
19da5 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65  .**.** $Id: vdbe
19da6 61 75 78 2e 63 2c 76 20 31 2e 34 38 30 20 32 30  aux.c,v 1.480 20
19da7 30 39 2f 30 38 2f 30 38 20 31 38 3a 30 31 3a 30  09/08/08 18:01:0
19da8 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a  8 drh Exp $.*/..
19da9 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 64 65 62  ../*.** When deb
19daa 75 67 67 69 6e 67 20 74 68 65 20 63 6f 64 65 20  ugging the code 
19dab 67 65 6e 65 72 61 74 6f 72 20 69 6e 20 61 20 73  generator in a s
19dac 79 6d 62 6f 6c 69 63 20 64 65 62 75 67 67 65 72  ymbolic debugger
19dad 2c 20 6f 6e 65 20 63 61 6e 0a 2a 2a 20 73 65 74  , one can.** set
19dae 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 62 65   the sqlite3Vdbe
19daf 41 64 64 6f 70 54 72 61 63 65 20 74 6f 20 31 20  AddopTrace to 1 
19db0 61 6e 64 20 61 6c 6c 20 6f 70 63 6f 64 65 73 20  and all opcodes 
19db1 77 69 6c 6c 20 62 65 20 70 72 69 6e 74 65 64 0a  will be printed.
19db2 2a 2a 20 61 73 20 74 68 65 79 20 61 72 65 20 61  ** as they are a
19db3 64 64 65 64 20 74 6f 20 74 68 65 20 69 6e 73 74  dded to the inst
19db4 72 75 63 74 69 6f 6e 20 73 74 72 65 61 6d 2e 0a  ruction stream..
19db5 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
19db6 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52  _DEBUG.SQLITE_PR
19db7 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
19db8 33 56 64 62 65 41 64 64 6f 70 54 72 61 63 65 20  3VdbeAddopTrace 
19db9 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  = 0;.#endif.../*
19dba 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77  .** Create a new
19dbb 20 76 69 72 74 75 61 6c 20 64 61 74 61 62 61 73   virtual databas
19dbc 65 20 65 6e 67 69 6e 65 2e 0a 2a 2f 0a 53 51 4c  e engine..*/.SQL
19dbd 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 65  ITE_PRIVATE Vdbe
19dbe 20 2a 73 71 6c 69 74 65 33 56 64 62 65 43 72 65   *sqlite3VdbeCre
19dbf 61 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29  ate(sqlite3 *db)
19dc0 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 20 70  {.  Vdbe *p;.  p
19dc1 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
19dc2 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f  ocZero(db, sizeo
19dc3 66 28 56 64 62 65 29 20 29 3b 0a 20 20 69 66 28  f(Vdbe) );.  if(
19dc4 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30   p==0 ) return 0
19dc5 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a  ;.  p->db = db;.
19dc6 20 20 69 66 28 20 64 62 2d 3e 70 56 64 62 65 20    if( db->pVdbe 
19dc7 29 7b 0a 20 20 20 20 64 62 2d 3e 70 56 64 62 65  ){.    db->pVdbe
19dc8 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20 20 7d  ->pPrev = p;.  }
19dc9 0a 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 64 62  .  p->pNext = db
19dca 2d 3e 70 56 64 62 65 3b 0a 20 20 70 2d 3e 70 50  ->pVdbe;.  p->pP
19dcb 72 65 76 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 70  rev = 0;.  db->p
19dcc 56 64 62 65 20 3d 20 70 3b 0a 20 20 70 2d 3e 6d  Vdbe = p;.  p->m
19dcd 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49  agic = VDBE_MAGI
19dce 43 5f 49 4e 49 54 3b 0a 20 20 72 65 74 75 72 6e  C_INIT;.  return
19dcf 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d   p;.}../*.** Rem
19dd0 65 6d 62 65 72 20 74 68 65 20 53 51 4c 20 73 74  ember the SQL st
19dd1 72 69 6e 67 20 66 6f 72 20 61 20 70 72 65 70 61  ring for a prepa
19dd2 72 65 64 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  red statement..*
19dd3 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
19dd4 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
19dd5 65 53 65 74 53 71 6c 28 56 64 62 65 20 2a 70 2c  eSetSql(Vdbe *p,
19dd6 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20   const char *z, 
19dd7 69 6e 74 20 6e 2c 20 69 6e 74 20 69 73 50 72 65  int n, int isPre
19dd8 70 61 72 65 56 32 29 7b 0a 20 20 69 66 28 20 70  pareV2){.  if( p
19dd9 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 23 69  ==0 ) return;.#i
19dda 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
19ddb 5f 54 52 41 43 45 0a 20 20 69 66 28 20 21 69 73  _TRACE.  if( !is
19ddc 50 72 65 70 61 72 65 56 32 20 29 20 72 65 74 75  PrepareV2 ) retu
19ddd 72 6e 3b 0a 23 65 6e 64 69 66 0a 20 20 61 73 73  rn;.#endif.  ass
19dde 65 72 74 28 20 70 2d 3e 7a 53 71 6c 3d 3d 30 20  ert( p->zSql==0 
19ddf 29 3b 0a 20 20 70 2d 3e 7a 53 71 6c 20 3d 20 73  );.  p->zSql = s
19de0 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28  qlite3DbStrNDup(
19de1 70 2d 3e 64 62 2c 20 7a 2c 20 6e 29 3b 0a 20 20  p->db, z, n);.  
19de2 70 2d 3e 69 73 50 72 65 70 61 72 65 56 32 20 3d  p->isPrepareV2 =
19de3 20 69 73 50 72 65 70 61 72 65 56 32 20 3f 20 31   isPrepareV2 ? 1
19de4 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52   : 0;.}../*.** R
19de5 65 74 75 72 6e 20 74 68 65 20 53 51 4c 20 61 73  eturn the SQL as
19de6 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20  sociated with a 
19de7 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
19de8 6e 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  nt.*/.SQLITE_API
19de9 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
19dea 69 74 65 33 5f 73 71 6c 28 73 71 6c 69 74 65 33  ite3_sql(sqlite3
19deb 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20  _stmt *pStmt){. 
19dec 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65   Vdbe *p = (Vdbe
19ded 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72 65 74 75   *)pStmt;.  retu
19dee 72 6e 20 28 70 2d 3e 69 73 50 72 65 70 61 72 65  rn (p->isPrepare
19def 56 32 20 3f 20 70 2d 3e 7a 53 71 6c 20 3a 20 30  V2 ? p->zSql : 0
19df0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 77 61 70  );.}../*.** Swap
19df1 20 61 6c 6c 20 63 6f 6e 74 65 6e 74 20 62 65 74   all content bet
19df2 77 65 65 6e 20 74 77 6f 20 56 44 42 45 20 73 74  ween two VDBE st
19df3 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a 53 51 4c  ructures..*/.SQL
19df4 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
19df5 20 73 71 6c 69 74 65 33 56 64 62 65 53 77 61 70   sqlite3VdbeSwap
19df6 28 56 64 62 65 20 2a 70 41 2c 20 56 64 62 65 20  (Vdbe *pA, Vdbe 
19df7 2a 70 42 29 7b 0a 20 20 56 64 62 65 20 74 6d 70  *pB){.  Vdbe tmp
19df8 2c 20 2a 70 54 6d 70 3b 0a 20 20 63 68 61 72 20  , *pTmp;.  char 
19df9 2a 7a 54 6d 70 3b 0a 20 20 74 6d 70 20 3d 20 2a  *zTmp;.  tmp = *
19dfa 70 41 3b 0a 20 20 2a 70 41 20 3d 20 2a 70 42 3b  pA;.  *pA = *pB;
19dfb 0a 20 20 2a 70 42 20 3d 20 74 6d 70 3b 0a 20 20  .  *pB = tmp;.  
19dfc 70 54 6d 70 20 3d 20 70 41 2d 3e 70 4e 65 78 74  pTmp = pA->pNext
19dfd 3b 0a 20 20 70 41 2d 3e 70 4e 65 78 74 20 3d 20  ;.  pA->pNext = 
19dfe 70 42 2d 3e 70 4e 65 78 74 3b 0a 20 20 70 42 2d  pB->pNext;.  pB-
19dff 3e 70 4e 65 78 74 20 3d 20 70 54 6d 70 3b 0a 20  >pNext = pTmp;. 
19e00 20 70 54 6d 70 20 3d 20 70 41 2d 3e 70 50 72 65   pTmp = pA->pPre
19e01 76 3b 0a 20 20 70 41 2d 3e 70 50 72 65 76 20 3d  v;.  pA->pPrev =
19e02 20 70 42 2d 3e 70 50 72 65 76 3b 0a 20 20 70 42   pB->pPrev;.  pB
19e03 2d 3e 70 50 72 65 76 20 3d 20 70 54 6d 70 3b 0a  ->pPrev = pTmp;.
19e04 20 20 7a 54 6d 70 20 3d 20 70 41 2d 3e 7a 53 71    zTmp = pA->zSq
19e05 6c 3b 0a 20 20 70 41 2d 3e 7a 53 71 6c 20 3d 20  l;.  pA->zSql = 
19e06 70 42 2d 3e 7a 53 71 6c 3b 0a 20 20 70 42 2d 3e  pB->zSql;.  pB->
19e07 7a 53 71 6c 20 3d 20 7a 54 6d 70 3b 0a 20 20 70  zSql = zTmp;.  p
19e08 42 2d 3e 69 73 50 72 65 70 61 72 65 56 32 20 3d  B->isPrepareV2 =
19e09 20 70 41 2d 3e 69 73 50 72 65 70 61 72 65 56 32   pA->isPrepareV2
19e0a 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  ;.}..#ifdef SQLI
19e0b 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54  TE_DEBUG./*.** T
19e0c 75 72 6e 20 74 72 61 63 69 6e 67 20 6f 6e 20 6f  urn tracing on o
19e0d 72 20 6f 66 66 0a 2a 2f 0a 53 51 4c 49 54 45 5f  r off.*/.SQLITE_
19e0e 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
19e0f 69 74 65 33 56 64 62 65 54 72 61 63 65 28 56 64  ite3VdbeTrace(Vd
19e10 62 65 20 2a 70 2c 20 46 49 4c 45 20 2a 74 72 61  be *p, FILE *tra
19e11 63 65 29 7b 0a 20 20 70 2d 3e 74 72 61 63 65 20  ce){.  p->trace 
19e12 3d 20 74 72 61 63 65 3b 0a 7d 0a 23 65 6e 64 69  = trace;.}.#endi
19e13 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 69 7a 65 20  f../*.** Resize 
19e14 74 68 65 20 56 64 62 65 2e 61 4f 70 20 61 72 72  the Vdbe.aOp arr
19e15 61 79 20 73 6f 20 74 68 61 74 20 69 74 20 69 73  ay so that it is
19e16 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 6f 70   at least one op
19e17 20 6c 61 72 67 65 72 20 74 68 61 6e 20 0a 2a 2a   larger than .**
19e18 20 69 74 20 77 61 73 2e 0a 2a 2a 0a 2a 2a 20 49   it was..**.** I
19e19 66 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f  f an out-of-memo
19e1a 72 79 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  ry error occurs 
19e1b 77 68 69 6c 65 20 72 65 73 69 7a 69 6e 67 20 74  while resizing t
19e1c 68 65 20 61 72 72 61 79 2c 20 72 65 74 75 72 6e  he array, return
19e1d 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  .** SQLITE_NOMEM
19e1e 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 56  . In this case V
19e1f 64 62 65 2e 61 4f 70 20 61 6e 64 20 56 64 62 65  dbe.aOp and Vdbe
19e20 2e 6e 4f 70 41 6c 6c 6f 63 20 72 65 6d 61 69 6e  .nOpAlloc remain
19e21 20 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64 20 28   .** unchanged (
19e22 74 68 69 73 20 69 73 20 73 6f 20 74 68 61 74 20  this is so that 
19e23 61 6e 79 20 6f 70 63 6f 64 65 73 20 61 6c 72 65  any opcodes alre
19e24 61 64 79 20 61 6c 6c 6f 63 61 74 65 64 20 63 61  ady allocated ca
19e25 6e 20 62 65 20 0a 2a 2a 20 63 6f 72 72 65 63 74  n be .** correct
19e26 6c 79 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 61  ly deallocated a
19e27 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 72 65  long with the re
19e28 73 74 20 6f 66 20 74 68 65 20 56 64 62 65 29 2e  st of the Vdbe).
19e29 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67  .*/.static int g
19e2a 72 6f 77 4f 70 41 72 72 61 79 28 56 64 62 65 20  rowOpArray(Vdbe 
19e2b 2a 70 29 7b 0a 20 20 56 64 62 65 4f 70 20 2a 70  *p){.  VdbeOp *p
19e2c 4e 65 77 3b 0a 20 20 69 6e 74 20 6e 4e 65 77 20  New;.  int nNew 
19e2d 3d 20 28 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 3f  = (p->nOpAlloc ?
19e2e 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 2a 32 20 3a   p->nOpAlloc*2 :
19e2f 20 28 69 6e 74 29 28 31 30 32 34 2f 73 69 7a 65   (int)(1024/size
19e30 6f 66 28 4f 70 29 29 29 3b 0a 20 20 70 4e 65 77  of(Op)));.  pNew
19e31 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c   = sqlite3DbReal
19e32 6c 6f 63 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4f  loc(p->db, p->aO
19e33 70 2c 20 6e 4e 65 77 2a 73 69 7a 65 6f 66 28 4f  p, nNew*sizeof(O
19e34 70 29 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 20  p));.  if( pNew 
19e35 29 7b 0a 20 20 20 20 70 2d 3e 6e 4f 70 41 6c 6c  ){.    p->nOpAll
19e36 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  oc = sqlite3DbMa
19e37 6c 6c 6f 63 53 69 7a 65 28 70 2d 3e 64 62 2c 20  llocSize(p->db, 
19e38 70 4e 65 77 29 2f 73 69 7a 65 6f 66 28 4f 70 29  pNew)/sizeof(Op)
19e39 3b 0a 20 20 20 20 70 2d 3e 61 4f 70 20 3d 20 70  ;.    p->aOp = p
19e3a 4e 65 77 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  New;.  }.  retur
19e3b 6e 20 28 70 4e 65 77 20 3f 20 53 51 4c 49 54 45  n (pNew ? SQLITE
19e3c 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 4e 4f 4d  _OK : SQLITE_NOM
19e3d 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  EM);.}../*.** Ad
19e3e 64 20 61 20 6e 65 77 20 69 6e 73 74 72 75 63 74  d a new instruct
19e3f 69 6f 6e 20 74 6f 20 74 68 65 20 6c 69 73 74 20  ion to the list 
19e40 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20  of instructions 
19e41 63 75 72 72 65 6e 74 20 69 6e 20 74 68 65 0a 2a  current in the.*
19e42 2a 20 56 44 42 45 2e 20 20 52 65 74 75 72 6e 20  * VDBE.  Return 
19e43 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 74  the address of t
19e44 68 65 20 6e 65 77 20 69 6e 73 74 72 75 63 74 69  he new instructi
19e45 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65  on..**.** Parame
19e46 74 65 72 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 70  ters:.**.**    p
19e47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50                 P
19e48 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 56 44  ointer to the VD
19e49 42 45 0a 2a 2a 0a 2a 2a 20 20 20 20 6f 70 20 20  BE.**.**    op  
19e4a 20 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20              The 
19e4b 6f 70 63 6f 64 65 20 66 6f 72 20 74 68 69 73 20  opcode for this 
19e4c 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 0a 2a  instruction.**.*
19e4d 2a 20 20 20 20 70 31 2c 20 70 32 2c 20 70 33 20  *    p1, p2, p3 
19e4e 20 20 20 20 20 4f 70 65 72 61 6e 64 73 0a 2a 2a       Operands.**
19e4f 0a 2a 2a 20 55 73 65 20 74 68 65 20 73 71 6c 69  .** Use the sqli
19e50 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
19e51 62 65 6c 28 29 20 66 75 6e 63 74 69 6f 6e 20 74  bel() function t
19e52 6f 20 66 69 78 20 61 6e 20 61 64 64 72 65 73 73  o fix an address
19e53 20 61 6e 64 0a 2a 2a 20 74 68 65 20 73 71 6c 69   and.** the sqli
19e54 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
19e55 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 68  ) function to ch
19e56 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f  ange the value o
19e57 66 20 74 68 65 20 50 34 0a 2a 2a 20 6f 70 65 72  f the P4.** oper
19e58 61 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  and..*/.SQLITE_P
19e59 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
19e5a 65 33 56 64 62 65 41 64 64 4f 70 33 28 56 64 62  e3VdbeAddOp3(Vdb
19e5b 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 6e  e *p, int op, in
19e5c 74 20 70 31 2c 20 69 6e 74 20 70 32 2c 20 69 6e  t p1, int p2, in
19e5d 74 20 70 33 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  t p3){.  int i;.
19e5e 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 0a    VdbeOp *pOp;..
19e5f 20 20 69 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20    i = p->nOp;.  
19e60 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63  assert( p->magic
19e61 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49  ==VDBE_MAGIC_INI
19e62 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f  T );.  assert( o
19e63 70 3e 30 20 26 26 20 6f 70 3c 30 78 66 66 20 29  p>0 && op<0xff )
19e64 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 41 6c  ;.  if( p->nOpAl
19e65 6c 6f 63 3c 3d 69 20 29 7b 0a 20 20 20 20 69 66  loc<=i ){.    if
19e66 28 20 67 72 6f 77 4f 70 41 72 72 61 79 28 70 29  ( growOpArray(p)
19e67 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
19e68 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   1;.    }.  }.  
19e69 70 2d 3e 6e 4f 70 2b 2b 3b 0a 20 20 70 4f 70 20  p->nOp++;.  pOp 
19e6a 3d 20 26 70 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20  = &p->aOp[i];.  
19e6b 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 28 75  pOp->opcode = (u
19e6c 38 29 6f 70 3b 0a 20 20 70 4f 70 2d 3e 70 35 20  8)op;.  pOp->p5 
19e6d 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e 70 31 20 3d  = 0;.  pOp->p1 =
19e6e 20 70 31 3b 0a 20 20 70 4f 70 2d 3e 70 32 20 3d   p1;.  pOp->p2 =
19e6f 20 70 32 3b 0a 20 20 70 4f 70 2d 3e 70 33 20 3d   p2;.  pOp->p3 =
19e70 20 70 33 3b 0a 20 20 70 4f 70 2d 3e 70 34 2e 70   p3;.  pOp->p4.p
19e71 20 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e 70 34 74   = 0;.  pOp->p4t
19e72 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44  ype = P4_NOTUSED
19e73 3b 0a 20 20 70 2d 3e 65 78 70 69 72 65 64 20 3d  ;.  p->expired =
19e74 20 30 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54   0;.#ifdef SQLIT
19e75 45 5f 44 45 42 55 47 0a 20 20 70 4f 70 2d 3e 7a  E_DEBUG.  pOp->z
19e76 43 6f 6d 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 69  Comment = 0;.  i
19e77 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  f( sqlite3VdbeAd
19e78 64 6f 70 54 72 61 63 65 20 29 20 73 71 6c 69 74  dopTrace ) sqlit
19e79 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 30 2c  e3VdbePrintOp(0,
19e7a 20 69 2c 20 26 70 2d 3e 61 4f 70 5b 69 5d 29 3b   i, &p->aOp[i]);
19e7b 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 56  .#endif.#ifdef V
19e7c 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 70 4f  DBE_PROFILE.  pO
19e7d 70 2d 3e 63 79 63 6c 65 73 20 3d 20 30 3b 0a 20  p->cycles = 0;. 
19e7e 20 70 4f 70 2d 3e 63 6e 74 20 3d 20 30 3b 0a 23   pOp->cnt = 0;.#
19e7f 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 69  endif.  return i
19e80 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
19e81 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
19e82 62 65 41 64 64 4f 70 30 28 56 64 62 65 20 2a 70  beAddOp0(Vdbe *p
19e83 2c 20 69 6e 74 20 6f 70 29 7b 0a 20 20 72 65 74  , int op){.  ret
19e84 75 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65 41  urn sqlite3VdbeA
19e85 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20 30 2c 20  ddOp3(p, op, 0, 
19e86 30 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  0, 0);.}.SQLITE_
19e87 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
19e88 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 56 64  te3VdbeAddOp1(Vd
19e89 62 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69  be *p, int op, i
19e8a 6e 74 20 70 31 29 7b 0a 20 20 72 65 74 75 72 6e  nt p1){.  return
19e8b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
19e8c 70 33 28 70 2c 20 6f 70 2c 20 70 31 2c 20 30 2c  p3(p, op, p1, 0,
19e8d 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52   0);.}.SQLITE_PR
19e8e 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
19e8f 33 56 64 62 65 41 64 64 4f 70 32 28 56 64 62 65  3VdbeAddOp2(Vdbe
19e90 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74   *p, int op, int
19e91 20 70 31 2c 20 69 6e 74 20 70 32 29 7b 0a 20 20   p1, int p2){.  
19e92 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 64  return sqlite3Vd
19e93 62 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20  beAddOp3(p, op, 
19e94 70 31 2c 20 70 32 2c 20 30 29 3b 0a 7d 0a 0a 0a  p1, p2, 0);.}...
19e95 2f 2a 0a 2a 2a 20 41 64 64 20 61 6e 20 6f 70 63  /*.** Add an opc
19e96 6f 64 65 20 74 68 61 74 20 69 6e 63 6c 75 64 65  ode that include
19e97 73 20 74 68 65 20 70 34 20 76 61 6c 75 65 20 61  s the p4 value a
19e98 73 20 61 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a  s a pointer..*/.
19e99 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
19e9a 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  nt sqlite3VdbeAd
19e9b 64 4f 70 34 28 0a 20 20 56 64 62 65 20 2a 70 2c  dOp4(.  Vdbe *p,
19e9c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
19e9d 64 64 20 74 68 65 20 6f 70 63 6f 64 65 20 74 6f  dd the opcode to
19e9e 20 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20 69 6e   this VM */.  in
19e9f 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20  t op,           
19ea0 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 6f 70 63    /* The new opc
19ea1 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 70 31 2c  ode */.  int p1,
19ea2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
19ea3 54 68 65 20 50 31 20 6f 70 65 72 61 6e 64 20 2a  The P1 operand *
19ea4 2f 0a 20 20 69 6e 74 20 70 32 2c 20 20 20 20 20  /.  int p2,     
19ea5 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50          /* The P
19ea6 32 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69  2 operand */.  i
19ea7 6e 74 20 70 33 2c 20 20 20 20 20 20 20 20 20 20  nt p3,          
19ea8 20 20 20 2f 2a 20 54 68 65 20 50 33 20 6f 70 65     /* The P3 ope
19ea9 72 61 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  rand */.  const 
19eaa 63 68 61 72 20 2a 7a 50 34 2c 20 20 20 20 2f 2a  char *zP4,    /*
19eab 20 54 68 65 20 50 34 20 6f 70 65 72 61 6e 64 20   The P4 operand 
19eac 2a 2f 0a 20 20 69 6e 74 20 70 34 74 79 70 65 20  */.  int p4type 
19ead 20 20 20 20 20 20 20 20 20 2f 2a 20 50 34 20 6f           /* P4 o
19eae 70 65 72 61 6e 64 20 74 79 70 65 20 2a 2f 0a 29  perand type */.)
19eaf 7b 0a 20 20 69 6e 74 20 61 64 64 72 20 3d 20 73  {.  int addr = s
19eb0 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
19eb1 28 70 2c 20 6f 70 2c 20 70 31 2c 20 70 32 2c 20  (p, op, p1, p2, 
19eb2 70 33 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  p3);.  sqlite3Vd
19eb3 62 65 43 68 61 6e 67 65 50 34 28 70 2c 20 61 64  beChangeP4(p, ad
19eb4 64 72 2c 20 7a 50 34 2c 20 70 34 74 79 70 65 29  dr, zP4, p4type)
19eb5 3b 0a 20 20 72 65 74 75 72 6e 20 61 64 64 72 3b  ;.  return addr;
19eb6 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65  .}../*.** Create
19eb7 20 61 20 6e 65 77 20 73 79 6d 62 6f 6c 69 63 20   a new symbolic 
19eb8 6c 61 62 65 6c 20 66 6f 72 20 61 6e 20 69 6e 73  label for an ins
19eb9 74 72 75 63 74 69 6f 6e 20 74 68 61 74 20 68 61  truction that ha
19eba 73 20 79 65 74 20 74 6f 20 62 65 0a 2a 2a 20 63  s yet to be.** c
19ebb 6f 64 65 64 2e 20 20 54 68 65 20 73 79 6d 62 6f  oded.  The symbo
19ebc 6c 69 63 20 6c 61 62 65 6c 20 69 73 20 72 65 61  lic label is rea
19ebd 6c 6c 79 20 6a 75 73 74 20 61 20 6e 65 67 61 74  lly just a negat
19ebe 69 76 65 20 6e 75 6d 62 65 72 2e 20 20 54 68 65  ive number.  The
19ebf 0a 2a 2a 20 6c 61 62 65 6c 20 63 61 6e 20 62 65  .** label can be
19ec0 20 75 73 65 64 20 61 73 20 74 68 65 20 50 32 20   used as the P2 
19ec1 76 61 6c 75 65 20 6f 66 20 61 6e 20 6f 70 65 72  value of an oper
19ec2 61 74 69 6f 6e 2e 20 20 4c 61 74 65 72 2c 20 77  ation.  Later, w
19ec3 68 65 6e 0a 2a 2a 20 74 68 65 20 6c 61 62 65 6c  hen.** the label
19ec4 20 69 73 20 72 65 73 6f 6c 76 65 64 20 74 6f 20   is resolved to 
19ec5 61 20 73 70 65 63 69 66 69 63 20 61 64 64 72 65  a specific addre
19ec6 73 73 2c 20 74 68 65 20 56 44 42 45 20 77 69 6c  ss, the VDBE wil
19ec7 6c 20 73 63 61 6e 0a 2a 2a 20 74 68 72 6f 75 67  l scan.** throug
19ec8 68 20 69 74 73 20 6f 70 65 72 61 74 69 6f 6e 20  h its operation 
19ec9 6c 69 73 74 20 61 6e 64 20 63 68 61 6e 67 65 20  list and change 
19eca 61 6c 6c 20 76 61 6c 75 65 73 20 6f 66 20 50 32  all values of P2
19ecb 20 77 68 69 63 68 20 6d 61 74 63 68 0a 2a 2a 20   which match.** 
19ecc 74 68 65 20 6c 61 62 65 6c 20 69 6e 74 6f 20 74  the label into t
19ecd 68 65 20 72 65 73 6f 6c 76 65 64 20 61 64 64 72  he resolved addr
19ece 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56  ess..**.** The V
19ecf 44 42 45 20 6b 6e 6f 77 73 20 74 68 61 74 20 61  DBE knows that a
19ed0 20 50 32 20 76 61 6c 75 65 20 69 73 20 61 20 6c   P2 value is a l
19ed1 61 62 65 6c 20 62 65 63 61 75 73 65 20 6c 61 62  abel because lab
19ed2 65 6c 73 20 61 72 65 0a 2a 2a 20 61 6c 77 61 79  els are.** alway
19ed3 73 20 6e 65 67 61 74 69 76 65 20 61 6e 64 20 50  s negative and P
19ed4 32 20 76 61 6c 75 65 73 20 61 72 65 20 73 75 70  2 values are sup
19ed5 70 6f 73 65 20 74 6f 20 62 65 20 6e 6f 6e 2d 6e  pose to be non-n
19ed6 65 67 61 74 69 76 65 2e 0a 2a 2a 20 48 65 6e 63  egative..** Henc
19ed7 65 2c 20 61 20 6e 65 67 61 74 69 76 65 20 50 32  e, a negative P2
19ed8 20 76 61 6c 75 65 20 69 73 20 61 20 6c 61 62 65   value is a labe
19ed9 6c 20 74 68 61 74 20 68 61 73 20 79 65 74 20 74  l that has yet t
19eda 6f 20 62 65 20 72 65 73 6f 6c 76 65 64 2e 0a 2a  o be resolved..*
19edb 2a 0a 2a 2a 20 5a 65 72 6f 20 69 73 20 72 65 74  *.** Zero is ret
19edc 75 72 6e 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f  urned if a mallo
19edd 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51  c() fails..*/.SQ
19ede 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
19edf 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
19ee0 4c 61 62 65 6c 28 56 64 62 65 20 2a 70 29 7b 0a  Label(Vdbe *p){.
19ee1 20 20 69 6e 74 20 69 3b 0a 20 20 69 20 3d 20 70    int i;.  i = p
19ee2 2d 3e 6e 4c 61 62 65 6c 2b 2b 3b 0a 20 20 61 73  ->nLabel++;.  as
19ee3 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d  sert( p->magic==
19ee4 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20  VDBE_MAGIC_INIT 
19ee5 29 3b 0a 20 20 69 66 28 20 69 3e 3d 70 2d 3e 6e  );.  if( i>=p->n
19ee6 4c 61 62 65 6c 41 6c 6c 6f 63 20 29 7b 0a 20 20  LabelAlloc ){.  
19ee7 20 20 69 6e 74 20 6e 20 3d 20 70 2d 3e 6e 4c 61    int n = p->nLa
19ee8 62 65 6c 41 6c 6c 6f 63 2a 32 20 2b 20 35 3b 0a  belAlloc*2 + 5;.
19ee9 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c 20 3d 20      p->aLabel = 
19eea 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63  sqlite3DbRealloc
19eeb 4f 72 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d  OrFree(p->db, p-
19eec 3e 61 4c 61 62 65 6c 2c 0a 20 20 20 20 20 20 20  >aLabel,.       
19eed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19eee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19eef 6e 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 4c 61 62  n*sizeof(p->aLab
19ef0 65 6c 5b 30 5d 29 29 3b 0a 20 20 20 20 70 2d 3e  el[0]));.    p->
19ef1 6e 4c 61 62 65 6c 41 6c 6c 6f 63 20 3d 20 73 71  nLabelAlloc = sq
19ef2 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a  lite3DbMallocSiz
19ef3 65 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c 61 62  e(p->db, p->aLab
19ef4 65 6c 29 2f 73 69 7a 65 6f 66 28 70 2d 3e 61 4c  el)/sizeof(p->aL
19ef5 61 62 65 6c 5b 30 5d 29 3b 0a 20 20 7d 0a 20 20  abel[0]);.  }.  
19ef6 69 66 28 20 70 2d 3e 61 4c 61 62 65 6c 20 29 7b  if( p->aLabel ){
19ef7 0a 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c 5b 69  .    p->aLabel[i
19ef8 5d 20 3d 20 2d 31 3b 0a 20 20 7d 0a 20 20 72 65  ] = -1;.  }.  re
19ef9 74 75 72 6e 20 2d 31 2d 69 3b 0a 7d 0a 0a 2f 2a  turn -1-i;.}../*
19efa 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 6c 61 62 65  .** Resolve labe
19efb 6c 20 22 78 22 20 74 6f 20 62 65 20 74 68 65 20  l "x" to be the 
19efc 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 6e  address of the n
19efd 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ext instruction 
19efe 74 6f 0a 2a 2a 20 62 65 20 69 6e 73 65 72 74 65  to.** be inserte
19eff 64 2e 20 20 54 68 65 20 70 61 72 61 6d 65 74 65  d.  The paramete
19f00 72 20 22 78 22 20 6d 75 73 74 20 68 61 76 65 20  r "x" must have 
19f01 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 66 72  been obtained fr
19f02 6f 6d 0a 2a 2a 20 61 20 70 72 69 6f 72 20 63 61  om.** a prior ca
19f03 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62  ll to sqlite3Vdb
19f04 65 4d 61 6b 65 4c 61 62 65 6c 28 29 2e 0a 2a 2f  eMakeLabel()..*/
19f05 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
19f06 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
19f07 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 56 64 62  ResolveLabel(Vdb
19f08 65 20 2a 70 2c 20 69 6e 74 20 78 29 7b 0a 20 20  e *p, int x){.  
19f09 69 6e 74 20 6a 20 3d 20 2d 31 2d 78 3b 0a 20 20  int j = -1-x;.  
19f0a 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63  assert( p->magic
19f0b 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49  ==VDBE_MAGIC_INI
19f0c 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6a  T );.  assert( j
19f0d 3e 3d 30 20 26 26 20 6a 3c 70 2d 3e 6e 4c 61 62  >=0 && j<p->nLab
19f0e 65 6c 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61  el );.  if( p->a
19f0f 4c 61 62 65 6c 20 29 7b 0a 20 20 20 20 70 2d 3e  Label ){.    p->
19f10 61 4c 61 62 65 6c 5b 6a 5d 20 3d 20 70 2d 3e 6e  aLabel[j] = p->n
19f11 4f 70 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 64 65  Op;.  }.}..#ifde
19f12 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 0a  f SQLITE_DEBUG..
19f13 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
19f14 69 6e 67 20 74 79 70 65 20 61 6e 64 20 66 75 6e  ing type and fun
19f15 63 74 69 6f 6e 20 61 72 65 20 75 73 65 64 20 74  ction are used t
19f16 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67  o iterate throug
19f17 68 20 61 6c 6c 20 6f 70 63 6f 64 65 73 0a 2a 2a  h all opcodes.**
19f18 20 69 6e 20 61 20 56 64 62 65 20 6d 61 69 6e 20   in a Vdbe main 
19f19 70 72 6f 67 72 61 6d 20 61 6e 64 20 65 61 63 68  program and each
19f1a 20 6f 66 20 74 68 65 20 73 75 62 2d 70 72 6f 67   of the sub-prog
19f1b 72 61 6d 73 20 28 74 72 69 67 67 65 72 73 29 20  rams (triggers) 
19f1c 69 74 20 6d 61 79 20 0a 2a 2a 20 69 6e 76 6f 6b  it may .** invok
19f1d 65 20 64 69 72 65 63 74 6c 79 20 6f 72 20 69 6e  e directly or in
19f1e 64 69 72 65 63 74 6c 79 2e 20 49 74 20 73 68 6f  directly. It sho
19f1f 75 6c 64 20 62 65 20 75 73 65 64 20 61 73 20 66  uld be used as f
19f20 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  ollows:.**.**   
19f21 4f 70 20 2a 70 4f 70 3b 0a 2a 2a 20 20 20 56 64  Op *pOp;.**   Vd
19f22 62 65 4f 70 49 74 65 72 20 73 49 74 65 72 3b 0a  beOpIter sIter;.
19f23 2a 2a 0a 2a 2a 20 20 20 6d 65 6d 73 65 74 28 26  **.**   memset(&
19f24 73 49 74 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66  sIter, 0, sizeof
19f25 28 73 49 74 65 72 29 29 3b 0a 2a 2a 20 20 20 73  (sIter));.**   s
19f26 49 74 65 72 2e 76 20 3d 20 76 3b 20 20 20 20 20  Iter.v = v;     
19f27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19f28 20 20 20 20 20 20 20 2f 2f 20 76 20 69 73 20 6f         // v is o
19f29 66 20 74 79 70 65 20 56 64 62 65 2a 20 0a 2a 2a  f type Vdbe* .**
19f2a 20 20 20 77 68 69 6c 65 28 20 28 70 4f 70 20 3d     while( (pOp =
19f2b 20 6f 70 49 74 65 72 4e 65 78 74 28 26 73 49 74   opIterNext(&sIt
19f2c 65 72 29 29 20 29 7b 0a 2a 2a 20 20 20 20 20 2f  er)) ){.**     /
19f2d 2f 20 44 6f 20 73 6f 6d 65 74 68 69 6e 67 20 77  / Do something w
19f2e 69 74 68 20 70 4f 70 0a 2a 2a 20 20 20 7d 0a 2a  ith pOp.**   }.*
19f2f 2a 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  *   sqlite3DbFre
19f30 65 28 76 2d 3e 64 62 2c 20 73 49 74 65 72 2e 61  e(v->db, sIter.a
19f31 70 53 75 62 29 3b 0a 2a 2a 20 0a 2a 2f 0a 74 79  pSub);.** .*/.ty
19f32 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62  pedef struct Vdb
19f33 65 4f 70 49 74 65 72 20 56 64 62 65 4f 70 49 74  eOpIter VdbeOpIt
19f34 65 72 3b 0a 73 74 72 75 63 74 20 56 64 62 65 4f  er;.struct VdbeO
19f35 70 49 74 65 72 20 7b 0a 20 20 56 64 62 65 20 2a  pIter {.  Vdbe *
19f36 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v;              
19f37 20 20 20 20 20 2f 2a 20 56 64 62 65 20 74 6f 20       /* Vdbe to 
19f38 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 20  iterate through 
19f39 74 68 65 20 6f 70 63 6f 64 65 73 20 6f 66 20 2a  the opcodes of *
19f3a 2f 0a 20 20 53 75 62 50 72 6f 67 72 61 6d 20 2a  /.  SubProgram *
19f3b 2a 61 70 53 75 62 3b 20 20 20 20 20 20 20 20 2f  *apSub;        /
19f3c 2a 20 41 72 72 61 79 20 6f 66 20 73 75 62 70 72  * Array of subpr
19f3d 6f 67 72 61 6d 73 20 2a 2f 0a 20 20 69 6e 74 20  ograms */.  int 
19f3e 6e 53 75 62 3b 20 20 20 20 20 20 20 20 20 20 20  nSub;           
19f3f 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
19f40 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61   of entries in a
19f41 70 53 75 62 20 2a 2f 0a 20 20 69 6e 74 20 69 41  pSub */.  int iA
19f42 64 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  ddr;            
19f43 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20       /* Address 
19f44 6f 66 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74  of next instruct
19f45 69 6f 6e 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f  ion to return */
19f46 0a 20 20 69 6e 74 20 69 53 75 62 3b 20 20 20 20  .  int iSub;    
19f47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
19f48 20 30 20 3d 20 6d 61 69 6e 20 70 72 6f 67 72 61   0 = main progra
19f49 6d 2c 20 31 20 3d 20 66 69 72 73 74 20 73 75 62  m, 1 = first sub
19f4a 2d 70 72 6f 67 72 61 6d 20 65 74 63 2e 20 2a 2f  -program etc. */
19f4b 0a 7d 3b 0a 73 74 61 74 69 63 20 4f 70 20 2a 6f  .};.static Op *o
19f4c 70 49 74 65 72 4e 65 78 74 28 56 64 62 65 4f 70  pIterNext(VdbeOp
19f4d 49 74 65 72 20 2a 70 29 7b 0a 20 20 56 64 62 65  Iter *p){.  Vdbe
19f4e 20 2a 76 20 3d 20 70 2d 3e 76 3b 0a 20 20 4f 70   *v = p->v;.  Op
19f4f 20 2a 70 52 65 74 20 3d 20 30 3b 0a 20 20 4f 70   *pRet = 0;.  Op
19f50 20 2a 61 4f 70 3b 0a 20 20 69 6e 74 20 6e 4f 70   *aOp;.  int nOp
19f51 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 69 53 75 62  ;..  if( p->iSub
19f52 3c 3d 70 2d 3e 6e 53 75 62 20 29 7b 0a 0a 20 20  <=p->nSub ){..  
19f53 20 20 69 66 28 20 70 2d 3e 69 53 75 62 3d 3d 30    if( p->iSub==0
19f54 20 29 7b 0a 20 20 20 20 20 20 61 4f 70 20 3d 20   ){.      aOp = 
19f55 76 2d 3e 61 4f 70 3b 0a 20 20 20 20 20 20 6e 4f  v->aOp;.      nO
19f56 70 20 3d 20 76 2d 3e 6e 4f 70 3b 0a 20 20 20 20  p = v->nOp;.    
19f57 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 4f 70  }else{.      aOp
19f58 20 3d 20 70 2d 3e 61 70 53 75 62 5b 70 2d 3e 69   = p->apSub[p->i
19f59 53 75 62 2d 31 5d 2d 3e 61 4f 70 3b 0a 20 20 20  Sub-1]->aOp;.   
19f5a 20 20 20 6e 4f 70 20 3d 20 70 2d 3e 61 70 53 75     nOp = p->apSu
19f5b 62 5b 70 2d 3e 69 53 75 62 2d 31 5d 2d 3e 6e 4f  b[p->iSub-1]->nO
19f5c 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  p;.    }.    ass
19f5d 65 72 74 28 20 70 2d 3e 69 41 64 64 72 3c 6e 4f  ert( p->iAddr<nO
19f5e 70 20 29 3b 0a 0a 20 20 20 20 70 52 65 74 20 3d  p );..    pRet =
19f5f 20 26 61 4f 70 5b 70 2d 3e 69 41 64 64 72 5d 3b   &aOp[p->iAddr];
19f60 0a 20 20 20 20 70 2d 3e 69 41 64 64 72 2b 2b 3b  .    p->iAddr++;
19f61 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 41 64 64  .    if( p->iAdd
19f62 72 3d 3d 6e 4f 70 20 29 7b 0a 20 20 20 20 20 20  r==nOp ){.      
19f63 70 2d 3e 69 53 75 62 2b 2b 3b 0a 20 20 20 20 20  p->iSub++;.     
19f64 20 70 2d 3e 69 41 64 64 72 20 3d 20 30 3b 0a 20   p->iAddr = 0;. 
19f65 20 20 20 7d 0a 20 20 0a 20 20 20 20 69 66 28 20     }.  .    if( 
19f66 70 52 65 74 2d 3e 70 34 74 79 70 65 3d 3d 50 34  pRet->p4type==P4
19f67 5f 53 55 42 50 52 4f 47 52 41 4d 20 29 7b 0a 20  _SUBPROGRAM ){. 
19f68 20 20 20 20 20 69 6e 74 20 6e 42 79 74 65 20 3d       int nByte =
19f69 20 28 70 2d 3e 6e 53 75 62 2b 31 29 2a 73 69 7a   (p->nSub+1)*siz
19f6a 65 6f 66 28 53 75 62 50 72 6f 67 72 61 6d 2a 29  eof(SubProgram*)
19f6b 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20  ;.      int j;. 
19f6c 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
19f6d 70 2d 3e 6e 53 75 62 3b 20 6a 2b 2b 29 7b 0a 20  p->nSub; j++){. 
19f6e 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 61 70         if( p->ap
19f6f 53 75 62 5b 6a 5d 3d 3d 70 52 65 74 2d 3e 70 34  Sub[j]==pRet->p4
19f70 2e 70 50 72 6f 67 72 61 6d 20 29 20 62 72 65 61  .pProgram ) brea
19f71 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
19f72 20 69 66 28 20 6a 3d 3d 70 2d 3e 6e 53 75 62 20   if( j==p->nSub 
19f73 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 70  ){.        p->ap
19f74 53 75 62 20 3d 20 73 71 6c 69 74 65 33 44 62 52  Sub = sqlite3DbR
19f75 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 76 2d 3e  eallocOrFree(v->
19f76 64 62 2c 20 70 2d 3e 61 70 53 75 62 2c 20 6e 42  db, p->apSub, nB
19f77 79 74 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66  yte);.        if
19f78 28 20 21 70 2d 3e 61 70 53 75 62 20 29 7b 0a 20  ( !p->apSub ){. 
19f79 20 20 20 20 20 20 20 20 20 70 52 65 74 20 3d 20           pRet = 
19f7a 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  0;.        }else
19f7b 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 61  {.          p->a
19f7c 70 53 75 62 5b 70 2d 3e 6e 53 75 62 2b 2b 5d 20  pSub[p->nSub++] 
19f7d 3d 20 70 52 65 74 2d 3e 70 34 2e 70 50 72 6f 67  = pRet->p4.pProg
19f7e 72 61 6d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ram;.        }. 
19f7f 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
19f80 0a 0a 20 20 72 65 74 75 72 6e 20 70 52 65 74 3b  ..  return pRet;
19f81 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20  .}../*.** Check 
19f82 69 66 20 74 68 65 20 70 72 6f 67 72 61 6d 20 73  if the program s
19f83 74 6f 72 65 64 20 69 6e 20 74 68 65 20 56 4d 20  tored in the VM 
19f84 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
19f85 70 50 61 72 73 65 20 6d 61 79 0a 2a 2a 20 74 68  pParse may.** th
19f86 72 6f 77 20 61 6e 20 41 42 4f 52 54 20 65 78 63  row an ABORT exc
19f87 65 70 74 69 6f 6e 20 28 63 61 75 73 69 6e 67 20  eption (causing 
19f88 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c 20 62  the statement, b
19f89 75 74 20 6e 6f 74 20 74 72 61 6e 73 61 63 74 69  ut not transacti
19f8a 6f 6e 0a 2a 2a 20 74 6f 20 62 65 20 72 6f 6c 6c  on.** to be roll
19f8b 65 64 20 62 61 63 6b 29 2e 20 54 68 69 73 20 63  ed back). This c
19f8c 6f 6e 64 69 74 69 6f 6e 20 69 73 20 74 72 75 65  ondition is true
19f8d 20 69 66 20 74 68 65 20 6d 61 69 6e 20 70 72 6f   if the main pro
19f8e 67 72 61 6d 20 6f 72 20 61 6e 79 0a 2a 2a 20 73  gram or any.** s
19f8f 75 62 2d 70 72 6f 67 72 61 6d 73 20 63 6f 6e 74  ub-programs cont
19f90 61 69 6e 73 20 61 6e 79 20 6f 66 20 74 68 65 20  ains any of the 
19f91 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a  following:.**.**
19f92 20 20 20 2a 20 20 4f 50 5f 48 61 6c 74 20 77 69     *  OP_Halt wi
19f93 74 68 20 50 31 3d 53 51 4c 49 54 45 5f 43 4f 4e  th P1=SQLITE_CON
19f94 53 54 52 41 49 4e 54 20 61 6e 64 20 50 32 3d 4f  STRAINT and P2=O
19f95 45 5f 41 62 6f 72 74 2e 0a 2a 2a 20 20 20 2a 20  E_Abort..**   * 
19f96 20 4f 50 5f 48 61 6c 74 49 66 4e 75 6c 6c 20 77   OP_HaltIfNull w
19f97 69 74 68 20 50 31 3d 53 51 4c 49 54 45 5f 43 4f  ith P1=SQLITE_CO
19f98 4e 53 54 52 41 49 4e 54 20 61 6e 64 20 50 32 3d  NSTRAINT and P2=
19f99 4f 45 5f 41 62 6f 72 74 2e 0a 2a 2a 20 20 20 2a  OE_Abort..**   *
19f9a 20 20 4f 50 5f 44 65 73 74 72 6f 79 0a 2a 2a 20    OP_Destroy.** 
19f9b 20 20 2a 20 20 4f 50 5f 56 55 70 64 61 74 65 0a    *  OP_VUpdate.
19f9c 2a 2a 20 20 20 2a 20 20 4f 50 5f 56 52 65 6e 61  **   *  OP_VRena
19f9d 6d 65 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 63 68  me.**.** Then ch
19f9e 65 63 6b 20 74 68 61 74 20 74 68 65 20 76 61 6c  eck that the val
19f9f 75 65 20 6f 66 20 50 61 72 73 65 2e 6d 61 79 41  ue of Parse.mayA
19fa0 62 6f 72 74 20 69 73 20 74 72 75 65 20 69 66 20  bort is true if 
19fa1 61 6e 0a 2a 2a 20 41 42 4f 52 54 20 6d 61 79 20  an.** ABORT may 
19fa2 62 65 20 74 68 72 6f 77 6e 2c 20 6f 72 20 66 61  be thrown, or fa
19fa3 6c 73 65 20 6f 74 68 65 72 77 69 73 65 2e 20 52  lse otherwise. R
19fa4 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 69 74  eturn true if it
19fa5 20 64 6f 65 73 0a 2a 2a 20 6d 61 74 63 68 2c 20   does.** match, 
19fa6 6f 72 20 66 61 6c 73 65 20 6f 74 68 65 72 77 69  or false otherwi
19fa7 73 65 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f  se. This functio
19fa8 6e 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f  n is intended to
19fa9 20 62 65 20 75 73 65 64 20 61 73 0a 2a 2a 20 70   be used as.** p
19faa 61 72 74 20 6f 66 20 61 6e 20 61 73 73 65 72 74  art of an assert
19fab 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68   statement in th
19fac 65 20 63 6f 6d 70 69 6c 65 72 2e 20 53 69 6d 69  e compiler. Simi
19fad 6c 61 72 20 74 6f 3a 0a 2a 2a 0a 2a 2a 20 20 20  lar to:.**.**   
19fae 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 56  assert( sqlite3V
19faf 64 62 65 41 73 73 65 72 74 4d 61 79 41 62 6f 72  dbeAssertMayAbor
19fb0 74 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c  t(pParse->pVdbe,
19fb1 20 70 50 61 72 73 65 2d 3e 6d 61 79 41 62 6f 72   pParse->mayAbor
19fb2 74 29 20 29 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f  t) );.*/.SQLITE_
19fb3 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
19fb4 74 65 33 56 64 62 65 41 73 73 65 72 74 4d 61 79  te3VdbeAssertMay
19fb5 41 62 6f 72 74 28 56 64 62 65 20 2a 76 2c 20 69  Abort(Vdbe *v, i
19fb6 6e 74 20 6d 61 79 41 62 6f 72 74 29 7b 0a 20 20  nt mayAbort){.  
19fb7 69 6e 74 20 68 61 73 41 62 6f 72 74 20 3d 20 30  int hasAbort = 0
19fb8 3b 0a 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 56  ;.  Op *pOp;.  V
19fb9 64 62 65 4f 70 49 74 65 72 20 73 49 74 65 72 3b  dbeOpIter sIter;
19fba 0a 20 20 6d 65 6d 73 65 74 28 26 73 49 74 65 72  .  memset(&sIter
19fbb 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 49 74 65  , 0, sizeof(sIte
19fbc 72 29 29 3b 0a 20 20 73 49 74 65 72 2e 76 20 3d  r));.  sIter.v =
19fbd 20 76 3b 0a 0a 20 20 77 68 69 6c 65 28 20 28 70   v;..  while( (p
19fbe 4f 70 20 3d 20 6f 70 49 74 65 72 4e 65 78 74 28  Op = opIterNext(
19fbf 26 73 49 74 65 72 29 29 21 3d 30 20 29 7b 0a 20  &sIter))!=0 ){. 
19fc0 20 20 20 69 6e 74 20 6f 70 63 6f 64 65 20 3d 20     int opcode = 
19fc1 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0a 20 20 20  pOp->opcode;.   
19fc2 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f   if( opcode==OP_
19fc3 44 65 73 74 72 6f 79 20 7c 7c 20 6f 70 63 6f 64  Destroy || opcod
19fc4 65 3d 3d 4f 50 5f 56 55 70 64 61 74 65 20 7c 7c  e==OP_VUpdate ||
19fc5 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 52 65 6e   opcode==OP_VRen
19fc6 61 6d 65 20 0a 20 20 20 20 20 7c 7c 20 28 28 6f  ame .     || ((o
19fc7 70 63 6f 64 65 3d 3d 4f 50 5f 48 61 6c 74 20 7c  pcode==OP_Halt |
19fc8 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 48 61 6c  | opcode==OP_Hal
19fc9 74 49 66 4e 75 6c 6c 29 20 0a 20 20 20 20 20 20  tIfNull) .      
19fca 26 26 20 28 70 4f 70 2d 3e 70 31 3d 3d 53 51 4c  && (pOp->p1==SQL
19fcb 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 26  ITE_CONSTRAINT &
19fcc 26 20 70 4f 70 2d 3e 70 32 3d 3d 4f 45 5f 41 62  & pOp->p2==OE_Ab
19fcd 6f 72 74 29 29 0a 20 20 20 20 29 7b 0a 20 20 20  ort)).    ){.   
19fce 20 20 20 68 61 73 41 62 6f 72 74 20 3d 20 31 3b     hasAbort = 1;
19fcf 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
19fd0 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65    }.  }.  sqlite
19fd1 33 44 62 46 72 65 65 28 76 2d 3e 64 62 2c 20 73  3DbFree(v->db, s
19fd2 49 74 65 72 2e 61 70 53 75 62 29 3b 0a 0a 20 20  Iter.apSub);..  
19fd3 2f 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69  /* Return true i
19fd4 66 20 68 61 73 41 62 6f 72 74 3d 3d 6d 61 79 41  f hasAbort==mayA
19fd5 62 6f 72 74 2e 20 4f 72 20 69 66 20 61 20 6d 61  bort. Or if a ma
19fd6 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f 63 63  lloc failure occ
19fd7 75 72 65 64 2e 0a 20 20 2a 2a 20 49 66 20 6d 61  ured..  ** If ma
19fd8 6c 6c 6f 63 20 66 61 69 6c 65 64 2c 20 74 68 65  lloc failed, the
19fd9 6e 20 74 68 65 20 77 68 69 6c 65 28 29 20 6c 6f  n the while() lo
19fda 6f 70 20 61 62 6f 76 65 20 6d 61 79 20 6e 6f 74  op above may not
19fdb 20 68 61 76 65 20 69 74 65 72 61 74 65 64 0a 20   have iterated. 
19fdc 20 2a 2a 20 74 68 72 6f 75 67 68 20 61 6c 6c 20   ** through all 
19fdd 6f 70 63 6f 64 65 73 20 61 6e 64 20 68 61 73 41  opcodes and hasA
19fde 62 6f 72 74 20 6d 61 79 20 62 65 20 73 65 74 20  bort may be set 
19fdf 69 6e 63 6f 72 72 65 63 74 6c 79 2e 20 52 65 74  incorrectly. Ret
19fe0 75 72 6e 0a 20 20 2a 2a 20 74 72 75 65 20 66 6f  urn.  ** true fo
19fe1 72 20 74 68 69 73 20 63 61 73 65 20 74 6f 20 70  r this case to p
19fe2 72 65 76 65 6e 74 20 74 68 65 20 61 73 73 65 72  revent the asser
19fe3 74 28 29 20 69 6e 20 74 68 65 20 63 61 6c 6c 65  t() in the calle
19fe4 72 73 20 66 72 61 6d 65 0a 20 20 2a 2a 20 66 72  rs frame.  ** fr
19fe5 6f 6d 20 66 61 69 6c 69 6e 67 2e 20 20 2a 2f 0a  om failing.  */.
19fe6 20 20 72 65 74 75 72 6e 20 28 20 76 2d 3e 64 62    return ( v->db
19fe7 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c  ->mallocFailed |
19fe8 7c 20 68 61 73 41 62 6f 72 74 3d 3d 6d 61 79 41  | hasAbort==mayA
19fe9 62 6f 72 74 20 29 3b 0a 7d 0a 23 65 6e 64 69 66  bort );.}.#endif
19fea 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 70 20 74 68 72  ../*.** Loop thr
19feb 6f 75 67 68 20 74 68 65 20 70 72 6f 67 72 61 6d  ough the program
19fec 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 50 32 20   looking for P2 
19fed 76 61 6c 75 65 73 20 74 68 61 74 20 61 72 65 20  values that are 
19fee 6e 65 67 61 74 69 76 65 0a 2a 2a 20 6f 6e 20 6a  negative.** on j
19fef 75 6d 70 20 69 6e 73 74 72 75 63 74 69 6f 6e 73  ump instructions
19ff0 2e 20 20 45 61 63 68 20 73 75 63 68 20 76 61 6c  .  Each such val
19ff1 75 65 20 69 73 20 61 20 6c 61 62 65 6c 2e 20 20  ue is a label.  
19ff2 52 65 73 6f 6c 76 65 20 74 68 65 0a 2a 2a 20 6c  Resolve the.** l
19ff3 61 62 65 6c 20 62 79 20 73 65 74 74 69 6e 67 20  abel by setting 
19ff4 74 68 65 20 50 32 20 76 61 6c 75 65 20 74 6f 20  the P2 value to 
19ff5 69 74 73 20 63 6f 72 72 65 63 74 20 6e 6f 6e 2d  its correct non-
19ff6 7a 65 72 6f 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a  zero value..**.*
19ff7 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
19ff8 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 61 66  s called once af
19ff9 74 65 72 20 61 6c 6c 20 6f 70 63 6f 64 65 73 20  ter all opcodes 
19ffa 68 61 76 65 20 62 65 65 6e 20 69 6e 73 65 72 74  have been insert
19ffb 65 64 2e 0a 2a 2a 0a 2a 2a 20 56 61 72 69 61 62  ed..**.** Variab
19ffc 6c 65 20 2a 70 4d 61 78 46 75 6e 63 41 72 67 73  le *pMaxFuncArgs
19ffd 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6d   is set to the m
19ffe 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20  aximum value of 
19fff 61 6e 79 20 50 32 20 61 72 67 75 6d 65 6e 74 20  any P2 argument 
1a000 0a 2a 2a 20 74 6f 20 61 6e 20 4f 50 5f 46 75 6e  .** to an OP_Fun
1a001 63 74 69 6f 6e 2c 20 4f 50 5f 41 67 67 53 74 65  ction, OP_AggSte
1a002 70 20 6f 72 20 4f 50 5f 56 46 69 6c 74 65 72 20  p or OP_VFilter 
1a003 6f 70 63 6f 64 65 2e 20 54 68 69 73 20 69 73 20  opcode. This is 
1a004 75 73 65 64 20 62 79 20 0a 2a 2a 20 73 71 6c 69  used by .** sqli
1a005 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79  te3VdbeMakeReady
1a006 28 29 20 74 6f 20 73 69 7a 65 20 74 68 65 20 56  () to size the V
1a007 64 62 65 2e 61 70 41 72 67 5b 5d 20 61 72 72 61  dbe.apArg[] arra
1a008 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  y..*/.static voi
1a009 64 20 72 65 73 6f 6c 76 65 50 32 56 61 6c 75 65  d resolveP2Value
1a00a 73 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 2a  s(Vdbe *p, int *
1a00b 70 4d 61 78 46 75 6e 63 41 72 67 73 29 7b 0a 20  pMaxFuncArgs){. 
1a00c 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 4d   int i;.  int nM
1a00d 61 78 41 72 67 73 20 3d 20 2a 70 4d 61 78 46 75  axArgs = *pMaxFu
1a00e 6e 63 41 72 67 73 3b 0a 20 20 4f 70 20 2a 70 4f  ncArgs;.  Op *pO
1a00f 70 3b 0a 20 20 69 6e 74 20 2a 61 4c 61 62 65 6c  p;.  int *aLabel
1a010 20 3d 20 70 2d 3e 61 4c 61 62 65 6c 3b 0a 20 20   = p->aLabel;.  
1a011 70 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 31 3b  p->readOnly = 1;
1a012 0a 20 20 66 6f 72 28 70 4f 70 3d 70 2d 3e 61 4f  .  for(pOp=p->aO
1a013 70 2c 20 69 3d 70 2d 3e 6e 4f 70 2d 31 3b 20 69  p, i=p->nOp-1; i
1a014 3e 3d 30 3b 20 69 2d 2d 2c 20 70 4f 70 2b 2b 29  >=0; i--, pOp++)
1a015 7b 0a 20 20 20 20 75 38 20 6f 70 63 6f 64 65 20  {.    u8 opcode 
1a016 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0a 0a  = pOp->opcode;..
1a017 20 20 20 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d      if( opcode==
1a018 4f 50 5f 46 75 6e 63 74 69 6f 6e 20 7c 7c 20 6f  OP_Function || o
1a019 70 63 6f 64 65 3d 3d 4f 50 5f 41 67 67 53 74 65  pcode==OP_AggSte
1a01a 70 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  p ){.      if( p
1a01b 4f 70 2d 3e 70 35 3e 6e 4d 61 78 41 72 67 73 20  Op->p5>nMaxArgs 
1a01c 29 20 6e 4d 61 78 41 72 67 73 20 3d 20 70 4f 70  ) nMaxArgs = pOp
1a01d 2d 3e 70 35 3b 0a 23 69 66 6e 64 65 66 20 53 51  ->p5;.#ifndef SQ
1a01e 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
1a01f 4c 54 41 42 4c 45 0a 20 20 20 20 7d 65 6c 73 65  LTABLE.    }else
1a020 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f   if( opcode==OP_
1a021 56 55 70 64 61 74 65 20 29 7b 0a 20 20 20 20 20  VUpdate ){.     
1a022 20 69 66 28 20 70 4f 70 2d 3e 70 32 3e 6e 4d 61   if( pOp->p2>nMa
1a023 78 41 72 67 73 20 29 20 6e 4d 61 78 41 72 67 73  xArgs ) nMaxArgs
1a024 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 23 65 6e 64   = pOp->p2;.#end
1a025 69 66 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  if.    }else if(
1a026 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 54 72 61 6e   opcode==OP_Tran
1a027 73 61 63 74 69 6f 6e 20 26 26 20 70 4f 70 2d 3e  saction && pOp->
1a028 70 32 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70  p2!=0 ){.      p
1a029 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 30 3b 0a  ->readOnly = 0;.
1a02a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1a02b 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1a02c 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f  .    }else if( o
1a02d 70 63 6f 64 65 3d 3d 4f 50 5f 56 46 69 6c 74 65  pcode==OP_VFilte
1a02e 72 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e  r ){.      int n
1a02f 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1a030 70 2d 3e 6e 4f 70 20 2d 20 69 20 3e 3d 20 33 20  p->nOp - i >= 3 
1a031 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
1a032 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65 3d   pOp[-1].opcode=
1a033 3d 4f 50 5f 49 6e 74 65 67 65 72 20 29 3b 0a 20  =OP_Integer );. 
1a034 20 20 20 20 20 6e 20 3d 20 70 4f 70 5b 2d 31 5d       n = pOp[-1]
1a035 2e 70 31 3b 0a 20 20 20 20 20 20 69 66 28 20 6e  .p1;.      if( n
1a036 3e 6e 4d 61 78 41 72 67 73 20 29 20 6e 4d 61 78  >nMaxArgs ) nMax
1a037 41 72 67 73 20 3d 20 6e 3b 0a 23 65 6e 64 69 66  Args = n;.#endif
1a038 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20  .    }..    if( 
1a039 73 71 6c 69 74 65 33 56 64 62 65 4f 70 63 6f 64  sqlite3VdbeOpcod
1a03a 65 48 61 73 50 72 6f 70 65 72 74 79 28 6f 70 63  eHasProperty(opc
1a03b 6f 64 65 2c 20 4f 50 46 4c 47 5f 4a 55 4d 50 29  ode, OPFLG_JUMP)
1a03c 20 26 26 20 70 4f 70 2d 3e 70 32 3c 30 20 29 7b   && pOp->p2<0 ){
1a03d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 2d  .      assert( -
1a03e 31 2d 70 4f 70 2d 3e 70 32 3c 70 2d 3e 6e 4c 61  1-pOp->p2<p->nLa
1a03f 62 65 6c 20 29 3b 0a 20 20 20 20 20 20 70 4f 70  bel );.      pOp
1a040 2d 3e 70 32 20 3d 20 61 4c 61 62 65 6c 5b 2d 31  ->p2 = aLabel[-1
1a041 2d 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 7d  -pOp->p2];.    }
1a042 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62  .  }.  sqlite3Db
1a043 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d 3e 61  Free(p->db, p->a
1a044 4c 61 62 65 6c 29 3b 0a 20 20 70 2d 3e 61 4c 61  Label);.  p->aLa
1a045 62 65 6c 20 3d 20 30 3b 0a 0a 20 20 2a 70 4d 61  bel = 0;..  *pMa
1a046 78 46 75 6e 63 41 72 67 73 20 3d 20 6e 4d 61 78  xFuncArgs = nMax
1a047 41 72 67 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  Args;.}../*.** R
1a048 65 74 75 72 6e 20 74 68 65 20 61 64 64 72 65 73  eturn the addres
1a049 73 20 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e  s of the next in
1a04a 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 62 65 20  struction to be 
1a04b 69 6e 73 65 72 74 65 64 2e 0a 2a 2f 0a 53 51 4c  inserted..*/.SQL
1a04c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1a04d 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
1a04e 6e 74 41 64 64 72 28 56 64 62 65 20 2a 70 29 7b  ntAddr(Vdbe *p){
1a04f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61  .  assert( p->ma
1a050 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f  gic==VDBE_MAGIC_
1a051 49 4e 49 54 20 29 3b 0a 20 20 72 65 74 75 72 6e  INIT );.  return
1a052 20 70 2d 3e 6e 4f 70 3b 0a 7d 0a 0a 2f 2a 0a 2a   p->nOp;.}../*.*
1a053 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
1a054 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
1a055 72 20 74 6f 20 74 68 65 20 61 72 72 61 79 20 6f  r to the array o
1a056 66 20 6f 70 63 6f 64 65 73 20 61 73 73 6f 63 69  f opcodes associ
1a057 61 74 65 64 20 77 69 74 68 0a 2a 2a 20 74 68 65  ated with.** the
1a058 20 56 64 62 65 20 70 61 73 73 65 64 20 61 73 20   Vdbe passed as 
1a059 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
1a05a 6e 74 2e 20 49 74 20 69 73 20 74 68 65 20 63 61  nt. It is the ca
1a05b 6c 6c 65 72 73 20 72 65 73 70 6f 6e 73 69 62 69  llers responsibi
1a05c 6c 69 74 79 0a 2a 2a 20 74 6f 20 61 72 72 61 6e  lity.** to arran
1a05d 67 65 20 66 6f 72 20 74 68 65 20 72 65 74 75 72  ge for the retur
1a05e 6e 65 64 20 61 72 72 61 79 20 74 6f 20 62 65 20  ned array to be 
1a05f 65 76 65 6e 74 75 61 6c 6c 79 20 66 72 65 65 64  eventually freed
1a060 20 75 73 69 6e 67 20 74 68 65 20 0a 2a 2a 20 76   using the .** v
1a061 64 62 65 46 72 65 65 4f 70 41 72 72 61 79 28 29  dbeFreeOpArray()
1a062 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   function..**.**
1a063 20 42 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e   Before returnin
1a064 67 2c 20 2a 70 6e 4f 70 20 69 73 20 73 65 74 20  g, *pnOp is set 
1a065 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  to the number of
1a066 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20   entries in the 
1a067 72 65 74 75 72 6e 65 64 0a 2a 2a 20 61 72 72 61  returned.** arra
1a068 79 2e 20 41 6c 73 6f 2c 20 2a 70 6e 4d 61 78 41  y. Also, *pnMaxA
1a069 72 67 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  rg is set to the
1a06a 20 6c 61 72 67 65 72 20 6f 66 20 69 74 73 20 63   larger of its c
1a06b 75 72 72 65 6e 74 20 76 61 6c 75 65 20 61 6e 64  urrent value and
1a06c 20 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20   .** the number 
1a06d 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68  of entries in th
1a06e 65 20 56 64 62 65 2e 61 70 41 72 67 5b 5d 20 61  e Vdbe.apArg[] a
1a06f 72 72 61 79 20 72 65 71 75 69 72 65 64 20 74 6f  rray required to
1a070 20 65 78 65 63 75 74 65 20 74 68 65 20 0a 2a 2a   execute the .**
1a071 20 72 65 74 75 72 6e 65 64 20 70 72 6f 67 72 61   returned progra
1a072 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  m..*/.SQLITE_PRI
1a073 56 41 54 45 20 56 64 62 65 4f 70 20 2a 73 71 6c  VATE VdbeOp *sql
1a074 69 74 65 33 56 64 62 65 54 61 6b 65 4f 70 41 72  ite3VdbeTakeOpAr
1a075 72 61 79 28 56 64 62 65 20 2a 70 2c 20 69 6e 74  ray(Vdbe *p, int
1a076 20 2a 70 6e 4f 70 2c 20 69 6e 74 20 2a 70 6e 4d   *pnOp, int *pnM
1a077 61 78 41 72 67 29 7b 0a 20 20 56 64 62 65 4f 70  axArg){.  VdbeOp
1a078 20 2a 61 4f 70 20 3d 20 70 2d 3e 61 4f 70 3b 0a   *aOp = p->aOp;.
1a079 20 20 61 73 73 65 72 74 28 20 61 4f 70 20 26 26    assert( aOp &&
1a07a 20 21 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46   !p->db->mallocF
1a07b 61 69 6c 65 64 20 29 3b 0a 0a 20 20 2f 2a 20 43  ailed );..  /* C
1a07c 68 65 63 6b 20 74 68 61 74 20 73 71 6c 69 74 65  heck that sqlite
1a07d 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28 29  3VdbeUsesBtree()
1a07e 20 77 61 73 20 6e 6f 74 20 63 61 6c 6c 65 64 20   was not called 
1a07f 6f 6e 20 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20  on this VM */.  
1a080 61 73 73 65 72 74 28 20 70 2d 3e 61 4d 75 74 65  assert( p->aMute
1a081 78 2e 6e 4d 75 74 65 78 3d 3d 30 20 29 3b 0a 0a  x.nMutex==0 );..
1a082 20 20 72 65 73 6f 6c 76 65 50 32 56 61 6c 75 65    resolveP2Value
1a083 73 28 70 2c 20 70 6e 4d 61 78 41 72 67 29 3b 0a  s(p, pnMaxArg);.
1a084 20 20 2a 70 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70    *pnOp = p->nOp
1a085 3b 0a 20 20 70 2d 3e 61 4f 70 20 3d 20 30 3b 0a  ;.  p->aOp = 0;.
1a086 20 20 72 65 74 75 72 6e 20 61 4f 70 3b 0a 7d 0a    return aOp;.}.
1a087 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 77 68 6f  ./*.** Add a who
1a088 6c 65 20 6c 69 73 74 20 6f 66 20 6f 70 65 72 61  le list of opera
1a089 74 69 6f 6e 73 20 74 6f 20 74 68 65 20 6f 70 65  tions to the ope
1a08a 72 61 74 69 6f 6e 20 73 74 61 63 6b 2e 20 20 52  ration stack.  R
1a08b 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20 61 64 64  eturn the.** add
1a08c 72 65 73 73 20 6f 66 20 74 68 65 20 66 69 72 73  ress of the firs
1a08d 74 20 6f 70 65 72 61 74 69 6f 6e 20 61 64 64 65  t operation adde
1a08e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
1a08f 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1a090 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 56 64  VdbeAddOpList(Vd
1a091 62 65 20 2a 70 2c 20 69 6e 74 20 6e 4f 70 2c 20  be *p, int nOp, 
1a092 56 64 62 65 4f 70 4c 69 73 74 20 63 6f 6e 73 74  VdbeOpList const
1a093 20 2a 61 4f 70 29 7b 0a 20 20 69 6e 74 20 61 64   *aOp){.  int ad
1a094 64 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  dr;.  assert( p-
1a095 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47  >magic==VDBE_MAG
1a096 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28  IC_INIT );.  if(
1a097 20 70 2d 3e 6e 4f 70 20 2b 20 6e 4f 70 20 3e 20   p->nOp + nOp > 
1a098 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 26 26 20 67  p->nOpAlloc && g
1a099 72 6f 77 4f 70 41 72 72 61 79 28 70 29 20 29 7b  rowOpArray(p) ){
1a09a 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
1a09b 20 7d 0a 20 20 61 64 64 72 20 3d 20 70 2d 3e 6e   }.  addr = p->n
1a09c 4f 70 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53  Op;.  if( ALWAYS
1a09d 28 6e 4f 70 3e 30 29 20 29 7b 0a 20 20 20 20 69  (nOp>0) ){.    i
1a09e 6e 74 20 69 3b 0a 20 20 20 20 56 64 62 65 4f 70  nt i;.    VdbeOp
1a09f 4c 69 73 74 20 63 6f 6e 73 74 20 2a 70 49 6e 20  List const *pIn 
1a0a0 3d 20 61 4f 70 3b 0a 20 20 20 20 66 6f 72 28 69  = aOp;.    for(i
1a0a1 3d 30 3b 20 69 3c 6e 4f 70 3b 20 69 2b 2b 2c 20  =0; i<nOp; i++, 
1a0a2 70 49 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e  pIn++){.      in
1a0a3 74 20 70 32 20 3d 20 70 49 6e 2d 3e 70 32 3b 0a  t p2 = pIn->p2;.
1a0a4 20 20 20 20 20 20 56 64 62 65 4f 70 20 2a 70 4f        VdbeOp *pO
1a0a5 75 74 20 3d 20 26 70 2d 3e 61 4f 70 5b 69 2b 61  ut = &p->aOp[i+a
1a0a6 64 64 72 5d 3b 0a 20 20 20 20 20 20 70 4f 75 74  ddr];.      pOut
1a0a7 2d 3e 6f 70 63 6f 64 65 20 3d 20 70 49 6e 2d 3e  ->opcode = pIn->
1a0a8 6f 70 63 6f 64 65 3b 0a 20 20 20 20 20 20 70 4f  opcode;.      pO
1a0a9 75 74 2d 3e 70 31 20 3d 20 70 49 6e 2d 3e 70 31  ut->p1 = pIn->p1
1a0aa 3b 0a 20 20 20 20 20 20 69 66 28 20 70 32 3c 30  ;.      if( p2<0
1a0ab 20 26 26 20 73 71 6c 69 74 65 33 56 64 62 65 4f   && sqlite3VdbeO
1a0ac 70 63 6f 64 65 48 61 73 50 72 6f 70 65 72 74 79  pcodeHasProperty
1a0ad 28 70 4f 75 74 2d 3e 6f 70 63 6f 64 65 2c 20 4f  (pOut->opcode, O
1a0ae 50 46 4c 47 5f 4a 55 4d 50 29 20 29 7b 0a 20 20  PFLG_JUMP) ){.  
1a0af 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 32 20 3d        pOut->p2 =
1a0b0 20 61 64 64 72 20 2b 20 41 44 44 52 28 70 32 29   addr + ADDR(p2)
1a0b1 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
1a0b2 20 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 32 20         pOut->p2 
1a0b3 3d 20 70 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20  = p2;.      }.  
1a0b4 20 20 20 20 70 4f 75 74 2d 3e 70 33 20 3d 20 70      pOut->p3 = p
1a0b5 49 6e 2d 3e 70 33 3b 0a 20 20 20 20 20 20 70 4f  In->p3;.      pO
1a0b6 75 74 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f  ut->p4type = P4_
1a0b7 4e 4f 54 55 53 45 44 3b 0a 20 20 20 20 20 20 70  NOTUSED;.      p
1a0b8 4f 75 74 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20  Out->p4.p = 0;. 
1a0b9 20 20 20 20 20 70 4f 75 74 2d 3e 70 35 20 3d 20       pOut->p5 = 
1a0ba 30 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  0;.#ifdef SQLITE
1a0bb 5f 44 45 42 55 47 0a 20 20 20 20 20 20 70 4f 75  _DEBUG.      pOu
1a0bc 74 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3d 20 30 3b  t->zComment = 0;
1a0bd 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
1a0be 65 33 56 64 62 65 41 64 64 6f 70 54 72 61 63 65  e3VdbeAddopTrace
1a0bf 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
1a0c0 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 30  te3VdbePrintOp(0
1a0c1 2c 20 69 2b 61 64 64 72 2c 20 26 70 2d 3e 61 4f  , i+addr, &p->aO
1a0c2 70 5b 69 2b 61 64 64 72 5d 29 3b 0a 20 20 20 20  p[i+addr]);.    
1a0c3 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d    }.#endif.    }
1a0c4 0a 20 20 20 20 70 2d 3e 6e 4f 70 20 2b 3d 20 6e  .    p->nOp += n
1a0c5 4f 70 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  Op;.  }.  return
1a0c6 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   addr;.}../*.** 
1a0c7 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65  Change the value
1a0c8 20 6f 66 20 74 68 65 20 50 31 20 6f 70 65 72 61   of the P1 opera
1a0c9 6e 64 20 66 6f 72 20 61 20 73 70 65 63 69 66 69  nd for a specifi
1a0ca 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a  c instruction..*
1a0cb 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
1a0cc 73 20 75 73 65 66 75 6c 20 77 68 65 6e 20 61 20  s useful when a 
1a0cd 6c 61 72 67 65 20 70 72 6f 67 72 61 6d 20 69 73  large program is
1a0ce 20 6c 6f 61 64 65 64 20 66 72 6f 6d 20 61 0a 2a   loaded from a.*
1a0cf 2a 20 73 74 61 74 69 63 20 61 72 72 61 79 20 75  * static array u
1a0d0 73 69 6e 67 20 73 71 6c 69 74 65 33 56 64 62 65  sing sqlite3Vdbe
1a0d1 41 64 64 4f 70 4c 69 73 74 20 62 75 74 20 77 65  AddOpList but we
1a0d2 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20 61 0a   want to make a.
1a0d3 2a 2a 20 66 65 77 20 6d 69 6e 6f 72 20 63 68 61  ** few minor cha
1a0d4 6e 67 65 73 20 74 6f 20 74 68 65 20 70 72 6f 67  nges to the prog
1a0d5 72 61 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ram..*/.SQLITE_P
1a0d6 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1a0d7 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28  te3VdbeChangeP1(
1a0d8 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64  Vdbe *p, int add
1a0d9 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20 61  r, int val){.  a
1a0da 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20  ssert( p!=0 );. 
1a0db 20 61 73 73 65 72 74 28 20 61 64 64 72 3e 3d 30   assert( addr>=0
1a0dc 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70   );.  if( p->nOp
1a0dd 3e 61 64 64 72 20 29 7b 0a 20 20 20 20 70 2d 3e  >addr ){.    p->
1a0de 61 4f 70 5b 61 64 64 72 5d 2e 70 31 20 3d 20 76  aOp[addr].p1 = v
1a0df 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  al;.  }.}../*.**
1a0e0 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75   Change the valu
1a0e1 65 20 6f 66 20 74 68 65 20 50 32 20 6f 70 65 72  e of the P2 oper
1a0e2 61 6e 64 20 66 6f 72 20 61 20 73 70 65 63 69 66  and for a specif
1a0e3 69 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a  ic instruction..
1a0e4 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
1a0e5 69 73 20 75 73 65 66 75 6c 20 66 6f 72 20 73 65  is useful for se
1a0e6 74 74 69 6e 67 20 61 20 6a 75 6d 70 20 64 65 73  tting a jump des
1a0e7 74 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  tination..*/.SQL
1a0e8 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1a0e9 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
1a0ea 67 65 50 32 28 56 64 62 65 20 2a 70 2c 20 69 6e  geP2(Vdbe *p, in
1a0eb 74 20 61 64 64 72 2c 20 69 6e 74 20 76 61 6c 29  t addr, int val)
1a0ec 7b 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30  {.  assert( p!=0
1a0ed 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 61 64   );.  assert( ad
1a0ee 64 72 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 70  dr>=0 );.  if( p
1a0ef 2d 3e 6e 4f 70 3e 61 64 64 72 20 29 7b 0a 20 20  ->nOp>addr ){.  
1a0f0 20 20 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 2e 70    p->aOp[addr].p
1a0f1 32 20 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a  2 = val;.  }.}..
1a0f2 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  /*.** Change the
1a0f3 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 33   value of the P3
1a0f4 20 6f 70 65 72 61 6e 64 20 66 6f 72 20 61 20 73   operand for a s
1a0f5 70 65 63 69 66 69 63 20 69 6e 73 74 72 75 63 74  pecific instruct
1a0f6 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion..*/.SQLITE_P
1a0f7 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1a0f8 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28  te3VdbeChangeP3(
1a0f9 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64  Vdbe *p, int add
1a0fa 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20 61  r, int val){.  a
1a0fb 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20  ssert( p!=0 );. 
1a0fc 20 61 73 73 65 72 74 28 20 61 64 64 72 3e 3d 30   assert( addr>=0
1a0fd 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70   );.  if( p->nOp
1a0fe 3e 61 64 64 72 20 29 7b 0a 20 20 20 20 70 2d 3e  >addr ){.    p->
1a0ff 61 4f 70 5b 61 64 64 72 5d 2e 70 33 20 3d 20 76  aOp[addr].p3 = v
1a100 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  al;.  }.}../*.**
1a101 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75   Change the valu
1a102 65 20 6f 66 20 74 68 65 20 50 35 20 6f 70 65 72  e of the P5 oper
1a103 61 6e 64 20 66 6f 72 20 74 68 65 20 6d 6f 73 74  and for the most
1a104 20 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 61 64 64   recently.** add
1a105 65 64 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2f  ed operation..*/
1a106 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1a107 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
1a108 43 68 61 6e 67 65 50 35 28 56 64 62 65 20 2a 70  ChangeP5(Vdbe *p
1a109 2c 20 75 38 20 76 61 6c 29 7b 0a 20 20 61 73 73  , u8 val){.  ass
1a10a 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 69  ert( p!=0 );.  i
1a10b 66 28 20 70 2d 3e 61 4f 70 20 29 7b 0a 20 20 20  f( p->aOp ){.   
1a10c 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e   assert( p->nOp>
1a10d 30 20 29 3b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b  0 );.    p->aOp[
1a10e 70 2d 3e 6e 4f 70 2d 31 5d 2e 70 35 20 3d 20 76  p->nOp-1].p5 = v
1a10f 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  al;.  }.}../*.**
1a110 20 43 68 61 6e 67 65 20 74 68 65 20 50 32 20 6f   Change the P2 o
1a111 70 65 72 61 6e 64 20 6f 66 20 69 6e 73 74 72 75  perand of instru
1a112 63 74 69 6f 6e 20 61 64 64 72 20 73 6f 20 74 68  ction addr so th
1a113 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 0a  at it points to.
1a114 2a 2a 20 74 68 65 20 61 64 64 72 65 73 73 20 6f  ** the address o
1a115 66 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72  f the next instr
1a116 75 63 74 69 6f 6e 20 74 6f 20 62 65 20 63 6f 64  uction to be cod
1a117 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
1a118 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1a119 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 56  e3VdbeJumpHere(V
1a11a 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72  dbe *p, int addr
1a11b 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  ){.  sqlite3Vdbe
1a11c 43 68 61 6e 67 65 50 32 28 70 2c 20 61 64 64 72  ChangeP2(p, addr
1a11d 2c 20 70 2d 3e 6e 4f 70 29 3b 0a 7d 0a 0a 0a 2f  , p->nOp);.}.../
1a11e 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 70 75  *.** If the inpu
1a11f 74 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 74  t FuncDef struct
1a120 75 72 65 20 69 73 20 65 70 68 65 6d 65 72 61 6c  ure is ephemeral
1a121 2c 20 74 68 65 6e 20 66 72 65 65 20 69 74 2e 20  , then free it. 
1a122 20 49 66 0a 2a 2a 20 74 68 65 20 46 75 6e 63 44   If.** the FuncD
1a123 65 66 20 69 73 20 6e 6f 74 20 65 70 68 65 72 6d  ef is not epherm
1a124 61 6c 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 68  al, then do noth
1a125 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ing..*/.static v
1a126 6f 69 64 20 66 72 65 65 45 70 68 65 6d 65 72 61  oid freeEphemera
1a127 6c 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65  lFunction(sqlite
1a128 33 20 2a 64 62 2c 20 46 75 6e 63 44 65 66 20 2a  3 *db, FuncDef *
1a129 70 44 65 66 29 7b 0a 20 20 69 66 28 20 41 4c 57  pDef){.  if( ALW
1a12a 41 59 53 28 70 44 65 66 29 20 26 26 20 28 70 44  AYS(pDef) && (pD
1a12b 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  ef->flags & SQLI
1a12c 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d 29 21 3d  TE_FUNC_EPHEM)!=
1a12d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
1a12e 44 62 46 72 65 65 28 64 62 2c 20 70 44 65 66 29  DbFree(db, pDef)
1a12f 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  ;.  }.}../*.** D
1a130 65 6c 65 74 65 20 61 20 50 34 20 76 61 6c 75 65  elete a P4 value
1a131 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a   if necessary..*
1a132 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 72  /.static void fr
1a133 65 65 50 34 28 73 71 6c 69 74 65 33 20 2a 64 62  eeP4(sqlite3 *db
1a134 2c 20 69 6e 74 20 70 34 74 79 70 65 2c 20 76 6f  , int p4type, vo
1a135 69 64 20 2a 70 34 29 7b 0a 20 20 69 66 28 20 70  id *p4){.  if( p
1a136 34 20 29 7b 0a 20 20 20 20 73 77 69 74 63 68 28  4 ){.    switch(
1a137 20 70 34 74 79 70 65 20 29 7b 0a 20 20 20 20 20   p4type ){.     
1a138 20 63 61 73 65 20 50 34 5f 52 45 41 4c 3a 0a 20   case P4_REAL:. 
1a139 20 20 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54       case P4_INT
1a13a 36 34 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50  64:.      case P
1a13b 34 5f 4d 50 52 49 4e 54 46 3a 0a 20 20 20 20 20  4_MPRINTF:.     
1a13c 20 63 61 73 65 20 50 34 5f 44 59 4e 41 4d 49 43   case P4_DYNAMIC
1a13d 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f  :.      case P4_
1a13e 4b 45 59 49 4e 46 4f 3a 0a 20 20 20 20 20 20 63  KEYINFO:.      c
1a13f 61 73 65 20 50 34 5f 49 4e 54 41 52 52 41 59 3a  ase P4_INTARRAY:
1a140 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 4b  .      case P4_K
1a141 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 3a 20  EYINFO_HANDOFF: 
1a142 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1a143 33 44 62 46 72 65 65 28 64 62 2c 20 70 34 29 3b  3DbFree(db, p4);
1a144 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
1a145 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61        }.      ca
1a146 73 65 20 50 34 5f 56 44 42 45 46 55 4e 43 3a 20  se P4_VDBEFUNC: 
1a147 7b 0a 20 20 20 20 20 20 20 20 56 64 62 65 46 75  {.        VdbeFu
1a148 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 20 3d 20  nc *pVdbeFunc = 
1a149 28 56 64 62 65 46 75 6e 63 20 2a 29 70 34 3b 0a  (VdbeFunc *)p4;.
1a14a 20 20 20 20 20 20 20 20 66 72 65 65 45 70 68 65          freeEphe
1a14b 6d 65 72 61 6c 46 75 6e 63 74 69 6f 6e 28 64 62  meralFunction(db
1a14c 2c 20 70 56 64 62 65 46 75 6e 63 2d 3e 70 46 75  , pVdbeFunc->pFu
1a14d 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  nc);.        sql
1a14e 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 41 75  ite3VdbeDeleteAu
1a14f 78 44 61 74 61 28 70 56 64 62 65 46 75 6e 63 2c  xData(pVdbeFunc,
1a150 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c   0);.        sql
1a151 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
1a152 56 64 62 65 46 75 6e 63 29 3b 0a 20 20 20 20 20  VdbeFunc);.     
1a153 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
1a154 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f  }.      case P4_
1a155 46 55 4e 43 44 45 46 3a 20 7b 0a 20 20 20 20 20  FUNCDEF: {.     
1a156 20 20 20 66 72 65 65 45 70 68 65 6d 65 72 61 6c     freeEphemeral
1a157 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 28 46 75  Function(db, (Fu
1a158 6e 63 44 65 66 2a 29 70 34 29 3b 0a 20 20 20 20  ncDef*)p4);.    
1a159 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
1a15a 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50 34   }.      case P4
1a15b 5f 4d 45 4d 3a 20 7b 0a 20 20 20 20 20 20 20 20  _MEM: {.        
1a15c 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65  sqlite3ValueFree
1a15d 28 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  ((sqlite3_value*
1a15e 29 70 34 29 3b 0a 20 20 20 20 20 20 20 20 62 72  )p4);.        br
1a15f 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
1a160 20 20 20 63 61 73 65 20 50 34 5f 56 54 41 42 20     case P4_VTAB 
1a161 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  : {.        sqli
1a162 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 28 56  te3VtabUnlock((V
1a163 54 61 62 6c 65 20 2a 29 70 34 29 3b 0a 20 20 20  Table *)p4);.   
1a164 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1a165 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50    }.      case P
1a166 34 5f 53 55 42 50 52 4f 47 52 41 4d 20 3a 20 7b  4_SUBPROGRAM : {
1a167 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1a168 56 64 62 65 50 72 6f 67 72 61 6d 44 65 6c 65 74  VdbeProgramDelet
1a169 65 28 64 62 2c 20 28 53 75 62 50 72 6f 67 72 61  e(db, (SubProgra
1a16a 6d 20 2a 29 70 34 2c 20 31 29 3b 0a 20 20 20 20  m *)p4, 1);.    
1a16b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
1a16c 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a   }.    }.  }.}..
1a16d 2f 2a 0a 2a 2a 20 46 72 65 65 20 74 68 65 20 73  /*.** Free the s
1a16e 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 66  pace allocated f
1a16f 6f 72 20 61 4f 70 20 61 6e 64 20 61 6e 79 20 70  or aOp and any p
1a170 34 20 76 61 6c 75 65 73 20 61 6c 6c 6f 63 61 74  4 values allocat
1a171 65 64 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6f 70  ed for the.** op
1a172 63 6f 64 65 73 20 63 6f 6e 74 61 69 6e 65 64 20  codes contained 
1a173 77 69 74 68 69 6e 2e 20 49 66 20 61 4f 70 20 69  within. If aOp i
1a174 73 20 6e 6f 74 20 4e 55 4c 4c 20 69 74 20 69 73  s not NULL it is
1a175 20 61 73 73 75 6d 65 64 20 74 6f 20 63 6f 6e 74   assumed to cont
1a176 61 69 6e 20 0a 2a 2a 20 6e 4f 70 20 65 6e 74 72  ain .** nOp entr
1a177 69 65 73 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20  ies. .*/.static 
1a178 76 6f 69 64 20 76 64 62 65 46 72 65 65 4f 70 41  void vdbeFreeOpA
1a179 72 72 61 79 28 73 71 6c 69 74 65 33 20 2a 64 62  rray(sqlite3 *db
1a17a 2c 20 4f 70 20 2a 61 4f 70 2c 20 69 6e 74 20 6e  , Op *aOp, int n
1a17b 4f 70 29 7b 0a 20 20 69 66 28 20 61 4f 70 20 29  Op){.  if( aOp )
1a17c 7b 0a 20 20 20 20 4f 70 20 2a 70 4f 70 3b 0a 20  {.    Op *pOp;. 
1a17d 20 20 20 66 6f 72 28 70 4f 70 3d 61 4f 70 3b 20     for(pOp=aOp; 
1a17e 70 4f 70 3c 26 61 4f 70 5b 6e 4f 70 5d 3b 20 70  pOp<&aOp[nOp]; p
1a17f 4f 70 2b 2b 29 7b 0a 20 20 20 20 20 20 66 72 65  Op++){.      fre
1a180 65 50 34 28 64 62 2c 20 70 4f 70 2d 3e 70 34 74  eP4(db, pOp->p4t
1a181 79 70 65 2c 20 70 4f 70 2d 3e 70 34 2e 70 29 3b  ype, pOp->p4.p);
1a182 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
1a183 45 42 55 47 0a 20 20 20 20 20 20 73 71 6c 69 74  EBUG.      sqlit
1a184 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4f 70  e3DbFree(db, pOp
1a185 2d 3e 7a 43 6f 6d 6d 65 6e 74 29 3b 0a 23 65 6e  ->zComment);.#en
1a186 64 69 66 20 20 20 20 20 0a 20 20 20 20 7d 0a 20  dif     .    }. 
1a187 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72   }.  sqlite3DbFr
1a188 65 65 28 64 62 2c 20 61 4f 70 29 3b 0a 7d 0a 0a  ee(db, aOp);.}..
1a189 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20  /*.** Decrement 
1a18a 74 68 65 20 72 65 66 2d 63 6f 75 6e 74 20 6f 6e  the ref-count on
1a18b 20 74 68 65 20 53 75 62 50 72 6f 67 72 61 6d 20   the SubProgram 
1a18c 73 74 72 75 63 74 75 72 65 20 70 61 73 73 65 64  structure passed
1a18d 20 61 73 20 74 68 65 0a 2a 2a 20 73 65 63 6f 6e   as the.** secon
1a18e 64 20 61 72 67 75 6d 65 6e 74 2e 20 49 66 20 74  d argument. If t
1a18f 68 65 20 72 65 66 2d 63 6f 75 6e 74 20 72 65 61  he ref-count rea
1a190 63 68 65 73 20 7a 65 72 6f 2c 20 66 72 65 65 20  ches zero, free 
1a191 74 68 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  the structure..*
1a192 2a 0a 2a 2a 20 54 68 65 20 61 72 72 61 79 20 6f  *.** The array o
1a193 66 20 56 44 42 45 20 6f 70 63 6f 64 65 73 20 73  f VDBE opcodes s
1a194 74 6f 72 65 64 20 61 73 20 53 75 62 50 72 6f 67  tored as SubProg
1a195 72 61 6d 2e 61 4f 70 20 69 73 20 66 72 65 65 64  ram.aOp is freed
1a196 20 69 66 0a 2a 2a 20 65 69 74 68 65 72 20 74 68   if.** either th
1a197 65 20 72 65 66 2d 63 6f 75 6e 74 20 72 65 61 63  e ref-count reac
1a198 68 65 73 20 7a 65 72 6f 20 6f 72 20 70 61 72 61  hes zero or para
1a199 6d 65 74 65 72 20 66 72 65 65 6f 70 20 69 73 20  meter freeop is 
1a19a 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20  non-zero..**.** 
1a19b 53 69 6e 63 65 20 74 68 65 20 61 72 72 61 79 20  Since the array 
1a19c 6f 66 20 6f 70 63 6f 64 65 73 20 70 6f 69 6e 74  of opcodes point
1a19d 65 64 20 74 6f 20 62 79 20 53 75 62 50 72 6f 67  ed to by SubProg
1a19e 72 61 6d 2e 61 4f 70 20 6d 61 79 20 64 69 72 65  ram.aOp may dire
1a19f 63 74 6c 79 0a 2a 2a 20 6f 72 20 69 6e 64 69 72  ctly.** or indir
1a1a0 65 63 74 6c 79 20 63 6f 6e 74 61 69 6e 20 61 20  ectly contain a 
1a1a1 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65  reference to the
1a1a2 20 53 75 62 50 72 6f 67 72 61 6d 20 73 74 72 75   SubProgram stru
1a1a3 63 74 75 72 65 20 69 74 73 65 6c 66 2e 0a 2a 2a  cture itself..**
1a1a4 20 42 79 20 70 61 73 73 69 6e 67 20 61 20 6e 6f   By passing a no
1a1a5 6e 2d 7a 65 72 6f 20 66 72 65 65 6f 70 20 70 61  n-zero freeop pa
1a1a6 72 61 6d 65 74 65 72 2c 20 74 68 65 20 63 61 6c  rameter, the cal
1a1a7 6c 65 72 20 6d 61 79 20 65 6e 73 75 72 65 20 74  ler may ensure t
1a1a8 68 61 74 20 61 6c 6c 0a 2a 2a 20 53 75 62 50 72  hat all.** SubPr
1a1a9 6f 67 72 61 6d 20 73 74 72 75 63 74 75 72 65 73  ogram structures
1a1aa 20 61 6e 64 20 74 68 65 69 72 20 61 4f 70 20 61   and their aOp a
1a1ab 72 72 61 79 73 20 61 72 65 20 66 72 65 65 64 2c  rrays are freed,
1a1ac 20 65 76 65 6e 20 77 68 65 6e 20 74 68 65 72 65   even when there
1a1ad 0a 2a 2a 20 61 72 65 20 73 75 63 68 20 63 69 72  .** are such cir
1a1ae 63 75 6c 61 72 20 72 65 66 65 72 65 6e 63 65 73  cular references
1a1af 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1a1b0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1a1b1 56 64 62 65 50 72 6f 67 72 61 6d 44 65 6c 65 74  VdbeProgramDelet
1a1b2 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53  e(sqlite3 *db, S
1a1b3 75 62 50 72 6f 67 72 61 6d 20 2a 70 2c 20 69 6e  ubProgram *p, in
1a1b4 74 20 66 72 65 65 6f 70 29 7b 0a 20 20 69 66 28  t freeop){.  if(
1a1b5 20 70 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74   p ){.    assert
1a1b6 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20  ( p->nRef>0 );. 
1a1b7 20 20 20 69 66 28 20 66 72 65 65 6f 70 20 7c 7c     if( freeop ||
1a1b8 20 70 2d 3e 6e 52 65 66 3d 3d 31 20 29 7b 0a 20   p->nRef==1 ){. 
1a1b9 20 20 20 20 20 4f 70 20 2a 61 4f 70 20 3d 20 70       Op *aOp = p
1a1ba 2d 3e 61 4f 70 3b 0a 20 20 20 20 20 20 70 2d 3e  ->aOp;.      p->
1a1bb 61 4f 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 76  aOp = 0;.      v
1a1bc 64 62 65 46 72 65 65 4f 70 41 72 72 61 79 28 64  dbeFreeOpArray(d
1a1bd 62 2c 20 61 4f 70 2c 20 70 2d 3e 6e 4f 70 29 3b  b, aOp, p->nOp);
1a1be 0a 20 20 20 20 20 20 70 2d 3e 6e 4f 70 20 3d 20  .      p->nOp = 
1a1bf 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e  0;.    }.    p->
1a1c0 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28 20  nRef--;.    if( 
1a1c1 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20  p->nRef==0 ){.  
1a1c2 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1a1c3 65 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 7d 0a  e(db, p);.    }.
1a1c4 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68    }.}.../*.** Ch
1a1c5 61 6e 67 65 20 4e 20 6f 70 63 6f 64 65 73 20 73  ange N opcodes s
1a1c6 74 61 72 74 69 6e 67 20 61 74 20 61 64 64 72 20  tarting at addr 
1a1c7 74 6f 20 4e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 53 51  to No-ops..*/.SQ
1a1c8 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1a1c9 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61  d sqlite3VdbeCha
1a1ca 6e 67 65 54 6f 4e 6f 6f 70 28 56 64 62 65 20 2a  ngeToNoop(Vdbe *
1a1cb 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74  p, int addr, int
1a1cc 20 4e 29 7b 0a 20 20 69 66 28 20 70 2d 3e 61 4f   N){.  if( p->aO
1a1cd 70 20 29 7b 0a 20 20 20 20 56 64 62 65 4f 70 20  p ){.    VdbeOp 
1a1ce 2a 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 61  *pOp = &p->aOp[a
1a1cf 64 64 72 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65  ddr];.    sqlite
1a1d0 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20  3 *db = p->db;. 
1a1d1 20 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 29 7b     while( N-- ){
1a1d2 0a 20 20 20 20 20 20 66 72 65 65 50 34 28 64 62  .      freeP4(db
1a1d3 2c 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20 70  , pOp->p4type, p
1a1d4 4f 70 2d 3e 70 34 2e 70 29 3b 0a 20 20 20 20 20  Op->p4.p);.     
1a1d5 20 6d 65 6d 73 65 74 28 70 4f 70 2c 20 30 2c 20   memset(pOp, 0, 
1a1d6 73 69 7a 65 6f 66 28 70 4f 70 5b 30 5d 29 29 3b  sizeof(pOp[0]));
1a1d7 0a 20 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f  .      pOp->opco
1a1d8 64 65 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20  de = OP_Noop;.  
1a1d9 20 20 20 20 70 4f 70 2b 2b 3b 0a 20 20 20 20 7d      pOp++;.    }
1a1da 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68  .  }.}../*.** Ch
1a1db 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f  ange the value o
1a1dc 66 20 74 68 65 20 50 34 20 6f 70 65 72 61 6e 64  f the P4 operand
1a1dd 20 66 6f 72 20 61 20 73 70 65 63 69 66 69 63 20   for a specific 
1a1de 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20  instruction..** 
1a1df 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
1a1e0 75 73 65 66 75 6c 20 77 68 65 6e 20 61 20 6c 61  useful when a la
1a1e1 72 67 65 20 70 72 6f 67 72 61 6d 20 69 73 20 6c  rge program is l
1a1e2 6f 61 64 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20  oaded from a.** 
1a1e3 73 74 61 74 69 63 20 61 72 72 61 79 20 75 73 69  static array usi
1a1e4 6e 67 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  ng sqlite3VdbeAd
1a1e5 64 4f 70 4c 69 73 74 20 62 75 74 20 77 65 20 77  dOpList but we w
1a1e6 61 6e 74 20 74 6f 20 6d 61 6b 65 20 61 0a 2a 2a  ant to make a.**
1a1e7 20 66 65 77 20 6d 69 6e 6f 72 20 63 68 61 6e 67   few minor chang
1a1e8 65 73 20 74 6f 20 74 68 65 20 70 72 6f 67 72 61  es to the progra
1a1e9 6d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 3e 3d 30  m..**.** If n>=0
1a1ea 20 74 68 65 6e 20 74 68 65 20 50 34 20 6f 70 65   then the P4 ope
1a1eb 72 61 6e 64 20 69 73 20 64 79 6e 61 6d 69 63 2c  rand is dynamic,
1a1ec 20 6d 65 61 6e 69 6e 67 20 74 68 61 74 20 61 20   meaning that a 
1a1ed 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20 73  copy of.** the s
1a1ee 74 72 69 6e 67 20 69 73 20 6d 61 64 65 20 69 6e  tring is made in
1a1ef 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e  to memory obtain
1a1f0 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f  ed from sqlite3_
1a1f1 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 41 20 76  malloc()..** A v
1a1f2 61 6c 75 65 20 6f 66 20 6e 3d 3d 30 20 6d 65 61  alue of n==0 mea
1a1f3 6e 73 20 63 6f 70 79 20 62 79 74 65 73 20 6f 66  ns copy bytes of
1a1f4 20 7a 50 34 20 75 70 20 74 6f 20 61 6e 64 20 69   zP4 up to and i
1a1f5 6e 63 6c 75 64 69 6e 67 20 74 68 65 0a 2a 2a 20  ncluding the.** 
1a1f6 66 69 72 73 74 20 6e 75 6c 6c 20 62 79 74 65 2e  first null byte.
1a1f7 20 20 49 66 20 6e 3e 30 20 74 68 65 6e 20 63 6f    If n>0 then co
1a1f8 70 79 20 6e 2b 31 20 62 79 74 65 73 20 6f 66 20  py n+1 bytes of 
1a1f9 7a 50 34 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 3d  zP4..**.** If n=
1a1fa 3d 50 34 5f 4b 45 59 49 4e 46 4f 20 69 74 20 6d  =P4_KEYINFO it m
1a1fb 65 61 6e 73 20 74 68 61 74 20 7a 50 34 20 69 73  eans that zP4 is
1a1fc 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
1a1fd 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72  KeyInfo structur
1a1fe 65 2e 0a 2a 2a 20 41 20 63 6f 70 79 20 69 73 20  e..** A copy is 
1a1ff 6d 61 64 65 20 6f 66 20 74 68 65 20 4b 65 79 49  made of the KeyI
1a200 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 69 6e  nfo structure in
1a201 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e  to memory obtain
1a202 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74  ed from.** sqlit
1a203 65 33 5f 6d 61 6c 6c 6f 63 2c 20 74 6f 20 62 65  e3_malloc, to be
1a204 20 66 72 65 65 64 20 77 68 65 6e 20 74 68 65 20   freed when the 
1a205 56 64 62 65 20 69 73 20 66 69 6e 61 6c 69 7a 65  Vdbe is finalize
1a206 64 2e 0a 2a 2a 20 6e 3d 3d 50 34 5f 4b 45 59 49  d..** n==P4_KEYI
1a207 4e 46 4f 5f 48 41 4e 44 4f 46 46 20 69 6e 64 69  NFO_HANDOFF indi
1a208 63 61 74 65 73 20 74 68 61 74 20 7a 50 34 20 70  cates that zP4 p
1a209 6f 69 6e 74 73 20 74 6f 20 61 20 4b 65 79 49 6e  oints to a KeyIn
1a20a 66 6f 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  fo structure.** 
1a20b 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79  stored in memory
1a20c 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65 72   that the caller
1a20d 20 68 61 73 20 6f 62 74 61 69 6e 65 64 20 66 72   has obtained fr
1a20e 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  om sqlite3_mallo
1a20f 63 2e 20 54 68 65 20 0a 2a 2a 20 63 61 6c 6c 65  c. The .** calle
1a210 72 20 73 68 6f 75 6c 64 20 6e 6f 74 20 66 72 65  r should not fre
1a211 65 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  e the allocation
1a212 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 66 72 65  , it will be fre
1a213 65 64 20 77 68 65 6e 20 74 68 65 20 56 64 62 65  ed when the Vdbe
1a214 20 69 73 0a 2a 2a 20 66 69 6e 61 6c 69 7a 65 64   is.** finalized
1a215 2e 0a 2a 2a 20 0a 2a 2a 20 4f 74 68 65 72 20 76  ..** .** Other v
1a216 61 6c 75 65 73 20 6f 66 20 6e 20 28 50 34 5f 53  alues of n (P4_S
1a217 54 41 54 49 43 2c 20 50 34 5f 43 4f 4c 4c 53 45  TATIC, P4_COLLSE
1a218 51 20 65 74 63 2e 29 20 69 6e 64 69 63 61 74 65  Q etc.) indicate
1a219 20 74 68 61 74 20 7a 50 34 20 70 6f 69 6e 74 73   that zP4 points
1a21a 0a 2a 2a 20 74 6f 20 61 20 73 74 72 69 6e 67 20  .** to a string 
1a21b 6f 72 20 73 74 72 75 63 74 75 72 65 20 74 68 61  or structure tha
1a21c 74 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20  t is guaranteed 
1a21d 74 6f 20 65 78 69 73 74 20 66 6f 72 20 74 68 65  to exist for the
1a21e 20 6c 69 66 65 74 69 6d 65 20 6f 66 0a 2a 2a 20   lifetime of.** 
1a21f 74 68 65 20 56 64 62 65 2e 20 49 6e 20 74 68 65  the Vdbe. In the
1a220 73 65 20 63 61 73 65 73 20 77 65 20 63 61 6e 20  se cases we can 
1a221 6a 75 73 74 20 63 6f 70 79 20 74 68 65 20 70 6f  just copy the po
1a222 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  inter..**.** If 
1a223 61 64 64 72 3c 30 20 74 68 65 6e 20 63 68 61 6e  addr<0 then chan
1a224 67 65 20 50 34 20 6f 6e 20 74 68 65 20 6d 6f 73  ge P4 on the mos
1a225 74 20 72 65 63 65 6e 74 6c 79 20 69 6e 73 65 72  t recently inser
1a226 74 65 64 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  ted instruction.
1a227 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1a228 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
1a229 64 62 65 43 68 61 6e 67 65 50 34 28 56 64 62 65  dbeChangeP4(Vdbe
1a22a 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 63   *p, int addr, c
1a22b 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 34 2c 20  onst char *zP4, 
1a22c 69 6e 74 20 6e 29 7b 0a 20 20 4f 70 20 2a 70 4f  int n){.  Op *pO
1a22d 70 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  p;.  sqlite3 *db
1a22e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30  ;.  assert( p!=0
1a22f 20 29 3b 0a 20 20 64 62 20 3d 20 70 2d 3e 64 62   );.  db = p->db
1a230 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d  ;.  assert( p->m
1a231 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43  agic==VDBE_MAGIC
1a232 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 70  _INIT );.  if( p
1a233 2d 3e 61 4f 70 3d 3d 30 20 7c 7c 20 64 62 2d 3e  ->aOp==0 || db->
1a234 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
1a235 20 20 20 20 69 66 20 28 20 6e 21 3d 50 34 5f 4b      if ( n!=P4_K
1a236 45 59 49 4e 46 4f 20 26 26 20 6e 21 3d 50 34 5f  EYINFO && n!=P4_
1a237 56 54 41 42 20 29 20 7b 0a 20 20 20 20 20 20 66  VTAB ) {.      f
1a238 72 65 65 50 34 28 64 62 2c 20 6e 2c 20 28 76 6f  reeP4(db, n, (vo
1a239 69 64 2a 29 2a 28 63 68 61 72 2a 2a 29 26 7a 50  id*)*(char**)&zP
1a23a 34 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65  4);.    }.    re
1a23b 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65  turn;.  }.  asse
1a23c 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 3b 0a  rt( p->nOp>0 );.
1a23d 20 20 61 73 73 65 72 74 28 20 61 64 64 72 3c 70    assert( addr<p
1a23e 2d 3e 6e 4f 70 20 29 3b 0a 20 20 69 66 28 20 61  ->nOp );.  if( a
1a23f 64 64 72 3c 30 20 29 7b 0a 20 20 20 20 61 64 64  ddr<0 ){.    add
1a240 72 20 3d 20 70 2d 3e 6e 4f 70 20 2d 20 31 3b 0a  r = p->nOp - 1;.
1a241 20 20 7d 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e    }.  pOp = &p->
1a242 61 4f 70 5b 61 64 64 72 5d 3b 0a 20 20 66 72 65  aOp[addr];.  fre
1a243 65 50 34 28 64 62 2c 20 70 4f 70 2d 3e 70 34 74  eP4(db, pOp->p4t
1a244 79 70 65 2c 20 70 4f 70 2d 3e 70 34 2e 70 29 3b  ype, pOp->p4.p);
1a245 0a 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 30  .  pOp->p4.p = 0
1a246 3b 0a 20 20 69 66 28 20 6e 3d 3d 50 34 5f 49 4e  ;.  if( n==P4_IN
1a247 54 33 32 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f  T32 ){.    /* No
1a248 74 65 3a 20 74 68 69 73 20 63 61 73 74 20 69 73  te: this cast is
1a249 20 73 61 66 65 2c 20 62 65 63 61 75 73 65 20 74   safe, because t
1a24a 68 65 20 6f 72 69 67 69 6e 20 64 61 74 61 20 70  he origin data p
1a24b 6f 69 6e 74 20 77 61 73 20 61 6e 20 69 6e 74 0a  oint was an int.
1a24c 20 20 20 20 2a 2a 20 74 68 61 74 20 77 61 73 20      ** that was 
1a24d 63 61 73 74 20 74 6f 20 61 20 28 63 6f 6e 73 74  cast to a (const
1a24e 20 63 68 61 72 20 2a 29 2e 20 2a 2f 0a 20 20 20   char *). */.   
1a24f 20 70 4f 70 2d 3e 70 34 2e 69 20 3d 20 53 51 4c   pOp->p4.i = SQL
1a250 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 7a  ITE_PTR_TO_INT(z
1a251 50 34 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34  P4);.    pOp->p4
1a252 74 79 70 65 20 3d 20 50 34 5f 49 4e 54 33 32 3b  type = P4_INT32;
1a253 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 50 34  .  }else if( zP4
1a254 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e  ==0 ){.    pOp->
1a255 70 34 2e 70 20 3d 20 30 3b 0a 20 20 20 20 70 4f  p4.p = 0;.    pO
1a256 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4e  p->p4type = P4_N
1a257 4f 54 55 53 45 44 3b 0a 20 20 7d 65 6c 73 65 20  OTUSED;.  }else 
1a258 69 66 28 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46  if( n==P4_KEYINF
1a259 4f 20 29 7b 0a 20 20 20 20 4b 65 79 49 6e 66 6f  O ){.    KeyInfo
1a25a 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20   *pKeyInfo;.    
1a25b 69 6e 74 20 6e 46 69 65 6c 64 2c 20 6e 42 79 74  int nField, nByt
1a25c 65 3b 0a 0a 20 20 20 20 6e 46 69 65 6c 64 20 3d  e;..    nField =
1a25d 20 28 28 4b 65 79 49 6e 66 6f 2a 29 7a 50 34 29   ((KeyInfo*)zP4)
1a25e 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 20 20 6e 42  ->nField;.    nB
1a25f 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 2a 70 4b  yte = sizeof(*pK
1a260 65 79 49 6e 66 6f 29 20 2b 20 28 6e 46 69 65 6c  eyInfo) + (nFiel
1a261 64 2d 31 29 2a 73 69 7a 65 6f 66 28 70 4b 65 79  d-1)*sizeof(pKey
1a262 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 30 5d 29 20  Info->aColl[0]) 
1a263 2b 20 6e 46 69 65 6c 64 3b 0a 20 20 20 20 70 4b  + nField;.    pK
1a264 65 79 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33  eyInfo = sqlite3
1a265 4d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 20 29 3b  Malloc( nByte );
1a266 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 4b 65  .    pOp->p4.pKe
1a267 79 49 6e 66 6f 20 3d 20 70 4b 65 79 49 6e 66 6f  yInfo = pKeyInfo
1a268 3b 0a 20 20 20 20 69 66 28 20 70 4b 65 79 49 6e  ;.    if( pKeyIn
1a269 66 6f 20 29 7b 0a 20 20 20 20 20 20 75 38 20 2a  fo ){.      u8 *
1a26a 61 53 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20  aSortOrder;.    
1a26b 20 20 6d 65 6d 63 70 79 28 70 4b 65 79 49 6e 66    memcpy(pKeyInf
1a26c 6f 2c 20 7a 50 34 2c 20 6e 42 79 74 65 29 3b 0a  o, zP4, nByte);.
1a26d 20 20 20 20 20 20 61 53 6f 72 74 4f 72 64 65 72        aSortOrder
1a26e 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f   = pKeyInfo->aSo
1a26f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 20 20 69  rtOrder;.      i
1a270 66 28 20 61 53 6f 72 74 4f 72 64 65 72 20 29 7b  f( aSortOrder ){
1a271 0a 20 20 20 20 20 20 20 20 70 4b 65 79 49 6e 66  .        pKeyInf
1a272 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d 20  o->aSortOrder = 
1a273 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29  (unsigned char*)
1a274 26 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c  &pKeyInfo->aColl
1a275 5b 6e 46 69 65 6c 64 5d 3b 0a 20 20 20 20 20 20  [nField];.      
1a276 20 20 6d 65 6d 63 70 79 28 70 4b 65 79 49 6e 66    memcpy(pKeyInf
1a277 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 2c 20 61  o->aSortOrder, a
1a278 53 6f 72 74 4f 72 64 65 72 2c 20 6e 46 69 65 6c  SortOrder, nFiel
1a279 64 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  d);.      }.    
1a27a 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20    pOp->p4type = 
1a27b 50 34 5f 4b 45 59 49 4e 46 4f 3b 0a 20 20 20 20  P4_KEYINFO;.    
1a27c 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 2d 3e  }else{.      p->
1a27d 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1a27e 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 4f 70 2d   = 1;.      pOp-
1a27f 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54  >p4type = P4_NOT
1a280 55 53 45 44 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  USED;.    }.  }e
1a281 6c 73 65 20 69 66 28 20 6e 3d 3d 50 34 5f 4b 45  lse if( n==P4_KE
1a282 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 20 29 7b  YINFO_HANDOFF ){
1a283 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d  .    pOp->p4.p =
1a284 20 28 76 6f 69 64 2a 29 7a 50 34 3b 0a 20 20 20   (void*)zP4;.   
1a285 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50   pOp->p4type = P
1a286 34 5f 4b 45 59 49 4e 46 4f 3b 0a 20 20 7d 65 6c  4_KEYINFO;.  }el
1a287 73 65 20 69 66 28 20 6e 3d 3d 50 34 5f 56 54 41  se if( n==P4_VTA
1a288 42 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 70 34  B ){.    pOp->p4
1a289 2e 70 20 3d 20 28 76 6f 69 64 2a 29 7a 50 34 3b  .p = (void*)zP4;
1a28a 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65  .    pOp->p4type
1a28b 20 3d 20 50 34 5f 56 54 41 42 3b 0a 20 20 20 20   = P4_VTAB;.    
1a28c 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b 28  sqlite3VtabLock(
1a28d 28 56 54 61 62 6c 65 20 2a 29 7a 50 34 29 3b 0a  (VTable *)zP4);.
1a28e 20 20 20 20 61 73 73 65 72 74 28 20 28 28 56 54      assert( ((VT
1a28f 61 62 6c 65 20 2a 29 7a 50 34 29 2d 3e 64 62 3d  able *)zP4)->db=
1a290 3d 70 2d 3e 64 62 20 29 3b 0a 20 20 7d 65 6c 73  =p->db );.  }els
1a291 65 20 69 66 28 20 6e 3c 30 20 29 7b 0a 20 20 20  e if( n<0 ){.   
1a292 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 28 76 6f   pOp->p4.p = (vo
1a293 69 64 2a 29 7a 50 34 3b 0a 20 20 20 20 70 4f 70  id*)zP4;.    pOp
1a294 2d 3e 70 34 74 79 70 65 20 3d 20 28 73 69 67 6e  ->p4type = (sign
1a295 65 64 20 63 68 61 72 29 6e 3b 0a 20 20 7d 65 6c  ed char)n;.  }el
1a296 73 65 7b 0a 20 20 20 20 69 66 28 20 6e 3d 3d 30  se{.    if( n==0
1a297 20 29 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74   ) n = sqlite3St
1a298 72 6c 65 6e 33 30 28 7a 50 34 29 3b 0a 20 20 20  rlen30(zP4);.   
1a299 20 70 4f 70 2d 3e 70 34 2e 7a 20 3d 20 73 71 6c   pOp->p4.z = sql
1a29a 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 70 2d  ite3DbStrNDup(p-
1a29b 3e 64 62 2c 20 7a 50 34 2c 20 6e 29 3b 0a 20 20  >db, zP4, n);.  
1a29c 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20    pOp->p4type = 
1a29d 50 34 5f 44 59 4e 41 4d 49 43 3b 0a 20 20 7d 0a  P4_DYNAMIC;.  }.
1a29e 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  }..#ifndef NDEBU
1a29f 47 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74  G./*.** Change t
1a2a0 68 65 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 74 68  he comment on th
1a2a1 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  e the most recen
1a2a2 74 6c 79 20 63 6f 64 65 64 20 69 6e 73 74 72 75  tly coded instru
1a2a3 63 74 69 6f 6e 2e 20 20 4f 72 0a 2a 2a 20 69 6e  ction.  Or.** in
1a2a4 73 65 72 74 20 61 20 4e 6f 2d 6f 70 20 61 6e 64  sert a No-op and
1a2a5 20 61 64 64 20 74 68 65 20 63 6f 6d 6d 65 6e 74   add the comment
1a2a6 20 74 6f 20 74 68 61 74 20 6e 65 77 20 69 6e 73   to that new ins
1a2a7 74 72 75 63 74 69 6f 6e 2e 20 20 54 68 69 73 0a  truction.  This.
1a2a8 2a 2a 20 6d 61 6b 65 73 20 74 68 65 20 63 6f 64  ** makes the cod
1a2a9 65 20 65 61 73 69 65 72 20 74 6f 20 72 65 61 64  e easier to read
1a2aa 20 64 75 72 69 6e 67 20 64 65 62 75 67 67 69 6e   during debuggin
1a2ab 67 2e 20 20 4e 6f 6e 65 20 6f 66 20 74 68 69 73  g.  None of this
1a2ac 20 68 61 70 70 65 6e 73 0a 2a 2a 20 69 6e 20 61   happens.** in a
1a2ad 20 70 72 6f 64 75 63 74 69 6f 6e 20 62 75 69 6c   production buil
1a2ae 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
1a2af 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1a2b0 33 56 64 62 65 43 6f 6d 6d 65 6e 74 28 56 64 62  3VdbeComment(Vdb
1a2b1 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72  e *p, const char
1a2b2 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b   *zFormat, ...){
1a2b3 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20  .  va_list ap;. 
1a2b4 20 69 66 28 20 21 70 20 29 20 72 65 74 75 72 6e   if( !p ) return
1a2b5 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e  ;.  assert( p->n
1a2b6 4f 70 3e 30 20 7c 7c 20 70 2d 3e 61 4f 70 3d 3d  Op>0 || p->aOp==
1a2b7 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
1a2b8 2d 3e 61 4f 70 3d 3d 30 20 7c 7c 20 70 2d 3e 61  ->aOp==0 || p->a
1a2b9 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f  Op[p->nOp-1].zCo
1a2ba 6d 6d 65 6e 74 3d 3d 30 20 7c 7c 20 70 2d 3e 64  mment==0 || p->d
1a2bb 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1a2bc 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 20  );.  if( p->nOp 
1a2bd 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 2a 70 7a  ){.    char **pz
1a2be 20 3d 20 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f   = &p->aOp[p->nO
1a2bf 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20  p-1].zComment;. 
1a2c0 20 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20     va_start(ap, 
1a2c1 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 20 20 73 71  zFormat);.    sq
1a2c2 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64  lite3DbFree(p->d
1a2c3 62 2c 20 2a 70 7a 29 3b 0a 20 20 20 20 2a 70 7a  b, *pz);.    *pz
1a2c4 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e   = sqlite3VMPrin
1a2c5 74 66 28 70 2d 3e 64 62 2c 20 7a 46 6f 72 6d 61  tf(p->db, zForma
1a2c6 74 2c 20 61 70 29 3b 0a 20 20 20 20 76 61 5f 65  t, ap);.    va_e
1a2c7 6e 64 28 61 70 29 3b 0a 20 20 7d 0a 7d 0a 53 51  nd(ap);.  }.}.SQ
1a2c8 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1a2c9 64 20 73 71 6c 69 74 65 33 56 64 62 65 4e 6f 6f  d sqlite3VdbeNoo
1a2ca 70 43 6f 6d 6d 65 6e 74 28 56 64 62 65 20 2a 70  pComment(Vdbe *p
1a2cb 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  , const char *zF
1a2cc 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76  ormat, ...){.  v
1a2cd 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69 66 28  a_list ap;.  if(
1a2ce 20 21 70 20 29 20 72 65 74 75 72 6e 3b 0a 20 20   !p ) return;.  
1a2cf 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1a2d0 30 28 70 2c 20 4f 50 5f 4e 6f 6f 70 29 3b 0a 20  0(p, OP_Noop);. 
1a2d1 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e   assert( p->nOp>
1a2d2 30 20 7c 7c 20 70 2d 3e 61 4f 70 3d 3d 30 20 29  0 || p->aOp==0 )
1a2d3 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61  ;.  assert( p->a
1a2d4 4f 70 3d 3d 30 20 7c 7c 20 70 2d 3e 61 4f 70 5b  Op==0 || p->aOp[
1a2d5 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65  p->nOp-1].zComme
1a2d6 6e 74 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 2d 3e  nt==0 || p->db->
1a2d7 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a  mallocFailed );.
1a2d8 20 20 69 66 28 20 70 2d 3e 6e 4f 70 20 29 7b 0a    if( p->nOp ){.
1a2d9 20 20 20 20 63 68 61 72 20 2a 2a 70 7a 20 3d 20      char **pz = 
1a2da 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31  &p->aOp[p->nOp-1
1a2db 5d 2e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20  ].zComment;.    
1a2dc 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f  va_start(ap, zFo
1a2dd 72 6d 61 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  rmat);.    sqlit
1a2de 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20  e3DbFree(p->db, 
1a2df 2a 70 7a 29 3b 0a 20 20 20 20 2a 70 7a 20 3d 20  *pz);.    *pz = 
1a2e0 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28  sqlite3VMPrintf(
1a2e1 70 2d 3e 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20  p->db, zFormat, 
1a2e2 61 70 29 3b 0a 20 20 20 20 76 61 5f 65 6e 64 28  ap);.    va_end(
1a2e3 61 70 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69  ap);.  }.}.#endi
1a2e4 66 20 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a  f  /* NDEBUG */.
1a2e5 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
1a2e6 65 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 20 67  e opcode for a g
1a2e7 69 76 65 6e 20 61 64 64 72 65 73 73 2e 20 20 49  iven address.  I
1a2e8 66 20 74 68 65 20 61 64 64 72 65 73 73 20 69 73  f the address is
1a2e9 20 2d 31 2c 20 74 68 65 6e 0a 2a 2a 20 72 65 74   -1, then.** ret
1a2ea 75 72 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63  urn the most rec
1a2eb 65 6e 74 6c 79 20 69 6e 73 65 72 74 65 64 20 6f  ently inserted o
1a2ec 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  pcode..**.** If 
1a2ed 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  a memory allocat
1a2ee 69 6f 6e 20 65 72 72 6f 72 20 68 61 73 20 6f 63  ion error has oc
1a2ef 63 75 72 72 65 64 20 70 72 69 6f 72 20 74 6f 20  curred prior to 
1a2f0 74 68 65 20 63 61 6c 6c 69 6e 67 20 6f 66 20 74  the calling of t
1a2f1 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 2c 20  his.** routine, 
1a2f2 74 68 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  then a pointer t
1a2f3 6f 20 61 20 64 75 6d 6d 79 20 56 64 62 65 4f 70  o a dummy VdbeOp
1a2f4 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65   will be returne
1a2f5 64 2e 20 20 54 68 61 74 20 6f 70 63 6f 64 65 0a  d.  That opcode.
1a2f6 2a 2a 20 69 73 20 72 65 61 64 61 62 6c 65 20 61  ** is readable a
1a2f7 6e 64 20 77 72 69 74 61 62 6c 65 2c 20 62 75 74  nd writable, but
1a2f8 20 69 74 20 68 61 73 20 6e 6f 20 65 66 66 65 63   it has no effec
1a2f9 74 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 6f  t.  The return o
1a2fa 66 20 61 20 64 75 6d 6d 79 0a 2a 2a 20 6f 70 63  f a dummy.** opc
1a2fb 6f 64 65 20 61 6c 6c 6f 77 73 20 74 68 65 20 63  ode allows the c
1a2fc 61 6c 6c 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20  all to continue 
1a2fd 66 75 6e 63 74 69 6f 6e 69 6e 67 20 61 66 74 65  functioning afte
1a2fe 72 20 61 20 4f 4f 4d 20 66 61 75 6c 74 20 77 69  r a OOM fault wi
1a2ff 74 68 6f 75 74 0a 2a 2a 20 68 61 76 69 6e 67 20  thout.** having 
1a300 74 6f 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20  to check to see 
1a301 69 66 20 74 68 65 20 72 65 74 75 72 6e 20 66 72  if the return fr
1a302 6f 6d 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  om this routine 
1a303 69 73 20 61 20 76 61 6c 69 64 20 70 6f 69 6e 74  is a valid point
1a304 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 62 6f 75 74 20  er..**.** About 
1a305 74 68 65 20 23 69 66 64 65 66 20 53 51 4c 49 54  the #ifdef SQLIT
1a306 45 5f 4f 4d 49 54 5f 54 52 41 43 45 3a 20 20 4e  E_OMIT_TRACE:  N
1a307 6f 72 6d 61 6c 6c 79 2c 20 74 68 69 73 20 72 6f  ormally, this ro
1a308 75 74 69 6e 65 20 69 73 20 6e 65 76 65 72 20 63  utine is never c
1a309 61 6c 6c 65 64 0a 2a 2a 20 75 6e 6c 65 73 73 20  alled.** unless 
1a30a 70 2d 3e 6e 4f 70 3e 30 2e 20 20 54 68 69 73 20  p->nOp>0.  This 
1a30b 69 73 20 62 65 63 61 75 73 65 20 69 6e 20 74 68  is because in th
1a30c 65 20 61 62 73 65 6e 73 65 20 6f 66 20 53 51 4c  e absense of SQL
1a30d 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 2c 0a  ITE_OMIT_TRACE,.
1a30e 2a 2a 20 61 6e 20 4f 50 5f 54 72 61 63 65 20 69  ** an OP_Trace i
1a30f 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 61 6c  nstruction is al
1a310 77 61 79 73 20 69 6e 73 65 72 74 65 64 20 62 79  ways inserted by
1a311 20 73 71 6c 69 74 65 33 56 64 62 65 47 65 74 28   sqlite3VdbeGet(
1a312 29 20 61 73 20 73 6f 6f 6e 20 61 73 0a 2a 2a 20  ) as soon as.** 
1a313 61 20 6e 65 77 20 56 44 42 45 20 69 73 20 63 72  a new VDBE is cr
1a314 65 61 74 65 64 2e 20 20 53 6f 20 77 65 20 61 72  eated.  So we ar
1a315 65 20 66 72 65 65 20 74 6f 20 73 65 74 20 61 64  e free to set ad
1a316 64 72 20 74 6f 20 70 2d 3e 6e 4f 70 2d 31 20 77  dr to p->nOp-1 w
1a317 69 74 68 6f 75 74 0a 2a 2a 20 68 61 76 69 6e 67  ithout.** having
1a318 20 74 6f 20 64 6f 75 62 6c 65 2d 63 68 65 63 6b   to double-check
1a319 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
1a31a 61 74 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  at the result is
1a31b 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 20 42   non-negative. B
1a31c 75 74 0a 2a 2a 20 69 66 20 53 51 4c 49 54 45 5f  ut.** if SQLITE_
1a31d 4f 4d 49 54 5f 54 52 41 43 45 20 69 73 20 64 65  OMIT_TRACE is de
1a31e 66 69 6e 65 64 2c 20 74 68 65 20 4f 50 5f 54 72  fined, the OP_Tr
1a31f 61 63 65 20 69 73 20 6f 6d 69 74 74 65 64 20 61  ace is omitted a
1a320 6e 64 20 77 65 20 64 6f 20 6e 65 65 64 20 74 6f  nd we do need to
1a321 0a 2a 2a 20 63 68 65 63 6b 20 74 68 65 20 76 61  .** check the va
1a322 6c 75 65 20 6f 66 20 70 2d 3e 6e 4f 70 2d 31 20  lue of p->nOp-1 
1a323 62 65 66 6f 72 65 20 63 6f 6e 74 69 6e 75 69 6e  before continuin
1a324 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  g..*/.SQLITE_PRI
1a325 56 41 54 45 20 56 64 62 65 4f 70 20 2a 73 71 6c  VATE VdbeOp *sql
1a326 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 56 64  ite3VdbeGetOp(Vd
1a327 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 29  be *p, int addr)
1a328 7b 0a 20 20 73 74 61 74 69 63 20 56 64 62 65 4f  {.  static VdbeO
1a329 70 20 64 75 6d 6d 79 3b 0a 20 20 61 73 73 65 72  p dummy;.  asser
1a32a 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42  t( p->magic==VDB
1a32b 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a  E_MAGIC_INIT );.
1a32c 20 20 69 66 28 20 61 64 64 72 3c 30 20 29 7b 0a    if( addr<0 ){.
1a32d 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
1a32e 49 54 5f 54 52 41 43 45 0a 20 20 20 20 69 66 28  IT_TRACE.    if(
1a32f 20 70 2d 3e 6e 4f 70 3d 3d 30 20 29 20 72 65 74   p->nOp==0 ) ret
1a330 75 72 6e 20 26 64 75 6d 6d 79 3b 0a 23 65 6e 64  urn &dummy;.#end
1a331 69 66 0a 20 20 20 20 61 64 64 72 20 3d 20 70 2d  if.    addr = p-
1a332 3e 6e 4f 70 20 2d 20 31 3b 0a 20 20 7d 0a 20 20  >nOp - 1;.  }.  
1a333 61 73 73 65 72 74 28 20 28 61 64 64 72 3e 3d 30  assert( (addr>=0
1a334 20 26 26 20 61 64 64 72 3c 70 2d 3e 6e 4f 70 29   && addr<p->nOp)
1a335 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f   || p->db->mallo
1a336 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28  cFailed );.  if(
1a337 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   p->db->mallocFa
1a338 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75  iled ){.    retu
1a339 72 6e 20 26 64 75 6d 6d 79 3b 0a 20 20 7d 65 6c  rn &dummy;.  }el
1a33a 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26  se{.    return &
1a33b 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 3b 0a 20 20  p->aOp[addr];.  
1a33c 7d 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65  }.}..#if !define
1a33d 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58  d(SQLITE_OMIT_EX
1a33e 50 4c 41 49 4e 29 20 7c 7c 20 21 64 65 66 69 6e  PLAIN) || !defin
1a33f 65 64 28 4e 44 45 42 55 47 29 20 5c 0a 20 20 20  ed(NDEBUG) \.   
1a340 20 20 7c 7c 20 64 65 66 69 6e 65 64 28 56 44 42    || defined(VDB
1a341 45 5f 50 52 4f 46 49 4c 45 29 20 7c 7c 20 64 65  E_PROFILE) || de
1a342 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42  fined(SQLITE_DEB
1a343 55 47 29 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74  UG)./*.** Comput
1a344 65 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20  e a string that 
1a345 64 65 73 63 72 69 62 65 73 20 74 68 65 20 50 34  describes the P4
1a346 20 70 61 72 61 6d 65 74 65 72 20 66 6f 72 20 61   parameter for a
1a347 6e 20 6f 70 63 6f 64 65 2e 0a 2a 2a 20 55 73 65  n opcode..** Use
1a348 20 7a 54 65 6d 70 20 66 6f 72 20 61 6e 79 20 72   zTemp for any r
1a349 65 71 75 69 72 65 64 20 74 65 6d 70 6f 72 61 72  equired temporar
1a34a 79 20 62 75 66 66 65 72 20 73 70 61 63 65 2e 0a  y buffer space..
1a34b 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a  */.static char *
1a34c 64 69 73 70 6c 61 79 50 34 28 4f 70 20 2a 70 4f  displayP4(Op *pO
1a34d 70 2c 20 63 68 61 72 20 2a 7a 54 65 6d 70 2c 20  p, char *zTemp, 
1a34e 69 6e 74 20 6e 54 65 6d 70 29 7b 0a 20 20 63 68  int nTemp){.  ch
1a34f 61 72 20 2a 7a 50 34 20 3d 20 7a 54 65 6d 70 3b  ar *zP4 = zTemp;
1a350 0a 20 20 61 73 73 65 72 74 28 20 6e 54 65 6d 70  .  assert( nTemp
1a351 3e 3d 32 30 20 29 3b 0a 20 20 73 77 69 74 63 68  >=20 );.  switch
1a352 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 29 7b  ( pOp->p4type ){
1a353 0a 20 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59  .    case P4_KEY
1a354 49 4e 46 4f 5f 53 54 41 54 49 43 3a 0a 20 20 20  INFO_STATIC:.   
1a355 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e 46 4f   case P4_KEYINFO
1a356 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 2c  : {.      int i,
1a357 20 6a 3b 0a 20 20 20 20 20 20 4b 65 79 49 6e 66   j;.      KeyInf
1a358 6f 20 2a 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f  o *pKeyInfo = pO
1a359 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a  p->p4.pKeyInfo;.
1a35a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
1a35b 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54  printf(nTemp, zT
1a35c 65 6d 70 2c 20 22 6b 65 79 69 6e 66 6f 28 25 64  emp, "keyinfo(%d
1a35d 22 2c 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69  ", pKeyInfo->nFi
1a35e 65 6c 64 29 3b 0a 20 20 20 20 20 20 69 20 3d 20  eld);.      i = 
1a35f 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
1a360 7a 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 66 6f  zTemp);.      fo
1a361 72 28 6a 3d 30 3b 20 6a 3c 70 4b 65 79 49 6e 66  r(j=0; j<pKeyInf
1a362 6f 2d 3e 6e 46 69 65 6c 64 3b 20 6a 2b 2b 29 7b  o->nField; j++){
1a363 0a 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71  .        CollSeq
1a364 20 2a 70 43 6f 6c 6c 20 3d 20 70 4b 65 79 49 6e   *pColl = pKeyIn
1a365 66 6f 2d 3e 61 43 6f 6c 6c 5b 6a 5d 3b 0a 20 20  fo->aColl[j];.  
1a366 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20        if( pColl 
1a367 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74  ){.          int
1a368 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c   n = sqlite3Strl
1a369 65 6e 33 30 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d  en30(pColl->zNam
1a36a 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  e);.          if
1a36b 28 20 69 2b 6e 3e 6e 54 65 6d 70 2d 36 20 29 7b  ( i+n>nTemp-6 ){
1a36c 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d  .            mem
1a36d 63 70 79 28 26 7a 54 65 6d 70 5b 69 5d 2c 22 2c  cpy(&zTemp[i],",
1a36e 2e 2e 2e 22 2c 34 29 3b 0a 20 20 20 20 20 20 20  ...",4);.       
1a36f 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1a370 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1a371 20 20 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20 27    zTemp[i++] = '
1a372 2c 27 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  ,';.          if
1a373 28 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72  ( pKeyInfo->aSor
1a374 74 4f 72 64 65 72 20 26 26 20 70 4b 65 79 49 6e  tOrder && pKeyIn
1a375 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 6a  fo->aSortOrder[j
1a376 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ] ){.           
1a377 20 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20 27 2d   zTemp[i++] = '-
1a378 27 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  ';.          }. 
1a379 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28           memcpy(
1a37a 26 7a 54 65 6d 70 5b 69 5d 2c 20 70 43 6f 6c 6c  &zTemp[i], pColl
1a37b 2d 3e 7a 4e 61 6d 65 2c 6e 2b 31 29 3b 0a 20 20  ->zName,n+1);.  
1a37c 20 20 20 20 20 20 20 20 69 20 2b 3d 20 6e 3b 0a          i += n;.
1a37d 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66          }else if
1a37e 28 20 69 2b 34 3c 6e 54 65 6d 70 2d 36 20 29 7b  ( i+4<nTemp-6 ){
1a37f 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70  .          memcp
1a380 79 28 26 7a 54 65 6d 70 5b 69 5d 2c 22 2c 6e 69  y(&zTemp[i],",ni
1a381 6c 22 2c 34 29 3b 0a 20 20 20 20 20 20 20 20 20  l",4);.         
1a382 20 69 20 2b 3d 20 34 3b 0a 20 20 20 20 20 20 20   i += 4;.       
1a383 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
1a384 20 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20 27 29   zTemp[i++] = ')
1a385 27 3b 0a 20 20 20 20 20 20 7a 54 65 6d 70 5b 69  ';.      zTemp[i
1a386 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 61 73 73  ] = 0;.      ass
1a387 65 72 74 28 20 69 3c 6e 54 65 6d 70 20 29 3b 0a  ert( i<nTemp );.
1a388 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1a389 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 43   }.    case P4_C
1a38a 4f 4c 4c 53 45 51 3a 20 7b 0a 20 20 20 20 20 20  OLLSEQ: {.      
1a38b 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d  CollSeq *pColl =
1a38c 20 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c 3b 0a   pOp->p4.pColl;.
1a38d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
1a38e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54  printf(nTemp, zT
1a38f 65 6d 70 2c 20 22 63 6f 6c 6c 73 65 71 28 25 2e  emp, "collseq(%.
1a390 32 30 73 29 22 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e  20s)", pColl->zN
1a391 61 6d 65 29 3b 0a 20 20 20 20 20 20 62 72 65 61  ame);.      brea
1a392 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
1a393 65 20 50 34 5f 46 55 4e 43 44 45 46 3a 20 7b 0a  e P4_FUNCDEF: {.
1a394 20 20 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70        FuncDef *p
1a395 44 65 66 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 46  Def = pOp->p4.pF
1a396 75 6e 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  unc;.      sqlit
1a397 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d  e3_snprintf(nTem
1a398 70 2c 20 7a 54 65 6d 70 2c 20 22 25 73 28 25 64  p, zTemp, "%s(%d
1a399 29 22 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c  )", pDef->zName,
1a39a 20 70 44 65 66 2d 3e 6e 41 72 67 29 3b 0a 20 20   pDef->nArg);.  
1a39b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1a39c 0a 20 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54  .    case P4_INT
1a39d 36 34 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69  64: {.      sqli
1a39e 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65  te3_snprintf(nTe
1a39f 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 6c 6c 64  mp, zTemp, "%lld
1a3a0 22 2c 20 2a 70 4f 70 2d 3e 70 34 2e 70 49 36 34  ", *pOp->p4.pI64
1a3a1 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
1a3a2 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50      }.    case P
1a3a3 34 5f 49 4e 54 33 32 3a 20 7b 0a 20 20 20 20 20  4_INT32: {.     
1a3a4 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
1a3a5 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20  f(nTemp, zTemp, 
1a3a6 22 25 64 22 2c 20 70 4f 70 2d 3e 70 34 2e 69 29  "%d", pOp->p4.i)
1a3a7 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
1a3a8 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34     }.    case P4
1a3a9 5f 52 45 41 4c 3a 20 7b 0a 20 20 20 20 20 20 73  _REAL: {.      s
1a3aa 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
1a3ab 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25  nTemp, zTemp, "%
1a3ac 2e 31 36 67 22 2c 20 2a 70 4f 70 2d 3e 70 34 2e  .16g", *pOp->p4.
1a3ad 70 52 65 61 6c 29 3b 0a 20 20 20 20 20 20 62 72  pReal);.      br
1a3ae 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
1a3af 61 73 65 20 50 34 5f 4d 45 4d 3a 20 7b 0a 20 20  ase P4_MEM: {.  
1a3b0 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20      Mem *pMem = 
1a3b1 70 4f 70 2d 3e 70 34 2e 70 4d 65 6d 3b 0a 20 20  pOp->p4.pMem;.  
1a3b2 20 20 20 20 61 73 73 65 72 74 28 20 28 70 4d 65      assert( (pMe
1a3b3 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e  m->flags & MEM_N
1a3b4 75 6c 6c 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20  ull)==0 );.     
1a3b5 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73   if( pMem->flags
1a3b6 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20   & MEM_Str ){.  
1a3b7 20 20 20 20 20 20 7a 50 34 20 3d 20 70 4d 65 6d        zP4 = pMem
1a3b8 2d 3e 7a 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  ->z;.      }else
1a3b9 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73   if( pMem->flags
1a3ba 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20   & MEM_Int ){.  
1a3bb 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
1a3bc 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54  printf(nTemp, zT
1a3bd 65 6d 70 2c 20 22 25 6c 6c 64 22 2c 20 70 4d 65  emp, "%lld", pMe
1a3be 6d 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 20 20 7d  m->u.i);.      }
1a3bf 65 6c 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e 66  else if( pMem->f
1a3c0 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20  lags & MEM_Real 
1a3c1 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1a3c2 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d  e3_snprintf(nTem
1a3c3 70 2c 20 7a 54 65 6d 70 2c 20 22 25 2e 31 36 67  p, zTemp, "%.16g
1a3c4 22 2c 20 70 4d 65 6d 2d 3e 72 29 3b 0a 20 20 20  ", pMem->r);.   
1a3c5 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1a3c6 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e    assert( pMem->
1a3c7 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62  flags & MEM_Blob
1a3c8 20 29 3b 0a 20 20 20 20 20 20 20 20 7a 50 34 20   );.        zP4 
1a3c9 3d 20 22 28 62 6c 6f 62 29 22 3b 0a 20 20 20 20  = "(blob)";.    
1a3ca 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b    }.      break;
1a3cb 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  .    }.#ifndef S
1a3cc 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
1a3cd 41 4c 54 41 42 4c 45 0a 20 20 20 20 63 61 73 65  ALTABLE.    case
1a3ce 20 50 34 5f 56 54 41 42 3a 20 7b 0a 20 20 20 20   P4_VTAB: {.    
1a3cf 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
1a3d0 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e  pVtab = pOp->p4.
1a3d1 70 56 74 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20  pVtab->pVtab;.  
1a3d2 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
1a3d3 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d  intf(nTemp, zTem
1a3d4 70 2c 20 22 76 74 61 62 3a 25 70 3a 25 70 22 2c  p, "vtab:%p:%p",
1a3d5 20 70 56 74 61 62 2c 20 70 56 74 61 62 2d 3e 70   pVtab, pVtab->p
1a3d6 4d 6f 64 75 6c 65 29 3b 0a 20 20 20 20 20 20 62  Module);.      b
1a3d7 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  reak;.    }.#end
1a3d8 69 66 0a 20 20 20 20 63 61 73 65 20 50 34 5f 49  if.    case P4_I
1a3d9 4e 54 41 52 52 41 59 3a 20 7b 0a 20 20 20 20 20  NTARRAY: {.     
1a3da 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
1a3db 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20  f(nTemp, zTemp, 
1a3dc 22 69 6e 74 61 72 72 61 79 22 29 3b 0a 20 20 20  "intarray");.   
1a3dd 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1a3de 20 20 20 20 63 61 73 65 20 50 34 5f 53 55 42 50      case P4_SUBP
1a3df 52 4f 47 52 41 4d 3a 20 7b 0a 20 20 20 20 20 20  ROGRAM: {.      
1a3e0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1a3e1 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22  (nTemp, zTemp, "
1a3e2 70 72 6f 67 72 61 6d 22 29 3b 0a 20 20 20 20 20  program");.     
1a3e3 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
1a3e4 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
1a3e5 20 20 20 7a 50 34 20 3d 20 70 4f 70 2d 3e 70 34     zP4 = pOp->p4
1a3e6 2e 7a 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 50  .z;.      if( zP
1a3e7 34 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  4==0 ){.        
1a3e8 7a 50 34 20 3d 20 7a 54 65 6d 70 3b 0a 20 20 20  zP4 = zTemp;.   
1a3e9 20 20 20 20 20 7a 54 65 6d 70 5b 30 5d 20 3d 20       zTemp[0] = 
1a3ea 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  0;.      }.    }
1a3eb 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 7a  .  }.  assert( z
1a3ec 50 34 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72  P4!=0 );.  retur
1a3ed 6e 20 7a 50 34 3b 0a 7d 0a 23 65 6e 64 69 66 0a  n zP4;.}.#endif.
1a3ee 0a 2f 2a 0a 2a 2a 20 44 65 63 6c 61 72 65 20 74  ./*.** Declare t
1a3ef 6f 20 74 68 65 20 56 64 62 65 20 74 68 61 74 20  o the Vdbe that 
1a3f0 74 68 65 20 42 54 72 65 65 20 6f 62 6a 65 63 74  the BTree object
1a3f1 20 61 74 20 64 62 2d 3e 61 44 62 5b 69 5d 20 69   at db->aDb[i] i
1a3f2 73 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  s used..*/.SQLIT
1a3f3 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1a3f4 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 74  qlite3VdbeUsesBt
1a3f5 72 65 65 28 56 64 62 65 20 2a 70 2c 20 69 6e 74  ree(Vdbe *p, int
1a3f6 20 69 29 7b 0a 20 20 69 6e 74 20 6d 61 73 6b 3b   i){.  int mask;
1a3f7 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20  .  assert( i>=0 
1a3f8 26 26 20 69 3c 70 2d 3e 64 62 2d 3e 6e 44 62 20  && i<p->db->nDb 
1a3f9 26 26 20 69 3c 73 69 7a 65 6f 66 28 75 33 32 29  && i<sizeof(u32)
1a3fa 2a 38 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  *8 );.  assert( 
1a3fb 69 3c 28 69 6e 74 29 73 69 7a 65 6f 66 28 70 2d  i<(int)sizeof(p-
1a3fc 3e 62 74 72 65 65 4d 61 73 6b 29 2a 38 20 29 3b  >btreeMask)*8 );
1a3fd 0a 20 20 6d 61 73 6b 20 3d 20 28 28 75 33 32 29  .  mask = ((u32)
1a3fe 31 29 3c 3c 69 3b 0a 20 20 69 66 28 20 28 70 2d  1)<<i;.  if( (p-
1a3ff 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 6d 61 73  >btreeMask & mas
1a400 6b 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e  k)==0 ){.    p->
1a401 62 74 72 65 65 4d 61 73 6b 20 7c 3d 20 6d 61 73  btreeMask |= mas
1a402 6b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  k;.    sqlite3Bt
1a403 72 65 65 4d 75 74 65 78 41 72 72 61 79 49 6e 73  reeMutexArrayIns
1a404 65 72 74 28 26 70 2d 3e 61 4d 75 74 65 78 2c 20  ert(&p->aMutex, 
1a405 70 2d 3e 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42  p->db->aDb[i].pB
1a406 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 23 69 66 20  t);.  }.}...#if 
1a407 64 65 66 69 6e 65 64 28 56 44 42 45 5f 50 52 4f  defined(VDBE_PRO
1a408 46 49 4c 45 29 20 7c 7c 20 64 65 66 69 6e 65 64  FILE) || defined
1a409 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f  (SQLITE_DEBUG)./
1a40a 2a 0a 2a 2a 20 50 72 69 6e 74 20 61 20 73 69 6e  *.** Print a sin
1a40b 67 6c 65 20 6f 70 63 6f 64 65 2e 20 20 54 68 69  gle opcode.  Thi
1a40c 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65  s routine is use
1a40d 64 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67 20  d for debugging 
1a40e 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  only..*/.SQLITE_
1a40f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1a410 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28  ite3VdbePrintOp(
1a411 46 49 4c 45 20 2a 70 4f 75 74 2c 20 69 6e 74 20  FILE *pOut, int 
1a412 70 63 2c 20 4f 70 20 2a 70 4f 70 29 7b 0a 20 20  pc, Op *pOp){.  
1a413 63 68 61 72 20 2a 7a 50 34 3b 0a 20 20 63 68 61  char *zP4;.  cha
1a414 72 20 7a 50 74 72 5b 35 30 5d 3b 0a 20 20 73 74  r zPtr[50];.  st
1a415 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
1a416 2a 7a 46 6f 72 6d 61 74 31 20 3d 20 22 25 34 64  *zFormat1 = "%4d
1a417 20 25 2d 31 33 73 20 25 34 64 20 25 34 64 20 25   %-13s %4d %4d %
1a418 34 64 20 25 2d 34 73 20 25 2e 32 58 20 25 73 5c  4d %-4s %.2X %s\
1a419 6e 22 3b 0a 20 20 69 66 28 20 70 4f 75 74 3d 3d  n";.  if( pOut==
1a41a 30 20 29 20 70 4f 75 74 20 3d 20 73 74 64 6f 75  0 ) pOut = stdou
1a41b 74 3b 0a 20 20 7a 50 34 20 3d 20 64 69 73 70 6c  t;.  zP4 = displ
1a41c 61 79 50 34 28 70 4f 70 2c 20 7a 50 74 72 2c 20  ayP4(pOp, zPtr, 
1a41d 73 69 7a 65 6f 66 28 7a 50 74 72 29 29 3b 0a 20  sizeof(zPtr));. 
1a41e 20 66 70 72 69 6e 74 66 28 70 4f 75 74 2c 20 7a   fprintf(pOut, z
1a41f 46 6f 72 6d 61 74 31 2c 20 70 63 2c 20 0a 20 20  Format1, pc, .  
1a420 20 20 20 20 73 71 6c 69 74 65 33 4f 70 63 6f 64      sqlite3Opcod
1a421 65 4e 61 6d 65 28 70 4f 70 2d 3e 6f 70 63 6f 64  eName(pOp->opcod
1a422 65 29 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70  e), pOp->p1, pOp
1a423 2d 3e 70 32 2c 20 70 4f 70 2d 3e 70 33 2c 20 7a  ->p2, pOp->p3, z
1a424 50 34 2c 20 70 4f 70 2d 3e 70 35 2c 0a 23 69 66  P4, pOp->p5,.#if
1a425 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
1a426 0a 20 20 20 20 20 20 70 4f 70 2d 3e 7a 43 6f 6d  .      pOp->zCom
1a427 6d 65 6e 74 20 3f 20 70 4f 70 2d 3e 7a 43 6f 6d  ment ? pOp->zCom
1a428 6d 65 6e 74 20 3a 20 22 22 0a 23 65 6c 73 65 0a  ment : "".#else.
1a429 20 20 20 20 20 20 22 22 0a 23 65 6e 64 69 66 0a        "".#endif.
1a42a 20 20 29 3b 0a 20 20 66 66 6c 75 73 68 28 70 4f    );.  fflush(pO
1a42b 75 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  ut);.}.#endif../
1a42c 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 6e 20  *.** Release an 
1a42d 61 72 72 61 79 20 6f 66 20 4e 20 4d 65 6d 20 65  array of N Mem e
1a42e 6c 65 6d 65 6e 74 73 0a 2a 2f 0a 73 74 61 74 69  lements.*/.stati
1a42f 63 20 76 6f 69 64 20 72 65 6c 65 61 73 65 4d 65  c void releaseMe
1a430 6d 41 72 72 61 79 28 4d 65 6d 20 2a 70 2c 20 69  mArray(Mem *p, i
1a431 6e 74 20 4e 29 7b 0a 20 20 69 66 28 20 70 20 26  nt N){.  if( p &
1a432 26 20 4e 20 29 7b 0a 20 20 20 20 4d 65 6d 20 2a  & N ){.    Mem *
1a433 70 45 6e 64 3b 0a 20 20 20 20 73 71 6c 69 74 65  pEnd;.    sqlite
1a434 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20  3 *db = p->db;. 
1a435 20 20 20 75 38 20 6d 61 6c 6c 6f 63 5f 66 61 69     u8 malloc_fai
1a436 6c 65 64 20 3d 20 64 62 2d 3e 6d 61 6c 6c 6f 63  led = db->malloc
1a437 46 61 69 6c 65 64 3b 0a 20 20 20 20 66 6f 72 28  Failed;.    for(
1a438 70 45 6e 64 3d 26 70 5b 4e 5d 3b 20 70 3c 70 45  pEnd=&p[N]; p<pE
1a439 6e 64 3b 20 70 2b 2b 29 7b 0a 20 20 20 20 20 20  nd; p++){.      
1a43a 61 73 73 65 72 74 28 20 28 26 70 5b 31 5d 29 3d  assert( (&p[1])=
1a43b 3d 70 45 6e 64 20 7c 7c 20 70 5b 30 5d 2e 64 62  =pEnd || p[0].db
1a43c 3d 3d 70 5b 31 5d 2e 64 62 20 29 3b 0a 0a 20 20  ==p[1].db );..  
1a43d 20 20 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63      /* This bloc
1a43e 6b 20 69 73 20 72 65 61 6c 6c 79 20 61 6e 20 69  k is really an i
1a43f 6e 6c 69 6e 65 64 20 76 65 72 73 69 6f 6e 20 6f  nlined version o
1a440 66 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  f sqlite3VdbeMem
1a441 52 65 6c 65 61 73 65 28 29 0a 20 20 20 20 20 20  Release().      
1a442 2a 2a 20 74 68 61 74 20 74 61 6b 65 73 20 61 64  ** that takes ad
1a443 76 61 6e 74 61 67 65 20 6f 66 20 74 68 65 20 66  vantage of the f
1a444 61 63 74 20 74 68 61 74 20 74 68 65 20 6d 65 6d  act that the mem
1a445 6f 72 79 20 63 65 6c 6c 20 76 61 6c 75 65 20 69  ory cell value i
1a446 73 20 0a 20 20 20 20 20 20 2a 2a 20 62 65 69 6e  s .      ** bein
1a447 67 20 73 65 74 20 74 6f 20 4e 55 4c 4c 20 61 66  g set to NULL af
1a448 74 65 72 20 72 65 6c 65 61 73 69 6e 67 20 61 6e  ter releasing an
1a449 79 20 64 79 6e 61 6d 69 63 20 72 65 73 6f 75 72  y dynamic resour
1a44a 63 65 73 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20  ces..      **.  
1a44b 20 20 20 20 2a 2a 20 54 68 65 20 6a 75 73 74 69      ** The justi
1a44c 66 69 63 61 74 69 6f 6e 20 66 6f 72 20 64 75 70  fication for dup
1a44d 6c 69 63 61 74 69 6e 67 20 63 6f 64 65 20 69 73  licating code is
1a44e 20 74 68 61 74 20 61 63 63 6f 72 64 69 6e 67 20   that according 
1a44f 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 63 61 6c  to .      ** cal
1a450 6c 67 72 69 6e 64 2c 20 74 68 69 73 20 63 61 75  lgrind, this cau
1a451 73 65 73 20 61 20 63 65 72 74 61 69 6e 20 74 65  ses a certain te
1a452 73 74 20 63 61 73 65 20 74 6f 20 68 69 74 20 74  st case to hit t
1a453 68 65 20 43 50 55 20 34 2e 37 20 0a 20 20 20 20  he CPU 4.7 .    
1a454 20 20 2a 2a 20 70 65 72 63 65 6e 74 20 6c 65 73    ** percent les
1a455 73 20 28 78 38 36 20 6c 69 6e 75 78 2c 20 67 63  s (x86 linux, gc
1a456 63 20 76 65 72 73 69 6f 6e 20 34 2e 31 2e 32 2c  c version 4.1.2,
1a457 20 2d 4f 36 29 20 74 68 61 6e 20 69 66 20 0a 20   -O6) than if . 
1a458 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d       ** sqlite3M
1a459 65 6d 52 65 6c 65 61 73 65 28 29 20 77 65 72 65  emRelease() were
1a45a 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 68 65 72   called from her
1a45b 65 2e 20 57 69 74 68 20 2d 4f 32 2c 20 74 68 69  e. With -O2, thi
1a45c 73 20 6a 75 6d 70 73 0a 20 20 20 20 20 20 2a 2a  s jumps.      **
1a45d 20 74 6f 20 36 2e 36 20 70 65 72 63 65 6e 74 2e   to 6.6 percent.
1a45e 20 54 68 65 20 74 65 73 74 20 63 61 73 65 20 69   The test case i
1a45f 73 20 69 6e 73 65 72 74 69 6e 67 20 31 30 30 30  s inserting 1000
1a460 20 72 6f 77 73 20 69 6e 74 6f 20 61 20 74 61 62   rows into a tab
1a461 6c 65 20 0a 20 20 20 20 20 20 2a 2a 20 77 69 74  le .      ** wit
1a462 68 20 6e 6f 20 69 6e 64 65 78 65 73 20 75 73 69  h no indexes usi
1a463 6e 67 20 61 20 73 69 6e 67 6c 65 20 70 72 65 70  ng a single prep
1a464 61 72 65 64 20 49 4e 53 45 52 54 20 73 74 61 74  ared INSERT stat
1a465 65 6d 65 6e 74 2c 20 62 69 6e 64 28 29 20 0a 20  ement, bind() . 
1a466 20 20 20 20 20 2a 2a 20 61 6e 64 20 72 65 73 65       ** and rese
1a467 74 28 29 2e 20 49 6e 73 65 72 74 73 20 61 72 65  t(). Inserts are
1a468 20 67 72 6f 75 70 65 64 20 69 6e 74 6f 20 61 20   grouped into a 
1a469 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 20  transaction..   
1a46a 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20     */.      if( 
1a46b 70 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 41 67  p->flags&(MEM_Ag
1a46c 67 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 46 72  g|MEM_Dyn|MEM_Fr
1a46d 61 6d 65 7c 4d 45 4d 5f 52 6f 77 53 65 74 29 20  ame|MEM_RowSet) 
1a46e 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1a46f 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
1a470 28 70 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  (p);.      }else
1a471 20 69 66 28 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20   if( p->zMalloc 
1a472 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1a473 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e  e3DbFree(db, p->
1a474 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20  zMalloc);.      
1a475 20 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30    p->zMalloc = 0
1a476 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
1a477 20 70 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f   p->flags = MEM_
1a478 4e 75 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Null;.    }.    
1a479 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1a47a 20 3d 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64   = malloc_failed
1a47b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  ;.  }.}../*.** D
1a47c 65 6c 65 74 65 20 61 20 56 64 62 65 46 72 61 6d  elete a VdbeFram
1a47d 65 20 6f 62 6a 65 63 74 20 61 6e 64 20 69 74 73  e object and its
1a47e 20 63 6f 6e 74 65 6e 74 73 2e 20 56 64 62 65 46   contents. VdbeF
1a47f 72 61 6d 65 20 6f 62 6a 65 63 74 73 20 61 72 65  rame objects are
1a480 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 62 79  .** allocated by
1a481 20 74 68 65 20 4f 50 5f 50 72 6f 67 72 61 6d 20   the OP_Program 
1a482 6f 70 63 6f 64 65 20 69 6e 20 73 71 6c 69 74 65  opcode in sqlite
1a483 33 56 64 62 65 45 78 65 63 28 29 2e 0a 2a 2f 0a  3VdbeExec()..*/.
1a484 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1a485 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 46  oid sqlite3VdbeF
1a486 72 61 6d 65 44 65 6c 65 74 65 28 56 64 62 65 46  rameDelete(VdbeF
1a487 72 61 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  rame *p){.  int 
1a488 69 3b 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 20 3d  i;.  Mem *aMem =
1a489 20 56 64 62 65 46 72 61 6d 65 4d 65 6d 28 70 29   VdbeFrameMem(p)
1a48a 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  ;.  VdbeCursor *
1a48b 2a 61 70 43 73 72 20 3d 20 28 56 64 62 65 43 75  *apCsr = (VdbeCu
1a48c 72 73 6f 72 20 2a 2a 29 26 61 4d 65 6d 5b 70 2d  rsor **)&aMem[p-
1a48d 3e 6e 43 68 69 6c 64 4d 65 6d 5d 3b 0a 20 20 66  >nChildMem];.  f
1a48e 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 68  or(i=0; i<p->nCh
1a48f 69 6c 64 43 73 72 3b 20 69 2b 2b 29 7b 0a 20 20  ildCsr; i++){.  
1a490 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 65    sqlite3VdbeFre
1a491 65 43 75 72 73 6f 72 28 70 2d 3e 76 2c 20 61 70  eCursor(p->v, ap
1a492 43 73 72 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 72  Csr[i]);.  }.  r
1a493 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 61  eleaseMemArray(a
1a494 4d 65 6d 2c 20 70 2d 3e 6e 43 68 69 6c 64 4d 65  Mem, p->nChildMe
1a495 6d 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  m);.  sqlite3DbF
1a496 72 65 65 28 70 2d 3e 76 2d 3e 64 62 2c 20 70 29  ree(p->v->db, p)
1a497 3b 0a 7d 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c  ;.}...#ifdef SQL
1a498 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52  ITE_ENABLE_MEMOR
1a499 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 53 51 4c  Y_MANAGEMENT.SQL
1a49a 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1a49b 73 71 6c 69 74 65 33 56 64 62 65 52 65 6c 65 61  sqlite3VdbeRelea
1a49c 73 65 42 75 66 66 65 72 73 28 56 64 62 65 20 2a  seBuffers(Vdbe *
1a49d 70 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20  p){.  int ii;.  
1a49e 69 6e 74 20 6e 46 72 65 65 20 3d 20 30 3b 0a 20  int nFree = 0;. 
1a49f 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1a4a0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64  _mutex_held(p->d
1a4a1 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66  b->mutex) );.  f
1a4a2 6f 72 28 69 69 3d 31 3b 20 69 69 3c 3d 70 2d 3e  or(ii=1; ii<=p->
1a4a3 6e 4d 65 6d 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  nMem; ii++){.   
1a4a4 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 26 70 2d   Mem *pMem = &p-
1a4a5 3e 61 4d 65 6d 5b 69 69 5d 3b 0a 20 20 20 20 69  >aMem[ii];.    i
1a4a6 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  f( pMem->flags &
1a4a7 20 4d 45 4d 5f 52 6f 77 53 65 74 20 29 7b 0a 20   MEM_RowSet ){. 
1a4a8 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77 53       sqlite3RowS
1a4a9 65 74 43 6c 65 61 72 28 70 4d 65 6d 2d 3e 75 2e  etClear(pMem->u.
1a4aa 70 52 6f 77 53 65 74 29 3b 0a 20 20 20 20 7d 0a  pRowSet);.    }.
1a4ab 20 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 20      if( pMem->z 
1a4ac 26 26 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d  && pMem->flags&M
1a4ad 45 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 20 20 20  EM_Dyn ){.      
1a4ae 61 73 73 65 72 74 28 20 21 70 4d 65 6d 2d 3e 78  assert( !pMem->x
1a4af 44 65 6c 20 29 3b 0a 20 20 20 20 20 20 6e 46 72  Del );.      nFr
1a4b0 65 65 20 2b 3d 20 73 71 6c 69 74 65 33 44 62 4d  ee += sqlite3DbM
1a4b1 61 6c 6c 6f 63 53 69 7a 65 28 70 4d 65 6d 2d 3e  allocSize(pMem->
1a4b2 64 62 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20  db, pMem->z);.  
1a4b3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1a4b4 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b  emRelease(pMem);
1a4b5 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
1a4b6 75 72 6e 20 6e 46 72 65 65 3b 0a 7d 0a 23 65 6e  urn nFree;.}.#en
1a4b7 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
1a4b8 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e  ITE_OMIT_EXPLAIN
1a4b9 0a 2f 2a 0a 2a 2a 20 47 69 76 65 20 61 20 6c 69  ./*.** Give a li
1a4ba 73 74 69 6e 67 20 6f 66 20 74 68 65 20 70 72 6f  sting of the pro
1a4bb 67 72 61 6d 20 69 6e 20 74 68 65 20 76 69 72 74  gram in the virt
1a4bc 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a  ual machine..**.
1a4bd 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65  ** The interface
1a4be 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20   is the same as 
1a4bf 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28  sqlite3VdbeExec(
1a4c0 29 2e 20 20 42 75 74 20 69 6e 73 74 65 61 64 20  ).  But instead 
1a4c1 6f 66 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 74 68  of.** running th
1a4c2 65 20 63 6f 64 65 2c 20 69 74 20 69 6e 76 6f 6b  e code, it invok
1a4c3 65 73 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  es the callback 
1a4c4 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 69 6e  once for each in
1a4c5 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68  struction..** Th
1a4c6 69 73 20 66 65 61 74 75 72 65 20 69 73 20 75 73  is feature is us
1a4c7 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  ed to implement 
1a4c8 22 45 58 50 4c 41 49 4e 22 2e 0a 2a 2a 0a 2a 2a  "EXPLAIN"..**.**
1a4c9 20 57 68 65 6e 20 70 2d 3e 65 78 70 6c 61 69 6e   When p->explain
1a4ca 3d 3d 31 2c 20 65 61 63 68 20 69 6e 73 74 72 75  ==1, each instru
1a4cb 63 74 69 6f 6e 20 69 73 20 6c 69 73 74 65 64 2e  ction is listed.
1a4cc 20 20 57 68 65 6e 0a 2a 2a 20 70 2d 3e 65 78 70    When.** p->exp
1a4cd 6c 61 69 6e 3d 3d 32 2c 20 6f 6e 6c 79 20 4f 50  lain==2, only OP
1a4ce 5f 45 78 70 6c 61 69 6e 20 69 6e 73 74 72 75 63  _Explain instruc
1a4cf 74 69 6f 6e 73 20 61 72 65 20 6c 69 73 74 65 64  tions are listed
1a4d0 20 61 6e 64 20 74 68 65 73 65 0a 2a 2a 20 61 72   and these.** ar
1a4d1 65 20 73 68 6f 77 6e 20 69 6e 20 61 20 64 69 66  e shown in a dif
1a4d2 66 65 72 65 6e 74 20 66 6f 72 6d 61 74 2e 20 20  ferent format.  
1a4d3 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 69 73  p->explain==2 is
1a4d4 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65   used to impleme
1a4d5 6e 74 0a 2a 2a 20 45 58 50 4c 41 49 4e 20 51 55  nt.** EXPLAIN QU
1a4d6 45 52 59 20 50 4c 41 4e 2e 0a 2a 2f 0a 53 51 4c  ERY PLAN..*/.SQL
1a4d7 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1a4d8 73 71 6c 69 74 65 33 56 64 62 65 4c 69 73 74 28  sqlite3VdbeList(
1a4d9 0a 20 20 56 64 62 65 20 2a 70 20 20 20 20 20 20  .  Vdbe *p      
1a4da 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1a4db 54 68 65 20 56 44 42 45 20 2a 2f 0a 29 7b 0a 20  The VDBE */.){. 
1a4dc 20 69 6e 74 20 6e 52 6f 77 3b 20 20 20 20 20 20   int nRow;      
1a4dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a4de 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e        /* Total n
1a4df 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 74 6f  umber of rows to
1a4e0 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74   return */.  int
1a4e1 20 6e 53 75 62 20 3d 20 30 3b 20 20 20 20 20 20   nSub = 0;      
1a4e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a4e3 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73    /* Number of s
1a4e4 75 62 2d 76 64 62 65 73 20 73 65 65 6e 20 73 6f  ub-vdbes seen so
1a4e5 20 66 61 72 20 2a 2f 0a 20 20 53 75 62 50 72 6f   far */.  SubPro
1a4e6 67 72 61 6d 20 2a 2a 61 70 53 75 62 20 3d 20 30  gram **apSub = 0
1a4e7 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
1a4e8 2a 20 41 72 72 61 79 20 6f 66 20 73 75 62 2d 76  * Array of sub-v
1a4e9 64 62 65 73 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70  dbes */.  Mem *p
1a4ea 53 75 62 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74  Sub = 0;.  sqlit
1a4eb 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a  e3 *db = p->db;.
1a4ec 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 72    int i;.  int r
1a4ed 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
1a4ee 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 70 2d 3e   Mem *pMem = p->
1a4ef 70 52 65 73 75 6c 74 53 65 74 20 3d 20 26 70 2d  pResultSet = &p-
1a4f0 3e 61 4d 65 6d 5b 31 5d 3b 0a 0a 20 20 61 73 73  >aMem[1];..  ass
1a4f1 65 72 74 28 20 70 2d 3e 65 78 70 6c 61 69 6e 20  ert( p->explain 
1a4f2 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
1a4f3 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49  magic==VDBE_MAGI
1a4f4 43 5f 52 55 4e 20 29 3b 0a 20 20 61 73 73 65 72  C_RUN );.  asser
1a4f5 74 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51  t( db->magic==SQ
1a4f6 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20  LITE_MAGIC_BUSY 
1a4f7 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
1a4f8 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc==SQLITE_OK ||
1a4f9 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 42   p->rc==SQLITE_B
1a4fa 55 53 59 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51  USY || p->rc==SQ
1a4fb 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 0a 20  LITE_NOMEM );.. 
1a4fc 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67 68 20   /* Even though 
1a4fd 74 68 69 73 20 6f 70 63 6f 64 65 20 64 6f 65 73  this opcode does
1a4fe 20 6e 6f 74 20 75 73 65 20 64 79 6e 61 6d 69 63   not use dynamic
1a4ff 20 73 74 72 69 6e 67 73 20 66 6f 72 0a 20 20 2a   strings for.  *
1a500 2a 20 74 68 65 20 72 65 73 75 6c 74 2c 20 72 65  * the result, re
1a501 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 6d 61 79  sult columns may
1a502 20 62 65 63 6f 6d 65 20 64 79 6e 61 6d 69 63 20   become dynamic 
1a503 69 66 20 74 68 65 20 75 73 65 72 20 63 61 6c 6c  if the user call
1a504 73 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63  s.  ** sqlite3_c
1a505 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 2c 20  olumn_text16(), 
1a506 63 61 75 73 69 6e 67 20 61 20 74 72 61 6e 73 6c  causing a transl
1a507 61 74 69 6f 6e 20 74 6f 20 55 54 46 2d 31 36 20  ation to UTF-16 
1a508 65 6e 63 6f 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20  encoding..  */. 
1a509 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79   releaseMemArray
1a50a 28 70 4d 65 6d 2c 20 38 29 3b 0a 0a 20 20 69 66  (pMem, 8);..  if
1a50b 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f  ( p->rc==SQLITE_
1a50c 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 2f 2a 20  NOMEM ){.    /* 
1a50d 54 68 69 73 20 68 61 70 70 65 6e 73 20 69 66 20  This happens if 
1a50e 61 20 6d 61 6c 6c 6f 63 28 29 20 69 6e 73 69 64  a malloc() insid
1a50f 65 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  e a call to sqli
1a510 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28  te3_column_text(
1a511 29 20 6f 72 0a 20 20 20 20 2a 2a 20 73 71 6c 69  ) or.    ** sqli
1a512 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31  te3_column_text1
1a513 36 28 29 20 66 61 69 6c 65 64 2e 20 20 2a 2f 0a  6() failed.  */.
1a514 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
1a515 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65  iled = 1;.    re
1a516 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
1a517 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 67  R;.  }..  /* Fig
1a518 75 72 65 20 6f 75 74 20 74 6f 74 61 6c 20 6e 75  ure out total nu
1a519 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 74 68 61  mber of rows tha
1a51a 74 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e  t will be return
1a51b 65 64 20 62 79 20 74 68 69 73 20 0a 20 20 2a 2a  ed by this .  **
1a51c 20 45 58 50 4c 41 49 4e 20 70 72 6f 67 72 61 6d   EXPLAIN program
1a51d 2e 20 20 2a 2f 0a 20 20 6e 52 6f 77 20 3d 20 70  .  */.  nRow = p
1a51e 2d 3e 6e 4f 70 3b 0a 20 20 69 66 28 20 70 2d 3e  ->nOp;.  if( p->
1a51f 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20  explain==1 ){.  
1a520 20 20 70 53 75 62 20 3d 20 26 70 2d 3e 61 4d 65    pSub = &p->aMe
1a521 6d 5b 39 5d 3b 0a 20 20 20 20 69 66 28 20 70 53  m[9];.    if( pS
1a522 75 62 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 6c  ub->flags&MEM_Bl
1a523 6f 62 20 29 7b 0a 20 20 20 20 20 20 6e 53 75 62  ob ){.      nSub
1a524 20 3d 20 70 53 75 62 2d 3e 6e 2f 73 69 7a 65 6f   = pSub->n/sizeo
1a525 66 28 56 64 62 65 2a 29 3b 0a 20 20 20 20 20 20  f(Vdbe*);.      
1a526 61 70 53 75 62 20 3d 20 28 53 75 62 50 72 6f 67  apSub = (SubProg
1a527 72 61 6d 20 2a 2a 29 70 53 75 62 2d 3e 7a 3b 0a  ram **)pSub->z;.
1a528 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d      }.    for(i=
1a529 30 3b 20 69 3c 6e 53 75 62 3b 20 69 2b 2b 29 7b  0; i<nSub; i++){
1a52a 0a 20 20 20 20 20 20 6e 52 6f 77 20 2b 3d 20 61  .      nRow += a
1a52b 70 53 75 62 5b 69 5d 2d 3e 6e 4f 70 3b 0a 20 20  pSub[i]->nOp;.  
1a52c 20 20 7d 0a 20 20 7d 0a 0a 20 20 64 6f 7b 0a 20    }.  }..  do{. 
1a52d 20 20 20 69 20 3d 20 70 2d 3e 70 63 2b 2b 3b 0a     i = p->pc++;.
1a52e 20 20 7d 77 68 69 6c 65 28 20 69 3c 6e 52 6f 77    }while( i<nRow
1a52f 20 26 26 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d   && p->explain==
1a530 32 20 26 26 20 70 2d 3e 61 4f 70 5b 69 5d 2e 6f  2 && p->aOp[i].o
1a531 70 63 6f 64 65 21 3d 4f 50 5f 45 78 70 6c 61 69  pcode!=OP_Explai
1a532 6e 20 29 3b 0a 20 20 69 66 28 20 69 3e 3d 6e 52  n );.  if( i>=nR
1a533 6f 77 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20  ow ){.    p->rc 
1a534 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
1a535 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e   rc = SQLITE_DON
1a536 45 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 64  E;.  }else if( d
1a537 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70  b->u1.isInterrup
1a538 74 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63  ted ){.    p->rc
1a539 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52   = SQLITE_INTERR
1a53a 55 50 54 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  UPT;.    rc = SQ
1a53b 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
1a53c 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67  sqlite3SetString
1a53d 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62  (&p->zErrMsg, db
1a53e 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33 45  , "%s", sqlite3E
1a53f 72 72 53 74 72 28 70 2d 3e 72 63 29 29 3b 0a 20  rrStr(p->rc));. 
1a540 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72   }else{.    char
1a541 20 2a 7a 3b 0a 20 20 20 20 4f 70 20 2a 70 4f 70   *z;.    Op *pOp
1a542 3b 0a 20 20 20 20 69 66 28 20 69 3c 70 2d 3e 6e  ;.    if( i<p->n
1a543 4f 70 20 29 7b 0a 20 20 20 20 20 20 70 4f 70 20  Op ){.      pOp 
1a544 3d 20 26 70 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20  = &p->aOp[i];.  
1a545 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
1a546 6e 74 20 6a 3b 0a 20 20 20 20 20 20 69 20 2d 3d  nt j;.      i -=
1a547 20 70 2d 3e 6e 4f 70 3b 0a 20 20 20 20 20 20 66   p->nOp;.      f
1a548 6f 72 28 6a 3d 30 3b 20 69 3e 3d 61 70 53 75 62  or(j=0; i>=apSub
1a549 5b 6a 5d 2d 3e 6e 4f 70 3b 20 6a 2b 2b 29 7b 0a  [j]->nOp; j++){.
1a54a 20 20 20 20 20 20 20 20 69 20 2d 3d 20 61 70 53          i -= apS
1a54b 75 62 5b 6a 5d 2d 3e 6e 4f 70 3b 0a 20 20 20 20  ub[j]->nOp;.    
1a54c 20 20 7d 0a 20 20 20 20 20 20 70 4f 70 20 3d 20    }.      pOp = 
1a54d 26 61 70 53 75 62 5b 6a 5d 2d 3e 61 4f 70 5b 69  &apSub[j]->aOp[i
1a54e 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  ];.    }.    if(
1a54f 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29   p->explain==1 )
1a550 7b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c  {.      pMem->fl
1a551 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20  ags = MEM_Int;. 
1a552 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20       pMem->type 
1a553 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52  = SQLITE_INTEGER
1a554 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e  ;.      pMem->u.
1a555 69 20 3d 20 69 3b 20 20 20 20 20 20 20 20 20 20  i = i;          
1a556 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a557 20 20 20 20 20 20 2f 2a 20 50 72 6f 67 72 61 6d        /* Program
1a558 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20   counter */.    
1a559 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20 0a 20 20 20    pMem++;.  .   
1a55a 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
1a55b 20 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f   MEM_Static|MEM_
1a55c 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20  Str|MEM_Term;.  
1a55d 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63      pMem->z = (c
1a55e 68 61 72 2a 29 73 71 6c 69 74 65 33 4f 70 63 6f  har*)sqlite3Opco
1a55f 64 65 4e 61 6d 65 28 70 4f 70 2d 3e 6f 70 63 6f  deName(pOp->opco
1a560 64 65 29 3b 20 20 2f 2a 20 4f 70 63 6f 64 65 20  de);  /* Opcode 
1a561 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  */.      assert(
1a562 20 70 4d 65 6d 2d 3e 7a 21 3d 30 20 29 3b 0a 20   pMem->z!=0 );. 
1a563 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73       pMem->n = s
1a564 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70  qlite3Strlen30(p
1a565 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70  Mem->z);.      p
1a566 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49  Mem->type = SQLI
1a567 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 20 20 70  TE_TEXT;.      p
1a568 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54  Mem->enc = SQLIT
1a569 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 70 4d  E_UTF8;.      pM
1a56a 65 6d 2b 2b 3b 0a 0a 20 20 20 20 20 20 69 66 28  em++;..      if(
1a56b 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34   pOp->p4type==P4
1a56c 5f 53 55 42 50 52 4f 47 52 41 4d 20 29 7b 0a 20  _SUBPROGRAM ){. 
1a56d 20 20 20 20 20 20 20 69 6e 74 20 6e 42 79 74 65         int nByte
1a56e 20 3d 20 28 6e 53 75 62 2b 31 29 2a 73 69 7a 65   = (nSub+1)*size
1a56f 6f 66 28 53 75 62 50 72 6f 67 72 61 6d 2a 29 3b  of(SubProgram*);
1a570 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a  .        int j;.
1a571 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b          for(j=0;
1a572 20 6a 3c 6e 53 75 62 3b 20 6a 2b 2b 29 7b 0a 20   j<nSub; j++){. 
1a573 20 20 20 20 20 20 20 20 20 69 66 28 20 61 70 53           if( apS
1a574 75 62 5b 6a 5d 3d 3d 70 4f 70 2d 3e 70 34 2e 70  ub[j]==pOp->p4.p
1a575 50 72 6f 67 72 61 6d 20 29 20 62 72 65 61 6b 3b  Program ) break;
1a576 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1a577 20 20 20 69 66 28 20 6a 3d 3d 6e 53 75 62 20 26     if( j==nSub &
1a578 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71 6c  & SQLITE_OK==sql
1a579 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28  ite3VdbeMemGrow(
1a57a 70 53 75 62 2c 20 6e 42 79 74 65 2c 20 31 29 20  pSub, nByte, 1) 
1a57b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 70 53  ){.          apS
1a57c 75 62 20 3d 20 28 53 75 62 50 72 6f 67 72 61 6d  ub = (SubProgram
1a57d 20 2a 2a 29 70 53 75 62 2d 3e 7a 3b 0a 20 20 20   **)pSub->z;.   
1a57e 20 20 20 20 20 20 20 61 70 53 75 62 5b 6e 53 75         apSub[nSu
1a57f 62 2b 2b 5d 20 3d 20 70 4f 70 2d 3e 70 34 2e 70  b++] = pOp->p4.p
1a580 50 72 6f 67 72 61 6d 3b 0a 20 20 20 20 20 20 20  Program;.       
1a581 20 20 20 70 53 75 62 2d 3e 66 6c 61 67 73 20 7c     pSub->flags |
1a582 3d 20 4d 45 4d 5f 42 6c 6f 62 3b 0a 20 20 20 20  = MEM_Blob;.    
1a583 20 20 20 20 20 20 70 53 75 62 2d 3e 6e 20 3d 20        pSub->n = 
1a584 6e 53 75 62 2a 73 69 7a 65 6f 66 28 53 75 62 50  nSub*sizeof(SubP
1a585 72 6f 67 72 61 6d 2a 29 3b 0a 20 20 20 20 20 20  rogram*);.      
1a586 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
1a587 7d 0a 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  }..    pMem->fla
1a588 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20  gs = MEM_Int;.  
1a589 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f    pMem->u.i = pO
1a58a 70 2d 3e 70 31 3b 20 20 20 20 20 20 20 20 20 20  p->p1;          
1a58b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a58c 2f 2a 20 50 31 20 2a 2f 0a 20 20 20 20 70 4d 65  /* P1 */.    pMe
1a58d 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45  m->type = SQLITE
1a58e 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 70 4d  _INTEGER;.    pM
1a58f 65 6d 2b 2b 3b 0a 0a 20 20 20 20 70 4d 65 6d 2d  em++;..    pMem-
1a590 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74  >flags = MEM_Int
1a591 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20  ;.    pMem->u.i 
1a592 3d 20 70 4f 70 2d 3e 70 32 3b 20 20 20 20 20 20  = pOp->p2;      
1a593 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a594 20 20 20 20 2f 2a 20 50 32 20 2a 2f 0a 20 20 20      /* P2 */.   
1a595 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51   pMem->type = SQ
1a596 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20  LITE_INTEGER;.  
1a597 20 20 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20 69    pMem++;..    i
1a598 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31  f( p->explain==1
1a599 20 29 7b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e   ){.      pMem->
1a59a 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
1a59b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69  .      pMem->u.i
1a59c 20 3d 20 70 4f 70 2d 3e 70 33 3b 20 20 20 20 20   = pOp->p3;     
1a59d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a59e 20 20 20 20 20 2f 2a 20 50 33 20 2a 2f 0a 20 20       /* P3 */.  
1a59f 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d      pMem->type =
1a5a0 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b   SQLITE_INTEGER;
1a5a1 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20  .      pMem++;. 
1a5a2 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 73 71     }..    if( sq
1a5a3 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77  lite3VdbeMemGrow
1a5a4 28 70 4d 65 6d 2c 20 33 32 2c 20 30 29 20 29 7b  (pMem, 32, 0) ){
1a5a5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
1a5a6 34 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72  4 */.      asser
1a5a7 74 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  t( p->db->malloc
1a5a8 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 20 20  Failed );.      
1a5a9 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
1a5aa 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  ROR;.    }.    p
1a5ab 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
1a5ac 5f 44 79 6e 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d  _Dyn|MEM_Str|MEM
1a5ad 5f 54 65 72 6d 3b 0a 20 20 20 20 7a 20 3d 20 64  _Term;.    z = d
1a5ae 69 73 70 6c 61 79 50 34 28 70 4f 70 2c 20 70 4d  isplayP4(pOp, pM
1a5af 65 6d 2d 3e 7a 2c 20 33 32 29 3b 0a 20 20 20 20  em->z, 32);.    
1a5b0 69 66 28 20 7a 21 3d 70 4d 65 6d 2d 3e 7a 20 29  if( z!=pMem->z )
1a5b1 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
1a5b2 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70 4d 65  dbeMemSetStr(pMe
1a5b3 6d 2c 20 7a 2c 20 2d 31 2c 20 53 51 4c 49 54 45  m, z, -1, SQLITE
1a5b4 5f 55 54 46 38 2c 20 30 29 3b 0a 20 20 20 20 7d  _UTF8, 0);.    }
1a5b5 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65  else{.      asse
1a5b6 72 74 28 20 70 4d 65 6d 2d 3e 7a 21 3d 30 20 29  rt( pMem->z!=0 )
1a5b7 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20  ;.      pMem->n 
1a5b8 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
1a5b9 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20  0(pMem->z);.    
1a5ba 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51    pMem->enc = SQ
1a5bb 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 7d  LITE_UTF8;.    }
1a5bc 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20  .    pMem->type 
1a5bd 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20  = SQLITE_TEXT;. 
1a5be 20 20 20 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20     pMem++;..    
1a5bf 69 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d  if( p->explain==
1a5c0 31 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73  1 ){.      if( s
1a5c1 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f  qlite3VdbeMemGro
1a5c2 77 28 70 4d 65 6d 2c 20 34 2c 20 30 29 20 29 7b  w(pMem, 4, 0) ){
1a5c3 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1a5c4 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   p->db->mallocFa
1a5c5 69 6c 65 64 20 29 3b 0a 20 20 20 20 20 20 20 20  iled );.        
1a5c6 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
1a5c7 52 4f 52 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ROR;.      }.   
1a5c8 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
1a5c9 20 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 53 74 72   MEM_Dyn|MEM_Str
1a5ca 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20  |MEM_Term;.     
1a5cb 20 70 4d 65 6d 2d 3e 6e 20 3d 20 32 3b 0a 20 20   pMem->n = 2;.  
1a5cc 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
1a5cd 69 6e 74 66 28 33 2c 20 70 4d 65 6d 2d 3e 7a 2c  intf(3, pMem->z,
1a5ce 20 22 25 2e 32 78 22 2c 20 70 4f 70 2d 3e 70 35   "%.2x", pOp->p5
1a5cf 29 3b 20 20 20 2f 2a 20 50 35 20 2a 2f 0a 20 20  );   /* P5 */.  
1a5d0 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d      pMem->type =
1a5d1 20 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20   SQLITE_TEXT;.  
1a5d2 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20      pMem->enc = 
1a5d3 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20  SQLITE_UTF8;.   
1a5d4 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20 0a 23 69     pMem++;.  .#i
1a5d5 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
1a5d6 47 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d  G.      if( pOp-
1a5d7 3e 7a 43 6f 6d 6d 65 6e 74 20 29 7b 0a 20 20 20  >zComment ){.   
1a5d8 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73       pMem->flags
1a5d9 20 3d 20 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54   = MEM_Str|MEM_T
1a5da 65 72 6d 3b 0a 20 20 20 20 20 20 20 20 70 4d 65  erm;.        pMe
1a5db 6d 2d 3e 7a 20 3d 20 70 4f 70 2d 3e 7a 43 6f 6d  m->z = pOp->zCom
1a5dc 6d 65 6e 74 3b 0a 20 20 20 20 20 20 20 20 70 4d  ment;.        pM
1a5dd 65 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 65 33 53  em->n = sqlite3S
1a5de 74 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29  trlen30(pMem->z)
1a5df 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e  ;.        pMem->
1a5e0 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46  enc = SQLITE_UTF
1a5e1 38 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d  8;.        pMem-
1a5e2 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54  >type = SQLITE_T
1a5e3 45 58 54 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  EXT;.      }else
1a5e4 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a  .#endif.      {.
1a5e5 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c          pMem->fl
1a5e6 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 20  ags = MEM_Null; 
1a5e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a5e8 20 20 20 20 20 20 2f 2a 20 43 6f 6d 6d 65 6e 74        /* Comment
1a5e9 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 4d 65 6d   */.        pMem
1a5ea 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f  ->type = SQLITE_
1a5eb 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 7d 0a 20 20  NULL;.      }.  
1a5ec 20 20 7d 0a 0a 20 20 20 20 70 2d 3e 6e 52 65 73    }..    p->nRes
1a5ed 43 6f 6c 75 6d 6e 20 3d 20 38 20 2d 20 35 2a 28  Column = 8 - 5*(
1a5ee 70 2d 3e 65 78 70 6c 61 69 6e 2d 31 29 3b 0a 20  p->explain-1);. 
1a5ef 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54     p->rc = SQLIT
1a5f0 45 5f 4f 4b 3b 0a 20 20 20 20 72 63 20 3d 20 53  E_OK;.    rc = S
1a5f1 51 4c 49 54 45 5f 52 4f 57 3b 0a 20 20 7d 0a 20  QLITE_ROW;.  }. 
1a5f2 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65   return rc;.}.#e
1a5f3 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
1a5f4 4d 49 54 5f 45 58 50 4c 41 49 4e 20 2a 2f 0a 0a  MIT_EXPLAIN */..
1a5f5 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
1a5f6 42 55 47 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20  BUG./*.** Print 
1a5f7 74 68 65 20 53 51 4c 20 74 68 61 74 20 77 61 73  the SQL that was
1a5f8 20 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74   used to generat
1a5f9 65 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d  e a VDBE program
1a5fa 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1a5fb 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1a5fc 56 64 62 65 50 72 69 6e 74 53 71 6c 28 56 64 62  VdbePrintSql(Vdb
1a5fd 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 4f 70  e *p){.  int nOp
1a5fe 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 56 64 62   = p->nOp;.  Vdb
1a5ff 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 69 66 28 20  eOp *pOp;.  if( 
1a600 6e 4f 70 3c 31 20 29 20 72 65 74 75 72 6e 3b 0a  nOp<1 ) return;.
1a601 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b    pOp = &p->aOp[
1a602 30 5d 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f  0];.  if( pOp->o
1a603 70 63 6f 64 65 3d 3d 4f 50 5f 54 72 61 63 65 20  pcode==OP_Trace 
1a604 26 26 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20  && pOp->p4.z!=0 
1a605 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  ){.    const cha
1a606 72 20 2a 7a 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a  r *z = pOp->p4.z
1a607 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c  ;.    while( sql
1a608 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a 29 20  ite3Isspace(*z) 
1a609 29 20 7a 2b 2b 3b 0a 20 20 20 20 70 72 69 6e 74  ) z++;.    print
1a60a 66 28 22 53 51 4c 3a 20 5b 25 73 5d 5c 6e 22 2c  f("SQL: [%s]\n",
1a60b 20 7a 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69   z);.  }.}.#endi
1a60c 66 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  f..#if !defined(
1a60d 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43  SQLITE_OMIT_TRAC
1a60e 45 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51  E) && defined(SQ
1a60f 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52  LITE_ENABLE_IOTR
1a610 41 43 45 29 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74  ACE)./*.** Print
1a611 20 61 6e 20 49 4f 54 52 41 43 45 20 6d 65 73 73   an IOTRACE mess
1a612 61 67 65 20 73 68 6f 77 69 6e 67 20 53 51 4c 20  age showing SQL 
1a613 63 6f 6e 74 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49  content..*/.SQLI
1a614 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1a615 73 71 6c 69 74 65 33 56 64 62 65 49 4f 54 72 61  sqlite3VdbeIOTra
1a616 63 65 53 71 6c 28 56 64 62 65 20 2a 70 29 7b 0a  ceSql(Vdbe *p){.
1a617 20 20 69 6e 74 20 6e 4f 70 20 3d 20 70 2d 3e 6e    int nOp = p->n
1a618 4f 70 3b 0a 20 20 56 64 62 65 4f 70 20 2a 70 4f  Op;.  VdbeOp *pO
1a619 70 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  p;.  if( sqlite3
1a61a 49 6f 54 72 61 63 65 3d 3d 30 20 29 20 72 65 74  IoTrace==0 ) ret
1a61b 75 72 6e 3b 0a 20 20 69 66 28 20 6e 4f 70 3c 31  urn;.  if( nOp<1
1a61c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 4f 70   ) return;.  pOp
1a61d 20 3d 20 26 70 2d 3e 61 4f 70 5b 30 5d 3b 0a 20   = &p->aOp[0];. 
1a61e 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65   if( pOp->opcode
1a61f 3d 3d 4f 50 5f 54 72 61 63 65 20 26 26 20 70 4f  ==OP_Trace && pO
1a620 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 7b 0a 20 20  p->p4.z!=0 ){.  
1a621 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 20 20    int i, j;.    
1a622 63 68 61 72 20 7a 5b 31 30 30 30 5d 3b 0a 20 20  char z[1000];.  
1a623 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
1a624 74 66 28 73 69 7a 65 6f 66 28 7a 29 2c 20 7a 2c  tf(sizeof(z), z,
1a625 20 22 25 73 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a   "%s", pOp->p4.z
1a626 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  );.    for(i=0; 
1a627 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 7a  sqlite3Isspace(z
1a628 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20  [i]); i++){}.   
1a629 20 66 6f 72 28 6a 3d 30 3b 20 7a 5b 69 5d 3b 20   for(j=0; z[i]; 
1a62a 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  i++){.      if( 
1a62b 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 7a  sqlite3Isspace(z
1a62c 5b 69 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20  [i]) ){.        
1a62d 69 66 28 20 7a 5b 69 2d 31 5d 21 3d 27 20 27 20  if( z[i-1]!=' ' 
1a62e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 5b 6a  ){.          z[j
1a62f 2b 2b 5d 20 3d 20 27 20 27 3b 0a 20 20 20 20 20  ++] = ' ';.     
1a630 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
1a631 7b 0a 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d  {.        z[j++]
1a632 20 3d 20 7a 5b 69 5d 3b 0a 20 20 20 20 20 20 7d   = z[i];.      }
1a633 0a 20 20 20 20 7d 0a 20 20 20 20 7a 5b 6a 5d 20  .    }.    z[j] 
1a634 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  = 0;.    sqlite3
1a635 49 6f 54 72 61 63 65 28 22 53 51 4c 20 25 73 5c  IoTrace("SQL %s\
1a636 6e 22 2c 20 7a 29 3b 0a 20 20 7d 0a 7d 0a 23 65  n", z);.  }.}.#e
1a637 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f  ndif /* !SQLITE_
1a638 4f 4d 49 54 5f 54 52 41 43 45 20 26 26 20 53 51  OMIT_TRACE && SQ
1a639 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52  LITE_ENABLE_IOTR
1a63a 41 43 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c  ACE */../*.** Al
1a63b 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 72 6f  locate space fro
1a63c 6d 20 61 20 66 69 78 65 64 20 73 69 7a 65 20 62  m a fixed size b
1a63d 75 66 66 65 72 2e 20 20 4d 61 6b 65 20 2a 70 70  uffer.  Make *pp
1a63e 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a 2a   point to the.**
1a63f 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65   allocated space
1a640 2e 20 20 28 4e 6f 74 65 3a 20 20 70 70 20 69 73  .  (Note:  pp is
1a641 20 61 20 63 68 61 72 2a 20 72 61 74 68 65 72 20   a char* rather 
1a642 74 68 61 6e 20 61 20 76 6f 69 64 2a 2a 20 74 6f  than a void** to
1a643 0a 2a 2a 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20  .** work around 
1a644 74 68 65 20 70 6f 69 6e 74 65 72 20 61 6c 69 61  the pointer alia
1a645 73 69 6e 67 20 72 75 6c 65 73 20 6f 66 20 43 2e  sing rules of C.
1a646 29 20 20 2a 70 70 20 73 68 6f 75 6c 64 20 69 6e  )  *pp should in
1a647 69 74 69 61 6c 6c 79 0a 2a 2a 20 62 65 20 7a 65  itially.** be ze
1a648 72 6f 2e 20 20 49 66 20 2a 70 70 20 69 73 20 6e  ro.  If *pp is n
1a649 6f 74 20 7a 65 72 6f 2c 20 74 68 61 74 20 6d 65  ot zero, that me
1a64a 61 6e 73 20 74 68 61 74 20 74 68 65 20 73 70 61  ans that the spa
1a64b 63 65 20 68 61 73 20 61 6c 72 65 61 64 79 0a 2a  ce has already.*
1a64c 2a 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64  * been allocated
1a64d 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e   and this routin
1a64e 65 20 69 73 20 61 20 6e 6f 6f 70 2e 0a 2a 2a 0a  e is a noop..**.
1a64f 2a 2a 20 6e 42 79 74 65 20 69 73 20 74 68 65 20  ** nByte is the 
1a650 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
1a651 6f 66 20 73 70 61 63 65 20 6e 65 65 64 65 64 2e  of space needed.
1a652 0a 2a 2a 0a 2a 2a 20 2a 70 70 46 72 6f 6d 20 70  .**.** *ppFrom p
1a653 6f 69 6e 74 20 74 6f 20 61 76 61 69 6c 61 62 6c  oint to availabl
1a654 65 20 73 70 61 63 65 20 61 6e 64 20 70 45 6e 64  e space and pEnd
1a655 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 65   points to the e
1a656 6e 64 20 6f 66 20 74 68 65 0a 2a 2a 20 61 76 61  nd of the.** ava
1a657 69 6c 61 62 6c 65 20 73 70 61 63 65 2e 0a 2a 2a  ilable space..**
1a658 0a 2a 2a 20 2a 70 6e 42 79 74 65 20 69 73 20 61  .** *pnByte is a
1a659 20 63 6f 75 6e 74 65 72 20 6f 66 20 74 68 65 20   counter of the 
1a65a 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
1a65b 6f 66 20 73 70 61 63 65 20 74 68 61 74 20 68 61  of space that ha
1a65c 76 65 20 66 61 69 6c 65 64 0a 2a 2a 20 74 6f 20  ve failed.** to 
1a65d 61 6c 6c 6f 63 61 74 65 2e 20 20 49 66 20 74 68  allocate.  If th
1a65e 65 72 65 20 69 73 20 69 6e 73 75 66 66 69 63 69  ere is insuffici
1a65f 65 6e 74 20 73 70 61 63 65 20 69 6e 20 2a 70 70  ent space in *pp
1a660 46 72 6f 6d 20 74 6f 20 73 61 74 69 73 66 79 20  From to satisfy 
1a661 74 68 65 0a 2a 2a 20 72 65 71 75 65 73 74 2c 20  the.** request, 
1a662 74 68 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 2a  then increment *
1a663 70 6e 42 79 74 65 20 62 79 20 74 68 65 20 61 6d  pnByte by the am
1a664 6f 75 6e 74 20 6f 66 20 74 68 65 20 72 65 71 75  ount of the requ
1a665 65 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  est..*/.static v
1a666 6f 69 64 20 61 6c 6c 6f 63 53 70 61 63 65 28 0a  oid allocSpace(.
1a667 20 20 63 68 61 72 20 2a 70 70 2c 20 20 20 20 20    char *pp,     
1a668 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54         /* IN/OUT
1a669 3a 20 53 65 74 20 2a 70 70 20 74 6f 20 70 6f 69  : Set *pp to poi
1a66a 6e 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 64 20  nt to allocated 
1a66b 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20  buffer */.  int 
1a66c 6e 42 79 74 65 2c 20 20 20 20 20 20 20 20 20 20  nByte,          
1a66d 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
1a66e 74 65 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  tes to allocate 
1a66f 2a 2f 0a 20 20 75 38 20 2a 2a 70 70 46 72 6f 6d  */.  u8 **ppFrom
1a670 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f  ,         /* IN/
1a671 4f 55 54 3a 20 41 6c 6c 6f 63 61 74 65 20 66 72  OUT: Allocate fr
1a672 6f 6d 20 2a 70 70 46 72 6f 6d 20 2a 2f 0a 20 20  om *ppFrom */.  
1a673 75 38 20 2a 70 45 6e 64 2c 20 20 20 20 20 20 20  u8 *pEnd,       
1a674 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
1a675 74 6f 20 31 20 62 79 74 65 20 70 61 73 74 20 74  to 1 byte past t
1a676 68 65 20 65 6e 64 20 6f 66 20 2a 70 70 46 72 6f  he end of *ppFro
1a677 6d 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e  m buffer */.  in
1a678 74 20 2a 70 6e 42 79 74 65 20 20 20 20 20 20 20  t *pnByte       
1a679 20 20 20 2f 2a 20 49 66 20 61 6c 6c 6f 63 61 74     /* If allocat
1a67a 69 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 20 6d 61  ion cannot be ma
1a67b 64 65 2c 20 69 6e 63 72 65 6d 65 6e 74 20 2a 70  de, increment *p
1a67c 6e 42 79 74 65 20 2a 2f 0a 29 7b 0a 20 20 61 73  nByte */.){.  as
1a67d 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45  sert( EIGHT_BYTE
1a67e 5f 41 4c 49 47 4e 4d 45 4e 54 28 2a 70 70 46 72  _ALIGNMENT(*ppFr
1a67f 6f 6d 29 20 29 3b 0a 20 20 69 66 28 20 28 2a 28  om) );.  if( (*(
1a680 76 6f 69 64 2a 2a 29 70 70 29 3d 3d 30 20 29 7b  void**)pp)==0 ){
1a681 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 52 4f 55  .    nByte = ROU
1a682 4e 44 38 28 6e 42 79 74 65 29 3b 0a 20 20 20 20  ND8(nByte);.    
1a683 69 66 28 20 26 28 2a 70 70 46 72 6f 6d 29 5b 6e  if( &(*ppFrom)[n
1a684 42 79 74 65 5d 20 3c 3d 20 70 45 6e 64 20 29 7b  Byte] <= pEnd ){
1a685 0a 20 20 20 20 20 20 2a 28 76 6f 69 64 2a 2a 29  .      *(void**)
1a686 70 70 20 3d 20 28 76 6f 69 64 20 2a 29 2a 70 70  pp = (void *)*pp
1a687 46 72 6f 6d 3b 0a 20 20 20 20 20 20 2a 70 70 46  From;.      *ppF
1a688 72 6f 6d 20 2b 3d 20 6e 42 79 74 65 3b 0a 20 20  rom += nByte;.  
1a689 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a    }else{.      *
1a68a 70 6e 42 79 74 65 20 2b 3d 20 6e 42 79 74 65 3b  pnByte += nByte;
1a68b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
1a68c 0a 2a 2a 20 50 72 65 70 61 72 65 20 61 20 76 69  .** Prepare a vi
1a68d 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 66 6f  rtual machine fo
1a68e 72 20 65 78 65 63 75 74 69 6f 6e 2e 20 20 54 68  r execution.  Th
1a68f 69 73 20 69 6e 76 6f 6c 76 65 73 20 74 68 69 6e  is involves thin
1a690 67 73 20 73 75 63 68 0a 2a 2a 20 61 73 20 61 6c  gs such.** as al
1a691 6c 6f 63 61 74 69 6e 67 20 73 74 61 63 6b 20 73  locating stack s
1a692 70 61 63 65 20 61 6e 64 20 69 6e 69 74 69 61 6c  pace and initial
1a693 69 7a 69 6e 67 20 74 68 65 20 70 72 6f 67 72 61  izing the progra
1a694 6d 20 63 6f 75 6e 74 65 72 2e 0a 2a 2a 20 41 66  m counter..** Af
1a695 74 65 72 20 74 68 65 20 56 44 42 45 20 68 61 73  ter the VDBE has
1a696 20 62 65 20 70 72 65 70 70 65 64 2c 20 69 74 20   be prepped, it 
1a697 63 61 6e 20 62 65 20 65 78 65 63 75 74 65 64 20  can be executed 
1a698 62 79 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a  by one or more.*
1a699 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74  * calls to sqlit
1a69a 65 33 56 64 62 65 45 78 65 63 28 29 2e 20 20 0a  e3VdbeExec().  .
1a69b 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68  **.** This is th
1a69c 65 20 6f 6e 6c 79 20 77 61 79 20 74 6f 20 6d 6f  e only way to mo
1a69d 76 65 20 61 20 56 44 42 45 20 66 72 6f 6d 20 56  ve a VDBE from V
1a69e 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 74  DBE_MAGIC_INIT t
1a69f 6f 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 49 43 5f  o.** VDBE_MAGIC_
1a6a0 52 55 4e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  RUN..**.** This 
1a6a1 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20  function may be 
1a6a2 63 61 6c 6c 65 64 20 6d 6f 72 65 20 74 68 61 6e  called more than
1a6a3 20 6f 6e 63 65 20 6f 6e 20 61 20 73 69 6e 67 6c   once on a singl
1a6a4 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e  e virtual machin
1a6a5 65 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20  e..** The first 
1a6a6 63 61 6c 6c 20 69 73 20 6d 61 64 65 20 77 68 69  call is made whi
1a6a7 6c 65 20 63 6f 6d 70 69 6c 69 6e 67 20 74 68 65  le compiling the
1a6a8 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20   SQL statement. 
1a6a9 53 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 63 61  Subsequent.** ca
1a6aa 6c 6c 73 20 61 72 65 20 6d 61 64 65 20 61 73 20  lls are made as 
1a6ab 70 61 72 74 20 6f 66 20 74 68 65 20 70 72 6f 63  part of the proc
1a6ac 65 73 73 20 6f 66 20 72 65 73 65 74 74 69 6e 67  ess of resetting
1a6ad 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20   a statement to 
1a6ae 62 65 0a 2a 2a 20 72 65 2d 65 78 65 63 75 74 65  be.** re-execute
1a6af 64 20 28 66 72 6f 6d 20 61 20 63 61 6c 6c 20 74  d (from a call t
1a6b0 6f 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28  o sqlite3_reset(
1a6b1 29 29 2e 20 54 68 65 20 6e 56 61 72 2c 20 6e 4d  )). The nVar, nM
1a6b2 65 6d 2c 20 6e 43 75 72 73 6f 72 20 0a 2a 2a 20  em, nCursor .** 
1a6b3 61 6e 64 20 69 73 45 78 70 6c 61 69 6e 20 70 61  and isExplain pa
1a6b4 72 61 6d 65 74 65 72 73 20 61 72 65 20 6f 6e 6c  rameters are onl
1a6b5 79 20 70 61 73 73 65 64 20 63 6f 72 72 65 63 74  y passed correct
1a6b6 20 76 61 6c 75 65 73 20 74 68 65 20 66 69 72 73   values the firs
1a6b7 74 20 74 69 6d 65 0a 2a 2a 20 74 68 65 20 66 75  t time.** the fu
1a6b8 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
1a6b9 2e 20 4f 6e 20 73 75 62 73 65 71 75 65 6e 74 20  . On subsequent 
1a6ba 63 61 6c 6c 73 2c 20 66 72 6f 6d 20 73 71 6c 69  calls, from sqli
1a6bb 74 65 33 5f 72 65 73 65 74 28 29 2c 20 6e 56 61  te3_reset(), nVa
1a6bc 72 0a 2a 2a 20 69 73 20 70 61 73 73 65 64 20 2d  r.** is passed -
1a6bd 31 20 61 6e 64 20 6e 4d 65 6d 2c 20 6e 43 75 72  1 and nMem, nCur
1a6be 73 6f 72 20 61 6e 64 20 69 73 45 78 70 6c 61 69  sor and isExplai
1a6bf 6e 20 61 72 65 20 61 6c 6c 20 70 61 73 73 65 64  n are all passed
1a6c0 20 7a 65 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45   zero..*/.SQLITE
1a6c1 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1a6c2 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61  lite3VdbeMakeRea
1a6c3 64 79 28 0a 20 20 56 64 62 65 20 2a 70 2c 20 20  dy(.  Vdbe *p,  
1a6c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a6c5 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45       /* The VDBE
1a6c6 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 2c 20   */.  int nVar, 
1a6c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a6c8 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1a6c9 66 20 27 3f 27 20 73 65 65 20 69 6e 20 74 68 65  f '?' see in the
1a6ca 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 2a   SQL statement *
1a6cb 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 2c 20 20 20  /.  int nMem,   
1a6cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a6cd 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1a6ce 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 74 6f 20  memory cells to 
1a6cf 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 20 69 6e  allocate */.  in
1a6d0 74 20 6e 43 75 72 73 6f 72 2c 20 20 20 20 20 20  t nCursor,      
1a6d1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1a6d2 4e 75 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72  Number of cursor
1a6d3 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f  s to allocate */
1a6d4 0a 20 20 69 6e 74 20 6e 41 72 67 2c 20 20 20 20  .  int nArg,    
1a6d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a6d6 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d    /* Maximum num
1a6d7 62 65 72 20 6f 66 20 61 72 67 73 20 69 6e 20 53  ber of args in S
1a6d8 75 62 50 72 6f 67 72 61 6d 73 20 2a 2f 0a 20 20  ubPrograms */.  
1a6d9 69 6e 74 20 69 73 45 78 70 6c 61 69 6e 2c 20 20  int isExplain,  
1a6da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1a6db 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 45 58  * True if the EX
1a6dc 50 4c 41 49 4e 20 6b 65 79 77 6f 72 64 73 20 69  PLAIN keywords i
1a6dd 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69  s present */.  i
1a6de 6e 74 20 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e  nt usesStmtJourn
1a6df 61 6c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  al            /*
1a6e0 20 54 72 75 65 20 74 6f 20 73 65 74 20 56 64 62   True to set Vdb
1a6e1 65 2e 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61  e.usesStmtJourna
1a6e2 6c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 3b  l */.){.  int n;
1a6e3 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
1a6e4 20 70 2d 3e 64 62 3b 0a 0a 20 20 61 73 73 65 72   p->db;..  asser
1a6e5 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73  t( p!=0 );.  ass
1a6e6 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56  ert( p->magic==V
1a6e7 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29  DBE_MAGIC_INIT )
1a6e8 3b 0a 0a 20 20 2f 2a 20 54 68 65 72 65 20 73 68  ;..  /* There sh
1a6e9 6f 75 6c 64 20 62 65 20 61 74 20 6c 65 61 73 74  ould be at least
1a6ea 20 6f 6e 65 20 6f 70 63 6f 64 65 2e 0a 20 20 2a   one opcode..  *
1a6eb 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e  /.  assert( p->n
1a6ec 4f 70 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 53 65  Op>0 );..  /* Se
1a6ed 74 20 74 68 65 20 6d 61 67 69 63 20 74 6f 20 56  t the magic to V
1a6ee 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 73 6f  DBE_MAGIC_RUN so
1a6ef 6f 6e 65 72 20 72 61 74 68 65 72 20 74 68 61 6e  oner rather than
1a6f0 20 6c 61 74 65 72 2e 20 2a 2f 0a 20 20 70 2d 3e   later. */.  p->
1a6f1 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47  magic = VDBE_MAG
1a6f2 49 43 5f 52 55 4e 3b 0a 0a 20 20 2f 2a 20 46 6f  IC_RUN;..  /* Fo
1a6f3 72 20 65 61 63 68 20 63 75 72 73 6f 72 20 72 65  r each cursor re
1a6f4 71 75 69 72 65 64 2c 20 61 6c 73 6f 20 61 6c 6c  quired, also all
1a6f5 6f 63 61 74 65 20 61 20 6d 65 6d 6f 72 79 20 63  ocate a memory c
1a6f6 65 6c 6c 2e 20 4d 65 6d 6f 72 79 0a 20 20 2a 2a  ell. Memory.  **
1a6f7 20 63 65 6c 6c 73 20 28 6e 4d 65 6d 2b 31 2d 6e   cells (nMem+1-n
1a6f8 43 75 72 73 6f 72 29 2e 2e 6e 4d 65 6d 2c 20 69  Cursor)..nMem, i
1a6f9 6e 63 6c 75 73 69 76 65 2c 20 77 69 6c 6c 20 6e  nclusive, will n
1a6fa 65 76 65 72 20 62 65 20 75 73 65 64 20 62 79 0a  ever be used by.
1a6fb 20 20 2a 2a 20 74 68 65 20 76 64 62 65 20 70 72    ** the vdbe pr
1a6fc 6f 67 72 61 6d 2e 20 49 6e 73 74 65 61 64 20 74  ogram. Instead t
1a6fd 68 65 79 20 61 72 65 20 75 73 65 64 20 74 6f 20  hey are used to 
1a6fe 61 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66  allocate space f
1a6ff 6f 72 0a 20 20 2a 2a 20 56 64 62 65 43 75 72 73  or.  ** VdbeCurs
1a700 6f 72 2f 42 74 43 75 72 73 6f 72 20 73 74 72 75  or/BtCursor stru
1a701 63 74 75 72 65 73 2e 20 54 68 65 20 62 6c 6f 62  ctures. The blob
1a702 20 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63   of memory assoc
1a703 69 61 74 65 64 20 77 69 74 68 20 0a 20 20 2a 2a  iated with .  **
1a704 20 63 75 72 73 6f 72 20 30 20 69 73 20 73 74 6f   cursor 0 is sto
1a705 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 63 65  red in memory ce
1a706 6c 6c 20 6e 4d 65 6d 2e 20 4d 65 6d 6f 72 79 20  ll nMem. Memory 
1a707 63 65 6c 6c 20 28 6e 4d 65 6d 2d 31 29 0a 20 20  cell (nMem-1).  
1a708 2a 2a 20 73 74 6f 72 65 73 20 74 68 65 20 62 6c  ** stores the bl
1a709 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73  ob of memory ass
1a70a 6f 63 69 61 74 65 64 20 77 69 74 68 20 63 75 72  ociated with cur
1a70b 73 6f 72 20 31 2c 20 65 74 63 2e 0a 20 20 2a 2a  sor 1, etc..  **
1a70c 0a 20 20 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  .  ** See also: 
1a70d 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 29  allocateCursor()
1a70e 2e 0a 20 20 2a 2f 0a 20 20 6e 4d 65 6d 20 2b 3d  ..  */.  nMem +=
1a70f 20 6e 43 75 72 73 6f 72 3b 0a 0a 20 20 2f 2a 20   nCursor;..  /* 
1a710 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66  Allocate space f
1a711 6f 72 20 6d 65 6d 6f 72 79 20 72 65 67 69 73 74  or memory regist
1a712 65 72 73 2c 20 53 51 4c 20 76 61 72 69 61 62 6c  ers, SQL variabl
1a713 65 73 2c 20 56 44 42 45 20 63 75 72 73 6f 72 73  es, VDBE cursors
1a714 20 61 6e 64 20 0a 20 20 2a 2a 20 61 6e 20 61 72   and .  ** an ar
1a715 72 61 79 20 74 6f 20 6d 61 72 73 68 61 6c 20 53  ray to marshal S
1a716 51 4c 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75  QL function argu
1a717 6d 65 6e 74 73 20 69 6e 2e 20 54 68 69 73 20 69  ments in. This i
1a718 73 20 6f 6e 6c 79 20 64 6f 6e 65 20 74 68 65 0a  s only done the.
1a719 20 20 2a 2a 20 66 69 72 73 74 20 74 69 6d 65 20    ** first time 
1a71a 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
1a71b 20 63 61 6c 6c 65 64 20 66 6f 72 20 61 20 67 69   called for a gi
1a71c 76 65 6e 20 56 44 42 45 2c 20 6e 6f 74 20 77 68  ven VDBE, not wh
1a71d 65 6e 20 69 74 20 69 73 0a 20 20 2a 2a 20 62 65  en it is.  ** be
1a71e 69 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20  ing called from 
1a71f 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 20  sqlite3_reset() 
1a720 74 6f 20 72 65 73 65 74 20 74 68 65 20 76 69 72  to reset the vir
1a721 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 20 20  tual machine..  
1a722 2a 2f 0a 20 20 69 66 28 20 6e 56 61 72 3e 3d 30  */.  if( nVar>=0
1a723 20 26 26 20 41 4c 57 41 59 53 28 64 62 2d 3e 6d   && ALWAYS(db->m
1a724 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 29 20  allocFailed==0) 
1a725 29 7b 0a 20 20 20 20 75 38 20 2a 7a 43 73 72 20  ){.    u8 *zCsr 
1a726 3d 20 28 75 38 20 2a 29 26 70 2d 3e 61 4f 70 5b  = (u8 *)&p->aOp[
1a727 70 2d 3e 6e 4f 70 5d 3b 0a 20 20 20 20 75 38 20  p->nOp];.    u8 
1a728 2a 7a 45 6e 64 20 3d 20 28 75 38 20 2a 29 26 70  *zEnd = (u8 *)&p
1a729 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 41 6c 6c 6f  ->aOp[p->nOpAllo
1a72a 63 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74  c];.    int nByt
1a72b 65 3b 0a 20 20 20 20 72 65 73 6f 6c 76 65 50 32  e;.    resolveP2
1a72c 56 61 6c 75 65 73 28 70 2c 20 26 6e 41 72 67 29  Values(p, &nArg)
1a72d 3b 0a 20 20 20 20 70 2d 3e 75 73 65 73 53 74 6d  ;.    p->usesStm
1a72e 74 4a 6f 75 72 6e 61 6c 20 3d 20 28 75 38 29 75  tJournal = (u8)u
1a72f 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 3b 0a  sesStmtJournal;.
1a730 20 20 20 20 69 66 28 20 69 73 45 78 70 6c 61 69      if( isExplai
1a731 6e 20 26 26 20 6e 4d 65 6d 3c 31 30 20 29 7b 0a  n && nMem<10 ){.
1a732 20 20 20 20 20 20 6e 4d 65 6d 20 3d 20 31 30 3b        nMem = 10;
1a733 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 65  .    }.    memse
1a734 74 28 7a 43 73 72 2c 20 30 2c 20 7a 45 6e 64 2d  t(zCsr, 0, zEnd-
1a735 7a 43 73 72 29 3b 0a 20 20 20 20 7a 43 73 72 20  zCsr);.    zCsr 
1a736 2b 3d 20 28 7a 43 73 72 20 2d 20 28 75 38 2a 29  += (zCsr - (u8*)
1a737 30 29 26 37 3b 0a 20 20 20 20 61 73 73 65 72 74  0)&7;.    assert
1a738 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49  ( EIGHT_BYTE_ALI
1a739 47 4e 4d 45 4e 54 28 7a 43 73 72 29 20 29 3b 0a  GNMENT(zCsr) );.
1a73a 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20  .    do {.      
1a73b 6e 42 79 74 65 20 3d 20 30 3b 0a 20 20 20 20 20  nByte = 0;.     
1a73c 20 61 6c 6c 6f 63 53 70 61 63 65 28 28 63 68 61   allocSpace((cha
1a73d 72 2a 29 26 70 2d 3e 61 4d 65 6d 2c 20 6e 4d 65  r*)&p->aMem, nMe
1a73e 6d 2a 73 69 7a 65 6f 66 28 4d 65 6d 29 2c 20 26  m*sizeof(Mem), &
1a73f 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 79  zCsr, zEnd, &nBy
1a740 74 65 29 3b 0a 20 20 20 20 20 20 61 6c 6c 6f 63  te);.      alloc
1a741 53 70 61 63 65 28 28 63 68 61 72 2a 29 26 70 2d  Space((char*)&p-
1a742 3e 61 56 61 72 2c 20 6e 56 61 72 2a 73 69 7a 65  >aVar, nVar*size
1a743 6f 66 28 4d 65 6d 29 2c 20 26 7a 43 73 72 2c 20  of(Mem), &zCsr, 
1a744 7a 45 6e 64 2c 20 26 6e 42 79 74 65 29 3b 0a 20  zEnd, &nByte);. 
1a745 20 20 20 20 20 61 6c 6c 6f 63 53 70 61 63 65 28       allocSpace(
1a746 28 63 68 61 72 2a 29 26 70 2d 3e 61 70 41 72 67  (char*)&p->apArg
1a747 2c 20 6e 41 72 67 2a 73 69 7a 65 6f 66 28 4d 65  , nArg*sizeof(Me
1a748 6d 2a 29 2c 20 26 7a 43 73 72 2c 20 7a 45 6e 64  m*), &zCsr, zEnd
1a749 2c 20 26 6e 42 79 74 65 29 3b 0a 20 20 20 20 20  , &nByte);.     
1a74a 20 61 6c 6c 6f 63 53 70 61 63 65 28 28 63 68 61   allocSpace((cha
1a74b 72 2a 29 26 70 2d 3e 61 7a 56 61 72 2c 20 6e 56  r*)&p->azVar, nV
1a74c 61 72 2a 73 69 7a 65 6f 66 28 63 68 61 72 2a 29  ar*sizeof(char*)
1a74d 2c 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26  , &zCsr, zEnd, &
1a74e 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 61 6c  nByte);.      al
1a74f 6c 6f 63 53 70 61 63 65 28 28 63 68 61 72 2a 29  locSpace((char*)
1a750 26 70 2d 3e 61 70 43 73 72 2c 20 0a 20 20 20 20  &p->apCsr, .    
1a751 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 43 75               nCu
1a752 72 73 6f 72 2a 73 69 7a 65 6f 66 28 56 64 62 65  rsor*sizeof(Vdbe
1a753 43 75 72 73 6f 72 2a 29 2c 20 26 7a 43 73 72 2c  Cursor*), &zCsr,
1a754 20 7a 45 6e 64 2c 20 26 6e 42 79 74 65 0a 20 20   zEnd, &nByte.  
1a755 20 20 20 20 29 3b 0a 20 20 20 20 20 20 69 66 28      );.      if(
1a756 20 6e 42 79 74 65 20 29 7b 0a 20 20 20 20 20 20   nByte ){.      
1a757 20 20 70 2d 3e 70 46 72 65 65 20 3d 20 73 71 6c    p->pFree = sql
1a758 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
1a759 28 64 62 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20  (db, nByte);.   
1a75a 20 20 20 7d 0a 20 20 20 20 20 20 7a 43 73 72 20     }.      zCsr 
1a75b 3d 20 70 2d 3e 70 46 72 65 65 3b 0a 20 20 20 20  = p->pFree;.    
1a75c 20 20 7a 45 6e 64 20 3d 20 26 7a 43 73 72 5b 6e    zEnd = &zCsr[n
1a75d 42 79 74 65 5d 3b 0a 20 20 20 20 7d 77 68 69 6c  Byte];.    }whil
1a75e 65 28 20 6e 42 79 74 65 20 26 26 20 21 64 62 2d  e( nByte && !db-
1a75f 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b  >mallocFailed );
1a760 0a 0a 20 20 20 20 70 2d 3e 6e 43 75 72 73 6f 72  ..    p->nCursor
1a761 20 3d 20 28 75 31 36 29 6e 43 75 72 73 6f 72 3b   = (u16)nCursor;
1a762 0a 20 20 20 20 69 66 28 20 70 2d 3e 61 56 61 72  .    if( p->aVar
1a763 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 56 61   ){.      p->nVa
1a764 72 20 3d 20 28 75 31 36 29 6e 56 61 72 3b 0a 20  r = (u16)nVar;. 
1a765 20 20 20 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c       for(n=0; n<
1a766 6e 56 61 72 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20  nVar; n++){.    
1a767 20 20 20 20 70 2d 3e 61 56 61 72 5b 6e 5d 2e 66      p->aVar[n].f
1a768 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b  lags = MEM_Null;
1a769 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 56 61 72  .        p->aVar
1a76a 5b 6e 5d 2e 64 62 20 3d 20 64 62 3b 0a 20 20 20  [n].db = db;.   
1a76b 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
1a76c 66 28 20 70 2d 3e 61 4d 65 6d 20 29 7b 0a 20 20  f( p->aMem ){.  
1a76d 20 20 20 20 70 2d 3e 61 4d 65 6d 2d 2d 3b 20 20      p->aMem--;  
1a76e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a76f 20 20 20 20 2f 2a 20 61 4d 65 6d 5b 5d 20 67 6f      /* aMem[] go
1a770 65 73 20 66 72 6f 6d 20 31 2e 2e 6e 4d 65 6d 20  es from 1..nMem 
1a771 2a 2f 0a 20 20 20 20 20 20 70 2d 3e 6e 4d 65 6d  */.      p->nMem
1a772 20 3d 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20   = nMem;        
1a773 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 20 20           /*     
1a774 20 20 6e 6f 74 20 66 72 6f 6d 20 30 2e 2e 6e 4d    not from 0..nM
1a775 65 6d 2d 31 20 2a 2f 0a 20 20 20 20 20 20 66 6f  em-1 */.      fo
1a776 72 28 6e 3d 31 3b 20 6e 3c 3d 6e 4d 65 6d 3b 20  r(n=1; n<=nMem; 
1a777 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 2d  n++){.        p-
1a778 3e 61 4d 65 6d 5b 6e 5d 2e 66 6c 61 67 73 20 3d  >aMem[n].flags =
1a779 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20   MEM_Null;.     
1a77a 20 20 20 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e 64 62     p->aMem[n].db
1a77b 20 3d 20 64 62 3b 0a 20 20 20 20 20 20 7d 0a 20   = db;.      }. 
1a77c 20 20 20 7d 0a 20 20 7d 0a 23 69 66 64 65 66 20     }.  }.#ifdef 
1a77d 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 66  SQLITE_DEBUG.  f
1a77e 6f 72 28 6e 3d 31 3b 20 6e 3c 70 2d 3e 6e 4d 65  or(n=1; n<p->nMe
1a77f 6d 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 61 73 73  m; n++){.    ass
1a780 65 72 74 28 20 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e  ert( p->aMem[n].
1a781 64 62 3d 3d 64 62 20 29 3b 0a 20 20 7d 0a 23 65  db==db );.  }.#e
1a782 6e 64 69 66 0a 0a 20 20 70 2d 3e 70 63 20 3d 20  ndif..  p->pc = 
1a783 2d 31 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51  -1;.  p->rc = SQ
1a784 4c 49 54 45 5f 4f 4b 3b 0a 20 20 70 2d 3e 65 72  LITE_OK;.  p->er
1a785 72 6f 72 41 63 74 69 6f 6e 20 3d 20 4f 45 5f 41  rorAction = OE_A
1a786 62 6f 72 74 3b 0a 20 20 70 2d 3e 65 78 70 6c 61  bort;.  p->expla
1a787 69 6e 20 7c 3d 20 69 73 45 78 70 6c 61 69 6e 3b  in |= isExplain;
1a788 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44  .  p->magic = VD
1a789 42 45 5f 4d 41 47 49 43 5f 52 55 4e 3b 0a 20 20  BE_MAGIC_RUN;.  
1a78a 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 30 3b 0a  p->nChange = 0;.
1a78b 20 20 70 2d 3e 63 61 63 68 65 43 74 72 20 3d 20    p->cacheCtr = 
1a78c 31 3b 0a 20 20 70 2d 3e 6d 69 6e 57 72 69 74 65  1;.  p->minWrite
1a78d 46 69 6c 65 46 6f 72 6d 61 74 20 3d 20 32 35 35  FileFormat = 255
1a78e 3b 0a 20 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e  ;.  p->iStatemen
1a78f 74 20 3d 20 30 3b 0a 23 69 66 64 65 66 20 56 44  t = 0;.#ifdef VD
1a790 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 7b 0a 20  BE_PROFILE.  {. 
1a791 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f     int i;.    fo
1a792 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b  r(i=0; i<p->nOp;
1a793 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 2d 3e   i++){.      p->
1a794 61 4f 70 5b 69 5d 2e 63 6e 74 20 3d 20 30 3b 0a  aOp[i].cnt = 0;.
1a795 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e        p->aOp[i].
1a796 63 79 63 6c 65 73 20 3d 20 30 3b 0a 20 20 20 20  cycles = 0;.    
1a797 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a  }.  }.#endif.}..
1a798 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 56 44  /*.** Close a VD
1a799 42 45 20 63 75 72 73 6f 72 20 61 6e 64 20 72 65  BE cursor and re
1a79a 6c 65 61 73 65 20 61 6c 6c 20 74 68 65 20 72 65  lease all the re
1a79b 73 6f 75 72 63 65 73 20 74 68 61 74 20 63 75 72  sources that cur
1a79c 73 6f 72 20 0a 2a 2a 20 68 61 70 70 65 6e 73 20  sor .** happens 
1a79d 74 6f 20 68 6f 6c 64 2e 0a 2a 2f 0a 53 51 4c 49  to hold..*/.SQLI
1a79e 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1a79f 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43  sqlite3VdbeFreeC
1a7a0 75 72 73 6f 72 28 56 64 62 65 20 2a 70 2c 20 56  ursor(Vdbe *p, V
1a7a1 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 29 7b  dbeCursor *pCx){
1a7a2 0a 20 20 69 66 28 20 70 43 78 3d 3d 30 20 29 7b  .  if( pCx==0 ){
1a7a3 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
1a7a4 0a 20 20 69 66 28 20 70 43 78 2d 3e 70 42 74 20  .  if( pCx->pBt 
1a7a5 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  ){.    sqlite3Bt
1a7a6 72 65 65 43 6c 6f 73 65 28 70 43 78 2d 3e 70 42  reeClose(pCx->pB
1a7a7 74 29 3b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70  t);.    /* The p
1a7a8 43 78 2d 3e 70 43 75 72 73 6f 72 20 77 69 6c 6c  Cx->pCursor will
1a7a9 20 62 65 20 63 6c 6f 73 65 20 61 75 74 6f 6d 61   be close automa
1a7aa 74 69 63 61 6c 6c 79 2c 20 69 66 20 69 74 20 65  tically, if it e
1a7ab 78 69 73 74 73 2c 20 62 79 0a 20 20 20 20 2a 2a  xists, by.    **
1a7ac 20 74 68 65 20 63 61 6c 6c 20 61 62 6f 76 65 2e   the call above.
1a7ad 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   */.  }else if( 
1a7ae 70 43 78 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0a  pCx->pCursor ){.
1a7af 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
1a7b0 43 6c 6f 73 65 43 75 72 73 6f 72 28 70 43 78 2d  CloseCursor(pCx-
1a7b1 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 23  >pCursor);.  }.#
1a7b2 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1a7b3 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
1a7b4 20 20 69 66 28 20 70 43 78 2d 3e 70 56 74 61 62    if( pCx->pVtab
1a7b5 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 71  Cursor ){.    sq
1a7b6 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
1a7b7 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 20 3d  r *pVtabCursor =
1a7b8 20 70 43 78 2d 3e 70 56 74 61 62 43 75 72 73 6f   pCx->pVtabCurso
1a7b9 72 3b 0a 20 20 20 20 63 6f 6e 73 74 20 73 71 6c  r;.    const sql
1a7ba 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f  ite3_module *pMo
1a7bb 64 75 6c 65 20 3d 20 70 43 78 2d 3e 70 4d 6f 64  dule = pCx->pMod
1a7bc 75 6c 65 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74  ule;.    p->inVt
1a7bd 61 62 4d 65 74 68 6f 64 20 3d 20 31 3b 0a 20 20  abMethod = 1;.  
1a7be 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
1a7bf 61 66 65 74 79 4f 66 66 28 70 2d 3e 64 62 29 3b  afetyOff(p->db);
1a7c0 0a 20 20 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43  .    pModule->xC
1a7c1 6c 6f 73 65 28 70 56 74 61 62 43 75 72 73 6f 72  lose(pVtabCursor
1a7c2 29 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c  );.    (void)sql
1a7c3 69 74 65 33 53 61 66 65 74 79 4f 6e 28 70 2d 3e  ite3SafetyOn(p->
1a7c4 64 62 29 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74  db);.    p->inVt
1a7c5 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20  abMethod = 0;.  
1a7c6 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a  }.#endif.}../*.*
1a7c7 2a 20 43 6f 70 79 20 74 68 65 20 76 61 6c 75 65  * Copy the value
1a7c8 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  s stored in the 
1a7c9 56 64 62 65 46 72 61 6d 65 20 73 74 72 75 63 74  VdbeFrame struct
1a7ca 75 72 65 20 74 6f 20 69 74 73 20 56 64 62 65 2e  ure to its Vdbe.
1a7cb 20 54 68 69 73 0a 2a 2a 20 69 73 20 75 73 65 64   This.** is used
1a7cc 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77  , for example, w
1a7cd 68 65 6e 20 61 20 74 72 69 67 67 65 72 20 73 75  hen a trigger su
1a7ce 62 2d 70 72 6f 67 72 61 6d 20 69 73 20 68 61 6c  b-program is hal
1a7cf 74 65 64 20 74 6f 20 72 65 73 74 6f 72 65 0a 2a  ted to restore.*
1a7d0 2a 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 68 65  * control to the
1a7d1 20 6d 61 69 6e 20 70 72 6f 67 72 61 6d 2e 0a 2a   main program..*
1a7d2 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1a7d3 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
1a7d4 46 72 61 6d 65 52 65 73 74 6f 72 65 28 56 64 62  FrameRestore(Vdb
1a7d5 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 29 7b  eFrame *pFrame){
1a7d6 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 46 72  .  Vdbe *v = pFr
1a7d7 61 6d 65 2d 3e 76 3b 0a 20 20 76 2d 3e 61 4f 70  ame->v;.  v->aOp
1a7d8 20 3d 20 70 46 72 61 6d 65 2d 3e 61 4f 70 3b 0a   = pFrame->aOp;.
1a7d9 20 20 76 2d 3e 6e 4f 70 20 3d 20 70 46 72 61 6d    v->nOp = pFram
1a7da 65 2d 3e 6e 4f 70 3b 0a 20 20 76 2d 3e 61 4d 65  e->nOp;.  v->aMe
1a7db 6d 20 3d 20 70 46 72 61 6d 65 2d 3e 61 4d 65 6d  m = pFrame->aMem
1a7dc 3b 0a 20 20 76 2d 3e 6e 4d 65 6d 20 3d 20 70 46  ;.  v->nMem = pF
1a7dd 72 61 6d 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 76 2d  rame->nMem;.  v-
1a7de 3e 61 70 43 73 72 20 3d 20 70 46 72 61 6d 65 2d  >apCsr = pFrame-
1a7df 3e 61 70 43 73 72 3b 0a 20 20 76 2d 3e 6e 43 75  >apCsr;.  v->nCu
1a7e0 72 73 6f 72 20 3d 20 70 46 72 61 6d 65 2d 3e 6e  rsor = pFrame->n
1a7e1 43 75 72 73 6f 72 3b 0a 20 20 76 2d 3e 64 62 2d  Cursor;.  v->db-
1a7e2 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 70 46 72  >lastRowid = pFr
1a7e3 61 6d 65 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a  ame->lastRowid;.
1a7e4 20 20 76 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 70    v->nChange = p
1a7e5 46 72 61 6d 65 2d 3e 6e 43 68 61 6e 67 65 3b 0a  Frame->nChange;.
1a7e6 20 20 72 65 74 75 72 6e 20 70 46 72 61 6d 65 2d    return pFrame-
1a7e7 3e 70 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c  >pc;.}../*.** Cl
1a7e8 6f 73 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 2e  ose all cursors.
1a7e9 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 20 72 65 6c 65  .**.** Also rele
1a7ea 61 73 65 20 61 6e 79 20 64 79 6e 61 6d 69 63 20  ase any dynamic 
1a7eb 6d 65 6d 6f 72 79 20 68 65 6c 64 20 62 79 20 74  memory held by t
1a7ec 68 65 20 56 4d 20 69 6e 20 74 68 65 20 56 64 62  he VM in the Vdb
1a7ed 65 2e 61 4d 65 6d 20 6d 65 6d 6f 72 79 20 0a 2a  e.aMem memory .*
1a7ee 2a 20 63 65 6c 6c 20 61 72 72 61 79 2e 20 54 68  * cell array. Th
1a7ef 69 73 20 69 73 20 6e 65 63 65 73 73 61 72 79 20  is is necessary 
1a7f0 61 73 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65  as the memory ce
1a7f1 6c 6c 20 61 72 72 61 79 20 6d 61 79 20 63 6f 6e  ll array may con
1a7f2 74 61 69 6e 0a 2a 2a 20 70 6f 69 6e 74 65 72 73  tain.** pointers
1a7f3 20 74 6f 20 56 64 62 65 46 72 61 6d 65 20 6f 62   to VdbeFrame ob
1a7f4 6a 65 63 74 73 2c 20 77 68 69 63 68 20 6d 61 79  jects, which may
1a7f5 20 69 6e 20 74 75 72 6e 20 63 6f 6e 74 61 69 6e   in turn contain
1a7f6 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20   pointers to.** 
1a7f7 6f 70 65 6e 20 63 75 72 73 6f 72 73 2e 0a 2a 2f  open cursors..*/
1a7f8 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c 6f  .static void clo
1a7f9 73 65 41 6c 6c 43 75 72 73 6f 72 73 28 56 64 62  seAllCursors(Vdb
1a7fa 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e  e *p){.  if( p->
1a7fb 70 46 72 61 6d 65 20 29 7b 0a 20 20 20 20 56 64  pFrame ){.    Vd
1a7fc 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 20  beFrame *pFrame 
1a7fd 3d 20 70 2d 3e 70 46 72 61 6d 65 3b 0a 20 20 20  = p->pFrame;.   
1a7fe 20 66 6f 72 28 70 46 72 61 6d 65 3d 70 2d 3e 70   for(pFrame=p->p
1a7ff 46 72 61 6d 65 3b 20 70 46 72 61 6d 65 2d 3e 70  Frame; pFrame->p
1a800 50 61 72 65 6e 74 3b 20 70 46 72 61 6d 65 3d 70  Parent; pFrame=p
1a801 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 29 3b  Frame->pParent);
1a802 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1a803 46 72 61 6d 65 52 65 73 74 6f 72 65 28 70 46 72  FrameRestore(pFr
1a804 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70  ame);.  }.  p->p
1a805 46 72 61 6d 65 20 3d 20 30 3b 0a 20 20 70 2d 3e  Frame = 0;.  p->
1a806 6e 46 72 61 6d 65 20 3d 20 30 3b 0a 0a 20 20 69  nFrame = 0;..  i
1a807 66 28 20 70 2d 3e 61 70 43 73 72 20 29 7b 0a 20  f( p->apCsr ){. 
1a808 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f     int i;.    fo
1a809 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 75 72  r(i=0; i<p->nCur
1a80a 73 6f 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  sor; i++){.     
1a80b 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 20   VdbeCursor *pC 
1a80c 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20  = p->apCsr[i];. 
1a80d 20 20 20 20 20 69 66 28 20 70 43 20 29 7b 0a 20       if( pC ){. 
1a80e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1a80f 62 65 46 72 65 65 43 75 72 73 6f 72 28 70 2c 20  beFreeCursor(p, 
1a810 70 43 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  pC);.        p->
1a811 61 70 43 73 72 5b 69 5d 20 3d 20 30 3b 0a 20 20  apCsr[i] = 0;.  
1a812 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
1a813 20 20 69 66 28 20 70 2d 3e 61 4d 65 6d 20 29 7b    if( p->aMem ){
1a814 0a 20 20 20 20 72 65 6c 65 61 73 65 4d 65 6d 41  .    releaseMemA
1a815 72 72 61 79 28 26 70 2d 3e 61 4d 65 6d 5b 31 5d  rray(&p->aMem[1]
1a816 2c 20 70 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 7d 0a  , p->nMem);.  }.
1a817 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75  }../*.** Clean u
1a818 70 20 74 68 65 20 56 4d 20 61 66 74 65 72 20 65  p the VM after e
1a819 78 65 63 75 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  xecution..**.** 
1a81a 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c  This routine wil
1a81b 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  l automatically 
1a81c 63 6c 6f 73 65 20 61 6e 79 20 63 75 72 73 6f 72  close any cursor
1a81d 73 2c 20 6c 69 73 74 73 2c 20 61 6e 64 2f 6f 72  s, lists, and/or
1a81e 0a 2a 2a 20 73 6f 72 74 65 72 73 20 74 68 61 74  .** sorters that
1a81f 20 77 65 72 65 20 6c 65 66 74 20 6f 70 65 6e 2e   were left open.
1a820 20 20 49 74 20 61 6c 73 6f 20 64 65 6c 65 74 65    It also delete
1a821 73 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 0a  s the values of.
1a822 2a 2a 20 76 61 72 69 61 62 6c 65 73 20 69 6e 20  ** variables in 
1a823 74 68 65 20 61 56 61 72 5b 5d 20 61 72 72 61 79  the aVar[] array
1a824 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
1a825 20 43 6c 65 61 6e 75 70 28 56 64 62 65 20 2a 70   Cleanup(Vdbe *p
1a826 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
1a827 20 3d 20 70 2d 3e 64 62 3b 0a 0a 23 69 66 64 65   = p->db;..#ifde
1a828 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
1a829 20 2f 2a 20 45 78 65 63 75 74 65 20 61 73 73 65   /* Execute asse
1a82a 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20  rt() statements 
1a82b 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 74  to ensure that t
1a82c 68 65 20 56 64 62 65 2e 61 70 43 73 72 5b 5d 20  he Vdbe.apCsr[] 
1a82d 61 6e 64 20 0a 20 20 2a 2a 20 56 64 62 65 2e 61  and .  ** Vdbe.a
1a82e 4d 65 6d 5b 5d 20 61 72 72 61 79 73 20 68 61 76  Mem[] arrays hav
1a82f 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63  e already been c
1a830 6c 65 61 6e 65 64 20 75 70 2e 20 20 2a 2f 0a 20  leaned up.  */. 
1a831 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d   int i;.  for(i=
1a832 30 3b 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 3b  0; i<p->nCursor;
1a833 20 69 2b 2b 29 20 61 73 73 65 72 74 28 20 70 2d   i++) assert( p-
1a834 3e 61 70 43 73 72 3d 3d 30 20 7c 7c 20 70 2d 3e  >apCsr==0 || p->
1a835 61 70 43 73 72 5b 69 5d 3d 3d 30 20 29 3b 0a 20  apCsr[i]==0 );. 
1a836 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 70 2d 3e   for(i=1; i<=p->
1a837 6e 4d 65 6d 3b 20 69 2b 2b 29 20 61 73 73 65 72  nMem; i++) asser
1a838 74 28 20 70 2d 3e 61 4d 65 6d 3d 3d 30 20 7c 7c  t( p->aMem==0 ||
1a839 20 70 2d 3e 61 4d 65 6d 5b 69 5d 2e 66 6c 61 67   p->aMem[i].flag
1a83a 73 3d 3d 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 23  s==MEM_Null );.#
1a83b 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33  endif..  sqlite3
1a83c 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45  DbFree(db, p->zE
1a83d 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72  rrMsg);.  p->zEr
1a83e 72 4d 73 67 20 3d 20 30 3b 0a 20 20 70 2d 3e 70  rMsg = 0;.  p->p
1a83f 52 65 73 75 6c 74 53 65 74 20 3d 20 30 3b 0a 7d  ResultSet = 0;.}
1a840 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
1a841 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74  number of result
1a842 20 63 6f 6c 75 6d 6e 73 20 74 68 61 74 20 77 69   columns that wi
1a843 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20 62  ll be returned b
1a844 79 20 74 68 69 73 20 53 51 4c 0a 2a 2a 20 73 74  y this SQL.** st
1a845 61 74 65 6d 65 6e 74 2e 20 54 68 69 73 20 69 73  atement. This is
1a846 20 6e 6f 77 20 73 65 74 20 61 74 20 63 6f 6d 70   now set at comp
1a847 69 6c 65 20 74 69 6d 65 2c 20 72 61 74 68 65 72  ile time, rather
1a848 20 74 68 61 6e 20 64 75 72 69 6e 67 0a 2a 2a 20   than during.** 
1a849 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 65  execution of the
1a84a 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 73 6f   vdbe program so
1a84b 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f 63 6f   that sqlite3_co
1a84c 6c 75 6d 6e 5f 63 6f 75 6e 74 28 29 20 63 61 6e  lumn_count() can
1a84d 0a 2a 2a 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e  .** be called on
1a84e 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   an SQL statemen
1a84f 74 20 62 65 66 6f 72 65 20 73 71 6c 69 74 65 33  t before sqlite3
1a850 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49  _step()..*/.SQLI
1a851 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1a852 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75  sqlite3VdbeSetNu
1a853 6d 43 6f 6c 73 28 56 64 62 65 20 2a 70 2c 20 69  mCols(Vdbe *p, i
1a854 6e 74 20 6e 52 65 73 43 6f 6c 75 6d 6e 29 7b 0a  nt nResColumn){.
1a855 20 20 4d 65 6d 20 2a 70 43 6f 6c 4e 61 6d 65 3b    Mem *pColName;
1a856 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69  .  int n;.  sqli
1a857 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b  te3 *db = p->db;
1a858 0a 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72  ..  releaseMemAr
1a859 72 61 79 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65 2c  ray(p->aColName,
1a85a 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43   p->nResColumn*C
1a85b 4f 4c 4e 41 4d 45 5f 4e 29 3b 0a 20 20 73 71 6c  OLNAME_N);.  sql
1a85c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
1a85d 2d 3e 61 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 6e  ->aColName);.  n
1a85e 20 3d 20 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f   = nResColumn*CO
1a85f 4c 4e 41 4d 45 5f 4e 3b 0a 20 20 70 2d 3e 6e 52  LNAME_N;.  p->nR
1a860 65 73 43 6f 6c 75 6d 6e 20 3d 20 28 75 31 36 29  esColumn = (u16)
1a861 6e 52 65 73 43 6f 6c 75 6d 6e 3b 0a 20 20 70 2d  nResColumn;.  p-
1a862 3e 61 43 6f 6c 4e 61 6d 65 20 3d 20 70 43 6f 6c  >aColName = pCol
1a863 4e 61 6d 65 20 3d 20 28 4d 65 6d 2a 29 73 71 6c  Name = (Mem*)sql
1a864 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
1a865 28 64 62 2c 20 73 69 7a 65 6f 66 28 4d 65 6d 29  (db, sizeof(Mem)
1a866 2a 6e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61  *n );.  if( p->a
1a867 43 6f 6c 4e 61 6d 65 3d 3d 30 20 29 20 72 65 74  ColName==0 ) ret
1a868 75 72 6e 3b 0a 20 20 77 68 69 6c 65 28 20 6e 2d  urn;.  while( n-
1a869 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 70 43 6f  - > 0 ){.    pCo
1a86a 6c 4e 61 6d 65 2d 3e 66 6c 61 67 73 20 3d 20 4d  lName->flags = M
1a86b 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 70 43 6f  EM_Null;.    pCo
1a86c 6c 4e 61 6d 65 2d 3e 64 62 20 3d 20 70 2d 3e 64  lName->db = p->d
1a86d 62 3b 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65 2b  b;.    pColName+
1a86e 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  +;.  }.}../*.** 
1a86f 53 65 74 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  Set the name of 
1a870 74 68 65 20 69 64 78 27 74 68 20 63 6f 6c 75 6d  the idx'th colum
1a871 6e 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64  n to be returned
1a872 20 62 79 20 74 68 65 20 53 51 4c 20 73 74 61 74   by the SQL stat
1a873 65 6d 65 6e 74 2e 0a 2a 2a 20 7a 4e 61 6d 65 20  ement..** zName 
1a874 6d 75 73 74 20 62 65 20 61 20 70 6f 69 6e 74 65  must be a pointe
1a875 72 20 74 6f 20 61 20 6e 75 6c 20 74 65 72 6d 69  r to a nul termi
1a876 6e 61 74 65 64 20 73 74 72 69 6e 67 2e 0a 2a 2a  nated string..**
1a877 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 6d 75  .** This call mu
1a878 73 74 20 62 65 20 6d 61 64 65 20 61 66 74 65 72  st be made after
1a879 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74   a call to sqlit
1a87a 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
1a87b 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  ()..**.** The fi
1a87c 6e 61 6c 20 70 61 72 61 6d 65 74 65 72 2c 20 78  nal parameter, x
1a87d 44 65 6c 2c 20 6d 75 73 74 20 62 65 20 6f 6e 65  Del, must be one
1a87e 20 6f 66 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d   of SQLITE_DYNAM
1a87f 49 43 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  IC, SQLITE_STATI
1a880 43 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 54  C.** or SQLITE_T
1a881 52 41 4e 53 49 45 4e 54 2e 20 49 66 20 69 74 20  RANSIENT. If it 
1a882 69 73 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49  is SQLITE_DYNAMI
1a883 43 2c 20 74 68 65 6e 20 74 68 65 20 62 75 66 66  C, then the buff
1a884 65 72 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f  er pointed.** to
1a885 20 62 79 20 7a 4e 61 6d 65 20 77 69 6c 6c 20 62   by zName will b
1a886 65 20 66 72 65 65 64 20 62 79 20 73 71 6c 69 74  e freed by sqlit
1a887 65 33 44 62 46 72 65 65 28 29 20 77 68 65 6e 20  e3DbFree() when 
1a888 74 68 65 20 76 64 62 65 20 69 73 20 64 65 73 74  the vdbe is dest
1a889 72 6f 79 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  royed..*/.SQLITE
1a88a 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1a88b 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
1a88c 6d 65 28 0a 20 20 56 64 62 65 20 2a 70 2c 20 20  me(.  Vdbe *p,  
1a88d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a88e 20 20 20 20 20 20 20 2f 2a 20 56 64 62 65 20 62         /* Vdbe b
1a88f 65 69 6e 67 20 63 6f 6e 66 69 67 75 72 65 64 20  eing configured 
1a890 2a 2f 0a 20 20 69 6e 74 20 69 64 78 2c 20 20 20  */.  int idx,   
1a891 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a892 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
1a893 66 20 63 6f 6c 75 6d 6e 20 7a 4e 61 6d 65 20 61  f column zName a
1a894 70 70 6c 69 65 73 20 74 6f 20 2a 2f 0a 20 20 69  pplies to */.  i
1a895 6e 74 20 76 61 72 2c 20 20 20 20 20 20 20 20 20  nt var,         
1a896 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a897 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 43 4f  /* One of the CO
1a898 4c 4e 41 4d 45 5f 2a 20 63 6f 6e 73 74 61 6e 74  LNAME_* constant
1a899 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  s */.  const cha
1a89a 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20  r *zName,       
1a89b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
1a89c 65 72 20 74 6f 20 62 75 66 66 65 72 20 63 6f 6e  er to buffer con
1a89d 74 61 69 6e 69 6e 67 20 6e 61 6d 65 20 2a 2f 0a  taining name */.
1a89e 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76    void (*xDel)(v
1a89f 6f 69 64 2a 29 20 20 20 20 20 20 20 20 20 20 20  oid*)           
1a8a0 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 6d 61 6e     /* Memory man
1a8a1 61 67 65 6d 65 6e 74 20 73 74 72 61 74 65 67 79  agement strategy
1a8a2 20 66 6f 72 20 7a 4e 61 6d 65 20 2a 2f 0a 29 7b   for zName */.){
1a8a3 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d  .  int rc;.  Mem
1a8a4 20 2a 70 43 6f 6c 4e 61 6d 65 3b 0a 20 20 61 73   *pColName;.  as
1a8a5 73 65 72 74 28 20 69 64 78 3c 70 2d 3e 6e 52 65  sert( idx<p->nRe
1a8a6 73 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 61 73 73  sColumn );.  ass
1a8a7 65 72 74 28 20 76 61 72 3c 43 4f 4c 4e 41 4d 45  ert( var<COLNAME
1a8a8 5f 4e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 64  _N );.  if( p->d
1a8a9 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1a8aa 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21  ){.    assert( !
1a8ab 7a 4e 61 6d 65 20 7c 7c 20 78 44 65 6c 21 3d 53  zName || xDel!=S
1a8ac 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 20 29 3b  QLITE_DYNAMIC );
1a8ad 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
1a8ae 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
1a8af 61 73 73 65 72 74 28 20 70 2d 3e 61 43 6f 6c 4e  assert( p->aColN
1a8b0 61 6d 65 21 3d 30 20 29 3b 0a 20 20 70 43 6f 6c  ame!=0 );.  pCol
1a8b1 4e 61 6d 65 20 3d 20 26 28 70 2d 3e 61 43 6f 6c  Name = &(p->aCol
1a8b2 4e 61 6d 65 5b 69 64 78 2b 76 61 72 2a 70 2d 3e  Name[idx+var*p->
1a8b3 6e 52 65 73 43 6f 6c 75 6d 6e 5d 29 3b 0a 20 20  nResColumn]);.  
1a8b4 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
1a8b5 4d 65 6d 53 65 74 53 74 72 28 70 43 6f 6c 4e 61  MemSetStr(pColNa
1a8b6 6d 65 2c 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20 53  me, zName, -1, S
1a8b7 51 4c 49 54 45 5f 55 54 46 38 2c 20 78 44 65 6c  QLITE_UTF8, xDel
1a8b8 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 21  );.  assert( rc!
1a8b9 3d 30 20 7c 7c 20 21 7a 4e 61 6d 65 20 7c 7c 20  =0 || !zName || 
1a8ba 28 70 43 6f 6c 4e 61 6d 65 2d 3e 66 6c 61 67 73  (pColName->flags
1a8bb 26 4d 45 4d 5f 54 65 72 6d 29 21 3d 30 20 29 3b  &MEM_Term)!=0 );
1a8bc 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1a8bd 0a 2f 2a 0a 2a 2a 20 41 20 72 65 61 64 20 6f 72  ./*.** A read or
1a8be 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
1a8bf 6f 6e 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f  on may or may no
1a8c0 74 20 62 65 20 61 63 74 69 76 65 20 6f 6e 20 64  t be active on d
1a8c1 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a  atabase handle.*
1a8c2 2a 20 64 62 2e 20 49 66 20 61 20 74 72 61 6e 73  * db. If a trans
1a8c3 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65  action is active
1a8c4 2c 20 63 6f 6d 6d 69 74 20 69 74 2e 20 49 66 20  , commit it. If 
1a8c5 74 68 65 72 65 20 69 73 20 61 0a 2a 2a 20 77 72  there is a.** wr
1a8c6 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20  ite-transaction 
1a8c7 73 70 61 6e 6e 69 6e 67 20 6d 6f 72 65 20 74 68  spanning more th
1a8c8 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20  an one database 
1a8c9 66 69 6c 65 2c 20 74 68 69 73 20 72 6f 75 74 69  file, this routi
1a8ca 6e 65 0a 2a 2a 20 74 61 6b 65 73 20 63 61 72 65  ne.** takes care
1a8cb 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a   of the master j
1a8cc 6f 75 72 6e 61 6c 20 74 72 69 63 6b 65 72 79 2e  ournal trickery.
1a8cd 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76  .*/.static int v
1a8ce 64 62 65 43 6f 6d 6d 69 74 28 73 71 6c 69 74 65  dbeCommit(sqlite
1a8cf 33 20 2a 64 62 2c 20 56 64 62 65 20 2a 70 29 7b  3 *db, Vdbe *p){
1a8d0 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
1a8d1 6e 54 72 61 6e 73 20 3d 20 30 3b 20 20 2f 2a 20  nTrans = 0;  /* 
1a8d2 4e 75 6d 62 65 72 20 6f 66 20 64 61 74 61 62 61  Number of databa
1a8d3 73 65 73 20 77 69 74 68 20 61 6e 20 61 63 74 69  ses with an acti
1a8d4 76 65 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63  ve write-transac
1a8d5 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63  tion */.  int rc
1a8d6 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
1a8d7 69 6e 74 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20  int needXcommit 
1a8d8 3d 20 30 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c  = 0;..#ifdef SQL
1a8d9 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1a8da 54 41 42 4c 45 0a 20 20 2f 2a 20 57 69 74 68 20  TABLE.  /* With 
1a8db 74 68 69 73 20 6f 70 74 69 6f 6e 2c 20 73 71 6c  this option, sql
1a8dc 69 74 65 33 56 74 61 62 53 79 6e 63 28 29 20 69  ite3VtabSync() i
1a8dd 73 20 64 65 66 69 6e 65 64 20 74 6f 20 62 65 20  s defined to be 
1a8de 73 69 6d 70 6c 79 20 0a 20 20 2a 2a 20 53 51 4c  simply .  ** SQL
1a8df 49 54 45 5f 4f 4b 20 73 6f 20 70 20 69 73 20 6e  ITE_OK so p is n
1a8e0 6f 74 20 75 73 65 64 2e 20 0a 20 20 2a 2f 0a 20  ot used. .  */. 
1a8e1 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
1a8e2 52 28 70 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20  R(p);.#endif..  
1a8e3 2f 2a 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20  /* Before doing 
1a8e4 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 2c 20 63  anything else, c
1a8e5 61 6c 6c 20 74 68 65 20 78 53 79 6e 63 28 29 20  all the xSync() 
1a8e6 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61 6e 79  callback for any
1a8e7 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d 6f  .  ** virtual mo
1a8e8 64 75 6c 65 20 74 61 62 6c 65 73 20 77 72 69 74  dule tables writ
1a8e9 74 65 6e 20 69 6e 20 74 68 69 73 20 74 72 61 6e  ten in this tran
1a8ea 73 61 63 74 69 6f 6e 2e 20 54 68 69 73 20 68 61  saction. This ha
1a8eb 73 20 74 6f 0a 20 20 2a 2a 20 62 65 20 64 6f 6e  s to.  ** be don
1a8ec 65 20 62 65 66 6f 72 65 20 64 65 74 65 72 6d 69  e before determi
1a8ed 6e 69 6e 67 20 77 68 65 74 68 65 72 20 61 20 6d  ning whether a m
1a8ee 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
1a8ef 6c 65 20 69 73 20 0a 20 20 2a 2a 20 72 65 71 75  le is .  ** requ
1a8f0 69 72 65 64 2c 20 61 73 20 61 6e 20 78 53 79 6e  ired, as an xSyn
1a8f1 63 28 29 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79  c() callback may
1a8f2 20 61 64 64 20 61 6e 20 61 74 74 61 63 68 65 64   add an attached
1a8f3 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 74   database.  ** t
1a8f4 6f 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  o the transactio
1a8f5 6e 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73  n..  */.  rc = s
1a8f6 71 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28 64  qlite3VtabSync(d
1a8f7 62 2c 20 26 70 2d 3e 7a 45 72 72 4d 73 67 29 3b  b, &p->zErrMsg);
1a8f8 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
1a8f9 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75  E_OK ){.    retu
1a8fa 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  rn rc;.  }..  /*
1a8fb 20 54 68 69 73 20 6c 6f 6f 70 20 64 65 74 65 72   This loop deter
1a8fc 6d 69 6e 65 73 20 28 61 29 20 69 66 20 74 68 65  mines (a) if the
1a8fd 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 73 68 6f   commit hook sho
1a8fe 75 6c 64 20 62 65 20 69 6e 76 6f 6b 65 64 20 61  uld be invoked a
1a8ff 6e 64 0a 20 20 2a 2a 20 28 62 29 20 68 6f 77 20  nd.  ** (b) how 
1a900 6d 61 6e 79 20 64 61 74 61 62 61 73 65 20 66 69  many database fi
1a901 6c 65 73 20 68 61 76 65 20 6f 70 65 6e 20 77 72  les have open wr
1a902 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73  ite transactions
1a903 2c 20 6e 6f 74 20 0a 20 20 2a 2a 20 69 6e 63 6c  , not .  ** incl
1a904 75 64 69 6e 67 20 74 68 65 20 74 65 6d 70 20 64  uding the temp d
1a905 61 74 61 62 61 73 65 2e 20 28 62 29 20 69 73 20  atabase. (b) is 
1a906 69 6d 70 6f 72 74 61 6e 74 20 62 65 63 61 75 73  important becaus
1a907 65 20 69 66 20 6d 6f 72 65 20 74 68 61 6e 20 0a  e if more than .
1a908 20 20 2a 2a 20 6f 6e 65 20 64 61 74 61 62 61 73    ** one databas
1a909 65 20 66 69 6c 65 20 68 61 73 20 61 6e 20 6f 70  e file has an op
1a90a 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73 61 63  en write transac
1a90b 74 69 6f 6e 2c 20 61 20 6d 61 73 74 65 72 20 6a  tion, a master j
1a90c 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65  ournal.  ** file
1a90d 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f 72   is required for
1a90e 20 61 6e 20 61 74 6f 6d 69 63 20 63 6f 6d 6d 69   an atomic commi
1a90f 74 2e 0a 20 20 2a 2f 20 0a 20 20 66 6f 72 28 69  t..  */ .  for(i
1a910 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69  =0; i<db->nDb; i
1a911 2b 2b 29 7b 20 0a 20 20 20 20 42 74 72 65 65 20  ++){ .    Btree 
1a912 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69  *pBt = db->aDb[i
1a913 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 73  ].pBt;.    if( s
1a914 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54  qlite3BtreeIsInT
1a915 72 61 6e 73 28 70 42 74 29 20 29 7b 0a 20 20 20  rans(pBt) ){.   
1a916 20 20 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20 3d     needXcommit =
1a917 20 31 3b 0a 20 20 20 20 20 20 69 66 28 20 69 21   1;.      if( i!
1a918 3d 31 20 29 20 6e 54 72 61 6e 73 2b 2b 3b 0a 20  =1 ) nTrans++;. 
1a919 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49     }.  }..  /* I
1a91a 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20  f there are any 
1a91b 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f  write-transactio
1a91c 6e 73 20 61 74 20 61 6c 6c 2c 20 69 6e 76 6f 6b  ns at all, invok
1a91d 65 20 74 68 65 20 63 6f 6d 6d 69 74 20 68 6f 6f  e the commit hoo
1a91e 6b 20 2a 2f 0a 20 20 69 66 28 20 6e 65 65 64 58  k */.  if( needX
1a91f 63 6f 6d 6d 69 74 20 26 26 20 64 62 2d 3e 78 43  commit && db->xC
1a920 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 20 29 7b  ommitCallback ){
1a921 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74  .    (void)sqlit
1a922 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b  e3SafetyOff(db);
1a923 0a 20 20 20 20 72 63 20 3d 20 64 62 2d 3e 78 43  .    rc = db->xC
1a924 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 28 64 62  ommitCallback(db
1a925 2d 3e 70 43 6f 6d 6d 69 74 41 72 67 29 3b 0a 20  ->pCommitArg);. 
1a926 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33     (void)sqlite3
1a927 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20  SafetyOn(db);.  
1a928 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
1a929 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1a92a 43 4f 4e 53 54 52 41 49 4e 54 3b 0a 20 20 20 20  CONSTRAINT;.    
1a92b 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20  }.  }..  /* The 
1a92c 73 69 6d 70 6c 65 20 63 61 73 65 20 2d 20 6e 6f  simple case - no
1a92d 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 64   more than one d
1a92e 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 6e 6f  atabase file (no
1a92f 74 20 63 6f 75 6e 74 69 6e 67 20 74 68 65 0a 20  t counting the. 
1a930 20 2a 2a 20 54 45 4d 50 20 64 61 74 61 62 61 73   ** TEMP databas
1a931 65 29 20 68 61 73 20 61 20 74 72 61 6e 73 61 63  e) has a transac
1a932 74 69 6f 6e 20 61 63 74 69 76 65 2e 20 20 20 54  tion active.   T
1a933 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20  here is no need 
1a934 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 6d 61 73  for the.  ** mas
1a935 74 65 72 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 20 2a  ter-journal..  *
1a936 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 72 65  *.  ** If the re
1a937 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 73 71  turn value of sq
1a938 6c 69 74 65 33 42 74 72 65 65 47 65 74 46 69 6c  lite3BtreeGetFil
1a939 65 6e 61 6d 65 28 29 20 69 73 20 61 20 7a 65 72  ename() is a zer
1a93a 6f 20 6c 65 6e 67 74 68 0a 20 20 2a 2a 20 73 74  o length.  ** st
1a93b 72 69 6e 67 2c 20 69 74 20 6d 65 61 6e 73 20 74  ring, it means t
1a93c 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
1a93d 20 69 73 20 3a 6d 65 6d 6f 72 79 3a 20 6f 72 20   is :memory: or 
1a93e 61 20 74 65 6d 70 20 66 69 6c 65 2e 20 20 49 6e  a temp file.  In
1a93f 20 0a 20 20 2a 2a 20 74 68 61 74 20 63 61 73 65   .  ** that case
1a940 20 77 65 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f   we do not suppo
1a941 72 74 20 61 74 6f 6d 69 63 20 6d 75 6c 74 69 2d  rt atomic multi-
1a942 66 69 6c 65 20 63 6f 6d 6d 69 74 73 2c 20 73 6f  file commits, so
1a943 20 75 73 65 20 74 68 65 20 0a 20 20 2a 2a 20 73   use the .  ** s
1a944 69 6d 70 6c 65 20 63 61 73 65 20 74 68 65 6e 20  imple case then 
1a945 74 6f 6f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  too..  */.  if( 
1a946 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  0==sqlite3Strlen
1a947 33 30 28 73 71 6c 69 74 65 33 42 74 72 65 65 47  30(sqlite3BtreeG
1a948 65 74 46 69 6c 65 6e 61 6d 65 28 64 62 2d 3e 61  etFilename(db->a
1a949 44 62 5b 30 5d 2e 70 42 74 29 29 0a 20 20 20 7c  Db[0].pBt)).   |
1a94a 7c 20 6e 54 72 61 6e 73 3c 3d 31 0a 20 20 29 7b  | nTrans<=1.  ){
1a94b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 72 63  .    for(i=0; rc
1a94c 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69  ==SQLITE_OK && i
1a94d 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a  <db->nDb; i++){.
1a94e 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74        Btree *pBt
1a94f 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42   = db->aDb[i].pB
1a950 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74  t;.      if( pBt
1a951 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
1a952 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d   sqlite3BtreeCom
1a953 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 42 74 2c  mitPhaseOne(pBt,
1a954 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   0);.      }.   
1a955 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 74 68   }..    /* Do th
1a956 65 20 63 6f 6d 6d 69 74 20 6f 6e 6c 79 20 69 66  e commit only if
1a957 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73 20 73   all databases s
1a958 75 63 63 65 73 73 66 75 6c 6c 79 20 63 6f 6d 70  uccessfully comp
1a959 6c 65 74 65 20 70 68 61 73 65 20 31 2e 20 0a 20  lete phase 1. . 
1a95a 20 20 20 2a 2a 20 49 66 20 6f 6e 65 20 6f 66 20     ** If one of 
1a95b 74 68 65 20 42 74 72 65 65 43 6f 6d 6d 69 74 50  the BtreeCommitP
1a95c 68 61 73 65 4f 6e 65 28 29 20 63 61 6c 6c 73 20  haseOne() calls 
1a95d 66 61 69 6c 73 2c 20 74 68 69 73 20 69 6e 64 69  fails, this indi
1a95e 63 61 74 65 73 20 61 6e 0a 20 20 20 20 2a 2a 20  cates an.    ** 
1a95f 49 4f 20 65 72 72 6f 72 20 77 68 69 6c 65 20 64  IO error while d
1a960 65 6c 65 74 69 6e 67 20 6f 72 20 74 72 75 6e 63  eleting or trunc
1a961 61 74 69 6e 67 20 61 20 6a 6f 75 72 6e 61 6c 20  ating a journal 
1a962 66 69 6c 65 2e 20 49 74 20 69 73 20 75 6e 6c 69  file. It is unli
1a963 6b 65 6c 79 2c 0a 20 20 20 20 2a 2a 20 62 75 74  kely,.    ** but
1a964 20 63 6f 75 6c 64 20 68 61 70 70 65 6e 2e 20 49   could happen. I
1a965 6e 20 74 68 69 73 20 63 61 73 65 20 61 62 61 6e  n this case aban
1a966 64 6f 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 61  don processing a
1a967 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 72  nd return the er
1a968 72 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ror..    */.    
1a969 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c  for(i=0; rc==SQL
1a96a 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e  ITE_OK && i<db->
1a96b 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  nDb; i++){.     
1a96c 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62   Btree *pBt = db
1a96d 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20  ->aDb[i].pBt;.  
1a96e 20 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20      if( pBt ){. 
1a96f 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
1a970 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68  te3BtreeCommitPh
1a971 61 73 65 54 77 6f 28 70 42 74 29 3b 0a 20 20 20  aseTwo(pBt);.   
1a972 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
1a973 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1a974 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1a975 33 56 74 61 62 43 6f 6d 6d 69 74 28 64 62 29 3b  3VtabCommit(db);
1a976 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
1a977 20 54 68 65 20 63 6f 6d 70 6c 65 78 20 63 61 73   The complex cas
1a978 65 20 2d 20 54 68 65 72 65 20 69 73 20 61 20 6d  e - There is a m
1a979 75 6c 74 69 2d 66 69 6c 65 20 77 72 69 74 65 2d  ulti-file write-
1a97a 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 63 74 69  transaction acti
1a97b 76 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 72 65  ve..  ** This re
1a97c 71 75 69 72 65 73 20 61 20 6d 61 73 74 65 72 20  quires a master 
1a97d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20  journal file to 
1a97e 65 6e 73 75 72 65 20 74 68 65 20 74 72 61 6e 73  ensure the trans
1a97f 61 63 74 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 63  action is.  ** c
1a980 6f 6d 6d 69 74 74 65 64 20 61 74 6f 6d 69 63 6c  ommitted atomicl
1a981 79 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20  y..  */.#ifndef 
1a982 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b  SQLITE_OMIT_DISK
1a983 49 4f 0a 20 20 65 6c 73 65 7b 0a 20 20 20 20 73  IO.  else{.    s
1a984 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
1a985 20 3d 20 64 62 2d 3e 70 56 66 73 3b 0a 20 20 20   = db->pVfs;.   
1a986 20 69 6e 74 20 6e 65 65 64 53 79 6e 63 20 3d 20   int needSync = 
1a987 30 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d 61  0;.    char *zMa
1a988 73 74 65 72 20 3d 20 30 3b 20 20 20 2f 2a 20 46  ster = 0;   /* F
1a989 69 6c 65 2d 6e 61 6d 65 20 66 6f 72 20 74 68 65  ile-name for the
1a98a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
1a98b 2a 2f 0a 20 20 20 20 63 68 61 72 20 63 6f 6e 73  */.    char cons
1a98c 74 20 2a 7a 4d 61 69 6e 46 69 6c 65 20 3d 20 73  t *zMainFile = s
1a98d 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 46 69  qlite3BtreeGetFi
1a98e 6c 65 6e 61 6d 65 28 64 62 2d 3e 61 44 62 5b 30  lename(db->aDb[0
1a98f 5d 2e 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69  ].pBt);.    sqli
1a990 74 65 33 5f 66 69 6c 65 20 2a 70 4d 61 73 74 65  te3_file *pMaste
1a991 72 20 3d 20 30 3b 0a 20 20 20 20 69 36 34 20 6f  r = 0;.    i64 o
1a992 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 69  ffset = 0;.    i
1a993 6e 74 20 72 65 73 3b 0a 0a 20 20 20 20 2f 2a 20  nt res;..    /* 
1a994 53 65 6c 65 63 74 20 61 20 6d 61 73 74 65 72 20  Select a master 
1a995 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d  journal file nam
1a996 65 20 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a 20 20  e */.    do {.  
1a997 20 20 20 20 75 33 32 20 69 52 61 6e 64 6f 6d 3b      u32 iRandom;
1a998 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
1a999 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72  Free(db, zMaster
1a99a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1a99b 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65  _randomness(size
1a99c 6f 66 28 69 52 61 6e 64 6f 6d 29 2c 20 26 69 52  of(iRandom), &iR
1a99d 61 6e 64 6f 6d 29 3b 0a 20 20 20 20 20 20 7a 4d  andom);.      zM
1a99e 61 73 74 65 72 20 3d 20 73 71 6c 69 74 65 33 4d  aster = sqlite3M
1a99f 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73 2d 6d  Printf(db, "%s-m
1a9a0 6a 25 30 38 58 22 2c 20 7a 4d 61 69 6e 46 69 6c  j%08X", zMainFil
1a9a1 65 2c 20 69 52 61 6e 64 6f 6d 26 30 78 37 66 66  e, iRandom&0x7ff
1a9a2 66 66 66 66 66 29 3b 0a 20 20 20 20 20 20 69 66  fffff);.      if
1a9a3 28 20 21 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20  ( !zMaster ){.  
1a9a4 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
1a9a5 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
1a9a6 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71   }.      rc = sq
1a9a7 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 70 56  lite3OsAccess(pV
1a9a8 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 53 51 4c  fs, zMaster, SQL
1a9a9 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54  ITE_ACCESS_EXIST
1a9aa 53 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 7d 77  S, &res);.    }w
1a9ab 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45  hile( rc==SQLITE
1a9ac 5f 4f 4b 20 26 26 20 72 65 73 20 29 3b 0a 20 20  _OK && res );.  
1a9ad 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1a9ae 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  _OK ){.      /* 
1a9af 4f 70 65 6e 20 74 68 65 20 6d 61 73 74 65 72 20  Open the master 
1a9b0 6a 6f 75 72 6e 61 6c 2e 20 2a 2f 0a 20 20 20 20  journal. */.    
1a9b1 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
1a9b2 4f 70 65 6e 4d 61 6c 6c 6f 63 28 70 56 66 73 2c  OpenMalloc(pVfs,
1a9b3 20 7a 4d 61 73 74 65 72 2c 20 26 70 4d 61 73 74   zMaster, &pMast
1a9b4 65 72 2c 20 0a 20 20 20 20 20 20 20 20 20 20 53  er, .          S
1a9b5 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
1a9b6 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e  RITE|SQLITE_OPEN
1a9b7 5f 43 52 45 41 54 45 7c 0a 20 20 20 20 20 20 20  _CREATE|.       
1a9b8 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45     SQLITE_OPEN_E
1a9b9 58 43 4c 55 53 49 56 45 7c 53 51 4c 49 54 45 5f  XCLUSIVE|SQLITE_
1a9ba 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52  OPEN_MASTER_JOUR
1a9bb 4e 41 4c 2c 20 30 0a 20 20 20 20 20 20 29 3b 0a  NAL, 0.      );.
1a9bc 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63      }.    if( rc
1a9bd 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1a9be 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
1a9bf 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b  ee(db, zMaster);
1a9c0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
1a9c1 3b 0a 20 20 20 20 7d 0a 20 0a 20 20 20 20 2f 2a  ;.    }. .    /*
1a9c2 20 57 72 69 74 65 20 74 68 65 20 6e 61 6d 65 20   Write the name 
1a9c3 6f 66 20 65 61 63 68 20 64 61 74 61 62 61 73 65  of each database
1a9c4 20 66 69 6c 65 20 69 6e 20 74 68 65 20 74 72 61   file in the tra
1a9c5 6e 73 61 63 74 69 6f 6e 20 69 6e 74 6f 20 74 68  nsaction into th
1a9c6 65 20 6e 65 77 0a 20 20 20 20 2a 2a 20 6d 61 73  e new.    ** mas
1a9c7 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
1a9c8 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  . If an error oc
1a9c9 63 75 72 73 20 61 74 20 74 68 69 73 20 70 6f 69  curs at this poi
1a9ca 6e 74 20 63 6c 6f 73 65 0a 20 20 20 20 2a 2a 20  nt close.    ** 
1a9cb 61 6e 64 20 64 65 6c 65 74 65 20 74 68 65 20 6d  and delete the m
1a9cc 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
1a9cd 6c 65 2e 20 41 6c 6c 20 74 68 65 20 69 6e 64 69  le. All the indi
1a9ce 76 69 64 75 61 6c 20 6a 6f 75 72 6e 61 6c 20 66  vidual journal f
1a9cf 69 6c 65 73 0a 20 20 20 20 2a 2a 20 73 74 69 6c  iles.    ** stil
1a9d0 6c 20 68 61 76 65 20 27 6e 75 6c 6c 27 20 61 73  l have 'null' as
1a9d1 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
1a9d2 6e 61 6c 20 70 6f 69 6e 74 65 72 2c 20 73 6f 20  nal pointer, so 
1a9d3 74 68 65 79 20 77 69 6c 6c 20 72 6f 6c 6c 0a 20  they will roll. 
1a9d4 20 20 20 2a 2a 20 62 61 63 6b 20 69 6e 64 65 70     ** back indep
1a9d5 65 6e 64 65 6e 74 6c 79 20 69 66 20 61 20 66 61  endently if a fa
1a9d6 69 6c 75 72 65 20 6f 63 63 75 72 73 2e 0a 20 20  ilure occurs..  
1a9d7 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30    */.    for(i=0
1a9d8 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
1a9d9 29 7b 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a  ){.      Btree *
1a9da 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d  pBt = db->aDb[i]
1a9db 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20  .pBt;.      if( 
1a9dc 69 3d 3d 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b  i==1 ) continue;
1a9dd 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 68 65     /* Ignore the
1a9de 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 20 2a   TEMP database *
1a9df 2f 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  /.      if( sqli
1a9e0 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e  te3BtreeIsInTran
1a9e1 73 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20  s(pBt) ){.      
1a9e2 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 46    char const *zF
1a9e3 69 6c 65 20 3d 20 73 71 6c 69 74 65 33 42 74 72  ile = sqlite3Btr
1a9e4 65 65 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65  eeGetJournalname
1a9e5 28 70 42 74 29 3b 0a 20 20 20 20 20 20 20 20 69  (pBt);.        i
1a9e6 66 28 20 7a 46 69 6c 65 5b 30 5d 3d 3d 30 20 29  f( zFile[0]==0 )
1a9e7 20 63 6f 6e 74 69 6e 75 65 3b 20 20 2f 2a 20 49   continue;  /* I
1a9e8 67 6e 6f 72 65 20 3a 6d 65 6d 6f 72 79 3a 20 64  gnore :memory: d
1a9e9 61 74 61 62 61 73 65 73 20 2a 2f 0a 20 20 20 20  atabases */.    
1a9ea 20 20 20 20 69 66 28 20 21 6e 65 65 64 53 79 6e      if( !needSyn
1a9eb 63 20 26 26 20 21 73 71 6c 69 74 65 33 42 74 72  c && !sqlite3Btr
1a9ec 65 65 53 79 6e 63 44 69 73 61 62 6c 65 64 28 70  eeSyncDisabled(p
1a9ed 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Bt) ){.         
1a9ee 20 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20   needSync = 1;. 
1a9ef 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1a9f0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57   rc = sqlite3OsW
1a9f1 72 69 74 65 28 70 4d 61 73 74 65 72 2c 20 7a 46  rite(pMaster, zF
1a9f2 69 6c 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c  ile, sqlite3Strl
1a9f3 65 6e 33 30 28 7a 46 69 6c 65 29 2b 31 2c 20 6f  en30(zFile)+1, o
1a9f4 66 66 73 65 74 29 3b 0a 20 20 20 20 20 20 20 20  ffset);.        
1a9f5 6f 66 66 73 65 74 20 2b 3d 20 73 71 6c 69 74 65  offset += sqlite
1a9f6 33 53 74 72 6c 65 6e 33 30 28 7a 46 69 6c 65 29  3Strlen30(zFile)
1a9f7 2b 31 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  +1;.        if( 
1a9f8 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1a9f9 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
1a9fa 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 70 4d  e3OsCloseFree(pM
1a9fb 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20  aster);.        
1a9fc 20 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74    sqlite3OsDelet
1a9fd 65 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c  e(pVfs, zMaster,
1a9fe 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73   0);.          s
1a9ff 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1aa00 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20   zMaster);.     
1aa01 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
1aa02 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1aa03 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  }.    }..    /* 
1aa04 53 79 6e 63 20 74 68 65 20 6d 61 73 74 65 72 20  Sync the master 
1aa05 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66  journal file. If
1aa06 20 74 68 65 20 49 4f 43 41 50 5f 53 45 51 55 45   the IOCAP_SEQUE
1aa07 4e 54 49 41 4c 20 64 65 76 69 63 65 0a 20 20 20  NTIAL device.   
1aa08 20 2a 2a 20 66 6c 61 67 20 69 73 20 73 65 74 20   ** flag is set 
1aa09 74 68 69 73 20 69 73 20 6e 6f 74 20 72 65 71 75  this is not requ
1aa0a 69 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ired..    */.   
1aa0b 20 69 66 28 20 6e 65 65 64 53 79 6e 63 20 0a 20   if( needSync . 
1aa0c 20 20 20 20 26 26 20 30 3d 3d 28 73 71 6c 69 74      && 0==(sqlit
1aa0d 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 63  e3OsDeviceCharac
1aa0e 74 65 72 69 73 74 69 63 73 28 70 4d 61 73 74 65  teristics(pMaste
1aa0f 72 29 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  r)&SQLITE_IOCAP_
1aa10 53 45 51 55 45 4e 54 49 41 4c 29 0a 20 20 20 20  SEQUENTIAL).    
1aa11 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28   && SQLITE_OK!=(
1aa12 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79  rc = sqlite3OsSy
1aa13 6e 63 28 70 4d 61 73 74 65 72 2c 20 53 51 4c 49  nc(pMaster, SQLI
1aa14 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 29 29  TE_SYNC_NORMAL))
1aa15 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 73 71  .    ){.      sq
1aa16 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65  lite3OsCloseFree
1aa17 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20  (pMaster);.     
1aa18 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65   sqlite3OsDelete
1aa19 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20  (pVfs, zMaster, 
1aa1a 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  0);.      sqlite
1aa1b 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73  3DbFree(db, zMas
1aa1c 74 65 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75  ter);.      retu
1aa1d 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20  rn rc;.    }..  
1aa1e 20 20 2f 2a 20 53 79 6e 63 20 61 6c 6c 20 74 68    /* Sync all th
1aa1f 65 20 64 62 20 66 69 6c 65 73 20 69 6e 76 6f 6c  e db files invol
1aa20 76 65 64 20 69 6e 20 74 68 65 20 74 72 61 6e 73  ved in the trans
1aa21 61 63 74 69 6f 6e 2e 20 54 68 65 20 73 61 6d 65  action. The same
1aa22 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 73 65 74   call.    ** set
1aa23 73 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  s the master jou
1aa24 72 6e 61 6c 20 70 6f 69 6e 74 65 72 20 69 6e 20  rnal pointer in 
1aa25 65 61 63 68 20 69 6e 64 69 76 69 64 75 61 6c 20  each individual 
1aa26 6a 6f 75 72 6e 61 6c 2e 20 49 66 0a 20 20 20 20  journal. If.    
1aa27 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  ** an error occu
1aa28 72 73 20 68 65 72 65 2c 20 64 6f 20 6e 6f 74 20  rs here, do not 
1aa29 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65  delete the maste
1aa2a 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a  r journal file..
1aa2b 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66      **.    ** If
1aa2c 20 74 68 65 20 65 72 72 6f 72 20 6f 63 63 75 72   the error occur
1aa2d 73 20 64 75 72 69 6e 67 20 74 68 65 20 66 69 72  s during the fir
1aa2e 73 74 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20 2a  st call to.    *
1aa2f 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  * sqlite3BtreeCo
1aa30 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 2c 20  mmitPhaseOne(), 
1aa31 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 61 20  then there is a 
1aa32 63 68 61 6e 63 65 20 74 68 61 74 20 74 68 65 0a  chance that the.
1aa33 20 20 20 20 2a 2a 20 6d 61 73 74 65 72 20 6a 6f      ** master jo
1aa34 75 72 6e 61 6c 20 66 69 6c 65 20 77 69 6c 6c 20  urnal file will 
1aa35 62 65 20 6f 72 70 68 61 6e 65 64 2e 20 42 75 74  be orphaned. But
1aa36 20 77 65 20 63 61 6e 6e 6f 74 20 64 65 6c 65 74   we cannot delet
1aa37 65 20 69 74 2c 0a 20 20 20 20 2a 2a 20 69 6e 20  e it,.    ** in 
1aa38 63 61 73 65 20 74 68 65 20 6d 61 73 74 65 72 20  case the master 
1aa39 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d  journal file nam
1aa3a 65 20 77 61 73 20 77 72 69 74 74 65 6e 20 69 6e  e was written in
1aa3b 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20  to the journal. 
1aa3c 20 20 20 2a 2a 20 66 69 6c 65 20 62 65 66 6f 72     ** file befor
1aa3d 65 20 74 68 65 20 66 61 69 6c 75 72 65 20 6f 63  e the failure oc
1aa3e 63 75 72 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20  curred..    */. 
1aa3f 20 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d     for(i=0; rc==
1aa40 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64  SQLITE_OK && i<d
1aa41 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20  b->nDb; i++){ . 
1aa42 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20       Btree *pBt 
1aa43 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74  = db->aDb[i].pBt
1aa44 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 20  ;.      if( pBt 
1aa45 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
1aa46 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d  sqlite3BtreeComm
1aa47 69 74 50 68 61 73 65 4f 6e 65 28 70 42 74 2c 20  itPhaseOne(pBt, 
1aa48 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20  zMaster);.      
1aa49 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  }.    }.    sqli
1aa4a 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 70  te3OsCloseFree(p
1aa4b 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 69 66 28  Master);.    if(
1aa4c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1aa4d 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  {.      sqlite3D
1aa4e 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 65  bFree(db, zMaste
1aa4f 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  r);.      return
1aa50 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20   rc;.    }..    
1aa51 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20 6d 61  /* Delete the ma
1aa52 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
1aa53 65 2e 20 54 68 69 73 20 63 6f 6d 6d 69 74 73 20  e. This commits 
1aa54 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  the transaction.
1aa55 20 41 66 74 65 72 0a 20 20 20 20 2a 2a 20 64 6f   After.    ** do
1aa56 69 6e 67 20 74 68 69 73 20 74 68 65 20 64 69 72  ing this the dir
1aa57 65 63 74 6f 72 79 20 69 73 20 73 79 6e 63 65 64  ectory is synced
1aa58 20 61 67 61 69 6e 20 62 65 66 6f 72 65 20 61 6e   again before an
1aa59 79 20 69 6e 64 69 76 69 64 75 61 6c 0a 20 20 20  y individual.   
1aa5a 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20   ** transaction 
1aa5b 66 69 6c 65 73 20 61 72 65 20 64 65 6c 65 74 65  files are delete
1aa5c 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63  d..    */.    rc
1aa5d 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65   = sqlite3OsDele
1aa5e 74 65 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72  te(pVfs, zMaster
1aa5f 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 1);.    sqlite
1aa60 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73  3DbFree(db, zMas
1aa61 74 65 72 29 3b 0a 20 20 20 20 7a 4d 61 73 74 65  ter);.    zMaste
1aa62 72 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 72  r = 0;.    if( r
1aa63 63 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  c ){.      retur
1aa64 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  n rc;.    }..   
1aa65 20 2f 2a 20 41 6c 6c 20 66 69 6c 65 73 20 61 6e   /* All files an
1aa66 64 20 64 69 72 65 63 74 6f 72 69 65 73 20 68 61  d directories ha
1aa67 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  ve already been 
1aa68 73 79 6e 63 65 64 2c 20 73 6f 20 74 68 65 20 66  synced, so the f
1aa69 6f 6c 6c 6f 77 69 6e 67 0a 20 20 20 20 2a 2a 20  ollowing.    ** 
1aa6a 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33  calls to sqlite3
1aa6b 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65  BtreeCommitPhase
1aa6c 54 77 6f 28 29 20 61 72 65 20 6f 6e 6c 79 20 63  Two() are only c
1aa6d 6c 6f 73 69 6e 67 20 66 69 6c 65 73 20 61 6e 64  losing files and
1aa6e 0a 20 20 20 20 2a 2a 20 64 65 6c 65 74 69 6e 67  .    ** deleting
1aa6f 20 6f 72 20 74 72 75 6e 63 61 74 69 6e 67 20 6a   or truncating j
1aa70 6f 75 72 6e 61 6c 73 2e 20 49 66 20 73 6f 6d 65  ournals. If some
1aa71 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67  thing goes wrong
1aa72 20 77 68 69 6c 65 0a 20 20 20 20 2a 2a 20 74 68   while.    ** th
1aa73 69 73 20 69 73 20 68 61 70 70 65 6e 69 6e 67 20  is is happening 
1aa74 77 65 20 64 6f 6e 27 74 20 72 65 61 6c 6c 79 20  we don't really 
1aa75 63 61 72 65 2e 20 54 68 65 20 69 6e 74 65 67 72  care. The integr
1aa76 69 74 79 20 6f 66 20 74 68 65 0a 20 20 20 20 2a  ity of the.    *
1aa77 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  * transaction is
1aa78 20 61 6c 72 65 61 64 79 20 67 75 61 72 61 6e 74   already guarant
1aa79 65 65 64 2c 20 62 75 74 20 73 6f 6d 65 20 73 74  eed, but some st
1aa7a 72 61 79 20 27 63 6f 6c 64 27 20 6a 6f 75 72 6e  ray 'cold' journ
1aa7b 61 6c 73 0a 20 20 20 20 2a 2a 20 6d 61 79 20 62  als.    ** may b
1aa7c 65 20 6c 79 69 6e 67 20 61 72 6f 75 6e 64 2e 20  e lying around. 
1aa7d 52 65 74 75 72 6e 69 6e 67 20 61 6e 20 65 72 72  Returning an err
1aa7e 6f 72 20 63 6f 64 65 20 77 6f 6e 27 74 20 68 65  or code won't he
1aa7f 6c 70 20 6d 61 74 74 65 72 73 2e 0a 20 20 20 20  lp matters..    
1aa80 2a 2f 0a 20 20 20 20 64 69 73 61 62 6c 65 5f 73  */.    disable_s
1aa81 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f  imulated_io_erro
1aa82 72 73 28 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  rs();.    sqlite
1aa83 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c  3BeginBenignMall
1aa84 6f 63 28 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d  oc();.    for(i=
1aa85 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  0; i<db->nDb; i+
1aa86 2b 29 7b 20 0a 20 20 20 20 20 20 42 74 72 65 65  +){ .      Btree
1aa87 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b   *pBt = db->aDb[
1aa88 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66  i].pBt;.      if
1aa89 28 20 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20  ( pBt ){.       
1aa8a 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d   sqlite3BtreeCom
1aa8b 6d 69 74 50 68 61 73 65 54 77 6f 28 70 42 74 29  mitPhaseTwo(pBt)
1aa8c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1aa8d 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65      sqlite3EndBe
1aa8e 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20  nignMalloc();.  
1aa8f 20 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74    enable_simulat
1aa90 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a  ed_io_errors();.
1aa91 0a 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62  .    sqlite3Vtab
1aa92 43 6f 6d 6d 69 74 28 64 62 29 3b 0a 20 20 7d 0a  Commit(db);.  }.
1aa93 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e  #endif..  return
1aa94 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54   rc;.}../* .** T
1aa95 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63  his routine chec
1aa96 6b 73 20 74 68 61 74 20 74 68 65 20 73 71 6c 69  ks that the sqli
1aa97 74 65 33 2e 61 63 74 69 76 65 56 64 62 65 43 6e  te3.activeVdbeCn
1aa98 74 20 63 6f 75 6e 74 20 76 61 72 69 61 62 6c 65  t count variable
1aa99 0a 2a 2a 20 6d 61 74 63 68 65 73 20 74 68 65 20  .** matches the 
1aa9a 6e 75 6d 62 65 72 20 6f 66 20 76 64 62 65 27 73  number of vdbe's
1aa9b 20 69 6e 20 74 68 65 20 6c 69 73 74 20 73 71 6c   in the list sql
1aa9c 69 74 65 33 2e 70 56 64 62 65 20 74 68 61 74 20  ite3.pVdbe that 
1aa9d 61 72 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79  are.** currently
1aa9e 20 61 63 74 69 76 65 2e 20 41 6e 20 61 73 73 65   active. An asse
1aa9f 72 74 69 6f 6e 20 66 61 69 6c 73 20 69 66 20 74  rtion fails if t
1aaa0 68 65 20 74 77 6f 20 63 6f 75 6e 74 73 20 64 6f  he two counts do
1aaa1 20 6e 6f 74 20 6d 61 74 63 68 2e 0a 2a 2a 20 54   not match..** T
1aaa2 68 69 73 20 69 73 20 61 6e 20 69 6e 74 65 72 6e  his is an intern
1aaa3 61 6c 20 73 65 6c 66 2d 63 68 65 63 6b 20 6f 6e  al self-check on
1aaa4 6c 79 20 2d 20 69 74 20 69 73 20 6e 6f 74 20 61  ly - it is not a
1aaa5 6e 20 65 73 73 65 6e 74 69 61 6c 20 70 72 6f 63  n essential proc
1aaa6 65 73 73 69 6e 67 0a 2a 2a 20 73 74 65 70 2e 0a  essing.** step..
1aaa7 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20  **.** This is a 
1aaa8 6e 6f 2d 6f 70 20 69 66 20 4e 44 45 42 55 47 20  no-op if NDEBUG 
1aaa9 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23  is defined..*/.#
1aaaa 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 74  ifndef NDEBUG.st
1aaab 61 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 41  atic void checkA
1aaac 63 74 69 76 65 56 64 62 65 43 6e 74 28 73 71 6c  ctiveVdbeCnt(sql
1aaad 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 56 64 62  ite3 *db){.  Vdb
1aaae 65 20 2a 70 3b 0a 20 20 69 6e 74 20 63 6e 74 20  e *p;.  int cnt 
1aaaf 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 57 72 69 74  = 0;.  int nWrit
1aab0 65 20 3d 20 30 3b 0a 20 20 70 20 3d 20 64 62 2d  e = 0;.  p = db-
1aab1 3e 70 56 64 62 65 3b 0a 20 20 77 68 69 6c 65 28  >pVdbe;.  while(
1aab2 20 70 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d   p ){.    if( p-
1aab3 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47  >magic==VDBE_MAG
1aab4 49 43 5f 52 55 4e 20 26 26 20 70 2d 3e 70 63 3e  IC_RUN && p->pc>
1aab5 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 6e 74 2b  =0 ){.      cnt+
1aab6 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e  +;.      if( p->
1aab7 72 65 61 64 4f 6e 6c 79 3d 3d 30 20 29 20 6e 57  readOnly==0 ) nW
1aab8 72 69 74 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20  rite++;.    }.  
1aab9 20 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a    p = p->pNext;.
1aaba 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 63 6e    }.  assert( cn
1aabb 74 3d 3d 64 62 2d 3e 61 63 74 69 76 65 56 64 62  t==db->activeVdb
1aabc 65 43 6e 74 20 29 3b 0a 20 20 61 73 73 65 72 74  eCnt );.  assert
1aabd 28 20 6e 57 72 69 74 65 3d 3d 64 62 2d 3e 77 72  ( nWrite==db->wr
1aabe 69 74 65 56 64 62 65 43 6e 74 20 29 3b 0a 7d 0a  iteVdbeCnt );.}.
1aabf 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 63 68  #else.#define ch
1aac0 65 63 6b 41 63 74 69 76 65 56 64 62 65 43 6e 74  eckActiveVdbeCnt
1aac1 28 78 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  (x).#endif../*.*
1aac2 2a 20 46 6f 72 20 65 76 65 72 79 20 42 74 72 65  * For every Btre
1aac3 65 20 74 68 61 74 20 69 6e 20 64 61 74 61 62 61  e that in databa
1aac4 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62  se connection db
1aac5 20 77 68 69 63 68 20 0a 2a 2a 20 68 61 73 20 62   which .** has b
1aac6 65 65 6e 20 6d 6f 64 69 66 69 65 64 2c 20 22 74  een modified, "t
1aac7 72 69 70 22 20 6f 72 20 69 6e 76 61 6c 69 64 61  rip" or invalida
1aac8 74 65 20 65 61 63 68 20 63 75 72 73 6f 72 20 69  te each cursor i
1aac9 6e 0a 2a 2a 20 74 68 61 74 20 42 74 72 65 65 20  n.** that Btree 
1aaca 6d 69 67 68 74 20 68 61 76 65 20 62 65 65 6e 20  might have been 
1aacb 6d 6f 64 69 66 69 65 64 20 73 6f 20 74 68 61 74  modified so that
1aacc 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 63   the cursor.** c
1aacd 61 6e 20 6e 65 76 65 72 20 62 65 20 75 73 65 64  an never be used
1aace 20 61 67 61 69 6e 2e 20 20 54 68 69 73 20 68 61   again.  This ha
1aacf 70 70 65 6e 73 20 77 68 65 6e 20 61 20 72 6f 6c  ppens when a rol
1aad0 6c 62 61 63 6b 0a 2a 2a 2a 20 6f 63 63 75 72 73  lback.*** occurs
1aad1 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20 74 72  .  We have to tr
1aad2 69 70 20 61 6c 6c 20 74 68 65 20 6f 74 68 65 72  ip all the other
1aad3 20 63 75 72 73 6f 72 73 2c 20 65 76 65 6e 0a 2a   cursors, even.*
1aad4 2a 20 63 75 72 73 6f 72 20 66 72 6f 6d 20 6f 74  * cursor from ot
1aad5 68 65 72 20 56 4d 73 20 69 6e 20 64 69 66 66 65  her VMs in diffe
1aad6 72 65 6e 74 20 64 61 74 61 62 61 73 65 20 63 6f  rent database co
1aad7 6e 6e 65 63 74 69 6f 6e 73 2c 0a 2a 2a 20 73 6f  nnections,.** so
1aad8 20 74 68 61 74 20 6e 6f 6e 65 20 6f 66 20 74 68   that none of th
1aad9 65 6d 20 74 72 79 20 74 6f 20 75 73 65 20 74 68  em try to use th
1aada 65 20 64 61 74 61 20 61 74 20 77 68 69 63 68 20  e data at which 
1aadb 74 68 65 79 0a 2a 2a 20 77 65 72 65 20 70 6f 69  they.** were poi
1aadc 6e 74 69 6e 67 20 61 6e 64 20 77 68 69 63 68 20  nting and which 
1aadd 6e 6f 77 20 6d 61 79 20 68 61 76 65 20 62 65 65  now may have bee
1aade 6e 20 63 68 61 6e 67 65 64 20 64 75 65 0a 2a 2a  n changed due.**
1aadf 20 74 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b   to the rollback
1aae0 2e 0a 2a 2a 0a 2a 2a 20 52 65 6d 65 6d 62 65 72  ..**.** Remember
1aae1 20 74 68 61 74 20 61 20 72 6f 6c 6c 62 61 63 6b   that a rollback
1aae2 20 63 61 6e 20 64 65 6c 65 74 65 20 74 61 62 6c   can delete tabl
1aae3 65 73 20 63 6f 6d 70 6c 65 74 65 20 61 6e 64 0a  es complete and.
1aae4 2a 2a 20 72 65 6f 72 64 65 72 20 72 6f 6f 74 70  ** reorder rootp
1aae5 61 67 65 73 2e 20 20 53 6f 20 69 74 20 69 73 20  ages.  So it is 
1aae6 6e 6f 74 20 73 75 66 66 69 63 69 65 6e 74 20 6a  not sufficient j
1aae7 75 73 74 20 74 6f 20 73 61 76 65 0a 2a 2a 20 74  ust to save.** t
1aae8 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20  he state of the 
1aae9 63 75 72 73 6f 72 2e 20 20 57 65 20 68 61 76 65  cursor.  We have
1aaea 20 74 6f 20 69 6e 76 61 6c 69 64 61 74 65 20 74   to invalidate t
1aaeb 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 73 6f 20  he cursor.** so 
1aaec 74 68 61 74 20 69 74 20 69 73 20 6e 65 76 65 72  that it is never
1aaed 20 75 73 65 64 20 61 67 61 69 6e 2e 0a 2a 2f 0a   used again..*/.
1aaee 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 76 61  static void inva
1aaef 6c 69 64 61 74 65 43 75 72 73 6f 72 73 4f 6e 4d  lidateCursorsOnM
1aaf0 6f 64 69 66 69 65 64 42 74 72 65 65 73 28 73 71  odifiedBtrees(sq
1aaf1 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e  lite3 *db){.  in
1aaf2 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  t i;.  for(i=0; 
1aaf3 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
1aaf4 0a 20 20 20 20 42 74 72 65 65 20 2a 70 20 3d 20  .    Btree *p = 
1aaf5 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a  db->aDb[i].pBt;.
1aaf6 20 20 20 20 69 66 28 20 70 20 26 26 20 73 71 6c      if( p && sql
1aaf7 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61  ite3BtreeIsInTra
1aaf8 6e 73 28 70 29 20 29 7b 0a 20 20 20 20 20 20 73  ns(p) ){.      s
1aaf9 71 6c 69 74 65 33 42 74 72 65 65 54 72 69 70 41  qlite3BtreeTripA
1aafa 6c 6c 43 75 72 73 6f 72 73 28 70 2c 20 53 51 4c  llCursors(p, SQL
1aafb 49 54 45 5f 41 42 4f 52 54 29 3b 0a 20 20 20 20  ITE_ABORT);.    
1aafc 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  }.  }.}../*.** I
1aafd 66 20 74 68 65 20 56 64 62 65 20 70 61 73 73 65  f the Vdbe passe
1aafe 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61  d as the first a
1aaff 72 67 75 6d 65 6e 74 20 6f 70 65 6e 65 64 20 61  rgument opened a
1ab00 20 73 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73   statement-trans
1ab01 61 63 74 69 6f 6e 2c 0a 2a 2a 20 63 6c 6f 73 65  action,.** close
1ab02 20 69 74 20 6e 6f 77 2e 20 41 72 67 75 6d 65 6e   it now. Argumen
1ab03 74 20 65 4f 70 20 6d 75 73 74 20 62 65 20 65 69  t eOp must be ei
1ab04 74 68 65 72 20 53 41 56 45 50 4f 49 4e 54 5f 52  ther SAVEPOINT_R
1ab05 4f 4c 4c 42 41 43 4b 20 6f 72 0a 2a 2a 20 53 41  OLLBACK or.** SA
1ab06 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e  VEPOINT_RELEASE.
1ab07 20 49 66 20 69 74 20 69 73 20 53 41 56 45 50 4f   If it is SAVEPO
1ab08 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 74 68  INT_ROLLBACK, th
1ab09 65 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  en the statement
1ab0a 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  .** transaction 
1ab0b 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20  is rolled back. 
1ab0c 49 66 20 65 4f 70 20 69 73 20 53 41 56 45 50 4f  If eOp is SAVEPO
1ab0d 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20 74 68 65  INT_RELEASE, the
1ab0e 6e 20 74 68 65 20 0a 2a 2a 20 73 74 61 74 65 6d  n the .** statem
1ab0f 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ent transaction 
1ab10 69 73 20 63 6f 6d 6d 74 74 65 64 2e 0a 2a 2a 0a  is commtted..**.
1ab11 2a 2a 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f  ** If an IO erro
1ab12 72 20 6f 63 63 75 72 73 2c 20 61 6e 20 53 51 4c  r occurs, an SQL
1ab13 49 54 45 5f 49 4f 45 52 52 5f 58 58 58 20 65 72  ITE_IOERR_XXX er
1ab14 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75  ror code is retu
1ab15 72 6e 65 64 2e 20 0a 2a 2a 20 4f 74 68 65 72 77  rned. .** Otherw
1ab16 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a  ise SQLITE_OK..*
1ab17 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1ab18 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
1ab19 43 6c 6f 73 65 53 74 61 74 65 6d 65 6e 74 28 56  CloseStatement(V
1ab1a 64 62 65 20 2a 70 2c 20 69 6e 74 20 65 4f 70 29  dbe *p, int eOp)
1ab1b 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 63 6f 6e  {.  sqlite3 *con
1ab1c 73 74 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20  st db = p->db;. 
1ab1d 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
1ab1e 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 2d  _OK;..  /* If p-
1ab1f 3e 69 53 74 61 74 65 6d 65 6e 74 20 69 73 20 67  >iStatement is g
1ab20 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f  reater than zero
1ab21 2c 20 74 68 65 6e 20 74 68 69 73 20 56 64 62 65  , then this Vdbe
1ab22 20 6f 70 65 6e 65 64 20 61 20 0a 20 20 2a 2a 20   opened a .  ** 
1ab23 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61  statement transa
1ab24 63 74 69 6f 6e 20 74 68 61 74 20 73 68 6f 75 6c  ction that shoul
1ab25 64 20 62 65 20 63 6c 6f 73 65 64 20 68 65 72 65  d be closed here
1ab26 2e 20 54 68 65 20 6f 6e 6c 79 20 65 78 63 65 70  . The only excep
1ab27 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 74 68 61  tion.  ** is tha
1ab28 74 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6d 61  t an IO error ma
1ab29 79 20 68 61 76 65 20 6f 63 63 75 72 65 64 2c 20  y have occured, 
1ab2a 63 61 75 73 69 6e 67 20 61 6e 20 65 6d 65 72 67  causing an emerg
1ab2b 65 6e 63 79 20 72 6f 6c 6c 62 61 63 6b 2e 0a 20  ency rollback.. 
1ab2c 20 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65   ** In this case
1ab2d 20 28 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74   (db->nStatement
1ab2e 3d 3d 30 29 2c 20 61 6e 64 20 74 68 65 72 65 20  ==0), and there 
1ab2f 69 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 64 6f  is nothing to do
1ab30 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d  ..  */.  if( db-
1ab31 3e 6e 53 74 61 74 65 6d 65 6e 74 20 26 26 20 70  >nStatement && p
1ab32 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 29 7b 0a  ->iStatement ){.
1ab33 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 63      int i;.    c
1ab34 6f 6e 73 74 20 69 6e 74 20 69 53 61 76 65 70 6f  onst int iSavepo
1ab35 69 6e 74 20 3d 20 70 2d 3e 69 53 74 61 74 65 6d  int = p->iStatem
1ab36 65 6e 74 2d 31 3b 0a 0a 20 20 20 20 61 73 73 65  ent-1;..    asse
1ab37 72 74 28 20 65 4f 70 3d 3d 53 41 56 45 50 4f 49  rt( eOp==SAVEPOI
1ab38 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 7c 7c 20 65  NT_ROLLBACK || e
1ab39 4f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45  Op==SAVEPOINT_RE
1ab3a 4c 45 41 53 45 29 3b 0a 20 20 20 20 61 73 73 65  LEASE);.    asse
1ab3b 72 74 28 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65  rt( db->nStateme
1ab3c 6e 74 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65  nt>0 );.    asse
1ab3d 72 74 28 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e  rt( p->iStatemen
1ab3e 74 3d 3d 28 64 62 2d 3e 6e 53 74 61 74 65 6d 65  t==(db->nStateme
1ab3f 6e 74 2b 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e  nt+db->nSavepoin
1ab40 74 29 20 29 3b 0a 0a 20 20 20 20 66 6f 72 28 69  t) );..    for(i
1ab41 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69  =0; i<db->nDb; i
1ab42 2b 2b 29 7b 20 0a 20 20 20 20 20 20 69 6e 74 20  ++){ .      int 
1ab43 72 63 32 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b  rc2 = SQLITE_OK;
1ab44 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 42  .      Btree *pB
1ab45 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70  t = db->aDb[i].p
1ab46 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42  Bt;.      if( pB
1ab47 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  t ){.        if(
1ab48 20 65 4f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f   eOp==SAVEPOINT_
1ab49 52 4f 4c 4c 42 41 43 4b 20 29 7b 0a 20 20 20 20  ROLLBACK ){.    
1ab4a 20 20 20 20 20 20 72 63 32 20 3d 20 73 71 6c 69        rc2 = sqli
1ab4b 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e  te3BtreeSavepoin
1ab4c 74 28 70 42 74 2c 20 53 41 56 45 50 4f 49 4e 54  t(pBt, SAVEPOINT
1ab4d 5f 52 4f 4c 4c 42 41 43 4b 2c 20 69 53 61 76 65  _ROLLBACK, iSave
1ab4e 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20  point);.        
1ab4f 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  }.        if( rc
1ab50 32 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  2==SQLITE_OK ){.
1ab51 20 20 20 20 20 20 20 20 20 20 72 63 32 20 3d 20            rc2 = 
1ab52 73 71 6c 69 74 65 33 42 74 72 65 65 53 61 76 65  sqlite3BtreeSave
1ab53 70 6f 69 6e 74 28 70 42 74 2c 20 53 41 56 45 50  point(pBt, SAVEP
1ab54 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20 69 53  OINT_RELEASE, iS
1ab55 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20  avepoint);.     
1ab56 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
1ab57 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1ab58 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  {.          rc =
1ab59 20 72 63 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a   rc2;.        }.
1ab5a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1ab5b 20 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74    db->nStatement
1ab5c 2d 2d 3b 0a 20 20 20 20 70 2d 3e 69 53 74 61 74  --;.    p->iStat
1ab5d 65 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 7d 0a 20  ement = 0;.  }. 
1ab5e 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1ab5f 2a 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65 20 69  *.** If SQLite i
1ab60 73 20 63 6f 6d 70 69 6c 65 64 20 74 6f 20 73 75  s compiled to su
1ab61 70 70 6f 72 74 20 73 68 61 72 65 64 2d 63 61 63  pport shared-cac
1ab62 68 65 20 6d 6f 64 65 20 61 6e 64 20 74 6f 20 62  he mode and to b
1ab63 65 20 74 68 72 65 61 64 73 61 66 65 2c 0a 2a 2a  e threadsafe,.**
1ab64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 62   this routine ob
1ab65 74 61 69 6e 73 20 74 68 65 20 6d 75 74 65 78 20  tains the mutex 
1ab66 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
1ab67 65 61 63 68 20 42 74 53 68 61 72 65 64 20 73 74  each BtShared st
1ab68 72 75 63 74 75 72 65 0a 2a 2a 20 74 68 61 74 20  ructure.** that 
1ab69 6d 61 79 20 62 65 20 61 63 63 65 73 73 65 64 20  may be accessed 
1ab6a 62 79 20 74 68 65 20 56 4d 20 70 61 73 73 65 64  by the VM passed
1ab6b 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e   as an argument.
1ab6c 20 49 6e 20 64 6f 69 6e 67 20 73 6f 20 69 74 0a   In doing so it.
1ab6d 2a 2a 20 73 65 74 73 20 74 68 65 20 42 74 53 68  ** sets the BtSh
1ab6e 61 72 65 64 2e 64 62 20 6d 65 6d 62 65 72 20 6f  ared.db member o
1ab6f 66 20 65 61 63 68 20 6f 66 20 74 68 65 20 42 74  f each of the Bt
1ab70 53 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65  Shared structure
1ab71 73 2c 20 65 6e 73 75 72 69 6e 67 0a 2a 2a 20 74  s, ensuring.** t
1ab72 68 61 74 20 74 68 65 20 63 6f 72 72 65 63 74 20  hat the correct 
1ab73 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 63 61 6c  busy-handler cal
1ab74 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64  lback is invoked
1ab75 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a   if required..**
1ab76 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65 20 69 73  .** If SQLite is
1ab77 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66 65 20   not threadsafe 
1ab78 62 75 74 20 64 6f 65 73 20 73 75 70 70 6f 72 74  but does support
1ab79 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f   shared-cache mo
1ab7a 64 65 2c 20 74 68 65 6e 0a 2a 2a 20 73 71 6c 69  de, then.** sqli
1ab7b 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c  te3BtreeEnterAll
1ab7c 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f  () is invoked to
1ab7d 20 73 65 74 20 74 68 65 20 42 74 53 68 61 72 65   set the BtShare
1ab7e 64 2e 64 62 20 76 61 72 69 61 62 6c 65 73 0a 2a  d.db variables.*
1ab7f 2a 20 6f 66 20 61 6c 6c 20 6f 66 20 42 74 53 68  * of all of BtSh
1ab80 61 72 65 64 20 73 74 72 75 63 74 75 72 65 73 20  ared structures 
1ab81 61 63 63 65 73 73 69 62 6c 65 20 76 69 61 20 74  accessible via t
1ab82 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  he database hand
1ab83 6c 65 20 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65  le .** associate
1ab84 64 20 77 69 74 68 20 74 68 65 20 56 4d 2e 20 4f  d with the VM. O
1ab85 66 20 63 6f 75 72 73 65 20 6f 6e 6c 79 20 61 20  f course only a 
1ab86 73 75 62 73 65 74 20 6f 66 20 74 68 65 73 65 20  subset of these 
1ab87 73 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 77 69  structures.** wi
1ab88 6c 6c 20 62 65 20 61 63 63 65 73 73 65 64 20 62  ll be accessed b
1ab89 79 20 74 68 65 20 56 4d 2c 20 61 6e 64 20 77 65  y the VM, and we
1ab8a 20 63 6f 75 6c 64 20 75 73 65 20 56 64 62 65 2e   could use Vdbe.
1ab8b 62 74 72 65 65 4d 61 73 6b 20 74 6f 20 66 69 67  btreeMask to fig
1ab8c 75 72 65 0a 2a 2a 20 74 68 61 74 20 73 75 62 73  ure.** that subs
1ab8d 65 74 20 6f 75 74 2c 20 62 75 74 20 74 68 65 72  et out, but ther
1ab8e 65 20 69 73 20 6e 6f 20 61 64 76 61 6e 74 61 67  e is no advantag
1ab8f 65 20 74 6f 20 64 6f 69 6e 67 20 73 6f 2e 0a 2a  e to doing so..*
1ab90 2a 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65 20 69  *.** If SQLite i
1ab91 73 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66 65  s not threadsafe
1ab92 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 73 75   and does not su
1ab93 70 70 6f 72 74 20 73 68 61 72 65 64 2d 63 61 63  pport shared-cac
1ab94 68 65 20 6d 6f 64 65 2c 20 74 68 69 73 0a 2a 2a  he mode, this.**
1ab95 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e   function is a n
1ab96 6f 2d 6f 70 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  o-op..*/.#ifndef
1ab97 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
1ab98 52 45 44 5f 43 41 43 48 45 0a 53 51 4c 49 54 45  RED_CACHE.SQLITE
1ab99 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1ab9a 6c 69 74 65 33 56 64 62 65 4d 75 74 65 78 41 72  lite3VdbeMutexAr
1ab9b 72 61 79 45 6e 74 65 72 28 56 64 62 65 20 2a 70  rayEnter(Vdbe *p
1ab9c 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  ){.#if SQLITE_TH
1ab9d 52 45 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74  READSAFE.  sqlit
1ab9e 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61  e3BtreeMutexArra
1ab9f 79 45 6e 74 65 72 28 26 70 2d 3e 61 4d 75 74 65  yEnter(&p->aMute
1aba0 78 29 3b 0a 23 65 6c 73 65 0a 20 20 73 71 6c 69  x);.#else.  sqli
1aba1 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c  te3BtreeEnterAll
1aba2 28 70 2d 3e 64 62 29 3b 0a 23 65 6e 64 69 66 0a  (p->db);.#endif.
1aba3 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
1aba4 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
1aba5 63 61 6c 6c 65 64 20 74 68 65 20 77 68 65 6e 20  called the when 
1aba6 61 20 56 44 42 45 20 74 72 69 65 73 20 74 6f 20  a VDBE tries to 
1aba7 68 61 6c 74 2e 20 20 49 66 20 74 68 65 20 56 44  halt.  If the VD
1aba8 42 45 0a 2a 2a 20 68 61 73 20 6d 61 64 65 20 63  BE.** has made c
1aba9 68 61 6e 67 65 73 20 61 6e 64 20 69 73 20 69 6e  hanges and is in
1abaa 20 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65   autocommit mode
1abab 2c 20 74 68 65 6e 20 63 6f 6d 6d 69 74 20 74 68  , then commit th
1abac 6f 73 65 0a 2a 2a 20 63 68 61 6e 67 65 73 2e 20  ose.** changes. 
1abad 20 49 66 20 61 20 72 6f 6c 6c 62 61 63 6b 20 69   If a rollback i
1abae 73 20 6e 65 65 64 65 64 2c 20 74 68 65 6e 20 64  s needed, then d
1abaf 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 0a  o the rollback..
1abb0 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
1abb1 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 77  ne is the only w
1abb2 61 79 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 73  ay to move the s
1abb3 74 61 74 65 20 6f 66 20 61 20 56 4d 20 66 72 6f  tate of a VM fro
1abb4 6d 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 47 49  m.** SQLITE_MAGI
1abb5 43 5f 52 55 4e 20 74 6f 20 53 51 4c 49 54 45 5f  C_RUN to SQLITE_
1abb6 4d 41 47 49 43 5f 48 41 4c 54 2e 20 20 49 74 20  MAGIC_HALT.  It 
1abb7 69 73 20 68 61 72 6d 6c 65 73 73 20 74 6f 0a 2a  is harmless to.*
1abb8 2a 20 63 61 6c 6c 20 74 68 69 73 20 6f 6e 20 61  * call this on a
1abb9 20 56 4d 20 74 68 61 74 20 69 73 20 69 6e 20 74   VM that is in t
1abba 68 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f  he SQLITE_MAGIC_
1abbb 48 41 4c 54 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a  HALT state..**.*
1abbc 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f  * Return an erro
1abbd 72 20 63 6f 64 65 2e 20 20 49 66 20 74 68 65 20  r code.  If the 
1abbe 63 6f 6d 6d 69 74 20 63 6f 75 6c 64 20 6e 6f 74  commit could not
1abbf 20 63 6f 6d 70 6c 65 74 65 20 62 65 63 61 75 73   complete becaus
1abc0 65 20 6f 66 0a 2a 2a 20 6c 6f 63 6b 20 63 6f 6e  e of.** lock con
1abc1 74 65 6e 74 69 6f 6e 2c 20 72 65 74 75 72 6e 20  tention, return 
1abc2 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 49 66  SQLITE_BUSY.  If
1abc3 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20   SQLITE_BUSY is 
1abc4 72 65 74 75 72 6e 65 64 2c 20 69 74 0a 2a 2a 20  returned, it.** 
1abc5 6d 65 61 6e 73 20 74 68 65 20 63 6c 6f 73 65 20  means the close 
1abc6 64 69 64 20 6e 6f 74 20 68 61 70 70 65 6e 20 61  did not happen a
1abc7 6e 64 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72  nd needs to be r
1abc8 65 70 65 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49  epeated..*/.SQLI
1abc9 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1abca 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28 56  qlite3VdbeHalt(V
1abcb 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72  dbe *p){.  int r
1abcc 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
1abcd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73             /* Us
1abce 65 64 20 74 6f 20 73 74 6f 72 65 20 74 72 61 6e  ed to store tran
1abcf 73 69 65 6e 74 20 72 65 74 75 72 6e 20 63 6f 64  sient return cod
1abd0 65 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  es */.  sqlite3 
1abd1 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20  *db = p->db;..  
1abd2 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  /* This function
1abd3 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6c 6f   contains the lo
1abd4 67 69 63 20 74 68 61 74 20 64 65 74 65 72 6d 69  gic that determi
1abd5 6e 65 73 20 69 66 20 61 20 73 74 61 74 65 6d 65  nes if a stateme
1abd6 6e 74 20 6f 72 0a 20 20 2a 2a 20 74 72 61 6e 73  nt or.  ** trans
1abd7 61 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 63  action will be c
1abd8 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c  ommitted or roll
1abd9 65 64 20 62 61 63 6b 20 61 73 20 61 20 72 65 73  ed back as a res
1abda 75 6c 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20  ult of the.  ** 
1abdb 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 69  execution of thi
1abdc 73 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e  s virtual machin
1abdd 65 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66  e. .  **.  ** If
1abde 20 61 6e 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c   any of the foll
1abdf 6f 77 69 6e 67 20 65 72 72 6f 72 73 20 6f 63 63  owing errors occ
1abe0 75 72 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  ur:.  **.  **   
1abe1 20 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 20    SQLITE_NOMEM. 
1abe2 20 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 49   **     SQLITE_I
1abe3 4f 45 52 52 0a 20 20 2a 2a 20 20 20 20 20 53 51  OERR.  **     SQ
1abe4 4c 49 54 45 5f 46 55 4c 4c 0a 20 20 2a 2a 20 20  LITE_FULL.  **  
1abe5 20 20 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52     SQLITE_INTERR
1abe6 55 50 54 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  UPT.  **.  ** Th
1abe7 65 6e 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  en the internal 
1abe8 63 61 63 68 65 20 6d 69 67 68 74 20 68 61 76 65  cache might have
1abe9 20 62 65 65 6e 20 6c 65 66 74 20 69 6e 20 61 6e   been left in an
1abea 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 0a 20 20   inconsistent.  
1abeb 2a 2a 20 73 74 61 74 65 2e 20 20 57 65 20 6e 65  ** state.  We ne
1abec 65 64 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74  ed to rollback t
1abed 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61  he statement tra
1abee 6e 73 61 63 74 69 6f 6e 2c 20 69 66 20 74 68 65  nsaction, if the
1abef 72 65 20 69 73 0a 20 20 2a 2a 20 6f 6e 65 2c 20  re is.  ** one, 
1abf0 6f 72 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20  or the complete 
1abf1 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 66 20 74  transaction if t
1abf2 68 65 72 65 20 69 73 20 6e 6f 20 73 74 61 74 65  here is no state
1abf3 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  ment transaction
1abf4 2e 0a 20 20 2a 2f 0a 0a 20 20 69 66 28 20 70 2d  ..  */..  if( p-
1abf5 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
1abf6 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d  d ){.    p->rc =
1abf7 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
1abf8 20 7d 0a 20 20 63 6c 6f 73 65 41 6c 6c 43 75 72   }.  closeAllCur
1abf9 73 6f 72 73 28 70 29 3b 0a 20 20 69 66 28 20 70  sors(p);.  if( p
1abfa 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d 41  ->magic!=VDBE_MA
1abfb 47 49 43 5f 52 55 4e 20 29 7b 0a 20 20 20 20 72  GIC_RUN ){.    r
1abfc 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1abfd 0a 20 20 7d 0a 20 20 63 68 65 63 6b 41 63 74 69  .  }.  checkActi
1abfe 76 65 56 64 62 65 43 6e 74 28 64 62 29 3b 0a 0a  veVdbeCnt(db);..
1abff 20 20 2f 2a 20 4e 6f 20 63 6f 6d 6d 69 74 20 6f    /* No commit o
1ac00 72 20 72 6f 6c 6c 62 61 63 6b 20 6e 65 65 64 65  r rollback neede
1ac01 64 20 69 66 20 74 68 65 20 70 72 6f 67 72 61 6d  d if the program
1ac02 20 6e 65 76 65 72 20 73 74 61 72 74 65 64 20 2a   never started *
1ac03 2f 0a 20 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30  /.  if( p->pc>=0
1ac04 20 29 7b 0a 20 20 20 20 69 6e 74 20 6d 72 63 3b   ){.    int mrc;
1ac05 20 20 20 2f 2a 20 50 72 69 6d 61 72 79 20 65 72     /* Primary er
1ac06 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 70 2d  ror code from p-
1ac07 3e 72 63 20 2a 2f 0a 20 20 20 20 69 6e 74 20 65  >rc */.    int e
1ac08 53 74 61 74 65 6d 65 6e 74 4f 70 20 3d 20 30 3b  StatementOp = 0;
1ac09 0a 20 20 20 20 69 6e 74 20 69 73 53 70 65 63 69  .    int isSpeci
1ac0a 61 6c 45 72 72 6f 72 3b 20 20 20 20 20 20 20 20  alError;        
1ac0b 20 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 74 72      /* Set to tr
1ac0c 75 65 20 69 66 20 61 20 27 73 70 65 63 69 61 6c  ue if a 'special
1ac0d 27 20 65 72 72 6f 72 20 2a 2f 0a 0a 20 20 20 20  ' error */..    
1ac0e 2f 2a 20 4c 6f 63 6b 20 61 6c 6c 20 62 74 72 65  /* Lock all btre
1ac0f 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20 73  es used by the s
1ac10 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 20  tatement */.    
1ac11 73 71 6c 69 74 65 33 56 64 62 65 4d 75 74 65 78  sqlite3VdbeMutex
1ac12 41 72 72 61 79 45 6e 74 65 72 28 70 29 3b 0a 0a  ArrayEnter(p);..
1ac13 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72      /* Check for
1ac14 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 70 65 63   one of the spec
1ac15 69 61 6c 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20  ial errors */.  
1ac16 20 20 6d 72 63 20 3d 20 70 2d 3e 72 63 20 26 20    mrc = p->rc & 
1ac17 30 78 66 66 3b 0a 20 20 20 20 61 73 73 65 72 74  0xff;.    assert
1ac18 28 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f  ( p->rc!=SQLITE_
1ac19 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 29 3b  IOERR_BLOCKED );
1ac1a 20 20 2f 2a 20 54 68 69 73 20 65 72 72 6f 72 20    /* This error 
1ac1b 6e 6f 20 6c 6f 6e 67 65 72 20 65 78 69 73 74 73  no longer exists
1ac1c 20 2a 2f 0a 20 20 20 20 69 73 53 70 65 63 69 61   */.    isSpecia
1ac1d 6c 45 72 72 6f 72 20 3d 20 6d 72 63 3d 3d 53 51  lError = mrc==SQ
1ac1e 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 6d 72  LITE_NOMEM || mr
1ac1f 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a  c==SQLITE_IOERR.
1ac20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ac21 20 20 20 20 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c       || mrc==SQL
1ac22 49 54 45 5f 49 4e 54 45 52 52 55 50 54 20 7c 7c  ITE_INTERRUPT ||
1ac23 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 46 55 4c   mrc==SQLITE_FUL
1ac24 4c 3b 0a 20 20 20 20 69 66 28 20 69 73 53 70 65  L;.    if( isSpe
1ac25 63 69 61 6c 45 72 72 6f 72 20 29 7b 0a 20 20 20  cialError ){.   
1ac26 20 20 20 2f 2a 20 49 66 20 74 68 65 20 71 75 65     /* If the que
1ac27 72 79 20 77 61 73 20 72 65 61 64 2d 6f 6e 6c 79  ry was read-only
1ac28 2c 20 77 65 20 6e 65 65 64 20 64 6f 20 6e 6f 20  , we need do no 
1ac29 72 6f 6c 6c 62 61 63 6b 20 61 74 20 61 6c 6c 2e  rollback at all.
1ac2a 20 4f 74 68 65 72 77 69 73 65 2c 0a 20 20 20 20   Otherwise,.    
1ac2b 20 20 2a 2a 20 70 72 6f 63 65 65 64 20 77 69 74    ** proceed wit
1ac2c 68 20 74 68 65 20 73 70 65 63 69 61 6c 20 68 61  h the special ha
1ac2d 6e 64 6c 69 6e 67 2e 0a 20 20 20 20 20 20 2a 2f  ndling..      */
1ac2e 0a 20 20 20 20 20 20 69 66 28 20 21 70 2d 3e 72  .      if( !p->r
1ac2f 65 61 64 4f 6e 6c 79 20 7c 7c 20 6d 72 63 21 3d  eadOnly || mrc!=
1ac30 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54  SQLITE_INTERRUPT
1ac31 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
1ac32 28 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  (mrc==SQLITE_NOM
1ac33 45 4d 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54  EM || mrc==SQLIT
1ac34 45 5f 46 55 4c 4c 29 20 26 26 20 70 2d 3e 75 73  E_FULL) && p->us
1ac35 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 29 7b  esStmtJournal ){
1ac36 0a 20 20 20 20 20 20 20 20 20 20 65 53 74 61 74  .          eStat
1ac37 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f  ementOp = SAVEPO
1ac38 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20  INT_ROLLBACK;.  
1ac39 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1ac3a 20 20 20 20 20 20 20 2f 2a 20 57 65 20 61 72 65         /* We are
1ac3b 20 66 6f 72 63 65 64 20 74 6f 20 72 6f 6c 6c 20   forced to roll 
1ac3c 62 61 63 6b 20 74 68 65 20 61 63 74 69 76 65 20  back the active 
1ac3d 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 42 65 66  transaction. Bef
1ac3e 6f 72 65 20 64 6f 69 6e 67 0a 20 20 20 20 20 20  ore doing.      
1ac3f 20 20 20 20 2a 2a 20 73 6f 2c 20 61 62 6f 72 74      ** so, abort
1ac40 20 61 6e 79 20 6f 74 68 65 72 20 73 74 61 74 65   any other state
1ac41 6d 65 6e 74 73 20 74 68 69 73 20 68 61 6e 64 6c  ments this handl
1ac42 65 20 63 75 72 72 65 6e 74 6c 79 20 68 61 73 20  e currently has 
1ac43 61 63 74 69 76 65 2e 0a 20 20 20 20 20 20 20 20  active..        
1ac44 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69    */.          i
1ac45 6e 76 61 6c 69 64 61 74 65 43 75 72 73 6f 72 73  nvalidateCursors
1ac46 4f 6e 4d 6f 64 69 66 69 65 64 42 74 72 65 65 73  OnModifiedBtrees
1ac47 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20  (db);.          
1ac48 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41  sqlite3RollbackA
1ac49 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20  ll(db);.        
1ac4a 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61    sqlite3CloseSa
1ac4b 76 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a 20 20  vepoints(db);.  
1ac4c 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f          db->auto
1ac4d 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20  Commit = 1;.    
1ac4e 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
1ac4f 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66    }.  .    /* If
1ac50 20 74 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74   the auto-commit
1ac51 20 66 6c 61 67 20 69 73 20 73 65 74 20 61 6e 64   flag is set and
1ac52 20 74 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c   this is the onl
1ac53 79 20 61 63 74 69 76 65 20 77 72 69 74 65 72 20  y active writer 
1ac54 0a 20 20 20 20 2a 2a 20 56 4d 2c 20 74 68 65 6e  .    ** VM, then
1ac55 20 77 65 20 64 6f 20 65 69 74 68 65 72 20 61 20   we do either a 
1ac56 63 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c 6c 62 61  commit or rollba
1ac57 63 6b 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e  ck of the curren
1ac58 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 0a  t transaction. .
1ac59 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f      **.    ** No
1ac5a 74 65 3a 20 54 68 69 73 20 62 6c 6f 63 6b 20 61  te: This block a
1ac5b 6c 73 6f 20 72 75 6e 73 20 69 66 20 6f 6e 65 20  lso runs if one 
1ac5c 6f 66 20 74 68 65 20 73 70 65 63 69 61 6c 20 65  of the special e
1ac5d 72 72 6f 72 73 20 68 61 6e 64 6c 65 64 20 0a 20  rrors handled . 
1ac5e 20 20 20 2a 2a 20 61 62 6f 76 65 20 68 61 73 20     ** above has 
1ac5f 6f 63 63 75 72 72 65 64 2e 20 0a 20 20 20 20 2a  occurred. .    *
1ac60 2f 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74  /.    if( !sqlit
1ac61 65 33 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29  e3VtabInSync(db)
1ac62 20 0a 20 20 20 20 20 26 26 20 64 62 2d 3e 61 75   .     && db->au
1ac63 74 6f 43 6f 6d 6d 69 74 20 0a 20 20 20 20 20 26  toCommit .     &
1ac64 26 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43  & db->writeVdbeC
1ac65 6e 74 3d 3d 28 70 2d 3e 72 65 61 64 4f 6e 6c 79  nt==(p->readOnly
1ac66 3d 3d 30 29 20 0a 20 20 20 20 29 7b 0a 20 20 20  ==0) .    ){.   
1ac67 20 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51     if( p->rc==SQ
1ac68 4c 49 54 45 5f 4f 4b 20 7c 7c 20 28 70 2d 3e 65  LITE_OK || (p->e
1ac69 72 72 6f 72 41 63 74 69 6f 6e 3d 3d 4f 45 5f 46  rrorAction==OE_F
1ac6a 61 69 6c 20 26 26 20 21 69 73 53 70 65 63 69 61  ail && !isSpecia
1ac6b 6c 45 72 72 6f 72 29 20 29 7b 0a 20 20 20 20 20  lError) ){.     
1ac6c 20 20 20 2f 2a 20 54 68 65 20 61 75 74 6f 2d 63     /* The auto-c
1ac6d 6f 6d 6d 69 74 20 66 6c 61 67 20 69 73 20 74 72  ommit flag is tr
1ac6e 75 65 2c 20 61 6e 64 20 74 68 65 20 76 64 62 65  ue, and the vdbe
1ac6f 20 70 72 6f 67 72 61 6d 20 77 61 73 20 0a 20 20   program was .  
1ac70 20 20 20 20 20 20 2a 2a 20 73 75 63 63 65 73 73        ** success
1ac71 66 75 6c 20 6f 72 20 68 69 74 20 61 6e 20 27 4f  ful or hit an 'O
1ac72 52 20 46 41 49 4c 27 20 63 6f 6e 73 74 72 61 69  R FAIL' constrai
1ac73 6e 74 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 61  nt. This means a
1ac74 20 63 6f 6d 6d 69 74 20 0a 20 20 20 20 20 20 20   commit .       
1ac75 20 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64 2e   ** is required.
1ac76 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
1ac77 20 20 20 20 72 63 20 3d 20 76 64 62 65 43 6f 6d      rc = vdbeCom
1ac78 6d 69 74 28 64 62 2c 20 70 29 3b 0a 20 20 20 20  mit(db, p);.    
1ac79 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1ac7a 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20  TE_BUSY ){.     
1ac7b 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
1ac7c 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76 65  eMutexArrayLeave
1ac7d 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b 0a 20 20  (&p->aMutex);.  
1ac7e 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
1ac7f 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20  QLITE_BUSY;.    
1ac80 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63      }else if( rc
1ac81 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1ac82 20 20 20 20 20 20 20 20 20 70 2d 3e 72 63 20 3d           p->rc =
1ac83 20 72 63 3b 0a 20 20 20 20 20 20 20 20 20 20 73   rc;.          s
1ac84 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c  qlite3RollbackAl
1ac85 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 7d  l(db);.        }
1ac86 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
1ac87 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 49 6e 74  sqlite3CommitInt
1ac88 65 72 6e 61 6c 43 68 61 6e 67 65 73 28 64 62 29  ernalChanges(db)
1ac89 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1ac8a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1ac8b 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b   sqlite3Rollback
1ac8c 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 7d  All(db);.      }
1ac8d 0a 20 20 20 20 20 20 64 62 2d 3e 6e 53 74 61 74  .      db->nStat
1ac8e 65 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20 7d  ement = 0;.    }
1ac8f 65 6c 73 65 20 69 66 28 20 65 53 74 61 74 65 6d  else if( eStatem
1ac90 65 6e 74 4f 70 3d 3d 30 20 29 7b 0a 20 20 20 20  entOp==0 ){.    
1ac91 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c    if( p->rc==SQL
1ac92 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 65 72 72  ITE_OK || p->err
1ac93 6f 72 41 63 74 69 6f 6e 3d 3d 4f 45 5f 46 61 69  orAction==OE_Fai
1ac94 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 65 53 74  l ){.        eSt
1ac95 61 74 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45  atementOp = SAVE
1ac96 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 3b 0a 20  POINT_RELEASE;. 
1ac97 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70       }else if( p
1ac98 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e 3d 3d 4f  ->errorAction==O
1ac99 45 5f 41 62 6f 72 74 20 29 7b 0a 20 20 20 20 20  E_Abort ){.     
1ac9a 20 20 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 20     eStatementOp 
1ac9b 3d 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c  = SAVEPOINT_ROLL
1ac9c 42 41 43 4b 3b 0a 20 20 20 20 20 20 7d 65 6c 73  BACK;.      }els
1ac9d 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 76 61 6c  e{.        inval
1ac9e 69 64 61 74 65 43 75 72 73 6f 72 73 4f 6e 4d 6f  idateCursorsOnMo
1ac9f 64 69 66 69 65 64 42 74 72 65 65 73 28 64 62 29  difiedBtrees(db)
1aca0 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
1aca1 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29  3RollbackAll(db)
1aca2 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
1aca3 33 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e 74 73  3CloseSavepoints
1aca4 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 64 62  (db);.        db
1aca5 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31  ->autoCommit = 1
1aca6 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1aca7 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 65 53 74    .    /* If eSt
1aca8 61 74 65 6d 65 6e 74 4f 70 20 69 73 20 6e 6f 6e  atementOp is non
1aca9 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 20 73 74  -zero, then a st
1acaa 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74  atement transact
1acab 69 6f 6e 20 6e 65 65 64 73 20 74 6f 0a 20 20 20  ion needs to.   
1acac 20 2a 2a 20 62 65 20 63 6f 6d 6d 69 74 74 65 64   ** be committed
1acad 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e   or rolled back.
1acae 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 56 64 62   Call sqlite3Vdb
1acaf 65 43 6c 6f 73 65 53 74 61 74 65 6d 65 6e 74 28  eCloseStatement(
1acb0 29 20 74 6f 0a 20 20 20 20 2a 2a 20 64 6f 20 73  ) to.    ** do s
1acb1 6f 2e 20 49 66 20 74 68 69 73 20 6f 70 65 72 61  o. If this opera
1acb2 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 6e 20  tion returns an 
1acb3 65 72 72 6f 72 2c 20 61 6e 64 20 74 68 65 20 63  error, and the c
1acb4 75 72 72 65 6e 74 20 73 74 61 74 65 6d 65 6e 74  urrent statement
1acb5 0a 20 20 20 20 2a 2a 20 65 72 72 6f 72 20 63 6f  .    ** error co
1acb6 64 65 20 69 73 20 53 51 4c 49 54 45 5f 4f 4b 20  de is SQLITE_OK 
1acb7 6f 72 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52  or SQLITE_CONSTR
1acb8 41 49 4e 54 2c 20 74 68 65 6e 20 73 65 74 20 74  AINT, then set t
1acb9 68 65 20 65 72 72 6f 72 0a 20 20 20 20 2a 2a 20  he error.    ** 
1acba 63 6f 64 65 20 74 6f 20 74 68 65 20 6e 65 77 20  code to the new 
1acbb 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  value..    */.  
1acbc 20 20 69 66 28 20 65 53 74 61 74 65 6d 65 6e 74    if( eStatement
1acbd 4f 70 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  Op ){.      rc =
1acbe 20 73 71 6c 69 74 65 33 56 64 62 65 43 6c 6f 73   sqlite3VdbeClos
1acbf 65 53 74 61 74 65 6d 65 6e 74 28 70 2c 20 65 53  eStatement(p, eS
1acc0 74 61 74 65 6d 65 6e 74 4f 70 29 3b 0a 20 20 20  tatementOp);.   
1acc1 20 20 20 69 66 28 20 72 63 20 26 26 20 28 70 2d     if( rc && (p-
1acc2 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c  >rc==SQLITE_OK |
1acc3 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f  | p->rc==SQLITE_
1acc4 43 4f 4e 53 54 52 41 49 4e 54 29 20 29 7b 0a 20  CONSTRAINT) ){. 
1acc5 20 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 72         p->rc = r
1acc6 63 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  c;.        sqlit
1acc7 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e  e3DbFree(db, p->
1acc8 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20  zErrMsg);.      
1acc9 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30    p->zErrMsg = 0
1acca 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1accb 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69    .    /* If thi
1accc 73 20 77 61 73 20 61 6e 20 49 4e 53 45 52 54 2c  s was an INSERT,
1accd 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54   UPDATE or DELET
1acce 45 20 61 6e 64 20 6e 6f 20 73 74 61 74 65 6d 65  E and no stateme
1accf 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20  nt transaction. 
1acd0 20 20 20 2a 2a 20 68 61 73 20 62 65 65 6e 20 72     ** has been r
1acd1 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 75 70 64 61  olled back, upda
1acd2 74 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  te the database 
1acd3 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 68 61 6e 67  connection chang
1acd4 65 2d 63 6f 75 6e 74 65 72 2e 20 0a 20 20 20 20  e-counter. .    
1acd5 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 63 68  */.    if( p->ch
1acd6 61 6e 67 65 43 6e 74 4f 6e 20 29 7b 0a 20 20 20  angeCntOn ){.   
1acd7 20 20 20 69 66 28 20 65 53 74 61 74 65 6d 65 6e     if( eStatemen
1acd8 74 4f 70 21 3d 53 41 56 45 50 4f 49 4e 54 5f 52  tOp!=SAVEPOINT_R
1acd9 4f 4c 4c 42 41 43 4b 20 29 7b 0a 20 20 20 20 20  OLLBACK ){.     
1acda 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
1acdb 74 43 68 61 6e 67 65 73 28 64 62 2c 20 70 2d 3e  tChanges(db, p->
1acdc 6e 43 68 61 6e 67 65 29 3b 0a 20 20 20 20 20 20  nChange);.      
1acdd 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73  }else{.        s
1acde 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 61  qlite3VdbeSetCha
1acdf 6e 67 65 73 28 64 62 2c 20 30 29 3b 0a 20 20 20  nges(db, 0);.   
1ace0 20 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e 6e 43     }.      p->nC
1ace1 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 20 20 7d  hange = 0;.    }
1ace2 0a 20 20 0a 20 20 20 20 2f 2a 20 52 6f 6c 6c 62  .  .    /* Rollb
1ace3 61 63 6b 20 6f 72 20 63 6f 6d 6d 69 74 20 61 6e  ack or commit an
1ace4 79 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73  y schema changes
1ace5 20 74 68 61 74 20 6f 63 63 75 72 72 65 64 2e 20   that occurred. 
1ace6 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 72 63  */.    if( p->rc
1ace7 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 64  !=SQLITE_OK && d
1ace8 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f  b->flags&SQLITE_
1ace9 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 20 29 7b  InternChanges ){
1acea 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65  .      sqlite3Re
1aceb 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d  setInternalSchem
1acec 61 28 64 62 2c 20 30 29 3b 0a 20 20 20 20 20 20  a(db, 0);.      
1aced 64 62 2d 3e 66 6c 61 67 73 20 3d 20 28 64 62 2d  db->flags = (db-
1acee 3e 66 6c 61 67 73 20 7c 20 53 51 4c 49 54 45 5f  >flags | SQLITE_
1acef 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 29 3b 0a  InternChanges);.
1acf0 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65      }..    /* Re
1acf1 6c 65 61 73 65 20 74 68 65 20 6c 6f 63 6b 73 20  lease the locks 
1acf2 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  */.    sqlite3Bt
1acf3 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61  reeMutexArrayLea
1acf4 76 65 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b 0a  ve(&p->aMutex);.
1acf5 20 20 7d 0a 0a 20 20 2f 2a 20 57 65 20 68 61 76    }..  /* We hav
1acf6 65 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 68  e successfully h
1acf7 61 6c 74 65 64 20 61 6e 64 20 63 6c 6f 73 65 64  alted and closed
1acf8 20 74 68 65 20 56 4d 2e 20 20 52 65 63 6f 72 64   the VM.  Record
1acf9 20 74 68 69 73 20 66 61 63 74 2e 20 2a 2f 0a 20   this fact. */. 
1acfa 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30 20 29 7b   if( p->pc>=0 ){
1acfb 0a 20 20 20 20 64 62 2d 3e 61 63 74 69 76 65 56  .    db->activeV
1acfc 64 62 65 43 6e 74 2d 2d 3b 0a 20 20 20 20 69 66  dbeCnt--;.    if
1acfd 28 20 21 70 2d 3e 72 65 61 64 4f 6e 6c 79 20 29  ( !p->readOnly )
1acfe 7b 0a 20 20 20 20 20 20 64 62 2d 3e 77 72 69 74  {.      db->writ
1acff 65 56 64 62 65 43 6e 74 2d 2d 3b 0a 20 20 20 20  eVdbeCnt--;.    
1ad00 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 62  }.    assert( db
1ad01 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e  ->activeVdbeCnt>
1ad02 3d 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e  =db->writeVdbeCn
1ad03 74 20 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 6d 61  t );.  }.  p->ma
1ad04 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43  gic = VDBE_MAGIC
1ad05 5f 48 41 4c 54 3b 0a 20 20 63 68 65 63 6b 41 63  _HALT;.  checkAc
1ad06 74 69 76 65 56 64 62 65 43 6e 74 28 64 62 29 3b  tiveVdbeCnt(db);
1ad07 0a 20 20 69 66 28 20 70 2d 3e 64 62 2d 3e 6d 61  .  if( p->db->ma
1ad08 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
1ad09 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45    p->rc = SQLITE
1ad0a 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 2f  _NOMEM;.  }..  /
1ad0b 2a 20 49 66 20 74 68 65 20 61 75 74 6f 2d 63 6f  * If the auto-co
1ad0c 6d 6d 69 74 20 66 6c 61 67 20 69 73 20 73 65 74  mmit flag is set
1ad0d 20 74 6f 20 74 72 75 65 2c 20 74 68 65 6e 20 61   to true, then a
1ad0e 6e 79 20 6c 6f 63 6b 73 20 74 68 61 74 20 77 65  ny locks that we
1ad0f 72 65 20 68 65 6c 64 0a 20 20 2a 2a 20 62 79 20  re held.  ** by 
1ad10 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20 68 61  connection db ha
1ad11 76 65 20 6e 6f 77 20 62 65 65 6e 20 72 65 6c 65  ve now been rele
1ad12 61 73 65 64 2e 20 43 61 6c 6c 20 73 71 6c 69 74  ased. Call sqlit
1ad13 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f  e3ConnectionUnlo
1ad14 63 6b 65 64 28 29 20 0a 20 20 2a 2a 20 74 6f 20  cked() .  ** to 
1ad15 69 6e 76 6f 6b 65 20 61 6e 79 20 72 65 71 75 69  invoke any requi
1ad16 72 65 64 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66  red unlock-notif
1ad17 79 20 63 61 6c 6c 62 61 63 6b 73 2e 0a 20 20 2a  y callbacks..  *
1ad18 2f 0a 20 20 69 66 28 20 64 62 2d 3e 61 75 74 6f  /.  if( db->auto
1ad19 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 73 71  Commit ){.    sq
1ad1a 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 55  lite3ConnectionU
1ad1b 6e 6c 6f 63 6b 65 64 28 64 62 29 3b 0a 20 20 7d  nlocked(db);.  }
1ad1c 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e  ..  assert( db->
1ad1d 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e 30 20  activeVdbeCnt>0 
1ad1e 7c 7c 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69  || db->autoCommi
1ad1f 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e 6e 53 74 61  t==0 || db->nSta
1ad20 74 65 6d 65 6e 74 3d 3d 30 20 29 3b 0a 20 20 72  tement==0 );.  r
1ad21 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1ad22 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20  .}.../*.** Each 
1ad23 56 44 42 45 20 68 6f 6c 64 73 20 74 68 65 20 72  VDBE holds the r
1ad24 65 73 75 6c 74 20 6f 66 20 74 68 65 20 6d 6f 73  esult of the mos
1ad25 74 20 72 65 63 65 6e 74 20 73 71 6c 69 74 65 33  t recent sqlite3
1ad26 5f 73 74 65 70 28 29 20 63 61 6c 6c 0a 2a 2a 20  _step() call.** 
1ad27 69 6e 20 70 2d 3e 72 63 2e 20 20 54 68 69 73 20  in p->rc.  This 
1ad28 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 61  routine sets tha
1ad29 74 20 72 65 73 75 6c 74 20 62 61 63 6b 20 74 6f  t result back to
1ad2a 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53   SQLITE_OK..*/.S
1ad2b 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1ad2c 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  id sqlite3VdbeRe
1ad2d 73 65 74 53 74 65 70 52 65 73 75 6c 74 28 56 64  setStepResult(Vd
1ad2e 62 65 20 2a 70 29 7b 0a 20 20 70 2d 3e 72 63 20  be *p){.  p->rc 
1ad2f 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  = SQLITE_OK;.}..
1ad30 2f 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75 70 20 61  /*.** Clean up a
1ad31 20 56 44 42 45 20 61 66 74 65 72 20 65 78 65 63   VDBE after exec
1ad32 75 74 69 6f 6e 20 62 75 74 20 64 6f 20 6e 6f 74  ution but do not
1ad33 20 64 65 6c 65 74 65 20 74 68 65 20 56 44 42 45   delete the VDBE
1ad34 20 6a 75 73 74 20 79 65 74 2e 0a 2a 2a 20 57 72   just yet..** Wr
1ad35 69 74 65 20 61 6e 79 20 65 72 72 6f 72 20 6d 65  ite any error me
1ad36 73 73 61 67 65 73 20 69 6e 74 6f 20 2a 70 7a 45  ssages into *pzE
1ad37 72 72 4d 73 67 2e 20 20 52 65 74 75 72 6e 20 74  rrMsg.  Return t
1ad38 68 65 20 72 65 73 75 6c 74 20 63 6f 64 65 2e 0a  he result code..
1ad39 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68 69 73  **.** After this
1ad3a 20 72 6f 75 74 69 6e 65 20 69 73 20 72 75 6e 2c   routine is run,
1ad3b 20 74 68 65 20 56 44 42 45 20 73 68 6f 75 6c 64   the VDBE should
1ad3c 20 62 65 20 72 65 61 64 79 20 74 6f 20 62 65 20   be ready to be 
1ad3d 65 78 65 63 75 74 65 64 0a 2a 2a 20 61 67 61 69  executed.** agai
1ad3e 6e 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 6c 6f 6f 6b  n..**.** To look
1ad3f 20 61 74 20 69 74 20 61 6e 6f 74 68 65 72 20 77   at it another w
1ad40 61 79 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ay, this routine
1ad41 20 72 65 73 65 74 73 20 74 68 65 20 73 74 61 74   resets the stat
1ad42 65 20 6f 66 20 74 68 65 0a 2a 2a 20 76 69 72 74  e of the.** virt
1ad43 75 61 6c 20 6d 61 63 68 69 6e 65 20 66 72 6f 6d  ual machine from
1ad44 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20   VDBE_MAGIC_RUN 
1ad45 6f 72 20 56 44 42 45 5f 4d 41 47 49 43 5f 48 41  or VDBE_MAGIC_HA
1ad46 4c 54 20 62 61 63 6b 20 74 6f 0a 2a 2a 20 56 44  LT back to.** VD
1ad47 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 2e 0a 2a  BE_MAGIC_INIT..*
1ad48 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1ad49 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
1ad4a 52 65 73 65 74 28 56 64 62 65 20 2a 70 29 7b 0a  Reset(Vdbe *p){.
1ad4b 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20    sqlite3 *db;. 
1ad4c 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20   db = p->db;..  
1ad4d 2f 2a 20 49 66 20 74 68 65 20 56 4d 20 64 69 64  /* If the VM did
1ad4e 20 6e 6f 74 20 72 75 6e 20 74 6f 20 63 6f 6d 70   not run to comp
1ad4f 6c 65 74 69 6f 6e 20 6f 72 20 69 66 20 69 74 20  letion or if it 
1ad50 65 6e 63 6f 75 6e 74 65 72 65 64 20 61 6e 0a 20  encountered an. 
1ad51 20 2a 2a 20 65 72 72 6f 72 2c 20 74 68 65 6e 20   ** error, then 
1ad52 69 74 20 6d 69 67 68 74 20 6e 6f 74 20 68 61 76  it might not hav
1ad53 65 20 62 65 65 6e 20 68 61 6c 74 65 64 20 70 72  e been halted pr
1ad54 6f 70 65 72 6c 79 2e 20 20 53 6f 20 68 61 6c 74  operly.  So halt
1ad55 0a 20 20 2a 2a 20 69 74 20 6e 6f 77 2e 0a 20 20  .  ** it now..  
1ad56 2a 2f 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74  */.  (void)sqlit
1ad57 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a  e3SafetyOn(db);.
1ad58 20 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c    sqlite3VdbeHal
1ad59 74 28 70 29 3b 0a 20 20 28 76 6f 69 64 29 73 71  t(p);.  (void)sq
1ad5a 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64  lite3SafetyOff(d
1ad5b 62 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65  b);..  /* If the
1ad5c 20 56 44 42 45 20 68 61 73 20 62 65 20 72 75 6e   VDBE has be run
1ad5d 20 65 76 65 6e 20 70 61 72 74 69 61 6c 6c 79 2c   even partially,
1ad5e 20 74 68 65 6e 20 74 72 61 6e 73 66 65 72 20 74   then transfer t
1ad5f 68 65 20 65 72 72 6f 72 20 63 6f 64 65 0a 20 20  he error code.  
1ad60 2a 2a 20 61 6e 64 20 65 72 72 6f 72 20 6d 65 73  ** and error mes
1ad61 73 61 67 65 20 66 72 6f 6d 20 74 68 65 20 56 44  sage from the VD
1ad62 42 45 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e  BE into the main
1ad63 20 64 61 74 61 62 61 73 65 20 73 74 72 75 63 74   database struct
1ad64 75 72 65 2e 20 20 42 75 74 0a 20 20 2a 2a 20 69  ure.  But.  ** i
1ad65 66 20 74 68 65 20 56 44 42 45 20 68 61 73 20 6a  f the VDBE has j
1ad66 75 73 74 20 62 65 65 6e 20 73 65 74 20 74 6f 20  ust been set to 
1ad67 72 75 6e 20 62 75 74 20 68 61 73 20 6e 6f 74 20  run but has not 
1ad68 61 63 74 75 61 6c 6c 79 20 65 78 65 63 75 74 65  actually execute
1ad69 64 20 61 6e 79 0a 20 20 2a 2a 20 69 6e 73 74 72  d any.  ** instr
1ad6a 75 63 74 69 6f 6e 73 20 79 65 74 2c 20 6c 65 61  uctions yet, lea
1ad6b 76 65 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  ve the main data
1ad6c 62 61 73 65 20 65 72 72 6f 72 20 69 6e 66 6f 72  base error infor
1ad6d 6d 61 74 69 6f 6e 20 75 6e 63 68 61 6e 67 65 64  mation unchanged
1ad6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e  ..  */.  if( p->
1ad6f 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20 69 66 28  pc>=0 ){.    if(
1ad70 20 70 2d 3e 7a 45 72 72 4d 73 67 20 29 7b 0a 20   p->zErrMsg ){. 
1ad71 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69       sqlite3Begi
1ad72 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  nBenignMalloc();
1ad73 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61  .      sqlite3Va
1ad74 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70 45  lueSetStr(db->pE
1ad75 72 72 2c 2d 31 2c 70 2d 3e 7a 45 72 72 4d 73 67  rr,-1,p->zErrMsg
1ad76 2c 53 51 4c 49 54 45 5f 55 54 46 38 2c 53 51 4c  ,SQLITE_UTF8,SQL
1ad77 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a  ITE_TRANSIENT);.
1ad78 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64        sqlite3End
1ad79 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a  BenignMalloc();.
1ad7a 20 20 20 20 20 20 64 62 2d 3e 65 72 72 43 6f 64        db->errCod
1ad7b 65 20 3d 20 70 2d 3e 72 63 3b 0a 20 20 20 20 20  e = p->rc;.     
1ad7c 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1ad7d 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a  b, p->zErrMsg);.
1ad7e 20 20 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67        p->zErrMsg
1ad7f 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20   = 0;.    }else 
1ad80 69 66 28 20 70 2d 3e 72 63 20 29 7b 0a 20 20 20  if( p->rc ){.   
1ad81 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28     sqlite3Error(
1ad82 64 62 2c 20 70 2d 3e 72 63 2c 20 30 29 3b 0a 20  db, p->rc, 0);. 
1ad83 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1ad84 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
1ad85 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b 0a   SQLITE_OK, 0);.
1ad86 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66      }.  }else if
1ad87 28 20 70 2d 3e 72 63 20 26 26 20 70 2d 3e 65 78  ( p->rc && p->ex
1ad88 70 69 72 65 64 20 29 7b 0a 20 20 20 20 2f 2a 20  pired ){.    /* 
1ad89 54 68 65 20 65 78 70 69 72 65 64 20 66 6c 61 67  The expired flag
1ad8a 20 77 61 73 20 73 65 74 20 6f 6e 20 74 68 65 20   was set on the 
1ad8b 56 44 42 45 20 62 65 66 6f 72 65 20 74 68 65 20  VDBE before the 
1ad8c 66 69 72 73 74 20 63 61 6c 6c 0a 20 20 20 20 2a  first call.    *
1ad8d 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 74 65  * to sqlite3_ste
1ad8e 70 28 29 2e 20 46 6f 72 20 63 6f 6e 73 69 73 74  p(). For consist
1ad8f 65 6e 63 79 20 28 73 69 6e 63 65 20 73 71 6c 69  ency (since sqli
1ad90 74 65 33 5f 73 74 65 70 28 29 20 77 61 73 0a 20  te3_step() was. 
1ad91 20 20 20 2a 2a 20 63 61 6c 6c 65 64 29 2c 20 73     ** called), s
1ad92 65 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20  et the database 
1ad93 65 72 72 6f 72 20 69 6e 20 74 68 69 73 20 63 61  error in this ca
1ad94 73 65 20 61 73 20 77 65 6c 6c 2e 0a 20 20 20 20  se as well..    
1ad95 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  */.    sqlite3Er
1ad96 72 6f 72 28 64 62 2c 20 70 2d 3e 72 63 2c 20 30  ror(db, p->rc, 0
1ad97 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61  );.    sqlite3Va
1ad98 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70 45  lueSetStr(db->pE
1ad99 72 72 2c 20 2d 31 2c 20 70 2d 3e 7a 45 72 72 4d  rr, -1, p->zErrM
1ad9a 73 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c  sg, SQLITE_UTF8,
1ad9b 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
1ad9c 54 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  T);.    sqlite3D
1ad9d 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72  bFree(db, p->zEr
1ad9e 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45  rMsg);.    p->zE
1ad9f 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 7d 0a 0a  rrMsg = 0;.  }..
1ada0 20 20 2f 2a 20 52 65 63 6c 61 69 6d 20 61 6c 6c    /* Reclaim all
1ada1 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 62 79 20   memory used by 
1ada2 74 68 65 20 56 44 42 45 0a 20 20 2a 2f 0a 20 20  the VDBE.  */.  
1ada3 43 6c 65 61 6e 75 70 28 70 29 3b 0a 0a 20 20 2f  Cleanup(p);..  /
1ada4 2a 20 53 61 76 65 20 70 72 6f 66 69 6c 69 6e 67  * Save profiling
1ada5 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f   information fro
1ada6 6d 20 74 68 69 73 20 56 44 42 45 20 72 75 6e 2e  m this VDBE run.
1ada7 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 56 44 42  .  */.#ifdef VDB
1ada8 45 5f 50 52 4f 46 49 4c 45 0a 20 20 7b 0a 20 20  E_PROFILE.  {.  
1ada9 20 20 46 49 4c 45 20 2a 6f 75 74 20 3d 20 66 6f    FILE *out = fo
1adaa 70 65 6e 28 22 76 64 62 65 5f 70 72 6f 66 69 6c  pen("vdbe_profil
1adab 65 2e 6f 75 74 22 2c 20 22 61 22 29 3b 0a 20 20  e.out", "a");.  
1adac 20 20 69 66 28 20 6f 75 74 20 29 7b 0a 20 20 20    if( out ){.   
1adad 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20     int i;.      
1adae 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 2d 2d  fprintf(out, "--
1adaf 2d 2d 20 22 29 3b 0a 20 20 20 20 20 20 66 6f 72  -- ");.      for
1adb0 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20  (i=0; i<p->nOp; 
1adb1 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 66 70  i++){.        fp
1adb2 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 30 32 78  rintf(out, "%02x
1adb3 22 2c 20 70 2d 3e 61 4f 70 5b 69 5d 2e 6f 70 63  ", p->aOp[i].opc
1adb4 6f 64 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ode);.      }.  
1adb5 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c      fprintf(out,
1adb6 20 22 5c 6e 22 29 3b 0a 20 20 20 20 20 20 66 6f   "\n");.      fo
1adb7 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b  r(i=0; i<p->nOp;
1adb8 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 66   i++){.        f
1adb9 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 36 64  printf(out, "%6d
1adba 20 25 31 30 6c 6c 64 20 25 38 6c 6c 64 20 22 2c   %10lld %8lld ",
1adbb 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 61  .           p->a
1adbc 4f 70 5b 69 5d 2e 63 6e 74 2c 0a 20 20 20 20 20  Op[i].cnt,.     
1adbd 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e        p->aOp[i].
1adbe 63 79 63 6c 65 73 2c 0a 20 20 20 20 20 20 20 20  cycles,.        
1adbf 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e 74     p->aOp[i].cnt
1adc0 3e 30 20 3f 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63  >0 ? p->aOp[i].c
1adc1 79 63 6c 65 73 2f 70 2d 3e 61 4f 70 5b 69 5d 2e  ycles/p->aOp[i].
1adc2 63 6e 74 20 3a 20 30 0a 20 20 20 20 20 20 20 20  cnt : 0.        
1adc3 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
1adc4 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 6f 75  e3VdbePrintOp(ou
1adc5 74 2c 20 69 2c 20 26 70 2d 3e 61 4f 70 5b 69 5d  t, i, &p->aOp[i]
1adc6 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1adc7 20 66 63 6c 6f 73 65 28 6f 75 74 29 3b 0a 20 20   fclose(out);.  
1adc8 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20    }.  }.#endif. 
1adc9 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 45   p->magic = VDBE
1adca 5f 4d 41 47 49 43 5f 49 4e 49 54 3b 0a 20 20 72  _MAGIC_INIT;.  r
1adcb 65 74 75 72 6e 20 70 2d 3e 72 63 20 26 20 64 62  eturn p->rc & db
1adcc 2d 3e 65 72 72 4d 61 73 6b 3b 0a 7d 0a 20 0a 2f  ->errMask;.}. ./
1adcd 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75 70 20 61 6e  *.** Clean up an
1adce 64 20 64 65 6c 65 74 65 20 61 20 56 44 42 45 20  d delete a VDBE 
1adcf 61 66 74 65 72 20 65 78 65 63 75 74 69 6f 6e 2e  after execution.
1add0 20 20 52 65 74 75 72 6e 20 61 6e 20 69 6e 74 65    Return an inte
1add1 67 65 72 20 77 68 69 63 68 20 69 73 0a 2a 2a 20  ger which is.** 
1add2 74 68 65 20 72 65 73 75 6c 74 20 63 6f 64 65 2e  the result code.
1add3 20 20 57 72 69 74 65 20 61 6e 79 20 65 72 72 6f    Write any erro
1add4 72 20 6d 65 73 73 61 67 65 20 74 65 78 74 20 69  r message text i
1add5 6e 74 6f 20 2a 70 7a 45 72 72 4d 73 67 2e 0a 2a  nto *pzErrMsg..*
1add6 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1add7 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
1add8 46 69 6e 61 6c 69 7a 65 28 56 64 62 65 20 2a 70  Finalize(Vdbe *p
1add9 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
1adda 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70  LITE_OK;.  if( p
1addb 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41  ->magic==VDBE_MA
1addc 47 49 43 5f 52 55 4e 20 7c 7c 20 70 2d 3e 6d 61  GIC_RUN || p->ma
1addd 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f  gic==VDBE_MAGIC_
1adde 48 41 4c 54 20 29 7b 0a 20 20 20 20 72 63 20 3d  HALT ){.    rc =
1addf 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 65   sqlite3VdbeRese
1ade0 74 28 70 29 3b 0a 20 20 20 20 61 73 73 65 72 74  t(p);.    assert
1ade1 28 20 28 72 63 20 26 20 70 2d 3e 64 62 2d 3e 65  ( (rc & p->db->e
1ade2 72 72 4d 61 73 6b 29 3d 3d 72 63 20 29 3b 0a 20  rrMask)==rc );. 
1ade3 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65   }.  sqlite3Vdbe
1ade4 44 65 6c 65 74 65 28 70 29 3b 0a 20 20 72 65 74  Delete(p);.  ret
1ade5 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
1ade6 20 43 61 6c 6c 20 74 68 65 20 64 65 73 74 72 75   Call the destru
1ade7 63 74 6f 72 20 66 6f 72 20 65 61 63 68 20 61 75  ctor for each au
1ade8 78 64 61 74 61 20 65 6e 74 72 79 20 69 6e 20 70  xdata entry in p
1ade9 56 64 62 65 46 75 6e 63 20 66 6f 72 20 77 68 69  VdbeFunc for whi
1adea 63 68 0a 2a 2a 20 74 68 65 20 63 6f 72 72 65 73  ch.** the corres
1adeb 70 6f 6e 64 69 6e 67 20 62 69 74 20 69 6e 20 6d  ponding bit in m
1adec 61 73 6b 20 69 73 20 63 6c 65 61 72 2e 20 20 41  ask is clear.  A
1aded 75 78 64 61 74 61 20 65 6e 74 72 69 65 73 20 62  uxdata entries b
1adee 65 79 6f 6e 64 20 33 31 0a 2a 2a 20 61 72 65 20  eyond 31.** are 
1adef 61 6c 77 61 79 73 20 64 65 73 74 72 6f 79 65 64  always destroyed
1adf0 2e 20 20 54 6f 20 64 65 73 74 72 6f 79 20 61 6c  .  To destroy al
1adf1 6c 20 61 75 78 64 61 74 61 20 65 6e 74 72 69 65  l auxdata entrie
1adf2 73 2c 20 63 61 6c 6c 20 74 68 69 73 0a 2a 2a 20  s, call this.** 
1adf3 72 6f 75 74 69 6e 65 20 77 69 74 68 20 6d 61 73  routine with mas
1adf4 6b 3d 3d 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  k==0..*/.SQLITE_
1adf5 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1adf6 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 41 75  ite3VdbeDeleteAu
1adf7 78 44 61 74 61 28 56 64 62 65 46 75 6e 63 20 2a  xData(VdbeFunc *
1adf8 70 56 64 62 65 46 75 6e 63 2c 20 69 6e 74 20 6d  pVdbeFunc, int m
1adf9 61 73 6b 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  ask){.  int i;. 
1adfa 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 56 64 62   for(i=0; i<pVdb
1adfb 65 46 75 6e 63 2d 3e 6e 41 75 78 3b 20 69 2b 2b  eFunc->nAux; i++
1adfc 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 41 75  ){.    struct Au
1adfd 78 44 61 74 61 20 2a 70 41 75 78 20 3d 20 26 70  xData *pAux = &p
1adfe 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b  VdbeFunc->apAux[
1adff 69 5d 3b 0a 20 20 20 20 69 66 28 20 28 69 3e 33  i];.    if( (i>3
1ae00 31 20 7c 7c 20 21 28 6d 61 73 6b 26 28 28 28 75  1 || !(mask&(((u
1ae01 33 32 29 31 29 3c 3c 69 29 29 29 20 26 26 20 70  32)1)<<i))) && p
1ae02 41 75 78 2d 3e 70 41 75 78 20 29 7b 0a 20 20 20  Aux->pAux ){.   
1ae03 20 20 20 69 66 28 20 70 41 75 78 2d 3e 78 44 65     if( pAux->xDe
1ae04 6c 65 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20  lete ){.        
1ae05 70 41 75 78 2d 3e 78 44 65 6c 65 74 65 28 70 41  pAux->xDelete(pA
1ae06 75 78 2d 3e 70 41 75 78 29 3b 0a 20 20 20 20 20  ux->pAux);.     
1ae07 20 7d 0a 20 20 20 20 20 20 70 41 75 78 2d 3e 70   }.      pAux->p
1ae08 41 75 78 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  Aux = 0;.    }. 
1ae09 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65   }.}../*.** Dele
1ae0a 74 65 20 61 6e 20 65 6e 74 69 72 65 20 56 44 42  te an entire VDB
1ae0b 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  E..*/.SQLITE_PRI
1ae0c 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1ae0d 33 56 64 62 65 44 65 6c 65 74 65 28 56 64 62 65  3VdbeDelete(Vdbe
1ae0e 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 20   *p){.  sqlite3 
1ae0f 2a 64 62 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45  *db;..  if( NEVE
1ae10 52 28 70 3d 3d 30 29 20 29 20 72 65 74 75 72 6e  R(p==0) ) return
1ae11 3b 0a 20 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a  ;.  db = p->db;.
1ae12 20 20 69 66 28 20 70 2d 3e 70 50 72 65 76 20 29    if( p->pPrev )
1ae13 7b 0a 20 20 20 20 70 2d 3e 70 50 72 65 76 2d 3e  {.    p->pPrev->
1ae14 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74  pNext = p->pNext
1ae15 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
1ae16 73 73 65 72 74 28 20 64 62 2d 3e 70 56 64 62 65  ssert( db->pVdbe
1ae17 3d 3d 70 20 29 3b 0a 20 20 20 20 64 62 2d 3e 70  ==p );.    db->p
1ae18 56 64 62 65 20 3d 20 70 2d 3e 70 4e 65 78 74 3b  Vdbe = p->pNext;
1ae19 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 4e  .  }.  if( p->pN
1ae1a 65 78 74 20 29 7b 0a 20 20 20 20 70 2d 3e 70 4e  ext ){.    p->pN
1ae1b 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 2d 3e  ext->pPrev = p->
1ae1c 70 50 72 65 76 3b 0a 20 20 7d 0a 20 20 72 65 6c  pPrev;.  }.  rel
1ae1d 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70 2d 3e  easeMemArray(p->
1ae1e 61 56 61 72 2c 20 70 2d 3e 6e 56 61 72 29 3b 0a  aVar, p->nVar);.
1ae1f 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61    releaseMemArra
1ae20 79 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65 2c 20 70  y(p->aColName, p
1ae21 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c  ->nResColumn*COL
1ae22 4e 41 4d 45 5f 4e 29 3b 0a 20 20 76 64 62 65 46  NAME_N);.  vdbeF
1ae23 72 65 65 4f 70 41 72 72 61 79 28 64 62 2c 20 70  reeOpArray(db, p
1ae24 2d 3e 61 4f 70 2c 20 70 2d 3e 6e 4f 70 29 3b 0a  ->aOp, p->nOp);.
1ae25 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1ae26 64 62 2c 20 70 2d 3e 61 4c 61 62 65 6c 29 3b 0a  db, p->aLabel);.
1ae27 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1ae28 64 62 2c 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 29  db, p->aColName)
1ae29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
1ae2a 65 28 64 62 2c 20 70 2d 3e 7a 53 71 6c 29 3b 0a  e(db, p->zSql);.
1ae2b 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42    p->magic = VDB
1ae2c 45 5f 4d 41 47 49 43 5f 44 45 41 44 3b 0a 20 20  E_MAGIC_DEAD;.  
1ae2d 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1ae2e 2c 20 70 2d 3e 70 46 72 65 65 29 3b 0a 20 20 73  , p->pFree);.  s
1ae2f 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1ae30 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61   p);.}../*.** Ma
1ae31 6b 65 20 73 75 72 65 20 74 68 65 20 63 75 72 73  ke sure the curs
1ae32 6f 72 20 70 20 69 73 20 72 65 61 64 79 20 74 6f  or p is ready to
1ae33 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 74   read or write t
1ae34 68 65 20 72 6f 77 20 74 6f 20 77 68 69 63 68 20  he row to which 
1ae35 69 74 0a 2a 2a 20 77 61 73 20 6c 61 73 74 20 70  it.** was last p
1ae36 6f 73 69 74 69 6f 6e 65 64 2e 20 20 52 65 74 75  ositioned.  Retu
1ae37 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  rn an error code
1ae38 20 69 66 20 61 6e 20 4f 4f 4d 20 66 61 75 6c 74   if an OOM fault
1ae39 20 6f 72 20 49 2f 4f 20 65 72 72 6f 72 0a 2a 2a   or I/O error.**
1ae3a 20 70 72 65 76 65 6e 74 73 20 75 73 20 66 72 6f   prevents us fro
1ae3b 6d 20 70 6f 73 69 74 69 6f 6e 69 6e 67 20 74 68  m positioning th
1ae3c 65 20 63 75 72 73 6f 72 20 74 6f 20 69 74 73 20  e cursor to its 
1ae3d 63 6f 72 72 65 63 74 20 70 6f 73 69 74 69 6f 6e  correct position
1ae3e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 4d 6f 76  ..**.** If a Mov
1ae3f 65 54 6f 20 6f 70 65 72 61 74 69 6f 6e 20 69 73  eTo operation is
1ae40 20 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20   pending on the 
1ae41 67 69 76 65 6e 20 63 75 72 73 6f 72 2c 20 74 68  given cursor, th
1ae42 65 6e 20 64 6f 20 74 68 61 74 0a 2a 2a 20 4d 6f  en do that.** Mo
1ae43 76 65 54 6f 20 6e 6f 77 2e 20 20 49 66 20 6e 6f  veTo now.  If no
1ae44 20 6d 6f 76 65 20 69 73 20 70 65 6e 64 69 6e 67   move is pending
1ae45 2c 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69  , check to see i
1ae46 66 20 74 68 65 20 72 6f 77 20 68 61 73 20 62 65  f the row has be
1ae47 65 6e 0a 2a 2a 20 64 65 6c 65 74 65 64 20 6f 75  en.** deleted ou
1ae48 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 74 68 65  t from under the
1ae49 20 63 75 72 73 6f 72 20 61 6e 64 20 69 66 20 69   cursor and if i
1ae4a 74 20 68 61 73 2c 20 6d 61 72 6b 20 74 68 65 20  t has, mark the 
1ae4b 72 6f 77 20 61 73 0a 2a 2a 20 61 20 4e 55 4c 4c  row as.** a NULL
1ae4c 20 72 6f 77 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74   row..**.** If t
1ae4d 68 65 20 63 75 72 73 6f 72 20 69 73 20 61 6c 72  he cursor is alr
1ae4e 65 61 64 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f  eady pointing to
1ae4f 20 74 68 65 20 63 6f 72 72 65 63 74 20 72 6f 77   the correct row
1ae50 20 61 6e 64 20 74 68 61 74 20 72 6f 77 20 68 61   and that row ha
1ae51 73 0a 2a 2a 20 6e 6f 74 20 62 65 65 6e 20 64 65  s.** not been de
1ae52 6c 65 74 65 64 20 6f 75 74 20 66 72 6f 6d 20 75  leted out from u
1ae53 6e 64 65 72 20 74 68 65 20 63 75 72 73 6f 72 2c  nder the cursor,
1ae54 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   then this routi
1ae55 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  ne is a no-op..*
1ae56 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1ae57 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
1ae58 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 56 64 62  CursorMoveto(Vdb
1ae59 65 43 75 72 73 6f 72 20 2a 70 29 7b 0a 20 20 69  eCursor *p){.  i
1ae5a 66 28 20 70 2d 3e 64 65 66 65 72 72 65 64 4d 6f  f( p->deferredMo
1ae5b 76 65 74 6f 20 29 7b 0a 20 20 20 20 69 6e 74 20  veto ){.    int 
1ae5c 72 65 73 2c 20 72 63 3b 0a 23 69 66 64 65 66 20  res, rc;.#ifdef 
1ae5d 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20  SQLITE_TEST.    
1ae5e 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74  extern int sqlit
1ae5f 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 3b  e3_search_count;
1ae60 0a 23 65 6e 64 69 66 0a 20 20 20 20 61 73 73 65  .#endif.    asse
1ae61 72 74 28 20 70 2d 3e 69 73 54 61 62 6c 65 20 29  rt( p->isTable )
1ae62 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
1ae63 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70  e3BtreeMovetoUnp
1ae64 61 63 6b 65 64 28 70 2d 3e 70 43 75 72 73 6f 72  acked(p->pCursor
1ae65 2c 20 30 2c 20 70 2d 3e 6d 6f 76 65 74 6f 54 61  , 0, p->movetoTa
1ae66 72 67 65 74 2c 20 30 2c 20 26 72 65 73 29 3b 0a  rget, 0, &res);.
1ae67 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
1ae68 75 72 6e 20 72 63 3b 0a 20 20 20 20 70 2d 3e 6c  urn rc;.    p->l
1ae69 61 73 74 52 6f 77 69 64 20 3d 20 70 2d 3e 6d 6f  astRowid = p->mo
1ae6a 76 65 74 6f 54 61 72 67 65 74 3b 0a 20 20 20 20  vetoTarget;.    
1ae6b 70 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20  p->rowidIsValid 
1ae6c 3d 20 41 4c 57 41 59 53 28 72 65 73 3d 3d 30 29  = ALWAYS(res==0)
1ae6d 20 3f 31 3a 30 3b 0a 20 20 20 20 69 66 28 20 4e   ?1:0;.    if( N
1ae6e 45 56 45 52 28 72 65 73 3c 30 29 20 29 7b 0a 20  EVER(res<0) ){. 
1ae6f 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1ae70 33 42 74 72 65 65 4e 65 78 74 28 70 2d 3e 70 43  3BtreeNext(p->pC
1ae71 75 72 73 6f 72 2c 20 26 72 65 73 29 3b 0a 20 20  ursor, &res);.  
1ae72 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
1ae73 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 23 69  urn rc;.    }.#i
1ae74 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
1ae75 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 65 61  .    sqlite3_sea
1ae76 72 63 68 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e  rch_count++;.#en
1ae77 64 69 66 0a 20 20 20 20 70 2d 3e 64 65 66 65 72  dif.    p->defer
1ae78 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20  redMoveto = 0;. 
1ae79 20 20 20 70 2d 3e 63 61 63 68 65 53 74 61 74 75     p->cacheStatu
1ae7a 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b  s = CACHE_STALE;
1ae7b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 41 4c 57  .  }else if( ALW
1ae7c 41 59 53 28 70 2d 3e 70 43 75 72 73 6f 72 29 20  AYS(p->pCursor) 
1ae7d 29 7b 0a 20 20 20 20 69 6e 74 20 68 61 73 4d 6f  ){.    int hasMo
1ae7e 76 65 64 3b 0a 20 20 20 20 69 6e 74 20 72 63 20  ved;.    int rc 
1ae7f 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75  = sqlite3BtreeCu
1ae80 72 73 6f 72 48 61 73 4d 6f 76 65 64 28 70 2d 3e  rsorHasMoved(p->
1ae81 70 43 75 72 73 6f 72 2c 20 26 68 61 73 4d 6f 76  pCursor, &hasMov
1ae82 65 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20  ed);.    if( rc 
1ae83 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
1ae84 20 69 66 28 20 68 61 73 4d 6f 76 65 64 20 29 7b   if( hasMoved ){
1ae85 0a 20 20 20 20 20 20 70 2d 3e 63 61 63 68 65 53  .      p->cacheS
1ae86 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54  tatus = CACHE_ST
1ae87 41 4c 45 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 75  ALE;.      p->nu
1ae88 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 20 20 7d  llRow = 1;.    }
1ae89 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
1ae8a 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
1ae8b 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
1ae8c 66 75 6e 63 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a  functions:.**.**
1ae8d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
1ae8e 61 6c 54 79 70 65 28 29 0a 2a 2a 20 73 71 6c 69  alType().** sqli
1ae8f 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70  te3VdbeSerialTyp
1ae90 65 4c 65 6e 28 29 0a 2a 2a 20 73 71 6c 69 74 65  eLen().** sqlite
1ae91 33 56 64 62 65 53 65 72 69 61 6c 4c 65 6e 28 29  3VdbeSerialLen()
1ae92 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53  .** sqlite3VdbeS
1ae93 65 72 69 61 6c 50 75 74 28 29 0a 2a 2a 20 73 71  erialPut().** sq
1ae94 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 47  lite3VdbeSerialG
1ae95 65 74 28 29 0a 2a 2a 0a 2a 2a 20 65 6e 63 61 70  et().**.** encap
1ae96 73 75 6c 61 74 65 20 74 68 65 20 63 6f 64 65 20  sulate the code 
1ae97 74 68 61 74 20 73 65 72 69 61 6c 69 7a 65 73 20  that serializes 
1ae98 76 61 6c 75 65 73 20 66 6f 72 20 73 74 6f 72 61  values for stora
1ae99 67 65 20 69 6e 20 53 51 4c 69 74 65 0a 2a 2a 20  ge in SQLite.** 
1ae9a 64 61 74 61 20 61 6e 64 20 69 6e 64 65 78 20 72  data and index r
1ae9b 65 63 6f 72 64 73 2e 20 45 61 63 68 20 73 65 72  ecords. Each ser
1ae9c 69 61 6c 69 7a 65 64 20 76 61 6c 75 65 20 63 6f  ialized value co
1ae9d 6e 73 69 73 74 73 20 6f 66 20 61 0a 2a 2a 20 27  nsists of a.** '
1ae9e 73 65 72 69 61 6c 2d 74 79 70 65 27 20 61 6e 64  serial-type' and
1ae9f 20 61 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 2e   a blob of data.
1aea0 20 54 68 65 20 73 65 72 69 61 6c 20 74 79 70 65   The serial type
1aea1 20 69 73 20 61 6e 20 38 2d 62 79 74 65 20 75 6e   is an 8-byte un
1aea2 73 69 67 6e 65 64 0a 2a 2a 20 69 6e 74 65 67 65  signed.** intege
1aea3 72 2c 20 73 74 6f 72 65 64 20 61 73 20 61 20 76  r, stored as a v
1aea4 61 72 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20  arint..**.** In 
1aea5 61 6e 20 53 51 4c 69 74 65 20 69 6e 64 65 78 20  an SQLite index 
1aea6 72 65 63 6f 72 64 2c 20 74 68 65 20 73 65 72 69  record, the seri
1aea7 61 6c 20 74 79 70 65 20 69 73 20 73 74 6f 72 65  al type is store
1aea8 64 20 64 69 72 65 63 74 6c 79 20 62 65 66 6f 72  d directly befor
1aea9 65 0a 2a 2a 20 74 68 65 20 62 6c 6f 62 20 6f 66  e.** the blob of
1aeaa 20 64 61 74 61 20 74 68 61 74 20 69 74 20 63 6f   data that it co
1aeab 72 72 65 73 70 6f 6e 64 73 20 74 6f 2e 20 49 6e  rresponds to. In
1aeac 20 61 20 74 61 62 6c 65 20 72 65 63 6f 72 64 2c   a table record,
1aead 20 61 6c 6c 20 73 65 72 69 61 6c 0a 2a 2a 20 74   all serial.** t
1aeae 79 70 65 73 20 61 72 65 20 73 74 6f 72 65 64 20  ypes are stored 
1aeaf 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  at the start of 
1aeb0 74 68 65 20 72 65 63 6f 72 64 2c 20 61 6e 64 20  the record, and 
1aeb1 74 68 65 20 62 6c 6f 62 73 20 6f 66 20 64 61 74  the blobs of dat
1aeb2 61 20 61 74 0a 2a 2a 20 74 68 65 20 65 6e 64 2e  a at.** the end.
1aeb3 20 48 65 6e 63 65 20 74 68 65 73 65 20 66 75 6e   Hence these fun
1aeb4 63 74 69 6f 6e 73 20 61 6c 6c 6f 77 20 74 68 65  ctions allow the
1aeb5 20 63 61 6c 6c 65 72 20 74 6f 20 68 61 6e 64 6c   caller to handl
1aeb6 65 20 74 68 65 0a 2a 2a 20 73 65 72 69 61 6c 2d  e the.** serial-
1aeb7 74 79 70 65 20 61 6e 64 20 64 61 74 61 20 62 6c  type and data bl
1aeb8 6f 62 20 73 65 70 65 72 61 74 65 6c 79 2e 0a 2a  ob seperately..*
1aeb9 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
1aeba 6e 67 20 74 61 62 6c 65 20 64 65 73 63 72 69 62  ng table describ
1aebb 65 73 20 74 68 65 20 76 61 72 69 6f 75 73 20 73  es the various s
1aebc 74 6f 72 61 67 65 20 63 6c 61 73 73 65 73 20 66  torage classes f
1aebd 6f 72 20 64 61 74 61 3a 0a 2a 2a 0a 2a 2a 20 20  or data:.**.**  
1aebe 20 73 65 72 69 61 6c 20 74 79 70 65 20 20 20 20   serial type    
1aebf 20 20 20 20 62 79 74 65 73 20 6f 66 20 64 61 74      bytes of dat
1aec0 61 20 20 20 20 20 20 74 79 70 65 0a 2a 2a 20 20  a      type.**  
1aec1 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20   -------------- 
1aec2 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d      ------------
1aec3 2d 2d 2d 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  ---    ---------
1aec4 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 30  ------.**      0
1aec5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1aec6 20 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20       0          
1aec7 20 20 4e 55 4c 4c 0a 2a 2a 20 20 20 20 20 20 31    NULL.**      1
1aec8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1aec9 20 20 20 20 20 31 20 20 20 20 20 20 20 20 20 20       1          
1aeca 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72    signed integer
1aecb 0a 2a 2a 20 20 20 20 20 20 32 20 20 20 20 20 20  .**      2      
1aecc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32                 2
1aecd 20 20 20 20 20 20 20 20 20 20 20 20 73 69 67 6e              sign
1aece 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20  ed integer.**   
1aecf 20 20 20 33 20 20 20 20 20 20 20 20 20 20 20 20     3            
1aed0 20 20 20 20 20 20 20 20 20 33 20 20 20 20 20 20           3      
1aed1 20 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74        signed int
1aed2 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 34 20 20  eger.**      4  
1aed3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1aed4 20 20 20 34 20 20 20 20 20 20 20 20 20 20 20 20     4            
1aed5 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a  signed integer.*
1aed6 2a 20 20 20 20 20 20 35 20 20 20 20 20 20 20 20  *      5        
1aed7 20 20 20 20 20 20 20 20 20 20 20 20 20 36 20 20               6  
1aed8 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 64            signed
1aed9 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20   integer.**     
1aeda 20 36 20 20 20 20 20 20 20 20 20 20 20 20 20 20   6              
1aedb 20 20 20 20 20 20 20 38 20 20 20 20 20 20 20 20         8        
1aedc 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67      signed integ
1aedd 65 72 0a 2a 2a 20 20 20 20 20 20 37 20 20 20 20  er.**      7    
1aede 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1aedf 20 38 20 20 20 20 20 20 20 20 20 20 20 20 49 45   8            IE
1aee0 45 45 20 66 6c 6f 61 74 0a 2a 2a 20 20 20 20 20  EE float.**     
1aee1 20 38 20 20 20 20 20 20 20 20 20 20 20 20 20 20   8              
1aee2 20 20 20 20 20 20 20 30 20 20 20 20 20 20 20 20         0        
1aee3 20 20 20 20 49 6e 74 65 67 65 72 20 63 6f 6e 73      Integer cons
1aee4 74 61 6e 74 20 30 0a 2a 2a 20 20 20 20 20 20 39  tant 0.**      9
1aee5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1aee6 20 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20       0          
1aee7 20 20 49 6e 74 65 67 65 72 20 63 6f 6e 73 74 61    Integer consta
1aee8 6e 74 20 31 0a 2a 2a 20 20 20 20 20 31 30 2c 31  nt 1.**     10,1
1aee9 31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1               
1aeea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1aeeb 72 65 73 65 72 76 65 64 20 66 6f 72 20 65 78 70  reserved for exp
1aeec 61 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20 4e 3e 3d  ansion.**    N>=
1aeed 31 32 20 61 6e 64 20 65 76 65 6e 20 20 20 20 20  12 and even     
1aeee 20 20 28 4e 2d 31 32 29 2f 32 20 20 20 20 20 20    (N-12)/2      
1aeef 20 20 42 4c 4f 42 0a 2a 2a 20 20 20 20 4e 3e 3d    BLOB.**    N>=
1aef0 31 33 20 61 6e 64 20 6f 64 64 20 20 20 20 20 20  13 and odd      
1aef1 20 20 28 4e 2d 31 33 29 2f 32 20 20 20 20 20 20    (N-13)/2      
1aef2 20 20 74 65 78 74 0a 2a 2a 0a 2a 2a 20 54 68 65    text.**.** The
1aef3 20 38 20 61 6e 64 20 39 20 74 79 70 65 73 20 77   8 and 9 types w
1aef4 65 72 65 20 61 64 64 65 64 20 69 6e 20 33 2e 33  ere added in 3.3
1aef5 2e 30 2c 20 66 69 6c 65 20 66 6f 72 6d 61 74 20  .0, file format 
1aef6 34 2e 20 20 50 72 69 6f 72 20 76 65 72 73 69 6f  4.  Prior versio
1aef7 6e 73 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 20  ns.** of SQLite 
1aef8 77 69 6c 6c 20 6e 6f 74 20 75 6e 64 65 72 73 74  will not underst
1aef9 61 6e 64 20 74 68 6f 73 65 20 73 65 72 69 61 6c  and those serial
1aefa 20 74 79 70 65 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a   types..*/../*.*
1aefb 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 65 72  * Return the ser
1aefc 69 61 6c 2d 74 79 70 65 20 66 6f 72 20 74 68 65  ial-type for the
1aefd 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e   value stored in
1aefe 20 70 4d 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45   pMem..*/.SQLITE
1aeff 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71 6c  _PRIVATE u32 sql
1af00 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79  ite3VdbeSerialTy
1af01 70 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e  pe(Mem *pMem, in
1af02 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 7b 0a  t file_format){.
1af03 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 70 4d    int flags = pM
1af04 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 6e 74  em->flags;.  int
1af05 20 6e 3b 0a 0a 20 20 69 66 28 20 66 6c 61 67 73   n;..  if( flags
1af06 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20  &MEM_Null ){.   
1af07 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
1af08 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 49   if( flags&MEM_I
1af09 6e 74 20 29 7b 0a 20 20 20 20 2f 2a 20 46 69 67  nt ){.    /* Fig
1af0a 75 72 65 20 6f 75 74 20 77 68 65 74 68 65 72 20  ure out whether 
1af0b 74 6f 20 75 73 65 20 31 2c 20 32 2c 20 34 2c 20  to use 1, 2, 4, 
1af0c 36 20 6f 72 20 38 20 62 79 74 65 73 2e 20 2a 2f  6 or 8 bytes. */
1af0d 0a 23 20 20 20 64 65 66 69 6e 65 20 4d 41 58 5f  .#   define MAX_
1af0e 36 42 59 54 45 20 28 28 28 28 69 36 34 29 30 78  6BYTE ((((i64)0x
1af0f 30 30 30 30 38 30 30 30 29 3c 3c 33 32 29 2d 31  00008000)<<32)-1
1af10 29 0a 20 20 20 20 69 36 34 20 69 20 3d 20 70 4d  ).    i64 i = pM
1af11 65 6d 2d 3e 75 2e 69 3b 0a 20 20 20 20 75 36 34  em->u.i;.    u64
1af12 20 75 3b 0a 20 20 20 20 69 66 28 20 66 69 6c 65   u;.    if( file
1af13 5f 66 6f 72 6d 61 74 3e 3d 34 20 26 26 20 28 69  _format>=4 && (i
1af14 26 31 29 3d 3d 69 20 29 7b 0a 20 20 20 20 20 20  &1)==i ){.      
1af15 72 65 74 75 72 6e 20 38 2b 28 75 33 32 29 69 3b  return 8+(u32)i;
1af16 0a 20 20 20 20 7d 0a 20 20 20 20 75 20 3d 20 69  .    }.    u = i
1af17 3c 30 20 3f 20 2d 69 20 3a 20 69 3b 0a 20 20 20  <0 ? -i : i;.   
1af18 20 69 66 28 20 75 3c 3d 31 32 37 20 29 20 72 65   if( u<=127 ) re
1af19 74 75 72 6e 20 31 3b 0a 20 20 20 20 69 66 28 20  turn 1;.    if( 
1af1a 75 3c 3d 33 32 37 36 37 20 29 20 72 65 74 75 72  u<=32767 ) retur
1af1b 6e 20 32 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d  n 2;.    if( u<=
1af1c 38 33 38 38 36 30 37 20 29 20 72 65 74 75 72 6e  8388607 ) return
1af1d 20 33 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d 32   3;.    if( u<=2
1af1e 31 34 37 34 38 33 36 34 37 20 29 20 72 65 74 75  147483647 ) retu
1af1f 72 6e 20 34 3b 0a 20 20 20 20 69 66 28 20 75 3c  rn 4;.    if( u<
1af20 3d 4d 41 58 5f 36 42 59 54 45 20 29 20 72 65 74  =MAX_6BYTE ) ret
1af21 75 72 6e 20 35 3b 0a 20 20 20 20 72 65 74 75 72  urn 5;.    retur
1af22 6e 20 36 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66  n 6;.  }.  if( f
1af23 6c 61 67 73 26 4d 45 4d 5f 52 65 61 6c 20 29 7b  lags&MEM_Real ){
1af24 0a 20 20 20 20 72 65 74 75 72 6e 20 37 3b 0a 20  .    return 7;. 
1af25 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65   }.  assert( pMe
1af26 6d 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  m->db->mallocFai
1af27 6c 65 64 20 7c 7c 20 66 6c 61 67 73 26 28 4d 45  led || flags&(ME
1af28 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20  M_Str|MEM_Blob) 
1af29 29 3b 0a 20 20 6e 20 3d 20 70 4d 65 6d 2d 3e 6e  );.  n = pMem->n
1af2a 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20  ;.  if( flags & 
1af2b 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20  MEM_Zero ){.    
1af2c 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65  n += pMem->u.nZe
1af2d 72 6f 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  ro;.  }.  assert
1af2e 28 20 6e 3e 3d 30 20 29 3b 0a 20 20 72 65 74 75  ( n>=0 );.  retu
1af2f 72 6e 20 28 28 6e 2a 32 29 20 2b 20 31 32 20 2b  rn ((n*2) + 12 +
1af30 20 28 28 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72   ((flags&MEM_Str
1af31 29 21 3d 30 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  )!=0));.}../*.**
1af32 20 52 65 74 75 72 6e 20 74 68 65 20 6c 65 6e 67   Return the leng
1af33 74 68 20 6f 66 20 74 68 65 20 64 61 74 61 20 63  th of the data c
1af34 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20  orresponding to 
1af35 74 68 65 20 73 75 70 70 6c 69 65 64 20 73 65 72  the supplied ser
1af36 69 61 6c 2d 74 79 70 65 2e 0a 2a 2f 0a 53 51 4c  ial-type..*/.SQL
1af37 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20  ITE_PRIVATE u32 
1af38 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
1af39 6c 54 79 70 65 4c 65 6e 28 75 33 32 20 73 65 72  lTypeLen(u32 ser
1af3a 69 61 6c 5f 74 79 70 65 29 7b 0a 20 20 69 66 28  ial_type){.  if(
1af3b 20 73 65 72 69 61 6c 5f 74 79 70 65 3e 3d 31 32   serial_type>=12
1af3c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28   ){.    return (
1af3d 73 65 72 69 61 6c 5f 74 79 70 65 2d 31 32 29 2f  serial_type-12)/
1af3e 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  2;.  }else{.    
1af3f 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20  static const u8 
1af40 61 53 69 7a 65 5b 5d 20 3d 20 7b 20 30 2c 20 31  aSize[] = { 0, 1
1af41 2c 20 32 2c 20 33 2c 20 34 2c 20 36 2c 20 38 2c  , 2, 3, 4, 6, 8,
1af42 20 38 2c 20 30 2c 20 30 2c 20 30 2c 20 30 20 7d   8, 0, 0, 0, 0 }
1af43 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 61 53 69  ;.    return aSi
1af44 7a 65 5b 73 65 72 69 61 6c 5f 74 79 70 65 5d 3b  ze[serial_type];
1af45 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66  .  }.}../*.** If
1af46 20 77 65 20 61 72 65 20 6f 6e 20 61 6e 20 61 72   we are on an ar
1af47 63 68 69 74 65 63 74 75 72 65 20 77 69 74 68 20  chitecture with 
1af48 6d 69 78 65 64 2d 65 6e 64 69 61 6e 20 66 6c 6f  mixed-endian flo
1af49 61 74 69 6e 67 20 0a 2a 2a 20 70 6f 69 6e 74 73  ating .** points
1af4a 20 28 65 78 3a 20 41 52 4d 37 29 20 74 68 65 6e   (ex: ARM7) then
1af4b 20 73 77 61 70 20 74 68 65 20 6c 6f 77 65 72 20   swap the lower 
1af4c 34 20 62 79 74 65 73 20 77 69 74 68 20 74 68 65  4 bytes with the
1af4d 20 0a 2a 2a 20 75 70 70 65 72 20 34 20 62 79 74   .** upper 4 byt
1af4e 65 73 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20  es.  Return the 
1af4f 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f  result..**.** Fo
1af50 72 20 6d 6f 73 74 20 61 72 63 68 69 74 65 63 74  r most architect
1af51 75 72 65 73 2c 20 74 68 69 73 20 69 73 20 61 20  ures, this is a 
1af52 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 28 6c 61  no-op..**.** (la
1af53 74 65 72 29 3a 20 20 49 74 20 69 73 20 72 65 70  ter):  It is rep
1af54 6f 72 74 65 64 20 74 6f 20 6d 65 20 74 68 61 74  orted to me that
1af55 20 74 68 65 20 6d 69 78 65 64 2d 65 6e 64 69 61   the mixed-endia
1af56 6e 20 70 72 6f 62 6c 65 6d 0a 2a 2a 20 6f 6e 20  n problem.** on 
1af57 41 52 4d 37 20 69 73 20 61 6e 20 69 73 73 75 65  ARM7 is an issue
1af58 20 77 69 74 68 20 47 43 43 2c 20 6e 6f 74 20 77   with GCC, not w
1af59 69 74 68 20 74 68 65 20 41 52 4d 37 20 63 68 69  ith the ARM7 chi
1af5a 70 2e 20 20 49 74 20 73 65 65 6d 73 0a 2a 2a 20  p.  It seems.** 
1af5b 74 68 61 74 20 65 61 72 6c 79 20 76 65 72 73 69  that early versi
1af5c 6f 6e 73 20 6f 66 20 47 43 43 20 73 74 6f 72 65  ons of GCC store
1af5d 64 20 74 68 65 20 74 77 6f 20 77 6f 72 64 73 20  d the two words 
1af5e 6f 66 20 61 20 36 34 2d 62 69 74 0a 2a 2a 20 66  of a 64-bit.** f
1af5f 6c 6f 61 74 20 69 6e 20 74 68 65 20 77 72 6f 6e  loat in the wron
1af60 67 20 6f 72 64 65 72 2e 20 20 41 6e 64 20 74 68  g order.  And th
1af61 61 74 20 65 72 72 6f 72 20 68 61 73 20 62 65 65  at error has bee
1af62 6e 20 70 72 6f 70 61 67 61 74 65 64 0a 2a 2a 20  n propagated.** 
1af63 65 76 65 72 20 73 69 6e 63 65 2e 20 20 54 68 65  ever since.  The
1af64 20 62 6c 61 6d 65 20 69 73 20 6e 6f 74 20 6e 65   blame is not ne
1af65 63 65 73 73 61 72 69 6c 79 20 77 69 74 68 20 47  cessarily with G
1af66 43 43 2c 20 74 68 6f 75 67 68 2e 0a 2a 2a 20 47  CC, though..** G
1af67 43 43 20 6d 69 67 68 74 20 68 61 76 65 20 6a 75  CC might have ju
1af68 73 74 20 63 6f 70 79 69 6e 67 20 74 68 65 20 70  st copying the p
1af69 72 6f 62 6c 65 6d 20 66 72 6f 6d 20 61 20 70 72  roblem from a pr
1af6a 69 6f 72 20 63 6f 6d 70 69 6c 65 72 2e 0a 2a 2a  ior compiler..**
1af6b 20 49 20 61 6d 20 61 6c 73 6f 20 74 6f 6c 64 20   I am also told 
1af6c 74 68 61 74 20 6e 65 77 65 72 20 76 65 72 73 69  that newer versi
1af6d 6f 6e 73 20 6f 66 20 47 43 43 20 74 68 61 74 20  ons of GCC that 
1af6e 66 6f 6c 6c 6f 77 20 61 20 64 69 66 66 65 72 65  follow a differe
1af6f 6e 74 0a 2a 2a 20 41 42 49 20 67 65 74 20 74 68  nt.** ABI get th
1af70 65 20 62 79 74 65 20 6f 72 64 65 72 20 72 69 67  e byte order rig
1af71 68 74 2e 0a 2a 2a 0a 2a 2a 20 44 65 76 65 6c 6f  ht..**.** Develo
1af72 70 65 72 73 20 75 73 69 6e 67 20 53 51 4c 69 74  pers using SQLit
1af73 65 20 6f 6e 20 61 6e 20 41 52 4d 37 20 73 68 6f  e on an ARM7 sho
1af74 75 6c 64 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20  uld compile and 
1af75 72 75 6e 20 74 68 65 69 72 0a 2a 2a 20 61 70 70  run their.** app
1af76 6c 69 63 61 74 69 6f 6e 20 75 73 69 6e 67 20 2d  lication using -
1af77 44 53 51 4c 49 54 45 5f 44 45 42 55 47 3d 31 20  DSQLITE_DEBUG=1 
1af78 61 74 20 6c 65 61 73 74 20 6f 6e 63 65 2e 20 20  at least once.  
1af79 57 69 74 68 20 44 45 42 55 47 0a 2a 2a 20 65 6e  With DEBUG.** en
1af7a 61 62 6c 65 64 2c 20 73 6f 6d 65 20 61 73 73 65  abled, some asse
1af7b 72 74 73 20 62 65 6c 6f 77 20 77 69 6c 6c 20 65  rts below will e
1af7c 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 20 62  nsure that the b
1af7d 79 74 65 20 6f 72 64 65 72 20 6f 66 0a 2a 2a 20  yte order of.** 
1af7e 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76  floating point v
1af7f 61 6c 75 65 73 20 69 73 20 63 6f 72 72 65 63 74  alues is correct
1af80 2e 0a 2a 2a 0a 2a 2a 20 28 32 30 30 37 2d 30 38  ..**.** (2007-08
1af81 2d 33 30 29 20 20 46 72 61 6e 6b 20 76 61 6e 20  -30)  Frank van 
1af82 56 75 67 74 20 68 61 73 20 73 74 75 64 69 65 64  Vugt has studied
1af83 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 63 6c   this problem cl
1af84 6f 73 65 6c 79 0a 2a 2a 20 61 6e 64 20 68 61 73  osely.** and has
1af85 20 73 65 6e 64 20 68 69 73 20 66 69 6e 64 69 6e   send his findin
1af86 67 73 20 74 6f 20 74 68 65 20 53 51 4c 69 74 65  gs to the SQLite
1af87 20 64 65 76 65 6c 6f 70 65 72 73 2e 20 20 46 72   developers.  Fr
1af88 61 6e 6b 0a 2a 2a 20 77 72 69 74 65 73 20 74 68  ank.** writes th
1af89 61 74 20 73 6f 6d 65 20 4c 69 6e 75 78 20 6b 65  at some Linux ke
1af8a 72 6e 65 6c 73 20 6f 66 66 65 72 20 66 6c 6f 61  rnels offer floa
1af8b 74 69 6e 67 20 70 6f 69 6e 74 20 68 61 72 64 77  ting point hardw
1af8c 61 72 65 0a 2a 2a 20 65 6d 75 6c 61 74 69 6f 6e  are.** emulation
1af8d 20 74 68 61 74 20 75 73 65 73 20 6f 6e 6c 79 20   that uses only 
1af8e 33 32 2d 62 69 74 20 6d 61 6e 74 69 73 73 61 73  32-bit mantissas
1af8f 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20 66 75   instead of a fu
1af90 6c 6c 20 0a 2a 2a 20 34 38 2d 62 69 74 73 20 61  ll .** 48-bits a
1af91 73 20 72 65 71 75 69 72 65 64 20 62 79 20 74 68  s required by th
1af92 65 20 49 45 45 45 20 73 74 61 6e 64 61 72 64 2e  e IEEE standard.
1af93 20 20 28 54 68 69 73 20 69 73 20 74 68 65 0a 2a    (This is the.*
1af94 2a 20 43 4f 4e 46 49 47 5f 46 50 45 5f 46 41 53  * CONFIG_FPE_FAS
1af95 54 46 50 45 20 6f 70 74 69 6f 6e 2e 29 20 20 4f  TFPE option.)  O
1af96 6e 20 73 75 63 68 20 73 79 73 74 65 6d 73 2c 20  n such systems, 
1af97 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a  floating point.*
1af98 2a 20 62 79 74 65 20 73 77 61 70 70 69 6e 67 20  * byte swapping 
1af99 62 65 63 6f 6d 65 73 20 76 65 72 79 20 63 6f 6d  becomes very com
1af9a 70 6c 69 63 61 74 65 64 2e 20 20 54 6f 20 61 76  plicated.  To av
1af9b 6f 69 64 20 70 72 6f 62 6c 65 6d 73 2c 0a 2a 2a  oid problems,.**
1af9c 20 74 68 65 20 6e 65 63 65 73 73 61 72 79 20 62   the necessary b
1af9d 79 74 65 20 73 77 61 70 70 69 6e 67 20 69 73 20  yte swapping is 
1af9e 63 61 72 72 69 65 64 20 6f 75 74 20 75 73 69 6e  carried out usin
1af9f 67 20 61 20 36 34 2d 62 69 74 20 69 6e 74 65 67  g a 64-bit integ
1afa0 65 72 0a 2a 2a 20 72 61 74 68 65 72 20 74 68 61  er.** rather tha
1afa1 6e 20 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 74  n a 64-bit float
1afa2 2e 20 20 46 72 61 6e 6b 20 61 73 73 75 72 65 73  .  Frank assures
1afa3 20 75 73 20 74 68 61 74 20 74 68 65 20 63 6f 64   us that the cod
1afa4 65 20 68 65 72 65 0a 2a 2a 20 77 6f 72 6b 73 20  e here.** works 
1afa5 66 6f 72 20 68 69 6d 2e 20 20 57 65 2c 20 74 68  for him.  We, th
1afa6 65 20 64 65 76 65 6c 6f 70 65 72 73 2c 20 68 61  e developers, ha
1afa7 76 65 20 6e 6f 20 77 61 79 20 74 6f 20 69 6e 64  ve no way to ind
1afa8 65 70 65 6e 64 65 6e 74 6c 79 0a 2a 2a 20 76 65  ependently.** ve
1afa9 72 69 66 79 20 74 68 69 73 2c 20 62 75 74 20 46  rify this, but F
1afaa 72 61 6e 6b 20 73 65 65 6d 73 20 74 6f 20 6b 6e  rank seems to kn
1afab 6f 77 20 77 68 61 74 20 68 65 20 69 73 20 74 61  ow what he is ta
1afac 6c 6b 69 6e 67 20 61 62 6f 75 74 0a 2a 2a 20 73  lking about.** s
1afad 6f 20 77 65 20 74 72 75 73 74 20 68 69 6d 2e 0a  o we trust him..
1afae 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
1afaf 5f 4d 49 58 45 44 5f 45 4e 44 49 41 4e 5f 36 34  _MIXED_ENDIAN_64
1afb0 42 49 54 5f 46 4c 4f 41 54 0a 73 74 61 74 69 63  BIT_FLOAT.static
1afb1 20 75 36 34 20 66 6c 6f 61 74 53 77 61 70 28 75   u64 floatSwap(u
1afb2 36 34 20 69 6e 29 7b 0a 20 20 75 6e 69 6f 6e 20  64 in){.  union 
1afb3 7b 0a 20 20 20 20 75 36 34 20 72 3b 0a 20 20 20  {.    u64 r;.   
1afb4 20 75 33 32 20 69 5b 32 5d 3b 0a 20 20 7d 20 75   u32 i[2];.  } u
1afb5 3b 0a 20 20 75 33 32 20 74 3b 0a 0a 20 20 75 2e  ;.  u32 t;..  u.
1afb6 72 20 3d 20 69 6e 3b 0a 20 20 74 20 3d 20 75 2e  r = in;.  t = u.
1afb7 69 5b 30 5d 3b 0a 20 20 75 2e 69 5b 30 5d 20 3d  i[0];.  u.i[0] =
1afb8 20 75 2e 69 5b 31 5d 3b 0a 20 20 75 2e 69 5b 31   u.i[1];.  u.i[1
1afb9 5d 20 3d 20 74 3b 0a 20 20 72 65 74 75 72 6e 20  ] = t;.  return 
1afba 75 2e 72 3b 0a 7d 0a 23 20 64 65 66 69 6e 65 20  u.r;.}.# define 
1afbb 73 77 61 70 4d 69 78 65 64 45 6e 64 69 61 6e 46  swapMixedEndianF
1afbc 6c 6f 61 74 28 58 29 20 20 58 20 3d 20 66 6c 6f  loat(X)  X = flo
1afbd 61 74 53 77 61 70 28 58 29 0a 23 65 6c 73 65 0a  atSwap(X).#else.
1afbe 23 20 64 65 66 69 6e 65 20 73 77 61 70 4d 69 78  # define swapMix
1afbf 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 58 29  edEndianFloat(X)
1afc0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57  .#endif../*.** W
1afc1 72 69 74 65 20 74 68 65 20 73 65 72 69 61 6c 69  rite the seriali
1afc2 7a 65 64 20 64 61 74 61 20 62 6c 6f 62 20 66 6f  zed data blob fo
1afc3 72 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72  r the value stor
1afc4 65 64 20 69 6e 20 70 4d 65 6d 20 69 6e 74 6f 20  ed in pMem into 
1afc5 0a 2a 2a 20 62 75 66 2e 20 49 74 20 69 73 20 61  .** buf. It is a
1afc6 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 20  ssumed that the 
1afc7 63 61 6c 6c 65 72 20 68 61 73 20 61 6c 6c 6f 63  caller has alloc
1afc8 61 74 65 64 20 73 75 66 66 69 63 69 65 6e 74 20  ated sufficient 
1afc9 73 70 61 63 65 2e 0a 2a 2a 20 52 65 74 75 72 6e  space..** Return
1afca 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
1afcb 79 74 65 73 20 77 72 69 74 74 65 6e 2e 0a 2a 2a  ytes written..**
1afcc 0a 2a 2a 20 6e 42 75 66 20 69 73 20 74 68 65 20  .** nBuf is the 
1afcd 61 6d 6f 75 6e 74 20 6f 66 20 73 70 61 63 65 20  amount of space 
1afce 6c 65 66 74 20 69 6e 20 62 75 66 5b 5d 2e 20 20  left in buf[].  
1afcf 6e 42 75 66 20 6d 75 73 74 20 61 6c 77 61 79 73  nBuf must always
1afd0 20 62 65 0a 2a 2a 20 6c 61 72 67 65 20 65 6e 6f   be.** large eno
1afd1 75 67 68 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ugh to hold the 
1afd2 65 6e 74 69 72 65 20 66 69 65 6c 64 2e 20 20 45  entire field.  E
1afd3 78 63 65 70 74 2c 20 69 66 20 74 68 65 20 66 69  xcept, if the fi
1afd4 65 6c 64 20 69 73 0a 2a 2a 20 61 20 62 6c 6f 62  eld is.** a blob
1afd5 20 77 69 74 68 20 61 20 7a 65 72 6f 2d 66 69 6c   with a zero-fil
1afd6 6c 65 64 20 74 61 69 6c 2c 20 74 68 65 6e 20 62  led tail, then b
1afd7 75 66 5b 5d 20 6d 69 67 68 74 20 62 65 20 6a 75  uf[] might be ju
1afd8 73 74 20 74 68 65 20 72 69 67 68 74 0a 2a 2a 20  st the right.** 
1afd9 73 69 7a 65 20 74 6f 20 68 6f 6c 64 20 65 76 65  size to hold eve
1afda 72 79 74 68 69 6e 67 20 65 78 63 65 70 74 20 66  rything except f
1afdb 6f 72 20 74 68 65 20 7a 65 72 6f 2d 66 69 6c 6c  or the zero-fill
1afdc 65 64 20 74 61 69 6c 2e 20 20 49 66 20 62 75 66  ed tail.  If buf
1afdd 5b 5d 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 62 69  [].** is only bi
1afde 67 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64  g enough to hold
1afdf 20 74 68 65 20 6e 6f 6e 2d 7a 65 72 6f 20 70 72   the non-zero pr
1afe0 65 66 69 78 2c 20 74 68 65 6e 20 6f 6e 6c 79 20  efix, then only 
1afe1 77 72 69 74 65 20 74 68 61 74 0a 2a 2a 20 70 72  write that.** pr
1afe2 65 66 69 78 20 69 6e 74 6f 20 62 75 66 5b 5d 2e  efix into buf[].
1afe3 20 20 42 75 74 20 69 66 20 62 75 66 5b 5d 20 69    But if buf[] i
1afe4 73 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74  s large enough t
1afe5 6f 20 68 6f 6c 64 20 62 6f 74 68 20 74 68 65 0a  o hold both the.
1afe6 2a 2a 20 70 72 65 66 69 78 20 61 6e 64 20 74 68  ** prefix and th
1afe7 65 20 74 61 69 6c 20 74 68 65 6e 20 77 72 69 74  e tail then writ
1afe8 65 20 74 68 65 20 70 72 65 66 69 78 20 61 6e 64  e the prefix and
1afe9 20 73 65 74 20 74 68 65 20 74 61 69 6c 20 74 6f   set the tail to
1afea 20 61 6c 6c 0a 2a 2a 20 7a 65 72 6f 73 2e 0a 2a   all.** zeros..*
1afeb 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
1afec 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
1afed 61 63 74 75 61 6c 6c 79 20 77 72 69 74 74 65 6e  actually written
1afee 20 69 6e 74 6f 20 62 75 66 5b 5d 2e 20 20 54 68   into buf[].  Th
1afef 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 62  e number.** of b
1aff0 79 74 65 73 20 69 6e 20 74 68 65 20 7a 65 72 6f  ytes in the zero
1aff1 2d 66 69 6c 6c 65 64 20 74 61 69 6c 20 69 73 20  -filled tail is 
1aff2 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 65 20  included in the 
1aff3 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 6e 6c  return value onl
1aff4 79 0a 2a 2a 20 69 66 20 74 68 6f 73 65 20 62 79  y.** if those by
1aff5 74 65 73 20 77 65 72 65 20 7a 65 72 6f 65 64 20  tes were zeroed 
1aff6 69 6e 20 62 75 66 5b 5d 2e 0a 2a 2f 20 0a 53 51  in buf[]..*/ .SQ
1aff7 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32  LITE_PRIVATE u32
1aff8 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
1aff9 61 6c 50 75 74 28 75 38 20 2a 62 75 66 2c 20 69  alPut(u8 *buf, i
1affa 6e 74 20 6e 42 75 66 2c 20 4d 65 6d 20 2a 70 4d  nt nBuf, Mem *pM
1affb 65 6d 2c 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72  em, int file_for
1affc 6d 61 74 29 7b 0a 20 20 75 33 32 20 73 65 72 69  mat){.  u32 seri
1affd 61 6c 5f 74 79 70 65 20 3d 20 73 71 6c 69 74 65  al_type = sqlite
1affe 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28  3VdbeSerialType(
1afff 70 4d 65 6d 2c 20 66 69 6c 65 5f 66 6f 72 6d 61  pMem, file_forma
1b000 74 29 3b 0a 20 20 75 33 32 20 6c 65 6e 3b 0a 0a  t);.  u32 len;..
1b001 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 61 6e 64    /* Integer and
1b002 20 52 65 61 6c 20 2a 2f 0a 20 20 69 66 28 20 73   Real */.  if( s
1b003 65 72 69 61 6c 5f 74 79 70 65 3c 3d 37 20 26 26  erial_type<=7 &&
1b004 20 73 65 72 69 61 6c 5f 74 79 70 65 3e 30 20 29   serial_type>0 )
1b005 7b 0a 20 20 20 20 75 36 34 20 76 3b 0a 20 20 20  {.    u64 v;.   
1b006 20 75 33 32 20 69 3b 0a 20 20 20 20 69 66 28 20   u32 i;.    if( 
1b007 73 65 72 69 61 6c 5f 74 79 70 65 3d 3d 37 20 29  serial_type==7 )
1b008 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
1b009 73 69 7a 65 6f 66 28 76 29 3d 3d 73 69 7a 65 6f  sizeof(v)==sizeo
1b00a 66 28 70 4d 65 6d 2d 3e 72 29 20 29 3b 0a 20 20  f(pMem->r) );.  
1b00b 20 20 20 20 6d 65 6d 63 70 79 28 26 76 2c 20 26      memcpy(&v, &
1b00c 70 4d 65 6d 2d 3e 72 2c 20 73 69 7a 65 6f 66 28  pMem->r, sizeof(
1b00d 76 29 29 3b 0a 20 20 20 20 20 20 73 77 61 70 4d  v));.      swapM
1b00e 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28  ixedEndianFloat(
1b00f 76 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  v);.    }else{. 
1b010 20 20 20 20 20 76 20 3d 20 70 4d 65 6d 2d 3e 75       v = pMem->u
1b011 2e 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6c 65  .i;.    }.    le
1b012 6e 20 3d 20 69 20 3d 20 73 71 6c 69 74 65 33 56  n = i = sqlite3V
1b013 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e  dbeSerialTypeLen
1b014 28 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20  (serial_type);. 
1b015 20 20 20 61 73 73 65 72 74 28 20 6c 65 6e 3c 3d     assert( len<=
1b016 28 75 33 32 29 6e 42 75 66 20 29 3b 0a 20 20 20  (u32)nBuf );.   
1b017 20 77 68 69 6c 65 28 20 69 2d 2d 20 29 7b 0a 20   while( i-- ){. 
1b018 20 20 20 20 20 62 75 66 5b 69 5d 20 3d 20 28 75       buf[i] = (u
1b019 38 29 28 76 26 30 78 46 46 29 3b 0a 20 20 20 20  8)(v&0xFF);.    
1b01a 20 20 76 20 3e 3e 3d 20 38 3b 0a 20 20 20 20 7d    v >>= 8;.    }
1b01b 0a 20 20 20 20 72 65 74 75 72 6e 20 6c 65 6e 3b  .    return len;
1b01c 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 72 69 6e  .  }..  /* Strin
1b01d 67 20 6f 72 20 62 6c 6f 62 20 2a 2f 0a 20 20 69  g or blob */.  i
1b01e 66 28 20 73 65 72 69 61 6c 5f 74 79 70 65 3e 3d  f( serial_type>=
1b01f 31 32 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  12 ){.    assert
1b020 28 20 70 4d 65 6d 2d 3e 6e 20 2b 20 28 28 70 4d  ( pMem->n + ((pM
1b021 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  em->flags & MEM_
1b022 5a 65 72 6f 29 3f 70 4d 65 6d 2d 3e 75 2e 6e 5a  Zero)?pMem->u.nZ
1b023 65 72 6f 3a 30 29 0a 20 20 20 20 20 20 20 20 20  ero:0).         
1b024 20 20 20 20 3d 3d 20 28 69 6e 74 29 73 71 6c 69      == (int)sqli
1b025 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70  te3VdbeSerialTyp
1b026 65 4c 65 6e 28 73 65 72 69 61 6c 5f 74 79 70 65  eLen(serial_type
1b027 29 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ) );.    assert(
1b028 20 70 4d 65 6d 2d 3e 6e 3c 3d 6e 42 75 66 20 29   pMem->n<=nBuf )
1b029 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 70 4d 65 6d  ;.    len = pMem
1b02a 2d 3e 6e 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  ->n;.    memcpy(
1b02b 62 75 66 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 6c 65  buf, pMem->z, le
1b02c 6e 29 3b 0a 20 20 20 20 69 66 28 20 70 4d 65 6d  n);.    if( pMem
1b02d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65  ->flags & MEM_Ze
1b02e 72 6f 20 29 7b 0a 20 20 20 20 20 20 6c 65 6e 20  ro ){.      len 
1b02f 2b 3d 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f  += pMem->u.nZero
1b030 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1b031 6e 42 75 66 3e 3d 30 20 29 3b 0a 20 20 20 20 20  nBuf>=0 );.     
1b032 20 69 66 28 20 6c 65 6e 20 3e 20 28 75 33 32 29   if( len > (u32)
1b033 6e 42 75 66 20 29 7b 0a 20 20 20 20 20 20 20 20  nBuf ){.        
1b034 6c 65 6e 20 3d 20 28 75 33 32 29 6e 42 75 66 3b  len = (u32)nBuf;
1b035 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d  .      }.      m
1b036 65 6d 73 65 74 28 26 62 75 66 5b 70 4d 65 6d 2d  emset(&buf[pMem-
1b037 3e 6e 5d 2c 20 30 2c 20 6c 65 6e 2d 70 4d 65 6d  >n], 0, len-pMem
1b038 2d 3e 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ->n);.    }.    
1b039 72 65 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 7d 0a  return len;.  }.
1b03a 0a 20 20 2f 2a 20 4e 55 4c 4c 20 6f 72 20 63 6f  .  /* NULL or co
1b03b 6e 73 74 61 6e 74 73 20 30 20 6f 72 20 31 20 2a  nstants 0 or 1 *
1b03c 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  /.  return 0;.}.
1b03d 0a 2f 2a 0a 2a 2a 20 44 65 73 65 72 69 61 6c 69  ./*.** Deseriali
1b03e 7a 65 20 74 68 65 20 64 61 74 61 20 62 6c 6f 62  ze the data blob
1b03f 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 62   pointed to by b
1b040 75 66 20 61 73 20 73 65 72 69 61 6c 20 74 79 70  uf as serial typ
1b041 65 20 73 65 72 69 61 6c 5f 74 79 70 65 0a 2a 2a  e serial_type.**
1b042 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72   and store the r
1b043 65 73 75 6c 74 20 69 6e 20 70 4d 65 6d 2e 20 20  esult in pMem.  
1b044 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
1b045 72 20 6f 66 20 62 79 74 65 73 20 72 65 61 64 2e  r of bytes read.
1b046 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/ .SQLITE_PRIV
1b047 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 56  ATE u32 sqlite3V
1b048 64 62 65 53 65 72 69 61 6c 47 65 74 28 0a 20 20  dbeSerialGet(.  
1b049 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
1b04a 68 61 72 20 2a 62 75 66 2c 20 20 20 20 20 2f 2a  har *buf,     /*
1b04b 20 42 75 66 66 65 72 20 74 6f 20 64 65 73 65 72   Buffer to deser
1b04c 69 61 6c 69 7a 65 20 66 72 6f 6d 20 2a 2f 0a 20  ialize from */. 
1b04d 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65   u32 serial_type
1b04e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
1b04f 2a 20 53 65 72 69 61 6c 20 74 79 70 65 20 74 6f  * Serial type to
1b050 20 64 65 73 65 72 69 61 6c 69 7a 65 20 2a 2f 0a   deserialize */.
1b051 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 20 20 20 20    Mem *pMem     
1b052 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b053 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 74  /* Memory cell t
1b054 6f 20 77 72 69 74 65 20 76 61 6c 75 65 20 69 6e  o write value in
1b055 74 6f 20 2a 2f 0a 29 7b 0a 20 20 73 77 69 74 63  to */.){.  switc
1b056 68 28 20 73 65 72 69 61 6c 5f 74 79 70 65 20 29  h( serial_type )
1b057 7b 0a 20 20 20 20 63 61 73 65 20 31 30 3a 20 20  {.    case 10:  
1b058 20 2f 2a 20 52 65 73 65 72 76 65 64 20 66 6f 72   /* Reserved for
1b059 20 66 75 74 75 72 65 20 75 73 65 20 2a 2f 0a 20   future use */. 
1b05a 20 20 20 63 61 73 65 20 31 31 3a 20 20 20 2f 2a     case 11:   /*
1b05b 20 52 65 73 65 72 76 65 64 20 66 6f 72 20 66 75   Reserved for fu
1b05c 74 75 72 65 20 75 73 65 20 2a 2f 0a 20 20 20 20  ture use */.    
1b05d 63 61 73 65 20 30 3a 20 7b 20 20 2f 2a 20 4e 55  case 0: {  /* NU
1b05e 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d  LL */.      pMem
1b05f 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75  ->flags = MEM_Nu
1b060 6c 6c 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ll;.      break;
1b061 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
1b062 31 3a 20 7b 20 2f 2a 20 31 2d 62 79 74 65 20 73  1: { /* 1-byte s
1b063 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f  igned integer */
1b064 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69  .      pMem->u.i
1b065 20 3d 20 28 73 69 67 6e 65 64 20 63 68 61 72 29   = (signed char)
1b066 62 75 66 5b 30 5d 3b 0a 20 20 20 20 20 20 70 4d  buf[0];.      pM
1b067 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
1b068 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 72  Int;.      retur
1b069 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  n 1;.    }.    c
1b06a 61 73 65 20 32 3a 20 7b 20 2f 2a 20 32 2d 62 79  ase 2: { /* 2-by
1b06b 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65  te signed intege
1b06c 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d  r */.      pMem-
1b06d 3e 75 2e 69 20 3d 20 28 28 28 73 69 67 6e 65 64  >u.i = (((signed
1b06e 20 63 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c 38   char)buf[0])<<8
1b06f 29 20 7c 20 62 75 66 5b 31 5d 3b 0a 20 20 20 20  ) | buf[1];.    
1b070 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
1b071 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72  MEM_Int;.      r
1b072 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 7d 0a 20  eturn 2;.    }. 
1b073 20 20 20 63 61 73 65 20 33 3a 20 7b 20 2f 2a 20     case 3: { /* 
1b074 33 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e  3-byte signed in
1b075 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 70  teger */.      p
1b076 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 28 28 73 69  Mem->u.i = (((si
1b077 67 6e 65 64 20 63 68 61 72 29 62 75 66 5b 30 5d  gned char)buf[0]
1b078 29 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 31 5d  )<<16) | (buf[1]
1b079 3c 3c 38 29 20 7c 20 62 75 66 5b 32 5d 3b 0a 20  <<8) | buf[2];. 
1b07a 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73       pMem->flags
1b07b 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20   = MEM_Int;.    
1b07c 20 20 72 65 74 75 72 6e 20 33 3b 0a 20 20 20 20    return 3;.    
1b07d 7d 0a 20 20 20 20 63 61 73 65 20 34 3a 20 7b 20  }.    case 4: { 
1b07e 2f 2a 20 34 2d 62 79 74 65 20 73 69 67 6e 65 64  /* 4-byte signed
1b07f 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20   integer */.    
1b080 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 62    pMem->u.i = (b
1b081 75 66 5b 30 5d 3c 3c 32 34 29 20 7c 20 28 62 75  uf[0]<<24) | (bu
1b082 66 5b 31 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66  f[1]<<16) | (buf
1b083 5b 32 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 33 5d  [2]<<8) | buf[3]
1b084 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c  ;.      pMem->fl
1b085 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20  ags = MEM_Int;. 
1b086 20 20 20 20 20 72 65 74 75 72 6e 20 34 3b 0a 20       return 4;. 
1b087 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 35 3a     }.    case 5:
1b088 20 7b 20 2f 2a 20 36 2d 62 79 74 65 20 73 69 67   { /* 6-byte sig
1b089 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20  ned integer */. 
1b08a 20 20 20 20 20 75 36 34 20 78 20 3d 20 28 28 28       u64 x = (((
1b08b 73 69 67 6e 65 64 20 63 68 61 72 29 62 75 66 5b  signed char)buf[
1b08c 30 5d 29 3c 3c 38 29 20 7c 20 62 75 66 5b 31 5d  0])<<8) | buf[1]
1b08d 3b 0a 20 20 20 20 20 20 75 33 32 20 79 20 3d 20  ;.      u32 y = 
1b08e 28 62 75 66 5b 32 5d 3c 3c 32 34 29 20 7c 20 28  (buf[2]<<24) | (
1b08f 62 75 66 5b 33 5d 3c 3c 31 36 29 20 7c 20 28 62  buf[3]<<16) | (b
1b090 75 66 5b 34 5d 3c 3c 38 29 20 7c 20 62 75 66 5b  uf[4]<<8) | buf[
1b091 35 5d 3b 0a 20 20 20 20 20 20 78 20 3d 20 28 78  5];.      x = (x
1b092 3c 3c 33 32 29 20 7c 20 79 3b 0a 20 20 20 20 20  <<32) | y;.     
1b093 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 2a 28 69   pMem->u.i = *(i
1b094 36 34 2a 29 26 78 3b 0a 20 20 20 20 20 20 70 4d  64*)&x;.      pM
1b095 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
1b096 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 72  Int;.      retur
1b097 6e 20 36 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  n 6;.    }.    c
1b098 61 73 65 20 36 3a 20 20 20 2f 2a 20 38 2d 62 79  ase 6:   /* 8-by
1b099 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65  te signed intege
1b09a 72 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 37 3a  r */.    case 7:
1b09b 20 7b 20 2f 2a 20 49 45 45 45 20 66 6c 6f 61 74   { /* IEEE float
1b09c 69 6e 67 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 20  ing point */.   
1b09d 20 20 20 75 36 34 20 78 3b 0a 20 20 20 20 20 20     u64 x;.      
1b09e 75 33 32 20 79 3b 0a 23 69 66 20 21 64 65 66 69  u32 y;.#if !defi
1b09f 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26 20 21  ned(NDEBUG) && !
1b0a0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
1b0a1 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49  MIT_FLOATING_POI
1b0a2 4e 54 29 0a 20 20 20 20 20 20 2f 2a 20 56 65 72  NT).      /* Ver
1b0a3 69 66 79 20 74 68 61 74 20 69 6e 74 65 67 65 72  ify that integer
1b0a4 73 20 61 6e 64 20 66 6c 6f 61 74 69 6e 67 20 70  s and floating p
1b0a5 6f 69 6e 74 20 76 61 6c 75 65 73 20 75 73 65 20  oint values use 
1b0a6 74 68 65 20 73 61 6d 65 0a 20 20 20 20 20 20 2a  the same.      *
1b0a7 2a 20 62 79 74 65 20 6f 72 64 65 72 2e 20 20 4f  * byte order.  O
1b0a8 72 2c 20 74 68 61 74 20 69 66 20 53 51 4c 49 54  r, that if SQLIT
1b0a9 45 5f 4d 49 58 45 44 5f 45 4e 44 49 41 4e 5f 36  E_MIXED_ENDIAN_6
1b0aa 34 42 49 54 5f 46 4c 4f 41 54 20 69 73 0a 20 20  4BIT_FLOAT is.  
1b0ab 20 20 20 20 2a 2a 20 64 65 66 69 6e 65 64 20 74      ** defined t
1b0ac 68 61 74 20 36 34 2d 62 69 74 20 66 6c 6f 61 74  hat 64-bit float
1b0ad 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 73  ing point values
1b0ae 20 72 65 61 6c 6c 79 20 61 72 65 20 6d 69 78 65   really are mixe
1b0af 64 0a 20 20 20 20 20 20 2a 2a 20 65 6e 64 69 61  d.      ** endia
1b0b0 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  n..      */.    
1b0b1 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
1b0b2 36 34 20 74 31 20 3d 20 28 28 75 36 34 29 30 78  64 t1 = ((u64)0x
1b0b3 33 66 66 30 30 30 30 30 29 3c 3c 33 32 3b 0a 20  3ff00000)<<32;. 
1b0b4 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73       static cons
1b0b5 74 20 64 6f 75 62 6c 65 20 72 31 20 3d 20 31 2e  t double r1 = 1.
1b0b6 30 3b 0a 20 20 20 20 20 20 75 36 34 20 74 32 20  0;.      u64 t2 
1b0b7 3d 20 74 31 3b 0a 20 20 20 20 20 20 73 77 61 70  = t1;.      swap
1b0b8 4d 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74  MixedEndianFloat
1b0b9 28 74 32 29 3b 0a 20 20 20 20 20 20 61 73 73 65  (t2);.      asse
1b0ba 72 74 28 20 73 69 7a 65 6f 66 28 72 31 29 3d 3d  rt( sizeof(r1)==
1b0bb 73 69 7a 65 6f 66 28 74 32 29 20 26 26 20 6d 65  sizeof(t2) && me
1b0bc 6d 63 6d 70 28 26 72 31 2c 20 26 74 32 2c 20 73  mcmp(&r1, &t2, s
1b0bd 69 7a 65 6f 66 28 72 31 29 29 3d 3d 30 20 29 3b  izeof(r1))==0 );
1b0be 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 78  .#endif..      x
1b0bf 20 3d 20 28 62 75 66 5b 30 5d 3c 3c 32 34 29 20   = (buf[0]<<24) 
1b0c0 7c 20 28 62 75 66 5b 31 5d 3c 3c 31 36 29 20 7c  | (buf[1]<<16) |
1b0c1 20 28 62 75 66 5b 32 5d 3c 3c 38 29 20 7c 20 62   (buf[2]<<8) | b
1b0c2 75 66 5b 33 5d 3b 0a 20 20 20 20 20 20 79 20 3d  uf[3];.      y =
1b0c3 20 28 62 75 66 5b 34 5d 3c 3c 32 34 29 20 7c 20   (buf[4]<<24) | 
1b0c4 28 62 75 66 5b 35 5d 3c 3c 31 36 29 20 7c 20 28  (buf[5]<<16) | (
1b0c5 62 75 66 5b 36 5d 3c 3c 38 29 20 7c 20 62 75 66  buf[6]<<8) | buf
1b0c6 5b 37 5d 3b 0a 20 20 20 20 20 20 78 20 3d 20 28  [7];.      x = (
1b0c7 78 3c 3c 33 32 29 20 7c 20 79 3b 0a 20 20 20 20  x<<32) | y;.    
1b0c8 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79 70    if( serial_typ
1b0c9 65 3d 3d 36 20 29 7b 0a 20 20 20 20 20 20 20 20  e==6 ){.        
1b0ca 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 2a 28 69 36  pMem->u.i = *(i6
1b0cb 34 2a 29 26 78 3b 0a 20 20 20 20 20 20 20 20 70  4*)&x;.        p
1b0cc 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
1b0cd 5f 49 6e 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73  _Int;.      }els
1b0ce 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  e{.        asser
1b0cf 74 28 20 73 69 7a 65 6f 66 28 78 29 3d 3d 38 20  t( sizeof(x)==8 
1b0d0 26 26 20 73 69 7a 65 6f 66 28 70 4d 65 6d 2d 3e  && sizeof(pMem->
1b0d1 72 29 3d 3d 38 20 29 3b 0a 20 20 20 20 20 20 20  r)==8 );.       
1b0d2 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69 61 6e   swapMixedEndian
1b0d3 46 6c 6f 61 74 28 78 29 3b 0a 20 20 20 20 20 20  Float(x);.      
1b0d4 20 20 6d 65 6d 63 70 79 28 26 70 4d 65 6d 2d 3e    memcpy(&pMem->
1b0d5 72 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 78 29  r, &x, sizeof(x)
1b0d6 29 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d  );.        pMem-
1b0d7 3e 66 6c 61 67 73 20 3d 20 73 71 6c 69 74 65 33  >flags = sqlite3
1b0d8 49 73 4e 61 4e 28 70 4d 65 6d 2d 3e 72 29 20 3f  IsNaN(pMem->r) ?
1b0d9 20 4d 45 4d 5f 4e 75 6c 6c 20 3a 20 4d 45 4d 5f   MEM_Null : MEM_
1b0da 52 65 61 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Real;.      }.  
1b0db 20 20 20 20 72 65 74 75 72 6e 20 38 3b 0a 20 20      return 8;.  
1b0dc 20 20 7d 0a 20 20 20 20 63 61 73 65 20 38 3a 20    }.    case 8: 
1b0dd 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 30 20     /* Integer 0 
1b0de 2a 2f 0a 20 20 20 20 63 61 73 65 20 39 3a 20 7b  */.    case 9: {
1b0df 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 31 20 2a    /* Integer 1 *
1b0e0 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e  /.      pMem->u.
1b0e1 69 20 3d 20 73 65 72 69 61 6c 5f 74 79 70 65 2d  i = serial_type-
1b0e2 38 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66  8;.      pMem->f
1b0e3 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a  lags = MEM_Int;.
1b0e4 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
1b0e5 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c      }.    defaul
1b0e6 74 3a 20 7b 0a 20 20 20 20 20 20 75 33 32 20 6c  t: {.      u32 l
1b0e7 65 6e 20 3d 20 28 73 65 72 69 61 6c 5f 74 79 70  en = (serial_typ
1b0e8 65 2d 31 32 29 2f 32 3b 0a 20 20 20 20 20 20 70  e-12)/2;.      p
1b0e9 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 20 2a  Mem->z = (char *
1b0ea 29 62 75 66 3b 0a 20 20 20 20 20 20 70 4d 65 6d  )buf;.      pMem
1b0eb 2d 3e 6e 20 3d 20 6c 65 6e 3b 0a 20 20 20 20 20  ->n = len;.     
1b0ec 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 30 3b   pMem->xDel = 0;
1b0ed 0a 20 20 20 20 20 20 69 66 28 20 73 65 72 69 61  .      if( seria
1b0ee 6c 5f 74 79 70 65 26 30 78 30 31 20 29 7b 0a 20  l_type&0x01 ){. 
1b0ef 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61         pMem->fla
1b0f0 67 73 20 3d 20 4d 45 4d 5f 53 74 72 20 7c 20 4d  gs = MEM_Str | M
1b0f1 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20 20 20  EM_Ephem;.      
1b0f2 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70  }else{.        p
1b0f3 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
1b0f4 5f 42 6c 6f 62 20 7c 20 4d 45 4d 5f 45 70 68 65  _Blob | MEM_Ephe
1b0f5 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  m;.      }.     
1b0f6 20 72 65 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 20   return len;.   
1b0f7 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
1b0f8 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 69 76  0;.}.../*.** Giv
1b0f9 65 6e 20 74 68 65 20 6e 4b 65 79 2d 62 79 74 65  en the nKey-byte
1b0fa 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20 72   encoding of a r
1b0fb 65 63 6f 72 64 20 69 6e 20 70 4b 65 79 5b 5d 2c  ecord in pKey[],
1b0fc 20 70 61 72 73 65 20 74 68 65 0a 2a 2a 20 72 65   parse the.** re
1b0fd 63 6f 72 64 20 69 6e 74 6f 20 61 20 55 6e 70 61  cord into a Unpa
1b0fe 63 6b 65 64 52 65 63 6f 72 64 20 73 74 72 75 63  ckedRecord struc
1b0ff 74 75 72 65 2e 20 20 52 65 74 75 72 6e 20 61 20  ture.  Return a 
1b100 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 74 68  pointer to.** th
1b101 61 74 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  at structure..**
1b102 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67 20  .** The calling 
1b103 66 75 6e 63 74 69 6f 6e 20 6d 69 67 68 74 20 70  function might p
1b104 72 6f 76 69 64 65 20 73 7a 53 70 61 63 65 20 62  rovide szSpace b
1b105 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a  ytes of memory.*
1b106 2a 20 73 70 61 63 65 20 61 74 20 70 53 70 61 63  * space at pSpac
1b107 65 2e 20 20 54 68 69 73 20 73 70 61 63 65 20 63  e.  This space c
1b108 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 68 6f  an be used to ho
1b109 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65 64 0a  ld the returned.
1b10a 2a 2a 20 56 44 62 65 50 61 72 73 65 64 52 65 63  ** VDbeParsedRec
1b10b 6f 72 64 20 73 74 72 75 63 74 75 72 65 20 69 66  ord structure if
1b10c 20 69 74 20 69 73 20 6c 61 72 67 65 20 65 6e 6f   it is large eno
1b10d 75 67 68 2e 20 20 49 66 20 69 74 20 69 73 0a 2a  ugh.  If it is.*
1b10e 2a 20 6e 6f 74 20 62 69 67 20 65 6e 6f 75 67 68  * not big enough
1b10f 2c 20 73 70 61 63 65 20 69 73 20 6f 62 74 61 69  , space is obtai
1b110 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
1b111 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a  _malloc()..**.**
1b112 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 73 74   The returned st
1b113 72 75 63 74 75 72 65 20 73 68 6f 75 6c 64 20 62  ructure should b
1b114 65 20 63 6c 6f 73 65 64 20 62 79 20 61 20 63 61  e closed by a ca
1b115 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33  ll to.** sqlite3
1b116 56 64 62 65 44 65 6c 65 74 65 55 6e 70 61 63 6b  VdbeDeleteUnpack
1b117 65 64 52 65 63 6f 72 64 28 29 2e 0a 2a 2f 20 0a  edRecord()..*/ .
1b118 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 55  SQLITE_PRIVATE U
1b119 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 73  npackedRecord *s
1b11a 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64  qlite3VdbeRecord
1b11b 55 6e 70 61 63 6b 28 0a 20 20 4b 65 79 49 6e 66  Unpack(.  KeyInf
1b11c 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20  o *pKeyInfo,    
1b11d 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20   /* Information 
1b11e 61 62 6f 75 74 20 74 68 65 20 72 65 63 6f 72 64  about the record
1b11f 20 66 6f 72 6d 61 74 20 2a 2f 0a 20 20 69 6e 74   format */.  int
1b120 20 6e 4b 65 79 2c 20 20 20 20 20 20 20 20 20 20   nKey,          
1b121 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74      /* Size of t
1b122 68 65 20 62 69 6e 61 72 79 20 72 65 63 6f 72 64  he binary record
1b123 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
1b124 20 2a 70 4b 65 79 2c 20 20 20 20 20 20 2f 2a 20   *pKey,      /* 
1b125 54 68 65 20 62 69 6e 61 72 79 20 72 65 63 6f 72  The binary recor
1b126 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 70 53 70  d */.  char *pSp
1b127 61 63 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  ace,          /*
1b128 20 55 6e 61 6c 69 67 6e 65 64 20 73 70 61 63 65   Unaligned space
1b129 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 68 6f   available to ho
1b12a 6c 64 20 74 68 65 20 6f 62 6a 65 63 74 20 2a 2f  ld the object */
1b12b 0a 20 20 69 6e 74 20 73 7a 53 70 61 63 65 20 20  .  int szSpace  
1b12c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
1b12d 65 20 6f 66 20 70 53 70 61 63 65 5b 5d 20 69 6e  e of pSpace[] in
1b12e 20 62 79 74 65 73 20 2a 2f 0a 29 7b 0a 20 20 63   bytes */.){.  c
1b12f 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
1b130 61 72 20 2a 61 4b 65 79 20 3d 20 28 63 6f 6e 73  ar *aKey = (cons
1b131 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
1b132 2a 29 70 4b 65 79 3b 0a 20 20 55 6e 70 61 63 6b  *)pKey;.  Unpack
1b133 65 64 52 65 63 6f 72 64 20 2a 70 3b 20 20 2f 2a  edRecord *p;  /*
1b134 20 54 68 65 20 75 6e 70 61 63 6b 65 64 20 72 65   The unpacked re
1b135 63 6f 72 64 20 74 68 61 74 20 77 65 20 77 69 6c  cord that we wil
1b136 6c 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e  l return */.  in
1b137 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20  t nByte;        
1b138 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 73 70 61 63    /* Memory spac
1b139 65 20 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c 64  e needed to hold
1b13a 20 70 2c 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a   p, in bytes */.
1b13b 20 20 69 6e 74 20 64 3b 0a 20 20 75 33 32 20 69    int d;.  u32 i
1b13c 64 78 3b 0a 20 20 75 31 36 20 75 3b 20 20 20 20  dx;.  u16 u;    
1b13d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e 73            /* Uns
1b13e 69 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 6e 74  igned loop count
1b13f 65 72 20 2a 2f 0a 20 20 75 33 32 20 73 7a 48 64  er */.  u32 szHd
1b140 72 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a  r;.  Mem *pMem;.
1b141 20 20 69 6e 74 20 6e 4f 66 66 3b 20 20 20 20 20    int nOff;     
1b142 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 61 73        /* Increas
1b143 65 20 70 53 70 61 63 65 20 62 79 20 74 68 69 73  e pSpace by this
1b144 20 6d 75 63 68 20 74 6f 20 38 2d 62 79 74 65 20   much to 8-byte 
1b145 61 6c 69 67 6e 20 69 74 20 2a 2f 0a 20 20 0a 20  align it */.  . 
1b146 20 2f 2a 0a 20 20 2a 2a 20 57 65 20 77 61 6e 74   /*.  ** We want
1b147 20 74 6f 20 73 68 69 66 74 20 74 68 65 20 70 6f   to shift the po
1b148 69 6e 74 65 72 20 70 53 70 61 63 65 20 75 70 20  inter pSpace up 
1b149 73 75 63 68 20 74 68 61 74 20 69 74 20 69 73 20  such that it is 
1b14a 38 2d 62 79 74 65 20 61 6c 69 67 6e 65 64 2e 0a  8-byte aligned..
1b14b 20 20 2a 2a 20 54 68 75 73 2c 20 77 65 20 6e 65    ** Thus, we ne
1b14c 65 64 20 74 6f 20 63 61 6c 63 75 6c 61 74 65 20  ed to calculate 
1b14d 61 20 76 61 6c 75 65 2c 20 6e 4f 66 66 2c 20 62  a value, nOff, b
1b14e 65 74 77 65 65 6e 20 30 20 61 6e 64 20 37 2c 20  etween 0 and 7, 
1b14f 74 6f 20 73 68 69 66 74 20 0a 20 20 2a 2a 20 69  to shift .  ** i
1b150 74 20 62 79 2e 20 20 49 66 20 70 53 70 61 63 65  t by.  If pSpace
1b151 20 69 73 20 61 6c 72 65 61 64 79 20 38 2d 62 79   is already 8-by
1b152 74 65 20 61 6c 69 67 6e 65 64 2c 20 6e 4f 66 66  te aligned, nOff
1b153 20 73 68 6f 75 6c 64 20 62 65 20 7a 65 72 6f 2e   should be zero.
1b154 0a 20 20 2a 2f 0a 20 20 6e 4f 66 66 20 3d 20 28  .  */.  nOff = (
1b155 38 20 2d 20 28 53 51 4c 49 54 45 5f 50 54 52 5f  8 - (SQLITE_PTR_
1b156 54 4f 5f 49 4e 54 28 70 53 70 61 63 65 29 20 26  TO_INT(pSpace) &
1b157 20 37 29 29 20 26 20 37 3b 0a 20 20 70 53 70 61   7)) & 7;.  pSpa
1b158 63 65 20 2b 3d 20 6e 4f 66 66 3b 0a 20 20 73 7a  ce += nOff;.  sz
1b159 53 70 61 63 65 20 2d 3d 20 6e 4f 66 66 3b 0a 20  Space -= nOff;. 
1b15a 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44 38 28   nByte = ROUND8(
1b15b 73 69 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64 52  sizeof(UnpackedR
1b15c 65 63 6f 72 64 29 29 20 2b 20 73 69 7a 65 6f 66  ecord)) + sizeof
1b15d 28 4d 65 6d 29 2a 28 70 4b 65 79 49 6e 66 6f 2d  (Mem)*(pKeyInfo-
1b15e 3e 6e 46 69 65 6c 64 2b 31 29 3b 0a 20 20 69 66  >nField+1);.  if
1b15f 28 20 6e 42 79 74 65 3e 73 7a 53 70 61 63 65 20  ( nByte>szSpace 
1b160 29 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74  ){.    p = sqlit
1b161 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 4b  e3DbMallocRaw(pK
1b162 65 79 49 6e 66 6f 2d 3e 64 62 2c 20 6e 42 79 74  eyInfo->db, nByt
1b163 65 29 3b 0a 20 20 20 20 69 66 28 20 70 3d 3d 30  e);.    if( p==0
1b164 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20   ) return 0;.   
1b165 20 70 2d 3e 66 6c 61 67 73 20 3d 20 55 4e 50 41   p->flags = UNPA
1b166 43 4b 45 44 5f 4e 45 45 44 5f 46 52 45 45 20 7c  CKED_NEED_FREE |
1b167 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f 44   UNPACKED_NEED_D
1b168 45 53 54 52 4f 59 3b 0a 20 20 7d 65 6c 73 65 7b  ESTROY;.  }else{
1b169 0a 20 20 20 20 70 20 3d 20 28 55 6e 70 61 63 6b  .    p = (Unpack
1b16a 65 64 52 65 63 6f 72 64 2a 29 70 53 70 61 63 65  edRecord*)pSpace
1b16b 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d  ;.    p->flags =
1b16c 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f 44   UNPACKED_NEED_D
1b16d 45 53 54 52 4f 59 3b 0a 20 20 7d 0a 20 20 70 2d  ESTROY;.  }.  p-
1b16e 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4b 65 79  >pKeyInfo = pKey
1b16f 49 6e 66 6f 3b 0a 20 20 70 2d 3e 6e 46 69 65 6c  Info;.  p->nFiel
1b170 64 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46  d = pKeyInfo->nF
1b171 69 65 6c 64 20 2b 20 31 3b 0a 20 20 70 2d 3e 61  ield + 1;.  p->a
1b172 4d 65 6d 20 3d 20 70 4d 65 6d 20 3d 20 28 4d 65  Mem = pMem = (Me
1b173 6d 2a 29 26 28 28 63 68 61 72 2a 29 70 29 5b 52  m*)&((char*)p)[R
1b174 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 55 6e 70  OUND8(sizeof(Unp
1b175 61 63 6b 65 64 52 65 63 6f 72 64 29 29 5d 3b 0a  ackedRecord))];.
1b176 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f    assert( EIGHT_
1b177 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70  BYTE_ALIGNMENT(p
1b178 4d 65 6d 29 20 29 3b 0a 20 20 69 64 78 20 3d 20  Mem) );.  idx = 
1b179 67 65 74 56 61 72 69 6e 74 33 32 28 61 4b 65 79  getVarint32(aKey
1b17a 2c 20 73 7a 48 64 72 29 3b 0a 20 20 64 20 3d 20  , szHdr);.  d = 
1b17b 73 7a 48 64 72 3b 0a 20 20 75 20 3d 20 30 3b 0a  szHdr;.  u = 0;.
1b17c 20 20 77 68 69 6c 65 28 20 69 64 78 3c 73 7a 48    while( idx<szH
1b17d 64 72 20 26 26 20 75 3c 70 2d 3e 6e 46 69 65 6c  dr && u<p->nFiel
1b17e 64 20 26 26 20 64 3c 3d 6e 4b 65 79 20 29 7b 0a  d && d<=nKey ){.
1b17f 20 20 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74      u32 serial_t
1b180 79 70 65 3b 0a 0a 20 20 20 20 69 64 78 20 2b 3d  ype;..    idx +=
1b181 20 67 65 74 56 61 72 69 6e 74 33 32 28 26 61 4b   getVarint32(&aK
1b182 65 79 5b 69 64 78 5d 2c 20 73 65 72 69 61 6c 5f  ey[idx], serial_
1b183 74 79 70 65 29 3b 0a 20 20 20 20 70 4d 65 6d 2d  type);.    pMem-
1b184 3e 65 6e 63 20 3d 20 70 4b 65 79 49 6e 66 6f 2d  >enc = pKeyInfo-
1b185 3e 65 6e 63 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e  >enc;.    pMem->
1b186 64 62 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 64  db = pKeyInfo->d
1b187 62 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  b;.    pMem->fla
1b188 67 73 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d  gs = 0;.    pMem
1b189 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20  ->zMalloc = 0;. 
1b18a 20 20 20 64 20 2b 3d 20 73 71 6c 69 74 65 33 56     d += sqlite3V
1b18b 64 62 65 53 65 72 69 61 6c 47 65 74 28 26 61 4b  dbeSerialGet(&aK
1b18c 65 79 5b 64 5d 2c 20 73 65 72 69 61 6c 5f 74 79  ey[d], serial_ty
1b18d 70 65 2c 20 70 4d 65 6d 29 3b 0a 20 20 20 20 70  pe, pMem);.    p
1b18e 4d 65 6d 2b 2b 3b 0a 20 20 20 20 75 2b 2b 3b 0a  Mem++;.    u++;.
1b18f 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 75 3c    }.  assert( u<
1b190 3d 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c  =pKeyInfo->nFiel
1b191 64 20 2b 20 31 20 29 3b 0a 20 20 70 2d 3e 6e 46  d + 1 );.  p->nF
1b192 69 65 6c 64 20 3d 20 75 3b 0a 20 20 72 65 74 75  ield = u;.  retu
1b193 72 6e 20 28 76 6f 69 64 2a 29 70 3b 0a 7d 0a 0a  rn (void*)p;.}..
1b194 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
1b195 6e 65 20 64 65 73 74 72 6f 79 73 20 61 20 55 6e  ne destroys a Un
1b196 70 61 63 6b 65 64 52 65 63 6f 72 64 20 6f 62 6a  packedRecord obj
1b197 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ect..*/.SQLITE_P
1b198 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1b199 74 65 33 56 64 62 65 44 65 6c 65 74 65 55 6e 70  te3VdbeDeleteUnp
1b19a 61 63 6b 65 64 52 65 63 6f 72 64 28 55 6e 70 61  ackedRecord(Unpa
1b19b 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 29 7b 0a  ckedRecord *p){.
1b19c 20 20 69 6e 74 20 69 3b 0a 20 20 4d 65 6d 20 2a    int i;.  Mem *
1b19d 70 4d 65 6d 3b 0a 0a 20 20 61 73 73 65 72 74 28  pMem;..  assert(
1b19e 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72   p!=0 );.  asser
1b19f 74 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 55 4e  t( p->flags & UN
1b1a0 50 41 43 4b 45 44 5f 4e 45 45 44 5f 44 45 53 54  PACKED_NEED_DEST
1b1a1 52 4f 59 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30  ROY );.  for(i=0
1b1a2 2c 20 70 4d 65 6d 3d 70 2d 3e 61 4d 65 6d 3b 20  , pMem=p->aMem; 
1b1a3 69 3c 70 2d 3e 6e 46 69 65 6c 64 3b 20 69 2b 2b  i<p->nField; i++
1b1a4 2c 20 70 4d 65 6d 2b 2b 29 7b 0a 20 20 20 20 2f  , pMem++){.    /
1b1a5 2a 20 54 68 65 20 75 6e 70 61 63 6b 65 64 20 72  * The unpacked r
1b1a6 65 63 6f 72 64 20 69 73 20 61 6c 77 61 79 73 20  ecord is always 
1b1a7 63 6f 6e 73 74 72 75 63 74 65 64 20 62 79 20 74  constructed by t
1b1a8 68 65 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65  he.    ** sqlite
1b1a9 33 56 64 62 65 55 6e 70 61 63 6b 52 65 63 6f 72  3VdbeUnpackRecor
1b1aa 64 28 29 20 66 75 6e 63 74 69 6f 6e 20 61 62 6f  d() function abo
1b1ab 76 65 2c 20 77 68 69 63 68 20 6d 61 6b 65 73 20  ve, which makes 
1b1ac 61 6c 6c 0a 20 20 20 20 2a 2a 20 73 74 72 69 6e  all.    ** strin
1b1ad 67 73 20 61 6e 64 20 62 6c 6f 62 73 20 73 74 61  gs and blobs sta
1b1ae 74 69 63 2e 20 20 41 6e 64 20 6e 6f 6e 65 20 6f  tic.  And none o
1b1af 66 20 74 68 65 20 65 6c 65 6d 65 6e 74 73 20 61  f the elements a
1b1b0 72 65 0a 20 20 20 20 2a 2a 20 65 76 65 72 20 74  re.    ** ever t
1b1b1 72 61 6e 73 66 6f 72 6d 65 64 2c 20 73 6f 20 74  ransformed, so t
1b1b2 68 65 72 65 20 69 73 20 6e 65 76 65 72 20 61 6e  here is never an
1b1b3 79 74 68 69 6e 67 20 74 6f 20 64 65 6c 65 74 65  ything to delete
1b1b4 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
1b1b5 20 4e 45 56 45 52 28 70 4d 65 6d 2d 3e 7a 4d 61   NEVER(pMem->zMa
1b1b6 6c 6c 6f 63 29 20 29 20 73 71 6c 69 74 65 33 56  lloc) ) sqlite3V
1b1b7 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d  dbeMemRelease(pM
1b1b8 65 6d 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  em);.  }.  if( p
1b1b9 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43 4b  ->flags & UNPACK
1b1ba 45 44 5f 4e 45 45 44 5f 46 52 45 45 20 29 7b 0a  ED_NEED_FREE ){.
1b1bb 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1b1bc 65 28 70 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e 64  e(p->pKeyInfo->d
1b1bd 62 2c 20 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  b, p);.  }.}../*
1b1be 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
1b1bf 6e 20 63 6f 6d 70 61 72 65 73 20 74 68 65 20 74  n compares the t
1b1c0 77 6f 20 74 61 62 6c 65 20 72 6f 77 73 20 6f 72  wo table rows or
1b1c1 20 69 6e 64 65 78 20 72 65 63 6f 72 64 73 0a 2a   index records.*
1b1c2 2a 20 73 70 65 63 69 66 69 65 64 20 62 79 20 7b  * specified by {
1b1c3 6e 4b 65 79 31 2c 20 70 4b 65 79 31 7d 20 61 6e  nKey1, pKey1} an
1b1c4 64 20 70 50 4b 65 79 32 2e 20 20 49 74 20 72 65  d pPKey2.  It re
1b1c5 74 75 72 6e 73 20 61 20 6e 65 67 61 74 69 76 65  turns a negative
1b1c6 2c 20 7a 65 72 6f 0a 2a 2a 20 6f 72 20 70 6f 73  , zero.** or pos
1b1c7 69 74 69 76 65 20 69 6e 74 65 67 65 72 20 69 66  itive integer if
1b1c8 20 6b 65 79 31 20 69 73 20 6c 65 73 73 20 74 68   key1 is less th
1b1c9 61 6e 2c 20 65 71 75 61 6c 20 74 6f 20 6f 72 20  an, equal to or 
1b1ca 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e  .** greater than
1b1cb 20 6b 65 79 32 2e 20 20 54 68 65 20 7b 6e 4b 65   key2.  The {nKe
1b1cc 79 31 2c 20 70 4b 65 79 31 7d 20 6b 65 79 20 6d  y1, pKey1} key m
1b1cd 75 73 74 20 62 65 20 61 20 62 6c 6f 62 0a 2a 2a  ust be a blob.**
1b1ce 20 63 72 65 61 74 65 64 20 62 79 20 74 68 20 4f   created by th O
1b1cf 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63  P_MakeRecord opc
1b1d0 6f 64 65 20 6f 66 20 74 68 65 20 56 44 42 45 2e  ode of the VDBE.
1b1d1 20 20 54 68 65 20 70 50 4b 65 79 32 0a 2a 2a 20    The pPKey2.** 
1b1d2 6b 65 79 20 6d 75 73 74 20 62 65 20 61 20 70 61  key must be a pa
1b1d3 72 73 65 64 20 6b 65 79 20 73 75 63 68 20 61 73  rsed key such as
1b1d4 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a   obtained from.*
1b1d5 2a 20 73 71 6c 69 74 65 33 56 64 62 65 50 61 72  * sqlite3VdbePar
1b1d6 73 65 52 65 63 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20  seRecord..**.** 
1b1d7 4b 65 79 31 20 61 6e 64 20 4b 65 79 32 20 64 6f  Key1 and Key2 do
1b1d8 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 63 6f 6e   not have to con
1b1d9 74 61 69 6e 20 74 68 65 20 73 61 6d 65 20 6e 75  tain the same nu
1b1da 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73 2e 0a  mber of fields..
1b1db 2a 2a 20 54 68 65 20 6b 65 79 20 77 69 74 68 20  ** The key with 
1b1dc 66 65 77 65 72 20 66 69 65 6c 64 73 20 69 73 20  fewer fields is 
1b1dd 75 73 75 61 6c 6c 79 20 63 6f 6d 70 61 72 65 73  usually compares
1b1de 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 0a   less than the .
1b1df 2a 2a 20 6c 6f 6e 67 65 72 20 6b 65 79 2e 20 20  ** longer key.  
1b1e0 48 6f 77 65 76 65 72 20 69 66 20 74 68 65 20 55  However if the U
1b1e1 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20  NPACKED_INCRKEY 
1b1e2 66 6c 61 67 73 20 69 6e 20 70 50 4b 65 79 32 20  flags in pPKey2 
1b1e3 69 73 20 73 65 74 0a 2a 2a 20 61 6e 64 20 74 68  is set.** and th
1b1e4 65 20 63 6f 6d 6d 6f 6e 20 70 72 65 66 69 78 65  e common prefixe
1b1e5 73 20 61 72 65 20 65 71 75 61 6c 2c 20 74 68 65  s are equal, the
1b1e6 6e 20 6b 65 79 31 20 69 73 20 6c 65 73 73 20 74  n key1 is less t
1b1e7 68 61 6e 20 6b 65 79 32 2e 0a 2a 2a 20 4f 72 20  han key2..** Or 
1b1e8 69 66 20 74 68 65 20 55 4e 50 41 43 4b 45 44 5f  if the UNPACKED_
1b1e9 4d 41 54 43 48 5f 50 52 45 46 49 58 20 66 6c 61  MATCH_PREFIX fla
1b1ea 67 20 69 73 20 73 65 74 20 61 6e 64 20 74 68 65  g is set and the
1b1eb 20 70 72 65 66 69 78 65 73 20 61 72 65 0a 2a 2a   prefixes are.**
1b1ec 20 65 71 75 61 6c 2c 20 74 68 65 6e 20 74 68 65   equal, then the
1b1ed 20 6b 65 79 73 20 61 72 65 20 63 6f 6e 73 69 64   keys are consid
1b1ee 65 72 65 64 20 74 6f 20 62 65 20 65 71 75 61 6c  ered to be equal
1b1ef 20 61 6e 64 0a 2a 2a 20 74 68 65 20 70 61 72 74   and.** the part
1b1f0 73 20 62 65 79 6f 6e 64 20 74 68 65 20 63 6f 6d  s beyond the com
1b1f1 6d 6f 6e 20 70 72 65 66 69 78 20 61 72 65 20 69  mon prefix are i
1b1f2 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  gnored..**.** If
1b1f3 20 74 68 65 20 55 4e 50 41 43 4b 45 44 5f 49 47   the UNPACKED_IG
1b1f4 4e 4f 52 45 5f 52 4f 57 49 44 20 66 6c 61 67 20  NORE_ROWID flag 
1b1f5 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65  is set, then the
1b1f6 20 6c 61 73 74 20 62 79 74 65 20 6f 66 0a 2a 2a   last byte of.**
1b1f7 20 74 68 65 20 68 65 61 64 65 72 20 6f 66 20 70   the header of p
1b1f8 4b 65 79 31 20 69 73 20 69 67 6e 6f 72 65 64 2e  Key1 is ignored.
1b1f9 20 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20    It is assumed 
1b1fa 74 68 61 74 20 70 4b 65 79 31 20 69 73 0a 2a 2a  that pKey1 is.**
1b1fb 20 61 6e 20 69 6e 64 65 78 20 6b 65 79 2c 20 61   an index key, a
1b1fc 6e 64 20 74 68 75 73 20 65 6e 64 73 20 77 69 74  nd thus ends wit
1b1fd 68 20 61 20 72 6f 77 69 64 20 76 61 6c 75 65 2e  h a rowid value.
1b1fe 20 20 54 68 65 20 6c 61 73 74 20 62 79 74 65 0a    The last byte.
1b1ff 2a 2a 20 6f 66 20 74 68 65 20 68 65 61 64 65 72  ** of the header
1b200 20 77 69 6c 6c 20 74 68 65 72 65 66 6f 72 65 20   will therefore 
1b201 62 65 20 74 68 65 20 73 65 72 69 61 6c 20 74 79  be the serial ty
1b202 70 65 20 6f 66 20 74 68 65 20 72 6f 77 69 64 3a  pe of the rowid:
1b203 0a 2a 2a 20 6f 6e 65 20 6f 66 20 31 2c 20 32 2c  .** one of 1, 2,
1b204 20 33 2c 20 34 2c 20 35 2c 20 36 2c 20 38 2c 20   3, 4, 5, 6, 8, 
1b205 6f 72 20 39 20 2d 20 74 68 65 20 69 6e 74 65 67  or 9 - the integ
1b206 65 72 20 73 65 72 69 61 6c 20 74 79 70 65 73 2e  er serial types.
1b207 0a 2a 2a 20 54 68 65 20 73 65 72 69 61 6c 20 74  .** The serial t
1b208 79 70 65 20 6f 66 20 74 68 65 20 66 69 6e 61 6c  ype of the final
1b209 20 72 6f 77 69 64 20 77 69 6c 6c 20 61 6c 77 61   rowid will alwa
1b20a 79 73 20 62 65 20 61 20 73 69 6e 67 6c 65 20 62  ys be a single b
1b20b 79 74 65 2e 0a 2a 2a 20 42 79 20 69 67 6e 6f 72  yte..** By ignor
1b20c 69 6e 67 20 74 68 69 73 20 6c 61 73 74 20 62 79  ing this last by
1b20d 74 65 20 6f 66 20 74 68 65 20 68 65 61 64 65 72  te of the header
1b20e 2c 20 77 65 20 66 6f 72 63 65 20 74 68 65 20 63  , we force the c
1b20f 6f 6d 70 61 72 69 73 6f 6e 0a 2a 2a 20 74 6f 20  omparison.** to 
1b210 69 67 6e 6f 72 65 20 74 68 65 20 72 6f 77 69 64  ignore the rowid
1b211 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 6b   at the end of k
1b212 65 79 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ey1..*/.SQLITE_P
1b213 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1b214 65 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70  e3VdbeRecordComp
1b215 61 72 65 28 0a 20 20 69 6e 74 20 6e 4b 65 79 31  are(.  int nKey1
1b216 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b  , const void *pK
1b217 65 79 31 2c 20 2f 2a 20 4c 65 66 74 20 6b 65 79  ey1, /* Left key
1b218 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 64 52 65   */.  UnpackedRe
1b219 63 6f 72 64 20 2a 70 50 4b 65 79 32 20 20 20 20  cord *pPKey2    
1b21a 20 20 20 20 2f 2a 20 52 69 67 68 74 20 6b 65 79      /* Right key
1b21b 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 64 31 3b   */.){.  int d1;
1b21c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
1b21d 66 66 73 65 74 20 69 6e 74 6f 20 61 4b 65 79 5b  ffset into aKey[
1b21e 5d 20 6f 66 20 6e 65 78 74 20 64 61 74 61 20 65  ] of next data e
1b21f 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 75 33 32 20  lement */.  u32 
1b220 69 64 78 31 3b 20 20 20 20 20 20 20 20 20 20 2f  idx1;          /
1b221 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 61 4b  * Offset into aK
1b222 65 79 5b 5d 20 6f 66 20 6e 65 78 74 20 68 65 61  ey[] of next hea
1b223 64 65 72 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20  der element */. 
1b224 20 75 33 32 20 73 7a 48 64 72 31 3b 20 20 20 20   u32 szHdr1;    
1b225 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1b226 20 62 79 74 65 73 20 69 6e 20 68 65 61 64 65 72   bytes in header
1b227 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 30 3b   */.  int i = 0;
1b228 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 0a 20  .  int nField;. 
1b229 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 63   int rc = 0;.  c
1b22a 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
1b22b 61 72 20 2a 61 4b 65 79 31 20 3d 20 28 63 6f 6e  ar *aKey1 = (con
1b22c 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
1b22d 20 2a 29 70 4b 65 79 31 3b 0a 20 20 4b 65 79 49   *)pKey1;.  KeyI
1b22e 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20  nfo *pKeyInfo;. 
1b22f 20 4d 65 6d 20 6d 65 6d 31 3b 0a 0a 20 20 70 4b   Mem mem1;..  pK
1b230 65 79 49 6e 66 6f 20 3d 20 70 50 4b 65 79 32 2d  eyInfo = pPKey2-
1b231 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 6d 65 6d  >pKeyInfo;.  mem
1b232 31 2e 65 6e 63 20 3d 20 70 4b 65 79 49 6e 66 6f  1.enc = pKeyInfo
1b233 2d 3e 65 6e 63 3b 0a 20 20 6d 65 6d 31 2e 64 62  ->enc;.  mem1.db
1b234 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 64 62 3b   = pKeyInfo->db;
1b235 0a 20 20 6d 65 6d 31 2e 66 6c 61 67 73 20 3d 20  .  mem1.flags = 
1b236 30 3b 0a 20 20 6d 65 6d 31 2e 75 2e 69 20 3d 20  0;.  mem1.u.i = 
1b237 30 3b 20 20 2f 2a 20 6e 6f 74 20 6e 65 65 64 65  0;  /* not neede
1b238 64 2c 20 68 65 72 65 20 74 6f 20 73 69 6c 65 6e  d, here to silen
1b239 63 65 20 63 6f 6d 70 69 6c 65 72 20 77 61 72 6e  ce compiler warn
1b23a 69 6e 67 20 2a 2f 0a 20 20 6d 65 6d 31 2e 7a 4d  ing */.  mem1.zM
1b23b 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 0a 20 20  alloc = 0;.  .  
1b23c 69 64 78 31 20 3d 20 67 65 74 56 61 72 69 6e 74  idx1 = getVarint
1b23d 33 32 28 61 4b 65 79 31 2c 20 73 7a 48 64 72 31  32(aKey1, szHdr1
1b23e 29 3b 0a 20 20 64 31 20 3d 20 73 7a 48 64 72 31  );.  d1 = szHdr1
1b23f 3b 0a 20 20 69 66 28 20 70 50 4b 65 79 32 2d 3e  ;.  if( pPKey2->
1b240 66 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44  flags & UNPACKED
1b241 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 20 29 7b  _IGNORE_ROWID ){
1b242 0a 20 20 20 20 73 7a 48 64 72 31 2d 2d 3b 0a 20  .    szHdr1--;. 
1b243 20 7d 0a 20 20 6e 46 69 65 6c 64 20 3d 20 70 4b   }.  nField = pK
1b244 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 3b 0a  eyInfo->nField;.
1b245 20 20 77 68 69 6c 65 28 20 69 64 78 31 3c 73 7a    while( idx1<sz
1b246 48 64 72 31 20 26 26 20 69 3c 70 50 4b 65 79 32  Hdr1 && i<pPKey2
1b247 2d 3e 6e 46 69 65 6c 64 20 29 7b 0a 20 20 20 20  ->nField ){.    
1b248 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 31  u32 serial_type1
1b249 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74  ;..    /* Read t
1b24a 68 65 20 73 65 72 69 61 6c 20 74 79 70 65 73 20  he serial types 
1b24b 66 6f 72 20 74 68 65 20 6e 65 78 74 20 65 6c 65  for the next ele
1b24c 6d 65 6e 74 20 69 6e 20 65 61 63 68 20 6b 65 79  ment in each key
1b24d 2e 20 2a 2f 0a 20 20 20 20 69 64 78 31 20 2b 3d  . */.    idx1 +=
1b24e 20 67 65 74 56 61 72 69 6e 74 33 32 28 20 61 4b   getVarint32( aK
1b24f 65 79 31 2b 69 64 78 31 2c 20 73 65 72 69 61 6c  ey1+idx1, serial
1b250 5f 74 79 70 65 31 20 29 3b 0a 20 20 20 20 69 66  _type1 );.    if
1b251 28 20 64 31 3e 3d 6e 4b 65 79 31 20 26 26 20 73  ( d1>=nKey1 && s
1b252 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
1b253 54 79 70 65 4c 65 6e 28 73 65 72 69 61 6c 5f 74  TypeLen(serial_t
1b254 79 70 65 31 29 3e 30 20 29 20 62 72 65 61 6b 3b  ype1)>0 ) break;
1b255 0a 0a 20 20 20 20 2f 2a 20 45 78 74 72 61 63 74  ..    /* Extract
1b256 20 74 68 65 20 76 61 6c 75 65 73 20 74 6f 20 62   the values to b
1b257 65 20 63 6f 6d 70 61 72 65 64 2e 0a 20 20 20 20  e compared..    
1b258 2a 2f 0a 20 20 20 20 64 31 20 2b 3d 20 73 71 6c  */.    d1 += sql
1b259 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 65  ite3VdbeSerialGe
1b25a 74 28 26 61 4b 65 79 31 5b 64 31 5d 2c 20 73 65  t(&aKey1[d1], se
1b25b 72 69 61 6c 5f 74 79 70 65 31 2c 20 26 6d 65 6d  rial_type1, &mem
1b25c 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 74  1);..    /* Do t
1b25d 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 0a 20 20  he comparison.  
1b25e 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71    */.    rc = sq
1b25f 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28  lite3MemCompare(
1b260 26 6d 65 6d 31 2c 20 26 70 50 4b 65 79 32 2d 3e  &mem1, &pPKey2->
1b261 61 4d 65 6d 5b 69 5d 2c 0a 20 20 20 20 20 20 20  aMem[i],.       
1b262 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b263 20 20 20 20 69 3c 6e 46 69 65 6c 64 20 3f 20 70      i<nField ? p
1b264 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 69  KeyInfo->aColl[i
1b265 5d 20 3a 20 30 29 3b 0a 20 20 20 20 69 66 28 20  ] : 0);.    if( 
1b266 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 62  rc!=0 ){.      b
1b267 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1b268 69 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e  i++;.  }..  /* N
1b269 6f 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  o memory allocat
1b26a 69 6f 6e 20 69 73 20 65 76 65 72 20 75 73 65 64  ion is ever used
1b26b 20 6f 6e 20 6d 65 6d 31 2e 20 2a 2f 0a 20 20 69   on mem1. */.  i
1b26c 66 28 20 4e 45 56 45 52 28 6d 65 6d 31 2e 7a 4d  f( NEVER(mem1.zM
1b26d 61 6c 6c 6f 63 29 20 29 20 73 71 6c 69 74 65 33  alloc) ) sqlite3
1b26e 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26  VdbeMemRelease(&
1b26f 6d 65 6d 31 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  mem1);..  /* If 
1b270 74 68 65 20 50 52 45 46 49 58 5f 53 45 41 52 43  the PREFIX_SEARC
1b271 48 20 66 6c 61 67 20 69 73 20 73 65 74 20 61 6e  H flag is set an
1b272 64 20 61 6c 6c 20 66 69 65 6c 64 73 20 65 78 63  d all fields exc
1b273 65 70 74 20 74 68 65 20 66 69 6e 61 6c 0a 20 20  ept the final.  
1b274 2a 2a 20 72 6f 77 69 64 20 66 69 65 6c 64 20 77  ** rowid field w
1b275 65 72 65 20 65 71 75 61 6c 2c 20 74 68 65 6e 20  ere equal, then 
1b276 63 6c 65 61 72 20 74 68 65 20 50 52 45 46 49 58  clear the PREFIX
1b277 5f 53 45 41 52 43 48 20 66 6c 61 67 20 61 6e 64  _SEARCH flag and
1b278 20 73 65 74 20 0a 20 20 2a 2a 20 70 50 4b 65 79   set .  ** pPKey
1b279 32 2d 3e 72 6f 77 69 64 20 74 6f 20 74 68 65 20  2->rowid to the 
1b27a 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 6f 77  value of the row
1b27b 69 64 20 66 69 65 6c 64 20 69 6e 20 28 70 4b 65  id field in (pKe
1b27c 79 31 2c 20 6e 4b 65 79 31 29 2e 0a 20 20 2a 2a  y1, nKey1)..  **
1b27d 20 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79   This is used by
1b27e 20 74 68 65 20 4f 50 5f 49 73 55 6e 69 71 75 65   the OP_IsUnique
1b27f 20 6f 70 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20   opcode..  */.  
1b280 69 66 28 20 28 70 50 4b 65 79 32 2d 3e 66 6c 61  if( (pPKey2->fla
1b281 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 50 52  gs & UNPACKED_PR
1b282 45 46 49 58 5f 53 45 41 52 43 48 29 20 26 26 20  EFIX_SEARCH) && 
1b283 69 3d 3d 28 70 50 4b 65 79 32 2d 3e 6e 46 69 65  i==(pPKey2->nFie
1b284 6c 64 2d 31 29 20 29 7b 0a 20 20 20 20 61 73 73  ld-1) ){.    ass
1b285 65 72 74 28 20 69 64 78 31 3d 3d 73 7a 48 64 72  ert( idx1==szHdr
1b286 31 20 26 26 20 72 63 20 29 3b 0a 20 20 20 20 61  1 && rc );.    a
1b287 73 73 65 72 74 28 20 6d 65 6d 31 2e 66 6c 61 67  ssert( mem1.flag
1b288 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20  s & MEM_Int );. 
1b289 20 20 20 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73     pPKey2->flags
1b28a 20 26 3d 20 7e 55 4e 50 41 43 4b 45 44 5f 50 52   &= ~UNPACKED_PR
1b28b 45 46 49 58 5f 53 45 41 52 43 48 3b 0a 20 20 20  EFIX_SEARCH;.   
1b28c 20 70 50 4b 65 79 32 2d 3e 72 6f 77 69 64 20 3d   pPKey2->rowid =
1b28d 20 6d 65 6d 31 2e 75 2e 69 3b 0a 20 20 7d 0a 0a   mem1.u.i;.  }..
1b28e 20 20 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20    if( rc==0 ){. 
1b28f 20 20 20 2f 2a 20 72 63 3d 3d 30 20 68 65 72 65     /* rc==0 here
1b290 20 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e 65 20   means that one 
1b291 6f 66 20 74 68 65 20 6b 65 79 73 20 72 61 6e 20  of the keys ran 
1b292 6f 75 74 20 6f 66 20 66 69 65 6c 64 73 20 61 6e  out of fields an
1b293 64 0a 20 20 20 20 2a 2a 20 61 6c 6c 20 74 68 65  d.    ** all the
1b294 20 66 69 65 6c 64 73 20 75 70 20 74 6f 20 74 68   fields up to th
1b295 61 74 20 70 6f 69 6e 74 20 77 65 72 65 20 65 71  at point were eq
1b296 75 61 6c 2e 20 49 66 20 74 68 65 20 55 4e 50 41  ual. If the UNPA
1b297 43 4b 45 44 5f 49 4e 43 52 4b 45 59 0a 20 20 20  CKED_INCRKEY.   
1b298 20 2a 2a 20 66 6c 61 67 20 69 73 20 73 65 74 2c   ** flag is set,
1b299 20 74 68 65 6e 20 62 72 65 61 6b 20 74 68 65 20   then break the 
1b29a 74 69 65 20 62 79 20 74 72 65 61 74 69 6e 67 20  tie by treating 
1b29b 6b 65 79 32 20 61 73 20 6c 61 72 67 65 72 2e 0a  key2 as larger..
1b29c 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 55 50      ** If the UP
1b29d 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54  ACKED_PREFIX_MAT
1b29e 43 48 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20  CH flag is set, 
1b29f 74 68 65 6e 20 6b 65 79 73 20 77 69 74 68 20 63  then keys with c
1b2a0 6f 6d 6d 6f 6e 20 70 72 65 66 69 78 65 73 0a 20  ommon prefixes. 
1b2a1 20 20 20 2a 2a 20 61 72 65 20 63 6f 6e 73 69 64     ** are consid
1b2a2 65 72 65 64 20 74 6f 20 62 65 20 65 71 75 61 6c  ered to be equal
1b2a3 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68  .  Otherwise, th
1b2a4 65 20 6c 6f 6e 67 65 72 20 6b 65 79 20 69 73 20  e longer key is 
1b2a5 74 68 65 20 0a 20 20 20 20 2a 2a 20 6c 61 72 67  the .    ** larg
1b2a6 65 72 2e 20 20 41 73 20 69 74 20 68 61 70 70 65  er.  As it happe
1b2a7 6e 73 2c 20 74 68 65 20 70 50 4b 65 79 32 20 77  ns, the pPKey2 w
1b2a8 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 20 74 68  ill always be th
1b2a9 65 20 6c 6f 6e 67 65 72 0a 20 20 20 20 2a 2a 20  e longer.    ** 
1b2aa 69 66 20 74 68 65 72 65 20 69 73 20 61 20 64 69  if there is a di
1b2ab 66 66 65 72 65 6e 63 65 2e 0a 20 20 20 20 2a 2f  fference..    */
1b2ac 0a 20 20 20 20 69 66 28 20 70 50 4b 65 79 32 2d  .    if( pPKey2-
1b2ad 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45  >flags & UNPACKE
1b2ae 44 5f 49 4e 43 52 4b 45 59 20 29 7b 0a 20 20 20  D_INCRKEY ){.   
1b2af 20 20 20 72 63 20 3d 20 2d 31 3b 0a 20 20 20 20     rc = -1;.    
1b2b0 7d 65 6c 73 65 20 69 66 28 20 70 50 4b 65 79 32  }else if( pPKey2
1b2b1 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43 4b  ->flags & UNPACK
1b2b2 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 43 48 20  ED_PREFIX_MATCH 
1b2b3 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4c 65 61 76  ){.      /* Leav
1b2b4 65 20 72 63 3d 3d 30 20 2a 2f 0a 20 20 20 20 7d  e rc==0 */.    }
1b2b5 65 6c 73 65 20 69 66 28 20 69 64 78 31 3c 73 7a  else if( idx1<sz
1b2b6 48 64 72 31 20 29 7b 0a 20 20 20 20 20 20 72 63  Hdr1 ){.      rc
1b2b7 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65   = 1;.    }.  }e
1b2b8 6c 73 65 20 69 66 28 20 70 4b 65 79 49 6e 66 6f  lse if( pKeyInfo
1b2b9 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 26 26 20  ->aSortOrder && 
1b2ba 69 3c 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65  i<pKeyInfo->nFie
1b2bb 6c 64 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ld.             
1b2bc 20 20 26 26 20 70 4b 65 79 49 6e 66 6f 2d 3e 61    && pKeyInfo->a
1b2bd 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 29 7b 0a  SortOrder[i] ){.
1b2be 20 20 20 20 72 63 20 3d 20 2d 72 63 3b 0a 20 20      rc = -rc;.  
1b2bf 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  }..  return rc;.
1b2c0 7d 0a 20 0a 0a 2f 2a 0a 2a 2a 20 70 43 75 72 20  }. ../*.** pCur 
1b2c1 70 6f 69 6e 74 73 20 61 74 20 61 6e 20 69 6e 64  points at an ind
1b2c2 65 78 20 65 6e 74 72 79 20 63 72 65 61 74 65 64  ex entry created
1b2c3 20 75 73 69 6e 67 20 74 68 65 20 4f 50 5f 4d 61   using the OP_Ma
1b2c4 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 2e  keRecord opcode.
1b2c5 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 72 6f 77  .** Read the row
1b2c6 69 64 20 28 74 68 65 20 6c 61 73 74 20 66 69 65  id (the last fie
1b2c7 6c 64 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64  ld in the record
1b2c8 29 20 61 6e 64 20 73 74 6f 72 65 20 69 74 20 69  ) and store it i
1b2c9 6e 20 2a 72 6f 77 69 64 2e 0a 2a 2a 20 52 65 74  n *rowid..** Ret
1b2ca 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66  urn SQLITE_OK if
1b2cb 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f 72 6b   everything work
1b2cc 73 2c 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63  s, or an error c
1b2cd 6f 64 65 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a  ode otherwise..*
1b2ce 2a 0a 2a 2a 20 70 43 75 72 20 6d 69 67 68 74 20  *.** pCur might 
1b2cf 62 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74  be pointing to t
1b2d0 65 78 74 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ext obtained fro
1b2d1 6d 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 61  m a corrupt data
1b2d2 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 53 6f  base file..** So
1b2d3 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 63 61 6e   the content can
1b2d4 6e 6f 74 20 62 65 20 74 72 75 73 74 65 64 2e 20  not be trusted. 
1b2d5 20 44 6f 20 61 70 70 72 6f 70 72 69 61 74 65 20   Do appropriate 
1b2d6 63 68 65 63 6b 73 20 6f 6e 20 74 68 65 20 63 6f  checks on the co
1b2d7 6e 74 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ntent..*/.SQLITE
1b2d8 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1b2d9 69 74 65 33 56 64 62 65 49 64 78 52 6f 77 69 64  ite3VdbeIdxRowid
1b2da 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 42 74  (sqlite3 *db, Bt
1b2db 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 36  Cursor *pCur, i6
1b2dc 34 20 2a 72 6f 77 69 64 29 7b 0a 20 20 69 36 34  4 *rowid){.  i64
1b2dd 20 6e 43 65 6c 6c 4b 65 79 20 3d 20 30 3b 0a 20   nCellKey = 0;. 
1b2de 20 69 6e 74 20 72 63 3b 0a 20 20 75 33 32 20 73   int rc;.  u32 s
1b2df 7a 48 64 72 3b 20 20 20 20 20 20 20 20 2f 2a 20  zHdr;        /* 
1b2e0 53 69 7a 65 20 6f 66 20 74 68 65 20 68 65 61 64  Size of the head
1b2e1 65 72 20 2a 2f 0a 20 20 75 33 32 20 74 79 70 65  er */.  u32 type
1b2e2 52 6f 77 69 64 3b 20 20 20 20 2f 2a 20 53 65 72  Rowid;    /* Ser
1b2e3 69 61 6c 20 74 79 70 65 20 6f 66 20 74 68 65 20  ial type of the 
1b2e4 72 6f 77 69 64 20 2a 2f 0a 20 20 75 33 32 20 6c  rowid */.  u32 l
1b2e5 65 6e 52 6f 77 69 64 3b 20 20 20 20 20 2f 2a 20  enRowid;     /* 
1b2e6 53 69 7a 65 20 6f 66 20 74 68 65 20 72 6f 77 69  Size of the rowi
1b2e7 64 20 2a 2f 0a 20 20 4d 65 6d 20 6d 2c 20 76 3b  d */.  Mem m, v;
1b2e8 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
1b2e9 45 54 45 52 28 64 62 29 3b 0a 0a 20 20 2f 2a 20  ETER(db);..  /* 
1b2ea 47 65 74 20 74 68 65 20 73 69 7a 65 20 6f 66 20  Get the size of 
1b2eb 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79 2e  the index entry.
1b2ec 20 20 4f 6e 6c 79 20 69 6e 64 69 63 65 73 20 65    Only indices e
1b2ed 6e 74 72 69 65 73 20 6f 66 20 6c 65 73 73 0a 20  ntries of less. 
1b2ee 20 2a 2a 20 74 68 61 6e 20 32 47 69 42 20 61 72   ** than 2GiB ar
1b2ef 65 20 73 75 70 70 6f 72 74 20 2d 20 61 6e 79 74  e support - anyt
1b2f0 68 69 6e 67 20 6c 61 72 67 65 20 6d 75 73 74 20  hing large must 
1b2f1 62 65 20 64 61 74 61 62 61 73 65 20 63 6f 72 72  be database corr
1b2f2 75 70 74 69 6f 6e 2e 0a 20 20 2a 2a 20 41 6e 79  uption..  ** Any
1b2f3 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20 64   corruption is d
1b2f4 65 74 65 63 74 65 64 20 69 6e 20 73 71 6c 69 74  etected in sqlit
1b2f5 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c  e3BtreeParseCell
1b2f6 50 74 72 28 29 2c 20 74 68 6f 75 67 68 2c 20 73  Ptr(), though, s
1b2f7 6f 0a 20 20 2a 2a 20 74 68 69 73 20 63 6f 64 65  o.  ** this code
1b2f8 20 63 61 6e 20 73 61 66 65 6c 79 20 61 73 73 75   can safely assu
1b2f9 6d 65 20 74 68 61 74 20 6e 43 65 6c 6c 4b 65 79  me that nCellKey
1b2fa 20 69 73 20 33 32 2d 62 69 74 73 20 20 0a 20 20   is 32-bits  .  
1b2fb 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  */.  assert( sql
1b2fc 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49  ite3BtreeCursorI
1b2fd 73 56 61 6c 69 64 28 70 43 75 72 29 20 29 3b 0a  sValid(pCur) );.
1b2fe 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1b2ff 72 65 65 4b 65 79 53 69 7a 65 28 70 43 75 72 2c  reeKeySize(pCur,
1b300 20 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 61   &nCellKey);.  a
1b301 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
1b302 45 5f 4f 4b 20 29 3b 20 20 20 20 20 2f 2a 20 70  E_OK );     /* p
1b303 43 75 72 20 69 73 20 61 6c 77 61 79 73 20 76 61  Cur is always va
1b304 6c 69 64 20 73 6f 20 4b 65 79 53 69 7a 65 20 63  lid so KeySize c
1b305 61 6e 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 20 20  annot fail */.  
1b306 61 73 73 65 72 74 28 20 28 6e 43 65 6c 6c 4b 65  assert( (nCellKe
1b307 79 20 26 20 53 51 4c 49 54 45 5f 4d 41 58 5f 55  y & SQLITE_MAX_U
1b308 33 32 29 3d 3d 28 75 36 34 29 6e 43 65 6c 6c 4b  32)==(u64)nCellK
1b309 65 79 20 29 3b 0a 0a 20 20 2f 2a 20 52 65 61 64  ey );..  /* Read
1b30a 20 69 6e 20 74 68 65 20 63 6f 6d 70 6c 65 74 65   in the complete
1b30b 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20   content of the 
1b30c 69 6e 64 65 78 20 65 6e 74 72 79 20 2a 2f 0a 20  index entry */. 
1b30d 20 6d 65 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73   memset(&m, 0, s
1b30e 69 7a 65 6f 66 28 6d 29 29 3b 0a 20 20 72 63 20  izeof(m));.  rc 
1b30f 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  = sqlite3VdbeMem
1b310 46 72 6f 6d 42 74 72 65 65 28 70 43 75 72 2c 20  FromBtree(pCur, 
1b311 30 2c 20 28 69 6e 74 29 6e 43 65 6c 6c 4b 65 79  0, (int)nCellKey
1b312 2c 20 31 2c 20 26 6d 29 3b 0a 20 20 69 66 28 20  , 1, &m);.  if( 
1b313 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  rc ){.    return
1b314 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54   rc;.  }..  /* T
1b315 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79 20 6d  he index entry m
1b316 75 73 74 20 62 65 67 69 6e 20 77 69 74 68 20 61  ust begin with a
1b317 20 68 65 61 64 65 72 20 73 69 7a 65 20 2a 2f 0a   header size */.
1b318 20 20 28 76 6f 69 64 29 67 65 74 56 61 72 69 6e    (void)getVarin
1b319 74 33 32 28 28 75 38 2a 29 6d 2e 7a 2c 20 73 7a  t32((u8*)m.z, sz
1b31a 48 64 72 29 3b 0a 20 20 74 65 73 74 63 61 73 65  Hdr);.  testcase
1b31b 28 20 73 7a 48 64 72 3d 3d 33 20 29 3b 0a 20 20  ( szHdr==3 );.  
1b31c 74 65 73 74 63 61 73 65 28 20 73 7a 48 64 72 3d  testcase( szHdr=
1b31d 3d 6d 2e 6e 20 29 3b 0a 20 20 69 66 28 20 75 6e  =m.n );.  if( un
1b31e 6c 69 6b 65 6c 79 28 73 7a 48 64 72 3c 33 20 7c  likely(szHdr<3 |
1b31f 7c 20 28 69 6e 74 29 73 7a 48 64 72 3e 6d 2e 6e  | (int)szHdr>m.n
1b320 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69 64  ) ){.    goto id
1b321 78 5f 72 6f 77 69 64 5f 63 6f 72 72 75 70 74 69  x_rowid_corrupti
1b322 6f 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68  on;.  }..  /* Th
1b323 65 20 6c 61 73 74 20 66 69 65 6c 64 20 6f 66 20  e last field of 
1b324 74 68 65 20 69 6e 64 65 78 20 73 68 6f 75 6c 64  the index should
1b325 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72 20 2d   be an integer -
1b326 20 74 68 65 20 52 4f 57 49 44 2e 0a 20 20 2a 2a   the ROWID..  **
1b327 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65   Verify that the
1b328 20 6c 61 73 74 20 65 6e 74 72 79 20 72 65 61 6c   last entry real
1b329 6c 79 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72  ly is an integer
1b32a 2e 20 2a 2f 0a 20 20 28 76 6f 69 64 29 67 65 74  . */.  (void)get
1b32b 56 61 72 69 6e 74 33 32 28 28 75 38 2a 29 26 6d  Varint32((u8*)&m
1b32c 2e 7a 5b 73 7a 48 64 72 2d 31 5d 2c 20 74 79 70  .z[szHdr-1], typ
1b32d 65 52 6f 77 69 64 29 3b 0a 20 20 74 65 73 74 63  eRowid);.  testc
1b32e 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d  ase( typeRowid==
1b32f 31 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28  1 );.  testcase(
1b330 20 74 79 70 65 52 6f 77 69 64 3d 3d 32 20 29 3b   typeRowid==2 );
1b331 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70  .  testcase( typ
1b332 65 52 6f 77 69 64 3d 3d 33 20 29 3b 0a 20 20 74  eRowid==3 );.  t
1b333 65 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77  estcase( typeRow
1b334 69 64 3d 3d 34 20 29 3b 0a 20 20 74 65 73 74 63  id==4 );.  testc
1b335 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d  ase( typeRowid==
1b336 35 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28  5 );.  testcase(
1b337 20 74 79 70 65 52 6f 77 69 64 3d 3d 36 20 29 3b   typeRowid==6 );
1b338 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70  .  testcase( typ
1b339 65 52 6f 77 69 64 3d 3d 38 20 29 3b 0a 20 20 74  eRowid==8 );.  t
1b33a 65 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77  estcase( typeRow
1b33b 69 64 3d 3d 39 20 29 3b 0a 20 20 69 66 28 20 75  id==9 );.  if( u
1b33c 6e 6c 69 6b 65 6c 79 28 74 79 70 65 52 6f 77 69  nlikely(typeRowi
1b33d 64 3c 31 20 7c 7c 20 74 79 70 65 52 6f 77 69 64  d<1 || typeRowid
1b33e 3e 39 20 7c 7c 20 74 79 70 65 52 6f 77 69 64 3d  >9 || typeRowid=
1b33f 3d 37 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  =7) ){.    goto 
1b340 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72 75 70  idx_rowid_corrup
1b341 74 69 6f 6e 3b 0a 20 20 7d 0a 20 20 6c 65 6e 52  tion;.  }.  lenR
1b342 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33 56 64  owid = sqlite3Vd
1b343 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28  beSerialTypeLen(
1b344 74 79 70 65 52 6f 77 69 64 29 3b 0a 20 20 74 65  typeRowid);.  te
1b345 73 74 63 61 73 65 28 20 28 75 33 32 29 6d 2e 6e  stcase( (u32)m.n
1b346 3d 3d 73 7a 48 64 72 2b 6c 65 6e 52 6f 77 69 64  ==szHdr+lenRowid
1b347 20 29 3b 0a 20 20 69 66 28 20 75 6e 6c 69 6b 65   );.  if( unlike
1b348 6c 79 28 28 75 33 32 29 6d 2e 6e 3c 73 7a 48 64  ly((u32)m.n<szHd
1b349 72 2b 6c 65 6e 52 6f 77 69 64 29 20 29 7b 0a 20  r+lenRowid) ){. 
1b34a 20 20 20 67 6f 74 6f 20 69 64 78 5f 72 6f 77 69     goto idx_rowi
1b34b 64 5f 63 6f 72 72 75 70 74 69 6f 6e 3b 0a 20 20  d_corruption;.  
1b34c 7d 0a 0a 20 20 2f 2a 20 46 65 74 63 68 20 74 68  }..  /* Fetch th
1b34d 65 20 69 6e 74 65 67 65 72 20 6f 66 66 20 74 68  e integer off th
1b34e 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 64  e end of the ind
1b34f 65 78 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 73  ex record */.  s
1b350 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
1b351 47 65 74 28 28 75 38 2a 29 26 6d 2e 7a 5b 6d 2e  Get((u8*)&m.z[m.
1b352 6e 2d 6c 65 6e 52 6f 77 69 64 5d 2c 20 74 79 70  n-lenRowid], typ
1b353 65 52 6f 77 69 64 2c 20 26 76 29 3b 0a 20 20 2a  eRowid, &v);.  *
1b354 72 6f 77 69 64 20 3d 20 76 2e 75 2e 69 3b 0a 20  rowid = v.u.i;. 
1b355 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
1b356 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20 20 72 65  elease(&m);.  re
1b357 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1b358 0a 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20  .  /* Jump here 
1b359 69 66 20 64 61 74 61 62 61 73 65 20 63 6f 72 72  if database corr
1b35a 75 70 74 69 6f 6e 20 69 73 20 64 65 74 65 63 74  uption is detect
1b35b 65 64 20 61 66 74 65 72 20 6d 20 68 61 73 20 62  ed after m has b
1b35c 65 65 6e 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74  een.  ** allocat
1b35d 65 64 2e 20 20 46 72 65 65 20 74 68 65 20 6d 20  ed.  Free the m 
1b35e 6f 62 6a 65 63 74 20 61 6e 64 20 72 65 74 75 72  object and retur
1b35f 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
1b360 2e 20 2a 2f 0a 69 64 78 5f 72 6f 77 69 64 5f 63  . */.idx_rowid_c
1b361 6f 72 72 75 70 74 69 6f 6e 3a 0a 20 20 74 65 73  orruption:.  tes
1b362 74 63 61 73 65 28 20 6d 2e 7a 4d 61 6c 6c 6f 63  tcase( m.zMalloc
1b363 21 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  !=0 );.  sqlite3
1b364 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26  VdbeMemRelease(&
1b365 6d 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  m);.  return SQL
1b366 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
1b367 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61  ;.}../*.** Compa
1b368 72 65 20 74 68 65 20 6b 65 79 20 6f 66 20 74 68  re the key of th
1b369 65 20 69 6e 64 65 78 20 65 6e 74 72 79 20 74 68  e index entry th
1b36a 61 74 20 63 75 72 73 6f 72 20 70 43 20 69 73 20  at cursor pC is 
1b36b 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 67 61 69  pointing to agai
1b36c 6e 73 74 0a 2a 2a 20 74 68 65 20 6b 65 79 20 73  nst.** the key s
1b36d 74 72 69 6e 67 20 69 6e 20 70 55 6e 70 61 63 6b  tring in pUnpack
1b36e 65 64 2e 20 20 57 72 69 74 65 20 69 6e 74 6f 20  ed.  Write into 
1b36f 2a 70 52 65 73 20 61 20 6e 75 6d 62 65 72 0a 2a  *pRes a number.*
1b370 2a 20 74 68 61 74 20 69 73 20 6e 65 67 61 74 69  * that is negati
1b371 76 65 2c 20 7a 65 72 6f 2c 20 6f 72 20 70 6f 73  ve, zero, or pos
1b372 69 74 69 76 65 20 69 66 20 70 43 20 69 73 20 6c  itive if pC is l
1b373 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61 6c 20  ess than, equal 
1b374 74 6f 2c 0a 2a 2a 20 6f 72 20 67 72 65 61 74 65  to,.** or greate
1b375 72 20 74 68 61 6e 20 70 55 6e 70 61 63 6b 65 64  r than pUnpacked
1b376 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  .  Return SQLITE
1b377 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a  _OK on success..
1b378 2a 2a 0a 2a 2a 20 70 55 6e 70 61 63 6b 65 64 20  **.** pUnpacked 
1b379 69 73 20 65 69 74 68 65 72 20 63 72 65 61 74 65  is either create
1b37a 64 20 77 69 74 68 6f 75 74 20 61 20 72 6f 77 69  d without a rowi
1b37b 64 20 6f 72 20 69 73 20 74 72 75 6e 63 61 74 65  d or is truncate
1b37c 64 20 73 6f 20 74 68 61 74 20 69 74 0a 2a 2a 20  d so that it.** 
1b37d 6f 6d 69 74 73 20 74 68 65 20 72 6f 77 69 64 20  omits the rowid 
1b37e 61 74 20 74 68 65 20 65 6e 64 2e 20 20 54 68 65  at the end.  The
1b37f 20 72 6f 77 69 64 20 61 74 20 74 68 65 20 65 6e   rowid at the en
1b380 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 65  d of the index e
1b381 6e 74 72 79 0a 2a 2a 20 69 73 20 69 67 6e 6f 72  ntry.** is ignor
1b382 65 64 20 61 73 20 77 65 6c 6c 2e 20 20 48 65 6e  ed as well.  Hen
1b383 63 65 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ce, this routine
1b384 20 6f 6e 6c 79 20 63 6f 6d 70 61 72 65 73 20 74   only compares t
1b385 68 65 20 70 72 65 66 69 78 65 73 20 0a 2a 2a 20  he prefixes .** 
1b386 6f 66 20 74 68 65 20 6b 65 79 73 20 70 72 69 6f  of the keys prio
1b387 72 20 74 6f 20 74 68 65 20 66 69 6e 61 6c 20 72  r to the final r
1b388 6f 77 69 64 2c 20 6e 6f 74 20 74 68 65 20 65 6e  owid, not the en
1b389 74 69 72 65 20 6b 65 79 2e 0a 2a 2f 0a 53 51 4c  tire key..*/.SQL
1b38a 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1b38b 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 4b 65  sqlite3VdbeIdxKe
1b38c 79 43 6f 6d 70 61 72 65 28 0a 20 20 56 64 62 65  yCompare(.  Vdbe
1b38d 43 75 72 73 6f 72 20 2a 70 43 2c 20 20 20 20 20  Cursor *pC,     
1b38e 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63          /* The c
1b38f 75 72 73 6f 72 20 74 6f 20 63 6f 6d 70 61 72 65  ursor to compare
1b390 20 61 67 61 69 6e 73 74 20 2a 2f 0a 20 20 55 6e   against */.  Un
1b391 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 55  packedRecord *pU
1b392 6e 70 61 63 6b 65 64 2c 20 20 2f 2a 20 55 6e 70  npacked,  /* Unp
1b393 61 63 6b 65 64 20 76 65 72 73 69 6f 6e 20 6f 66  acked version of
1b394 20 6b 65 79 20 74 6f 20 63 6f 6d 70 61 72 65 20   key to compare 
1b395 61 67 61 69 6e 73 74 20 2a 2f 0a 20 20 69 6e 74  against */.  int
1b396 20 2a 72 65 73 20 20 20 20 20 20 20 20 20 20 20   *res           
1b397 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
1b398 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e  e the comparison
1b399 20 72 65 73 75 6c 74 20 68 65 72 65 20 2a 2f 0a   result here */.
1b39a 29 7b 0a 20 20 69 36 34 20 6e 43 65 6c 6c 4b 65  ){.  i64 nCellKe
1b39b 79 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b  y = 0;.  int rc;
1b39c 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75  .  BtCursor *pCu
1b39d 72 20 3d 20 70 43 2d 3e 70 43 75 72 73 6f 72 3b  r = pC->pCursor;
1b39e 0a 20 20 4d 65 6d 20 6d 3b 0a 0a 20 20 61 73 73  .  Mem m;..  ass
1b39f 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
1b3a0 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 70  eCursorIsValid(p
1b3a1 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 73  Cur) );.  rc = s
1b3a2 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53 69  qlite3BtreeKeySi
1b3a3 7a 65 28 70 43 75 72 2c 20 26 6e 43 65 6c 6c 4b  ze(pCur, &nCellK
1b3a4 65 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72  ey);.  assert( r
1b3a5 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20  c==SQLITE_OK ); 
1b3a6 20 20 20 2f 2a 20 70 43 75 72 20 69 73 20 61 6c     /* pCur is al
1b3a7 77 61 79 73 20 76 61 6c 69 64 20 73 6f 20 4b 65  ways valid so Ke
1b3a8 79 53 69 7a 65 20 63 61 6e 6e 6f 74 20 66 61 69  ySize cannot fai
1b3a9 6c 20 2a 2f 0a 20 20 2f 2a 20 6e 43 65 6c 6c 4b  l */.  /* nCellK
1b3aa 65 79 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62  ey will always b
1b3ab 65 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20  e between 0 and 
1b3ac 30 78 66 66 66 66 66 66 66 66 20 62 65 63 61 75  0xffffffff becau
1b3ad 73 65 20 6f 66 20 74 68 65 20 73 61 79 0a 20 20  se of the say.  
1b3ae 2a 2a 20 74 68 61 74 20 62 74 72 65 65 50 61 72  ** that btreePar
1b3af 73 65 43 65 6c 6c 50 74 72 28 29 20 61 6e 64 20  seCellPtr() and 
1b3b0 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74  sqlite3GetVarint
1b3b1 33 32 28 29 20 61 72 65 20 69 6d 70 6c 65 6d 65  32() are impleme
1b3b2 6e 74 65 64 20 2a 2f 0a 20 20 69 66 28 20 6e 43  nted */.  if( nC
1b3b3 65 6c 6c 4b 65 79 3c 3d 30 20 7c 7c 20 6e 43 65  ellKey<=0 || nCe
1b3b4 6c 6c 4b 65 79 3e 30 78 37 66 66 66 66 66 66 66  llKey>0x7fffffff
1b3b5 20 29 7b 0a 20 20 20 20 2a 72 65 73 20 3d 20 30   ){.    *res = 0
1b3b6 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
1b3b7 49 54 45 5f 43 4f 52 52 55 50 54 3b 0a 20 20 7d  ITE_CORRUPT;.  }
1b3b8 0a 20 20 6d 65 6d 73 65 74 28 26 6d 2c 20 30 2c  .  memset(&m, 0,
1b3b9 20 73 69 7a 65 6f 66 28 6d 29 29 3b 0a 20 20 72   sizeof(m));.  r
1b3ba 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  c = sqlite3VdbeM
1b3bb 65 6d 46 72 6f 6d 42 74 72 65 65 28 70 43 2d 3e  emFromBtree(pC->
1b3bc 70 43 75 72 73 6f 72 2c 20 30 2c 20 28 69 6e 74  pCursor, 0, (int
1b3bd 29 6e 43 65 6c 6c 4b 65 79 2c 20 31 2c 20 26 6d  )nCellKey, 1, &m
1b3be 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20  );.  if( rc ){. 
1b3bf 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
1b3c0 7d 0a 20 20 61 73 73 65 72 74 28 20 70 55 6e 70  }.  assert( pUnp
1b3c1 61 63 6b 65 64 2d 3e 66 6c 61 67 73 20 26 20 55  acked->flags & U
1b3c2 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52  NPACKED_IGNORE_R
1b3c3 4f 57 49 44 20 29 3b 0a 20 20 2a 72 65 73 20 3d  OWID );.  *res =
1b3c4 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f   sqlite3VdbeReco
1b3c5 72 64 43 6f 6d 70 61 72 65 28 6d 2e 6e 2c 20 6d  rdCompare(m.n, m
1b3c6 2e 7a 2c 20 70 55 6e 70 61 63 6b 65 64 29 3b 0a  .z, pUnpacked);.
1b3c7 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1b3c8 52 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20 20 72  Release(&m);.  r
1b3c9 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1b3ca 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
1b3cb 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 20  outine sets the 
1b3cc 76 61 6c 75 65 20 74 6f 20 62 65 20 72 65 74 75  value to be retu
1b3cd 72 6e 65 64 20 62 79 20 73 75 62 73 65 71 75 65  rned by subseque
1b3ce 6e 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73  nt calls to.** s
1b3cf 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29  qlite3_changes()
1b3d0 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
1b3d1 20 68 61 6e 64 6c 65 20 27 64 62 27 2e 20 0a 2a   handle 'db'. .*
1b3d2 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1b3d3 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
1b3d4 65 53 65 74 43 68 61 6e 67 65 73 28 73 71 6c 69  eSetChanges(sqli
1b3d5 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6e 43 68  te3 *db, int nCh
1b3d6 61 6e 67 65 29 7b 0a 20 20 61 73 73 65 72 74 28  ange){.  assert(
1b3d7 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
1b3d8 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29  eld(db->mutex) )
1b3d9 3b 0a 20 20 64 62 2d 3e 6e 43 68 61 6e 67 65 20  ;.  db->nChange 
1b3da 3d 20 6e 43 68 61 6e 67 65 3b 0a 20 20 64 62 2d  = nChange;.  db-
1b3db 3e 6e 54 6f 74 61 6c 43 68 61 6e 67 65 20 2b 3d  >nTotalChange +=
1b3dc 20 6e 43 68 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a 0a   nChange;.}../*.
1b3dd 2a 2a 20 53 65 74 20 61 20 66 6c 61 67 20 69 6e  ** Set a flag in
1b3de 20 74 68 65 20 76 64 62 65 20 74 6f 20 75 70 64   the vdbe to upd
1b3df 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 20 63  ate the change c
1b3e0 6f 75 6e 74 65 72 20 77 68 65 6e 20 69 74 20 69  ounter when it i
1b3e1 73 20 66 69 6e 61 6c 69 73 65 64 0a 2a 2a 20 6f  s finalised.** o
1b3e2 72 20 72 65 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49  r reset..*/.SQLI
1b3e3 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1b3e4 73 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e 74  sqlite3VdbeCount
1b3e5 43 68 61 6e 67 65 73 28 56 64 62 65 20 2a 76 29  Changes(Vdbe *v)
1b3e6 7b 0a 20 20 76 2d 3e 63 68 61 6e 67 65 43 6e 74  {.  v->changeCnt
1b3e7 4f 6e 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  On = 1;.}../*.**
1b3e8 20 4d 61 72 6b 20 65 76 65 72 79 20 70 72 65 70   Mark every prep
1b3e9 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 61  ared statement a
1b3ea 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61  ssociated with a
1b3eb 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
1b3ec 74 69 6f 6e 0a 2a 2a 20 61 73 20 65 78 70 69 72  tion.** as expir
1b3ed 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 70  ed..**.** An exp
1b3ee 69 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 6d  ired statement m
1b3ef 65 61 6e 73 20 74 68 61 74 20 72 65 63 6f 6d 70  eans that recomp
1b3f0 69 6c 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73  ilation of the s
1b3f1 74 61 74 65 6d 65 6e 74 20 69 73 0a 2a 2a 20 72  tatement is.** r
1b3f2 65 63 6f 6d 6d 65 6e 64 2e 20 20 53 74 61 74 65  ecommend.  State
1b3f3 6d 65 6e 74 73 20 65 78 70 69 72 65 20 77 68 65  ments expire whe
1b3f4 6e 20 74 68 69 6e 67 73 20 68 61 70 70 65 6e 20  n things happen 
1b3f5 74 68 61 74 20 6d 61 6b 65 20 74 68 65 69 72 0a  that make their.
1b3f6 2a 2a 20 70 72 6f 67 72 61 6d 73 20 6f 62 73 6f  ** programs obso
1b3f7 6c 65 74 65 2e 20 20 52 65 6d 6f 76 69 6e 67 20  lete.  Removing 
1b3f8 75 73 65 72 2d 64 65 66 69 6e 65 64 20 66 75 6e  user-defined fun
1b3f9 63 74 69 6f 6e 73 20 6f 72 20 63 6f 6c 6c 61 74  ctions or collat
1b3fa 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 73  ing.** sequences
1b3fb 2c 20 6f 72 20 63 68 61 6e 67 69 6e 67 20 61 6e  , or changing an
1b3fc 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66   authorization f
1b3fd 75 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 65 20  unction are the 
1b3fe 74 79 70 65 73 20 6f 66 0a 2a 2a 20 74 68 69 6e  types of.** thin
1b3ff 67 73 20 74 68 61 74 20 6d 61 6b 65 20 70 72 65  gs that make pre
1b400 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73  pared statements
1b401 20 6f 62 73 6f 6c 65 74 65 2e 0a 2a 2f 0a 53 51   obsolete..*/.SQ
1b402 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1b403 64 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 50  d sqlite3ExpireP
1b404 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74  reparedStatement
1b405 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  s(sqlite3 *db){.
1b406 20 20 56 64 62 65 20 2a 70 3b 0a 20 20 66 6f 72    Vdbe *p;.  for
1b407 28 70 20 3d 20 64 62 2d 3e 70 56 64 62 65 3b 20  (p = db->pVdbe; 
1b408 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a  p; p=p->pNext){.
1b409 20 20 20 20 70 2d 3e 65 78 70 69 72 65 64 20 3d      p->expired =
1b40a 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a   1;.  }.}../*.**
1b40b 20 52 65 74 75 72 6e 20 74 68 65 20 64 61 74 61   Return the data
1b40c 62 61 73 65 20 61 73 73 6f 63 69 61 74 65 64 20  base associated 
1b40d 77 69 74 68 20 74 68 65 20 56 64 62 65 2e 0a 2a  with the Vdbe..*
1b40e 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1b40f 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65   sqlite3 *sqlite
1b410 33 56 64 62 65 44 62 28 56 64 62 65 20 2a 76 29  3VdbeDb(Vdbe *v)
1b411 7b 0a 20 20 72 65 74 75 72 6e 20 76 2d 3e 64 62  {.  return v->db
1b412 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;.}../**********
1b413 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65  **** End of vdbe
1b414 61 75 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  aux.c **********
1b415 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b416 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b417 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
1b418 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
1b419 76 64 62 65 61 70 69 2e 63 20 2a 2a 2a 2a 2a 2a  vdbeapi.c ******
1b41a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b41b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b41c 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20  ***/./*.** 2004 
1b41d 4d 61 79 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65  May 26.**.** The
1b41e 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
1b41f 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
1b420 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
1b421 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
1b422 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
1b423 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
1b424 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
1b425 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
1b426 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
1b427 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
1b428 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
1b429 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
1b42a 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
1b42b 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
1b42c 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
1b42d 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
1b42e 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
1b42f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b431 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b432 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b433 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
1b434 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
1b435 6f 64 65 20 75 73 65 20 74 6f 20 69 6d 70 6c 65  ode use to imple
1b436 6d 65 6e 74 20 41 50 49 73 20 74 68 61 74 20 61  ment APIs that a
1b437 72 65 20 70 61 72 74 20 6f 66 20 74 68 65 0a 2a  re part of the.*
1b438 2a 20 56 44 42 45 2e 0a 2a 2a 0a 2a 2a 20 24 49  * VDBE..**.** $I
1b439 64 3a 20 76 64 62 65 61 70 69 2e 63 2c 76 20 31  d: vdbeapi.c,v 1
1b43a 2e 31 36 37 20 32 30 30 39 2f 30 36 2f 32 35 20  .167 2009/06/25 
1b43b 30 31 3a 34 37 3a 31 32 20 64 72 68 20 45 78 70  01:47:12 drh Exp
1b43c 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53   $.*/..#ifndef S
1b43d 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45  QLITE_OMIT_DEPRE
1b43e 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 52 65 74 75  CATED./*.** Retu
1b43f 72 6e 20 54 52 55 45 20 28 6e 6f 6e 2d 7a 65 72  rn TRUE (non-zer
1b440 6f 29 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d  o) of the statem
1b441 65 6e 74 20 73 75 70 70 6c 69 65 64 20 61 73 20  ent supplied as 
1b442 61 6e 20 61 72 67 75 6d 65 6e 74 20 6e 65 65 64  an argument need
1b443 73 0a 2a 2a 20 74 6f 20 62 65 20 72 65 63 6f 6d  s.** to be recom
1b444 70 69 6c 65 64 2e 20 20 41 20 73 74 61 74 65 6d  piled.  A statem
1b445 65 6e 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20  ent needs to be 
1b446 72 65 63 6f 6d 70 69 6c 65 64 20 77 68 65 6e 65  recompiled whene
1b447 76 65 72 20 74 68 65 0a 2a 2a 20 65 78 65 63 75  ver the.** execu
1b448 74 69 6f 6e 20 65 6e 76 69 72 6f 6e 6d 65 6e 74  tion environment
1b449 20 63 68 61 6e 67 65 73 20 69 6e 20 61 20 77 61   changes in a wa
1b44a 79 20 74 68 61 74 20 77 6f 75 6c 64 20 61 6c 74  y that would alt
1b44b 65 72 20 74 68 65 20 70 72 6f 67 72 61 6d 0a 2a  er the program.*
1b44c 2a 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f 70  * that sqlite3_p
1b44d 72 65 70 61 72 65 28 29 20 67 65 6e 65 72 61 74  repare() generat
1b44e 65 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65  es.  For example
1b44f 2c 20 69 66 20 6e 65 77 20 66 75 6e 63 74 69 6f  , if new functio
1b450 6e 73 20 6f 72 0a 2a 2a 20 63 6f 6c 6c 61 74 69  ns or.** collati
1b451 6e 67 20 73 65 71 75 65 6e 63 65 73 20 61 72 65  ng sequences are
1b452 20 72 65 67 69 73 74 65 72 65 64 20 6f 72 20 69   registered or i
1b453 66 20 61 6e 20 61 75 74 68 6f 72 69 7a 65 72 20  f an authorizer 
1b454 66 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 61  function is.** a
1b455 64 64 65 64 20 6f 72 20 63 68 61 6e 67 65 64 2e  dded or changed.
1b456 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
1b457 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72  nt sqlite3_expir
1b458 65 64 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  ed(sqlite3_stmt 
1b459 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20  *pStmt){.  Vdbe 
1b45a 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d  *p = (Vdbe*)pStm
1b45b 74 3b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30  t;.  return p==0
1b45c 20 7c 7c 20 70 2d 3e 65 78 70 69 72 65 64 3b 0a   || p->expired;.
1b45d 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
1b45e 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f  The following ro
1b45f 75 74 69 6e 65 20 64 65 73 74 72 6f 79 73 20 61  utine destroys a
1b460 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
1b461 20 74 68 61 74 20 69 73 20 63 72 65 61 74 65 64   that is created
1b462 20 62 79 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74   by.** the sqlit
1b463 65 33 5f 63 6f 6d 70 69 6c 65 28 29 20 72 6f 75  e3_compile() rou
1b464 74 69 6e 65 2e 20 54 68 65 20 69 6e 74 65 67 65  tine. The intege
1b465 72 20 72 65 74 75 72 6e 65 64 20 69 73 20 61 6e  r returned is an
1b466 20 53 51 4c 49 54 45 5f 0a 2a 2a 20 73 75 63 63   SQLITE_.** succ
1b467 65 73 73 2f 66 61 69 6c 75 72 65 20 63 6f 64 65  ess/failure code
1b468 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20   that describes 
1b469 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 65 78  the result of ex
1b46a 65 63 75 74 69 6e 67 20 74 68 65 20 76 69 72 74  ecuting the virt
1b46b 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 2e 0a  ual.** machine..
1b46c 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
1b46d 6e 65 20 73 65 74 73 20 74 68 65 20 65 72 72 6f  ne sets the erro
1b46e 72 20 63 6f 64 65 20 61 6e 64 20 73 74 72 69 6e  r code and strin
1b46f 67 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a  g returned by.**
1b470 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65   sqlite3_errcode
1b471 28 29 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d  (), sqlite3_errm
1b472 73 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  sg() and sqlite3
1b473 5f 65 72 72 6d 73 67 31 36 28 29 2e 0a 2a 2f 0a  _errmsg16()..*/.
1b474 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1b475 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
1b476 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
1b477 74 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  tmt){.  int rc;.
1b478 20 20 69 66 28 20 70 53 74 6d 74 3d 3d 30 20 29    if( pStmt==0 )
1b479 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
1b47a 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  E_OK;.  }else{. 
1b47b 20 20 20 56 64 62 65 20 2a 76 20 3d 20 28 56 64     Vdbe *v = (Vd
1b47c 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20 73  be*)pStmt;.    s
1b47d 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 76 2d 3e  qlite3 *db = v->
1b47e 64 62 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54  db;.#if SQLITE_T
1b47f 48 52 45 41 44 53 41 46 45 0a 20 20 20 20 73 71  HREADSAFE.    sq
1b480 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74  lite3_mutex *mut
1b481 65 78 20 3d 20 76 2d 3e 64 62 2d 3e 6d 75 74 65  ex = v->db->mute
1b482 78 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 73 71  x;.#endif.    sq
1b483 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
1b484 72 28 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 63  r(mutex);.    rc
1b485 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 46 69   = sqlite3VdbeFi
1b486 6e 61 6c 69 7a 65 28 76 29 3b 0a 20 20 20 20 72  nalize(v);.    r
1b487 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78  c = sqlite3ApiEx
1b488 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 20 20  it(db, rc);.    
1b489 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
1b48a 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 7d 0a  ave(mutex);.  }.
1b48b 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1b48c 2f 2a 0a 2a 2a 20 54 65 72 6d 69 6e 61 74 65 20  /*.** Terminate 
1b48d 74 68 65 20 63 75 72 72 65 6e 74 20 65 78 65 63  the current exec
1b48e 75 74 69 6f 6e 20 6f 66 20 61 6e 20 53 51 4c 20  ution of an SQL 
1b48f 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 72 65  statement and re
1b490 73 65 74 20 69 74 0a 2a 2a 20 62 61 63 6b 20 74  set it.** back t
1b491 6f 20 69 74 73 20 73 74 61 72 74 69 6e 67 20 73  o its starting s
1b492 74 61 74 65 20 73 6f 20 74 68 61 74 20 69 74 20  tate so that it 
1b493 63 61 6e 20 62 65 20 72 65 75 73 65 64 2e 20 41  can be reused. A
1b494 20 73 75 63 63 65 73 73 20 63 6f 64 65 20 66 72   success code fr
1b495 6f 6d 0a 2a 2a 20 74 68 65 20 70 72 69 6f 72 20  om.** the prior 
1b496 65 78 65 63 75 74 69 6f 6e 20 69 73 20 72 65 74  execution is ret
1b497 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  urned..**.** Thi
1b498 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74  s routine sets t
1b499 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e  he error code an
1b49a 64 20 73 74 72 69 6e 67 20 72 65 74 75 72 6e 65  d string returne
1b49b 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  d by.** sqlite3_
1b49c 65 72 72 63 6f 64 65 28 29 2c 20 73 71 6c 69 74  errcode(), sqlit
1b49d 65 33 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 20  e3_errmsg() and 
1b49e 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36  sqlite3_errmsg16
1b49f 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ()..*/.SQLITE_AP
1b4a0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 65  I int sqlite3_re
1b4a1 73 65 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  set(sqlite3_stmt
1b4a2 20 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74 20   *pStmt){.  int 
1b4a3 72 63 3b 0a 20 20 69 66 28 20 70 53 74 6d 74 3d  rc;.  if( pStmt=
1b4a4 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  =0 ){.    rc = S
1b4a5 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73  QLITE_OK;.  }els
1b4a6 65 7b 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d  e{.    Vdbe *v =
1b4a7 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20   (Vdbe*)pStmt;. 
1b4a8 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
1b4a9 5f 65 6e 74 65 72 28 76 2d 3e 64 62 2d 3e 6d 75  _enter(v->db->mu
1b4aa 74 65 78 29 3b 0a 20 20 20 20 72 63 20 3d 20 73  tex);.    rc = s
1b4ab 71 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 28  qlite3VdbeReset(
1b4ac 76 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  v);.    sqlite3V
1b4ad 64 62 65 4d 61 6b 65 52 65 61 64 79 28 76 2c 20  dbeMakeReady(v, 
1b4ae 2d 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20  -1, 0, 0, 0, 0, 
1b4af 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  0);.    assert( 
1b4b0 28 72 63 20 26 20 28 76 2d 3e 64 62 2d 3e 65 72  (rc & (v->db->er
1b4b1 72 4d 61 73 6b 29 29 3d 3d 72 63 20 29 3b 0a 20  rMask))==rc );. 
1b4b2 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41     rc = sqlite3A
1b4b3 70 69 45 78 69 74 28 76 2d 3e 64 62 2c 20 72 63  piExit(v->db, rc
1b4b4 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
1b4b5 75 74 65 78 5f 6c 65 61 76 65 28 76 2d 3e 64 62  utex_leave(v->db
1b4b6 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20  ->mutex);.  }.  
1b4b7 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1b4b8 0a 2a 2a 20 53 65 74 20 61 6c 6c 20 74 68 65 20  .** Set all the 
1b4b9 70 61 72 61 6d 65 74 65 72 73 20 69 6e 20 74 68  parameters in th
1b4ba 65 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73  e compiled SQL s
1b4bb 74 61 74 65 6d 65 6e 74 20 74 6f 20 4e 55 4c 4c  tatement to NULL
1b4bc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1b4bd 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6c 65 61  int sqlite3_clea
1b4be 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 74  r_bindings(sqlit
1b4bf 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b  e3_stmt *pStmt){
1b4c0 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
1b4c1 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1b4c2 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62    Vdbe *p = (Vdb
1b4c3 65 2a 29 70 53 74 6d 74 3b 0a 23 69 66 20 53 51  e*)pStmt;.#if SQ
1b4c4 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a  LITE_THREADSAFE.
1b4c5 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20    sqlite3_mutex 
1b4c6 2a 6d 75 74 65 78 20 3d 20 28 28 56 64 62 65 2a  *mutex = ((Vdbe*
1b4c7 29 70 53 74 6d 74 29 2d 3e 64 62 2d 3e 6d 75 74  )pStmt)->db->mut
1b4c8 65 78 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c  ex;.#endif.  sql
1b4c9 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
1b4ca 28 6d 75 74 65 78 29 3b 0a 20 20 66 6f 72 28 69  (mutex);.  for(i
1b4cb 3d 30 3b 20 69 3c 70 2d 3e 6e 56 61 72 3b 20 69  =0; i<p->nVar; i
1b4cc 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ++){.    sqlite3
1b4cd 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26  VdbeMemRelease(&
1b4ce 70 2d 3e 61 56 61 72 5b 69 5d 29 3b 0a 20 20 20  p->aVar[i]);.   
1b4cf 20 70 2d 3e 61 56 61 72 5b 69 5d 2e 66 6c 61 67   p->aVar[i].flag
1b4d0 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20  s = MEM_Null;.  
1b4d1 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
1b4d2 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a  x_leave(mutex);.
1b4d3 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1b4d4 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1b4d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73  ************** s
1b4d6 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 20 20 2a  qlite3_value_  *
1b4d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b4d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
1b4d9 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
1b4da 72 6f 75 74 69 6e 65 73 20 65 78 74 72 61 63 74  routines extract
1b4db 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f   information fro
1b4dc 6d 20 61 20 4d 65 6d 20 6f 72 20 73 71 6c 69 74  m a Mem or sqlit
1b4dd 65 33 5f 76 61 6c 75 65 0a 2a 2a 20 73 74 72 75  e3_value.** stru
1b4de 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  cture..*/.SQLITE
1b4df 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20  _API const void 
1b4e0 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62  *sqlite3_value_b
1b4e1 6c 6f 62 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  lob(sqlite3_valu
1b4e2 65 20 2a 70 56 61 6c 29 7b 0a 20 20 4d 65 6d 20  e *pVal){.  Mem 
1b4e3 2a 70 20 3d 20 28 4d 65 6d 2a 29 70 56 61 6c 3b  *p = (Mem*)pVal;
1b4e4 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20  .  if( p->flags 
1b4e5 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f  & (MEM_Blob|MEM_
1b4e6 53 74 72 29 20 29 7b 0a 20 20 20 20 73 71 6c 69  Str) ){.    sqli
1b4e7 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64  te3VdbeMemExpand
1b4e8 42 6c 6f 62 28 70 29 3b 0a 20 20 20 20 70 2d 3e  Blob(p);.    p->
1b4e9 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f 53 74  flags &= ~MEM_St
1b4ea 72 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20  r;.    p->flags 
1b4eb 7c 3d 20 4d 45 4d 5f 42 6c 6f 62 3b 0a 20 20 20  |= MEM_Blob;.   
1b4ec 20 72 65 74 75 72 6e 20 70 2d 3e 7a 3b 0a 20 20   return p->z;.  
1b4ed 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72  }else{.    retur
1b4ee 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  n sqlite3_value_
1b4ef 74 65 78 74 28 70 56 61 6c 29 3b 0a 20 20 7d 0a  text(pVal);.  }.
1b4f0 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  }.SQLITE_API int
1b4f1 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
1b4f2 79 74 65 73 28 73 71 6c 69 74 65 33 5f 76 61 6c  ytes(sqlite3_val
1b4f3 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74  ue *pVal){.  ret
1b4f4 75 72 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65  urn sqlite3Value
1b4f5 42 79 74 65 73 28 70 56 61 6c 2c 20 53 51 4c 49  Bytes(pVal, SQLI
1b4f6 54 45 5f 55 54 46 38 29 3b 0a 7d 0a 53 51 4c 49  TE_UTF8);.}.SQLI
1b4f7 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1b4f8 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 31 36  e3_value_bytes16
1b4f9 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a  (sqlite3_value *
1b4fa 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20  pVal){.  return 
1b4fb 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79 74 65  sqlite3ValueByte
1b4fc 73 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55  s(pVal, SQLITE_U
1b4fd 54 46 31 36 4e 41 54 49 56 45 29 3b 0a 7d 0a 53  TF16NATIVE);.}.S
1b4fe 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 6c 65  QLITE_API double
1b4ff 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64   sqlite3_value_d
1b500 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 76 61  ouble(sqlite3_va
1b501 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65  lue *pVal){.  re
1b502 74 75 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65  turn sqlite3Vdbe
1b503 52 65 61 6c 56 61 6c 75 65 28 28 4d 65 6d 2a 29  RealValue((Mem*)
1b504 70 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  pVal);.}.SQLITE_
1b505 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1b506 76 61 6c 75 65 5f 69 6e 74 28 73 71 6c 69 74 65  value_int(sqlite
1b507 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a  3_value *pVal){.
1b508 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29 73 71    return (int)sq
1b509 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75  lite3VdbeIntValu
1b50a 65 28 28 4d 65 6d 2a 29 70 56 61 6c 29 3b 0a 7d  e((Mem*)pVal);.}
1b50b 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
1b50c 74 65 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33  te_int64 sqlite3
1b50d 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 73 71 6c  _value_int64(sql
1b50e 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c  ite3_value *pVal
1b50f 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  ){.  return sqli
1b510 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28  te3VdbeIntValue(
1b511 28 4d 65 6d 2a 29 70 56 61 6c 29 3b 0a 7d 0a 53  (Mem*)pVal);.}.S
1b512 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
1b513 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73  unsigned char *s
1b514 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
1b515 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  t(sqlite3_value 
1b516 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e  *pVal){.  return
1b517 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64   (const unsigned
1b518 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 56   char *)sqlite3V
1b519 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53  alueText(pVal, S
1b51a 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 7d 0a 23  QLITE_UTF8);.}.#
1b51b 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1b51c 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f  IT_UTF16.SQLITE_
1b51d 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
1b51e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
1b51f 78 74 31 36 28 73 71 6c 69 74 65 33 5f 76 61 6c  xt16(sqlite3_val
1b520 75 65 2a 20 70 56 61 6c 29 7b 0a 20 20 72 65 74  ue* pVal){.  ret
1b521 75 72 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65  urn sqlite3Value
1b522 54 65 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54  Text(pVal, SQLIT
1b523 45 5f 55 54 46 31 36 4e 41 54 49 56 45 29 3b 0a  E_UTF16NATIVE);.
1b524 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  }.SQLITE_API con
1b525 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
1b526 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65 28  _value_text16be(
1b527 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
1b528 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73  Val){.  return s
1b529 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28  qlite3ValueText(
1b52a 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46  pVal, SQLITE_UTF
1b52b 31 36 42 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  16BE);.}.SQLITE_
1b52c 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
1b52d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
1b52e 78 74 31 36 6c 65 28 73 71 6c 69 74 65 33 5f 76  xt16le(sqlite3_v
1b52f 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72  alue *pVal){.  r
1b530 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 61 6c  eturn sqlite3Val
1b531 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53 51 4c  ueText(pVal, SQL
1b532 49 54 45 5f 55 54 46 31 36 4c 45 29 3b 0a 7d 0a  ITE_UTF16LE);.}.
1b533 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1b534 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53  _OMIT_UTF16 */.S
1b535 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1b536 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
1b537 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20  (sqlite3_value* 
1b538 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20  pVal){.  return 
1b539 70 56 61 6c 2d 3e 74 79 70 65 3b 0a 7d 0a 0a 2f  pVal->type;.}../
1b53a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b53b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c  ************ sql
1b53c 69 74 65 33 5f 72 65 73 75 6c 74 5f 20 20 2a 2a  ite3_result_  **
1b53d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b53e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
1b53f 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72   The following r
1b540 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64  outines are used
1b541 20 62 79 20 75 73 65 72 2d 64 65 66 69 6e 65 64   by user-defined
1b542 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 73 70   functions to sp
1b543 65 63 69 66 79 0a 2a 2a 20 74 68 65 20 66 75 6e  ecify.** the fun
1b544 63 74 69 6f 6e 20 72 65 73 75 6c 74 2e 0a 2a 2a  ction result..**
1b545 0a 2a 2a 20 54 68 65 20 73 65 74 53 74 72 4f 72  .** The setStrOr
1b546 45 72 72 6f 72 28 29 20 66 75 6e 74 69 6f 6e 20  Error() funtion 
1b547 63 61 6c 6c 73 20 73 71 6c 69 74 65 33 56 64 62  calls sqlite3Vdb
1b548 65 4d 65 6d 53 65 74 53 74 72 28 29 20 74 6f 20  eMemSetStr() to 
1b549 73 74 6f 72 65 20 74 68 65 0a 2a 2a 20 72 65 73  store the.** res
1b54a 75 6c 74 20 61 73 20 61 20 73 74 72 69 6e 67 20  ult as a string 
1b54b 6f 72 20 62 6c 6f 62 20 62 75 74 20 69 66 20 74  or blob but if t
1b54c 68 65 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f  he string or blo
1b54d 62 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 2c 20  b is too large, 
1b54e 69 74 0a 2a 2a 20 74 68 65 6e 20 73 65 74 73 20  it.** then sets 
1b54f 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 74  the error code t
1b550 6f 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 0a  o SQLITE_TOOBIG.
1b551 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
1b552 65 74 52 65 73 75 6c 74 53 74 72 4f 72 45 72 72  etResultStrOrErr
1b553 6f 72 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  or(.  sqlite3_co
1b554 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 20 2f 2a  ntext *pCtx,  /*
1b555 20 46 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 78   Function contex
1b556 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  t */.  const cha
1b557 72 20 2a 7a 2c 20 20 20 20 20 20 20 20 20 20 2f  r *z,          /
1b558 2a 20 53 74 72 69 6e 67 20 70 6f 69 6e 74 65 72  * String pointer
1b559 20 2a 2f 0a 20 20 69 6e 74 20 6e 2c 20 20 20 20   */.  int n,    
1b55a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1b55b 20 42 79 74 65 73 20 69 6e 20 73 74 72 69 6e 67   Bytes in string
1b55c 2c 20 6f 72 20 6e 65 67 61 74 69 76 65 20 2a 2f  , or negative */
1b55d 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20 20  .  u8 enc,      
1b55e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e             /* En
1b55f 63 6f 64 69 6e 67 20 6f 66 20 7a 2e 20 20 30 20  coding of z.  0 
1b560 66 6f 72 20 42 4c 4f 42 73 20 2a 2f 0a 20 20 76  for BLOBs */.  v
1b561 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64  oid (*xDel)(void
1b562 2a 29 20 20 20 20 20 2f 2a 20 44 65 73 74 72 75  *)     /* Destru
1b563 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  ctor function */
1b564 0a 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  .){.  if( sqlite
1b565 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26  3VdbeMemSetStr(&
1b566 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 65  pCtx->s, z, n, e
1b567 6e 63 2c 20 78 44 65 6c 29 3d 3d 53 51 4c 49 54  nc, xDel)==SQLIT
1b568 45 5f 54 4f 4f 42 49 47 20 29 7b 0a 20 20 20 20  E_TOOBIG ){.    
1b569 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
1b56a 72 72 6f 72 5f 74 6f 6f 62 69 67 28 70 43 74 78  rror_toobig(pCtx
1b56b 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f  );.  }.}.SQLITE_
1b56c 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
1b56d 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 0a 20 20  _result_blob(.  
1b56e 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1b56f 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20  *pCtx, .  const 
1b570 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20  void *z, .  int 
1b571 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65  n, .  void (*xDe
1b572 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20  l)(void *).){.  
1b573 61 73 73 65 72 74 28 20 6e 3e 3d 30 20 29 3b 0a  assert( n>=0 );.
1b574 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1b575 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74  3_mutex_held(pCt
1b576 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20  x->s.db->mutex) 
1b577 29 3b 0a 20 20 73 65 74 52 65 73 75 6c 74 53 74  );.  setResultSt
1b578 72 4f 72 45 72 72 6f 72 28 70 43 74 78 2c 20 7a  rOrError(pCtx, z
1b579 2c 20 6e 2c 20 30 2c 20 78 44 65 6c 29 3b 0a 7d  , n, 0, xDel);.}
1b57a 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
1b57b 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
1b57c 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 63  double(sqlite3_c
1b57d 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 64 6f  ontext *pCtx, do
1b57e 75 62 6c 65 20 72 56 61 6c 29 7b 0a 20 20 61 73  uble rVal){.  as
1b57f 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1b580 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73  tex_held(pCtx->s
1b581 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  .db->mutex) );. 
1b582 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1b583 65 74 44 6f 75 62 6c 65 28 26 70 43 74 78 2d 3e  etDouble(&pCtx->
1b584 73 2c 20 72 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49  s, rVal);.}.SQLI
1b585 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
1b586 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
1b587 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
1b588 20 2a 70 43 74 78 2c 20 63 6f 6e 73 74 20 63 68   *pCtx, const ch
1b589 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20  ar *z, int n){. 
1b58a 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1b58b 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78  _mutex_held(pCtx
1b58c 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29  ->s.db->mutex) )
1b58d 3b 0a 20 20 70 43 74 78 2d 3e 69 73 45 72 72 6f  ;.  pCtx->isErro
1b58e 72 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  r = SQLITE_ERROR
1b58f 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ;.  sqlite3VdbeM
1b590 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e  emSetStr(&pCtx->
1b591 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f  s, z, n, SQLITE_
1b592 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 54 52 41  UTF8, SQLITE_TRA
1b593 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 69 66 6e 64  NSIENT);.}.#ifnd
1b594 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  ef SQLITE_OMIT_U
1b595 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20  TF16.SQLITE_API 
1b596 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
1b597 75 6c 74 5f 65 72 72 6f 72 31 36 28 73 71 6c 69  ult_error16(sqli
1b598 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74  te3_context *pCt
1b599 78 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  x, const void *z
1b59a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 61 73 73 65  , int n){.  asse
1b59b 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1b59c 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64  x_held(pCtx->s.d
1b59d 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70  b->mutex) );.  p
1b59e 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53  Ctx->isError = S
1b59f 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 73  QLITE_ERROR;.  s
1b5a0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
1b5a1 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c  Str(&pCtx->s, z,
1b5a2 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36   n, SQLITE_UTF16
1b5a3 4e 41 54 49 56 45 2c 20 53 51 4c 49 54 45 5f 54  NATIVE, SQLITE_T
1b5a4 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 65 6e  RANSIENT);.}.#en
1b5a5 64 69 66 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  dif.SQLITE_API v
1b5a6 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
1b5a7 6c 74 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 63  lt_int(sqlite3_c
1b5a8 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69 6e  ontext *pCtx, in
1b5a9 74 20 69 56 61 6c 29 7b 0a 20 20 61 73 73 65 72  t iVal){.  asser
1b5aa 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1b5ab 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62  _held(pCtx->s.db
1b5ac 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71  ->mutex) );.  sq
1b5ad 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49  lite3VdbeMemSetI
1b5ae 6e 74 36 34 28 26 70 43 74 78 2d 3e 73 2c 20 28  nt64(&pCtx->s, (
1b5af 69 36 34 29 69 56 61 6c 29 3b 0a 7d 0a 53 51 4c  i64)iVal);.}.SQL
1b5b0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
1b5b1 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36  ite3_result_int6
1b5b2 34 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  4(sqlite3_contex
1b5b3 74 20 2a 70 43 74 78 2c 20 69 36 34 20 69 56 61  t *pCtx, i64 iVa
1b5b4 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71  l){.  assert( sq
1b5b5 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1b5b6 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74  (pCtx->s.db->mut
1b5b7 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  ex) );.  sqlite3
1b5b8 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28  VdbeMemSetInt64(
1b5b9 26 70 43 74 78 2d 3e 73 2c 20 69 56 61 6c 29 3b  &pCtx->s, iVal);
1b5ba 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  .}.SQLITE_API vo
1b5bb 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
1b5bc 74 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f 63  t_null(sqlite3_c
1b5bd 6f 6e 74 65 78 74 20 2a 70 43 74 78 29 7b 0a 20  ontext *pCtx){. 
1b5be 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1b5bf 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78  _mutex_held(pCtx
1b5c0 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29  ->s.db->mutex) )
1b5c1 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ;.  sqlite3VdbeM
1b5c2 65 6d 53 65 74 4e 75 6c 6c 28 26 70 43 74 78 2d  emSetNull(&pCtx-
1b5c3 3e 73 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  >s);.}.SQLITE_AP
1b5c4 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
1b5c5 65 73 75 6c 74 5f 74 65 78 74 28 0a 20 20 73 71  esult_text(.  sq
1b5c6 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
1b5c7 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68  Ctx, .  const ch
1b5c8 61 72 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c  ar *z, .  int n,
1b5c9 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28  .  void (*xDel)(
1b5ca 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73  void *).){.  ass
1b5cb 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1b5cc 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e  ex_held(pCtx->s.
1b5cd 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
1b5ce 73 65 74 52 65 73 75 6c 74 53 74 72 4f 72 45 72  setResultStrOrEr
1b5cf 72 6f 72 28 70 43 74 78 2c 20 7a 2c 20 6e 2c 20  ror(pCtx, z, n, 
1b5d0 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 78 44 65  SQLITE_UTF8, xDe
1b5d1 6c 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51  l);.}.#ifndef SQ
1b5d2 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a  LITE_OMIT_UTF16.
1b5d3 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
1b5d4 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
1b5d5 65 78 74 31 36 28 0a 20 20 73 71 6c 69 74 65 33  ext16(.  sqlite3
1b5d6 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20  _context *pCtx, 
1b5d7 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  .  const void *z
1b5d8 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 76  , .  int n, .  v
1b5d9 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64  oid (*xDel)(void
1b5da 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72 74 28   *).){.  assert(
1b5db 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
1b5dc 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e  eld(pCtx->s.db->
1b5dd 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 65 74 52  mutex) );.  setR
1b5de 65 73 75 6c 74 53 74 72 4f 72 45 72 72 6f 72 28  esultStrOrError(
1b5df 70 43 74 78 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49  pCtx, z, n, SQLI
1b5e0 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20  TE_UTF16NATIVE, 
1b5e1 78 44 65 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  xDel);.}.SQLITE_
1b5e2 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
1b5e3 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65  _result_text16be
1b5e4 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
1b5e5 65 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f  ext *pCtx, .  co
1b5e6 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20  nst void *z, .  
1b5e7 69 6e 74 20 6e 2c 20 0a 20 20 76 6f 69 64 20 28  int n, .  void (
1b5e8 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29  *xDel)(void *).)
1b5e9 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  {.  assert( sqli
1b5ea 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1b5eb 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78  Ctx->s.db->mutex
1b5ec 29 20 29 3b 0a 20 20 73 65 74 52 65 73 75 6c 74  ) );.  setResult
1b5ed 53 74 72 4f 72 45 72 72 6f 72 28 70 43 74 78 2c  StrOrError(pCtx,
1b5ee 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54   z, n, SQLITE_UT
1b5ef 46 31 36 42 45 2c 20 78 44 65 6c 29 3b 0a 7d 0a  F16BE, xDel);.}.
1b5f0 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
1b5f1 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
1b5f2 65 78 74 31 36 6c 65 28 0a 20 20 73 71 6c 69 74  ext16le(.  sqlit
1b5f3 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
1b5f4 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  , .  const void 
1b5f5 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20  *z, .  int n, . 
1b5f6 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f   void (*xDel)(vo
1b5f7 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72  id *).){.  asser
1b5f8 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1b5f9 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62  _held(pCtx->s.db
1b5fa 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 65  ->mutex) );.  se
1b5fb 74 52 65 73 75 6c 74 53 74 72 4f 72 45 72 72 6f  tResultStrOrErro
1b5fc 72 28 70 43 74 78 2c 20 7a 2c 20 6e 2c 20 53 51  r(pCtx, z, n, SQ
1b5fd 4c 49 54 45 5f 55 54 46 31 36 4c 45 2c 20 78 44  LITE_UTF16LE, xD
1b5fe 65 6c 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  el);.}.#endif /*
1b5ff 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
1b600 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  16 */.SQLITE_API
1b601 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
1b602 73 75 6c 74 5f 76 61 6c 75 65 28 73 71 6c 69 74  sult_value(sqlit
1b603 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
1b604 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
1b605 2a 70 56 61 6c 75 65 29 7b 0a 20 20 61 73 73 65  *pValue){.  asse
1b606 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1b607 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64  x_held(pCtx->s.d
1b608 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73  b->mutex) );.  s
1b609 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70  qlite3VdbeMemCop
1b60a 79 28 26 70 43 74 78 2d 3e 73 2c 20 70 56 61 6c  y(&pCtx->s, pVal
1b60b 75 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  ue);.}.SQLITE_AP
1b60c 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
1b60d 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 73  esult_zeroblob(s
1b60e 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
1b60f 70 43 74 78 2c 20 69 6e 74 20 6e 29 7b 0a 20 20  pCtx, int n){.  
1b610 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
1b611 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d  mutex_held(pCtx-
1b612 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  >s.db->mutex) );
1b613 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
1b614 6d 53 65 74 5a 65 72 6f 42 6c 6f 62 28 26 70 43  mSetZeroBlob(&pC
1b615 74 78 2d 3e 73 2c 20 6e 29 3b 0a 7d 0a 53 51 4c  tx->s, n);.}.SQL
1b616 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
1b617 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
1b618 72 5f 63 6f 64 65 28 73 71 6c 69 74 65 33 5f 63  r_code(sqlite3_c
1b619 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69 6e  ontext *pCtx, in
1b61a 74 20 65 72 72 43 6f 64 65 29 7b 0a 20 20 70 43  t errCode){.  pC
1b61b 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 65 72  tx->isError = er
1b61c 72 43 6f 64 65 3b 0a 20 20 69 66 28 20 70 43 74  rCode;.  if( pCt
1b61d 78 2d 3e 73 2e 66 6c 61 67 73 20 26 20 4d 45 4d  x->s.flags & MEM
1b61e 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c  _Null ){.    sql
1b61f 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74  ite3VdbeMemSetSt
1b620 72 28 26 70 43 74 78 2d 3e 73 2c 20 73 71 6c 69  r(&pCtx->s, sqli
1b621 74 65 33 45 72 72 53 74 72 28 65 72 72 43 6f 64  te3ErrStr(errCod
1b622 65 29 2c 20 2d 31 2c 20 0a 20 20 20 20 20 20 20  e), -1, .       
1b623 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b624 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53    SQLITE_UTF8, S
1b625 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
1b626 20 7d 0a 7d 0a 0a 2f 2a 20 46 6f 72 63 65 20 61   }.}../* Force a
1b627 6e 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20  n SQLITE_TOOBIG 
1b628 65 72 72 6f 72 2e 20 2a 2f 0a 53 51 4c 49 54 45  error. */.SQLITE
1b629 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
1b62a 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74  3_result_error_t
1b62b 6f 6f 62 69 67 28 73 71 6c 69 74 65 33 5f 63 6f  oobig(sqlite3_co
1b62c 6e 74 65 78 74 20 2a 70 43 74 78 29 7b 0a 20 20  ntext *pCtx){.  
1b62d 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
1b62e 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d  mutex_held(pCtx-
1b62f 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  >s.db->mutex) );
1b630 0a 20 20 70 43 74 78 2d 3e 69 73 45 72 72 6f 72  .  pCtx->isError
1b631 20 3d 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47   = SQLITE_TOOBIG
1b632 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ;.  sqlite3VdbeM
1b633 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e  emSetStr(&pCtx->
1b634 73 2c 20 22 73 74 72 69 6e 67 20 6f 72 20 62 6c  s, "string or bl
1b635 6f 62 20 74 6f 6f 20 62 69 67 22 2c 20 2d 31 2c  ob too big", -1,
1b636 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
1b637 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
1b638 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41  UTF8, SQLITE_STA
1b639 54 49 43 29 3b 0a 7d 0a 0a 2f 2a 20 41 6e 20 53  TIC);.}../* An S
1b63a 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 65 72 72 6f  QLITE_NOMEM erro
1b63b 72 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  r. */.SQLITE_API
1b63c 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
1b63d 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d  sult_error_nomem
1b63e 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
1b63f 20 2a 70 43 74 78 29 7b 0a 20 20 61 73 73 65 72   *pCtx){.  asser
1b640 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1b641 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62  _held(pCtx->s.db
1b642 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71  ->mutex) );.  sq
1b643 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e  lite3VdbeMemSetN
1b644 75 6c 6c 28 26 70 43 74 78 2d 3e 73 29 3b 0a 20  ull(&pCtx->s);. 
1b645 20 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d   pCtx->isError =
1b646 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
1b647 20 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 61 6c   pCtx->s.db->mal
1b648 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 7d  locFailed = 1;.}
1b649 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20  ../*.** Execute 
1b64a 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 70 53  the statement pS
1b64b 74 6d 74 2c 20 65 69 74 68 65 72 20 75 6e 74 69  tmt, either unti
1b64c 6c 20 61 20 72 6f 77 20 6f 66 20 64 61 74 61 20  l a row of data 
1b64d 69 73 20 72 65 61 64 79 2c 20 74 68 65 0a 2a 2a  is ready, the.**
1b64e 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 63 6f   statement is co
1b64f 6d 70 6c 65 74 65 6c 79 20 65 78 65 63 75 74 65  mpletely execute
1b650 64 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 6f 63  d or an error oc
1b651 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  curs..**.** This
1b652 20 72 6f 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65   routine impleme
1b653 6e 74 73 20 74 68 65 20 62 75 6c 6b 20 6f 66 20  nts the bulk of 
1b654 74 68 65 20 6c 6f 67 69 63 20 62 65 68 69 6e 64  the logic behind
1b655 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 65 70   the sqlite_step
1b656 28 29 0a 2a 2a 20 41 50 49 2e 20 20 54 68 65 20  ().** API.  The 
1b657 6f 6e 6c 79 20 74 68 69 6e 67 20 6f 6d 69 74 74  only thing omitt
1b658 65 64 20 69 73 20 74 68 65 20 61 75 74 6f 6d 61  ed is the automa
1b659 74 69 63 20 72 65 63 6f 6d 70 69 6c 65 20 69 66  tic recompile if
1b65a 20 61 20 0a 2a 2a 20 73 63 68 65 6d 61 20 63 68   a .** schema ch
1b65b 61 6e 67 65 20 68 61 73 20 6f 63 63 75 72 72 65  ange has occurre
1b65c 64 2e 20 20 54 68 61 74 20 64 65 74 61 69 6c 20  d.  That detail 
1b65d 69 73 20 68 61 6e 64 6c 65 64 20 62 79 20 74 68  is handled by th
1b65e 65 0a 2a 2a 20 6f 75 74 65 72 20 73 71 6c 69 74  e.** outer sqlit
1b65f 65 33 5f 73 74 65 70 28 29 20 77 72 61 70 70 65  e3_step() wrappe
1b660 72 20 70 72 6f 63 65 64 75 72 65 2e 0a 2a 2f 0a  r procedure..*/.
1b661 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74  static int sqlit
1b662 65 33 53 74 65 70 28 56 64 62 65 20 2a 70 29 7b  e3Step(Vdbe *p){
1b663 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a  .  sqlite3 *db;.
1b664 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73    int rc;..  ass
1b665 65 72 74 28 70 29 3b 0a 20 20 69 66 28 20 70 2d  ert(p);.  if( p-
1b666 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d 41 47  >magic!=VDBE_MAG
1b667 49 43 5f 52 55 4e 20 29 7b 0a 20 20 20 20 72 65  IC_RUN ){.    re
1b668 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55  turn SQLITE_MISU
1b669 53 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 73  SE;.  }..  /* As
1b66a 73 65 72 74 20 74 68 61 74 20 6d 61 6c 6c 6f 63  sert that malloc
1b66b 28 29 20 68 61 73 20 6e 6f 74 20 66 61 69 6c 65  () has not faile
1b66c 64 20 2a 2f 0a 20 20 64 62 20 3d 20 70 2d 3e 64  d */.  db = p->d
1b66d 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  b;.  if( db->mal
1b66e 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
1b66f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
1b670 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  OMEM;.  }..  if(
1b671 20 70 2d 3e 70 63 3c 3d 30 20 26 26 20 70 2d 3e   p->pc<=0 && p->
1b672 65 78 70 69 72 65 64 20 29 7b 0a 20 20 20 20 69  expired ){.    i
1b673 66 28 20 41 4c 57 41 59 53 28 70 2d 3e 72 63 3d  f( ALWAYS(p->rc=
1b674 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 7b 0a 20  =SQLITE_OK) ){. 
1b675 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c       p->rc = SQL
1b676 49 54 45 5f 53 43 48 45 4d 41 3b 0a 20 20 20 20  ITE_SCHEMA;.    
1b677 7d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  }.    rc = SQLIT
1b678 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 67 6f 74  E_ERROR;.    got
1b679 6f 20 65 6e 64 5f 6f 66 5f 73 74 65 70 3b 0a 20  o end_of_step;. 
1b67a 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33   }.  if( sqlite3
1b67b 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a  SafetyOn(db) ){.
1b67c 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49      p->rc = SQLI
1b67d 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 20 20 72  TE_MISUSE;.    r
1b67e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53  eturn SQLITE_MIS
1b67f 55 53 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  USE;.  }.  if( p
1b680 2d 3e 70 63 3c 30 20 29 7b 0a 20 20 20 20 2f 2a  ->pc<0 ){.    /*
1b681 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f   If there are no
1b682 20 6f 74 68 65 72 20 73 74 61 74 65 6d 65 6e 74   other statement
1b683 73 20 63 75 72 72 65 6e 74 6c 79 20 72 75 6e 6e  s currently runn
1b684 69 6e 67 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a  ing, then.    **
1b685 20 72 65 73 65 74 20 74 68 65 20 69 6e 74 65 72   reset the inter
1b686 72 75 70 74 20 66 6c 61 67 2e 20 20 54 68 69 73  rupt flag.  This
1b687 20 70 72 65 76 65 6e 74 73 20 61 20 63 61 6c 6c   prevents a call
1b688 20 74 6f 20 73 71 6c 69 74 65 33 5f 69 6e 74 65   to sqlite3_inte
1b689 72 72 75 70 74 0a 20 20 20 20 2a 2a 20 66 72 6f  rrupt.    ** fro
1b68a 6d 20 69 6e 74 65 72 72 75 70 74 69 6e 67 20 61  m interrupting a
1b68b 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20   statement that 
1b68c 68 61 73 20 6e 6f 74 20 79 65 74 20 73 74 61 72  has not yet star
1b68d 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ted..    */.    
1b68e 69 66 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64  if( db->activeVd
1b68f 62 65 43 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20  beCnt==0 ){.    
1b690 20 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72    db->u1.isInter
1b691 72 75 70 74 65 64 20 3d 20 30 3b 0a 20 20 20 20  rupted = 0;.    
1b692 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
1b693 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 20 20 20  E_OMIT_TRACE.   
1b694 20 69 66 28 20 64 62 2d 3e 78 50 72 6f 66 69 6c   if( db->xProfil
1b695 65 20 26 26 20 21 64 62 2d 3e 69 6e 69 74 2e 62  e && !db->init.b
1b696 75 73 79 20 29 7b 0a 20 20 20 20 20 20 64 6f 75  usy ){.      dou
1b697 62 6c 65 20 72 4e 6f 77 3b 0a 20 20 20 20 20 20  ble rNow;.      
1b698 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74  sqlite3OsCurrent
1b699 54 69 6d 65 28 64 62 2d 3e 70 56 66 73 2c 20 26  Time(db->pVfs, &
1b69a 72 4e 6f 77 29 3b 0a 20 20 20 20 20 20 70 2d 3e  rNow);.      p->
1b69b 73 74 61 72 74 54 69 6d 65 20 3d 20 28 75 36 34  startTime = (u64
1b69c 29 28 28 72 4e 6f 77 20 2d 20 28 69 6e 74 29 72  )((rNow - (int)r
1b69d 4e 6f 77 29 2a 33 36 30 30 2e 30 2a 32 34 2e 30  Now)*3600.0*24.0
1b69e 2a 31 30 30 30 30 30 30 30 30 30 2e 30 29 3b 0a  *1000000000.0);.
1b69f 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20      }.#endif..  
1b6a0 20 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65    db->activeVdbe
1b6a1 43 6e 74 2b 2b 3b 0a 20 20 20 20 69 66 28 20 70  Cnt++;.    if( p
1b6a2 2d 3e 72 65 61 64 4f 6e 6c 79 3d 3d 30 20 29 20  ->readOnly==0 ) 
1b6a3 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74  db->writeVdbeCnt
1b6a4 2b 2b 3b 0a 20 20 20 20 70 2d 3e 70 63 20 3d 20  ++;.    p->pc = 
1b6a5 30 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  0;.  }.#ifndef S
1b6a6 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41  QLITE_OMIT_EXPLA
1b6a7 49 4e 0a 20 20 69 66 28 20 70 2d 3e 65 78 70 6c  IN.  if( p->expl
1b6a8 61 69 6e 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ain ){.    rc = 
1b6a9 73 71 6c 69 74 65 33 56 64 62 65 4c 69 73 74 28  sqlite3VdbeList(
1b6aa 70 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64  p);.  }else.#end
1b6ab 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
1b6ac 54 5f 45 58 50 4c 41 49 4e 20 2a 2f 0a 20 20 7b  T_EXPLAIN */.  {
1b6ad 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1b6ae 33 56 64 62 65 45 78 65 63 28 70 29 3b 0a 20 20  3VdbeExec(p);.  
1b6af 7d 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  }..  if( sqlite3
1b6b0 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 7b  SafetyOff(db) ){
1b6b1 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
1b6b2 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 0a 23 69  _MISUSE;.  }..#i
1b6b3 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1b6b4 54 5f 54 52 41 43 45 0a 20 20 2f 2a 20 49 6e 76  T_TRACE.  /* Inv
1b6b5 6f 6b 65 20 74 68 65 20 70 72 6f 66 69 6c 65 20  oke the profile 
1b6b6 63 61 6c 6c 62 61 63 6b 20 69 66 20 74 68 65 72  callback if ther
1b6b7 65 20 69 73 20 6f 6e 65 0a 20 20 2a 2f 0a 20 20  e is one.  */.  
1b6b8 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52  if( rc!=SQLITE_R
1b6b9 4f 57 20 26 26 20 64 62 2d 3e 78 50 72 6f 66 69  OW && db->xProfi
1b6ba 6c 65 20 26 26 20 21 64 62 2d 3e 69 6e 69 74 2e  le && !db->init.
1b6bb 62 75 73 79 20 26 26 20 70 2d 3e 7a 53 71 6c 20  busy && p->zSql 
1b6bc 29 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20 72 4e  ){.    double rN
1b6bd 6f 77 3b 0a 20 20 20 20 75 36 34 20 65 6c 61 70  ow;.    u64 elap
1b6be 73 65 54 69 6d 65 3b 0a 0a 20 20 20 20 73 71 6c  seTime;..    sql
1b6bf 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d  ite3OsCurrentTim
1b6c0 65 28 64 62 2d 3e 70 56 66 73 2c 20 26 72 4e 6f  e(db->pVfs, &rNo
1b6c1 77 29 3b 0a 20 20 20 20 65 6c 61 70 73 65 54 69  w);.    elapseTi
1b6c2 6d 65 20 3d 20 28 75 36 34 29 28 28 72 4e 6f 77  me = (u64)((rNow
1b6c3 20 2d 20 28 69 6e 74 29 72 4e 6f 77 29 2a 33 36   - (int)rNow)*36
1b6c4 30 30 2e 30 2a 32 34 2e 30 2a 31 30 30 30 30 30  00.0*24.0*100000
1b6c5 30 30 30 30 2e 30 29 3b 0a 20 20 20 20 65 6c 61  0000.0);.    ela
1b6c6 70 73 65 54 69 6d 65 20 2d 3d 20 70 2d 3e 73 74  pseTime -= p->st
1b6c7 61 72 74 54 69 6d 65 3b 0a 20 20 20 20 64 62 2d  artTime;.    db-
1b6c8 3e 78 50 72 6f 66 69 6c 65 28 64 62 2d 3e 70 50  >xProfile(db->pP
1b6c9 72 6f 66 69 6c 65 41 72 67 2c 20 70 2d 3e 7a 53  rofileArg, p->zS
1b6ca 71 6c 2c 20 65 6c 61 70 73 65 54 69 6d 65 29 3b  ql, elapseTime);
1b6cb 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 64  .  }.#endif..  d
1b6cc 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 63 3b  b->errCode = rc;
1b6cd 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e 4f  .  if( SQLITE_NO
1b6ce 4d 45 4d 3d 3d 73 71 6c 69 74 65 33 41 70 69 45  MEM==sqlite3ApiE
1b6cf 78 69 74 28 70 2d 3e 64 62 2c 20 70 2d 3e 72 63  xit(p->db, p->rc
1b6d0 29 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d  ) ){.    p->rc =
1b6d1 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
1b6d2 20 7d 0a 65 6e 64 5f 6f 66 5f 73 74 65 70 3a 0a   }.end_of_step:.
1b6d3 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69    /* At this poi
1b6d4 6e 74 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  nt local variabl
1b6d5 65 20 72 63 20 68 6f 6c 64 73 20 74 68 65 20 76  e rc holds the v
1b6d6 61 6c 75 65 20 74 68 61 74 20 73 68 6f 75 6c 64  alue that should
1b6d7 20 62 65 20 0a 20 20 2a 2a 20 72 65 74 75 72 6e   be .  ** return
1b6d8 65 64 20 69 66 20 74 68 69 73 20 73 74 61 74 65  ed if this state
1b6d9 6d 65 6e 74 20 77 61 73 20 63 6f 6d 70 69 6c 65  ment was compile
1b6da 64 20 75 73 69 6e 67 20 74 68 65 20 6c 65 67 61  d using the lega
1b6db 63 79 20 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33  cy .  ** sqlite3
1b6dc 5f 70 72 65 70 61 72 65 28 29 20 69 6e 74 65 72  _prepare() inter
1b6dd 66 61 63 65 2e 20 41 63 63 6f 72 64 69 6e 67 20  face. According 
1b6de 74 6f 20 74 68 65 20 64 6f 63 73 2c 20 74 68 69  to the docs, thi
1b6df 73 20 63 61 6e 20 6f 6e 6c 79 0a 20 20 2a 2a 20  s can only.  ** 
1b6e0 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 76 61  be one of the va
1b6e1 6c 75 65 73 20 69 6e 20 74 68 65 20 66 69 72 73  lues in the firs
1b6e2 74 20 61 73 73 65 72 74 28 29 20 62 65 6c 6f 77  t assert() below
1b6e3 2e 20 56 61 72 69 61 62 6c 65 20 70 2d 3e 72 63  . Variable p->rc
1b6e4 20 0a 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 20   .  ** contains 
1b6e5 74 68 65 20 76 61 6c 75 65 20 74 68 61 74 20 77  the value that w
1b6e6 6f 75 6c 64 20 62 65 20 72 65 74 75 72 6e 65 64  ould be returned
1b6e7 20 69 66 20 73 71 6c 69 74 65 33 5f 66 69 6e 61   if sqlite3_fina
1b6e8 6c 69 7a 65 28 29 20 0a 20 20 2a 2a 20 77 65 72  lize() .  ** wer
1b6e9 65 20 63 61 6c 6c 65 64 20 6f 6e 20 73 74 61 74  e called on stat
1b6ea 65 6d 65 6e 74 20 70 2e 0a 20 20 2a 2f 0a 20 20  ement p..  */.  
1b6eb 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
1b6ec 54 45 5f 52 4f 57 20 20 7c 7c 20 72 63 3d 3d 53  TE_ROW  || rc==S
1b6ed 51 4c 49 54 45 5f 44 4f 4e 45 20 20 20 7c 7c 20  QLITE_DONE   || 
1b6ee 72 63 3d 3d 53 51 4c 49 54 45 5f 45 52 52 4f 52  rc==SQLITE_ERROR
1b6ef 20 0a 20 20 20 20 20 20 20 7c 7c 20 72 63 3d 3d   .       || rc==
1b6f0 53 51 4c 49 54 45 5f 42 55 53 59 20 7c 7c 20 72  SQLITE_BUSY || r
1b6f1 63 3d 3d 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  c==SQLITE_MISUSE
1b6f2 0a 20 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  .  );.  assert( 
1b6f3 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f  p->rc!=SQLITE_RO
1b6f4 57 20 26 26 20 70 2d 3e 72 63 21 3d 53 51 4c 49  W && p->rc!=SQLI
1b6f5 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 69 66 28  TE_DONE );.  if(
1b6f6 20 70 2d 3e 69 73 50 72 65 70 61 72 65 56 32 20   p->isPrepareV2 
1b6f7 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f  && rc!=SQLITE_RO
1b6f8 57 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f  W && rc!=SQLITE_
1b6f9 44 4f 4e 45 20 29 7b 0a 20 20 20 20 2f 2a 20 49  DONE ){.    /* I
1b6fa 66 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74  f this statement
1b6fb 20 77 61 73 20 70 72 65 70 61 72 65 64 20 75 73   was prepared us
1b6fc 69 6e 67 20 73 71 6c 69 74 65 33 5f 70 72 65 70  ing sqlite3_prep
1b6fd 61 72 65 5f 76 32 28 29 2c 20 61 6e 64 20 61 6e  are_v2(), and an
1b6fe 0a 20 20 20 20 2a 2a 20 65 72 72 6f 72 20 68 61  .    ** error ha
1b6ff 73 20 6f 63 63 75 72 65 64 2c 20 74 68 65 6e 20  s occured, then 
1b700 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72  return the error
1b701 20 63 6f 64 65 20 69 6e 20 70 2d 3e 72 63 20 74   code in p->rc t
1b702 6f 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 61 6c  o the.    ** cal
1b703 6c 65 72 2e 20 53 65 74 20 74 68 65 20 65 72 72  ler. Set the err
1b704 6f 72 20 63 6f 64 65 20 69 6e 20 74 68 65 20 64  or code in the d
1b705 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74  atabase handle t
1b706 6f 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65  o the same value
1b707 2e 0a 20 20 20 20 2a 2f 20 0a 20 20 20 20 72 63  ..    */ .    rc
1b708 20 3d 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d   = db->errCode =
1b709 20 70 2d 3e 72 63 3b 0a 20 20 7d 0a 20 20 72 65   p->rc;.  }.  re
1b70a 74 75 72 6e 20 28 72 63 26 64 62 2d 3e 65 72 72  turn (rc&db->err
1b70b 4d 61 73 6b 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Mask);.}../*.** 
1b70c 54 68 69 73 20 69 73 20 74 68 65 20 74 6f 70 2d  This is the top-
1b70d 6c 65 76 65 6c 20 69 6d 70 6c 65 6d 65 6e 74 61  level implementa
1b70e 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 5f  tion of sqlite3_
1b70f 73 74 65 70 28 29 2e 20 20 43 61 6c 6c 0a 2a 2a  step().  Call.**
1b710 20 73 71 6c 69 74 65 33 53 74 65 70 28 29 20 74   sqlite3Step() t
1b711 6f 20 64 6f 20 6d 6f 73 74 20 6f 66 20 74 68 65  o do most of the
1b712 20 77 6f 72 6b 2e 20 20 49 66 20 61 20 73 63 68   work.  If a sch
1b713 65 6d 61 20 65 72 72 6f 72 20 6f 63 63 75 72 73  ema error occurs
1b714 2c 0a 2a 2a 20 63 61 6c 6c 20 73 71 6c 69 74 65  ,.** call sqlite
1b715 33 52 65 70 72 65 70 61 72 65 28 29 20 61 6e 64  3Reprepare() and
1b716 20 74 72 79 20 61 67 61 69 6e 2e 0a 2a 2f 0a 53   try again..*/.S
1b717 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1b718 6c 69 74 65 33 5f 73 74 65 70 28 73 71 6c 69 74  lite3_step(sqlit
1b719 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b  e3_stmt *pStmt){
1b71a 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
1b71b 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 69 66 28  TE_MISUSE;.  if(
1b71c 20 70 53 74 6d 74 20 29 7b 0a 20 20 20 20 69 6e   pStmt ){.    in
1b71d 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 56  t cnt = 0;.    V
1b71e 64 62 65 20 2a 76 20 3d 20 28 56 64 62 65 2a 29  dbe *v = (Vdbe*)
1b71f 70 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c 69 74  pStmt;.    sqlit
1b720 65 33 20 2a 64 62 20 3d 20 76 2d 3e 64 62 3b 0a  e3 *db = v->db;.
1b721 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
1b722 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
1b723 78 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28  x);.    while( (
1b724 72 63 20 3d 20 73 71 6c 69 74 65 33 53 74 65 70  rc = sqlite3Step
1b725 28 76 29 29 3d 3d 53 51 4c 49 54 45 5f 53 43 48  (v))==SQLITE_SCH
1b726 45 4d 41 0a 20 20 20 20 20 20 20 20 20 20 20 26  EMA.           &
1b727 26 20 63 6e 74 2b 2b 20 3c 20 35 0a 20 20 20 20  & cnt++ < 5.    
1b728 20 20 20 20 20 20 20 26 26 20 28 72 63 20 3d 20         && (rc = 
1b729 73 71 6c 69 74 65 33 52 65 70 72 65 70 61 72 65  sqlite3Reprepare
1b72a 28 76 29 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  (v))==SQLITE_OK 
1b72b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1b72c 5f 72 65 73 65 74 28 70 53 74 6d 74 29 3b 0a 20  _reset(pStmt);. 
1b72d 20 20 20 20 20 76 2d 3e 65 78 70 69 72 65 64 20       v->expired 
1b72e 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  = 0;.    }.    i
1b72f 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43  f( rc==SQLITE_SC
1b730 48 45 4d 41 20 26 26 20 41 4c 57 41 59 53 28 76  HEMA && ALWAYS(v
1b731 2d 3e 69 73 50 72 65 70 61 72 65 56 32 29 20 26  ->isPrepareV2) &
1b732 26 20 41 4c 57 41 59 53 28 64 62 2d 3e 70 45 72  & ALWAYS(db->pEr
1b733 72 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54  r) ){.      /* T
1b734 68 69 73 20 63 61 73 65 20 6f 63 63 75 72 73 20  his case occurs 
1b735 61 66 74 65 72 20 66 61 69 6c 69 6e 67 20 74 6f  after failing to
1b736 20 72 65 63 6f 6d 70 69 6c 65 20 61 6e 20 73 71   recompile an sq
1b737 6c 20 73 74 61 74 65 6d 65 6e 74 2e 20 0a 20 20  l statement. .  
1b738 20 20 20 20 2a 2a 20 54 68 65 20 65 72 72 6f 72      ** The error
1b739 20 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 74 68   message from th
1b73a 65 20 53 51 4c 20 63 6f 6d 70 69 6c 65 72 20 68  e SQL compiler h
1b73b 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
1b73c 6c 6f 61 64 65 64 20 0a 20 20 20 20 20 20 2a 2a  loaded .      **
1b73d 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61   into the databa
1b73e 73 65 20 68 61 6e 64 6c 65 2e 20 54 68 69 73 20  se handle. This 
1b73f 62 6c 6f 63 6b 20 63 6f 70 69 65 73 20 74 68 65  block copies the
1b740 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 0a   error message .
1b741 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74 68        ** from th
1b742 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  e database handl
1b743 65 20 69 6e 74 6f 20 74 68 65 20 73 74 61 74 65  e into the state
1b744 6d 65 6e 74 20 61 6e 64 20 73 65 74 73 20 74 68  ment and sets th
1b745 65 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 20 20  e statement.    
1b746 20 20 2a 2a 20 70 72 6f 67 72 61 6d 20 63 6f 75    ** program cou
1b747 6e 74 65 72 20 74 6f 20 30 20 74 6f 20 65 6e 73  nter to 0 to ens
1b748 75 72 65 20 74 68 61 74 20 77 68 65 6e 20 74 68  ure that when th
1b749 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 0a  e statement is .
1b74a 20 20 20 20 20 20 2a 2a 20 66 69 6e 61 6c 69 7a        ** finaliz
1b74b 65 64 20 6f 72 20 72 65 73 65 74 20 74 68 65 20  ed or reset the 
1b74c 70 61 72 73 65 72 20 65 72 72 6f 72 20 6d 65 73  parser error mes
1b74d 73 61 67 65 20 69 73 20 61 76 61 69 6c 61 62 6c  sage is availabl
1b74e 65 20 76 69 61 0a 20 20 20 20 20 20 2a 2a 20 73  e via.      ** s
1b74f 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 20  qlite3_errmsg() 
1b750 61 6e 64 20 73 71 6c 69 74 65 33 5f 65 72 72 63  and sqlite3_errc
1b751 6f 64 65 28 29 2e 0a 20 20 20 20 20 20 2a 2f 0a  ode()..      */.
1b752 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
1b753 20 2a 7a 45 72 72 20 3d 20 28 63 6f 6e 73 74 20   *zErr = (const 
1b754 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76  char *)sqlite3_v
1b755 61 6c 75 65 5f 74 65 78 74 28 64 62 2d 3e 70 45  alue_text(db->pE
1b756 72 72 29 3b 20 0a 20 20 20 20 20 20 73 71 6c 69  rr); .      sqli
1b757 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 76 2d  te3DbFree(db, v-
1b758 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20  >zErrMsg);.     
1b759 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63   if( !db->malloc
1b75a 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20  Failed ){.      
1b75b 20 20 76 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 73    v->zErrMsg = s
1b75c 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64  qlite3DbStrDup(d
1b75d 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20  b, zErr);.      
1b75e 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 20  } else {.       
1b75f 20 76 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b   v->zErrMsg = 0;
1b760 0a 20 20 20 20 20 20 20 20 76 2d 3e 72 63 20 3d  .        v->rc =
1b761 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
1b762 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
1b763 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69   rc = sqlite3Api
1b764 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20  Exit(db, rc);.  
1b765 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1b766 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
1b767 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
1b768 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72  c;.}../*.** Extr
1b769 61 63 74 20 74 68 65 20 75 73 65 72 20 64 61 74  act the user dat
1b76a 61 20 66 72 6f 6d 20 61 20 73 71 6c 69 74 65 33  a from a sqlite3
1b76b 5f 63 6f 6e 74 65 78 74 20 73 74 72 75 63 74 75  _context structu
1b76c 72 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61 0a  re and return a.
1b76d 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74  ** pointer to it
1b76e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1b76f 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 75 73  void *sqlite3_us
1b770 65 72 5f 64 61 74 61 28 73 71 6c 69 74 65 33 5f  er_data(sqlite3_
1b771 63 6f 6e 74 65 78 74 20 2a 70 29 7b 0a 20 20 61  context *p){.  a
1b772 73 73 65 72 74 28 20 70 20 26 26 20 70 2d 3e 70  ssert( p && p->p
1b773 46 75 6e 63 20 29 3b 0a 20 20 72 65 74 75 72 6e  Func );.  return
1b774 20 70 2d 3e 70 46 75 6e 63 2d 3e 70 55 73 65 72   p->pFunc->pUser
1b775 44 61 74 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45  Data;.}../*.** E
1b776 78 74 72 61 63 74 20 74 68 65 20 75 73 65 72 20  xtract the user 
1b777 64 61 74 61 20 66 72 6f 6d 20 61 20 73 71 6c 69  data from a sqli
1b778 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 74 72 75  te3_context stru
1b779 63 74 75 72 65 20 61 6e 64 20 72 65 74 75 72 6e  cture and return
1b77a 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f   a.** pointer to
1b77b 20 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41   it..*/.SQLITE_A
1b77c 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69  PI sqlite3 *sqli
1b77d 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68  te3_context_db_h
1b77e 61 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f  andle(sqlite3_co
1b77f 6e 74 65 78 74 20 2a 70 29 7b 0a 20 20 61 73 73  ntext *p){.  ass
1b780 65 72 74 28 20 70 20 26 26 20 70 2d 3e 70 46 75  ert( p && p->pFu
1b781 6e 63 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  nc );.  return p
1b782 2d 3e 73 2e 64 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ->s.db;.}../*.**
1b783 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69   The following i
1b784 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  s the implementa
1b785 74 69 6f 6e 20 6f 66 20 61 6e 20 53 51 4c 20 66  tion of an SQL f
1b786 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 61 6c 77  unction that alw
1b787 61 79 73 0a 2a 2a 20 66 61 69 6c 73 20 77 69 74  ays.** fails wit
1b788 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  h an error messa
1b789 67 65 20 73 74 61 74 69 6e 67 20 74 68 61 74 20  ge stating that 
1b78a 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  the function is 
1b78b 75 73 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 77  used in the.** w
1b78c 72 6f 6e 67 20 63 6f 6e 74 65 78 74 2e 20 20 54  rong context.  T
1b78d 68 65 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c  he sqlite3_overl
1b78e 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 29 20 41  oad_function() A
1b78f 50 49 20 6d 69 67 68 74 20 63 6f 6e 73 74 72 75  PI might constru
1b790 63 74 0a 2a 2a 20 53 51 4c 20 66 75 6e 63 74 69  ct.** SQL functi
1b791 6f 6e 20 74 68 61 74 20 75 73 65 20 74 68 69 73  on that use this
1b792 20 72 6f 75 74 69 6e 65 20 73 6f 20 74 68 61 74   routine so that
1b793 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 77   the functions w
1b794 69 6c 6c 20 65 78 69 73 74 0a 2a 2a 20 66 6f 72  ill exist.** for
1b795 20 6e 61 6d 65 20 72 65 73 6f 6c 75 74 69 6f 6e   name resolution
1b796 20 62 75 74 20 61 72 65 20 61 63 74 75 61 6c 6c   but are actuall
1b797 79 20 6f 76 65 72 6c 6f 61 64 65 64 20 62 79 20  y overloaded by 
1b798 74 68 65 20 78 46 69 6e 64 46 75 6e 63 74 69 6f  the xFindFunctio
1b799 6e 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f 66 20 76  n.** method of v
1b79a 69 72 74 75 61 6c 20 74 61 62 6c 65 73 2e 0a 2a  irtual tables..*
1b79b 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1b79c 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 6e 76   void sqlite3Inv
1b79d 61 6c 69 64 46 75 6e 63 74 69 6f 6e 28 0a 20 20  alidFunction(.  
1b79e 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1b79f 2a 63 6f 6e 74 65 78 74 2c 20 20 2f 2a 20 54 68  *context,  /* Th
1b7a0 65 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 69  e function calli
1b7a1 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
1b7a2 69 6e 74 20 4e 6f 74 55 73 65 64 2c 20 20 20 20  int NotUsed,    
1b7a3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
1b7a4 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
1b7a5 73 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f  s to the functio
1b7a6 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76  n */.  sqlite3_v
1b7a7 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 20  alue **NotUsed2 
1b7a8 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20 65 61    /* Value of ea
1b7a9 63 68 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 29  ch argument */.)
1b7aa 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  {.  const char *
1b7ab 7a 4e 61 6d 65 20 3d 20 63 6f 6e 74 65 78 74 2d  zName = context-
1b7ac 3e 70 46 75 6e 63 2d 3e 7a 4e 61 6d 65 3b 0a 20  >pFunc->zName;. 
1b7ad 20 63 68 61 72 20 2a 7a 45 72 72 3b 0a 20 20 55   char *zErr;.  U
1b7ae 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32  NUSED_PARAMETER2
1b7af 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65  (NotUsed, NotUse
1b7b0 64 32 29 3b 0a 20 20 7a 45 72 72 20 3d 20 73 71  d2);.  zErr = sq
1b7b1 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 0a 20  lite3_mprintf(. 
1b7b2 20 20 20 20 20 22 75 6e 61 62 6c 65 20 74 6f 20       "unable to 
1b7b3 75 73 65 20 66 75 6e 63 74 69 6f 6e 20 25 73 20  use function %s 
1b7b4 69 6e 20 74 68 65 20 72 65 71 75 65 73 74 65 64  in the requested
1b7b5 20 63 6f 6e 74 65 78 74 22 2c 20 7a 4e 61 6d 65   context", zName
1b7b6 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  );.  sqlite3_res
1b7b7 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78  ult_error(contex
1b7b8 74 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0a 20 20  t, zErr, -1);.  
1b7b9 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 72  sqlite3_free(zEr
1b7ba 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c  r);.}../*.** All
1b7bb 6f 63 61 74 65 20 6f 72 20 72 65 74 75 72 6e 20  ocate or return 
1b7bc 74 68 65 20 61 67 67 72 65 67 61 74 65 20 63 6f  the aggregate co
1b7bd 6e 74 65 78 74 20 66 6f 72 20 61 20 75 73 65 72  ntext for a user
1b7be 20 66 75 6e 63 74 69 6f 6e 2e 20 20 41 20 6e 65   function.  A ne
1b7bf 77 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69 73 20  w.** context is 
1b7c0 61 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 74 68 65  allocated on the
1b7c1 20 66 69 72 73 74 20 63 61 6c 6c 2e 20 20 53 75   first call.  Su
1b7c2 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 72  bsequent calls r
1b7c3 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20 73 61 6d  eturn the.** sam
1b7c4 65 20 63 6f 6e 74 65 78 74 20 74 68 61 74 20 77  e context that w
1b7c5 61 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 70  as returned on p
1b7c6 72 69 6f 72 20 63 61 6c 6c 73 2e 0a 2a 2f 0a 53  rior calls..*/.S
1b7c7 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
1b7c8 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
1b7c9 65 5f 63 6f 6e 74 65 78 74 28 73 71 6c 69 74 65  e_context(sqlite
1b7ca 33 5f 63 6f 6e 74 65 78 74 20 2a 70 2c 20 69 6e  3_context *p, in
1b7cb 74 20 6e 42 79 74 65 29 7b 0a 20 20 4d 65 6d 20  t nByte){.  Mem 
1b7cc 2a 70 4d 65 6d 3b 0a 20 20 61 73 73 65 72 74 28  *pMem;.  assert(
1b7cd 20 70 20 26 26 20 70 2d 3e 70 46 75 6e 63 20 26   p && p->pFunc &
1b7ce 26 20 70 2d 3e 70 46 75 6e 63 2d 3e 78 53 74 65  & p->pFunc->xSte
1b7cf 70 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  p );.  assert( s
1b7d0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
1b7d1 64 28 70 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78  d(p->s.db->mutex
1b7d2 29 20 29 3b 0a 20 20 70 4d 65 6d 20 3d 20 70 2d  ) );.  pMem = p-
1b7d3 3e 70 4d 65 6d 3b 0a 20 20 69 66 28 20 28 70 4d  >pMem;.  if( (pM
1b7d4 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  em->flags & MEM_
1b7d5 41 67 67 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69  Agg)==0 ){.    i
1b7d6 66 28 20 6e 42 79 74 65 3d 3d 30 20 29 7b 0a 20  f( nByte==0 ){. 
1b7d7 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1b7d8 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e  MemReleaseExtern
1b7d9 61 6c 28 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20  al(pMem);.      
1b7da 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
1b7db 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 70 4d  M_Null;.      pM
1b7dc 65 6d 2d 3e 7a 20 3d 20 30 3b 0a 20 20 20 20 7d  em->z = 0;.    }
1b7dd 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
1b7de 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70  te3VdbeMemGrow(p
1b7df 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 30 29 3b 0a  Mem, nByte, 0);.
1b7e0 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67        pMem->flag
1b7e1 73 20 3d 20 4d 45 4d 5f 41 67 67 3b 0a 20 20 20  s = MEM_Agg;.   
1b7e2 20 20 20 70 4d 65 6d 2d 3e 75 2e 70 44 65 66 20     pMem->u.pDef 
1b7e3 3d 20 70 2d 3e 70 46 75 6e 63 3b 0a 20 20 20 20  = p->pFunc;.    
1b7e4 20 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 20 29 7b    if( pMem->z ){
1b7e5 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28  .        memset(
1b7e6 70 4d 65 6d 2d 3e 7a 2c 20 30 2c 20 6e 42 79 74  pMem->z, 0, nByt
1b7e7 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  e);.      }.    
1b7e8 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28  }.  }.  return (
1b7e9 76 6f 69 64 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 7d  void*)pMem->z;.}
1b7ea 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
1b7eb 68 65 20 61 75 78 69 6c 61 72 79 20 64 61 74 61  he auxilary data
1b7ec 20 70 6f 69 6e 74 65 72 2c 20 69 66 20 61 6e 79   pointer, if any
1b7ed 2c 20 66 6f 72 20 74 68 65 20 69 41 72 67 27 74  , for the iArg't
1b7ee 68 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a  h argument to.**
1b7ef 20 74 68 65 20 75 73 65 72 2d 66 75 6e 63 74 69   the user-functi
1b7f0 6f 6e 20 64 65 66 69 6e 65 64 20 62 79 20 70 43  on defined by pC
1b7f1 74 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  tx..*/.SQLITE_AP
1b7f2 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  I void *sqlite3_
1b7f3 67 65 74 5f 61 75 78 64 61 74 61 28 73 71 6c 69  get_auxdata(sqli
1b7f4 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74  te3_context *pCt
1b7f5 78 2c 20 69 6e 74 20 69 41 72 67 29 7b 0a 20 20  x, int iArg){.  
1b7f6 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46  VdbeFunc *pVdbeF
1b7f7 75 6e 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  unc;..  assert( 
1b7f8 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
1b7f9 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d  ld(pCtx->s.db->m
1b7fa 75 74 65 78 29 20 29 3b 0a 20 20 70 56 64 62 65  utex) );.  pVdbe
1b7fb 46 75 6e 63 20 3d 20 70 43 74 78 2d 3e 70 56 64  Func = pCtx->pVd
1b7fc 62 65 46 75 6e 63 3b 0a 20 20 69 66 28 20 21 70  beFunc;.  if( !p
1b7fd 56 64 62 65 46 75 6e 63 20 7c 7c 20 69 41 72 67  VdbeFunc || iArg
1b7fe 3e 3d 70 56 64 62 65 46 75 6e 63 2d 3e 6e 41 75  >=pVdbeFunc->nAu
1b7ff 78 20 7c 7c 20 69 41 72 67 3c 30 20 29 7b 0a 20  x || iArg<0 ){. 
1b800 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
1b801 0a 20 20 72 65 74 75 72 6e 20 70 56 64 62 65 46  .  return pVdbeF
1b802 75 6e 63 2d 3e 61 70 41 75 78 5b 69 41 72 67 5d  unc->apAux[iArg]
1b803 2e 70 41 75 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  .pAux;.}../*.** 
1b804 53 65 74 20 74 68 65 20 61 75 78 69 6c 61 72 79  Set the auxilary
1b805 20 64 61 74 61 20 70 6f 69 6e 74 65 72 20 61 6e   data pointer an
1b806 64 20 64 65 6c 65 74 65 20 66 75 6e 63 74 69 6f  d delete functio
1b807 6e 2c 20 66 6f 72 20 74 68 65 20 69 41 72 67 27  n, for the iArg'
1b808 74 68 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74  th.** argument t
1b809 6f 20 74 68 65 20 75 73 65 72 2d 66 75 6e 63 74  o the user-funct
1b80a 69 6f 6e 20 64 65 66 69 6e 65 64 20 62 79 20 70  ion defined by p
1b80b 43 74 78 2e 20 41 6e 79 20 70 72 65 76 69 6f 75  Ctx. Any previou
1b80c 73 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 64 65  s value is.** de
1b80d 6c 65 74 65 64 20 62 79 20 63 61 6c 6c 69 6e 67  leted by calling
1b80e 20 74 68 65 20 64 65 6c 65 74 65 20 66 75 6e 63   the delete func
1b80f 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64 20 77  tion specified w
1b810 68 65 6e 20 69 74 20 77 61 73 20 73 65 74 2e 0a  hen it was set..
1b811 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  */.SQLITE_API vo
1b812 69 64 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61  id sqlite3_set_a
1b813 75 78 64 61 74 61 28 0a 20 20 73 71 6c 69 74 65  uxdata(.  sqlite
1b814 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c  3_context *pCtx,
1b815 20 0a 20 20 69 6e 74 20 69 41 72 67 2c 20 0a 20   .  int iArg, . 
1b816 20 76 6f 69 64 20 2a 70 41 75 78 2c 20 0a 20 20   void *pAux, .  
1b817 76 6f 69 64 20 28 2a 78 44 65 6c 65 74 65 29 28  void (*xDelete)(
1b818 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 73 74 72 75  void*).){.  stru
1b819 63 74 20 41 75 78 44 61 74 61 20 2a 70 41 75 78  ct AuxData *pAux
1b81a 44 61 74 61 3b 0a 20 20 56 64 62 65 46 75 6e 63  Data;.  VdbeFunc
1b81b 20 2a 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 69   *pVdbeFunc;.  i
1b81c 66 28 20 69 41 72 67 3c 30 20 29 20 67 6f 74 6f  f( iArg<0 ) goto
1b81d 20 66 61 69 6c 65 64 3b 0a 0a 20 20 61 73 73 65   failed;..  asse
1b81e 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1b81f 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64  x_held(pCtx->s.d
1b820 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70  b->mutex) );.  p
1b821 56 64 62 65 46 75 6e 63 20 3d 20 70 43 74 78 2d  VdbeFunc = pCtx-
1b822 3e 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 69 66  >pVdbeFunc;.  if
1b823 28 20 21 70 56 64 62 65 46 75 6e 63 20 7c 7c 20  ( !pVdbeFunc || 
1b824 70 56 64 62 65 46 75 6e 63 2d 3e 6e 41 75 78 3c  pVdbeFunc->nAux<
1b825 3d 69 41 72 67 20 29 7b 0a 20 20 20 20 69 6e 74  =iArg ){.    int
1b826 20 6e 41 75 78 20 3d 20 28 70 56 64 62 65 46 75   nAux = (pVdbeFu
1b827 6e 63 20 3f 20 70 56 64 62 65 46 75 6e 63 2d 3e  nc ? pVdbeFunc->
1b828 6e 41 75 78 20 3a 20 30 29 3b 0a 20 20 20 20 69  nAux : 0);.    i
1b829 6e 74 20 6e 4d 61 6c 6c 6f 63 20 3d 20 73 69 7a  nt nMalloc = siz
1b82a 65 6f 66 28 56 64 62 65 46 75 6e 63 29 20 2b 20  eof(VdbeFunc) + 
1b82b 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 41 75  sizeof(struct Au
1b82c 78 44 61 74 61 29 2a 69 41 72 67 3b 0a 20 20 20  xData)*iArg;.   
1b82d 20 70 56 64 62 65 46 75 6e 63 20 3d 20 73 71 6c   pVdbeFunc = sql
1b82e 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 70 43  ite3DbRealloc(pC
1b82f 74 78 2d 3e 73 2e 64 62 2c 20 70 56 64 62 65 46  tx->s.db, pVdbeF
1b830 75 6e 63 2c 20 6e 4d 61 6c 6c 6f 63 29 3b 0a 20  unc, nMalloc);. 
1b831 20 20 20 69 66 28 20 21 70 56 64 62 65 46 75 6e     if( !pVdbeFun
1b832 63 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  c ){.      goto 
1b833 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20  failed;.    }.  
1b834 20 20 70 43 74 78 2d 3e 70 56 64 62 65 46 75 6e    pCtx->pVdbeFun
1b835 63 20 3d 20 70 56 64 62 65 46 75 6e 63 3b 0a 20  c = pVdbeFunc;. 
1b836 20 20 20 6d 65 6d 73 65 74 28 26 70 56 64 62 65     memset(&pVdbe
1b837 46 75 6e 63 2d 3e 61 70 41 75 78 5b 6e 41 75 78  Func->apAux[nAux
1b838 5d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 74 72  ], 0, sizeof(str
1b839 75 63 74 20 41 75 78 44 61 74 61 29 2a 28 69 41  uct AuxData)*(iA
1b83a 72 67 2b 31 2d 6e 41 75 78 29 29 3b 0a 20 20 20  rg+1-nAux));.   
1b83b 20 70 56 64 62 65 46 75 6e 63 2d 3e 6e 41 75 78   pVdbeFunc->nAux
1b83c 20 3d 20 69 41 72 67 2b 31 3b 0a 20 20 20 20 70   = iArg+1;.    p
1b83d 56 64 62 65 46 75 6e 63 2d 3e 70 46 75 6e 63 20  VdbeFunc->pFunc 
1b83e 3d 20 70 43 74 78 2d 3e 70 46 75 6e 63 3b 0a 20  = pCtx->pFunc;. 
1b83f 20 7d 0a 0a 20 20 70 41 75 78 44 61 74 61 20 3d   }..  pAuxData =
1b840 20 26 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41   &pVdbeFunc->apA
1b841 75 78 5b 69 41 72 67 5d 3b 0a 20 20 69 66 28 20  ux[iArg];.  if( 
1b842 70 41 75 78 44 61 74 61 2d 3e 70 41 75 78 20 26  pAuxData->pAux &
1b843 26 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65 6c  & pAuxData->xDel
1b844 65 74 65 20 29 7b 0a 20 20 20 20 70 41 75 78 44  ete ){.    pAuxD
1b845 61 74 61 2d 3e 78 44 65 6c 65 74 65 28 70 41 75  ata->xDelete(pAu
1b846 78 44 61 74 61 2d 3e 70 41 75 78 29 3b 0a 20 20  xData->pAux);.  
1b847 7d 0a 20 20 70 41 75 78 44 61 74 61 2d 3e 70 41  }.  pAuxData->pA
1b848 75 78 20 3d 20 70 41 75 78 3b 0a 20 20 70 41 75  ux = pAux;.  pAu
1b849 78 44 61 74 61 2d 3e 78 44 65 6c 65 74 65 20 3d  xData->xDelete =
1b84a 20 78 44 65 6c 65 74 65 3b 0a 20 20 72 65 74 75   xDelete;.  retu
1b84b 72 6e 3b 0a 0a 66 61 69 6c 65 64 3a 0a 20 20 69  rn;..failed:.  i
1b84c 66 28 20 78 44 65 6c 65 74 65 20 29 7b 0a 20 20  f( xDelete ){.  
1b84d 20 20 78 44 65 6c 65 74 65 28 70 41 75 78 29 3b    xDelete(pAux);
1b84e 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  .  }.}..#ifndef 
1b84f 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52  SQLITE_OMIT_DEPR
1b850 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 52 65 74  ECATED./*.** Ret
1b851 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
1b852 66 20 74 69 6d 65 73 20 74 68 65 20 53 74 65 70  f times the Step
1b853 20 66 75 6e 63 74 69 6f 6e 20 6f 66 20 61 20 61   function of a a
1b854 67 67 72 65 67 61 74 65 20 68 61 73 20 62 65 65  ggregate has bee
1b855 6e 20 0a 2a 2a 20 63 61 6c 6c 65 64 2e 0a 2a 2a  n .** called..**
1b856 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
1b857 6e 20 69 73 20 64 65 70 72 65 63 61 74 65 64 2e  n is deprecated.
1b858 20 20 44 6f 20 6e 6f 74 20 75 73 65 20 69 74 20    Do not use it 
1b859 66 6f 72 20 6e 65 77 20 63 6f 64 65 2e 20 20 49  for new code.  I
1b85a 74 20 69 73 0a 2a 2a 20 70 72 6f 76 69 64 65 20  t is.** provide 
1b85b 6f 6e 6c 79 20 74 6f 20 61 76 6f 69 64 20 62 72  only to avoid br
1b85c 65 61 6b 69 6e 67 20 6c 65 67 61 63 79 20 63 6f  eaking legacy co
1b85d 64 65 2e 20 20 4e 65 77 20 61 67 67 72 65 67 61  de.  New aggrega
1b85e 74 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69  te function.** i
1b85f 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 73  mplementations s
1b860 68 6f 75 6c 64 20 6b 65 65 70 20 74 68 65 69 72  hould keep their
1b861 20 6f 77 6e 20 63 6f 75 6e 74 73 20 77 69 74 68   own counts with
1b862 69 6e 20 74 68 65 69 72 20 61 67 67 72 65 67 61  in their aggrega
1b863 74 65 0a 2a 2a 20 63 6f 6e 74 65 78 74 2e 0a 2a  te.** context..*
1b864 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1b865 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61   sqlite3_aggrega
1b866 74 65 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33  te_count(sqlite3
1b867 5f 63 6f 6e 74 65 78 74 20 2a 70 29 7b 0a 20 20  _context *p){.  
1b868 61 73 73 65 72 74 28 20 70 20 26 26 20 70 2d 3e  assert( p && p->
1b869 70 4d 65 6d 20 26 26 20 70 2d 3e 70 46 75 6e 63  pMem && p->pFunc
1b86a 20 26 26 20 70 2d 3e 70 46 75 6e 63 2d 3e 78 53   && p->pFunc->xS
1b86b 74 65 70 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  tep );.  return 
1b86c 70 2d 3e 70 4d 65 6d 2d 3e 6e 3b 0a 7d 0a 23 65  p->pMem->n;.}.#e
1b86d 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ndif../*.** Retu
1b86e 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
1b86f 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
1b870 72 65 73 75 6c 74 20 73 65 74 20 66 6f 72 20 74  result set for t
1b871 68 65 20 73 74 61 74 65 6d 65 6e 74 20 70 53 74  he statement pSt
1b872 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  mt..*/.SQLITE_AP
1b873 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f  I int sqlite3_co
1b874 6c 75 6d 6e 5f 63 6f 75 6e 74 28 73 71 6c 69 74  lumn_count(sqlit
1b875 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b  e3_stmt *pStmt){
1b876 0a 20 20 56 64 62 65 20 2a 70 56 6d 20 3d 20 28  .  Vdbe *pVm = (
1b877 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20  Vdbe *)pStmt;.  
1b878 72 65 74 75 72 6e 20 70 56 6d 20 3f 20 70 56 6d  return pVm ? pVm
1b879 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 3a 20 30  ->nResColumn : 0
1b87a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
1b87b 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
1b87c 76 61 6c 75 65 73 20 61 76 61 69 6c 61 62 6c 65  values available
1b87d 20 66 72 6f 6d 20 74 68 65 20 63 75 72 72 65 6e   from the curren
1b87e 74 20 72 6f 77 20 6f 66 20 74 68 65 0a 2a 2a 20  t row of the.** 
1b87f 63 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74  currently execut
1b880 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 70 53  ing statement pS
1b881 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  tmt..*/.SQLITE_A
1b882 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64  PI int sqlite3_d
1b883 61 74 61 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65  ata_count(sqlite
1b884 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a  3_stmt *pStmt){.
1b885 20 20 56 64 62 65 20 2a 70 56 6d 20 3d 20 28 56    Vdbe *pVm = (V
1b886 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 69  dbe *)pStmt;.  i
1b887 66 28 20 70 56 6d 3d 3d 30 20 7c 7c 20 70 56 6d  f( pVm==0 || pVm
1b888 2d 3e 70 52 65 73 75 6c 74 53 65 74 3d 3d 30 20  ->pResultSet==0 
1b889 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 72 65  ) return 0;.  re
1b88a 74 75 72 6e 20 70 56 6d 2d 3e 6e 52 65 73 43 6f  turn pVm->nResCo
1b88b 6c 75 6d 6e 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  lumn;.}.../*.** 
1b88c 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  Check to see if 
1b88d 63 6f 6c 75 6d 6e 20 69 43 6f 6c 20 6f 66 20 74  column iCol of t
1b88e 68 65 20 67 69 76 65 6e 20 73 74 61 74 65 6d 65  he given stateme
1b88f 6e 74 20 69 73 20 76 61 6c 69 64 2e 20 20 49 66  nt is valid.  If
1b890 0a 2a 2a 20 69 74 20 69 73 2c 20 72 65 74 75 72  .** it is, retur
1b891 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
1b892 68 65 20 4d 65 6d 20 66 6f 72 20 74 68 65 20 76  he Mem for the v
1b893 61 6c 75 65 20 6f 66 20 74 68 61 74 20 63 6f 6c  alue of that col
1b894 75 6d 6e 2e 0a 2a 2a 20 49 66 20 69 43 6f 6c 20  umn..** If iCol 
1b895 69 73 20 6e 6f 74 20 76 61 6c 69 64 2c 20 72 65  is not valid, re
1b896 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
1b897 6f 20 61 20 4d 65 6d 20 77 68 69 63 68 20 68 61  o a Mem which ha
1b898 73 20 61 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20  s a value.** of 
1b899 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  NULL..*/.static 
1b89a 4d 65 6d 20 2a 63 6f 6c 75 6d 6e 4d 65 6d 28 73  Mem *columnMem(s
1b89b 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1b89c 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 56 64  mt, int i){.  Vd
1b89d 62 65 20 2a 70 56 6d 3b 0a 20 20 69 6e 74 20 76  be *pVm;.  int v
1b89e 61 6c 73 3b 0a 20 20 4d 65 6d 20 2a 70 4f 75 74  als;.  Mem *pOut
1b89f 3b 0a 0a 20 20 70 56 6d 20 3d 20 28 56 64 62 65  ;..  pVm = (Vdbe
1b8a0 20 2a 29 70 53 74 6d 74 3b 0a 20 20 69 66 28 20   *)pStmt;.  if( 
1b8a1 70 56 6d 20 26 26 20 70 56 6d 2d 3e 70 52 65 73  pVm && pVm->pRes
1b8a2 75 6c 74 53 65 74 21 3d 30 20 26 26 20 69 3c 70  ultSet!=0 && i<p
1b8a3 56 6d 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 26  Vm->nResColumn &
1b8a4 26 20 69 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71  & i>=0 ){.    sq
1b8a5 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
1b8a6 72 28 70 56 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78  r(pVm->db->mutex
1b8a7 29 3b 0a 20 20 20 20 76 61 6c 73 20 3d 20 73 71  );.    vals = sq
1b8a8 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74  lite3_data_count
1b8a9 28 70 53 74 6d 74 29 3b 0a 20 20 20 20 70 4f 75  (pStmt);.    pOu
1b8aa 74 20 3d 20 26 70 56 6d 2d 3e 70 52 65 73 75 6c  t = &pVm->pResul
1b8ab 74 53 65 74 5b 69 5d 3b 0a 20 20 7d 65 6c 73 65  tSet[i];.  }else
1b8ac 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20  {.    /* If the 
1b8ad 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 73 20  value passed as 
1b8ae 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
1b8af 65 6e 74 20 69 73 20 6f 75 74 20 6f 66 20 72 61  ent is out of ra
1b8b0 6e 67 65 2c 20 72 65 74 75 72 6e 0a 20 20 20 20  nge, return.    
1b8b1 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** a pointer to 
1b8b2 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
1b8b3 61 74 69 63 20 4d 65 6d 20 6f 62 6a 65 63 74 20  atic Mem object 
1b8b4 77 68 69 63 68 20 63 6f 6e 74 61 69 6e 73 20 74  which contains t
1b8b5 68 65 0a 20 20 20 20 2a 2a 20 76 61 6c 75 65 20  he.    ** value 
1b8b6 53 51 4c 20 4e 55 4c 4c 2e 20 45 76 65 6e 20 74  SQL NULL. Even t
1b8b7 68 6f 75 67 68 20 74 68 65 20 4d 65 6d 20 73 74  hough the Mem st
1b8b8 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73  ructure contains
1b8b9 20 61 6e 20 65 6c 65 6d 65 6e 74 0a 20 20 20 20   an element.    
1b8ba 2a 2a 20 6f 66 20 74 79 70 65 20 69 36 34 2c 20  ** of type i64, 
1b8bb 6f 6e 20 63 65 72 74 61 69 6e 20 61 72 63 68 69  on certain archi
1b8bc 74 65 63 74 75 72 65 20 28 78 38 36 29 20 77 69  tecture (x86) wi
1b8bd 74 68 20 63 65 72 74 61 69 6e 20 63 6f 6d 70 69  th certain compi
1b8be 6c 65 72 0a 20 20 20 20 2a 2a 20 73 77 69 74 63  ler.    ** switc
1b8bf 68 65 73 20 28 2d 4f 73 29 2c 20 67 63 63 20 6d  hes (-Os), gcc m
1b8c0 61 79 20 61 6c 69 67 6e 20 74 68 69 73 20 4d 65  ay align this Me
1b8c1 6d 20 6f 62 6a 65 63 74 20 6f 6e 20 61 20 34 2d  m object on a 4-
1b8c2 62 79 74 65 20 62 6f 75 6e 64 61 72 79 0a 20 20  byte boundary.  
1b8c3 20 20 2a 2a 20 69 6e 73 74 65 61 64 20 6f 66 20    ** instead of 
1b8c4 61 6e 20 38 2d 62 79 74 65 20 6f 6e 65 2e 20 54  an 8-byte one. T
1b8c5 68 69 73 20 61 6c 6c 20 77 6f 72 6b 73 20 66 69  his all works fi
1b8c6 6e 65 2c 20 65 78 63 65 70 74 20 74 68 61 74 20  ne, except that 
1b8c7 77 68 65 6e 0a 20 20 20 20 2a 2a 20 72 75 6e 6e  when.    ** runn
1b8c8 69 6e 67 20 77 69 74 68 20 53 51 4c 49 54 45 5f  ing with SQLITE_
1b8c9 44 45 42 55 47 20 64 65 66 69 6e 65 64 20 74 68  DEBUG defined th
1b8ca 65 20 53 51 4c 69 74 65 20 63 6f 64 65 20 73 6f  e SQLite code so
1b8cb 6d 65 74 69 6d 65 73 20 61 73 73 65 72 74 28 29  metimes assert()
1b8cc 73 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 61 20  s.    ** that a 
1b8cd 4d 65 6d 20 73 74 72 75 63 74 75 72 65 20 69 73  Mem structure is
1b8ce 20 6c 6f 63 61 74 65 64 20 6f 6e 20 61 6e 20 38   located on an 8
1b8cf 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 79 2e 20  -byte boundary. 
1b8d0 54 6f 20 70 72 65 76 65 6e 74 0a 20 20 20 20 2a  To prevent.    *
1b8d1 2a 20 74 68 69 73 20 61 73 73 65 72 74 28 29 20  * this assert() 
1b8d2 66 72 6f 6d 20 66 61 69 6c 69 6e 67 2c 20 77 68  from failing, wh
1b8d3 65 6e 20 62 75 69 6c 64 69 6e 67 20 77 69 74 68  en building with
1b8d4 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 64 65   SQLITE_DEBUG de
1b8d5 66 69 6e 65 64 0a 20 20 20 20 2a 2a 20 75 73 69  fined.    ** usi
1b8d6 6e 67 20 67 63 63 2c 20 66 6f 72 63 65 20 6e 75  ng gcc, force nu
1b8d7 6c 6c 4d 65 6d 20 74 6f 20 62 65 20 38 2d 62 79  llMem to be 8-by
1b8d8 74 65 20 61 6c 69 67 6e 65 64 20 75 73 69 6e 67  te aligned using
1b8d9 20 74 68 65 20 6d 61 67 69 63 61 6c 0a 20 20 20   the magical.   
1b8da 20 2a 2a 20 5f 5f 61 74 74 72 69 62 75 74 65 5f   ** __attribute_
1b8db 5f 28 28 61 6c 69 67 6e 65 64 28 38 29 29 29 20  _((aligned(8))) 
1b8dc 6d 61 63 72 6f 2e 20 20 2a 2f 0a 20 20 20 20 73  macro.  */.    s
1b8dd 74 61 74 69 63 20 63 6f 6e 73 74 20 4d 65 6d 20  tatic const Mem 
1b8de 6e 75 6c 6c 4d 65 6d 20 0a 23 69 66 20 64 65 66  nullMem .#if def
1b8df 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
1b8e0 47 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f  G) && defined(__
1b8e1 47 4e 55 43 5f 5f 29 0a 20 20 20 20 20 20 5f 5f  GNUC__).      __
1b8e2 61 74 74 72 69 62 75 74 65 5f 5f 28 28 61 6c 69  attribute__((ali
1b8e3 67 6e 65 64 28 38 29 29 29 20 0a 23 65 6e 64 69  gned(8))) .#endi
1b8e4 66 0a 20 20 20 20 20 20 3d 20 7b 7b 30 7d 2c 20  f.      = {{0}, 
1b8e5 28 64 6f 75 62 6c 65 29 30 2c 20 30 2c 20 22 22  (double)0, 0, ""
1b8e6 2c 20 30 2c 20 4d 45 4d 5f 4e 75 6c 6c 2c 20 53  , 0, MEM_Null, S
1b8e7 51 4c 49 54 45 5f 4e 55 4c 4c 2c 20 30 2c 20 30  QLITE_NULL, 0, 0
1b8e8 2c 20 30 20 7d 3b 0a 0a 20 20 20 20 69 66 28 20  , 0 };..    if( 
1b8e9 70 56 6d 20 26 26 20 41 4c 57 41 59 53 28 70 56  pVm && ALWAYS(pV
1b8ea 6d 2d 3e 64 62 29 20 29 7b 0a 20 20 20 20 20 20  m->db) ){.      
1b8eb 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
1b8ec 74 65 72 28 70 56 6d 2d 3e 64 62 2d 3e 6d 75 74  ter(pVm->db->mut
1b8ed 65 78 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ex);.      sqlit
1b8ee 65 33 45 72 72 6f 72 28 70 56 6d 2d 3e 64 62 2c  e3Error(pVm->db,
1b8ef 20 53 51 4c 49 54 45 5f 52 41 4e 47 45 2c 20 30   SQLITE_RANGE, 0
1b8f0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f 75  );.    }.    pOu
1b8f1 74 20 3d 20 28 4d 65 6d 2a 29 26 6e 75 6c 6c 4d  t = (Mem*)&nullM
1b8f2 65 6d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  em;.  }.  return
1b8f3 20 70 4f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   pOut;.}../*.** 
1b8f4 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
1b8f5 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 69 6e   called after in
1b8f6 76 6f 6b 69 6e 67 20 61 6e 20 73 71 6c 69 74 65  voking an sqlite
1b8f7 33 5f 76 61 6c 75 65 5f 58 58 58 20 66 75 6e 63  3_value_XXX func
1b8f8 74 69 6f 6e 20 6f 6e 20 61 20 0a 2a 2a 20 63 6f  tion on a .** co
1b8f9 6c 75 6d 6e 20 76 61 6c 75 65 20 28 69 2e 65 2e  lumn value (i.e.
1b8fa 20 61 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65   a value returne
1b8fb 64 20 62 79 20 65 76 61 6c 75 61 74 69 6e 67 20  d by evaluating 
1b8fc 61 6e 20 53 51 4c 20 65 78 70 72 65 73 73 69 6f  an SQL expressio
1b8fd 6e 20 69 6e 20 74 68 65 0a 2a 2a 20 73 65 6c 65  n in the.** sele
1b8fe 63 74 20 6c 69 73 74 20 6f 66 20 61 20 53 45 4c  ct list of a SEL
1b8ff 45 43 54 20 73 74 61 74 65 6d 65 6e 74 29 20 74  ECT statement) t
1b900 68 61 74 20 6d 61 79 20 63 61 75 73 65 20 61 20  hat may cause a 
1b901 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65  malloc() failure
1b902 2e 20 49 66 20 0a 2a 2a 20 6d 61 6c 6c 6f 63 28  . If .** malloc(
1b903 29 20 68 61 73 20 66 61 69 6c 65 64 2c 20 74 68  ) has failed, th
1b904 65 20 74 68 72 65 61 64 73 20 6d 61 6c 6c 6f 63  e threads malloc
1b905 46 61 69 6c 65 64 20 66 6c 61 67 20 69 73 20 63  Failed flag is c
1b906 6c 65 61 72 65 64 20 61 6e 64 20 74 68 65 20 72  leared and the r
1b907 65 73 75 6c 74 0a 2a 2a 20 63 6f 64 65 20 6f 66  esult.** code of
1b908 20 73 74 61 74 65 6d 65 6e 74 20 70 53 74 6d 74   statement pStmt
1b909 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4e   set to SQLITE_N
1b90a 4f 4d 45 4d 2e 0a 2a 2a 0a 2a 2a 20 53 70 65 63  OMEM..**.** Spec
1b90b 69 66 69 63 61 6c 6c 79 2c 20 74 68 69 73 20 69  ifically, this i
1b90c 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69  s called from wi
1b90d 74 68 69 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  thin:.**.**     
1b90e 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
1b90f 6e 74 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69  nt().**     sqli
1b910 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34  te3_column_int64
1b911 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65  ().**     sqlite
1b912 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 0a  3_column_text().
1b913 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63  **     sqlite3_c
1b914 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 0a 2a  olumn_text16().*
1b915 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f  *     sqlite3_co
1b916 6c 75 6d 6e 5f 72 65 61 6c 28 29 0a 2a 2a 20 20  lumn_real().**  
1b917 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d     sqlite3_colum
1b918 6e 5f 62 79 74 65 73 28 29 0a 2a 2a 20 20 20 20  n_bytes().**    
1b919 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
1b91a 62 79 74 65 73 31 36 28 29 0a 2a 2a 0a 2a 2a 20  bytes16().**.** 
1b91b 42 75 74 20 6e 6f 74 20 66 6f 72 20 73 71 6c 69  But not for sqli
1b91c 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
1b91d 29 2c 20 77 68 69 63 68 20 6e 65 76 65 72 20 63  ), which never c
1b91e 61 6c 6c 73 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a  alls malloc()..*
1b91f 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f  /.static void co
1b920 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72  lumnMallocFailur
1b921 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  e(sqlite3_stmt *
1b922 70 53 74 6d 74 29 0a 7b 0a 20 20 2f 2a 20 49 66  pStmt).{.  /* If
1b923 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 65 64   malloc() failed
1b924 20 64 75 72 69 6e 67 20 61 6e 20 65 6e 63 6f 64   during an encod
1b925 69 6e 67 20 63 6f 6e 76 65 72 73 69 6f 6e 20 77  ing conversion w
1b926 69 74 68 69 6e 20 61 6e 0a 20 20 2a 2a 20 73 71  ithin an.  ** sq
1b927 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 58 58 58  lite3_column_XXX
1b928 20 41 50 49 2c 20 74 68 65 6e 20 73 65 74 20 74   API, then set t
1b929 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 6f  he return code o
1b92a 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  f the statement 
1b92b 74 6f 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4e  to.  ** SQLITE_N
1b92c 4f 4d 45 4d 2e 20 54 68 65 20 6e 65 78 74 20 63  OMEM. The next c
1b92d 61 6c 6c 20 74 6f 20 5f 73 74 65 70 28 29 20 28  all to _step() (
1b92e 69 66 20 61 6e 79 29 20 77 69 6c 6c 20 72 65 74  if any) will ret
1b92f 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
1b930 0a 20 20 2a 2a 20 61 6e 64 20 5f 66 69 6e 61 6c  .  ** and _final
1b931 69 7a 65 28 29 20 77 69 6c 6c 20 72 65 74 75 72  ize() will retur
1b932 6e 20 4e 4f 4d 45 4d 2e 0a 20 20 2a 2f 0a 20 20  n NOMEM..  */.  
1b933 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20  Vdbe *p = (Vdbe 
1b934 2a 29 70 53 74 6d 74 3b 0a 20 20 69 66 28 20 70  *)pStmt;.  if( p
1b935 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20   ){.    p->rc = 
1b936 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 70  sqlite3ApiExit(p
1b937 2d 3e 64 62 2c 20 70 2d 3e 72 63 29 3b 0a 20 20  ->db, p->rc);.  
1b938 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1b939 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74  leave(p->db->mut
1b93a 65 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a  ex);.  }.}../***
1b93b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b93c 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65  ********* sqlite
1b93d 33 5f 63 6f 6c 75 6d 6e 5f 20 20 2a 2a 2a 2a 2a  3_column_  *****
1b93e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b93f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
1b940 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74  e following rout
1b941 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 74 6f  ines are used to
1b942 20 61 63 63 65 73 73 20 65 6c 65 6d 65 6e 74 73   access elements
1b943 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20   of the current 
1b944 72 6f 77 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65  row.** in the re
1b945 73 75 6c 74 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c  sult set..*/.SQL
1b946 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f  ITE_API const vo
1b947 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  id *sqlite3_colu
1b948 6d 6e 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f  mn_blob(sqlite3_
1b949 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
1b94a 20 69 29 7b 0a 20 20 63 6f 6e 73 74 20 76 6f 69   i){.  const voi
1b94b 64 20 2a 76 61 6c 3b 0a 20 20 76 61 6c 20 3d 20  d *val;.  val = 
1b94c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
1b94d 6f 62 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53  ob( columnMem(pS
1b94e 74 6d 74 2c 69 29 20 29 3b 0a 20 20 2f 2a 20 45  tmt,i) );.  /* E
1b94f 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65 72 65  ven though there
1b950 20 69 73 20 6e 6f 20 65 6e 63 6f 64 69 6e 67 20   is no encoding 
1b951 63 6f 6e 76 65 72 73 69 6f 6e 2c 20 76 61 6c 75  conversion, valu
1b952 65 5f 62 6c 6f 62 28 29 20 6d 69 67 68 74 0a 20  e_blob() might. 
1b953 20 2a 2a 20 6e 65 65 64 20 74 6f 20 63 61 6c 6c   ** need to call
1b954 20 6d 61 6c 6c 6f 63 28 29 20 74 6f 20 65 78 70   malloc() to exp
1b955 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74 20 6f  and the result o
1b956 66 20 61 20 7a 65 72 6f 62 6c 6f 62 28 29 20 0a  f a zeroblob() .
1b957 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2e    ** expression.
1b958 20 0a 20 20 2a 2f 0a 20 20 63 6f 6c 75 6d 6e 4d   .  */.  columnM
1b959 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74  allocFailure(pSt
1b95a 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61  mt);.  return va
1b95b 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  l;.}.SQLITE_API 
1b95c 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  int sqlite3_colu
1b95d 6d 6e 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33  mn_bytes(sqlite3
1b95e 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e  _stmt *pStmt, in
1b95f 74 20 69 29 7b 0a 20 20 69 6e 74 20 76 61 6c 20  t i){.  int val 
1b960 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
1b961 62 79 74 65 73 28 20 63 6f 6c 75 6d 6e 4d 65 6d  bytes( columnMem
1b962 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63  (pStmt,i) );.  c
1b963 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75  olumnMallocFailu
1b964 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74  re(pStmt);.  ret
1b965 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54  urn val;.}.SQLIT
1b966 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1b967 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36  3_column_bytes16
1b968 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
1b969 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20  Stmt, int i){.  
1b96a 69 6e 74 20 76 61 6c 20 3d 20 73 71 6c 69 74 65  int val = sqlite
1b96b 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 31 36 28  3_value_bytes16(
1b96c 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74   columnMem(pStmt
1b96d 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d  ,i) );.  columnM
1b96e 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74  allocFailure(pSt
1b96f 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61  mt);.  return va
1b970 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  l;.}.SQLITE_API 
1b971 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 33 5f 63  double sqlite3_c
1b972 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 28 73 71 6c  olumn_double(sql
1b973 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1b974 2c 20 69 6e 74 20 69 29 7b 0a 20 20 64 6f 75 62  , int i){.  doub
1b975 6c 65 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 33  le val = sqlite3
1b976 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 20 63  _value_double( c
1b977 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69  olumnMem(pStmt,i
1b978 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c  ) );.  columnMal
1b979 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74  locFailure(pStmt
1b97a 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b  );.  return val;
1b97b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  .}.SQLITE_API in
1b97c 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  t sqlite3_column
1b97d 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d  _int(sqlite3_stm
1b97e 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29  t *pStmt, int i)
1b97f 7b 0a 20 20 69 6e 74 20 76 61 6c 20 3d 20 73 71  {.  int val = sq
1b980 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28  lite3_value_int(
1b981 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74   columnMem(pStmt
1b982 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d  ,i) );.  columnM
1b983 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74  allocFailure(pSt
1b984 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61  mt);.  return va
1b985 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  l;.}.SQLITE_API 
1b986 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 71 6c  sqlite_int64 sql
1b987 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36  ite3_column_int6
1b988 34 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  4(sqlite3_stmt *
1b989 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20  pStmt, int i){. 
1b98a 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 76 61   sqlite_int64 va
1b98b 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  l = sqlite3_valu
1b98c 65 5f 69 6e 74 36 34 28 20 63 6f 6c 75 6d 6e 4d  e_int64( columnM
1b98d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20  em(pStmt,i) );. 
1b98e 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69   columnMallocFai
1b98f 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72  lure(pStmt);.  r
1b990 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c  eturn val;.}.SQL
1b991 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 75 6e  ITE_API const un
1b992 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73 71 6c  signed char *sql
1b993 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
1b994 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
1b995 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20  Stmt, int i){.  
1b996 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
1b997 68 61 72 20 2a 76 61 6c 20 3d 20 73 71 6c 69 74  har *val = sqlit
1b998 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 20 63  e3_value_text( c
1b999 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69  olumnMem(pStmt,i
1b99a 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c  ) );.  columnMal
1b99b 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74  locFailure(pStmt
1b99c 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b  );.  return val;
1b99d 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71  .}.SQLITE_API sq
1b99e 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 73 71 6c  lite3_value *sql
1b99f 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75  ite3_column_valu
1b9a0 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  e(sqlite3_stmt *
1b9a1 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20  pStmt, int i){. 
1b9a2 20 4d 65 6d 20 2a 70 4f 75 74 20 3d 20 63 6f 6c   Mem *pOut = col
1b9a3 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 20 69 29  umnMem(pStmt, i)
1b9a4 3b 0a 20 20 69 66 28 20 70 4f 75 74 2d 3e 66 6c  ;.  if( pOut->fl
1b9a5 61 67 73 26 4d 45 4d 5f 53 74 61 74 69 63 20 29  ags&MEM_Static )
1b9a6 7b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67  {.    pOut->flag
1b9a7 73 20 26 3d 20 7e 4d 45 4d 5f 53 74 61 74 69 63  s &= ~MEM_Static
1b9a8 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67  ;.    pOut->flag
1b9a9 73 20 7c 3d 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a  s |= MEM_Ephem;.
1b9aa 20 20 7d 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c    }.  columnMall
1b9ab 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29  ocFailure(pStmt)
1b9ac 3b 0a 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69  ;.  return (sqli
1b9ad 74 65 33 5f 76 61 6c 75 65 20 2a 29 70 4f 75 74  te3_value *)pOut
1b9ae 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ;.}.#ifndef SQLI
1b9af 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51  TE_OMIT_UTF16.SQ
1b9b0 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76  LITE_API const v
1b9b1 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  oid *sqlite3_col
1b9b2 75 6d 6e 5f 74 65 78 74 31 36 28 73 71 6c 69 74  umn_text16(sqlit
1b9b3 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
1b9b4 69 6e 74 20 69 29 7b 0a 20 20 63 6f 6e 73 74 20  int i){.  const 
1b9b5 76 6f 69 64 20 2a 76 61 6c 20 3d 20 73 71 6c 69  void *val = sqli
1b9b6 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36  te3_value_text16
1b9b7 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d  ( columnMem(pStm
1b9b8 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e  t,i) );.  column
1b9b9 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53  MallocFailure(pS
1b9ba 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76  tmt);.  return v
1b9bb 61 6c 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  al;.}.#endif /* 
1b9bc 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
1b9bd 36 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  6 */.SQLITE_API 
1b9be 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  int sqlite3_colu
1b9bf 6d 6e 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f  mn_type(sqlite3_
1b9c0 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
1b9c1 20 69 29 7b 0a 20 20 69 6e 74 20 69 54 79 70 65   i){.  int iType
1b9c2 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
1b9c3 5f 74 79 70 65 28 20 63 6f 6c 75 6d 6e 4d 65 6d  _type( columnMem
1b9c4 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63  (pStmt,i) );.  c
1b9c5 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75  olumnMallocFailu
1b9c6 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74  re(pStmt);.  ret
1b9c7 75 72 6e 20 69 54 79 70 65 3b 0a 7d 0a 0a 2f 2a  urn iType;.}../*
1b9c8 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66   The following f
1b9c9 75 6e 63 74 69 6f 6e 20 69 73 20 65 78 70 65 72  unction is exper
1b9ca 69 6d 65 6e 74 61 6c 20 61 6e 64 20 73 75 62 6a  imental and subj
1b9cb 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 6f 72  ect to change or
1b9cc 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 2a 2f 0a 2f  .** removal */./
1b9cd 2a 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c  *int sqlite3_col
1b9ce 75 6d 6e 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65  umn_numeric_type
1b9cf 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
1b9d0 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 2a 2a  Stmt, int i){.**
1b9d1 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
1b9d2 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74  _value_numeric_t
1b9d3 79 70 65 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70  ype( columnMem(p
1b9d4 53 74 6d 74 2c 69 29 20 29 3b 0a 2a 2a 7d 0a 2a  Stmt,i) );.**}.*
1b9d5 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74  /../*.** Convert
1b9d6 20 74 68 65 20 4e 2d 74 68 20 65 6c 65 6d 65 6e   the N-th elemen
1b9d7 74 20 6f 66 20 70 53 74 6d 74 2d 3e 70 43 6f 6c  t of pStmt->pCol
1b9d8 4e 61 6d 65 5b 5d 20 69 6e 74 6f 20 61 20 73 74  Name[] into a st
1b9d9 72 69 6e 67 20 75 73 69 6e 67 0a 2a 2a 20 78 46  ring using.** xF
1b9da 75 6e 63 28 29 20 74 68 65 6e 20 72 65 74 75 72  unc() then retur
1b9db 6e 20 74 68 61 74 20 73 74 72 69 6e 67 2e 20 20  n that string.  
1b9dc 49 66 20 4e 20 69 73 20 6f 75 74 20 6f 66 20 72  If N is out of r
1b9dd 61 6e 67 65 2c 20 72 65 74 75 72 6e 20 30 2e 0a  ange, return 0..
1b9de 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20  **.** There are 
1b9df 75 70 20 74 6f 20 35 20 6e 61 6d 65 73 20 66 6f  up to 5 names fo
1b9e0 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 2e 20 20  r each column.  
1b9e1 75 73 65 54 79 70 65 20 64 65 74 65 72 6d 69 6e  useType determin
1b9e2 65 73 20 77 68 69 63 68 0a 2a 2a 20 6e 61 6d 65  es which.** name
1b9e3 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 48   is returned.  H
1b9e4 65 72 65 20 61 72 65 20 74 68 65 20 6e 61 6d 65  ere are the name
1b9e5 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 30 20 20 20  s:.**.**    0   
1b9e6 20 20 20 54 68 65 20 63 6f 6c 75 6d 6e 20 6e 61     The column na
1b9e7 6d 65 20 61 73 20 69 74 20 73 68 6f 75 6c 64 20  me as it should 
1b9e8 62 65 20 64 69 73 70 6c 61 79 65 64 20 66 6f 72  be displayed for
1b9e9 20 6f 75 74 70 75 74 0a 2a 2a 20 20 20 20 31 20   output.**    1 
1b9ea 20 20 20 20 20 54 68 65 20 64 61 74 61 74 79 70       The datatyp
1b9eb 65 20 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 63  e name for the c
1b9ec 6f 6c 75 6d 6e 0a 2a 2a 20 20 20 20 32 20 20 20  olumn.**    2   
1b9ed 20 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74     The name of t
1b9ee 68 65 20 64 61 74 61 62 61 73 65 20 74 68 61 74  he database that
1b9ef 20 74 68 65 20 63 6f 6c 75 6d 6e 20 64 65 72 69   the column deri
1b9f0 76 65 73 20 66 72 6f 6d 0a 2a 2a 20 20 20 20 33  ves from.**    3
1b9f1 20 20 20 20 20 20 54 68 65 20 6e 61 6d 65 20 6f        The name o
1b9f2 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74  f the table that
1b9f3 20 74 68 65 20 63 6f 6c 75 6d 6e 20 64 65 72 69   the column deri
1b9f4 76 65 73 20 66 72 6f 6d 0a 2a 2a 20 20 20 20 34  ves from.**    4
1b9f5 20 20 20 20 20 20 54 68 65 20 6e 61 6d 65 20 6f        The name o
1b9f6 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c 75  f the table colu
1b9f7 6d 6e 20 74 68 61 74 20 74 68 65 20 72 65 73 75  mn that the resu
1b9f8 6c 74 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65  lt column derive
1b9f9 73 20 66 72 6f 6d 0a 2a 2a 0a 2a 2a 20 49 66 20  s from.**.** If 
1b9fa 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 6e 6f  the result is no
1b9fb 74 20 61 20 73 69 6d 70 6c 65 20 63 6f 6c 75 6d  t a simple colum
1b9fc 6e 20 72 65 66 65 72 65 6e 63 65 20 28 69 66 20  n reference (if 
1b9fd 69 74 20 69 73 20 61 6e 20 65 78 70 72 65 73 73  it is an express
1b9fe 69 6f 6e 0a 2a 2a 20 6f 72 20 61 20 63 6f 6e 73  ion.** or a cons
1b9ff 74 61 6e 74 29 20 74 68 65 6e 20 75 73 65 54 79  tant) then useTy
1ba00 70 65 73 20 32 2c 20 33 2c 20 61 6e 64 20 34 20  pes 2, 3, and 4 
1ba01 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2f 0a  return NULL..*/.
1ba02 73 74 61 74 69 63 20 63 6f 6e 73 74 20 76 6f 69  static const voi
1ba03 64 20 2a 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20  d *columnName(. 
1ba04 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
1ba05 53 74 6d 74 2c 0a 20 20 69 6e 74 20 4e 2c 0a 20  Stmt,.  int N,. 
1ba06 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 28 2a 78   const void *(*x
1ba07 46 75 6e 63 29 28 4d 65 6d 2a 29 2c 0a 20 20 69  Func)(Mem*),.  i
1ba08 6e 74 20 75 73 65 54 79 70 65 0a 29 7b 0a 20 20  nt useType.){.  
1ba09 63 6f 6e 73 74 20 76 6f 69 64 20 2a 72 65 74 20  const void *ret 
1ba0a 3d 20 30 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d  = 0;.  Vdbe *p =
1ba0b 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a   (Vdbe *)pStmt;.
1ba0c 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74    int n;.  sqlit
1ba0d 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a  e3 *db = p->db;.
1ba0e 20 20 0a 20 20 61 73 73 65 72 74 28 20 64 62 21    .  assert( db!
1ba0f 3d 30 20 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69  =0 );.  n = sqli
1ba10 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74  te3_column_count
1ba11 28 70 53 74 6d 74 29 3b 0a 20 20 69 66 28 20 4e  (pStmt);.  if( N
1ba12 3c 6e 20 26 26 20 4e 3e 3d 30 20 29 7b 0a 20 20  <n && N>=0 ){.  
1ba13 20 20 4e 20 2b 3d 20 75 73 65 54 79 70 65 2a 6e    N += useType*n
1ba14 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
1ba15 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
1ba16 74 65 78 29 3b 0a 20 20 20 20 61 73 73 65 72 74  tex);.    assert
1ba17 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
1ba18 65 64 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65 74  ed==0 );.    ret
1ba19 20 3d 20 78 46 75 6e 63 28 26 70 2d 3e 61 43 6f   = xFunc(&p->aCo
1ba1a 6c 4e 61 6d 65 5b 4e 5d 29 3b 0a 20 20 20 20 20  lName[N]);.     
1ba1b 2f 2a 20 41 20 6d 61 6c 6c 6f 63 20 6d 61 79 20  /* A malloc may 
1ba1c 68 61 76 65 20 66 61 69 6c 65 64 20 69 6e 73 69  have failed insi
1ba1d 64 65 20 6f 66 20 74 68 65 20 78 46 75 6e 63 28  de of the xFunc(
1ba1e 29 20 63 61 6c 6c 2e 20 49 66 20 74 68 69 73 0a  ) call. If this.
1ba1f 20 20 20 20 2a 2a 20 69 73 20 74 68 65 20 63 61      ** is the ca
1ba20 73 65 2c 20 63 6c 65 61 72 20 74 68 65 20 6d 61  se, clear the ma
1ba21 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20  llocFailed flag 
1ba22 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e  and return NULL.
1ba23 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
1ba24 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1ba25 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61   ){.      db->ma
1ba26 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a  llocFailed = 0;.
1ba27 20 20 20 20 20 20 72 65 74 20 3d 20 30 3b 0a 20        ret = 0;. 
1ba28 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
1ba29 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
1ba2a 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72  >mutex);.  }.  r
1ba2b 65 74 75 72 6e 20 72 65 74 3b 0a 7d 0a 0a 2f 2a  eturn ret;.}../*
1ba2c 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
1ba2d 61 6d 65 20 6f 66 20 74 68 65 20 4e 74 68 20 63  ame of the Nth c
1ba2e 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 65 73  olumn of the res
1ba2f 75 6c 74 20 73 65 74 20 72 65 74 75 72 6e 65 64  ult set returned
1ba30 20 62 79 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65   by SQL.** state
1ba31 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53  ment pStmt..*/.S
1ba32 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
1ba33 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f  char *sqlite3_co
1ba34 6c 75 6d 6e 5f 6e 61 6d 65 28 73 71 6c 69 74 65  lumn_name(sqlite
1ba35 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69  3_stmt *pStmt, i
1ba36 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20  nt N){.  return 
1ba37 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20  columnName(.    
1ba38 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e    pStmt, N, (con
1ba39 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a  st void*(*)(Mem*
1ba3a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ))sqlite3_value_
1ba3b 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  text, COLNAME_NA
1ba3c 4d 45 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53  ME);.}.#ifndef S
1ba3d 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
1ba3e 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
1ba3f 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  t void *sqlite3_
1ba40 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 73 71  column_name16(sq
1ba41 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1ba42 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74  t, int N){.  ret
1ba43 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a  urn columnName(.
1ba44 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20        pStmt, N, 
1ba45 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28  (const void*(*)(
1ba46 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61  Mem*))sqlite3_va
1ba47 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e  lue_text16, COLN
1ba48 41 4d 45 5f 4e 41 4d 45 29 3b 0a 7d 0a 23 65 6e  AME_NAME);.}.#en
1ba49 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74  dif../*.** Const
1ba4a 72 61 69 6e 74 3a 20 20 49 66 20 79 6f 75 20 68  raint:  If you h
1ba4b 61 76 65 20 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d  ave ENABLE_COLUM
1ba4c 4e 5f 4d 45 54 41 44 41 54 41 20 74 68 65 6e 20  N_METADATA then 
1ba4d 79 6f 75 20 6d 75 73 74 0a 2a 2a 20 6e 6f 74 20  you must.** not 
1ba4e 64 65 66 69 6e 65 20 4f 4d 49 54 5f 44 45 43 4c  define OMIT_DECL
1ba4f 54 59 50 45 2e 0a 2a 2f 0a 23 69 66 20 64 65 66  TYPE..*/.#if def
1ba50 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
1ba51 5f 44 45 43 4c 54 59 50 45 29 20 26 26 20 64 65  _DECLTYPE) && de
1ba52 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
1ba53 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44  BLE_COLUMN_METAD
1ba54 41 54 41 29 0a 23 20 65 72 72 6f 72 20 22 4d 75  ATA).# error "Mu
1ba55 73 74 20 6e 6f 74 20 64 65 66 69 6e 65 20 62 6f  st not define bo
1ba56 74 68 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  th SQLITE_OMIT_D
1ba57 45 43 4c 54 59 50 45 20 5c 0a 20 20 20 20 20 20  ECLTYPE \.      
1ba58 20 20 20 61 6e 64 20 53 51 4c 49 54 45 5f 45 4e     and SQLITE_EN
1ba59 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41  ABLE_COLUMN_META
1ba5a 44 41 54 41 22 0a 23 65 6e 64 69 66 0a 0a 23 69  DATA".#endif..#i
1ba5b 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1ba5c 54 5f 44 45 43 4c 54 59 50 45 0a 2f 2a 0a 2a 2a  T_DECLTYPE./*.**
1ba5d 20 52 65 74 75 72 6e 20 74 68 65 20 63 6f 6c 75   Return the colu
1ba5e 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74  mn declaration t
1ba5f 79 70 65 20 28 69 66 20 61 70 70 6c 69 63 61 62  ype (if applicab
1ba60 6c 65 29 20 6f 66 20 74 68 65 20 27 69 27 74 68  le) of the 'i'th
1ba61 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f 66 20 74 68   column.** of th
1ba62 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20  e result set of 
1ba63 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 70 53  SQL statement pS
1ba64 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  tmt..*/.SQLITE_A
1ba65 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  PI const char *s
1ba66 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65  qlite3_column_de
1ba67 63 6c 74 79 70 65 28 73 71 6c 69 74 65 33 5f 73  cltype(sqlite3_s
1ba68 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
1ba69 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c  N){.  return col
1ba6a 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70  umnName(.      p
1ba6b 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20  Stmt, N, (const 
1ba6c 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73  void*(*)(Mem*))s
1ba6d 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
1ba6e 74 2c 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c 54  t, COLNAME_DECLT
1ba6f 59 50 45 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20  YPE);.}.#ifndef 
1ba70 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
1ba71 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  6.SQLITE_API con
1ba72 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
1ba73 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65  _column_decltype
1ba74 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  16(sqlite3_stmt 
1ba75 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a  *pStmt, int N){.
1ba76 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e    return columnN
1ba77 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74  ame(.      pStmt
1ba78 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64  , N, (const void
1ba79 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74  *(*)(Mem*))sqlit
1ba7a 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c  e3_value_text16,
1ba7b 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c 54 59 50   COLNAME_DECLTYP
1ba7c 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  E);.}.#endif /* 
1ba7d 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
1ba7e 36 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53  6 */.#endif /* S
1ba7f 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54  QLITE_OMIT_DECLT
1ba80 59 50 45 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53  YPE */..#ifdef S
1ba81 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c  QLITE_ENABLE_COL
1ba82 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a 2f 2a 0a  UMN_METADATA./*.
1ba83 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 61  ** Return the na
1ba84 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  me of the databa
1ba85 73 65 20 66 72 6f 6d 20 77 68 69 63 68 20 61 20  se from which a 
1ba86 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 64 65  result column de
1ba87 72 69 76 65 73 2e 0a 2a 2a 20 4e 55 4c 4c 20 69  rives..** NULL i
1ba88 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68  s returned if th
1ba89 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20  e result column 
1ba8a 69 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  is an expression
1ba8b 20 6f 72 20 63 6f 6e 73 74 61 6e 74 20 6f 72 0a   or constant or.
1ba8c 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65  ** anything else
1ba8d 20 77 68 69 63 68 20 69 73 20 6e 6f 74 20 61 6e   which is not an
1ba8e 20 75 6e 61 62 69 67 75 6f 75 73 20 72 65 66 65   unabiguous refe
1ba8f 72 65 6e 63 65 20 74 6f 20 61 20 64 61 74 61 62  rence to a datab
1ba90 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53  ase column..*/.S
1ba91 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
1ba92 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f  char *sqlite3_co
1ba93 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61  lumn_database_na
1ba94 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  me(sqlite3_stmt 
1ba95 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a  *pStmt, int N){.
1ba96 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e    return columnN
1ba97 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74  ame(.      pStmt
1ba98 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64  , N, (const void
1ba99 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74  *(*)(Mem*))sqlit
1ba9a 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43  e3_value_text, C
1ba9b 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 29  OLNAME_DATABASE)
1ba9c 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ;.}.#ifndef SQLI
1ba9d 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51  TE_OMIT_UTF16.SQ
1ba9e 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76  LITE_API const v
1ba9f 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  oid *sqlite3_col
1baa0 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d  umn_database_nam
1baa1 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  e16(sqlite3_stmt
1baa2 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b   *pStmt, int N){
1baa3 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e  .  return column
1baa4 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d  Name(.      pStm
1baa5 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69  t, N, (const voi
1baa6 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69  d*(*)(Mem*))sqli
1baa7 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36  te3_value_text16
1baa8 2c 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 41  , COLNAME_DATABA
1baa9 53 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  SE);.}.#endif /*
1baaa 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
1baab 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  16 */../*.** Ret
1baac 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  urn the name of 
1baad 74 68 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 77  the table from w
1baae 68 69 63 68 20 61 20 72 65 73 75 6c 74 20 63 6f  hich a result co
1baaf 6c 75 6d 6e 20 64 65 72 69 76 65 73 2e 0a 2a 2a  lumn derives..**
1bab0 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65   NULL is returne
1bab1 64 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 20  d if the result 
1bab2 63 6f 6c 75 6d 6e 20 69 73 20 61 6e 20 65 78 70  column is an exp
1bab3 72 65 73 73 69 6f 6e 20 6f 72 20 63 6f 6e 73 74  ression or const
1bab4 61 6e 74 20 6f 72 0a 2a 2a 20 61 6e 79 74 68 69  ant or.** anythi
1bab5 6e 67 20 65 6c 73 65 20 77 68 69 63 68 20 69 73  ng else which is
1bab6 20 6e 6f 74 20 61 6e 20 75 6e 61 62 69 67 75 6f   not an unabiguo
1bab7 75 73 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20  us reference to 
1bab8 61 20 64 61 74 61 62 61 73 65 20 63 6f 6c 75 6d  a database colum
1bab9 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  n..*/.SQLITE_API
1baba 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
1babb 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c  ite3_column_tabl
1babc 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73  e_name(sqlite3_s
1babd 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
1babe 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c  N){.  return col
1babf 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70  umnName(.      p
1bac0 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20  Stmt, N, (const 
1bac1 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73  void*(*)(Mem*))s
1bac2 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
1bac3 74 2c 20 43 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45  t, COLNAME_TABLE
1bac4 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c  );.}.#ifndef SQL
1bac5 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53  ITE_OMIT_UTF16.S
1bac6 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
1bac7 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f  void *sqlite3_co
1bac8 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31  lumn_table_name1
1bac9 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  6(sqlite3_stmt *
1baca 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20  pStmt, int N){. 
1bacb 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61   return columnNa
1bacc 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c  me(.      pStmt,
1bacd 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a   N, (const void*
1bace 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65  (*)(Mem*))sqlite
1bacf 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c 20  3_value_text16, 
1bad0 43 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45 29 3b 0a  COLNAME_TABLE);.
1bad1 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
1bad2 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f  TE_OMIT_UTF16 */
1bad3 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
1bad4 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74  he name of the t
1bad5 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d  able column from
1bad6 20 77 68 69 63 68 20 61 20 72 65 73 75 6c 74 20   which a result 
1bad7 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 2e 0a  column derives..
1bad8 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72  ** NULL is retur
1bad9 6e 65 64 20 69 66 20 74 68 65 20 72 65 73 75 6c  ned if the resul
1bada 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6e 20 65  t column is an e
1badb 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 63 6f 6e  xpression or con
1badc 73 74 61 6e 74 20 6f 72 0a 2a 2a 20 61 6e 79 74  stant or.** anyt
1badd 68 69 6e 67 20 65 6c 73 65 20 77 68 69 63 68 20  hing else which 
1bade 69 73 20 6e 6f 74 20 61 6e 20 75 6e 61 62 69 67  is not an unabig
1badf 75 6f 75 73 20 72 65 66 65 72 65 6e 63 65 20 74  uous reference t
1bae0 6f 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6c  o a database col
1bae1 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  umn..*/.SQLITE_A
1bae2 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  PI const char *s
1bae3 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72  qlite3_column_or
1bae4 69 67 69 6e 5f 6e 61 6d 65 28 73 71 6c 69 74 65  igin_name(sqlite
1bae5 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69  3_stmt *pStmt, i
1bae6 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20  nt N){.  return 
1bae7 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20  columnName(.    
1bae8 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e    pStmt, N, (con
1bae9 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a  st void*(*)(Mem*
1baea 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ))sqlite3_value_
1baeb 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 43 4f  text, COLNAME_CO
1baec 4c 55 4d 4e 29 3b 0a 7d 0a 23 69 66 6e 64 65 66  LUMN);.}.#ifndef
1baed 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
1baee 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  16.SQLITE_API co
1baef 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
1baf0 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f  3_column_origin_
1baf1 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73  name16(sqlite3_s
1baf2 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
1baf3 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c  N){.  return col
1baf4 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70  umnName(.      p
1baf5 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20  Stmt, N, (const 
1baf6 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73  void*(*)(Mem*))s
1baf7 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
1baf8 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c  t16, COLNAME_COL
1baf9 55 4d 4e 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  UMN);.}.#endif /
1bafa 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  * SQLITE_OMIT_UT
1bafb 46 31 36 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a  F16 */.#endif /*
1bafc 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43   SQLITE_ENABLE_C
1bafd 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 20 2a  OLUMN_METADATA *
1bafe 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /.../***********
1baff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bb00 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 69 6e  **** sqlite3_bin
1bb01 64 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  d_  ************
1bb02 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
1bb03 2a 2a 20 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20  ** .** Routines 
1bb04 75 73 65 64 20 74 6f 20 61 74 74 61 63 68 20 76  used to attach v
1bb05 61 6c 75 65 73 20 74 6f 20 77 69 6c 64 63 61 72  alues to wildcar
1bb06 64 73 20 69 6e 20 61 20 63 6f 6d 70 69 6c 65 64  ds in a compiled
1bb07 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a   SQL statement..
1bb08 2a 2f 0a 2f 2a 0a 2a 2a 20 55 6e 62 69 6e 64 20  */./*.** Unbind 
1bb09 74 68 65 20 76 61 6c 75 65 20 62 6f 75 6e 64 20  the value bound 
1bb0a 74 6f 20 76 61 72 69 61 62 6c 65 20 69 20 69 6e  to variable i in
1bb0b 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
1bb0c 20 70 2e 20 54 68 69 73 20 69 73 20 74 68 65 20   p. This is the 
1bb0d 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 61 73 20  .** the same as 
1bb0e 62 69 6e 64 69 6e 67 20 61 20 4e 55 4c 4c 20 76  binding a NULL v
1bb0f 61 6c 75 65 20 74 6f 20 74 68 65 20 63 6f 6c 75  alue to the colu
1bb10 6d 6e 2e 20 49 66 20 74 68 65 20 22 69 22 20 70  mn. If the "i" p
1bb11 61 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 6f  arameter is.** o
1bb12 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 74 68 65  ut of range, the
1bb13 6e 20 53 51 4c 49 54 45 5f 52 41 4e 47 45 20 69  n SQLITE_RANGE i
1bb14 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65  s returned. Othe
1bb15 77 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a  wise SQLITE_OK..
1bb16 2a 2a 0a 2a 2a 20 41 20 73 75 63 63 65 73 73 66  **.** A successf
1bb17 75 6c 20 65 76 61 6c 75 61 74 69 6f 6e 20 6f 66  ul evaluation of
1bb18 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 63   this routine ac
1bb19 71 75 69 72 65 73 20 74 68 65 20 6d 75 74 65 78  quires the mutex
1bb1a 20 6f 6e 20 70 2e 0a 2a 2a 20 74 68 65 20 6d 75   on p..** the mu
1bb1b 74 65 78 20 69 73 20 72 65 6c 65 61 73 65 64 20  tex is released 
1bb1c 69 66 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20 65  if any kind of e
1bb1d 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a  rror occurs..**.
1bb1e 2a 2a 20 54 68 65 20 65 72 72 6f 72 20 63 6f 64  ** The error cod
1bb1f 65 20 73 74 6f 72 65 64 20 69 6e 20 64 61 74 61  e stored in data
1bb20 62 61 73 65 20 70 2d 3e 64 62 20 69 73 20 6f 76  base p->db is ov
1bb21 65 72 77 72 69 74 74 65 6e 20 77 69 74 68 20 74  erwritten with t
1bb22 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c  he return.** val
1bb23 75 65 20 69 6e 20 61 6e 79 20 63 61 73 65 2e 0a  ue in any case..
1bb24 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76 64  */.static int vd
1bb25 62 65 55 6e 62 69 6e 64 28 56 64 62 65 20 2a 70  beUnbind(Vdbe *p
1bb26 2c 20 69 6e 74 20 69 29 7b 0a 20 20 4d 65 6d 20  , int i){.  Mem 
1bb27 2a 70 56 61 72 3b 0a 20 20 69 66 28 20 70 3d 3d  *pVar;.  if( p==
1bb28 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  0 ) return SQLIT
1bb29 45 5f 4d 49 53 55 53 45 3b 0a 20 20 73 71 6c 69  E_MISUSE;.  sqli
1bb2a 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
1bb2b 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  p->db->mutex);. 
1bb2c 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 21 3d 56   if( p->magic!=V
1bb2d 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 7c 7c  DBE_MAGIC_RUN ||
1bb2e 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20   p->pc>=0 ){.   
1bb2f 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 2d   sqlite3Error(p-
1bb30 3e 64 62 2c 20 53 51 4c 49 54 45 5f 4d 49 53 55  >db, SQLITE_MISU
1bb31 53 45 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69  SE, 0);.    sqli
1bb32 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
1bb33 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  p->db->mutex);. 
1bb34 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1bb35 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 69  _MISUSE;.  }.  i
1bb36 66 28 20 69 3c 31 20 7c 7c 20 69 3e 70 2d 3e 6e  f( i<1 || i>p->n
1bb37 56 61 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Var ){.    sqlit
1bb38 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20 53  e3Error(p->db, S
1bb39 51 4c 49 54 45 5f 52 41 4e 47 45 2c 20 30 29 3b  QLITE_RANGE, 0);
1bb3a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
1bb3b 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e  ex_leave(p->db->
1bb3c 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 65 74 75  mutex);.    retu
1bb3d 72 6e 20 53 51 4c 49 54 45 5f 52 41 4e 47 45 3b  rn SQLITE_RANGE;
1bb3e 0a 20 20 7d 0a 20 20 69 2d 2d 3b 0a 20 20 70 56  .  }.  i--;.  pV
1bb3f 61 72 20 3d 20 26 70 2d 3e 61 56 61 72 5b 69 5d  ar = &p->aVar[i]
1bb40 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ;.  sqlite3VdbeM
1bb41 65 6d 52 65 6c 65 61 73 65 28 70 56 61 72 29 3b  emRelease(pVar);
1bb42 0a 20 20 70 56 61 72 2d 3e 66 6c 61 67 73 20 3d  .  pVar->flags =
1bb43 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 73 71 6c   MEM_Null;.  sql
1bb44 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c  ite3Error(p->db,
1bb45 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b 0a   SQLITE_OK, 0);.
1bb46 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1bb47 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e  OK;.}../*.** Bin
1bb48 64 20 61 20 74 65 78 74 20 6f 72 20 42 4c 4f 42  d a text or BLOB
1bb49 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69   value..*/.stati
1bb4a 63 20 69 6e 74 20 62 69 6e 64 54 65 78 74 28 0a  c int bindText(.
1bb4b 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
1bb4c 70 53 74 6d 74 2c 20 20 20 2f 2a 20 54 68 65 20  pStmt,   /* The 
1bb4d 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 69 6e  statement to bin
1bb4e 64 20 61 67 61 69 6e 73 74 20 2a 2f 0a 20 20 69  d against */.  i
1bb4f 6e 74 20 69 2c 20 20 20 20 20 20 20 20 20 20 20  nt i,           
1bb50 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
1bb51 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20  f the parameter 
1bb52 74 6f 20 62 69 6e 64 20 2a 2f 0a 20 20 63 6f 6e  to bind */.  con
1bb53 73 74 20 76 6f 69 64 20 2a 7a 44 61 74 61 2c 20  st void *zData, 
1bb54 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
1bb55 6f 20 74 68 65 20 64 61 74 61 20 74 6f 20 62 65  o the data to be
1bb56 20 62 6f 75 6e 64 20 2a 2f 0a 20 20 69 6e 74 20   bound */.  int 
1bb57 6e 44 61 74 61 2c 20 20 20 20 20 20 20 20 20 20  nData,          
1bb58 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1bb59 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 74 6f  bytes of data to
1bb5a 20 62 65 20 62 6f 75 6e 64 20 2a 2f 0a 20 20 76   be bound */.  v
1bb5b 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64  oid (*xDel)(void
1bb5c 2a 29 2c 20 20 20 2f 2a 20 44 65 73 74 72 75 63  *),   /* Destruc
1bb5d 74 6f 72 20 66 6f 72 20 74 68 65 20 64 61 74 61  tor for the data
1bb5e 20 2a 2f 0a 20 20 75 38 20 65 6e 63 6f 64 69 6e   */.  u8 encodin
1bb5f 67 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  g            /* 
1bb60 45 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 68 65  Encoding for the
1bb61 20 64 61 74 61 20 2a 2f 0a 29 7b 0a 20 20 56 64   data */.){.  Vd
1bb62 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29  be *p = (Vdbe *)
1bb63 70 53 74 6d 74 3b 0a 20 20 4d 65 6d 20 2a 70 56  pStmt;.  Mem *pV
1bb64 61 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20  ar;.  int rc;.. 
1bb65 20 72 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64   rc = vdbeUnbind
1bb66 28 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63  (p, i);.  if( rc
1bb67 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1bb68 20 20 20 69 66 28 20 7a 44 61 74 61 21 3d 30 20     if( zData!=0 
1bb69 29 7b 0a 20 20 20 20 20 20 70 56 61 72 20 3d 20  ){.      pVar = 
1bb6a 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 3b 0a 20  &p->aVar[i-1];. 
1bb6b 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1bb6c 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70  3VdbeMemSetStr(p
1bb6d 56 61 72 2c 20 7a 44 61 74 61 2c 20 6e 44 61 74  Var, zData, nDat
1bb6e 61 2c 20 65 6e 63 6f 64 69 6e 67 2c 20 78 44 65  a, encoding, xDe
1bb6f 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  l);.      if( rc
1bb70 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 65  ==SQLITE_OK && e
1bb71 6e 63 6f 64 69 6e 67 21 3d 30 20 29 7b 0a 20 20  ncoding!=0 ){.  
1bb72 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1bb73 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f  e3VdbeChangeEnco
1bb74 64 69 6e 67 28 70 56 61 72 2c 20 45 4e 43 28 70  ding(pVar, ENC(p
1bb75 2d 3e 64 62 29 29 3b 0a 20 20 20 20 20 20 7d 0a  ->db));.      }.
1bb76 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
1bb77 6f 72 28 70 2d 3e 64 62 2c 20 72 63 2c 20 30 29  or(p->db, rc, 0)
1bb78 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
1bb79 69 74 65 33 41 70 69 45 78 69 74 28 70 2d 3e 64  ite3ApiExit(p->d
1bb7a 62 2c 20 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20  b, rc);.    }.  
1bb7b 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1bb7c 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74  leave(p->db->mut
1bb7d 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ex);.  }.  retur
1bb7e 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  n rc;.}.../*.** 
1bb7f 42 69 6e 64 20 61 20 62 6c 6f 62 20 76 61 6c 75  Bind a blob valu
1bb80 65 20 74 6f 20 61 6e 20 53 51 4c 20 73 74 61 74  e to an SQL stat
1bb81 65 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 2e 0a  ement variable..
1bb82 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
1bb83 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62  t sqlite3_bind_b
1bb84 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65 33 5f 73  lob(.  sqlite3_s
1bb85 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69  tmt *pStmt, .  i
1bb86 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20 76  nt i, .  const v
1bb87 6f 69 64 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69  oid *zData, .  i
1bb88 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69  nt nData, .  voi
1bb89 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29  d (*xDel)(void*)
1bb8a 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 69 6e  .){.  return bin
1bb8b 64 54 65 78 74 28 70 53 74 6d 74 2c 20 69 2c 20  dText(pStmt, i, 
1bb8c 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 78 44  zData, nData, xD
1bb8d 65 6c 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45  el, 0);.}.SQLITE
1bb8e 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1bb8f 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 28 73 71 6c  _bind_double(sql
1bb90 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1bb91 2c 20 69 6e 74 20 69 2c 20 64 6f 75 62 6c 65 20  , int i, double 
1bb92 72 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72  rValue){.  int r
1bb93 63 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28  c;.  Vdbe *p = (
1bb94 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20  Vdbe *)pStmt;.  
1bb95 72 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28  rc = vdbeUnbind(
1bb96 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d  p, i);.  if( rc=
1bb97 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1bb98 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1bb99 53 65 74 44 6f 75 62 6c 65 28 26 70 2d 3e 61 56  SetDouble(&p->aV
1bb9a 61 72 5b 69 2d 31 5d 2c 20 72 56 61 6c 75 65 29  ar[i-1], rValue)
1bb9b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
1bb9c 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d  tex_leave(p->db-
1bb9d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72  >mutex);.  }.  r
1bb9e 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49  eturn rc;.}.SQLI
1bb9f 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1bba0 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 71 6c 69  e3_bind_int(sqli
1bba1 74 65 33 5f 73 74 6d 74 20 2a 70 2c 20 69 6e 74  te3_stmt *p, int
1bba2 20 69 2c 20 69 6e 74 20 69 56 61 6c 75 65 29 7b   i, int iValue){
1bba3 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
1bba4 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 2c 20  3_bind_int64(p, 
1bba5 69 2c 20 28 69 36 34 29 69 56 61 6c 75 65 29 3b  i, (i64)iValue);
1bba6 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  .}.SQLITE_API in
1bba7 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69  t sqlite3_bind_i
1bba8 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 73 74 6d  nt64(sqlite3_stm
1bba9 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 2c  t *pStmt, int i,
1bbaa 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 56   sqlite_int64 iV
1bbab 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b  alue){.  int rc;
1bbac 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64  .  Vdbe *p = (Vd
1bbad 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63  be *)pStmt;.  rc
1bbae 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c   = vdbeUnbind(p,
1bbaf 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53   i);.  if( rc==S
1bbb0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1bbb1 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
1bbb2 74 49 6e 74 36 34 28 26 70 2d 3e 61 56 61 72 5b  tInt64(&p->aVar[
1bbb3 69 2d 31 5d 2c 20 69 56 61 6c 75 65 29 3b 0a 20  i-1], iValue);. 
1bbb4 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
1bbb5 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75  _leave(p->db->mu
1bbb6 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  tex);.  }.  retu
1bbb7 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f  rn rc;.}.SQLITE_
1bbb8 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1bbb9 62 69 6e 64 5f 6e 75 6c 6c 28 73 71 6c 69 74 65  bind_null(sqlite
1bbba 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69  3_stmt *pStmt, i
1bbbb 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 72 63 3b  nt i){.  int rc;
1bbbc 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64  .  Vdbe *p = (Vd
1bbbd 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 20  be*)pStmt;.  rc 
1bbbe 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20  = vdbeUnbind(p, 
1bbbf 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  i);.  if( rc==SQ
1bbc0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73  LITE_OK ){.    s
1bbc1 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
1bbc2 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  ve(p->db->mutex)
1bbc3 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
1bbc4 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  c;.}.SQLITE_API 
1bbc5 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  int sqlite3_bind
1bbc6 5f 74 65 78 74 28 20 0a 20 20 73 71 6c 69 74 65  _text( .  sqlite
1bbc7 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a  3_stmt *pStmt, .
1bbc8 20 20 69 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73    int i, .  cons
1bbc9 74 20 63 68 61 72 20 2a 7a 44 61 74 61 2c 20 0a  t char *zData, .
1bbca 20 20 69 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20    int nData, .  
1bbcb 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69  void (*xDel)(voi
1bbcc 64 2a 29 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20  d*).){.  return 
1bbcd 62 69 6e 64 54 65 78 74 28 70 53 74 6d 74 2c 20  bindText(pStmt, 
1bbce 69 2c 20 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c  i, zData, nData,
1bbcf 20 78 44 65 6c 2c 20 53 51 4c 49 54 45 5f 55 54   xDel, SQLITE_UT
1bbd0 46 38 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53  F8);.}.#ifndef S
1bbd1 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
1bbd2 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1bbd3 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78  sqlite3_bind_tex
1bbd4 74 31 36 28 0a 20 20 73 71 6c 69 74 65 33 5f 73  t16(.  sqlite3_s
1bbd5 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69  tmt *pStmt, .  i
1bbd6 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20 76  nt i, .  const v
1bbd7 6f 69 64 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69  oid *zData, .  i
1bbd8 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69  nt nData, .  voi
1bbd9 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29  d (*xDel)(void*)
1bbda 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 69 6e  .){.  return bin
1bbdb 64 54 65 78 74 28 70 53 74 6d 74 2c 20 69 2c 20  dText(pStmt, i, 
1bbdc 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 78 44  zData, nData, xD
1bbdd 65 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36  el, SQLITE_UTF16
1bbde 4e 41 54 49 56 45 29 3b 0a 7d 0a 23 65 6e 64 69  NATIVE);.}.#endi
1bbdf 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
1bbe0 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54 45  _UTF16 */.SQLITE
1bbe1 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1bbe2 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73 71 6c 69  _bind_value(sqli
1bbe3 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
1bbe4 20 69 6e 74 20 69 2c 20 63 6f 6e 73 74 20 73 71   int i, const sq
1bbe5 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61  lite3_value *pVa
1bbe6 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  lue){.  int rc;.
1bbe7 20 20 73 77 69 74 63 68 28 20 70 56 61 6c 75 65    switch( pValue
1bbe8 2d 3e 74 79 70 65 20 29 7b 0a 20 20 20 20 63 61  ->type ){.    ca
1bbe9 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45  se SQLITE_INTEGE
1bbea 52 3a 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  R: {.      rc = 
1bbeb 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
1bbec 36 34 28 70 53 74 6d 74 2c 20 69 2c 20 70 56 61  64(pStmt, i, pVa
1bbed 6c 75 65 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 20  lue->u.i);.     
1bbee 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
1bbef 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 4c    case SQLITE_FL
1bbf0 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 72 63 20  OAT: {.      rc 
1bbf1 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64  = sqlite3_bind_d
1bbf2 6f 75 62 6c 65 28 70 53 74 6d 74 2c 20 69 2c 20  ouble(pStmt, i, 
1bbf3 70 56 61 6c 75 65 2d 3e 72 29 3b 0a 20 20 20 20  pValue->r);.    
1bbf4 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
1bbf5 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 42     case SQLITE_B
1bbf6 4c 4f 42 3a 20 7b 0a 20 20 20 20 20 20 69 66 28  LOB: {.      if(
1bbf7 20 70 56 61 6c 75 65 2d 3e 66 6c 61 67 73 20 26   pValue->flags &
1bbf8 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20   MEM_Zero ){.   
1bbf9 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1bbfa 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28  3_bind_zeroblob(
1bbfb 70 53 74 6d 74 2c 20 69 2c 20 70 56 61 6c 75 65  pStmt, i, pValue
1bbfc 2d 3e 75 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20 20  ->u.nZero);.    
1bbfd 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1bbfe 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
1bbff 6e 64 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 69  nd_blob(pStmt, i
1bc00 2c 20 70 56 61 6c 75 65 2d 3e 7a 2c 20 70 56 61  , pValue->z, pVa
1bc01 6c 75 65 2d 3e 6e 2c 53 51 4c 49 54 45 5f 54 52  lue->n,SQLITE_TR
1bc02 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 20 20  ANSIENT);.      
1bc03 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
1bc04 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51     }.    case SQ
1bc05 4c 49 54 45 5f 54 45 58 54 3a 20 7b 0a 20 20 20  LITE_TEXT: {.   
1bc06 20 20 20 72 63 20 3d 20 62 69 6e 64 54 65 78 74     rc = bindText
1bc07 28 70 53 74 6d 74 2c 69 2c 20 20 70 56 61 6c 75  (pStmt,i,  pValu
1bc08 65 2d 3e 7a 2c 20 70 56 61 6c 75 65 2d 3e 6e 2c  e->z, pValue->n,
1bc09 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
1bc0a 54 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  T,.             
1bc0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bc0c 20 70 56 61 6c 75 65 2d 3e 65 6e 63 29 3b 0a 20   pValue->enc);. 
1bc0d 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1bc0e 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b  }.    default: {
1bc0f 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1bc10 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 70 53  te3_bind_null(pS
1bc11 74 6d 74 2c 20 69 29 3b 0a 20 20 20 20 20 20 62  tmt, i);.      b
1bc12 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
1bc13 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53    return rc;.}.S
1bc14 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1bc15 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62  lite3_bind_zerob
1bc16 6c 6f 62 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  lob(sqlite3_stmt
1bc17 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 2c 20   *pStmt, int i, 
1bc18 69 6e 74 20 6e 29 7b 0a 20 20 69 6e 74 20 72 63  int n){.  int rc
1bc19 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56  ;.  Vdbe *p = (V
1bc1a 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72  dbe *)pStmt;.  r
1bc1b 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70  c = vdbeUnbind(p
1bc1c 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  , i);.  if( rc==
1bc1d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1bc1e 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1bc1f 65 74 5a 65 72 6f 42 6c 6f 62 28 26 70 2d 3e 61  etZeroBlob(&p->a
1bc20 56 61 72 5b 69 2d 31 5d 2c 20 6e 29 3b 0a 20 20  Var[i-1], n);.  
1bc21 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1bc22 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74  leave(p->db->mut
1bc23 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ex);.  }.  retur
1bc24 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  n rc;.}../*.** R
1bc25 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
1bc26 20 6f 66 20 77 69 6c 64 63 61 72 64 73 20 74 68   of wildcards th
1bc27 61 74 20 63 61 6e 20 62 65 20 70 6f 74 65 6e 74  at can be potent
1bc28 69 61 6c 6c 79 20 62 6f 75 6e 64 20 74 6f 2e 0a  ially bound to..
1bc29 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
1bc2a 69 73 20 61 64 64 65 64 20 74 6f 20 73 75 70 70  is added to supp
1bc2b 6f 72 74 20 44 42 44 3a 3a 53 51 4c 69 74 65 2e  ort DBD::SQLite.
1bc2c 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49    .*/.SQLITE_API
1bc2d 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
1bc2e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e  d_parameter_coun
1bc2f 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  t(sqlite3_stmt *
1bc30 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a  pStmt){.  Vdbe *
1bc31 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74  p = (Vdbe*)pStmt
1bc32 3b 0a 20 20 72 65 74 75 72 6e 20 70 20 3f 20 70  ;.  return p ? p
1bc33 2d 3e 6e 56 61 72 20 3a 20 30 3b 0a 7d 0a 0a 2f  ->nVar : 0;.}../
1bc34 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6d 61  *.** Create a ma
1bc35 70 70 69 6e 67 20 66 72 6f 6d 20 76 61 72 69 61  pping from varia
1bc36 62 6c 65 20 6e 75 6d 62 65 72 73 20 74 6f 20 76  ble numbers to v
1bc37 61 72 69 61 62 6c 65 20 6e 61 6d 65 73 0a 2a 2a  ariable names.**
1bc38 20 69 6e 20 74 68 65 20 56 64 62 65 2e 61 7a 56   in the Vdbe.azV
1bc39 61 72 5b 5d 20 61 72 72 61 79 2c 20 69 66 20 73  ar[] array, if s
1bc3a 75 63 68 20 61 20 6d 61 70 70 69 6e 67 20 64 6f  uch a mapping do
1bc3b 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 0a 2a  es not already.*
1bc3c 2a 20 65 78 69 73 74 2e 0a 2a 2f 0a 73 74 61 74  * exist..*/.stat
1bc3d 69 63 20 76 6f 69 64 20 63 72 65 61 74 65 56 61  ic void createVa
1bc3e 72 4d 61 70 28 56 64 62 65 20 2a 70 29 7b 0a 20  rMap(Vdbe *p){. 
1bc3f 20 69 66 28 20 21 70 2d 3e 6f 6b 56 61 72 20 29   if( !p->okVar )
1bc40 7b 0a 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20  {.    int j;.   
1bc41 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20 73 71   Op *pOp;.    sq
1bc42 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
1bc43 72 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b  r(p->db->mutex);
1bc44 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 61 63 65  .    /* The race
1bc45 20 63 6f 6e 64 69 74 69 6f 6e 20 68 65 72 65 20   condition here 
1bc46 69 73 20 68 61 72 6d 6c 65 73 73 2e 20 20 49 66  is harmless.  If
1bc47 20 74 77 6f 20 74 68 72 65 61 64 73 20 63 61 6c   two threads cal
1bc48 6c 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 72 6f  l this.    ** ro
1bc49 75 74 69 6e 65 20 6f 6e 20 74 68 65 20 73 61 6d  utine on the sam
1bc4a 65 20 56 64 62 65 20 61 74 20 74 68 65 20 73 61  e Vdbe at the sa
1bc4b 6d 65 20 74 69 6d 65 2c 20 74 68 65 79 20 62 6f  me time, they bo
1bc4c 74 68 20 6d 69 67 68 74 20 65 6e 64 0a 20 20 20  th might end.   
1bc4d 20 2a 2a 20 75 70 20 69 6e 69 74 69 61 6c 69 7a   ** up initializ
1bc4e 69 6e 67 20 74 68 65 20 56 64 62 65 2e 61 7a 56  ing the Vdbe.azV
1bc4f 61 72 5b 5d 20 61 72 72 61 79 2e 20 20 54 68 61  ar[] array.  Tha
1bc50 74 20 69 73 20 61 20 6c 69 74 74 6c 65 20 65 78  t is a little ex
1bc51 74 72 61 0a 20 20 20 20 2a 2a 20 77 6f 72 6b 20  tra.    ** work 
1bc52 62 75 74 20 69 74 20 72 65 73 75 6c 74 73 20 69  but it results i
1bc53 6e 20 74 68 65 20 73 61 6d 65 20 61 6e 73 77 65  n the same answe
1bc54 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f  r..    */.    fo
1bc55 72 28 6a 3d 30 2c 20 70 4f 70 3d 70 2d 3e 61 4f  r(j=0, pOp=p->aO
1bc56 70 3b 20 6a 3c 70 2d 3e 6e 4f 70 3b 20 6a 2b 2b  p; j<p->nOp; j++
1bc57 2c 20 70 4f 70 2b 2b 29 7b 0a 20 20 20 20 20 20  , pOp++){.      
1bc58 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d  if( pOp->opcode=
1bc59 3d 4f 50 5f 56 61 72 69 61 62 6c 65 20 29 7b 0a  =OP_Variable ){.
1bc5a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1bc5b 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70  pOp->p1>0 && pOp
1bc5c 2d 3e 70 31 3c 3d 70 2d 3e 6e 56 61 72 20 29 3b  ->p1<=p->nVar );
1bc5d 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 7a 56 61  .        p->azVa
1bc5e 72 5b 70 4f 70 2d 3e 70 31 2d 31 5d 20 3d 20 70  r[pOp->p1-1] = p
1bc5f 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 20 20  Op->p4.z;.      
1bc60 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 6f  }.    }.    p->o
1bc61 6b 56 61 72 20 3d 20 31 3b 0a 20 20 20 20 73 71  kVar = 1;.    sq
1bc62 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
1bc63 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b  e(p->db->mutex);
1bc64 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
1bc65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66  turn the name of
1bc66 20 61 20 77 69 6c 64 63 61 72 64 20 70 61 72 61   a wildcard para
1bc67 6d 65 74 65 72 2e 20 20 52 65 74 75 72 6e 20 4e  meter.  Return N
1bc68 55 4c 4c 20 69 66 20 74 68 65 20 69 6e 64 65 78  ULL if the index
1bc69 0a 2a 2a 20 69 73 20 6f 75 74 20 6f 66 20 72 61  .** is out of ra
1bc6a 6e 67 65 20 6f 72 20 69 66 20 74 68 65 20 77 69  nge or if the wi
1bc6b 6c 64 63 61 72 64 20 69 73 20 75 6e 6e 61 6d 65  ldcard is unname
1bc6c 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73  d..**.** The res
1bc6d 75 6c 74 20 69 73 20 61 6c 77 61 79 73 20 55 54  ult is always UT
1bc6e 46 2d 38 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  F-8..*/.SQLITE_A
1bc6f 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  PI const char *s
1bc70 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61  qlite3_bind_para
1bc71 6d 65 74 65 72 5f 6e 61 6d 65 28 73 71 6c 69 74  meter_name(sqlit
1bc72 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
1bc73 69 6e 74 20 69 29 7b 0a 20 20 56 64 62 65 20 2a  int i){.  Vdbe *
1bc74 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74  p = (Vdbe*)pStmt
1bc75 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20  ;.  if( p==0 || 
1bc76 69 3c 31 20 7c 7c 20 69 3e 70 2d 3e 6e 56 61 72  i<1 || i>p->nVar
1bc77 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
1bc78 3b 0a 20 20 7d 0a 20 20 63 72 65 61 74 65 56 61  ;.  }.  createVa
1bc79 72 4d 61 70 28 70 29 3b 0a 20 20 72 65 74 75 72  rMap(p);.  retur
1bc7a 6e 20 70 2d 3e 61 7a 56 61 72 5b 69 2d 31 5d 3b  n p->azVar[i-1];
1bc7b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20  .}../*.** Given 
1bc7c 61 20 77 69 6c 64 63 61 72 64 20 70 61 72 61 6d  a wildcard param
1bc7d 65 74 65 72 20 6e 61 6d 65 2c 20 72 65 74 75 72  eter name, retur
1bc7e 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74  n the index of t
1bc7f 68 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 77  he variable.** w
1bc80 69 74 68 20 74 68 61 74 20 6e 61 6d 65 2e 20 20  ith that name.  
1bc81 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 76  If there is no v
1bc82 61 72 69 61 62 6c 65 20 77 69 74 68 20 74 68 65  ariable with the
1bc83 20 67 69 76 65 6e 20 6e 61 6d 65 2c 0a 2a 2a 20   given name,.** 
1bc84 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53 51 4c  return 0..*/.SQL
1bc85 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1bc86 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
1bc87 65 72 5f 69 6e 64 65 78 28 73 71 6c 69 74 65 33  er_index(sqlite3
1bc88 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 63 6f  _stmt *pStmt, co
1bc89 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29  nst char *zName)
1bc8a 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56  {.  Vdbe *p = (V
1bc8b 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 69 6e  dbe*)pStmt;.  in
1bc8c 74 20 69 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20  t i;.  if( p==0 
1bc8d 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
1bc8e 0a 20 20 7d 0a 20 20 63 72 65 61 74 65 56 61 72  .  }.  createVar
1bc8f 4d 61 70 28 70 29 3b 20 0a 20 20 69 66 28 20 7a  Map(p); .  if( z
1bc90 4e 61 6d 65 20 29 7b 0a 20 20 20 20 66 6f 72 28  Name ){.    for(
1bc91 69 3d 30 3b 20 69 3c 70 2d 3e 6e 56 61 72 3b 20  i=0; i<p->nVar; 
1bc92 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73  i++){.      cons
1bc93 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 2d 3e 61  t char *z = p->a
1bc94 7a 56 61 72 5b 69 5d 3b 0a 20 20 20 20 20 20 69  zVar[i];.      i
1bc95 66 28 20 7a 20 26 26 20 73 74 72 63 6d 70 28 7a  f( z && strcmp(z
1bc96 2c 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20  ,zName)==0 ){.  
1bc97 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 2b 31        return i+1
1bc98 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1bc99 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
1bc9a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 66 65  }../*.** Transfe
1bc9b 72 20 61 6c 6c 20 62 69 6e 64 69 6e 67 73 20 66  r all bindings f
1bc9c 72 6f 6d 20 74 68 65 20 66 69 72 73 74 20 73 74  rom the first st
1bc9d 61 74 65 6d 65 6e 74 20 6f 76 65 72 20 74 6f 20  atement over to 
1bc9e 74 68 65 20 73 65 63 6f 6e 64 2e 0a 2a 2f 0a 53  the second..*/.S
1bc9f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1bca0 74 20 73 71 6c 69 74 65 33 54 72 61 6e 73 66 65  t sqlite3Transfe
1bca1 72 42 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 65  rBindings(sqlite
1bca2 33 5f 73 74 6d 74 20 2a 70 46 72 6f 6d 53 74 6d  3_stmt *pFromStm
1bca3 74 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  t, sqlite3_stmt 
1bca4 2a 70 54 6f 53 74 6d 74 29 7b 0a 20 20 56 64 62  *pToStmt){.  Vdb
1bca5 65 20 2a 70 46 72 6f 6d 20 3d 20 28 56 64 62 65  e *pFrom = (Vdbe
1bca6 2a 29 70 46 72 6f 6d 53 74 6d 74 3b 0a 20 20 56  *)pFromStmt;.  V
1bca7 64 62 65 20 2a 70 54 6f 20 3d 20 28 56 64 62 65  dbe *pTo = (Vdbe
1bca8 2a 29 70 54 6f 53 74 6d 74 3b 0a 20 20 69 6e 74  *)pToStmt;.  int
1bca9 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54   i;.  assert( pT
1bcaa 6f 2d 3e 64 62 3d 3d 70 46 72 6f 6d 2d 3e 64 62  o->db==pFrom->db
1bcab 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54   );.  assert( pT
1bcac 6f 2d 3e 6e 56 61 72 3d 3d 70 46 72 6f 6d 2d 3e  o->nVar==pFrom->
1bcad 6e 56 61 72 20 29 3b 0a 20 20 73 71 6c 69 74 65  nVar );.  sqlite
1bcae 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 54  3_mutex_enter(pT
1bcaf 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  o->db->mutex);. 
1bcb0 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 46 72 6f   for(i=0; i<pFro
1bcb1 6d 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b 0a 20  m->nVar; i++){. 
1bcb2 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1bcb3 6d 4d 6f 76 65 28 26 70 54 6f 2d 3e 61 56 61 72  mMove(&pTo->aVar
1bcb4 5b 69 5d 2c 20 26 70 46 72 6f 6d 2d 3e 61 56 61  [i], &pFrom->aVa
1bcb5 72 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  r[i]);.  }.  sql
1bcb6 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
1bcb7 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  (pTo->db->mutex)
1bcb8 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
1bcb9 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66  E_OK;.}..#ifndef
1bcba 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50   SQLITE_OMIT_DEP
1bcbb 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 44 65  RECATED./*.** De
1bcbc 70 72 65 63 61 74 65 64 20 65 78 74 65 72 6e 61  precated externa
1bcbd 6c 20 69 6e 74 65 72 66 61 63 65 2e 20 20 49 6e  l interface.  In
1bcbe 74 65 72 6e 61 6c 2f 63 6f 72 65 20 53 51 4c 69  ternal/core SQLi
1bcbf 74 65 20 63 6f 64 65 0a 2a 2a 20 73 68 6f 75 6c  te code.** shoul
1bcc0 64 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 54 72  d call sqlite3Tr
1bcc1 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 73 2e 0a  ansferBindings..
1bcc2 2a 2a 0a 2a 2a 20 49 73 20 69 73 20 6d 69 73 75  **.** Is is misu
1bcc3 73 65 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20  se to call this 
1bcc4 72 6f 75 74 69 6e 65 20 77 69 74 68 20 73 74 61  routine with sta
1bcc5 74 65 6d 65 6e 74 73 20 66 72 6f 6d 20 64 69 66  tements from dif
1bcc6 66 65 72 65 6e 74 0a 2a 2a 20 64 61 74 61 62 61  ferent.** databa
1bcc7 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20  se connections. 
1bcc8 20 42 75 74 20 61 73 20 74 68 69 73 20 69 73 20   But as this is 
1bcc9 61 20 64 65 70 72 65 63 61 74 65 64 20 69 6e 74  a deprecated int
1bcca 65 72 66 61 63 65 2c 20 77 65 0a 2a 2a 20 77 69  erface, we.** wi
1bccb 6c 6c 20 6e 6f 74 20 62 6f 74 68 65 72 20 74 6f  ll not bother to
1bccc 20 63 68 65 63 6b 20 66 6f 72 20 74 68 61 74 20   check for that 
1bccd 63 6f 6e 64 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  condition..**.**
1bcce 20 49 66 20 74 68 65 20 74 77 6f 20 73 74 61 74   If the two stat
1bccf 65 6d 65 6e 74 73 20 63 6f 6e 74 61 69 6e 20 61  ements contain a
1bcd0 20 64 69 66 66 65 72 65 6e 74 20 6e 75 6d 62 65   different numbe
1bcd1 72 20 6f 66 20 62 69 6e 64 69 6e 67 73 2c 20 74  r of bindings, t
1bcd2 68 65 6e 0a 2a 2a 20 61 6e 20 53 51 4c 49 54 45  hen.** an SQLITE
1bcd3 5f 45 52 52 4f 52 20 69 73 20 72 65 74 75 72 6e  _ERROR is return
1bcd4 65 64 2e 20 20 4e 6f 74 68 69 6e 67 20 65 6c 73  ed.  Nothing els
1bcd5 65 20 63 61 6e 20 67 6f 20 77 72 6f 6e 67 2c 20  e can go wrong, 
1bcd6 73 6f 20 6f 74 68 65 72 77 69 73 65 0a 2a 2a 20  so otherwise.** 
1bcd7 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
1bcd8 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  urned..*/.SQLITE
1bcd9 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1bcda 5f 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69 6e  _transfer_bindin
1bcdb 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  gs(sqlite3_stmt 
1bcdc 2a 70 46 72 6f 6d 53 74 6d 74 2c 20 73 71 6c 69  *pFromStmt, sqli
1bcdd 74 65 33 5f 73 74 6d 74 20 2a 70 54 6f 53 74 6d  te3_stmt *pToStm
1bcde 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 46 72 6f  t){.  Vdbe *pFro
1bcdf 6d 20 3d 20 28 56 64 62 65 2a 29 70 46 72 6f 6d  m = (Vdbe*)pFrom
1bce0 53 74 6d 74 3b 0a 20 20 56 64 62 65 20 2a 70 54  Stmt;.  Vdbe *pT
1bce1 6f 20 3d 20 28 56 64 62 65 2a 29 70 54 6f 53 74  o = (Vdbe*)pToSt
1bce2 6d 74 3b 0a 20 20 69 66 28 20 70 46 72 6f 6d 2d  mt;.  if( pFrom-
1bce3 3e 6e 56 61 72 21 3d 70 54 6f 2d 3e 6e 56 61 72  >nVar!=pTo->nVar
1bce4 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
1bce5 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
1bce6 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
1bce7 33 54 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67  3TransferBinding
1bce8 73 28 70 46 72 6f 6d 53 74 6d 74 2c 20 70 54 6f  s(pFromStmt, pTo
1bce9 53 74 6d 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  Stmt);.}.#endif.
1bcea 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
1bceb 65 20 73 71 6c 69 74 65 33 2a 20 64 61 74 61 62  e sqlite3* datab
1bcec 61 73 65 20 68 61 6e 64 6c 65 20 74 6f 20 77 68  ase handle to wh
1bced 69 63 68 20 74 68 65 20 70 72 65 70 61 72 65 64  ich the prepared
1bcee 20 73 74 61 74 65 6d 65 6e 74 20 67 69 76 65 6e   statement given
1bcef 0a 2a 2a 20 69 6e 20 74 68 65 20 61 72 67 75 6d  .** in the argum
1bcf0 65 6e 74 20 62 65 6c 6f 6e 67 73 2e 20 20 54 68  ent belongs.  Th
1bcf1 69 73 20 69 73 20 74 68 65 20 73 61 6d 65 20 64  is is the same d
1bcf2 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74  atabase handle t
1bcf3 68 61 74 20 77 61 73 0a 2a 2a 20 74 68 65 20 66  hat was.** the f
1bcf4 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
1bcf5 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65   the sqlite3_pre
1bcf6 70 61 72 65 28 29 20 74 68 61 74 20 77 61 73 20  pare() that was 
1bcf7 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 0a 2a  used to create.*
1bcf8 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  * the statement 
1bcf9 69 6e 20 74 68 65 20 66 69 72 73 74 20 70 6c 61  in the first pla
1bcfa 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ce..*/.SQLITE_AP
1bcfb 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74  I sqlite3 *sqlit
1bcfc 65 33 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c  e3_db_handle(sql
1bcfd 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1bcfe 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 53 74 6d  ){.  return pStm
1bcff 74 20 3f 20 28 28 56 64 62 65 2a 29 70 53 74 6d  t ? ((Vdbe*)pStm
1bd00 74 29 2d 3e 64 62 20 3a 20 30 3b 0a 7d 0a 0a 2f  t)->db : 0;.}../
1bd01 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f  *.** Return a po
1bd02 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 78  inter to the nex
1bd03 74 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  t prepared state
1bd04 6d 65 6e 74 20 61 66 74 65 72 20 70 53 74 6d 74  ment after pStmt
1bd05 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77   associated.** w
1bd06 69 74 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ith database con
1bd07 6e 65 63 74 69 6f 6e 20 70 44 62 2e 20 20 49 66  nection pDb.  If
1bd08 20 70 53 74 6d 74 20 69 73 20 4e 55 4c 4c 2c 20   pStmt is NULL, 
1bd09 72 65 74 75 72 6e 20 74 68 65 20 66 69 72 73 74  return the first
1bd0a 0a 2a 2a 20 70 72 65 70 61 72 65 64 20 73 74 61  .** prepared sta
1bd0b 74 65 6d 65 6e 74 20 66 6f 72 20 74 68 65 20 64  tement for the d
1bd0c 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
1bd0d 6f 6e 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c  on.  Return NULL
1bd0e 20 69 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 65   if there.** are
1bd0f 20 6e 6f 20 6d 6f 72 65 2e 0a 2a 2f 0a 53 51 4c   no more..*/.SQL
1bd10 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f  ITE_API sqlite3_
1bd11 73 74 6d 74 20 2a 73 71 6c 69 74 65 33 5f 6e 65  stmt *sqlite3_ne
1bd12 78 74 5f 73 74 6d 74 28 73 71 6c 69 74 65 33 20  xt_stmt(sqlite3 
1bd13 2a 70 44 62 2c 20 73 71 6c 69 74 65 33 5f 73 74  *pDb, sqlite3_st
1bd14 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 73 71  mt *pStmt){.  sq
1bd15 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 4e 65 78  lite3_stmt *pNex
1bd16 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  t;.  sqlite3_mut
1bd17 65 78 5f 65 6e 74 65 72 28 70 44 62 2d 3e 6d 75  ex_enter(pDb->mu
1bd18 74 65 78 29 3b 0a 20 20 69 66 28 20 70 53 74 6d  tex);.  if( pStm
1bd19 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4e 65 78  t==0 ){.    pNex
1bd1a 74 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 6d  t = (sqlite3_stm
1bd1b 74 2a 29 70 44 62 2d 3e 70 56 64 62 65 3b 0a 20  t*)pDb->pVdbe;. 
1bd1c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 78   }else{.    pNex
1bd1d 74 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 6d  t = (sqlite3_stm
1bd1e 74 2a 29 28 28 56 64 62 65 2a 29 70 53 74 6d 74  t*)((Vdbe*)pStmt
1bd1f 29 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20  )->pNext;.  }.  
1bd20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
1bd21 61 76 65 28 70 44 62 2d 3e 6d 75 74 65 78 29 3b  ave(pDb->mutex);
1bd22 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 78 74 3b  .  return pNext;
1bd23 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
1bd24 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20   the value of a 
1bd25 73 74 61 74 75 73 20 63 6f 75 6e 74 65 72 20 66  status counter f
1bd26 6f 72 20 61 20 70 72 65 70 61 72 65 64 20 73 74  or a prepared st
1bd27 61 74 65 6d 65 6e 74 0a 2a 2f 0a 53 51 4c 49 54  atement.*/.SQLIT
1bd28 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1bd29 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 28 73 71  3_stmt_status(sq
1bd2a 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1bd2b 74 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 72  t, int op, int r
1bd2c 65 73 65 74 46 6c 61 67 29 7b 0a 20 20 56 64 62  esetFlag){.  Vdb
1bd2d 65 20 2a 70 56 64 62 65 20 3d 20 28 56 64 62 65  e *pVdbe = (Vdbe
1bd2e 2a 29 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 76  *)pStmt;.  int v
1bd2f 20 3d 20 70 56 64 62 65 2d 3e 61 43 6f 75 6e 74   = pVdbe->aCount
1bd30 65 72 5b 6f 70 2d 31 5d 3b 0a 20 20 69 66 28 20  er[op-1];.  if( 
1bd31 72 65 73 65 74 46 6c 61 67 20 29 20 70 56 64 62  resetFlag ) pVdb
1bd32 65 2d 3e 61 43 6f 75 6e 74 65 72 5b 6f 70 2d 31  e->aCounter[op-1
1bd33 5d 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20  ] = 0;.  return 
1bd34 76 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  v;.}../*********
1bd35 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62  ***** End of vdb
1bd36 65 61 70 69 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eapi.c *********
1bd37 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bd38 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bd39 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
1bd3a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
1bd3b 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   vdbe.c ********
1bd3c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bd3d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bd3e 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31  ****/./*.** 2001
1bd3f 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a   September 15.**
1bd40 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
1bd41 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
1bd42 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
1bd43 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
1bd44 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
1bd45 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
1bd46 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
1bd47 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
1bd48 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
1bd49 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
1bd4a 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
1bd4b 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
1bd4c 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
1bd4d 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
1bd4e 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
1bd4f 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
1bd50 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
1bd51 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
1bd52 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bd53 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bd54 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bd55 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
1bd56 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73  The code in this
1bd57 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73   file implements
1bd58 20 65 78 65 63 75 74 69 6f 6e 20 6d 65 74 68 6f   execution metho
1bd59 64 20 6f 66 20 74 68 65 20 0a 2a 2a 20 56 69 72  d of the .** Vir
1bd5a 74 75 61 6c 20 44 61 74 61 62 61 73 65 20 45 6e  tual Database En
1bd5b 67 69 6e 65 20 28 56 44 42 45 29 2e 20 20 41 20  gine (VDBE).  A 
1bd5c 73 65 70 61 72 61 74 65 20 66 69 6c 65 20 28 22  separate file ("
1bd5d 76 64 62 65 61 75 78 2e 63 22 29 0a 2a 2a 20 68  vdbeaux.c").** h
1bd5e 61 6e 64 6c 65 73 20 68 6f 75 73 65 6b 65 65 70  andles housekeep
1bd5f 69 6e 67 20 64 65 74 61 69 6c 73 20 73 75 63 68  ing details such
1bd60 20 61 73 20 63 72 65 61 74 69 6e 67 20 61 6e 64   as creating and
1bd61 20 64 65 6c 65 74 69 6e 67 0a 2a 2a 20 56 44 42   deleting.** VDB
1bd62 45 20 69 6e 73 74 61 6e 63 65 73 2e 20 20 54 68  E instances.  Th
1bd63 69 73 20 66 69 6c 65 20 69 73 20 73 6f 6c 65 6c  is file is solel
1bd64 79 20 69 6e 74 65 72 65 73 74 65 64 20 69 6e 20  y interested in 
1bd65 65 78 65 63 75 74 69 6e 67 0a 2a 2a 20 74 68 65  executing.** the
1bd66 20 56 44 42 45 20 70 72 6f 67 72 61 6d 2e 0a 2a   VDBE program..*
1bd67 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 65 78 74 65  *.** In the exte
1bd68 72 6e 61 6c 20 69 6e 74 65 72 66 61 63 65 2c 20  rnal interface, 
1bd69 61 6e 20 22 73 71 6c 69 74 65 33 5f 73 74 6d 74  an "sqlite3_stmt
1bd6a 2a 22 20 69 73 20 61 6e 20 6f 70 61 71 75 65 20  *" is an opaque 
1bd6b 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 20  pointer.** to a 
1bd6c 56 44 42 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  VDBE..**.** The 
1bd6d 53 51 4c 20 70 61 72 73 65 72 20 67 65 6e 65 72  SQL parser gener
1bd6e 61 74 65 73 20 61 20 70 72 6f 67 72 61 6d 20 77  ates a program w
1bd6f 68 69 63 68 20 69 73 20 74 68 65 6e 20 65 78 65  hich is then exe
1bd70 63 75 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20  cuted by.** the 
1bd71 56 44 42 45 20 74 6f 20 64 6f 20 74 68 65 20 77  VDBE to do the w
1bd72 6f 72 6b 20 6f 66 20 74 68 65 20 53 51 4c 20 73  ork of the SQL s
1bd73 74 61 74 65 6d 65 6e 74 2e 20 20 56 44 42 45 20  tatement.  VDBE 
1bd74 70 72 6f 67 72 61 6d 73 20 61 72 65 20 0a 2a 2a  programs are .**
1bd75 20 73 69 6d 69 6c 61 72 20 69 6e 20 66 6f 72 6d   similar in form
1bd76 20 74 6f 20 61 73 73 65 6d 62 6c 79 20 6c 61 6e   to assembly lan
1bd77 67 75 61 67 65 2e 20 20 54 68 65 20 70 72 6f 67  guage.  The prog
1bd78 72 61 6d 20 63 6f 6e 73 69 73 74 73 20 6f 66 0a  ram consists of.
1bd79 2a 2a 20 61 20 6c 69 6e 65 61 72 20 73 65 71 75  ** a linear sequ
1bd7a 65 6e 63 65 20 6f 66 20 6f 70 65 72 61 74 69 6f  ence of operatio
1bd7b 6e 73 2e 20 20 45 61 63 68 20 6f 70 65 72 61 74  ns.  Each operat
1bd7c 69 6f 6e 20 68 61 73 20 61 6e 20 6f 70 63 6f 64  ion has an opcod
1bd7d 65 20 0a 2a 2a 20 61 6e 64 20 35 20 6f 70 65 72  e .** and 5 oper
1bd7e 61 6e 64 73 2e 20 20 4f 70 65 72 61 6e 64 73 20  ands.  Operands 
1bd7f 50 31 2c 20 50 32 2c 20 61 6e 64 20 50 33 20 61  P1, P2, and P3 a
1bd80 72 65 20 69 6e 74 65 67 65 72 73 2e 20 20 4f 70  re integers.  Op
1bd81 65 72 61 6e 64 20 50 34 20 0a 2a 2a 20 69 73 20  erand P4 .** is 
1bd82 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65  a null-terminate
1bd83 64 20 73 74 72 69 6e 67 2e 20 20 4f 70 65 72 61  d string.  Opera
1bd84 6e 64 20 50 35 20 69 73 20 61 6e 20 75 6e 73 69  nd P5 is an unsi
1bd85 67 6e 65 64 20 63 68 61 72 61 63 74 65 72 2e 0a  gned character..
1bd86 2a 2a 20 46 65 77 20 6f 70 63 6f 64 65 73 20 75  ** Few opcodes u
1bd87 73 65 20 61 6c 6c 20 35 20 6f 70 65 72 61 6e 64  se all 5 operand
1bd88 73 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 75 74 61  s..**.** Computa
1bd89 74 69 6f 6e 20 72 65 73 75 6c 74 73 20 61 72 65  tion results are
1bd8a 20 73 74 6f 72 65 64 20 6f 6e 20 61 20 73 65 74   stored on a set
1bd8b 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 6e 75   of registers nu
1bd8c 6d 62 65 72 65 64 20 62 65 67 69 6e 6e 69 6e 67  mbered beginning
1bd8d 0a 2a 2a 20 77 69 74 68 20 31 20 61 6e 64 20 67  .** with 1 and g
1bd8e 6f 69 6e 67 20 75 70 20 74 6f 20 56 64 62 65 2e  oing up to Vdbe.
1bd8f 6e 4d 65 6d 2e 20 20 45 61 63 68 20 72 65 67 69  nMem.  Each regi
1bd90 73 74 65 72 20 63 61 6e 20 73 74 6f 72 65 0a 2a  ster can store.*
1bd91 2a 20 65 69 74 68 65 72 20 61 6e 20 69 6e 74 65  * either an inte
1bd92 67 65 72 2c 20 61 20 6e 75 6c 6c 2d 74 65 72 6d  ger, a null-term
1bd93 69 6e 61 74 65 64 20 73 74 72 69 6e 67 2c 20 61  inated string, a
1bd94 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a   floating point.
1bd95 2a 2a 20 6e 75 6d 62 65 72 2c 20 6f 72 20 74 68  ** number, or th
1bd96 65 20 53 51 4c 20 22 4e 55 4c 4c 22 20 76 61 6c  e SQL "NULL" val
1bd97 75 65 2e 20 20 41 6e 20 69 6d 70 6c 69 63 69 74  ue.  An implicit
1bd98 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 72 6f 6d   conversion from
1bd99 20 6f 6e 65 0a 2a 2a 20 74 79 70 65 20 74 6f 20   one.** type to 
1bd9a 74 68 65 20 6f 74 68 65 72 20 6f 63 63 75 72 73  the other occurs
1bd9b 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a   as necessary..*
1bd9c 2a 20 0a 2a 2a 20 4d 6f 73 74 20 6f 66 20 74 68  * .** Most of th
1bd9d 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66  e code in this f
1bd9e 69 6c 65 20 69 73 20 74 61 6b 65 6e 20 75 70 20  ile is taken up 
1bd9f 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 56 64  by the sqlite3Vd
1bda0 62 65 45 78 65 63 28 29 0a 2a 2a 20 66 75 6e 63  beExec().** func
1bda1 74 69 6f 6e 20 77 68 69 63 68 20 64 6f 65 73 20  tion which does 
1bda2 74 68 65 20 77 6f 72 6b 20 6f 66 20 69 6e 74 65  the work of inte
1bda3 72 70 72 65 74 69 6e 67 20 61 20 56 44 42 45 20  rpreting a VDBE 
1bda4 70 72 6f 67 72 61 6d 2e 0a 2a 2a 20 42 75 74 20  program..** But 
1bda5 6f 74 68 65 72 20 72 6f 75 74 69 6e 65 73 20 61  other routines a
1bda6 72 65 20 61 6c 73 6f 20 70 72 6f 76 69 64 65 64  re also provided
1bda7 20 74 6f 20 68 65 6c 70 20 69 6e 20 62 75 69 6c   to help in buil
1bda8 64 69 6e 67 20 75 70 0a 2a 2a 20 61 20 70 72 6f  ding up.** a pro
1bda9 67 72 61 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e  gram instruction
1bdaa 20 62 79 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e   by instruction.
1bdab 0a 2a 2a 0a 2a 2a 20 56 61 72 69 6f 75 73 20 73  .**.** Various s
1bdac 63 72 69 70 74 73 20 73 63 61 6e 20 74 68 69 73  cripts scan this
1bdad 20 73 6f 75 72 63 65 20 66 69 6c 65 20 69 6e 20   source file in 
1bdae 6f 72 64 65 72 20 74 6f 20 67 65 6e 65 72 61 74  order to generat
1bdaf 65 20 48 54 4d 4c 0a 2a 2a 20 64 6f 63 75 6d 65  e HTML.** docume
1bdb0 6e 74 61 74 69 6f 6e 2c 20 68 65 61 64 65 72 73  ntation, headers
1bdb1 20 66 69 6c 65 73 2c 20 6f 72 20 6f 74 68 65 72   files, or other
1bdb2 20 64 65 72 69 76 65 64 20 66 69 6c 65 73 2e 20   derived files. 
1bdb3 20 54 68 65 20 66 6f 72 6d 61 74 74 69 6e 67 0a   The formatting.
1bdb4 2a 2a 20 6f 66 20 74 68 65 20 63 6f 64 65 20 69  ** of the code i
1bdb5 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 2c 20  n this file is, 
1bdb6 74 68 65 72 65 66 6f 72 65 2c 20 69 6d 70 6f 72  therefore, impor
1bdb7 74 61 6e 74 2e 20 20 53 65 65 20 6f 74 68 65 72  tant.  See other
1bdb8 20 63 6f 6d 6d 65 6e 74 73 0a 2a 2a 20 69 6e 20   comments.** in 
1bdb9 74 68 69 73 20 66 69 6c 65 20 66 6f 72 20 64 65  this file for de
1bdba 74 61 69 6c 73 2e 20 20 49 66 20 69 6e 20 64 6f  tails.  If in do
1bdbb 75 62 74 2c 20 64 6f 20 6e 6f 74 20 64 65 76 69  ubt, do not devi
1bdbc 61 74 65 20 66 72 6f 6d 20 65 78 69 73 74 69 6e  ate from existin
1bdbd 67 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 69 6e 67 20  g.** commenting 
1bdbe 61 6e 64 20 69 6e 64 65 6e 74 61 74 69 6f 6e 20  and indentation 
1bdbf 70 72 61 63 74 69 63 65 73 20 77 68 65 6e 20 63  practices when c
1bdc0 68 61 6e 67 69 6e 67 20 6f 72 20 61 64 64 69 6e  hanging or addin
1bdc1 67 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 24 49  g code..**.** $I
1bdc2 64 3a 20 76 64 62 65 2e 63 2c 76 20 31 2e 38 37  d: vdbe.c,v 1.87
1bdc3 34 20 32 30 30 39 2f 30 37 2f 32 34 20 31 37 3a  4 2009/07/24 17:
1bdc4 35 38 3a 35 33 20 64 61 6e 69 65 6c 6b 31 39 37  58:53 danielk197
1bdc5 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a  7 Exp $.*/../*.*
1bdc6 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
1bdc7 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20  global variable 
1bdc8 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 65  is incremented e
1bdc9 76 65 72 79 20 74 69 6d 65 20 61 20 63 75 72 73  very time a curs
1bdca 6f 72 0a 2a 2a 20 6d 6f 76 65 73 2c 20 65 69 74  or.** moves, eit
1bdcb 68 65 72 20 62 79 20 74 68 65 20 4f 50 5f 53 65  her by the OP_Se
1bdcc 65 6b 58 58 2c 20 4f 50 5f 4e 65 78 74 2c 20 6f  ekXX, OP_Next, o
1bdcd 72 20 4f 50 5f 50 72 65 76 20 6f 70 63 6f 64 65  r OP_Prev opcode
1bdce 73 2e 20 20 54 68 65 20 74 65 73 74 0a 2a 2a 20  s.  The test.** 
1bdcf 70 72 6f 63 65 64 75 72 65 73 20 75 73 65 20 74  procedures use t
1bdd0 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  his information 
1bdd1 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61  to make sure tha
1bdd2 74 20 69 6e 64 69 63 65 73 20 61 72 65 0a 2a 2a  t indices are.**
1bdd3 20 77 6f 72 6b 69 6e 67 20 63 6f 72 72 65 63 74   working correct
1bdd4 6c 79 2e 20 20 54 68 69 73 20 76 61 72 69 61 62  ly.  This variab
1bdd5 6c 65 20 68 61 73 20 6e 6f 20 66 75 6e 63 74 69  le has no functi
1bdd6 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 74 6f  on other than to
1bdd7 0a 2a 2a 20 68 65 6c 70 20 76 65 72 69 66 79 20  .** help verify 
1bdd8 74 68 65 20 63 6f 72 72 65 63 74 20 6f 70 65 72  the correct oper
1bdd9 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 62  ation of the lib
1bdda 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  rary..*/.#ifdef 
1bddb 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49  SQLITE_TEST.SQLI
1bddc 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1bddd 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 20  e3_search_count 
1bdde 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  = 0;.#endif../*.
1bddf 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 67 6c 6f  ** When this glo
1bde0 62 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73 20  bal variable is 
1bde1 70 6f 73 69 74 69 76 65 2c 20 69 74 20 67 65 74  positive, it get
1bde2 73 20 64 65 63 72 65 6d 65 6e 74 65 64 20 6f 6e  s decremented on
1bde3 63 65 20 62 65 66 6f 72 65 0a 2a 2a 20 65 61 63  ce before.** eac
1bde4 68 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e  h instruction in
1bde5 20 74 68 65 20 56 44 42 45 2e 20 20 57 68 65 6e   the VDBE.  When
1bde6 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2c 20 74   reaches zero, t
1bde7 68 65 20 75 31 2e 69 73 49 6e 74 65 72 72 75 70  he u1.isInterrup
1bde8 74 65 64 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 20  ted.** field of 
1bde9 74 68 65 20 73 71 6c 69 74 65 33 20 73 74 72 75  the sqlite3 stru
1bdea 63 74 75 72 65 20 69 73 20 73 65 74 20 69 6e 20  cture is set in 
1bdeb 6f 72 64 65 72 20 74 6f 20 73 69 6d 75 6c 61 74  order to simulat
1bdec 65 20 61 6e 64 20 69 6e 74 65 72 72 75 70 74 2e  e and interrupt.
1bded 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 61 63 69  .**.** This faci
1bdee 6c 69 74 79 20 69 73 20 75 73 65 64 20 66 6f 72  lity is used for
1bdef 20 74 65 73 74 69 6e 67 20 70 75 72 70 6f 73 65   testing purpose
1bdf0 73 20 6f 6e 6c 79 2e 20 20 49 74 20 64 6f 65 73  s only.  It does
1bdf1 20 6e 6f 74 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a   not function.**
1bdf2 20 69 6e 20 61 6e 20 6f 72 64 69 6e 61 72 79 20   in an ordinary 
1bdf3 62 75 69 6c 64 2e 0a 2a 2f 0a 23 69 66 64 65 66  build..*/.#ifdef
1bdf4 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
1bdf5 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1bdf6 74 65 33 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f  te3_interrupt_co
1bdf7 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a  unt = 0;.#endif.
1bdf8 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20  ./*.** The next 
1bdf9 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20  global variable 
1bdfa 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 65  is incremented e
1bdfb 61 63 68 20 74 79 70 65 20 74 68 65 20 4f 50 5f  ach type the OP_
1bdfc 53 6f 72 74 20 6f 70 63 6f 64 65 0a 2a 2a 20 69  Sort opcode.** i
1bdfd 73 20 65 78 65 63 75 74 65 64 2e 20 20 54 68 65  s executed.  The
1bdfe 20 74 65 73 74 20 70 72 6f 63 65 64 75 72 65 73   test procedures
1bdff 20 75 73 65 20 74 68 69 73 20 69 6e 66 6f 72 6d   use this inform
1be00 61 74 69 6f 6e 20 74 6f 20 6d 61 6b 65 20 73 75  ation to make su
1be01 72 65 20 74 68 61 74 0a 2a 2a 20 73 6f 72 74 69  re that.** sorti
1be02 6e 67 20 69 73 20 6f 63 63 75 72 72 69 6e 67 20  ng is occurring 
1be03 6f 72 20 6e 6f 74 20 6f 63 63 75 72 72 69 6e 67  or not occurring
1be04 20 61 74 20 61 70 70 72 6f 70 72 69 61 74 65 20   at appropriate 
1be05 74 69 6d 65 73 2e 20 20 20 54 68 69 73 20 76 61  times.   This va
1be06 72 69 61 62 6c 65 0a 2a 2a 20 68 61 73 20 6e 6f  riable.** has no
1be07 20 66 75 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20   function other 
1be08 74 68 61 6e 20 74 6f 20 68 65 6c 70 20 76 65 72  than to help ver
1be09 69 66 79 20 74 68 65 20 63 6f 72 72 65 63 74 20  ify the correct 
1be0a 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65  operation of the
1be0b 0a 2a 2a 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a  .** library..*/.
1be0c 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
1be0d 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  ST.SQLITE_API in
1be0e 74 20 73 71 6c 69 74 65 33 5f 73 6f 72 74 5f 63  t sqlite3_sort_c
1be0f 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66  ount = 0;.#endif
1be10 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74  ../*.** The next
1be11 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65   global variable
1be12 20 72 65 63 6f 72 64 73 20 74 68 65 20 73 69 7a   records the siz
1be13 65 20 6f 66 20 74 68 65 20 6c 61 72 67 65 73 74  e of the largest
1be14 20 4d 45 4d 5f 42 6c 6f 62 0a 2a 2a 20 6f 72 20   MEM_Blob.** or 
1be15 4d 45 4d 5f 53 74 72 20 74 68 61 74 20 68 61 73  MEM_Str that has
1be16 20 62 65 65 6e 20 75 73 65 64 20 62 79 20 61 20   been used by a 
1be17 56 44 42 45 20 6f 70 63 6f 64 65 2e 20 20 54 68  VDBE opcode.  Th
1be18 65 20 74 65 73 74 20 70 72 6f 63 65 64 75 72 65  e test procedure
1be19 73 0a 2a 2a 20 75 73 65 20 74 68 69 73 20 69 6e  s.** use this in
1be1a 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 6b  formation to mak
1be1b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20  e sure that the 
1be1c 7a 65 72 6f 2d 62 6c 6f 62 20 66 75 6e 63 74 69  zero-blob functi
1be1d 6f 6e 61 6c 69 74 79 0a 2a 2a 20 69 73 20 77 6f  onality.** is wo
1be1e 72 6b 69 6e 67 20 63 6f 72 72 65 63 74 6c 79 2e  rking correctly.
1be1f 20 20 20 54 68 69 73 20 76 61 72 69 61 62 6c 65     This variable
1be20 20 68 61 73 20 6e 6f 20 66 75 6e 63 74 69 6f 6e   has no function
1be21 20 6f 74 68 65 72 20 74 68 61 6e 20 74 6f 0a 2a   other than to.*
1be22 2a 20 68 65 6c 70 20 76 65 72 69 66 79 20 74 68  * help verify th
1be23 65 20 63 6f 72 72 65 63 74 20 6f 70 65 72 61 74  e correct operat
1be24 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 62 72 61  ion of the libra
1be25 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ry..*/.#ifdef SQ
1be26 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
1be27 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1be28 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20 3d 20  _max_blobsize = 
1be29 30 3b 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75  0;.static void u
1be2a 70 64 61 74 65 4d 61 78 42 6c 6f 62 73 69 7a 65  pdateMaxBlobsize
1be2b 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20  (Mem *p){.  if( 
1be2c 28 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d  (p->flags & (MEM
1be2d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 21  _Str|MEM_Blob))!
1be2e 3d 30 20 26 26 20 70 2d 3e 6e 3e 73 71 6c 69 74  =0 && p->n>sqlit
1be2f 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20  e3_max_blobsize 
1be30 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  ){.    sqlite3_m
1be31 61 78 5f 62 6c 6f 62 73 69 7a 65 20 3d 20 70 2d  ax_blobsize = p-
1be32 3e 6e 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66  >n;.  }.}.#endif
1be33 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 61 20 72  ../*.** Test a r
1be34 65 67 69 73 74 65 72 20 74 6f 20 73 65 65 20 69  egister to see i
1be35 66 20 69 74 20 65 78 63 65 65 64 73 20 74 68 65  f it exceeds the
1be36 20 63 75 72 72 65 6e 74 20 6d 61 78 69 6d 75 6d   current maximum
1be37 20 62 6c 6f 62 20 73 69 7a 65 2e 0a 2a 2a 20 49   blob size..** I
1be38 66 20 69 74 20 64 6f 65 73 2c 20 72 65 63 6f 72  f it does, recor
1be39 64 20 74 68 65 20 6e 65 77 20 6d 61 78 69 6d 75  d the new maximu
1be3a 6d 20 62 6c 6f 62 20 73 69 7a 65 2e 0a 2a 2f 0a  m blob size..*/.
1be3b 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
1be3c 54 45 5f 54 45 53 54 29 20 26 26 20 21 64 65 66  TE_TEST) && !def
1be3d 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
1be3e 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 29 0a 23  _BUILTIN_TEST).#
1be3f 20 64 65 66 69 6e 65 20 55 50 44 41 54 45 5f 4d   define UPDATE_M
1be40 41 58 5f 42 4c 4f 42 53 49 5a 45 28 50 29 20 20  AX_BLOBSIZE(P)  
1be41 75 70 64 61 74 65 4d 61 78 42 6c 6f 62 73 69 7a  updateMaxBlobsiz
1be42 65 28 50 29 0a 23 65 6c 73 65 0a 23 20 64 65 66  e(P).#else.# def
1be43 69 6e 65 20 55 50 44 41 54 45 5f 4d 41 58 5f 42  ine UPDATE_MAX_B
1be44 4c 4f 42 53 49 5a 45 28 50 29 0a 23 65 6e 64 69  LOBSIZE(P).#endi
1be45 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74  f../*.** Convert
1be46 20 74 68 65 20 67 69 76 65 6e 20 72 65 67 69 73   the given regis
1be47 74 65 72 20 69 6e 74 6f 20 61 20 73 74 72 69 6e  ter into a strin
1be48 67 20 69 66 20 69 74 20 69 73 6e 27 74 20 6f 6e  g if it isn't on
1be49 65 0a 2a 2a 20 61 6c 72 65 61 64 79 2e 20 52 65  e.** already. Re
1be4a 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66  turn non-zero if
1be4b 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c   a malloc() fail
1be4c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 74  s..*/.#define St
1be4d 72 69 6e 67 69 66 79 28 50 2c 20 65 6e 63 29 20  ringify(P, enc) 
1be4e 5c 0a 20 20 20 69 66 28 28 28 50 29 2d 3e 66 6c  \.   if(((P)->fl
1be4f 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d  ags&(MEM_Str|MEM
1be50 5f 42 6c 6f 62 29 29 3d 3d 30 20 26 26 20 73 71  _Blob))==0 && sq
1be51 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69  lite3VdbeMemStri
1be52 6e 67 69 66 79 28 50 2c 65 6e 63 29 29 20 5c 0a  ngify(P,enc)) \.
1be53 20 20 20 20 20 7b 20 67 6f 74 6f 20 6e 6f 5f 6d       { goto no_m
1be54 65 6d 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  em; }../*.** An 
1be55 65 70 68 65 6d 65 72 61 6c 20 73 74 72 69 6e 67  ephemeral string
1be56 20 76 61 6c 75 65 20 28 73 69 67 6e 69 66 69 65   value (signifie
1be57 64 20 62 79 20 74 68 65 20 4d 45 4d 5f 45 70 68  d by the MEM_Eph
1be58 65 6d 20 66 6c 61 67 29 20 63 6f 6e 74 61 69 6e  em flag) contain
1be59 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74  s.** a pointer t
1be5a 6f 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20  o a dynamically 
1be5b 61 6c 6c 6f 63 61 74 65 64 20 73 74 72 69 6e 67  allocated string
1be5c 20 77 68 65 72 65 20 73 6f 6d 65 20 6f 74 68 65   where some othe
1be5d 72 20 65 6e 74 69 74 79 0a 2a 2a 20 69 73 20 72  r entity.** is r
1be5e 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 64  esponsible for d
1be5f 65 61 6c 6c 6f 63 61 74 69 6e 67 20 74 68 61 74  eallocating that
1be60 20 73 74 72 69 6e 67 2e 20 20 42 65 63 61 75 73   string.  Becaus
1be61 65 20 74 68 65 20 72 65 67 69 73 74 65 72 0a 2a  e the register.*
1be62 2a 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 72  * does not contr
1be63 6f 6c 20 74 68 65 20 73 74 72 69 6e 67 2c 20 69  ol the string, i
1be64 74 20 6d 69 67 68 74 20 62 65 20 64 65 6c 65 74  t might be delet
1be65 65 64 20 77 69 74 68 6f 75 74 20 74 68 65 20 72  ed without the r
1be66 65 67 69 73 74 65 72 0a 2a 2a 20 6b 6e 6f 77 69  egister.** knowi
1be67 6e 67 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ng it..**.** Thi
1be68 73 20 72 6f 75 74 69 6e 65 20 63 6f 6e 76 65 72  s routine conver
1be69 74 73 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20  ts an ephemeral 
1be6a 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 64 79  string into a dy
1be6b 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61  namically alloca
1be6c 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 74 68  ted.** string th
1be6d 61 74 20 74 68 65 20 72 65 67 69 73 74 65 72 20  at the register 
1be6e 69 74 73 65 6c 66 20 63 6f 6e 74 72 6f 6c 73 2e  itself controls.
1be6f 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73    In other words
1be70 2c 20 69 74 0a 2a 2a 20 63 6f 6e 76 65 72 74 73  , it.** converts
1be71 20 61 6e 20 4d 45 4d 5f 45 70 68 65 6d 20 73 74   an MEM_Ephem st
1be72 72 69 6e 67 20 69 6e 74 6f 20 61 6e 20 4d 45 4d  ring into an MEM
1be73 5f 44 79 6e 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a  _Dyn string..*/.
1be74 23 64 65 66 69 6e 65 20 44 65 65 70 68 65 6d 65  #define Deepheme
1be75 72 61 6c 69 7a 65 28 50 29 20 5c 0a 20 20 20 69  ralize(P) \.   i
1be76 66 28 20 28 28 50 29 2d 3e 66 6c 61 67 73 26 4d  f( ((P)->flags&M
1be77 45 4d 5f 45 70 68 65 6d 29 21 3d 30 20 5c 0a 20  EM_Ephem)!=0 \. 
1be78 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33        && sqlite3
1be79 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65  VdbeMemMakeWrite
1be7a 61 62 6c 65 28 50 29 20 29 7b 20 67 6f 74 6f 20  able(P) ){ goto 
1be7b 6e 6f 5f 6d 65 6d 3b 7d 0a 0a 2f 2a 0a 2a 2a 20  no_mem;}../*.** 
1be7c 43 61 6c 6c 20 73 71 6c 69 74 65 33 56 64 62 65  Call sqlite3Vdbe
1be7d 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 29 20  MemExpandBlob() 
1be7e 6f 6e 20 74 68 65 20 73 75 70 70 6c 69 65 64 20  on the supplied 
1be7f 76 61 6c 75 65 20 28 74 79 70 65 20 4d 65 6d 2a  value (type Mem*
1be80 29 0a 2a 2a 20 50 20 69 66 20 72 65 71 75 69 72  ).** P if requir
1be81 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45  ed..*/.#define E
1be82 78 70 61 6e 64 42 6c 6f 62 28 50 29 20 28 28 28  xpandBlob(P) (((
1be83 50 29 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 5a 65  P)->flags&MEM_Ze
1be84 72 6f 29 3f 73 71 6c 69 74 65 33 56 64 62 65 4d  ro)?sqlite3VdbeM
1be85 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 50 29 3a  emExpandBlob(P):
1be86 30 29 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65  0)../*.** Argume
1be87 6e 74 20 70 4d 65 6d 20 70 6f 69 6e 74 73 20 61  nt pMem points a
1be88 74 20 61 20 72 65 67 69 73 74 65 72 20 74 68 61  t a register tha
1be89 74 20 77 69 6c 6c 20 62 65 20 70 61 73 73 65 64  t will be passed
1be8a 20 74 6f 20 61 0a 2a 2a 20 75 73 65 72 2d 64 65   to a.** user-de
1be8b 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 6f  fined function o
1be8c 72 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68  r returned to th
1be8d 65 20 75 73 65 72 20 61 73 20 74 68 65 20 72 65  e user as the re
1be8e 73 75 6c 74 20 6f 66 20 61 20 71 75 65 72 79 2e  sult of a query.
1be8f 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61  .** The second a
1be90 72 67 75 6d 65 6e 74 2c 20 27 64 62 5f 65 6e 63  rgument, 'db_enc
1be91 27 20 69 73 20 74 68 65 20 74 65 78 74 20 65 6e  ' is the text en
1be92 63 6f 64 69 6e 67 20 75 73 65 64 20 62 79 20 74  coding used by t
1be93 68 65 20 76 64 62 65 20 66 6f 72 0a 2a 2a 20 72  he vdbe for.** r
1be94 65 67 69 73 74 65 72 20 76 61 72 69 61 62 6c 65  egister variable
1be95 73 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  s.  This routine
1be96 20 73 65 74 73 20 74 68 65 20 70 4d 65 6d 2d 3e   sets the pMem->
1be97 65 6e 63 20 61 6e 64 20 70 4d 65 6d 2d 3e 74 79  enc and pMem->ty
1be98 70 65 0a 2a 2a 20 76 61 72 69 61 62 6c 65 73 20  pe.** variables 
1be99 75 73 65 64 20 62 79 20 74 68 65 20 73 71 6c 69  used by the sqli
1be9a 74 65 33 5f 76 61 6c 75 65 5f 2a 28 29 20 72 6f  te3_value_*() ro
1be9b 75 74 69 6e 65 73 2e 0a 2a 2f 0a 23 64 65 66 69  utines..*/.#defi
1be9c 6e 65 20 73 74 6f 72 65 54 79 70 65 49 6e 66 6f  ne storeTypeInfo
1be9d 28 41 2c 42 29 20 5f 73 74 6f 72 65 54 79 70 65  (A,B) _storeType
1be9e 49 6e 66 6f 28 41 29 0a 73 74 61 74 69 63 20 76  Info(A).static v
1be9f 6f 69 64 20 5f 73 74 6f 72 65 54 79 70 65 49 6e  oid _storeTypeIn
1bea0 66 6f 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20  fo(Mem *pMem){. 
1bea1 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 70 4d 65   int flags = pMe
1bea2 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 28 20  m->flags;.  if( 
1bea3 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c  flags & MEM_Null
1bea4 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79   ){.    pMem->ty
1bea5 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c  pe = SQLITE_NULL
1bea6 3b 0a 20 20 7d 0a 20 20 65 6c 73 65 20 69 66 28  ;.  }.  else if(
1bea7 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74   flags & MEM_Int
1bea8 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79   ){.    pMem->ty
1bea9 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45  pe = SQLITE_INTE
1beaa 47 45 52 3b 0a 20 20 7d 0a 20 20 65 6c 73 65 20  GER;.  }.  else 
1beab 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f  if( flags & MEM_
1beac 52 65 61 6c 20 29 7b 0a 20 20 20 20 70 4d 65 6d  Real ){.    pMem
1bead 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f  ->type = SQLITE_
1beae 46 4c 4f 41 54 3b 0a 20 20 7d 0a 20 20 65 6c 73  FLOAT;.  }.  els
1beaf 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45  e if( flags & ME
1beb0 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 70 4d 65  M_Str ){.    pMe
1beb1 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45  m->type = SQLITE
1beb2 5f 54 45 58 54 3b 0a 20 20 7d 65 6c 73 65 7b 0a  _TEXT;.  }else{.
1beb3 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d      pMem->type =
1beb4 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3b 0a 20 20   SQLITE_BLOB;.  
1beb5 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 70 65  }.}../*.** Prope
1beb6 72 74 69 65 73 20 6f 66 20 6f 70 63 6f 64 65 73  rties of opcodes
1beb7 2e 20 20 54 68 65 20 4f 50 46 4c 47 5f 49 4e 49  .  The OPFLG_INI
1beb8 54 49 41 4c 49 5a 45 52 20 6d 61 63 72 6f 20 69  TIALIZER macro i
1beb9 73 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20  s.** created by 
1beba 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 6b 20 64 75  mkopcodeh.awk du
1bebb 72 69 6e 67 20 63 6f 6d 70 69 6c 61 74 69 6f 6e  ring compilation
1bebc 2e 20 20 44 61 74 61 20 69 73 20 6f 62 74 61 69  .  Data is obtai
1bebd 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20  ned.** from the 
1bebe 63 6f 6d 6d 65 6e 74 73 20 66 6f 6c 6c 6f 77 69  comments followi
1bebf 6e 67 20 74 68 65 20 22 63 61 73 65 20 4f 50 5f  ng the "case OP_
1bec0 78 78 78 78 3a 22 20 73 74 61 74 65 6d 65 6e 74  xxxx:" statement
1bec1 73 20 69 6e 0a 2a 2a 20 74 68 69 73 20 66 69 6c  s in.** this fil
1bec2 65 2e 20 20 0a 2a 2f 0a 73 74 61 74 69 63 20 63  e.  .*/.static c
1bec3 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
1bec4 61 72 20 6f 70 63 6f 64 65 50 72 6f 70 65 72 74  ar opcodePropert
1bec5 79 5b 5d 20 3d 20 4f 50 46 4c 47 5f 49 4e 49 54  y[] = OPFLG_INIT
1bec6 49 41 4c 49 5a 45 52 3b 0a 0a 2f 2a 0a 2a 2a 20  IALIZER;../*.** 
1bec7 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 61  Return true if a
1bec8 6e 20 6f 70 63 6f 64 65 20 68 61 73 20 61 6e 79  n opcode has any
1bec9 20 6f 66 20 74 68 65 20 4f 50 46 4c 47 5f 78 78   of the OPFLG_xx
1beca 78 20 70 72 6f 70 65 72 74 69 65 73 0a 2a 2a 20  x properties.** 
1becb 73 70 65 63 69 66 69 65 64 20 62 79 20 6d 61 73  specified by mas
1becc 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  k..*/.SQLITE_PRI
1becd 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1bece 56 64 62 65 4f 70 63 6f 64 65 48 61 73 50 72 6f  VdbeOpcodeHasPro
1becf 70 65 72 74 79 28 69 6e 74 20 6f 70 63 6f 64 65  perty(int opcode
1bed0 2c 20 69 6e 74 20 6d 61 73 6b 29 7b 0a 20 20 61  , int mask){.  a
1bed1 73 73 65 72 74 28 20 6f 70 63 6f 64 65 3e 30 20  ssert( opcode>0 
1bed2 26 26 20 6f 70 63 6f 64 65 3c 28 69 6e 74 29 73  && opcode<(int)s
1bed3 69 7a 65 6f 66 28 6f 70 63 6f 64 65 50 72 6f 70  izeof(opcodeProp
1bed4 65 72 74 79 29 20 29 3b 0a 20 20 72 65 74 75 72  erty) );.  retur
1bed5 6e 20 28 6f 70 63 6f 64 65 50 72 6f 70 65 72 74  n (opcodePropert
1bed6 79 5b 6f 70 63 6f 64 65 5d 26 6d 61 73 6b 29 21  y[opcode]&mask)!
1bed7 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c  =0;.}../*.** All
1bed8 6f 63 61 74 65 20 56 64 62 65 43 75 72 73 6f 72  ocate VdbeCursor
1bed9 20 6e 75 6d 62 65 72 20 69 43 75 72 2e 20 20 52   number iCur.  R
1beda 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
1bedb 74 6f 20 69 74 2e 20 20 52 65 74 75 72 6e 20 4e  to it.  Return N
1bedc 55 4c 4c 0a 2a 2a 20 69 66 20 77 65 20 72 75 6e  ULL.** if we run
1bedd 20 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a   out of memory..
1bede 2a 2f 0a 73 74 61 74 69 63 20 56 64 62 65 43 75  */.static VdbeCu
1bedf 72 73 6f 72 20 2a 61 6c 6c 6f 63 61 74 65 43 75  rsor *allocateCu
1bee0 72 73 6f 72 28 0a 20 20 56 64 62 65 20 2a 70 2c  rsor(.  Vdbe *p,
1bee1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1bee2 20 54 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63   The virtual mac
1bee3 68 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 69 43  hine */.  int iC
1bee4 75 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ur,             
1bee5 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20  /* Index of the 
1bee6 6e 65 77 20 56 64 62 65 43 75 72 73 6f 72 20 2a  new VdbeCursor *
1bee7 2f 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 2c 20  /.  int nField, 
1bee8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
1bee9 62 65 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e  ber of fields in
1beea 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e   the table or in
1beeb 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62  dex */.  int iDb
1beec 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
1beed 2a 20 57 68 65 6e 20 64 61 74 61 62 61 73 65 20  * When database 
1beee 74 68 65 20 63 75 72 73 6f 72 20 62 65 6c 6f 6e  the cursor belon
1beef 67 73 20 74 6f 2c 20 6f 72 20 2d 31 20 2a 2f 0a  gs to, or -1 */.
1bef0 20 20 69 6e 74 20 69 73 42 74 72 65 65 43 75 72    int isBtreeCur
1bef1 73 6f 72 20 20 20 20 20 2f 2a 20 54 72 75 65 20  sor     /* True 
1bef2 66 6f 72 20 42 2d 54 72 65 65 2e 20 20 46 61 6c  for B-Tree.  Fal
1bef3 73 65 20 66 6f 72 20 70 73 65 75 64 6f 2d 74 61  se for pseudo-ta
1bef4 62 6c 65 20 6f 72 20 76 74 61 62 20 2a 2f 0a 29  ble or vtab */.)
1bef5 7b 0a 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20  {.  /* Find the 
1bef6 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68 61 74  memory cell that
1bef7 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 74 6f   will be used to
1bef8 20 73 74 6f 72 65 20 74 68 65 20 62 6c 6f 62 20   store the blob 
1bef9 6f 66 20 6d 65 6d 6f 72 79 0a 20 20 2a 2a 20 72  of memory.  ** r
1befa 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 69 73  equired for this
1befb 20 56 64 62 65 43 75 72 73 6f 72 20 73 74 72 75   VdbeCursor stru
1befc 63 74 75 72 65 2e 20 49 74 20 69 73 20 63 6f 6e  cture. It is con
1befd 76 65 6e 69 65 6e 74 20 74 6f 20 75 73 65 20 61  venient to use a
1befe 20 0a 20 20 2a 2a 20 76 64 62 65 20 6d 65 6d 6f   .  ** vdbe memo
1beff 72 79 20 63 65 6c 6c 20 74 6f 20 6d 61 6e 61 67  ry cell to manag
1bf00 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  e the memory all
1bf01 6f 63 61 74 69 6f 6e 20 72 65 71 75 69 72 65 64  ocation required
1bf02 20 66 6f 72 20 61 0a 20 20 2a 2a 20 56 64 62 65   for a.  ** Vdbe
1bf03 43 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 65  Cursor structure
1bf04 20 66 6f 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69   for the followi
1bf05 6e 67 20 72 65 61 73 6f 6e 73 3a 0a 20 20 2a 2a  ng reasons:.  **
1bf06 0a 20 20 2a 2a 20 20 20 2a 20 53 6f 6d 65 74 69  .  **   * Someti
1bf07 6d 65 73 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  mes cursor numbe
1bf08 72 73 20 61 72 65 20 75 73 65 64 20 66 6f 72 20  rs are used for 
1bf09 61 20 63 6f 75 70 6c 65 20 6f 66 20 64 69 66 66  a couple of diff
1bf0a 65 72 65 6e 74 0a 20 20 2a 2a 20 20 20 20 20 70  erent.  **     p
1bf0b 75 72 70 6f 73 65 73 20 69 6e 20 61 20 76 64 62  urposes in a vdb
1bf0c 65 20 70 72 6f 67 72 61 6d 2e 20 54 68 65 20 64  e program. The d
1bf0d 69 66 66 65 72 65 6e 74 20 75 73 65 73 20 6d 69  ifferent uses mi
1bf0e 67 68 74 20 72 65 71 75 69 72 65 0a 20 20 2a 2a  ght require.  **
1bf0f 20 20 20 20 20 64 69 66 66 65 72 65 6e 74 20 73       different s
1bf10 69 7a 65 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  ized allocations
1bf11 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 70  . Memory cells p
1bf12 72 6f 76 69 64 65 20 67 72 6f 77 61 62 6c 65 0a  rovide growable.
1bf13 20 20 2a 2a 20 20 20 20 20 61 6c 6c 6f 63 61 74    **     allocat
1bf14 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ions..  **.  ** 
1bf15 20 20 2a 20 57 68 65 6e 20 75 73 69 6e 67 20 45    * When using E
1bf16 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e  NABLE_MEMORY_MAN
1bf17 41 47 45 4d 45 4e 54 2c 20 6d 65 6d 6f 72 79 20  AGEMENT, memory 
1bf18 63 65 6c 6c 20 62 75 66 66 65 72 73 20 63 61 6e  cell buffers can
1bf19 0a 20 20 2a 2a 20 20 20 20 20 62 65 20 66 72 65  .  **     be fre
1bf1a 65 64 20 6c 61 7a 69 6c 79 20 76 69 61 20 74 68  ed lazily via th
1bf1b 65 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73  e sqlite3_releas
1bf1c 65 5f 6d 65 6d 6f 72 79 28 29 20 41 50 49 2e 20  e_memory() API. 
1bf1d 54 68 69 73 0a 20 20 2a 2a 20 20 20 20 20 6d 69  This.  **     mi
1bf1e 6e 69 6d 69 7a 65 73 20 74 68 65 20 6e 75 6d 62  nimizes the numb
1bf1f 65 72 20 6f 66 20 6d 61 6c 6c 6f 63 20 63 61 6c  er of malloc cal
1bf20 6c 73 20 6d 61 64 65 20 62 79 20 74 68 65 20 73  ls made by the s
1bf21 79 73 74 65 6d 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  ystem..  **.  **
1bf22 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 66 6f   Memory cells fo
1bf23 72 20 63 75 72 73 6f 72 73 20 61 72 65 20 61 6c  r cursors are al
1bf24 6c 6f 63 61 74 65 64 20 61 74 20 74 68 65 20 74  located at the t
1bf25 6f 70 20 6f 66 20 74 68 65 20 61 64 64 72 65 73  op of the addres
1bf26 73 0a 20 20 2a 2a 20 73 70 61 63 65 2e 20 4d 65  s.  ** space. Me
1bf27 6d 6f 72 79 20 63 65 6c 6c 20 28 70 2d 3e 6e 4d  mory cell (p->nM
1bf28 65 6d 29 20 63 6f 72 72 65 73 70 6f 6e 64 73 20  em) corresponds 
1bf29 74 6f 20 63 75 72 73 6f 72 20 30 2e 20 53 70 61  to cursor 0. Spa
1bf2a 63 65 20 66 6f 72 0a 20 20 2a 2a 20 63 75 72 73  ce for.  ** curs
1bf2b 6f 72 20 31 20 69 73 20 6d 61 6e 61 67 65 64 20  or 1 is managed 
1bf2c 62 79 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 28  by memory cell (
1bf2d 70 2d 3e 6e 4d 65 6d 2d 31 29 2c 20 65 74 63 2e  p->nMem-1), etc.
1bf2e 0a 20 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65  .  */.  Mem *pMe
1bf2f 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 2d 3e  m = &p->aMem[p->
1bf30 6e 4d 65 6d 2d 69 43 75 72 5d 3b 0a 0a 20 20 69  nMem-iCur];..  i
1bf31 6e 74 20 6e 42 79 74 65 3b 0a 20 20 56 64 62 65  nt nByte;.  Vdbe
1bf32 43 75 72 73 6f 72 20 2a 70 43 78 20 3d 20 30 3b  Cursor *pCx = 0;
1bf33 0a 20 20 6e 42 79 74 65 20 3d 20 0a 20 20 20 20  .  nByte = .    
1bf34 20 20 73 69 7a 65 6f 66 28 56 64 62 65 43 75 72    sizeof(VdbeCur
1bf35 73 6f 72 29 20 2b 20 0a 20 20 20 20 20 20 28 69  sor) + .      (i
1bf36 73 42 74 72 65 65 43 75 72 73 6f 72 3f 73 71 6c  sBtreeCursor?sql
1bf37 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 53  ite3BtreeCursorS
1bf38 69 7a 65 28 29 3a 30 29 20 2b 20 0a 20 20 20 20  ize():0) + .    
1bf39 20 20 32 2a 6e 46 69 65 6c 64 2a 73 69 7a 65 6f    2*nField*sizeo
1bf3a 66 28 75 33 32 29 3b 0a 0a 20 20 61 73 73 65 72  f(u32);..  asser
1bf3b 74 28 20 69 43 75 72 3c 70 2d 3e 6e 43 75 72 73  t( iCur<p->nCurs
1bf3c 6f 72 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61  or );.  if( p->a
1bf3d 70 43 73 72 5b 69 43 75 72 5d 20 29 7b 0a 20 20  pCsr[iCur] ){.  
1bf3e 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 65    sqlite3VdbeFre
1bf3f 65 43 75 72 73 6f 72 28 70 2c 20 70 2d 3e 61 70  eCursor(p, p->ap
1bf40 43 73 72 5b 69 43 75 72 5d 29 3b 0a 20 20 20 20  Csr[iCur]);.    
1bf41 70 2d 3e 61 70 43 73 72 5b 69 43 75 72 5d 20 3d  p->apCsr[iCur] =
1bf42 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 53 51   0;.  }.  if( SQ
1bf43 4c 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33  LITE_OK==sqlite3
1bf44 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d  VdbeMemGrow(pMem
1bf45 2c 20 6e 42 79 74 65 2c 20 30 29 20 29 7b 0a 20  , nByte, 0) ){. 
1bf46 20 20 20 70 2d 3e 61 70 43 73 72 5b 69 43 75 72     p->apCsr[iCur
1bf47 5d 20 3d 20 70 43 78 20 3d 20 28 56 64 62 65 43  ] = pCx = (VdbeC
1bf48 75 72 73 6f 72 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a  ursor*)pMem->z;.
1bf49 20 20 20 20 6d 65 6d 73 65 74 28 70 4d 65 6d 2d      memset(pMem-
1bf4a 3e 7a 2c 20 30 2c 20 6e 42 79 74 65 29 3b 0a 20  >z, 0, nByte);. 
1bf4b 20 20 20 70 43 78 2d 3e 69 44 62 20 3d 20 69 44     pCx->iDb = iD
1bf4c 62 3b 0a 20 20 20 20 70 43 78 2d 3e 6e 46 69 65  b;.    pCx->nFie
1bf4d 6c 64 20 3d 20 6e 46 69 65 6c 64 3b 0a 20 20 20  ld = nField;.   
1bf4e 20 69 66 28 20 6e 46 69 65 6c 64 20 29 7b 0a 20   if( nField ){. 
1bf4f 20 20 20 20 20 70 43 78 2d 3e 61 54 79 70 65 20       pCx->aType 
1bf50 3d 20 28 75 33 32 20 2a 29 26 70 4d 65 6d 2d 3e  = (u32 *)&pMem->
1bf51 7a 5b 73 69 7a 65 6f 66 28 56 64 62 65 43 75 72  z[sizeof(VdbeCur
1bf52 73 6f 72 29 5d 3b 0a 20 20 20 20 7d 0a 20 20 20  sor)];.    }.   
1bf53 20 69 66 28 20 69 73 42 74 72 65 65 43 75 72 73   if( isBtreeCurs
1bf54 6f 72 20 29 7b 0a 20 20 20 20 20 20 70 43 78 2d  or ){.      pCx-
1bf55 3e 70 43 75 72 73 6f 72 20 3d 20 28 42 74 43 75  >pCursor = (BtCu
1bf56 72 73 6f 72 2a 29 0a 20 20 20 20 20 20 20 20 20  rsor*).         
1bf57 20 26 70 4d 65 6d 2d 3e 7a 5b 73 69 7a 65 6f 66   &pMem->z[sizeof
1bf58 28 56 64 62 65 43 75 72 73 6f 72 29 2b 32 2a 6e  (VdbeCursor)+2*n
1bf59 46 69 65 6c 64 2a 73 69 7a 65 6f 66 28 75 33 32  Field*sizeof(u32
1bf5a 29 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  )];.    }.  }.  
1bf5b 72 65 74 75 72 6e 20 70 43 78 3b 0a 7d 0a 0a 2f  return pCx;.}../
1bf5c 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 63 6f 6e 76  *.** Try to conv
1bf5d 65 72 74 20 61 20 76 61 6c 75 65 20 69 6e 74 6f  ert a value into
1bf5e 20 61 20 6e 75 6d 65 72 69 63 20 72 65 70 72 65   a numeric repre
1bf5f 73 65 6e 74 61 74 69 6f 6e 20 69 66 20 77 65 20  sentation if we 
1bf60 63 61 6e 0a 2a 2a 20 64 6f 20 73 6f 20 77 69 74  can.** do so wit
1bf61 68 6f 75 74 20 6c 6f 73 73 20 6f 66 20 69 6e 66  hout loss of inf
1bf62 6f 72 6d 61 74 69 6f 6e 2e 20 20 49 6e 20 6f 74  ormation.  In ot
1bf63 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 74 68  her words, if th
1bf64 65 20 73 74 72 69 6e 67 0a 2a 2a 20 6c 6f 6f 6b  e string.** look
1bf65 73 20 6c 69 6b 65 20 61 20 6e 75 6d 62 65 72 2c  s like a number,
1bf66 20 63 6f 6e 76 65 72 74 20 69 74 20 69 6e 74 6f   convert it into
1bf67 20 61 20 6e 75 6d 62 65 72 2e 20 20 49 66 20 69   a number.  If i
1bf68 74 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6c 6f  t does not.** lo
1bf69 6f 6b 20 6c 69 6b 65 20 61 20 6e 75 6d 62 65 72  ok like a number
1bf6a 2c 20 6c 65 61 76 65 20 69 74 20 61 6c 6f 6e 65  , leave it alone
1bf6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
1bf6c 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66   applyNumericAff
1bf6d 69 6e 69 74 79 28 4d 65 6d 20 2a 70 52 65 63 29  inity(Mem *pRec)
1bf6e 7b 0a 20 20 69 66 28 20 28 70 52 65 63 2d 3e 66  {.  if( (pRec->f
1bf6f 6c 61 67 73 20 26 20 28 4d 45 4d 5f 52 65 61 6c  lags & (MEM_Real
1bf70 7c 4d 45 4d 5f 49 6e 74 29 29 3d 3d 30 20 29 7b  |MEM_Int))==0 ){
1bf71 0a 20 20 20 20 69 6e 74 20 72 65 61 6c 6e 75 6d  .    int realnum
1bf72 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
1bf73 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65  eMemNulTerminate
1bf74 28 70 52 65 63 29 3b 0a 20 20 20 20 69 66 28 20  (pRec);.    if( 
1bf75 28 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d 45 4d  (pRec->flags&MEM
1bf76 5f 53 74 72 29 0a 20 20 20 20 20 20 20 20 20 26  _Str).         &
1bf77 26 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65  & sqlite3IsNumbe
1bf78 72 28 70 52 65 63 2d 3e 7a 2c 20 26 72 65 61 6c  r(pRec->z, &real
1bf79 6e 75 6d 2c 20 70 52 65 63 2d 3e 65 6e 63 29 20  num, pRec->enc) 
1bf7a 29 7b 0a 20 20 20 20 20 20 69 36 34 20 76 61 6c  ){.      i64 val
1bf7b 75 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ue;.      sqlite
1bf7c 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64  3VdbeChangeEncod
1bf7d 69 6e 67 28 70 52 65 63 2c 20 53 51 4c 49 54 45  ing(pRec, SQLITE
1bf7e 5f 55 54 46 38 29 3b 0a 20 20 20 20 20 20 69 66  _UTF8);.      if
1bf7f 28 20 21 72 65 61 6c 6e 75 6d 20 26 26 20 73 71  ( !realnum && sq
1bf80 6c 69 74 65 33 41 74 6f 69 36 34 28 70 52 65 63  lite3Atoi64(pRec
1bf81 2d 3e 7a 2c 20 26 76 61 6c 75 65 29 20 29 7b 0a  ->z, &value) ){.
1bf82 20 20 20 20 20 20 20 20 70 52 65 63 2d 3e 75 2e          pRec->u.
1bf83 69 20 3d 20 76 61 6c 75 65 3b 0a 20 20 20 20 20  i = value;.     
1bf84 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61     MemSetTypeFla
1bf85 67 28 70 52 65 63 2c 20 4d 45 4d 5f 49 6e 74 29  g(pRec, MEM_Int)
1bf86 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
1bf87 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1bf88 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 70 52 65  beMemRealify(pRe
1bf89 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  c);.      }.    
1bf8a 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  }.  }.}../*.** P
1bf8b 72 6f 63 65 73 73 69 6e 67 20 69 73 20 64 65 74  rocessing is det
1bf8c 65 72 6d 69 6e 65 20 62 79 20 74 68 65 20 61 66  ermine by the af
1bf8d 66 69 6e 69 74 79 20 70 61 72 61 6d 65 74 65 72  finity parameter
1bf8e 3a 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41  :.**.** SQLITE_A
1bf8f 46 46 5f 49 4e 54 45 47 45 52 3a 0a 2a 2a 20 53  FF_INTEGER:.** S
1bf90 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 3a 0a  QLITE_AFF_REAL:.
1bf91 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55  ** SQLITE_AFF_NU
1bf92 4d 45 52 49 43 3a 0a 2a 2a 20 20 20 20 54 72 79  MERIC:.**    Try
1bf93 20 74 6f 20 63 6f 6e 76 65 72 74 20 70 52 65 63   to convert pRec
1bf94 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 72   to an integer r
1bf95 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 72  epresentation or
1bf96 20 61 20 0a 2a 2a 20 20 20 20 66 6c 6f 61 74 69   a .**    floati
1bf97 6e 67 2d 70 6f 69 6e 74 20 72 65 70 72 65 73 65  ng-point represe
1bf98 6e 74 61 74 69 6f 6e 20 69 66 20 61 6e 20 69 6e  ntation if an in
1bf99 74 65 67 65 72 20 72 65 70 72 65 73 65 6e 74 61  teger representa
1bf9a 74 69 6f 6e 0a 2a 2a 20 20 20 20 69 73 20 6e 6f  tion.**    is no
1bf9b 74 20 70 6f 73 73 69 62 6c 65 2e 20 20 4e 6f 74  t possible.  Not
1bf9c 65 20 74 68 61 74 20 74 68 65 20 69 6e 74 65 67  e that the integ
1bf9d 65 72 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  er representatio
1bf9e 6e 20 69 73 0a 2a 2a 20 20 20 20 61 6c 77 61 79  n is.**    alway
1bf9f 73 20 70 72 65 66 65 72 72 65 64 2c 20 65 76 65  s preferred, eve
1bfa0 6e 20 69 66 20 74 68 65 20 61 66 66 69 6e 69 74  n if the affinit
1bfa1 79 20 69 73 20 52 45 41 4c 2c 20 62 65 63 61 75  y is REAL, becau
1bfa2 73 65 0a 2a 2a 20 20 20 20 61 6e 20 69 6e 74 65  se.**    an inte
1bfa3 67 65 72 20 72 65 70 72 65 73 65 6e 74 61 74 69  ger representati
1bfa4 6f 6e 20 69 73 20 6d 6f 72 65 20 73 70 61 63 65  on is more space
1bfa5 20 65 66 66 69 63 69 65 6e 74 20 6f 6e 20 64 69   efficient on di
1bfa6 73 6b 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45  sk..**.** SQLITE
1bfa7 5f 41 46 46 5f 54 45 58 54 3a 0a 2a 2a 20 20 20  _AFF_TEXT:.**   
1bfa8 20 43 6f 6e 76 65 72 74 20 70 52 65 63 20 74 6f   Convert pRec to
1bfa9 20 61 20 74 65 78 74 20 72 65 70 72 65 73 65 6e   a text represen
1bfaa 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 51  tation..**.** SQ
1bfab 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3a 0a 2a  LITE_AFF_NONE:.*
1bfac 2a 20 20 20 20 4e 6f 2d 6f 70 2e 20 20 70 52 65  *    No-op.  pRe
1bfad 63 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a  c is unchanged..
1bfae 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61  */.static void a
1bfaf 70 70 6c 79 41 66 66 69 6e 69 74 79 28 0a 20 20  pplyAffinity(.  
1bfb0 4d 65 6d 20 2a 70 52 65 63 2c 20 20 20 20 20 20  Mem *pRec,      
1bfb1 20 20 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 65      /* The value
1bfb2 20 74 6f 20 61 70 70 6c 79 20 61 66 66 69 6e 69   to apply affini
1bfb3 74 79 20 74 6f 20 2a 2f 0a 20 20 63 68 61 72 20  ty to */.  char 
1bfb4 61 66 66 69 6e 69 74 79 2c 20 20 20 20 20 20 2f  affinity,      /
1bfb5 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79 20 74  * The affinity t
1bfb6 6f 20 62 65 20 61 70 70 6c 69 65 64 20 2a 2f 0a  o be applied */.
1bfb7 20 20 75 38 20 65 6e 63 20 20 20 20 20 20 20 20    u8 enc        
1bfb8 20 20 20 20 20 20 2f 2a 20 55 73 65 20 74 68 69        /* Use thi
1bfb9 73 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20  s text encoding 
1bfba 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 61 66 66 69  */.){.  if( affi
1bfbb 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46  nity==SQLITE_AFF
1bfbc 5f 54 45 58 54 20 29 7b 0a 20 20 20 20 2f 2a 20  _TEXT ){.    /* 
1bfbd 4f 6e 6c 79 20 61 74 74 65 6d 70 74 20 74 68 65  Only attempt the
1bfbe 20 63 6f 6e 76 65 72 73 69 6f 6e 20 74 6f 20 54   conversion to T
1bfbf 45 58 54 20 69 66 20 74 68 65 72 65 20 69 73 20  EXT if there is 
1bfc0 61 6e 20 69 6e 74 65 67 65 72 20 6f 72 20 72 65  an integer or re
1bfc1 61 6c 0a 20 20 20 20 2a 2a 20 72 65 70 72 65 73  al.    ** repres
1bfc2 65 6e 74 61 74 69 6f 6e 20 28 62 6c 6f 62 20 61  entation (blob a
1bfc3 6e 64 20 4e 55 4c 4c 20 64 6f 20 6e 6f 74 20 67  nd NULL do not g
1bfc4 65 74 20 63 6f 6e 76 65 72 74 65 64 29 20 62 75  et converted) bu
1bfc5 74 20 6e 6f 20 73 74 72 69 6e 67 0a 20 20 20 20  t no string.    
1bfc6 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  ** representatio
1bfc7 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  n..    */.    if
1bfc8 28 20 30 3d 3d 28 70 52 65 63 2d 3e 66 6c 61 67  ( 0==(pRec->flag
1bfc9 73 26 4d 45 4d 5f 53 74 72 29 20 26 26 20 28 70  s&MEM_Str) && (p
1bfca 52 65 63 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f  Rec->flags&(MEM_
1bfcb 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 29 20 29  Real|MEM_Int)) )
1bfcc 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
1bfcd 64 62 65 4d 65 6d 53 74 72 69 6e 67 69 66 79 28  dbeMemStringify(
1bfce 70 52 65 63 2c 20 65 6e 63 29 3b 0a 20 20 20 20  pRec, enc);.    
1bfcf 7d 0a 20 20 20 20 70 52 65 63 2d 3e 66 6c 61 67  }.    pRec->flag
1bfd0 73 20 26 3d 20 7e 28 4d 45 4d 5f 52 65 61 6c 7c  s &= ~(MEM_Real|
1bfd1 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 65 6c 73  MEM_Int);.  }els
1bfd2 65 20 69 66 28 20 61 66 66 69 6e 69 74 79 21 3d  e if( affinity!=
1bfd3 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20  SQLITE_AFF_NONE 
1bfd4 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 61  ){.    assert( a
1bfd5 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f  ffinity==SQLITE_
1bfd6 41 46 46 5f 49 4e 54 45 47 45 52 20 7c 7c 20 61  AFF_INTEGER || a
1bfd7 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f  ffinity==SQLITE_
1bfd8 41 46 46 5f 52 45 41 4c 0a 20 20 20 20 20 20 20  AFF_REAL.       
1bfd9 20 20 20 20 20 20 7c 7c 20 61 66 66 69 6e 69 74        || affinit
1bfda 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55  y==SQLITE_AFF_NU
1bfdb 4d 45 52 49 43 20 29 3b 0a 20 20 20 20 61 70 70  MERIC );.    app
1bfdc 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74  lyNumericAffinit
1bfdd 79 28 70 52 65 63 29 3b 0a 20 20 20 20 69 66 28  y(pRec);.    if(
1bfde 20 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 20 4d   pRec->flags & M
1bfdf 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 20  EM_Real ){.     
1bfe0 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 65   sqlite3VdbeInte
1bfe1 67 65 72 41 66 66 69 6e 69 74 79 28 70 52 65 63  gerAffinity(pRec
1bfe2 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  );.    }.  }.}..
1bfe3 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 63 6f 6e  /*.** Try to con
1bfe4 76 65 72 74 20 74 68 65 20 74 79 70 65 20 6f 66  vert the type of
1bfe5 20 61 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75   a function argu
1bfe6 6d 65 6e 74 20 6f 72 20 61 20 72 65 73 75 6c 74  ment or a result
1bfe7 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 6e 74 6f 20   column.** into 
1bfe8 61 20 6e 75 6d 65 72 69 63 20 72 65 70 72 65 73  a numeric repres
1bfe9 65 6e 74 61 74 69 6f 6e 2e 20 20 55 73 65 20 65  entation.  Use e
1bfea 69 74 68 65 72 20 49 4e 54 45 47 45 52 20 6f 72  ither INTEGER or
1bfeb 20 52 45 41 4c 20 77 68 69 63 68 65 76 65 72 0a   REAL whichever.
1bfec 2a 2a 20 69 73 20 61 70 70 72 6f 70 72 69 61 74  ** is appropriat
1bfed 65 2e 20 20 42 75 74 20 6f 6e 6c 79 20 64 6f 20  e.  But only do 
1bfee 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69  the conversion i
1bfef 66 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65  f it is possible
1bff0 20 77 69 74 68 6f 75 74 0a 2a 2a 20 6c 6f 73 73   without.** loss
1bff1 20 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20   of information 
1bff2 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 72  and return the r
1bff3 65 76 69 73 65 64 20 74 79 70 65 20 6f 66 20 74  evised type of t
1bff4 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a  he argument..**.
1bff5 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20 45 58  ** This is an EX
1bff6 50 45 52 49 4d 45 4e 54 41 4c 20 61 70 69 20 61  PERIMENTAL api a
1bff7 6e 64 20 69 73 20 73 75 62 6a 65 63 74 20 74 6f  nd is subject to
1bff8 20 63 68 61 6e 67 65 20 6f 72 20 72 65 6d 6f 76   change or remov
1bff9 61 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  al..*/.SQLITE_AP
1bffa 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61  I int sqlite3_va
1bffb 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65  lue_numeric_type
1bffc 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a  (sqlite3_value *
1bffd 70 56 61 6c 29 7b 0a 20 20 4d 65 6d 20 2a 70 4d  pVal){.  Mem *pM
1bffe 65 6d 20 3d 20 28 4d 65 6d 2a 29 70 56 61 6c 3b  em = (Mem*)pVal;
1bfff 0a 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41  .  applyNumericA
1c000 66 66 69 6e 69 74 79 28 70 4d 65 6d 29 3b 0a 20  ffinity(pMem);. 
1c001 20 73 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 70   storeTypeInfo(p
1c002 4d 65 6d 2c 20 30 29 3b 0a 20 20 72 65 74 75 72  Mem, 0);.  retur
1c003 6e 20 70 4d 65 6d 2d 3e 74 79 70 65 3b 0a 7d 0a  n pMem->type;.}.
1c004 0a 2f 2a 0a 2a 2a 20 45 78 70 6f 72 74 65 64 20  ./*.** Exported 
1c005 76 65 72 73 69 6f 6e 20 6f 66 20 61 70 70 6c 79  version of apply
1c006 41 66 66 69 6e 69 74 79 28 29 2e 20 54 68 69 73  Affinity(). This
1c007 20 6f 6e 65 20 77 6f 72 6b 73 20 6f 6e 20 73 71   one works on sq
1c008 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c 20 0a 2a  lite3_value*, .*
1c009 2a 20 6e 6f 74 20 74 68 65 20 69 6e 74 65 72 6e  * not the intern
1c00a 61 6c 20 4d 65 6d 2a 20 74 79 70 65 2e 0a 2a 2f  al Mem* type..*/
1c00b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1c00c 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75  void sqlite3Valu
1c00d 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 0a  eApplyAffinity(.
1c00e 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
1c00f 2a 70 56 61 6c 2c 20 0a 20 20 75 38 20 61 66 66  *pVal, .  u8 aff
1c010 69 6e 69 74 79 2c 20 0a 20 20 75 38 20 65 6e 63  inity, .  u8 enc
1c011 0a 29 7b 0a 20 20 61 70 70 6c 79 41 66 66 69 6e  .){.  applyAffin
1c012 69 74 79 28 28 4d 65 6d 20 2a 29 70 56 61 6c 2c  ity((Mem *)pVal,
1c013 20 61 66 66 69 6e 69 74 79 2c 20 65 6e 63 29 3b   affinity, enc);
1c014 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .}..#ifdef SQLIT
1c015 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 57 72  E_DEBUG./*.** Wr
1c016 69 74 65 20 61 20 6e 69 63 65 20 73 74 72 69 6e  ite a nice strin
1c017 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  g representation
1c018 20 6f 66 20 74 68 65 20 63 6f 6e 74 65 6e 74 73   of the contents
1c019 20 6f 66 20 63 65 6c 6c 20 70 4d 65 6d 0a 2a 2a   of cell pMem.**
1c01a 20 69 6e 74 6f 20 62 75 66 66 65 72 20 7a 42 75   into buffer zBu
1c01b 66 2c 20 6c 65 6e 67 74 68 20 6e 42 75 66 2e 0a  f, length nBuf..
1c01c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1c01d 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
1c01e 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74  beMemPrettyPrint
1c01f 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 63 68 61 72  (Mem *pMem, char
1c020 20 2a 7a 42 75 66 29 7b 0a 20 20 63 68 61 72 20   *zBuf){.  char 
1c021 2a 7a 43 73 72 20 3d 20 7a 42 75 66 3b 0a 20 20  *zCsr = zBuf;.  
1c022 69 6e 74 20 66 20 3d 20 70 4d 65 6d 2d 3e 66 6c  int f = pMem->fl
1c023 61 67 73 3b 0a 0a 20 20 73 74 61 74 69 63 20 63  ags;..  static c
1c024 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74  onst char *const
1c025 20 65 6e 63 6e 61 6d 65 73 5b 5d 20 3d 20 7b 22   encnames[] = {"
1c026 28 58 29 22 2c 20 22 28 38 29 22 2c 20 22 28 31  (X)", "(8)", "(1
1c027 36 4c 45 29 22 2c 20 22 28 31 36 42 45 29 22 7d  6LE)", "(16BE)"}
1c028 3b 0a 0a 20 20 69 66 28 20 66 26 4d 45 4d 5f 42  ;..  if( f&MEM_B
1c029 6c 6f 62 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  lob ){.    int i
1c02a 3b 0a 20 20 20 20 63 68 61 72 20 63 3b 0a 20 20  ;.    char c;.  
1c02b 20 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 44 79    if( f & MEM_Dy
1c02c 6e 20 29 7b 0a 20 20 20 20 20 20 63 20 3d 20 27  n ){.      c = '
1c02d 7a 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  z';.      assert
1c02e 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 74 61 74  ( (f & (MEM_Stat
1c02f 69 63 7c 4d 45 4d 5f 45 70 68 65 6d 29 29 3d 3d  ic|MEM_Ephem))==
1c030 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  0 );.    }else i
1c031 66 28 20 66 20 26 20 4d 45 4d 5f 53 74 61 74 69  f( f & MEM_Stati
1c032 63 20 29 7b 0a 20 20 20 20 20 20 63 20 3d 20 27  c ){.      c = '
1c033 74 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  t';.      assert
1c034 28 20 28 66 20 26 20 28 4d 45 4d 5f 44 79 6e 7c  ( (f & (MEM_Dyn|
1c035 4d 45 4d 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29  MEM_Ephem))==0 )
1c036 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
1c037 66 20 26 20 4d 45 4d 5f 45 70 68 65 6d 20 29 7b  f & MEM_Ephem ){
1c038 0a 20 20 20 20 20 20 63 20 3d 20 27 65 27 3b 0a  .      c = 'e';.
1c039 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 66        assert( (f
1c03a 20 26 20 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d   & (MEM_Static|M
1c03b 45 4d 5f 44 79 6e 29 29 3d 3d 30 20 29 3b 0a 20  EM_Dyn))==0 );. 
1c03c 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1c03d 63 20 3d 20 27 73 27 3b 0a 20 20 20 20 7d 0a 0a  c = 's';.    }..
1c03e 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
1c03f 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 2c 20  intf(100, zCsr, 
1c040 22 25 63 22 2c 20 63 29 3b 0a 20 20 20 20 7a 43  "%c", c);.    zC
1c041 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72  sr += sqlite3Str
1c042 6c 65 6e 33 30 28 7a 43 73 72 29 3b 0a 20 20 20  len30(zCsr);.   
1c043 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
1c044 66 28 31 30 30 2c 20 7a 43 73 72 2c 20 22 25 64  f(100, zCsr, "%d
1c045 5b 22 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20  [", pMem->n);.  
1c046 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69 74 65    zCsr += sqlite
1c047 33 53 74 72 6c 65 6e 33 30 28 7a 43 73 72 29 3b  3Strlen30(zCsr);
1c048 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
1c049 31 36 20 26 26 20 69 3c 70 4d 65 6d 2d 3e 6e 3b  16 && i<pMem->n;
1c04a 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c   i++){.      sql
1c04b 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30  ite3_snprintf(10
1c04c 30 2c 20 7a 43 73 72 2c 20 22 25 30 32 58 22 2c  0, zCsr, "%02X",
1c04d 20 28 28 69 6e 74 29 70 4d 65 6d 2d 3e 7a 5b 69   ((int)pMem->z[i
1c04e 5d 20 26 20 30 78 46 46 29 29 3b 0a 20 20 20 20  ] & 0xFF));.    
1c04f 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69 74 65    zCsr += sqlite
1c050 33 53 74 72 6c 65 6e 33 30 28 7a 43 73 72 29 3b  3Strlen30(zCsr);
1c051 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69  .    }.    for(i
1c052 3d 30 3b 20 69 3c 31 36 20 26 26 20 69 3c 70 4d  =0; i<16 && i<pM
1c053 65 6d 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20  em->n; i++){.   
1c054 20 20 20 63 68 61 72 20 7a 20 3d 20 70 4d 65 6d     char z = pMem
1c055 2d 3e 7a 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66  ->z[i];.      if
1c056 28 20 7a 3c 33 32 20 7c 7c 20 7a 3e 31 32 36 20  ( z<32 || z>126 
1c057 29 20 2a 7a 43 73 72 2b 2b 20 3d 20 27 2e 27 3b  ) *zCsr++ = '.';
1c058 0a 20 20 20 20 20 20 65 6c 73 65 20 2a 7a 43 73  .      else *zCs
1c059 72 2b 2b 20 3d 20 7a 3b 0a 20 20 20 20 7d 0a 0a  r++ = z;.    }..
1c05a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
1c05b 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 2c 20  intf(100, zCsr, 
1c05c 22 5d 25 73 22 2c 20 65 6e 63 6e 61 6d 65 73 5b  "]%s", encnames[
1c05d 70 4d 65 6d 2d 3e 65 6e 63 5d 29 3b 0a 20 20 20  pMem->enc]);.   
1c05e 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69 74 65 33   zCsr += sqlite3
1c05f 53 74 72 6c 65 6e 33 30 28 7a 43 73 72 29 3b 0a  Strlen30(zCsr);.
1c060 20 20 20 20 69 66 28 20 66 20 26 20 4d 45 4d 5f      if( f & MEM_
1c061 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 73 71  Zero ){.      sq
1c062 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31  lite3_snprintf(1
1c063 30 30 2c 20 7a 43 73 72 2c 22 2b 25 64 7a 22 2c  00, zCsr,"+%dz",
1c064 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 29 3b 0a  pMem->u.nZero);.
1c065 20 20 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 71        zCsr += sq
1c066 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 43  lite3Strlen30(zC
1c067 73 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a  sr);.    }.    *
1c068 7a 43 73 72 20 3d 20 27 5c 30 27 3b 0a 20 20 7d  zCsr = '\0';.  }
1c069 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d  else if( f & MEM
1c06a 5f 53 74 72 20 29 7b 0a 20 20 20 20 69 6e 74 20  _Str ){.    int 
1c06b 6a 2c 20 6b 3b 0a 20 20 20 20 7a 42 75 66 5b 30  j, k;.    zBuf[0
1c06c 5d 20 3d 20 27 20 27 3b 0a 20 20 20 20 69 66 28  ] = ' ';.    if(
1c06d 20 66 20 26 20 4d 45 4d 5f 44 79 6e 20 29 7b 0a   f & MEM_Dyn ){.
1c06e 20 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20        zBuf[1] = 
1c06f 27 7a 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72  'z';.      asser
1c070 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 74 61  t( (f & (MEM_Sta
1c071 74 69 63 7c 4d 45 4d 5f 45 70 68 65 6d 29 29 3d  tic|MEM_Ephem))=
1c072 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20  =0 );.    }else 
1c073 69 66 28 20 66 20 26 20 4d 45 4d 5f 53 74 61 74  if( f & MEM_Stat
1c074 69 63 20 29 7b 0a 20 20 20 20 20 20 7a 42 75 66  ic ){.      zBuf
1c075 5b 31 5d 20 3d 20 27 74 27 3b 0a 20 20 20 20 20  [1] = 't';.     
1c076 20 61 73 73 65 72 74 28 20 28 66 20 26 20 28 4d   assert( (f & (M
1c077 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d  EM_Dyn|MEM_Ephem
1c078 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c  ))==0 );.    }el
1c079 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 45  se if( f & MEM_E
1c07a 70 68 65 6d 20 29 7b 0a 20 20 20 20 20 20 7a 42  phem ){.      zB
1c07b 75 66 5b 31 5d 20 3d 20 27 65 27 3b 0a 20 20 20  uf[1] = 'e';.   
1c07c 20 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20     assert( (f & 
1c07d 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f  (MEM_Static|MEM_
1c07e 44 79 6e 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20  Dyn))==0 );.    
1c07f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 42 75  }else{.      zBu
1c080 66 5b 31 5d 20 3d 20 27 73 27 3b 0a 20 20 20 20  f[1] = 's';.    
1c081 7d 0a 20 20 20 20 6b 20 3d 20 32 3b 0a 20 20 20  }.    k = 2;.   
1c082 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
1c083 66 28 31 30 30 2c 20 26 7a 42 75 66 5b 6b 5d 2c  f(100, &zBuf[k],
1c084 20 22 25 64 22 2c 20 70 4d 65 6d 2d 3e 6e 29 3b   "%d", pMem->n);
1c085 0a 20 20 20 20 6b 20 2b 3d 20 73 71 6c 69 74 65  .    k += sqlite
1c086 33 53 74 72 6c 65 6e 33 30 28 26 7a 42 75 66 5b  3Strlen30(&zBuf[
1c087 6b 5d 29 3b 0a 20 20 20 20 7a 42 75 66 5b 6b 2b  k]);.    zBuf[k+
1c088 2b 5d 20 3d 20 27 5b 27 3b 0a 20 20 20 20 66 6f  +] = '[';.    fo
1c089 72 28 6a 3d 30 3b 20 6a 3c 31 35 20 26 26 20 6a  r(j=0; j<15 && j
1c08a 3c 70 4d 65 6d 2d 3e 6e 3b 20 6a 2b 2b 29 7b 0a  <pMem->n; j++){.
1c08b 20 20 20 20 20 20 75 38 20 63 20 3d 20 70 4d 65        u8 c = pMe
1c08c 6d 2d 3e 7a 5b 6a 5d 3b 0a 20 20 20 20 20 20 69  m->z[j];.      i
1c08d 66 28 20 63 3e 3d 30 78 32 30 20 26 26 20 63 3c  f( c>=0x20 && c<
1c08e 30 78 37 66 20 29 7b 0a 20 20 20 20 20 20 20 20  0x7f ){.        
1c08f 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 63 3b 0a 20  zBuf[k++] = c;. 
1c090 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1c091 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20      zBuf[k++] = 
1c092 27 2e 27 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  '.';.      }.   
1c093 20 7d 0a 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d   }.    zBuf[k++]
1c094 20 3d 20 27 5d 27 3b 0a 20 20 20 20 73 71 6c 69   = ']';.    sqli
1c095 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30  te3_snprintf(100
1c096 2c 26 7a 42 75 66 5b 6b 5d 2c 20 65 6e 63 6e 61  ,&zBuf[k], encna
1c097 6d 65 73 5b 70 4d 65 6d 2d 3e 65 6e 63 5d 29 3b  mes[pMem->enc]);
1c098 0a 20 20 20 20 6b 20 2b 3d 20 73 71 6c 69 74 65  .    k += sqlite
1c099 33 53 74 72 6c 65 6e 33 30 28 26 7a 42 75 66 5b  3Strlen30(&zBuf[
1c09a 6b 5d 29 3b 0a 20 20 20 20 7a 42 75 66 5b 6b 2b  k]);.    zBuf[k+
1c09b 2b 5d 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 23 65  +] = 0;.  }.}.#e
1c09c 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
1c09d 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20  ITE_DEBUG./*.** 
1c09e 50 72 69 6e 74 20 74 68 65 20 76 61 6c 75 65 20  Print the value 
1c09f 6f 66 20 61 20 72 65 67 69 73 74 65 72 20 66 6f  of a register fo
1c0a0 72 20 74 72 61 63 69 6e 67 20 70 75 72 70 6f 73  r tracing purpos
1c0a1 65 73 3a 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  es:.*/.static vo
1c0a2 69 64 20 6d 65 6d 54 72 61 63 65 50 72 69 6e 74  id memTracePrint
1c0a3 28 46 49 4c 45 20 2a 6f 75 74 2c 20 4d 65 6d 20  (FILE *out, Mem 
1c0a4 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 66 6c  *p){.  if( p->fl
1c0a5 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29  ags & MEM_Null )
1c0a6 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75  {.    fprintf(ou
1c0a7 74 2c 20 22 20 4e 55 4c 4c 22 29 3b 0a 20 20 7d  t, " NULL");.  }
1c0a8 65 6c 73 65 20 69 66 28 20 28 70 2d 3e 66 6c 61  else if( (p->fla
1c0a9 67 73 20 26 20 28 4d 45 4d 5f 49 6e 74 7c 4d 45  gs & (MEM_Int|ME
1c0aa 4d 5f 53 74 72 29 29 3d 3d 28 4d 45 4d 5f 49 6e  M_Str))==(MEM_In
1c0ab 74 7c 4d 45 4d 5f 53 74 72 29 20 29 7b 0a 20 20  t|MEM_Str) ){.  
1c0ac 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
1c0ad 20 73 69 3a 25 6c 6c 64 22 2c 20 70 2d 3e 75 2e   si:%lld", p->u.
1c0ae 69 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  i);.  }else if( 
1c0af 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49  p->flags & MEM_I
1c0b0 6e 74 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74  nt ){.    fprint
1c0b1 66 28 6f 75 74 2c 20 22 20 69 3a 25 6c 6c 64 22  f(out, " i:%lld"
1c0b2 2c 20 70 2d 3e 75 2e 69 29 3b 0a 23 69 66 6e 64  , p->u.i);.#ifnd
1c0b3 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
1c0b4 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20  LOATING_POINT.  
1c0b5 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61  }else if( p->fla
1c0b6 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b  gs & MEM_Real ){
1c0b7 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74  .    fprintf(out
1c0b8 2c 20 22 20 72 3a 25 67 22 2c 20 70 2d 3e 72 29  , " r:%g", p->r)
1c0b9 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65  ;.#endif.  }else
1c0ba 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20   if( p->flags & 
1c0bb 4d 45 4d 5f 52 6f 77 53 65 74 20 29 7b 0a 20 20  MEM_RowSet ){.  
1c0bc 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
1c0bd 20 28 72 6f 77 73 65 74 29 22 29 3b 0a 20 20 7d   (rowset)");.  }
1c0be 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 20 7a  else{.    char z
1c0bf 42 75 66 5b 32 30 30 5d 3b 0a 20 20 20 20 73 71  Buf[200];.    sq
1c0c0 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74  lite3VdbeMemPret
1c0c1 74 79 50 72 69 6e 74 28 70 2c 20 7a 42 75 66 29  tyPrint(p, zBuf)
1c0c2 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75  ;.    fprintf(ou
1c0c3 74 2c 20 22 20 22 29 3b 0a 20 20 20 20 66 70 72  t, " ");.    fpr
1c0c4 69 6e 74 66 28 6f 75 74 2c 20 22 25 73 22 2c 20  intf(out, "%s", 
1c0c5 7a 42 75 66 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61  zBuf);.  }.}.sta
1c0c6 74 69 63 20 76 6f 69 64 20 72 65 67 69 73 74 65  tic void registe
1c0c7 72 54 72 61 63 65 28 46 49 4c 45 20 2a 6f 75 74  rTrace(FILE *out
1c0c8 2c 20 69 6e 74 20 69 52 65 67 2c 20 4d 65 6d 20  , int iReg, Mem 
1c0c9 2a 70 29 7b 0a 20 20 66 70 72 69 6e 74 66 28 6f  *p){.  fprintf(o
1c0ca 75 74 2c 20 22 52 45 47 5b 25 64 5d 20 3d 20 22  ut, "REG[%d] = "
1c0cb 2c 20 69 52 65 67 29 3b 0a 20 20 6d 65 6d 54 72  , iReg);.  memTr
1c0cc 61 63 65 50 72 69 6e 74 28 6f 75 74 2c 20 70 29  acePrint(out, p)
1c0cd 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c  ;.  fprintf(out,
1c0ce 20 22 5c 6e 22 29 3b 0a 7d 0a 23 65 6e 64 69 66   "\n");.}.#endif
1c0cf 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
1c0d0 44 45 42 55 47 0a 23 20 20 64 65 66 69 6e 65 20  DEBUG.#  define 
1c0d1 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 52  REGISTER_TRACE(R
1c0d2 2c 4d 29 20 69 66 28 70 2d 3e 74 72 61 63 65 29  ,M) if(p->trace)
1c0d3 72 65 67 69 73 74 65 72 54 72 61 63 65 28 70 2d  registerTrace(p-
1c0d4 3e 74 72 61 63 65 2c 52 2c 4d 29 0a 23 65 6c 73  >trace,R,M).#els
1c0d5 65 0a 23 20 20 64 65 66 69 6e 65 20 52 45 47 49  e.#  define REGI
1c0d6 53 54 45 52 5f 54 52 41 43 45 28 52 2c 4d 29 0a  STER_TRACE(R,M).
1c0d7 23 65 6e 64 69 66 0a 0a 0a 23 69 66 64 65 66 20  #endif...#ifdef 
1c0d8 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a 0a 2f 2a  VDBE_PROFILE../*
1c0d9 20 0a 2a 2a 20 68 77 74 69 6d 65 2e 68 20 63 6f   .** hwtime.h co
1c0da 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73  ntains inline as
1c0db 73 65 6d 62 6c 65 72 20 63 6f 64 65 20 66 6f 72  sembler code for
1c0dc 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 0a 2a   implementing .*
1c0dd 2a 20 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e  * high-performan
1c0de 63 65 20 74 69 6d 69 6e 67 20 72 6f 75 74 69 6e  ce timing routin
1c0df 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  es..*/./********
1c0e0 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68  ****** Include h
1c0e1 77 74 69 6d 65 2e 68 20 69 6e 20 74 68 65 20 6d  wtime.h in the m
1c0e2 69 64 64 6c 65 20 6f 66 20 76 64 62 65 2e 63 20  iddle of vdbe.c 
1c0e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c0e4 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
1c0e5 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
1c0e6 65 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a  e hwtime.h *****
1c0e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c0e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c0e9 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
1c0ea 38 20 4d 61 79 20 32 37 0a 2a 2a 0a 2a 2a 20 54  8 May 27.**.** T
1c0eb 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
1c0ec 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
1c0ed 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
1c0ee 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
1c0ef 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
1c0f0 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
1c0f1 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
1c0f2 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
1c0f3 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
1c0f4 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
1c0f5 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
1c0f6 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
1c0f7 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
1c0f8 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
1c0f9 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
1c0fa 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
1c0fb 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
1c0fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c0fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c0fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c0ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
1c101 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
1c102 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 6d  tains inline asm
1c103 20 63 6f 64 65 20 66 6f 72 20 72 65 74 72 69 65   code for retrie
1c104 76 69 6e 67 20 22 68 69 67 68 2d 70 65 72 66 6f  ving "high-perfo
1c105 72 6d 61 6e 63 65 22 0a 2a 2a 20 63 6f 75 6e 74  rmance".** count
1c106 65 72 73 20 66 6f 72 20 78 38 36 20 63 6c 61 73  ers for x86 clas
1c107 73 20 43 50 55 73 2e 0a 2a 2a 0a 2a 2a 20 24 49  s CPUs..**.** $I
1c108 64 3a 20 68 77 74 69 6d 65 2e 68 2c 76 20 31 2e  d: hwtime.h,v 1.
1c109 33 20 32 30 30 38 2f 30 38 2f 30 31 20 31 34 3a  3 2008/08/01 14:
1c10a 33 33 3a 31 35 20 73 68 61 6e 65 20 45 78 70 20  33:15 shane Exp 
1c10b 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57  $.*/.#ifndef _HW
1c10c 54 49 4d 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20  TIME_H_.#define 
1c10d 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a  _HWTIME_H_../*.*
1c10e 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
1c10f 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72  routine only wor
1c110 6b 73 20 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c  ks on pentium-cl
1c111 61 73 73 20 28 6f 72 20 6e 65 77 65 72 29 20 70  ass (or newer) p
1c112 72 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74  rocessors..** It
1c113 20 75 73 65 73 20 74 68 65 20 52 44 54 53 43 20   uses the RDTSC 
1c114 6f 70 63 6f 64 65 20 74 6f 20 72 65 61 64 20 74  opcode to read t
1c115 68 65 20 63 79 63 6c 65 20 63 6f 75 6e 74 20 76  he cycle count v
1c116 61 6c 75 65 20 6f 75 74 20 6f 66 20 74 68 65 0a  alue out of the.
1c117 2a 2a 20 70 72 6f 63 65 73 73 6f 72 20 61 6e 64  ** processor and
1c118 20 72 65 74 75 72 6e 73 20 74 68 61 74 20 76 61   returns that va
1c119 6c 75 65 2e 20 20 54 68 69 73 20 63 61 6e 20 62  lue.  This can b
1c11a 65 20 75 73 65 64 20 66 6f 72 20 68 69 67 68 2d  e used for high-
1c11b 72 65 73 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67  res.** profiling
1c11c 2e 0a 2a 2f 0a 23 69 66 20 28 64 65 66 69 6e 65  ..*/.#if (define
1c11d 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64  d(__GNUC__) || d
1c11e 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29  efined(_MSC_VER)
1c11f 29 20 26 26 20 5c 0a 20 20 20 20 20 20 28 64 65  ) && \.      (de
1c120 66 69 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 64  fined(i386) || d
1c121 65 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29  efined(__i386__)
1c122 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49   || defined(_M_I
1c123 58 38 36 29 29 0a 0a 20 20 23 69 66 20 64 65 66  X86))..  #if def
1c124 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a  ined(__GNUC__)..
1c125 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c    __inline__ sql
1c126 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74  ite_uint64 sqlit
1c127 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a  e3Hwtime(void){.
1c128 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e       unsigned in
1c129 74 20 6c 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f  t lo, hi;.     _
1c12a 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c  _asm__ __volatil
1c12b 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22  e__ ("rdtsc" : "
1c12c 3d 61 22 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28  =a" (lo), "=d" (
1c12d 68 69 29 29 3b 0a 20 20 20 20 20 72 65 74 75 72  hi));.     retur
1c12e 6e 20 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  n (sqlite_uint64
1c12f 29 68 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a  )hi << 32 | lo;.
1c130 20 20 7d 0a 0a 20 20 23 65 6c 69 66 20 64 65 66    }..  #elif def
1c131 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a  ined(_MSC_VER)..
1c132 20 20 5f 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b    __declspec(nak
1c133 65 64 29 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c  ed) __inline sql
1c134 69 74 65 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65  ite_uint64 __cde
1c135 63 6c 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65  cl sqlite3Hwtime
1c136 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61  (void){.     __a
1c137 73 6d 20 7b 0a 20 20 20 20 20 20 20 20 72 64 74  sm {.        rdt
1c138 73 63 0a 20 20 20 20 20 20 20 20 72 65 74 20 20  sc.        ret  
1c139 20 20 20 20 20 3b 20 72 65 74 75 72 6e 20 76 61       ; return va
1c13a 6c 75 65 20 61 74 20 45 44 58 3a 45 41 58 0a 20  lue at EDX:EAX. 
1c13b 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e      }.  }..  #en
1c13c 64 69 66 0a 0a 23 65 6c 69 66 20 28 64 65 66 69  dif..#elif (defi
1c13d 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26  ned(__GNUC__) &&
1c13e 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36   defined(__x86_6
1c13f 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e  4__))..  __inlin
1c140 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  e__ sqlite_uint6
1c141 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  4 sqlite3Hwtime(
1c142 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73  void){.      uns
1c143 69 67 6e 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a  igned long val;.
1c144 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f        __asm__ __
1c145 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74  volatile__ ("rdt
1c146 73 63 22 20 3a 20 22 3d 41 22 20 28 76 61 6c 29  sc" : "=A" (val)
1c147 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
1c148 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66  val;.  }. .#elif
1c149 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43   (defined(__GNUC
1c14a 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f  __) && defined(_
1c14b 5f 70 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e  _ppc__))..  __in
1c14c 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69  line__ sqlite_ui
1c14d 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69  nt64 sqlite3Hwti
1c14e 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20  me(void){.      
1c14f 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f  unsigned long lo
1c150 6e 67 20 72 65 74 76 61 6c 3b 0a 20 20 20 20 20  ng retval;.     
1c151 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a   unsigned long j
1c152 75 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d  unk;.      __asm
1c153 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20  __ __volatile__ 
1c154 28 22 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20  ("\n\.          
1c155 31 3a 20 20 20 20 20 20 6d 66 74 62 75 20 20 20  1:      mftbu   
1c156 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20  %1\n\.          
1c157 20 20 20 20 20 20 20 20 6d 66 74 62 20 20 20 20          mftb    
1c158 25 4c 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20  %L0\n\.         
1c159 20 20 20 20 20 20 20 20 20 6d 66 74 62 75 20 20           mftbu  
1c15a 20 25 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20   %0\n\.         
1c15b 20 20 20 20 20 20 20 20 20 63 6d 70 77 20 20 20           cmpw   
1c15c 20 25 30 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20   %0,%1\n\.      
1c15d 20 20 20 20 20 20 20 20 20 20 20 20 62 6e 65 20              bne 
1c15e 20 20 20 20 31 62 22 0a 20 20 20 20 20 20 20 20      1b".        
1c15f 20 20 20 20 20 20 20 20 20 20 3a 20 22 3d 72 22            : "=r"
1c160 20 28 72 65 74 76 61 6c 29 2c 20 22 3d 72 22 20   (retval), "=r" 
1c161 28 6a 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72  (junk));.      r
1c162 65 74 75 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20  eturn retval;.  
1c163 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20 23 65 72 72  }..#else..  #err
1c164 6f 72 20 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e  or Need implemen
1c165 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65  tation of sqlite
1c166 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f  3Hwtime() for yo
1c167 75 72 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20  ur platform...  
1c168 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69  /*.  ** To compi
1c169 6c 65 20 77 69 74 68 6f 75 74 20 69 6d 70 6c 65  le without imple
1c16a 6d 65 6e 74 69 6e 67 20 73 71 6c 69 74 65 33 48  menting sqlite3H
1c16b 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72  wtime() for your
1c16c 20 70 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20   platform,.  ** 
1c16d 79 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65 20 74  you can remove t
1c16e 68 65 20 61 62 6f 76 65 20 23 65 72 72 6f 72 20  he above #error 
1c16f 61 6e 64 20 75 73 65 20 74 68 65 20 66 6f 6c 6c  and use the foll
1c170 6f 77 69 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20  owing.  ** stub 
1c171 66 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77  function.  You w
1c172 69 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20  ill lose timing 
1c173 73 75 70 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79  support for many
1c174 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 62  .  ** of the deb
1c175 75 67 67 69 6e 67 20 61 6e 64 20 74 65 73 74 69  ugging and testi
1c176 6e 67 20 75 74 69 6c 69 74 69 65 73 2c 20 62 75  ng utilities, bu
1c177 74 20 69 74 20 73 68 6f 75 6c 64 20 61 74 0a 20  t it should at. 
1c178 20 2a 2a 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c   ** least compil
1c179 65 20 61 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a  e and run..  */.
1c17a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
1c17b 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73   sqlite_uint64 s
1c17c 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69  qlite3Hwtime(voi
1c17d 64 29 7b 20 72 65 74 75 72 6e 20 28 28 73 71 6c  d){ return ((sql
1c17e 69 74 65 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d  ite_uint64)0); }
1c17f 0a 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66  ..#endif..#endif
1c180 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 48 57   /* !defined(_HW
1c181 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a  TIME_H_) */../**
1c182 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
1c183 20 6f 66 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a   of hwtime.h ***
1c184 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c185 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c186 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
1c187 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
1c188 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
1c189 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 76 64 62   left off in vdb
1c18a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
1c18b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 65  ***********/..#e
1c18c 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
1c18d 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52  CHECK_FOR_INTERR
1c18e 55 50 54 20 6d 61 63 72 6f 20 64 65 66 69 6e 65  UPT macro define
1c18f 64 20 68 65 72 65 20 6c 6f 6f 6b 73 20 74 6f 20  d here looks to 
1c190 73 65 65 20 69 66 20 74 68 65 0a 2a 2a 20 73 71  see if the.** sq
1c191 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28  lite3_interrupt(
1c192 29 20 72 6f 75 74 69 6e 65 20 68 61 73 20 62 65  ) routine has be
1c193 65 6e 20 63 61 6c 6c 65 64 2e 20 20 49 66 20 69  en called.  If i
1c194 74 20 68 61 73 20 62 65 65 6e 2c 20 74 68 65 6e  t has been, then
1c195 0a 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67 20 6f  .** processing o
1c196 66 20 74 68 65 20 56 44 42 45 20 70 72 6f 67 72  f the VDBE progr
1c197 61 6d 20 69 73 20 69 6e 74 65 72 72 75 70 74 65  am is interrupte
1c198 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 61  d..**.** This ma
1c199 63 72 6f 20 61 64 64 65 64 20 74 6f 20 65 76 65  cro added to eve
1c19a 72 79 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74  ry instruction t
1c19b 68 61 74 20 64 6f 65 73 20 61 20 6a 75 6d 70 20  hat does a jump 
1c19c 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 69  in order to.** i
1c19d 6d 70 6c 65 6d 65 6e 74 20 61 20 6c 6f 6f 70 2e  mplement a loop.
1c19e 20 20 54 68 69 73 20 74 65 73 74 20 75 73 65 64    This test used
1c19f 20 74 6f 20 62 65 20 6f 6e 20 65 76 65 72 79 20   to be on every 
1c1a0 73 69 6e 67 6c 65 20 69 6e 73 74 72 75 63 74 69  single instructi
1c1a1 6f 6e 2c 0a 2a 2a 20 62 75 74 20 74 68 61 74 20  on,.** but that 
1c1a2 6d 65 61 6e 74 20 77 65 20 6d 6f 72 65 20 74 65  meant we more te
1c1a3 73 74 69 6e 67 20 74 68 61 74 20 77 65 20 6e 65  sting that we ne
1c1a4 65 64 65 64 2e 20 20 42 79 20 6f 6e 6c 79 20 74  eded.  By only t
1c1a5 65 73 74 69 6e 67 20 74 68 65 0a 2a 2a 20 66 6c  esting the.** fl
1c1a6 61 67 20 6f 6e 20 6a 75 6d 70 20 69 6e 73 74 72  ag on jump instr
1c1a7 75 63 74 69 6f 6e 73 2c 20 77 65 20 67 65 74 20  uctions, we get 
1c1a8 61 20 28 73 6d 61 6c 6c 29 20 73 70 65 65 64 20  a (small) speed 
1c1a9 69 6d 70 72 6f 76 65 6d 65 6e 74 2e 0a 2a 2f 0a  improvement..*/.
1c1aa 23 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 46 4f  #define CHECK_FO
1c1ab 52 5f 49 4e 54 45 52 52 55 50 54 20 5c 0a 20 20  R_INTERRUPT \.  
1c1ac 20 69 66 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e   if( db->u1.isIn
1c1ad 74 65 72 72 75 70 74 65 64 20 29 20 67 6f 74 6f  terrupted ) goto
1c1ae 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 69 6e   abort_due_to_in
1c1af 74 65 72 72 75 70 74 3b 0a 0a 23 69 66 64 65 66  terrupt;..#ifdef
1c1b0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 73 74   SQLITE_DEBUG.st
1c1b1 61 74 69 63 20 69 6e 74 20 66 69 6c 65 45 78 69  atic int fileExi
1c1b2 73 74 73 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  sts(sqlite3 *db,
1c1b3 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
1c1b4 6c 65 29 7b 0a 20 20 69 6e 74 20 72 65 73 20 3d  le){.  int res =
1c1b5 20 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53   0;.  int rc = S
1c1b6 51 4c 49 54 45 5f 4f 4b 3b 0a 23 69 66 64 65 66  QLITE_OK;.#ifdef
1c1b7 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 2f   SQLITE_TEST.  /
1c1b8 2a 20 49 66 20 77 65 20 61 72 65 20 63 75 72 72  * If we are curr
1c1b9 65 6e 74 6c 79 20 74 65 73 74 69 6e 67 20 49 4f  ently testing IO
1c1ba 20 65 72 72 6f 72 73 2c 20 74 68 65 6e 20 64 6f   errors, then do
1c1bb 20 6e 6f 74 20 63 61 6c 6c 20 4f 73 41 63 63 65   not call OsAcce
1c1bc 73 73 28 29 20 74 6f 0a 20 20 2a 2a 20 74 65 73  ss() to.  ** tes
1c1bd 74 20 66 6f 72 20 74 68 65 20 70 72 65 73 65 6e  t for the presen
1c1be 63 65 20 6f 66 20 7a 46 69 6c 65 2e 20 54 68 69  ce of zFile. Thi
1c1bf 73 20 69 73 20 62 65 63 61 75 73 65 20 61 6e 79  s is because any
1c1c0 20 49 4f 20 65 72 72 6f 72 20 74 68 61 74 0a 20   IO error that. 
1c1c1 20 2a 2a 20 6f 63 63 75 72 73 20 68 65 72 65 20   ** occurs here 
1c1c2 77 69 6c 6c 20 6e 6f 74 20 62 65 20 72 65 70 6f  will not be repo
1c1c3 72 74 65 64 2c 20 63 61 75 73 69 6e 67 20 74 68  rted, causing th
1c1c4 65 20 74 65 73 74 20 74 6f 20 66 61 69 6c 2e 0a  e test to fail..
1c1c5 20 20 2a 2f 0a 20 20 65 78 74 65 72 6e 20 69 6e    */.  extern in
1c1c6 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  t sqlite3_io_err
1c1c7 6f 72 5f 70 65 6e 64 69 6e 67 3b 0a 20 20 69 66  or_pending;.  if
1c1c8 28 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  ( sqlite3_io_err
1c1c9 6f 72 5f 70 65 6e 64 69 6e 67 3c 3d 30 20 29 0a  or_pending<=0 ).
1c1ca 23 65 6e 64 69 66 0a 20 20 20 20 72 63 20 3d 20  #endif.    rc = 
1c1cb 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28  sqlite3OsAccess(
1c1cc 64 62 2d 3e 70 56 66 73 2c 20 7a 46 69 6c 65 2c  db->pVfs, zFile,
1c1cd 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45   SQLITE_ACCESS_E
1c1ce 58 49 53 54 53 2c 20 26 72 65 73 29 3b 0a 20 20  XISTS, &res);.  
1c1cf 72 65 74 75 72 6e 20 28 72 65 73 20 26 26 20 72  return (res && r
1c1d0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 3b 0a 7d  c==SQLITE_OK);.}
1c1d1 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66  .#endif..#ifndef
1c1d2 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68   NDEBUG./*.** Th
1c1d3 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f  is function is o
1c1d4 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20  nly called from 
1c1d5 77 69 74 68 69 6e 20 61 6e 20 61 73 73 65 72 74  within an assert
1c1d6 28 29 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 49  () expression. I
1c1d7 74 0a 2a 2a 20 63 68 65 63 6b 73 20 74 68 61 74  t.** checks that
1c1d8 20 74 68 65 20 73 71 6c 69 74 65 33 2e 6e 54 72   the sqlite3.nTr
1c1d9 61 6e 73 61 63 74 69 6f 6e 20 76 61 72 69 61 62  ansaction variab
1c1da 6c 65 20 69 73 20 63 6f 72 72 65 63 74 6c 79 20  le is correctly 
1c1db 73 65 74 20 74 6f 0a 2a 2a 20 74 68 65 20 6e 75  set to.** the nu
1c1dc 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 74 72 61 6e  mber of non-tran
1c1dd 73 61 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e  saction savepoin
1c1de 74 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20  ts currently in 
1c1df 74 68 65 20 0a 2a 2a 20 6c 69 6e 6b 65 64 20 6c  the .** linked l
1c1e0 69 73 74 20 73 74 61 72 74 69 6e 67 20 61 74 20  ist starting at 
1c1e1 73 71 6c 69 74 65 33 2e 70 53 61 76 65 70 6f 69  sqlite3.pSavepoi
1c1e2 6e 74 2e 0a 2a 2a 20 0a 2a 2a 20 55 73 61 67 65  nt..** .** Usage
1c1e3 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 61 73 73 65  :.**.**     asse
1c1e4 72 74 28 20 63 68 65 63 6b 53 61 76 65 70 6f 69  rt( checkSavepoi
1c1e5 6e 74 43 6f 75 6e 74 28 64 62 29 20 29 3b 0a 2a  ntCount(db) );.*
1c1e6 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 65  /.static int che
1c1e7 63 6b 53 61 76 65 70 6f 69 6e 74 43 6f 75 6e 74  ckSavepointCount
1c1e8 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
1c1e9 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 53 61   int n = 0;.  Sa
1c1ea 76 65 70 6f 69 6e 74 20 2a 70 3b 0a 20 20 66 6f  vepoint *p;.  fo
1c1eb 72 28 70 3d 64 62 2d 3e 70 53 61 76 65 70 6f 69  r(p=db->pSavepoi
1c1ec 6e 74 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78  nt; p; p=p->pNex
1c1ed 74 29 20 6e 2b 2b 3b 0a 20 20 61 73 73 65 72 74  t) n++;.  assert
1c1ee 28 20 6e 3d 3d 28 64 62 2d 3e 6e 53 61 76 65 70  ( n==(db->nSavep
1c1ef 6f 69 6e 74 20 2b 20 64 62 2d 3e 69 73 54 72 61  oint + db->isTra
1c1f0 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e  nsactionSavepoin
1c1f1 74 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 31  t) );.  return 1
1c1f2 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
1c1f3 2a 20 45 78 65 63 75 74 65 20 61 73 20 6d 75 63  * Execute as muc
1c1f4 68 20 6f 66 20 61 20 56 44 42 45 20 70 72 6f 67  h of a VDBE prog
1c1f5 72 61 6d 20 61 73 20 77 65 20 63 61 6e 20 74 68  ram as we can th
1c1f6 65 6e 20 72 65 74 75 72 6e 2e 0a 2a 2a 0a 2a 2a  en return..**.**
1c1f7 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
1c1f8 52 65 61 64 79 28 29 20 6d 75 73 74 20 62 65 20  Ready() must be 
1c1f9 63 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 74 68  called before th
1c1fa 69 73 20 72 6f 75 74 69 6e 65 20 69 6e 20 6f 72  is routine in or
1c1fb 64 65 72 20 74 6f 0a 2a 2a 20 63 6c 6f 73 65 20  der to.** close 
1c1fc 74 68 65 20 70 72 6f 67 72 61 6d 20 77 69 74 68  the program with
1c1fd 20 61 20 66 69 6e 61 6c 20 4f 50 5f 48 61 6c 74   a final OP_Halt
1c1fe 20 61 6e 64 20 74 6f 20 73 65 74 20 75 70 20 74   and to set up t
1c1ff 68 65 20 63 61 6c 6c 62 61 63 6b 73 0a 2a 2a 20  he callbacks.** 
1c200 61 6e 64 20 74 68 65 20 65 72 72 6f 72 20 6d 65  and the error me
1c201 73 73 61 67 65 20 70 6f 69 6e 74 65 72 2e 0a 2a  ssage pointer..*
1c202 2a 0a 2a 2a 20 57 68 65 6e 65 76 65 72 20 61 20  *.** Whenever a 
1c203 72 6f 77 20 6f 72 20 72 65 73 75 6c 74 20 64 61  row or result da
1c204 74 61 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c  ta is available,
1c205 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69   this routine wi
1c206 6c 6c 20 65 69 74 68 65 72 0a 2a 2a 20 69 6e 76  ll either.** inv
1c207 6f 6b 65 20 74 68 65 20 72 65 73 75 6c 74 20 63  oke the result c
1c208 61 6c 6c 62 61 63 6b 20 28 69 66 20 74 68 65 72  allback (if ther
1c209 65 20 69 73 20 6f 6e 65 29 20 6f 72 20 72 65 74  e is one) or ret
1c20a 75 72 6e 20 77 69 74 68 0a 2a 2a 20 53 51 4c 49  urn with.** SQLI
1c20b 54 45 5f 52 4f 57 2e 0a 2a 2a 0a 2a 2a 20 49 66  TE_ROW..**.** If
1c20c 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d   an attempt is m
1c20d 61 64 65 20 74 6f 20 6f 70 65 6e 20 61 20 6c 6f  ade to open a lo
1c20e 63 6b 65 64 20 64 61 74 61 62 61 73 65 2c 20 74  cked database, t
1c20f 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
1c210 0a 2a 2a 20 77 69 6c 6c 20 65 69 74 68 65 72 20  .** will either 
1c211 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20  invoke the busy 
1c212 63 61 6c 6c 62 61 63 6b 20 28 69 66 20 74 68 65  callback (if the
1c213 72 65 20 69 73 20 6f 6e 65 29 20 6f 72 20 69 74  re is one) or it
1c214 20 77 69 6c 6c 0a 2a 2a 20 72 65 74 75 72 6e 20   will.** return 
1c215 53 51 4c 49 54 45 5f 42 55 53 59 2e 0a 2a 2a 0a  SQLITE_BUSY..**.
1c216 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  ** If an error o
1c217 63 63 75 72 73 2c 20 61 6e 20 65 72 72 6f 72 20  ccurs, an error 
1c218 6d 65 73 73 61 67 65 20 69 73 20 77 72 69 74 74  message is writt
1c219 65 6e 20 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74  en to memory obt
1c21a 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 73 71  ained.** from sq
1c21b 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 61  lite3_malloc() a
1c21c 6e 64 20 70 2d 3e 7a 45 72 72 4d 73 67 20 69 73  nd p->zErrMsg is
1c21d 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74   made to point t
1c21e 6f 20 74 68 61 74 20 6d 65 6d 6f 72 79 2e 0a 2a  o that memory..*
1c21f 2a 20 54 68 65 20 65 72 72 6f 72 20 63 6f 64 65  * The error code
1c220 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70 2d   is stored in p-
1c221 3e 72 63 20 61 6e 64 20 74 68 69 73 20 72 6f 75  >rc and this rou
1c222 74 69 6e 65 20 72 65 74 75 72 6e 73 20 53 51 4c  tine returns SQL
1c223 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a  ITE_ERROR..**.**
1c224 20 49 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b   If the callback
1c225 20 65 76 65 72 20 72 65 74 75 72 6e 73 20 6e 6f   ever returns no
1c226 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65  n-zero, then the
1c227 20 70 72 6f 67 72 61 6d 20 65 78 69 74 73 0a 2a   program exits.*
1c228 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 20  * immediately.  
1c229 54 68 65 72 65 20 77 69 6c 6c 20 62 65 20 6e 6f  There will be no
1c22a 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 62   error message b
1c22b 75 74 20 74 68 65 20 70 2d 3e 72 63 20 66 69 65  ut the p->rc fie
1c22c 6c 64 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20  ld is.** set to 
1c22d 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 61 6e 64  SQLITE_ABORT and
1c22e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69   this routine wi
1c22f 6c 6c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  ll return SQLITE
1c230 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41 20  _ERROR..**.** A 
1c231 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
1c232 6e 20 65 72 72 6f 72 20 63 61 75 73 65 73 20 70  n error causes p
1c233 2d 3e 72 63 20 74 6f 20 62 65 20 73 65 74 20 74  ->rc to be set t
1c234 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 61  o SQLITE_NOMEM a
1c235 6e 64 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69  nd this.** routi
1c236 6e 65 20 74 6f 20 72 65 74 75 72 6e 20 53 51 4c  ne to return SQL
1c237 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a  ITE_ERROR..**.**
1c238 20 4f 74 68 65 72 20 66 61 74 61 6c 20 65 72 72   Other fatal err
1c239 6f 72 73 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ors return SQLIT
1c23a 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41  E_ERROR..**.** A
1c23b 66 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e  fter this routin
1c23c 65 20 68 61 73 20 66 69 6e 69 73 68 65 64 2c 20  e has finished, 
1c23d 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c  sqlite3VdbeFinal
1c23e 69 7a 65 28 29 20 73 68 6f 75 6c 64 20 62 65 0a  ize() should be.
1c23f 2a 2a 20 75 73 65 64 20 74 6f 20 63 6c 65 61 6e  ** used to clean
1c240 20 75 70 20 74 68 65 20 6d 65 73 73 20 74 68 61   up the mess tha
1c241 74 20 77 61 73 20 6c 65 66 74 20 62 65 68 69 6e  t was left behin
1c242 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
1c243 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1c244 56 64 62 65 45 78 65 63 28 0a 20 20 56 64 62 65  VdbeExec(.  Vdbe
1c245 20 2a 70 20 20 20 20 20 20 20 20 20 20 20 20 20   *p             
1c246 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44         /* The VD
1c247 42 45 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 70  BE */.){.  int p
1c248 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
1c249 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72 6f        /* The pro
1c24a 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  gram counter */.
1c24b 20 20 4f 70 20 2a 70 4f 70 3b 20 20 20 20 20 20    Op *pOp;      
1c24c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1c24d 43 75 72 72 65 6e 74 20 6f 70 65 72 61 74 69 6f  Current operatio
1c24e 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20  n */.  int rc = 
1c24f 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20  SQLITE_OK;      
1c250 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65    /* Value to re
1c251 74 75 72 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65  turn */.  sqlite
1c252 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 20 20  3 *db = p->db;  
1c253 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
1c254 62 61 73 65 20 2a 2f 0a 20 20 75 38 20 65 6e 63  base */.  u8 enc
1c255 6f 64 69 6e 67 20 3d 20 45 4e 43 28 64 62 29 3b  oding = ENC(db);
1c256 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
1c257 62 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 2a 2f  base encoding */
1c258 0a 20 20 4d 65 6d 20 2a 70 49 6e 31 20 3d 20 30  .  Mem *pIn1 = 0
1c259 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
1c25a 20 31 73 74 20 69 6e 70 75 74 20 6f 70 65 72 61   1st input opera
1c25b 6e 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e  nd */.  Mem *pIn
1c25c 32 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  2 = 0;          
1c25d 20 20 20 2f 2a 20 32 6e 64 20 69 6e 70 75 74 20     /* 2nd input 
1c25e 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 4d 65 6d  operand */.  Mem
1c25f 20 2a 70 49 6e 33 20 3d 20 30 3b 20 20 20 20 20   *pIn3 = 0;     
1c260 20 20 20 20 20 20 20 20 2f 2a 20 33 72 64 20 69          /* 3rd i
1c261 6e 70 75 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a  nput operand */.
1c262 20 20 4d 65 6d 20 2a 70 4f 75 74 20 3d 20 30 3b    Mem *pOut = 0;
1c263 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1c264 4f 75 74 70 75 74 20 6f 70 65 72 61 6e 64 20 2a  Output operand *
1c265 2f 0a 20 20 75 38 20 6f 70 50 72 6f 70 65 72 74  /.  u8 opPropert
1c266 79 3b 0a 20 20 69 6e 74 20 69 43 6f 6d 70 61 72  y;.  int iCompar
1c267 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  e = 0;          
1c268 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 6c 61 73  /* Result of las
1c269 74 20 4f 50 5f 43 6f 6d 70 61 72 65 20 6f 70 65  t OP_Compare ope
1c26a 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  ration */.  int 
1c26b 2a 61 50 65 72 6d 75 74 65 20 3d 20 30 3b 20 20  *aPermute = 0;  
1c26c 20 20 20 20 20 20 20 2f 2a 20 50 65 72 6d 75 74         /* Permut
1c26d 61 74 69 6f 6e 20 6f 66 20 63 6f 6c 75 6d 6e 73  ation of columns
1c26e 20 66 6f 72 20 4f 50 5f 43 6f 6d 70 61 72 65 20   for OP_Compare 
1c26f 2a 2f 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50  */.#ifdef VDBE_P
1c270 52 4f 46 49 4c 45 0a 20 20 75 36 34 20 73 74 61  ROFILE.  u64 sta
1c271 72 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rt;             
1c272 20 20 20 20 2f 2a 20 43 50 55 20 63 6c 6f 63 6b      /* CPU clock
1c273 20 63 6f 75 6e 74 20 61 74 20 73 74 61 72 74 20   count at start 
1c274 6f 66 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 69  of opcode */.  i
1c275 6e 74 20 6f 72 69 67 50 63 3b 20 20 20 20 20 20  nt origPc;      
1c276 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 6f            /* Pro
1c277 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 61 74 20  gram counter at 
1c278 73 74 61 72 74 20 6f 66 20 6f 70 63 6f 64 65 20  start of opcode 
1c279 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65  */.#endif.#ifnde
1c27a 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52  f SQLITE_OMIT_PR
1c27b 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0a  OGRESS_CALLBACK.
1c27c 20 20 69 6e 74 20 6e 50 72 6f 67 72 65 73 73 4f    int nProgressO
1c27d 70 73 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20  ps = 0;      /* 
1c27e 4f 70 63 6f 64 65 73 20 65 78 65 63 75 74 65 64  Opcodes executed
1c27f 20 73 69 6e 63 65 20 70 72 6f 67 72 65 73 73 20   since progress 
1c280 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 23 65 6e  callback. */.#en
1c281 64 69 66 0a 20 20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  dif.  /*********
1c282 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c283 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c284 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c285 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2a  ***********.  **
1c286 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67   Automatically g
1c287 65 6e 65 72 61 74 65 64 20 63 6f 64 65 0a 20 20  enerated code.  
1c288 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c  **.  ** The foll
1c289 6f 77 69 6e 67 20 75 6e 69 6f 6e 20 69 73 20 61  owing union is a
1c28a 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e  utomatically gen
1c28b 65 72 61 74 65 64 20 62 79 20 74 68 65 0a 20 20  erated by the.  
1c28c 2a 2a 20 76 64 62 65 2d 63 6f 6d 70 72 65 73 73  ** vdbe-compress
1c28d 2e 74 63 6c 20 73 63 72 69 70 74 2e 20 20 54 68  .tcl script.  Th
1c28e 65 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68 69  e purpose of thi
1c28f 73 20 75 6e 69 6f 6e 20 69 73 20 74 6f 0a 20 20  s union is to.  
1c290 2a 2a 20 72 65 64 75 63 65 20 74 68 65 20 61 6d  ** reduce the am
1c291 6f 75 6e 74 20 6f 66 20 73 74 61 63 6b 20 73 70  ount of stack sp
1c292 61 63 65 20 72 65 71 75 69 72 65 64 20 62 79 20  ace required by 
1c293 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 20  this function.. 
1c294 20 2a 2a 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73   ** See comments
1c295 20 69 6e 20 74 68 65 20 76 64 62 65 2d 63 6f 6d   in the vdbe-com
1c296 70 72 65 73 73 2e 74 63 6c 20 73 63 72 69 70 74  press.tcl script
1c297 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 20 20   for details..  
1c298 2a 2f 0a 20 20 75 6e 69 6f 6e 20 76 64 62 65 45  */.  union vdbeE
1c299 78 65 63 55 6e 69 6f 6e 20 7b 0a 20 20 20 20 73  xecUnion {.    s
1c29a 74 72 75 63 74 20 4f 50 5f 59 69 65 6c 64 5f 73  truct OP_Yield_s
1c29b 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c29c 20 20 69 6e 74 20 70 63 44 65 73 74 3b 0a 20 20    int pcDest;.  
1c29d 20 20 7d 20 61 61 3b 0a 20 20 20 20 73 74 72 75    } aa;.    stru
1c29e 63 74 20 4f 50 5f 56 61 72 69 61 62 6c 65 5f 73  ct OP_Variable_s
1c29f 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c2a0 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20    int p1;       
1c2a1 20 20 20 2f 2a 20 56 61 72 69 61 62 6c 65 20 74     /* Variable t
1c2a2 6f 20 63 6f 70 79 20 66 72 6f 6d 20 2a 2f 0a 20  o copy from */. 
1c2a3 20 20 20 20 20 69 6e 74 20 70 32 3b 20 20 20 20       int p2;    
1c2a4 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
1c2a5 72 20 74 6f 20 63 6f 70 79 20 74 6f 20 2a 2f 0a  r to copy to */.
1c2a6 20 20 20 20 20 20 69 6e 74 20 6e 3b 20 20 20 20        int n;    
1c2a7 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1c2a8 20 6f 66 20 76 61 6c 75 65 73 20 6c 65 66 74 20   of values left 
1c2a9 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 20 20 20  to copy */.     
1c2aa 20 4d 65 6d 20 2a 70 56 61 72 3b 20 20 20 20 20   Mem *pVar;     
1c2ab 20 20 2f 2a 20 56 61 6c 75 65 20 62 65 69 6e 67    /* Value being
1c2ac 20 74 72 61 6e 73 66 65 72 72 65 64 20 2a 2f 0a   transferred */.
1c2ad 20 20 20 20 7d 20 61 62 3b 0a 20 20 20 20 73 74      } ab;.    st
1c2ae 72 75 63 74 20 4f 50 5f 4d 6f 76 65 5f 73 74 61  ruct OP_Move_sta
1c2af 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c2b0 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63 3b 20 20  char *zMalloc;  
1c2b1 20 2f 2a 20 48 6f 6c 64 69 6e 67 20 76 61 72 69   /* Holding vari
1c2b2 61 62 6c 65 20 66 6f 72 20 61 6c 6c 6f 63 61 74  able for allocat
1c2b3 65 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 20  ed memory */.   
1c2b4 20 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20     int n;       
1c2b5 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1c2b6 20 72 65 67 69 73 74 65 72 73 20 6c 65 66 74 20   registers left 
1c2b7 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 20 20 20  to copy */.     
1c2b8 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20 20   int p1;        
1c2b9 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74 6f    /* Register to
1c2ba 20 63 6f 70 79 20 66 72 6f 6d 20 2a 2f 0a 20 20   copy from */.  
1c2bb 20 20 20 20 69 6e 74 20 70 32 3b 20 20 20 20 20      int p2;     
1c2bc 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72       /* Register
1c2bd 20 74 6f 20 63 6f 70 79 20 74 6f 20 2a 2f 0a 20   to copy to */. 
1c2be 20 20 20 7d 20 61 63 3b 0a 20 20 20 20 73 74 72     } ac;.    str
1c2bf 75 63 74 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77  uct OP_ResultRow
1c2c0 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20  _stack_vars {.  
1c2c1 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20      Mem *pMem;. 
1c2c2 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20       int i;.    
1c2c3 7d 20 61 64 3b 0a 20 20 20 20 73 74 72 75 63 74  } ad;.    struct
1c2c4 20 4f 50 5f 43 6f 6e 63 61 74 5f 73 74 61 63 6b   OP_Concat_stack
1c2c5 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 36  _vars {.      i6
1c2c6 34 20 6e 42 79 74 65 3b 0a 20 20 20 20 7d 20 61  4 nByte;.    } a
1c2c7 65 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  e;.    struct OP
1c2c8 5f 52 65 6d 61 69 6e 64 65 72 5f 73 74 61 63 6b  _Remainder_stack
1c2c9 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e  _vars {.      in
1c2ca 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 2f 2a  t flags;      /*
1c2cb 20 43 6f 6d 62 69 6e 65 64 20 4d 45 4d 5f 2a 20   Combined MEM_* 
1c2cc 66 6c 61 67 73 20 66 72 6f 6d 20 62 6f 74 68 20  flags from both 
1c2cd 69 6e 70 75 74 73 20 2a 2f 0a 20 20 20 20 20 20  inputs */.      
1c2ce 69 36 34 20 69 41 3b 20 20 20 20 20 20 20 20 20  i64 iA;         
1c2cf 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65  /* Integer value
1c2d0 20 6f 66 20 6c 65 66 74 20 6f 70 65 72 61 6e 64   of left operand
1c2d1 20 2a 2f 0a 20 20 20 20 20 20 69 36 34 20 69 42   */.      i64 iB
1c2d2 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74  ;         /* Int
1c2d3 65 67 65 72 20 76 61 6c 75 65 20 6f 66 20 72 69  eger value of ri
1c2d4 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20  ght operand */. 
1c2d5 20 20 20 20 20 64 6f 75 62 6c 65 20 72 41 3b 20       double rA; 
1c2d6 20 20 20 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c       /* Real val
1c2d7 75 65 20 6f 66 20 6c 65 66 74 20 6f 70 65 72 61  ue of left opera
1c2d8 6e 64 20 2a 2f 0a 20 20 20 20 20 20 64 6f 75 62  nd */.      doub
1c2d9 6c 65 20 72 42 3b 20 20 20 20 20 20 2f 2a 20 52  le rB;      /* R
1c2da 65 61 6c 20 76 61 6c 75 65 20 6f 66 20 72 69 67  eal value of rig
1c2db 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20  ht operand */.  
1c2dc 20 20 7d 20 61 66 3b 0a 20 20 20 20 73 74 72 75    } af;.    stru
1c2dd 63 74 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 5f 73  ct OP_Function_s
1c2de 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c2df 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 4d    int i;.      M
1c2e0 65 6d 20 2a 70 41 72 67 3b 0a 20 20 20 20 20 20  em *pArg;.      
1c2e1 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1c2e2 63 74 78 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ctx;.      sqlit
1c2e3 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c  e3_value **apVal
1c2e4 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20  ;.      int n;. 
1c2e5 20 20 20 7d 20 61 67 3b 0a 20 20 20 20 73 74 72     } ag;.    str
1c2e6 75 63 74 20 4f 50 5f 53 68 69 66 74 52 69 67 68  uct OP_ShiftRigh
1c2e7 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  t_stack_vars {. 
1c2e8 20 20 20 20 20 69 36 34 20 61 3b 0a 20 20 20 20       i64 a;.    
1c2e9 20 20 69 36 34 20 62 3b 0a 20 20 20 20 7d 20 61    i64 b;.    } a
1c2ea 68 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  h;.    struct OP
1c2eb 5f 47 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  _Ge_stack_vars {
1c2ec 0a 20 20 20 20 20 20 69 6e 74 20 66 6c 61 67 73  .      int flags
1c2ed 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b  ;.      int res;
1c2ee 0a 20 20 20 20 20 20 63 68 61 72 20 61 66 66 69  .      char affi
1c2ef 6e 69 74 79 3b 0a 20 20 20 20 7d 20 61 69 3b 0a  nity;.    } ai;.
1c2f0 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 43 6f      struct OP_Co
1c2f1 6d 70 61 72 65 5f 73 74 61 63 6b 5f 76 61 72 73  mpare_stack_vars
1c2f2 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a   {.      int n;.
1c2f3 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20        int i;.   
1c2f4 20 20 20 69 6e 74 20 70 31 3b 0a 20 20 20 20 20     int p1;.     
1c2f5 20 69 6e 74 20 70 32 3b 0a 20 20 20 20 20 20 63   int p2;.      c
1c2f6 6f 6e 73 74 20 4b 65 79 49 6e 66 6f 20 2a 70 4b  onst KeyInfo *pK
1c2f7 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 69 6e  eyInfo;.      in
1c2f8 74 20 69 64 78 3b 0a 20 20 20 20 20 20 43 6f 6c  t idx;.      Col
1c2f9 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20  lSeq *pColl;    
1c2fa 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71  /* Collating seq
1c2fb 75 65 6e 63 65 20 74 6f 20 75 73 65 20 6f 6e 20  uence to use on 
1c2fc 74 68 69 73 20 74 65 72 6d 20 2a 2f 0a 20 20 20  this term */.   
1c2fd 20 20 20 69 6e 74 20 62 52 65 76 3b 20 20 20 20     int bRev;    
1c2fe 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f        /* True fo
1c2ff 72 20 44 45 53 43 45 4e 44 49 4e 47 20 73 6f 72  r DESCENDING sor
1c300 74 20 6f 72 64 65 72 20 2a 2f 0a 20 20 20 20 7d  t order */.    }
1c301 20 61 6a 3b 0a 20 20 20 20 73 74 72 75 63 74 20   aj;.    struct 
1c302 4f 50 5f 4f 72 5f 73 74 61 63 6b 5f 76 61 72 73  OP_Or_stack_vars
1c303 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 76 31 3b   {.      int v1;
1c304 20 20 20 20 2f 2a 20 4c 65 66 74 20 6f 70 65 72      /* Left oper
1c305 61 6e 64 3a 20 20 30 3d 3d 46 41 4c 53 45 2c 20  and:  0==FALSE, 
1c306 31 3d 3d 54 52 55 45 2c 20 32 3d 3d 55 4e 4b 4e  1==TRUE, 2==UNKN
1c307 4f 57 4e 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20  OWN or NULL */. 
1c308 20 20 20 20 20 69 6e 74 20 76 32 3b 20 20 20 20       int v2;    
1c309 2f 2a 20 52 69 67 68 74 20 6f 70 65 72 61 6e 64  /* Right operand
1c30a 3a 20 30 3d 3d 46 41 4c 53 45 2c 20 31 3d 3d 54  : 0==FALSE, 1==T
1c30b 52 55 45 2c 20 32 3d 3d 55 4e 4b 4e 4f 57 4e 20  RUE, 2==UNKNOWN 
1c30c 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 7d  or NULL */.    }
1c30d 20 61 6b 3b 0a 20 20 20 20 73 74 72 75 63 74 20   ak;.    struct 
1c30e 4f 50 5f 49 66 4e 6f 74 5f 73 74 61 63 6b 5f 76  OP_IfNot_stack_v
1c30f 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  ars {.      int 
1c310 63 3b 0a 20 20 20 20 7d 20 61 6c 3b 0a 20 20 20  c;.    } al;.   
1c311 20 73 74 72 75 63 74 20 4f 50 5f 43 6f 6c 75 6d   struct OP_Colum
1c312 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  n_stack_vars {. 
1c313 20 20 20 20 20 75 33 32 20 70 61 79 6c 6f 61 64       u32 payload
1c314 53 69 7a 65 3b 20 20 20 2f 2a 20 4e 75 6d 62 65  Size;   /* Numbe
1c315 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68  r of bytes in th
1c316 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20  e record */.    
1c317 20 20 69 36 34 20 70 61 79 6c 6f 61 64 53 69 7a    i64 payloadSiz
1c318 65 36 34 3b 20 2f 2a 20 4e 75 6d 62 65 72 20 6f  e64; /* Number o
1c319 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 72  f bytes in the r
1c31a 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 69  ecord */.      i
1c31b 6e 74 20 70 31 3b 20 20 20 20 20 20 20 20 20 20  nt p1;          
1c31c 20 20 2f 2a 20 50 31 20 76 61 6c 75 65 20 6f 66    /* P1 value of
1c31d 20 74 68 65 20 6f 70 63 6f 64 65 20 2a 2f 0a 20   the opcode */. 
1c31e 20 20 20 20 20 69 6e 74 20 70 32 3b 20 20 20 20       int p2;    
1c31f 20 20 20 20 20 20 20 20 2f 2a 20 63 6f 6c 75 6d          /* colum
1c320 6e 20 6e 75 6d 62 65 72 20 74 6f 20 72 65 74 72  n number to retr
1c321 69 65 76 65 20 2a 2f 0a 20 20 20 20 20 20 56 64  ieve */.      Vd
1c322 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 20 20 20  beCursor *pC;   
1c323 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 72   /* The VDBE cur
1c324 73 6f 72 20 2a 2f 0a 20 20 20 20 20 20 63 68 61  sor */.      cha
1c325 72 20 2a 7a 52 65 63 3b 20 20 20 20 20 20 20 20  r *zRec;        
1c326 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 63 6f  /* Pointer to co
1c327 6d 70 6c 65 74 65 20 72 65 63 6f 72 64 2d 64 61  mplete record-da
1c328 74 61 20 2a 2f 0a 20 20 20 20 20 20 42 74 43 75  ta */.      BtCu
1c329 72 73 6f 72 20 2a 70 43 72 73 72 3b 20 20 20 2f  rsor *pCrsr;   /
1c32a 2a 20 54 68 65 20 42 54 72 65 65 20 63 75 72 73  * The BTree curs
1c32b 6f 72 20 2a 2f 0a 20 20 20 20 20 20 75 33 32 20  or */.      u32 
1c32c 2a 61 54 79 70 65 3b 20 20 20 20 20 20 20 20 2f  *aType;        /
1c32d 2a 20 61 54 79 70 65 5b 69 5d 20 68 6f 6c 64 73  * aType[i] holds
1c32e 20 74 68 65 20 6e 75 6d 65 72 69 63 20 74 79 70   the numeric typ
1c32f 65 20 6f 66 20 74 68 65 20 69 2d 74 68 20 63 6f  e of the i-th co
1c330 6c 75 6d 6e 20 2a 2f 0a 20 20 20 20 20 20 75 33  lumn */.      u3
1c331 32 20 2a 61 4f 66 66 73 65 74 3b 20 20 20 20 20  2 *aOffset;     
1c332 20 2f 2a 20 61 4f 66 66 73 65 74 5b 69 5d 20 69   /* aOffset[i] i
1c333 73 20 6f 66 66 73 65 74 20 74 6f 20 73 74 61 72  s offset to star
1c334 74 20 6f 66 20 64 61 74 61 20 66 6f 72 20 69 2d  t of data for i-
1c335 74 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 20  th column */.   
1c336 20 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20     int nField;  
1c337 20 20 20 20 20 20 2f 2a 20 6e 75 6d 62 65 72 20        /* number 
1c338 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65  of fields in the
1c339 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20   record */.     
1c33a 20 69 6e 74 20 6c 65 6e 3b 20 20 20 20 20 20 20   int len;       
1c33b 20 20 20 20 2f 2a 20 54 68 65 20 6c 65 6e 67 74      /* The lengt
1c33c 68 20 6f 66 20 74 68 65 20 73 65 72 69 61 6c 69  h of the seriali
1c33d 7a 65 64 20 64 61 74 61 20 66 6f 72 20 74 68 65  zed data for the
1c33e 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 20 20 20   column */.     
1c33f 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
1c340 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
1c341 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 63 68 61  ter */.      cha
1c342 72 20 2a 7a 44 61 74 61 3b 20 20 20 20 20 20 20  r *zData;       
1c343 2f 2a 20 50 61 72 74 20 6f 66 20 74 68 65 20 72  /* Part of the r
1c344 65 63 6f 72 64 20 62 65 69 6e 67 20 64 65 63 6f  ecord being deco
1c345 64 65 64 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d  ded */.      Mem
1c346 20 2a 70 44 65 73 74 3b 20 20 20 20 20 20 20 20   *pDest;        
1c347 2f 2a 20 57 68 65 72 65 20 74 6f 20 77 72 69 74  /* Where to writ
1c348 65 20 74 68 65 20 65 78 74 72 61 63 74 65 64 20  e the extracted 
1c349 76 61 6c 75 65 20 2a 2f 0a 20 20 20 20 20 20 4d  value */.      M
1c34a 65 6d 20 73 4d 65 6d 3b 20 20 20 20 20 20 20 20  em sMem;        
1c34b 20 20 2f 2a 20 46 6f 72 20 73 74 6f 72 69 6e 67    /* For storing
1c34c 20 74 68 65 20 72 65 63 6f 72 64 20 62 65 69 6e   the record bein
1c34d 67 20 64 65 63 6f 64 65 64 20 2a 2f 0a 20 20 20  g decoded */.   
1c34e 20 20 20 75 38 20 2a 7a 49 64 78 3b 20 20 20 20     u8 *zIdx;    
1c34f 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69        /* Index i
1c350 6e 74 6f 20 68 65 61 64 65 72 20 2a 2f 0a 20 20  nto header */.  
1c351 20 20 20 20 75 38 20 2a 7a 45 6e 64 48 64 72 3b      u8 *zEndHdr;
1c352 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
1c353 72 20 74 6f 20 66 69 72 73 74 20 62 79 74 65 20  r to first byte 
1c354 61 66 74 65 72 20 74 68 65 20 68 65 61 64 65 72  after the header
1c355 20 2a 2f 0a 20 20 20 20 20 20 75 33 32 20 6f 66   */.      u32 of
1c356 66 73 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20  fset;        /* 
1c357 4f 66 66 73 65 74 20 69 6e 74 6f 20 74 68 65 20  Offset into the 
1c358 64 61 74 61 20 2a 2f 0a 20 20 20 20 20 20 75 36  data */.      u6
1c359 34 20 6f 66 66 73 65 74 36 34 3b 20 20 20 20 20  4 offset64;     
1c35a 20 2f 2a 20 36 34 2d 62 69 74 20 6f 66 66 73 65   /* 64-bit offse
1c35b 74 2e 20 20 36 34 20 62 69 74 73 20 6e 65 65 64  t.  64 bits need
1c35c 65 64 20 74 6f 20 63 61 74 63 68 20 6f 76 65 72  ed to catch over
1c35d 66 6c 6f 77 20 2a 2f 0a 20 20 20 20 20 20 69 6e  flow */.      in
1c35e 74 20 73 7a 48 64 72 3b 20 20 20 20 20 20 20 20  t szHdr;        
1c35f 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20   /* Size of the 
1c360 68 65 61 64 65 72 20 73 69 7a 65 20 66 69 65 6c  header size fiel
1c361 64 20 61 74 20 73 74 61 72 74 20 6f 66 20 72 65  d at start of re
1c362 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e  cord */.      in
1c363 74 20 61 76 61 69 6c 3b 20 20 20 20 20 20 20 20  t avail;        
1c364 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
1c365 74 65 73 20 6f 66 20 61 76 61 69 6c 61 62 6c 65  tes of available
1c366 20 64 61 74 61 20 2a 2f 0a 20 20 20 20 20 20 4d   data */.      M
1c367 65 6d 20 2a 70 52 65 67 3b 20 20 20 20 20 20 20  em *pReg;       
1c368 20 20 2f 2a 20 50 73 65 75 64 6f 54 61 62 6c 65    /* PseudoTable
1c369 20 69 6e 70 75 74 20 72 65 67 69 73 74 65 72 20   input register 
1c36a 2a 2f 0a 20 20 20 20 7d 20 61 6d 3b 0a 20 20 20  */.    } am;.   
1c36b 20 73 74 72 75 63 74 20 4f 50 5f 41 66 66 69 6e   struct OP_Affin
1c36c 69 74 79 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  ity_stack_vars {
1c36d 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 41 66  .      char *zAf
1c36e 66 69 6e 69 74 79 3b 20 20 20 2f 2a 20 54 68 65  finity;   /* The
1c36f 20 61 66 66 69 6e 69 74 79 20 74 6f 20 62 65 20   affinity to be 
1c370 61 70 70 6c 69 65 64 20 2a 2f 0a 20 20 20 20 20  applied */.     
1c371 20 4d 65 6d 20 2a 70 44 61 74 61 30 3b 20 20 20   Mem *pData0;   
1c372 20 20 20 20 2f 2a 20 46 69 72 73 74 20 72 65 67      /* First reg
1c373 69 73 74 65 72 20 74 6f 20 77 68 69 63 68 20 74  ister to which t
1c374 6f 20 61 70 70 6c 79 20 61 66 66 69 6e 69 74 79  o apply affinity
1c375 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70   */.      Mem *p
1c376 4c 61 73 74 3b 20 20 20 20 20 20 20 20 2f 2a 20  Last;        /* 
1c377 4c 61 73 74 20 72 65 67 69 73 74 65 72 20 74 6f  Last register to
1c378 20 77 68 69 63 68 20 74 6f 20 61 70 70 6c 79 20   which to apply 
1c379 61 66 66 69 6e 69 74 79 20 2a 2f 0a 20 20 20 20  affinity */.    
1c37a 20 20 4d 65 6d 20 2a 70 52 65 63 3b 20 20 20 20    Mem *pRec;    
1c37b 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
1c37c 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 20 20  register */.    
1c37d 7d 20 61 6e 3b 0a 20 20 20 20 73 74 72 75 63 74  } an;.    struct
1c37e 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 5f 73   OP_MakeRecord_s
1c37f 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c380 20 20 75 38 20 2a 7a 4e 65 77 52 65 63 6f 72 64    u8 *zNewRecord
1c381 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 20 62 75  ;        /* A bu
1c382 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65  ffer to hold the
1c383 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 6e 65   data for the ne
1c384 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20  w record */.    
1c385 20 20 4d 65 6d 20 2a 70 52 65 63 3b 20 20 20 20    Mem *pRec;    
1c386 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1c387 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  new record */.  
1c388 20 20 20 20 75 36 34 20 6e 44 61 74 61 3b 20 20      u64 nData;  
1c389 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
1c38a 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
1c38b 20 64 61 74 61 20 73 70 61 63 65 20 2a 2f 0a 20   data space */. 
1c38c 20 20 20 20 20 69 6e 74 20 6e 48 64 72 3b 20 20       int nHdr;  
1c38d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
1c38e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
1c38f 66 20 68 65 61 64 65 72 20 73 70 61 63 65 20 2a  f header space *
1c390 2f 0a 20 20 20 20 20 20 69 36 34 20 6e 42 79 74  /.      i64 nByt
1c391 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  e;             /
1c392 2a 20 44 61 74 61 20 73 70 61 63 65 20 72 65 71  * Data space req
1c393 75 69 72 65 64 20 66 6f 72 20 74 68 69 73 20 72  uired for this r
1c394 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 69  ecord */.      i
1c395 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20 20 20  nt nZero;       
1c396 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1c397 6f 66 20 7a 65 72 6f 20 62 79 74 65 73 20 61 74  of zero bytes at
1c398 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
1c399 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20  record */.      
1c39a 69 6e 74 20 6e 56 61 72 69 6e 74 3b 20 20 20 20  int nVarint;    
1c39b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1c39c 20 6f 66 20 62 79 74 65 73 20 69 6e 20 61 20 76   of bytes in a v
1c39d 61 72 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 75  arint */.      u
1c39e 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 3b 20  32 serial_type; 
1c39f 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 66 69        /* Type fi
1c3a0 65 6c 64 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d  eld */.      Mem
1c3a1 20 2a 70 44 61 74 61 30 3b 20 20 20 20 20 20 20   *pData0;       
1c3a2 20 20 20 20 2f 2a 20 46 69 72 73 74 20 66 69 65      /* First fie
1c3a3 6c 64 20 74 6f 20 62 65 20 63 6f 6d 62 69 6e 65  ld to be combine
1c3a4 64 20 69 6e 74 6f 20 74 68 65 20 72 65 63 6f 72  d into the recor
1c3a5 64 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a  d */.      Mem *
1c3a6 70 4c 61 73 74 3b 20 20 20 20 20 20 20 20 20 20  pLast;          
1c3a7 20 20 2f 2a 20 4c 61 73 74 20 66 69 65 6c 64 20    /* Last field 
1c3a8 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f  of the record */
1c3a9 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 69 65 6c  .      int nFiel
1c3aa 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  d;            /*
1c3ab 20 4e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64   Number of field
1c3ac 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20  s in the record 
1c3ad 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  */.      char *z
1c3ae 41 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 20  Affinity;       
1c3af 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79 20  /* The affinity 
1c3b0 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65 20 72  string for the r
1c3b1 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 69  ecord */.      i
1c3b2 6e 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3b 20  nt file_format; 
1c3b3 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 66 6f        /* File fo
1c3b4 72 6d 61 74 20 74 6f 20 75 73 65 20 66 6f 72 20  rmat to use for 
1c3b5 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 20 20  encoding */.    
1c3b6 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
1c3b7 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63           /* Spac
1c3b8 65 20 75 73 65 64 20 69 6e 20 7a 4e 65 77 52 65  e used in zNewRe
1c3b9 63 6f 72 64 5b 5d 20 2a 2f 0a 20 20 20 20 20 20  cord[] */.      
1c3ba 69 6e 74 20 6c 65 6e 3b 20 20 20 20 20 20 20 20  int len;        
1c3bb 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68         /* Length
1c3bc 20 6f 66 20 61 20 66 69 65 6c 64 20 2a 2f 0a 20   of a field */. 
1c3bd 20 20 20 7d 20 61 6f 3b 0a 20 20 20 20 73 74 72     } ao;.    str
1c3be 75 63 74 20 4f 50 5f 43 6f 75 6e 74 5f 73 74 61  uct OP_Count_sta
1c3bf 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c3c0 69 36 34 20 6e 45 6e 74 72 79 3b 0a 20 20 20 20  i64 nEntry;.    
1c3c1 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73    BtCursor *pCrs
1c3c2 72 3b 0a 20 20 20 20 7d 20 61 70 3b 0a 20 20 20  r;.    } ap;.   
1c3c3 20 73 74 72 75 63 74 20 4f 50 5f 53 61 76 65 70   struct OP_Savep
1c3c4 6f 69 6e 74 5f 73 74 61 63 6b 5f 76 61 72 73 20  oint_stack_vars 
1c3c5 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 31 3b 20  {.      int p1; 
1c3c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c3c7 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65          /* Value
1c3c8 20 6f 66 20 50 31 20 6f 70 65 72 61 6e 64 20 2a   of P1 operand *
1c3c9 2f 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e  /.      char *zN
1c3ca 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ame;            
1c3cb 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
1c3cc 6f 66 20 73 61 76 65 70 6f 69 6e 74 20 2a 2f 0a  of savepoint */.
1c3cd 20 20 20 20 20 20 69 6e 74 20 6e 4e 61 6d 65 3b        int nName;
1c3ce 0a 20 20 20 20 20 20 53 61 76 65 70 6f 69 6e 74  .      Savepoint
1c3cf 20 2a 70 4e 65 77 3b 0a 20 20 20 20 20 20 53 61   *pNew;.      Sa
1c3d0 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 70 6f  vepoint *pSavepo
1c3d1 69 6e 74 3b 0a 20 20 20 20 20 20 53 61 76 65 70  int;.      Savep
1c3d2 6f 69 6e 74 20 2a 70 54 6d 70 3b 0a 20 20 20 20  oint *pTmp;.    
1c3d3 20 20 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74    int iSavepoint
1c3d4 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 69 3b 0a  ;.      int ii;.
1c3d5 20 20 20 20 7d 20 61 71 3b 0a 20 20 20 20 73 74      } aq;.    st
1c3d6 72 75 63 74 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d  ruct OP_AutoComm
1c3d7 69 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  it_stack_vars {.
1c3d8 20 20 20 20 20 20 69 6e 74 20 64 65 73 69 72 65        int desire
1c3d9 64 41 75 74 6f 43 6f 6d 6d 69 74 3b 0a 20 20 20  dAutoCommit;.   
1c3da 20 20 20 69 6e 74 20 69 52 6f 6c 6c 62 61 63 6b     int iRollback
1c3db 3b 0a 20 20 20 20 20 20 69 6e 74 20 74 75 72 6e  ;.      int turn
1c3dc 4f 6e 41 43 3b 0a 20 20 20 20 7d 20 61 72 3b 0a  OnAC;.    } ar;.
1c3dd 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 54 72      struct OP_Tr
1c3de 61 6e 73 61 63 74 69 6f 6e 5f 73 74 61 63 6b 5f  ansaction_stack_
1c3df 76 61 72 73 20 7b 0a 20 20 20 20 20 20 42 74 72  vars {.      Btr
1c3e0 65 65 20 2a 70 42 74 3b 0a 20 20 20 20 7d 20 61  ee *pBt;.    } a
1c3e1 73 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  s;.    struct OP
1c3e2 5f 52 65 61 64 43 6f 6f 6b 69 65 5f 73 74 61 63  _ReadCookie_stac
1c3e3 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69  k_vars {.      i
1c3e4 6e 74 20 69 4d 65 74 61 3b 0a 20 20 20 20 20 20  nt iMeta;.      
1c3e5 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 20 20 69  int iDb;.      i
1c3e6 6e 74 20 69 43 6f 6f 6b 69 65 3b 0a 20 20 20 20  nt iCookie;.    
1c3e7 7d 20 61 74 3b 0a 20 20 20 20 73 74 72 75 63 74  } at;.    struct
1c3e8 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 5f 73 74   OP_SetCookie_st
1c3e9 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
1c3ea 20 44 62 20 2a 70 44 62 3b 0a 20 20 20 20 7d 20   Db *pDb;.    } 
1c3eb 61 75 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f  au;.    struct O
1c3ec 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 5f 73  P_VerifyCookie_s
1c3ed 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c3ee 20 20 69 6e 74 20 69 4d 65 74 61 3b 0a 20 20 20    int iMeta;.   
1c3ef 20 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 20     Btree *pBt;. 
1c3f0 20 20 20 7d 20 61 76 3b 0a 20 20 20 20 73 74 72     } av;.    str
1c3f1 75 63 74 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65  uct OP_OpenWrite
1c3f2 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20  _stack_vars {.  
1c3f3 20 20 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 0a      int nField;.
1c3f4 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70        KeyInfo *p
1c3f5 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 69  KeyInfo;.      i
1c3f6 6e 74 20 70 32 3b 0a 20 20 20 20 20 20 69 6e 74  nt p2;.      int
1c3f7 20 69 44 62 3b 0a 20 20 20 20 20 20 69 6e 74 20   iDb;.      int 
1c3f8 77 72 46 6c 61 67 3b 0a 20 20 20 20 20 20 42 74  wrFlag;.      Bt
1c3f9 72 65 65 20 2a 70 58 3b 0a 20 20 20 20 20 20 56  ree *pX;.      V
1c3fa 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 3b  dbeCursor *pCur;
1c3fb 0a 20 20 20 20 20 20 44 62 20 2a 70 44 62 3b 0a  .      Db *pDb;.
1c3fc 20 20 20 20 7d 20 61 77 3b 0a 20 20 20 20 73 74      } aw;.    st
1c3fd 72 75 63 74 20 4f 50 5f 4f 70 65 6e 45 70 68 65  ruct OP_OpenEphe
1c3fe 6d 65 72 61 6c 5f 73 74 61 63 6b 5f 76 61 72 73  meral_stack_vars
1c3ff 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72   {.      VdbeCur
1c400 73 6f 72 20 2a 70 43 78 3b 0a 20 20 20 20 7d 20  sor *pCx;.    } 
1c401 61 78 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f  ax;.    struct O
1c402 50 5f 4f 70 65 6e 50 73 65 75 64 6f 5f 73 74 61  P_OpenPseudo_sta
1c403 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c404 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b  VdbeCursor *pCx;
1c405 0a 20 20 20 20 7d 20 61 79 3b 0a 20 20 20 20 73  .    } ay;.    s
1c406 74 72 75 63 74 20 4f 50 5f 53 65 65 6b 47 74 5f  truct OP_SeekGt_
1c407 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
1c408 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20     int res;.    
1c409 20 20 69 6e 74 20 6f 63 3b 0a 20 20 20 20 20 20    int oc;.      
1c40a 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a  VdbeCursor *pC;.
1c40b 20 20 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65        UnpackedRe
1c40c 63 6f 72 64 20 72 3b 0a 20 20 20 20 20 20 69 6e  cord r;.      in
1c40d 74 20 6e 46 69 65 6c 64 3b 0a 20 20 20 20 20 20  t nField;.      
1c40e 69 36 34 20 69 4b 65 79 3b 20 20 20 20 20 20 2f  i64 iKey;      /
1c40f 2a 20 54 68 65 20 72 6f 77 69 64 20 77 65 20 61  * The rowid we a
1c410 72 65 20 74 6f 20 73 65 65 6b 20 74 6f 20 2a 2f  re to seek to */
1c411 0a 20 20 20 20 7d 20 61 7a 3b 0a 20 20 20 20 73  .    } az;.    s
1c412 74 72 75 63 74 20 4f 50 5f 53 65 65 6b 5f 73 74  truct OP_Seek_st
1c413 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
1c414 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b   VdbeCursor *pC;
1c415 0a 20 20 20 20 7d 20 62 61 3b 0a 20 20 20 20 73  .    } ba;.    s
1c416 74 72 75 63 74 20 4f 50 5f 46 6f 75 6e 64 5f 73  truct OP_Found_s
1c417 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c418 20 20 69 6e 74 20 61 6c 72 65 61 64 79 45 78 69    int alreadyExi
1c419 73 74 73 3b 0a 20 20 20 20 20 20 56 64 62 65 43  sts;.      VdbeC
1c41a 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20  ursor *pC;.     
1c41b 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 20   int res;.      
1c41c 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a  UnpackedRecord *
1c41d 70 49 64 78 4b 65 79 3b 0a 20 20 20 20 20 20 63  pIdxKey;.      c
1c41e 68 61 72 20 61 54 65 6d 70 52 65 63 5b 52 4f 55  har aTempRec[ROU
1c41f 4e 44 38 28 73 69 7a 65 6f 66 28 55 6e 70 61 63  ND8(sizeof(Unpac
1c420 6b 65 64 52 65 63 6f 72 64 29 29 20 2b 20 73 69  kedRecord)) + si
1c421 7a 65 6f 66 28 4d 65 6d 29 2a 33 20 2b 20 37 5d  zeof(Mem)*3 + 7]
1c422 3b 0a 20 20 20 20 7d 20 62 62 3b 0a 20 20 20 20  ;.    } bb;.    
1c423 73 74 72 75 63 74 20 4f 50 5f 49 73 55 6e 69 71  struct OP_IsUniq
1c424 75 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  ue_stack_vars {.
1c425 20 20 20 20 20 20 75 31 36 20 69 69 3b 0a 20 20        u16 ii;.  
1c426 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a      VdbeCursor *
1c427 70 43 78 3b 0a 20 20 20 20 20 20 42 74 43 75 72  pCx;.      BtCur
1c428 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 20 20  sor *pCrsr;.    
1c429 20 20 75 31 36 20 6e 46 69 65 6c 64 3b 0a 20 20    u16 nField;.  
1c42a 20 20 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b 0a 20      Mem *aMem;. 
1c42b 20 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63       UnpackedRec
1c42c 6f 72 64 20 72 3b 20 20 20 20 20 20 20 20 20 20  ord r;          
1c42d 20 20 20 20 20 20 20 20 2f 2a 20 42 2d 54 72 65          /* B-Tre
1c42e 65 20 69 6e 64 65 78 20 73 65 61 72 63 68 20 6b  e index search k
1c42f 65 79 20 2a 2f 0a 20 20 20 20 20 20 69 36 34 20  ey */.      i64 
1c430 52 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  R;              
1c431 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1c432 2a 20 52 6f 77 69 64 20 73 74 6f 72 65 64 20 69  * Rowid stored i
1c433 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 2a 2f  n register P3 */
1c434 0a 20 20 20 20 7d 20 62 63 3b 0a 20 20 20 20 73  .    } bc;.    s
1c435 74 72 75 63 74 20 4f 50 5f 4e 6f 74 45 78 69 73  truct OP_NotExis
1c436 74 73 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  ts_stack_vars {.
1c437 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72        VdbeCursor
1c438 20 2a 70 43 3b 0a 20 20 20 20 20 20 42 74 43 75   *pC;.      BtCu
1c439 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 20  rsor *pCrsr;.   
1c43a 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20     int res;.    
1c43b 20 20 75 36 34 20 69 4b 65 79 3b 0a 20 20 20 20    u64 iKey;.    
1c43c 7d 20 62 64 3b 0a 20 20 20 20 73 74 72 75 63 74  } bd;.    struct
1c43d 20 4f 50 5f 4e 65 77 52 6f 77 69 64 5f 73 74 61   OP_NewRowid_sta
1c43e 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c43f 69 36 34 20 76 3b 20 20 20 20 20 20 20 20 20 20  i64 v;          
1c440 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65         /* The ne
1c441 77 20 72 6f 77 69 64 20 2a 2f 0a 20 20 20 20 20  w rowid */.     
1c442 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b   VdbeCursor *pC;
1c443 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f          /* Curso
1c444 72 20 6f 66 20 74 61 62 6c 65 20 74 6f 20 67 65  r of table to ge
1c445 74 20 74 68 65 20 6e 65 77 20 72 6f 77 69 64 20  t the new rowid 
1c446 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73  */.      int res
1c447 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1c448 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 6e 20  /* Result of an 
1c449 73 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73 74  sqlite3BtreeLast
1c44a 28 29 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  () */.      int 
1c44b 63 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  cnt;            
1c44c 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 20 74 6f     /* Counter to
1c44d 20 6c 69 6d 69 74 20 74 68 65 20 6e 75 6d 62 65   limit the numbe
1c44e 72 20 6f 66 20 73 65 61 72 63 68 65 73 20 2a 2f  r of searches */
1c44f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d  .      Mem *pMem
1c450 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
1c451 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e   Register holdin
1c452 67 20 6c 61 72 67 65 73 74 20 72 6f 77 69 64 20  g largest rowid 
1c453 66 6f 72 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e  for AUTOINCREMEN
1c454 54 20 2a 2f 0a 20 20 20 20 20 20 56 64 62 65 46  T */.      VdbeF
1c455 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20 20  rame *pFrame;   
1c456 20 20 2f 2a 20 52 6f 6f 74 20 66 72 61 6d 65 20    /* Root frame 
1c457 6f 66 20 56 44 42 45 20 2a 2f 0a 20 20 20 20 7d  of VDBE */.    }
1c458 20 62 65 3b 0a 20 20 20 20 73 74 72 75 63 74 20   be;.    struct 
1c459 4f 50 5f 49 6e 73 65 72 74 5f 73 74 61 63 6b 5f  OP_Insert_stack_
1c45a 76 61 72 73 20 7b 0a 20 20 20 20 20 20 4d 65 6d  vars {.      Mem
1c45b 20 2a 70 44 61 74 61 3b 20 20 20 20 20 20 20 2f   *pData;       /
1c45c 2a 20 4d 45 4d 20 63 65 6c 6c 20 68 6f 6c 64 69  * MEM cell holdi
1c45d 6e 67 20 64 61 74 61 20 66 6f 72 20 74 68 65 20  ng data for the 
1c45e 72 65 63 6f 72 64 20 74 6f 20 62 65 20 69 6e 73  record to be ins
1c45f 65 72 74 65 64 20 2a 2f 0a 20 20 20 20 20 20 4d  erted */.      M
1c460 65 6d 20 2a 70 4b 65 79 3b 20 20 20 20 20 20 20  em *pKey;       
1c461 20 2f 2a 20 4d 45 4d 20 63 65 6c 6c 20 68 6f 6c   /* MEM cell hol
1c462 64 69 6e 67 20 6b 65 79 20 20 66 6f 72 20 74 68  ding key  for th
1c463 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20  e record */.    
1c464 20 20 69 36 34 20 69 4b 65 79 3b 20 20 20 20 20    i64 iKey;     
1c465 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 74 65 67      /* The integ
1c466 65 72 20 52 4f 57 49 44 20 6f 72 20 6b 65 79 20  er ROWID or key 
1c467 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 74  for the record t
1c468 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f  o be inserted */
1c469 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f  .      VdbeCurso
1c46a 72 20 2a 70 43 3b 20 20 20 2f 2a 20 43 75 72 73  r *pC;   /* Curs
1c46b 6f 72 20 74 6f 20 74 61 62 6c 65 20 69 6e 74 6f  or to table into
1c46c 20 77 68 69 63 68 20 69 6e 73 65 72 74 20 69 73   which insert is
1c46d 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 20 20   written */.    
1c46e 20 20 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20    int nZero;    
1c46f 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1c470 20 7a 65 72 6f 2d 62 79 74 65 73 20 74 6f 20 61   zero-bytes to a
1c471 70 70 65 6e 64 20 2a 2f 0a 20 20 20 20 20 20 69  ppend */.      i
1c472 6e 74 20 73 65 65 6b 52 65 73 75 6c 74 3b 20 20  nt seekResult;  
1c473 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 70 72   /* Result of pr
1c474 69 6f 72 20 73 65 65 6b 20 6f 72 20 30 20 69 66  ior seek or 0 if
1c475 20 6e 6f 20 55 53 45 53 45 45 4b 52 45 53 55 4c   no USESEEKRESUL
1c476 54 20 66 6c 61 67 20 2a 2f 0a 20 20 20 20 20 20  T flag */.      
1c477 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b  const char *zDb;
1c478 20 20 2f 2a 20 64 61 74 61 62 61 73 65 20 6e 61    /* database na
1c479 6d 65 20 2d 20 75 73 65 64 20 62 79 20 74 68 65  me - used by the
1c47a 20 75 70 64 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a   update hook */.
1c47b 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
1c47c 20 2a 7a 54 62 6c 3b 20 2f 2a 20 54 61 62 6c 65   *zTbl; /* Table
1c47d 20 6e 61 6d 65 20 2d 20 75 73 65 64 20 62 79 20   name - used by 
1c47e 74 68 65 20 6f 70 64 61 74 65 20 68 6f 6f 6b 20  the opdate hook 
1c47f 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6f 70 3b  */.      int op;
1c480 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70             /* Op
1c481 63 6f 64 65 20 66 6f 72 20 75 70 64 61 74 65 20  code for update 
1c482 68 6f 6f 6b 3a 20 53 51 4c 49 54 45 5f 55 50 44  hook: SQLITE_UPD
1c483 41 54 45 20 6f 72 20 53 51 4c 49 54 45 5f 49 4e  ATE or SQLITE_IN
1c484 53 45 52 54 20 2a 2f 0a 20 20 20 20 7d 20 62 66  SERT */.    } bf
1c485 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c486 44 65 6c 65 74 65 5f 73 74 61 63 6b 5f 76 61 72  Delete_stack_var
1c487 73 20 7b 0a 20 20 20 20 20 20 69 36 34 20 69 4b  s {.      i64 iK
1c488 65 79 3b 0a 20 20 20 20 20 20 56 64 62 65 43 75  ey;.      VdbeCu
1c489 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 7d 20  rsor *pC;.    } 
1c48a 62 67 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f  bg;.    struct O
1c48b 50 5f 52 6f 77 44 61 74 61 5f 73 74 61 63 6b 5f  P_RowData_stack_
1c48c 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62  vars {.      Vdb
1c48d 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20  eCursor *pC;.   
1c48e 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72     BtCursor *pCr
1c48f 73 72 3b 0a 20 20 20 20 20 20 75 33 32 20 6e 3b  sr;.      u32 n;
1c490 0a 20 20 20 20 20 20 69 36 34 20 6e 36 34 3b 0a  .      i64 n64;.
1c491 20 20 20 20 7d 20 62 68 3b 0a 20 20 20 20 73 74      } bh;.    st
1c492 72 75 63 74 20 4f 50 5f 52 6f 77 69 64 5f 73 74  ruct OP_Rowid_st
1c493 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
1c494 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b   VdbeCursor *pC;
1c495 0a 20 20 20 20 20 20 69 36 34 20 76 3b 0a 20 20  .      i64 v;.  
1c496 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62      sqlite3_vtab
1c497 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 20 63   *pVtab;.      c
1c498 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64  onst sqlite3_mod
1c499 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20  ule *pModule;.  
1c49a 20 20 7d 20 62 69 3b 0a 20 20 20 20 73 74 72 75    } bi;.    stru
1c49b 63 74 20 4f 50 5f 4e 75 6c 6c 52 6f 77 5f 73 74  ct OP_NullRow_st
1c49c 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
1c49d 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b   VdbeCursor *pC;
1c49e 0a 20 20 20 20 7d 20 62 6a 3b 0a 20 20 20 20 73  .    } bj;.    s
1c49f 74 72 75 63 74 20 4f 50 5f 4c 61 73 74 5f 73 74  truct OP_Last_st
1c4a0 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
1c4a1 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b   VdbeCursor *pC;
1c4a2 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 20  .      BtCursor 
1c4a3 2a 70 43 72 73 72 3b 0a 20 20 20 20 20 20 69 6e  *pCrsr;.      in
1c4a4 74 20 72 65 73 3b 0a 20 20 20 20 7d 20 62 6b 3b  t res;.    } bk;
1c4a5 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 52  .    struct OP_R
1c4a6 65 77 69 6e 64 5f 73 74 61 63 6b 5f 76 61 72 73  ewind_stack_vars
1c4a7 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72   {.      VdbeCur
1c4a8 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 42  sor *pC;.      B
1c4a9 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a  tCursor *pCrsr;.
1c4aa 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20        int res;. 
1c4ab 20 20 20 7d 20 62 6c 3b 0a 20 20 20 20 73 74 72     } bl;.    str
1c4ac 75 63 74 20 4f 50 5f 4e 65 78 74 5f 73 74 61 63  uct OP_Next_stac
1c4ad 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56  k_vars {.      V
1c4ae 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20  dbeCursor *pC;. 
1c4af 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70       BtCursor *p
1c4b0 43 72 73 72 3b 0a 20 20 20 20 20 20 69 6e 74 20  Crsr;.      int 
1c4b1 72 65 73 3b 0a 20 20 20 20 7d 20 62 6d 3b 0a 20  res;.    } bm;. 
1c4b2 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 64 78     struct OP_Idx
1c4b3 49 6e 73 65 72 74 5f 73 74 61 63 6b 5f 76 61 72  Insert_stack_var
1c4b4 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75  s {.      VdbeCu
1c4b5 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20  rsor *pC;.      
1c4b6 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b  BtCursor *pCrsr;
1c4b7 0a 20 20 20 20 20 20 69 6e 74 20 6e 4b 65 79 3b  .      int nKey;
1c4b8 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61  .      const cha
1c4b9 72 20 2a 7a 4b 65 79 3b 0a 20 20 20 20 7d 20 62  r *zKey;.    } b
1c4ba 6e 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  n;.    struct OP
1c4bb 5f 49 64 78 44 65 6c 65 74 65 5f 73 74 61 63 6b  _IdxDelete_stack
1c4bc 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64  _vars {.      Vd
1c4bd 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20  beCursor *pC;.  
1c4be 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43      BtCursor *pC
1c4bf 72 73 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 72  rsr;.      int r
1c4c0 65 73 3b 0a 20 20 20 20 20 20 55 6e 70 61 63 6b  es;.      Unpack
1c4c1 65 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 20 20  edRecord r;.    
1c4c2 7d 20 62 6f 3b 0a 20 20 20 20 73 74 72 75 63 74  } bo;.    struct
1c4c3 20 4f 50 5f 49 64 78 52 6f 77 69 64 5f 73 74 61   OP_IdxRowid_sta
1c4c4 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c4c5 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b  BtCursor *pCrsr;
1c4c6 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f  .      VdbeCurso
1c4c7 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 69 36 34  r *pC;.      i64
1c4c8 20 72 6f 77 69 64 3b 0a 20 20 20 20 7d 20 62 70   rowid;.    } bp
1c4c9 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c4ca 49 64 78 47 45 5f 73 74 61 63 6b 5f 76 61 72 73  IdxGE_stack_vars
1c4cb 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72   {.      VdbeCur
1c4cc 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 69  sor *pC;.      i
1c4cd 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 20 55 6e  nt res;.      Un
1c4ce 70 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a  packedRecord r;.
1c4cf 20 20 20 20 7d 20 62 71 3b 0a 20 20 20 20 73 74      } bq;.    st
1c4d0 72 75 63 74 20 4f 50 5f 44 65 73 74 72 6f 79 5f  ruct OP_Destroy_
1c4d1 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
1c4d2 20 20 20 69 6e 74 20 69 4d 6f 76 65 64 3b 0a 20     int iMoved;. 
1c4d3 20 20 20 20 20 69 6e 74 20 69 43 6e 74 3b 0a 20       int iCnt;. 
1c4d4 20 20 20 20 20 56 64 62 65 20 2a 70 56 64 62 65       Vdbe *pVdbe
1c4d5 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b  ;.      int iDb;
1c4d6 0a 20 20 20 20 7d 20 62 72 3b 0a 20 20 20 20 73  .    } br;.    s
1c4d7 74 72 75 63 74 20 4f 50 5f 43 6c 65 61 72 5f 73  truct OP_Clear_s
1c4d8 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c4d9 20 20 69 6e 74 20 6e 43 68 61 6e 67 65 3b 0a 20    int nChange;. 
1c4da 20 20 20 7d 20 62 73 3b 0a 20 20 20 20 73 74 72     } bs;.    str
1c4db 75 63 74 20 4f 50 5f 43 72 65 61 74 65 54 61 62  uct OP_CreateTab
1c4dc 6c 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  le_stack_vars {.
1c4dd 20 20 20 20 20 20 69 6e 74 20 70 67 6e 6f 3b 0a        int pgno;.
1c4de 20 20 20 20 20 20 69 6e 74 20 66 6c 61 67 73 3b        int flags;
1c4df 0a 20 20 20 20 20 20 44 62 20 2a 70 44 62 3b 0a  .      Db *pDb;.
1c4e0 20 20 20 20 7d 20 62 74 3b 0a 20 20 20 20 73 74      } bt;.    st
1c4e1 72 75 63 74 20 4f 50 5f 50 61 72 73 65 53 63 68  ruct OP_ParseSch
1c4e2 65 6d 61 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  ema_stack_vars {
1c4e3 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a  .      int iDb;.
1c4e4 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
1c4e5 20 2a 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 20   *zMaster;.     
1c4e6 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 20   char *zSql;.   
1c4e7 20 20 20 49 6e 69 74 44 61 74 61 20 69 6e 69 74     InitData init
1c4e8 44 61 74 61 3b 0a 20 20 20 20 7d 20 62 75 3b 0a  Data;.    } bu;.
1c4e9 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 6e      struct OP_In
1c4ea 74 65 67 72 69 74 79 43 6b 5f 73 74 61 63 6b 5f  tegrityCk_stack_
1c4eb 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74  vars {.      int
1c4ec 20 6e 52 6f 6f 74 3b 20 20 20 20 20 20 2f 2a 20   nRoot;      /* 
1c4ed 4e 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73  Number of tables
1c4ee 20 74 6f 20 63 68 65 63 6b 2e 20 20 28 4e 75 6d   to check.  (Num
1c4ef 62 65 72 20 6f 66 20 72 6f 6f 74 20 70 61 67 65  ber of root page
1c4f0 73 2e 29 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  s.) */.      int
1c4f1 20 2a 61 52 6f 6f 74 3b 20 20 20 20 20 2f 2a 20   *aRoot;     /* 
1c4f2 41 72 72 61 79 20 6f 66 20 72 6f 6f 74 70 61 67  Array of rootpag
1c4f3 65 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 74 61  e numbers for ta
1c4f4 62 6c 65 73 20 74 6f 20 62 65 20 63 68 65 63 6b  bles to be check
1c4f5 65 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  ed */.      int 
1c4f6 6a 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  j;          /* L
1c4f7 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
1c4f8 20 20 20 20 20 69 6e 74 20 6e 45 72 72 3b 20 20       int nErr;  
1c4f9 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1c4fa 66 20 65 72 72 6f 72 73 20 72 65 70 6f 72 74 65  f errors reporte
1c4fb 64 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20  d */.      char 
1c4fc 2a 7a 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 65  *z;        /* Te
1c4fd 78 74 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20  xt of the error 
1c4fe 72 65 70 6f 72 74 20 2a 2f 0a 20 20 20 20 20 20  report */.      
1c4ff 4d 65 6d 20 2a 70 6e 45 72 72 3b 20 20 20 20 20  Mem *pnErr;     
1c500 2f 2a 20 52 65 67 69 73 74 65 72 20 6b 65 65 70  /* Register keep
1c501 69 6e 67 20 74 72 61 63 6b 20 6f 66 20 65 72 72  ing track of err
1c502 6f 72 73 20 72 65 6d 61 69 6e 69 6e 67 20 2a 2f  ors remaining */
1c503 0a 20 20 20 20 7d 20 62 76 3b 0a 20 20 20 20 73  .    } bv;.    s
1c504 74 72 75 63 74 20 4f 50 5f 52 6f 77 53 65 74 41  truct OP_RowSetA
1c505 64 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  dd_stack_vars {.
1c506 20 20 20 20 20 20 4d 65 6d 20 2a 70 49 64 78 3b        Mem *pIdx;
1c507 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 56 61 6c  .      Mem *pVal
1c508 3b 0a 20 20 20 20 7d 20 62 77 3b 0a 20 20 20 20  ;.    } bw;.    
1c509 73 74 72 75 63 74 20 4f 50 5f 52 6f 77 53 65 74  struct OP_RowSet
1c50a 52 65 61 64 5f 73 74 61 63 6b 5f 76 61 72 73 20  Read_stack_vars 
1c50b 7b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 49 64  {.      Mem *pId
1c50c 78 3b 0a 20 20 20 20 20 20 69 36 34 20 76 61 6c  x;.      i64 val
1c50d 3b 0a 20 20 20 20 7d 20 62 78 3b 0a 20 20 20 20  ;.    } bx;.    
1c50e 73 74 72 75 63 74 20 4f 50 5f 52 6f 77 53 65 74  struct OP_RowSet
1c50f 54 65 73 74 5f 73 74 61 63 6b 5f 76 61 72 73 20  Test_stack_vars 
1c510 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 53 65 74  {.      int iSet
1c511 3b 0a 20 20 20 20 20 20 69 6e 74 20 65 78 69 73  ;.      int exis
1c512 74 73 3b 0a 20 20 20 20 7d 20 62 79 3b 0a 20 20  ts;.    } by;.  
1c513 20 20 73 74 72 75 63 74 20 4f 50 5f 50 72 6f 67    struct OP_Prog
1c514 72 61 6d 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  ram_stack_vars {
1c515 0a 20 20 20 20 20 20 69 6e 74 20 6e 4d 65 6d 3b  .      int nMem;
1c516 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1c517 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f  * Number of memo
1c518 72 79 20 72 65 67 69 73 74 65 72 73 20 66 6f 72  ry registers for
1c519 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a   sub-program */.
1c51a 20 20 20 20 20 20 69 6e 74 20 6e 42 79 74 65 3b        int nByte;
1c51b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1c51c 20 42 79 74 65 73 20 6f 66 20 72 75 6e 74 69 6d   Bytes of runtim
1c51d 65 20 73 70 61 63 65 20 72 65 71 75 69 72 65 64  e space required
1c51e 20 66 6f 72 20 73 75 62 2d 70 72 6f 67 72 61 6d   for sub-program
1c51f 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70   */.      Mem *p
1c520 52 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Rt;             
1c521 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74 6f    /* Register to
1c522 20 61 6c 6c 6f 63 61 74 65 20 72 75 6e 74 69 6d   allocate runtim
1c523 65 20 73 70 61 63 65 20 2a 2f 0a 20 20 20 20 20  e space */.     
1c524 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20   Mem *pMem;     
1c525 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64           /* Used
1c526 20 74 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f   to iterate thro
1c527 75 67 68 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73  ugh memory cells
1c528 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70   */.      Mem *p
1c529 45 6e 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  End;            
1c52a 20 20 2f 2a 20 4c 61 73 74 20 6d 65 6d 6f 72 79    /* Last memory
1c52b 20 63 65 6c 6c 20 69 6e 20 6e 65 77 20 61 72 72   cell in new arr
1c52c 61 79 20 2a 2f 0a 20 20 20 20 20 20 56 64 62 65  ay */.      Vdbe
1c52d 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20  Frame *pFrame;  
1c52e 20 20 20 20 2f 2a 20 4e 65 77 20 76 64 62 65 20      /* New vdbe 
1c52f 66 72 61 6d 65 20 74 6f 20 65 78 65 63 75 74 65  frame to execute
1c530 20 69 6e 20 2a 2f 0a 20 20 20 20 20 20 53 75 62   in */.      Sub
1c531 50 72 6f 67 72 61 6d 20 2a 70 50 72 6f 67 72 61  Program *pProgra
1c532 6d 3b 20 20 20 2f 2a 20 53 75 62 2d 70 72 6f 67  m;   /* Sub-prog
1c533 72 61 6d 20 74 6f 20 65 78 65 63 75 74 65 20 2a  ram to execute *
1c534 2f 0a 20 20 20 20 20 20 76 6f 69 64 20 2a 74 3b  /.      void *t;
1c535 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c536 2f 2a 20 54 6f 6b 65 6e 20 69 64 65 6e 74 69 66  /* Token identif
1c537 79 69 6e 67 20 74 72 69 67 67 65 72 20 2a 2f 0a  ying trigger */.
1c538 20 20 20 20 7d 20 62 7a 3b 0a 20 20 20 20 73 74      } bz;.    st
1c539 72 75 63 74 20 4f 50 5f 50 61 72 61 6d 5f 73 74  ruct OP_Param_st
1c53a 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
1c53b 20 56 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61   VdbeFrame *pFra
1c53c 6d 65 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70  me;.      Mem *p
1c53d 49 6e 3b 0a 20 20 20 20 7d 20 63 61 3b 0a 20 20  In;.    } ca;.  
1c53e 20 20 73 74 72 75 63 74 20 4f 50 5f 4d 65 6d 4d    struct OP_MemM
1c53f 61 78 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  ax_stack_vars {.
1c540 20 20 20 20 20 20 4d 65 6d 20 2a 70 49 6e 31 3b        Mem *pIn1;
1c541 0a 20 20 20 20 20 20 56 64 62 65 46 72 61 6d 65  .      VdbeFrame
1c542 20 2a 70 46 72 61 6d 65 3b 0a 20 20 20 20 7d 20   *pFrame;.    } 
1c543 63 62 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f  cb;.    struct O
1c544 50 5f 41 67 67 53 74 65 70 5f 73 74 61 63 6b 5f  P_AggStep_stack_
1c545 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74  vars {.      int
1c546 20 6e 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b   n;.      int i;
1c547 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d  .      Mem *pMem
1c548 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 52 65  ;.      Mem *pRe
1c549 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  c;.      sqlite3
1c54a 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b 0a 20 20  _context ctx;.  
1c54b 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75      sqlite3_valu
1c54c 65 20 2a 2a 61 70 56 61 6c 3b 0a 20 20 20 20 7d  e **apVal;.    }
1c54d 20 63 63 3b 0a 20 20 20 20 73 74 72 75 63 74 20   cc;.    struct 
1c54e 4f 50 5f 41 67 67 46 69 6e 61 6c 5f 73 74 61 63  OP_AggFinal_stac
1c54f 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 4d  k_vars {.      M
1c550 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 20 20 7d 20  em *pMem;.    } 
1c551 63 64 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f  cd;.    struct O
1c552 50 5f 49 6e 63 72 56 61 63 75 75 6d 5f 73 74 61  P_IncrVacuum_sta
1c553 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c554 42 74 72 65 65 20 2a 70 42 74 3b 0a 20 20 20 20  Btree *pBt;.    
1c555 7d 20 63 65 3b 0a 20 20 20 20 73 74 72 75 63 74  } ce;.    struct
1c556 20 4f 50 5f 56 42 65 67 69 6e 5f 73 74 61 63 6b   OP_VBegin_stack
1c557 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 54  _vars {.      VT
1c558 61 62 6c 65 20 2a 70 56 54 61 62 3b 0a 20 20 20  able *pVTab;.   
1c559 20 7d 20 63 66 3b 0a 20 20 20 20 73 74 72 75 63   } cf;.    struc
1c55a 74 20 4f 50 5f 56 4f 70 65 6e 5f 73 74 61 63 6b  t OP_VOpen_stack
1c55b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64  _vars {.      Vd
1c55c 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a  beCursor *pCur;.
1c55d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74        sqlite3_vt
1c55e 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62  ab_cursor *pVtab
1c55f 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 73 71  Cursor;.      sq
1c560 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
1c561 62 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  b;.      sqlite3
1c562 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65  _module *pModule
1c563 3b 0a 20 20 20 20 7d 20 63 67 3b 0a 20 20 20 20  ;.    } cg;.    
1c564 73 74 72 75 63 74 20 4f 50 5f 56 46 69 6c 74 65  struct OP_VFilte
1c565 72 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  r_stack_vars {. 
1c566 20 20 20 20 20 69 6e 74 20 6e 41 72 67 3b 0a 20       int nArg;. 
1c567 20 20 20 20 20 69 6e 74 20 69 51 75 65 72 79 3b       int iQuery;
1c568 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 73 71 6c  .      const sql
1c569 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f  ite3_module *pMo
1c56a 64 75 6c 65 3b 0a 20 20 20 20 20 20 4d 65 6d 20  dule;.      Mem 
1c56b 2a 70 51 75 65 72 79 3b 0a 20 20 20 20 20 20 4d  *pQuery;.      M
1c56c 65 6d 20 2a 70 41 72 67 63 3b 0a 20 20 20 20 20  em *pArgc;.     
1c56d 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75   sqlite3_vtab_cu
1c56e 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f  rsor *pVtabCurso
1c56f 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  r;.      sqlite3
1c570 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20  _vtab *pVtab;.  
1c571 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a      VdbeCursor *
1c572 70 43 75 72 3b 0a 20 20 20 20 20 20 69 6e 74 20  pCur;.      int 
1c573 72 65 73 3b 0a 20 20 20 20 20 20 69 6e 74 20 69  res;.      int i
1c574 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 2a 61 70  ;.      Mem **ap
1c575 41 72 67 3b 0a 20 20 20 20 7d 20 63 68 3b 0a 20  Arg;.    } ch;. 
1c576 20 20 20 73 74 72 75 63 74 20 4f 50 5f 56 43 6f     struct OP_VCo
1c577 6c 75 6d 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20  lumn_stack_vars 
1c578 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
1c579 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 20  vtab *pVtab;.   
1c57a 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33     const sqlite3
1c57b 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65  _module *pModule
1c57c 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 44 65  ;.      Mem *pDe
1c57d 73 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  st;.      sqlite
1c57e 33 5f 63 6f 6e 74 65 78 74 20 73 43 6f 6e 74 65  3_context sConte
1c57f 78 74 3b 0a 20 20 20 20 7d 20 63 69 3b 0a 20 20  xt;.    } ci;.  
1c580 20 20 73 74 72 75 63 74 20 4f 50 5f 56 4e 65 78    struct OP_VNex
1c581 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  t_stack_vars {. 
1c582 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61       sqlite3_vta
1c583 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 20  b *pVtab;.      
1c584 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f  const sqlite3_mo
1c585 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20  dule *pModule;. 
1c586 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20       int res;.  
1c587 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a      VdbeCursor *
1c588 70 43 75 72 3b 0a 20 20 20 20 7d 20 63 6a 3b 0a  pCur;.    } cj;.
1c589 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 56 52      struct OP_VR
1c58a 65 6e 61 6d 65 5f 73 74 61 63 6b 5f 76 61 72 73  ename_stack_vars
1c58b 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   {.      sqlite3
1c58c 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20  _vtab *pVtab;.  
1c58d 20 20 20 20 4d 65 6d 20 2a 70 4e 61 6d 65 3b 0a      Mem *pName;.
1c58e 20 20 20 20 7d 20 63 6b 3b 0a 20 20 20 20 73 74      } ck;.    st
1c58f 72 75 63 74 20 4f 50 5f 56 55 70 64 61 74 65 5f  ruct OP_VUpdate_
1c590 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
1c591 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20     sqlite3_vtab 
1c592 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 20 73 71  *pVtab;.      sq
1c593 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d  lite3_module *pM
1c594 6f 64 75 6c 65 3b 0a 20 20 20 20 20 20 69 6e 74  odule;.      int
1c595 20 6e 41 72 67 3b 0a 20 20 20 20 20 20 69 6e 74   nArg;.      int
1c596 20 69 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   i;.      sqlite
1c597 5f 69 6e 74 36 34 20 72 6f 77 69 64 3b 0a 20 20  _int64 rowid;.  
1c598 20 20 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67 3b      Mem **apArg;
1c599 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 58 3b 0a  .      Mem *pX;.
1c59a 20 20 20 20 7d 20 63 6c 3b 0a 20 20 20 20 73 74      } cl;.    st
1c59b 72 75 63 74 20 4f 50 5f 50 61 67 65 63 6f 75 6e  ruct OP_Pagecoun
1c59c 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  t_stack_vars {. 
1c59d 20 20 20 20 20 69 6e 74 20 70 31 3b 0a 20 20 20       int p1;.   
1c59e 20 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a 20 20     int nPage;.  
1c59f 20 20 20 20 50 61 67 65 72 20 2a 70 50 61 67 65      Pager *pPage
1c5a0 72 3b 0a 20 20 20 20 7d 20 63 6d 3b 0a 20 20 20  r;.    } cm;.   
1c5a1 20 73 74 72 75 63 74 20 4f 50 5f 54 72 61 63 65   struct OP_Trace
1c5a2 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20  _stack_vars {.  
1c5a3 20 20 20 20 63 68 61 72 20 2a 7a 54 72 61 63 65      char *zTrace
1c5a4 3b 0a 20 20 20 20 7d 20 63 6e 3b 0a 20 20 7d 20  ;.    } cn;.  } 
1c5a5 75 3b 0a 20 20 2f 2a 20 45 6e 64 20 61 75 74 6f  u;.  /* End auto
1c5a6 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e 65 72 61  matically genera
1c5a7 74 65 64 20 63 6f 64 65 0a 20 20 2a 2a 2a 2a 2a  ted code.  *****
1c5a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c5a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c5aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c5ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1c5ac 0a 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d  ..  assert( p->m
1c5ad 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43  agic==VDBE_MAGIC
1c5ae 5f 52 55 4e 20 29 3b 20 20 2f 2a 20 73 71 6c 69  _RUN );  /* sqli
1c5af 74 65 33 5f 73 74 65 70 28 29 20 76 65 72 69 66  te3_step() verif
1c5b0 69 65 73 20 74 68 69 73 20 2a 2f 0a 20 20 61 73  ies this */.  as
1c5b1 73 65 72 74 28 20 64 62 2d 3e 6d 61 67 69 63 3d  sert( db->magic=
1c5b2 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55  =SQLITE_MAGIC_BU
1c5b3 53 59 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  SY );.  sqlite3V
1c5b4 64 62 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74  dbeMutexArrayEnt
1c5b5 65 72 28 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e  er(p);.  if( p->
1c5b6 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
1c5b7 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20   ){.    /* This 
1c5b8 68 61 70 70 65 6e 73 20 69 66 20 61 20 6d 61 6c  happens if a mal
1c5b9 6c 6f 63 28 29 20 69 6e 73 69 64 65 20 61 20 63  loc() inside a c
1c5ba 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 63  all to sqlite3_c
1c5bb 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a  olumn_text() or.
1c5bc 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63      ** sqlite3_c
1c5bd 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 66  olumn_text16() f
1c5be 61 69 6c 65 64 2e 20 20 2a 2f 0a 20 20 20 20 67  ailed.  */.    g
1c5bf 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a  oto no_mem;.  }.
1c5c0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 72 63 3d    assert( p->rc=
1c5c1 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d  =SQLITE_OK || p-
1c5c2 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59  >rc==SQLITE_BUSY
1c5c3 20 29 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51   );.  p->rc = SQ
1c5c4 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72  LITE_OK;.  asser
1c5c5 74 28 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 30  t( p->explain==0
1c5c6 20 29 3b 0a 20 20 70 2d 3e 70 52 65 73 75 6c 74   );.  p->pResult
1c5c7 53 65 74 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 62  Set = 0;.  db->b
1c5c8 75 73 79 48 61 6e 64 6c 65 72 2e 6e 42 75 73 79  usyHandler.nBusy
1c5c9 20 3d 20 30 3b 0a 20 20 43 48 45 43 4b 5f 46 4f   = 0;.  CHECK_FO
1c5ca 52 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 73  R_INTERRUPT;.  s
1c5cb 71 6c 69 74 65 33 56 64 62 65 49 4f 54 72 61 63  qlite3VdbeIOTrac
1c5cc 65 53 71 6c 28 70 29 3b 0a 23 69 66 64 65 66 20  eSql(p);.#ifdef 
1c5cd 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 73  SQLITE_DEBUG.  s
1c5ce 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67  qlite3BeginBenig
1c5cf 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 69 66 28  nMalloc();.  if(
1c5d0 20 70 2d 3e 70 63 3d 3d 30 20 0a 20 20 20 26 26   p->pc==0 .   &&
1c5d1 20 28 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 20   ((p->db->flags 
1c5d2 26 20 53 51 4c 49 54 45 5f 56 64 62 65 4c 69 73  & SQLITE_VdbeLis
1c5d3 74 69 6e 67 29 20 7c 7c 20 66 69 6c 65 45 78 69  ting) || fileExi
1c5d4 73 74 73 28 64 62 2c 20 22 76 64 62 65 5f 65 78  sts(db, "vdbe_ex
1c5d5 70 6c 61 69 6e 22 29 29 0a 20 20 29 7b 0a 20 20  plain")).  ){.  
1c5d6 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 70 72 69    int i;.    pri
1c5d7 6e 74 66 28 22 56 44 42 45 20 50 72 6f 67 72 61  ntf("VDBE Progra
1c5d8 6d 20 4c 69 73 74 69 6e 67 3a 5c 6e 22 29 3b 0a  m Listing:\n");.
1c5d9 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 50      sqlite3VdbeP
1c5da 72 69 6e 74 53 71 6c 28 70 29 3b 0a 20 20 20 20  rintSql(p);.    
1c5db 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f  for(i=0; i<p->nO
1c5dc 70 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73  p; i++){.      s
1c5dd 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f  qlite3VdbePrintO
1c5de 70 28 73 74 64 6f 75 74 2c 20 69 2c 20 26 70 2d  p(stdout, i, &p-
1c5df 3e 61 4f 70 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a  >aOp[i]);.    }.
1c5e0 20 20 7d 0a 20 20 69 66 28 20 66 69 6c 65 45 78    }.  if( fileEx
1c5e1 69 73 74 73 28 64 62 2c 20 22 76 64 62 65 5f 74  ists(db, "vdbe_t
1c5e2 72 61 63 65 22 29 20 29 7b 0a 20 20 20 20 70 2d  race") ){.    p-
1c5e3 3e 74 72 61 63 65 20 3d 20 73 74 64 6f 75 74 3b  >trace = stdout;
1c5e4 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 6e  .  }.  sqlite3En
1c5e5 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  dBenignMalloc();
1c5e6 0a 23 65 6e 64 69 66 0a 20 20 66 6f 72 28 70 63  .#endif.  for(pc
1c5e7 3d 70 2d 3e 70 63 3b 20 72 63 3d 3d 53 51 4c 49  =p->pc; rc==SQLI
1c5e8 54 45 5f 4f 4b 3b 20 70 63 2b 2b 29 7b 0a 20 20  TE_OK; pc++){.  
1c5e9 20 20 61 73 73 65 72 74 28 20 70 63 3e 3d 30 20    assert( pc>=0 
1c5ea 26 26 20 70 63 3c 70 2d 3e 6e 4f 70 20 29 3b 0a  && pc<p->nOp );.
1c5eb 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c      if( db->mall
1c5ec 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20  ocFailed ) goto 
1c5ed 6e 6f 5f 6d 65 6d 3b 0a 23 69 66 64 65 66 20 56  no_mem;.#ifdef V
1c5ee 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 20 20  DBE_PROFILE.    
1c5ef 6f 72 69 67 50 63 20 3d 20 70 63 3b 0a 20 20 20  origPc = pc;.   
1c5f0 20 73 74 61 72 74 20 3d 20 73 71 6c 69 74 65 33   start = sqlite3
1c5f1 48 77 74 69 6d 65 28 29 3b 0a 23 65 6e 64 69 66  Hwtime();.#endif
1c5f2 0a 20 20 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61  .    pOp = &p->a
1c5f3 4f 70 5b 70 63 5d 3b 0a 0a 20 20 20 20 2f 2a 20  Op[pc];..    /* 
1c5f4 4f 6e 6c 79 20 61 6c 6c 6f 77 20 74 72 61 63 69  Only allow traci
1c5f5 6e 67 20 69 66 20 53 51 4c 49 54 45 5f 44 45 42  ng if SQLITE_DEB
1c5f6 55 47 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 20  UG is defined.. 
1c5f7 20 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c     */.#ifdef SQL
1c5f8 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 69 66  ITE_DEBUG.    if
1c5f9 28 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20  ( p->trace ){.  
1c5fa 20 20 20 20 69 66 28 20 70 63 3d 3d 30 20 29 7b      if( pc==0 ){
1c5fb 0a 20 20 20 20 20 20 20 20 70 72 69 6e 74 66 28  .        printf(
1c5fc 22 56 44 42 45 20 45 78 65 63 75 74 69 6f 6e 20  "VDBE Execution 
1c5fd 54 72 61 63 65 3a 5c 6e 22 29 3b 0a 20 20 20 20  Trace:\n");.    
1c5fe 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 50      sqlite3VdbeP
1c5ff 72 69 6e 74 53 71 6c 28 70 29 3b 0a 20 20 20 20  rintSql(p);.    
1c600 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
1c601 33 56 64 62 65 50 72 69 6e 74 4f 70 28 70 2d 3e  3VdbePrintOp(p->
1c602 74 72 61 63 65 2c 20 70 63 2c 20 70 4f 70 29 3b  trace, pc, pOp);
1c603 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
1c604 2d 3e 74 72 61 63 65 3d 3d 30 20 26 26 20 70 63  ->trace==0 && pc
1c605 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ==0 ){.      sql
1c606 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d  ite3BeginBenignM
1c607 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 69  alloc();.      i
1c608 66 28 20 66 69 6c 65 45 78 69 73 74 73 28 64 62  f( fileExists(db
1c609 2c 20 22 76 64 62 65 5f 73 71 6c 74 72 61 63 65  , "vdbe_sqltrace
1c60a 22 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ") ){.        sq
1c60b 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 53 71  lite3VdbePrintSq
1c60c 6c 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  l(p);.      }.  
1c60d 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65      sqlite3EndBe
1c60e 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20  nignMalloc();.  
1c60f 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20    }.#endif.     
1c610 20 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20   ..    /* Check 
1c611 74 6f 20 73 65 65 20 69 66 20 77 65 20 6e 65 65  to see if we nee
1c612 64 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 6e  d to simulate an
1c613 20 69 6e 74 65 72 72 75 70 74 2e 20 20 54 68 69   interrupt.  Thi
1c614 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73 0a 20  s only happens. 
1c615 20 20 20 2a 2a 20 69 66 20 77 65 20 68 61 76 65     ** if we have
1c616 20 61 20 73 70 65 63 69 61 6c 20 74 65 73 74 20   a special test 
1c617 62 75 69 6c 64 2e 0a 20 20 20 20 2a 2f 0a 23 69  build..    */.#i
1c618 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
1c619 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1c61a 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f 75 6e 74  _interrupt_count
1c61b 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  >0 ){.      sqli
1c61c 74 65 33 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f  te3_interrupt_co
1c61d 75 6e 74 2d 2d 3b 0a 20 20 20 20 20 20 69 66 28  unt--;.      if(
1c61e 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75   sqlite3_interru
1c61f 70 74 5f 63 6f 75 6e 74 3d 3d 30 20 29 7b 0a 20  pt_count==0 ){. 
1c620 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69         sqlite3_i
1c621 6e 74 65 72 72 75 70 74 28 64 62 29 3b 0a 20 20  nterrupt(db);.  
1c622 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64      }.    }.#end
1c623 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
1c624 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53  TE_OMIT_PROGRESS
1c625 5f 43 41 4c 4c 42 41 43 4b 0a 20 20 20 20 2f 2a  _CALLBACK.    /*
1c626 20 43 61 6c 6c 20 74 68 65 20 70 72 6f 67 72 65   Call the progre
1c627 73 73 20 63 61 6c 6c 62 61 63 6b 20 69 66 20 69  ss callback if i
1c628 74 20 69 73 20 63 6f 6e 66 69 67 75 72 65 64 20  t is configured 
1c629 61 6e 64 20 74 68 65 20 72 65 71 75 69 72 65 64  and the required
1c62a 20 6e 75 6d 62 65 72 0a 20 20 20 20 2a 2a 20 6f   number.    ** o
1c62b 66 20 56 44 42 45 20 6f 70 73 20 68 61 76 65 20  f VDBE ops have 
1c62c 62 65 65 6e 20 65 78 65 63 75 74 65 64 20 28 65  been executed (e
1c62d 69 74 68 65 72 20 73 69 6e 63 65 20 74 68 69 73  ither since this
1c62e 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 0a 20   invocation of. 
1c62f 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 64 62     ** sqlite3Vdb
1c630 65 45 78 65 63 28 29 20 6f 72 20 73 69 6e 63 65  eExec() or since
1c631 20 6c 61 73 74 20 74 69 6d 65 20 74 68 65 20 70   last time the p
1c632 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b  rogress callback
1c633 20 77 61 73 20 63 61 6c 6c 65 64 29 2e 0a 20 20   was called)..  
1c634 20 20 2a 2a 20 49 66 20 74 68 65 20 70 72 6f 67    ** If the prog
1c635 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 72 65  ress callback re
1c636 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  turns non-zero, 
1c637 65 78 69 74 20 74 68 65 20 76 69 72 74 75 61 6c  exit the virtual
1c638 20 6d 61 63 68 69 6e 65 20 77 69 74 68 0a 20 20   machine with.  
1c639 20 20 2a 2a 20 61 20 72 65 74 75 72 6e 20 63 6f    ** a return co
1c63a 64 65 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 2e  de SQLITE_ABORT.
1c63b 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
1c63c 64 62 2d 3e 78 50 72 6f 67 72 65 73 73 20 29 7b  db->xProgress ){
1c63d 0a 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 6e  .      if( db->n
1c63e 50 72 6f 67 72 65 73 73 4f 70 73 3d 3d 6e 50 72  ProgressOps==nPr
1c63f 6f 67 72 65 73 73 4f 70 73 20 29 7b 0a 20 20 20  ogressOps ){.   
1c640 20 20 20 20 20 69 6e 74 20 70 72 63 3b 0a 20 20       int prc;.  
1c641 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
1c642 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29  3SafetyOff(db) )
1c643 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1c644 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 20  to_misuse;.     
1c645 20 20 20 70 72 63 20 3d 64 62 2d 3e 78 50 72 6f     prc =db->xPro
1c646 67 72 65 73 73 28 64 62 2d 3e 70 50 72 6f 67 72  gress(db->pProgr
1c647 65 73 73 41 72 67 29 3b 0a 20 20 20 20 20 20 20  essArg);.       
1c648 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65   if( sqlite3Safe
1c649 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20  tyOn(db) ) goto 
1c64a 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73  abort_due_to_mis
1c64b 75 73 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28  use;.        if(
1c64c 20 70 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 20   prc!=0 ){.     
1c64d 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
1c64e 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 20 20  _INTERRUPT;.    
1c64f 20 20 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f        goto vdbe_
1c650 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 20 20 20 20  error_halt;.    
1c651 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 50      }.        nP
1c652 72 6f 67 72 65 73 73 4f 70 73 20 3d 20 30 3b 0a  rogressOps = 0;.
1c653 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e 50        }.      nP
1c654 72 6f 67 72 65 73 73 4f 70 73 2b 2b 3b 0a 20 20  rogressOps++;.  
1c655 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20    }.#endif..    
1c656 2f 2a 20 44 6f 20 63 6f 6d 6d 6f 6e 20 73 65 74  /* Do common set
1c657 75 70 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f  up processing fo
1c658 72 20 61 6e 79 20 6f 70 63 6f 64 65 20 74 68 61  r any opcode tha
1c659 74 20 69 73 20 6d 61 72 6b 65 64 0a 20 20 20 20  t is marked.    
1c65a 2a 2a 20 77 69 74 68 20 74 68 65 20 22 6f 75 74  ** with the "out
1c65b 32 2d 70 72 65 72 65 6c 65 61 73 65 22 20 74 61  2-prerelease" ta
1c65c 67 2e 20 20 53 75 63 68 20 6f 70 63 6f 64 65 73  g.  Such opcodes
1c65d 20 68 61 76 65 20 61 20 73 69 6e 67 6c 65 0a 20   have a single. 
1c65e 20 20 20 2a 2a 20 6f 75 74 70 75 74 20 77 68 69     ** output whi
1c65f 63 68 20 69 73 20 73 70 65 63 69 66 69 65 64 20  ch is specified 
1c660 62 79 20 74 68 65 20 50 32 20 70 61 72 61 6d 65  by the P2 parame
1c661 74 65 72 2e 20 20 54 68 65 20 50 32 20 72 65 67  ter.  The P2 reg
1c662 69 73 74 65 72 0a 20 20 20 20 2a 2a 20 69 73 20  ister.    ** is 
1c663 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 61  initialized to a
1c664 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20   NULL..    */.  
1c665 20 20 6f 70 50 72 6f 70 65 72 74 79 20 3d 20 6f    opProperty = o
1c666 70 63 6f 64 65 50 72 6f 70 65 72 74 79 5b 70 4f  pcodeProperty[pO
1c667 70 2d 3e 6f 70 63 6f 64 65 5d 3b 0a 20 20 20 20  p->opcode];.    
1c668 69 66 28 20 28 6f 70 50 72 6f 70 65 72 74 79 20  if( (opProperty 
1c669 26 20 4f 50 46 4c 47 5f 4f 55 54 32 5f 50 52 45  & OPFLG_OUT2_PRE
1c66a 52 45 4c 45 41 53 45 29 21 3d 30 20 29 7b 0a 20  RELEASE)!=0 ){. 
1c66b 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70       assert( pOp
1c66c 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20 20 20  ->p2>0 );.      
1c66d 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c  assert( pOp->p2<
1c66e 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20  =p->nMem );.    
1c66f 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65    pOut = &p->aMe
1c670 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20  m[pOp->p2];.    
1c671 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1c672 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28  ReleaseExternal(
1c673 70 4f 75 74 29 3b 0a 20 20 20 20 20 20 70 4f 75  pOut);.      pOu
1c674 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e  t->flags = MEM_N
1c675 75 6c 6c 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d  ull;.      pOut-
1c676 3e 6e 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73  >n = 0;.    }els
1c677 65 0a 20 0a 20 20 20 20 2f 2a 20 44 6f 20 63 6f  e. .    /* Do co
1c678 6d 6d 6f 6e 20 73 65 74 75 70 20 66 6f 72 20 6f  mmon setup for o
1c679 70 63 6f 64 65 73 20 6d 61 72 6b 65 64 20 77 69  pcodes marked wi
1c67a 74 68 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f  th one of the fo
1c67b 6c 6c 6f 77 69 6e 67 0a 20 20 20 20 2a 2a 20 63  llowing.    ** c
1c67c 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 6f 66 20 70  ombinations of p
1c67d 72 6f 70 65 72 74 69 65 73 2e 0a 20 20 20 20 2a  roperties..    *
1c67e 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  *.    **        
1c67f 20 20 20 69 6e 31 0a 20 20 20 20 2a 2a 20 20 20     in1.    **   
1c680 20 20 20 20 20 20 20 20 69 6e 31 20 69 6e 32 0a          in1 in2.
1c681 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
1c682 20 69 6e 31 20 69 6e 32 20 6f 75 74 33 0a 20 20   in1 in2 out3.  
1c683 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 69    **           i
1c684 6e 31 20 69 6e 33 0a 20 20 20 20 2a 2a 0a 20 20  n1 in3.    **.  
1c685 20 20 2a 2a 20 56 61 72 69 61 62 6c 65 73 20 70    ** Variables p
1c686 49 6e 31 2c 20 70 49 6e 32 2c 20 61 6e 64 20 70  In1, pIn2, and p
1c687 49 6e 33 20 61 72 65 20 6d 61 64 65 20 74 6f 20  In3 are made to 
1c688 70 6f 69 6e 74 20 74 6f 20 61 70 70 72 6f 70 72  point to appropr
1c689 69 61 74 65 0a 20 20 20 20 2a 2a 20 72 65 67 69  iate.    ** regi
1c68a 73 74 65 72 73 20 66 6f 72 20 69 6e 70 75 74 73  sters for inputs
1c68b 2e 20 20 56 61 72 69 61 62 6c 65 20 70 4f 75 74  .  Variable pOut
1c68c 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6f   points to the o
1c68d 75 74 70 75 74 20 72 65 67 69 73 74 65 72 2e 0a  utput register..
1c68e 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 28      */.    if( (
1c68f 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46  opProperty & OPF
1c690 4c 47 5f 49 4e 31 29 21 3d 30 20 29 7b 0a 20 20  LG_IN1)!=0 ){.  
1c691 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
1c692 3e 70 31 3e 30 20 29 3b 0a 20 20 20 20 20 20 61  >p1>0 );.      a
1c693 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3c 3d  ssert( pOp->p1<=
1c694 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20  p->nMem );.     
1c695 20 70 49 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d   pIn1 = &p->aMem
1c696 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 20 20 20  [pOp->p1];.     
1c697 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28   REGISTER_TRACE(
1c698 70 4f 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a  pOp->p1, pIn1);.
1c699 20 20 20 20 20 20 69 66 28 20 28 6f 70 50 72 6f        if( (opPro
1c69a 70 65 72 74 79 20 26 20 4f 50 46 4c 47 5f 49 4e  perty & OPFLG_IN
1c69b 32 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  2)!=0 ){.       
1c69c 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32   assert( pOp->p2
1c69d 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73  >0 );.        as
1c69e 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d 70  sert( pOp->p2<=p
1c69f 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20  ->nMem );.      
1c6a0 20 20 70 49 6e 32 20 3d 20 26 70 2d 3e 61 4d 65    pIn2 = &p->aMe
1c6a1 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20  m[pOp->p2];.    
1c6a2 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41      REGISTER_TRA
1c6a3 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 49 6e 32  CE(pOp->p2, pIn2
1c6a4 29 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 73  );.        /* As
1c6a5 20 63 75 72 72 65 6e 74 6c 79 20 69 6d 70 6c 65   currently imple
1c6a6 6d 65 6e 74 65 64 2c 20 69 6e 32 20 69 6d 70 6c  mented, in2 impl
1c6a7 69 65 73 20 6f 75 74 33 2e 20 20 54 68 65 72 65  ies out3.  There
1c6a8 20 69 73 20 6e 6f 20 72 65 61 73 6f 6e 0a 20 20   is no reason.  
1c6a9 20 20 20 20 20 20 2a 2a 20 77 68 79 20 74 68 69        ** why thi
1c6aa 73 20 68 61 73 20 74 6f 20 62 65 2c 20 69 74 20  s has to be, it 
1c6ab 6a 75 73 74 20 77 6f 72 6b 65 64 20 6f 75 74 20  just worked out 
1c6ac 74 68 61 74 20 77 61 79 2e 20 2a 2f 0a 20 20 20  that way. */.   
1c6ad 20 20 20 20 20 61 73 73 65 72 74 28 20 28 6f 70       assert( (op
1c6ae 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47  Property & OPFLG
1c6af 5f 4f 55 54 33 29 21 3d 30 20 29 3b 0a 20 20 20  _OUT3)!=0 );.   
1c6b0 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70       assert( pOp
1c6b1 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20 20 20 20  ->p3>0 );.      
1c6b2 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1c6b3 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  3<=p->nMem );.  
1c6b4 20 20 20 20 20 20 70 4f 75 74 20 3d 20 26 70 2d        pOut = &p-
1c6b5 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a  >aMem[pOp->p3];.
1c6b6 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
1c6b7 28 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50  (opProperty & OP
1c6b8 46 4c 47 5f 49 4e 33 29 21 3d 30 20 29 7b 0a 20  FLG_IN3)!=0 ){. 
1c6b9 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
1c6ba 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20 20  Op->p3>0 );.    
1c6bb 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
1c6bc 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a  >p3<=p->nMem );.
1c6bd 20 20 20 20 20 20 20 20 70 49 6e 33 20 3d 20 26          pIn3 = &
1c6be 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d  p->aMem[pOp->p3]
1c6bf 3b 0a 20 20 20 20 20 20 20 20 52 45 47 49 53 54  ;.        REGIST
1c6c0 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33  ER_TRACE(pOp->p3
1c6c1 2c 20 70 49 6e 33 29 3b 0a 20 20 20 20 20 20 7d  , pIn3);.      }
1c6c2 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28  .    }else if( (
1c6c3 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46  opProperty & OPF
1c6c4 4c 47 5f 49 4e 32 29 21 3d 30 20 29 7b 0a 20 20  LG_IN2)!=0 ){.  
1c6c5 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
1c6c6 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20 20 20 61  >p2>0 );.      a
1c6c7 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d  ssert( pOp->p2<=
1c6c8 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20  p->nMem );.     
1c6c9 20 70 49 6e 32 20 3d 20 26 70 2d 3e 61 4d 65 6d   pIn2 = &p->aMem
1c6ca 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20  [pOp->p2];.     
1c6cb 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28   REGISTER_TRACE(
1c6cc 70 4f 70 2d 3e 70 32 2c 20 70 49 6e 32 29 3b 0a  pOp->p2, pIn2);.
1c6cd 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 6f      }else if( (o
1c6ce 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c  pProperty & OPFL
1c6cf 47 5f 49 4e 33 29 21 3d 30 20 29 7b 0a 20 20 20  G_IN3)!=0 ){.   
1c6d0 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
1c6d1 70 33 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73  p3>0 );.      as
1c6d2 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70  sert( pOp->p3<=p
1c6d3 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20  ->nMem );.      
1c6d4 70 49 6e 33 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  pIn3 = &p->aMem[
1c6d5 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20  pOp->p3];.      
1c6d6 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
1c6d7 4f 70 2d 3e 70 33 2c 20 70 49 6e 33 29 3b 0a 20  Op->p3, pIn3);. 
1c6d8 20 20 20 7d 0a 0a 20 20 20 20 73 77 69 74 63 68     }..    switch
1c6d9 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b  ( pOp->opcode ){
1c6da 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
1c6db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c6dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c6dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c6de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c6df 0a 2a 2a 20 57 68 61 74 20 66 6f 6c 6c 6f 77 73  .** What follows
1c6e0 20 69 73 20 61 20 6d 61 73 73 69 76 65 20 73 77   is a massive sw
1c6e1 69 74 63 68 20 73 74 61 74 65 6d 65 6e 74 20 77  itch statement w
1c6e2 68 65 72 65 20 65 61 63 68 20 63 61 73 65 20 69  here each case i
1c6e3 6d 70 6c 65 6d 65 6e 74 73 20 61 0a 2a 2a 20 73  mplements a.** s
1c6e4 65 70 61 72 61 74 65 20 69 6e 73 74 72 75 63 74  eparate instruct
1c6e5 69 6f 6e 20 69 6e 20 74 68 65 20 76 69 72 74 75  ion in the virtu
1c6e6 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 49 66 20  al machine.  If 
1c6e7 77 65 20 66 6f 6c 6c 6f 77 20 74 68 65 20 75 73  we follow the us
1c6e8 75 61 6c 0a 2a 2a 20 69 6e 64 65 6e 74 61 74 69  ual.** indentati
1c6e9 6f 6e 20 63 6f 6e 76 65 6e 74 69 6f 6e 73 2c 20  on conventions, 
1c6ea 65 61 63 68 20 63 61 73 65 20 73 68 6f 75 6c 64  each case should
1c6eb 20 62 65 20 69 6e 64 65 6e 74 65 64 20 62 79 20   be indented by 
1c6ec 36 20 73 70 61 63 65 73 2e 20 20 42 75 74 0a 2a  6 spaces.  But.*
1c6ed 2a 20 74 68 61 74 20 69 73 20 61 20 6c 6f 74 20  * that is a lot 
1c6ee 6f 66 20 77 61 73 74 65 64 20 73 70 61 63 65 20  of wasted space 
1c6ef 6f 6e 20 74 68 65 20 6c 65 66 74 20 6d 61 72 67  on the left marg
1c6f0 69 6e 2e 20 20 53 6f 20 74 68 65 20 63 6f 64 65  in.  So the code
1c6f1 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 73   within.** the s
1c6f2 77 69 74 63 68 20 73 74 61 74 65 6d 65 6e 74 20  witch statement 
1c6f3 77 69 6c 6c 20 62 72 65 61 6b 20 77 69 74 68 20  will break with 
1c6f4 63 6f 6e 76 65 6e 74 69 6f 6e 20 61 6e 64 20 62  convention and b
1c6f5 65 20 66 6c 75 73 68 2d 6c 65 66 74 2e 20 41 6e  e flush-left. An
1c6f6 6f 74 68 65 72 0a 2a 2a 20 62 69 67 20 63 6f 6d  other.** big com
1c6f7 6d 65 6e 74 20 28 73 69 6d 69 6c 61 72 20 74 6f  ment (similar to
1c6f8 20 74 68 69 73 20 6f 6e 65 29 20 77 69 6c 6c 20   this one) will 
1c6f9 6d 61 72 6b 20 74 68 65 20 70 6f 69 6e 74 20 69  mark the point i
1c6fa 6e 20 74 68 65 20 63 6f 64 65 20 77 68 65 72 65  n the code where
1c6fb 0a 2a 2a 20 77 65 20 74 72 61 6e 73 69 74 69 6f  .** we transitio
1c6fc 6e 20 62 61 63 6b 20 74 6f 20 6e 6f 72 6d 61 6c  n back to normal
1c6fd 20 69 6e 64 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a   indentation..**
1c6fe 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61 74 74 69  .** The formatti
1c6ff 6e 67 20 6f 66 20 65 61 63 68 20 63 61 73 65 20  ng of each case 
1c700 69 73 20 69 6d 70 6f 72 74 61 6e 74 2e 20 20 54  is important.  T
1c701 68 65 20 6d 61 6b 65 66 69 6c 65 20 66 6f 72 20  he makefile for 
1c702 53 51 4c 69 74 65 0a 2a 2a 20 67 65 6e 65 72 61  SQLite.** genera
1c703 74 65 73 20 74 77 6f 20 43 20 66 69 6c 65 73 20  tes two C files 
1c704 22 6f 70 63 6f 64 65 73 2e 68 22 20 61 6e 64 20  "opcodes.h" and 
1c705 22 6f 70 63 6f 64 65 73 2e 63 22 20 62 79 20 73  "opcodes.c" by s
1c706 63 61 6e 6e 69 6e 67 20 74 68 69 73 0a 2a 2a 20  canning this.** 
1c707 66 69 6c 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72  file looking for
1c708 20 6c 69 6e 65 73 20 74 68 61 74 20 62 65 67 69   lines that begi
1c709 6e 20 77 69 74 68 20 22 63 61 73 65 20 4f 50 5f  n with "case OP_
1c70a 22 2e 20 20 54 68 65 20 6f 70 63 6f 64 65 73 2e  ".  The opcodes.
1c70b 68 20 66 69 6c 65 73 0a 2a 2a 20 77 69 6c 6c 20  h files.** will 
1c70c 62 65 20 66 69 6c 6c 65 64 20 77 69 74 68 20 23  be filled with #
1c70d 64 65 66 69 6e 65 73 20 74 68 61 74 20 67 69 76  defines that giv
1c70e 65 20 75 6e 69 71 75 65 20 69 6e 74 65 67 65 72  e unique integer
1c70f 20 76 61 6c 75 65 73 20 74 6f 20 65 61 63 68 0a   values to each.
1c710 2a 2a 20 6f 70 63 6f 64 65 20 61 6e 64 20 74 68  ** opcode and th
1c711 65 20 6f 70 63 6f 64 65 73 2e 63 20 66 69 6c 65  e opcodes.c file
1c712 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20   is filled with 
1c713 61 6e 20 61 72 72 61 79 20 6f 66 20 73 74 72 69  an array of stri
1c714 6e 67 73 20 77 68 65 72 65 0a 2a 2a 20 65 61 63  ngs where.** eac
1c715 68 20 73 74 72 69 6e 67 20 69 73 20 74 68 65 20  h string is the 
1c716 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 20 66 6f  symbolic name fo
1c717 72 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  r the correspond
1c718 69 6e 67 20 6f 70 63 6f 64 65 2e 20 20 49 66 20  ing opcode.  If 
1c719 74 68 65 0a 2a 2a 20 63 61 73 65 20 73 74 61 74  the.** case stat
1c71a 65 6d 65 6e 74 20 69 73 20 66 6f 6c 6c 6f 77 65  ement is followe
1c71b 64 20 62 79 20 61 20 63 6f 6d 6d 65 6e 74 20 6f  d by a comment o
1c71c 66 20 74 68 65 20 66 6f 72 6d 20 22 2f 23 20 73  f the form "/# s
1c71d 61 6d 65 20 61 73 20 2e 2e 2e 20 23 2f 22 0a 2a  ame as ... #/".*
1c71e 2a 20 74 68 61 74 20 63 6f 6d 6d 65 6e 74 20 69  * that comment i
1c71f 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d  s used to determ
1c720 69 6e 65 20 74 68 65 20 70 61 72 74 69 63 75 6c  ine the particul
1c721 61 72 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  ar value of the 
1c722 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74  opcode..**.** Ot
1c723 68 65 72 20 6b 65 79 77 6f 72 64 73 20 69 6e 20  her keywords in 
1c724 74 68 65 20 63 6f 6d 6d 65 6e 74 20 74 68 61 74  the comment that
1c725 20 66 6f 6c 6c 6f 77 73 20 65 61 63 68 20 63 61   follows each ca
1c726 73 65 20 61 72 65 20 75 73 65 64 20 74 6f 0a 2a  se are used to.*
1c727 2a 20 63 6f 6e 73 74 72 75 63 74 20 74 68 65 20  * construct the 
1c728 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45  OPFLG_INITIALIZE
1c729 52 20 76 61 6c 75 65 20 74 68 61 74 20 69 6e 69  R value that ini
1c72a 74 69 61 6c 69 7a 65 73 20 6f 70 63 6f 64 65 50  tializes opcodeP
1c72b 72 6f 70 65 72 74 79 5b 5d 2e 0a 2a 2a 20 4b 65  roperty[]..** Ke
1c72c 79 77 6f 72 64 73 20 69 6e 63 6c 75 64 65 3a 20  ywords include: 
1c72d 69 6e 31 2c 20 69 6e 32 2c 20 69 6e 33 2c 20 6f  in1, in2, in3, o
1c72e 75 74 32 5f 70 72 65 72 65 6c 65 61 73 65 2c 20  ut2_prerelease, 
1c72f 6f 75 74 32 2c 20 6f 75 74 33 2e 20 20 53 65 65  out2, out3.  See
1c730 0a 2a 2a 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65  .** the mkopcode
1c731 68 2e 61 77 6b 20 73 63 72 69 70 74 20 66 6f 72  h.awk script for
1c732 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f   additional info
1c733 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 44  rmation..**.** D
1c734 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 61 62 6f  ocumentation abo
1c735 75 74 20 56 44 42 45 20 6f 70 63 6f 64 65 73 20  ut VDBE opcodes 
1c736 69 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 20  is generated by 
1c737 73 63 61 6e 6e 69 6e 67 20 74 68 69 73 20 66 69  scanning this fi
1c738 6c 65 0a 2a 2a 20 66 6f 72 20 6c 69 6e 65 73 20  le.** for lines 
1c739 6f 66 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 20  of that contain 
1c73a 22 4f 70 63 6f 64 65 3a 22 2e 20 20 54 68 61 74  "Opcode:".  That
1c73b 20 6c 69 6e 65 20 61 6e 64 20 61 6c 6c 20 73 75   line and all su
1c73c 62 73 65 71 75 65 6e 74 0a 2a 2a 20 63 6f 6d 6d  bsequent.** comm
1c73d 65 6e 74 20 6c 69 6e 65 73 20 61 72 65 20 75 73  ent lines are us
1c73e 65 64 20 69 6e 20 74 68 65 20 67 65 6e 65 72 61  ed in the genera
1c73f 74 69 6f 6e 20 6f 66 20 74 68 65 20 6f 70 63 6f  tion of the opco
1c740 64 65 2e 68 74 6d 6c 20 64 6f 63 75 6d 65 6e 74  de.html document
1c741 61 74 69 6f 6e 0a 2a 2a 20 66 69 6c 65 2e 0a 2a  ation.** file..*
1c742 2a 0a 2a 2a 20 53 55 4d 4d 41 52 59 3a 0a 2a 2a  *.** SUMMARY:.**
1c743 0a 2a 2a 20 20 20 20 20 46 6f 72 6d 61 74 74 69  .**     Formatti
1c744 6e 67 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20  ng is important 
1c745 74 6f 20 73 63 72 69 70 74 73 20 74 68 61 74 20  to scripts that 
1c746 73 63 61 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a  scan this file..
1c747 2a 2a 20 20 20 20 20 44 6f 20 6e 6f 74 20 64 65  **     Do not de
1c748 76 69 61 74 65 20 66 72 6f 6d 20 74 68 65 20 66  viate from the f
1c749 6f 72 6d 61 74 74 69 6e 67 20 73 74 79 6c 65 20  ormatting style 
1c74a 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65  currently in use
1c74b 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
1c74c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c74d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c74e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c74f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c750 2a 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  **/../* Opcode: 
1c751 20 47 6f 74 6f 20 2a 20 50 32 20 2a 20 2a 20 2a   Goto * P2 * * *
1c752 0a 2a 2a 0a 2a 2a 20 41 6e 20 75 6e 63 6f 6e 64  .**.** An uncond
1c753 69 74 69 6f 6e 61 6c 20 6a 75 6d 70 20 74 6f 20  itional jump to 
1c754 61 64 64 72 65 73 73 20 50 32 2e 0a 2a 2a 20 54  address P2..** T
1c755 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74  he next instruct
1c756 69 6f 6e 20 65 78 65 63 75 74 65 64 20 77 69 6c  ion executed wil
1c757 6c 20 62 65 20 0a 2a 2a 20 74 68 65 20 6f 6e 65  l be .** the one
1c758 20 61 74 20 69 6e 64 65 78 20 50 32 20 66 72 6f   at index P2 fro
1c759 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  m the beginning 
1c75a 6f 66 0a 2a 2a 20 74 68 65 20 70 72 6f 67 72 61  of.** the progra
1c75b 6d 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 6f  m..*/.case OP_Go
1c75c 74 6f 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  to: {           
1c75d 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 43    /* jump */.  C
1c75e 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55  HECK_FOR_INTERRU
1c75f 50 54 3b 0a 20 20 70 63 20 3d 20 70 4f 70 2d 3e  PT;.  pc = pOp->
1c760 70 32 20 2d 20 31 3b 0a 20 20 62 72 65 61 6b 3b  p2 - 1;.  break;
1c761 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20  .}../* Opcode:  
1c762 47 6f 73 75 62 20 50 31 20 50 32 20 2a 20 2a 20  Gosub P1 P2 * * 
1c763 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68  *.**.** Write th
1c764 65 20 63 75 72 72 65 6e 74 20 61 64 64 72 65 73  e current addres
1c765 73 20 6f 6e 74 6f 20 72 65 67 69 73 74 65 72 20  s onto register 
1c766 50 31 0a 2a 2a 20 61 6e 64 20 74 68 65 6e 20 6a  P1.** and then j
1c767 75 6d 70 20 74 6f 20 61 64 64 72 65 73 73 20 50  ump to address P
1c768 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 6f  2..*/.case OP_Go
1c769 73 75 62 3a 20 7b 20 20 20 20 20 20 20 20 20 20  sub: {          
1c76a 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 61    /* jump */.  a
1c76b 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30  ssert( pOp->p1>0
1c76c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f   );.  assert( pO
1c76d 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29  p->p1<=p->nMem )
1c76e 3b 0a 20 20 70 49 6e 31 20 3d 20 26 70 2d 3e 61  ;.  pIn1 = &p->a
1c76f 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Mem[pOp->p1];.  
1c770 61 73 73 65 72 74 28 20 28 70 49 6e 31 2d 3e 66  assert( (pIn1->f
1c771 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 3d  lags & MEM_Dyn)=
1c772 3d 30 20 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c  =0 );.  pIn1->fl
1c773 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20  ags = MEM_Int;. 
1c774 20 70 49 6e 31 2d 3e 75 2e 69 20 3d 20 70 63 3b   pIn1->u.i = pc;
1c775 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43  .  REGISTER_TRAC
1c776 45 28 70 4f 70 2d 3e 70 31 2c 20 70 49 6e 31 29  E(pOp->p1, pIn1)
1c777 3b 0a 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32  ;.  pc = pOp->p2
1c778 20 2d 20 31 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d   - 1;.  break;.}
1c779 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 52 65  ../* Opcode:  Re
1c77a 74 75 72 6e 20 50 31 20 2a 20 2a 20 2a 20 2a 0a  turn P1 * * * *.
1c77b 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 74 68  **.** Jump to th
1c77c 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69  e next instructi
1c77d 6f 6e 20 61 66 74 65 72 20 74 68 65 20 61 64 64  on after the add
1c77e 72 65 73 73 20 69 6e 20 72 65 67 69 73 74 65 72  ress in register
1c77f 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f   P1..*/.case OP_
1c780 52 65 74 75 72 6e 3a 20 7b 20 20 20 20 20 20 20  Return: {       
1c781 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20      /* in1 */.  
1c782 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c  assert( pIn1->fl
1c783 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b  ags & MEM_Int );
1c784 0a 20 20 70 63 20 3d 20 28 69 6e 74 29 70 49 6e  .  pc = (int)pIn
1c785 31 2d 3e 75 2e 69 3b 0a 20 20 62 72 65 61 6b 3b  1->u.i;.  break;
1c786 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20  .}../* Opcode:  
1c787 59 69 65 6c 64 20 50 31 20 2a 20 2a 20 2a 20 2a  Yield P1 * * * *
1c788 0a 2a 2a 0a 2a 2a 20 53 77 61 70 20 74 68 65 20  .**.** Swap the 
1c789 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 20  program counter 
1c78a 77 69 74 68 20 74 68 65 20 76 61 6c 75 65 20 69  with the value i
1c78b 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a  n register P1..*
1c78c 2f 0a 63 61 73 65 20 4f 50 5f 59 69 65 6c 64 3a  /.case OP_Yield:
1c78d 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a   {            /*
1c78e 20 69 6e 31 20 2a 2f 0a 23 69 66 20 30 20 20 2f   in1 */.#if 0  /
1c78f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1c790 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
1c791 61 20 2a 2f 0a 20 20 69 6e 74 20 70 63 44 65 73  a */.  int pcDes
1c792 74 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  t;.#endif /* loc
1c793 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1c794 65 64 20 69 6e 74 6f 20 75 2e 61 61 20 2a 2f 0a  ed into u.aa */.
1c795 20 20 61 73 73 65 72 74 28 20 28 70 49 6e 31 2d    assert( (pIn1-
1c796 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e  >flags & MEM_Dyn
1c797 29 3d 3d 30 20 29 3b 0a 20 20 70 49 6e 31 2d 3e  )==0 );.  pIn1->
1c798 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
1c799 0a 20 20 75 2e 61 61 2e 70 63 44 65 73 74 20 3d  .  u.aa.pcDest =
1c79a 20 28 69 6e 74 29 70 49 6e 31 2d 3e 75 2e 69 3b   (int)pIn1->u.i;
1c79b 0a 20 20 70 49 6e 31 2d 3e 75 2e 69 20 3d 20 70  .  pIn1->u.i = p
1c79c 63 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52  c;.  REGISTER_TR
1c79d 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20 70 49 6e  ACE(pOp->p1, pIn
1c79e 31 29 3b 0a 20 20 70 63 20 3d 20 75 2e 61 61 2e  1);.  pc = u.aa.
1c79f 70 63 44 65 73 74 3b 0a 20 20 62 72 65 61 6b 3b  pcDest;.  break;
1c7a0 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20  .}../* Opcode:  
1c7a1 48 61 6c 74 49 66 4e 75 6c 6c 20 20 50 31 20 50  HaltIfNull  P1 P
1c7a2 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20  2 P3 P4 *.**.** 
1c7a3 43 68 65 63 6b 20 74 68 65 20 76 61 6c 75 65 20  Check the value 
1c7a4 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 20  in register P3. 
1c7a5 20 49 66 20 69 73 20 69 73 20 4e 55 4c 4c 20 74   If is is NULL t
1c7a6 68 65 6e 20 48 61 6c 74 20 75 73 69 6e 67 0a 2a  hen Halt using.*
1c7a7 2a 20 70 61 72 61 6d 65 74 65 72 20 50 31 2c 20  * parameter P1, 
1c7a8 50 32 2c 20 61 6e 64 20 50 34 20 61 73 20 69 66  P2, and P4 as if
1c7a9 20 74 68 69 73 20 77 65 72 65 20 61 20 48 61 6c   this were a Hal
1c7aa 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20  t instruction.  
1c7ab 49 66 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20  If the.** value 
1c7ac 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 69  in register P3 i
1c7ad 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e  s not NULL, then
1c7ae 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
1c7af 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 63 61 73   a no-op..*/.cas
1c7b0 65 20 4f 50 5f 48 61 6c 74 49 66 4e 75 6c 6c 3a  e OP_HaltIfNull:
1c7b1 20 7b 20 20 20 20 20 20 2f 2a 20 69 6e 33 20 2a   {      /* in3 *
1c7b2 2f 0a 20 20 69 66 28 20 28 70 49 6e 33 2d 3e 66  /.  if( (pIn3->f
1c7b3 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29  lags & MEM_Null)
1c7b4 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 2f  ==0 ) break;.  /
1c7b5 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 69  * Fall through i
1c7b6 6e 74 6f 20 4f 50 5f 48 61 6c 74 20 2a 2f 0a 7d  nto OP_Halt */.}
1c7b7 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 48 61  ../* Opcode:  Ha
1c7b8 6c 74 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a  lt P1 P2 * P4 *.
1c7b9 2a 2a 0a 2a 2a 20 45 78 69 74 20 69 6d 6d 65 64  **.** Exit immed
1c7ba 69 61 74 65 6c 79 2e 20 20 41 6c 6c 20 6f 70 65  iately.  All ope
1c7bb 6e 20 63 75 72 73 6f 72 73 2c 20 65 74 63 20 61  n cursors, etc a
1c7bc 72 65 20 63 6c 6f 73 65 64 0a 2a 2a 20 61 75 74  re closed.** aut
1c7bd 6f 6d 61 74 69 63 61 6c 6c 79 2e 0a 2a 2a 0a 2a  omatically..**.*
1c7be 2a 20 50 31 20 69 73 20 74 68 65 20 72 65 73 75  * P1 is the resu
1c7bf 6c 74 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64  lt code returned
1c7c0 20 62 79 20 73 71 6c 69 74 65 33 5f 65 78 65 63   by sqlite3_exec
1c7c1 28 29 2c 20 73 71 6c 69 74 65 33 5f 72 65 73 65  (), sqlite3_rese
1c7c2 74 28 29 2c 0a 2a 2a 20 6f 72 20 73 71 6c 69 74  t(),.** or sqlit
1c7c3 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 2e 20 20  e3_finalize().  
1c7c4 46 6f 72 20 61 20 6e 6f 72 6d 61 6c 20 68 61 6c  For a normal hal
1c7c5 74 2c 20 74 68 69 73 20 73 68 6f 75 6c 64 20 62  t, this should b
1c7c6 65 20 53 51 4c 49 54 45 5f 4f 4b 20 28 30 29 2e  e SQLITE_OK (0).
1c7c7 0a 2a 2a 20 46 6f 72 20 65 72 72 6f 72 73 2c 20  .** For errors, 
1c7c8 69 74 20 63 61 6e 20 62 65 20 73 6f 6d 65 20 6f  it can be some o
1c7c9 74 68 65 72 20 76 61 6c 75 65 2e 20 20 49 66 20  ther value.  If 
1c7ca 50 31 21 3d 30 20 74 68 65 6e 20 50 32 20 77 69  P1!=0 then P2 wi
1c7cb 6c 6c 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20  ll determine.** 
1c7cc 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  whether or not t
1c7cd 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 63  o rollback the c
1c7ce 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69  urrent transacti
1c7cf 6f 6e 2e 20 20 44 6f 20 6e 6f 74 20 72 6f 6c 6c  on.  Do not roll
1c7d0 62 61 63 6b 0a 2a 2a 20 69 66 20 50 32 3d 3d 4f  back.** if P2==O
1c7d1 45 5f 46 61 69 6c 2e 20 44 6f 20 74 68 65 20 72  E_Fail. Do the r
1c7d2 6f 6c 6c 62 61 63 6b 20 69 66 20 50 32 3d 3d 4f  ollback if P2==O
1c7d3 45 5f 52 6f 6c 6c 62 61 63 6b 2e 20 20 49 66 20  E_Rollback.  If 
1c7d4 50 32 3d 3d 4f 45 5f 41 62 6f 72 74 2c 0a 2a 2a  P2==OE_Abort,.**
1c7d5 20 74 68 65 6e 20 62 61 63 6b 20 6f 75 74 20 61   then back out a
1c7d6 6c 6c 20 63 68 61 6e 67 65 73 20 74 68 61 74 20  ll changes that 
1c7d7 68 61 76 65 20 6f 63 63 75 72 72 65 64 20 64 75  have occurred du
1c7d8 72 69 6e 67 20 74 68 69 73 20 65 78 65 63 75 74  ring this execut
1c7d9 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 56 44  ion of the.** VD
1c7da 42 45 2c 20 62 75 74 20 64 6f 20 6e 6f 74 20 72  BE, but do not r
1c7db 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61 6e  ollback the tran
1c7dc 73 61 63 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20  saction. .**.** 
1c7dd 49 66 20 50 34 20 69 73 20 6e 6f 74 20 6e 75 6c  If P4 is not nul
1c7de 6c 20 74 68 65 6e 20 69 74 20 69 73 20 61 6e 20  l then it is an 
1c7df 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74  error message st
1c7e0 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72  ring..**.** Ther
1c7e1 65 20 69 73 20 61 6e 20 69 6d 70 6c 69 65 64 20  e is an implied 
1c7e2 22 48 61 6c 74 20 30 20 30 20 30 22 20 69 6e 73  "Halt 0 0 0" ins
1c7e3 74 72 75 63 74 69 6f 6e 20 69 6e 73 65 72 74 65  truction inserte
1c7e4 64 20 61 74 20 74 68 65 20 76 65 72 79 20 65 6e  d at the very en
1c7e5 64 20 6f 66 0a 2a 2a 20 65 76 65 72 79 20 70 72  d of.** every pr
1c7e6 6f 67 72 61 6d 2e 20 20 53 6f 20 61 20 6a 75 6d  ogram.  So a jum
1c7e7 70 20 70 61 73 74 20 74 68 65 20 6c 61 73 74 20  p past the last 
1c7e8 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 74  instruction of t
1c7e9 68 65 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 69 73  he program.** is
1c7ea 20 74 68 65 20 73 61 6d 65 20 61 73 20 65 78 65   the same as exe
1c7eb 63 75 74 69 6e 67 20 48 61 6c 74 2e 0a 2a 2f 0a  cuting Halt..*/.
1c7ec 63 61 73 65 20 4f 50 5f 48 61 6c 74 3a 20 7b 0a  case OP_Halt: {.
1c7ed 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 3d 3d 53    if( pOp->p1==S
1c7ee 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 2d 3e 70  QLITE_OK && p->p
1c7ef 46 72 61 6d 65 20 29 7b 0a 20 20 20 20 2f 2a 20  Frame ){.    /* 
1c7f0 48 61 6c 74 20 74 68 65 20 73 75 62 2d 70 72 6f  Halt the sub-pro
1c7f1 67 72 61 6d 2e 20 52 65 74 75 72 6e 20 63 6f 6e  gram. Return con
1c7f2 74 72 6f 6c 20 74 6f 20 74 68 65 20 70 61 72 65  trol to the pare
1c7f3 6e 74 20 66 72 61 6d 65 2e 20 2a 2f 0a 20 20 20  nt frame. */.   
1c7f4 20 56 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61   VdbeFrame *pFra
1c7f5 6d 65 20 3d 20 70 2d 3e 70 46 72 61 6d 65 3b 0a  me = p->pFrame;.
1c7f6 20 20 20 20 70 2d 3e 70 46 72 61 6d 65 20 3d 20      p->pFrame = 
1c7f7 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 3b  pFrame->pParent;
1c7f8 0a 20 20 20 20 70 2d 3e 6e 46 72 61 6d 65 2d 2d  .    p->nFrame--
1c7f9 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
1c7fa 65 53 65 74 43 68 61 6e 67 65 73 28 64 62 2c 20  eSetChanges(db, 
1c7fb 70 2d 3e 6e 43 68 61 6e 67 65 29 3b 0a 20 20 20  p->nChange);.   
1c7fc 20 70 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   pc = sqlite3Vdb
1c7fd 65 46 72 61 6d 65 52 65 73 74 6f 72 65 28 70 46  eFrameRestore(pF
1c7fe 72 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 70  rame);.    if( p
1c7ff 4f 70 2d 3e 70 32 3d 3d 4f 45 5f 49 67 6e 6f 72  Op->p2==OE_Ignor
1c800 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e  e ){.      /* In
1c801 73 74 72 75 63 74 69 6f 6e 20 70 63 20 69 73 20  struction pc is 
1c802 74 68 65 20 4f 50 5f 50 72 6f 67 72 61 6d 20 74  the OP_Program t
1c803 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20  hat invoked the 
1c804 73 75 62 2d 70 72 6f 67 72 61 6d 20 0a 20 20 20  sub-program .   
1c805 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20     ** currently 
1c806 62 65 69 6e 67 20 68 61 6c 74 65 64 2e 20 49 66  being halted. If
1c807 20 74 68 65 20 70 32 20 69 6e 73 74 72 75 63 74   the p2 instruct
1c808 69 6f 6e 20 6f 66 20 74 68 69 73 20 4f 50 5f 48  ion of this OP_H
1c809 61 6c 74 0a 20 20 20 20 20 20 2a 2a 20 69 6e 73  alt.      ** ins
1c80a 74 72 75 63 74 69 6f 6e 20 69 73 20 73 65 74 20  truction is set 
1c80b 74 6f 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 74 68  to OE_Ignore, th
1c80c 65 6e 20 74 68 65 20 73 75 62 2d 70 72 6f 67 72  en the sub-progr
1c80d 61 6d 20 69 73 20 74 68 72 6f 77 69 6e 67 0a 20  am is throwing. 
1c80e 20 20 20 20 20 2a 2a 20 61 6e 20 49 47 4e 4f 52       ** an IGNOR
1c80f 45 20 65 78 63 65 70 74 69 6f 6e 2e 20 49 6e 20  E exception. In 
1c810 74 68 69 73 20 63 61 73 65 20 6a 75 6d 70 20 74  this case jump t
1c811 6f 20 74 68 65 20 61 64 64 72 65 73 73 20 73 70  o the address sp
1c812 65 63 69 66 69 65 64 0a 20 20 20 20 20 20 2a 2a  ecified.      **
1c813 20 61 73 20 74 68 65 20 70 32 20 6f 66 20 74 68   as the p2 of th
1c814 65 20 63 61 6c 6c 69 6e 67 20 4f 50 5f 50 72 6f  e calling OP_Pro
1c815 67 72 61 6d 2e 20 20 2a 2f 0a 20 20 20 20 20 20  gram.  */.      
1c816 70 63 20 3d 20 70 2d 3e 61 4f 70 5b 70 63 5d 2e  pc = p->aOp[pc].
1c817 70 32 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  p2-1;.    }.    
1c818 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 70 2d  break;.  }..  p-
1c819 3e 72 63 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20  >rc = pOp->p1;. 
1c81a 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e 20   p->errorAction 
1c81b 3d 20 28 75 38 29 70 4f 70 2d 3e 70 32 3b 0a 20  = (u8)pOp->p2;. 
1c81c 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 69   p->pc = pc;.  i
1c81d 66 28 20 70 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a  f( pOp->p4.z ){.
1c81e 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74      sqlite3SetSt
1c81f 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67  ring(&p->zErrMsg
1c820 2c 20 64 62 2c 20 22 25 73 22 2c 20 70 4f 70 2d  , db, "%s", pOp-
1c821 3e 70 34 2e 7a 29 3b 0a 20 20 7d 0a 20 20 72 63  >p4.z);.  }.  rc
1c822 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 48 61   = sqlite3VdbeHa
1c823 6c 74 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28  lt(p);.  assert(
1c824 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59   rc==SQLITE_BUSY
1c825 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f   || rc==SQLITE_O
1c826 4b 20 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  K );.  if( rc==S
1c827 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20  QLITE_BUSY ){.  
1c828 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53    p->rc = rc = S
1c829 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65  QLITE_BUSY;.  }e
1c82a 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 70 2d  lse{.    rc = p-
1c82b 3e 72 63 20 3f 20 53 51 4c 49 54 45 5f 45 52 52  >rc ? SQLITE_ERR
1c82c 4f 52 20 3a 20 53 51 4c 49 54 45 5f 44 4f 4e 45  OR : SQLITE_DONE
1c82d 3b 0a 20 20 7d 0a 20 20 67 6f 74 6f 20 76 64 62  ;.  }.  goto vdb
1c82e 65 5f 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20  e_return;.}../* 
1c82f 4f 70 63 6f 64 65 3a 20 49 6e 74 65 67 65 72 20  Opcode: Integer 
1c830 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  P1 P2 * * *.**.*
1c831 2a 20 54 68 65 20 33 32 2d 62 69 74 20 69 6e 74  * The 32-bit int
1c832 65 67 65 72 20 76 61 6c 75 65 20 50 31 20 69 73  eger value P1 is
1c833 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 72 65   written into re
1c834 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61  gister P2..*/.ca
1c835 73 65 20 4f 50 5f 49 6e 74 65 67 65 72 3a 20 7b  se OP_Integer: {
1c836 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32           /* out2
1c837 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20  -prerelease */. 
1c838 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d   pOut->flags = M
1c839 45 4d 5f 49 6e 74 3b 0a 20 20 70 4f 75 74 2d 3e  EM_Int;.  pOut->
1c83a 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20  u.i = pOp->p1;. 
1c83b 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1c83c 63 6f 64 65 3a 20 49 6e 74 36 34 20 2a 20 50 32  code: Int64 * P2
1c83d 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34   * P4 *.**.** P4
1c83e 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
1c83f 20 61 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65   a 64-bit intege
1c840 72 20 76 61 6c 75 65 2e 0a 2a 2a 20 57 72 69 74  r value..** Writ
1c841 65 20 74 68 61 74 20 76 61 6c 75 65 20 69 6e 74  e that value int
1c842 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a  o register P2..*
1c843 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 74 36 34 3a  /.case OP_Int64:
1c844 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20   {           /* 
1c845 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20  out2-prerelease 
1c846 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  */.  assert( pOp
1c847 2d 3e 70 34 2e 70 49 36 34 21 3d 30 20 29 3b 0a  ->p4.pI64!=0 );.
1c848 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20    pOut->flags = 
1c849 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 4f 75 74 2d  MEM_Int;.  pOut-
1c84a 3e 75 2e 69 20 3d 20 2a 70 4f 70 2d 3e 70 34 2e  >u.i = *pOp->p4.
1c84b 70 49 36 34 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  pI64;.  break;.}
1c84c 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 61  ../* Opcode: Rea
1c84d 6c 20 2a 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a  l * P2 * P4 *.**
1c84e 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e  .** P4 is a poin
1c84f 74 65 72 20 74 6f 20 61 20 36 34 2d 62 69 74 20  ter to a 64-bit 
1c850 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76  floating point v
1c851 61 6c 75 65 2e 0a 2a 2a 20 57 72 69 74 65 20 74  alue..** Write t
1c852 68 61 74 20 76 61 6c 75 65 20 69 6e 74 6f 20 72  hat value into r
1c853 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63  egister P2..*/.c
1c854 61 73 65 20 4f 50 5f 52 65 61 6c 3a 20 7b 20 20  ase OP_Real: {  
1c855 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d            /* sam
1c856 65 20 61 73 20 54 4b 5f 46 4c 4f 41 54 2c 20 6f  e as TK_FLOAT, o
1c857 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a  ut2-prerelease *
1c858 2f 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20  /.  pOut->flags 
1c859 3d 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20 20 61 73  = MEM_Real;.  as
1c85a 73 65 72 74 28 20 21 73 71 6c 69 74 65 33 49 73  sert( !sqlite3Is
1c85b 4e 61 4e 28 2a 70 4f 70 2d 3e 70 34 2e 70 52 65  NaN(*pOp->p4.pRe
1c85c 61 6c 29 20 29 3b 0a 20 20 70 4f 75 74 2d 3e 72  al) );.  pOut->r
1c85d 20 3d 20 2a 70 4f 70 2d 3e 70 34 2e 70 52 65 61   = *pOp->p4.pRea
1c85e 6c 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  l;.  break;.}../
1c85f 2a 20 4f 70 63 6f 64 65 3a 20 53 74 72 69 6e 67  * Opcode: String
1c860 38 20 2a 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a  8 * P2 * P4 *.**
1c861 0a 2a 2a 20 50 34 20 70 6f 69 6e 74 73 20 74 6f  .** P4 points to
1c862 20 61 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65   a nul terminate
1c863 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 2e 20  d UTF-8 string. 
1c864 54 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20 74  This opcode is t
1c865 72 61 6e 73 66 6f 72 6d 65 64 20 0a 2a 2a 20 69  ransformed .** i
1c866 6e 74 6f 20 61 6e 20 4f 50 5f 53 74 72 69 6e 67  nto an OP_String
1c867 20 62 65 66 6f 72 65 20 69 74 20 69 73 20 65 78   before it is ex
1c868 65 63 75 74 65 64 20 66 6f 72 20 74 68 65 20 66  ecuted for the f
1c869 69 72 73 74 20 74 69 6d 65 2e 0a 2a 2f 0a 63 61  irst time..*/.ca
1c86a 73 65 20 4f 50 5f 53 74 72 69 6e 67 38 3a 20 7b  se OP_String8: {
1c86b 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65           /* same
1c86c 20 61 73 20 54 4b 5f 53 54 52 49 4e 47 2c 20 6f   as TK_STRING, o
1c86d 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a  ut2-prerelease *
1c86e 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  /.  assert( pOp-
1c86f 3e 70 34 2e 7a 21 3d 30 20 29 3b 0a 20 20 70 4f  >p4.z!=0 );.  pO
1c870 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 53  p->opcode = OP_S
1c871 74 72 69 6e 67 3b 0a 20 20 70 4f 70 2d 3e 70 31  tring;.  pOp->p1
1c872 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
1c873 33 30 28 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 0a  30(pOp->p4.z);..
1c874 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1c875 4d 49 54 5f 55 54 46 31 36 0a 20 20 69 66 28 20  MIT_UTF16.  if( 
1c876 65 6e 63 6f 64 69 6e 67 21 3d 53 51 4c 49 54 45  encoding!=SQLITE
1c877 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 72 63 20  _UTF8 ){.    rc 
1c878 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  = sqlite3VdbeMem
1c879 53 65 74 53 74 72 28 70 4f 75 74 2c 20 70 4f 70  SetStr(pOut, pOp
1c87a 2d 3e 70 34 2e 7a 2c 20 2d 31 2c 20 53 51 4c 49  ->p4.z, -1, SQLI
1c87b 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f  TE_UTF8, SQLITE_
1c87c 53 54 41 54 49 43 29 3b 0a 20 20 20 20 69 66 28  STATIC);.    if(
1c87d 20 72 63 3d 3d 53 51 4c 49 54 45 5f 54 4f 4f 42   rc==SQLITE_TOOB
1c87e 49 47 20 29 20 67 6f 74 6f 20 74 6f 6f 5f 62 69  IG ) goto too_bi
1c87f 67 3b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54  g;.    if( SQLIT
1c880 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 56 64 62  E_OK!=sqlite3Vdb
1c881 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28  eChangeEncoding(
1c882 70 4f 75 74 2c 20 65 6e 63 6f 64 69 6e 67 29 20  pOut, encoding) 
1c883 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20  ) goto no_mem;. 
1c884 20 20 20 61 73 73 65 72 74 28 20 70 4f 75 74 2d     assert( pOut-
1c885 3e 7a 4d 61 6c 6c 6f 63 3d 3d 70 4f 75 74 2d 3e  >zMalloc==pOut->
1c886 7a 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  z );.    assert(
1c887 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 26 20 4d   pOut->flags & M
1c888 45 4d 5f 44 79 6e 20 29 3b 0a 20 20 20 20 70 4f  EM_Dyn );.    pO
1c889 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b  ut->zMalloc = 0;
1c88a 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73  .    pOut->flags
1c88b 20 7c 3d 20 4d 45 4d 5f 53 74 61 74 69 63 3b 0a   |= MEM_Static;.
1c88c 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20      pOut->flags 
1c88d 26 3d 20 7e 4d 45 4d 5f 44 79 6e 3b 0a 20 20 20  &= ~MEM_Dyn;.   
1c88e 20 69 66 28 20 70 4f 70 2d 3e 70 34 74 79 70 65   if( pOp->p4type
1c88f 3d 3d 50 34 5f 44 59 4e 41 4d 49 43 20 29 7b 0a  ==P4_DYNAMIC ){.
1c890 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
1c891 72 65 65 28 64 62 2c 20 70 4f 70 2d 3e 70 34 2e  ree(db, pOp->p4.
1c892 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f  z);.    }.    pO
1c893 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 44  p->p4type = P4_D
1c894 59 4e 41 4d 49 43 3b 0a 20 20 20 20 70 4f 70 2d  YNAMIC;.    pOp-
1c895 3e 70 34 2e 7a 20 3d 20 70 4f 75 74 2d 3e 7a 3b  >p4.z = pOut->z;
1c896 0a 20 20 20 20 70 4f 70 2d 3e 70 31 20 3d 20 70  .    pOp->p1 = p
1c897 4f 75 74 2d 3e 6e 3b 0a 20 20 7d 0a 23 65 6e 64  Out->n;.  }.#end
1c898 69 66 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 31  if.  if( pOp->p1
1c899 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  >db->aLimit[SQLI
1c89a 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d  TE_LIMIT_LENGTH]
1c89b 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f   ){.    goto too
1c89c 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 2f 2a 20 46  _big;.  }.  /* F
1c89d 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74  all through to t
1c89e 68 65 20 6e 65 78 74 20 63 61 73 65 2c 20 4f 50  he next case, OP
1c89f 5f 53 74 72 69 6e 67 20 2a 2f 0a 7d 0a 20 20 0a  _String */.}.  .
1c8a0 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 74 72 69 6e  /* Opcode: Strin
1c8a1 67 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a  g P1 P2 * P4 *.*
1c8a2 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 20  *.** The string 
1c8a3 76 61 6c 75 65 20 50 34 20 6f 66 20 6c 65 6e 67  value P4 of leng
1c8a4 74 68 20 50 31 20 28 62 79 74 65 73 29 20 69 73  th P1 (bytes) is
1c8a5 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73   stored in regis
1c8a6 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20  ter P2..*/.case 
1c8a7 4f 50 5f 53 74 72 69 6e 67 3a 20 7b 20 20 20 20  OP_String: {    
1c8a8 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72        /* out2-pr
1c8a9 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73  erelease */.  as
1c8aa 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 7a 21  sert( pOp->p4.z!
1c8ab 3d 30 20 29 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c  =0 );.  pOut->fl
1c8ac 61 67 73 20 3d 20 4d 45 4d 5f 53 74 72 7c 4d 45  ags = MEM_Str|ME
1c8ad 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 54 65 72  M_Static|MEM_Ter
1c8ae 6d 3b 0a 20 20 70 4f 75 74 2d 3e 7a 20 3d 20 70  m;.  pOut->z = p
1c8af 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 70 4f 75 74  Op->p4.z;.  pOut
1c8b0 2d 3e 6e 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20  ->n = pOp->p1;. 
1c8b1 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 65 6e 63   pOut->enc = enc
1c8b2 6f 64 69 6e 67 3b 0a 20 20 55 50 44 41 54 45 5f  oding;.  UPDATE_
1c8b3 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75  MAX_BLOBSIZE(pOu
1c8b4 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  t);.  break;.}..
1c8b5 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 75 6c 6c 20  /* Opcode: Null 
1c8b6 2a 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  * P2 * * *.**.**
1c8b7 20 57 72 69 74 65 20 61 20 4e 55 4c 4c 20 69 6e   Write a NULL in
1c8b8 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a  to register P2..
1c8b9 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 75 6c 6c 3a  */.case OP_Null:
1c8ba 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20   {           /* 
1c8bb 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20  out2-prerelease 
1c8bc 2a 2f 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a  */.  break;.}...
1c8bd 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 6c 6f 62 20  /* Opcode: Blob 
1c8be 50 31 20 50 32 20 2a 20 50 34 0a 2a 2a 0a 2a 2a  P1 P2 * P4.**.**
1c8bf 20 50 34 20 70 6f 69 6e 74 73 20 74 6f 20 61 20   P4 points to a 
1c8c0 62 6c 6f 62 20 6f 66 20 64 61 74 61 20 50 31 20  blob of data P1 
1c8c1 62 79 74 65 73 20 6c 6f 6e 67 2e 20 20 53 74 6f  bytes long.  Sto
1c8c2 72 65 20 74 68 69 73 0a 2a 2a 20 62 6c 6f 62 20  re this.** blob 
1c8c3 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 20  in register P2. 
1c8c4 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  This instruction
1c8c5 20 69 73 20 6e 6f 74 20 63 6f 64 65 64 20 64 69   is not coded di
1c8c6 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 74 68 65  rectly.** by the
1c8c7 20 63 6f 6d 70 69 6c 65 72 2e 20 49 6e 73 74 65   compiler. Inste
1c8c8 61 64 2c 20 74 68 65 20 63 6f 6d 70 69 6c 65 72  ad, the compiler
1c8c9 20 6c 61 79 65 72 20 73 70 65 63 69 66 69 65 73   layer specifies
1c8ca 0a 2a 2a 20 61 6e 20 4f 50 5f 48 65 78 42 6c 6f  .** an OP_HexBlo
1c8cb 62 20 6f 70 63 6f 64 65 2c 20 77 69 74 68 20 74  b opcode, with t
1c8cc 68 65 20 68 65 78 20 73 74 72 69 6e 67 20 72 65  he hex string re
1c8cd 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 0a  presentation of.
1c8ce 2a 2a 20 74 68 65 20 62 6c 6f 62 20 61 73 20 50  ** the blob as P
1c8cf 34 2e 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69  4. This opcode i
1c8d0 73 20 74 72 61 6e 73 66 6f 72 6d 65 64 20 74 6f  s transformed to
1c8d1 20 61 6e 20 4f 50 5f 42 6c 6f 62 0a 2a 2a 20 74   an OP_Blob.** t
1c8d2 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 69 74  he first time it
1c8d3 20 69 73 20 65 78 65 63 75 74 65 64 2e 0a 2a 2f   is executed..*/
1c8d4 0a 63 61 73 65 20 4f 50 5f 42 6c 6f 62 3a 20 7b  .case OP_Blob: {
1c8d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c8d6 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61  /* out2-prerelea
1c8d7 73 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  se */.  assert( 
1c8d8 70 4f 70 2d 3e 70 31 20 3c 3d 20 53 51 4c 49 54  pOp->p1 <= SQLIT
1c8d9 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 29 3b 0a  E_MAX_LENGTH );.
1c8da 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1c8db 53 65 74 53 74 72 28 70 4f 75 74 2c 20 70 4f 70  SetStr(pOut, pOp
1c8dc 2d 3e 70 34 2e 7a 2c 20 70 4f 70 2d 3e 70 31 2c  ->p4.z, pOp->p1,
1c8dd 20 30 2c 20 30 29 3b 0a 20 20 70 4f 75 74 2d 3e   0, 0);.  pOut->
1c8de 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a  enc = encoding;.
1c8df 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f    UPDATE_MAX_BLO
1c8e0 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62  BSIZE(pOut);.  b
1c8e1 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1c8e2 64 65 3a 20 56 61 72 69 61 62 6c 65 20 50 31 20  de: Variable P1 
1c8e3 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a  P2 P3 P4 *.**.**
1c8e4 20 54 72 61 6e 73 66 65 72 20 74 68 65 20 76 61   Transfer the va
1c8e5 6c 75 65 73 20 6f 66 20 62 6f 75 6e 64 20 70 61  lues of bound pa
1c8e6 72 61 6d 65 74 65 72 73 20 50 31 2e 2e 50 31 2b  rameters P1..P1+
1c8e7 50 33 2d 31 20 69 6e 74 6f 20 72 65 67 69 73 74  P3-1 into regist
1c8e8 65 72 73 0a 2a 2a 20 50 32 2e 2e 50 32 2b 50 33  ers.** P2..P2+P3
1c8e9 2d 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  -1..**.** If the
1c8ea 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 61   parameter is na
1c8eb 6d 65 64 2c 20 74 68 65 6e 20 69 74 73 20 6e 61  med, then its na
1c8ec 6d 65 20 61 70 70 65 61 72 73 20 69 6e 20 50 34  me appears in P4
1c8ed 20 61 6e 64 20 50 33 3d 3d 31 2e 0a 2a 2a 20 54   and P3==1..** T
1c8ee 68 65 20 50 34 20 76 61 6c 75 65 20 69 73 20 75  he P4 value is u
1c8ef 73 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 62  sed by sqlite3_b
1c8f0 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61  ind_parameter_na
1c8f1 6d 65 28 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  me()..*/.case OP
1c8f2 5f 56 61 72 69 61 62 6c 65 3a 20 7b 0a 23 69 66  _Variable: {.#if
1c8f3 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72   0  /* local var
1c8f4 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1c8f5 6f 20 75 2e 61 62 20 2a 2f 0a 20 20 69 6e 74 20  o u.ab */.  int 
1c8f6 70 31 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  p1;          /* 
1c8f7 56 61 72 69 61 62 6c 65 20 74 6f 20 63 6f 70 79  Variable to copy
1c8f8 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 70   from */.  int p
1c8f9 32 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52  2;          /* R
1c8fa 65 67 69 73 74 65 72 20 74 6f 20 63 6f 70 79 20  egister to copy 
1c8fb 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20  to */.  int n;  
1c8fc 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
1c8fd 65 72 20 6f 66 20 76 61 6c 75 65 73 20 6c 65 66  er of values lef
1c8fe 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 4d  t to copy */.  M
1c8ff 65 6d 20 2a 70 56 61 72 3b 20 20 20 20 20 20 20  em *pVar;       
1c900 2f 2a 20 56 61 6c 75 65 20 62 65 69 6e 67 20 74  /* Value being t
1c901 72 61 6e 73 66 65 72 72 65 64 20 2a 2f 0a 23 65  ransferred */.#e
1c902 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1c903 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1c904 74 6f 20 75 2e 61 62 20 2a 2f 0a 0a 20 20 75 2e  to u.ab */..  u.
1c905 61 62 2e 70 31 20 3d 20 70 4f 70 2d 3e 70 31 20  ab.p1 = pOp->p1 
1c906 2d 20 31 3b 0a 20 20 75 2e 61 62 2e 70 32 20 3d  - 1;.  u.ab.p2 =
1c907 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 62   pOp->p2;.  u.ab
1c908 2e 6e 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20  .n = pOp->p3;.  
1c909 61 73 73 65 72 74 28 20 75 2e 61 62 2e 70 31 3e  assert( u.ab.p1>
1c90a 3d 30 20 26 26 20 75 2e 61 62 2e 70 31 2b 75 2e  =0 && u.ab.p1+u.
1c90b 61 62 2e 6e 3c 3d 70 2d 3e 6e 56 61 72 20 29 3b  ab.n<=p->nVar );
1c90c 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 62 2e  .  assert( u.ab.
1c90d 70 32 3e 3d 31 20 26 26 20 75 2e 61 62 2e 70 32  p2>=1 && u.ab.p2
1c90e 2b 75 2e 61 62 2e 6e 2d 31 3c 3d 70 2d 3e 6e 4d  +u.ab.n-1<=p->nM
1c90f 65 6d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  em );.  assert( 
1c910 70 4f 70 2d 3e 70 34 2e 7a 3d 3d 30 20 7c 7c 20  pOp->p4.z==0 || 
1c911 70 4f 70 2d 3e 70 33 3d 3d 31 20 29 3b 0a 0a 20  pOp->p3==1 );.. 
1c912 20 77 68 69 6c 65 28 20 75 2e 61 62 2e 6e 2d 2d   while( u.ab.n--
1c913 20 3e 20 30 20 29 7b 0a 20 20 20 20 75 2e 61 62   > 0 ){.    u.ab
1c914 2e 70 56 61 72 20 3d 20 26 70 2d 3e 61 56 61 72  .pVar = &p->aVar
1c915 5b 75 2e 61 62 2e 70 31 2b 2b 5d 3b 0a 20 20 20  [u.ab.p1++];.   
1c916 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65   if( sqlite3Vdbe
1c917 4d 65 6d 54 6f 6f 42 69 67 28 75 2e 61 62 2e 70  MemTooBig(u.ab.p
1c918 56 61 72 29 20 29 7b 0a 20 20 20 20 20 20 67 6f  Var) ){.      go
1c919 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20  to too_big;.    
1c91a 7d 0a 20 20 20 20 70 4f 75 74 20 3d 20 26 70 2d  }.    pOut = &p-
1c91b 3e 61 4d 65 6d 5b 75 2e 61 62 2e 70 32 2b 2b 5d  >aMem[u.ab.p2++]
1c91c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
1c91d 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72  eMemReleaseExter
1c91e 6e 61 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20 70  nal(pOut);.    p
1c91f 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Out->flags = MEM
1c920 5f 4e 75 6c 6c 3b 0a 20 20 20 20 73 71 6c 69 74  _Null;.    sqlit
1c921 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77  e3VdbeMemShallow
1c922 43 6f 70 79 28 70 4f 75 74 2c 20 75 2e 61 62 2e  Copy(pOut, u.ab.
1c923 70 56 61 72 2c 20 4d 45 4d 5f 53 74 61 74 69 63  pVar, MEM_Static
1c924 29 3b 0a 20 20 20 20 55 50 44 41 54 45 5f 4d 41  );.    UPDATE_MA
1c925 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29  X_BLOBSIZE(pOut)
1c926 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1c927 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 6f 76  ../* Opcode: Mov
1c928 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a  e P1 P2 P3 * *.*
1c929 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 76 61  *.** Move the va
1c92a 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74 65 72  lues in register
1c92b 20 50 31 2e 2e 50 31 2b 50 33 2d 31 20 6f 76 65   P1..P1+P3-1 ove
1c92c 72 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74  r into.** regist
1c92d 65 72 73 20 50 32 2e 2e 50 32 2b 50 33 2d 31 2e  ers P2..P2+P3-1.
1c92e 20 20 52 65 67 69 73 74 65 72 73 20 50 31 2e 2e    Registers P1..
1c92f 50 31 2b 50 31 2d 31 20 61 72 65 0a 2a 2a 20 6c  P1+P1-1 are.** l
1c930 65 66 74 20 68 6f 6c 64 69 6e 67 20 61 20 4e 55  eft holding a NU
1c931 4c 4c 2e 20 20 49 74 20 69 73 20 61 6e 20 65 72  LL.  It is an er
1c932 72 6f 72 20 66 6f 72 20 72 65 67 69 73 74 65 72  ror for register
1c933 20 72 61 6e 67 65 73 0a 2a 2a 20 50 31 2e 2e 50   ranges.** P1..P
1c934 31 2b 50 33 2d 31 20 61 6e 64 20 50 32 2e 2e 50  1+P3-1 and P2..P
1c935 32 2b 50 33 2d 31 20 74 6f 20 6f 76 65 72 6c 61  2+P3-1 to overla
1c936 70 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 6f  p..*/.case OP_Mo
1c937 76 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20  ve: {.#if 0  /* 
1c938 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1c939 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 63 20  moved into u.ac 
1c93a 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 6c 6c  */.  char *zMall
1c93b 6f 63 3b 20 20 20 2f 2a 20 48 6f 6c 64 69 6e 67  oc;   /* Holding
1c93c 20 76 61 72 69 61 62 6c 65 20 66 6f 72 20 61 6c   variable for al
1c93d 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 20 2a  located memory *
1c93e 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20  /.  int n;      
1c93f 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1c940 66 20 72 65 67 69 73 74 65 72 73 20 6c 65 66 74  f registers left
1c941 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 69 6e   to copy */.  in
1c942 74 20 70 31 3b 20 20 20 20 20 20 20 20 20 20 2f  t p1;          /
1c943 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20 63 6f  * Register to co
1c944 70 79 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74  py from */.  int
1c945 20 70 32 3b 20 20 20 20 20 20 20 20 20 20 2f 2a   p2;          /*
1c946 20 52 65 67 69 73 74 65 72 20 74 6f 20 63 6f 70   Register to cop
1c947 79 20 74 6f 20 2a 2f 0a 23 65 6e 64 69 66 20 2f  y to */.#endif /
1c948 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1c949 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
1c94a 63 20 2a 2f 0a 0a 20 20 75 2e 61 63 2e 6e 20 3d  c */..  u.ac.n =
1c94b 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 75 2e 61 63   pOp->p3;.  u.ac
1c94c 2e 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20  .p1 = pOp->p1;. 
1c94d 20 75 2e 61 63 2e 70 32 20 3d 20 70 4f 70 2d 3e   u.ac.p2 = pOp->
1c94e 70 32 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e  p2;.  assert( u.
1c94f 61 63 2e 6e 3e 30 20 26 26 20 75 2e 61 63 2e 70  ac.n>0 && u.ac.p
1c950 31 3e 30 20 26 26 20 75 2e 61 63 2e 70 32 3e 30  1>0 && u.ac.p2>0
1c951 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e   );.  assert( u.
1c952 61 63 2e 70 31 2b 75 2e 61 63 2e 6e 3c 3d 75 2e  ac.p1+u.ac.n<=u.
1c953 61 63 2e 70 32 20 7c 7c 20 75 2e 61 63 2e 70 32  ac.p2 || u.ac.p2
1c954 2b 75 2e 61 63 2e 6e 3c 3d 75 2e 61 63 2e 70 31  +u.ac.n<=u.ac.p1
1c955 20 29 3b 0a 0a 20 20 70 49 6e 31 20 3d 20 26 70   );..  pIn1 = &p
1c956 2d 3e 61 4d 65 6d 5b 75 2e 61 63 2e 70 31 5d 3b  ->aMem[u.ac.p1];
1c957 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d  .  pOut = &p->aM
1c958 65 6d 5b 75 2e 61 63 2e 70 32 5d 3b 0a 20 20 77  em[u.ac.p2];.  w
1c959 68 69 6c 65 28 20 75 2e 61 63 2e 6e 2d 2d 20 29  hile( u.ac.n-- )
1c95a 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f  {.    assert( pO
1c95b 75 74 3c 3d 26 70 2d 3e 61 4d 65 6d 5b 70 2d 3e  ut<=&p->aMem[p->
1c95c 6e 4d 65 6d 5d 20 29 3b 0a 20 20 20 20 61 73 73  nMem] );.    ass
1c95d 65 72 74 28 20 70 49 6e 31 3c 3d 26 70 2d 3e 61  ert( pIn1<=&p->a
1c95e 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 5d 20 29 3b 0a  Mem[p->nMem] );.
1c95f 20 20 20 20 75 2e 61 63 2e 7a 4d 61 6c 6c 6f 63      u.ac.zMalloc
1c960 20 3d 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63   = pOut->zMalloc
1c961 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 4d 61 6c  ;.    pOut->zMal
1c962 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c  loc = 0;.    sql
1c963 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28  ite3VdbeMemMove(
1c964 70 4f 75 74 2c 20 70 49 6e 31 29 3b 0a 20 20 20  pOut, pIn1);.   
1c965 20 70 49 6e 31 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d   pIn1->zMalloc =
1c966 20 75 2e 61 63 2e 7a 4d 61 6c 6c 6f 63 3b 0a 20   u.ac.zMalloc;. 
1c967 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43     REGISTER_TRAC
1c968 45 28 75 2e 61 63 2e 70 32 2b 2b 2c 20 70 4f 75  E(u.ac.p2++, pOu
1c969 74 29 3b 0a 20 20 20 20 70 49 6e 31 2b 2b 3b 0a  t);.    pIn1++;.
1c96a 20 20 20 20 70 4f 75 74 2b 2b 3b 0a 20 20 7d 0a      pOut++;.  }.
1c96b 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1c96c 70 63 6f 64 65 3a 20 43 6f 70 79 20 50 31 20 50  pcode: Copy P1 P
1c96d 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 61  2 * * *.**.** Ma
1c96e 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 72 65 67  ke a copy of reg
1c96f 69 73 74 65 72 20 50 31 20 69 6e 74 6f 20 72 65  ister P1 into re
1c970 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a  gister P2..**.**
1c971 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   This instructio
1c972 6e 20 6d 61 6b 65 73 20 61 20 64 65 65 70 20 63  n makes a deep c
1c973 6f 70 79 20 6f 66 20 74 68 65 20 76 61 6c 75 65  opy of the value
1c974 2e 20 20 41 20 64 75 70 6c 69 63 61 74 65 0a 2a  .  A duplicate.*
1c975 2a 20 69 73 20 6d 61 64 65 20 6f 66 20 61 6e 79  * is made of any
1c976 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20   string or blob 
1c977 63 6f 6e 73 74 61 6e 74 2e 20 20 53 65 65 20 61  constant.  See a
1c978 6c 73 6f 20 4f 50 5f 53 43 6f 70 79 2e 0a 2a 2f  lso OP_SCopy..*/
1c979 0a 63 61 73 65 20 4f 50 5f 43 6f 70 79 3a 20 7b  .case OP_Copy: {
1c97a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1c97b 69 6e 31 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  in1 */.  assert(
1c97c 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20   pOp->p2>0 );.  
1c97d 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c  assert( pOp->p2<
1c97e 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f  =p->nMem );.  pO
1c97f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  ut = &p->aMem[pO
1c980 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 73 65 72 74  p->p2];.  assert
1c981 28 20 70 4f 75 74 21 3d 70 49 6e 31 20 29 3b 0a  ( pOut!=pIn1 );.
1c982 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1c983 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 74  ShallowCopy(pOut
1c984 2c 20 70 49 6e 31 2c 20 4d 45 4d 5f 45 70 68 65  , pIn1, MEM_Ephe
1c985 6d 29 3b 0a 20 20 44 65 65 70 68 65 6d 65 72 61  m);.  Deephemera
1c986 6c 69 7a 65 28 70 4f 75 74 29 3b 0a 20 20 52 45  lize(pOut);.  RE
1c987 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70  GISTER_TRACE(pOp
1c988 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 62  ->p2, pOut);.  b
1c989 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1c98a 64 65 3a 20 53 43 6f 70 79 20 50 31 20 50 32 20  de: SCopy P1 P2 
1c98b 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 61 6b 65  * * *.**.** Make
1c98c 20 61 20 73 68 61 6c 6c 6f 77 20 63 6f 70 79 20   a shallow copy 
1c98d 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 20 69  of register P1 i
1c98e 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e  nto register P2.
1c98f 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74  .**.** This inst
1c990 72 75 63 74 69 6f 6e 20 6d 61 6b 65 73 20 61 20  ruction makes a 
1c991 73 68 61 6c 6c 6f 77 20 63 6f 70 79 20 6f 66 20  shallow copy of 
1c992 74 68 65 20 76 61 6c 75 65 2e 20 20 49 66 20 74  the value.  If t
1c993 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 61  he value.** is a
1c994 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 2c   string or blob,
1c995 20 74 68 65 6e 20 74 68 65 20 63 6f 70 79 20 69   then the copy i
1c996 73 20 6f 6e 6c 79 20 61 20 70 6f 69 6e 74 65 72  s only a pointer
1c997 20 74 6f 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69   to the.** origi
1c998 6e 61 6c 20 61 6e 64 20 68 65 6e 63 65 20 69 66  nal and hence if
1c999 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 63 68   the original ch
1c99a 61 6e 67 65 73 20 73 6f 20 77 69 6c 6c 20 74 68  anges so will th
1c99b 65 20 63 6f 70 79 2e 0a 2a 2a 20 57 6f 72 73 65  e copy..** Worse
1c99c 2c 20 69 66 20 74 68 65 20 6f 72 69 67 69 6e 61  , if the origina
1c99d 6c 20 69 73 20 64 65 61 6c 6c 6f 63 61 74 65 64  l is deallocated
1c99e 2c 20 74 68 65 20 63 6f 70 79 20 62 65 63 6f 6d  , the copy becom
1c99f 65 73 20 69 6e 76 61 6c 69 64 2e 0a 2a 2a 20 54  es invalid..** T
1c9a0 68 75 73 20 74 68 65 20 70 72 6f 67 72 61 6d 20  hus the program 
1c9a1 6d 75 73 74 20 67 75 61 72 61 6e 74 65 65 20 74  must guarantee t
1c9a2 68 61 74 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  hat the original
1c9a3 20 77 69 6c 6c 20 6e 6f 74 20 63 68 61 6e 67 65   will not change
1c9a4 0a 2a 2a 20 64 75 72 69 6e 67 20 74 68 65 20 6c  .** during the l
1c9a5 69 66 65 74 69 6d 65 20 6f 66 20 74 68 65 20 63  ifetime of the c
1c9a6 6f 70 79 2e 20 20 55 73 65 20 4f 50 5f 43 6f 70  opy.  Use OP_Cop
1c9a7 79 20 74 6f 20 6d 61 6b 65 20 61 20 63 6f 6d 70  y to make a comp
1c9a8 6c 65 74 65 0a 2a 2a 20 63 6f 70 79 2e 0a 2a 2f  lete.** copy..*/
1c9a9 0a 63 61 73 65 20 4f 50 5f 53 43 6f 70 79 3a 20  .case OP_SCopy: 
1c9aa 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  {            /* 
1c9ab 69 6e 31 20 2a 2f 0a 20 20 52 45 47 49 53 54 45  in1 */.  REGISTE
1c9ac 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c  R_TRACE(pOp->p1,
1c9ad 20 70 49 6e 31 29 3b 0a 20 20 61 73 73 65 72 74   pIn1);.  assert
1c9ae 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20  ( pOp->p2>0 );. 
1c9af 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32   assert( pOp->p2
1c9b0 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70  <=p->nMem );.  p
1c9b1 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  Out = &p->aMem[p
1c9b2 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 73 65 72  Op->p2];.  asser
1c9b3 74 28 20 70 4f 75 74 21 3d 70 49 6e 31 20 29 3b  t( pOut!=pIn1 );
1c9b4 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
1c9b5 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f 75  mShallowCopy(pOu
1c9b6 74 2c 20 70 49 6e 31 2c 20 4d 45 4d 5f 45 70 68  t, pIn1, MEM_Eph
1c9b7 65 6d 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f  em);.  REGISTER_
1c9b8 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70  TRACE(pOp->p2, p
1c9b9 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  Out);.  break;.}
1c9ba 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 73  ../* Opcode: Res
1c9bb 75 6c 74 52 6f 77 20 50 31 20 50 32 20 2a 20 2a  ultRow P1 P2 * *
1c9bc 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 67   *.**.** The reg
1c9bd 69 73 74 65 72 73 20 50 31 20 74 68 72 6f 75 67  isters P1 throug
1c9be 68 20 50 31 2b 50 32 2d 31 20 63 6f 6e 74 61 69  h P1+P2-1 contai
1c9bf 6e 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f  n a single row o
1c9c0 66 0a 2a 2a 20 72 65 73 75 6c 74 73 2e 20 54 68  f.** results. Th
1c9c1 69 73 20 6f 70 63 6f 64 65 20 63 61 75 73 65 73  is opcode causes
1c9c2 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 65   the sqlite3_ste
1c9c3 70 28 29 20 63 61 6c 6c 20 74 6f 20 74 65 72 6d  p() call to term
1c9c4 69 6e 61 74 65 0a 2a 2a 20 77 69 74 68 20 61 6e  inate.** with an
1c9c5 20 53 51 4c 49 54 45 5f 52 4f 57 20 72 65 74 75   SQLITE_ROW retu
1c9c6 72 6e 20 63 6f 64 65 20 61 6e 64 20 69 74 20 73  rn code and it s
1c9c7 65 74 73 20 75 70 20 74 68 65 20 73 71 6c 69 74  ets up the sqlit
1c9c8 65 33 5f 73 74 6d 74 0a 2a 2a 20 73 74 72 75 63  e3_stmt.** struc
1c9c9 74 75 72 65 20 74 6f 20 70 72 6f 76 69 64 65 20  ture to provide 
1c9ca 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 74 6f  access to the to
1c9cb 70 20 50 31 20 76 61 6c 75 65 73 20 61 73 20 74  p P1 values as t
1c9cc 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 72 6f 77  he result.** row
1c9cd 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 73  ..*/.case OP_Res
1c9ce 75 6c 74 52 6f 77 3a 20 7b 0a 23 69 66 20 30 20  ultRow: {.#if 0 
1c9cf 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1c9d0 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1c9d1 2e 61 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d  .ad */.  Mem *pM
1c9d2 65 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a 23 65 6e  em;.  int i;.#en
1c9d3 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
1c9d4 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1c9d5 6f 20 75 2e 61 64 20 2a 2f 0a 20 20 61 73 73 65  o u.ad */.  asse
1c9d6 72 74 28 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d  rt( p->nResColum
1c9d7 6e 3d 3d 70 4f 70 2d 3e 70 32 20 29 3b 0a 20 20  n==pOp->p2 );.  
1c9d8 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e  assert( pOp->p1>
1c9d9 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
1c9da 4f 70 2d 3e 70 31 2b 70 4f 70 2d 3e 70 32 3c 3d  Op->p1+pOp->p2<=
1c9db 70 2d 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 0a 20 20  p->nMem+1 );..  
1c9dc 2f 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54 45  /* If the SQLITE
1c9dd 5f 43 6f 75 6e 74 52 6f 77 73 20 66 6c 61 67 20  _CountRows flag 
1c9de 69 73 20 73 65 74 20 69 6e 20 73 71 6c 69 74 65  is set in sqlite
1c9df 33 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74 68  3.flags mask, th
1c9e0 65 6e 0a 20 20 2a 2a 20 44 4d 4c 20 73 74 61 74  en.  ** DML stat
1c9e1 65 6d 65 6e 74 73 20 69 6e 76 6f 6b 65 20 74 68  ements invoke th
1c9e2 69 73 20 6f 70 63 6f 64 65 20 74 6f 20 72 65 74  is opcode to ret
1c9e3 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
1c9e4 66 20 72 6f 77 73 0a 20 20 2a 2a 20 6d 6f 64 69  f rows.  ** modi
1c9e5 66 69 65 64 20 74 6f 20 74 68 65 20 75 73 65 72  fied to the user
1c9e6 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 6f 6e  . This is the on
1c9e7 6c 79 20 77 61 79 20 74 68 61 74 20 61 20 56 4d  ly way that a VM
1c9e8 20 74 68 61 74 0a 20 20 2a 2a 20 6f 70 65 6e 73   that.  ** opens
1c9e9 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61   a statement tra
1c9ea 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 69 6e 76  nsaction may inv
1c9eb 6f 6b 65 20 74 68 69 73 20 6f 70 63 6f 64 65 2e  oke this opcode.
1c9ec 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 63 61  .  **.  ** In ca
1c9ed 73 65 20 74 68 69 73 20 69 73 20 73 75 63 68 20  se this is such 
1c9ee 61 20 73 74 61 74 65 6d 65 6e 74 2c 20 63 6c 6f  a statement, clo
1c9ef 73 65 20 61 6e 79 20 73 74 61 74 65 6d 65 6e 74  se any statement
1c9f0 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a   transaction.  *
1c9f1 2a 20 6f 70 65 6e 65 64 20 62 79 20 74 68 69 73  * opened by this
1c9f2 20 56 4d 20 62 65 66 6f 72 65 20 72 65 74 75 72   VM before retur
1c9f3 6e 69 6e 67 20 63 6f 6e 74 72 6f 6c 20 74 6f 20  ning control to 
1c9f4 74 68 65 20 75 73 65 72 2e 20 54 68 69 73 20 69  the user. This i
1c9f5 73 20 74 6f 0a 20 20 2a 2a 20 65 6e 73 75 72 65  s to.  ** ensure
1c9f6 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74 2d   that statement-
1c9f7 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 61 72 65  transactions are
1c9f8 20 61 6c 77 61 79 73 20 6e 65 73 74 65 64 2c 20   always nested, 
1c9f9 6e 6f 74 20 6f 76 65 72 6c 61 70 70 69 6e 67 2e  not overlapping.
1c9fa 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 6f 70 65  .  ** If the ope
1c9fb 6e 20 73 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e  n statement-tran
1c9fc 73 61 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 63  saction is not c
1c9fd 6c 6f 73 65 64 20 68 65 72 65 2c 20 74 68 65 6e  losed here, then
1c9fe 20 74 68 65 20 75 73 65 72 0a 20 20 2a 2a 20 6d   the user.  ** m
1c9ff 61 79 20 73 74 65 70 20 61 6e 6f 74 68 65 72 20  ay step another 
1ca00 56 4d 20 74 68 61 74 20 6f 70 65 6e 73 20 69 74  VM that opens it
1ca01 73 20 6f 77 6e 20 73 74 61 74 65 6d 65 6e 74 20  s own statement 
1ca02 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 69  transaction. Thi
1ca03 73 0a 20 20 2a 2a 20 6d 61 79 20 6c 65 61 64 20  s.  ** may lead 
1ca04 74 6f 20 6f 76 65 72 6c 61 70 70 69 6e 67 20 73  to overlapping s
1ca05 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63  tatement transac
1ca06 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  tions..  **.  **
1ca07 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74   The statement t
1ca08 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6e 65  ransaction is ne
1ca09 76 65 72 20 61 20 74 6f 70 2d 6c 65 76 65 6c 20  ver a top-level 
1ca0a 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 48 65  transaction.  He
1ca0b 6e 63 65 0a 20 20 2a 2a 20 74 68 65 20 52 45 4c  nce.  ** the REL
1ca0c 45 41 53 45 20 63 61 6c 6c 20 62 65 6c 6f 77 20  EASE call below 
1ca0d 63 61 6e 20 6e 65 76 65 72 20 66 61 69 6c 2e 0a  can never fail..
1ca0e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
1ca0f 2d 3e 69 53 74 61 74 65 6d 65 6e 74 3d 3d 30 20  ->iStatement==0 
1ca10 7c 7c 20 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c  || db->flags&SQL
1ca11 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 29 3b  ITE_CountRows );
1ca12 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56  .  rc = sqlite3V
1ca13 64 62 65 43 6c 6f 73 65 53 74 61 74 65 6d 65 6e  dbeCloseStatemen
1ca14 74 28 70 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52  t(p, SAVEPOINT_R
1ca15 45 4c 45 41 53 45 29 3b 0a 20 20 69 66 28 20 4e  ELEASE);.  if( N
1ca16 45 56 45 52 28 72 63 21 3d 53 51 4c 49 54 45 5f  EVER(rc!=SQLITE_
1ca17 4f 4b 29 20 29 7b 0a 20 20 20 20 62 72 65 61 6b  OK) ){.    break
1ca18 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 76 61  ;.  }..  /* Inva
1ca19 6c 69 64 61 74 65 20 61 6c 6c 20 65 70 68 65 6d  lidate all ephem
1ca1a 65 72 61 6c 20 63 75 72 73 6f 72 20 72 6f 77 20  eral cursor row 
1ca1b 63 61 63 68 65 73 20 2a 2f 0a 20 20 70 2d 3e 63  caches */.  p->c
1ca1c 61 63 68 65 43 74 72 20 3d 20 28 70 2d 3e 63 61  acheCtr = (p->ca
1ca1d 63 68 65 43 74 72 20 2b 20 32 29 7c 31 3b 0a 0a  cheCtr + 2)|1;..
1ca1e 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74    /* Make sure t
1ca1f 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68  he results of th
1ca20 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 61 72  e current row ar
1ca21 65 20 5c 30 30 30 20 74 65 72 6d 69 6e 61 74 65  e \000 terminate
1ca22 64 0a 20 20 2a 2a 20 61 6e 64 20 68 61 76 65 20  d.  ** and have 
1ca23 61 6e 20 61 73 73 69 67 6e 65 64 20 74 79 70 65  an assigned type
1ca24 2e 20 20 54 68 65 20 72 65 73 75 6c 74 73 20 61  .  The results a
1ca25 72 65 20 64 65 2d 65 70 68 65 6d 65 72 61 6c 69  re de-ephemerali
1ca26 7a 65 64 20 61 73 0a 20 20 2a 2a 20 61 73 20 73  zed as.  ** as s
1ca27 69 64 65 20 65 66 66 65 63 74 2e 0a 20 20 2a 2f  ide effect..  */
1ca28 0a 20 20 75 2e 61 64 2e 70 4d 65 6d 20 3d 20 70  .  u.ad.pMem = p
1ca29 2d 3e 70 52 65 73 75 6c 74 53 65 74 20 3d 20 26  ->pResultSet = &
1ca2a 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d  p->aMem[pOp->p1]
1ca2b 3b 0a 20 20 66 6f 72 28 75 2e 61 64 2e 69 3d 30  ;.  for(u.ad.i=0
1ca2c 3b 20 75 2e 61 64 2e 69 3c 70 4f 70 2d 3e 70 32  ; u.ad.i<pOp->p2
1ca2d 3b 20 75 2e 61 64 2e 69 2b 2b 29 7b 0a 20 20 20  ; u.ad.i++){.   
1ca2e 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e   sqlite3VdbeMemN
1ca2f 75 6c 54 65 72 6d 69 6e 61 74 65 28 26 75 2e 61  ulTerminate(&u.a
1ca30 64 2e 70 4d 65 6d 5b 75 2e 61 64 2e 69 5d 29 3b  d.pMem[u.ad.i]);
1ca31 0a 20 20 20 20 73 74 6f 72 65 54 79 70 65 49 6e  .    storeTypeIn
1ca32 66 6f 28 26 75 2e 61 64 2e 70 4d 65 6d 5b 75 2e  fo(&u.ad.pMem[u.
1ca33 61 64 2e 69 5d 2c 20 65 6e 63 6f 64 69 6e 67 29  ad.i], encoding)
1ca34 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f 54  ;.    REGISTER_T
1ca35 52 41 43 45 28 70 4f 70 2d 3e 70 31 2b 75 2e 61  RACE(pOp->p1+u.a
1ca36 64 2e 69 2c 20 26 75 2e 61 64 2e 70 4d 65 6d 5b  d.i, &u.ad.pMem[
1ca37 75 2e 61 64 2e 69 5d 29 3b 0a 20 20 7d 0a 20 20  u.ad.i]);.  }.  
1ca38 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
1ca39 69 6c 65 64 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d  iled ) goto no_m
1ca3a 65 6d 3b 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e  em;..  /* Return
1ca3b 20 53 51 4c 49 54 45 5f 52 4f 57 0a 20 20 2a 2f   SQLITE_ROW.  */
1ca3c 0a 20 20 70 2d 3e 70 63 20 3d 20 70 63 20 2b 20  .  p->pc = pc + 
1ca3d 31 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45  1;.  rc = SQLITE
1ca3e 5f 52 4f 57 3b 0a 20 20 67 6f 74 6f 20 76 64 62  _ROW;.  goto vdb
1ca3f 65 5f 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20  e_return;.}../* 
1ca40 4f 70 63 6f 64 65 3a 20 43 6f 6e 63 61 74 20 50  Opcode: Concat P
1ca41 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a  1 P2 P3 * *.**.*
1ca42 2a 20 41 64 64 20 74 68 65 20 74 65 78 74 20 69  * Add the text i
1ca43 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 6f 6e  n register P1 on
1ca44 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  to the end of th
1ca45 65 20 74 65 78 74 20 69 6e 0a 2a 2a 20 72 65 67  e text in.** reg
1ca46 69 73 74 65 72 20 50 32 20 61 6e 64 20 73 74 6f  ister P2 and sto
1ca47 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e  re the result in
1ca48 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a   register P3..**
1ca49 20 49 66 20 65 69 74 68 65 72 20 74 68 65 20 50   If either the P
1ca4a 31 20 6f 72 20 50 32 20 74 65 78 74 20 61 72 65  1 or P2 text are
1ca4b 20 4e 55 4c 4c 20 74 68 65 6e 20 73 74 6f 72 65   NULL then store
1ca4c 20 4e 55 4c 4c 20 69 6e 20 50 33 2e 0a 2a 2a 0a   NULL in P3..**.
1ca4d 2a 2a 20 20 20 50 33 20 3d 20 50 32 20 7c 7c 20  **   P3 = P2 || 
1ca4e 50 31 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69  P1.**.** It is i
1ca4f 6c 6c 65 67 61 6c 20 66 6f 72 20 50 31 20 61 6e  llegal for P1 an
1ca50 64 20 50 33 20 74 6f 20 62 65 20 74 68 65 20 73  d P3 to be the s
1ca51 61 6d 65 20 72 65 67 69 73 74 65 72 2e 20 53 6f  ame register. So
1ca52 6d 65 74 69 6d 65 73 2c 0a 2a 2a 20 69 66 20 50  metimes,.** if P
1ca53 33 20 69 73 20 74 68 65 20 73 61 6d 65 20 72 65  3 is the same re
1ca54 67 69 73 74 65 72 20 61 73 20 50 32 2c 20 74 68  gister as P2, th
1ca55 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
1ca56 20 69 73 20 61 62 6c 65 0a 2a 2a 20 74 6f 20 61   is able.** to a
1ca57 76 6f 69 64 20 61 20 6d 65 6d 63 70 79 28 29 2e  void a memcpy().
1ca58 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6e 63  .*/.case OP_Conc
1ca59 61 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  at: {           
1ca5a 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 43 4f  /* same as TK_CO
1ca5b 4e 43 41 54 2c 20 69 6e 31 2c 20 69 6e 32 2c 20  NCAT, in1, in2, 
1ca5c 6f 75 74 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f  out3 */.#if 0  /
1ca5d 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1ca5e 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
1ca5f 65 20 2a 2f 0a 20 20 69 36 34 20 6e 42 79 74 65  e */.  i64 nByte
1ca60 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
1ca61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1ca62 64 20 69 6e 74 6f 20 75 2e 61 65 20 2a 2f 0a 0a  d into u.ae */..
1ca63 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 21 3d    assert( pIn1!=
1ca64 70 4f 75 74 20 29 3b 0a 20 20 69 66 28 20 28 70  pOut );.  if( (p
1ca65 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 20 70 49 6e  In1->flags | pIn
1ca66 32 2d 3e 66 6c 61 67 73 29 20 26 20 4d 45 4d 5f  2->flags) & MEM_
1ca67 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69  Null ){.    sqli
1ca68 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c  te3VdbeMemSetNul
1ca69 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20 62 72 65  l(pOut);.    bre
1ca6a 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 45 78  ak;.  }.  if( Ex
1ca6b 70 61 6e 64 42 6c 6f 62 28 70 49 6e 31 29 20 7c  pandBlob(pIn1) |
1ca6c 7c 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e  | ExpandBlob(pIn
1ca6d 32 29 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d  2) ) goto no_mem
1ca6e 3b 0a 20 20 53 74 72 69 6e 67 69 66 79 28 70 49  ;.  Stringify(pI
1ca6f 6e 31 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20  n1, encoding);. 
1ca70 20 53 74 72 69 6e 67 69 66 79 28 70 49 6e 32 2c   Stringify(pIn2,
1ca71 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 75 2e   encoding);.  u.
1ca72 61 65 2e 6e 42 79 74 65 20 3d 20 70 49 6e 31 2d  ae.nByte = pIn1-
1ca73 3e 6e 20 2b 20 70 49 6e 32 2d 3e 6e 3b 0a 20 20  >n + pIn2->n;.  
1ca74 69 66 28 20 75 2e 61 65 2e 6e 42 79 74 65 3e 64  if( u.ae.nByte>d
1ca75 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
1ca76 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29  _LIMIT_LENGTH] )
1ca77 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62  {.    goto too_b
1ca78 69 67 3b 0a 20 20 7d 0a 20 20 4d 65 6d 53 65 74  ig;.  }.  MemSet
1ca79 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d  TypeFlag(pOut, M
1ca7a 45 4d 5f 53 74 72 29 3b 0a 20 20 69 66 28 20 73  EM_Str);.  if( s
1ca7b 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f  qlite3VdbeMemGro
1ca7c 77 28 70 4f 75 74 2c 20 28 69 6e 74 29 75 2e 61  w(pOut, (int)u.a
1ca7d 65 2e 6e 42 79 74 65 2b 32 2c 20 70 4f 75 74 3d  e.nByte+2, pOut=
1ca7e 3d 70 49 6e 32 29 20 29 7b 0a 20 20 20 20 67 6f  =pIn2) ){.    go
1ca7f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20  to no_mem;.  }. 
1ca80 20 69 66 28 20 70 4f 75 74 21 3d 70 49 6e 32 20   if( pOut!=pIn2 
1ca81 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4f  ){.    memcpy(pO
1ca82 75 74 2d 3e 7a 2c 20 70 49 6e 32 2d 3e 7a 2c 20  ut->z, pIn2->z, 
1ca83 70 49 6e 32 2d 3e 6e 29 3b 0a 20 20 7d 0a 20 20  pIn2->n);.  }.  
1ca84 6d 65 6d 63 70 79 28 26 70 4f 75 74 2d 3e 7a 5b  memcpy(&pOut->z[
1ca85 70 49 6e 32 2d 3e 6e 5d 2c 20 70 49 6e 31 2d 3e  pIn2->n], pIn1->
1ca86 7a 2c 20 70 49 6e 31 2d 3e 6e 29 3b 0a 20 20 70  z, pIn1->n);.  p
1ca87 4f 75 74 2d 3e 7a 5b 75 2e 61 65 2e 6e 42 79 74  Out->z[u.ae.nByt
1ca88 65 5d 20 3d 20 30 3b 0a 20 20 70 4f 75 74 2d 3e  e] = 0;.  pOut->
1ca89 7a 5b 75 2e 61 65 2e 6e 42 79 74 65 2b 31 5d 20  z[u.ae.nByte+1] 
1ca8a 3d 20 30 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c 61  = 0;.  pOut->fla
1ca8b 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a  gs |= MEM_Term;.
1ca8c 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 28 69 6e 74    pOut->n = (int
1ca8d 29 75 2e 61 65 2e 6e 42 79 74 65 3b 0a 20 20 70  )u.ae.nByte;.  p
1ca8e 4f 75 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f 64  Out->enc = encod
1ca8f 69 6e 67 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41  ing;.  UPDATE_MA
1ca90 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29  X_BLOBSIZE(pOut)
1ca91 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
1ca92 20 4f 70 63 6f 64 65 3a 20 41 64 64 20 50 31 20   Opcode: Add P1 
1ca93 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  P2 P3 * *.**.** 
1ca94 41 64 64 20 74 68 65 20 76 61 6c 75 65 20 69 6e  Add the value in
1ca95 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20   register P1 to 
1ca96 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67  the value in reg
1ca97 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64 20  ister P2.** and 
1ca98 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74  store the result
1ca99 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e   in register P3.
1ca9a 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e  .** If either in
1ca9b 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65  put is NULL, the
1ca9c 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e   result is NULL.
1ca9d 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d  .*/./* Opcode: M
1ca9e 75 6c 74 69 70 6c 79 20 50 31 20 50 32 20 50 33  ultiply P1 P2 P3
1ca9f 20 2a 20 2a 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 4d 75   * *.**.**.** Mu
1caa0 6c 74 69 70 6c 79 20 74 68 65 20 76 61 6c 75 65  ltiply the value
1caa1 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
1caa2 62 79 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  by the value in 
1caa3 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61  register P2.** a
1caa4 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73  nd store the res
1caa5 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20  ult in register 
1caa6 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72  P3..** If either
1caa7 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20   input is NULL, 
1caa8 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55  the result is NU
1caa9 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65  LL..*/./* Opcode
1caaa 3a 20 53 75 62 74 72 61 63 74 20 50 31 20 50 32  : Subtract P1 P2
1caab 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 75   P3 * *.**.** Su
1caac 62 74 72 61 63 74 20 74 68 65 20 76 61 6c 75 65  btract the value
1caad 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
1caae 66 72 6f 6d 20 74 68 65 20 76 61 6c 75 65 20 69  from the value i
1caaf 6e 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a  n register P2.**
1cab0 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72   and store the r
1cab1 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65  esult in registe
1cab2 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68  r P3..** If eith
1cab3 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c  er input is NULL
1cab4 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  , the result is 
1cab5 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f  NULL..*/./* Opco
1cab6 64 65 3a 20 44 69 76 69 64 65 20 50 31 20 50 32  de: Divide P1 P2
1cab7 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 69   P3 * *.**.** Di
1cab8 76 69 64 65 20 74 68 65 20 76 61 6c 75 65 20 69  vide the value i
1cab9 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 62 79  n register P1 by
1caba 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
1cabb 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64  gister P2.** and
1cabc 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c   store the resul
1cabd 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33  t in register P3
1cabe 20 28 50 33 3d 50 32 2f 50 31 29 2e 20 49 66 20   (P3=P2/P1). If 
1cabf 74 68 65 20 76 61 6c 75 65 20 69 6e 20 0a 2a 2a  the value in .**
1cac0 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20   register P1 is 
1cac1 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 72  zero, then the r
1cac2 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 20 49  esult is NULL. I
1cac3 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69  f either input i
1cac4 73 20 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 20  s .** NULL, the 
1cac5 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a  result is NULL..
1cac6 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65  */./* Opcode: Re
1cac7 6d 61 69 6e 64 65 72 20 50 31 20 50 32 20 50 33  mainder P1 P2 P3
1cac8 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 75   * *.**.** Compu
1cac9 74 65 20 74 68 65 20 72 65 6d 61 69 6e 64 65 72  te the remainder
1caca 20 61 66 74 65 72 20 69 6e 74 65 67 65 72 20 64   after integer d
1cacb 69 76 69 73 69 6f 6e 20 6f 66 20 74 68 65 20 76  ivision of the v
1cacc 61 6c 75 65 20 69 6e 0a 2a 2a 20 72 65 67 69 73  alue in.** regis
1cacd 74 65 72 20 50 31 20 62 79 20 74 68 65 20 76 61  ter P1 by the va
1cace 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
1cacf 50 32 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65  P2 and store the
1cad0 20 72 65 73 75 6c 74 20 69 6e 20 50 33 2e 20 0a   result in P3. .
1cad1 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20  ** If the value 
1cad2 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 20 69  in register P2 i
1cad3 73 20 7a 65 72 6f 20 74 68 65 20 72 65 73 75 6c  s zero the resul
1cad4 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 20 49 66  t is NULL..** If
1cad5 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20   either operand 
1cad6 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73  is NULL, the res
1cad7 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a  ult is NULL..*/.
1cad8 63 61 73 65 20 4f 50 5f 41 64 64 3a 20 20 20 20  case OP_Add:    
1cad9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1cada 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 50 4c 55  * same as TK_PLU
1cadb 53 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74  S, in1, in2, out
1cadc 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 75 62  3 */.case OP_Sub
1cadd 74 72 61 63 74 3a 20 20 20 20 20 20 20 20 20 20  tract:          
1cade 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
1cadf 4b 5f 4d 49 4e 55 53 2c 20 69 6e 31 2c 20 69 6e  K_MINUS, in1, in
1cae0 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20  2, out3 */.case 
1cae1 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 20 20  OP_Multiply:    
1cae2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d            /* sam
1cae3 65 20 61 73 20 54 4b 5f 53 54 41 52 2c 20 69 6e  e as TK_STAR, in
1cae4 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a  1, in2, out3 */.
1cae5 63 61 73 65 20 4f 50 5f 44 69 76 69 64 65 3a 20  case OP_Divide: 
1cae6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1cae7 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 4c 41  * same as TK_SLA
1cae8 53 48 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75  SH, in1, in2, ou
1cae9 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65  t3 */.case OP_Re
1caea 6d 61 69 6e 64 65 72 3a 20 7b 20 20 20 20 20 20  mainder: {      
1caeb 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1caec 54 4b 5f 52 45 4d 2c 20 69 6e 31 2c 20 69 6e 32  TK_REM, in1, in2
1caed 2c 20 6f 75 74 33 20 2a 2f 0a 23 69 66 20 30 20  , out3 */.#if 0 
1caee 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1caef 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1caf0 2e 61 66 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61  .af */.  int fla
1caf1 67 73 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6d 62  gs;      /* Comb
1caf2 69 6e 65 64 20 4d 45 4d 5f 2a 20 66 6c 61 67 73  ined MEM_* flags
1caf3 20 66 72 6f 6d 20 62 6f 74 68 20 69 6e 70 75 74   from both input
1caf4 73 20 2a 2f 0a 20 20 69 36 34 20 69 41 3b 20 20  s */.  i64 iA;  
1caf5 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65         /* Intege
1caf6 72 20 76 61 6c 75 65 20 6f 66 20 6c 65 66 74 20  r value of left 
1caf7 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 36 34  operand */.  i64
1caf8 20 69 42 3b 20 20 20 20 20 20 20 20 20 2f 2a 20   iB;         /* 
1caf9 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 6f 66  Integer value of
1cafa 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a   right operand *
1cafb 2f 0a 20 20 64 6f 75 62 6c 65 20 72 41 3b 20 20  /.  double rA;  
1cafc 20 20 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c 75      /* Real valu
1cafd 65 20 6f 66 20 6c 65 66 74 20 6f 70 65 72 61 6e  e of left operan
1cafe 64 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 42  d */.  double rB
1caff 3b 20 20 20 20 20 20 2f 2a 20 52 65 61 6c 20 76  ;      /* Real v
1cb00 61 6c 75 65 20 6f 66 20 72 69 67 68 74 20 6f 70  alue of right op
1cb01 65 72 61 6e 64 20 2a 2f 0a 23 65 6e 64 69 66 20  erand */.#endif 
1cb02 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1cb03 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1cb04 61 66 20 2a 2f 0a 0a 20 20 61 70 70 6c 79 4e 75  af */..  applyNu
1cb05 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 70 49  mericAffinity(pI
1cb06 6e 31 29 3b 0a 20 20 61 70 70 6c 79 4e 75 6d 65  n1);.  applyNume
1cb07 72 69 63 41 66 66 69 6e 69 74 79 28 70 49 6e 32  ricAffinity(pIn2
1cb08 29 3b 0a 20 20 75 2e 61 66 2e 66 6c 61 67 73 20  );.  u.af.flags 
1cb09 3d 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 20  = pIn1->flags | 
1cb0a 70 49 6e 32 2d 3e 66 6c 61 67 73 3b 0a 20 20 69  pIn2->flags;.  i
1cb0b 66 28 20 28 75 2e 61 66 2e 66 6c 61 67 73 20 26  f( (u.af.flags &
1cb0c 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20 29 20   MEM_Null)!=0 ) 
1cb0d 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f  goto arithmetic_
1cb0e 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a  result_is_null;.
1cb0f 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61    if( (pIn1->fla
1cb10 67 73 20 26 20 70 49 6e 32 2d 3e 66 6c 61 67 73  gs & pIn2->flags
1cb11 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d 4d 45 4d   & MEM_Int)==MEM
1cb12 5f 49 6e 74 20 29 7b 0a 20 20 20 20 75 2e 61 66  _Int ){.    u.af
1cb13 2e 69 41 20 3d 20 70 49 6e 31 2d 3e 75 2e 69 3b  .iA = pIn1->u.i;
1cb14 0a 20 20 20 20 75 2e 61 66 2e 69 42 20 3d 20 70  .    u.af.iB = p
1cb15 49 6e 32 2d 3e 75 2e 69 3b 0a 20 20 20 20 73 77  In2->u.i;.    sw
1cb16 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64  itch( pOp->opcod
1cb17 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20  e ){.      case 
1cb18 4f 50 5f 41 64 64 3a 20 20 20 20 20 20 20 20 20  OP_Add:         
1cb19 75 2e 61 66 2e 69 42 20 2b 3d 20 75 2e 61 66 2e  u.af.iB += u.af.
1cb1a 69 41 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b  iA;       break;
1cb1b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 53  .      case OP_S
1cb1c 75 62 74 72 61 63 74 3a 20 20 20 20 75 2e 61 66  ubtract:    u.af
1cb1d 2e 69 42 20 2d 3d 20 75 2e 61 66 2e 69 41 3b 20  .iB -= u.af.iA; 
1cb1e 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1cb1f 20 20 20 63 61 73 65 20 4f 50 5f 4d 75 6c 74 69     case OP_Multi
1cb20 70 6c 79 3a 20 20 20 20 75 2e 61 66 2e 69 42 20  ply:    u.af.iB 
1cb21 2a 3d 20 75 2e 61 66 2e 69 41 3b 20 20 20 20 20  *= u.af.iA;     
1cb22 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
1cb23 61 73 65 20 4f 50 5f 44 69 76 69 64 65 3a 20 7b  ase OP_Divide: {
1cb24 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 61  .        if( u.a
1cb25 66 2e 69 41 3d 3d 30 20 29 20 67 6f 74 6f 20 61  f.iA==0 ) goto a
1cb26 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74  rithmetic_result
1cb27 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20  _is_null;.      
1cb28 20 20 2f 2a 20 44 69 76 69 64 69 6e 67 20 74 68    /* Dividing th
1cb29 65 20 6c 61 72 67 65 73 74 20 70 6f 73 73 69 62  e largest possib
1cb2a 6c 65 20 6e 65 67 61 74 69 76 65 20 36 34 2d 62  le negative 64-b
1cb2b 69 74 20 69 6e 74 65 67 65 72 20 28 31 3c 3c 36  it integer (1<<6
1cb2c 33 29 20 62 79 0a 20 20 20 20 20 20 20 20 2a 2a  3) by.        **
1cb2d 20 2d 31 20 72 65 74 75 72 6e 73 20 61 6e 20 69   -1 returns an i
1cb2e 6e 74 65 67 65 72 20 74 6f 6f 20 6c 61 72 67 65  nteger too large
1cb2f 20 74 6f 20 73 74 6f 72 65 20 69 6e 20 61 20 36   to store in a 6
1cb30 34 2d 62 69 74 20 64 61 74 61 2d 74 79 70 65 2e  4-bit data-type.
1cb31 20 4f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 73   On.        ** s
1cb32 6f 6d 65 20 61 72 63 68 69 74 65 63 74 75 72 65  ome architecture
1cb33 73 2c 20 74 68 65 20 76 61 6c 75 65 20 6f 76 65  s, the value ove
1cb34 72 66 6c 6f 77 73 20 74 6f 20 28 31 3c 3c 36 33  rflows to (1<<63
1cb35 29 2e 20 4f 6e 20 6f 74 68 65 72 73 2c 0a 20 20  ). On others,.  
1cb36 20 20 20 20 20 20 2a 2a 20 61 20 53 49 47 46 50        ** a SIGFP
1cb37 45 20 69 73 20 69 73 73 75 65 64 2e 20 54 68 65  E is issued. The
1cb38 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65   following state
1cb39 6d 65 6e 74 20 6e 6f 72 6d 61 6c 69 7a 65 73 20  ment normalizes 
1cb3a 74 68 69 73 0a 20 20 20 20 20 20 20 20 2a 2a 20  this.        ** 
1cb3b 62 65 68 61 76 69 6f 72 20 73 6f 20 74 68 61 74  behavior so that
1cb3c 20 61 6c 6c 20 61 72 63 68 69 74 65 63 74 75 72   all architectur
1cb3d 65 73 20 62 65 68 61 76 65 20 61 73 20 69 66 20  es behave as if 
1cb3e 69 6e 74 65 67 65 72 0a 20 20 20 20 20 20 20 20  integer.        
1cb3f 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 6f 63 63 75  ** overflow occu
1cb40 72 72 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f  rred..        */
1cb41 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 61  .        if( u.a
1cb42 66 2e 69 41 3d 3d 2d 31 20 26 26 20 75 2e 61 66  f.iA==-1 && u.af
1cb43 2e 69 42 3d 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e  .iB==SMALLEST_IN
1cb44 54 36 34 20 29 20 75 2e 61 66 2e 69 41 20 3d 20  T64 ) u.af.iA = 
1cb45 31 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 66 2e  1;.        u.af.
1cb46 69 42 20 2f 3d 20 75 2e 61 66 2e 69 41 3b 0a 20  iB /= u.af.iA;. 
1cb47 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1cb48 20 20 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61      }.      defa
1cb49 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 69  ult: {.        i
1cb4a 66 28 20 75 2e 61 66 2e 69 41 3d 3d 30 20 29 20  f( u.af.iA==0 ) 
1cb4b 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f  goto arithmetic_
1cb4c 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a  result_is_null;.
1cb4d 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 66          if( u.af
1cb4e 2e 69 41 3d 3d 2d 31 20 29 20 75 2e 61 66 2e 69  .iA==-1 ) u.af.i
1cb4f 41 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 75  A = 1;.        u
1cb50 2e 61 66 2e 69 42 20 25 3d 20 75 2e 61 66 2e 69  .af.iB %= u.af.i
1cb51 41 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  A;.        break
1cb52 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1cb53 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20      pOut->u.i = 
1cb54 75 2e 61 66 2e 69 42 3b 0a 20 20 20 20 4d 65 6d  u.af.iB;.    Mem
1cb55 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74  SetTypeFlag(pOut
1cb56 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 65  , MEM_Int);.  }e
1cb57 6c 73 65 7b 0a 20 20 20 20 75 2e 61 66 2e 72 41  lse{.    u.af.rA
1cb58 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65   = sqlite3VdbeRe
1cb59 61 6c 56 61 6c 75 65 28 70 49 6e 31 29 3b 0a 20  alValue(pIn1);. 
1cb5a 20 20 20 75 2e 61 66 2e 72 42 20 3d 20 73 71 6c     u.af.rB = sql
1cb5b 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75  ite3VdbeRealValu
1cb5c 65 28 70 49 6e 32 29 3b 0a 20 20 20 20 73 77 69  e(pIn2);.    swi
1cb5d 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65  tch( pOp->opcode
1cb5e 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 4f   ){.      case O
1cb5f 50 5f 41 64 64 3a 20 20 20 20 20 20 20 20 20 75  P_Add:         u
1cb60 2e 61 66 2e 72 42 20 2b 3d 20 75 2e 61 66 2e 72  .af.rB += u.af.r
1cb61 41 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  A;       break;.
1cb62 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 53 75        case OP_Su
1cb63 62 74 72 61 63 74 3a 20 20 20 20 75 2e 61 66 2e  btract:    u.af.
1cb64 72 42 20 2d 3d 20 75 2e 61 66 2e 72 41 3b 20 20  rB -= u.af.rA;  
1cb65 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1cb66 20 20 63 61 73 65 20 4f 50 5f 4d 75 6c 74 69 70    case OP_Multip
1cb67 6c 79 3a 20 20 20 20 75 2e 61 66 2e 72 42 20 2a  ly:    u.af.rB *
1cb68 3d 20 75 2e 61 66 2e 72 41 3b 20 20 20 20 20 20  = u.af.rA;      
1cb69 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
1cb6a 73 65 20 4f 50 5f 44 69 76 69 64 65 3a 20 7b 0a  se OP_Divide: {.
1cb6b 20 20 20 20 20 20 20 20 2f 2a 20 28 64 6f 75 62          /* (doub
1cb6c 6c 65 29 30 20 49 6e 20 63 61 73 65 20 6f 66 20  le)0 In case of 
1cb6d 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41  SQLITE_OMIT_FLOA
1cb6e 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f  TING_POINT... */
1cb6f 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 61  .        if( u.a
1cb70 66 2e 72 41 3d 3d 28 64 6f 75 62 6c 65 29 30 20  f.rA==(double)0 
1cb71 29 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69  ) goto arithmeti
1cb72 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c  c_result_is_null
1cb73 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 66 2e 72  ;.        u.af.r
1cb74 42 20 2f 3d 20 75 2e 61 66 2e 72 41 3b 0a 20 20  B /= u.af.rA;.  
1cb75 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1cb76 20 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61 75     }.      defau
1cb77 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 75 2e  lt: {.        u.
1cb78 61 66 2e 69 41 20 3d 20 28 69 36 34 29 75 2e 61  af.iA = (i64)u.a
1cb79 66 2e 72 41 3b 0a 20 20 20 20 20 20 20 20 75 2e  f.rA;.        u.
1cb7a 61 66 2e 69 42 20 3d 20 28 69 36 34 29 75 2e 61  af.iB = (i64)u.a
1cb7b 66 2e 72 42 3b 0a 20 20 20 20 20 20 20 20 69 66  f.rB;.        if
1cb7c 28 20 75 2e 61 66 2e 69 41 3d 3d 30 20 29 20 67  ( u.af.iA==0 ) g
1cb7d 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72  oto arithmetic_r
1cb7e 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20  esult_is_null;. 
1cb7f 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e         if( u.af.
1cb80 69 41 3d 3d 2d 31 20 29 20 75 2e 61 66 2e 69 41  iA==-1 ) u.af.iA
1cb81 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 75 2e   = 1;.        u.
1cb82 61 66 2e 72 42 20 3d 20 28 64 6f 75 62 6c 65 29  af.rB = (double)
1cb83 28 75 2e 61 66 2e 69 42 20 25 20 75 2e 61 66 2e  (u.af.iB % u.af.
1cb84 69 41 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65  iA);.        bre
1cb85 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
1cb86 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  }.    if( sqlite
1cb87 33 49 73 4e 61 4e 28 75 2e 61 66 2e 72 42 29 20  3IsNaN(u.af.rB) 
1cb88 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 72  ){.      goto ar
1cb89 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f  ithmetic_result_
1cb8a 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 7d 0a 20  is_null;.    }. 
1cb8b 20 20 20 70 4f 75 74 2d 3e 72 20 3d 20 75 2e 61     pOut->r = u.a
1cb8c 66 2e 72 42 3b 0a 20 20 20 20 4d 65 6d 53 65 74  f.rB;.    MemSet
1cb8d 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d  TypeFlag(pOut, M
1cb8e 45 4d 5f 52 65 61 6c 29 3b 0a 20 20 20 20 69 66  EM_Real);.    if
1cb8f 28 20 28 75 2e 61 66 2e 66 6c 61 67 73 20 26 20  ( (u.af.flags & 
1cb90 4d 45 4d 5f 52 65 61 6c 29 3d 3d 30 20 29 7b 0a  MEM_Real)==0 ){.
1cb91 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1cb92 65 49 6e 74 65 67 65 72 41 66 66 69 6e 69 74 79  eIntegerAffinity
1cb93 28 70 4f 75 74 29 3b 0a 20 20 20 20 7d 0a 20 20  (pOut);.    }.  
1cb94 7d 0a 20 20 62 72 65 61 6b 3b 0a 0a 61 72 69 74  }.  break;..arit
1cb95 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73  hmetic_result_is
1cb96 5f 6e 75 6c 6c 3a 0a 20 20 73 71 6c 69 74 65 33  _null:.  sqlite3
1cb97 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70  VdbeMemSetNull(p
1cb98 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  Out);.  break;.}
1cb99 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6c  ../* Opcode: Col
1cb9a 6c 53 65 71 20 2a 20 2a 20 50 34 0a 2a 2a 0a 2a  lSeq * * P4.**.*
1cb9b 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65  * P4 is a pointe
1cb9c 72 20 74 6f 20 61 20 43 6f 6c 6c 53 65 71 20 73  r to a CollSeq s
1cb9d 74 72 75 63 74 2e 20 49 66 20 74 68 65 20 6e 65  truct. If the ne
1cb9e 78 74 20 63 61 6c 6c 20 74 6f 20 61 20 75 73 65  xt call to a use
1cb9f 72 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6f 72  r function.** or
1cba0 20 61 67 67 72 65 67 61 74 65 20 63 61 6c 6c 73   aggregate calls
1cba1 20 73 71 6c 69 74 65 33 47 65 74 46 75 6e 63 43   sqlite3GetFuncC
1cba2 6f 6c 6c 53 65 71 28 29 2c 20 74 68 69 73 20 63  ollSeq(), this c
1cba3 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
1cba4 65 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 72 65 74  e will.** be ret
1cba5 75 72 6e 65 64 2e 20 54 68 69 73 20 69 73 20 75  urned. This is u
1cba6 73 65 64 20 62 79 20 74 68 65 20 62 75 69 6c 74  sed by the built
1cba7 2d 69 6e 20 6d 69 6e 28 29 2c 20 6d 61 78 28 29  -in min(), max()
1cba8 20 61 6e 64 20 6e 75 6c 6c 69 66 28 29 0a 2a 2a   and nullif().**
1cba9 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a   functions..**.*
1cbaa 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20  * The interface 
1cbab 75 73 65 64 20 62 79 20 74 68 65 20 69 6d 70 6c  used by the impl
1cbac 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
1cbad 65 20 61 66 6f 72 65 6d 65 6e 74 69 6f 6e 65 64  e aforementioned
1cbae 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f   functions.** to
1cbaf 20 72 65 74 72 69 65 76 65 20 74 68 65 20 63 6f   retrieve the co
1cbb0 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
1cbb1 20 73 65 74 20 62 79 20 74 68 69 73 20 6f 70 63   set by this opc
1cbb2 6f 64 65 20 69 73 20 6e 6f 74 20 61 76 61 69 6c  ode is not avail
1cbb3 61 62 6c 65 0a 2a 2a 20 70 75 62 6c 69 63 6c 79  able.** publicly
1cbb4 2c 20 6f 6e 6c 79 20 74 6f 20 75 73 65 72 20 66  , only to user f
1cbb5 75 6e 63 74 69 6f 6e 73 20 64 65 66 69 6e 65 64  unctions defined
1cbb6 20 69 6e 20 66 75 6e 63 2e 63 2e 0a 2a 2f 0a 63   in func.c..*/.c
1cbb7 61 73 65 20 4f 50 5f 43 6f 6c 6c 53 65 71 3a 20  ase OP_CollSeq: 
1cbb8 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  {.  assert( pOp-
1cbb9 3e 70 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c  >p4type==P4_COLL
1cbba 53 45 51 20 29 3b 0a 20 20 62 72 65 61 6b 3b 0a  SEQ );.  break;.
1cbbb 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 75  }../* Opcode: Fu
1cbbc 6e 63 74 69 6f 6e 20 50 31 20 50 32 20 50 33 20  nction P1 P2 P3 
1cbbd 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 49 6e 76 6f  P4 P5.**.** Invo
1cbbe 6b 65 20 61 20 75 73 65 72 20 66 75 6e 63 74 69  ke a user functi
1cbbf 6f 6e 20 28 50 34 20 69 73 20 61 20 70 6f 69 6e  on (P4 is a poin
1cbc0 74 65 72 20 74 6f 20 61 20 46 75 6e 63 74 69 6f  ter to a Functio
1cbc1 6e 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  n structure that
1cbc2 0a 2a 2a 20 64 65 66 69 6e 65 73 20 74 68 65 20  .** defines the 
1cbc3 66 75 6e 63 74 69 6f 6e 29 20 77 69 74 68 20 50  function) with P
1cbc4 35 20 61 72 67 75 6d 65 6e 74 73 20 74 61 6b 65  5 arguments take
1cbc5 6e 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72 20  n from register 
1cbc6 50 32 20 61 6e 64 0a 2a 2a 20 73 75 63 63 65 73  P2 and.** succes
1cbc7 73 6f 72 73 2e 20 20 54 68 65 20 72 65 73 75 6c  sors.  The resul
1cbc8 74 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f  t of the functio
1cbc9 6e 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72  n is stored in r
1cbca 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 52  egister P3..** R
1cbcb 65 67 69 73 74 65 72 20 50 33 20 6d 75 73 74 20  egister P3 must 
1cbcc 6e 6f 74 20 62 65 20 6f 6e 65 20 6f 66 20 74 68  not be one of th
1cbcd 65 20 66 75 6e 63 74 69 6f 6e 20 69 6e 70 75 74  e function input
1cbce 73 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61  s..**.** P1 is a
1cbcf 20 33 32 2d 62 69 74 20 62 69 74 6d 61 73 6b 20   32-bit bitmask 
1cbd0 69 6e 64 69 63 61 74 69 6e 67 20 77 68 65 74 68  indicating wheth
1cbd1 65 72 20 6f 72 20 6e 6f 74 20 65 61 63 68 20 61  er or not each a
1cbd2 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 0a  rgument to the .
1cbd3 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20  ** function was 
1cbd4 64 65 74 65 72 6d 69 6e 65 64 20 74 6f 20 62 65  determined to be
1cbd5 20 63 6f 6e 73 74 61 6e 74 20 61 74 20 63 6f 6d   constant at com
1cbd6 70 69 6c 65 20 74 69 6d 65 2e 20 49 66 20 74 68  pile time. If th
1cbd7 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d  e first.** argum
1cbd8 65 6e 74 20 77 61 73 20 63 6f 6e 73 74 61 6e 74  ent was constant
1cbd9 20 74 68 65 6e 20 62 69 74 20 30 20 6f 66 20 50   then bit 0 of P
1cbda 31 20 69 73 20 73 65 74 2e 20 54 68 69 73 20 69  1 is set. This i
1cbdb 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d  s used to determ
1cbdc 69 6e 65 0a 2a 2a 20 77 68 65 74 68 65 72 20 6d  ine.** whether m
1cbdd 65 74 61 20 64 61 74 61 20 61 73 73 6f 63 69 61  eta data associa
1cbde 74 65 64 20 77 69 74 68 20 61 20 75 73 65 72 20  ted with a user 
1cbdf 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e  function argumen
1cbe0 74 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 73  t using the.** s
1cbe1 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61  qlite3_set_auxda
1cbe2 74 61 28 29 20 41 50 49 20 6d 61 79 20 62 65 20  ta() API may be 
1cbe3 73 61 66 65 6c 79 20 72 65 74 61 69 6e 65 64 20  safely retained 
1cbe4 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 0a 2a  until the next.*
1cbe5 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20  * invocation of 
1cbe6 74 68 69 73 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a  this opcode..**.
1cbe7 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 41 67 67  ** See also: Agg
1cbe8 53 74 65 70 20 61 6e 64 20 41 67 67 46 69 6e 61  Step and AggFina
1cbe9 6c 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 46 75 6e  l.*/.case OP_Fun
1cbea 63 74 69 6f 6e 3a 20 7b 0a 23 69 66 20 30 20 20  ction: {.#if 0  
1cbeb 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1cbec 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1cbed 61 67 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 0a 20  ag */.  int i;. 
1cbee 20 4d 65 6d 20 2a 70 41 72 67 3b 0a 20 20 73 71   Mem *pArg;.  sq
1cbef 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 63 74  lite3_context ct
1cbf0 78 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  x;.  sqlite3_val
1cbf1 75 65 20 2a 2a 61 70 56 61 6c 3b 0a 20 20 69 6e  ue **apVal;.  in
1cbf2 74 20 6e 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  t n;.#endif /* l
1cbf3 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1cbf4 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 67 20 2a  oved into u.ag *
1cbf5 2f 0a 0a 20 20 75 2e 61 67 2e 6e 20 3d 20 70 4f  /..  u.ag.n = pO
1cbf6 70 2d 3e 70 35 3b 0a 20 20 75 2e 61 67 2e 61 70  p->p5;.  u.ag.ap
1cbf7 56 61 6c 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0a  Val = p->apArg;.
1cbf8 20 20 61 73 73 65 72 74 28 20 75 2e 61 67 2e 61    assert( u.ag.a
1cbf9 70 56 61 6c 20 7c 7c 20 75 2e 61 67 2e 6e 3d 3d  pVal || u.ag.n==
1cbfa 30 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  0 );..  assert( 
1cbfb 75 2e 61 67 2e 6e 3d 3d 30 20 7c 7c 20 28 70 4f  u.ag.n==0 || (pO
1cbfc 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e  p->p2>0 && pOp->
1cbfd 70 32 2b 75 2e 61 67 2e 6e 3c 3d 70 2d 3e 6e 4d  p2+u.ag.n<=p->nM
1cbfe 65 6d 2b 31 29 20 29 3b 0a 20 20 61 73 73 65 72  em+1) );.  asser
1cbff 74 28 20 70 4f 70 2d 3e 70 33 3c 70 4f 70 2d 3e  t( pOp->p3<pOp->
1cc00 70 32 20 7c 7c 20 70 4f 70 2d 3e 70 33 3e 3d 70  p2 || pOp->p3>=p
1cc01 4f 70 2d 3e 70 32 2b 75 2e 61 67 2e 6e 20 29 3b  Op->p2+u.ag.n );
1cc02 0a 20 20 75 2e 61 67 2e 70 41 72 67 20 3d 20 26  .  u.ag.pArg = &
1cc03 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d  p->aMem[pOp->p2]
1cc04 3b 0a 20 20 66 6f 72 28 75 2e 61 67 2e 69 3d 30  ;.  for(u.ag.i=0
1cc05 3b 20 75 2e 61 67 2e 69 3c 75 2e 61 67 2e 6e 3b  ; u.ag.i<u.ag.n;
1cc06 20 75 2e 61 67 2e 69 2b 2b 2c 20 75 2e 61 67 2e   u.ag.i++, u.ag.
1cc07 70 41 72 67 2b 2b 29 7b 0a 20 20 20 20 75 2e 61  pArg++){.    u.a
1cc08 67 2e 61 70 56 61 6c 5b 75 2e 61 67 2e 69 5d 20  g.apVal[u.ag.i] 
1cc09 3d 20 75 2e 61 67 2e 70 41 72 67 3b 0a 20 20 20  = u.ag.pArg;.   
1cc0a 20 73 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 75   storeTypeInfo(u
1cc0b 2e 61 67 2e 70 41 72 67 2c 20 65 6e 63 6f 64 69  .ag.pArg, encodi
1cc0c 6e 67 29 3b 0a 20 20 20 20 52 45 47 49 53 54 45  ng);.    REGISTE
1cc0d 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c  R_TRACE(pOp->p2,
1cc0e 20 75 2e 61 67 2e 70 41 72 67 29 3b 0a 20 20 7d   u.ag.pArg);.  }
1cc0f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ..  assert( pOp-
1cc10 3e 70 34 74 79 70 65 3d 3d 50 34 5f 46 55 4e 43  >p4type==P4_FUNC
1cc11 44 45 46 20 7c 7c 20 70 4f 70 2d 3e 70 34 74 79  DEF || pOp->p4ty
1cc12 70 65 3d 3d 50 34 5f 56 44 42 45 46 55 4e 43 20  pe==P4_VDBEFUNC 
1cc13 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 34  );.  if( pOp->p4
1cc14 74 79 70 65 3d 3d 50 34 5f 46 55 4e 43 44 45 46  type==P4_FUNCDEF
1cc15 20 29 7b 0a 20 20 20 20 75 2e 61 67 2e 63 74 78   ){.    u.ag.ctx
1cc16 2e 70 46 75 6e 63 20 3d 20 70 4f 70 2d 3e 70 34  .pFunc = pOp->p4
1cc17 2e 70 46 75 6e 63 3b 0a 20 20 20 20 75 2e 61 67  .pFunc;.    u.ag
1cc18 2e 63 74 78 2e 70 56 64 62 65 46 75 6e 63 20 3d  .ctx.pVdbeFunc =
1cc19 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
1cc1a 20 75 2e 61 67 2e 63 74 78 2e 70 56 64 62 65 46   u.ag.ctx.pVdbeF
1cc1b 75 6e 63 20 3d 20 28 56 64 62 65 46 75 6e 63 2a  unc = (VdbeFunc*
1cc1c 29 70 4f 70 2d 3e 70 34 2e 70 56 64 62 65 46 75  )pOp->p4.pVdbeFu
1cc1d 6e 63 3b 0a 20 20 20 20 75 2e 61 67 2e 63 74 78  nc;.    u.ag.ctx
1cc1e 2e 70 46 75 6e 63 20 3d 20 75 2e 61 67 2e 63 74  .pFunc = u.ag.ct
1cc1f 78 2e 70 56 64 62 65 46 75 6e 63 2d 3e 70 46 75  x.pVdbeFunc->pFu
1cc20 6e 63 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72  nc;.  }..  asser
1cc21 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20  t( pOp->p3>0 && 
1cc22 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d  pOp->p3<=p->nMem
1cc23 20 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d   );.  pOut = &p-
1cc24 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a  >aMem[pOp->p3];.
1cc25 20 20 75 2e 61 67 2e 63 74 78 2e 73 2e 66 6c 61    u.ag.ctx.s.fla
1cc26 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20  gs = MEM_Null;. 
1cc27 20 75 2e 61 67 2e 63 74 78 2e 73 2e 64 62 20 3d   u.ag.ctx.s.db =
1cc28 20 64 62 3b 0a 20 20 75 2e 61 67 2e 63 74 78 2e   db;.  u.ag.ctx.
1cc29 73 2e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 75 2e  s.xDel = 0;.  u.
1cc2a 61 67 2e 63 74 78 2e 73 2e 7a 4d 61 6c 6c 6f 63  ag.ctx.s.zMalloc
1cc2b 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 68 65 20   = 0;..  /* The 
1cc2c 6f 75 74 70 75 74 20 63 65 6c 6c 20 6d 61 79 20  output cell may 
1cc2d 61 6c 72 65 61 64 79 20 68 61 76 65 20 61 20 62  already have a b
1cc2e 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 2e  uffer allocated.
1cc2f 20 4d 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20 70   Move.  ** the p
1cc30 6f 69 6e 74 65 72 20 74 6f 20 75 2e 61 67 2e 63  ointer to u.ag.c
1cc31 74 78 2e 73 20 73 6f 20 69 6e 20 63 61 73 65 20  tx.s so in case 
1cc32 74 68 65 20 75 73 65 72 2d 66 75 6e 63 74 69 6f  the user-functio
1cc33 6e 20 63 61 6e 20 75 73 65 0a 20 20 2a 2a 20 74  n can use.  ** t
1cc34 68 65 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63  he already alloc
1cc35 61 74 65 64 20 62 75 66 66 65 72 20 69 6e 73 74  ated buffer inst
1cc36 65 61 64 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6e  ead of allocatin
1cc37 67 20 61 20 6e 65 77 20 6f 6e 65 2e 0a 20 20 2a  g a new one..  *
1cc38 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  /.  sqlite3VdbeM
1cc39 65 6d 4d 6f 76 65 28 26 75 2e 61 67 2e 63 74 78  emMove(&u.ag.ctx
1cc3a 2e 73 2c 20 70 4f 75 74 29 3b 0a 20 20 4d 65 6d  .s, pOut);.  Mem
1cc3b 53 65 74 54 79 70 65 46 6c 61 67 28 26 75 2e 61  SetTypeFlag(&u.a
1cc3c 67 2e 63 74 78 2e 73 2c 20 4d 45 4d 5f 4e 75 6c  g.ctx.s, MEM_Nul
1cc3d 6c 29 3b 0a 0a 20 20 75 2e 61 67 2e 63 74 78 2e  l);..  u.ag.ctx.
1cc3e 69 73 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 69  isError = 0;.  i
1cc3f 66 28 20 75 2e 61 67 2e 63 74 78 2e 70 46 75 6e  f( u.ag.ctx.pFun
1cc40 63 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54  c->flags & SQLIT
1cc41 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20  E_FUNC_NEEDCOLL 
1cc42 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
1cc43 4f 70 3e 70 2d 3e 61 4f 70 20 29 3b 0a 20 20 20  Op>p->aOp );.   
1cc44 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d   assert( pOp[-1]
1cc45 2e 70 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c  .p4type==P4_COLL
1cc46 53 45 51 20 29 3b 0a 20 20 20 20 61 73 73 65 72  SEQ );.    asser
1cc47 74 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64  t( pOp[-1].opcod
1cc48 65 3d 3d 4f 50 5f 43 6f 6c 6c 53 65 71 20 29 3b  e==OP_CollSeq );
1cc49 0a 20 20 20 20 75 2e 61 67 2e 63 74 78 2e 70 43  .    u.ag.ctx.pC
1cc4a 6f 6c 6c 20 3d 20 70 4f 70 5b 2d 31 5d 2e 70 34  oll = pOp[-1].p4
1cc4b 2e 70 43 6f 6c 6c 3b 0a 20 20 7d 0a 20 20 69 66  .pColl;.  }.  if
1cc4c 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  ( sqlite3SafetyO
1cc4d 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62  ff(db) ) goto ab
1cc4e 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73  ort_due_to_misus
1cc4f 65 3b 0a 20 20 28 2a 75 2e 61 67 2e 63 74 78 2e  e;.  (*u.ag.ctx.
1cc50 70 46 75 6e 63 2d 3e 78 46 75 6e 63 29 28 26 75  pFunc->xFunc)(&u
1cc51 2e 61 67 2e 63 74 78 2c 20 75 2e 61 67 2e 6e 2c  .ag.ctx, u.ag.n,
1cc52 20 75 2e 61 67 2e 61 70 56 61 6c 29 3b 0a 20 20   u.ag.apVal);.  
1cc53 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74  if( sqlite3Safet
1cc54 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 73  yOn(db) ){.    s
1cc55 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
1cc56 65 61 73 65 28 26 75 2e 61 67 2e 63 74 78 2e 73  ease(&u.ag.ctx.s
1cc57 29 3b 0a 20 20 20 20 67 6f 74 6f 20 61 62 6f 72  );.    goto abor
1cc58 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b  t_due_to_misuse;
1cc59 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d  .  }.  if( db->m
1cc5a 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
1cc5b 20 20 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67     /* Even thoug
1cc5c 68 20 61 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73  h a malloc() has
1cc5d 20 66 61 69 6c 65 64 2c 20 74 68 65 20 69 6d 70   failed, the imp
1cc5e 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
1cc5f 68 65 0a 20 20 20 20 2a 2a 20 75 73 65 72 20 66  he.    ** user f
1cc60 75 6e 63 74 69 6f 6e 20 6d 61 79 20 68 61 76 65  unction may have
1cc61 20 63 61 6c 6c 65 64 20 61 6e 20 73 71 6c 69 74   called an sqlit
1cc62 65 33 5f 72 65 73 75 6c 74 5f 58 58 58 28 29 20  e3_result_XXX() 
1cc63 66 75 6e 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20  function.    ** 
1cc64 74 6f 20 72 65 74 75 72 6e 20 61 20 76 61 6c 75  to return a valu
1cc65 65 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  e. The following
1cc66 20 63 61 6c 6c 20 72 65 6c 65 61 73 65 73 20 61   call releases a
1cc67 6e 79 20 72 65 73 6f 75 72 63 65 73 0a 20 20 20  ny resources.   
1cc68 20 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77   ** associated w
1cc69 69 74 68 20 73 75 63 68 20 61 20 76 61 6c 75 65  ith such a value
1cc6a 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
1cc6b 4e 6f 74 65 3a 20 4d 61 79 62 65 20 4d 65 6d 52  Note: Maybe MemR
1cc6c 65 6c 65 61 73 65 28 29 20 73 68 6f 75 6c 64 20  elease() should 
1cc6d 62 65 20 63 61 6c 6c 65 64 20 69 66 20 73 71 6c  be called if sql
1cc6e 69 74 65 33 53 61 66 65 74 79 4f 6e 28 29 0a 20  ite3SafetyOn(). 
1cc6f 20 20 20 2a 2a 20 66 61 69 6c 73 20 61 6c 73 6f     ** fails also
1cc70 20 28 74 68 65 20 69 66 28 2e 2e 2e 29 20 73 74   (the if(...) st
1cc71 61 74 65 6d 65 6e 74 20 61 62 6f 76 65 29 2e 20  atement above). 
1cc72 42 75 74 20 69 66 20 70 65 6f 70 6c 65 20 61 72  But if people ar
1cc73 65 0a 20 20 20 20 2a 2a 20 6d 69 73 75 73 69 6e  e.    ** misusin
1cc74 67 20 73 71 6c 69 74 65 2c 20 74 68 65 79 20 68  g sqlite, they h
1cc75 61 76 65 20 62 69 67 67 65 72 20 70 72 6f 62 6c  ave bigger probl
1cc76 65 6d 73 20 74 68 61 6e 20 61 20 6c 65 61 6b 65  ems than a leake
1cc77 64 20 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f 0a  d value..    */.
1cc78 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1cc79 65 6d 52 65 6c 65 61 73 65 28 26 75 2e 61 67 2e  emRelease(&u.ag.
1cc7a 63 74 78 2e 73 29 3b 0a 20 20 20 20 67 6f 74 6f  ctx.s);.    goto
1cc7b 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 0a 20 20   no_mem;.  }..  
1cc7c 2f 2a 20 49 66 20 61 6e 79 20 61 75 78 69 6c 69  /* If any auxili
1cc7d 61 72 79 20 64 61 74 61 20 66 75 6e 63 74 69 6f  ary data functio
1cc7e 6e 73 20 68 61 76 65 20 62 65 65 6e 20 63 61 6c  ns have been cal
1cc7f 6c 65 64 20 62 79 20 74 68 69 73 20 75 73 65 72  led by this user
1cc80 20 66 75 6e 63 74 69 6f 6e 2c 0a 20 20 2a 2a 20   function,.  ** 
1cc81 69 6d 6d 65 64 69 61 74 65 6c 79 20 63 61 6c 6c  immediately call
1cc82 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20   the destructor 
1cc83 66 6f 72 20 61 6e 79 20 6e 6f 6e 2d 73 74 61 74  for any non-stat
1cc84 69 63 20 76 61 6c 75 65 73 2e 0a 20 20 2a 2f 0a  ic values..  */.
1cc85 20 20 69 66 28 20 75 2e 61 67 2e 63 74 78 2e 70    if( u.ag.ctx.p
1cc86 56 64 62 65 46 75 6e 63 20 29 7b 0a 20 20 20 20  VdbeFunc ){.    
1cc87 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74  sqlite3VdbeDelet
1cc88 65 41 75 78 44 61 74 61 28 75 2e 61 67 2e 63 74  eAuxData(u.ag.ct
1cc89 78 2e 70 56 64 62 65 46 75 6e 63 2c 20 70 4f 70  x.pVdbeFunc, pOp
1cc8a 2d 3e 70 31 29 3b 0a 20 20 20 20 70 4f 70 2d 3e  ->p1);.    pOp->
1cc8b 70 34 2e 70 56 64 62 65 46 75 6e 63 20 3d 20 75  p4.pVdbeFunc = u
1cc8c 2e 61 67 2e 63 74 78 2e 70 56 64 62 65 46 75 6e  .ag.ctx.pVdbeFun
1cc8d 63 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79  c;.    pOp->p4ty
1cc8e 70 65 20 3d 20 50 34 5f 56 44 42 45 46 55 4e 43  pe = P4_VDBEFUNC
1cc8f 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  ;.  }..  /* If t
1cc90 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  he function retu
1cc91 72 6e 65 64 20 61 6e 20 65 72 72 6f 72 2c 20 74  rned an error, t
1cc92 68 72 6f 77 20 61 6e 20 65 78 63 65 70 74 69 6f  hrow an exceptio
1cc93 6e 20 2a 2f 0a 20 20 69 66 28 20 75 2e 61 67 2e  n */.  if( u.ag.
1cc94 63 74 78 2e 69 73 45 72 72 6f 72 20 29 7b 0a 20  ctx.isError ){. 
1cc95 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72     sqlite3SetStr
1cc96 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c  ing(&p->zErrMsg,
1cc97 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74   db, "%s", sqlit
1cc98 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 26 75  e3_value_text(&u
1cc99 2e 61 67 2e 63 74 78 2e 73 29 29 3b 0a 20 20 20  .ag.ctx.s));.   
1cc9a 20 72 63 20 3d 20 75 2e 61 67 2e 63 74 78 2e 69   rc = u.ag.ctx.i
1cc9b 73 45 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20 2f  sError;.  }..  /
1cc9c 2a 20 43 6f 70 79 20 74 68 65 20 72 65 73 75 6c  * Copy the resul
1cc9d 74 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f  t of the functio
1cc9e 6e 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20  n into register 
1cc9f 50 33 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56  P3 */.  sqlite3V
1cca0 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e  dbeChangeEncodin
1cca1 67 28 26 75 2e 61 67 2e 63 74 78 2e 73 2c 20 65  g(&u.ag.ctx.s, e
1cca2 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 73 71 6c 69  ncoding);.  sqli
1cca3 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 70  te3VdbeMemMove(p
1cca4 4f 75 74 2c 20 26 75 2e 61 67 2e 63 74 78 2e 73  Out, &u.ag.ctx.s
1cca5 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  );.  if( sqlite3
1cca6 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 70 4f  VdbeMemTooBig(pO
1cca7 75 74 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  ut) ){.    goto 
1cca8 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 52  too_big;.  }.  R
1cca9 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f  EGISTER_TRACE(pO
1ccaa 70 2d 3e 70 33 2c 20 70 4f 75 74 29 3b 0a 20 20  p->p3, pOut);.  
1ccab 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53  UPDATE_MAX_BLOBS
1ccac 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65  IZE(pOut);.  bre
1ccad 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1ccae 3a 20 42 69 74 41 6e 64 20 50 31 20 50 32 20 50  : BitAnd P1 P2 P
1ccaf 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65  3 * *.**.** Take
1ccb0 20 74 68 65 20 62 69 74 2d 77 69 73 65 20 41 4e   the bit-wise AN
1ccb1 44 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20  D of the values 
1ccb2 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 61  in register P1 a
1ccb3 6e 64 20 50 32 20 61 6e 64 0a 2a 2a 20 73 74 6f  nd P2 and.** sto
1ccb4 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e  re the result in
1ccb5 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a   register P3..**
1ccb6 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 74   If either input
1ccb7 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65   is NULL, the re
1ccb8 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f  sult is NULL..*/
1ccb9 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 69 74 4f  ./* Opcode: BitO
1ccba 72 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a  r P1 P2 P3 * *.*
1ccbb 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20 62 69  *.** Take the bi
1ccbc 74 2d 77 69 73 65 20 4f 52 20 6f 66 20 74 68 65  t-wise OR of the
1ccbd 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73   values in regis
1ccbe 74 65 72 20 50 31 20 61 6e 64 20 50 32 20 61 6e  ter P1 and P2 an
1ccbf 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 72  d.** store the r
1ccc0 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65  esult in registe
1ccc1 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68  r P3..** If eith
1ccc2 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c  er input is NULL
1ccc3 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  , the result is 
1ccc4 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f  NULL..*/./* Opco
1ccc5 64 65 3a 20 53 68 69 66 74 4c 65 66 74 20 50 31  de: ShiftLeft P1
1ccc6 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a   P2 P3 * *.**.**
1ccc7 20 53 68 69 66 74 20 74 68 65 20 69 6e 74 65 67   Shift the integ
1ccc8 65 72 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  er value in regi
1ccc9 73 74 65 72 20 50 32 20 74 6f 20 74 68 65 20 6c  ster P2 to the l
1ccca 65 66 74 20 62 79 20 74 68 65 0a 2a 2a 20 6e 75  eft by the.** nu
1cccb 6d 62 65 72 20 6f 66 20 62 69 74 73 20 73 70 65  mber of bits spe
1cccc 63 69 66 69 65 64 20 62 79 20 74 68 65 20 69 6e  cified by the in
1cccd 74 65 67 65 72 20 69 6e 20 72 65 67 69 73 65 72  teger in regiser
1ccce 20 50 31 2e 0a 2a 2a 20 53 74 6f 72 65 20 74 68   P1..** Store th
1cccf 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69  e result in regi
1ccd0 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65  ster P3..** If e
1ccd1 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e  ither input is N
1ccd2 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20  ULL, the result 
1ccd3 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f  is NULL..*/./* O
1ccd4 70 63 6f 64 65 3a 20 53 68 69 66 74 52 69 67 68  pcode: ShiftRigh
1ccd5 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a  t P1 P2 P3 * *.*
1ccd6 2a 0a 2a 2a 20 53 68 69 66 74 20 74 68 65 20 69  *.** Shift the i
1ccd7 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69 6e 20  nteger value in 
1ccd8 72 65 67 69 73 74 65 72 20 50 32 20 74 6f 20 74  register P2 to t
1ccd9 68 65 20 72 69 67 68 74 20 62 79 20 74 68 65 0a  he right by the.
1ccda 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 74  ** number of bit
1ccdb 73 20 73 70 65 63 69 66 69 65 64 20 62 79 20 74  s specified by t
1ccdc 68 65 20 69 6e 74 65 67 65 72 20 69 6e 20 72 65  he integer in re
1ccdd 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a 20 53 74  gister P1..** St
1ccde 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69  ore the result i
1ccdf 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a  n register P3..*
1cce0 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75  * If either inpu
1cce1 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72  t is NULL, the r
1cce2 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a  esult is NULL..*
1cce3 2f 0a 63 61 73 65 20 4f 50 5f 42 69 74 41 6e 64  /.case OP_BitAnd
1cce4 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  :               
1cce5 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
1cce6 42 49 54 41 4e 44 2c 20 69 6e 31 2c 20 69 6e 32  BITAND, in1, in2
1cce7 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f  , out3 */.case O
1cce8 50 5f 42 69 74 4f 72 3a 20 20 20 20 20 20 20 20  P_BitOr:        
1cce9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d            /* sam
1ccea 65 20 61 73 20 54 4b 5f 42 49 54 4f 52 2c 20 69  e as TK_BITOR, i
1cceb 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f  n1, in2, out3 */
1ccec 0a 63 61 73 65 20 4f 50 5f 53 68 69 66 74 4c 65  .case OP_ShiftLe
1cced 66 74 3a 20 20 20 20 20 20 20 20 20 20 20 20 20  ft:             
1ccee 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c   /* same as TK_L
1ccef 53 48 49 46 54 2c 20 69 6e 31 2c 20 69 6e 32 2c  SHIFT, in1, in2,
1ccf0 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50   out3 */.case OP
1ccf1 5f 53 68 69 66 74 52 69 67 68 74 3a 20 7b 20 20  _ShiftRight: {  
1ccf2 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65           /* same
1ccf3 20 61 73 20 54 4b 5f 52 53 48 49 46 54 2c 20 69   as TK_RSHIFT, i
1ccf4 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f  n1, in2, out3 */
1ccf5 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1ccf6 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1ccf7 20 69 6e 74 6f 20 75 2e 61 68 20 2a 2f 0a 20 20   into u.ah */.  
1ccf8 69 36 34 20 61 3b 0a 20 20 69 36 34 20 62 3b 0a  i64 a;.  i64 b;.
1ccf9 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
1ccfa 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1ccfb 69 6e 74 6f 20 75 2e 61 68 20 2a 2f 0a 0a 20 20  into u.ah */..  
1ccfc 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73  if( (pIn1->flags
1ccfd 20 7c 20 70 49 6e 32 2d 3e 66 6c 61 67 73 29 20   | pIn2->flags) 
1ccfe 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20  & MEM_Null ){.  
1ccff 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1cd00 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20  SetNull(pOut);. 
1cd01 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20     break;.  }.  
1cd02 75 2e 61 68 2e 61 20 3d 20 73 71 6c 69 74 65 33  u.ah.a = sqlite3
1cd03 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e  VdbeIntValue(pIn
1cd04 32 29 3b 0a 20 20 75 2e 61 68 2e 62 20 3d 20 73  2);.  u.ah.b = s
1cd05 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c  qlite3VdbeIntVal
1cd06 75 65 28 70 49 6e 31 29 3b 0a 20 20 73 77 69 74  ue(pIn1);.  swit
1cd07 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20  ch( pOp->opcode 
1cd08 29 7b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 42  ){.    case OP_B
1cd09 69 74 41 6e 64 3a 20 20 20 20 20 20 75 2e 61 68  itAnd:      u.ah
1cd0a 2e 61 20 26 3d 20 75 2e 61 68 2e 62 3b 20 20 20  .a &= u.ah.b;   
1cd0b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
1cd0c 65 20 4f 50 5f 42 69 74 4f 72 3a 20 20 20 20 20  e OP_BitOr:     
1cd0d 20 20 75 2e 61 68 2e 61 20 7c 3d 20 75 2e 61 68    u.ah.a |= u.ah
1cd0e 2e 62 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  .b;     break;. 
1cd0f 20 20 20 63 61 73 65 20 4f 50 5f 53 68 69 66 74     case OP_Shift
1cd10 4c 65 66 74 3a 20 20 20 75 2e 61 68 2e 61 20 3c  Left:   u.ah.a <
1cd11 3c 3d 20 75 2e 61 68 2e 62 3b 20 20 20 20 62 72  <= u.ah.b;    br
1cd12 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74  eak;.    default
1cd13 3a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  :  assert( pOp->
1cd14 6f 70 63 6f 64 65 3d 3d 4f 50 5f 53 68 69 66 74  opcode==OP_Shift
1cd15 52 69 67 68 74 20 29 3b 0a 20 20 20 20 20 20 20  Right );.       
1cd16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cd17 20 20 75 2e 61 68 2e 61 20 3e 3e 3d 20 75 2e 61    u.ah.a >>= u.a
1cd18 68 2e 62 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20  h.b;    break;. 
1cd19 20 7d 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d   }.  pOut->u.i =
1cd1a 20 75 2e 61 68 2e 61 3b 0a 20 20 4d 65 6d 53 65   u.ah.a;.  MemSe
1cd1b 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20  tTypeFlag(pOut, 
1cd1c 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61  MEM_Int);.  brea
1cd1d 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1cd1e 20 41 64 64 49 6d 6d 20 20 50 31 20 50 32 20 2a   AddImm  P1 P2 *
1cd1f 20 2a 20 2a 0a 2a 2a 20 0a 2a 2a 20 41 64 64 20   * *.** .** Add 
1cd20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 50 32 20  the constant P2 
1cd21 74 6f 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  to the value in 
1cd22 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a 20  register P1..** 
1cd23 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 6c  The result is al
1cd24 77 61 79 73 20 61 6e 20 69 6e 74 65 67 65 72 2e  ways an integer.
1cd25 0a 2a 2a 0a 2a 2a 20 54 6f 20 66 6f 72 63 65 20  .**.** To force 
1cd26 61 6e 79 20 72 65 67 69 73 74 65 72 20 74 6f 20  any register to 
1cd27 62 65 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 6a  be an integer, j
1cd28 75 73 74 20 61 64 64 20 30 2e 0a 2a 2f 0a 63 61  ust add 0..*/.ca
1cd29 73 65 20 4f 50 5f 41 64 64 49 6d 6d 3a 20 7b 20  se OP_AddImm: { 
1cd2a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e             /* in
1cd2b 31 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64  1 */.  sqlite3Vd
1cd2c 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28  beMemIntegerify(
1cd2d 70 49 6e 31 29 3b 0a 20 20 70 49 6e 31 2d 3e 75  pIn1);.  pIn1->u
1cd2e 2e 69 20 2b 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20  .i += pOp->p2;. 
1cd2f 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1cd30 63 6f 64 65 3a 20 4d 75 73 74 42 65 49 6e 74 20  code: MustBeInt 
1cd31 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 20 0a  P1 P2 * * *.** .
1cd32 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c  ** Force the val
1cd33 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
1cd34 31 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 65 67  1 to be an integ
1cd35 65 72 2e 20 20 49 66 20 74 68 65 20 76 61 6c 75  er.  If the valu
1cd36 65 0a 2a 2a 20 69 6e 20 50 31 20 69 73 20 6e 6f  e.** in P1 is no
1cd37 74 20 61 6e 20 69 6e 74 65 67 65 72 20 61 6e 64  t an integer and
1cd38 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65   cannot be conve
1cd39 72 74 65 64 20 69 6e 74 6f 20 61 6e 20 69 6e 74  rted into an int
1cd3a 65 67 65 72 0a 2a 2a 20 77 69 74 68 6f 75 74 20  eger.** without 
1cd3b 64 61 74 61 20 6c 6f 73 73 2c 20 74 68 65 6e 20  data loss, then 
1cd3c 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79  jump immediately
1cd3d 20 74 6f 20 50 32 2c 20 6f 72 20 69 66 20 50 32   to P2, or if P2
1cd3e 3d 3d 30 0a 2a 2a 20 72 61 69 73 65 20 61 6e 20  ==0.** raise an 
1cd3f 53 51 4c 49 54 45 5f 4d 49 53 4d 41 54 43 48 20  SQLITE_MISMATCH 
1cd40 65 78 63 65 70 74 69 6f 6e 2e 0a 2a 2f 0a 63 61  exception..*/.ca
1cd41 73 65 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 3a  se OP_MustBeInt:
1cd42 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a   {            /*
1cd43 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20   jump, in1 */.  
1cd44 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 49  applyAffinity(pI
1cd45 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  n1, SQLITE_AFF_N
1cd46 55 4d 45 52 49 43 2c 20 65 6e 63 6f 64 69 6e 67  UMERIC, encoding
1cd47 29 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e  );.  if( (pIn1->
1cd48 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29  flags & MEM_Int)
1cd49 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70  ==0 ){.    if( p
1cd4a 4f 70 2d 3e 70 32 3d 3d 30 20 29 7b 0a 20 20 20  Op->p2==0 ){.   
1cd4b 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d     rc = SQLITE_M
1cd4c 49 53 4d 41 54 43 48 3b 0a 20 20 20 20 20 20 67  ISMATCH;.      g
1cd4d 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1cd4e 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 65 6c 73  _error;.    }els
1cd4f 65 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f  e{.      pc = pO
1cd50 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d  p->p2 - 1;.    }
1cd51 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4d 65  .  }else{.    Me
1cd52 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 49 6e  mSetTypeFlag(pIn
1cd53 31 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d  1, MEM_Int);.  }
1cd54 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1cd55 4f 70 63 6f 64 65 3a 20 52 65 61 6c 41 66 66 69  Opcode: RealAffi
1cd56 6e 69 74 79 20 50 31 20 2a 20 2a 20 2a 20 2a 0a  nity P1 * * * *.
1cd57 2a 2a 0a 2a 2a 20 49 66 20 72 65 67 69 73 74 65  **.** If registe
1cd58 72 20 50 31 20 68 6f 6c 64 73 20 61 6e 20 69 6e  r P1 holds an in
1cd59 74 65 67 65 72 20 63 6f 6e 76 65 72 74 20 69 74  teger convert it
1cd5a 20 74 6f 20 61 20 72 65 61 6c 20 76 61 6c 75 65   to a real value
1cd5b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63  ..**.** This opc
1cd5c 6f 64 65 20 69 73 20 75 73 65 64 20 77 68 65 6e  ode is used when
1cd5d 20 65 78 74 72 61 63 74 69 6e 67 20 69 6e 66 6f   extracting info
1cd5e 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 63  rmation from a c
1cd5f 6f 6c 75 6d 6e 20 74 68 61 74 0a 2a 2a 20 68 61  olumn that.** ha
1cd60 73 20 52 45 41 4c 20 61 66 66 69 6e 69 74 79 2e  s REAL affinity.
1cd61 20 20 53 75 63 68 20 63 6f 6c 75 6d 6e 20 76 61    Such column va
1cd62 6c 75 65 73 20 6d 61 79 20 73 74 69 6c 6c 20 62  lues may still b
1cd63 65 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a 20 69  e stored as.** i
1cd64 6e 74 65 67 65 72 73 2c 20 66 6f 72 20 73 70 61  ntegers, for spa
1cd65 63 65 20 65 66 66 69 63 69 65 6e 63 79 2c 20 62  ce efficiency, b
1cd66 75 74 20 61 66 74 65 72 20 65 78 74 72 61 63 74  ut after extract
1cd67 69 6f 6e 20 77 65 20 77 61 6e 74 20 74 68 65 6d  ion we want them
1cd68 0a 2a 2a 20 74 6f 20 68 61 76 65 20 6f 6e 6c 79  .** to have only
1cd69 20 61 20 72 65 61 6c 20 76 61 6c 75 65 2e 0a 2a   a real value..*
1cd6a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 61 6c 41 66  /.case OP_RealAf
1cd6b 66 69 6e 69 74 79 3a 20 7b 20 20 20 20 20 20 20  finity: {       
1cd6c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e             /* in
1cd6d 31 20 2a 2f 0a 20 20 69 66 28 20 70 49 6e 31 2d  1 */.  if( pIn1-
1cd6e 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74  >flags & MEM_Int
1cd6f 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
1cd70 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 70 49  dbeMemRealify(pI
1cd71 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  n1);.  }.  break
1cd72 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
1cd73 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 2f 2a  ITE_OMIT_CAST./*
1cd74 20 4f 70 63 6f 64 65 3a 20 54 6f 54 65 78 74 20   Opcode: ToText 
1cd75 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  P1 * * * *.**.**
1cd76 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65   Force the value
1cd77 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
1cd78 74 6f 20 62 65 20 74 65 78 74 2e 0a 2a 2a 20 49  to be text..** I
1cd79 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 6e  f the value is n
1cd7a 75 6d 65 72 69 63 2c 20 63 6f 6e 76 65 72 74 20  umeric, convert 
1cd7b 69 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20 75  it to a string u
1cd7c 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65 71 75 69  sing the.** equi
1cd7d 76 61 6c 65 6e 74 20 6f 66 20 70 72 69 6e 74 66  valent of printf
1cd7e 28 29 2e 20 20 42 6c 6f 62 20 76 61 6c 75 65 73  ().  Blob values
1cd7f 20 61 72 65 20 75 6e 63 68 61 6e 67 65 64 20 61   are unchanged a
1cd80 6e 64 0a 2a 2a 20 61 72 65 20 61 66 74 65 72 77  nd.** are afterw
1cd81 61 72 64 73 20 73 69 6d 70 6c 79 20 69 6e 74 65  ards simply inte
1cd82 72 70 72 65 74 65 64 20 61 73 20 74 65 78 74 2e  rpreted as text.
1cd83 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61  .**.** A NULL va
1cd84 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67  lue is not chang
1cd85 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69  ed by this routi
1cd86 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20  ne.  It remains 
1cd87 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  NULL..*/.case OP
1cd88 5f 54 6f 54 65 78 74 3a 20 7b 20 20 20 20 20 20  _ToText: {      
1cd89 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
1cd8a 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 54 45 58  ame as TK_TO_TEX
1cd8b 54 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20  T, in1 */.  if( 
1cd8c 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
1cd8d 4d 5f 4e 75 6c 6c 20 29 20 62 72 65 61 6b 3b 0a  M_Null ) break;.
1cd8e 20 20 61 73 73 65 72 74 28 20 4d 45 4d 5f 53 74    assert( MEM_St
1cd8f 72 3d 3d 28 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 29  r==(MEM_Blob>>3)
1cd90 20 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 67   );.  pIn1->flag
1cd91 73 20 7c 3d 20 28 70 49 6e 31 2d 3e 66 6c 61 67  s |= (pIn1->flag
1cd92 73 26 4d 45 4d 5f 42 6c 6f 62 29 3e 3e 33 3b 0a  s&MEM_Blob)>>3;.
1cd93 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28    applyAffinity(
1cd94 70 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46 46  pIn1, SQLITE_AFF
1cd95 5f 54 45 58 54 2c 20 65 6e 63 6f 64 69 6e 67 29  _TEXT, encoding)
1cd96 3b 0a 20 20 72 63 20 3d 20 45 78 70 61 6e 64 42  ;.  rc = ExpandB
1cd97 6c 6f 62 28 70 49 6e 31 29 3b 0a 20 20 61 73 73  lob(pIn1);.  ass
1cd98 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73  ert( pIn1->flags
1cd99 20 26 20 4d 45 4d 5f 53 74 72 20 7c 7c 20 64 62   & MEM_Str || db
1cd9a 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
1cd9b 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20  ;.  pIn1->flags 
1cd9c 26 3d 20 7e 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d  &= ~(MEM_Int|MEM
1cd9d 5f 52 65 61 6c 7c 4d 45 4d 5f 42 6c 6f 62 7c 4d  _Real|MEM_Blob|M
1cd9e 45 4d 5f 5a 65 72 6f 29 3b 0a 20 20 55 50 44 41  EM_Zero);.  UPDA
1cd9f 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28  TE_MAX_BLOBSIZE(
1cda0 70 49 6e 31 29 3b 0a 20 20 62 72 65 61 6b 3b 0a  pIn1);.  break;.
1cda1 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f  }../* Opcode: To
1cda2 42 6c 6f 62 20 50 31 20 2a 20 2a 20 2a 20 2a 0a  Blob P1 * * * *.
1cda3 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20  **.** Force the 
1cda4 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65  value in registe
1cda5 72 20 50 31 20 74 6f 20 62 65 20 61 20 42 4c 4f  r P1 to be a BLO
1cda6 42 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c  B..** If the val
1cda7 75 65 20 69 73 20 6e 75 6d 65 72 69 63 2c 20 63  ue is numeric, c
1cda8 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 20 73  onvert it to a s
1cda9 74 72 69 6e 67 20 66 69 72 73 74 2e 0a 2a 2a 20  tring first..** 
1cdaa 53 74 72 69 6e 67 73 20 61 72 65 20 73 69 6d 70  Strings are simp
1cdab 6c 79 20 72 65 69 6e 74 65 72 70 72 65 74 65 64  ly reinterpreted
1cdac 20 61 73 20 62 6c 6f 62 73 20 77 69 74 68 20 6e   as blobs with n
1cdad 6f 20 63 68 61 6e 67 65 0a 2a 2a 20 74 6f 20 74  o change.** to t
1cdae 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 61  he underlying da
1cdaf 74 61 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c  ta..**.** A NULL
1cdb0 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 63 68   value is not ch
1cdb1 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 72 6f  anged by this ro
1cdb2 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d 61 69  utine.  It remai
1cdb3 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65  ns NULL..*/.case
1cdb4 20 4f 50 5f 54 6f 42 6c 6f 62 3a 20 7b 20 20 20   OP_ToBlob: {   
1cdb5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1cdb6 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f  * same as TK_TO_
1cdb7 42 4c 4f 42 2c 20 69 6e 31 20 2a 2f 0a 20 20 69  BLOB, in1 */.  i
1cdb8 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26  f( pIn1->flags &
1cdb9 20 4d 45 4d 5f 4e 75 6c 6c 20 29 20 62 72 65 61   MEM_Null ) brea
1cdba 6b 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e  k;.  if( (pIn1->
1cdbb 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62  flags & MEM_Blob
1cdbc 29 3d 3d 30 20 29 7b 0a 20 20 20 20 61 70 70 6c  )==0 ){.    appl
1cdbd 79 41 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20  yAffinity(pIn1, 
1cdbe 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 2c  SQLITE_AFF_TEXT,
1cdbf 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20 20   encoding);.    
1cdc0 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c  assert( pIn1->fl
1cdc1 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 7c 7c  ags & MEM_Str ||
1cdc2 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
1cdc3 64 20 29 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54  d );.    MemSetT
1cdc4 79 70 65 46 6c 61 67 28 70 49 6e 31 2c 20 4d 45  ypeFlag(pIn1, ME
1cdc5 4d 5f 42 6c 6f 62 29 3b 0a 20 20 7d 65 6c 73 65  M_Blob);.  }else
1cdc6 7b 0a 20 20 20 20 70 49 6e 31 2d 3e 66 6c 61 67  {.    pIn1->flag
1cdc7 73 20 26 3d 20 7e 28 4d 45 4d 5f 54 79 70 65 4d  s &= ~(MEM_TypeM
1cdc8 61 73 6b 26 7e 4d 45 4d 5f 42 6c 6f 62 29 3b 0a  ask&~MEM_Blob);.
1cdc9 20 20 7d 0a 20 20 55 50 44 41 54 45 5f 4d 41 58    }.  UPDATE_MAX
1cdca 5f 42 4c 4f 42 53 49 5a 45 28 70 49 6e 31 29 3b  _BLOBSIZE(pIn1);
1cdcb 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1cdcc 4f 70 63 6f 64 65 3a 20 54 6f 4e 75 6d 65 72 69  Opcode: ToNumeri
1cdcd 63 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a  c P1 * * * *.**.
1cdce 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c  ** Force the val
1cdcf 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
1cdd0 31 20 74 6f 20 62 65 20 6e 75 6d 65 72 69 63 20  1 to be numeric 
1cdd1 28 65 69 74 68 65 72 20 61 6e 0a 2a 2a 20 69 6e  (either an.** in
1cdd2 74 65 67 65 72 20 6f 72 20 61 20 66 6c 6f 61 74  teger or a float
1cdd3 69 6e 67 2d 70 6f 69 6e 74 20 6e 75 6d 62 65 72  ing-point number
1cdd4 2e 29 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c  .).** If the val
1cdd5 75 65 20 69 73 20 74 65 78 74 20 6f 72 20 62 6c  ue is text or bl
1cdd6 6f 62 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76 65  ob, try to conve
1cdd7 72 74 20 69 74 20 74 6f 20 61 6e 20 75 73 69 6e  rt it to an usin
1cdd8 67 20 74 68 65 0a 2a 2a 20 65 71 75 69 76 61 6c  g the.** equival
1cdd9 65 6e 74 20 6f 66 20 61 74 6f 69 28 29 20 6f 72  ent of atoi() or
1cdda 20 61 74 6f 66 28 29 20 61 6e 64 20 73 74 6f 72   atof() and stor
1cddb 65 20 30 20 69 66 20 6e 6f 20 73 75 63 68 20 63  e 0 if no such c
1cddc 6f 6e 76 65 72 73 69 6f 6e 20 0a 2a 2a 20 69 73  onversion .** is
1cddd 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a   possible..**.**
1cdde 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73   A NULL value is
1cddf 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20   not changed by 
1cde0 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49  this routine.  I
1cde1 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a  t remains NULL..
1cde2 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 4e 75 6d  */.case OP_ToNum
1cde3 65 72 69 63 3a 20 7b 20 20 20 20 20 20 20 20 20  eric: {         
1cde4 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65           /* same
1cde5 20 61 73 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52 49   as TK_TO_NUMERI
1cde6 43 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20  C, in1 */.  if( 
1cde7 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 28  (pIn1->flags & (
1cde8 4d 45 4d 5f 4e 75 6c 6c 7c 4d 45 4d 5f 49 6e 74  MEM_Null|MEM_Int
1cde9 7c 4d 45 4d 5f 52 65 61 6c 29 29 3d 3d 30 20 29  |MEM_Real))==0 )
1cdea 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
1cdeb 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28 70 49 6e  eMemNumerify(pIn
1cdec 31 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  1);.  }.  break;
1cded 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
1cdee 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 20 2a 2f  ITE_OMIT_CAST */
1cdef 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 49  ../* Opcode: ToI
1cdf0 6e 74 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a  nt P1 * * * *.**
1cdf1 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76 61  .** Force the va
1cdf2 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
1cdf3 50 31 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72  P1 be an integer
1cdf4 2e 20 20 49 66 0a 2a 2a 20 54 68 65 20 76 61 6c  .  If.** The val
1cdf5 75 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  ue is currently 
1cdf6 61 20 72 65 61 6c 20 6e 75 6d 62 65 72 2c 20 64  a real number, d
1cdf7 72 6f 70 20 69 74 73 20 66 72 61 63 74 69 6f 6e  rop its fraction
1cdf8 61 6c 20 70 61 72 74 2e 0a 2a 2a 20 49 66 20 74  al part..** If t
1cdf9 68 65 20 76 61 6c 75 65 20 69 73 20 74 65 78 74  he value is text
1cdfa 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f   or blob, try to
1cdfb 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61   convert it to a
1cdfc 6e 20 69 6e 74 65 67 65 72 20 75 73 69 6e 67 20  n integer using 
1cdfd 74 68 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e  the.** equivalen
1cdfe 74 20 6f 66 20 61 74 6f 69 28 29 20 61 6e 64 20  t of atoi() and 
1cdff 73 74 6f 72 65 20 30 20 69 66 20 6e 6f 20 73 75  store 0 if no su
1ce00 63 68 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73  ch conversion is
1ce01 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a   possible..**.**
1ce02 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73   A NULL value is
1ce03 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20   not changed by 
1ce04 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49  this routine.  I
1ce05 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a  t remains NULL..
1ce06 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 49 6e 74  */.case OP_ToInt
1ce07 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  : {             
1ce08 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1ce09 54 4b 5f 54 4f 5f 49 4e 54 2c 20 69 6e 31 20 2a  TK_TO_INT, in1 *
1ce0a 2f 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66  /.  if( (pIn1->f
1ce0b 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29  lags & MEM_Null)
1ce0c 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
1ce0d 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72  e3VdbeMemInteger
1ce0e 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20  ify(pIn1);.  }. 
1ce0f 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e 64   break;.}..#ifnd
1ce10 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ef SQLITE_OMIT_C
1ce11 41 53 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54  AST./* Opcode: T
1ce12 6f 52 65 61 6c 20 50 31 20 2a 20 2a 20 2a 20 2a  oReal P1 * * * *
1ce13 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65  .**.** Force the
1ce14 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
1ce15 65 72 20 50 31 20 74 6f 20 62 65 20 61 20 66 6c  er P1 to be a fl
1ce16 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d  oating point num
1ce17 62 65 72 2e 0a 2a 2a 20 49 66 20 54 68 65 20 76  ber..** If The v
1ce18 61 6c 75 65 20 69 73 20 63 75 72 72 65 6e 74 6c  alue is currentl
1ce19 79 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 63 6f  y an integer, co
1ce1a 6e 76 65 72 74 20 69 74 2e 0a 2a 2a 20 49 66 20  nvert it..** If 
1ce1b 74 68 65 20 76 61 6c 75 65 20 69 73 20 74 65 78  the value is tex
1ce1c 74 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74  t or blob, try t
1ce1d 6f 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20  o convert it to 
1ce1e 61 6e 20 69 6e 74 65 67 65 72 20 75 73 69 6e 67  an integer using
1ce1f 20 74 68 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65   the.** equivale
1ce20 6e 74 20 6f 66 20 61 74 6f 69 28 29 20 61 6e 64  nt of atoi() and
1ce21 20 73 74 6f 72 65 20 30 2e 30 20 69 66 20 6e 6f   store 0.0 if no
1ce22 20 73 75 63 68 20 63 6f 6e 76 65 72 73 69 6f 6e   such conversion
1ce23 20 69 73 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a   is possible..**
1ce24 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65  .** A NULL value
1ce25 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 20   is not changed 
1ce26 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e  by this routine.
1ce27 20 20 49 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c    It remains NUL
1ce28 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f  L..*/.case OP_To
1ce29 52 65 61 6c 3a 20 7b 20 20 20 20 20 20 20 20 20  Real: {         
1ce2a 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65           /* same
1ce2b 20 61 73 20 54 4b 5f 54 4f 5f 52 45 41 4c 2c 20   as TK_TO_REAL, 
1ce2c 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 28 70 49  in1 */.  if( (pI
1ce2d 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  n1->flags & MEM_
1ce2e 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20  Null)==0 ){.    
1ce2f 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
1ce30 61 6c 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 7d  alify(pIn1);.  }
1ce31 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64  .  break;.}.#end
1ce32 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
1ce33 54 5f 43 41 53 54 20 2a 2f 0a 0a 2f 2a 20 4f 70  T_CAST */../* Op
1ce34 63 6f 64 65 3a 20 4c 74 20 50 31 20 50 32 20 50  code: Lt P1 P2 P
1ce35 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 43 6f  3 P4 P5.**.** Co
1ce36 6d 70 61 72 65 20 74 68 65 20 76 61 6c 75 65 73  mpare the values
1ce37 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
1ce38 61 6e 64 20 50 33 2e 20 20 49 66 20 72 65 67 28  and P3.  If reg(
1ce39 50 33 29 3c 72 65 67 28 50 31 29 20 74 68 65 6e  P3)<reg(P1) then
1ce3a 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 61 64 64 72  .** jump to addr
1ce3b 65 73 73 20 50 32 2e 20 20 0a 2a 2a 0a 2a 2a 20  ess P2.  .**.** 
1ce3c 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f 4a 55  If the SQLITE_JU
1ce3d 4d 50 49 46 4e 55 4c 4c 20 62 69 74 20 6f 66 20  MPIFNULL bit of 
1ce3e 50 35 20 69 73 20 73 65 74 20 61 6e 64 20 65 69  P5 is set and ei
1ce3f 74 68 65 72 20 72 65 67 28 50 31 29 20 6f 72 0a  ther reg(P1) or.
1ce40 2a 2a 20 72 65 67 28 50 33 29 20 69 73 20 4e 55  ** reg(P3) is NU
1ce41 4c 4c 20 74 68 65 6e 20 74 61 6b 65 20 74 68 65  LL then take the
1ce42 20 6a 75 6d 70 2e 20 20 49 66 20 74 68 65 20 53   jump.  If the S
1ce43 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c  QLITE_JUMPIFNULL
1ce44 20 0a 2a 2a 20 62 69 74 20 69 73 20 63 6c 65 61   .** bit is clea
1ce45 72 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 75  r then fall thru
1ce46 20 69 66 20 65 69 74 68 65 72 20 6f 70 65 72 61   if either opera
1ce47 6e 64 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a  nd is NULL..**.*
1ce48 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 41 46 46  * The SQLITE_AFF
1ce49 5f 4d 41 53 4b 20 70 6f 72 74 69 6f 6e 20 6f 66  _MASK portion of
1ce4a 20 50 35 20 6d 75 73 74 20 62 65 20 61 6e 20 61   P5 must be an a
1ce4b 66 66 69 6e 69 74 79 20 63 68 61 72 61 63 74 65  ffinity characte
1ce4c 72 20 2d 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46  r -.** SQLITE_AF
1ce4d 46 5f 54 45 58 54 2c 20 53 51 4c 49 54 45 5f 41  F_TEXT, SQLITE_A
1ce4e 46 46 5f 49 4e 54 45 47 45 52 2c 20 61 6e 64 20  FF_INTEGER, and 
1ce4f 73 6f 20 66 6f 72 74 68 2e 20 41 6e 20 61 74 74  so forth. An att
1ce50 65 6d 70 74 20 69 73 20 6d 61 64 65 20 0a 2a 2a  empt is made .**
1ce51 20 74 6f 20 63 6f 65 72 63 65 20 62 6f 74 68 20   to coerce both 
1ce52 69 6e 70 75 74 73 20 61 63 63 6f 72 64 69 6e 67  inputs according
1ce53 20 74 6f 20 74 68 69 73 20 61 66 66 69 6e 69 74   to this affinit
1ce54 79 20 62 65 66 6f 72 65 20 74 68 65 0a 2a 2a 20  y before the.** 
1ce55 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 6d 61  comparison is ma
1ce56 64 65 2e 20 49 66 20 74 68 65 20 53 51 4c 49 54  de. If the SQLIT
1ce57 45 5f 41 46 46 5f 4d 41 53 4b 20 69 73 20 30 78  E_AFF_MASK is 0x
1ce58 30 30 2c 20 74 68 65 6e 20 6e 75 6d 65 72 69 63  00, then numeric
1ce59 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20 69 73 20  .** affinity is 
1ce5a 75 73 65 64 2e 20 4e 6f 74 65 20 74 68 61 74 20  used. Note that 
1ce5b 74 68 65 20 61 66 66 69 6e 69 74 79 20 63 6f 6e  the affinity con
1ce5c 76 65 72 73 69 6f 6e 73 20 61 72 65 20 73 74 6f  versions are sto
1ce5d 72 65 64 0a 2a 2a 20 62 61 63 6b 20 69 6e 74 6f  red.** back into
1ce5e 20 74 68 65 20 69 6e 70 75 74 20 72 65 67 69 73   the input regis
1ce5f 74 65 72 73 20 50 31 20 61 6e 64 20 50 33 2e 20  ters P1 and P3. 
1ce60 20 53 6f 20 74 68 69 73 20 6f 70 63 6f 64 65 20   So this opcode 
1ce61 63 61 6e 20 63 61 75 73 65 0a 2a 2a 20 70 65 72  can cause.** per
1ce62 73 69 73 74 65 6e 74 20 63 68 61 6e 67 65 73 20  sistent changes 
1ce63 74 6f 20 72 65 67 69 73 74 65 72 73 20 50 31 20  to registers P1 
1ce64 61 6e 64 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 4f 6e  and P3..**.** On
1ce65 63 65 20 61 6e 79 20 63 6f 6e 76 65 72 73 69 6f  ce any conversio
1ce66 6e 73 20 68 61 76 65 20 74 61 6b 65 6e 20 70 6c  ns have taken pl
1ce67 61 63 65 2c 20 61 6e 64 20 6e 65 69 74 68 65 72  ace, and neither
1ce68 20 76 61 6c 75 65 20 69 73 20 4e 55 4c 4c 2c 20   value is NULL, 
1ce69 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 73 20 61  .** the values a
1ce6a 72 65 20 63 6f 6d 70 61 72 65 64 2e 20 49 66 20  re compared. If 
1ce6b 62 6f 74 68 20 76 61 6c 75 65 73 20 61 72 65 20  both values are 
1ce6c 62 6c 6f 62 73 20 74 68 65 6e 20 6d 65 6d 63 6d  blobs then memcm
1ce6d 70 28 29 20 69 73 0a 2a 2a 20 75 73 65 64 20 74  p() is.** used t
1ce6e 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20  o determine the 
1ce6f 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 63  results of the c
1ce70 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 49 66 20 62  omparison.  If b
1ce71 6f 74 68 20 76 61 6c 75 65 73 0a 2a 2a 20 61 72  oth values.** ar
1ce72 65 20 74 65 78 74 2c 20 74 68 65 6e 20 74 68 65  e text, then the
1ce73 20 61 70 70 72 6f 70 72 69 61 74 65 20 63 6f 6c   appropriate col
1ce74 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  lating function 
1ce75 73 70 65 63 69 66 69 65 64 20 69 6e 0a 2a 2a 20  specified in.** 
1ce76 50 34 20 69 73 20 20 75 73 65 64 20 74 6f 20 64  P4 is  used to d
1ce77 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e  o the comparison
1ce78 2e 20 20 49 66 20 50 34 20 69 73 20 6e 6f 74 20  .  If P4 is not 
1ce79 73 70 65 63 69 66 69 65 64 20 74 68 65 6e 0a 2a  specified then.*
1ce7a 2a 20 6d 65 6d 63 6d 70 28 29 20 69 73 20 75 73  * memcmp() is us
1ce7b 65 64 20 74 6f 20 63 6f 6d 70 61 72 65 20 74 65  ed to compare te
1ce7c 78 74 20 73 74 72 69 6e 67 2e 20 20 49 66 20 62  xt string.  If b
1ce7d 6f 74 68 20 76 61 6c 75 65 73 20 61 72 65 0a 2a  oth values are.*
1ce7e 2a 20 6e 75 6d 65 72 69 63 2c 20 74 68 65 6e 20  * numeric, then 
1ce7f 61 20 6e 75 6d 65 72 69 63 20 63 6f 6d 70 61 72  a numeric compar
1ce80 69 73 6f 6e 20 69 73 20 75 73 65 64 2e 20 49 66  ison is used. If
1ce81 20 74 68 65 20 74 77 6f 20 76 61 6c 75 65 73 0a   the two values.
1ce82 2a 2a 20 61 72 65 20 6f 66 20 64 69 66 66 65 72  ** are of differ
1ce83 65 6e 74 20 74 79 70 65 73 2c 20 74 68 65 6e 20  ent types, then 
1ce84 6e 75 6d 62 65 72 73 20 61 72 65 20 63 6f 6e 73  numbers are cons
1ce85 69 64 65 72 65 64 20 6c 65 73 73 20 74 68 61 6e  idered less than
1ce86 0a 2a 2a 20 73 74 72 69 6e 67 73 20 61 6e 64 20  .** strings and 
1ce87 73 74 72 69 6e 67 73 20 61 72 65 20 63 6f 6e 73  strings are cons
1ce88 69 64 65 72 65 64 20 6c 65 73 73 20 74 68 61 6e  idered less than
1ce89 20 62 6c 6f 62 73 2e 0a 2a 2a 0a 2a 2a 20 49 66   blobs..**.** If
1ce8a 20 74 68 65 20 53 51 4c 49 54 45 5f 53 54 4f 52   the SQLITE_STOR
1ce8b 45 50 32 20 62 69 74 20 6f 66 20 50 35 20 69 73  EP2 bit of P5 is
1ce8c 20 73 65 74 2c 20 74 68 65 6e 20 64 6f 20 6e 6f   set, then do no
1ce8d 74 20 6a 75 6d 70 2e 20 20 49 6e 73 74 65 61 64  t jump.  Instead
1ce8e 2c 0a 2a 2a 20 73 74 6f 72 65 20 61 20 62 6f 6f  ,.** store a boo
1ce8f 6c 65 61 6e 20 72 65 73 75 6c 74 20 28 65 69 74  lean result (eit
1ce90 68 65 72 20 30 2c 20 6f 72 20 31 2c 20 6f 72 20  her 0, or 1, or 
1ce91 4e 55 4c 4c 29 20 69 6e 20 72 65 67 69 73 74 65  NULL) in registe
1ce92 72 20 50 32 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f  r P2..*/./* Opco
1ce93 64 65 3a 20 4e 65 20 50 31 20 50 32 20 50 33 20  de: Ne P1 P2 P3 
1ce94 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73  P4 P5.**.** This
1ce95 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65   works just like
1ce96 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 65   the Lt opcode e
1ce97 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20 6a  xcept that the j
1ce98 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66 0a  ump is taken if.
1ce99 2a 2a 20 74 68 65 20 6f 70 65 72 61 6e 64 73 20  ** the operands 
1ce9a 69 6e 20 72 65 67 69 73 74 65 72 73 20 50 31 20  in registers P1 
1ce9b 61 6e 64 20 50 33 20 61 72 65 20 6e 6f 74 20 65  and P3 are not e
1ce9c 71 75 61 6c 2e 20 20 53 65 65 20 74 68 65 20 4c  qual.  See the L
1ce9d 74 20 6f 70 63 6f 64 65 20 66 6f 72 0a 2a 2a 20  t opcode for.** 
1ce9e 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  additional infor
1ce9f 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70  mation..*/./* Op
1cea0 63 6f 64 65 3a 20 45 71 20 50 31 20 50 32 20 50  code: Eq P1 P2 P
1cea1 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68  3 P4 P5.**.** Th
1cea2 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69  is works just li
1cea3 6b 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65  ke the Lt opcode
1cea4 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65   except that the
1cea5 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 69   jump is taken i
1cea6 66 0a 2a 2a 20 74 68 65 20 6f 70 65 72 61 6e 64  f.** the operand
1cea7 73 20 69 6e 20 72 65 67 69 73 74 65 72 73 20 50  s in registers P
1cea8 31 20 61 6e 64 20 50 33 20 61 72 65 20 65 71 75  1 and P3 are equ
1cea9 61 6c 2e 0a 2a 2a 20 53 65 65 20 74 68 65 20 4c  al..** See the L
1ceaa 74 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64  t opcode for add
1ceab 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
1ceac 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64  ion..*/./* Opcod
1cead 65 3a 20 4c 65 20 50 31 20 50 32 20 50 33 20 50  e: Le P1 P2 P3 P
1ceae 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  4 P5.**.** This 
1ceaf 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20  works just like 
1ceb0 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78  the Lt opcode ex
1ceb1 63 65 70 74 20 74 68 61 74 20 74 68 65 20 6a 75  cept that the ju
1ceb2 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a  mp is taken if.*
1ceb3 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  * the content of
1ceb4 20 72 65 67 69 73 74 65 72 20 50 33 20 69 73 20   register P3 is 
1ceb5 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75  less than or equ
1ceb6 61 6c 20 74 6f 20 74 68 65 20 63 6f 6e 74 65 6e  al to the conten
1ceb7 74 20 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65 72  t of.** register
1ceb8 20 50 31 2e 20 20 53 65 65 20 74 68 65 20 4c 74   P1.  See the Lt
1ceb9 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69   opcode for addi
1ceba 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69  tional informati
1cebb 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65  on..*/./* Opcode
1cebc 3a 20 47 74 20 50 31 20 50 32 20 50 33 20 50 34  : Gt P1 P2 P3 P4
1cebd 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77   P5.**.** This w
1cebe 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74  orks just like t
1cebf 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63  he Lt opcode exc
1cec0 65 70 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d  ept that the jum
1cec1 70 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a  p is taken if.**
1cec2 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20   the content of 
1cec3 72 65 67 69 73 74 65 72 20 50 33 20 69 73 20 67  register P3 is g
1cec4 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20  reater than the 
1cec5 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 72 65  content of.** re
1cec6 67 69 73 74 65 72 20 50 31 2e 20 20 53 65 65 20  gister P1.  See 
1cec7 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f  the Lt opcode fo
1cec8 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66  r additional inf
1cec9 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20  ormation..*/./* 
1ceca 4f 70 63 6f 64 65 3a 20 47 65 20 50 31 20 50 32  Opcode: Ge P1 P2
1cecb 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20   P3 P4 P5.**.** 
1cecc 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 20  This works just 
1cecd 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 63 6f  like the Lt opco
1cece 64 65 20 65 78 63 65 70 74 20 74 68 61 74 20 74  de except that t
1cecf 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e  he jump is taken
1ced0 20 69 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65   if.** the conte
1ced1 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72 20 50  nt of register P
1ced2 33 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  3 is greater tha
1ced3 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68  n or equal to th
1ced4 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20  e content of.** 
1ced5 72 65 67 69 73 74 65 72 20 50 31 2e 20 20 53 65  register P1.  Se
1ced6 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20  e the Lt opcode 
1ced7 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69  for additional i
1ced8 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 63  nformation..*/.c
1ced9 61 73 65 20 4f 50 5f 45 71 3a 20 20 20 20 20 20  ase OP_Eq:      
1ceda 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65           /* same
1cedb 20 61 73 20 54 4b 5f 45 51 2c 20 6a 75 6d 70 2c   as TK_EQ, jump,
1cedc 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 73   in1, in3 */.cas
1cedd 65 20 4f 50 5f 4e 65 3a 20 20 20 20 20 20 20 20  e OP_Ne:        
1cede 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
1cedf 73 20 54 4b 5f 4e 45 2c 20 6a 75 6d 70 2c 20 69  s TK_NE, jump, i
1cee0 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20  n1, in3 */.case 
1cee1 4f 50 5f 4c 74 3a 20 20 20 20 20 20 20 20 20 20  OP_Lt:          
1cee2 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1cee3 54 4b 5f 4c 54 2c 20 6a 75 6d 70 2c 20 69 6e 31  TK_LT, jump, in1
1cee4 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50  , in3 */.case OP
1cee5 5f 4c 65 3a 20 20 20 20 20 20 20 20 20 20 20 20  _Le:            
1cee6 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
1cee7 5f 4c 45 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20  _LE, jump, in1, 
1cee8 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 47  in3 */.case OP_G
1cee9 74 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t:              
1ceea 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 47   /* same as TK_G
1ceeb 54 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e  T, jump, in1, in
1ceec 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 65 3a  3 */.case OP_Ge:
1ceed 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f   {             /
1ceee 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 47 45 2c  * same as TK_GE,
1ceef 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20   jump, in1, in3 
1cef0 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63  */.#if 0  /* loc
1cef1 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1cef2 65 64 20 69 6e 74 6f 20 75 2e 61 69 20 2a 2f 0a  ed into u.ai */.
1cef3 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20 69    int flags;.  i
1cef4 6e 74 20 72 65 73 3b 0a 20 20 63 68 61 72 20 61  nt res;.  char a
1cef5 66 66 69 6e 69 74 79 3b 0a 23 65 6e 64 69 66 20  ffinity;.#endif 
1cef6 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1cef7 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1cef8 61 69 20 2a 2f 0a 0a 20 20 75 2e 61 69 2e 66 6c  ai */..  u.ai.fl
1cef9 61 67 73 20 3d 20 70 49 6e 31 2d 3e 66 6c 61 67  ags = pIn1->flag
1cefa 73 7c 70 49 6e 33 2d 3e 66 6c 61 67 73 3b 0a 0a  s|pIn3->flags;..
1cefb 20 20 69 66 28 20 75 2e 61 69 2e 66 6c 61 67 73    if( u.ai.flags
1cefc 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20  &MEM_Null ){.   
1cefd 20 2f 2a 20 49 66 20 65 69 74 68 65 72 20 6f 70   /* If either op
1cefe 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 74 68  erand is NULL th
1ceff 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  en the result is
1cf00 20 61 6c 77 61 79 73 20 4e 55 4c 4c 2e 0a 20 20   always NULL..  
1cf01 20 20 2a 2a 20 54 68 65 20 6a 75 6d 70 20 69 73    ** The jump is
1cf02 20 74 61 6b 65 6e 20 69 66 20 74 68 65 20 53 51   taken if the SQ
1cf03 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20  LITE_JUMPIFNULL 
1cf04 62 69 74 20 69 73 20 73 65 74 2e 0a 20 20 20 20  bit is set..    
1cf05 2a 2f 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e  */.    if( pOp->
1cf06 70 35 20 26 20 53 51 4c 49 54 45 5f 53 54 4f 52  p5 & SQLITE_STOR
1cf07 45 50 32 20 29 7b 0a 20 20 20 20 20 20 70 4f 75  EP2 ){.      pOu
1cf08 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70  t = &p->aMem[pOp
1cf09 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20 4d 65 6d  ->p2];.      Mem
1cf0a 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74  SetTypeFlag(pOut
1cf0b 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 20 20 20  , MEM_Null);.   
1cf0c 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43     REGISTER_TRAC
1cf0d 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75 74 29  E(pOp->p2, pOut)
1cf0e 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
1cf0f 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 45  pOp->p5 & SQLITE
1cf10 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 29 7b 0a 20  _JUMPIFNULL ){. 
1cf11 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70       pc = pOp->p
1cf12 32 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 62  2-1;.    }.    b
1cf13 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 75 2e 61  reak;.  }..  u.a
1cf14 69 2e 61 66 66 69 6e 69 74 79 20 3d 20 70 4f 70  i.affinity = pOp
1cf15 2d 3e 70 35 20 26 20 53 51 4c 49 54 45 5f 41 46  ->p5 & SQLITE_AF
1cf16 46 5f 4d 41 53 4b 3b 0a 20 20 69 66 28 20 75 2e  F_MASK;.  if( u.
1cf17 61 69 2e 61 66 66 69 6e 69 74 79 20 29 7b 0a 20  ai.affinity ){. 
1cf18 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79     applyAffinity
1cf19 28 70 49 6e 31 2c 20 75 2e 61 69 2e 61 66 66 69  (pIn1, u.ai.affi
1cf1a 6e 69 74 79 2c 20 65 6e 63 6f 64 69 6e 67 29 3b  nity, encoding);
1cf1b 0a 20 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69  .    applyAffini
1cf1c 74 79 28 70 49 6e 33 2c 20 75 2e 61 69 2e 61 66  ty(pIn3, u.ai.af
1cf1d 66 69 6e 69 74 79 2c 20 65 6e 63 6f 64 69 6e 67  finity, encoding
1cf1e 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d  );.    if( db->m
1cf1f 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f  allocFailed ) go
1cf20 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 0a  to no_mem;.  }..
1cf21 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1cf22 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45  4type==P4_COLLSE
1cf23 51 20 7c 7c 20 70 4f 70 2d 3e 70 34 2e 70 43 6f  Q || pOp->p4.pCo
1cf24 6c 6c 3d 3d 30 20 29 3b 0a 20 20 45 78 70 61 6e  ll==0 );.  Expan
1cf25 64 42 6c 6f 62 28 70 49 6e 31 29 3b 0a 20 20 45  dBlob(pIn1);.  E
1cf26 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 33 29 3b  xpandBlob(pIn3);
1cf27 0a 20 20 75 2e 61 69 2e 72 65 73 20 3d 20 73 71  .  u.ai.res = sq
1cf28 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28  lite3MemCompare(
1cf29 70 49 6e 33 2c 20 70 49 6e 31 2c 20 70 4f 70 2d  pIn3, pIn1, pOp-
1cf2a 3e 70 34 2e 70 43 6f 6c 6c 29 3b 0a 20 20 73 77  >p4.pColl);.  sw
1cf2b 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64  itch( pOp->opcod
1cf2c 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 4f 50  e ){.    case OP
1cf2d 5f 45 71 3a 20 20 20 20 75 2e 61 69 2e 72 65 73  _Eq:    u.ai.res
1cf2e 20 3d 20 75 2e 61 69 2e 72 65 73 3d 3d 30 3b 20   = u.ai.res==0; 
1cf2f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63      break;.    c
1cf30 61 73 65 20 4f 50 5f 4e 65 3a 20 20 20 20 75 2e  ase OP_Ne:    u.
1cf31 61 69 2e 72 65 73 20 3d 20 75 2e 61 69 2e 72 65  ai.res = u.ai.re
1cf32 73 21 3d 30 3b 20 20 20 20 20 62 72 65 61 6b 3b  s!=0;     break;
1cf33 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 4c 74 3a  .    case OP_Lt:
1cf34 20 20 20 20 75 2e 61 69 2e 72 65 73 20 3d 20 75      u.ai.res = u
1cf35 2e 61 69 2e 72 65 73 3c 30 3b 20 20 20 20 20 20  .ai.res<0;      
1cf36 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
1cf37 4f 50 5f 4c 65 3a 20 20 20 20 75 2e 61 69 2e 72  OP_Le:    u.ai.r
1cf38 65 73 20 3d 20 75 2e 61 69 2e 72 65 73 3c 3d 30  es = u.ai.res<=0
1cf39 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  ;     break;.   
1cf3a 20 63 61 73 65 20 4f 50 5f 47 74 3a 20 20 20 20   case OP_Gt:    
1cf3b 75 2e 61 69 2e 72 65 73 20 3d 20 75 2e 61 69 2e  u.ai.res = u.ai.
1cf3c 72 65 73 3e 30 3b 20 20 20 20 20 20 62 72 65 61  res>0;      brea
1cf3d 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20  k;.    default: 
1cf3e 20 20 20 20 20 20 75 2e 61 69 2e 72 65 73 20 3d        u.ai.res =
1cf3f 20 75 2e 61 69 2e 72 65 73 3e 3d 30 3b 20 20 20   u.ai.res>=0;   
1cf40 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20    break;.  }..  
1cf41 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 53 51  if( pOp->p5 & SQ
1cf42 4c 49 54 45 5f 53 54 4f 52 45 50 32 20 29 7b 0a  LITE_STOREP2 ){.
1cf43 20 20 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61      pOut = &p->a
1cf44 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20  Mem[pOp->p2];.  
1cf45 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
1cf46 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b  (pOut, MEM_Int);
1cf47 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d  .    pOut->u.i =
1cf48 20 75 2e 61 69 2e 72 65 73 3b 0a 20 20 20 20 52   u.ai.res;.    R
1cf49 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f  EGISTER_TRACE(pO
1cf4a 70 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20  p->p2, pOut);.  
1cf4b 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 69 2e 72  }else if( u.ai.r
1cf4c 65 73 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70  es ){.    pc = p
1cf4d 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 7d 0a 20 20  Op->p2-1;.  }.  
1cf4e 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1cf4f 6f 64 65 3a 20 50 65 72 6d 75 74 61 74 69 6f 6e  ode: Permutation
1cf50 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a   * * * P4 *.**.*
1cf51 2a 20 53 65 74 20 74 68 65 20 70 65 72 6d 75 74  * Set the permut
1cf52 61 74 69 6f 6e 20 75 73 65 64 20 62 79 20 74 68  ation used by th
1cf53 65 20 4f 50 5f 43 6f 6d 70 61 72 65 20 6f 70 65  e OP_Compare ope
1cf54 72 61 74 6f 72 20 74 6f 20 62 65 20 74 68 65 20  rator to be the 
1cf55 61 72 72 61 79 0a 2a 2a 20 6f 66 20 69 6e 74 65  array.** of inte
1cf56 67 65 72 73 20 69 6e 20 50 34 2e 0a 2a 2a 0a 2a  gers in P4..**.*
1cf57 2a 20 54 68 65 20 70 65 72 6d 75 74 61 74 69 6f  * The permutatio
1cf58 6e 20 69 73 20 6f 6e 6c 79 20 76 61 6c 69 64 20  n is only valid 
1cf59 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 20 4f  until the next O
1cf5a 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e 2c 20 4f  P_Permutation, O
1cf5b 50 5f 43 6f 6d 70 61 72 65 2c 0a 2a 2a 20 4f 50  P_Compare,.** OP
1cf5c 5f 48 61 6c 74 2c 20 6f 72 20 4f 50 5f 52 65 73  _Halt, or OP_Res
1cf5d 75 6c 74 52 6f 77 2e 20 20 54 79 70 69 63 61 6c  ultRow.  Typical
1cf5e 6c 79 20 74 68 65 20 4f 50 5f 50 65 72 6d 75 74  ly the OP_Permut
1cf5f 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6f 63 63  ation should occ
1cf60 75 72 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c  ur.** immediatel
1cf61 79 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 4f  y prior to the O
1cf62 50 5f 43 6f 6d 70 61 72 65 2e 0a 2a 2f 0a 63 61  P_Compare..*/.ca
1cf63 73 65 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f  se OP_Permutatio
1cf64 6e 3a 20 7b 0a 20 20 61 73 73 65 72 74 28 20 70  n: {.  assert( p
1cf65 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 49  Op->p4type==P4_I
1cf66 4e 54 41 52 52 41 59 20 29 3b 0a 20 20 61 73 73  NTARRAY );.  ass
1cf67 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 61 69 20  ert( pOp->p4.ai 
1cf68 29 3b 0a 20 20 61 50 65 72 6d 75 74 65 20 3d 20  );.  aPermute = 
1cf69 70 4f 70 2d 3e 70 34 2e 61 69 3b 0a 20 20 62 72  pOp->p4.ai;.  br
1cf6a 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1cf6b 65 3a 20 43 6f 6d 70 61 72 65 20 50 31 20 50 32  e: Compare P1 P2
1cf6c 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 43   P3 P4 *.**.** C
1cf6d 6f 6d 70 61 72 65 20 74 6f 20 76 65 63 74 6f 72  ompare to vector
1cf6e 73 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 69  s of registers i
1cf6f 6e 20 72 65 67 28 50 31 29 2e 2e 72 65 67 28 50  n reg(P1)..reg(P
1cf70 31 2b 50 33 2d 31 29 20 28 61 6c 6c 20 74 68 69  1+P3-1) (all thi
1cf71 73 0a 2a 2a 20 6f 6e 65 20 22 41 22 29 20 61 6e  s.** one "A") an
1cf72 64 20 69 6e 20 72 65 67 28 50 32 29 2e 2e 72 65  d in reg(P2)..re
1cf73 67 28 50 32 2b 50 33 2d 31 29 20 28 22 42 22 29  g(P2+P3-1) ("B")
1cf74 2e 20 20 53 61 76 65 20 74 68 65 20 72 65 73 75  .  Save the resu
1cf75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6d  lt of.** the com
1cf76 70 61 72 69 73 6f 6e 20 66 6f 72 20 75 73 65 20  parison for use 
1cf77 62 79 20 74 68 65 20 6e 65 78 74 20 4f 50 5f 4a  by the next OP_J
1cf78 75 6d 70 20 69 6e 73 74 72 75 63 74 2e 0a 2a 2a  ump instruct..**
1cf79 0a 2a 2a 20 50 34 20 69 73 20 61 20 4b 65 79 49  .** P4 is a KeyI
1cf7a 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 74 68  nfo structure th
1cf7b 61 74 20 64 65 66 69 6e 65 73 20 63 6f 6c 6c 61  at defines colla
1cf7c 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20 61  ting sequences a
1cf7d 6e 64 20 73 6f 72 74 0a 2a 2a 20 6f 72 64 65 72  nd sort.** order
1cf7e 73 20 66 6f 72 20 74 68 65 20 63 6f 6d 70 61 72  s for the compar
1cf7f 69 73 6f 6e 2e 20 20 54 68 65 20 70 65 72 6d 75  ison.  The permu
1cf80 74 61 74 69 6f 6e 20 61 70 70 6c 69 65 73 20 74  tation applies t
1cf81 6f 20 72 65 67 69 73 74 65 72 73 0a 2a 2a 20 6f  o registers.** o
1cf82 6e 6c 79 2e 20 20 54 68 65 20 4b 65 79 49 6e 66  nly.  The KeyInf
1cf83 6f 20 65 6c 65 6d 65 6e 74 73 20 61 72 65 20 75  o elements are u
1cf84 73 65 64 20 73 65 71 75 65 6e 74 69 61 6c 6c 79  sed sequentially
1cf85 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6d 70  ..**.** The comp
1cf86 61 72 69 73 6f 6e 20 69 73 20 61 20 73 6f 72 74  arison is a sort
1cf87 20 63 6f 6d 70 61 72 69 73 6f 6e 2c 20 73 6f 20   comparison, so 
1cf88 4e 55 4c 4c 73 20 63 6f 6d 70 61 72 65 20 65 71  NULLs compare eq
1cf89 75 61 6c 2c 0a 2a 2a 20 4e 55 4c 4c 73 20 61 72  ual,.** NULLs ar
1cf8a 65 20 6c 65 73 73 20 74 68 61 6e 20 6e 75 6d 62  e less than numb
1cf8b 65 72 73 2c 20 6e 75 6d 62 65 72 73 20 61 72 65  ers, numbers are
1cf8c 20 6c 65 73 73 20 74 68 61 6e 20 73 74 72 69 6e   less than strin
1cf8d 67 73 2c 0a 2a 2a 20 61 6e 64 20 73 74 72 69 6e  gs,.** and strin
1cf8e 67 73 20 61 72 65 20 6c 65 73 73 20 74 68 61 6e  gs are less than
1cf8f 20 62 6c 6f 62 73 2e 0a 2a 2f 0a 63 61 73 65 20   blobs..*/.case 
1cf90 4f 50 5f 43 6f 6d 70 61 72 65 3a 20 7b 0a 23 69  OP_Compare: {.#i
1cf91 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1cf92 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1cf93 74 6f 20 75 2e 61 6a 20 2a 2f 0a 20 20 69 6e 74  to u.aj */.  int
1cf94 20 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69   n;.  int i;.  i
1cf95 6e 74 20 70 31 3b 0a 20 20 69 6e 74 20 70 32 3b  nt p1;.  int p2;
1cf96 0a 20 20 63 6f 6e 73 74 20 4b 65 79 49 6e 66 6f  .  const KeyInfo
1cf97 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 69 6e   *pKeyInfo;.  in
1cf98 74 20 69 64 78 3b 0a 20 20 43 6f 6c 6c 53 65 71  t idx;.  CollSeq
1cf99 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 2f 2a 20 43   *pColl;    /* C
1cf9a 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
1cf9b 65 20 74 6f 20 75 73 65 20 6f 6e 20 74 68 69 73  e to use on this
1cf9c 20 74 65 72 6d 20 2a 2f 0a 20 20 69 6e 74 20 62   term */.  int b
1cf9d 52 65 76 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  Rev;          /*
1cf9e 20 54 72 75 65 20 66 6f 72 20 44 45 53 43 45 4e   True for DESCEN
1cf9f 44 49 4e 47 20 73 6f 72 74 20 6f 72 64 65 72 20  DING sort order 
1cfa0 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  */.#endif /* loc
1cfa1 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1cfa2 65 64 20 69 6e 74 6f 20 75 2e 61 6a 20 2a 2f 0a  ed into u.aj */.
1cfa3 0a 20 20 75 2e 61 6a 2e 6e 20 3d 20 70 4f 70 2d  .  u.aj.n = pOp-
1cfa4 3e 70 33 3b 0a 20 20 75 2e 61 6a 2e 70 4b 65 79  >p3;.  u.aj.pKey
1cfa5 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70  Info = pOp->p4.p
1cfa6 4b 65 79 49 6e 66 6f 3b 0a 20 20 61 73 73 65 72  KeyInfo;.  asser
1cfa7 74 28 20 75 2e 61 6a 2e 6e 3e 30 20 29 3b 0a 20  t( u.aj.n>0 );. 
1cfa8 20 61 73 73 65 72 74 28 20 75 2e 61 6a 2e 70 4b   assert( u.aj.pK
1cfa9 65 79 49 6e 66 6f 21 3d 30 20 29 3b 0a 20 20 75  eyInfo!=0 );.  u
1cfaa 2e 61 6a 2e 70 31 20 3d 20 70 4f 70 2d 3e 70 31  .aj.p1 = pOp->p1
1cfab 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 6a  ;.  assert( u.aj
1cfac 2e 70 31 3e 30 20 26 26 20 75 2e 61 6a 2e 70 31  .p1>0 && u.aj.p1
1cfad 2b 75 2e 61 6a 2e 6e 3c 3d 70 2d 3e 6e 4d 65 6d  +u.aj.n<=p->nMem
1cfae 2b 31 20 29 3b 0a 20 20 75 2e 61 6a 2e 70 32 20  +1 );.  u.aj.p2 
1cfaf 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 61 73 73  = pOp->p2;.  ass
1cfb0 65 72 74 28 20 75 2e 61 6a 2e 70 32 3e 30 20 26  ert( u.aj.p2>0 &
1cfb1 26 20 75 2e 61 6a 2e 70 32 2b 75 2e 61 6a 2e 6e  & u.aj.p2+u.aj.n
1cfb2 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 20  <=p->nMem+1 );. 
1cfb3 20 66 6f 72 28 75 2e 61 6a 2e 69 3d 30 3b 20 75   for(u.aj.i=0; u
1cfb4 2e 61 6a 2e 69 3c 75 2e 61 6a 2e 6e 3b 20 75 2e  .aj.i<u.aj.n; u.
1cfb5 61 6a 2e 69 2b 2b 29 7b 0a 20 20 20 20 75 2e 61  aj.i++){.    u.a
1cfb6 6a 2e 69 64 78 20 3d 20 61 50 65 72 6d 75 74 65  j.idx = aPermute
1cfb7 20 3f 20 61 50 65 72 6d 75 74 65 5b 75 2e 61 6a   ? aPermute[u.aj
1cfb8 2e 69 5d 20 3a 20 75 2e 61 6a 2e 69 3b 0a 20 20  .i] : u.aj.i;.  
1cfb9 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45    REGISTER_TRACE
1cfba 28 75 2e 61 6a 2e 70 31 2b 75 2e 61 6a 2e 69 64  (u.aj.p1+u.aj.id
1cfbb 78 2c 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e 61 6a  x, &p->aMem[u.aj
1cfbc 2e 70 31 2b 75 2e 61 6a 2e 69 64 78 5d 29 3b 0a  .p1+u.aj.idx]);.
1cfbd 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41      REGISTER_TRA
1cfbe 43 45 28 75 2e 61 6a 2e 70 32 2b 75 2e 61 6a 2e  CE(u.aj.p2+u.aj.
1cfbf 69 64 78 2c 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e  idx, &p->aMem[u.
1cfc0 61 6a 2e 70 32 2b 75 2e 61 6a 2e 69 64 78 5d 29  aj.p2+u.aj.idx])
1cfc1 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e  ;.    assert( u.
1cfc2 61 6a 2e 69 3c 75 2e 61 6a 2e 70 4b 65 79 49 6e  aj.i<u.aj.pKeyIn
1cfc3 66 6f 2d 3e 6e 46 69 65 6c 64 20 29 3b 0a 20 20  fo->nField );.  
1cfc4 20 20 75 2e 61 6a 2e 70 43 6f 6c 6c 20 3d 20 75    u.aj.pColl = u
1cfc5 2e 61 6a 2e 70 4b 65 79 49 6e 66 6f 2d 3e 61 43  .aj.pKeyInfo->aC
1cfc6 6f 6c 6c 5b 75 2e 61 6a 2e 69 5d 3b 0a 20 20 20  oll[u.aj.i];.   
1cfc7 20 75 2e 61 6a 2e 62 52 65 76 20 3d 20 75 2e 61   u.aj.bRev = u.a
1cfc8 6a 2e 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72  j.pKeyInfo->aSor
1cfc9 74 4f 72 64 65 72 5b 75 2e 61 6a 2e 69 5d 3b 0a  tOrder[u.aj.i];.
1cfca 20 20 20 20 69 43 6f 6d 70 61 72 65 20 3d 20 73      iCompare = s
1cfcb 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65  qlite3MemCompare
1cfcc 28 26 70 2d 3e 61 4d 65 6d 5b 75 2e 61 6a 2e 70  (&p->aMem[u.aj.p
1cfcd 31 2b 75 2e 61 6a 2e 69 64 78 5d 2c 20 26 70 2d  1+u.aj.idx], &p-
1cfce 3e 61 4d 65 6d 5b 75 2e 61 6a 2e 70 32 2b 75 2e  >aMem[u.aj.p2+u.
1cfcf 61 6a 2e 69 64 78 5d 2c 20 75 2e 61 6a 2e 70 43  aj.idx], u.aj.pC
1cfd0 6f 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 69 43  oll);.    if( iC
1cfd1 6f 6d 70 61 72 65 20 29 7b 0a 20 20 20 20 20 20  ompare ){.      
1cfd2 69 66 28 20 75 2e 61 6a 2e 62 52 65 76 20 29 20  if( u.aj.bRev ) 
1cfd3 69 43 6f 6d 70 61 72 65 20 3d 20 2d 69 43 6f 6d  iCompare = -iCom
1cfd4 70 61 72 65 3b 0a 20 20 20 20 20 20 62 72 65 61  pare;.      brea
1cfd5 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61  k;.    }.  }.  a
1cfd6 50 65 72 6d 75 74 65 20 3d 20 30 3b 0a 20 20 62  Permute = 0;.  b
1cfd7 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1cfd8 64 65 3a 20 4a 75 6d 70 20 50 31 20 50 32 20 50  de: Jump P1 P2 P
1cfd9 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70  3 * *.**.** Jump
1cfda 20 74 6f 20 74 68 65 20 69 6e 73 74 72 75 63 74   to the instruct
1cfdb 69 6f 6e 20 61 74 20 61 64 64 72 65 73 73 20 50  ion at address P
1cfdc 31 2c 20 50 32 2c 20 6f 72 20 50 33 20 64 65 70  1, P2, or P3 dep
1cfdd 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65  ending on whethe
1cfde 72 0a 2a 2a 20 69 6e 20 74 68 65 20 6d 6f 73 74  r.** in the most
1cfdf 20 72 65 63 65 6e 74 20 4f 50 5f 43 6f 6d 70 61   recent OP_Compa
1cfe0 72 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74  re instruction t
1cfe1 68 65 20 50 31 20 76 65 63 74 6f 72 20 77 61 73  he P1 vector was
1cfe2 20 6c 65 73 73 20 74 68 61 6e 0a 2a 2a 20 65 71   less than.** eq
1cfe3 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74  ual to, or great
1cfe4 65 72 20 74 68 61 6e 20 74 68 65 20 50 32 20 76  er than the P2 v
1cfe5 65 63 74 6f 72 2c 20 72 65 73 70 65 63 74 69 76  ector, respectiv
1cfe6 65 6c 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ely..*/.case OP_
1cfe7 4a 75 6d 70 3a 20 7b 20 20 20 20 20 20 20 20 20  Jump: {         
1cfe8 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20      /* jump */. 
1cfe9 20 69 66 28 20 69 43 6f 6d 70 61 72 65 3c 30 20   if( iCompare<0 
1cfea 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  ){.    pc = pOp-
1cfeb 3e 70 31 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65  >p1 - 1;.  }else
1cfec 20 69 66 28 20 69 43 6f 6d 70 61 72 65 3d 3d 30   if( iCompare==0
1cfed 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70   ){.    pc = pOp
1cfee 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 65 6c 73  ->p2 - 1;.  }els
1cfef 65 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  e{.    pc = pOp-
1cff0 3e 70 33 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62  >p3 - 1;.  }.  b
1cff1 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1cff2 64 65 3a 20 41 6e 64 20 50 31 20 50 32 20 50 33  de: And P1 P2 P3
1cff3 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20   * *.**.** Take 
1cff4 74 68 65 20 6c 6f 67 69 63 61 6c 20 41 4e 44 20  the logical AND 
1cff5 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e  of the values in
1cff6 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e   registers P1 an
1cff7 64 20 50 32 20 61 6e 64 0a 2a 2a 20 77 72 69 74  d P2 and.** writ
1cff8 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74  e the result int
1cff9 6f 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a  o register P3..*
1cffa 2a 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 50  *.** If either P
1cffb 31 20 6f 72 20 50 32 20 69 73 20 30 20 28 66 61  1 or P2 is 0 (fa
1cffc 6c 73 65 29 20 74 68 65 6e 20 74 68 65 20 72 65  lse) then the re
1cffd 73 75 6c 74 20 69 73 20 30 20 65 76 65 6e 20 69  sult is 0 even i
1cffe 66 0a 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 69  f.** the other i
1cfff 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2e 20 20 41  nput is NULL.  A
1d000 20 4e 55 4c 4c 20 61 6e 64 20 74 72 75 65 20 6f   NULL and true o
1d001 72 20 74 77 6f 20 4e 55 4c 4c 73 20 67 69 76 65  r two NULLs give
1d002 0a 2a 2a 20 61 20 4e 55 4c 4c 20 6f 75 74 70 75  .** a NULL outpu
1d003 74 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a  t..*/./* Opcode:
1d004 20 4f 72 20 50 31 20 50 32 20 50 33 20 2a 20 2a   Or P1 P2 P3 * *
1d005 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20  .**.** Take the 
1d006 6c 6f 67 69 63 61 6c 20 4f 52 20 6f 66 20 74 68  logical OR of th
1d007 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69  e values in regi
1d008 73 74 65 72 20 50 31 20 61 6e 64 20 50 32 20 61  ster P1 and P2 a
1d009 6e 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 20  nd.** store the 
1d00a 61 6e 73 77 65 72 20 69 6e 20 72 65 67 69 73 74  answer in regist
1d00b 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  er P3..**.** If 
1d00c 65 69 74 68 65 72 20 50 31 20 6f 72 20 50 32 20  either P1 or P2 
1d00d 69 73 20 6e 6f 6e 7a 65 72 6f 20 28 74 72 75 65  is nonzero (true
1d00e 29 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c  ) then the resul
1d00f 74 20 69 73 20 31 20 28 74 72 75 65 29 0a 2a 2a  t is 1 (true).**
1d010 20 65 76 65 6e 20 69 66 20 74 68 65 20 6f 74 68   even if the oth
1d011 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c  er input is NULL
1d012 2e 20 20 41 20 4e 55 4c 4c 20 61 6e 64 20 66 61  .  A NULL and fa
1d013 6c 73 65 20 6f 72 20 74 77 6f 20 4e 55 4c 4c 73  lse or two NULLs
1d014 0a 2a 2a 20 67 69 76 65 20 61 20 4e 55 4c 4c 20  .** give a NULL 
1d015 6f 75 74 70 75 74 2e 0a 2a 2f 0a 63 61 73 65 20  output..*/.case 
1d016 4f 50 5f 41 6e 64 3a 20 20 20 20 20 20 20 20 20  OP_And:         
1d017 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1d018 54 4b 5f 41 4e 44 2c 20 69 6e 31 2c 20 69 6e 32  TK_AND, in1, in2
1d019 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f  , out3 */.case O
1d01a 50 5f 4f 72 3a 20 7b 20 20 20 20 20 20 20 20 20  P_Or: {         
1d01b 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
1d01c 4b 5f 4f 52 2c 20 69 6e 31 2c 20 69 6e 32 2c 20  K_OR, in1, in2, 
1d01d 6f 75 74 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f  out3 */.#if 0  /
1d01e 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1d01f 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
1d020 6b 20 2a 2f 0a 20 20 69 6e 74 20 76 31 3b 20 20  k */.  int v1;  
1d021 20 20 2f 2a 20 4c 65 66 74 20 6f 70 65 72 61 6e    /* Left operan
1d022 64 3a 20 20 30 3d 3d 46 41 4c 53 45 2c 20 31 3d  d:  0==FALSE, 1=
1d023 3d 54 52 55 45 2c 20 32 3d 3d 55 4e 4b 4e 4f 57  =TRUE, 2==UNKNOW
1d024 4e 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69  N or NULL */.  i
1d025 6e 74 20 76 32 3b 20 20 20 20 2f 2a 20 52 69 67  nt v2;    /* Rig
1d026 68 74 20 6f 70 65 72 61 6e 64 3a 20 30 3d 3d 46  ht operand: 0==F
1d027 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32  ALSE, 1==TRUE, 2
1d028 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c  ==UNKNOWN or NUL
1d029 4c 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  L */.#endif /* l
1d02a 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1d02b 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6b 20 2a  oved into u.ak *
1d02c 2f 0a 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66  /..  if( pIn1->f
1d02d 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20  lags & MEM_Null 
1d02e 29 7b 0a 20 20 20 20 75 2e 61 6b 2e 76 31 20 3d  ){.    u.ak.v1 =
1d02f 20 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   2;.  }else{.   
1d030 20 75 2e 61 6b 2e 76 31 20 3d 20 73 71 6c 69 74   u.ak.v1 = sqlit
1d031 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70  e3VdbeIntValue(p
1d032 49 6e 31 29 21 3d 30 3b 0a 20 20 7d 0a 20 20 69  In1)!=0;.  }.  i
1d033 66 28 20 70 49 6e 32 2d 3e 66 6c 61 67 73 20 26  f( pIn2->flags &
1d034 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20   MEM_Null ){.   
1d035 20 75 2e 61 6b 2e 76 32 20 3d 20 32 3b 0a 20 20   u.ak.v2 = 2;.  
1d036 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 6b 2e  }else{.    u.ak.
1d037 76 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  v2 = sqlite3Vdbe
1d038 49 6e 74 56 61 6c 75 65 28 70 49 6e 32 29 21 3d  IntValue(pIn2)!=
1d039 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 70  0;.  }.  if( pOp
1d03a 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 41 6e 64  ->opcode==OP_And
1d03b 20 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63   ){.    static c
1d03c 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
1d03d 61 72 20 61 6e 64 5f 6c 6f 67 69 63 5b 5d 20 3d  ar and_logic[] =
1d03e 20 7b 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31   { 0, 0, 0, 0, 1
1d03f 2c 20 32 2c 20 30 2c 20 32 2c 20 32 20 7d 3b 0a  , 2, 0, 2, 2 };.
1d040 20 20 20 20 75 2e 61 6b 2e 76 31 20 3d 20 61 6e      u.ak.v1 = an
1d041 64 5f 6c 6f 67 69 63 5b 75 2e 61 6b 2e 76 31 2a  d_logic[u.ak.v1*
1d042 33 2b 75 2e 61 6b 2e 76 32 5d 3b 0a 20 20 7d 65  3+u.ak.v2];.  }e
1d043 6c 73 65 7b 0a 20 20 20 20 73 74 61 74 69 63 20  lse{.    static 
1d044 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
1d045 68 61 72 20 6f 72 5f 6c 6f 67 69 63 5b 5d 20 3d  har or_logic[] =
1d046 20 7b 20 30 2c 20 31 2c 20 32 2c 20 31 2c 20 31   { 0, 1, 2, 1, 1
1d047 2c 20 31 2c 20 32 2c 20 31 2c 20 32 20 7d 3b 0a  , 1, 2, 1, 2 };.
1d048 20 20 20 20 75 2e 61 6b 2e 76 31 20 3d 20 6f 72      u.ak.v1 = or
1d049 5f 6c 6f 67 69 63 5b 75 2e 61 6b 2e 76 31 2a 33  _logic[u.ak.v1*3
1d04a 2b 75 2e 61 6b 2e 76 32 5d 3b 0a 20 20 7d 0a 20  +u.ak.v2];.  }. 
1d04b 20 69 66 28 20 75 2e 61 6b 2e 76 31 3d 3d 32 20   if( u.ak.v1==2 
1d04c 29 7b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70  ){.    MemSetTyp
1d04d 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f  eFlag(pOut, MEM_
1d04e 4e 75 6c 6c 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Null);.  }else{.
1d04f 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20      pOut->u.i = 
1d050 75 2e 61 6b 2e 76 31 3b 0a 20 20 20 20 4d 65 6d  u.ak.v1;.    Mem
1d051 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74  SetTypeFlag(pOut
1d052 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 0a  , MEM_Int);.  }.
1d053 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1d054 70 63 6f 64 65 3a 20 4e 6f 74 20 50 31 20 50 32  pcode: Not P1 P2
1d055 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 6e 74   * * *.**.** Int
1d056 65 72 70 72 65 74 20 74 68 65 20 76 61 6c 75 65  erpret the value
1d057 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
1d058 61 73 20 61 20 62 6f 6f 6c 65 61 6e 20 76 61 6c  as a boolean val
1d059 75 65 2e 20 20 53 74 6f 72 65 20 74 68 65 0a 2a  ue.  Store the.*
1d05a 2a 20 62 6f 6f 6c 65 61 6e 20 63 6f 6d 70 6c 65  * boolean comple
1d05b 6d 65 6e 74 20 69 6e 20 72 65 67 69 73 74 65 72  ment in register
1d05c 20 50 32 2e 20 20 49 66 20 74 68 65 20 76 61 6c   P2.  If the val
1d05d 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
1d05e 31 20 69 73 20 0a 2a 2a 20 4e 55 4c 4c 2c 20 74  1 is .** NULL, t
1d05f 68 65 6e 20 61 20 4e 55 4c 4c 20 69 73 20 73 74  hen a NULL is st
1d060 6f 72 65 64 20 69 6e 20 50 32 2e 0a 2a 2f 0a 63  ored in P2..*/.c
1d061 61 73 65 20 4f 50 5f 4e 6f 74 3a 20 7b 20 20 20  ase OP_Not: {   
1d062 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1d063 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 2c 20  same as TK_NOT, 
1d064 69 6e 31 20 2a 2f 0a 20 20 70 4f 75 74 20 3d 20  in1 */.  pOut = 
1d065 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32  &p->aMem[pOp->p2
1d066 5d 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66  ];.  if( pIn1->f
1d067 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20  lags & MEM_Null 
1d068 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
1d069 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75  beMemSetNull(pOu
1d06a 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  t);.  }else{.   
1d06b 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1d06c 65 74 49 6e 74 36 34 28 70 4f 75 74 2c 20 21 73  etInt64(pOut, !s
1d06d 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c  qlite3VdbeIntVal
1d06e 75 65 28 70 49 6e 31 29 29 3b 0a 20 20 7d 0a 20  ue(pIn1));.  }. 
1d06f 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1d070 63 6f 64 65 3a 20 42 69 74 4e 6f 74 20 50 31 20  code: BitNot P1 
1d071 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49  P2 * * *.**.** I
1d072 6e 74 65 72 70 72 65 74 20 74 68 65 20 63 6f 6e  nterpret the con
1d073 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72  tent of register
1d074 20 50 31 20 61 73 20 61 6e 20 69 6e 74 65 67 65   P1 as an intege
1d075 72 2e 20 20 53 74 6f 72 65 20 74 68 65 0a 2a 2a  r.  Store the.**
1d076 20 6f 6e 65 73 2d 63 6f 6d 70 6c 65 6d 65 6e 74   ones-complement
1d077 20 6f 66 20 74 68 65 20 50 31 20 76 61 6c 75 65   of the P1 value
1d078 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50   into register P
1d079 32 2e 20 20 49 66 20 50 31 20 68 6f 6c 64 73 0a  2.  If P1 holds.
1d07a 2a 2a 20 61 20 4e 55 4c 4c 20 74 68 65 6e 20 73  ** a NULL then s
1d07b 74 6f 72 65 20 61 20 4e 55 4c 4c 20 69 6e 20 50  tore a NULL in P
1d07c 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 69  2..*/.case OP_Bi
1d07d 74 4e 6f 74 3a 20 7b 20 20 20 20 20 20 20 20 20  tNot: {         
1d07e 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
1d07f 4b 5f 42 49 54 4e 4f 54 2c 20 69 6e 31 20 2a 2f  K_BITNOT, in1 */
1d080 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d  .  pOut = &p->aM
1d081 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 69  em[pOp->p2];.  i
1d082 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26  f( pIn1->flags &
1d083 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20   MEM_Null ){.   
1d084 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1d085 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20  etNull(pOut);.  
1d086 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
1d087 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36  e3VdbeMemSetInt6
1d088 34 28 70 4f 75 74 2c 20 7e 73 71 6c 69 74 65 33  4(pOut, ~sqlite3
1d089 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e  VdbeIntValue(pIn
1d08a 31 29 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  1));.  }.  break
1d08b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1d08c 49 66 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  If P1 P2 P3 * *.
1d08d 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 32  **.** Jump to P2
1d08e 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e   if the value in
1d08f 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20   register P1 is 
1d090 74 72 75 65 2e 20 20 54 68 65 20 76 61 6c 75 65  true.  The value
1d091 20 69 73 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64   is.** is consid
1d092 65 72 65 64 20 74 72 75 65 20 69 66 20 69 74 20  ered true if it 
1d093 69 73 20 6e 75 6d 65 72 69 63 20 61 6e 64 20 6e  is numeric and n
1d094 6f 6e 2d 7a 65 72 6f 2e 20 20 49 66 20 74 68 65  on-zero.  If the
1d095 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 31 20   value.** in P1 
1d096 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 6b  is NULL then tak
1d097 65 20 74 68 65 20 6a 75 6d 70 20 69 66 20 50 33  e the jump if P3
1d098 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 2f 2a 20   is true..*/./* 
1d099 4f 70 63 6f 64 65 3a 20 49 66 4e 6f 74 20 50 31  Opcode: IfNot P1
1d09a 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a   P2 P3 * *.**.**
1d09b 20 4a 75 6d 70 20 74 6f 20 50 32 20 69 66 20 74   Jump to P2 if t
1d09c 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
1d09d 73 74 65 72 20 50 31 20 69 73 20 46 61 6c 73 65  ster P1 is False
1d09e 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 0a  .  The value is.
1d09f 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64  ** is considered
1d0a0 20 74 72 75 65 20 69 66 20 69 74 20 68 61 73 20   true if it has 
1d0a1 61 20 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 20  a numeric value 
1d0a2 6f 66 20 7a 65 72 6f 2e 20 20 49 66 20 74 68 65  of zero.  If the
1d0a3 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 31 20   value.** in P1 
1d0a4 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 6b  is NULL then tak
1d0a5 65 20 74 68 65 20 6a 75 6d 70 20 69 66 20 50 33  e the jump if P3
1d0a6 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 63 61 73   is true..*/.cas
1d0a7 65 20 4f 50 5f 49 66 3a 20 20 20 20 20 20 20 20  e OP_If:        
1d0a8 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70           /* jump
1d0a9 2c 20 69 6e 31 20 2a 2f 0a 63 61 73 65 20 4f 50  , in1 */.case OP
1d0aa 5f 49 66 4e 6f 74 3a 20 7b 20 20 20 20 20 20 20  _IfNot: {       
1d0ab 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e       /* jump, in
1d0ac 31 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c  1 */.#if 0  /* l
1d0ad 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1d0ae 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6c 20 2a  oved into u.al *
1d0af 2f 0a 20 20 69 6e 74 20 63 3b 0a 23 65 6e 64 69  /.  int c;.#endi
1d0b0 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1d0b1 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1d0b2 75 2e 61 6c 20 2a 2f 0a 20 20 69 66 28 20 70 49  u.al */.  if( pI
1d0b3 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  n1->flags & MEM_
1d0b4 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 75 2e 61 6c  Null ){.    u.al
1d0b5 2e 63 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20  .c = pOp->p3;.  
1d0b6 7d 65 6c 73 65 7b 0a 23 69 66 64 65 66 20 53 51  }else{.#ifdef SQ
1d0b7 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
1d0b8 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 75 2e 61  NG_POINT.    u.a
1d0b9 6c 2e 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62  l.c = sqlite3Vdb
1d0ba 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 21  eIntValue(pIn1)!
1d0bb 3d 30 3b 0a 23 65 6c 73 65 0a 20 20 20 20 75 2e  =0;.#else.    u.
1d0bc 61 6c 2e 63 20 3d 20 73 71 6c 69 74 65 33 56 64  al.c = sqlite3Vd
1d0bd 62 65 52 65 61 6c 56 61 6c 75 65 28 70 49 6e 31  beRealValue(pIn1
1d0be 29 21 3d 30 2e 30 3b 0a 23 65 6e 64 69 66 0a 20  )!=0.0;.#endif. 
1d0bf 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f     if( pOp->opco
1d0c0 64 65 3d 3d 4f 50 5f 49 66 4e 6f 74 20 29 20 75  de==OP_IfNot ) u
1d0c1 2e 61 6c 2e 63 20 3d 20 21 75 2e 61 6c 2e 63 3b  .al.c = !u.al.c;
1d0c2 0a 20 20 7d 0a 20 20 69 66 28 20 75 2e 61 6c 2e  .  }.  if( u.al.
1d0c3 63 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f  c ){.    pc = pO
1d0c4 70 2d 3e 70 32 2d 31 3b 0a 20 20 7d 0a 20 20 62  p->p2-1;.  }.  b
1d0c5 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1d0c6 64 65 3a 20 49 73 4e 75 6c 6c 20 50 31 20 50 32  de: IsNull P1 P2
1d0c7 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d   * * *.**.** Jum
1d0c8 70 20 74 6f 20 50 32 20 69 66 20 74 68 65 20 76  p to P2 if the v
1d0c9 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
1d0ca 20 50 31 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a   P1 is NULL..*/.
1d0cb 63 61 73 65 20 4f 50 5f 49 73 4e 75 6c 6c 3a 20  case OP_IsNull: 
1d0cc 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  {            /* 
1d0cd 73 61 6d 65 20 61 73 20 54 4b 5f 49 53 4e 55 4c  same as TK_ISNUL
1d0ce 4c 2c 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a  L, jump, in1 */.
1d0cf 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61    if( (pIn1->fla
1d0d0 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d  gs & MEM_Null)!=
1d0d1 30 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f  0 ){.    pc = pO
1d0d2 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20  p->p2 - 1;.  }. 
1d0d3 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1d0d4 63 6f 64 65 3a 20 4e 6f 74 4e 75 6c 6c 20 50 31  code: NotNull P1
1d0d5 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1d0d6 4a 75 6d 70 20 74 6f 20 50 32 20 69 66 20 74 68  Jump to P2 if th
1d0d7 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
1d0d8 74 65 72 20 50 31 20 69 73 20 6e 6f 74 20 4e 55  ter P1 is not NU
1d0d9 4c 4c 2e 20 20 0a 2a 2f 0a 63 61 73 65 20 4f 50  LL.  .*/.case OP
1d0da 5f 4e 6f 74 4e 75 6c 6c 3a 20 7b 20 20 20 20 20  _NotNull: {     
1d0db 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
1d0dc 73 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c 20 6a 75  s TK_NOTNULL, ju
1d0dd 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28  mp, in1 */.  if(
1d0de 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20   (pIn1->flags & 
1d0df 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a  MEM_Null)==0 ){.
1d0e0 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32      pc = pOp->p2
1d0e1 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61   - 1;.  }.  brea
1d0e2 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1d0e3 20 43 6f 6c 75 6d 6e 20 50 31 20 50 32 20 50 33   Column P1 P2 P3
1d0e4 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 49 6e 74   P4 P5.**.** Int
1d0e5 65 72 70 72 65 74 20 74 68 65 20 64 61 74 61 20  erpret the data 
1d0e6 74 68 61 74 20 63 75 72 73 6f 72 20 50 31 20 70  that cursor P1 p
1d0e7 6f 69 6e 74 73 20 74 6f 20 61 73 20 61 20 73 74  oints to as a st
1d0e8 72 75 63 74 75 72 65 20 62 75 69 6c 74 20 75 73  ructure built us
1d0e9 69 6e 67 0a 2a 2a 20 74 68 65 20 4d 61 6b 65 52  ing.** the MakeR
1d0ea 65 63 6f 72 64 20 69 6e 73 74 72 75 63 74 69 6f  ecord instructio
1d0eb 6e 2e 20 20 28 53 65 65 20 74 68 65 20 4d 61 6b  n.  (See the Mak
1d0ec 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 20 66  eRecord opcode f
1d0ed 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a  or additional.**
1d0ee 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
1d0ef 75 74 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66  ut the format of
1d0f0 20 74 68 65 20 64 61 74 61 2e 29 20 20 45 78 74   the data.)  Ext
1d0f1 72 61 63 74 20 74 68 65 20 50 32 2d 74 68 20 63  ract the P2-th c
1d0f2 6f 6c 75 6d 6e 0a 2a 2a 20 66 72 6f 6d 20 74 68  olumn.** from th
1d0f3 69 73 20 72 65 63 6f 72 64 2e 20 20 49 66 20 74  is record.  If t
1d0f4 68 65 72 65 20 61 72 65 20 6c 65 73 73 20 74 68  here are less th
1d0f5 61 74 20 28 50 32 2b 31 29 20 0a 2a 2a 20 76 61  at (P2+1) .** va
1d0f6 6c 75 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f  lues in the reco
1d0f7 72 64 2c 20 65 78 74 72 61 63 74 20 61 20 4e 55  rd, extract a NU
1d0f8 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61  LL..**.** The va
1d0f9 6c 75 65 20 65 78 74 72 61 63 74 65 64 20 69 73  lue extracted is
1d0fa 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73   stored in regis
1d0fb 74 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66  ter P3..**.** If
1d0fc 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74   the column cont
1d0fd 61 69 6e 73 20 66 65 77 65 72 20 74 68 61 6e 20  ains fewer than 
1d0fe 50 32 20 66 69 65 6c 64 73 2c 20 74 68 65 6e 20  P2 fields, then 
1d0ff 65 78 74 72 61 63 74 20 61 20 4e 55 4c 4c 2e 20  extract a NULL. 
1d100 20 4f 72 2c 0a 2a 2a 20 69 66 20 74 68 65 20 50   Or,.** if the P
1d101 34 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20  4 argument is a 
1d102 50 34 5f 4d 45 4d 20 75 73 65 20 74 68 65 20 76  P4_MEM use the v
1d103 61 6c 75 65 20 6f 66 20 74 68 65 20 50 34 20 61  alue of the P4 a
1d104 72 67 75 6d 65 6e 74 20 61 73 0a 2a 2a 20 74 68  rgument as.** th
1d105 65 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20  e result..**.** 
1d106 49 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 43 4c  If the OPFLAG_CL
1d107 45 41 52 43 41 43 48 45 20 62 69 74 20 69 73 20  EARCACHE bit is 
1d108 73 65 74 20 6f 6e 20 50 35 20 61 6e 64 20 50 31  set on P5 and P1
1d109 20 69 73 20 61 20 70 73 65 75 64 6f 2d 74 61 62   is a pseudo-tab
1d10a 6c 65 20 63 75 72 73 6f 72 2c 0a 2a 2a 20 74 68  le cursor,.** th
1d10b 65 6e 20 74 68 65 20 63 61 63 68 65 20 6f 66 20  en the cache of 
1d10c 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 72 65  the cursor is re
1d10d 73 65 74 20 70 72 69 6f 72 20 74 6f 20 65 78 74  set prior to ext
1d10e 72 61 63 74 69 6e 67 20 74 68 65 20 63 6f 6c 75  racting the colu
1d10f 6d 6e 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  mn..** The first
1d110 20 4f 50 5f 43 6f 6c 75 6d 6e 20 61 67 61 69 6e   OP_Column again
1d111 73 74 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c  st a pseudo-tabl
1d112 65 20 61 66 74 65 72 20 74 68 65 20 76 61 6c 75  e after the valu
1d113 65 20 6f 66 20 74 68 65 20 63 6f 6e 74 65 6e 74  e of the content
1d114 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 68 61 73  .** register has
1d115 20 63 68 61 6e 67 65 64 20 73 68 6f 75 6c 64 20   changed should 
1d116 68 61 76 65 20 74 68 69 73 20 62 69 74 20 73 65  have this bit se
1d117 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f  t..*/.case OP_Co
1d118 6c 75 6d 6e 3a 20 7b 0a 23 69 66 20 30 20 20 2f  lumn: {.#if 0  /
1d119 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1d11a 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
1d11b 6d 20 2a 2f 0a 20 20 75 33 32 20 70 61 79 6c 6f  m */.  u32 paylo
1d11c 61 64 53 69 7a 65 3b 20 20 20 2f 2a 20 4e 75 6d  adSize;   /* Num
1d11d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
1d11e 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  the record */.  
1d11f 69 36 34 20 70 61 79 6c 6f 61 64 53 69 7a 65 36  i64 payloadSize6
1d120 34 3b 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20  4; /* Number of 
1d121 62 79 74 65 73 20 69 6e 20 74 68 65 20 72 65 63  bytes in the rec
1d122 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b  ord */.  int p1;
1d123 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
1d124 31 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6f  1 value of the o
1d125 70 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 70  pcode */.  int p
1d126 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  2;            /*
1d127 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 74   column number t
1d128 6f 20 72 65 74 72 69 65 76 65 20 2a 2f 0a 20 20  o retrieve */.  
1d129 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 20  VdbeCursor *pC; 
1d12a 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63     /* The VDBE c
1d12b 75 72 73 6f 72 20 2a 2f 0a 20 20 63 68 61 72 20  ursor */.  char 
1d12c 2a 7a 52 65 63 3b 20 20 20 20 20 20 20 20 2f 2a  *zRec;        /*
1d12d 20 50 6f 69 6e 74 65 72 20 74 6f 20 63 6f 6d 70   Pointer to comp
1d12e 6c 65 74 65 20 72 65 63 6f 72 64 2d 64 61 74 61  lete record-data
1d12f 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a   */.  BtCursor *
1d130 70 43 72 73 72 3b 20 20 20 2f 2a 20 54 68 65 20  pCrsr;   /* The 
1d131 42 54 72 65 65 20 63 75 72 73 6f 72 20 2a 2f 0a  BTree cursor */.
1d132 20 20 75 33 32 20 2a 61 54 79 70 65 3b 20 20 20    u32 *aType;   
1d133 20 20 20 20 20 2f 2a 20 61 54 79 70 65 5b 69 5d       /* aType[i]
1d134 20 68 6f 6c 64 73 20 74 68 65 20 6e 75 6d 65 72   holds the numer
1d135 69 63 20 74 79 70 65 20 6f 66 20 74 68 65 20 69  ic type of the i
1d136 2d 74 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20  -th column */.  
1d137 75 33 32 20 2a 61 4f 66 66 73 65 74 3b 20 20 20  u32 *aOffset;   
1d138 20 20 20 2f 2a 20 61 4f 66 66 73 65 74 5b 69 5d     /* aOffset[i]
1d139 20 69 73 20 6f 66 66 73 65 74 20 74 6f 20 73 74   is offset to st
1d13a 61 72 74 20 6f 66 20 64 61 74 61 20 66 6f 72 20  art of data for 
1d13b 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20  i-th column */. 
1d13c 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20   int nField;    
1d13d 20 20 20 20 2f 2a 20 6e 75 6d 62 65 72 20 6f 66      /* number of
1d13e 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 72   fields in the r
1d13f 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6c  ecord */.  int l
1d140 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  en;           /*
1d141 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74   The length of t
1d142 68 65 20 73 65 72 69 61 6c 69 7a 65 64 20 64 61  he serialized da
1d143 74 61 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d  ta for the colum
1d144 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20  n */.  int i;   
1d145 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
1d146 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 63  p counter */.  c
1d147 68 61 72 20 2a 7a 44 61 74 61 3b 20 20 20 20 20  har *zData;     
1d148 20 20 2f 2a 20 50 61 72 74 20 6f 66 20 74 68 65    /* Part of the
1d149 20 72 65 63 6f 72 64 20 62 65 69 6e 67 20 64 65   record being de
1d14a 63 6f 64 65 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a  coded */.  Mem *
1d14b 70 44 65 73 74 3b 20 20 20 20 20 20 20 20 2f 2a  pDest;        /*
1d14c 20 57 68 65 72 65 20 74 6f 20 77 72 69 74 65 20   Where to write 
1d14d 74 68 65 20 65 78 74 72 61 63 74 65 64 20 76 61  the extracted va
1d14e 6c 75 65 20 2a 2f 0a 20 20 4d 65 6d 20 73 4d 65  lue */.  Mem sMe
1d14f 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46  m;          /* F
1d150 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 65 20 72  or storing the r
1d151 65 63 6f 72 64 20 62 65 69 6e 67 20 64 65 63 6f  ecord being deco
1d152 64 65 64 20 2a 2f 0a 20 20 75 38 20 2a 7a 49 64  ded */.  u8 *zId
1d153 78 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  x;          /* I
1d154 6e 64 65 78 20 69 6e 74 6f 20 68 65 61 64 65 72  ndex into header
1d155 20 2a 2f 0a 20 20 75 38 20 2a 7a 45 6e 64 48 64   */.  u8 *zEndHd
1d156 72 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e  r;       /* Poin
1d157 74 65 72 20 74 6f 20 66 69 72 73 74 20 62 79 74  ter to first byt
1d158 65 20 61 66 74 65 72 20 74 68 65 20 68 65 61 64  e after the head
1d159 65 72 20 2a 2f 0a 20 20 75 33 32 20 6f 66 66 73  er */.  u32 offs
1d15a 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 66  et;        /* Of
1d15b 66 73 65 74 20 69 6e 74 6f 20 74 68 65 20 64 61  fset into the da
1d15c 74 61 20 2a 2f 0a 20 20 75 36 34 20 6f 66 66 73  ta */.  u64 offs
1d15d 65 74 36 34 3b 20 20 20 20 20 20 2f 2a 20 36 34  et64;      /* 64
1d15e 2d 62 69 74 20 6f 66 66 73 65 74 2e 20 20 36 34  -bit offset.  64
1d15f 20 62 69 74 73 20 6e 65 65 64 65 64 20 74 6f 20   bits needed to 
1d160 63 61 74 63 68 20 6f 76 65 72 66 6c 6f 77 20 2a  catch overflow *
1d161 2f 0a 20 20 69 6e 74 20 73 7a 48 64 72 3b 20 20  /.  int szHdr;  
1d162 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
1d163 66 20 74 68 65 20 68 65 61 64 65 72 20 73 69 7a  f the header siz
1d164 65 20 66 69 65 6c 64 20 61 74 20 73 74 61 72 74  e field at start
1d165 20 6f 66 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20   of record */.  
1d166 69 6e 74 20 61 76 61 69 6c 3b 20 20 20 20 20 20  int avail;      
1d167 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1d168 62 79 74 65 73 20 6f 66 20 61 76 61 69 6c 61 62  bytes of availab
1d169 6c 65 20 64 61 74 61 20 2a 2f 0a 20 20 4d 65 6d  le data */.  Mem
1d16a 20 2a 70 52 65 67 3b 20 20 20 20 20 20 20 20 20   *pReg;         
1d16b 2f 2a 20 50 73 65 75 64 6f 54 61 62 6c 65 20 69  /* PseudoTable i
1d16c 6e 70 75 74 20 72 65 67 69 73 74 65 72 20 2a 2f  nput register */
1d16d 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1d16e 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1d16f 20 69 6e 74 6f 20 75 2e 61 6d 20 2a 2f 0a 0a 0a   into u.am */...
1d170 20 20 75 2e 61 6d 2e 70 31 20 3d 20 70 4f 70 2d    u.am.p1 = pOp-
1d171 3e 70 31 3b 0a 20 20 75 2e 61 6d 2e 70 32 20 3d  >p1;.  u.am.p2 =
1d172 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 6d   pOp->p2;.  u.am
1d173 2e 70 43 20 3d 20 30 3b 0a 20 20 6d 65 6d 73 65  .pC = 0;.  memse
1d174 74 28 26 75 2e 61 6d 2e 73 4d 65 6d 2c 20 30 2c  t(&u.am.sMem, 0,
1d175 20 73 69 7a 65 6f 66 28 75 2e 61 6d 2e 73 4d 65   sizeof(u.am.sMe
1d176 6d 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75  m));.  assert( u
1d177 2e 61 6d 2e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f  .am.p1<p->nCurso
1d178 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  r );.  assert( p
1d179 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d  Op->p3>0 && pOp-
1d17a 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a  >p3<=p->nMem );.
1d17b 20 20 75 2e 61 6d 2e 70 44 65 73 74 20 3d 20 26    u.am.pDest = &
1d17c 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d  p->aMem[pOp->p3]
1d17d 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c  ;.  MemSetTypeFl
1d17e 61 67 28 75 2e 61 6d 2e 70 44 65 73 74 2c 20 4d  ag(u.am.pDest, M
1d17f 45 4d 5f 4e 75 6c 6c 29 3b 0a 20 20 75 2e 61 6d  EM_Null);.  u.am
1d180 2e 7a 52 65 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a  .zRec = 0;..  /*
1d181 20 54 68 69 73 20 62 6c 6f 63 6b 20 73 65 74 73   This block sets
1d182 20 74 68 65 20 76 61 72 69 61 62 6c 65 20 75 2e   the variable u.
1d183 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20 74  am.payloadSize t
1d184 6f 20 62 65 20 74 68 65 20 74 6f 74 61 6c 20 6e  o be the total n
1d185 75 6d 62 65 72 20 6f 66 0a 20 20 2a 2a 20 62 79  umber of.  ** by
1d186 74 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72  tes in the recor
1d187 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 75 2e 61  d..  **.  ** u.a
1d188 6d 2e 7a 52 65 63 20 69 73 20 73 65 74 20 74 6f  m.zRec is set to
1d189 20 62 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 65   be the complete
1d18a 20 74 65 78 74 20 6f 66 20 74 68 65 20 72 65 63   text of the rec
1d18b 6f 72 64 20 69 66 20 69 74 20 69 73 20 61 76 61  ord if it is ava
1d18c 69 6c 61 62 6c 65 2e 0a 20 20 2a 2a 20 54 68 65  ilable..  ** The
1d18d 20 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72 64   complete record
1d18e 20 74 65 78 74 20 69 73 20 61 6c 77 61 79 73 20   text is always 
1d18f 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 70 73  available for ps
1d190 65 75 64 6f 2d 74 61 62 6c 65 73 0a 20 20 2a 2a  eudo-tables.  **
1d191 20 49 66 20 74 68 65 20 72 65 63 6f 72 64 20 69   If the record i
1d192 73 20 73 74 6f 72 65 64 20 69 6e 20 61 20 63 75  s stored in a cu
1d193 72 73 6f 72 2c 20 74 68 65 20 63 6f 6d 70 6c 65  rsor, the comple
1d194 74 65 20 72 65 63 6f 72 64 20 74 65 78 74 0a 20  te record text. 
1d195 20 2a 2a 20 6d 69 67 68 74 20 62 65 20 61 76 61   ** might be ava
1d196 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 20 75  ilable in the  u
1d197 2e 61 6d 2e 70 43 2d 3e 61 52 6f 77 20 63 61 63  .am.pC->aRow cac
1d198 68 65 2e 20 20 4f 72 20 69 74 20 6d 69 67 68 74  he.  Or it might
1d199 20 6e 6f 74 20 62 65 2e 0a 20 20 2a 2a 20 49 66   not be..  ** If
1d19a 20 74 68 65 20 64 61 74 61 20 69 73 20 75 6e 61   the data is una
1d19b 76 61 69 6c 61 62 6c 65 2c 20 20 75 2e 61 6d 2e  vailable,  u.am.
1d19c 7a 52 65 63 20 69 73 20 73 65 74 20 74 6f 20 4e  zRec is set to N
1d19d 55 4c 4c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57  ULL..  **.  ** W
1d19e 65 20 61 6c 73 6f 20 63 6f 6d 70 75 74 65 20 74  e also compute t
1d19f 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  he number of col
1d1a0 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 63 6f  umns in the reco
1d1a1 72 64 2e 20 20 46 6f 72 20 63 75 72 73 6f 72 73  rd.  For cursors
1d1a2 2c 0a 20 20 2a 2a 20 74 68 65 20 6e 75 6d 62 65  ,.  ** the numbe
1d1a3 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 73 20  r of columns is 
1d1a4 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 56 64  stored in the Vd
1d1a5 62 65 43 75 72 73 6f 72 2e 6e 46 69 65 6c 64 20  beCursor.nField 
1d1a6 65 6c 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20  element..  */.  
1d1a7 75 2e 61 6d 2e 70 43 20 3d 20 70 2d 3e 61 70 43  u.am.pC = p->apC
1d1a8 73 72 5b 75 2e 61 6d 2e 70 31 5d 3b 0a 20 20 61  sr[u.am.p1];.  a
1d1a9 73 73 65 72 74 28 20 75 2e 61 6d 2e 70 43 21 3d  ssert( u.am.pC!=
1d1aa 30 20 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  0 );.#ifndef SQL
1d1ab 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1d1ac 54 41 42 4c 45 0a 20 20 61 73 73 65 72 74 28 20  TABLE.  assert( 
1d1ad 75 2e 61 6d 2e 70 43 2d 3e 70 56 74 61 62 43 75  u.am.pC->pVtabCu
1d1ae 72 73 6f 72 3d 3d 30 20 29 3b 0a 23 65 6e 64 69  rsor==0 );.#endi
1d1af 66 0a 20 20 75 2e 61 6d 2e 70 43 72 73 72 20 3d  f.  u.am.pCrsr =
1d1b0 20 75 2e 61 6d 2e 70 43 2d 3e 70 43 75 72 73 6f   u.am.pC->pCurso
1d1b1 72 3b 0a 20 20 69 66 28 20 75 2e 61 6d 2e 70 43  r;.  if( u.am.pC
1d1b2 72 73 72 21 3d 30 20 29 7b 0a 20 20 20 20 2f 2a  rsr!=0 ){.    /*
1d1b3 20 54 68 65 20 72 65 63 6f 72 64 20 69 73 20 73   The record is s
1d1b4 74 6f 72 65 64 20 69 6e 20 61 20 42 2d 54 72 65  tored in a B-Tre
1d1b5 65 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71  e */.    rc = sq
1d1b6 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d  lite3VdbeCursorM
1d1b7 6f 76 65 74 6f 28 75 2e 61 6d 2e 70 43 29 3b 0a  oveto(u.am.pC);.
1d1b8 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74      if( rc ) got
1d1b9 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65  o abort_due_to_e
1d1ba 72 72 6f 72 3b 0a 20 20 20 20 69 66 28 20 75 2e  rror;.    if( u.
1d1bb 61 6d 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 29  am.pC->nullRow )
1d1bc 7b 0a 20 20 20 20 20 20 75 2e 61 6d 2e 70 61 79  {.      u.am.pay
1d1bd 6c 6f 61 64 53 69 7a 65 20 3d 20 30 3b 0a 20 20  loadSize = 0;.  
1d1be 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 6d    }else if( u.am
1d1bf 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73  .pC->cacheStatus
1d1c0 3d 3d 70 2d 3e 63 61 63 68 65 43 74 72 20 29 7b  ==p->cacheCtr ){
1d1c1 0a 20 20 20 20 20 20 75 2e 61 6d 2e 70 61 79 6c  .      u.am.payl
1d1c2 6f 61 64 53 69 7a 65 20 3d 20 75 2e 61 6d 2e 70  oadSize = u.am.p
1d1c3 43 2d 3e 70 61 79 6c 6f 61 64 53 69 7a 65 3b 0a  C->payloadSize;.
1d1c4 20 20 20 20 20 20 75 2e 61 6d 2e 7a 52 65 63 20        u.am.zRec 
1d1c5 3d 20 28 63 68 61 72 2a 29 75 2e 61 6d 2e 70 43  = (char*)u.am.pC
1d1c6 2d 3e 61 52 6f 77 3b 0a 20 20 20 20 7d 65 6c 73  ->aRow;.    }els
1d1c7 65 20 69 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 69  e if( u.am.pC->i
1d1c8 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 20 20  sIndex ){.      
1d1c9 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
1d1ca 74 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c 69  treeCursorIsVali
1d1cb 64 28 75 2e 61 6d 2e 70 43 72 73 72 29 20 29 3b  d(u.am.pCrsr) );
1d1cc 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1d1cd 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28  te3BtreeKeySize(
1d1ce 75 2e 61 6d 2e 70 43 72 73 72 2c 20 26 75 2e 61  u.am.pCrsr, &u.a
1d1cf 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 29  m.payloadSize64)
1d1d0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1d1d1 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc==SQLITE_OK );
1d1d2 20 20 20 2f 2a 20 54 72 75 65 20 62 65 63 61 75     /* True becau
1d1d3 73 65 20 6f 66 20 43 75 72 73 6f 72 4d 6f 76 65  se of CursorMove
1d1d4 74 6f 28 29 20 63 61 6c 6c 20 61 62 6f 76 65 20  to() call above 
1d1d5 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 73 71 6c 69  */.      /* sqli
1d1d6 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c  te3BtreeParseCel
1d1d7 6c 50 74 72 28 29 20 75 73 65 73 20 67 65 74 56  lPtr() uses getV
1d1d8 61 72 69 6e 74 33 32 28 29 20 74 6f 20 65 78 74  arint32() to ext
1d1d9 72 61 63 74 20 74 68 65 0a 20 20 20 20 20 20 2a  ract the.      *
1d1da 2a 20 70 61 79 6c 6f 61 64 20 73 69 7a 65 2c 20  * payload size, 
1d1db 73 6f 20 69 74 20 69 73 20 69 6d 70 6f 73 73 69  so it is impossi
1d1dc 62 6c 65 20 66 6f 72 20 75 2e 61 6d 2e 70 61 79  ble for u.am.pay
1d1dd 6c 6f 61 64 53 69 7a 65 36 34 20 74 6f 20 62 65  loadSize64 to be
1d1de 0a 20 20 20 20 20 20 2a 2a 20 6c 61 72 67 65 72  .      ** larger
1d1df 20 74 68 61 6e 20 33 32 20 62 69 74 73 2e 20 2a   than 32 bits. *
1d1e0 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  /.      assert( 
1d1e1 28 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a  (u.am.payloadSiz
1d1e2 65 36 34 20 26 20 53 51 4c 49 54 45 5f 4d 41 58  e64 & SQLITE_MAX
1d1e3 5f 55 33 32 29 3d 3d 28 75 36 34 29 75 2e 61 6d  _U32)==(u64)u.am
1d1e4 2e 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 20 29  .payloadSize64 )
1d1e5 3b 0a 20 20 20 20 20 20 75 2e 61 6d 2e 70 61 79  ;.      u.am.pay
1d1e6 6c 6f 61 64 53 69 7a 65 20 3d 20 28 75 33 32 29  loadSize = (u32)
1d1e7 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65  u.am.payloadSize
1d1e8 36 34 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  64;.    }else{. 
1d1e9 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c       assert( sql
1d1ea 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49  ite3BtreeCursorI
1d1eb 73 56 61 6c 69 64 28 75 2e 61 6d 2e 70 43 72 73  sValid(u.am.pCrs
1d1ec 72 29 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  r) );.      rc =
1d1ed 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74   sqlite3BtreeDat
1d1ee 61 53 69 7a 65 28 75 2e 61 6d 2e 70 43 72 73 72  aSize(u.am.pCrsr
1d1ef 2c 20 26 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53  , &u.am.payloadS
1d1f0 69 7a 65 29 3b 0a 20 20 20 20 20 20 61 73 73 65  ize);.      asse
1d1f1 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  rt( rc==SQLITE_O
1d1f2 4b 20 29 3b 20 20 20 2f 2a 20 44 61 74 61 53 69  K );   /* DataSi
1d1f3 7a 65 28 29 20 63 61 6e 6e 6f 74 20 66 61 69 6c  ze() cannot fail
1d1f4 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73   */.    }.  }els
1d1f5 65 20 69 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 70  e if( u.am.pC->p
1d1f6 73 65 75 64 6f 54 61 62 6c 65 52 65 67 3e 30 20  seudoTableReg>0 
1d1f7 29 7b 0a 20 20 20 20 75 2e 61 6d 2e 70 52 65 67  ){.    u.am.pReg
1d1f8 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e 61 6d   = &p->aMem[u.am
1d1f9 2e 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65  .pC->pseudoTable
1d1fa 52 65 67 5d 3b 0a 20 20 20 20 61 73 73 65 72 74  Reg];.    assert
1d1fb 28 20 75 2e 61 6d 2e 70 52 65 67 2d 3e 66 6c 61  ( u.am.pReg->fla
1d1fc 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b  gs & MEM_Blob );
1d1fd 0a 20 20 20 20 75 2e 61 6d 2e 70 61 79 6c 6f 61  .    u.am.payloa
1d1fe 64 53 69 7a 65 20 3d 20 75 2e 61 6d 2e 70 52 65  dSize = u.am.pRe
1d1ff 67 2d 3e 6e 3b 0a 20 20 20 20 75 2e 61 6d 2e 7a  g->n;.    u.am.z
1d200 52 65 63 20 3d 20 75 2e 61 6d 2e 70 52 65 67 2d  Rec = u.am.pReg-
1d201 3e 7a 3b 0a 20 20 20 20 75 2e 61 6d 2e 70 43 2d  >z;.    u.am.pC-
1d202 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 28  >cacheStatus = (
1d203 70 4f 70 2d 3e 70 35 26 4f 50 46 4c 41 47 5f 43  pOp->p5&OPFLAG_C
1d204 4c 45 41 52 43 41 43 48 45 29 20 3f 20 43 41 43  LEARCACHE) ? CAC
1d205 48 45 5f 53 54 41 4c 45 20 3a 20 70 2d 3e 63 61  HE_STALE : p->ca
1d206 63 68 65 43 74 72 3b 0a 20 20 20 20 61 73 73 65  cheCtr;.    asse
1d207 72 74 28 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64  rt( u.am.payload
1d208 53 69 7a 65 3d 3d 30 20 7c 7c 20 75 2e 61 6d 2e  Size==0 || u.am.
1d209 7a 52 65 63 21 3d 30 20 29 3b 0a 20 20 7d 65 6c  zRec!=0 );.  }el
1d20a 73 65 7b 0a 20 20 20 20 2f 2a 20 43 6f 6e 73 69  se{.    /* Consi
1d20b 64 65 72 20 74 68 65 20 72 6f 77 20 74 6f 20 62  der the row to b
1d20c 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 75 2e  e NULL */.    u.
1d20d 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d  am.payloadSize =
1d20e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66   0;.  }..  /* If
1d20f 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a   u.am.payloadSiz
1d210 65 20 69 73 20 30 2c 20 74 68 65 6e 20 6a 75 73  e is 0, then jus
1d211 74 20 73 74 6f 72 65 20 61 20 4e 55 4c 4c 20 2a  t store a NULL *
1d212 2f 0a 20 20 69 66 28 20 75 2e 61 6d 2e 70 61 79  /.  if( u.am.pay
1d213 6c 6f 61 64 53 69 7a 65 3d 3d 30 20 29 7b 0a 20  loadSize==0 ){. 
1d214 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d 2e     assert( u.am.
1d215 70 44 65 73 74 2d 3e 66 6c 61 67 73 26 4d 45 4d  pDest->flags&MEM
1d216 5f 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 67 6f 74  _Null );.    got
1d217 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b  o op_column_out;
1d218 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 64  .  }.  assert( d
1d219 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
1d21a 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3e 3d  _LIMIT_LENGTH]>=
1d21b 30 20 29 3b 0a 20 20 69 66 28 20 75 2e 61 6d 2e  0 );.  if( u.am.
1d21c 70 61 79 6c 6f 61 64 53 69 7a 65 20 3e 20 28 75  payloadSize > (u
1d21d 33 32 29 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  32)db->aLimit[SQ
1d21e 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
1d21f 48 5d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74  H] ){.    goto t
1d220 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 0a 20 20 75  oo_big;.  }..  u
1d221 2e 61 6d 2e 6e 46 69 65 6c 64 20 3d 20 75 2e 61  .am.nField = u.a
1d222 6d 2e 70 43 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20  m.pC->nField;.  
1d223 61 73 73 65 72 74 28 20 75 2e 61 6d 2e 70 32 3c  assert( u.am.p2<
1d224 75 2e 61 6d 2e 6e 46 69 65 6c 64 20 29 3b 0a 0a  u.am.nField );..
1d225 20 20 2f 2a 20 52 65 61 64 20 61 6e 64 20 70 61    /* Read and pa
1d226 72 73 65 20 74 68 65 20 74 61 62 6c 65 20 68 65  rse the table he
1d227 61 64 65 72 2e 20 20 53 74 6f 72 65 20 74 68 65  ader.  Store the
1d228 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20   results of the 
1d229 70 61 72 73 65 0a 20 20 2a 2a 20 69 6e 74 6f 20  parse.  ** into 
1d22a 74 68 65 20 72 65 63 6f 72 64 20 68 65 61 64 65  the record heade
1d22b 72 20 63 61 63 68 65 20 66 69 65 6c 64 73 20 6f  r cache fields o
1d22c 66 20 74 68 65 20 63 75 72 73 6f 72 2e 0a 20 20  f the cursor..  
1d22d 2a 2f 0a 20 20 75 2e 61 6d 2e 61 54 79 70 65 20  */.  u.am.aType 
1d22e 3d 20 75 2e 61 6d 2e 70 43 2d 3e 61 54 79 70 65  = u.am.pC->aType
1d22f 3b 0a 20 20 69 66 28 20 75 2e 61 6d 2e 70 43 2d  ;.  if( u.am.pC-
1d230 3e 63 61 63 68 65 53 74 61 74 75 73 3d 3d 70 2d  >cacheStatus==p-
1d231 3e 63 61 63 68 65 43 74 72 20 29 7b 0a 20 20 20  >cacheCtr ){.   
1d232 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 20 3d 20   u.am.aOffset = 
1d233 75 2e 61 6d 2e 70 43 2d 3e 61 4f 66 66 73 65 74  u.am.pC->aOffset
1d234 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
1d235 73 73 65 72 74 28 75 2e 61 6d 2e 61 54 79 70 65  ssert(u.am.aType
1d236 29 3b 0a 20 20 20 20 75 2e 61 6d 2e 61 76 61 69  );.    u.am.avai
1d237 6c 20 3d 20 30 3b 0a 20 20 20 20 75 2e 61 6d 2e  l = 0;.    u.am.
1d238 70 43 2d 3e 61 4f 66 66 73 65 74 20 3d 20 75 2e  pC->aOffset = u.
1d239 61 6d 2e 61 4f 66 66 73 65 74 20 3d 20 26 75 2e  am.aOffset = &u.
1d23a 61 6d 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 6e 46  am.aType[u.am.nF
1d23b 69 65 6c 64 5d 3b 0a 20 20 20 20 75 2e 61 6d 2e  ield];.    u.am.
1d23c 70 43 2d 3e 70 61 79 6c 6f 61 64 53 69 7a 65 20  pC->payloadSize 
1d23d 3d 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69  = u.am.payloadSi
1d23e 7a 65 3b 0a 20 20 20 20 75 2e 61 6d 2e 70 43 2d  ze;.    u.am.pC-
1d23f 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 70  >cacheStatus = p
1d240 2d 3e 63 61 63 68 65 43 74 72 3b 0a 0a 20 20 20  ->cacheCtr;..   
1d241 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68   /* Figure out h
1d242 6f 77 20 6d 61 6e 79 20 62 79 74 65 73 20 61 72  ow many bytes ar
1d243 65 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 20  e in the header 
1d244 2a 2f 0a 20 20 20 20 69 66 28 20 75 2e 61 6d 2e  */.    if( u.am.
1d245 7a 52 65 63 20 29 7b 0a 20 20 20 20 20 20 75 2e  zRec ){.      u.
1d246 61 6d 2e 7a 44 61 74 61 20 3d 20 75 2e 61 6d 2e  am.zData = u.am.
1d247 7a 52 65 63 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  zRec;.    }else{
1d248 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61 6d 2e  .      if( u.am.
1d249 70 43 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20  pC->isIndex ){. 
1d24a 20 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74         u.am.zDat
1d24b 61 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74  a = (char*)sqlit
1d24c 65 33 42 74 72 65 65 4b 65 79 46 65 74 63 68 28  e3BtreeKeyFetch(
1d24d 75 2e 61 6d 2e 70 43 72 73 72 2c 20 26 75 2e 61  u.am.pCrsr, &u.a
1d24e 6d 2e 61 76 61 69 6c 29 3b 0a 20 20 20 20 20 20  m.avail);.      
1d24f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 75  }else{.        u
1d250 2e 61 6d 2e 7a 44 61 74 61 20 3d 20 28 63 68 61  .am.zData = (cha
1d251 72 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 44  r*)sqlite3BtreeD
1d252 61 74 61 46 65 74 63 68 28 75 2e 61 6d 2e 70 43  ataFetch(u.am.pC
1d253 72 73 72 2c 20 26 75 2e 61 6d 2e 61 76 61 69 6c  rsr, &u.am.avail
1d254 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1d255 20 2f 2a 20 49 66 20 4b 65 79 46 65 74 63 68 28   /* If KeyFetch(
1d256 29 2f 44 61 74 61 46 65 74 63 68 28 29 20 6d 61  )/DataFetch() ma
1d257 6e 61 67 65 64 20 74 6f 20 67 65 74 20 74 68 65  naged to get the
1d258 20 65 6e 74 69 72 65 20 70 61 79 6c 6f 61 64 2c   entire payload,
1d259 0a 20 20 20 20 20 20 2a 2a 20 73 61 76 65 20 74  .      ** save t
1d25a 68 65 20 70 61 79 6c 6f 61 64 20 69 6e 20 74 68  he payload in th
1d25b 65 20 75 2e 61 6d 2e 70 43 2d 3e 61 52 6f 77 20  e u.am.pC->aRow 
1d25c 63 61 63 68 65 2e 20 20 54 68 61 74 20 77 69 6c  cache.  That wil
1d25d 6c 20 73 61 76 65 20 75 73 20 66 72 6f 6d 0a 20  l save us from. 
1d25e 20 20 20 20 20 2a 2a 20 68 61 76 69 6e 67 20 74       ** having t
1d25f 6f 20 6d 61 6b 65 20 61 64 64 69 74 69 6f 6e 61  o make additiona
1d260 6c 20 63 61 6c 6c 73 20 74 6f 20 66 65 74 63 68  l calls to fetch
1d261 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 70 6f 72   the content por
1d262 74 69 6f 6e 20 6f 66 0a 20 20 20 20 20 20 2a 2a  tion of.      **
1d263 20 74 68 65 20 72 65 63 6f 72 64 2e 0a 20 20 20   the record..   
1d264 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65     */.      asse
1d265 72 74 28 20 75 2e 61 6d 2e 61 76 61 69 6c 3e 3d  rt( u.am.avail>=
1d266 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 75  0 );.      if( u
1d267 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20  .am.payloadSize 
1d268 3c 3d 20 28 75 33 32 29 75 2e 61 6d 2e 61 76 61  <= (u32)u.am.ava
1d269 69 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e  il ){.        u.
1d26a 61 6d 2e 7a 52 65 63 20 3d 20 75 2e 61 6d 2e 7a  am.zRec = u.am.z
1d26b 44 61 74 61 3b 0a 20 20 20 20 20 20 20 20 75 2e  Data;.        u.
1d26c 61 6d 2e 70 43 2d 3e 61 52 6f 77 20 3d 20 28 75  am.pC->aRow = (u
1d26d 38 2a 29 75 2e 61 6d 2e 7a 44 61 74 61 3b 0a 20  8*)u.am.zData;. 
1d26e 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1d26f 20 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 61 52 6f      u.am.pC->aRo
1d270 77 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  w = 0;.      }. 
1d271 20 20 20 7d 0a 20 20 20 20 2f 2a 20 54 68 65 20     }.    /* The 
1d272 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74  following assert
1d273 20 69 73 20 74 72 75 65 20 69 6e 20 61 6c 6c 20   is true in all 
1d274 63 61 73 65 73 20 61 63 63 65 70 74 20 77 68 65  cases accept whe
1d275 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74  n.    ** the dat
1d276 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 20 62  abase file has b
1d277 65 65 6e 20 63 6f 72 72 75 70 74 65 64 20 65 78  een corrupted ex
1d278 74 65 72 6e 61 6c 6c 79 2e 0a 20 20 20 20 2a 2a  ternally..    **
1d279 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d      assert( u.am
1d27a 2e 7a 52 65 63 21 3d 30 20 7c 7c 20 75 2e 61 6d  .zRec!=0 || u.am
1d27b 2e 61 76 61 69 6c 3e 3d 75 2e 61 6d 2e 70 61 79  .avail>=u.am.pay
1d27c 6c 6f 61 64 53 69 7a 65 20 7c 7c 20 75 2e 61 6d  loadSize || u.am
1d27d 2e 61 76 61 69 6c 3e 3d 39 20 29 3b 20 2a 2f 0a  .avail>=9 ); */.
1d27e 20 20 20 20 75 2e 61 6d 2e 73 7a 48 64 72 20 3d      u.am.szHdr =
1d27f 20 67 65 74 56 61 72 69 6e 74 33 32 28 28 75 38   getVarint32((u8
1d280 2a 29 75 2e 61 6d 2e 7a 44 61 74 61 2c 20 75 2e  *)u.am.zData, u.
1d281 61 6d 2e 6f 66 66 73 65 74 29 3b 0a 0a 20 20 20  am.offset);..   
1d282 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 20   /* Make sure a 
1d283 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65  corrupt database
1d284 20 68 61 73 20 6e 6f 74 20 67 69 76 65 6e 20 75   has not given u
1d285 73 20 61 6e 20 6f 76 65 72 73 69 7a 65 20 68 65  s an oversize he
1d286 61 64 65 72 2e 0a 20 20 20 20 2a 2a 20 44 6f 20  ader..    ** Do 
1d287 74 68 69 73 20 6e 6f 77 20 74 6f 20 61 76 6f 69  this now to avoi
1d288 64 20 61 6e 20 6f 76 65 72 73 69 7a 65 20 6d 65  d an oversize me
1d289 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e  mory allocation.
1d28a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54  .    **.    ** T
1d28b 79 70 65 20 65 6e 74 72 69 65 73 20 63 61 6e 20  ype entries can 
1d28c 62 65 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64  be between 1 and
1d28d 20 35 20 62 79 74 65 73 20 65 61 63 68 2e 20 20   5 bytes each.  
1d28e 42 75 74 20 34 20 61 6e 64 20 35 20 62 79 74 65  But 4 and 5 byte
1d28f 0a 20 20 20 20 2a 2a 20 74 79 70 65 73 20 75 73  .    ** types us
1d290 65 20 73 6f 20 6d 75 63 68 20 64 61 74 61 20 73  e so much data s
1d291 70 61 63 65 20 74 68 61 74 20 74 68 65 72 65 20  pace that there 
1d292 63 61 6e 20 6f 6e 6c 79 20 62 65 20 34 30 39 36  can only be 4096
1d293 20 61 6e 64 20 33 32 20 6f 66 0a 20 20 20 20 2a   and 32 of.    *
1d294 2a 20 74 68 65 6d 2c 20 72 65 73 70 65 63 74 69  * them, respecti
1d295 76 65 6c 79 2e 20 20 53 6f 20 74 68 65 20 6d 61  vely.  So the ma
1d296 78 69 6d 75 6d 20 68 65 61 64 65 72 20 6c 65 6e  ximum header len
1d297 67 74 68 20 72 65 73 75 6c 74 73 20 66 72 6f 6d  gth results from
1d298 20 61 0a 20 20 20 20 2a 2a 20 33 2d 62 79 74 65   a.    ** 3-byte
1d299 20 74 79 70 65 20 66 6f 72 20 65 61 63 68 20 6f   type for each o
1d29a 66 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66  f the maximum of
1d29b 20 33 32 37 36 38 20 63 6f 6c 75 6d 6e 73 20 70   32768 columns p
1d29c 6c 75 73 20 74 68 72 65 65 0a 20 20 20 20 2a 2a  lus three.    **
1d29d 20 65 78 74 72 61 20 62 79 74 65 73 20 66 6f 72   extra bytes for
1d29e 20 74 68 65 20 68 65 61 64 65 72 20 6c 65 6e 67   the header leng
1d29f 74 68 20 69 74 73 65 6c 66 2e 20 20 33 32 37 36  th itself.  3276
1d2a0 38 2a 33 20 2b 20 33 20 3d 20 39 38 33 30 37 2e  8*3 + 3 = 98307.
1d2a1 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
1d2a2 75 2e 61 6d 2e 6f 66 66 73 65 74 20 3e 20 39 38  u.am.offset > 98
1d2a3 33 30 37 20 29 7b 0a 20 20 20 20 20 20 72 63 20  307 ){.      rc 
1d2a4 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  = SQLITE_CORRUPT
1d2a5 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74  _BKPT;.      got
1d2a6 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b  o op_column_out;
1d2a7 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43  .    }..    /* C
1d2a8 6f 6d 70 75 74 65 20 69 6e 20 75 2e 61 6d 2e 6c  ompute in u.am.l
1d2a9 65 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  en the number of
1d2aa 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 77   bytes of data w
1d2ab 65 20 6e 65 65 64 20 74 6f 20 72 65 61 64 20 69  e need to read i
1d2ac 6e 20 6f 72 64 65 72 0a 20 20 20 20 2a 2a 20 74  n order.    ** t
1d2ad 6f 20 67 65 74 20 75 2e 61 6d 2e 6e 46 69 65 6c  o get u.am.nFiel
1d2ae 64 20 74 79 70 65 20 76 61 6c 75 65 73 2e 20 20  d type values.  
1d2af 75 2e 61 6d 2e 6f 66 66 73 65 74 20 69 73 20 61  u.am.offset is a
1d2b0 6e 20 75 70 70 65 72 20 62 6f 75 6e 64 20 6f 6e  n upper bound on
1d2b1 20 74 68 69 73 2e 20 20 42 75 74 0a 20 20 20 20   this.  But.    
1d2b2 2a 2a 20 75 2e 61 6d 2e 6e 46 69 65 6c 64 20 6d  ** u.am.nField m
1d2b3 69 67 68 74 20 62 65 20 73 69 67 6e 69 66 69 63  ight be signific
1d2b4 61 6e 74 6c 79 20 6c 65 73 73 20 74 68 61 6e 20  antly less than 
1d2b5 74 68 65 20 74 72 75 65 20 6e 75 6d 62 65 72 20  the true number 
1d2b6 6f 66 20 63 6f 6c 75 6d 6e 73 0a 20 20 20 20 2a  of columns.    *
1d2b7 2a 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2c 20  * in the table, 
1d2b8 61 6e 64 20 69 6e 20 74 68 61 74 20 63 61 73 65  and in that case
1d2b9 2c 20 35 2a 75 2e 61 6d 2e 6e 46 69 65 6c 64 2b  , 5*u.am.nField+
1d2ba 33 20 6d 69 67 68 74 20 62 65 20 73 6d 61 6c 6c  3 might be small
1d2bb 65 72 20 74 68 61 6e 20 75 2e 61 6d 2e 6f 66 66  er than u.am.off
1d2bc 73 65 74 2e 0a 20 20 20 20 2a 2a 20 57 65 20 77  set..    ** We w
1d2bd 61 6e 74 20 74 6f 20 6d 69 6e 69 6d 69 7a 65 20  ant to minimize 
1d2be 75 2e 61 6d 2e 6c 65 6e 20 69 6e 20 6f 72 64 65  u.am.len in orde
1d2bf 72 20 74 6f 20 6c 69 6d 69 74 20 74 68 65 20 73  r to limit the s
1d2c0 69 7a 65 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72  ize of the memor
1d2c1 79 0a 20 20 20 20 2a 2a 20 61 6c 6c 6f 63 61 74  y.    ** allocat
1d2c2 69 6f 6e 2c 20 65 73 70 65 63 69 61 6c 6c 79 20  ion, especially 
1d2c3 69 66 20 61 20 63 6f 72 72 75 70 74 20 64 61 74  if a corrupt dat
1d2c4 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 20 63  abase file has c
1d2c5 61 75 73 65 64 20 75 2e 61 6d 2e 6f 66 66 73 65  aused u.am.offse
1d2c6 74 0a 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 6f  t.    ** to be o
1d2c7 76 65 72 73 69 7a 65 64 2e 20 4f 66 66 73 65 74  versized. Offset
1d2c8 20 69 73 20 6c 69 6d 69 74 65 64 20 74 6f 20 39   is limited to 9
1d2c9 38 33 30 37 20 61 62 6f 76 65 2e 20 20 42 75 74  8307 above.  But
1d2ca 20 39 38 33 30 37 20 6d 69 67 68 74 0a 20 20 20   98307 might.   
1d2cb 20 2a 2a 20 73 74 69 6c 6c 20 65 78 63 65 65 64   ** still exceed
1d2cc 20 52 6f 62 73 6f 6e 20 6d 65 6d 6f 72 79 20 61   Robson memory a
1d2cd 6c 6c 6f 63 61 74 69 6f 6e 20 6c 69 6d 69 74 73  llocation limits
1d2ce 20 6f 6e 20 73 6f 6d 65 20 63 6f 6e 66 69 67 75   on some configu
1d2cf 72 61 74 69 6f 6e 73 2e 0a 20 20 20 20 2a 2a 20  rations..    ** 
1d2d0 4f 6e 20 73 79 73 74 65 6d 73 20 74 68 61 74 20  On systems that 
1d2d1 63 61 6e 6e 6f 74 20 74 6f 6c 65 72 61 74 65 20  cannot tolerate 
1d2d2 6c 61 72 67 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  large memory all
1d2d3 6f 63 61 74 69 6f 6e 73 2c 20 75 2e 61 6d 2e 6e  ocations, u.am.n
1d2d4 46 69 65 6c 64 2a 35 2b 33 0a 20 20 20 20 2a 2a  Field*5+3.    **
1d2d5 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 62 65 20   will likely be 
1d2d6 6d 75 63 68 20 73 6d 61 6c 6c 65 72 20 73 69 6e  much smaller sin
1d2d7 63 65 20 75 2e 61 6d 2e 6e 46 69 65 6c 64 20 77  ce u.am.nField w
1d2d8 69 6c 6c 20 6c 69 6b 65 6c 79 20 62 65 20 6c 65  ill likely be le
1d2d9 73 73 20 74 68 61 6e 0a 20 20 20 20 2a 2a 20 32  ss than.    ** 2
1d2da 30 20 6f 72 20 73 6f 2e 20 20 54 68 69 73 20 69  0 or so.  This i
1d2db 6e 73 75 72 65 73 20 74 68 61 74 20 52 6f 62 73  nsures that Robs
1d2dc 6f 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  on memory alloca
1d2dd 74 69 6f 6e 20 6c 69 6d 69 74 73 20 61 72 65 0a  tion limits are.
1d2de 20 20 20 20 2a 2a 20 6e 6f 74 20 65 78 63 65 65      ** not excee
1d2df 64 65 64 20 65 76 65 6e 20 66 6f 72 20 63 6f 72  ded even for cor
1d2e0 72 75 70 74 20 64 61 74 61 62 61 73 65 20 66 69  rupt database fi
1d2e1 6c 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  les..    */.    
1d2e2 75 2e 61 6d 2e 6c 65 6e 20 3d 20 75 2e 61 6d 2e  u.am.len = u.am.
1d2e3 6e 46 69 65 6c 64 2a 35 20 2b 20 33 3b 0a 20 20  nField*5 + 3;.  
1d2e4 20 20 69 66 28 20 75 2e 61 6d 2e 6c 65 6e 20 3e    if( u.am.len >
1d2e5 20 28 69 6e 74 29 75 2e 61 6d 2e 6f 66 66 73 65   (int)u.am.offse
1d2e6 74 20 29 20 75 2e 61 6d 2e 6c 65 6e 20 3d 20 28  t ) u.am.len = (
1d2e7 69 6e 74 29 75 2e 61 6d 2e 6f 66 66 73 65 74 3b  int)u.am.offset;
1d2e8 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 4b 65 79  ..    /* The Key
1d2e9 46 65 74 63 68 28 29 20 6f 72 20 44 61 74 61 46  Fetch() or DataF
1d2ea 65 74 63 68 28 29 20 61 62 6f 76 65 20 61 72 65  etch() above are
1d2eb 20 66 61 73 74 20 61 6e 64 20 77 69 6c 6c 20 67   fast and will g
1d2ec 65 74 20 74 68 65 20 65 6e 74 69 72 65 0a 20 20  et the entire.  
1d2ed 20 20 2a 2a 20 72 65 63 6f 72 64 20 68 65 61 64    ** record head
1d2ee 65 72 20 69 6e 20 6d 6f 73 74 20 63 61 73 65 73  er in most cases
1d2ef 2e 20 20 42 75 74 20 74 68 65 79 20 77 69 6c 6c  .  But they will
1d2f0 20 66 61 69 6c 20 74 6f 20 67 65 74 20 74 68 65   fail to get the
1d2f1 20 63 6f 6d 70 6c 65 74 65 0a 20 20 20 20 2a 2a   complete.    **
1d2f2 20 72 65 63 6f 72 64 20 68 65 61 64 65 72 20 69   record header i
1d2f3 66 20 74 68 65 20 72 65 63 6f 72 64 20 68 65 61  f the record hea
1d2f4 64 65 72 20 64 6f 65 73 20 6e 6f 74 20 66 69 74  der does not fit
1d2f5 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 70 61 67   on a single pag
1d2f6 65 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20  e.    ** in the 
1d2f7 42 2d 54 72 65 65 2e 20 20 57 68 65 6e 20 74 68  B-Tree.  When th
1d2f8 61 74 20 68 61 70 70 65 6e 73 2c 20 75 73 65 20  at happens, use 
1d2f9 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72  sqlite3VdbeMemFr
1d2fa 6f 6d 42 74 72 65 65 28 29 20 74 6f 0a 20 20 20  omBtree() to.   
1d2fb 20 2a 2a 20 61 63 71 75 69 72 65 20 74 68 65 20   ** acquire the 
1d2fc 63 6f 6d 70 6c 65 74 65 20 68 65 61 64 65 72 20  complete header 
1d2fd 74 65 78 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  text..    */.   
1d2fe 20 69 66 28 20 21 75 2e 61 6d 2e 7a 52 65 63 20   if( !u.am.zRec 
1d2ff 26 26 20 75 2e 61 6d 2e 61 76 61 69 6c 3c 75 2e  && u.am.avail<u.
1d300 61 6d 2e 6c 65 6e 20 29 7b 0a 20 20 20 20 20 20  am.len ){.      
1d301 75 2e 61 6d 2e 73 4d 65 6d 2e 66 6c 61 67 73 20  u.am.sMem.flags 
1d302 3d 20 30 3b 0a 20 20 20 20 20 20 75 2e 61 6d 2e  = 0;.      u.am.
1d303 73 4d 65 6d 2e 64 62 20 3d 20 30 3b 0a 20 20 20  sMem.db = 0;.   
1d304 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
1d305 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28  dbeMemFromBtree(
1d306 75 2e 61 6d 2e 70 43 72 73 72 2c 20 30 2c 20 75  u.am.pCrsr, 0, u
1d307 2e 61 6d 2e 6c 65 6e 2c 20 75 2e 61 6d 2e 70 43  .am.len, u.am.pC
1d308 2d 3e 69 73 49 6e 64 65 78 2c 20 26 75 2e 61 6d  ->isIndex, &u.am
1d309 2e 73 4d 65 6d 29 3b 0a 20 20 20 20 20 20 69 66  .sMem);.      if
1d30a 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1d30b 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  ){.        goto 
1d30c 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a 20  op_column_out;. 
1d30d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 75 2e 61       }.      u.a
1d30e 6d 2e 7a 44 61 74 61 20 3d 20 75 2e 61 6d 2e 73  m.zData = u.am.s
1d30f 4d 65 6d 2e 7a 3b 0a 20 20 20 20 7d 0a 20 20 20  Mem.z;.    }.   
1d310 20 75 2e 61 6d 2e 7a 45 6e 64 48 64 72 20 3d 20   u.am.zEndHdr = 
1d311 28 75 38 20 2a 29 26 75 2e 61 6d 2e 7a 44 61 74  (u8 *)&u.am.zDat
1d312 61 5b 75 2e 61 6d 2e 6c 65 6e 5d 3b 0a 20 20 20  a[u.am.len];.   
1d313 20 75 2e 61 6d 2e 7a 49 64 78 20 3d 20 28 75 38   u.am.zIdx = (u8
1d314 20 2a 29 26 75 2e 61 6d 2e 7a 44 61 74 61 5b 75   *)&u.am.zData[u
1d315 2e 61 6d 2e 73 7a 48 64 72 5d 3b 0a 0a 20 20 20  .am.szHdr];..   
1d316 20 2f 2a 20 53 63 61 6e 20 74 68 65 20 68 65 61   /* Scan the hea
1d317 64 65 72 20 61 6e 64 20 75 73 65 20 69 74 20 74  der and use it t
1d318 6f 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 75 2e  o fill in the u.
1d319 61 6d 2e 61 54 79 70 65 5b 5d 20 61 6e 64 20 75  am.aType[] and u
1d31a 2e 61 6d 2e 61 4f 66 66 73 65 74 5b 5d 0a 20 20  .am.aOffset[].  
1d31b 20 20 2a 2a 20 61 72 72 61 79 73 2e 20 20 75 2e    ** arrays.  u.
1d31c 61 6d 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 69 5d  am.aType[u.am.i]
1d31d 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 74 68   will contain th
1d31e 65 20 74 79 70 65 20 69 6e 74 65 67 65 72 20 66  e type integer f
1d31f 6f 72 20 74 68 65 20 75 2e 61 6d 2e 69 2d 74 68  or the u.am.i-th
1d320 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 61  .    ** column a
1d321 6e 64 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b  nd u.am.aOffset[
1d322 75 2e 61 6d 2e 69 5d 20 77 69 6c 6c 20 63 6f 6e  u.am.i] will con
1d323 74 61 69 6e 20 74 68 65 20 75 2e 61 6d 2e 6f 66  tain the u.am.of
1d324 66 73 65 74 20 66 72 6f 6d 20 74 68 65 20 62 65  fset from the be
1d325 67 69 6e 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 6f  ginning.    ** o
1d326 66 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20  f the record to 
1d327 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65  the start of the
1d328 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 75 2e   data for the u.
1d329 61 6d 2e 69 2d 74 68 20 63 6f 6c 75 6d 6e 0a 20  am.i-th column. 
1d32a 20 20 20 2a 2f 0a 20 20 20 20 75 2e 61 6d 2e 6f     */.    u.am.o
1d32b 66 66 73 65 74 36 34 20 3d 20 75 2e 61 6d 2e 6f  ffset64 = u.am.o
1d32c 66 66 73 65 74 3b 0a 20 20 20 20 66 6f 72 28 75  ffset;.    for(u
1d32d 2e 61 6d 2e 69 3d 30 3b 20 75 2e 61 6d 2e 69 3c  .am.i=0; u.am.i<
1d32e 75 2e 61 6d 2e 6e 46 69 65 6c 64 3b 20 75 2e 61  u.am.nField; u.a
1d32f 6d 2e 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  m.i++){.      if
1d330 28 20 75 2e 61 6d 2e 7a 49 64 78 3c 75 2e 61 6d  ( u.am.zIdx<u.am
1d331 2e 7a 45 6e 64 48 64 72 20 29 7b 0a 20 20 20 20  .zEndHdr ){.    
1d332 20 20 20 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74      u.am.aOffset
1d333 5b 75 2e 61 6d 2e 69 5d 20 3d 20 28 75 33 32 29  [u.am.i] = (u32)
1d334 75 2e 61 6d 2e 6f 66 66 73 65 74 36 34 3b 0a 20  u.am.offset64;. 
1d335 20 20 20 20 20 20 20 75 2e 61 6d 2e 7a 49 64 78         u.am.zIdx
1d336 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28   += getVarint32(
1d337 75 2e 61 6d 2e 7a 49 64 78 2c 20 75 2e 61 6d 2e  u.am.zIdx, u.am.
1d338 61 54 79 70 65 5b 75 2e 61 6d 2e 69 5d 29 3b 0a  aType[u.am.i]);.
1d339 20 20 20 20 20 20 20 20 75 2e 61 6d 2e 6f 66 66          u.am.off
1d33a 73 65 74 36 34 20 2b 3d 20 73 71 6c 69 74 65 33  set64 += sqlite3
1d33b 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65  VdbeSerialTypeLe
1d33c 6e 28 75 2e 61 6d 2e 61 54 79 70 65 5b 75 2e 61  n(u.am.aType[u.a
1d33d 6d 2e 69 5d 29 3b 0a 20 20 20 20 20 20 7d 65 6c  m.i]);.      }el
1d33e 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49  se{.        /* I
1d33f 66 20 75 2e 61 6d 2e 69 20 69 73 20 6c 65 73 73  f u.am.i is less
1d340 20 74 68 61 74 20 75 2e 61 6d 2e 6e 46 69 65 6c   that u.am.nFiel
1d341 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20 61 72  d, then there ar
1d342 65 20 6c 65 73 73 20 66 69 65 6c 64 73 20 69 6e  e less fields in
1d343 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 2a 2a   this.        **
1d344 20 72 65 63 6f 72 64 20 74 68 61 6e 20 53 65 74   record than Set
1d345 4e 75 6d 43 6f 6c 75 6d 6e 73 20 69 6e 64 69 63  NumColumns indic
1d346 61 74 65 64 20 74 68 65 72 65 20 61 72 65 20 63  ated there are c
1d347 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 0a 20 20  olumns in the.  
1d348 20 20 20 20 20 20 2a 2a 20 74 61 62 6c 65 2e 20        ** table. 
1d349 53 65 74 20 74 68 65 20 75 2e 61 6d 2e 6f 66 66  Set the u.am.off
1d34a 73 65 74 20 66 6f 72 20 61 6e 79 20 65 78 74 72  set for any extr
1d34b 61 20 63 6f 6c 75 6d 6e 73 20 6e 6f 74 20 70 72  a columns not pr
1d34c 65 73 65 6e 74 20 69 6e 0a 20 20 20 20 20 20 20  esent in.       
1d34d 20 2a 2a 20 74 68 65 20 72 65 63 6f 72 64 20 74   ** the record t
1d34e 6f 20 30 2e 20 54 68 69 73 20 74 65 6c 6c 73 20  o 0. This tells 
1d34f 63 6f 64 65 20 62 65 6c 6f 77 20 74 6f 20 73 74  code below to st
1d350 6f 72 65 20 61 20 4e 55 4c 4c 0a 20 20 20 20 20  ore a NULL.     
1d351 20 20 20 2a 2a 20 69 6e 73 74 65 61 64 20 6f 66     ** instead of
1d352 20 64 65 73 65 72 69 61 6c 69 7a 69 6e 67 20 61   deserializing a
1d353 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 20   value from the 
1d354 72 65 63 6f 72 64 2e 0a 20 20 20 20 20 20 20 20  record..        
1d355 2a 2f 0a 20 20 20 20 20 20 20 20 75 2e 61 6d 2e  */.        u.am.
1d356 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 69 5d 20  aOffset[u.am.i] 
1d357 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 0;.      }.   
1d358 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64   }.    sqlite3Vd
1d359 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 75 2e  beMemRelease(&u.
1d35a 61 6d 2e 73 4d 65 6d 29 3b 0a 20 20 20 20 75 2e  am.sMem);.    u.
1d35b 61 6d 2e 73 4d 65 6d 2e 66 6c 61 67 73 20 3d 20  am.sMem.flags = 
1d35c 4d 45 4d 5f 4e 75 6c 6c 3b 0a 0a 20 20 20 20 2f  MEM_Null;..    /
1d35d 2a 20 49 66 20 77 65 20 68 61 76 65 20 72 65 61  * If we have rea
1d35e 64 20 6d 6f 72 65 20 68 65 61 64 65 72 20 64 61  d more header da
1d35f 74 61 20 74 68 61 6e 20 77 61 73 20 63 6f 6e 74  ta than was cont
1d360 61 69 6e 65 64 20 69 6e 20 74 68 65 20 68 65 61  ained in the hea
1d361 64 65 72 2c 0a 20 20 20 20 2a 2a 20 6f 72 20 69  der,.    ** or i
1d362 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  f the end of the
1d363 20 6c 61 73 74 20 66 69 65 6c 64 20 61 70 70 65   last field appe
1d364 61 72 73 20 74 6f 20 62 65 20 70 61 73 74 20 74  ars to be past t
1d365 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 20 20  he end of the.  
1d366 20 20 2a 2a 20 72 65 63 6f 72 64 2c 20 6f 72 20    ** record, or 
1d367 69 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  if the end of th
1d368 65 20 6c 61 73 74 20 66 69 65 6c 64 20 61 70 70  e last field app
1d369 65 61 72 73 20 74 6f 20 62 65 20 62 65 66 6f 72  ears to be befor
1d36a 65 20 74 68 65 20 65 6e 64 0a 20 20 20 20 2a 2a  e the end.    **
1d36b 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 28   of the record (
1d36c 77 68 65 6e 20 61 6c 6c 20 66 69 65 6c 64 73 20  when all fields 
1d36d 70 72 65 73 65 6e 74 29 2c 20 74 68 65 6e 20 77  present), then w
1d36e 65 20 6d 75 73 74 20 62 65 20 64 65 61 6c 69 6e  e must be dealin
1d36f 67 0a 20 20 20 20 2a 2a 20 77 69 74 68 20 61 20  g.    ** with a 
1d370 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65  corrupt database
1d371 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
1d372 20 28 75 2e 61 6d 2e 7a 49 64 78 20 3e 20 75 2e   (u.am.zIdx > u.
1d373 61 6d 2e 7a 45 6e 64 48 64 72 29 7c 7c 20 28 75  am.zEndHdr)|| (u
1d374 2e 61 6d 2e 6f 66 66 73 65 74 36 34 20 3e 20 75  .am.offset64 > u
1d375 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 29  .am.payloadSize)
1d376 0a 20 20 20 20 20 7c 7c 20 28 75 2e 61 6d 2e 7a  .     || (u.am.z
1d377 49 64 78 3d 3d 75 2e 61 6d 2e 7a 45 6e 64 48 64  Idx==u.am.zEndHd
1d378 72 20 26 26 20 75 2e 61 6d 2e 6f 66 66 73 65 74  r && u.am.offset
1d379 36 34 21 3d 28 75 36 34 29 75 2e 61 6d 2e 70 61  64!=(u64)u.am.pa
1d37a 79 6c 6f 61 64 53 69 7a 65 29 20 29 7b 0a 20 20  yloadSize) ){.  
1d37b 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1d37c 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
1d37d 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75      goto op_colu
1d37e 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20  mn_out;.    }.  
1d37f 7d 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68 65 20  }..  /* Get the 
1d380 63 6f 6c 75 6d 6e 20 69 6e 66 6f 72 6d 61 74 69  column informati
1d381 6f 6e 2e 20 49 66 20 75 2e 61 6d 2e 61 4f 66 66  on. If u.am.aOff
1d382 73 65 74 5b 75 2e 61 6d 2e 70 32 5d 20 69 73 20  set[u.am.p2] is 
1d383 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 0a 20  non-zero, then. 
1d384 20 2a 2a 20 64 65 73 65 72 69 61 6c 69 7a 65 20   ** deserialize 
1d385 74 68 65 20 76 61 6c 75 65 20 66 72 6f 6d 20 74  the value from t
1d386 68 65 20 72 65 63 6f 72 64 2e 20 49 66 20 75 2e  he record. If u.
1d387 61 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e  am.aOffset[u.am.
1d388 70 32 5d 20 69 73 20 7a 65 72 6f 2c 0a 20 20 2a  p2] is zero,.  *
1d389 2a 20 74 68 65 6e 20 74 68 65 72 65 20 61 72 65  * then there are
1d38a 20 6e 6f 74 20 65 6e 6f 75 67 68 20 66 69 65 6c   not enough fiel
1d38b 64 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64  ds in the record
1d38c 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 0a   to satisfy the.
1d38d 20 20 2a 2a 20 72 65 71 75 65 73 74 2e 20 20 49    ** request.  I
1d38e 6e 20 74 68 69 73 20 63 61 73 65 2c 20 73 65 74  n this case, set
1d38f 20 74 68 65 20 76 61 6c 75 65 20 4e 55 4c 4c 20   the value NULL 
1d390 6f 72 20 74 6f 20 50 34 20 69 66 20 50 34 20 69  or to P4 if P4 i
1d391 73 0a 20 20 2a 2a 20 61 20 70 6f 69 6e 74 65 72  s.  ** a pointer
1d392 20 74 6f 20 61 20 4d 65 6d 20 6f 62 6a 65 63 74   to a Mem object
1d393 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 75 2e 61  ..  */.  if( u.a
1d394 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 70  m.aOffset[u.am.p
1d395 32 5d 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  2] ){.    assert
1d396 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1d397 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 6d 2e  );.    if( u.am.
1d398 7a 52 65 63 20 29 7b 0a 20 20 20 20 20 20 73 71  zRec ){.      sq
1d399 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
1d39a 61 73 65 45 78 74 65 72 6e 61 6c 28 75 2e 61 6d  aseExternal(u.am
1d39b 2e 70 44 65 73 74 29 3b 0a 20 20 20 20 20 20 73  .pDest);.      s
1d39c 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
1d39d 47 65 74 28 28 75 38 20 2a 29 26 75 2e 61 6d 2e  Get((u8 *)&u.am.
1d39e 7a 52 65 63 5b 75 2e 61 6d 2e 61 4f 66 66 73 65  zRec[u.am.aOffse
1d39f 74 5b 75 2e 61 6d 2e 70 32 5d 5d 2c 20 75 2e 61  t[u.am.p2]], u.a
1d3a0 6d 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 70 32 5d  m.aType[u.am.p2]
1d3a1 2c 20 75 2e 61 6d 2e 70 44 65 73 74 29 3b 0a 20  , u.am.pDest);. 
1d3a2 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1d3a3 75 2e 61 6d 2e 6c 65 6e 20 3d 20 73 71 6c 69 74  u.am.len = sqlit
1d3a4 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65  e3VdbeSerialType
1d3a5 4c 65 6e 28 75 2e 61 6d 2e 61 54 79 70 65 5b 75  Len(u.am.aType[u
1d3a6 2e 61 6d 2e 70 32 5d 29 3b 0a 20 20 20 20 20 20  .am.p2]);.      
1d3a7 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f  sqlite3VdbeMemMo
1d3a8 76 65 28 26 75 2e 61 6d 2e 73 4d 65 6d 2c 20 75  ve(&u.am.sMem, u
1d3a9 2e 61 6d 2e 70 44 65 73 74 29 3b 0a 20 20 20 20  .am.pDest);.    
1d3aa 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
1d3ab 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 75  beMemFromBtree(u
1d3ac 2e 61 6d 2e 70 43 72 73 72 2c 20 75 2e 61 6d 2e  .am.pCrsr, u.am.
1d3ad 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 70 32 5d  aOffset[u.am.p2]
1d3ae 2c 20 75 2e 61 6d 2e 6c 65 6e 2c 20 75 2e 61 6d  , u.am.len, u.am
1d3af 2e 70 43 2d 3e 69 73 49 6e 64 65 78 2c 20 26 75  .pC->isIndex, &u
1d3b0 2e 61 6d 2e 73 4d 65 6d 29 3b 0a 20 20 20 20 20  .am.sMem);.     
1d3b1 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1d3b2 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f  OK ){.        go
1d3b3 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74  to op_column_out
1d3b4 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1d3b5 75 2e 61 6d 2e 7a 44 61 74 61 20 3d 20 75 2e 61  u.am.zData = u.a
1d3b6 6d 2e 73 4d 65 6d 2e 7a 3b 0a 20 20 20 20 20 20  m.sMem.z;.      
1d3b7 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
1d3b8 6c 47 65 74 28 28 75 38 2a 29 75 2e 61 6d 2e 7a  lGet((u8*)u.am.z
1d3b9 44 61 74 61 2c 20 75 2e 61 6d 2e 61 54 79 70 65  Data, u.am.aType
1d3ba 5b 75 2e 61 6d 2e 70 32 5d 2c 20 75 2e 61 6d 2e  [u.am.p2], u.am.
1d3bb 70 44 65 73 74 29 3b 0a 20 20 20 20 7d 0a 20 20  pDest);.    }.  
1d3bc 20 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 65 6e    u.am.pDest->en
1d3bd 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20  c = encoding;.  
1d3be 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 70  }else{.    if( p
1d3bf 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 4d  Op->p4type==P4_M
1d3c0 45 4d 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  EM ){.      sqli
1d3c1 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f  te3VdbeMemShallo
1d3c2 77 43 6f 70 79 28 75 2e 61 6d 2e 70 44 65 73 74  wCopy(u.am.pDest
1d3c3 2c 20 70 4f 70 2d 3e 70 34 2e 70 4d 65 6d 2c 20  , pOp->p4.pMem, 
1d3c4 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20 20  MEM_Static);.   
1d3c5 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73   }else{.      as
1d3c6 73 65 72 74 28 20 75 2e 61 6d 2e 70 44 65 73 74  sert( u.am.pDest
1d3c7 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c  ->flags&MEM_Null
1d3c8 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20   );.    }.  }.. 
1d3c9 20 2f 2a 20 49 66 20 77 65 20 64 79 6e 61 6d 69   /* If we dynami
1d3ca 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20  cally allocated 
1d3cb 73 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68  space to hold th
1d3cc 65 20 64 61 74 61 20 28 69 6e 20 74 68 65 0a 20  e data (in the. 
1d3cd 20 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 4d   ** sqlite3VdbeM
1d3ce 65 6d 46 72 6f 6d 42 74 72 65 65 28 29 20 63 61  emFromBtree() ca
1d3cf 6c 6c 20 61 62 6f 76 65 29 20 74 68 65 6e 20 74  ll above) then t
1d3d0 72 61 6e 73 66 65 72 20 63 6f 6e 74 72 6f 6c 20  ransfer control 
1d3d1 6f 66 20 74 68 61 74 0a 20 20 2a 2a 20 64 79 6e  of that.  ** dyn
1d3d2 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74  amically allocat
1d3d3 65 64 20 73 70 61 63 65 20 6f 76 65 72 20 74 6f  ed space over to
1d3d4 20 74 68 65 20 75 2e 61 6d 2e 70 44 65 73 74 20   the u.am.pDest 
1d3d5 73 74 72 75 63 74 75 72 65 2e 0a 20 20 2a 2a 20  structure..  ** 
1d3d6 54 68 69 73 20 70 72 65 76 65 6e 74 73 20 61 20  This prevents a 
1d3d7 6d 65 6d 6f 72 79 20 63 6f 70 79 2e 0a 20 20 2a  memory copy..  *
1d3d8 2f 0a 20 20 69 66 28 20 75 2e 61 6d 2e 73 4d 65  /.  if( u.am.sMe
1d3d9 6d 2e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20  m.zMalloc ){.   
1d3da 20 61 73 73 65 72 74 28 20 75 2e 61 6d 2e 73 4d   assert( u.am.sM
1d3db 65 6d 2e 7a 3d 3d 75 2e 61 6d 2e 73 4d 65 6d 2e  em.z==u.am.sMem.
1d3dc 7a 4d 61 6c 6c 6f 63 20 29 3b 0a 20 20 20 20 61  zMalloc );.    a
1d3dd 73 73 65 72 74 28 20 21 28 75 2e 61 6d 2e 70 44  ssert( !(u.am.pD
1d3de 65 73 74 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  est->flags & MEM
1d3df 5f 44 79 6e 29 20 29 3b 0a 20 20 20 20 61 73 73  _Dyn) );.    ass
1d3e0 65 72 74 28 20 21 28 75 2e 61 6d 2e 70 44 65 73  ert( !(u.am.pDes
1d3e1 74 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f  t->flags & (MEM_
1d3e2 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 29 20 7c  Blob|MEM_Str)) |
1d3e3 7c 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 7a 3d  | u.am.pDest->z=
1d3e4 3d 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 20 29 3b 0a  =u.am.sMem.z );.
1d3e5 20 20 20 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e      u.am.pDest->
1d3e6 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 45  flags &= ~(MEM_E
1d3e7 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 69 63 29  phem|MEM_Static)
1d3e8 3b 0a 20 20 20 20 75 2e 61 6d 2e 70 44 65 73 74  ;.    u.am.pDest
1d3e9 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54  ->flags |= MEM_T
1d3ea 65 72 6d 3b 0a 20 20 20 20 75 2e 61 6d 2e 70 44  erm;.    u.am.pD
1d3eb 65 73 74 2d 3e 7a 20 3d 20 75 2e 61 6d 2e 73 4d  est->z = u.am.sM
1d3ec 65 6d 2e 7a 3b 0a 20 20 20 20 75 2e 61 6d 2e 70  em.z;.    u.am.p
1d3ed 44 65 73 74 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20  Dest->zMalloc = 
1d3ee 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f  u.am.sMem.zMallo
1d3ef 63 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73  c;.  }..  rc = s
1d3f0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b  qlite3VdbeMemMak
1d3f1 65 57 72 69 74 65 61 62 6c 65 28 75 2e 61 6d 2e  eWriteable(u.am.
1d3f2 70 44 65 73 74 29 3b 0a 0a 6f 70 5f 63 6f 6c 75  pDest);..op_colu
1d3f3 6d 6e 5f 6f 75 74 3a 0a 20 20 55 50 44 41 54 45  mn_out:.  UPDATE
1d3f4 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 75 2e  _MAX_BLOBSIZE(u.
1d3f5 61 6d 2e 70 44 65 73 74 29 3b 0a 20 20 52 45 47  am.pDest);.  REG
1d3f6 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d  ISTER_TRACE(pOp-
1d3f7 3e 70 33 2c 20 75 2e 61 6d 2e 70 44 65 73 74 29  >p3, u.am.pDest)
1d3f8 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
1d3f9 20 4f 70 63 6f 64 65 3a 20 41 66 66 69 6e 69 74   Opcode: Affinit
1d3fa 79 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a  y P1 P2 * P4 *.*
1d3fb 2a 0a 2a 2a 20 41 70 70 6c 79 20 61 66 66 69 6e  *.** Apply affin
1d3fc 69 74 69 65 73 20 74 6f 20 61 20 72 61 6e 67 65  ities to a range
1d3fd 20 6f 66 20 50 32 20 72 65 67 69 73 74 65 72 73   of P2 registers
1d3fe 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 50   starting with P
1d3ff 31 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61  1..**.** P4 is a
1d400 20 73 74 72 69 6e 67 20 74 68 61 74 20 69 73 20   string that is 
1d401 50 32 20 63 68 61 72 61 63 74 65 72 73 20 6c 6f  P2 characters lo
1d402 6e 67 2e 20 54 68 65 20 6e 74 68 20 63 68 61 72  ng. The nth char
1d403 61 63 74 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20  acter of the.** 
1d404 73 74 72 69 6e 67 20 69 6e 64 69 63 61 74 65 73  string indicates
1d405 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69   the column affi
1d406 6e 69 74 79 20 74 68 61 74 20 73 68 6f 75 6c 64  nity that should
1d407 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 65   be used for the
1d408 20 6e 74 68 0a 2a 2a 20 6d 65 6d 6f 72 79 20 63   nth.** memory c
1d409 65 6c 6c 20 69 6e 20 74 68 65 20 72 61 6e 67 65  ell in the range
1d40a 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 66 66  ..*/.case OP_Aff
1d40b 69 6e 69 74 79 3a 20 7b 0a 23 69 66 20 30 20 20  inity: {.#if 0  
1d40c 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1d40d 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1d40e 61 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 41  an */.  char *zA
1d40f 66 66 69 6e 69 74 79 3b 20 20 20 2f 2a 20 54 68  ffinity;   /* Th
1d410 65 20 61 66 66 69 6e 69 74 79 20 74 6f 20 62 65  e affinity to be
1d411 20 61 70 70 6c 69 65 64 20 2a 2f 0a 20 20 4d 65   applied */.  Me
1d412 6d 20 2a 70 44 61 74 61 30 3b 20 20 20 20 20 20  m *pData0;      
1d413 20 2f 2a 20 46 69 72 73 74 20 72 65 67 69 73 74   /* First regist
1d414 65 72 20 74 6f 20 77 68 69 63 68 20 74 6f 20 61  er to which to a
1d415 70 70 6c 79 20 61 66 66 69 6e 69 74 79 20 2a 2f  pply affinity */
1d416 0a 20 20 4d 65 6d 20 2a 70 4c 61 73 74 3b 20 20  .  Mem *pLast;  
1d417 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 72 65        /* Last re
1d418 67 69 73 74 65 72 20 74 6f 20 77 68 69 63 68 20  gister to which 
1d419 74 6f 20 61 70 70 6c 79 20 61 66 66 69 6e 69 74  to apply affinit
1d41a 79 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 65 63  y */.  Mem *pRec
1d41b 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72  ;         /* Cur
1d41c 72 65 6e 74 20 72 65 67 69 73 74 65 72 20 2a 2f  rent register */
1d41d 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1d41e 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1d41f 20 69 6e 74 6f 20 75 2e 61 6e 20 2a 2f 0a 0a 20   into u.an */.. 
1d420 20 75 2e 61 6e 2e 7a 41 66 66 69 6e 69 74 79 20   u.an.zAffinity 
1d421 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 75  = pOp->p4.z;.  u
1d422 2e 61 6e 2e 70 44 61 74 61 30 20 3d 20 26 70 2d  .an.pData0 = &p-
1d423 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  >aMem[pOp->p1];.
1d424 20 20 75 2e 61 6e 2e 70 4c 61 73 74 20 3d 20 26    u.an.pLast = &
1d425 75 2e 61 6e 2e 70 44 61 74 61 30 5b 70 4f 70 2d  u.an.pData0[pOp-
1d426 3e 70 32 2d 31 5d 3b 0a 20 20 66 6f 72 28 75 2e  >p2-1];.  for(u.
1d427 61 6e 2e 70 52 65 63 3d 75 2e 61 6e 2e 70 44 61  an.pRec=u.an.pDa
1d428 74 61 30 3b 20 75 2e 61 6e 2e 70 52 65 63 3c 3d  ta0; u.an.pRec<=
1d429 75 2e 61 6e 2e 70 4c 61 73 74 3b 20 75 2e 61 6e  u.an.pLast; u.an
1d42a 2e 70 52 65 63 2b 2b 29 7b 0a 20 20 20 20 45 78  .pRec++){.    Ex
1d42b 70 61 6e 64 42 6c 6f 62 28 75 2e 61 6e 2e 70 52  pandBlob(u.an.pR
1d42c 65 63 29 3b 0a 20 20 20 20 61 70 70 6c 79 41 66  ec);.    applyAf
1d42d 66 69 6e 69 74 79 28 75 2e 61 6e 2e 70 52 65 63  finity(u.an.pRec
1d42e 2c 20 75 2e 61 6e 2e 7a 41 66 66 69 6e 69 74 79  , u.an.zAffinity
1d42f 5b 75 2e 61 6e 2e 70 52 65 63 2d 75 2e 61 6e 2e  [u.an.pRec-u.an.
1d430 70 44 61 74 61 30 5d 2c 20 65 6e 63 6f 64 69 6e  pData0], encodin
1d431 67 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  g);.  }.  break;
1d432 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d  .}../* Opcode: M
1d433 61 6b 65 52 65 63 6f 72 64 20 50 31 20 50 32 20  akeRecord P1 P2 
1d434 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f  P3 P4 *.**.** Co
1d435 6e 76 65 72 74 20 50 32 20 72 65 67 69 73 74 65  nvert P2 registe
1d436 72 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74  rs beginning wit
1d437 68 20 50 31 20 69 6e 74 6f 20 61 20 73 69 6e 67  h P1 into a sing
1d438 6c 65 20 65 6e 74 72 79 0a 2a 2a 20 73 75 69 74  le entry.** suit
1d439 61 62 6c 65 20 66 6f 72 20 75 73 65 20 61 73 20  able for use as 
1d43a 61 20 64 61 74 61 20 72 65 63 6f 72 64 20 69 6e  a data record in
1d43b 20 61 20 64 61 74 61 62 61 73 65 20 74 61 62 6c   a database tabl
1d43c 65 20 6f 72 20 61 73 20 61 20 6b 65 79 0a 2a 2a  e or as a key.**
1d43d 20 69 6e 20 61 6e 20 69 6e 64 65 78 2e 20 20 54   in an index.  T
1d43e 68 65 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68  he details of th
1d43f 65 20 66 6f 72 6d 61 74 20 61 72 65 20 69 72 72  e format are irr
1d440 65 6c 65 76 61 6e 74 20 61 73 20 6c 6f 6e 67 20  elevant as long 
1d441 61 73 0a 2a 2a 20 74 68 65 20 4f 50 5f 43 6f 6c  as.** the OP_Col
1d442 75 6d 6e 20 6f 70 63 6f 64 65 20 63 61 6e 20 64  umn opcode can d
1d443 65 63 6f 64 65 20 74 68 65 20 72 65 63 6f 72 64  ecode the record
1d444 20 6c 61 74 65 72 2e 0a 2a 2a 20 52 65 66 65 72   later..** Refer
1d445 20 74 6f 20 73 6f 75 72 63 65 20 63 6f 64 65 20   to source code 
1d446 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 74 68 65  comments for the
1d447 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68 65 20   details of the 
1d448 72 65 63 6f 72 64 0a 2a 2a 20 66 6f 72 6d 61 74  record.** format
1d449 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 6d 61 79 20 62  ..**.** P4 may b
1d44a 65 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20  e a string that 
1d44b 69 73 20 50 32 20 63 68 61 72 61 63 74 65 72 73  is P2 characters
1d44c 20 6c 6f 6e 67 2e 20 20 54 68 65 20 6e 74 68 20   long.  The nth 
1d44d 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 68 65  character of the
1d44e 0a 2a 2a 20 73 74 72 69 6e 67 20 69 6e 64 69 63  .** string indic
1d44f 61 74 65 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20  ates the column 
1d450 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 68  affinity that sh
1d451 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72  ould be used for
1d452 20 74 68 65 20 6e 74 68 0a 2a 2a 20 66 69 65 6c   the nth.** fiel
1d453 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 6b  d of the index k
1d454 65 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61  ey..**.** The ma
1d455 70 70 69 6e 67 20 66 72 6f 6d 20 63 68 61 72 61  pping from chara
1d456 63 74 65 72 20 74 6f 20 61 66 66 69 6e 69 74 79  cter to affinity
1d457 20 69 73 20 67 69 76 65 6e 20 62 79 20 74 68 65   is given by the
1d458 20 53 51 4c 49 54 45 5f 41 46 46 5f 0a 2a 2a 20   SQLITE_AFF_.** 
1d459 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 64 20 69  macros defined i
1d45a 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 2e 0a 2a  n sqliteInt.h..*
1d45b 2a 0a 2a 2a 20 49 66 20 50 34 20 69 73 20 4e 55  *.** If P4 is NU
1d45c 4c 4c 20 74 68 65 6e 20 61 6c 6c 20 69 6e 64 65  LL then all inde
1d45d 78 20 66 69 65 6c 64 73 20 68 61 76 65 20 74 68  x fields have th
1d45e 65 20 61 66 66 69 6e 69 74 79 20 4e 4f 4e 45 2e  e affinity NONE.
1d45f 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 61 6b 65  .*/.case OP_Make
1d460 52 65 63 6f 72 64 3a 20 7b 0a 23 69 66 20 30 20  Record: {.#if 0 
1d461 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1d462 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1d463 2e 61 6f 20 2a 2f 0a 20 20 75 38 20 2a 7a 4e 65  .ao */.  u8 *zNe
1d464 77 52 65 63 6f 72 64 3b 20 20 20 20 20 20 20 20  wRecord;        
1d465 2f 2a 20 41 20 62 75 66 66 65 72 20 74 6f 20 68  /* A buffer to h
1d466 6f 6c 64 20 74 68 65 20 64 61 74 61 20 66 6f 72  old the data for
1d467 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20   the new record 
1d468 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 65 63 3b 20  */.  Mem *pRec; 
1d469 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1d46a 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f  he new record */
1d46b 0a 20 20 75 36 34 20 6e 44 61 74 61 3b 20 20 20  .  u64 nData;   
1d46c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
1d46d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20  ber of bytes of 
1d46e 64 61 74 61 20 73 70 61 63 65 20 2a 2f 0a 20 20  data space */.  
1d46f 69 6e 74 20 6e 48 64 72 3b 20 20 20 20 20 20 20  int nHdr;       
1d470 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1d471 20 6f 66 20 62 79 74 65 73 20 6f 66 20 68 65 61   of bytes of hea
1d472 64 65 72 20 73 70 61 63 65 20 2a 2f 0a 20 20 69  der space */.  i
1d473 36 34 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20  64 nByte;       
1d474 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 73 70        /* Data sp
1d475 61 63 65 20 72 65 71 75 69 72 65 64 20 66 6f 72  ace required for
1d476 20 74 68 69 73 20 72 65 63 6f 72 64 20 2a 2f 0a   this record */.
1d477 20 20 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20    int nZero;    
1d478 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
1d479 65 72 20 6f 66 20 7a 65 72 6f 20 62 79 74 65 73  er of zero bytes
1d47a 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74   at the end of t
1d47b 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69  he record */.  i
1d47c 6e 74 20 6e 56 61 72 69 6e 74 3b 20 20 20 20 20  nt nVarint;     
1d47d 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1d47e 6f 66 20 62 79 74 65 73 20 69 6e 20 61 20 76 61  of bytes in a va
1d47f 72 69 6e 74 20 2a 2f 0a 20 20 75 33 32 20 73 65  rint */.  u32 se
1d480 72 69 61 6c 5f 74 79 70 65 3b 20 20 20 20 20 20  rial_type;      
1d481 20 2f 2a 20 54 79 70 65 20 66 69 65 6c 64 20 2a   /* Type field *
1d482 2f 0a 20 20 4d 65 6d 20 2a 70 44 61 74 61 30 3b  /.  Mem *pData0;
1d483 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
1d484 72 73 74 20 66 69 65 6c 64 20 74 6f 20 62 65 20  rst field to be 
1d485 63 6f 6d 62 69 6e 65 64 20 69 6e 74 6f 20 74 68  combined into th
1d486 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 4d 65  e record */.  Me
1d487 6d 20 2a 70 4c 61 73 74 3b 20 20 20 20 20 20 20  m *pLast;       
1d488 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 66 69 65       /* Last fie
1d489 6c 64 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64  ld of the record
1d48a 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64   */.  int nField
1d48b 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
1d48c 4e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73  Number of fields
1d48d 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a   in the record *
1d48e 2f 0a 20 20 63 68 61 72 20 2a 7a 41 66 66 69 6e  /.  char *zAffin
1d48f 69 74 79 3b 20 20 20 20 20 20 20 2f 2a 20 54 68  ity;       /* Th
1d490 65 20 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e  e affinity strin
1d491 67 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64  g for the record
1d492 20 2a 2f 0a 20 20 69 6e 74 20 66 69 6c 65 5f 66   */.  int file_f
1d493 6f 72 6d 61 74 3b 20 20 20 20 20 20 20 2f 2a 20  ormat;       /* 
1d494 46 69 6c 65 20 66 6f 72 6d 61 74 20 74 6f 20 75  File format to u
1d495 73 65 20 66 6f 72 20 65 6e 63 6f 64 69 6e 67 20  se for encoding 
1d496 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  */.  int i;     
1d497 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
1d498 70 61 63 65 20 75 73 65 64 20 69 6e 20 7a 4e 65  pace used in zNe
1d499 77 52 65 63 6f 72 64 5b 5d 20 2a 2f 0a 20 20 69  wRecord[] */.  i
1d49a 6e 74 20 6c 65 6e 3b 20 20 20 20 20 20 20 20 20  nt len;         
1d49b 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20        /* Length 
1d49c 6f 66 20 61 20 66 69 65 6c 64 20 2a 2f 0a 23 65  of a field */.#e
1d49d 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1d49e 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1d49f 74 6f 20 75 2e 61 6f 20 2a 2f 0a 0a 20 20 2f 2a  to u.ao */..  /*
1d4a0 20 41 73 73 75 6d 69 6e 67 20 74 68 65 20 72 65   Assuming the re
1d4a1 63 6f 72 64 20 63 6f 6e 74 61 69 6e 73 20 4e 20  cord contains N 
1d4a2 66 69 65 6c 64 73 2c 20 74 68 65 20 72 65 63 6f  fields, the reco
1d4a3 72 64 20 66 6f 72 6d 61 74 20 6c 6f 6f 6b 73 0a  rd format looks.
1d4a4 20 20 2a 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a    ** like this:.
1d4a5 20 20 2a 2a 0a 20 20 2a 2a 20 2d 2d 2d 2d 2d 2d    **.  ** ------
1d4a6 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d4a7 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d4a8 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d4a9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d4aa 2d 2d 0a 20 20 2a 2a 20 7c 20 68 64 72 2d 73 69  --.  ** | hdr-si
1d4ab 7a 65 20 7c 20 74 79 70 65 20 30 20 7c 20 74 79  ze | type 0 | ty
1d4ac 70 65 20 31 20 7c 20 2e 2e 2e 20 7c 20 74 79 70  pe 1 | ... | typ
1d4ad 65 20 4e 2d 31 20 7c 20 64 61 74 61 30 20 7c 20  e N-1 | data0 | 
1d4ae 2e 2e 2e 20 7c 20 64 61 74 61 20 4e 2d 31 20 7c  ... | data N-1 |
1d4af 0a 20 20 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .  ** ----------
1d4b0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d4b1 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d4b2 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d4b3 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20  --------------. 
1d4b4 20 2a 2a 0a 20 20 2a 2a 20 44 61 74 61 28 30 29   **.  ** Data(0)
1d4b5 20 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 72   is taken from r
1d4b6 65 67 69 73 74 65 72 20 50 31 2e 20 20 44 61 74  egister P1.  Dat
1d4b7 61 28 31 29 20 63 6f 6d 65 73 20 66 72 6f 6d 20  a(1) comes from 
1d4b8 72 65 67 69 73 74 65 72 20 50 31 2b 31 0a 20 20  register P1+1.  
1d4b9 2a 2a 20 61 6e 64 20 73 6f 20 66 72 6f 74 68 2e  ** and so froth.
1d4ba 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 61 63 68 20  .  **.  ** Each 
1d4bb 74 79 70 65 20 66 69 65 6c 64 20 69 73 20 61 20  type field is a 
1d4bc 76 61 72 69 6e 74 20 72 65 70 72 65 73 65 6e 74  varint represent
1d4bd 69 6e 67 20 74 68 65 20 73 65 72 69 61 6c 20 74  ing the serial t
1d4be 79 70 65 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20  ype of the.  ** 
1d4bf 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 64 61  corresponding da
1d4c0 74 61 20 65 6c 65 6d 65 6e 74 20 28 73 65 65 20  ta element (see 
1d4c1 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
1d4c2 6c 54 79 70 65 28 29 29 2e 20 54 68 65 0a 20 20  lType()). The.  
1d4c3 2a 2a 20 68 64 72 2d 73 69 7a 65 20 66 69 65 6c  ** hdr-size fiel
1d4c4 64 20 69 73 20 61 6c 73 6f 20 61 20 76 61 72 69  d is also a vari
1d4c5 6e 74 20 77 68 69 63 68 20 69 73 20 74 68 65 20  nt which is the 
1d4c6 6f 66 66 73 65 74 20 66 72 6f 6d 20 74 68 65 20  offset from the 
1d4c7 62 65 67 69 6e 6e 69 6e 67 0a 20 20 2a 2a 20 6f  beginning.  ** o
1d4c8 66 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20  f the record to 
1d4c9 64 61 74 61 30 2e 0a 20 20 2a 2f 0a 20 20 75 2e  data0..  */.  u.
1d4ca 61 6f 2e 6e 44 61 74 61 20 3d 20 30 3b 20 20 20  ao.nData = 0;   
1d4cb 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1d4cc 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61  of bytes of data
1d4cd 20 73 70 61 63 65 20 2a 2f 0a 20 20 75 2e 61 6f   space */.  u.ao
1d4ce 2e 6e 48 64 72 20 3d 20 30 3b 20 20 20 20 20 20  .nHdr = 0;      
1d4cf 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1d4d0 20 62 79 74 65 73 20 6f 66 20 68 65 61 64 65 72   bytes of header
1d4d1 20 73 70 61 63 65 20 2a 2f 0a 20 20 75 2e 61 6f   space */.  u.ao
1d4d2 2e 6e 42 79 74 65 20 3d 20 30 3b 20 20 20 20 20  .nByte = 0;     
1d4d3 20 20 20 20 2f 2a 20 44 61 74 61 20 73 70 61 63      /* Data spac
1d4d4 65 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 74  e required for t
1d4d5 68 69 73 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  his record */.  
1d4d6 75 2e 61 6f 2e 6e 5a 65 72 6f 20 3d 20 30 3b 20  u.ao.nZero = 0; 
1d4d7 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
1d4d8 72 20 6f 66 20 7a 65 72 6f 20 62 79 74 65 73 20  r of zero bytes 
1d4d9 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  at the end of th
1d4da 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 2e  e record */.  u.
1d4db 61 6f 2e 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d  ao.nField = pOp-
1d4dc 3e 70 31 3b 0a 20 20 75 2e 61 6f 2e 7a 41 66 66  >p1;.  u.ao.zAff
1d4dd 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e 70 34 2e  inity = pOp->p4.
1d4de 7a 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61  z;.  assert( u.a
1d4df 6f 2e 6e 46 69 65 6c 64 3e 30 20 26 26 20 70 4f  o.nField>0 && pO
1d4e0 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e  p->p2>0 && pOp->
1d4e1 70 32 2b 75 2e 61 6f 2e 6e 46 69 65 6c 64 3c 3d  p2+u.ao.nField<=
1d4e2 70 2d 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 20 20 75  p->nMem+1 );.  u
1d4e3 2e 61 6f 2e 70 44 61 74 61 30 20 3d 20 26 70 2d  .ao.pData0 = &p-
1d4e4 3e 61 4d 65 6d 5b 75 2e 61 6f 2e 6e 46 69 65 6c  >aMem[u.ao.nFiel
1d4e5 64 5d 3b 0a 20 20 75 2e 61 6f 2e 6e 46 69 65 6c  d];.  u.ao.nFiel
1d4e6 64 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 75  d = pOp->p2;.  u
1d4e7 2e 61 6f 2e 70 4c 61 73 74 20 3d 20 26 75 2e 61  .ao.pLast = &u.a
1d4e8 6f 2e 70 44 61 74 61 30 5b 75 2e 61 6f 2e 6e 46  o.pData0[u.ao.nF
1d4e9 69 65 6c 64 2d 31 5d 3b 0a 20 20 75 2e 61 6f 2e  ield-1];.  u.ao.
1d4ea 66 69 6c 65 5f 66 6f 72 6d 61 74 20 3d 20 70 2d  file_format = p-
1d4eb 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72  >minWriteFileFor
1d4ec 6d 61 74 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 70 20  mat;..  /* Loop 
1d4ed 74 68 72 6f 75 67 68 20 74 68 65 20 65 6c 65 6d  through the elem
1d4ee 65 6e 74 73 20 74 68 61 74 20 77 69 6c 6c 20 6d  ents that will m
1d4ef 61 6b 65 20 75 70 20 74 68 65 20 72 65 63 6f 72  ake up the recor
1d4f0 64 20 74 6f 20 66 69 67 75 72 65 0a 20 20 2a 2a  d to figure.  **
1d4f1 20 6f 75 74 20 68 6f 77 20 6d 75 63 68 20 73 70   out how much sp
1d4f2 61 63 65 20 69 73 20 72 65 71 75 69 72 65 64 20  ace is required 
1d4f3 66 6f 72 20 74 68 65 20 6e 65 77 20 72 65 63 6f  for the new reco
1d4f4 72 64 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 75  rd..  */.  for(u
1d4f5 2e 61 6f 2e 70 52 65 63 3d 75 2e 61 6f 2e 70 44  .ao.pRec=u.ao.pD
1d4f6 61 74 61 30 3b 20 75 2e 61 6f 2e 70 52 65 63 3c  ata0; u.ao.pRec<
1d4f7 3d 75 2e 61 6f 2e 70 4c 61 73 74 3b 20 75 2e 61  =u.ao.pLast; u.a
1d4f8 6f 2e 70 52 65 63 2b 2b 29 7b 0a 20 20 20 20 69  o.pRec++){.    i
1d4f9 66 28 20 75 2e 61 6f 2e 7a 41 66 66 69 6e 69 74  f( u.ao.zAffinit
1d4fa 79 20 29 7b 0a 20 20 20 20 20 20 61 70 70 6c 79  y ){.      apply
1d4fb 41 66 66 69 6e 69 74 79 28 75 2e 61 6f 2e 70 52  Affinity(u.ao.pR
1d4fc 65 63 2c 20 75 2e 61 6f 2e 7a 41 66 66 69 6e 69  ec, u.ao.zAffini
1d4fd 74 79 5b 75 2e 61 6f 2e 70 52 65 63 2d 75 2e 61  ty[u.ao.pRec-u.a
1d4fe 6f 2e 70 44 61 74 61 30 5d 2c 20 65 6e 63 6f 64  o.pData0], encod
1d4ff 69 6e 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ing);.    }.    
1d500 69 66 28 20 75 2e 61 6f 2e 70 52 65 63 2d 3e 66  if( u.ao.pRec->f
1d501 6c 61 67 73 26 4d 45 4d 5f 5a 65 72 6f 20 26 26  lags&MEM_Zero &&
1d502 20 75 2e 61 6f 2e 70 52 65 63 2d 3e 6e 3e 30 20   u.ao.pRec->n>0 
1d503 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1d504 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f  VdbeMemExpandBlo
1d505 62 28 75 2e 61 6f 2e 70 52 65 63 29 3b 0a 20 20  b(u.ao.pRec);.  
1d506 20 20 7d 0a 20 20 20 20 75 2e 61 6f 2e 73 65 72    }.    u.ao.ser
1d507 69 61 6c 5f 74 79 70 65 20 3d 20 73 71 6c 69 74  ial_type = sqlit
1d508 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65  e3VdbeSerialType
1d509 28 75 2e 61 6f 2e 70 52 65 63 2c 20 75 2e 61 6f  (u.ao.pRec, u.ao
1d50a 2e 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20  .file_format);. 
1d50b 20 20 20 75 2e 61 6f 2e 6c 65 6e 20 3d 20 73 71     u.ao.len = sq
1d50c 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54  lite3VdbeSerialT
1d50d 79 70 65 4c 65 6e 28 75 2e 61 6f 2e 73 65 72 69  ypeLen(u.ao.seri
1d50e 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20 75 2e  al_type);.    u.
1d50f 61 6f 2e 6e 44 61 74 61 20 2b 3d 20 75 2e 61 6f  ao.nData += u.ao
1d510 2e 6c 65 6e 3b 0a 20 20 20 20 75 2e 61 6f 2e 6e  .len;.    u.ao.n
1d511 48 64 72 20 2b 3d 20 73 71 6c 69 74 65 33 56 61  Hdr += sqlite3Va
1d512 72 69 6e 74 4c 65 6e 28 75 2e 61 6f 2e 73 65 72  rintLen(u.ao.ser
1d513 69 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20 69  ial_type);.    i
1d514 66 28 20 75 2e 61 6f 2e 70 52 65 63 2d 3e 66 6c  f( u.ao.pRec->fl
1d515 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29  ags & MEM_Zero )
1d516 7b 0a 20 20 20 20 20 20 2f 2a 20 4f 6e 6c 79 20  {.      /* Only 
1d517 70 75 72 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64  pure zero-filled
1d518 20 42 4c 4f 42 73 20 63 61 6e 20 62 65 20 69 6e   BLOBs can be in
1d519 70 75 74 20 74 6f 20 74 68 69 73 20 4f 70 63 6f  put to this Opco
1d51a 64 65 2e 0a 20 20 20 20 20 20 2a 2a 20 57 65 20  de..      ** We 
1d51b 64 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 62 6c 6f  do not allow blo
1d51c 62 73 20 77 69 74 68 20 61 20 70 72 65 66 69 78  bs with a prefix
1d51d 20 61 6e 64 20 61 20 7a 65 72 6f 2d 66 69 6c 6c   and a zero-fill
1d51e 65 64 20 74 61 69 6c 2e 20 2a 2f 0a 20 20 20 20  ed tail. */.    
1d51f 20 20 75 2e 61 6f 2e 6e 5a 65 72 6f 20 2b 3d 20    u.ao.nZero += 
1d520 75 2e 61 6f 2e 70 52 65 63 2d 3e 75 2e 6e 5a 65  u.ao.pRec->u.nZe
1d521 72 6f 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  ro;.    }else if
1d522 28 20 75 2e 61 6f 2e 6c 65 6e 20 29 7b 0a 20 20  ( u.ao.len ){.  
1d523 20 20 20 20 75 2e 61 6f 2e 6e 5a 65 72 6f 20 3d      u.ao.nZero =
1d524 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20   0;.    }.  }.. 
1d525 20 2f 2a 20 41 64 64 20 74 68 65 20 69 6e 69 74   /* Add the init
1d526 69 61 6c 20 68 65 61 64 65 72 20 76 61 72 69 6e  ial header varin
1d527 74 20 61 6e 64 20 74 6f 74 61 6c 20 74 68 65 20  t and total the 
1d528 73 69 7a 65 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e  size */.  u.ao.n
1d529 48 64 72 20 2b 3d 20 75 2e 61 6f 2e 6e 56 61 72  Hdr += u.ao.nVar
1d52a 69 6e 74 20 3d 20 73 71 6c 69 74 65 33 56 61 72  int = sqlite3Var
1d52b 69 6e 74 4c 65 6e 28 75 2e 61 6f 2e 6e 48 64 72  intLen(u.ao.nHdr
1d52c 29 3b 0a 20 20 69 66 28 20 75 2e 61 6f 2e 6e 56  );.  if( u.ao.nV
1d52d 61 72 69 6e 74 3c 73 71 6c 69 74 65 33 56 61 72  arint<sqlite3Var
1d52e 69 6e 74 4c 65 6e 28 75 2e 61 6f 2e 6e 48 64 72  intLen(u.ao.nHdr
1d52f 29 20 29 7b 0a 20 20 20 20 75 2e 61 6f 2e 6e 48  ) ){.    u.ao.nH
1d530 64 72 2b 2b 3b 0a 20 20 7d 0a 20 20 75 2e 61 6f  dr++;.  }.  u.ao
1d531 2e 6e 42 79 74 65 20 3d 20 75 2e 61 6f 2e 6e 48  .nByte = u.ao.nH
1d532 64 72 2b 75 2e 61 6f 2e 6e 44 61 74 61 2d 75 2e  dr+u.ao.nData-u.
1d533 61 6f 2e 6e 5a 65 72 6f 3b 0a 20 20 69 66 28 20  ao.nZero;.  if( 
1d534 75 2e 61 6f 2e 6e 42 79 74 65 3e 64 62 2d 3e 61  u.ao.nByte>db->a
1d535 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
1d536 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20  IT_LENGTH] ){.  
1d537 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a    goto too_big;.
1d538 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73    }..  /* Make s
1d539 75 72 65 20 74 68 65 20 6f 75 74 70 75 74 20 72  ure the output r
1d53a 65 67 69 73 74 65 72 20 68 61 73 20 61 20 62 75  egister has a bu
1d53b 66 66 65 72 20 6c 61 72 67 65 20 65 6e 6f 75 67  ffer large enoug
1d53c 68 20 74 6f 20 73 74 6f 72 65 0a 20 20 2a 2a 20  h to store.  ** 
1d53d 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 2e 20  the new record. 
1d53e 54 68 65 20 6f 75 74 70 75 74 20 72 65 67 69 73  The output regis
1d53f 74 65 72 20 28 70 4f 70 2d 3e 70 33 29 20 69 73  ter (pOp->p3) is
1d540 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f 0a   not allowed to.
1d541 20 20 2a 2a 20 62 65 20 6f 6e 65 20 6f 66 20 74    ** be one of t
1d542 68 65 20 69 6e 70 75 74 20 72 65 67 69 73 74 65  he input registe
1d543 72 73 20 28 62 65 63 61 75 73 65 20 74 68 65 20  rs (because the 
1d544 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 74  following call t
1d545 6f 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 64  o.  ** sqlite3Vd
1d546 62 65 4d 65 6d 47 72 6f 77 28 29 20 63 6f 75 6c  beMemGrow() coul
1d547 64 20 63 6c 6f 62 62 65 72 20 74 68 65 20 76 61  d clobber the va
1d548 6c 75 65 20 62 65 66 6f 72 65 20 69 74 20 69 73  lue before it is
1d549 20 75 73 65 64 29 2e 0a 20 20 2a 2f 0a 20 20 61   used)..  */.  a
1d54a 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 70  ssert( pOp->p3<p
1d54b 4f 70 2d 3e 70 31 20 7c 7c 20 70 4f 70 2d 3e 70  Op->p1 || pOp->p
1d54c 33 3e 3d 70 4f 70 2d 3e 70 31 2b 70 4f 70 2d 3e  3>=pOp->p1+pOp->
1d54d 70 32 20 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26  p2 );.  pOut = &
1d54e 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d  p->aMem[pOp->p3]
1d54f 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56  ;.  if( sqlite3V
1d550 64 62 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c  dbeMemGrow(pOut,
1d551 20 28 69 6e 74 29 75 2e 61 6f 2e 6e 42 79 74 65   (int)u.ao.nByte
1d552 2c 20 30 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  , 0) ){.    goto
1d553 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 75   no_mem;.  }.  u
1d554 2e 61 6f 2e 7a 4e 65 77 52 65 63 6f 72 64 20 3d  .ao.zNewRecord =
1d555 20 28 75 38 20 2a 29 70 4f 75 74 2d 3e 7a 3b 0a   (u8 *)pOut->z;.
1d556 0a 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20  .  /* Write the 
1d557 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 2e 61 6f  record */.  u.ao
1d558 2e 69 20 3d 20 70 75 74 56 61 72 69 6e 74 33 32  .i = putVarint32
1d559 28 75 2e 61 6f 2e 7a 4e 65 77 52 65 63 6f 72 64  (u.ao.zNewRecord
1d55a 2c 20 75 2e 61 6f 2e 6e 48 64 72 29 3b 0a 20 20  , u.ao.nHdr);.  
1d55b 66 6f 72 28 75 2e 61 6f 2e 70 52 65 63 3d 75 2e  for(u.ao.pRec=u.
1d55c 61 6f 2e 70 44 61 74 61 30 3b 20 75 2e 61 6f 2e  ao.pData0; u.ao.
1d55d 70 52 65 63 3c 3d 75 2e 61 6f 2e 70 4c 61 73 74  pRec<=u.ao.pLast
1d55e 3b 20 75 2e 61 6f 2e 70 52 65 63 2b 2b 29 7b 0a  ; u.ao.pRec++){.
1d55f 20 20 20 20 75 2e 61 6f 2e 73 65 72 69 61 6c 5f      u.ao.serial_
1d560 74 79 70 65 20 3d 20 73 71 6c 69 74 65 33 56 64  type = sqlite3Vd
1d561 62 65 53 65 72 69 61 6c 54 79 70 65 28 75 2e 61  beSerialType(u.a
1d562 6f 2e 70 52 65 63 2c 20 75 2e 61 6f 2e 66 69 6c  o.pRec, u.ao.fil
1d563 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 20 20 75  e_format);.    u
1d564 2e 61 6f 2e 69 20 2b 3d 20 70 75 74 56 61 72 69  .ao.i += putVari
1d565 6e 74 33 32 28 26 75 2e 61 6f 2e 7a 4e 65 77 52  nt32(&u.ao.zNewR
1d566 65 63 6f 72 64 5b 75 2e 61 6f 2e 69 5d 2c 20 75  ecord[u.ao.i], u
1d567 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70 65 29  .ao.serial_type)
1d568 3b 20 20 20 20 20 20 2f 2a 20 73 65 72 69 61 6c  ;      /* serial
1d569 20 74 79 70 65 20 2a 2f 0a 20 20 7d 0a 20 20 66   type */.  }.  f
1d56a 6f 72 28 75 2e 61 6f 2e 70 52 65 63 3d 75 2e 61  or(u.ao.pRec=u.a
1d56b 6f 2e 70 44 61 74 61 30 3b 20 75 2e 61 6f 2e 70  o.pData0; u.ao.p
1d56c 52 65 63 3c 3d 75 2e 61 6f 2e 70 4c 61 73 74 3b  Rec<=u.ao.pLast;
1d56d 20 75 2e 61 6f 2e 70 52 65 63 2b 2b 29 7b 20 20   u.ao.pRec++){  
1d56e 2f 2a 20 73 65 72 69 61 6c 20 64 61 74 61 20 2a  /* serial data *
1d56f 2f 0a 20 20 20 20 75 2e 61 6f 2e 69 20 2b 3d 20  /.    u.ao.i += 
1d570 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
1d571 6c 50 75 74 28 26 75 2e 61 6f 2e 7a 4e 65 77 52  lPut(&u.ao.zNewR
1d572 65 63 6f 72 64 5b 75 2e 61 6f 2e 69 5d 2c 20 28  ecord[u.ao.i], (
1d573 69 6e 74 29 28 75 2e 61 6f 2e 6e 42 79 74 65 2d  int)(u.ao.nByte-
1d574 75 2e 61 6f 2e 69 29 2c 20 75 2e 61 6f 2e 70 52  u.ao.i), u.ao.pR
1d575 65 63 2c 75 2e 61 6f 2e 66 69 6c 65 5f 66 6f 72  ec,u.ao.file_for
1d576 6d 61 74 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65  mat);.  }.  asse
1d577 72 74 28 20 75 2e 61 6f 2e 69 3d 3d 75 2e 61 6f  rt( u.ao.i==u.ao
1d578 2e 6e 42 79 74 65 20 29 3b 0a 0a 20 20 61 73 73  .nByte );..  ass
1d579 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26  ert( pOp->p3>0 &
1d57a 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d  & pOp->p3<=p->nM
1d57b 65 6d 20 29 3b 0a 20 20 70 4f 75 74 2d 3e 6e 20  em );.  pOut->n 
1d57c 3d 20 28 69 6e 74 29 75 2e 61 6f 2e 6e 42 79 74  = (int)u.ao.nByt
1d57d 65 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73  e;.  pOut->flags
1d57e 20 3d 20 4d 45 4d 5f 42 6c 6f 62 20 7c 20 4d 45   = MEM_Blob | ME
1d57f 4d 5f 44 79 6e 3b 0a 20 20 70 4f 75 74 2d 3e 78  M_Dyn;.  pOut->x
1d580 44 65 6c 20 3d 20 30 3b 0a 20 20 69 66 28 20 75  Del = 0;.  if( u
1d581 2e 61 6f 2e 6e 5a 65 72 6f 20 29 7b 0a 20 20 20  .ao.nZero ){.   
1d582 20 70 4f 75 74 2d 3e 75 2e 6e 5a 65 72 6f 20 3d   pOut->u.nZero =
1d583 20 75 2e 61 6f 2e 6e 5a 65 72 6f 3b 0a 20 20 20   u.ao.nZero;.   
1d584 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20   pOut->flags |= 
1d585 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 20 7d 0a 20 20  MEM_Zero;.  }.  
1d586 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 53 51 4c 49  pOut->enc = SQLI
1d587 54 45 5f 55 54 46 38 3b 20 20 2f 2a 20 49 6e 20  TE_UTF8;  /* In 
1d588 63 61 73 65 20 74 68 65 20 62 6c 6f 62 20 69 73  case the blob is
1d589 20 65 76 65 72 20 63 6f 6e 76 65 72 74 65 64 20   ever converted 
1d58a 74 6f 20 74 65 78 74 20 2a 2f 0a 20 20 52 45 47  to text */.  REG
1d58b 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d  ISTER_TRACE(pOp-
1d58c 3e 70 33 2c 20 70 4f 75 74 29 3b 0a 20 20 55 50  >p3, pOut);.  UP
1d58d 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a  DATE_MAX_BLOBSIZ
1d58e 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b  E(pOut);.  break
1d58f 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1d590 43 6f 75 6e 74 20 50 31 20 50 32 20 2a 20 2a 20  Count P1 P2 * * 
1d591 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65 20 74 68  *.**.** Store th
1d592 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  e number of entr
1d593 69 65 73 20 28 61 6e 20 69 6e 74 65 67 65 72 20  ies (an integer 
1d594 76 61 6c 75 65 29 20 69 6e 20 74 68 65 20 74 61  value) in the ta
1d595 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 0a 2a 2a  ble or index .**
1d596 20 6f 70 65 6e 65 64 20 62 79 20 63 75 72 73 6f   opened by curso
1d597 72 20 50 31 20 69 6e 20 72 65 67 69 73 74 65 72  r P1 in register
1d598 20 50 32 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53   P2.*/.#ifndef S
1d599 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 54 52 45 45  QLITE_OMIT_BTREE
1d59a 43 4f 55 4e 54 0a 63 61 73 65 20 4f 50 5f 43 6f  COUNT.case OP_Co
1d59b 75 6e 74 3a 20 7b 20 20 20 20 20 20 20 20 20 2f  unt: {         /
1d59c 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73  * out2-prereleas
1d59d 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c  e */.#if 0  /* l
1d59e 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1d59f 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 70 20 2a  oved into u.ap *
1d5a0 2f 0a 20 20 69 36 34 20 6e 45 6e 74 72 79 3b 0a  /.  i64 nEntry;.
1d5a1 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73    BtCursor *pCrs
1d5a2 72 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  r;.#endif /* loc
1d5a3 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1d5a4 65 64 20 69 6e 74 6f 20 75 2e 61 70 20 2a 2f 0a  ed into u.ap */.
1d5a5 0a 20 20 75 2e 61 70 2e 70 43 72 73 72 20 3d 20  .  u.ap.pCrsr = 
1d5a6 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31  p->apCsr[pOp->p1
1d5a7 5d 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 66  ]->pCursor;.  if
1d5a8 28 20 75 2e 61 70 2e 70 43 72 73 72 20 29 7b 0a  ( u.ap.pCrsr ){.
1d5a9 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1d5aa 42 74 72 65 65 43 6f 75 6e 74 28 75 2e 61 70 2e  BtreeCount(u.ap.
1d5ab 70 43 72 73 72 2c 20 26 75 2e 61 70 2e 6e 45 6e  pCrsr, &u.ap.nEn
1d5ac 74 72 79 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  try);.  }else{. 
1d5ad 20 20 20 75 2e 61 70 2e 6e 45 6e 74 72 79 20 3d     u.ap.nEntry =
1d5ae 20 30 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e   0;.  }.  pOut->
1d5af 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
1d5b0 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75  .  pOut->u.i = u
1d5b1 2e 61 70 2e 6e 45 6e 74 72 79 3b 0a 20 20 62 72  .ap.nEntry;.  br
1d5b2 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  eak;.}.#endif../
1d5b3 2a 20 4f 70 63 6f 64 65 3a 20 53 61 76 65 70 6f  * Opcode: Savepo
1d5b4 69 6e 74 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a  int P1 * * P4 *.
1d5b5 2a 2a 0a 2a 2a 20 4f 70 65 6e 2c 20 72 65 6c 65  **.** Open, rele
1d5b6 61 73 65 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20  ase or rollback 
1d5b7 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 6e 61  the savepoint na
1d5b8 6d 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72  med by parameter
1d5b9 20 50 34 2c 20 64 65 70 65 6e 64 69 6e 67 0a 2a   P4, depending.*
1d5ba 2a 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 6f  * on the value o
1d5bb 66 20 50 31 2e 20 54 6f 20 6f 70 65 6e 20 61 20  f P1. To open a 
1d5bc 6e 65 77 20 73 61 76 65 70 6f 69 6e 74 2c 20 50  new savepoint, P
1d5bd 31 3d 3d 30 2e 20 54 6f 20 72 65 6c 65 61 73 65  1==0. To release
1d5be 20 28 63 6f 6d 6d 69 74 29 20 61 6e 0a 2a 2a 20   (commit) an.** 
1d5bf 65 78 69 73 74 69 6e 67 20 73 61 76 65 70 6f 69  existing savepoi
1d5c0 6e 74 2c 20 50 31 3d 3d 31 2c 20 6f 72 20 74 6f  nt, P1==1, or to
1d5c1 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 20 65 78 69   rollback an exi
1d5c2 73 74 69 6e 67 20 73 61 76 65 70 6f 69 6e 74 20  sting savepoint 
1d5c3 50 31 3d 3d 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f  P1==2..*/.case O
1d5c4 50 5f 53 61 76 65 70 6f 69 6e 74 3a 20 7b 0a 23  P_Savepoint: {.#
1d5c5 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
1d5c6 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1d5c7 6e 74 6f 20 75 2e 61 71 20 2a 2f 0a 20 20 69 6e  nto u.aq */.  in
1d5c8 74 20 70 31 3b 20 20 20 20 20 20 20 20 20 20 20  t p1;           
1d5c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1d5ca 20 56 61 6c 75 65 20 6f 66 20 50 31 20 6f 70 65   Value of P1 ope
1d5cb 72 61 6e 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a  rand */.  char *
1d5cc 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20  zName;          
1d5cd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
1d5ce 65 20 6f 66 20 73 61 76 65 70 6f 69 6e 74 20 2a  e of savepoint *
1d5cf 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20  /.  int nName;. 
1d5d0 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 4e 65 77   Savepoint *pNew
1d5d1 3b 0a 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70  ;.  Savepoint *p
1d5d2 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 53 61 76  Savepoint;.  Sav
1d5d3 65 70 6f 69 6e 74 20 2a 70 54 6d 70 3b 0a 20 20  epoint *pTmp;.  
1d5d4 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 3b 0a  int iSavepoint;.
1d5d5 20 20 69 6e 74 20 69 69 3b 0a 23 65 6e 64 69 66    int ii;.#endif
1d5d6 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1d5d7 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1d5d8 2e 61 71 20 2a 2f 0a 0a 20 20 75 2e 61 71 2e 70  .aq */..  u.aq.p
1d5d9 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75  1 = pOp->p1;.  u
1d5da 2e 61 71 2e 7a 4e 61 6d 65 20 3d 20 70 4f 70 2d  .aq.zName = pOp-
1d5db 3e 70 34 2e 7a 3b 0a 0a 20 20 2f 2a 20 41 73 73  >p4.z;..  /* Ass
1d5dc 65 72 74 20 74 68 61 74 20 74 68 65 20 75 2e 61  ert that the u.a
1d5dd 71 2e 70 31 20 70 61 72 61 6d 65 74 65 72 20 69  q.p1 parameter i
1d5de 73 20 76 61 6c 69 64 2e 20 41 6c 73 6f 20 74 68  s valid. Also th
1d5df 61 74 20 69 66 20 74 68 65 72 65 20 69 73 20 6e  at if there is n
1d5e0 6f 20 6f 70 65 6e 0a 20 20 2a 2a 20 74 72 61 6e  o open.  ** tran
1d5e1 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68  saction, then th
1d5e2 65 72 65 20 63 61 6e 6e 6f 74 20 62 65 20 61 6e  ere cannot be an
1d5e3 79 20 73 61 76 65 70 6f 69 6e 74 73 2e 0a 20 20  y savepoints..  
1d5e4 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d  */.  assert( db-
1d5e5 3e 70 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 7c  >pSavepoint==0 |
1d5e6 7c 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74  | db->autoCommit
1d5e7 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
1d5e8 20 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f   u.aq.p1==SAVEPO
1d5e9 49 4e 54 5f 42 45 47 49 4e 7c 7c 75 2e 61 71 2e  INT_BEGIN||u.aq.
1d5ea 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45  p1==SAVEPOINT_RE
1d5eb 4c 45 41 53 45 7c 7c 75 2e 61 71 2e 70 31 3d 3d  LEASE||u.aq.p1==
1d5ec 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41  SAVEPOINT_ROLLBA
1d5ed 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  CK );.  assert( 
1d5ee 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 7c  db->pSavepoint |
1d5ef 7c 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74  | db->isTransact
1d5f0 69 6f 6e 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20  ionSavepoint==0 
1d5f1 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 68 65  );.  assert( che
1d5f2 63 6b 53 61 76 65 70 6f 69 6e 74 43 6f 75 6e 74  ckSavepointCount
1d5f3 28 64 62 29 20 29 3b 0a 0a 20 20 69 66 28 20 75  (db) );..  if( u
1d5f4 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e  .aq.p1==SAVEPOIN
1d5f5 54 5f 42 45 47 49 4e 20 29 7b 0a 20 20 20 20 69  T_BEGIN ){.    i
1d5f6 66 28 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65  f( db->writeVdbe
1d5f7 43 6e 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 2f  Cnt>0 ){.      /
1d5f8 2a 20 41 20 6e 65 77 20 73 61 76 65 70 6f 69 6e  * A new savepoin
1d5f9 74 20 63 61 6e 6e 6f 74 20 62 65 20 63 72 65 61  t cannot be crea
1d5fa 74 65 64 20 69 66 20 74 68 65 72 65 20 61 72 65  ted if there are
1d5fb 20 61 63 74 69 76 65 20 77 72 69 74 65 0a 20 20   active write.  
1d5fc 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74      ** statement
1d5fd 73 20 28 69 2e 65 2e 20 6f 70 65 6e 20 72 65 61  s (i.e. open rea
1d5fe 64 2f 77 72 69 74 65 20 69 6e 63 72 65 6d 65 6e  d/write incremen
1d5ff 74 61 6c 20 62 6c 6f 62 20 68 61 6e 64 6c 65 73  tal blob handles
1d600 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  )..      */.    
1d601 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
1d602 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20  ng(&p->zErrMsg, 
1d603 64 62 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e  db, "cannot open
1d604 20 73 61 76 65 70 6f 69 6e 74 20 2d 20 22 0a 20   savepoint - ". 
1d605 20 20 20 20 20 20 20 22 53 51 4c 20 73 74 61 74         "SQL stat
1d606 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 65  ements in progre
1d607 73 73 22 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  ss");.      rc =
1d608 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
1d609 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 75    }else{.      u
1d60a 2e 61 71 2e 6e 4e 61 6d 65 20 3d 20 73 71 6c 69  .aq.nName = sqli
1d60b 74 65 33 53 74 72 6c 65 6e 33 30 28 75 2e 61 71  te3Strlen30(u.aq
1d60c 2e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 20 20 20 20  .zName);..      
1d60d 2f 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  /* Create a new 
1d60e 73 61 76 65 70 6f 69 6e 74 20 73 74 72 75 63 74  savepoint struct
1d60f 75 72 65 2e 20 2a 2f 0a 20 20 20 20 20 20 75 2e  ure. */.      u.
1d610 61 71 2e 70 4e 65 77 20 3d 20 73 71 6c 69 74 65  aq.pNew = sqlite
1d611 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
1d612 20 73 69 7a 65 6f 66 28 53 61 76 65 70 6f 69 6e   sizeof(Savepoin
1d613 74 29 2b 75 2e 61 71 2e 6e 4e 61 6d 65 2b 31 29  t)+u.aq.nName+1)
1d614 3b 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61 71  ;.      if( u.aq
1d615 2e 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20  .pNew ){.       
1d616 20 75 2e 61 71 2e 70 4e 65 77 2d 3e 7a 4e 61 6d   u.aq.pNew->zNam
1d617 65 20 3d 20 28 63 68 61 72 20 2a 29 26 75 2e 61  e = (char *)&u.a
1d618 71 2e 70 4e 65 77 5b 31 5d 3b 0a 20 20 20 20 20  q.pNew[1];.     
1d619 20 20 20 6d 65 6d 63 70 79 28 75 2e 61 71 2e 70     memcpy(u.aq.p
1d61a 4e 65 77 2d 3e 7a 4e 61 6d 65 2c 20 75 2e 61 71  New->zName, u.aq
1d61b 2e 7a 4e 61 6d 65 2c 20 75 2e 61 71 2e 6e 4e 61  .zName, u.aq.nNa
1d61c 6d 65 2b 31 29 3b 0a 0a 20 20 20 20 20 20 20 20  me+1);..        
1d61d 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e  /* If there is n
1d61e 6f 20 6f 70 65 6e 20 74 72 61 6e 73 61 63 74 69  o open transacti
1d61f 6f 6e 2c 20 74 68 65 6e 20 6d 61 72 6b 20 74 68  on, then mark th
1d620 69 73 20 61 73 20 61 20 73 70 65 63 69 61 6c 0a  is as a special.
1d621 20 20 20 20 20 20 20 20 2a 2a 20 22 74 72 61 6e          ** "tran
1d622 73 61 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e  saction savepoin
1d623 74 22 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69  t". */.        i
1d624 66 28 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69  f( db->autoCommi
1d625 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64  t ){.          d
1d626 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20  b->autoCommit = 
1d627 30 3b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d  0;.          db-
1d628 3e 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61  >isTransactionSa
1d629 76 65 70 6f 69 6e 74 20 3d 20 31 3b 0a 20 20 20  vepoint = 1;.   
1d62a 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1d62b 20 20 20 20 20 20 64 62 2d 3e 6e 53 61 76 65 70        db->nSavep
1d62c 6f 69 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20  oint++;.        
1d62d 7d 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 4c 69  }..        /* Li
1d62e 6e 6b 20 74 68 65 20 6e 65 77 20 73 61 76 65 70  nk the new savep
1d62f 6f 69 6e 74 20 69 6e 74 6f 20 74 68 65 20 64 61  oint into the da
1d630 74 61 62 61 73 65 20 68 61 6e 64 6c 65 27 73 20  tabase handle's 
1d631 6c 69 73 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20  list. */.       
1d632 20 75 2e 61 71 2e 70 4e 65 77 2d 3e 70 4e 65 78   u.aq.pNew->pNex
1d633 74 20 3d 20 64 62 2d 3e 70 53 61 76 65 70 6f 69  t = db->pSavepoi
1d634 6e 74 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e  nt;.        db->
1d635 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 75 2e 61  pSavepoint = u.a
1d636 71 2e 70 4e 65 77 3b 0a 20 20 20 20 20 20 7d 0a  q.pNew;.      }.
1d637 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
1d638 20 20 20 75 2e 61 71 2e 69 53 61 76 65 70 6f 69     u.aq.iSavepoi
1d639 6e 74 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20  nt = 0;..    /* 
1d63a 46 69 6e 64 20 74 68 65 20 6e 61 6d 65 64 20 73  Find the named s
1d63b 61 76 65 70 6f 69 6e 74 2e 20 49 66 20 74 68 65  avepoint. If the
1d63c 72 65 20 69 73 20 6e 6f 20 73 75 63 68 20 73 61  re is no such sa
1d63d 76 65 70 6f 69 6e 74 2c 20 74 68 65 6e 20 61 6e  vepoint, then an
1d63e 0a 20 20 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72  .    ** an error
1d63f 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20   is returned to 
1d640 74 68 65 20 75 73 65 72 2e 20 20 2a 2f 0a 20 20  the user.  */.  
1d641 20 20 66 6f 72 28 0a 20 20 20 20 20 20 75 2e 61    for(.      u.a
1d642 71 2e 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 64  q.pSavepoint = d
1d643 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20  b->pSavepoint;. 
1d644 20 20 20 20 20 75 2e 61 71 2e 70 53 61 76 65 70       u.aq.pSavep
1d645 6f 69 6e 74 20 26 26 20 73 71 6c 69 74 65 33 53  oint && sqlite3S
1d646 74 72 49 43 6d 70 28 75 2e 61 71 2e 70 53 61 76  trICmp(u.aq.pSav
1d647 65 70 6f 69 6e 74 2d 3e 7a 4e 61 6d 65 2c 20 75  epoint->zName, u
1d648 2e 61 71 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  .aq.zName);.    
1d649 20 20 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e    u.aq.pSavepoin
1d64a 74 20 3d 20 75 2e 61 71 2e 70 53 61 76 65 70 6f  t = u.aq.pSavepo
1d64b 69 6e 74 2d 3e 70 4e 65 78 74 0a 20 20 20 20 29  int->pNext.    )
1d64c 7b 0a 20 20 20 20 20 20 75 2e 61 71 2e 69 53 61  {.      u.aq.iSa
1d64d 76 65 70 6f 69 6e 74 2b 2b 3b 0a 20 20 20 20 7d  vepoint++;.    }
1d64e 0a 20 20 20 20 69 66 28 20 21 75 2e 61 71 2e 70  .    if( !u.aq.p
1d64f 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20  Savepoint ){.   
1d650 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72     sqlite3SetStr
1d651 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c  ing(&p->zErrMsg,
1d652 20 64 62 2c 20 22 6e 6f 20 73 75 63 68 20 73 61   db, "no such sa
1d653 76 65 70 6f 69 6e 74 3a 20 25 73 22 2c 20 75 2e  vepoint: %s", u.
1d654 61 71 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  aq.zName);.     
1d655 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
1d656 4f 52 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  OR;.    }else if
1d657 28 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 77 72  (.        db->wr
1d658 69 74 65 56 64 62 65 43 6e 74 3e 30 20 7c 7c 20  iteVdbeCnt>0 || 
1d659 28 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f  (u.aq.p1==SAVEPO
1d65a 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 26 26 20  INT_ROLLBACK && 
1d65b 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e  db->activeVdbeCn
1d65c 74 3e 31 29 0a 20 20 20 20 29 7b 0a 20 20 20 20  t>1).    ){.    
1d65d 20 20 2f 2a 20 49 74 20 69 73 20 6e 6f 74 20 70    /* It is not p
1d65e 6f 73 73 69 62 6c 65 20 74 6f 20 72 65 6c 65 61  ossible to relea
1d65f 73 65 20 28 63 6f 6d 6d 69 74 29 20 61 20 73 61  se (commit) a sa
1d660 76 65 70 6f 69 6e 74 20 69 66 20 74 68 65 72 65  vepoint if there
1d661 20 61 72 65 0a 20 20 20 20 20 20 2a 2a 20 61 63   are.      ** ac
1d662 74 69 76 65 20 77 72 69 74 65 20 73 74 61 74 65  tive write state
1d663 6d 65 6e 74 73 2e 20 49 74 20 69 73 20 6e 6f 74  ments. It is not
1d664 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 72 6f 6c   possible to rol
1d665 6c 62 61 63 6b 20 61 20 73 61 76 65 70 6f 69 6e  lback a savepoin
1d666 74 0a 20 20 20 20 20 20 2a 2a 20 69 66 20 74 68  t.      ** if th
1d667 65 72 65 20 61 72 65 20 61 6e 79 20 61 63 74 69  ere are any acti
1d668 76 65 20 73 74 61 74 65 6d 65 6e 74 73 20 61 74  ve statements at
1d669 20 61 6c 6c 2e 0a 20 20 20 20 20 20 2a 2f 0a 20   all..      */. 
1d66a 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53       sqlite3SetS
1d66b 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73  tring(&p->zErrMs
1d66c 67 2c 20 64 62 2c 0a 20 20 20 20 20 20 20 20 22  g, db,.        "
1d66d 63 61 6e 6e 6f 74 20 25 73 20 73 61 76 65 70 6f  cannot %s savepo
1d66e 69 6e 74 20 2d 20 53 51 4c 20 73 74 61 74 65 6d  int - SQL statem
1d66f 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 73 73  ents in progress
1d670 22 2c 0a 20 20 20 20 20 20 20 20 28 75 2e 61 71  ",.        (u.aq
1d671 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52  .p1==SAVEPOINT_R
1d672 4f 4c 4c 42 41 43 4b 20 3f 20 22 72 6f 6c 6c 62  OLLBACK ? "rollb
1d673 61 63 6b 22 3a 20 22 72 65 6c 65 61 73 65 22 29  ack": "release")
1d674 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20  .      );.      
1d675 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59  rc = SQLITE_BUSY
1d676 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 0a 20 20  ;.    }else{..  
1d677 20 20 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65      /* Determine
1d678 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
1d679 74 68 69 73 20 69 73 20 61 20 74 72 61 6e 73 61  this is a transa
1d67a 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 2e  ction savepoint.
1d67b 20 49 66 20 73 6f 2c 0a 20 20 20 20 20 20 2a 2a   If so,.      **
1d67c 20 61 6e 64 20 74 68 69 73 20 69 73 20 61 20 52   and this is a R
1d67d 45 4c 45 41 53 45 20 63 6f 6d 6d 61 6e 64 2c 20  ELEASE command, 
1d67e 74 68 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74  then the current
1d67f 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 20   transaction.   
1d680 20 20 20 2a 2a 20 69 73 20 63 6f 6d 6d 69 74 74     ** is committ
1d681 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ed..      */.   
1d682 20 20 20 69 6e 74 20 69 73 54 72 61 6e 73 61 63     int isTransac
1d683 74 69 6f 6e 20 3d 20 75 2e 61 71 2e 70 53 61 76  tion = u.aq.pSav
1d684 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 3d 3d 30  epoint->pNext==0
1d685 20 26 26 20 64 62 2d 3e 69 73 54 72 61 6e 73 61   && db->isTransa
1d686 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 3b 0a  ctionSavepoint;.
1d687 20 20 20 20 20 20 69 66 28 20 69 73 54 72 61 6e        if( isTran
1d688 73 61 63 74 69 6f 6e 20 26 26 20 75 2e 61 71 2e  saction && u.aq.
1d689 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45  p1==SAVEPOINT_RE
1d68a 4c 45 41 53 45 20 29 7b 0a 20 20 20 20 20 20 20  LEASE ){.       
1d68b 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20   db->autoCommit 
1d68c 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 69 66 28  = 1;.        if(
1d68d 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74   sqlite3VdbeHalt
1d68e 28 70 29 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59  (p)==SQLITE_BUSY
1d68f 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d   ){.          p-
1d690 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20 20 20 20  >pc = pc;.      
1d691 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d      db->autoComm
1d692 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  it = 0;.        
1d693 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53    p->rc = rc = S
1d694 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20  QLITE_BUSY;.    
1d695 20 20 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f        goto vdbe_
1d696 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20  return;.        
1d697 7d 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 69 73  }.        db->is
1d698 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70  TransactionSavep
1d699 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  oint = 0;.      
1d69a 20 20 72 63 20 3d 20 70 2d 3e 72 63 3b 0a 20 20    rc = p->rc;.  
1d69b 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1d69c 20 20 20 75 2e 61 71 2e 69 53 61 76 65 70 6f 69     u.aq.iSavepoi
1d69d 6e 74 20 3d 20 64 62 2d 3e 6e 53 61 76 65 70 6f  nt = db->nSavepo
1d69e 69 6e 74 20 2d 20 75 2e 61 71 2e 69 53 61 76 65  int - u.aq.iSave
1d69f 70 6f 69 6e 74 20 2d 20 31 3b 0a 20 20 20 20 20  point - 1;.     
1d6a0 20 20 20 66 6f 72 28 75 2e 61 71 2e 69 69 3d 30     for(u.aq.ii=0
1d6a1 3b 20 75 2e 61 71 2e 69 69 3c 64 62 2d 3e 6e 44  ; u.aq.ii<db->nD
1d6a2 62 3b 20 75 2e 61 71 2e 69 69 2b 2b 29 7b 0a 20  b; u.aq.ii++){. 
1d6a3 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71           rc = sq
1d6a4 6c 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f  lite3BtreeSavepo
1d6a5 69 6e 74 28 64 62 2d 3e 61 44 62 5b 75 2e 61 71  int(db->aDb[u.aq
1d6a6 2e 69 69 5d 2e 70 42 74 2c 20 75 2e 61 71 2e 70  .ii].pBt, u.aq.p
1d6a7 31 2c 20 75 2e 61 71 2e 69 53 61 76 65 70 6f 69  1, u.aq.iSavepoi
1d6a8 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  nt);.          i
1d6a9 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1d6aa 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
1d6ab 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1d6ac 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20  o_error;.       
1d6ad 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
1d6ae 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 71 2e         if( u.aq.
1d6af 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f  p1==SAVEPOINT_RO
1d6b0 4c 4c 42 41 43 4b 20 26 26 20 28 64 62 2d 3e 66  LLBACK && (db->f
1d6b1 6c 61 67 73 26 53 51 4c 49 54 45 5f 49 6e 74 65  lags&SQLITE_Inte
1d6b2 72 6e 43 68 61 6e 67 65 73 29 21 3d 30 20 29 7b  rnChanges)!=0 ){
1d6b3 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
1d6b4 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64  e3ExpirePrepared
1d6b5 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a  Statements(db);.
1d6b6 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1d6b7 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63  3ResetInternalSc
1d6b8 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 20  hema(db, 0);.   
1d6b9 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a       }.      }..
1d6ba 20 20 20 20 20 20 2f 2a 20 52 65 67 61 72 64 6c        /* Regardl
1d6bb 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 74  ess of whether t
1d6bc 68 69 73 20 69 73 20 61 20 52 45 4c 45 41 53 45  his is a RELEASE
1d6bd 20 6f 72 20 52 4f 4c 4c 42 41 43 4b 2c 20 64 65   or ROLLBACK, de
1d6be 73 74 72 6f 79 20 61 6c 6c 0a 20 20 20 20 20 20  stroy all.      
1d6bf 2a 2a 20 73 61 76 65 70 6f 69 6e 74 73 20 6e 65  ** savepoints ne
1d6c0 73 74 65 64 20 69 6e 73 69 64 65 20 6f 66 20 74  sted inside of t
1d6c1 68 65 20 73 61 76 65 70 6f 69 6e 74 20 62 65 69  he savepoint bei
1d6c2 6e 67 20 6f 70 65 72 61 74 65 64 20 6f 6e 2e 20  ng operated on. 
1d6c3 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  */.      while( 
1d6c4 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 21 3d  db->pSavepoint!=
1d6c5 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 20  u.aq.pSavepoint 
1d6c6 29 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 71 2e  ){.        u.aq.
1d6c7 70 54 6d 70 20 3d 20 64 62 2d 3e 70 53 61 76 65  pTmp = db->pSave
1d6c8 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20 20 20 64  point;.        d
1d6c9 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 3d 20  b->pSavepoint = 
1d6ca 75 2e 61 71 2e 70 54 6d 70 2d 3e 70 4e 65 78 74  u.aq.pTmp->pNext
1d6cb 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
1d6cc 33 44 62 46 72 65 65 28 64 62 2c 20 75 2e 61 71  3DbFree(db, u.aq
1d6cd 2e 70 54 6d 70 29 3b 0a 20 20 20 20 20 20 20 20  .pTmp);.        
1d6ce 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 2d 2d  db->nSavepoint--
1d6cf 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
1d6d0 20 2f 2a 20 49 66 20 69 74 20 69 73 20 61 20 52   /* If it is a R
1d6d1 45 4c 45 41 53 45 2c 20 74 68 65 6e 20 64 65 73  ELEASE, then des
1d6d2 74 72 6f 79 20 74 68 65 20 73 61 76 65 70 6f 69  troy the savepoi
1d6d3 6e 74 20 62 65 69 6e 67 20 6f 70 65 72 61 74 65  nt being operate
1d6d4 64 20 6f 6e 20 74 6f 6f 20 2a 2f 0a 20 20 20 20  d on too */.    
1d6d5 20 20 69 66 28 20 75 2e 61 71 2e 70 31 3d 3d 53    if( u.aq.p1==S
1d6d6 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45  AVEPOINT_RELEASE
1d6d7 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
1d6d8 72 74 28 20 75 2e 61 71 2e 70 53 61 76 65 70 6f  rt( u.aq.pSavepo
1d6d9 69 6e 74 3d 3d 64 62 2d 3e 70 53 61 76 65 70 6f  int==db->pSavepo
1d6da 69 6e 74 20 29 3b 0a 20 20 20 20 20 20 20 20 64  int );.        d
1d6db 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 3d 20  b->pSavepoint = 
1d6dc 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 2d  u.aq.pSavepoint-
1d6dd 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20  >pNext;.        
1d6de 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1d6df 2c 20 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e  , u.aq.pSavepoin
1d6e0 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  t);.        if( 
1d6e1 21 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 20 29  !isTransaction )
1d6e2 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e  {.          db->
1d6e3 6e 53 61 76 65 70 6f 69 6e 74 2d 2d 3b 0a 20 20  nSavepoint--;.  
1d6e4 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1d6e5 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 62 72 65      }.  }..  bre
1d6e6 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1d6e7 3a 20 41 75 74 6f 43 6f 6d 6d 69 74 20 50 31 20  : AutoCommit P1 
1d6e8 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53  P2 * * *.**.** S
1d6e9 65 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20  et the database 
1d6ea 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67  auto-commit flag
1d6eb 20 74 6f 20 50 31 20 28 31 20 6f 72 20 30 29 2e   to P1 (1 or 0).
1d6ec 20 49 66 20 50 32 20 69 73 20 74 72 75 65 2c 20   If P2 is true, 
1d6ed 72 6f 6c 6c 0a 2a 2a 20 62 61 63 6b 20 61 6e 79  roll.** back any
1d6ee 20 63 75 72 72 65 6e 74 6c 79 20 61 63 74 69 76   currently activ
1d6ef 65 20 62 74 72 65 65 20 74 72 61 6e 73 61 63 74  e btree transact
1d6f0 69 6f 6e 73 2e 20 49 66 20 74 68 65 72 65 20 61  ions. If there a
1d6f1 72 65 20 61 6e 79 20 61 63 74 69 76 65 0a 2a 2a  re any active.**
1d6f2 20 56 4d 73 20 28 61 70 61 72 74 20 66 72 6f 6d   VMs (apart from
1d6f3 20 74 68 69 73 20 6f 6e 65 29 2c 20 74 68 65 6e   this one), then
1d6f4 20 61 20 52 4f 4c 4c 42 41 43 4b 20 66 61 69 6c   a ROLLBACK fail
1d6f5 73 2e 20 20 41 20 43 4f 4d 4d 49 54 20 66 61 69  s.  A COMMIT fai
1d6f6 6c 73 20 69 66 0a 2a 2a 20 74 68 65 72 65 20 61  ls if.** there a
1d6f7 72 65 20 61 63 74 69 76 65 20 77 72 69 74 69 6e  re active writin
1d6f8 67 20 56 4d 73 20 6f 72 20 61 63 74 69 76 65 20  g VMs or active 
1d6f9 56 4d 73 20 74 68 61 74 20 75 73 65 20 73 68 61  VMs that use sha
1d6fa 72 65 64 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a  red cache..**.**
1d6fb 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   This instructio
1d6fc 6e 20 63 61 75 73 65 73 20 74 68 65 20 56 4d 20  n causes the VM 
1d6fd 74 6f 20 68 61 6c 74 2e 0a 2a 2f 0a 63 61 73 65  to halt..*/.case
1d6fe 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 3a 20   OP_AutoCommit: 
1d6ff 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  {.#if 0  /* loca
1d700 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1d701 64 20 69 6e 74 6f 20 75 2e 61 72 20 2a 2f 0a 20  d into u.ar */. 
1d702 20 69 6e 74 20 64 65 73 69 72 65 64 41 75 74 6f   int desiredAuto
1d703 43 6f 6d 6d 69 74 3b 0a 20 20 69 6e 74 20 69 52  Commit;.  int iR
1d704 6f 6c 6c 62 61 63 6b 3b 0a 20 20 69 6e 74 20 74  ollback;.  int t
1d705 75 72 6e 4f 6e 41 43 3b 0a 23 65 6e 64 69 66 20  urnOnAC;.#endif 
1d706 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1d707 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1d708 61 72 20 2a 2f 0a 0a 20 20 75 2e 61 72 2e 64 65  ar */..  u.ar.de
1d709 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 20  siredAutoCommit 
1d70a 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 2e 61  = pOp->p1;.  u.a
1d70b 72 2e 69 52 6f 6c 6c 62 61 63 6b 20 3d 20 70 4f  r.iRollback = pO
1d70c 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 72 2e 74 75  p->p2;.  u.ar.tu
1d70d 72 6e 4f 6e 41 43 20 3d 20 75 2e 61 72 2e 64 65  rnOnAC = u.ar.de
1d70e 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 20  siredAutoCommit 
1d70f 26 26 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d  && !db->autoComm
1d710 69 74 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e  it;.  assert( u.
1d711 61 72 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f  ar.desiredAutoCo
1d712 6d 6d 69 74 3d 3d 31 20 7c 7c 20 75 2e 61 72 2e  mmit==1 || u.ar.
1d713 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69  desiredAutoCommi
1d714 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  t==0 );.  assert
1d715 28 20 75 2e 61 72 2e 64 65 73 69 72 65 64 41 75  ( u.ar.desiredAu
1d716 74 6f 43 6f 6d 6d 69 74 3d 3d 31 20 7c 7c 20 75  toCommit==1 || u
1d717 2e 61 72 2e 69 52 6f 6c 6c 62 61 63 6b 3d 3d 30  .ar.iRollback==0
1d718 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62   );.  assert( db
1d719 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e  ->activeVdbeCnt>
1d71a 30 20 29 3b 20 20 2f 2a 20 41 74 20 6c 65 61 73  0 );  /* At leas
1d71b 74 20 74 68 69 73 20 6f 6e 65 20 56 4d 20 69 73  t this one VM is
1d71c 20 61 63 74 69 76 65 20 2a 2f 0a 0a 20 20 69 66   active */..  if
1d71d 28 20 75 2e 61 72 2e 74 75 72 6e 4f 6e 41 43 20  ( u.ar.turnOnAC 
1d71e 26 26 20 75 2e 61 72 2e 69 52 6f 6c 6c 62 61 63  && u.ar.iRollbac
1d71f 6b 20 26 26 20 64 62 2d 3e 61 63 74 69 76 65 56  k && db->activeV
1d720 64 62 65 43 6e 74 3e 31 20 29 7b 0a 20 20 20 20  dbeCnt>1 ){.    
1d721 2f 2a 20 49 66 20 74 68 69 73 20 69 6e 73 74 72  /* If this instr
1d722 75 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74  uction implement
1d723 73 20 61 20 52 4f 4c 4c 42 41 43 4b 20 61 6e 64  s a ROLLBACK and
1d724 20 6f 74 68 65 72 20 56 4d 73 20 61 72 65 0a 20   other VMs are. 
1d725 20 20 20 2a 2a 20 73 74 69 6c 6c 20 72 75 6e 6e     ** still runn
1d726 69 6e 67 2c 20 61 6e 64 20 61 20 74 72 61 6e 73  ing, and a trans
1d727 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65  action is active
1d728 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f  , return an erro
1d729 72 20 69 6e 64 69 63 61 74 69 6e 67 0a 20 20 20  r indicating.   
1d72a 20 2a 2a 20 74 68 61 74 20 74 68 65 20 6f 74 68   ** that the oth
1d72b 65 72 20 56 4d 73 20 6d 75 73 74 20 63 6f 6d 70  er VMs must comp
1d72c 6c 65 74 65 20 66 69 72 73 74 2e 0a 20 20 20 20  lete first..    
1d72d 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65  */.    sqlite3Se
1d72e 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72  tString(&p->zErr
1d72f 4d 73 67 2c 20 64 62 2c 20 22 63 61 6e 6e 6f 74  Msg, db, "cannot
1d730 20 72 6f 6c 6c 62 61 63 6b 20 74 72 61 6e 73 61   rollback transa
1d731 63 74 69 6f 6e 20 2d 20 22 0a 20 20 20 20 20 20  ction - ".      
1d732 20 20 22 53 51 4c 20 73 74 61 74 65 6d 65 6e 74    "SQL statement
1d733 73 20 69 6e 20 70 72 6f 67 72 65 73 73 22 29 3b  s in progress");
1d734 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
1d735 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65 20 69  _BUSY;.  }else i
1d736 66 28 20 75 2e 61 72 2e 74 75 72 6e 4f 6e 41 43  f( u.ar.turnOnAC
1d737 20 26 26 20 21 75 2e 61 72 2e 69 52 6f 6c 6c 62   && !u.ar.iRollb
1d738 61 63 6b 20 26 26 20 64 62 2d 3e 77 72 69 74 65  ack && db->write
1d739 56 64 62 65 43 6e 74 3e 30 20 29 7b 0a 20 20 20  VdbeCnt>0 ){.   
1d73a 20 2f 2a 20 49 66 20 74 68 69 73 20 69 6e 73 74   /* If this inst
1d73b 72 75 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e  ruction implemen
1d73c 74 73 20 61 20 43 4f 4d 4d 49 54 20 61 6e 64 20  ts a COMMIT and 
1d73d 6f 74 68 65 72 20 56 4d 73 20 61 72 65 20 77 72  other VMs are wr
1d73e 69 74 69 6e 67 0a 20 20 20 20 2a 2a 20 72 65 74  iting.    ** ret
1d73f 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 6e 64  urn an error ind
1d740 69 63 61 74 69 6e 67 20 74 68 61 74 20 74 68 65  icating that the
1d741 20 6f 74 68 65 72 20 56 4d 73 20 6d 75 73 74 20   other VMs must 
1d742 63 6f 6d 70 6c 65 74 65 20 66 69 72 73 74 2e 0a  complete first..
1d743 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74      */.    sqlit
1d744 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e  e3SetString(&p->
1d745 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 63 61  zErrMsg, db, "ca
1d746 6e 6e 6f 74 20 63 6f 6d 6d 69 74 20 74 72 61 6e  nnot commit tran
1d747 73 61 63 74 69 6f 6e 20 2d 20 22 0a 20 20 20 20  saction - ".    
1d748 20 20 20 20 22 53 51 4c 20 73 74 61 74 65 6d 65      "SQL stateme
1d749 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 73 73 22  nts in progress"
1d74a 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  );.    rc = SQLI
1d74b 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65  TE_BUSY;.  }else
1d74c 20 69 66 28 20 75 2e 61 72 2e 64 65 73 69 72 65   if( u.ar.desire
1d74d 64 41 75 74 6f 43 6f 6d 6d 69 74 21 3d 64 62 2d  dAutoCommit!=db-
1d74e 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20  >autoCommit ){. 
1d74f 20 20 20 69 66 28 20 75 2e 61 72 2e 69 52 6f 6c     if( u.ar.iRol
1d750 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 20 20 61  lback ){.      a
1d751 73 73 65 72 74 28 20 75 2e 61 72 2e 64 65 73 69  ssert( u.ar.desi
1d752 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31  redAutoCommit==1
1d753 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
1d754 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29  3RollbackAll(db)
1d755 3b 0a 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f  ;.      db->auto
1d756 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20  Commit = 1;.    
1d757 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 64 62 2d  }else{.      db-
1d758 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 28 75  >autoCommit = (u
1d759 38 29 75 2e 61 72 2e 64 65 73 69 72 65 64 41 75  8)u.ar.desiredAu
1d75a 74 6f 43 6f 6d 6d 69 74 3b 0a 20 20 20 20 20 20  toCommit;.      
1d75b 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 48  if( sqlite3VdbeH
1d75c 61 6c 74 28 70 29 3d 3d 53 51 4c 49 54 45 5f 42  alt(p)==SQLITE_B
1d75d 55 53 59 20 29 7b 0a 20 20 20 20 20 20 20 20 70  USY ){.        p
1d75e 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20 20 20  ->pc = pc;.     
1d75f 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69     db->autoCommi
1d760 74 20 3d 20 28 75 38 29 28 31 2d 75 2e 61 72 2e  t = (u8)(1-u.ar.
1d761 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69  desiredAutoCommi
1d762 74 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 72  t);.        p->r
1d763 63 20 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  c = rc = SQLITE_
1d764 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20 67 6f  BUSY;.        go
1d765 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a  to vdbe_return;.
1d766 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1d767 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6e 53    assert( db->nS
1d768 74 61 74 65 6d 65 6e 74 3d 3d 30 20 29 3b 0a 20  tatement==0 );. 
1d769 20 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53     sqlite3CloseS
1d76a 61 76 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a 20  avepoints(db);. 
1d76b 20 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51     if( p->rc==SQ
1d76c 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1d76d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e   rc = SQLITE_DON
1d76e 45 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  E;.    }else{.  
1d76f 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1d770 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20  ERROR;.    }.   
1d771 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72   goto vdbe_retur
1d772 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  n;.  }else{.    
1d773 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67  sqlite3SetString
1d774 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62  (&p->zErrMsg, db
1d775 2c 0a 20 20 20 20 20 20 20 20 28 21 75 2e 61 72  ,.        (!u.ar
1d776 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d  .desiredAutoComm
1d777 69 74 29 3f 22 63 61 6e 6e 6f 74 20 73 74 61 72  it)?"cannot star
1d778 74 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  t a transaction 
1d779 77 69 74 68 69 6e 20 61 20 74 72 61 6e 73 61 63  within a transac
1d77a 74 69 6f 6e 22 3a 28 0a 20 20 20 20 20 20 20 20  tion":(.        
1d77b 28 75 2e 61 72 2e 69 52 6f 6c 6c 62 61 63 6b 29  (u.ar.iRollback)
1d77c 3f 22 63 61 6e 6e 6f 74 20 72 6f 6c 6c 62 61 63  ?"cannot rollbac
1d77d 6b 20 2d 20 6e 6f 20 74 72 61 6e 73 61 63 74 69  k - no transacti
1d77e 6f 6e 20 69 73 20 61 63 74 69 76 65 22 3a 0a 20  on is active":. 
1d77f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d780 20 20 22 63 61 6e 6e 6f 74 20 63 6f 6d 6d 69 74    "cannot commit
1d781 20 2d 20 6e 6f 20 74 72 61 6e 73 61 63 74 69 6f   - no transactio
1d782 6e 20 69 73 20 61 63 74 69 76 65 22 29 29 3b 0a  n is active"));.
1d783 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
1d784 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 62 72  _ERROR;.  }.  br
1d785 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1d786 65 3a 20 54 72 61 6e 73 61 63 74 69 6f 6e 20 50  e: Transaction P
1d787 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  1 P2 * * *.**.**
1d788 20 42 65 67 69 6e 20 61 20 74 72 61 6e 73 61 63   Begin a transac
1d789 74 69 6f 6e 2e 20 20 54 68 65 20 74 72 61 6e 73  tion.  The trans
1d78a 61 63 74 69 6f 6e 20 65 6e 64 73 20 77 68 65 6e  action ends when
1d78b 20 61 20 43 6f 6d 6d 69 74 20 6f 72 20 52 6f 6c   a Commit or Rol
1d78c 6c 62 61 63 6b 0a 2a 2a 20 6f 70 63 6f 64 65 20  lback.** opcode 
1d78d 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 20  is encountered. 
1d78e 20 44 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68   Depending on th
1d78f 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 73 65  e ON CONFLICT se
1d790 74 74 69 6e 67 2c 20 74 68 65 0a 2a 2a 20 74 72  tting, the.** tr
1d791 61 6e 73 61 63 74 69 6f 6e 20 6d 69 67 68 74 20  ansaction might 
1d792 61 6c 73 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62  also be rolled b
1d793 61 63 6b 20 69 66 20 61 6e 20 65 72 72 6f 72 20  ack if an error 
1d794 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a  is encountered..
1d795 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 74 68 65 20  **.** P1 is the 
1d796 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74  index of the dat
1d797 61 62 61 73 65 20 66 69 6c 65 20 6f 6e 20 77 68  abase file on wh
1d798 69 63 68 20 74 68 65 20 74 72 61 6e 73 61 63 74  ich the transact
1d799 69 6f 6e 20 69 73 0a 2a 2a 20 73 74 61 72 74 65  ion is.** starte
1d79a 64 2e 20 20 49 6e 64 65 78 20 30 20 69 73 20 74  d.  Index 0 is t
1d79b 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
1d79c 20 66 69 6c 65 20 61 6e 64 20 69 6e 64 65 78 20   file and index 
1d79d 31 20 69 73 20 74 68 65 0a 2a 2a 20 66 69 6c 65  1 is the.** file
1d79e 20 75 73 65 64 20 66 6f 72 20 74 65 6d 70 6f 72   used for tempor
1d79f 61 72 79 20 74 61 62 6c 65 73 2e 20 20 49 6e 64  ary tables.  Ind
1d7a0 69 63 65 73 20 6f 66 20 32 20 6f 72 20 6d 6f 72  ices of 2 or mor
1d7a1 65 20 61 72 65 20 75 73 65 64 20 66 6f 72 0a 2a  e are used for.*
1d7a2 2a 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62  * attached datab
1d7a3 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50  ases..**.** If P
1d7a4 32 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74  2 is non-zero, t
1d7a5 68 65 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e  hen a write-tran
1d7a6 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74  saction is start
1d7a7 65 64 2e 20 20 41 20 52 45 53 45 52 56 45 44 20  ed.  A RESERVED 
1d7a8 6c 6f 63 6b 20 69 73 0a 2a 2a 20 6f 62 74 61 69  lock is.** obtai
1d7a9 6e 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ned on the datab
1d7aa 61 73 65 20 66 69 6c 65 20 77 68 65 6e 20 61 20  ase file when a 
1d7ab 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f  write-transactio
1d7ac 6e 20 69 73 20 73 74 61 72 74 65 64 2e 20 20 4e  n is started.  N
1d7ad 6f 0a 2a 2a 20 6f 74 68 65 72 20 70 72 6f 63 65  o.** other proce
1d7ae 73 73 20 63 61 6e 20 73 74 61 72 74 20 61 6e 6f  ss can start ano
1d7af 74 68 65 72 20 77 72 69 74 65 20 74 72 61 6e 73  ther write trans
1d7b0 61 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 68 69  action while thi
1d7b1 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  s transaction is
1d7b2 0a 2a 2a 20 75 6e 64 65 72 77 61 79 2e 20 20 53  .** underway.  S
1d7b3 74 61 72 74 69 6e 67 20 61 20 77 72 69 74 65 20  tarting a write 
1d7b4 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6c 73 6f  transaction also
1d7b5 20 63 72 65 61 74 65 73 20 61 20 72 6f 6c 6c 62   creates a rollb
1d7b6 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 41 0a 2a  ack journal. A.*
1d7b7 2a 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74  * write transact
1d7b8 69 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 61 72  ion must be star
1d7b9 74 65 64 20 62 65 66 6f 72 65 20 61 6e 79 20 63  ted before any c
1d7ba 68 61 6e 67 65 73 20 63 61 6e 20 62 65 20 6d 61  hanges can be ma
1d7bb 64 65 20 74 6f 20 74 68 65 0a 2a 2a 20 64 61 74  de to the.** dat
1d7bc 61 62 61 73 65 2e 20 20 49 66 20 50 32 20 69 73  abase.  If P2 is
1d7bd 20 32 20 6f 72 20 67 72 65 61 74 65 72 20 74 68   2 or greater th
1d7be 65 6e 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20  en an EXCLUSIVE 
1d7bf 6c 6f 63 6b 20 69 73 20 61 6c 73 6f 20 6f 62 74  lock is also obt
1d7c0 61 69 6e 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20  ained.** on the 
1d7c1 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  file..**.** If a
1d7c2 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69   write-transacti
1d7c3 6f 6e 20 69 73 20 73 74 61 72 74 65 64 20 61 6e  on is started an
1d7c4 64 20 74 68 65 20 56 64 62 65 2e 75 73 65 73 53  d the Vdbe.usesS
1d7c5 74 6d 74 4a 6f 75 72 6e 61 6c 20 66 6c 61 67 20  tmtJournal flag 
1d7c6 69 73 0a 2a 2a 20 74 72 75 65 20 28 74 68 69 73  is.** true (this
1d7c7 20 66 6c 61 67 20 69 73 20 73 65 74 20 69 66 20   flag is set if 
1d7c8 74 68 65 20 56 64 62 65 20 6d 61 79 20 6d 6f 64  the Vdbe may mod
1d7c9 69 66 79 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e  ify more than on
1d7ca 65 20 72 6f 77 20 61 6e 64 20 6d 61 79 0a 2a 2a  e row and may.**
1d7cb 20 74 68 72 6f 77 20 61 6e 20 41 42 4f 52 54 20   throw an ABORT 
1d7cc 65 78 63 65 70 74 69 6f 6e 29 2c 20 61 20 73 74  exception), a st
1d7cd 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74  atement transact
1d7ce 69 6f 6e 20 6d 61 79 20 61 6c 73 6f 20 62 65 20  ion may also be 
1d7cf 6f 70 65 6e 65 64 2e 0a 2a 2a 20 4d 6f 72 65 20  opened..** More 
1d7d0 73 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 61 20  specifically, a 
1d7d1 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61  statement transa
1d7d2 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 65 64 20  ction is opened 
1d7d3 69 66 66 20 74 68 65 20 64 61 74 61 62 61 73 65  iff the database
1d7d4 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69  .** connection i
1d7d5 73 20 63 75 72 72 65 6e 74 6c 79 20 6e 6f 74 20  s currently not 
1d7d6 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f  in autocommit mo
1d7d7 64 65 2c 20 6f 72 20 69 66 20 74 68 65 72 65 20  de, or if there 
1d7d8 61 72 65 20 6f 74 68 65 72 0a 2a 2a 20 61 63 74  are other.** act
1d7d9 69 76 65 20 73 74 61 74 65 6d 65 6e 74 73 2e 20  ive statements. 
1d7da 41 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e  A statement tran
1d7db 73 61 63 74 69 6f 6e 20 61 6c 6c 6f 77 73 20 74  saction allows t
1d7dc 68 65 20 61 66 66 65 63 74 73 20 6f 66 20 74 68  he affects of th
1d7dd 69 73 0a 2a 2a 20 56 44 42 45 20 74 6f 20 62 65  is.** VDBE to be
1d7de 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 66 74   rolled back aft
1d7df 65 72 20 61 6e 20 65 72 72 6f 72 20 77 69 74 68  er an error with
1d7e0 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 72 6f  out having to ro
1d7e1 6c 6c 20 62 61 63 6b 20 74 68 65 0a 2a 2a 20 65  ll back the.** e
1d7e2 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74 69 6f  ntire transactio
1d7e3 6e 2e 20 49 66 20 6e 6f 20 65 72 72 6f 72 20 69  n. If no error i
1d7e4 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 74  s encountered, t
1d7e5 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61  he statement tra
1d7e6 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 77 69 6c 6c  nsaction.** will
1d7e7 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63   automatically c
1d7e8 6f 6d 6d 69 74 20 77 68 65 6e 20 74 68 65 20 56  ommit when the V
1d7e9 44 42 45 20 68 61 6c 74 73 2e 0a 2a 2a 0a 2a 2a  DBE halts..**.**
1d7ea 20 49 66 20 50 32 20 69 73 20 7a 65 72 6f 2c 20   If P2 is zero, 
1d7eb 74 68 65 6e 20 61 20 72 65 61 64 2d 6c 6f 63 6b  then a read-lock
1d7ec 20 69 73 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20   is obtained on 
1d7ed 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
1d7ee 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 72  e..*/.case OP_Tr
1d7ef 61 6e 73 61 63 74 69 6f 6e 3a 20 7b 0a 23 69 66  ansaction: {.#if
1d7f0 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72   0  /* local var
1d7f1 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1d7f2 6f 20 75 2e 61 73 20 2a 2f 0a 20 20 42 74 72 65  o u.as */.  Btre
1d7f3 65 20 2a 70 42 74 3b 0a 23 65 6e 64 69 66 20 2f  e *pBt;.#endif /
1d7f4 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1d7f5 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
1d7f6 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  s */..  assert( 
1d7f7 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f  pOp->p1>=0 && pO
1d7f8 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b  p->p1<db->nDb );
1d7f9 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62  .  assert( (p->b
1d7fa 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70  treeMask & (1<<p
1d7fb 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20  Op->p1))!=0 );. 
1d7fc 20 75 2e 61 73 2e 70 42 74 20 3d 20 64 62 2d 3e   u.as.pBt = db->
1d7fd 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 42 74  aDb[pOp->p1].pBt
1d7fe 3b 0a 0a 20 20 69 66 28 20 75 2e 61 73 2e 70 42  ;..  if( u.as.pB
1d7ff 74 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  t ){.    rc = sq
1d800 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54  lite3BtreeBeginT
1d801 72 61 6e 73 28 75 2e 61 73 2e 70 42 74 2c 20 70  rans(u.as.pBt, p
1d802 4f 70 2d 3e 70 32 29 3b 0a 20 20 20 20 69 66 28  Op->p2);.    if(
1d803 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59   rc==SQLITE_BUSY
1d804 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 63 20   ){.      p->pc 
1d805 3d 20 70 63 3b 0a 20 20 20 20 20 20 70 2d 3e 72  = pc;.      p->r
1d806 63 20 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  c = rc = SQLITE_
1d807 42 55 53 59 3b 0a 20 20 20 20 20 20 67 6f 74 6f  BUSY;.      goto
1d808 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20   vdbe_return;.  
1d809 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d    }.    if( rc!=
1d80a 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21  SQLITE_OK && rc!
1d80b 3d 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59  =SQLITE_READONLY
1d80c 20 2f 2a 20 26 26 20 72 63 21 3d 53 51 4c 49 54   /* && rc!=SQLIT
1d80d 45 5f 42 55 53 59 20 2a 2f 20 29 7b 0a 20 20 20  E_BUSY */ ){.   
1d80e 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75     goto abort_du
1d80f 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20  e_to_error;.    
1d810 7d 0a 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e  }..    if( pOp->
1d811 70 32 20 26 26 20 70 2d 3e 75 73 65 73 53 74 6d  p2 && p->usesStm
1d812 74 4a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 26 26  tJournal.     &&
1d813 20 28 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74   (db->autoCommit
1d814 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61 63 74 69 76  ==0 || db->activ
1d815 65 56 64 62 65 43 6e 74 3e 31 29 0a 20 20 20 20  eVdbeCnt>1).    
1d816 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
1d817 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49   sqlite3BtreeIsI
1d818 6e 54 72 61 6e 73 28 75 2e 61 73 2e 70 42 74 29  nTrans(u.as.pBt)
1d819 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d   );.      if( p-
1d81a 3e 69 53 74 61 74 65 6d 65 6e 74 3d 3d 30 20 29  >iStatement==0 )
1d81b 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
1d81c 28 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74  ( db->nStatement
1d81d 3e 3d 30 20 26 26 20 64 62 2d 3e 6e 53 61 76 65  >=0 && db->nSave
1d81e 70 6f 69 6e 74 3e 3d 30 20 29 3b 0a 20 20 20 20  point>=0 );.    
1d81f 20 20 20 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65      db->nStateme
1d820 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 70 2d  nt++;.        p-
1d821 3e 69 53 74 61 74 65 6d 65 6e 74 20 3d 20 64 62  >iStatement = db
1d822 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 2b 20 64  ->nSavepoint + d
1d823 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3b 0a 20  b->nStatement;. 
1d824 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20       }.      rc 
1d825 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65  = sqlite3BtreeBe
1d826 67 69 6e 53 74 6d 74 28 75 2e 61 73 2e 70 42 74  ginStmt(u.as.pBt
1d827 2c 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 29  , p->iStatement)
1d828 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72  ;.    }.  }.  br
1d829 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1d82a 65 3a 20 52 65 61 64 43 6f 6f 6b 69 65 20 50 31  e: ReadCookie P1
1d82b 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a   P2 P3 * *.**.**
1d82c 20 52 65 61 64 20 63 6f 6f 6b 69 65 20 6e 75 6d   Read cookie num
1d82d 62 65 72 20 50 33 20 66 72 6f 6d 20 64 61 74 61  ber P3 from data
1d82e 62 61 73 65 20 50 31 20 61 6e 64 20 77 72 69 74  base P1 and writ
1d82f 65 20 69 74 20 69 6e 74 6f 20 72 65 67 69 73 74  e it into regist
1d830 65 72 20 50 32 2e 0a 2a 2a 20 50 33 3d 3d 31 20  er P2..** P3==1 
1d831 69 73 20 74 68 65 20 73 63 68 65 6d 61 20 76 65  is the schema ve
1d832 72 73 69 6f 6e 2e 20 20 50 33 3d 3d 32 20 69 73  rsion.  P3==2 is
1d833 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 6f   the database fo
1d834 72 6d 61 74 2e 0a 2a 2a 20 50 33 3d 3d 33 20 69  rmat..** P3==3 i
1d835 73 20 74 68 65 20 72 65 63 6f 6d 6d 65 6e 64 65  s the recommende
1d836 64 20 70 61 67 65 72 20 63 61 63 68 65 20 73 69  d pager cache si
1d837 7a 65 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68  ze, and so forth
1d838 2e 20 20 50 31 3d 3d 30 20 69 73 0a 2a 2a 20 74  .  P1==0 is.** t
1d839 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
1d83a 20 66 69 6c 65 20 61 6e 64 20 50 31 3d 3d 31 20   file and P1==1 
1d83b 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  is the database 
1d83c 66 69 6c 65 20 75 73 65 64 20 74 6f 20 73 74 6f  file used to sto
1d83d 72 65 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20  re.** temporary 
1d83e 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  tables..**.** Th
1d83f 65 72 65 20 6d 75 73 74 20 62 65 20 61 20 72 65  ere must be a re
1d840 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64  ad-lock on the d
1d841 61 74 61 62 61 73 65 20 28 65 69 74 68 65 72 20  atabase (either 
1d842 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a  a transaction.**
1d843 20 6d 75 73 74 20 62 65 20 73 74 61 72 74 65 64   must be started
1d844 20 6f 72 20 74 68 65 72 65 20 6d 75 73 74 20 62   or there must b
1d845 65 20 61 6e 20 6f 70 65 6e 20 63 75 72 73 6f 72  e an open cursor
1d846 29 20 62 65 66 6f 72 65 0a 2a 2a 20 65 78 65 63  ) before.** exec
1d847 75 74 69 6e 67 20 74 68 69 73 20 69 6e 73 74 72  uting this instr
1d848 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20  uction..*/.case 
1d849 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 3a 20 7b  OP_ReadCookie: {
1d84a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1d84b 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73  * out2-prereleas
1d84c 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c  e */.#if 0  /* l
1d84d 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1d84e 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 74 20 2a  oved into u.at *
1d84f 2f 0a 20 20 69 6e 74 20 69 4d 65 74 61 3b 0a 20  /.  int iMeta;. 
1d850 20 69 6e 74 20 69 44 62 3b 0a 20 20 69 6e 74 20   int iDb;.  int 
1d851 69 43 6f 6f 6b 69 65 3b 0a 23 65 6e 64 69 66 20  iCookie;.#endif 
1d852 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1d853 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1d854 61 74 20 2a 2f 0a 0a 20 20 75 2e 61 74 2e 69 44  at */..  u.at.iD
1d855 62 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75  b = pOp->p1;.  u
1d856 2e 61 74 2e 69 43 6f 6f 6b 69 65 20 3d 20 70 4f  .at.iCookie = pO
1d857 70 2d 3e 70 33 3b 0a 20 20 61 73 73 65 72 74 28  p->p3;.  assert(
1d858 20 70 4f 70 2d 3e 70 33 3c 53 51 4c 49 54 45 5f   pOp->p3<SQLITE_
1d859 4e 5f 42 54 52 45 45 5f 4d 45 54 41 20 29 3b 0a  N_BTREE_META );.
1d85a 20 20 61 73 73 65 72 74 28 20 75 2e 61 74 2e 69    assert( u.at.i
1d85b 44 62 3e 3d 30 20 26 26 20 75 2e 61 74 2e 69 44  Db>=0 && u.at.iD
1d85c 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61  b<db->nDb );.  a
1d85d 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62 5b 75  ssert( db->aDb[u
1d85e 2e 61 74 2e 69 44 62 5d 2e 70 42 74 21 3d 30 20  .at.iDb].pBt!=0 
1d85f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d  );.  assert( (p-
1d860 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c  >btreeMask & (1<
1d861 3c 75 2e 61 74 2e 69 44 62 29 29 21 3d 30 20 29  <u.at.iDb))!=0 )
1d862 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  ;..  sqlite3Btre
1d863 65 47 65 74 4d 65 74 61 28 64 62 2d 3e 61 44 62  eGetMeta(db->aDb
1d864 5b 75 2e 61 74 2e 69 44 62 5d 2e 70 42 74 2c 20  [u.at.iDb].pBt, 
1d865 75 2e 61 74 2e 69 43 6f 6f 6b 69 65 2c 20 28 75  u.at.iCookie, (u
1d866 33 32 20 2a 29 26 75 2e 61 74 2e 69 4d 65 74 61  32 *)&u.at.iMeta
1d867 29 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d  );.  pOut->u.i =
1d868 20 75 2e 61 74 2e 69 4d 65 74 61 3b 0a 20 20 4d   u.at.iMeta;.  M
1d869 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f  emSetTypeFlag(pO
1d86a 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20  ut, MEM_Int);.  
1d86b 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1d86c 6f 64 65 3a 20 53 65 74 43 6f 6f 6b 69 65 20 50  ode: SetCookie P
1d86d 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a  1 P2 P3 * *.**.*
1d86e 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f 6e 74  * Write the cont
1d86f 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72 20  ent of register 
1d870 50 33 20 28 69 6e 74 65 72 70 72 65 74 65 64 20  P3 (interpreted 
1d871 61 73 20 61 6e 20 69 6e 74 65 67 65 72 29 0a 2a  as an integer).*
1d872 2a 20 69 6e 74 6f 20 63 6f 6f 6b 69 65 20 6e 75  * into cookie nu
1d873 6d 62 65 72 20 50 32 20 6f 66 20 64 61 74 61 62  mber P2 of datab
1d874 61 73 65 20 50 31 2e 20 20 50 32 3d 3d 31 20 69  ase P1.  P2==1 i
1d875 73 20 74 68 65 20 73 63 68 65 6d 61 20 76 65 72  s the schema ver
1d876 73 69 6f 6e 2e 20 20 0a 2a 2a 20 50 32 3d 3d 32  sion.  .** P2==2
1d877 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65   is the database
1d878 20 66 6f 72 6d 61 74 2e 20 50 32 3d 3d 33 20 69   format. P2==3 i
1d879 73 20 74 68 65 20 72 65 63 6f 6d 6d 65 6e 64 65  s the recommende
1d87a 64 20 70 61 67 65 72 20 63 61 63 68 65 20 0a 2a  d pager cache .*
1d87b 2a 20 73 69 7a 65 2c 20 61 6e 64 20 73 6f 20 66  * size, and so f
1d87c 6f 72 74 68 2e 20 20 50 31 3d 3d 30 20 69 73 20  orth.  P1==0 is 
1d87d 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
1d87e 65 20 66 69 6c 65 20 61 6e 64 20 50 31 3d 3d 31  e file and P1==1
1d87f 20 69 73 20 74 68 65 20 0a 2a 2a 20 64 61 74 61   is the .** data
1d880 62 61 73 65 20 66 69 6c 65 20 75 73 65 64 20 74  base file used t
1d881 6f 20 73 74 6f 72 65 20 74 65 6d 70 6f 72 61 72  o store temporar
1d882 79 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20  y tables..**.** 
1d883 41 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75  A transaction mu
1d884 73 74 20 62 65 20 73 74 61 72 74 65 64 20 62 65  st be started be
1d885 66 6f 72 65 20 65 78 65 63 75 74 69 6e 67 20 74  fore executing t
1d886 68 69 73 20 6f 70 63 6f 64 65 2e 0a 2a 2f 0a 63  his opcode..*/.c
1d887 61 73 65 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65  ase OP_SetCookie
1d888 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20 69 6e 33  : {       /* in3
1d889 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f   */.#if 0  /* lo
1d88a 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1d88b 76 65 64 20 69 6e 74 6f 20 75 2e 61 75 20 2a 2f  ved into u.au */
1d88c 0a 20 20 44 62 20 2a 70 44 62 3b 0a 23 65 6e 64  .  Db *pDb;.#end
1d88d 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1d88e 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1d88f 20 75 2e 61 75 20 2a 2f 0a 20 20 61 73 73 65 72   u.au */.  asser
1d890 74 28 20 70 4f 70 2d 3e 70 32 3c 53 51 4c 49 54  t( pOp->p2<SQLIT
1d891 45 5f 4e 5f 42 54 52 45 45 5f 4d 45 54 41 20 29  E_N_BTREE_META )
1d892 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
1d893 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70  >p1>=0 && pOp->p
1d894 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61  1<db->nDb );.  a
1d895 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65  ssert( (p->btree
1d896 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e  Mask & (1<<pOp->
1d897 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 75 2e 61  p1))!=0 );.  u.a
1d898 75 2e 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62  u.pDb = &db->aDb
1d899 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73  [pOp->p1];.  ass
1d89a 65 72 74 28 20 75 2e 61 75 2e 70 44 62 2d 3e 70  ert( u.au.pDb->p
1d89b 42 74 21 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74  Bt!=0 );.  sqlit
1d89c 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72  e3VdbeMemInteger
1d89d 69 66 79 28 70 49 6e 33 29 3b 0a 20 20 2f 2a 20  ify(pIn3);.  /* 
1d89e 53 65 65 20 6e 6f 74 65 20 61 62 6f 75 74 20 69  See note about i
1d89f 6e 64 65 78 20 73 68 69 66 74 69 6e 67 20 6f 6e  ndex shifting on
1d8a0 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 20 2a   OP_ReadCookie *
1d8a1 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  /.  rc = sqlite3
1d8a2 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 28  BtreeUpdateMeta(
1d8a3 75 2e 61 75 2e 70 44 62 2d 3e 70 42 74 2c 20 70  u.au.pDb->pBt, p
1d8a4 4f 70 2d 3e 70 32 2c 20 28 69 6e 74 29 70 49 6e  Op->p2, (int)pIn
1d8a5 33 2d 3e 75 2e 69 29 3b 0a 20 20 69 66 28 20 70  3->u.i);.  if( p
1d8a6 4f 70 2d 3e 70 32 3d 3d 42 54 52 45 45 5f 53 43  Op->p2==BTREE_SC
1d8a7 48 45 4d 41 5f 56 45 52 53 49 4f 4e 20 29 7b 0a  HEMA_VERSION ){.
1d8a8 20 20 20 20 2f 2a 20 57 68 65 6e 20 74 68 65 20      /* When the 
1d8a9 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 63 68  schema cookie ch
1d8aa 61 6e 67 65 73 2c 20 72 65 63 6f 72 64 20 74 68  anges, record th
1d8ab 65 20 6e 65 77 20 63 6f 6f 6b 69 65 20 69 6e 74  e new cookie int
1d8ac 65 72 6e 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 75  ernally */.    u
1d8ad 2e 61 75 2e 70 44 62 2d 3e 70 53 63 68 65 6d 61  .au.pDb->pSchema
1d8ae 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 20  ->schema_cookie 
1d8af 3d 20 28 69 6e 74 29 70 49 6e 33 2d 3e 75 2e 69  = (int)pIn3->u.i
1d8b0 3b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20  ;.    db->flags 
1d8b1 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e  |= SQLITE_Intern
1d8b2 43 68 61 6e 67 65 73 3b 0a 20 20 7d 65 6c 73 65  Changes;.  }else
1d8b3 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 42 54   if( pOp->p2==BT
1d8b4 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 20  REE_FILE_FORMAT 
1d8b5 29 7b 0a 20 20 20 20 2f 2a 20 52 65 63 6f 72 64  ){.    /* Record
1d8b6 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68 65 20   changes in the 
1d8b7 66 69 6c 65 20 66 6f 72 6d 61 74 20 2a 2f 0a 20  file format */. 
1d8b8 20 20 20 75 2e 61 75 2e 70 44 62 2d 3e 70 53 63     u.au.pDb->pSc
1d8b9 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61  hema->file_forma
1d8ba 74 20 3d 20 28 75 38 29 70 49 6e 33 2d 3e 75 2e  t = (u8)pIn3->u.
1d8bb 69 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 70  i;.  }.  if( pOp
1d8bc 2d 3e 70 31 3d 3d 31 20 29 7b 0a 20 20 20 20 2f  ->p1==1 ){.    /
1d8bd 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c  * Invalidate all
1d8be 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
1d8bf 65 6e 74 73 20 77 68 65 6e 65 76 65 72 20 74 68  ents whenever th
1d8c0 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 0a  e TEMP database.
1d8c1 20 20 20 20 2a 2a 20 73 63 68 65 6d 61 20 69 73      ** schema is
1d8c2 20 63 68 61 6e 67 65 64 2e 20 20 54 69 63 6b 65   changed.  Ticke
1d8c3 74 20 23 31 36 34 34 20 2a 2f 0a 20 20 20 20 73  t #1644 */.    s
1d8c4 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70  qlite3ExpirePrep
1d8c5 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64  aredStatements(d
1d8c6 62 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  b);.  }.  break;
1d8c7 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56  .}../* Opcode: V
1d8c8 65 72 69 66 79 43 6f 6f 6b 69 65 20 50 31 20 50  erifyCookie P1 P
1d8c9 32 20 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63 6b 20  2 *.**.** Check 
1d8ca 74 68 65 20 76 61 6c 75 65 20 6f 66 20 67 6c 6f  the value of glo
1d8cb 62 61 6c 20 64 61 74 61 62 61 73 65 20 70 61 72  bal database par
1d8cc 61 6d 65 74 65 72 20 6e 75 6d 62 65 72 20 30 20  ameter number 0 
1d8cd 28 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 76  (the.** schema v
1d8ce 65 72 73 69 6f 6e 29 20 61 6e 64 20 6d 61 6b 65  ersion) and make
1d8cf 20 73 75 72 65 20 69 74 20 69 73 20 65 71 75 61   sure it is equa
1d8d0 6c 20 74 6f 20 50 32 2e 20 20 0a 2a 2a 20 50 31  l to P2.  .** P1
1d8d1 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65   is the database
1d8d2 20 6e 75 6d 62 65 72 20 77 68 69 63 68 20 69 73   number which is
1d8d3 20 30 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20   0 for the main 
1d8d4 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a  database file.**
1d8d5 20 61 6e 64 20 31 20 66 6f 72 20 74 68 65 20 66   and 1 for the f
1d8d6 69 6c 65 20 68 6f 6c 64 69 6e 67 20 74 65 6d 70  ile holding temp
1d8d7 6f 72 61 72 79 20 74 61 62 6c 65 73 20 61 6e 64  orary tables and
1d8d8 20 73 6f 6d 65 20 68 69 67 68 65 72 20 6e 75 6d   some higher num
1d8d9 62 65 72 0a 2a 2a 20 66 6f 72 20 61 75 78 69 6c  ber.** for auxil
1d8da 69 61 72 79 20 64 61 74 61 62 61 73 65 73 2e 0a  iary databases..
1d8db 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6f 6b 69 65  **.** The cookie
1d8dc 20 63 68 61 6e 67 65 73 20 69 74 73 20 76 61 6c   changes its val
1d8dd 75 65 20 77 68 65 6e 65 76 65 72 20 74 68 65 20  ue whenever the 
1d8de 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20  database schema 
1d8df 63 68 61 6e 67 65 73 2e 0a 2a 2a 20 54 68 69 73  changes..** This
1d8e0 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 75 73   operation is us
1d8e1 65 64 20 74 6f 20 64 65 74 65 63 74 20 77 68 65  ed to detect whe
1d8e2 6e 20 74 68 61 74 20 74 68 65 20 63 6f 6f 6b 69  n that the cooki
1d8e3 65 20 68 61 73 20 63 68 61 6e 67 65 64 0a 2a 2a  e has changed.**
1d8e4 20 61 6e 64 20 74 68 61 74 20 74 68 65 20 63 75   and that the cu
1d8e5 72 72 65 6e 74 20 70 72 6f 63 65 73 73 20 6e 65  rrent process ne
1d8e6 65 64 73 20 74 6f 20 72 65 72 65 61 64 20 74 68  eds to reread th
1d8e7 65 20 73 63 68 65 6d 61 2e 0a 2a 2a 0a 2a 2a 20  e schema..**.** 
1d8e8 45 69 74 68 65 72 20 61 20 74 72 61 6e 73 61 63  Either a transac
1d8e9 74 69 6f 6e 20 6e 65 65 64 73 20 74 6f 20 68 61  tion needs to ha
1d8ea 76 65 20 62 65 65 6e 20 73 74 61 72 74 65 64 20  ve been started 
1d8eb 6f 72 20 61 6e 20 4f 50 5f 4f 70 65 6e 20 6e 65  or an OP_Open ne
1d8ec 65 64 73 0a 2a 2a 20 74 6f 20 62 65 20 65 78 65  eds.** to be exe
1d8ed 63 75 74 65 64 20 28 74 6f 20 65 73 74 61 62 6c  cuted (to establ
1d8ee 69 73 68 20 61 20 72 65 61 64 20 6c 6f 63 6b 29  ish a read lock)
1d8ef 20 62 65 66 6f 72 65 20 74 68 69 73 20 6f 70 63   before this opc
1d8f0 6f 64 65 20 69 73 0a 2a 2a 20 69 6e 76 6f 6b 65  ode is.** invoke
1d8f1 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 65  d..*/.case OP_Ve
1d8f2 72 69 66 79 43 6f 6f 6b 69 65 3a 20 7b 0a 23 69  rifyCookie: {.#i
1d8f3 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1d8f4 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1d8f5 74 6f 20 75 2e 61 76 20 2a 2f 0a 20 20 69 6e 74  to u.av */.  int
1d8f6 20 69 4d 65 74 61 3b 0a 20 20 42 74 72 65 65 20   iMeta;.  Btree 
1d8f7 2a 70 42 74 3b 0a 23 65 6e 64 69 66 20 2f 2a 20  *pBt;.#endif /* 
1d8f8 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1d8f9 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 76 20  moved into u.av 
1d8fa 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  */.  assert( pOp
1d8fb 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e  ->p1>=0 && pOp->
1d8fc 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20  p1<db->nDb );.  
1d8fd 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65  assert( (p->btre
1d8fe 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d  eMask & (1<<pOp-
1d8ff 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 75 2e  >p1))!=0 );.  u.
1d900 61 76 2e 70 42 74 20 3d 20 64 62 2d 3e 61 44 62  av.pBt = db->aDb
1d901 5b 70 4f 70 2d 3e 70 31 5d 2e 70 42 74 3b 0a 20  [pOp->p1].pBt;. 
1d902 20 69 66 28 20 75 2e 61 76 2e 70 42 74 20 29 7b   if( u.av.pBt ){
1d903 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
1d904 65 47 65 74 4d 65 74 61 28 75 2e 61 76 2e 70 42  eGetMeta(u.av.pB
1d905 74 2c 20 42 54 52 45 45 5f 53 43 48 45 4d 41 5f  t, BTREE_SCHEMA_
1d906 56 45 52 53 49 4f 4e 2c 20 28 75 33 32 20 2a 29  VERSION, (u32 *)
1d907 26 75 2e 61 76 2e 69 4d 65 74 61 29 3b 0a 20 20  &u.av.iMeta);.  
1d908 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 76 2e  }else{.    u.av.
1d909 69 4d 65 74 61 20 3d 20 30 3b 0a 20 20 7d 0a 20  iMeta = 0;.  }. 
1d90a 20 69 66 28 20 75 2e 61 76 2e 69 4d 65 74 61 21   if( u.av.iMeta!
1d90b 3d 70 4f 70 2d 3e 70 32 20 29 7b 0a 20 20 20 20  =pOp->p2 ){.    
1d90c 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1d90d 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20  , p->zErrMsg);. 
1d90e 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20     p->zErrMsg = 
1d90f 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
1d910 64 62 2c 20 22 64 61 74 61 62 61 73 65 20 73 63  db, "database sc
1d911 68 65 6d 61 20 68 61 73 20 63 68 61 6e 67 65 64  hema has changed
1d912 22 29 3b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  ");.    /* If th
1d913 65 20 73 63 68 65 6d 61 2d 63 6f 6f 6b 69 65 20  e schema-cookie 
1d914 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
1d915 65 20 66 69 6c 65 20 6d 61 74 63 68 65 73 20 74  e file matches t
1d916 68 65 20 63 6f 6f 6b 69 65 0a 20 20 20 20 2a 2a  he cookie.    **
1d917 20 73 74 6f 72 65 64 20 77 69 74 68 20 74 68 65   stored with the
1d918 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65   in-memory repre
1d919 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  sentation of the
1d91a 20 73 63 68 65 6d 61 2c 20 64 6f 0a 20 20 20 20   schema, do.    
1d91b 2a 2a 20 6e 6f 74 20 72 65 6c 6f 61 64 20 74 68  ** not reload th
1d91c 65 20 73 63 68 65 6d 61 20 66 72 6f 6d 20 74 68  e schema from th
1d91d 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
1d91e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49  .    **.    ** I
1d91f 66 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 73  f virtual-tables
1d920 20 61 72 65 20 69 6e 20 75 73 65 2c 20 74 68 69   are in use, thi
1d921 73 20 69 73 20 6e 6f 74 20 6a 75 73 74 20 61 6e  s is not just an
1d922 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 0a 20   optimization.. 
1d923 20 20 20 2a 2a 20 4f 66 74 65 6e 2c 20 76 2d 74     ** Often, v-t
1d924 61 62 6c 65 73 20 73 74 6f 72 65 20 74 68 65 69  ables store thei
1d925 72 20 64 61 74 61 20 69 6e 20 6f 74 68 65 72 20  r data in other 
1d926 53 51 4c 69 74 65 20 74 61 62 6c 65 73 2c 20 77  SQLite tables, w
1d927 68 69 63 68 0a 20 20 20 20 2a 2a 20 61 72 65 20  hich.    ** are 
1d928 71 75 65 72 69 65 64 20 66 72 6f 6d 20 77 69 74  queried from wit
1d929 68 69 6e 20 78 4e 65 78 74 28 29 20 61 6e 64 20  hin xNext() and 
1d92a 6f 74 68 65 72 20 76 2d 74 61 62 6c 65 20 6d 65  other v-table me
1d92b 74 68 6f 64 73 20 75 73 69 6e 67 0a 20 20 20 20  thods using.    
1d92c 2a 2a 20 70 72 65 70 61 72 65 64 20 71 75 65 72  ** prepared quer
1d92d 69 65 73 2e 20 49 66 20 73 75 63 68 20 61 20 71  ies. If such a q
1d92e 75 65 72 79 20 69 73 20 6f 75 74 2d 6f 66 2d 64  uery is out-of-d
1d92f 61 74 65 2c 20 77 65 20 64 6f 20 6e 6f 74 20 77  ate, we do not w
1d930 61 6e 74 20 74 6f 0a 20 20 20 20 2a 2a 20 64 69  ant to.    ** di
1d931 73 63 61 72 64 20 74 68 65 20 64 61 74 61 62 61  scard the databa
1d932 73 65 20 73 63 68 65 6d 61 2c 20 61 73 20 74 68  se schema, as th
1d933 65 20 75 73 65 72 20 63 6f 64 65 20 69 6d 70 6c  e user code impl
1d934 65 6d 65 6e 74 69 6e 67 20 74 68 65 0a 20 20 20  ementing the.   
1d935 20 2a 2a 20 76 2d 74 61 62 6c 65 20 77 6f 75 6c   ** v-table woul
1d936 64 20 68 61 76 65 20 74 6f 20 62 65 20 72 65 61  d have to be rea
1d937 64 79 20 66 6f 72 20 74 68 65 20 73 71 6c 69 74  dy for the sqlit
1d938 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 72  e3_vtab structur
1d939 65 20 69 74 73 65 6c 66 0a 20 20 20 20 2a 2a 20  e itself.    ** 
1d93a 74 6f 20 62 65 20 69 6e 76 61 6c 69 64 61 74 65  to be invalidate
1d93b 64 20 77 68 65 6e 65 76 65 72 20 73 71 6c 69 74  d whenever sqlit
1d93c 65 33 5f 73 74 65 70 28 29 20 69 73 20 63 61 6c  e3_step() is cal
1d93d 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 0a  led from within.
1d93e 20 20 20 20 2a 2a 20 61 20 76 2d 74 61 62 6c 65      ** a v-table
1d93f 20 6d 65 74 68 6f 64 2e 0a 20 20 20 20 2a 2f 0a   method..    */.
1d940 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b      if( db->aDb[
1d941 70 4f 70 2d 3e 70 31 5d 2e 70 53 63 68 65 6d 61  pOp->p1].pSchema
1d942 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 21  ->schema_cookie!
1d943 3d 75 2e 61 76 2e 69 4d 65 74 61 20 29 7b 0a 20  =u.av.iMeta ){. 
1d944 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 65       sqlite3Rese
1d945 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28  tInternalSchema(
1d946 64 62 2c 20 70 4f 70 2d 3e 70 31 29 3b 0a 20 20  db, pOp->p1);.  
1d947 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 33    }..    sqlite3
1d948 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53 74  ExpirePreparedSt
1d949 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20  atements(db);.  
1d94a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 53 43    rc = SQLITE_SC
1d94b 48 45 4d 41 3b 0a 20 20 7d 0a 20 20 62 72 65 61  HEMA;.  }.  brea
1d94c 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1d94d 20 4f 70 65 6e 52 65 61 64 20 50 31 20 50 32 20   OpenRead P1 P2 
1d94e 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 4f  P3 P4 P5.**.** O
1d94f 70 65 6e 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20  pen a read-only 
1d950 63 75 72 73 6f 72 20 66 6f 72 20 74 68 65 20 64  cursor for the d
1d951 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 77 68  atabase table wh
1d952 6f 73 65 20 72 6f 6f 74 20 70 61 67 65 20 69 73  ose root page is
1d953 0a 2a 2a 20 50 32 20 69 6e 20 61 20 64 61 74 61  .** P2 in a data
1d954 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20  base file.  The 
1d955 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73  database file is
1d956 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 50   determined by P
1d957 33 2e 20 0a 2a 2a 20 50 33 3d 3d 30 20 6d 65 61  3. .** P3==0 mea
1d958 6e 73 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  ns the main data
1d959 62 61 73 65 2c 20 50 33 3d 3d 31 20 6d 65 61 6e  base, P3==1 mean
1d95a 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 75  s the database u
1d95b 73 65 64 20 66 6f 72 20 0a 2a 2a 20 74 65 6d 70  sed for .** temp
1d95c 6f 72 61 72 79 20 74 61 62 6c 65 73 2c 20 61 6e  orary tables, an
1d95d 64 20 50 33 3e 31 20 6d 65 61 6e 73 20 75 73 65  d P3>1 means use
1d95e 64 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  d the correspond
1d95f 69 6e 67 20 61 74 74 61 63 68 65 64 0a 2a 2a 20  ing attached.** 
1d960 64 61 74 61 62 61 73 65 2e 20 20 47 69 76 65 20  database.  Give 
1d961 74 68 65 20 6e 65 77 20 63 75 72 73 6f 72 20 61  the new cursor a
1d962 6e 20 69 64 65 6e 74 69 66 69 65 72 20 6f 66 20  n identifier of 
1d963 50 31 2e 20 20 54 68 65 20 50 31 0a 2a 2a 20 76  P1.  The P1.** v
1d964 61 6c 75 65 73 20 6e 65 65 64 20 6e 6f 74 20 62  alues need not b
1d965 65 20 63 6f 6e 74 69 67 75 6f 75 73 20 62 75 74  e contiguous but
1d966 20 61 6c 6c 20 50 31 20 76 61 6c 75 65 73 20 73   all P1 values s
1d967 68 6f 75 6c 64 20 62 65 20 73 6d 61 6c 6c 20 69  hould be small i
1d968 6e 74 65 67 65 72 73 2e 0a 2a 2a 20 49 74 20 69  ntegers..** It i
1d969 73 20 61 6e 20 65 72 72 6f 72 20 66 6f 72 20 50  s an error for P
1d96a 31 20 74 6f 20 62 65 20 6e 65 67 61 74 69 76 65  1 to be negative
1d96b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 21 3d 30  ..**.** If P5!=0
1d96c 20 74 68 65 6e 20 75 73 65 20 74 68 65 20 63 6f   then use the co
1d96d 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65  ntent of registe
1d96e 72 20 50 32 20 61 73 20 74 68 65 20 72 6f 6f 74  r P2 as the root
1d96f 20 70 61 67 65 2c 20 6e 6f 74 0a 2a 2a 20 74 68   page, not.** th
1d970 65 20 76 61 6c 75 65 20 6f 66 20 50 32 20 69 74  e value of P2 it
1d971 73 65 6c 66 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72  self..**.** Ther
1d972 65 20 77 69 6c 6c 20 62 65 20 61 20 72 65 61 64  e will be a read
1d973 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74   lock on the dat
1d974 61 62 61 73 65 20 77 68 65 6e 65 76 65 72 20 74  abase whenever t
1d975 68 65 72 65 20 69 73 20 61 6e 0a 2a 2a 20 6f 70  here is an.** op
1d976 65 6e 20 63 75 72 73 6f 72 2e 20 20 49 66 20 74  en cursor.  If t
1d977 68 65 20 64 61 74 61 62 61 73 65 20 77 61 73 20  he database was 
1d978 75 6e 6c 6f 63 6b 65 64 20 70 72 69 6f 72 20 74  unlocked prior t
1d979 6f 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69  o this instructi
1d97a 6f 6e 0a 2a 2a 20 74 68 65 6e 20 61 20 72 65 61  on.** then a rea
1d97b 64 20 6c 6f 63 6b 20 69 73 20 61 63 71 75 69 72  d lock is acquir
1d97c 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68  ed as part of th
1d97d 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20  is instruction. 
1d97e 20 41 20 72 65 61 64 0a 2a 2a 20 6c 6f 63 6b 20   A read.** lock 
1d97f 61 6c 6c 6f 77 73 20 6f 74 68 65 72 20 70 72 6f  allows other pro
1d980 63 65 73 73 65 73 20 74 6f 20 72 65 61 64 20 74  cesses to read t
1d981 68 65 20 64 61 74 61 62 61 73 65 20 62 75 74 20  he database but 
1d982 70 72 6f 68 69 62 69 74 73 0a 2a 2a 20 61 6e 79  prohibits.** any
1d983 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 66   other process f
1d984 72 6f 6d 20 6d 6f 64 69 66 79 69 6e 67 20 74 68  rom modifying th
1d985 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 65  e database.  The
1d986 20 72 65 61 64 20 6c 6f 63 6b 20 69 73 0a 2a 2a   read lock is.**
1d987 20 72 65 6c 65 61 73 65 64 20 77 68 65 6e 20 61   released when a
1d988 6c 6c 20 63 75 72 73 6f 72 73 20 61 72 65 20 63  ll cursors are c
1d989 6c 6f 73 65 64 2e 20 20 49 66 20 74 68 69 73 20  losed.  If this 
1d98a 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 74 74 65  instruction atte
1d98b 6d 70 74 73 0a 2a 2a 20 74 6f 20 67 65 74 20 61  mpts.** to get a
1d98c 20 72 65 61 64 20 6c 6f 63 6b 20 62 75 74 20 66   read lock but f
1d98d 61 69 6c 73 2c 20 74 68 65 20 73 63 72 69 70 74  ails, the script
1d98e 20 74 65 72 6d 69 6e 61 74 65 73 20 77 69 74 68   terminates with
1d98f 20 61 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55   an.** SQLITE_BU
1d990 53 59 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a  SY error code..*
1d991 2a 0a 2a 2a 20 54 68 65 20 50 34 20 76 61 6c 75  *.** The P4 valu
1d992 65 20 6d 61 79 20 62 65 20 65 69 74 68 65 72 20  e may be either 
1d993 61 6e 20 69 6e 74 65 67 65 72 20 28 50 34 5f 49  an integer (P4_I
1d994 4e 54 33 32 29 20 6f 72 20 61 20 70 6f 69 6e 74  NT32) or a point
1d995 65 72 20 74 6f 0a 2a 2a 20 61 20 4b 65 79 49 6e  er to.** a KeyIn
1d996 66 6f 20 73 74 72 75 63 74 75 72 65 20 28 50 34  fo structure (P4
1d997 5f 4b 45 59 49 4e 46 4f 29 2e 20 49 66 20 69 74  _KEYINFO). If it
1d998 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
1d999 20 61 20 4b 65 79 49 6e 66 6f 20 0a 2a 2a 20 73   a KeyInfo .** s
1d99a 74 72 75 63 74 75 72 65 2c 20 74 68 65 6e 20 73  tructure, then s
1d99b 61 69 64 20 73 74 72 75 63 74 75 72 65 20 64 65  aid structure de
1d99c 66 69 6e 65 73 20 74 68 65 20 63 6f 6e 74 65 6e  fines the conten
1d99d 74 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6e 67 20  t and collating 
1d99e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 6f 66 20  .** sequence of 
1d99f 74 68 65 20 69 6e 64 65 78 20 62 65 69 6e 67 20  the index being 
1d9a0 6f 70 65 6e 65 64 2e 20 4f 74 68 65 72 77 69 73  opened. Otherwis
1d9a1 65 2c 20 69 66 20 50 34 20 69 73 20 61 6e 20 69  e, if P4 is an i
1d9a2 6e 74 65 67 65 72 20 0a 2a 2a 20 76 61 6c 75 65  nteger .** value
1d9a3 2c 20 69 74 20 69 73 20 73 65 74 20 74 6f 20 74  , it is set to t
1d9a4 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  he number of col
1d9a5 75 6d 6e 73 20 69 6e 20 74 68 65 20 74 61 62 6c  umns in the tabl
1d9a6 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  e..**.** See als
1d9a7 6f 20 4f 70 65 6e 57 72 69 74 65 2e 0a 2a 2f 0a  o OpenWrite..*/.
1d9a8 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 65 6e 57  /* Opcode: OpenW
1d9a9 72 69 74 65 20 50 31 20 50 32 20 50 33 20 50 34  rite P1 P2 P3 P4
1d9aa 20 50 35 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61   P5.**.** Open a
1d9ab 20 72 65 61 64 2f 77 72 69 74 65 20 63 75 72 73   read/write curs
1d9ac 6f 72 20 6e 61 6d 65 64 20 50 31 20 6f 6e 20 74  or named P1 on t
1d9ad 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65  he table or inde
1d9ae 78 20 77 68 6f 73 65 20 72 6f 6f 74 0a 2a 2a 20  x whose root.** 
1d9af 70 61 67 65 20 69 73 20 50 32 2e 20 20 4f 72 20  page is P2.  Or 
1d9b0 69 66 20 50 35 21 3d 30 20 75 73 65 20 74 68 65  if P5!=0 use the
1d9b1 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69   content of regi
1d9b2 73 74 65 72 20 50 32 20 74 6f 20 66 69 6e 64 20  ster P2 to find 
1d9b3 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65  the.** root page
1d9b4 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 20 76  ..**.** The P4 v
1d9b5 61 6c 75 65 20 6d 61 79 20 62 65 20 65 69 74 68  alue may be eith
1d9b6 65 72 20 61 6e 20 69 6e 74 65 67 65 72 20 28 50  er an integer (P
1d9b7 34 5f 49 4e 54 33 32 29 20 6f 72 20 61 20 70 6f  4_INT32) or a po
1d9b8 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 61 20 4b 65  inter to.** a Ke
1d9b9 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  yInfo structure 
1d9ba 28 50 34 5f 4b 45 59 49 4e 46 4f 29 2e 20 49 66  (P4_KEYINFO). If
1d9bb 20 69 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72   it is a pointer
1d9bc 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 0a 2a   to a KeyInfo .*
1d9bd 2a 20 73 74 72 75 63 74 75 72 65 2c 20 74 68 65  * structure, the
1d9be 6e 20 73 61 69 64 20 73 74 72 75 63 74 75 72 65  n said structure
1d9bf 20 64 65 66 69 6e 65 73 20 74 68 65 20 63 6f 6e   defines the con
1d9c0 74 65 6e 74 20 61 6e 64 20 63 6f 6c 6c 61 74 69  tent and collati
1d9c1 6e 67 20 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20  ng .** sequence 
1d9c2 6f 66 20 74 68 65 20 69 6e 64 65 78 20 62 65 69  of the index bei
1d9c3 6e 67 20 6f 70 65 6e 65 64 2e 20 4f 74 68 65 72  ng opened. Other
1d9c4 77 69 73 65 2c 20 69 66 20 50 34 20 69 73 20 61  wise, if P4 is a
1d9c5 6e 20 69 6e 74 65 67 65 72 20 0a 2a 2a 20 76 61  n integer .** va
1d9c6 6c 75 65 2c 20 69 74 20 69 73 20 73 65 74 20 74  lue, it is set t
1d9c7 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  o the number of 
1d9c8 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 74  columns in the t
1d9c9 61 62 6c 65 2c 20 6f 72 20 74 6f 20 74 68 65 0a  able, or to the.
1d9ca 2a 2a 20 6c 61 72 67 65 73 74 20 69 6e 64 65 78  ** largest index
1d9cb 20 6f 66 20 61 6e 79 20 63 6f 6c 75 6d 6e 20 6f   of any column o
1d9cc 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74  f the table that
1d9cd 20 69 73 20 61 63 74 75 61 6c 6c 79 20 75 73 65   is actually use
1d9ce 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  d..**.** This in
1d9cf 73 74 72 75 63 74 69 6f 6e 20 77 6f 72 6b 73 20  struction works 
1d9d0 6a 75 73 74 20 6c 69 6b 65 20 4f 70 65 6e 52 65  just like OpenRe
1d9d1 61 64 20 65 78 63 65 70 74 20 74 68 61 74 20 69  ad except that i
1d9d2 74 20 6f 70 65 6e 73 20 74 68 65 20 63 75 72 73  t opens the curs
1d9d3 6f 72 0a 2a 2a 20 69 6e 20 72 65 61 64 2f 77 72  or.** in read/wr
1d9d4 69 74 65 20 6d 6f 64 65 2e 20 20 46 6f 72 20 61  ite mode.  For a
1d9d5 20 67 69 76 65 6e 20 74 61 62 6c 65 2c 20 74 68   given table, th
1d9d6 65 72 65 20 63 61 6e 20 62 65 20 6f 6e 65 20 6f  ere can be one o
1d9d7 72 20 6d 6f 72 65 20 72 65 61 64 2d 6f 6e 6c 79  r more read-only
1d9d8 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f 72 20 61  .** cursors or a
1d9d9 20 73 69 6e 67 6c 65 20 72 65 61 64 2f 77 72 69   single read/wri
1d9da 74 65 20 63 75 72 73 6f 72 20 62 75 74 20 6e 6f  te cursor but no
1d9db 74 20 62 6f 74 68 2e 0a 2a 2a 0a 2a 2a 20 53 65  t both..**.** Se
1d9dc 65 20 61 6c 73 6f 20 4f 70 65 6e 52 65 61 64 2e  e also OpenRead.
1d9dd 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f 70 65 6e  .*/.case OP_Open
1d9de 52 65 61 64 3a 0a 63 61 73 65 20 4f 50 5f 4f 70  Read:.case OP_Op
1d9df 65 6e 57 72 69 74 65 3a 20 7b 0a 23 69 66 20 30  enWrite: {.#if 0
1d9e0 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
1d9e1 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1d9e2 75 2e 61 77 20 2a 2f 0a 20 20 69 6e 74 20 6e 46  u.aw */.  int nF
1d9e3 69 65 6c 64 3b 0a 20 20 4b 65 79 49 6e 66 6f 20  ield;.  KeyInfo 
1d9e4 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 69 6e 74  *pKeyInfo;.  int
1d9e5 20 70 32 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a   p2;.  int iDb;.
1d9e6 20 20 69 6e 74 20 77 72 46 6c 61 67 3b 0a 20 20    int wrFlag;.  
1d9e7 42 74 72 65 65 20 2a 70 58 3b 0a 20 20 56 64 62  Btree *pX;.  Vdb
1d9e8 65 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20  eCursor *pCur;. 
1d9e9 20 44 62 20 2a 70 44 62 3b 0a 23 65 6e 64 69 66   Db *pDb;.#endif
1d9ea 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1d9eb 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1d9ec 2e 61 77 20 2a 2f 0a 0a 20 20 75 2e 61 77 2e 6e  .aw */..  u.aw.n
1d9ed 46 69 65 6c 64 20 3d 20 30 3b 0a 20 20 75 2e 61  Field = 0;.  u.a
1d9ee 77 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 30 3b 0a  w.pKeyInfo = 0;.
1d9ef 20 20 75 2e 61 77 2e 70 32 20 3d 20 70 4f 70 2d    u.aw.p2 = pOp-
1d9f0 3e 70 32 3b 0a 20 20 75 2e 61 77 2e 69 44 62 20  >p2;.  u.aw.iDb 
1d9f1 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 61 73 73  = pOp->p3;.  ass
1d9f2 65 72 74 28 20 75 2e 61 77 2e 69 44 62 3e 3d 30  ert( u.aw.iDb>=0
1d9f3 20 26 26 20 75 2e 61 77 2e 69 44 62 3c 64 62 2d   && u.aw.iDb<db-
1d9f4 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74  >nDb );.  assert
1d9f5 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20  ( (p->btreeMask 
1d9f6 26 20 28 31 3c 3c 75 2e 61 77 2e 69 44 62 29 29  & (1<<u.aw.iDb))
1d9f7 21 3d 30 20 29 3b 0a 20 20 75 2e 61 77 2e 70 44  !=0 );.  u.aw.pD
1d9f8 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 75 2e 61  b = &db->aDb[u.a
1d9f9 77 2e 69 44 62 5d 3b 0a 20 20 75 2e 61 77 2e 70  w.iDb];.  u.aw.p
1d9fa 58 20 3d 20 75 2e 61 77 2e 70 44 62 2d 3e 70 42  X = u.aw.pDb->pB
1d9fb 74 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61  t;.  assert( u.a
1d9fc 77 2e 70 58 21 3d 30 20 29 3b 0a 20 20 69 66 28  w.pX!=0 );.  if(
1d9fd 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
1d9fe 5f 4f 70 65 6e 57 72 69 74 65 20 29 7b 0a 20 20  _OpenWrite ){.  
1d9ff 20 20 75 2e 61 77 2e 77 72 46 6c 61 67 20 3d 20    u.aw.wrFlag = 
1da00 31 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 77 2e  1;.    if( u.aw.
1da01 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69  pDb->pSchema->fi
1da02 6c 65 5f 66 6f 72 6d 61 74 20 3c 20 70 2d 3e 6d  le_format < p->m
1da03 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 6d 61  inWriteFileForma
1da04 74 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6d 69  t ){.      p->mi
1da05 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 6d 61 74  nWriteFileFormat
1da06 20 3d 20 75 2e 61 77 2e 70 44 62 2d 3e 70 53 63   = u.aw.pDb->pSc
1da07 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61  hema->file_forma
1da08 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  t;.    }.  }else
1da09 7b 0a 20 20 20 20 75 2e 61 77 2e 77 72 46 6c 61  {.    u.aw.wrFla
1da0a 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28  g = 0;.  }.  if(
1da0b 20 70 4f 70 2d 3e 70 35 20 29 7b 0a 20 20 20 20   pOp->p5 ){.    
1da0c 61 73 73 65 72 74 28 20 75 2e 61 77 2e 70 32 3e  assert( u.aw.p2>
1da0d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
1da0e 20 75 2e 61 77 2e 70 32 3c 3d 70 2d 3e 6e 4d 65   u.aw.p2<=p->nMe
1da0f 6d 20 29 3b 0a 20 20 20 20 70 49 6e 32 20 3d 20  m );.    pIn2 = 
1da10 26 70 2d 3e 61 4d 65 6d 5b 75 2e 61 77 2e 70 32  &p->aMem[u.aw.p2
1da11 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ];.    sqlite3Vd
1da12 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28  beMemIntegerify(
1da13 70 49 6e 32 29 3b 0a 20 20 20 20 75 2e 61 77 2e  pIn2);.    u.aw.
1da14 70 32 20 3d 20 28 69 6e 74 29 70 49 6e 32 2d 3e  p2 = (int)pIn2->
1da15 75 2e 69 3b 0a 20 20 20 20 2f 2a 20 54 68 65 20  u.i;.    /* The 
1da16 75 2e 61 77 2e 70 32 20 76 61 6c 75 65 20 61 6c  u.aw.p2 value al
1da17 77 61 79 73 20 63 6f 6d 65 73 20 66 72 6f 6d 20  ways comes from 
1da18 61 20 70 72 69 6f 72 20 4f 50 5f 43 72 65 61 74  a prior OP_Creat
1da19 65 54 61 62 6c 65 20 6f 70 63 6f 64 65 20 61 6e  eTable opcode an
1da1a 64 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 6f 70  d.    ** that op
1da1b 63 6f 64 65 20 77 69 6c 6c 20 61 6c 77 61 79 73  code will always
1da1c 20 73 65 74 20 74 68 65 20 75 2e 61 77 2e 70 32   set the u.aw.p2
1da1d 20 76 61 6c 75 65 20 74 6f 20 32 20 6f 72 20 6d   value to 2 or m
1da1e 6f 72 65 20 6f 72 20 65 6c 73 65 20 66 61 69 6c  ore or else fail
1da1f 2e 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 72  ..    ** If ther
1da20 65 20 77 65 72 65 20 61 20 66 61 69 6c 75 72 65  e were a failure
1da21 2c 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73  , the prepared s
1da22 74 61 74 65 6d 65 6e 74 20 77 6f 75 6c 64 20 68  tatement would h
1da23 61 76 65 20 68 61 6c 74 65 64 0a 20 20 20 20 2a  ave halted.    *
1da24 2a 20 62 65 66 6f 72 65 20 72 65 61 63 68 69 6e  * before reachin
1da25 67 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69  g this instructi
1da26 6f 6e 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 4e  on. */.    if( N
1da27 45 56 45 52 28 75 2e 61 77 2e 70 32 3c 32 29 20  EVER(u.aw.p2<2) 
1da28 29 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53  ) {.      rc = S
1da29 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
1da2a 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61  PT;.      goto a
1da2b 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f  bort_due_to_erro
1da2c 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  r;.    }.  }.  i
1da2d 66 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d  f( pOp->p4type==
1da2e 50 34 5f 4b 45 59 49 4e 46 4f 20 29 7b 0a 20 20  P4_KEYINFO ){.  
1da2f 20 20 75 2e 61 77 2e 70 4b 65 79 49 6e 66 6f 20    u.aw.pKeyInfo 
1da30 3d 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e  = pOp->p4.pKeyIn
1da31 66 6f 3b 0a 20 20 20 20 75 2e 61 77 2e 70 4b 65  fo;.    u.aw.pKe
1da32 79 49 6e 66 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43  yInfo->enc = ENC
1da33 28 70 2d 3e 64 62 29 3b 0a 20 20 20 20 75 2e 61  (p->db);.    u.a
1da34 77 2e 6e 46 69 65 6c 64 20 3d 20 75 2e 61 77 2e  w.nField = u.aw.
1da35 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64  pKeyInfo->nField
1da36 2b 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  +1;.  }else if( 
1da37 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f  pOp->p4type==P4_
1da38 49 4e 54 33 32 20 29 7b 0a 20 20 20 20 75 2e 61  INT32 ){.    u.a
1da39 77 2e 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e  w.nField = pOp->
1da3a 70 34 2e 69 3b 0a 20 20 7d 0a 20 20 61 73 73 65  p4.i;.  }.  asse
1da3b 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 29  rt( pOp->p1>=0 )
1da3c 3b 0a 20 20 75 2e 61 77 2e 70 43 75 72 20 3d 20  ;.  u.aw.pCur = 
1da3d 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70  allocateCursor(p
1da3e 2c 20 70 4f 70 2d 3e 70 31 2c 20 75 2e 61 77 2e  , pOp->p1, u.aw.
1da3f 6e 46 69 65 6c 64 2c 20 75 2e 61 77 2e 69 44 62  nField, u.aw.iDb
1da40 2c 20 31 29 3b 0a 20 20 69 66 28 20 75 2e 61 77  , 1);.  if( u.aw
1da41 2e 70 43 75 72 3d 3d 30 20 29 20 67 6f 74 6f 20  .pCur==0 ) goto 
1da42 6e 6f 5f 6d 65 6d 3b 0a 20 20 75 2e 61 77 2e 70  no_mem;.  u.aw.p
1da43 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31  Cur->nullRow = 1
1da44 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
1da45 42 74 72 65 65 43 75 72 73 6f 72 28 75 2e 61 77  BtreeCursor(u.aw
1da46 2e 70 58 2c 20 75 2e 61 77 2e 70 32 2c 20 75 2e  .pX, u.aw.p2, u.
1da47 61 77 2e 77 72 46 6c 61 67 2c 20 75 2e 61 77 2e  aw.wrFlag, u.aw.
1da48 70 4b 65 79 49 6e 66 6f 2c 20 75 2e 61 77 2e 70  pKeyInfo, u.aw.p
1da49 43 75 72 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20  Cur->pCursor);. 
1da4a 20 75 2e 61 77 2e 70 43 75 72 2d 3e 70 4b 65 79   u.aw.pCur->pKey
1da4b 49 6e 66 6f 20 3d 20 75 2e 61 77 2e 70 4b 65 79  Info = u.aw.pKey
1da4c 49 6e 66 6f 3b 0a 0a 20 20 2f 2a 20 53 69 6e 63  Info;..  /* Sinc
1da4d 65 20 69 74 20 70 65 72 66 6f 72 6d 73 20 6e 6f  e it performs no
1da4e 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
1da4f 6f 6e 20 6f 72 20 49 4f 2c 20 74 68 65 20 6f 6e  on or IO, the on
1da50 6c 79 20 76 61 6c 75 65 73 20 74 68 61 74 0a 20  ly values that. 
1da51 20 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65   ** sqlite3Btree
1da52 43 75 72 73 6f 72 28 29 20 6d 61 79 20 72 65 74  Cursor() may ret
1da53 75 72 6e 20 61 72 65 20 53 51 4c 49 54 45 5f 45  urn are SQLITE_E
1da54 4d 50 54 59 20 61 6e 64 20 53 51 4c 49 54 45 5f  MPTY and SQLITE_
1da55 4f 4b 2e 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f  OK..  ** SQLITE_
1da56 45 4d 50 54 59 20 69 73 20 6f 6e 6c 79 20 72 65  EMPTY is only re
1da57 74 75 72 6e 65 64 20 77 68 65 6e 20 61 74 74 65  turned when atte
1da58 6d 70 74 69 6e 67 20 74 6f 20 6f 70 65 6e 20 74  mpting to open t
1da59 68 65 20 74 61 62 6c 65 0a 20 20 2a 2a 20 72 6f  he table.  ** ro
1da5a 6f 74 65 64 20 61 74 20 70 61 67 65 20 31 20 6f  oted at page 1 o
1da5b 66 20 61 20 7a 65 72 6f 2d 62 79 74 65 20 64 61  f a zero-byte da
1da5c 74 61 62 61 73 65 2e 20 20 2a 2f 0a 20 20 61 73  tabase.  */.  as
1da5d 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
1da5e 5f 45 4d 50 54 59 20 7c 7c 20 72 63 3d 3d 53 51  _EMPTY || rc==SQ
1da5f 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69 66 28  LITE_OK );.  if(
1da60 20 72 63 3d 3d 53 51 4c 49 54 45 5f 45 4d 50 54   rc==SQLITE_EMPT
1da61 59 20 29 7b 0a 20 20 20 20 75 2e 61 77 2e 70 43  Y ){.    u.aw.pC
1da62 75 72 2d 3e 70 43 75 72 73 6f 72 20 3d 20 30 3b  ur->pCursor = 0;
1da63 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
1da64 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53  _OK;.  }..  /* S
1da65 65 74 20 74 68 65 20 56 64 62 65 43 75 72 73 6f  et the VdbeCurso
1da66 72 2e 69 73 54 61 62 6c 65 20 61 6e 64 20 69 73  r.isTable and is
1da67 49 6e 64 65 78 20 76 61 72 69 61 62 6c 65 73 2e  Index variables.
1da68 20 50 72 65 76 69 6f 75 73 20 76 65 72 73 69 6f   Previous versio
1da69 6e 73 20 6f 66 0a 20 20 2a 2a 20 53 51 4c 69 74  ns of.  ** SQLit
1da6a 65 20 75 73 65 64 20 74 6f 20 63 68 65 63 6b 20  e used to check 
1da6b 69 66 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65  if the root-page
1da6c 20 66 6c 61 67 73 20 77 65 72 65 20 73 61 6e 65   flags were sane
1da6d 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 0a 20   at this point. 
1da6e 20 2a 2a 20 61 6e 64 20 72 65 70 6f 72 74 20 64   ** and report d
1da6f 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69  atabase corrupti
1da70 6f 6e 20 69 66 20 74 68 65 79 20 77 65 72 65 20  on if they were 
1da71 6e 6f 74 2c 20 62 75 74 20 74 68 69 73 20 63 68  not, but this ch
1da72 65 63 6b 20 68 61 73 0a 20 20 2a 2a 20 73 69 6e  eck has.  ** sin
1da73 63 65 20 6d 6f 76 65 64 20 69 6e 74 6f 20 74 68  ce moved into th
1da74 65 20 62 74 72 65 65 20 6c 61 79 65 72 2e 20 20  e btree layer.  
1da75 2a 2f 0a 20 20 75 2e 61 77 2e 70 43 75 72 2d 3e  */.  u.aw.pCur->
1da76 69 73 54 61 62 6c 65 20 3d 20 70 4f 70 2d 3e 70  isTable = pOp->p
1da77 34 74 79 70 65 21 3d 50 34 5f 4b 45 59 49 4e 46  4type!=P4_KEYINF
1da78 4f 3b 0a 20 20 75 2e 61 77 2e 70 43 75 72 2d 3e  O;.  u.aw.pCur->
1da79 69 73 49 6e 64 65 78 20 3d 20 21 75 2e 61 77 2e  isIndex = !u.aw.
1da7a 70 43 75 72 2d 3e 69 73 54 61 62 6c 65 3b 0a 20  pCur->isTable;. 
1da7b 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1da7c 63 6f 64 65 3a 20 4f 70 65 6e 45 70 68 65 6d 65  code: OpenEpheme
1da7d 72 61 6c 20 50 31 20 50 32 20 2a 20 50 34 20 2a  ral P1 P2 * P4 *
1da7e 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e 65  .**.** Open a ne
1da7f 77 20 63 75 72 73 6f 72 20 50 31 20 74 6f 20 61  w cursor P1 to a
1da80 20 74 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65   transient table
1da81 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 20  ..** The cursor 
1da82 69 73 20 61 6c 77 61 79 73 20 6f 70 65 6e 65 64  is always opened
1da83 20 72 65 61 64 2f 77 72 69 74 65 20 65 76 65 6e   read/write even
1da84 20 69 66 20 0a 2a 2a 20 74 68 65 20 6d 61 69 6e   if .** the main
1da85 20 64 61 74 61 62 61 73 65 20 69 73 20 72 65 61   database is rea
1da86 64 2d 6f 6e 6c 79 2e 20 20 54 68 65 20 74 72 61  d-only.  The tra
1da87 6e 73 69 65 6e 74 20 6f 72 20 76 69 72 74 75 61  nsient or virtua
1da88 6c 0a 2a 2a 20 74 61 62 6c 65 20 69 73 20 64 65  l.** table is de
1da89 6c 65 74 65 64 20 61 75 74 6f 6d 61 74 69 63 61  leted automatica
1da8a 6c 6c 79 20 77 68 65 6e 20 74 68 65 20 63 75 72  lly when the cur
1da8b 73 6f 72 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a  sor is closed..*
1da8c 2a 0a 2a 2a 20 50 32 20 69 73 20 74 68 65 20 6e  *.** P2 is the n
1da8d 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
1da8e 20 69 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20   in the virtual 
1da8f 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 75  table..** The cu
1da90 72 73 6f 72 20 70 6f 69 6e 74 73 20 74 6f 20 61  rsor points to a
1da91 20 42 54 72 65 65 20 74 61 62 6c 65 20 69 66 20   BTree table if 
1da92 50 34 3d 3d 30 20 61 6e 64 20 74 6f 20 61 20 42  P4==0 and to a B
1da93 54 72 65 65 20 69 6e 64 65 78 0a 2a 2a 20 69 66  Tree index.** if
1da94 20 50 34 20 69 73 20 6e 6f 74 20 30 2e 20 20 49   P4 is not 0.  I
1da95 66 20 50 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  f P4 is not NULL
1da96 2c 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 61  , it points to a
1da97 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75   KeyInfo structu
1da98 72 65 0a 2a 2a 20 74 68 61 74 20 64 65 66 69 6e  re.** that defin
1da99 65 73 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66  es the format of
1da9a 20 6b 65 79 73 20 69 6e 20 74 68 65 20 69 6e 64   keys in the ind
1da9b 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f  ex..**.** This o
1da9c 70 63 6f 64 65 20 77 61 73 20 6f 6e 63 65 20 63  pcode was once c
1da9d 61 6c 6c 65 64 20 4f 70 65 6e 54 65 6d 70 2e 20  alled OpenTemp. 
1da9e 20 42 75 74 20 74 68 61 74 20 63 72 65 61 74 65   But that create
1da9f 64 0a 2a 2a 20 63 6f 6e 66 75 73 69 6f 6e 20 62  d.** confusion b
1daa0 65 63 61 75 73 65 20 74 68 65 20 74 65 72 6d 20  ecause the term 
1daa1 22 74 65 6d 70 20 74 61 62 6c 65 22 2c 20 6d 69  "temp table", mi
1daa2 67 68 74 20 72 65 66 65 72 20 65 69 74 68 65 72  ght refer either
1daa3 0a 2a 2a 20 74 6f 20 61 20 54 45 4d 50 20 74 61  .** to a TEMP ta
1daa4 62 6c 65 20 61 74 20 74 68 65 20 53 51 4c 20 6c  ble at the SQL l
1daa5 65 76 65 6c 2c 20 6f 72 20 74 6f 20 61 20 74 61  evel, or to a ta
1daa6 62 6c 65 20 6f 70 65 6e 65 64 20 62 79 0a 2a 2a  ble opened by.**
1daa7 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 20 20 54   this opcode.  T
1daa8 68 65 6e 20 74 68 69 73 20 6f 70 63 6f 64 65 20  hen this opcode 
1daa9 77 61 73 20 63 61 6c 6c 20 4f 70 65 6e 56 69 72  was call OpenVir
1daaa 74 75 61 6c 2e 20 20 42 75 74 0a 2a 2a 20 74 68  tual.  But.** th
1daab 61 74 20 63 72 65 61 74 65 64 20 63 6f 6e 66 75  at created confu
1daac 73 69 6f 6e 20 77 69 74 68 20 74 68 65 20 77 68  sion with the wh
1daad 6f 6c 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c  ole virtual-tabl
1daae 65 20 69 64 65 61 2e 0a 2a 2f 0a 63 61 73 65 20  e idea..*/.case 
1daaf 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  OP_OpenEphemeral
1dab0 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f  : {.#if 0  /* lo
1dab1 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1dab2 76 65 64 20 69 6e 74 6f 20 75 2e 61 78 20 2a 2f  ved into u.ax */
1dab3 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70  .  VdbeCursor *p
1dab4 43 78 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f  Cx;.#endif /* lo
1dab5 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1dab6 76 65 64 20 69 6e 74 6f 20 75 2e 61 78 20 2a 2f  ved into u.ax */
1dab7 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
1dab8 69 6e 74 20 6f 70 65 6e 46 6c 61 67 73 20 3d 0a  int openFlags =.
1dab9 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45        SQLITE_OPE
1daba 4e 5f 52 45 41 44 57 52 49 54 45 20 7c 0a 20 20  N_READWRITE |.  
1dabb 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f      SQLITE_OPEN_
1dabc 43 52 45 41 54 45 20 7c 0a 20 20 20 20 20 20 53  CREATE |.      S
1dabd 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55  QLITE_OPEN_EXCLU
1dabe 53 49 56 45 20 7c 0a 20 20 20 20 20 20 53 51 4c  SIVE |.      SQL
1dabf 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f  ITE_OPEN_DELETEO
1dac0 4e 43 4c 4f 53 45 20 7c 0a 20 20 20 20 20 20 53  NCLOSE |.      S
1dac1 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53  QLITE_OPEN_TRANS
1dac2 49 45 4e 54 5f 44 42 3b 0a 0a 20 20 61 73 73 65  IENT_DB;..  asse
1dac3 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 29  rt( pOp->p1>=0 )
1dac4 3b 0a 20 20 75 2e 61 78 2e 70 43 78 20 3d 20 61  ;.  u.ax.pCx = a
1dac5 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70 2c  llocateCursor(p,
1dac6 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70   pOp->p1, pOp->p
1dac7 32 2c 20 2d 31 2c 20 31 29 3b 0a 20 20 69 66 28  2, -1, 1);.  if(
1dac8 20 75 2e 61 78 2e 70 43 78 3d 3d 30 20 29 20 67   u.ax.pCx==0 ) g
1dac9 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 75 2e  oto no_mem;.  u.
1daca 61 78 2e 70 43 78 2d 3e 6e 75 6c 6c 52 6f 77 20  ax.pCx->nullRow 
1dacb 3d 20 31 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  = 1;.  rc = sqli
1dacc 74 65 33 42 74 72 65 65 46 61 63 74 6f 72 79 28  te3BtreeFactory(
1dacd 64 62 2c 20 30 2c 20 31 2c 20 53 51 4c 49 54 45  db, 0, 1, SQLITE
1dace 5f 44 45 46 41 55 4c 54 5f 54 45 4d 50 5f 43 41  _DEFAULT_TEMP_CA
1dacf 43 48 45 5f 53 49 5a 45 2c 20 6f 70 65 6e 46 6c  CHE_SIZE, openFl
1dad0 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ags,.           
1dad1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dad2 26 75 2e 61 78 2e 70 43 78 2d 3e 70 42 74 29 3b  &u.ax.pCx->pBt);
1dad3 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
1dad4 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d  E_OK ){.    rc =
1dad5 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67   sqlite3BtreeBeg
1dad6 69 6e 54 72 61 6e 73 28 75 2e 61 78 2e 70 43 78  inTrans(u.ax.pCx
1dad7 2d 3e 70 42 74 2c 20 31 29 3b 0a 20 20 7d 0a 20  ->pBt, 1);.  }. 
1dad8 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1dad9 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20  OK ){.    /* If 
1dada 61 20 74 72 61 6e 73 69 65 6e 74 20 69 6e 64 65  a transient inde
1dadb 78 20 69 73 20 72 65 71 75 69 72 65 64 2c 20 63  x is required, c
1dadc 72 65 61 74 65 20 69 74 20 62 79 20 63 61 6c 6c  reate it by call
1dadd 69 6e 67 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74  ing.    ** sqlit
1dade 65 33 42 74 72 65 65 43 72 65 61 74 65 54 61 62  e3BtreeCreateTab
1dadf 6c 65 28 29 20 77 69 74 68 20 74 68 65 20 42 54  le() with the BT
1dae0 52 45 45 5f 5a 45 52 4f 44 41 54 41 20 66 6c 61  REE_ZERODATA fla
1dae1 67 20 62 65 66 6f 72 65 0a 20 20 20 20 2a 2a 20  g before.    ** 
1dae2 6f 70 65 6e 69 6e 67 20 69 74 2e 20 49 66 20 61  opening it. If a
1dae3 20 74 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65   transient table
1dae4 20 69 73 20 72 65 71 75 69 72 65 64 2c 20 6a 75   is required, ju
1dae5 73 74 20 75 73 65 20 74 68 65 0a 20 20 20 20 2a  st use the.    *
1dae6 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  * automatically 
1dae7 63 72 65 61 74 65 64 20 74 61 62 6c 65 20 77 69  created table wi
1dae8 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 31 20 28  th root-page 1 (
1dae9 61 6e 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 29  an INTKEY table)
1daea 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
1daeb 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66   pOp->p4.pKeyInf
1daec 6f 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 70  o ){.      int p
1daed 67 6e 6f 3b 0a 20 20 20 20 20 20 61 73 73 65 72  gno;.      asser
1daee 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d  t( pOp->p4type==
1daef 50 34 5f 4b 45 59 49 4e 46 4f 20 29 3b 0a 20 20  P4_KEYINFO );.  
1daf0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1daf1 42 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 65  BtreeCreateTable
1daf2 28 75 2e 61 78 2e 70 43 78 2d 3e 70 42 74 2c 20  (u.ax.pCx->pBt, 
1daf3 26 70 67 6e 6f 2c 20 42 54 52 45 45 5f 5a 45 52  &pgno, BTREE_ZER
1daf4 4f 44 41 54 41 29 3b 0a 20 20 20 20 20 20 69 66  ODATA);.      if
1daf5 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1daf6 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ){.        asser
1daf7 74 28 20 70 67 6e 6f 3d 3d 4d 41 53 54 45 52 5f  t( pgno==MASTER_
1daf8 52 4f 4f 54 2b 31 20 29 3b 0a 20 20 20 20 20 20  ROOT+1 );.      
1daf9 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1dafa 72 65 65 43 75 72 73 6f 72 28 75 2e 61 78 2e 70  reeCursor(u.ax.p
1dafb 43 78 2d 3e 70 42 74 2c 20 70 67 6e 6f 2c 20 31  Cx->pBt, pgno, 1
1dafc 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1dafd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dafe 20 20 28 4b 65 79 49 6e 66 6f 2a 29 70 4f 70 2d    (KeyInfo*)pOp-
1daff 3e 70 34 2e 7a 2c 20 75 2e 61 78 2e 70 43 78 2d  >p4.z, u.ax.pCx-
1db00 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20  >pCursor);.     
1db01 20 20 20 75 2e 61 78 2e 70 43 78 2d 3e 70 4b 65     u.ax.pCx->pKe
1db02 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e  yInfo = pOp->p4.
1db03 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20  pKeyInfo;.      
1db04 20 20 75 2e 61 78 2e 70 43 78 2d 3e 70 4b 65 79    u.ax.pCx->pKey
1db05 49 6e 66 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28  Info->enc = ENC(
1db06 70 2d 3e 64 62 29 3b 0a 20 20 20 20 20 20 7d 0a  p->db);.      }.
1db07 20 20 20 20 20 20 75 2e 61 78 2e 70 43 78 2d 3e        u.ax.pCx->
1db08 69 73 54 61 62 6c 65 20 3d 20 30 3b 0a 20 20 20  isTable = 0;.   
1db09 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63   }else{.      rc
1db0a 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43   = sqlite3BtreeC
1db0b 75 72 73 6f 72 28 75 2e 61 78 2e 70 43 78 2d 3e  ursor(u.ax.pCx->
1db0c 70 42 74 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54  pBt, MASTER_ROOT
1db0d 2c 20 31 2c 20 30 2c 20 75 2e 61 78 2e 70 43 78  , 1, 0, u.ax.pCx
1db0e 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20  ->pCursor);.    
1db0f 20 20 75 2e 61 78 2e 70 43 78 2d 3e 69 73 54 61    u.ax.pCx->isTa
1db10 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  ble = 1;.    }. 
1db11 20 7d 0a 20 20 75 2e 61 78 2e 70 43 78 2d 3e 69   }.  u.ax.pCx->i
1db12 73 49 6e 64 65 78 20 3d 20 21 75 2e 61 78 2e 70  sIndex = !u.ax.p
1db13 43 78 2d 3e 69 73 54 61 62 6c 65 3b 0a 20 20 62  Cx->isTable;.  b
1db14 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1db15 64 65 3a 20 4f 70 65 6e 50 73 65 75 64 6f 20 50  de: OpenPseudo P
1db16 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a  1 P2 P3 * *.**.*
1db17 2a 20 4f 70 65 6e 20 61 20 6e 65 77 20 63 75 72  * Open a new cur
1db18 73 6f 72 20 74 68 61 74 20 70 6f 69 6e 74 73 20  sor that points 
1db19 74 6f 20 61 20 66 61 6b 65 20 74 61 62 6c 65 20  to a fake table 
1db1a 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 20  that contains a 
1db1b 73 69 6e 67 6c 65 0a 2a 2a 20 72 6f 77 20 6f 66  single.** row of
1db1c 20 64 61 74 61 2e 20 20 54 68 65 20 63 6f 6e 74   data.  The cont
1db1d 65 6e 74 20 6f 66 20 74 68 61 74 20 6f 6e 65 20  ent of that one 
1db1e 72 6f 77 20 69 6e 20 74 68 65 20 63 6f 6e 74 65  row in the conte
1db1f 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20  nt of memory.** 
1db20 72 65 67 69 73 74 65 72 20 50 32 2e 20 20 49 6e  register P2.  In
1db21 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 63 75   other words, cu
1db22 72 73 6f 72 20 50 31 20 62 65 63 6f 6d 65 73 20  rsor P1 becomes 
1db23 61 6e 20 61 6c 69 61 73 20 66 6f 72 20 74 68 65  an alias for the
1db24 20 0a 2a 2a 20 4d 45 4d 5f 42 6c 6f 62 20 63 6f   .** MEM_Blob co
1db25 6e 74 65 6e 74 20 63 6f 6e 74 61 69 6e 65 64 20  ntent contained 
1db26 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a  in register P2..
1db27 2a 2a 0a 2a 2a 20 41 20 70 73 65 75 64 6f 2d 74  **.** A pseudo-t
1db28 61 62 6c 65 20 63 72 65 61 74 65 64 20 62 79 20  able created by 
1db29 74 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20 75  this opcode is u
1db2a 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  sed to hold the 
1db2b 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 72 6f 77 20  a single.** row 
1db2c 6f 75 74 70 75 74 20 66 72 6f 6d 20 74 68 65 20  output from the 
1db2d 73 6f 72 74 65 72 20 73 6f 20 74 68 61 74 20 74  sorter so that t
1db2e 68 65 20 72 6f 77 20 63 61 6e 20 62 65 20 64 65  he row can be de
1db2f 63 6f 6d 70 6f 73 65 64 20 69 6e 74 6f 0a 2a 2a  composed into.**
1db30 20 69 6e 64 69 76 69 64 75 61 6c 20 63 6f 6c 75   individual colu
1db31 6d 6e 73 20 75 73 69 6e 67 20 74 68 65 20 4f 50  mns using the OP
1db32 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 2e 20  _Column opcode. 
1db33 20 54 68 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f   The OP_Column o
1db34 70 63 6f 64 65 0a 2a 2a 20 69 73 20 74 68 65 20  pcode.** is the 
1db35 6f 6e 6c 79 20 63 75 72 73 6f 72 20 6f 70 63 6f  only cursor opco
1db36 64 65 20 74 68 61 74 20 77 6f 72 6b 73 20 77 69  de that works wi
1db37 74 68 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c  th a pseudo-tabl
1db38 65 2e 0a 2a 2a 0a 2a 2a 20 50 33 20 69 73 20 74  e..**.** P3 is t
1db39 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 69 65  he number of fie
1db3a 6c 64 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72  lds in the recor
1db3b 64 73 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20  ds that will be 
1db3c 73 74 6f 72 65 64 20 62 79 0a 2a 2a 20 74 68 65  stored by.** the
1db3d 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a   pseudo-table..*
1db3e 2f 0a 63 61 73 65 20 4f 50 5f 4f 70 65 6e 50 73  /.case OP_OpenPs
1db3f 65 75 64 6f 3a 20 7b 0a 23 69 66 20 30 20 20 2f  eudo: {.#if 0  /
1db40 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1db41 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
1db42 79 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f  y */.  VdbeCurso
1db43 72 20 2a 70 43 78 3b 0a 23 65 6e 64 69 66 20 2f  r *pCx;.#endif /
1db44 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1db45 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
1db46 79 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  y */..  assert( 
1db47 70 4f 70 2d 3e 70 31 3e 3d 30 20 29 3b 0a 20 20  pOp->p1>=0 );.  
1db48 75 2e 61 79 2e 70 43 78 20 3d 20 61 6c 6c 6f 63  u.ay.pCx = alloc
1db49 61 74 65 43 75 72 73 6f 72 28 70 2c 20 70 4f 70  ateCursor(p, pOp
1db4a 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 33 2c 20 2d  ->p1, pOp->p3, -
1db4b 31 2c 20 30 29 3b 0a 20 20 69 66 28 20 75 2e 61  1, 0);.  if( u.a
1db4c 79 2e 70 43 78 3d 3d 30 20 29 20 67 6f 74 6f 20  y.pCx==0 ) goto 
1db4d 6e 6f 5f 6d 65 6d 3b 0a 20 20 75 2e 61 79 2e 70  no_mem;.  u.ay.p
1db4e 43 78 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b  Cx->nullRow = 1;
1db4f 0a 20 20 75 2e 61 79 2e 70 43 78 2d 3e 70 73 65  .  u.ay.pCx->pse
1db50 75 64 6f 54 61 62 6c 65 52 65 67 20 3d 20 70 4f  udoTableReg = pO
1db51 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 79 2e 70 43  p->p2;.  u.ay.pC
1db52 78 2d 3e 69 73 54 61 62 6c 65 20 3d 20 31 3b 0a  x->isTable = 1;.
1db53 20 20 75 2e 61 79 2e 70 43 78 2d 3e 69 73 49 6e    u.ay.pCx->isIn
1db54 64 65 78 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b  dex = 0;.  break
1db55 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1db56 43 6c 6f 73 65 20 50 31 20 2a 20 2a 20 2a 20 2a  Close P1 * * * *
1db57 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 63  .**.** Close a c
1db58 75 72 73 6f 72 20 70 72 65 76 69 6f 75 73 6c 79  ursor previously
1db59 20 6f 70 65 6e 65 64 20 61 73 20 50 31 2e 20 20   opened as P1.  
1db5a 49 66 20 50 31 20 69 73 20 6e 6f 74 0a 2a 2a 20  If P1 is not.** 
1db5b 63 75 72 72 65 6e 74 6c 79 20 6f 70 65 6e 2c 20  currently open, 
1db5c 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  this instruction
1db5d 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a   is a no-op..*/.
1db5e 63 61 73 65 20 4f 50 5f 43 6c 6f 73 65 3a 20 7b  case OP_Close: {
1db5f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1db60 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31  p1>=0 && pOp->p1
1db61 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20  <p->nCursor );. 
1db62 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 65   sqlite3VdbeFree
1db63 43 75 72 73 6f 72 28 70 2c 20 70 2d 3e 61 70 43  Cursor(p, p->apC
1db64 73 72 5b 70 4f 70 2d 3e 70 31 5d 29 3b 0a 20 20  sr[pOp->p1]);.  
1db65 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31  p->apCsr[pOp->p1
1db66 5d 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a  ] = 0;.  break;.
1db67 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65  }../* Opcode: Se
1db68 65 6b 47 65 20 50 31 20 50 32 20 50 33 20 50 34  ekGe P1 P2 P3 P4
1db69 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73   *.**.** If curs
1db6a 6f 72 20 50 31 20 72 65 66 65 72 73 20 74 6f 20  or P1 refers to 
1db6b 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 28 42 2d  an SQL table (B-
1db6c 54 72 65 65 20 74 68 61 74 20 75 73 65 73 20 69  Tree that uses i
1db6d 6e 74 65 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a  nteger keys), .*
1db6e 2a 20 75 73 65 20 74 68 65 20 76 61 6c 75 65 20  * use the value 
1db6f 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 61  in register P3 a
1db70 73 20 74 68 65 20 6b 65 79 2e 20 20 49 66 20 63  s the key.  If c
1db71 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 20  ursor P1 refers 
1db72 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e  .** to an SQL in
1db73 64 65 78 2c 20 74 68 65 6e 20 50 33 20 69 73 20  dex, then P3 is 
1db74 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20  the first in an 
1db75 61 72 72 61 79 20 6f 66 20 50 34 20 72 65 67 69  array of P4 regi
1db76 73 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 61  sters .** that a
1db77 72 65 20 75 73 65 64 20 61 73 20 61 6e 20 75 6e  re used as an un
1db78 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79  packed index key
1db79 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74  . .**.** Reposit
1db7a 69 6f 6e 20 63 75 72 73 6f 72 20 50 31 20 73 6f  ion cursor P1 so
1db7b 20 74 68 61 74 20 20 69 74 20 70 6f 69 6e 74 73   that  it points
1db7c 20 74 6f 20 74 68 65 20 73 6d 61 6c 6c 65 73 74   to the smallest
1db7d 20 65 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a 20   entry that .** 
1db7e 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
1db7f 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20  or equal to the 
1db80 6b 65 79 20 76 61 6c 75 65 2e 20 49 66 20 74 68  key value. If th
1db81 65 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72  ere are no recor
1db82 64 73 20 0a 2a 2a 20 67 72 65 61 74 65 72 20 74  ds .** greater t
1db83 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20  han or equal to 
1db84 74 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20 69  the key and P2 i
1db85 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e  s not zero, then
1db86 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a   jump to P2..**.
1db87 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75  ** See also: Fou
1db88 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69  nd, NotFound, Di
1db89 73 74 69 6e 63 74 2c 20 53 65 65 6b 4c 74 2c 20  stinct, SeekLt, 
1db8a 53 65 65 6b 47 74 2c 20 53 65 65 6b 4c 65 0a 2a  SeekGt, SeekLe.*
1db8b 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65  /./* Opcode: See
1db8c 6b 47 74 20 50 31 20 50 32 20 50 33 20 50 34 20  kGt P1 P2 P3 P4 
1db8d 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f  *.**.** If curso
1db8e 72 20 50 31 20 72 65 66 65 72 73 20 74 6f 20 61  r P1 refers to a
1db8f 6e 20 53 51 4c 20 74 61 62 6c 65 20 28 42 2d 54  n SQL table (B-T
1db90 72 65 65 20 74 68 61 74 20 75 73 65 73 20 69 6e  ree that uses in
1db91 74 65 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a  teger keys), .**
1db92 20 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 69   use the value i
1db93 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 61 73  n register P3 as
1db94 20 61 20 6b 65 79 2e 20 49 66 20 63 75 72 73 6f   a key. If curso
1db95 72 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a 20  r P1 refers .** 
1db96 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c  to an SQL index,
1db97 20 74 68 65 6e 20 50 33 20 69 73 20 74 68 65 20   then P3 is the 
1db98 66 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61  first in an arra
1db99 79 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 72  y of P4 register
1db9a 73 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 75  s .** that are u
1db9b 73 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63 6b  sed as an unpack
1db9c 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a  ed index key. .*
1db9d 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20  *.** Reposition 
1db9e 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61  cursor P1 so tha
1db9f 74 20 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20  t  it points to 
1dba0 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 65 6e 74  the smallest ent
1dba1 72 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 67  ry that .** is g
1dba2 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20  reater than the 
1dba3 6b 65 79 20 76 61 6c 75 65 2e 20 49 66 20 74 68  key value. If th
1dba4 65 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72  ere are no recor
1dba5 64 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  ds greater than 
1dba6 0a 2a 2a 20 74 68 65 20 6b 65 79 20 61 6e 64 20  .** the key and 
1dba7 50 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20  P2 is not zero, 
1dba8 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e  then jump to P2.
1dba9 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  .**.** See also:
1dbaa 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64   Found, NotFound
1dbab 2c 20 44 69 73 74 69 6e 63 74 2c 20 53 65 65 6b  , Distinct, Seek
1dbac 4c 74 2c 20 53 65 65 6b 47 65 2c 20 53 65 65 6b  Lt, SeekGe, Seek
1dbad 4c 65 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a  Le.*/./* Opcode:
1dbae 20 53 65 65 6b 4c 74 20 50 31 20 50 32 20 50 33   SeekLt P1 P2 P3
1dbaf 20 50 34 20 2a 20 0a 2a 2a 0a 2a 2a 20 49 66 20   P4 * .**.** If 
1dbb0 63 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73  cursor P1 refers
1dbb1 20 74 6f 20 61 6e 20 53 51 4c 20 74 61 62 6c 65   to an SQL table
1dbb2 20 28 42 2d 54 72 65 65 20 74 68 61 74 20 75 73   (B-Tree that us
1dbb3 65 73 20 69 6e 74 65 67 65 72 20 6b 65 79 73 29  es integer keys)
1dbb4 2c 20 0a 2a 2a 20 75 73 65 20 74 68 65 20 76 61  , .** use the va
1dbb5 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
1dbb6 50 33 20 61 73 20 61 20 6b 65 79 2e 20 49 66 20  P3 as a key. If 
1dbb7 63 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73  cursor P1 refers
1dbb8 20 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69   .** to an SQL i
1dbb9 6e 64 65 78 2c 20 74 68 65 6e 20 50 33 20 69 73  ndex, then P3 is
1dbba 20 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e   the first in an
1dbbb 20 61 72 72 61 79 20 6f 66 20 50 34 20 72 65 67   array of P4 reg
1dbbc 69 73 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 20  isters .** that 
1dbbd 61 72 65 20 75 73 65 64 20 61 73 20 61 6e 20 75  are used as an u
1dbbe 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65  npacked index ke
1dbbf 79 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69  y. .**.** Reposi
1dbc0 74 69 6f 6e 20 63 75 72 73 6f 72 20 50 31 20 73  tion cursor P1 s
1dbc1 6f 20 74 68 61 74 20 20 69 74 20 70 6f 69 6e 74  o that  it point
1dbc2 73 20 74 6f 20 74 68 65 20 6c 61 72 67 65 73 74  s to the largest
1dbc3 20 65 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a 20   entry that .** 
1dbc4 69 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65  is less than the
1dbc5 20 6b 65 79 20 76 61 6c 75 65 2e 20 49 66 20 74   key value. If t
1dbc6 68 65 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f  here are no reco
1dbc7 72 64 73 20 6c 65 73 73 20 74 68 61 6e 20 0a 2a  rds less than .*
1dbc8 2a 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50 32  * the key and P2
1dbc9 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68   is not zero, th
1dbca 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a  en jump to P2..*
1dbcb 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46  *.** See also: F
1dbcc 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20  ound, NotFound, 
1dbcd 44 69 73 74 69 6e 63 74 2c 20 53 65 65 6b 47 74  Distinct, SeekGt
1dbce 2c 20 53 65 65 6b 47 65 2c 20 53 65 65 6b 4c 65  , SeekGe, SeekLe
1dbcf 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53  .*/./* Opcode: S
1dbd0 65 65 6b 4c 65 20 50 31 20 50 32 20 50 33 20 50  eekLe P1 P2 P3 P
1dbd1 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72  4 *.**.** If cur
1dbd2 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 74 6f  sor P1 refers to
1dbd3 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 28 42   an SQL table (B
1dbd4 2d 54 72 65 65 20 74 68 61 74 20 75 73 65 73 20  -Tree that uses 
1dbd5 69 6e 74 65 67 65 72 20 6b 65 79 73 29 2c 20 0a  integer keys), .
1dbd6 2a 2a 20 75 73 65 20 74 68 65 20 76 61 6c 75 65  ** use the value
1dbd7 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 20   in register P3 
1dbd8 61 73 20 61 20 6b 65 79 2e 20 49 66 20 63 75 72  as a key. If cur
1dbd9 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 0a 2a  sor P1 refers .*
1dbda 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65  * to an SQL inde
1dbdb 78 2c 20 74 68 65 6e 20 50 33 20 69 73 20 74 68  x, then P3 is th
1dbdc 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20 61 72  e first in an ar
1dbdd 72 61 79 20 6f 66 20 50 34 20 72 65 67 69 73 74  ray of P4 regist
1dbde 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 72 65  ers .** that are
1dbdf 20 75 73 65 64 20 61 73 20 61 6e 20 75 6e 70 61   used as an unpa
1dbe0 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20  cked index key. 
1dbe1 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f  .**.** Repositio
1dbe2 6e 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74  n cursor P1 so t
1dbe3 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f  hat it points to
1dbe4 20 74 68 65 20 6c 61 72 67 65 73 74 20 65 6e 74   the largest ent
1dbe5 72 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 6c  ry that .** is l
1dbe6 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61  ess than or equa
1dbe7 6c 20 74 6f 20 74 68 65 20 6b 65 79 20 76 61 6c  l to the key val
1dbe8 75 65 2e 20 49 66 20 74 68 65 72 65 20 61 72 65  ue. If there are
1dbe9 20 6e 6f 20 72 65 63 6f 72 64 73 20 0a 2a 2a 20   no records .** 
1dbea 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75  less than or equ
1dbeb 61 6c 20 74 6f 20 74 68 65 20 6b 65 79 20 61 6e  al to the key an
1dbec 64 20 50 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f  d P2 is not zero
1dbed 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50  , then jump to P
1dbee 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  2..**.** See als
1dbef 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75  o: Found, NotFou
1dbf0 6e 64 2c 20 44 69 73 74 69 6e 63 74 2c 20 53 65  nd, Distinct, Se
1dbf1 65 6b 47 74 2c 20 53 65 65 6b 47 65 2c 20 53 65  ekGt, SeekGe, Se
1dbf2 65 6b 4c 74 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ekLt.*/.case OP_
1dbf3 53 65 65 6b 4c 74 3a 20 20 20 20 20 20 20 20 20  SeekLt:         
1dbf4 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a  /* jump, in3 */.
1dbf5 63 61 73 65 20 4f 50 5f 53 65 65 6b 4c 65 3a 20  case OP_SeekLe: 
1dbf6 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c          /* jump,
1dbf7 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f   in3 */.case OP_
1dbf8 53 65 65 6b 47 65 3a 20 20 20 20 20 20 20 20 20  SeekGe:         
1dbf9 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a  /* jump, in3 */.
1dbfa 63 61 73 65 20 4f 50 5f 53 65 65 6b 47 74 3a 20  case OP_SeekGt: 
1dbfb 7b 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c  {       /* jump,
1dbfc 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f   in3 */.#if 0  /
1dbfd 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1dbfe 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
1dbff 7a 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 3b 0a  z */.  int res;.
1dc00 20 20 69 6e 74 20 6f 63 3b 0a 20 20 56 64 62 65    int oc;.  Vdbe
1dc01 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 55 6e  Cursor *pC;.  Un
1dc02 70 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a  packedRecord r;.
1dc03 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 0a 20 20    int nField;.  
1dc04 69 36 34 20 69 4b 65 79 3b 20 20 20 20 20 20 2f  i64 iKey;      /
1dc05 2a 20 54 68 65 20 72 6f 77 69 64 20 77 65 20 61  * The rowid we a
1dc06 72 65 20 74 6f 20 73 65 65 6b 20 74 6f 20 2a 2f  re to seek to */
1dc07 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1dc08 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1dc09 20 69 6e 74 6f 20 75 2e 61 7a 20 2a 2f 0a 0a 20   into u.az */.. 
1dc0a 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31   assert( pOp->p1
1dc0b 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70  >=0 && pOp->p1<p
1dc0c 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61  ->nCursor );.  a
1dc0d 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 21 3d  ssert( pOp->p2!=
1dc0e 30 20 29 3b 0a 20 20 75 2e 61 7a 2e 70 43 20 3d  0 );.  u.az.pC =
1dc0f 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70   p->apCsr[pOp->p
1dc10 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e  1];.  assert( u.
1dc11 61 7a 2e 70 43 21 3d 30 20 29 3b 0a 20 20 61 73  az.pC!=0 );.  as
1dc12 73 65 72 74 28 20 75 2e 61 7a 2e 70 43 2d 3e 70  sert( u.az.pC->p
1dc13 73 65 75 64 6f 54 61 62 6c 65 52 65 67 3d 3d 30  seudoTableReg==0
1dc14 20 29 3b 0a 20 20 69 66 28 20 75 2e 61 7a 2e 70   );.  if( u.az.p
1dc15 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 7b  C->pCursor!=0 ){
1dc16 0a 20 20 20 20 75 2e 61 7a 2e 6f 63 20 3d 20 70  .    u.az.oc = p
1dc17 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0a 20 20 20 20  Op->opcode;.    
1dc18 75 2e 61 7a 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77  u.az.pC->nullRow
1dc19 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 75 2e   = 0;.    if( u.
1dc1a 61 7a 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20 29  az.pC->isTable )
1dc1b 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69  {.      /* The i
1dc1c 6e 70 75 74 20 76 61 6c 75 65 20 69 6e 20 50 33  nput value in P3
1dc1d 20 6d 69 67 68 74 20 62 65 20 6f 66 20 61 6e 79   might be of any
1dc1e 20 74 79 70 65 3a 20 69 6e 74 65 67 65 72 2c 20   type: integer, 
1dc1f 72 65 61 6c 2c 20 73 74 72 69 6e 67 2c 0a 20 20  real, string,.  
1dc20 20 20 20 20 2a 2a 20 62 6c 6f 62 2c 20 6f 72 20      ** blob, or 
1dc21 4e 55 4c 4c 2e 20 20 42 75 74 20 69 74 20 6e 65  NULL.  But it ne
1dc22 65 64 73 20 74 6f 20 62 65 20 61 6e 20 69 6e 74  eds to be an int
1dc23 65 67 65 72 20 62 65 66 6f 72 65 20 77 65 20 63  eger before we c
1dc24 61 6e 20 64 6f 0a 20 20 20 20 20 20 2a 2a 20 74  an do.      ** t
1dc25 68 65 20 73 65 65 6b 2c 20 73 6f 20 63 6f 76 65  he seek, so cove
1dc26 72 74 20 69 74 2e 20 2a 2f 0a 20 20 20 20 20 20  rt it. */.      
1dc27 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69  applyNumericAffi
1dc28 6e 69 74 79 28 70 49 6e 33 29 3b 0a 20 20 20 20  nity(pIn3);.    
1dc29 20 20 75 2e 61 7a 2e 69 4b 65 79 20 3d 20 73 71    u.az.iKey = sq
1dc2a 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75  lite3VdbeIntValu
1dc2b 65 28 70 49 6e 33 29 3b 0a 20 20 20 20 20 20 75  e(pIn3);.      u
1dc2c 2e 61 7a 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56  .az.pC->rowidIsV
1dc2d 61 6c 69 64 20 3d 20 30 3b 0a 0a 20 20 20 20 20  alid = 0;..     
1dc2e 20 2f 2a 20 49 66 20 74 68 65 20 50 33 20 76 61   /* If the P3 va
1dc2f 6c 75 65 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65  lue could not be
1dc30 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20   converted into 
1dc31 61 6e 20 69 6e 74 65 67 65 72 20 77 69 74 68 6f  an integer witho
1dc32 75 74 0a 20 20 20 20 20 20 2a 2a 20 6c 6f 73 73  ut.      ** loss
1dc33 20 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2c   of information,
1dc34 20 74 68 65 6e 20 73 70 65 63 69 61 6c 20 70 72   then special pr
1dc35 6f 63 65 73 73 69 6e 67 20 69 73 20 72 65 71 75  ocessing is requ
1dc36 69 72 65 64 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20  ired... */.     
1dc37 20 69 66 28 20 28 70 49 6e 33 2d 3e 66 6c 61 67   if( (pIn3->flag
1dc38 73 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d 30 20  s & MEM_Int)==0 
1dc39 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 28  ){.        if( (
1dc3a 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn3->flags & ME
1dc3b 4d 5f 52 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 20  M_Real)==0 ){.  
1dc3c 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68          /* If th
1dc3d 65 20 50 33 20 76 61 6c 75 65 20 63 61 6e 6e 6f  e P3 value canno
1dc3e 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 69  t be converted i
1dc3f 6e 74 6f 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20  nto any kind of 
1dc40 61 20 6e 75 6d 62 65 72 2c 0a 20 20 20 20 20 20  a number,.      
1dc41 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20      ** then the 
1dc42 73 65 65 6b 20 69 73 20 6e 6f 74 20 70 6f 73 73  seek is not poss
1dc43 69 62 6c 65 2c 20 73 6f 20 6a 75 6d 70 20 74 6f  ible, so jump to
1dc44 20 50 32 20 2a 2f 0a 20 20 20 20 20 20 20 20 20   P2 */.         
1dc45 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
1dc46 31 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  1;.          bre
1dc47 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
1dc48 20 20 20 20 20 20 2f 2a 20 49 66 20 77 65 20 72        /* If we r
1dc49 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74 2c  each this point,
1dc4a 20 74 68 65 6e 20 74 68 65 20 50 33 20 76 61 6c   then the P3 val
1dc4b 75 65 20 6d 75 73 74 20 62 65 20 61 20 66 6c 6f  ue must be a flo
1dc4c 61 74 69 6e 67 0a 20 20 20 20 20 20 20 20 2a 2a  ating.        **
1dc4d 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72 2e 20 2a   point number. *
1dc4e 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  /.        assert
1dc4f 28 20 28 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26  ( (pIn3->flags &
1dc50 20 4d 45 4d 5f 52 65 61 6c 29 21 3d 30 20 29 3b   MEM_Real)!=0 );
1dc51 0a 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e  ..        if( u.
1dc52 61 7a 2e 69 4b 65 79 3d 3d 53 4d 41 4c 4c 45 53  az.iKey==SMALLES
1dc53 54 5f 49 4e 54 36 34 20 26 26 20 28 70 49 6e 33  T_INT64 && (pIn3
1dc54 2d 3e 72 3c 28 64 6f 75 62 6c 65 29 75 2e 61 7a  ->r<(double)u.az
1dc55 2e 69 4b 65 79 20 7c 7c 20 70 49 6e 33 2d 3e 72  .iKey || pIn3->r
1dc56 3e 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  >0) ){.         
1dc57 20 2f 2a 20 54 68 65 20 50 33 20 76 61 6c 75 65   /* The P3 value
1dc58 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 69 6e   is too large in
1dc59 20 6d 61 67 6e 69 74 75 64 65 20 74 6f 20 62 65   magnitude to be
1dc5a 20 65 78 70 72 65 73 73 65 64 20 61 73 20 61 6e   expressed as an
1dc5b 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 6e  .          ** in
1dc5c 74 65 67 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20  teger. */.      
1dc5d 20 20 20 20 75 2e 61 7a 2e 72 65 73 20 3d 20 31      u.az.res = 1
1dc5e 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
1dc5f 70 49 6e 33 2d 3e 72 3c 30 20 29 7b 0a 20 20 20  pIn3->r<0 ){.   
1dc60 20 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 61           if( u.a
1dc61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 20  z.oc==OP_SeekGt 
1dc62 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53  || u.az.oc==OP_S
1dc63 65 65 6b 47 65 20 29 7b 0a 20 20 20 20 20 20 20  eekGe ){.       
1dc64 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
1dc65 74 65 33 42 74 72 65 65 46 69 72 73 74 28 75 2e  te3BtreeFirst(u.
1dc66 61 7a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20  az.pC->pCursor, 
1dc67 26 75 2e 61 7a 2e 72 65 73 29 3b 0a 20 20 20 20  &u.az.res);.    
1dc68 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63            if( rc
1dc69 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
1dc6a 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1dc6b 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 20  error;.         
1dc6c 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d     }.          }
1dc6d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
1dc6e 20 20 69 66 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f    if( u.az.oc==O
1dc6f 50 5f 53 65 65 6b 4c 74 20 7c 7c 20 75 2e 61 7a  P_SeekLt || u.az
1dc70 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 29  .oc==OP_SeekLe )
1dc71 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
1dc72 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1dc73 65 4c 61 73 74 28 75 2e 61 7a 2e 70 43 2d 3e 70  eLast(u.az.pC->p
1dc74 43 75 72 73 6f 72 2c 20 26 75 2e 61 7a 2e 72 65  Cursor, &u.az.re
1dc75 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  s);.            
1dc76 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1dc77 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74  _OK ) goto abort
1dc78 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20  _due_to_error;. 
1dc79 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
1dc7a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1dc7b 20 20 20 69 66 28 20 75 2e 61 7a 2e 72 65 73 20     if( u.az.res 
1dc7c 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  ){.            p
1dc7d 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b  c = pOp->p2 - 1;
1dc7e 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
1dc7f 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1dc80 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
1dc81 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b  u.az.oc==OP_Seek
1dc82 4c 74 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f  Lt || u.az.oc==O
1dc83 50 5f 53 65 65 6b 47 65 20 29 7b 0a 20 20 20 20  P_SeekGe ){.    
1dc84 20 20 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65        /* Use the
1dc85 20 63 65 69 6c 69 6e 67 28 29 20 66 75 6e 63 74   ceiling() funct
1dc86 69 6f 6e 20 74 6f 20 63 6f 6e 76 65 72 74 20 72  ion to convert r
1dc87 65 61 6c 2d 3e 69 6e 74 20 2a 2f 0a 20 20 20 20  eal->int */.    
1dc88 20 20 20 20 20 20 69 66 28 20 70 49 6e 33 2d 3e        if( pIn3->
1dc89 72 20 3e 20 28 64 6f 75 62 6c 65 29 75 2e 61 7a  r > (double)u.az
1dc8a 2e 69 4b 65 79 20 29 20 75 2e 61 7a 2e 69 4b 65  .iKey ) u.az.iKe
1dc8b 79 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  y++;.        }el
1dc8c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a  se{.          /*
1dc8d 20 55 73 65 20 74 68 65 20 66 6c 6f 6f 72 28 29   Use the floor()
1dc8e 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 6f 6e   function to con
1dc8f 76 65 72 74 20 72 65 61 6c 2d 3e 69 6e 74 20 2a  vert real->int *
1dc90 2f 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  /.          asse
1dc91 72 74 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f  rt( u.az.oc==OP_
1dc92 53 65 65 6b 4c 65 20 7c 7c 20 75 2e 61 7a 2e 6f  SeekLe || u.az.o
1dc93 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 20 29 3b 0a  c==OP_SeekGt );.
1dc94 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 49            if( pI
1dc95 6e 33 2d 3e 72 20 3c 20 28 64 6f 75 62 6c 65 29  n3->r < (double)
1dc96 75 2e 61 7a 2e 69 4b 65 79 20 29 20 75 2e 61 7a  u.az.iKey ) u.az
1dc97 2e 69 4b 65 79 2d 2d 3b 0a 20 20 20 20 20 20 20  .iKey--;.       
1dc98 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
1dc99 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1dc9a 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64  eeMovetoUnpacked
1dc9b 28 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 73 6f  (u.az.pC->pCurso
1dc9c 72 2c 20 30 2c 20 28 75 36 34 29 75 2e 61 7a 2e  r, 0, (u64)u.az.
1dc9d 69 4b 65 79 2c 20 30 2c 20 26 75 2e 61 7a 2e 72  iKey, 0, &u.az.r
1dc9e 65 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  es);.      if( r
1dc9f 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
1dca0 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f          goto abo
1dca1 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b  rt_due_to_error;
1dca2 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
1dca3 66 28 20 75 2e 61 7a 2e 72 65 73 3d 3d 30 20 29  f( u.az.res==0 )
1dca4 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 7a 2e 70  {.        u.az.p
1dca5 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20  C->rowidIsValid 
1dca6 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 75 2e 61  = 1;.        u.a
1dca7 7a 2e 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64 20  z.pC->lastRowid 
1dca8 3d 20 75 2e 61 7a 2e 69 4b 65 79 3b 0a 20 20 20  = u.az.iKey;.   
1dca9 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
1dcaa 20 20 20 20 20 20 75 2e 61 7a 2e 6e 46 69 65 6c        u.az.nFiel
1dcab 64 20 3d 20 70 4f 70 2d 3e 70 34 2e 69 3b 0a 20  d = pOp->p4.i;. 
1dcac 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70       assert( pOp
1dcad 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54  ->p4type==P4_INT
1dcae 33 32 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  32 );.      asse
1dcaf 72 74 28 20 75 2e 61 7a 2e 6e 46 69 65 6c 64 3e  rt( u.az.nField>
1dcb0 30 20 29 3b 0a 20 20 20 20 20 20 75 2e 61 7a 2e  0 );.      u.az.
1dcb1 72 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 75 2e 61  r.pKeyInfo = u.a
1dcb2 7a 2e 70 43 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a  z.pC->pKeyInfo;.
1dcb3 20 20 20 20 20 20 75 2e 61 7a 2e 72 2e 6e 46 69        u.az.r.nFi
1dcb4 65 6c 64 20 3d 20 28 75 31 36 29 75 2e 61 7a 2e  eld = (u16)u.az.
1dcb5 6e 46 69 65 6c 64 3b 0a 20 20 20 20 20 20 69 66  nField;.      if
1dcb6 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65  ( u.az.oc==OP_Se
1dcb7 65 6b 47 74 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d  ekGt || u.az.oc=
1dcb8 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 7b 0a 20 20  =OP_SeekLe ){.  
1dcb9 20 20 20 20 20 20 75 2e 61 7a 2e 72 2e 66 6c 61        u.az.r.fla
1dcba 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f 49 4e  gs = UNPACKED_IN
1dcbb 43 52 4b 45 59 3b 0a 20 20 20 20 20 20 7d 65 6c  CRKEY;.      }el
1dcbc 73 65 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 7a  se{.        u.az
1dcbd 2e 72 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20  .r.flags = 0;.  
1dcbe 20 20 20 20 7d 0a 20 20 20 20 20 20 75 2e 61 7a      }.      u.az
1dcbf 2e 72 2e 61 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d  .r.aMem = &p->aM
1dcc0 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20  em[pOp->p3];.   
1dcc1 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
1dcc2 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b  treeMovetoUnpack
1dcc3 65 64 28 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72  ed(u.az.pC->pCur
1dcc4 73 6f 72 2c 20 26 75 2e 61 7a 2e 72 2c 20 30 2c  sor, &u.az.r, 0,
1dcc5 20 30 2c 20 26 75 2e 61 7a 2e 72 65 73 29 3b 0a   0, &u.az.res);.
1dcc6 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
1dcc7 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1dcc8 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75     goto abort_du
1dcc9 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20  e_to_error;.    
1dcca 20 20 7d 0a 20 20 20 20 20 20 75 2e 61 7a 2e 70    }.      u.az.p
1dccb 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20  C->rowidIsValid 
1dccc 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75  = 0;.    }.    u
1dccd 2e 61 7a 2e 70 43 2d 3e 64 65 66 65 72 72 65 64  .az.pC->deferred
1dcce 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20  Moveto = 0;.    
1dccf 75 2e 61 7a 2e 70 43 2d 3e 63 61 63 68 65 53 74  u.az.pC->cacheSt
1dcd0 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41  atus = CACHE_STA
1dcd1 4c 45 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  LE;.#ifdef SQLIT
1dcd2 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c 69 74  E_TEST.    sqlit
1dcd3 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 2b  e3_search_count+
1dcd4 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66  +;.#endif.    if
1dcd5 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65  ( u.az.oc==OP_Se
1dcd6 65 6b 47 65 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d  ekGe || u.az.oc=
1dcd7 3d 4f 50 5f 53 65 65 6b 47 74 20 29 7b 0a 20 20  =OP_SeekGt ){.  
1dcd8 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 72 65 73      if( u.az.res
1dcd9 3c 30 20 7c 7c 20 28 75 2e 61 7a 2e 72 65 73 3d  <0 || (u.az.res=
1dcda 3d 30 20 26 26 20 75 2e 61 7a 2e 6f 63 3d 3d 4f  =0 && u.az.oc==O
1dcdb 50 5f 53 65 65 6b 47 74 29 20 29 7b 0a 20 20 20  P_SeekGt) ){.   
1dcdc 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1dcdd 33 42 74 72 65 65 4e 65 78 74 28 75 2e 61 7a 2e  3BtreeNext(u.az.
1dcde 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e  pC->pCursor, &u.
1dcdf 61 7a 2e 72 65 73 29 3b 0a 20 20 20 20 20 20 20  az.res);.       
1dce0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1dce1 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f  OK ) goto abort_
1dce2 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20  due_to_error;.  
1dce3 20 20 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 72        u.az.pC->r
1dce4 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b  owidIsValid = 0;
1dce5 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1dce6 20 20 20 20 20 20 75 2e 61 7a 2e 72 65 73 20 3d        u.az.res =
1dce7 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
1dce8 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73  }else{.      ass
1dce9 65 72 74 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50  ert( u.az.oc==OP
1dcea 5f 53 65 65 6b 4c 74 20 7c 7c 20 75 2e 61 7a 2e  _SeekLt || u.az.
1dceb 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 3b  oc==OP_SeekLe );
1dcec 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61 7a 2e  .      if( u.az.
1dced 72 65 73 3e 30 20 7c 7c 20 28 75 2e 61 7a 2e 72  res>0 || (u.az.r
1dcee 65 73 3d 3d 30 20 26 26 20 75 2e 61 7a 2e 6f 63  es==0 && u.az.oc
1dcef 3d 3d 4f 50 5f 53 65 65 6b 4c 74 29 20 29 7b 0a  ==OP_SeekLt) ){.
1dcf0 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
1dcf1 69 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75  ite3BtreePreviou
1dcf2 73 28 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 73  s(u.az.pC->pCurs
1dcf3 6f 72 2c 20 26 75 2e 61 7a 2e 72 65 73 29 3b 0a  or, &u.az.res);.
1dcf4 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
1dcf5 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
1dcf6 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72   abort_due_to_er
1dcf7 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 75 2e 61  ror;.        u.a
1dcf8 7a 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c  z.pC->rowidIsVal
1dcf9 69 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65  id = 0;.      }e
1dcfa 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  lse{.        /* 
1dcfb 75 2e 61 7a 2e 72 65 73 20 6d 69 67 68 74 20 62  u.az.res might b
1dcfc 65 20 6e 65 67 61 74 69 76 65 20 62 65 63 61 75  e negative becau
1dcfd 73 65 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  se the table is 
1dcfe 65 6d 70 74 79 2e 20 20 43 68 65 63 6b 20 74 6f  empty.  Check to
1dcff 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 65 65 20  .        ** see 
1dd00 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63  if this is the c
1dd01 61 73 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  ase..        */.
1dd02 20 20 20 20 20 20 20 20 75 2e 61 7a 2e 72 65 73          u.az.res
1dd03 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 45   = sqlite3BtreeE
1dd04 6f 66 28 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72  of(u.az.pC->pCur
1dd05 73 6f 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  sor);.      }.  
1dd06 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
1dd07 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20  pOp->p2>0 );.   
1dd08 20 69 66 28 20 75 2e 61 7a 2e 72 65 73 20 29 7b   if( u.az.res ){
1dd09 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  .      pc = pOp-
1dd0a 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20  >p2 - 1;.    }. 
1dd0b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54   }else{.    /* T
1dd0c 68 69 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e  his happens when
1dd0d 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 6f   attempting to o
1dd0e 70 65 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f  pen the sqlite3_
1dd0f 6d 61 73 74 65 72 20 74 61 62 6c 65 0a 20 20 20  master table.   
1dd10 20 2a 2a 20 66 6f 72 20 72 65 61 64 20 61 63 63   ** for read acc
1dd11 65 73 73 20 72 65 74 75 72 6e 73 20 53 51 4c 49  ess returns SQLI
1dd12 54 45 5f 45 4d 50 54 59 2e 20 49 6e 20 74 68 69  TE_EMPTY. In thi
1dd13 73 20 63 61 73 65 20 61 6c 77 61 79 73 0a 20 20  s case always.  
1dd14 20 20 2a 2a 20 74 61 6b 65 20 74 68 65 20 6a 75    ** take the ju
1dd15 6d 70 20 28 73 69 6e 63 65 20 74 68 65 72 65 20  mp (since there 
1dd16 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 69  are no records i
1dd17 6e 20 74 68 65 20 74 61 62 6c 65 29 2e 0a 20 20  n the table)..  
1dd18 20 20 2a 2f 0a 20 20 20 20 70 63 20 3d 20 70 4f    */.    pc = pO
1dd19 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20  p->p2 - 1;.  }. 
1dd1a 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1dd1b 63 6f 64 65 3a 20 53 65 65 6b 20 50 31 20 50 32  code: Seek P1 P2
1dd1c 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 31 20   * * *.**.** P1 
1dd1d 69 73 20 61 6e 20 6f 70 65 6e 20 74 61 62 6c 65  is an open table
1dd1e 20 63 75 72 73 6f 72 20 61 6e 64 20 50 32 20 69   cursor and P2 i
1dd1f 73 20 61 20 72 6f 77 69 64 20 69 6e 74 65 67 65  s a rowid intege
1dd20 72 2e 20 20 41 72 72 61 6e 67 65 0a 2a 2a 20 66  r.  Arrange.** f
1dd21 6f 72 20 50 31 20 74 6f 20 6d 6f 76 65 20 73 6f  or P1 to move so
1dd22 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20   that it points 
1dd23 74 6f 20 74 68 65 20 72 6f 77 69 64 20 67 69 76  to the rowid giv
1dd24 65 6e 20 62 79 20 50 32 2e 0a 2a 2a 0a 2a 2a 20  en by P2..**.** 
1dd25 54 68 69 73 20 69 73 20 61 63 74 75 61 6c 6c 79  This is actually
1dd26 20 61 20 64 65 66 65 72 72 65 64 20 73 65 65 6b   a deferred seek
1dd27 2e 20 20 4e 6f 74 68 69 6e 67 20 61 63 74 75 61  .  Nothing actua
1dd28 6c 6c 79 20 68 61 70 70 65 6e 73 20 75 6e 74 69  lly happens unti
1dd29 6c 0a 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 20  l.** the cursor 
1dd2a 69 73 20 75 73 65 64 20 74 6f 20 72 65 61 64 20  is used to read 
1dd2b 61 20 72 65 63 6f 72 64 2e 20 20 54 68 61 74 20  a record.  That 
1dd2c 77 61 79 2c 20 69 66 20 6e 6f 20 72 65 61 64 73  way, if no reads
1dd2d 0a 2a 2a 20 6f 63 63 75 72 2c 20 6e 6f 20 75 6e  .** occur, no un
1dd2e 6e 65 63 65 73 73 61 72 79 20 49 2f 4f 20 68 61  necessary I/O ha
1dd2f 70 70 65 6e 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f  ppens..*/.case O
1dd30 50 5f 53 65 65 6b 3a 20 7b 20 20 20 20 2f 2a 20  P_Seek: {    /* 
1dd31 69 6e 32 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a  in2 */.#if 0  /*
1dd32 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1dd33 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 61   moved into u.ba
1dd34 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72   */.  VdbeCursor
1dd35 20 2a 70 43 3b 0a 23 65 6e 64 69 66 20 2f 2a 20   *pC;.#endif /* 
1dd36 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1dd37 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 61 20  moved into u.ba 
1dd38 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f  */..  assert( pO
1dd39 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d  p->p1>=0 && pOp-
1dd3a 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29  >p1<p->nCursor )
1dd3b 3b 0a 20 20 75 2e 62 61 2e 70 43 20 3d 20 70 2d  ;.  u.ba.pC = p-
1dd3c 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b  >apCsr[pOp->p1];
1dd3d 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 61 2e  .  assert( u.ba.
1dd3e 70 43 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 41  pC!=0 );.  if( A
1dd3f 4c 57 41 59 53 28 75 2e 62 61 2e 70 43 2d 3e 70  LWAYS(u.ba.pC->p
1dd40 43 75 72 73 6f 72 21 3d 30 29 20 29 7b 0a 20 20  Cursor!=0) ){.  
1dd41 20 20 61 73 73 65 72 74 28 20 75 2e 62 61 2e 70    assert( u.ba.p
1dd42 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20  C->isTable );.  
1dd43 20 20 75 2e 62 61 2e 70 43 2d 3e 6e 75 6c 6c 52    u.ba.pC->nullR
1dd44 6f 77 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 61  ow = 0;.    u.ba
1dd45 2e 70 43 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 65  .pC->movetoTarge
1dd46 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49  t = sqlite3VdbeI
1dd47 6e 74 56 61 6c 75 65 28 70 49 6e 32 29 3b 0a 20  ntValue(pIn2);. 
1dd48 20 20 20 75 2e 62 61 2e 70 43 2d 3e 72 6f 77 69     u.ba.pC->rowi
1dd49 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20  dIsValid = 0;.  
1dd4a 20 20 75 2e 62 61 2e 70 43 2d 3e 64 65 66 65 72    u.ba.pC->defer
1dd4b 72 65 64 4d 6f 76 65 74 6f 20 3d 20 31 3b 0a 20  redMoveto = 1;. 
1dd4c 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 20 20   }.  break;.}.  
1dd4d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 6f 75  ../* Opcode: Fou
1dd4e 6e 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  nd P1 P2 P3 * *.
1dd4f 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 50  **.** Register P
1dd50 33 20 68 6f 6c 64 73 20 61 20 62 6c 6f 62 20 63  3 holds a blob c
1dd51 6f 6e 73 74 72 75 63 74 65 64 20 62 79 20 4d 61  onstructed by Ma
1dd52 6b 65 52 65 63 6f 72 64 2e 20 20 50 31 20 69 73  keRecord.  P1 is
1dd53 20 61 6e 20 69 6e 64 65 78 2e 0a 2a 2a 20 49 66   an index..** If
1dd54 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 20 6d   an entry that m
1dd55 61 74 63 68 65 73 20 74 68 65 20 76 61 6c 75 65  atches the value
1dd56 20 69 6e 20 72 65 67 69 73 74 65 72 20 70 33 20   in register p3 
1dd57 65 78 69 73 74 73 20 69 6e 20 50 31 20 74 68 65  exists in P1 the
1dd58 6e 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 50 32 2e  n.** jump to P2.
1dd59 20 20 49 66 20 74 68 65 20 50 33 20 76 61 6c 75    If the P3 valu
1dd5a 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68  e does not match
1dd5b 20 61 6e 79 20 65 6e 74 72 79 20 69 6e 20 50 31   any entry in P1
1dd5c 0a 2a 2a 20 74 68 65 6e 20 66 61 6c 6c 20 74 68  .** then fall th
1dd5d 72 75 2e 20 20 54 68 65 20 50 31 20 63 75 72 73  ru.  The P1 curs
1dd5e 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74  or is left point
1dd5f 69 6e 67 20 61 74 20 74 68 65 20 6d 61 74 63 68  ing at the match
1dd60 69 6e 67 20 65 6e 74 72 79 0a 2a 2a 20 69 66 20  ing entry.** if 
1dd61 69 74 20 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a 2a  it exists..**.**
1dd62 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   This instructio
1dd63 6e 20 69 73 20 75 73 65 64 20 74 6f 20 69 6d 70  n is used to imp
1dd64 6c 65 6d 65 6e 74 20 74 68 65 20 49 4e 20 6f 70  lement the IN op
1dd65 65 72 61 74 6f 72 20 77 68 65 72 65 20 74 68 65  erator where the
1dd66 0a 2a 2a 20 6c 65 66 74 2d 68 61 6e 64 20 73 69  .** left-hand si
1dd67 64 65 20 69 73 20 61 20 53 45 4c 45 43 54 20 73  de is a SELECT s
1dd68 74 61 74 65 6d 65 6e 74 2e 20 20 50 31 20 6d 61  tatement.  P1 ma
1dd69 79 20 62 65 20 61 20 74 72 75 65 20 69 6e 64 65  y be a true inde
1dd6a 78 2c 20 6f 72 20 69 74 0a 2a 2a 20 6d 61 79 20  x, or it.** may 
1dd6b 62 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 69  be a temporary i
1dd6c 6e 64 65 78 20 74 68 61 74 20 68 6f 6c 64 73 20  ndex that holds 
1dd6d 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74  the results of t
1dd6e 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 73 74 61  he SELECT.** sta
1dd6f 74 65 6d 65 6e 74 2e 20 20 20 54 68 69 73 20 69  tement.   This i
1dd70 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 61 6c  nstruction is al
1dd71 73 6f 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  so used to imple
1dd72 6d 65 6e 74 20 74 68 65 0a 2a 2a 20 44 49 53 54  ment the.** DIST
1dd73 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 69 6e 20  INCT keyword in 
1dd74 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
1dd75 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  s..**.** This in
1dd76 73 74 72 75 63 74 69 6f 6e 20 63 68 65 63 6b 73  struction checks
1dd77 20 69 66 20 69 6e 64 65 78 20 50 31 20 63 6f 6e   if index P1 con
1dd78 74 61 69 6e 73 20 61 20 72 65 63 6f 72 64 20 66  tains a record f
1dd79 6f 72 20 77 68 69 63 68 20 0a 2a 2a 20 74 68 65  or which .** the
1dd7a 20 66 69 72 73 74 20 4e 20 73 65 72 69 61 6c 69   first N seriali
1dd7b 7a 65 64 20 76 61 6c 75 65 73 20 65 78 61 63 74  zed values exact
1dd7c 6c 79 20 6d 61 74 63 68 20 74 68 65 20 4e 20 73  ly match the N s
1dd7d 65 72 69 61 6c 69 7a 65 64 20 76 61 6c 75 65 73  erialized values
1dd7e 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 63 6f 72  .** in the recor
1dd7f 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33  d in register P3
1dd80 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65  , where N is the
1dd81 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   total number of
1dd82 20 76 61 6c 75 65 73 20 69 6e 0a 2a 2a 20 74 68   values in.** th
1dd83 65 20 50 33 20 72 65 63 6f 72 64 20 28 74 68 65  e P3 record (the
1dd84 20 50 33 20 72 65 63 6f 72 64 20 69 73 20 61 20   P3 record is a 
1dd85 70 72 65 66 69 78 20 6f 66 20 74 68 65 20 50 31  prefix of the P1
1dd86 20 72 65 63 6f 72 64 29 2e 20 0a 2a 2a 0a 2a 2a   record). .**.**
1dd87 20 53 65 65 20 61 6c 73 6f 3a 20 4e 6f 74 46 6f   See also: NotFo
1dd88 75 6e 64 2c 20 49 73 55 6e 69 71 75 65 2c 20 4e  und, IsUnique, N
1dd89 6f 74 45 78 69 73 74 73 0a 2a 2f 0a 2f 2a 20 4f  otExists.*/./* O
1dd8a 70 63 6f 64 65 3a 20 4e 6f 74 46 6f 75 6e 64 20  pcode: NotFound 
1dd8b 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a  P1 P2 P3 * *.**.
1dd8c 2a 2a 20 52 65 67 69 73 74 65 72 20 50 33 20 68  ** Register P3 h
1dd8d 6f 6c 64 73 20 61 20 62 6c 6f 62 20 63 6f 6e 73  olds a blob cons
1dd8e 74 72 75 63 74 65 64 20 62 79 20 4d 61 6b 65 52  tructed by MakeR
1dd8f 65 63 6f 72 64 2e 20 20 50 31 20 69 73 0a 2a 2a  ecord.  P1 is.**
1dd90 20 61 6e 20 69 6e 64 65 78 2e 20 20 49 66 20 6e   an index.  If n
1dd91 6f 20 65 6e 74 72 79 20 65 78 69 73 74 73 20 69  o entry exists i
1dd92 6e 20 50 31 20 74 68 61 74 20 6d 61 74 63 68 65  n P1 that matche
1dd93 73 20 74 68 65 20 62 6c 6f 62 20 74 68 65 6e 20  s the blob then 
1dd94 6a 75 6d 70 0a 2a 2a 20 74 6f 20 50 32 2e 20 20  jump.** to P2.  
1dd95 49 66 20 61 6e 20 65 6e 74 72 79 20 64 6f 65 73  If an entry does
1dd96 20 65 78 69 73 74 69 6e 67 2c 20 66 61 6c 6c 20   existing, fall 
1dd97 74 68 72 6f 75 67 68 2e 20 20 54 68 65 20 63 75  through.  The cu
1dd98 72 73 6f 72 20 69 73 20 6c 65 66 74 0a 2a 2a 20  rsor is left.** 
1dd99 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20  pointing to the 
1dd9a 65 6e 74 72 79 20 74 68 61 74 20 6d 61 74 63 68  entry that match
1dd9b 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c  es..**.** See al
1dd9c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 45 78  so: Found, NotEx
1dd9d 69 73 74 73 2c 20 49 73 55 6e 69 71 75 65 0a 2a  ists, IsUnique.*
1dd9e 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 46 6f 75  /.case OP_NotFou
1dd9f 6e 64 3a 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d  nd:       /* jum
1dda0 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f  p, in3 */.case O
1dda1 50 5f 46 6f 75 6e 64 3a 20 7b 20 20 20 20 20 20  P_Found: {      
1dda2 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a    /* jump, in3 *
1dda3 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  /.#if 0  /* loca
1dda4 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1dda5 64 20 69 6e 74 6f 20 75 2e 62 62 20 2a 2f 0a 20  d into u.bb */. 
1dda6 20 69 6e 74 20 61 6c 72 65 61 64 79 45 78 69 73   int alreadyExis
1dda7 74 73 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72  ts;.  VdbeCursor
1dda8 20 2a 70 43 3b 0a 20 20 69 6e 74 20 72 65 73 3b   *pC;.  int res;
1dda9 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  .  UnpackedRecor
1ddaa 64 20 2a 70 49 64 78 4b 65 79 3b 0a 20 20 63 68  d *pIdxKey;.  ch
1ddab 61 72 20 61 54 65 6d 70 52 65 63 5b 52 4f 55 4e  ar aTempRec[ROUN
1ddac 44 38 28 73 69 7a 65 6f 66 28 55 6e 70 61 63 6b  D8(sizeof(Unpack
1ddad 65 64 52 65 63 6f 72 64 29 29 20 2b 20 73 69 7a  edRecord)) + siz
1ddae 65 6f 66 28 4d 65 6d 29 2a 33 20 2b 20 37 5d 3b  eof(Mem)*3 + 7];
1ddaf 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1ddb0 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1ddb1 20 69 6e 74 6f 20 75 2e 62 62 20 2a 2f 0a 0a 20   into u.bb */.. 
1ddb2 20 75 2e 62 62 2e 61 6c 72 65 61 64 79 45 78 69   u.bb.alreadyExi
1ddb3 73 74 73 20 3d 20 30 3b 0a 20 20 61 73 73 65 72  sts = 0;.  asser
1ddb4 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26  t( pOp->p1>=0 &&
1ddb5 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72   pOp->p1<p->nCur
1ddb6 73 6f 72 20 29 3b 0a 20 20 75 2e 62 62 2e 70 43  sor );.  u.bb.pC
1ddb7 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d   = p->apCsr[pOp-
1ddb8 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  >p1];.  assert( 
1ddb9 75 2e 62 62 2e 70 43 21 3d 30 20 29 3b 0a 20 20  u.bb.pC!=0 );.  
1ddba 69 66 28 20 41 4c 57 41 59 53 28 75 2e 62 62 2e  if( ALWAYS(u.bb.
1ddbb 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 29 20  pC->pCursor!=0) 
1ddbc 29 7b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20  ){..    assert( 
1ddbd 75 2e 62 62 2e 70 43 2d 3e 69 73 54 61 62 6c 65  u.bb.pC->isTable
1ddbe 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ==0 );.    asser
1ddbf 74 28 20 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26  t( pIn3->flags &
1ddc0 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20   MEM_Blob );.   
1ddc1 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 33   ExpandBlob(pIn3
1ddc2 29 3b 0a 20 20 20 20 75 2e 62 62 2e 70 49 64 78  );.    u.bb.pIdx
1ddc3 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 56 64 62  Key = sqlite3Vdb
1ddc4 65 52 65 63 6f 72 64 55 6e 70 61 63 6b 28 75 2e  eRecordUnpack(u.
1ddc5 62 62 2e 70 43 2d 3e 70 4b 65 79 49 6e 66 6f 2c  bb.pC->pKeyInfo,
1ddc6 20 70 49 6e 33 2d 3e 6e 2c 20 70 49 6e 33 2d 3e   pIn3->n, pIn3->
1ddc7 7a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  z,.             
1ddc8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ddc9 20 20 20 20 20 20 20 20 20 75 2e 62 62 2e 61 54           u.bb.aT
1ddca 65 6d 70 52 65 63 2c 20 73 69 7a 65 6f 66 28 75  empRec, sizeof(u
1ddcb 2e 62 62 2e 61 54 65 6d 70 52 65 63 29 29 3b 0a  .bb.aTempRec));.
1ddcc 20 20 20 20 69 66 28 20 75 2e 62 62 2e 70 49 64      if( u.bb.pId
1ddcd 78 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20  xKey==0 ){.     
1ddce 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20   goto no_mem;.  
1ddcf 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4f 70 2d    }.    if( pOp-
1ddd0 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46 6f 75 6e  >opcode==OP_Foun
1ddd1 64 20 29 7b 0a 20 20 20 20 20 20 75 2e 62 62 2e  d ){.      u.bb.
1ddd2 70 49 64 78 4b 65 79 2d 3e 66 6c 61 67 73 20 7c  pIdxKey->flags |
1ddd3 3d 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49  = UNPACKED_PREFI
1ddd4 58 5f 4d 41 54 43 48 3b 0a 20 20 20 20 7d 0a 20  X_MATCH;.    }. 
1ddd5 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
1ddd6 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b  treeMovetoUnpack
1ddd7 65 64 28 75 2e 62 62 2e 70 43 2d 3e 70 43 75 72  ed(u.bb.pC->pCur
1ddd8 73 6f 72 2c 20 75 2e 62 62 2e 70 49 64 78 4b 65  sor, u.bb.pIdxKe
1ddd9 79 2c 20 30 2c 20 30 2c 20 26 75 2e 62 62 2e 72  y, 0, 0, &u.bb.r
1ddda 65 73 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  es);.    sqlite3
1dddb 56 64 62 65 44 65 6c 65 74 65 55 6e 70 61 63 6b  VdbeDeleteUnpack
1dddc 65 64 52 65 63 6f 72 64 28 75 2e 62 62 2e 70 49  edRecord(u.bb.pI
1dddd 64 78 4b 65 79 29 3b 0a 20 20 20 20 69 66 28 20  dxKey);.    if( 
1ddde 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1dddf 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1dde0 20 20 7d 0a 20 20 20 20 75 2e 62 62 2e 61 6c 72    }.    u.bb.alr
1dde1 65 61 64 79 45 78 69 73 74 73 20 3d 20 28 75 2e  eadyExists = (u.
1dde2 62 62 2e 72 65 73 3d 3d 30 29 3b 0a 20 20 20 20  bb.res==0);.    
1dde3 75 2e 62 62 2e 70 43 2d 3e 64 65 66 65 72 72 65  u.bb.pC->deferre
1dde4 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20  dMoveto = 0;.   
1dde5 20 75 2e 62 62 2e 70 43 2d 3e 63 61 63 68 65 53   u.bb.pC->cacheS
1dde6 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54  tatus = CACHE_ST
1dde7 41 4c 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  ALE;.  }.  if( p
1dde8 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46  Op->opcode==OP_F
1dde9 6f 75 6e 64 20 29 7b 0a 20 20 20 20 69 66 28 20  ound ){.    if( 
1ddea 75 2e 62 62 2e 61 6c 72 65 61 64 79 45 78 69 73  u.bb.alreadyExis
1ddeb 74 73 20 29 20 70 63 20 3d 20 70 4f 70 2d 3e 70  ts ) pc = pOp->p
1ddec 32 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a  2 - 1;.  }else{.
1dded 20 20 20 20 69 66 28 20 21 75 2e 62 62 2e 61 6c      if( !u.bb.al
1ddee 72 65 61 64 79 45 78 69 73 74 73 20 29 20 70 63  readyExists ) pc
1ddef 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
1ddf0 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1ddf1 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 73 55 6e 69  /* Opcode: IsUni
1ddf2 71 75 65 20 50 31 20 50 32 20 50 33 20 50 34 20  que P1 P2 P3 P4 
1ddf3 2a 0a 2a 2a 0a 2a 2a 20 43 75 72 73 6f 72 20 50  *.**.** Cursor P
1ddf4 31 20 69 73 20 6f 70 65 6e 20 6f 6e 20 61 6e 20  1 is open on an 
1ddf5 69 6e 64 65 78 2e 20 20 53 6f 20 69 74 20 68 61  index.  So it ha
1ddf6 73 20 6e 6f 20 64 61 74 61 20 61 6e 64 20 69 74  s no data and it
1ddf7 73 20 6b 65 79 20 63 6f 6e 73 69 73 74 73 20 0a  s key consists .
1ddf8 2a 2a 20 6f 66 20 61 20 72 65 63 6f 72 64 20 67  ** of a record g
1ddf9 65 6e 65 72 61 74 65 64 20 62 79 20 4f 50 5f 4d  enerated by OP_M
1ddfa 61 6b 65 52 65 63 6f 72 64 20 77 68 65 72 65 20  akeRecord where 
1ddfb 74 68 65 20 6c 61 73 74 20 66 69 65 6c 64 20 69  the last field i
1ddfc 73 20 74 68 65 20 0a 2a 2a 20 72 6f 77 69 64 20  s the .** rowid 
1ddfd 6f 66 20 74 68 65 20 65 6e 74 72 79 20 74 68 61  of the entry tha
1ddfe 74 20 74 68 65 20 69 6e 64 65 78 20 72 65 66 65  t the index refe
1ddff 72 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  rs to..**.** The
1de00 20 50 33 20 72 65 67 69 73 74 65 72 20 63 6f 6e   P3 register con
1de01 74 61 69 6e 73 20 61 6e 20 69 6e 74 65 67 65 72  tains an integer
1de02 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 2e 20   record number. 
1de03 43 61 6c 6c 20 74 68 69 73 20 72 65 63 6f 72 64  Call this record
1de04 20 0a 2a 2a 20 6e 75 6d 62 65 72 20 52 2e 20 52   .** number R. R
1de05 65 67 69 73 74 65 72 20 50 34 20 69 73 20 74 68  egister P4 is th
1de06 65 20 66 69 72 73 74 20 69 6e 20 61 20 73 65 74  e first in a set
1de07 20 6f 66 20 4e 20 63 6f 6e 74 69 67 75 6f 75 73   of N contiguous
1de08 20 72 65 67 69 73 74 65 72 73 0a 2a 2a 20 74 68   registers.** th
1de09 61 74 20 6d 61 6b 65 20 75 70 20 61 6e 20 75 6e  at make up an un
1de0a 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79  packed index key
1de0b 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 73 65   that can be use
1de0c 64 20 77 69 74 68 20 63 75 72 73 6f 72 20 50 31  d with cursor P1
1de0d 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f  ..** The value o
1de0e 66 20 4e 20 63 61 6e 20 62 65 20 69 6e 66 65 72  f N can be infer
1de0f 72 65 64 20 66 72 6f 6d 20 74 68 65 20 63 75 72  red from the cur
1de10 73 6f 72 2e 20 4e 20 69 6e 63 6c 75 64 65 73 20  sor. N includes 
1de11 74 68 65 20 72 6f 77 69 64 0a 2a 2a 20 76 61 6c  the rowid.** val
1de12 75 65 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74  ue appended to t
1de13 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69 6e  he end of the in
1de14 64 65 78 20 72 65 63 6f 72 64 2e 20 54 68 69 73  dex record. This
1de15 20 72 6f 77 69 64 20 76 61 6c 75 65 20 6d 61 79   rowid value may
1de16 0a 2a 2a 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62  .** or may not b
1de17 65 20 74 68 65 20 73 61 6d 65 20 61 73 20 52 2e  e the same as R.
1de18 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 20 6f 66  .**.** If any of
1de19 20 74 68 65 20 4e 20 72 65 67 69 73 74 65 72 73   the N registers
1de1a 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20   beginning with 
1de1b 72 65 67 69 73 74 65 72 20 50 34 20 63 6f 6e 74  register P4 cont
1de1c 61 69 6e 73 20 61 20 4e 55 4c 4c 0a 2a 2a 20 76  ains a NULL.** v
1de1d 61 6c 75 65 2c 20 6a 75 6d 70 20 69 6d 6d 65 64  alue, jump immed
1de1e 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a  iately to P2..**
1de1f 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 74  .** Otherwise, t
1de20 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
1de21 63 68 65 63 6b 73 20 69 66 20 63 75 72 73 6f 72  checks if cursor
1de22 20 50 31 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20   P1 contains an 
1de23 65 6e 74 72 79 0a 2a 2a 20 77 68 65 72 65 20 74  entry.** where t
1de24 68 65 20 66 69 72 73 74 20 28 4e 2d 31 29 20 66  he first (N-1) f
1de25 69 65 6c 64 73 20 6d 61 74 63 68 20 62 75 74 20  ields match but 
1de26 74 68 65 20 72 6f 77 69 64 20 76 61 6c 75 65 20  the rowid value 
1de27 61 74 20 74 68 65 20 65 6e 64 0a 2a 2a 20 6f 66  at the end.** of
1de28 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79   the index entry
1de29 20 69 73 20 6e 6f 74 20 52 2e 20 49 66 20 74 68   is not R. If th
1de2a 65 72 65 20 69 73 20 6e 6f 20 73 75 63 68 20 65  ere is no such e
1de2b 6e 74 72 79 2c 20 63 6f 6e 74 72 6f 6c 20 6a 75  ntry, control ju
1de2c 6d 70 73 0a 2a 2a 20 74 6f 20 69 6e 73 74 72 75  mps.** to instru
1de2d 63 74 69 6f 6e 20 50 32 2e 20 4f 74 68 65 72 77  ction P2. Otherw
1de2e 69 73 65 2c 20 74 68 65 20 72 6f 77 69 64 20 6f  ise, the rowid o
1de2f 66 20 74 68 65 20 63 6f 6e 66 6c 69 63 74 69 6e  f the conflictin
1de30 67 20 69 6e 64 65 78 0a 2a 2a 20 65 6e 74 72 79  g index.** entry
1de31 20 69 73 20 63 6f 70 69 65 64 20 74 6f 20 72 65   is copied to re
1de32 67 69 73 74 65 72 20 50 33 20 61 6e 64 20 63 6f  gister P3 and co
1de33 6e 74 72 6f 6c 20 66 61 6c 6c 73 20 74 68 72 6f  ntrol falls thro
1de34 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 0a  ugh to the next.
1de35 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a  ** instruction..
1de36 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  **.** See also: 
1de37 4e 6f 74 46 6f 75 6e 64 2c 20 4e 6f 74 45 78 69  NotFound, NotExi
1de38 73 74 73 2c 20 46 6f 75 6e 64 0a 2a 2f 0a 63 61  sts, Found.*/.ca
1de39 73 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 3a 20  se OP_IsUnique: 
1de3a 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70  {        /* jump
1de3b 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20  , in3 */.#if 0  
1de3c 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1de3d 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1de3e 62 63 20 2a 2f 0a 20 20 75 31 36 20 69 69 3b 0a  bc */.  u16 ii;.
1de3f 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1de40 78 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  x;.  BtCursor *p
1de41 43 72 73 72 3b 0a 20 20 75 31 36 20 6e 46 69 65  Crsr;.  u16 nFie
1de42 6c 64 3b 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b  ld;.  Mem *aMem;
1de43 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  .  UnpackedRecor
1de44 64 20 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  d r;            
1de45 20 20 20 20 20 20 2f 2a 20 42 2d 54 72 65 65 20        /* B-Tree 
1de46 69 6e 64 65 78 20 73 65 61 72 63 68 20 6b 65 79  index search key
1de47 20 2a 2f 0a 20 20 69 36 34 20 52 3b 20 20 20 20   */.  i64 R;    
1de48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1de49 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 69           /* Rowi
1de4a 64 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69  d stored in regi
1de4b 73 74 65 72 20 50 33 20 2a 2f 0a 23 65 6e 64 69  ster P3 */.#endi
1de4c 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1de4d 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1de4e 75 2e 62 63 20 2a 2f 0a 0a 20 20 75 2e 62 63 2e  u.bc */..  u.bc.
1de4f 61 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  aMem = &p->aMem[
1de50 70 4f 70 2d 3e 70 34 2e 69 5d 3b 0a 20 20 2f 2a  pOp->p4.i];.  /*
1de51 20 41 73 73 65 72 74 20 74 68 61 74 20 74 68 65   Assert that the
1de52 20 76 61 6c 75 65 73 20 6f 66 20 70 61 72 61 6d   values of param
1de53 65 74 65 72 73 20 50 31 20 61 6e 64 20 50 34 20  eters P1 and P4 
1de54 61 72 65 20 69 6e 20 72 61 6e 67 65 2e 20 2a 2f  are in range. */
1de55 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1de56 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32  p4type==P4_INT32
1de57 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f   );.  assert( pO
1de58 70 2d 3e 70 34 2e 69 3e 30 20 26 26 20 70 4f 70  p->p4.i>0 && pOp
1de59 2d 3e 70 34 2e 69 3c 3d 70 2d 3e 6e 4d 65 6d 20  ->p4.i<=p->nMem 
1de5a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  );.  assert( pOp
1de5b 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e  ->p1>=0 && pOp->
1de5c 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b  p1<p->nCursor );
1de5d 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20  ..  /* Find the 
1de5e 69 6e 64 65 78 20 63 75 72 73 6f 72 2e 20 2a 2f  index cursor. */
1de5f 0a 20 20 75 2e 62 63 2e 70 43 78 20 3d 20 70 2d  .  u.bc.pCx = p-
1de60 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b  >apCsr[pOp->p1];
1de61 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 63 2e  .  assert( u.bc.
1de62 70 43 78 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76  pCx->deferredMov
1de63 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 75 2e 62 63  eto==0 );.  u.bc
1de64 2e 70 43 78 2d 3e 73 65 65 6b 52 65 73 75 6c 74  .pCx->seekResult
1de65 20 3d 20 30 3b 0a 20 20 75 2e 62 63 2e 70 43 78   = 0;.  u.bc.pCx
1de66 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20  ->cacheStatus = 
1de67 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 75  CACHE_STALE;.  u
1de68 2e 62 63 2e 70 43 72 73 72 20 3d 20 75 2e 62 63  .bc.pCrsr = u.bc
1de69 2e 70 43 78 2d 3e 70 43 75 72 73 6f 72 3b 0a 0a  .pCx->pCursor;..
1de6a 20 20 2f 2a 20 49 66 20 61 6e 79 20 6f 66 20 74    /* If any of t
1de6b 68 65 20 76 61 6c 75 65 73 20 61 72 65 20 4e 55  he values are NU
1de6c 4c 4c 2c 20 74 61 6b 65 20 74 68 65 20 6a 75 6d  LL, take the jum
1de6d 70 2e 20 2a 2f 0a 20 20 75 2e 62 63 2e 6e 46 69  p. */.  u.bc.nFi
1de6e 65 6c 64 20 3d 20 75 2e 62 63 2e 70 43 78 2d 3e  eld = u.bc.pCx->
1de6f 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64  pKeyInfo->nField
1de70 3b 0a 20 20 66 6f 72 28 75 2e 62 63 2e 69 69 3d  ;.  for(u.bc.ii=
1de71 30 3b 20 75 2e 62 63 2e 69 69 3c 75 2e 62 63 2e  0; u.bc.ii<u.bc.
1de72 6e 46 69 65 6c 64 3b 20 75 2e 62 63 2e 69 69 2b  nField; u.bc.ii+
1de73 2b 29 7b 0a 20 20 20 20 69 66 28 20 75 2e 62 63  +){.    if( u.bc
1de74 2e 61 4d 65 6d 5b 75 2e 62 63 2e 69 69 5d 2e 66  .aMem[u.bc.ii].f
1de75 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20  lags & MEM_Null 
1de76 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f  ){.      pc = pO
1de77 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20  p->p2 - 1;.     
1de78 20 75 2e 62 63 2e 70 43 72 73 72 20 3d 20 30 3b   u.bc.pCrsr = 0;
1de79 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1de7a 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74    }.  }.  assert
1de7b 28 20 28 75 2e 62 63 2e 61 4d 65 6d 5b 75 2e 62  ( (u.bc.aMem[u.b
1de7c 63 2e 6e 46 69 65 6c 64 5d 2e 66 6c 61 67 73 20  c.nField].flags 
1de7d 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29  & MEM_Null)==0 )
1de7e 3b 0a 0a 20 20 69 66 28 20 75 2e 62 63 2e 70 43  ;..  if( u.bc.pC
1de7f 72 73 72 21 3d 30 20 29 7b 0a 20 20 20 20 2f 2a  rsr!=0 ){.    /*
1de80 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 69 6e   Populate the in
1de81 64 65 78 20 73 65 61 72 63 68 20 6b 65 79 2e 20  dex search key. 
1de82 2a 2f 0a 20 20 20 20 75 2e 62 63 2e 72 2e 70 4b  */.    u.bc.r.pK
1de83 65 79 49 6e 66 6f 20 3d 20 75 2e 62 63 2e 70 43  eyInfo = u.bc.pC
1de84 78 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20  x->pKeyInfo;.   
1de85 20 75 2e 62 63 2e 72 2e 6e 46 69 65 6c 64 20 3d   u.bc.r.nField =
1de86 20 75 2e 62 63 2e 6e 46 69 65 6c 64 20 2b 20 31   u.bc.nField + 1
1de87 3b 0a 20 20 20 20 75 2e 62 63 2e 72 2e 66 6c 61  ;.    u.bc.r.fla
1de88 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f 50 52  gs = UNPACKED_PR
1de89 45 46 49 58 5f 53 45 41 52 43 48 3b 0a 20 20 20  EFIX_SEARCH;.   
1de8a 20 75 2e 62 63 2e 72 2e 61 4d 65 6d 20 3d 20 75   u.bc.r.aMem = u
1de8b 2e 62 63 2e 61 4d 65 6d 3b 0a 0a 20 20 20 20 2f  .bc.aMem;..    /
1de8c 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 76 61  * Extract the va
1de8d 6c 75 65 20 6f 66 20 75 2e 62 63 2e 52 20 66 72  lue of u.bc.R fr
1de8e 6f 6d 20 72 65 67 69 73 74 65 72 20 50 33 2e 20  om register P3. 
1de8f 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  */.    sqlite3Vd
1de90 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28  beMemIntegerify(
1de91 70 49 6e 33 29 3b 0a 20 20 20 20 75 2e 62 63 2e  pIn3);.    u.bc.
1de92 52 20 3d 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 0a  R = pIn3->u.i;..
1de93 20 20 20 20 2f 2a 20 53 65 61 72 63 68 20 74 68      /* Search th
1de94 65 20 42 2d 54 72 65 65 20 69 6e 64 65 78 2e 20  e B-Tree index. 
1de95 49 66 20 6e 6f 20 63 6f 6e 66 6c 69 63 74 69 6e  If no conflictin
1de96 67 20 72 65 63 6f 72 64 20 69 73 20 66 6f 75 6e  g record is foun
1de97 64 2c 20 6a 75 6d 70 0a 20 20 20 20 2a 2a 20 74  d, jump.    ** t
1de98 6f 20 50 32 2e 20 4f 74 68 65 72 77 69 73 65 2c  o P2. Otherwise,
1de99 20 63 6f 70 79 20 74 68 65 20 72 6f 77 69 64 20   copy the rowid 
1de9a 6f 66 20 74 68 65 20 63 6f 6e 66 6c 69 63 74 69  of the conflicti
1de9b 6e 67 20 72 65 63 6f 72 64 20 74 6f 0a 20 20 20  ng record to.   
1de9c 20 2a 2a 20 72 65 67 69 73 74 65 72 20 50 33 20   ** register P3 
1de9d 61 6e 64 20 66 61 6c 6c 20 74 68 72 6f 75 67 68  and fall through
1de9e 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73   to the next ins
1de9f 74 72 75 63 74 69 6f 6e 2e 20 20 2a 2f 0a 20 20  truction.  */.  
1dea0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1dea1 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65  reeMovetoUnpacke
1dea2 64 28 75 2e 62 63 2e 70 43 72 73 72 2c 20 26 75  d(u.bc.pCrsr, &u
1dea3 2e 62 63 2e 72 2c 20 30 2c 20 30 2c 20 26 75 2e  .bc.r, 0, 0, &u.
1dea4 62 63 2e 70 43 78 2d 3e 73 65 65 6b 52 65 73 75  bc.pCx->seekResu
1dea5 6c 74 29 3b 0a 20 20 20 20 69 66 28 20 28 75 2e  lt);.    if( (u.
1dea6 62 63 2e 72 2e 66 6c 61 67 73 20 26 20 55 4e 50  bc.r.flags & UNP
1dea7 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41  ACKED_PREFIX_SEA
1dea8 52 43 48 29 20 7c 7c 20 75 2e 62 63 2e 72 2e 72  RCH) || u.bc.r.r
1dea9 6f 77 69 64 3d 3d 75 2e 62 63 2e 52 20 29 7b 0a  owid==u.bc.R ){.
1deaa 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e        pc = pOp->
1deab 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d 65 6c 73  p2 - 1;.    }els
1deac 65 7b 0a 20 20 20 20 20 20 70 49 6e 33 2d 3e 75  e{.      pIn3->u
1dead 2e 69 20 3d 20 75 2e 62 63 2e 72 2e 72 6f 77 69  .i = u.bc.r.rowi
1deae 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62  d;.    }.  }.  b
1deaf 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1deb0 64 65 3a 20 4e 6f 74 45 78 69 73 74 73 20 50 31  de: NotExists P1
1deb1 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a   P2 P3 * *.**.**
1deb2 20 55 73 65 20 74 68 65 20 63 6f 6e 74 65 6e 74   Use the content
1deb3 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 33 20   of register P3 
1deb4 61 73 20 61 20 69 6e 74 65 67 65 72 20 6b 65 79  as a integer key
1deb5 2e 20 20 49 66 20 61 20 72 65 63 6f 72 64 20 0a  .  If a record .
1deb6 2a 2a 20 77 69 74 68 20 74 68 61 74 20 6b 65 79  ** with that key
1deb7 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20   does not exist 
1deb8 69 6e 20 74 61 62 6c 65 20 6f 66 20 50 31 2c 20  in table of P1, 
1deb9 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e  then jump to P2.
1deba 20 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 63 6f   .** If the reco
1debb 72 64 20 64 6f 65 73 20 65 78 69 73 74 2c 20 74  rd does exist, t
1debc 68 65 6e 20 66 61 6c 6c 20 74 68 72 75 2e 20 20  hen fall thru.  
1debd 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65  The cursor is le
1debe 66 74 20 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 20  ft .** pointing 
1debf 74 6f 20 74 68 65 20 72 65 63 6f 72 64 20 69 66  to the record if
1dec0 20 69 74 20 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a   it exists..**.*
1dec1 2a 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65  * The difference
1dec2 20 62 65 74 77 65 65 6e 20 74 68 69 73 20 6f 70   between this op
1dec3 65 72 61 74 69 6f 6e 20 61 6e 64 20 4e 6f 74 46  eration and NotF
1dec4 6f 75 6e 64 20 69 73 20 74 68 61 74 20 74 68 69  ound is that thi
1dec5 73 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 61  s.** operation a
1dec6 73 73 75 6d 65 73 20 74 68 65 20 6b 65 79 20 69  ssumes the key i
1dec7 73 20 61 6e 20 69 6e 74 65 67 65 72 20 61 6e 64  s an integer and
1dec8 20 74 68 61 74 20 50 31 20 69 73 20 61 20 74 61   that P1 is a ta
1dec9 62 6c 65 20 77 68 65 72 65 61 73 0a 2a 2a 20 4e  ble whereas.** N
1deca 6f 74 46 6f 75 6e 64 20 61 73 73 75 6d 65 73 20  otFound assumes 
1decb 6b 65 79 20 69 73 20 61 20 62 6c 6f 62 20 63 6f  key is a blob co
1decc 6e 73 74 72 75 63 74 65 64 20 66 72 6f 6d 20 4d  nstructed from M
1decd 61 6b 65 52 65 63 6f 72 64 20 61 6e 64 0a 2a 2a  akeRecord and.**
1dece 20 50 31 20 69 73 20 61 6e 20 69 6e 64 65 78 2e   P1 is an index.
1decf 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  .**.** See also:
1ded0 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64   Found, NotFound
1ded1 2c 20 49 73 55 6e 69 71 75 65 0a 2a 2f 0a 63 61  , IsUnique.*/.ca
1ded2 73 65 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 3a  se OP_NotExists:
1ded3 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d   {        /* jum
1ded4 70 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20  p, in3 */.#if 0 
1ded5 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1ded6 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1ded7 2e 62 64 20 2a 2f 0a 20 20 56 64 62 65 43 75 72  .bd */.  VdbeCur
1ded8 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72  sor *pC;.  BtCur
1ded9 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e  sor *pCrsr;.  in
1deda 74 20 72 65 73 3b 0a 20 20 75 36 34 20 69 4b 65  t res;.  u64 iKe
1dedb 79 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  y;.#endif /* loc
1dedc 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1dedd 65 64 20 69 6e 74 6f 20 75 2e 62 64 20 2a 2f 0a  ed into u.bd */.
1dede 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 33 2d  .  assert( pIn3-
1dedf 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74  >flags & MEM_Int
1dee0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f   );.  assert( pO
1dee1 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d  p->p1>=0 && pOp-
1dee2 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29  >p1<p->nCursor )
1dee3 3b 0a 20 20 75 2e 62 64 2e 70 43 20 3d 20 70 2d  ;.  u.bd.pC = p-
1dee4 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b  >apCsr[pOp->p1];
1dee5 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 64 2e  .  assert( u.bd.
1dee6 70 43 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  pC!=0 );.  asser
1dee7 74 28 20 75 2e 62 64 2e 70 43 2d 3e 69 73 54 61  t( u.bd.pC->isTa
1dee8 62 6c 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ble );.  assert(
1dee9 20 75 2e 62 64 2e 70 43 2d 3e 70 73 65 75 64 6f   u.bd.pC->pseudo
1deea 54 61 62 6c 65 52 65 67 3d 3d 30 20 29 3b 0a 20  TableReg==0 );. 
1deeb 20 75 2e 62 64 2e 70 43 72 73 72 20 3d 20 75 2e   u.bd.pCrsr = u.
1deec 62 64 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a  bd.pC->pCursor;.
1deed 20 20 69 66 28 20 75 2e 62 64 2e 70 43 72 73 72    if( u.bd.pCrsr
1deee 21 3d 30 20 29 7b 0a 20 20 20 20 75 2e 62 64 2e  !=0 ){.    u.bd.
1deef 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62  res = 0;.    u.b
1def0 64 2e 69 4b 65 79 20 3d 20 70 49 6e 33 2d 3e 75  d.iKey = pIn3->u
1def1 2e 69 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  .i;.    rc = sql
1def2 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55  ite3BtreeMovetoU
1def3 6e 70 61 63 6b 65 64 28 75 2e 62 64 2e 70 43 72  npacked(u.bd.pCr
1def4 73 72 2c 20 30 2c 20 75 2e 62 64 2e 69 4b 65 79  sr, 0, u.bd.iKey
1def5 2c 20 30 2c 20 26 75 2e 62 64 2e 72 65 73 29 3b  , 0, &u.bd.res);
1def6 0a 20 20 20 20 75 2e 62 64 2e 70 43 2d 3e 6c 61  .    u.bd.pC->la
1def7 73 74 52 6f 77 69 64 20 3d 20 70 49 6e 33 2d 3e  stRowid = pIn3->
1def8 75 2e 69 3b 0a 20 20 20 20 75 2e 62 64 2e 70 43  u.i;.    u.bd.pC
1def9 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d  ->rowidIsValid =
1defa 20 75 2e 62 64 2e 72 65 73 3d 3d 30 20 3f 31 3a   u.bd.res==0 ?1:
1defb 30 3b 0a 20 20 20 20 75 2e 62 64 2e 70 43 2d 3e  0;.    u.bd.pC->
1defc 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20 20 20  nullRow = 0;.   
1defd 20 75 2e 62 64 2e 70 43 2d 3e 63 61 63 68 65 53   u.bd.pC->cacheS
1defe 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54  tatus = CACHE_ST
1deff 41 4c 45 3b 0a 20 20 20 20 75 2e 62 64 2e 70 43  ALE;.    u.bd.pC
1df00 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f  ->deferredMoveto
1df01 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 75 2e   = 0;.    if( u.
1df02 62 64 2e 72 65 73 21 3d 30 20 29 7b 0a 20 20 20  bd.res!=0 ){.   
1df03 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20     pc = pOp->p2 
1df04 2d 20 31 3b 0a 20 20 20 20 20 20 61 73 73 65 72  - 1;.      asser
1df05 74 28 20 75 2e 62 64 2e 70 43 2d 3e 72 6f 77 69  t( u.bd.pC->rowi
1df06 64 49 73 56 61 6c 69 64 3d 3d 30 20 29 3b 0a 20  dIsValid==0 );. 
1df07 20 20 20 7d 0a 20 20 20 20 75 2e 62 64 2e 70 43     }.    u.bd.pC
1df08 2d 3e 73 65 65 6b 52 65 73 75 6c 74 20 3d 20 75  ->seekResult = u
1df09 2e 62 64 2e 72 65 73 3b 0a 20 20 7d 65 6c 73 65  .bd.res;.  }else
1df0a 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 68 61  {.    /* This ha
1df0b 70 70 65 6e 73 20 77 68 65 6e 20 61 6e 20 61 74  ppens when an at
1df0c 74 65 6d 70 74 20 74 6f 20 6f 70 65 6e 20 61 20  tempt to open a 
1df0d 72 65 61 64 20 63 75 72 73 6f 72 20 6f 6e 20 74  read cursor on t
1df0e 68 65 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65  he.    ** sqlite
1df0f 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20 72 65  _master table re
1df10 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 45 4d 50  turns SQLITE_EMP
1df11 54 59 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70  TY..    */.    p
1df12 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b  c = pOp->p2 - 1;
1df13 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62  .    assert( u.b
1df14 64 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c  d.pC->rowidIsVal
1df15 69 64 3d 3d 30 20 29 3b 0a 20 20 20 20 75 2e 62  id==0 );.    u.b
1df16 64 2e 70 43 2d 3e 73 65 65 6b 52 65 73 75 6c 74  d.pC->seekResult
1df17 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 62 72 65 61   = 0;.  }.  brea
1df18 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1df19 20 53 65 71 75 65 6e 63 65 20 50 31 20 50 32 20   Sequence P1 P2 
1df1a 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 69 6e 64  * * *.**.** Find
1df1b 20 74 68 65 20 6e 65 78 74 20 61 76 61 69 6c 61   the next availa
1df1c 62 6c 65 20 73 65 71 75 65 6e 63 65 20 6e 75 6d  ble sequence num
1df1d 62 65 72 20 66 6f 72 20 63 75 72 73 6f 72 20 50  ber for cursor P
1df1e 31 2e 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20  1..** Write the 
1df1f 73 65 71 75 65 6e 63 65 20 6e 75 6d 62 65 72 20  sequence number 
1df20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32  into register P2
1df21 2e 0a 2a 2a 20 54 68 65 20 73 65 71 75 65 6e 63  ..** The sequenc
1df22 65 20 6e 75 6d 62 65 72 20 6f 6e 20 74 68 65 20  e number on the 
1df23 63 75 72 73 6f 72 20 69 73 20 69 6e 63 72 65 6d  cursor is increm
1df24 65 6e 74 65 64 20 61 66 74 65 72 20 74 68 69 73  ented after this
1df25 0a 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  .** instruction.
1df26 20 20 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65    .*/.case OP_Se
1df27 71 75 65 6e 63 65 3a 20 7b 20 20 20 20 20 20 20  quence: {       
1df28 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72      /* out2-prer
1df29 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 73 65  elease */.  asse
1df2a 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26  rt( pOp->p1>=0 &
1df2b 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75  & pOp->p1<p->nCu
1df2c 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74  rsor );.  assert
1df2d 28 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e  ( p->apCsr[pOp->
1df2e 70 31 5d 21 3d 30 20 29 3b 0a 20 20 70 4f 75 74  p1]!=0 );.  pOut
1df2f 2d 3e 75 2e 69 20 3d 20 70 2d 3e 61 70 43 73 72  ->u.i = p->apCsr
1df30 5b 70 4f 70 2d 3e 70 31 5d 2d 3e 73 65 71 43 6f  [pOp->p1]->seqCo
1df31 75 6e 74 2b 2b 3b 0a 20 20 4d 65 6d 53 65 74 54  unt++;.  MemSetT
1df32 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45  ypeFlag(pOut, ME
1df33 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b  M_Int);.  break;
1df34 0a 7d 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  .}.../* Opcode: 
1df35 4e 65 77 52 6f 77 69 64 20 50 31 20 50 32 20 50  NewRowid P1 P2 P
1df36 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 47 65 74 20  3 * *.**.** Get 
1df37 61 20 6e 65 77 20 69 6e 74 65 67 65 72 20 72 65  a new integer re
1df38 63 6f 72 64 20 6e 75 6d 62 65 72 20 28 61 2e 6b  cord number (a.k
1df39 2e 61 20 22 72 6f 77 69 64 22 29 20 75 73 65 64  .a "rowid") used
1df3a 20 61 73 20 74 68 65 20 6b 65 79 20 74 6f 20 61   as the key to a
1df3b 20 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 72   table..** The r
1df3c 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73 20  ecord number is 
1df3d 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20 75  not previously u
1df3e 73 65 64 20 61 73 20 61 20 6b 65 79 20 69 6e 20  sed as a key in 
1df3f 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  the database.** 
1df40 74 61 62 6c 65 20 74 68 61 74 20 63 75 72 73 6f  table that curso
1df41 72 20 50 31 20 70 6f 69 6e 74 73 20 74 6f 2e 20  r P1 points to. 
1df42 20 54 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20   The new record 
1df43 6e 75 6d 62 65 72 20 69 73 20 77 72 69 74 74 65  number is writte
1df44 6e 0a 2a 2a 20 77 72 69 74 74 65 6e 20 74 6f 20  n.** written to 
1df45 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a  register P2..**.
1df46 2a 2a 20 49 66 20 50 33 3e 30 20 74 68 65 6e 20  ** If P3>0 then 
1df47 50 33 20 69 73 20 61 20 72 65 67 69 73 74 65 72  P3 is a register
1df48 20 69 6e 20 74 68 65 20 72 6f 6f 74 20 66 72 61   in the root fra
1df49 6d 65 20 6f 66 20 74 68 69 73 20 56 44 42 45 20  me of this VDBE 
1df4a 74 68 61 74 20 68 6f 6c 64 73 20 0a 2a 2a 20 74  that holds .** t
1df4b 68 65 20 6c 61 72 67 65 73 74 20 70 72 65 76 69  he largest previ
1df4c 6f 75 73 6c 79 20 67 65 6e 65 72 61 74 65 64 20  ously generated 
1df4d 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 2e 20 4e  record number. N
1df4e 6f 20 6e 65 77 20 72 65 63 6f 72 64 20 6e 75 6d  o new record num
1df4f 62 65 72 73 20 61 72 65 0a 2a 2a 20 61 6c 6c 6f  bers are.** allo
1df50 77 65 64 20 74 6f 20 62 65 20 6c 65 73 73 20 74  wed to be less t
1df51 68 61 6e 20 74 68 69 73 20 76 61 6c 75 65 2e 20  han this value. 
1df52 57 68 65 6e 20 74 68 69 73 20 76 61 6c 75 65 20  When this value 
1df53 72 65 61 63 68 65 73 20 69 74 73 20 6d 61 78 69  reaches its maxi
1df54 6d 75 6d 2c 20 0a 2a 2a 20 61 20 53 51 4c 49 54  mum, .** a SQLIT
1df55 45 5f 46 55 4c 4c 20 65 72 72 6f 72 20 69 73 20  E_FULL error is 
1df56 67 65 6e 65 72 61 74 65 64 2e 20 54 68 65 20 50  generated. The P
1df57 33 20 72 65 67 69 73 74 65 72 20 69 73 20 75 70  3 register is up
1df58 64 61 74 65 64 20 77 69 74 68 20 74 68 65 20 27  dated with the '
1df59 0a 2a 2a 20 67 65 6e 65 72 61 74 65 64 20 72 65  .** generated re
1df5a 63 6f 72 64 20 6e 75 6d 62 65 72 2e 20 54 68 69  cord number. Thi
1df5b 73 20 50 33 20 6d 65 63 68 61 6e 69 73 6d 20 69  s P3 mechanism i
1df5c 73 20 75 73 65 64 20 74 6f 20 68 65 6c 70 20 69  s used to help i
1df5d 6d 70 6c 65 6d 65 6e 74 20 74 68 65 0a 2a 2a 20  mplement the.** 
1df5e 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 66 65  AUTOINCREMENT fe
1df5f 61 74 75 72 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f  ature..*/.case O
1df60 50 5f 4e 65 77 52 6f 77 69 64 3a 20 7b 20 20 20  P_NewRowid: {   
1df61 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d          /* out2-
1df62 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69  prerelease */.#i
1df63 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1df64 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1df65 74 6f 20 75 2e 62 65 20 2a 2f 0a 20 20 69 36 34  to u.be */.  i64
1df66 20 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   v;             
1df67 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72      /* The new r
1df68 6f 77 69 64 20 2a 2f 0a 20 20 56 64 62 65 43 75  owid */.  VdbeCu
1df69 72 73 6f 72 20 2a 70 43 3b 20 20 20 20 20 20 20  rsor *pC;       
1df6a 20 2f 2a 20 43 75 72 73 6f 72 20 6f 66 20 74 61   /* Cursor of ta
1df6b 62 6c 65 20 74 6f 20 67 65 74 20 74 68 65 20 6e  ble to get the n
1df6c 65 77 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69 6e  ew rowid */.  in
1df6d 74 20 72 65 73 3b 20 20 20 20 20 20 20 20 20 20  t res;          
1df6e 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f       /* Result o
1df6f 66 20 61 6e 20 73 71 6c 69 74 65 33 42 74 72 65  f an sqlite3Btre
1df70 65 4c 61 73 74 28 29 20 2a 2f 0a 20 20 69 6e 74  eLast() */.  int
1df71 20 63 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20   cnt;           
1df72 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 20 74      /* Counter t
1df73 6f 20 6c 69 6d 69 74 20 74 68 65 20 6e 75 6d 62  o limit the numb
1df74 65 72 20 6f 66 20 73 65 61 72 63 68 65 73 20 2a  er of searches *
1df75 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20  /.  Mem *pMem;  
1df76 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
1df77 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 6c  gister holding l
1df78 61 72 67 65 73 74 20 72 6f 77 69 64 20 66 6f 72  argest rowid for
1df79 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a   AUTOINCREMENT *
1df7a 2f 0a 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70  /.  VdbeFrame *p
1df7b 46 72 61 6d 65 3b 20 20 20 20 20 2f 2a 20 52 6f  Frame;     /* Ro
1df7c 6f 74 20 66 72 61 6d 65 20 6f 66 20 56 44 42 45  ot frame of VDBE
1df7d 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f   */.#endif /* lo
1df7e 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1df7f 76 65 64 20 69 6e 74 6f 20 75 2e 62 65 20 2a 2f  ved into u.be */
1df80 0a 0a 20 20 75 2e 62 65 2e 76 20 3d 20 30 3b 0a  ..  u.be.v = 0;.
1df81 20 20 75 2e 62 65 2e 72 65 73 20 3d 20 30 3b 0a    u.be.res = 0;.
1df82 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1df83 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c  1>=0 && pOp->p1<
1df84 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
1df85 75 2e 62 65 2e 70 43 20 3d 20 70 2d 3e 61 70 43  u.be.pC = p->apC
1df86 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61  sr[pOp->p1];.  a
1df87 73 73 65 72 74 28 20 75 2e 62 65 2e 70 43 21 3d  ssert( u.be.pC!=
1df88 30 20 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52  0 );.  if( NEVER
1df89 28 75 2e 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f  (u.be.pC->pCurso
1df8a 72 3d 3d 30 29 20 29 7b 0a 20 20 20 20 2f 2a 20  r==0) ){.    /* 
1df8b 54 68 65 20 7a 65 72 6f 20 69 6e 69 74 69 61 6c  The zero initial
1df8c 69 7a 61 74 69 6f 6e 20 61 62 6f 76 65 20 69 73  ization above is
1df8d 20 61 6c 6c 20 74 68 61 74 20 69 73 20 6e 65 65   all that is nee
1df8e 64 65 64 20 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a  ded */.  }else{.
1df8f 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20      /* The next 
1df90 72 6f 77 69 64 20 6f 72 20 72 65 63 6f 72 64 20  rowid or record 
1df91 6e 75 6d 62 65 72 20 28 64 69 66 66 65 72 65 6e  number (differen
1df92 74 20 74 65 72 6d 73 20 66 6f 72 20 74 68 65 20  t terms for the 
1df93 73 61 6d 65 0a 20 20 20 20 2a 2a 20 74 68 69 6e  same.    ** thin
1df94 67 29 20 69 73 20 6f 62 74 61 69 6e 65 64 20 69  g) is obtained i
1df95 6e 20 61 20 74 77 6f 2d 73 74 65 70 20 61 6c 67  n a two-step alg
1df96 6f 72 69 74 68 6d 2e 0a 20 20 20 20 2a 2a 0a 20  orithm..    **. 
1df97 20 20 20 2a 2a 20 46 69 72 73 74 20 77 65 20 61     ** First we a
1df98 74 74 65 6d 70 74 20 74 6f 20 66 69 6e 64 20 74  ttempt to find t
1df99 68 65 20 6c 61 72 67 65 73 74 20 65 78 69 73 74  he largest exist
1df9a 69 6e 67 20 72 6f 77 69 64 20 61 6e 64 20 61 64  ing rowid and ad
1df9b 64 20 6f 6e 65 0a 20 20 20 20 2a 2a 20 74 6f 20  d one.    ** to 
1df9c 74 68 61 74 2e 20 20 42 75 74 20 69 66 20 74 68  that.  But if th
1df9d 65 20 6c 61 72 67 65 73 74 20 65 78 69 73 74 69  e largest existi
1df9e 6e 67 20 72 6f 77 69 64 20 69 73 20 61 6c 72 65  ng rowid is alre
1df9f 61 64 79 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a  ady the maximum.
1dfa0 20 20 20 20 2a 2a 20 70 6f 73 69 74 69 76 65 20      ** positive 
1dfa1 69 6e 74 65 67 65 72 2c 20 77 65 20 68 61 76 65  integer, we have
1dfa2 20 74 6f 20 66 61 6c 6c 20 74 68 72 6f 75 67 68   to fall through
1dfa3 20 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 0a 20   to the second. 
1dfa4 20 20 20 2a 2a 20 70 72 6f 62 61 62 69 6c 69 73     ** probabilis
1dfa5 74 69 63 20 61 6c 67 6f 72 69 74 68 6d 0a 20 20  tic algorithm.  
1dfa6 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20    **.    ** The 
1dfa7 73 65 63 6f 6e 64 20 61 6c 67 6f 72 69 74 68 6d  second algorithm
1dfa8 20 69 73 20 74 6f 20 73 65 6c 65 63 74 20 61 20   is to select a 
1dfa9 72 6f 77 69 64 20 61 74 20 72 61 6e 64 6f 6d 20  rowid at random 
1dfaa 61 6e 64 20 73 65 65 20 69 66 0a 20 20 20 20 2a  and see if.    *
1dfab 2a 20 69 74 20 61 6c 72 65 61 64 79 20 65 78 69  * it already exi
1dfac 73 74 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65  sts in the table
1dfad 2e 20 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f  .  If it does no
1dfae 74 20 65 78 69 73 74 2c 20 77 65 20 68 61 76 65  t exist, we have
1dfaf 0a 20 20 20 20 2a 2a 20 73 75 63 63 65 65 64 65  .    ** succeede
1dfb0 64 2e 20 20 49 66 20 74 68 65 20 72 61 6e 64 6f  d.  If the rando
1dfb1 6d 20 72 6f 77 69 64 20 64 6f 65 73 20 65 78 69  m rowid does exi
1dfb2 73 74 2c 20 77 65 20 73 65 6c 65 63 74 20 61 20  st, we select a 
1dfb3 6e 65 77 20 6f 6e 65 0a 20 20 20 20 2a 2a 20 61  new one.    ** a
1dfb4 6e 64 20 74 72 79 20 61 67 61 69 6e 2c 20 75 70  nd try again, up
1dfb5 20 74 6f 20 31 30 30 20 74 69 6d 65 73 2e 0a 20   to 100 times.. 
1dfb6 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74     */.    assert
1dfb7 28 20 75 2e 62 65 2e 70 43 2d 3e 69 73 54 61 62  ( u.be.pC->isTab
1dfb8 6c 65 20 29 3b 0a 20 20 20 20 75 2e 62 65 2e 63  le );.    u.be.c
1dfb9 6e 74 20 3d 20 30 3b 0a 0a 23 69 66 64 65 66 20  nt = 0;..#ifdef 
1dfba 53 51 4c 49 54 45 5f 33 32 42 49 54 5f 52 4f 57  SQLITE_32BIT_ROW
1dfbb 49 44 0a 23 20 20 20 64 65 66 69 6e 65 20 4d 41  ID.#   define MA
1dfbc 58 5f 52 4f 57 49 44 20 30 78 37 66 66 66 66 66  X_ROWID 0x7fffff
1dfbd 66 66 0a 23 65 6c 73 65 0a 20 20 20 20 2f 2a 20  ff.#else.    /* 
1dfbe 53 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 73 20 63  Some compilers c
1dfbf 6f 6d 70 6c 61 69 6e 20 61 62 6f 75 74 20 63 6f  omplain about co
1dfc0 6e 73 74 61 6e 74 73 20 6f 66 20 74 68 65 20 66  nstants of the f
1dfc1 6f 72 6d 20 30 78 37 66 66 66 66 66 66 66 66 66  orm 0x7fffffffff
1dfc2 66 66 66 66 66 66 2e 0a 20 20 20 20 2a 2a 20 4f  ffffff..    ** O
1dfc3 74 68 65 72 73 20 63 6f 6d 70 6c 61 69 6e 20 61  thers complain a
1dfc4 62 6f 75 74 20 30 78 37 66 66 66 66 66 66 66 66  bout 0x7ffffffff
1dfc5 66 66 66 66 66 66 66 66 4c 4c 2e 20 20 54 68 65  ffffffffLL.  The
1dfc6 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f   following macro
1dfc7 20 73 65 65 6d 73 0a 20 20 20 20 2a 2a 20 74 6f   seems.    ** to
1dfc8 20 70 72 6f 76 69 64 65 20 74 68 65 20 63 6f 6e   provide the con
1dfc9 73 74 61 6e 74 20 77 68 69 6c 65 20 6d 61 6b 69  stant while maki
1dfca 6e 67 20 61 6c 6c 20 63 6f 6d 70 69 6c 65 72 73  ng all compilers
1dfcb 20 68 61 70 70 79 2e 0a 20 20 20 20 2a 2f 0a 23   happy..    */.#
1dfcc 20 20 20 64 65 66 69 6e 65 20 4d 41 58 5f 52 4f     define MAX_RO
1dfcd 57 49 44 20 20 28 69 36 34 29 28 20 28 28 28 75  WID  (i64)( (((u
1dfce 36 34 29 30 78 37 66 66 66 66 66 66 66 29 3c 3c  64)0x7fffffff)<<
1dfcf 33 32 29 20 7c 20 28 75 36 34 29 30 78 66 66 66  32) | (u64)0xfff
1dfd0 66 66 66 66 66 20 29 0a 23 65 6e 64 69 66 0a 0a  fffff ).#endif..
1dfd1 20 20 20 20 69 66 28 20 21 75 2e 62 65 2e 70 43      if( !u.be.pC
1dfd2 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64  ->useRandomRowid
1dfd3 20 29 7b 0a 20 20 20 20 20 20 75 2e 62 65 2e 76   ){.      u.be.v
1dfd4 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47   = sqlite3BtreeG
1dfd5 65 74 43 61 63 68 65 64 52 6f 77 69 64 28 75 2e  etCachedRowid(u.
1dfd6 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72 29 3b  be.pC->pCursor);
1dfd7 0a 20 20 20 20 20 20 69 66 28 20 75 2e 62 65 2e  .      if( u.be.
1dfd8 76 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  v==0 ){.        
1dfd9 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1dfda 65 4c 61 73 74 28 75 2e 62 65 2e 70 43 2d 3e 70  eLast(u.be.pC->p
1dfdb 43 75 72 73 6f 72 2c 20 26 75 2e 62 65 2e 72 65  Cursor, &u.be.re
1dfdc 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  s);.        if( 
1dfdd 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1dfde 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
1dfdf 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72  abort_due_to_err
1dfe0 6f 72 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  or;.        }.  
1dfe1 20 20 20 20 20 20 69 66 28 20 75 2e 62 65 2e 72        if( u.be.r
1dfe2 65 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  es ){.          
1dfe3 75 2e 62 65 2e 76 20 3d 20 31 3b 0a 20 20 20 20  u.be.v = 1;.    
1dfe4 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1dfe5 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c       assert( sql
1dfe6 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49  ite3BtreeCursorI
1dfe7 73 56 61 6c 69 64 28 75 2e 62 65 2e 70 43 2d 3e  sValid(u.be.pC->
1dfe8 70 43 75 72 73 6f 72 29 20 29 3b 0a 20 20 20 20  pCursor) );.    
1dfe9 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1dfea 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 75  e3BtreeKeySize(u
1dfeb 2e 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c  .be.pC->pCursor,
1dfec 20 26 75 2e 62 65 2e 76 29 3b 0a 20 20 20 20 20   &u.be.v);.     
1dfed 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d       assert( rc=
1dfee 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20  =SQLITE_OK );   
1dfef 2f 2a 20 43 61 6e 6e 6f 74 20 66 61 69 6c 20 66  /* Cannot fail f
1dff0 6f 6c 6c 6f 77 69 6e 67 20 42 74 72 65 65 4c 61  ollowing BtreeLa
1dff1 73 74 28 29 20 2a 2f 0a 20 20 20 20 20 20 20 20  st() */.        
1dff2 20 20 69 66 28 20 75 2e 62 65 2e 76 3d 3d 4d 41    if( u.be.v==MA
1dff3 58 5f 52 4f 57 49 44 20 29 7b 0a 20 20 20 20 20  X_ROWID ){.     
1dff4 20 20 20 20 20 20 20 75 2e 62 65 2e 70 43 2d 3e         u.be.pC->
1dff5 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64 20 3d  useRandomRowid =
1dff6 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65   1;.          }e
1dff7 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
1dff8 20 75 2e 62 65 2e 76 2b 2b 3b 0a 20 20 20 20 20   u.be.v++;.     
1dff9 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
1dffa 0a 20 20 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65  .      }..#ifnde
1dffb 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
1dffc 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 20 20  TOINCREMENT.    
1dffd 20 20 69 66 28 20 70 4f 70 2d 3e 70 33 20 29 7b    if( pOp->p3 ){
1dffe 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 73 73 65  .        /* Asse
1dfff 72 74 20 74 68 61 74 20 50 33 20 69 73 20 61 20  rt that P3 is a 
1e000 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20 63 65 6c  valid memory cel
1e001 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73  l. */.        as
1e002 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20  sert( pOp->p3>0 
1e003 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
1e004 2d 3e 70 46 72 61 6d 65 20 29 7b 0a 20 20 20 20  ->pFrame ){.    
1e005 20 20 20 20 20 20 66 6f 72 28 75 2e 62 65 2e 70        for(u.be.p
1e006 46 72 61 6d 65 3d 70 2d 3e 70 46 72 61 6d 65 3b  Frame=p->pFrame;
1e007 20 75 2e 62 65 2e 70 46 72 61 6d 65 2d 3e 70 50   u.be.pFrame->pP
1e008 61 72 65 6e 74 3b 20 75 2e 62 65 2e 70 46 72 61  arent; u.be.pFra
1e009 6d 65 3d 75 2e 62 65 2e 70 46 72 61 6d 65 2d 3e  me=u.be.pFrame->
1e00a 70 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 20 20  pParent);.      
1e00b 20 20 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68      /* Assert th
1e00c 61 74 20 50 33 20 69 73 20 61 20 76 61 6c 69 64  at P3 is a valid
1e00d 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 2a 2f   memory cell. */
1e00e 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
1e00f 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 75 2e 62 65  t( pOp->p3<=u.be
1e010 2e 70 46 72 61 6d 65 2d 3e 6e 4d 65 6d 20 29 3b  .pFrame->nMem );
1e011 0a 20 20 20 20 20 20 20 20 20 20 75 2e 62 65 2e  .          u.be.
1e012 70 4d 65 6d 20 3d 20 26 75 2e 62 65 2e 70 46 72  pMem = &u.be.pFr
1e013 61 6d 65 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70  ame->aMem[pOp->p
1e014 33 5d 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  3];.        }els
1e015 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e{.          /* 
1e016 41 73 73 65 72 74 20 74 68 61 74 20 50 33 20 69  Assert that P3 i
1e017 73 20 61 20 76 61 6c 69 64 20 6d 65 6d 6f 72 79  s a valid memory
1e018 20 63 65 6c 6c 2e 20 2a 2f 0a 20 20 20 20 20 20   cell. */.      
1e019 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
1e01a 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a  >p3<=p->nMem );.
1e01b 20 20 20 20 20 20 20 20 20 20 75 2e 62 65 2e 70            u.be.p
1e01c 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  Mem = &p->aMem[p
1e01d 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20 20  Op->p3];.       
1e01e 20 7d 0a 0a 20 20 20 20 20 20 20 20 52 45 47 49   }..        REGI
1e01f 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e  STER_TRACE(pOp->
1e020 70 33 2c 20 75 2e 62 65 2e 70 4d 65 6d 29 3b 0a  p3, u.be.pMem);.
1e021 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1e022 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79  dbeMemIntegerify
1e023 28 75 2e 62 65 2e 70 4d 65 6d 29 3b 0a 20 20 20  (u.be.pMem);.   
1e024 20 20 20 20 20 61 73 73 65 72 74 28 20 28 75 2e       assert( (u.
1e025 62 65 2e 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  be.pMem->flags &
1e026 20 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 29 3b 20   MEM_Int)!=0 ); 
1e027 20 2f 2a 20 6d 65 6d 28 50 33 29 20 68 6f 6c 64   /* mem(P3) hold
1e028 73 20 61 6e 20 69 6e 74 65 67 65 72 20 2a 2f 0a  s an integer */.
1e029 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 62 65          if( u.be
1e02a 2e 70 4d 65 6d 2d 3e 75 2e 69 3d 3d 4d 41 58 5f  .pMem->u.i==MAX_
1e02b 52 4f 57 49 44 20 7c 7c 20 75 2e 62 65 2e 70 43  ROWID || u.be.pC
1e02c 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64  ->useRandomRowid
1e02d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63   ){.          rc
1e02e 20 3d 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a   = SQLITE_FULL;.
1e02f 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61            goto a
1e030 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f  bort_due_to_erro
1e031 72 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  r;.        }.   
1e032 20 20 20 20 20 69 66 28 20 75 2e 62 65 2e 76 3c       if( u.be.v<
1e033 75 2e 62 65 2e 70 4d 65 6d 2d 3e 75 2e 69 2b 31  u.be.pMem->u.i+1
1e034 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75 2e   ){.          u.
1e035 62 65 2e 76 20 3d 20 75 2e 62 65 2e 70 4d 65 6d  be.v = u.be.pMem
1e036 2d 3e 75 2e 69 20 2b 20 31 3b 0a 20 20 20 20 20  ->u.i + 1;.     
1e037 20 20 20 7d 0a 20 20 20 20 20 20 20 20 75 2e 62     }.        u.b
1e038 65 2e 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 75 2e  e.pMem->u.i = u.
1e039 62 65 2e 76 3b 0a 20 20 20 20 20 20 7d 0a 23 65  be.v;.      }.#e
1e03a 6e 64 69 66 0a 0a 20 20 20 20 20 20 73 71 6c 69  ndif..      sqli
1e03b 74 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65  te3BtreeSetCache
1e03c 64 52 6f 77 69 64 28 75 2e 62 65 2e 70 43 2d 3e  dRowid(u.be.pC->
1e03d 70 43 75 72 73 6f 72 2c 20 75 2e 62 65 2e 76 3c  pCursor, u.be.v<
1e03e 4d 41 58 5f 52 4f 57 49 44 20 3f 20 75 2e 62 65  MAX_ROWID ? u.be
1e03f 2e 76 2b 31 20 3a 20 30 29 3b 0a 20 20 20 20 7d  .v+1 : 0);.    }
1e040 0a 20 20 20 20 69 66 28 20 75 2e 62 65 2e 70 43  .    if( u.be.pC
1e041 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64  ->useRandomRowid
1e042 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
1e043 28 20 70 4f 70 2d 3e 70 33 3d 3d 30 20 29 3b 20  ( pOp->p3==0 ); 
1e044 20 2f 2a 20 57 65 20 63 61 6e 6e 6f 74 20 62 65   /* We cannot be
1e045 20 69 6e 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64   in random rowid
1e046 20 6d 6f 64 65 20 69 66 20 74 68 69 73 20 69 73   mode if this is
1e047 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1e048 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a                **
1e049 20 61 6e 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e   an AUTOINCREMEN
1e04a 54 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20  T table. */.    
1e04b 20 20 75 2e 62 65 2e 76 20 3d 20 64 62 2d 3e 6c    u.be.v = db->l
1e04c 61 73 74 52 6f 77 69 64 3b 0a 20 20 20 20 20 20  astRowid;.      
1e04d 75 2e 62 65 2e 63 6e 74 20 3d 20 30 3b 0a 20 20  u.be.cnt = 0;.  
1e04e 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 20 20      do{.        
1e04f 69 66 28 20 75 2e 62 65 2e 63 6e 74 3d 3d 30 20  if( u.be.cnt==0 
1e050 26 26 20 28 75 2e 62 65 2e 76 26 30 78 66 66 66  && (u.be.v&0xfff
1e051 66 66 66 29 3d 3d 75 2e 62 65 2e 76 20 29 7b 0a  fff)==u.be.v ){.
1e052 20 20 20 20 20 20 20 20 20 20 75 2e 62 65 2e 76            u.be.v
1e053 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  ++;.        }els
1e054 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  e{.          sql
1e055 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28  ite3_randomness(
1e056 73 69 7a 65 6f 66 28 75 2e 62 65 2e 76 29 2c 20  sizeof(u.be.v), 
1e057 26 75 2e 62 65 2e 76 29 3b 0a 20 20 20 20 20 20  &u.be.v);.      
1e058 20 20 20 20 69 66 28 20 75 2e 62 65 2e 63 6e 74      if( u.be.cnt
1e059 3c 35 20 29 20 75 2e 62 65 2e 76 20 26 3d 20 30  <5 ) u.be.v &= 0
1e05a 78 66 66 66 66 66 66 3b 0a 20 20 20 20 20 20 20  xffffff;.       
1e05b 20 7d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20   }.        rc = 
1e05c 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65  sqlite3BtreeMove
1e05d 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e 62 65 2e  toUnpacked(u.be.
1e05e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20  pC->pCursor, 0, 
1e05f 28 75 36 34 29 75 2e 62 65 2e 76 2c 20 30 2c 20  (u64)u.be.v, 0, 
1e060 26 75 2e 62 65 2e 72 65 73 29 3b 0a 20 20 20 20  &u.be.res);.    
1e061 20 20 20 20 75 2e 62 65 2e 63 6e 74 2b 2b 3b 0a      u.be.cnt++;.
1e062 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20 75 2e        }while( u.
1e063 62 65 2e 63 6e 74 3c 31 30 30 20 26 26 20 72 63  be.cnt<100 && rc
1e064 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 75  ==SQLITE_OK && u
1e065 2e 62 65 2e 72 65 73 3d 3d 30 20 29 3b 0a 20 20  .be.res==0 );.  
1e066 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1e067 54 45 5f 4f 4b 20 26 26 20 75 2e 62 65 2e 72 65  TE_OK && u.be.re
1e068 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  s==0 ){.        
1e069 72 63 20 3d 20 53 51 4c 49 54 45 5f 46 55 4c 4c  rc = SQLITE_FULL
1e06a 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61  ;.        goto a
1e06b 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f  bort_due_to_erro
1e06c 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  r;.      }.    }
1e06d 0a 20 20 20 20 75 2e 62 65 2e 70 43 2d 3e 72 6f  .    u.be.pC->ro
1e06e 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a  widIsValid = 0;.
1e06f 20 20 20 20 75 2e 62 65 2e 70 43 2d 3e 64 65 66      u.be.pC->def
1e070 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b  erredMoveto = 0;
1e071 0a 20 20 20 20 75 2e 62 65 2e 70 43 2d 3e 63 61  .    u.be.pC->ca
1e072 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48  cheStatus = CACH
1e073 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 4d  E_STALE;.  }.  M
1e074 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f  emSetTypeFlag(pO
1e075 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20  ut, MEM_Int);.  
1e076 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 62 65  pOut->u.i = u.be
1e077 2e 76 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  .v;.  break;.}..
1e078 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 73 65 72  /* Opcode: Inser
1e079 74 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35  t P1 P2 P3 P4 P5
1e07a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 61 6e 20  .**.** Write an 
1e07b 65 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 20 74  entry into the t
1e07c 61 62 6c 65 20 6f 66 20 63 75 72 73 6f 72 20 50  able of cursor P
1e07d 31 2e 20 20 41 20 6e 65 77 20 65 6e 74 72 79 20  1.  A new entry 
1e07e 69 73 0a 2a 2a 20 63 72 65 61 74 65 64 20 69 66  is.** created if
1e07f 20 69 74 20 64 6f 65 73 6e 27 74 20 61 6c 72 65   it doesn't alre
1e080 61 64 79 20 65 78 69 73 74 20 6f 72 20 74 68 65  ady exist or the
1e081 20 64 61 74 61 20 66 6f 72 20 61 6e 20 65 78 69   data for an exi
1e082 73 74 69 6e 67 0a 2a 2a 20 65 6e 74 72 79 20 69  sting.** entry i
1e083 73 20 6f 76 65 72 77 72 69 74 74 65 6e 2e 20 20  s overwritten.  
1e084 54 68 65 20 64 61 74 61 20 69 73 20 74 68 65 20  The data is the 
1e085 76 61 6c 75 65 20 4d 45 4d 5f 42 6c 6f 62 20 73  value MEM_Blob s
1e086 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65  tored in registe
1e087 72 0a 2a 2a 20 6e 75 6d 62 65 72 20 50 32 2e 20  r.** number P2. 
1e088 54 68 65 20 6b 65 79 20 69 73 20 73 74 6f 72 65  The key is store
1e089 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33  d in register P3
1e08a 2e 20 54 68 65 20 6b 65 79 20 6d 75 73 74 0a 2a  . The key must.*
1e08b 2a 20 62 65 20 61 20 4d 45 4d 5f 49 6e 74 2e 0a  * be a MEM_Int..
1e08c 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f 50 46  **.** If the OPF
1e08d 4c 41 47 5f 4e 43 48 41 4e 47 45 20 66 6c 61 67  LAG_NCHANGE flag
1e08e 20 6f 66 20 50 35 20 69 73 20 73 65 74 2c 20 74   of P5 is set, t
1e08f 68 65 6e 20 74 68 65 20 72 6f 77 20 63 68 61 6e  hen the row chan
1e090 67 65 20 63 6f 75 6e 74 20 69 73 0a 2a 2a 20 69  ge count is.** i
1e091 6e 63 72 65 6d 65 6e 74 65 64 20 28 6f 74 68 65  ncremented (othe
1e092 72 77 69 73 65 20 6e 6f 74 29 2e 20 20 49 66 20  rwise not).  If 
1e093 74 68 65 20 4f 50 46 4c 41 47 5f 4c 41 53 54 52  the OPFLAG_LASTR
1e094 4f 57 49 44 20 66 6c 61 67 20 6f 66 20 50 35 20  OWID flag of P5 
1e095 69 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 6e 20  is set,.** then 
1e096 72 6f 77 69 64 20 69 73 20 73 74 6f 72 65 64 20  rowid is stored 
1e097 66 6f 72 20 73 75 62 73 65 71 75 65 6e 74 20 72  for subsequent r
1e098 65 74 75 72 6e 20 62 79 20 74 68 65 0a 2a 2a 20  eturn by the.** 
1e099 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73  sqlite3_last_ins
1e09a 65 72 74 5f 72 6f 77 69 64 28 29 20 66 75 6e 63  ert_rowid() func
1e09b 74 69 6f 6e 20 28 6f 74 68 65 72 77 69 73 65 20  tion (otherwise 
1e09c 69 74 20 69 73 20 75 6e 6d 6f 64 69 66 69 65 64  it is unmodified
1e09d 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  )..**.** If the 
1e09e 4f 50 46 4c 41 47 5f 55 53 45 53 45 45 4b 52 45  OPFLAG_USESEEKRE
1e09f 53 55 4c 54 20 66 6c 61 67 20 6f 66 20 50 35 20  SULT flag of P5 
1e0a0 69 73 20 73 65 74 20 61 6e 64 20 69 66 20 74 68  is set and if th
1e0a1 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 74  e result of.** t
1e0a2 68 65 20 6c 61 73 74 20 73 65 65 6b 20 6f 70 65  he last seek ope
1e0a3 72 61 74 69 6f 6e 20 28 4f 50 5f 4e 6f 74 45 78  ration (OP_NotEx
1e0a4 69 73 74 73 29 20 77 61 73 20 61 20 73 75 63 63  ists) was a succ
1e0a5 65 73 73 2c 20 74 68 65 6e 20 74 68 69 73 0a 2a  ess, then this.*
1e0a6 2a 20 6f 70 65 72 61 74 69 6f 6e 20 77 69 6c 6c  * operation will
1e0a7 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20   not attempt to 
1e0a8 66 69 6e 64 20 74 68 65 20 61 70 70 72 6f 70 72  find the appropr
1e0a9 69 61 74 65 20 72 6f 77 20 62 65 66 6f 72 65 20  iate row before 
1e0aa 64 6f 69 6e 67 0a 2a 2a 20 74 68 65 20 69 6e 73  doing.** the ins
1e0ab 65 72 74 20 62 75 74 20 77 69 6c 6c 20 69 6e 73  ert but will ins
1e0ac 74 65 61 64 20 6f 76 65 72 77 72 69 74 65 20 74  tead overwrite t
1e0ad 68 65 20 72 6f 77 20 74 68 61 74 20 74 68 65 20  he row that the 
1e0ae 63 75 72 73 6f 72 20 69 73 0a 2a 2a 20 63 75 72  cursor is.** cur
1e0af 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20  rently pointing 
1e0b0 74 6f 2e 20 20 50 72 65 73 75 6d 61 62 6c 79 2c  to.  Presumably,
1e0b1 20 74 68 65 20 70 72 69 6f 72 20 4f 50 5f 4e 6f   the prior OP_No
1e0b2 74 45 78 69 73 74 73 20 6f 70 63 6f 64 65 0a 2a  tExists opcode.*
1e0b3 2a 20 68 61 73 20 61 6c 72 65 61 64 79 20 70 6f  * has already po
1e0b4 73 69 74 69 6f 6e 65 64 20 74 68 65 20 63 75 72  sitioned the cur
1e0b5 73 6f 72 20 63 6f 72 72 65 63 74 6c 79 2e 20 20  sor correctly.  
1e0b6 54 68 69 73 20 69 73 20 61 6e 20 6f 70 74 69 6d  This is an optim
1e0b7 69 7a 61 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 20  ization.** that 
1e0b8 62 6f 6f 73 74 73 20 70 65 72 66 6f 72 6d 61 6e  boosts performan
1e0b9 63 65 20 62 79 20 61 76 6f 69 64 69 6e 67 20 72  ce by avoiding r
1e0ba 65 64 75 6e 64 61 6e 74 20 73 65 65 6b 73 2e 0a  edundant seeks..
1e0bb 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f 50 46  **.** If the OPF
1e0bc 4c 41 47 5f 49 53 55 50 44 41 54 45 20 66 6c 61  LAG_ISUPDATE fla
1e0bd 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74  g is set, then t
1e0be 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20 70 61  his opcode is pa
1e0bf 72 74 20 6f 66 20 61 6e 0a 2a 2a 20 55 50 44 41  rt of an.** UPDA
1e0c0 54 45 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 4f  TE operation.  O
1e0c1 74 68 65 72 77 69 73 65 20 28 69 66 20 74 68 65  therwise (if the
1e0c2 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72 29 20   flag is clear) 
1e0c3 74 68 65 6e 20 74 68 69 73 20 6f 70 63 6f 64 65  then this opcode
1e0c4 0a 2a 2a 20 69 73 20 70 61 72 74 20 6f 66 20 61  .** is part of a
1e0c5 6e 20 49 4e 53 45 52 54 20 6f 70 65 72 61 74 69  n INSERT operati
1e0c6 6f 6e 2e 20 20 54 68 65 20 64 69 66 66 65 72 65  on.  The differe
1e0c7 6e 63 65 20 69 73 20 6f 6e 6c 79 20 69 6d 70 6f  nce is only impo
1e0c8 72 74 61 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 20  rtant to.** the 
1e0c9 75 70 64 61 74 65 20 68 6f 6f 6b 2e 0a 2a 2a 0a  update hook..**.
1e0ca 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 50 34 20  ** Parameter P4 
1e0cb 6d 61 79 20 70 6f 69 6e 74 20 74 6f 20 61 20 73  may point to a s
1e0cc 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67  tring containing
1e0cd 20 74 68 65 20 74 61 62 6c 65 2d 6e 61 6d 65 2c   the table-name,
1e0ce 20 6f 72 0a 2a 2a 20 6d 61 79 20 62 65 20 4e 55   or.** may be NU
1e0cf 4c 4c 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 74  LL. If it is not
1e0d0 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20   NULL, then the 
1e0d1 75 70 64 61 74 65 2d 68 6f 6f 6b 20 0a 2a 2a 20  update-hook .** 
1e0d2 28 73 71 6c 69 74 65 33 2e 78 55 70 64 61 74 65  (sqlite3.xUpdate
1e0d3 43 61 6c 6c 62 61 63 6b 29 20 69 73 20 69 6e 76  Callback) is inv
1e0d4 6f 6b 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 61  oked following a
1e0d5 20 73 75 63 63 65 73 73 66 75 6c 20 69 6e 73 65   successful inse
1e0d6 72 74 2e 0a 2a 2a 0a 2a 2a 20 28 57 41 52 4e 49  rt..**.** (WARNI
1e0d7 4e 47 2f 54 4f 44 4f 3a 20 49 66 20 50 31 20 69  NG/TODO: If P1 i
1e0d8 73 20 61 20 70 73 65 75 64 6f 2d 63 75 72 73 6f  s a pseudo-curso
1e0d9 72 20 61 6e 64 20 50 32 20 69 73 20 64 79 6e 61  r and P2 is dyna
1e0da 6d 69 63 61 6c 6c 79 0a 2a 2a 20 61 6c 6c 6f 63  mically.** alloc
1e0db 61 74 65 64 2c 20 74 68 65 6e 20 6f 77 6e 65 72  ated, then owner
1e0dc 73 68 69 70 20 6f 66 20 50 32 20 69 73 20 74 72  ship of P2 is tr
1e0dd 61 6e 73 66 65 72 72 65 64 20 74 6f 20 74 68 65  ansferred to the
1e0de 20 70 73 65 75 64 6f 2d 63 75 72 73 6f 72 0a 2a   pseudo-cursor.*
1e0df 2a 20 61 6e 64 20 72 65 67 69 73 74 65 72 20 50  * and register P
1e0e0 32 20 62 65 63 6f 6d 65 73 20 65 70 68 65 6d 65  2 becomes epheme
1e0e1 72 61 6c 2e 20 20 49 66 20 74 68 65 20 63 75 72  ral.  If the cur
1e0e2 73 6f 72 20 69 73 20 63 68 61 6e 67 65 64 2c 20  sor is changed, 
1e0e3 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20  the.** value of 
1e0e4 72 65 67 69 73 74 65 72 20 50 32 20 77 69 6c 6c  register P2 will
1e0e5 20 74 68 65 6e 20 63 68 61 6e 67 65 2e 20 20 4d   then change.  M
1e0e6 61 6b 65 20 73 75 72 65 20 74 68 69 73 20 64 6f  ake sure this do
1e0e7 65 73 20 6e 6f 74 0a 2a 2a 20 63 61 75 73 65 20  es not.** cause 
1e0e8 61 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 29 0a 2a  any problems.).*
1e0e9 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75  *.** This instru
1e0ea 63 74 69 6f 6e 20 6f 6e 6c 79 20 77 6f 72 6b 73  ction only works
1e0eb 20 6f 6e 20 74 61 62 6c 65 73 2e 20 20 54 68 65   on tables.  The
1e0ec 20 65 71 75 69 76 61 6c 65 6e 74 20 69 6e 73 74   equivalent inst
1e0ed 72 75 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 69  ruction.** for i
1e0ee 6e 64 69 63 65 73 20 69 73 20 4f 50 5f 49 64 78  ndices is OP_Idx
1e0ef 49 6e 73 65 72 74 2e 0a 2a 2f 0a 63 61 73 65 20  Insert..*/.case 
1e0f0 4f 50 5f 49 6e 73 65 72 74 3a 20 7b 0a 23 69 66  OP_Insert: {.#if
1e0f1 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72   0  /* local var
1e0f2 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1e0f3 6f 20 75 2e 62 66 20 2a 2f 0a 20 20 4d 65 6d 20  o u.bf */.  Mem 
1e0f4 2a 70 44 61 74 61 3b 20 20 20 20 20 20 20 2f 2a  *pData;       /*
1e0f5 20 4d 45 4d 20 63 65 6c 6c 20 68 6f 6c 64 69 6e   MEM cell holdin
1e0f6 67 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 72  g data for the r
1e0f7 65 63 6f 72 64 20 74 6f 20 62 65 20 69 6e 73 65  ecord to be inse
1e0f8 72 74 65 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70  rted */.  Mem *p
1e0f9 4b 65 79 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d  Key;        /* M
1e0fa 45 4d 20 63 65 6c 6c 20 68 6f 6c 64 69 6e 67 20  EM cell holding 
1e0fb 6b 65 79 20 20 66 6f 72 20 74 68 65 20 72 65 63  key  for the rec
1e0fc 6f 72 64 20 2a 2f 0a 20 20 69 36 34 20 69 4b 65  ord */.  i64 iKe
1e0fd 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  y;         /* Th
1e0fe 65 20 69 6e 74 65 67 65 72 20 52 4f 57 49 44 20  e integer ROWID 
1e0ff 6f 72 20 6b 65 79 20 66 6f 72 20 74 68 65 20 72  or key for the r
1e100 65 63 6f 72 64 20 74 6f 20 62 65 20 69 6e 73 65  ecord to be inse
1e101 72 74 65 64 20 2a 2f 0a 20 20 56 64 62 65 43 75  rted */.  VdbeCu
1e102 72 73 6f 72 20 2a 70 43 3b 20 20 20 2f 2a 20 43  rsor *pC;   /* C
1e103 75 72 73 6f 72 20 74 6f 20 74 61 62 6c 65 20 69  ursor to table i
1e104 6e 74 6f 20 77 68 69 63 68 20 69 6e 73 65 72 74  nto which insert
1e105 20 69 73 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20   is written */. 
1e106 20 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20   int nZero;     
1e107 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1e108 7a 65 72 6f 2d 62 79 74 65 73 20 74 6f 20 61 70  zero-bytes to ap
1e109 70 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 73 65  pend */.  int se
1e10a 65 6b 52 65 73 75 6c 74 3b 20 20 20 2f 2a 20 52  ekResult;   /* R
1e10b 65 73 75 6c 74 20 6f 66 20 70 72 69 6f 72 20 73  esult of prior s
1e10c 65 65 6b 20 6f 72 20 30 20 69 66 20 6e 6f 20 55  eek or 0 if no U
1e10d 53 45 53 45 45 4b 52 45 53 55 4c 54 20 66 6c 61  SESEEKRESULT fla
1e10e 67 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  g */.  const cha
1e10f 72 20 2a 7a 44 62 3b 20 20 2f 2a 20 64 61 74 61  r *zDb;  /* data
1e110 62 61 73 65 20 6e 61 6d 65 20 2d 20 75 73 65 64  base name - used
1e111 20 62 79 20 74 68 65 20 75 70 64 61 74 65 20 68   by the update h
1e112 6f 6f 6b 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ook */.  const c
1e113 68 61 72 20 2a 7a 54 62 6c 3b 20 2f 2a 20 54 61  har *zTbl; /* Ta
1e114 62 6c 65 20 6e 61 6d 65 20 2d 20 75 73 65 64 20  ble name - used 
1e115 62 79 20 74 68 65 20 6f 70 64 61 74 65 20 68 6f  by the opdate ho
1e116 6f 6b 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 3b 20  ok */.  int op; 
1e117 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 63            /* Opc
1e118 6f 64 65 20 66 6f 72 20 75 70 64 61 74 65 20 68  ode for update h
1e119 6f 6f 6b 3a 20 53 51 4c 49 54 45 5f 55 50 44 41  ook: SQLITE_UPDA
1e11a 54 45 20 6f 72 20 53 51 4c 49 54 45 5f 49 4e 53  TE or SQLITE_INS
1e11b 45 52 54 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a  ERT */.#endif /*
1e11c 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1e11d 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 66   moved into u.bf
1e11e 20 2a 2f 0a 0a 20 20 75 2e 62 66 2e 70 44 61 74   */..  u.bf.pDat
1e11f 61 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70  a = &p->aMem[pOp
1e120 2d 3e 70 32 5d 3b 0a 20 20 75 2e 62 66 2e 70 4b  ->p2];.  u.bf.pK
1e121 65 79 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  ey = &p->aMem[pO
1e122 70 2d 3e 70 33 5d 3b 0a 20 20 61 73 73 65 72 74  p->p3];.  assert
1e123 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20  ( pOp->p1>=0 && 
1e124 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73  pOp->p1<p->nCurs
1e125 6f 72 20 29 3b 0a 20 20 75 2e 62 66 2e 70 43 20  or );.  u.bf.pC 
1e126 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e  = p->apCsr[pOp->
1e127 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75  p1];.  assert( u
1e128 2e 62 66 2e 70 43 21 3d 30 20 29 3b 0a 20 20 61  .bf.pC!=0 );.  a
1e129 73 73 65 72 74 28 20 75 2e 62 66 2e 70 43 2d 3e  ssert( u.bf.pC->
1e12a 70 43 75 72 73 6f 72 21 3d 30 20 29 3b 0a 20 20  pCursor!=0 );.  
1e12b 61 73 73 65 72 74 28 20 75 2e 62 66 2e 70 43 2d  assert( u.bf.pC-
1e12c 3e 70 73 65 75 64 6f 54 61 62 6c 65 52 65 67 3d  >pseudoTableReg=
1e12d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
1e12e 75 2e 62 66 2e 70 4b 65 79 2d 3e 66 6c 61 67 73  u.bf.pKey->flags
1e12f 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20   & MEM_Int );.  
1e130 61 73 73 65 72 74 28 20 75 2e 62 66 2e 70 43 2d  assert( u.bf.pC-
1e131 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 52 45  >isTable );.  RE
1e132 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70  GISTER_TRACE(pOp
1e133 2d 3e 70 32 2c 20 75 2e 62 66 2e 70 44 61 74 61  ->p2, u.bf.pData
1e134 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52  );.  REGISTER_TR
1e135 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 62  ACE(pOp->p3, u.b
1e136 66 2e 70 4b 65 79 29 3b 0a 0a 20 20 75 2e 62 66  f.pKey);..  u.bf
1e137 2e 69 4b 65 79 20 3d 20 75 2e 62 66 2e 70 4b 65  .iKey = u.bf.pKe
1e138 79 2d 3e 75 2e 69 3b 0a 20 20 69 66 28 20 70 4f  y->u.i;.  if( pO
1e139 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f 4e  p->p5 & OPFLAG_N
1e13a 43 48 41 4e 47 45 20 29 20 70 2d 3e 6e 43 68 61  CHANGE ) p->nCha
1e13b 6e 67 65 2b 2b 3b 0a 20 20 69 66 28 20 70 4f 70  nge++;.  if( pOp
1e13c 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f 4c 41  ->p5 & OPFLAG_LA
1e13d 53 54 52 4f 57 49 44 20 29 20 64 62 2d 3e 6c 61  STROWID ) db->la
1e13e 73 74 52 6f 77 69 64 20 3d 20 75 2e 62 66 2e 70  stRowid = u.bf.p
1e13f 4b 65 79 2d 3e 75 2e 69 3b 0a 20 20 69 66 28 20  Key->u.i;.  if( 
1e140 75 2e 62 66 2e 70 44 61 74 61 2d 3e 66 6c 61 67  u.bf.pData->flag
1e141 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a  s & MEM_Null ){.
1e142 20 20 20 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e      u.bf.pData->
1e143 7a 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 66 2e  z = 0;.    u.bf.
1e144 70 44 61 74 61 2d 3e 6e 20 3d 20 30 3b 0a 20 20  pData->n = 0;.  
1e145 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72  }else{.    asser
1e146 74 28 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e 66  t( u.bf.pData->f
1e147 6c 61 67 73 20 26 20 28 4d 45 4d 5f 42 6c 6f 62  lags & (MEM_Blob
1e148 7c 4d 45 4d 5f 53 74 72 29 20 29 3b 0a 20 20 7d  |MEM_Str) );.  }
1e149 0a 20 20 75 2e 62 66 2e 73 65 65 6b 52 65 73 75  .  u.bf.seekResu
1e14a 6c 74 20 3d 20 28 28 70 4f 70 2d 3e 70 35 20 26  lt = ((pOp->p5 &
1e14b 20 4f 50 46 4c 41 47 5f 55 53 45 53 45 45 4b 52   OPFLAG_USESEEKR
1e14c 45 53 55 4c 54 29 20 3f 20 75 2e 62 66 2e 70 43  ESULT) ? u.bf.pC
1e14d 2d 3e 73 65 65 6b 52 65 73 75 6c 74 20 3a 20 30  ->seekResult : 0
1e14e 29 3b 0a 20 20 69 66 28 20 75 2e 62 66 2e 70 44  );.  if( u.bf.pD
1e14f 61 74 61 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  ata->flags & MEM
1e150 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 75 2e 62  _Zero ){.    u.b
1e151 66 2e 6e 5a 65 72 6f 20 3d 20 75 2e 62 66 2e 70  f.nZero = u.bf.p
1e152 44 61 74 61 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20  Data->u.nZero;. 
1e153 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 62 66   }else{.    u.bf
1e154 2e 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 7d 0a  .nZero = 0;.  }.
1e155 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65    sqlite3BtreeSe
1e156 74 43 61 63 68 65 64 52 6f 77 69 64 28 75 2e 62  tCachedRowid(u.b
1e157 66 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30  f.pC->pCursor, 0
1e158 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
1e159 33 42 74 72 65 65 49 6e 73 65 72 74 28 75 2e 62  3BtreeInsert(u.b
1e15a 66 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30  f.pC->pCursor, 0
1e15b 2c 20 75 2e 62 66 2e 69 4b 65 79 2c 0a 20 20 20  , u.bf.iKey,.   
1e15c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e15d 20 20 20 20 20 20 20 75 2e 62 66 2e 70 44 61 74         u.bf.pDat
1e15e 61 2d 3e 7a 2c 20 75 2e 62 66 2e 70 44 61 74 61  a->z, u.bf.pData
1e15f 2d 3e 6e 2c 20 75 2e 62 66 2e 6e 5a 65 72 6f 2c  ->n, u.bf.nZero,
1e160 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1e161 20 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e             pOp->
1e162 70 35 20 26 20 4f 50 46 4c 41 47 5f 41 50 50 45  p5 & OPFLAG_APPE
1e163 4e 44 2c 20 75 2e 62 66 2e 73 65 65 6b 52 65 73  ND, u.bf.seekRes
1e164 75 6c 74 0a 20 20 29 3b 0a 20 20 75 2e 62 66 2e  ult.  );.  u.bf.
1e165 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64  pC->rowidIsValid
1e166 20 3d 20 30 3b 0a 20 20 75 2e 62 66 2e 70 43 2d   = 0;.  u.bf.pC-
1e167 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20  >deferredMoveto 
1e168 3d 20 30 3b 0a 20 20 75 2e 62 66 2e 70 43 2d 3e  = 0;.  u.bf.pC->
1e169 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41  cacheStatus = CA
1e16a 43 48 45 5f 53 54 41 4c 45 3b 0a 0a 20 20 2f 2a  CHE_STALE;..  /*
1e16b 20 49 6e 76 6f 6b 65 20 74 68 65 20 75 70 64 61   Invoke the upda
1e16c 74 65 2d 68 6f 6f 6b 20 69 66 20 72 65 71 75 69  te-hook if requi
1e16d 72 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20 72 63  red. */.  if( rc
1e16e 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 64  ==SQLITE_OK && d
1e16f 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62 61  b->xUpdateCallba
1e170 63 6b 20 26 26 20 70 4f 70 2d 3e 70 34 2e 7a 20  ck && pOp->p4.z 
1e171 29 7b 0a 20 20 20 20 75 2e 62 66 2e 7a 44 62 20  ){.    u.bf.zDb 
1e172 3d 20 64 62 2d 3e 61 44 62 5b 75 2e 62 66 2e 70  = db->aDb[u.bf.p
1e173 43 2d 3e 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20  C->iDb].zName;. 
1e174 20 20 20 75 2e 62 66 2e 7a 54 62 6c 20 3d 20 70     u.bf.zTbl = p
1e175 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 75 2e  Op->p4.z;.    u.
1e176 62 66 2e 6f 70 20 3d 20 28 28 70 4f 70 2d 3e 70  bf.op = ((pOp->p
1e177 35 20 26 20 4f 50 46 4c 41 47 5f 49 53 55 50 44  5 & OPFLAG_ISUPD
1e178 41 54 45 29 20 3f 20 53 51 4c 49 54 45 5f 55 50  ATE) ? SQLITE_UP
1e179 44 41 54 45 20 3a 20 53 51 4c 49 54 45 5f 49 4e  DATE : SQLITE_IN
1e17a 53 45 52 54 29 3b 0a 20 20 20 20 61 73 73 65 72  SERT);.    asser
1e17b 74 28 20 75 2e 62 66 2e 70 43 2d 3e 69 73 54 61  t( u.bf.pC->isTa
1e17c 62 6c 65 20 29 3b 0a 20 20 20 20 64 62 2d 3e 78  ble );.    db->x
1e17d 55 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 28 64  UpdateCallback(d
1e17e 62 2d 3e 70 55 70 64 61 74 65 41 72 67 2c 20 75  b->pUpdateArg, u
1e17f 2e 62 66 2e 6f 70 2c 20 75 2e 62 66 2e 7a 44 62  .bf.op, u.bf.zDb
1e180 2c 20 75 2e 62 66 2e 7a 54 62 6c 2c 20 75 2e 62  , u.bf.zTbl, u.b
1e181 66 2e 69 4b 65 79 29 3b 0a 20 20 20 20 61 73 73  f.iKey);.    ass
1e182 65 72 74 28 20 75 2e 62 66 2e 70 43 2d 3e 69 44  ert( u.bf.pC->iD
1e183 62 3e 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 62 72  b>=0 );.  }.  br
1e184 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1e185 65 3a 20 44 65 6c 65 74 65 20 50 31 20 50 32 20  e: Delete P1 P2 
1e186 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 44 65 6c  * P4 *.**.** Del
1e187 65 74 65 20 74 68 65 20 72 65 63 6f 72 64 20 61  ete the record a
1e188 74 20 77 68 69 63 68 20 74 68 65 20 50 31 20 63  t which the P1 c
1e189 75 72 73 6f 72 20 69 73 20 63 75 72 72 65 6e 74  ursor is current
1e18a 6c 79 20 70 6f 69 6e 74 69 6e 67 2e 0a 2a 2a 0a  ly pointing..**.
1e18b 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 20 77 69  ** The cursor wi
1e18c 6c 6c 20 62 65 20 6c 65 66 74 20 70 6f 69 6e 74  ll be left point
1e18d 69 6e 67 20 61 74 20 65 69 74 68 65 72 20 74 68  ing at either th
1e18e 65 20 6e 65 78 74 20 6f 72 20 74 68 65 20 70 72  e next or the pr
1e18f 65 76 69 6f 75 73 0a 2a 2a 20 72 65 63 6f 72 64  evious.** record
1e190 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 49   in the table. I
1e191 66 20 69 74 20 69 73 20 6c 65 66 74 20 70 6f 69  f it is left poi
1e192 6e 74 69 6e 67 20 61 74 20 74 68 65 20 6e 65 78  nting at the nex
1e193 74 20 72 65 63 6f 72 64 2c 20 74 68 65 6e 0a 2a  t record, then.*
1e194 2a 20 74 68 65 20 6e 65 78 74 20 4e 65 78 74 20  * the next Next 
1e195 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 69 6c 6c  instruction will
1e196 20 62 65 20 61 20 6e 6f 2d 6f 70 2e 20 20 48 65   be a no-op.  He
1e197 6e 63 65 20 69 74 20 69 73 20 4f 4b 20 74 6f 20  nce it is OK to 
1e198 64 65 6c 65 74 65 0a 2a 2a 20 61 20 72 65 63 6f  delete.** a reco
1e199 72 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61  rd from within a
1e19a 6e 20 4e 65 78 74 20 6c 6f 6f 70 2e 0a 2a 2a 0a  n Next loop..**.
1e19b 2a 2a 20 49 66 20 74 68 65 20 4f 50 46 4c 41 47  ** If the OPFLAG
1e19c 5f 4e 43 48 41 4e 47 45 20 66 6c 61 67 20 6f 66  _NCHANGE flag of
1e19d 20 50 32 20 69 73 20 73 65 74 2c 20 74 68 65 6e   P2 is set, then
1e19e 20 74 68 65 20 72 6f 77 20 63 68 61 6e 67 65 20   the row change 
1e19f 63 6f 75 6e 74 20 69 73 0a 2a 2a 20 69 6e 63 72  count is.** incr
1e1a0 65 6d 65 6e 74 65 64 20 28 6f 74 68 65 72 77 69  emented (otherwi
1e1a1 73 65 20 6e 6f 74 29 2e 0a 2a 2a 0a 2a 2a 20 50  se not)..**.** P
1e1a2 31 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 70 73  1 must not be ps
1e1a3 65 75 64 6f 2d 74 61 62 6c 65 2e 20 20 49 74 20  eudo-table.  It 
1e1a4 68 61 73 20 74 6f 20 62 65 20 61 20 72 65 61 6c  has to be a real
1e1a5 20 74 61 62 6c 65 20 77 69 74 68 0a 2a 2a 20 6d   table with.** m
1e1a6 75 6c 74 69 70 6c 65 20 72 6f 77 73 2e 0a 2a 2a  ultiple rows..**
1e1a7 0a 2a 2a 20 49 66 20 50 34 20 69 73 20 6e 6f 74  .** If P4 is not
1e1a8 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 69 74 20 69   NULL, then it i
1e1a9 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  s the name of th
1e1aa 65 20 74 61 62 6c 65 20 74 68 61 74 20 50 31 20  e table that P1 
1e1ab 69 73 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 74  is.** pointing t
1e1ac 6f 2e 20 20 54 68 65 20 75 70 64 61 74 65 20 68  o.  The update h
1e1ad 6f 6f 6b 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f  ook will be invo
1e1ae 6b 65 64 2c 20 69 66 20 69 74 20 65 78 69 73 74  ked, if it exist
1e1af 73 2e 0a 2a 2a 20 49 66 20 50 34 20 69 73 20 6e  s..** If P4 is n
1e1b0 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65  ot NULL then the
1e1b1 20 50 31 20 63 75 72 73 6f 72 20 6d 75 73 74 20   P1 cursor must 
1e1b2 68 61 76 65 20 62 65 65 6e 20 70 6f 73 69 74 69  have been positi
1e1b3 6f 6e 65 64 0a 2a 2a 20 75 73 69 6e 67 20 4f 50  oned.** using OP
1e1b4 5f 4e 6f 74 46 6f 75 6e 64 20 70 72 69 6f 72 20  _NotFound prior 
1e1b5 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73  to invoking this
1e1b6 20 6f 70 63 6f 64 65 2e 0a 2a 2f 0a 63 61 73 65   opcode..*/.case
1e1b7 20 4f 50 5f 44 65 6c 65 74 65 3a 20 7b 0a 23 69   OP_Delete: {.#i
1e1b8 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1e1b9 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1e1ba 74 6f 20 75 2e 62 67 20 2a 2f 0a 20 20 69 36 34  to u.bg */.  i64
1e1bb 20 69 4b 65 79 3b 0a 20 20 56 64 62 65 43 75 72   iKey;.  VdbeCur
1e1bc 73 6f 72 20 2a 70 43 3b 0a 23 65 6e 64 69 66 20  sor *pC;.#endif 
1e1bd 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1e1be 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1e1bf 62 67 20 2a 2f 0a 0a 20 20 75 2e 62 67 2e 69 4b  bg */..  u.bg.iK
1e1c0 65 79 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74  ey = 0;.  assert
1e1c1 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20  ( pOp->p1>=0 && 
1e1c2 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73  pOp->p1<p->nCurs
1e1c3 6f 72 20 29 3b 0a 20 20 75 2e 62 67 2e 70 43 20  or );.  u.bg.pC 
1e1c4 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e  = p->apCsr[pOp->
1e1c5 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75  p1];.  assert( u
1e1c6 2e 62 67 2e 70 43 21 3d 30 20 29 3b 0a 20 20 61  .bg.pC!=0 );.  a
1e1c7 73 73 65 72 74 28 20 75 2e 62 67 2e 70 43 2d 3e  ssert( u.bg.pC->
1e1c8 70 43 75 72 73 6f 72 21 3d 30 20 29 3b 20 20 2f  pCursor!=0 );  /
1e1c9 2a 20 4f 6e 6c 79 20 76 61 6c 69 64 20 66 6f 72  * Only valid for
1e1ca 20 72 65 61 6c 20 74 61 62 6c 65 73 2c 20 6e 6f   real tables, no
1e1cb 20 70 73 65 75 64 6f 74 61 62 6c 65 73 20 2a 2f   pseudotables */
1e1cc 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 75 70  ..  /* If the up
1e1cd 64 61 74 65 2d 68 6f 6f 6b 20 77 69 6c 6c 20 62  date-hook will b
1e1ce 65 20 69 6e 76 6f 6b 65 64 2c 20 73 65 74 20 75  e invoked, set u
1e1cf 2e 62 67 2e 69 4b 65 79 20 74 6f 20 74 68 65 20  .bg.iKey to the 
1e1d0 72 6f 77 69 64 20 6f 66 20 74 68 65 0a 20 20 2a  rowid of the.  *
1e1d1 2a 20 72 6f 77 20 62 65 69 6e 67 20 64 65 6c 65  * row being dele
1e1d2 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ted..  */.  if( 
1e1d3 64 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62  db->xUpdateCallb
1e1d4 61 63 6b 20 26 26 20 70 4f 70 2d 3e 70 34 2e 7a  ack && pOp->p4.z
1e1d5 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
1e1d6 75 2e 62 67 2e 70 43 2d 3e 69 73 54 61 62 6c 65  u.bg.pC->isTable
1e1d7 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
1e1d8 75 2e 62 67 2e 70 43 2d 3e 72 6f 77 69 64 49 73  u.bg.pC->rowidIs
1e1d9 56 61 6c 69 64 20 29 3b 20 20 2f 2a 20 6c 61 73  Valid );  /* las
1e1da 74 52 6f 77 69 64 20 73 65 74 20 62 79 20 70 72  tRowid set by pr
1e1db 65 76 69 6f 75 73 20 4f 50 5f 4e 6f 74 46 6f 75  evious OP_NotFou
1e1dc 6e 64 20 2a 2f 0a 20 20 20 20 75 2e 62 67 2e 69  nd */.    u.bg.i
1e1dd 4b 65 79 20 3d 20 75 2e 62 67 2e 70 43 2d 3e 6c  Key = u.bg.pC->l
1e1de 61 73 74 52 6f 77 69 64 3b 0a 20 20 7d 0a 0a 20  astRowid;.  }.. 
1e1df 20 2f 2a 20 54 68 65 20 4f 50 5f 44 65 6c 65 74   /* The OP_Delet
1e1e0 65 20 6f 70 63 6f 64 65 20 61 6c 77 61 79 73 20  e opcode always 
1e1e1 66 6f 6c 6c 6f 77 73 20 61 6e 20 4f 50 5f 4e 6f  follows an OP_No
1e1e2 74 45 78 69 73 74 73 20 6f 72 20 4f 50 5f 4c 61  tExists or OP_La
1e1e3 73 74 20 6f 72 0a 20 20 2a 2a 20 4f 50 5f 43 6f  st or.  ** OP_Co
1e1e4 6c 75 6d 6e 20 6f 6e 20 74 68 65 20 73 61 6d 65  lumn on the same
1e1e5 20 74 61 62 6c 65 20 77 69 74 68 6f 75 74 20 61   table without a
1e1e6 6e 79 20 69 6e 74 65 72 76 65 6e 69 6e 67 20 6f  ny intervening o
1e1e7 70 65 72 61 74 69 6f 6e 73 20 74 68 61 74 0a 20  perations that. 
1e1e8 20 2a 2a 20 6d 69 67 68 74 20 6d 6f 76 65 20 6f   ** might move o
1e1e9 72 20 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65  r invalidate the
1e1ea 20 63 75 72 73 6f 72 2e 20 20 48 65 6e 63 65 20   cursor.  Hence 
1e1eb 63 75 72 73 6f 72 20 75 2e 62 67 2e 70 43 20 69  cursor u.bg.pC i
1e1ec 73 20 61 6c 77 61 79 73 20 70 6f 69 6e 74 69 6e  s always pointin
1e1ed 67 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 72 6f  g.  ** to the ro
1e1ee 77 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20  w to be deleted 
1e1ef 61 6e 64 20 74 68 65 20 73 71 6c 69 74 65 33 56  and the sqlite3V
1e1f0 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28  dbeCursorMoveto(
1e1f1 29 20 6f 70 65 72 61 74 69 6f 6e 0a 20 20 2a 2a  ) operation.  **
1e1f2 20 62 65 6c 6f 77 20 69 73 20 61 6c 77 61 79 73   below is always
1e1f3 20 61 20 6e 6f 2d 6f 70 20 61 6e 64 20 63 61 6e   a no-op and can
1e1f4 6e 6f 74 20 66 61 69 6c 2e 20 20 57 65 20 77 69  not fail.  We wi
1e1f5 6c 6c 20 72 75 6e 20 69 74 20 61 6e 79 68 6f 77  ll run it anyhow
1e1f6 2c 20 74 68 6f 75 67 68 2c 0a 20 20 2a 2a 20 74  , though,.  ** t
1e1f7 6f 20 67 75 61 72 64 20 61 67 61 69 6e 73 74 20  o guard against 
1e1f8 66 75 74 75 72 65 20 63 68 61 6e 67 65 73 20 74  future changes t
1e1f9 6f 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72  o the code gener
1e1fa 61 74 6f 72 2e 0a 20 20 2a 2a 2f 0a 20 20 61 73  ator..  **/.  as
1e1fb 73 65 72 74 28 20 75 2e 62 67 2e 70 43 2d 3e 64  sert( u.bg.pC->d
1e1fc 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30  eferredMoveto==0
1e1fd 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   );.  rc = sqlit
1e1fe 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65  e3VdbeCursorMove
1e1ff 74 6f 28 75 2e 62 67 2e 70 43 29 3b 0a 20 20 69  to(u.bg.pC);.  i
1e200 66 28 20 4e 45 56 45 52 28 72 63 21 3d 53 51 4c  f( NEVER(rc!=SQL
1e201 49 54 45 5f 4f 4b 29 20 29 20 67 6f 74 6f 20 61  ITE_OK) ) goto a
1e202 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f  bort_due_to_erro
1e203 72 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72  r;..  sqlite3Btr
1e204 65 65 53 65 74 43 61 63 68 65 64 52 6f 77 69 64  eeSetCachedRowid
1e205 28 75 2e 62 67 2e 70 43 2d 3e 70 43 75 72 73 6f  (u.bg.pC->pCurso
1e206 72 2c 20 30 29 3b 0a 20 20 72 63 20 3d 20 73 71  r, 0);.  rc = sq
1e207 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 65  lite3BtreeDelete
1e208 28 75 2e 62 67 2e 70 43 2d 3e 70 43 75 72 73 6f  (u.bg.pC->pCurso
1e209 72 29 3b 0a 20 20 75 2e 62 67 2e 70 43 2d 3e 63  r);.  u.bg.pC->c
1e20a 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43  acheStatus = CAC
1e20b 48 45 5f 53 54 41 4c 45 3b 0a 0a 20 20 2f 2a 20  HE_STALE;..  /* 
1e20c 49 6e 76 6f 6b 65 20 74 68 65 20 75 70 64 61 74  Invoke the updat
1e20d 65 2d 68 6f 6f 6b 20 69 66 20 72 65 71 75 69 72  e-hook if requir
1e20e 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d  ed. */.  if( rc=
1e20f 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 64 62  =SQLITE_OK && db
1e210 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63  ->xUpdateCallbac
1e211 6b 20 26 26 20 70 4f 70 2d 3e 70 34 2e 7a 20 29  k && pOp->p4.z )
1e212 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
1e213 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b   *zDb = db->aDb[
1e214 75 2e 62 67 2e 70 43 2d 3e 69 44 62 5d 2e 7a 4e  u.bg.pC->iDb].zN
1e215 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63  ame;.    const c
1e216 68 61 72 20 2a 7a 54 62 6c 20 3d 20 70 4f 70 2d  har *zTbl = pOp-
1e217 3e 70 34 2e 7a 3b 0a 20 20 20 20 64 62 2d 3e 78  >p4.z;.    db->x
1e218 55 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 28 64  UpdateCallback(d
1e219 62 2d 3e 70 55 70 64 61 74 65 41 72 67 2c 20 53  b->pUpdateArg, S
1e21a 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20 7a 44  QLITE_DELETE, zD
1e21b 62 2c 20 7a 54 62 6c 2c 20 75 2e 62 67 2e 69 4b  b, zTbl, u.bg.iK
1e21c 65 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ey);.    assert(
1e21d 20 75 2e 62 67 2e 70 43 2d 3e 69 44 62 3e 3d 30   u.bg.pC->iDb>=0
1e21e 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f   );.  }.  if( pO
1e21f 70 2d 3e 70 32 20 26 20 4f 50 46 4c 41 47 5f 4e  p->p2 & OPFLAG_N
1e220 43 48 41 4e 47 45 20 29 20 70 2d 3e 6e 43 68 61  CHANGE ) p->nCha
1e221 6e 67 65 2b 2b 3b 0a 20 20 62 72 65 61 6b 3b 0a  nge++;.  break;.
1e222 7d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 73  }./* Opcode: Res
1e223 65 74 43 6f 75 6e 74 20 2a 20 2a 20 2a 20 2a 20  etCount * * * * 
1e224 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75  *.**.** The valu
1e225 65 20 6f 66 20 74 68 65 20 63 68 61 6e 67 65 20  e of the change 
1e226 63 6f 75 6e 74 65 72 20 69 73 20 63 6f 70 69 65  counter is copie
1e227 64 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  d to the databas
1e228 65 20 68 61 6e 64 6c 65 0a 2a 2a 20 63 68 61 6e  e handle.** chan
1e229 67 65 20 63 6f 75 6e 74 65 72 20 28 72 65 74 75  ge counter (retu
1e22a 72 6e 65 64 20 62 79 20 73 75 62 73 65 71 75 65  rned by subseque
1e22b 6e 74 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69  nt calls to sqli
1e22c 74 65 33 5f 63 68 61 6e 67 65 73 28 29 29 2e 0a  te3_changes())..
1e22d 2a 2a 20 54 68 65 6e 20 74 68 65 20 56 4d 73 20  ** Then the VMs 
1e22e 69 6e 74 65 72 6e 61 6c 20 63 68 61 6e 67 65 20  internal change 
1e22f 63 6f 75 6e 74 65 72 20 72 65 73 65 74 73 20 74  counter resets t
1e230 6f 20 30 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20  o 0..** This is 
1e231 75 73 65 64 20 62 79 20 74 72 69 67 67 65 72 20  used by trigger 
1e232 70 72 6f 67 72 61 6d 73 2e 0a 2a 2f 0a 63 61 73  programs..*/.cas
1e233 65 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74 3a  e OP_ResetCount:
1e234 20 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65   {.  sqlite3Vdbe
1e235 53 65 74 43 68 61 6e 67 65 73 28 64 62 2c 20 70  SetChanges(db, p
1e236 2d 3e 6e 43 68 61 6e 67 65 29 3b 0a 20 20 70 2d  ->nChange);.  p-
1e237 3e 6e 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20  >nChange = 0;.  
1e238 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1e239 6f 64 65 3a 20 52 6f 77 44 61 74 61 20 50 31 20  ode: RowData P1 
1e23a 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57  P2 * * *.**.** W
1e23b 72 69 74 65 20 69 6e 74 6f 20 72 65 67 69 73 74  rite into regist
1e23c 65 72 20 50 32 20 74 68 65 20 63 6f 6d 70 6c 65  er P2 the comple
1e23d 74 65 20 72 6f 77 20 64 61 74 61 20 66 6f 72 20  te row data for 
1e23e 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 54 68  cursor P1..** Th
1e23f 65 72 65 20 69 73 20 6e 6f 20 69 6e 74 65 72 70  ere is no interp
1e240 72 65 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  retation of the 
1e241 64 61 74 61 2e 20 20 0a 2a 2a 20 49 74 20 69 73  data.  .** It is
1e242 20 6a 75 73 74 20 63 6f 70 69 65 64 20 6f 6e 74   just copied ont
1e243 6f 20 74 68 65 20 50 32 20 72 65 67 69 73 74 65  o the P2 registe
1e244 72 20 65 78 61 63 74 6c 79 20 61 73 20 0a 2a 2a  r exactly as .**
1e245 20 69 74 20 69 73 20 66 6f 75 6e 64 20 69 6e 20   it is found in 
1e246 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
1e247 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
1e248 50 31 20 63 75 72 73 6f 72 20 6d 75 73 74 20 62  P1 cursor must b
1e249 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20  e pointing to a 
1e24a 76 61 6c 69 64 20 72 6f 77 20 28 6e 6f 74 20 61  valid row (not a
1e24b 20 4e 55 4c 4c 20 72 6f 77 29 0a 2a 2a 20 6f 66   NULL row).** of
1e24c 20 61 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 6e   a real table, n
1e24d 6f 74 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c  ot a pseudo-tabl
1e24e 65 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a  e..*/./* Opcode:
1e24f 20 52 6f 77 4b 65 79 20 50 31 20 50 32 20 2a 20   RowKey P1 P2 * 
1e250 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20  * *.**.** Write 
1e251 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32  into register P2
1e252 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 72 6f   the complete ro
1e253 77 20 6b 65 79 20 66 6f 72 20 63 75 72 73 6f 72  w key for cursor
1e254 20 50 31 2e 0a 2a 2a 20 54 68 65 72 65 20 69 73   P1..** There is
1e255 20 6e 6f 20 69 6e 74 65 72 70 72 65 74 61 74 69   no interpretati
1e256 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 2e 20  on of the data. 
1e257 20 0a 2a 2a 20 54 68 65 20 6b 65 79 20 69 73 20   .** The key is 
1e258 63 6f 70 69 65 64 20 6f 6e 74 6f 20 74 68 65 20  copied onto the 
1e259 50 33 20 72 65 67 69 73 74 65 72 20 65 78 61 63  P3 register exac
1e25a 74 6c 79 20 61 73 20 0a 2a 2a 20 69 74 20 69 73  tly as .** it is
1e25b 20 66 6f 75 6e 64 20 69 6e 20 74 68 65 20 64 61   found in the da
1e25c 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a  tabase file..**.
1e25d 2a 2a 20 49 66 20 74 68 65 20 50 31 20 63 75 72  ** If the P1 cur
1e25e 73 6f 72 20 6d 75 73 74 20 62 65 20 70 6f 69 6e  sor must be poin
1e25f 74 69 6e 67 20 74 6f 20 61 20 76 61 6c 69 64 20  ting to a valid 
1e260 72 6f 77 20 28 6e 6f 74 20 61 20 4e 55 4c 4c 20  row (not a NULL 
1e261 72 6f 77 29 0a 2a 2a 20 6f 66 20 61 20 72 65 61  row).** of a rea
1e262 6c 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70  l table, not a p
1e263 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a  seudo-table..*/.
1e264 63 61 73 65 20 4f 50 5f 52 6f 77 4b 65 79 3a 0a  case OP_RowKey:.
1e265 63 61 73 65 20 4f 50 5f 52 6f 77 44 61 74 61 3a  case OP_RowData:
1e266 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63   {.#if 0  /* loc
1e267 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1e268 65 64 20 69 6e 74 6f 20 75 2e 62 68 20 2a 2f 0a  ed into u.bh */.
1e269 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1e26a 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43  ;.  BtCursor *pC
1e26b 72 73 72 3b 0a 20 20 75 33 32 20 6e 3b 0a 20 20  rsr;.  u32 n;.  
1e26c 69 36 34 20 6e 36 34 3b 0a 23 65 6e 64 69 66 20  i64 n64;.#endif 
1e26d 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1e26e 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1e26f 62 68 20 2a 2f 0a 0a 20 20 70 4f 75 74 20 3d 20  bh */..  pOut = 
1e270 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32  &p->aMem[pOp->p2
1e271 5d 3b 0a 0a 20 20 2f 2a 20 4e 6f 74 65 20 74 68  ];..  /* Note th
1e272 61 74 20 52 6f 77 4b 65 79 20 61 6e 64 20 52 6f  at RowKey and Ro
1e273 77 44 61 74 61 20 61 72 65 20 72 65 61 6c 6c 79  wData are really
1e274 20 65 78 61 63 74 6c 79 20 74 68 65 20 73 61 6d   exactly the sam
1e275 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f  e instruction */
1e276 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1e277 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31  p1>=0 && pOp->p1
1e278 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20  <p->nCursor );. 
1e279 20 75 2e 62 68 2e 70 43 20 3d 20 70 2d 3e 61 70   u.bh.pC = p->ap
1e27a 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Csr[pOp->p1];.  
1e27b 61 73 73 65 72 74 28 20 75 2e 62 68 2e 70 43 2d  assert( u.bh.pC-
1e27c 3e 69 73 54 61 62 6c 65 20 7c 7c 20 70 4f 70 2d  >isTable || pOp-
1e27d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f 77 4b  >opcode==OP_RowK
1e27e 65 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ey );.  assert( 
1e27f 75 2e 62 68 2e 70 43 2d 3e 69 73 49 6e 64 65 78  u.bh.pC->isIndex
1e280 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d   || pOp->opcode=
1e281 3d 4f 50 5f 52 6f 77 44 61 74 61 20 29 3b 0a 20  =OP_RowData );. 
1e282 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e 70 43   assert( u.bh.pC
1e283 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
1e284 20 75 2e 62 68 2e 70 43 2d 3e 6e 75 6c 6c 52 6f   u.bh.pC->nullRo
1e285 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  w==0 );.  assert
1e286 28 20 75 2e 62 68 2e 70 43 2d 3e 70 73 65 75 64  ( u.bh.pC->pseud
1e287 6f 54 61 62 6c 65 52 65 67 3d 3d 30 20 29 3b 0a  oTableReg==0 );.
1e288 20 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e 70    assert( u.bh.p
1e289 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 3b  C->pCursor!=0 );
1e28a 0a 20 20 75 2e 62 68 2e 70 43 72 73 72 20 3d 20  .  u.bh.pCrsr = 
1e28b 75 2e 62 68 2e 70 43 2d 3e 70 43 75 72 73 6f 72  u.bh.pC->pCursor
1e28c 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
1e28d 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49 73  te3BtreeCursorIs
1e28e 56 61 6c 69 64 28 75 2e 62 68 2e 70 43 72 73 72  Valid(u.bh.pCrsr
1e28f 29 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 4f  ) );..  /* The O
1e290 50 5f 52 6f 77 4b 65 79 20 61 6e 64 20 4f 50 5f  P_RowKey and OP_
1e291 52 6f 77 44 61 74 61 20 6f 70 63 6f 64 65 73 20  RowData opcodes 
1e292 61 6c 77 61 79 73 20 66 6f 6c 6c 6f 77 20 4f 50  always follow OP
1e293 5f 4e 6f 74 45 78 69 73 74 73 20 6f 72 0a 20 20  _NotExists or.  
1e294 2a 2a 20 4f 50 5f 52 65 77 69 6e 64 2f 4f 70 5f  ** OP_Rewind/Op_
1e295 4e 65 78 74 20 77 69 74 68 20 6e 6f 20 69 6e 74  Next with no int
1e296 65 72 76 65 6e 69 6e 67 20 69 6e 73 74 72 75 63  ervening instruc
1e297 74 69 6f 6e 73 20 74 68 61 74 20 6d 69 67 68 74  tions that might
1e298 20 69 6e 76 61 6c 69 64 61 74 65 0a 20 20 2a 2a   invalidate.  **
1e299 20 74 68 65 20 63 75 72 73 6f 72 2e 20 20 48 65   the cursor.  He
1e29a 6e 63 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  nce the followin
1e29b 67 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  g sqlite3VdbeCur
1e29c 73 6f 72 4d 6f 76 65 74 6f 28 29 20 63 61 6c 6c  sorMoveto() call
1e29d 20 69 73 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20   is always.  ** 
1e29e 61 20 6e 6f 2d 6f 70 20 61 6e 64 20 63 61 6e 20  a no-op and can 
1e29f 6e 65 76 65 72 20 66 61 69 6c 2e 20 20 42 75 74  never fail.  But
1e2a0 20 77 65 20 6c 65 61 76 65 20 69 74 20 69 6e 20   we leave it in 
1e2a1 70 6c 61 63 65 20 61 73 20 61 20 73 61 66 65 74  place as a safet
1e2a2 79 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  y..  */.  assert
1e2a3 28 20 75 2e 62 68 2e 70 43 2d 3e 64 65 66 65 72  ( u.bh.pC->defer
1e2a4 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a  redMoveto==0 );.
1e2a5 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
1e2a6 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 75  beCursorMoveto(u
1e2a7 2e 62 68 2e 70 43 29 3b 0a 20 20 69 66 28 20 4e  .bh.pC);.  if( N
1e2a8 45 56 45 52 28 72 63 21 3d 53 51 4c 49 54 45 5f  EVER(rc!=SQLITE_
1e2a9 4f 4b 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74  OK) ) goto abort
1e2aa 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 0a  _due_to_error;..
1e2ab 20 20 69 66 28 20 75 2e 62 68 2e 70 43 2d 3e 69    if( u.bh.pC->i
1e2ac 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 61 73  sIndex ){.    as
1e2ad 73 65 72 74 28 20 21 75 2e 62 68 2e 70 43 2d 3e  sert( !u.bh.pC->
1e2ae 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 72  isTable );.    r
1e2af 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1e2b0 4b 65 79 53 69 7a 65 28 75 2e 62 68 2e 70 43 72  KeySize(u.bh.pCr
1e2b1 73 72 2c 20 26 75 2e 62 68 2e 6e 36 34 29 3b 0a  sr, &u.bh.n64);.
1e2b2 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d      assert( rc==
1e2b3 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 20  SQLITE_OK );    
1e2b4 2f 2a 20 54 72 75 65 20 62 65 63 61 75 73 65 20  /* True because 
1e2b5 6f 66 20 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28  of CursorMoveto(
1e2b6 29 20 63 61 6c 6c 20 61 62 6f 76 65 20 2a 2f 0a  ) call above */.
1e2b7 20 20 20 20 69 66 28 20 75 2e 62 68 2e 6e 36 34      if( u.bh.n64
1e2b8 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  >db->aLimit[SQLI
1e2b9 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d  TE_LIMIT_LENGTH]
1e2ba 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74   ){.      goto t
1e2bb 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a 20 20  oo_big;.    }.  
1e2bc 20 20 75 2e 62 68 2e 6e 20 3d 20 28 75 33 32 29    u.bh.n = (u32)
1e2bd 75 2e 62 68 2e 6e 36 34 3b 0a 20 20 7d 65 6c 73  u.bh.n64;.  }els
1e2be 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  e{.    rc = sqli
1e2bf 74 65 33 42 74 72 65 65 44 61 74 61 53 69 7a 65  te3BtreeDataSize
1e2c0 28 75 2e 62 68 2e 70 43 72 73 72 2c 20 26 75 2e  (u.bh.pCrsr, &u.
1e2c1 62 68 2e 6e 29 3b 0a 20 20 20 20 61 73 73 65 72  bh.n);.    asser
1e2c2 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
1e2c3 20 29 3b 20 20 20 20 2f 2a 20 44 61 74 61 53 69   );    /* DataSi
1e2c4 7a 65 28 29 20 63 61 6e 6e 6f 74 20 66 61 69 6c  ze() cannot fail
1e2c5 20 2a 2f 0a 20 20 20 20 69 66 28 20 75 2e 62 68   */.    if( u.bh
1e2c6 2e 6e 3e 28 75 33 32 29 64 62 2d 3e 61 4c 69 6d  .n>(u32)db->aLim
1e2c7 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
1e2c8 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 20  LENGTH] ){.     
1e2c9 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20   goto too_big;. 
1e2ca 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 73     }.  }.  if( s
1e2cb 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f  qlite3VdbeMemGro
1e2cc 77 28 70 4f 75 74 2c 20 75 2e 62 68 2e 6e 2c 20  w(pOut, u.bh.n, 
1e2cd 30 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6e  0) ){.    goto n
1e2ce 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 70 4f 75  o_mem;.  }.  pOu
1e2cf 74 2d 3e 6e 20 3d 20 75 2e 62 68 2e 6e 3b 0a 20  t->n = u.bh.n;. 
1e2d0 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28   MemSetTypeFlag(
1e2d1 70 4f 75 74 2c 20 4d 45 4d 5f 42 6c 6f 62 29 3b  pOut, MEM_Blob);
1e2d2 0a 20 20 69 66 28 20 75 2e 62 68 2e 70 43 2d 3e  .  if( u.bh.pC->
1e2d3 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 72  isIndex ){.    r
1e2d4 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1e2d5 4b 65 79 28 75 2e 62 68 2e 70 43 72 73 72 2c 20  Key(u.bh.pCrsr, 
1e2d6 30 2c 20 75 2e 62 68 2e 6e 2c 20 70 4f 75 74 2d  0, u.bh.n, pOut-
1e2d7 3e 7a 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  >z);.  }else{.  
1e2d8 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1e2d9 72 65 65 44 61 74 61 28 75 2e 62 68 2e 70 43 72  reeData(u.bh.pCr
1e2da 73 72 2c 20 30 2c 20 75 2e 62 68 2e 6e 2c 20 70  sr, 0, u.bh.n, p
1e2db 4f 75 74 2d 3e 7a 29 3b 0a 20 20 7d 0a 20 20 70  Out->z);.  }.  p
1e2dc 4f 75 74 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54  Out->enc = SQLIT
1e2dd 45 5f 55 54 46 38 3b 20 20 2f 2a 20 49 6e 20 63  E_UTF8;  /* In c
1e2de 61 73 65 20 74 68 65 20 62 6c 6f 62 20 69 73 20  ase the blob is 
1e2df 65 76 65 72 20 63 61 73 74 20 74 6f 20 74 65 78  ever cast to tex
1e2e0 74 20 2a 2f 0a 20 20 55 50 44 41 54 45 5f 4d 41  t */.  UPDATE_MA
1e2e1 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29  X_BLOBSIZE(pOut)
1e2e2 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
1e2e3 20 4f 70 63 6f 64 65 3a 20 52 6f 77 69 64 20 50   Opcode: Rowid P
1e2e4 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  1 P2 * * *.**.**
1e2e5 20 53 74 6f 72 65 20 69 6e 20 72 65 67 69 73 74   Store in regist
1e2e6 65 72 20 50 32 20 61 6e 20 69 6e 74 65 67 65 72  er P2 an integer
1e2e7 20 77 68 69 63 68 20 69 73 20 74 68 65 20 6b 65   which is the ke
1e2e8 79 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 65  y of the table e
1e2e9 6e 74 72 79 20 74 68 61 74 0a 2a 2a 20 50 31 20  ntry that.** P1 
1e2ea 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69  is currently poi
1e2eb 6e 74 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 50 31 20  nt to..**.** P1 
1e2ec 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 61 6e  can be either an
1e2ed 20 6f 72 64 69 6e 61 72 79 20 74 61 62 6c 65 20   ordinary table 
1e2ee 6f 72 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  or a virtual tab
1e2ef 6c 65 2e 20 20 54 68 65 72 65 20 75 73 65 64 20  le.  There used 
1e2f0 74 6f 0a 2a 2a 20 62 65 20 61 20 73 65 70 61 72  to.** be a separ
1e2f1 61 74 65 20 4f 50 5f 56 52 6f 77 69 64 20 6f 70  ate OP_VRowid op
1e2f2 63 6f 64 65 20 66 6f 72 20 75 73 65 20 77 69 74  code for use wit
1e2f3 68 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73  h virtual tables
1e2f4 2c 20 62 75 74 20 74 68 69 73 0a 2a 2a 20 6f 6e  , but this.** on
1e2f5 65 20 6f 70 63 6f 64 65 20 6e 6f 77 20 77 6f 72  e opcode now wor
1e2f6 6b 73 20 66 6f 72 20 62 6f 74 68 20 74 61 62 6c  ks for both tabl
1e2f7 65 20 74 79 70 65 73 2e 0a 2a 2f 0a 63 61 73 65  e types..*/.case
1e2f8 20 4f 50 5f 52 6f 77 69 64 3a 20 7b 20 20 20 20   OP_Rowid: {    
1e2f9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1e2fa 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20  out2-prerelease 
1e2fb 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63  */.#if 0  /* loc
1e2fc 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1e2fd 65 64 20 69 6e 74 6f 20 75 2e 62 69 20 2a 2f 0a  ed into u.bi */.
1e2fe 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1e2ff 3b 0a 20 20 69 36 34 20 76 3b 0a 20 20 73 71 6c  ;.  i64 v;.  sql
1e300 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
1e301 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  ;.  const sqlite
1e302 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c  3_module *pModul
1e303 65 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  e;.#endif /* loc
1e304 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1e305 65 64 20 69 6e 74 6f 20 75 2e 62 69 20 2a 2f 0a  ed into u.bi */.
1e306 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1e307 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31  p1>=0 && pOp->p1
1e308 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20  <p->nCursor );. 
1e309 20 75 2e 62 69 2e 70 43 20 3d 20 70 2d 3e 61 70   u.bi.pC = p->ap
1e30a 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Csr[pOp->p1];.  
1e30b 61 73 73 65 72 74 28 20 75 2e 62 69 2e 70 43 21  assert( u.bi.pC!
1e30c 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
1e30d 75 2e 62 69 2e 70 43 2d 3e 70 73 65 75 64 6f 54  u.bi.pC->pseudoT
1e30e 61 62 6c 65 52 65 67 3d 3d 30 20 29 3b 0a 20 20  ableReg==0 );.  
1e30f 69 66 28 20 75 2e 62 69 2e 70 43 2d 3e 6e 75 6c  if( u.bi.pC->nul
1e310 6c 52 6f 77 20 29 7b 0a 20 20 20 20 2f 2a 20 44  lRow ){.    /* D
1e311 6f 20 6e 6f 74 68 69 6e 67 20 73 6f 20 74 68 61  o nothing so tha
1e312 74 20 72 65 67 5b 50 32 5d 20 72 65 6d 61 69 6e  t reg[P2] remain
1e313 73 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 62 72  s NULL */.    br
1e314 65 61 6b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  eak;.  }else if(
1e315 20 75 2e 62 69 2e 70 43 2d 3e 64 65 66 65 72 72   u.bi.pC->deferr
1e316 65 64 4d 6f 76 65 74 6f 20 29 7b 0a 20 20 20 20  edMoveto ){.    
1e317 75 2e 62 69 2e 76 20 3d 20 75 2e 62 69 2e 70 43  u.bi.v = u.bi.pC
1e318 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 65 74 3b 0a  ->movetoTarget;.
1e319 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1e31a 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1e31b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 62  .  }else if( u.b
1e31c 69 2e 70 43 2d 3e 70 56 74 61 62 43 75 72 73 6f  i.pC->pVtabCurso
1e31d 72 20 29 7b 0a 20 20 20 20 75 2e 62 69 2e 70 56  r ){.    u.bi.pV
1e31e 74 61 62 20 3d 20 75 2e 62 69 2e 70 43 2d 3e 70  tab = u.bi.pC->p
1e31f 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61  VtabCursor->pVta
1e320 62 3b 0a 20 20 20 20 75 2e 62 69 2e 70 4d 6f 64  b;.    u.bi.pMod
1e321 75 6c 65 20 3d 20 75 2e 62 69 2e 70 56 74 61 62  ule = u.bi.pVtab
1e322 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 61  ->pModule;.    a
1e323 73 73 65 72 74 28 20 75 2e 62 69 2e 70 4d 6f 64  ssert( u.bi.pMod
1e324 75 6c 65 2d 3e 78 52 6f 77 69 64 20 29 3b 0a 20  ule->xRowid );. 
1e325 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61     if( sqlite3Sa
1e326 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f  fetyOff(db) ) go
1e327 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1e328 6d 69 73 75 73 65 3b 0a 20 20 20 20 72 63 20 3d  misuse;.    rc =
1e329 20 75 2e 62 69 2e 70 4d 6f 64 75 6c 65 2d 3e 78   u.bi.pModule->x
1e32a 52 6f 77 69 64 28 75 2e 62 69 2e 70 43 2d 3e 70  Rowid(u.bi.pC->p
1e32b 56 74 61 62 43 75 72 73 6f 72 2c 20 26 75 2e 62  VtabCursor, &u.b
1e32c 69 2e 76 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  i.v);.    sqlite
1e32d 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a  3DbFree(db, p->z
1e32e 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e  ErrMsg);.    p->
1e32f 7a 45 72 72 4d 73 67 20 3d 20 75 2e 62 69 2e 70  zErrMsg = u.bi.p
1e330 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20  Vtab->zErrMsg;. 
1e331 20 20 20 75 2e 62 69 2e 70 56 74 61 62 2d 3e 7a     u.bi.pVtab->z
1e332 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20  ErrMsg = 0;.    
1e333 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74  if( sqlite3Safet
1e334 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61  yOn(db) ) goto a
1e335 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75  bort_due_to_misu
1e336 73 65 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  se;.#endif /* SQ
1e337 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
1e338 4c 54 41 42 4c 45 20 2a 2f 0a 20 20 7d 65 6c 73  LTABLE */.  }els
1e339 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75  e{.    assert( u
1e33a 2e 62 69 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21  .bi.pC->pCursor!
1e33b 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73  =0 );.    rc = s
1e33c 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72  qlite3VdbeCursor
1e33d 4d 6f 76 65 74 6f 28 75 2e 62 69 2e 70 43 29 3b  Moveto(u.bi.pC);
1e33e 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f  .    if( rc ) go
1e33f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1e340 65 72 72 6f 72 3b 0a 20 20 20 20 69 66 28 20 75  error;.    if( u
1e341 2e 62 69 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56  .bi.pC->rowidIsV
1e342 61 6c 69 64 20 29 7b 0a 20 20 20 20 20 20 75 2e  alid ){.      u.
1e343 62 69 2e 76 20 3d 20 75 2e 62 69 2e 70 43 2d 3e  bi.v = u.bi.pC->
1e344 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 20 20 7d  lastRowid;.    }
1e345 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d  else{.      rc =
1e346 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79   sqlite3BtreeKey
1e347 53 69 7a 65 28 75 2e 62 69 2e 70 43 2d 3e 70 43  Size(u.bi.pC->pC
1e348 75 72 73 6f 72 2c 20 26 75 2e 62 69 2e 76 29 3b  ursor, &u.bi.v);
1e349 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72  .      assert( r
1e34a 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20  c==SQLITE_OK ); 
1e34b 20 2f 2a 20 41 6c 77 61 79 73 20 73 6f 20 62 65   /* Always so be
1e34c 63 61 75 73 65 20 6f 66 20 43 75 72 73 6f 72 4d  cause of CursorM
1e34d 6f 76 65 74 6f 28 29 20 61 62 6f 76 65 20 2a 2f  oveto() above */
1e34e 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 4f 75  .    }.  }.  pOu
1e34f 74 2d 3e 75 2e 69 20 3d 20 75 2e 62 69 2e 76 3b  t->u.i = u.bi.v;
1e350 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61  .  MemSetTypeFla
1e351 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29  g(pOut, MEM_Int)
1e352 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
1e353 20 4f 70 63 6f 64 65 3a 20 4e 75 6c 6c 52 6f 77   Opcode: NullRow
1e354 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a   P1 * * * *.**.*
1e355 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f  * Move the curso
1e356 72 20 50 31 20 74 6f 20 61 20 6e 75 6c 6c 20 72  r P1 to a null r
1e357 6f 77 2e 20 20 41 6e 79 20 4f 50 5f 43 6f 6c 75  ow.  Any OP_Colu
1e358 6d 6e 20 6f 70 65 72 61 74 69 6f 6e 73 0a 2a 2a  mn operations.**
1e359 20 74 68 61 74 20 6f 63 63 75 72 20 77 68 69 6c   that occur whil
1e35a 65 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  e the cursor is 
1e35b 6f 6e 20 74 68 65 20 6e 75 6c 6c 20 72 6f 77 20  on the null row 
1e35c 77 69 6c 6c 20 61 6c 77 61 79 73 0a 2a 2a 20 77  will always.** w
1e35d 72 69 74 65 20 61 20 4e 55 4c 4c 2e 0a 2a 2f 0a  rite a NULL..*/.
1e35e 63 61 73 65 20 4f 50 5f 4e 75 6c 6c 52 6f 77 3a  case OP_NullRow:
1e35f 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63   {.#if 0  /* loc
1e360 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1e361 65 64 20 69 6e 74 6f 20 75 2e 62 6a 20 2a 2f 0a  ed into u.bj */.
1e362 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1e363 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
1e364 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1e365 64 20 69 6e 74 6f 20 75 2e 62 6a 20 2a 2f 0a 0a  d into u.bj */..
1e366 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1e367 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c  1>=0 && pOp->p1<
1e368 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
1e369 75 2e 62 6a 2e 70 43 20 3d 20 70 2d 3e 61 70 43  u.bj.pC = p->apC
1e36a 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61  sr[pOp->p1];.  a
1e36b 73 73 65 72 74 28 20 75 2e 62 6a 2e 70 43 21 3d  ssert( u.bj.pC!=
1e36c 30 20 29 3b 0a 20 20 75 2e 62 6a 2e 70 43 2d 3e  0 );.  u.bj.pC->
1e36d 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 75  nullRow = 1;.  u
1e36e 2e 62 6a 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56  .bj.pC->rowidIsV
1e36f 61 6c 69 64 20 3d 20 30 3b 0a 20 20 69 66 28 20  alid = 0;.  if( 
1e370 75 2e 62 6a 2e 70 43 2d 3e 70 43 75 72 73 6f 72  u.bj.pC->pCursor
1e371 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42   ){.    sqlite3B
1e372 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28  treeClearCursor(
1e373 75 2e 62 6a 2e 70 43 2d 3e 70 43 75 72 73 6f 72  u.bj.pC->pCursor
1e374 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  );.  }.  break;.
1e375 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 61  }../* Opcode: La
1e376 73 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  st P1 P2 * * *.*
1e377 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 75 73  *.** The next us
1e378 65 20 6f 66 20 74 68 65 20 52 6f 77 69 64 20 6f  e of the Rowid o
1e379 72 20 43 6f 6c 75 6d 6e 20 6f 72 20 4e 65 78 74  r Column or Next
1e37a 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 66 6f 72   instruction for
1e37b 20 50 31 20 0a 2a 2a 20 77 69 6c 6c 20 72 65 66   P1 .** will ref
1e37c 65 72 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65  er to the last e
1e37d 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 61  ntry in the data
1e37e 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20 69 6e  base table or in
1e37f 64 65 78 2e 0a 2a 2a 20 49 66 20 74 68 65 20 74  dex..** If the t
1e380 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 73  able or index is
1e381 20 65 6d 70 74 79 20 61 6e 64 20 50 32 3e 30 2c   empty and P2>0,
1e382 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 65 64   then jump immed
1e383 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a  iately to P2..**
1e384 20 49 66 20 50 32 20 69 73 20 30 20 6f 72 20 69   If P2 is 0 or i
1e385 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69  f the table or i
1e386 6e 64 65 78 20 69 73 20 6e 6f 74 20 65 6d 70 74  ndex is not empt
1e387 79 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 0a  y, fall through.
1e388 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77  ** to the follow
1e389 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  ing instruction.
1e38a 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 61 73 74  .*/.case OP_Last
1e38b 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75  : {        /* ju
1e38c 6d 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20  mp */.#if 0  /* 
1e38d 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1e38e 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6b 20  moved into u.bk 
1e38f 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20  */.  VdbeCursor 
1e390 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20  *pC;.  BtCursor 
1e391 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 65  *pCrsr;.  int re
1e392 73 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  s;.#endif /* loc
1e393 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1e394 65 64 20 69 6e 74 6f 20 75 2e 62 6b 20 2a 2f 0a  ed into u.bk */.
1e395 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1e396 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31  p1>=0 && pOp->p1
1e397 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20  <p->nCursor );. 
1e398 20 75 2e 62 6b 2e 70 43 20 3d 20 70 2d 3e 61 70   u.bk.pC = p->ap
1e399 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Csr[pOp->p1];.  
1e39a 61 73 73 65 72 74 28 20 75 2e 62 6b 2e 70 43 21  assert( u.bk.pC!
1e39b 3d 30 20 29 3b 0a 20 20 75 2e 62 6b 2e 70 43 72  =0 );.  u.bk.pCr
1e39c 73 72 20 3d 20 75 2e 62 6b 2e 70 43 2d 3e 70 43  sr = u.bk.pC->pC
1e39d 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 75 2e 62  ursor;.  if( u.b
1e39e 6b 2e 70 43 72 73 72 3d 3d 30 20 29 7b 0a 20 20  k.pCrsr==0 ){.  
1e39f 20 20 75 2e 62 6b 2e 72 65 73 20 3d 20 31 3b 0a    u.bk.res = 1;.
1e3a0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
1e3a1 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 61  = sqlite3BtreeLa
1e3a2 73 74 28 75 2e 62 6b 2e 70 43 72 73 72 2c 20 26  st(u.bk.pCrsr, &
1e3a3 75 2e 62 6b 2e 72 65 73 29 3b 0a 20 20 7d 0a 20  u.bk.res);.  }. 
1e3a4 20 75 2e 62 6b 2e 70 43 2d 3e 6e 75 6c 6c 52 6f   u.bk.pC->nullRo
1e3a5 77 20 3d 20 28 75 38 29 75 2e 62 6b 2e 72 65 73  w = (u8)u.bk.res
1e3a6 3b 0a 20 20 75 2e 62 6b 2e 70 43 2d 3e 64 65 66  ;.  u.bk.pC->def
1e3a7 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b  erredMoveto = 0;
1e3a8 0a 20 20 75 2e 62 6b 2e 70 43 2d 3e 72 6f 77 69  .  u.bk.pC->rowi
1e3a9 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20  dIsValid = 0;.  
1e3aa 75 2e 62 6b 2e 70 43 2d 3e 63 61 63 68 65 53 74  u.bk.pC->cacheSt
1e3ab 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41  atus = CACHE_STA
1e3ac 4c 45 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70  LE;.  if( pOp->p
1e3ad 32 3e 30 20 26 26 20 75 2e 62 6b 2e 72 65 73 20  2>0 && u.bk.res 
1e3ae 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  ){.    pc = pOp-
1e3af 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62  >p2 - 1;.  }.  b
1e3b0 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a 20 4f 70 63  reak;.}.../* Opc
1e3b1 6f 64 65 3a 20 53 6f 72 74 20 50 31 20 50 32 20  ode: Sort P1 P2 
1e3b2 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  * * *.**.** This
1e3b3 20 6f 70 63 6f 64 65 20 64 6f 65 73 20 65 78 61   opcode does exa
1e3b4 63 74 6c 79 20 74 68 65 20 73 61 6d 65 20 74 68  ctly the same th
1e3b5 69 6e 67 20 61 73 20 4f 50 5f 52 65 77 69 6e 64  ing as OP_Rewind
1e3b6 20 65 78 63 65 70 74 20 74 68 61 74 0a 2a 2a 20   except that.** 
1e3b7 69 74 20 69 6e 63 72 65 6d 65 6e 74 73 20 61 6e  it increments an
1e3b8 20 75 6e 64 6f 63 75 6d 65 6e 74 65 64 20 67 6c   undocumented gl
1e3b9 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 75 73  obal variable us
1e3ba 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 2e 0a  ed for testing..
1e3bb 2a 2a 0a 2a 2a 20 53 6f 72 74 69 6e 67 20 69 73  **.** Sorting is
1e3bc 20 61 63 63 6f 6d 70 6c 69 73 68 65 64 20 62 79   accomplished by
1e3bd 20 77 72 69 74 69 6e 67 20 72 65 63 6f 72 64 73   writing records
1e3be 20 69 6e 74 6f 20 61 20 73 6f 72 74 69 6e 67 20   into a sorting 
1e3bf 69 6e 64 65 78 2c 0a 2a 2a 20 74 68 65 6e 20 72  index,.** then r
1e3c0 65 77 69 6e 64 69 6e 67 20 74 68 61 74 20 69 6e  ewinding that in
1e3c1 64 65 78 20 61 6e 64 20 70 6c 61 79 69 6e 67 20  dex and playing 
1e3c2 69 74 20 62 61 63 6b 20 66 72 6f 6d 20 62 65 67  it back from beg
1e3c3 69 6e 6e 69 6e 67 20 74 6f 0a 2a 2a 20 65 6e 64  inning to.** end
1e3c4 2e 20 20 57 65 20 75 73 65 20 74 68 65 20 4f 50  .  We use the OP
1e3c5 5f 53 6f 72 74 20 6f 70 63 6f 64 65 20 69 6e 73  _Sort opcode ins
1e3c6 74 65 61 64 20 6f 66 20 4f 50 5f 52 65 77 69 6e  tead of OP_Rewin
1e3c7 64 20 74 6f 20 64 6f 20 74 68 65 0a 2a 2a 20 72  d to do the.** r
1e3c8 65 77 69 6e 64 69 6e 67 20 73 6f 20 74 68 61 74  ewinding so that
1e3c9 20 74 68 65 20 67 6c 6f 62 61 6c 20 76 61 72 69   the global vari
1e3ca 61 62 6c 65 20 77 69 6c 6c 20 62 65 20 69 6e 63  able will be inc
1e3cb 72 65 6d 65 6e 74 65 64 20 61 6e 64 0a 2a 2a 20  remented and.** 
1e3cc 72 65 67 72 65 73 73 69 6f 6e 20 74 65 73 74 73  regression tests
1e3cd 20 63 61 6e 20 64 65 74 65 72 6d 69 6e 65 20 77   can determine w
1e3ce 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
1e3cf 65 20 6f 70 74 69 6d 69 7a 65 72 20 69 73 0a 2a  e optimizer is.*
1e3d0 2a 20 63 6f 72 72 65 63 74 6c 79 20 6f 70 74 69  * correctly opti
1e3d1 6d 69 7a 69 6e 67 20 6f 75 74 20 73 6f 72 74 73  mizing out sorts
1e3d2 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 6f 72  ..*/.case OP_Sor
1e3d3 74 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a  t: {        /* j
1e3d4 75 6d 70 20 2a 2f 0a 23 69 66 64 65 66 20 53 51  ump */.#ifdef SQ
1e3d5 4c 49 54 45 5f 54 45 53 54 0a 20 20 73 71 6c 69  LITE_TEST.  sqli
1e3d6 74 65 33 5f 73 6f 72 74 5f 63 6f 75 6e 74 2b 2b  te3_sort_count++
1e3d7 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 65 61 72  ;.  sqlite3_sear
1e3d8 63 68 5f 63 6f 75 6e 74 2d 2d 3b 0a 23 65 6e 64  ch_count--;.#end
1e3d9 69 66 0a 20 20 70 2d 3e 61 43 6f 75 6e 74 65 72  if.  p->aCounter
1e3da 5b 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54  [SQLITE_STMTSTAT
1e3db 55 53 5f 53 4f 52 54 2d 31 5d 2b 2b 3b 0a 20 20  US_SORT-1]++;.  
1e3dc 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20  /* Fall through 
1e3dd 69 6e 74 6f 20 4f 50 5f 52 65 77 69 6e 64 20 2a  into OP_Rewind *
1e3de 2f 0a 7d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52  /.}./* Opcode: R
1e3df 65 77 69 6e 64 20 50 31 20 50 32 20 2a 20 2a 20  ewind P1 P2 * * 
1e3e0 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74  *.**.** The next
1e3e1 20 75 73 65 20 6f 66 20 74 68 65 20 52 6f 77 69   use of the Rowi
1e3e2 64 20 6f 72 20 43 6f 6c 75 6d 6e 20 6f 72 20 4e  d or Column or N
1e3e3 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ext instruction 
1e3e4 66 6f 72 20 50 31 20 0a 2a 2a 20 77 69 6c 6c 20  for P1 .** will 
1e3e5 72 65 66 65 72 20 74 6f 20 74 68 65 20 66 69 72  refer to the fir
1e3e6 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  st entry in the 
1e3e7 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f  database table o
1e3e8 72 20 69 6e 64 65 78 2e 0a 2a 2a 20 49 66 20 74  r index..** If t
1e3e9 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65  he table or inde
1e3ea 78 20 69 73 20 65 6d 70 74 79 20 61 6e 64 20 50  x is empty and P
1e3eb 32 3e 30 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69  2>0, then jump i
1e3ec 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32  mmediately to P2
1e3ed 2e 0a 2a 2a 20 49 66 20 50 32 20 69 73 20 30 20  ..** If P2 is 0 
1e3ee 6f 72 20 69 66 20 74 68 65 20 74 61 62 6c 65 20  or if the table 
1e3ef 6f 72 20 69 6e 64 65 78 20 69 73 20 6e 6f 74 20  or index is not 
1e3f0 65 6d 70 74 79 2c 20 66 61 6c 6c 20 74 68 72 6f  empty, fall thro
1e3f1 75 67 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f  ugh.** to the fo
1e3f2 6c 6c 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74  llowing instruct
1e3f3 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ion..*/.case OP_
1e3f4 52 65 77 69 6e 64 3a 20 7b 20 20 20 20 20 20 20  Rewind: {       
1e3f5 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 66 20   /* jump */.#if 
1e3f6 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1e3f7 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1e3f8 20 75 2e 62 6c 20 2a 2f 0a 20 20 56 64 62 65 43   u.bl */.  VdbeC
1e3f9 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43  ursor *pC;.  BtC
1e3fa 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20  ursor *pCrsr;.  
1e3fb 69 6e 74 20 72 65 73 3b 0a 23 65 6e 64 69 66 20  int res;.#endif 
1e3fc 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1e3fd 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1e3fe 62 6c 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  bl */..  assert(
1e3ff 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70   pOp->p1>=0 && p
1e400 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f  Op->p1<p->nCurso
1e401 72 20 29 3b 0a 20 20 75 2e 62 6c 2e 70 43 20 3d  r );.  u.bl.pC =
1e402 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70   p->apCsr[pOp->p
1e403 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e  1];.  assert( u.
1e404 62 6c 2e 70 43 21 3d 30 20 29 3b 0a 20 20 69 66  bl.pC!=0 );.  if
1e405 28 20 28 75 2e 62 6c 2e 70 43 72 73 72 20 3d 20  ( (u.bl.pCrsr = 
1e406 75 2e 62 6c 2e 70 43 2d 3e 70 43 75 72 73 6f 72  u.bl.pC->pCursor
1e407 29 21 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d  )!=0 ){.    rc =
1e408 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 69 72   sqlite3BtreeFir
1e409 73 74 28 75 2e 62 6c 2e 70 43 72 73 72 2c 20 26  st(u.bl.pCrsr, &
1e40a 75 2e 62 6c 2e 72 65 73 29 3b 0a 20 20 20 20 75  u.bl.res);.    u
1e40b 2e 62 6c 2e 70 43 2d 3e 61 74 46 69 72 73 74 20  .bl.pC->atFirst 
1e40c 3d 20 75 2e 62 6c 2e 72 65 73 3d 3d 30 20 3f 31  = u.bl.res==0 ?1
1e40d 3a 30 3b 0a 20 20 20 20 75 2e 62 6c 2e 70 43 2d  :0;.    u.bl.pC-
1e40e 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20  >deferredMoveto 
1e40f 3d 20 30 3b 0a 20 20 20 20 75 2e 62 6c 2e 70 43  = 0;.    u.bl.pC
1e410 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20  ->cacheStatus = 
1e411 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20  CACHE_STALE;.   
1e412 20 75 2e 62 6c 2e 70 43 2d 3e 72 6f 77 69 64 49   u.bl.pC->rowidI
1e413 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 7d 65  sValid = 0;.  }e
1e414 6c 73 65 7b 0a 20 20 20 20 75 2e 62 6c 2e 72 65  lse{.    u.bl.re
1e415 73 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 75 2e 62  s = 1;.  }.  u.b
1e416 6c 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20  l.pC->nullRow = 
1e417 28 75 38 29 75 2e 62 6c 2e 72 65 73 3b 0a 20 20  (u8)u.bl.res;.  
1e418 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e  assert( pOp->p2>
1e419 30 20 26 26 20 70 4f 70 2d 3e 70 32 3c 70 2d 3e  0 && pOp->p2<p->
1e41a 6e 4f 70 20 29 3b 0a 20 20 69 66 28 20 75 2e 62  nOp );.  if( u.b
1e41b 6c 2e 72 65 73 20 29 7b 0a 20 20 20 20 70 63 20  l.res ){.    pc 
1e41c 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
1e41d 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
1e41e 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 78 74 20 50  * Opcode: Next P
1e41f 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  1 P2 * * *.**.**
1e420 20 41 64 76 61 6e 63 65 20 63 75 72 73 6f 72 20   Advance cursor 
1e421 50 31 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f  P1 so that it po
1e422 69 6e 74 73 20 74 6f 20 74 68 65 20 6e 65 78 74  ints to the next
1e423 20 6b 65 79 2f 64 61 74 61 20 70 61 69 72 20 69   key/data pair i
1e424 6e 20 69 74 73 0a 2a 2a 20 74 61 62 6c 65 20 6f  n its.** table o
1e425 72 20 69 6e 64 65 78 2e 20 20 49 66 20 74 68 65  r index.  If the
1e426 72 65 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20 6b  re are no more k
1e427 65 79 2f 76 61 6c 75 65 20 70 61 69 72 73 20 74  ey/value pairs t
1e428 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f 75 67 68  hen fall through
1e429 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f  .** to the follo
1e42a 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f 6e  wing instruction
1e42b 2e 20 20 42 75 74 20 69 66 20 74 68 65 20 63 75  .  But if the cu
1e42c 72 73 6f 72 20 61 64 76 61 6e 63 65 20 77 61 73  rsor advance was
1e42d 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20   successful,.** 
1e42e 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79  jump immediately
1e42f 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68   to P2..**.** Th
1e430 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75 73 74  e P1 cursor must
1e431 20 62 65 20 66 6f 72 20 61 20 72 65 61 6c 20 74   be for a real t
1e432 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65 75  able, not a pseu
1e433 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  do-table..**.** 
1e434 53 65 65 20 61 6c 73 6f 3a 20 50 72 65 76 0a 2a  See also: Prev.*
1e435 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 72 65  /./* Opcode: Pre
1e436 76 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  v P1 P2 * * *.**
1e437 0a 2a 2a 20 42 61 63 6b 20 75 70 20 63 75 72 73  .** Back up curs
1e438 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20 69 74  or P1 so that it
1e439 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 70   points to the p
1e43a 72 65 76 69 6f 75 73 20 6b 65 79 2f 64 61 74 61  revious key/data
1e43b 20 70 61 69 72 20 69 6e 20 69 74 73 0a 2a 2a 20   pair in its.** 
1e43c 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 20  table or index. 
1e43d 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   If there is no 
1e43e 70 72 65 76 69 6f 75 73 20 6b 65 79 2f 76 61 6c  previous key/val
1e43f 75 65 20 70 61 69 72 73 20 74 68 65 6e 20 66 61  ue pairs then fa
1e440 6c 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f  ll through.** to
1e441 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69   the following i
1e442 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 42 75 74  nstruction.  But
1e443 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 62   if the cursor b
1e444 61 63 6b 75 70 20 77 61 73 20 73 75 63 63 65 73  ackup was succes
1e445 73 66 75 6c 2c 0a 2a 2a 20 6a 75 6d 70 20 69 6d  sful,.** jump im
1e446 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e  mediately to P2.
1e447 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 31 20 63 75  .**.** The P1 cu
1e448 72 73 6f 72 20 6d 75 73 74 20 62 65 20 66 6f 72  rsor must be for
1e449 20 61 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 6e   a real table, n
1e44a 6f 74 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c  ot a pseudo-tabl
1e44b 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 72  e..*/.case OP_Pr
1e44c 65 76 3a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ev:          /* 
1e44d 6a 75 6d 70 20 2a 2f 0a 63 61 73 65 20 4f 50 5f  jump */.case OP_
1e44e 4e 65 78 74 3a 20 7b 20 20 20 20 20 20 20 20 2f  Next: {        /
1e44f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 66 20 30 20  * jump */.#if 0 
1e450 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1e451 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1e452 2e 62 6d 20 2a 2f 0a 20 20 56 64 62 65 43 75 72  .bm */.  VdbeCur
1e453 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72  sor *pC;.  BtCur
1e454 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e  sor *pCrsr;.  in
1e455 74 20 72 65 73 3b 0a 23 65 6e 64 69 66 20 2f 2a  t res;.#endif /*
1e456 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1e457 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6d   moved into u.bm
1e458 20 2a 2f 0a 0a 20 20 43 48 45 43 4b 5f 46 4f 52   */..  CHECK_FOR
1e459 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 61 73  _INTERRUPT;.  as
1e45a 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30  sert( pOp->p1>=0
1e45b 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e   && pOp->p1<p->n
1e45c 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 6d  Cursor );.  u.bm
1e45d 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70  .pC = p->apCsr[p
1e45e 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 28 20 75  Op->p1];.  if( u
1e45f 2e 62 6d 2e 70 43 3d 3d 30 20 29 7b 0a 20 20 20  .bm.pC==0 ){.   
1e460 20 62 72 65 61 6b 3b 20 20 2f 2a 20 53 65 65 20   break;  /* See 
1e461 74 69 63 6b 65 74 20 23 32 32 37 33 20 2a 2f 0a  ticket #2273 */.
1e462 20 20 7d 0a 20 20 75 2e 62 6d 2e 70 43 72 73 72    }.  u.bm.pCrsr
1e463 20 3d 20 75 2e 62 6d 2e 70 43 2d 3e 70 43 75 72   = u.bm.pC->pCur
1e464 73 6f 72 3b 0a 20 20 69 66 28 20 75 2e 62 6d 2e  sor;.  if( u.bm.
1e465 70 43 72 73 72 3d 3d 30 20 29 7b 0a 20 20 20 20  pCrsr==0 ){.    
1e466 75 2e 62 6d 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77  u.bm.pC->nullRow
1e467 20 3d 20 31 3b 0a 20 20 20 20 62 72 65 61 6b 3b   = 1;.    break;
1e468 0a 20 20 7d 0a 20 20 75 2e 62 6d 2e 72 65 73 20  .  }.  u.bm.res 
1e469 3d 20 31 3b 0a 20 20 61 73 73 65 72 74 28 20 75  = 1;.  assert( u
1e46a 2e 62 6d 2e 70 43 2d 3e 64 65 66 65 72 72 65 64  .bm.pC->deferred
1e46b 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 72  Moveto==0 );.  r
1e46c 63 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d  c = pOp->opcode=
1e46d 3d 4f 50 5f 4e 65 78 74 20 3f 20 73 71 6c 69 74  =OP_Next ? sqlit
1e46e 65 33 42 74 72 65 65 4e 65 78 74 28 75 2e 62 6d  e3BtreeNext(u.bm
1e46f 2e 70 43 72 73 72 2c 20 26 75 2e 62 6d 2e 72 65  .pCrsr, &u.bm.re
1e470 73 29 20 3a 0a 20 20 20 20 20 20 20 20 20 20 20  s) :.           
1e471 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e472 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 50     sqlite3BtreeP
1e473 72 65 76 69 6f 75 73 28 75 2e 62 6d 2e 70 43 72  revious(u.bm.pCr
1e474 73 72 2c 20 26 75 2e 62 6d 2e 72 65 73 29 3b 0a  sr, &u.bm.res);.
1e475 20 20 75 2e 62 6d 2e 70 43 2d 3e 6e 75 6c 6c 52    u.bm.pC->nullR
1e476 6f 77 20 3d 20 28 75 38 29 75 2e 62 6d 2e 72 65  ow = (u8)u.bm.re
1e477 73 3b 0a 20 20 75 2e 62 6d 2e 70 43 2d 3e 63 61  s;.  u.bm.pC->ca
1e478 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48  cheStatus = CACH
1e479 45 5f 53 54 41 4c 45 3b 0a 20 20 69 66 28 20 75  E_STALE;.  if( u
1e47a 2e 62 6d 2e 72 65 73 3d 3d 30 20 29 7b 0a 20 20  .bm.res==0 ){.  
1e47b 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d    pc = pOp->p2 -
1e47c 20 31 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d   1;.    if( pOp-
1e47d 3e 70 35 20 29 20 70 2d 3e 61 43 6f 75 6e 74 65  >p5 ) p->aCounte
1e47e 72 5b 70 4f 70 2d 3e 70 35 2d 31 5d 2b 2b 3b 0a  r[pOp->p5-1]++;.
1e47f 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
1e480 53 54 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  ST.    sqlite3_s
1e481 65 61 72 63 68 5f 63 6f 75 6e 74 2b 2b 3b 0a 23  earch_count++;.#
1e482 65 6e 64 69 66 0a 20 20 7d 0a 20 20 75 2e 62 6d  endif.  }.  u.bm
1e483 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69  .pC->rowidIsVali
1e484 64 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a  d = 0;.  break;.
1e485 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64  }../* Opcode: Id
1e486 78 49 6e 73 65 72 74 20 50 31 20 50 32 20 50 33  xInsert P1 P2 P3
1e487 20 2a 20 50 35 0a 2a 2a 0a 2a 2a 20 52 65 67 69   * P5.**.** Regi
1e488 73 74 65 72 20 50 32 20 68 6f 6c 64 73 20 61 20  ster P2 holds a 
1e489 53 51 4c 20 69 6e 64 65 78 20 6b 65 79 20 6d 61  SQL index key ma
1e48a 64 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20  de using the.** 
1e48b 4d 61 6b 65 52 65 63 6f 72 64 20 69 6e 73 74 72  MakeRecord instr
1e48c 75 63 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 6f  uctions.  This o
1e48d 70 63 6f 64 65 20 77 72 69 74 65 73 20 74 68 61  pcode writes tha
1e48e 74 20 6b 65 79 0a 2a 2a 20 69 6e 74 6f 20 74 68  t key.** into th
1e48f 65 20 69 6e 64 65 78 20 50 31 2e 20 20 44 61 74  e index P1.  Dat
1e490 61 20 66 6f 72 20 74 68 65 20 65 6e 74 72 79 20  a for the entry 
1e491 69 73 20 6e 69 6c 2e 0a 2a 2a 0a 2a 2a 20 50 33  is nil..**.** P3
1e492 20 69 73 20 61 20 66 6c 61 67 20 74 68 61 74 20   is a flag that 
1e493 70 72 6f 76 69 64 65 73 20 61 20 68 69 6e 74 20  provides a hint 
1e494 74 6f 20 74 68 65 20 62 2d 74 72 65 65 20 6c 61  to the b-tree la
1e495 79 65 72 20 74 68 61 74 20 74 68 69 73 0a 2a 2a  yer that this.**
1e496 20 69 6e 73 65 72 74 20 69 73 20 6c 69 6b 65 6c   insert is likel
1e497 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70 65 6e  y to be an appen
1e498 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  d..**.** This in
1e499 73 74 72 75 63 74 69 6f 6e 20 6f 6e 6c 79 20 77  struction only w
1e49a 6f 72 6b 73 20 66 6f 72 20 69 6e 64 69 63 65 73  orks for indices
1e49b 2e 20 20 54 68 65 20 65 71 75 69 76 61 6c 65 6e  .  The equivalen
1e49c 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a  t instruction.**
1e49d 20 66 6f 72 20 74 61 62 6c 65 73 20 69 73 20 4f   for tables is O
1e49e 50 5f 49 6e 73 65 72 74 2e 0a 2a 2f 0a 63 61 73  P_Insert..*/.cas
1e49f 65 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 3a 20  e OP_IdxInsert: 
1e4a0 7b 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 32 20  {        /* in2 
1e4a1 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63  */.#if 0  /* loc
1e4a2 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1e4a3 65 64 20 69 6e 74 6f 20 75 2e 62 6e 20 2a 2f 0a  ed into u.bn */.
1e4a4 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1e4a5 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43  ;.  BtCursor *pC
1e4a6 72 73 72 3b 0a 20 20 69 6e 74 20 6e 4b 65 79 3b  rsr;.  int nKey;
1e4a7 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
1e4a8 4b 65 79 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  Key;.#endif /* l
1e4a9 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1e4aa 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6e 20 2a  oved into u.bn *
1e4ab 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  /..  assert( pOp
1e4ac 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e  ->p1>=0 && pOp->
1e4ad 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b  p1<p->nCursor );
1e4ae 0a 20 20 75 2e 62 6e 2e 70 43 20 3d 20 70 2d 3e  .  u.bn.pC = p->
1e4af 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  apCsr[pOp->p1];.
1e4b0 20 20 61 73 73 65 72 74 28 20 75 2e 62 6e 2e 70    assert( u.bn.p
1e4b1 43 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  C!=0 );.  assert
1e4b2 28 20 70 49 6e 32 2d 3e 66 6c 61 67 73 20 26 20  ( pIn2->flags & 
1e4b3 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 75 2e  MEM_Blob );.  u.
1e4b4 62 6e 2e 70 43 72 73 72 20 3d 20 75 2e 62 6e 2e  bn.pCrsr = u.bn.
1e4b5 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69  pC->pCursor;.  i
1e4b6 66 28 20 41 4c 57 41 59 53 28 75 2e 62 6e 2e 70  f( ALWAYS(u.bn.p
1e4b7 43 72 73 72 21 3d 30 29 20 29 7b 0a 20 20 20 20  Crsr!=0) ){.    
1e4b8 61 73 73 65 72 74 28 20 75 2e 62 6e 2e 70 43 2d  assert( u.bn.pC-
1e4b9 3e 69 73 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20  >isTable==0 );. 
1e4ba 20 20 20 72 63 20 3d 20 45 78 70 61 6e 64 42 6c     rc = ExpandBl
1e4bb 6f 62 28 70 49 6e 32 29 3b 0a 20 20 20 20 69 66  ob(pIn2);.    if
1e4bc 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1e4bd 29 7b 0a 20 20 20 20 20 20 75 2e 62 6e 2e 6e 4b  ){.      u.bn.nK
1e4be 65 79 20 3d 20 70 49 6e 32 2d 3e 6e 3b 0a 20 20  ey = pIn2->n;.  
1e4bf 20 20 20 20 75 2e 62 6e 2e 7a 4b 65 79 20 3d 20      u.bn.zKey = 
1e4c0 70 49 6e 32 2d 3e 7a 3b 0a 20 20 20 20 20 20 72  pIn2->z;.      r
1e4c1 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1e4c2 49 6e 73 65 72 74 28 75 2e 62 6e 2e 70 43 72 73  Insert(u.bn.pCrs
1e4c3 72 2c 20 75 2e 62 6e 2e 7a 4b 65 79 2c 20 75 2e  r, u.bn.zKey, u.
1e4c4 62 6e 2e 6e 4b 65 79 2c 20 22 22 2c 20 30 2c 20  bn.nKey, "", 0, 
1e4c5 30 2c 20 70 4f 70 2d 3e 70 33 2c 0a 20 20 20 20  0, pOp->p3,.    
1e4c6 20 20 20 20 20 20 28 28 70 4f 70 2d 3e 70 35 20        ((pOp->p5 
1e4c7 26 20 4f 50 46 4c 41 47 5f 55 53 45 53 45 45 4b  & OPFLAG_USESEEK
1e4c8 52 45 53 55 4c 54 29 20 3f 20 75 2e 62 6e 2e 70  RESULT) ? u.bn.p
1e4c9 43 2d 3e 73 65 65 6b 52 65 73 75 6c 74 20 3a 20  C->seekResult : 
1e4ca 30 29 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20  0).      );.    
1e4cb 20 20 61 73 73 65 72 74 28 20 75 2e 62 6e 2e 70    assert( u.bn.p
1e4cc 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74  C->deferredMovet
1e4cd 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 75 2e  o==0 );.      u.
1e4ce 62 6e 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74  bn.pC->cacheStat
1e4cf 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45  us = CACHE_STALE
1e4d0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72  ;.    }.  }.  br
1e4d1 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1e4d2 65 3a 20 49 64 78 44 65 6c 65 74 65 20 50 31 20  e: IdxDelete P1 
1e4d3 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  P2 P3 * *.**.** 
1e4d4 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 50  The content of P
1e4d5 33 20 72 65 67 69 73 74 65 72 73 20 73 74 61 72  3 registers star
1e4d6 74 69 6e 67 20 61 74 20 72 65 67 69 73 74 65 72  ting at register
1e4d7 20 50 32 20 66 6f 72 6d 0a 2a 2a 20 61 6e 20 75   P2 form.** an u
1e4d8 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65  npacked index ke
1e4d9 79 2e 20 54 68 69 73 20 6f 70 63 6f 64 65 20 72  y. This opcode r
1e4da 65 6d 6f 76 65 73 20 74 68 61 74 20 65 6e 74 72  emoves that entr
1e4db 79 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 69  y from the .** i
1e4dc 6e 64 65 78 20 6f 70 65 6e 65 64 20 62 79 20 63  ndex opened by c
1e4dd 75 72 73 6f 72 20 50 31 2e 0a 2a 2f 0a 63 61 73  ursor P1..*/.cas
1e4de 65 20 4f 50 5f 49 64 78 44 65 6c 65 74 65 3a 20  e OP_IdxDelete: 
1e4df 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  {.#if 0  /* loca
1e4e0 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1e4e1 64 20 69 6e 74 6f 20 75 2e 62 6f 20 2a 2f 0a 20  d into u.bo */. 
1e4e2 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b   VdbeCursor *pC;
1e4e3 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72  .  BtCursor *pCr
1e4e4 73 72 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20  sr;.  int res;. 
1e4e5 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20   UnpackedRecord 
1e4e6 72 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  r;.#endif /* loc
1e4e7 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1e4e8 65 64 20 69 6e 74 6f 20 75 2e 62 6f 20 2a 2f 0a  ed into u.bo */.
1e4e9 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1e4ea 70 33 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74  p3>0 );.  assert
1e4eb 28 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70  ( pOp->p2>0 && p
1e4ec 4f 70 2d 3e 70 32 2b 70 4f 70 2d 3e 70 33 3c 3d  Op->p2+pOp->p3<=
1e4ed 70 2d 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 20 20 61  p->nMem+1 );.  a
1e4ee 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d  ssert( pOp->p1>=
1e4ef 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e  0 && pOp->p1<p->
1e4f0 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62  nCursor );.  u.b
1e4f1 6f 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b  o.pC = p->apCsr[
1e4f2 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65  pOp->p1];.  asse
1e4f3 72 74 28 20 75 2e 62 6f 2e 70 43 21 3d 30 20 29  rt( u.bo.pC!=0 )
1e4f4 3b 0a 20 20 75 2e 62 6f 2e 70 43 72 73 72 20 3d  ;.  u.bo.pCrsr =
1e4f5 20 75 2e 62 6f 2e 70 43 2d 3e 70 43 75 72 73 6f   u.bo.pC->pCurso
1e4f6 72 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28  r;.  if( ALWAYS(
1e4f7 75 2e 62 6f 2e 70 43 72 73 72 21 3d 30 29 20 29  u.bo.pCrsr!=0) )
1e4f8 7b 0a 20 20 20 20 75 2e 62 6f 2e 72 2e 70 4b 65  {.    u.bo.r.pKe
1e4f9 79 49 6e 66 6f 20 3d 20 75 2e 62 6f 2e 70 43 2d  yInfo = u.bo.pC-
1e4fa 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 75  >pKeyInfo;.    u
1e4fb 2e 62 6f 2e 72 2e 6e 46 69 65 6c 64 20 3d 20 28  .bo.r.nField = (
1e4fc 75 31 36 29 70 4f 70 2d 3e 70 33 3b 0a 20 20 20  u16)pOp->p3;.   
1e4fd 20 75 2e 62 6f 2e 72 2e 66 6c 61 67 73 20 3d 20   u.bo.r.flags = 
1e4fe 30 3b 0a 20 20 20 20 75 2e 62 6f 2e 72 2e 61 4d  0;.    u.bo.r.aM
1e4ff 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  em = &p->aMem[pO
1e500 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 72 63 20 3d  p->p2];.    rc =
1e501 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76   sqlite3BtreeMov
1e502 65 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e 62 6f  etoUnpacked(u.bo
1e503 2e 70 43 72 73 72 2c 20 26 75 2e 62 6f 2e 72 2c  .pCrsr, &u.bo.r,
1e504 20 30 2c 20 30 2c 20 26 75 2e 62 6f 2e 72 65 73   0, 0, &u.bo.res
1e505 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
1e506 51 4c 49 54 45 5f 4f 4b 20 26 26 20 75 2e 62 6f  QLITE_OK && u.bo
1e507 2e 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20  .res==0 ){.     
1e508 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1e509 65 65 44 65 6c 65 74 65 28 75 2e 62 6f 2e 70 43  eeDelete(u.bo.pC
1e50a 72 73 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rsr);.    }.    
1e50b 61 73 73 65 72 74 28 20 75 2e 62 6f 2e 70 43 2d  assert( u.bo.pC-
1e50c 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d  >deferredMoveto=
1e50d 3d 30 20 29 3b 0a 20 20 20 20 75 2e 62 6f 2e 70  =0 );.    u.bo.p
1e50e 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d  C->cacheStatus =
1e50f 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20   CACHE_STALE;.  
1e510 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1e511 20 4f 70 63 6f 64 65 3a 20 49 64 78 52 6f 77 69   Opcode: IdxRowi
1e512 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  d P1 P2 * * *.**
1e513 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f 20 72  .** Write into r
1e514 65 67 69 73 74 65 72 20 50 32 20 61 6e 20 69 6e  egister P2 an in
1e515 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74  teger which is t
1e516 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e  he last entry in
1e517 20 74 68 65 20 72 65 63 6f 72 64 20 61 74 0a 2a   the record at.*
1e518 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  * the end of the
1e519 20 69 6e 64 65 78 20 6b 65 79 20 70 6f 69 6e 74   index key point
1e51a 65 64 20 74 6f 20 62 79 20 63 75 72 73 6f 72 20  ed to by cursor 
1e51b 50 31 2e 20 20 54 68 69 73 20 69 6e 74 65 67 65  P1.  This intege
1e51c 72 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 74  r should be.** t
1e51d 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20  he rowid of the 
1e51e 74 61 62 6c 65 20 65 6e 74 72 79 20 74 6f 20 77  table entry to w
1e51f 68 69 63 68 20 74 68 69 73 20 69 6e 64 65 78 20  hich this index 
1e520 65 6e 74 72 79 20 70 6f 69 6e 74 73 2e 0a 2a 2a  entry points..**
1e521 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 52 6f  .** See also: Ro
1e522 77 69 64 2c 20 4d 61 6b 65 52 65 63 6f 72 64 2e  wid, MakeRecord.
1e523 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 52  .*/.case OP_IdxR
1e524 6f 77 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20  owid: {         
1e525 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65       /* out2-pre
1e526 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30  release */.#if 0
1e527 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
1e528 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1e529 75 2e 62 70 20 2a 2f 0a 20 20 42 74 43 75 72 73  u.bp */.  BtCurs
1e52a 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 56 64 62  or *pCrsr;.  Vdb
1e52b 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 69  eCursor *pC;.  i
1e52c 36 34 20 72 6f 77 69 64 3b 0a 23 65 6e 64 69 66  64 rowid;.#endif
1e52d 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1e52e 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1e52f 2e 62 70 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  .bp */..  assert
1e530 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20  ( pOp->p1>=0 && 
1e531 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73  pOp->p1<p->nCurs
1e532 6f 72 20 29 3b 0a 20 20 75 2e 62 70 2e 70 43 20  or );.  u.bp.pC 
1e533 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e  = p->apCsr[pOp->
1e534 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75  p1];.  assert( u
1e535 2e 62 70 2e 70 43 21 3d 30 20 29 3b 0a 20 20 75  .bp.pC!=0 );.  u
1e536 2e 62 70 2e 70 43 72 73 72 20 3d 20 75 2e 62 70  .bp.pCrsr = u.bp
1e537 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20  .pC->pCursor;.  
1e538 69 66 28 20 41 4c 57 41 59 53 28 75 2e 62 70 2e  if( ALWAYS(u.bp.
1e539 70 43 72 73 72 21 3d 30 29 20 29 7b 0a 20 20 20  pCrsr!=0) ){.   
1e53a 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
1e53b 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 75 2e  eCursorMoveto(u.
1e53c 62 70 2e 70 43 29 3b 0a 20 20 20 20 69 66 28 20  bp.pC);.    if( 
1e53d 4e 45 56 45 52 28 72 63 29 20 29 20 67 6f 74 6f  NEVER(rc) ) goto
1e53e 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72   abort_due_to_er
1e53f 72 6f 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ror;.    assert(
1e540 20 75 2e 62 70 2e 70 43 2d 3e 64 65 66 65 72 72   u.bp.pC->deferr
1e541 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20  edMoveto==0 );. 
1e542 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 70 2e     assert( u.bp.
1e543 70 43 2d 3e 69 73 54 61 62 6c 65 3d 3d 30 20 29  pC->isTable==0 )
1e544 3b 0a 20 20 20 20 69 66 28 20 21 75 2e 62 70 2e  ;.    if( !u.bp.
1e545 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20  pC->nullRow ){. 
1e546 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1e547 33 56 64 62 65 49 64 78 52 6f 77 69 64 28 64 62  3VdbeIdxRowid(db
1e548 2c 20 75 2e 62 70 2e 70 43 72 73 72 2c 20 26 75  , u.bp.pCrsr, &u
1e549 2e 62 70 2e 72 6f 77 69 64 29 3b 0a 20 20 20 20  .bp.rowid);.    
1e54a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1e54b 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67  _OK ){.        g
1e54c 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1e54d 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a  _error;.      }.
1e54e 20 20 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65        MemSetType
1e54f 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49  Flag(pOut, MEM_I
1e550 6e 74 29 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d  nt);.      pOut-
1e551 3e 75 2e 69 20 3d 20 75 2e 62 70 2e 72 6f 77 69  >u.i = u.bp.rowi
1e552 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62  d;.    }.  }.  b
1e553 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1e554 64 65 3a 20 49 64 78 47 45 20 50 31 20 50 32 20  de: IdxGE P1 P2 
1e555 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54  P3 P4 P5.**.** T
1e556 68 65 20 50 34 20 72 65 67 69 73 74 65 72 20 76  he P4 register v
1e557 61 6c 75 65 73 20 62 65 67 69 6e 6e 69 6e 67 20  alues beginning 
1e558 77 69 74 68 20 50 33 20 66 6f 72 6d 20 61 6e 20  with P3 form an 
1e559 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 0a  unpacked index .
1e55a 2a 2a 20 6b 65 79 20 74 68 61 74 20 6f 6d 69 74  ** key that omit
1e55b 73 20 74 68 65 20 52 4f 57 49 44 2e 20 20 43 6f  s the ROWID.  Co
1e55c 6d 70 61 72 65 20 74 68 69 73 20 6b 65 79 20 76  mpare this key v
1e55d 61 6c 75 65 20 61 67 61 69 6e 73 74 20 74 68 65  alue against the
1e55e 20 69 6e 64 65 78 20 0a 2a 2a 20 74 68 61 74 20   index .** that 
1e55f 50 31 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  P1 is currently 
1e560 70 6f 69 6e 74 69 6e 67 20 74 6f 2c 20 69 67 6e  pointing to, ign
1e561 6f 72 69 6e 67 20 74 68 65 20 52 4f 57 49 44 20  oring the ROWID 
1e562 6f 6e 20 74 68 65 20 50 31 20 69 6e 64 65 78 2e  on the P1 index.
1e563 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 31  .**.** If the P1
1e564 20 69 6e 64 65 78 20 65 6e 74 72 79 20 69 73 20   index entry is 
1e565 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20  greater than or 
1e566 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 79  equal to the key
1e567 20 76 61 6c 75 65 0a 2a 2a 20 74 68 65 6e 20 6a   value.** then j
1e568 75 6d 70 20 74 6f 20 50 32 2e 20 20 4f 74 68 65  ump to P2.  Othe
1e569 72 77 69 73 65 20 66 61 6c 6c 20 74 68 72 6f 75  rwise fall throu
1e56a 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69  gh to the next i
1e56b 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  nstruction..**.*
1e56c 2a 20 49 66 20 50 35 20 69 73 20 6e 6f 6e 2d 7a  * If P5 is non-z
1e56d 65 72 6f 20 74 68 65 6e 20 74 68 65 20 6b 65 79  ero then the key
1e56e 20 76 61 6c 75 65 20 69 73 20 69 6e 63 72 65 61   value is increa
1e56f 73 65 64 20 62 79 20 61 6e 20 65 70 73 69 6c 6f  sed by an epsilo
1e570 6e 20 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 74  n .** prior to t
1e571 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20  he comparison.  
1e572 54 68 69 73 20 6d 61 6b 65 20 74 68 65 20 6f 70  This make the op
1e573 63 6f 64 65 20 77 6f 72 6b 20 6c 69 6b 65 20 49  code work like I
1e574 64 78 47 54 20 65 78 63 65 70 74 0a 2a 2a 20 74  dxGT except.** t
1e575 68 61 74 20 69 66 20 74 68 65 20 6b 65 79 20 66  hat if the key f
1e576 72 6f 6d 20 72 65 67 69 73 74 65 72 20 50 33 20  rom register P3 
1e577 69 73 20 61 20 70 72 65 66 69 78 20 6f 66 20 74  is a prefix of t
1e578 68 65 20 6b 65 79 20 69 6e 20 74 68 65 20 63 75  he key in the cu
1e579 72 73 6f 72 2c 0a 2a 2a 20 74 68 65 20 72 65 73  rsor,.** the res
1e57a 75 6c 74 20 69 73 20 66 61 6c 73 65 20 77 68 65  ult is false whe
1e57b 72 65 61 73 20 69 74 20 77 6f 75 6c 64 20 62 65  reas it would be
1e57c 20 74 72 75 65 20 77 69 74 68 20 49 64 78 47 54   true with IdxGT
1e57d 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ..*/./* Opcode: 
1e57e 49 64 78 4c 54 20 50 31 20 50 32 20 50 33 20 2a  IdxLT P1 P2 P3 *
1e57f 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34   P5.**.** The P4
1e580 20 72 65 67 69 73 74 65 72 20 76 61 6c 75 65 73   register values
1e581 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20   beginning with 
1e582 50 33 20 66 6f 72 6d 20 61 6e 20 75 6e 70 61 63  P3 form an unpac
1e583 6b 65 64 20 69 6e 64 65 78 20 0a 2a 2a 20 6b 65  ked index .** ke
1e584 79 20 74 68 61 74 20 6f 6d 69 74 73 20 74 68 65  y that omits the
1e585 20 52 4f 57 49 44 2e 20 20 43 6f 6d 70 61 72 65   ROWID.  Compare
1e586 20 74 68 69 73 20 6b 65 79 20 76 61 6c 75 65 20   this key value 
1e587 61 67 61 69 6e 73 74 20 74 68 65 20 69 6e 64 65  against the inde
1e588 78 20 0a 2a 2a 20 74 68 61 74 20 50 31 20 69 73  x .** that P1 is
1e589 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74   currently point
1e58a 69 6e 67 20 74 6f 2c 20 69 67 6e 6f 72 69 6e 67  ing to, ignoring
1e58b 20 74 68 65 20 52 4f 57 49 44 20 6f 6e 20 74 68   the ROWID on th
1e58c 65 20 50 31 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a  e P1 index..**.*
1e58d 2a 20 49 66 20 74 68 65 20 50 31 20 69 6e 64 65  * If the P1 inde
1e58e 78 20 65 6e 74 72 79 20 69 73 20 6c 65 73 73 20  x entry is less 
1e58f 74 68 61 6e 20 74 68 65 20 6b 65 79 20 76 61 6c  than the key val
1e590 75 65 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20  ue then jump to 
1e591 50 32 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65  P2..** Otherwise
1e592 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f   fall through to
1e593 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75   the next instru
1e594 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ction..**.** If 
1e595 50 35 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 74  P5 is non-zero t
1e596 68 65 6e 20 74 68 65 20 6b 65 79 20 76 61 6c 75  hen the key valu
1e597 65 20 69 73 20 69 6e 63 72 65 61 73 65 64 20 62  e is increased b
1e598 79 20 61 6e 20 65 70 73 69 6c 6f 6e 20 70 72 69  y an epsilon pri
1e599 6f 72 20 0a 2a 2a 20 74 6f 20 74 68 65 20 63 6f  or .** to the co
1e59a 6d 70 61 72 69 73 6f 6e 2e 20 20 54 68 69 73 20  mparison.  This 
1e59b 6d 61 6b 65 73 20 74 68 65 20 6f 70 63 6f 64 65  makes the opcode
1e59c 20 77 6f 72 6b 20 6c 69 6b 65 20 49 64 78 4c 45   work like IdxLE
1e59d 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78  ..*/.case OP_Idx
1e59e 4c 54 3a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  LT:          /* 
1e59f 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73  jump, in3 */.cas
1e5a0 65 20 4f 50 5f 49 64 78 47 45 3a 20 7b 20 20 20  e OP_IdxGE: {   
1e5a1 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e       /* jump, in
1e5a2 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c  3 */.#if 0  /* l
1e5a3 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1e5a4 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 71 20 2a  oved into u.bq *
1e5a5 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  /.  VdbeCursor *
1e5a6 70 43 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20  pC;.  int res;. 
1e5a7 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20   UnpackedRecord 
1e5a8 72 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  r;.#endif /* loc
1e5a9 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1e5aa 65 64 20 69 6e 74 6f 20 75 2e 62 71 20 2a 2f 0a  ed into u.bq */.
1e5ab 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1e5ac 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31  p1>=0 && pOp->p1
1e5ad 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20  <p->nCursor );. 
1e5ae 20 75 2e 62 71 2e 70 43 20 3d 20 70 2d 3e 61 70   u.bq.pC = p->ap
1e5af 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Csr[pOp->p1];.  
1e5b0 61 73 73 65 72 74 28 20 75 2e 62 71 2e 70 43 21  assert( u.bq.pC!
1e5b1 3d 30 20 29 3b 0a 20 20 69 66 28 20 41 4c 57 41  =0 );.  if( ALWA
1e5b2 59 53 28 75 2e 62 71 2e 70 43 2d 3e 70 43 75 72  YS(u.bq.pC->pCur
1e5b3 73 6f 72 21 3d 30 29 20 29 7b 0a 20 20 20 20 61  sor!=0) ){.    a
1e5b4 73 73 65 72 74 28 20 75 2e 62 71 2e 70 43 2d 3e  ssert( u.bq.pC->
1e5b5 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d  deferredMoveto==
1e5b6 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
1e5b7 20 70 4f 70 2d 3e 70 35 3d 3d 30 20 7c 7c 20 70   pOp->p5==0 || p
1e5b8 4f 70 2d 3e 70 35 3d 3d 31 20 29 3b 0a 20 20 20  Op->p5==1 );.   
1e5b9 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34   assert( pOp->p4
1e5ba 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29  type==P4_INT32 )
1e5bb 3b 0a 20 20 20 20 75 2e 62 71 2e 72 2e 70 4b 65  ;.    u.bq.r.pKe
1e5bc 79 49 6e 66 6f 20 3d 20 75 2e 62 71 2e 70 43 2d  yInfo = u.bq.pC-
1e5bd 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 75  >pKeyInfo;.    u
1e5be 2e 62 71 2e 72 2e 6e 46 69 65 6c 64 20 3d 20 28  .bq.r.nField = (
1e5bf 75 31 36 29 70 4f 70 2d 3e 70 34 2e 69 3b 0a 20  u16)pOp->p4.i;. 
1e5c0 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 29     if( pOp->p5 )
1e5c1 7b 0a 20 20 20 20 20 20 75 2e 62 71 2e 72 2e 66  {.      u.bq.r.f
1e5c2 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f  lags = UNPACKED_
1e5c3 49 4e 43 52 4b 45 59 20 7c 20 55 4e 50 41 43 4b  INCRKEY | UNPACK
1e5c4 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 3b  ED_IGNORE_ROWID;
1e5c5 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1e5c6 20 20 75 2e 62 71 2e 72 2e 66 6c 61 67 73 20 3d    u.bq.r.flags =
1e5c7 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45   UNPACKED_IGNORE
1e5c8 5f 52 4f 57 49 44 3b 0a 20 20 20 20 7d 0a 20 20  _ROWID;.    }.  
1e5c9 20 20 75 2e 62 71 2e 72 2e 61 4d 65 6d 20 3d 20    u.bq.r.aMem = 
1e5ca 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33  &p->aMem[pOp->p3
1e5cb 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ];.    rc = sqli
1e5cc 74 65 33 56 64 62 65 49 64 78 4b 65 79 43 6f 6d  te3VdbeIdxKeyCom
1e5cd 70 61 72 65 28 75 2e 62 71 2e 70 43 2c 20 26 75  pare(u.bq.pC, &u
1e5ce 2e 62 71 2e 72 2c 20 26 75 2e 62 71 2e 72 65 73  .bq.r, &u.bq.res
1e5cf 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e  );.    if( pOp->
1e5d0 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 64 78 4c 54  opcode==OP_IdxLT
1e5d1 20 29 7b 0a 20 20 20 20 20 20 75 2e 62 71 2e 72   ){.      u.bq.r
1e5d2 65 73 20 3d 20 2d 75 2e 62 71 2e 72 65 73 3b 0a  es = -u.bq.res;.
1e5d3 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1e5d4 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70   assert( pOp->op
1e5d5 63 6f 64 65 3d 3d 4f 50 5f 49 64 78 47 45 20 29  code==OP_IdxGE )
1e5d6 3b 0a 20 20 20 20 20 20 75 2e 62 71 2e 72 65 73  ;.      u.bq.res
1e5d7 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ++;.    }.    if
1e5d8 28 20 75 2e 62 71 2e 72 65 73 3e 30 20 29 7b 0a  ( u.bq.res>0 ){.
1e5d9 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e        pc = pOp->
1e5da 70 32 20 2d 20 31 20 3b 0a 20 20 20 20 7d 0a 20  p2 - 1 ;.    }. 
1e5db 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
1e5dc 2a 20 4f 70 63 6f 64 65 3a 20 44 65 73 74 72 6f  * Opcode: Destro
1e5dd 79 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a  y P1 P2 P3 * *.*
1e5de 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65  *.** Delete an e
1e5df 6e 74 69 72 65 20 64 61 74 61 62 61 73 65 20 74  ntire database t
1e5e0 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 77 68  able or index wh
1e5e1 6f 73 65 20 72 6f 6f 74 20 70 61 67 65 20 69 6e  ose root page in
1e5e2 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a   the database.**
1e5e3 20 66 69 6c 65 20 69 73 20 67 69 76 65 6e 20 62   file is given b
1e5e4 79 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  y P1..**.** The 
1e5e5 74 61 62 6c 65 20 62 65 69 6e 67 20 64 65 73 74  table being dest
1e5e6 72 6f 79 65 64 20 69 73 20 69 6e 20 74 68 65 20  royed is in the 
1e5e7 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69  main database fi
1e5e8 6c 65 20 69 66 20 50 33 3d 3d 30 2e 20 20 49 66  le if P3==0.  If
1e5e9 0a 2a 2a 20 50 33 3d 3d 31 20 74 68 65 6e 20 74  .** P3==1 then t
1e5ea 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 63  he table to be c
1e5eb 6c 65 61 72 20 69 73 20 69 6e 20 74 68 65 20 61  lear is in the a
1e5ec 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73  uxiliary databas
1e5ed 65 20 66 69 6c 65 0a 2a 2a 20 74 68 61 74 20 69  e file.** that i
1e5ee 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20  s used to store 
1e5ef 74 61 62 6c 65 73 20 63 72 65 61 74 65 20 75 73  tables create us
1e5f0 69 6e 67 20 43 52 45 41 54 45 20 54 45 4d 50 4f  ing CREATE TEMPO
1e5f1 52 41 52 59 20 54 41 42 4c 45 2e 0a 2a 2a 0a 2a  RARY TABLE..**.*
1e5f2 2a 20 49 66 20 41 55 54 4f 56 41 43 55 55 4d 20  * If AUTOVACUUM 
1e5f3 69 73 20 65 6e 61 62 6c 65 64 20 74 68 65 6e 20  is enabled then 
1e5f4 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74  it is possible t
1e5f5 68 61 74 20 61 6e 6f 74 68 65 72 20 72 6f 6f 74  hat another root
1e5f6 20 70 61 67 65 0a 2a 2a 20 6d 69 67 68 74 20 62   page.** might b
1e5f7 65 20 6d 6f 76 65 64 20 69 6e 74 6f 20 74 68 65  e moved into the
1e5f8 20 6e 65 77 6c 79 20 64 65 6c 65 74 65 64 20 72   newly deleted r
1e5f9 6f 6f 74 20 70 61 67 65 20 69 6e 20 6f 72 64 65  oot page in orde
1e5fa 72 20 74 6f 20 6b 65 65 70 20 61 6c 6c 0a 2a 2a  r to keep all.**
1e5fb 20 72 6f 6f 74 20 70 61 67 65 73 20 63 6f 6e 74   root pages cont
1e5fc 69 67 75 6f 75 73 20 61 74 20 74 68 65 20 62 65  iguous at the be
1e5fd 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 64  ginning of the d
1e5fe 61 74 61 62 61 73 65 2e 20 20 54 68 65 20 66 6f  atabase.  The fo
1e5ff 72 6d 65 72 0a 2a 2a 20 76 61 6c 75 65 20 6f 66  rmer.** value of
1e600 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 74   the root page t
1e601 68 61 74 20 6d 6f 76 65 64 20 2d 20 69 74 73 20  hat moved - its 
1e602 76 61 6c 75 65 20 62 65 66 6f 72 65 20 74 68 65  value before the
1e603 20 6d 6f 76 65 20 6f 63 63 75 72 72 65 64 20 2d   move occurred -
1e604 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64 20 69 6e  .** is stored in
1e605 20 72 65 67 69 73 74 65 72 20 50 32 2e 20 20 49   register P2.  I
1e606 66 20 6e 6f 20 70 61 67 65 20 0a 2a 2a 20 6d 6f  f no page .** mo
1e607 76 65 6d 65 6e 74 20 77 61 73 20 72 65 71 75 69  vement was requi
1e608 72 65 64 20 28 62 65 63 61 75 73 65 20 74 68 65  red (because the
1e609 20 74 61 62 6c 65 20 62 65 69 6e 67 20 64 72 6f   table being dro
1e60a 70 70 65 64 20 77 61 73 20 61 6c 72 65 61 64 79  pped was already
1e60b 20 0a 2a 2a 20 74 68 65 20 6c 61 73 74 20 6f 6e   .** the last on
1e60c 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  e in the databas
1e60d 65 29 20 74 68 65 6e 20 61 20 7a 65 72 6f 20 69  e) then a zero i
1e60e 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69  s stored in regi
1e60f 73 74 65 72 20 50 32 2e 0a 2a 2a 20 49 66 20 41  ster P2..** If A
1e610 55 54 4f 56 41 43 55 55 4d 20 69 73 20 64 69 73  UTOVACUUM is dis
1e611 61 62 6c 65 64 20 74 68 65 6e 20 61 20 7a 65 72  abled then a zer
1e612 6f 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72  o is stored in r
1e613 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a  egister P2..**.*
1e614 2a 20 53 65 65 20 61 6c 73 6f 3a 20 43 6c 65 61  * See also: Clea
1e615 72 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 65 73  r.*/.case OP_Des
1e616 74 72 6f 79 3a 20 7b 20 20 20 20 20 2f 2a 20 6f  troy: {     /* o
1e617 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a  ut2-prerelease *
1e618 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  /.#if 0  /* loca
1e619 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1e61a 64 20 69 6e 74 6f 20 75 2e 62 72 20 2a 2f 0a 20  d into u.br */. 
1e61b 20 69 6e 74 20 69 4d 6f 76 65 64 3b 0a 20 20 69   int iMoved;.  i
1e61c 6e 74 20 69 43 6e 74 3b 0a 20 20 56 64 62 65 20  nt iCnt;.  Vdbe 
1e61d 2a 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 44  *pVdbe;.  int iD
1e61e 62 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  b;.#endif /* loc
1e61f 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1e620 65 64 20 69 6e 74 6f 20 75 2e 62 72 20 2a 2f 0a  ed into u.br */.
1e621 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1e622 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1e623 0a 20 20 75 2e 62 72 2e 69 43 6e 74 20 3d 20 30  .  u.br.iCnt = 0
1e624 3b 0a 20 20 66 6f 72 28 75 2e 62 72 2e 70 56 64  ;.  for(u.br.pVd
1e625 62 65 3d 64 62 2d 3e 70 56 64 62 65 3b 20 75 2e  be=db->pVdbe; u.
1e626 62 72 2e 70 56 64 62 65 3b 20 75 2e 62 72 2e 70  br.pVdbe; u.br.p
1e627 56 64 62 65 20 3d 20 75 2e 62 72 2e 70 56 64 62  Vdbe = u.br.pVdb
1e628 65 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69  e->pNext){.    i
1e629 66 28 20 75 2e 62 72 2e 70 56 64 62 65 2d 3e 6d  f( u.br.pVdbe->m
1e62a 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43  agic==VDBE_MAGIC
1e62b 5f 52 55 4e 20 26 26 20 75 2e 62 72 2e 70 56 64  _RUN && u.br.pVd
1e62c 62 65 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64  be->inVtabMethod
1e62d 3c 32 20 26 26 20 75 2e 62 72 2e 70 56 64 62 65  <2 && u.br.pVdbe
1e62e 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20 20  ->pc>=0 ){.     
1e62f 20 75 2e 62 72 2e 69 43 6e 74 2b 2b 3b 0a 20 20   u.br.iCnt++;.  
1e630 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20    }.  }.#else.  
1e631 75 2e 62 72 2e 69 43 6e 74 20 3d 20 64 62 2d 3e  u.br.iCnt = db->
1e632 61 63 74 69 76 65 56 64 62 65 43 6e 74 3b 0a 23  activeVdbeCnt;.#
1e633 65 6e 64 69 66 0a 20 20 69 66 28 20 75 2e 62 72  endif.  if( u.br
1e634 2e 69 43 6e 74 3e 31 20 29 7b 0a 20 20 20 20 72  .iCnt>1 ){.    r
1e635 63 20 3d 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  c = SQLITE_LOCKE
1e636 44 3b 0a 20 20 20 20 70 2d 3e 65 72 72 6f 72 41  D;.    p->errorA
1e637 63 74 69 6f 6e 20 3d 20 4f 45 5f 41 62 6f 72 74  ction = OE_Abort
1e638 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75  ;.  }else{.    u
1e639 2e 62 72 2e 69 44 62 20 3d 20 70 4f 70 2d 3e 70  .br.iDb = pOp->p
1e63a 33 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75  3;.    assert( u
1e63b 2e 62 72 2e 69 43 6e 74 3d 3d 31 20 29 3b 0a 20  .br.iCnt==1 );. 
1e63c 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62     assert( (p->b
1e63d 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 75  treeMask & (1<<u
1e63e 2e 62 72 2e 69 44 62 29 29 21 3d 30 20 29 3b 0a  .br.iDb))!=0 );.
1e63f 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1e640 42 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28 64  BtreeDropTable(d
1e641 62 2d 3e 61 44 62 5b 75 2e 62 72 2e 69 44 62 5d  b->aDb[u.br.iDb]
1e642 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 31 2c 20 26  .pBt, pOp->p1, &
1e643 75 2e 62 72 2e 69 4d 6f 76 65 64 29 3b 0a 20 20  u.br.iMoved);.  
1e644 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
1e645 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b  (pOut, MEM_Int);
1e646 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d  .    pOut->u.i =
1e647 20 75 2e 62 72 2e 69 4d 6f 76 65 64 3b 0a 23 69   u.br.iMoved;.#i
1e648 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1e649 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20  T_AUTOVACUUM.   
1e64a 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1e64b 4f 4b 20 26 26 20 75 2e 62 72 2e 69 4d 6f 76 65  OK && u.br.iMove
1e64c 64 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  d!=0 ){.      sq
1e64d 6c 69 74 65 33 52 6f 6f 74 50 61 67 65 4d 6f 76  lite3RootPageMov
1e64e 65 64 28 26 64 62 2d 3e 61 44 62 5b 75 2e 62 72  ed(&db->aDb[u.br
1e64f 2e 69 44 62 5d 2c 20 75 2e 62 72 2e 69 4d 6f 76  .iDb], u.br.iMov
1e650 65 64 2c 20 70 4f 70 2d 3e 70 31 29 3b 0a 20 20  ed, pOp->p1);.  
1e651 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20    }.#endif.  }. 
1e652 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1e653 63 6f 64 65 3a 20 43 6c 65 61 72 20 50 31 20 50  code: Clear P1 P
1e654 32 20 50 33 0a 2a 2a 0a 2a 2a 20 44 65 6c 65 74  2 P3.**.** Delet
1e655 65 20 61 6c 6c 20 63 6f 6e 74 65 6e 74 73 20 6f  e all contents o
1e656 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74  f the database t
1e657 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 77 68  able or index wh
1e658 6f 73 65 20 72 6f 6f 74 20 70 61 67 65 0a 2a 2a  ose root page.**
1e659 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
1e65a 20 66 69 6c 65 20 69 73 20 67 69 76 65 6e 20 62   file is given b
1e65b 79 20 50 31 2e 20 20 42 75 74 2c 20 75 6e 6c 69  y P1.  But, unli
1e65c 6b 65 20 44 65 73 74 72 6f 79 2c 20 64 6f 20 6e  ke Destroy, do n
1e65d 6f 74 0a 2a 2a 20 72 65 6d 6f 76 65 20 74 68 65  ot.** remove the
1e65e 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20   table or index 
1e65f 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
1e660 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  e file..**.** Th
1e661 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 63 6c  e table being cl
1e662 65 61 72 20 69 73 20 69 6e 20 74 68 65 20 6d 61  ear is in the ma
1e663 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  in database file
1e664 20 69 66 20 50 32 3d 3d 30 2e 20 20 49 66 0a 2a   if P2==0.  If.*
1e665 2a 20 50 32 3d 3d 31 20 74 68 65 6e 20 74 68 65  * P2==1 then the
1e666 20 74 61 62 6c 65 20 74 6f 20 62 65 20 63 6c 65   table to be cle
1e667 61 72 20 69 73 20 69 6e 20 74 68 65 20 61 75 78  ar is in the aux
1e668 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20  iliary database 
1e669 66 69 6c 65 0a 2a 2a 20 74 68 61 74 20 69 73 20  file.** that is 
1e66a 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 61  used to store ta
1e66b 62 6c 65 73 20 63 72 65 61 74 65 20 75 73 69 6e  bles create usin
1e66c 67 20 43 52 45 41 54 45 20 54 45 4d 50 4f 52 41  g CREATE TEMPORA
1e66d 52 59 20 54 41 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20  RY TABLE..**.** 
1e66e 49 66 20 74 68 65 20 50 33 20 76 61 6c 75 65 20  If the P3 value 
1e66f 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  is non-zero, the
1e670 6e 20 74 68 65 20 74 61 62 6c 65 20 72 65 66 65  n the table refe
1e671 72 72 65 64 20 74 6f 20 6d 75 73 74 20 62 65 20  rred to must be 
1e672 61 6e 0a 2a 2a 20 69 6e 74 6b 65 79 20 74 61 62  an.** intkey tab
1e673 6c 65 20 28 61 6e 20 53 51 4c 20 74 61 62 6c 65  le (an SQL table
1e674 2c 20 6e 6f 74 20 61 6e 20 69 6e 64 65 78 29 2e  , not an index).
1e675 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68   In this case th
1e676 65 20 72 6f 77 20 63 68 61 6e 67 65 20 0a 2a 2a  e row change .**
1e677 20 63 6f 75 6e 74 20 69 73 20 69 6e 63 72 65 6d   count is increm
1e678 65 6e 74 65 64 20 62 79 20 74 68 65 20 6e 75 6d  ented by the num
1e679 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74  ber of rows in t
1e67a 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 63  he table being c
1e67b 6c 65 61 72 65 64 2e 20 0a 2a 2a 20 49 66 20 50  leared. .** If P
1e67c 33 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  3 is greater tha
1e67d 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65  n zero, then the
1e67e 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e   value stored in
1e67f 20 72 65 67 69 73 74 65 72 20 50 33 20 69 73 0a   register P3 is.
1e680 2a 2a 20 61 6c 73 6f 20 69 6e 63 72 65 6d 65 6e  ** also incremen
1e681 74 65 64 20 62 79 20 74 68 65 20 6e 75 6d 62 65  ted by the numbe
1e682 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65  r of rows in the
1e683 20 74 61 62 6c 65 20 62 65 69 6e 67 20 63 6c 65   table being cle
1e684 61 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  ared..**.** See 
1e685 61 6c 73 6f 3a 20 44 65 73 74 72 6f 79 0a 2a 2f  also: Destroy.*/
1e686 0a 63 61 73 65 20 4f 50 5f 43 6c 65 61 72 3a 20  .case OP_Clear: 
1e687 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  {.#if 0  /* loca
1e688 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1e689 64 20 69 6e 74 6f 20 75 2e 62 73 20 2a 2f 0a 20  d into u.bs */. 
1e68a 20 69 6e 74 20 6e 43 68 61 6e 67 65 3b 0a 23 65   int nChange;.#e
1e68b 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1e68c 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1e68d 74 6f 20 75 2e 62 73 20 2a 2f 0a 0a 20 20 75 2e  to u.bs */..  u.
1e68e 62 73 2e 6e 43 68 61 6e 67 65 20 3d 20 30 3b 0a  bs.nChange = 0;.
1e68f 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74    assert( (p->bt
1e690 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f  reeMask & (1<<pO
1e691 70 2d 3e 70 32 29 29 21 3d 30 20 29 3b 0a 20 20  p->p2))!=0 );.  
1e692 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1e693 65 43 6c 65 61 72 54 61 62 6c 65 28 0a 20 20 20  eClearTable(.   
1e694 20 20 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e     db->aDb[pOp->
1e695 70 32 5d 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 31  p2].pBt, pOp->p1
1e696 2c 20 28 70 4f 70 2d 3e 70 33 20 3f 20 26 75 2e  , (pOp->p3 ? &u.
1e697 62 73 2e 6e 43 68 61 6e 67 65 20 3a 20 30 29 0a  bs.nChange : 0).
1e698 20 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e    );.  if( pOp->
1e699 70 33 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 43 68  p3 ){.    p->nCh
1e69a 61 6e 67 65 20 2b 3d 20 75 2e 62 73 2e 6e 43 68  ange += u.bs.nCh
1e69b 61 6e 67 65 3b 0a 20 20 20 20 69 66 28 20 70 4f  ange;.    if( pO
1e69c 70 2d 3e 70 33 3e 30 20 29 7b 0a 20 20 20 20 20  p->p3>0 ){.     
1e69d 20 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33   p->aMem[pOp->p3
1e69e 5d 2e 75 2e 69 20 2b 3d 20 75 2e 62 73 2e 6e 43  ].u.i += u.bs.nC
1e69f 68 61 6e 67 65 3b 0a 20 20 20 20 7d 0a 20 20 7d  hange;.    }.  }
1e6a0 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1e6a1 4f 70 63 6f 64 65 3a 20 43 72 65 61 74 65 54 61  Opcode: CreateTa
1e6a2 62 6c 65 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  ble P1 P2 * * *.
1e6a3 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61  **.** Allocate a
1e6a4 20 6e 65 77 20 74 61 62 6c 65 20 69 6e 20 74 68   new table in th
1e6a5 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
1e6a6 66 69 6c 65 20 69 66 20 50 31 3d 3d 30 20 6f 72  file if P1==0 or
1e6a7 20 69 6e 20 74 68 65 0a 2a 2a 20 61 75 78 69 6c   in the.** auxil
1e6a8 69 61 72 79 20 64 61 74 61 62 61 73 65 20 66 69  iary database fi
1e6a9 6c 65 20 69 66 20 50 31 3d 3d 31 20 6f 72 20 69  le if P1==1 or i
1e6aa 6e 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 61  n an attached da
1e6ab 74 61 62 61 73 65 20 69 66 0a 2a 2a 20 50 31 3e  tabase if.** P1>
1e6ac 31 2e 20 20 57 72 69 74 65 20 74 68 65 20 72 6f  1.  Write the ro
1e6ad 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f  ot page number o
1e6ae 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20  f the new table 
1e6af 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72  into.** register
1e6b0 20 50 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69   P2.**.** The di
1e6b1 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e  fference between
1e6b2 20 61 20 74 61 62 6c 65 20 61 6e 64 20 61 6e 20   a table and an 
1e6b3 69 6e 64 65 78 20 69 73 20 74 68 69 73 3a 20 20  index is this:  
1e6b4 41 20 74 61 62 6c 65 20 6d 75 73 74 0a 2a 2a 20  A table must.** 
1e6b5 68 61 76 65 20 61 20 34 2d 62 79 74 65 20 69 6e  have a 4-byte in
1e6b6 74 65 67 65 72 20 6b 65 79 20 61 6e 64 20 63 61  teger key and ca
1e6b7 6e 20 68 61 76 65 20 61 72 62 69 74 72 61 72 79  n have arbitrary
1e6b8 20 64 61 74 61 2e 20 20 41 6e 20 69 6e 64 65 78   data.  An index
1e6b9 0a 2a 2a 20 68 61 73 20 61 6e 20 61 72 62 69 74  .** has an arbit
1e6ba 72 61 72 79 20 6b 65 79 20 62 75 74 20 6e 6f 20  rary key but no 
1e6bb 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  data..**.** See 
1e6bc 61 6c 73 6f 3a 20 43 72 65 61 74 65 49 6e 64 65  also: CreateInde
1e6bd 78 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  x.*/./* Opcode: 
1e6be 43 72 65 61 74 65 49 6e 64 65 78 20 50 31 20 50  CreateIndex P1 P
1e6bf 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 6c  2 * * *.**.** Al
1e6c0 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 69 6e 64  locate a new ind
1e6c1 65 78 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64  ex in the main d
1e6c2 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20  atabase file if 
1e6c3 50 31 3d 3d 30 20 6f 72 20 69 6e 20 74 68 65 0a  P1==0 or in the.
1e6c4 2a 2a 20 61 75 78 69 6c 69 61 72 79 20 64 61 74  ** auxiliary dat
1e6c5 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 31  abase file if P1
1e6c6 3d 3d 31 20 6f 72 20 69 6e 20 61 6e 20 61 74 74  ==1 or in an att
1e6c7 61 63 68 65 64 20 64 61 74 61 62 61 73 65 20 69  ached database i
1e6c8 66 0a 2a 2a 20 50 31 3e 31 2e 20 20 57 72 69 74  f.** P1>1.  Writ
1e6c9 65 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20  e the root page 
1e6ca 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65  number of the ne
1e6cb 77 20 74 61 62 6c 65 20 69 6e 74 6f 0a 2a 2a 20  w table into.** 
1e6cc 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a  register P2..**.
1e6cd 2a 2a 20 53 65 65 20 64 6f 63 75 6d 65 6e 74 61  ** See documenta
1e6ce 74 69 6f 6e 20 6f 6e 20 4f 50 5f 43 72 65 61 74  tion on OP_Creat
1e6cf 65 54 61 62 6c 65 20 66 6f 72 20 61 64 64 69 74  eTable for addit
1e6d0 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
1e6d1 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 72  n..*/.case OP_Cr
1e6d2 65 61 74 65 49 6e 64 65 78 3a 20 20 20 20 20 20  eateIndex:      
1e6d3 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72        /* out2-pr
1e6d4 65 72 65 6c 65 61 73 65 20 2a 2f 0a 63 61 73 65  erelease */.case
1e6d5 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 3a   OP_CreateTable:
1e6d6 20 7b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f   {          /* o
1e6d7 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a  ut2-prerelease *
1e6d8 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  /.#if 0  /* loca
1e6d9 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1e6da 64 20 69 6e 74 6f 20 75 2e 62 74 20 2a 2f 0a 20  d into u.bt */. 
1e6db 20 69 6e 74 20 70 67 6e 6f 3b 0a 20 20 69 6e 74   int pgno;.  int
1e6dc 20 66 6c 61 67 73 3b 0a 20 20 44 62 20 2a 70 44   flags;.  Db *pD
1e6dd 62 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  b;.#endif /* loc
1e6de 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1e6df 65 64 20 69 6e 74 6f 20 75 2e 62 74 20 2a 2f 0a  ed into u.bt */.
1e6e0 0a 20 20 75 2e 62 74 2e 70 67 6e 6f 20 3d 20 30  .  u.bt.pgno = 0
1e6e1 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
1e6e2 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70  >p1>=0 && pOp->p
1e6e3 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61  1<db->nDb );.  a
1e6e4 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65  ssert( (p->btree
1e6e5 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e  Mask & (1<<pOp->
1e6e6 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 75 2e 62  p1))!=0 );.  u.b
1e6e7 74 2e 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62  t.pDb = &db->aDb
1e6e8 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73  [pOp->p1];.  ass
1e6e9 65 72 74 28 20 75 2e 62 74 2e 70 44 62 2d 3e 70  ert( u.bt.pDb->p
1e6ea 42 74 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70  Bt!=0 );.  if( p
1e6eb 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 43  Op->opcode==OP_C
1e6ec 72 65 61 74 65 54 61 62 6c 65 20 29 7b 0a 20 20  reateTable ){.  
1e6ed 20 20 2f 2a 20 75 2e 62 74 2e 66 6c 61 67 73 20    /* u.bt.flags 
1e6ee 3d 20 42 54 52 45 45 5f 49 4e 54 4b 45 59 3b 20  = BTREE_INTKEY; 
1e6ef 2a 2f 0a 20 20 20 20 75 2e 62 74 2e 66 6c 61 67  */.    u.bt.flag
1e6f0 73 20 3d 20 42 54 52 45 45 5f 4c 45 41 46 44 41  s = BTREE_LEAFDA
1e6f1 54 41 7c 42 54 52 45 45 5f 49 4e 54 4b 45 59 3b  TA|BTREE_INTKEY;
1e6f2 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e  .  }else{.    u.
1e6f3 62 74 2e 66 6c 61 67 73 20 3d 20 42 54 52 45 45  bt.flags = BTREE
1e6f4 5f 5a 45 52 4f 44 41 54 41 3b 0a 20 20 7d 0a 20  _ZERODATA;.  }. 
1e6f5 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1e6f6 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 75 2e  eeCreateTable(u.
1e6f7 62 74 2e 70 44 62 2d 3e 70 42 74 2c 20 26 75 2e  bt.pDb->pBt, &u.
1e6f8 62 74 2e 70 67 6e 6f 2c 20 75 2e 62 74 2e 66 6c  bt.pgno, u.bt.fl
1e6f9 61 67 73 29 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e  ags);.  pOut->u.
1e6fa 69 20 3d 20 75 2e 62 74 2e 70 67 6e 6f 3b 0a 20  i = u.bt.pgno;. 
1e6fb 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28   MemSetTypeFlag(
1e6fc 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a  pOut, MEM_Int);.
1e6fd 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1e6fe 70 63 6f 64 65 3a 20 50 61 72 73 65 53 63 68 65  pcode: ParseSche
1e6ff 6d 61 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a  ma P1 P2 * P4 *.
1e700 2a 2a 0a 2a 2a 20 52 65 61 64 20 61 6e 64 20 70  **.** Read and p
1e701 61 72 73 65 20 61 6c 6c 20 65 6e 74 72 69 65 73  arse all entries
1e702 20 66 72 6f 6d 20 74 68 65 20 53 51 4c 49 54 45   from the SQLITE
1e703 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20 6f 66  _MASTER table of
1e704 20 64 61 74 61 62 61 73 65 20 50 31 0a 2a 2a 20   database P1.** 
1e705 74 68 61 74 20 6d 61 74 63 68 20 74 68 65 20 57  that match the W
1e706 48 45 52 45 20 63 6c 61 75 73 65 20 50 34 2e 20  HERE clause P4. 
1e707 20 50 32 20 69 73 20 74 68 65 20 22 66 6f 72 63   P2 is the "forc
1e708 65 22 20 66 6c 61 67 2e 20 20 20 41 6c 77 61 79  e" flag.   Alway
1e709 73 20 64 6f 0a 2a 2a 20 74 68 65 20 70 61 72 73  s do.** the pars
1e70a 69 6e 67 20 69 66 20 50 32 20 69 73 20 74 72 75  ing if P2 is tru
1e70b 65 2e 20 20 49 66 20 50 32 20 69 73 20 66 61 6c  e.  If P2 is fal
1e70c 73 65 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f  se, then this ro
1e70d 75 74 69 6e 65 20 69 73 20 61 0a 2a 2a 20 6e 6f  utine is a.** no
1e70e 2d 6f 70 20 69 66 20 74 68 65 20 73 63 68 65 6d  -op if the schem
1e70f 61 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74  a is not current
1e710 6c 79 20 6c 6f 61 64 65 64 2e 20 20 49 6e 20 6f  ly loaded.  In o
1e711 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 50  ther words, if P
1e712 32 0a 2a 2a 20 69 73 20 66 61 6c 73 65 2c 20 74  2.** is false, t
1e713 68 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52  he SQLITE_MASTER
1e714 20 74 61 62 6c 65 20 69 73 20 6f 6e 6c 79 20 70   table is only p
1e715 61 72 73 65 64 20 69 66 20 74 68 65 20 72 65 73  arsed if the res
1e716 74 20 6f 66 20 74 68 65 0a 2a 2a 20 73 63 68 65  t of the.** sche
1e717 6d 61 20 69 73 20 61 6c 72 65 61 64 79 20 6c 6f  ma is already lo
1e718 61 64 65 64 20 69 6e 74 6f 20 74 68 65 20 73 79  aded into the sy
1e719 6d 62 6f 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a  mbol table..**.*
1e71a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 6e  * This opcode in
1e71b 76 6f 6b 65 73 20 74 68 65 20 70 61 72 73 65 72  vokes the parser
1e71c 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77   to create a new
1e71d 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
1e71e 2c 0a 2a 2a 20 74 68 65 6e 20 72 75 6e 73 20 74  ,.** then runs t
1e71f 68 65 20 6e 65 77 20 76 69 72 74 75 61 6c 20 6d  he new virtual m
1e720 61 63 68 69 6e 65 2e 20 20 49 74 20 69 73 20 74  achine.  It is t
1e721 68 75 73 20 61 20 72 65 2d 65 6e 74 72 61 6e 74  hus a re-entrant
1e722 20 6f 70 63 6f 64 65 2e 0a 2a 2f 0a 63 61 73 65   opcode..*/.case
1e723 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61 3a   OP_ParseSchema:
1e724 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63   {.#if 0  /* loc
1e725 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1e726 65 64 20 69 6e 74 6f 20 75 2e 62 75 20 2a 2f 0a  ed into u.bu */.
1e727 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 63 6f 6e    int iDb;.  con
1e728 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72  st char *zMaster
1e729 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a  ;.  char *zSql;.
1e72a 20 20 49 6e 69 74 44 61 74 61 20 69 6e 69 74 44    InitData initD
1e72b 61 74 61 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  ata;.#endif /* l
1e72c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1e72d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 75 20 2a  oved into u.bu *
1e72e 2f 0a 0a 20 20 75 2e 62 75 2e 69 44 62 20 3d 20  /..  u.bu.iDb = 
1e72f 70 4f 70 2d 3e 70 31 3b 0a 20 20 61 73 73 65 72  pOp->p1;.  asser
1e730 74 28 20 75 2e 62 75 2e 69 44 62 3e 3d 30 20 26  t( u.bu.iDb>=0 &
1e731 26 20 75 2e 62 75 2e 69 44 62 3c 64 62 2d 3e 6e  & u.bu.iDb<db->n
1e732 44 62 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 70  Db );..  /* If p
1e733 4f 70 2d 3e 70 32 20 69 73 20 30 2c 20 74 68 65  Op->p2 is 0, the
1e734 6e 20 74 68 69 73 20 6f 70 63 6f 64 65 20 69 73  n this opcode is
1e735 20 62 65 69 6e 67 20 65 78 65 63 75 74 65 64 20   being executed 
1e736 74 6f 20 72 65 61 64 20 61 0a 20 20 2a 2a 20 73  to read a.  ** s
1e737 69 6e 67 6c 65 20 72 6f 77 2c 20 66 6f 72 20 65  ingle row, for e
1e738 78 61 6d 70 6c 65 20 74 68 65 20 72 6f 77 20 63  xample the row c
1e739 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20  orresponding to 
1e73a 61 20 6e 65 77 20 69 6e 64 65 78 0a 20 20 2a 2a  a new index.  **
1e73b 20 63 72 65 61 74 65 64 20 62 79 20 74 68 69 73   created by this
1e73c 20 56 44 42 45 2c 20 66 72 6f 6d 20 74 68 65 20   VDBE, from the 
1e73d 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61  sqlite_master ta
1e73e 62 6c 65 2e 20 49 74 20 6f 6e 6c 79 0a 20 20 2a  ble. It only.  *
1e73f 2a 20 64 6f 65 73 20 74 68 69 73 20 69 66 20 74  * does this if t
1e740 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
1e741 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 63 68 65 6d   in-memory schem
1e742 61 20 69 73 20 63 75 72 72 65 6e 74 6c 79 0a 20  a is currently. 
1e743 20 2a 2a 20 6c 6f 61 64 65 64 2e 20 4f 74 68 65   ** loaded. Othe
1e744 72 77 69 73 65 2c 20 74 68 65 20 6e 65 77 20 69  rwise, the new i
1e745 6e 64 65 78 20 64 65 66 69 6e 69 74 69 6f 6e 20  ndex definition 
1e746 63 61 6e 20 62 65 20 6c 6f 61 64 65 64 20 61 6c  can be loaded al
1e747 6f 6e 67 0a 20 20 2a 2a 20 77 69 74 68 20 74 68  ong.  ** with th
1e748 65 20 72 65 73 74 20 6f 66 20 74 68 65 20 73 63  e rest of the sc
1e749 68 65 6d 61 20 77 68 65 6e 20 69 74 20 69 73 20  hema when it is 
1e74a 72 65 71 75 69 72 65 64 2e 0a 20 20 2a 2a 0a 20  required..  **. 
1e74b 20 2a 2a 20 41 6c 74 68 6f 75 67 68 20 74 68 65   ** Although the
1e74c 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 20 42 74   mutex on the Bt
1e74d 53 68 61 72 65 64 20 6f 62 6a 65 63 74 20 74 68  Shared object th
1e74e 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74  at corresponds t
1e74f 6f 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20  o.  ** database 
1e750 75 2e 62 75 2e 69 44 62 20 28 74 68 65 20 64 61  u.bu.iDb (the da
1e751 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e  tabase containin
1e752 67 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73  g the sqlite_mas
1e753 74 65 72 20 74 61 62 6c 65 0a 20 20 2a 2a 20 72  ter table.  ** r
1e754 65 61 64 20 62 79 20 74 68 69 73 20 69 6e 73 74  ead by this inst
1e755 72 75 63 74 69 6f 6e 29 20 69 73 20 63 75 72 72  ruction) is curr
1e756 65 6e 74 6c 79 20 68 65 6c 64 2c 20 69 74 20 69  ently held, it i
1e757 73 20 6e 65 63 65 73 73 61 72 79 20 74 6f 0a 20  s necessary to. 
1e758 20 2a 2a 20 6f 62 74 61 69 6e 20 74 68 65 20 6d   ** obtain the m
1e759 75 74 65 78 65 73 20 6f 6e 20 61 6c 6c 20 61 74  utexes on all at
1e75a 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73  tached databases
1e75b 20 62 65 66 6f 72 65 20 63 68 65 63 6b 69 6e 67   before checking
1e75c 20 69 66 0a 20 20 2a 2a 20 74 68 65 20 73 63 68   if.  ** the sch
1e75d 65 6d 61 20 6f 66 20 75 2e 62 75 2e 69 44 62 20  ema of u.bu.iDb 
1e75e 69 73 20 6c 6f 61 64 65 64 2e 20 54 68 69 73 20  is loaded. This 
1e75f 69 73 20 62 65 63 61 75 73 65 2c 20 61 74 20 74  is because, at t
1e760 68 65 20 73 74 61 72 74 20 6f 66 0a 20 20 2a 2a  he start of.  **
1e761 20 74 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65   the sqlite3_exe
1e762 63 28 29 20 63 61 6c 6c 20 62 65 6c 6f 77 2c 20  c() call below, 
1e763 53 51 4c 69 74 65 20 77 69 6c 6c 20 69 6e 76 6f  SQLite will invo
1e764 6b 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 42  ke.  ** sqlite3B
1e765 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 29 2e 20  treeEnterAll(). 
1e766 49 66 20 61 6c 6c 20 6d 75 74 65 78 65 73 20 61  If all mutexes a
1e767 72 65 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 68  re not already h
1e768 65 6c 64 2c 20 74 68 65 0a 20 20 2a 2a 20 75 2e  eld, the.  ** u.
1e769 62 75 2e 69 44 62 20 6d 75 74 65 78 20 6d 61 79  bu.iDb mutex may
1e76a 20 62 65 20 74 65 6d 70 6f 72 61 72 69 6c 79 20   be temporarily 
1e76b 72 65 6c 65 61 73 65 64 20 74 6f 20 61 76 6f 69  released to avoi
1e76c 64 20 64 65 61 64 6c 6f 63 6b 2e 20 49 66 0a 20  d deadlock. If. 
1e76d 20 2a 2a 20 74 68 69 73 20 68 61 70 70 65 6e 73   ** this happens
1e76e 2c 20 74 68 65 6e 20 73 6f 6d 65 20 6f 74 68 65  , then some othe
1e76f 72 20 74 68 72 65 61 64 20 6d 61 79 20 64 65 6c  r thread may del
1e770 65 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72  ete the in-memor
1e771 79 0a 20 20 2a 2a 20 73 63 68 65 6d 61 20 6f 66  y.  ** schema of
1e772 20 64 61 74 61 62 61 73 65 20 75 2e 62 75 2e 69   database u.bu.i
1e773 44 62 20 62 65 66 6f 72 65 20 74 68 65 20 53 51  Db before the SQ
1e774 4c 20 73 74 61 74 65 6d 65 6e 74 20 72 75 6e 73  L statement runs
1e775 2e 20 54 68 65 20 73 63 68 65 6d 61 0a 20 20 2a  . The schema.  *
1e776 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 72 65  * will not be re
1e777 6c 6f 61 64 65 64 20 62 65 63 75 61 73 65 20 74  loaded becuase t
1e778 68 65 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  he db->init.busy
1e779 20 66 6c 61 67 20 69 73 20 73 65 74 2e 20 54 68   flag is set. Th
1e77a 69 73 0a 20 20 2a 2a 20 63 61 6e 20 72 65 73 75  is.  ** can resu
1e77b 6c 74 20 69 6e 20 61 20 22 6e 6f 20 73 75 63 68  lt in a "no such
1e77c 20 74 61 62 6c 65 3a 20 73 71 6c 69 74 65 5f 6d   table: sqlite_m
1e77d 61 73 74 65 72 22 20 6f 72 20 22 6d 61 6c 66 6f  aster" or "malfo
1e77e 72 6d 65 64 0a 20 20 2a 2a 20 64 61 74 61 62 61  rmed.  ** databa
1e77f 73 65 20 73 63 68 65 6d 61 22 20 65 72 72 6f 72  se schema" error
1e780 20 62 65 69 6e 67 20 72 65 74 75 72 6e 65 64 20   being returned 
1e781 74 6f 20 74 68 65 20 75 73 65 72 2e 0a 20 20 2a  to the user..  *
1e782 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  /.  assert( sqli
1e783 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74  te3BtreeHoldsMut
1e784 65 78 28 64 62 2d 3e 61 44 62 5b 75 2e 62 75 2e  ex(db->aDb[u.bu.
1e785 69 44 62 5d 2e 70 42 74 29 20 29 3b 0a 20 20 73  iDb].pBt) );.  s
1e786 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
1e787 41 6c 6c 28 64 62 29 3b 0a 20 20 69 66 28 20 70  All(db);.  if( p
1e788 4f 70 2d 3e 70 32 20 7c 7c 20 44 62 48 61 73 50  Op->p2 || DbHasP
1e789 72 6f 70 65 72 74 79 28 64 62 2c 20 75 2e 62 75  roperty(db, u.bu
1e78a 2e 69 44 62 2c 20 44 42 5f 53 63 68 65 6d 61 4c  .iDb, DB_SchemaL
1e78b 6f 61 64 65 64 29 20 29 7b 0a 20 20 20 20 75 2e  oaded) ){.    u.
1e78c 62 75 2e 7a 4d 61 73 74 65 72 20 3d 20 53 43 48  bu.zMaster = SCH
1e78d 45 4d 41 5f 54 41 42 4c 45 28 75 2e 62 75 2e 69  EMA_TABLE(u.bu.i
1e78e 44 62 29 3b 0a 20 20 20 20 75 2e 62 75 2e 69 6e  Db);.    u.bu.in
1e78f 69 74 44 61 74 61 2e 64 62 20 3d 20 64 62 3b 0a  itData.db = db;.
1e790 20 20 20 20 75 2e 62 75 2e 69 6e 69 74 44 61 74      u.bu.initDat
1e791 61 2e 69 44 62 20 3d 20 70 4f 70 2d 3e 70 31 3b  a.iDb = pOp->p1;
1e792 0a 20 20 20 20 75 2e 62 75 2e 69 6e 69 74 44 61  .    u.bu.initDa
1e793 74 61 2e 70 7a 45 72 72 4d 73 67 20 3d 20 26 70  ta.pzErrMsg = &p
1e794 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 75  ->zErrMsg;.    u
1e795 2e 62 75 2e 7a 53 71 6c 20 3d 20 73 71 6c 69 74  .bu.zSql = sqlit
1e796 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 0a 20 20  e3MPrintf(db,.  
1e797 20 20 20 20 20 22 53 45 4c 45 43 54 20 6e 61 6d       "SELECT nam
1e798 65 2c 20 72 6f 6f 74 70 61 67 65 2c 20 73 71 6c  e, rootpage, sql
1e799 20 46 52 4f 4d 20 27 25 71 27 2e 25 73 20 57 48   FROM '%q'.%s WH
1e79a 45 52 45 20 25 73 22 2c 0a 20 20 20 20 20 20 20  ERE %s",.       
1e79b 64 62 2d 3e 61 44 62 5b 75 2e 62 75 2e 69 44 62  db->aDb[u.bu.iDb
1e79c 5d 2e 7a 4e 61 6d 65 2c 20 75 2e 62 75 2e 7a 4d  ].zName, u.bu.zM
1e79d 61 73 74 65 72 2c 20 70 4f 70 2d 3e 70 34 2e 7a  aster, pOp->p4.z
1e79e 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 62 75 2e  );.    if( u.bu.
1e79f 7a 53 71 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 20  zSql==0 ){.     
1e7a0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
1e7a1 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  EM;.    }else{. 
1e7a2 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74       (void)sqlit
1e7a3 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b  e3SafetyOff(db);
1e7a4 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64  .      assert( d
1e7a5 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30 20  b->init.busy==0 
1e7a6 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 69 6e 69  );.      db->ini
1e7a7 74 2e 62 75 73 79 20 3d 20 31 3b 0a 20 20 20 20  t.busy = 1;.    
1e7a8 20 20 75 2e 62 75 2e 69 6e 69 74 44 61 74 61 2e    u.bu.initData.
1e7a9 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1e7aa 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 64        assert( !d
1e7ab 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1e7ac 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  );.      rc = sq
1e7ad 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20 75  lite3_exec(db, u
1e7ae 2e 62 75 2e 7a 53 71 6c 2c 20 73 71 6c 69 74 65  .bu.zSql, sqlite
1e7af 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 2c 20 26  3InitCallback, &
1e7b0 75 2e 62 75 2e 69 6e 69 74 44 61 74 61 2c 20 30  u.bu.initData, 0
1e7b1 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
1e7b2 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 63 20  =SQLITE_OK ) rc 
1e7b3 3d 20 75 2e 62 75 2e 69 6e 69 74 44 61 74 61 2e  = u.bu.initData.
1e7b4 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  rc;.      sqlite
1e7b5 33 44 62 46 72 65 65 28 64 62 2c 20 75 2e 62 75  3DbFree(db, u.bu
1e7b6 2e 7a 53 71 6c 29 3b 0a 20 20 20 20 20 20 64 62  .zSql);.      db
1e7b7 2d 3e 69 6e 69 74 2e 62 75 73 79 20 3d 20 30 3b  ->init.busy = 0;
1e7b8 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c  .      (void)sql
1e7b9 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29  ite3SafetyOn(db)
1e7ba 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71  ;.    }.  }.  sq
1e7bb 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 41  lite3BtreeLeaveA
1e7bc 6c 6c 28 64 62 29 3b 0a 20 20 69 66 28 20 72 63  ll(db);.  if( rc
1e7bd 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29  ==SQLITE_NOMEM )
1e7be 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65  {.    goto no_me
1e7bf 6d 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  m;.  }.  break;.
1e7c0 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  }..#if !defined(
1e7c1 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c  SQLITE_OMIT_ANAL
1e7c2 59 5a 45 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  YZE)./* Opcode: 
1e7c3 4c 6f 61 64 41 6e 61 6c 79 73 69 73 20 50 31 20  LoadAnalysis P1 
1e7c4 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65  * * * *.**.** Re
1e7c5 61 64 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74  ad the sqlite_st
1e7c6 61 74 31 20 74 61 62 6c 65 20 66 6f 72 20 64 61  at1 table for da
1e7c7 74 61 62 61 73 65 20 50 31 20 61 6e 64 20 6c 6f  tabase P1 and lo
1e7c8 61 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 0a 2a  ad the content.*
1e7c9 2a 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65 20  * of that table 
1e7ca 69 6e 74 6f 20 74 68 65 20 69 6e 74 65 72 6e 61  into the interna
1e7cb 6c 20 69 6e 64 65 78 20 68 61 73 68 20 74 61 62  l index hash tab
1e7cc 6c 65 2e 20 20 54 68 69 73 20 77 69 6c 6c 20 63  le.  This will c
1e7cd 61 75 73 65 0a 2a 2a 20 74 68 65 20 61 6e 61 6c  ause.** the anal
1e7ce 79 73 69 73 20 74 6f 20 62 65 20 75 73 65 64 20  ysis to be used 
1e7cf 77 68 65 6e 20 70 72 65 70 61 72 69 6e 67 20 61  when preparing a
1e7d0 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20 71 75  ll subsequent qu
1e7d1 65 72 69 65 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f  eries..*/.case O
1e7d2 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 73 3a 20  P_LoadAnalysis: 
1e7d3 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  {.  assert( pOp-
1e7d4 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70  >p1>=0 && pOp->p
1e7d5 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 72  1<db->nDb );.  r
1e7d6 63 20 3d 20 73 71 6c 69 74 65 33 41 6e 61 6c 79  c = sqlite3Analy
1e7d7 73 69 73 4c 6f 61 64 28 64 62 2c 20 70 4f 70 2d  sisLoad(db, pOp-
1e7d8 3e 70 31 29 3b 0a 20 20 62 72 65 61 6b 3b 20 20  >p1);.  break;  
1e7d9 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  .}.#endif /* !de
1e7da 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
1e7db 54 5f 41 4e 41 4c 59 5a 45 29 20 2a 2f 0a 0a 2f  T_ANALYZE) */../
1e7dc 2a 20 4f 70 63 6f 64 65 3a 20 44 72 6f 70 54 61  * Opcode: DropTa
1e7dd 62 6c 65 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a  ble P1 * * P4 *.
1e7de 2a 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65  **.** Remove the
1e7df 20 69 6e 74 65 72 6e 61 6c 20 28 69 6e 2d 6d 65   internal (in-me
1e7e0 6d 6f 72 79 29 20 64 61 74 61 20 73 74 72 75 63  mory) data struc
1e7e1 74 75 72 65 73 20 74 68 61 74 20 64 65 73 63 72  tures that descr
1e7e2 69 62 65 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65  ibe.** the table
1e7e3 20 6e 61 6d 65 64 20 50 34 20 69 6e 20 64 61 74   named P4 in dat
1e7e4 61 62 61 73 65 20 50 31 2e 20 20 54 68 69 73 20  abase P1.  This 
1e7e5 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20  is called after 
1e7e6 61 20 74 61 62 6c 65 0a 2a 2a 20 69 73 20 64 72  a table.** is dr
1e7e7 6f 70 70 65 64 20 69 6e 20 6f 72 64 65 72 20 74  opped in order t
1e7e8 6f 20 6b 65 65 70 20 74 68 65 20 69 6e 74 65 72  o keep the inter
1e7e9 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69  nal representati
1e7ea 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 73 63 68  on of the.** sch
1e7eb 65 6d 61 20 63 6f 6e 73 69 73 74 65 6e 74 20 77  ema consistent w
1e7ec 69 74 68 20 77 68 61 74 20 69 73 20 6f 6e 20 64  ith what is on d
1e7ed 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  isk..*/.case OP_
1e7ee 44 72 6f 70 54 61 62 6c 65 3a 20 7b 0a 20 20 73  DropTable: {.  s
1e7ef 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44  qlite3UnlinkAndD
1e7f0 65 6c 65 74 65 54 61 62 6c 65 28 64 62 2c 20 70  eleteTable(db, p
1e7f1 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e  Op->p1, pOp->p4.
1e7f2 7a 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  z);.  break;.}..
1e7f3 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 72 6f 70 49  /* Opcode: DropI
1e7f4 6e 64 65 78 20 50 31 20 2a 20 2a 20 50 34 20 2a  ndex P1 * * P4 *
1e7f5 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68  .**.** Remove th
1e7f6 65 20 69 6e 74 65 72 6e 61 6c 20 28 69 6e 2d 6d  e internal (in-m
1e7f7 65 6d 6f 72 79 29 20 64 61 74 61 20 73 74 72 75  emory) data stru
1e7f8 63 74 75 72 65 73 20 74 68 61 74 20 64 65 73 63  ctures that desc
1e7f9 72 69 62 65 0a 2a 2a 20 74 68 65 20 69 6e 64 65  ribe.** the inde
1e7fa 78 20 6e 61 6d 65 64 20 50 34 20 69 6e 20 64 61  x named P4 in da
1e7fb 74 61 62 61 73 65 20 50 31 2e 20 20 54 68 69 73  tabase P1.  This
1e7fc 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72   is called after
1e7fd 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 69 73 20   an index.** is 
1e7fe 64 72 6f 70 70 65 64 20 69 6e 20 6f 72 64 65 72  dropped in order
1e7ff 20 74 6f 20 6b 65 65 70 20 74 68 65 20 69 6e 74   to keep the int
1e800 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61  ernal representa
1e801 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 73  tion of the.** s
1e802 63 68 65 6d 61 20 63 6f 6e 73 69 73 74 65 6e 74  chema consistent
1e803 20 77 69 74 68 20 77 68 61 74 20 69 73 20 6f 6e   with what is on
1e804 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f   disk..*/.case O
1e805 50 5f 44 72 6f 70 49 6e 64 65 78 3a 20 7b 0a 20  P_DropIndex: {. 
1e806 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e   sqlite3UnlinkAn
1e807 64 44 65 6c 65 74 65 49 6e 64 65 78 28 64 62 2c  dDeleteIndex(db,
1e808 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70   pOp->p1, pOp->p
1e809 34 2e 7a 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  4.z);.  break;.}
1e80a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 72 6f  ../* Opcode: Dro
1e80b 70 54 72 69 67 67 65 72 20 50 31 20 2a 20 2a 20  pTrigger P1 * * 
1e80c 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f 76  P4 *.**.** Remov
1e80d 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 28  e the internal (
1e80e 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 61 74 61 20  in-memory) data 
1e80f 73 74 72 75 63 74 75 72 65 73 20 74 68 61 74 20  structures that 
1e810 64 65 73 63 72 69 62 65 0a 2a 2a 20 74 68 65 20  describe.** the 
1e811 74 72 69 67 67 65 72 20 6e 61 6d 65 64 20 50 34  trigger named P4
1e812 20 69 6e 20 64 61 74 61 62 61 73 65 20 50 31 2e   in database P1.
1e813 20 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64    This is called
1e814 20 61 66 74 65 72 20 61 20 74 72 69 67 67 65 72   after a trigger
1e815 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 64 20 69  .** is dropped i
1e816 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20  n order to keep 
1e817 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70  the internal rep
1e818 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  resentation of t
1e819 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63 6f 6e  he.** schema con
1e81a 73 69 73 74 65 6e 74 20 77 69 74 68 20 77 68 61  sistent with wha
1e81b 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2f  t is on disk..*/
1e81c 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70 54 72 69  .case OP_DropTri
1e81d 67 67 65 72 3a 20 7b 0a 20 20 73 71 6c 69 74 65  gger: {.  sqlite
1e81e 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65  3UnlinkAndDelete
1e81f 54 72 69 67 67 65 72 28 64 62 2c 20 70 4f 70 2d  Trigger(db, pOp-
1e820 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b  >p1, pOp->p4.z);
1e821 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 23 69  .  break;.}...#i
1e822 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1e823 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43  T_INTEGRITY_CHEC
1e824 4b 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 74  K./* Opcode: Int
1e825 65 67 72 69 74 79 43 6b 20 50 31 20 50 32 20 50  egrityCk P1 P2 P
1e826 33 20 2a 20 50 35 0a 2a 2a 0a 2a 2a 20 44 6f 20  3 * P5.**.** Do 
1e827 61 6e 20 61 6e 61 6c 79 73 69 73 20 6f 66 20 74  an analysis of t
1e828 68 65 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 65  he currently ope
1e829 6e 20 64 61 74 61 62 61 73 65 2e 20 20 53 74 6f  n database.  Sto
1e82a 72 65 20 69 6e 0a 2a 2a 20 72 65 67 69 73 74 65  re in.** registe
1e82b 72 20 50 31 20 74 68 65 20 74 65 78 74 20 6f 66  r P1 the text of
1e82c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
1e82d 65 20 64 65 73 63 72 69 62 69 6e 67 20 61 6e 79  e describing any
1e82e 20 70 72 6f 62 6c 65 6d 73 2e 0a 2a 2a 20 49 66   problems..** If
1e82f 20 6e 6f 20 70 72 6f 62 6c 65 6d 73 20 61 72 65   no problems are
1e830 20 66 6f 75 6e 64 2c 20 73 74 6f 72 65 20 61 20   found, store a 
1e831 4e 55 4c 4c 20 69 6e 20 72 65 67 69 73 74 65 72  NULL in register
1e832 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72   P1..**.** The r
1e833 65 67 69 73 74 65 72 20 50 33 20 63 6f 6e 74 61  egister P3 conta
1e834 69 6e 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20  ins the maximum 
1e835 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 77 65  number of allowe
1e836 64 20 65 72 72 6f 72 73 2e 0a 2a 2a 20 41 74 20  d errors..** At 
1e837 6d 6f 73 74 20 72 65 67 28 50 33 29 20 65 72 72  most reg(P3) err
1e838 6f 72 73 20 77 69 6c 6c 20 62 65 20 72 65 70 6f  ors will be repo
1e839 72 74 65 64 2e 0a 2a 2a 20 49 6e 20 6f 74 68 65  rted..** In othe
1e83a 72 20 77 6f 72 64 73 2c 20 74 68 65 20 61 6e 61  r words, the ana
1e83b 6c 79 73 69 73 20 73 74 6f 70 73 20 61 73 20 73  lysis stops as s
1e83c 6f 6f 6e 20 61 73 20 72 65 67 28 50 31 29 20 65  oon as reg(P1) e
1e83d 72 72 6f 72 73 20 61 72 65 20 0a 2a 2a 20 73 65  rrors are .** se
1e83e 65 6e 2e 20 20 52 65 67 28 50 31 29 20 69 73 20  en.  Reg(P1) is 
1e83f 75 70 64 61 74 65 64 20 77 69 74 68 20 74 68 65  updated with the
1e840 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72   number of error
1e841 73 20 72 65 6d 61 69 6e 69 6e 67 2e 0a 2a 2a 0a  s remaining..**.
1e842 2a 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 67 65  ** The root page
1e843 20 6e 75 6d 62 65 72 73 20 6f 66 20 61 6c 6c 20   numbers of all 
1e844 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 64 61  tables in the da
1e845 74 61 62 61 73 65 20 61 72 65 20 69 6e 74 65 67  tabase are integ
1e846 65 72 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20  er.** stored in 
1e847 72 65 67 28 50 31 29 2c 20 72 65 67 28 50 31 2b  reg(P1), reg(P1+
1e848 31 29 2c 20 72 65 67 28 50 31 2b 32 29 2c 20 2e  1), reg(P1+2), .
1e849 2e 2e 2e 20 20 54 68 65 72 65 20 61 72 65 20 50  ...  There are P
1e84a 32 20 74 61 62 6c 65 73 0a 2a 2a 20 74 6f 74 61  2 tables.** tota
1e84b 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 20 69  l..**.** If P5 i
1e84c 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 20  s not zero, the 
1e84d 63 68 65 63 6b 20 69 73 20 64 6f 6e 65 20 6f 6e  check is done on
1e84e 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20 64   the auxiliary d
1e84f 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 2c  atabase.** file,
1e850 20 6e 6f 74 20 74 68 65 20 6d 61 69 6e 20 64 61   not the main da
1e851 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a  tabase file..**.
1e852 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69  ** This opcode i
1e853 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d  s used to implem
1e854 65 6e 74 20 74 68 65 20 69 6e 74 65 67 72 69 74  ent the integrit
1e855 79 5f 63 68 65 63 6b 20 70 72 61 67 6d 61 2e 0a  y_check pragma..
1e856 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 74 65 67  */.case OP_Integ
1e857 72 69 74 79 43 6b 3a 20 7b 0a 23 69 66 20 30 20  rityCk: {.#if 0 
1e858 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1e859 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1e85a 2e 62 76 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 6f  .bv */.  int nRo
1e85b 6f 74 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  ot;      /* Numb
1e85c 65 72 20 6f 66 20 74 61 62 6c 65 73 20 74 6f 20  er of tables to 
1e85d 63 68 65 63 6b 2e 20 20 28 4e 75 6d 62 65 72 20  check.  (Number 
1e85e 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 2e 29 20  of root pages.) 
1e85f 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 6f 6f 74 3b  */.  int *aRoot;
1e860 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66       /* Array of
1e861 20 72 6f 6f 74 70 61 67 65 20 6e 75 6d 62 65 72   rootpage number
1e862 73 20 66 6f 72 20 74 61 62 6c 65 73 20 74 6f 20  s for tables to 
1e863 62 65 20 63 68 65 63 6b 65 64 20 2a 2f 0a 20 20  be checked */.  
1e864 69 6e 74 20 6a 3b 20 20 20 20 20 20 20 20 20 20  int j;          
1e865 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
1e866 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20 20  */.  int nErr;  
1e867 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1e868 66 20 65 72 72 6f 72 73 20 72 65 70 6f 72 74 65  f errors reporte
1e869 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 3b 20  d */.  char *z; 
1e86a 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f         /* Text o
1e86b 66 20 74 68 65 20 65 72 72 6f 72 20 72 65 70 6f  f the error repo
1e86c 72 74 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 6e 45  rt */.  Mem *pnE
1e86d 72 72 3b 20 20 20 20 20 2f 2a 20 52 65 67 69 73  rr;     /* Regis
1e86e 74 65 72 20 6b 65 65 70 69 6e 67 20 74 72 61 63  ter keeping trac
1e86f 6b 20 6f 66 20 65 72 72 6f 72 73 20 72 65 6d 61  k of errors rema
1e870 69 6e 69 6e 67 20 2a 2f 0a 23 65 6e 64 69 66 20  ining */.#endif 
1e871 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1e872 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1e873 62 76 20 2a 2f 0a 0a 20 20 75 2e 62 76 2e 6e 52  bv */..  u.bv.nR
1e874 6f 6f 74 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20  oot = pOp->p2;. 
1e875 20 61 73 73 65 72 74 28 20 75 2e 62 76 2e 6e 52   assert( u.bv.nR
1e876 6f 6f 74 3e 30 20 29 3b 0a 20 20 75 2e 62 76 2e  oot>0 );.  u.bv.
1e877 61 52 6f 6f 74 20 3d 20 73 71 6c 69 74 65 33 44  aRoot = sqlite3D
1e878 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73  bMallocRaw(db, s
1e879 69 7a 65 6f 66 28 69 6e 74 29 2a 28 75 2e 62 76  izeof(int)*(u.bv
1e87a 2e 6e 52 6f 6f 74 2b 31 29 20 29 3b 0a 20 20 69  .nRoot+1) );.  i
1e87b 66 28 20 75 2e 62 76 2e 61 52 6f 6f 74 3d 3d 30  f( u.bv.aRoot==0
1e87c 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a   ) goto no_mem;.
1e87d 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1e87e 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d  3>0 && pOp->p3<=
1e87f 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e 62  p->nMem );.  u.b
1e880 76 2e 70 6e 45 72 72 20 3d 20 26 70 2d 3e 61 4d  v.pnErr = &p->aM
1e881 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 61  em[pOp->p3];.  a
1e882 73 73 65 72 74 28 20 28 75 2e 62 76 2e 70 6e 45  ssert( (u.bv.pnE
1e883 72 72 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  rr->flags & MEM_
1e884 49 6e 74 29 21 3d 30 20 29 3b 0a 20 20 61 73 73  Int)!=0 );.  ass
1e885 65 72 74 28 20 28 75 2e 62 76 2e 70 6e 45 72 72  ert( (u.bv.pnErr
1e886 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53  ->flags & (MEM_S
1e887 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 3d 3d 30  tr|MEM_Blob))==0
1e888 20 29 3b 0a 20 20 70 49 6e 31 20 3d 20 26 70 2d   );.  pIn1 = &p-
1e889 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  >aMem[pOp->p1];.
1e88a 20 20 66 6f 72 28 75 2e 62 76 2e 6a 3d 30 3b 20    for(u.bv.j=0; 
1e88b 75 2e 62 76 2e 6a 3c 75 2e 62 76 2e 6e 52 6f 6f  u.bv.j<u.bv.nRoo
1e88c 74 3b 20 75 2e 62 76 2e 6a 2b 2b 29 7b 0a 20 20  t; u.bv.j++){.  
1e88d 20 20 75 2e 62 76 2e 61 52 6f 6f 74 5b 75 2e 62    u.bv.aRoot[u.b
1e88e 76 2e 6a 5d 20 3d 20 28 69 6e 74 29 73 71 6c 69  v.j] = (int)sqli
1e88f 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28  te3VdbeIntValue(
1e890 26 70 49 6e 31 5b 75 2e 62 76 2e 6a 5d 29 3b 0a  &pIn1[u.bv.j]);.
1e891 20 20 7d 0a 20 20 75 2e 62 76 2e 61 52 6f 6f 74    }.  u.bv.aRoot
1e892 5b 75 2e 62 76 2e 6a 5d 20 3d 20 30 3b 0a 20 20  [u.bv.j] = 0;.  
1e893 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 35 3c  assert( pOp->p5<
1e894 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73  db->nDb );.  ass
1e895 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61  ert( (p->btreeMa
1e896 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 35  sk & (1<<pOp->p5
1e897 29 29 21 3d 30 20 29 3b 0a 20 20 75 2e 62 76 2e  ))!=0 );.  u.bv.
1e898 7a 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  z = sqlite3Btree
1e899 49 6e 74 65 67 72 69 74 79 43 68 65 63 6b 28 64  IntegrityCheck(d
1e89a 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 35 5d 2e  b->aDb[pOp->p5].
1e89b 70 42 74 2c 20 75 2e 62 76 2e 61 52 6f 6f 74 2c  pBt, u.bv.aRoot,
1e89c 20 75 2e 62 76 2e 6e 52 6f 6f 74 2c 0a 20 20 20   u.bv.nRoot,.   
1e89d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e89e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 69                (i
1e89f 6e 74 29 75 2e 62 76 2e 70 6e 45 72 72 2d 3e 75  nt)u.bv.pnErr->u
1e8a0 2e 69 2c 20 26 75 2e 62 76 2e 6e 45 72 72 29 3b  .i, &u.bv.nErr);
1e8a1 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
1e8a2 28 64 62 2c 20 75 2e 62 76 2e 61 52 6f 6f 74 29  (db, u.bv.aRoot)
1e8a3 3b 0a 20 20 75 2e 62 76 2e 70 6e 45 72 72 2d 3e  ;.  u.bv.pnErr->
1e8a4 75 2e 69 20 2d 3d 20 75 2e 62 76 2e 6e 45 72 72  u.i -= u.bv.nErr
1e8a5 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ;.  sqlite3VdbeM
1e8a6 65 6d 53 65 74 4e 75 6c 6c 28 70 49 6e 31 29 3b  emSetNull(pIn1);
1e8a7 0a 20 20 69 66 28 20 75 2e 62 76 2e 6e 45 72 72  .  if( u.bv.nErr
1e8a8 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ==0 ){.    asser
1e8a9 74 28 20 75 2e 62 76 2e 7a 3d 3d 30 20 29 3b 0a  t( u.bv.z==0 );.
1e8aa 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 62 76    }else if( u.bv
1e8ab 2e 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74  .z==0 ){.    got
1e8ac 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 65 6c 73  o no_mem;.  }els
1e8ad 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  e{.    sqlite3Vd
1e8ae 62 65 4d 65 6d 53 65 74 53 74 72 28 70 49 6e 31  beMemSetStr(pIn1
1e8af 2c 20 75 2e 62 76 2e 7a 2c 20 2d 31 2c 20 53 51  , u.bv.z, -1, SQ
1e8b0 4c 49 54 45 5f 55 54 46 38 2c 20 73 71 6c 69 74  LITE_UTF8, sqlit
1e8b1 65 33 5f 66 72 65 65 29 3b 0a 20 20 7d 0a 20 20  e3_free);.  }.  
1e8b2 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53  UPDATE_MAX_BLOBS
1e8b3 49 5a 45 28 70 49 6e 31 29 3b 0a 20 20 73 71 6c  IZE(pIn1);.  sql
1e8b4 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e  ite3VdbeChangeEn
1e8b5 63 6f 64 69 6e 67 28 70 49 6e 31 2c 20 65 6e 63  coding(pIn1, enc
1e8b6 6f 64 69 6e 67 29 3b 0a 20 20 62 72 65 61 6b 3b  oding);.  break;
1e8b7 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
1e8b8 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49  ITE_OMIT_INTEGRI
1e8b9 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 2f 2a 20  TY_CHECK */../* 
1e8ba 4f 70 63 6f 64 65 3a 20 52 6f 77 53 65 74 41 64  Opcode: RowSetAd
1e8bb 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  d P1 P2 * * *.**
1e8bc 0a 2a 2a 20 49 6e 73 65 72 74 20 74 68 65 20 69  .** Insert the i
1e8bd 6e 74 65 67 65 72 20 76 61 6c 75 65 20 68 65 6c  nteger value hel
1e8be 64 20 62 79 20 72 65 67 69 73 74 65 72 20 50 32  d by register P2
1e8bf 20 69 6e 74 6f 20 61 20 62 6f 6f 6c 65 61 6e 20   into a boolean 
1e8c0 69 6e 64 65 78 0a 2a 2a 20 68 65 6c 64 20 69 6e  index.** held in
1e8c1 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a   register P1..**
1e8c2 0a 2a 2a 20 41 6e 20 61 73 73 65 72 74 69 6f 6e  .** An assertion
1e8c3 20 66 61 69 6c 73 20 69 66 20 50 32 20 69 73 20   fails if P2 is 
1e8c4 6e 6f 74 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a  not an integer..
1e8c5 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77 53 65  */.case OP_RowSe
1e8c6 74 41 64 64 3a 20 7b 20 20 20 20 20 20 20 2f 2a  tAdd: {       /*
1e8c7 20 69 6e 32 20 2a 2f 0a 23 69 66 20 30 20 20 2f   in2 */.#if 0  /
1e8c8 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1e8c9 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62  s moved into u.b
1e8ca 77 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 64 78  w */.  Mem *pIdx
1e8cb 3b 0a 20 20 4d 65 6d 20 2a 70 56 61 6c 3b 0a 23  ;.  Mem *pVal;.#
1e8cc 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76  endif /* local v
1e8cd 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1e8ce 6e 74 6f 20 75 2e 62 77 20 2a 2f 0a 20 20 61 73  nto u.bw */.  as
1e8cf 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20  sert( pOp->p1>0 
1e8d0 26 26 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e  && pOp->p1<=p->n
1e8d1 4d 65 6d 20 29 3b 0a 20 20 75 2e 62 77 2e 70 49  Mem );.  u.bw.pI
1e8d2 64 78 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  dx = &p->aMem[pO
1e8d3 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74  p->p1];.  assert
1e8d4 28 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70  ( pOp->p2>0 && p
1e8d5 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20  Op->p2<=p->nMem 
1e8d6 29 3b 0a 20 20 75 2e 62 77 2e 70 56 61 6c 20 3d  );.  u.bw.pVal =
1e8d7 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
1e8d8 32 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 28 75  2];.  assert( (u
1e8d9 2e 62 77 2e 70 56 61 6c 2d 3e 66 6c 61 67 73 20  .bw.pVal->flags 
1e8da 26 20 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 29 3b  & MEM_Int)!=0 );
1e8db 0a 20 20 69 66 28 20 28 75 2e 62 77 2e 70 49 64  .  if( (u.bw.pId
1e8dc 78 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52  x->flags & MEM_R
1e8dd 6f 77 53 65 74 29 3d 3d 30 20 29 7b 0a 20 20 20  owSet)==0 ){.   
1e8de 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1e8df 65 74 52 6f 77 53 65 74 28 75 2e 62 77 2e 70 49  etRowSet(u.bw.pI
1e8e0 64 78 29 3b 0a 20 20 20 20 69 66 28 20 28 75 2e  dx);.    if( (u.
1e8e1 62 77 2e 70 49 64 78 2d 3e 66 6c 61 67 73 20 26  bw.pIdx->flags &
1e8e2 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20   MEM_RowSet)==0 
1e8e3 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20  ) goto no_mem;. 
1e8e4 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 6f 77 53   }.  sqlite3RowS
1e8e5 65 74 49 6e 73 65 72 74 28 75 2e 62 77 2e 70 49  etInsert(u.bw.pI
1e8e6 64 78 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20 75  dx->u.pRowSet, u
1e8e7 2e 62 77 2e 70 56 61 6c 2d 3e 75 2e 69 29 3b 0a  .bw.pVal->u.i);.
1e8e8 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1e8e9 70 63 6f 64 65 3a 20 52 6f 77 53 65 74 52 65 61  pcode: RowSetRea
1e8ea 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a  d P1 P2 P3 * *.*
1e8eb 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65  *.** Extract the
1e8ec 20 73 6d 61 6c 6c 65 73 74 20 76 61 6c 75 65 20   smallest value 
1e8ed 66 72 6f 6d 20 62 6f 6f 6c 65 61 6e 20 69 6e 64  from boolean ind
1e8ee 65 78 20 50 31 20 61 6e 64 20 70 75 74 20 74 68  ex P1 and put th
1e8ef 61 74 20 76 61 6c 75 65 20 69 6e 74 6f 0a 2a 2a  at value into.**
1e8f0 20 72 65 67 69 73 74 65 72 20 50 33 2e 20 20 4f   register P3.  O
1e8f1 72 2c 20 69 66 20 62 6f 6f 6c 65 61 6e 20 69 6e  r, if boolean in
1e8f2 64 65 78 20 50 31 20 69 73 20 69 6e 69 74 69 61  dex P1 is initia
1e8f3 6c 6c 79 20 65 6d 70 74 79 2c 20 6c 65 61 76 65  lly empty, leave
1e8f4 20 50 33 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64   P3.** unchanged
1e8f5 20 61 6e 64 20 6a 75 6d 70 20 74 6f 20 69 6e 73   and jump to ins
1e8f6 74 72 75 63 74 69 6f 6e 20 50 32 2e 0a 2a 2f 0a  truction P2..*/.
1e8f7 63 61 73 65 20 4f 50 5f 52 6f 77 53 65 74 52 65  case OP_RowSetRe
1e8f8 61 64 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20 6a  ad: {       /* j
1e8f9 75 6d 70 2c 20 6f 75 74 33 20 2a 2f 0a 23 69 66  ump, out3 */.#if
1e8fa 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72   0  /* local var
1e8fb 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1e8fc 6f 20 75 2e 62 78 20 2a 2f 0a 20 20 4d 65 6d 20  o u.bx */.  Mem 
1e8fd 2a 70 49 64 78 3b 0a 20 20 69 36 34 20 76 61 6c  *pIdx;.  i64 val
1e8fe 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
1e8ff 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1e900 64 20 69 6e 74 6f 20 75 2e 62 78 20 2a 2f 0a 20  d into u.bx */. 
1e901 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31   assert( pOp->p1
1e902 3e 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 3d 70  >0 && pOp->p1<=p
1e903 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 43 48 45 43  ->nMem );.  CHEC
1e904 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b  K_FOR_INTERRUPT;
1e905 0a 20 20 75 2e 62 78 2e 70 49 64 78 20 3d 20 26  .  u.bx.pIdx = &
1e906 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d  p->aMem[pOp->p1]
1e907 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61  ;.  pOut = &p->a
1e908 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20  Mem[pOp->p3];.  
1e909 69 66 28 20 28 75 2e 62 78 2e 70 49 64 78 2d 3e  if( (u.bx.pIdx->
1e90a 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53  flags & MEM_RowS
1e90b 65 74 29 3d 3d 30 0a 20 20 20 7c 7c 20 73 71 6c  et)==0.   || sql
1e90c 69 74 65 33 52 6f 77 53 65 74 4e 65 78 74 28 75  ite3RowSetNext(u
1e90d 2e 62 78 2e 70 49 64 78 2d 3e 75 2e 70 52 6f 77  .bx.pIdx->u.pRow
1e90e 53 65 74 2c 20 26 75 2e 62 78 2e 76 61 6c 29 3d  Set, &u.bx.val)=
1e90f 3d 30 0a 20 20 29 7b 0a 20 20 20 20 2f 2a 20 54  =0.  ){.    /* T
1e910 68 65 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 65 78  he boolean index
1e911 20 69 73 20 65 6d 70 74 79 20 2a 2f 0a 20 20 20   is empty */.   
1e912 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1e913 65 74 4e 75 6c 6c 28 75 2e 62 78 2e 70 49 64 78  etNull(u.bx.pIdx
1e914 29 3b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  );.    pc = pOp-
1e915 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65  >p2 - 1;.  }else
1e916 7b 0a 20 20 20 20 2f 2a 20 41 20 76 61 6c 75 65  {.    /* A value
1e917 20 77 61 73 20 70 75 6c 6c 65 64 20 66 72 6f 6d   was pulled from
1e918 20 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20   the index */.  
1e919 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1e91a 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d  3>0 && pOp->p3<=
1e91b 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 73  p->nMem );.    s
1e91c 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
1e91d 49 6e 74 36 34 28 70 4f 75 74 2c 20 75 2e 62 78  Int64(pOut, u.bx
1e91e 2e 76 61 6c 29 3b 0a 20 20 7d 0a 20 20 62 72 65  .val);.  }.  bre
1e91f 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1e920 3a 20 52 6f 77 53 65 74 54 65 73 74 20 50 31 20  : RowSetTest P1 
1e921 50 32 20 50 33 20 50 34 0a 2a 2a 0a 2a 2a 20 52  P2 P3 P4.**.** R
1e922 65 67 69 73 74 65 72 20 50 33 20 69 73 20 61 73  egister P3 is as
1e923 73 75 6d 65 64 20 74 6f 20 68 6f 6c 64 20 61 20  sumed to hold a 
1e924 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 76  64-bit integer v
1e925 61 6c 75 65 2e 20 49 66 20 72 65 67 69 73 74 65  alue. If registe
1e926 72 20 50 31 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73  r P1.** contains
1e927 20 61 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74   a RowSet object
1e928 20 61 6e 64 20 74 68 61 74 20 52 6f 77 53 65 74   and that RowSet
1e929 20 6f 62 6a 65 63 74 20 63 6f 6e 74 61 69 6e 73   object contains
1e92a 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 68 65  .** the value he
1e92b 6c 64 20 69 6e 20 50 33 2c 20 6a 75 6d 70 20 74  ld in P3, jump t
1e92c 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 20 4f  o register P2. O
1e92d 74 68 65 72 77 69 73 65 2c 20 69 6e 73 65 72 74  therwise, insert
1e92e 20 74 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20   the.** integer 
1e92f 69 6e 20 50 33 20 69 6e 74 6f 20 74 68 65 20 52  in P3 into the R
1e930 6f 77 53 65 74 20 61 6e 64 20 63 6f 6e 74 69 6e  owSet and contin
1e931 75 65 20 6f 6e 20 74 6f 20 74 68 65 0a 2a 2a 20  ue on to the.** 
1e932 6e 65 78 74 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a  next opcode..**.
1e933 2a 2a 20 54 68 65 20 52 6f 77 53 65 74 20 6f 62  ** The RowSet ob
1e934 6a 65 63 74 20 69 73 20 6f 70 74 69 6d 69 7a 65  ject is optimize
1e935 64 20 66 6f 72 20 74 68 65 20 63 61 73 65 20 77  d for the case w
1e936 68 65 72 65 20 73 75 63 63 65 73 73 69 76 65 20  here successive 
1e937 73 65 74 73 0a 2a 2a 20 6f 66 20 69 6e 74 65 67  sets.** of integ
1e938 65 72 73 2c 20 77 68 65 72 65 20 65 61 63 68 20  ers, where each 
1e939 73 65 74 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20  set contains no 
1e93a 64 75 70 6c 69 63 61 74 65 73 2e 20 45 61 63 68  duplicates. Each
1e93b 20 73 65 74 0a 2a 2a 20 6f 66 20 76 61 6c 75 65   set.** of value
1e93c 73 20 69 73 20 69 64 65 6e 74 69 66 69 65 64 20  s is identified 
1e93d 62 79 20 61 20 75 6e 69 71 75 65 20 50 34 20 76  by a unique P4 v
1e93e 61 6c 75 65 2e 20 54 68 65 20 66 69 72 73 74 20  alue. The first 
1e93f 73 65 74 0a 2a 2a 20 6d 75 73 74 20 68 61 76 65  set.** must have
1e940 20 50 34 3d 3d 30 2c 20 74 68 65 20 66 69 6e 61   P4==0, the fina
1e941 6c 20 73 65 74 20 50 34 3d 2d 31 2e 20 20 50 34  l set P4=-1.  P4
1e942 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20   must be either 
1e943 2d 31 20 6f 72 0a 2a 2a 20 6e 6f 6e 2d 6e 65 67  -1 or.** non-neg
1e944 61 74 69 76 65 2e 20 20 46 6f 72 20 6e 6f 6e 2d  ative.  For non-
1e945 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65 73 20  negative values 
1e946 6f 66 20 50 34 20 6f 6e 6c 79 20 74 68 65 20 6c  of P4 only the l
1e947 6f 77 65 72 20 34 0a 2a 2a 20 62 69 74 73 20 61  ower 4.** bits a
1e948 72 65 20 73 69 67 6e 69 66 69 63 61 6e 74 2e 0a  re significant..
1e949 2a 2a 0a 2a 2a 20 54 68 69 73 20 61 6c 6c 6f 77  **.** This allow
1e94a 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 3a  s optimizations:
1e94b 20 28 61 29 20 77 68 65 6e 20 50 34 3d 3d 30 20   (a) when P4==0 
1e94c 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64  there is no need
1e94d 20 74 6f 20 74 65 73 74 0a 2a 2a 20 74 68 65 20   to test.** the 
1e94e 72 6f 77 73 65 74 20 6f 62 6a 65 63 74 20 66 6f  rowset object fo
1e94f 72 20 50 33 2c 20 61 73 20 69 74 20 69 73 20 67  r P3, as it is g
1e950 75 61 72 61 6e 74 65 65 64 20 6e 6f 74 20 74 6f  uaranteed not to
1e951 20 63 6f 6e 74 61 69 6e 20 69 74 2c 0a 2a 2a 20   contain it,.** 
1e952 28 62 29 20 77 68 65 6e 20 50 34 3d 3d 2d 31 20  (b) when P4==-1 
1e953 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64  there is no need
1e954 20 74 6f 20 69 6e 73 65 72 74 20 74 68 65 20 76   to insert the v
1e955 61 6c 75 65 2c 20 61 73 20 69 74 20 77 69 6c 6c  alue, as it will
1e956 0a 2a 2a 20 6e 65 76 65 72 20 62 65 20 74 65 73  .** never be tes
1e957 74 65 64 20 66 6f 72 2c 20 61 6e 64 20 28 63 29  ted for, and (c)
1e958 20 77 68 65 6e 20 61 20 76 61 6c 75 65 20 74 68   when a value th
1e959 61 74 20 69 73 20 70 61 72 74 20 6f 66 20 73 65  at is part of se
1e95a 74 20 58 20 69 73 0a 2a 2a 20 69 6e 73 65 72 74  t X is.** insert
1e95b 65 64 2c 20 74 68 65 72 65 20 69 73 20 6e 6f 20  ed, there is no 
1e95c 6e 65 65 64 20 74 6f 20 73 65 61 72 63 68 20 74  need to search t
1e95d 6f 20 73 65 65 20 69 66 20 74 68 65 20 73 61 6d  o see if the sam
1e95e 65 20 76 61 6c 75 65 20 77 61 73 0a 2a 2a 20 70  e value was.** p
1e95f 72 65 76 69 6f 75 73 6c 79 20 69 6e 73 65 72 74  reviously insert
1e960 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 73 65  ed as part of se
1e961 74 20 58 20 28 6f 6e 6c 79 20 69 66 20 69 74 20  t X (only if it 
1e962 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a  was previously.*
1e963 2a 20 69 6e 73 65 72 74 65 64 20 61 73 20 70 61  * inserted as pa
1e964 72 74 20 6f 66 20 73 6f 6d 65 20 6f 74 68 65 72  rt of some other
1e965 20 73 65 74 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f   set)..*/.case O
1e966 50 5f 52 6f 77 53 65 74 54 65 73 74 3a 20 7b 20  P_RowSetTest: { 
1e967 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e968 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31      /* jump, in1
1e969 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20  , in3 */.#if 0  
1e96a 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1e96b 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1e96c 62 79 20 2a 2f 0a 20 20 69 6e 74 20 69 53 65 74  by */.  int iSet
1e96d 3b 0a 20 20 69 6e 74 20 65 78 69 73 74 73 3b 0a  ;.  int exists;.
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 79 20 2a 2f 0a 0a 20 20  into u.by */..  
1e971 75 2e 62 79 2e 69 53 65 74 20 3d 20 70 4f 70 2d  u.by.iSet = pOp-
1e972 3e 70 34 2e 69 3b 0a 20 20 61 73 73 65 72 74 28  >p4.i;.  assert(
1e973 20 70 49 6e 33 2d 3e 66 6c 61 67 73 26 4d 45 4d   pIn3->flags&MEM
1e974 5f 49 6e 74 20 29 3b 0a 0a 20 20 2f 2a 20 49 66  _Int );..  /* If
1e975 20 74 68 65 72 65 20 69 73 20 61 6e 79 74 68 69   there is anythi
1e976 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 61 20  ng other than a 
1e977 72 6f 77 73 65 74 20 6f 62 6a 65 63 74 20 69 6e  rowset object in
1e978 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 50 31 2c   memory cell P1,
1e979 0a 20 20 2a 2a 20 64 65 6c 65 74 65 20 69 74 20  .  ** delete it 
1e97a 6e 6f 77 20 61 6e 64 20 69 6e 69 74 69 61 6c 69  now and initiali
1e97b 7a 65 20 50 31 20 77 69 74 68 20 61 6e 20 65 6d  ze P1 with an em
1e97c 70 74 79 20 72 6f 77 73 65 74 0a 20 20 2a 2f 0a  pty rowset.  */.
1e97d 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61    if( (pIn1->fla
1e97e 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29  gs & MEM_RowSet)
1e97f 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
1e980 65 33 56 64 62 65 4d 65 6d 53 65 74 52 6f 77 53  e3VdbeMemSetRowS
1e981 65 74 28 70 49 6e 31 29 3b 0a 20 20 20 20 69 66  et(pIn1);.    if
1e982 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26  ( (pIn1->flags &
1e983 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20   MEM_RowSet)==0 
1e984 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20  ) goto no_mem;. 
1e985 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f   }..  assert( pO
1e986 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e  p->p4type==P4_IN
1e987 54 33 32 20 29 3b 0a 20 20 61 73 73 65 72 74 28  T32 );.  assert(
1e988 20 75 2e 62 79 2e 69 53 65 74 3d 3d 2d 31 20 7c   u.by.iSet==-1 |
1e989 7c 20 75 2e 62 79 2e 69 53 65 74 3e 3d 30 20 29  | u.by.iSet>=0 )
1e98a 3b 0a 20 20 69 66 28 20 75 2e 62 79 2e 69 53 65  ;.  if( u.by.iSe
1e98b 74 20 29 7b 0a 20 20 20 20 75 2e 62 79 2e 65 78  t ){.    u.by.ex
1e98c 69 73 74 73 20 3d 20 73 71 6c 69 74 65 33 52 6f  ists = sqlite3Ro
1e98d 77 53 65 74 54 65 73 74 28 70 49 6e 31 2d 3e 75  wSetTest(pIn1->u
1e98e 2e 70 52 6f 77 53 65 74 2c 0a 20 20 20 20 20 20  .pRowSet,.      
1e98f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e990 20 20 20 20 20 20 20 20 20 28 75 38 29 28 75 2e           (u8)(u.
1e991 62 79 2e 69 53 65 74 3e 3d 30 20 3f 20 75 2e 62  by.iSet>=0 ? u.b
1e992 79 2e 69 53 65 74 20 26 20 30 78 66 20 3a 20 30  y.iSet & 0xf : 0
1e993 78 66 66 29 2c 0a 20 20 20 20 20 20 20 20 20 20  xff),.          
1e994 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e995 20 20 20 20 20 70 49 6e 33 2d 3e 75 2e 69 29 3b       pIn3->u.i);
1e996 0a 20 20 20 20 69 66 28 20 75 2e 62 79 2e 65 78  .    if( u.by.ex
1e997 69 73 74 73 20 29 7b 0a 20 20 20 20 20 20 70 63  ists ){.      pc
1e998 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
1e999 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1e99a 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 75 2e 62   }.  }.  if( u.b
1e99b 79 2e 69 53 65 74 3e 3d 30 20 29 7b 0a 20 20 20  y.iSet>=0 ){.   
1e99c 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e   sqlite3RowSetIn
1e99d 73 65 72 74 28 70 49 6e 31 2d 3e 75 2e 70 52 6f  sert(pIn1->u.pRo
1e99e 77 53 65 74 2c 20 70 49 6e 33 2d 3e 75 2e 69 29  wSet, pIn3->u.i)
1e99f 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1e9a0 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
1e9a1 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 0a  E_OMIT_TRIGGER..
1e9a2 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 72 6f 67 72  /* Opcode: Progr
1e9a3 61 6d 20 50 31 20 50 32 20 50 33 20 50 34 20 2a  am P1 P2 P3 P4 *
1e9a4 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 74  .**.** Execute t
1e9a5 68 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 72  he trigger progr
1e9a6 61 6d 20 70 61 73 73 65 64 20 61 73 20 50 34 20  am passed as P4 
1e9a7 28 74 79 70 65 20 50 34 5f 53 55 42 50 52 4f 47  (type P4_SUBPROG
1e9a8 52 41 4d 29 2e 20 0a 2a 2a 0a 2a 2a 20 50 31 20  RAM). .**.** P1 
1e9a9 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 61 64 64  contains the add
1e9aa 72 65 73 73 20 6f 66 20 74 68 65 20 6d 65 6d 6f  ress of the memo
1e9ab 72 79 20 63 65 6c 6c 20 74 68 61 74 20 63 6f 6e  ry cell that con
1e9ac 74 61 69 6e 73 20 74 68 65 20 66 69 72 73 74 20  tains the first 
1e9ad 6d 65 6d 6f 72 79 20 0a 2a 2a 20 63 65 6c 6c 20  memory .** cell 
1e9ae 69 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20 76  in an array of v
1e9af 61 6c 75 65 73 20 75 73 65 64 20 61 73 20 61 72  alues used as ar
1e9b0 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 73  guments to the s
1e9b1 75 62 2d 70 72 6f 67 72 61 6d 2e 20 50 32 20 0a  ub-program. P2 .
1e9b2 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  ** contains the 
1e9b3 61 64 64 72 65 73 73 20 74 6f 20 6a 75 6d 70 20  address to jump 
1e9b4 74 6f 20 69 66 20 74 68 65 20 73 75 62 2d 70 72  to if the sub-pr
1e9b5 6f 67 72 61 6d 20 74 68 72 6f 77 73 20 61 6e 20  ogram throws an 
1e9b6 49 47 4e 4f 52 45 20 0a 2a 2a 20 65 78 63 65 70  IGNORE .** excep
1e9b7 74 69 6f 6e 20 75 73 69 6e 67 20 74 68 65 20 52  tion using the R
1e9b8 41 49 53 45 28 29 20 66 75 6e 63 74 69 6f 6e 2e  AISE() function.
1e9b9 20 52 65 67 69 73 74 65 72 20 50 33 20 63 6f 6e   Register P3 con
1e9ba 74 61 69 6e 73 20 74 68 65 20 61 64 64 72 65 73  tains the addres
1e9bb 73 20 0a 2a 2a 20 6f 66 20 61 20 6d 65 6d 6f 72  s .** of a memor
1e9bc 79 20 63 65 6c 6c 20 69 6e 20 74 68 69 73 20 28  y cell in this (
1e9bd 74 68 65 20 70 61 72 65 6e 74 29 20 56 4d 20 74  the parent) VM t
1e9be 68 61 74 20 69 73 20 75 73 65 64 20 74 6f 20 61  hat is used to a
1e9bf 6c 6c 6f 63 61 74 65 20 74 68 65 20 0a 2a 2a 20  llocate the .** 
1e9c0 6d 65 6d 6f 72 79 20 72 65 71 75 69 72 65 64 20  memory required 
1e9c1 62 79 20 74 68 65 20 73 75 62 2d 76 64 62 65 20  by the sub-vdbe 
1e9c2 61 74 20 72 75 6e 74 69 6d 65 2e 0a 2a 2a 0a 2a  at runtime..**.*
1e9c3 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65  * P4 is a pointe
1e9c4 72 20 74 6f 20 74 68 65 20 56 4d 20 63 6f 6e 74  r to the VM cont
1e9c5 61 69 6e 69 6e 67 20 74 68 65 20 74 72 69 67 67  aining the trigg
1e9c6 65 72 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 63  er program..*/.c
1e9c7 61 73 65 20 4f 50 5f 50 72 6f 67 72 61 6d 3a 20  ase OP_Program: 
1e9c8 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70  {        /* jump
1e9c9 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f   */.#if 0  /* lo
1e9ca 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1e9cb 76 65 64 20 69 6e 74 6f 20 75 2e 62 7a 20 2a 2f  ved into u.bz */
1e9cc 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20  .  int nMem;    
1e9cd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
1e9ce 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 72  mber of memory r
1e9cf 65 67 69 73 74 65 72 73 20 66 6f 72 20 73 75 62  egisters for sub
1e9d0 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e  -program */.  in
1e9d1 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20  t nByte;        
1e9d2 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f        /* Bytes o
1e9d3 66 20 72 75 6e 74 69 6d 65 20 73 70 61 63 65 20  f runtime space 
1e9d4 72 65 71 75 69 72 65 64 20 66 6f 72 20 73 75 62  required for sub
1e9d5 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 4d 65  -program */.  Me
1e9d6 6d 20 2a 70 52 74 3b 20 20 20 20 20 20 20 20 20  m *pRt;         
1e9d7 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
1e9d8 72 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 72 75  r to allocate ru
1e9d9 6e 74 69 6d 65 20 73 70 61 63 65 20 2a 2f 0a 20  ntime space */. 
1e9da 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20   Mem *pMem;     
1e9db 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64           /* Used
1e9dc 20 74 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f   to iterate thro
1e9dd 75 67 68 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73  ugh memory cells
1e9de 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 45 6e 64 3b   */.  Mem *pEnd;
1e9df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1e9e0 20 4c 61 73 74 20 6d 65 6d 6f 72 79 20 63 65 6c   Last memory cel
1e9e1 6c 20 69 6e 20 6e 65 77 20 61 72 72 61 79 20 2a  l in new array *
1e9e2 2f 0a 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70  /.  VdbeFrame *p
1e9e3 46 72 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20 4e  Frame;      /* N
1e9e4 65 77 20 76 64 62 65 20 66 72 61 6d 65 20 74 6f  ew vdbe frame to
1e9e5 20 65 78 65 63 75 74 65 20 69 6e 20 2a 2f 0a 20   execute in */. 
1e9e6 20 53 75 62 50 72 6f 67 72 61 6d 20 2a 70 50 72   SubProgram *pPr
1e9e7 6f 67 72 61 6d 3b 20 20 20 2f 2a 20 53 75 62 2d  ogram;   /* Sub-
1e9e8 70 72 6f 67 72 61 6d 20 74 6f 20 65 78 65 63 75  program to execu
1e9e9 74 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 74 3b  te */.  void *t;
1e9ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e9eb 2f 2a 20 54 6f 6b 65 6e 20 69 64 65 6e 74 69 66  /* Token identif
1e9ec 79 69 6e 67 20 74 72 69 67 67 65 72 20 2a 2f 0a  ying trigger */.
1e9ed 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
1e9ee 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1e9ef 69 6e 74 6f 20 75 2e 62 7a 20 2a 2f 0a 0a 20 20  into u.bz */..  
1e9f0 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d 20 3d 20  u.bz.pProgram = 
1e9f1 70 4f 70 2d 3e 70 34 2e 70 50 72 6f 67 72 61 6d  pOp->p4.pProgram
1e9f2 3b 0a 20 20 75 2e 62 7a 2e 70 52 74 20 3d 20 26  ;.  u.bz.pRt = &
1e9f3 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d  p->aMem[pOp->p3]
1e9f4 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 7a  ;.  assert( u.bz
1e9f5 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 4f 70 3e 30  .pProgram->nOp>0
1e9f6 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65   );..  /* If the
1e9f7 20 53 51 4c 49 54 45 5f 52 65 63 54 72 69 67 67   SQLITE_RecTrigg
1e9f8 65 72 73 20 66 6c 61 67 20 69 73 20 63 6c 65 61  ers flag is clea
1e9f9 72 2c 20 74 68 65 6e 20 72 65 63 75 72 73 69 76  r, then recursiv
1e9fa 65 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 0a  e invocation of.
1e9fb 20 20 2a 2a 20 74 72 69 67 67 65 72 73 20 69 73    ** triggers is
1e9fc 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20 62 61   disabled for ba
1e9fd 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62  ckwards compatib
1e9fe 69 6c 69 74 79 20 28 66 6c 61 67 20 73 65 74 2f  ility (flag set/
1e9ff 63 6c 65 61 72 65 64 20 62 79 0a 20 20 2a 2a 20  cleared by.  ** 
1ea00 74 68 65 20 22 50 52 41 47 4d 41 20 72 65 63 75  the "PRAGMA recu
1ea01 72 73 69 76 65 5f 74 72 69 67 67 65 72 73 22 20  rsive_triggers" 
1ea02 63 6f 6d 6d 61 6e 64 29 2e 0a 20 20 2a 2a 0a 20  command)..  **. 
1ea03 20 2a 2a 20 49 74 20 69 73 20 72 65 63 75 72 73   ** It is recurs
1ea04 69 76 65 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f  ive invocation o
1ea05 66 20 74 72 69 67 67 65 72 73 2c 20 61 74 20 74  f triggers, at t
1ea06 68 65 20 53 51 4c 20 6c 65 76 65 6c 2c 20 74 68  he SQL level, th
1ea07 61 74 20 69 73 0a 20 20 2a 2a 20 64 69 73 61 62  at is.  ** disab
1ea08 6c 65 64 2e 20 49 6e 20 73 6f 6d 65 20 63 61 73  led. In some cas
1ea09 65 73 20 61 20 73 69 6e 67 6c 65 20 74 72 69 67  es a single trig
1ea0a 67 65 72 20 6d 61 79 20 67 65 6e 65 72 61 74 65  ger may generate
1ea0b 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 0a 20   more than one. 
1ea0c 20 2a 2a 20 53 75 62 50 72 6f 67 72 61 6d 20 28   ** SubProgram (
1ea0d 69 66 20 74 68 65 20 74 72 69 67 67 65 72 20 6d  if the trigger m
1ea0e 61 79 20 62 65 20 65 78 65 63 75 74 65 64 20 77  ay be executed w
1ea0f 69 74 68 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e  ith more than on
1ea10 65 20 64 69 66 66 65 72 65 6e 74 0a 20 20 2a 2a  e different.  **
1ea11 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 61 6c 67   ON CONFLICT alg
1ea12 6f 72 69 74 68 6d 29 2e 20 53 75 62 50 72 6f 67  orithm). SubProg
1ea13 72 61 6d 20 73 74 72 75 63 74 75 72 65 73 20 61  ram structures a
1ea14 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61  ssociated with a
1ea15 0a 20 20 2a 2a 20 73 69 6e 67 6c 65 20 74 72 69  .  ** single tri
1ea16 67 67 65 72 20 61 6c 6c 20 68 61 76 65 20 74 68  gger all have th
1ea17 65 20 73 61 6d 65 20 76 61 6c 75 65 20 66 6f 72  e same value for
1ea18 20 74 68 65 20 53 75 62 50 72 6f 67 72 61 6d 2e   the SubProgram.
1ea19 74 6f 6b 65 6e 0a 20 20 2a 2a 20 76 61 72 69 61  token.  ** varia
1ea1a 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ble..  */.  if( 
1ea1b 30 3d 3d 28 64 62 2d 3e 66 6c 61 67 73 26 53 51  0==(db->flags&SQ
1ea1c 4c 49 54 45 5f 52 65 63 54 72 69 67 67 65 72 73  LITE_RecTriggers
1ea1d 29 20 29 7b 0a 20 20 20 20 75 2e 62 7a 2e 74 20  ) ){.    u.bz.t 
1ea1e 3d 20 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d 2d  = u.bz.pProgram-
1ea1f 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20 66 6f 72 28  >token;.    for(
1ea20 75 2e 62 7a 2e 70 46 72 61 6d 65 3d 70 2d 3e 70  u.bz.pFrame=p->p
1ea21 46 72 61 6d 65 3b 20 75 2e 62 7a 2e 70 46 72 61  Frame; u.bz.pFra
1ea22 6d 65 20 26 26 20 75 2e 62 7a 2e 70 46 72 61 6d  me && u.bz.pFram
1ea23 65 2d 3e 74 6f 6b 65 6e 21 3d 75 2e 62 7a 2e 74  e->token!=u.bz.t
1ea24 3b 20 75 2e 62 7a 2e 70 46 72 61 6d 65 3d 75 2e  ; u.bz.pFrame=u.
1ea25 62 7a 2e 70 46 72 61 6d 65 2d 3e 70 50 61 72 65  bz.pFrame->pPare
1ea26 6e 74 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 62  nt);.    if( u.b
1ea27 7a 2e 70 46 72 61 6d 65 20 29 20 62 72 65 61 6b  z.pFrame ) break
1ea28 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 2d 3e  ;.  }..  if( p->
1ea29 6e 46 72 61 6d 65 3e 64 62 2d 3e 61 4c 69 6d 69  nFrame>db->aLimi
1ea2a 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 54  t[SQLITE_LIMIT_T
1ea2b 52 49 47 47 45 52 5f 44 45 50 54 48 5d 20 29 7b  RIGGER_DEPTH] ){
1ea2c 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
1ea2d 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69  _ERROR;.    sqli
1ea2e 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d  te3SetString(&p-
1ea2f 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 74  >zErrMsg, db, "t
1ea30 6f 6f 20 6d 61 6e 79 20 6c 65 76 65 6c 73 20 6f  oo many levels o
1ea31 66 20 74 72 69 67 67 65 72 20 72 65 63 75 72 73  f trigger recurs
1ea32 69 6f 6e 22 29 3b 0a 20 20 20 20 62 72 65 61 6b  ion");.    break
1ea33 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 67 69  ;.  }..  /* Regi
1ea34 73 74 65 72 20 75 2e 62 7a 2e 70 52 74 20 69 73  ster u.bz.pRt is
1ea35 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74   used to store t
1ea36 68 65 20 6d 65 6d 6f 72 79 20 72 65 71 75 69 72  he memory requir
1ea37 65 64 20 74 6f 20 73 61 76 65 20 74 68 65 20 73  ed to save the s
1ea38 74 61 74 65 0a 20 20 2a 2a 20 6f 66 20 74 68 65  tate.  ** of the
1ea39 20 63 75 72 72 65 6e 74 20 70 72 6f 67 72 61 6d   current program
1ea3a 2c 20 61 6e 64 20 74 68 65 20 6d 65 6d 6f 72 79  , and the memory
1ea3b 20 72 65 71 75 69 72 65 64 20 61 74 20 72 75 6e   required at run
1ea3c 74 69 6d 65 20 74 6f 20 65 78 65 63 75 74 65 0a  time to execute.
1ea3d 20 20 2a 2a 20 74 68 65 20 74 72 69 67 67 65 72    ** the trigger
1ea3e 20 70 72 6f 67 72 61 6d 2e 20 49 66 20 74 68 69   program. If thi
1ea3f 73 20 74 72 69 67 67 65 72 20 68 61 73 20 62 65  s trigger has be
1ea40 65 6e 20 66 69 72 65 64 20 62 65 66 6f 72 65 2c  en fired before,
1ea41 20 74 68 65 6e 20 75 2e 62 7a 2e 70 52 74 0a 20   then u.bz.pRt. 
1ea42 20 2a 2a 20 69 73 20 61 6c 72 65 61 64 79 20 61   ** is already a
1ea43 6c 6c 6f 63 61 74 65 64 2e 20 4f 74 68 65 72 77  llocated. Otherw
1ea44 69 73 65 2c 20 69 74 20 6d 75 73 74 20 62 65 20  ise, it must be 
1ea45 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 20 2a 2f  initialized.  */
1ea46 0a 20 20 69 66 28 20 28 75 2e 62 7a 2e 70 52 74  .  if( (u.bz.pRt
1ea47 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 46 72 61 6d  ->flags&MEM_Fram
1ea48 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20  e)==0 ){.    /* 
1ea49 53 75 62 50 72 6f 67 72 61 6d 2e 6e 4d 65 6d 20  SubProgram.nMem 
1ea4a 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75  is set to the nu
1ea4b 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 63  mber of memory c
1ea4c 65 6c 6c 73 20 75 73 65 64 20 62 79 20 74 68 65  ells used by the
1ea4d 0a 20 20 20 20 2a 2a 20 70 72 6f 67 72 61 6d 20  .    ** program 
1ea4e 73 74 6f 72 65 64 20 69 6e 20 53 75 62 50 72 6f  stored in SubPro
1ea4f 67 72 61 6d 2e 61 4f 70 2e 20 41 73 20 77 65 6c  gram.aOp. As wel
1ea50 6c 20 61 73 20 74 68 65 73 65 2c 20 6f 6e 65 20  l as these, one 
1ea51 6d 65 6d 6f 72 79 0a 20 20 20 20 2a 2a 20 63 65  memory.    ** ce
1ea52 6c 6c 20 69 73 20 72 65 71 75 69 72 65 64 20 66  ll is required f
1ea53 6f 72 20 65 61 63 68 20 63 75 72 73 6f 72 20 75  or each cursor u
1ea54 73 65 64 20 62 79 20 74 68 65 20 70 72 6f 67 72  sed by the progr
1ea55 61 6d 2e 20 53 65 74 20 6c 6f 63 61 6c 0a 20 20  am. Set local.  
1ea56 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 20 75 2e    ** variable u.
1ea57 62 7a 2e 6e 4d 65 6d 20 28 61 6e 64 20 6c 61 74  bz.nMem (and lat
1ea58 65 72 2c 20 56 64 62 65 46 72 61 6d 65 2e 6e 43  er, VdbeFrame.nC
1ea59 68 69 6c 64 4d 65 6d 29 20 74 6f 20 74 68 69 73  hildMem) to this
1ea5a 20 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f 0a 20   value..    */. 
1ea5b 20 20 20 75 2e 62 7a 2e 6e 4d 65 6d 20 3d 20 75     u.bz.nMem = u
1ea5c 2e 62 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 4d  .bz.pProgram->nM
1ea5d 65 6d 20 2b 20 75 2e 62 7a 2e 70 50 72 6f 67 72  em + u.bz.pProgr
1ea5e 61 6d 2d 3e 6e 43 73 72 3b 0a 20 20 20 20 75 2e  am->nCsr;.    u.
1ea5f 62 7a 2e 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44  bz.nByte = ROUND
1ea60 38 28 73 69 7a 65 6f 66 28 56 64 62 65 46 72 61  8(sizeof(VdbeFra
1ea61 6d 65 29 29 0a 20 20 20 20 20 20 20 20 20 20 20  me)).           
1ea62 20 20 20 2b 20 75 2e 62 7a 2e 6e 4d 65 6d 20 2a     + u.bz.nMem *
1ea63 20 73 69 7a 65 6f 66 28 4d 65 6d 29 0a 20 20 20   sizeof(Mem).   
1ea64 20 20 20 20 20 20 20 20 20 20 20 2b 20 75 2e 62             + u.b
1ea65 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 72  z.pProgram->nCsr
1ea66 20 2a 20 73 69 7a 65 6f 66 28 56 64 62 65 43 75   * sizeof(VdbeCu
1ea67 72 73 6f 72 20 2a 29 3b 0a 20 20 20 20 75 2e 62  rsor *);.    u.b
1ea68 7a 2e 70 46 72 61 6d 65 20 3d 20 73 71 6c 69 74  z.pFrame = sqlit
1ea69 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
1ea6a 62 2c 20 75 2e 62 7a 2e 6e 42 79 74 65 29 3b 0a  b, u.bz.nByte);.
1ea6b 20 20 20 20 69 66 28 20 21 75 2e 62 7a 2e 70 46      if( !u.bz.pF
1ea6c 72 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 67 6f  rame ){.      go
1ea6d 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d  to no_mem;.    }
1ea6e 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1ea6f 4d 65 6d 52 65 6c 65 61 73 65 28 75 2e 62 7a 2e  MemRelease(u.bz.
1ea70 70 52 74 29 3b 0a 20 20 20 20 75 2e 62 7a 2e 70  pRt);.    u.bz.p
1ea71 52 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  Rt->flags = MEM_
1ea72 46 72 61 6d 65 3b 0a 20 20 20 20 75 2e 62 7a 2e  Frame;.    u.bz.
1ea73 70 52 74 2d 3e 75 2e 70 46 72 61 6d 65 20 3d 20  pRt->u.pFrame = 
1ea74 75 2e 62 7a 2e 70 46 72 61 6d 65 3b 0a 0a 20 20  u.bz.pFrame;..  
1ea75 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 76    u.bz.pFrame->v
1ea76 20 3d 20 70 3b 0a 20 20 20 20 75 2e 62 7a 2e 70   = p;.    u.bz.p
1ea77 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 4d 65 6d  Frame->nChildMem
1ea78 20 3d 20 75 2e 62 7a 2e 6e 4d 65 6d 3b 0a 20 20   = u.bz.nMem;.  
1ea79 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e    u.bz.pFrame->n
1ea7a 43 68 69 6c 64 43 73 72 20 3d 20 75 2e 62 7a 2e  ChildCsr = u.bz.
1ea7b 70 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 3b 0a  pProgram->nCsr;.
1ea7c 20 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d      u.bz.pFrame-
1ea7d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20 20 75 2e  >pc = pc;.    u.
1ea7e 62 7a 2e 70 46 72 61 6d 65 2d 3e 61 4d 65 6d 20  bz.pFrame->aMem 
1ea7f 3d 20 70 2d 3e 61 4d 65 6d 3b 0a 20 20 20 20 75  = p->aMem;.    u
1ea80 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 4d 65 6d  .bz.pFrame->nMem
1ea81 20 3d 20 70 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20   = p->nMem;.    
1ea82 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 61 70 43  u.bz.pFrame->apC
1ea83 73 72 20 3d 20 70 2d 3e 61 70 43 73 72 3b 0a 20  sr = p->apCsr;. 
1ea84 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e     u.bz.pFrame->
1ea85 6e 43 75 72 73 6f 72 20 3d 20 70 2d 3e 6e 43 75  nCursor = p->nCu
1ea86 72 73 6f 72 3b 0a 20 20 20 20 75 2e 62 7a 2e 70  rsor;.    u.bz.p
1ea87 46 72 61 6d 65 2d 3e 61 4f 70 20 3d 20 70 2d 3e  Frame->aOp = p->
1ea88 61 4f 70 3b 0a 20 20 20 20 75 2e 62 7a 2e 70 46  aOp;.    u.bz.pF
1ea89 72 61 6d 65 2d 3e 6e 4f 70 20 3d 20 70 2d 3e 6e  rame->nOp = p->n
1ea8a 4f 70 3b 0a 20 20 20 20 75 2e 62 7a 2e 70 46 72  Op;.    u.bz.pFr
1ea8b 61 6d 65 2d 3e 74 6f 6b 65 6e 20 3d 20 75 2e 62  ame->token = u.b
1ea8c 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e 74 6f 6b 65  z.pProgram->toke
1ea8d 6e 3b 0a 0a 20 20 20 20 75 2e 62 7a 2e 70 45 6e  n;..    u.bz.pEn
1ea8e 64 20 3d 20 26 56 64 62 65 46 72 61 6d 65 4d 65  d = &VdbeFrameMe
1ea8f 6d 28 75 2e 62 7a 2e 70 46 72 61 6d 65 29 5b 75  m(u.bz.pFrame)[u
1ea90 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 69  .bz.pFrame->nChi
1ea91 6c 64 4d 65 6d 5d 3b 0a 20 20 20 20 66 6f 72 28  ldMem];.    for(
1ea92 75 2e 62 7a 2e 70 4d 65 6d 3d 56 64 62 65 46 72  u.bz.pMem=VdbeFr
1ea93 61 6d 65 4d 65 6d 28 75 2e 62 7a 2e 70 46 72 61  ameMem(u.bz.pFra
1ea94 6d 65 29 3b 20 75 2e 62 7a 2e 70 4d 65 6d 21 3d  me); u.bz.pMem!=
1ea95 75 2e 62 7a 2e 70 45 6e 64 3b 20 75 2e 62 7a 2e  u.bz.pEnd; u.bz.
1ea96 70 4d 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 75  pMem++){.      u
1ea97 2e 62 7a 2e 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  .bz.pMem->flags 
1ea98 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20  = MEM_Null;.    
1ea99 20 20 75 2e 62 7a 2e 70 4d 65 6d 2d 3e 64 62 20    u.bz.pMem->db 
1ea9a 3d 20 64 62 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  = db;.    }.  }e
1ea9b 6c 73 65 7b 0a 20 20 20 20 75 2e 62 7a 2e 70 46  lse{.    u.bz.pF
1ea9c 72 61 6d 65 20 3d 20 75 2e 62 7a 2e 70 52 74 2d  rame = u.bz.pRt-
1ea9d 3e 75 2e 70 46 72 61 6d 65 3b 0a 20 20 20 20 61  >u.pFrame;.    a
1ea9e 73 73 65 72 74 28 20 75 2e 62 7a 2e 70 50 72 6f  ssert( u.bz.pPro
1ea9f 67 72 61 6d 2d 3e 6e 4d 65 6d 2b 75 2e 62 7a 2e  gram->nMem+u.bz.
1eaa0 70 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 3d 3d  pProgram->nCsr==
1eaa1 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 43 68  u.bz.pFrame->nCh
1eaa2 69 6c 64 4d 65 6d 20 29 3b 0a 20 20 20 20 61 73  ildMem );.    as
1eaa3 73 65 72 74 28 20 75 2e 62 7a 2e 70 50 72 6f 67  sert( u.bz.pProg
1eaa4 72 61 6d 2d 3e 6e 43 73 72 3d 3d 75 2e 62 7a 2e  ram->nCsr==u.bz.
1eaa5 70 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 43 73  pFrame->nChildCs
1eaa6 72 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  r );.    assert(
1eaa7 20 70 63 3d 3d 75 2e 62 7a 2e 70 46 72 61 6d 65   pc==u.bz.pFrame
1eaa8 2d 3e 70 63 20 29 3b 0a 20 20 7d 0a 0a 20 20 70  ->pc );.  }..  p
1eaa9 2d 3e 6e 46 72 61 6d 65 2b 2b 3b 0a 20 20 75 2e  ->nFrame++;.  u.
1eaaa 62 7a 2e 70 46 72 61 6d 65 2d 3e 70 50 61 72 65  bz.pFrame->pPare
1eaab 6e 74 20 3d 20 70 2d 3e 70 46 72 61 6d 65 3b 0a  nt = p->pFrame;.
1eaac 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6c    u.bz.pFrame->l
1eaad 61 73 74 52 6f 77 69 64 20 3d 20 64 62 2d 3e 6c  astRowid = db->l
1eaae 61 73 74 52 6f 77 69 64 3b 0a 20 20 75 2e 62 7a  astRowid;.  u.bz
1eaaf 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 61 6e 67 65  .pFrame->nChange
1eab0 20 3d 20 70 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20   = p->nChange;. 
1eab1 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 30 3b   p->nChange = 0;
1eab2 0a 20 20 70 2d 3e 70 46 72 61 6d 65 20 3d 20 75  .  p->pFrame = u
1eab3 2e 62 7a 2e 70 46 72 61 6d 65 3b 0a 20 20 70 2d  .bz.pFrame;.  p-
1eab4 3e 61 4d 65 6d 20 3d 20 26 56 64 62 65 46 72 61  >aMem = &VdbeFra
1eab5 6d 65 4d 65 6d 28 75 2e 62 7a 2e 70 46 72 61 6d  meMem(u.bz.pFram
1eab6 65 29 5b 2d 31 5d 3b 0a 20 20 70 2d 3e 6e 4d 65  e)[-1];.  p->nMe
1eab7 6d 20 3d 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d  m = u.bz.pFrame-
1eab8 3e 6e 43 68 69 6c 64 4d 65 6d 3b 0a 20 20 70 2d  >nChildMem;.  p-
1eab9 3e 6e 43 75 72 73 6f 72 20 3d 20 28 75 31 36 29  >nCursor = (u16)
1eaba 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 43 68  u.bz.pFrame->nCh
1eabb 69 6c 64 43 73 72 3b 0a 20 20 70 2d 3e 61 70 43  ildCsr;.  p->apC
1eabc 73 72 20 3d 20 28 56 64 62 65 43 75 72 73 6f 72  sr = (VdbeCursor
1eabd 20 2a 2a 29 26 70 2d 3e 61 4d 65 6d 5b 70 2d 3e   **)&p->aMem[p->
1eabe 6e 4d 65 6d 2b 31 5d 3b 0a 20 20 70 2d 3e 61 4f  nMem+1];.  p->aO
1eabf 70 20 3d 20 75 2e 62 7a 2e 70 50 72 6f 67 72 61  p = u.bz.pProgra
1eac0 6d 2d 3e 61 4f 70 3b 0a 20 20 70 2d 3e 6e 4f 70  m->aOp;.  p->nOp
1eac1 20 3d 20 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d   = u.bz.pProgram
1eac2 2d 3e 6e 4f 70 3b 0a 20 20 70 63 20 3d 20 2d 31  ->nOp;.  pc = -1
1eac3 3b 0a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  ;..  break;.}../
1eac4 2a 20 4f 70 63 6f 64 65 3a 20 50 61 72 61 6d 20  * Opcode: Param 
1eac5 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  P1 P2 * * *.**.*
1eac6 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 73  * This opcode is
1eac7 20 6f 6e 6c 79 20 65 76 65 72 20 70 72 65 73 65   only ever prese
1eac8 6e 74 20 69 6e 20 73 75 62 2d 70 72 6f 67 72 61  nt in sub-progra
1eac9 6d 73 20 63 61 6c 6c 65 64 20 76 69 61 20 74 68  ms called via th
1eaca 65 20 0a 2a 2a 20 4f 50 5f 50 72 6f 67 72 61 6d  e .** OP_Program
1eacb 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 43 6f   instruction. Co
1eacc 70 79 20 61 20 76 61 6c 75 65 20 63 75 72 72 65  py a value curre
1eacd 6e 74 6c 79 20 73 74 6f 72 65 64 20 69 6e 20 61  ntly stored in a
1eace 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 63 65 6c 6c   memory .** cell
1eacf 20 6f 66 20 74 68 65 20 63 61 6c 6c 69 6e 67 20   of the calling 
1ead0 28 70 61 72 65 6e 74 29 20 66 72 61 6d 65 20 74  (parent) frame t
1ead1 6f 20 63 65 6c 6c 20 50 32 20 69 6e 20 74 68 65  o cell P2 in the
1ead2 20 63 75 72 72 65 6e 74 20 66 72 61 6d 65 73 20   current frames 
1ead3 0a 2a 2a 20 61 64 64 72 65 73 73 20 73 70 61 63  .** address spac
1ead4 65 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20  e. This is used 
1ead5 62 79 20 74 72 69 67 67 65 72 20 70 72 6f 67 72  by trigger progr
1ead6 61 6d 73 20 74 6f 20 61 63 63 65 73 73 20 74 68  ams to access th
1ead7 65 20 6e 65 77 2e 2a 20 0a 2a 2a 20 61 6e 64 20  e new.* .** and 
1ead8 6f 6c 64 2e 2a 20 76 61 6c 75 65 73 2e 0a 2a 2a  old.* values..**
1ead9 0a 2a 2a 20 54 68 65 20 61 64 64 72 65 73 73 20  .** The address 
1eada 6f 66 20 74 68 65 20 63 65 6c 6c 20 69 6e 20 74  of the cell in t
1eadb 68 65 20 70 61 72 65 6e 74 20 66 72 61 6d 65 20  he parent frame 
1eadc 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79  is determined by
1eadd 20 61 64 64 69 6e 67 0a 2a 2a 20 74 68 65 20 76   adding.** the v
1eade 61 6c 75 65 20 6f 66 20 74 68 65 20 50 31 20 61  alue of the P1 a
1eadf 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 76  rgument to the v
1eae0 61 6c 75 65 20 6f 66 20 74 68 65 20 50 31 20 61  alue of the P1 a
1eae1 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 0a 2a  rgument to the.*
1eae2 2a 20 63 61 6c 6c 69 6e 67 20 4f 50 5f 50 72 6f  * calling OP_Pro
1eae3 67 72 61 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e  gram instruction
1eae4 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 61 72  ..*/.case OP_Par
1eae5 61 6d 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  am: {           
1eae6 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61  /* out2-prerelea
1eae7 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20  se */.#if 0  /* 
1eae8 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1eae9 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 61 20  moved into u.ca 
1eaea 2a 2f 0a 20 20 56 64 62 65 46 72 61 6d 65 20 2a  */.  VdbeFrame *
1eaeb 70 46 72 61 6d 65 3b 0a 20 20 4d 65 6d 20 2a 70  pFrame;.  Mem *p
1eaec 49 6e 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f  In;.#endif /* lo
1eaed 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1eaee 76 65 64 20 69 6e 74 6f 20 75 2e 63 61 20 2a 2f  ved into u.ca */
1eaef 0a 20 20 75 2e 63 61 2e 70 46 72 61 6d 65 20 3d  .  u.ca.pFrame =
1eaf0 20 70 2d 3e 70 46 72 61 6d 65 3b 0a 20 20 75 2e   p->pFrame;.  u.
1eaf1 63 61 2e 70 49 6e 20 3d 20 26 75 2e 63 61 2e 70  ca.pIn = &u.ca.p
1eaf2 46 72 61 6d 65 2d 3e 61 4d 65 6d 5b 70 4f 70 2d  Frame->aMem[pOp-
1eaf3 3e 70 31 20 2b 20 75 2e 63 61 2e 70 46 72 61 6d  >p1 + u.ca.pFram
1eaf4 65 2d 3e 61 4f 70 5b 75 2e 63 61 2e 70 46 72 61  e->aOp[u.ca.pFra
1eaf5 6d 65 2d 3e 70 63 5d 2e 70 31 5d 3b 0a 20 20 73  me->pc].p1];.  s
1eaf6 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61  qlite3VdbeMemSha
1eaf7 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 74 2c 20 75  llowCopy(pOut, u
1eaf8 2e 63 61 2e 70 49 6e 2c 20 4d 45 4d 5f 45 70 68  .ca.pIn, MEM_Eph
1eaf9 65 6d 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  em);.  break;.}.
1eafa 0a 23 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64  .#endif /* #ifnd
1eafb 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
1eafc 52 49 47 47 45 52 20 2a 2f 0a 0a 23 69 66 6e 64  RIGGER */..#ifnd
1eafd 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
1eafe 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 2f 2a 20  UTOINCREMENT./* 
1eaff 4f 70 63 6f 64 65 3a 20 4d 65 6d 4d 61 78 20 50  Opcode: MemMax P
1eb00 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  1 P2 * * *.**.**
1eb01 20 50 31 20 69 73 20 61 20 72 65 67 69 73 74 65   P1 is a registe
1eb02 72 20 69 6e 20 74 68 65 20 72 6f 6f 74 20 66 72  r in the root fr
1eb03 61 6d 65 20 6f 66 20 74 68 69 73 20 56 4d 20 28  ame of this VM (
1eb04 74 68 65 20 72 6f 6f 74 20 66 72 61 6d 65 20 69  the root frame i
1eb05 73 0a 2a 2a 20 64 69 66 66 65 72 65 6e 74 20 66  s.** different f
1eb06 72 6f 6d 20 74 68 65 20 63 75 72 72 65 6e 74 20  rom the current 
1eb07 66 72 61 6d 65 20 69 66 20 74 68 69 73 20 69 6e  frame if this in
1eb08 73 74 72 75 63 74 69 6f 6e 20 69 73 20 62 65 69  struction is bei
1eb09 6e 67 20 65 78 65 63 75 74 65 64 0a 2a 2a 20 77  ng executed.** w
1eb0a 69 74 68 69 6e 20 61 20 73 75 62 2d 70 72 6f 67  ithin a sub-prog
1eb0b 72 61 6d 29 2e 20 53 65 74 20 74 68 65 20 76 61  ram). Set the va
1eb0c 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65 72 20  lue of register 
1eb0d 50 31 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75  P1 to the maximu
1eb0e 6d 20 6f 66 20 0a 2a 2a 20 69 74 73 20 63 75 72  m of .** its cur
1eb0f 72 65 6e 74 20 76 61 6c 75 65 20 61 6e 64 20 74  rent value and t
1eb10 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
1eb11 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54  ster P2..**.** T
1eb12 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
1eb13 74 68 72 6f 77 73 20 61 6e 20 65 72 72 6f 72 20  throws an error 
1eb14 69 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65  if the memory ce
1eb15 6c 6c 20 69 73 20 6e 6f 74 20 69 6e 69 74 69 61  ll is not initia
1eb16 6c 6c 79 0a 2a 2a 20 61 6e 20 69 6e 74 65 67 65  lly.** an intege
1eb17 72 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 65  r..*/.case OP_Me
1eb18 6d 4d 61 78 3a 20 7b 20 20 20 20 20 20 20 20 2f  mMax: {        /
1eb19 2a 20 69 6e 32 20 2a 2f 0a 23 69 66 20 30 20 20  * in2 */.#if 0  
1eb1a 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1eb1b 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1eb1c 63 62 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e  cb */.  Mem *pIn
1eb1d 31 3b 0a 20 20 56 64 62 65 46 72 61 6d 65 20 2a  1;.  VdbeFrame *
1eb1e 70 46 72 61 6d 65 3b 0a 23 65 6e 64 69 66 20 2f  pFrame;.#endif /
1eb1f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1eb20 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63  s moved into u.c
1eb21 62 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 46  b */.  if( p->pF
1eb22 72 61 6d 65 20 29 7b 0a 20 20 20 20 66 6f 72 28  rame ){.    for(
1eb23 75 2e 63 62 2e 70 46 72 61 6d 65 3d 70 2d 3e 70  u.cb.pFrame=p->p
1eb24 46 72 61 6d 65 3b 20 75 2e 63 62 2e 70 46 72 61  Frame; u.cb.pFra
1eb25 6d 65 2d 3e 70 50 61 72 65 6e 74 3b 20 75 2e 63  me->pParent; u.c
1eb26 62 2e 70 46 72 61 6d 65 3d 75 2e 63 62 2e 70 46  b.pFrame=u.cb.pF
1eb27 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 29 3b 0a  rame->pParent);.
1eb28 20 20 20 20 75 2e 63 62 2e 70 49 6e 31 20 3d 20      u.cb.pIn1 = 
1eb29 26 75 2e 63 62 2e 70 46 72 61 6d 65 2d 3e 61 4d  &u.cb.pFrame->aM
1eb2a 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 7d  em[pOp->p1];.  }
1eb2b 65 6c 73 65 7b 0a 20 20 20 20 75 2e 63 62 2e 70  else{.    u.cb.p
1eb2c 49 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  In1 = &p->aMem[p
1eb2d 4f 70 2d 3e 70 31 5d 3b 0a 20 20 7d 0a 20 20 73  Op->p1];.  }.  s
1eb2e 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74  qlite3VdbeMemInt
1eb2f 65 67 65 72 69 66 79 28 75 2e 63 62 2e 70 49 6e  egerify(u.cb.pIn
1eb30 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  1);.  sqlite3Vdb
1eb31 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70  eMemIntegerify(p
1eb32 49 6e 32 29 3b 0a 20 20 69 66 28 20 75 2e 63 62  In2);.  if( u.cb
1eb33 2e 70 49 6e 31 2d 3e 75 2e 69 3c 70 49 6e 32 2d  .pIn1->u.i<pIn2-
1eb34 3e 75 2e 69 29 7b 0a 20 20 20 20 75 2e 63 62 2e  >u.i){.    u.cb.
1eb35 70 49 6e 31 2d 3e 75 2e 69 20 3d 20 70 49 6e 32  pIn1->u.i = pIn2
1eb36 2d 3e 75 2e 69 3b 0a 20 20 7d 0a 20 20 62 72 65  ->u.i;.  }.  bre
1eb37 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ak;.}.#endif /* 
1eb38 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
1eb39 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 0a 2f 2a  INCREMENT */../*
1eb3a 20 4f 70 63 6f 64 65 3a 20 49 66 50 6f 73 20 50   Opcode: IfPos P
1eb3b 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  1 P2 * * *.**.**
1eb3c 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66   If the value of
1eb3d 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20   register P1 is 
1eb3e 31 20 6f 72 20 67 72 65 61 74 65 72 2c 20 6a 75  1 or greater, ju
1eb3f 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20  mp to P2..**.** 
1eb40 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f  It is illegal to
1eb41 20 75 73 65 20 74 68 69 73 20 69 6e 73 74 72 75   use this instru
1eb42 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65 67 69 73  ction on a regis
1eb43 74 65 72 20 74 68 61 74 20 64 6f 65 73 0a 2a 2a  ter that does.**
1eb44 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 6e 20   not contain an 
1eb45 69 6e 74 65 67 65 72 2e 20 20 41 6e 20 61 73 73  integer.  An ass
1eb46 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 69 6c  ertion fault wil
1eb47 6c 20 72 65 73 75 6c 74 20 69 66 20 79 6f 75 20  l result if you 
1eb48 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  try..*/.case OP_
1eb49 49 66 50 6f 73 3a 20 7b 20 20 20 20 20 20 20 20  IfPos: {        
1eb4a 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a  /* jump, in1 */.
1eb4b 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e    assert( pIn1->
1eb4c 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 3b  flags&MEM_Int );
1eb4d 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 75 2e 69  .  if( pIn1->u.i
1eb4e 3e 30 20 29 7b 0a 20 20 20 20 20 70 63 20 3d 20  >0 ){.     pc = 
1eb4f 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d  pOp->p2 - 1;.  }
1eb50 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1eb51 4f 70 63 6f 64 65 3a 20 49 66 4e 65 67 20 50 31  Opcode: IfNeg P1
1eb52 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1eb53 49 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  If the value of 
1eb54 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20 6c  register P1 is l
1eb55 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 6a  ess than zero, j
1eb56 75 6d 70 20 74 6f 20 50 32 2e 20 0a 2a 2a 0a 2a  ump to P2. .**.*
1eb57 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20  * It is illegal 
1eb58 74 6f 20 75 73 65 20 74 68 69 73 20 69 6e 73 74  to use this inst
1eb59 72 75 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65 67  ruction on a reg
1eb5a 69 73 74 65 72 20 74 68 61 74 20 64 6f 65 73 0a  ister that does.
1eb5b 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61  ** not contain a
1eb5c 6e 20 69 6e 74 65 67 65 72 2e 20 20 41 6e 20 61  n integer.  An a
1eb5d 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 77  ssertion fault w
1eb5e 69 6c 6c 20 72 65 73 75 6c 74 20 69 66 20 79 6f  ill result if yo
1eb5f 75 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f  u try..*/.case O
1eb60 50 5f 49 66 4e 65 67 3a 20 7b 20 20 20 20 20 20  P_IfNeg: {      
1eb61 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a    /* jump, in1 *
1eb62 2f 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 31  /.  assert( pIn1
1eb63 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20  ->flags&MEM_Int 
1eb64 29 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 75  );.  if( pIn1->u
1eb65 2e 69 3c 30 20 29 7b 0a 20 20 20 20 20 70 63 20  .i<0 ){.     pc 
1eb66 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
1eb67 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
1eb68 2a 20 4f 70 63 6f 64 65 3a 20 49 66 5a 65 72 6f  * Opcode: IfZero
1eb69 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a   P1 P2 * * *.**.
1eb6a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20  ** If the value 
1eb6b 6f 66 20 72 65 67 69 73 74 65 72 20 50 31 20 69  of register P1 i
1eb6c 73 20 65 78 61 63 74 6c 79 20 30 2c 20 6a 75 6d  s exactly 0, jum
1eb6d 70 20 74 6f 20 50 32 2e 20 0a 2a 2a 0a 2a 2a 20  p to P2. .**.** 
1eb6e 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f  It is illegal to
1eb6f 20 75 73 65 20 74 68 69 73 20 69 6e 73 74 72 75   use this instru
1eb70 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65 67 69 73  ction on a regis
1eb71 74 65 72 20 74 68 61 74 20 64 6f 65 73 0a 2a 2a  ter that does.**
1eb72 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 6e 20   not contain an 
1eb73 69 6e 74 65 67 65 72 2e 20 20 41 6e 20 61 73 73  integer.  An ass
1eb74 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 69 6c  ertion fault wil
1eb75 6c 20 72 65 73 75 6c 74 20 69 66 20 79 6f 75 20  l result if you 
1eb76 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  try..*/.case OP_
1eb77 49 66 5a 65 72 6f 3a 20 7b 20 20 20 20 20 20 20  IfZero: {       
1eb78 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f   /* jump, in1 */
1eb79 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d  .  assert( pIn1-
1eb7a 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29  >flags&MEM_Int )
1eb7b 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 75 2e  ;.  if( pIn1->u.
1eb7c 69 3d 3d 30 20 29 7b 0a 20 20 20 20 20 70 63 20  i==0 ){.     pc 
1eb7d 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
1eb7e 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
1eb7f 2a 20 4f 70 63 6f 64 65 3a 20 41 67 67 53 74 65  * Opcode: AggSte
1eb80 70 20 2a 20 50 32 20 50 33 20 50 34 20 50 35 0a  p * P2 P3 P4 P5.
1eb81 2a 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 74 68  **.** Execute th
1eb82 65 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20  e step function 
1eb83 66 6f 72 20 61 6e 20 61 67 67 72 65 67 61 74 65  for an aggregate
1eb84 2e 20 20 54 68 65 0a 2a 2a 20 66 75 6e 63 74 69  .  The.** functi
1eb85 6f 6e 20 68 61 73 20 50 35 20 61 72 67 75 6d 65  on has P5 argume
1eb86 6e 74 73 2e 20 20 20 50 34 20 69 73 20 61 20 70  nts.   P4 is a p
1eb87 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 46 75  ointer to the Fu
1eb88 6e 63 44 65 66 0a 2a 2a 20 73 74 72 75 63 74 75  ncDef.** structu
1eb89 72 65 20 74 68 61 74 20 73 70 65 63 69 66 69 65  re that specifie
1eb8a 73 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20  s the function. 
1eb8b 20 55 73 65 20 72 65 67 69 73 74 65 72 0a 2a 2a   Use register.**
1eb8c 20 50 33 20 61 73 20 74 68 65 20 61 63 63 75 6d   P3 as the accum
1eb8d 75 6c 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  ulator..**.** Th
1eb8e 65 20 50 35 20 61 72 67 75 6d 65 6e 74 73 20 61  e P5 arguments a
1eb8f 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 72 65  re taken from re
1eb90 67 69 73 74 65 72 20 50 32 20 61 6e 64 20 69 74  gister P2 and it
1eb91 73 0a 2a 2a 20 73 75 63 63 65 73 73 6f 72 73 2e  s.** successors.
1eb92 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 67 67 53  .*/.case OP_AggS
1eb93 74 65 70 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a  tep: {.#if 0  /*
1eb94 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1eb95 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 63   moved into u.cc
1eb96 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 69   */.  int n;.  i
1eb97 6e 74 20 69 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65  nt i;.  Mem *pMe
1eb98 6d 3b 0a 20 20 4d 65 6d 20 2a 70 52 65 63 3b 0a  m;.  Mem *pRec;.
1eb99 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
1eb9a 74 20 63 74 78 3b 0a 20 20 73 71 6c 69 74 65 33  t ctx;.  sqlite3
1eb9b 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c 3b 0a  _value **apVal;.
1eb9c 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
1eb9d 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1eb9e 69 6e 74 6f 20 75 2e 63 63 20 2a 2f 0a 0a 20 20  into u.cc */..  
1eb9f 75 2e 63 63 2e 6e 20 3d 20 70 4f 70 2d 3e 70 35  u.cc.n = pOp->p5
1eba0 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 63 63  ;.  assert( u.cc
1eba1 2e 6e 3e 3d 30 20 29 3b 0a 20 20 75 2e 63 63 2e  .n>=0 );.  u.cc.
1eba2 70 52 65 63 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  pRec = &p->aMem[
1eba3 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 75 2e 63 63  pOp->p2];.  u.cc
1eba4 2e 61 70 56 61 6c 20 3d 20 70 2d 3e 61 70 41 72  .apVal = p->apAr
1eba5 67 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 63  g;.  assert( u.c
1eba6 63 2e 61 70 56 61 6c 20 7c 7c 20 75 2e 63 63 2e  c.apVal || u.cc.
1eba7 6e 3d 3d 30 20 29 3b 0a 20 20 66 6f 72 28 75 2e  n==0 );.  for(u.
1eba8 63 63 2e 69 3d 30 3b 20 75 2e 63 63 2e 69 3c 75  cc.i=0; u.cc.i<u
1eba9 2e 63 63 2e 6e 3b 20 75 2e 63 63 2e 69 2b 2b 2c  .cc.n; u.cc.i++,
1ebaa 20 75 2e 63 63 2e 70 52 65 63 2b 2b 29 7b 0a 20   u.cc.pRec++){. 
1ebab 20 20 20 75 2e 63 63 2e 61 70 56 61 6c 5b 75 2e     u.cc.apVal[u.
1ebac 63 63 2e 69 5d 20 3d 20 75 2e 63 63 2e 70 52 65  cc.i] = u.cc.pRe
1ebad 63 3b 0a 20 20 20 20 73 74 6f 72 65 54 79 70 65  c;.    storeType
1ebae 49 6e 66 6f 28 75 2e 63 63 2e 70 52 65 63 2c 20  Info(u.cc.pRec, 
1ebaf 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 7d 0a 20  encoding);.  }. 
1ebb0 20 75 2e 63 63 2e 63 74 78 2e 70 46 75 6e 63 20   u.cc.ctx.pFunc 
1ebb1 3d 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b  = pOp->p4.pFunc;
1ebb2 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1ebb3 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c  p3>0 && pOp->p3<
1ebb4 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e  =p->nMem );.  u.
1ebb5 63 63 2e 63 74 78 2e 70 4d 65 6d 20 3d 20 75 2e  cc.ctx.pMem = u.
1ebb6 63 63 2e 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d  cc.pMem = &p->aM
1ebb7 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 75  em[pOp->p3];.  u
1ebb8 2e 63 63 2e 70 4d 65 6d 2d 3e 6e 2b 2b 3b 0a 20  .cc.pMem->n++;. 
1ebb9 20 75 2e 63 63 2e 63 74 78 2e 73 2e 66 6c 61 67   u.cc.ctx.s.flag
1ebba 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20  s = MEM_Null;.  
1ebbb 75 2e 63 63 2e 63 74 78 2e 73 2e 7a 20 3d 20 30  u.cc.ctx.s.z = 0
1ebbc 3b 0a 20 20 75 2e 63 63 2e 63 74 78 2e 73 2e 7a  ;.  u.cc.ctx.s.z
1ebbd 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 75 2e  Malloc = 0;.  u.
1ebbe 63 63 2e 63 74 78 2e 73 2e 78 44 65 6c 20 3d 20  cc.ctx.s.xDel = 
1ebbf 30 3b 0a 20 20 75 2e 63 63 2e 63 74 78 2e 73 2e  0;.  u.cc.ctx.s.
1ebc0 64 62 20 3d 20 64 62 3b 0a 20 20 75 2e 63 63 2e  db = db;.  u.cc.
1ebc1 63 74 78 2e 69 73 45 72 72 6f 72 20 3d 20 30 3b  ctx.isError = 0;
1ebc2 0a 20 20 75 2e 63 63 2e 63 74 78 2e 70 43 6f 6c  .  u.cc.ctx.pCol
1ebc3 6c 20 3d 20 30 3b 0a 20 20 69 66 28 20 75 2e 63  l = 0;.  if( u.c
1ebc4 63 2e 63 74 78 2e 70 46 75 6e 63 2d 3e 66 6c 61  c.ctx.pFunc->fla
1ebc5 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43  gs & SQLITE_FUNC
1ebc6 5f 4e 45 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20  _NEEDCOLL ){.   
1ebc7 20 61 73 73 65 72 74 28 20 70 4f 70 3e 70 2d 3e   assert( pOp>p->
1ebc8 61 4f 70 20 29 3b 0a 20 20 20 20 61 73 73 65 72  aOp );.    asser
1ebc9 74 28 20 70 4f 70 5b 2d 31 5d 2e 70 34 74 79 70  t( pOp[-1].p4typ
1ebca 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 29 3b  e==P4_COLLSEQ );
1ebcb 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70  .    assert( pOp
1ebcc 5b 2d 31 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f  [-1].opcode==OP_
1ebcd 43 6f 6c 6c 53 65 71 20 29 3b 0a 20 20 20 20 75  CollSeq );.    u
1ebce 2e 63 63 2e 63 74 78 2e 70 43 6f 6c 6c 20 3d 20  .cc.ctx.pColl = 
1ebcf 70 4f 70 5b 2d 31 5d 2e 70 34 2e 70 43 6f 6c 6c  pOp[-1].p4.pColl
1ebd0 3b 0a 20 20 7d 0a 20 20 28 75 2e 63 63 2e 63 74  ;.  }.  (u.cc.ct
1ebd1 78 2e 70 46 75 6e 63 2d 3e 78 53 74 65 70 29 28  x.pFunc->xStep)(
1ebd2 26 75 2e 63 63 2e 63 74 78 2c 20 75 2e 63 63 2e  &u.cc.ctx, u.cc.
1ebd3 6e 2c 20 75 2e 63 63 2e 61 70 56 61 6c 29 3b 0a  n, u.cc.apVal);.
1ebd4 20 20 69 66 28 20 75 2e 63 63 2e 63 74 78 2e 69    if( u.cc.ctx.i
1ebd5 73 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 73 71  sError ){.    sq
1ebd6 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26  lite3SetString(&
1ebd7 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20  p->zErrMsg, db, 
1ebd8 22 25 73 22 2c 20 73 71 6c 69 74 65 33 5f 76 61  "%s", sqlite3_va
1ebd9 6c 75 65 5f 74 65 78 74 28 26 75 2e 63 63 2e 63  lue_text(&u.cc.c
1ebda 74 78 2e 73 29 29 3b 0a 20 20 20 20 72 63 20 3d  tx.s));.    rc =
1ebdb 20 75 2e 63 63 2e 63 74 78 2e 69 73 45 72 72 6f   u.cc.ctx.isErro
1ebdc 72 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  r;.  }.  sqlite3
1ebdd 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26  VdbeMemRelease(&
1ebde 75 2e 63 63 2e 63 74 78 2e 73 29 3b 0a 20 20 62  u.cc.ctx.s);.  b
1ebdf 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1ebe0 64 65 3a 20 41 67 67 46 69 6e 61 6c 20 50 31 20  de: AggFinal P1 
1ebe1 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20  P2 * P4 *.**.** 
1ebe2 45 78 65 63 75 74 65 20 74 68 65 20 66 69 6e 61  Execute the fina
1ebe3 6c 69 7a 65 72 20 66 75 6e 63 74 69 6f 6e 20 66  lizer function f
1ebe4 6f 72 20 61 6e 20 61 67 67 72 65 67 61 74 65 2e  or an aggregate.
1ebe5 20 20 50 31 20 69 73 0a 2a 2a 20 74 68 65 20 6d    P1 is.** the m
1ebe6 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74  emory location t
1ebe7 68 61 74 20 69 73 20 74 68 65 20 61 63 63 75 6d  hat is the accum
1ebe8 75 6c 61 74 6f 72 20 66 6f 72 20 74 68 65 20 61  ulator for the a
1ebe9 67 67 72 65 67 61 74 65 2e 0a 2a 2a 0a 2a 2a 20  ggregate..**.** 
1ebea 50 32 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  P2 is the number
1ebeb 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 68   of arguments th
1ebec 61 74 20 74 68 65 20 73 74 65 70 20 66 75 6e 63  at the step func
1ebed 74 69 6f 6e 20 74 61 6b 65 73 20 61 6e 64 0a 2a  tion takes and.*
1ebee 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65  * P4 is a pointe
1ebef 72 20 74 6f 20 74 68 65 20 46 75 6e 63 44 65 66  r to the FuncDef
1ebf0 20 66 6f 72 20 74 68 69 73 20 66 75 6e 63 74 69   for this functi
1ebf1 6f 6e 2e 20 20 54 68 65 20 50 32 0a 2a 2a 20 61  on.  The P2.** a
1ebf2 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 74 20 75  rgument is not u
1ebf3 73 65 64 20 62 79 20 74 68 69 73 20 6f 70 63 6f  sed by this opco
1ebf4 64 65 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79 20  de.  It is only 
1ebf5 74 68 65 72 65 20 74 6f 20 64 69 73 61 6d 62 69  there to disambi
1ebf6 67 75 61 74 65 0a 2a 2a 20 66 75 6e 63 74 69 6f  guate.** functio
1ebf7 6e 73 20 74 68 61 74 20 63 61 6e 20 74 61 6b 65  ns that can take
1ebf8 20 76 61 72 79 69 6e 67 20 6e 75 6d 62 65 72 73   varying numbers
1ebf9 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e 20 20   of arguments.  
1ebfa 54 68 65 0a 2a 2a 20 50 34 20 61 72 67 75 6d 65  The.** P4 argume
1ebfb 6e 74 20 69 73 20 6f 6e 6c 79 20 6e 65 65 64 65  nt is only neede
1ebfc 64 20 66 6f 72 20 74 68 65 20 64 65 67 65 6e 65  d for the degene
1ebfd 72 61 74 65 20 63 61 73 65 20 77 68 65 72 65 0a  rate case where.
1ebfe 2a 2a 20 74 68 65 20 73 74 65 70 20 66 75 6e 63  ** the step func
1ebff 74 69 6f 6e 20 77 61 73 20 6e 6f 74 20 70 72 65  tion was not pre
1ec00 76 69 6f 75 73 6c 79 20 63 61 6c 6c 65 64 2e 0a  viously called..
1ec01 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 67 67 46 69  */.case OP_AggFi
1ec02 6e 61 6c 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a  nal: {.#if 0  /*
1ec03 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1ec04 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 64   moved into u.cd
1ec05 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b   */.  Mem *pMem;
1ec06 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1ec07 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1ec08 20 69 6e 74 6f 20 75 2e 63 64 20 2a 2f 0a 20 20   into u.cd */.  
1ec09 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e  assert( pOp->p1>
1ec0a 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d  0 && pOp->p1<=p-
1ec0b 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e 63 64 2e  >nMem );.  u.cd.
1ec0c 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  pMem = &p->aMem[
1ec0d 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65  pOp->p1];.  asse
1ec0e 72 74 28 20 28 75 2e 63 64 2e 70 4d 65 6d 2d 3e  rt( (u.cd.pMem->
1ec0f 66 6c 61 67 73 20 26 20 7e 28 4d 45 4d 5f 4e 75  flags & ~(MEM_Nu
1ec10 6c 6c 7c 4d 45 4d 5f 41 67 67 29 29 3d 3d 30 20  ll|MEM_Agg))==0 
1ec11 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
1ec12 33 56 64 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65  3VdbeMemFinalize
1ec13 28 75 2e 63 64 2e 70 4d 65 6d 2c 20 70 4f 70 2d  (u.cd.pMem, pOp-
1ec14 3e 70 34 2e 70 46 75 6e 63 29 3b 0a 20 20 69 66  >p4.pFunc);.  if
1ec15 28 20 72 63 20 29 7b 0a 20 20 20 20 73 71 6c 69  ( rc ){.    sqli
1ec16 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d  te3SetString(&p-
1ec17 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25  >zErrMsg, db, "%
1ec18 73 22 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  s", sqlite3_valu
1ec19 65 5f 74 65 78 74 28 75 2e 63 64 2e 70 4d 65 6d  e_text(u.cd.pMem
1ec1a 29 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  ));.  }.  sqlite
1ec1b 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64  3VdbeChangeEncod
1ec1c 69 6e 67 28 75 2e 63 64 2e 70 4d 65 6d 2c 20 65  ing(u.cd.pMem, e
1ec1d 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 55 50 44 41  ncoding);.  UPDA
1ec1e 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28  TE_MAX_BLOBSIZE(
1ec1f 75 2e 63 64 2e 70 4d 65 6d 29 3b 0a 20 20 69 66  u.cd.pMem);.  if
1ec20 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  ( sqlite3VdbeMem
1ec21 54 6f 6f 42 69 67 28 75 2e 63 64 2e 70 4d 65 6d  TooBig(u.cd.pMem
1ec22 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f  ) ){.    goto to
1ec23 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 62 72 65  o_big;.  }.  bre
1ec24 61 6b 3b 0a 7d 0a 0a 0a 23 69 66 20 21 64 65 66  ak;.}...#if !def
1ec25 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
1ec26 5f 56 41 43 55 55 4d 29 20 26 26 20 21 64 65 66  _VACUUM) && !def
1ec27 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
1ec28 5f 41 54 54 41 43 48 29 0a 2f 2a 20 4f 70 63 6f  _ATTACH)./* Opco
1ec29 64 65 3a 20 56 61 63 75 75 6d 20 2a 20 2a 20 2a  de: Vacuum * * *
1ec2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 56 61 63 75 75   * *.**.** Vacuu
1ec2b 6d 20 74 68 65 20 65 6e 74 69 72 65 20 64 61 74  m the entire dat
1ec2c 61 62 61 73 65 2e 20 20 54 68 69 73 20 6f 70 63  abase.  This opc
1ec2d 6f 64 65 20 77 69 6c 6c 20 63 61 75 73 65 20 6f  ode will cause o
1ec2e 74 68 65 72 20 76 69 72 74 75 61 6c 0a 2a 2a 20  ther virtual.** 
1ec2f 6d 61 63 68 69 6e 65 73 20 74 6f 20 62 65 20 63  machines to be c
1ec30 72 65 61 74 65 64 20 61 6e 64 20 72 75 6e 2e 20  reated and run. 
1ec31 20 49 74 20 6d 61 79 20 6e 6f 74 20 62 65 20 63   It may not be c
1ec32 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69  alled from withi
1ec33 6e 0a 2a 2a 20 61 20 74 72 61 6e 73 61 63 74 69  n.** a transacti
1ec34 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56  on..*/.case OP_V
1ec35 61 63 75 75 6d 3a 20 7b 0a 20 20 69 66 28 20 73  acuum: {.  if( s
1ec36 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
1ec37 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74  db) ) goto abort
1ec38 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 20  _due_to_misuse; 
1ec39 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 52  .  rc = sqlite3R
1ec3a 75 6e 56 61 63 75 75 6d 28 26 70 2d 3e 7a 45 72  unVacuum(&p->zEr
1ec3b 72 4d 73 67 2c 20 64 62 29 3b 0a 20 20 69 66 28  rMsg, db);.  if(
1ec3c 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e   sqlite3SafetyOn
1ec3d 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72  (db) ) goto abor
1ec3e 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b  t_due_to_misuse;
1ec3f 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64  .  break;.}.#end
1ec40 69 66 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64  if..#if !defined
1ec41 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54  (SQLITE_OMIT_AUT
1ec42 4f 56 41 43 55 55 4d 29 0a 2f 2a 20 4f 70 63 6f  OVACUUM)./* Opco
1ec43 64 65 3a 20 49 6e 63 72 56 61 63 75 75 6d 20 50  de: IncrVacuum P
1ec44 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  1 P2 * * *.**.**
1ec45 20 50 65 72 66 6f 72 6d 20 61 20 73 69 6e 67 6c   Perform a singl
1ec46 65 20 73 74 65 70 20 6f 66 20 74 68 65 20 69 6e  e step of the in
1ec47 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d  cremental vacuum
1ec48 20 70 72 6f 63 65 64 75 72 65 20 6f 6e 0a 2a 2a   procedure on.**
1ec49 20 74 68 65 20 50 31 20 64 61 74 61 62 61 73 65   the P1 database
1ec4a 2e 20 49 66 20 74 68 65 20 76 61 63 75 75 6d 20  . If the vacuum 
1ec4b 68 61 73 20 66 69 6e 69 73 68 65 64 2c 20 6a 75  has finished, ju
1ec4c 6d 70 20 74 6f 20 69 6e 73 74 72 75 63 74 69 6f  mp to instructio
1ec4d 6e 0a 2a 2a 20 50 32 2e 20 4f 74 68 65 72 77 69  n.** P2. Otherwi
1ec4e 73 65 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 68  se, fall through
1ec4f 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73   to the next ins
1ec50 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73  truction..*/.cas
1ec51 65 20 4f 50 5f 49 6e 63 72 56 61 63 75 75 6d 3a  e OP_IncrVacuum:
1ec52 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d   {        /* jum
1ec53 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c  p */.#if 0  /* l
1ec54 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1ec55 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 65 20 2a  oved into u.ce *
1ec56 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a  /.  Btree *pBt;.
1ec57 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
1ec58 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1ec59 69 6e 74 6f 20 75 2e 63 65 20 2a 2f 0a 0a 20 20  into u.ce */..  
1ec5a 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e  assert( pOp->p1>
1ec5b 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62  =0 && pOp->p1<db
1ec5c 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72  ->nDb );.  asser
1ec5d 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b  t( (p->btreeMask
1ec5e 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29   & (1<<pOp->p1))
1ec5f 21 3d 30 20 29 3b 0a 20 20 75 2e 63 65 2e 70 42  !=0 );.  u.ce.pB
1ec60 74 20 3d 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d  t = db->aDb[pOp-
1ec61 3e 70 31 5d 2e 70 42 74 3b 0a 20 20 72 63 20 3d  >p1].pBt;.  rc =
1ec62 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 63   sqlite3BtreeInc
1ec63 72 56 61 63 75 75 6d 28 75 2e 63 65 2e 70 42 74  rVacuum(u.ce.pBt
1ec64 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
1ec65 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20  ITE_DONE ){.    
1ec66 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31  pc = pOp->p2 - 1
1ec67 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
1ec68 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 62 72 65 61  E_OK;.  }.  brea
1ec69 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20  k;.}.#endif../* 
1ec6a 4f 70 63 6f 64 65 3a 20 45 78 70 69 72 65 20 50  Opcode: Expire P
1ec6b 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  1 * * * *.**.** 
1ec6c 43 61 75 73 65 20 70 72 65 63 6f 6d 70 69 6c 65  Cause precompile
1ec6d 64 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20  d statements to 
1ec6e 62 65 63 6f 6d 65 20 65 78 70 69 72 65 64 2e 20  become expired. 
1ec6f 41 6e 20 65 78 70 69 72 65 64 20 73 74 61 74 65  An expired state
1ec70 6d 65 6e 74 0a 2a 2a 20 66 61 69 6c 73 20 77 69  ment.** fails wi
1ec71 74 68 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  th an error code
1ec72 20 6f 66 20 53 51 4c 49 54 45 5f 53 43 48 45 4d   of SQLITE_SCHEM
1ec73 41 20 69 66 20 69 74 20 69 73 20 65 76 65 72 20  A if it is ever 
1ec74 65 78 65 63 75 74 65 64 20 0a 2a 2a 20 28 76 69  executed .** (vi
1ec75 61 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  a sqlite3_step()
1ec76 29 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 50 31 20  )..** .** If P1 
1ec77 69 73 20 30 2c 20 74 68 65 6e 20 61 6c 6c 20 53  is 0, then all S
1ec78 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 62 65  QL statements be
1ec79 63 6f 6d 65 20 65 78 70 69 72 65 64 2e 20 49 66  come expired. If
1ec7a 20 50 31 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c   P1 is non-zero,
1ec7b 0a 2a 2a 20 74 68 65 6e 20 6f 6e 6c 79 20 74 68  .** then only th
1ec7c 65 20 63 75 72 72 65 6e 74 6c 79 20 65 78 65 63  e currently exec
1ec7d 75 74 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20  uting statement 
1ec7e 69 73 20 61 66 66 65 63 74 65 64 2e 20 0a 2a 2f  is affected. .*/
1ec7f 0a 63 61 73 65 20 4f 50 5f 45 78 70 69 72 65 3a  .case OP_Expire:
1ec80 20 7b 0a 20 20 69 66 28 20 21 70 4f 70 2d 3e 70   {.  if( !pOp->p
1ec81 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  1 ){.    sqlite3
1ec82 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53 74  ExpirePreparedSt
1ec83 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20  atements(db);.  
1ec84 7d 65 6c 73 65 7b 0a 20 20 20 20 70 2d 3e 65 78  }else{.    p->ex
1ec85 70 69 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20  pired = 1;.  }. 
1ec86 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e 64   break;.}..#ifnd
1ec87 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
1ec88 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 20 4f  HARED_CACHE./* O
1ec89 70 63 6f 64 65 3a 20 54 61 62 6c 65 4c 6f 63 6b  pcode: TableLock
1ec8a 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a   P1 P2 P3 P4 *.*
1ec8b 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 61 20 6c 6f  *.** Obtain a lo
1ec8c 63 6b 20 6f 6e 20 61 20 70 61 72 74 69 63 75 6c  ck on a particul
1ec8d 61 72 20 74 61 62 6c 65 2e 20 54 68 69 73 20 69  ar table. This i
1ec8e 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 6f 6e  nstruction is on
1ec8f 6c 79 20 75 73 65 64 20 77 68 65 6e 0a 2a 2a 20  ly used when.** 
1ec90 74 68 65 20 73 68 61 72 65 64 2d 63 61 63 68 65  the shared-cache
1ec91 20 66 65 61 74 75 72 65 20 69 73 20 65 6e 61 62   feature is enab
1ec92 6c 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 50 31 20 69  led. .**.** P1 i
1ec93 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74  s the index of t
1ec94 68 65 20 64 61 74 61 62 61 73 65 20 69 6e 20 73  he database in s
1ec95 71 6c 69 74 65 33 2e 61 44 62 5b 5d 20 6f 66 20  qlite3.aDb[] of 
1ec96 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  the database.** 
1ec97 6f 6e 20 77 68 69 63 68 20 74 68 65 20 6c 6f 63  on which the loc
1ec98 6b 20 69 73 20 61 63 71 75 69 72 65 64 2e 20 20  k is acquired.  
1ec99 41 20 72 65 61 64 6c 6f 63 6b 20 69 73 20 6f 62  A readlock is ob
1ec9a 74 61 69 6e 65 64 20 69 66 20 50 33 3d 3d 30 20  tained if P3==0 
1ec9b 6f 72 0a 2a 2a 20 61 20 77 72 69 74 65 20 6c 6f  or.** a write lo
1ec9c 63 6b 20 69 66 20 50 33 3d 3d 31 2e 0a 2a 2a 0a  ck if P3==1..**.
1ec9d 2a 2a 20 50 32 20 63 6f 6e 74 61 69 6e 73 20 74  ** P2 contains t
1ec9e 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66 20  he root-page of 
1ec9f 74 68 65 20 74 61 62 6c 65 20 74 6f 20 6c 6f 63  the table to loc
1eca0 6b 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 63 6f 6e 74  k..**.** P4 cont
1eca1 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74  ains a pointer t
1eca2 6f 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  o the name of th
1eca3 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 6c 6f  e table being lo
1eca4 63 6b 65 64 2e 20 54 68 69 73 20 69 73 20 6f 6e  cked. This is on
1eca5 6c 79 0a 2a 2a 20 75 73 65 64 20 74 6f 20 67 65  ly.** used to ge
1eca6 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72 20  nerate an error 
1eca7 6d 65 73 73 61 67 65 20 69 66 20 74 68 65 20 6c  message if the l
1eca8 6f 63 6b 20 63 61 6e 6e 6f 74 20 62 65 20 6f 62  ock cannot be ob
1eca9 74 61 69 6e 65 64 2e 0a 2a 2f 0a 63 61 73 65 20  tained..*/.case 
1ecaa 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 3a 20 7b 0a  OP_TableLock: {.
1ecab 20 20 75 38 20 69 73 57 72 69 74 65 4c 6f 63 6b    u8 isWriteLock
1ecac 20 3d 20 28 75 38 29 70 4f 70 2d 3e 70 33 3b 0a   = (u8)pOp->p3;.
1ecad 20 20 69 66 28 20 69 73 57 72 69 74 65 4c 6f 63    if( isWriteLoc
1ecae 6b 20 7c 7c 20 30 3d 3d 28 64 62 2d 3e 66 6c 61  k || 0==(db->fla
1ecaf 67 73 26 53 51 4c 49 54 45 5f 52 65 61 64 55 6e  gs&SQLITE_ReadUn
1ecb0 63 6f 6d 6d 69 74 74 65 64 29 20 29 7b 0a 20 20  committed) ){.  
1ecb1 20 20 69 6e 74 20 70 31 20 3d 20 70 4f 70 2d 3e    int p1 = pOp->
1ecb2 70 31 3b 20 0a 20 20 20 20 61 73 73 65 72 74 28  p1; .    assert(
1ecb3 20 70 31 3e 3d 30 20 26 26 20 70 31 3c 64 62 2d   p1>=0 && p1<db-
1ecb4 3e 6e 44 62 20 29 3b 0a 20 20 20 20 61 73 73 65  >nDb );.    asse
1ecb5 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73  rt( (p->btreeMas
1ecb6 6b 20 26 20 28 31 3c 3c 70 31 29 29 21 3d 30 20  k & (1<<p1))!=0 
1ecb7 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  );.    assert( i
1ecb8 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 30 20 7c 7c  sWriteLock==0 ||
1ecb9 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 31 20   isWriteLock==1 
1ecba 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  );.    rc = sqli
1ecbb 74 65 33 42 74 72 65 65 4c 6f 63 6b 54 61 62 6c  te3BtreeLockTabl
1ecbc 65 28 64 62 2d 3e 61 44 62 5b 70 31 5d 2e 70 42  e(db->aDb[p1].pB
1ecbd 74 2c 20 70 4f 70 2d 3e 70 32 2c 20 69 73 57 72  t, pOp->p2, isWr
1ecbe 69 74 65 4c 6f 63 6b 29 3b 0a 20 20 20 20 69 66  iteLock);.    if
1ecbf 28 20 28 72 63 26 30 78 46 46 29 3d 3d 53 51 4c  ( (rc&0xFF)==SQL
1ecc0 49 54 45 5f 4c 4f 43 4b 45 44 20 29 7b 0a 20 20  ITE_LOCKED ){.  
1ecc1 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
1ecc2 7a 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20  z = pOp->p4.z;. 
1ecc3 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53       sqlite3SetS
1ecc4 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73  tring(&p->zErrMs
1ecc5 67 2c 20 64 62 2c 20 22 64 61 74 61 62 61 73 65  g, db, "database
1ecc6 20 74 61 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64   table is locked
1ecc7 3a 20 25 73 22 2c 20 7a 29 3b 0a 20 20 20 20 7d  : %s", z);.    }
1ecc8 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1ecc9 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1ecca 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
1eccb 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  HE */..#ifndef S
1eccc 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
1eccd 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64  ALTABLE./* Opcod
1ecce 65 3a 20 56 42 65 67 69 6e 20 2a 20 2a 20 2a 20  e: VBegin * * * 
1eccf 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 6d 61  P4 *.**.** P4 ma
1ecd0 79 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74  y be a pointer t
1ecd1 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61  o an sqlite3_vta
1ecd2 62 20 73 74 72 75 63 74 75 72 65 2e 20 49 66 20  b structure. If 
1ecd3 73 6f 2c 20 63 61 6c 6c 20 74 68 65 20 0a 2a 2a  so, call the .**
1ecd4 20 78 42 65 67 69 6e 20 6d 65 74 68 6f 64 20 66   xBegin method f
1ecd5 6f 72 20 74 68 61 74 20 74 61 62 6c 65 2e 0a 2a  or that table..*
1ecd6 2a 0a 2a 2a 20 41 6c 73 6f 2c 20 77 68 65 74 68  *.** Also, wheth
1ecd7 65 72 20 6f 72 20 6e 6f 74 20 50 34 20 69 73 20  er or not P4 is 
1ecd8 73 65 74 2c 20 63 68 65 63 6b 20 74 68 61 74 20  set, check that 
1ecd9 74 68 69 73 20 69 73 20 6e 6f 74 20 62 65 69 6e  this is not bein
1ecda 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d 0a 2a 2a  g called from.**
1ecdb 20 77 69 74 68 69 6e 20 61 20 63 61 6c 6c 62 61   within a callba
1ecdc 63 6b 20 74 6f 20 61 20 76 69 72 74 75 61 6c 20  ck to a virtual 
1ecdd 74 61 62 6c 65 20 78 53 79 6e 63 28 29 20 6d 65  table xSync() me
1ecde 74 68 6f 64 2e 20 49 66 20 69 74 20 69 73 2c 20  thod. If it is, 
1ecdf 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64  the error.** cod
1ece0 65 20 77 69 6c 6c 20 62 65 20 73 65 74 20 74 6f  e will be set to
1ece1 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a   SQLITE_LOCKED..
1ece2 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 42 65 67 69  */.case OP_VBegi
1ece3 6e 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c  n: {.#if 0  /* l
1ece4 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1ece5 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 66 20 2a  oved into u.cf *
1ece6 2f 0a 20 20 56 54 61 62 6c 65 20 2a 70 56 54 61  /.  VTable *pVTa
1ece7 62 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  b;.#endif /* loc
1ece8 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1ece9 65 64 20 69 6e 74 6f 20 75 2e 63 66 20 2a 2f 0a  ed into u.cf */.
1ecea 20 20 75 2e 63 66 2e 70 56 54 61 62 20 3d 20 70    u.cf.pVTab = p
1eceb 4f 70 2d 3e 70 34 2e 70 56 74 61 62 3b 0a 20 20  Op->p4.pVtab;.  
1ecec 72 63 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62  rc = sqlite3Vtab
1eced 42 65 67 69 6e 28 64 62 2c 20 75 2e 63 66 2e 70  Begin(db, u.cf.p
1ecee 56 54 61 62 29 3b 0a 20 20 69 66 28 20 75 2e 63  VTab);.  if( u.c
1ecef 66 2e 70 56 54 61 62 20 29 7b 0a 20 20 20 20 73  f.pVTab ){.    s
1ecf0 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1ecf1 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20   p->zErrMsg);.  
1ecf2 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75    p->zErrMsg = u
1ecf3 2e 63 66 2e 70 56 54 61 62 2d 3e 70 56 74 61 62  .cf.pVTab->pVtab
1ecf4 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 75  ->zErrMsg;.    u
1ecf5 2e 63 66 2e 70 56 54 61 62 2d 3e 70 56 74 61 62  .cf.pVTab->pVtab
1ecf6 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20  ->zErrMsg = 0;. 
1ecf7 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65   }.  break;.}.#e
1ecf8 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
1ecf9 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1ecfa 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
1ecfb 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1ecfc 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a  TABLE./* Opcode:
1ecfd 20 56 43 72 65 61 74 65 20 50 31 20 2a 20 2a 20   VCreate P1 * * 
1ecfe 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73  P4 *.**.** P4 is
1ecff 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 76   the name of a v
1ed00 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 20  irtual table in 
1ed01 64 61 74 61 62 61 73 65 20 50 31 2e 20 43 61 6c  database P1. Cal
1ed02 6c 20 74 68 65 20 78 43 72 65 61 74 65 20 6d 65  l the xCreate me
1ed03 74 68 6f 64 0a 2a 2a 20 66 6f 72 20 74 68 61 74  thod.** for that
1ed04 20 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20   table..*/.case 
1ed05 4f 50 5f 56 43 72 65 61 74 65 3a 20 7b 0a 20 20  OP_VCreate: {.  
1ed06 72 63 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62  rc = sqlite3Vtab
1ed07 43 61 6c 6c 43 72 65 61 74 65 28 64 62 2c 20 70  CallCreate(db, p
1ed08 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e  Op->p1, pOp->p4.
1ed09 7a 2c 20 26 70 2d 3e 7a 45 72 72 4d 73 67 29 3b  z, &p->zErrMsg);
1ed0a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64  .  break;.}.#end
1ed0b 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
1ed0c 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a  T_VIRTUALTABLE *
1ed0d 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
1ed0e 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
1ed0f 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56  BLE./* Opcode: V
1ed10 44 65 73 74 72 6f 79 20 50 31 20 2a 20 2a 20 50  Destroy P1 * * P
1ed11 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20  4 *.**.** P4 is 
1ed12 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 76 69  the name of a vi
1ed13 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 20 64  rtual table in d
1ed14 61 74 61 62 61 73 65 20 50 31 2e 20 20 43 61 6c  atabase P1.  Cal
1ed15 6c 20 74 68 65 20 78 44 65 73 74 72 6f 79 20 6d  l the xDestroy m
1ed16 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 61 74  ethod.** of that
1ed17 20 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20   table..*/.case 
1ed18 4f 50 5f 56 44 65 73 74 72 6f 79 3a 20 7b 0a 20  OP_VDestroy: {. 
1ed19 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64   p->inVtabMethod
1ed1a 20 3d 20 32 3b 0a 20 20 72 63 20 3d 20 73 71 6c   = 2;.  rc = sql
1ed1b 69 74 65 33 56 74 61 62 43 61 6c 6c 44 65 73 74  ite3VtabCallDest
1ed1c 72 6f 79 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c  roy(db, pOp->p1,
1ed1d 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 70   pOp->p4.z);.  p
1ed1e 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d  ->inVtabMethod =
1ed1f 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23   0;.  break;.}.#
1ed20 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
1ed21 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
1ed22 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  E */..#ifndef SQ
1ed23 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
1ed24 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65  LTABLE./* Opcode
1ed25 3a 20 56 4f 70 65 6e 20 50 31 20 2a 20 2a 20 50  : VOpen P1 * * P
1ed26 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20  4 *.**.** P4 is 
1ed27 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 76  a pointer to a v
1ed28 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a  irtual table obj
1ed29 65 63 74 2c 20 61 6e 20 73 71 6c 69 74 65 33 5f  ect, an sqlite3_
1ed2a 76 74 61 62 20 73 74 72 75 63 74 75 72 65 2e 0a  vtab structure..
1ed2b 2a 2a 20 50 31 20 69 73 20 61 20 63 75 72 73 6f  ** P1 is a curso
1ed2c 72 20 6e 75 6d 62 65 72 2e 20 20 54 68 69 73 20  r number.  This 
1ed2d 6f 70 63 6f 64 65 20 6f 70 65 6e 73 20 61 20 63  opcode opens a c
1ed2e 75 72 73 6f 72 20 74 6f 20 74 68 65 20 76 69 72  ursor to the vir
1ed2f 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 61 6e  tual.** table an
1ed30 64 20 73 74 6f 72 65 73 20 74 68 61 74 20 63 75  d stores that cu
1ed31 72 73 6f 72 20 69 6e 20 50 31 2e 0a 2a 2f 0a 63  rsor in P1..*/.c
1ed32 61 73 65 20 4f 50 5f 56 4f 70 65 6e 3a 20 7b 0a  ase OP_VOpen: {.
1ed33 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1ed34 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1ed35 69 6e 74 6f 20 75 2e 63 67 20 2a 2f 0a 20 20 56  into u.cg */.  V
1ed36 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 3b  dbeCursor *pCur;
1ed37 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  .  sqlite3_vtab_
1ed38 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72  cursor *pVtabCur
1ed39 73 6f 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76  sor;.  sqlite3_v
1ed3a 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 73 71  tab *pVtab;.  sq
1ed3b 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d  lite3_module *pM
1ed3c 6f 64 75 6c 65 3b 0a 23 65 6e 64 69 66 20 2f 2a  odule;.#endif /*
1ed3d 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1ed3e 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 67   moved into u.cg
1ed3f 20 2a 2f 0a 0a 20 20 75 2e 63 67 2e 70 43 75 72   */..  u.cg.pCur
1ed40 20 3d 20 30 3b 0a 20 20 75 2e 63 67 2e 70 56 74   = 0;.  u.cg.pVt
1ed41 61 62 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20  abCursor = 0;.  
1ed42 75 2e 63 67 2e 70 56 74 61 62 20 3d 20 70 4f 70  u.cg.pVtab = pOp
1ed43 2d 3e 70 34 2e 70 56 74 61 62 2d 3e 70 56 74 61  ->p4.pVtab->pVta
1ed44 62 3b 0a 20 20 75 2e 63 67 2e 70 4d 6f 64 75 6c  b;.  u.cg.pModul
1ed45 65 20 3d 20 28 73 71 6c 69 74 65 33 5f 6d 6f 64  e = (sqlite3_mod
1ed46 75 6c 65 20 2a 29 75 2e 63 67 2e 70 56 74 61 62  ule *)u.cg.pVtab
1ed47 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 61 73 73  ->pModule;.  ass
1ed48 65 72 74 28 75 2e 63 67 2e 70 56 74 61 62 20 26  ert(u.cg.pVtab &
1ed49 26 20 75 2e 63 67 2e 70 4d 6f 64 75 6c 65 29 3b  & u.cg.pModule);
1ed4a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61  .  if( sqlite3Sa
1ed4b 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f  fetyOff(db) ) go
1ed4c 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1ed4d 6d 69 73 75 73 65 3b 0a 20 20 72 63 20 3d 20 75  misuse;.  rc = u
1ed4e 2e 63 67 2e 70 4d 6f 64 75 6c 65 2d 3e 78 4f 70  .cg.pModule->xOp
1ed4f 65 6e 28 75 2e 63 67 2e 70 56 74 61 62 2c 20 26  en(u.cg.pVtab, &
1ed50 75 2e 63 67 2e 70 56 74 61 62 43 75 72 73 6f 72  u.cg.pVtabCursor
1ed51 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
1ed52 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73  ee(db, p->zErrMs
1ed53 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67  g);.  p->zErrMsg
1ed54 20 3d 20 75 2e 63 67 2e 70 56 74 61 62 2d 3e 7a   = u.cg.pVtab->z
1ed55 45 72 72 4d 73 67 3b 0a 20 20 75 2e 63 67 2e 70  ErrMsg;.  u.cg.p
1ed56 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20  Vtab->zErrMsg = 
1ed57 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  0;.  if( sqlite3
1ed58 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67  SafetyOn(db) ) g
1ed59 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1ed5a 5f 6d 69 73 75 73 65 3b 0a 20 20 69 66 28 20 53  _misuse;.  if( S
1ed5b 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20 29 7b 0a  QLITE_OK==rc ){.
1ed5c 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a      /* Initializ
1ed5d 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  e sqlite3_vtab_c
1ed5e 75 72 73 6f 72 20 62 61 73 65 20 63 6c 61 73 73  ursor base class
1ed5f 20 2a 2f 0a 20 20 20 20 75 2e 63 67 2e 70 56 74   */.    u.cg.pVt
1ed60 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 20  abCursor->pVtab 
1ed61 3d 20 75 2e 63 67 2e 70 56 74 61 62 3b 0a 0a 20  = u.cg.pVtab;.. 
1ed62 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 73 65     /* Initialise
1ed63 20 76 64 62 65 20 63 75 72 73 6f 72 20 6f 62 6a   vdbe cursor obj
1ed64 65 63 74 20 2a 2f 0a 20 20 20 20 75 2e 63 67 2e  ect */.    u.cg.
1ed65 70 43 75 72 20 3d 20 61 6c 6c 6f 63 61 74 65 43  pCur = allocateC
1ed66 75 72 73 6f 72 28 70 2c 20 70 4f 70 2d 3e 70 31  ursor(p, pOp->p1
1ed67 2c 20 30 2c 20 2d 31 2c 20 30 29 3b 0a 20 20 20  , 0, -1, 0);.   
1ed68 20 69 66 28 20 75 2e 63 67 2e 70 43 75 72 20 29   if( u.cg.pCur )
1ed69 7b 0a 20 20 20 20 20 20 75 2e 63 67 2e 70 43 75  {.      u.cg.pCu
1ed6a 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 20 3d  r->pVtabCursor =
1ed6b 20 75 2e 63 67 2e 70 56 74 61 62 43 75 72 73 6f   u.cg.pVtabCurso
1ed6c 72 3b 0a 20 20 20 20 20 20 75 2e 63 67 2e 70 43  r;.      u.cg.pC
1ed6d 75 72 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 75 2e  ur->pModule = u.
1ed6e 63 67 2e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e  cg.pVtabCursor->
1ed6f 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a  pVtab->pModule;.
1ed70 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1ed71 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
1ed72 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 75 2e 63  d = 1;.      u.c
1ed73 67 2e 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73  g.pModule->xClos
1ed74 65 28 75 2e 63 67 2e 70 56 74 61 62 43 75 72 73  e(u.cg.pVtabCurs
1ed75 6f 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  or);.    }.  }. 
1ed76 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66   break;.}.#endif
1ed77 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1ed78 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a  VIRTUALTABLE */.
1ed79 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1ed7a 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
1ed7b 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 46 69  E./* Opcode: VFi
1ed7c 6c 74 65 72 20 50 31 20 50 32 20 50 33 20 50 34  lter P1 P2 P3 P4
1ed7d 20 2a 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61   *.**.** P1 is a
1ed7e 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20 75   cursor opened u
1ed7f 73 69 6e 67 20 56 4f 70 65 6e 2e 20 20 50 32 20  sing VOpen.  P2 
1ed80 69 73 20 61 6e 20 61 64 64 72 65 73 73 20 74 6f  is an address to
1ed81 20 6a 75 6d 70 20 74 6f 20 69 66 0a 2a 2a 20 74   jump to if.** t
1ed82 68 65 20 66 69 6c 74 65 72 65 64 20 72 65 73 75  he filtered resu
1ed83 6c 74 20 73 65 74 20 69 73 20 65 6d 70 74 79 2e  lt set is empty.
1ed84 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 65 69 74  .**.** P4 is eit
1ed85 68 65 72 20 4e 55 4c 4c 20 6f 72 20 61 20 73 74  her NULL or a st
1ed86 72 69 6e 67 20 74 68 61 74 20 77 61 73 20 67 65  ring that was ge
1ed87 6e 65 72 61 74 65 64 20 62 79 20 74 68 65 20 78  nerated by the x
1ed88 42 65 73 74 49 6e 64 65 78 0a 2a 2a 20 6d 65 74  BestIndex.** met
1ed89 68 6f 64 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c  hod of the modul
1ed8a 65 2e 20 20 54 68 65 20 69 6e 74 65 72 70 72 65  e.  The interpre
1ed8b 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 50 34  tation of the P4
1ed8c 20 73 74 72 69 6e 67 20 69 73 20 6c 65 66 74 0a   string is left.
1ed8d 2a 2a 20 74 6f 20 74 68 65 20 6d 6f 64 75 6c 65  ** to the module
1ed8e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e   implementation.
1ed8f 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f  .**.** This opco
1ed90 64 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 78  de invokes the x
1ed91 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 20 6f 6e  Filter method on
1ed92 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
1ed93 6c 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20  le specified.** 
1ed94 62 79 20 50 31 2e 20 20 54 68 65 20 69 6e 74 65  by P1.  The inte
1ed95 67 65 72 20 71 75 65 72 79 20 70 6c 61 6e 20 70  ger query plan p
1ed96 61 72 61 6d 65 74 65 72 20 74 6f 20 78 46 69 6c  arameter to xFil
1ed97 74 65 72 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ter is stored in
1ed98 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 50 33 2e   register.** P3.
1ed99 20 52 65 67 69 73 74 65 72 20 50 33 2b 31 20 73   Register P3+1 s
1ed9a 74 6f 72 65 73 20 74 68 65 20 61 72 67 63 20 70  tores the argc p
1ed9b 61 72 61 6d 65 74 65 72 20 74 6f 20 62 65 20 70  arameter to be p
1ed9c 61 73 73 65 64 20 74 6f 20 74 68 65 0a 2a 2a 20  assed to the.** 
1ed9d 78 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 2e 20  xFilter method. 
1ed9e 52 65 67 69 73 74 65 72 73 20 50 33 2b 32 2e 2e  Registers P3+2..
1ed9f 50 33 2b 31 2b 61 72 67 63 20 61 72 65 20 74 68  P3+1+argc are th
1eda0 65 20 61 72 67 63 0a 2a 2a 20 61 64 64 69 74 69  e argc.** additi
1eda1 6f 6e 61 6c 20 70 61 72 61 6d 65 74 65 72 73 20  onal parameters 
1eda2 77 68 69 63 68 20 61 72 65 20 70 61 73 73 65 64  which are passed
1eda3 20 74 6f 0a 2a 2a 20 78 46 69 6c 74 65 72 20 61   to.** xFilter a
1eda4 73 20 61 72 67 76 2e 20 52 65 67 69 73 74 65 72  s argv. Register
1eda5 20 50 33 2b 32 20 62 65 63 6f 6d 65 73 20 61 72   P3+2 becomes ar
1eda6 67 76 5b 30 5d 20 77 68 65 6e 20 70 61 73 73 65  gv[0] when passe
1eda7 64 20 74 6f 20 78 46 69 6c 74 65 72 2e 0a 2a 2a  d to xFilter..**
1eda8 0a 2a 2a 20 41 20 6a 75 6d 70 20 69 73 20 6d 61  .** A jump is ma
1eda9 64 65 20 74 6f 20 50 32 20 69 66 20 74 68 65 20  de to P2 if the 
1edaa 72 65 73 75 6c 74 20 73 65 74 20 61 66 74 65 72  result set after
1edab 20 66 69 6c 74 65 72 69 6e 67 20 77 6f 75 6c 64   filtering would
1edac 20 62 65 20 65 6d 70 74 79 2e 0a 2a 2f 0a 63 61   be empty..*/.ca
1edad 73 65 20 4f 50 5f 56 46 69 6c 74 65 72 3a 20 7b  se OP_VFilter: {
1edae 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69     /* jump */.#i
1edaf 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1edb0 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1edb1 74 6f 20 75 2e 63 68 20 2a 2f 0a 20 20 69 6e 74  to u.ch */.  int
1edb2 20 6e 41 72 67 3b 0a 20 20 69 6e 74 20 69 51 75   nArg;.  int iQu
1edb3 65 72 79 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c  ery;.  const sql
1edb4 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f  ite3_module *pMo
1edb5 64 75 6c 65 3b 0a 20 20 4d 65 6d 20 2a 70 51 75  dule;.  Mem *pQu
1edb6 65 72 79 3b 0a 20 20 4d 65 6d 20 2a 70 41 72 67  ery;.  Mem *pArg
1edb7 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61  c;.  sqlite3_vta
1edb8 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43  b_cursor *pVtabC
1edb9 75 72 73 6f 72 3b 0a 20 20 73 71 6c 69 74 65 33  ursor;.  sqlite3
1edba 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20  _vtab *pVtab;.  
1edbb 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72  VdbeCursor *pCur
1edbc 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 69  ;.  int res;.  i
1edbd 6e 74 20 69 3b 0a 20 20 4d 65 6d 20 2a 2a 61 70  nt i;.  Mem **ap
1edbe 41 72 67 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  Arg;.#endif /* l
1edbf 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1edc0 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 68 20 2a  oved into u.ch *
1edc1 2f 0a 0a 20 20 75 2e 63 68 2e 70 51 75 65 72 79  /..  u.ch.pQuery
1edc2 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d   = &p->aMem[pOp-
1edc3 3e 70 33 5d 3b 0a 20 20 75 2e 63 68 2e 70 41 72  >p3];.  u.ch.pAr
1edc4 67 63 20 3d 20 26 75 2e 63 68 2e 70 51 75 65 72  gc = &u.ch.pQuer
1edc5 79 5b 31 5d 3b 0a 20 20 75 2e 63 68 2e 70 43 75  y[1];.  u.ch.pCu
1edc6 72 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70  r = p->apCsr[pOp
1edc7 2d 3e 70 31 5d 3b 0a 20 20 52 45 47 49 53 54 45  ->p1];.  REGISTE
1edc8 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c  R_TRACE(pOp->p3,
1edc9 20 75 2e 63 68 2e 70 51 75 65 72 79 29 3b 0a 20   u.ch.pQuery);. 
1edca 20 61 73 73 65 72 74 28 20 75 2e 63 68 2e 70 43   assert( u.ch.pC
1edcb 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 20  ur->pVtabCursor 
1edcc 29 3b 0a 20 20 75 2e 63 68 2e 70 56 74 61 62 43  );.  u.ch.pVtabC
1edcd 75 72 73 6f 72 20 3d 20 75 2e 63 68 2e 70 43 75  ursor = u.ch.pCu
1edce 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 3b 0a  r->pVtabCursor;.
1edcf 20 20 75 2e 63 68 2e 70 56 74 61 62 20 3d 20 75    u.ch.pVtab = u
1edd0 2e 63 68 2e 70 56 74 61 62 43 75 72 73 6f 72 2d  .ch.pVtabCursor-
1edd1 3e 70 56 74 61 62 3b 0a 20 20 75 2e 63 68 2e 70  >pVtab;.  u.ch.p
1edd2 4d 6f 64 75 6c 65 20 3d 20 75 2e 63 68 2e 70 56  Module = u.ch.pV
1edd3 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 0a 20  tab->pModule;.. 
1edd4 20 2f 2a 20 47 72 61 62 20 74 68 65 20 69 6e 64   /* Grab the ind
1edd5 65 78 20 6e 75 6d 62 65 72 20 61 6e 64 20 61 72  ex number and ar
1edd6 67 63 20 70 61 72 61 6d 65 74 65 72 73 20 2a 2f  gc parameters */
1edd7 0a 20 20 61 73 73 65 72 74 28 20 28 75 2e 63 68  .  assert( (u.ch
1edd8 2e 70 51 75 65 72 79 2d 3e 66 6c 61 67 73 26 4d  .pQuery->flags&M
1edd9 45 4d 5f 49 6e 74 29 21 3d 30 20 26 26 20 75 2e  EM_Int)!=0 && u.
1edda 63 68 2e 70 41 72 67 63 2d 3e 66 6c 61 67 73 3d  ch.pArgc->flags=
1eddb 3d 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 75 2e  =MEM_Int );.  u.
1eddc 63 68 2e 6e 41 72 67 20 3d 20 28 69 6e 74 29 75  ch.nArg = (int)u
1eddd 2e 63 68 2e 70 41 72 67 63 2d 3e 75 2e 69 3b 0a  .ch.pArgc->u.i;.
1edde 20 20 75 2e 63 68 2e 69 51 75 65 72 79 20 3d 20    u.ch.iQuery = 
1eddf 28 69 6e 74 29 75 2e 63 68 2e 70 51 75 65 72 79  (int)u.ch.pQuery
1ede0 2d 3e 75 2e 69 3b 0a 0a 20 20 2f 2a 20 49 6e 76  ->u.i;..  /* Inv
1ede1 6f 6b 65 20 74 68 65 20 78 46 69 6c 74 65 72 20  oke the xFilter 
1ede2 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 7b 0a 20 20  method */.  {.  
1ede3 20 20 75 2e 63 68 2e 72 65 73 20 3d 20 30 3b 0a    u.ch.res = 0;.
1ede4 20 20 20 20 75 2e 63 68 2e 61 70 41 72 67 20 3d      u.ch.apArg =
1ede5 20 70 2d 3e 61 70 41 72 67 3b 0a 20 20 20 20 66   p->apArg;.    f
1ede6 6f 72 28 75 2e 63 68 2e 69 20 3d 20 30 3b 20 75  or(u.ch.i = 0; u
1ede7 2e 63 68 2e 69 3c 75 2e 63 68 2e 6e 41 72 67 3b  .ch.i<u.ch.nArg;
1ede8 20 75 2e 63 68 2e 69 2b 2b 29 7b 0a 20 20 20 20   u.ch.i++){.    
1ede9 20 20 75 2e 63 68 2e 61 70 41 72 67 5b 75 2e 63    u.ch.apArg[u.c
1edea 68 2e 69 5d 20 3d 20 26 75 2e 63 68 2e 70 41 72  h.i] = &u.ch.pAr
1edeb 67 63 5b 75 2e 63 68 2e 69 2b 31 5d 3b 0a 20 20  gc[u.ch.i+1];.  
1edec 20 20 20 20 73 74 6f 72 65 54 79 70 65 49 6e 66      storeTypeInf
1eded 6f 28 75 2e 63 68 2e 61 70 41 72 67 5b 75 2e 63  o(u.ch.apArg[u.c
1edee 68 2e 69 5d 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  h.i], 0);.    }.
1edef 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1edf0 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20  SafetyOff(db) ) 
1edf1 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1edf2 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 70 2d  o_misuse;.    p-
1edf3 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20  >inVtabMethod = 
1edf4 31 3b 0a 20 20 20 20 72 63 20 3d 20 75 2e 63 68  1;.    rc = u.ch
1edf5 2e 70 4d 6f 64 75 6c 65 2d 3e 78 46 69 6c 74 65  .pModule->xFilte
1edf6 72 28 75 2e 63 68 2e 70 56 74 61 62 43 75 72 73  r(u.ch.pVtabCurs
1edf7 6f 72 2c 20 75 2e 63 68 2e 69 51 75 65 72 79 2c  or, u.ch.iQuery,
1edf8 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 75 2e 63 68   pOp->p4.z, u.ch
1edf9 2e 6e 41 72 67 2c 20 75 2e 63 68 2e 61 70 41 72  .nArg, u.ch.apAr
1edfa 67 29 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61  g);.    p->inVta
1edfb 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 20  bMethod = 0;.   
1edfc 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1edfd 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a  b, p->zErrMsg);.
1edfe 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d      p->zErrMsg =
1edff 20 75 2e 63 68 2e 70 56 74 61 62 2d 3e 7a 45 72   u.ch.pVtab->zEr
1ee00 72 4d 73 67 3b 0a 20 20 20 20 75 2e 63 68 2e 70  rMsg;.    u.ch.p
1ee01 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20  Vtab->zErrMsg = 
1ee02 30 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  0;.    if( rc==S
1ee03 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1ee04 20 20 75 2e 63 68 2e 72 65 73 20 3d 20 75 2e 63    u.ch.res = u.c
1ee05 68 2e 70 4d 6f 64 75 6c 65 2d 3e 78 45 6f 66 28  h.pModule->xEof(
1ee06 75 2e 63 68 2e 70 56 74 61 62 43 75 72 73 6f 72  u.ch.pVtabCursor
1ee07 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
1ee08 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e   sqlite3SafetyOn
1ee09 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72  (db) ) goto abor
1ee0a 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b  t_due_to_misuse;
1ee0b 0a 0a 20 20 20 20 69 66 28 20 75 2e 63 68 2e 72  ..    if( u.ch.r
1ee0c 65 73 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d  es ){.      pc =
1ee0d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20   pOp->p2 - 1;.  
1ee0e 20 20 7d 0a 20 20 7d 0a 20 20 75 2e 63 68 2e 70    }.  }.  u.ch.p
1ee0f 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30  Cur->nullRow = 0
1ee10 3b 0a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65  ;..  break;.}.#e
1ee11 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
1ee12 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1ee13 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
1ee14 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1ee15 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a  TABLE./* Opcode:
1ee16 20 56 43 6f 6c 75 6d 6e 20 50 31 20 50 32 20 50   VColumn P1 P2 P
1ee17 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f 72  3 * *.**.** Stor
1ee18 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  e the value of t
1ee19 68 65 20 50 32 2d 74 68 20 63 6f 6c 75 6d 6e 20  he P2-th column 
1ee1a 6f 66 0a 2a 2a 20 74 68 65 20 72 6f 77 20 6f 66  of.** the row of
1ee1b 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62   the virtual-tab
1ee1c 6c 65 20 74 68 61 74 20 74 68 65 20 0a 2a 2a 20  le that the .** 
1ee1d 50 31 20 63 75 72 73 6f 72 20 69 73 20 70 6f 69  P1 cursor is poi
1ee1e 6e 74 69 6e 67 20 74 6f 20 69 6e 74 6f 20 72 65  nting to into re
1ee1f 67 69 73 74 65 72 20 50 33 2e 0a 2a 2f 0a 63 61  gister P3..*/.ca
1ee20 73 65 20 4f 50 5f 56 43 6f 6c 75 6d 6e 3a 20 7b  se OP_VColumn: {
1ee21 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1ee22 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1ee23 20 69 6e 74 6f 20 75 2e 63 69 20 2a 2f 0a 20 20   into u.ci */.  
1ee24 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
1ee25 74 61 62 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c  tab;.  const sql
1ee26 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f  ite3_module *pMo
1ee27 64 75 6c 65 3b 0a 20 20 4d 65 6d 20 2a 70 44 65  dule;.  Mem *pDe
1ee28 73 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  st;.  sqlite3_co
1ee29 6e 74 65 78 74 20 73 43 6f 6e 74 65 78 74 3b 0a  ntext sContext;.
1ee2a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
1ee2b 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1ee2c 69 6e 74 6f 20 75 2e 63 69 20 2a 2f 0a 0a 20 20  into u.ci */..  
1ee2d 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72  VdbeCursor *pCur
1ee2e 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d   = p->apCsr[pOp-
1ee2f 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  >p1];.  assert( 
1ee30 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f  pCur->pVtabCurso
1ee31 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  r );.  assert( p
1ee32 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d  Op->p3>0 && pOp-
1ee33 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a  >p3<=p->nMem );.
1ee34 20 20 75 2e 63 69 2e 70 44 65 73 74 20 3d 20 26    u.ci.pDest = &
1ee35 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d  p->aMem[pOp->p3]
1ee36 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 6e 75  ;.  if( pCur->nu
1ee37 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 73 71 6c  llRow ){.    sql
1ee38 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75  ite3VdbeMemSetNu
1ee39 6c 6c 28 75 2e 63 69 2e 70 44 65 73 74 29 3b 0a  ll(u.ci.pDest);.
1ee3a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20      break;.  }. 
1ee3b 20 75 2e 63 69 2e 70 56 74 61 62 20 3d 20 70 43   u.ci.pVtab = pC
1ee3c 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d  ur->pVtabCursor-
1ee3d 3e 70 56 74 61 62 3b 0a 20 20 75 2e 63 69 2e 70  >pVtab;.  u.ci.p
1ee3e 4d 6f 64 75 6c 65 20 3d 20 75 2e 63 69 2e 70 56  Module = u.ci.pV
1ee3f 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20  tab->pModule;.  
1ee40 61 73 73 65 72 74 28 20 75 2e 63 69 2e 70 4d 6f  assert( u.ci.pMo
1ee41 64 75 6c 65 2d 3e 78 43 6f 6c 75 6d 6e 20 29 3b  dule->xColumn );
1ee42 0a 20 20 6d 65 6d 73 65 74 28 26 75 2e 63 69 2e  .  memset(&u.ci.
1ee43 73 43 6f 6e 74 65 78 74 2c 20 30 2c 20 73 69 7a  sContext, 0, siz
1ee44 65 6f 66 28 75 2e 63 69 2e 73 43 6f 6e 74 65 78  eof(u.ci.sContex
1ee45 74 29 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6f  t));..  /* The o
1ee46 75 74 70 75 74 20 63 65 6c 6c 20 6d 61 79 20 61  utput cell may a
1ee47 6c 72 65 61 64 79 20 68 61 76 65 20 61 20 62 75  lready have a bu
1ee48 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 2e 20  ffer allocated. 
1ee49 4d 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20 63 75  Move.  ** the cu
1ee4a 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73 20 74  rrent contents t
1ee4b 6f 20 75 2e 63 69 2e 73 43 6f 6e 74 65 78 74 2e  o u.ci.sContext.
1ee4c 73 20 73 6f 20 69 6e 20 63 61 73 65 20 74 68 65  s so in case the
1ee4d 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 0a 20   user-function. 
1ee4e 20 2a 2a 20 63 61 6e 20 75 73 65 20 74 68 65 20   ** can use the 
1ee4f 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63 61 74 65  already allocate
1ee50 64 20 62 75 66 66 65 72 20 69 6e 73 74 65 61 64  d buffer instead
1ee51 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6e 67 20 61   of allocating a
1ee52 0a 20 20 2a 2a 20 6e 65 77 20 6f 6e 65 2e 0a 20  .  ** new one.. 
1ee53 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62   */.  sqlite3Vdb
1ee54 65 4d 65 6d 4d 6f 76 65 28 26 75 2e 63 69 2e 73  eMemMove(&u.ci.s
1ee55 43 6f 6e 74 65 78 74 2e 73 2c 20 75 2e 63 69 2e  Context.s, u.ci.
1ee56 70 44 65 73 74 29 3b 0a 20 20 4d 65 6d 53 65 74  pDest);.  MemSet
1ee57 54 79 70 65 46 6c 61 67 28 26 75 2e 63 69 2e 73  TypeFlag(&u.ci.s
1ee58 43 6f 6e 74 65 78 74 2e 73 2c 20 4d 45 4d 5f 4e  Context.s, MEM_N
1ee59 75 6c 6c 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c  ull);..  if( sql
1ee5a 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62  ite3SafetyOff(db
1ee5b 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64  ) ) goto abort_d
1ee5c 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20  ue_to_misuse;.  
1ee5d 72 63 20 3d 20 75 2e 63 69 2e 70 4d 6f 64 75 6c  rc = u.ci.pModul
1ee5e 65 2d 3e 78 43 6f 6c 75 6d 6e 28 70 43 75 72 2d  e->xColumn(pCur-
1ee5f 3e 70 56 74 61 62 43 75 72 73 6f 72 2c 20 26 75  >pVtabCursor, &u
1ee60 2e 63 69 2e 73 43 6f 6e 74 65 78 74 2c 20 70 4f  .ci.sContext, pO
1ee61 70 2d 3e 70 32 29 3b 0a 20 20 73 71 6c 69 74 65  p->p2);.  sqlite
1ee62 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a  3DbFree(db, p->z
1ee63 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45  ErrMsg);.  p->zE
1ee64 72 72 4d 73 67 20 3d 20 75 2e 63 69 2e 70 56 74  rrMsg = u.ci.pVt
1ee65 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 75  ab->zErrMsg;.  u
1ee66 2e 63 69 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d  .ci.pVtab->zErrM
1ee67 73 67 20 3d 20 30 3b 0a 20 20 69 66 28 20 75 2e  sg = 0;.  if( u.
1ee68 63 69 2e 73 43 6f 6e 74 65 78 74 2e 69 73 45 72  ci.sContext.isEr
1ee69 72 6f 72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ror ){.    rc = 
1ee6a 75 2e 63 69 2e 73 43 6f 6e 74 65 78 74 2e 69 73  u.ci.sContext.is
1ee6b 45 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  Error;.  }..  /*
1ee6c 20 43 6f 70 79 20 74 68 65 20 72 65 73 75 6c 74   Copy the result
1ee6d 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   of the function
1ee6e 20 74 6f 20 74 68 65 20 50 33 20 72 65 67 69 73   to the P3 regis
1ee6f 74 65 72 2e 20 57 65 0a 20 20 2a 2a 20 64 6f 20  ter. We.  ** do 
1ee70 74 68 69 73 20 72 65 67 61 72 64 6c 65 73 73 20  this regardless 
1ee71 6f 66 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  of whether or no
1ee72 74 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  t an error occur
1ee73 72 65 64 20 74 6f 20 65 6e 73 75 72 65 20 61 6e  red to ensure an
1ee74 79 0a 20 20 2a 2a 20 64 79 6e 61 6d 69 63 20 61  y.  ** dynamic a
1ee75 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 75 2e 63  llocation in u.c
1ee76 69 2e 73 43 6f 6e 74 65 78 74 2e 73 20 28 61 20  i.sContext.s (a 
1ee77 4d 65 6d 20 73 74 72 75 63 74 29 20 69 73 20 20  Mem struct) is  
1ee78 72 65 6c 65 61 73 65 64 2e 0a 20 20 2a 2f 0a 20  released..  */. 
1ee79 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
1ee7a 67 65 45 6e 63 6f 64 69 6e 67 28 26 75 2e 63 69  geEncoding(&u.ci
1ee7b 2e 73 43 6f 6e 74 65 78 74 2e 73 2c 20 65 6e 63  .sContext.s, enc
1ee7c 6f 64 69 6e 67 29 3b 0a 20 20 52 45 47 49 53 54  oding);.  REGIST
1ee7d 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33  ER_TRACE(pOp->p3
1ee7e 2c 20 75 2e 63 69 2e 70 44 65 73 74 29 3b 0a 20  , u.ci.pDest);. 
1ee7f 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d   sqlite3VdbeMemM
1ee80 6f 76 65 28 75 2e 63 69 2e 70 44 65 73 74 2c 20  ove(u.ci.pDest, 
1ee81 26 75 2e 63 69 2e 73 43 6f 6e 74 65 78 74 2e 73  &u.ci.sContext.s
1ee82 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f  );.  UPDATE_MAX_
1ee83 42 4c 4f 42 53 49 5a 45 28 75 2e 63 69 2e 70 44  BLOBSIZE(u.ci.pD
1ee84 65 73 74 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c  est);..  if( sql
1ee85 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29  ite3SafetyOn(db)
1ee86 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 61 62 6f   ){.    goto abo
1ee87 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65  rt_due_to_misuse
1ee88 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69  ;.  }.  if( sqli
1ee89 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67  te3VdbeMemTooBig
1ee8a 28 75 2e 63 69 2e 70 44 65 73 74 29 20 29 7b 0a  (u.ci.pDest) ){.
1ee8b 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67      goto too_big
1ee8c 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1ee8d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1ee8e 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
1ee8f 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  BLE */..#ifndef 
1ee90 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
1ee91 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f  UALTABLE./* Opco
1ee92 64 65 3a 20 56 4e 65 78 74 20 50 31 20 50 32 20  de: VNext P1 P2 
1ee93 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 64 76 61  * * *.**.** Adva
1ee94 6e 63 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  nce virtual tabl
1ee95 65 20 50 31 20 74 6f 20 74 68 65 20 6e 65 78 74  e P1 to the next
1ee96 20 72 6f 77 20 69 6e 20 69 74 73 20 72 65 73 75   row in its resu
1ee97 6c 74 20 73 65 74 20 61 6e 64 0a 2a 2a 20 6a 75  lt set and.** ju
1ee98 6d 70 20 74 6f 20 69 6e 73 74 72 75 63 74 69 6f  mp to instructio
1ee99 6e 20 50 32 2e 20 20 4f 72 2c 20 69 66 20 74 68  n P2.  Or, if th
1ee9a 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
1ee9b 68 61 73 20 72 65 61 63 68 65 64 0a 2a 2a 20 74  has reached.** t
1ee9c 68 65 20 65 6e 64 20 6f 66 20 69 74 73 20 72 65  he end of its re
1ee9d 73 75 6c 74 20 73 65 74 2c 20 74 68 65 6e 20 66  sult set, then f
1ee9e 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74  all through to t
1ee9f 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74  he next instruct
1eea0 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ion..*/.case OP_
1eea1 56 4e 65 78 74 3a 20 7b 20 20 20 2f 2a 20 6a 75  VNext: {   /* ju
1eea2 6d 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20  mp */.#if 0  /* 
1eea3 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1eea4 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6a 20  moved into u.cj 
1eea5 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61  */.  sqlite3_vta
1eea6 62 20 2a 70 56 74 61 62 3b 0a 20 20 63 6f 6e 73  b *pVtab;.  cons
1eea7 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  t sqlite3_module
1eea8 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e 74   *pModule;.  int
1eea9 20 72 65 73 3b 0a 20 20 56 64 62 65 43 75 72 73   res;.  VdbeCurs
1eeaa 6f 72 20 2a 70 43 75 72 3b 0a 23 65 6e 64 69 66  or *pCur;.#endif
1eeab 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1eeac 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1eead 2e 63 6a 20 2a 2f 0a 0a 20 20 75 2e 63 6a 2e 72  .cj */..  u.cj.r
1eeae 65 73 20 3d 20 30 3b 0a 20 20 75 2e 63 6a 2e 70  es = 0;.  u.cj.p
1eeaf 43 75 72 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70  Cur = p->apCsr[p
1eeb0 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72  Op->p1];.  asser
1eeb1 74 28 20 75 2e 63 6a 2e 70 43 75 72 2d 3e 70 56  t( u.cj.pCur->pV
1eeb2 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 20 69  tabCursor );.  i
1eeb3 66 28 20 75 2e 63 6a 2e 70 43 75 72 2d 3e 6e 75  f( u.cj.pCur->nu
1eeb4 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 62 72 65  llRow ){.    bre
1eeb5 61 6b 3b 0a 20 20 7d 0a 20 20 75 2e 63 6a 2e 70  ak;.  }.  u.cj.p
1eeb6 56 74 61 62 20 3d 20 75 2e 63 6a 2e 70 43 75 72  Vtab = u.cj.pCur
1eeb7 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70  ->pVtabCursor->p
1eeb8 56 74 61 62 3b 0a 20 20 75 2e 63 6a 2e 70 4d 6f  Vtab;.  u.cj.pMo
1eeb9 64 75 6c 65 20 3d 20 75 2e 63 6a 2e 70 56 74 61  dule = u.cj.pVta
1eeba 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 61 73  b->pModule;.  as
1eebb 73 65 72 74 28 20 75 2e 63 6a 2e 70 4d 6f 64 75  sert( u.cj.pModu
1eebc 6c 65 2d 3e 78 4e 65 78 74 20 29 3b 0a 0a 20 20  le->xNext );..  
1eebd 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 78 4e  /* Invoke the xN
1eebe 65 78 74 28 29 20 6d 65 74 68 6f 64 20 6f 66 20  ext() method of 
1eebf 74 68 65 20 6d 6f 64 75 6c 65 2e 20 54 68 65 72  the module. Ther
1eec0 65 20 69 73 20 6e 6f 20 77 61 79 20 66 6f 72 20  e is no way for 
1eec1 74 68 65 0a 20 20 2a 2a 20 75 6e 64 65 72 6c 79  the.  ** underly
1eec2 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ing implementati
1eec3 6f 6e 20 74 6f 20 72 65 74 75 72 6e 20 61 6e 20  on to return an 
1eec4 65 72 72 6f 72 20 69 66 20 6f 6e 65 20 6f 63 63  error if one occ
1eec5 75 72 73 20 64 75 72 69 6e 67 0a 20 20 2a 2a 20  urs during.  ** 
1eec6 78 4e 65 78 74 28 29 2e 20 49 6e 73 74 65 61 64  xNext(). Instead
1eec7 2c 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  , if an error oc
1eec8 63 75 72 73 2c 20 74 72 75 65 20 69 73 20 72 65  curs, true is re
1eec9 74 75 72 6e 65 64 20 28 69 6e 64 69 63 61 74 69  turned (indicati
1eeca 6e 67 20 74 68 61 74 0a 20 20 2a 2a 20 64 61 74  ng that.  ** dat
1eecb 61 20 69 73 20 61 76 61 69 6c 61 62 6c 65 29 20  a is available) 
1eecc 61 6e 64 20 74 68 65 20 65 72 72 6f 72 20 63 6f  and the error co
1eecd 64 65 20 72 65 74 75 72 6e 65 64 20 77 68 65 6e  de returned when
1eece 20 78 43 6f 6c 75 6d 6e 20 6f 72 0a 20 20 2a 2a   xColumn or.  **
1eecf 20 73 6f 6d 65 20 6f 74 68 65 72 20 6d 65 74 68   some other meth
1eed0 6f 64 20 69 73 20 6e 65 78 74 20 69 6e 76 6f 6b  od is next invok
1eed1 65 64 20 6f 6e 20 74 68 65 20 73 61 76 65 20 76  ed on the save v
1eed2 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63 75 72  irtual table cur
1eed3 73 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  sor..  */.  if( 
1eed4 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
1eed5 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72  (db) ) goto abor
1eed6 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b  t_due_to_misuse;
1eed7 0a 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68  .  p->inVtabMeth
1eed8 6f 64 20 3d 20 31 3b 0a 20 20 72 63 20 3d 20 75  od = 1;.  rc = u
1eed9 2e 63 6a 2e 70 4d 6f 64 75 6c 65 2d 3e 78 4e 65  .cj.pModule->xNe
1eeda 78 74 28 75 2e 63 6a 2e 70 43 75 72 2d 3e 70 56  xt(u.cj.pCur->pV
1eedb 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 70 2d  tabCursor);.  p-
1eedc 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20  >inVtabMethod = 
1eedd 30 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  0;.  sqlite3DbFr
1eede 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73  ee(db, p->zErrMs
1eedf 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67  g);.  p->zErrMsg
1eee0 20 3d 20 75 2e 63 6a 2e 70 56 74 61 62 2d 3e 7a   = u.cj.pVtab->z
1eee1 45 72 72 4d 73 67 3b 0a 20 20 75 2e 63 6a 2e 70  ErrMsg;.  u.cj.p
1eee2 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20  Vtab->zErrMsg = 
1eee3 30 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  0;.  if( rc==SQL
1eee4 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 2e  ITE_OK ){.    u.
1eee5 63 6a 2e 72 65 73 20 3d 20 75 2e 63 6a 2e 70 4d  cj.res = u.cj.pM
1eee6 6f 64 75 6c 65 2d 3e 78 45 6f 66 28 75 2e 63 6a  odule->xEof(u.cj
1eee7 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73  .pCur->pVtabCurs
1eee8 6f 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73  or);.  }.  if( s
1eee9 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64  qlite3SafetyOn(d
1eeea 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f  b) ) goto abort_
1eeeb 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a  due_to_misuse;..
1eeec 20 20 69 66 28 20 21 75 2e 63 6a 2e 72 65 73 20    if( !u.cj.res 
1eeed 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  ){.    /* If the
1eeee 72 65 20 69 73 20 64 61 74 61 2c 20 6a 75 6d 70  re is data, jump
1eeef 20 74 6f 20 50 32 20 2a 2f 0a 20 20 20 20 70 63   to P2 */.    pc
1eef0 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
1eef1 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23    }.  break;.}.#
1eef2 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
1eef3 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
1eef4 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  E */..#ifndef SQ
1eef5 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
1eef6 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65  LTABLE./* Opcode
1eef7 3a 20 56 52 65 6e 61 6d 65 20 50 31 20 2a 20 2a  : VRename P1 * *
1eef8 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69   P4 *.**.** P4 i
1eef9 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
1eefa 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6f   virtual table o
1eefb 62 6a 65 63 74 2c 20 61 6e 20 73 71 6c 69 74 65  bject, an sqlite
1eefc 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 72 65  3_vtab structure
1eefd 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65  ..** This opcode
1eefe 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 63 6f 72   invokes the cor
1eeff 72 65 73 70 6f 6e 64 69 6e 67 20 78 52 65 6e 61  responding xRena
1ef00 6d 65 20 6d 65 74 68 6f 64 2e 20 54 68 65 20 76  me method. The v
1ef01 61 6c 75 65 0a 2a 2a 20 69 6e 20 72 65 67 69 73  alue.** in regis
1ef02 74 65 72 20 50 31 20 69 73 20 70 61 73 73 65 64  ter P1 is passed
1ef03 20 61 73 20 74 68 65 20 7a 4e 61 6d 65 20 61 72   as the zName ar
1ef04 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 78 52  gument to the xR
1ef05 65 6e 61 6d 65 20 6d 65 74 68 6f 64 2e 0a 2a 2f  ename method..*/
1ef06 0a 63 61 73 65 20 4f 50 5f 56 52 65 6e 61 6d 65  .case OP_VRename
1ef07 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f  : {.#if 0  /* lo
1ef08 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1ef09 76 65 64 20 69 6e 74 6f 20 75 2e 63 6b 20 2a 2f  ved into u.ck */
1ef0a 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  .  sqlite3_vtab 
1ef0b 2a 70 56 74 61 62 3b 0a 20 20 4d 65 6d 20 2a 70  *pVtab;.  Mem *p
1ef0c 4e 61 6d 65 3b 0a 23 65 6e 64 69 66 20 2f 2a 20  Name;.#endif /* 
1ef0d 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1ef0e 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6b 20  moved into u.ck 
1ef0f 2a 2f 0a 0a 20 20 75 2e 63 6b 2e 70 56 74 61 62  */..  u.ck.pVtab
1ef10 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62   = pOp->p4.pVtab
1ef11 2d 3e 70 56 74 61 62 3b 0a 20 20 75 2e 63 6b 2e  ->pVtab;.  u.ck.
1ef12 70 4e 61 6d 65 20 3d 20 26 70 2d 3e 61 4d 65 6d  pName = &p->aMem
1ef13 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73  [pOp->p1];.  ass
1ef14 65 72 74 28 20 75 2e 63 6b 2e 70 56 74 61 62 2d  ert( u.ck.pVtab-
1ef15 3e 70 4d 6f 64 75 6c 65 2d 3e 78 52 65 6e 61 6d  >pModule->xRenam
1ef16 65 20 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f  e );.  REGISTER_
1ef17 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20 75  TRACE(pOp->p1, u
1ef18 2e 63 6b 2e 70 4e 61 6d 65 29 3b 0a 20 20 61 73  .ck.pName);.  as
1ef19 73 65 72 74 28 20 75 2e 63 6b 2e 70 4e 61 6d 65  sert( u.ck.pName
1ef1a 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74  ->flags & MEM_St
1ef1b 72 20 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  r );.  if( sqlit
1ef1c 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20  e3SafetyOff(db) 
1ef1d 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
1ef1e 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 72 63  _to_misuse;.  rc
1ef1f 20 3d 20 75 2e 63 6b 2e 70 56 74 61 62 2d 3e 70   = u.ck.pVtab->p
1ef20 4d 6f 64 75 6c 65 2d 3e 78 52 65 6e 61 6d 65 28  Module->xRename(
1ef21 75 2e 63 6b 2e 70 56 74 61 62 2c 20 75 2e 63 6b  u.ck.pVtab, u.ck
1ef22 2e 70 4e 61 6d 65 2d 3e 7a 29 3b 0a 20 20 73 71  .pName->z);.  sq
1ef23 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
1ef24 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70  p->zErrMsg);.  p
1ef25 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63 6b  ->zErrMsg = u.ck
1ef26 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b  .pVtab->zErrMsg;
1ef27 0a 20 20 75 2e 63 6b 2e 70 56 74 61 62 2d 3e 7a  .  u.ck.pVtab->z
1ef28 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 69 66  ErrMsg = 0;.  if
1ef29 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  ( sqlite3SafetyO
1ef2a 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f  n(db) ) goto abo
1ef2b 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65  rt_due_to_misuse
1ef2c 3b 0a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65  ;..  break;.}.#e
1ef2d 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
1ef2e 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
1ef2f 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65  LTABLE./* Opcode
1ef30 3a 20 56 55 70 64 61 74 65 20 50 31 20 50 32 20  : VUpdate P1 P2 
1ef31 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34  P3 P4 *.**.** P4
1ef32 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
1ef33 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
1ef34 20 6f 62 6a 65 63 74 2c 20 61 6e 20 73 71 6c 69   object, an sqli
1ef35 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75  te3_vtab structu
1ef36 72 65 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f  re..** This opco
1ef37 64 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 63  de invokes the c
1ef38 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 78 55 70  orresponding xUp
1ef39 64 61 74 65 20 6d 65 74 68 6f 64 2e 20 50 32 20  date method. P2 
1ef3a 76 61 6c 75 65 73 0a 2a 2a 20 61 72 65 20 63 6f  values.** are co
1ef3b 6e 74 69 67 75 6f 75 73 20 6d 65 6d 6f 72 79 20  ntiguous memory 
1ef3c 63 65 6c 6c 73 20 73 74 61 72 74 69 6e 67 20 61  cells starting a
1ef3d 74 20 50 33 20 74 6f 20 70 61 73 73 20 74 6f 20  t P3 to pass to 
1ef3e 74 68 65 20 78 55 70 64 61 74 65 20 0a 2a 2a 20  the xUpdate .** 
1ef3f 69 6e 76 6f 63 61 74 69 6f 6e 2e 20 54 68 65 20  invocation. The 
1ef40 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65  value in registe
1ef41 72 20 28 50 33 2b 50 32 2d 31 29 20 63 6f 72 72  r (P3+P2-1) corr
1ef42 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 0a  esponds to the .
1ef43 2a 2a 20 70 32 74 68 20 65 6c 65 6d 65 6e 74 20  ** p2th element 
1ef44 6f 66 20 74 68 65 20 61 72 67 76 20 61 72 72 61  of the argv arra
1ef45 79 20 70 61 73 73 65 64 20 74 6f 20 78 55 70 64  y passed to xUpd
1ef46 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78  ate..**.** The x
1ef47 55 70 64 61 74 65 20 6d 65 74 68 6f 64 20 77 69  Update method wi
1ef48 6c 6c 20 64 6f 20 61 20 44 45 4c 45 54 45 20 6f  ll do a DELETE o
1ef49 72 20 61 6e 20 49 4e 53 45 52 54 20 6f 72 20 62  r an INSERT or b
1ef4a 6f 74 68 2e 0a 2a 2a 20 54 68 65 20 61 72 67 76  oth..** The argv
1ef4b 5b 30 5d 20 65 6c 65 6d 65 6e 74 20 28 77 68 69  [0] element (whi
1ef4c 63 68 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74  ch corresponds t
1ef4d 6f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 50 33  o memory cell P3
1ef4e 29 0a 2a 2a 20 69 73 20 74 68 65 20 72 6f 77 69  ).** is the rowi
1ef4f 64 20 6f 66 20 61 20 72 6f 77 20 74 6f 20 64 65  d of a row to de
1ef50 6c 65 74 65 2e 20 20 49 66 20 61 72 67 76 5b 30  lete.  If argv[0
1ef51 5d 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 6e  ] is NULL then n
1ef52 6f 20 0a 2a 2a 20 64 65 6c 65 74 69 6f 6e 20 6f  o .** deletion o
1ef53 63 63 75 72 73 2e 20 20 54 68 65 20 61 72 67 76  ccurs.  The argv
1ef54 5b 31 5d 20 65 6c 65 6d 65 6e 74 20 69 73 20 74  [1] element is t
1ef55 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20  he rowid of the 
1ef56 6e 65 77 20 0a 2a 2a 20 72 6f 77 2e 20 20 54 68  new .** row.  Th
1ef57 69 73 20 63 61 6e 20 62 65 20 4e 55 4c 4c 20 74  is can be NULL t
1ef58 6f 20 68 61 76 65 20 74 68 65 20 76 69 72 74 75  o have the virtu
1ef59 61 6c 20 74 61 62 6c 65 20 73 65 6c 65 63 74 20  al table select 
1ef5a 74 68 65 20 6e 65 77 20 0a 2a 2a 20 72 6f 77 69  the new .** rowi
1ef5b 64 20 66 6f 72 20 69 74 73 65 6c 66 2e 20 20 54  d for itself.  T
1ef5c 68 65 20 73 75 62 73 65 71 75 65 6e 74 20 65 6c  he subsequent el
1ef5d 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 61 72  ements in the ar
1ef5e 72 61 79 20 61 72 65 20 0a 2a 2a 20 74 68 65 20  ray are .** the 
1ef5f 76 61 6c 75 65 73 20 6f 66 20 63 6f 6c 75 6d 6e  values of column
1ef60 73 20 69 6e 20 74 68 65 20 6e 65 77 20 72 6f 77  s in the new row
1ef61 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 32 3d 3d 31  ..**.** If P2==1
1ef62 20 74 68 65 6e 20 6e 6f 20 69 6e 73 65 72 74 20   then no insert 
1ef63 69 73 20 70 65 72 66 6f 72 6d 65 64 2e 20 20 61  is performed.  a
1ef64 72 67 76 5b 30 5d 20 69 73 20 74 68 65 20 72 6f  rgv[0] is the ro
1ef65 77 69 64 20 6f 66 0a 2a 2a 20 61 20 72 6f 77 20  wid of.** a row 
1ef66 74 6f 20 64 65 6c 65 74 65 2e 0a 2a 2a 0a 2a 2a  to delete..**.**
1ef67 20 50 31 20 69 73 20 61 20 62 6f 6f 6c 65 61 6e   P1 is a boolean
1ef68 20 66 6c 61 67 2e 20 49 66 20 69 74 20 69 73 20   flag. If it is 
1ef69 73 65 74 20 74 6f 20 74 72 75 65 20 61 6e 64 20  set to true and 
1ef6a 74 68 65 20 78 55 70 64 61 74 65 20 63 61 6c 6c  the xUpdate call
1ef6b 0a 2a 2a 20 69 73 20 73 75 63 63 65 73 73 66 75  .** is successfu
1ef6c 6c 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75  l, then the valu
1ef6d 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71  e returned by sq
1ef6e 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72  lite3_last_inser
1ef6f 74 5f 72 6f 77 69 64 28 29 20 0a 2a 2a 20 69 73  t_rowid() .** is
1ef70 20 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75   set to the valu
1ef71 65 20 6f 66 20 74 68 65 20 72 6f 77 69 64 20 66  e of the rowid f
1ef72 6f 72 20 74 68 65 20 72 6f 77 20 6a 75 73 74 20  or the row just 
1ef73 69 6e 73 65 72 74 65 64 2e 0a 2a 2f 0a 63 61 73  inserted..*/.cas
1ef74 65 20 4f 50 5f 56 55 70 64 61 74 65 3a 20 7b 0a  e OP_VUpdate: {.
1ef75 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1ef76 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1ef77 69 6e 74 6f 20 75 2e 63 6c 20 2a 2f 0a 20 20 73  into u.cl */.  s
1ef78 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
1ef79 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 6f  ab;.  sqlite3_mo
1ef7a 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20  dule *pModule;. 
1ef7b 20 69 6e 74 20 6e 41 72 67 3b 0a 20 20 69 6e 74   int nArg;.  int
1ef7c 20 69 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74   i;.  sqlite_int
1ef7d 36 34 20 72 6f 77 69 64 3b 0a 20 20 4d 65 6d 20  64 rowid;.  Mem 
1ef7e 2a 2a 61 70 41 72 67 3b 0a 20 20 4d 65 6d 20 2a  **apArg;.  Mem *
1ef7f 70 58 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f  pX;.#endif /* lo
1ef80 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1ef81 76 65 64 20 69 6e 74 6f 20 75 2e 63 6c 20 2a 2f  ved into u.cl */
1ef82 0a 0a 20 20 75 2e 63 6c 2e 70 56 74 61 62 20 3d  ..  u.cl.pVtab =
1ef83 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 2d 3e   pOp->p4.pVtab->
1ef84 70 56 74 61 62 3b 0a 20 20 75 2e 63 6c 2e 70 4d  pVtab;.  u.cl.pM
1ef85 6f 64 75 6c 65 20 3d 20 28 73 71 6c 69 74 65 33  odule = (sqlite3
1ef86 5f 6d 6f 64 75 6c 65 20 2a 29 75 2e 63 6c 2e 70  _module *)u.cl.p
1ef87 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20  Vtab->pModule;. 
1ef88 20 75 2e 63 6c 2e 6e 41 72 67 20 3d 20 70 4f 70   u.cl.nArg = pOp
1ef89 2d 3e 70 32 3b 0a 20 20 61 73 73 65 72 74 28 20  ->p2;.  assert( 
1ef8a 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f  pOp->p4type==P4_
1ef8b 56 54 41 42 20 29 3b 0a 20 20 69 66 28 20 41 4c  VTAB );.  if( AL
1ef8c 57 41 59 53 28 75 2e 63 6c 2e 70 4d 6f 64 75 6c  WAYS(u.cl.pModul
1ef8d 65 2d 3e 78 55 70 64 61 74 65 29 20 29 7b 0a 20  e->xUpdate) ){. 
1ef8e 20 20 20 75 2e 63 6c 2e 61 70 41 72 67 20 3d 20     u.cl.apArg = 
1ef8f 70 2d 3e 61 70 41 72 67 3b 0a 20 20 20 20 75 2e  p->apArg;.    u.
1ef90 63 6c 2e 70 58 20 3d 20 26 70 2d 3e 61 4d 65 6d  cl.pX = &p->aMem
1ef91 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 66  [pOp->p3];.    f
1ef92 6f 72 28 75 2e 63 6c 2e 69 3d 30 3b 20 75 2e 63  or(u.cl.i=0; u.c
1ef93 6c 2e 69 3c 75 2e 63 6c 2e 6e 41 72 67 3b 20 75  l.i<u.cl.nArg; u
1ef94 2e 63 6c 2e 69 2b 2b 29 7b 0a 20 20 20 20 20 20  .cl.i++){.      
1ef95 73 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 75 2e  storeTypeInfo(u.
1ef96 63 6c 2e 70 58 2c 20 30 29 3b 0a 20 20 20 20 20  cl.pX, 0);.     
1ef97 20 75 2e 63 6c 2e 61 70 41 72 67 5b 75 2e 63 6c   u.cl.apArg[u.cl
1ef98 2e 69 5d 20 3d 20 75 2e 63 6c 2e 70 58 3b 0a 20  .i] = u.cl.pX;. 
1ef99 20 20 20 20 20 75 2e 63 6c 2e 70 58 2b 2b 3b 0a       u.cl.pX++;.
1ef9a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71      }.    if( sq
1ef9b 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64  lite3SafetyOff(d
1ef9c 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f  b) ) goto abort_
1ef9d 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20  due_to_misuse;. 
1ef9e 20 20 20 72 63 20 3d 20 75 2e 63 6c 2e 70 4d 6f     rc = u.cl.pMo
1ef9f 64 75 6c 65 2d 3e 78 55 70 64 61 74 65 28 75 2e  dule->xUpdate(u.
1efa0 63 6c 2e 70 56 74 61 62 2c 20 75 2e 63 6c 2e 6e  cl.pVtab, u.cl.n
1efa1 41 72 67 2c 20 75 2e 63 6c 2e 61 70 41 72 67 2c  Arg, u.cl.apArg,
1efa2 20 26 75 2e 63 6c 2e 72 6f 77 69 64 29 3b 0a 20   &u.cl.rowid);. 
1efa3 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
1efa4 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29  (db, p->zErrMsg)
1efa5 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67  ;.    p->zErrMsg
1efa6 20 3d 20 75 2e 63 6c 2e 70 56 74 61 62 2d 3e 7a   = u.cl.pVtab->z
1efa7 45 72 72 4d 73 67 3b 0a 20 20 20 20 75 2e 63 6c  ErrMsg;.    u.cl
1efa8 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20  .pVtab->zErrMsg 
1efa9 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  = 0;.    if( sql
1efaa 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29  ite3SafetyOn(db)
1efab 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75   ) goto abort_du
1efac 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20  e_to_misuse;.   
1efad 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1efae 4f 4b 20 26 26 20 70 4f 70 2d 3e 70 31 20 29 7b  OK && pOp->p1 ){
1efaf 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75  .      assert( u
1efb0 2e 63 6c 2e 6e 41 72 67 3e 31 20 26 26 20 75 2e  .cl.nArg>1 && u.
1efb1 63 6c 2e 61 70 41 72 67 5b 30 5d 20 26 26 20 28  cl.apArg[0] && (
1efb2 75 2e 63 6c 2e 61 70 41 72 67 5b 30 5d 2d 3e 66  u.cl.apArg[0]->f
1efb3 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 29 20 29  lags&MEM_Null) )
1efb4 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6c 61 73 74  ;.      db->last
1efb5 52 6f 77 69 64 20 3d 20 75 2e 63 6c 2e 72 6f 77  Rowid = u.cl.row
1efb6 69 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d  id;.    }.    p-
1efb7 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20 20 7d 0a  >nChange++;.  }.
1efb8 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69    break;.}.#endi
1efb9 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
1efba 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f  _VIRTUALTABLE */
1efbb 0a 0a 23 69 66 6e 64 65 66 20 20 53 51 4c 49 54  ..#ifndef  SQLIT
1efbc 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41  E_OMIT_PAGER_PRA
1efbd 47 4d 41 53 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  GMAS./* Opcode: 
1efbe 50 61 67 65 63 6f 75 6e 74 20 50 31 20 50 32 20  Pagecount P1 P2 
1efbf 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74  * * *.**.** Writ
1efc0 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 6e 75  e the current nu
1efc1 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e  mber of pages in
1efc2 20 64 61 74 61 62 61 73 65 20 50 31 20 74 6f 20   database P1 to 
1efc3 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 50 32 2e 0a  memory cell P2..
1efc4 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 61 67 65 63  */.case OP_Pagec
1efc5 6f 75 6e 74 3a 20 7b 20 20 20 20 20 20 20 20 20  ount: {         
1efc6 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65     /* out2-prere
1efc7 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 20 20  lease */.#if 0  
1efc8 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1efc9 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1efca 63 6d 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b 0a  cm */.  int p1;.
1efcb 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a 20 20 50    int nPage;.  P
1efcc 61 67 65 72 20 2a 70 50 61 67 65 72 3b 0a 23 65  ager *pPager;.#e
1efcd 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1efce 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1efcf 74 6f 20 75 2e 63 6d 20 2a 2f 0a 0a 20 20 75 2e  to u.cm */..  u.
1efd0 63 6d 2e 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b  cm.p1 = pOp->p1;
1efd1 0a 20 20 75 2e 63 6d 2e 70 50 61 67 65 72 20 3d  .  u.cm.pPager =
1efd2 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67   sqlite3BtreePag
1efd3 65 72 28 64 62 2d 3e 61 44 62 5b 75 2e 63 6d 2e  er(db->aDb[u.cm.
1efd4 70 31 5d 2e 70 42 74 29 3b 0a 20 20 72 63 20 3d  p1].pBt);.  rc =
1efd5 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
1efd6 65 63 6f 75 6e 74 28 75 2e 63 6d 2e 70 50 61 67  ecount(u.cm.pPag
1efd7 65 72 2c 20 26 75 2e 63 6d 2e 6e 50 61 67 65 29  er, &u.cm.nPage)
1efd8 3b 0a 20 20 2f 2a 20 4f 50 5f 50 61 67 65 63 6f  ;.  /* OP_Pageco
1efd9 75 6e 74 20 69 73 20 61 6c 77 61 79 73 20 63 61  unt is always ca
1efda 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e  lled from within
1efdb 20 61 20 72 65 61 64 20 74 72 61 6e 73 61 63 74   a read transact
1efdc 69 6f 6e 2e 20 20 54 68 65 0a 20 20 2a 2a 20 70  ion.  The.  ** p
1efdd 61 67 65 20 63 6f 75 6e 74 20 68 61 73 20 61 6c  age count has al
1efde 72 65 61 64 79 20 62 65 65 6e 20 73 75 63 63 65  ready been succe
1efdf 73 73 66 75 6c 6c 79 20 72 65 61 64 20 61 6e 64  ssfully read and
1efe0 20 63 61 63 68 65 64 2e 20 20 53 6f 20 74 68 65   cached.  So the
1efe1 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 50 61 67  .  ** sqlite3Pag
1efe2 65 72 50 61 67 65 63 6f 75 6e 74 28 29 20 63 61  erPagecount() ca
1efe3 6c 6c 20 61 62 6f 76 65 20 63 61 6e 6e 6f 74 20  ll above cannot 
1efe4 66 61 69 6c 2e 20 2a 2f 0a 20 20 69 66 28 20 41  fail. */.  if( A
1efe5 4c 57 41 59 53 28 72 63 3d 3d 53 51 4c 49 54 45  LWAYS(rc==SQLITE
1efe6 5f 4f 4b 29 20 29 7b 0a 20 20 20 20 70 4f 75 74  _OK) ){.    pOut
1efe7 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e  ->flags = MEM_In
1efe8 74 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69  t;.    pOut->u.i
1efe9 20 3d 20 75 2e 63 6d 2e 6e 50 61 67 65 3b 0a 20   = u.cm.nPage;. 
1efea 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65   }.  break;.}.#e
1efeb 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
1efec 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a  LITE_OMIT_TRACE.
1efed 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 72 61 63 65  /* Opcode: Trace
1efee 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a   * * * P4 *.**.*
1efef 2a 20 49 66 20 74 72 61 63 69 6e 67 20 69 73 20  * If tracing is 
1eff0 65 6e 61 62 6c 65 64 20 28 62 79 20 74 68 65 20  enabled (by the 
1eff1 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 29 29  sqlite3_trace())
1eff2 20 69 6e 74 65 72 66 61 63 65 2c 20 74 68 65 6e   interface, then
1eff3 0a 2a 2a 20 74 68 65 20 55 54 46 2d 38 20 73 74  .** the UTF-8 st
1eff4 72 69 6e 67 20 63 6f 6e 74 61 69 6e 65 64 20 69  ring contained i
1eff5 6e 20 50 34 20 69 73 20 65 6d 69 74 74 65 64 20  n P4 is emitted 
1eff6 6f 6e 20 74 68 65 20 74 72 61 63 65 20 63 61 6c  on the trace cal
1eff7 6c 62 61 63 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f  lback..*/.case O
1eff8 50 5f 54 72 61 63 65 3a 20 7b 0a 23 69 66 20 30  P_Trace: {.#if 0
1eff9 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
1effa 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1effb 75 2e 63 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a  u.cn */.  char *
1effc 7a 54 72 61 63 65 3b 0a 23 65 6e 64 69 66 20 2f  zTrace;.#endif /
1effd 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1effe 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63  s moved into u.c
1efff 6e 20 2a 2f 0a 0a 20 20 75 2e 63 6e 2e 7a 54 72  n */..  u.cn.zTr
1f000 61 63 65 20 3d 20 28 70 4f 70 2d 3e 70 34 2e 7a  ace = (pOp->p4.z
1f001 20 3f 20 70 4f 70 2d 3e 70 34 2e 7a 20 3a 20 70   ? pOp->p4.z : p
1f002 2d 3e 7a 53 71 6c 29 3b 0a 20 20 69 66 28 20 75  ->zSql);.  if( u
1f003 2e 63 6e 2e 7a 54 72 61 63 65 20 29 7b 0a 20 20  .cn.zTrace ){.  
1f004 20 20 69 66 28 20 64 62 2d 3e 78 54 72 61 63 65    if( db->xTrace
1f005 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 78 54   ){.      db->xT
1f006 72 61 63 65 28 64 62 2d 3e 70 54 72 61 63 65 41  race(db->pTraceA
1f007 72 67 2c 20 75 2e 63 6e 2e 7a 54 72 61 63 65 29  rg, u.cn.zTrace)
1f008 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53  ;.    }.#ifdef S
1f009 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20  QLITE_DEBUG.    
1f00a 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73 20 26  if( (db->flags &
1f00b 20 53 51 4c 49 54 45 5f 53 71 6c 54 72 61 63 65   SQLITE_SqlTrace
1f00c 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  )!=0 ){.      sq
1f00d 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
1f00e 28 22 53 51 4c 2d 74 72 61 63 65 3a 20 25 73 5c  ("SQL-trace: %s\
1f00f 6e 22 2c 20 75 2e 63 6e 2e 7a 54 72 61 63 65 29  n", u.cn.zTrace)
1f010 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f  ;.    }.#endif /
1f011 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 2a  * SQLITE_DEBUG *
1f012 2f 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  /.  }.  break;.}
1f013 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 20 4f 70 63  .#endif.../* Opc
1f014 6f 64 65 3a 20 4e 6f 6f 70 20 2a 20 2a 20 2a 20  ode: Noop * * * 
1f015 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 6f 20 6e 6f 74  * *.**.** Do not
1f016 68 69 6e 67 2e 20 20 54 68 69 73 20 69 6e 73 74  hing.  This inst
1f017 72 75 63 74 69 6f 6e 20 69 73 20 6f 66 74 65 6e  ruction is often
1f018 20 75 73 65 66 75 6c 20 61 73 20 61 20 6a 75 6d   useful as a jum
1f019 70 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e  p.** destination
1f01a 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d  ..*/./*.** The m
1f01b 61 67 69 63 20 45 78 70 6c 61 69 6e 20 6f 70 63  agic Explain opc
1f01c 6f 64 65 20 61 72 65 20 6f 6e 6c 79 20 69 6e 73  ode are only ins
1f01d 65 72 74 65 64 20 77 68 65 6e 20 65 78 70 6c 61  erted when expla
1f01e 69 6e 3d 3d 32 20 28 77 68 69 63 68 0a 2a 2a 20  in==2 (which.** 
1f01f 69 73 20 74 6f 20 73 61 79 20 77 68 65 6e 20 74  is to say when t
1f020 68 65 20 45 58 50 4c 41 49 4e 20 51 55 45 52 59  he EXPLAIN QUERY
1f021 20 50 4c 41 4e 20 73 79 6e 74 61 78 20 69 73 20   PLAN syntax is 
1f022 75 73 65 64 2e 29 0a 2a 2a 20 54 68 69 73 20 6f  used.).** This o
1f023 70 63 6f 64 65 20 72 65 63 6f 72 64 73 20 69 6e  pcode records in
1f024 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74  formation from t
1f025 68 65 20 6f 70 74 69 6d 69 7a 65 72 2e 20 20 49  he optimizer.  I
1f026 74 20 69 73 20 74 68 65 0a 2a 2a 20 74 68 65 20  t is the.** the 
1f027 73 61 6d 65 20 61 73 20 61 20 6e 6f 2d 6f 70 2e  same as a no-op.
1f028 20 20 54 68 69 73 20 6f 70 63 6f 64 65 73 6e 65    This opcodesne
1f029 76 65 72 20 61 70 70 65 61 72 73 20 69 6e 20 61  ver appears in a
1f02a 20 72 65 61 6c 20 56 4d 20 70 72 6f 67 72 61 6d   real VM program
1f02b 2e 0a 2a 2f 0a 64 65 66 61 75 6c 74 3a 20 7b 20  ..*/.default: { 
1f02c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73           /* This
1f02d 20 69 73 20 72 65 61 6c 6c 79 20 4f 50 5f 4e 6f   is really OP_No
1f02e 6f 70 20 61 6e 64 20 4f 50 5f 45 78 70 6c 61 69  op and OP_Explai
1f02f 6e 20 2a 2f 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  n */.  break;.}.
1f030 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1f031 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f032 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f033 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f034 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
1f035 2a 2a 20 54 68 65 20 63 61 73 65 73 20 6f 66 20  ** The cases of 
1f036 74 68 65 20 73 77 69 74 63 68 20 73 74 61 74 65  the switch state
1f037 6d 65 6e 74 20 61 62 6f 76 65 20 74 68 69 73 20  ment above this 
1f038 6c 69 6e 65 20 73 68 6f 75 6c 64 20 61 6c 6c 20  line should all 
1f039 62 65 20 69 6e 64 65 6e 74 65 64 0a 2a 2a 20 62  be indented.** b
1f03a 79 20 36 20 73 70 61 63 65 73 2e 20 20 42 75 74  y 6 spaces.  But
1f03b 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 36   the left-most 6
1f03c 20 73 70 61 63 65 73 20 68 61 76 65 20 62 65 65   spaces have bee
1f03d 6e 20 72 65 6d 6f 76 65 64 20 74 6f 20 69 6d 70  n removed to imp
1f03e 72 6f 76 65 20 74 68 65 0a 2a 2a 20 72 65 61 64  rove the.** read
1f03f 61 62 69 6c 69 74 79 2e 20 20 46 72 6f 6d 20 74  ability.  From t
1f040 68 69 73 20 70 6f 69 6e 74 20 6f 6e 20 64 6f 77  his point on dow
1f041 6e 2c 20 74 68 65 20 6e 6f 72 6d 61 6c 20 69 6e  n, the normal in
1f042 64 65 6e 74 61 74 69 6f 6e 20 72 75 6c 65 73 20  dentation rules 
1f043 61 72 65 0a 2a 2a 20 72 65 73 74 6f 72 65 64 2e  are.** restored.
1f044 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
1f045 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f046 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f047 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f048 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1f049 20 20 20 20 7d 0a 0a 23 69 66 64 65 66 20 56 44      }..#ifdef VD
1f04a 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 20 20 7b  BE_PROFILE.    {
1f04b 0a 20 20 20 20 20 20 75 36 34 20 65 6c 61 70 73  .      u64 elaps
1f04c 65 64 20 3d 20 73 71 6c 69 74 65 33 48 77 74 69  ed = sqlite3Hwti
1f04d 6d 65 28 29 20 2d 20 73 74 61 72 74 3b 0a 20 20  me() - start;.  
1f04e 20 20 20 20 70 4f 70 2d 3e 63 79 63 6c 65 73 20      pOp->cycles 
1f04f 2b 3d 20 65 6c 61 70 73 65 64 3b 0a 20 20 20 20  += elapsed;.    
1f050 20 20 70 4f 70 2d 3e 63 6e 74 2b 2b 3b 0a 23 69    pOp->cnt++;.#i
1f051 66 20 30 0a 20 20 20 20 20 20 20 20 66 70 72 69  f 0.        fpri
1f052 6e 74 66 28 73 74 64 6f 75 74 2c 20 22 25 31 30  ntf(stdout, "%10
1f053 6c 6c 75 20 22 2c 20 65 6c 61 70 73 65 64 29 3b  llu ", elapsed);
1f054 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1f055 56 64 62 65 50 72 69 6e 74 4f 70 28 73 74 64 6f  VdbePrintOp(stdo
1f056 75 74 2c 20 6f 72 69 67 50 63 2c 20 26 70 2d 3e  ut, origPc, &p->
1f057 61 4f 70 5b 6f 72 69 67 50 63 5d 29 3b 0a 23 65  aOp[origPc]);.#e
1f058 6e 64 69 66 0a 20 20 20 20 7d 0a 23 65 6e 64 69  ndif.    }.#endi
1f059 66 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f  f..    /* The fo
1f05a 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 61 64 64  llowing code add
1f05b 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 65  s nothing to the
1f05c 20 61 63 74 75 61 6c 20 66 75 6e 63 74 69 6f 6e   actual function
1f05d 61 6c 69 74 79 0a 20 20 20 20 2a 2a 20 6f 66 20  ality.    ** of 
1f05e 74 68 65 20 70 72 6f 67 72 61 6d 2e 20 20 49 74  the program.  It
1f05f 20 69 73 20 6f 6e 6c 79 20 68 65 72 65 20 66 6f   is only here fo
1f060 72 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65  r testing and de
1f061 62 75 67 67 69 6e 67 2e 0a 20 20 20 20 2a 2a 20  bugging..    ** 
1f062 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e  On the other han
1f063 64 2c 20 69 74 20 64 6f 65 73 20 62 75 72 6e 20  d, it does burn 
1f064 43 50 55 20 63 79 63 6c 65 73 20 65 76 65 72 79  CPU cycles every
1f065 20 74 69 6d 65 20 74 68 72 6f 75 67 68 0a 20 20   time through.  
1f066 20 20 2a 2a 20 74 68 65 20 65 76 61 6c 75 61 74    ** the evaluat
1f067 6f 72 20 6c 6f 6f 70 2e 20 20 53 6f 20 77 65 20  or loop.  So we 
1f068 63 61 6e 20 6c 65 61 76 65 20 69 74 20 6f 75 74  can leave it out
1f069 20 77 68 65 6e 20 4e 44 45 42 55 47 20 69 73 20   when NDEBUG is 
1f06a 64 65 66 69 6e 65 64 2e 0a 20 20 20 20 2a 2f 0a  defined..    */.
1f06b 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
1f06c 20 20 20 61 73 73 65 72 74 28 20 70 63 3e 3d 2d     assert( pc>=-
1f06d 31 20 26 26 20 70 63 3c 70 2d 3e 6e 4f 70 20 29  1 && pc<p->nOp )
1f06e 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ;..#ifdef SQLITE
1f06f 5f 44 45 42 55 47 0a 20 20 20 20 69 66 28 20 70  _DEBUG.    if( p
1f070 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20  ->trace ){.     
1f071 20 69 66 28 20 72 63 21 3d 30 20 29 20 66 70 72   if( rc!=0 ) fpr
1f072 69 6e 74 66 28 70 2d 3e 74 72 61 63 65 2c 22 72  intf(p->trace,"r
1f073 63 3d 25 64 5c 6e 22 2c 72 63 29 3b 0a 20 20 20  c=%d\n",rc);.   
1f074 20 20 20 69 66 28 20 6f 70 50 72 6f 70 65 72 74     if( opPropert
1f075 79 20 26 20 4f 50 46 4c 47 5f 4f 55 54 32 5f 50  y & OPFLG_OUT2_P
1f076 52 45 52 45 4c 45 41 53 45 20 29 7b 0a 20 20 20  RERELEASE ){.   
1f077 20 20 20 20 20 72 65 67 69 73 74 65 72 54 72 61       registerTra
1f078 63 65 28 70 2d 3e 74 72 61 63 65 2c 20 70 4f 70  ce(p->trace, pOp
1f079 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 20  ->p2, pOut);.   
1f07a 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6f     }.      if( o
1f07b 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c  pProperty & OPFL
1f07c 47 5f 4f 55 54 33 20 29 7b 0a 20 20 20 20 20 20  G_OUT3 ){.      
1f07d 20 20 72 65 67 69 73 74 65 72 54 72 61 63 65 28    registerTrace(
1f07e 70 2d 3e 74 72 61 63 65 2c 20 70 4f 70 2d 3e 70  p->trace, pOp->p
1f07f 33 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 20 20  3, pOut);.      
1f080 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 20  }.    }.#endif  
1f081 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20  /* SQLITE_DEBUG 
1f082 2a 2f 0a 23 65 6e 64 69 66 20 20 2f 2a 20 4e 44  */.#endif  /* ND
1f083 45 42 55 47 20 2a 2f 0a 20 20 7d 20 20 2f 2a 20  EBUG */.  }  /* 
1f084 54 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66  The end of the f
1f085 6f 72 28 3b 3b 29 20 6c 6f 6f 70 20 74 68 65 20  or(;;) loop the 
1f086 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 6f 70  loops through op
1f087 63 6f 64 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 49  codes */..  /* I
1f088 66 20 77 65 20 72 65 61 63 68 20 74 68 69 73 20  f we reach this 
1f089 70 6f 69 6e 74 2c 20 69 74 20 6d 65 61 6e 73 20  point, it means 
1f08a 74 68 61 74 20 65 78 65 63 75 74 69 6f 6e 20 69  that execution i
1f08b 73 20 66 69 6e 69 73 68 65 64 20 77 69 74 68 0a  s finished with.
1f08c 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 66    ** an error of
1f08d 20 73 6f 6d 65 20 6b 69 6e 64 2e 0a 20 20 2a 2f   some kind..  */
1f08e 0a 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74  .vdbe_error_halt
1f08f 3a 0a 20 20 61 73 73 65 72 74 28 20 72 63 20 29  :.  assert( rc )
1f090 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a  ;.  p->rc = rc;.
1f091 20 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c    sqlite3VdbeHal
1f092 74 28 70 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  t(p);.  if( rc==
1f093 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d  SQLITE_IOERR_NOM
1f094 45 4d 20 29 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  EM ) db->mallocF
1f095 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 72 63 20  ailed = 1;.  rc 
1f096 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
1f097 0a 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68  .  /* This is th
1f098 65 20 6f 6e 6c 79 20 77 61 79 20 6f 75 74 20 6f  e only way out o
1f099 66 20 74 68 69 73 20 70 72 6f 63 65 64 75 72 65  f this procedure
1f09a 2e 20 20 57 65 20 68 61 76 65 20 74 6f 0a 20 20  .  We have to.  
1f09b 2a 2a 20 72 65 6c 65 61 73 65 20 74 68 65 20 6d  ** release the m
1f09c 75 74 65 78 65 73 20 6f 6e 20 62 74 72 65 65 73  utexes on btrees
1f09d 20 74 68 61 74 20 77 65 72 65 20 61 63 71 75 69   that were acqui
1f09e 72 65 64 20 61 74 20 74 68 65 0a 20 20 2a 2a 20  red at the.  ** 
1f09f 74 6f 70 2e 20 2a 2f 0a 76 64 62 65 5f 72 65 74  top. */.vdbe_ret
1f0a0 75 72 6e 3a 0a 20 20 73 71 6c 69 74 65 33 42 74  urn:.  sqlite3Bt
1f0a1 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61  reeMutexArrayLea
1f0a2 76 65 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b 0a  ve(&p->aMutex);.
1f0a3 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20    return rc;..  
1f0a4 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20  /* Jump to here 
1f0a5 69 66 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62  if a string or b
1f0a6 6c 6f 62 20 6c 61 72 67 65 72 20 74 68 61 6e 20  lob larger than 
1f0a7 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54  SQLITE_MAX_LENGT
1f0a8 48 0a 20 20 2a 2a 20 69 73 20 65 6e 63 6f 75 6e  H.  ** is encoun
1f0a9 74 65 72 65 64 2e 0a 20 20 2a 2f 0a 74 6f 6f 5f  tered..  */.too_
1f0aa 62 69 67 3a 0a 20 20 73 71 6c 69 74 65 33 53 65  big:.  sqlite3Se
1f0ab 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72  tString(&p->zErr
1f0ac 4d 73 67 2c 20 64 62 2c 20 22 73 74 72 69 6e 67  Msg, db, "string
1f0ad 20 6f 72 20 62 6c 6f 62 20 74 6f 6f 20 62 69 67   or blob too big
1f0ae 22 29 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54  ");.  rc = SQLIT
1f0af 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 67 6f 74 6f  E_TOOBIG;.  goto
1f0b0 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74   vdbe_error_halt
1f0b1 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20  ;..  /* Jump to 
1f0b2 68 65 72 65 20 69 66 20 61 20 6d 61 6c 6c 6f 63  here if a malloc
1f0b3 28 29 20 66 61 69 6c 73 2e 0a 20 20 2a 2f 0a 6e  () fails..  */.n
1f0b4 6f 5f 6d 65 6d 3a 0a 20 20 64 62 2d 3e 6d 61 6c  o_mem:.  db->mal
1f0b5 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
1f0b6 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e   sqlite3SetStrin
1f0b7 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64  g(&p->zErrMsg, d
1f0b8 62 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72  b, "out of memor
1f0b9 79 22 29 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49  y");.  rc = SQLI
1f0ba 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 67 6f 74 6f  TE_NOMEM;.  goto
1f0bb 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74   vdbe_error_halt
1f0bc 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20  ;..  /* Jump to 
1f0bd 68 65 72 65 20 66 6f 72 20 61 6e 20 53 51 4c 49  here for an SQLI
1f0be 54 45 5f 4d 49 53 55 53 45 20 65 72 72 6f 72 2e  TE_MISUSE error.
1f0bf 0a 20 20 2a 2f 0a 61 62 6f 72 74 5f 64 75 65 5f  .  */.abort_due_
1f0c0 74 6f 5f 6d 69 73 75 73 65 3a 0a 20 20 72 63 20  to_misuse:.  rc 
1f0c1 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  = SQLITE_MISUSE;
1f0c2 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 75 20  .  /* Fall thru 
1f0c3 69 6e 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  into abort_due_t
1f0c4 6f 5f 65 72 72 6f 72 20 2a 2f 0a 0a 20 20 2f 2a  o_error */..  /*
1f0c5 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 66 6f   Jump to here fo
1f0c6 72 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64  r any other kind
1f0c7 20 6f 66 20 66 61 74 61 6c 20 65 72 72 6f 72 2e   of fatal error.
1f0c8 20 20 54 68 65 20 22 72 63 22 20 76 61 72 69 61    The "rc" varia
1f0c9 62 6c 65 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20  ble.  ** should 
1f0ca 68 6f 6c 64 20 74 68 65 20 65 72 72 6f 72 20 6e  hold the error n
1f0cb 75 6d 62 65 72 2e 0a 20 20 2a 2f 0a 61 62 6f 72  umber..  */.abor
1f0cc 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3a 0a  t_due_to_error:.
1f0cd 20 20 61 73 73 65 72 74 28 20 70 2d 3e 7a 45 72    assert( p->zEr
1f0ce 72 4d 73 67 3d 3d 30 20 29 3b 0a 20 20 69 66 28  rMsg==0 );.  if(
1f0cf 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
1f0d0 64 20 29 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  d ) rc = SQLITE_
1f0d1 4e 4f 4d 45 4d 3b 0a 20 20 69 66 28 20 72 63 21  NOMEM;.  if( rc!
1f0d2 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f  =SQLITE_IOERR_NO
1f0d3 4d 45 4d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  MEM ){.    sqlit
1f0d4 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e  e3SetString(&p->
1f0d5 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73  zErrMsg, db, "%s
1f0d6 22 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74 72  ", sqlite3ErrStr
1f0d7 28 72 63 29 29 3b 0a 20 20 7d 0a 20 20 67 6f 74  (rc));.  }.  got
1f0d8 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c  o vdbe_error_hal
1f0d9 74 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f  t;..  /* Jump to
1f0da 20 68 65 72 65 20 69 66 20 74 68 65 20 73 71 6c   here if the sql
1f0db 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29  ite3_interrupt()
1f0dc 20 41 50 49 20 73 65 74 73 20 74 68 65 20 69 6e   API sets the in
1f0dd 74 65 72 72 75 70 74 0a 20 20 2a 2a 20 66 6c 61  terrupt.  ** fla
1f0de 67 2e 0a 20 20 2a 2f 0a 61 62 6f 72 74 5f 64 75  g..  */.abort_du
1f0df 65 5f 74 6f 5f 69 6e 74 65 72 72 75 70 74 3a 0a  e_to_interrupt:.
1f0e0 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 75 31    assert( db->u1
1f0e1 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 29  .isInterrupted )
1f0e2 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  ;.  rc = SQLITE_
1f0e3 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 70 2d 3e  INTERRUPT;.  p->
1f0e4 72 63 20 3d 20 72 63 3b 0a 20 20 73 71 6c 69 74  rc = rc;.  sqlit
1f0e5 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e  e3SetString(&p->
1f0e6 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73  zErrMsg, db, "%s
1f0e7 22 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74 72  ", sqlite3ErrStr
1f0e8 28 72 63 29 29 3b 0a 20 20 67 6f 74 6f 20 76 64  (rc));.  goto vd
1f0e9 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 7d  be_error_halt;.}
1f0ea 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
1f0eb 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 2e 63 20  * End of vdbe.c 
1f0ec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f0ed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f0ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f0ef 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
1f0f0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62  * Begin file vdb
1f0f1 65 62 6c 6f 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  eblob.c ********
1f0f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f0f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f0f4 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4d 61 79  /./*.** 2007 May
1f0f5 20 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74   1.**.** The aut
1f0f6 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
1f0f7 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
1f0f8 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
1f0f9 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
1f0fa 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
1f0fb 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
1f0fc 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
1f0fd 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
1f0fe 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
1f0ff 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
1f100 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
1f101 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
1f102 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
1f103 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
1f104 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
1f105 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
1f106 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
1f107 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f108 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f109 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f10a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f10b 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  *.**.** This fil
1f10c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20  e contains code 
1f10d 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  used to implemen
1f10e 74 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 42 4c  t incremental BL
1f10f 4f 42 20 49 2f 4f 2e 0a 2a 2a 0a 2a 2a 20 24 49  OB I/O..**.** $I
1f110 64 3a 20 76 64 62 65 62 6c 6f 62 2e 63 2c 76 20  d: vdbeblob.c,v 
1f111 31 2e 33 35 20 32 30 30 39 2f 30 37 2f 30 32 20  1.35 2009/07/02 
1f112 30 37 3a 34 37 3a 33 33 20 64 61 6e 69 65 6c 6b  07:47:33 danielk
1f113 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a  1977 Exp $.*/...
1f114 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1f115 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 0a 2f 2a  MIT_INCRBLOB../*
1f116 0a 2a 2a 20 56 61 6c 69 64 20 73 71 6c 69 74 65  .** Valid sqlite
1f117 33 5f 62 6c 6f 62 2a 20 68 61 6e 64 6c 65 73 20  3_blob* handles 
1f118 70 6f 69 6e 74 20 74 6f 20 49 6e 63 72 62 6c 6f  point to Incrblo
1f119 62 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f  b structures..*/
1f11a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
1f11b 49 6e 63 72 62 6c 6f 62 20 49 6e 63 72 62 6c 6f  Incrblob Incrblo
1f11c 62 3b 0a 73 74 72 75 63 74 20 49 6e 63 72 62 6c  b;.struct Incrbl
1f11d 6f 62 20 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73  ob {.  int flags
1f11e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
1f11f 2a 20 43 6f 70 79 20 6f 66 20 22 66 6c 61 67 73  * Copy of "flags
1f120 22 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69  " passed to sqli
1f121 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 20  te3_blob_open() 
1f122 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 20  */.  int nByte; 
1f123 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1f124 53 69 7a 65 20 6f 66 20 6f 70 65 6e 20 62 6c 6f  Size of open blo
1f125 62 2c 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20  b, in bytes */. 
1f126 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 20 20 20   int iOffset;   
1f127 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65           /* Byte
1f128 20 6f 66 66 73 65 74 20 6f 66 20 62 6c 6f 62 20   offset of blob 
1f129 69 6e 20 63 75 72 73 6f 72 20 64 61 74 61 20 2a  in cursor data *
1f12a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43  /.  BtCursor *pC
1f12b 73 72 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43  sr;         /* C
1f12c 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 61  ursor pointing a
1f12d 74 20 62 6c 6f 62 20 72 6f 77 20 2a 2f 0a 20 20  t blob row */.  
1f12e 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
1f12f 74 6d 74 3b 20 20 20 20 2f 2a 20 53 74 61 74 65  tmt;    /* State
1f130 6d 65 6e 74 20 68 6f 6c 64 69 6e 67 20 63 75 72  ment holding cur
1f131 73 6f 72 20 6f 70 65 6e 20 2a 2f 0a 20 20 73 71  sor open */.  sq
1f132 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20  lite3 *db;      
1f133 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 73 73        /* The ass
1f134 6f 63 69 61 74 65 64 20 64 61 74 61 62 61 73 65  ociated database
1f135 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4f 70   */.};../*.** Op
1f136 65 6e 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65  en a blob handle
1f137 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1f138 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  int sqlite3_blob
1f139 5f 6f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33  _open(.  sqlite3
1f13a 2a 20 64 62 2c 20 20 20 20 20 20 20 20 20 20 20  * db,           
1f13b 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
1f13c 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
1f13d 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
1f13e 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ,        /* The 
1f13f 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
1f140 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  e containing the
1f141 20 62 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73 74   blob */.  const
1f142 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 2c 20 20   char *zTable,  
1f143 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20     /* The table 
1f144 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 62  containing the b
1f145 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  lob */.  const c
1f146 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 2c 20 20 20  har *zColumn,   
1f147 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 63   /* The column c
1f148 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 62 6c  ontaining the bl
1f149 6f 62 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69  ob */.  sqlite_i
1f14a 6e 74 36 34 20 69 52 6f 77 2c 20 20 20 20 20 20  nt64 iRow,      
1f14b 2f 2a 20 54 68 65 20 72 6f 77 20 63 6f 6e 74 61  /* The row conta
1f14c 69 6e 69 6e 67 20 74 68 65 20 67 6c 6f 62 20 2a  ining the glob *
1f14d 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20  /.  int flags,  
1f14e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1f14f 72 75 65 20 2d 3e 20 72 65 61 64 2f 77 72 69 74  rue -> read/writ
1f150 65 20 61 63 63 65 73 73 2c 20 66 61 6c 73 65 20  e access, false 
1f151 2d 3e 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a  -> read-only */.
1f152 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a    sqlite3_blob *
1f153 2a 70 70 42 6c 6f 62 20 20 20 2f 2a 20 48 61 6e  *ppBlob   /* Han
1f154 64 6c 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e  dle for accessin
1f155 67 20 74 68 65 20 62 6c 6f 62 20 72 65 74 75 72  g the blob retur
1f156 6e 65 64 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  ned here */.){. 
1f157 20 69 6e 74 20 6e 41 74 74 65 6d 70 74 20 3d 20   int nAttempt = 
1f158 30 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 20 20  0;.  int iCol;  
1f159 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1f15a 49 6e 64 65 78 20 6f 66 20 7a 43 6f 6c 75 6d 6e  Index of zColumn
1f15b 20 69 6e 20 72 6f 77 2d 72 65 63 6f 72 64 20 2a   in row-record *
1f15c 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 56 44 42  /..  /* This VDB
1f15d 45 20 70 72 6f 67 72 61 6d 20 73 65 65 6b 73 20  E program seeks 
1f15e 61 20 62 74 72 65 65 20 63 75 72 73 6f 72 20 74  a btree cursor t
1f15f 6f 20 74 68 65 20 69 64 65 6e 74 69 66 69 65 64  o the identified
1f160 20 0a 20 20 2a 2a 20 64 62 2f 74 61 62 6c 65 2f   .  ** db/table/
1f161 72 6f 77 20 65 6e 74 72 79 2e 20 54 68 65 20 72  row entry. The r
1f162 65 61 73 6f 6e 20 66 6f 72 20 75 73 69 6e 67 20  eason for using 
1f163 61 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 69  a vdbe program i
1f164 6e 73 74 65 61 64 0a 20 20 2a 2a 20 6f 66 20 77  nstead.  ** of w
1f165 72 69 74 69 6e 67 20 63 6f 64 65 20 74 6f 20 75  riting code to u
1f166 73 65 20 74 68 65 20 62 2d 74 72 65 65 20 6c 61  se the b-tree la
1f167 79 65 72 20 64 69 72 65 63 74 6c 79 20 69 73 20  yer directly is 
1f168 74 68 61 74 20 74 68 65 0a 20 20 2a 2a 20 76 64  that the.  ** vd
1f169 62 65 20 70 72 6f 67 72 61 6d 20 77 69 6c 6c 20  be program will 
1f16a 74 61 6b 65 20 61 64 76 61 6e 74 61 67 65 20 6f  take advantage o
1f16b 66 20 74 68 65 20 76 61 72 69 6f 75 73 20 74 72  f the various tr
1f16c 61 6e 73 61 63 74 69 6f 6e 2c 0a 20 20 2a 2a 20  ansaction,.  ** 
1f16d 6c 6f 63 6b 69 6e 67 20 61 6e 64 20 65 72 72 6f  locking and erro
1f16e 72 20 68 61 6e 64 6c 69 6e 67 20 69 6e 66 72 61  r handling infra
1f16f 73 74 72 75 63 74 75 72 65 20 62 75 69 6c 74 20  structure built 
1f170 69 6e 74 6f 20 74 68 65 20 76 64 62 65 2e 0a 20  into the vdbe.. 
1f171 20 2a 2a 0a 20 20 2a 2a 20 41 66 74 65 72 20 73   **.  ** After s
1f172 65 65 6b 69 6e 67 20 74 68 65 20 63 75 72 73 6f  eeking the curso
1f173 72 2c 20 74 68 65 20 76 64 62 65 20 65 78 65 63  r, the vdbe exec
1f174 75 74 65 73 20 61 6e 20 4f 50 5f 52 65 73 75 6c  utes an OP_Resul
1f175 74 52 6f 77 2e 0a 20 20 2a 2a 20 43 6f 64 65 20  tRow..  ** Code 
1f176 65 78 74 65 72 6e 61 6c 20 74 6f 20 74 68 65 20  external to the 
1f177 56 64 62 65 20 74 68 65 6e 20 22 62 6f 72 72 6f  Vdbe then "borro
1f178 77 73 22 20 74 68 65 20 62 2d 74 72 65 65 20 63  ws" the b-tree c
1f179 75 72 73 6f 72 20 61 6e 64 0a 20 20 2a 2a 20 75  ursor and.  ** u
1f17a 73 65 73 20 69 74 20 74 6f 20 69 6d 70 6c 65 6d  ses it to implem
1f17b 65 6e 74 20 74 68 65 20 62 6c 6f 62 5f 72 65 61  ent the blob_rea
1f17c 64 28 29 2c 20 62 6c 6f 62 5f 77 72 69 74 65 28  d(), blob_write(
1f17d 29 20 61 6e 64 20 0a 20 20 2a 2a 20 62 6c 6f 62  ) and .  ** blob
1f17e 5f 62 79 74 65 73 28 29 20 66 75 6e 63 74 69 6f  _bytes() functio
1f17f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  ns..  **.  ** Th
1f180 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63  e sqlite3_blob_c
1f181 6c 6f 73 65 28 29 20 66 75 6e 63 74 69 6f 6e 20  lose() function 
1f182 66 69 6e 61 6c 69 7a 65 73 20 74 68 65 20 76 64  finalizes the vd
1f183 62 65 20 70 72 6f 67 72 61 6d 2c 0a 20 20 2a 2a  be program,.  **
1f184 20 77 68 69 63 68 20 63 6c 6f 73 65 73 20 74 68   which closes th
1f185 65 20 62 2d 74 72 65 65 20 63 75 72 73 6f 72 20  e b-tree cursor 
1f186 61 6e 64 20 28 70 6f 73 73 69 62 6c 79 29 20 63  and (possibly) c
1f187 6f 6d 6d 69 74 73 20 74 68 65 20 0a 20 20 2a 2a  ommits the .  **
1f188 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20   transaction..  
1f189 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  */.  static cons
1f18a 74 20 56 64 62 65 4f 70 4c 69 73 74 20 6f 70 65  t VdbeOpList ope
1f18b 6e 42 6c 6f 62 5b 5d 20 3d 20 7b 0a 20 20 20 20  nBlob[] = {.    
1f18c 7b 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c  {OP_Transaction,
1f18d 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 2f   0, 0, 0},     /
1f18e 2a 20 30 3a 20 53 74 61 72 74 20 61 20 74 72 61  * 0: Start a tra
1f18f 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20  nsaction */.    
1f190 7b 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65  {OP_VerifyCookie
1f191 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 2f  , 0, 0, 0},    /
1f192 2a 20 31 3a 20 43 68 65 63 6b 20 74 68 65 20 73  * 1: Check the s
1f193 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 2a 2f 0a  chema cookie */.
1f194 20 20 20 20 7b 4f 50 5f 54 61 62 6c 65 4c 6f 63      {OP_TableLoc
1f195 6b 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20  k, 0, 0, 0},    
1f196 20 20 20 2f 2a 20 32 3a 20 41 63 71 75 69 72 65     /* 2: Acquire
1f197 20 61 20 72 65 61 64 20 6f 72 20 77 72 69 74 65   a read or write
1f198 20 6c 6f 63 6b 20 2a 2f 0a 0a 20 20 20 20 2f 2a   lock */..    /*
1f199 20 4f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c   One of the foll
1f19a 6f 77 69 6e 67 20 74 77 6f 20 69 6e 73 74 72 75  owing two instru
1f19b 63 74 69 6f 6e 73 20 69 73 20 72 65 70 6c 61 63  ctions is replac
1f19c 65 64 20 62 79 20 61 6e 20 4f 50 5f 4e 6f 6f 70  ed by an OP_Noop
1f19d 2e 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 4f 70 65  . */.    {OP_Ope
1f19e 6e 52 65 61 64 2c 20 30 2c 20 30 2c 20 30 7d 2c  nRead, 0, 0, 0},
1f19f 20 20 20 20 20 20 20 20 2f 2a 20 33 3a 20 4f 70          /* 3: Op
1f1a0 65 6e 20 63 75 72 73 6f 72 20 30 20 66 6f 72 20  en cursor 0 for 
1f1a1 72 65 61 64 69 6e 67 20 2a 2f 0a 20 20 20 20 7b  reading */.    {
1f1a2 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 30 2c  OP_OpenWrite, 0,
1f1a3 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 2f 2a   0, 0},       /*
1f1a4 20 34 3a 20 4f 70 65 6e 20 63 75 72 73 6f 72 20   4: Open cursor 
1f1a5 30 20 66 6f 72 20 72 65 61 64 2f 77 72 69 74 65  0 for read/write
1f1a6 20 2a 2f 0a 0a 20 20 20 20 7b 4f 50 5f 56 61 72   */..    {OP_Var
1f1a7 69 61 62 6c 65 2c 20 31 2c 20 31 2c 20 31 7d 2c  iable, 1, 1, 1},
1f1a8 20 20 20 20 20 20 20 20 2f 2a 20 35 3a 20 50 75          /* 5: Pu
1f1a9 73 68 20 74 68 65 20 72 6f 77 69 64 20 74 6f 20  sh the rowid to 
1f1aa 74 68 65 20 73 74 61 63 6b 20 2a 2f 0a 20 20 20  the stack */.   
1f1ab 20 7b 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20   {OP_NotExists, 
1f1ac 30 2c 20 39 2c 20 31 7d 2c 20 20 20 20 20 20 20  0, 9, 1},       
1f1ad 2f 2a 20 36 3a 20 53 65 65 6b 20 74 68 65 20 63  /* 6: Seek the c
1f1ae 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 7b 4f 50  ursor */.    {OP
1f1af 5f 43 6f 6c 75 6d 6e 2c 20 30 2c 20 30 2c 20 31  _Column, 0, 0, 1
1f1b0 7d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 37  },          /* 7
1f1b1 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 52 65 73    */.    {OP_Res
1f1b2 75 6c 74 52 6f 77 2c 20 31 2c 20 30 2c 20 30 7d  ultRow, 1, 0, 0}
1f1b3 2c 20 20 20 20 20 20 20 2f 2a 20 38 20 20 2a 2f  ,       /* 8  */
1f1b4 0a 20 20 20 20 7b 4f 50 5f 43 6c 6f 73 65 2c 20  .    {OP_Close, 
1f1b5 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20  0, 0, 0},       
1f1b6 20 20 20 20 2f 2a 20 39 20 20 2a 2f 0a 20 20 20      /* 9  */.   
1f1b7 20 7b 4f 50 5f 48 61 6c 74 2c 20 30 2c 20 30 2c   {OP_Halt, 0, 0,
1f1b8 20 30 7d 2c 20 20 20 20 20 20 20 20 20 20 20 20   0},            
1f1b9 2f 2a 20 31 30 20 2a 2f 0a 20 20 7d 3b 0a 0a 20  /* 10 */.  };.. 
1f1ba 20 56 64 62 65 20 2a 76 20 3d 20 30 3b 0a 20 20   Vdbe *v = 0;.  
1f1bb 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
1f1bc 4f 4b 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72  OK;.  char *zErr
1f1bd 20 3d 20 30 3b 0a 20 20 54 61 62 6c 65 20 2a 70   = 0;.  Table *p
1f1be 54 61 62 3b 0a 20 20 50 61 72 73 65 20 2a 70 50  Tab;.  Parse *pP
1f1bf 61 72 73 65 3b 0a 0a 20 20 2a 70 70 42 6c 6f 62  arse;..  *ppBlob
1f1c0 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f   = 0;.  sqlite3_
1f1c1 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
1f1c2 6d 75 74 65 78 29 3b 0a 20 20 70 50 61 72 73 65  mutex);.  pParse
1f1c3 20 3d 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41   = sqlite3StackA
1f1c4 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65  llocRaw(db, size
1f1c5 6f 66 28 2a 70 50 61 72 73 65 29 29 3b 0a 20 20  of(*pParse));.  
1f1c6 69 66 28 20 70 50 61 72 73 65 3d 3d 30 20 29 7b  if( pParse==0 ){
1f1c7 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
1f1c8 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f  _NOMEM;.    goto
1f1c9 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a   blob_open_out;.
1f1ca 20 20 7d 0a 20 20 64 6f 20 7b 0a 20 20 20 20 6d    }.  do {.    m
1f1cb 65 6d 73 65 74 28 70 50 61 72 73 65 2c 20 30 2c  emset(pParse, 0,
1f1cc 20 73 69 7a 65 6f 66 28 50 61 72 73 65 29 29 3b   sizeof(Parse));
1f1cd 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 64 62 20  .    pParse->db 
1f1ce 3d 20 64 62 3b 0a 0a 20 20 20 20 69 66 28 20 73  = db;..    if( s
1f1cf 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64  qlite3SafetyOn(d
1f1d0 62 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  b) ){.      sqli
1f1d1 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45  te3DbFree(db, zE
1f1d2 72 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  rr);.      sqlit
1f1d3 65 33 53 74 61 63 6b 46 72 65 65 28 64 62 2c 20  e3StackFree(db, 
1f1d4 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 73  pParse);.      s
1f1d5 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
1f1d6 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
1f1d7 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
1f1d8 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 20 20 7d  TE_MISUSE;.    }
1f1d9 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ..    sqlite3Btr
1f1da 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a  eeEnterAll(db);.
1f1db 20 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74      pTab = sqlit
1f1dc 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50  e3LocateTable(pP
1f1dd 61 72 73 65 2c 20 30 2c 20 7a 54 61 62 6c 65 2c  arse, 0, zTable,
1f1de 20 7a 44 62 29 3b 0a 20 20 20 20 69 66 28 20 70   zDb);.    if( p
1f1df 54 61 62 20 26 26 20 49 73 56 69 72 74 75 61 6c  Tab && IsVirtual
1f1e0 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20  (pTab) ){.      
1f1e1 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 20 20  pTab = 0;.      
1f1e2 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1f1e3 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20  pParse, "cannot 
1f1e4 6f 70 65 6e 20 76 69 72 74 75 61 6c 20 74 61 62  open virtual tab
1f1e5 6c 65 3a 20 25 73 22 2c 20 7a 54 61 62 6c 65 29  le: %s", zTable)
1f1e6 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20  ;.    }.#ifndef 
1f1e7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
1f1e8 0a 20 20 20 20 69 66 28 20 70 54 61 62 20 26 26  .    if( pTab &&
1f1e9 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29   pTab->pSelect )
1f1ea 7b 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20 30  {.      pTab = 0
1f1eb 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
1f1ec 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
1f1ed 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 76 69 65  "cannot open vie
1f1ee 77 3a 20 25 73 22 2c 20 7a 54 61 62 6c 65 29 3b  w: %s", zTable);
1f1ef 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
1f1f0 20 20 69 66 28 20 21 70 54 61 62 20 29 7b 0a 20    if( !pTab ){. 
1f1f1 20 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d       if( pParse-
1f1f2 3e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20  >zErrMsg ){.    
1f1f3 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1f1f4 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20  e(db, zErr);.   
1f1f5 20 20 20 20 20 7a 45 72 72 20 3d 20 70 50 61 72       zErr = pPar
1f1f6 73 65 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20  se->zErrMsg;.   
1f1f7 20 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 45 72       pParse->zEr
1f1f8 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 20 20  rMsg = 0;.      
1f1f9 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  }.      rc = SQL
1f1fa 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20  ITE_ERROR;.     
1f1fb 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61   (void)sqlite3Sa
1f1fc 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20  fetyOff(db);.   
1f1fd 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c     sqlite3BtreeL
1f1fe 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20  eaveAll(db);.   
1f1ff 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65     goto blob_ope
1f200 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20  n_out;.    }..  
1f201 20 20 2f 2a 20 4e 6f 77 20 73 65 61 72 63 68 20    /* Now search 
1f202 70 54 61 62 20 66 6f 72 20 74 68 65 20 65 78 61  pTab for the exa
1f203 63 74 20 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a 20 20  ct column. */.  
1f204 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43    for(iCol=0; iC
1f205 6f 6c 20 3c 20 70 54 61 62 2d 3e 6e 43 6f 6c 3b  ol < pTab->nCol;
1f206 20 69 43 6f 6c 2b 2b 29 20 7b 0a 20 20 20 20 20   iCol++) {.     
1f207 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
1f208 43 6d 70 28 70 54 61 62 2d 3e 61 43 6f 6c 5b 69  Cmp(pTab->aCol[i
1f209 43 6f 6c 5d 2e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c  Col].zName, zCol
1f20a 75 6d 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  umn)==0 ){.     
1f20b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
1f20c 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
1f20d 69 43 6f 6c 3d 3d 70 54 61 62 2d 3e 6e 43 6f 6c  iCol==pTab->nCol
1f20e 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1f20f 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72  3DbFree(db, zErr
1f210 29 3b 0a 20 20 20 20 20 20 7a 45 72 72 20 3d 20  );.      zErr = 
1f211 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
1f212 62 2c 20 22 6e 6f 20 73 75 63 68 20 63 6f 6c 75  b, "no such colu
1f213 6d 6e 3a 20 5c 22 25 73 5c 22 22 2c 20 7a 43 6f  mn: \"%s\"", zCo
1f214 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 72 63 20  lumn);.      rc 
1f215 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
1f216 20 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69        (void)sqli
1f217 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29  te3SafetyOff(db)
1f218 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  ;.      sqlite3B
1f219 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29  treeLeaveAll(db)
1f21a 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f  ;.      goto blo
1f21b 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20  b_open_out;.    
1f21c 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  }..    /* If the
1f21d 20 76 61 6c 75 65 20 69 73 20 62 65 69 6e 67 20   value is being 
1f21e 6f 70 65 6e 65 64 20 66 6f 72 20 77 72 69 74 69  opened for writi
1f21f 6e 67 2c 20 63 68 65 63 6b 20 74 68 61 74 20 74  ng, check that t
1f220 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e  he.    ** column
1f221 20 69 73 20 6e 6f 74 20 69 6e 64 65 78 65 64 2e   is not indexed.
1f222 20 49 74 20 69 73 20 61 67 61 69 6e 73 74 20 74   It is against t
1f223 68 65 20 72 75 6c 65 73 20 74 6f 20 6f 70 65 6e  he rules to open
1f224 20 61 6e 0a 20 20 20 20 2a 2a 20 69 6e 64 65 78   an.    ** index
1f225 65 64 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 77 72  ed column for wr
1f226 69 74 69 6e 67 2e 0a 20 20 20 20 2a 2f 0a 20 20  iting..    */.  
1f227 20 20 69 66 28 20 66 6c 61 67 73 20 29 7b 0a 20    if( flags ){. 
1f228 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78       Index *pIdx
1f229 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 49 64 78  ;.      for(pIdx
1f22a 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
1f22b 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
1f22c 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20  pNext){.        
1f22d 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20 20 66  int j;.        f
1f22e 6f 72 28 6a 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e  or(j=0; j<pIdx->
1f22f 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20  nColumn; j++){. 
1f230 20 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64           if( pId
1f231 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3d 3d  x->aiColumn[j]==
1f232 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  iCol ){.        
1f233 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1f234 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20  e(db, zErr);.   
1f235 20 20 20 20 20 20 20 20 20 7a 45 72 72 20 3d 20           zErr = 
1f236 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
1f237 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  b,.             
1f238 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f239 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 69 6e 64  "cannot open ind
1f23a 65 78 65 64 20 63 6f 6c 75 6d 6e 20 66 6f 72 20  exed column for 
1f23b 77 72 69 74 69 6e 67 22 29 3b 0a 20 20 20 20 20  writing");.     
1f23c 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
1f23d 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20  TE_ERROR;.      
1f23e 20 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69        (void)sqli
1f23f 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29  te3SafetyOff(db)
1f240 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ;.            sq
1f241 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 41  lite3BtreeLeaveA
1f242 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20  ll(db);.        
1f243 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70      goto blob_op
1f244 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20  en_out;.        
1f245 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
1f246 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
1f247 20 76 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65   v = sqlite3Vdbe
1f248 43 72 65 61 74 65 28 64 62 29 3b 0a 20 20 20 20  Create(db);.    
1f249 69 66 28 20 76 20 29 7b 0a 20 20 20 20 20 20 69  if( v ){.      i
1f24a 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33  nt iDb = sqlite3
1f24b 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62  SchemaToIndex(db
1f24c 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29  , pTab->pSchema)
1f24d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1f24e 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76 2c 20  dbeAddOpList(v, 
1f24f 73 69 7a 65 6f 66 28 6f 70 65 6e 42 6c 6f 62 29  sizeof(openBlob)
1f250 2f 73 69 7a 65 6f 66 28 56 64 62 65 4f 70 4c 69  /sizeof(VdbeOpLi
1f251 73 74 29 2c 20 6f 70 65 6e 42 6c 6f 62 29 3b 0a  st), openBlob);.
1f252 20 20 20 20 20 20 66 6c 61 67 73 20 3d 20 21 21        flags = !!
1f253 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20  flags;          
1f254 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73 20         /* flags 
1f255 3d 20 28 66 6c 61 67 73 20 3f 20 31 20 3a 20 30  = (flags ? 1 : 0
1f256 29 3b 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20  ); */..      /* 
1f257 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 4f 50  Configure the OP
1f258 5f 54 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a  _Transaction */.
1f259 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1f25a 65 43 68 61 6e 67 65 50 31 28 76 2c 20 30 2c 20  eChangeP1(v, 0, 
1f25b 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  iDb);.      sqli
1f25c 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28  te3VdbeChangeP2(
1f25d 76 2c 20 30 2c 20 66 6c 61 67 73 29 3b 0a 0a 20  v, 0, flags);.. 
1f25e 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72       /* Configur
1f25f 65 20 74 68 65 20 4f 50 5f 56 65 72 69 66 79 43  e the OP_VerifyC
1f260 6f 6f 6b 69 65 20 2a 2f 0a 20 20 20 20 20 20 73  ookie */.      s
1f261 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1f262 50 31 28 76 2c 20 31 2c 20 69 44 62 29 3b 0a 20  P1(v, 1, iDb);. 
1f263 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1f264 43 68 61 6e 67 65 50 32 28 76 2c 20 31 2c 20 70  ChangeP2(v, 1, p
1f265 54 61 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 73 63  Tab->pSchema->sc
1f266 68 65 6d 61 5f 63 6f 6f 6b 69 65 29 3b 0a 0a 20  hema_cookie);.. 
1f267 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72       /* Make sur
1f268 65 20 61 20 6d 75 74 65 78 20 69 73 20 68 65 6c  e a mutex is hel
1f269 64 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 74  d on the table t
1f26a 6f 20 62 65 20 61 63 63 65 73 73 65 64 20 2a 2f  o be accessed */
1f26b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1f26c 62 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69  beUsesBtree(v, i
1f26d 44 62 29 3b 20 0a 0a 20 20 20 20 20 20 2f 2a 20  Db); ..      /* 
1f26e 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 4f 50  Configure the OP
1f26f 5f 54 61 62 6c 65 4c 6f 63 6b 20 69 6e 73 74 72  _TableLock instr
1f270 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20  uction */.      
1f271 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1f272 65 50 31 28 76 2c 20 32 2c 20 69 44 62 29 3b 0a  eP1(v, 2, iDb);.
1f273 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1f274 65 43 68 61 6e 67 65 50 32 28 76 2c 20 32 2c 20  eChangeP2(v, 2, 
1f275 70 54 61 62 2d 3e 74 6e 75 6d 29 3b 0a 20 20 20  pTab->tnum);.   
1f276 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
1f277 61 6e 67 65 50 33 28 76 2c 20 32 2c 20 66 6c 61  angeP3(v, 2, fla
1f278 67 73 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  gs);.      sqlit
1f279 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
1f27a 2c 20 32 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  , 2, pTab->zName
1f27b 2c 20 50 34 5f 54 52 41 4e 53 49 45 4e 54 29 3b  , P4_TRANSIENT);
1f27c 0a 0a 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76  ..      /* Remov
1f27d 65 20 65 69 74 68 65 72 20 74 68 65 20 4f 50 5f  e either the OP_
1f27e 4f 70 65 6e 57 72 69 74 65 20 6f 72 20 4f 70 65  OpenWrite or Ope
1f27f 6e 52 65 61 64 2e 20 53 65 74 20 74 68 65 20 50  nRead. Set the P
1f280 32 20 0a 20 20 20 20 20 20 2a 2a 20 70 61 72 61  2 .      ** para
1f281 6d 65 74 65 72 20 6f 66 20 74 68 65 20 6f 74 68  meter of the oth
1f282 65 72 20 74 6f 20 70 54 61 62 2d 3e 74 6e 75 6d  er to pTab->tnum
1f283 2e 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69  .  */.      sqli
1f284 74 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f 4e  te3VdbeChangeToN
1f285 6f 6f 70 28 76 2c 20 34 20 2d 20 66 6c 61 67 73  oop(v, 4 - flags
1f286 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 1);.      sqli
1f287 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28  te3VdbeChangeP2(
1f288 76 2c 20 33 20 2b 20 66 6c 61 67 73 2c 20 70 54  v, 3 + flags, pT
1f289 61 62 2d 3e 74 6e 75 6d 29 3b 0a 20 20 20 20 20  ab->tnum);.     
1f28a 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
1f28b 67 65 50 33 28 76 2c 20 33 20 2b 20 66 6c 61 67  geP3(v, 3 + flag
1f28c 73 2c 20 69 44 62 29 3b 0a 0a 20 20 20 20 20 20  s, iDb);..      
1f28d 2f 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65  /* Configure the
1f28e 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
1f28f 6e 73 2e 20 43 6f 6e 66 69 67 75 72 65 20 74 68  ns. Configure th
1f290 65 20 63 75 72 73 6f 72 20 74 6f 0a 20 20 20 20  e cursor to.    
1f291 20 20 2a 2a 20 74 68 69 6e 6b 20 74 68 61 74 20    ** think that 
1f292 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 6f 6e  the table has on
1f293 65 20 6d 6f 72 65 20 63 6f 6c 75 6d 6e 20 74 68  e more column th
1f294 61 6e 20 69 74 20 72 65 61 6c 6c 79 0a 20 20 20  an it really.   
1f295 20 20 20 2a 2a 20 64 6f 65 73 2e 20 41 6e 20 4f     ** does. An O
1f296 50 5f 43 6f 6c 75 6d 6e 20 74 6f 20 72 65 74 72  P_Column to retr
1f297 69 65 76 65 20 74 68 69 73 20 69 6d 61 67 69 6e  ieve this imagin
1f298 61 72 79 20 63 6f 6c 75 6d 6e 20 77 69 6c 6c 0a  ary column will.
1f299 20 20 20 20 20 20 2a 2a 20 61 6c 77 61 79 73 20        ** always 
1f29a 72 65 74 75 72 6e 20 61 6e 20 53 51 4c 20 4e 55  return an SQL NU
1f29b 4c 4c 2e 20 54 68 69 73 20 69 73 20 75 73 65 66  LL. This is usef
1f29c 75 6c 20 62 65 63 61 75 73 65 20 69 74 20 6d 65  ul because it me
1f29d 61 6e 73 0a 20 20 20 20 20 20 2a 2a 20 77 65 20  ans.      ** we 
1f29e 63 61 6e 20 69 6e 76 6f 6b 65 20 4f 50 5f 43 6f  can invoke OP_Co
1f29f 6c 75 6d 6e 20 74 6f 20 66 69 6c 6c 20 69 6e 20  lumn to fill in 
1f2a0 74 68 65 20 76 64 62 65 20 63 75 72 73 6f 72 73  the vdbe cursors
1f2a1 20 74 79 70 65 20 0a 20 20 20 20 20 20 2a 2a 20   type .      ** 
1f2a2 61 6e 64 20 6f 66 66 73 65 74 20 63 61 63 68 65  and offset cache
1f2a3 20 77 69 74 68 6f 75 74 20 63 61 75 73 69 6e 67   without causing
1f2a4 20 61 6e 79 20 49 4f 2e 0a 20 20 20 20 20 20 2a   any IO..      *
1f2a5 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  /.      sqlite3V
1f2a6 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 33  dbeChangeP4(v, 3
1f2a7 2b 66 6c 61 67 73 2c 20 53 51 4c 49 54 45 5f 49  +flags, SQLITE_I
1f2a8 4e 54 5f 54 4f 5f 50 54 52 28 70 54 61 62 2d 3e  NT_TO_PTR(pTab->
1f2a9 6e 43 6f 6c 2b 31 29 2c 50 34 5f 49 4e 54 33 32  nCol+1),P4_INT32
1f2aa 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1f2ab 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20  VdbeChangeP2(v, 
1f2ac 37 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a  7, pTab->nCol);.
1f2ad 20 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d        if( !db->m
1f2ae 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
1f2af 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1f2b0 62 65 4d 61 6b 65 52 65 61 64 79 28 76 2c 20 31  beMakeReady(v, 1
1f2b1 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 29  , 1, 1, 0, 0, 0)
1f2b2 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1f2b3 20 20 20 0a 20 20 20 20 73 71 6c 69 74 65 33 42     .    sqlite3B
1f2b4 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29  treeLeaveAll(db)
1f2b5 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
1f2b6 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b  e3SafetyOff(db);
1f2b7 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 72  .    if( NEVER(r
1f2b8 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 7c 7c  c!=SQLITE_OK) ||
1f2b9 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
1f2ba 64 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  d ){.      goto 
1f2bb 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20  blob_open_out;. 
1f2bc 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65     }..    sqlite
1f2bd 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 28 73 71  3_bind_int64((sq
1f2be 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 76 2c 20  lite3_stmt *)v, 
1f2bf 31 2c 20 69 52 6f 77 29 3b 0a 20 20 20 20 72 63  1, iRow);.    rc
1f2c0 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
1f2c1 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29  (sqlite3_stmt *)
1f2c2 76 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  v);.    if( rc!=
1f2c3 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20  SQLITE_ROW ){.  
1f2c4 20 20 20 20 6e 41 74 74 65 6d 70 74 2b 2b 3b 0a      nAttempt++;.
1f2c5 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1f2c6 65 33 5f 66 69 6e 61 6c 69 7a 65 28 28 73 71 6c  e3_finalize((sql
1f2c7 69 74 65 33 5f 73 74 6d 74 20 2a 29 76 29 3b 0a  ite3_stmt *)v);.
1f2c8 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
1f2c9 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20  ree(db, zErr);. 
1f2ca 20 20 20 20 20 7a 45 72 72 20 3d 20 73 71 6c 69       zErr = sqli
1f2cb 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 73  te3MPrintf(db, s
1f2cc 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 64 62  qlite3_errmsg(db
1f2cd 29 29 3b 0a 20 20 20 20 20 20 76 20 3d 20 30 3b  ));.      v = 0;
1f2ce 0a 20 20 20 20 7d 0a 20 20 7d 20 77 68 69 6c 65  .    }.  } while
1f2cf 28 20 6e 41 74 74 65 6d 70 74 3c 35 20 26 26 20  ( nAttempt<5 && 
1f2d0 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d  rc==SQLITE_SCHEM
1f2d1 41 20 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d  A );..  if( rc==
1f2d2 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20  SQLITE_ROW ){.  
1f2d3 20 20 2f 2a 20 54 68 65 20 72 6f 77 2d 72 65 63    /* The row-rec
1f2d4 6f 72 64 20 68 61 73 20 62 65 65 6e 20 6f 70 65  ord has been ope
1f2d5 6e 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79  ned successfully
1f2d6 2e 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65  . Check that the
1f2d7 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69  .    ** column i
1f2d8 6e 20 71 75 65 73 74 69 6f 6e 20 63 6f 6e 74 61  n question conta
1f2d9 69 6e 73 20 74 65 78 74 20 6f 72 20 61 20 62 6c  ins text or a bl
1f2da 6f 62 2e 20 49 66 20 69 74 20 63 6f 6e 74 61 69  ob. If it contai
1f2db 6e 73 0a 20 20 20 20 2a 2a 20 74 65 78 74 2c 20  ns.    ** text, 
1f2dc 69 74 20 69 73 20 75 70 20 74 6f 20 74 68 65 20  it is up to the 
1f2dd 63 61 6c 6c 65 72 20 74 6f 20 67 65 74 20 74 68  caller to get th
1f2de 65 20 65 6e 63 6f 64 69 6e 67 20 72 69 67 68 74  e encoding right
1f2df 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 49 6e 63  ..    */.    Inc
1f2e0 72 62 6c 6f 62 20 2a 70 42 6c 6f 62 3b 0a 20 20  rblob *pBlob;.  
1f2e1 20 20 75 33 32 20 74 79 70 65 20 3d 20 76 2d 3e    u32 type = v->
1f2e2 61 70 43 73 72 5b 30 5d 2d 3e 61 54 79 70 65 5b  apCsr[0]->aType[
1f2e3 69 43 6f 6c 5d 3b 0a 0a 20 20 20 20 69 66 28 20  iCol];..    if( 
1f2e4 74 79 70 65 3c 31 32 20 29 7b 0a 20 20 20 20 20  type<12 ){.     
1f2e5 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1f2e6 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20  b, zErr);.      
1f2e7 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d 50  zErr = sqlite3MP
1f2e8 72 69 6e 74 66 28 64 62 2c 20 22 63 61 6e 6e 6f  rintf(db, "canno
1f2e9 74 20 6f 70 65 6e 20 76 61 6c 75 65 20 6f 66 20  t open value of 
1f2ea 74 79 70 65 20 25 73 22 2c 0a 20 20 20 20 20 20  type %s",.      
1f2eb 20 20 20 20 74 79 70 65 3d 3d 30 3f 22 6e 75 6c      type==0?"nul
1f2ec 6c 22 3a 20 74 79 70 65 3d 3d 37 3f 22 72 65 61  l": type==7?"rea
1f2ed 6c 22 3a 20 22 69 6e 74 65 67 65 72 22 0a 20 20  l": "integer".  
1f2ee 20 20 20 20 29 3b 0a 20 20 20 20 20 20 72 63 20      );.      rc 
1f2ef 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
1f2f0 20 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f        goto blob_
1f2f1 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a  open_out;.    }.
1f2f2 20 20 20 20 70 42 6c 6f 62 20 3d 20 28 49 6e 63      pBlob = (Inc
1f2f3 72 62 6c 6f 62 20 2a 29 73 71 6c 69 74 65 33 44  rblob *)sqlite3D
1f2f4 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
1f2f5 73 69 7a 65 6f 66 28 49 6e 63 72 62 6c 6f 62 29  sizeof(Incrblob)
1f2f6 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d  );.    if( db->m
1f2f7 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
1f2f8 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
1f2f9 65 65 28 64 62 2c 20 70 42 6c 6f 62 29 3b 0a 20  ee(db, pBlob);. 
1f2fa 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f       goto blob_o
1f2fb 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20  pen_out;.    }. 
1f2fc 20 20 20 70 42 6c 6f 62 2d 3e 66 6c 61 67 73 20     pBlob->flags 
1f2fd 3d 20 66 6c 61 67 73 3b 0a 20 20 20 20 70 42 6c  = flags;.    pBl
1f2fe 6f 62 2d 3e 70 43 73 72 20 3d 20 20 76 2d 3e 61  ob->pCsr =  v->a
1f2ff 70 43 73 72 5b 30 5d 2d 3e 70 43 75 72 73 6f 72  pCsr[0]->pCursor
1f300 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ;.    sqlite3Btr
1f301 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28 70 42  eeEnterCursor(pB
1f302 6c 6f 62 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20  lob->pCsr);.    
1f303 73 71 6c 69 74 65 33 42 74 72 65 65 43 61 63 68  sqlite3BtreeCach
1f304 65 4f 76 65 72 66 6c 6f 77 28 70 42 6c 6f 62 2d  eOverflow(pBlob-
1f305 3e 70 43 73 72 29 3b 0a 20 20 20 20 73 71 6c 69  >pCsr);.    sqli
1f306 74 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72  te3BtreeLeaveCur
1f307 73 6f 72 28 70 42 6c 6f 62 2d 3e 70 43 73 72 29  sor(pBlob->pCsr)
1f308 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 70 53 74  ;.    pBlob->pSt
1f309 6d 74 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74  mt = (sqlite3_st
1f30a 6d 74 20 2a 29 76 3b 0a 20 20 20 20 70 42 6c 6f  mt *)v;.    pBlo
1f30b 62 2d 3e 69 4f 66 66 73 65 74 20 3d 20 76 2d 3e  b->iOffset = v->
1f30c 61 70 43 73 72 5b 30 5d 2d 3e 61 4f 66 66 73 65  apCsr[0]->aOffse
1f30d 74 5b 69 43 6f 6c 5d 3b 0a 20 20 20 20 70 42 6c  t[iCol];.    pBl
1f30e 6f 62 2d 3e 6e 42 79 74 65 20 3d 20 73 71 6c 69  ob->nByte = sqli
1f30f 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70  te3VdbeSerialTyp
1f310 65 4c 65 6e 28 74 79 70 65 29 3b 0a 20 20 20 20  eLen(type);.    
1f311 70 42 6c 6f 62 2d 3e 64 62 20 3d 20 64 62 3b 0a  pBlob->db = db;.
1f312 20 20 20 20 2a 70 70 42 6c 6f 62 20 3d 20 28 73      *ppBlob = (s
1f313 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 29 70 42  qlite3_blob *)pB
1f314 6c 6f 62 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  lob;.    rc = SQ
1f315 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65  LITE_OK;.  }else
1f316 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1f317 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  OK ){.    sqlite
1f318 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72  3DbFree(db, zErr
1f319 29 3b 0a 20 20 20 20 7a 45 72 72 20 3d 20 73 71  );.    zErr = sq
1f31a 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
1f31b 20 22 6e 6f 20 73 75 63 68 20 72 6f 77 69 64 3a   "no such rowid:
1f31c 20 25 6c 6c 64 22 2c 20 69 52 6f 77 29 3b 0a 20   %lld", iRow);. 
1f31d 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
1f31e 52 52 4f 52 3b 0a 20 20 7d 0a 0a 62 6c 6f 62 5f  RROR;.  }..blob_
1f31f 6f 70 65 6e 5f 6f 75 74 3a 0a 20 20 69 66 28 20  open_out:.  if( 
1f320 76 20 26 26 20 28 72 63 21 3d 53 51 4c 49 54 45  v && (rc!=SQLITE
1f321 5f 4f 4b 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  _OK || db->mallo
1f322 63 46 61 69 6c 65 64 29 20 29 7b 0a 20 20 20 20  cFailed) ){.    
1f323 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c  sqlite3VdbeFinal
1f324 69 7a 65 28 76 29 3b 0a 20 20 7d 0a 20 20 73 71  ize(v);.  }.  sq
1f325 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72  lite3Error(db, r
1f326 63 2c 20 7a 45 72 72 29 3b 0a 20 20 73 71 6c 69  c, zErr);.  sqli
1f327 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45  te3DbFree(db, zE
1f328 72 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 74  rr);.  sqlite3St
1f329 61 63 6b 46 72 65 65 28 64 62 2c 20 70 50 61 72  ackFree(db, pPar
1f32a 73 65 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  se);.  rc = sqli
1f32b 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72  te3ApiExit(db, r
1f32c 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  c);.  sqlite3_mu
1f32d 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
1f32e 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  tex);.  return r
1f32f 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73  c;.}../*.** Clos
1f330 65 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 20  e a blob handle 
1f331 74 68 61 74 20 77 61 73 20 70 72 65 76 69 6f 75  that was previou
1f332 73 6c 79 20 63 72 65 61 74 65 64 20 75 73 69 6e  sly created usin
1f333 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 6c 6f  g.** sqlite3_blo
1f334 62 5f 6f 70 65 6e 28 29 2e 0a 2a 2f 0a 53 51 4c  b_open()..*/.SQL
1f335 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1f336 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 73  te3_blob_close(s
1f337 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c  qlite3_blob *pBl
1f338 6f 62 29 7b 0a 20 20 49 6e 63 72 62 6c 6f 62 20  ob){.  Incrblob 
1f339 2a 70 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20 2a  *p = (Incrblob *
1f33a 29 70 42 6c 6f 62 3b 0a 20 20 69 6e 74 20 72 63  )pBlob;.  int rc
1f33b 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  ;.  sqlite3 *db;
1f33c 0a 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  ..  if( p ){.   
1f33d 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20   db = p->db;.   
1f33e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
1f33f 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
1f340 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1f341 33 5f 66 69 6e 61 6c 69 7a 65 28 70 2d 3e 70 53  3_finalize(p->pS
1f342 74 6d 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  tmt);.    sqlite
1f343 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a  3DbFree(db, p);.
1f344 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
1f345 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
1f346 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  x);.  }else{.   
1f347 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
1f348 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
1f349 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f  ;.}../*.** Perfo
1f34a 72 6d 20 61 20 72 65 61 64 20 6f 72 20 77 72 69  rm a read or wri
1f34b 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 6f 6e 20  te operation on 
1f34c 61 20 62 6c 6f 62 0a 2a 2f 0a 73 74 61 74 69 63  a blob.*/.static
1f34d 20 69 6e 74 20 62 6c 6f 62 52 65 61 64 57 72 69   int blobReadWri
1f34e 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c  te(.  sqlite3_bl
1f34f 6f 62 20 2a 70 42 6c 6f 62 2c 20 0a 20 20 76 6f  ob *pBlob, .  vo
1f350 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c  id *z, .  int n,
1f351 20 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74 2c   .  int iOffset,
1f352 20 0a 20 20 69 6e 74 20 28 2a 78 43 61 6c 6c 29   .  int (*xCall)
1f353 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 2c  (BtCursor*, u32,
1f354 20 75 33 32 2c 20 76 6f 69 64 2a 29 0a 29 7b 0a   u32, void*).){.
1f355 20 20 69 6e 74 20 72 63 3b 0a 20 20 49 6e 63 72    int rc;.  Incr
1f356 62 6c 6f 62 20 2a 70 20 3d 20 28 49 6e 63 72 62  blob *p = (Incrb
1f357 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 56  lob *)pBlob;.  V
1f358 64 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65  dbe *v;.  sqlite
1f359 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28 20 70 3d  3 *db;..  if( p=
1f35a 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  =0 ) return SQLI
1f35b 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 64 62 20  TE_MISUSE;.  db 
1f35c 3d 20 70 2d 3e 64 62 3b 0a 20 20 73 71 6c 69 74  = p->db;.  sqlit
1f35d 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
1f35e 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 76 20 3d  b->mutex);.  v =
1f35f 20 28 56 64 62 65 2a 29 70 2d 3e 70 53 74 6d 74   (Vdbe*)p->pStmt
1f360 3b 0a 0a 20 20 69 66 28 20 6e 3c 30 20 7c 7c 20  ;..  if( n<0 || 
1f361 69 4f 66 66 73 65 74 3c 30 20 7c 7c 20 28 69 4f  iOffset<0 || (iO
1f362 66 66 73 65 74 2b 6e 29 3e 70 2d 3e 6e 42 79 74  ffset+n)>p->nByt
1f363 65 20 29 7b 0a 20 20 20 20 2f 2a 20 52 65 71 75  e ){.    /* Requ
1f364 65 73 74 20 69 73 20 6f 75 74 20 6f 66 20 72 61  est is out of ra
1f365 6e 67 65 2e 20 52 65 74 75 72 6e 20 61 20 74 72  nge. Return a tr
1f366 61 6e 73 69 65 6e 74 20 65 72 72 6f 72 2e 20 2a  ansient error. *
1f367 2f 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  /.    rc = SQLIT
1f368 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c  E_ERROR;.    sql
1f369 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51  ite3Error(db, SQ
1f36a 4c 49 54 45 5f 45 52 52 4f 52 2c 20 30 29 3b 0a  LITE_ERROR, 0);.
1f36b 20 20 7d 20 65 6c 73 65 20 69 66 28 20 76 3d 3d    } else if( v==
1f36c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74  0 ){.    /* If t
1f36d 68 65 72 65 20 69 73 20 6e 6f 20 73 74 61 74 65  here is no state
1f36e 6d 65 6e 74 20 68 61 6e 64 6c 65 2c 20 74 68 65  ment handle, the
1f36f 6e 20 74 68 65 20 62 6c 6f 62 2d 68 61 6e 64 6c  n the blob-handl
1f370 65 20 68 61 73 0a 20 20 20 20 2a 2a 20 61 6c 72  e has.    ** alr
1f371 65 61 64 79 20 62 65 65 6e 20 69 6e 76 61 6c 69  eady been invali
1f372 64 61 74 65 64 2e 20 52 65 74 75 72 6e 20 53 51  dated. Return SQ
1f373 4c 49 54 45 5f 41 42 4f 52 54 20 69 6e 20 74 68  LITE_ABORT in th
1f374 69 73 20 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a  is case..    */.
1f375 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1f376 41 42 4f 52 54 3b 0a 20 20 7d 65 6c 73 65 7b 0a  ABORT;.  }else{.
1f377 20 20 20 20 2f 2a 20 43 61 6c 6c 20 65 69 74 68      /* Call eith
1f378 65 72 20 42 74 72 65 65 44 61 74 61 28 29 20 6f  er BtreeData() o
1f379 72 20 42 74 72 65 65 50 75 74 44 61 74 61 28 29  r BtreePutData()
1f37a 2e 20 49 66 20 53 51 4c 49 54 45 5f 41 42 4f 52  . If SQLITE_ABOR
1f37b 54 20 69 73 0a 20 20 20 20 2a 2a 20 72 65 74 75  T is.    ** retu
1f37c 72 6e 65 64 2c 20 63 6c 65 61 6e 2d 75 70 20 74  rned, clean-up t
1f37d 68 65 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e  he statement han
1f37e 64 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  dle..    */.    
1f37f 61 73 73 65 72 74 28 20 64 62 20 3d 3d 20 76 2d  assert( db == v-
1f380 3e 64 62 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  >db );.    sqlit
1f381 65 33 42 74 72 65 65 45 6e 74 65 72 43 75 72 73  e3BtreeEnterCurs
1f382 6f 72 28 70 2d 3e 70 43 73 72 29 3b 0a 20 20 20  or(p->pCsr);.   
1f383 20 72 63 20 3d 20 78 43 61 6c 6c 28 70 2d 3e 70   rc = xCall(p->p
1f384 43 73 72 2c 20 69 4f 66 66 73 65 74 2b 70 2d 3e  Csr, iOffset+p->
1f385 69 4f 66 66 73 65 74 2c 20 6e 2c 20 7a 29 3b 0a  iOffset, n, z);.
1f386 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
1f387 4c 65 61 76 65 43 75 72 73 6f 72 28 70 2d 3e 70  LeaveCursor(p->p
1f388 43 73 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63  Csr);.    if( rc
1f389 3d 3d 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 29  ==SQLITE_ABORT )
1f38a 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
1f38b 64 62 65 46 69 6e 61 6c 69 7a 65 28 76 29 3b 0a  dbeFinalize(v);.
1f38c 20 20 20 20 20 20 70 2d 3e 70 53 74 6d 74 20 3d        p->pStmt =
1f38d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   0;.    }else{. 
1f38e 20 20 20 20 20 64 62 2d 3e 65 72 72 43 6f 64 65       db->errCode
1f38f 20 3d 20 72 63 3b 0a 20 20 20 20 20 20 76 2d 3e   = rc;.      v->
1f390 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 7d 0a 20  rc = rc;.    }. 
1f391 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65   }.  rc = sqlite
1f392 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29  3ApiExit(db, rc)
1f393 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
1f394 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
1f395 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  x);.  return rc;
1f396 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64  .}../*.** Read d
1f397 61 74 61 20 66 72 6f 6d 20 61 20 62 6c 6f 62 20  ata from a blob 
1f398 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54  handle..*/.SQLIT
1f399 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1f39a 33 5f 62 6c 6f 62 5f 72 65 61 64 28 73 71 6c 69  3_blob_read(sqli
1f39b 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c  te3_blob *pBlob,
1f39c 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c   void *z, int n,
1f39d 20 69 6e 74 20 69 4f 66 66 73 65 74 29 7b 0a 20   int iOffset){. 
1f39e 20 72 65 74 75 72 6e 20 62 6c 6f 62 52 65 61 64   return blobRead
1f39f 57 72 69 74 65 28 70 42 6c 6f 62 2c 20 7a 2c 20  Write(pBlob, z, 
1f3a0 6e 2c 20 69 4f 66 66 73 65 74 2c 20 73 71 6c 69  n, iOffset, sqli
1f3a1 74 65 33 42 74 72 65 65 44 61 74 61 29 3b 0a 7d  te3BtreeData);.}
1f3a2 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61  ../*.** Write da
1f3a3 74 61 20 74 6f 20 61 20 62 6c 6f 62 20 68 61 6e  ta to a blob han
1f3a4 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  dle..*/.SQLITE_A
1f3a5 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
1f3a6 6c 6f 62 5f 77 72 69 74 65 28 73 71 6c 69 74 65  lob_write(sqlite
1f3a7 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 63  3_blob *pBlob, c
1f3a8 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e  onst void *z, in
1f3a9 74 20 6e 2c 20 69 6e 74 20 69 4f 66 66 73 65 74  t n, int iOffset
1f3aa 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 6c 6f 62  ){.  return blob
1f3ab 52 65 61 64 57 72 69 74 65 28 70 42 6c 6f 62 2c  ReadWrite(pBlob,
1f3ac 20 28 76 6f 69 64 20 2a 29 7a 2c 20 6e 2c 20 69   (void *)z, n, i
1f3ad 4f 66 66 73 65 74 2c 20 73 71 6c 69 74 65 33 42  Offset, sqlite3B
1f3ae 74 72 65 65 50 75 74 44 61 74 61 29 3b 0a 7d 0a  treePutData);.}.
1f3af 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 61 20 62  ./*.** Query a b
1f3b0 6c 6f 62 20 68 61 6e 64 6c 65 20 66 6f 72 20 74  lob handle for t
1f3b1 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64  he size of the d
1f3b2 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49  ata..**.** The I
1f3b3 6e 63 72 62 6c 6f 62 2e 6e 42 79 74 65 20 66 69  ncrblob.nByte fi
1f3b4 65 6c 64 20 69 73 20 66 69 78 65 64 20 66 6f 72  eld is fixed for
1f3b5 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66   the lifetime of
1f3b6 20 74 68 65 20 49 6e 63 72 62 6c 6f 62 0a 2a 2a   the Incrblob.**
1f3b7 20 73 6f 20 6e 6f 20 6d 75 74 65 78 20 69 73 20   so no mutex is 
1f3b8 72 65 71 75 69 72 65 64 20 66 6f 72 20 61 63 63  required for acc
1f3b9 65 73 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ess..*/.SQLITE_A
1f3ba 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
1f3bb 6c 6f 62 5f 62 79 74 65 73 28 73 71 6c 69 74 65  lob_bytes(sqlite
1f3bc 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a  3_blob *pBlob){.
1f3bd 20 20 49 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20    Incrblob *p = 
1f3be 28 49 6e 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f  (Incrblob *)pBlo
1f3bf 62 3b 0a 20 20 72 65 74 75 72 6e 20 70 20 3f 20  b;.  return p ? 
1f3c0 70 2d 3e 6e 42 79 74 65 20 3a 20 30 3b 0a 7d 0a  p->nByte : 0;.}.
1f3c1 0a 23 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64  .#endif /* #ifnd
1f3c2 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49  ef SQLITE_OMIT_I
1f3c3 4e 43 52 42 4c 4f 42 20 2a 2f 0a 0a 2f 2a 2a 2a  NCRBLOB */../***
1f3c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
1f3c5 6f 66 20 76 64 62 65 62 6c 6f 62 2e 63 20 2a 2a  of vdbeblob.c **
1f3c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f3c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f3c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
1f3c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
1f3ca 6e 20 66 69 6c 65 20 6a 6f 75 72 6e 61 6c 2e 63  n file journal.c
1f3cb 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1f3cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f3cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
1f3ce 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 32 32  * 2007 August 22
1f3cf 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
1f3d0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
1f3d1 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
1f3d2 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
1f3d3 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
1f3d4 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
1f3d5 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
1f3d6 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
1f3d7 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
1f3d8 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
1f3d9 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
1f3da 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
1f3db 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
1f3dc 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
1f3dd 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
1f3de 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
1f3df 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
1f3e0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
1f3e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f3e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f3e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f3e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
1f3e5 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20  **.** @(#) $Id: 
1f3e6 6a 6f 75 72 6e 61 6c 2e 63 2c 76 20 31 2e 39 20  journal.c,v 1.9 
1f3e7 32 30 30 39 2f 30 31 2f 32 30 20 31 37 3a 30 36  2009/01/20 17:06
1f3e8 3a 32 37 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :27 danielk1977 
1f3e9 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 64 65 66  Exp $.*/..#ifdef
1f3ea 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41   SQLITE_ENABLE_A
1f3eb 54 4f 4d 49 43 5f 57 52 49 54 45 0a 0a 2f 2a 0a  TOMIC_WRITE../*.
1f3ec 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70  ** This file imp
1f3ed 6c 65 6d 65 6e 74 73 20 61 20 73 70 65 63 69 61  lements a specia
1f3ee 6c 20 6b 69 6e 64 20 6f 66 20 73 71 6c 69 74 65  l kind of sqlite
1f3ef 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 20 75 73  3_file object us
1f3f0 65 64 0a 2a 2a 20 62 79 20 53 51 4c 69 74 65 20  ed.** by SQLite 
1f3f1 74 6f 20 63 72 65 61 74 65 20 6a 6f 75 72 6e 61  to create journa
1f3f2 6c 20 66 69 6c 65 73 20 69 66 20 74 68 65 20 61  l files if the a
1f3f3 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69  tomic-write opti
1f3f4 6d 69 7a 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 65  mization.** is e
1f3f5 6e 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  nabled..**.** Th
1f3f6 65 20 64 69 73 74 69 6e 63 74 69 76 65 20 63 68  e distinctive ch
1f3f7 61 72 61 63 74 65 72 69 73 74 69 63 20 6f 66 20  aracteristic of 
1f3f8 74 68 69 73 20 73 71 6c 69 74 65 33 5f 66 69 6c  this sqlite3_fil
1f3f9 65 20 69 73 20 74 68 61 74 20 74 68 65 0a 2a 2a  e is that the.**
1f3fa 20 61 63 74 75 61 6c 20 6f 6e 20 64 69 73 6b 20   actual on disk 
1f3fb 66 69 6c 65 20 69 73 20 63 72 65 61 74 65 64 20  file is created 
1f3fc 6c 61 7a 69 6c 79 2e 20 57 68 65 6e 20 74 68 65  lazily. When the
1f3fd 20 66 69 6c 65 20 69 73 20 63 72 65 61 74 65 64   file is created
1f3fe 2c 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 20  ,.** the caller 
1f3ff 73 70 65 63 69 66 69 65 73 20 61 20 62 75 66 66  specifies a buff
1f400 65 72 20 73 69 7a 65 20 66 6f 72 20 61 6e 20 69  er size for an i
1f401 6e 2d 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20  n-memory buffer 
1f402 74 6f 0a 2a 2a 20 62 65 20 75 73 65 64 20 74 6f  to.** be used to
1f403 20 73 65 72 76 69 63 65 20 72 65 61 64 28 29 20   service read() 
1f404 61 6e 64 20 77 72 69 74 65 28 29 20 72 65 71 75  and write() requ
1f405 65 73 74 73 2e 20 54 68 65 20 61 63 74 75 61 6c  ests. The actual
1f406 20 66 69 6c 65 0a 2a 2a 20 6f 6e 20 64 69 73 6b   file.** on disk
1f407 20 69 73 20 6e 6f 74 20 63 72 65 61 74 65 64 20   is not created 
1f408 6f 72 20 70 6f 70 75 6c 61 74 65 64 20 75 6e 74  or populated unt
1f409 69 6c 20 65 69 74 68 65 72 3a 0a 2a 2a 0a 2a 2a  il either:.**.**
1f40a 20 20 20 31 29 20 54 68 65 20 69 6e 2d 6d 65 6d     1) The in-mem
1f40b 6f 72 79 20 72 65 70 72 65 73 65 6e 74 61 74 69  ory representati
1f40c 6f 6e 20 67 72 6f 77 73 20 74 6f 6f 20 6c 61 72  on grows too lar
1f40d 67 65 20 66 6f 72 20 74 68 65 20 61 6c 6c 6f 63  ge for the alloc
1f40e 61 74 65 64 20 0a 2a 2a 20 20 20 20 20 20 62 75  ated .**      bu
1f40f 66 66 65 72 2c 20 6f 72 0a 2a 2a 20 20 20 32 29  ffer, or.**   2)
1f410 20 54 68 65 20 73 71 6c 69 74 65 33 4a 6f 75 72   The sqlite3Jour
1f411 6e 61 6c 43 72 65 61 74 65 28 29 20 66 75 6e 63  nalCreate() func
1f412 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a  tion is called..
1f413 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 4a 6f  */..../*.** A Jo
1f414 75 72 6e 61 6c 46 69 6c 65 20 6f 62 6a 65 63 74  urnalFile object
1f415 20 69 73 20 61 20 73 75 62 63 6c 61 73 73 20 6f   is a subclass o
1f416 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 75  f sqlite3_file u
1f417 73 65 64 20 62 79 0a 2a 2a 20 61 73 20 61 6e 20  sed by.** as an 
1f418 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65  open file handle
1f419 20 66 6f 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   for journal fil
1f41a 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4a 6f  es..*/.struct Jo
1f41b 75 72 6e 61 6c 46 69 6c 65 20 7b 0a 20 20 73 71  urnalFile {.  sq
1f41c 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
1f41d 20 2a 70 4d 65 74 68 6f 64 3b 20 20 20 20 2f 2a   *pMethod;    /*
1f41e 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 6f 6e 20   I/O methods on 
1f41f 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f  journal files */
1f420 0a 20 20 69 6e 74 20 6e 42 75 66 3b 20 20 20 20  .  int nBuf;    
1f421 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f422 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 42     /* Size of zB
1f423 75 66 5b 5d 20 69 6e 20 62 79 74 65 73 20 2a 2f  uf[] in bytes */
1f424 0a 20 20 63 68 61 72 20 2a 7a 42 75 66 3b 20 20  .  char *zBuf;  
1f425 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f426 20 20 20 2f 2a 20 53 70 61 63 65 20 74 6f 20 62     /* Space to b
1f427 75 66 66 65 72 20 6a 6f 75 72 6e 61 6c 20 77 72  uffer journal wr
1f428 69 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 53  ites */.  int iS
1f429 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ize;            
1f42a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6d 6f            /* Amo
1f42b 75 6e 74 20 6f 66 20 7a 42 75 66 5b 5d 20 63 75  unt of zBuf[] cu
1f42c 72 72 65 6e 74 6c 79 20 75 73 65 64 20 2a 2f 0a  rrently used */.
1f42d 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20    int flags;    
1f42e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f42f 20 20 2f 2a 20 78 4f 70 65 6e 20 66 6c 61 67 73    /* xOpen flags
1f430 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 66   */.  sqlite3_vf
1f431 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20  s *pVfs;        
1f432 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22 72 65        /* The "re
1f433 61 6c 22 20 75 6e 64 65 72 6c 79 69 6e 67 20 56  al" underlying V
1f434 46 53 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  FS */.  sqlite3_
1f435 66 69 6c 65 20 2a 70 52 65 61 6c 3b 20 20 20 20  file *pReal;    
1f436 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22          /* The "
1f437 72 65 61 6c 22 20 75 6e 64 65 72 6c 79 69 6e 67  real" underlying
1f438 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
1f439 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
1f43a 20 2a 7a 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20   *zJournal;     
1f43b 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
1f43c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
1f43d 65 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20  e */.};.typedef 
1f43e 73 74 72 75 63 74 20 4a 6f 75 72 6e 61 6c 46 69  struct JournalFi
1f43f 6c 65 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 3b 0a  le JournalFile;.
1f440 0a 2f 2a 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65  ./*.** If it doe
1f441 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78  s not already ex
1f442 69 73 74 73 2c 20 63 72 65 61 74 65 20 61 6e 64  ists, create and
1f443 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6f 6e   populate the on
1f444 2d 64 69 73 6b 20 66 69 6c 65 20 0a 2a 2a 20 66  -disk file .** f
1f445 6f 72 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 70  or JournalFile p
1f446 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
1f447 63 72 65 61 74 65 46 69 6c 65 28 4a 6f 75 72 6e  createFile(Journ
1f448 61 6c 46 69 6c 65 20 2a 70 29 7b 0a 20 20 69 6e  alFile *p){.  in
1f449 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
1f44a 3b 0a 20 20 69 66 28 20 21 70 2d 3e 70 52 65 61  ;.  if( !p->pRea
1f44b 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  l ){.    sqlite3
1f44c 5f 66 69 6c 65 20 2a 70 52 65 61 6c 20 3d 20 28  _file *pReal = (
1f44d 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 26  sqlite3_file *)&
1f44e 70 5b 31 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73  p[1];.    rc = s
1f44f 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 2d 3e  qlite3OsOpen(p->
1f450 70 56 66 73 2c 20 70 2d 3e 7a 4a 6f 75 72 6e 61  pVfs, p->zJourna
1f451 6c 2c 20 70 52 65 61 6c 2c 20 70 2d 3e 66 6c 61  l, pReal, p->fla
1f452 67 73 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20  gs, 0);.    if( 
1f453 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1f454 0a 20 20 20 20 20 20 70 2d 3e 70 52 65 61 6c 20  .      p->pReal 
1f455 3d 20 70 52 65 61 6c 3b 0a 20 20 20 20 20 20 69  = pReal;.      i
1f456 66 28 20 70 2d 3e 69 53 69 7a 65 3e 30 20 29 7b  f( p->iSize>0 ){
1f457 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1f458 70 2d 3e 69 53 69 7a 65 3c 3d 70 2d 3e 6e 42 75  p->iSize<=p->nBu
1f459 66 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  f);.        rc =
1f45a 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28   sqlite3OsWrite(
1f45b 70 2d 3e 70 52 65 61 6c 2c 20 70 2d 3e 7a 42 75  p->pReal, p->zBu
1f45c 66 2c 20 70 2d 3e 69 53 69 7a 65 2c 20 30 29 3b  f, p->iSize, 0);
1f45d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1f45e 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
1f45f 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74  }../*.** Close t
1f460 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74  he file..*/.stat
1f461 69 63 20 69 6e 74 20 6a 72 6e 6c 43 6c 6f 73 65  ic int jrnlClose
1f462 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70  (sqlite3_file *p
1f463 4a 66 64 29 7b 0a 20 20 4a 6f 75 72 6e 61 6c 46  Jfd){.  JournalF
1f464 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61  ile *p = (Journa
1f465 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20  lFile *)pJfd;.  
1f466 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a  if( p->pReal ){.
1f467 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f      sqlite3OsClo
1f468 73 65 28 70 2d 3e 70 52 65 61 6c 29 3b 0a 20 20  se(p->pReal);.  
1f469 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  }.  sqlite3_free
1f46a 28 70 2d 3e 7a 42 75 66 29 3b 0a 20 20 72 65 74  (p->zBuf);.  ret
1f46b 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1f46c 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74  ../*.** Read dat
1f46d 61 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 2e  a from the file.
1f46e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a  .*/.static int j
1f46f 72 6e 6c 52 65 61 64 28 0a 20 20 73 71 6c 69 74  rnlRead(.  sqlit
1f470 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20  e3_file *pJfd,  
1f471 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c    /* The journal
1f472 20 66 69 6c 65 20 66 72 6f 6d 20 77 68 69 63 68   file from which
1f473 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 76 6f   to read */.  vo
1f474 69 64 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 20  id *zBuf,       
1f475 20 20 20 20 20 2f 2a 20 50 75 74 20 74 68 65 20       /* Put the 
1f476 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a  results here */.
1f477 20 20 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20    int iAmt,     
1f478 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
1f479 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72  er of bytes to r
1f47a 65 61 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f  ead */.  sqlite_
1f47b 69 6e 74 36 34 20 69 4f 66 73 74 20 20 20 20 20  int64 iOfst     
1f47c 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67  /* Begin reading
1f47d 20 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20   at this offset 
1f47e 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  */.){.  int rc =
1f47f 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f   SQLITE_OK;.  Jo
1f480 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28  urnalFile *p = (
1f481 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a  JournalFile *)pJ
1f482 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65  fd;.  if( p->pRe
1f483 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  al ){.    rc = s
1f484 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 2d 3e  qlite3OsRead(p->
1f485 70 52 65 61 6c 2c 20 7a 42 75 66 2c 20 69 41 6d  pReal, zBuf, iAm
1f486 74 2c 20 69 4f 66 73 74 29 3b 0a 20 20 7d 65 6c  t, iOfst);.  }el
1f487 73 65 20 69 66 28 20 28 69 41 6d 74 2b 69 4f 66  se if( (iAmt+iOf
1f488 73 74 29 3e 70 2d 3e 69 53 69 7a 65 20 29 7b 0a  st)>p->iSize ){.
1f489 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1f48a 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44  IOERR_SHORT_READ
1f48b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d  ;.  }else{.    m
1f48c 65 6d 63 70 79 28 7a 42 75 66 2c 20 26 70 2d 3e  emcpy(zBuf, &p->
1f48d 7a 42 75 66 5b 69 4f 66 73 74 5d 2c 20 69 41 6d  zBuf[iOfst], iAm
1f48e 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  t);.  }.  return
1f48f 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72   rc;.}../*.** Wr
1f490 69 74 65 20 64 61 74 61 20 74 6f 20 74 68 65 20  ite data to the 
1f491 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  file..*/.static 
1f492 69 6e 74 20 6a 72 6e 6c 57 72 69 74 65 28 0a 20  int jrnlWrite(. 
1f493 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70   sqlite3_file *p
1f494 4a 66 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a  Jfd,    /* The j
1f495 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f  ournal file into
1f496 20 77 68 69 63 68 20 74 6f 20 77 72 69 74 65 20   which to write 
1f497 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  */.  const void 
1f498 2a 7a 42 75 66 2c 20 20 20 20 20 20 2f 2a 20 54  *zBuf,      /* T
1f499 61 6b 65 20 64 61 74 61 20 74 6f 20 62 65 20 77  ake data to be w
1f49a 72 69 74 74 65 6e 20 66 72 6f 6d 20 68 65 72 65  ritten from here
1f49b 20 2a 2f 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20   */.  int iAmt, 
1f49c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1f49d 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
1f49e 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 73 71  to write */.  sq
1f49f 6c 69 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 74  lite_int64 iOfst
1f4a0 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 77 72       /* Begin wr
1f4a1 69 74 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66  iting at this of
1f4a2 66 73 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69  fset into the fi
1f4a3 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  le */.){.  int r
1f4a4 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
1f4a5 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20   JournalFile *p 
1f4a6 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a  = (JournalFile *
1f4a7 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 21 70 2d  )pJfd;.  if( !p-
1f4a8 3e 70 52 65 61 6c 20 26 26 20 28 69 4f 66 73 74  >pReal && (iOfst
1f4a9 2b 69 41 6d 74 29 3e 70 2d 3e 6e 42 75 66 20 29  +iAmt)>p->nBuf )
1f4aa 7b 0a 20 20 20 20 72 63 20 3d 20 63 72 65 61 74  {.    rc = creat
1f4ab 65 46 69 6c 65 28 70 29 3b 0a 20 20 7d 0a 20 20  eFile(p);.  }.  
1f4ac 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1f4ad 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e  K ){.    if( p->
1f4ae 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 20 20 72  pReal ){.      r
1f4af 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69  c = sqlite3OsWri
1f4b0 74 65 28 70 2d 3e 70 52 65 61 6c 2c 20 7a 42 75  te(p->pReal, zBu
1f4b1 66 2c 20 69 41 6d 74 2c 20 69 4f 66 73 74 29 3b  f, iAmt, iOfst);
1f4b2 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1f4b3 20 20 6d 65 6d 63 70 79 28 26 70 2d 3e 7a 42 75    memcpy(&p->zBu
1f4b4 66 5b 69 4f 66 73 74 5d 2c 20 7a 42 75 66 2c 20  f[iOfst], zBuf, 
1f4b5 69 41 6d 74 29 3b 0a 20 20 20 20 20 20 69 66 28  iAmt);.      if(
1f4b6 20 70 2d 3e 69 53 69 7a 65 3c 28 69 4f 66 73 74   p->iSize<(iOfst
1f4b7 2b 69 41 6d 74 29 20 29 7b 0a 20 20 20 20 20 20  +iAmt) ){.      
1f4b8 20 20 70 2d 3e 69 53 69 7a 65 20 3d 20 28 69 4f    p->iSize = (iO
1f4b9 66 73 74 2b 69 41 6d 74 29 3b 0a 20 20 20 20 20  fst+iAmt);.     
1f4ba 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72   }.    }.  }.  r
1f4bb 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
1f4bc 2a 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65 20  ** Truncate the 
1f4bd 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  file..*/.static 
1f4be 69 6e 74 20 6a 72 6e 6c 54 72 75 6e 63 61 74 65  int jrnlTruncate
1f4bf 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70  (sqlite3_file *p
1f4c0 4a 66 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36  Jfd, sqlite_int6
1f4c1 34 20 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72  4 size){.  int r
1f4c2 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
1f4c3 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20   JournalFile *p 
1f4c4 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a  = (JournalFile *
1f4c5 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e  )pJfd;.  if( p->
1f4c6 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 63 20  pReal ){.    rc 
1f4c7 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63  = sqlite3OsTrunc
1f4c8 61 74 65 28 70 2d 3e 70 52 65 61 6c 2c 20 73 69  ate(p->pReal, si
1f4c9 7a 65 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ze);.  }else if(
1f4ca 20 73 69 7a 65 3c 70 2d 3e 69 53 69 7a 65 20 29   size<p->iSize )
1f4cb 7b 0a 20 20 20 20 70 2d 3e 69 53 69 7a 65 20 3d  {.    p->iSize =
1f4cc 20 73 69 7a 65 3b 0a 20 20 7d 0a 20 20 72 65 74   size;.  }.  ret
1f4cd 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
1f4ce 20 53 79 6e 63 20 74 68 65 20 66 69 6c 65 2e 0a   Sync the file..
1f4cf 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72  */.static int jr
1f4d0 6e 6c 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66  nlSync(sqlite3_f
1f4d1 69 6c 65 20 2a 70 4a 66 64 2c 20 69 6e 74 20 66  ile *pJfd, int f
1f4d2 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b  lags){.  int rc;
1f4d3 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a  .  JournalFile *
1f4d4 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65  p = (JournalFile
1f4d5 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70   *)pJfd;.  if( p
1f4d6 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 72  ->pReal ){.    r
1f4d7 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e  c = sqlite3OsSyn
1f4d8 63 28 70 2d 3e 70 52 65 61 6c 2c 20 66 6c 61 67  c(p->pReal, flag
1f4d9 73 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  s);.  }else{.   
1f4da 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
1f4db 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
1f4dc 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79  ;.}../*.** Query
1f4dd 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
1f4de 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 2e 0a   file in bytes..
1f4df 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72  */.static int jr
1f4e0 6e 6c 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74  nlFileSize(sqlit
1f4e1 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73  e3_file *pJfd, s
1f4e2 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 53 69  qlite_int64 *pSi
1f4e3 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ze){.  int rc = 
1f4e4 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75  SQLITE_OK;.  Jou
1f4e5 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a  rnalFile *p = (J
1f4e6 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66  ournalFile *)pJf
1f4e7 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61  d;.  if( p->pRea
1f4e8 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  l ){.    rc = sq
1f4e9 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28  lite3OsFileSize(
1f4ea 70 2d 3e 70 52 65 61 6c 2c 20 70 53 69 7a 65 29  p->pReal, pSize)
1f4eb 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a  ;.  }else{.    *
1f4ec 70 53 69 7a 65 20 3d 20 28 73 71 6c 69 74 65 5f  pSize = (sqlite_
1f4ed 69 6e 74 36 34 29 20 70 2d 3e 69 53 69 7a 65 3b  int64) p->iSize;
1f4ee 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
1f4ef 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 6c 65  ;.}../*.** Table
1f4f0 20 6f 66 20 6d 65 74 68 6f 64 73 20 66 6f 72 20   of methods for 
1f4f1 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 73 71 6c 69  JournalFile sqli
1f4f2 74 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e  te3_file object.
1f4f3 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63  .*/.static struc
1f4f4 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
1f4f5 68 6f 64 73 20 4a 6f 75 72 6e 61 6c 46 69 6c 65  hods JournalFile
1f4f6 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 31 2c  Methods = {.  1,
1f4f7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1f4f8 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 6a 72  iVersion */.  jr
1f4f9 6e 6c 43 6c 6f 73 65 2c 20 20 20 20 20 2f 2a 20  nlClose,     /* 
1f4fa 78 43 6c 6f 73 65 20 2a 2f 0a 20 20 6a 72 6e 6c  xClose */.  jrnl
1f4fb 52 65 61 64 2c 20 20 20 20 20 20 2f 2a 20 78 52  Read,      /* xR
1f4fc 65 61 64 20 2a 2f 0a 20 20 6a 72 6e 6c 57 72 69  ead */.  jrnlWri
1f4fd 74 65 2c 20 20 20 20 20 2f 2a 20 78 57 72 69 74  te,     /* xWrit
1f4fe 65 20 2a 2f 0a 20 20 6a 72 6e 6c 54 72 75 6e 63  e */.  jrnlTrunc
1f4ff 61 74 65 2c 20 20 2f 2a 20 78 54 72 75 6e 63 61  ate,  /* xTrunca
1f500 74 65 20 2a 2f 0a 20 20 6a 72 6e 6c 53 79 6e 63  te */.  jrnlSync
1f501 2c 20 20 20 20 20 20 2f 2a 20 78 53 79 6e 63 20  ,      /* xSync 
1f502 2a 2f 0a 20 20 6a 72 6e 6c 46 69 6c 65 53 69 7a  */.  jrnlFileSiz
1f503 65 2c 20 20 2f 2a 20 78 46 69 6c 65 53 69 7a 65  e,  /* xFileSize
1f504 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20   */.  0,        
1f505 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f       /* xLock */
1f506 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20  .  0,           
1f507 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 0a    /* xUnlock */.
1f508 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
1f509 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76   /* xCheckReserv
1f50a 65 64 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20  edLock */.  0,  
1f50b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46             /* xF
1f50c 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 0a 20 20  ileControl */.  
1f50d 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  0,             /
1f50e 2a 20 78 53 65 63 74 6f 72 53 69 7a 65 20 2a 2f  * xSectorSize */
1f50f 0a 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20  .  0            
1f510 20 20 2f 2a 20 78 44 65 76 69 63 65 43 68 61 72    /* xDeviceChar
1f511 61 63 74 65 72 69 73 74 69 63 73 20 2a 2f 0a 7d  acteristics */.}
1f512 3b 0a 0a 2f 2a 20 0a 2a 2a 20 4f 70 65 6e 20 61  ;../* .** Open a
1f513 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a   journal file..*
1f514 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1f515 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72   int sqlite3Jour
1f516 6e 61 6c 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74  nalOpen(.  sqlit
1f517 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20  e3_vfs *pVfs,   
1f518 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53        /* The VFS
1f519 20 74 6f 20 75 73 65 20 66 6f 72 20 61 63 74 75   to use for actu
1f51a 61 6c 20 66 69 6c 65 20 49 2f 4f 20 2a 2f 0a 20  al file I/O */. 
1f51b 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
1f51c 6d 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  me,         /* N
1f51d 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  ame of the journ
1f51e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 73 71 6c  al file */.  sql
1f51f 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c  ite3_file *pJfd,
1f520 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 61 6c          /* Preal
1f521 6c 6f 63 61 74 65 64 2c 20 62 6c 61 6e 6b 20 66  located, blank f
1f522 69 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20  ile handle */.  
1f523 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20  int flags,      
1f524 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70             /* Op
1f525 65 6e 69 6e 67 20 66 6c 61 67 73 20 2a 2f 0a 20  ening flags */. 
1f526 20 69 6e 74 20 6e 42 75 66 20 20 20 20 20 20 20   int nBuf       
1f527 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
1f528 79 74 65 73 20 62 75 66 66 65 72 65 64 20 62 65  ytes buffered be
1f529 66 6f 72 65 20 6f 70 65 6e 69 6e 67 20 74 68 65  fore opening the
1f52a 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 4a 6f   file */.){.  Jo
1f52b 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28  urnalFile *p = (
1f52c 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a  JournalFile *)pJ
1f52d 66 64 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20  fd;.  memset(p, 
1f52e 30 2c 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61  0, sqlite3Journa
1f52f 6c 53 69 7a 65 28 70 56 66 73 29 29 3b 0a 20 20  lSize(pVfs));.  
1f530 69 66 28 20 6e 42 75 66 3e 30 20 29 7b 0a 20 20  if( nBuf>0 ){.  
1f531 20 20 70 2d 3e 7a 42 75 66 20 3d 20 73 71 6c 69    p->zBuf = sqli
1f532 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 6e 42  te3MallocZero(nB
1f533 75 66 29 3b 0a 20 20 20 20 69 66 28 20 21 70 2d  uf);.    if( !p-
1f534 3e 7a 42 75 66 20 29 7b 0a 20 20 20 20 20 20 72  >zBuf ){.      r
1f535 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
1f536 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  EM;.    }.  }els
1f537 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71  e{.    return sq
1f538 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73  lite3OsOpen(pVfs
1f539 2c 20 7a 4e 61 6d 65 2c 20 70 4a 66 64 2c 20 66  , zName, pJfd, f
1f53a 6c 61 67 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  lags, 0);.  }.  
1f53b 70 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 4a 6f  p->pMethod = &Jo
1f53c 75 72 6e 61 6c 46 69 6c 65 4d 65 74 68 6f 64 73  urnalFileMethods
1f53d 3b 0a 20 20 70 2d 3e 6e 42 75 66 20 3d 20 6e 42  ;.  p->nBuf = nB
1f53e 75 66 3b 0a 20 20 70 2d 3e 66 6c 61 67 73 20 3d  uf;.  p->flags =
1f53f 20 66 6c 61 67 73 3b 0a 20 20 70 2d 3e 7a 4a 6f   flags;.  p->zJo
1f540 75 72 6e 61 6c 20 3d 20 7a 4e 61 6d 65 3b 0a 20  urnal = zName;. 
1f541 20 70 2d 3e 70 56 66 73 20 3d 20 70 56 66 73 3b   p->pVfs = pVfs;
1f542 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
1f543 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66  _OK;.}../*.** If
1f544 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 70 20   the argument p 
1f545 70 6f 69 6e 74 73 20 74 6f 20 61 20 4a 6f 75 72  points to a Jour
1f546 6e 61 6c 46 69 6c 65 20 73 74 72 75 63 74 75 72  nalFile structur
1f547 65 2c 20 61 6e 64 20 74 68 65 20 75 6e 64 65 72  e, and the under
1f548 6c 79 69 6e 67 0a 2a 2a 20 66 69 6c 65 20 68 61  lying.** file ha
1f549 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 63  s not yet been c
1f54a 72 65 61 74 65 64 2c 20 63 72 65 61 74 65 20 69  reated, create i
1f54b 74 20 6e 6f 77 2e 0a 2a 2f 0a 53 51 4c 49 54 45  t now..*/.SQLITE
1f54c 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1f54d 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74  ite3JournalCreat
1f54e 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
1f54f 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 70 4d 65  p){.  if( p->pMe
1f550 74 68 6f 64 73 21 3d 26 4a 6f 75 72 6e 61 6c 46  thods!=&JournalF
1f551 69 6c 65 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20  ileMethods ){.  
1f552 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1f553 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  OK;.  }.  return
1f554 20 63 72 65 61 74 65 46 69 6c 65 28 28 4a 6f 75   createFile((Jou
1f555 72 6e 61 6c 46 69 6c 65 20 2a 29 70 29 3b 0a 7d  rnalFile *)p);.}
1f556 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20  ../* .** Return 
1f557 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
1f558 74 65 73 20 72 65 71 75 69 72 65 64 20 74 6f 20  tes required to 
1f559 73 74 6f 72 65 20 61 20 4a 6f 75 72 6e 61 6c 46  store a JournalF
1f55a 69 6c 65 20 74 68 61 74 20 75 73 65 73 20 76 66  ile that uses vf
1f55b 73 0a 2a 2a 20 70 56 66 73 20 74 6f 20 63 72 65  s.** pVfs to cre
1f55c 61 74 65 20 74 68 65 20 75 6e 64 65 72 6c 79 69  ate the underlyi
1f55d 6e 67 20 6f 6e 2d 64 69 73 6b 20 66 69 6c 65 73  ng on-disk files
1f55e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1f55f 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a  ATE int sqlite3J
1f560 6f 75 72 6e 61 6c 53 69 7a 65 28 73 71 6c 69 74  ournalSize(sqlit
1f561 65 33 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a 20  e3_vfs *pVfs){. 
1f562 20 72 65 74 75 72 6e 20 28 70 56 66 73 2d 3e 73   return (pVfs->s
1f563 7a 4f 73 46 69 6c 65 2b 73 69 7a 65 6f 66 28 4a  zOsFile+sizeof(J
1f564 6f 75 72 6e 61 6c 46 69 6c 65 29 29 3b 0a 7d 0a  ournalFile));.}.
1f565 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  #endif../*******
1f566 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6a  ******* End of j
1f567 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a  ournal.c *******
1f568 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f569 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f56a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
1f56b 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
1f56c 6c 65 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63 20  le memjournal.c 
1f56d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f56e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f56f 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
1f570 30 38 20 4f 63 74 6f 62 65 72 20 37 0a 2a 2a 0a  08 October 7.**.
1f571 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
1f572 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
1f573 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
1f574 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
1f575 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
1f576 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
1f577 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
1f578 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
1f579 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
1f57a 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
1f57b 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
1f57c 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
1f57d 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
1f57e 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
1f57f 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
1f580 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
1f581 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
1f582 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
1f583 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f584 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f585 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f586 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
1f587 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
1f588 61 69 6e 73 20 63 6f 64 65 20 75 73 65 20 74 6f  ains code use to
1f589 20 69 6d 70 6c 65 6d 65 6e 74 20 61 6e 20 69 6e   implement an in
1f58a 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b  -memory rollback
1f58b 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65   journal..** The
1f58c 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c 62   in-memory rollb
1f58d 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 75  ack journal is u
1f58e 73 65 64 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 74  sed to journal t
1f58f 72 61 6e 73 61 63 74 69 6f 6e 73 20 66 6f 72 0a  ransactions for.
1f590 2a 2a 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 64 61  ** ":memory:" da
1f591 74 61 62 61 73 65 73 20 61 6e 64 20 77 68 65 6e  tabases and when
1f592 20 74 68 65 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64   the journal_mod
1f593 65 3d 4d 45 4d 4f 52 59 20 70 72 61 67 6d 61 20  e=MEMORY pragma 
1f594 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 40  is used..**.** @
1f595 28 23 29 20 24 49 64 3a 20 6d 65 6d 6a 6f 75 72  (#) $Id: memjour
1f596 6e 61 6c 2e 63 2c 76 20 31 2e 31 32 20 32 30 30  nal.c,v 1.12 200
1f597 39 2f 30 35 2f 30 34 20 31 31 3a 34 32 3a 33 30  9/05/04 11:42:30
1f598 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70   danielk1977 Exp
1f599 20 24 0a 2a 2f 0a 0a 2f 2a 20 46 6f 72 77 61 72   $.*/../* Forwar
1f59a 64 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20  d references to 
1f59b 69 6e 74 65 72 6e 61 6c 20 73 74 72 75 63 74 75  internal structu
1f59c 72 65 73 20 2a 2f 0a 74 79 70 65 64 65 66 20 73  res */.typedef s
1f59d 74 72 75 63 74 20 4d 65 6d 4a 6f 75 72 6e 61 6c  truct MemJournal
1f59e 20 4d 65 6d 4a 6f 75 72 6e 61 6c 3b 0a 74 79 70   MemJournal;.typ
1f59f 65 64 65 66 20 73 74 72 75 63 74 20 46 69 6c 65  edef struct File
1f5a0 50 6f 69 6e 74 20 46 69 6c 65 50 6f 69 6e 74 3b  Point FilePoint;
1f5a1 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
1f5a2 46 69 6c 65 43 68 75 6e 6b 20 46 69 6c 65 43 68  FileChunk FileCh
1f5a3 75 6e 6b 3b 0a 0a 2f 2a 20 53 70 61 63 65 20 74  unk;../* Space t
1f5a4 6f 20 68 6f 6c 64 20 74 68 65 20 72 6f 6c 6c 62  o hold the rollb
1f5a5 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61  ack journal is a
1f5a6 6c 6c 6f 63 61 74 65 64 20 69 6e 20 69 6e 63 72  llocated in incr
1f5a7 65 6d 65 6e 74 73 20 6f 66 0a 2a 2a 20 74 68 69  ements of.** thi
1f5a8 73 20 6d 61 6e 79 20 62 79 74 65 73 2e 0a 2a 2a  s many bytes..**
1f5a9 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 63 68 6f  .** The size cho
1f5aa 73 65 6e 20 69 73 20 61 20 6c 69 74 74 6c 65 20  sen is a little 
1f5ab 6c 65 73 73 20 74 68 61 6e 20 61 20 70 6f 77 65  less than a powe
1f5ac 72 20 6f 66 20 74 77 6f 2e 20 20 54 68 61 74 20  r of two.  That 
1f5ad 77 61 79 2c 0a 2a 2a 20 74 68 65 20 46 69 6c 65  way,.** the File
1f5ae 43 68 75 6e 6b 20 6f 62 6a 65 63 74 20 77 69 6c  Chunk object wil
1f5af 6c 20 68 61 76 65 20 61 20 73 69 7a 65 20 74 68  l have a size th
1f5b0 61 74 20 61 6c 6d 6f 73 74 20 65 78 61 63 74 6c  at almost exactl
1f5b1 79 20 66 69 6c 6c 73 0a 2a 2a 20 61 20 70 6f 77  y fills.** a pow
1f5b2 65 72 2d 6f 66 2d 74 77 6f 20 61 6c 6c 6f 63 61  er-of-two alloca
1f5b3 74 69 6f 6e 2e 20 20 54 68 69 73 20 6d 69 6d 69  tion.  This mimi
1f5b4 6d 69 7a 65 73 20 77 61 73 74 65 64 20 73 70 61  mizes wasted spa
1f5b5 63 65 20 69 6e 20 70 6f 77 65 72 2d 6f 66 2d 74  ce in power-of-t
1f5b6 77 6f 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c  wo.** memory all
1f5b7 6f 63 61 74 6f 72 73 2e 0a 2a 2f 0a 23 64 65 66  ocators..*/.#def
1f5b8 69 6e 65 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e  ine JOURNAL_CHUN
1f5b9 4b 53 49 5a 45 20 28 28 69 6e 74 29 28 31 30 32  KSIZE ((int)(102
1f5ba 34 2d 73 69 7a 65 6f 66 28 46 69 6c 65 43 68 75  4-sizeof(FileChu
1f5bb 6e 6b 2a 29 29 29 0a 0a 2f 2a 20 4d 61 63 72 6f  nk*)))../* Macro
1f5bc 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6d 69 6e   to find the min
1f5bd 69 6d 75 6d 20 6f 66 20 74 77 6f 20 6e 75 6d 65  imum of two nume
1f5be 72 69 63 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 23  ric values..*/.#
1f5bf 69 66 6e 64 65 66 20 4d 49 4e 0a 23 20 64 65 66  ifndef MIN.# def
1f5c0 69 6e 65 20 4d 49 4e 28 78 2c 79 29 20 28 28 78  ine MIN(x,y) ((x
1f5c1 29 3c 28 79 29 3f 28 78 29 3a 28 79 29 29 0a 23  )<(y)?(x):(y)).#
1f5c2 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
1f5c3 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61   rollback journa
1f5c4 6c 20 69 73 20 63 6f 6d 70 6f 73 65 64 20 6f 66  l is composed of
1f5c5 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f   a linked list o
1f5c6 66 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72  f these structur
1f5c7 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 69  es..*/.struct Fi
1f5c8 6c 65 43 68 75 6e 6b 20 7b 0a 20 20 46 69 6c 65  leChunk {.  File
1f5c9 43 68 75 6e 6b 20 2a 70 4e 65 78 74 3b 20 20 20  Chunk *pNext;   
1f5ca 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
1f5cb 65 78 74 20 63 68 75 6e 6b 20 69 6e 20 74 68 65  ext chunk in the
1f5cc 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 38   journal */.  u8
1f5cd 20 7a 43 68 75 6e 6b 5b 4a 4f 55 52 4e 41 4c 5f   zChunk[JOURNAL_
1f5ce 43 48 55 4e 4b 53 49 5a 45 5d 3b 20 20 20 2f 2a  CHUNKSIZE];   /*
1f5cf 20 43 6f 6e 74 65 6e 74 20 6f 66 20 74 68 69 73   Content of this
1f5d0 20 63 68 75 6e 6b 20 2a 2f 0a 7d 3b 0a 0a 2f 2a   chunk */.};../*
1f5d1 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
1f5d2 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 73  of this object s
1f5d3 65 72 76 65 73 20 61 73 20 61 20 63 75 72 73 6f  erves as a curso
1f5d4 72 20 69 6e 74 6f 20 74 68 65 20 72 6f 6c 6c 62  r into the rollb
1f5d5 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20  ack journal..** 
1f5d6 54 68 65 20 63 75 72 73 6f 72 20 63 61 6e 20 62  The cursor can b
1f5d7 65 20 65 69 74 68 65 72 20 66 6f 72 20 72 65 61  e either for rea
1f5d8 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 2e  ding or writing.
1f5d9 0a 2a 2f 0a 73 74 72 75 63 74 20 46 69 6c 65 50  .*/.struct FileP
1f5da 6f 69 6e 74 20 7b 0a 20 20 73 71 6c 69 74 65 33  oint {.  sqlite3
1f5db 5f 69 6e 74 36 34 20 69 4f 66 66 73 65 74 3b 20  _int64 iOffset; 
1f5dc 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73           /* Offs
1f5dd 65 74 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69  et from the begi
1f5de 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 66 69 6c  nning of the fil
1f5df 65 20 2a 2f 0a 20 20 46 69 6c 65 43 68 75 6e 6b  e */.  FileChunk
1f5e0 20 2a 70 43 68 75 6e 6b 3b 20 20 20 20 20 20 20   *pChunk;       
1f5e1 20 20 20 20 20 20 20 2f 2a 20 53 70 65 63 69 66         /* Specif
1f5e2 69 63 20 63 68 75 6e 6b 20 69 6e 74 6f 20 77 68  ic chunk into wh
1f5e3 69 63 68 20 63 75 72 73 6f 72 20 70 6f 69 6e 74  ich cursor point
1f5e4 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54  s */.};../*.** T
1f5e5 68 69 73 20 73 75 62 63 6c 61 73 73 20 69 73 20  his subclass is 
1f5e6 61 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71  a subclass of sq
1f5e7 6c 69 74 65 33 5f 66 69 6c 65 2e 20 20 45 61 63  lite3_file.  Eac
1f5e8 68 20 6f 70 65 6e 20 6d 65 6d 6f 72 79 2d 6a 6f  h open memory-jo
1f5e9 75 72 6e 61 6c 0a 2a 2a 20 69 73 20 61 6e 20 69  urnal.** is an i
1f5ea 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20  nstance of this 
1f5eb 63 6c 61 73 73 2e 0a 2a 2f 0a 73 74 72 75 63 74  class..*/.struct
1f5ec 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 7b 0a 20 20   MemJournal {.  
1f5ed 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
1f5ee 64 73 20 2a 70 4d 65 74 68 6f 64 3b 20 20 20 20  ds *pMethod;    
1f5ef 2f 2a 20 50 61 72 65 6e 74 20 63 6c 61 73 73 2e  /* Parent class.
1f5f0 20 4d 55 53 54 20 42 45 20 46 49 52 53 54 20 2a   MUST BE FIRST *
1f5f1 2f 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70  /.  FileChunk *p
1f5f2 46 69 72 73 74 3b 20 20 20 20 20 20 20 20 20 20  First;          
1f5f3 20 20 20 20 2f 2a 20 48 65 61 64 20 6f 66 20 69      /* Head of i
1f5f4 6e 2d 6d 65 6d 6f 72 79 20 63 68 75 6e 6b 2d 6c  n-memory chunk-l
1f5f5 69 73 74 20 2a 2f 0a 20 20 46 69 6c 65 50 6f 69  ist */.  FilePoi
1f5f6 6e 74 20 65 6e 64 70 6f 69 6e 74 3b 20 20 20 20  nt endpoint;    
1f5f7 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
1f5f8 74 65 72 20 74 6f 20 74 68 65 20 65 6e 64 20 6f  ter to the end o
1f5f9 66 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20  f the file */.  
1f5fa 46 69 6c 65 50 6f 69 6e 74 20 72 65 61 64 70 6f  FilePoint readpo
1f5fb 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  int;            
1f5fc 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68  /* Pointer to th
1f5fd 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6c 61 73  e end of the las
1f5fe 74 20 78 52 65 61 64 28 29 20 2a 2f 0a 7d 3b 0a  t xRead() */.};.
1f5ff 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61  ./*.** Read data
1f600 20 66 72 6f 6d 20 74 68 65 20 69 6e 2d 6d 65 6d   from the in-mem
1f601 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ory journal file
1f602 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20 69  .  This is the i
1f603 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a  mplementation.**
1f604 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   of the sqlite3_
1f605 76 66 73 2e 78 52 65 61 64 20 6d 65 74 68 6f 64  vfs.xRead method
1f606 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
1f607 6d 65 6d 6a 72 6e 6c 52 65 61 64 28 0a 20 20 73  memjrnlRead(.  s
1f608 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66  qlite3_file *pJf
1f609 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75  d,    /* The jou
1f60a 72 6e 61 6c 20 66 69 6c 65 20 66 72 6f 6d 20 77  rnal file from w
1f60b 68 69 63 68 20 74 6f 20 72 65 61 64 20 2a 2f 0a  hich to read */.
1f60c 20 20 76 6f 69 64 20 2a 7a 42 75 66 2c 20 20 20    void *zBuf,   
1f60d 20 20 20 20 20 20 20 20 20 2f 2a 20 50 75 74 20           /* Put 
1f60e 74 68 65 20 72 65 73 75 6c 74 73 20 68 65 72 65  the results here
1f60f 20 2a 2f 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20   */.  int iAmt, 
1f610 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1f611 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
1f612 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 73 71 6c  to read */.  sql
1f613 69 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 74 20  ite_int64 iOfst 
1f614 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 61      /* Begin rea
1f615 64 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66  ding at this off
1f616 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 4a  set */.){.  MemJ
1f617 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d  ournal *p = (Mem
1f618 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a  Journal *)pJfd;.
1f619 20 20 75 38 20 2a 7a 4f 75 74 20 3d 20 7a 42 75    u8 *zOut = zBu
1f61a 66 3b 0a 20 20 69 6e 74 20 6e 52 65 61 64 20 3d  f;.  int nRead =
1f61b 20 69 41 6d 74 3b 0a 20 20 69 6e 74 20 69 43 68   iAmt;.  int iCh
1f61c 75 6e 6b 4f 66 66 73 65 74 3b 0a 20 20 46 69 6c  unkOffset;.  Fil
1f61d 65 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b 0a  eChunk *pChunk;.
1f61e 0a 20 20 2f 2a 20 53 51 4c 69 74 65 20 6e 65 76  .  /* SQLite nev
1f61f 65 72 20 74 72 69 65 73 20 74 6f 20 72 65 61 64  er tries to read
1f620 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66   past the end of
1f621 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72   a rollback jour
1f622 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 61 73  nal file */.  as
1f623 73 65 72 74 28 20 69 4f 66 73 74 2b 69 41 6d 74  sert( iOfst+iAmt
1f624 3c 3d 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f  <=p->endpoint.iO
1f625 66 66 73 65 74 20 29 3b 0a 0a 20 20 69 66 28 20  ffset );..  if( 
1f626 70 2d 3e 72 65 61 64 70 6f 69 6e 74 2e 69 4f 66  p->readpoint.iOf
1f627 66 73 65 74 21 3d 69 4f 66 73 74 20 7c 7c 20 69  fset!=iOfst || i
1f628 4f 66 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73  Ofst==0 ){.    s
1f629 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4f 66  qlite3_int64 iOf
1f62a 66 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 70  f = 0;.    for(p
1f62b 43 68 75 6e 6b 3d 70 2d 3e 70 46 69 72 73 74 3b  Chunk=p->pFirst;
1f62c 20 0a 20 20 20 20 20 20 20 20 41 4c 57 41 59 53   .        ALWAYS
1f62d 28 70 43 68 75 6e 6b 29 20 26 26 20 28 69 4f 66  (pChunk) && (iOf
1f62e 66 2b 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53  f+JOURNAL_CHUNKS
1f62f 49 5a 45 29 3c 3d 69 4f 66 73 74 3b 0a 20 20 20  IZE)<=iOfst;.   
1f630 20 20 20 20 20 70 43 68 75 6e 6b 3d 70 43 68 75       pChunk=pChu
1f631 6e 6b 2d 3e 70 4e 65 78 74 0a 20 20 20 20 29 7b  nk->pNext.    ){
1f632 0a 20 20 20 20 20 20 69 4f 66 66 20 2b 3d 20 4a  .      iOff += J
1f633 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45  OURNAL_CHUNKSIZE
1f634 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
1f635 0a 20 20 20 20 70 43 68 75 6e 6b 20 3d 20 70 2d  .    pChunk = p-
1f636 3e 72 65 61 64 70 6f 69 6e 74 2e 70 43 68 75 6e  >readpoint.pChun
1f637 6b 3b 0a 20 20 7d 0a 0a 20 20 69 43 68 75 6e 6b  k;.  }..  iChunk
1f638 4f 66 66 73 65 74 20 3d 20 28 69 6e 74 29 28 69  Offset = (int)(i
1f639 4f 66 73 74 25 4a 4f 55 52 4e 41 4c 5f 43 48 55  Ofst%JOURNAL_CHU
1f63a 4e 4b 53 49 5a 45 29 3b 0a 20 20 64 6f 20 7b 0a  NKSIZE);.  do {.
1f63b 20 20 20 20 69 6e 74 20 69 53 70 61 63 65 20 3d      int iSpace =
1f63c 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49   JOURNAL_CHUNKSI
1f63d 5a 45 20 2d 20 69 43 68 75 6e 6b 4f 66 66 73 65  ZE - iChunkOffse
1f63e 74 3b 0a 20 20 20 20 69 6e 74 20 6e 43 6f 70 79  t;.    int nCopy
1f63f 20 3d 20 4d 49 4e 28 6e 52 65 61 64 2c 20 28 4a   = MIN(nRead, (J
1f640 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45  OURNAL_CHUNKSIZE
1f641 20 2d 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 29   - iChunkOffset)
1f642 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 4f  );.    memcpy(zO
1f643 75 74 2c 20 26 70 43 68 75 6e 6b 2d 3e 7a 43 68  ut, &pChunk->zCh
1f644 75 6e 6b 5b 69 43 68 75 6e 6b 4f 66 66 73 65 74  unk[iChunkOffset
1f645 5d 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 20 20 7a  ], nCopy);.    z
1f646 4f 75 74 20 2b 3d 20 6e 43 6f 70 79 3b 0a 20 20  Out += nCopy;.  
1f647 20 20 6e 52 65 61 64 20 2d 3d 20 69 53 70 61 63    nRead -= iSpac
1f648 65 3b 0a 20 20 20 20 69 43 68 75 6e 6b 4f 66 66  e;.    iChunkOff
1f649 73 65 74 20 3d 20 30 3b 0a 20 20 7d 20 77 68 69  set = 0;.  } whi
1f64a 6c 65 28 20 6e 52 65 61 64 3e 3d 30 20 26 26 20  le( nRead>=0 && 
1f64b 28 70 43 68 75 6e 6b 3d 70 43 68 75 6e 6b 2d 3e  (pChunk=pChunk->
1f64c 70 4e 65 78 74 29 21 3d 30 20 26 26 20 6e 52 65  pNext)!=0 && nRe
1f64d 61 64 3e 30 20 29 3b 0a 20 20 70 2d 3e 72 65 61  ad>0 );.  p->rea
1f64e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 3d  dpoint.iOffset =
1f64f 20 69 4f 66 73 74 2b 69 41 6d 74 3b 0a 20 20 70   iOfst+iAmt;.  p
1f650 2d 3e 72 65 61 64 70 6f 69 6e 74 2e 70 43 68 75  ->readpoint.pChu
1f651 6e 6b 20 3d 20 70 43 68 75 6e 6b 3b 0a 0a 20 20  nk = pChunk;..  
1f652 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1f653 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  ;.}../*.** Write
1f654 20 64 61 74 61 20 74 6f 20 74 68 65 20 66 69 6c   data to the fil
1f655 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
1f656 20 6d 65 6d 6a 72 6e 6c 57 72 69 74 65 28 0a 20   memjrnlWrite(. 
1f657 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70   sqlite3_file *p
1f658 4a 66 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a  Jfd,    /* The j
1f659 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f  ournal file into
1f65a 20 77 68 69 63 68 20 74 6f 20 77 72 69 74 65 20   which to write 
1f65b 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  */.  const void 
1f65c 2a 7a 42 75 66 2c 20 20 20 20 20 20 2f 2a 20 54  *zBuf,      /* T
1f65d 61 6b 65 20 64 61 74 61 20 74 6f 20 62 65 20 77  ake data to be w
1f65e 72 69 74 74 65 6e 20 66 72 6f 6d 20 68 65 72 65  ritten from here
1f65f 20 2a 2f 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20   */.  int iAmt, 
1f660 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1f661 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
1f662 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 73 71  to write */.  sq
1f663 6c 69 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 74  lite_int64 iOfst
1f664 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 77 72       /* Begin wr
1f665 69 74 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66  iting at this of
1f666 66 73 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69  fset into the fi
1f667 6c 65 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 4a 6f  le */.){.  MemJo
1f668 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a  urnal *p = (MemJ
1f669 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20  ournal *)pJfd;. 
1f66a 20 69 6e 74 20 6e 57 72 69 74 65 20 3d 20 69 41   int nWrite = iA
1f66b 6d 74 3b 0a 20 20 75 38 20 2a 7a 57 72 69 74 65  mt;.  u8 *zWrite
1f66c 20 3d 20 28 75 38 20 2a 29 7a 42 75 66 3b 0a 0a   = (u8 *)zBuf;..
1f66d 20 20 2f 2a 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72    /* An in-memor
1f66e 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73  y journal file s
1f66f 68 6f 75 6c 64 20 6f 6e 6c 79 20 65 76 65 72 20  hould only ever 
1f670 62 65 20 61 70 70 65 6e 64 65 64 20 74 6f 2e 20  be appended to. 
1f671 52 61 6e 64 6f 6d 0a 20 20 2a 2a 20 61 63 63 65  Random.  ** acce
1f672 73 73 20 77 72 69 74 65 73 20 61 72 65 20 6e 6f  ss writes are no
1f673 74 20 72 65 71 75 69 72 65 64 20 62 79 20 73 71  t required by sq
1f674 6c 69 74 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  lite..  */.  ass
1f675 65 72 74 28 20 69 4f 66 73 74 3d 3d 70 2d 3e 65  ert( iOfst==p->e
1f676 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20  ndpoint.iOffset 
1f677 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
1f678 4d 45 54 45 52 28 69 4f 66 73 74 29 3b 0a 0a 20  METER(iOfst);.. 
1f679 20 77 68 69 6c 65 28 20 6e 57 72 69 74 65 3e 30   while( nWrite>0
1f67a 20 29 7b 0a 20 20 20 20 46 69 6c 65 43 68 75 6e   ){.    FileChun
1f67b 6b 20 2a 70 43 68 75 6e 6b 20 3d 20 70 2d 3e 65  k *pChunk = p->e
1f67c 6e 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 3b 0a  ndpoint.pChunk;.
1f67d 20 20 20 20 69 6e 74 20 69 43 68 75 6e 6b 4f 66      int iChunkOf
1f67e 66 73 65 74 20 3d 20 28 69 6e 74 29 28 70 2d 3e  fset = (int)(p->
1f67f 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74  endpoint.iOffset
1f680 25 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49  %JOURNAL_CHUNKSI
1f681 5a 45 29 3b 0a 20 20 20 20 69 6e 74 20 69 53 70  ZE);.    int iSp
1f682 61 63 65 20 3d 20 4d 49 4e 28 6e 57 72 69 74 65  ace = MIN(nWrite
1f683 2c 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53  , JOURNAL_CHUNKS
1f684 49 5a 45 20 2d 20 69 43 68 75 6e 6b 4f 66 66 73  IZE - iChunkOffs
1f685 65 74 29 3b 0a 0a 20 20 20 20 69 66 28 20 69 43  et);..    if( iC
1f686 68 75 6e 6b 4f 66 66 73 65 74 3d 3d 30 20 29 7b  hunkOffset==0 ){
1f687 0a 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 63 68  .      /* New ch
1f688 75 6e 6b 20 69 73 20 72 65 71 75 69 72 65 64 20  unk is required 
1f689 74 6f 20 65 78 74 65 6e 64 20 74 68 65 20 66 69  to extend the fi
1f68a 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 46 69 6c  le. */.      Fil
1f68b 65 43 68 75 6e 6b 20 2a 70 4e 65 77 20 3d 20 73  eChunk *pNew = s
1f68c 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
1f68d 7a 65 6f 66 28 46 69 6c 65 43 68 75 6e 6b 29 29  zeof(FileChunk))
1f68e 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 4e 65  ;.      if( !pNe
1f68f 77 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  w ){.        ret
1f690 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
1f691 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a  _NOMEM;.      }.
1f692 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4e 65 78        pNew->pNex
1f693 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28  t = 0;.      if(
1f694 20 70 43 68 75 6e 6b 20 29 7b 0a 20 20 20 20 20   pChunk ){.     
1f695 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 46     assert( p->pF
1f696 69 72 73 74 20 29 3b 0a 20 20 20 20 20 20 20 20  irst );.        
1f697 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 20 3d 20  pChunk->pNext = 
1f698 70 4e 65 77 3b 0a 20 20 20 20 20 20 7d 65 6c 73  pNew;.      }els
1f699 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  e{.        asser
1f69a 74 28 20 21 70 2d 3e 70 46 69 72 73 74 20 29 3b  t( !p->pFirst );
1f69b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 46 69 72  .        p->pFir
1f69c 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20  st = pNew;.     
1f69d 20 7d 0a 20 20 20 20 20 20 70 2d 3e 65 6e 64 70   }.      p->endp
1f69e 6f 69 6e 74 2e 70 43 68 75 6e 6b 20 3d 20 70 4e  oint.pChunk = pN
1f69f 65 77 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6d  ew;.    }..    m
1f6a0 65 6d 63 70 79 28 26 70 2d 3e 65 6e 64 70 6f 69  emcpy(&p->endpoi
1f6a1 6e 74 2e 70 43 68 75 6e 6b 2d 3e 7a 43 68 75 6e  nt.pChunk->zChun
1f6a2 6b 5b 69 43 68 75 6e 6b 4f 66 66 73 65 74 5d 2c  k[iChunkOffset],
1f6a3 20 7a 57 72 69 74 65 2c 20 69 53 70 61 63 65 29   zWrite, iSpace)
1f6a4 3b 0a 20 20 20 20 7a 57 72 69 74 65 20 2b 3d 20  ;.    zWrite += 
1f6a5 69 53 70 61 63 65 3b 0a 20 20 20 20 6e 57 72 69  iSpace;.    nWri
1f6a6 74 65 20 2d 3d 20 69 53 70 61 63 65 3b 0a 20 20  te -= iSpace;.  
1f6a7 20 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f    p->endpoint.iO
1f6a8 66 66 73 65 74 20 2b 3d 20 69 53 70 61 63 65 3b  ffset += iSpace;
1f6a9 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53  .  }..  return S
1f6aa 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
1f6ab 2a 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65 20  ** Truncate the 
1f6ac 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  file..*/.static 
1f6ad 69 6e 74 20 6d 65 6d 6a 72 6e 6c 54 72 75 6e 63  int memjrnlTrunc
1f6ae 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ate(sqlite3_file
1f6af 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74 65 5f 69   *pJfd, sqlite_i
1f6b0 6e 74 36 34 20 73 69 7a 65 29 7b 0a 20 20 4d 65  nt64 size){.  Me
1f6b1 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d  mJournal *p = (M
1f6b2 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64  emJournal *)pJfd
1f6b3 3b 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70  ;.  FileChunk *p
1f6b4 43 68 75 6e 6b 3b 0a 20 20 61 73 73 65 72 74 28  Chunk;.  assert(
1f6b5 73 69 7a 65 3d 3d 30 29 3b 0a 20 20 55 4e 55 53  size==0);.  UNUS
1f6b6 45 44 5f 50 41 52 41 4d 45 54 45 52 28 73 69 7a  ED_PARAMETER(siz
1f6b7 65 29 3b 0a 20 20 70 43 68 75 6e 6b 20 3d 20 70  e);.  pChunk = p
1f6b8 2d 3e 70 46 69 72 73 74 3b 0a 20 20 77 68 69 6c  ->pFirst;.  whil
1f6b9 65 28 20 70 43 68 75 6e 6b 20 29 7b 0a 20 20 20  e( pChunk ){.   
1f6ba 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 54 6d 70   FileChunk *pTmp
1f6bb 20 3d 20 70 43 68 75 6e 6b 3b 0a 20 20 20 20 70   = pChunk;.    p
1f6bc 43 68 75 6e 6b 20 3d 20 70 43 68 75 6e 6b 2d 3e  Chunk = pChunk->
1f6bd 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74  pNext;.    sqlit
1f6be 65 33 5f 66 72 65 65 28 70 54 6d 70 29 3b 0a 20  e3_free(pTmp);. 
1f6bf 20 7d 0a 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a   }.  sqlite3MemJ
1f6c0 6f 75 72 6e 61 6c 4f 70 65 6e 28 70 4a 66 64 29  ournalOpen(pJfd)
1f6c1 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
1f6c2 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  E_OK;.}../*.** C
1f6c3 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 2e 0a 2a  lose the file..*
1f6c4 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d  /.static int mem
1f6c5 6a 72 6e 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65  jrnlClose(sqlite
1f6c6 33 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a 20  3_file *pJfd){. 
1f6c7 20 6d 65 6d 6a 72 6e 6c 54 72 75 6e 63 61 74 65   memjrnlTruncate
1f6c8 28 70 4a 66 64 2c 20 30 29 3b 0a 20 20 72 65 74  (pJfd, 0);.  ret
1f6c9 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1f6ca 0a 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68  .../*.** Sync th
1f6cb 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 79  e file..**.** Sy
1f6cc 6e 63 69 6e 67 20 61 6e 20 69 6e 2d 6d 65 6d 6f  ncing an in-memo
1f6cd 72 79 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 20  ry journal is a 
1f6ce 6e 6f 2d 6f 70 2e 20 20 41 6e 64 2c 20 69 6e 20  no-op.  And, in 
1f6cf 66 61 63 74 2c 20 74 68 69 73 20 72 6f 75 74 69  fact, this routi
1f6d0 6e 65 0a 2a 2a 20 69 73 20 6e 65 76 65 72 20 63  ne.** is never c
1f6d1 61 6c 6c 65 64 20 69 6e 20 61 20 77 6f 72 6b 69  alled in a worki
1f6d2 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ng implementatio
1f6d3 6e 2e 20 20 54 68 69 73 20 69 6d 70 6c 65 6d 65  n.  This impleme
1f6d4 6e 74 61 74 69 6f 6e 0a 2a 2a 20 65 78 69 73 74  ntation.** exist
1f6d5 73 20 70 75 72 65 6c 79 20 61 73 20 61 20 63 6f  s purely as a co
1f6d6 6e 74 69 6e 67 65 6e 63 79 2c 20 69 6e 20 63 61  ntingency, in ca
1f6d7 73 65 20 73 6f 6d 65 20 6d 61 6c 66 75 6e 63 74  se some malfunct
1f6d8 69 6f 6e 20 69 6e 20 73 6f 6d 65 20 6f 74 68 65  ion in some othe
1f6d9 72 0a 2a 2a 20 70 61 72 74 20 6f 66 20 53 51 4c  r.** part of SQL
1f6da 69 74 65 20 63 61 75 73 65 73 20 53 79 6e 63 20  ite causes Sync 
1f6db 74 6f 20 62 65 20 63 61 6c 6c 65 64 20 62 79 20  to be called by 
1f6dc 6d 69 73 74 61 6b 65 2e 0a 2a 2f 0a 73 74 61 74  mistake..*/.stat
1f6dd 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 53 79  ic int memjrnlSy
1f6de 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  nc(sqlite3_file 
1f6df 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 4e 6f  *NotUsed, int No
1f6e0 74 55 73 65 64 32 29 7b 20 20 20 2f 2a 4e 4f 5f  tUsed2){   /*NO_
1f6e1 54 45 53 54 2a 2f 0a 20 20 55 4e 55 53 45 44 5f  TEST*/.  UNUSED_
1f6e2 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73  PARAMETER2(NotUs
1f6e3 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 20 20  ed, NotUsed2);  
1f6e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f6e5 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a        /*NO_TEST*
1f6e6 2f 0a 20 20 61 73 73 65 72 74 28 20 30 20 29 3b  /.  assert( 0 );
1f6e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f6e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f6e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f6ea 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 20 20 72   /*NO_TEST*/.  r
1f6eb 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1f6ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f6ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f6ee 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f              /*NO
1f6ef 5f 54 45 53 54 2a 2f 0a 7d 20 20 20 20 20 20 20  _TEST*/.}       
1f6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f6f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f6f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f6f3 20 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54         /*NO_TEST
1f6f4 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20  */../*.** Query 
1f6f5 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
1f6f6 66 69 6c 65 20 69 6e 20 62 79 74 65 73 2e 0a 2a  file in bytes..*
1f6f7 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d  /.static int mem
1f6f8 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 28 73 71 6c  jrnlFileSize(sql
1f6f9 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c  ite3_file *pJfd,
1f6fa 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70   sqlite_int64 *p
1f6fb 53 69 7a 65 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72  Size){.  MemJour
1f6fc 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75  nal *p = (MemJou
1f6fd 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 2a  rnal *)pJfd;.  *
1f6fe 70 53 69 7a 65 20 3d 20 28 73 71 6c 69 74 65 5f  pSize = (sqlite_
1f6ff 69 6e 74 36 34 29 20 70 2d 3e 65 6e 64 70 6f 69  int64) p->endpoi
1f700 6e 74 2e 69 4f 66 66 73 65 74 3b 0a 20 20 72 65  nt.iOffset;.  re
1f701 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1f702 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 6c 65 20 6f  }../*.** Table o
1f703 66 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 4d 65  f methods for Me
1f704 6d 4a 6f 75 72 6e 61 6c 20 73 71 6c 69 74 65 33  mJournal sqlite3
1f705 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f  _file object..*/
1f706 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 73  .static struct s
1f707 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
1f708 73 20 4d 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74 68  s MemJournalMeth
1f709 6f 64 73 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20  ods = {.  1,    
1f70a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69              /* i
1f70b 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 6d 65 6d  Version */.  mem
1f70c 6a 72 6e 6c 43 6c 6f 73 65 2c 20 20 20 20 20 2f  jrnlClose,     /
1f70d 2a 20 78 43 6c 6f 73 65 20 2a 2f 0a 20 20 6d 65  * xClose */.  me
1f70e 6d 6a 72 6e 6c 52 65 61 64 2c 20 20 20 20 20 20  mjrnlRead,      
1f70f 2f 2a 20 78 52 65 61 64 20 2a 2f 0a 20 20 6d 65  /* xRead */.  me
1f710 6d 6a 72 6e 6c 57 72 69 74 65 2c 20 20 20 20 20  mjrnlWrite,     
1f711 2f 2a 20 78 57 72 69 74 65 20 2a 2f 0a 20 20 6d  /* xWrite */.  m
1f712 65 6d 6a 72 6e 6c 54 72 75 6e 63 61 74 65 2c 20  emjrnlTruncate, 
1f713 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 2a 2f   /* xTruncate */
1f714 0a 20 20 6d 65 6d 6a 72 6e 6c 53 79 6e 63 2c 20  .  memjrnlSync, 
1f715 20 20 20 20 20 2f 2a 20 78 53 79 6e 63 20 2a 2f       /* xSync */
1f716 0a 20 20 6d 65 6d 6a 72 6e 6c 46 69 6c 65 53 69  .  memjrnlFileSi
1f717 7a 65 2c 20 20 2f 2a 20 78 46 69 6c 65 53 69 7a  ze,  /* xFileSiz
1f718 65 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20  e */.  0,       
1f719 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63           /* xLoc
1f71a 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20  k */.  0,       
1f71b 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c           /* xUnl
1f71c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20  ock */.  0,     
1f71d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43             /* xC
1f71e 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
1f71f 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20   */.  0,        
1f720 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6c 65          /* xFile
1f721 43 6f 6e 74 72 6f 6c 20 2a 2f 0a 20 20 30 2c 20  Control */.  0, 
1f722 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1f723 2a 20 78 53 65 63 74 6f 72 53 69 7a 65 20 2a 2f  * xSectorSize */
1f724 0a 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20  .  0            
1f725 20 20 20 20 20 2f 2a 20 78 44 65 76 69 63 65 43       /* xDeviceC
1f726 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20 2a  haracteristics *
1f727 2f 0a 7d 3b 0a 0a 2f 2a 20 0a 2a 2a 20 4f 70 65  /.};../* .** Ope
1f728 6e 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  n a journal file
1f729 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1f72a 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1f72b 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 73  MemJournalOpen(s
1f72c 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66  qlite3_file *pJf
1f72d 64 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c  d){.  MemJournal
1f72e 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61   *p = (MemJourna
1f72f 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 61 73 73 65  l *)pJfd;.  asse
1f730 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41  rt( EIGHT_BYTE_A
1f731 4c 49 47 4e 4d 45 4e 54 28 70 29 20 29 3b 0a 20  LIGNMENT(p) );. 
1f732 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 71   memset(p, 0, sq
1f733 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53  lite3MemJournalS
1f734 69 7a 65 28 29 29 3b 0a 20 20 70 2d 3e 70 4d 65  ize());.  p->pMe
1f735 74 68 6f 64 20 3d 20 26 4d 65 6d 4a 6f 75 72 6e  thod = &MemJourn
1f736 61 6c 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 2f 2a  alMethods;.}../*
1f737 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  .** Return true 
1f738 69 66 20 74 68 65 20 66 69 6c 65 2d 68 61 6e 64  if the file-hand
1f739 6c 65 20 70 61 73 73 65 64 20 61 73 20 61 6e 20  le passed as an 
1f73a 61 72 67 75 6d 65 6e 74 20 69 73 20 0a 2a 2a 20  argument is .** 
1f73b 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75  an in-memory jou
1f73c 72 6e 61 6c 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rnal .*/.SQLITE_
1f73d 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1f73e 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28  te3IsMemJournal(
1f73f 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a  sqlite3_file *pJ
1f740 66 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 4a  fd){.  return pJ
1f741 66 64 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d 26 4d  fd->pMethods==&M
1f742 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74 68 6f 64 73  emJournalMethods
1f743 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75  ;.}../* .** Retu
1f744 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
1f745 20 62 79 74 65 73 20 72 65 71 75 69 72 65 64 20   bytes required 
1f746 74 6f 20 73 74 6f 72 65 20 61 20 4d 65 6d 4a 6f  to store a MemJo
1f747 75 72 6e 61 6c 20 74 68 61 74 20 75 73 65 73 20  urnal that uses 
1f748 76 66 73 0a 2a 2a 20 70 56 66 73 20 74 6f 20 63  vfs.** pVfs to c
1f749 72 65 61 74 65 20 74 68 65 20 75 6e 64 65 72 6c  reate the underl
1f74a 79 69 6e 67 20 6f 6e 2d 64 69 73 6b 20 66 69 6c  ying on-disk fil
1f74b 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  es..*/.SQLITE_PR
1f74c 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1f74d 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28  3MemJournalSize(
1f74e 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20  void){.  return 
1f74f 73 69 7a 65 6f 66 28 4d 65 6d 4a 6f 75 72 6e 61  sizeof(MemJourna
1f750 6c 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  l);.}../********
1f751 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65  ****** End of me
1f752 6d 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a  mjournal.c *****
1f753 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f754 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f755 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
1f756 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
1f757 65 20 77 61 6c 6b 65 72 2e 63 20 2a 2a 2a 2a 2a  e walker.c *****
1f758 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f759 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f75a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
1f75b 38 20 41 75 67 75 73 74 20 31 36 0a 2a 2a 0a 2a  8 August 16.**.*
1f75c 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
1f75d 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
1f75e 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
1f75f 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
1f760 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
1f761 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
1f762 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
1f763 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
1f764 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
1f765 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
1f766 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
1f767 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
1f768 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
1f769 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
1f76a 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
1f76b 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
1f76c 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
1f76d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
1f76e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f76f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f771 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
1f772 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
1f773 20 72 6f 75 74 69 6e 65 73 20 75 73 65 64 20 66   routines used f
1f774 6f 72 20 77 61 6c 6b 69 6e 67 20 74 68 65 20 70  or walking the p
1f775 61 72 73 65 72 20 74 72 65 65 20 66 6f 72 0a 2a  arser tree for.*
1f776 2a 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65  * an SQL stateme
1f777 6e 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 77  nt..**.** $Id: w
1f778 61 6c 6b 65 72 2e 63 2c 76 20 31 2e 37 20 32 30  alker.c,v 1.7 20
1f779 30 39 2f 30 36 2f 31 35 20 32 33 3a 31 35 3a 35  09/06/15 23:15:5
1f77a 39 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a  9 drh Exp $.*/..
1f77b 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65  ./*.** Walk an e
1f77c 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20  xpression tree. 
1f77d 20 49 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c   Invoke the call
1f77e 62 61 63 6b 20 6f 6e 63 65 20 66 6f 72 20 65 61  back once for ea
1f77f 63 68 20 6e 6f 64 65 0a 2a 2a 20 6f 66 20 74 68  ch node.** of th
1f780 65 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 77 68  e expression, wh
1f781 69 6c 65 20 64 65 63 65 6e 64 69 6e 67 2e 20 20  ile decending.  
1f782 28 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c  (In other words,
1f783 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a   the callback.**
1f784 20 69 73 20 69 6e 76 6f 6b 65 64 20 62 65 66 6f   is invoked befo
1f785 72 65 20 76 69 73 69 74 69 6e 67 20 63 68 69 6c  re visiting chil
1f786 64 72 65 6e 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65  dren.).**.** The
1f787 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72   return value fr
1f788 6f 6d 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  om the callback 
1f789 73 68 6f 75 6c 64 20 62 65 20 6f 6e 65 20 6f 66  should be one of
1f78a 20 74 68 65 20 57 52 43 5f 2a 0a 2a 2a 20 63 6f   the WRC_*.** co
1f78b 6e 73 74 61 6e 74 73 20 74 6f 20 73 70 65 63 69  nstants to speci
1f78c 66 79 20 68 6f 77 20 74 6f 20 70 72 6f 63 65 65  fy how to procee
1f78d 64 20 77 69 74 68 20 74 68 65 20 77 61 6c 6b 2e  d with the walk.
1f78e 0a 2a 2a 0a 2a 2a 20 20 20 20 57 52 43 5f 43 6f  .**.**    WRC_Co
1f78f 6e 74 69 6e 75 65 20 20 20 20 20 20 43 6f 6e 74  ntinue      Cont
1f790 69 6e 75 65 20 64 65 73 63 65 6e 64 69 6e 67 20  inue descending 
1f791 64 6f 77 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a  down the tree..*
1f792 2a 0a 2a 2a 20 20 20 20 57 52 43 5f 50 72 75 6e  *.**    WRC_Prun
1f793 65 20 20 20 20 20 20 20 20 20 44 6f 20 6e 6f 74  e         Do not
1f794 20 64 65 73 63 65 6e 64 20 69 6e 74 6f 20 63 68   descend into ch
1f795 69 6c 64 20 6e 6f 64 65 73 2e 20 20 42 75 74 20  ild nodes.  But 
1f796 61 6c 6c 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20  allow.**        
1f797 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68                th
1f798 65 20 77 61 6c 6b 20 74 6f 20 63 6f 6e 74 69 6e  e walk to contin
1f799 75 65 20 77 69 74 68 20 73 69 62 6c 69 6e 67 20  ue with sibling 
1f79a 6e 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  nodes..**.**    
1f79b 57 52 43 5f 41 62 6f 72 74 20 20 20 20 20 20 20  WRC_Abort       
1f79c 20 20 44 6f 20 6e 6f 20 6d 6f 72 65 20 63 61 6c    Do no more cal
1f79d 6c 62 61 63 6b 73 2e 20 20 55 6e 77 69 6e 64 20  lbacks.  Unwind 
1f79e 74 68 65 20 73 74 61 63 6b 20 61 6e 64 0a 2a 2a  the stack and.**
1f79f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f7a0 20 20 20 20 20 20 72 65 74 75 72 6e 20 74 68 65        return the
1f7a1 20 74 6f 70 2d 6c 65 76 65 6c 20 77 61 6c 6b 20   top-level walk 
1f7a2 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  call..**.** The 
1f7a3 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f  return value fro
1f7a4 6d 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  m this routine i
1f7a5 73 20 57 52 43 5f 41 62 6f 72 74 20 74 6f 20 61  s WRC_Abort to a
1f7a6 62 61 6e 64 6f 6e 20 74 68 65 20 74 72 65 65 20  bandon the tree 
1f7a7 77 61 6c 6b 0a 2a 2a 20 61 6e 64 20 57 52 43 5f  walk.** and WRC_
1f7a8 43 6f 6e 74 69 6e 75 65 20 74 6f 20 63 6f 6e 74  Continue to cont
1f7a9 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  inue..*/.SQLITE_
1f7aa 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1f7ab 74 65 33 57 61 6c 6b 45 78 70 72 28 57 61 6c 6b  te3WalkExpr(Walk
1f7ac 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70  er *pWalker, Exp
1f7ad 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 69 6e 74  r *pExpr){.  int
1f7ae 20 72 63 3b 0a 20 20 69 66 28 20 70 45 78 70 72   rc;.  if( pExpr
1f7af 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57 52 43  ==0 ) return WRC
1f7b0 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 74 65 73  _Continue;.  tes
1f7b1 74 63 61 73 65 28 20 45 78 70 72 48 61 73 50 72  tcase( ExprHasPr
1f7b2 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
1f7b3 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 3b 0a 20  _TokenOnly) );. 
1f7b4 20 74 65 73 74 63 61 73 65 28 20 45 78 70 72 48   testcase( ExprH
1f7b5 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
1f7b6 2c 20 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b  , EP_Reduced) );
1f7b7 0a 20 20 72 63 20 3d 20 70 57 61 6c 6b 65 72 2d  .  rc = pWalker-
1f7b8 3e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 28 70  >xExprCallback(p
1f7b9 57 61 6c 6b 65 72 2c 20 70 45 78 70 72 29 3b 0a  Walker, pExpr);.
1f7ba 20 20 69 66 28 20 72 63 3d 3d 57 52 43 5f 43 6f    if( rc==WRC_Co
1f7bb 6e 74 69 6e 75 65 0a 20 20 20 20 20 20 20 20 20  ntinue.         
1f7bc 20 20 20 20 20 26 26 20 21 45 78 70 72 48 61 73       && !ExprHas
1f7bd 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70  AnyProperty(pExp
1f7be 72 2c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20  r,EP_TokenOnly) 
1f7bf 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ){.    if( sqlit
1f7c0 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b  e3WalkExpr(pWalk
1f7c1 65 72 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  er, pExpr->pLeft
1f7c2 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41  ) ) return WRC_A
1f7c3 62 6f 72 74 3b 0a 20 20 20 20 69 66 28 20 73 71  bort;.    if( sq
1f7c4 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57  lite3WalkExpr(pW
1f7c5 61 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e 70 52  alker, pExpr->pR
1f7c6 69 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 57  ight) ) return W
1f7c7 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 69 66  RC_Abort;.    if
1f7c8 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74  ( ExprHasPropert
1f7c9 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53  y(pExpr, EP_xIsS
1f7ca 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20  elect) ){.      
1f7cb 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 53  if( sqlite3WalkS
1f7cc 65 6c 65 63 74 28 70 57 61 6c 6b 65 72 2c 20 70  elect(pWalker, p
1f7cd 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 29  Expr->x.pSelect)
1f7ce 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62   ) return WRC_Ab
1f7cf 6f 72 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ort;.    }else{.
1f7d0 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
1f7d1 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57  3WalkExprList(pW
1f7d2 61 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e 78 2e  alker, pExpr->x.
1f7d3 70 4c 69 73 74 29 20 29 20 72 65 74 75 72 6e 20  pList) ) return 
1f7d4 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d  WRC_Abort;.    }
1f7d5 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
1f7d6 20 26 20 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a   & WRC_Abort;.}.
1f7d7 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69  ./*.** Call sqli
1f7d8 74 65 33 57 61 6c 6b 45 78 70 72 28 29 20 66 6f  te3WalkExpr() fo
1f7d9 72 20 65 76 65 72 79 20 65 78 70 72 65 73 73 69  r every expressi
1f7da 6f 6e 20 69 6e 20 6c 69 73 74 20 70 20 6f 72 20  on in list p or 
1f7db 75 6e 74 69 6c 0a 2a 2a 20 61 6e 20 61 62 6f 72  until.** an abor
1f7dc 74 20 72 65 71 75 65 73 74 20 69 73 20 73 65 65  t request is see
1f7dd 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  n..*/.SQLITE_PRI
1f7de 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1f7df 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 57 61 6c  WalkExprList(Wal
1f7e0 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78  ker *pWalker, Ex
1f7e1 70 72 4c 69 73 74 20 2a 70 29 7b 0a 20 20 69 6e  prList *p){.  in
1f7e2 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 45 78  t i;.  struct Ex
1f7e3 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  prList_item *pIt
1f7e4 65 6d 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  em;.  if( p ){. 
1f7e5 20 20 20 66 6f 72 28 69 3d 70 2d 3e 6e 45 78 70     for(i=p->nExp
1f7e6 72 2c 20 70 49 74 65 6d 3d 70 2d 3e 61 3b 20 69  r, pItem=p->a; i
1f7e7 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b  >0; i--, pItem++
1f7e8 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  ){.      if( sql
1f7e9 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61  ite3WalkExpr(pWa
1f7ea 6c 6b 65 72 2c 20 70 49 74 65 6d 2d 3e 70 45 78  lker, pItem->pEx
1f7eb 70 72 29 20 29 20 72 65 74 75 72 6e 20 57 52 43  pr) ) return WRC
1f7ec 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20  _Abort;.    }.  
1f7ed 7d 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43  }.  return WRC_C
1f7ee 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  ontinue;.}../*.*
1f7ef 2a 20 57 61 6c 6b 20 61 6c 6c 20 65 78 70 72 65  * Walk all expre
1f7f0 73 73 69 6f 6e 73 20 61 73 73 6f 63 69 61 74 65  ssions associate
1f7f1 64 20 77 69 74 68 20 53 45 4c 45 43 54 20 73 74  d with SELECT st
1f7f2 61 74 65 6d 65 6e 74 20 70 2e 20 20 44 6f 0a 2a  atement p.  Do.*
1f7f3 2a 20 6e 6f 74 20 69 6e 76 6f 6b 65 20 74 68 65  * not invoke the
1f7f4 20 53 45 4c 45 43 54 20 63 61 6c 6c 62 61 63 6b   SELECT callback
1f7f5 20 6f 6e 20 70 2c 20 62 75 74 20 64 6f 20 28 6f   on p, but do (o
1f7f6 66 20 63 6f 75 72 73 65 29 20 69 6e 76 6f 6b 65  f course) invoke
1f7f7 0a 2a 2a 20 61 6e 79 20 65 78 70 72 20 63 61 6c  .** any expr cal
1f7f8 6c 62 61 63 6b 73 20 61 6e 64 20 53 45 4c 45 43  lbacks and SELEC
1f7f9 54 20 63 61 6c 6c 62 61 63 6b 73 20 74 68 61 74  T callbacks that
1f7fa 20 63 6f 6d 65 20 66 72 6f 6d 20 73 75 62 71 75   come from subqu
1f7fb 65 72 69 65 73 2e 0a 2a 2a 20 52 65 74 75 72 6e  eries..** Return
1f7fc 20 57 52 43 5f 41 62 6f 72 74 20 6f 72 20 57 52   WRC_Abort or WR
1f7fd 43 5f 43 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53  C_Continue..*/.S
1f7fe 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1f7ff 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c  t sqlite3WalkSel
1f800 65 63 74 45 78 70 72 28 57 61 6c 6b 65 72 20 2a  ectExpr(Walker *
1f801 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20  pWalker, Select 
1f802 2a 70 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74  *p){.  if( sqlit
1f803 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70  e3WalkExprList(p
1f804 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 45 4c 69 73  Walker, p->pELis
1f805 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f  t) ) return WRC_
1f806 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c  Abort;.  if( sql
1f807 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61  ite3WalkExpr(pWa
1f808 6c 6b 65 72 2c 20 70 2d 3e 70 57 68 65 72 65 29  lker, p->pWhere)
1f809 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62   ) return WRC_Ab
1f80a 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  ort;.  if( sqlit
1f80b 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70  e3WalkExprList(p
1f80c 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 47 72 6f 75  Walker, p->pGrou
1f80d 70 42 79 29 20 29 20 72 65 74 75 72 6e 20 57 52  pBy) ) return WR
1f80e 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73  C_Abort;.  if( s
1f80f 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70  qlite3WalkExpr(p
1f810 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 48 61 76 69  Walker, p->pHavi
1f811 6e 67 29 20 29 20 72 65 74 75 72 6e 20 57 52 43  ng) ) return WRC
1f812 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71  _Abort;.  if( sq
1f813 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73  lite3WalkExprLis
1f814 74 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4f  t(pWalker, p->pO
1f815 72 64 65 72 42 79 29 20 29 20 72 65 74 75 72 6e  rderBy) ) return
1f816 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66   WRC_Abort;.  if
1f817 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70  ( sqlite3WalkExp
1f818 72 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4c  r(pWalker, p->pL
1f819 69 6d 69 74 29 20 29 20 72 65 74 75 72 6e 20 57  imit) ) return W
1f81a 52 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20  RC_Abort;.  if( 
1f81b 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28  sqlite3WalkExpr(
1f81c 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4f 66 66  pWalker, p->pOff
1f81d 73 65 74 29 20 29 20 72 65 74 75 72 6e 20 57 52  set) ) return WR
1f81e 43 5f 41 62 6f 72 74 3b 0a 20 20 72 65 74 75 72  C_Abort;.  retur
1f81f 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a  n WRC_Continue;.
1f820 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 74 68  }../*.** Walk th
1f821 65 20 70 61 72 73 65 20 74 72 65 65 73 20 61 73  e parse trees as
1f822 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 6c  sociated with al
1f823 6c 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20  l subqueries in 
1f824 74 68 65 0a 2a 2a 20 46 52 4f 4d 20 63 6c 61 75  the.** FROM clau
1f825 73 65 20 6f 66 20 53 45 4c 45 43 54 20 73 74 61  se of SELECT sta
1f826 74 65 6d 65 6e 74 20 70 2e 20 20 44 6f 20 6e 6f  tement p.  Do no
1f827 74 20 69 6e 76 6f 6b 65 20 74 68 65 20 73 65 6c  t invoke the sel
1f828 65 63 74 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20  ect.** callback 
1f829 6f 6e 20 70 2c 20 62 75 74 20 64 6f 20 69 6e 76  on p, but do inv
1f82a 6f 6b 65 20 69 74 20 6f 6e 20 65 61 63 68 20 46  oke it on each F
1f82b 52 4f 4d 20 63 6c 61 75 73 65 20 73 75 62 71 75  ROM clause subqu
1f82c 65 72 79 0a 2a 2a 20 61 6e 64 20 6f 6e 20 61 6e  ery.** and on an
1f82d 79 20 73 75 62 71 75 65 72 69 65 73 20 66 75 72  y subqueries fur
1f82e 74 68 65 72 20 64 6f 77 6e 20 69 6e 20 74 68 65  ther down in the
1f82f 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e 20 0a   tree.  Return .
1f830 2a 2a 20 57 52 43 5f 41 62 6f 72 74 20 6f 72 20  ** WRC_Abort or 
1f831 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 2a 2f  WRC_Continue;.*/
1f832 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1f833 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53  int sqlite3WalkS
1f834 65 6c 65 63 74 46 72 6f 6d 28 57 61 6c 6b 65 72  electFrom(Walker
1f835 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63   *pWalker, Selec
1f836 74 20 2a 70 29 7b 0a 20 20 53 72 63 4c 69 73 74  t *p){.  SrcList
1f837 20 2a 70 53 72 63 3b 0a 20 20 69 6e 74 20 69 3b   *pSrc;.  int i;
1f838 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
1f839 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a  t_item *pItem;..
1f83a 20 20 70 53 72 63 20 3d 20 70 2d 3e 70 53 72 63    pSrc = p->pSrc
1f83b 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70  ;.  if( ALWAYS(p
1f83c 53 72 63 29 20 29 7b 0a 20 20 20 20 66 6f 72 28  Src) ){.    for(
1f83d 69 3d 70 53 72 63 2d 3e 6e 53 72 63 2c 20 70 49  i=pSrc->nSrc, pI
1f83e 74 65 6d 3d 70 53 72 63 2d 3e 61 3b 20 69 3e 30  tem=pSrc->a; i>0
1f83f 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b  ; i--, pItem++){
1f840 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
1f841 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61  e3WalkSelect(pWa
1f842 6c 6b 65 72 2c 20 70 49 74 65 6d 2d 3e 70 53 65  lker, pItem->pSe
1f843 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 20  lect) ){.       
1f844 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   return WRC_Abor
1f845 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  t;.      }.    }
1f846 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 57 52  .  }.  return WR
1f847 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 20 0a 0a  C_Continue;.} ..
1f848 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74  /*.** Call sqlit
1f849 65 33 57 61 6c 6b 45 78 70 72 28 29 20 66 6f 72  e3WalkExpr() for
1f84a 20 65 76 65 72 79 20 65 78 70 72 65 73 73 69 6f   every expressio
1f84b 6e 20 69 6e 20 53 65 6c 65 63 74 20 73 74 61 74  n in Select stat
1f84c 65 6d 65 6e 74 20 70 2e 0a 2a 2a 20 49 6e 76 6f  ement p..** Invo
1f84d 6b 65 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65  ke sqlite3WalkSe
1f84e 6c 65 63 74 28 29 20 66 6f 72 20 73 75 62 71 75  lect() for subqu
1f84f 65 72 69 65 73 20 69 6e 20 74 68 65 20 46 52 4f  eries in the FRO
1f850 4d 20 63 6c 61 75 73 65 20 61 6e 64 0a 2a 2a 20  M clause and.** 
1f851 6f 6e 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20  on the compound 
1f852 73 65 6c 65 63 74 20 63 68 61 69 6e 2c 20 70 2d  select chain, p-
1f853 3e 70 50 72 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 52  >pPrior..**.** R
1f854 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e  eturn WRC_Contin
1f855 75 65 20 75 6e 64 65 72 20 6e 6f 72 6d 61 6c 20  ue under normal 
1f856 63 6f 6e 64 69 74 69 6f 6e 73 2e 20 20 52 65 74  conditions.  Ret
1f857 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 20 69 66  urn WRC_Abort if
1f858 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 61 6e 20  .** there is an 
1f859 61 62 6f 72 74 20 72 65 71 75 65 73 74 2e 0a 2a  abort request..*
1f85a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 57 61 6c 6b  *.** If the Walk
1f85b 65 72 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65  er does not have
1f85c 20 61 6e 20 78 53 65 6c 65 63 74 43 61 6c 6c 62   an xSelectCallb
1f85d 61 63 6b 28 29 20 74 68 65 6e 20 74 68 69 73 20  ack() then this 
1f85e 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 61 20  routine.** is a 
1f85f 6e 6f 2d 6f 70 20 72 65 74 75 72 6e 69 6e 67 20  no-op returning 
1f860 57 52 43 5f 43 6f 6e 74 69 6e 75 65 2e 0a 2a 2f  WRC_Continue..*/
1f861 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1f862 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53  int sqlite3WalkS
1f863 65 6c 65 63 74 28 57 61 6c 6b 65 72 20 2a 70 57  elect(Walker *pW
1f864 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70  alker, Select *p
1f865 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69  ){.  int rc;.  i
1f866 66 28 20 70 3d 3d 30 20 7c 7c 20 70 57 61 6c 6b  f( p==0 || pWalk
1f867 65 72 2d 3e 78 53 65 6c 65 63 74 43 61 6c 6c 62  er->xSelectCallb
1f868 61 63 6b 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ack==0 ) return 
1f869 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20  WRC_Continue;.  
1f86a 72 63 20 3d 20 57 52 43 5f 43 6f 6e 74 69 6e 75  rc = WRC_Continu
1f86b 65 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 20 29  e;.  while( p  )
1f86c 7b 0a 20 20 20 20 72 63 20 3d 20 70 57 61 6c 6b  {.    rc = pWalk
1f86d 65 72 2d 3e 78 53 65 6c 65 63 74 43 61 6c 6c 62  er->xSelectCallb
1f86e 61 63 6b 28 70 57 61 6c 6b 65 72 2c 20 70 29 3b  ack(pWalker, p);
1f86f 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 62 72  .    if( rc ) br
1f870 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  eak;.    if( sql
1f871 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 45 78  ite3WalkSelectEx
1f872 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 29 20 29  pr(pWalker, p) )
1f873 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   return WRC_Abor
1f874 74 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  t;.    if( sqlit
1f875 65 33 57 61 6c 6b 53 65 6c 65 63 74 46 72 6f 6d  e3WalkSelectFrom
1f876 28 70 57 61 6c 6b 65 72 2c 20 70 29 20 29 20 72  (pWalker, p) ) r
1f877 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b  eturn WRC_Abort;
1f878 0a 20 20 20 20 70 20 3d 20 70 2d 3e 70 50 72 69  .    p = p->pPri
1f879 6f 72 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  or;.  }.  return
1f87a 20 72 63 20 26 20 57 52 43 5f 41 62 6f 72 74 3b   rc & WRC_Abort;
1f87b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
1f87c 2a 2a 2a 20 45 6e 64 20 6f 66 20 77 61 6c 6b 65  *** End of walke
1f87d 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  r.c ************
1f87e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f87f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f880 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
1f881 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 72  *** Begin file r
1f882 65 73 6f 6c 76 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  esolve.c *******
1f883 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f884 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f885 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41  **/./*.** 2008 A
1f886 75 67 75 73 74 20 31 38 0a 2a 2a 0a 2a 2a 20 54  ugust 18.**.** T
1f887 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
1f888 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
1f889 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
1f88a 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
1f88b 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
1f88c 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
1f88d 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
1f88e 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
1f88f 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
1f890 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
1f891 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
1f892 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
1f893 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
1f894 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
1f895 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
1f896 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
1f897 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
1f898 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f899 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f89a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f89b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f89c 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
1f89d 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
1f89e 20 72 6f 75 74 69 6e 65 73 20 75 73 65 64 20 66   routines used f
1f89f 6f 72 20 77 61 6c 6b 69 6e 67 20 74 68 65 20 70  or walking the p
1f8a0 61 72 73 65 72 20 74 72 65 65 20 61 6e 64 0a 2a  arser tree and.*
1f8a1 2a 20 72 65 73 6f 6c 76 65 20 61 6c 6c 20 69 64  * resolve all id
1f8a2 65 6e 74 69 66 69 65 72 73 20 62 79 20 61 73 73  entifiers by ass
1f8a3 6f 63 69 61 74 69 6e 67 20 74 68 65 6d 20 77 69  ociating them wi
1f8a4 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a  th a particular.
1f8a5 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c  ** table and col
1f8a6 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  umn..**.** $Id: 
1f8a7 72 65 73 6f 6c 76 65 2e 63 2c 76 20 31 2e 33 30  resolve.c,v 1.30
1f8a8 20 32 30 30 39 2f 30 36 2f 31 35 20 32 33 3a 31   2009/06/15 23:1
1f8a9 35 3a 35 39 20 64 72 68 20 45 78 70 20 24 0a 2a  5:59 drh Exp $.*
1f8aa 2f 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 68  /../*.** Turn th
1f8ab 65 20 70 45 78 70 72 20 65 78 70 72 65 73 73 69  e pExpr expressi
1f8ac 6f 6e 20 69 6e 74 6f 20 61 6e 20 61 6c 69 61 73  on into an alias
1f8ad 20 66 6f 72 20 74 68 65 20 69 43 6f 6c 2d 74 68   for the iCol-th
1f8ae 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 0a 2a   column of the.*
1f8af 2a 20 72 65 73 75 6c 74 20 73 65 74 20 69 6e 20  * result set in 
1f8b0 70 45 4c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66  pEList..**.** If
1f8b1 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
1f8b2 63 6f 6c 75 6d 6e 20 69 73 20 61 20 73 69 6d 70  column is a simp
1f8b3 6c 65 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65  le column refere
1f8b4 6e 63 65 2c 20 74 68 65 6e 20 74 68 69 73 20 72  nce, then this r
1f8b5 6f 75 74 69 6e 65 0a 2a 2a 20 6d 61 6b 65 73 20  outine.** makes 
1f8b6 61 6e 20 65 78 61 63 74 20 63 6f 70 79 2e 20 20  an exact copy.  
1f8b7 42 75 74 20 66 6f 72 20 61 6e 79 20 6f 74 68 65  But for any othe
1f8b8 72 20 6b 69 6e 64 20 6f 66 20 65 78 70 72 65 73  r kind of expres
1f8b9 73 69 6f 6e 2c 20 74 68 69 73 0a 2a 2a 20 72 6f  sion, this.** ro
1f8ba 75 74 69 6e 65 20 6d 61 6b 65 20 61 20 63 6f 70  utine make a cop
1f8bb 79 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20  y of the result 
1f8bc 73 65 74 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68  set column as th
1f8bd 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  e argument to th
1f8be 65 0a 2a 2a 20 54 4b 5f 41 53 20 6f 70 65 72 61  e.** TK_AS opera
1f8bf 74 6f 72 2e 20 20 54 68 65 20 54 4b 5f 41 53 20  tor.  The TK_AS 
1f8c0 6f 70 65 72 61 74 6f 72 20 63 61 75 73 65 73 20  operator causes 
1f8c1 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74  the expression t
1f8c2 6f 20 62 65 0a 2a 2a 20 65 76 61 6c 75 61 74 65  o be.** evaluate
1f8c3 64 20 6a 75 73 74 20 6f 6e 63 65 20 61 6e 64 20  d just once and 
1f8c4 74 68 65 6e 20 72 65 75 73 65 64 20 66 6f 72 20  then reused for 
1f8c5 65 61 63 68 20 61 6c 69 61 73 2e 0a 2a 2a 0a 2a  each alias..**.*
1f8c6 2a 20 54 68 65 20 72 65 61 73 6f 6e 20 66 6f 72  * The reason for
1f8c7 20 73 75 70 70 72 65 73 73 69 6e 67 20 74 68 65   suppressing the
1f8c8 20 54 4b 5f 41 53 20 74 65 72 6d 20 77 68 65 6e   TK_AS term when
1f8c9 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
1f8ca 69 73 20 61 20 73 69 6d 70 6c 65 0a 2a 2a 20 63  is a simple.** c
1f8cb 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 20  olumn reference 
1f8cc 69 73 20 73 6f 20 74 68 61 74 20 74 68 65 20 63  is so that the c
1f8cd 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 20  olumn reference 
1f8ce 77 69 6c 6c 20 62 65 20 72 65 63 6f 67 6e 69 7a  will be recogniz
1f8cf 65 64 20 61 73 0a 2a 2a 20 75 73 61 62 6c 65 20  ed as.** usable 
1f8d0 62 79 20 69 6e 64 69 63 65 73 20 77 69 74 68 69  by indices withi
1f8d1 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  n the WHERE clau
1f8d2 73 65 20 70 72 6f 63 65 73 73 69 6e 67 20 6c 6f  se processing lo
1f8d3 67 69 63 2e 20 0a 2a 2a 0a 2a 2a 20 48 61 63 6b  gic. .**.** Hack
1f8d4 3a 20 20 54 68 65 20 54 4b 5f 41 53 20 6f 70 65  :  The TK_AS ope
1f8d5 72 61 74 6f 72 20 69 73 20 69 6e 68 69 62 69 74  rator is inhibit
1f8d6 65 64 20 69 66 20 7a 54 79 70 65 5b 30 5d 3d 3d  ed if zType[0]==
1f8d7 27 47 27 2e 20 20 54 68 69 73 20 6d 65 61 6e 73  'G'.  This means
1f8d8 0a 2a 2a 20 74 68 61 74 20 69 6e 20 61 20 47 52  .** that in a GR
1f8d9 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2c 20 74  OUP BY clause, t
1f8da 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
1f8db 20 65 76 61 6c 75 61 74 65 64 20 74 77 69 63 65   evaluated twice
1f8dc 2e 20 20 48 65 6e 63 65 3a 0a 2a 2a 0a 2a 2a 20  .  Hence:.**.** 
1f8dd 20 20 20 20 53 45 4c 45 43 54 20 72 61 6e 64 6f      SELECT rando
1f8de 6d 28 29 25 35 20 41 53 20 78 2c 20 63 6f 75 6e  m()%5 AS x, coun
1f8df 74 28 2a 29 20 46 52 4f 4d 20 74 61 62 20 47 52  t(*) FROM tab GR
1f8e0 4f 55 50 20 42 59 20 78 0a 2a 2a 0a 2a 2a 20 49  OUP BY x.**.** I
1f8e1 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 3a  s equivalent to:
1f8e2 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43  .**.**     SELEC
1f8e3 54 20 72 61 6e 64 6f 6d 28 29 25 35 20 41 53 20  T random()%5 AS 
1f8e4 78 2c 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d  x, count(*) FROM
1f8e5 20 74 61 62 20 47 52 4f 55 50 20 42 59 20 72 61   tab GROUP BY ra
1f8e6 6e 64 6f 6d 28 29 25 35 0a 2a 2a 0a 2a 2a 20 54  ndom()%5.**.** T
1f8e7 68 65 20 72 65 73 75 6c 74 20 6f 66 20 72 61 6e  he result of ran
1f8e8 64 6f 6d 28 29 25 35 20 69 6e 20 74 68 65 20 47  dom()%5 in the G
1f8e9 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 69  ROUP BY clause i
1f8ea 73 20 70 72 6f 62 61 62 6c 79 20 64 69 66 66 65  s probably diffe
1f8eb 72 65 6e 74 0a 2a 2a 20 66 72 6f 6d 20 74 68 65  rent.** from the
1f8ec 20 72 65 73 75 6c 74 20 69 6e 20 74 68 65 20 72   result in the r
1f8ed 65 73 75 6c 74 2d 73 65 74 2e 20 20 57 65 20 6d  esult-set.  We m
1f8ee 69 67 68 74 20 66 69 78 20 74 68 69 73 20 73 6f  ight fix this so
1f8ef 6d 65 64 61 79 2e 20 20 4f 72 0a 2a 2a 20 74 68  meday.  Or.** th
1f8f0 65 6e 20 61 67 61 69 6e 2c 20 77 65 20 6d 69 67  en again, we mig
1f8f1 68 74 20 6e 6f 74 2e 2e 2e 0a 2a 2f 0a 73 74 61  ht not....*/.sta
1f8f2 74 69 63 20 76 6f 69 64 20 72 65 73 6f 6c 76 65  tic void resolve
1f8f3 41 6c 69 61 73 28 0a 20 20 50 61 72 73 65 20 2a  Alias(.  Parse *
1f8f4 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
1f8f5 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
1f8f6 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  xt */.  ExprList
1f8f7 20 2a 70 45 4c 69 73 74 2c 20 20 20 20 20 20 2f   *pEList,      /
1f8f8 2a 20 41 20 72 65 73 75 6c 74 20 73 65 74 20 2a  * A result set *
1f8f9 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 2c 20 20 20  /.  int iCol,   
1f8fa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20             /* A 
1f8fb 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72 65  column in the re
1f8fc 73 75 6c 74 20 73 65 74 2e 20 20 30 2e 2e 70 45  sult set.  0..pE
1f8fd 4c 69 73 74 2d 3e 6e 45 78 70 72 2d 31 20 2a 2f  List->nExpr-1 */
1f8fe 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20  .  Expr *pExpr, 
1f8ff 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 61            /* Tra
1f900 6e 73 66 6f 72 6d 20 74 68 69 73 20 69 6e 74 6f  nsform this into
1f901 20 61 6e 20 61 6c 69 61 73 20 74 6f 20 74 68 65   an alias to the
1f902 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20   result set */. 
1f903 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79   const char *zTy
1f904 70 65 20 20 20 20 20 20 2f 2a 20 22 47 52 4f 55  pe      /* "GROU
1f905 50 22 20 6f 72 20 22 4f 52 44 45 52 22 20 6f 72  P" or "ORDER" or
1f906 20 22 22 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72   "" */.){.  Expr
1f907 20 2a 70 4f 72 69 67 3b 20 20 20 20 20 20 20 20   *pOrig;        
1f908 20 20 20 2f 2a 20 54 68 65 20 69 43 6f 6c 2d 74     /* The iCol-t
1f909 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  h column of the 
1f90a 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20  result set */.  
1f90b 45 78 70 72 20 2a 70 44 75 70 3b 20 20 20 20 20  Expr *pDup;     
1f90c 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f         /* Copy o
1f90d 66 20 70 4f 72 69 67 20 2a 2f 0a 20 20 73 71 6c  f pOrig */.  sql
1f90e 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
1f90f 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
1f910 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
1f911 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 43 6f  /..  assert( iCo
1f912 6c 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70 45 4c  l>=0 && iCol<pEL
1f913 69 73 74 2d 3e 6e 45 78 70 72 20 29 3b 0a 20 20  ist->nExpr );.  
1f914 70 4f 72 69 67 20 3d 20 70 45 4c 69 73 74 2d 3e  pOrig = pEList->
1f915 61 5b 69 43 6f 6c 5d 2e 70 45 78 70 72 3b 0a 20  a[iCol].pExpr;. 
1f916 20 61 73 73 65 72 74 28 20 70 4f 72 69 67 21 3d   assert( pOrig!=
1f917 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
1f918 4f 72 69 67 2d 3e 66 6c 61 67 73 20 26 20 45 50  Orig->flags & EP
1f919 5f 52 65 73 6f 6c 76 65 64 20 29 3b 0a 20 20 64  _Resolved );.  d
1f91a 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
1f91b 20 20 69 66 28 20 70 4f 72 69 67 2d 3e 6f 70 21    if( pOrig->op!
1f91c 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 7a 54  =TK_COLUMN && zT
1f91d 79 70 65 5b 30 5d 21 3d 27 47 27 20 29 7b 0a 20  ype[0]!='G' ){. 
1f91e 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65     pDup = sqlite
1f91f 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 72  3ExprDup(db, pOr
1f920 69 67 2c 20 30 29 3b 0a 20 20 20 20 70 44 75 70  ig, 0);.    pDup
1f921 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
1f922 70 50 61 72 73 65 2c 20 54 4b 5f 41 53 2c 20 70  pParse, TK_AS, p
1f923 44 75 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  Dup, 0, 0);.    
1f924 69 66 28 20 70 44 75 70 3d 3d 30 20 29 20 72 65  if( pDup==0 ) re
1f925 74 75 72 6e 3b 0a 20 20 20 20 69 66 28 20 70 45  turn;.    if( pE
1f926 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 41  List->a[iCol].iA
1f927 6c 69 61 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20  lias==0 ){.     
1f928 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d   pEList->a[iCol]
1f929 2e 69 41 6c 69 61 73 20 3d 20 28 75 31 36 29 28  .iAlias = (u16)(
1f92a 2b 2b 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73  ++pParse->nAlias
1f92b 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 44 75  );.    }.    pDu
1f92c 70 2d 3e 69 54 61 62 6c 65 20 3d 20 70 45 4c 69  p->iTable = pELi
1f92d 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 41 6c 69  st->a[iCol].iAli
1f92e 61 73 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  as;.  }else if( 
1f92f 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
1f930 70 4f 72 69 67 2c 20 45 50 5f 49 6e 74 56 61 6c  pOrig, EP_IntVal
1f931 75 65 29 20 7c 7c 20 70 4f 72 69 67 2d 3e 75 2e  ue) || pOrig->u.
1f932 7a 54 6f 6b 65 6e 3d 3d 30 20 29 7b 0a 20 20 20  zToken==0 ){.   
1f933 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45   pDup = sqlite3E
1f934 78 70 72 44 75 70 28 64 62 2c 20 70 4f 72 69 67  xprDup(db, pOrig
1f935 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 44  , 0);.    if( pD
1f936 75 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  up==0 ) return;.
1f937 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61    }else{.    cha
1f938 72 20 2a 7a 54 6f 6b 65 6e 20 3d 20 70 4f 72 69  r *zToken = pOri
1f939 67 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20  g->u.zToken;.   
1f93a 20 61 73 73 65 72 74 28 20 7a 54 6f 6b 65 6e 21   assert( zToken!
1f93b 3d 30 20 29 3b 0a 20 20 20 20 70 4f 72 69 67 2d  =0 );.    pOrig-
1f93c 3e 75 2e 7a 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20  >u.zToken = 0;. 
1f93d 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65     pDup = sqlite
1f93e 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 72  3ExprDup(db, pOr
1f93f 69 67 2c 20 30 29 3b 0a 20 20 20 20 70 4f 72 69  ig, 0);.    pOri
1f940 67 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 3d 20 7a 54  g->u.zToken = zT
1f941 6f 6b 65 6e 3b 0a 20 20 20 20 69 66 28 20 70 44  oken;.    if( pD
1f942 75 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  up==0 ) return;.
1f943 20 20 20 20 61 73 73 65 72 74 28 20 28 70 44 75      assert( (pDu
1f944 70 2d 3e 66 6c 61 67 73 20 26 20 28 45 50 5f 52  p->flags & (EP_R
1f945 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f  educed|EP_TokenO
1f946 6e 6c 79 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20  nly))==0 );.    
1f947 70 44 75 70 2d 3e 66 6c 61 67 73 32 20 7c 3d 20  pDup->flags2 |= 
1f948 45 50 32 5f 4d 61 6c 6c 6f 63 65 64 54 6f 6b 65  EP2_MallocedToke
1f949 6e 3b 0a 20 20 20 20 70 44 75 70 2d 3e 75 2e 7a  n;.    pDup->u.z
1f94a 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65 33 44  Token = sqlite3D
1f94b 62 53 74 72 44 75 70 28 64 62 2c 20 7a 54 6f 6b  bStrDup(db, zTok
1f94c 65 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  en);.  }.  if( p
1f94d 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50  Expr->flags & EP
1f94e 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20  _ExpCollate ){. 
1f94f 20 20 20 70 44 75 70 2d 3e 70 43 6f 6c 6c 20 3d     pDup->pColl =
1f950 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 3b 0a 20   pExpr->pColl;. 
1f951 20 20 20 70 44 75 70 2d 3e 66 6c 61 67 73 20 7c     pDup->flags |
1f952 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b  = EP_ExpCollate;
1f953 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 78  .  }.  sqlite3Ex
1f954 70 72 43 6c 65 61 72 28 64 62 2c 20 70 45 78 70  prClear(db, pExp
1f955 72 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 45 78  r);.  memcpy(pEx
1f956 70 72 2c 20 70 44 75 70 2c 20 73 69 7a 65 6f 66  pr, pDup, sizeof
1f957 28 2a 70 45 78 70 72 29 29 3b 0a 20 20 73 71 6c  (*pExpr));.  sql
1f958 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
1f959 44 75 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  Dup);.}../*.** G
1f95a 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66  iven the name of
1f95b 20 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65   a column of the
1f95c 20 66 6f 72 6d 20 58 2e 59 2e 5a 20 6f 72 20 59   form X.Y.Z or Y
1f95d 2e 5a 20 6f 72 20 6a 75 73 74 20 5a 2c 20 6c 6f  .Z or just Z, lo
1f95e 6f 6b 20 75 70 0a 2a 2a 20 74 68 61 74 20 6e 61  ok up.** that na
1f95f 6d 65 20 69 6e 20 74 68 65 20 73 65 74 20 6f 66  me in the set of
1f960 20 73 6f 75 72 63 65 20 74 61 62 6c 65 73 20 69   source tables i
1f961 6e 20 70 53 72 63 4c 69 73 74 20 61 6e 64 20 6d  n pSrcList and m
1f962 61 6b 65 20 74 68 65 20 70 45 78 70 72 20 0a 2a  ake the pExpr .*
1f963 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64  * expression nod
1f964 65 20 72 65 66 65 72 20 62 61 63 6b 20 74 6f 20  e refer back to 
1f965 74 68 61 74 20 73 6f 75 72 63 65 20 63 6f 6c 75  that source colu
1f966 6d 6e 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69  mn.  The followi
1f967 6e 67 20 63 68 61 6e 67 65 73 0a 2a 2a 20 61 72  ng changes.** ar
1f968 65 20 6d 61 64 65 20 74 6f 20 70 45 78 70 72 3a  e made to pExpr:
1f969 0a 2a 2a 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d  .**.**    pExpr-
1f96a 3e 69 44 62 20 20 20 20 20 20 20 20 20 20 20 53  >iDb           S
1f96b 65 74 20 74 68 65 20 69 6e 64 65 78 20 69 6e 20  et the index in 
1f96c 64 62 2d 3e 61 44 62 5b 5d 20 6f 66 20 74 68 65  db->aDb[] of the
1f96d 20 64 61 74 61 62 61 73 65 20 58 0a 2a 2a 20 20   database X.**  
1f96e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f96f 20 20 20 20 20 20 20 28 65 76 65 6e 20 69 66 20         (even if 
1f970 58 20 69 73 20 69 6d 70 6c 69 65 64 29 2e 0a 2a  X is implied)..*
1f971 2a 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62  *    pExpr->iTab
1f972 6c 65 20 20 20 20 20 20 20 20 53 65 74 20 74 6f  le        Set to
1f973 20 74 68 65 20 63 75 72 73 6f 72 20 6e 75 6d 62   the cursor numb
1f974 65 72 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65  er for the table
1f975 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 20 20 20   obtained.**    
1f976 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f977 20 20 20 20 20 66 72 6f 6d 20 70 53 72 63 4c 69       from pSrcLi
1f978 73 74 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d  st..**    pExpr-
1f979 3e 70 54 61 62 20 20 20 20 20 20 20 20 20 20 50  >pTab          P
1f97a 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 54 61 62  oints to the Tab
1f97b 6c 65 20 73 74 72 75 63 74 75 72 65 20 6f 66 20  le structure of 
1f97c 58 2e 59 20 28 65 76 65 6e 20 69 66 0a 2a 2a 20  X.Y (even if.** 
1f97d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f97e 20 20 20 20 20 20 20 20 58 20 61 6e 64 2f 6f 72          X and/or
1f97f 20 59 20 61 72 65 20 69 6d 70 6c 69 65 64 2e 29   Y are implied.)
1f980 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 69 43  .**    pExpr->iC
1f981 6f 6c 75 6d 6e 20 20 20 20 20 20 20 53 65 74 20  olumn       Set 
1f982 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 75  to the column nu
1f983 6d 62 65 72 20 77 69 74 68 69 6e 20 74 68 65 20  mber within the 
1f984 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 20 70 45 78  table..**    pEx
1f985 70 72 2d 3e 6f 70 20 20 20 20 20 20 20 20 20 20  pr->op          
1f986 20 20 53 65 74 20 74 6f 20 54 4b 5f 43 4f 4c 55    Set to TK_COLU
1f987 4d 4e 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d  MN..**    pExpr-
1f988 3e 70 4c 65 66 74 20 20 20 20 20 20 20 20 20 41  >pLeft         A
1f989 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68  ny expression th
1f98a 69 73 20 70 6f 69 6e 74 73 20 74 6f 20 69 73 20  is points to is 
1f98b 64 65 6c 65 74 65 64 0a 2a 2a 20 20 20 20 70 45  deleted.**    pE
1f98c 78 70 72 2d 3e 70 52 69 67 68 74 20 20 20 20 20  xpr->pRight     
1f98d 20 20 20 41 6e 79 20 65 78 70 72 65 73 73 69 6f     Any expressio
1f98e 6e 20 74 68 69 73 20 70 6f 69 6e 74 73 20 74 6f  n this points to
1f98f 20 69 73 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 0a   is deleted..**.
1f990 2a 2a 20 54 68 65 20 7a 44 62 20 76 61 72 69 61  ** The zDb varia
1f991 62 6c 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20  ble is the name 
1f992 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
1f993 28 74 68 65 20 22 58 22 29 2e 20 20 54 68 69 73  (the "X").  This
1f994 20 76 61 6c 75 65 20 6d 61 79 20 62 65 0a 2a 2a   value may be.**
1f995 20 4e 55 4c 4c 20 6d 65 61 6e 69 6e 67 20 74 68   NULL meaning th
1f996 61 74 20 6e 61 6d 65 20 69 73 20 6f 66 20 74 68  at name is of th
1f997 65 20 66 6f 72 6d 20 59 2e 5a 20 6f 72 20 5a 2e  e form Y.Z or Z.
1f998 20 20 41 6e 79 20 61 76 61 69 6c 61 62 6c 65 20    Any available 
1f999 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 61 6e 20  database.** can 
1f99a 62 65 20 75 73 65 64 2e 20 20 54 68 65 20 7a 54  be used.  The zT
1f99b 61 62 6c 65 20 76 61 72 69 61 62 6c 65 20 69 73  able variable is
1f99c 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
1f99d 20 74 61 62 6c 65 20 28 74 68 65 20 22 59 22 29   table (the "Y")
1f99e 2e 20 20 54 68 69 73 0a 2a 2a 20 76 61 6c 75 65  .  This.** value
1f99f 20 63 61 6e 20 62 65 20 4e 55 4c 4c 20 69 66 20   can be NULL if 
1f9a0 7a 44 62 20 69 73 20 61 6c 73 6f 20 4e 55 4c 4c  zDb is also NULL
1f9a1 2e 20 20 49 66 20 7a 54 61 62 6c 65 20 69 73 20  .  If zTable is 
1f9a2 4e 55 4c 4c 20 69 74 0a 2a 2a 20 6d 65 61 6e 73  NULL it.** means
1f9a3 20 74 68 61 74 20 74 68 65 20 66 6f 72 6d 20 6f   that the form o
1f9a4 66 20 74 68 65 20 6e 61 6d 65 20 69 73 20 5a 20  f the name is Z 
1f9a5 61 6e 64 20 74 68 61 74 20 63 6f 6c 75 6d 6e 73  and that columns
1f9a6 20 66 72 6f 6d 20 61 6e 79 20 74 61 62 6c 65 0a   from any table.
1f9a7 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 2e 0a  ** can be used..
1f9a8 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 61 6d  **.** If the nam
1f9a9 65 20 63 61 6e 6e 6f 74 20 62 65 20 72 65 73 6f  e cannot be reso
1f9aa 6c 76 65 64 20 75 6e 61 6d 62 69 67 75 6f 75 73  lved unambiguous
1f9ab 6c 79 2c 20 6c 65 61 76 65 20 61 6e 20 65 72 72  ly, leave an err
1f9ac 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 6e  or message.** in
1f9ad 20 70 50 61 72 73 65 20 61 6e 64 20 72 65 74 75   pParse and retu
1f9ae 72 6e 20 57 52 43 5f 41 62 6f 72 74 2e 20 20 52  rn WRC_Abort.  R
1f9af 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 20  eturn WRC_Prune 
1f9b0 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2f 0a 73  on success..*/.s
1f9b1 74 61 74 69 63 20 69 6e 74 20 6c 6f 6f 6b 75 70  tatic int lookup
1f9b2 4e 61 6d 65 28 0a 20 20 50 61 72 73 65 20 2a 70  Name(.  Parse *p
1f9b3 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20  Parse,       /* 
1f9b4 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74  The parsing cont
1f9b5 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ext */.  const c
1f9b6 68 61 72 20 2a 7a 44 62 2c 20 20 20 20 20 2f 2a  har *zDb,     /*
1f9b7 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74   Name of the dat
1f9b8 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67  abase containing
1f9b9 20 74 61 62 6c 65 2c 20 6f 72 20 4e 55 4c 4c 20   table, or NULL 
1f9ba 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
1f9bb 2a 7a 54 61 62 2c 20 20 20 20 2f 2a 20 4e 61 6d  *zTab,    /* Nam
1f9bc 65 20 6f 66 20 74 61 62 6c 65 20 63 6f 6e 74 61  e of table conta
1f9bd 69 6e 69 6e 67 20 63 6f 6c 75 6d 6e 2c 20 6f 72  ining column, or
1f9be 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74   NULL */.  const
1f9bf 20 63 68 61 72 20 2a 7a 43 6f 6c 2c 20 20 20 20   char *zCol,    
1f9c0 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63  /* Name of the c
1f9c1 6f 6c 75 6d 6e 2e 20 2a 2f 0a 20 20 4e 61 6d 65  olumn. */.  Name
1f9c2 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20 20  Context *pNC,   
1f9c3 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 63 6f 6e   /* The name con
1f9c4 74 65 78 74 20 75 73 65 64 20 74 6f 20 72 65 73  text used to res
1f9c5 6f 6c 76 65 20 74 68 65 20 6e 61 6d 65 20 2a 2f  olve the name */
1f9c6 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 20  .  Expr *pExpr  
1f9c7 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20          /* Make 
1f9c8 74 68 69 73 20 45 58 50 52 20 6e 6f 64 65 20 70  this EXPR node p
1f9c9 6f 69 6e 74 20 74 6f 20 74 68 65 20 73 65 6c 65  oint to the sele
1f9ca 63 74 65 64 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 29  cted column */.)
1f9cb 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20  {.  int i, j;   
1f9cc 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
1f9cd 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69   counters */.  i
1f9ce 6e 74 20 63 6e 74 20 3d 20 30 3b 20 20 20 20 20  nt cnt = 0;     
1f9cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f9d0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 61   /* Number of ma
1f9d1 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e 20 6e 61  tching column na
1f9d2 6d 65 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74  mes */.  int cnt
1f9d3 54 61 62 20 3d 20 30 3b 20 20 20 20 20 20 20 20  Tab = 0;        
1f9d4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
1f9d5 6d 62 65 72 20 6f 66 20 6d 61 74 63 68 69 6e 67  mber of matching
1f9d6 20 74 61 62 6c 65 20 6e 61 6d 65 73 20 2a 2f 0a   table names */.
1f9d7 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
1f9d8 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20  pParse->db;     
1f9d9 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
1f9da 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
1f9db 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69  /.  struct SrcLi
1f9dc 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 20  st_item *pItem; 
1f9dd 20 20 20 20 20 20 2f 2a 20 55 73 65 20 66 6f 72        /* Use for
1f9de 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 70 53   looping over pS
1f9df 72 63 4c 69 73 74 20 69 74 65 6d 73 20 2a 2f 0a  rcList items */.
1f9e0 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
1f9e1 5f 69 74 65 6d 20 2a 70 4d 61 74 63 68 20 3d 20  _item *pMatch = 
1f9e2 30 3b 20 20 2f 2a 20 54 68 65 20 6d 61 74 63 68  0;  /* The match
1f9e3 69 6e 67 20 70 53 72 63 4c 69 73 74 20 69 74 65  ing pSrcList ite
1f9e4 6d 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65  m */.  NameConte
1f9e5 78 74 20 2a 70 54 6f 70 4e 43 20 3d 20 70 4e 43  xt *pTopNC = pNC
1f9e6 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73  ;        /* Firs
1f9e7 74 20 6e 61 6d 65 63 6f 6e 74 65 78 74 20 69 6e  t namecontext in
1f9e8 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 53   the list */.  S
1f9e9 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d  chema *pSchema =
1f9ea 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
1f9eb 20 2f 2a 20 53 63 68 65 6d 61 20 6f 66 20 74 68   /* Schema of th
1f9ec 65 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a  e expression */.
1f9ed 20 20 69 6e 74 20 69 73 54 72 69 67 67 65 72 20    int isTrigger 
1f9ee 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  = 0;..  assert( 
1f9ef 70 4e 43 20 29 3b 20 20 20 20 20 2f 2a 20 74 68  pNC );     /* th
1f9f0 65 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 20 63  e name context c
1f9f1 61 6e 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e 20 2a  annot be NULL. *
1f9f2 2f 0a 20 20 61 73 73 65 72 74 28 20 7a 43 6f 6c  /.  assert( zCol
1f9f3 20 29 3b 20 20 20 20 2f 2a 20 54 68 65 20 5a 20   );    /* The Z 
1f9f4 69 6e 20 58 2e 59 2e 5a 20 63 61 6e 6e 6f 74 20  in X.Y.Z cannot 
1f9f5 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 61 73 73  be NULL */.  ass
1f9f6 65 72 74 28 20 7e 45 78 70 72 48 61 73 41 6e 79  ert( ~ExprHasAny
1f9f7 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
1f9f8 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f  EP_TokenOnly|EP_
1f9f9 52 65 64 75 63 65 64 29 20 29 3b 0a 0a 20 20 2f  Reduced) );..  /
1f9fa 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  * Initialize the
1f9fb 20 6e 6f 64 65 20 74 6f 20 6e 6f 2d 6d 61 74 63   node to no-matc
1f9fc 68 20 2a 2f 0a 20 20 70 45 78 70 72 2d 3e 69 54  h */.  pExpr->iT
1f9fd 61 62 6c 65 20 3d 20 2d 31 3b 0a 20 20 70 45 78  able = -1;.  pEx
1f9fe 70 72 2d 3e 70 54 61 62 20 3d 20 30 3b 0a 20 20  pr->pTab = 0;.  
1f9ff 45 78 70 72 53 65 74 49 72 72 65 64 75 63 69 62  ExprSetIrreducib
1fa00 6c 65 28 70 45 78 70 72 29 3b 0a 0a 20 20 2f 2a  le(pExpr);..  /*
1fa01 20 53 74 61 72 74 20 61 74 20 74 68 65 20 69 6e   Start at the in
1fa02 6e 65 72 2d 6d 6f 73 74 20 63 6f 6e 74 65 78 74  ner-most context
1fa03 20 61 6e 64 20 6d 6f 76 65 20 6f 75 74 77 61 72   and move outwar
1fa04 64 20 75 6e 74 69 6c 20 61 20 6d 61 74 63 68 20  d until a match 
1fa05 69 73 20 66 6f 75 6e 64 20 2a 2f 0a 20 20 77 68  is found */.  wh
1fa06 69 6c 65 28 20 70 4e 43 20 26 26 20 63 6e 74 3d  ile( pNC && cnt=
1fa07 3d 30 20 29 7b 0a 20 20 20 20 45 78 70 72 4c 69  =0 ){.    ExprLi
1fa08 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 20 20  st *pEList;.    
1fa09 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73  SrcList *pSrcLis
1fa0a 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73  t = pNC->pSrcLis
1fa0b 74 3b 0a 0a 20 20 20 20 69 66 28 20 70 53 72 63  t;..    if( pSrc
1fa0c 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 66 6f  List ){.      fo
1fa0d 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 53 72  r(i=0, pItem=pSr
1fa0e 63 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 53 72 63  cList->a; i<pSrc
1fa0f 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c  List->nSrc; i++,
1fa10 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20   pItem++){.     
1fa11 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a     Table *pTab;.
1fa12 20 20 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b          int iDb;
1fa13 0a 20 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20  .        Column 
1fa14 2a 70 43 6f 6c 3b 0a 20 20 0a 20 20 20 20 20 20  *pCol;.  .      
1fa15 20 20 70 54 61 62 20 3d 20 70 49 74 65 6d 2d 3e    pTab = pItem->
1fa16 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 61 73  pTab;.        as
1fa17 73 65 72 74 28 20 70 54 61 62 21 3d 30 20 26 26  sert( pTab!=0 &&
1fa18 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 21 3d 30 20   pTab->zName!=0 
1fa19 29 3b 0a 20 20 20 20 20 20 20 20 69 44 62 20 3d  );.        iDb =
1fa1a 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
1fa1b 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e  Index(db, pTab->
1fa1c 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 20  pSchema);.      
1fa1d 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e    assert( pTab->
1fa1e 6e 43 6f 6c 3e 30 20 29 3b 0a 20 20 20 20 20 20  nCol>0 );.      
1fa1f 20 20 69 66 28 20 7a 54 61 62 20 29 7b 0a 20 20    if( zTab ){.  
1fa20 20 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65          if( pIte
1fa21 6d 2d 3e 7a 41 6c 69 61 73 20 29 7b 0a 20 20 20  m->zAlias ){.   
1fa22 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a           char *z
1fa23 54 61 62 4e 61 6d 65 20 3d 20 70 49 74 65 6d 2d  TabName = pItem-
1fa24 3e 7a 41 6c 69 61 73 3b 0a 20 20 20 20 20 20 20  >zAlias;.       
1fa25 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
1fa26 53 74 72 49 43 6d 70 28 7a 54 61 62 4e 61 6d 65  StrICmp(zTabName
1fa27 2c 20 7a 54 61 62 29 21 3d 30 20 29 20 63 6f 6e  , zTab)!=0 ) con
1fa28 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20  tinue;.         
1fa29 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1fa2a 20 20 20 20 63 68 61 72 20 2a 7a 54 61 62 4e 61      char *zTabNa
1fa2b 6d 65 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  me = pTab->zName
1fa2c 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  ;.            if
1fa2d 28 20 4e 45 56 45 52 28 7a 54 61 62 4e 61 6d 65  ( NEVER(zTabName
1fa2e 3d 3d 30 29 20 7c 7c 20 73 71 6c 69 74 65 33 53  ==0) || sqlite3S
1fa2f 74 72 49 43 6d 70 28 7a 54 61 62 4e 61 6d 65 2c  trICmp(zTabName,
1fa30 20 7a 54 61 62 29 21 3d 30 20 29 7b 0a 20 20 20   zTab)!=0 ){.   
1fa31 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69             conti
1fa32 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20  nue;.           
1fa33 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69   }.            i
1fa34 66 28 20 7a 44 62 21 3d 30 20 26 26 20 73 71 6c  f( zDb!=0 && sql
1fa35 69 74 65 33 53 74 72 49 43 6d 70 28 64 62 2d 3e  ite3StrICmp(db->
1fa36 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20  aDb[iDb].zName, 
1fa37 7a 44 62 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  zDb)!=0 ){.     
1fa38 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75           continu
1fa39 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  e;.            }
1fa3a 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
1fa3b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
1fa3c 66 28 20 30 3d 3d 28 63 6e 74 54 61 62 2b 2b 29  f( 0==(cntTab++)
1fa3d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 45   ){.          pE
1fa3e 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 49  xpr->iTable = pI
1fa3f 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20  tem->iCursor;.  
1fa40 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70          pExpr->p
1fa41 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 20 20  Tab = pTab;.    
1fa42 20 20 20 20 20 20 70 53 63 68 65 6d 61 20 3d 20        pSchema = 
1fa43 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a 20  pTab->pSchema;. 
1fa44 20 20 20 20 20 20 20 20 20 70 4d 61 74 63 68 20           pMatch 
1fa45 3d 20 70 49 74 65 6d 3b 0a 20 20 20 20 20 20 20  = pItem;.       
1fa46 20 7d 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a   }.        for(j
1fa47 3d 30 2c 20 70 43 6f 6c 3d 70 54 61 62 2d 3e 61  =0, pCol=pTab->a
1fa48 43 6f 6c 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f  Col; j<pTab->nCo
1fa49 6c 3b 20 6a 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b  l; j++, pCol++){
1fa4a 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73  .          if( s
1fa4b 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43  qlite3StrICmp(pC
1fa4c 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29  ol->zName, zCol)
1fa4d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
1fa4e 20 20 20 49 64 4c 69 73 74 20 2a 70 55 73 69 6e     IdList *pUsin
1fa4f 67 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63  g;.            c
1fa50 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20  nt++;.          
1fa51 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20    pExpr->iTable 
1fa52 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72  = pItem->iCursor
1fa53 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 45  ;.            pE
1fa54 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 54 61 62  xpr->pTab = pTab
1fa55 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 4d  ;.            pM
1fa56 61 74 63 68 20 3d 20 70 49 74 65 6d 3b 0a 20 20  atch = pItem;.  
1fa57 20 20 20 20 20 20 20 20 20 20 70 53 63 68 65 6d            pSchem
1fa58 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  a = pTab->pSchem
1fa59 61 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f  a;.            /
1fa5a 2a 20 53 75 62 73 74 69 74 75 74 65 20 74 68 65  * Substitute the
1fa5b 20 72 6f 77 69 64 20 28 63 6f 6c 75 6d 6e 20 2d   rowid (column -
1fa5c 31 29 20 66 6f 72 20 74 68 65 20 49 4e 54 45 47  1) for the INTEG
1fa5d 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 2a  ER PRIMARY KEY *
1fa5e 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 45  /.            pE
1fa5f 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 6a  xpr->iColumn = j
1fa60 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 3f 20  ==pTab->iPKey ? 
1fa61 2d 31 20 3a 20 28 69 31 36 29 6a 3b 0a 20 20 20  -1 : (i16)j;.   
1fa62 20 20 20 20 20 20 20 20 20 69 66 28 20 69 3c 70           if( i<p
1fa63 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 2d 31 20  SrcList->nSrc-1 
1fa64 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
1fa65 20 69 66 28 20 70 49 74 65 6d 5b 31 5d 2e 6a 6f   if( pItem[1].jo
1fa66 69 6e 74 79 70 65 20 26 20 4a 54 5f 4e 41 54 55  intype & JT_NATU
1fa67 52 41 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 20  RAL ){.         
1fa68 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69         /* If thi
1fa69 73 20 6d 61 74 63 68 20 6f 63 63 75 72 72 65 64  s match occurred
1fa6a 20 69 6e 20 74 68 65 20 6c 65 66 74 20 74 61 62   in the left tab
1fa6b 6c 65 20 6f 66 20 61 20 6e 61 74 75 72 61 6c 20  le of a natural 
1fa6c 6a 6f 69 6e 2c 0a 20 20 20 20 20 20 20 20 20 20  join,.          
1fa6d 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 73 6b        ** then sk
1fa6e 69 70 20 74 68 65 20 72 69 67 68 74 20 74 61 62  ip the right tab
1fa6f 6c 65 20 74 6f 20 61 76 6f 69 64 20 61 20 64 75  le to avoid a du
1fa70 70 6c 69 63 61 74 65 20 6d 61 74 63 68 20 2a 2f  plicate match */
1fa71 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1fa72 20 70 49 74 65 6d 2b 2b 3b 0a 20 20 20 20 20 20   pItem++;.      
1fa73 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20            i++;. 
1fa74 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c               }el
1fa75 73 65 20 69 66 28 20 28 70 55 73 69 6e 67 20 3d  se if( (pUsing =
1fa76 20 70 49 74 65 6d 5b 31 5d 2e 70 55 73 69 6e 67   pItem[1].pUsing
1fa77 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )!=0 ){.        
1fa78 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68          /* If th
1fa79 69 73 20 6d 61 74 63 68 20 6f 63 63 75 72 73 20  is match occurs 
1fa7a 6f 6e 20 61 20 63 6f 6c 75 6d 6e 20 74 68 61 74  on a column that
1fa7b 20 69 73 20 69 6e 20 74 68 65 20 55 53 49 4e 47   is in the USING
1fa7c 20 63 6c 61 75 73 65 0a 20 20 20 20 20 20 20 20   clause.        
1fa7d 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 61 20          ** of a 
1fa7e 6a 6f 69 6e 2c 20 73 6b 69 70 20 74 68 65 20 73  join, skip the s
1fa7f 65 61 72 63 68 20 6f 66 20 74 68 65 20 72 69 67  earch of the rig
1fa80 68 74 20 74 61 62 6c 65 20 6f 66 20 74 68 65 20  ht table of the 
1fa81 6a 6f 69 6e 0a 20 20 20 20 20 20 20 20 20 20 20  join.           
1fa82 20 20 20 20 20 2a 2a 20 74 6f 20 61 76 6f 69 64       ** to avoid
1fa83 20 61 20 64 75 70 6c 69 63 61 74 65 20 6d 61 74   a duplicate mat
1fa84 63 68 20 74 68 65 72 65 2e 20 2a 2f 0a 20 20 20  ch there. */.   
1fa85 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74               int
1fa86 20 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   k;.            
1fa87 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70      for(k=0; k<p
1fa88 55 73 69 6e 67 2d 3e 6e 49 64 3b 20 6b 2b 2b 29  Using->nId; k++)
1fa89 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
1fa8a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
1fa8b 74 72 49 43 6d 70 28 70 55 73 69 6e 67 2d 3e 61  trICmp(pUsing->a
1fa8c 5b 6b 5d 2e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29  [k].zName, zCol)
1fa8d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
1fa8e 20 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d             pItem
1fa8f 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ++;.            
1fa90 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20          i++;.   
1fa91 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa92 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
1fa93 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
1fa94 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
1fa95 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
1fa96 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
1fa97 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
1fa98 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1fa99 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
1fa9a 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   }..#ifndef SQLI
1fa9b 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a  TE_OMIT_TRIGGER.
1fa9c 20 20 20 20 2f 2a 20 49 66 20 77 65 20 68 61 76      /* If we hav
1fa9d 65 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 72 65  e not already re
1fa9e 73 6f 6c 76 65 64 20 74 68 65 20 6e 61 6d 65 2c  solved the name,
1fa9f 20 74 68 65 6e 20 6d 61 79 62 65 20 0a 20 20 20   then maybe .   
1faa0 20 2a 2a 20 69 74 20 69 73 20 61 20 6e 65 77 2e   ** it is a new.
1faa1 2a 20 6f 72 20 6f 6c 64 2e 2a 20 74 72 69 67 67  * or old.* trigg
1faa2 65 72 20 61 72 67 75 6d 65 6e 74 20 72 65 66 65  er argument refe
1faa3 72 65 6e 63 65 0a 20 20 20 20 2a 2f 0a 20 20 20  rence.    */.   
1faa4 20 69 66 28 20 7a 44 62 3d 3d 30 20 26 26 20 7a   if( zDb==0 && z
1faa5 54 61 62 21 3d 30 20 26 26 20 63 6e 74 3d 3d 30  Tab!=0 && cnt==0
1faa6 20 26 26 20 70 50 61 72 73 65 2d 3e 70 54 72 69   && pParse->pTri
1faa7 67 67 65 72 54 61 62 21 3d 30 20 29 7b 0a 20 20  ggerTab!=0 ){.  
1faa8 20 20 20 20 69 6e 74 20 6f 70 20 3d 20 70 50 61      int op = pPa
1faa9 72 73 65 2d 3e 65 54 72 69 67 67 65 72 4f 70 3b  rse->eTriggerOp;
1faaa 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54  .      Table *pT
1faab 61 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 61 73  ab = 0;.      as
1faac 73 65 72 74 28 20 6f 70 3d 3d 54 4b 5f 44 45 4c  sert( op==TK_DEL
1faad 45 54 45 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 55 50  ETE || op==TK_UP
1faae 44 41 54 45 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 49  DATE || op==TK_I
1faaf 4e 53 45 52 54 20 29 3b 0a 20 20 20 20 20 20 69  NSERT );.      i
1fab0 66 28 20 6f 70 21 3d 54 4b 5f 44 45 4c 45 54 45  f( op!=TK_DELETE
1fab1 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43   && sqlite3StrIC
1fab2 6d 70 28 22 6e 65 77 22 2c 7a 54 61 62 29 20 3d  mp("new",zTab) =
1fab3 3d 20 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70  = 0 ){.        p
1fab4 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 31  Expr->iTable = 1
1fab5 3b 0a 20 20 20 20 20 20 20 20 70 54 61 62 20 3d  ;.        pTab =
1fab6 20 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65   pParse->pTrigge
1fab7 72 54 61 62 3b 0a 20 20 20 20 20 20 7d 65 6c 73  rTab;.      }els
1fab8 65 20 69 66 28 20 6f 70 21 3d 54 4b 5f 49 4e 53  e if( op!=TK_INS
1fab9 45 52 54 20 26 26 20 73 71 6c 69 74 65 33 53 74  ERT && sqlite3St
1faba 72 49 43 6d 70 28 22 6f 6c 64 22 2c 7a 54 61 62  rICmp("old",zTab
1fabb 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
1fabc 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20  pExpr->iTable = 
1fabd 30 3b 0a 20 20 20 20 20 20 20 20 70 54 61 62 20  0;.        pTab 
1fabe 3d 20 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67  = pParse->pTrigg
1fabf 65 72 54 61 62 3b 0a 20 20 20 20 20 20 7d 0a 0a  erTab;.      }..
1fac0 20 20 20 20 20 20 69 66 28 20 70 54 61 62 20 29        if( pTab )
1fac1 7b 20 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  { .        int i
1fac2 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 53 63  Col;.        pSc
1fac3 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63  hema = pTab->pSc
1fac4 68 65 6d 61 3b 0a 20 20 20 20 20 20 20 20 63 6e  hema;.        cn
1fac5 74 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20 20  tTab++;.        
1fac6 69 66 28 20 73 71 6c 69 74 65 33 49 73 52 6f 77  if( sqlite3IsRow
1fac7 69 64 28 7a 43 6f 6c 29 20 29 7b 0a 20 20 20 20  id(zCol) ){.    
1fac8 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 2d 31 3b        iCol = -1;
1fac9 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
1faca 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 43            for(iC
1facb 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54 61 62 2d  ol=0; iCol<pTab-
1facc 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b 0a  >nCol; iCol++){.
1facd 20 20 20 20 20 20 20 20 20 20 20 20 43 6f 6c 75              Colu
1face 6d 6e 20 2a 70 43 6f 6c 20 3d 20 26 70 54 61 62  mn *pCol = &pTab
1facf 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 3b 0a 20 20  ->aCol[iCol];.  
1fad0 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71            if( sq
1fad1 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f  lite3StrICmp(pCo
1fad2 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3d  l->zName, zCol)=
1fad3 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
1fad4 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d 70 54      if( iCol==pT
1fad5 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20  ab->iPKey ){.   
1fad6 20 20 20 20 20 20 20 20 20 20 20 20 20 69 43 6f               iCo
1fad7 6c 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20  l = -1;.        
1fad8 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1fad9 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1fada 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1fadb 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
1fadc 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f  .        if( iCo
1fadd 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a  l<pTab->nCol ){.
1fade 20 20 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b            cnt++;
1fadf 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69  .          if( i
1fae0 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 20  Col<0 ){.       
1fae1 20 20 20 20 20 70 45 78 70 72 2d 3e 61 66 66 69       pExpr->affi
1fae2 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 46  nity = SQLITE_AF
1fae3 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20  F_INTEGER;.     
1fae4 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70       }else if( p
1fae5 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 30 20  Expr->iTable==0 
1fae6 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 74  ){.            t
1fae7 65 73 74 63 61 73 65 28 20 69 43 6f 6c 3d 3d 33  estcase( iCol==3
1fae8 31 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  1 );.           
1fae9 20 74 65 73 74 63 61 73 65 28 20 69 43 6f 6c 3d   testcase( iCol=
1faea 3d 33 32 20 29 3b 0a 20 20 20 20 20 20 20 20 20  =32 );.         
1faeb 20 20 20 70 50 61 72 73 65 2d 3e 6f 6c 64 6d 61     pParse->oldma
1faec 73 6b 20 7c 3d 20 28 69 43 6f 6c 3e 3d 33 32 20  sk |= (iCol>=32 
1faed 3f 20 30 78 66 66 66 66 66 66 66 66 20 3a 20 28  ? 0xffffffff : (
1faee 28 28 75 33 32 29 31 29 3c 3c 69 43 6f 6c 29 29  ((u32)1)<<iCol))
1faef 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
1faf0 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69          pExpr->i
1faf1 43 6f 6c 75 6d 6e 20 3d 20 28 69 31 36 29 69 43  Column = (i16)iC
1faf2 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 70 45  ol;.          pE
1faf3 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 54 61 62  xpr->pTab = pTab
1faf4 3b 0a 20 20 20 20 20 20 20 20 20 20 69 73 54 72  ;.          isTr
1faf5 69 67 67 65 72 20 3d 20 31 3b 0a 20 20 20 20 20  igger = 1;.     
1faf6 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
1faf7 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65   }.#endif /* !de
1faf8 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
1faf9 54 5f 54 52 49 47 47 45 52 29 20 2a 2f 0a 0a 20  T_TRIGGER) */.. 
1fafa 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 50 65 72     /*.    ** Per
1fafb 68 61 70 73 20 74 68 65 20 6e 61 6d 65 20 69 73  haps the name is
1fafc 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20   a reference to 
1fafd 74 68 65 20 52 4f 57 49 44 0a 20 20 20 20 2a 2f  the ROWID.    */
1fafe 0a 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 20  .    if( cnt==0 
1faff 26 26 20 63 6e 74 54 61 62 3d 3d 31 20 26 26 20  && cntTab==1 && 
1fb00 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 7a  sqlite3IsRowid(z
1fb01 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 63 6e  Col) ){.      cn
1fb02 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 45 78  t = 1;.      pEx
1fb03 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31  pr->iColumn = -1
1fb04 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 61  ;.      pExpr->a
1fb05 66 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45  ffinity = SQLITE
1fb06 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20  _AFF_INTEGER;.  
1fb07 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20    }..    /*.    
1fb08 2a 2a 20 49 66 20 74 68 65 20 69 6e 70 75 74 20  ** If the input 
1fb09 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 5a  is of the form Z
1fb0a 20 28 6e 6f 74 20 59 2e 5a 20 6f 72 20 58 2e 59   (not Y.Z or X.Y
1fb0b 2e 5a 29 20 74 68 65 6e 20 74 68 65 20 6e 61 6d  .Z) then the nam
1fb0c 65 20 5a 0a 20 20 20 20 2a 2a 20 6d 69 67 68 74  e Z.    ** might
1fb0d 20 72 65 66 65 72 20 74 6f 20 61 6e 20 72 65 73   refer to an res
1fb0e 75 6c 74 2d 73 65 74 20 61 6c 69 61 73 2e 20 20  ult-set alias.  
1fb0f 54 68 69 73 20 68 61 70 70 65 6e 73 2c 20 66 6f  This happens, fo
1fb10 72 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 0a  r example, when.
1fb11 20 20 20 20 2a 2a 20 77 65 20 61 72 65 20 72 65      ** we are re
1fb12 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20 69 6e  solving names in
1fb13 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
1fb14 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
1fb15 6e 67 20 63 6f 6d 6d 61 6e 64 3a 0a 20 20 20 20  ng command:.    
1fb16 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 53 45  **.    **     SE
1fb17 4c 45 43 54 20 61 2b 62 20 41 53 20 78 20 46 52  LECT a+b AS x FR
1fb18 4f 4d 20 74 61 62 6c 65 20 57 48 45 52 45 20 78  OM table WHERE x
1fb19 3c 31 30 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20  <10;.    **.    
1fb1a 2a 2a 20 49 6e 20 63 61 73 65 73 20 6c 69 6b 65  ** In cases like
1fb1b 20 74 68 69 73 2c 20 72 65 70 6c 61 63 65 20 70   this, replace p
1fb1c 45 78 70 72 20 77 69 74 68 20 61 20 63 6f 70 79  Expr with a copy
1fb1d 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69   of the expressi
1fb1e 6f 6e 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 66  on that.    ** f
1fb1f 6f 72 6d 73 20 74 68 65 20 72 65 73 75 6c 74 20  orms the result 
1fb20 73 65 74 20 65 6e 74 72 79 20 28 22 61 2b 62 22  set entry ("a+b"
1fb21 20 69 6e 20 74 68 65 20 65 78 61 6d 70 6c 65 29   in the example)
1fb22 20 61 6e 64 20 72 65 74 75 72 6e 20 69 6d 6d 65   and return imme
1fb23 64 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2a 20  diately..    ** 
1fb24 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 65 78  Note that the ex
1fb25 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 20  pression in the 
1fb26 72 65 73 75 6c 74 20 73 65 74 20 73 68 6f 75 6c  result set shoul
1fb27 64 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62  d have already b
1fb28 65 65 6e 0a 20 20 20 20 2a 2a 20 72 65 73 6f 6c  een.    ** resol
1fb29 76 65 64 20 62 79 20 74 68 65 20 74 69 6d 65 20  ved by the time 
1fb2a 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
1fb2b 20 69 73 20 72 65 73 6f 6c 76 65 64 2e 0a 20 20   is resolved..  
1fb2c 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e 74    */.    if( cnt
1fb2d 3d 3d 30 20 26 26 20 28 70 45 4c 69 73 74 20 3d  ==0 && (pEList =
1fb2e 20 70 4e 43 2d 3e 70 45 4c 69 73 74 29 21 3d 30   pNC->pEList)!=0
1fb2f 20 26 26 20 7a 54 61 62 3d 3d 30 20 29 7b 0a 20   && zTab==0 ){. 
1fb30 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
1fb31 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 6a  pEList->nExpr; j
1fb32 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61  ++){.        cha
1fb33 72 20 2a 7a 41 73 20 3d 20 70 45 4c 69 73 74 2d  r *zAs = pEList-
1fb34 3e 61 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20  >a[j].zName;.   
1fb35 20 20 20 20 20 69 66 28 20 7a 41 73 21 3d 30 20       if( zAs!=0 
1fb36 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  && sqlite3StrICm
1fb37 70 28 7a 41 73 2c 20 7a 43 6f 6c 29 3d 3d 30 20  p(zAs, zCol)==0 
1fb38 29 7b 0a 20 20 20 20 20 20 20 20 20 20 45 78 70  ){.          Exp
1fb39 72 20 2a 70 4f 72 69 67 3b 0a 20 20 20 20 20 20  r *pOrig;.      
1fb3a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70      assert( pExp
1fb3b 72 2d 3e 70 4c 65 66 74 3d 3d 30 20 26 26 20 70  r->pLeft==0 && p
1fb3c 45 78 70 72 2d 3e 70 52 69 67 68 74 3d 3d 30 20  Expr->pRight==0 
1fb3d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  );.          ass
1fb3e 65 72 74 28 20 70 45 78 70 72 2d 3e 78 2e 70 4c  ert( pExpr->x.pL
1fb3f 69 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ist==0 );.      
1fb40 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70      assert( pExp
1fb41 72 2d 3e 78 2e 70 53 65 6c 65 63 74 3d 3d 30 20  r->x.pSelect==0 
1fb42 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 72  );.          pOr
1fb43 69 67 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 6a  ig = pEList->a[j
1fb44 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20  ].pExpr;.       
1fb45 20 20 20 69 66 28 20 21 70 4e 43 2d 3e 61 6c 6c     if( !pNC->all
1fb46 6f 77 41 67 67 20 26 26 20 45 78 70 72 48 61 73  owAgg && ExprHas
1fb47 50 72 6f 70 65 72 74 79 28 70 4f 72 69 67 2c 20  Property(pOrig, 
1fb48 45 50 5f 41 67 67 29 20 29 7b 0a 20 20 20 20 20  EP_Agg) ){.     
1fb49 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
1fb4a 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
1fb4b 6d 69 73 75 73 65 20 6f 66 20 61 6c 69 61 73 65  misuse of aliase
1fb4c 64 20 61 67 67 72 65 67 61 74 65 20 25 73 22 2c  d aggregate %s",
1fb4d 20 7a 41 73 29 3b 0a 20 20 20 20 20 20 20 20 20   zAs);.         
1fb4e 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62     return WRC_Ab
1fb4f 6f 72 74 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  ort;.          }
1fb50 0a 20 20 20 20 20 20 20 20 20 20 72 65 73 6f 6c  .          resol
1fb51 76 65 41 6c 69 61 73 28 70 50 61 72 73 65 2c 20  veAlias(pParse, 
1fb52 70 45 4c 69 73 74 2c 20 6a 2c 20 70 45 78 70 72  pEList, j, pExpr
1fb53 2c 20 22 22 29 3b 0a 20 20 20 20 20 20 20 20 20  , "");.         
1fb54 20 63 6e 74 20 3d 20 31 3b 0a 20 20 20 20 20 20   cnt = 1;.      
1fb55 20 20 20 20 70 4d 61 74 63 68 20 3d 20 30 3b 0a      pMatch = 0;.
1fb56 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
1fb57 28 20 7a 54 61 62 3d 3d 30 20 26 26 20 7a 44 62  ( zTab==0 && zDb
1fb58 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20  ==0 );.         
1fb59 20 67 6f 74 6f 20 6c 6f 6f 6b 75 70 6e 61 6d 65   goto lookupname
1fb5a 5f 65 6e 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a  _end;.        }.
1fb5b 20 20 20 20 20 20 7d 20 0a 20 20 20 20 7d 0a 0a        } .    }..
1fb5c 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 74      /* Advance t
1fb5d 6f 20 74 68 65 20 6e 65 78 74 20 6e 61 6d 65 20  o the next name 
1fb5e 63 6f 6e 74 65 78 74 2e 20 20 54 68 65 20 6c 6f  context.  The lo
1fb5f 6f 70 20 77 69 6c 6c 20 65 78 69 74 20 77 68 65  op will exit whe
1fb60 6e 20 65 69 74 68 65 72 0a 20 20 20 20 2a 2a 20  n either.    ** 
1fb61 77 65 20 68 61 76 65 20 61 20 6d 61 74 63 68 20  we have a match 
1fb62 28 63 6e 74 3e 30 29 20 6f 72 20 77 68 65 6e 20  (cnt>0) or when 
1fb63 77 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 6e 61  we run out of na
1fb64 6d 65 20 63 6f 6e 74 65 78 74 73 2e 0a 20 20 20  me contexts..   
1fb65 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e 74 3d   */.    if( cnt=
1fb66 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 4e 43 20  =0 ){.      pNC 
1fb67 3d 20 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 20  = pNC->pNext;.  
1fb68 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20    }.  }..  /*.  
1fb69 2a 2a 20 49 66 20 58 20 61 6e 64 20 59 20 61 72  ** If X and Y ar
1fb6a 65 20 4e 55 4c 4c 20 28 69 6e 20 6f 74 68 65 72  e NULL (in other
1fb6b 20 77 6f 72 64 73 20 69 66 20 6f 6e 6c 79 20 74   words if only t
1fb6c 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 5a  he column name Z
1fb6d 20 69 73 0a 20 20 2a 2a 20 73 75 70 70 6c 69 65   is.  ** supplie
1fb6e 64 29 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65  d) and the value
1fb6f 20 6f 66 20 5a 20 69 73 20 65 6e 63 6c 6f 73 65   of Z is enclose
1fb70 64 20 69 6e 20 64 6f 75 62 6c 65 2d 71 75 6f 74  d in double-quot
1fb71 65 73 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 5a 20  es, then.  ** Z 
1fb72 69 73 20 61 20 73 74 72 69 6e 67 20 6c 69 74 65  is a string lite
1fb73 72 61 6c 20 69 66 20 69 74 20 64 6f 65 73 6e 27  ral if it doesn'
1fb74 74 20 6d 61 74 63 68 20 61 6e 79 20 63 6f 6c 75  t match any colu
1fb75 6d 6e 20 6e 61 6d 65 73 2e 20 20 49 6e 20 74 68  mn names.  In th
1fb76 61 74 0a 20 20 2a 2a 20 63 61 73 65 2c 20 77 65  at.  ** case, we
1fb77 20 6e 65 65 64 20 74 6f 20 72 65 74 75 72 6e 20   need to return 
1fb78 72 69 67 68 74 20 61 77 61 79 20 61 6e 64 20 6e  right away and n
1fb79 6f 74 20 6d 61 6b 65 20 61 6e 79 20 63 68 61 6e  ot make any chan
1fb7a 67 65 73 20 74 6f 0a 20 20 2a 2a 20 70 45 78 70  ges to.  ** pExp
1fb7b 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 42 65 63  r..  **.  ** Bec
1fb7c 61 75 73 65 20 6e 6f 20 72 65 66 65 72 65 6e 63  ause no referenc
1fb7d 65 20 77 61 73 20 6d 61 64 65 20 74 6f 20 6f 75  e was made to ou
1fb7e 74 65 72 20 63 6f 6e 74 65 78 74 73 2c 20 74 68  ter contexts, th
1fb7f 65 20 70 4e 43 2d 3e 6e 52 65 66 0a 20 20 2a 2a  e pNC->nRef.  **
1fb80 20 66 69 65 6c 64 73 20 61 72 65 20 6e 6f 74 20   fields are not 
1fb81 63 68 61 6e 67 65 64 20 69 6e 20 61 6e 79 20 63  changed in any c
1fb82 6f 6e 74 65 78 74 2e 0a 20 20 2a 2f 0a 20 20 69  ontext..  */.  i
1fb83 66 28 20 63 6e 74 3d 3d 30 20 26 26 20 7a 54 61  f( cnt==0 && zTa
1fb84 62 3d 3d 30 20 26 26 20 45 78 70 72 48 61 73 50  b==0 && ExprHasP
1fb85 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 45 50  roperty(pExpr,EP
1fb86 5f 44 62 6c 51 75 6f 74 65 64 29 20 29 7b 0a 20  _DblQuoted) ){. 
1fb87 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54     pExpr->op = T
1fb88 4b 5f 53 54 52 49 4e 47 3b 0a 20 20 20 20 70 45  K_STRING;.    pE
1fb89 78 70 72 2d 3e 70 54 61 62 20 3d 20 30 3b 0a 20  xpr->pTab = 0;. 
1fb8a 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72     return WRC_Pr
1fb8b 75 6e 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20  une;.  }..  /*. 
1fb8c 20 2a 2a 20 63 6e 74 3d 3d 30 20 6d 65 61 6e 73   ** cnt==0 means
1fb8d 20 74 68 65 72 65 20 77 61 73 20 6e 6f 74 20 6d   there was not m
1fb8e 61 74 63 68 2e 20 20 63 6e 74 3e 31 20 6d 65 61  atch.  cnt>1 mea
1fb8f 6e 73 20 74 68 65 72 65 20 77 65 72 65 20 74 77  ns there were tw
1fb90 6f 20 6f 72 0a 20 20 2a 2a 20 6d 6f 72 65 20 6d  o or.  ** more m
1fb91 61 74 63 68 65 73 2e 20 20 45 69 74 68 65 72 20  atches.  Either 
1fb92 77 61 79 2c 20 77 65 20 68 61 76 65 20 61 6e 20  way, we have an 
1fb93 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66  error..  */.  if
1fb94 28 20 63 6e 74 21 3d 31 20 29 7b 0a 20 20 20 20  ( cnt!=1 ){.    
1fb95 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 72 72  const char *zErr
1fb96 3b 0a 20 20 20 20 7a 45 72 72 20 3d 20 63 6e 74  ;.    zErr = cnt
1fb97 3d 3d 30 20 3f 20 22 6e 6f 20 73 75 63 68 20 63  ==0 ? "no such c
1fb98 6f 6c 75 6d 6e 22 20 3a 20 22 61 6d 62 69 67 75  olumn" : "ambigu
1fb99 6f 75 73 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 22  ous column name"
1fb9a 3b 0a 20 20 20 20 69 66 28 20 7a 44 62 20 29 7b  ;.    if( zDb ){
1fb9b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
1fb9c 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
1fb9d 25 73 3a 20 25 73 2e 25 73 2e 25 73 22 2c 20 7a  %s: %s.%s.%s", z
1fb9e 45 72 72 2c 20 7a 44 62 2c 20 7a 54 61 62 2c 20  Err, zDb, zTab, 
1fb9f 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d 65 6c 73 65  zCol);.    }else
1fba0 20 69 66 28 20 7a 54 61 62 20 29 7b 0a 20 20 20   if( zTab ){.   
1fba1 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1fba2 73 67 28 70 50 61 72 73 65 2c 20 22 25 73 3a 20  sg(pParse, "%s: 
1fba3 25 73 2e 25 73 22 2c 20 7a 45 72 72 2c 20 7a 54  %s.%s", zErr, zT
1fba4 61 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d  ab, zCol);.    }
1fba5 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
1fba6 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1fba7 73 65 2c 20 22 25 73 3a 20 25 73 22 2c 20 7a 45  se, "%s: %s", zE
1fba8 72 72 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d  rr, zCol);.    }
1fba9 0a 20 20 20 20 70 54 6f 70 4e 43 2d 3e 6e 45 72  .    pTopNC->nEr
1fbaa 72 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  r++;.  }..  /* I
1fbab 66 20 61 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20  f a column from 
1fbac 61 20 74 61 62 6c 65 20 69 6e 20 70 53 72 63 4c  a table in pSrcL
1fbad 69 73 74 20 69 73 20 72 65 66 65 72 65 6e 63 65  ist is reference
1fbae 64 2c 20 74 68 65 6e 20 72 65 63 6f 72 64 0a 20  d, then record. 
1fbaf 20 2a 2a 20 74 68 69 73 20 66 61 63 74 20 69 6e   ** this fact in
1fbb0 20 74 68 65 20 70 53 72 63 4c 69 73 74 2e 61 5b   the pSrcList.a[
1fbb1 5d 2e 63 6f 6c 55 73 65 64 20 62 69 74 6d 61 73  ].colUsed bitmas
1fbb2 6b 2e 20 20 43 6f 6c 75 6d 6e 20 30 20 63 61 75  k.  Column 0 cau
1fbb3 73 65 73 0a 20 20 2a 2a 20 62 69 74 20 30 20 74  ses.  ** bit 0 t
1fbb4 6f 20 62 65 20 73 65 74 2e 20 20 43 6f 6c 75 6d  o be set.  Colum
1fbb5 6e 20 31 20 73 65 74 73 20 62 69 74 20 31 2e 20  n 1 sets bit 1. 
1fbb6 20 41 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20   And so forth.  
1fbb7 49 66 20 74 68 65 0a 20 20 2a 2a 20 63 6f 6c 75  If the.  ** colu
1fbb8 6d 6e 20 6e 75 6d 62 65 72 20 69 73 20 67 72 65  mn number is gre
1fbb9 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 6e 75  ater than the nu
1fbba 6d 62 65 72 20 6f 66 20 62 69 74 73 20 69 6e 20  mber of bits in 
1fbbb 74 68 65 20 62 69 74 6d 61 73 6b 0a 20 20 2a 2a  the bitmask.  **
1fbbc 20 74 68 65 6e 20 73 65 74 20 74 68 65 20 68 69   then set the hi
1fbbd 67 68 2d 6f 72 64 65 72 20 62 69 74 20 6f 66 20  gh-order bit of 
1fbbe 74 68 65 20 62 69 74 6d 61 73 6b 2e 0a 20 20 2a  the bitmask..  *
1fbbf 2f 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69  /.  if( pExpr->i
1fbc0 43 6f 6c 75 6d 6e 3e 3d 30 20 26 26 20 70 4d 61  Column>=0 && pMa
1fbc1 74 63 68 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e  tch!=0 ){.    in
1fbc2 74 20 6e 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f  t n = pExpr->iCo
1fbc3 6c 75 6d 6e 3b 0a 20 20 20 20 74 65 73 74 63 61  lumn;.    testca
1fbc4 73 65 28 20 6e 3d 3d 42 4d 53 2d 31 20 29 3b 0a  se( n==BMS-1 );.
1fbc5 20 20 20 20 69 66 28 20 6e 3e 3d 42 4d 53 20 29      if( n>=BMS )
1fbc6 7b 0a 20 20 20 20 20 20 6e 20 3d 20 42 4d 53 2d  {.      n = BMS-
1fbc7 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  1;.    }.    ass
1fbc8 65 72 74 28 20 70 4d 61 74 63 68 2d 3e 69 43 75  ert( pMatch->iCu
1fbc9 72 73 6f 72 3d 3d 70 45 78 70 72 2d 3e 69 54 61  rsor==pExpr->iTa
1fbca 62 6c 65 20 29 3b 0a 20 20 20 20 70 4d 61 74 63  ble );.    pMatc
1fbcb 68 2d 3e 63 6f 6c 55 73 65 64 20 7c 3d 20 28 28  h->colUsed |= ((
1fbcc 42 69 74 6d 61 73 6b 29 31 29 3c 3c 6e 3b 0a 20  Bitmask)1)<<n;. 
1fbcd 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e 20 75   }..  /* Clean u
1fbce 70 20 61 6e 64 20 72 65 74 75 72 6e 0a 20 20 2a  p and return.  *
1fbcf 2f 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44  /.  sqlite3ExprD
1fbd0 65 6c 65 74 65 28 64 62 2c 20 70 45 78 70 72 2d  elete(db, pExpr-
1fbd1 3e 70 4c 65 66 74 29 3b 0a 20 20 70 45 78 70 72  >pLeft);.  pExpr
1fbd2 2d 3e 70 4c 65 66 74 20 3d 20 30 3b 0a 20 20 73  ->pLeft = 0;.  s
1fbd3 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
1fbd4 28 64 62 2c 20 70 45 78 70 72 2d 3e 70 52 69 67  (db, pExpr->pRig
1fbd5 68 74 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70 52  ht);.  pExpr->pR
1fbd6 69 67 68 74 20 3d 20 30 3b 0a 20 20 70 45 78 70  ight = 0;.  pExp
1fbd7 72 2d 3e 6f 70 20 3d 20 28 69 73 54 72 69 67 67  r->op = (isTrigg
1fbd8 65 72 20 3f 20 54 4b 5f 54 52 49 47 47 45 52 20  er ? TK_TRIGGER 
1fbd9 3a 20 54 4b 5f 43 4f 4c 55 4d 4e 29 3b 0a 6c 6f  : TK_COLUMN);.lo
1fbda 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 3a 0a 20 20  okupname_end:.  
1fbdb 69 66 28 20 63 6e 74 3d 3d 31 20 29 7b 0a 20 20  if( cnt==1 ){.  
1fbdc 20 20 61 73 73 65 72 74 28 20 70 4e 43 21 3d 30    assert( pNC!=0
1fbdd 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 41   );.    sqlite3A
1fbde 75 74 68 52 65 61 64 28 70 50 61 72 73 65 2c 20  uthRead(pParse, 
1fbdf 70 45 78 70 72 2c 20 70 53 63 68 65 6d 61 2c 20  pExpr, pSchema, 
1fbe0 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 29 3b 0a  pNC->pSrcList);.
1fbe1 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74      /* Increment
1fbe2 20 74 68 65 20 6e 52 65 66 20 76 61 6c 75 65 20   the nRef value 
1fbe3 6f 6e 20 61 6c 6c 20 6e 61 6d 65 20 63 6f 6e 74  on all name cont
1fbe4 65 78 74 73 20 66 72 6f 6d 20 54 6f 70 4e 43 20  exts from TopNC 
1fbe5 75 70 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65  up to.    ** the
1fbe6 20 70 6f 69 6e 74 20 77 68 65 72 65 20 74 68 65   point where the
1fbe7 20 6e 61 6d 65 20 6d 61 74 63 68 65 64 2e 20 2a   name matched. *
1fbe8 2f 0a 20 20 20 20 66 6f 72 28 3b 3b 29 7b 0a 20  /.    for(;;){. 
1fbe9 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54 6f       assert( pTo
1fbea 70 4e 43 21 3d 30 20 29 3b 0a 20 20 20 20 20 20  pNC!=0 );.      
1fbeb 70 54 6f 70 4e 43 2d 3e 6e 52 65 66 2b 2b 3b 0a  pTopNC->nRef++;.
1fbec 20 20 20 20 20 20 69 66 28 20 70 54 6f 70 4e 43        if( pTopNC
1fbed 3d 3d 70 4e 43 20 29 20 62 72 65 61 6b 3b 0a 20  ==pNC ) break;. 
1fbee 20 20 20 20 20 70 54 6f 70 4e 43 20 3d 20 70 54       pTopNC = pT
1fbef 6f 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  opNC->pNext;.   
1fbf0 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52   }.    return WR
1fbf1 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 20 65 6c 73  C_Prune;.  } els
1fbf2 65 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57  e {.    return W
1fbf3 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 7d 0a  RC_Abort;.  }.}.
1fbf4 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
1fbf5 69 6e 65 20 69 73 20 63 61 6c 6c 62 61 63 6b 20  ine is callback 
1fbf6 66 6f 72 20 73 71 6c 69 74 65 33 57 61 6c 6b 45  for sqlite3WalkE
1fbf7 78 70 72 28 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 73  xpr()..**.** Res
1fbf8 6f 6c 76 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61  olve symbolic na
1fbf9 6d 65 73 20 69 6e 74 6f 20 54 4b 5f 43 4f 4c 55  mes into TK_COLU
1fbfa 4d 4e 20 6f 70 65 72 61 74 6f 72 73 20 66 6f 72  MN operators for
1fbfb 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20   the current.** 
1fbfc 6e 6f 64 65 20 69 6e 20 74 68 65 20 65 78 70 72  node in the expr
1fbfd 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65  ession tree.  Re
1fbfe 74 75 72 6e 20 30 20 74 6f 20 63 6f 6e 74 69 6e  turn 0 to contin
1fbff 75 65 20 74 68 65 20 73 65 61 72 63 68 20 64 6f  ue the search do
1fc00 77 6e 0a 2a 2a 20 74 68 65 20 74 72 65 65 20 6f  wn.** the tree o
1fc01 72 20 32 20 74 6f 20 61 62 6f 72 74 20 74 68 65  r 2 to abort the
1fc02 20 74 72 65 65 20 77 61 6c 6b 2e 0a 2a 2a 0a 2a   tree walk..**.*
1fc03 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61  * This routine a
1fc04 6c 73 6f 20 64 6f 65 73 20 65 72 72 6f 72 20 63  lso does error c
1fc05 68 65 63 6b 69 6e 67 20 61 6e 64 20 6e 61 6d 65  hecking and name
1fc06 20 72 65 73 6f 6c 75 74 69 6f 6e 20 66 6f 72 0a   resolution for.
1fc07 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65  ** function name
1fc08 73 2e 20 20 54 68 65 20 6f 70 65 72 61 74 6f 72  s.  The operator
1fc09 20 66 6f 72 20 61 67 67 72 65 67 61 74 65 20 66   for aggregate f
1fc0a 75 6e 63 74 69 6f 6e 73 20 69 73 20 63 68 61 6e  unctions is chan
1fc0b 67 65 64 0a 2a 2a 20 74 6f 20 54 4b 5f 41 47 47  ged.** to TK_AGG
1fc0c 5f 46 55 4e 43 54 49 4f 4e 2e 0a 2a 2f 0a 73 74  _FUNCTION..*/.st
1fc0d 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65  atic int resolve
1fc0e 45 78 70 72 53 74 65 70 28 57 61 6c 6b 65 72 20  ExprStep(Walker 
1fc0f 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a  *pWalker, Expr *
1fc10 70 45 78 70 72 29 7b 0a 20 20 4e 61 6d 65 43 6f  pExpr){.  NameCo
1fc11 6e 74 65 78 74 20 2a 70 4e 43 3b 0a 20 20 50 61  ntext *pNC;.  Pa
1fc12 72 73 65 20 2a 70 50 61 72 73 65 3b 0a 0a 20 20  rse *pParse;..  
1fc13 70 4e 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75  pNC = pWalker->u
1fc14 2e 70 4e 43 3b 0a 20 20 61 73 73 65 72 74 28 20  .pNC;.  assert( 
1fc15 70 4e 43 21 3d 30 20 29 3b 0a 20 20 70 50 61 72  pNC!=0 );.  pPar
1fc16 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65  se = pNC->pParse
1fc17 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72  ;.  assert( pPar
1fc18 73 65 3d 3d 70 57 61 6c 6b 65 72 2d 3e 70 50 61  se==pWalker->pPa
1fc19 72 73 65 20 29 3b 0a 0a 20 20 69 66 28 20 45 78  rse );..  if( Ex
1fc1a 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79  prHasAnyProperty
1fc1b 28 70 45 78 70 72 2c 20 45 50 5f 52 65 73 6f 6c  (pExpr, EP_Resol
1fc1c 76 65 64 29 20 29 20 72 65 74 75 72 6e 20 57 52  ved) ) return WR
1fc1d 43 5f 50 72 75 6e 65 3b 0a 20 20 45 78 70 72 53  C_Prune;.  ExprS
1fc1e 65 74 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  etProperty(pExpr
1fc1f 2c 20 45 50 5f 52 65 73 6f 6c 76 65 64 29 3b 0a  , EP_Resolved);.
1fc20 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
1fc21 20 69 66 28 20 70 4e 43 2d 3e 70 53 72 63 4c 69   if( pNC->pSrcLi
1fc22 73 74 20 26 26 20 70 4e 43 2d 3e 70 53 72 63 4c  st && pNC->pSrcL
1fc23 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3e 30 20 29 7b  ist->nAlloc>0 ){
1fc24 0a 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53  .    SrcList *pS
1fc25 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53  rcList = pNC->pS
1fc26 72 63 4c 69 73 74 3b 0a 20 20 20 20 69 6e 74 20  rcList;.    int 
1fc27 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  i;.    for(i=0; 
1fc28 69 3c 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 2d  i<pNC->pSrcList-
1fc29 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nSrc; i++){.   
1fc2a 20 20 20 61 73 73 65 72 74 28 20 70 53 72 63 4c     assert( pSrcL
1fc2b 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f  ist->a[i].iCurso
1fc2c 72 3e 3d 30 20 26 26 20 70 53 72 63 4c 69 73 74  r>=0 && pSrcList
1fc2d 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 3c 70  ->a[i].iCursor<p
1fc2e 50 61 72 73 65 2d 3e 6e 54 61 62 29 3b 0a 20 20  Parse->nTab);.  
1fc2f 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20    }.  }.#endif. 
1fc30 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e   switch( pExpr->
1fc31 6f 70 20 29 7b 0a 0a 23 69 66 20 64 65 66 69 6e  op ){..#if defin
1fc32 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
1fc33 5f 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c  _UPDATE_DELETE_L
1fc34 49 4d 49 54 29 20 26 26 20 21 64 65 66 69 6e 65  IMIT) && !define
1fc35 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  d(SQLITE_OMIT_SU
1fc36 42 51 55 45 52 59 29 0a 20 20 20 20 2f 2a 20 54  BQUERY).    /* T
1fc37 68 65 20 73 70 65 63 69 61 6c 20 6f 70 65 72 61  he special opera
1fc38 74 6f 72 20 54 4b 5f 52 4f 57 20 6d 65 61 6e 73  tor TK_ROW means
1fc39 20 75 73 65 20 74 68 65 20 72 6f 77 69 64 20 66   use the rowid f
1fc3a 6f 72 20 74 68 65 20 66 69 72 73 74 0a 20 20 20  or the first.   
1fc3b 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68   ** column in th
1fc3c 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20  e FROM clause.  
1fc3d 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20  This is used by 
1fc3e 74 68 65 20 4c 49 4d 49 54 20 61 6e 64 20 4f 52  the LIMIT and OR
1fc3f 44 45 52 20 42 59 0a 20 20 20 20 2a 2a 20 63 6c  DER BY.    ** cl
1fc40 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67 20  ause processing 
1fc41 6f 6e 20 55 50 44 41 54 45 20 61 6e 64 20 44 45  on UPDATE and DE
1fc42 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 73 2e  LETE statements.
1fc43 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65  .    */.    case
1fc44 20 54 4b 5f 52 4f 57 3a 20 7b 0a 20 20 20 20 20   TK_ROW: {.     
1fc45 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69   SrcList *pSrcLi
1fc46 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69  st = pNC->pSrcLi
1fc47 73 74 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74  st;.      struct
1fc48 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
1fc49 49 74 65 6d 3b 0a 20 20 20 20 20 20 61 73 73 65  Item;.      asse
1fc4a 72 74 28 20 70 53 72 63 4c 69 73 74 20 26 26 20  rt( pSrcList && 
1fc4b 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d  pSrcList->nSrc==
1fc4c 31 20 29 3b 0a 20 20 20 20 20 20 70 49 74 65 6d  1 );.      pItem
1fc4d 20 3d 20 70 53 72 63 4c 69 73 74 2d 3e 61 3b 20   = pSrcList->a; 
1fc4e 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70  .      pExpr->op
1fc4f 20 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 3b 0a 20 20   = TK_COLUMN;.  
1fc50 20 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20      pExpr->pTab 
1fc51 3d 20 70 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20  = pItem->pTab;. 
1fc52 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62       pExpr->iTab
1fc53 6c 65 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72  le = pItem->iCur
1fc54 73 6f 72 3b 0a 20 20 20 20 20 20 70 45 78 70 72  sor;.      pExpr
1fc55 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a  ->iColumn = -1;.
1fc56 20 20 20 20 20 20 70 45 78 70 72 2d 3e 61 66 66        pExpr->aff
1fc57 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41  inity = SQLITE_A
1fc58 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20  FF_INTEGER;.    
1fc59 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23    break;.    }.#
1fc5a 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64  endif /* defined
1fc5b 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55  (SQLITE_ENABLE_U
1fc5c 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d  PDATE_DELETE_LIM
1fc5d 49 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  IT) && !defined(
1fc5e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
1fc5f 55 45 52 59 29 20 2a 2f 0a 0a 20 20 20 20 2f 2a  UERY) */..    /*
1fc60 20 41 20 6c 6f 6e 65 20 69 64 65 6e 74 69 66 69   A lone identifi
1fc61 65 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f  er is the name o
1fc62 66 20 61 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20  f a column..    
1fc63 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49  */.    case TK_I
1fc64 44 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72  D: {.      retur
1fc65 6e 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28 70 50 61  n lookupName(pPa
1fc66 72 73 65 2c 20 30 2c 20 30 2c 20 70 45 78 70 72  rse, 0, 0, pExpr
1fc67 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 70 4e 43 2c  ->u.zToken, pNC,
1fc68 20 70 45 78 70 72 29 3b 0a 20 20 20 20 7d 0a 20   pExpr);.    }. 
1fc69 20 0a 20 20 20 20 2f 2a 20 41 20 74 61 62 6c 65   .    /* A table
1fc6a 20 6e 61 6d 65 20 61 6e 64 20 63 6f 6c 75 6d 6e   name and column
1fc6b 20 6e 61 6d 65 3a 20 20 20 20 20 49 44 2e 49 44   name:     ID.ID
1fc6c 0a 20 20 20 20 2a 2a 20 4f 72 20 61 20 64 61 74  .    ** Or a dat
1fc6d 61 62 61 73 65 2c 20 74 61 62 6c 65 20 61 6e 64  abase, table and
1fc6e 20 63 6f 6c 75 6d 6e 3a 20 20 49 44 2e 49 44 2e   column:  ID.ID.
1fc6f 49 44 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61  ID.    */.    ca
1fc70 73 65 20 54 4b 5f 44 4f 54 3a 20 7b 0a 20 20 20  se TK_DOT: {.   
1fc71 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
1fc72 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 63 6f  Column;.      co
1fc73 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65  nst char *zTable
1fc74 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68  ;.      const ch
1fc75 61 72 20 2a 7a 44 62 3b 0a 20 20 20 20 20 20 45  ar *zDb;.      E
1fc76 78 70 72 20 2a 70 52 69 67 68 74 3b 0a 0a 20 20  xpr *pRight;..  
1fc77 20 20 20 20 2f 2a 20 69 66 28 20 70 53 72 63 4c      /* if( pSrcL
1fc78 69 73 74 3d 3d 30 20 29 20 62 72 65 61 6b 3b 20  ist==0 ) break; 
1fc79 2a 2f 0a 20 20 20 20 20 20 70 52 69 67 68 74 20  */.      pRight 
1fc7a 3d 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b  = pExpr->pRight;
1fc7b 0a 20 20 20 20 20 20 69 66 28 20 70 52 69 67 68  .      if( pRigh
1fc7c 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 7b 0a  t->op==TK_ID ){.
1fc7d 20 20 20 20 20 20 20 20 7a 44 62 20 3d 20 30 3b          zDb = 0;
1fc7e 0a 20 20 20 20 20 20 20 20 7a 54 61 62 6c 65 20  .        zTable 
1fc7f 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e  = pExpr->pLeft->
1fc80 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20  u.zToken;.      
1fc81 20 20 7a 43 6f 6c 75 6d 6e 20 3d 20 70 52 69 67    zColumn = pRig
1fc82 68 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20  ht->u.zToken;.  
1fc83 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1fc84 20 20 20 61 73 73 65 72 74 28 20 70 52 69 67 68     assert( pRigh
1fc85 74 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20 29 3b  t->op==TK_DOT );
1fc86 0a 20 20 20 20 20 20 20 20 7a 44 62 20 3d 20 70  .        zDb = p
1fc87 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 75 2e 7a  Expr->pLeft->u.z
1fc88 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 7a  Token;.        z
1fc89 54 61 62 6c 65 20 3d 20 70 52 69 67 68 74 2d 3e  Table = pRight->
1fc8a 70 4c 65 66 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b  pLeft->u.zToken;
1fc8b 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c 75 6d 6e  .        zColumn
1fc8c 20 3d 20 70 52 69 67 68 74 2d 3e 70 52 69 67 68   = pRight->pRigh
1fc8d 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20  t->u.zToken;.   
1fc8e 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72     }.      retur
1fc8f 6e 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28 70 50 61  n lookupName(pPa
1fc90 72 73 65 2c 20 7a 44 62 2c 20 7a 54 61 62 6c 65  rse, zDb, zTable
1fc91 2c 20 7a 43 6f 6c 75 6d 6e 2c 20 70 4e 43 2c 20  , zColumn, pNC, 
1fc92 70 45 78 70 72 29 3b 0a 20 20 20 20 7d 0a 0a 20  pExpr);.    }.. 
1fc93 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 66 75     /* Resolve fu
1fc94 6e 63 74 69 6f 6e 20 6e 61 6d 65 73 0a 20 20 20  nction names.   
1fc95 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f   */.    case TK_
1fc96 43 4f 4e 53 54 5f 46 55 4e 43 3a 0a 20 20 20 20  CONST_FUNC:.    
1fc97 63 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e  case TK_FUNCTION
1fc98 3a 20 7b 0a 20 20 20 20 20 20 45 78 70 72 4c 69  : {.      ExprLi
1fc99 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45 78 70  st *pList = pExp
1fc9a 72 2d 3e 78 2e 70 4c 69 73 74 3b 20 20 20 20 2f  r->x.pList;    /
1fc9b 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 6c  * The argument l
1fc9c 69 73 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  ist */.      int
1fc9d 20 6e 20 3d 20 70 4c 69 73 74 20 3f 20 70 4c 69   n = pList ? pLi
1fc9e 73 74 2d 3e 6e 45 78 70 72 20 3a 20 30 3b 20 20  st->nExpr : 0;  
1fc9f 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61    /* Number of a
1fca0 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20  rguments */.    
1fca1 20 20 69 6e 74 20 6e 6f 5f 73 75 63 68 5f 66 75    int no_such_fu
1fca2 6e 63 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a  nc = 0;       /*
1fca3 20 54 72 75 65 20 69 66 20 6e 6f 20 73 75 63 68   True if no such
1fca4 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 73 74 73   function exists
1fca5 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 77 72   */.      int wr
1fca6 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73 20 3d 20 30  ong_num_args = 0
1fca7 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66  ;     /* True if
1fca8 20 77 72 6f 6e 67 20 6e 75 6d 62 65 72 20 6f 66   wrong number of
1fca9 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20   arguments */.  
1fcaa 20 20 20 20 69 6e 74 20 69 73 5f 61 67 67 20 3d      int is_agg =
1fcab 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
1fcac 2f 2a 20 54 72 75 65 20 69 66 20 69 73 20 61 6e  /* True if is an
1fcad 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
1fcae 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  ion */.      int
1fcaf 20 61 75 74 68 3b 20 20 20 20 20 20 20 20 20 20   auth;          
1fcb0 20 20 20 20 20 20 20 20 20 2f 2a 20 41 75 74 68           /* Auth
1fcb1 6f 72 69 7a 61 74 69 6f 6e 20 74 6f 20 75 73 65  orization to use
1fcb2 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f   the function */
1fcb3 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 64 3b 20  .      int nId; 
1fcb4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fcb5 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1fcb6 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 66 75  characters in fu
1fcb7 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20  nction name */. 
1fcb8 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
1fcb9 2a 7a 49 64 3b 20 20 20 20 20 20 20 20 20 20 20  *zId;           
1fcba 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e   /* The function
1fcbb 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20   name. */.      
1fcbc 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 20 20  FuncDef *pDef;  
1fcbd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
1fcbe 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
1fcbf 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f   the function */
1fcc0 0a 20 20 20 20 20 20 75 38 20 65 6e 63 20 3d 20  .      u8 enc = 
1fcc1 45 4e 43 28 70 50 61 72 73 65 2d 3e 64 62 29 3b  ENC(pParse->db);
1fcc2 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
1fcc3 73 65 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 0a  se encoding */..
1fcc4 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1fcc5 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f  pExpr->op==TK_CO
1fcc6 4e 53 54 5f 46 55 4e 43 20 29 3b 0a 20 20 20 20  NST_FUNC );.    
1fcc7 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48    assert( !ExprH
1fcc8 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
1fcc9 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20  , EP_xIsSelect) 
1fcca 29 3b 0a 20 20 20 20 20 20 7a 49 64 20 3d 20 70  );.      zId = p
1fccb 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a  Expr->u.zToken;.
1fccc 20 20 20 20 20 20 6e 49 64 20 3d 20 73 71 6c 69        nId = sqli
1fccd 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 49 64 29  te3Strlen30(zId)
1fcce 3b 0a 20 20 20 20 20 20 70 44 65 66 20 3d 20 73  ;.      pDef = s
1fccf 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69  qlite3FindFuncti
1fcd0 6f 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a  on(pParse->db, z
1fcd1 49 64 2c 20 6e 49 64 2c 20 6e 2c 20 65 6e 63 2c  Id, nId, n, enc,
1fcd2 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70   0);.      if( p
1fcd3 44 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Def==0 ){.      
1fcd4 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33    pDef = sqlite3
1fcd5 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 70 50 61  FindFunction(pPa
1fcd6 72 73 65 2d 3e 64 62 2c 20 7a 49 64 2c 20 6e 49  rse->db, zId, nI
1fcd7 64 2c 20 2d 31 2c 20 65 6e 63 2c 20 30 29 3b 0a  d, -1, enc, 0);.
1fcd8 20 20 20 20 20 20 20 20 69 66 28 20 70 44 65 66          if( pDef
1fcd9 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
1fcda 20 6e 6f 5f 73 75 63 68 5f 66 75 6e 63 20 3d 20   no_such_func = 
1fcdb 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  1;.        }else
1fcdc 7b 0a 20 20 20 20 20 20 20 20 20 20 77 72 6f 6e  {.          wron
1fcdd 67 5f 6e 75 6d 5f 61 72 67 73 20 3d 20 31 3b 0a  g_num_args = 1;.
1fcde 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1fcdf 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69  }else{.        i
1fce0 73 5f 61 67 67 20 3d 20 70 44 65 66 2d 3e 78 46  s_agg = pDef->xF
1fce1 75 6e 63 3d 3d 30 3b 0a 20 20 20 20 20 20 7d 0a  unc==0;.      }.
1fce2 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1fce3 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f  MIT_AUTHORIZATIO
1fce4 4e 0a 20 20 20 20 20 20 69 66 28 20 70 44 65 66  N.      if( pDef
1fce5 20 29 7b 0a 20 20 20 20 20 20 20 20 61 75 74 68   ){.        auth
1fce6 20 3d 20 73 71 6c 69 74 65 33 41 75 74 68 43 68   = sqlite3AuthCh
1fce7 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49  eck(pParse, SQLI
1fce8 54 45 5f 46 55 4e 43 54 49 4f 4e 2c 20 30 2c 20  TE_FUNCTION, 0, 
1fce9 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b  pDef->zName, 0);
1fcea 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 75 74  .        if( aut
1fceb 68 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  h!=SQLITE_OK ){.
1fcec 20 20 20 20 20 20 20 20 20 20 69 66 28 20 61 75            if( au
1fced 74 68 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20  th==SQLITE_DENY 
1fcee 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  ){.            s
1fcef 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
1fcf0 50 61 72 73 65 2c 20 22 6e 6f 74 20 61 75 74 68  Parse, "not auth
1fcf1 6f 72 69 7a 65 64 20 74 6f 20 75 73 65 20 66 75  orized to use fu
1fcf2 6e 63 74 69 6f 6e 3a 20 25 73 22 2c 0a 20 20 20  nction: %s",.   
1fcf3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fcf4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fcf5 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 3b 0a 20   pDef->zName);. 
1fcf6 20 20 20 20 20 20 20 20 20 20 20 70 4e 43 2d 3e             pNC->
1fcf7 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20  nErr++;.        
1fcf8 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 45    }.          pE
1fcf9 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c  xpr->op = TK_NUL
1fcfa 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  L;.          ret
1fcfb 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20  urn WRC_Prune;. 
1fcfc 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
1fcfd 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69 66  .#endif.      if
1fcfe 28 20 69 73 5f 61 67 67 20 26 26 20 21 70 4e 43  ( is_agg && !pNC
1fcff 2d 3e 61 6c 6c 6f 77 41 67 67 20 29 7b 0a 20 20  ->allowAgg ){.  
1fd00 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
1fd01 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6d  orMsg(pParse, "m
1fd02 69 73 75 73 65 20 6f 66 20 61 67 67 72 65 67 61  isuse of aggrega
1fd03 74 65 20 66 75 6e 63 74 69 6f 6e 20 25 2e 2a 73  te function %.*s
1fd04 28 29 22 2c 20 6e 49 64 2c 7a 49 64 29 3b 0a 20  ()", nId,zId);. 
1fd05 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72         pNC->nErr
1fd06 2b 2b 3b 0a 20 20 20 20 20 20 20 20 69 73 5f 61  ++;.        is_a
1fd07 67 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65  gg = 0;.      }e
1fd08 6c 73 65 20 69 66 28 20 6e 6f 5f 73 75 63 68 5f  lse if( no_such_
1fd09 66 75 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20  func ){.        
1fd0a 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1fd0b 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68  pParse, "no such
1fd0c 20 66 75 6e 63 74 69 6f 6e 3a 20 25 2e 2a 73 22   function: %.*s"
1fd0d 2c 20 6e 49 64 2c 20 7a 49 64 29 3b 0a 20 20 20  , nId, zId);.   
1fd0e 20 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b       pNC->nErr++
1fd0f 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
1fd10 28 20 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73  ( wrong_num_args
1fd11 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
1fd12 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1fd13 73 65 2c 22 77 72 6f 6e 67 20 6e 75 6d 62 65 72  se,"wrong number
1fd14 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f   of arguments to
1fd15 20 66 75 6e 63 74 69 6f 6e 20 25 2e 2a 73 28 29   function %.*s()
1fd16 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
1fd17 6e 49 64 2c 20 7a 49 64 29 3b 0a 20 20 20 20 20  nId, zId);.     
1fd18 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a     pNC->nErr++;.
1fd19 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
1fd1a 28 20 69 73 5f 61 67 67 20 29 7b 0a 20 20 20 20  ( is_agg ){.    
1fd1b 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20      pExpr->op = 
1fd1c 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3b  TK_AGG_FUNCTION;
1fd1d 0a 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 68 61  .        pNC->ha
1fd1e 73 41 67 67 20 3d 20 31 3b 0a 20 20 20 20 20 20  sAgg = 1;.      
1fd1f 7d 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f 61  }.      if( is_a
1fd20 67 67 20 29 20 70 4e 43 2d 3e 61 6c 6c 6f 77 41  gg ) pNC->allowA
1fd21 67 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71  gg = 0;.      sq
1fd22 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73  lite3WalkExprLis
1fd23 74 28 70 57 61 6c 6b 65 72 2c 20 70 4c 69 73 74  t(pWalker, pList
1fd24 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f  );.      if( is_
1fd25 61 67 67 20 29 20 70 4e 43 2d 3e 61 6c 6c 6f 77  agg ) pNC->allow
1fd26 41 67 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 2f  Agg = 1;.      /
1fd27 2a 20 46 49 58 20 4d 45 3a 20 20 43 6f 6d 70 75  * FIX ME:  Compu
1fd28 74 65 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69  te pExpr->affini
1fd29 74 79 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20  ty based on the 
1fd2a 65 78 70 65 63 74 65 64 20 72 65 74 75 72 6e 0a  expected return.
1fd2b 20 20 20 20 20 20 2a 2a 20 74 79 70 65 20 6f 66        ** type of
1fd2c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 0a 20   the function . 
1fd2d 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 65       */.      re
1fd2e 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a  turn WRC_Prune;.
1fd2f 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51      }.#ifndef SQ
1fd30 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
1fd31 52 59 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53  RY.    case TK_S
1fd32 45 4c 45 43 54 3a 0a 20 20 20 20 63 61 73 65 20  ELECT:.    case 
1fd33 54 4b 5f 45 58 49 53 54 53 3a 20 20 74 65 73 74  TK_EXISTS:  test
1fd34 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d  case( pExpr->op=
1fd35 3d 54 4b 5f 45 58 49 53 54 53 20 29 3b 0a 23 65  =TK_EXISTS );.#e
1fd36 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 54 4b  ndif.    case TK
1fd37 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 74 65 73  _IN: {.      tes
1fd38 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70  tcase( pExpr->op
1fd39 3d 3d 54 4b 5f 49 4e 20 29 3b 0a 20 20 20 20 20  ==TK_IN );.     
1fd3a 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70   if( ExprHasProp
1fd3b 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78  erty(pExpr, EP_x
1fd3c 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20  IsSelect) ){.   
1fd3d 20 20 20 20 20 69 6e 74 20 6e 52 65 66 20 3d 20       int nRef = 
1fd3e 70 4e 43 2d 3e 6e 52 65 66 3b 0a 23 69 66 6e 64  pNC->nRef;.#ifnd
1fd3f 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ef SQLITE_OMIT_C
1fd40 48 45 43 4b 0a 20 20 20 20 20 20 20 20 69 66 28  HECK.        if(
1fd41 20 70 4e 43 2d 3e 69 73 43 68 65 63 6b 20 29 7b   pNC->isCheck ){
1fd42 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
1fd43 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
1fd44 65 2c 22 73 75 62 71 75 65 72 69 65 73 20 70 72  e,"subqueries pr
1fd45 6f 68 69 62 69 74 65 64 20 69 6e 20 43 48 45 43  ohibited in CHEC
1fd46 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 22 29 3b  K constraints");
1fd47 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  .        }.#endi
1fd48 66 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  f.        sqlite
1fd49 33 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c  3WalkSelect(pWal
1fd4a 6b 65 72 2c 20 70 45 78 70 72 2d 3e 78 2e 70 53  ker, pExpr->x.pS
1fd4b 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 20 20  elect);.        
1fd4c 61 73 73 65 72 74 28 20 70 4e 43 2d 3e 6e 52 65  assert( pNC->nRe
1fd4d 66 3e 3d 6e 52 65 66 20 29 3b 0a 20 20 20 20 20  f>=nRef );.     
1fd4e 20 20 20 69 66 28 20 6e 52 65 66 21 3d 70 4e 43     if( nRef!=pNC
1fd4f 2d 3e 6e 52 65 66 20 29 7b 0a 20 20 20 20 20 20  ->nRef ){.      
1fd50 20 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65      ExprSetPrope
1fd51 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 56 61  rty(pExpr, EP_Va
1fd52 72 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20  rSelect);.      
1fd53 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
1fd54 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23    break;.    }.#
1fd55 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1fd56 49 54 5f 43 48 45 43 4b 0a 20 20 20 20 63 61 73  IT_CHECK.    cas
1fd57 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a 20 7b  e TK_VARIABLE: {
1fd58 0a 20 20 20 20 20 20 69 66 28 20 70 4e 43 2d 3e  .      if( pNC->
1fd59 69 73 43 68 65 63 6b 20 29 7b 0a 20 20 20 20 20  isCheck ){.     
1fd5a 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1fd5b 73 67 28 70 50 61 72 73 65 2c 22 70 61 72 61 6d  sg(pParse,"param
1fd5c 65 74 65 72 73 20 70 72 6f 68 69 62 69 74 65 64  eters prohibited
1fd5d 20 69 6e 20 43 48 45 43 4b 20 63 6f 6e 73 74 72   in CHECK constr
1fd5e 61 69 6e 74 73 22 29 3b 0a 20 20 20 20 20 20 7d  aints");.      }
1fd5f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1fd60 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20    }.#endif.  }. 
1fd61 20 72 65 74 75 72 6e 20 28 70 50 61 72 73 65 2d   return (pParse-
1fd62 3e 6e 45 72 72 20 7c 7c 20 70 50 61 72 73 65 2d  >nErr || pParse-
1fd63 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
1fd64 64 29 20 3f 20 57 52 43 5f 41 62 6f 72 74 20 3a  d) ? WRC_Abort :
1fd65 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d   WRC_Continue;.}
1fd66 0a 0a 2f 2a 0a 2a 2a 20 70 45 4c 69 73 74 20 69  ../*.** pEList i
1fd67 73 20 61 20 6c 69 73 74 20 6f 66 20 65 78 70 72  s a list of expr
1fd68 65 73 73 69 6f 6e 73 20 77 68 69 63 68 20 61 72  essions which ar
1fd69 65 20 72 65 61 6c 6c 79 20 74 68 65 20 72 65 73  e really the res
1fd6a 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65 0a 2a  ult set of the.*
1fd6b 2a 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65  * a SELECT state
1fd6c 6d 65 6e 74 2e 20 20 70 45 20 69 73 20 61 20 74  ment.  pE is a t
1fd6d 65 72 6d 20 69 6e 20 61 6e 20 4f 52 44 45 52 20  erm in an ORDER 
1fd6e 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63  BY or GROUP BY c
1fd6f 6c 61 75 73 65 2e 0a 2a 2a 20 54 68 69 73 20 72  lause..** This r
1fd70 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 74 6f  outine checks to
1fd71 20 73 65 65 20 69 66 20 70 45 20 69 73 20 61 20   see if pE is a 
1fd72 73 69 6d 70 6c 65 20 69 64 65 6e 74 69 66 69 65  simple identifie
1fd73 72 20 77 68 69 63 68 20 63 6f 72 72 65 73 70 6f  r which correspo
1fd74 6e 64 73 0a 2a 2a 20 74 6f 20 74 68 65 20 41 53  nds.** to the AS
1fd75 2d 6e 61 6d 65 20 6f 66 20 6f 6e 65 20 6f 66 20  -name of one of 
1fd76 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65  the terms of the
1fd77 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74   expression list
1fd78 2e 20 20 49 66 20 69 74 20 69 73 2c 0a 2a 2a 20  .  If it is,.** 
1fd79 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  this routine ret
1fd7a 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 62  urn an integer b
1fd7b 65 74 77 65 65 6e 20 31 20 61 6e 64 20 4e 20 77  etween 1 and N w
1fd7c 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 6e 75  here N is the nu
1fd7d 6d 62 65 72 20 6f 66 0a 2a 2a 20 65 6c 65 6d 65  mber of.** eleme
1fd7e 6e 74 73 20 69 6e 20 70 45 4c 69 73 74 2c 20 63  nts in pEList, c
1fd7f 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20  orresponding to 
1fd80 74 68 65 20 6d 61 74 63 68 69 6e 67 20 65 6e 74  the matching ent
1fd81 72 79 2e 20 20 49 66 20 74 68 65 72 65 20 69 73  ry.  If there is
1fd82 0a 2a 2a 20 6e 6f 20 6d 61 74 63 68 2c 20 6f 72  .** no match, or
1fd83 20 69 66 20 70 45 20 69 73 20 6e 6f 74 20 61 20   if pE is not a 
1fd84 73 69 6d 70 6c 65 20 69 64 65 6e 74 69 66 69 65  simple identifie
1fd85 72 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75  r, then this rou
1fd86 74 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 20 30  tine.** return 0
1fd87 2e 0a 2a 2a 0a 2a 2a 20 70 45 4c 69 73 74 20 68  ..**.** pEList h
1fd88 61 73 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64  as been resolved
1fd89 2e 20 20 70 45 20 68 61 73 20 6e 6f 74 2e 0a 2a  .  pE has not..*
1fd8a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73  /.static int res
1fd8b 6f 6c 76 65 41 73 4e 61 6d 65 28 0a 20 20 50 61  olveAsName(.  Pa
1fd8c 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
1fd8d 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
1fd8e 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 6d 65  ext for error me
1fd8f 73 73 61 67 65 73 20 2a 2f 0a 20 20 45 78 70 72  ssages */.  Expr
1fd90 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 2f  List *pEList,  /
1fd91 2a 20 4c 69 73 74 20 6f 66 20 65 78 70 72 65 73  * List of expres
1fd92 73 69 6f 6e 73 20 74 6f 20 73 63 61 6e 20 2a 2f  sions to scan */
1fd93 0a 20 20 45 78 70 72 20 2a 70 45 20 20 20 20 20  .  Expr *pE     
1fd94 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73        /* Express
1fd95 69 6f 6e 20 77 65 20 61 72 65 20 74 72 79 69 6e  ion we are tryin
1fd96 67 20 74 6f 20 6d 61 74 63 68 20 2a 2f 0a 29 7b  g to match */.){
1fd97 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
1fd98 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
1fd99 75 6e 74 65 72 20 2a 2f 0a 0a 20 20 55 4e 55 53  unter */..  UNUS
1fd9a 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 50 61  ED_PARAMETER(pPa
1fd9b 72 73 65 29 3b 0a 0a 20 20 69 66 28 20 70 45 2d  rse);..  if( pE-
1fd9c 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 7b 0a 20 20  >op==TK_ID ){.  
1fd9d 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20 3d 20 70    char *zCol = p
1fd9e 45 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20  E->u.zToken;.   
1fd9f 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c 69   for(i=0; i<pELi
1fda0 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b  st->nExpr; i++){
1fda1 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 41 73  .      char *zAs
1fda2 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e   = pEList->a[i].
1fda3 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 69 66 28  zName;.      if(
1fda4 20 7a 41 73 21 3d 30 20 26 26 20 73 71 6c 69 74   zAs!=0 && sqlit
1fda5 65 33 53 74 72 49 43 6d 70 28 7a 41 73 2c 20 7a  e3StrICmp(zAs, z
1fda6 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Col)==0 ){.     
1fda7 20 20 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20     return i+1;. 
1fda8 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
1fda9 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
1fdaa 2f 2a 0a 2a 2a 20 70 45 20 69 73 20 61 20 70 6f  /*.** pE is a po
1fdab 69 6e 74 65 72 20 74 6f 20 61 6e 20 65 78 70 72  inter to an expr
1fdac 65 73 73 69 6f 6e 20 77 68 69 63 68 20 69 73 20  ession which is 
1fdad 61 20 73 69 6e 67 6c 65 20 74 65 72 6d 20 69 6e  a single term in
1fdae 20 74 68 65 0a 2a 2a 20 4f 52 44 45 52 20 42 59   the.** ORDER BY
1fdaf 20 6f 66 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53   of a compound S
1fdb0 45 4c 45 43 54 2e 20 20 54 68 65 20 65 78 70 72  ELECT.  The expr
1fdb1 65 73 73 69 6f 6e 20 68 61 73 20 6e 6f 74 20 62  ession has not b
1fdb2 65 65 6e 0a 2a 2a 20 6e 61 6d 65 20 72 65 73 6f  een.** name reso
1fdb3 6c 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 74  lved..**.** At t
1fdb4 68 65 20 70 6f 69 6e 74 20 74 68 69 73 20 72 6f  he point this ro
1fdb5 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c  utine is called,
1fdb6 20 77 65 20 61 6c 72 65 61 64 79 20 6b 6e 6f 77   we already know
1fdb7 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 4f 52 44   that the.** ORD
1fdb8 45 52 20 42 59 20 74 65 72 6d 20 69 73 20 6e 6f  ER BY term is no
1fdb9 74 20 61 6e 20 69 6e 74 65 67 65 72 20 69 6e 64  t an integer ind
1fdba 65 78 20 69 6e 74 6f 20 74 68 65 20 72 65 73 75  ex into the resu
1fdbb 6c 74 20 73 65 74 2e 20 20 54 68 61 74 0a 2a 2a  lt set.  That.**
1fdbc 20 63 61 73 65 20 69 73 20 68 61 6e 64 6c 65 64   case is handled
1fdbd 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20   by the calling 
1fdbe 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 41  routine..**.** A
1fdbf 74 74 65 6d 70 74 20 74 6f 20 6d 61 74 63 68 20  ttempt to match 
1fdc0 70 45 20 61 67 61 69 6e 73 74 20 72 65 73 75 6c  pE against resul
1fdc1 74 20 73 65 74 20 63 6f 6c 75 6d 6e 73 20 69 6e  t set columns in
1fdc2 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 0a 2a   the left-most.*
1fdc3 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  * SELECT stateme
1fdc4 6e 74 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20  nt.  Return the 
1fdc5 69 6e 64 65 78 20 69 20 6f 66 20 74 68 65 20 6d  index i of the m
1fdc6 61 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e 2c 0a  atching column,.
1fdc7 2a 2a 20 61 73 20 61 6e 20 69 6e 64 69 63 61 74  ** as an indicat
1fdc8 69 6f 6e 20 74 6f 20 74 68 65 20 63 61 6c 6c 65  ion to the calle
1fdc9 72 20 74 68 61 74 20 69 74 20 73 68 6f 75 6c 64  r that it should
1fdca 20 73 6f 72 74 20 62 79 20 74 68 65 20 69 2d 74   sort by the i-t
1fdcb 68 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 54 68 65  h column..** The
1fdcc 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d   left-most colum
1fdcd 6e 20 69 73 20 31 2e 20 20 49 6e 20 6f 74 68 65  n is 1.  In othe
1fdce 72 20 77 6f 72 64 73 2c 20 74 68 65 20 76 61 6c  r words, the val
1fdcf 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 74  ue returned is t
1fdd0 68 65 0a 2a 2a 20 73 61 6d 65 20 69 6e 74 65 67  he.** same integ
1fdd1 65 72 20 76 61 6c 75 65 20 74 68 61 74 20 77 6f  er value that wo
1fdd2 75 6c 64 20 62 65 20 75 73 65 64 20 69 6e 20 74  uld be used in t
1fdd3 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
1fdd4 20 74 6f 20 69 6e 64 69 63 61 74 65 0a 2a 2a 20   to indicate.** 
1fdd5 74 68 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a  the column..**.*
1fdd6 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f  * If there is no
1fdd7 20 6d 61 74 63 68 2c 20 72 65 74 75 72 6e 20 30   match, return 0
1fdd8 2e 20 20 52 65 74 75 72 6e 20 2d 31 20 69 66 20  .  Return -1 if 
1fdd9 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e  an error occurs.
1fdda 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
1fddb 65 73 6f 6c 76 65 4f 72 64 65 72 42 79 54 65 72  esolveOrderByTer
1fddc 6d 54 6f 45 78 70 72 4c 69 73 74 28 0a 20 20 50  mToExprList(.  P
1fddd 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
1fdde 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
1fddf 74 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 6d  text for error m
1fde0 65 73 73 61 67 65 73 20 2a 2f 0a 20 20 53 65 6c  essages */.  Sel
1fde1 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20  ect *pSelect,   
1fde2 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74  /* The SELECT st
1fde3 61 74 65 6d 65 6e 74 20 77 69 74 68 20 74 68 65  atement with the
1fde4 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
1fde5 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 20 20   */.  Expr *pE  
1fde6 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1fde7 73 70 65 63 69 66 69 63 20 4f 52 44 45 52 20 42  specific ORDER B
1fde8 59 20 74 65 72 6d 20 2a 2f 0a 29 7b 0a 20 20 69  Y term */.){.  i
1fde9 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
1fdea 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
1fdeb 72 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  r */.  ExprList 
1fdec 2a 70 45 4c 69 73 74 3b 20 20 2f 2a 20 54 68 65  *pEList;  /* The
1fded 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20   columns of the 
1fdee 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20  result set */.  
1fdef 4e 61 6d 65 43 6f 6e 74 65 78 74 20 6e 63 3b 20  NameContext nc; 
1fdf0 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65     /* Name conte
1fdf1 78 74 20 66 6f 72 20 72 65 73 6f 6c 76 69 6e 67  xt for resolving
1fdf2 20 70 45 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74   pE */..  assert
1fdf3 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 49  ( sqlite3ExprIsI
1fdf4 6e 74 65 67 65 72 28 70 45 2c 20 26 69 29 3d 3d  nteger(pE, &i)==
1fdf5 30 20 29 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20  0 );.  pEList = 
1fdf6 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b  pSelect->pEList;
1fdf7 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 61  ..  /* Resolve a
1fdf8 6c 6c 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20  ll names in the 
1fdf9 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 65 78  ORDER BY term ex
1fdfa 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2f 0a 20 20  pression.  */.  
1fdfb 6d 65 6d 73 65 74 28 26 6e 63 2c 20 30 2c 20 73  memset(&nc, 0, s
1fdfc 69 7a 65 6f 66 28 6e 63 29 29 3b 0a 20 20 6e 63  izeof(nc));.  nc
1fdfd 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65  .pParse = pParse
1fdfe 3b 0a 20 20 6e 63 2e 70 53 72 63 4c 69 73 74 20  ;.  nc.pSrcList 
1fdff 3d 20 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 3b  = pSelect->pSrc;
1fe00 0a 20 20 6e 63 2e 70 45 4c 69 73 74 20 3d 20 70  .  nc.pEList = p
1fe01 45 4c 69 73 74 3b 0a 20 20 6e 63 2e 61 6c 6c 6f  EList;.  nc.allo
1fe02 77 41 67 67 20 3d 20 31 3b 0a 20 20 6e 63 2e 6e  wAgg = 1;.  nc.n
1fe03 45 72 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 73  Err = 0;.  if( s
1fe04 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70  qlite3ResolveExp
1fe05 72 4e 61 6d 65 73 28 26 6e 63 2c 20 70 45 29 20  rNames(&nc, pE) 
1fe06 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
1fe07 72 6f 72 43 6c 65 61 72 28 70 50 61 72 73 65 29  rorClear(pParse)
1fe08 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  ;.    return 0;.
1fe09 20 20 7d 0a 0a 20 20 2f 2a 20 54 72 79 20 74 6f    }..  /* Try to
1fe0a 20 6d 61 74 63 68 20 74 68 65 20 4f 52 44 45 52   match the ORDER
1fe0b 20 42 59 20 65 78 70 72 65 73 73 69 6f 6e 20 61   BY expression a
1fe0c 67 61 69 6e 73 74 20 61 6e 20 65 78 70 72 65 73  gainst an expres
1fe0d 73 69 6f 6e 0a 20 20 2a 2a 20 69 6e 20 74 68 65  sion.  ** in the
1fe0e 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 52 65   result set.  Re
1fe0f 74 75 72 6e 20 61 6e 20 31 2d 62 61 73 65 64 20  turn an 1-based 
1fe10 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6d 61 74  index of the mat
1fe11 63 68 69 6e 67 0a 20 20 2a 2a 20 72 65 73 75 6c  ching.  ** resul
1fe12 74 2d 73 65 74 20 65 6e 74 72 79 2e 0a 20 20 2a  t-set entry..  *
1fe13 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  /.  for(i=0; i<p
1fe14 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b  EList->nExpr; i+
1fe15 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  +){.    if( sqli
1fe16 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 70  te3ExprCompare(p
1fe17 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70  EList->a[i].pExp
1fe18 72 2c 20 70 45 29 20 29 7b 0a 20 20 20 20 20 20  r, pE) ){.      
1fe19 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20 20 20  return i+1;.    
1fe1a 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e  }.  }..  /* If n
1fe1b 6f 20 6d 61 74 63 68 2c 20 72 65 74 75 72 6e 20  o match, return 
1fe1c 30 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30  0. */.  return 0
1fe1d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  ;.}../*.** Gener
1fe1e 61 74 65 20 61 6e 20 4f 52 44 45 52 20 42 59 20  ate an ORDER BY 
1fe1f 6f 72 20 47 52 4f 55 50 20 42 59 20 74 65 72 6d  or GROUP BY term
1fe20 20 6f 75 74 2d 6f 66 2d 72 61 6e 67 65 20 65 72   out-of-range er
1fe21 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ror..*/.static v
1fe22 6f 69 64 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66  oid resolveOutOf
1fe23 52 61 6e 67 65 45 72 72 6f 72 28 0a 20 20 50 61  RangeError(.  Pa
1fe24 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
1fe25 20 20 20 20 20 2f 2a 20 54 68 65 20 65 72 72 6f       /* The erro
1fe26 72 20 63 6f 6e 74 65 78 74 20 69 6e 74 6f 20 77  r context into w
1fe27 68 69 63 68 20 74 6f 20 77 72 69 74 65 20 74 68  hich to write th
1fe28 65 20 65 72 72 6f 72 20 2a 2f 0a 20 20 63 6f 6e  e error */.  con
1fe29 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65 2c 20  st char *zType, 
1fe2a 20 20 20 20 2f 2a 20 22 4f 52 44 45 52 22 20 6f      /* "ORDER" o
1fe2b 72 20 22 47 52 4f 55 50 22 20 2a 2f 0a 20 20 69  r "GROUP" */.  i
1fe2c 6e 74 20 69 2c 20 20 20 20 20 20 20 20 20 20 20  nt i,           
1fe2d 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64        /* The ind
1fe2e 65 78 20 28 31 2d 62 61 73 65 64 29 20 6f 66 20  ex (1-based) of 
1fe2f 74 68 65 20 74 65 72 6d 20 6f 75 74 20 6f 66 20  the term out of 
1fe30 72 61 6e 67 65 20 2a 2f 0a 20 20 69 6e 74 20 6d  range */.  int m
1fe31 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x               
1fe32 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 70 65 72    /* Largest per
1fe33 6d 69 73 73 69 62 6c 65 20 76 61 6c 75 65 20 6f  missible value o
1fe34 66 20 69 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69  f i */.){.  sqli
1fe35 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1fe36 73 65 2c 20 0a 20 20 20 20 22 25 72 20 25 73 20  se, .    "%r %s 
1fe37 42 59 20 74 65 72 6d 20 6f 75 74 20 6f 66 20 72  BY term out of r
1fe38 61 6e 67 65 20 2d 20 73 68 6f 75 6c 64 20 62 65  ange - should be
1fe39 20 22 0a 20 20 20 20 22 62 65 74 77 65 65 6e 20   ".    "between 
1fe3a 31 20 61 6e 64 20 25 64 22 2c 20 69 2c 20 7a 54  1 and %d", i, zT
1fe3b 79 70 65 2c 20 6d 78 29 3b 0a 7d 0a 0a 2f 2a 0a  ype, mx);.}../*.
1fe3c 2a 2a 20 41 6e 61 6c 79 7a 65 20 74 68 65 20 4f  ** Analyze the O
1fe3d 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 69  RDER BY clause i
1fe3e 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c  n a compound SEL
1fe3f 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  ECT statement.  
1fe40 20 4d 6f 64 69 66 79 0a 2a 2a 20 65 61 63 68 20   Modify.** each 
1fe41 74 65 72 6d 20 6f 66 20 74 68 65 20 4f 52 44 45  term of the ORDE
1fe42 52 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 61  R BY clause is a
1fe43 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65 67 65   constant intege
1fe44 72 20 62 65 74 77 65 65 6e 20 31 0a 2a 2a 20 61  r between 1.** a
1fe45 6e 64 20 4e 20 77 68 65 72 65 20 4e 20 69 73 20  nd N where N is 
1fe46 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f  the number of co
1fe47 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 63 6f 6d  lumns in the com
1fe48 70 6f 75 6e 64 20 53 45 4c 45 43 54 2e 0a 2a 2a  pound SELECT..**
1fe49 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 74 65 72  .** ORDER BY ter
1fe4a 6d 73 20 74 68 61 74 20 61 72 65 20 61 6c 72 65  ms that are alre
1fe4b 61 64 79 20 61 6e 20 69 6e 74 65 67 65 72 20 62  ady an integer b
1fe4c 65 74 77 65 65 6e 20 31 20 61 6e 64 20 4e 20 61  etween 1 and N a
1fe4d 72 65 0a 2a 2a 20 75 6e 6d 6f 64 69 66 69 65 64  re.** unmodified
1fe4e 2e 20 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d  .  ORDER BY term
1fe4f 73 20 74 68 61 74 20 61 72 65 20 69 6e 74 65 67  s that are integ
1fe50 65 72 73 20 6f 75 74 73 69 64 65 20 74 68 65 20  ers outside the 
1fe51 72 61 6e 67 65 20 6f 66 0a 2a 2a 20 31 20 74 68  range of.** 1 th
1fe52 72 6f 75 67 68 20 4e 20 67 65 6e 65 72 61 74 65  rough N generate
1fe53 20 61 6e 20 65 72 72 6f 72 2e 20 20 4f 52 44 45   an error.  ORDE
1fe54 52 20 42 59 20 74 65 72 6d 73 20 74 68 61 74 20  R BY terms that 
1fe55 61 72 65 20 65 78 70 72 65 73 73 69 6f 6e 73 0a  are expressions.
1fe56 2a 2a 20 61 72 65 20 6d 61 74 63 68 65 64 20 61  ** are matched a
1fe57 67 61 69 6e 73 74 20 72 65 73 75 6c 74 20 73 65  gainst result se
1fe58 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 66  t expressions of
1fe59 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54   compound SELECT
1fe5a 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20 77 69  .** beginning wi
1fe5b 74 68 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74  th the left-most
1fe5c 20 53 45 4c 45 43 54 20 61 6e 64 20 77 6f 72 6b   SELECT and work
1fe5d 69 6e 67 20 74 6f 77 61 72 64 20 74 68 65 20 72  ing toward the r
1fe5e 69 67 68 74 2e 0a 2a 2a 20 41 74 20 74 68 65 20  ight..** At the 
1fe5f 66 69 72 73 74 20 6d 61 74 63 68 2c 20 74 68 65  first match, the
1fe60 20 4f 52 44 45 52 20 42 59 20 65 78 70 72 65 73   ORDER BY expres
1fe61 73 69 6f 6e 20 69 73 20 74 72 61 6e 73 66 6f 72  sion is transfor
1fe62 6d 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20  med into.** the 
1fe63 69 6e 74 65 67 65 72 20 63 6f 6c 75 6d 6e 20 6e  integer column n
1fe64 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74  umber..**.** Ret
1fe65 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
1fe66 66 20 65 72 72 6f 72 73 20 73 65 65 6e 2e 0a 2a  f errors seen..*
1fe67 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73  /.static int res
1fe68 6f 6c 76 65 43 6f 6d 70 6f 75 6e 64 4f 72 64 65  olveCompoundOrde
1fe69 72 42 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50  rBy(.  Parse *pP
1fe6a 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  arse,        /* 
1fe6b 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 2e  Parsing context.
1fe6c 20 20 4c 65 61 76 65 20 65 72 72 6f 72 20 6d 65    Leave error me
1fe6d 73 73 61 67 65 73 20 68 65 72 65 20 2a 2f 0a 20  ssages here */. 
1fe6e 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
1fe6f 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45         /* The SE
1fe70 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 63  LECT statement c
1fe71 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 4f 52  ontaining the OR
1fe72 44 45 52 20 42 59 20 2a 2f 0a 29 7b 0a 20 20 69  DER BY */.){.  i
1fe73 6e 74 20 69 3b 0a 20 20 45 78 70 72 4c 69 73 74  nt i;.  ExprList
1fe74 20 2a 70 4f 72 64 65 72 42 79 3b 0a 20 20 45 78   *pOrderBy;.  Ex
1fe75 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a  prList *pEList;.
1fe76 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20    sqlite3 *db;. 
1fe77 20 69 6e 74 20 6d 6f 72 65 54 6f 44 6f 20 3d 20   int moreToDo = 
1fe78 31 3b 0a 0a 20 20 70 4f 72 64 65 72 42 79 20 3d  1;..  pOrderBy =
1fe79 20 70 53 65 6c 65 63 74 2d 3e 70 4f 72 64 65 72   pSelect->pOrder
1fe7a 42 79 3b 0a 20 20 69 66 28 20 70 4f 72 64 65 72  By;.  if( pOrder
1fe7b 42 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  By==0 ) return 0
1fe7c 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d  ;.  db = pParse-
1fe7d 3e 64 62 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f  >db;.#if SQLITE_
1fe7e 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 28  MAX_COLUMN.  if(
1fe7f 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72   pOrderBy->nExpr
1fe80 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  >db->aLimit[SQLI
1fe81 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d  TE_LIMIT_COLUMN]
1fe82 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
1fe83 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
1fe84 22 74 6f 6f 20 6d 61 6e 79 20 74 65 72 6d 73 20  "too many terms 
1fe85 69 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  in ORDER BY clau
1fe86 73 65 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  se");.    return
1fe87 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20   1;.  }.#endif. 
1fe88 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 72 64   for(i=0; i<pOrd
1fe89 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b  erBy->nExpr; i++
1fe8a 29 7b 0a 20 20 20 20 70 4f 72 64 65 72 42 79 2d  ){.    pOrderBy-
1fe8b 3e 61 5b 69 5d 2e 64 6f 6e 65 20 3d 20 30 3b 0a  >a[i].done = 0;.
1fe8c 20 20 7d 0a 20 20 70 53 65 6c 65 63 74 2d 3e 70    }.  pSelect->p
1fe8d 4e 65 78 74 20 3d 20 30 3b 0a 20 20 77 68 69 6c  Next = 0;.  whil
1fe8e 65 28 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69  e( pSelect->pPri
1fe8f 6f 72 20 29 7b 0a 20 20 20 20 70 53 65 6c 65 63  or ){.    pSelec
1fe90 74 2d 3e 70 50 72 69 6f 72 2d 3e 70 4e 65 78 74  t->pPrior->pNext
1fe91 20 3d 20 70 53 65 6c 65 63 74 3b 0a 20 20 20 20   = pSelect;.    
1fe92 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63  pSelect = pSelec
1fe93 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20  t->pPrior;.  }. 
1fe94 20 77 68 69 6c 65 28 20 70 53 65 6c 65 63 74 20   while( pSelect 
1fe95 26 26 20 6d 6f 72 65 54 6f 44 6f 20 29 7b 0a 20  && moreToDo ){. 
1fe96 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69     struct ExprLi
1fe97 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a  st_item *pItem;.
1fe98 20 20 20 20 6d 6f 72 65 54 6f 44 6f 20 3d 20 30      moreToDo = 0
1fe99 3b 0a 20 20 20 20 70 45 4c 69 73 74 20 3d 20 70  ;.    pEList = p
1fe9a 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a  Select->pEList;.
1fe9b 20 20 20 20 61 73 73 65 72 74 28 20 70 45 4c 69      assert( pELi
1fe9c 73 74 21 3d 30 20 29 3b 0a 20 20 20 20 66 6f 72  st!=0 );.    for
1fe9d 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4f 72 64  (i=0, pItem=pOrd
1fe9e 65 72 42 79 2d 3e 61 3b 20 69 3c 70 4f 72 64 65  erBy->a; i<pOrde
1fe9f 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c  rBy->nExpr; i++,
1fea0 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20   pItem++){.     
1fea1 20 69 6e 74 20 69 43 6f 6c 20 3d 20 2d 31 3b 0a   int iCol = -1;.
1fea2 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 2c 20        Expr *pE, 
1fea3 2a 70 44 75 70 3b 0a 20 20 20 20 20 20 69 66 28  *pDup;.      if(
1fea4 20 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 29 20 63   pItem->done ) c
1fea5 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 70  ontinue;.      p
1fea6 45 20 3d 20 70 49 74 65 6d 2d 3e 70 45 78 70 72  E = pItem->pExpr
1fea7 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  ;.      if( sqli
1fea8 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72  te3ExprIsInteger
1fea9 28 70 45 2c 20 26 69 43 6f 6c 29 20 29 7b 0a 20  (pE, &iCol) ){. 
1feaa 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c         if( iCol<
1feab 3d 30 20 7c 7c 20 69 43 6f 6c 3e 70 45 4c 69 73  =0 || iCol>pELis
1feac 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20  t->nExpr ){.    
1fead 20 20 20 20 20 20 72 65 73 6f 6c 76 65 4f 75 74        resolveOut
1feae 4f 66 52 61 6e 67 65 45 72 72 6f 72 28 70 50 61  OfRangeError(pPa
1feaf 72 73 65 2c 20 22 4f 52 44 45 52 22 2c 20 69 2b  rse, "ORDER", i+
1feb0 31 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  1, pEList->nExpr
1feb1 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  );.          ret
1feb2 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  urn 1;.        }
1feb3 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1feb4 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 72 65 73        iCol = res
1feb5 6f 6c 76 65 41 73 4e 61 6d 65 28 70 50 61 72 73  olveAsName(pPars
1feb6 65 2c 20 70 45 4c 69 73 74 2c 20 70 45 29 3b 0a  e, pEList, pE);.
1feb7 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c          if( iCol
1feb8 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
1feb9 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45   pDup = sqlite3E
1feba 78 70 72 44 75 70 28 64 62 2c 20 70 45 2c 20 30  xprDup(db, pE, 0
1febb 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
1febc 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
1febd 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ed ){.          
1febe 20 20 61 73 73 65 72 74 28 70 44 75 70 29 3b 0a    assert(pDup);.
1febf 20 20 20 20 20 20 20 20 20 20 20 20 69 43 6f 6c              iCol
1fec0 20 3d 20 72 65 73 6f 6c 76 65 4f 72 64 65 72 42   = resolveOrderB
1fec1 79 54 65 72 6d 54 6f 45 78 70 72 4c 69 73 74 28  yTermToExprList(
1fec2 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2c  pParse, pSelect,
1fec3 20 70 44 75 70 29 3b 0a 20 20 20 20 20 20 20 20   pDup);.        
1fec4 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73 71    }.          sq
1fec5 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
1fec6 64 62 2c 20 70 44 75 70 29 3b 0a 20 20 20 20 20  db, pDup);.     
1fec7 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
1fec8 20 20 20 69 66 28 20 69 43 6f 6c 3e 30 20 29 7b     if( iCol>0 ){
1fec9 0a 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71  .        CollSeq
1feca 20 2a 70 43 6f 6c 6c 20 3d 20 70 45 2d 3e 70 43   *pColl = pE->pC
1fecb 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  oll;.        int
1fecc 20 66 6c 61 67 73 20 3d 20 70 45 2d 3e 66 6c 61   flags = pE->fla
1fecd 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61  gs & EP_ExpColla
1fece 74 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  te;.        sqli
1fecf 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
1fed0 2c 20 70 45 29 3b 0a 20 20 20 20 20 20 20 20 70  , pE);.        p
1fed1 49 74 65 6d 2d 3e 70 45 78 70 72 20 3d 20 70 45  Item->pExpr = pE
1fed2 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 64   = sqlite3Expr(d
1fed3 62 2c 20 54 4b 5f 49 4e 54 45 47 45 52 2c 20 30  b, TK_INTEGER, 0
1fed4 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
1fed5 45 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b  E==0 ) return 1;
1fed6 0a 20 20 20 20 20 20 20 20 70 45 2d 3e 70 43 6f  .        pE->pCo
1fed7 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20  ll = pColl;.    
1fed8 20 20 20 20 70 45 2d 3e 66 6c 61 67 73 20 7c 3d      pE->flags |=
1fed9 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 7c 20 66   EP_IntValue | f
1feda 6c 61 67 73 3b 0a 20 20 20 20 20 20 20 20 70 45  lags;.        pE
1fedb 2d 3e 75 2e 69 56 61 6c 75 65 20 3d 20 69 43 6f  ->u.iValue = iCo
1fedc 6c 3b 0a 20 20 20 20 20 20 20 20 70 49 74 65 6d  l;.        pItem
1fedd 2d 3e 69 43 6f 6c 20 3d 20 28 75 31 36 29 69 43  ->iCol = (u16)iC
1fede 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 49 74 65  ol;.        pIte
1fedf 6d 2d 3e 64 6f 6e 65 20 3d 20 31 3b 0a 20 20 20  m->done = 1;.   
1fee0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1fee1 20 20 6d 6f 72 65 54 6f 44 6f 20 3d 20 31 3b 0a    moreToDo = 1;.
1fee2 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1fee3 20 20 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c    pSelect = pSel
1fee4 65 63 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a  ect->pNext;.  }.
1fee5 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 72    for(i=0; i<pOr
1fee6 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b  derBy->nExpr; i+
1fee7 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 4f 72 64  +){.    if( pOrd
1fee8 65 72 42 79 2d 3e 61 5b 69 5d 2e 64 6f 6e 65 3d  erBy->a[i].done=
1fee9 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
1feea 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1feeb 73 65 2c 20 22 25 72 20 4f 52 44 45 52 20 42 59  se, "%r ORDER BY
1feec 20 74 65 72 6d 20 64 6f 65 73 20 6e 6f 74 20 6d   term does not m
1feed 61 74 63 68 20 61 6e 79 20 22 0a 20 20 20 20 20  atch any ".     
1feee 20 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e 20 69         "column i
1feef 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  n the result set
1fef0 22 2c 20 69 2b 31 29 3b 0a 20 20 20 20 20 20 72  ", i+1);.      r
1fef1 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
1fef2 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
1fef3 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 65 76  ../*.** Check ev
1fef4 65 72 79 20 74 65 72 6d 20 69 6e 20 74 68 65 20  ery term in the 
1fef5 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55  ORDER BY or GROU
1fef6 50 20 42 59 20 63 6c 61 75 73 65 20 70 4f 72 64  P BY clause pOrd
1fef7 65 72 42 79 20 6f 66 0a 2a 2a 20 74 68 65 20 53  erBy of.** the S
1fef8 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
1fef9 70 53 65 6c 65 63 74 2e 20 20 49 66 20 61 6e 79  pSelect.  If any
1fefa 20 74 65 72 6d 20 69 73 20 72 65 66 65 72 65 6e   term is referen
1fefb 63 65 20 74 6f 20 61 0a 2a 2a 20 72 65 73 75 6c  ce to a.** resul
1fefc 74 20 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e  t set expression
1fefd 20 28 61 73 20 64 65 74 65 72 6d 69 6e 65 64 20   (as determined 
1fefe 62 79 20 74 68 65 20 45 78 70 72 4c 69 73 74 2e  by the ExprList.
1feff 61 2e 69 43 6f 6c 20 66 69 65 6c 64 29 0a 2a 2a  a.iCol field).**
1ff00 20 74 68 65 6e 20 63 6f 6e 76 65 72 74 20 74 68   then convert th
1ff01 61 74 20 74 65 72 6d 20 69 6e 74 6f 20 61 20 63  at term into a c
1ff02 6f 70 79 20 6f 66 20 74 68 65 20 63 6f 72 72 65  opy of the corre
1ff03 73 70 6f 6e 64 69 6e 67 20 72 65 73 75 6c 74 20  sponding result 
1ff04 73 65 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 2e 0a 2a  set.** column..*
1ff05 2a 0a 2a 2a 20 49 66 20 61 6e 79 20 65 72 72 6f  *.** If any erro
1ff06 72 73 20 61 72 65 20 64 65 74 65 63 74 65 64 2c  rs are detected,
1ff07 20 61 64 64 20 61 6e 20 65 72 72 6f 72 20 6d 65   add an error me
1ff08 73 73 61 67 65 20 74 6f 20 70 50 61 72 73 65 20  ssage to pParse 
1ff09 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 6e 6f  and.** return no
1ff0a 6e 2d 7a 65 72 6f 2e 20 20 52 65 74 75 72 6e 20  n-zero.  Return 
1ff0b 7a 65 72 6f 20 69 66 20 6e 6f 20 65 72 72 6f 72  zero if no error
1ff0c 73 20 61 72 65 20 73 65 65 6e 2e 0a 2a 2f 0a 53  s are seen..*/.S
1ff0d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1ff0e 74 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65  t sqlite3Resolve
1ff0f 4f 72 64 65 72 47 72 6f 75 70 42 79 28 0a 20 20  OrderGroupBy(.  
1ff10 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
1ff11 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67        /* Parsing
1ff12 20 63 6f 6e 74 65 78 74 2e 20 20 4c 65 61 76 65   context.  Leave
1ff13 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20   error messages 
1ff14 68 65 72 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74  here */.  Select
1ff15 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 20 20 20   *pSelect,      
1ff16 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74  /* The SELECT st
1ff17 61 74 65 6d 65 6e 74 20 63 6f 6e 74 61 69 6e 69  atement containi
1ff18 6e 67 20 74 68 65 20 63 6c 61 75 73 65 20 2a 2f  ng the clause */
1ff19 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72  .  ExprList *pOr
1ff1a 64 65 72 42 79 2c 20 20 20 2f 2a 20 54 68 65 20  derBy,   /* The 
1ff1b 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55  ORDER BY or GROU
1ff1c 50 20 42 59 20 63 6c 61 75 73 65 20 74 6f 20 62  P BY clause to b
1ff1d 65 20 70 72 6f 63 65 73 73 65 64 20 2a 2f 0a 20  e processed */. 
1ff1e 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79   const char *zTy
1ff1f 70 65 20 20 20 20 20 2f 2a 20 22 4f 52 44 45 52  pe     /* "ORDER
1ff20 22 20 6f 72 20 22 47 52 4f 55 50 22 20 2a 2f 0a  " or "GROUP" */.
1ff21 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 71  ){.  int i;.  sq
1ff22 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
1ff23 73 65 2d 3e 64 62 3b 0a 20 20 45 78 70 72 4c 69  se->db;.  ExprLi
1ff24 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 73 74  st *pEList;.  st
1ff25 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74  ruct ExprList_it
1ff26 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 69 66  em *pItem;..  if
1ff27 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 7c 7c  ( pOrderBy==0 ||
1ff28 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c   pParse->db->mal
1ff29 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75  locFailed ) retu
1ff2a 72 6e 20 30 3b 0a 23 69 66 20 53 51 4c 49 54 45  rn 0;.#if SQLITE
1ff2b 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66  _MAX_COLUMN.  if
1ff2c 28 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70  ( pOrderBy->nExp
1ff2d 72 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  r>db->aLimit[SQL
1ff2e 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e  ITE_LIMIT_COLUMN
1ff2f 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ] ){.    sqlite3
1ff30 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
1ff31 20 22 74 6f 6f 20 6d 61 6e 79 20 74 65 72 6d 73   "too many terms
1ff32 20 69 6e 20 25 73 20 42 59 20 63 6c 61 75 73 65   in %s BY clause
1ff33 22 2c 20 7a 54 79 70 65 29 3b 0a 20 20 20 20 72  ", zType);.    r
1ff34 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e  eturn 1;.  }.#en
1ff35 64 69 66 0a 20 20 70 45 4c 69 73 74 20 3d 20 70  dif.  pEList = p
1ff36 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a  Select->pEList;.
1ff37 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 73 74    assert( pEList
1ff38 21 3d 30 20 29 3b 20 20 2f 2a 20 73 71 6c 69 74  !=0 );  /* sqlit
1ff39 65 33 53 65 6c 65 63 74 4e 65 77 28 29 20 67 75  e3SelectNew() gu
1ff3a 61 72 61 6e 74 65 65 73 20 74 68 69 73 20 2a 2f  arantees this */
1ff3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65  .  for(i=0, pIte
1ff3c 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 69  m=pOrderBy->a; i
1ff3d 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72  <pOrderBy->nExpr
1ff3e 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b  ; i++, pItem++){
1ff3f 0a 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e  .    if( pItem->
1ff40 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 69 66  iCol ){.      if
1ff41 28 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 3e 70 45  ( pItem->iCol>pE
1ff42 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20  List->nExpr ){. 
1ff43 20 20 20 20 20 20 20 72 65 73 6f 6c 76 65 4f 75         resolveOu
1ff44 74 4f 66 52 61 6e 67 65 45 72 72 6f 72 28 70 50  tOfRangeError(pP
1ff45 61 72 73 65 2c 20 7a 54 79 70 65 2c 20 69 2b 31  arse, zType, i+1
1ff46 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29  , pEList->nExpr)
1ff47 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
1ff48 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   1;.      }.    
1ff49 20 20 72 65 73 6f 6c 76 65 41 6c 69 61 73 28 70    resolveAlias(p
1ff4a 50 61 72 73 65 2c 20 70 45 4c 69 73 74 2c 20 70  Parse, pEList, p
1ff4b 49 74 65 6d 2d 3e 69 43 6f 6c 2d 31 2c 20 70 49  Item->iCol-1, pI
1ff4c 74 65 6d 2d 3e 70 45 78 70 72 2c 20 7a 54 79 70  tem->pExpr, zTyp
1ff4d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  e);.    }.  }.  
1ff4e 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
1ff4f 2a 2a 20 70 4f 72 64 65 72 42 79 20 69 73 20 61  ** pOrderBy is a
1ff50 6e 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52  n ORDER BY or GR
1ff51 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 69 6e  OUP BY clause in
1ff52 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
1ff53 74 20 70 53 65 6c 65 63 74 2e 0a 2a 2a 20 54 68  t pSelect..** Th
1ff54 65 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 6f  e Name context o
1ff55 66 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61  f the SELECT sta
1ff56 74 65 6d 65 6e 74 20 69 73 20 70 4e 43 2e 20 20  tement is pNC.  
1ff57 7a 54 79 70 65 20 69 73 20 65 69 74 68 65 72 0a  zType is either.
1ff58 2a 2a 20 22 4f 52 44 45 52 22 20 6f 72 20 22 47  ** "ORDER" or "G
1ff59 52 4f 55 50 22 20 64 65 70 65 6e 64 69 6e 67 20  ROUP" depending 
1ff5a 6f 6e 20 77 68 69 63 68 20 74 79 70 65 20 6f 66  on which type of
1ff5b 20 63 6c 61 75 73 65 20 70 4f 72 64 65 72 42 79   clause pOrderBy
1ff5c 20 69 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20   is..**.** This 
1ff5d 72 6f 75 74 69 6e 65 20 72 65 73 6f 6c 76 65 73  routine resolves
1ff5e 20 65 61 63 68 20 74 65 72 6d 20 6f 66 20 74 68   each term of th
1ff5f 65 20 63 6c 61 75 73 65 20 69 6e 74 6f 20 61 6e  e clause into an
1ff60 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 20   expression..** 
1ff61 49 66 20 74 68 65 20 6f 72 64 65 72 2d 62 79 20  If the order-by 
1ff62 74 65 72 6d 20 69 73 20 61 6e 20 69 6e 74 65 67  term is an integ
1ff63 65 72 20 49 20 62 65 74 77 65 65 6e 20 31 20 61  er I between 1 a
1ff64 6e 64 20 4e 20 28 77 68 65 72 65 20 4e 20 69 73  nd N (where N is
1ff65 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f   the.** number o
1ff66 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  f columns in the
1ff67 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74   result set of t
1ff68 68 65 20 53 45 4c 45 43 54 29 20 74 68 65 6e 20  he SELECT) then 
1ff69 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a  the expression.*
1ff6a 2a 20 69 6e 20 74 68 65 20 72 65 73 6f 6c 75 74  * in the resolut
1ff6b 69 6f 6e 20 69 73 20 61 20 63 6f 70 79 20 6f 66  ion is a copy of
1ff6c 20 74 68 65 20 49 2d 74 68 20 72 65 73 75 6c 74   the I-th result
1ff6d 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 2e  -set expression.
1ff6e 20 20 49 66 0a 2a 2a 20 74 68 65 20 6f 72 64 65    If.** the orde
1ff6f 72 2d 62 79 20 74 65 72 6d 20 69 73 20 61 6e 20  r-by term is an 
1ff70 69 64 65 6e 74 69 66 79 20 74 68 61 74 20 63 6f  identify that co
1ff71 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65  rresponds to the
1ff72 20 41 53 2d 6e 61 6d 65 20 6f 66 0a 2a 2a 20 61   AS-name of.** a
1ff73 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72   result-set expr
1ff74 65 73 73 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65  ession, then the
1ff75 20 74 65 72 6d 20 72 65 73 6f 6c 76 65 73 20 74   term resolves t
1ff76 6f 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a  o a copy of the.
1ff77 2a 2a 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78  ** result-set ex
1ff78 70 72 65 73 73 69 6f 6e 2e 20 20 4f 74 68 65 72  pression.  Other
1ff79 77 69 73 65 2c 20 74 68 65 20 65 78 70 72 65 73  wise, the expres
1ff7a 73 69 6f 6e 20 69 73 20 72 65 73 6f 6c 76 65 64  sion is resolved
1ff7b 20 69 6e 0a 2a 2a 20 74 68 65 20 75 73 75 61 6c   in.** the usual
1ff7c 20 77 61 79 20 2d 20 75 73 69 6e 67 20 73 71 6c   way - using sql
1ff7d 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e  ite3ResolveExprN
1ff7e 61 6d 65 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68  ames()..**.** Th
1ff7f 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  is routine retur
1ff80 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ns the number of
1ff81 20 65 72 72 6f 72 73 2e 20 20 49 66 20 65 72 72   errors.  If err
1ff82 6f 72 73 20 6f 63 63 75 72 2c 20 74 68 65 6e 0a  ors occur, then.
1ff83 2a 2a 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74  ** an appropriat
1ff84 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  e error message 
1ff85 6d 69 67 68 74 20 62 65 20 6c 65 66 74 20 69 6e  might be left in
1ff86 20 70 50 61 72 73 65 2e 20 20 28 4f 4f 4d 20 65   pParse.  (OOM e
1ff87 72 72 6f 72 73 0a 2a 2a 20 65 78 63 65 70 74 65  rrors.** excepte
1ff88 64 2e 29 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  d.).*/.static in
1ff89 74 20 72 65 73 6f 6c 76 65 4f 72 64 65 72 47 72  t resolveOrderGr
1ff8a 6f 75 70 42 79 28 0a 20 20 4e 61 6d 65 43 6f 6e  oupBy(.  NameCon
1ff8b 74 65 78 74 20 2a 70 4e 43 2c 20 20 20 20 20 2f  text *pNC,     /
1ff8c 2a 20 54 68 65 20 6e 61 6d 65 20 63 6f 6e 74 65  * The name conte
1ff8d 78 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  xt of the SELECT
1ff8e 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
1ff8f 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c  Select *pSelect,
1ff90 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c        /* The SEL
1ff91 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 68 6f  ECT statement ho
1ff92 6c 64 69 6e 67 20 70 4f 72 64 65 72 42 79 20 2a  lding pOrderBy *
1ff93 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f  /.  ExprList *pO
1ff94 72 64 65 72 42 79 2c 20 20 20 2f 2a 20 41 6e 20  rderBy,   /* An 
1ff95 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55  ORDER BY or GROU
1ff96 50 20 42 59 20 63 6c 61 75 73 65 20 74 6f 20 72  P BY clause to r
1ff97 65 73 6f 6c 76 65 20 2a 2f 0a 20 20 63 6f 6e 73  esolve */.  cons
1ff98 74 20 63 68 61 72 20 2a 7a 54 79 70 65 20 20 20  t char *zType   
1ff99 20 20 2f 2a 20 45 69 74 68 65 72 20 22 4f 52 44    /* Either "ORD
1ff9a 45 52 22 20 6f 72 20 22 47 52 4f 55 50 22 2c 20  ER" or "GROUP", 
1ff9b 61 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 2a  as appropriate *
1ff9c 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20  /.){.  int i;   
1ff9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ff9e 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
1ff9f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 69  unter */.  int i
1ffa0 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  Col;            
1ffa1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c            /* Col
1ffa2 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  umn number */.  
1ffa3 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
1ffa4 69 74 65 6d 20 2a 70 49 74 65 6d 3b 20 20 20 2f  item *pItem;   /
1ffa5 2a 20 41 20 74 65 72 6d 20 6f 66 20 74 68 65 20  * A term of the 
1ffa6 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
1ffa7 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  */.  Parse *pPar
1ffa8 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  se;             
1ffa9 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
1ffaa 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20  ontext */.  int 
1ffab 6e 52 65 73 75 6c 74 3b 20 20 20 20 20 20 20 20  nResult;        
1ffac 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
1ffad 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e  mber of terms in
1ffae 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
1ffaf 2a 2f 0a 0a 20 20 69 66 28 20 70 4f 72 64 65 72  */..  if( pOrder
1ffb0 42 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  By==0 ) return 0
1ffb1 3b 0a 20 20 6e 52 65 73 75 6c 74 20 3d 20 70 53  ;.  nResult = pS
1ffb2 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 6e  elect->pEList->n
1ffb3 45 78 70 72 3b 0a 20 20 70 50 61 72 73 65 20 3d  Expr;.  pParse =
1ffb4 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20   pNC->pParse;.  
1ffb5 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70  for(i=0, pItem=p
1ffb6 4f 72 64 65 72 42 79 2d 3e 61 3b 20 69 3c 70 4f  OrderBy->a; i<pO
1ffb7 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69  rderBy->nExpr; i
1ffb8 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20  ++, pItem++){.  
1ffb9 20 20 45 78 70 72 20 2a 70 45 20 3d 20 70 49 74    Expr *pE = pIt
1ffba 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 69  em->pExpr;.    i
1ffbb 43 6f 6c 20 3d 20 72 65 73 6f 6c 76 65 41 73 4e  Col = resolveAsN
1ffbc 61 6d 65 28 70 50 61 72 73 65 2c 20 70 53 65 6c  ame(pParse, pSel
1ffbd 65 63 74 2d 3e 70 45 4c 69 73 74 2c 20 70 45 29  ect->pEList, pE)
1ffbe 3b 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 30  ;.    if( iCol>0
1ffbf 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20   ){.      /* If 
1ffc0 61 6e 20 41 53 2d 6e 61 6d 65 20 6d 61 74 63 68  an AS-name match
1ffc1 20 69 73 20 66 6f 75 6e 64 2c 20 6d 61 72 6b 20   is found, mark 
1ffc2 74 68 69 73 20 4f 52 44 45 52 20 42 59 20 63 6f  this ORDER BY co
1ffc3 6c 75 6d 6e 20 61 73 20 62 65 69 6e 67 0a 20 20  lumn as being.  
1ffc4 20 20 20 20 2a 2a 20 61 20 63 6f 70 79 20 6f 66      ** a copy of
1ffc5 20 74 68 65 20 69 43 6f 6c 2d 74 68 20 72 65 73   the iCol-th res
1ffc6 75 6c 74 2d 73 65 74 20 63 6f 6c 75 6d 6e 2e 20  ult-set column. 
1ffc7 20 54 68 65 20 73 75 62 73 65 71 75 65 6e 74 20   The subsequent 
1ffc8 63 61 6c 6c 20 74 6f 0a 20 20 20 20 20 20 2a 2a  call to.      **
1ffc9 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f   sqlite3ResolveO
1ffca 72 64 65 72 47 72 6f 75 70 42 79 28 29 20 77 69  rderGroupBy() wi
1ffcb 6c 6c 20 63 6f 6e 76 65 72 74 20 74 68 65 20 65  ll convert the e
1ffcc 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 61 0a 20  xpression to a. 
1ffcd 20 20 20 20 20 2a 2a 20 63 6f 70 79 20 6f 66 20       ** copy of 
1ffce 74 68 65 20 69 43 6f 6c 2d 74 68 20 72 65 73 75  the iCol-th resu
1ffcf 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f  lt-set expressio
1ffd0 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 70 49 74 65  n. */.      pIte
1ffd1 6d 2d 3e 69 43 6f 6c 20 3d 20 28 75 31 36 29 69  m->iCol = (u16)i
1ffd2 43 6f 6c 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69  Col;.      conti
1ffd3 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  nue;.    }.    i
1ffd4 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73  f( sqlite3ExprIs
1ffd5 49 6e 74 65 67 65 72 28 70 45 2c 20 26 69 43 6f  Integer(pE, &iCo
1ffd6 6c 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54  l) ){.      /* T
1ffd7 68 65 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d  he ORDER BY term
1ffd8 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63   is an integer c
1ffd9 6f 6e 73 74 61 6e 74 2e 20 20 41 67 61 69 6e 2c  onstant.  Again,
1ffda 20 73 65 74 20 74 68 65 20 63 6f 6c 75 6d 6e 0a   set the column.
1ffdb 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20        ** number 
1ffdc 73 6f 20 74 68 61 74 20 73 71 6c 69 74 65 33 52  so that sqlite3R
1ffdd 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70  esolveOrderGroup
1ffde 42 79 28 29 20 77 69 6c 6c 20 63 6f 6e 76 65 72  By() will conver
1ffdf 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 6f  t the.      ** o
1ffe0 72 64 65 72 2d 62 79 20 74 65 72 6d 20 74 6f 20  rder-by term to 
1ffe1 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65  a copy of the re
1ffe2 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73  sult-set express
1ffe3 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  ion */.      if(
1ffe4 20 69 43 6f 6c 3c 31 20 29 7b 0a 20 20 20 20 20   iCol<1 ){.     
1ffe5 20 20 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66 52     resolveOutOfR
1ffe6 61 6e 67 65 45 72 72 6f 72 28 70 50 61 72 73 65  angeError(pParse
1ffe7 2c 20 7a 54 79 70 65 2c 20 69 2b 31 2c 20 6e 52  , zType, i+1, nR
1ffe8 65 73 75 6c 74 29 3b 0a 20 20 20 20 20 20 20 20  esult);.        
1ffe9 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20  return 1;.      
1ffea 7d 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69  }.      pItem->i
1ffeb 43 6f 6c 20 3d 20 28 75 31 36 29 69 43 6f 6c 3b  Col = (u16)iCol;
1ffec 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b  .      continue;
1ffed 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4f  .    }..    /* O
1ffee 74 68 65 72 77 69 73 65 2c 20 74 72 65 61 74 20  therwise, treat 
1ffef 74 68 65 20 4f 52 44 45 52 20 42 59 20 74 65 72  the ORDER BY ter
1fff0 6d 20 61 73 20 61 6e 20 6f 72 64 69 6e 61 72 79  m as an ordinary
1fff1 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20   expression */. 
1fff2 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d     pItem->iCol =
1fff3 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69   0;.    if( sqli
1fff4 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61  te3ResolveExprNa
1fff5 6d 65 73 28 70 4e 43 2c 20 70 45 29 20 29 7b 0a  mes(pNC, pE) ){.
1fff6 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
1fff7 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
1fff8 72 6e 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76  rn sqlite3Resolv
1fff9 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 70 50  eOrderGroupBy(pP
1fffa 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2c 20 70  arse, pSelect, p
1fffb 4f 72 64 65 72 42 79 2c 20 7a 54 79 70 65 29 3b  OrderBy, zType);
1fffc 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c 76  .}../*.** Resolv
1fffd 65 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 53  e names in the S
1fffe 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
1ffff 70 20 61 6e 64 20 61 6c 6c 20 6f 66 20 69 74 73  p and all of its
20000 20 64 65 73 63 65 6e 64 65 6e 74 73 2e 0a 2a 2f   descendents..*/
20001 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f  .static int reso
20002 6c 76 65 53 65 6c 65 63 74 53 74 65 70 28 57 61  lveSelectStep(Wa
20003 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53  lker *pWalker, S
20004 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 4e 61 6d  elect *p){.  Nam
20005 65 43 6f 6e 74 65 78 74 20 2a 70 4f 75 74 65 72  eContext *pOuter
20006 4e 43 3b 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20  NC;  /* Context 
20007 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68  that contains th
20008 69 73 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 4e  is SELECT */.  N
20009 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20  ameContext sNC; 
2000a 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63         /* Name c
2000b 6f 6e 74 65 78 74 20 6f 66 20 74 68 69 73 20 53  ontext of this S
2000c 45 4c 45 43 54 20 2a 2f 0a 20 20 69 6e 74 20 69  ELECT */.  int i
2000d 73 43 6f 6d 70 6f 75 6e 64 3b 20 20 20 20 20 20  sCompound;      
2000e 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 20     /* True if p 
2000f 69 73 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65  is a compound se
20010 6c 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  lect */.  int nC
20011 6f 6d 70 6f 75 6e 64 3b 20 20 20 20 20 20 20 20  ompound;        
20012 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
20013 6f 6d 70 6f 75 6e 64 20 74 65 72 6d 73 20 70 72  ompound terms pr
20014 6f 63 65 73 73 65 64 20 73 6f 20 66 61 72 20 2a  ocessed so far *
20015 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  /.  Parse *pPars
20016 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50  e;          /* P
20017 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
20018 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
20019 4c 69 73 74 3b 20 20 20 20 20 20 20 2f 2a 20 52  List;       /* R
2001a 65 73 75 6c 74 20 73 65 74 20 65 78 70 72 65 73  esult set expres
2001b 73 69 6f 6e 20 6c 69 73 74 20 2a 2f 0a 20 20 69  sion list */.  i
2001c 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
2001d 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
2001e 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 45 78 70 72  ounter */.  Expr
2001f 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79 3b 20  List *pGroupBy; 
20020 20 20 20 20 2f 2a 20 54 68 65 20 47 52 4f 55 50      /* The GROUP
20021 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20   BY clause */.  
20022 53 65 6c 65 63 74 20 2a 70 4c 65 66 74 6d 6f 73  Select *pLeftmos
20023 74 3b 20 20 20 20 20 20 2f 2a 20 4c 65 66 74 2d  t;      /* Left-
20024 6d 6f 73 74 20 6f 66 20 53 45 4c 45 43 54 20 6f  most of SELECT o
20025 66 20 61 20 63 6f 6d 70 6f 75 6e 64 20 2a 2f 0a  f a compound */.
20026 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
20027 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
20028 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
20029 20 2a 2f 0a 20 20 0a 0a 20 20 61 73 73 65 72 74   */.  ..  assert
2002a 28 20 70 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  ( p!=0 );.  if( 
2002b 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46  p->selFlags & SF
2002c 5f 52 65 73 6f 6c 76 65 64 20 29 7b 0a 20 20 20  _Resolved ){.   
2002d 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e   return WRC_Prun
2002e 65 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 65 72 4e  e;.  }.  pOuterN
2002f 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70  C = pWalker->u.p
20030 4e 43 3b 0a 20 20 70 50 61 72 73 65 20 3d 20 70  NC;.  pParse = p
20031 57 61 6c 6b 65 72 2d 3e 70 50 61 72 73 65 3b 0a  Walker->pParse;.
20032 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64    db = pParse->d
20033 62 3b 0a 0a 20 20 2f 2a 20 4e 6f 72 6d 61 6c 6c  b;..  /* Normall
20034 79 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45  y sqlite3SelectE
20035 78 70 61 6e 64 28 29 20 77 69 6c 6c 20 62 65 20  xpand() will be 
20036 63 61 6c 6c 65 64 20 66 69 72 73 74 20 61 6e 64  called first and
20037 20 77 69 6c 6c 20 68 61 76 65 0a 20 20 2a 2a 20   will have.  ** 
20038 61 6c 72 65 61 64 79 20 65 78 70 61 6e 64 65 64  already expanded
20039 20 74 68 69 73 20 53 45 4c 45 43 54 2e 20 20 48   this SELECT.  H
2003a 6f 77 65 76 65 72 2c 20 69 66 20 74 68 69 73 20  owever, if this 
2003b 69 73 20 61 20 73 75 62 71 75 65 72 79 20 77 69  is a subquery wi
2003c 74 68 69 6e 0a 20 20 2a 2a 20 61 6e 20 65 78 70  thin.  ** an exp
2003d 72 65 73 73 69 6f 6e 2c 20 73 71 6c 69 74 65 33  ression, sqlite3
2003e 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73  ResolveExprNames
2003f 28 29 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65  () will be calle
20040 64 20 77 69 74 68 6f 75 74 20 61 0a 20 20 2a 2a  d without a.  **
20041 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73   prior call to s
20042 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 61  qlite3SelectExpa
20043 6e 64 28 29 2e 20 20 57 68 65 6e 20 74 68 61 74  nd().  When that
20044 20 68 61 70 70 65 6e 73 2c 20 6c 65 74 0a 20 20   happens, let.  
20045 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  ** sqlite3Select
20046 50 72 65 70 28 29 20 64 6f 20 61 6c 6c 20 6f 66  Prep() do all of
20047 20 74 68 65 20 70 72 6f 63 65 73 73 69 6e 67 20   the processing 
20048 66 6f 72 20 74 68 69 73 20 53 45 4c 45 43 54 2e  for this SELECT.
20049 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c  .  ** sqlite3Sel
2004a 65 63 74 50 72 65 70 28 29 20 77 69 6c 6c 20 69  ectPrep() will i
2004b 6e 76 6f 6b 65 20 62 6f 74 68 20 73 71 6c 69 74  nvoke both sqlit
2004c 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 29  e3SelectExpand()
2004d 20 61 6e 64 0a 20 20 2a 2a 20 74 68 69 73 20 72   and.  ** this r
2004e 6f 75 74 69 6e 65 20 69 6e 20 74 68 65 20 63 6f  outine in the co
2004f 72 72 65 63 74 20 6f 72 64 65 72 2e 0a 20 20 2a  rrect order..  *
20050 2f 0a 20 20 69 66 28 20 28 70 2d 3e 73 65 6c 46  /.  if( (p->selF
20051 6c 61 67 73 20 26 20 53 46 5f 45 78 70 61 6e 64  lags & SF_Expand
20052 65 64 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  ed)==0 ){.    sq
20053 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65 70 28  lite3SelectPrep(
20054 70 50 61 72 73 65 2c 20 70 2c 20 70 4f 75 74 65  pParse, p, pOute
20055 72 4e 43 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  rNC);.    return
20056 20 28 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c   (pParse->nErr |
20057 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
20058 65 64 29 20 3f 20 57 52 43 5f 41 62 6f 72 74 20  ed) ? WRC_Abort 
20059 3a 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d  : WRC_Prune;.  }
2005a 0a 0a 20 20 69 73 43 6f 6d 70 6f 75 6e 64 20 3d  ..  isCompound =
2005b 20 70 2d 3e 70 50 72 69 6f 72 21 3d 30 3b 0a 20   p->pPrior!=0;. 
2005c 20 6e 43 6f 6d 70 6f 75 6e 64 20 3d 20 30 3b 0a   nCompound = 0;.
2005d 20 20 70 4c 65 66 74 6d 6f 73 74 20 3d 20 70 3b    pLeftmost = p;
2005e 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20  .  while( p ){. 
2005f 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 73     assert( (p->s
20060 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 45 78 70  elFlags & SF_Exp
20061 61 6e 64 65 64 29 21 3d 30 20 29 3b 0a 20 20 20  anded)!=0 );.   
20062 20 61 73 73 65 72 74 28 20 28 70 2d 3e 73 65 6c   assert( (p->sel
20063 46 6c 61 67 73 20 26 20 53 46 5f 52 65 73 6f 6c  Flags & SF_Resol
20064 76 65 64 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70  ved)==0 );.    p
20065 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46  ->selFlags |= SF
20066 5f 52 65 73 6f 6c 76 65 64 3b 0a 0a 20 20 20 20  _Resolved;..    
20067 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20 65  /* Resolve the e
20068 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68  xpressions in th
20069 65 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46 53  e LIMIT and OFFS
2006a 45 54 20 63 6c 61 75 73 65 73 2e 20 54 68 65 73  ET clauses. Thes
2006b 65 0a 20 20 20 20 2a 2a 20 61 72 65 20 6e 6f 74  e.    ** are not
2006c 20 61 6c 6c 6f 77 65 64 20 74 6f 20 72 65 66 65   allowed to refe
2006d 72 20 74 6f 20 61 6e 79 20 6e 61 6d 65 73 2c 20  r to any names, 
2006e 73 6f 20 70 61 73 73 20 61 6e 20 65 6d 70 74 79  so pass an empty
2006f 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2e 0a 20 20   NameContext..  
20070 20 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28    */.    memset(
20071 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28  &sNC, 0, sizeof(
20072 73 4e 43 29 29 3b 0a 20 20 20 20 73 4e 43 2e 70  sNC));.    sNC.p
20073 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a  Parse = pParse;.
20074 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52      if( sqlite3R
20075 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28  esolveExprNames(
20076 26 73 4e 43 2c 20 70 2d 3e 70 4c 69 6d 69 74 29  &sNC, p->pLimit)
20077 20 7c 7c 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ||.        sqli
20078 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61  te3ResolveExprNa
20079 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70 4f 66  mes(&sNC, p->pOf
2007a 66 73 65 74 29 20 29 7b 0a 20 20 20 20 20 20 72  fset) ){.      r
2007b 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b  eturn WRC_Abort;
2007c 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a  .    }.  .    /*
2007d 20 53 65 74 20 75 70 20 74 68 65 20 6c 6f 63 61   Set up the loca
2007e 6c 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20 74  l name-context t
2007f 6f 20 70 61 73 73 20 74 6f 20 73 71 6c 69 74 65  o pass to sqlite
20080 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65  3ResolveExprName
20081 73 28 29 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65  s() to.    ** re
20082 73 6f 6c 76 65 20 74 68 65 20 72 65 73 75 6c 74  solve the result
20083 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 20  -set expression 
20084 6c 69 73 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  list..    */.   
20085 20 73 4e 43 2e 61 6c 6c 6f 77 41 67 67 20 3d 20   sNC.allowAgg = 
20086 31 3b 0a 20 20 20 20 73 4e 43 2e 70 53 72 63 4c  1;.    sNC.pSrcL
20087 69 73 74 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20  ist = p->pSrc;. 
20088 20 20 20 73 4e 43 2e 70 4e 65 78 74 20 3d 20 70     sNC.pNext = p
20089 4f 75 74 65 72 4e 43 3b 0a 20 20 0a 20 20 20 20  OuterNC;.  .    
2008a 2f 2a 20 52 65 73 6f 6c 76 65 20 6e 61 6d 65 73  /* Resolve names
2008b 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
2008c 65 74 2e 20 2a 2f 0a 20 20 20 20 70 45 4c 69 73  et. */.    pELis
2008d 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20  t = p->pEList;. 
2008e 20 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 73     assert( pELis
2008f 74 21 3d 30 20 29 3b 0a 20 20 20 20 66 6f 72 28  t!=0 );.    for(
20090 69 3d 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e  i=0; i<pEList->n
20091 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Expr; i++){.    
20092 20 20 45 78 70 72 20 2a 70 58 20 3d 20 70 45 4c    Expr *pX = pEL
20093 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b  ist->a[i].pExpr;
20094 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
20095 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d  e3ResolveExprNam
20096 65 73 28 26 73 4e 43 2c 20 70 58 29 20 29 7b 0a  es(&sNC, pX) ){.
20097 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 57          return W
20098 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20  RC_Abort;.      
20099 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f  }.    }.  .    /
2009a 2a 20 52 65 63 75 72 73 69 76 65 6c 79 20 72 65  * Recursively re
2009b 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20 61  solve names in a
2009c 6c 6c 20 73 75 62 71 75 65 72 69 65 73 0a 20 20  ll subqueries.  
2009d 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30    */.    for(i=0
2009e 3b 20 69 3c 70 2d 3e 70 53 72 63 2d 3e 6e 53 72  ; i<p->pSrc->nSr
2009f 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73  c; i++){.      s
200a0 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
200a1 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 2d 3e  em *pItem = &p->
200a2 70 53 72 63 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20  pSrc->a[i];.    
200a3 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 70 53 65    if( pItem->pSe
200a4 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20  lect ){.        
200a5 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 61 76  const char *zSav
200a6 65 64 43 6f 6e 74 65 78 74 20 3d 20 70 50 61 72  edContext = pPar
200a7 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74  se->zAuthContext
200a8 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 49  ;.        if( pI
200a9 74 65 6d 2d 3e 7a 4e 61 6d 65 20 29 20 70 50 61  tem->zName ) pPa
200aa 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78  rse->zAuthContex
200ab 74 20 3d 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65  t = pItem->zName
200ac 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
200ad 33 52 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61  3ResolveSelectNa
200ae 6d 65 73 28 70 50 61 72 73 65 2c 20 70 49 74 65  mes(pParse, pIte
200af 6d 2d 3e 70 53 65 6c 65 63 74 2c 20 70 4f 75 74  m->pSelect, pOut
200b0 65 72 4e 43 29 3b 0a 20 20 20 20 20 20 20 20 70  erNC);.        p
200b1 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74  Parse->zAuthCont
200b2 65 78 74 20 3d 20 7a 53 61 76 65 64 43 6f 6e 74  ext = zSavedCont
200b3 65 78 74 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ext;.        if(
200b4 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c   pParse->nErr ||
200b5 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
200b6 64 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41  d ) return WRC_A
200b7 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20  bort;.      }.  
200b8 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66    }.  .    /* If
200b9 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 61 67   there are no ag
200ba 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
200bb 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 2d  s in the result-
200bc 73 65 74 2c 20 61 6e 64 20 6e 6f 20 47 52 4f 55  set, and no GROU
200bd 50 20 42 59 20 0a 20 20 20 20 2a 2a 20 65 78 70  P BY .    ** exp
200be 72 65 73 73 69 6f 6e 2c 20 64 6f 20 6e 6f 74 20  ression, do not 
200bf 61 6c 6c 6f 77 20 61 67 67 72 65 67 61 74 65 73  allow aggregates
200c0 20 69 6e 20 61 6e 79 20 6f 66 20 74 68 65 20 6f   in any of the o
200c1 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 73  ther expressions
200c2 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73  ..    */.    ass
200c3 65 72 74 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67  ert( (p->selFlag
200c4 73 20 26 20 53 46 5f 41 67 67 72 65 67 61 74 65  s & SF_Aggregate
200c5 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70 47 72 6f  )==0 );.    pGro
200c6 75 70 42 79 20 3d 20 70 2d 3e 70 47 72 6f 75 70  upBy = p->pGroup
200c7 42 79 3b 0a 20 20 20 20 69 66 28 20 70 47 72 6f  By;.    if( pGro
200c8 75 70 42 79 20 7c 7c 20 73 4e 43 2e 68 61 73 41  upBy || sNC.hasA
200c9 67 67 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 73  gg ){.      p->s
200ca 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f 41 67  elFlags |= SF_Ag
200cb 67 72 65 67 61 74 65 3b 0a 20 20 20 20 7d 65 6c  gregate;.    }el
200cc 73 65 7b 0a 20 20 20 20 20 20 73 4e 43 2e 61 6c  se{.      sNC.al
200cd 6c 6f 77 41 67 67 20 3d 20 30 3b 0a 20 20 20 20  lowAgg = 0;.    
200ce 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 61  }.  .    /* If a
200cf 20 48 41 56 49 4e 47 20 63 6c 61 75 73 65 20 69   HAVING clause i
200d0 73 20 70 72 65 73 65 6e 74 2c 20 74 68 65 6e 20  s present, then 
200d1 74 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 20  there must be a 
200d2 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2e  GROUP BY clause.
200d3 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
200d4 70 2d 3e 70 48 61 76 69 6e 67 20 26 26 20 21 70  p->pHaving && !p
200d5 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 20  GroupBy ){.     
200d6 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
200d7 28 70 50 61 72 73 65 2c 20 22 61 20 47 52 4f 55  (pParse, "a GROU
200d8 50 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 72  P BY clause is r
200d9 65 71 75 69 72 65 64 20 62 65 66 6f 72 65 20 48  equired before H
200da 41 56 49 4e 47 22 29 3b 0a 20 20 20 20 20 20 72  AVING");.      r
200db 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b  eturn WRC_Abort;
200dc 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a  .    }.  .    /*
200dd 20 41 64 64 20 74 68 65 20 65 78 70 72 65 73 73   Add the express
200de 69 6f 6e 20 6c 69 73 74 20 74 6f 20 74 68 65 20  ion list to the 
200df 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20 62 65 66  name-context bef
200e0 6f 72 65 20 70 61 72 73 69 6e 67 20 74 68 65 0a  ore parsing the.
200e1 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 65 78 70      ** other exp
200e2 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 20  ressions in the 
200e3 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
200e4 2e 20 54 68 69 73 20 69 73 20 73 6f 20 74 68 61  . This is so tha
200e5 74 0a 20 20 20 20 2a 2a 20 65 78 70 72 65 73 73  t.    ** express
200e6 69 6f 6e 73 20 69 6e 20 74 68 65 20 57 48 45 52  ions in the WHER
200e7 45 20 63 6c 61 75 73 65 20 28 65 74 63 2e 29 20  E clause (etc.) 
200e8 63 61 6e 20 72 65 66 65 72 20 74 6f 20 65 78 70  can refer to exp
200e9 72 65 73 73 69 6f 6e 73 20 62 79 0a 20 20 20 20  ressions by.    
200ea 2a 2a 20 61 6c 69 61 73 65 73 20 69 6e 20 74 68  ** aliases in th
200eb 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a 20 20  e result set..  
200ec 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4d 69 6e 6f    **.    ** Mino
200ed 72 20 70 6f 69 6e 74 3a 20 49 66 20 74 68 69 73  r point: If this
200ee 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 68   is the case, th
200ef 65 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  en the expressio
200f0 6e 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 2a 2a  n will be.    **
200f1 20 72 65 2d 65 76 61 6c 75 61 74 65 64 20 66 6f   re-evaluated fo
200f2 72 20 65 61 63 68 20 72 65 66 65 72 65 6e 63 65  r each reference
200f3 20 74 6f 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20   to it..    */. 
200f4 20 20 20 73 4e 43 2e 70 45 4c 69 73 74 20 3d 20     sNC.pEList = 
200f5 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20 69  p->pEList;.    i
200f6 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76  f( sqlite3Resolv
200f7 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c  eExprNames(&sNC,
200f8 20 70 2d 3e 70 57 68 65 72 65 29 20 7c 7c 0a 20   p->pWhere) ||. 
200f9 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 73        sqlite3Res
200fa 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73  olveExprNames(&s
200fb 4e 43 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29 0a  NC, p->pHaving).
200fc 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 65 74      ){.      ret
200fd 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20  urn WRC_Abort;. 
200fe 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65     }..    /* The
200ff 20 4f 52 44 45 52 20 42 59 20 61 6e 64 20 47 52   ORDER BY and GR
20100 4f 55 50 20 42 59 20 63 6c 61 75 73 65 73 20 6d  OUP BY clauses m
20101 61 79 20 6e 6f 74 20 72 65 66 65 72 20 74 6f 20  ay not refer to 
20102 74 65 72 6d 73 20 69 6e 0a 20 20 20 20 2a 2a 20  terms in.    ** 
20103 6f 75 74 65 72 20 71 75 65 72 69 65 73 20 0a 20  outer queries . 
20104 20 20 20 2a 2f 0a 20 20 20 20 73 4e 43 2e 70 4e     */.    sNC.pN
20105 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 73 4e 43  ext = 0;.    sNC
20106 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b 0a 0a  .allowAgg = 1;..
20107 20 20 20 20 2f 2a 20 50 72 6f 63 65 73 73 20 74      /* Process t
20108 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
20109 73 65 20 66 6f 72 20 73 69 6e 67 6c 65 74 6f 6e  se for singleton
2010a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2010b 74 73 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 4f  ts..    ** The O
2010c 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 66  RDER BY clause f
2010d 6f 72 20 63 6f 6d 70 6f 75 6e 64 73 20 53 45 4c  or compounds SEL
2010e 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 69  ECT statements i
2010f 73 20 68 61 6e 64 6c 65 64 0a 20 20 20 20 2a 2a  s handled.    **
20110 20 62 65 6c 6f 77 2c 20 61 66 74 65 72 20 61 6c   below, after al
20111 6c 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 2d  l of the result-
20112 73 65 74 73 20 66 6f 72 20 61 6c 6c 20 6f 66 20  sets for all of 
20113 74 68 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66 0a  the elements of.
20114 20 20 20 20 2a 2a 20 74 68 65 20 63 6f 6d 70 6f      ** the compo
20115 75 6e 64 20 68 61 76 65 20 62 65 65 6e 20 72 65  und have been re
20116 73 6f 6c 76 65 64 2e 0a 20 20 20 20 2a 2f 0a 20  solved..    */. 
20117 20 20 20 69 66 28 20 21 69 73 43 6f 6d 70 6f 75     if( !isCompou
20118 6e 64 20 26 26 20 72 65 73 6f 6c 76 65 4f 72 64  nd && resolveOrd
20119 65 72 47 72 6f 75 70 42 79 28 26 73 4e 43 2c 20  erGroupBy(&sNC, 
2011a 70 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20  p, p->pOrderBy, 
2011b 22 4f 52 44 45 52 22 29 20 29 7b 0a 20 20 20 20  "ORDER") ){.    
2011c 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f    return WRC_Abo
2011d 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  rt;.    }.    if
2011e 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
2011f 65 64 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  ed ){.      retu
20120 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20  rn WRC_Abort;.  
20121 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 52 65    }.  .    /* Re
20122 73 6f 6c 76 65 20 74 68 65 20 47 52 4f 55 50 20  solve the GROUP 
20123 42 59 20 63 6c 61 75 73 65 2e 20 20 41 74 20 74  BY clause.  At t
20124 68 65 20 73 61 6d 65 20 74 69 6d 65 2c 20 6d 61  he same time, ma
20125 6b 65 20 73 75 72 65 20 0a 20 20 20 20 2a 2a 20  ke sure .    ** 
20126 74 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61  the GROUP BY cla
20127 75 73 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e  use does not con
20128 74 61 69 6e 20 61 67 67 72 65 67 61 74 65 20 66  tain aggregate f
20129 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 20 20 2a 2f  unctions..    */
2012a 0a 20 20 20 20 69 66 28 20 70 47 72 6f 75 70 42  .    if( pGroupB
2012b 79 20 29 7b 0a 20 20 20 20 20 20 73 74 72 75 63  y ){.      struc
2012c 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
2012d 2a 70 49 74 65 6d 3b 0a 20 20 20 20 0a 20 20 20  *pItem;.    .   
2012e 20 20 20 69 66 28 20 72 65 73 6f 6c 76 65 4f 72     if( resolveOr
2012f 64 65 72 47 72 6f 75 70 42 79 28 26 73 4e 43 2c  derGroupBy(&sNC,
20130 20 70 2c 20 70 47 72 6f 75 70 42 79 2c 20 22 47   p, pGroupBy, "G
20131 52 4f 55 50 22 29 20 7c 7c 20 64 62 2d 3e 6d 61  ROUP") || db->ma
20132 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
20133 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43        return WRC
20134 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a  _Abort;.      }.
20135 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70        for(i=0, p
20136 49 74 65 6d 3d 70 47 72 6f 75 70 42 79 2d 3e 61  Item=pGroupBy->a
20137 3b 20 69 3c 70 47 72 6f 75 70 42 79 2d 3e 6e 45  ; i<pGroupBy->nE
20138 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b  xpr; i++, pItem+
20139 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  +){.        if( 
2013a 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
2013b 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 45 50  pItem->pExpr, EP
2013c 5f 41 67 67 29 20 29 7b 0a 20 20 20 20 20 20 20  _Agg) ){.       
2013d 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2013e 73 67 28 70 50 61 72 73 65 2c 20 22 61 67 67 72  sg(pParse, "aggr
2013f 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20  egate functions 
20140 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20  are not allowed 
20141 69 6e 20 22 0a 20 20 20 20 20 20 20 20 20 20 20  in ".           
20142 20 20 20 22 74 68 65 20 47 52 4f 55 50 20 42 59     "the GROUP BY
20143 20 63 6c 61 75 73 65 22 29 3b 0a 20 20 20 20 20   clause");.     
20144 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f       return WRC_
20145 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 20 20 7d  Abort;.        }
20146 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
20147 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 74      /* Advance t
20148 6f 20 74 68 65 20 6e 65 78 74 20 74 65 72 6d 20  o the next term 
20149 6f 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 0a  of the compound.
2014a 20 20 20 20 2a 2f 0a 20 20 20 20 70 20 3d 20 70      */.    p = p
2014b 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 6e 43  ->pPrior;.    nC
2014c 6f 6d 70 6f 75 6e 64 2b 2b 3b 0a 20 20 7d 0a 0a  ompound++;.  }..
2014d 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65    /* Resolve the
2014e 20 4f 52 44 45 52 20 42 59 20 6f 6e 20 61 20 63   ORDER BY on a c
2014f 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 61  ompound SELECT a
20150 66 74 65 72 20 61 6c 6c 20 74 65 72 6d 73 20 6f  fter all terms o
20151 66 0a 20 20 2a 2a 20 74 68 65 20 63 6f 6d 70 6f  f.  ** the compo
20152 75 6e 64 20 68 61 76 65 20 62 65 65 6e 20 72 65  und have been re
20153 73 6f 6c 76 65 64 2e 0a 20 20 2a 2f 0a 20 20 69  solved..  */.  i
20154 66 28 20 69 73 43 6f 6d 70 6f 75 6e 64 20 26 26  f( isCompound &&
20155 20 72 65 73 6f 6c 76 65 43 6f 6d 70 6f 75 6e 64   resolveCompound
20156 4f 72 64 65 72 42 79 28 70 50 61 72 73 65 2c 20  OrderBy(pParse, 
20157 70 4c 65 66 74 6d 6f 73 74 29 20 29 7b 0a 20 20  pLeftmost) ){.  
20158 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f    return WRC_Abo
20159 72 74 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72  rt;.  }..  retur
2015a 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 7d 0a 0a  n WRC_Prune;.}..
2015b 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
2015c 6e 65 20 77 61 6c 6b 73 20 61 6e 20 65 78 70 72  ne walks an expr
2015d 65 73 73 69 6f 6e 20 74 72 65 65 20 61 6e 64 20  ession tree and 
2015e 72 65 73 6f 6c 76 65 73 20 72 65 66 65 72 65 6e  resolves referen
2015f 63 65 73 20 74 6f 0a 2a 2a 20 74 61 62 6c 65 20  ces to.** table 
20160 63 6f 6c 75 6d 6e 73 20 61 6e 64 20 72 65 73 75  columns and resu
20161 6c 74 2d 73 65 74 20 63 6f 6c 75 6d 6e 73 2e 20  lt-set columns. 
20162 20 41 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d   At the same tim
20163 65 2c 20 64 6f 20 65 72 72 6f 72 0a 2a 2a 20 63  e, do error.** c
20164 68 65 63 6b 69 6e 67 20 6f 6e 20 66 75 6e 63 74  hecking on funct
20165 69 6f 6e 20 75 73 61 67 65 20 61 6e 64 20 73 65  ion usage and se
20166 74 20 61 20 66 6c 61 67 20 69 66 20 61 6e 79 20  t a flag if any 
20167 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
20168 6f 6e 73 0a 2a 2a 20 61 72 65 20 73 65 65 6e 2e  ons.** are seen.
20169 0a 2a 2a 0a 2a 2a 20 54 6f 20 72 65 73 6f 6c 76  .**.** To resolv
2016a 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 73 20  e table columns 
2016b 72 65 66 65 72 65 6e 63 65 73 20 77 65 20 6c 6f  references we lo
2016c 6f 6b 20 66 6f 72 20 6e 6f 64 65 73 20 28 6f 72  ok for nodes (or
2016d 20 73 75 62 74 72 65 65 73 29 20 6f 66 20 74 68   subtrees) of th
2016e 65 20 0a 2a 2a 20 66 6f 72 6d 20 58 2e 59 2e 5a  e .** form X.Y.Z
2016f 20 6f 72 20 59 2e 5a 20 6f 72 20 6a 75 73 74 20   or Y.Z or just 
20170 5a 20 77 68 65 72 65 0a 2a 2a 0a 2a 2a 20 20 20  Z where.**.**   
20171 20 20 20 58 3a 20 20 20 54 68 65 20 6e 61 6d 65     X:   The name
20172 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 2e 20   of a database. 
20173 20 45 78 3a 20 20 22 6d 61 69 6e 22 20 6f 72 20   Ex:  "main" or 
20174 22 74 65 6d 70 22 20 6f 72 0a 2a 2a 20 20 20 20  "temp" or.**    
20175 20 20 20 20 20 20 20 74 68 65 20 73 79 6d 62 6f         the symbo
20176 6c 69 63 20 6e 61 6d 65 20 61 73 73 69 67 6e 65  lic name assigne
20177 64 20 74 6f 20 61 6e 20 41 54 54 41 43 48 2d 65  d to an ATTACH-e
20178 64 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a  d database..**.*
20179 2a 20 20 20 20 20 20 59 3a 20 20 20 54 68 65 20  *      Y:   The 
2017a 6e 61 6d 65 20 6f 66 20 61 20 74 61 62 6c 65 20  name of a table 
2017b 69 6e 20 61 20 46 52 4f 4d 20 63 6c 61 75 73 65  in a FROM clause
2017c 2e 20 20 4f 72 20 69 6e 20 61 20 74 72 69 67 67  .  Or in a trigg
2017d 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  er.**           
2017e 6f 6e 65 20 6f 66 20 74 68 65 20 73 70 65 63 69  one of the speci
2017f 61 6c 20 6e 61 6d 65 73 20 22 6f 6c 64 22 20 6f  al names "old" o
20180 72 20 22 6e 65 77 22 2e 0a 2a 2a 0a 2a 2a 20 20  r "new"..**.**  
20181 20 20 20 20 5a 3a 20 20 20 54 68 65 20 6e 61 6d      Z:   The nam
20182 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 69 6e  e of a column in
20183 20 74 61 62 6c 65 20 59 2e 0a 2a 2a 0a 2a 2a 20   table Y..**.** 
20184 54 68 65 20 6e 6f 64 65 20 61 74 20 74 68 65 20  The node at the 
20185 72 6f 6f 74 20 6f 66 20 74 68 65 20 73 75 62 74  root of the subt
20186 72 65 65 20 69 73 20 6d 6f 64 69 66 69 65 64 20  ree is modified 
20187 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
20188 2a 20 20 20 20 45 78 70 72 2e 6f 70 20 20 20 20  *    Expr.op    
20189 20 20 20 20 43 68 61 6e 67 65 64 20 74 6f 20 54      Changed to T
2018a 4b 5f 43 4f 4c 55 4d 4e 0a 2a 2a 20 20 20 20 45  K_COLUMN.**    E
2018b 78 70 72 2e 70 54 61 62 20 20 20 20 20 20 50 6f  xpr.pTab      Po
2018c 69 6e 74 73 20 74 6f 20 74 68 65 20 54 61 62 6c  ints to the Tabl
2018d 65 20 6f 62 6a 65 63 74 20 66 6f 72 20 58 2e 59  e object for X.Y
2018e 0a 2a 2a 20 20 20 20 45 78 70 72 2e 69 43 6f 6c  .**    Expr.iCol
2018f 75 6d 6e 20 20 20 54 68 65 20 63 6f 6c 75 6d 6e  umn   The column
20190 20 69 6e 64 65 78 20 69 6e 20 58 2e 59 2e 20 20   index in X.Y.  
20191 2d 31 20 66 6f 72 20 74 68 65 20 72 6f 77 69 64  -1 for the rowid
20192 2e 0a 2a 2a 20 20 20 20 45 78 70 72 2e 69 54 61  ..**    Expr.iTa
20193 62 6c 65 20 20 20 20 54 68 65 20 56 44 42 45 20  ble    The VDBE 
20194 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f  cursor number fo
20195 72 20 58 2e 59 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  r X.Y.**.**.** T
20196 6f 20 72 65 73 6f 6c 76 65 20 72 65 73 75 6c 74  o resolve result
20197 2d 73 65 74 20 72 65 66 65 72 65 6e 63 65 73 2c  -set references,
20198 20 6c 6f 6f 6b 20 66 6f 72 20 65 78 70 72 65 73   look for expres
20199 73 69 6f 6e 20 6e 6f 64 65 73 20 6f 66 20 74 68  sion nodes of th
2019a 65 0a 2a 2a 20 66 6f 72 6d 20 5a 20 28 77 69 74  e.** form Z (wit
2019b 68 20 6e 6f 20 58 20 61 6e 64 20 59 20 70 72 65  h no X and Y pre
2019c 66 69 78 29 20 77 68 65 72 65 20 74 68 65 20 5a  fix) where the Z
2019d 20 6d 61 74 63 68 65 73 20 74 68 65 20 72 69 67   matches the rig
2019e 68 74 2d 68 61 6e 64 0a 2a 2a 20 73 69 7a 65 20  ht-hand.** size 
2019f 6f 66 20 61 6e 20 41 53 20 63 6c 61 75 73 65 20  of an AS clause 
201a0 69 6e 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65  in the result-se
201a1 74 20 6f 66 20 61 20 53 45 4c 45 43 54 2e 20 20  t of a SELECT.  
201a2 54 68 65 20 5a 20 65 78 70 72 65 73 73 69 6f 6e  The Z expression
201a3 0a 2a 2a 20 69 73 20 72 65 70 6c 61 63 65 64 20  .** is replaced 
201a4 62 79 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  by a copy of the
201a5 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20   left-hand side 
201a6 6f 66 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65  of the result-se
201a7 74 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a  t expression..**
201a8 20 54 61 62 6c 65 2d 6e 61 6d 65 20 61 6e 64 20   Table-name and 
201a9 66 75 6e 63 74 69 6f 6e 20 72 65 73 6f 6c 75 74  function resolut
201aa 69 6f 6e 20 6f 63 63 75 72 73 20 6f 6e 20 74 68  ion occurs on th
201ab 65 20 73 75 62 73 74 69 74 75 74 65 64 20 65 78  e substituted ex
201ac 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 65  pression.** tree
201ad 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  .  For example, 
201ae 69 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 53  in:.**.**      S
201af 45 4c 45 43 54 20 61 2b 62 20 41 53 20 78 2c 20  ELECT a+b AS x, 
201b0 63 2b 64 20 41 53 20 79 20 46 52 4f 4d 20 74 31  c+d AS y FROM t1
201b1 20 4f 52 44 45 52 20 42 59 20 78 3b 0a 2a 2a 0a   ORDER BY x;.**.
201b2 2a 2a 20 54 68 65 20 22 78 22 20 74 65 72 6d 20  ** The "x" term 
201b3 6f 66 20 74 68 65 20 6f 72 64 65 72 20 62 79 20  of the order by 
201b4 69 73 20 72 65 70 6c 61 63 65 64 20 62 79 20 22  is replaced by "
201b5 61 2b 62 22 20 74 6f 20 72 65 6e 64 65 72 3a 0a  a+b" to render:.
201b6 2a 2a 0a 2a 2a 20 20 20 20 20 20 53 45 4c 45 43  **.**      SELEC
201b7 54 20 61 2b 62 20 41 53 20 78 2c 20 63 2b 64 20  T a+b AS x, c+d 
201b8 41 53 20 79 20 46 52 4f 4d 20 74 31 20 4f 52 44  AS y FROM t1 ORD
201b9 45 52 20 42 59 20 61 2b 62 3b 0a 2a 2a 0a 2a 2a  ER BY a+b;.**.**
201ba 20 46 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 20   Function calls 
201bb 61 72 65 20 63 68 65 63 6b 65 64 20 74 6f 20 6d  are checked to m
201bc 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68  ake sure that th
201bd 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 0a 2a  e function is .*
201be 2a 20 64 65 66 69 6e 65 64 20 61 6e 64 20 74 68  * defined and th
201bf 61 74 20 74 68 65 20 63 6f 72 72 65 63 74 20 6e  at the correct n
201c0 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e  umber of argumen
201c1 74 73 20 61 72 65 20 73 70 65 63 69 66 69 65 64  ts are specified
201c2 2e 0a 2a 2a 20 49 66 20 74 68 65 20 66 75 6e 63  ..** If the func
201c3 74 69 6f 6e 20 69 73 20 61 6e 20 61 67 67 72 65  tion is an aggre
201c4 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 2c 20 74  gate function, t
201c5 68 65 6e 20 74 68 65 20 70 4e 43 2d 3e 68 61 73  hen the pNC->has
201c6 41 67 67 20 69 73 0a 2a 2a 20 73 65 74 20 61 6e  Agg is.** set an
201c7 64 20 74 68 65 20 6f 70 63 6f 64 65 20 69 73 20  d the opcode is 
201c8 63 68 61 6e 67 65 64 20 66 72 6f 6d 20 54 4b 5f  changed from TK_
201c9 46 55 4e 43 54 49 4f 4e 20 74 6f 20 54 4b 5f 41  FUNCTION to TK_A
201ca 47 47 5f 46 55 4e 43 54 49 4f 4e 2e 0a 2a 2a 20  GG_FUNCTION..** 
201cb 49 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  If an expression
201cc 20 63 6f 6e 74 61 69 6e 73 20 61 67 67 72 65 67   contains aggreg
201cd 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 74 68  ate functions th
201ce 65 6e 20 74 68 65 20 45 50 5f 41 67 67 0a 2a 2a  en the EP_Agg.**
201cf 20 70 72 6f 70 65 72 74 79 20 6f 6e 20 74 68 65   property on the
201d0 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 73   expression is s
201d1 65 74 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 72 72  et..**.** An err
201d2 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20 6c 65  or message is le
201d3 66 74 20 69 6e 20 70 50 61 72 73 65 20 69 66 20  ft in pParse if 
201d4 61 6e 79 74 68 69 6e 67 20 69 73 20 61 6d 69 73  anything is amis
201d5 73 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 0a 2a  s.  The number.*
201d6 2a 20 69 66 20 65 72 72 6f 72 73 20 69 73 20 72  * if errors is r
201d7 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49  eturned..*/.SQLI
201d8 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
201d9 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70  qlite3ResolveExp
201da 72 4e 61 6d 65 73 28 20 0a 20 20 4e 61 6d 65 43  rNames( .  NameC
201db 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20 20 20  ontext *pNC,    
201dc 20 20 20 2f 2a 20 4e 61 6d 65 73 70 61 63 65 20     /* Namespace 
201dd 74 6f 20 72 65 73 6f 6c 76 65 20 65 78 70 72 65  to resolve expre
201de 73 73 69 6f 6e 73 20 69 6e 2e 20 2a 2f 0a 20 20  ssions in. */.  
201df 45 78 70 72 20 2a 70 45 78 70 72 20 20 20 20 20  Expr *pExpr     
201e0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65          /* The e
201e1 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 20  xpression to be 
201e2 61 6e 61 6c 79 7a 65 64 2e 20 2a 2f 0a 29 7b 0a  analyzed. */.){.
201e3 20 20 69 6e 74 20 73 61 76 65 64 48 61 73 41 67    int savedHasAg
201e4 67 3b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a 0a  g;.  Walker w;..
201e5 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29    if( pExpr==0 )
201e6 20 72 65 74 75 72 6e 20 30 3b 0a 23 69 66 20 53   return 0;.#if S
201e7 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44  QLITE_MAX_EXPR_D
201e8 45 50 54 48 3e 30 0a 20 20 7b 0a 20 20 20 20 50  EPTH>0.  {.    P
201e9 61 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20 70  arse *pParse = p
201ea 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 20 20  NC->pParse;.    
201eb 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 43  if( sqlite3ExprC
201ec 68 65 63 6b 48 65 69 67 68 74 28 70 50 61 72 73  heckHeight(pPars
201ed 65 2c 20 70 45 78 70 72 2d 3e 6e 48 65 69 67 68  e, pExpr->nHeigh
201ee 74 2b 70 4e 43 2d 3e 70 50 61 72 73 65 2d 3e 6e  t+pNC->pParse->n
201ef 48 65 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20  Height) ){.     
201f0 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
201f1 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 48 65  .    pParse->nHe
201f2 69 67 68 74 20 2b 3d 20 70 45 78 70 72 2d 3e 6e  ight += pExpr->n
201f3 48 65 69 67 68 74 3b 0a 20 20 7d 0a 23 65 6e 64  Height;.  }.#end
201f4 69 66 0a 20 20 73 61 76 65 64 48 61 73 41 67 67  if.  savedHasAgg
201f5 20 3d 20 70 4e 43 2d 3e 68 61 73 41 67 67 3b 0a   = pNC->hasAgg;.
201f6 20 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 3d 20    pNC->hasAgg = 
201f7 30 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 6c  0;.  w.xExprCall
201f8 62 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65 45 78  back = resolveEx
201f9 70 72 53 74 65 70 3b 0a 20 20 77 2e 78 53 65 6c  prStep;.  w.xSel
201fa 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65  ectCallback = re
201fb 73 6f 6c 76 65 53 65 6c 65 63 74 53 74 65 70 3b  solveSelectStep;
201fc 0a 20 20 77 2e 70 50 61 72 73 65 20 3d 20 70 4e  .  w.pParse = pN
201fd 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 77 2e 75  C->pParse;.  w.u
201fe 2e 70 4e 43 20 3d 20 70 4e 43 3b 0a 20 20 73 71  .pNC = pNC;.  sq
201ff 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 26 77  lite3WalkExpr(&w
20200 2c 20 70 45 78 70 72 29 3b 0a 23 69 66 20 53 51  , pExpr);.#if SQ
20201 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45  LITE_MAX_EXPR_DE
20202 50 54 48 3e 30 0a 20 20 70 4e 43 2d 3e 70 50 61  PTH>0.  pNC->pPa
20203 72 73 65 2d 3e 6e 48 65 69 67 68 74 20 2d 3d 20  rse->nHeight -= 
20204 70 45 78 70 72 2d 3e 6e 48 65 69 67 68 74 3b 0a  pExpr->nHeight;.
20205 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70 4e 43  #endif.  if( pNC
20206 2d 3e 6e 45 72 72 3e 30 20 7c 7c 20 77 2e 70 50  ->nErr>0 || w.pP
20207 61 72 73 65 2d 3e 6e 45 72 72 3e 30 20 29 7b 0a  arse->nErr>0 ){.
20208 20 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65      ExprSetPrope
20209 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 45 72  rty(pExpr, EP_Er
2020a 72 6f 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  ror);.  }.  if( 
2020b 70 4e 43 2d 3e 68 61 73 41 67 67 20 29 7b 0a 20  pNC->hasAgg ){. 
2020c 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72     ExprSetProper
2020d 74 79 28 70 45 78 70 72 2c 20 45 50 5f 41 67 67  ty(pExpr, EP_Agg
2020e 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73  );.  }else if( s
2020f 61 76 65 64 48 61 73 41 67 67 20 29 7b 0a 20 20  avedHasAgg ){.  
20210 20 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 3d 20    pNC->hasAgg = 
20211 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  1;.  }.  return 
20212 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
20213 70 45 78 70 72 2c 20 45 50 5f 45 72 72 6f 72 29  pExpr, EP_Error)
20214 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f  ;.}.../*.** Reso
20215 6c 76 65 20 61 6c 6c 20 6e 61 6d 65 73 20 69 6e  lve all names in
20216 20 61 6c 6c 20 65 78 70 72 65 73 73 69 6f 6e 73   all expressions
20217 20 6f 66 20 61 20 53 45 4c 45 43 54 20 61 6e 64   of a SELECT and
20218 20 69 6e 20 61 6c 6c 0a 2a 2a 20 64 65 63 65 6e   in all.** decen
20219 64 65 6e 74 73 20 6f 66 20 74 68 65 20 53 45 4c  dents of the SEL
2021a 45 43 54 2c 20 69 6e 63 6c 75 64 69 6e 67 20 63  ECT, including c
2021b 6f 6d 70 6f 75 6e 64 73 20 6f 66 66 20 6f 66 20  ompounds off of 
2021c 70 2d 3e 70 50 72 69 6f 72 2c 0a 2a 2a 20 73 75  p->pPrior,.** su
2021d 62 71 75 65 72 69 65 73 20 69 6e 20 65 78 70 72  bqueries in expr
2021e 65 73 73 69 6f 6e 73 2c 20 61 6e 64 20 73 75 62  essions, and sub
2021f 71 75 65 72 69 65 73 20 75 73 65 64 20 61 73 20  queries used as 
20220 46 52 4f 4d 20 63 6c 61 75 73 65 0a 2a 2a 20 74  FROM clause.** t
20221 65 72 6d 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  erms..**.** See 
20222 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78  sqlite3ResolveEx
20223 70 72 4e 61 6d 65 73 28 29 20 66 6f 72 20 61 20  prNames() for a 
20224 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74  description of t
20225 68 65 20 6b 69 6e 64 73 20 6f 66 0a 2a 2a 20 74  he kinds of.** t
20226 72 61 6e 73 66 6f 72 6d 61 74 69 6f 6e 73 20 74  ransformations t
20227 68 61 74 20 6f 63 63 75 72 2e 0a 2a 2a 0a 2a 2a  hat occur..**.**
20228 20 41 6c 6c 20 53 45 4c 45 43 54 20 73 74 61 74   All SELECT stat
20229 65 6d 65 6e 74 73 20 73 68 6f 75 6c 64 20 68 61  ements should ha
2022a 76 65 20 62 65 65 6e 20 65 78 70 61 6e 64 65 64  ve been expanded
2022b 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65   using.** sqlite
2022c 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 29 20  3SelectExpand() 
2022d 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e  prior to invokin
2022e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a  g this routine..
2022f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
20230 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65  E void sqlite3Re
20231 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61 6d 65 73  solveSelectNames
20232 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
20233 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  e,         /* Th
20234 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74  e parser context
20235 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c   */.  Select *p,
20236 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
20237 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65  The SELECT state
20238 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f 64 65 64  ment being coded
20239 2e 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65  . */.  NameConte
2023a 78 74 20 2a 70 4f 75 74 65 72 4e 43 20 20 2f 2a  xt *pOuterNC  /*
2023b 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f   Name context fo
2023c 72 20 70 61 72 65 6e 74 20 53 45 4c 45 43 54 20  r parent SELECT 
2023d 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a  statement */.){.
2023e 20 20 57 61 6c 6b 65 72 20 77 3b 0a 0a 20 20 61    Walker w;..  a
2023f 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20  ssert( p!=0 );. 
20240 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b   w.xExprCallback
20241 20 3d 20 72 65 73 6f 6c 76 65 45 78 70 72 53 74   = resolveExprSt
20242 65 70 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 43  ep;.  w.xSelectC
20243 61 6c 6c 62 61 63 6b 20 3d 20 72 65 73 6f 6c 76  allback = resolv
20244 65 53 65 6c 65 63 74 53 74 65 70 3b 0a 20 20 77  eSelectStep;.  w
20245 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65  .pParse = pParse
20246 3b 0a 20 20 77 2e 75 2e 70 4e 43 20 3d 20 70 4f  ;.  w.u.pNC = pO
20247 75 74 65 72 4e 43 3b 0a 20 20 73 71 6c 69 74 65  uterNC;.  sqlite
20248 33 57 61 6c 6b 53 65 6c 65 63 74 28 26 77 2c 20  3WalkSelect(&w, 
20249 70 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  p);.}../********
2024a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 72 65  ****** End of re
2024b 73 6f 6c 76 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  solve.c ********
2024c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2024d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2024e 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
2024f 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
20250 65 20 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a  e expr.c *******
20251 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20252 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20253 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
20254 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a  1 September 15.*
20255 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
20256 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
20257 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
20258 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
20259 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
2025a 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
2025b 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
2025c 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
2025d 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
2025e 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
2025f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
20260 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
20261 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
20262 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
20263 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
20264 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
20265 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
20266 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
20267 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20268 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20269 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2026a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
2026b 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
2026c 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 75 73 65  ins routines use
2026d 64 20 66 6f 72 20 61 6e 61 6c 79 7a 69 6e 67 20  d for analyzing 
2026e 65 78 70 72 65 73 73 69 6f 6e 73 20 61 6e 64 0a  expressions and.
2026f 2a 2a 20 66 6f 72 20 67 65 6e 65 72 61 74 69 6e  ** for generatin
20270 67 20 56 44 42 45 20 63 6f 64 65 20 74 68 61 74  g VDBE code that
20271 20 65 76 61 6c 75 61 74 65 73 20 65 78 70 72 65   evaluates expre
20272 73 73 69 6f 6e 73 20 69 6e 20 53 51 4c 69 74 65  ssions in SQLite
20273 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ..*/../*.** Retu
20274 72 6e 20 74 68 65 20 27 61 66 66 69 6e 69 74 79  rn the 'affinity
20275 27 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73  ' of the express
20276 69 6f 6e 20 70 45 78 70 72 20 69 66 20 61 6e 79  ion pExpr if any
20277 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 45 78 70 72  ..**.** If pExpr
20278 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 61 20   is a column, a 
20279 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 63  reference to a c
2027a 6f 6c 75 6d 6e 20 76 69 61 20 61 6e 20 27 41 53  olumn via an 'AS
2027b 27 20 61 6c 69 61 73 2c 0a 2a 2a 20 6f 72 20 61  ' alias,.** or a
2027c 20 73 75 62 2d 73 65 6c 65 63 74 20 77 69 74 68   sub-select with
2027d 20 61 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68 65   a column as the
2027e 20 72 65 74 75 72 6e 20 76 61 6c 75 65 2c 20 74   return value, t
2027f 68 65 6e 20 74 68 65 20 0a 2a 2a 20 61 66 66 69  hen the .** affi
20280 6e 69 74 79 20 6f 66 20 74 68 61 74 20 63 6f 6c  nity of that col
20281 75 6d 6e 20 69 73 20 72 65 74 75 72 6e 65 64 2e  umn is returned.
20282 20 4f 74 68 65 72 77 69 73 65 2c 20 30 78 30 30   Otherwise, 0x00
20283 20 69 73 20 72 65 74 75 72 6e 65 64 2c 0a 2a 2a   is returned,.**
20284 20 69 6e 64 69 63 61 74 69 6e 67 20 6e 6f 20 61   indicating no a
20285 66 66 69 6e 69 74 79 20 66 6f 72 20 74 68 65 20  ffinity for the 
20286 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a  expression..**.*
20287 2a 20 69 2e 65 2e 20 74 68 65 20 57 48 45 52 45  * i.e. the WHERE
20288 20 63 6c 61 75 73 65 20 65 78 70 72 65 73 73 73   clause expresss
20289 69 6f 6e 73 20 69 6e 20 74 68 65 20 66 6f 6c 6c  ions in the foll
2028a 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 73  owing statements
2028b 20 61 6c 6c 0a 2a 2a 20 68 61 76 65 20 61 6e 20   all.** have an 
2028c 61 66 66 69 6e 69 74 79 3a 0a 2a 2a 0a 2a 2a 20  affinity:.**.** 
2028d 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28  CREATE TABLE t1(
2028e 61 29 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 2a 20  a);.** SELECT * 
2028f 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 61 3b  FROM t1 WHERE a;
20290 0a 2a 2a 20 53 45 4c 45 43 54 20 61 20 41 53 20  .** SELECT a AS 
20291 62 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20  b FROM t1 WHERE 
20292 62 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 2a 20 46  b;.** SELECT * F
20293 52 4f 4d 20 74 31 20 57 48 45 52 45 20 28 73 65  ROM t1 WHERE (se
20294 6c 65 63 74 20 61 20 66 72 6f 6d 20 74 31 29 3b  lect a from t1);
20295 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
20296 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 45  TE char sqlite3E
20297 78 70 72 41 66 66 69 6e 69 74 79 28 45 78 70 72  xprAffinity(Expr
20298 20 2a 70 45 78 70 72 29 7b 0a 20 20 69 6e 74 20   *pExpr){.  int 
20299 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a  op = pExpr->op;.
2029a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 53 45 4c    if( op==TK_SEL
2029b 45 43 54 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ECT ){.    asser
2029c 74 28 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 26  t( pExpr->flags&
2029d 45 50 5f 78 49 73 53 65 6c 65 63 74 20 29 3b 0a  EP_xIsSelect );.
2029e 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74      return sqlit
2029f 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 70  e3ExprAffinity(p
202a0 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 2d  Expr->x.pSelect-
202a1 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45  >pEList->a[0].pE
202a2 78 70 72 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65  xpr);.  }.#ifnde
202a3 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41  f SQLITE_OMIT_CA
202a4 53 54 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f  ST.  if( op==TK_
202a5 43 41 53 54 20 29 7b 0a 20 20 20 20 61 73 73 65  CAST ){.    asse
202a6 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70  rt( !ExprHasProp
202a7 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49  erty(pExpr, EP_I
202a8 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20  ntValue) );.    
202a9 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41 66  return sqlite3Af
202aa 66 69 6e 69 74 79 54 79 70 65 28 70 45 78 70 72  finityType(pExpr
202ab 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 7d  ->u.zToken);.  }
202ac 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 28 6f  .#endif.  if( (o
202ad 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e  p==TK_AGG_COLUMN
202ae 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d   || op==TK_COLUM
202af 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 52 45 47 49  N || op==TK_REGI
202b0 53 54 45 52 29 20 0a 20 20 20 26 26 20 70 45 78  STER) .   && pEx
202b1 70 72 2d 3e 70 54 61 62 21 3d 30 0a 20 20 29 7b  pr->pTab!=0.  ){
202b2 0a 20 20 20 20 2f 2a 20 6f 70 3d 3d 54 4b 5f 52  .    /* op==TK_R
202b3 45 47 49 53 54 45 52 20 26 26 20 70 45 78 70 72  EGISTER && pExpr
202b4 2d 3e 70 54 61 62 21 3d 30 20 68 61 70 70 65 6e  ->pTab!=0 happen
202b5 73 20 77 68 65 6e 20 70 45 78 70 72 20 77 61 73  s when pExpr was
202b6 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20 20 20   originally.    
202b7 2a 2a 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 62  ** a TK_COLUMN b
202b8 75 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c  ut was previousl
202b9 79 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20  y evaluated and 
202ba 63 61 63 68 65 64 20 69 6e 20 61 20 72 65 67 69  cached in a regi
202bb 73 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ster */.    int 
202bc 6a 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75  j = pExpr->iColu
202bd 6d 6e 3b 0a 20 20 20 20 69 66 28 20 6a 3c 30 20  mn;.    if( j<0 
202be 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
202bf 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20  AFF_INTEGER;.   
202c0 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
202c1 70 54 61 62 20 26 26 20 6a 3c 70 45 78 70 72 2d  pTab && j<pExpr-
202c2 3e 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20  >pTab->nCol );. 
202c3 20 20 20 72 65 74 75 72 6e 20 70 45 78 70 72 2d     return pExpr-
202c4 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 61  >pTab->aCol[j].a
202c5 66 66 69 6e 69 74 79 3b 0a 20 20 7d 0a 20 20 72  ffinity;.  }.  r
202c6 65 74 75 72 6e 20 70 45 78 70 72 2d 3e 61 66 66  eturn pExpr->aff
202c7 69 6e 69 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  inity;.}../*.** 
202c8 53 65 74 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e  Set the collatin
202c9 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 65  g sequence for e
202ca 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20  xpression pExpr 
202cb 74 6f 20 62 65 20 74 68 65 20 63 6f 6c 6c 61 74  to be the collat
202cc 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20  ing.** sequence 
202cd 6e 61 6d 65 64 20 62 79 20 70 54 6f 6b 65 6e 2e  named by pToken.
202ce 20 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e     Return a poin
202cf 74 65 72 20 74 6f 20 74 68 65 20 72 65 76 69 73  ter to the revis
202d0 65 64 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a  ed expression..*
202d1 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20  * The collating 
202d2 73 65 71 75 65 6e 63 65 20 69 73 20 6d 61 72 6b  sequence is mark
202d3 65 64 20 61 73 20 22 65 78 70 6c 69 63 69 74 22  ed as "explicit"
202d4 20 75 73 69 6e 67 20 74 68 65 20 45 50 5f 45 78   using the EP_Ex
202d5 70 43 6f 6c 6c 61 74 65 0a 2a 2a 20 66 6c 61 67  pCollate.** flag
202d6 2e 20 20 41 6e 20 65 78 70 6c 69 63 69 74 20 63  .  An explicit c
202d7 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
202d8 65 20 77 69 6c 6c 20 6f 76 65 72 72 69 64 65 20  e will override 
202d9 69 6d 70 6c 69 63 69 74 0a 2a 2a 20 63 6f 6c 6c  implicit.** coll
202da 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 2e  ating sequences.
202db 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
202dc 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33  TE Expr *sqlite3
202dd 45 78 70 72 53 65 74 43 6f 6c 6c 28 50 61 72 73  ExprSetColl(Pars
202de 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20  e *pParse, Expr 
202df 2a 70 45 78 70 72 2c 20 54 6f 6b 65 6e 20 2a 70  *pExpr, Token *p
202e0 43 6f 6c 6c 4e 61 6d 65 29 7b 0a 20 20 63 68 61  CollName){.  cha
202e1 72 20 2a 7a 43 6f 6c 6c 20 3d 20 30 3b 20 20 20  r *zColl = 0;   
202e2 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 71 75           /* Dequ
202e3 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 63 6f 6c  oted name of col
202e4 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
202e5 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43  */.  CollSeq *pC
202e6 6f 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  oll;.  sqlite3 *
202e7 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
202e8 0a 20 20 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74  .  zColl = sqlit
202e9 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
202ea 64 62 2c 20 70 43 6f 6c 6c 4e 61 6d 65 29 3b 0a  db, pCollName);.
202eb 20 20 69 66 28 20 70 45 78 70 72 20 26 26 20 7a    if( pExpr && z
202ec 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 70 43 6f 6c  Coll ){.    pCol
202ed 6c 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74  l = sqlite3Locat
202ee 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  eCollSeq(pParse,
202ef 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 69 66 28   zColl);.    if(
202f0 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20   pColl ){.      
202f1 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 20 3d 20 70  pExpr->pColl = p
202f2 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 70 45 78 70  Coll;.      pExp
202f3 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 45  r->flags |= EP_E
202f4 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 20 20 7d  xpCollate;.    }
202f5 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62  .  }.  sqlite3Db
202f6 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c 6c 29 3b  Free(db, zColl);
202f7 0a 20 20 72 65 74 75 72 6e 20 70 45 78 70 72 3b  .  return pExpr;
202f8 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
202f9 20 74 68 65 20 64 65 66 61 75 6c 74 20 63 6f 6c   the default col
202fa 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
202fb 66 6f 72 20 74 68 65 20 65 78 70 72 65 73 73 69  for the expressi
202fc 6f 6e 20 70 45 78 70 72 2e 20 49 66 0a 2a 2a 20  on pExpr. If.** 
202fd 74 68 65 72 65 20 69 73 20 6e 6f 20 64 65 66 61  there is no defa
202fe 75 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e 20 74 79  ult collation ty
202ff 70 65 2c 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f  pe, return 0..*/
20300 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
20301 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33  CollSeq *sqlite3
20302 45 78 70 72 43 6f 6c 6c 53 65 71 28 50 61 72 73  ExprCollSeq(Pars
20303 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20  e *pParse, Expr 
20304 2a 70 45 78 70 72 29 7b 0a 20 20 43 6f 6c 6c 53  *pExpr){.  CollS
20305 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20  eq *pColl = 0;. 
20306 20 45 78 70 72 20 2a 70 20 3d 20 70 45 78 70 72   Expr *p = pExpr
20307 3b 0a 20 20 77 68 69 6c 65 28 20 41 4c 57 41 59  ;.  while( ALWAY
20308 53 28 70 29 20 29 7b 0a 20 20 20 20 69 6e 74 20  S(p) ){.    int 
20309 6f 70 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20  op;.    pColl = 
2030a 70 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 69 66  p->pColl;.    if
2030b 28 20 70 43 6f 6c 6c 20 29 20 62 72 65 61 6b 3b  ( pColl ) break;
2030c 0a 20 20 20 20 6f 70 20 3d 20 70 2d 3e 6f 70 3b  .    op = p->op;
2030d 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 54 61 62  .    if( p->pTab
2030e 21 3d 30 20 26 26 20 28 0a 20 20 20 20 20 20 20  !=0 && (.       
2030f 20 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55   op==TK_AGG_COLU
20310 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 43 4f 4c  MN || op==TK_COL
20311 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 52 45  UMN || op==TK_RE
20312 47 49 53 54 45 52 20 7c 7c 20 6f 70 3d 3d 54 4b  GISTER || op==TK
20313 5f 54 52 49 47 47 45 52 0a 20 20 20 20 29 29 7b  _TRIGGER.    )){
20314 0a 20 20 20 20 20 20 2f 2a 20 6f 70 3d 3d 54 4b  .      /* op==TK
20315 5f 52 45 47 49 53 54 45 52 20 26 26 20 70 2d 3e  _REGISTER && p->
20316 70 54 61 62 21 3d 30 20 68 61 70 70 65 6e 73 20  pTab!=0 happens 
20317 77 68 65 6e 20 70 45 78 70 72 20 77 61 73 20 6f  when pExpr was o
20318 72 69 67 69 6e 61 6c 6c 79 0a 20 20 20 20 20 20  riginally.      
20319 2a 2a 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 62  ** a TK_COLUMN b
2031a 75 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c  ut was previousl
2031b 79 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20  y evaluated and 
2031c 63 61 63 68 65 64 20 69 6e 20 61 20 72 65 67 69  cached in a regi
2031d 73 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 63 6f  ster */.      co
2031e 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b  nst char *zColl;
2031f 0a 20 20 20 20 20 20 69 6e 74 20 6a 20 3d 20 70  .      int j = p
20320 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20  ->iColumn;.     
20321 20 69 66 28 20 6a 3e 3d 30 20 29 7b 0a 20 20 20   if( j>=0 ){.   
20322 20 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62       sqlite3 *db
20323 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
20324 20 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 70         zColl = p
20325 2d 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e  ->pTab->aCol[j].
20326 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 70  zColl;.        p
20327 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69  Coll = sqlite3Fi
20328 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e  ndCollSeq(db, EN
20329 43 28 64 62 29 2c 20 7a 43 6f 6c 6c 2c 20 30 29  C(db), zColl, 0)
2032a 3b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d  ;.        pExpr-
2032b 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a  >pColl = pColl;.
2032c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
2032d 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  eak;.    }.    i
2032e 66 28 20 6f 70 21 3d 54 4b 5f 43 41 53 54 20 26  f( op!=TK_CAST &
2032f 26 20 6f 70 21 3d 54 4b 5f 55 50 4c 55 53 20 29  & op!=TK_UPLUS )
20330 7b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  {.      break;. 
20331 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d 3e     }.    p = p->
20332 70 4c 65 66 74 3b 0a 20 20 7d 0a 20 20 69 66 28  pLeft;.  }.  if(
20333 20 73 71 6c 69 74 65 33 43 68 65 63 6b 43 6f 6c   sqlite3CheckCol
20334 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 43 6f  lSeq(pParse, pCo
20335 6c 6c 29 20 29 7b 20 0a 20 20 20 20 70 43 6f 6c  ll) ){ .    pCol
20336 6c 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74  l = 0;.  }.  ret
20337 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a  urn pColl;.}../*
20338 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61 6e 20  .** pExpr is an 
20339 6f 70 65 72 61 6e 64 20 6f 66 20 61 20 63 6f 6d  operand of a com
2033a 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72  parison operator
2033b 2e 20 20 61 66 66 32 20 69 73 20 74 68 65 0a 2a  .  aff2 is the.*
2033c 2a 20 74 79 70 65 20 61 66 66 69 6e 69 74 79 20  * type affinity 
2033d 6f 66 20 74 68 65 20 6f 74 68 65 72 20 6f 70 65  of the other ope
2033e 72 61 6e 64 2e 20 20 54 68 69 73 20 72 6f 75 74  rand.  This rout
2033f 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 0a  ine returns the.
20340 2a 2a 20 74 79 70 65 20 61 66 66 69 6e 69 74 79  ** type affinity
20341 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20   that should be 
20342 75 73 65 64 20 66 6f 72 20 74 68 65 20 63 6f 6d  used for the com
20343 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72  parison operator
20344 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
20345 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33  ATE char sqlite3
20346 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28  CompareAffinity(
20347 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68 61  Expr *pExpr, cha
20348 72 20 61 66 66 32 29 7b 0a 20 20 63 68 61 72 20  r aff2){.  char 
20349 61 66 66 31 20 3d 20 73 71 6c 69 74 65 33 45 78  aff1 = sqlite3Ex
2034a 70 72 41 66 66 69 6e 69 74 79 28 70 45 78 70 72  prAffinity(pExpr
2034b 29 3b 0a 20 20 69 66 28 20 61 66 66 31 20 26 26  );.  if( aff1 &&
2034c 20 61 66 66 32 20 29 7b 0a 20 20 20 20 2f 2a 20   aff2 ){.    /* 
2034d 42 6f 74 68 20 73 69 64 65 73 20 6f 66 20 74 68  Both sides of th
2034e 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 61 72 65  e comparison are
2034f 20 63 6f 6c 75 6d 6e 73 2e 20 49 66 20 6f 6e 65   columns. If one
20350 20 68 61 73 20 6e 75 6d 65 72 69 63 0a 20 20 20   has numeric.   
20351 20 2a 2a 20 61 66 66 69 6e 69 74 79 2c 20 75 73   ** affinity, us
20352 65 20 74 68 61 74 2e 20 4f 74 68 65 72 77 69 73  e that. Otherwis
20353 65 20 75 73 65 20 6e 6f 20 61 66 66 69 6e 69 74  e use no affinit
20354 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  y..    */.    if
20355 28 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 65 72  ( sqlite3IsNumer
20356 69 63 41 66 66 69 6e 69 74 79 28 61 66 66 31 29  icAffinity(aff1)
20357 20 7c 7c 20 73 71 6c 69 74 65 33 49 73 4e 75 6d   || sqlite3IsNum
20358 65 72 69 63 41 66 66 69 6e 69 74 79 28 61 66 66  ericAffinity(aff
20359 32 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  2) ){.      retu
2035a 72 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55  rn SQLITE_AFF_NU
2035b 4d 45 52 49 43 3b 0a 20 20 20 20 7d 65 6c 73 65  MERIC;.    }else
2035c 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
2035d 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a  QLITE_AFF_NONE;.
2035e 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66      }.  }else if
2035f 28 20 21 61 66 66 31 20 26 26 20 21 61 66 66 32  ( !aff1 && !aff2
20360 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 65 69 74 68   ){.    /* Neith
20361 65 72 20 73 69 64 65 20 6f 66 20 74 68 65 20 63  er side of the c
20362 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 20 63  omparison is a c
20363 6f 6c 75 6d 6e 2e 20 20 43 6f 6d 70 61 72 65 20  olumn.  Compare 
20364 74 68 65 0a 20 20 20 20 2a 2a 20 72 65 73 75 6c  the.    ** resul
20365 74 73 20 64 69 72 65 63 74 6c 79 2e 0a 20 20 20  ts directly..   
20366 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53   */.    return S
20367 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a  QLITE_AFF_NONE;.
20368 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
20369 4f 6e 65 20 73 69 64 65 20 69 73 20 61 20 63 6f  One side is a co
2036a 6c 75 6d 6e 2c 20 74 68 65 20 6f 74 68 65 72 20  lumn, the other 
2036b 69 73 20 6e 6f 74 2e 20 55 73 65 20 74 68 65 20  is not. Use the 
2036c 63 6f 6c 75 6d 6e 73 20 61 66 66 69 6e 69 74 79  columns affinity
2036d 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28  . */.    assert(
2036e 20 61 66 66 31 3d 3d 30 20 7c 7c 20 61 66 66 32   aff1==0 || aff2
2036f 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65 74 75 72  ==0 );.    retur
20370 6e 20 28 61 66 66 31 20 2b 20 61 66 66 32 29 3b  n (aff1 + aff2);
20371 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45  .  }.}../*.** pE
20372 78 70 72 20 69 73 20 61 20 63 6f 6d 70 61 72 69  xpr is a compari
20373 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 20 20 52  son operator.  R
20374 65 74 75 72 6e 20 74 68 65 20 74 79 70 65 20 61  eturn the type a
20375 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 68 6f  ffinity that sho
20376 75 6c 64 0a 2a 2a 20 62 65 20 61 70 70 6c 69 65  uld.** be applie
20377 64 20 74 6f 20 62 6f 74 68 20 6f 70 65 72 61 6e  d to both operan
20378 64 73 20 70 72 69 6f 72 20 74 6f 20 64 6f 69 6e  ds prior to doin
20379 67 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e  g the comparison
2037a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72  ..*/.static char
2037b 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e   comparisonAffin
2037c 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72 29  ity(Expr *pExpr)
2037d 7b 0a 20 20 63 68 61 72 20 61 66 66 3b 0a 20 20  {.  char aff;.  
2037e 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f  assert( pExpr->o
2037f 70 3d 3d 54 4b 5f 45 51 20 7c 7c 20 70 45 78 70  p==TK_EQ || pExp
20380 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20 7c 7c 20  r->op==TK_IN || 
20381 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4c 54  pExpr->op==TK_LT
20382 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 70 45   ||.          pE
20383 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 47 54 20 7c  xpr->op==TK_GT |
20384 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  | pExpr->op==TK_
20385 47 45 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d  GE || pExpr->op=
20386 3d 54 4b 5f 4c 45 20 7c 7c 0a 20 20 20 20 20 20  =TK_LE ||.      
20387 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54      pExpr->op==T
20388 4b 5f 4e 45 20 29 3b 0a 20 20 61 73 73 65 72 74  K_NE );.  assert
20389 28 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 29  ( pExpr->pLeft )
2038a 3b 0a 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65  ;.  aff = sqlite
2038b 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 45  3ExprAffinity(pE
2038c 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 69  xpr->pLeft);.  i
2038d 66 28 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  f( pExpr->pRight
2038e 20 29 7b 0a 20 20 20 20 61 66 66 20 3d 20 73 71   ){.    aff = sq
2038f 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69  lite3CompareAffi
20390 6e 69 74 79 28 70 45 78 70 72 2d 3e 70 52 69 67  nity(pExpr->pRig
20391 68 74 2c 20 61 66 66 29 3b 0a 20 20 7d 65 6c 73  ht, aff);.  }els
20392 65 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f  e if( ExprHasPro
20393 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
20394 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20  xIsSelect) ){.  
20395 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33 43    aff = sqlite3C
20396 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 70  ompareAffinity(p
20397 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 2d  Expr->x.pSelect-
20398 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45  >pEList->a[0].pE
20399 78 70 72 2c 20 61 66 66 29 3b 0a 20 20 7d 65 6c  xpr, aff);.  }el
2039a 73 65 20 69 66 28 20 21 61 66 66 20 29 7b 0a 20  se if( !aff ){. 
2039b 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f     aff = SQLITE_
2039c 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 7d 0a 20 20  AFF_NONE;.  }.  
2039d 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a 2f  return aff;.}../
2039e 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61 20  *.** pExpr is a 
2039f 63 6f 6d 70 61 72 69 73 6f 6e 20 65 78 70 72 65  comparison expre
203a0 73 73 69 6f 6e 2c 20 65 67 2e 20 27 3d 27 2c 20  ssion, eg. '=', 
203a1 27 3c 27 2c 20 49 4e 28 2e 2e 2e 29 20 65 74 63  '<', IN(...) etc
203a2 2e 0a 2a 2a 20 69 64 78 5f 61 66 66 69 6e 69 74  ..** idx_affinit
203a3 79 20 69 73 20 74 68 65 20 61 66 66 69 6e 69 74  y is the affinit
203a4 79 20 6f 66 20 61 6e 20 69 6e 64 65 78 65 64 20  y of an indexed 
203a5 63 6f 6c 75 6d 6e 2e 20 52 65 74 75 72 6e 20 74  column. Return t
203a6 72 75 65 0a 2a 2a 20 69 66 20 74 68 65 20 69 6e  rue.** if the in
203a7 64 65 78 20 77 69 74 68 20 61 66 66 69 6e 69 74  dex with affinit
203a8 79 20 69 64 78 5f 61 66 66 69 6e 69 74 79 20 6d  y idx_affinity m
203a9 61 79 20 62 65 20 75 73 65 64 20 74 6f 20 69 6d  ay be used to im
203aa 70 6c 65 6d 65 6e 74 0a 2a 2a 20 74 68 65 20 63  plement.** the c
203ab 6f 6d 70 61 72 69 73 6f 6e 20 69 6e 20 70 45 78  omparison in pEx
203ac 70 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  pr..*/.SQLITE_PR
203ad 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
203ae 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 4f 6b  3IndexAffinityOk
203af 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68  (Expr *pExpr, ch
203b0 61 72 20 69 64 78 5f 61 66 66 69 6e 69 74 79 29  ar idx_affinity)
203b1 7b 0a 20 20 63 68 61 72 20 61 66 66 20 3d 20 63  {.  char aff = c
203b2 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e 69 74  omparisonAffinit
203b3 79 28 70 45 78 70 72 29 3b 0a 20 20 73 77 69 74  y(pExpr);.  swit
203b4 63 68 28 20 61 66 66 20 29 7b 0a 20 20 20 20 63  ch( aff ){.    c
203b5 61 73 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  ase SQLITE_AFF_N
203b6 4f 4e 45 3a 0a 20 20 20 20 20 20 72 65 74 75 72  ONE:.      retur
203b7 6e 20 31 3b 0a 20 20 20 20 63 61 73 65 20 53 51  n 1;.    case SQ
203b8 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3a 0a 20  LITE_AFF_TEXT:. 
203b9 20 20 20 20 20 72 65 74 75 72 6e 20 69 64 78 5f       return idx_
203ba 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45  affinity==SQLITE
203bb 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20 20 20 64  _AFF_TEXT;.    d
203bc 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 72 65  efault:.      re
203bd 74 75 72 6e 20 73 71 6c 69 74 65 33 49 73 4e 75  turn sqlite3IsNu
203be 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 69 64  mericAffinity(id
203bf 78 5f 61 66 66 69 6e 69 74 79 29 3b 0a 20 20 7d  x_affinity);.  }
203c0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
203c1 20 74 68 65 20 50 35 20 76 61 6c 75 65 20 74 68   the P5 value th
203c2 61 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65  at should be use
203c3 64 20 66 6f 72 20 61 20 62 69 6e 61 72 79 20 63  d for a binary c
203c4 6f 6d 70 61 72 69 73 6f 6e 0a 2a 2a 20 6f 70 63  omparison.** opc
203c5 6f 64 65 20 28 4f 50 5f 45 71 2c 20 4f 50 5f 47  ode (OP_Eq, OP_G
203c6 65 20 65 74 63 2e 29 20 75 73 65 64 20 74 6f 20  e etc.) used to 
203c7 63 6f 6d 70 61 72 65 20 70 45 78 70 72 31 20 61  compare pExpr1 a
203c8 6e 64 20 70 45 78 70 72 32 2e 0a 2a 2f 0a 73 74  nd pExpr2..*/.st
203c9 61 74 69 63 20 75 38 20 62 69 6e 61 72 79 43 6f  atic u8 binaryCo
203ca 6d 70 61 72 65 50 35 28 45 78 70 72 20 2a 70 45  mpareP5(Expr *pE
203cb 78 70 72 31 2c 20 45 78 70 72 20 2a 70 45 78 70  xpr1, Expr *pExp
203cc 72 32 2c 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75  r2, int jumpIfNu
203cd 6c 6c 29 7b 0a 20 20 75 38 20 61 66 66 20 3d 20  ll){.  u8 aff = 
203ce 28 63 68 61 72 29 73 71 6c 69 74 65 33 45 78 70  (char)sqlite3Exp
203cf 72 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 32  rAffinity(pExpr2
203d0 29 3b 0a 20 20 61 66 66 20 3d 20 28 75 38 29 73  );.  aff = (u8)s
203d1 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66  qlite3CompareAff
203d2 69 6e 69 74 79 28 70 45 78 70 72 31 2c 20 61 66  inity(pExpr1, af
203d3 66 29 20 7c 20 28 75 38 29 6a 75 6d 70 49 66 4e  f) | (u8)jumpIfN
203d4 75 6c 6c 3b 0a 20 20 72 65 74 75 72 6e 20 61 66  ull;.  return af
203d5 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  f;.}../*.** Retu
203d6 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
203d7 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  the collation se
203d8 71 75 65 6e 63 65 20 74 68 61 74 20 73 68 6f 75  quence that shou
203d9 6c 64 20 62 65 20 75 73 65 64 20 62 79 0a 2a 2a  ld be used by.**
203da 20 61 20 62 69 6e 61 72 79 20 63 6f 6d 70 61 72   a binary compar
203db 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 20 63 6f  ison operator co
203dc 6d 70 61 72 69 6e 67 20 70 4c 65 66 74 20 61 6e  mparing pLeft an
203dd 64 20 70 52 69 67 68 74 2e 0a 2a 2a 0a 2a 2a 20  d pRight..**.** 
203de 49 66 20 74 68 65 20 6c 65 66 74 20 68 61 6e 64  If the left hand
203df 20 65 78 70 72 65 73 73 69 6f 6e 20 68 61 73 20   expression has 
203e0 61 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  a collating sequ
203e1 65 6e 63 65 20 74 79 70 65 2c 20 74 68 65 6e 20  ence type, then 
203e2 69 74 20 69 73 0a 2a 2a 20 75 73 65 64 2e 20 4f  it is.** used. O
203e3 74 68 65 72 77 69 73 65 20 74 68 65 20 63 6f 6c  therwise the col
203e4 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
203e5 66 6f 72 20 74 68 65 20 72 69 67 68 74 20 68 61  for the right ha
203e6 6e 64 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a  nd expression.**
203e7 20 69 73 20 75 73 65 64 2c 20 6f 72 20 74 68 65   is used, or the
203e8 20 64 65 66 61 75 6c 74 20 28 42 49 4e 41 52 59   default (BINARY
203e9 29 20 69 66 20 6e 65 69 74 68 65 72 20 65 78 70  ) if neither exp
203ea 72 65 73 73 69 6f 6e 20 68 61 73 20 61 20 63 6f  ression has a co
203eb 6c 6c 61 74 69 6e 67 0a 2a 2a 20 74 79 70 65 2e  llating.** type.
203ec 0a 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20  .**.** Argument 
203ed 70 52 69 67 68 74 20 28 62 75 74 20 6e 6f 74 20  pRight (but not 
203ee 70 4c 65 66 74 29 20 6d 61 79 20 62 65 20 61 20  pLeft) may be a 
203ef 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 2e 20 49 6e  null pointer. In
203f0 20 74 68 69 73 20 63 61 73 65 2c 0a 2a 2a 20 69   this case,.** i
203f1 74 20 69 73 20 6e 6f 74 20 63 6f 6e 73 69 64 65  t is not conside
203f2 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  red..*/.SQLITE_P
203f3 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a  RIVATE CollSeq *
203f4 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d  sqlite3BinaryCom
203f5 70 61 72 65 43 6f 6c 6c 53 65 71 28 0a 20 20 50  pareCollSeq(.  P
203f6 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 0a 20  arse *pParse, . 
203f7 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 0a 20   Expr *pLeft, . 
203f8 20 45 78 70 72 20 2a 70 52 69 67 68 74 0a 29 7b   Expr *pRight.){
203f9 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c  .  CollSeq *pCol
203fa 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 65  l;.  assert( pLe
203fb 66 74 20 29 3b 0a 20 20 69 66 28 20 70 4c 65 66  ft );.  if( pLef
203fc 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78  t->flags & EP_Ex
203fd 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20  pCollate ){.    
203fe 61 73 73 65 72 74 28 20 70 4c 65 66 74 2d 3e 70  assert( pLeft->p
203ff 43 6f 6c 6c 20 29 3b 0a 20 20 20 20 70 43 6f 6c  Coll );.    pCol
20400 6c 20 3d 20 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c  l = pLeft->pColl
20401 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 52  ;.  }else if( pR
20402 69 67 68 74 20 26 26 20 70 52 69 67 68 74 2d 3e  ight && pRight->
20403 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f  flags & EP_ExpCo
20404 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 61 73 73  llate ){.    ass
20405 65 72 74 28 20 70 52 69 67 68 74 2d 3e 70 43 6f  ert( pRight->pCo
20406 6c 6c 20 29 3b 0a 20 20 20 20 70 43 6f 6c 6c 20  ll );.    pColl 
20407 3d 20 70 52 69 67 68 74 2d 3e 70 43 6f 6c 6c 3b  = pRight->pColl;
20408 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43  .  }else{.    pC
20409 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70  oll = sqlite3Exp
2040a 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  rCollSeq(pParse,
2040b 20 70 4c 65 66 74 29 3b 0a 20 20 20 20 69 66 28   pLeft);.    if(
2040c 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20   !pColl ){.     
2040d 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33   pColl = sqlite3
2040e 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72  ExprCollSeq(pPar
2040f 73 65 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 20  se, pRight);.   
20410 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
20411 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  pColl;.}../*.** 
20412 47 65 6e 65 72 61 74 65 20 74 68 65 20 6f 70 65  Generate the ope
20413 72 61 6e 64 73 20 66 6f 72 20 61 20 63 6f 6d 70  rands for a comp
20414 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 69 6f 6e  arison operation
20415 2e 20 20 42 65 66 6f 72 65 0a 2a 2a 20 67 65 6e  .  Before.** gen
20416 65 72 61 74 69 6e 67 20 74 68 65 20 63 6f 64 65  erating the code
20417 20 66 6f 72 20 65 61 63 68 20 6f 70 65 72 61 6e   for each operan
20418 64 2c 20 73 65 74 20 74 68 65 20 45 50 5f 41 6e  d, set the EP_An
20419 79 41 66 66 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20  yAff.** flag on 
2041a 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 73  the expression s
2041b 6f 20 74 68 61 74 20 69 74 20 77 69 6c 6c 20 62  o that it will b
2041c 65 20 61 62 6c 65 20 74 6f 20 75 73 65 64 20 61  e able to used a
2041d 0a 2a 2a 20 63 61 63 68 65 64 20 63 6f 6c 75 6d  .** cached colum
2041e 6e 20 76 61 6c 75 65 20 74 68 61 74 20 68 61 73  n value that has
2041f 20 70 72 65 76 69 6f 75 73 6c 79 20 75 6e 64 65   previously unde
20420 72 67 6f 6e 65 20 61 6e 0a 2a 2a 20 61 66 66 69  rgone an.** affi
20421 6e 69 74 79 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a  nity change..*/.
20422 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65  static void code
20423 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64 73 28  CompareOperands(
20424 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
20425 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20  ,    /* Parsing 
20426 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74  and code generat
20427 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
20428 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 20 20   Expr *pLeft,   
20429 20 20 20 2f 2a 20 54 68 65 20 6c 65 66 74 20 6f     /* The left o
2042a 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20  perand */.  int 
2042b 2a 70 52 65 67 4c 65 66 74 2c 20 20 20 20 2f 2a  *pRegLeft,    /*
2042c 20 52 65 67 69 73 74 65 72 20 77 68 65 72 65 20   Register where 
2042d 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 69 73 20  left operand is 
2042e 73 74 6f 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20  stored */.  int 
2042f 2a 70 46 72 65 65 4c 65 66 74 2c 20 20 20 2f 2a  *pFreeLeft,   /*
20430 20 46 72 65 65 20 74 68 69 73 20 72 65 67 69 73   Free this regis
20431 74 65 72 20 77 68 65 6e 20 64 6f 6e 65 20 2a 2f  ter when done */
20432 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 2c  .  Expr *pRight,
20433 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68       /* The righ
20434 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69  t operand */.  i
20435 6e 74 20 2a 70 52 65 67 52 69 67 68 74 2c 20 20  nt *pRegRight,  
20436 20 2f 2a 20 52 65 67 69 73 74 65 72 20 77 68 65   /* Register whe
20437 72 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64  re right operand
20438 20 69 73 20 73 74 6f 72 65 64 20 2a 2f 0a 20 20   is stored */.  
20439 69 6e 74 20 2a 70 46 72 65 65 52 69 67 68 74 20  int *pFreeRight 
2043a 20 20 2f 2a 20 57 72 69 74 65 20 74 65 6d 70 20    /* Write temp 
2043b 72 65 67 69 73 74 65 72 20 66 6f 72 20 72 69 67  register for rig
2043c 68 74 20 6f 70 65 72 61 6e 64 20 74 68 65 72 65  ht operand there
2043d 20 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20   */.){.  while( 
2043e 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 55 50  pLeft->op==TK_UP
2043f 4c 55 53 20 29 20 70 4c 65 66 74 20 3d 20 70 4c  LUS ) pLeft = pL
20440 65 66 74 2d 3e 70 4c 65 66 74 3b 0a 20 20 70 4c  eft->pLeft;.  pL
20441 65 66 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50  eft->flags |= EP
20442 5f 41 6e 79 41 66 66 3b 0a 20 20 2a 70 52 65 67  _AnyAff;.  *pReg
20443 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 45 78  Left = sqlite3Ex
20444 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73  prCodeTemp(pPars
20445 65 2c 20 70 4c 65 66 74 2c 20 70 46 72 65 65 4c  e, pLeft, pFreeL
20446 65 66 74 29 3b 0a 20 20 77 68 69 6c 65 28 20 70  eft);.  while( p
20447 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 55 50  Right->op==TK_UP
20448 4c 55 53 20 29 20 70 52 69 67 68 74 20 3d 20 70  LUS ) pRight = p
20449 52 69 67 68 74 2d 3e 70 4c 65 66 74 3b 0a 20 20  Right->pLeft;.  
2044a 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 7c 3d  pRight->flags |=
2044b 20 45 50 5f 41 6e 79 41 66 66 3b 0a 20 20 2a 70   EP_AnyAff;.  *p
2044c 52 65 67 52 69 67 68 74 20 3d 20 73 71 6c 69 74  RegRight = sqlit
2044d 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70  e3ExprCodeTemp(p
2044e 50 61 72 73 65 2c 20 70 52 69 67 68 74 2c 20 70  Parse, pRight, p
2044f 46 72 65 65 52 69 67 68 74 29 3b 0a 7d 0a 0a 2f  FreeRight);.}../
20450 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
20451 64 65 20 66 6f 72 20 61 20 63 6f 6d 70 61 72 69  de for a compari
20452 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f  son operator..*/
20453 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 64 65  .static int code
20454 43 6f 6d 70 61 72 65 28 0a 20 20 50 61 72 73 65  Compare(.  Parse
20455 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a 20   *pParse,    /* 
20456 54 68 65 20 70 61 72 73 69 6e 67 20 28 61 6e 64  The parsing (and
20457 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67   code generating
20458 29 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45  ) context */.  E
20459 78 70 72 20 2a 70 4c 65 66 74 2c 20 20 20 20 20  xpr *pLeft,     
2045a 20 2f 2a 20 54 68 65 20 6c 65 66 74 20 6f 70 65   /* The left ope
2045b 72 61 6e 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a  rand */.  Expr *
2045c 70 52 69 67 68 74 2c 20 20 20 20 20 2f 2a 20 54  pRight,     /* T
2045d 68 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64  he right operand
2045e 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 63 6f 64 65   */.  int opcode
2045f 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63  ,       /* The c
20460 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 63 6f 64 65  omparison opcode
20461 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 31 2c 20 69   */.  int in1, i
20462 6e 74 20 69 6e 32 2c 20 2f 2a 20 52 65 67 69 73  nt in2, /* Regis
20463 74 65 72 20 68 6f 6c 64 69 6e 67 20 6f 70 65 72  ter holding oper
20464 61 6e 64 73 20 2a 2f 0a 20 20 69 6e 74 20 64 65  ands */.  int de
20465 73 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4a  st,         /* J
20466 75 6d 70 20 68 65 72 65 20 69 66 20 74 72 75 65  ump here if true
20467 2e 20 20 2a 2f 0a 20 20 69 6e 74 20 6a 75 6d 70  .  */.  int jump
20468 49 66 4e 75 6c 6c 20 20 20 20 2f 2a 20 49 66 20  IfNull    /* If 
20469 74 72 75 65 2c 20 6a 75 6d 70 20 69 66 20 65 69  true, jump if ei
2046a 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20  ther operand is 
2046b 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  NULL */.){.  int
2046c 20 70 35 3b 0a 20 20 69 6e 74 20 61 64 64 72 3b   p5;.  int addr;
2046d 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 34 3b 0a  .  CollSeq *p4;.
2046e 0a 20 20 70 34 20 3d 20 73 71 6c 69 74 65 33 42  .  p4 = sqlite3B
2046f 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c  inaryCompareColl
20470 53 65 71 28 70 50 61 72 73 65 2c 20 70 4c 65 66  Seq(pParse, pLef
20471 74 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 70 35  t, pRight);.  p5
20472 20 3d 20 62 69 6e 61 72 79 43 6f 6d 70 61 72 65   = binaryCompare
20473 50 35 28 70 4c 65 66 74 2c 20 70 52 69 67 68 74  P5(pLeft, pRight
20474 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20  , jumpIfNull);. 
20475 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
20476 64 62 65 41 64 64 4f 70 34 28 70 50 61 72 73 65  dbeAddOp4(pParse
20477 2d 3e 70 56 64 62 65 2c 20 6f 70 63 6f 64 65 2c  ->pVdbe, opcode,
20478 20 69 6e 32 2c 20 64 65 73 74 2c 20 69 6e 31 2c   in2, dest, in1,
20479 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2047a 20 20 20 20 20 20 20 20 20 20 20 20 28 76 6f 69              (voi
2047b 64 2a 29 70 34 2c 20 50 34 5f 43 4f 4c 4c 53 45  d*)p4, P4_COLLSE
2047c 51 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  Q);.  sqlite3Vdb
2047d 65 43 68 61 6e 67 65 50 35 28 70 50 61 72 73 65  eChangeP5(pParse
2047e 2d 3e 70 56 64 62 65 2c 20 28 75 38 29 70 35 29  ->pVdbe, (u8)p5)
2047f 3b 0a 20 20 69 66 28 20 28 70 35 20 26 20 53 51  ;.  if( (p5 & SQ
20480 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b 29 21 3d  LITE_AFF_MASK)!=
20481 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20  SQLITE_AFF_NONE 
20482 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  ){.    sqlite3Ex
20483 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43  prCacheAffinityC
20484 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 69 6e  hange(pParse, in
20485 31 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  1, 1);.    sqlit
20486 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e  e3ExprCacheAffin
20487 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65  ityChange(pParse
20488 2c 20 69 6e 32 2c 20 31 29 3b 0a 20 20 7d 0a 20  , in2, 1);.  }. 
20489 20 72 65 74 75 72 6e 20 61 64 64 72 3b 0a 7d 0a   return addr;.}.
2048a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  .#if SQLITE_MAX_
2048b 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 2f 2a 0a  EXPR_DEPTH>0./*.
2048c 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 20 61 72  ** Check that ar
2048d 67 75 6d 65 6e 74 20 6e 48 65 69 67 68 74 20 69  gument nHeight i
2048e 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65  s less than or e
2048f 71 75 61 6c 20 74 6f 20 74 68 65 20 6d 61 78 69  qual to the maxi
20490 6d 75 6d 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f  mum.** expressio
20491 6e 20 64 65 70 74 68 20 61 6c 6c 6f 77 65 64 2e  n depth allowed.
20492 20 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 6c   If it is not, l
20493 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65  eave an error me
20494 73 73 61 67 65 20 69 6e 0a 2a 2a 20 70 50 61 72  ssage in.** pPar
20495 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  se..*/.SQLITE_PR
20496 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
20497 33 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 74  3ExprCheckHeight
20498 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
20499 69 6e 74 20 6e 48 65 69 67 68 74 29 7b 0a 20 20  int nHeight){.  
2049a 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
2049b 4f 4b 3b 0a 20 20 69 6e 74 20 6d 78 48 65 69 67  OK;.  int mxHeig
2049c 68 74 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d  ht = pParse->db-
2049d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
2049e 49 4d 49 54 5f 45 58 50 52 5f 44 45 50 54 48 5d  IMIT_EXPR_DEPTH]
2049f 3b 0a 20 20 69 66 28 20 6e 48 65 69 67 68 74 3e  ;.  if( nHeight>
204a0 6d 78 48 65 69 67 68 74 20 29 7b 0a 20 20 20 20  mxHeight ){.    
204a1 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
204a2 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20  pParse, .       
204a3 22 45 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65  "Expression tree
204a4 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 28 6d   is too large (m
204a5 61 78 69 6d 75 6d 20 64 65 70 74 68 20 25 64 29  aximum depth %d)
204a6 22 2c 20 6d 78 48 65 69 67 68 74 0a 20 20 20 20  ", mxHeight.    
204a7 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  );.    rc = SQLI
204a8 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20  TE_ERROR;.  }.  
204a9 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
204aa 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74   The following t
204ab 68 72 65 65 20 66 75 6e 63 74 69 6f 6e 73 2c 20  hree functions, 
204ac 68 65 69 67 68 74 4f 66 45 78 70 72 28 29 2c 20  heightOfExpr(), 
204ad 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74  heightOfExprList
204ae 28 29 0a 2a 2a 20 61 6e 64 20 68 65 69 67 68 74  ().** and height
204af 4f 66 53 65 6c 65 63 74 28 29 2c 20 61 72 65 20  OfSelect(), are 
204b0 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e  used to determin
204b1 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65  e the maximum he
204b2 69 67 68 74 0a 2a 2a 20 6f 66 20 61 6e 79 20 65  ight.** of any e
204b3 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 72  xpression tree r
204b4 65 66 65 72 65 6e 63 65 64 20 62 79 20 74 68 65  eferenced by the
204b5 20 73 74 72 75 63 74 75 72 65 20 70 61 73 73 65   structure passe
204b6 64 20 61 73 20 74 68 65 0a 2a 2a 20 66 69 72 73  d as the.** firs
204b7 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a  t argument..**.*
204b8 2a 20 49 66 20 74 68 69 73 20 6d 61 78 69 6d 75  * If this maximu
204b9 6d 20 68 65 69 67 68 74 20 69 73 20 67 72 65 61  m height is grea
204ba 74 65 72 20 74 68 61 6e 20 74 68 65 20 63 75 72  ter than the cur
204bb 72 65 6e 74 20 76 61 6c 75 65 20 70 6f 69 6e 74  rent value point
204bc 65 64 0a 2a 2a 20 74 6f 20 62 79 20 70 6e 48 65  ed.** to by pnHe
204bd 69 67 68 74 2c 20 74 68 65 20 73 65 63 6f 6e 64  ight, the second
204be 20 70 61 72 61 6d 65 74 65 72 2c 20 74 68 65 6e   parameter, then
204bf 20 73 65 74 20 2a 70 6e 48 65 69 67 68 74 20 74   set *pnHeight t
204c0 6f 20 74 68 61 74 0a 2a 2a 20 76 61 6c 75 65 2e  o that.** value.
204c1 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
204c2 68 65 69 67 68 74 4f 66 45 78 70 72 28 45 78 70  heightOfExpr(Exp
204c3 72 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69  r *p, int *pnHei
204c4 67 68 74 29 7b 0a 20 20 69 66 28 20 70 20 29 7b  ght){.  if( p ){
204c5 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 48 65 69  .    if( p->nHei
204c6 67 68 74 3e 2a 70 6e 48 65 69 67 68 74 20 29 7b  ght>*pnHeight ){
204c7 0a 20 20 20 20 20 20 2a 70 6e 48 65 69 67 68 74  .      *pnHeight
204c8 20 3d 20 70 2d 3e 6e 48 65 69 67 68 74 3b 0a 20   = p->nHeight;. 
204c9 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69     }.  }.}.stati
204ca 63 20 76 6f 69 64 20 68 65 69 67 68 74 4f 66 45  c void heightOfE
204cb 78 70 72 4c 69 73 74 28 45 78 70 72 4c 69 73 74  xprList(ExprList
204cc 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69 67   *p, int *pnHeig
204cd 68 74 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a  ht){.  if( p ){.
204ce 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66      int i;.    f
204cf 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 45 78  or(i=0; i<p->nEx
204d0 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  pr; i++){.      
204d1 68 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e  heightOfExpr(p->
204d2 61 5b 69 5d 2e 70 45 78 70 72 2c 20 70 6e 48 65  a[i].pExpr, pnHe
204d3 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ight);.    }.  }
204d4 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68  .}.static void h
204d5 65 69 67 68 74 4f 66 53 65 6c 65 63 74 28 53 65  eightOfSelect(Se
204d6 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20 2a 70 6e  lect *p, int *pn
204d7 48 65 69 67 68 74 29 7b 0a 20 20 69 66 28 20 70  Height){.  if( p
204d8 20 29 7b 0a 20 20 20 20 68 65 69 67 68 74 4f 66   ){.    heightOf
204d9 45 78 70 72 28 70 2d 3e 70 57 68 65 72 65 2c 20  Expr(p->pWhere, 
204da 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68  pnHeight);.    h
204db 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70  eightOfExpr(p->p
204dc 48 61 76 69 6e 67 2c 20 70 6e 48 65 69 67 68 74  Having, pnHeight
204dd 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45  );.    heightOfE
204de 78 70 72 28 70 2d 3e 70 4c 69 6d 69 74 2c 20 70  xpr(p->pLimit, p
204df 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65  nHeight);.    he
204e0 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 4f  ightOfExpr(p->pO
204e1 66 66 73 65 74 2c 20 70 6e 48 65 69 67 68 74 29  ffset, pnHeight)
204e2 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78  ;.    heightOfEx
204e3 70 72 4c 69 73 74 28 70 2d 3e 70 45 4c 69 73 74  prList(p->pEList
204e4 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20  , pnHeight);.   
204e5 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73   heightOfExprLis
204e6 74 28 70 2d 3e 70 47 72 6f 75 70 42 79 2c 20 70  t(p->pGroupBy, p
204e7 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65  nHeight);.    he
204e8 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70  ightOfExprList(p
204e9 2d 3e 70 4f 72 64 65 72 42 79 2c 20 70 6e 48 65  ->pOrderBy, pnHe
204ea 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68  ight);.    heigh
204eb 74 4f 66 53 65 6c 65 63 74 28 70 2d 3e 70 50 72  tOfSelect(p->pPr
204ec 69 6f 72 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a  ior, pnHeight);.
204ed 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74    }.}../*.** Set
204ee 20 74 68 65 20 45 78 70 72 2e 6e 48 65 69 67 68   the Expr.nHeigh
204ef 74 20 76 61 72 69 61 62 6c 65 20 69 6e 20 74 68  t variable in th
204f0 65 20 73 74 72 75 63 74 75 72 65 20 70 61 73 73  e structure pass
204f1 65 64 20 61 73 20 61 6e 20 0a 2a 2a 20 61 72 67  ed as an .** arg
204f2 75 6d 65 6e 74 2e 20 41 6e 20 65 78 70 72 65 73  ument. An expres
204f3 73 69 6f 6e 20 77 69 74 68 20 6e 6f 20 63 68 69  sion with no chi
204f4 6c 64 72 65 6e 2c 20 45 78 70 72 2e 70 4c 69 73  ldren, Expr.pLis
204f5 74 20 6f 72 20 0a 2a 2a 20 45 78 70 72 2e 70 53  t or .** Expr.pS
204f6 65 6c 65 63 74 20 6d 65 6d 62 65 72 20 68 61 73  elect member has
204f7 20 61 20 68 65 69 67 68 74 20 6f 66 20 31 2e 20   a height of 1. 
204f8 41 6e 79 20 6f 74 68 65 72 20 65 78 70 72 65 73  Any other expres
204f9 73 69 6f 6e 0a 2a 2a 20 68 61 73 20 61 20 68 65  sion.** has a he
204fa 69 67 68 74 20 65 71 75 61 6c 20 74 6f 20 74 68  ight equal to th
204fb 65 20 6d 61 78 69 6d 75 6d 20 68 65 69 67 68 74  e maximum height
204fc 20 6f 66 20 61 6e 79 20 6f 74 68 65 72 20 0a 2a   of any other .*
204fd 2a 20 72 65 66 65 72 65 6e 63 65 64 20 45 78 70  * referenced Exp
204fe 72 20 70 6c 75 73 20 6f 6e 65 2e 0a 2a 2f 0a 73  r plus one..*/.s
204ff 74 61 74 69 63 20 76 6f 69 64 20 65 78 70 72 53  tatic void exprS
20500 65 74 48 65 69 67 68 74 28 45 78 70 72 20 2a 70  etHeight(Expr *p
20501 29 7b 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 74  ){.  int nHeight
20502 20 3d 20 30 3b 0a 20 20 68 65 69 67 68 74 4f 66   = 0;.  heightOf
20503 45 78 70 72 28 70 2d 3e 70 4c 65 66 74 2c 20 26  Expr(p->pLeft, &
20504 6e 48 65 69 67 68 74 29 3b 0a 20 20 68 65 69 67  nHeight);.  heig
20505 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 52 69 67  htOfExpr(p->pRig
20506 68 74 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20  ht, &nHeight);. 
20507 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70   if( ExprHasProp
20508 65 72 74 79 28 70 2c 20 45 50 5f 78 49 73 53 65  erty(p, EP_xIsSe
20509 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 68 65 69  lect) ){.    hei
2050a 67 68 74 4f 66 53 65 6c 65 63 74 28 70 2d 3e 78  ghtOfSelect(p->x
2050b 2e 70 53 65 6c 65 63 74 2c 20 26 6e 48 65 69 67  .pSelect, &nHeig
2050c 68 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ht);.  }else{.  
2050d 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69    heightOfExprLi
2050e 73 74 28 70 2d 3e 78 2e 70 4c 69 73 74 2c 20 26  st(p->x.pList, &
2050f 6e 48 65 69 67 68 74 29 3b 0a 20 20 7d 0a 20 20  nHeight);.  }.  
20510 70 2d 3e 6e 48 65 69 67 68 74 20 3d 20 6e 48 65  p->nHeight = nHe
20511 69 67 68 74 20 2b 20 31 3b 0a 7d 0a 0a 2f 2a 0a  ight + 1;.}../*.
20512 2a 2a 20 53 65 74 20 74 68 65 20 45 78 70 72 2e  ** Set the Expr.
20513 6e 48 65 69 67 68 74 20 76 61 72 69 61 62 6c 65  nHeight variable
20514 20 75 73 69 6e 67 20 74 68 65 20 65 78 70 72 53   using the exprS
20515 65 74 48 65 69 67 68 74 28 29 20 66 75 6e 63 74  etHeight() funct
20516 69 6f 6e 2e 20 49 66 0a 2a 2a 20 74 68 65 20 68  ion. If.** the h
20517 65 69 67 68 74 20 69 73 20 67 72 65 61 74 65 72  eight is greater
20518 20 74 68 61 6e 20 74 68 65 20 6d 61 78 69 6d 75   than the maximu
20519 6d 20 61 6c 6c 6f 77 65 64 20 65 78 70 72 65 73  m allowed expres
2051a 73 69 6f 6e 20 64 65 70 74 68 2c 0a 2a 2a 20 6c  sion depth,.** l
2051b 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 69 6e  eave an error in
2051c 20 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49   pParse..*/.SQLI
2051d 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2051e 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65  sqlite3ExprSetHe
2051f 69 67 68 74 28 50 61 72 73 65 20 2a 70 50 61 72  ight(Parse *pPar
20520 73 65 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 20  se, Expr *p){.  
20521 65 78 70 72 53 65 74 48 65 69 67 68 74 28 70 29  exprSetHeight(p)
20522 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43  ;.  sqlite3ExprC
20523 68 65 63 6b 48 65 69 67 68 74 28 70 50 61 72 73  heckHeight(pPars
20524 65 2c 20 70 2d 3e 6e 48 65 69 67 68 74 29 3b 0a  e, p->nHeight);.
20525 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
20526 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 69 67  the maximum heig
20527 68 74 20 6f 66 20 61 6e 79 20 65 78 70 72 65 73  ht of any expres
20528 73 69 6f 6e 20 74 72 65 65 20 72 65 66 65 72 65  sion tree refere
20529 6e 63 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 73  nced.** by the s
2052a 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20  elect statement 
2052b 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 67  passed as an arg
2052c 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ument..*/.SQLITE
2052d 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
2052e 69 74 65 33 53 65 6c 65 63 74 45 78 70 72 48 65  ite3SelectExprHe
2052f 69 67 68 74 28 53 65 6c 65 63 74 20 2a 70 29 7b  ight(Select *p){
20530 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 74 20 3d  .  int nHeight =
20531 20 30 3b 0a 20 20 68 65 69 67 68 74 4f 66 53 65   0;.  heightOfSe
20532 6c 65 63 74 28 70 2c 20 26 6e 48 65 69 67 68 74  lect(p, &nHeight
20533 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e 48 65 69  );.  return nHei
20534 67 68 74 3b 0a 7d 0a 23 65 6c 73 65 0a 20 20 23  ght;.}.#else.  #
20535 64 65 66 69 6e 65 20 65 78 70 72 53 65 74 48 65  define exprSetHe
20536 69 67 68 74 28 79 29 0a 23 65 6e 64 69 66 20 2f  ight(y).#endif /
20537 2a 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50  * SQLITE_MAX_EXP
20538 52 5f 44 45 50 54 48 3e 30 20 2a 2f 0a 0a 2f 2a  R_DEPTH>0 */../*
20539 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
2053a 20 69 73 20 74 68 65 20 63 6f 72 65 20 61 6c 6c   is the core all
2053b 6f 63 61 74 6f 72 20 66 6f 72 20 45 78 70 72 20  ocator for Expr 
2053c 6e 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e  nodes..**.** Con
2053d 73 74 72 75 63 74 20 61 20 6e 65 77 20 65 78 70  struct a new exp
2053e 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20 61 6e 64  ression node and
2053f 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   return a pointe
20540 72 20 74 6f 20 69 74 2e 20 20 4d 65 6d 6f 72 79  r to it.  Memory
20541 0a 2a 2a 20 66 6f 72 20 74 68 69 73 20 6e 6f 64  .** for this nod
20542 65 20 61 6e 64 20 66 6f 72 20 74 68 65 20 70 54  e and for the pT
20543 6f 6b 65 6e 20 61 72 67 75 6d 65 6e 74 20 69 73  oken argument is
20544 20 61 20 73 69 6e 67 6c 65 20 61 6c 6c 6f 63 61   a single alloca
20545 74 69 6f 6e 0a 2a 2a 20 6f 62 74 61 69 6e 65 64  tion.** obtained
20546 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 44 62 4d   from sqlite3DbM
20547 61 6c 6c 6f 63 28 29 2e 20 20 54 68 65 20 63 61  alloc().  The ca
20548 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a  lling function.*
20549 2a 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65  * is responsible
2054a 20 66 6f 72 20 6d 61 6b 69 6e 67 20 73 75 72 65   for making sure
2054b 20 74 68 65 20 6e 6f 64 65 20 65 76 65 6e 74 75   the node eventu
2054c 61 6c 6c 79 20 67 65 74 73 20 66 72 65 65 64 2e  ally gets freed.
2054d 0a 2a 2a 0a 2a 2a 20 49 66 20 64 65 71 75 6f 74  .**.** If dequot
2054e 65 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20  e is true, then 
2054f 74 68 65 20 74 6f 6b 65 6e 20 28 69 66 20 69 74  the token (if it
20550 20 65 78 69 73 74 73 29 20 69 73 20 64 65 71 75   exists) is dequ
20551 6f 74 65 64 2e 0a 2a 2a 20 49 66 20 64 65 71 75  oted..** If dequ
20552 6f 74 65 20 69 73 20 66 61 6c 73 65 2c 20 6e 6f  ote is false, no
20553 20 64 65 71 75 6f 74 69 6e 67 20 69 73 20 70 65   dequoting is pe
20554 72 66 6f 72 6d 61 6e 63 65 2e 20 20 54 68 65 20  rformance.  The 
20555 64 65 51 75 6f 74 65 0a 2a 2a 20 70 61 72 61 6d  deQuote.** param
20556 65 74 65 72 20 69 73 20 69 67 6e 6f 72 65 64 20  eter is ignored 
20557 69 66 20 70 54 6f 6b 65 6e 20 69 73 20 4e 55 4c  if pToken is NUL
20558 4c 20 6f 72 20 69 66 20 74 68 65 20 74 6f 6b 65  L or if the toke
20559 6e 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 61 70  n does not.** ap
2055a 70 65 61 72 20 74 6f 20 62 65 20 71 75 6f 74 65  pear to be quote
2055b 64 2e 20 20 49 66 20 74 68 65 20 71 75 6f 74 65  d.  If the quote
2055c 73 20 77 65 72 65 20 6f 66 20 74 68 65 20 66 6f  s were of the fo
2055d 72 6d 20 22 2e 2e 2e 22 20 28 64 6f 75 62 6c 65  rm "..." (double
2055e 2d 71 75 6f 74 65 73 29 0a 2a 2a 20 74 68 65 6e  -quotes).** then
2055f 20 74 68 65 20 45 50 5f 44 62 6c 51 75 6f 74 65   the EP_DblQuote
20560 64 20 66 6c 61 67 20 69 73 20 73 65 74 20 6f 6e  d flag is set on
20561 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
20562 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 70 65 63  node..**.** Spec
20563 69 61 6c 20 63 61 73 65 3a 20 20 49 66 20 6f 70  ial case:  If op
20564 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 61 6e 64  ==TK_INTEGER and
20565 20 70 54 6f 6b 65 6e 20 70 6f 69 6e 74 73 20 74   pToken points t
20566 6f 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 0a  o a string that.
20567 2a 2a 20 63 61 6e 20 62 65 20 74 72 61 6e 73 6c  ** can be transl
20568 61 74 65 64 20 69 6e 74 6f 20 61 20 33 32 2d 62  ated into a 32-b
20569 69 74 20 69 6e 74 65 67 65 72 2c 20 74 68 65 6e  it integer, then
2056a 20 74 68 65 20 74 6f 6b 65 6e 20 69 73 20 6e 6f   the token is no
2056b 74 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 75  t.** stored in u
2056c 2e 7a 54 6f 6b 65 6e 2e 20 20 49 6e 73 74 65 61  .zToken.  Instea
2056d 64 2c 20 74 68 65 20 69 6e 74 65 67 65 72 20 76  d, the integer v
2056e 61 6c 75 65 73 20 69 73 20 77 72 69 74 74 65 6e  alues is written
2056f 0a 2a 2a 20 69 6e 74 6f 20 75 2e 69 56 61 6c 75  .** into u.iValu
20570 65 20 61 6e 64 20 74 68 65 20 45 50 5f 49 6e 74  e and the EP_Int
20571 56 61 6c 75 65 20 66 6c 61 67 20 69 73 20 73 65  Value flag is se
20572 74 2e 20 20 4e 6f 20 65 78 74 72 61 20 73 74 6f  t.  No extra sto
20573 72 61 67 65 0a 2a 2a 20 69 73 20 61 6c 6c 6f 63  rage.** is alloc
20574 61 74 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65  ated to hold the
20575 20 69 6e 74 65 67 65 72 20 74 65 78 74 20 61 6e   integer text an
20576 64 20 74 68 65 20 64 65 71 75 6f 74 65 20 66 6c  d the dequote fl
20577 61 67 20 69 73 20 69 67 6e 6f 72 65 64 2e 0a 2a  ag is ignored..*
20578 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
20579 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78   Expr *sqlite3Ex
2057a 70 72 41 6c 6c 6f 63 28 0a 20 20 73 71 6c 69 74  prAlloc(.  sqlit
2057b 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
2057c 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72     /* Handle for
2057d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
2057e 5a 65 72 6f 28 29 20 28 6d 61 79 20 62 65 20 6e  Zero() (may be n
2057f 75 6c 6c 29 20 2a 2f 0a 20 20 69 6e 74 20 6f 70  ull) */.  int op
20580 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
20581 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20    /* Expression 
20582 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 63 6f 6e 73  opcode */.  cons
20583 74 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 2c  t Token *pToken,
20584 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 61 72 67      /* Token arg
20585 75 6d 65 6e 74 2e 20 20 4d 69 67 68 74 20 62 65  ument.  Might be
20586 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 64   NULL */.  int d
20587 65 71 75 6f 74 65 20 20 20 20 20 20 20 20 20 20  equote          
20588 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 64 65     /* True to de
20589 71 75 6f 74 65 20 2a 2f 0a 29 7b 0a 20 20 45 78  quote */.){.  Ex
2058a 70 72 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20  pr *pNew;.  int 
2058b 6e 45 78 74 72 61 20 3d 20 30 3b 0a 20 20 69 6e  nExtra = 0;.  in
2058c 74 20 69 56 61 6c 75 65 20 3d 20 30 3b 0a 0a 20  t iValue = 0;.. 
2058d 20 69 66 28 20 70 54 6f 6b 65 6e 20 29 7b 0a 20   if( pToken ){. 
2058e 20 20 20 69 66 28 20 6f 70 21 3d 54 4b 5f 49 4e     if( op!=TK_IN
2058f 54 45 47 45 52 20 7c 7c 20 70 54 6f 6b 65 6e 2d  TEGER || pToken-
20590 3e 7a 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 20  >z==0.          
20591 7c 7c 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74  || sqlite3GetInt
20592 33 32 28 70 54 6f 6b 65 6e 2d 3e 7a 2c 20 26 69  32(pToken->z, &i
20593 56 61 6c 75 65 29 3d 3d 30 20 29 7b 0a 20 20 20  Value)==0 ){.   
20594 20 20 20 6e 45 78 74 72 61 20 3d 20 70 54 6f 6b     nExtra = pTok
20595 65 6e 2d 3e 6e 2b 31 3b 0a 20 20 20 20 7d 0a 20  en->n+1;.    }. 
20596 20 7d 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69   }.  pNew = sqli
20597 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
20598 64 62 2c 20 73 69 7a 65 6f 66 28 45 78 70 72 29  db, sizeof(Expr)
20599 2b 6e 45 78 74 72 61 29 3b 0a 20 20 69 66 28 20  +nExtra);.  if( 
2059a 70 4e 65 77 20 29 7b 0a 20 20 20 20 70 4e 65 77  pNew ){.    pNew
2059b 2d 3e 6f 70 20 3d 20 28 75 38 29 6f 70 3b 0a 20  ->op = (u8)op;. 
2059c 20 20 20 70 4e 65 77 2d 3e 69 41 67 67 20 3d 20     pNew->iAgg = 
2059d 2d 31 3b 0a 20 20 20 20 69 66 28 20 70 54 6f 6b  -1;.    if( pTok
2059e 65 6e 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  en ){.      if( 
2059f 6e 45 78 74 72 61 3d 3d 30 20 29 7b 0a 20 20 20  nExtra==0 ){.   
205a0 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73       pNew->flags
205a1 20 7c 3d 20 45 50 5f 49 6e 74 56 61 6c 75 65 3b   |= EP_IntValue;
205a2 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 75  .        pNew->u
205a3 2e 69 56 61 6c 75 65 20 3d 20 69 56 61 6c 75 65  .iValue = iValue
205a4 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
205a5 20 20 20 20 20 20 20 69 6e 74 20 63 3b 0a 20 20         int c;.  
205a6 20 20 20 20 20 20 70 4e 65 77 2d 3e 75 2e 7a 54        pNew->u.zT
205a7 6f 6b 65 6e 20 3d 20 28 63 68 61 72 2a 29 26 70  oken = (char*)&p
205a8 4e 65 77 5b 31 5d 3b 0a 20 20 20 20 20 20 20 20  New[1];.        
205a9 6d 65 6d 63 70 79 28 70 4e 65 77 2d 3e 75 2e 7a  memcpy(pNew->u.z
205aa 54 6f 6b 65 6e 2c 20 70 54 6f 6b 65 6e 2d 3e 7a  Token, pToken->z
205ab 2c 20 70 54 6f 6b 65 6e 2d 3e 6e 29 3b 0a 20 20  , pToken->n);.  
205ac 20 20 20 20 20 20 70 4e 65 77 2d 3e 75 2e 7a 54        pNew->u.zT
205ad 6f 6b 65 6e 5b 70 54 6f 6b 65 6e 2d 3e 6e 5d 20  oken[pToken->n] 
205ae 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28  = 0;.        if(
205af 20 64 65 71 75 6f 74 65 20 26 26 20 6e 45 78 74   dequote && nExt
205b0 72 61 3e 3d 33 20 0a 20 20 20 20 20 20 20 20 20  ra>=3 .         
205b1 20 20 20 20 26 26 20 28 28 63 20 3d 20 70 54 6f      && ((c = pTo
205b2 6b 65 6e 2d 3e 7a 5b 30 5d 29 3d 3d 27 5c 27 27  ken->z[0])=='\''
205b3 20 7c 7c 20 63 3d 3d 27 22 27 20 7c 7c 20 63 3d   || c=='"' || c=
205b4 3d 27 5b 27 20 7c 7c 20 63 3d 3d 27 60 27 29 20  ='[' || c=='`') 
205b5 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
205b6 69 74 65 33 44 65 71 75 6f 74 65 28 70 4e 65 77  ite3Dequote(pNew
205b7 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20  ->u.zToken);.   
205b8 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d 27 22         if( c=='"
205b9 27 20 29 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20  ' ) pNew->flags 
205ba 7c 3d 20 45 50 5f 44 62 6c 51 75 6f 74 65 64 3b  |= EP_DblQuoted;
205bb 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
205bc 20 7d 0a 20 20 20 20 7d 0a 23 69 66 20 53 51 4c   }.    }.#if SQL
205bd 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50  ITE_MAX_EXPR_DEP
205be 54 48 3e 30 0a 20 20 20 20 70 4e 65 77 2d 3e 6e  TH>0.    pNew->n
205bf 48 65 69 67 68 74 20 3d 20 31 3b 0a 23 65 6e 64  Height = 1;.#end
205c0 69 66 20 20 0a 20 20 7d 0a 20 20 72 65 74 75 72  if  .  }.  retur
205c1 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n pNew;.}../*.**
205c2 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20   Allocate a new 
205c3 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20  expression node 
205c4 66 72 6f 6d 20 61 20 7a 65 72 6f 2d 74 65 72 6d  from a zero-term
205c5 69 6e 61 74 65 64 20 74 6f 6b 65 6e 20 74 68 61  inated token tha
205c6 74 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79  t has.** already
205c7 20 62 65 65 6e 20 64 65 71 75 6f 74 65 64 2e 0a   been dequoted..
205c8 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
205c9 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45  E Expr *sqlite3E
205ca 78 70 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  xpr(.  sqlite3 *
205cb 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  db,            /
205cc 2a 20 48 61 6e 64 6c 65 20 66 6f 72 20 73 71 6c  * Handle for sql
205cd 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
205ce 28 29 20 28 6d 61 79 20 62 65 20 6e 75 6c 6c 29  () (may be null)
205cf 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20   */.  int op,   
205d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
205d1 20 45 78 70 72 65 73 73 69 6f 6e 20 6f 70 63 6f   Expression opco
205d2 64 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  de */.  const ch
205d3 61 72 20 2a 7a 54 6f 6b 65 6e 20 20 20 20 20 20  ar *zToken      
205d4 2f 2a 20 54 6f 6b 65 6e 20 61 72 67 75 6d 65 6e  /* Token argumen
205d5 74 2e 20 20 4d 69 67 68 74 20 62 65 20 4e 55 4c  t.  Might be NUL
205d6 4c 20 2a 2f 0a 29 7b 0a 20 20 54 6f 6b 65 6e 20  L */.){.  Token 
205d7 78 3b 0a 20 20 78 2e 7a 20 3d 20 7a 54 6f 6b 65  x;.  x.z = zToke
205d8 6e 3b 0a 20 20 78 2e 6e 20 3d 20 7a 54 6f 6b 65  n;.  x.n = zToke
205d9 6e 20 3f 20 73 71 6c 69 74 65 33 53 74 72 6c 65  n ? sqlite3Strle
205da 6e 33 30 28 7a 54 6f 6b 65 6e 29 20 3a 20 30 3b  n30(zToken) : 0;
205db 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
205dc 33 45 78 70 72 41 6c 6c 6f 63 28 64 62 2c 20 6f  3ExprAlloc(db, o
205dd 70 2c 20 26 78 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a  p, &x, 0);.}../*
205de 0a 2a 2a 20 41 74 74 61 63 68 20 73 75 62 74 72  .** Attach subtr
205df 65 65 73 20 70 4c 65 66 74 20 61 6e 64 20 70 52  ees pLeft and pR
205e0 69 67 68 74 20 74 6f 20 74 68 65 20 45 78 70 72  ight to the Expr
205e1 20 6e 6f 64 65 20 70 52 6f 6f 74 2e 0a 2a 2a 0a   node pRoot..**.
205e2 2a 2a 20 49 66 20 70 52 6f 6f 74 3d 3d 4e 55 4c  ** If pRoot==NUL
205e3 4c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61  L that means tha
205e4 74 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  t a memory alloc
205e5 61 74 69 6f 6e 20 65 72 72 6f 72 20 68 61 73 20  ation error has 
205e6 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 20 49 6e 20  occurred..** In 
205e7 74 68 61 74 20 63 61 73 65 2c 20 64 65 6c 65 74  that case, delet
205e8 65 20 74 68 65 20 73 75 62 74 72 65 65 73 20 70  e the subtrees p
205e9 4c 65 66 74 20 61 6e 64 20 70 52 69 67 68 74 2e  Left and pRight.
205ea 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
205eb 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
205ec 78 70 72 41 74 74 61 63 68 53 75 62 74 72 65 65  xprAttachSubtree
205ed 73 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  s(.  sqlite3 *db
205ee 2c 0a 20 20 45 78 70 72 20 2a 70 52 6f 6f 74 2c  ,.  Expr *pRoot,
205ef 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 0a  .  Expr *pLeft,.
205f0 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 0a 29    Expr *pRight.)
205f1 7b 0a 20 20 69 66 28 20 70 52 6f 6f 74 3d 3d 30  {.  if( pRoot==0
205f2 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
205f3 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
205f4 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   );.    sqlite3E
205f5 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 4c  xprDelete(db, pL
205f6 65 66 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  eft);.    sqlite
205f7 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
205f8 70 52 69 67 68 74 29 3b 0a 20 20 7d 65 6c 73 65  pRight);.  }else
205f9 7b 0a 20 20 20 20 69 66 28 20 70 52 69 67 68 74  {.    if( pRight
205fa 20 29 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74 2d   ){.      pRoot-
205fb 3e 70 52 69 67 68 74 20 3d 20 70 52 69 67 68 74  >pRight = pRight
205fc 3b 0a 20 20 20 20 20 20 69 66 28 20 70 52 69 67  ;.      if( pRig
205fd 68 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45  ht->flags & EP_E
205fe 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20  xpCollate ){.   
205ff 20 20 20 20 20 70 52 6f 6f 74 2d 3e 66 6c 61 67       pRoot->flag
20600 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61  s |= EP_ExpColla
20601 74 65 3b 0a 20 20 20 20 20 20 20 20 70 52 6f 6f  te;.        pRoo
20602 74 2d 3e 70 43 6f 6c 6c 20 3d 20 70 52 69 67 68  t->pColl = pRigh
20603 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20  t->pColl;.      
20604 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
20605 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20 70  pLeft ){.      p
20606 52 6f 6f 74 2d 3e 70 4c 65 66 74 20 3d 20 70 4c  Root->pLeft = pL
20607 65 66 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70  eft;.      if( p
20608 4c 65 66 74 2d 3e 66 6c 61 67 73 20 26 20 45 50  Left->flags & EP
20609 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20  _ExpCollate ){. 
2060a 20 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e 66 6c         pRoot->fl
2060b 61 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c  ags |= EP_ExpCol
2060c 6c 61 74 65 3b 0a 20 20 20 20 20 20 20 20 70 52  late;.        pR
2060d 6f 6f 74 2d 3e 70 43 6f 6c 6c 20 3d 20 70 4c 65  oot->pColl = pLe
2060e 66 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20  ft->pColl;.     
2060f 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 65 78 70   }.    }.    exp
20610 72 53 65 74 48 65 69 67 68 74 28 70 52 6f 6f 74  rSetHeight(pRoot
20611 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
20612 41 6c 6c 6f 63 61 74 65 20 61 20 45 78 70 72 20  Allocate a Expr 
20613 6e 6f 64 65 20 77 68 69 63 68 20 6a 6f 69 6e 73  node which joins
20614 20 61 73 20 6d 61 6e 79 20 61 73 20 74 77 6f 20   as many as two 
20615 73 75 62 74 72 65 65 73 2e 0a 2a 2a 0a 2a 2a 20  subtrees..**.** 
20616 4f 6e 65 20 6f 72 20 62 6f 74 68 20 6f 66 20 74  One or both of t
20617 68 65 20 73 75 62 74 72 65 65 73 20 63 61 6e 20  he subtrees can 
20618 62 65 20 4e 55 4c 4c 2e 20 20 52 65 74 75 72 6e  be NULL.  Return
20619 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
2061a 65 20 6e 65 77 0a 2a 2a 20 45 78 70 72 20 6e 6f  e new.** Expr no
2061b 64 65 2e 20 20 4f 72 2c 20 69 66 20 61 6e 20 4f  de.  Or, if an O
2061c 4f 4d 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  OM error occurs,
2061d 20 73 65 74 20 70 50 61 72 73 65 2d 3e 64 62 2d   set pParse->db-
2061e 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 2c 0a 2a  >mallocFailed,.*
2061f 2a 20 66 72 65 65 20 74 68 65 20 73 75 62 74 72  * free the subtr
20620 65 65 73 20 61 6e 64 20 72 65 74 75 72 6e 20 4e  ees and return N
20621 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ULL..*/.SQLITE_P
20622 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c  RIVATE Expr *sql
20623 69 74 65 33 50 45 78 70 72 28 0a 20 20 50 61 72  ite3PExpr(.  Par
20624 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
20625 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
20626 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74  context */.  int
20627 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   op,            
20628 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69       /* Expressi
20629 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 45  on opcode */.  E
2062a 78 70 72 20 2a 70 4c 65 66 74 2c 20 20 20 20 20  xpr *pLeft,     
2062b 20 20 20 20 20 20 20 2f 2a 20 4c 65 66 74 20 6f         /* Left o
2062c 70 65 72 61 6e 64 20 2a 2f 0a 20 20 45 78 70 72  perand */.  Expr
2062d 20 2a 70 52 69 67 68 74 2c 20 20 20 20 20 20 20   *pRight,       
2062e 20 20 20 20 2f 2a 20 52 69 67 68 74 20 6f 70 65      /* Right ope
2062f 72 61 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  rand */.  const 
20630 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20 20 20  Token *pToken   
20631 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f    /* Argument to
20632 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72  ken */.){.  Expr
20633 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70   *p = sqlite3Exp
20634 72 41 6c 6c 6f 63 28 70 50 61 72 73 65 2d 3e 64  rAlloc(pParse->d
20635 62 2c 20 6f 70 2c 20 70 54 6f 6b 65 6e 2c 20 31  b, op, pToken, 1
20636 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
20637 41 74 74 61 63 68 53 75 62 74 72 65 65 73 28 70  AttachSubtrees(p
20638 50 61 72 73 65 2d 3e 64 62 2c 20 70 2c 20 70 4c  Parse->db, p, pL
20639 65 66 74 2c 20 70 52 69 67 68 74 29 3b 0a 20 20  eft, pRight);.  
2063a 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a  return p;.}../*.
2063b 2a 2a 20 4a 6f 69 6e 20 74 77 6f 20 65 78 70 72  ** Join two expr
2063c 65 73 73 69 6f 6e 73 20 75 73 69 6e 67 20 61 6e  essions using an
2063d 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e 20 20   AND operator.  
2063e 49 66 20 65 69 74 68 65 72 20 65 78 70 72 65 73  If either expres
2063f 73 69 6f 6e 20 69 73 0a 2a 2a 20 4e 55 4c 4c 2c  sion is.** NULL,
20640 20 74 68 65 6e 20 6a 75 73 74 20 72 65 74 75 72   then just retur
20641 6e 20 74 68 65 20 6f 74 68 65 72 20 65 78 70 72  n the other expr
20642 65 73 73 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  ession..*/.SQLIT
20643 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a  E_PRIVATE Expr *
20644 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 73  sqlite3ExprAnd(s
20645 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72  qlite3 *db, Expr
20646 20 2a 70 4c 65 66 74 2c 20 45 78 70 72 20 2a 70   *pLeft, Expr *p
20647 52 69 67 68 74 29 7b 0a 20 20 69 66 28 20 70 4c  Right){.  if( pL
20648 65 66 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  eft==0 ){.    re
20649 74 75 72 6e 20 70 52 69 67 68 74 3b 0a 20 20 7d  turn pRight;.  }
2064a 65 6c 73 65 20 69 66 28 20 70 52 69 67 68 74 3d  else if( pRight=
2064b 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
2064c 20 70 4c 65 66 74 3b 0a 20 20 7d 65 6c 73 65 7b   pLeft;.  }else{
2064d 0a 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77 20  .    Expr *pNew 
2064e 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 6c 6c  = sqlite3ExprAll
2064f 6f 63 28 64 62 2c 20 54 4b 5f 41 4e 44 2c 20 30  oc(db, TK_AND, 0
20650 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
20651 33 45 78 70 72 41 74 74 61 63 68 53 75 62 74 72  3ExprAttachSubtr
20652 65 65 73 28 64 62 2c 20 70 4e 65 77 2c 20 70 4c  ees(db, pNew, pL
20653 65 66 74 2c 20 70 52 69 67 68 74 29 3b 0a 20 20  eft, pRight);.  
20654 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 20    return pNew;. 
20655 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73   }.}../*.** Cons
20656 74 72 75 63 74 20 61 20 6e 65 77 20 65 78 70 72  truct a new expr
20657 65 73 73 69 6f 6e 20 6e 6f 64 65 20 66 6f 72 20  ession node for 
20658 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20  a function with 
20659 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 61 72 67 75  multiple.** argu
2065a 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ments..*/.SQLITE
2065b 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73  _PRIVATE Expr *s
2065c 71 6c 69 74 65 33 45 78 70 72 46 75 6e 63 74 69  qlite3ExprFuncti
2065d 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  on(Parse *pParse
2065e 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73  , ExprList *pLis
2065f 74 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e  t, Token *pToken
20660 29 7b 0a 20 20 45 78 70 72 20 2a 70 4e 65 77 3b  ){.  Expr *pNew;
20661 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
20662 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61   pParse->db;.  a
20663 73 73 65 72 74 28 20 70 54 6f 6b 65 6e 20 29 3b  ssert( pToken );
20664 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65  .  pNew = sqlite
20665 33 45 78 70 72 41 6c 6c 6f 63 28 64 62 2c 20 54  3ExprAlloc(db, T
20666 4b 5f 46 55 4e 43 54 49 4f 4e 2c 20 70 54 6f 6b  K_FUNCTION, pTok
20667 65 6e 2c 20 31 29 3b 0a 20 20 69 66 28 20 70 4e  en, 1);.  if( pN
20668 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  ew==0 ){.    sql
20669 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
2066a 74 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 20 2f  te(db, pList); /
2066b 2a 20 41 76 6f 69 64 20 6d 65 6d 6f 72 79 20 6c  * Avoid memory l
2066c 65 61 6b 20 77 68 65 6e 20 6d 61 6c 6c 6f 63 20  eak when malloc 
2066d 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20 72 65 74  fails */.    ret
2066e 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4e 65  urn 0;.  }.  pNe
2066f 77 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 70 4c 69  w->x.pList = pLi
20670 73 74 3b 0a 20 20 61 73 73 65 72 74 28 20 21 45  st;.  assert( !E
20671 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
20672 4e 65 77 2c 20 45 50 5f 78 49 73 53 65 6c 65 63  New, EP_xIsSelec
20673 74 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  t) );.  sqlite3E
20674 78 70 72 53 65 74 48 65 69 67 68 74 28 70 50 61  xprSetHeight(pPa
20675 72 73 65 2c 20 70 4e 65 77 29 3b 0a 20 20 72 65  rse, pNew);.  re
20676 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a  turn pNew;.}../*
20677 0a 2a 2a 20 41 73 73 69 67 6e 20 61 20 76 61 72  .** Assign a var
20678 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 74 6f 20  iable number to 
20679 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68  an expression th
2067a 61 74 20 65 6e 63 6f 64 65 73 20 61 20 77 69 6c  at encodes a wil
2067b 64 63 61 72 64 0a 2a 2a 20 69 6e 20 74 68 65 20  dcard.** in the 
2067c 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 73 74 61  original SQL sta
2067d 74 65 6d 65 6e 74 2e 20 20 0a 2a 2a 0a 2a 2a 20  tement.  .**.** 
2067e 57 69 6c 64 63 61 72 64 73 20 63 6f 6e 73 69 73  Wildcards consis
2067f 74 69 6e 67 20 6f 66 20 61 20 73 69 6e 67 6c 65  ting of a single
20680 20 22 3f 22 20 61 72 65 20 61 73 73 69 67 6e 65   "?" are assigne
20681 64 20 74 68 65 20 6e 65 78 74 20 73 65 71 75 65  d the next seque
20682 6e 74 69 61 6c 0a 2a 2a 20 76 61 72 69 61 62 6c  ntial.** variabl
20683 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20  e number..**.** 
20684 57 69 6c 64 63 61 72 64 73 20 6f 66 20 74 68 65  Wildcards of the
20685 20 66 6f 72 6d 20 22 3f 6e 6e 6e 22 20 61 72 65   form "?nnn" are
20686 20 61 73 73 69 67 6e 65 64 20 74 68 65 20 6e 75   assigned the nu
20687 6d 62 65 72 20 22 6e 6e 6e 22 2e 20 20 57 65 20  mber "nnn".  We 
20688 6d 61 6b 65 0a 2a 2a 20 73 75 72 65 20 22 6e 6e  make.** sure "nn
20689 6e 22 20 69 73 20 6e 6f 74 20 74 6f 6f 20 62 65  n" is not too be
2068a 20 74 6f 20 61 76 6f 69 64 20 61 20 64 65 6e 69   to avoid a deni
2068b 61 6c 20 6f 66 20 73 65 72 76 69 63 65 20 61 74  al of service at
2068c 74 61 63 6b 20 77 68 65 6e 0a 2a 2a 20 74 68 65  tack when.** the
2068d 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 63   SQL statement c
2068e 6f 6d 65 73 20 66 72 6f 6d 20 61 6e 20 65 78 74  omes from an ext
2068f 65 72 6e 61 6c 20 73 6f 75 72 63 65 2e 0a 2a 2a  ernal source..**
20690 0a 2a 2a 20 57 69 6c 64 63 61 72 64 73 20 6f 66  .** Wildcards of
20691 20 74 68 65 20 66 6f 72 6d 20 22 3a 61 61 61 22   the form ":aaa"
20692 2c 20 22 40 61 61 61 22 2c 20 6f 72 20 22 24 61  , "@aaa", or "$a
20693 61 61 22 20 61 72 65 20 61 73 73 69 67 6e 65 64  aa" are assigned
20694 20 74 68 65 20 73 61 6d 65 20 6e 75 6d 62 65 72   the same number
20695 0a 2a 2a 20 61 73 20 74 68 65 20 70 72 65 76 69  .** as the previ
20696 6f 75 73 20 69 6e 73 74 61 6e 63 65 20 6f 66 20  ous instance of 
20697 74 68 65 20 73 61 6d 65 20 77 69 6c 64 63 61 72  the same wildcar
20698 64 2e 20 20 4f 72 20 69 66 20 74 68 69 73 20 69  d.  Or if this i
20699 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 69  s the first.** i
2069a 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 77  nstance of the w
2069b 69 6c 64 63 61 72 64 2c 20 74 68 65 20 6e 65 78  ildcard, the nex
2069c 74 20 73 65 71 75 65 6e 69 61 6c 20 76 61 72 69  t sequenial vari
2069d 61 62 6c 65 20 6e 75 6d 62 65 72 20 69 73 0a 2a  able number is.*
2069e 2a 20 61 73 73 69 67 6e 65 64 2e 0a 2a 2f 0a 53  * assigned..*/.S
2069f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
206a0 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 73  id sqlite3ExprAs
206a1 73 69 67 6e 56 61 72 4e 75 6d 62 65 72 28 50 61  signVarNumber(Pa
206a2 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70  rse *pParse, Exp
206a3 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 73 71 6c  r *pExpr){.  sql
206a4 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
206a5 65 2d 3e 64 62 3b 0a 20 20 63 6f 6e 73 74 20 63  e->db;.  const c
206a6 68 61 72 20 2a 7a 3b 0a 0a 20 20 69 66 28 20 70  har *z;..  if( p
206a7 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75 72 6e  Expr==0 ) return
206a8 3b 0a 20 20 61 73 73 65 72 74 28 20 21 45 78 70  ;.  assert( !Exp
206a9 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28  rHasAnyProperty(
206aa 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c  pExpr, EP_IntVal
206ab 75 65 7c 45 50 5f 52 65 64 75 63 65 64 7c 45 50  ue|EP_Reduced|EP
206ac 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 3b 0a 20  _TokenOnly) );. 
206ad 20 7a 20 3d 20 70 45 78 70 72 2d 3e 75 2e 7a 54   z = pExpr->u.zT
206ae 6f 6b 65 6e 3b 0a 20 20 61 73 73 65 72 74 28 20  oken;.  assert( 
206af 7a 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  z!=0 );.  assert
206b0 28 20 7a 5b 30 5d 21 3d 30 20 29 3b 0a 20 20 69  ( z[0]!=0 );.  i
206b1 66 28 20 7a 5b 31 5d 3d 3d 30 20 29 7b 0a 20 20  f( z[1]==0 ){.  
206b2 20 20 2f 2a 20 57 69 6c 64 63 61 72 64 20 6f 66    /* Wildcard of
206b3 20 74 68 65 20 66 6f 72 6d 20 22 3f 22 2e 20 20   the form "?".  
206b4 41 73 73 69 67 6e 20 74 68 65 20 6e 65 78 74 20  Assign the next 
206b5 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 20  variable number 
206b6 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 7a  */.    assert( z
206b7 5b 30 5d 3d 3d 27 3f 27 20 29 3b 0a 20 20 20 20  [0]=='?' );.    
206b8 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20  pExpr->iTable = 
206b9 2b 2b 70 50 61 72 73 65 2d 3e 6e 56 61 72 3b 0a  ++pParse->nVar;.
206ba 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 5b 30 5d    }else if( z[0]
206bb 3d 3d 27 3f 27 20 29 7b 0a 20 20 20 20 2f 2a 20  =='?' ){.    /* 
206bc 57 69 6c 64 63 61 72 64 20 6f 66 20 74 68 65 20  Wildcard of the 
206bd 66 6f 72 6d 20 22 3f 6e 6e 6e 22 2e 20 20 43 6f  form "?nnn".  Co
206be 6e 76 65 72 74 20 22 6e 6e 6e 22 20 74 6f 20 61  nvert "nnn" to a
206bf 6e 20 69 6e 74 65 67 65 72 20 61 6e 64 0a 20 20  n integer and.  
206c0 20 20 2a 2a 20 75 73 65 20 69 74 20 61 73 20 74    ** use it as t
206c1 68 65 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62  he variable numb
206c2 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 3b  er */.    int i;
206c3 0a 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62  .    pExpr->iTab
206c4 6c 65 20 3d 20 69 20 3d 20 61 74 6f 69 28 28 63  le = i = atoi((c
206c5 68 61 72 2a 29 26 7a 5b 31 5d 29 3b 0a 20 20 20  har*)&z[1]);.   
206c6 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 30 20   testcase( i==0 
206c7 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
206c8 20 69 3d 3d 31 20 29 3b 0a 20 20 20 20 74 65 73   i==1 );.    tes
206c9 74 63 61 73 65 28 20 69 3d 3d 64 62 2d 3e 61 4c  tcase( i==db->aL
206ca 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
206cb 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45  T_VARIABLE_NUMBE
206cc 52 5d 2d 31 20 29 3b 0a 20 20 20 20 74 65 73 74  R]-1 );.    test
206cd 63 61 73 65 28 20 69 3d 3d 64 62 2d 3e 61 4c 69  case( i==db->aLi
206ce 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
206cf 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52  _VARIABLE_NUMBER
206d0 5d 20 29 3b 0a 20 20 20 20 69 66 28 20 69 3c 31  ] );.    if( i<1
206d1 20 7c 7c 20 69 3e 64 62 2d 3e 61 4c 69 6d 69 74   || i>db->aLimit
206d2 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41  [SQLITE_LIMIT_VA
206d3 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 29  RIABLE_NUMBER] )
206d4 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
206d5 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
206d6 22 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72  "variable number
206d7 20 6d 75 73 74 20 62 65 20 62 65 74 77 65 65 6e   must be between
206d8 20 3f 31 20 61 6e 64 20 3f 25 64 22 2c 0a 20 20   ?1 and ?%d",.  
206d9 20 20 20 20 20 20 20 20 64 62 2d 3e 61 4c 69 6d          db->aLim
206da 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
206db 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d  VARIABLE_NUMBER]
206dc 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
206dd 20 69 3e 70 50 61 72 73 65 2d 3e 6e 56 61 72 20   i>pParse->nVar 
206de 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  ){.      pParse-
206df 3e 6e 56 61 72 20 3d 20 69 3b 0a 20 20 20 20 7d  >nVar = i;.    }
206e0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
206e1 20 57 69 6c 64 63 61 72 64 73 20 6c 69 6b 65 20   Wildcards like 
206e2 22 3a 61 61 61 22 2c 20 22 24 61 61 61 22 20 6f  ":aaa", "$aaa" o
206e3 72 20 22 40 61 61 61 22 2e 20 20 52 65 75 73 65  r "@aaa".  Reuse
206e4 20 74 68 65 20 73 61 6d 65 20 76 61 72 69 61 62   the same variab
206e5 6c 65 0a 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72  le.    ** number
206e6 20 61 73 20 74 68 65 20 70 72 69 6f 72 20 61 70   as the prior ap
206e7 70 65 61 72 61 6e 63 65 20 6f 66 20 74 68 65 20  pearance of the 
206e8 73 61 6d 65 20 6e 61 6d 65 2c 20 6f 72 20 69 66  same name, or if
206e9 20 74 68 65 20 6e 61 6d 65 0a 20 20 20 20 2a 2a   the name.    **
206ea 20 68 61 73 20 6e 65 76 65 72 20 61 70 70 65 61   has never appea
206eb 72 65 64 20 62 65 66 6f 72 65 2c 20 72 65 75 73  red before, reus
206ec 65 20 74 68 65 20 73 61 6d 65 20 76 61 72 69 61  e the same varia
206ed 62 6c 65 20 6e 75 6d 62 65 72 0a 20 20 20 20 2a  ble number.    *
206ee 2f 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20  /.    int i;.   
206ef 20 75 33 32 20 6e 3b 0a 20 20 20 20 6e 20 3d 20   u32 n;.    n = 
206f0 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
206f1 7a 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  z);.    for(i=0;
206f2 20 69 3c 70 50 61 72 73 65 2d 3e 6e 56 61 72 45   i<pParse->nVarE
206f3 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  xpr; i++){.     
206f4 20 45 78 70 72 20 2a 70 45 20 3d 20 70 50 61 72   Expr *pE = pPar
206f5 73 65 2d 3e 61 70 56 61 72 45 78 70 72 5b 69 5d  se->apVarExpr[i]
206f6 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
206f7 70 45 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 69  pE!=0 );.      i
206f8 66 28 20 6d 65 6d 63 6d 70 28 70 45 2d 3e 75 2e  f( memcmp(pE->u.
206f9 7a 54 6f 6b 65 6e 2c 20 7a 2c 20 6e 29 3d 3d 30  zToken, z, n)==0
206fa 20 26 26 20 70 45 2d 3e 75 2e 7a 54 6f 6b 65 6e   && pE->u.zToken
206fb 5b 6e 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  [n]==0 ){.      
206fc 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20    pExpr->iTable 
206fd 3d 20 70 45 2d 3e 69 54 61 62 6c 65 3b 0a 20 20  = pE->iTable;.  
206fe 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
206ff 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
20700 66 28 20 69 3e 3d 70 50 61 72 73 65 2d 3e 6e 56  f( i>=pParse->nV
20701 61 72 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20  arExpr ){.      
20702 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20  pExpr->iTable = 
20703 2b 2b 70 50 61 72 73 65 2d 3e 6e 56 61 72 3b 0a  ++pParse->nVar;.
20704 20 20 20 20 20 20 69 66 28 20 70 50 61 72 73 65        if( pParse
20705 2d 3e 6e 56 61 72 45 78 70 72 3e 3d 70 50 61 72  ->nVarExpr>=pPar
20706 73 65 2d 3e 6e 56 61 72 45 78 70 72 41 6c 6c 6f  se->nVarExprAllo
20707 63 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 70  c-1 ){.        p
20708 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 41  Parse->nVarExprA
20709 6c 6c 6f 63 20 2b 3d 20 70 50 61 72 73 65 2d 3e  lloc += pParse->
2070a 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 20 2b 20  nVarExprAlloc + 
2070b 31 30 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72  10;.        pPar
2070c 73 65 2d 3e 61 70 56 61 72 45 78 70 72 20 3d 0a  se->apVarExpr =.
2070d 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
2070e 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72  te3DbReallocOrFr
2070f 65 65 28 0a 20 20 20 20 20 20 20 20 20 20 20 20  ee(.            
20710 20 20 64 62 2c 0a 20 20 20 20 20 20 20 20 20 20    db,.          
20711 20 20 20 20 70 50 61 72 73 65 2d 3e 61 70 56 61      pParse->apVa
20712 72 45 78 70 72 2c 0a 20 20 20 20 20 20 20 20 20  rExpr,.         
20713 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 56 61       pParse->nVa
20714 72 45 78 70 72 41 6c 6c 6f 63 2a 73 69 7a 65 6f  rExprAlloc*sizeo
20715 66 28 70 50 61 72 73 65 2d 3e 61 70 56 61 72 45  f(pParse->apVarE
20716 78 70 72 5b 30 5d 29 0a 20 20 20 20 20 20 20 20  xpr[0]).        
20717 20 20 20 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20      );.      }. 
20718 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d 61       if( !db->ma
20719 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
2071a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
2071b 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 21  arse->apVarExpr!
2071c 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 70 50  =0 );.        pP
2071d 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 5b  arse->apVarExpr[
2071e 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72  pParse->nVarExpr
2071f 2b 2b 5d 20 3d 20 70 45 78 70 72 3b 0a 20 20 20  ++] = pExpr;.   
20720 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 20 0a     }.    }.  } .
20721 20 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e 6e    if( !pParse->n
20722 45 72 72 20 26 26 20 70 50 61 72 73 65 2d 3e 6e  Err && pParse->n
20723 56 61 72 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53  Var>db->aLimit[S
20724 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49  QLITE_LIMIT_VARI
20725 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 29 7b 0a  ABLE_NUMBER] ){.
20726 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
20727 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f  Msg(pParse, "too
20728 20 6d 61 6e 79 20 53 51 4c 20 76 61 72 69 61 62   many SQL variab
20729 6c 65 73 22 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  les");.  }.}../*
2072a 0a 2a 2a 20 43 6c 65 61 72 20 61 6e 20 65 78 70  .** Clear an exp
2072b 72 65 73 73 69 6f 6e 20 73 74 72 75 63 74 75 72  ression structur
2072c 65 20 77 69 74 68 6f 75 74 20 64 65 6c 65 74 69  e without deleti
2072d 6e 67 20 74 68 65 20 73 74 72 75 63 74 75 72 65  ng the structure
2072e 20 69 74 73 65 6c 66 2e 0a 2a 2a 20 53 75 62 73   itself..** Subs
2072f 74 72 75 63 74 75 72 65 20 69 73 20 64 65 6c 65  tructure is dele
20730 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ted..*/.SQLITE_P
20731 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
20732 74 65 33 45 78 70 72 43 6c 65 61 72 28 73 71 6c  te3ExprClear(sql
20733 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20 2a  ite3 *db, Expr *
20734 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 21  p){.  assert( p!
20735 3d 30 20 29 3b 0a 20 20 69 66 28 20 21 45 78 70  =0 );.  if( !Exp
20736 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28  rHasAnyProperty(
20737 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29  p, EP_TokenOnly)
20738 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
20739 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d  xprDelete(db, p-
2073a 3e 70 4c 65 66 74 29 3b 0a 20 20 20 20 73 71 6c  >pLeft);.    sql
2073b 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
2073c 62 2c 20 70 2d 3e 70 52 69 67 68 74 29 3b 0a 20  b, p->pRight);. 
2073d 20 20 20 69 66 28 20 21 45 78 70 72 48 61 73 50     if( !ExprHasP
2073e 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 52 65  roperty(p, EP_Re
2073f 64 75 63 65 64 29 20 26 26 20 28 70 2d 3e 66 6c  duced) && (p->fl
20740 61 67 73 32 20 26 20 45 50 32 5f 4d 61 6c 6c 6f  ags2 & EP2_Mallo
20741 63 65 64 54 6f 6b 65 6e 29 21 3d 30 20 29 7b 0a  cedToken)!=0 ){.
20742 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
20743 72 65 65 28 64 62 2c 20 70 2d 3e 75 2e 7a 54 6f  ree(db, p->u.zTo
20744 6b 65 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ken);.    }.    
20745 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65  if( ExprHasPrope
20746 72 74 79 28 70 2c 20 45 50 5f 78 49 73 53 65 6c  rty(p, EP_xIsSel
20747 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 73 71  ect) ){.      sq
20748 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74  lite3SelectDelet
20749 65 28 64 62 2c 20 70 2d 3e 78 2e 70 53 65 6c 65  e(db, p->x.pSele
2074a 63 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ct);.    }else{.
2074b 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2074c 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  rListDelete(db, 
2074d 70 2d 3e 78 2e 70 4c 69 73 74 29 3b 0a 20 20 20  p->x.pList);.   
2074e 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
2074f 52 65 63 75 72 73 69 76 65 6c 79 20 64 65 6c 65  Recursively dele
20750 74 65 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  te an expression
20751 20 74 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45   tree..*/.SQLITE
20752 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
20753 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
20754 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70  sqlite3 *db, Exp
20755 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d 3d  r *p){.  if( p==
20756 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71  0 ) return;.  sq
20757 6c 69 74 65 33 45 78 70 72 43 6c 65 61 72 28 64  lite3ExprClear(d
20758 62 2c 20 70 29 3b 0a 20 20 69 66 28 20 21 45 78  b, p);.  if( !Ex
20759 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c  prHasProperty(p,
2075a 20 45 50 5f 53 74 61 74 69 63 29 20 29 7b 0a 20   EP_Static) ){. 
2075b 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2075c 28 64 62 2c 20 70 29 3b 0a 20 20 7d 0a 7d 0a 0a  (db, p);.  }.}..
2075d 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
2075e 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
2075f 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 74   allocated for t
20760 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 73 74  he expression st
20761 72 75 63 74 75 72 65 20 0a 2a 2a 20 70 61 73 73  ructure .** pass
20762 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20  ed as the first 
20763 61 72 67 75 6d 65 6e 74 2e 20 54 68 69 73 20 69  argument. This i
20764 73 20 61 6c 77 61 79 73 20 6f 6e 65 20 6f 66 20  s always one of 
20765 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45 2c 0a 2a  EXPR_FULLSIZE,.*
20766 2a 20 45 58 50 52 5f 52 45 44 55 43 45 44 53 49  * EXPR_REDUCEDSI
20767 5a 45 20 6f 72 20 45 58 50 52 5f 54 4f 4b 45 4e  ZE or EXPR_TOKEN
20768 4f 4e 4c 59 53 49 5a 45 2e 0a 2a 2f 0a 73 74 61  ONLYSIZE..*/.sta
20769 74 69 63 20 69 6e 74 20 65 78 70 72 53 74 72 75  tic int exprStru
2076a 63 74 53 69 7a 65 28 45 78 70 72 20 2a 70 29 7b  ctSize(Expr *p){
2076b 0a 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72  .  if( ExprHasPr
2076c 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 54 6f 6b  operty(p, EP_Tok
2076d 65 6e 4f 6e 6c 79 29 20 29 20 72 65 74 75 72 6e  enOnly) ) return
2076e 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53   EXPR_TOKENONLYS
2076f 49 5a 45 3b 0a 20 20 69 66 28 20 45 78 70 72 48  IZE;.  if( ExprH
20770 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50  asProperty(p, EP
20771 5f 52 65 64 75 63 65 64 29 20 29 20 72 65 74 75  _Reduced) ) retu
20772 72 6e 20 45 58 50 52 5f 52 45 44 55 43 45 44 53  rn EXPR_REDUCEDS
20773 49 5a 45 3b 0a 20 20 72 65 74 75 72 6e 20 45 58  IZE;.  return EX
20774 50 52 5f 46 55 4c 4c 53 49 5a 45 3b 0a 7d 0a 0a  PR_FULLSIZE;.}..
20775 2f 2a 0a 2a 2a 20 54 68 65 20 64 75 70 65 64 45  /*.** The dupedE
20776 78 70 72 2a 53 69 7a 65 28 29 20 72 6f 75 74 69  xpr*Size() routi
20777 6e 65 73 20 65 61 63 68 20 72 65 74 75 72 6e 20  nes each return 
20778 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
20779 74 65 73 20 72 65 71 75 69 72 65 64 0a 2a 2a 20  tes required.** 
2077a 74 6f 20 73 74 6f 72 65 20 61 20 63 6f 70 79 20  to store a copy 
2077b 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  of an expression
2077c 20 6f 72 20 65 78 70 72 65 73 73 69 6f 6e 20 74   or expression t
2077d 72 65 65 2e 20 20 54 68 65 79 20 64 69 66 66 65  ree.  They diffe
2077e 72 20 69 6e 0a 2a 2a 20 68 6f 77 20 6d 75 63 68  r in.** how much
2077f 20 6f 66 20 74 68 65 20 74 72 65 65 20 69 73 20   of the tree is 
20780 6d 65 61 73 75 72 65 64 2e 0a 2a 2a 0a 2a 2a 20  measured..**.** 
20781 20 20 20 20 64 75 70 65 64 45 78 70 72 53 74 72      dupedExprStr
20782 75 63 74 53 69 7a 65 28 29 20 20 20 20 20 53 69  uctSize()     Si
20783 7a 65 20 6f 66 20 6f 6e 6c 79 20 74 68 65 20 45  ze of only the E
20784 78 70 72 20 73 74 72 75 63 74 75 72 65 20 0a 2a  xpr structure .*
20785 2a 20 20 20 20 20 64 75 70 65 64 45 78 70 72 4e  *     dupedExprN
20786 6f 64 65 53 69 7a 65 28 29 20 20 20 20 20 20 20  odeSize()       
20787 53 69 7a 65 20 6f 66 20 45 78 70 72 20 2b 20 73  Size of Expr + s
20788 70 61 63 65 20 66 6f 72 20 74 6f 6b 65 6e 0a 2a  pace for token.*
20789 2a 20 20 20 20 20 64 75 70 65 64 45 78 70 72 53  *     dupedExprS
2078a 69 7a 65 28 29 20 20 20 20 20 20 20 20 20 20 20  ize()           
2078b 45 78 70 72 20 2b 20 74 6f 6b 65 6e 20 2b 20 73  Expr + token + s
2078c 75 62 74 72 65 65 20 63 6f 6d 70 6f 6e 65 6e 74  ubtree component
2078d 73 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s.**.***********
2078e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2078f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20791 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20792 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 75 70 65 64  .**.** The duped
20793 45 78 70 72 53 74 72 75 63 74 53 69 7a 65 28 29  ExprStructSize()
20794 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
20795 73 20 74 77 6f 20 76 61 6c 75 65 73 20 4f 52 2d  s two values OR-
20796 65 64 20 74 6f 67 65 74 68 65 72 3a 20 20 0a 2a  ed together:  .*
20797 2a 20 28 31 29 20 74 68 65 20 73 70 61 63 65 20  * (1) the space 
20798 72 65 71 75 69 72 65 64 20 66 6f 72 20 61 20 63  required for a c
20799 6f 70 79 20 6f 66 20 74 68 65 20 45 78 70 72 20  opy of the Expr 
2079a 73 74 72 75 63 74 75 72 65 20 6f 6e 6c 79 20 61  structure only a
2079b 6e 64 20 0a 2a 2a 20 28 32 29 20 74 68 65 20 45  nd .** (2) the E
2079c 50 5f 78 78 78 20 66 6c 61 67 73 20 74 68 61 74  P_xxx flags that
2079d 20 69 6e 64 69 63 61 74 65 20 77 68 61 74 20 74   indicate what t
2079e 68 65 20 73 74 72 75 63 74 75 72 65 20 73 69 7a  he structure siz
2079f 65 20 73 68 6f 75 6c 64 20 62 65 2e 0a 2a 2a 20  e should be..** 
207a0 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
207a1 73 20 69 73 20 61 6c 77 61 79 73 20 6f 6e 65 20  s is always one 
207a2 6f 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 45  of:.**.**      E
207a3 58 50 52 5f 46 55 4c 4c 53 49 5a 45 0a 2a 2a 20  XPR_FULLSIZE.** 
207a4 20 20 20 20 20 45 58 50 52 5f 52 45 44 55 43 45       EXPR_REDUCE
207a5 44 53 49 5a 45 20 20 20 7c 20 45 50 5f 52 65 64  DSIZE   | EP_Red
207a6 75 63 65 64 0a 2a 2a 20 20 20 20 20 20 45 58 50  uced.**      EXP
207a7 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 20  R_TOKENONLYSIZE 
207a8 7c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 0a 2a  | EP_TokenOnly.*
207a9 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66  *.** The size of
207aa 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 63   the structure c
207ab 61 6e 20 62 65 20 66 6f 75 6e 64 20 62 79 20 6d  an be found by m
207ac 61 73 6b 69 6e 67 20 74 68 65 20 72 65 74 75 72  asking the retur
207ad 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68  n value.** of th
207ae 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20  is routine with 
207af 30 78 66 66 66 2e 20 20 54 68 65 20 66 6c 61 67  0xfff.  The flag
207b0 73 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 20 62  s can be found b
207b1 79 20 6d 61 73 6b 69 6e 67 20 74 68 65 0a 2a 2a  y masking the.**
207b2 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 77 69   return value wi
207b3 74 68 20 45 50 5f 52 65 64 75 63 65 64 7c 45 50  th EP_Reduced|EP
207b4 5f 54 6f 6b 65 6e 4f 6e 6c 79 2e 0a 2a 2a 0a 2a  _TokenOnly..**.*
207b5 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 69 74 68  * Note that with
207b6 20 66 6c 61 67 73 3d 3d 45 58 50 52 44 55 50 5f   flags==EXPRDUP_
207b7 52 45 44 55 43 45 2c 20 74 68 69 73 20 72 6f 75  REDUCE, this rou
207b8 74 69 6e 65 73 20 77 6f 72 6b 73 20 6f 6e 20 66  tines works on f
207b9 75 6c 6c 2d 73 69 7a 65 0a 2a 2a 20 28 75 6e 72  ull-size.** (unr
207ba 65 64 75 63 65 64 29 20 45 78 70 72 20 6f 62 6a  educed) Expr obj
207bb 65 63 74 73 20 61 73 20 74 68 65 79 20 6f 72 20  ects as they or 
207bc 6f 72 69 67 69 6e 61 6c 6c 79 20 63 6f 6e 73 74  originally const
207bd 72 75 63 74 65 64 20 62 79 20 74 68 65 20 70 61  ructed by the pa
207be 72 73 65 72 2e 0a 2a 2a 20 44 75 72 69 6e 67 20  rser..** During 
207bf 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 61 6c 79  expression analy
207c0 73 69 73 2c 20 65 78 74 72 61 20 69 6e 66 6f 72  sis, extra infor
207c1 6d 61 74 69 6f 6e 20 69 73 20 63 6f 6d 70 75 74  mation is comput
207c2 65 64 20 61 6e 64 20 6d 6f 76 65 64 20 69 6e 74  ed and moved int
207c3 6f 0a 2a 2a 20 6c 61 74 65 72 20 70 61 72 74 73  o.** later parts
207c4 20 6f 66 20 74 65 68 20 45 78 70 72 20 6f 62 6a   of teh Expr obj
207c5 65 63 74 20 61 6e 64 20 74 68 61 74 20 65 78 74  ect and that ext
207c6 72 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6d  ra information m
207c7 69 67 68 74 20 67 65 74 20 63 68 6f 70 70 65 64  ight get chopped
207c8 0a 2a 2a 20 6f 66 66 20 69 66 20 74 68 65 20 65  .** off if the e
207c9 78 70 72 65 73 73 69 6f 6e 20 69 73 20 72 65 64  xpression is red
207ca 75 63 65 64 2e 20 20 4e 6f 74 65 20 61 6c 73 6f  uced.  Note also
207cb 20 74 68 61 74 20 69 74 20 64 6f 65 73 20 6e 6f   that it does no
207cc 74 20 77 6f 72 6b 20 74 6f 0a 2a 2a 20 6d 61 6b  t work to.** mak
207cd 65 20 61 20 45 58 50 52 44 55 50 5f 52 45 44 55  e a EXPRDUP_REDU
207ce 43 45 20 63 6f 70 79 20 6f 66 20 61 20 72 65 64  CE copy of a red
207cf 75 63 65 64 20 65 78 70 72 65 73 73 69 6f 6e 2e  uced expression.
207d0 20 20 49 74 20 69 73 20 6f 6e 6c 79 20 6c 65 67    It is only leg
207d1 61 6c 0a 2a 2a 20 74 6f 20 72 65 64 75 63 65 20  al.** to reduce 
207d2 61 20 70 72 69 73 74 69 6e 65 20 65 78 70 72 65  a pristine expre
207d3 73 73 69 6f 6e 20 74 72 65 65 20 66 72 6f 6d 20  ssion tree from 
207d4 74 68 65 20 70 61 72 73 65 72 2e 20 20 54 68 65  the parser.  The
207d5 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a   implementation.
207d6 2a 2a 20 6f 66 20 64 75 70 65 64 45 78 70 72 53  ** of dupedExprS
207d7 74 72 75 63 74 53 69 7a 65 28 29 20 63 6f 6e 74  tructSize() cont
207d8 61 69 6e 20 6d 75 6c 74 69 70 6c 65 20 61 73 73  ain multiple ass
207d9 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73  ert() statements
207da 20 74 68 61 74 20 61 74 74 65 6d 70 74 0a 2a 2a   that attempt.**
207db 20 74 6f 20 65 6e 66 6f 72 63 65 20 74 68 69 73   to enforce this
207dc 20 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 2a 2f 0a   constraint..*/.
207dd 73 74 61 74 69 63 20 69 6e 74 20 64 75 70 65 64  static int duped
207de 45 78 70 72 53 74 72 75 63 74 53 69 7a 65 28 45  ExprStructSize(E
207df 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67  xpr *p, int flag
207e0 73 29 7b 0a 20 20 69 6e 74 20 6e 53 69 7a 65 3b  s){.  int nSize;
207e1 0a 20 20 61 73 73 65 72 74 28 20 66 6c 61 67 73  .  assert( flags
207e2 3d 3d 45 58 50 52 44 55 50 5f 52 45 44 55 43 45  ==EXPRDUP_REDUCE
207e3 20 7c 7c 20 66 6c 61 67 73 3d 3d 30 20 29 3b 20   || flags==0 ); 
207e4 2f 2a 20 4f 6e 6c 79 20 6f 6e 65 20 66 6c 61 67  /* Only one flag
207e5 20 76 61 6c 75 65 20 61 6c 6c 6f 77 65 64 20 2a   value allowed *
207e6 2f 0a 20 20 69 66 28 20 30 3d 3d 28 66 6c 61 67  /.  if( 0==(flag
207e7 73 26 45 58 50 52 44 55 50 5f 52 45 44 55 43 45  s&EXPRDUP_REDUCE
207e8 29 20 29 7b 0a 20 20 20 20 6e 53 69 7a 65 20 3d  ) ){.    nSize =
207e9 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45 3b 0a   EXPR_FULLSIZE;.
207ea 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
207eb 65 72 74 28 20 21 45 78 70 72 48 61 73 41 6e 79  ert( !ExprHasAny
207ec 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 54  Property(p, EP_T
207ed 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 52 65 64 75  okenOnly|EP_Redu
207ee 63 65 64 29 20 29 3b 0a 20 20 20 20 61 73 73 65  ced) );.    asse
207ef 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70  rt( !ExprHasProp
207f0 65 72 74 79 28 70 2c 20 45 50 5f 46 72 6f 6d 4a  erty(p, EP_FromJ
207f1 6f 69 6e 29 20 29 3b 20 0a 20 20 20 20 61 73 73  oin) ); .    ass
207f2 65 72 74 28 20 28 70 2d 3e 66 6c 61 67 73 32 20  ert( (p->flags2 
207f3 26 20 45 50 32 5f 4d 61 6c 6c 6f 63 65 64 54 6f  & EP2_MallocedTo
207f4 6b 65 6e 29 3d 3d 30 20 29 3b 0a 20 20 20 20 61  ken)==0 );.    a
207f5 73 73 65 72 74 28 20 28 70 2d 3e 66 6c 61 67 73  ssert( (p->flags
207f6 32 20 26 20 45 50 32 5f 49 72 72 65 64 75 63 69  2 & EP2_Irreduci
207f7 62 6c 65 29 3d 3d 30 20 29 3b 0a 20 20 20 20 69  ble)==0 );.    i
207f8 66 28 20 70 2d 3e 70 4c 65 66 74 20 7c 7c 20 70  f( p->pLeft || p
207f9 2d 3e 70 52 69 67 68 74 20 7c 7c 20 70 2d 3e 70  ->pRight || p->p
207fa 43 6f 6c 6c 20 7c 7c 20 70 2d 3e 78 2e 70 4c 69  Coll || p->x.pLi
207fb 73 74 20 29 7b 0a 20 20 20 20 20 20 6e 53 69 7a  st ){.      nSiz
207fc 65 20 3d 20 45 58 50 52 5f 52 45 44 55 43 45 44  e = EXPR_REDUCED
207fd 53 49 5a 45 20 7c 20 45 50 5f 52 65 64 75 63 65  SIZE | EP_Reduce
207fe 64 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  d;.    }else{.  
207ff 20 20 20 20 6e 53 69 7a 65 20 3d 20 45 58 50 52      nSize = EXPR
20800 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 20 7c  _TOKENONLYSIZE |
20801 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 3b 0a 20   EP_TokenOnly;. 
20802 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
20803 6e 20 6e 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  n nSize;.}../*.*
20804 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
20805 72 65 74 75 72 6e 73 20 74 68 65 20 73 70 61 63  returns the spac
20806 65 20 69 6e 20 62 79 74 65 73 20 72 65 71 75 69  e in bytes requi
20807 72 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65  red to store the
20808 20 63 6f 70 79 20 0a 2a 2a 20 6f 66 20 74 68 65   copy .** of the
20809 20 45 78 70 72 20 73 74 72 75 63 74 75 72 65 20   Expr structure 
2080a 61 6e 64 20 61 20 63 6f 70 79 20 6f 66 20 74 68  and a copy of th
2080b 65 20 45 78 70 72 2e 75 2e 7a 54 6f 6b 65 6e 20  e Expr.u.zToken 
2080c 73 74 72 69 6e 67 20 28 69 66 20 74 68 61 74 0a  string (if that.
2080d 2a 2a 20 73 74 72 69 6e 67 20 69 73 20 64 65 66  ** string is def
2080e 69 6e 65 64 2e 29 0a 2a 2f 0a 73 74 61 74 69 63  ined.).*/.static
2080f 20 69 6e 74 20 64 75 70 65 64 45 78 70 72 4e 6f   int dupedExprNo
20810 64 65 53 69 7a 65 28 45 78 70 72 20 2a 70 2c 20  deSize(Expr *p, 
20811 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 69 6e  int flags){.  in
20812 74 20 6e 42 79 74 65 20 3d 20 64 75 70 65 64 45  t nByte = dupedE
20813 78 70 72 53 74 72 75 63 74 53 69 7a 65 28 70 2c  xprStructSize(p,
20814 20 66 6c 61 67 73 29 20 26 20 30 78 66 66 66 3b   flags) & 0xfff;
20815 0a 20 20 69 66 28 20 21 45 78 70 72 48 61 73 50  .  if( !ExprHasP
20816 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 49 6e  roperty(p, EP_In
20817 74 56 61 6c 75 65 29 20 26 26 20 70 2d 3e 75 2e  tValue) && p->u.
20818 7a 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 6e 42  zToken ){.    nB
20819 79 74 65 20 2b 3d 20 73 71 6c 69 74 65 33 53 74  yte += sqlite3St
2081a 72 6c 65 6e 33 30 28 70 2d 3e 75 2e 7a 54 6f 6b  rlen30(p->u.zTok
2081b 65 6e 29 2b 31 3b 0a 20 20 7d 0a 20 20 72 65 74  en)+1;.  }.  ret
2081c 75 72 6e 20 52 4f 55 4e 44 38 28 6e 42 79 74 65  urn ROUND8(nByte
2081d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  );.}../*.** Retu
2081e 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
2081f 20 62 79 74 65 73 20 72 65 71 75 69 72 65 64 20   bytes required 
20820 74 6f 20 63 72 65 61 74 65 20 61 20 64 75 70 6c  to create a dupl
20821 69 63 61 74 65 20 6f 66 20 74 68 65 20 0a 2a 2a  icate of the .**
20822 20 65 78 70 72 65 73 73 69 6f 6e 20 70 61 73 73   expression pass
20823 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20  ed as the first 
20824 61 72 67 75 6d 65 6e 74 2e 20 54 68 65 20 73 65  argument. The se
20825 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73  cond argument is
20826 20 61 0a 2a 2a 20 6d 61 73 6b 20 63 6f 6e 74 61   a.** mask conta
20827 69 6e 69 6e 67 20 45 58 50 52 44 55 50 5f 58 58  ining EXPRDUP_XX
20828 58 20 66 6c 61 67 73 2e 0a 2a 2a 0a 2a 2a 20 54  X flags..**.** T
20829 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
2082a 64 20 69 6e 63 6c 75 64 65 73 20 73 70 61 63 65  d includes space
2082b 20 74 6f 20 63 72 65 61 74 65 20 61 20 63 6f 70   to create a cop
2082c 79 20 6f 66 20 74 68 65 20 45 78 70 72 20 73 74  y of the Expr st
2082d 72 75 63 74 0a 2a 2a 20 69 74 73 65 6c 66 20 61  ruct.** itself a
2082e 6e 64 20 74 68 65 20 62 75 66 66 65 72 20 72 65  nd the buffer re
2082f 66 65 72 72 65 64 20 74 6f 20 62 79 20 45 78 70  ferred to by Exp
20830 72 2e 75 2e 7a 54 6f 6b 65 6e 2c 20 69 66 20 61  r.u.zToken, if a
20831 6e 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ny..**.** If the
20832 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 20   EXPRDUP_REDUCE 
20833 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65  flag is set, the
20834 6e 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  n the return val
20835 75 65 20 69 6e 63 6c 75 64 65 73 20 0a 2a 2a 20  ue includes .** 
20836 73 70 61 63 65 20 74 6f 20 64 75 70 6c 69 63 61  space to duplica
20837 74 65 20 61 6c 6c 20 45 78 70 72 20 6e 6f 64 65  te all Expr node
20838 73 20 69 6e 20 74 68 65 20 74 72 65 65 20 66 6f  s in the tree fo
20839 72 6d 65 64 20 62 79 20 45 78 70 72 2e 70 4c 65  rmed by Expr.pLe
2083a 66 74 20 0a 2a 2a 20 61 6e 64 20 45 78 70 72 2e  ft .** and Expr.
2083b 70 52 69 67 68 74 20 76 61 72 69 61 62 6c 65 73  pRight variables
2083c 20 28 62 75 74 20 6e 6f 74 20 66 6f 72 20 61 6e   (but not for an
2083d 79 20 73 74 72 75 63 74 75 72 65 73 20 70 6f 69  y structures poi
2083e 6e 74 65 64 20 74 6f 20 6f 72 20 0a 2a 2a 20 64  nted to or .** d
2083f 65 73 63 65 6e 64 65 64 20 66 72 6f 6d 20 74 68  escended from th
20840 65 20 45 78 70 72 2e 78 2e 70 4c 69 73 74 20 6f  e Expr.x.pList o
20841 72 20 45 78 70 72 2e 78 2e 70 53 65 6c 65 63 74  r Expr.x.pSelect
20842 20 76 61 72 69 61 62 6c 65 73 29 2e 0a 2a 2f 0a   variables)..*/.
20843 73 74 61 74 69 63 20 69 6e 74 20 64 75 70 65 64  static int duped
20844 45 78 70 72 53 69 7a 65 28 45 78 70 72 20 2a 70  ExprSize(Expr *p
20845 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20  , int flags){.  
20846 69 6e 74 20 6e 42 79 74 65 20 3d 20 30 3b 0a 20  int nByte = 0;. 
20847 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 6e 42   if( p ){.    nB
20848 79 74 65 20 3d 20 64 75 70 65 64 45 78 70 72 4e  yte = dupedExprN
20849 6f 64 65 53 69 7a 65 28 70 2c 20 66 6c 61 67 73  odeSize(p, flags
2084a 29 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73  );.    if( flags
2084b 26 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 20  &EXPRDUP_REDUCE 
2084c 29 7b 0a 20 20 20 20 20 20 6e 42 79 74 65 20 2b  ){.      nByte +
2084d 3d 20 64 75 70 65 64 45 78 70 72 53 69 7a 65 28  = dupedExprSize(
2084e 70 2d 3e 70 4c 65 66 74 2c 20 66 6c 61 67 73 29  p->pLeft, flags)
2084f 20 2b 20 64 75 70 65 64 45 78 70 72 53 69 7a 65   + dupedExprSize
20850 28 70 2d 3e 70 52 69 67 68 74 2c 20 66 6c 61 67  (p->pRight, flag
20851 73 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  s);.    }.  }.  
20852 72 65 74 75 72 6e 20 6e 42 79 74 65 3b 0a 7d 0a  return nByte;.}.
20853 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
20854 74 69 6f 6e 20 69 73 20 73 69 6d 69 6c 61 72 20  tion is similar 
20855 74 6f 20 73 71 6c 69 74 65 33 45 78 70 72 44 75  to sqlite3ExprDu
20856 70 28 29 2c 20 65 78 63 65 70 74 20 74 68 61 74  p(), except that
20857 20 69 66 20 70 7a 42 75 66 66 65 72 20 0a 2a 2a   if pzBuffer .**
20858 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 65   is not NULL the
20859 6e 20 2a 70 7a 42 75 66 66 65 72 20 69 73 20 61  n *pzBuffer is a
2085a 73 73 75 6d 65 64 20 74 6f 20 70 6f 69 6e 74 20  ssumed to point 
2085b 74 6f 20 61 20 62 75 66 66 65 72 20 6c 61 72 67  to a buffer larg
2085c 65 20 65 6e 6f 75 67 68 20 0a 2a 2a 20 74 6f 20  e enough .** to 
2085d 73 74 6f 72 65 20 74 68 65 20 63 6f 70 79 20 6f  store the copy o
2085e 66 20 65 78 70 72 65 73 73 69 6f 6e 20 70 2c 20  f expression p, 
2085f 74 68 65 20 63 6f 70 69 65 73 20 6f 66 20 70 2d  the copies of p-
20860 3e 75 2e 7a 54 6f 6b 65 6e 0a 2a 2a 20 28 69 66  >u.zToken.** (if
20861 20 61 70 70 6c 69 63 61 62 6c 65 29 2c 20 61 6e   applicable), an
20862 64 20 74 68 65 20 63 6f 70 69 65 73 20 6f 66 20  d the copies of 
20863 74 68 65 20 70 2d 3e 70 4c 65 66 74 20 61 6e 64  the p->pLeft and
20864 20 70 2d 3e 70 52 69 67 68 74 20 65 78 70 72 65   p->pRight expre
20865 73 73 69 6f 6e 73 2c 0a 2a 2a 20 69 66 20 61 6e  ssions,.** if an
20866 79 2e 20 42 65 66 6f 72 65 20 72 65 74 75 72 6e  y. Before return
20867 69 6e 67 2c 20 2a 70 7a 42 75 66 66 65 72 20 69  ing, *pzBuffer i
20868 73 20 73 65 74 20 74 6f 20 74 68 65 20 66 69 72  s set to the fir
20869 73 74 20 62 79 74 65 20 70 61 73 73 65 64 20 74  st byte passed t
2086a 68 65 0a 2a 2a 20 70 6f 72 74 69 6f 6e 20 6f 66  he.** portion of
2086b 20 74 68 65 20 62 75 66 66 65 72 20 63 6f 70 69   the buffer copi
2086c 65 64 20 69 6e 74 6f 20 62 79 20 74 68 69 73 20  ed into by this 
2086d 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  function..*/.sta
2086e 74 69 63 20 45 78 70 72 20 2a 65 78 70 72 44 75  tic Expr *exprDu
2086f 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45  p(sqlite3 *db, E
20870 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67  xpr *p, int flag
20871 73 2c 20 75 38 20 2a 2a 70 7a 42 75 66 66 65 72  s, u8 **pzBuffer
20872 29 7b 0a 20 20 45 78 70 72 20 2a 70 4e 65 77 20  ){.  Expr *pNew 
20873 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
20874 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c            /* Val
20875 75 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a  ue to return */.
20876 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 63    if( p ){.    c
20877 6f 6e 73 74 20 69 6e 74 20 69 73 52 65 64 75 63  onst int isReduc
20878 65 64 20 3d 20 28 66 6c 61 67 73 26 45 58 50 52  ed = (flags&EXPR
20879 44 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 20 20  DUP_REDUCE);.   
2087a 20 75 38 20 2a 7a 41 6c 6c 6f 63 3b 0a 20 20 20   u8 *zAlloc;.   
2087b 20 75 33 32 20 73 74 61 74 69 63 46 6c 61 67 20   u32 staticFlag 
2087c 3d 20 30 3b 0a 0a 20 20 20 20 61 73 73 65 72 74  = 0;..    assert
2087d 28 20 70 7a 42 75 66 66 65 72 3d 3d 30 20 7c 7c  ( pzBuffer==0 ||
2087e 20 69 73 52 65 64 75 63 65 64 20 29 3b 0a 0a 20   isReduced );.. 
2087f 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74     /* Figure out
20880 20 77 68 65 72 65 20 74 6f 20 77 72 69 74 65 20   where to write 
20881 74 68 65 20 6e 65 77 20 45 78 70 72 20 73 74 72  the new Expr str
20882 75 63 74 75 72 65 2e 20 2a 2f 0a 20 20 20 20 69  ucture. */.    i
20883 66 28 20 70 7a 42 75 66 66 65 72 20 29 7b 0a 20  f( pzBuffer ){. 
20884 20 20 20 20 20 7a 41 6c 6c 6f 63 20 3d 20 2a 70       zAlloc = *p
20885 7a 42 75 66 66 65 72 3b 0a 20 20 20 20 20 20 73  zBuffer;.      s
20886 74 61 74 69 63 46 6c 61 67 20 3d 20 45 50 5f 53  taticFlag = EP_S
20887 74 61 74 69 63 3b 0a 20 20 20 20 7d 65 6c 73 65  tatic;.    }else
20888 7b 0a 20 20 20 20 20 20 7a 41 6c 6c 6f 63 20 3d  {.      zAlloc =
20889 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
2088a 52 61 77 28 64 62 2c 20 64 75 70 65 64 45 78 70  Raw(db, dupedExp
2088b 72 53 69 7a 65 28 70 2c 20 66 6c 61 67 73 29 29  rSize(p, flags))
2088c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 77  ;.    }.    pNew
2088d 20 3d 20 28 45 78 70 72 20 2a 29 7a 41 6c 6c 6f   = (Expr *)zAllo
2088e 63 3b 0a 0a 20 20 20 20 69 66 28 20 70 4e 65 77  c;..    if( pNew
2088f 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53 65 74   ){.      /* Set
20890 20 6e 4e 65 77 53 69 7a 65 20 74 6f 20 74 68 65   nNewSize to the
20891 20 73 69 7a 65 20 61 6c 6c 6f 63 61 74 65 64 20   size allocated 
20892 66 6f 72 20 74 68 65 20 73 74 72 75 63 74 75 72  for the structur
20893 65 20 70 6f 69 6e 74 65 64 20 74 6f 0a 20 20 20  e pointed to.   
20894 20 20 20 2a 2a 20 62 79 20 70 4e 65 77 2e 20 54     ** by pNew. T
20895 68 69 73 20 69 73 20 65 69 74 68 65 72 20 45 58  his is either EX
20896 50 52 5f 46 55 4c 4c 53 49 5a 45 2c 20 45 58 50  PR_FULLSIZE, EXP
20897 52 5f 52 45 44 55 43 45 44 53 49 5a 45 20 6f 72  R_REDUCEDSIZE or
20898 0a 20 20 20 20 20 20 2a 2a 20 45 58 50 52 5f 54  .      ** EXPR_T
20899 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 2e 20 6e 54  OKENONLYSIZE. nT
2089a 6f 6b 65 6e 20 69 73 20 73 65 74 20 74 6f 20 74  oken is set to t
2089b 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
2089c 65 73 20 63 6f 6e 73 75 6d 65 64 0a 20 20 20 20  es consumed.    
2089d 20 20 2a 2a 20 62 79 20 74 68 65 20 63 6f 70 79    ** by the copy
2089e 20 6f 66 20 74 68 65 20 70 2d 3e 75 2e 7a 54 6f   of the p->u.zTo
2089f 6b 65 6e 20 73 74 72 69 6e 67 20 28 69 66 20 61  ken string (if a
208a0 6e 79 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ny)..      */.  
208a1 20 20 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e      const unsign
208a2 65 64 20 6e 53 74 72 75 63 74 53 69 7a 65 20 3d  ed nStructSize =
208a3 20 64 75 70 65 64 45 78 70 72 53 74 72 75 63 74   dupedExprStruct
208a4 53 69 7a 65 28 70 2c 20 66 6c 61 67 73 29 3b 0a  Size(p, flags);.
208a5 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20        const int 
208a6 6e 4e 65 77 53 69 7a 65 20 3d 20 6e 53 74 72 75  nNewSize = nStru
208a7 63 74 53 69 7a 65 20 26 20 30 78 66 66 66 3b 0a  ctSize & 0xfff;.
208a8 20 20 20 20 20 20 69 6e 74 20 6e 54 6f 6b 65 6e        int nToken
208a9 3b 0a 20 20 20 20 20 20 69 66 28 20 21 45 78 70  ;.      if( !Exp
208aa 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20  rHasProperty(p, 
208ab 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 26 26 20  EP_IntValue) && 
208ac 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 29 7b 0a 20  p->u.zToken ){. 
208ad 20 20 20 20 20 20 20 6e 54 6f 6b 65 6e 20 3d 20         nToken = 
208ae 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
208af 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 20 2b 20 31  p->u.zToken) + 1
208b0 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
208b1 20 20 20 20 20 20 20 6e 54 6f 6b 65 6e 20 3d 20         nToken = 
208b2 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  0;.      }.     
208b3 20 69 66 28 20 69 73 52 65 64 75 63 65 64 20 29   if( isReduced )
208b4 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
208b5 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74  ( ExprHasPropert
208b6 79 28 70 2c 20 45 50 5f 52 65 64 75 63 65 64 29  y(p, EP_Reduced)
208b7 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 6d  ==0 );.        m
208b8 65 6d 63 70 79 28 7a 41 6c 6c 6f 63 2c 20 70 2c  emcpy(zAlloc, p,
208b9 20 6e 4e 65 77 53 69 7a 65 29 3b 0a 20 20 20 20   nNewSize);.    
208ba 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
208bb 20 69 6e 74 20 6e 53 69 7a 65 20 3d 20 65 78 70   int nSize = exp
208bc 72 53 74 72 75 63 74 53 69 7a 65 28 70 29 3b 0a  rStructSize(p);.
208bd 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a          memcpy(z
208be 41 6c 6c 6f 63 2c 20 70 2c 20 6e 53 69 7a 65 29  Alloc, p, nSize)
208bf 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74  ;.        memset
208c0 28 26 7a 41 6c 6c 6f 63 5b 6e 53 69 7a 65 5d 2c  (&zAlloc[nSize],
208c1 20 30 2c 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a   0, EXPR_FULLSIZ
208c2 45 2d 6e 53 69 7a 65 29 3b 0a 20 20 20 20 20 20  E-nSize);.      
208c3 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 20  }..      /* Set 
208c4 74 68 65 20 45 50 5f 52 65 64 75 63 65 64 2c 20  the EP_Reduced, 
208c5 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 2c 20 61 6e  EP_TokenOnly, an
208c6 64 20 45 50 5f 53 74 61 74 69 63 20 66 6c 61 67  d EP_Static flag
208c7 73 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e  s appropriately.
208c8 20 2a 2f 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e   */.      pNew->
208c9 66 6c 61 67 73 20 26 3d 20 7e 28 45 50 5f 52 65  flags &= ~(EP_Re
208ca 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e  duced|EP_TokenOn
208cb 6c 79 7c 45 50 5f 53 74 61 74 69 63 29 3b 0a 20  ly|EP_Static);. 
208cc 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73       pNew->flags
208cd 20 7c 3d 20 6e 53 74 72 75 63 74 53 69 7a 65 20   |= nStructSize 
208ce 26 20 28 45 50 5f 52 65 64 75 63 65 64 7c 45 50  & (EP_Reduced|EP
208cf 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 3b 0a 20 20 20  _TokenOnly);.   
208d0 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c     pNew->flags |
208d1 3d 20 73 74 61 74 69 63 46 6c 61 67 3b 0a 0a 20  = staticFlag;.. 
208d2 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 74 68 65       /* Copy the
208d3 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 73 74 72   p->u.zToken str
208d4 69 6e 67 2c 20 69 66 20 61 6e 79 2e 20 2a 2f 0a  ing, if any. */.
208d5 20 20 20 20 20 20 69 66 28 20 6e 54 6f 6b 65 6e        if( nToken
208d6 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72   ){.        char
208d7 20 2a 7a 54 6f 6b 65 6e 20 3d 20 70 4e 65 77 2d   *zToken = pNew-
208d8 3e 75 2e 7a 54 6f 6b 65 6e 20 3d 20 28 63 68 61  >u.zToken = (cha
208d9 72 2a 29 26 7a 41 6c 6c 6f 63 5b 6e 4e 65 77 53  r*)&zAlloc[nNewS
208da 69 7a 65 5d 3b 0a 20 20 20 20 20 20 20 20 6d 65  ize];.        me
208db 6d 63 70 79 28 7a 54 6f 6b 65 6e 2c 20 70 2d 3e  mcpy(zToken, p->
208dc 75 2e 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e  u.zToken, nToken
208dd 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  );.      }..    
208de 20 20 69 66 28 20 30 3d 3d 28 28 70 2d 3e 66 6c    if( 0==((p->fl
208df 61 67 73 7c 70 4e 65 77 2d 3e 66 6c 61 67 73 29  ags|pNew->flags)
208e0 20 26 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29   & EP_TokenOnly)
208e1 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 46   ){.        /* F
208e2 69 6c 6c 20 69 6e 20 74 68 65 20 70 4e 65 77 2d  ill in the pNew-
208e3 3e 78 2e 70 53 65 6c 65 63 74 20 6f 72 20 70 4e  >x.pSelect or pN
208e4 65 77 2d 3e 78 2e 70 4c 69 73 74 20 6d 65 6d 62  ew->x.pList memb
208e5 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69  er. */.        i
208e6 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72  f( ExprHasProper
208e7 74 79 28 70 2c 20 45 50 5f 78 49 73 53 65 6c 65  ty(p, EP_xIsSele
208e8 63 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ct) ){.         
208e9 20 70 4e 65 77 2d 3e 78 2e 70 53 65 6c 65 63 74   pNew->x.pSelect
208ea 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
208eb 44 75 70 28 64 62 2c 20 70 2d 3e 78 2e 70 53 65  Dup(db, p->x.pSe
208ec 6c 65 63 74 2c 20 69 73 52 65 64 75 63 65 64 29  lect, isReduced)
208ed 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
208ee 0a 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d  .          pNew-
208ef 3e 78 2e 70 4c 69 73 74 20 3d 20 73 71 6c 69 74  >x.pList = sqlit
208f0 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62  e3ExprListDup(db
208f1 2c 20 70 2d 3e 78 2e 70 4c 69 73 74 2c 20 69 73  , p->x.pList, is
208f2 52 65 64 75 63 65 64 29 3b 0a 20 20 20 20 20 20  Reduced);.      
208f3 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20    }.      }..   
208f4 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 70 4e     /* Fill in pN
208f5 65 77 2d 3e 70 4c 65 66 74 20 61 6e 64 20 70 4e  ew->pLeft and pN
208f6 65 77 2d 3e 70 52 69 67 68 74 2e 20 2a 2f 0a 20  ew->pRight. */. 
208f7 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73       if( ExprHas
208f8 41 6e 79 50 72 6f 70 65 72 74 79 28 70 4e 65 77  AnyProperty(pNew
208f9 2c 20 45 50 5f 52 65 64 75 63 65 64 7c 45 50 5f  , EP_Reduced|EP_
208fa 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20  TokenOnly) ){.  
208fb 20 20 20 20 20 20 7a 41 6c 6c 6f 63 20 2b 3d 20        zAlloc += 
208fc 64 75 70 65 64 45 78 70 72 4e 6f 64 65 53 69 7a  dupedExprNodeSiz
208fd 65 28 70 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20  e(p, flags);.   
208fe 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73       if( ExprHas
208ff 50 72 6f 70 65 72 74 79 28 70 4e 65 77 2c 20 45  Property(pNew, E
20900 50 5f 52 65 64 75 63 65 64 29 20 29 7b 0a 20 20  P_Reduced) ){.  
20901 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4c          pNew->pL
20902 65 66 74 20 3d 20 65 78 70 72 44 75 70 28 64 62  eft = exprDup(db
20903 2c 20 70 2d 3e 70 4c 65 66 74 2c 20 45 58 50 52  , p->pLeft, EXPR
20904 44 55 50 5f 52 45 44 55 43 45 2c 20 26 7a 41 6c  DUP_REDUCE, &zAl
20905 6c 6f 63 29 3b 0a 20 20 20 20 20 20 20 20 20 20  loc);.          
20906 70 4e 65 77 2d 3e 70 52 69 67 68 74 20 3d 20 65  pNew->pRight = e
20907 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 52  xprDup(db, p->pR
20908 69 67 68 74 2c 20 45 58 50 52 44 55 50 5f 52 45  ight, EXPRDUP_RE
20909 44 55 43 45 2c 20 26 7a 41 6c 6c 6f 63 29 3b 0a  DUCE, &zAlloc);.
2090a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2090b 20 20 69 66 28 20 70 7a 42 75 66 66 65 72 20 29    if( pzBuffer )
2090c 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 70 7a 42  {.          *pzB
2090d 75 66 66 65 72 20 3d 20 7a 41 6c 6c 6f 63 3b 0a  uffer = zAlloc;.
2090e 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2090f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70  }else{.        p
20910 4e 65 77 2d 3e 66 6c 61 67 73 32 20 3d 20 30 3b  New->flags2 = 0;
20911 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 45 78  .        if( !Ex
20912 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79  prHasAnyProperty
20913 28 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79  (p, EP_TokenOnly
20914 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  ) ){.          p
20915 4e 65 77 2d 3e 70 4c 65 66 74 20 3d 20 73 71 6c  New->pLeft = sql
20916 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20  ite3ExprDup(db, 
20917 70 2d 3e 70 4c 65 66 74 2c 20 30 29 3b 0a 20 20  p->pLeft, 0);.  
20918 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 52          pNew->pR
20919 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33 45 78  ight = sqlite3Ex
2091a 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 52 69  prDup(db, p->pRi
2091b 67 68 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  ght, 0);.       
2091c 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20   }.      }..    
2091d 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  }.  }.  return p
2091e 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  New;.}../*.** Th
2091f 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 72 6f 75  e following grou
20920 70 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 6d 61  p of routines ma
20921 6b 65 20 64 65 65 70 20 63 6f 70 69 65 73 20 6f  ke deep copies o
20922 66 20 65 78 70 72 65 73 73 69 6f 6e 73 2c 0a 2a  f expressions,.*
20923 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  * expression lis
20924 74 73 2c 20 49 44 20 6c 69 73 74 73 2c 20 61 6e  ts, ID lists, an
20925 64 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65  d select stateme
20926 6e 74 73 2e 20 20 54 68 65 20 63 6f 70 69 65 73  nts.  The copies
20927 20 63 61 6e 0a 2a 2a 20 62 65 20 64 65 6c 65 74   can.** be delet
20928 65 64 20 28 62 79 20 62 65 69 6e 67 20 70 61 73  ed (by being pas
20929 73 65 64 20 74 6f 20 74 68 65 69 72 20 72 65 73  sed to their res
2092a 70 65 63 74 69 76 65 20 2e 2e 2e 44 65 6c 65 74  pective ...Delet
2092b 65 28 29 20 72 6f 75 74 69 6e 65 73 29 0a 2a 2a  e() routines).**
2092c 20 77 69 74 68 6f 75 74 20 65 66 66 65 63 74 69   without effecti
2092d 6e 67 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 73  ng the originals
2092e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 78 70 72  ..**.** The expr
2092f 65 73 73 69 6f 6e 20 6c 69 73 74 2c 20 49 44 2c  ession list, ID,
20930 20 61 6e 64 20 73 6f 75 72 63 65 20 6c 69 73 74   and source list
20931 73 20 72 65 74 75 72 6e 20 62 79 20 73 71 6c 69  s return by sqli
20932 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 29  te3ExprListDup()
20933 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 49 64 4c 69  ,.** sqlite3IdLi
20934 73 74 44 75 70 28 29 2c 20 61 6e 64 20 73 71 6c  stDup(), and sql
20935 69 74 65 33 53 72 63 4c 69 73 74 44 75 70 28 29  ite3SrcListDup()
20936 20 63 61 6e 20 6e 6f 74 20 62 65 20 66 75 72 74   can not be furt
20937 68 65 72 20 65 78 70 61 6e 64 65 64 20 0a 2a 2a  her expanded .**
20938 20 62 79 20 73 75 62 73 65 71 75 65 6e 74 20 63   by subsequent c
20939 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 2a 4c  alls to sqlite*L
2093a 69 73 74 41 70 70 65 6e 64 28 29 20 72 6f 75 74  istAppend() rout
2093b 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20  ines..**.** Any 
2093c 74 61 62 6c 65 73 20 74 68 61 74 20 74 68 65 20  tables that the 
2093d 53 72 63 4c 69 73 74 20 6d 69 67 68 74 20 70 6f  SrcList might po
2093e 69 6e 74 20 74 6f 20 61 72 65 20 6e 6f 74 20 64  int to are not d
2093f 75 70 6c 69 63 61 74 65 64 2e 0a 2a 2a 0a 2a 2a  uplicated..**.**
20940 20 54 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d   The flags param
20941 65 74 65 72 20 63 6f 6e 74 61 69 6e 73 20 61 20  eter contains a 
20942 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 74  combination of t
20943 68 65 20 45 58 50 52 44 55 50 5f 58 58 58 20 66  he EXPRDUP_XXX f
20944 6c 61 67 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20  lags..** If the 
20945 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 20 66  EXPRDUP_REDUCE f
20946 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e  lag is set, then
20947 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 72   the structure r
20948 65 74 75 72 6e 65 64 20 69 73 20 61 0a 2a 2a 20  eturned is a.** 
20949 74 72 75 6e 63 61 74 65 64 20 76 65 72 73 69 6f  truncated versio
2094a 6e 20 6f 66 20 74 68 65 20 75 73 75 61 6c 20 45  n of the usual E
2094b 78 70 72 20 73 74 72 75 63 74 75 72 65 20 74 68  xpr structure th
2094c 61 74 20 77 69 6c 6c 20 62 65 20 73 74 6f 72 65  at will be store
2094d 64 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20  d as.** part of 
2094e 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65  the in-memory re
2094f 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20  presentation of 
20950 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68  the database sch
20951 65 6d 61 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ema..*/.SQLITE_P
20952 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c  RIVATE Expr *sql
20953 69 74 65 33 45 78 70 72 44 75 70 28 73 71 6c 69  ite3ExprDup(sqli
20954 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20 2a 70  te3 *db, Expr *p
20955 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20  , int flags){.  
20956 72 65 74 75 72 6e 20 65 78 70 72 44 75 70 28 64  return exprDup(d
20957 62 2c 20 70 2c 20 66 6c 61 67 73 2c 20 30 29 3b  b, p, flags, 0);
20958 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
20959 45 20 45 78 70 72 4c 69 73 74 20 2a 73 71 6c 69  E ExprList *sqli
2095a 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 73  te3ExprListDup(s
2095b 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72  qlite3 *db, Expr
2095c 4c 69 73 74 20 2a 70 2c 20 69 6e 74 20 66 6c 61  List *p, int fla
2095d 67 73 29 7b 0a 20 20 45 78 70 72 4c 69 73 74 20  gs){.  ExprList 
2095e 2a 70 4e 65 77 3b 0a 20 20 73 74 72 75 63 74 20  *pNew;.  struct 
2095f 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70  ExprList_item *p
20960 49 74 65 6d 2c 20 2a 70 4f 6c 64 49 74 65 6d 3b  Item, *pOldItem;
20961 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20  .  int i;.  if( 
20962 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  p==0 ) return 0;
20963 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65  .  pNew = sqlite
20964 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
20965 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20 29   sizeof(*pNew) )
20966 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20  ;.  if( pNew==0 
20967 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e  ) return 0;.  pN
20968 65 77 2d 3e 69 45 43 75 72 73 6f 72 20 3d 20 30  ew->iECursor = 0
20969 3b 0a 20 20 70 4e 65 77 2d 3e 6e 45 78 70 72 20  ;.  pNew->nExpr 
2096a 3d 20 70 4e 65 77 2d 3e 6e 41 6c 6c 6f 63 20 3d  = pNew->nAlloc =
2096b 20 70 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 4e 65   p->nExpr;.  pNe
2096c 77 2d 3e 61 20 3d 20 70 49 74 65 6d 20 3d 20 73  w->a = pItem = s
2096d 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
2096e 77 28 64 62 2c 20 20 70 2d 3e 6e 45 78 70 72 2a  w(db,  p->nExpr*
2096f 73 69 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d 29 20  sizeof(p->a[0]) 
20970 29 3b 0a 20 20 69 66 28 20 70 49 74 65 6d 3d 3d  );.  if( pItem==
20971 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
20972 44 62 46 72 65 65 28 64 62 2c 20 70 4e 65 77 29  DbFree(db, pNew)
20973 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  ;.    return 0;.
20974 20 20 7d 20 0a 20 20 70 4f 6c 64 49 74 65 6d 20    } .  pOldItem 
20975 3d 20 70 2d 3e 61 3b 0a 20 20 66 6f 72 28 69 3d  = p->a;.  for(i=
20976 30 3b 20 69 3c 70 2d 3e 6e 45 78 70 72 3b 20 69  0; i<p->nExpr; i
20977 2b 2b 2c 20 70 49 74 65 6d 2b 2b 2c 20 70 4f 6c  ++, pItem++, pOl
20978 64 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 45 78  dItem++){.    Ex
20979 70 72 20 2a 70 4f 6c 64 45 78 70 72 20 3d 20 70  pr *pOldExpr = p
2097a 4f 6c 64 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a  OldItem->pExpr;.
2097b 20 20 20 20 70 49 74 65 6d 2d 3e 70 45 78 70 72      pItem->pExpr
2097c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
2097d 70 28 64 62 2c 20 70 4f 6c 64 45 78 70 72 2c 20  p(db, pOldExpr, 
2097e 66 6c 61 67 73 29 3b 0a 20 20 20 20 70 49 74 65  flags);.    pIte
2097f 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74  m->zName = sqlit
20980 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70  e3DbStrDup(db, p
20981 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b  OldItem->zName);
20982 0a 20 20 20 20 70 49 74 65 6d 2d 3e 7a 53 70 61  .    pItem->zSpa
20983 6e 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  n = sqlite3DbStr
20984 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d  Dup(db, pOldItem
20985 2d 3e 7a 53 70 61 6e 29 3b 0a 20 20 20 20 70 49  ->zSpan);.    pI
20986 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 20 3d  tem->sortOrder =
20987 20 70 4f 6c 64 49 74 65 6d 2d 3e 73 6f 72 74 4f   pOldItem->sortO
20988 72 64 65 72 3b 0a 20 20 20 20 70 49 74 65 6d 2d  rder;.    pItem-
20989 3e 64 6f 6e 65 20 3d 20 30 3b 0a 20 20 20 20 70  >done = 0;.    p
2098a 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 70 4f 6c  Item->iCol = pOl
2098b 64 49 74 65 6d 2d 3e 69 43 6f 6c 3b 0a 20 20 20  dItem->iCol;.   
2098c 20 70 49 74 65 6d 2d 3e 69 41 6c 69 61 73 20 3d   pItem->iAlias =
2098d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 41 6c 69 61   pOldItem->iAlia
2098e 73 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  s;.  }.  return 
2098f 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  pNew;.}../*.** I
20990 66 20 63 75 72 73 6f 72 73 2c 20 74 72 69 67 67  f cursors, trigg
20991 65 72 73 2c 20 76 69 65 77 73 20 61 6e 64 20 73  ers, views and s
20992 75 62 71 75 65 72 69 65 73 20 61 72 65 20 61 6c  ubqueries are al
20993 6c 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 0a 2a  l omitted from.*
20994 2a 20 74 68 65 20 62 75 69 6c 64 2c 20 74 68 65  * the build, the
20995 6e 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f  n none of the fo
20996 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73  llowing routines
20997 2c 20 65 78 63 65 70 74 20 66 6f 72 20 0a 2a 2a  , except for .**
20998 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75   sqlite3SelectDu
20999 70 28 29 2c 20 63 61 6e 20 62 65 20 63 61 6c 6c  p(), can be call
2099a 65 64 2e 20 73 71 6c 69 74 65 33 53 65 6c 65 63  ed. sqlite3Selec
2099b 74 44 75 70 28 29 20 69 73 20 73 6f 6d 65 74 69  tDup() is someti
2099c 6d 65 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 77 69  mes.** called wi
2099d 74 68 20 61 20 4e 55 4c 4c 20 61 72 67 75 6d 65  th a NULL argume
2099e 6e 74 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69  nt..*/.#if !defi
2099f 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
209a0 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65  VIEW) || !define
209a1 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  d(SQLITE_OMIT_TR
209a2 49 47 47 45 52 29 20 5c 0a 20 7c 7c 20 21 64 65  IGGER) \. || !de
209a3 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
209a4 54 5f 53 55 42 51 55 45 52 59 29 0a 53 51 4c 49  T_SUBQUERY).SQLI
209a5 54 45 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69  TE_PRIVATE SrcLi
209a6 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69  st *sqlite3SrcLi
209a7 73 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64  stDup(sqlite3 *d
209a8 62 2c 20 53 72 63 4c 69 73 74 20 2a 70 2c 20 69  b, SrcList *p, i
209a9 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 53 72 63  nt flags){.  Src
209aa 4c 69 73 74 20 2a 70 4e 65 77 3b 0a 20 20 69 6e  List *pNew;.  in
209ab 74 20 69 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65  t i;.  int nByte
209ac 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ;.  if( p==0 ) r
209ad 65 74 75 72 6e 20 30 3b 0a 20 20 6e 42 79 74 65  eturn 0;.  nByte
209ae 20 3d 20 73 69 7a 65 6f 66 28 2a 70 29 20 2b 20   = sizeof(*p) + 
209af 28 70 2d 3e 6e 53 72 63 3e 30 20 3f 20 73 69 7a  (p->nSrc>0 ? siz
209b0 65 6f 66 28 70 2d 3e 61 5b 30 5d 29 20 2a 20 28  eof(p->a[0]) * (
209b1 70 2d 3e 6e 53 72 63 2d 31 29 20 3a 20 30 29 3b  p->nSrc-1) : 0);
209b2 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65  .  pNew = sqlite
209b3 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
209b4 20 6e 42 79 74 65 20 29 3b 0a 20 20 69 66 28 20   nByte );.  if( 
209b5 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e  pNew==0 ) return
209b6 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 6e 53 72 63   0;.  pNew->nSrc
209b7 20 3d 20 70 4e 65 77 2d 3e 6e 41 6c 6c 6f 63 20   = pNew->nAlloc 
209b8 3d 20 70 2d 3e 6e 53 72 63 3b 0a 20 20 66 6f 72  = p->nSrc;.  for
209b9 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 53 72 63 3b  (i=0; i<p->nSrc;
209ba 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75 63   i++){.    struc
209bb 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
209bc 70 4e 65 77 49 74 65 6d 20 3d 20 26 70 4e 65 77  pNewItem = &pNew
209bd 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 73 74 72 75  ->a[i];.    stru
209be 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
209bf 2a 70 4f 6c 64 49 74 65 6d 20 3d 20 26 70 2d 3e  *pOldItem = &p->
209c0 61 5b 69 5d 3b 0a 20 20 20 20 54 61 62 6c 65 20  a[i];.    Table 
209c1 2a 70 54 61 62 3b 0a 20 20 20 20 70 4e 65 77 49  *pTab;.    pNewI
209c2 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20 3d  tem->zDatabase =
209c3 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70   sqlite3DbStrDup
209c4 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a  (db, pOldItem->z
209c5 44 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20 70  Database);.    p
209c6 4e 65 77 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d  NewItem->zName =
209c7 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70   sqlite3DbStrDup
209c8 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a  (db, pOldItem->z
209c9 4e 61 6d 65 29 3b 0a 20 20 20 20 70 4e 65 77 49  Name);.    pNewI
209ca 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 3d 20 73 71  tem->zAlias = sq
209cb 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62  lite3DbStrDup(db
209cc 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 41 6c 69  , pOldItem->zAli
209cd 61 73 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65  as);.    pNewIte
209ce 6d 2d 3e 6a 6f 69 6e 74 79 70 65 20 3d 20 70 4f  m->jointype = pO
209cf 6c 64 49 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65  ldItem->jointype
209d0 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e  ;.    pNewItem->
209d1 69 43 75 72 73 6f 72 20 3d 20 70 4f 6c 64 49 74  iCursor = pOldIt
209d2 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20  em->iCursor;.   
209d3 20 70 4e 65 77 49 74 65 6d 2d 3e 69 73 50 6f 70   pNewItem->isPop
209d4 75 6c 61 74 65 64 20 3d 20 70 4f 6c 64 49 74 65  ulated = pOldIte
209d5 6d 2d 3e 69 73 50 6f 70 75 6c 61 74 65 64 3b 0a  m->isPopulated;.
209d6 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 49      pNewItem->zI
209d7 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33 44 62  ndex = sqlite3Db
209d8 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49  StrDup(db, pOldI
209d9 74 65 6d 2d 3e 7a 49 6e 64 65 78 29 3b 0a 20 20  tem->zIndex);.  
209da 20 20 70 4e 65 77 49 74 65 6d 2d 3e 6e 6f 74 49    pNewItem->notI
209db 6e 64 65 78 65 64 20 3d 20 70 4f 6c 64 49 74 65  ndexed = pOldIte
209dc 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 3b 0a 20  m->notIndexed;. 
209dd 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 49 6e     pNewItem->pIn
209de 64 65 78 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e  dex = pOldItem->
209df 70 49 6e 64 65 78 3b 0a 20 20 20 20 70 54 61 62  pIndex;.    pTab
209e0 20 3d 20 70 4e 65 77 49 74 65 6d 2d 3e 70 54 61   = pNewItem->pTa
209e1 62 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 54  b = pOldItem->pT
209e2 61 62 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62  ab;.    if( pTab
209e3 20 29 7b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e   ){.      pTab->
209e4 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20  nRef++;.    }.  
209e5 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 53 65 6c    pNewItem->pSel
209e6 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65 6c  ect = sqlite3Sel
209e7 65 63 74 44 75 70 28 64 62 2c 20 70 4f 6c 64 49  ectDup(db, pOldI
209e8 74 65 6d 2d 3e 70 53 65 6c 65 63 74 2c 20 66 6c  tem->pSelect, fl
209e9 61 67 73 29 3b 0a 20 20 20 20 70 4e 65 77 49 74  ags);.    pNewIt
209ea 65 6d 2d 3e 70 4f 6e 20 3d 20 73 71 6c 69 74 65  em->pOn = sqlite
209eb 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 6c  3ExprDup(db, pOl
209ec 64 49 74 65 6d 2d 3e 70 4f 6e 2c 20 66 6c 61 67  dItem->pOn, flag
209ed 73 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d  s);.    pNewItem
209ee 2d 3e 70 55 73 69 6e 67 20 3d 20 73 71 6c 69 74  ->pUsing = sqlit
209ef 65 33 49 64 4c 69 73 74 44 75 70 28 64 62 2c 20  e3IdListDup(db, 
209f0 70 4f 6c 64 49 74 65 6d 2d 3e 70 55 73 69 6e 67  pOldItem->pUsing
209f1 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d  );.    pNewItem-
209f2 3e 63 6f 6c 55 73 65 64 20 3d 20 70 4f 6c 64 49  >colUsed = pOldI
209f3 74 65 6d 2d 3e 63 6f 6c 55 73 65 64 3b 0a 20 20  tem->colUsed;.  
209f4 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b  }.  return pNew;
209f5 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
209f6 45 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65  E IdList *sqlite
209f7 33 49 64 4c 69 73 74 44 75 70 28 73 71 6c 69 74  3IdListDup(sqlit
209f8 65 33 20 2a 64 62 2c 20 49 64 4c 69 73 74 20 2a  e3 *db, IdList *
209f9 70 29 7b 0a 20 20 49 64 4c 69 73 74 20 2a 70 4e  p){.  IdList *pN
209fa 65 77 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  ew;.  int i;.  i
209fb 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( p==0 ) return
209fc 20 30 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c   0;.  pNew = sql
209fd 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
209fe 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77  db, sizeof(*pNew
209ff 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d  ) );.  if( pNew=
20a00 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
20a01 20 70 4e 65 77 2d 3e 6e 49 64 20 3d 20 70 4e 65   pNew->nId = pNe
20a02 77 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e  w->nAlloc = p->n
20a03 49 64 3b 0a 20 20 70 4e 65 77 2d 3e 61 20 3d 20  Id;.  pNew->a = 
20a04 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
20a05 61 77 28 64 62 2c 20 70 2d 3e 6e 49 64 2a 73 69  aw(db, p->nId*si
20a06 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d 29 20 29 3b  zeof(p->a[0]) );
20a07 0a 20 20 69 66 28 20 70 4e 65 77 2d 3e 61 3d 3d  .  if( pNew->a==
20a08 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
20a09 44 62 46 72 65 65 28 64 62 2c 20 70 4e 65 77 29  DbFree(db, pNew)
20a0a 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  ;.    return 0;.
20a0b 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69    }.  for(i=0; i
20a0c 3c 70 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20  <p->nId; i++){. 
20a0d 20 20 20 73 74 72 75 63 74 20 49 64 4c 69 73 74     struct IdList
20a0e 5f 69 74 65 6d 20 2a 70 4e 65 77 49 74 65 6d 20  _item *pNewItem 
20a0f 3d 20 26 70 4e 65 77 2d 3e 61 5b 69 5d 3b 0a 20  = &pNew->a[i];. 
20a10 20 20 20 73 74 72 75 63 74 20 49 64 4c 69 73 74     struct IdList
20a11 5f 69 74 65 6d 20 2a 70 4f 6c 64 49 74 65 6d 20  _item *pOldItem 
20a12 3d 20 26 70 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20  = &p->a[i];.    
20a13 70 4e 65 77 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20  pNewItem->zName 
20a14 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
20a15 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e  p(db, pOldItem->
20a16 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 4e 65 77  zName);.    pNew
20a17 49 74 65 6d 2d 3e 69 64 78 20 3d 20 70 4f 6c 64  Item->idx = pOld
20a18 49 74 65 6d 2d 3e 69 64 78 3b 0a 20 20 7d 0a 20  Item->idx;.  }. 
20a19 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a   return pNew;.}.
20a1a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53  SQLITE_PRIVATE S
20a1b 65 6c 65 63 74 20 2a 73 71 6c 69 74 65 33 53 65  elect *sqlite3Se
20a1c 6c 65 63 74 44 75 70 28 73 71 6c 69 74 65 33 20  lectDup(sqlite3 
20a1d 2a 64 62 2c 20 53 65 6c 65 63 74 20 2a 70 2c 20  *db, Select *p, 
20a1e 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 53 65  int flags){.  Se
20a1f 6c 65 63 74 20 2a 70 4e 65 77 3b 0a 20 20 69 66  lect *pNew;.  if
20a20 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ( p==0 ) return 
20a21 30 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69  0;.  pNew = sqli
20a22 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
20a23 62 2c 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b  b, sizeof(*p) );
20a24 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29  .  if( pNew==0 )
20a25 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65   return 0;.  pNe
20a26 77 2d 3e 70 45 4c 69 73 74 20 3d 20 73 71 6c 69  w->pEList = sqli
20a27 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64  te3ExprListDup(d
20a28 62 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20 66 6c  b, p->pEList, fl
20a29 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 53  ags);.  pNew->pS
20a2a 72 63 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c  rc = sqlite3SrcL
20a2b 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 53  istDup(db, p->pS
20a2c 72 63 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e  rc, flags);.  pN
20a2d 65 77 2d 3e 70 57 68 65 72 65 20 3d 20 73 71 6c  ew->pWhere = sql
20a2e 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20  ite3ExprDup(db, 
20a2f 70 2d 3e 70 57 68 65 72 65 2c 20 66 6c 61 67 73  p->pWhere, flags
20a30 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 47 72 6f 75  );.  pNew->pGrou
20a31 70 42 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70  pBy = sqlite3Exp
20a32 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e  rListDup(db, p->
20a33 70 47 72 6f 75 70 42 79 2c 20 66 6c 61 67 73 29  pGroupBy, flags)
20a34 3b 0a 20 20 70 4e 65 77 2d 3e 70 48 61 76 69 6e  ;.  pNew->pHavin
20a35 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  g = sqlite3ExprD
20a36 75 70 28 64 62 2c 20 70 2d 3e 70 48 61 76 69 6e  up(db, p->pHavin
20a37 67 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65  g, flags);.  pNe
20a38 77 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 73 71  w->pOrderBy = sq
20a39 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70  lite3ExprListDup
20a3a 28 64 62 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79  (db, p->pOrderBy
20a3b 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77  , flags);.  pNew
20a3c 2d 3e 6f 70 20 3d 20 70 2d 3e 6f 70 3b 0a 20 20  ->op = p->op;.  
20a3d 70 4e 65 77 2d 3e 70 50 72 69 6f 72 20 3d 20 73  pNew->pPrior = s
20a3e 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28  qlite3SelectDup(
20a3f 64 62 2c 20 70 2d 3e 70 50 72 69 6f 72 2c 20 66  db, p->pPrior, f
20a40 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70  lags);.  pNew->p
20a41 4c 69 6d 69 74 20 3d 20 73 71 6c 69 74 65 33 45  Limit = sqlite3E
20a42 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4c  xprDup(db, p->pL
20a43 69 6d 69 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20  imit, flags);.  
20a44 70 4e 65 77 2d 3e 70 4f 66 66 73 65 74 20 3d 20  pNew->pOffset = 
20a45 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64  sqlite3ExprDup(d
20a46 62 2c 20 70 2d 3e 70 4f 66 66 73 65 74 2c 20 66  b, p->pOffset, f
20a47 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 69  lags);.  pNew->i
20a48 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 70 4e 65  Limit = 0;.  pNe
20a49 77 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30 3b 0a  w->iOffset = 0;.
20a4a 20 20 70 4e 65 77 2d 3e 73 65 6c 46 6c 61 67 73    pNew->selFlags
20a4b 20 3d 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26   = p->selFlags &
20a4c 20 7e 53 46 5f 55 73 65 73 45 70 68 65 6d 65 72   ~SF_UsesEphemer
20a4d 61 6c 3b 0a 20 20 70 4e 65 77 2d 3e 70 52 69 67  al;.  pNew->pRig
20a4e 68 74 6d 6f 73 74 20 3d 20 30 3b 0a 20 20 70 4e  htmost = 0;.  pN
20a4f 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d  ew->addrOpenEphm
20a50 5b 30 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e 65 77  [0] = -1;.  pNew
20a51 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 31  ->addrOpenEphm[1
20a52 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e 65 77 2d 3e  ] = -1;.  pNew->
20a53 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 32 5d 20  addrOpenEphm[2] 
20a54 3d 20 2d 31 3b 0a 20 20 72 65 74 75 72 6e 20 70  = -1;.  return p
20a55 4e 65 77 3b 0a 7d 0a 23 65 6c 73 65 0a 53 51 4c  New;.}.#else.SQL
20a56 49 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65  ITE_PRIVATE Sele
20a57 63 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63  ct *sqlite3Selec
20a58 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62  tDup(sqlite3 *db
20a59 2c 20 53 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74  , Select *p, int
20a5a 20 66 6c 61 67 73 29 7b 0a 20 20 61 73 73 65 72   flags){.  asser
20a5b 74 28 20 70 3d 3d 30 20 29 3b 0a 20 20 72 65 74  t( p==0 );.  ret
20a5c 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a  urn 0;.}.#endif.
20a5d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65  ../*.** Add a ne
20a5e 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68 65  w element to the
20a5f 20 65 6e 64 20 6f 66 20 61 6e 20 65 78 70 72 65   end of an expre
20a60 73 73 69 6f 6e 20 6c 69 73 74 2e 20 20 49 66 20  ssion list.  If 
20a61 70 4c 69 73 74 20 69 73 0a 2a 2a 20 69 6e 69 74  pList is.** init
20a62 69 61 6c 6c 79 20 4e 55 4c 4c 2c 20 74 68 65 6e  ially NULL, then
20a63 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 65 78   create a new ex
20a64 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a  pression list..*
20a65 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79  *.** If a memory
20a66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f   allocation erro
20a67 72 20 6f 63 63 75 72 73 2c 20 74 68 65 20 65 6e  r occurs, the en
20a68 74 69 72 65 20 6c 69 73 74 20 69 73 20 66 72 65  tire list is fre
20a69 65 64 20 61 6e 64 0a 2a 2a 20 4e 55 4c 4c 20 69  ed and.** NULL i
20a6a 73 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20  s returned.  If 
20a6b 6e 6f 6e 2d 4e 55 4c 4c 20 69 73 20 72 65 74 75  non-NULL is retu
20a6c 72 6e 65 64 2c 20 74 68 65 6e 20 69 74 20 69 73  rned, then it is
20a6d 20 67 75 61 72 61 6e 74 65 65 64 0a 2a 2a 20 74   guaranteed.** t
20a6e 68 61 74 20 74 68 65 20 6e 65 77 20 65 6e 74 72  hat the new entr
20a6f 79 20 77 61 73 20 73 75 63 63 65 73 73 66 75 6c  y was successful
20a70 6c 79 20 61 70 70 65 6e 64 65 64 2e 0a 2a 2f 0a  ly appended..*/.
20a71 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45  SQLITE_PRIVATE E
20a72 78 70 72 4c 69 73 74 20 2a 73 71 6c 69 74 65 33  xprList *sqlite3
20a73 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 0a  ExprListAppend(.
20a74 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
20a75 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72            /* Par
20a76 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
20a77 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73    ExprList *pLis
20a78 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73  t,        /* Lis
20a79 74 20 74 6f 20 77 68 69 63 68 20 74 6f 20 61 70  t to which to ap
20a7a 70 65 6e 64 2e 20 4d 69 67 68 74 20 62 65 20 4e  pend. Might be N
20a7b 55 4c 4c 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ULL */.  Expr *p
20a7c 45 78 70 72 20 20 20 20 20 20 20 20 20 20 20 20  Expr            
20a7d 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 74   /* Expression t
20a7e 6f 20 62 65 20 61 70 70 65 6e 64 65 64 2e 20 4d  o be appended. M
20a7f 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a  ight be NULL */.
20a80 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
20a81 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
20a82 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b   if( pList==0 ){
20a83 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 73 71 6c  .    pList = sql
20a84 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
20a85 28 64 62 2c 20 73 69 7a 65 6f 66 28 45 78 70 72  (db, sizeof(Expr
20a86 4c 69 73 74 29 20 29 3b 0a 20 20 20 20 69 66 28  List) );.    if(
20a87 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20   pList==0 ){.   
20a88 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a     goto no_mem;.
20a89 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
20a8a 28 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3d  ( pList->nAlloc=
20a8b 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  =0 );.  }.  if( 
20a8c 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3c 3d 70  pList->nAlloc<=p
20a8d 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20  List->nExpr ){. 
20a8e 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69     struct ExprLi
20a8f 73 74 5f 69 74 65 6d 20 2a 61 3b 0a 20 20 20 20  st_item *a;.    
20a90 69 6e 74 20 6e 20 3d 20 70 4c 69 73 74 2d 3e 6e  int n = pList->n
20a91 41 6c 6c 6f 63 2a 32 20 2b 20 34 3b 0a 20 20 20  Alloc*2 + 4;.   
20a92 20 61 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65   a = sqlite3DbRe
20a93 61 6c 6c 6f 63 28 64 62 2c 20 70 4c 69 73 74 2d  alloc(db, pList-
20a94 3e 61 2c 20 6e 2a 73 69 7a 65 6f 66 28 70 4c 69  >a, n*sizeof(pLi
20a95 73 74 2d 3e 61 5b 30 5d 29 29 3b 0a 20 20 20 20  st->a[0]));.    
20a96 69 66 28 20 61 3d 3d 30 20 29 7b 0a 20 20 20 20  if( a==0 ){.    
20a97 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20    goto no_mem;. 
20a98 20 20 20 7d 0a 20 20 20 20 70 4c 69 73 74 2d 3e     }.    pList->
20a99 61 20 3d 20 61 3b 0a 20 20 20 20 70 4c 69 73 74  a = a;.    pList
20a9a 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74  ->nAlloc = sqlit
20a9b 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 64  e3DbMallocSize(d
20a9c 62 2c 20 61 29 2f 73 69 7a 65 6f 66 28 61 5b 30  b, a)/sizeof(a[0
20a9d 5d 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  ]);.  }.  assert
20a9e 28 20 70 4c 69 73 74 2d 3e 61 21 3d 30 20 29 3b  ( pList->a!=0 );
20a9f 0a 20 20 69 66 28 20 31 20 29 7b 0a 20 20 20 20  .  if( 1 ){.    
20aa0 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
20aa1 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70  item *pItem = &p
20aa2 4c 69 73 74 2d 3e 61 5b 70 4c 69 73 74 2d 3e 6e  List->a[pList->n
20aa3 45 78 70 72 2b 2b 5d 3b 0a 20 20 20 20 6d 65 6d  Expr++];.    mem
20aa4 73 65 74 28 70 49 74 65 6d 2c 20 30 2c 20 73 69  set(pItem, 0, si
20aa5 7a 65 6f 66 28 2a 70 49 74 65 6d 29 29 3b 0a 20  zeof(*pItem));. 
20aa6 20 20 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 20     pItem->pExpr 
20aa7 3d 20 70 45 78 70 72 3b 0a 20 20 7d 0a 20 20 72  = pExpr;.  }.  r
20aa8 65 74 75 72 6e 20 70 4c 69 73 74 3b 0a 0a 6e 6f  eturn pList;..no
20aa9 5f 6d 65 6d 3a 20 20 20 20 20 0a 20 20 2f 2a 20  _mem:     .  /* 
20aaa 41 76 6f 69 64 20 6c 65 61 6b 69 6e 67 20 6d 65  Avoid leaking me
20aab 6d 6f 72 79 20 69 66 20 6d 61 6c 6c 6f 63 20 68  mory if malloc h
20aac 61 73 20 66 61 69 6c 65 64 2e 20 2a 2f 0a 20 20  as failed. */.  
20aad 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
20aae 65 28 64 62 2c 20 70 45 78 70 72 29 3b 0a 20 20  e(db, pExpr);.  
20aaf 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
20ab0 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73 74 29  elete(db, pList)
20ab1 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  ;.  return 0;.}.
20ab2 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 45  ./*.** Set the E
20ab3 78 70 72 4c 69 73 74 2e 61 5b 5d 2e 7a 4e 61 6d  xprList.a[].zNam
20ab4 65 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65  e element of the
20ab5 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 61   most recently a
20ab6 64 64 65 64 20 69 74 65 6d 0a 2a 2a 20 6f 6e 20  dded item.** on 
20ab7 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c  the expression l
20ab8 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 70 4c 69 73 74  ist..**.** pList
20ab9 20 6d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 66   might be NULL f
20aba 6f 6c 6c 6f 77 69 6e 67 20 61 6e 20 4f 4f 4d 20  ollowing an OOM 
20abb 65 72 72 6f 72 2e 20 20 42 75 74 20 70 4e 61 6d  error.  But pNam
20abc 65 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62  e should never b
20abd 65 0a 2a 2a 20 4e 55 4c 4c 2e 20 20 49 66 20 61  e.** NULL.  If a
20abe 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
20abf 6f 6e 20 66 61 69 6c 73 2c 20 74 68 65 20 70 50  on fails, the pP
20ac0 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  arse->db->malloc
20ac1 46 61 69 6c 65 64 20 66 6c 61 67 0a 2a 2a 20 69  Failed flag.** i
20ac2 73 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  s set..*/.SQLITE
20ac3 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
20ac4 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53 65 74  lite3ExprListSet
20ac5 4e 61 6d 65 28 0a 20 20 50 61 72 73 65 20 2a 70  Name(.  Parse *p
20ac6 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
20ac7 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
20ac8 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  xt */.  ExprList
20ac9 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 20 20 20   *pList,        
20aca 2f 2a 20 4c 69 73 74 20 74 6f 20 77 68 69 63 68  /* List to which
20acb 20 74 6f 20 61 64 64 20 74 68 65 20 73 70 61 6e   to add the span
20acc 2e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e  . */.  Token *pN
20acd 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f  ame,           /
20ace 2a 20 4e 61 6d 65 20 74 6f 20 62 65 20 61 64 64  * Name to be add
20acf 65 64 20 2a 2f 0a 20 20 69 6e 74 20 64 65 71 75  ed */.  int dequ
20ad0 6f 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ote             
20ad1 2f 2a 20 54 72 75 65 20 74 6f 20 63 61 75 73 65  /* True to cause
20ad2 20 74 68 65 20 6e 61 6d 65 20 74 6f 20 62 65 20   the name to be 
20ad3 64 65 71 75 6f 74 65 64 20 2a 2f 0a 29 7b 0a 20  dequoted */.){. 
20ad4 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 21 3d   assert( pList!=
20ad5 30 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d  0 || pParse->db-
20ad6 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 21 3d 30  >mallocFailed!=0
20ad7 20 29 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 20   );.  if( pList 
20ad8 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 45 78  ){.    struct Ex
20ad9 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  prList_item *pIt
20ada 65 6d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  em;.    assert( 
20adb 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 30 20 29  pList->nExpr>0 )
20adc 3b 0a 20 20 20 20 70 49 74 65 6d 20 3d 20 26 70  ;.    pItem = &p
20add 4c 69 73 74 2d 3e 61 5b 70 4c 69 73 74 2d 3e 6e  List->a[pList->n
20ade 45 78 70 72 2d 31 5d 3b 0a 20 20 20 20 61 73 73  Expr-1];.    ass
20adf 65 72 74 28 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d  ert( pItem->zNam
20ae0 65 3d 3d 30 20 29 3b 0a 20 20 20 20 70 49 74 65  e==0 );.    pIte
20ae1 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74  m->zName = sqlit
20ae2 65 33 44 62 53 74 72 4e 44 75 70 28 70 50 61 72  e3DbStrNDup(pPar
20ae3 73 65 2d 3e 64 62 2c 20 70 4e 61 6d 65 2d 3e 7a  se->db, pName->z
20ae4 2c 20 70 4e 61 6d 65 2d 3e 6e 29 3b 0a 20 20 20  , pName->n);.   
20ae5 20 69 66 28 20 64 65 71 75 6f 74 65 20 26 26 20   if( dequote && 
20ae6 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 29 20 73  pItem->zName ) s
20ae7 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 70 49  qlite3Dequote(pI
20ae8 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7d  tem->zName);.  }
20ae9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68  .}../*.** Set th
20aea 65 20 45 78 70 72 4c 69 73 74 2e 61 5b 5d 2e 7a  e ExprList.a[].z
20aeb 53 70 61 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 20  Span element of 
20aec 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c  the most recentl
20aed 79 20 61 64 64 65 64 20 69 74 65 6d 0a 2a 2a 20  y added item.** 
20aee 6f 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  on the expressio
20aef 6e 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 70 4c  n list..**.** pL
20af0 69 73 74 20 6d 69 67 68 74 20 62 65 20 4e 55 4c  ist might be NUL
20af1 4c 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 6e 20 4f  L following an O
20af2 4f 4d 20 65 72 72 6f 72 2e 20 20 42 75 74 20 70  OM error.  But p
20af3 53 70 61 6e 20 73 68 6f 75 6c 64 20 6e 65 76 65  Span should neve
20af4 72 20 62 65 0a 2a 2a 20 4e 55 4c 4c 2e 20 20 49  r be.** NULL.  I
20af5 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  f a memory alloc
20af6 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 74 68 65  ation fails, the
20af7 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c   pParse->db->mal
20af8 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 0a 2a  locFailed flag.*
20af9 2a 20 69 73 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c  * is set..*/.SQL
20afa 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
20afb 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
20afc 53 65 74 53 70 61 6e 28 0a 20 20 50 61 72 73 65  SetSpan(.  Parse
20afd 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
20afe 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
20aff 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c  ntext */.  ExprL
20b00 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20  ist *pList,     
20b01 20 20 20 2f 2a 20 4c 69 73 74 20 74 6f 20 77 68     /* List to wh
20b02 69 63 68 20 74 6f 20 61 64 64 20 74 68 65 20 73  ich to add the s
20b03 70 61 6e 2e 20 2a 2f 0a 20 20 45 78 70 72 53 70  pan. */.  ExprSp
20b04 61 6e 20 2a 70 53 70 61 6e 20 20 20 20 20 20 20  an *pSpan       
20b05 20 20 2f 2a 20 54 68 65 20 73 70 61 6e 20 74 6f    /* The span to
20b06 20 62 65 20 61 64 64 65 64 20 2a 2f 0a 29 7b 0a   be added */.){.
20b07 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
20b08 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73  pParse->db;.  as
20b09 73 65 72 74 28 20 70 4c 69 73 74 21 3d 30 20 7c  sert( pList!=0 |
20b0a 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
20b0b 65 64 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70  ed!=0 );.  if( p
20b0c 4c 69 73 74 20 29 7b 0a 20 20 20 20 73 74 72 75  List ){.    stru
20b0d 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d  ct ExprList_item
20b0e 20 2a 70 49 74 65 6d 20 3d 20 26 70 4c 69 73 74   *pItem = &pList
20b0f 2d 3e 61 5b 70 4c 69 73 74 2d 3e 6e 45 78 70 72  ->a[pList->nExpr
20b10 2d 31 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28  -1];.    assert(
20b11 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 30 20   pList->nExpr>0 
20b12 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64  );.    assert( d
20b13 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
20b14 7c 7c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 3d  || pItem->pExpr=
20b15 3d 70 53 70 61 6e 2d 3e 70 45 78 70 72 20 29 3b  =pSpan->pExpr );
20b16 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
20b17 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 53  ee(db, pItem->zS
20b18 70 61 6e 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d  pan);.    pItem-
20b19 3e 7a 53 70 61 6e 20 3d 20 73 71 6c 69 74 65 33  >zSpan = sqlite3
20b1a 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28 63  DbStrNDup(db, (c
20b1b 68 61 72 2a 29 70 53 70 61 6e 2d 3e 7a 53 74 61  har*)pSpan->zSta
20b1c 72 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  rt,.            
20b1d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20b1e 20 20 20 20 20 20 20 20 28 69 6e 74 29 28 70 53          (int)(pS
20b1f 70 61 6e 2d 3e 7a 45 6e 64 20 2d 20 70 53 70 61  pan->zEnd - pSpa
20b20 6e 2d 3e 7a 53 74 61 72 74 29 29 3b 0a 20 20 7d  n->zStart));.  }
20b21 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  .}../*.** If the
20b22 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74   expression list
20b23 20 70 45 4c 69 73 74 20 63 6f 6e 74 61 69 6e 73   pEList contains
20b24 20 6d 6f 72 65 20 74 68 61 6e 20 69 4c 69 6d 69   more than iLimi
20b25 74 20 65 6c 65 6d 65 6e 74 73 2c 0a 2a 2a 20 6c  t elements,.** l
20b26 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65  eave an error me
20b27 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 2e  ssage in pParse.
20b28 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
20b29 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
20b2a 78 70 72 4c 69 73 74 43 68 65 63 6b 4c 65 6e 67  xprListCheckLeng
20b2b 74 68 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  th(.  Parse *pPa
20b2c 72 73 65 2c 0a 20 20 45 78 70 72 4c 69 73 74 20  rse,.  ExprList 
20b2d 2a 70 45 4c 69 73 74 2c 0a 20 20 63 6f 6e 73 74  *pEList,.  const
20b2e 20 63 68 61 72 20 2a 7a 4f 62 6a 65 63 74 0a 29   char *zObject.)
20b2f 7b 0a 20 20 69 6e 74 20 6d 78 20 3d 20 70 50 61  {.  int mx = pPa
20b30 72 73 65 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b  rse->db->aLimit[
20b31 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c  SQLITE_LIMIT_COL
20b32 55 4d 4e 5d 3b 0a 20 20 74 65 73 74 63 61 73 65  UMN];.  testcase
20b33 28 20 70 45 4c 69 73 74 20 26 26 20 70 45 4c 69  ( pEList && pELi
20b34 73 74 2d 3e 6e 45 78 70 72 3d 3d 6d 78 20 29 3b  st->nExpr==mx );
20b35 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 45 4c  .  testcase( pEL
20b36 69 73 74 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e  ist && pEList->n
20b37 45 78 70 72 3d 3d 6d 78 2b 31 20 29 3b 0a 20 20  Expr==mx+1 );.  
20b38 69 66 28 20 70 45 4c 69 73 74 20 26 26 20 70 45  if( pEList && pE
20b39 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 6d 78 20 29  List->nExpr>mx )
20b3a 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
20b3b 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74  orMsg(pParse, "t
20b3c 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73 20  oo many columns 
20b3d 69 6e 20 25 73 22 2c 20 7a 4f 62 6a 65 63 74 29  in %s", zObject)
20b3e 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  ;.  }.}../*.** D
20b3f 65 6c 65 74 65 20 61 6e 20 65 6e 74 69 72 65 20  elete an entire 
20b40 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e  expression list.
20b41 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
20b42 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
20b43 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 73 71  xprListDelete(sq
20b44 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 4c  lite3 *db, ExprL
20b45 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69  ist *pList){.  i
20b46 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 45  nt i;.  struct E
20b47 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  xprList_item *pI
20b48 74 65 6d 3b 0a 20 20 69 66 28 20 70 4c 69 73 74  tem;.  if( pList
20b49 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
20b4a 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e 61  assert( pList->a
20b4b 21 3d 30 20 7c 7c 20 28 70 4c 69 73 74 2d 3e 6e  !=0 || (pList->n
20b4c 45 78 70 72 3d 3d 30 20 26 26 20 70 4c 69 73 74  Expr==0 && pList
20b4d 2d 3e 6e 41 6c 6c 6f 63 3d 3d 30 29 20 29 3b 0a  ->nAlloc==0) );.
20b4e 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d    assert( pList-
20b4f 3e 6e 45 78 70 72 3c 3d 70 4c 69 73 74 2d 3e 6e  >nExpr<=pList->n
20b50 41 6c 6c 6f 63 20 29 3b 0a 20 20 66 6f 72 28 70  Alloc );.  for(p
20b51 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20 69  Item=pList->a, i
20b52 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78  =0; i<pList->nEx
20b53 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b  pr; i++, pItem++
20b54 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  ){.    sqlite3Ex
20b55 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 49 74  prDelete(db, pIt
20b56 65 6d 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20 20  em->pExpr);.    
20b57 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
20b58 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b  , pItem->zName);
20b59 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
20b5a 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 53  ee(db, pItem->zS
20b5b 70 61 6e 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  pan);.  }.  sqli
20b5c 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c  te3DbFree(db, pL
20b5d 69 73 74 2d 3e 61 29 3b 0a 20 20 73 71 6c 69 74  ist->a);.  sqlit
20b5e 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c 69  e3DbFree(db, pLi
20b5f 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  st);.}../*.** Th
20b60 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65  ese routines are
20b61 20 57 61 6c 6b 65 72 20 63 61 6c 6c 62 61 63 6b   Walker callback
20b62 73 2e 20 20 57 61 6c 6b 65 72 2e 75 2e 70 69 20  s.  Walker.u.pi 
20b63 69 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20  is a pointer.** 
20b64 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20  to an integer.  
20b65 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61  These routines a
20b66 72 65 20 63 68 65 63 6b 69 6e 67 20 61 6e 20 65  re checking an e
20b67 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 73 65 65  xpression to see
20b68 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 61 20 63  .** if it is a c
20b69 6f 6e 73 74 61 6e 74 2e 20 20 53 65 74 20 2a 57  onstant.  Set *W
20b6a 61 6c 6b 65 72 2e 75 2e 70 69 20 74 6f 20 30 20  alker.u.pi to 0 
20b6b 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  if the expressio
20b6c 6e 20 69 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 73  n is.** not cons
20b6d 74 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73  tant..**.** Thes
20b6e 65 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69  e callback routi
20b6f 6e 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20  nes are used to 
20b70 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 66 6f  implement the fo
20b71 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  llowing:.**.**  
20b72 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 73     sqlite3ExprIs
20b73 43 6f 6e 73 74 61 6e 74 28 29 0a 2a 2a 20 20 20  Constant().**   
20b74 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43    sqlite3ExprIsC
20b75 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 29  onstantNotJoin()
20b76 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 45  .**     sqlite3E
20b77 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f 72 46  xprIsConstantOrF
20b78 75 6e 63 74 69 6f 6e 28 29 0a 2a 2a 0a 2a 2f 0a  unction().**.*/.
20b79 73 74 61 74 69 63 20 69 6e 74 20 65 78 70 72 4e  static int exprN
20b7a 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 28 57 61  odeIsConstant(Wa
20b7b 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45  lker *pWalker, E
20b7c 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 0a 20 20  xpr *pExpr){..  
20b7d 2f 2a 20 49 66 20 70 57 61 6c 6b 65 72 2d 3e 75  /* If pWalker->u
20b7e 2e 69 20 69 73 20 33 20 74 68 65 6e 20 61 6e 79  .i is 3 then any
20b7f 20 74 65 72 6d 20 6f 66 20 74 68 65 20 65 78 70   term of the exp
20b80 72 65 73 73 69 6f 6e 20 74 68 61 74 20 63 6f 6d  ression that com
20b81 65 73 20 66 72 6f 6d 0a 20 20 2a 2a 20 74 68 65  es from.  ** the
20b82 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61   ON or USING cla
20b83 75 73 65 73 20 6f 66 20 61 20 6a 6f 69 6e 20 64  uses of a join d
20b84 69 73 71 75 61 6c 69 66 69 65 73 20 74 68 65 20  isqualifies the 
20b85 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2a 20  expression.  ** 
20b86 66 72 6f 6d 20 62 65 69 6e 67 20 63 6f 6e 73 69  from being consi
20b87 64 65 72 65 64 20 63 6f 6e 73 74 61 6e 74 2e 20  dered constant. 
20b88 2a 2f 0a 20 20 69 66 28 20 70 57 61 6c 6b 65 72  */.  if( pWalker
20b89 2d 3e 75 2e 69 3d 3d 33 20 26 26 20 45 78 70 72  ->u.i==3 && Expr
20b8a 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70  HasAnyProperty(p
20b8b 45 78 70 72 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69  Expr, EP_FromJoi
20b8c 6e 29 20 29 7b 0a 20 20 20 20 70 57 61 6c 6b 65  n) ){.    pWalke
20b8d 72 2d 3e 75 2e 69 20 3d 20 30 3b 0a 20 20 20 20  r->u.i = 0;.    
20b8e 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
20b8f 3b 0a 20 20 7d 0a 0a 20 20 73 77 69 74 63 68 28  ;.  }..  switch(
20b90 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20   pExpr->op ){.  
20b91 20 20 2f 2a 20 43 6f 6e 73 69 64 65 72 20 66 75    /* Consider fu
20b92 6e 63 74 69 6f 6e 73 20 74 6f 20 62 65 20 63 6f  nctions to be co
20b93 6e 73 74 61 6e 74 20 69 66 20 61 6c 6c 20 74 68  nstant if all th
20b94 65 69 72 20 61 72 67 75 6d 65 6e 74 73 20 61 72  eir arguments ar
20b95 65 20 63 6f 6e 73 74 61 6e 74 0a 20 20 20 20 2a  e constant.    *
20b96 2a 20 61 6e 64 20 70 57 61 6c 6b 65 72 2d 3e 75  * and pWalker->u
20b97 2e 69 3d 3d 32 20 2a 2f 0a 20 20 20 20 63 61 73  .i==2 */.    cas
20b98 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20  e TK_FUNCTION:. 
20b99 20 20 20 20 20 69 66 28 20 70 57 61 6c 6b 65 72       if( pWalker
20b9a 2d 3e 75 2e 69 3d 3d 32 20 29 20 72 65 74 75 72  ->u.i==2 ) retur
20b9b 6e 20 30 3b 0a 20 20 20 20 20 20 2f 2a 20 46 61  n 0;.      /* Fa
20b9c 6c 6c 20 74 68 72 6f 75 67 68 20 2a 2f 0a 20 20  ll through */.  
20b9d 20 20 63 61 73 65 20 54 4b 5f 49 44 3a 0a 20 20    case TK_ID:.  
20b9e 20 20 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e    case TK_COLUMN
20b9f 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47  :.    case TK_AG
20ba0 47 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20 20  G_FUNCTION:.    
20ba1 63 61 73 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55  case TK_AGG_COLU
20ba2 4d 4e 3a 0a 20 20 20 20 20 20 74 65 73 74 63 61  MN:.      testca
20ba3 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54  se( pExpr->op==T
20ba4 4b 5f 49 44 20 29 3b 0a 20 20 20 20 20 20 74 65  K_ID );.      te
20ba5 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f  stcase( pExpr->o
20ba6 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a  p==TK_COLUMN );.
20ba7 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
20ba8 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 41 47  pExpr->op==TK_AG
20ba9 47 5f 46 55 4e 43 54 49 4f 4e 20 29 3b 0a 20 20  G_FUNCTION );.  
20baa 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45      testcase( pE
20bab 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 41 47 47 5f  xpr->op==TK_AGG_
20bac 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20  COLUMN );.      
20bad 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 3d 20 30  pWalker->u.i = 0
20bae 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 57  ;.      return W
20baf 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 64 65  RC_Abort;.    de
20bb0 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 74 65 73  fault:.      tes
20bb1 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70  tcase( pExpr->op
20bb2 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 29 3b 20 2f  ==TK_SELECT ); /
20bb3 2a 20 73 65 6c 65 63 74 4e 6f 64 65 49 73 43 6f  * selectNodeIsCo
20bb4 6e 73 74 61 6e 74 20 77 69 6c 6c 20 64 69 73 61  nstant will disa
20bb5 6c 6c 6f 77 20 2a 2f 0a 20 20 20 20 20 20 74 65  llow */.      te
20bb6 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f  stcase( pExpr->o
20bb7 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20 29 3b 20  p==TK_EXISTS ); 
20bb8 2f 2a 20 73 65 6c 65 63 74 4e 6f 64 65 49 73 43  /* selectNodeIsC
20bb9 6f 6e 73 74 61 6e 74 20 77 69 6c 6c 20 64 69 73  onstant will dis
20bba 61 6c 6c 6f 77 20 2a 2f 0a 20 20 20 20 20 20 72  allow */.      r
20bbb 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e  eturn WRC_Contin
20bbc 75 65 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63  ue;.  }.}.static
20bbd 20 69 6e 74 20 73 65 6c 65 63 74 4e 6f 64 65 49   int selectNodeI
20bbe 73 43 6f 6e 73 74 61 6e 74 28 57 61 6c 6b 65 72  sConstant(Walker
20bbf 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63   *pWalker, Selec
20bc0 74 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55  t *NotUsed){.  U
20bc1 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
20bc2 4e 6f 74 55 73 65 64 29 3b 0a 20 20 70 57 61 6c  NotUsed);.  pWal
20bc3 6b 65 72 2d 3e 75 2e 69 20 3d 20 30 3b 0a 20 20  ker->u.i = 0;.  
20bc4 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
20bc5 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 65  ;.}.static int e
20bc6 78 70 72 49 73 43 6f 6e 73 74 28 45 78 70 72 20  xprIsConst(Expr 
20bc7 2a 70 2c 20 69 6e 74 20 69 6e 69 74 46 6c 61 67  *p, int initFlag
20bc8 29 7b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a 20  ){.  Walker w;. 
20bc9 20 77 2e 75 2e 69 20 3d 20 69 6e 69 74 46 6c 61   w.u.i = initFla
20bca 67 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 6c  g;.  w.xExprCall
20bcb 62 61 63 6b 20 3d 20 65 78 70 72 4e 6f 64 65 49  back = exprNodeI
20bcc 73 43 6f 6e 73 74 61 6e 74 3b 0a 20 20 77 2e 78  sConstant;.  w.x
20bcd 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d  SelectCallback =
20bce 20 73 65 6c 65 63 74 4e 6f 64 65 49 73 43 6f 6e   selectNodeIsCon
20bcf 73 74 61 6e 74 3b 0a 20 20 73 71 6c 69 74 65 33  stant;.  sqlite3
20bd0 57 61 6c 6b 45 78 70 72 28 26 77 2c 20 70 29 3b  WalkExpr(&w, p);
20bd1 0a 20 20 72 65 74 75 72 6e 20 77 2e 75 2e 69 3b  .  return w.u.i;
20bd2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61  .}../*.** Walk a
20bd3 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65  n expression tre
20bd4 65 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66 20  e.  Return 1 if 
20bd5 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
20bd6 73 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 61 6e  s constant.** an
20bd7 64 20 30 20 69 66 20 69 74 20 69 6e 76 6f 6c 76  d 0 if it involv
20bd8 65 73 20 76 61 72 69 61 62 6c 65 73 20 6f 72 20  es variables or 
20bd9 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 2e 0a  function calls..
20bda 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 75  **.** For the pu
20bdb 72 70 6f 73 65 73 20 6f 66 20 74 68 69 73 20 66  rposes of this f
20bdc 75 6e 63 74 69 6f 6e 2c 20 61 20 64 6f 75 62 6c  unction, a doubl
20bdd 65 2d 71 75 6f 74 65 64 20 73 74 72 69 6e 67 20  e-quoted string 
20bde 28 65 78 3a 20 22 61 62 63 22 29 0a 2a 2a 20 69  (ex: "abc").** i
20bdf 73 20 63 6f 6e 73 69 64 65 72 65 64 20 61 20 76  s considered a v
20be0 61 72 69 61 62 6c 65 20 62 75 74 20 61 20 73 69  ariable but a si
20be1 6e 67 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69  ngle-quoted stri
20be2 6e 67 20 28 65 78 3a 20 27 61 62 63 27 29 20 69  ng (ex: 'abc') i
20be3 73 0a 2a 2a 20 61 20 63 6f 6e 73 74 61 6e 74 2e  s.** a constant.
20be4 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
20be5 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78  TE int sqlite3Ex
20be6 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 45 78 70  prIsConstant(Exp
20be7 72 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20  r *p){.  return 
20be8 65 78 70 72 49 73 43 6f 6e 73 74 28 70 2c 20 31  exprIsConst(p, 1
20be9 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b  );.}../*.** Walk
20bea 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74   an expression t
20beb 72 65 65 2e 20 20 52 65 74 75 72 6e 20 31 20 69  ree.  Return 1 i
20bec 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
20bed 20 69 73 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20   is constant.** 
20bee 74 68 61 74 20 64 6f 65 73 20 6e 6f 20 6f 72 69  that does no ori
20bef 67 69 6e 61 74 65 20 66 72 6f 6d 20 74 68 65 20  ginate from the 
20bf0 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75  ON or USING clau
20bf1 73 65 73 20 6f 66 20 61 20 6a 6f 69 6e 2e 0a 2a  ses of a join..*
20bf2 2a 20 52 65 74 75 72 6e 20 30 20 69 66 20 69 74  * Return 0 if it
20bf3 20 69 6e 76 6f 6c 76 65 73 20 76 61 72 69 61 62   involves variab
20bf4 6c 65 73 20 6f 72 20 66 75 6e 63 74 69 6f 6e 20  les or function 
20bf5 63 61 6c 6c 73 20 6f 72 20 74 65 72 6d 73 20 66  calls or terms f
20bf6 72 6f 6d 0a 2a 2a 20 61 6e 20 4f 4e 20 6f 72 20  rom.** an ON or 
20bf7 55 53 49 4e 47 20 63 6c 61 75 73 65 2e 0a 2a 2f  USING clause..*/
20bf8 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
20bf9 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 49  int sqlite3ExprI
20bfa 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69 6e  sConstantNotJoin
20bfb 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 72 65 74  (Expr *p){.  ret
20bfc 75 72 6e 20 65 78 70 72 49 73 43 6f 6e 73 74 28  urn exprIsConst(
20bfd 70 2c 20 33 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  p, 3);.}../*.** 
20bfe 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 73 69  Walk an expressi
20bff 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e  on tree.  Return
20c00 20 31 20 69 66 20 74 68 65 20 65 78 70 72 65 73   1 if the expres
20c01 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74 61 6e 74  sion is constant
20c02 0a 2a 2a 20 6f 72 20 61 20 66 75 6e 63 74 69 6f  .** or a functio
20c03 6e 20 63 61 6c 6c 20 77 69 74 68 20 63 6f 6e 73  n call with cons
20c04 74 61 6e 74 20 61 72 67 75 6d 65 6e 74 73 2e 20  tant arguments. 
20c05 20 52 65 74 75 72 6e 20 61 6e 64 20 30 20 69 66   Return and 0 if
20c06 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 61 6e   there.** are an
20c07 79 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a 2a 0a  y variables..**.
20c08 2a 2a 20 46 6f 72 20 74 68 65 20 70 75 72 70 6f  ** For the purpo
20c09 73 65 73 20 6f 66 20 74 68 69 73 20 66 75 6e 63  ses of this func
20c0a 74 69 6f 6e 2c 20 61 20 64 6f 75 62 6c 65 2d 71  tion, a double-q
20c0b 75 6f 74 65 64 20 73 74 72 69 6e 67 20 28 65 78  uoted string (ex
20c0c 3a 20 22 61 62 63 22 29 0a 2a 2a 20 69 73 20 63  : "abc").** is c
20c0d 6f 6e 73 69 64 65 72 65 64 20 61 20 76 61 72 69  onsidered a vari
20c0e 61 62 6c 65 20 62 75 74 20 61 20 73 69 6e 67 6c  able but a singl
20c0f 65 2d 71 75 6f 74 65 64 20 73 74 72 69 6e 67 20  e-quoted string 
20c10 28 65 78 3a 20 27 61 62 63 27 29 20 69 73 0a 2a  (ex: 'abc') is.*
20c11 2a 20 61 20 63 6f 6e 73 74 61 6e 74 2e 0a 2a 2f  * a constant..*/
20c12 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
20c13 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 49  int sqlite3ExprI
20c14 73 43 6f 6e 73 74 61 6e 74 4f 72 46 75 6e 63 74  sConstantOrFunct
20c15 69 6f 6e 28 45 78 70 72 20 2a 70 29 7b 0a 20 20  ion(Expr *p){.  
20c16 72 65 74 75 72 6e 20 65 78 70 72 49 73 43 6f 6e  return exprIsCon
20c17 73 74 28 70 2c 20 32 29 3b 0a 7d 0a 0a 2f 2a 0a  st(p, 2);.}../*.
20c18 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73  ** If the expres
20c19 73 69 6f 6e 20 70 20 63 6f 64 65 73 20 61 20 63  sion p codes a c
20c1a 6f 6e 73 74 61 6e 74 20 69 6e 74 65 67 65 72 20  onstant integer 
20c1b 74 68 61 74 20 69 73 20 73 6d 61 6c 6c 20 65 6e  that is small en
20c1c 6f 75 67 68 0a 2a 2a 20 74 6f 20 66 69 74 20 69  ough.** to fit i
20c1d 6e 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67  n a 32-bit integ
20c1e 65 72 2c 20 72 65 74 75 72 6e 20 31 20 61 6e 64  er, return 1 and
20c1f 20 70 75 74 20 74 68 65 20 76 61 6c 75 65 20 6f   put the value o
20c20 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a  f the integer.**
20c21 20 69 6e 20 2a 70 56 61 6c 75 65 2e 20 20 49 66   in *pValue.  If
20c22 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
20c23 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67 65  is not an intege
20c24 72 20 6f 72 20 69 66 20 69 74 20 69 73 20 74 6f  r or if it is to
20c25 6f 20 62 69 67 0a 2a 2a 20 74 6f 20 66 69 74 20  o big.** to fit 
20c26 69 6e 20 61 20 73 69 67 6e 65 64 20 33 32 2d 62  in a signed 32-b
20c27 69 74 20 69 6e 74 65 67 65 72 2c 20 72 65 74 75  it integer, retu
20c28 72 6e 20 30 20 61 6e 64 20 6c 65 61 76 65 20 2a  rn 0 and leave *
20c29 70 56 61 6c 75 65 20 75 6e 63 68 61 6e 67 65 64  pValue unchanged
20c2a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
20c2b 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45  ATE int sqlite3E
20c2c 78 70 72 49 73 49 6e 74 65 67 65 72 28 45 78 70  xprIsInteger(Exp
20c2d 72 20 2a 70 2c 20 69 6e 74 20 2a 70 56 61 6c 75  r *p, int *pValu
20c2e 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 30  e){.  int rc = 0
20c2f 3b 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73  ;.  if( p->flags
20c30 20 26 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 29   & EP_IntValue )
20c31 7b 0a 20 20 20 20 2a 70 56 61 6c 75 65 20 3d 20  {.    *pValue = 
20c32 70 2d 3e 75 2e 69 56 61 6c 75 65 3b 0a 20 20 20  p->u.iValue;.   
20c33 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20   return 1;.  }. 
20c34 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70 20 29   switch( p->op )
20c35 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e  {.    case TK_IN
20c36 54 45 47 45 52 3a 20 7b 0a 20 20 20 20 20 20 72  TEGER: {.      r
20c37 63 20 3d 20 73 71 6c 69 74 65 33 47 65 74 49 6e  c = sqlite3GetIn
20c38 74 33 32 28 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c  t32(p->u.zToken,
20c39 20 70 56 61 6c 75 65 29 3b 0a 20 20 20 20 20 20   pValue);.      
20c3a 61 73 73 65 72 74 28 20 72 63 3d 3d 30 20 29 3b  assert( rc==0 );
20c3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
20c3c 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
20c3d 55 50 4c 55 53 3a 20 7b 0a 20 20 20 20 20 20 72  UPLUS: {.      r
20c3e 63 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 49  c = sqlite3ExprI
20c3f 73 49 6e 74 65 67 65 72 28 70 2d 3e 70 4c 65 66  sInteger(p->pLef
20c40 74 2c 20 70 56 61 6c 75 65 29 3b 0a 20 20 20 20  t, pValue);.    
20c41 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
20c42 20 20 20 63 61 73 65 20 54 4b 5f 55 4d 49 4e 55     case TK_UMINU
20c43 53 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 76  S: {.      int v
20c44 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  ;.      if( sqli
20c45 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72  te3ExprIsInteger
20c46 28 70 2d 3e 70 4c 65 66 74 2c 20 26 76 29 20 29  (p->pLeft, &v) )
20c47 7b 0a 20 20 20 20 20 20 20 20 2a 70 56 61 6c 75  {.        *pValu
20c48 65 20 3d 20 2d 76 3b 0a 20 20 20 20 20 20 20 20  e = -v;.        
20c49 72 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a  rc = 1;.      }.
20c4a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
20c4b 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20   }.    default: 
20c4c 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28  break;.  }.  if(
20c4d 20 72 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72   rc ){.    asser
20c4e 74 28 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f  t( ExprHasAnyPro
20c4f 70 65 72 74 79 28 70 2c 20 45 50 5f 52 65 64 75  perty(p, EP_Redu
20c50 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79  ced|EP_TokenOnly
20c51 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ).              
20c52 20 7c 7c 20 28 70 2d 3e 66 6c 61 67 73 32 20 26   || (p->flags2 &
20c53 20 45 50 32 5f 4d 61 6c 6c 6f 63 65 64 54 6f 6b   EP2_MallocedTok
20c54 65 6e 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70 2d  en)==0 );.    p-
20c55 3e 6f 70 20 3d 20 54 4b 5f 49 4e 54 45 47 45 52  >op = TK_INTEGER
20c56 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 7c  ;.    p->flags |
20c57 3d 20 45 50 5f 49 6e 74 56 61 6c 75 65 3b 0a 20  = EP_IntValue;. 
20c58 20 20 20 70 2d 3e 75 2e 69 56 61 6c 75 65 20 3d     p->u.iValue =
20c59 20 2a 70 56 61 6c 75 65 3b 0a 20 20 7d 0a 20 20   *pValue;.  }.  
20c5a 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
20c5b 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20  .** Return TRUE 
20c5c 69 66 20 74 68 65 20 67 69 76 65 6e 20 73 74 72  if the given str
20c5d 69 6e 67 20 69 73 20 61 20 72 6f 77 2d 69 64 20  ing is a row-id 
20c5e 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2e 0a 2a 2f 0a  column name..*/.
20c5f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
20c60 6e 74 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69  nt sqlite3IsRowi
20c61 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29  d(const char *z)
20c62 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  {.  if( sqlite3S
20c63 74 72 49 43 6d 70 28 7a 2c 20 22 5f 52 4f 57 49  trICmp(z, "_ROWI
20c64 44 5f 22 29 3d 3d 30 20 29 20 72 65 74 75 72 6e  D_")==0 ) return
20c65 20 31 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65   1;.  if( sqlite
20c66 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 52 4f 57  3StrICmp(z, "ROW
20c67 49 44 22 29 3d 3d 30 20 29 20 72 65 74 75 72 6e  ID")==0 ) return
20c68 20 31 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65   1;.  if( sqlite
20c69 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 4f 49 44  3StrICmp(z, "OID
20c6a 22 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31  ")==0 ) return 1
20c6b 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  ;.  return 0;.}.
20c6c 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72  ./*.** Return tr
20c6d 75 65 20 69 66 20 77 65 20 61 72 65 20 61 62 6c  ue if we are abl
20c6e 65 20 74 6f 20 74 68 65 20 49 4e 20 6f 70 65 72  e to the IN oper
20c6f 61 74 6f 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f  ator optimizatio
20c70 6e 20 6f 6e 20 61 0a 2a 2a 20 71 75 65 72 79 20  n on a.** query 
20c71 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a  of the form.**.*
20c72 2a 20 20 20 20 20 20 20 78 20 49 4e 20 28 53 45  *       x IN (SE
20c73 4c 45 43 54 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20  LECT ...).**.** 
20c74 57 68 65 72 65 20 74 68 65 20 53 45 4c 45 43 54  Where the SELECT
20c75 2e 2e 2e 20 63 6c 61 75 73 65 20 69 73 20 61 73  ... clause is as
20c76 20 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68   specified by th
20c77 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74  e parameter to t
20c78 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e 0a  his.** routine..
20c79 2a 2a 0a 2a 2a 20 54 68 65 20 53 65 6c 65 63 74  **.** The Select
20c7a 20 6f 62 6a 65 63 74 20 70 61 73 73 65 64 20 69   object passed i
20c7b 6e 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  n has already be
20c7c 65 6e 20 70 72 65 70 72 6f 63 65 73 73 65 64 20  en preprocessed 
20c7d 61 6e 64 20 6e 6f 0a 2a 2a 20 65 72 72 6f 72 73  and no.** errors
20c7e 20 68 61 76 65 20 62 65 65 6e 20 66 6f 75 6e 64   have been found
20c7f 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
20c80 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
20c81 59 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 43  Y.static int isC
20c82 61 6e 64 69 64 61 74 65 46 6f 72 49 6e 4f 70 74  andidateForInOpt
20c83 28 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 53  (Select *p){.  S
20c84 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 0a 20 20  rcList *pSrc;.  
20c85 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
20c86 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b  ;.  Table *pTab;
20c87 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65  .  if( p==0 ) re
20c88 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20  turn 0;         
20c89 20 20 20 20 20 20 20 20 20 20 2f 2a 20 72 69 67            /* rig
20c8a 68 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20  ht-hand side of 
20c8b 49 4e 20 69 73 20 53 45 4c 45 43 54 20 2a 2f 0a  IN is SELECT */.
20c8c 20 20 69 66 28 20 70 2d 3e 70 50 72 69 6f 72 20    if( p->pPrior 
20c8d 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20  ) return 0;     
20c8e 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20           /* Not 
20c8f 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43  a compound SELEC
20c90 54 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 73 65  T */.  if( p->se
20c91 6c 46 6c 61 67 73 20 26 20 28 53 46 5f 44 69 73  lFlags & (SF_Dis
20c92 74 69 6e 63 74 7c 53 46 5f 41 67 67 72 65 67 61  tinct|SF_Aggrega
20c93 74 65 29 20 29 7b 0a 20 20 20 20 74 65 73 74 63  te) ){.    testc
20c94 61 73 65 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67  ase( (p->selFlag
20c95 73 20 26 20 28 53 46 5f 44 69 73 74 69 6e 63 74  s & (SF_Distinct
20c96 7c 53 46 5f 41 67 67 72 65 67 61 74 65 29 29 3d  |SF_Aggregate))=
20c97 3d 53 46 5f 44 69 73 74 69 6e 63 74 20 29 3b 0a  =SF_Distinct );.
20c98 20 20 20 20 74 65 73 74 63 61 73 65 28 20 28 70      testcase( (p
20c99 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28 53 46  ->selFlags & (SF
20c9a 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41 67 67  _Distinct|SF_Agg
20c9b 72 65 67 61 74 65 29 29 3d 3d 53 46 5f 41 67 67  regate))==SF_Agg
20c9c 72 65 67 61 74 65 20 29 3b 0a 20 20 20 20 72 65  regate );.    re
20c9d 74 75 72 6e 20 30 3b 20 2f 2a 20 4e 6f 20 44 49  turn 0; /* No DI
20c9e 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 61  STINCT keyword a
20c9f 6e 64 20 6e 6f 20 61 67 67 72 65 67 61 74 65 20  nd no aggregate 
20ca0 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 7d  functions */.  }
20ca1 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 47  .  assert( p->pG
20ca2 72 6f 75 70 42 79 3d 3d 30 20 29 3b 20 20 20 20  roupBy==0 );    
20ca3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73            /* Has
20ca4 20 6e 6f 20 47 52 4f 55 50 20 42 59 20 63 6c 61   no GROUP BY cla
20ca5 75 73 65 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e  use */.  if( p->
20ca6 70 4c 69 6d 69 74 20 29 20 72 65 74 75 72 6e 20  pLimit ) return 
20ca7 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
20ca8 2f 2a 20 48 61 73 20 6e 6f 20 4c 49 4d 49 54 20  /* Has no LIMIT 
20ca9 63 6c 61 75 73 65 20 2a 2f 0a 20 20 61 73 73 65  clause */.  asse
20caa 72 74 28 20 70 2d 3e 70 4f 66 66 73 65 74 3d 3d  rt( p->pOffset==
20cab 30 20 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  0 );            
20cac 20 20 20 2f 2a 20 4e 6f 20 4c 49 4d 49 54 20 6d     /* No LIMIT m
20cad 65 61 6e 73 20 6e 6f 20 4f 46 46 53 45 54 20 2a  eans no OFFSET *
20cae 2f 0a 20 20 69 66 28 20 70 2d 3e 70 57 68 65 72  /.  if( p->pWher
20caf 65 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20  e ) return 0;   
20cb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61             /* Ha
20cb1 73 20 6e 6f 20 57 48 45 52 45 20 63 6c 61 75 73  s no WHERE claus
20cb2 65 20 2a 2f 0a 20 20 70 53 72 63 20 3d 20 70 2d  e */.  pSrc = p-
20cb3 3e 70 53 72 63 3b 0a 20 20 61 73 73 65 72 74 28  >pSrc;.  assert(
20cb4 20 70 53 72 63 21 3d 30 20 29 3b 0a 20 20 69 66   pSrc!=0 );.  if
20cb5 28 20 70 53 72 63 2d 3e 6e 53 72 63 21 3d 31 20  ( pSrc->nSrc!=1 
20cb6 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20  ) return 0;     
20cb7 20 20 20 20 20 2f 2a 20 53 69 6e 67 6c 65 20 74       /* Single t
20cb8 65 72 6d 20 69 6e 20 46 52 4f 4d 20 63 6c 61 75  erm in FROM clau
20cb9 73 65 20 2a 2f 0a 20 20 69 66 28 20 70 53 72 63  se */.  if( pSrc
20cba 2d 3e 61 5b 30 5d 2e 70 53 65 6c 65 63 74 20 29  ->a[0].pSelect )
20cbb 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 2f   return 0;     /
20cbc 2a 20 46 52 4f 4d 20 69 73 20 6e 6f 74 20 61 20  * FROM is not a 
20cbd 73 75 62 71 75 65 72 79 20 6f 72 20 76 69 65 77  subquery or view
20cbe 20 2a 2f 0a 20 20 70 54 61 62 20 3d 20 70 53 72   */.  pTab = pSr
20cbf 63 2d 3e 61 5b 30 5d 2e 70 54 61 62 3b 0a 20 20  c->a[0].pTab;.  
20cc0 69 66 28 20 4e 45 56 45 52 28 70 54 61 62 3d 3d  if( NEVER(pTab==
20cc1 30 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  0) ) return 0;. 
20cc2 20 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e 70   assert( pTab->p
20cc3 53 65 6c 65 63 74 3d 3d 30 20 29 3b 20 20 20 20  Select==0 );    
20cc4 20 20 20 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20          /* FROM 
20cc5 63 6c 61 75 73 65 20 69 73 20 6e 6f 74 20 61 20  clause is not a 
20cc6 76 69 65 77 20 2a 2f 0a 20 20 69 66 28 20 49 73  view */.  if( Is
20cc7 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 20  Virtual(pTab) ) 
20cc8 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20  return 0;       
20cc9 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20   /* FROM clause 
20cca 6e 6f 74 20 61 20 76 69 72 74 75 61 6c 20 74 61  not a virtual ta
20ccb 62 6c 65 20 2a 2f 0a 20 20 70 45 4c 69 73 74 20  ble */.  pEList 
20ccc 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 69  = p->pEList;.  i
20ccd 66 28 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  f( pEList->nExpr
20cce 21 3d 31 20 29 20 72 65 74 75 72 6e 20 30 3b 20  !=1 ) return 0; 
20ccf 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 63 6f 6c        /* One col
20cd0 75 6d 6e 20 69 6e 20 74 68 65 20 72 65 73 75 6c  umn in the resul
20cd1 74 20 73 65 74 20 2a 2f 0a 20 20 69 66 28 20 70  t set */.  if( p
20cd2 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70  EList->a[0].pExp
20cd3 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e  r->op!=TK_COLUMN
20cd4 20 29 20 72 65 74 75 72 6e 20 30 3b 20 2f 2a 20   ) return 0; /* 
20cd5 52 65 73 75 6c 74 20 69 73 20 61 20 63 6f 6c 75  Result is a colu
20cd6 6d 6e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 31  mn */.  return 1
20cd7 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
20cd8 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
20cd9 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  RY */../*.** Thi
20cda 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  s function is us
20cdb 65 64 20 62 79 20 74 68 65 20 69 6d 70 6c 65 6d  ed by the implem
20cdc 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
20cdd 49 4e 20 28 2e 2e 2e 29 20 6f 70 65 72 61 74 6f  IN (...) operato
20cde 72 2e 0a 2a 2a 20 49 74 27 73 20 6a 6f 62 20 69  r..** It's job i
20cdf 73 20 74 6f 20 66 69 6e 64 20 6f 72 20 63 72 65  s to find or cre
20ce0 61 74 65 20 61 20 62 2d 74 72 65 65 20 73 74 72  ate a b-tree str
20ce1 75 63 74 75 72 65 20 74 68 61 74 20 6d 61 79 20  ucture that may 
20ce2 62 65 20 75 73 65 64 0a 2a 2a 20 65 69 74 68 65  be used.** eithe
20ce3 72 20 74 6f 20 74 65 73 74 20 66 6f 72 20 6d 65  r to test for me
20ce4 6d 62 65 72 73 68 69 70 20 6f 66 20 74 68 65 20  mbership of the 
20ce5 28 2e 2e 2e 29 20 73 65 74 20 6f 72 20 74 6f 20  (...) set or to 
20ce6 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 0a  iterate through.
20ce7 2a 2a 20 69 74 73 20 6d 65 6d 62 65 72 73 2c 20  ** its members, 
20ce8 73 6b 69 70 70 69 6e 67 20 64 75 70 6c 69 63 61  skipping duplica
20ce9 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69  tes..**.** The i
20cea 6e 64 65 78 20 6f 66 20 74 68 65 20 63 75 72 73  ndex of the curs
20ceb 6f 72 20 6f 70 65 6e 65 64 20 6f 6e 20 74 68 65  or opened on the
20cec 20 62 2d 74 72 65 65 20 28 64 61 74 61 62 61 73   b-tree (databas
20ced 65 20 74 61 62 6c 65 2c 20 64 61 74 61 62 61 73  e table, databas
20cee 65 20 69 6e 64 65 78 20 0a 2a 2a 20 6f 72 20 65  e index .** or e
20cef 70 68 65 72 6d 61 6c 20 74 61 62 6c 65 29 20 69  phermal table) i
20cf0 73 20 73 74 6f 72 65 64 20 69 6e 20 70 58 2d 3e  s stored in pX->
20cf1 69 54 61 62 6c 65 20 62 65 66 6f 72 65 20 74 68  iTable before th
20cf2 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  is function retu
20cf3 72 6e 73 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75  rns..** The retu
20cf4 72 6e 65 64 20 76 61 6c 75 65 20 6f 66 20 74 68  rned value of th
20cf5 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e 64 69  is function indi
20cf6 63 61 74 65 73 20 74 68 65 20 62 2d 74 72 65 65  cates the b-tree
20cf7 20 74 79 70 65 2c 20 61 73 20 66 6f 6c 6c 6f 77   type, as follow
20cf8 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 49 4e 5f 49 4e  s:.**.**   IN_IN
20cf9 44 45 58 5f 52 4f 57 49 44 20 2d 20 54 68 65 20  DEX_ROWID - The 
20cfa 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65 6e 65  cursor was opene
20cfb 64 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20  d on a database 
20cfc 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 49 4e 5f 49  table..**   IN_I
20cfd 4e 44 45 58 5f 49 4e 44 45 58 20 2d 20 54 68 65  NDEX_INDEX - The
20cfe 20 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65 6e   cursor was open
20cff 65 64 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65  ed on a database
20d00 20 69 6e 64 65 78 2e 0a 2a 2a 20 20 20 49 4e 5f   index..**   IN_
20d01 49 4e 44 45 58 5f 45 50 48 20 2d 20 20 20 54 68  INDEX_EPH -   Th
20d02 65 20 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65  e cursor was ope
20d03 6e 65 64 20 6f 6e 20 61 20 73 70 65 63 69 61 6c  ned on a special
20d04 6c 79 20 63 72 65 61 74 65 64 20 61 6e 64 0a 2a  ly created and.*
20d05 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
20d06 20 20 20 20 20 70 6f 70 75 6c 61 74 65 64 20 65       populated e
20d07 70 68 65 72 65 6d 61 6c 20 74 61 62 6c 65 2e 0a  pheremal table..
20d08 2a 2a 0a 2a 2a 20 41 6e 20 65 78 69 73 74 69 6e  **.** An existin
20d09 67 20 62 2d 74 72 65 65 20 6d 61 79 20 6f 6e 6c  g b-tree may onl
20d0a 79 20 62 65 20 75 73 65 64 20 69 66 20 74 68 65  y be used if the
20d0b 20 53 45 4c 45 43 54 20 69 73 20 6f 66 20 74 68   SELECT is of th
20d0c 65 20 73 69 6d 70 6c 65 0a 2a 2a 20 66 6f 72 6d  e simple.** form
20d0d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45  :.**.**     SELE
20d0e 43 54 20 3c 63 6f 6c 75 6d 6e 3e 20 46 52 4f 4d  CT <column> FROM
20d0f 20 3c 74 61 62 6c 65 3e 0a 2a 2a 0a 2a 2a 20 49   <table>.**.** I
20d10 66 20 74 68 65 20 70 72 4e 6f 74 46 6f 75 6e 64  f the prNotFound
20d11 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 30 2c   parameter is 0,
20d12 20 74 68 65 6e 20 74 68 65 20 62 2d 74 72 65 65   then the b-tree
20d13 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 74 6f   will be used to
20d14 20 69 74 65 72 61 74 65 0a 2a 2a 20 74 68 72 6f   iterate.** thro
20d15 75 67 68 20 74 68 65 20 73 65 74 20 6d 65 6d 62  ugh the set memb
20d16 65 72 73 2c 20 73 6b 69 70 70 69 6e 67 20 61 6e  ers, skipping an
20d17 79 20 64 75 70 6c 69 63 61 74 65 73 2e 20 49 6e  y duplicates. In
20d18 20 74 68 69 73 20 63 61 73 65 20 61 6e 0a 2a 2a   this case an.**
20d19 20 65 70 68 65 72 65 6d 61 6c 20 74 61 62 6c 65   epheremal table
20d1a 20 6d 75 73 74 20 62 65 20 75 73 65 64 20 75 6e   must be used un
20d1b 6c 65 73 73 20 74 68 65 20 73 65 6c 65 63 74 65  less the selecte
20d1c 64 20 3c 63 6f 6c 75 6d 6e 3e 20 69 73 20 67 75  d <column> is gu
20d1d 61 72 61 6e 74 65 65 64 0a 2a 2a 20 74 6f 20 62  aranteed.** to b
20d1e 65 20 75 6e 69 71 75 65 20 2d 20 65 69 74 68 65  e unique - eithe
20d1f 72 20 62 65 63 61 75 73 65 20 69 74 20 69 73 20  r because it is 
20d20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  an INTEGER PRIMA
20d21 52 59 20 4b 45 59 20 6f 72 20 69 74 0a 2a 2a 20  RY KEY or it.** 
20d22 68 61 73 20 61 20 55 4e 49 51 55 45 20 63 6f 6e  has a UNIQUE con
20d23 73 74 72 61 69 6e 74 20 6f 72 20 55 4e 49 51 55  straint or UNIQU
20d24 45 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49  E index..**.** I
20d25 66 20 74 68 65 20 70 72 4e 6f 74 46 6f 75 6e 64  f the prNotFound
20d26 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f   parameter is no
20d27 74 20 30 2c 20 74 68 65 6e 20 74 68 65 20 62 2d  t 0, then the b-
20d28 74 72 65 65 20 77 69 6c 6c 20 62 65 20 75 73 65  tree will be use
20d29 64 20 0a 2a 2a 20 66 6f 72 20 66 61 73 74 20 73  d .** for fast s
20d2a 65 74 20 6d 65 6d 62 65 72 73 68 69 70 20 74 65  et membership te
20d2b 73 74 73 2e 20 49 6e 20 74 68 69 73 20 63 61 73  sts. In this cas
20d2c 65 20 61 6e 20 65 70 68 65 72 65 6d 61 6c 20 74  e an epheremal t
20d2d 61 62 6c 65 20 6d 75 73 74 20 0a 2a 2a 20 62 65  able must .** be
20d2e 20 75 73 65 64 20 75 6e 6c 65 73 73 20 3c 63 6f   used unless <co
20d2f 6c 75 6d 6e 3e 20 69 73 20 61 6e 20 49 4e 54 45  lumn> is an INTE
20d30 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20  GER PRIMARY KEY 
20d31 6f 72 20 61 6e 20 69 6e 64 65 78 20 63 61 6e 20  or an index can 
20d32 0a 2a 2a 20 62 65 20 66 6f 75 6e 64 20 77 69 74  .** be found wit
20d33 68 20 3c 63 6f 6c 75 6d 6e 3e 20 61 73 20 69 74  h <column> as it
20d34 73 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75  s left-most colu
20d35 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74  mn..**.** When t
20d36 68 65 20 62 2d 74 72 65 65 20 69 73 20 62 65 69  he b-tree is bei
20d37 6e 67 20 75 73 65 64 20 66 6f 72 20 6d 65 6d 62  ng used for memb
20d38 65 72 73 68 69 70 20 74 65 73 74 73 2c 20 74 68  ership tests, th
20d39 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69  e calling functi
20d3a 6f 6e 0a 2a 2a 20 6e 65 65 64 73 20 74 6f 20 6b  on.** needs to k
20d3b 6e 6f 77 20 77 68 65 74 68 65 72 20 6f 72 20 6e  now whether or n
20d3c 6f 74 20 74 68 65 20 73 74 72 75 63 74 75 72 65  ot the structure
20d3d 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 53 51 4c   contains an SQL
20d3e 20 4e 55 4c 4c 20 0a 2a 2a 20 76 61 6c 75 65 20   NULL .** value 
20d3f 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 6f 72 72  in order to corr
20d40 65 63 74 6c 79 20 65 76 61 6c 75 61 74 65 20 65  ectly evaluate e
20d41 78 70 72 65 73 73 69 6f 6e 73 20 6c 69 6b 65 20  xpressions like 
20d42 22 58 20 49 4e 20 28 59 2c 20 5a 29 22 2e 0a 2a  "X IN (Y, Z)"..*
20d43 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20  * If there is a 
20d44 63 68 61 6e 63 65 20 74 68 61 74 20 74 68 65 20  chance that the 
20d45 62 2d 74 72 65 65 20 6d 69 67 68 74 20 63 6f 6e  b-tree might con
20d46 74 61 69 6e 20 61 20 4e 55 4c 4c 20 76 61 6c 75  tain a NULL valu
20d47 65 20 61 74 0a 2a 2a 20 72 75 6e 74 69 6d 65 2c  e at.** runtime,
20d48 20 74 68 65 6e 20 61 20 72 65 67 69 73 74 65 72   then a register
20d49 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e   is allocated an
20d4a 64 20 74 68 65 20 72 65 67 69 73 74 65 72 20 6e  d the register n
20d4b 75 6d 62 65 72 20 77 72 69 74 74 65 6e 0a 2a 2a  umber written.**
20d4c 20 74 6f 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 2e   to *prNotFound.
20d4d 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   If there is no 
20d4e 63 68 61 6e 63 65 20 74 68 61 74 20 74 68 65 20  chance that the 
20d4f 62 2d 74 72 65 65 20 63 6f 6e 74 61 69 6e 73 20  b-tree contains 
20d50 61 0a 2a 2a 20 4e 55 4c 4c 20 76 61 6c 75 65 2c  a.** NULL value,
20d51 20 74 68 65 6e 20 2a 70 72 4e 6f 74 46 6f 75 6e   then *prNotFoun
20d52 64 20 69 73 20 6c 65 66 74 20 75 6e 63 68 61 6e  d is left unchan
20d53 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20  ged..**.** If a 
20d54 72 65 67 69 73 74 65 72 20 69 73 20 61 6c 6c 6f  register is allo
20d55 63 61 74 65 64 20 61 6e 64 20 69 74 73 20 6c 6f  cated and its lo
20d56 63 61 74 69 6f 6e 20 73 74 6f 72 65 64 20 69 6e  cation stored in
20d57 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 2c 20 74 68   *prNotFound, th
20d58 65 6e 0a 2a 2a 20 69 74 73 20 69 6e 69 74 69 61  en.** its initia
20d59 6c 20 76 61 6c 75 65 20 69 73 20 4e 55 4c 4c 2e  l value is NULL.
20d5a 20 49 66 20 74 68 65 20 62 2d 74 72 65 65 20 64   If the b-tree d
20d5b 6f 65 73 20 6e 6f 74 20 72 65 6d 61 69 6e 20 63  oes not remain c
20d5c 6f 6e 73 74 61 6e 74 0a 2a 2a 20 66 6f 72 20 74  onstant.** for t
20d5d 68 65 20 64 75 72 61 74 69 6f 6e 20 6f 66 20 74  he duration of t
20d5e 68 65 20 71 75 65 72 79 20 28 69 2e 65 2e 20 74  he query (i.e. t
20d5f 68 65 20 53 45 4c 45 43 54 20 74 68 61 74 20 67  he SELECT that g
20d60 65 6e 65 72 61 74 65 73 20 74 68 65 20 62 2d 74  enerates the b-t
20d61 72 65 65 0a 2a 2a 20 69 73 20 61 20 63 6f 72 72  ree.** is a corr
20d62 65 6c 61 74 65 64 20 73 75 62 71 75 65 72 79 29  elated subquery)
20d63 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20   then the value 
20d64 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64  of the allocated
20d65 20 72 65 67 69 73 74 65 72 20 69 73 0a 2a 2a 20   register is.** 
20d66 72 65 73 65 74 20 74 6f 20 4e 55 4c 4c 20 65 61  reset to NULL ea
20d67 63 68 20 74 69 6d 65 20 74 68 65 20 62 2d 74 72  ch time the b-tr
20d68 65 65 20 69 73 20 72 65 70 6f 70 75 6c 61 74 65  ee is repopulate
20d69 64 2e 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 74  d. This allows t
20d6a 68 65 0a 2a 2a 20 63 61 6c 6c 65 72 20 74 6f 20  he.** caller to 
20d6b 75 73 65 20 76 64 62 65 20 63 6f 64 65 20 65 71  use vdbe code eq
20d6c 75 69 76 61 6c 65 6e 74 20 74 6f 20 74 68 65 20  uivalent to the 
20d6d 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a  following:.**.**
20d6e 20 20 20 69 66 28 20 72 65 67 69 73 74 65 72 3d     if( register=
20d6f 3d 4e 55 4c 4c 20 29 7b 0a 2a 2a 20 20 20 20 20  =NULL ){.**     
20d70 68 61 73 5f 6e 75 6c 6c 20 3d 20 3c 74 65 73 74  has_null = <test
20d71 20 69 66 20 64 61 74 61 20 73 74 72 75 63 74 75   if data structu
20d72 72 65 20 63 6f 6e 74 61 69 6e 73 20 6e 75 6c 6c  re contains null
20d73 3e 0a 2a 2a 20 20 20 20 20 72 65 67 69 73 74 65  >.**     registe
20d74 72 20 3d 20 31 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a  r = 1.**   }.**.
20d75 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 61  ** in order to a
20d76 76 6f 69 64 20 72 75 6e 6e 69 6e 67 20 74 68 65  void running the
20d77 20 3c 74 65 73 74 20 69 66 20 64 61 74 61 20 73   <test if data s
20d78 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e  tructure contain
20d79 73 20 6e 75 6c 6c 3e 0a 2a 2a 20 74 65 73 74 20  s null>.** test 
20d7a 6d 6f 72 65 20 6f 66 74 65 6e 20 74 68 61 6e 20  more often than 
20d7b 69 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f  is necessary..*/
20d7c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
20d7d 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 53 51  OMIT_SUBQUERY.SQ
20d7e 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
20d7f 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 49 6e   sqlite3FindInIn
20d80 64 65 78 28 50 61 72 73 65 20 2a 70 50 61 72 73  dex(Parse *pPars
20d81 65 2c 20 45 78 70 72 20 2a 70 58 2c 20 69 6e 74  e, Expr *pX, int
20d82 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 29 7b 0a 20   *prNotFound){. 
20d83 20 53 65 6c 65 63 74 20 2a 70 3b 20 20 20 20 20   Select *p;     
20d84 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20d85 20 20 20 20 20 20 20 2f 2a 20 53 45 4c 45 43 54         /* SELECT
20d86 20 74 6f 20 74 68 65 20 72 69 67 68 74 20 6f 66   to the right of
20d87 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a   IN operator */.
20d88 20 20 69 6e 74 20 65 54 79 70 65 20 3d 20 30 3b    int eType = 0;
20d89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20d8a 20 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20          /* Type 
20d8b 6f 66 20 52 48 53 20 74 61 62 6c 65 2e 20 49 4e  of RHS table. IN
20d8c 5f 49 4e 44 45 58 5f 2a 20 2a 2f 0a 20 20 69 6e  _INDEX_* */.  in
20d8d 74 20 69 54 61 62 20 3d 20 70 50 61 72 73 65 2d  t iTab = pParse-
20d8e 3e 6e 54 61 62 2b 2b 3b 20 20 20 20 20 20 20 20  >nTab++;        
20d8f 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f 66      /* Cursor of
20d90 20 74 68 65 20 52 48 53 20 74 61 62 6c 65 20 2a   the RHS table *
20d91 2f 0a 20 20 69 6e 74 20 6d 75 73 74 42 65 55 6e  /.  int mustBeUn
20d92 69 71 75 65 20 3d 20 28 70 72 4e 6f 74 46 6f 75  ique = (prNotFou
20d93 6e 64 3d 3d 30 29 3b 20 20 20 2f 2a 20 54 72 75  nd==0);   /* Tru
20d94 65 20 69 66 20 52 48 53 20 6d 75 73 74 20 62 65  e if RHS must be
20d95 20 75 6e 69 71 75 65 20 2a 2f 0a 0a 20 20 2f 2a   unique */..  /*
20d96 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   Check to see if
20d97 20 61 6e 20 65 78 69 73 74 69 6e 67 20 74 61 62   an existing tab
20d98 6c 65 20 6f 72 20 69 6e 64 65 78 20 63 61 6e 20  le or index can 
20d99 62 65 20 75 73 65 64 20 74 6f 0a 20 20 2a 2a 20  be used to.  ** 
20d9a 73 61 74 69 73 66 79 20 74 68 65 20 71 75 65 72  satisfy the quer
20d9b 79 2e 20 20 54 68 69 73 20 69 73 20 70 72 65 66  y.  This is pref
20d9c 65 72 61 62 6c 65 20 74 6f 20 67 65 6e 65 72 61  erable to genera
20d9d 74 69 6e 67 20 61 20 6e 65 77 20 0a 20 20 2a 2a  ting a new .  **
20d9e 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65   ephemeral table
20d9f 2e 0a 20 20 2a 2f 0a 20 20 70 20 3d 20 28 45 78  ..  */.  p = (Ex
20da0 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 58  prHasProperty(pX
20da1 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20  , EP_xIsSelect) 
20da2 3f 20 70 58 2d 3e 78 2e 70 53 65 6c 65 63 74 20  ? pX->x.pSelect 
20da3 3a 20 30 29 3b 0a 20 20 69 66 28 20 41 4c 57 41  : 0);.  if( ALWA
20da4 59 53 28 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d  YS(pParse->nErr=
20da5 3d 30 29 20 26 26 20 69 73 43 61 6e 64 69 64 61  =0) && isCandida
20da6 74 65 46 6f 72 49 6e 4f 70 74 28 70 29 20 29 7b  teForInOpt(p) ){
20da7 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62  .    sqlite3 *db
20da8 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20   = pParse->db;  
20da9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
20daa 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
20dab 6f 6e 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a  on */.    Expr *
20dac 70 45 78 70 72 20 3d 20 70 2d 3e 70 45 4c 69 73  pExpr = p->pELis
20dad 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 20 20  t->a[0].pExpr;  
20dae 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 3c   /* Expression <
20daf 63 6f 6c 75 6d 6e 3e 20 2a 2f 0a 20 20 20 20 69  column> */.    i
20db0 6e 74 20 69 43 6f 6c 20 3d 20 70 45 78 70 72 2d  nt iCol = pExpr-
20db1 3e 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20  >iColumn;       
20db2 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
20db3 66 20 63 6f 6c 75 6d 6e 20 3c 63 6f 6c 75 6d 6e  f column <column
20db4 3e 20 2a 2f 0a 20 20 20 20 56 64 62 65 20 2a 76  > */.    Vdbe *v
20db5 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
20db6 65 28 70 50 61 72 73 65 29 3b 20 20 20 20 20 20  e(pParse);      
20db7 2f 2a 20 56 69 72 74 75 61 6c 20 6d 61 63 68 69  /* Virtual machi
20db8 6e 65 20 62 65 69 6e 67 20 63 6f 64 65 64 20 2a  ne being coded *
20db9 2f 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61  /.    Table *pTa
20dba 62 20 3d 20 70 2d 3e 70 53 72 63 2d 3e 61 5b 30  b = p->pSrc->a[0
20dbb 5d 2e 70 54 61 62 3b 20 20 20 20 20 20 2f 2a 20  ].pTab;      /* 
20dbc 54 61 62 6c 65 20 3c 74 61 62 6c 65 3e 2e 20 2a  Table <table>. *
20dbd 2f 0a 20 20 20 20 69 6e 74 20 69 44 62 3b 20 20  /.    int iDb;  
20dbe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20dbf 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
20dc0 44 61 74 61 62 61 73 65 20 69 64 78 20 66 6f 72  Database idx for
20dc1 20 70 54 61 62 20 2a 2f 0a 20 20 20 0a 20 20 20   pTab */.   .   
20dc2 20 2f 2a 20 43 6f 64 65 20 61 6e 20 4f 50 5f 56   /* Code an OP_V
20dc3 65 72 69 66 79 43 6f 6f 6b 69 65 20 61 6e 64 20  erifyCookie and 
20dc4 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 20 66 6f 72  OP_TableLock for
20dc5 20 3c 74 61 62 6c 65 3e 2e 20 2a 2f 0a 20 20 20   <table>. */.   
20dc6 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
20dc7 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20  hemaToIndex(db, 
20dc8 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  pTab->pSchema);.
20dc9 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 56      sqlite3CodeV
20dca 65 72 69 66 79 53 63 68 65 6d 61 28 70 50 61 72  erifySchema(pPar
20dcb 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71  se, iDb);.    sq
20dcc 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70  lite3TableLock(p
20dcd 50 61 72 73 65 2c 20 69 44 62 2c 20 70 54 61 62  Parse, iDb, pTab
20dce 2d 3e 74 6e 75 6d 2c 20 30 2c 20 70 54 61 62 2d  ->tnum, 0, pTab-
20dcf 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 20 20 2f 2a  >zName);..    /*
20dd0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
20dd1 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72  s only called fr
20dd2 6f 6d 20 74 77 6f 20 70 6c 61 63 65 73 2e 20 49  om two places. I
20dd3 6e 20 62 6f 74 68 20 63 61 73 65 73 20 74 68 65  n both cases the
20dd4 20 76 64 62 65 0a 20 20 20 20 2a 2a 20 68 61 73   vdbe.    ** has
20dd5 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 61 6c   already been al
20dd6 6c 6f 63 61 74 65 64 2e 20 53 6f 20 61 73 73 75  located. So assu
20dd7 6d 65 20 73 71 6c 69 74 65 33 47 65 74 56 64 62  me sqlite3GetVdb
20dd8 65 28 29 20 69 73 20 61 6c 77 61 79 73 0a 20 20  e() is always.  
20dd9 20 20 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20    ** successful 
20dda 68 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  here..    */.   
20ddb 20 61 73 73 65 72 74 28 76 29 3b 0a 20 20 20 20   assert(v);.    
20ddc 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b 0a 20 20  if( iCol<0 ){.  
20ddd 20 20 20 20 69 6e 74 20 69 4d 65 6d 20 3d 20 2b      int iMem = +
20dde 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
20ddf 20 20 20 20 20 69 6e 74 20 69 41 64 64 72 3b 0a       int iAddr;.
20de0 0a 20 20 20 20 20 20 69 41 64 64 72 20 3d 20 73  .      iAddr = s
20de1 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
20de2 28 76 2c 20 4f 50 5f 49 66 2c 20 69 4d 65 6d 29  (v, OP_If, iMem)
20de3 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
20de4 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
20de5 49 6e 74 65 67 65 72 2c 20 31 2c 20 69 4d 65 6d  Integer, 1, iMem
20de6 29 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65  );..      sqlite
20de7 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73  3OpenTable(pPars
20de8 65 2c 20 69 54 61 62 2c 20 69 44 62 2c 20 70 54  e, iTab, iDb, pT
20de9 61 62 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29  ab, OP_OpenRead)
20dea 3b 0a 20 20 20 20 20 20 65 54 79 70 65 20 3d 20  ;.      eType = 
20deb 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 3b 0a  IN_INDEX_ROWID;.
20dec 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
20ded 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 69 41  beJumpHere(v, iA
20dee 64 64 72 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ddr);.    }else{
20def 0a 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49  .      Index *pI
20df0 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dx;             
20df1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
20df2 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65  terator variable
20df3 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68   */..      /* Th
20df4 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  e collation sequ
20df5 65 6e 63 65 20 75 73 65 64 20 62 79 20 74 68 65  ence used by the
20df6 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 49 66 20   comparison. If 
20df7 61 6e 20 69 6e 64 65 78 20 69 73 20 74 6f 0a 20  an index is to. 
20df8 20 20 20 20 20 2a 2a 20 62 65 20 75 73 65 64 20       ** be used 
20df9 69 6e 20 70 6c 61 63 65 20 6f 66 20 61 20 74 65  in place of a te
20dfa 6d 70 2d 74 61 62 6c 65 2c 20 69 74 20 6d 75 73  mp-table, it mus
20dfb 74 20 62 65 20 6f 72 64 65 72 65 64 20 61 63 63  t be ordered acc
20dfc 6f 72 64 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20  ording.      ** 
20dfd 74 6f 20 74 68 69 73 20 63 6f 6c 6c 61 74 69 6f  to this collatio
20dfe 6e 20 73 65 71 75 65 6e 63 65 2e 20 20 2a 2f 0a  n sequence.  */.
20dff 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70        CollSeq *p
20e00 52 65 71 20 3d 20 73 71 6c 69 74 65 33 42 69 6e  Req = sqlite3Bin
20e01 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65  aryCompareCollSe
20e02 71 28 70 50 61 72 73 65 2c 20 70 58 2d 3e 70 4c  q(pParse, pX->pL
20e03 65 66 74 2c 20 70 45 78 70 72 29 3b 0a 0a 20 20  eft, pExpr);..  
20e04 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61      /* Check tha
20e05 74 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 74  t the affinity t
20e06 68 61 74 20 77 69 6c 6c 20 62 65 20 75 73 65 64  hat will be used
20e07 20 74 6f 20 70 65 72 66 6f 72 6d 20 74 68 65 20   to perform the 
20e08 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6d 70 61 72  .      ** compar
20e09 69 73 6f 6e 20 69 73 20 74 68 65 20 73 61 6d 65  ison is the same
20e0a 20 61 73 20 74 68 65 20 61 66 66 69 6e 69 74 79   as the affinity
20e0b 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20   of the column. 
20e0c 49 66 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 69  If.      ** it i
20e0d 73 20 6e 6f 74 2c 20 69 74 20 69 73 20 6e 6f 74  s not, it is not
20e0e 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 75 73 65   possible to use
20e0f 20 61 6e 79 20 69 6e 64 65 78 2e 0a 20 20 20 20   any index..    
20e10 20 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20    */.      char 
20e11 61 66 66 20 3d 20 63 6f 6d 70 61 72 69 73 6f 6e  aff = comparison
20e12 41 66 66 69 6e 69 74 79 28 70 58 29 3b 0a 20 20  Affinity(pX);.  
20e13 20 20 20 20 69 6e 74 20 61 66 66 69 6e 69 74 79      int affinity
20e14 5f 6f 6b 20 3d 20 28 70 54 61 62 2d 3e 61 43 6f  _ok = (pTab->aCo
20e15 6c 5b 69 43 6f 6c 5d 2e 61 66 66 69 6e 69 74 79  l[iCol].affinity
20e16 3d 3d 61 66 66 7c 7c 61 66 66 3d 3d 53 51 4c 49  ==aff||aff==SQLI
20e17 54 45 5f 41 46 46 5f 4e 4f 4e 45 29 3b 0a 0a 20  TE_AFF_NONE);.. 
20e18 20 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54       for(pIdx=pT
20e19 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78  ab->pIndex; pIdx
20e1a 20 26 26 20 65 54 79 70 65 3d 3d 30 20 26 26 20   && eType==0 && 
20e1b 61 66 66 69 6e 69 74 79 5f 6f 6b 3b 20 70 49 64  affinity_ok; pId
20e1c 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a  x=pIdx->pNext){.
20e1d 20 20 20 20 20 20 20 20 69 66 28 20 28 70 49 64          if( (pId
20e1e 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d  x->aiColumn[0]==
20e1f 69 43 6f 6c 29 0a 20 20 20 20 20 20 20 20 20 26  iCol).         &
20e20 26 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c  & sqlite3FindCol
20e21 6c 53 65 71 28 64 62 2c 20 45 4e 43 28 64 62 29  lSeq(db, ENC(db)
20e22 2c 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 30  , pIdx->azColl[0
20e23 5d 2c 20 30 29 3d 3d 70 52 65 71 0a 20 20 20 20  ], 0)==pReq.    
20e24 20 20 20 20 20 26 26 20 28 21 6d 75 73 74 42 65       && (!mustBe
20e25 55 6e 69 71 75 65 20 7c 7c 20 28 70 49 64 78 2d  Unique || (pIdx-
20e26 3e 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 26 26 20 70  >nColumn==1 && p
20e27 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45  Idx->onError!=OE
20e28 5f 4e 6f 6e 65 29 29 0a 20 20 20 20 20 20 20 20  _None)).        
20e29 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74  ){.          int
20e2a 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65   iMem = ++pParse
20e2b 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20  ->nMem;.        
20e2c 20 20 69 6e 74 20 69 41 64 64 72 3b 0a 20 20 20    int iAddr;.   
20e2d 20 20 20 20 20 20 20 63 68 61 72 20 2a 70 4b 65         char *pKe
20e2e 79 3b 0a 20 20 0a 20 20 20 20 20 20 20 20 20 20  y;.  .          
20e2f 70 4b 65 79 20 3d 20 28 63 68 61 72 20 2a 29 73  pKey = (char *)s
20e30 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e  qlite3IndexKeyin
20e31 66 6f 28 70 50 61 72 73 65 2c 20 70 49 64 78 29  fo(pParse, pIdx)
20e32 3b 0a 20 20 20 20 20 20 20 20 20 20 69 41 64 64  ;.          iAdd
20e33 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
20e34 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20  ddOp1(v, OP_If, 
20e35 69 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 20  iMem);.         
20e36 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
20e37 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
20e38 2c 20 31 2c 20 69 4d 65 6d 29 3b 0a 20 20 0a 20  , 1, iMem);.  . 
20e39 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
20e3a 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
20e3b 5f 4f 70 65 6e 52 65 61 64 2c 20 69 54 61 62 2c  _OpenRead, iTab,
20e3c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62   pIdx->tnum, iDb
20e3d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
20e3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20e3f 20 70 4b 65 79 2c 50 34 5f 4b 45 59 49 4e 46 4f   pKey,P4_KEYINFO
20e40 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 20  _HANDOFF);.     
20e41 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74       VdbeComment
20e42 28 28 76 2c 20 22 25 73 22 2c 20 70 49 64 78 2d  ((v, "%s", pIdx-
20e43 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 20 20  >zName));.      
20e44 20 20 20 20 65 54 79 70 65 20 3d 20 49 4e 5f 49      eType = IN_I
20e45 4e 44 45 58 5f 49 4e 44 45 58 3b 0a 0a 20 20 20  NDEX_INDEX;..   
20e46 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
20e47 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 69 41  beJumpHere(v, iA
20e48 64 64 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ddr);.          
20e49 69 66 28 20 70 72 4e 6f 74 46 6f 75 6e 64 20 26  if( prNotFound &
20e4a 26 20 21 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43  & !pTab->aCol[iC
20e4b 6f 6c 5d 2e 6e 6f 74 4e 75 6c 6c 20 29 7b 0a 20  ol].notNull ){. 
20e4c 20 20 20 20 20 20 20 20 20 20 20 2a 70 72 4e 6f             *prNo
20e4d 74 46 6f 75 6e 64 20 3d 20 2b 2b 70 50 61 72 73  tFound = ++pPars
20e4e 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 20  e->nMem;.       
20e4f 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
20e50 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
20e51 0a 0a 20 20 69 66 28 20 65 54 79 70 65 3d 3d 30  ..  if( eType==0
20e52 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6f 75 6c 64   ){.    /* Could
20e53 20 6e 6f 74 20 66 6f 75 6e 64 20 61 6e 20 65 78   not found an ex
20e54 69 73 74 69 6e 67 20 61 62 6c 65 20 6f 72 20 69  isting able or i
20e55 6e 64 65 78 20 74 6f 20 75 73 65 20 61 73 20 74  ndex to use as t
20e56 68 65 20 52 48 53 20 62 2d 74 72 65 65 2e 0a 20  he RHS b-tree.. 
20e57 20 20 20 2a 2a 20 57 65 20 77 69 6c 6c 20 68 61     ** We will ha
20e58 76 65 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61  ve to generate a
20e59 6e 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c  n ephemeral tabl
20e5a 65 20 74 6f 20 64 6f 20 74 68 65 20 6a 6f 62 2e  e to do the job.
20e5b 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20  .    */.    int 
20e5c 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 3d 20 30  rMayHaveNull = 0
20e5d 3b 0a 20 20 20 20 65 54 79 70 65 20 3d 20 49 4e  ;.    eType = IN
20e5e 5f 49 4e 44 45 58 5f 45 50 48 3b 0a 20 20 20 20  _INDEX_EPH;.    
20e5f 69 66 28 20 70 72 4e 6f 74 46 6f 75 6e 64 20 29  if( prNotFound )
20e60 7b 0a 20 20 20 20 20 20 2a 70 72 4e 6f 74 46 6f  {.      *prNotFo
20e61 75 6e 64 20 3d 20 72 4d 61 79 48 61 76 65 4e 75  und = rMayHaveNu
20e62 6c 6c 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  ll = ++pParse->n
20e63 4d 65 6d 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  Mem;.    }else i
20e64 66 28 20 70 58 2d 3e 70 4c 65 66 74 2d 3e 69 43  f( pX->pLeft->iC
20e65 6f 6c 75 6d 6e 3c 30 20 26 26 20 21 45 78 70 72  olumn<0 && !Expr
20e66 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70  HasAnyProperty(p
20e67 58 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  X, EP_xIsSelect)
20e68 20 29 7b 0a 20 20 20 20 20 20 65 54 79 70 65 20   ){.      eType 
20e69 3d 20 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44  = IN_INDEX_ROWID
20e6a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
20e6b 74 65 33 43 6f 64 65 53 75 62 73 65 6c 65 63 74  te3CodeSubselect
20e6c 28 70 50 61 72 73 65 2c 20 70 58 2c 20 72 4d 61  (pParse, pX, rMa
20e6d 79 48 61 76 65 4e 75 6c 6c 2c 20 65 54 79 70 65  yHaveNull, eType
20e6e 3d 3d 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44  ==IN_INDEX_ROWID
20e6f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
20e70 70 58 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54 61  pX->iTable = iTa
20e71 62 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  b;.  }.  return 
20e72 65 54 79 70 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a  eType;.}.#endif.
20e73 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
20e74 63 6f 64 65 20 66 6f 72 20 73 63 61 6c 61 72 20  code for scalar 
20e75 73 75 62 71 75 65 72 69 65 73 20 75 73 65 64 20  subqueries used 
20e76 61 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  as an expression
20e77 0a 2a 2a 20 61 6e 64 20 49 4e 20 6f 70 65 72 61  .** and IN opera
20e78 74 6f 72 73 2e 20 20 45 78 61 6d 70 6c 65 73 3a  tors.  Examples:
20e79 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 53 45 4c 45  .**.**     (SELE
20e7a 43 54 20 61 20 46 52 4f 4d 20 62 29 20 20 20 20  CT a FROM b)    
20e7b 20 20 20 20 20 20 2d 2d 20 73 75 62 71 75 65 72        -- subquer
20e7c 79 0a 2a 2a 20 20 20 20 20 45 58 49 53 54 53 20  y.**     EXISTS 
20e7d 28 53 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 62  (SELECT a FROM b
20e7e 29 20 20 20 2d 2d 20 45 58 49 53 54 53 20 73 75  )   -- EXISTS su
20e7f 62 71 75 65 72 79 0a 2a 2a 20 20 20 20 20 78 20  bquery.**     x 
20e80 49 4e 20 28 34 2c 35 2c 31 31 29 20 20 20 20 20  IN (4,5,11)     
20e81 20 20 20 20 20 20 20 20 20 2d 2d 20 49 4e 20 6f           -- IN o
20e82 70 65 72 61 74 6f 72 20 77 69 74 68 20 6c 69 73  perator with lis
20e83 74 20 6f 6e 20 72 69 67 68 74 2d 68 61 6e 64 20  t on right-hand 
20e84 73 69 64 65 0a 2a 2a 20 20 20 20 20 78 20 49 4e  side.**     x IN
20e85 20 28 53 45 4c 45 43 54 20 61 20 46 52 4f 4d 20   (SELECT a FROM 
20e86 62 29 20 20 20 20 20 2d 2d 20 49 4e 20 6f 70 65  b)     -- IN ope
20e87 72 61 74 6f 72 20 77 69 74 68 20 73 75 62 71 75  rator with subqu
20e88 65 72 79 20 6f 6e 20 74 68 65 20 72 69 67 68 74  ery on the right
20e89 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 45 78 70 72  .**.** The pExpr
20e8a 20 70 61 72 61 6d 65 74 65 72 20 64 65 73 63 72   parameter descr
20e8b 69 62 65 73 20 74 68 65 20 65 78 70 72 65 73 73  ibes the express
20e8c 69 6f 6e 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ion that contain
20e8d 73 20 74 68 65 20 49 4e 0a 2a 2a 20 6f 70 65 72  s the IN.** oper
20e8e 61 74 6f 72 20 6f 72 20 73 75 62 71 75 65 72 79  ator or subquery
20e8f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 72 61 6d  ..**.** If param
20e90 65 74 65 72 20 69 73 52 6f 77 69 64 20 69 73 20  eter isRowid is 
20e91 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 65  non-zero, then e
20e92 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20  xpression pExpr 
20e93 69 73 20 67 75 61 72 61 6e 74 65 65 64 0a 2a 2a  is guaranteed.**
20e94 20 74 6f 20 62 65 20 6f 66 20 74 68 65 20 66 6f   to be of the fo
20e95 72 6d 20 22 3c 72 6f 77 69 64 3e 20 49 4e 20 28  rm "<rowid> IN (
20e96 3f 2c 20 3f 2c 20 3f 29 22 2c 20 77 68 65 72 65  ?, ?, ?)", where
20e97 20 3c 72 6f 77 69 64 3e 20 69 73 20 61 20 72 65   <rowid> is a re
20e98 66 65 72 65 6e 63 65 0a 2a 2a 20 74 6f 20 73 6f  ference.** to so
20e99 6d 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20 63  me integer key c
20e9a 6f 6c 75 6d 6e 20 6f 66 20 61 20 74 61 62 6c 65  olumn of a table
20e9b 20 42 2d 54 72 65 65 2e 20 49 6e 20 74 68 69 73   B-Tree. In this
20e9c 20 63 61 73 65 2c 20 75 73 65 20 61 6e 0a 2a 2a   case, use an.**
20e9d 20 69 6e 74 6b 65 79 20 42 2d 54 72 65 65 20 74   intkey B-Tree t
20e9e 6f 20 73 74 6f 72 65 20 74 68 65 20 73 65 74 20  o store the set 
20e9f 6f 66 20 49 4e 28 2e 2e 2e 29 20 76 61 6c 75 65  of IN(...) value
20ea0 73 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65  s instead of the
20ea1 20 75 73 75 61 6c 0a 2a 2a 20 28 73 6c 6f 77 65   usual.** (slowe
20ea2 72 29 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67  r) variable leng
20ea3 74 68 20 6b 65 79 73 20 42 2d 54 72 65 65 2e 0a  th keys B-Tree..
20ea4 2a 2a 0a 2a 2a 20 49 66 20 72 4d 61 79 48 61 76  **.** If rMayHav
20ea5 65 4e 75 6c 6c 20 69 73 20 6e 6f 6e 2d 7a 65 72  eNull is non-zer
20ea6 6f 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68  o, that means th
20ea7 61 74 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e  at the operation
20ea8 20 69 73 20 61 6e 20 49 4e 0a 2a 2a 20 28 6e 6f   is an IN.** (no
20ea9 74 20 61 20 53 45 4c 45 43 54 20 6f 72 20 45 58  t a SELECT or EX
20eaa 49 53 54 53 29 20 61 6e 64 20 74 68 61 74 20 74  ISTS) and that t
20eab 68 65 20 52 48 53 20 6d 69 67 68 74 20 63 6f 6e  he RHS might con
20eac 74 61 69 6e 73 20 4e 55 4c 4c 73 2e 0a 2a 2a 20  tains NULLs..** 
20ead 46 75 72 74 68 65 72 6d 6f 72 65 2c 20 74 68 65  Furthermore, the
20eae 20 49 4e 20 69 73 20 69 6e 20 61 20 57 48 45 52   IN is in a WHER
20eaf 45 20 63 6c 61 75 73 65 20 61 6e 64 20 74 68 61  E clause and tha
20eb0 74 20 77 65 20 72 65 61 6c 6c 79 20 77 61 6e 74  t we really want
20eb1 0a 2a 2a 20 74 6f 20 69 74 65 72 61 74 65 20 6f  .** to iterate o
20eb2 76 65 72 20 74 68 65 20 52 48 53 20 6f 66 20 74  ver the RHS of t
20eb3 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 69  he IN operator i
20eb4 6e 20 6f 72 64 65 72 20 74 6f 20 71 75 69 63 6b  n order to quick
20eb5 6c 79 20 6c 6f 63 61 74 65 0a 2a 2a 20 61 6c 6c  ly locate.** all
20eb6 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 4c   corresponding L
20eb7 48 53 20 65 6c 65 6d 65 6e 74 73 2e 20 20 41 6c  HS elements.  Al
20eb8 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 64  l this routine d
20eb9 6f 65 73 20 69 73 20 69 6e 69 74 69 61 6c 69 7a  oes is initializ
20eba 65 0a 2a 2a 20 74 68 65 20 72 65 67 69 73 74 65  e.** the registe
20ebb 72 20 67 69 76 65 6e 20 62 79 20 72 4d 61 79 48  r given by rMayH
20ebc 61 76 65 4e 75 6c 6c 20 74 6f 20 4e 55 4c 4c 2e  aveNull to NULL.
20ebd 20 20 43 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e    Calling routin
20ebe 65 73 20 77 69 6c 6c 20 74 61 6b 65 0a 2a 2a 20  es will take.** 
20ebf 63 61 72 65 20 6f 66 20 63 68 61 6e 67 69 6e 67  care of changing
20ec0 20 74 68 69 73 20 72 65 67 69 73 74 65 72 20 76   this register v
20ec1 61 6c 75 65 20 74 6f 20 6e 6f 6e 2d 4e 55 4c 4c  alue to non-NULL
20ec2 20 69 66 20 74 68 65 20 52 48 53 20 69 73 20 4e   if the RHS is N
20ec3 55 4c 4c 2d 66 72 65 65 2e 0a 2a 2a 0a 2a 2a 20  ULL-free..**.** 
20ec4 49 66 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20  If rMayHaveNull 
20ec5 69 73 20 7a 65 72 6f 2c 20 74 68 61 74 20 6d 65  is zero, that me
20ec6 61 6e 73 20 74 68 61 74 20 74 68 65 20 73 75 62  ans that the sub
20ec7 71 75 65 72 79 20 69 73 20 62 65 69 6e 67 20 75  query is being u
20ec8 73 65 64 0a 2a 2a 20 66 6f 72 20 6d 65 6d 62 65  sed.** for membe
20ec9 72 73 68 69 70 20 74 65 73 74 69 6e 67 20 6f 6e  rship testing on
20eca 6c 79 2e 20 20 54 68 65 72 65 20 69 73 20 6e 6f  ly.  There is no
20ecb 20 6e 65 65 64 20 74 6f 20 69 6e 69 74 69 61 6c   need to initial
20ecc 69 7a 65 20 61 6e 79 0a 2a 2a 20 72 65 67 69 73  ize any.** regis
20ecd 74 65 72 73 20 74 6f 20 69 6e 64 69 63 61 74 65  ters to indicate
20ece 20 74 68 65 20 70 72 65 73 65 6e 73 65 20 6f 72   the presense or
20ecf 20 61 62 73 65 6e 63 65 20 6f 66 20 4e 55 4c 4c   absence of NULL
20ed0 73 20 6f 6e 20 74 68 65 20 52 48 53 2e 0a 2a 2f  s on the RHS..*/
20ed1 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
20ed2 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 53 51  OMIT_SUBQUERY.SQ
20ed3 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
20ed4 64 20 73 71 6c 69 74 65 33 43 6f 64 65 53 75 62  d sqlite3CodeSub
20ed5 73 65 6c 65 63 74 28 0a 20 20 50 61 72 73 65 20  select(.  Parse 
20ed6 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
20ed7 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
20ed8 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a  text */.  Expr *
20ed9 70 45 78 70 72 2c 20 20 20 20 20 20 20 20 20 20  pExpr,          
20eda 20 20 2f 2a 20 54 68 65 20 49 4e 2c 20 53 45 4c    /* The IN, SEL
20edb 45 43 54 2c 20 6f 72 20 45 58 49 53 54 53 20 6f  ECT, or EXISTS o
20edc 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 69 6e 74  perator */.  int
20edd 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 2c 20 20   rMayHaveNull,  
20ede 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72       /* Register
20edf 20 74 68 61 74 20 72 65 63 6f 72 64 73 20 77 68   that records wh
20ee0 65 74 68 65 72 20 4e 55 4c 4c 73 20 65 78 69 73  ether NULLs exis
20ee1 74 20 69 6e 20 52 48 53 20 2a 2f 0a 20 20 69 6e  t in RHS */.  in
20ee2 74 20 69 73 52 6f 77 69 64 20 20 20 20 20 20 20  t isRowid       
20ee3 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65        /* If true
20ee4 2c 20 4c 48 53 20 6f 66 20 49 4e 20 6f 70 65 72  , LHS of IN oper
20ee5 61 74 6f 72 20 69 73 20 61 20 72 6f 77 69 64 20  ator is a rowid 
20ee6 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 74 65 73 74  */.){.  int test
20ee7 41 64 64 72 20 3d 20 30 3b 20 20 20 20 20 20 20  Addr = 0;       
20ee8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20ee9 2f 2a 20 4f 6e 65 2d 74 69 6d 65 20 74 65 73 74  /* One-time test
20eea 20 61 64 64 72 65 73 73 20 2a 2f 0a 20 20 56 64   address */.  Vd
20eeb 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47  be *v = sqlite3G
20eec 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
20eed 20 20 69 66 28 20 4e 45 56 45 52 28 76 3d 3d 30    if( NEVER(v==0
20eee 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71  ) ) return;.  sq
20eef 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 75  lite3ExprCachePu
20ef0 73 68 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 2f  sh(pParse);..  /
20ef1 2a 20 54 68 69 73 20 63 6f 64 65 20 6d 75 73 74  * This code must
20ef2 20 62 65 20 72 75 6e 20 69 6e 20 69 74 73 20 65   be run in its e
20ef3 6e 74 69 72 65 74 79 20 65 76 65 72 79 20 74 69  ntirety every ti
20ef4 6d 65 20 69 74 20 69 73 20 65 6e 63 6f 75 6e 74  me it is encount
20ef5 65 72 65 64 0a 20 20 2a 2a 20 69 66 20 61 6e 79  ered.  ** if any
20ef6 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
20ef7 67 20 69 73 20 74 72 75 65 3a 0a 20 20 2a 2a 0a  g is true:.  **.
20ef8 20 20 2a 2a 20 20 20 20 2a 20 20 54 68 65 20 72    **    *  The r
20ef9 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 69  ight-hand side i
20efa 73 20 61 20 63 6f 72 72 65 6c 61 74 65 64 20 73  s a correlated s
20efb 75 62 71 75 65 72 79 0a 20 20 2a 2a 20 20 20 20  ubquery.  **    
20efc 2a 20 20 54 68 65 20 72 69 67 68 74 2d 68 61 6e  *  The right-han
20efd 64 20 73 69 64 65 20 69 73 20 61 6e 20 65 78 70  d side is an exp
20efe 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 63 6f 6e  ression list con
20eff 74 61 69 6e 69 6e 67 20 76 61 72 69 61 62 6c 65  taining variable
20f00 73 0a 20 20 2a 2a 20 20 20 20 2a 20 20 57 65 20  s.  **    *  We 
20f01 61 72 65 20 69 6e 73 69 64 65 20 61 20 74 72 69  are inside a tri
20f02 67 67 65 72 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49  gger.  **.  ** I
20f03 66 20 61 6c 6c 20 6f 66 20 74 68 65 20 61 62 6f  f all of the abo
20f04 76 65 20 61 72 65 20 66 61 6c 73 65 2c 20 74 68  ve are false, th
20f05 65 6e 20 77 65 20 63 61 6e 20 72 75 6e 20 74 68  en we can run th
20f06 69 73 20 63 6f 64 65 20 6a 75 73 74 20 6f 6e 63  is code just onc
20f07 65 0a 20 20 2a 2a 20 73 61 76 65 20 74 68 65 20  e.  ** save the 
20f08 72 65 73 75 6c 74 73 2c 20 61 6e 64 20 72 65 75  results, and reu
20f09 73 65 20 74 68 65 20 73 61 6d 65 20 72 65 73 75  se the same resu
20f0a 6c 74 20 6f 6e 20 73 75 62 73 65 71 75 65 6e 74  lt on subsequent
20f0b 20 69 6e 76 6f 63 61 74 69 6f 6e 73 2e 0a 20 20   invocations..  
20f0c 2a 2f 0a 20 20 69 66 28 20 21 45 78 70 72 48 61  */.  if( !ExprHa
20f0d 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78  sAnyProperty(pEx
20f0e 70 72 2c 20 45 50 5f 56 61 72 53 65 6c 65 63 74  pr, EP_VarSelect
20f0f 29 20 26 26 20 21 70 50 61 72 73 65 2d 3e 70 54  ) && !pParse->pT
20f10 72 69 67 67 65 72 54 61 62 20 29 7b 0a 20 20 20  riggerTab ){.   
20f11 20 69 6e 74 20 6d 65 6d 20 3d 20 2b 2b 70 50 61   int mem = ++pPa
20f12 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73  rse->nMem;.    s
20f13 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
20f14 28 76 2c 20 4f 50 5f 49 66 2c 20 6d 65 6d 29 3b  (v, OP_If, mem);
20f15 0a 20 20 20 20 74 65 73 74 41 64 64 72 20 3d 20  .    testAddr = 
20f16 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
20f17 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
20f18 20 31 2c 20 6d 65 6d 29 3b 0a 20 20 20 20 61 73   1, mem);.    as
20f19 73 65 72 74 28 20 74 65 73 74 41 64 64 72 3e 30  sert( testAddr>0
20f1a 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e   || pParse->db->
20f1b 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a  mallocFailed );.
20f1c 20 20 7d 0a 0a 20 20 73 77 69 74 63 68 28 20 70    }..  switch( p
20f1d 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20  Expr->op ){.    
20f1e 63 61 73 65 20 54 4b 5f 49 4e 3a 20 7b 0a 20 20  case TK_IN: {.  
20f1f 20 20 20 20 63 68 61 72 20 61 66 66 69 6e 69 74      char affinit
20f20 79 3b 0a 20 20 20 20 20 20 4b 65 79 49 6e 66 6f  y;.      KeyInfo
20f21 20 6b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20   keyInfo;.      
20f22 69 6e 74 20 61 64 64 72 3b 20 20 20 20 20 20 20  int addr;       
20f23 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 4f   /* Address of O
20f24 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20  P_OpenEphemeral 
20f25 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20  instruction */. 
20f26 20 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74       Expr *pLeft
20f27 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b   = pExpr->pLeft;
20f28 0a 0a 20 20 20 20 20 20 69 66 28 20 72 4d 61 79  ..      if( rMay
20f29 48 61 76 65 4e 75 6c 6c 20 29 7b 0a 20 20 20 20  HaveNull ){.    
20f2a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
20f2b 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp2(v, OP_Null
20f2c 2c 20 30 2c 20 72 4d 61 79 48 61 76 65 4e 75 6c  , 0, rMayHaveNul
20f2d 6c 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  l);.      }..   
20f2e 20 20 20 61 66 66 69 6e 69 74 79 20 3d 20 73 71     affinity = sq
20f2f 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74  lite3ExprAffinit
20f30 79 28 70 4c 65 66 74 29 3b 0a 0a 20 20 20 20 20  y(pLeft);..     
20f31 20 2f 2a 20 57 68 65 74 68 65 72 20 74 68 69 73   /* Whether this
20f32 20 69 73 20 61 6e 20 27 78 20 49 4e 28 53 45 4c   is an 'x IN(SEL
20f33 45 43 54 2e 2e 2e 29 27 20 6f 72 20 61 6e 20 27  ECT...)' or an '
20f34 78 20 49 4e 28 3c 65 78 70 72 6c 69 73 74 3e 29  x IN(<exprlist>)
20f35 27 0a 20 20 20 20 20 20 2a 2a 20 65 78 70 72 65  '.      ** expre
20f36 73 73 69 6f 6e 20 69 74 20 69 73 20 68 61 6e 64  ssion it is hand
20f37 6c 65 64 20 74 68 65 20 73 61 6d 65 20 77 61 79  led the same way
20f38 2e 20 41 20 76 69 72 74 75 61 6c 20 74 61 62 6c  . A virtual tabl
20f39 65 20 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 66  e is .      ** f
20f3a 69 6c 6c 65 64 20 77 69 74 68 20 73 69 6e 67 6c  illed with singl
20f3b 65 2d 66 69 65 6c 64 20 69 6e 64 65 78 20 6b 65  e-field index ke
20f3c 79 73 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20  ys representing 
20f3d 74 68 65 20 72 65 73 75 6c 74 73 0a 20 20 20 20  the results.    
20f3e 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 53 45    ** from the SE
20f3f 4c 45 43 54 20 6f 72 20 74 68 65 20 3c 65 78 70  LECT or the <exp
20f40 72 6c 69 73 74 3e 2e 0a 20 20 20 20 20 20 2a 2a  rlist>..      **
20f41 0a 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65  .      ** If the
20f42 20 27 78 27 20 65 78 70 72 65 73 73 69 6f 6e 20   'x' expression 
20f43 69 73 20 61 20 63 6f 6c 75 6d 6e 20 76 61 6c 75  is a column valu
20f44 65 2c 20 6f 72 20 74 68 65 20 53 45 4c 45 43 54  e, or the SELECT
20f45 2e 2e 2e 0a 20 20 20 20 20 20 2a 2a 20 73 74 61  ....      ** sta
20f46 74 65 6d 65 6e 74 20 72 65 74 75 72 6e 73 20 61  tement returns a
20f47 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c 20 74   column value, t
20f48 68 65 6e 20 74 68 65 20 61 66 66 69 6e 69 74 79  hen the affinity
20f49 20 6f 66 20 74 68 61 74 0a 20 20 20 20 20 20 2a   of that.      *
20f4a 2a 20 63 6f 6c 75 6d 6e 20 69 73 20 75 73 65 64  * column is used
20f4b 20 74 6f 20 62 75 69 6c 64 20 74 68 65 20 69 6e   to build the in
20f4c 64 65 78 20 6b 65 79 73 2e 20 49 66 20 62 6f 74  dex keys. If bot
20f4d 68 20 27 78 27 20 61 6e 64 20 74 68 65 0a 20 20  h 'x' and the.  
20f4e 20 20 20 20 2a 2a 20 53 45 4c 45 43 54 2e 2e 2e      ** SELECT...
20f4f 20 73 74 61 74 65 6d 65 6e 74 20 61 72 65 20 63   statement are c
20f50 6f 6c 75 6d 6e 73 2c 20 74 68 65 6e 20 6e 75 6d  olumns, then num
20f51 65 72 69 63 20 61 66 66 69 6e 69 74 79 20 69 73  eric affinity is
20f52 20 75 73 65 64 0a 20 20 20 20 20 20 2a 2a 20 69   used.      ** i
20f53 66 20 65 69 74 68 65 72 20 63 6f 6c 75 6d 6e 20  f either column 
20f54 68 61 73 20 4e 55 4d 45 52 49 43 20 6f 72 20 49  has NUMERIC or I
20f55 4e 54 45 47 45 52 20 61 66 66 69 6e 69 74 79 2e  NTEGER affinity.
20f56 20 49 66 20 6e 65 69 74 68 65 72 0a 20 20 20 20   If neither.    
20f57 20 20 2a 2a 20 27 78 27 20 6e 6f 72 20 74 68 65    ** 'x' nor the
20f58 20 53 45 4c 45 43 54 2e 2e 2e 20 73 74 61 74 65   SELECT... state
20f59 6d 65 6e 74 20 61 72 65 20 63 6f 6c 75 6d 6e 73  ment are columns
20f5a 2c 20 74 68 65 6e 20 6e 75 6d 65 72 69 63 20 61  , then numeric a
20f5b 66 66 69 6e 69 74 79 0a 20 20 20 20 20 20 2a 2a  ffinity.      **
20f5c 20 69 73 20 75 73 65 64 2e 0a 20 20 20 20 20 20   is used..      
20f5d 2a 2f 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e  */.      pExpr->
20f5e 69 54 61 62 6c 65 20 3d 20 70 50 61 72 73 65 2d  iTable = pParse-
20f5f 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 61  >nTab++;.      a
20f60 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
20f61 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70  eAddOp2(v, OP_Op
20f62 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 70 45 78  enEphemeral, pEx
20f63 70 72 2d 3e 69 54 61 62 6c 65 2c 20 21 69 73 52  pr->iTable, !isR
20f64 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 6d 65 6d  owid);.      mem
20f65 73 65 74 28 26 6b 65 79 49 6e 66 6f 2c 20 30 2c  set(&keyInfo, 0,
20f66 20 73 69 7a 65 6f 66 28 6b 65 79 49 6e 66 6f 29   sizeof(keyInfo)
20f67 29 3b 0a 20 20 20 20 20 20 6b 65 79 49 6e 66 6f  );.      keyInfo
20f68 2e 6e 46 69 65 6c 64 20 3d 20 31 3b 0a 0a 20 20  .nField = 1;..  
20f69 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50      if( ExprHasP
20f6a 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
20f6b 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a  P_xIsSelect) ){.
20f6c 20 20 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20          /* Case 
20f6d 31 3a 20 20 20 20 20 65 78 70 72 20 49 4e 20 28  1:     expr IN (
20f6e 53 45 4c 45 43 54 20 2e 2e 2e 29 0a 20 20 20 20  SELECT ...).    
20f6f 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a      **.        *
20f70 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
20f71 74 6f 20 77 72 69 74 65 20 74 68 65 20 72 65 73  to write the res
20f72 75 6c 74 73 20 6f 66 20 74 68 65 20 73 65 6c 65  ults of the sele
20f73 63 74 20 69 6e 74 6f 20 74 68 65 20 74 65 6d 70  ct into the temp
20f74 6f 72 61 72 79 0a 20 20 20 20 20 20 20 20 2a 2a  orary.        **
20f75 20 74 61 62 6c 65 20 61 6c 6c 6f 63 61 74 65 64   table allocated
20f76 20 61 6e 64 20 6f 70 65 6e 65 64 20 61 62 6f 76   and opened abov
20f77 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  e..        */.  
20f78 20 20 20 20 20 20 53 65 6c 65 63 74 44 65 73 74        SelectDest
20f79 20 64 65 73 74 3b 0a 20 20 20 20 20 20 20 20 45   dest;.        E
20f7a 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b  xprList *pEList;
20f7b 0a 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ..        assert
20f7c 28 20 21 69 73 52 6f 77 69 64 20 29 3b 0a 20 20  ( !isRowid );.  
20f7d 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c        sqlite3Sel
20f7e 65 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73  ectDestInit(&des
20f7f 74 2c 20 53 52 54 5f 53 65 74 2c 20 70 45 78 70  t, SRT_Set, pExp
20f80 72 2d 3e 69 54 61 62 6c 65 29 3b 0a 20 20 20 20  r->iTable);.    
20f81 20 20 20 20 64 65 73 74 2e 61 66 66 69 6e 69 74      dest.affinit
20f82 79 20 3d 20 28 75 38 29 61 66 66 69 6e 69 74 79  y = (u8)affinity
20f83 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
20f84 28 20 28 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  ( (pExpr->iTable
20f85 26 30 78 30 30 30 30 46 46 46 46 29 3d 3d 70 45  &0x0000FFFF)==pE
20f86 78 70 72 2d 3e 69 54 61 62 6c 65 20 29 3b 0a 20  xpr->iTable );. 
20f87 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
20f88 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c  e3Select(pParse,
20f89 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63   pExpr->x.pSelec
20f8a 74 2c 20 26 64 65 73 74 29 20 29 7b 0a 20 20 20  t, &dest) ){.   
20f8b 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20         return;. 
20f8c 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
20f8d 20 70 45 4c 69 73 74 20 3d 20 70 45 78 70 72 2d   pEList = pExpr-
20f8e 3e 78 2e 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69  >x.pSelect->pELi
20f8f 73 74 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  st;.        if( 
20f90 41 4c 57 41 59 53 28 70 45 4c 69 73 74 21 3d 30  ALWAYS(pEList!=0
20f91 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70   && pEList->nExp
20f92 72 3e 30 29 20 29 7b 20 0a 20 20 20 20 20 20 20  r>0) ){ .       
20f93 20 20 20 6b 65 79 49 6e 66 6f 2e 61 43 6f 6c 6c     keyInfo.aColl
20f94 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33 42 69 6e  [0] = sqlite3Bin
20f95 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65  aryCompareCollSe
20f96 71 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  q(pParse, pExpr-
20f97 3e 70 4c 65 66 74 2c 0a 20 20 20 20 20 20 20 20  >pLeft,.        
20f98 20 20 20 20 20 20 70 45 4c 69 73 74 2d 3e 61 5b        pEList->a[
20f99 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 20  0].pExpr);.     
20f9a 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
20f9b 20 69 66 28 20 70 45 78 70 72 2d 3e 78 2e 70 4c   if( pExpr->x.pL
20f9c 69 73 74 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  ist!=0 ){.      
20f9d 20 20 2f 2a 20 43 61 73 65 20 32 3a 20 20 20 20    /* Case 2:    
20f9e 20 65 78 70 72 20 49 4e 20 28 65 78 70 72 6c 69   expr IN (exprli
20f9f 73 74 29 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20  st).        **. 
20fa0 20 20 20 20 20 20 20 2a 2a 20 46 6f 72 20 65 61         ** For ea
20fa1 63 68 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 62  ch expression, b
20fa2 75 69 6c 64 20 61 6e 20 69 6e 64 65 78 20 6b 65  uild an index ke
20fa3 79 20 66 72 6f 6d 20 74 68 65 20 65 76 61 6c 75  y from the evalu
20fa4 61 74 69 6f 6e 20 61 6e 64 0a 20 20 20 20 20 20  ation and.      
20fa5 20 20 2a 2a 20 73 74 6f 72 65 20 69 74 20 69 6e    ** store it in
20fa6 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74   the temporary t
20fa7 61 62 6c 65 2e 20 49 66 20 3c 65 78 70 72 3e 20  able. If <expr> 
20fa8 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 74 68 65  is a column, the
20fa9 6e 20 75 73 65 0a 20 20 20 20 20 20 20 20 2a 2a  n use.        **
20faa 20 74 68 61 74 20 63 6f 6c 75 6d 6e 73 20 61 66   that columns af
20fab 66 69 6e 69 74 79 20 77 68 65 6e 20 62 75 69 6c  finity when buil
20fac 64 69 6e 67 20 69 6e 64 65 78 20 6b 65 79 73 2e  ding index keys.
20fad 20 49 66 20 3c 65 78 70 72 3e 20 69 73 20 6e 6f   If <expr> is no
20fae 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20 63  t.        ** a c
20faf 6f 6c 75 6d 6e 2c 20 75 73 65 20 6e 75 6d 65 72  olumn, use numer
20fb0 69 63 20 61 66 66 69 6e 69 74 79 2e 0a 20 20 20  ic affinity..   
20fb1 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
20fb2 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20 45  int i;.        E
20fb3 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d  xprList *pList =
20fb4 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b   pExpr->x.pList;
20fb5 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20  .        struct 
20fb6 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70  ExprList_item *p
20fb7 49 74 65 6d 3b 0a 20 20 20 20 20 20 20 20 69 6e  Item;.        in
20fb8 74 20 72 31 2c 20 72 32 2c 20 72 33 3b 0a 0a 20  t r1, r2, r3;.. 
20fb9 20 20 20 20 20 20 20 69 66 28 20 21 61 66 66 69         if( !affi
20fba 6e 69 74 79 20 29 7b 0a 20 20 20 20 20 20 20 20  nity ){.        
20fbb 20 20 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c    affinity = SQL
20fbc 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20  ITE_AFF_NONE;.  
20fbd 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
20fbe 6b 65 79 49 6e 66 6f 2e 61 43 6f 6c 6c 5b 30 5d  keyInfo.aColl[0]
20fbf 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
20fc0 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 45  llSeq(pParse, pE
20fc1 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 0a 20 20  xpr->pLeft);..  
20fc2 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 68        /* Loop th
20fc3 72 6f 75 67 68 20 65 61 63 68 20 65 78 70 72 65  rough each expre
20fc4 73 73 69 6f 6e 20 69 6e 20 3c 65 78 70 72 6c 69  ssion in <exprli
20fc5 73 74 3e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  st>. */.        
20fc6 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  r1 = sqlite3GetT
20fc7 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
20fc8 20 20 20 20 20 20 20 20 72 32 20 3d 20 73 71 6c          r2 = sql
20fc9 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
20fca 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20  Parse);.        
20fcb 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
20fcc 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c  2(v, OP_Null, 0,
20fcd 20 72 32 29 3b 0a 20 20 20 20 20 20 20 20 66 6f   r2);.        fo
20fce 72 28 69 3d 70 4c 69 73 74 2d 3e 6e 45 78 70 72  r(i=pList->nExpr
20fcf 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61  , pItem=pList->a
20fd0 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65  ; i>0; i--, pIte
20fd1 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  m++){.          
20fd2 45 78 70 72 20 2a 70 45 32 20 3d 20 70 49 74 65  Expr *pE2 = pIte
20fd3 6d 2d 3e 70 45 78 70 72 3b 0a 0a 20 20 20 20 20  m->pExpr;..     
20fd4 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 65       /* If the e
20fd5 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6e 6f 74  xpression is not
20fd6 20 63 6f 6e 73 74 61 6e 74 20 74 68 65 6e 20 77   constant then w
20fd7 65 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f 0a 20  e will need to. 
20fd8 20 20 20 20 20 20 20 20 20 2a 2a 20 64 69 73 61           ** disa
20fd9 62 6c 65 20 74 68 65 20 74 65 73 74 20 74 68 61  ble the test tha
20fda 74 20 77 61 73 20 67 65 6e 65 72 61 74 65 64 20  t was generated 
20fdb 61 62 6f 76 65 20 74 68 61 74 20 6d 61 6b 65 73  above that makes
20fdc 20 73 75 72 65 0a 20 20 20 20 20 20 20 20 20 20   sure.          
20fdd 2a 2a 20 74 68 69 73 20 63 6f 64 65 20 6f 6e 6c  ** this code onl
20fde 79 20 65 78 65 63 75 74 65 73 20 6f 6e 63 65 2e  y executes once.
20fdf 20 20 42 65 63 61 75 73 65 20 66 6f 72 20 61 20    Because for a 
20fe0 6e 6f 6e 2d 63 6f 6e 73 74 61 6e 74 0a 20 20 20  non-constant.   
20fe1 20 20 20 20 20 20 20 2a 2a 20 65 78 70 72 65 73         ** expres
20fe2 73 69 6f 6e 20 77 65 20 6e 65 65 64 20 74 6f 20  sion we need to 
20fe3 72 65 72 75 6e 20 74 68 69 73 20 63 6f 64 65 20  rerun this code 
20fe4 65 61 63 68 20 74 69 6d 65 2e 0a 20 20 20 20 20  each time..     
20fe5 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
20fe6 20 20 69 66 28 20 74 65 73 74 41 64 64 72 20 26    if( testAddr &
20fe7 26 20 21 73 71 6c 69 74 65 33 45 78 70 72 49 73  & !sqlite3ExprIs
20fe8 43 6f 6e 73 74 61 6e 74 28 70 45 32 29 20 29 7b  Constant(pE2) ){
20fe9 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
20fea 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f  ite3VdbeChangeTo
20feb 4e 6f 6f 70 28 76 2c 20 74 65 73 74 41 64 64 72  Noop(v, testAddr
20fec 2d 31 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20  -1, 2);.        
20fed 20 20 20 20 74 65 73 74 41 64 64 72 20 3d 20 30      testAddr = 0
20fee 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 0a 20  ;.          }.. 
20fef 20 20 20 20 20 20 20 20 20 2f 2a 20 45 76 61 6c           /* Eval
20ff0 75 61 74 65 20 74 68 65 20 65 78 70 72 65 73 73  uate the express
20ff1 69 6f 6e 20 61 6e 64 20 69 6e 73 65 72 74 20 69  ion and insert i
20ff2 74 20 69 6e 74 6f 20 74 68 65 20 74 65 6d 70 20  t into the temp 
20ff3 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20  table */.       
20ff4 20 20 20 72 33 20 3d 20 73 71 6c 69 74 65 33 45     r3 = sqlite3E
20ff5 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50  xprCodeTarget(pP
20ff6 61 72 73 65 2c 20 70 45 32 2c 20 72 31 29 3b 0a  arse, pE2, r1);.
20ff7 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 73            if( is
20ff8 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20  Rowid ){.       
20ff9 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
20ffa 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4d 75 73  AddOp2(v, OP_Mus
20ffb 74 42 65 49 6e 74 2c 20 72 33 2c 20 73 71 6c 69  tBeInt, r3, sqli
20ffc 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
20ffd 64 72 28 76 29 2b 32 29 3b 0a 20 20 20 20 20 20  dr(v)+2);.      
20ffe 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
20fff 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e  eAddOp3(v, OP_In
21000 73 65 72 74 2c 20 70 45 78 70 72 2d 3e 69 54 61  sert, pExpr->iTa
21001 62 6c 65 2c 20 72 32 2c 20 72 33 29 3b 0a 20 20  ble, r2, r3);.  
21002 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
21003 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
21004 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
21005 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72  OP_MakeRecord, r
21006 33 2c 20 31 2c 20 72 32 2c 20 26 61 66 66 69 6e  3, 1, r2, &affin
21007 69 74 79 2c 20 31 29 3b 0a 20 20 20 20 20 20 20  ity, 1);.       
21008 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
21009 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61  CacheAffinityCha
2100a 6e 67 65 28 70 50 61 72 73 65 2c 20 72 33 2c 20  nge(pParse, r3, 
2100b 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  1);.            
2100c 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2100d 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72  2(v, OP_IdxInser
2100e 74 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  t, pExpr->iTable
2100f 2c 20 72 32 29 3b 0a 20 20 20 20 20 20 20 20 20  , r2);.         
21010 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
21011 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65       sqlite3Rele
21012 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
21013 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20  e, r1);.        
21014 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
21015 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 32  mpReg(pParse, r2
21016 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
21017 20 69 66 28 20 21 69 73 52 6f 77 69 64 20 29 7b   if( !isRowid ){
21018 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
21019 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
2101a 61 64 64 72 2c 20 28 76 6f 69 64 20 2a 29 26 6b  addr, (void *)&k
2101b 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e  eyInfo, P4_KEYIN
2101c 46 4f 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  FO);.      }.   
2101d 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
2101e 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 58 49  .    case TK_EXI
2101f 53 54 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  STS:.    case TK
21020 5f 53 45 4c 45 43 54 3a 0a 20 20 20 20 64 65 66  _SELECT:.    def
21021 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 2f 2a  ault: {.      /*
21022 20 49 66 20 74 68 69 73 20 68 61 73 20 74 6f 20   If this has to 
21023 62 65 20 61 20 73 63 61 6c 61 72 20 53 45 4c 45  be a scalar SELE
21024 43 54 2e 20 20 47 65 6e 65 72 61 74 65 20 63 6f  CT.  Generate co
21025 64 65 20 74 6f 20 70 75 74 20 74 68 65 0a 20 20  de to put the.  
21026 20 20 20 20 2a 2a 20 76 61 6c 75 65 20 6f 66 20      ** value of 
21027 74 68 69 73 20 73 65 6c 65 63 74 20 69 6e 20 61  this select in a
21028 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61 6e 64   memory cell and
21029 20 72 65 63 6f 72 64 20 74 68 65 20 6e 75 6d 62   record the numb
2102a 65 72 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 74  er.      ** of t
2102b 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69  he memory cell i
2102c 6e 20 69 43 6f 6c 75 6d 6e 2e 20 20 49 66 20 74  n iColumn.  If t
2102d 68 69 73 20 69 73 20 61 6e 20 45 58 49 53 54 53  his is an EXISTS
2102e 2c 20 77 72 69 74 65 0a 20 20 20 20 20 20 2a 2a  , write.      **
2102f 20 61 6e 20 69 6e 74 65 67 65 72 20 30 20 28 6e   an integer 0 (n
21030 6f 74 20 65 78 69 73 74 73 29 20 6f 72 20 31 20  ot exists) or 1 
21031 28 65 78 69 73 74 73 29 20 69 6e 74 6f 20 61 20  (exists) into a 
21032 6d 65 6d 6f 72 79 20 63 65 6c 6c 0a 20 20 20 20  memory cell.    
21033 20 20 2a 2a 20 61 6e 64 20 72 65 63 6f 72 64 20    ** and record 
21034 74 68 61 74 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  that memory cell
21035 20 69 6e 20 69 43 6f 6c 75 6d 6e 2e 0a 20 20 20   in iColumn..   
21036 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 74 61 74     */.      stat
21037 69 63 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 6f  ic const Token o
21038 6e 65 20 3d 20 7b 20 22 31 22 2c 20 31 20 7d 3b  ne = { "1", 1 };
21039 20 20 2f 2a 20 54 6f 6b 65 6e 20 66 6f 72 20 6c    /* Token for l
2103a 69 74 65 72 61 6c 20 76 61 6c 75 65 20 31 20 2a  iteral value 1 *
2103b 2f 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a  /.      Select *
2103c 70 53 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20  pSel;           
2103d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2103e 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2103f 74 20 74 6f 20 65 6e 63 6f 64 65 20 2a 2f 0a 20  t to encode */. 
21040 20 20 20 20 20 53 65 6c 65 63 74 44 65 73 74 20       SelectDest 
21041 64 65 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  dest;           
21042 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f             /* Ho
21043 77 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 53  w to deal with S
21044 45 4c 45 43 74 20 72 65 73 75 6c 74 20 2a 2f 0a  ELECt result */.
21045 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21046 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45   pExpr->op==TK_E
21047 58 49 53 54 53 20 29 3b 0a 20 20 20 20 20 20 74  XISTS );.      t
21048 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e  estcase( pExpr->
21049 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 29 3b  op==TK_SELECT );
2104a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
2104b 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 49  Expr->op==TK_EXI
2104c 53 54 53 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70  STS || pExpr->op
2104d 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 29 3b 0a 0a  ==TK_SELECT );..
2104e 20 20 20 20 20 20 61 73 73 65 72 74 28 20 45 78        assert( Ex
2104f 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
21050 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63  xpr, EP_xIsSelec
21051 74 29 20 29 3b 0a 20 20 20 20 20 20 70 53 65 6c  t) );.      pSel
21052 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c   = pExpr->x.pSel
21053 65 63 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ect;.      sqlit
21054 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74  e3SelectDestInit
21055 28 26 64 65 73 74 2c 20 30 2c 20 2b 2b 70 50 61  (&dest, 0, ++pPa
21056 72 73 65 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20  rse->nMem);.    
21057 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d    if( pExpr->op=
21058 3d 54 4b 5f 53 45 4c 45 43 54 20 29 7b 0a 20 20  =TK_SELECT ){.  
21059 20 20 20 20 20 20 64 65 73 74 2e 65 44 65 73 74        dest.eDest
2105a 20 3d 20 53 52 54 5f 4d 65 6d 3b 0a 20 20 20 20   = SRT_Mem;.    
2105b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2105c 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp2(v, OP_Null
2105d 2c 20 30 2c 20 64 65 73 74 2e 69 50 61 72 6d 29  , 0, dest.iParm)
2105e 3b 0a 20 20 20 20 20 20 20 20 56 64 62 65 43 6f  ;.        VdbeCo
2105f 6d 6d 65 6e 74 28 28 76 2c 20 22 49 6e 69 74 20  mment((v, "Init 
21060 73 75 62 71 75 65 72 79 20 72 65 73 75 6c 74 22  subquery result"
21061 29 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ));.      }else{
21062 0a 20 20 20 20 20 20 20 20 64 65 73 74 2e 65 44  .        dest.eD
21063 65 73 74 20 3d 20 53 52 54 5f 45 78 69 73 74 73  est = SRT_Exists
21064 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
21065 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
21066 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 64 65  P_Integer, 0, de
21067 73 74 2e 69 50 61 72 6d 29 3b 0a 20 20 20 20 20  st.iParm);.     
21068 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
21069 76 2c 20 22 49 6e 69 74 20 45 58 49 53 54 53 20  v, "Init EXISTS 
2106a 72 65 73 75 6c 74 22 29 29 3b 0a 20 20 20 20 20  result"));.     
2106b 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
2106c 45 78 70 72 44 65 6c 65 74 65 28 70 50 61 72 73  ExprDelete(pPars
2106d 65 2d 3e 64 62 2c 20 70 53 65 6c 2d 3e 70 4c 69  e->db, pSel->pLi
2106e 6d 69 74 29 3b 0a 20 20 20 20 20 20 70 53 65 6c  mit);.      pSel
2106f 2d 3e 70 4c 69 6d 69 74 20 3d 20 73 71 6c 69 74  ->pLimit = sqlit
21070 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
21071 54 4b 5f 49 4e 54 45 47 45 52 2c 20 30 2c 20 30  TK_INTEGER, 0, 0
21072 2c 20 26 6f 6e 65 29 3b 0a 20 20 20 20 20 20 69  , &one);.      i
21073 66 28 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  f( sqlite3Select
21074 28 70 50 61 72 73 65 2c 20 70 53 65 6c 2c 20 26  (pParse, pSel, &
21075 64 65 73 74 29 20 29 7b 0a 20 20 20 20 20 20 20  dest) ){.       
21076 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
21077 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43  .      pExpr->iC
21078 6f 6c 75 6d 6e 20 3d 20 28 69 31 36 29 64 65 73  olumn = (i16)des
21079 74 2e 69 50 61 72 6d 3b 0a 20 20 20 20 20 20 45  t.iParm;.      E
2107a 78 70 72 53 65 74 49 72 72 65 64 75 63 69 62 6c  xprSetIrreducibl
2107b 65 28 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20  e(pExpr);.      
2107c 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
2107d 0a 0a 20 20 69 66 28 20 74 65 73 74 41 64 64 72  ..  if( testAddr
2107e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
2107f 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 74  dbeJumpHere(v, t
21080 65 73 74 41 64 64 72 2d 31 29 3b 0a 20 20 7d 0a  estAddr-1);.  }.
21081 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
21082 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29  hePop(pParse, 1)
21083 3b 0a 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 23  ;..  return;.}.#
21084 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
21085 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f  OMIT_SUBQUERY */
21086 0a 0a 2f 2a 0a 2a 2a 20 44 75 70 6c 69 63 61 74  ../*.** Duplicat
21087 65 20 61 6e 20 38 2d 62 79 74 65 20 76 61 6c 75  e an 8-byte valu
21088 65 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72  e.*/.static char
21089 20 2a 64 75 70 38 62 79 74 65 73 28 56 64 62 65   *dup8bytes(Vdbe
2108a 20 2a 76 2c 20 63 6f 6e 73 74 20 63 68 61 72 20   *v, const char 
2108b 2a 69 6e 29 7b 0a 20 20 63 68 61 72 20 2a 6f 75  *in){.  char *ou
2108c 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  t = sqlite3DbMal
2108d 6c 6f 63 52 61 77 28 73 71 6c 69 74 65 33 56 64  locRaw(sqlite3Vd
2108e 62 65 44 62 28 76 29 2c 20 38 29 3b 0a 20 20 69  beDb(v), 8);.  i
2108f 66 28 20 6f 75 74 20 29 7b 0a 20 20 20 20 6d 65  f( out ){.    me
21090 6d 63 70 79 28 6f 75 74 2c 20 69 6e 2c 20 38 29  mcpy(out, in, 8)
21091 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6f  ;.  }.  return o
21092 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e  ut;.}../*.** Gen
21093 65 72 61 74 65 20 61 6e 20 69 6e 73 74 72 75 63  erate an instruc
21094 74 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 70  tion that will p
21095 75 74 20 74 68 65 20 66 6c 6f 61 74 69 6e 67 20  ut the floating 
21096 70 6f 69 6e 74 0a 2a 2a 20 76 61 6c 75 65 20 64  point.** value d
21097 65 73 63 72 69 62 65 64 20 62 79 20 7a 5b 30 2e  escribed by z[0.
21098 2e 6e 2d 31 5d 20 69 6e 74 6f 20 72 65 67 69 73  .n-1] into regis
21099 74 65 72 20 69 4d 65 6d 2e 0a 2a 2a 0a 2a 2a 20  ter iMem..**.** 
2109a 54 68 65 20 7a 5b 5d 20 73 74 72 69 6e 67 20 77  The z[] string w
2109b 69 6c 6c 20 70 72 6f 62 61 62 6c 79 20 6e 6f 74  ill probably not
2109c 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61   be zero-termina
2109d 74 65 64 2e 20 20 42 75 74 20 74 68 65 20 0a 2a  ted.  But the .*
2109e 2a 20 7a 5b 6e 5d 20 63 68 61 72 61 63 74 65 72  * z[n] character
2109f 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74   is guaranteed t
210a0 6f 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 20 74  o be something t
210a1 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6c 6f 6f  hat does not loo
210a2 6b 0a 2a 2a 20 6c 69 6b 65 20 74 68 65 20 63 6f  k.** like the co
210a3 6e 74 69 6e 75 61 74 69 6f 6e 20 6f 66 20 74 68  ntinuation of th
210a4 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61  e number..*/.sta
210a5 74 69 63 20 76 6f 69 64 20 63 6f 64 65 52 65 61  tic void codeRea
210a6 6c 28 56 64 62 65 20 2a 76 2c 20 63 6f 6e 73 74  l(Vdbe *v, const
210a7 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 65   char *z, int ne
210a8 67 61 74 65 46 6c 61 67 2c 20 69 6e 74 20 69 4d  gateFlag, int iM
210a9 65 6d 29 7b 0a 20 20 69 66 28 20 41 4c 57 41 59  em){.  if( ALWAY
210aa 53 28 7a 21 3d 30 29 20 29 7b 0a 20 20 20 20 64  S(z!=0) ){.    d
210ab 6f 75 62 6c 65 20 76 61 6c 75 65 3b 0a 20 20 20  ouble value;.   
210ac 20 63 68 61 72 20 2a 7a 56 3b 0a 20 20 20 20 73   char *zV;.    s
210ad 71 6c 69 74 65 33 41 74 6f 46 28 7a 2c 20 26 76  qlite3AtoF(z, &v
210ae 61 6c 75 65 29 3b 0a 20 20 20 20 61 73 73 65 72  alue);.    asser
210af 74 28 20 21 73 71 6c 69 74 65 33 49 73 4e 61 4e  t( !sqlite3IsNaN
210b0 28 76 61 6c 75 65 29 20 29 3b 20 2f 2a 20 54 68  (value) ); /* Th
210b1 65 20 6e 65 77 20 41 74 6f 46 20 6e 65 76 65 72  e new AtoF never
210b2 20 72 65 74 75 72 6e 73 20 4e 61 4e 20 2a 2f 0a   returns NaN */.
210b3 20 20 20 20 69 66 28 20 6e 65 67 61 74 65 46 6c      if( negateFl
210b4 61 67 20 29 20 76 61 6c 75 65 20 3d 20 2d 76 61  ag ) value = -va
210b5 6c 75 65 3b 0a 20 20 20 20 7a 56 20 3d 20 64 75  lue;.    zV = du
210b6 70 38 62 79 74 65 73 28 76 2c 20 28 63 68 61 72  p8bytes(v, (char
210b7 2a 29 26 76 61 6c 75 65 29 3b 0a 20 20 20 20 73  *)&value);.    s
210b8 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
210b9 28 76 2c 20 4f 50 5f 52 65 61 6c 2c 20 30 2c 20  (v, OP_Real, 0, 
210ba 69 4d 65 6d 2c 20 30 2c 20 7a 56 2c 20 50 34 5f  iMem, 0, zV, P4_
210bb 52 45 41 4c 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f  REAL);.  }.}.../
210bc 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 6e  *.** Generate an
210bd 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 61   instruction tha
210be 74 20 77 69 6c 6c 20 70 75 74 20 74 68 65 20 69  t will put the i
210bf 6e 74 65 67 65 72 20 64 65 73 63 72 69 62 65 20  nteger describe 
210c0 62 79 0a 2a 2a 20 74 65 78 74 20 7a 5b 30 2e 2e  by.** text z[0..
210c1 6e 2d 31 5d 20 69 6e 74 6f 20 72 65 67 69 73 74  n-1] into regist
210c2 65 72 20 69 4d 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54  er iMem..**.** T
210c3 68 65 20 7a 5b 5d 20 73 74 72 69 6e 67 20 77 69  he z[] string wi
210c4 6c 6c 20 70 72 6f 62 61 62 6c 79 20 6e 6f 74 20  ll probably not 
210c5 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74  be zero-terminat
210c6 65 64 2e 20 20 42 75 74 20 74 68 65 20 0a 2a 2a  ed.  But the .**
210c7 20 7a 5b 6e 5d 20 63 68 61 72 61 63 74 65 72 20   z[n] character 
210c8 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f  is guaranteed to
210c9 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 20 74 68   be something th
210ca 61 74 20 64 6f 65 73 20 6e 6f 74 20 6c 6f 6f 6b  at does not look
210cb 0a 2a 2a 20 6c 69 6b 65 20 74 68 65 20 63 6f 6e  .** like the con
210cc 74 69 6e 75 61 74 69 6f 6e 20 6f 66 20 74 68 65  tinuation of the
210cd 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74   number..*/.stat
210ce 69 63 20 76 6f 69 64 20 63 6f 64 65 49 6e 74 65  ic void codeInte
210cf 67 65 72 28 56 64 62 65 20 2a 76 2c 20 45 78 70  ger(Vdbe *v, Exp
210d0 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 6e 65  r *pExpr, int ne
210d1 67 46 6c 61 67 2c 20 69 6e 74 20 69 4d 65 6d 29  gFlag, int iMem)
210d2 7b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 66  {.  if( pExpr->f
210d3 6c 61 67 73 20 26 20 45 50 5f 49 6e 74 56 61 6c  lags & EP_IntVal
210d4 75 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 20  ue ){.    int i 
210d5 3d 20 70 45 78 70 72 2d 3e 75 2e 69 56 61 6c 75  = pExpr->u.iValu
210d6 65 3b 0a 20 20 20 20 69 66 28 20 6e 65 67 46 6c  e;.    if( negFl
210d7 61 67 20 29 20 69 20 3d 20 2d 69 3b 0a 20 20 20  ag ) i = -i;.   
210d8 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
210d9 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
210da 2c 20 69 2c 20 69 4d 65 6d 29 3b 0a 20 20 7d 65  , i, iMem);.  }e
210db 6c 73 65 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63  lse{.    const c
210dc 68 61 72 20 2a 7a 20 3d 20 70 45 78 70 72 2d 3e  har *z = pExpr->
210dd 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 61 73  u.zToken;.    as
210de 73 65 72 74 28 20 7a 21 3d 30 20 29 3b 0a 20 20  sert( z!=0 );.  
210df 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 74    if( sqlite3Fit
210e0 73 49 6e 36 34 42 69 74 73 28 7a 2c 20 6e 65 67  sIn64Bits(z, neg
210e1 46 6c 61 67 29 20 29 7b 0a 20 20 20 20 20 20 69  Flag) ){.      i
210e2 36 34 20 76 61 6c 75 65 3b 0a 20 20 20 20 20 20  64 value;.      
210e3 63 68 61 72 20 2a 7a 56 3b 0a 20 20 20 20 20 20  char *zV;.      
210e4 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 7a 2c  sqlite3Atoi64(z,
210e5 20 26 76 61 6c 75 65 29 3b 0a 20 20 20 20 20 20   &value);.      
210e6 69 66 28 20 6e 65 67 46 6c 61 67 20 29 20 76 61  if( negFlag ) va
210e7 6c 75 65 20 3d 20 2d 76 61 6c 75 65 3b 0a 20 20  lue = -value;.  
210e8 20 20 20 20 7a 56 20 3d 20 64 75 70 38 62 79 74      zV = dup8byt
210e9 65 73 28 76 2c 20 28 63 68 61 72 2a 29 26 76 61  es(v, (char*)&va
210ea 6c 75 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  lue);.      sqli
210eb 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
210ec 20 4f 50 5f 49 6e 74 36 34 2c 20 30 2c 20 69 4d   OP_Int64, 0, iM
210ed 65 6d 2c 20 30 2c 20 7a 56 2c 20 50 34 5f 49 4e  em, 0, zV, P4_IN
210ee 54 36 34 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  T64);.    }else{
210ef 0a 20 20 20 20 20 20 63 6f 64 65 52 65 61 6c 28  .      codeReal(
210f0 76 2c 20 7a 2c 20 6e 65 67 46 6c 61 67 2c 20 69  v, z, negFlag, i
210f1 4d 65 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Mem);.    }.  }.
210f2 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 61  }../*.** Clear a
210f3 20 63 61 63 68 65 20 65 6e 74 72 79 2e 0a 2a 2f   cache entry..*/
210f4 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 61 63  .static void cac
210f5 68 65 45 6e 74 72 79 43 6c 65 61 72 28 50 61 72  heEntryClear(Par
210f6 73 65 20 2a 70 50 61 72 73 65 2c 20 73 74 72 75  se *pParse, stru
210f7 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 29  ct yColCache *p)
210f8 7b 0a 20 20 69 66 28 20 70 2d 3e 74 65 6d 70 52  {.  if( p->tempR
210f9 65 67 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50  eg ){.    if( pP
210fa 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 3c 41  arse->nTempReg<A
210fb 72 72 61 79 53 69 7a 65 28 70 50 61 72 73 65 2d  rraySize(pParse-
210fc 3e 61 54 65 6d 70 52 65 67 29 20 29 7b 0a 20 20  >aTempReg) ){.  
210fd 20 20 20 20 70 50 61 72 73 65 2d 3e 61 54 65 6d      pParse->aTem
210fe 70 52 65 67 5b 70 50 61 72 73 65 2d 3e 6e 54 65  pReg[pParse->nTe
210ff 6d 70 52 65 67 2b 2b 5d 20 3d 20 70 2d 3e 69 52  mpReg++] = p->iR
21100 65 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d  eg;.    }.    p-
21101 3e 74 65 6d 70 52 65 67 20 3d 20 30 3b 0a 20 20  >tempReg = 0;.  
21102 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f  }.}.../*.** Reco
21103 72 64 20 69 6e 20 74 68 65 20 63 6f 6c 75 6d 6e  rd in the column
21104 20 63 61 63 68 65 20 74 68 61 74 20 61 20 70 61   cache that a pa
21105 72 74 69 63 75 6c 61 72 20 63 6f 6c 75 6d 6e 20  rticular column 
21106 66 72 6f 6d 20 61 0a 2a 2a 20 70 61 72 74 69 63  from a.** partic
21107 75 6c 61 72 20 74 61 62 6c 65 20 69 73 20 73 74  ular table is st
21108 6f 72 65 64 20 69 6e 20 61 20 70 61 72 74 69 63  ored in a partic
21109 75 6c 61 72 20 72 65 67 69 73 74 65 72 2e 0a 2a  ular register..*
2110a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2110b 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
2110c 72 43 61 63 68 65 53 74 6f 72 65 28 50 61 72 73  rCacheStore(Pars
2110d 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69  e *pParse, int i
2110e 54 61 62 2c 20 69 6e 74 20 69 43 6f 6c 2c 20 69  Tab, int iCol, i
2110f 6e 74 20 69 52 65 67 29 7b 0a 20 20 69 6e 74 20  nt iReg){.  int 
21110 69 3b 0a 20 20 69 6e 74 20 6d 69 6e 4c 72 75 3b  i;.  int minLru;
21111 0a 20 20 69 6e 74 20 69 64 78 4c 72 75 3b 0a 20  .  int idxLru;. 
21112 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68   struct yColCach
21113 65 20 2a 70 3b 0a 0a 20 20 61 73 73 65 72 74 28  e *p;..  assert(
21114 20 69 52 65 67 3e 30 20 29 3b 20 20 2f 2a 20 52   iReg>0 );  /* R
21115 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 73 20  egister numbers 
21116 61 72 65 20 61 6c 77 61 79 73 20 70 6f 73 69 74  are always posit
21117 69 76 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ive */.  assert(
21118 20 69 43 6f 6c 3e 3d 2d 31 20 26 26 20 69 43 6f   iCol>=-1 && iCo
21119 6c 3c 33 32 37 36 38 20 29 3b 20 20 2f 2a 20 46  l<32768 );  /* F
2111a 69 6e 69 74 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d  inite column num
2111b 62 65 72 73 20 2a 2f 0a 0a 20 20 2f 2a 20 46 69  bers */..  /* Fi
2111c 72 73 74 20 72 65 70 6c 61 63 65 20 61 6e 79 20  rst replace any 
2111d 65 78 69 73 74 69 6e 67 20 65 6e 74 72 79 20 2a  existing entry *
2111e 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70  /.  for(i=0, p=p
2111f 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65  Parse->aColCache
21120 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c  ; i<SQLITE_N_COL
21121 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29  CACHE; i++, p++)
21122 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 52 65  {.    if( p->iRe
21123 67 20 26 26 20 70 2d 3e 69 54 61 62 6c 65 3d 3d  g && p->iTable==
21124 69 54 61 62 20 26 26 20 70 2d 3e 69 43 6f 6c 75  iTab && p->iColu
21125 6d 6e 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20 20 20  mn==iCol ){.    
21126 20 20 63 61 63 68 65 45 6e 74 72 79 43 6c 65 61    cacheEntryClea
21127 72 28 70 50 61 72 73 65 2c 20 70 29 3b 0a 20 20  r(pParse, p);.  
21128 20 20 20 20 70 2d 3e 69 4c 65 76 65 6c 20 3d 20      p->iLevel = 
21129 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65  pParse->iCacheLe
2112a 76 65 6c 3b 0a 20 20 20 20 20 20 70 2d 3e 69 52  vel;.      p->iR
2112b 65 67 20 3d 20 69 52 65 67 3b 0a 20 20 20 20 20  eg = iReg;.     
2112c 20 70 2d 3e 61 66 66 43 68 61 6e 67 65 20 3d 20   p->affChange = 
2112d 30 3b 0a 20 20 20 20 20 20 70 2d 3e 6c 72 75 20  0;.      p->lru 
2112e 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65  = pParse->iCache
2112f 43 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74  Cnt++;.      ret
21130 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  urn;.    }.  }..
21131 20 20 2f 2a 20 46 69 6e 64 20 61 6e 20 65 6d 70    /* Find an emp
21132 74 79 20 73 6c 6f 74 20 61 6e 64 20 72 65 70 6c  ty slot and repl
21133 61 63 65 20 69 74 20 2a 2f 0a 20 20 66 6f 72 28  ace it */.  for(
21134 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61  i=0, p=pParse->a
21135 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49  ColCache; i<SQLI
21136 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69  TE_N_COLCACHE; i
21137 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66  ++, p++){.    if
21138 28 20 70 2d 3e 69 52 65 67 3d 3d 30 20 29 7b 0a  ( p->iReg==0 ){.
21139 20 20 20 20 20 20 70 2d 3e 69 4c 65 76 65 6c 20        p->iLevel 
2113a 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65  = pParse->iCache
2113b 4c 65 76 65 6c 3b 0a 20 20 20 20 20 20 70 2d 3e  Level;.      p->
2113c 69 54 61 62 6c 65 20 3d 20 69 54 61 62 3b 0a 20  iTable = iTab;. 
2113d 20 20 20 20 20 70 2d 3e 69 43 6f 6c 75 6d 6e 20       p->iColumn 
2113e 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20 70 2d  = iCol;.      p-
2113f 3e 69 52 65 67 20 3d 20 69 52 65 67 3b 0a 20 20  >iReg = iReg;.  
21140 20 20 20 20 70 2d 3e 61 66 66 43 68 61 6e 67 65      p->affChange
21141 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 74   = 0;.      p->t
21142 65 6d 70 52 65 67 20 3d 20 30 3b 0a 20 20 20 20  empReg = 0;.    
21143 20 20 70 2d 3e 6c 72 75 20 3d 20 70 50 61 72 73    p->lru = pPars
21144 65 2d 3e 69 43 61 63 68 65 43 6e 74 2b 2b 3b 0a  e->iCacheCnt++;.
21145 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
21146 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65    }.  }..  /* Re
21147 70 6c 61 63 65 20 74 68 65 20 6c 61 73 74 20 72  place the last r
21148 65 63 65 6e 74 6c 79 20 75 73 65 64 20 2a 2f 0a  ecently used */.
21149 20 20 6d 69 6e 4c 72 75 20 3d 20 30 78 37 66 66    minLru = 0x7ff
2114a 66 66 66 66 66 3b 0a 20 20 69 64 78 4c 72 75 20  fffff;.  idxLru 
2114b 3d 20 2d 31 3b 0a 20 20 66 6f 72 28 69 3d 30 2c  = -1;.  for(i=0,
2114c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43   p=pParse->aColC
2114d 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e  ache; i<SQLITE_N
2114e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20  _COLCACHE; i++, 
2114f 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d  p++){.    if( p-
21150 3e 6c 72 75 3c 6d 69 6e 4c 72 75 20 29 7b 0a 20  >lru<minLru ){. 
21151 20 20 20 20 20 69 64 78 4c 72 75 20 3d 20 69 3b       idxLru = i;
21152 0a 20 20 20 20 20 20 6d 69 6e 4c 72 75 20 3d 20  .      minLru = 
21153 70 2d 3e 6c 72 75 3b 0a 20 20 20 20 7d 0a 20 20  p->lru;.    }.  
21154 7d 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 69  }.  if( ALWAYS(i
21155 64 78 4c 72 75 3e 3d 30 29 20 29 7b 0a 20 20 20  dxLru>=0) ){.   
21156 20 70 20 3d 20 26 70 50 61 72 73 65 2d 3e 61 43   p = &pParse->aC
21157 6f 6c 43 61 63 68 65 5b 69 64 78 4c 72 75 5d 3b  olCache[idxLru];
21158 0a 20 20 20 20 70 2d 3e 69 4c 65 76 65 6c 20 3d  .    p->iLevel =
21159 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c   pParse->iCacheL
2115a 65 76 65 6c 3b 0a 20 20 20 20 70 2d 3e 69 54 61  evel;.    p->iTa
2115b 62 6c 65 20 3d 20 69 54 61 62 3b 0a 20 20 20 20  ble = iTab;.    
2115c 70 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 69 43 6f  p->iColumn = iCo
2115d 6c 3b 0a 20 20 20 20 70 2d 3e 69 52 65 67 20 3d  l;.    p->iReg =
2115e 20 69 52 65 67 3b 0a 20 20 20 20 70 2d 3e 61 66   iReg;.    p->af
2115f 66 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 20  fChange = 0;.   
21160 20 70 2d 3e 74 65 6d 70 52 65 67 20 3d 20 30 3b   p->tempReg = 0;
21161 0a 20 20 20 20 70 2d 3e 6c 72 75 20 3d 20 70 50  .    p->lru = pP
21162 61 72 73 65 2d 3e 69 43 61 63 68 65 43 6e 74 2b  arse->iCacheCnt+
21163 2b 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  +;.    return;. 
21164 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 64 69   }.}../*.** Indi
21165 63 61 74 65 20 74 68 61 74 20 61 20 72 65 67 69  cate that a regi
21166 73 74 65 72 20 69 73 20 62 65 69 6e 67 20 6f 76  ster is being ov
21167 65 72 77 72 69 74 74 65 6e 2e 20 20 50 75 72 67  erwritten.  Purg
21168 65 20 74 68 65 20 72 65 67 69 73 74 65 72 0a 2a  e the register.*
21169 2a 20 66 72 6f 6d 20 74 68 65 20 63 6f 6c 75 6d  * from the colum
2116a 6e 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49  n cache..*/.SQLI
2116b 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2116c 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
2116d 52 65 6d 6f 76 65 28 50 61 72 73 65 20 2a 70 50  Remove(Parse *pP
2116e 61 72 73 65 2c 20 69 6e 74 20 69 52 65 67 29 7b  arse, int iReg){
2116f 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75  .  int i;.  stru
21170 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b  ct yColCache *p;
21171 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50  .  for(i=0, p=pP
21172 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b  arse->aColCache;
21173 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43   i<SQLITE_N_COLC
21174 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b  ACHE; i++, p++){
21175 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 52 65 67  .    if( p->iReg
21176 3d 3d 69 52 65 67 20 29 7b 0a 20 20 20 20 20 20  ==iReg ){.      
21177 63 61 63 68 65 45 6e 74 72 79 43 6c 65 61 72 28  cacheEntryClear(
21178 70 50 61 72 73 65 2c 20 70 29 3b 0a 20 20 20 20  pParse, p);.    
21179 20 20 70 2d 3e 69 52 65 67 20 3d 20 30 3b 0a 20    p->iReg = 0;. 
2117a 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
2117b 2a 20 52 65 6d 65 6d 62 65 72 20 74 68 65 20 63  * Remember the c
2117c 75 72 72 65 6e 74 20 63 6f 6c 75 6d 6e 20 63 61  urrent column ca
2117d 63 68 65 20 63 6f 6e 74 65 78 74 2e 20 20 41 6e  che context.  An
2117e 79 20 6e 65 77 20 65 6e 74 72 69 65 73 20 61 64  y new entries ad
2117f 64 65 64 0a 2a 2a 20 61 64 64 65 64 20 74 6f 20  ded.** added to 
21180 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65  the column cache
21181 20 61 66 74 65 72 20 74 68 69 73 20 63 61 6c 6c   after this call
21182 20 61 72 65 20 72 65 6d 6f 76 65 64 20 77 68 65   are removed whe
21183 6e 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73 70  n the.** corresp
21184 6f 6e 64 69 6e 67 20 70 6f 70 20 6f 63 63 75 72  onding pop occur
21185 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
21186 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
21187 33 45 78 70 72 43 61 63 68 65 50 75 73 68 28 50  3ExprCachePush(P
21188 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20  arse *pParse){. 
21189 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c   pParse->iCacheL
2118a 65 76 65 6c 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  evel++;.}../*.**
2118b 20 52 65 6d 6f 76 65 20 66 72 6f 6d 20 74 68 65   Remove from the
2118c 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 61 6e   column cache an
2118d 79 20 65 6e 74 72 69 65 73 20 74 68 61 74 20 77  y entries that w
2118e 65 72 65 20 61 64 64 65 64 20 73 69 6e 63 65 20  ere added since 
2118f 74 68 65 0a 2a 2a 20 74 68 65 20 70 72 65 76 69  the.** the previ
21190 6f 75 73 20 4e 20 50 75 73 68 20 6f 70 65 72 61  ous N Push opera
21191 74 69 6f 6e 73 2e 20 20 49 6e 20 6f 74 68 65 72  tions.  In other
21192 20 77 6f 72 64 73 2c 20 72 65 73 74 6f 72 65 20   words, restore 
21193 74 68 65 20 63 61 63 68 65 0a 2a 2a 20 74 6f 20  the cache.** to 
21194 74 68 65 20 73 74 61 74 65 20 69 74 20 77 61 73  the state it was
21195 20 69 6e 20 4e 20 50 75 73 68 65 73 20 61 67 6f   in N Pushes ago
21196 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
21197 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
21198 45 78 70 72 43 61 63 68 65 50 6f 70 28 50 61 72  ExprCachePop(Par
21199 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
2119a 4e 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73  N){.  int i;.  s
2119b 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20  truct yColCache 
2119c 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 4e 3e  *p;.  assert( N>
2119d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
2119e 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 76  Parse->iCacheLev
2119f 65 6c 3e 3d 4e 20 29 3b 0a 20 20 70 50 61 72 73  el>=N );.  pPars
211a0 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 20 2d  e->iCacheLevel -
211a1 3d 20 4e 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20  = N;.  for(i=0, 
211a2 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61  p=pParse->aColCa
211a3 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f  che; i<SQLITE_N_
211a4 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70  COLCACHE; i++, p
211a5 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e  ++){.    if( p->
211a6 69 52 65 67 20 26 26 20 70 2d 3e 69 4c 65 76 65  iReg && p->iLeve
211a7 6c 3e 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65  l>pParse->iCache
211a8 4c 65 76 65 6c 20 29 7b 0a 20 20 20 20 20 20 63  Level ){.      c
211a9 61 63 68 65 45 6e 74 72 79 43 6c 65 61 72 28 70  acheEntryClear(p
211aa 50 61 72 73 65 2c 20 70 29 3b 0a 20 20 20 20 20  Parse, p);.     
211ab 20 70 2d 3e 69 52 65 67 20 3d 20 30 3b 0a 20 20   p->iReg = 0;.  
211ac 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
211ad 20 57 68 65 6e 20 61 20 63 61 63 68 65 64 20 63   When a cached c
211ae 6f 6c 75 6d 6e 20 69 73 20 72 65 75 73 65 64 2c  olumn is reused,
211af 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20   make sure that 
211b0 69 74 73 20 72 65 67 69 73 74 65 72 20 69 73 0a  its register is.
211b1 2a 2a 20 6e 6f 20 6c 6f 6e 67 65 72 20 61 76 61  ** no longer ava
211b2 69 6c 61 62 6c 65 20 61 73 20 61 20 74 65 6d 70  ilable as a temp
211b3 20 72 65 67 69 73 74 65 72 2e 20 20 74 69 63 6b   register.  tick
211b4 65 74 20 23 33 38 37 39 3a 20 20 74 68 61 74 20  et #3879:  that 
211b5 73 61 6d 65 0a 2a 2a 20 72 65 67 69 73 74 65 72  same.** register
211b6 20 6d 69 67 68 74 20 62 65 20 69 6e 20 74 68 65   might be in the
211b7 20 63 61 63 68 65 20 69 6e 20 6d 75 6c 74 69 70   cache in multip
211b8 6c 65 20 70 6c 61 63 65 73 2c 20 73 6f 20 62 65  le places, so be
211b9 20 73 75 72 65 20 74 6f 0a 2a 2a 20 67 65 74 20   sure to.** get 
211ba 74 68 65 6d 20 61 6c 6c 2e 0a 2a 2f 0a 73 74 61  them all..*/.sta
211bb 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33  tic void sqlite3
211bc 45 78 70 72 43 61 63 68 65 50 69 6e 52 65 67 69  ExprCachePinRegi
211bd 73 74 65 72 28 50 61 72 73 65 20 2a 70 50 61 72  ster(Parse *pPar
211be 73 65 2c 20 69 6e 74 20 69 52 65 67 29 7b 0a 20  se, int iReg){. 
211bf 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74   int i;.  struct
211c0 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 20   yColCache *p;. 
211c1 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72   for(i=0, p=pPar
211c2 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69  se->aColCache; i
211c3 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43  <SQLITE_N_COLCAC
211c4 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20  HE; i++, p++){. 
211c5 20 20 20 69 66 28 20 70 2d 3e 69 52 65 67 3d 3d     if( p->iReg==
211c6 69 52 65 67 20 29 7b 0a 20 20 20 20 20 20 70 2d  iReg ){.      p-
211c7 3e 74 65 6d 70 52 65 67 20 3d 20 30 3b 0a 20 20  >tempReg = 0;.  
211c8 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
211c9 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
211ca 68 61 74 20 77 69 6c 6c 20 65 78 74 72 61 63 74  hat will extract
211cb 20 74 68 65 20 69 43 6f 6c 75 6d 6e 2d 74 68 20   the iColumn-th 
211cc 63 6f 6c 75 6d 6e 20 66 72 6f 6d 0a 2a 2a 20 74  column from.** t
211cd 61 62 6c 65 20 70 54 61 62 20 61 6e 64 20 73 74  able pTab and st
211ce 6f 72 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 76  ore the column v
211cf 61 6c 75 65 20 69 6e 20 61 20 72 65 67 69 73 74  alue in a regist
211d0 65 72 2e 20 20 41 6e 20 65 66 66 6f 72 74 0a 2a  er.  An effort.*
211d1 2a 20 69 73 20 6d 61 64 65 20 74 6f 20 73 74 6f  * is made to sto
211d2 72 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 76 61  re the column va
211d3 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
211d4 69 52 65 67 2c 20 62 75 74 20 74 68 69 73 20 69  iReg, but this i
211d5 73 0a 2a 2a 20 6e 6f 74 20 67 75 61 72 61 6e 74  s.** not guarant
211d6 65 65 64 2e 20 20 54 68 65 20 6c 6f 63 61 74 69  eed.  The locati
211d7 6f 6e 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  on of the column
211d8 20 76 61 6c 75 65 20 69 73 20 72 65 74 75 72 6e   value is return
211d9 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20  ed..**.** There 
211da 6d 75 73 74 20 62 65 20 61 6e 20 6f 70 65 6e 20  must be an open 
211db 63 75 72 73 6f 72 20 74 6f 20 70 54 61 62 20 69  cursor to pTab i
211dc 6e 20 69 54 61 62 6c 65 20 77 68 65 6e 20 74 68  n iTable when th
211dd 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73  is routine.** is
211de 20 63 61 6c 6c 65 64 2e 20 20 49 66 20 69 43 6f   called.  If iCo
211df 6c 75 6d 6e 3c 30 20 74 68 65 6e 20 63 6f 64 65  lumn<0 then code
211e0 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 74 68   is generated th
211e1 61 74 20 65 78 74 72 61 63 74 73 20 74 68 65 20  at extracts the 
211e2 72 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  rowid..**.** Thi
211e3 73 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 74 20  s routine might 
211e4 61 74 74 65 6d 70 74 20 74 6f 20 72 65 75 73 65  attempt to reuse
211e5 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
211e6 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 0a 2a 2a  e column that.**
211e7 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65   has already bee
211e8 6e 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20 61 20  n loaded into a 
211e9 72 65 67 69 73 74 65 72 2e 20 20 54 68 65 20 76  register.  The v
211ea 61 6c 75 65 20 77 69 6c 6c 20 61 6c 77 61 79 73  alue will always
211eb 0a 2a 2a 20 62 65 20 75 73 65 64 20 69 66 20 69  .** be used if i
211ec 74 20 68 61 73 20 6e 6f 74 20 75 6e 64 65 72 67  t has not underg
211ed 6f 6e 65 20 61 6e 79 20 61 66 66 69 6e 69 74 79  one any affinity
211ee 20 63 68 61 6e 67 65 73 2e 20 20 42 75 74 20 69   changes.  But i
211ef 66 0a 2a 2a 20 61 6e 20 61 66 66 69 6e 69 74 79  f.** an affinity
211f0 20 63 68 61 6e 67 65 20 68 61 73 20 6f 63 63 75   change has occu
211f1 72 72 65 64 2c 20 74 68 65 6e 20 74 68 65 20 63  rred, then the c
211f2 61 63 68 65 64 20 76 61 6c 75 65 20 77 69 6c 6c  ached value will
211f3 20 6f 6e 6c 79 20 62 65 0a 2a 2a 20 75 73 65 64   only be.** used
211f4 20 69 66 20 61 6c 6c 6f 77 41 66 66 43 68 6e 67   if allowAffChng
211f5 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 53 51 4c   is true..*/.SQL
211f6 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
211f7 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47  sqlite3ExprCodeG
211f8 65 74 43 6f 6c 75 6d 6e 28 0a 20 20 50 61 72 73  etColumn(.  Pars
211f9 65 20 2a 70 50 61 72 73 65 2c 20 20 20 2f 2a 20  e *pParse,   /* 
211fa 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 65  Parsing and code
211fb 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74   generating cont
211fc 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ext */.  Table *
211fd 70 54 61 62 2c 20 20 20 20 20 2f 2a 20 44 65 73  pTab,     /* Des
211fe 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 65 20  cription of the 
211ff 74 61 62 6c 65 20 77 65 20 61 72 65 20 72 65 61  table we are rea
21200 64 69 6e 67 20 66 72 6f 6d 20 2a 2f 0a 20 20 69  ding from */.  i
21201 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 20 20 20 20  nt iColumn,     
21202 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20  /* Index of the 
21203 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a  table column */.
21204 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20    int iTable,   
21205 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72     /* The cursor
21206 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65   pointing to the
21207 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20   table */.  int 
21208 69 52 65 67 2c 20 20 20 20 20 20 20 20 2f 2a 20  iReg,        /* 
21209 53 74 6f 72 65 20 72 65 73 75 6c 74 73 20 68 65  Store results he
2120a 72 65 20 2a 2f 0a 20 20 69 6e 74 20 61 6c 6c 6f  re */.  int allo
2120b 77 41 66 66 43 68 6e 67 20 2f 2a 20 54 72 75 65  wAffChng /* True
2120c 20 69 66 20 70 72 69 6f 72 20 61 66 66 69 6e 69   if prior affini
2120d 74 79 20 63 68 61 6e 67 65 73 20 61 72 65 20 4f  ty changes are O
2120e 4b 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a  K */.){.  Vdbe *
2120f 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
21210 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74  e;.  int i;.  st
21211 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a  ruct yColCache *
21212 70 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70  p;..  for(i=0, p
21213 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63  =pParse->aColCac
21214 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43  he; i<SQLITE_N_C
21215 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b  OLCACHE; i++, p+
21216 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69  +){.    if( p->i
21217 52 65 67 3e 30 20 26 26 20 70 2d 3e 69 54 61 62  Reg>0 && p->iTab
21218 6c 65 3d 3d 69 54 61 62 6c 65 20 26 26 20 70 2d  le==iTable && p-
21219 3e 69 43 6f 6c 75 6d 6e 3d 3d 69 43 6f 6c 75 6d  >iColumn==iColum
2121a 6e 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20  n.           && 
2121b 28 21 70 2d 3e 61 66 66 43 68 61 6e 67 65 20 7c  (!p->affChange |
2121c 7c 20 61 6c 6c 6f 77 41 66 66 43 68 6e 67 29 20  | allowAffChng) 
2121d 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6c 72 75 20  ){.      p->lru 
2121e 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65  = pParse->iCache
2121f 43 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 73 71 6c  Cnt++;.      sql
21220 69 74 65 33 45 78 70 72 43 61 63 68 65 50 69 6e  ite3ExprCachePin
21221 52 65 67 69 73 74 65 72 28 70 50 61 72 73 65 2c  Register(pParse,
21222 20 70 2d 3e 69 52 65 67 29 3b 0a 20 20 20 20 20   p->iReg);.     
21223 20 72 65 74 75 72 6e 20 70 2d 3e 69 52 65 67 3b   return p->iReg;
21224 0a 20 20 20 20 7d 0a 20 20 7d 20 20 0a 20 20 61  .    }.  }  .  a
21225 73 73 65 72 74 28 20 76 21 3d 30 20 29 3b 0a 20  ssert( v!=0 );. 
21226 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3c 30 20 29   if( iColumn<0 )
21227 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
21228 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f  eAddOp2(v, OP_Ro
21229 77 69 64 2c 20 69 54 61 62 6c 65 2c 20 69 52 65  wid, iTable, iRe
2122a 67 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  g);.  }else if( 
2122b 41 4c 57 41 59 53 28 70 54 61 62 21 3d 30 29 20  ALWAYS(pTab!=0) 
2122c 29 7b 0a 20 20 20 20 69 6e 74 20 6f 70 20 3d 20  ){.    int op = 
2122d 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
2122e 3f 20 4f 50 5f 56 43 6f 6c 75 6d 6e 20 3a 20 4f  ? OP_VColumn : O
2122f 50 5f 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 73 71  P_Column;.    sq
21230 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
21231 76 2c 20 6f 70 2c 20 69 54 61 62 6c 65 2c 20 69  v, op, iTable, i
21232 43 6f 6c 75 6d 6e 2c 20 69 52 65 67 29 3b 0a 20  Column, iReg);. 
21233 20 20 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e     sqlite3Column
21234 44 65 66 61 75 6c 74 28 76 2c 20 70 54 61 62 2c  Default(v, pTab,
21235 20 69 43 6f 6c 75 6d 6e 2c 20 69 52 65 67 29 3b   iColumn, iReg);
21236 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 78  .  }.  sqlite3Ex
21237 70 72 43 61 63 68 65 53 74 6f 72 65 28 70 50 61  prCacheStore(pPa
21238 72 73 65 2c 20 69 54 61 62 6c 65 2c 20 69 43 6f  rse, iTable, iCo
21239 6c 75 6d 6e 2c 20 69 52 65 67 29 3b 0a 20 20 72  lumn, iReg);.  r
2123a 65 74 75 72 6e 20 69 52 65 67 3b 0a 7d 0a 0a 2f  eturn iReg;.}../
2123b 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 6c 6c 20 63  *.** Clear all c
2123c 6f 6c 75 6d 6e 20 63 61 63 68 65 20 65 6e 74 72  olumn cache entr
2123d 69 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ies..*/.SQLITE_P
2123e 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2123f 74 65 33 45 78 70 72 43 61 63 68 65 43 6c 65 61  te3ExprCacheClea
21240 72 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29  r(Parse *pParse)
21241 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72  {.  int i;.  str
21242 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70  uct yColCache *p
21243 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d  ;..  for(i=0, p=
21244 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68  pParse->aColCach
21245 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f  e; i<SQLITE_N_CO
21246 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b  LCACHE; i++, p++
21247 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 52  ){.    if( p->iR
21248 65 67 20 29 7b 0a 20 20 20 20 20 20 63 61 63 68  eg ){.      cach
21249 65 45 6e 74 72 79 43 6c 65 61 72 28 70 50 61 72  eEntryClear(pPar
2124a 73 65 2c 20 70 29 3b 0a 20 20 20 20 20 20 70 2d  se, p);.      p-
2124b 3e 69 52 65 67 20 3d 20 30 3b 0a 20 20 20 20 7d  >iReg = 0;.    }
2124c 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
2124d 63 6f 72 64 20 74 68 65 20 66 61 63 74 20 74 68  cord the fact th
2124e 61 74 20 61 6e 20 61 66 66 69 6e 69 74 79 20 63  at an affinity c
2124f 68 61 6e 67 65 20 68 61 73 20 6f 63 63 75 72 72  hange has occurr
21250 65 64 20 6f 6e 20 69 43 6f 75 6e 74 0a 2a 2a 20  ed on iCount.** 
21251 72 65 67 69 73 74 65 72 73 20 73 74 61 72 74 69  registers starti
21252 6e 67 20 77 69 74 68 20 69 53 74 61 72 74 2e 0a  ng with iStart..
21253 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
21254 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
21255 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43  prCacheAffinityC
21256 68 61 6e 67 65 28 50 61 72 73 65 20 2a 70 50 61  hange(Parse *pPa
21257 72 73 65 2c 20 69 6e 74 20 69 53 74 61 72 74 2c  rse, int iStart,
21258 20 69 6e 74 20 69 43 6f 75 6e 74 29 7b 0a 20 20   int iCount){.  
21259 69 6e 74 20 69 45 6e 64 20 3d 20 69 53 74 61 72  int iEnd = iStar
2125a 74 20 2b 20 69 43 6f 75 6e 74 20 2d 20 31 3b 0a  t + iCount - 1;.
2125b 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63    int i;.  struc
2125c 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a  t yColCache *p;.
2125d 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61    for(i=0, p=pPa
2125e 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20  rse->aColCache; 
2125f 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41  i<SQLITE_N_COLCA
21260 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a  CHE; i++, p++){.
21261 20 20 20 20 69 6e 74 20 72 20 3d 20 70 2d 3e 69      int r = p->i
21262 52 65 67 3b 0a 20 20 20 20 69 66 28 20 72 3e 3d  Reg;.    if( r>=
21263 69 53 74 61 72 74 20 26 26 20 72 3c 3d 69 45 6e  iStart && r<=iEn
21264 64 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61 66  d ){.      p->af
21265 66 43 68 61 6e 67 65 20 3d 20 31 3b 0a 20 20 20  fChange = 1;.   
21266 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
21267 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
21268 20 6d 6f 76 65 20 63 6f 6e 74 65 6e 74 20 66 72   move content fr
21269 6f 6d 20 72 65 67 69 73 74 65 72 73 20 69 46 72  om registers iFr
2126a 6f 6d 2e 2e 2e 69 46 72 6f 6d 2b 6e 52 65 67 2d  om...iFrom+nReg-
2126b 31 0a 2a 2a 20 6f 76 65 72 20 74 6f 20 69 54 6f  1.** over to iTo
2126c 2e 2e 69 54 6f 2b 6e 52 65 67 2d 31 2e 20 4b 65  ..iTo+nReg-1. Ke
2126d 65 70 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61  ep the column ca
2126e 63 68 65 20 75 70 2d 74 6f 2d 64 61 74 65 2e 0a  che up-to-date..
2126f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
21270 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
21271 70 72 43 6f 64 65 4d 6f 76 65 28 50 61 72 73 65  prCodeMove(Parse
21272 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 46   *pParse, int iF
21273 72 6f 6d 2c 20 69 6e 74 20 69 54 6f 2c 20 69 6e  rom, int iTo, in
21274 74 20 6e 52 65 67 29 7b 0a 20 20 69 6e 74 20 69  t nReg){.  int i
21275 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43  ;.  struct yColC
21276 61 63 68 65 20 2a 70 3b 0a 20 20 69 66 28 20 4e  ache *p;.  if( N
21277 45 56 45 52 28 69 46 72 6f 6d 3d 3d 69 54 6f 29  EVER(iFrom==iTo)
21278 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c   ) return;.  sql
21279 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 70  ite3VdbeAddOp3(p
2127a 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50  Parse->pVdbe, OP
2127b 5f 4d 6f 76 65 2c 20 69 46 72 6f 6d 2c 20 69 54  _Move, iFrom, iT
2127c 6f 2c 20 6e 52 65 67 29 3b 0a 20 20 66 6f 72 28  o, nReg);.  for(
2127d 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61  i=0, p=pParse->a
2127e 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49  ColCache; i<SQLI
2127f 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69  TE_N_COLCACHE; i
21280 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 6e  ++, p++){.    in
21281 74 20 78 20 3d 20 70 2d 3e 69 52 65 67 3b 0a 20  t x = p->iReg;. 
21282 20 20 20 69 66 28 20 78 3e 3d 69 46 72 6f 6d 20     if( x>=iFrom 
21283 26 26 20 78 3c 69 46 72 6f 6d 2b 6e 52 65 67 20  && x<iFrom+nReg 
21284 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 67  ){.      p->iReg
21285 20 2b 3d 20 69 54 6f 2d 69 46 72 6f 6d 3b 0a 20   += iTo-iFrom;. 
21286 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
21287 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
21288 74 6f 20 63 6f 70 79 20 63 6f 6e 74 65 6e 74 20  to copy content 
21289 66 72 6f 6d 20 72 65 67 69 73 74 65 72 73 20 69  from registers i
2128a 46 72 6f 6d 2e 2e 2e 69 46 72 6f 6d 2b 6e 52 65  From...iFrom+nRe
2128b 67 2d 31 0a 2a 2a 20 6f 76 65 72 20 74 6f 20 69  g-1.** over to i
2128c 54 6f 2e 2e 69 54 6f 2b 6e 52 65 67 2d 31 2e 0a  To..iTo+nReg-1..
2128d 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2128e 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
2128f 70 72 43 6f 64 65 43 6f 70 79 28 50 61 72 73 65  prCodeCopy(Parse
21290 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 46   *pParse, int iF
21291 72 6f 6d 2c 20 69 6e 74 20 69 54 6f 2c 20 69 6e  rom, int iTo, in
21292 74 20 6e 52 65 67 29 7b 0a 20 20 69 6e 74 20 69  t nReg){.  int i
21293 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 69 46  ;.  if( NEVER(iF
21294 72 6f 6d 3d 3d 69 54 6f 29 20 29 20 72 65 74 75  rom==iTo) ) retu
21295 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  rn;.  for(i=0; i
21296 3c 6e 52 65 67 3b 20 69 2b 2b 29 7b 0a 20 20 20  <nReg; i++){.   
21297 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21298 70 32 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65  p2(pParse->pVdbe
21299 2c 20 4f 50 5f 43 6f 70 79 2c 20 69 46 72 6f 6d  , OP_Copy, iFrom
2129a 2b 69 2c 20 69 54 6f 2b 69 29 3b 0a 20 20 7d 0a  +i, iTo+i);.  }.
2129b 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
2129c 74 72 75 65 20 69 66 20 61 6e 79 20 72 65 67 69  true if any regi
2129d 73 74 65 72 20 69 6e 20 74 68 65 20 72 61 6e 67  ster in the rang
2129e 65 20 69 46 72 6f 6d 2e 2e 69 54 6f 20 28 69 6e  e iFrom..iTo (in
2129f 63 6c 75 73 69 76 65 29 0a 2a 2a 20 69 73 20 75  clusive).** is u
212a0 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74  sed as part of t
212a1 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 2e  he column cache.
212a2 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75  .*/.static int u
212a3 73 65 64 41 73 43 6f 6c 75 6d 6e 43 61 63 68 65  sedAsColumnCache
212a4 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
212a5 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74 20 69  int iFrom, int i
212a6 54 6f 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  To){.  int i;.  
212a7 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65  struct yColCache
212a8 20 2a 70 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20   *p;.  for(i=0, 
212a9 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61  p=pParse->aColCa
212aa 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f  che; i<SQLITE_N_
212ab 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70  COLCACHE; i++, p
212ac 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 72 20 3d  ++){.    int r =
212ad 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20 69 66   p->iReg;.    if
212ae 28 20 72 3e 3d 69 46 72 6f 6d 20 26 26 20 72 3c  ( r>=iFrom && r<
212af 3d 69 54 6f 20 29 20 72 65 74 75 72 6e 20 31 3b  =iTo ) return 1;
212b0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
212b1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  .}../*.** If the
212b2 20 6c 61 73 74 20 69 6e 73 74 72 75 63 74 69 6f   last instructio
212b3 6e 20 63 6f 64 65 64 20 69 73 20 61 6e 20 65 70  n coded is an ep
212b4 68 65 6d 65 72 61 6c 20 63 6f 70 79 20 6f 66 20  hemeral copy of 
212b5 61 6e 79 20 6f 66 0a 2a 2a 20 74 68 65 20 72 65  any of.** the re
212b6 67 69 73 74 65 72 73 20 69 6e 20 74 68 65 20 6e  gisters in the n
212b7 52 65 67 20 72 65 67 69 73 74 65 72 73 20 62 65  Reg registers be
212b8 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 69 52 65  ginning with iRe
212b9 67 2c 20 74 68 65 6e 0a 2a 2a 20 63 6f 6e 76 65  g, then.** conve
212ba 72 74 20 74 68 65 20 6c 61 73 74 20 69 6e 73 74  rt the last inst
212bb 72 75 63 74 69 6f 6e 20 66 72 6f 6d 20 4f 50 5f  ruction from OP_
212bc 53 43 6f 70 79 20 74 6f 20 4f 50 5f 43 6f 70 79  SCopy to OP_Copy
212bd 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
212be 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
212bf 45 78 70 72 48 61 72 64 43 6f 70 79 28 50 61 72  ExprHardCopy(Par
212c0 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
212c1 69 52 65 67 2c 20 69 6e 74 20 6e 52 65 67 29 7b  iReg, int nReg){
212c2 0a 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a  .  VdbeOp *pOp;.
212c3 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20 61 73    Vdbe *v;..  as
212c4 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 64 62  sert( pParse->db
212c5 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d  ->mallocFailed==
212c6 30 20 29 3b 0a 20 20 76 20 3d 20 70 50 61 72 73  0 );.  v = pPars
212c7 65 2d 3e 70 56 64 62 65 3b 0a 20 20 61 73 73 65  e->pVdbe;.  asse
212c8 72 74 28 20 76 21 3d 30 20 29 3b 0a 20 20 70 4f  rt( v!=0 );.  pO
212c9 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 47  p = sqlite3VdbeG
212ca 65 74 4f 70 28 76 2c 20 2d 31 29 3b 0a 20 20 61  etOp(v, -1);.  a
212cb 73 73 65 72 74 28 20 70 4f 70 21 3d 30 20 29 3b  ssert( pOp!=0 );
212cc 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f  .  if( pOp->opco
212cd 64 65 3d 3d 4f 50 5f 53 43 6f 70 79 20 26 26 20  de==OP_SCopy && 
212ce 70 4f 70 2d 3e 70 31 3e 3d 69 52 65 67 20 26 26  pOp->p1>=iReg &&
212cf 20 70 4f 70 2d 3e 70 31 3c 69 52 65 67 2b 6e 52   pOp->p1<iReg+nR
212d0 65 67 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 6f  eg ){.    pOp->o
212d1 70 63 6f 64 65 20 3d 20 4f 50 5f 43 6f 70 79 3b  pcode = OP_Copy;
212d2 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  .  }.}../*.** Ge
212d3 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 73  nerate code to s
212d4 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65 20 6f  tore the value o
212d5 66 20 74 68 65 20 69 41 6c 69 61 73 2d 74 68 20  f the iAlias-th 
212d6 61 6c 69 61 73 20 69 6e 20 72 65 67 69 73 74 65  alias in registe
212d7 72 0a 2a 2a 20 74 61 72 67 65 74 2e 20 20 54 68  r.** target.  Th
212d8 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 69  e first time thi
212d9 73 20 69 73 20 63 61 6c 6c 65 64 2c 20 70 45 78  s is called, pEx
212da 70 72 20 69 73 20 65 76 61 6c 75 61 74 65 64 20  pr is evaluated 
212db 74 6f 20 63 6f 6d 70 75 74 65 0a 2a 2a 20 74 68  to compute.** th
212dc 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 61  e value of the a
212dd 6c 69 61 73 2e 20 20 54 68 65 20 76 61 6c 75 65  lias.  The value
212de 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 6e   is stored in an
212df 20 61 75 78 69 6c 69 61 72 79 20 72 65 67 69 73   auxiliary regis
212e0 74 65 72 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6e  ter.** and the n
212e1 75 6d 62 65 72 20 6f 66 20 74 68 61 74 20 72 65  umber of that re
212e2 67 69 73 74 65 72 20 69 73 20 72 65 74 75 72 6e  gister is return
212e3 65 64 2e 20 20 4f 6e 20 73 75 62 73 65 71 75 65  ed.  On subseque
212e4 6e 74 20 63 61 6c 6c 73 2c 0a 2a 2a 20 74 68 65  nt calls,.** the
212e5 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72   register number
212e6 20 69 73 20 72 65 74 75 72 6e 65 64 20 77 69 74   is returned wit
212e7 68 6f 75 74 20 67 65 6e 65 72 61 74 69 6e 67 20  hout generating 
212e8 61 6e 79 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20  any code..**.** 
212e9 4e 6f 74 65 20 74 68 61 74 20 69 6e 20 6f 72 64  Note that in ord
212ea 65 72 20 66 6f 72 20 74 68 69 73 20 74 6f 20 77  er for this to w
212eb 6f 72 6b 2c 20 63 6f 64 65 20 6d 75 73 74 20 62  ork, code must b
212ec 65 20 67 65 6e 65 72 61 74 65 64 20 69 6e 20 74  e generated in t
212ed 68 65 0a 2a 2a 20 73 61 6d 65 20 6f 72 64 65 72  he.** same order
212ee 20 74 68 61 74 20 69 74 20 69 73 20 65 78 65 63   that it is exec
212ef 75 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6c 69 61  uted..**.** Alia
212f0 73 65 73 20 61 72 65 20 6e 75 6d 62 65 72 65 64  ses are numbered
212f1 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 31   starting with 1
212f2 2e 20 20 53 6f 20 69 41 6c 69 61 73 20 69 73 20  .  So iAlias is 
212f3 69 6e 20 74 68 65 20 72 61 6e 67 65 0a 2a 2a 20  in the range.** 
212f4 6f 66 20 31 20 74 6f 20 70 50 61 72 73 65 2d 3e  of 1 to pParse->
212f5 6e 41 6c 69 61 73 20 69 6e 63 6c 75 73 69 76 65  nAlias inclusive
212f6 2e 20 20 0a 2a 2a 0a 2a 2a 20 70 50 61 72 73 65  .  .**.** pParse
212f7 2d 3e 61 41 6c 69 61 73 5b 69 41 6c 69 61 73 2d  ->aAlias[iAlias-
212f8 31 5d 20 72 65 63 6f 72 64 73 20 74 68 65 20 72  1] records the r
212f9 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20 77  egister number w
212fa 68 65 72 65 20 74 68 65 20 76 61 6c 75 65 0a 2a  here the value.*
212fb 2a 20 6f 66 20 74 68 65 20 69 41 6c 69 61 73 2d  * of the iAlias-
212fc 74 68 20 61 6c 69 61 73 20 69 73 20 73 74 6f 72  th alias is stor
212fd 65 64 2e 20 20 49 66 20 7a 65 72 6f 2c 20 74 68  ed.  If zero, th
212fe 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  at means that th
212ff 65 0a 2a 2a 20 61 6c 69 61 73 20 68 61 73 20 6e  e.** alias has n
21300 6f 74 20 79 65 74 20 62 65 65 6e 20 63 6f 6d 70  ot yet been comp
21301 75 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  uted..*/.static 
21302 69 6e 74 20 63 6f 64 65 41 6c 69 61 73 28 50 61  int codeAlias(Pa
21303 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74  rse *pParse, int
21304 20 69 41 6c 69 61 73 2c 20 45 78 70 72 20 2a 70   iAlias, Expr *p
21305 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67 65 74  Expr, int target
21306 29 7b 0a 23 69 66 20 30 0a 20 20 73 71 6c 69 74  ){.#if 0.  sqlit
21307 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
21308 3e 64 62 3b 0a 20 20 69 6e 74 20 69 52 65 67 3b  >db;.  int iReg;
21309 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
2130a 41 6c 69 61 73 41 6c 6c 6f 63 3c 70 50 61 72 73  AliasAlloc<pPars
2130b 65 2d 3e 6e 41 6c 69 61 73 20 29 7b 0a 20 20 20  e->nAlias ){.   
2130c 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 20   pParse->aAlias 
2130d 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c  = sqlite3DbReall
2130e 6f 63 4f 72 46 72 65 65 28 64 62 2c 20 70 50 61  ocOrFree(db, pPa
2130f 72 73 65 2d 3e 61 41 6c 69 61 73 2c 0a 20 20 20  rse->aAlias,.   
21310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21311 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 69                si
21312 7a 65 6f 66 28 70 50 61 72 73 65 2d 3e 61 41 6c  zeof(pParse->aAl
21313 69 61 73 5b 30 5d 29 2a 70 50 61 72 73 65 2d 3e  ias[0])*pParse->
21314 6e 41 6c 69 61 73 20 29 3b 0a 20 20 20 20 74 65  nAlias );.    te
21315 73 74 63 61 73 65 28 20 64 62 2d 3e 6d 61 6c 6c  stcase( db->mall
21316 6f 63 46 61 69 6c 65 64 20 26 26 20 70 50 61 72  ocFailed && pPar
21317 73 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 3e  se->nAliasAlloc>
21318 30 20 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d  0 );.    if( db-
21319 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20  >mallocFailed ) 
2131a 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 6d 65  return 0;.    me
2131b 6d 73 65 74 28 26 70 50 61 72 73 65 2d 3e 61 41  mset(&pParse->aA
2131c 6c 69 61 73 5b 70 50 61 72 73 65 2d 3e 6e 41 6c  lias[pParse->nAl
2131d 69 61 73 41 6c 6c 6f 63 5d 2c 20 30 2c 0a 20 20  iasAlloc], 0,.  
2131e 20 20 20 20 20 20 20 20 20 28 70 50 61 72 73 65           (pParse
2131f 2d 3e 6e 41 6c 69 61 73 2d 70 50 61 72 73 65 2d  ->nAlias-pParse-
21320 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 29 2a 73 69  >nAliasAlloc)*si
21321 7a 65 6f 66 28 70 50 61 72 73 65 2d 3e 61 41 6c  zeof(pParse->aAl
21322 69 61 73 5b 30 5d 29 29 3b 0a 20 20 20 20 70 50  ias[0]));.    pP
21323 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f  arse->nAliasAllo
21324 63 20 3d 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69  c = pParse->nAli
21325 61 73 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  as;.  }.  assert
21326 28 20 69 41 6c 69 61 73 3e 30 20 26 26 20 69 41  ( iAlias>0 && iA
21327 6c 69 61 73 3c 3d 70 50 61 72 73 65 2d 3e 6e 41  lias<=pParse->nA
21328 6c 69 61 73 20 29 3b 0a 20 20 69 52 65 67 20 3d  lias );.  iReg =
21329 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b   pParse->aAlias[
2132a 69 41 6c 69 61 73 2d 31 5d 3b 0a 20 20 69 66 28  iAlias-1];.  if(
2132b 20 69 52 65 67 3d 3d 30 20 29 7b 0a 20 20 20 20   iReg==0 ){.    
2132c 69 66 28 20 70 50 61 72 73 65 2d 3e 69 43 61 63  if( pParse->iCac
2132d 68 65 4c 65 76 65 6c 3e 30 20 29 7b 0a 20 20 20  heLevel>0 ){.   
2132e 20 20 20 69 52 65 67 20 3d 20 73 71 6c 69 74 65     iReg = sqlite
2132f 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28  3ExprCodeTarget(
21330 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 74  pParse, pExpr, t
21331 61 72 67 65 74 29 3b 0a 20 20 20 20 7d 65 6c 73  arget);.    }els
21332 65 7b 0a 20 20 20 20 20 20 69 52 65 67 20 3d 20  e{.      iReg = 
21333 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
21334 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
21335 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45  rCode(pParse, pE
21336 78 70 72 2c 20 69 52 65 67 29 3b 0a 20 20 20 20  xpr, iReg);.    
21337 20 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73    pParse->aAlias
21338 5b 69 41 6c 69 61 73 2d 31 5d 20 3d 20 69 52 65  [iAlias-1] = iRe
21339 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  g;.    }.  }.  r
2133a 65 74 75 72 6e 20 69 52 65 67 3b 0a 23 65 6c 73  eturn iReg;.#els
2133b 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  e.  UNUSED_PARAM
2133c 45 54 45 52 28 69 41 6c 69 61 73 29 3b 0a 20 20  ETER(iAlias);.  
2133d 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 45 78  return sqlite3Ex
2133e 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 61  prCodeTarget(pPa
2133f 72 73 65 2c 20 70 45 78 70 72 2c 20 74 61 72 67  rse, pExpr, targ
21340 65 74 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f  et);.#endif.}../
21341 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
21342 64 65 20 69 6e 74 6f 20 74 68 65 20 63 75 72 72  de into the curr
21343 65 6e 74 20 56 64 62 65 20 74 6f 20 65 76 61 6c  ent Vdbe to eval
21344 75 61 74 65 20 74 68 65 20 67 69 76 65 6e 0a 2a  uate the given.*
21345 2a 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 41  * expression.  A
21346 74 74 65 6d 70 74 20 74 6f 20 73 74 6f 72 65 20  ttempt to store 
21347 74 68 65 20 72 65 73 75 6c 74 73 20 69 6e 20 72  the results in r
21348 65 67 69 73 74 65 72 20 22 74 61 72 67 65 74 22  egister "target"
21349 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  ..** Return the 
2134a 72 65 67 69 73 74 65 72 20 77 68 65 72 65 20 72  register where r
2134b 65 73 75 6c 74 73 20 61 72 65 20 73 74 6f 72 65  esults are store
2134c 64 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20 74 68  d..**.** With th
2134d 69 73 20 72 6f 75 74 69 6e 65 2c 20 74 68 65 72  is routine, ther
2134e 65 20 69 73 20 6e 6f 20 67 75 61 72 61 6e 74 65  e is no guarante
2134f 65 20 74 68 61 74 20 72 65 73 75 6c 74 73 20 77  e that results w
21350 69 6c 6c 0a 2a 2a 20 62 65 20 73 74 6f 72 65 64  ill.** be stored
21351 20 69 6e 20 74 61 72 67 65 74 2e 20 20 54 68 65   in target.  The
21352 20 72 65 73 75 6c 74 20 6d 69 67 68 74 20 62 65   result might be
21353 20 73 74 6f 72 65 64 20 69 6e 20 73 6f 6d 65 20   stored in some 
21354 6f 74 68 65 72 0a 2a 2a 20 72 65 67 69 73 74 65  other.** registe
21355 72 20 69 66 20 69 74 20 69 73 20 63 6f 6e 76 65  r if it is conve
21356 6e 69 65 6e 74 20 74 6f 20 64 6f 20 73 6f 2e 20  nient to do so. 
21357 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e   The calling fun
21358 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 63 68  ction.** must ch
21359 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 63  eck the return c
2135a 6f 64 65 20 61 6e 64 20 6d 6f 76 65 20 74 68 65  ode and move the
2135b 20 72 65 73 75 6c 74 73 20 74 6f 20 74 68 65 20   results to the 
2135c 64 65 73 69 72 65 64 0a 2a 2a 20 72 65 67 69 73  desired.** regis
2135d 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ter..*/.SQLITE_P
2135e 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2135f 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74  e3ExprCodeTarget
21360 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
21361 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74  Expr *pExpr, int
21362 20 74 61 72 67 65 74 29 7b 0a 20 20 56 64 62 65   target){.  Vdbe
21363 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
21364 64 62 65 3b 20 20 2f 2a 20 54 68 65 20 56 4d 20  dbe;  /* The VM 
21365 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69  under constructi
21366 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 3b 20  on */.  int op; 
21367 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21368 20 20 2f 2a 20 54 68 65 20 6f 70 63 6f 64 65 20    /* The opcode 
21369 62 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a 20  being coded */. 
2136a 20 69 6e 74 20 69 6e 52 65 67 20 3d 20 74 61 72   int inReg = tar
2136b 67 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 52 65  get;       /* Re
2136c 73 75 6c 74 73 20 73 74 6f 72 65 64 20 69 6e 20  sults stored in 
2136d 72 65 67 69 73 74 65 72 20 69 6e 52 65 67 20 2a  register inReg *
2136e 2f 0a 20 20 69 6e 74 20 72 65 67 46 72 65 65 31  /.  int regFree1
2136f 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a   = 0;         /*
21370 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20 66 72 65   If non-zero fre
21371 65 20 74 68 69 73 20 74 65 6d 70 6f 72 61 72 79  e this temporary
21372 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 69   register */.  i
21373 6e 74 20 72 65 67 46 72 65 65 32 20 3d 20 30 3b  nt regFree2 = 0;
21374 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 6e           /* If n
21375 6f 6e 2d 7a 65 72 6f 20 66 72 65 65 20 74 68 69  on-zero free thi
21376 73 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69  s temporary regi
21377 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 31  ster */.  int r1
21378 2c 20 72 32 2c 20 72 33 2c 20 72 34 3b 20 20 20  , r2, r3, r4;   
21379 20 20 20 20 2f 2a 20 56 61 72 69 6f 75 73 20 72      /* Various r
2137a 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 73 20  egister numbers 
2137b 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
2137c 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 2f   = pParse->db; /
2137d 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  * The database c
2137e 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20  onnection */..  
2137f 61 73 73 65 72 74 28 20 74 61 72 67 65 74 3e 30  assert( target>0
21380 20 26 26 20 74 61 72 67 65 74 3c 3d 70 50 61 72   && target<=pPar
21381 73 65 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 69 66  se->nMem );.  if
21382 28 20 76 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73  ( v==0 ){.    as
21383 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 64 62  sert( pParse->db
21384 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
21385 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  ;.    return 0;.
21386 20 20 7d 0a 0a 20 20 69 66 28 20 70 45 78 70 72    }..  if( pExpr
21387 3d 3d 30 20 29 7b 0a 20 20 20 20 6f 70 20 3d 20  ==0 ){.    op = 
21388 54 4b 5f 4e 55 4c 4c 3b 0a 20 20 7d 65 6c 73 65  TK_NULL;.  }else
21389 7b 0a 20 20 20 20 6f 70 20 3d 20 70 45 78 70 72  {.    op = pExpr
2138a 2d 3e 6f 70 3b 0a 20 20 7d 0a 20 20 73 77 69 74  ->op;.  }.  swit
2138b 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61  ch( op ){.    ca
2138c 73 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e  se TK_AGG_COLUMN
2138d 3a 20 7b 0a 20 20 20 20 20 20 41 67 67 49 6e 66  : {.      AggInf
2138e 6f 20 2a 70 41 67 67 49 6e 66 6f 20 3d 20 70 45  o *pAggInfo = pE
2138f 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f 3b 0a 20  xpr->pAggInfo;. 
21390 20 20 20 20 20 73 74 72 75 63 74 20 41 67 67 49       struct AggI
21391 6e 66 6f 5f 63 6f 6c 20 2a 70 43 6f 6c 20 3d 20  nfo_col *pCol = 
21392 26 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b  &pAggInfo->aCol[
21393 70 45 78 70 72 2d 3e 69 41 67 67 5d 3b 0a 20 20  pExpr->iAgg];.  
21394 20 20 20 20 69 66 28 20 21 70 41 67 67 49 6e 66      if( !pAggInf
21395 6f 2d 3e 64 69 72 65 63 74 4d 6f 64 65 20 29 7b  o->directMode ){
21396 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
21397 20 70 43 6f 6c 2d 3e 69 4d 65 6d 3e 30 20 29 3b   pCol->iMem>0 );
21398 0a 20 20 20 20 20 20 20 20 69 6e 52 65 67 20 3d  .        inReg =
21399 20 70 43 6f 6c 2d 3e 69 4d 65 6d 3b 0a 20 20 20   pCol->iMem;.   
2139a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2139b 20 20 7d 65 6c 73 65 20 69 66 28 20 70 41 67 67    }else if( pAgg
2139c 49 6e 66 6f 2d 3e 75 73 65 53 6f 72 74 69 6e 67  Info->useSorting
2139d 49 64 78 20 29 7b 0a 20 20 20 20 20 20 20 20 73  Idx ){.        s
2139e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2139f 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 70  (v, OP_Column, p
213a0 41 67 67 49 6e 66 6f 2d 3e 73 6f 72 74 69 6e 67  AggInfo->sorting
213a1 49 64 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Idx,.           
213a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
213a3 20 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72     pCol->iSorter
213a4 43 6f 6c 75 6d 6e 2c 20 74 61 72 67 65 74 29 3b  Column, target);
213a5 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
213a6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a        }.      /*
213a7 20 4f 74 68 65 72 77 69 73 65 2c 20 66 61 6c 6c   Otherwise, fall
213a8 20 74 68 72 75 20 69 6e 74 6f 20 74 68 65 20 54   thru into the T
213a9 4b 5f 43 4f 4c 55 4d 4e 20 63 61 73 65 20 2a 2f  K_COLUMN case */
213aa 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
213ab 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20  TK_COLUMN: {.   
213ac 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 54     if( pExpr->iT
213ad 61 62 6c 65 3c 30 20 29 7b 0a 20 20 20 20 20 20  able<0 ){.      
213ae 20 20 2f 2a 20 54 68 69 73 20 6f 6e 6c 79 20 68    /* This only h
213af 61 70 70 65 6e 73 20 77 68 65 6e 20 63 6f 64 69  appens when codi
213b0 6e 67 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 61  ng check constra
213b1 69 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 20 20  ints */.        
213b2 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e  assert( pParse->
213b3 63 6b 42 61 73 65 3e 30 20 29 3b 0a 20 20 20 20  ckBase>0 );.    
213b4 20 20 20 20 69 6e 52 65 67 20 3d 20 70 45 78 70      inReg = pExp
213b5 72 2d 3e 69 43 6f 6c 75 6d 6e 20 2b 20 70 50 61  r->iColumn + pPa
213b6 72 73 65 2d 3e 63 6b 42 61 73 65 3b 0a 20 20 20  rse->ckBase;.   
213b7 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
213b8 20 20 74 65 73 74 63 61 73 65 28 20 28 70 45 78    testcase( (pEx
213b9 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 41  pr->flags & EP_A
213ba 6e 79 41 66 66 29 21 3d 30 20 29 3b 0a 20 20 20  nyAff)!=0 );.   
213bb 20 20 20 20 20 69 6e 52 65 67 20 3d 20 73 71 6c       inReg = sql
213bc 69 74 65 33 45 78 70 72 43 6f 64 65 47 65 74 43  ite3ExprCodeGetC
213bd 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20 70 45  olumn(pParse, pE
213be 78 70 72 2d 3e 70 54 61 62 2c 0a 20 20 20 20 20  xpr->pTab,.     
213bf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
213c0 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70              pExp
213c1 72 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 70 45 78 70  r->iColumn, pExp
213c2 72 2d 3e 69 54 61 62 6c 65 2c 20 74 61 72 67 65  r->iTable, targe
213c3 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t,.             
213c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
213c5 20 20 20 20 70 45 78 70 72 2d 3e 66 6c 61 67 73      pExpr->flags
213c6 20 26 20 45 50 5f 41 6e 79 41 66 66 29 3b 0a 20   & EP_AnyAff);. 
213c7 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
213c8 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
213c9 73 65 20 54 4b 5f 49 4e 54 45 47 45 52 3a 20 7b  se TK_INTEGER: {
213ca 0a 20 20 20 20 20 20 63 6f 64 65 49 6e 74 65 67  .      codeInteg
213cb 65 72 28 76 2c 20 70 45 78 70 72 2c 20 30 2c 20  er(v, pExpr, 0, 
213cc 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 62  target);.      b
213cd 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
213ce 63 61 73 65 20 54 4b 5f 46 4c 4f 41 54 3a 20 7b  case TK_FLOAT: {
213cf 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21  .      assert( !
213d0 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
213d1 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c  pExpr, EP_IntVal
213d2 75 65 29 20 29 3b 0a 20 20 20 20 20 20 63 6f 64  ue) );.      cod
213d3 65 52 65 61 6c 28 76 2c 20 70 45 78 70 72 2d 3e  eReal(v, pExpr->
213d4 75 2e 7a 54 6f 6b 65 6e 2c 20 30 2c 20 74 61 72  u.zToken, 0, tar
213d5 67 65 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61  get);.      brea
213d6 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
213d7 65 20 54 4b 5f 53 54 52 49 4e 47 3a 20 7b 0a 20  e TK_STRING: {. 
213d8 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78       assert( !Ex
213d9 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
213da 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65  xpr, EP_IntValue
213db 29 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ) );.      sqlit
213dc 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
213dd 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 74  OP_String8, 0, t
213de 61 72 67 65 74 2c 20 30 2c 20 70 45 78 70 72 2d  arget, 0, pExpr-
213df 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 30 29 3b 0a 20  >u.zToken, 0);. 
213e0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
213e1 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 55  }.    case TK_NU
213e2 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69  LL: {.      sqli
213e3 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
213e4 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 74 61 72   OP_Null, 0, tar
213e5 67 65 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61  get);.      brea
213e6 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66  k;.    }.#ifndef
213e7 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f   SQLITE_OMIT_BLO
213e8 42 5f 4c 49 54 45 52 41 4c 0a 20 20 20 20 63 61  B_LITERAL.    ca
213e9 73 65 20 54 4b 5f 42 4c 4f 42 3a 20 7b 0a 20 20  se TK_BLOB: {.  
213ea 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 20      int n;.     
213eb 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b 0a   const char *z;.
213ec 20 20 20 20 20 20 63 68 61 72 20 2a 7a 42 6c 6f        char *zBlo
213ed 62 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  b;.      assert(
213ee 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74   !ExprHasPropert
213ef 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56  y(pExpr, EP_IntV
213f0 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20 61  alue) );.      a
213f1 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75 2e  ssert( pExpr->u.
213f2 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d 27 78 27 20 7c  zToken[0]=='x' |
213f3 7c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65  | pExpr->u.zToke
213f4 6e 5b 30 5d 3d 3d 27 58 27 20 29 3b 0a 20 20 20  n[0]=='X' );.   
213f5 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72     assert( pExpr
213f6 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 31 5d 3d 3d 27  ->u.zToken[1]=='
213f7 5c 27 27 20 29 3b 0a 20 20 20 20 20 20 7a 20 3d  \'' );.      z =
213f8 20 26 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65   &pExpr->u.zToke
213f9 6e 5b 32 5d 3b 0a 20 20 20 20 20 20 6e 20 3d 20  n[2];.      n = 
213fa 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
213fb 7a 29 20 2d 20 31 3b 0a 20 20 20 20 20 20 61 73  z) - 1;.      as
213fc 73 65 72 74 28 20 7a 5b 6e 5d 3d 3d 27 5c 27 27  sert( z[n]=='\''
213fd 20 29 3b 0a 20 20 20 20 20 20 7a 42 6c 6f 62 20   );.      zBlob 
213fe 3d 20 73 71 6c 69 74 65 33 48 65 78 54 6f 42 6c  = sqlite3HexToBl
213ff 6f 62 28 73 71 6c 69 74 65 33 56 64 62 65 44 62  ob(sqlite3VdbeDb
21400 28 76 29 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20  (v), z, n);.    
21401 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
21402 4f 70 34 28 76 2c 20 4f 50 5f 42 6c 6f 62 2c 20  Op4(v, OP_Blob, 
21403 6e 2f 32 2c 20 74 61 72 67 65 74 2c 20 30 2c 20  n/2, target, 0, 
21404 7a 42 6c 6f 62 2c 20 50 34 5f 44 59 4e 41 4d 49  zBlob, P4_DYNAMI
21405 43 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  C);.      break;
21406 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
21407 20 20 63 61 73 65 20 54 4b 5f 56 41 52 49 41 42    case TK_VARIAB
21408 4c 45 3a 20 7b 0a 20 20 20 20 20 20 56 64 62 65  LE: {.      Vdbe
21409 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20 20 20 61  Op *pOp;.      a
2140a 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50  ssert( !ExprHasP
2140b 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
2140c 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20  P_IntValue) );. 
2140d 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78       assert( pEx
2140e 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 21 3d 30 20  pr->u.zToken!=0 
2140f 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
21410 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e   pExpr->u.zToken
21411 5b 30 5d 21 3d 30 20 29 3b 0a 20 20 20 20 20 20  [0]!=0 );.      
21412 69 66 28 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f  if( pExpr->u.zTo
21413 6b 65 6e 5b 31 5d 3d 3d 30 0a 20 20 20 20 20 20  ken[1]==0.      
21414 20 20 20 26 26 20 28 70 4f 70 20 3d 20 73 71 6c     && (pOp = sql
21415 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 76 2c  ite3VdbeGetOp(v,
21416 20 2d 31 29 29 2d 3e 6f 70 63 6f 64 65 3d 3d 4f   -1))->opcode==O
21417 50 5f 56 61 72 69 61 62 6c 65 0a 20 20 20 20 20  P_Variable.     
21418 20 20 20 20 26 26 20 70 4f 70 2d 3e 70 31 2b 70      && pOp->p1+p
21419 4f 70 2d 3e 70 33 3d 3d 70 45 78 70 72 2d 3e 69  Op->p3==pExpr->i
2141a 54 61 62 6c 65 0a 20 20 20 20 20 20 20 20 20 26  Table.         &
2141b 26 20 70 4f 70 2d 3e 70 32 2b 70 4f 70 2d 3e 70  & pOp->p2+pOp->p
2141c 33 3d 3d 74 61 72 67 65 74 0a 20 20 20 20 20 20  3==target.      
2141d 20 20 20 26 26 20 70 4f 70 2d 3e 70 34 2e 7a 3d     && pOp->p4.z=
2141e 3d 30 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20  =0.      ){.    
2141f 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 70 72      /* If the pr
21420 65 76 69 6f 75 73 20 69 6e 73 74 72 75 63 74 69  evious instructi
21421 6f 6e 20 77 61 73 20 61 20 63 6f 70 79 20 6f 66  on was a copy of
21422 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 75 6e   the previous un
21423 6e 61 6d 65 64 0a 20 20 20 20 20 20 20 20 2a 2a  named.        **
21424 20 70 61 72 61 6d 65 74 65 72 20 69 6e 74 6f 20   parameter into 
21425 74 68 65 20 70 72 65 76 69 6f 75 73 20 72 65 67  the previous reg
21426 69 73 74 65 72 2c 20 74 68 65 6e 20 73 69 6d 70  ister, then simp
21427 6c 79 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65  ly increment the
21428 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 65 70 65  .        ** repe
21429 61 74 20 63 6f 75 6e 74 20 6f 6e 20 74 68 65 20  at count on the 
2142a 70 72 69 6f 72 20 69 6e 73 74 72 75 63 74 69 6f  prior instructio
2142b 6e 20 72 61 74 68 65 72 20 74 68 61 6e 20 6d 61  n rather than ma
2142c 6b 69 6e 67 20 61 20 6e 65 77 0a 20 20 20 20 20  king a new.     
2142d 20 20 20 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f     ** instructio
2142e 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  n..        */.  
2142f 20 20 20 20 20 20 70 4f 70 2d 3e 70 33 2b 2b 3b        pOp->p3++;
21430 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
21431 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
21432 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 56 61  eAddOp3(v, OP_Va
21433 72 69 61 62 6c 65 2c 20 70 45 78 70 72 2d 3e 69  riable, pExpr->i
21434 54 61 62 6c 65 2c 20 74 61 72 67 65 74 2c 20 31  Table, target, 1
21435 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
21436 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 31  Expr->u.zToken[1
21437 5d 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  ]!=0 ){.        
21438 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
21439 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 70 45 78  ngeP4(v, -1, pEx
2143a 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 30 29  pr->u.zToken, 0)
2143b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2143c 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b    }.      break;
2143d 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
2143e 54 4b 5f 52 45 47 49 53 54 45 52 3a 20 7b 0a 20  TK_REGISTER: {. 
2143f 20 20 20 20 20 69 6e 52 65 67 20 3d 20 70 45 78       inReg = pEx
21440 70 72 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20  pr->iTable;.    
21441 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
21442 20 20 20 63 61 73 65 20 54 4b 5f 41 53 3a 20 7b     case TK_AS: {
21443 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 63  .      inReg = c
21444 6f 64 65 41 6c 69 61 73 28 70 50 61 72 73 65 2c  odeAlias(pParse,
21445 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20   pExpr->iTable, 
21446 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61  pExpr->pLeft, ta
21447 72 67 65 74 29 3b 0a 20 20 20 20 20 20 62 72 65  rget);.      bre
21448 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65  ak;.    }.#ifnde
21449 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41  f SQLITE_OMIT_CA
2144a 53 54 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43  ST.    case TK_C
2144b 41 53 54 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20  AST: {.      /* 
2144c 45 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 74  Expressions of t
2144d 68 65 20 66 6f 72 6d 3a 20 20 20 43 41 53 54 28  he form:   CAST(
2144e 70 4c 65 66 74 20 41 53 20 74 6f 6b 65 6e 29 20  pLeft AS token) 
2144f 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 66 66  */.      int aff
21450 2c 20 74 6f 5f 6f 70 3b 0a 20 20 20 20 20 20 69  , to_op;.      i
21451 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78  nReg = sqlite3Ex
21452 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 61  prCodeTarget(pPa
21453 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
21454 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  t, target);.    
21455 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48    assert( !ExprH
21456 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
21457 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29  , EP_IntValue) )
21458 3b 0a 20 20 20 20 20 20 61 66 66 20 3d 20 73 71  ;.      aff = sq
21459 6c 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 70  lite3AffinityTyp
2145a 65 28 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65  e(pExpr->u.zToke
2145b 6e 29 3b 0a 20 20 20 20 20 20 74 6f 5f 6f 70 20  n);.      to_op 
2145c 3d 20 61 66 66 20 2d 20 53 51 4c 49 54 45 5f 41  = aff - SQLITE_A
2145d 46 46 5f 54 45 58 54 20 2b 20 4f 50 5f 54 6f 54  FF_TEXT + OP_ToT
2145e 65 78 74 3b 0a 20 20 20 20 20 20 61 73 73 65 72  ext;.      asser
2145f 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 54  t( to_op==OP_ToT
21460 65 78 74 20 20 20 20 7c 7c 20 61 66 66 21 3d 53  ext    || aff!=S
21461 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20 20  QLITE_AFF_TEXT  
21462 20 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72    );.      asser
21463 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 42  t( to_op==OP_ToB
21464 6c 6f 62 20 20 20 20 7c 7c 20 61 66 66 21 3d 53  lob    || aff!=S
21465 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 20  QLITE_AFF_NONE  
21466 20 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72    );.      asser
21467 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 4e  t( to_op==OP_ToN
21468 75 6d 65 72 69 63 20 7c 7c 20 61 66 66 21 3d 53  umeric || aff!=S
21469 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49  QLITE_AFF_NUMERI
2146a 43 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  C );.      asser
2146b 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 49  t( to_op==OP_ToI
2146c 6e 74 20 20 20 20 20 7c 7c 20 61 66 66 21 3d 53  nt     || aff!=S
2146d 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45  QLITE_AFF_INTEGE
2146e 52 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  R );.      asser
2146f 74 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 52  t( to_op==OP_ToR
21470 65 61 6c 20 20 20 20 7c 7c 20 61 66 66 21 3d 53  eal    || aff!=S
21471 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 20 20  QLITE_AFF_REAL  
21472 20 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63    );.      testc
21473 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54  ase( to_op==OP_T
21474 6f 54 65 78 74 20 29 3b 0a 20 20 20 20 20 20 74  oText );.      t
21475 65 73 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d  estcase( to_op==
21476 4f 50 5f 54 6f 42 6c 6f 62 20 29 3b 0a 20 20 20  OP_ToBlob );.   
21477 20 20 20 74 65 73 74 63 61 73 65 28 20 74 6f 5f     testcase( to_
21478 6f 70 3d 3d 4f 50 5f 54 6f 4e 75 6d 65 72 69 63  op==OP_ToNumeric
21479 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
2147a 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f  se( to_op==OP_To
2147b 49 6e 74 20 29 3b 0a 20 20 20 20 20 20 74 65 73  Int );.      tes
2147c 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50  tcase( to_op==OP
2147d 5f 54 6f 52 65 61 6c 20 29 3b 0a 20 20 20 20 20  _ToReal );.     
2147e 20 69 66 28 20 69 6e 52 65 67 21 3d 74 61 72 67   if( inReg!=targ
2147f 65 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  et ){.        sq
21480 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
21481 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 69 6e 52  v, OP_SCopy, inR
21482 65 67 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20  eg, target);.   
21483 20 20 20 20 20 69 6e 52 65 67 20 3d 20 74 61 72       inReg = tar
21484 67 65 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  get;.      }.   
21485 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
21486 64 4f 70 31 28 76 2c 20 74 6f 5f 6f 70 2c 20 69  dOp1(v, to_op, i
21487 6e 52 65 67 29 3b 0a 20 20 20 20 20 20 74 65 73  nReg);.      tes
21488 74 63 61 73 65 28 20 75 73 65 64 41 73 43 6f 6c  tcase( usedAsCol
21489 75 6d 6e 43 61 63 68 65 28 70 50 61 72 73 65 2c  umnCache(pParse,
2148a 20 69 6e 52 65 67 2c 20 69 6e 52 65 67 29 20 29   inReg, inReg) )
2148b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
2148c 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79  xprCacheAffinity
2148d 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 69  Change(pParse, i
2148e 6e 52 65 67 2c 20 31 29 3b 0a 20 20 20 20 20 20  nReg, 1);.      
2148f 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e  break;.    }.#en
21490 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
21491 49 54 5f 43 41 53 54 20 2a 2f 0a 20 20 20 20 63  IT_CAST */.    c
21492 61 73 65 20 54 4b 5f 4c 54 3a 0a 20 20 20 20 63  ase TK_LT:.    c
21493 61 73 65 20 54 4b 5f 4c 45 3a 0a 20 20 20 20 63  ase TK_LE:.    c
21494 61 73 65 20 54 4b 5f 47 54 3a 0a 20 20 20 20 63  ase TK_GT:.    c
21495 61 73 65 20 54 4b 5f 47 45 3a 0a 20 20 20 20 63  ase TK_GE:.    c
21496 61 73 65 20 54 4b 5f 4e 45 3a 0a 20 20 20 20 63  ase TK_NE:.    c
21497 61 73 65 20 54 4b 5f 45 51 3a 20 7b 0a 20 20 20  ase TK_EQ: {.   
21498 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 54     assert( TK_LT
21499 3d 3d 4f 50 5f 4c 74 20 29 3b 0a 20 20 20 20 20  ==OP_Lt );.     
2149a 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 45 3d 3d   assert( TK_LE==
2149b 4f 50 5f 4c 65 20 29 3b 0a 20 20 20 20 20 20 61  OP_Le );.      a
2149c 73 73 65 72 74 28 20 54 4b 5f 47 54 3d 3d 4f 50  ssert( TK_GT==OP
2149d 5f 47 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73  _Gt );.      ass
2149e 65 72 74 28 20 54 4b 5f 47 45 3d 3d 4f 50 5f 47  ert( TK_GE==OP_G
2149f 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  e );.      asser
214a0 74 28 20 54 4b 5f 45 51 3d 3d 4f 50 5f 45 71 20  t( TK_EQ==OP_Eq 
214a1 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
214a2 20 54 4b 5f 4e 45 3d 3d 4f 50 5f 4e 65 20 29 3b   TK_NE==OP_Ne );
214a3 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
214a4 20 6f 70 3d 3d 54 4b 5f 4c 54 20 29 3b 0a 20 20   op==TK_LT );.  
214a5 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
214a6 3d 3d 54 4b 5f 4c 45 20 29 3b 0a 20 20 20 20 20  ==TK_LE );.     
214a7 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
214a8 4b 5f 47 54 20 29 3b 0a 20 20 20 20 20 20 74 65  K_GT );.      te
214a9 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47  stcase( op==TK_G
214aa 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  E );.      testc
214ab 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 45 51 20 29  ase( op==TK_EQ )
214ac 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
214ad 28 20 6f 70 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20  ( op==TK_NE );. 
214ae 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65       codeCompare
214af 4f 70 65 72 61 6e 64 73 28 70 50 61 72 73 65 2c  Operands(pParse,
214b0 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26   pExpr->pLeft, &
214b1 72 31 2c 20 26 72 65 67 46 72 65 65 31 2c 0a 20  r1, &regFree1,. 
214b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
214b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
214b4 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20   pExpr->pRight, 
214b5 26 72 32 2c 20 26 72 65 67 46 72 65 65 32 29 3b  &r2, &regFree2);
214b6 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61  .      codeCompa
214b7 72 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  re(pParse, pExpr
214b8 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e  ->pLeft, pExpr->
214b9 70 52 69 67 68 74 2c 20 6f 70 2c 0a 20 20 20 20  pRight, op,.    
214ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 31                r1
214bb 2c 20 72 32 2c 20 69 6e 52 65 67 2c 20 53 51 4c  , r2, inReg, SQL
214bc 49 54 45 5f 53 54 4f 52 45 50 32 29 3b 0a 20 20  ITE_STOREP2);.  
214bd 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65      testcase( re
214be 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20  gFree1==0 );.   
214bf 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67     testcase( reg
214c0 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20  Free2==0 );.    
214c1 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
214c2 20 20 20 63 61 73 65 20 54 4b 5f 41 4e 44 3a 0a     case TK_AND:.
214c3 20 20 20 20 63 61 73 65 20 54 4b 5f 4f 52 3a 0a      case TK_OR:.
214c4 20 20 20 20 63 61 73 65 20 54 4b 5f 50 4c 55 53      case TK_PLUS
214c5 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 54  :.    case TK_ST
214c6 41 52 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  AR:.    case TK_
214c7 4d 49 4e 55 53 3a 0a 20 20 20 20 63 61 73 65 20  MINUS:.    case 
214c8 54 4b 5f 52 45 4d 3a 0a 20 20 20 20 63 61 73 65  TK_REM:.    case
214c9 20 54 4b 5f 42 49 54 41 4e 44 3a 0a 20 20 20 20   TK_BITAND:.    
214ca 63 61 73 65 20 54 4b 5f 42 49 54 4f 52 3a 0a 20  case TK_BITOR:. 
214cb 20 20 20 63 61 73 65 20 54 4b 5f 53 4c 41 53 48     case TK_SLASH
214cc 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c 53  :.    case TK_LS
214cd 48 49 46 54 3a 0a 20 20 20 20 63 61 73 65 20 54  HIFT:.    case T
214ce 4b 5f 52 53 48 49 46 54 3a 20 0a 20 20 20 20 63  K_RSHIFT: .    c
214cf 61 73 65 20 54 4b 5f 43 4f 4e 43 41 54 3a 20 7b  ase TK_CONCAT: {
214d0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
214d1 4b 5f 41 4e 44 3d 3d 4f 50 5f 41 6e 64 20 29 3b  K_AND==OP_And );
214d2 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
214d3 4b 5f 4f 52 3d 3d 4f 50 5f 4f 72 20 29 3b 0a 20  K_OR==OP_Or );. 
214d4 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
214d5 50 4c 55 53 3d 3d 4f 50 5f 41 64 64 20 29 3b 0a  PLUS==OP_Add );.
214d6 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
214d7 5f 4d 49 4e 55 53 3d 3d 4f 50 5f 53 75 62 74 72  _MINUS==OP_Subtr
214d8 61 63 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73  act );.      ass
214d9 65 72 74 28 20 54 4b 5f 52 45 4d 3d 3d 4f 50 5f  ert( TK_REM==OP_
214da 52 65 6d 61 69 6e 64 65 72 20 29 3b 0a 20 20 20  Remainder );.   
214db 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 42 49     assert( TK_BI
214dc 54 41 4e 44 3d 3d 4f 50 5f 42 69 74 41 6e 64 20  TAND==OP_BitAnd 
214dd 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
214de 20 54 4b 5f 42 49 54 4f 52 3d 3d 4f 50 5f 42 69   TK_BITOR==OP_Bi
214df 74 4f 72 20 29 3b 0a 20 20 20 20 20 20 61 73 73  tOr );.      ass
214e0 65 72 74 28 20 54 4b 5f 53 4c 41 53 48 3d 3d 4f  ert( TK_SLASH==O
214e1 50 5f 44 69 76 69 64 65 20 29 3b 0a 20 20 20 20  P_Divide );.    
214e2 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 53 48    assert( TK_LSH
214e3 49 46 54 3d 3d 4f 50 5f 53 68 69 66 74 4c 65 66  IFT==OP_ShiftLef
214e4 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  t );.      asser
214e5 74 28 20 54 4b 5f 52 53 48 49 46 54 3d 3d 4f 50  t( TK_RSHIFT==OP
214e6 5f 53 68 69 66 74 52 69 67 68 74 20 29 3b 0a 20  _ShiftRight );. 
214e7 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
214e8 43 4f 4e 43 41 54 3d 3d 4f 50 5f 43 6f 6e 63 61  CONCAT==OP_Conca
214e9 74 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  t );.      testc
214ea 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 41 4e 44 20  ase( op==TK_AND 
214eb 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
214ec 65 28 20 6f 70 3d 3d 54 4b 5f 4f 52 20 29 3b 0a  e( op==TK_OR );.
214ed 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
214ee 6f 70 3d 3d 54 4b 5f 50 4c 55 53 20 29 3b 0a 20  op==TK_PLUS );. 
214ef 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
214f0 70 3d 3d 54 4b 5f 4d 49 4e 55 53 20 29 3b 0a 20  p==TK_MINUS );. 
214f1 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
214f2 70 3d 3d 54 4b 5f 52 45 4d 20 29 3b 0a 20 20 20  p==TK_REM );.   
214f3 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
214f4 3d 54 4b 5f 42 49 54 41 4e 44 20 29 3b 0a 20 20  =TK_BITAND );.  
214f5 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
214f6 3d 3d 54 4b 5f 42 49 54 4f 52 20 29 3b 0a 20 20  ==TK_BITOR );.  
214f7 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
214f8 3d 3d 54 4b 5f 53 4c 41 53 48 20 29 3b 0a 20 20  ==TK_SLASH );.  
214f9 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
214fa 3d 3d 54 4b 5f 4c 53 48 49 46 54 20 29 3b 0a 20  ==TK_LSHIFT );. 
214fb 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
214fc 70 3d 3d 54 4b 5f 52 53 48 49 46 54 20 29 3b 0a  p==TK_RSHIFT );.
214fd 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
214fe 6f 70 3d 3d 54 4b 5f 43 4f 4e 43 41 54 20 29 3b  op==TK_CONCAT );
214ff 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69  .      r1 = sqli
21500 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28  te3ExprCodeTemp(
21501 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
21502 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65 31 29  Left, &regFree1)
21503 3b 0a 20 20 20 20 20 20 72 32 20 3d 20 73 71 6c  ;.      r2 = sql
21504 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70  ite3ExprCodeTemp
21505 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
21506 70 52 69 67 68 74 2c 20 26 72 65 67 46 72 65 65  pRight, &regFree
21507 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  2);.      sqlite
21508 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 6f  3VdbeAddOp3(v, o
21509 70 2c 20 72 32 2c 20 72 31 2c 20 74 61 72 67 65  p, r2, r1, targe
2150a 74 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61  t);.      testca
2150b 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20  se( regFree1==0 
2150c 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
2150d 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29  e( regFree2==0 )
2150e 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
2150f 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b     }.    case TK
21510 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20 20 20 20 20  _UMINUS: {.     
21511 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 70   Expr *pLeft = p
21512 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20  Expr->pLeft;.   
21513 20 20 20 61 73 73 65 72 74 28 20 70 4c 65 66 74     assert( pLeft
21514 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4c   );.      if( pL
21515 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41  eft->op==TK_FLOA
21516 54 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  T ){.        ass
21517 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f  ert( !ExprHasPro
21518 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
21519 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20  IntValue) );.   
2151a 20 20 20 20 20 63 6f 64 65 52 65 61 6c 28 76 2c       codeReal(v,
2151b 20 70 4c 65 66 74 2d 3e 75 2e 7a 54 6f 6b 65 6e   pLeft->u.zToken
2151c 2c 20 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20  , 1, target);.  
2151d 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4c      }else if( pL
2151e 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 45  eft->op==TK_INTE
2151f 47 45 52 20 29 7b 0a 20 20 20 20 20 20 20 20 63  GER ){.        c
21520 6f 64 65 49 6e 74 65 67 65 72 28 76 2c 20 70 4c  odeInteger(v, pL
21521 65 66 74 2c 20 31 2c 20 74 61 72 67 65 74 29 3b  eft, 1, target);
21522 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
21523 20 20 20 20 20 20 72 65 67 46 72 65 65 31 20 3d        regFree1 =
21524 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74   r1 = sqlite3Get
21525 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
21526 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
21527 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
21528 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 31 29  _Integer, 0, r1)
21529 3b 0a 20 20 20 20 20 20 20 20 72 32 20 3d 20 73  ;.        r2 = s
2152a 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65  qlite3ExprCodeTe
2152b 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  mp(pParse, pExpr
2152c 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72 65  ->pLeft, &regFre
2152d 65 32 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  e2);.        sql
2152e 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2152f 2c 20 4f 50 5f 53 75 62 74 72 61 63 74 2c 20 72  , OP_Subtract, r
21530 32 2c 20 72 31 2c 20 74 61 72 67 65 74 29 3b 0a  2, r1, target);.
21531 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
21532 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b  ( regFree2==0 );
21533 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
21534 6e 52 65 67 20 3d 20 74 61 72 67 65 74 3b 0a 20  nReg = target;. 
21535 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
21536 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 49  }.    case TK_BI
21537 54 4e 4f 54 3a 0a 20 20 20 20 63 61 73 65 20 54  TNOT:.    case T
21538 4b 5f 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 20 61  K_NOT: {.      a
21539 73 73 65 72 74 28 20 54 4b 5f 42 49 54 4e 4f 54  ssert( TK_BITNOT
2153a 3d 3d 4f 50 5f 42 69 74 4e 6f 74 20 29 3b 0a 20  ==OP_BitNot );. 
2153b 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
2153c 4e 4f 54 3d 3d 4f 50 5f 4e 6f 74 20 29 3b 0a 20  NOT==OP_Not );. 
2153d 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
2153e 70 3d 3d 54 4b 5f 42 49 54 4e 4f 54 20 29 3b 0a  p==TK_BITNOT );.
2153f 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
21540 6f 70 3d 3d 54 4b 5f 4e 4f 54 20 29 3b 0a 20 20  op==TK_NOT );.  
21541 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33      r1 = sqlite3
21542 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61  ExprCodeTemp(pPa
21543 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
21544 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20  t, &regFree1);. 
21545 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72       testcase( r
21546 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20  egFree1==0 );.  
21547 20 20 20 20 69 6e 52 65 67 20 3d 20 74 61 72 67      inReg = targ
21548 65 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  et;.      sqlite
21549 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 6f  3VdbeAddOp2(v, o
2154a 70 2c 20 72 31 2c 20 69 6e 52 65 67 29 3b 0a 20  p, r1, inReg);. 
2154b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2154c 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 53  }.    case TK_IS
2154d 4e 55 4c 4c 3a 0a 20 20 20 20 63 61 73 65 20 54  NULL:.    case T
2154e 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 7b 0a 20 20 20  K_NOTNULL: {.   
2154f 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 20     int addr;.   
21550 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 49 53     assert( TK_IS
21551 4e 55 4c 4c 3d 3d 4f 50 5f 49 73 4e 75 6c 6c 20  NULL==OP_IsNull 
21552 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
21553 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3d 3d 4f 50 5f   TK_NOTNULL==OP_
21554 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 20  NotNull );.     
21555 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
21556 4b 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20  K_ISNULL );.    
21557 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
21558 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 29 3b 0a 20 20  TK_NOTNULL );.  
21559 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2155a 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
2155b 67 65 72 2c 20 31 2c 20 74 61 72 67 65 74 29 3b  ger, 1, target);
2155c 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69  .      r1 = sqli
2155d 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28  te3ExprCodeTemp(
2155e 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
2155f 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65 31 29  Left, &regFree1)
21560 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
21561 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b  ( regFree1==0 );
21562 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71  .      addr = sq
21563 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
21564 76 2c 20 6f 70 2c 20 72 31 29 3b 0a 20 20 20 20  v, op, r1);.    
21565 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
21566 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d  Op2(v, OP_AddImm
21567 2c 20 74 61 72 67 65 74 2c 20 2d 31 29 3b 0a 20  , target, -1);. 
21568 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
21569 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
2156a 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
2156b 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
2156c 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 20  K_AGG_FUNCTION: 
2156d 7b 0a 20 20 20 20 20 20 41 67 67 49 6e 66 6f 20  {.      AggInfo 
2156e 2a 70 49 6e 66 6f 20 3d 20 70 45 78 70 72 2d 3e  *pInfo = pExpr->
2156f 70 41 67 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20  pAggInfo;.      
21570 69 66 28 20 70 49 6e 66 6f 3d 3d 30 20 29 7b 0a  if( pInfo==0 ){.
21571 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
21572 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79  !ExprHasProperty
21573 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61  (pExpr, EP_IntVa
21574 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20 20 20  lue) );.        
21575 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
21576 70 50 61 72 73 65 2c 20 22 6d 69 73 75 73 65 20  pParse, "misuse 
21577 6f 66 20 61 67 67 72 65 67 61 74 65 3a 20 25 73  of aggregate: %s
21578 28 29 22 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54  ()", pExpr->u.zT
21579 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 7d 65 6c  oken);.      }el
2157a 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 52 65  se{.        inRe
2157b 67 20 3d 20 70 49 6e 66 6f 2d 3e 61 46 75 6e 63  g = pInfo->aFunc
2157c 5b 70 45 78 70 72 2d 3e 69 41 67 67 5d 2e 69 4d  [pExpr->iAgg].iM
2157d 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  em;.      }.    
2157e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
2157f 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e 53 54     case TK_CONST
21580 5f 46 55 4e 43 3a 0a 20 20 20 20 63 61 73 65 20  _FUNC:.    case 
21581 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a 20  TK_FUNCTION: {. 
21582 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70       ExprList *p
21583 46 61 72 67 3b 20 20 20 20 20 20 20 2f 2a 20 4c  Farg;       /* L
21584 69 73 74 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20  ist of function 
21585 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20  arguments */.   
21586 20 20 20 69 6e 74 20 6e 46 61 72 67 3b 20 20 20     int nFarg;   
21587 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
21588 62 65 72 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20  ber of function 
21589 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20  arguments */.   
2158a 20 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66     FuncDef *pDef
2158b 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ;         /* The
2158c 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69   function defini
2158d 74 69 6f 6e 20 6f 62 6a 65 63 74 20 2a 2f 0a 20  tion object */. 
2158e 20 20 20 20 20 69 6e 74 20 6e 49 64 3b 20 20 20       int nId;   
2158f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
21590 65 6e 67 74 68 20 6f 66 20 74 68 65 20 66 75 6e  ength of the fun
21591 63 74 69 6f 6e 20 6e 61 6d 65 20 69 6e 20 62 79  ction name in by
21592 74 65 73 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e  tes */.      con
21593 73 74 20 63 68 61 72 20 2a 7a 49 64 3b 20 20 20  st char *zId;   
21594 20 20 20 20 2f 2a 20 54 68 65 20 66 75 6e 63 74      /* The funct
21595 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20  ion name */.    
21596 20 20 69 6e 74 20 63 6f 6e 73 74 4d 61 73 6b 20    int constMask 
21597 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4d 61 73 6b  = 0;     /* Mask
21598 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 61 72 67   of function arg
21599 75 6d 65 6e 74 73 20 74 68 61 74 20 61 72 65 20  uments that are 
2159a 63 6f 6e 73 74 61 6e 74 20 2a 2f 0a 20 20 20 20  constant */.    
2159b 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
2159c 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
2159d 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20   counter */.    
2159e 20 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 64    u8 enc = ENC(d
2159f 62 29 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20  b);      /* The 
215a0 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 75 73  text encoding us
215a1 65 64 20 62 79 20 74 68 69 73 20 64 61 74 61 62  ed by this datab
215a2 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 43 6f 6c  ase */.      Col
215a3 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 30 3b  lSeq *pColl = 0;
215a4 20 20 20 20 2f 2a 20 41 20 63 6f 6c 6c 61 74 69      /* A collati
215a5 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 0a  ng sequence */..
215a6 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45        assert( !E
215a7 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
215a8 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65  Expr, EP_xIsSele
215a9 63 74 29 20 29 3b 0a 20 20 20 20 20 20 74 65 73  ct) );.      tes
215aa 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 43 4f  tcase( op==TK_CO
215ab 4e 53 54 5f 46 55 4e 43 20 29 3b 0a 20 20 20 20  NST_FUNC );.    
215ac 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
215ad 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 29 3b 0a 20  TK_FUNCTION );. 
215ae 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73       if( ExprHas
215af 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70  AnyProperty(pExp
215b0 72 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29  r, EP_TokenOnly)
215b1 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 61 72   ){.        pFar
215b2 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c  g = 0;.      }el
215b3 73 65 7b 0a 20 20 20 20 20 20 20 20 70 46 61 72  se{.        pFar
215b4 67 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69  g = pExpr->x.pLi
215b5 73 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  st;.      }.    
215b6 20 20 6e 46 61 72 67 20 3d 20 70 46 61 72 67 20    nFarg = pFarg 
215b7 3f 20 70 46 61 72 67 2d 3e 6e 45 78 70 72 20 3a  ? pFarg->nExpr :
215b8 20 30 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   0;.      assert
215b9 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72  ( !ExprHasProper
215ba 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74  ty(pExpr, EP_Int
215bb 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20  Value) );.      
215bc 7a 49 64 20 3d 20 70 45 78 70 72 2d 3e 75 2e 7a  zId = pExpr->u.z
215bd 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 6e 49 64  Token;.      nId
215be 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
215bf 33 30 28 7a 49 64 29 3b 0a 20 20 20 20 20 20 70  30(zId);.      p
215c0 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  Def = sqlite3Fin
215c1 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 49  dFunction(db, zI
215c2 64 2c 20 6e 49 64 2c 20 6e 46 61 72 67 2c 20 65  d, nId, nFarg, e
215c3 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66  nc, 0);.      if
215c4 28 20 70 44 65 66 3d 3d 30 20 29 7b 0a 20 20 20  ( pDef==0 ){.   
215c5 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
215c6 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 75 6e  rMsg(pParse, "un
215c7 6b 6e 6f 77 6e 20 66 75 6e 63 74 69 6f 6e 3a 20  known function: 
215c8 25 2e 2a 73 28 29 22 2c 20 6e 49 64 2c 20 7a 49  %.*s()", nId, zI
215c9 64 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  d);.        brea
215ca 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
215cb 20 69 66 28 20 70 46 61 72 67 20 29 7b 0a 20 20   if( pFarg ){.  
215cc 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74        r1 = sqlit
215cd 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70  e3GetTempRange(p
215ce 50 61 72 73 65 2c 20 6e 46 61 72 67 29 3b 0a 20  Parse, nFarg);. 
215cf 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
215d0 70 72 43 61 63 68 65 50 75 73 68 28 70 50 61 72  prCachePush(pPar
215d1 73 65 29 3b 20 20 20 20 20 2f 2a 20 54 69 63 6b  se);     /* Tick
215d2 65 74 20 32 65 61 32 34 32 35 64 33 34 62 65 20  et 2ea2425d34be 
215d3 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  */.        sqlit
215d4 65 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69  e3ExprCodeExprLi
215d5 73 74 28 70 50 61 72 73 65 2c 20 70 46 61 72 67  st(pParse, pFarg
215d6 2c 20 72 31 2c 20 31 29 3b 0a 20 20 20 20 20 20  , r1, 1);.      
215d7 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
215d8 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29  hePop(pParse, 1)
215d9 3b 20 20 20 2f 2a 20 54 69 63 6b 65 74 20 32 65  ;   /* Ticket 2e
215da 61 32 34 32 35 64 33 34 62 65 20 2a 2f 0a 20 20  a2425d34be */.  
215db 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
215dc 20 20 20 72 31 20 3d 20 30 3b 0a 20 20 20 20 20     r1 = 0;.     
215dd 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
215de 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
215df 42 4c 45 0a 20 20 20 20 20 20 2f 2a 20 50 6f 73  BLE.      /* Pos
215e0 73 69 62 6c 79 20 6f 76 65 72 6c 6f 61 64 20 74  sibly overload t
215e1 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 74  he function if t
215e2 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
215e3 74 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 61 20  t is.      ** a 
215e4 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63 6f  virtual table co
215e5 6c 75 6d 6e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20  lumn..      **. 
215e6 20 20 20 20 20 2a 2a 20 46 6f 72 20 69 6e 66 69       ** For infi
215e7 78 20 66 75 6e 63 74 69 6f 6e 73 20 28 4c 49 4b  x functions (LIK
215e8 45 2c 20 47 4c 4f 42 2c 20 52 45 47 45 58 50 2c  E, GLOB, REGEXP,
215e9 20 61 6e 64 20 4d 41 54 43 48 29 20 75 73 65 20   and MATCH) use 
215ea 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 73 65 63  the.      ** sec
215eb 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 6e 6f  ond argument, no
215ec 74 20 74 68 65 20 66 69 72 73 74 2c 20 61 73 20  t the first, as 
215ed 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  the argument to 
215ee 74 65 73 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a  test to.      **
215ef 20 73 65 65 20 69 66 20 69 74 20 69 73 20 61 20   see if it is a 
215f0 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 76 69 72 74  column in a virt
215f1 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 69 73  ual table.  This
215f2 20 69 73 20 64 6f 6e 65 20 62 65 63 61 75 73 65   is done because
215f3 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6c 65  .      ** the le
215f4 66 74 20 6f 70 65 72 61 6e 64 20 6f 66 20 69 6e  ft operand of in
215f5 66 69 78 20 66 75 6e 63 74 69 6f 6e 73 20 28 74  fix functions (t
215f6 68 65 20 6f 70 65 72 61 6e 64 20 77 65 20 77 61  he operand we wa
215f7 6e 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 63  nt to.      ** c
215f8 6f 6e 74 72 6f 6c 20 6f 76 65 72 6c 6f 61 64 69  ontrol overloadi
215f9 6e 67 29 20 65 6e 64 73 20 75 70 20 61 73 20 74  ng) ends up as t
215fa 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
215fb 6e 74 20 74 6f 20 74 68 65 0a 20 20 20 20 20 20  nt to the.      
215fc 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68  ** function.  Th
215fd 65 20 65 78 70 72 65 73 73 69 6f 6e 20 22 41 20  e expression "A 
215fe 67 6c 6f 62 20 42 22 20 69 73 20 65 71 75 69 76  glob B" is equiv
215ff 61 6c 65 6e 74 20 74 6f 20 0a 20 20 20 20 20 20  alent to .      
21600 2a 2a 20 22 67 6c 6f 62 28 42 2c 41 29 2e 20 20  ** "glob(B,A).  
21601 57 65 20 77 61 6e 74 20 74 6f 20 75 73 65 20 74  We want to use t
21602 68 65 20 41 20 69 6e 20 22 41 20 67 6c 6f 62 20  he A in "A glob 
21603 42 22 20 74 6f 20 74 65 73 74 0a 20 20 20 20 20  B" to test.     
21604 20 2a 2a 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e   ** for function
21605 20 6f 76 65 72 6c 6f 61 64 69 6e 67 2e 20 20 42   overloading.  B
21606 75 74 20 77 65 20 75 73 65 20 74 68 65 20 42 20  ut we use the B 
21607 74 65 72 6d 20 69 6e 20 22 67 6c 6f 62 28 42 2c  term in "glob(B,
21608 41 29 22 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  A)"..      */.  
21609 20 20 20 20 69 66 28 20 6e 46 61 72 67 3e 3d 32      if( nFarg>=2
2160a 20 26 26 20 28 70 45 78 70 72 2d 3e 66 6c 61 67   && (pExpr->flag
2160b 73 20 26 20 45 50 5f 49 6e 66 69 78 46 75 6e 63  s & EP_InfixFunc
2160c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44 65  ) ){.        pDe
2160d 66 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62 4f  f = sqlite3VtabO
2160e 76 65 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e 28  verloadFunction(
2160f 64 62 2c 20 70 44 65 66 2c 20 6e 46 61 72 67 2c  db, pDef, nFarg,
21610 20 70 46 61 72 67 2d 3e 61 5b 31 5d 2e 70 45 78   pFarg->a[1].pEx
21611 70 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  pr);.      }else
21612 20 69 66 28 20 6e 46 61 72 67 3e 30 20 29 7b 0a   if( nFarg>0 ){.
21613 20 20 20 20 20 20 20 20 70 44 65 66 20 3d 20 73          pDef = s
21614 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72 6c 6f  qlite3VtabOverlo
21615 61 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70  adFunction(db, p
21616 44 65 66 2c 20 6e 46 61 72 67 2c 20 70 46 61 72  Def, nFarg, pFar
21617 67 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 29 3b 0a  g->a[0].pExpr);.
21618 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20        }.#endif. 
21619 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
2161a 6e 46 61 72 67 3b 20 69 2b 2b 29 7b 0a 20 20 20  nFarg; i++){.   
2161b 20 20 20 20 20 69 66 28 20 69 3c 33 32 20 26 26       if( i<32 &&
2161c 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f   sqlite3ExprIsCo
2161d 6e 73 74 61 6e 74 28 70 46 61 72 67 2d 3e 61 5b  nstant(pFarg->a[
2161e 69 5d 2e 70 45 78 70 72 29 20 29 7b 0a 20 20 20  i].pExpr) ){.   
2161f 20 20 20 20 20 20 20 63 6f 6e 73 74 4d 61 73 6b         constMask
21620 20 7c 3d 20 28 31 3c 3c 69 29 3b 0a 20 20 20 20   |= (1<<i);.    
21621 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
21622 28 20 28 70 44 65 66 2d 3e 66 6c 61 67 73 20 26  ( (pDef->flags &
21623 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45   SQLITE_FUNC_NEE
21624 44 43 4f 4c 4c 29 21 3d 30 20 26 26 20 21 70 43  DCOLL)!=0 && !pC
21625 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20  oll ){.         
21626 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33   pColl = sqlite3
21627 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72  ExprCollSeq(pPar
21628 73 65 2c 20 70 46 61 72 67 2d 3e 61 5b 69 5d 2e  se, pFarg->a[i].
21629 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20  pExpr);.        
2162a 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
2162b 69 66 28 20 70 44 65 66 2d 3e 66 6c 61 67 73 20  if( pDef->flags 
2162c 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45  & SQLITE_FUNC_NE
2162d 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 20 20  EDCOLL ){.      
2162e 20 20 69 66 28 20 21 70 43 6f 6c 6c 20 29 20 70    if( !pColl ) p
2162f 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74  Coll = db->pDflt
21630 43 6f 6c 6c 3b 20 0a 20 20 20 20 20 20 20 20 73  Coll; .        s
21631 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
21632 28 76 2c 20 4f 50 5f 43 6f 6c 6c 53 65 71 2c 20  (v, OP_CollSeq, 
21633 30 2c 20 30 2c 20 30 2c 20 28 63 68 61 72 20 2a  0, 0, 0, (char *
21634 29 70 43 6f 6c 6c 2c 20 50 34 5f 43 4f 4c 4c 53  )pColl, P4_COLLS
21635 45 51 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  EQ);.      }.   
21636 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
21637 64 4f 70 34 28 76 2c 20 4f 50 5f 46 75 6e 63 74  dOp4(v, OP_Funct
21638 69 6f 6e 2c 20 63 6f 6e 73 74 4d 61 73 6b 2c 20  ion, constMask, 
21639 72 31 2c 20 74 61 72 67 65 74 2c 0a 20 20 20 20  r1, target,.    
2163a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2163b 20 20 20 20 28 63 68 61 72 2a 29 70 44 65 66 2c      (char*)pDef,
2163c 20 50 34 5f 46 55 4e 43 44 45 46 29 3b 0a 20 20   P4_FUNCDEF);.  
2163d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
2163e 68 61 6e 67 65 50 35 28 76 2c 20 28 75 38 29 6e  hangeP5(v, (u8)n
2163f 46 61 72 67 29 3b 0a 20 20 20 20 20 20 69 66 28  Farg);.      if(
21640 20 6e 46 61 72 67 20 29 7b 0a 20 20 20 20 20 20   nFarg ){.      
21641 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
21642 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65  TempRange(pParse
21643 2c 20 72 31 2c 20 6e 46 61 72 67 29 3b 0a 20 20  , r1, nFarg);.  
21644 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
21645 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69  te3ExprCacheAffi
21646 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73  nityChange(pPars
21647 65 2c 20 72 31 2c 20 6e 46 61 72 67 29 3b 0a 20  e, r1, nFarg);. 
21648 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
21649 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
2164a 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20  _OMIT_SUBQUERY. 
2164b 20 20 20 63 61 73 65 20 54 4b 5f 45 58 49 53 54     case TK_EXIST
2164c 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53  S:.    case TK_S
2164d 45 4c 45 43 54 3a 20 7b 0a 20 20 20 20 20 20 74  ELECT: {.      t
2164e 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
2164f 45 58 49 53 54 53 20 29 3b 0a 20 20 20 20 20 20  EXISTS );.      
21650 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
21651 5f 53 45 4c 45 43 54 20 29 3b 0a 20 20 20 20 20  _SELECT );.     
21652 20 73 71 6c 69 74 65 33 43 6f 64 65 53 75 62 73   sqlite3CodeSubs
21653 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 45  elect(pParse, pE
21654 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  xpr, 0, 0);.    
21655 20 20 69 6e 52 65 67 20 3d 20 70 45 78 70 72 2d    inReg = pExpr-
21656 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20  >iColumn;.      
21657 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
21658 20 63 61 73 65 20 54 4b 5f 49 4e 3a 20 7b 0a 20   case TK_IN: {. 
21659 20 20 20 20 20 69 6e 74 20 72 4e 6f 74 46 6f 75       int rNotFou
2165a 6e 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 6e  nd = 0;.      in
2165b 74 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 3d  t rMayHaveNull =
2165c 20 30 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 32   0;.      int j2
2165d 2c 20 6a 33 2c 20 6a 34 2c 20 6a 35 3b 0a 20 20  , j3, j4, j5;.  
2165e 20 20 20 20 63 68 61 72 20 61 66 66 69 6e 69 74      char affinit
2165f 79 3b 0a 20 20 20 20 20 20 69 6e 74 20 65 54 79  y;.      int eTy
21660 70 65 3b 0a 0a 20 20 20 20 20 20 56 64 62 65 4e  pe;..      VdbeN
21661 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  oopComment((v, "
21662 62 65 67 69 6e 20 49 4e 20 65 78 70 72 20 72 25  begin IN expr r%
21663 64 22 2c 20 74 61 72 67 65 74 29 29 3b 0a 20 20  d", target));.  
21664 20 20 20 20 65 54 79 70 65 20 3d 20 73 71 6c 69      eType = sqli
21665 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 28 70  te3FindInIndex(p
21666 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 26 72  Parse, pExpr, &r
21667 4d 61 79 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 20  MayHaveNull);.  
21668 20 20 20 20 69 66 28 20 72 4d 61 79 48 61 76 65      if( rMayHave
21669 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  Null ){.        
2166a 72 4e 6f 74 46 6f 75 6e 64 20 3d 20 2b 2b 70 50  rNotFound = ++pP
2166b 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20  arse->nMem;.    
2166c 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 46 69    }..      /* Fi
2166d 67 75 72 65 20 6f 75 74 20 74 68 65 20 61 66 66  gure out the aff
2166e 69 6e 69 74 79 20 74 6f 20 75 73 65 20 74 6f 20  inity to use to 
2166f 63 72 65 61 74 65 20 61 20 6b 65 79 20 66 72 6f  create a key fro
21670 6d 20 74 68 65 20 72 65 73 75 6c 74 73 0a 20 20  m the results.  
21671 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 65 78      ** of the ex
21672 70 72 65 73 73 69 6f 6e 2e 20 61 66 66 69 6e 69  pression. affini
21673 74 79 53 74 72 20 73 74 6f 72 65 73 20 61 20 73  tyStr stores a s
21674 74 61 74 69 63 20 73 74 72 69 6e 67 20 73 75 69  tatic string sui
21675 74 61 62 6c 65 20 66 6f 72 0a 20 20 20 20 20 20  table for.      
21676 2a 2a 20 50 34 20 6f 66 20 4f 50 5f 4d 61 6b 65  ** P4 of OP_Make
21677 52 65 63 6f 72 64 2e 0a 20 20 20 20 20 20 2a 2f  Record..      */
21678 0a 20 20 20 20 20 20 61 66 66 69 6e 69 74 79 20  .      affinity 
21679 3d 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69  = comparisonAffi
2167a 6e 69 74 79 28 70 45 78 70 72 29 3b 0a 0a 0a 20  nity(pExpr);... 
2167b 20 20 20 20 20 2f 2a 20 43 6f 64 65 20 74 68 65       /* Code the
2167c 20 3c 65 78 70 72 3e 20 66 72 6f 6d 20 22 3c 65   <expr> from "<e
2167d 78 70 72 3e 20 49 4e 20 28 2e 2e 2e 29 22 2e 20  xpr> IN (...)". 
2167e 54 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61  The temporary ta
2167f 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20 70 45 78  ble.      ** pEx
21680 70 72 2d 3e 69 54 61 62 6c 65 20 63 6f 6e 74 61  pr->iTable conta
21681 69 6e 73 20 74 68 65 20 76 61 6c 75 65 73 20 74  ins the values t
21682 68 61 74 20 6d 61 6b 65 20 75 70 20 74 68 65 20  hat make up the 
21683 28 2e 2e 2e 29 20 73 65 74 2e 0a 20 20 20 20 20  (...) set..     
21684 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
21685 33 45 78 70 72 43 61 63 68 65 50 75 73 68 28 70  3ExprCachePush(p
21686 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 73 71  Parse);.      sq
21687 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
21688 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65  arse, pExpr->pLe
21689 66 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20  ft, target);.   
2168a 20 20 20 6a 32 20 3d 20 73 71 6c 69 74 65 33 56     j2 = sqlite3V
2168b 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2168c 49 73 4e 75 6c 6c 2c 20 74 61 72 67 65 74 29 3b  IsNull, target);
2168d 0a 20 20 20 20 20 20 69 66 28 20 65 54 79 70 65  .      if( eType
2168e 3d 3d 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44  ==IN_INDEX_ROWID
2168f 20 29 7b 0a 20 20 20 20 20 20 20 20 6a 33 20 3d   ){.        j3 =
21690 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21691 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49  p1(v, OP_MustBeI
21692 6e 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20  nt, target);.   
21693 20 20 20 20 20 6a 34 20 3d 20 73 71 6c 69 74 65       j4 = sqlite
21694 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
21695 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20 70 45 78  P_NotExists, pEx
21696 70 72 2d 3e 69 54 61 62 6c 65 2c 20 30 2c 20 74  pr->iTable, 0, t
21697 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20  arget);.        
21698 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
21699 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
2169a 20 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20   1, target);.   
2169b 20 20 20 20 20 6a 35 20 3d 20 73 71 6c 69 74 65       j5 = sqlite
2169c 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f  3VdbeAddOp0(v, O
2169d 50 5f 47 6f 74 6f 29 3b 0a 20 20 20 20 20 20 20  P_Goto);.       
2169e 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
2169f 48 65 72 65 28 76 2c 20 6a 33 29 3b 0a 20 20 20  Here(v, j3);.   
216a0 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
216a1 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 34 29 3b  JumpHere(v, j4);
216a2 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
216a3 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
216a4 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 74 61 72  _Integer, 0, tar
216a5 67 65 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  get);.      }els
216a6 65 7b 0a 20 20 20 20 20 20 20 20 72 32 20 3d 20  e{.        r2 = 
216a7 72 65 67 46 72 65 65 32 20 3d 20 73 71 6c 69 74  regFree2 = sqlit
216a8 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
216a9 72 73 65 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f  rse);..        /
216aa 2a 20 43 72 65 61 74 65 20 61 20 72 65 63 6f 72  * Create a recor
216ab 64 20 61 6e 64 20 74 65 73 74 20 66 6f 72 20 73  d and test for s
216ac 65 74 20 6d 65 6d 62 65 72 73 68 69 70 2e 20 49  et membership. I
216ad 66 20 74 68 65 20 73 65 74 20 63 6f 6e 74 61 69  f the set contai
216ae 6e 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68  ns.        ** th
216af 65 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 6a 75  e value, then ju
216b0 6d 70 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66  mp to the end of
216b1 20 74 68 65 20 74 65 73 74 20 63 6f 64 65 2e 20   the test code. 
216b2 54 68 65 20 74 61 72 67 65 74 0a 20 20 20 20 20  The target.     
216b3 20 20 20 2a 2a 20 72 65 67 69 73 74 65 72 20 73     ** register s
216b4 74 69 6c 6c 20 63 6f 6e 74 61 69 6e 73 20 74 68  till contains th
216b5 65 20 74 72 75 65 20 28 31 29 20 76 61 6c 75 65  e true (1) value
216b6 20 77 72 69 74 74 65 6e 20 74 6f 20 69 74 20 65   written to it e
216b7 61 72 6c 69 65 72 2e 0a 20 20 20 20 20 20 20 20  arlier..        
216b8 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  */.        sqlit
216b9 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
216ba 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 74  OP_MakeRecord, t
216bb 61 72 67 65 74 2c 20 31 2c 20 72 32 2c 20 26 61  arget, 1, r2, &a
216bc 66 66 69 6e 69 74 79 2c 20 31 29 3b 0a 20 20 20  ffinity, 1);.   
216bd 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
216be 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
216bf 65 67 65 72 2c 20 31 2c 20 74 61 72 67 65 74 29  eger, 1, target)
216c0 3b 0a 20 20 20 20 20 20 20 20 6a 35 20 3d 20 73  ;.        j5 = s
216c1 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
216c2 28 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 70 45  (v, OP_Found, pE
216c3 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 30 2c 20  xpr->iTable, 0, 
216c4 72 32 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a  r2);..        /*
216c5 20 49 66 20 74 68 65 20 73 65 74 20 6d 65 6d 62   If the set memb
216c6 65 72 73 68 69 70 20 74 65 73 74 20 66 61 69 6c  ership test fail
216c7 73 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75  s, then the resu
216c8 6c 74 20 6f 66 20 74 68 65 20 0a 20 20 20 20 20  lt of the .     
216c9 20 20 20 2a 2a 20 22 78 20 49 4e 20 28 2e 2e 2e     ** "x IN (...
216ca 29 22 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 75  )" expression mu
216cb 73 74 20 62 65 20 65 69 74 68 65 72 20 30 20 6f  st be either 0 o
216cc 72 20 4e 55 4c 4c 2e 20 49 66 20 74 68 65 20 73  r NULL. If the s
216cd 65 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 6f  et.        ** co
216ce 6e 74 61 69 6e 73 20 6e 6f 20 4e 55 4c 4c 20 76  ntains no NULL v
216cf 61 6c 75 65 73 2c 20 74 68 65 6e 20 74 68 65 20  alues, then the 
216d0 72 65 73 75 6c 74 20 69 73 20 30 2e 20 49 66 20  result is 0. If 
216d1 74 68 65 20 73 65 74 20 0a 20 20 20 20 20 20 20  the set .       
216d2 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65   ** contains one
216d3 20 6f 72 20 6d 6f 72 65 20 4e 55 4c 4c 20 76 61   or more NULL va
216d4 6c 75 65 73 2c 20 74 68 65 6e 20 74 68 65 20 72  lues, then the r
216d5 65 73 75 6c 74 20 6f 66 20 74 68 65 0a 20 20 20  esult of the.   
216d6 20 20 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69       ** expressi
216d7 6f 6e 20 69 73 20 61 6c 73 6f 20 4e 55 4c 4c 2e  on is also NULL.
216d8 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
216d9 20 20 20 20 69 66 28 20 72 4e 6f 74 46 6f 75 6e      if( rNotFoun
216da 64 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  d==0 ){.        
216db 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e 63 68    /* This branch
216dc 20 72 75 6e 73 20 69 66 20 69 74 20 69 73 20 6b   runs if it is k
216dd 6e 6f 77 6e 20 61 74 20 63 6f 6d 70 69 6c 65 20  nown at compile 
216de 74 69 6d 65 20 28 6e 6f 77 29 20 74 68 61 74 20  time (now) that 
216df 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68  .          ** th
216e0 65 20 73 65 74 20 63 6f 6e 74 61 69 6e 73 20 6e  e set contains n
216e1 6f 20 4e 55 4c 4c 20 76 61 6c 75 65 73 2e 20 54  o NULL values. T
216e2 68 69 73 20 68 61 70 70 65 6e 73 20 61 73 20 74  his happens as t
216e3 68 65 20 72 65 73 75 6c 74 0a 20 20 20 20 20 20  he result.      
216e4 20 20 20 20 2a 2a 20 6f 66 20 61 20 22 4e 4f 54      ** of a "NOT
216e5 20 4e 55 4c 4c 22 20 63 6f 6e 73 74 72 61 69 6e   NULL" constrain
216e6 74 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  t in the databas
216e7 65 20 73 63 68 65 6d 61 2e 20 4e 6f 20 6e 65 65  e schema. No nee
216e8 64 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74  d.          ** t
216e9 6f 20 74 65 73 74 20 74 68 65 20 64 61 74 61 20  o test the data 
216ea 73 74 72 75 63 74 75 72 65 20 61 74 20 72 75 6e  structure at run
216eb 74 69 6d 65 20 69 6e 20 74 68 69 73 20 63 61 73  time in this cas
216ec 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  e..          */.
216ed 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
216ee 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
216ef 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 74 61  P_Integer, 0, ta
216f0 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 7d  rget);.        }
216f1 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
216f2 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 70 6f  /* This block po
216f3 70 75 6c 61 74 65 73 20 74 68 65 20 72 4e 6f 74  pulates the rNot
216f4 46 6f 75 6e 64 20 72 65 67 69 73 74 65 72 20 77  Found register w
216f5 69 74 68 20 65 69 74 68 65 72 20 4e 55 4c 4c 0a  ith either NULL.
216f6 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20            ** or 
216f7 30 20 28 61 6e 20 69 6e 74 65 67 65 72 20 76 61  0 (an integer va
216f8 6c 75 65 29 2e 20 49 66 20 74 68 65 20 64 61 74  lue). If the dat
216f9 61 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74  a structure cont
216fa 61 69 6e 73 20 6f 6e 65 0a 20 20 20 20 20 20 20  ains one.       
216fb 20 20 20 2a 2a 20 6f 72 20 6d 6f 72 65 20 4e 55     ** or more NU
216fc 4c 4c 73 2c 20 74 68 65 6e 20 73 65 74 20 72 4e  LLs, then set rN
216fd 6f 74 46 6f 75 6e 64 20 74 6f 20 4e 55 4c 4c 2e  otFound to NULL.
216fe 20 4f 74 68 65 72 77 69 73 65 2c 20 73 65 74 20   Otherwise, set 
216ff 69 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  it.          ** 
21700 74 6f 20 30 2e 20 49 66 20 72 65 67 69 73 74 65  to 0. If registe
21701 72 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 69  r rMayHaveNull i
21702 73 20 61 6c 72 65 61 64 79 20 73 65 74 20 74 6f  s already set to
21703 20 73 6f 6d 65 20 76 61 6c 75 65 0a 20 20 20 20   some value.    
21704 20 20 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 74        ** other t
21705 68 61 6e 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74  han NULL, then t
21706 68 65 20 74 65 73 74 20 68 61 73 20 61 6c 72 65  he test has alre
21707 61 64 79 20 62 65 65 6e 20 72 75 6e 20 61 6e 64  ady been run and
21708 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 72   .          ** r
21709 4e 6f 74 46 6f 75 6e 64 20 69 73 20 61 6c 72 65  NotFound is alre
2170a 61 64 79 20 70 6f 70 75 6c 61 74 65 64 2e 0a 20  ady populated.. 
2170b 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20           */.    
2170c 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e        static con
2170d 73 74 20 63 68 61 72 20 6e 75 6c 6c 52 65 63 6f  st char nullReco
2170e 72 64 5b 5d 20 3d 20 7b 20 30 78 30 32 2c 20 30  rd[] = { 0x02, 0
2170f 78 30 30 20 7d 3b 0a 20 20 20 20 20 20 20 20 20  x00 };.         
21710 20 6a 33 20 3d 20 73 71 6c 69 74 65 33 56 64 62   j3 = sqlite3Vdb
21711 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f  eAddOp1(v, OP_No
21712 74 4e 75 6c 6c 2c 20 72 4d 61 79 48 61 76 65 4e  tNull, rMayHaveN
21713 75 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ull);.          
21714 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
21715 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c  2(v, OP_Null, 0,
21716 20 72 4e 6f 74 46 6f 75 6e 64 29 3b 0a 20 20 20   rNotFound);.   
21717 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
21718 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 42  beAddOp4(v, OP_B
21719 6c 6f 62 2c 20 32 2c 20 72 4d 61 79 48 61 76 65  lob, 2, rMayHave
2171a 4e 75 6c 6c 2c 20 30 2c 20 0a 20 20 20 20 20 20  Null, 0, .      
2171b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2171c 20 20 20 20 20 20 20 6e 75 6c 6c 52 65 63 6f 72         nullRecor
2171d 64 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20  d, P4_STATIC);. 
2171e 20 20 20 20 20 20 20 20 20 6a 34 20 3d 20 73 71           j4 = sq
2171f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
21720 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 70 45 78  v, OP_Found, pEx
21721 70 72 2d 3e 69 54 61 62 6c 65 2c 20 30 2c 20 72  pr->iTable, 0, r
21722 4d 61 79 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 20  MayHaveNull);.  
21723 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
21724 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
21725 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 4e 6f 74  Integer, 0, rNot
21726 46 6f 75 6e 64 29 3b 0a 20 20 20 20 20 20 20 20  Found);.        
21727 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
21728 70 48 65 72 65 28 76 2c 20 6a 34 29 3b 0a 20 20  pHere(v, j4);.  
21729 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2172a 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a  dbeJumpHere(v, j
2172b 33 29 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 2f  3);..          /
2172c 2a 20 43 6f 70 79 20 74 68 65 20 76 61 6c 75 65  * Copy the value
2172d 20 6f 66 20 72 65 67 69 73 74 65 72 20 72 4e 6f   of register rNo
2172e 74 46 6f 75 6e 64 20 28 77 68 69 63 68 20 69 73  tFound (which is
2172f 20 65 69 74 68 65 72 20 4e 55 4c 4c 20 6f 72 20   either NULL or 
21730 30 29 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  0).          ** 
21731 69 6e 74 6f 20 74 68 65 20 74 61 72 67 65 74 20  into the target 
21732 72 65 67 69 73 74 65 72 2e 20 54 68 69 73 20 77  register. This w
21733 69 6c 6c 20 62 65 20 74 68 65 20 72 65 73 75 6c  ill be the resul
21734 74 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20  t of the.       
21735 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e     ** expression
21736 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  ..          */. 
21737 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
21738 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
21739 5f 43 6f 70 79 2c 20 72 4e 6f 74 46 6f 75 6e 64  _Copy, rNotFound
2173a 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20  , target);.     
2173b 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
2173c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
2173d 6d 70 48 65 72 65 28 76 2c 20 6a 32 29 3b 0a 20  mpHere(v, j2);. 
2173e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2173f 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 35 29 3b  JumpHere(v, j5);
21740 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
21741 70 72 43 61 63 68 65 50 6f 70 28 70 50 61 72 73  prCachePop(pPars
21742 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 56 64 62  e, 1);.      Vdb
21743 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 65 6e  eComment((v, "en
21744 64 20 49 4e 20 65 78 70 72 20 72 25 64 22 2c 20  d IN expr r%d", 
21745 74 61 72 67 65 74 29 29 3b 0a 20 20 20 20 20 20  target));.      
21746 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e  break;.    }.#en
21747 64 69 66 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a  dif.    /*.    *
21748 2a 20 20 20 20 78 20 42 45 54 57 45 45 4e 20 79  *    x BETWEEN y
21749 20 41 4e 44 20 7a 0a 20 20 20 20 2a 2a 0a 20 20   AND z.    **.  
2174a 20 20 2a 2a 20 54 68 69 73 20 69 73 20 65 71 75    ** This is equ
2174b 69 76 61 6c 65 6e 74 20 74 6f 0a 20 20 20 20 2a  ivalent to.    *
2174c 2a 0a 20 20 20 20 2a 2a 20 20 20 20 78 3e 3d 79  *.    **    x>=y
2174d 20 41 4e 44 20 78 3c 3d 7a 0a 20 20 20 20 2a 2a   AND x<=z.    **
2174e 0a 20 20 20 20 2a 2a 20 58 20 69 73 20 73 74 6f  .    ** X is sto
2174f 72 65 64 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c  red in pExpr->pL
21750 65 66 74 2e 0a 20 20 20 20 2a 2a 20 59 20 69 73  eft..    ** Y is
21751 20 73 74 6f 72 65 64 20 69 6e 20 70 45 78 70 72   stored in pExpr
21752 2d 3e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45  ->pList->a[0].pE
21753 78 70 72 2e 0a 20 20 20 20 2a 2a 20 5a 20 69 73  xpr..    ** Z is
21754 20 73 74 6f 72 65 64 20 69 6e 20 70 45 78 70 72   stored in pExpr
21755 2d 3e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45  ->pList->a[1].pE
21756 78 70 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  xpr..    */.    
21757 63 61 73 65 20 54 4b 5f 42 45 54 57 45 45 4e 3a  case TK_BETWEEN:
21758 20 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70   {.      Expr *p
21759 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c  Left = pExpr->pL
2175a 65 66 74 3b 0a 20 20 20 20 20 20 73 74 72 75 63  eft;.      struc
2175b 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
2175c 2a 70 4c 49 74 65 6d 20 3d 20 70 45 78 70 72 2d  *pLItem = pExpr-
2175d 3e 78 2e 70 4c 69 73 74 2d 3e 61 3b 0a 20 20 20  >x.pList->a;.   
2175e 20 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 20     Expr *pRight 
2175f 3d 20 70 4c 49 74 65 6d 2d 3e 70 45 78 70 72 3b  = pLItem->pExpr;
21760 0a 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70  ..      codeComp
21761 61 72 65 4f 70 65 72 61 6e 64 73 28 70 50 61 72  areOperands(pPar
21762 73 65 2c 20 70 4c 65 66 74 2c 20 26 72 31 2c 20  se, pLeft, &r1, 
21763 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20  &regFree1,.     
21764 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21765 20 20 20 20 20 20 20 20 20 20 20 20 20 70 52 69               pRi
21766 67 68 74 2c 20 26 72 32 2c 20 26 72 65 67 46 72  ght, &r2, &regFr
21767 65 65 32 29 3b 0a 20 20 20 20 20 20 74 65 73 74  ee2);.      test
21768 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d  case( regFree1==
21769 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  0 );.      testc
2176a 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30  ase( regFree2==0
2176b 20 29 3b 0a 20 20 20 20 20 20 72 33 20 3d 20 73   );.      r3 = s
2176c 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
2176d 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
2176e 72 34 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  r4 = sqlite3GetT
2176f 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
21770 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72        codeCompar
21771 65 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 2c  e(pParse, pLeft,
21772 20 70 52 69 67 68 74 2c 20 4f 50 5f 47 65 2c 0a   pRight, OP_Ge,.
21773 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21774 20 20 72 31 2c 20 72 32 2c 20 72 33 2c 20 53 51    r1, r2, r3, SQ
21775 4c 49 54 45 5f 53 54 4f 52 45 50 32 29 3b 0a 20  LITE_STOREP2);. 
21776 20 20 20 20 20 70 4c 49 74 65 6d 2b 2b 3b 0a 20       pLItem++;. 
21777 20 20 20 20 20 70 52 69 67 68 74 20 3d 20 70 4c       pRight = pL
21778 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20  Item->pExpr;.   
21779 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
2177a 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
2177b 20 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20   regFree2);.    
2177c 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78    r2 = sqlite3Ex
2177d 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73  prCodeTemp(pPars
2177e 65 2c 20 70 52 69 67 68 74 2c 20 26 72 65 67 46  e, pRight, &regF
2177f 72 65 65 32 29 3b 0a 20 20 20 20 20 20 74 65 73  ree2);.      tes
21780 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d  tcase( regFree2=
21781 3d 30 20 29 3b 0a 20 20 20 20 20 20 63 6f 64 65  =0 );.      code
21782 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20  Compare(pParse, 
21783 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 4f  pLeft, pRight, O
21784 50 5f 4c 65 2c 20 72 31 2c 20 72 32 2c 20 72 34  P_Le, r1, r2, r4
21785 2c 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32  , SQLITE_STOREP2
21786 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
21787 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
21788 5f 41 6e 64 2c 20 72 33 2c 20 72 34 2c 20 74 61  _And, r3, r4, ta
21789 72 67 65 74 29 3b 0a 20 20 20 20 20 20 73 71 6c  rget);.      sql
2178a 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
2178b 65 67 28 70 50 61 72 73 65 2c 20 72 33 29 3b 0a  eg(pParse, r3);.
2178c 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c        sqlite3Rel
2178d 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
2178e 73 65 2c 20 72 34 29 3b 0a 20 20 20 20 20 20 62  se, r4);.      b
2178f 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
21790 63 61 73 65 20 54 4b 5f 55 50 4c 55 53 3a 20 7b  case TK_UPLUS: {
21791 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 73  .      inReg = s
21792 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61  qlite3ExprCodeTa
21793 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78  rget(pParse, pEx
21794 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67 65  pr->pLeft, targe
21795 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  t);.      break;
21796 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63 61 73 65  .    }..    case
21797 20 54 4b 5f 54 52 49 47 47 45 52 3a 20 7b 0a 20   TK_TRIGGER: {. 
21798 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6f       /* If the o
21799 70 63 6f 64 65 20 69 73 20 54 4b 5f 54 52 49 47  pcode is TK_TRIG
2179a 47 45 52 2c 20 74 68 65 6e 20 74 68 65 20 65 78  GER, then the ex
2179b 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 72 65  pression is a re
2179c 66 65 72 65 6e 63 65 0a 20 20 20 20 20 20 2a 2a  ference.      **
2179d 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20   to a column in 
2179e 74 68 65 20 6e 65 77 2e 2a 20 6f 72 20 6f 6c 64  the new.* or old
2179f 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 73  .* pseudo-tables
217a0 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 0a 20 20   available to.  
217a1 20 20 20 20 2a 2a 20 74 72 69 67 67 65 72 20 70      ** trigger p
217a2 72 6f 67 72 61 6d 73 2e 20 49 6e 20 74 68 69 73  rograms. In this
217a3 20 63 61 73 65 20 45 78 70 72 2e 69 54 61 62 6c   case Expr.iTabl
217a4 65 20 69 73 20 73 65 74 20 74 6f 20 31 20 66 6f  e is set to 1 fo
217a5 72 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 6e  r the.      ** n
217a6 65 77 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62 6c  ew.* pseudo-tabl
217a7 65 2c 20 6f 72 20 30 20 66 6f 72 20 74 68 65 20  e, or 0 for the 
217a8 6f 6c 64 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62  old.* pseudo-tab
217a9 6c 65 2e 20 45 78 70 72 2e 69 43 6f 6c 75 6d 6e  le. Expr.iColumn
217aa 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 73 65 74  .      ** is set
217ab 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f   to the column o
217ac 66 20 74 68 65 20 70 73 65 75 64 6f 2d 74 61 62  f the pseudo-tab
217ad 6c 65 20 74 6f 20 72 65 61 64 2c 20 6f 72 20 74  le to read, or t
217ae 6f 20 2d 31 20 74 6f 0a 20 20 20 20 20 20 2a 2a  o -1 to.      **
217af 20 72 65 61 64 20 74 68 65 20 72 6f 77 69 64 20   read the rowid 
217b0 66 69 65 6c 64 2e 0a 20 20 20 20 20 20 2a 2a 0a  field..      **.
217b1 20 20 20 20 20 20 2a 2a 20 54 68 65 20 65 78 70        ** The exp
217b2 72 65 73 73 69 6f 6e 20 69 73 20 69 6d 70 6c 65  ression is imple
217b3 6d 65 6e 74 65 64 20 75 73 69 6e 67 20 61 6e 20  mented using an 
217b4 4f 50 5f 50 61 72 61 6d 20 6f 70 63 6f 64 65 2e  OP_Param opcode.
217b5 20 54 68 65 20 70 31 0a 20 20 20 20 20 20 2a 2a   The p1.      **
217b6 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 73 65   parameter is se
217b7 74 20 74 6f 20 30 20 66 6f 72 20 61 6e 20 6f 6c  t to 0 for an ol
217b8 64 2e 72 6f 77 69 64 20 72 65 66 65 72 65 6e 63  d.rowid referenc
217b9 65 2c 20 6f 72 20 74 6f 20 28 69 2b 31 29 0a 20  e, or to (i+1). 
217ba 20 20 20 20 20 2a 2a 20 74 6f 20 72 65 66 65 72       ** to refer
217bb 65 6e 63 65 20 61 6e 6f 74 68 65 72 20 63 6f 6c  ence another col
217bc 75 6d 6e 20 6f 66 20 74 68 65 20 6f 6c 64 2e 2a  umn of the old.*
217bd 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2c 20 77   pseudo-table, w
217be 68 65 72 65 20 0a 20 20 20 20 20 20 2a 2a 20 69  here .      ** i
217bf 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66   is the index of
217c0 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 46 6f 72   the column. For
217c1 20 61 20 6e 65 77 2e 72 6f 77 69 64 20 72 65 66   a new.rowid ref
217c2 65 72 65 6e 63 65 2c 20 70 31 20 69 73 0a 20 20  erence, p1 is.  
217c3 20 20 20 20 2a 2a 20 73 65 74 20 74 6f 20 28 6e      ** set to (n
217c4 2b 31 29 2c 20 77 68 65 72 65 20 6e 20 69 73 20  +1), where n is 
217c5 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f  the number of co
217c6 6c 75 6d 6e 73 20 69 6e 20 65 61 63 68 20 70 73  lumns in each ps
217c7 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 20 20 20 20  eudo-table..    
217c8 20 20 2a 2a 20 46 6f 72 20 61 20 72 65 66 65 72    ** For a refer
217c9 65 6e 63 65 20 74 6f 20 61 6e 79 20 6f 74 68 65  ence to any othe
217ca 72 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20  r column in the 
217cb 6e 65 77 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62  new.* pseudo-tab
217cc 6c 65 2c 20 70 31 0a 20 20 20 20 20 20 2a 2a 20  le, p1.      ** 
217cd 69 73 20 73 65 74 20 74 6f 20 28 6e 2b 32 2b 69  is set to (n+2+i
217ce 29 2c 20 77 68 65 72 65 20 6e 20 61 6e 64 20 69  ), where n and i
217cf 20 61 72 65 20 61 73 20 64 65 66 69 6e 65 64 20   are as defined 
217d0 70 72 65 76 69 6f 75 73 6c 79 2e 20 46 6f 72 0a  previously. For.
217d1 20 20 20 20 20 20 2a 2a 20 65 78 61 6d 70 6c 65        ** example
217d2 2c 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 6f  , if the table o
217d3 6e 20 77 68 69 63 68 20 74 72 69 67 67 65 72 73  n which triggers
217d4 20 61 72 65 20 62 65 69 6e 67 20 66 69 72 65 64   are being fired
217d5 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 64 65 63   is.      ** dec
217d6 6c 61 72 65 64 20 61 73 3a 0a 20 20 20 20 20 20  lared as:.      
217d7 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 43 52  **.      **   CR
217d8 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c  EATE TABLE t1(a,
217d9 20 62 29 3b 0a 20 20 20 20 20 20 2a 2a 0a 20 20   b);.      **.  
217da 20 20 20 20 2a 2a 20 54 68 65 6e 20 70 31 20 69      ** Then p1 i
217db 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73  s interpreted as
217dc 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 20 20 20 20   follows:.      
217dd 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 70 31  **.      **   p1
217de 3d 3d 30 20 20 20 2d 3e 20 20 20 20 6f 6c 64 2e  ==0   ->    old.
217df 72 6f 77 69 64 20 20 20 20 20 70 31 3d 3d 33 20  rowid     p1==3 
217e0 20 20 2d 3e 20 20 20 20 6e 65 77 2e 72 6f 77 69    ->    new.rowi
217e1 64 0a 20 20 20 20 20 20 2a 2a 20 20 20 70 31 3d  d.      **   p1=
217e2 3d 31 20 20 20 2d 3e 20 20 20 20 6f 6c 64 2e 61  =1   ->    old.a
217e3 20 20 20 20 20 20 20 20 20 70 31 3d 3d 34 20 20           p1==4  
217e4 20 2d 3e 20 20 20 20 6e 65 77 2e 61 0a 20 20 20   ->    new.a.   
217e5 20 20 20 2a 2a 20 20 20 70 31 3d 3d 32 20 20 20     **   p1==2   
217e6 2d 3e 20 20 20 20 6f 6c 64 2e 62 20 20 20 20 20  ->    old.b     
217e7 20 20 20 20 70 31 3d 3d 35 20 20 20 2d 3e 20 20      p1==5   ->  
217e8 20 20 6e 65 77 2e 62 20 20 20 20 20 20 20 0a 20    new.b       . 
217e9 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 54 61       */.      Ta
217ea 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 45 78 70  ble *pTab = pExp
217eb 72 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 69  r->pTab;.      i
217ec 6e 74 20 70 31 20 3d 20 70 45 78 70 72 2d 3e 69  nt p1 = pExpr->i
217ed 54 61 62 6c 65 20 2a 20 28 70 54 61 62 2d 3e 6e  Table * (pTab->n
217ee 43 6f 6c 2b 31 29 20 2b 20 31 20 2b 20 70 45 78  Col+1) + 1 + pEx
217ef 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 0a 20 20  pr->iColumn;..  
217f0 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70      assert( pExp
217f1 72 2d 3e 69 54 61 62 6c 65 3d 3d 30 20 7c 7c 20  r->iTable==0 || 
217f2 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 31  pExpr->iTable==1
217f3 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
217f4 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e  ( pExpr->iColumn
217f5 3e 3d 2d 31 20 26 26 20 70 45 78 70 72 2d 3e 69  >=-1 && pExpr->i
217f6 43 6f 6c 75 6d 6e 3c 70 54 61 62 2d 3e 6e 43 6f  Column<pTab->nCo
217f7 6c 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  l );.      asser
217f8 74 28 20 70 54 61 62 2d 3e 69 50 4b 65 79 3c 30  t( pTab->iPKey<0
217f9 20 7c 7c 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75   || pExpr->iColu
217fa 6d 6e 21 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20  mn!=pTab->iPKey 
217fb 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
217fc 20 70 31 3e 3d 30 20 26 26 20 70 31 3c 28 70 54   p1>=0 && p1<(pT
217fd 61 62 2d 3e 6e 43 6f 6c 2a 32 2b 32 29 20 29 3b  ab->nCol*2+2) );
217fe 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ..      sqlite3V
217ff 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
21800 50 61 72 61 6d 2c 20 70 31 2c 20 74 61 72 67 65  Param, p1, targe
21801 74 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f  t);.      VdbeCo
21802 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 2e 25 73  mment((v, "%s.%s
21803 20 2d 3e 20 24 25 64 22 2c 0a 20 20 20 20 20 20   -> $%d",.      
21804 20 20 28 70 45 78 70 72 2d 3e 69 54 61 62 6c 65    (pExpr->iTable
21805 20 3f 20 22 6e 65 77 22 20 3a 20 22 6f 6c 64 22   ? "new" : "old"
21806 29 2c 0a 20 20 20 20 20 20 20 20 28 70 45 78 70  ),.        (pExp
21807 72 2d 3e 69 43 6f 6c 75 6d 6e 3c 30 20 3f 20 22  r->iColumn<0 ? "
21808 72 6f 77 69 64 22 20 3a 20 70 45 78 70 72 2d 3e  rowid" : pExpr->
21809 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 45 78 70 72  pTab->aCol[pExpr
2180a 2d 3e 69 43 6f 6c 75 6d 6e 5d 2e 7a 4e 61 6d 65  ->iColumn].zName
2180b 29 2c 0a 20 20 20 20 20 20 20 20 74 61 72 67 65  ),.        targe
2180c 74 0a 20 20 20 20 20 20 29 29 3b 0a 0a 20 20 20  t.      ));..   
2180d 20 20 20 2f 2a 20 49 66 20 74 68 65 20 63 6f 6c     /* If the col
2180e 75 6d 6e 20 68 61 73 20 52 45 41 4c 20 61 66 66  umn has REAL aff
2180f 69 6e 69 74 79 2c 20 69 74 20 6d 61 79 20 63 75  inity, it may cu
21810 72 72 65 6e 74 6c 79 20 62 65 20 73 74 6f 72 65  rrently be store
21811 64 20 61 73 20 61 6e 0a 20 20 20 20 20 20 2a 2a  d as an.      **
21812 20 69 6e 74 65 67 65 72 2e 20 55 73 65 20 4f 50   integer. Use OP
21813 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 20 74 6f  _RealAffinity to
21814 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 69 73   make sure it is
21815 20 72 65 61 6c 6c 79 20 72 65 61 6c 2e 20 20 2a   really real.  *
21816 2f 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 70  /.      if( pExp
21817 72 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 30 20 0a 20  r->iColumn>=0 . 
21818 20 20 20 20 20 20 26 26 20 70 54 61 62 2d 3e 61        && pTab->a
21819 43 6f 6c 5b 70 45 78 70 72 2d 3e 69 43 6f 6c 75  Col[pExpr->iColu
2181a 6d 6e 5d 2e 61 66 66 69 6e 69 74 79 3d 3d 53 51  mn].affinity==SQ
2181b 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a 20 20  LITE_AFF_REAL.  
2181c 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 73      ){.        s
2181d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
2181e 28 76 2c 20 4f 50 5f 52 65 61 6c 41 66 66 69 6e  (v, OP_RealAffin
2181f 69 74 79 2c 20 74 61 72 67 65 74 29 3b 0a 20 20  ity, target);.  
21820 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
21821 6b 3b 0a 20 20 20 20 7d 0a 0a 0a 20 20 20 20 2f  k;.    }...    /
21822 2a 0a 20 20 20 20 2a 2a 20 46 6f 72 6d 20 41 3a  *.    ** Form A:
21823 0a 20 20 20 20 2a 2a 20 20 20 43 41 53 45 20 78  .    **   CASE x
21824 20 57 48 45 4e 20 65 31 20 54 48 45 4e 20 72 31   WHEN e1 THEN r1
21825 20 57 48 45 4e 20 65 32 20 54 48 45 4e 20 72 32   WHEN e2 THEN r2
21826 20 2e 2e 2e 20 57 48 45 4e 20 65 4e 20 54 48 45   ... WHEN eN THE
21827 4e 20 72 4e 20 45 4c 53 45 20 79 20 45 4e 44 0a  N rN ELSE y END.
21828 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 6f      **.    ** Fo
21829 72 6d 20 42 3a 0a 20 20 20 20 2a 2a 20 20 20 43  rm B:.    **   C
2182a 41 53 45 20 57 48 45 4e 20 65 31 20 54 48 45 4e  ASE WHEN e1 THEN
2182b 20 72 31 20 57 48 45 4e 20 65 32 20 54 48 45 4e   r1 WHEN e2 THEN
2182c 20 72 32 20 2e 2e 2e 20 57 48 45 4e 20 65 4e 20   r2 ... WHEN eN 
2182d 54 48 45 4e 20 72 4e 20 45 4c 53 45 20 79 20 45  THEN rN ELSE y E
2182e 4e 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  ND.    **.    **
2182f 20 46 6f 72 6d 20 41 20 69 73 20 63 61 6e 20 62   Form A is can b
21830 65 20 74 72 61 6e 73 66 6f 72 6d 65 64 20 69 6e  e transformed in
21831 74 6f 20 74 68 65 20 65 71 75 69 76 61 6c 65 6e  to the equivalen
21832 74 20 66 6f 72 6d 20 42 20 61 73 20 66 6f 6c 6c  t form B as foll
21833 6f 77 73 3a 0a 20 20 20 20 2a 2a 20 20 20 43 41  ows:.    **   CA
21834 53 45 20 57 48 45 4e 20 78 3d 65 31 20 54 48 45  SE WHEN x=e1 THE
21835 4e 20 72 31 20 57 48 45 4e 20 78 3d 65 32 20 54  N r1 WHEN x=e2 T
21836 48 45 4e 20 72 32 20 2e 2e 2e 0a 20 20 20 20 2a  HEN r2 ....    *
21837 2a 20 20 20 20 20 20 20 20 57 48 45 4e 20 78 3d  *        WHEN x=
21838 65 4e 20 54 48 45 4e 20 72 4e 20 45 4c 53 45 20  eN THEN rN ELSE 
21839 79 20 45 4e 44 0a 20 20 20 20 2a 2a 0a 20 20 20  y END.    **.   
2183a 20 2a 2a 20 58 20 28 69 66 20 69 74 20 65 78 69   ** X (if it exi
2183b 73 74 73 29 20 69 73 20 69 6e 20 70 45 78 70 72  sts) is in pExpr
2183c 2d 3e 70 4c 65 66 74 2e 0a 20 20 20 20 2a 2a 20  ->pLeft..    ** 
2183d 59 20 69 73 20 69 6e 20 70 45 78 70 72 2d 3e 70  Y is in pExpr->p
2183e 52 69 67 68 74 2e 20 20 54 68 65 20 59 20 69 73  Right.  The Y is
2183f 20 61 6c 73 6f 20 6f 70 74 69 6f 6e 61 6c 2e 20   also optional. 
21840 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 0a   If there is no.
21841 20 20 20 20 2a 2a 20 45 4c 53 45 20 63 6c 61 75      ** ELSE clau
21842 73 65 20 61 6e 64 20 6e 6f 20 6f 74 68 65 72 20  se and no other 
21843 74 65 72 6d 20 6d 61 74 63 68 65 73 2c 20 74 68  term matches, th
21844 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66  en the result of
21845 20 74 68 65 0a 20 20 20 20 2a 2a 20 65 78 70 72   the.    ** expr
21846 73 73 69 6f 6e 20 69 73 20 4e 55 4c 4c 2e 0a 20  ssion is NULL.. 
21847 20 20 20 2a 2a 20 45 69 20 69 73 20 69 6e 20 70     ** Ei is in p
21848 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 69  Expr->pList->a[i
21849 2a 32 5d 20 61 6e 64 20 52 69 20 69 73 20 70 45  *2] and Ri is pE
2184a 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 69 2a  xpr->pList->a[i*
2184b 32 2b 31 5d 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  2+1]..    **.   
2184c 20 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 6f   ** The result o
2184d 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
2184e 20 69 73 20 74 68 65 20 52 69 20 66 6f 72 20 74   is the Ri for t
2184f 68 65 20 66 69 72 73 74 20 6d 61 74 63 68 69 6e  he first matchin
21850 67 20 45 69 2c 0a 20 20 20 20 2a 2a 20 6f 72 20  g Ei,.    ** or 
21851 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d  if there is no m
21852 61 74 63 68 69 6e 67 20 45 69 2c 20 74 68 65 20  atching Ei, the 
21853 45 4c 53 45 20 74 65 72 6d 20 59 2c 20 6f 72 20  ELSE term Y, or 
21854 69 66 20 74 68 65 72 65 20 69 73 0a 20 20 20 20  if there is.    
21855 2a 2a 20 6e 6f 20 45 4c 53 45 20 74 65 72 6d 2c  ** no ELSE term,
21856 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20   NULL..    */.  
21857 20 20 64 65 66 61 75 6c 74 3a 20 61 73 73 65 72    default: asser
21858 74 28 20 6f 70 3d 3d 54 4b 5f 43 41 53 45 20 29  t( op==TK_CASE )
21859 3b 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 6e  ; {.      int en
2185a 64 4c 61 62 65 6c 3b 20 20 20 20 20 20 20 20 20  dLabel;         
2185b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47              /* G
2185c 4f 54 4f 20 6c 61 62 65 6c 20 66 6f 72 20 65 6e  OTO label for en
2185d 64 20 6f 66 20 43 41 53 45 20 73 74 6d 74 20 2a  d of CASE stmt *
2185e 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 65 78 74  /.      int next
2185f 43 61 73 65 3b 20 20 20 20 20 20 20 20 20 20 20  Case;           
21860 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47 4f 54            /* GOT
21861 4f 20 6c 61 62 65 6c 20 66 6f 72 20 6e 65 78 74  O label for next
21862 20 57 48 45 4e 20 63 6c 61 75 73 65 20 2a 2f 0a   WHEN clause */.
21863 20 20 20 20 20 20 69 6e 74 20 6e 45 78 70 72 3b        int nExpr;
21864 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21865 20 20 20 20 20 20 20 20 2f 2a 20 32 78 20 6e 75          /* 2x nu
21866 6d 62 65 72 20 6f 66 20 57 48 45 4e 20 74 65 72  mber of WHEN ter
21867 6d 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  ms */.      int 
21868 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
21869 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2186a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
2186b 0a 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20  .      ExprList 
2186c 2a 70 45 4c 69 73 74 3b 20 20 20 20 20 20 20 20  *pEList;        
2186d 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74           /* List
2186e 20 6f 66 20 57 48 45 4e 20 74 65 72 6d 73 20 2a   of WHEN terms *
2186f 2f 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 45  /.      struct E
21870 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 61 4c  xprList_item *aL
21871 69 73 74 65 6c 65 6d 3b 20 20 2f 2a 20 41 72 72  istelem;  /* Arr
21872 61 79 20 6f 66 20 57 48 45 4e 20 74 65 72 6d 73  ay of WHEN terms
21873 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 6f   */.      Expr o
21874 70 43 6f 6d 70 61 72 65 3b 20 20 20 20 20 20 20  pCompare;       
21875 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
21876 68 65 20 58 3d 3d 45 69 20 65 78 70 72 65 73 73  he X==Ei express
21877 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 45 78 70  ion */.      Exp
21878 72 20 63 61 63 68 65 58 3b 20 20 20 20 20 20 20  r cacheX;       
21879 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2187a 2a 20 43 61 63 68 65 64 20 65 78 70 72 65 73 73  * Cached express
2187b 69 6f 6e 20 58 20 2a 2f 0a 20 20 20 20 20 20 45  ion X */.      E
2187c 78 70 72 20 2a 70 58 3b 20 20 20 20 20 20 20 20  xpr *pX;        
2187d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2187e 20 2f 2a 20 54 68 65 20 58 20 65 78 70 72 65 73   /* The X expres
2187f 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 45 78  sion */.      Ex
21880 70 72 20 2a 70 54 65 73 74 20 3d 20 30 3b 20 20  pr *pTest = 0;  
21881 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21882 2f 2a 20 58 3d 3d 45 69 20 28 66 6f 72 6d 20 41  /* X==Ei (form A
21883 29 20 6f 72 20 6a 75 73 74 20 45 69 20 28 66 6f  ) or just Ei (fo
21884 72 6d 20 42 29 20 2a 2f 0a 20 20 20 20 20 20 56  rm B) */.      V
21885 56 41 5f 4f 4e 4c 59 28 20 69 6e 74 20 69 43 61  VA_ONLY( int iCa
21886 63 68 65 4c 65 76 65 6c 20 3d 20 70 50 61 72 73  cheLevel = pPars
21887 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3b 20  e->iCacheLevel; 
21888 29 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  )..      assert(
21889 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74   !ExprHasPropert
2188a 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53  y(pExpr, EP_xIsS
2188b 65 6c 65 63 74 29 20 26 26 20 70 45 78 70 72 2d  elect) && pExpr-
2188c 3e 78 2e 70 4c 69 73 74 20 29 3b 0a 20 20 20 20  >x.pList );.    
2188d 20 20 61 73 73 65 72 74 28 28 70 45 78 70 72 2d    assert((pExpr-
2188e 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20  >x.pList->nExpr 
2188f 25 20 32 29 20 3d 3d 20 30 29 3b 0a 20 20 20 20  % 2) == 0);.    
21890 20 20 61 73 73 65 72 74 28 70 45 78 70 72 2d 3e    assert(pExpr->
21891 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3e  x.pList->nExpr >
21892 20 30 29 3b 0a 20 20 20 20 20 20 70 45 4c 69 73   0);.      pELis
21893 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69  t = pExpr->x.pLi
21894 73 74 3b 0a 20 20 20 20 20 20 61 4c 69 73 74 65  st;.      aListe
21895 6c 65 6d 20 3d 20 70 45 4c 69 73 74 2d 3e 61 3b  lem = pEList->a;
21896 0a 20 20 20 20 20 20 6e 45 78 70 72 20 3d 20 70  .      nExpr = p
21897 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20  EList->nExpr;.  
21898 20 20 20 20 65 6e 64 4c 61 62 65 6c 20 3d 20 73      endLabel = s
21899 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
2189a 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 69 66  bel(v);.      if
2189b 28 20 28 70 58 20 3d 20 70 45 78 70 72 2d 3e 70  ( (pX = pExpr->p
2189c 4c 65 66 74 29 21 3d 30 20 29 7b 0a 20 20 20 20  Left)!=0 ){.    
2189d 20 20 20 20 63 61 63 68 65 58 20 3d 20 2a 70 58      cacheX = *pX
2189e 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61  ;.        testca
2189f 73 65 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 43  se( pX->op==TK_C
218a0 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 20  OLUMN );.       
218a1 20 74 65 73 74 63 61 73 65 28 20 70 58 2d 3e 6f   testcase( pX->o
218a2 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 29  p==TK_REGISTER )
218a3 3b 0a 20 20 20 20 20 20 20 20 63 61 63 68 65 58  ;.        cacheX
218a4 2e 69 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65  .iTable = sqlite
218a5 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50  3ExprCodeTemp(pP
218a6 61 72 73 65 2c 20 70 58 2c 20 26 72 65 67 46 72  arse, pX, &regFr
218a7 65 65 31 29 3b 0a 20 20 20 20 20 20 20 20 74 65  ee1);.        te
218a8 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31  stcase( regFree1
218a9 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 63  ==0 );.        c
218aa 61 63 68 65 58 2e 6f 70 20 3d 20 54 4b 5f 52 45  acheX.op = TK_RE
218ab 47 49 53 54 45 52 3b 0a 20 20 20 20 20 20 20 20  GISTER;.        
218ac 6f 70 43 6f 6d 70 61 72 65 2e 6f 70 20 3d 20 54  opCompare.op = T
218ad 4b 5f 45 51 3b 0a 20 20 20 20 20 20 20 20 6f 70  K_EQ;.        op
218ae 43 6f 6d 70 61 72 65 2e 70 4c 65 66 74 20 3d 20  Compare.pLeft = 
218af 26 63 61 63 68 65 58 3b 0a 20 20 20 20 20 20 20  &cacheX;.       
218b0 20 70 54 65 73 74 20 3d 20 26 6f 70 43 6f 6d 70   pTest = &opComp
218b1 61 72 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  are;.      }.   
218b2 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 45     for(i=0; i<nE
218b3 78 70 72 3b 20 69 3d 69 2b 32 29 7b 0a 20 20 20  xpr; i=i+2){.   
218b4 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
218b5 43 61 63 68 65 50 75 73 68 28 70 50 61 72 73 65  CachePush(pParse
218b6 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
218b7 58 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61  X ){.          a
218b8 73 73 65 72 74 28 20 70 54 65 73 74 21 3d 30 20  ssert( pTest!=0 
218b9 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6f 70 43  );.          opC
218ba 6f 6d 70 61 72 65 2e 70 52 69 67 68 74 20 3d 20  ompare.pRight = 
218bb 61 4c 69 73 74 65 6c 65 6d 5b 69 5d 2e 70 45 78  aListelem[i].pEx
218bc 70 72 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  pr;.        }els
218bd 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70 54 65  e{.          pTe
218be 73 74 20 3d 20 61 4c 69 73 74 65 6c 65 6d 5b 69  st = aListelem[i
218bf 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20  ].pExpr;.       
218c0 20 7d 0a 20 20 20 20 20 20 20 20 6e 65 78 74 43   }.        nextC
218c1 61 73 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ase = sqlite3Vdb
218c2 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20  eMakeLabel(v);. 
218c3 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28         testcase(
218c4 20 70 54 65 73 74 2d 3e 6f 70 3d 3d 54 4b 5f 43   pTest->op==TK_C
218c5 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 20  OLUMN );.       
218c6 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61   sqlite3ExprIfFa
218c7 6c 73 65 28 70 50 61 72 73 65 2c 20 70 54 65 73  lse(pParse, pTes
218c8 74 2c 20 6e 65 78 74 43 61 73 65 2c 20 53 51 4c  t, nextCase, SQL
218c9 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b  ITE_JUMPIFNULL);
218ca 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73  .        testcas
218cb 65 28 20 61 4c 69 73 74 65 6c 65 6d 5b 69 2b 31  e( aListelem[i+1
218cc 5d 2e 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ].pExpr->op==TK_
218cd 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20  COLUMN );.      
218ce 20 20 74 65 73 74 63 61 73 65 28 20 61 4c 69 73    testcase( aLis
218cf 74 65 6c 65 6d 5b 69 2b 31 5d 2e 70 45 78 70 72  telem[i+1].pExpr
218d0 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45  ->op==TK_REGISTE
218d1 52 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  R );.        sql
218d2 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
218d3 72 73 65 2c 20 61 4c 69 73 74 65 6c 65 6d 5b 69  rse, aListelem[i
218d4 2b 31 5d 2e 70 45 78 70 72 2c 20 74 61 72 67 65  +1].pExpr, targe
218d5 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  t);.        sqli
218d6 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
218d7 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 65 6e 64   OP_Goto, 0, end
218d8 4c 61 62 65 6c 29 3b 0a 20 20 20 20 20 20 20 20  Label);.        
218d9 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
218da 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29 3b 0a  Pop(pParse, 1);.
218db 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
218dc 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
218dd 76 2c 20 6e 65 78 74 43 61 73 65 29 3b 0a 20 20  v, nextCase);.  
218de 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
218df 70 45 78 70 72 2d 3e 70 52 69 67 68 74 20 29 7b  pExpr->pRight ){
218e0 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
218e1 45 78 70 72 43 61 63 68 65 50 75 73 68 28 70 50  ExprCachePush(pP
218e2 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73  arse);.        s
218e3 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
218e4 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52  Parse, pExpr->pR
218e5 69 67 68 74 2c 20 74 61 72 67 65 74 29 3b 0a 20  ight, target);. 
218e6 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
218e7 70 72 43 61 63 68 65 50 6f 70 28 70 50 61 72 73  prCachePop(pPars
218e8 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d 65 6c  e, 1);.      }el
218e9 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  se{.        sqli
218ea 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
218eb 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 74 61 72   OP_Null, 0, tar
218ec 67 65 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  get);.      }.  
218ed 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e      assert( db->
218ee 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20  mallocFailed || 
218ef 70 50 61 72 73 65 2d 3e 6e 45 72 72 3e 30 20 0a  pParse->nErr>0 .
218f0 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 50             || pP
218f1 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65  arse->iCacheLeve
218f2 6c 3d 3d 69 43 61 63 68 65 4c 65 76 65 6c 20 29  l==iCacheLevel )
218f3 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
218f4 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
218f5 76 2c 20 65 6e 64 4c 61 62 65 6c 29 3b 0a 20 20  v, endLabel);.  
218f6 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
218f7 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
218f8 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20  OMIT_TRIGGER.   
218f9 20 63 61 73 65 20 54 4b 5f 52 41 49 53 45 3a 20   case TK_RAISE: 
218fa 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
218fb 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 3d  pExpr->affinity=
218fc 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 0a 20 20  =OE_Rollback .  
218fd 20 20 20 20 20 20 20 20 20 7c 7c 20 70 45 78 70           || pExp
218fe 72 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 4f 45 5f  r->affinity==OE_
218ff 41 62 6f 72 74 0a 20 20 20 20 20 20 20 20 20 20  Abort.          
21900 20 7c 7c 20 70 45 78 70 72 2d 3e 61 66 66 69 6e   || pExpr->affin
21901 69 74 79 3d 3d 4f 45 5f 46 61 69 6c 0a 20 20 20  ity==OE_Fail.   
21902 20 20 20 20 20 20 20 20 7c 7c 20 70 45 78 70 72          || pExpr
21903 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 4f 45 5f 49  ->affinity==OE_I
21904 67 6e 6f 72 65 0a 20 20 20 20 20 20 29 3b 0a 20  gnore.      );. 
21905 20 20 20 20 20 69 66 28 20 21 70 50 61 72 73 65       if( !pParse
21906 2d 3e 70 54 72 69 67 67 65 72 54 61 62 20 29 7b  ->pTriggerTab ){
21907 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
21908 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
21909 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2190a 20 20 20 20 20 20 20 20 22 52 41 49 53 45 28 29          "RAISE()
2190b 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 65   may only be use
2190c 64 20 77 69 74 68 69 6e 20 61 20 74 72 69 67 67  d within a trigg
2190d 65 72 2d 70 72 6f 67 72 61 6d 22 29 3b 0a 20 20  er-program");.  
2190e 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
2190f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
21910 28 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74  ( pExpr->affinit
21911 79 3d 3d 4f 45 5f 41 62 6f 72 74 20 29 7b 0a 20  y==OE_Abort ){. 
21912 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 61         sqlite3Ma
21913 79 41 62 6f 72 74 28 70 50 61 72 73 65 29 3b 0a  yAbort(pParse);.
21914 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73        }.      as
21915 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72  sert( !ExprHasPr
21916 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
21917 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20  _IntValue) );.  
21918 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 61      if( pExpr->a
21919 66 66 69 6e 69 74 79 3d 3d 4f 45 5f 49 67 6e 6f  ffinity==OE_Igno
2191a 72 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  re ){.        sq
2191b 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
2191c 0a 20 20 20 20 20 20 20 20 20 20 20 20 76 2c 20  .            v, 
2191d 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54 45 5f  OP_Halt, SQLITE_
2191e 4f 4b 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 30  OK, OE_Ignore, 0
2191f 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65  , pExpr->u.zToke
21920 6e 2c 30 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  n,0);.      }els
21921 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  e{.        sqlit
21922 65 33 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e 74  e3HaltConstraint
21923 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
21924 61 66 66 69 6e 69 74 79 2c 20 70 45 78 70 72 2d  affinity, pExpr-
21925 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 30 29 3b 0a 20  >u.zToken, 0);. 
21926 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 62 72       }..      br
21927 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  eak;.    }.#endi
21928 66 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52  f.  }.  sqlite3R
21929 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
2192a 61 72 73 65 2c 20 72 65 67 46 72 65 65 31 29 3b  arse, regFree1);
2192b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  .  sqlite3Releas
2192c 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
2192d 20 72 65 67 46 72 65 65 32 29 3b 0a 20 20 72 65   regFree2);.  re
2192e 74 75 72 6e 20 69 6e 52 65 67 3b 0a 7d 0a 0a 2f  turn inReg;.}../
2192f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
21930 64 65 20 74 6f 20 65 76 61 6c 75 61 74 65 20 61  de to evaluate a
21931 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64  n expression and
21932 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c   store the resul
21933 74 73 0a 2a 2a 20 69 6e 74 6f 20 61 20 72 65 67  ts.** into a reg
21934 69 73 74 65 72 2e 20 20 52 65 74 75 72 6e 20 74  ister.  Return t
21935 68 65 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62  he register numb
21936 65 72 20 77 68 65 72 65 20 74 68 65 20 72 65 73  er where the res
21937 75 6c 74 73 0a 2a 2a 20 61 72 65 20 73 74 6f 72  ults.** are stor
21938 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ed..**.** If the
21939 20 72 65 67 69 73 74 65 72 20 69 73 20 61 20 74   register is a t
2193a 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 65  emporary registe
2193b 72 20 74 68 61 74 20 63 61 6e 20 62 65 20 64 65  r that can be de
2193c 61 6c 6c 6f 63 61 74 65 64 2c 0a 2a 2a 20 74 68  allocated,.** th
2193d 65 6e 20 77 72 69 74 65 20 69 74 73 20 6e 75 6d  en write its num
2193e 62 65 72 20 69 6e 74 6f 20 2a 70 52 65 67 2e 20  ber into *pReg. 
2193f 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 72   If the result r
21940 65 67 69 73 74 65 72 20 69 73 20 6e 6f 74 0a 2a  egister is not.*
21941 2a 20 61 20 74 65 6d 70 6f 72 61 72 79 2c 20 74  * a temporary, t
21942 68 65 6e 20 73 65 74 20 2a 70 52 65 67 20 74 6f  hen set *pReg to
21943 20 7a 65 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45   zero..*/.SQLITE
21944 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
21945 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70  ite3ExprCodeTemp
21946 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
21947 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74  Expr *pExpr, int
21948 20 2a 70 52 65 67 29 7b 0a 20 20 69 6e 74 20 72   *pReg){.  int r
21949 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  1 = sqlite3GetTe
2194a 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
2194b 20 69 6e 74 20 72 32 20 3d 20 73 71 6c 69 74 65   int r2 = sqlite
2194c 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28  3ExprCodeTarget(
2194d 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 72  pParse, pExpr, r
2194e 31 29 3b 0a 20 20 69 66 28 20 72 32 3d 3d 72 31  1);.  if( r2==r1
2194f 20 29 7b 0a 20 20 20 20 2a 70 52 65 67 20 3d 20   ){.    *pReg = 
21950 72 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  r1;.  }else{.   
21951 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
21952 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
21953 31 29 3b 0a 20 20 20 20 2a 70 52 65 67 20 3d 20  1);.    *pReg = 
21954 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
21955 72 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e  r2;.}../*.** Gen
21956 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20  erate code that 
21957 77 69 6c 6c 20 65 76 61 6c 75 61 74 65 20 65 78  will evaluate ex
21958 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 61  pression pExpr a
21959 6e 64 20 73 74 6f 72 65 20 74 68 65 0a 2a 2a 20  nd store the.** 
2195a 72 65 73 75 6c 74 73 20 69 6e 20 72 65 67 69 73  results in regis
2195b 74 65 72 20 74 61 72 67 65 74 2e 20 20 54 68 65  ter target.  The
2195c 20 72 65 73 75 6c 74 73 20 61 72 65 20 67 75 61   results are gua
2195d 72 61 6e 74 65 65 64 20 74 6f 20 61 70 70 65 61  ranteed to appea
2195e 72 0a 2a 2a 20 69 6e 20 72 65 67 69 73 74 65 72  r.** in register
2195f 20 74 61 72 67 65 74 2e 0a 2a 2f 0a 53 51 4c 49   target..*/.SQLI
21960 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
21961 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 50  qlite3ExprCode(P
21962 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78  arse *pParse, Ex
21963 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 74  pr *pExpr, int t
21964 61 72 67 65 74 29 7b 0a 20 20 69 6e 74 20 69 6e  arget){.  int in
21965 52 65 67 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  Reg;..  assert( 
21966 74 61 72 67 65 74 3e 30 20 26 26 20 74 61 72 67  target>0 && targ
21967 65 74 3c 3d 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  et<=pParse->nMem
21968 20 29 3b 0a 20 20 69 6e 52 65 67 20 3d 20 73 71   );.  inReg = sq
21969 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 72  lite3ExprCodeTar
2196a 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78 70  get(pParse, pExp
2196b 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 61 73  r, target);.  as
2196c 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70 56  sert( pParse->pV
2196d 64 62 65 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64  dbe || pParse->d
2196e 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2196f 29 3b 0a 20 20 69 66 28 20 69 6e 52 65 67 21 3d  );.  if( inReg!=
21970 74 61 72 67 65 74 20 26 26 20 70 50 61 72 73 65  target && pParse
21971 2d 3e 70 56 64 62 65 20 29 7b 0a 20 20 20 20 73  ->pVdbe ){.    s
21972 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
21973 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20  (pParse->pVdbe, 
21974 4f 50 5f 53 43 6f 70 79 2c 20 69 6e 52 65 67 2c  OP_SCopy, inReg,
21975 20 74 61 72 67 65 74 29 3b 0a 20 20 7d 0a 20 20   target);.  }.  
21976 72 65 74 75 72 6e 20 74 61 72 67 65 74 3b 0a 7d  return target;.}
21977 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
21978 20 63 6f 64 65 20 74 68 61 74 20 65 76 61 6c 75   code that evalu
21979 74 65 73 20 74 68 65 20 67 69 76 65 6e 20 65 78  tes the given ex
2197a 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 70 75 74  pression and put
2197b 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20  s the result.** 
2197c 69 6e 20 72 65 67 69 73 74 65 72 20 74 61 72 67  in register targ
2197d 65 74 2e 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 20 6d  et..**.** Also m
2197e 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68  ake a copy of th
2197f 65 20 65 78 70 72 65 73 73 69 6f 6e 20 72 65 73  e expression res
21980 75 6c 74 73 20 69 6e 74 6f 20 61 6e 6f 74 68 65  ults into anothe
21981 72 20 22 63 61 63 68 65 22 20 72 65 67 69 73 74  r "cache" regist
21982 65 72 0a 2a 2a 20 61 6e 64 20 6d 6f 64 69 66 79  er.** and modify
21983 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
21984 73 6f 20 74 68 61 74 20 74 68 65 20 6e 65 78 74  so that the next
21985 20 74 69 6d 65 20 69 74 20 69 73 20 65 76 61 6c   time it is eval
21986 75 61 74 65 64 2c 0a 2a 2a 20 74 68 65 20 72 65  uated,.** the re
21987 73 75 6c 74 20 69 73 20 61 20 63 6f 70 79 20 6f  sult is a copy o
21988 66 20 74 68 65 20 63 61 63 68 65 20 72 65 67 69  f the cache regi
21989 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ster..**.** This
2198a 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64   routine is used
2198b 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f 6e 73   for expressions
2198c 20 74 68 61 74 20 61 72 65 20 75 73 65 64 20 6d   that are used m
2198d 75 6c 74 69 70 6c 65 20 0a 2a 2a 20 74 69 6d 65  ultiple .** time
2198e 73 2e 20 20 54 68 65 79 20 61 72 65 20 65 76 61  s.  They are eva
2198f 6c 75 61 74 65 64 20 6f 6e 63 65 20 61 6e 64 20  luated once and 
21990 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74  the results of t
21991 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a  he expression.**
21992 20 61 72 65 20 72 65 75 73 65 64 2e 0a 2a 2f 0a   are reused..*/.
21993 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
21994 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  nt sqlite3ExprCo
21995 64 65 41 6e 64 43 61 63 68 65 28 50 61 72 73 65  deAndCache(Parse
21996 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a   *pParse, Expr *
21997 70 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67 65  pExpr, int targe
21998 74 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  t){.  Vdbe *v = 
21999 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20  pParse->pVdbe;. 
2199a 20 69 6e 74 20 69 6e 52 65 67 3b 0a 20 20 69 6e   int inReg;.  in
2199b 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70  Reg = sqlite3Exp
2199c 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45  rCode(pParse, pE
2199d 78 70 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20  xpr, target);.  
2199e 61 73 73 65 72 74 28 20 74 61 72 67 65 74 3e 30  assert( target>0
2199f 20 29 3b 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f   );.  /* This ro
219a0 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
219a1 66 6f 72 20 74 65 72 6d 73 20 74 6f 20 49 4e 53  for terms to INS
219a2 45 52 54 20 6f 72 20 55 50 44 41 54 45 2e 20 20  ERT or UPDATE.  
219a3 41 6e 64 20 74 68 65 20 6f 6e 6c 79 0a 20 20 2a  And the only.  *
219a4 2a 20 6f 74 68 65 72 20 70 6c 61 63 65 20 77 68  * other place wh
219a5 65 72 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20  ere expressions 
219a6 63 61 6e 20 62 65 20 63 6f 6e 76 65 72 74 65 64  can be converted
219a7 20 69 6e 74 6f 20 54 4b 5f 52 45 47 49 53 54 45   into TK_REGISTE
219a8 52 20 69 73 0a 20 20 2a 2a 20 69 6e 20 57 48 45  R is.  ** in WHE
219a9 52 45 20 63 6c 61 75 73 65 20 70 72 6f 63 65 73  RE clause proces
219aa 73 69 6e 67 2e 20 20 53 6f 20 61 73 20 63 75 72  sing.  So as cur
219ab 72 65 6e 74 6c 79 20 69 6d 70 6c 65 6d 65 6e 74  rently implement
219ac 65 64 2c 20 74 68 65 72 65 20 69 73 0a 20 20 2a  ed, there is.  *
219ad 2a 20 6e 6f 20 77 61 79 20 66 6f 72 20 61 20 54  * no way for a T
219ae 4b 5f 52 45 47 49 53 54 45 52 20 74 6f 20 65 78  K_REGISTER to ex
219af 69 73 74 20 68 65 72 65 2e 20 20 42 75 74 20 69  ist here.  But i
219b0 74 20 73 65 65 6d 73 20 70 72 75 64 65 6e 74 20  t seems prudent 
219b1 74 6f 0a 20 20 2a 2a 20 6b 65 65 70 20 74 68 65  to.  ** keep the
219b2 20 41 4c 57 41 59 53 28 29 20 69 6e 20 63 61 73   ALWAYS() in cas
219b3 65 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 73  e the conditions
219b4 20 61 62 6f 76 65 20 63 68 61 6e 67 65 20 77 69   above change wi
219b5 74 68 20 66 75 74 75 72 65 0a 20 20 2a 2a 20 6d  th future.  ** m
219b6 6f 64 69 66 69 63 61 74 69 6f 6e 73 20 6f 72 20  odifications or 
219b7 65 6e 68 61 6e 63 65 6d 65 6e 74 73 2e 20 2a 2f  enhancements. */
219b8 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 45  .  if( ALWAYS(pE
219b9 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 52 45 47 49  xpr->op!=TK_REGI
219ba 53 54 45 52 29 20 29 7b 20 20 0a 20 20 20 20 69  STER) ){  .    i
219bb 6e 74 20 69 4d 65 6d 3b 0a 20 20 20 20 69 4d 65  nt iMem;.    iMe
219bc 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  m = ++pParse->nM
219bd 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  em;.    sqlite3V
219be 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
219bf 43 6f 70 79 2c 20 69 6e 52 65 67 2c 20 69 4d 65  Copy, inReg, iMe
219c0 6d 29 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 69  m);.    pExpr->i
219c1 54 61 62 6c 65 20 3d 20 69 4d 65 6d 3b 0a 20 20  Table = iMem;.  
219c2 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b    pExpr->op = TK
219c3 5f 52 45 47 49 53 54 45 52 3b 0a 20 20 7d 0a 20  _REGISTER;.  }. 
219c4 20 72 65 74 75 72 6e 20 69 6e 52 65 67 3b 0a 7d   return inReg;.}
219c5 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54  ../*.** Return T
219c6 52 55 45 20 69 66 20 70 45 78 70 72 20 69 73 20  RUE if pExpr is 
219c7 61 6e 20 63 6f 6e 73 74 61 6e 74 20 65 78 70 72  an constant expr
219c8 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20 61  ession that is a
219c9 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20 66 6f  ppropriate.** fo
219ca 72 20 66 61 63 74 6f 72 69 6e 67 20 6f 75 74 20  r factoring out 
219cb 6f 66 20 61 20 6c 6f 6f 70 2e 20 20 41 70 70 72  of a loop.  Appr
219cc 6f 70 72 69 61 74 65 20 65 78 70 72 65 73 73 69  opriate expressi
219cd 6f 6e 73 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20  ons are:.**.**  
219ce 20 20 2a 20 20 41 6e 79 20 65 78 70 72 65 73 73    *  Any express
219cf 69 6f 6e 20 74 68 61 74 20 65 76 61 6c 75 61 74  ion that evaluat
219d0 65 73 20 74 6f 20 74 77 6f 20 6f 72 20 6d 6f 72  es to two or mor
219d1 65 20 6f 70 63 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a  e opcodes..**.**
219d2 20 20 20 20 2a 20 20 41 6e 79 20 4f 50 5f 49 6e      *  Any OP_In
219d3 74 65 67 65 72 2c 20 4f 50 5f 52 65 61 6c 2c 20  teger, OP_Real, 
219d4 4f 50 5f 53 74 72 69 6e 67 2c 20 4f 50 5f 42 6c  OP_String, OP_Bl
219d5 6f 62 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 0a 2a 2a  ob, OP_Null, .**
219d6 20 20 20 20 20 20 20 6f 72 20 4f 50 5f 56 61 72         or OP_Var
219d7 69 61 62 6c 65 20 74 68 61 74 20 64 6f 65 73 20  iable that does 
219d8 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 70  not need to be p
219d9 6c 61 63 65 64 20 69 6e 20 61 20 0a 2a 2a 20 20  laced in a .**  
219da 20 20 20 20 20 73 70 65 63 69 66 69 63 20 72 65       specific re
219db 67 69 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  gister..**.** Th
219dc 65 72 65 20 69 73 20 6e 6f 20 70 6f 69 6e 74 20  ere is no point 
219dd 69 6e 20 66 61 63 74 6f 72 69 6e 67 20 6f 75 74  in factoring out
219de 20 73 69 6e 67 6c 65 2d 69 6e 73 74 72 75 63 74   single-instruct
219df 69 6f 6e 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20  ion constant.** 
219e0 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68 61 74  expressions that
219e1 20 6e 65 65 64 20 74 6f 20 62 65 20 70 6c 61 63   need to be plac
219e2 65 64 20 69 6e 20 61 20 70 61 72 74 69 63 75 6c  ed in a particul
219e3 61 72 20 72 65 67 69 73 74 65 72 2e 20 20 0a 2a  ar register.  .*
219e4 2a 20 57 65 20 63 6f 75 6c 64 20 66 61 63 74 6f  * We could facto
219e5 72 20 74 68 65 6d 20 6f 75 74 2c 20 62 75 74 20  r them out, but 
219e6 74 68 65 6e 20 77 65 20 77 6f 75 6c 64 20 65 6e  then we would en
219e7 64 20 75 70 20 61 64 64 69 6e 67 20 61 6e 0a 2a  d up adding an.*
219e8 2a 20 4f 50 5f 53 43 6f 70 79 20 69 6e 73 74 72  * OP_SCopy instr
219e9 75 63 74 69 6f 6e 20 74 6f 20 6d 6f 76 65 20 74  uction to move t
219ea 68 65 20 76 61 6c 75 65 20 69 6e 74 6f 20 74 68  he value into th
219eb 65 20 63 6f 72 72 65 63 74 20 72 65 67 69 73 74  e correct regist
219ec 65 72 0a 2a 2a 20 6c 61 74 65 72 2e 20 20 57 65  er.** later.  We
219ed 20 6d 69 67 68 74 20 61 73 20 77 65 6c 6c 20 6a   might as well j
219ee 75 73 74 20 75 73 65 20 74 68 65 20 6f 72 69 67  ust use the orig
219ef 69 6e 61 6c 20 69 6e 73 74 72 75 63 74 69 6f 6e  inal instruction
219f0 20 61 6e 64 0a 2a 2a 20 61 76 6f 69 64 20 74 68   and.** avoid th
219f1 65 20 4f 50 5f 53 43 6f 70 79 2e 0a 2a 2f 0a 73  e OP_SCopy..*/.s
219f2 74 61 74 69 63 20 69 6e 74 20 69 73 41 70 70 72  tatic int isAppr
219f3 6f 70 72 69 61 74 65 46 6f 72 46 61 63 74 6f 72  opriateForFactor
219f4 69 6e 67 28 45 78 70 72 20 2a 70 29 7b 0a 20 20  ing(Expr *p){.  
219f5 69 66 28 20 21 73 71 6c 69 74 65 33 45 78 70 72  if( !sqlite3Expr
219f6 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69  IsConstantNotJoi
219f7 6e 28 70 29 20 29 7b 0a 20 20 20 20 72 65 74 75  n(p) ){.    retu
219f8 72 6e 20 30 3b 20 20 2f 2a 20 4f 6e 6c 79 20 63  rn 0;  /* Only c
219f9 6f 6e 73 74 61 6e 74 20 65 78 70 72 65 73 73 69  onstant expressi
219fa 6f 6e 73 20 61 72 65 20 61 70 70 72 6f 70 72 69  ons are appropri
219fb 61 74 65 20 66 6f 72 20 66 61 63 74 6f 72 69 6e  ate for factorin
219fc 67 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 28  g */.  }.  if( (
219fd 70 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 46 69  p->flags & EP_Fi
219fe 78 65 64 44 65 73 74 29 3d 3d 30 20 29 7b 0a 20  xedDest)==0 ){. 
219ff 20 20 20 72 65 74 75 72 6e 20 31 3b 20 20 2f 2a     return 1;  /*
21a00 20 41 6e 79 20 63 6f 6e 73 74 61 6e 74 20 77 69   Any constant wi
21a01 74 68 6f 75 74 20 61 20 66 69 78 65 64 20 64 65  thout a fixed de
21a02 73 74 69 6e 61 74 69 6f 6e 20 69 73 20 61 70 70  stination is app
21a03 72 6f 70 72 69 61 74 65 20 2a 2f 0a 20 20 7d 0a  ropriate */.  }.
21a04 20 20 77 68 69 6c 65 28 20 70 2d 3e 6f 70 3d 3d    while( p->op==
21a05 54 4b 5f 55 50 4c 55 53 20 29 20 70 20 3d 20 70  TK_UPLUS ) p = p
21a06 2d 3e 70 4c 65 66 74 3b 0a 20 20 73 77 69 74 63  ->pLeft;.  switc
21a07 68 28 20 70 2d 3e 6f 70 20 29 7b 0a 23 69 66 6e  h( p->op ){.#ifn
21a08 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
21a09 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 0a 20 20 20  BLOB_LITERAL.   
21a0a 20 63 61 73 65 20 54 4b 5f 42 4c 4f 42 3a 0a 23   case TK_BLOB:.#
21a0b 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 54  endif.    case T
21a0c 4b 5f 56 41 52 49 41 42 4c 45 3a 0a 20 20 20 20  K_VARIABLE:.    
21a0d 63 61 73 65 20 54 4b 5f 49 4e 54 45 47 45 52 3a  case TK_INTEGER:
21a0e 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 4c 4f  .    case TK_FLO
21a0f 41 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  AT:.    case TK_
21a10 4e 55 4c 4c 3a 0a 20 20 20 20 63 61 73 65 20 54  NULL:.    case T
21a11 4b 5f 53 54 52 49 4e 47 3a 20 7b 0a 20 20 20 20  K_STRING: {.    
21a12 20 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e 6f    testcase( p->o
21a13 70 3d 3d 54 4b 5f 42 4c 4f 42 20 29 3b 0a 20 20  p==TK_BLOB );.  
21a14 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 2d      testcase( p-
21a15 3e 6f 70 3d 3d 54 4b 5f 56 41 52 49 41 42 4c 45  >op==TK_VARIABLE
21a16 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
21a17 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e  se( p->op==TK_IN
21a18 54 45 47 45 52 20 29 3b 0a 20 20 20 20 20 20 74  TEGER );.      t
21a19 65 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d  estcase( p->op==
21a1a 54 4b 5f 46 4c 4f 41 54 20 29 3b 0a 20 20 20 20  TK_FLOAT );.    
21a1b 20 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e 6f    testcase( p->o
21a1c 70 3d 3d 54 4b 5f 4e 55 4c 4c 20 29 3b 0a 20 20  p==TK_NULL );.  
21a1d 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 2d      testcase( p-
21a1e 3e 6f 70 3d 3d 54 4b 5f 53 54 52 49 4e 47 20 29  >op==TK_STRING )
21a1f 3b 0a 20 20 20 20 20 20 2f 2a 20 53 69 6e 67 6c  ;.      /* Singl
21a20 65 2d 69 6e 73 74 72 75 63 74 69 6f 6e 20 63 6f  e-instruction co
21a21 6e 73 74 61 6e 74 73 20 77 69 74 68 20 61 20 66  nstants with a f
21a22 69 78 65 64 20 64 65 73 74 69 6e 61 74 69 6f 6e  ixed destination
21a23 20 61 72 65 0a 20 20 20 20 20 20 2a 2a 20 62 65   are.      ** be
21a24 74 74 65 72 20 64 6f 6e 65 20 69 6e 2d 6c 69 6e  tter done in-lin
21a25 65 2e 20 20 49 66 20 77 65 20 66 61 63 74 6f 72  e.  If we factor
21a26 20 74 68 65 6d 2c 20 74 68 65 79 20 77 69 6c 6c   them, they will
21a27 20 6a 75 73 74 20 65 6e 64 0a 20 20 20 20 20 20   just end.      
21a28 2a 2a 20 75 70 20 67 65 6e 65 72 61 74 69 6e 67  ** up generating
21a29 20 61 6e 20 4f 50 5f 53 43 6f 70 79 20 74 6f 20   an OP_SCopy to 
21a2a 6d 6f 76 65 20 74 68 65 20 76 61 6c 75 65 20 74  move the value t
21a2b 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  o the destinatio
21a2c 6e 0a 20 20 20 20 20 20 2a 2a 20 72 65 67 69 73  n.      ** regis
21a2d 74 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 72 65  ter. */.      re
21a2e 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20  turn 0;.    }.  
21a2f 20 20 63 61 73 65 20 54 4b 5f 55 4d 49 4e 55 53    case TK_UMINUS
21a30 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 70 2d  : {.      if( p-
21a31 3e 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 46  >pLeft->op==TK_F
21a32 4c 4f 41 54 20 7c 7c 20 70 2d 3e 70 4c 65 66 74  LOAT || p->pLeft
21a33 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52  ->op==TK_INTEGER
21a34 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
21a35 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 0;.      }.  
21a36 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
21a37 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a  .    default: {.
21a38 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
21a39 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
21a3a 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70  1;.}../*.** If p
21a3b 45 78 70 72 20 69 73 20 61 20 63 6f 6e 73 74 61  Expr is a consta
21a3c 6e 74 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68  nt expression th
21a3d 61 74 20 69 73 20 61 70 70 72 6f 70 72 69 61 74  at is appropriat
21a3e 65 20 66 6f 72 0a 2a 2a 20 66 61 63 74 6f 72 69  e for.** factori
21a3f 6e 67 20 6f 75 74 20 6f 66 20 61 20 6c 6f 6f 70  ng out of a loop
21a40 2c 20 74 68 65 6e 20 65 76 61 6c 75 61 74 65 20  , then evaluate 
21a41 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a  the expression.*
21a42 2a 20 69 6e 74 6f 20 61 20 72 65 67 69 73 74 65  * into a registe
21a43 72 20 61 6e 64 20 63 6f 6e 76 65 72 74 20 74 68  r and convert th
21a44 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 74  e expression int
21a45 6f 20 61 20 54 4b 5f 52 45 47 49 53 54 45 52 0a  o a TK_REGISTER.
21a46 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a  ** expression..*
21a47 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 65 76 61  /.static int eva
21a48 6c 43 6f 6e 73 74 45 78 70 72 28 57 61 6c 6b 65  lConstExpr(Walke
21a49 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72  r *pWalker, Expr
21a4a 20 2a 70 45 78 70 72 29 7b 0a 20 20 50 61 72 73   *pExpr){.  Pars
21a4b 65 20 2a 70 50 61 72 73 65 20 3d 20 70 57 61 6c  e *pParse = pWal
21a4c 6b 65 72 2d 3e 70 50 61 72 73 65 3b 0a 20 20 73  ker->pParse;.  s
21a4d 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70  witch( pExpr->op
21a4e 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f   ){.    case TK_
21a4f 52 45 47 49 53 54 45 52 3a 20 7b 0a 20 20 20 20  REGISTER: {.    
21a50 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75    return WRC_Pru
21a51 6e 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ne;.    }.    ca
21a52 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a 0a  se TK_FUNCTION:.
21a53 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f      case TK_AGG_
21a54 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20 20 63 61  FUNCTION:.    ca
21a55 73 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43  se TK_CONST_FUNC
21a56 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65  : {.      /* The
21a57 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 61 20   arguments to a 
21a58 66 75 6e 63 74 69 6f 6e 20 68 61 76 65 20 61 20  function have a 
21a59 66 69 78 65 64 20 64 65 73 74 69 6e 61 74 69 6f  fixed destinatio
21a5a 6e 2e 0a 20 20 20 20 20 20 2a 2a 20 4d 61 72 6b  n..      ** Mark
21a5b 20 74 68 65 6d 20 74 68 69 73 20 77 61 79 20 74   them this way t
21a5c 6f 20 61 76 6f 69 64 20 67 65 6e 65 72 61 74 65  o avoid generate
21a5d 64 20 75 6e 6e 65 65 64 65 64 20 4f 50 5f 53 43  d unneeded OP_SC
21a5e 6f 70 79 0a 20 20 20 20 20 20 2a 2a 20 69 6e 73  opy.      ** ins
21a5f 74 72 75 63 74 69 6f 6e 73 2e 20 0a 20 20 20 20  tructions. .    
21a60 20 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 4c    */.      ExprL
21a61 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45 78  ist *pList = pEx
21a62 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 20  pr->x.pList;.   
21a63 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72     assert( !Expr
21a64 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
21a65 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  r, EP_xIsSelect)
21a66 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4c   );.      if( pL
21a67 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ist ){.        i
21a68 6e 74 20 69 20 3d 20 70 4c 69 73 74 2d 3e 6e 45  nt i = pList->nE
21a69 78 70 72 3b 0a 20 20 20 20 20 20 20 20 73 74 72  xpr;.        str
21a6a 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
21a6b 6d 20 2a 70 49 74 65 6d 20 3d 20 70 4c 69 73 74  m *pItem = pList
21a6c 2d 3e 61 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  ->a;.        for
21a6d 28 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74  (; i>0; i--, pIt
21a6e 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20  em++){.         
21a6f 20 69 66 28 20 41 4c 57 41 59 53 28 70 49 74 65   if( ALWAYS(pIte
21a70 6d 2d 3e 70 45 78 70 72 29 20 29 20 70 49 74 65  m->pExpr) ) pIte
21a71 6d 2d 3e 70 45 78 70 72 2d 3e 66 6c 61 67 73 20  m->pExpr->flags 
21a72 7c 3d 20 45 50 5f 46 69 78 65 64 44 65 73 74 3b  |= EP_FixedDest;
21a73 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
21a74 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
21a75 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
21a76 69 73 41 70 70 72 6f 70 72 69 61 74 65 46 6f 72  isAppropriateFor
21a77 46 61 63 74 6f 72 69 6e 67 28 70 45 78 70 72 29  Factoring(pExpr)
21a78 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 31 20 3d   ){.    int r1 =
21a79 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
21a7a 0a 20 20 20 20 69 6e 74 20 72 32 3b 0a 20 20 20  .    int r2;.   
21a7b 20 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70   r2 = sqlite3Exp
21a7c 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 72  rCodeTarget(pPar
21a7d 73 65 2c 20 70 45 78 70 72 2c 20 72 31 29 3b 0a  se, pExpr, r1);.
21a7e 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 72 31      if( NEVER(r1
21a7f 21 3d 72 32 29 20 29 20 73 71 6c 69 74 65 33 52  !=r2) ) sqlite3R
21a80 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
21a81 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 70  arse, r1);.    p
21a82 45 78 70 72 2d 3e 6f 70 32 20 3d 20 70 45 78 70  Expr->op2 = pExp
21a83 72 2d 3e 6f 70 3b 0a 20 20 20 20 70 45 78 70 72  r->op;.    pExpr
21a84 2d 3e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 53 54  ->op = TK_REGIST
21a85 45 52 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 69  ER;.    pExpr->i
21a86 54 61 62 6c 65 20 3d 20 72 32 3b 0a 20 20 20 20  Table = r2;.    
21a87 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65  return WRC_Prune
21a88 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 57  ;.  }.  return W
21a89 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a  RC_Continue;.}..
21a8a 2f 2a 0a 2a 2a 20 50 72 65 65 76 61 6c 75 61 74  /*.** Preevaluat
21a8b 65 20 63 6f 6e 73 74 61 6e 74 20 73 75 62 65 78  e constant subex
21a8c 70 72 65 73 73 69 6f 6e 73 20 77 69 74 68 69 6e  pressions within
21a8d 20 70 45 78 70 72 20 61 6e 64 20 73 74 6f 72 65   pExpr and store
21a8e 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 73 20   the.** results 
21a8f 69 6e 20 72 65 67 69 73 74 65 72 73 2e 20 20 4d  in registers.  M
21a90 6f 64 69 66 79 20 70 45 78 70 72 20 73 6f 20 74  odify pExpr so t
21a91 68 61 74 20 74 68 65 20 63 6f 6e 73 74 61 6e 74  hat the constant
21a92 20 73 75 62 65 78 70 72 65 73 69 6f 6e 73 0a 2a   subexpresions.*
21a93 2a 20 61 72 65 20 54 4b 5f 52 45 47 49 53 54 45  * are TK_REGISTE
21a94 52 20 6f 70 63 6f 64 65 73 20 74 68 61 74 20 72  R opcodes that r
21a95 65 66 65 72 20 74 6f 20 74 68 65 20 70 72 65 63  efer to the prec
21a96 6f 6d 70 75 74 65 64 20 76 61 6c 75 65 73 2e 0a  omputed values..
21a97 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
21a98 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
21a99 70 72 43 6f 64 65 43 6f 6e 73 74 61 6e 74 73 28  prCodeConstants(
21a9a 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45  Parse *pParse, E
21a9b 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 57  xpr *pExpr){.  W
21a9c 61 6c 6b 65 72 20 77 3b 0a 20 20 77 2e 78 45 78  alker w;.  w.xEx
21a9d 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 65 76 61  prCallback = eva
21a9e 6c 43 6f 6e 73 74 45 78 70 72 3b 0a 20 20 77 2e  lConstExpr;.  w.
21a9f 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20  xSelectCallback 
21aa0 3d 20 30 3b 0a 20 20 77 2e 70 50 61 72 73 65 20  = 0;.  w.pParse 
21aa1 3d 20 70 50 61 72 73 65 3b 0a 20 20 73 71 6c 69  = pParse;.  sqli
21aa2 74 65 33 57 61 6c 6b 45 78 70 72 28 26 77 2c 20  te3WalkExpr(&w, 
21aa3 70 45 78 70 72 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  pExpr);.}.../*.*
21aa4 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
21aa5 74 68 61 74 20 70 75 73 68 65 73 20 74 68 65 20  that pushes the 
21aa6 76 61 6c 75 65 20 6f 66 20 65 76 65 72 79 20 65  value of every e
21aa7 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 67 69  lement of the gi
21aa8 76 65 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f  ven.** expressio
21aa9 6e 20 6c 69 73 74 20 69 6e 74 6f 20 61 20 73 65  n list into a se
21aaa 71 75 65 6e 63 65 20 6f 66 20 72 65 67 69 73 74  quence of regist
21aab 65 72 73 20 62 65 67 69 6e 6e 69 6e 67 20 61 74  ers beginning at
21aac 20 74 61 72 67 65 74 2e 0a 2a 2a 0a 2a 2a 20 52   target..**.** R
21aad 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
21aae 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 65 76 61   of elements eva
21aaf 6c 75 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  luated..*/.SQLIT
21ab0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
21ab1 6c 69 74 65 33 45 78 70 72 43 6f 64 65 45 78 70  lite3ExprCodeExp
21ab2 72 4c 69 73 74 28 0a 20 20 50 61 72 73 65 20 2a  rList(.  Parse *
21ab3 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 50  pParse,     /* P
21ab4 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
21ab5 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c  /.  ExprList *pL
21ab6 69 73 74 2c 20 20 20 2f 2a 20 54 68 65 20 65 78  ist,   /* The ex
21ab7 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 74 6f  pression list to
21ab8 20 62 65 20 63 6f 64 65 64 20 2a 2f 0a 20 20 69   be coded */.  i
21ab9 6e 74 20 74 61 72 67 65 74 2c 20 20 20 20 20 20  nt target,      
21aba 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 77 72    /* Where to wr
21abb 69 74 65 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20  ite results */. 
21abc 20 69 6e 74 20 64 6f 48 61 72 64 43 6f 70 79 20   int doHardCopy 
21abd 20 20 20 20 2f 2a 20 4d 61 6b 65 20 61 20 68 61      /* Make a ha
21abe 72 64 20 63 6f 70 79 20 6f 66 20 65 76 65 72 79  rd copy of every
21abf 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20   element */.){. 
21ac0 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74   struct ExprList
21ac1 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20  _item *pItem;.  
21ac2 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 61 73 73 65  int i, n;.  asse
21ac3 72 74 28 20 70 4c 69 73 74 21 3d 30 20 29 3b 0a  rt( pList!=0 );.
21ac4 20 20 61 73 73 65 72 74 28 20 74 61 72 67 65 74    assert( target
21ac5 3e 30 20 29 3b 0a 20 20 6e 20 3d 20 70 4c 69 73  >0 );.  n = pLis
21ac6 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 66 6f 72 28  t->nExpr;.  for(
21ac7 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20  pItem=pList->a, 
21ac8 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 2c 20 70  i=0; i<n; i++, p
21ac9 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28  Item++){.    if(
21aca 20 70 49 74 65 6d 2d 3e 69 41 6c 69 61 73 20 29   pItem->iAlias )
21acb 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 52 65 67  {.      int iReg
21acc 20 3d 20 63 6f 64 65 41 6c 69 61 73 28 70 50 61   = codeAlias(pPa
21acd 72 73 65 2c 20 70 49 74 65 6d 2d 3e 69 41 6c 69  rse, pItem->iAli
21ace 61 73 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72  as, pItem->pExpr
21acf 2c 20 74 61 72 67 65 74 2b 69 29 3b 0a 20 20 20  , target+i);.   
21ad0 20 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c     Vdbe *v = sql
21ad1 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
21ad2 73 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69  se);.      if( i
21ad3 52 65 67 21 3d 74 61 72 67 65 74 2b 69 20 29 7b  Reg!=target+i ){
21ad4 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
21ad5 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
21ad6 5f 53 43 6f 70 79 2c 20 69 52 65 67 2c 20 74 61  _SCopy, iReg, ta
21ad7 72 67 65 74 2b 69 29 3b 0a 20 20 20 20 20 20 7d  rget+i);.      }
21ad8 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
21ad9 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
21ada 65 28 70 50 61 72 73 65 2c 20 70 49 74 65 6d 2d  e(pParse, pItem-
21adb 3e 70 45 78 70 72 2c 20 74 61 72 67 65 74 2b 69  >pExpr, target+i
21adc 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
21add 20 64 6f 48 61 72 64 43 6f 70 79 20 26 26 20 21   doHardCopy && !
21ade 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c  pParse->db->mall
21adf 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
21ae0 20 20 73 71 6c 69 74 65 33 45 78 70 72 48 61 72    sqlite3ExprHar
21ae1 64 43 6f 70 79 28 70 50 61 72 73 65 2c 20 74 61  dCopy(pParse, ta
21ae2 72 67 65 74 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a  rget, n);.    }.
21ae3 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a    }.  return n;.
21ae4 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
21ae5 65 20 63 6f 64 65 20 66 6f 72 20 61 20 62 6f 6f  e code for a boo
21ae6 6c 65 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  lean expression 
21ae7 73 75 63 68 20 74 68 61 74 20 61 20 6a 75 6d 70  such that a jump
21ae8 20 69 73 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 74   is made.** to t
21ae9 68 65 20 6c 61 62 65 6c 20 22 64 65 73 74 22 20  he label "dest" 
21aea 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  if the expressio
21aeb 6e 20 69 73 20 74 72 75 65 20 62 75 74 20 65 78  n is true but ex
21aec 65 63 75 74 69 6f 6e 0a 2a 2a 20 63 6f 6e 74 69  ecution.** conti
21aed 6e 75 65 73 20 73 74 72 61 69 67 68 74 20 74 68  nues straight th
21aee 72 75 20 69 66 20 74 68 65 20 65 78 70 72 65 73  ru if the expres
21aef 73 69 6f 6e 20 69 73 20 66 61 6c 73 65 2e 0a 2a  sion is false..*
21af0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72  *.** If the expr
21af1 65 73 73 69 6f 6e 20 65 76 61 6c 75 61 74 65 73  ession evaluates
21af2 20 74 6f 20 4e 55 4c 4c 20 28 6e 65 69 74 68 65   to NULL (neithe
21af3 72 20 74 72 75 65 20 6e 6f 72 20 66 61 6c 73 65  r true nor false
21af4 29 2c 20 74 68 65 6e 0a 2a 2a 20 74 61 6b 65 20  ), then.** take 
21af5 74 68 65 20 6a 75 6d 70 20 69 66 20 74 68 65 20  the jump if the 
21af6 6a 75 6d 70 49 66 4e 75 6c 6c 20 66 6c 61 67 20  jumpIfNull flag 
21af7 69 73 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46  is SQLITE_JUMPIF
21af8 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  NULL..**.** This
21af9 20 63 6f 64 65 20 64 65 70 65 6e 64 73 20 6f 6e   code depends on
21afa 20 74 68 65 20 66 61 63 74 20 74 68 61 74 20 63   the fact that c
21afb 65 72 74 61 69 6e 20 74 6f 6b 65 6e 20 76 61 6c  ertain token val
21afc 75 65 73 20 28 65 78 3a 20 54 4b 5f 45 51 29 0a  ues (ex: TK_EQ).
21afd 2a 2a 20 61 72 65 20 74 68 65 20 73 61 6d 65 20  ** are the same 
21afe 61 73 20 6f 70 63 6f 64 65 20 76 61 6c 75 65 73  as opcode values
21aff 20 28 65 78 3a 20 4f 50 5f 45 71 29 20 74 68 61   (ex: OP_Eq) tha
21b00 74 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  t implement the 
21b01 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a  corresponding.**
21b02 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 53 70 65   operation.  Spe
21b03 63 69 61 6c 20 63 6f 6d 6d 65 6e 74 73 20 69 6e  cial comments in
21b04 20 76 64 62 65 2e 63 20 61 6e 64 20 74 68 65 20   vdbe.c and the 
21b05 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 6b 20 73 63  mkopcodeh.awk sc
21b06 72 69 70 74 20 69 6e 0a 2a 2a 20 74 68 65 20 6d  ript in.** the m
21b07 61 6b 65 20 70 72 6f 63 65 73 73 20 63 61 75 73  ake process caus
21b08 65 20 74 68 65 73 65 20 76 61 6c 75 65 73 20 74  e these values t
21b09 6f 20 61 6c 69 67 6e 2e 20 20 41 73 73 65 72 74  o align.  Assert
21b0a 28 29 73 20 69 6e 20 74 68 65 20 63 6f 64 65 0a  ()s in the code.
21b0b 2a 2a 20 62 65 6c 6f 77 20 76 65 72 69 66 79 20  ** below verify 
21b0c 74 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72 73  that the numbers
21b0d 20 61 72 65 20 61 6c 69 67 6e 65 64 20 63 6f 72   are aligned cor
21b0e 72 65 63 74 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54  rectly..*/.SQLIT
21b0f 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
21b10 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65  qlite3ExprIfTrue
21b11 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
21b12 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74  Expr *pExpr, int
21b13 20 64 65 73 74 2c 20 69 6e 74 20 6a 75 6d 70 49   dest, int jumpI
21b14 66 4e 75 6c 6c 29 7b 0a 20 20 56 64 62 65 20 2a  fNull){.  Vdbe *
21b15 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
21b16 65 3b 0a 20 20 69 6e 74 20 6f 70 20 3d 20 30 3b  e;.  int op = 0;
21b17 0a 20 20 69 6e 74 20 72 65 67 46 72 65 65 31 20  .  int regFree1 
21b18 3d 20 30 3b 0a 20 20 69 6e 74 20 72 65 67 46 72  = 0;.  int regFr
21b19 65 65 32 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72  ee2 = 0;.  int r
21b1a 31 2c 20 72 32 3b 0a 0a 20 20 61 73 73 65 72 74  1, r2;..  assert
21b1b 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 53 51  ( jumpIfNull==SQ
21b1c 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20  LITE_JUMPIFNULL 
21b1d 7c 7c 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30  || jumpIfNull==0
21b1e 20 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28   );.  if( NEVER(
21b1f 76 3d 3d 30 29 20 29 20 20 20 20 20 72 65 74 75  v==0) )     retu
21b20 72 6e 3b 20 20 2f 2a 20 45 78 69 73 74 61 6e 63  rn;  /* Existanc
21b21 65 20 6f 66 20 56 44 42 45 20 63 68 65 63 6b 65  e of VDBE checke
21b22 64 20 62 79 20 63 61 6c 6c 65 72 20 2a 2f 0a 20  d by caller */. 
21b23 20 69 66 28 20 4e 45 56 45 52 28 70 45 78 70 72   if( NEVER(pExpr
21b24 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 3b 20 20  ==0) ) return;  
21b25 2f 2a 20 4e 6f 20 77 61 79 20 74 68 69 73 20 63  /* No way this c
21b26 61 6e 20 68 61 70 70 65 6e 20 2a 2f 0a 20 20 6f  an happen */.  o
21b27 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20  p = pExpr->op;. 
21b28 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20   switch( op ){. 
21b29 20 20 20 63 61 73 65 20 54 4b 5f 41 4e 44 3a 20     case TK_AND: 
21b2a 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 32 20 3d  {.      int d2 =
21b2b 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
21b2c 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20  Label(v);.      
21b2d 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66  testcase( jumpIf
21b2e 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20  Null==0 );.     
21b2f 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
21b30 65 50 75 73 68 28 70 50 61 72 73 65 29 3b 0a 20  ePush(pParse);. 
21b31 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
21b32 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20  IfFalse(pParse, 
21b33 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64 32  pExpr->pLeft, d2
21b34 2c 6a 75 6d 70 49 66 4e 75 6c 6c 5e 53 51 4c 49  ,jumpIfNull^SQLI
21b35 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a  TE_JUMPIFNULL);.
21b36 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
21b37 72 49 66 54 72 75 65 28 70 50 61 72 73 65 2c 20  rIfTrue(pParse, 
21b38 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 64  pExpr->pRight, d
21b39 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29  est, jumpIfNull)
21b3a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
21b3b 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
21b3c 76 2c 20 64 32 29 3b 0a 20 20 20 20 20 20 73 71  v, d2);.      sq
21b3d 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 6f  lite3ExprCachePo
21b3e 70 28 70 50 61 72 73 65 2c 20 31 29 3b 0a 20 20  p(pParse, 1);.  
21b3f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
21b40 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4f 52 3a  .    case TK_OR:
21b41 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73   {.      testcas
21b42 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30  e( jumpIfNull==0
21b43 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
21b44 33 45 78 70 72 49 66 54 72 75 65 28 70 50 61 72  3ExprIfTrue(pPar
21b45 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  se, pExpr->pLeft
21b46 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75  , dest, jumpIfNu
21b47 6c 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ll);.      sqlit
21b48 65 33 45 78 70 72 49 66 54 72 75 65 28 70 50 61  e3ExprIfTrue(pPa
21b49 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67  rse, pExpr->pRig
21b4a 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66  ht, dest, jumpIf
21b4b 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65  Null);.      bre
21b4c 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
21b4d 73 65 20 54 4b 5f 4e 4f 54 3a 20 7b 0a 20 20 20  se TK_NOT: {.   
21b4e 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d     testcase( jum
21b4f 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20  pIfNull==0 );.  
21b50 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49      sqlite3ExprI
21b51 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70  fFalse(pParse, p
21b52 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64 65 73  Expr->pLeft, des
21b53 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a  t, jumpIfNull);.
21b54 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
21b55 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c   }.    case TK_L
21b56 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c  T:.    case TK_L
21b57 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47  E:.    case TK_G
21b58 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47  T:.    case TK_G
21b59 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e  E:.    case TK_N
21b5a 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45  E:.    case TK_E
21b5b 51 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72  Q: {.      asser
21b5c 74 28 20 54 4b 5f 4c 54 3d 3d 4f 50 5f 4c 74 20  t( TK_LT==OP_Lt 
21b5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
21b5e 20 54 4b 5f 4c 45 3d 3d 4f 50 5f 4c 65 20 29 3b   TK_LE==OP_Le );
21b5f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
21b60 4b 5f 47 54 3d 3d 4f 50 5f 47 74 20 29 3b 0a 20  K_GT==OP_Gt );. 
21b61 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
21b62 47 45 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20 20 20  GE==OP_Ge );.   
21b63 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 45 51     assert( TK_EQ
21b64 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 20 20 20  ==OP_Eq );.     
21b65 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 45 3d 3d   assert( TK_NE==
21b66 4f 50 5f 4e 65 20 29 3b 0a 20 20 20 20 20 20 74  OP_Ne );.      t
21b67 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
21b68 4c 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  LT );.      test
21b69 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 45 20  case( op==TK_LE 
21b6a 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
21b6b 65 28 20 6f 70 3d 3d 54 4b 5f 47 54 20 29 3b 0a  e( op==TK_GT );.
21b6c 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
21b6d 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b 0a 20 20 20  op==TK_GE );.   
21b6e 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
21b6f 3d 54 4b 5f 45 51 20 29 3b 0a 20 20 20 20 20 20  =TK_EQ );.      
21b70 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
21b71 5f 4e 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73  _NE );.      tes
21b72 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c  tcase( jumpIfNul
21b73 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 63 6f  l==0 );.      co
21b74 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64  deCompareOperand
21b75 73 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  s(pParse, pExpr-
21b76 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 72 65  >pLeft, &r1, &re
21b77 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20 20 20  gFree1,.        
21b78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21b79 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d            pExpr-
21b7a 3e 70 52 69 67 68 74 2c 20 26 72 32 2c 20 26 72  >pRight, &r2, &r
21b7b 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20  egFree2);.      
21b7c 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61 72  codeCompare(pPar
21b7d 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  se, pExpr->pLeft
21b7e 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c  , pExpr->pRight,
21b7f 20 6f 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20   op,.           
21b80 20 20 20 20 20 20 20 72 31 2c 20 72 32 2c 20 64         r1, r2, d
21b81 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29  est, jumpIfNull)
21b82 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
21b83 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b  ( regFree1==0 );
21b84 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21b85 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a   regFree2==0 );.
21b86 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
21b87 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49   }.    case TK_I
21b88 53 4e 55 4c 4c 3a 0a 20 20 20 20 63 61 73 65 20  SNULL:.    case 
21b89 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 7b 0a 20 20  TK_NOTNULL: {.  
21b8a 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 49      assert( TK_I
21b8b 53 4e 55 4c 4c 3d 3d 4f 50 5f 49 73 4e 75 6c 6c  SNULL==OP_IsNull
21b8c 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
21b8d 28 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3d 3d 4f 50  ( TK_NOTNULL==OP
21b8e 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20 20 20 20  _NotNull );.    
21b8f 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
21b90 54 4b 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20  TK_ISNULL );.   
21b91 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
21b92 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 29 3b 0a 20  =TK_NOTNULL );. 
21b93 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65       r1 = sqlite
21b94 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50  3ExprCodeTemp(pP
21b95 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65  arse, pExpr->pLe
21b96 66 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a  ft, &regFree1);.
21b97 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
21b98 65 41 64 64 4f 70 32 28 76 2c 20 6f 70 2c 20 72  eAddOp2(v, op, r
21b99 31 2c 20 64 65 73 74 29 3b 0a 20 20 20 20 20 20  1, dest);.      
21b9a 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65  testcase( regFre
21b9b 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62  e1==0 );.      b
21b9c 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
21b9d 63 61 73 65 20 54 4b 5f 42 45 54 57 45 45 4e 3a  case TK_BETWEEN:
21b9e 20 7b 0a 20 20 20 20 20 20 2f 2a 20 20 20 20 78   {.      /*    x
21b9f 20 42 45 54 57 45 45 4e 20 79 20 41 4e 44 20 7a   BETWEEN y AND z
21ba0 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
21ba1 2a 2a 20 49 73 20 65 71 75 69 76 61 6c 65 6e 74  ** Is equivalent
21ba2 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20   to .      **.  
21ba3 20 20 20 20 2a 2a 20 20 20 20 78 3e 3d 79 20 41      **    x>=y A
21ba4 4e 44 20 78 3c 3d 7a 0a 20 20 20 20 20 20 2a 2a  ND x<=z.      **
21ba5 0a 20 20 20 20 20 20 2a 2a 20 43 6f 64 65 20 69  .      ** Code i
21ba6 74 20 61 73 20 73 75 63 68 2c 20 74 61 6b 69 6e  t as such, takin
21ba7 67 20 63 61 72 65 20 74 6f 20 64 6f 20 74 68 65  g care to do the
21ba8 20 63 6f 6d 6d 6f 6e 20 73 75 62 65 78 70 72 65   common subexpre
21ba9 73 73 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 65  ssion.      ** e
21baa 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 78  lementation of x
21bab 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
21bac 20 45 78 70 72 20 65 78 70 72 41 6e 64 3b 0a 20   Expr exprAnd;. 
21bad 20 20 20 20 20 45 78 70 72 20 63 6f 6d 70 4c 65       Expr compLe
21bae 66 74 3b 0a 20 20 20 20 20 20 45 78 70 72 20 63  ft;.      Expr c
21baf 6f 6d 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20  ompRight;.      
21bb0 45 78 70 72 20 65 78 70 72 58 3b 0a 0a 20 20 20  Expr exprX;..   
21bb1 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72     assert( !Expr
21bb2 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
21bb3 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  r, EP_xIsSelect)
21bb4 20 29 3b 0a 20 20 20 20 20 20 65 78 70 72 58 20   );.      exprX 
21bb5 3d 20 2a 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b  = *pExpr->pLeft;
21bb6 0a 20 20 20 20 20 20 65 78 70 72 41 6e 64 2e 6f  .      exprAnd.o
21bb7 70 20 3d 20 54 4b 5f 41 4e 44 3b 0a 20 20 20 20  p = TK_AND;.    
21bb8 20 20 65 78 70 72 41 6e 64 2e 70 4c 65 66 74 20    exprAnd.pLeft 
21bb9 3d 20 26 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 20  = &compLeft;.   
21bba 20 20 20 65 78 70 72 41 6e 64 2e 70 52 69 67 68     exprAnd.pRigh
21bbb 74 20 3d 20 26 63 6f 6d 70 52 69 67 68 74 3b 0a  t = &compRight;.
21bbc 20 20 20 20 20 20 63 6f 6d 70 4c 65 66 74 2e 6f        compLeft.o
21bbd 70 20 3d 20 54 4b 5f 47 45 3b 0a 20 20 20 20 20  p = TK_GE;.     
21bbe 20 63 6f 6d 70 4c 65 66 74 2e 70 4c 65 66 74 20   compLeft.pLeft 
21bbf 3d 20 26 65 78 70 72 58 3b 0a 20 20 20 20 20 20  = &exprX;.      
21bc0 63 6f 6d 70 4c 65 66 74 2e 70 52 69 67 68 74 20  compLeft.pRight 
21bc1 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  = pExpr->x.pList
21bc2 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20  ->a[0].pExpr;.  
21bc3 20 20 20 20 63 6f 6d 70 52 69 67 68 74 2e 6f 70      compRight.op
21bc4 20 3d 20 54 4b 5f 4c 45 3b 0a 20 20 20 20 20 20   = TK_LE;.      
21bc5 63 6f 6d 70 52 69 67 68 74 2e 70 4c 65 66 74 20  compRight.pLeft 
21bc6 3d 20 26 65 78 70 72 58 3b 0a 20 20 20 20 20 20  = &exprX;.      
21bc7 63 6f 6d 70 52 69 67 68 74 2e 70 52 69 67 68 74  compRight.pRight
21bc8 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73   = pExpr->x.pLis
21bc9 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 3b 0a 20  t->a[1].pExpr;. 
21bca 20 20 20 20 20 65 78 70 72 58 2e 69 54 61 62 6c       exprX.iTabl
21bcb 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  e = sqlite3ExprC
21bcc 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20  odeTemp(pParse, 
21bcd 26 65 78 70 72 58 2c 20 26 72 65 67 46 72 65 65  &exprX, &regFree
21bce 31 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61  1);.      testca
21bcf 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20  se( regFree1==0 
21bd0 29 3b 0a 20 20 20 20 20 20 65 78 70 72 58 2e 6f  );.      exprX.o
21bd1 70 20 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b  p = TK_REGISTER;
21bd2 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21bd3 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29   jumpIfNull==0 )
21bd4 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
21bd5 78 70 72 49 66 54 72 75 65 28 70 50 61 72 73 65  xprIfTrue(pParse
21bd6 2c 20 26 65 78 70 72 41 6e 64 2c 20 64 65 73 74  , &exprAnd, dest
21bd7 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20  , jumpIfNull);. 
21bd8 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
21bd9 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b  }.    default: {
21bda 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69  .      r1 = sqli
21bdb 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28  te3ExprCodeTemp(
21bdc 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 26  pParse, pExpr, &
21bdd 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20  regFree1);.     
21bde 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21bdf 70 33 28 76 2c 20 4f 50 5f 49 66 2c 20 72 31 2c  p3(v, OP_If, r1,
21be0 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c   dest, jumpIfNul
21be1 6c 21 3d 30 29 3b 0a 20 20 20 20 20 20 74 65 73  l!=0);.      tes
21be2 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d  tcase( regFree1=
21be3 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  =0 );.      test
21be4 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c  case( jumpIfNull
21be5 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65  ==0 );.      bre
21be6 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ak;.    }.  }.  
21be7 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
21be8 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65  mpReg(pParse, re
21be9 67 46 72 65 65 31 29 3b 0a 20 20 73 71 6c 69 74  gFree1);.  sqlit
21bea 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
21beb 28 70 50 61 72 73 65 2c 20 72 65 67 46 72 65 65  (pParse, regFree
21bec 32 29 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  2);  .}../*.** G
21bed 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72  enerate code for
21bee 20 61 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65   a boolean expre
21bef 73 73 69 6f 6e 20 73 75 63 68 20 74 68 61 74 20  ssion such that 
21bf0 61 20 6a 75 6d 70 20 69 73 20 6d 61 64 65 0a 2a  a jump is made.*
21bf1 2a 20 74 6f 20 74 68 65 20 6c 61 62 65 6c 20 22  * to the label "
21bf2 64 65 73 74 22 20 69 66 20 74 68 65 20 65 78 70  dest" if the exp
21bf3 72 65 73 73 69 6f 6e 20 69 73 20 66 61 6c 73 65  ression is false
21bf4 20 62 75 74 20 65 78 65 63 75 74 69 6f 6e 0a 2a   but execution.*
21bf5 2a 20 63 6f 6e 74 69 6e 75 65 73 20 73 74 72 61  * continues stra
21bf6 69 67 68 74 20 74 68 72 75 20 69 66 20 74 68 65  ight thru if the
21bf7 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74   expression is t
21bf8 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  rue..**.** If th
21bf9 65 20 65 78 70 72 65 73 73 69 6f 6e 20 65 76 61  e expression eva
21bfa 6c 75 61 74 65 73 20 74 6f 20 4e 55 4c 4c 20 28  luates to NULL (
21bfb 6e 65 69 74 68 65 72 20 74 72 75 65 20 6e 6f 72  neither true nor
21bfc 20 66 61 6c 73 65 29 20 74 68 65 6e 0a 2a 2a 20   false) then.** 
21bfd 6a 75 6d 70 20 69 66 20 6a 75 6d 70 49 66 4e 75  jump if jumpIfNu
21bfe 6c 6c 20 69 73 20 53 51 4c 49 54 45 5f 4a 55 4d  ll is SQLITE_JUM
21bff 50 49 46 4e 55 4c 4c 20 6f 72 20 66 61 6c 6c 20  PIFNULL or fall 
21c00 74 68 72 6f 75 67 68 20 69 66 20 6a 75 6d 70 49  through if jumpI
21c01 66 4e 75 6c 6c 0a 2a 2a 20 69 73 20 30 2e 0a 2a  fNull.** is 0..*
21c02 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
21c03 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
21c04 72 49 66 46 61 6c 73 65 28 50 61 72 73 65 20 2a  rIfFalse(Parse *
21c05 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45  pParse, Expr *pE
21c06 78 70 72 2c 20 69 6e 74 20 64 65 73 74 2c 20 69  xpr, int dest, i
21c07 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a  nt jumpIfNull){.
21c08 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
21c09 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74  se->pVdbe;.  int
21c0a 20 6f 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72   op = 0;.  int r
21c0b 65 67 46 72 65 65 31 20 3d 20 30 3b 0a 20 20 69  egFree1 = 0;.  i
21c0c 6e 74 20 72 65 67 46 72 65 65 32 20 3d 20 30 3b  nt regFree2 = 0;
21c0d 0a 20 20 69 6e 74 20 72 31 2c 20 72 32 3b 0a 0a  .  int r1, r2;..
21c0e 20 20 61 73 73 65 72 74 28 20 6a 75 6d 70 49 66    assert( jumpIf
21c0f 4e 75 6c 6c 3d 3d 53 51 4c 49 54 45 5f 4a 55 4d  Null==SQLITE_JUM
21c10 50 49 46 4e 55 4c 4c 20 7c 7c 20 6a 75 6d 70 49  PIFNULL || jumpI
21c11 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 69 66  fNull==0 );.  if
21c12 28 20 4e 45 56 45 52 28 76 3d 3d 30 29 20 29 20  ( NEVER(v==0) ) 
21c13 72 65 74 75 72 6e 3b 20 2f 2a 20 45 78 69 73 74  return; /* Exist
21c14 61 6e 63 65 20 6f 66 20 56 44 42 45 20 63 68 65  ance of VDBE che
21c15 63 6b 65 64 20 62 79 20 63 61 6c 6c 65 72 20 2a  cked by caller *
21c16 2f 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30  /.  if( pExpr==0
21c17 20 29 20 20 20 20 72 65 74 75 72 6e 3b 0a 0a 20   )    return;.. 
21c18 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66   /* The value of
21c19 20 70 45 78 70 72 2d 3e 6f 70 20 61 6e 64 20 6f   pExpr->op and o
21c1a 70 20 61 72 65 20 72 65 6c 61 74 65 64 20 61 73  p are related as
21c1b 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 2a 2a 0a 20   follows:.  **. 
21c1c 20 2a 2a 20 20 20 20 20 20 20 70 45 78 70 72 2d   **       pExpr-
21c1d 3e 6f 70 20 20 20 20 20 20 20 20 20 20 20 20 6f  >op            o
21c1e 70 0a 20 20 2a 2a 20 20 20 20 20 20 20 2d 2d 2d  p.  **       ---
21c1f 2d 2d 2d 2d 2d 2d 20 20 20 20 20 20 20 20 20 20  ------          
21c20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 20  ----------.  ** 
21c21 20 20 20 20 20 20 54 4b 5f 49 53 4e 55 4c 4c 20        TK_ISNULL 
21c22 20 20 20 20 20 20 20 20 20 4f 50 5f 4e 6f 74 4e           OP_NotN
21c23 75 6c 6c 0a 20 20 2a 2a 20 20 20 20 20 20 20 54  ull.  **       T
21c24 4b 5f 4e 4f 54 4e 55 4c 4c 20 20 20 20 20 20 20  K_NOTNULL       
21c25 20 20 4f 50 5f 49 73 4e 75 6c 6c 0a 20 20 2a 2a    OP_IsNull.  **
21c26 20 20 20 20 20 20 20 54 4b 5f 4e 45 20 20 20 20         TK_NE    
21c27 20 20 20 20 20 20 20 20 20 20 4f 50 5f 45 71 0a            OP_Eq.
21c28 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 45 51    **       TK_EQ
21c29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f 50                OP
21c2a 5f 4e 65 0a 20 20 2a 2a 20 20 20 20 20 20 20 54  _Ne.  **       T
21c2b 4b 5f 47 54 20 20 20 20 20 20 20 20 20 20 20 20  K_GT            
21c2c 20 20 4f 50 5f 4c 65 0a 20 20 2a 2a 20 20 20 20    OP_Le.  **    
21c2d 20 20 20 54 4b 5f 4c 45 20 20 20 20 20 20 20 20     TK_LE        
21c2e 20 20 20 20 20 20 4f 50 5f 47 74 0a 20 20 2a 2a        OP_Gt.  **
21c2f 20 20 20 20 20 20 20 54 4b 5f 47 45 20 20 20 20         TK_GE    
21c30 20 20 20 20 20 20 20 20 20 20 4f 50 5f 4c 74 0a            OP_Lt.
21c31 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 4c 54    **       TK_LT
21c32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f 50                OP
21c33 5f 47 65 0a 20 20 2a 2a 0a 20 20 2a 2a 20 46 6f  _Ge.  **.  ** Fo
21c34 72 20 6f 74 68 65 72 20 76 61 6c 75 65 73 20 6f  r other values o
21c35 66 20 70 45 78 70 72 2d 3e 6f 70 2c 20 6f 70 20  f pExpr->op, op 
21c36 69 73 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64  is undefined and
21c37 20 75 6e 75 73 65 64 2e 0a 20 20 2a 2a 20 54 68   unused..  ** Th
21c38 65 20 76 61 6c 75 65 20 6f 66 20 54 4b 5f 20 61  e value of TK_ a
21c39 6e 64 20 4f 50 5f 20 63 6f 6e 73 74 61 6e 74 73  nd OP_ constants
21c3a 20 61 72 65 20 61 72 72 61 6e 67 65 64 20 73 75   are arranged su
21c3b 63 68 20 74 68 61 74 20 77 65 0a 20 20 2a 2a 20  ch that we.  ** 
21c3c 63 61 6e 20 63 6f 6d 70 75 74 65 20 74 68 65 20  can compute the 
21c3d 6d 61 70 70 69 6e 67 20 61 62 6f 76 65 20 75 73  mapping above us
21c3e 69 6e 67 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  ing the followin
21c3f 67 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 20 20  g expression..  
21c40 2a 2a 20 41 73 73 65 72 74 28 29 73 20 76 65 72  ** Assert()s ver
21c41 69 66 79 20 74 68 61 74 20 74 68 65 20 63 6f 6d  ify that the com
21c42 70 75 74 61 74 69 6f 6e 20 69 73 20 63 6f 72 72  putation is corr
21c43 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 6f 70 20 3d  ect..  */.  op =
21c44 20 28 28 70 45 78 70 72 2d 3e 6f 70 2b 28 54 4b   ((pExpr->op+(TK
21c45 5f 49 53 4e 55 4c 4c 26 31 29 29 5e 31 29 2d 28  _ISNULL&1))^1)-(
21c46 54 4b 5f 49 53 4e 55 4c 4c 26 31 29 3b 0a 0a 20  TK_ISNULL&1);.. 
21c47 20 2f 2a 20 56 65 72 69 66 79 20 63 6f 72 72 65   /* Verify corre
21c48 63 74 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20  ct alignment of 
21c49 54 4b 5f 20 61 6e 64 20 4f 50 5f 20 63 6f 6e 73  TK_ and OP_ cons
21c4a 74 61 6e 74 73 0a 20 20 2a 2f 0a 20 20 61 73 73  tants.  */.  ass
21c4b 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d  ert( pExpr->op!=
21c4c 54 4b 5f 49 53 4e 55 4c 4c 20 7c 7c 20 6f 70 3d  TK_ISNULL || op=
21c4d 3d 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20  =OP_NotNull );. 
21c4e 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
21c4f 6f 70 21 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 7c  op!=TK_NOTNULL |
21c50 7c 20 6f 70 3d 3d 4f 50 5f 49 73 4e 75 6c 6c 20  | op==OP_IsNull 
21c51 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 78  );.  assert( pEx
21c52 70 72 2d 3e 6f 70 21 3d 54 4b 5f 4e 45 20 7c 7c  pr->op!=TK_NE ||
21c53 20 6f 70 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20   op==OP_Eq );.  
21c54 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f  assert( pExpr->o
21c55 70 21 3d 54 4b 5f 45 51 20 7c 7c 20 6f 70 3d 3d  p!=TK_EQ || op==
21c56 4f 50 5f 4e 65 20 29 3b 0a 20 20 61 73 73 65 72  OP_Ne );.  asser
21c57 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b  t( pExpr->op!=TK
21c58 5f 4c 54 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 47 65  _LT || op==OP_Ge
21c59 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45   );.  assert( pE
21c5a 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 4c 45 20 7c  xpr->op!=TK_LE |
21c5b 7c 20 6f 70 3d 3d 4f 50 5f 47 74 20 29 3b 0a 20  | op==OP_Gt );. 
21c5c 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
21c5d 6f 70 21 3d 54 4b 5f 47 54 20 7c 7c 20 6f 70 3d  op!=TK_GT || op=
21c5e 3d 4f 50 5f 4c 65 20 29 3b 0a 20 20 61 73 73 65  =OP_Le );.  asse
21c5f 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54  rt( pExpr->op!=T
21c60 4b 5f 47 45 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4c  K_GE || op==OP_L
21c61 74 20 29 3b 0a 0a 20 20 73 77 69 74 63 68 28 20  t );..  switch( 
21c62 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20  pExpr->op ){.   
21c63 20 63 61 73 65 20 54 4b 5f 41 4e 44 3a 20 7b 0a   case TK_AND: {.
21c64 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
21c65 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b  jumpIfNull==0 );
21c66 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
21c67 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65  prIfFalse(pParse
21c68 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  , pExpr->pLeft, 
21c69 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c  dest, jumpIfNull
21c6a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
21c6b 45 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72  ExprIfFalse(pPar
21c6c 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68  se, pExpr->pRigh
21c6d 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e  t, dest, jumpIfN
21c6e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61  ull);.      brea
21c6f 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
21c70 65 20 54 4b 5f 4f 52 3a 20 7b 0a 20 20 20 20 20  e TK_OR: {.     
21c71 20 69 6e 74 20 64 32 20 3d 20 73 71 6c 69 74 65   int d2 = sqlite
21c72 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76  3VdbeMakeLabel(v
21c73 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
21c74 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30  e( jumpIfNull==0
21c75 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
21c76 33 45 78 70 72 43 61 63 68 65 50 75 73 68 28 70  3ExprCachePush(p
21c77 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 73 71  Parse);.      sq
21c78 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28  lite3ExprIfTrue(
21c79 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
21c7a 4c 65 66 74 2c 20 64 32 2c 20 6a 75 6d 70 49 66  Left, d2, jumpIf
21c7b 4e 75 6c 6c 5e 53 51 4c 49 54 45 5f 4a 55 4d 50  Null^SQLITE_JUMP
21c7c 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 73  IFNULL);.      s
21c7d 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73  qlite3ExprIfFals
21c7e 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  e(pParse, pExpr-
21c7f 3e 70 52 69 67 68 74 2c 20 64 65 73 74 2c 20 6a  >pRight, dest, j
21c80 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20  umpIfNull);.    
21c81 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
21c82 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 64 32 29  olveLabel(v, d2)
21c83 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
21c84 78 70 72 43 61 63 68 65 50 6f 70 28 70 50 61 72  xprCachePop(pPar
21c85 73 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 62 72  se, 1);.      br
21c86 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
21c87 61 73 65 20 54 4b 5f 4e 4f 54 3a 20 7b 0a 20 20  ase TK_NOT: {.  
21c88 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49      sqlite3ExprI
21c89 66 54 72 75 65 28 70 50 61 72 73 65 2c 20 70 45  fTrue(pParse, pE
21c8a 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64 65 73 74  xpr->pLeft, dest
21c8b 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20  , jumpIfNull);. 
21c8c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
21c8d 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c 54  }.    case TK_LT
21c8e 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c 45  :.    case TK_LE
21c8f 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47 54  :.    case TK_GT
21c90 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47 45  :.    case TK_GE
21c91 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 45  :.    case TK_NE
21c92 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 51  :.    case TK_EQ
21c93 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61  : {.      testca
21c94 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 54 20 29 3b  se( op==TK_LT );
21c95 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21c96 20 6f 70 3d 3d 54 4b 5f 4c 45 20 29 3b 0a 20 20   op==TK_LE );.  
21c97 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
21c98 3d 3d 54 4b 5f 47 54 20 29 3b 0a 20 20 20 20 20  ==TK_GT );.     
21c99 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
21c9a 4b 5f 47 45 20 29 3b 0a 20 20 20 20 20 20 74 65  K_GE );.      te
21c9b 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 45  stcase( op==TK_E
21c9c 51 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  Q );.      testc
21c9d 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 45 20 29  ase( op==TK_NE )
21c9e 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
21c9f 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20  ( jumpIfNull==0 
21ca0 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d  );.      codeCom
21ca1 70 61 72 65 4f 70 65 72 61 6e 64 73 28 70 50 61  pareOperands(pPa
21ca2 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
21ca3 74 2c 20 26 72 31 2c 20 26 72 65 67 46 72 65 65  t, &r1, &regFree
21ca4 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  1,.             
21ca5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21ca6 20 20 20 20 20 70 45 78 70 72 2d 3e 70 52 69 67       pExpr->pRig
21ca7 68 74 2c 20 26 72 32 2c 20 26 72 65 67 46 72 65  ht, &r2, &regFre
21ca8 65 32 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43  e2);.      codeC
21ca9 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20 70  ompare(pParse, p
21caa 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78  Expr->pLeft, pEx
21cab 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c 0a  pr->pRight, op,.
21cac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21cad 20 20 72 31 2c 20 72 32 2c 20 64 65 73 74 2c 20    r1, r2, dest, 
21cae 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20  jumpIfNull);.   
21caf 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67     testcase( reg
21cb0 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20  Free1==0 );.    
21cb1 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46    testcase( regF
21cb2 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20  ree2==0 );.     
21cb3 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
21cb4 20 20 63 61 73 65 20 54 4b 5f 49 53 4e 55 4c 4c    case TK_ISNULL
21cb5 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f  :.    case TK_NO
21cb6 54 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 74  TNULL: {.      t
21cb7 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
21cb8 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20  ISNULL );.      
21cb9 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
21cba 5f 4e 4f 54 4e 55 4c 4c 20 29 3b 0a 20 20 20 20  _NOTNULL );.    
21cbb 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78    r1 = sqlite3Ex
21cbc 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73  prCodeTemp(pPars
21cbd 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c  e, pExpr->pLeft,
21cbe 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20   &regFree1);.   
21cbf 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
21cc0 64 4f 70 32 28 76 2c 20 6f 70 2c 20 72 31 2c 20  dOp2(v, op, r1, 
21cc1 64 65 73 74 29 3b 0a 20 20 20 20 20 20 74 65 73  dest);.      tes
21cc2 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d  tcase( regFree1=
21cc3 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61  =0 );.      brea
21cc4 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
21cc5 65 20 54 4b 5f 42 45 54 57 45 45 4e 3a 20 7b 0a  e TK_BETWEEN: {.
21cc6 20 20 20 20 20 20 2f 2a 20 20 20 20 78 20 42 45        /*    x BE
21cc7 54 57 45 45 4e 20 79 20 41 4e 44 20 7a 0a 20 20  TWEEN y AND z.  
21cc8 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
21cc9 49 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f  Is equivalent to
21cca 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20   .      **.     
21ccb 20 2a 2a 20 20 20 20 78 3e 3d 79 20 41 4e 44 20   **    x>=y AND 
21ccc 78 3c 3d 7a 0a 20 20 20 20 20 20 2a 2a 0a 20 20  x<=z.      **.  
21ccd 20 20 20 20 2a 2a 20 43 6f 64 65 20 69 74 20 61      ** Code it a
21cce 73 20 73 75 63 68 2c 20 74 61 6b 69 6e 67 20 63  s such, taking c
21ccf 61 72 65 20 74 6f 20 64 6f 20 74 68 65 20 63 6f  are to do the co
21cd0 6d 6d 6f 6e 20 73 75 62 65 78 70 72 65 73 73 69  mmon subexpressi
21cd1 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 65 6c 65 6d  on.      ** elem
21cd2 65 6e 74 61 74 69 6f 6e 20 6f 66 20 78 2e 0a 20  entation of x.. 
21cd3 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 45 78       */.      Ex
21cd4 70 72 20 65 78 70 72 41 6e 64 3b 0a 20 20 20 20  pr exprAnd;.    
21cd5 20 20 45 78 70 72 20 63 6f 6d 70 4c 65 66 74 3b    Expr compLeft;
21cd6 0a 20 20 20 20 20 20 45 78 70 72 20 63 6f 6d 70  .      Expr comp
21cd7 52 69 67 68 74 3b 0a 20 20 20 20 20 20 45 78 70  Right;.      Exp
21cd8 72 20 65 78 70 72 58 3b 0a 0a 20 20 20 20 20 20  r exprX;..      
21cd9 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73  assert( !ExprHas
21cda 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
21cdb 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b  EP_xIsSelect) );
21cdc 0a 20 20 20 20 20 20 65 78 70 72 58 20 3d 20 2a  .      exprX = *
21cdd 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20  pExpr->pLeft;.  
21cde 20 20 20 20 65 78 70 72 41 6e 64 2e 6f 70 20 3d      exprAnd.op =
21cdf 20 54 4b 5f 41 4e 44 3b 0a 20 20 20 20 20 20 65   TK_AND;.      e
21ce0 78 70 72 41 6e 64 2e 70 4c 65 66 74 20 3d 20 26  xprAnd.pLeft = &
21ce1 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 20 20 20 20  compLeft;.      
21ce2 65 78 70 72 41 6e 64 2e 70 52 69 67 68 74 20 3d  exprAnd.pRight =
21ce3 20 26 63 6f 6d 70 52 69 67 68 74 3b 0a 20 20 20   &compRight;.   
21ce4 20 20 20 63 6f 6d 70 4c 65 66 74 2e 6f 70 20 3d     compLeft.op =
21ce5 20 54 4b 5f 47 45 3b 0a 20 20 20 20 20 20 63 6f   TK_GE;.      co
21ce6 6d 70 4c 65 66 74 2e 70 4c 65 66 74 20 3d 20 26  mpLeft.pLeft = &
21ce7 65 78 70 72 58 3b 0a 20 20 20 20 20 20 63 6f 6d  exprX;.      com
21ce8 70 4c 65 66 74 2e 70 52 69 67 68 74 20 3d 20 70  pLeft.pRight = p
21ce9 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61  Expr->x.pList->a
21cea 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20  [0].pExpr;.     
21ceb 20 63 6f 6d 70 52 69 67 68 74 2e 6f 70 20 3d 20   compRight.op = 
21cec 54 4b 5f 4c 45 3b 0a 20 20 20 20 20 20 63 6f 6d  TK_LE;.      com
21ced 70 52 69 67 68 74 2e 70 4c 65 66 74 20 3d 20 26  pRight.pLeft = &
21cee 65 78 70 72 58 3b 0a 20 20 20 20 20 20 63 6f 6d  exprX;.      com
21cef 70 52 69 67 68 74 2e 70 52 69 67 68 74 20 3d 20  pRight.pRight = 
21cf0 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e  pExpr->x.pList->
21cf1 61 5b 31 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20  a[1].pExpr;.    
21cf2 20 20 65 78 70 72 58 2e 69 54 61 62 6c 65 20 3d    exprX.iTable =
21cf3 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
21cf4 54 65 6d 70 28 70 50 61 72 73 65 2c 20 26 65 78  Temp(pParse, &ex
21cf5 70 72 58 2c 20 26 72 65 67 46 72 65 65 31 29 3b  prX, &regFree1);
21cf6 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21cf7 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a   regFree1==0 );.
21cf8 20 20 20 20 20 20 65 78 70 72 58 2e 6f 70 20 3d        exprX.op =
21cf9 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 20   TK_REGISTER;.  
21cfa 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75      testcase( ju
21cfb 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20  mpIfNull==0 );. 
21cfc 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
21cfd 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20  IfFalse(pParse, 
21cfe 26 65 78 70 72 41 6e 64 2c 20 64 65 73 74 2c 20  &exprAnd, dest, 
21cff 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20  jumpIfNull);.   
21d00 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
21d01 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
21d02 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65       r1 = sqlite
21d03 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50  3ExprCodeTemp(pP
21d04 61 72 73 65 2c 20 70 45 78 70 72 2c 20 26 72 65  arse, pExpr, &re
21d05 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 73  gFree1);.      s
21d06 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
21d07 28 76 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20 72 31  (v, OP_IfNot, r1
21d08 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75  , dest, jumpIfNu
21d09 6c 6c 21 3d 30 29 3b 0a 20 20 20 20 20 20 74 65  ll!=0);.      te
21d0a 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31  stcase( regFree1
21d0b 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73  ==0 );.      tes
21d0c 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c  tcase( jumpIfNul
21d0d 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72  l==0 );.      br
21d0e 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  eak;.    }.  }. 
21d0f 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
21d10 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
21d11 65 67 46 72 65 65 31 29 3b 0a 20 20 73 71 6c 69  egFree1);.  sqli
21d12 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
21d13 67 28 70 50 61 72 73 65 2c 20 72 65 67 46 72 65  g(pParse, regFre
21d14 65 32 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f  e2);.}../*.** Do
21d15 20 61 20 64 65 65 70 20 63 6f 6d 70 61 72 69 73   a deep comparis
21d16 6f 6e 20 6f 66 20 74 77 6f 20 65 78 70 72 65 73  on of two expres
21d17 73 69 6f 6e 20 74 72 65 65 73 2e 20 20 52 65 74  sion trees.  Ret
21d18 75 72 6e 20 54 52 55 45 20 28 6e 6f 6e 2d 7a 65  urn TRUE (non-ze
21d19 72 6f 29 0a 2a 2a 20 69 66 20 74 68 65 79 20 61  ro).** if they a
21d1a 72 65 20 69 64 65 6e 74 69 63 61 6c 20 61 6e 64  re identical and
21d1b 20 72 65 74 75 72 6e 20 46 41 4c 53 45 20 69 66   return FALSE if
21d1c 20 74 68 65 79 20 64 69 66 66 65 72 20 69 6e 20   they differ in 
21d1d 61 6e 79 20 77 61 79 2e 0a 2a 2a 0a 2a 2a 20 53  any way..**.** S
21d1e 6f 6d 65 74 69 6d 65 73 20 74 68 69 73 20 72 6f  ometimes this ro
21d1f 75 74 69 6e 65 20 77 69 6c 6c 20 72 65 74 75 72  utine will retur
21d20 6e 20 46 41 4c 53 45 20 65 76 65 6e 20 69 66 20  n FALSE even if 
21d21 74 68 65 20 74 77 6f 20 65 78 70 72 65 73 73 69  the two expressi
21d22 6f 6e 73 0a 2a 2a 20 72 65 61 6c 6c 79 20 61 72  ons.** really ar
21d23 65 20 65 71 75 69 76 61 6c 65 6e 74 2e 20 20 49  e equivalent.  I
21d24 66 20 77 65 20 63 61 6e 6e 6f 74 20 70 72 6f 76  f we cannot prov
21d25 65 20 74 68 61 74 20 74 68 65 20 65 78 70 72 65  e that the expre
21d26 73 73 69 6f 6e 73 20 61 72 65 0a 2a 2a 20 69 64  ssions are.** id
21d27 65 6e 74 69 63 61 6c 2c 20 77 65 20 72 65 74 75  entical, we retu
21d28 72 6e 20 46 41 4c 53 45 20 6a 75 73 74 20 74 6f  rn FALSE just to
21d29 20 62 65 20 73 61 66 65 2e 20 20 53 6f 20 69 66   be safe.  So if
21d2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   this routine.**
21d2b 20 72 65 74 75 72 6e 73 20 66 61 6c 73 65 2c 20   returns false, 
21d2c 74 68 65 6e 20 79 6f 75 20 64 6f 20 6e 6f 74 20  then you do not 
21d2d 72 65 61 6c 6c 79 20 6b 6e 6f 77 20 66 6f 72 20  really know for 
21d2e 63 65 72 74 61 69 6e 20 69 66 20 74 68 65 20 74  certain if the t
21d2f 77 6f 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e  wo.** expression
21d30 73 20 61 72 65 20 74 68 65 20 73 61 6d 65 2e 20  s are the same. 
21d31 20 42 75 74 20 69 66 20 79 6f 75 20 67 65 74 20   But if you get 
21d32 61 20 54 52 55 45 20 72 65 74 75 72 6e 2c 20 74  a TRUE return, t
21d33 68 65 6e 20 79 6f 75 0a 2a 2a 20 63 61 6e 20 62  hen you.** can b
21d34 65 20 73 75 72 65 20 74 68 65 20 65 78 70 72 65  e sure the expre
21d35 73 73 69 6f 6e 73 20 61 72 65 20 74 68 65 20 73  ssions are the s
21d36 61 6d 65 2e 20 20 49 6e 20 74 68 65 20 70 6c 61  ame.  In the pla
21d37 63 65 73 20 77 68 65 72 65 0a 2a 2a 20 74 68 69  ces where.** thi
21d38 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65  s routine is use
21d39 64 2c 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 68  d, it does not h
21d3a 75 72 74 20 74 6f 20 67 65 74 20 61 6e 20 65 78  urt to get an ex
21d3b 74 72 61 20 46 41 4c 53 45 20 2d 20 74 68 61 74  tra FALSE - that
21d3c 0a 2a 2a 20 6a 75 73 74 20 6d 69 67 68 74 20 72  .** just might r
21d3d 65 73 75 6c 74 20 69 6e 20 73 6f 6d 65 20 73 6c  esult in some sl
21d3e 69 67 68 74 6c 79 20 73 6c 6f 77 65 72 20 63 6f  ightly slower co
21d3f 64 65 2e 20 20 42 75 74 20 72 65 74 75 72 6e 69  de.  But returni
21d40 6e 67 0a 2a 2a 20 61 6e 20 69 6e 63 6f 72 72 65  ng.** an incorre
21d41 63 74 20 54 52 55 45 20 63 6f 75 6c 64 20 6c 65  ct TRUE could le
21d42 61 64 20 74 6f 20 61 20 6d 61 6c 66 75 6e 63 74  ad to a malfunct
21d43 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion..*/.SQLITE_P
21d44 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
21d45 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 45 78  e3ExprCompare(Ex
21d46 70 72 20 2a 70 41 2c 20 45 78 70 72 20 2a 70 42  pr *pA, Expr *pB
21d47 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66  ){.  int i;.  if
21d48 28 20 70 41 3d 3d 30 7c 7c 70 42 3d 3d 30 20 29  ( pA==0||pB==0 )
21d49 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 42 3d  {.    return pB=
21d4a 3d 70 41 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  =pA;.  }.  asser
21d4b 74 28 20 21 45 78 70 72 48 61 73 41 6e 79 50 72  t( !ExprHasAnyPr
21d4c 6f 70 65 72 74 79 28 70 41 2c 20 45 50 5f 54 6f  operty(pA, EP_To
21d4d 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 52 65 64 75 63  kenOnly|EP_Reduc
21d4e 65 64 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ed) );.  assert(
21d4f 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70   !ExprHasAnyProp
21d50 65 72 74 79 28 70 42 2c 20 45 50 5f 54 6f 6b 65  erty(pB, EP_Toke
21d51 6e 4f 6e 6c 79 7c 45 50 5f 52 65 64 75 63 65 64  nOnly|EP_Reduced
21d52 29 20 29 3b 0a 20 20 69 66 28 20 45 78 70 72 48  ) );.  if( ExprH
21d53 61 73 50 72 6f 70 65 72 74 79 28 70 41 2c 20 45  asProperty(pA, E
21d54 50 5f 78 49 73 53 65 6c 65 63 74 29 20 7c 7c 20  P_xIsSelect) || 
21d55 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
21d56 70 42 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  pB, EP_xIsSelect
21d57 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
21d58 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 28 70 41  0;.  }.  if( (pA
21d59 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 44 69 73  ->flags & EP_Dis
21d5a 74 69 6e 63 74 29 21 3d 28 70 42 2d 3e 66 6c 61  tinct)!=(pB->fla
21d5b 67 73 20 26 20 45 50 5f 44 69 73 74 69 6e 63 74  gs & EP_Distinct
21d5c 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  ) ) return 0;.  
21d5d 69 66 28 20 70 41 2d 3e 6f 70 21 3d 70 42 2d 3e  if( pA->op!=pB->
21d5e 6f 70 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  op ) return 0;. 
21d5f 20 69 66 28 20 21 73 71 6c 69 74 65 33 45 78 70   if( !sqlite3Exp
21d60 72 43 6f 6d 70 61 72 65 28 70 41 2d 3e 70 4c 65  rCompare(pA->pLe
21d61 66 74 2c 20 70 42 2d 3e 70 4c 65 66 74 29 20 29  ft, pB->pLeft) )
21d62 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28   return 0;.  if(
21d63 20 21 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d   !sqlite3ExprCom
21d64 70 61 72 65 28 70 41 2d 3e 70 52 69 67 68 74 2c  pare(pA->pRight,
21d65 20 70 42 2d 3e 70 52 69 67 68 74 29 20 29 20 72   pB->pRight) ) r
21d66 65 74 75 72 6e 20 30 3b 0a 0a 20 20 69 66 28 20  eturn 0;..  if( 
21d67 70 41 2d 3e 78 2e 70 4c 69 73 74 20 26 26 20 70  pA->x.pList && p
21d68 42 2d 3e 78 2e 70 4c 69 73 74 20 29 7b 0a 20 20  B->x.pList ){.  
21d69 20 20 69 66 28 20 70 41 2d 3e 78 2e 70 4c 69 73    if( pA->x.pLis
21d6a 74 2d 3e 6e 45 78 70 72 21 3d 70 42 2d 3e 78 2e  t->nExpr!=pB->x.
21d6b 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 20 72  pList->nExpr ) r
21d6c 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 66 6f 72  eturn 0;.    for
21d6d 28 69 3d 30 3b 20 69 3c 70 41 2d 3e 78 2e 70 4c  (i=0; i<pA->x.pL
21d6e 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29  ist->nExpr; i++)
21d6f 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 45  {.      Expr *pE
21d70 78 70 72 41 20 3d 20 70 41 2d 3e 78 2e 70 4c 69  xprA = pA->x.pLi
21d71 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a  st->a[i].pExpr;.
21d72 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 78 70        Expr *pExp
21d73 72 42 20 3d 20 70 42 2d 3e 78 2e 70 4c 69 73 74  rB = pB->x.pList
21d74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20  ->a[i].pExpr;.  
21d75 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33      if( !sqlite3
21d76 45 78 70 72 43 6f 6d 70 61 72 65 28 70 45 78 70  ExprCompare(pExp
21d77 72 41 2c 20 70 45 78 70 72 42 29 20 29 20 72 65  rA, pExprB) ) re
21d78 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20  turn 0;.    }.  
21d79 7d 65 6c 73 65 20 69 66 28 20 70 41 2d 3e 78 2e  }else if( pA->x.
21d7a 70 4c 69 73 74 20 7c 7c 20 70 42 2d 3e 78 2e 70  pList || pB->x.p
21d7b 4c 69 73 74 20 29 7b 0a 20 20 20 20 72 65 74 75  List ){.    retu
21d7c 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  rn 0;.  }..  if(
21d7d 20 70 41 2d 3e 69 54 61 62 6c 65 21 3d 70 42 2d   pA->iTable!=pB-
21d7e 3e 69 54 61 62 6c 65 20 7c 7c 20 70 41 2d 3e 69  >iTable || pA->i
21d7f 43 6f 6c 75 6d 6e 21 3d 70 42 2d 3e 69 43 6f 6c  Column!=pB->iCol
21d80 75 6d 6e 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  umn ) return 0;.
21d81 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f    if( ExprHasPro
21d82 70 65 72 74 79 28 70 41 2c 20 45 50 5f 49 6e 74  perty(pA, EP_Int
21d83 56 61 6c 75 65 29 20 29 7b 0a 20 20 20 20 69 66  Value) ){.    if
21d84 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72  ( !ExprHasProper
21d85 74 79 28 70 42 2c 20 45 50 5f 49 6e 74 56 61 6c  ty(pB, EP_IntVal
21d86 75 65 29 20 7c 7c 20 70 41 2d 3e 75 2e 69 56 61  ue) || pA->u.iVa
21d87 6c 75 65 21 3d 70 42 2d 3e 75 2e 69 56 61 6c 75  lue!=pB->u.iValu
21d88 65 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  e ){.      retur
21d89 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  n 0;.    }.  }el
21d8a 73 65 20 69 66 28 20 70 41 2d 3e 6f 70 21 3d 54  se if( pA->op!=T
21d8b 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 70 41 2d 3e  K_COLUMN && pA->
21d8c 75 2e 7a 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20  u.zToken ){.    
21d8d 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65  if( ExprHasPrope
21d8e 72 74 79 28 70 42 2c 20 45 50 5f 49 6e 74 56 61  rty(pB, EP_IntVa
21d8f 6c 75 65 29 20 7c 7c 20 4e 45 56 45 52 28 70 42  lue) || NEVER(pB
21d90 2d 3e 75 2e 7a 54 6f 6b 65 6e 3d 3d 30 29 20 29  ->u.zToken==0) )
21d91 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 69   return 0;.    i
21d92 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
21d93 70 28 70 41 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 70  p(pA->u.zToken,p
21d94 42 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 21 3d 30 20  B->u.zToken)!=0 
21d95 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
21d96 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  0;.    }.  }.  r
21d97 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a  eturn 1;.}.../*.
21d98 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 65 6c 65  ** Add a new ele
21d99 6d 65 6e 74 20 74 6f 20 74 68 65 20 70 41 67 67  ment to the pAgg
21d9a 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 61 72 72  Info->aCol[] arr
21d9b 61 79 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20  ay.  Return the 
21d9c 69 6e 64 65 78 20 6f 66 0a 2a 2a 20 74 68 65 20  index of.** the 
21d9d 6e 65 77 20 65 6c 65 6d 65 6e 74 2e 20 20 52 65  new element.  Re
21d9e 74 75 72 6e 20 61 20 6e 65 67 61 74 69 76 65 20  turn a negative 
21d9f 6e 75 6d 62 65 72 20 69 66 20 6d 61 6c 6c 6f 63  number if malloc
21da0 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69   fails..*/.stati
21da1 63 20 69 6e 74 20 61 64 64 41 67 67 49 6e 66 6f  c int addAggInfo
21da2 43 6f 6c 75 6d 6e 28 73 71 6c 69 74 65 33 20 2a  Column(sqlite3 *
21da3 64 62 2c 20 41 67 67 49 6e 66 6f 20 2a 70 49 6e  db, AggInfo *pIn
21da4 66 6f 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  fo){.  int i;.  
21da5 70 49 6e 66 6f 2d 3e 61 43 6f 6c 20 3d 20 73 71  pInfo->aCol = sq
21da6 6c 69 74 65 33 41 72 72 61 79 41 6c 6c 6f 63 61  lite3ArrayAlloca
21da7 74 65 28 0a 20 20 20 20 20 20 20 64 62 2c 0a 20  te(.       db,. 
21da8 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f        pInfo->aCo
21da9 6c 2c 0a 20 20 20 20 20 20 20 73 69 7a 65 6f 66  l,.       sizeof
21daa 28 70 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 30 5d 29  (pInfo->aCol[0])
21dab 2c 0a 20 20 20 20 20 20 20 33 2c 0a 20 20 20 20  ,.       3,.    
21dac 20 20 20 26 70 49 6e 66 6f 2d 3e 6e 43 6f 6c 75     &pInfo->nColu
21dad 6d 6e 2c 0a 20 20 20 20 20 20 20 26 70 49 6e 66  mn,.       &pInf
21dae 6f 2d 3e 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f 63 2c  o->nColumnAlloc,
21daf 0a 20 20 20 20 20 20 20 26 69 0a 20 20 29 3b 0a  .       &i.  );.
21db0 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 20 20 20    return i;.}   
21db1 20 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e   ../*.** Add a n
21db2 65 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68  ew element to th
21db3 65 20 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e  e pAggInfo->aFun
21db4 63 5b 5d 20 61 72 72 61 79 2e 20 20 52 65 74 75  c[] array.  Retu
21db5 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 0a  rn the index of.
21db6 2a 2a 20 74 68 65 20 6e 65 77 20 65 6c 65 6d 65  ** the new eleme
21db7 6e 74 2e 20 20 52 65 74 75 72 6e 20 61 20 6e 65  nt.  Return a ne
21db8 67 61 74 69 76 65 20 6e 75 6d 62 65 72 20 69 66  gative number if
21db9 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a   malloc fails..*
21dba 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 64 64  /.static int add
21dbb 41 67 67 49 6e 66 6f 46 75 6e 63 28 73 71 6c 69  AggInfoFunc(sqli
21dbc 74 65 33 20 2a 64 62 2c 20 41 67 67 49 6e 66 6f  te3 *db, AggInfo
21dbd 20 2a 70 49 6e 66 6f 29 7b 0a 20 20 69 6e 74 20   *pInfo){.  int 
21dbe 69 3b 0a 20 20 70 49 6e 66 6f 2d 3e 61 46 75 6e  i;.  pInfo->aFun
21dbf 63 20 3d 20 73 71 6c 69 74 65 33 41 72 72 61 79  c = sqlite3Array
21dc0 41 6c 6c 6f 63 61 74 65 28 0a 20 20 20 20 20 20  Allocate(.      
21dc1 20 64 62 2c 20 0a 20 20 20 20 20 20 20 70 49 6e   db, .       pIn
21dc2 66 6f 2d 3e 61 46 75 6e 63 2c 0a 20 20 20 20 20  fo->aFunc,.     
21dc3 20 20 73 69 7a 65 6f 66 28 70 49 6e 66 6f 2d 3e    sizeof(pInfo->
21dc4 61 46 75 6e 63 5b 30 5d 29 2c 0a 20 20 20 20 20  aFunc[0]),.     
21dc5 20 20 33 2c 0a 20 20 20 20 20 20 20 26 70 49 6e    3,.       &pIn
21dc6 66 6f 2d 3e 6e 46 75 6e 63 2c 0a 20 20 20 20 20  fo->nFunc,.     
21dc7 20 20 26 70 49 6e 66 6f 2d 3e 6e 46 75 6e 63 41    &pInfo->nFuncA
21dc8 6c 6c 6f 63 2c 0a 20 20 20 20 20 20 20 26 69 0a  lloc,.       &i.
21dc9 20 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b    );.  return i;
21dca 0a 7d 20 20 20 20 0a 0a 2f 2a 0a 2a 2a 20 54 68  .}    ../*.** Th
21dcb 69 73 20 69 73 20 74 68 65 20 78 45 78 70 72 43  is is the xExprC
21dcc 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61 20 74 72  allback for a tr
21dcd 65 65 20 77 61 6c 6b 65 72 2e 20 20 49 74 20 69  ee walker.  It i
21dce 73 20 75 73 65 64 20 74 6f 0a 2a 2a 20 69 6d 70  s used to.** imp
21dcf 6c 65 6d 65 6e 74 20 73 71 6c 69 74 65 33 45 78  lement sqlite3Ex
21dd0 70 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61  prAnalyzeAggrega
21dd1 74 65 73 28 29 2e 20 20 53 65 65 20 73 71 6c 69  tes().  See sqli
21dd2 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67  te3ExprAnalyzeAg
21dd3 67 72 65 67 61 74 65 73 0a 2a 2a 20 66 6f 72 20  gregates.** for 
21dd4 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  additional infor
21dd5 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  mation..*/.stati
21dd6 63 20 69 6e 74 20 61 6e 61 6c 79 7a 65 41 67 67  c int analyzeAgg
21dd7 72 65 67 61 74 65 28 57 61 6c 6b 65 72 20 2a 70  regate(Walker *p
21dd8 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45  Walker, Expr *pE
21dd9 78 70 72 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  xpr){.  int i;. 
21dda 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e   NameContext *pN
21ddb 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70  C = pWalker->u.p
21ddc 4e 43 3b 0a 20 20 50 61 72 73 65 20 2a 70 50 61  NC;.  Parse *pPa
21ddd 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73  rse = pNC->pPars
21dde 65 3b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53  e;.  SrcList *pS
21ddf 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53  rcList = pNC->pS
21de0 72 63 4c 69 73 74 3b 0a 20 20 41 67 67 49 6e 66  rcList;.  AggInf
21de1 6f 20 2a 70 41 67 67 49 6e 66 6f 20 3d 20 70 4e  o *pAggInfo = pN
21de2 43 2d 3e 70 41 67 67 49 6e 66 6f 3b 0a 0a 20 20  C->pAggInfo;..  
21de3 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f  switch( pExpr->o
21de4 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b  p ){.    case TK
21de5 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20  _AGG_COLUMN:.   
21de6 20 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a   case TK_COLUMN:
21de7 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73   {.      testcas
21de8 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  e( pExpr->op==TK
21de9 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20  _AGG_COLUMN );. 
21dea 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
21deb 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c  Expr->op==TK_COL
21dec 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20  UMN );.      /* 
21ded 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  Check to see if 
21dee 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 69 6e  the column is in
21def 20 6f 6e 65 20 6f 66 20 74 68 65 20 74 61 62 6c   one of the tabl
21df0 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 0a 20  es in the FROM. 
21df1 20 20 20 20 20 2a 2a 20 63 6c 61 75 73 65 20 6f       ** clause o
21df2 66 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20  f the aggregate 
21df3 71 75 65 72 79 20 2a 2f 0a 20 20 20 20 20 20 69  query */.      i
21df4 66 28 20 41 4c 57 41 59 53 28 70 53 72 63 4c 69  f( ALWAYS(pSrcLi
21df5 73 74 21 3d 30 29 20 29 7b 0a 20 20 20 20 20 20  st!=0) ){.      
21df6 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
21df7 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 70  _item *pItem = p
21df8 53 72 63 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20  SrcList->a;.    
21df9 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
21dfa 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69  SrcList->nSrc; i
21dfb 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20  ++, pItem++){.  
21dfc 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20 41          struct A
21dfd 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70 43 6f 6c  ggInfo_col *pCol
21dfe 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  ;.          asse
21dff 72 74 28 20 21 45 78 70 72 48 61 73 41 6e 79 50  rt( !ExprHasAnyP
21e00 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
21e01 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 52  P_TokenOnly|EP_R
21e02 65 64 75 63 65 64 29 20 29 3b 0a 20 20 20 20 20  educed) );.     
21e03 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e       if( pExpr->
21e04 69 54 61 62 6c 65 3d 3d 70 49 74 65 6d 2d 3e 69  iTable==pItem->i
21e05 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 20 20  Cursor ){.      
21e06 20 20 20 20 20 20 2f 2a 20 49 66 20 77 65 20 72        /* If we r
21e07 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74 2c  each this point,
21e08 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 70   it means that p
21e09 45 78 70 72 20 72 65 66 65 72 73 20 74 6f 20 61  Expr refers to a
21e0a 20 74 61 62 6c 65 0a 20 20 20 20 20 20 20 20 20   table.         
21e0b 20 20 20 2a 2a 20 74 68 61 74 20 69 73 20 69 6e     ** that is in
21e0c 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
21e0d 20 6f 66 20 74 68 65 20 61 67 67 72 65 67 61 74   of the aggregat
21e0e 65 20 71 75 65 72 79 2e 20 20 0a 20 20 20 20 20  e query.  .     
21e0f 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20         **.      
21e10 20 20 20 20 20 20 2a 2a 20 4d 61 6b 65 20 61 6e        ** Make an
21e11 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 63   entry for the c
21e12 6f 6c 75 6d 6e 20 69 6e 20 70 41 67 67 49 6e 66  olumn in pAggInf
21e13 6f 2d 3e 61 43 6f 6c 5b 5d 20 69 66 20 74 68 65  o->aCol[] if the
21e14 72 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  re.            *
21e15 2a 20 69 73 20 6e 6f 74 20 61 6e 20 65 6e 74 72  * is not an entr
21e16 79 20 74 68 65 72 65 20 61 6c 72 65 61 64 79 2e  y there already.
21e17 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  .            */.
21e18 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20              int 
21e19 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  k;.            p
21e1a 43 6f 6c 20 3d 20 70 41 67 67 49 6e 66 6f 2d 3e  Col = pAggInfo->
21e1b 61 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20  aCol;.          
21e1c 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 41 67    for(k=0; k<pAg
21e1d 67 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20  gInfo->nColumn; 
21e1e 6b 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20  k++, pCol++){.  
21e1f 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
21e20 70 43 6f 6c 2d 3e 69 54 61 62 6c 65 3d 3d 70 45  pCol->iTable==pE
21e21 78 70 72 2d 3e 69 54 61 62 6c 65 20 26 26 0a 20  xpr->iTable &&. 
21e22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21e23 20 70 43 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d   pCol->iColumn==
21e24 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 29  pExpr->iColumn )
21e25 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
21e26 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
21e27 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
21e28 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
21e29 20 20 20 69 66 28 20 28 6b 3e 3d 70 41 67 67 49     if( (k>=pAggI
21e2a 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 29 0a 20 20  nfo->nColumn).  
21e2b 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28 6b             && (k
21e2c 20 3d 20 61 64 64 41 67 67 49 6e 66 6f 43 6f 6c   = addAggInfoCol
21e2d 75 6d 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  umn(pParse->db, 
21e2e 70 41 67 67 49 6e 66 6f 29 29 3e 3d 30 20 0a 20  pAggInfo))>=0 . 
21e2f 20 20 20 20 20 20 20 20 20 20 20 29 7b 0a 20 20             ){.  
21e30 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c              pCol
21e31 20 3d 20 26 70 41 67 67 49 6e 66 6f 2d 3e 61 43   = &pAggInfo->aC
21e32 6f 6c 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 20 20  ol[k];.         
21e33 20 20 20 20 20 70 43 6f 6c 2d 3e 70 54 61 62 20       pCol->pTab 
21e34 3d 20 70 45 78 70 72 2d 3e 70 54 61 62 3b 0a 20  = pExpr->pTab;. 
21e35 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f               pCo
21e36 6c 2d 3e 69 54 61 62 6c 65 20 3d 20 70 45 78 70  l->iTable = pExp
21e37 72 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20 20  r->iTable;.     
21e38 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69           pCol->i
21e39 43 6f 6c 75 6d 6e 20 3d 20 70 45 78 70 72 2d 3e  Column = pExpr->
21e3a 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 20  iColumn;.       
21e3b 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 4d 65         pCol->iMe
21e3c 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  m = ++pParse->nM
21e3d 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  em;.            
21e3e 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43    pCol->iSorterC
21e3f 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20  olumn = -1;.    
21e40 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e            pCol->
21e41 70 45 78 70 72 20 3d 20 70 45 78 70 72 3b 0a 20  pExpr = pExpr;. 
21e42 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28               if(
21e43 20 70 41 67 67 49 6e 66 6f 2d 3e 70 47 72 6f 75   pAggInfo->pGrou
21e44 70 42 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20  pBy ){.         
21e45 20 20 20 20 20 20 20 69 6e 74 20 6a 2c 20 6e 3b         int j, n;
21e46 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
21e47 20 45 78 70 72 4c 69 73 74 20 2a 70 47 42 20 3d   ExprList *pGB =
21e48 20 70 41 67 67 49 6e 66 6f 2d 3e 70 47 72 6f 75   pAggInfo->pGrou
21e49 70 42 79 3b 0a 20 20 20 20 20 20 20 20 20 20 20  pBy;.           
21e4a 20 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72       struct Expr
21e4b 4c 69 73 74 5f 69 74 65 6d 20 2a 70 54 65 72 6d  List_item *pTerm
21e4c 20 3d 20 70 47 42 2d 3e 61 3b 0a 20 20 20 20 20   = pGB->a;.     
21e4d 20 20 20 20 20 20 20 20 20 20 20 6e 20 3d 20 70             n = p
21e4e 47 42 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20  GB->nExpr;.     
21e4f 20 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6a             for(j
21e50 3d 30 3b 20 6a 3c 6e 3b 20 6a 2b 2b 2c 20 70 54  =0; j<n; j++, pT
21e51 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  erm++){.        
21e52 20 20 20 20 20 20 20 20 20 20 45 78 70 72 20 2a            Expr *
21e53 70 45 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70  pE = pTerm->pExp
21e54 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r;.             
21e55 20 20 20 20 20 69 66 28 20 70 45 2d 3e 6f 70 3d       if( pE->op=
21e56 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 70 45  =TK_COLUMN && pE
21e57 2d 3e 69 54 61 62 6c 65 3d 3d 70 45 78 70 72 2d  ->iTable==pExpr-
21e58 3e 69 54 61 62 6c 65 20 26 26 0a 20 20 20 20 20  >iTable &&.     
21e59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21e5a 20 70 45 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 70 45   pE->iColumn==pE
21e5b 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 29 7b 0a  xpr->iColumn ){.
21e5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21e5d 20 20 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65      pCol->iSorte
21e5e 72 43 6f 6c 75 6d 6e 20 3d 20 6a 3b 0a 20 20 20  rColumn = j;.   
21e5f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21e60 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
21e61 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
21e62 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
21e63 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
21e64 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
21e65 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c  pCol->iSorterCol
21e66 75 6d 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 20  umn<0 ){.       
21e67 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69           pCol->i
21e68 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 20 3d 20 70  SorterColumn = p
21e69 41 67 67 49 6e 66 6f 2d 3e 6e 53 6f 72 74 69 6e  AggInfo->nSortin
21e6a 67 43 6f 6c 75 6d 6e 2b 2b 3b 0a 20 20 20 20 20  gColumn++;.     
21e6b 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
21e6c 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
21e6d 20 20 20 20 20 2f 2a 20 54 68 65 72 65 20 69 73       /* There is
21e6e 20 6e 6f 77 20 61 6e 20 65 6e 74 72 79 20 66 6f   now an entry fo
21e6f 72 20 70 45 78 70 72 20 69 6e 20 70 41 67 67 49  r pExpr in pAggI
21e70 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 28 65 69 74  nfo->aCol[] (eit
21e71 68 65 72 0a 20 20 20 20 20 20 20 20 20 20 20 20  her.            
21e72 2a 2a 20 62 65 63 61 75 73 65 20 69 74 20 77 61  ** because it wa
21e73 73 20 74 68 65 72 65 20 62 65 66 6f 72 65 20 6f  s there before o
21e74 72 20 62 65 63 61 75 73 65 20 77 65 20 6a 75 73  r because we jus
21e75 74 20 63 72 65 61 74 65 64 20 69 74 29 2e 0a 20  t created it).. 
21e76 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 43 6f             ** Co
21e77 6e 76 65 72 74 20 74 68 65 20 70 45 78 70 72 20  nvert the pExpr 
21e78 74 6f 20 62 65 20 61 20 54 4b 5f 41 47 47 5f 43  to be a TK_AGG_C
21e79 4f 4c 55 4d 4e 20 72 65 66 65 72 72 69 6e 67 20  OLUMN referring 
21e7a 74 6f 20 74 68 61 74 0a 20 20 20 20 20 20 20 20  to that.        
21e7b 20 20 20 20 2a 2a 20 70 41 67 67 49 6e 66 6f 2d      ** pAggInfo-
21e7c 3e 61 43 6f 6c 5b 5d 20 65 6e 74 72 79 2e 0a 20  >aCol[] entry.. 
21e7d 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20             */.  
21e7e 20 20 20 20 20 20 20 20 20 20 45 78 70 72 53 65            ExprSe
21e7f 74 49 72 72 65 64 75 63 69 62 6c 65 28 70 45 78  tIrreducible(pEx
21e80 70 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  pr);.           
21e81 20 70 45 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f   pExpr->pAggInfo
21e82 20 3d 20 70 41 67 67 49 6e 66 6f 3b 0a 20 20 20   = pAggInfo;.   
21e83 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e           pExpr->
21e84 6f 70 20 3d 20 54 4b 5f 41 47 47 5f 43 4f 4c 55  op = TK_AGG_COLU
21e85 4d 4e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  MN;.            
21e86 70 45 78 70 72 2d 3e 69 41 67 67 20 3d 20 28 69  pExpr->iAgg = (i
21e87 31 36 29 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  16)k;.          
21e88 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
21e89 20 20 20 7d 20 2f 2a 20 65 6e 64 69 66 20 70 45     } /* endif pE
21e8a 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 70 49 74  xpr->iTable==pIt
21e8b 65 6d 2d 3e 69 43 75 72 73 6f 72 20 2a 2f 0a 20  em->iCursor */. 
21e8c 20 20 20 20 20 20 20 7d 20 2f 2a 20 65 6e 64 20         } /* end 
21e8d 6c 6f 6f 70 20 6f 76 65 72 20 70 53 72 63 4c 69  loop over pSrcLi
21e8e 73 74 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20  st */.      }.  
21e8f 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50      return WRC_P
21e90 72 75 6e 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rune;.    }.    
21e91 63 61 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43  case TK_AGG_FUNC
21e92 54 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 2f 2a  TION: {.      /*
21e93 20 54 68 65 20 70 4e 43 2d 3e 6e 44 65 70 74 68   The pNC->nDepth
21e94 3d 3d 30 20 74 65 73 74 20 63 61 75 73 65 73 20  ==0 test causes 
21e95 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
21e96 6f 6e 73 20 69 6e 20 73 75 62 71 75 65 72 69 65  ons in subquerie
21e97 73 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 62 65  s.      ** to be
21e98 20 69 67 6e 6f 72 65 64 20 2a 2f 0a 20 20 20 20   ignored */.    
21e99 20 20 69 66 28 20 70 4e 43 2d 3e 6e 44 65 70 74    if( pNC->nDept
21e9a 68 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  h==0 ){.        
21e9b 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20  /* Check to see 
21e9c 69 66 20 70 45 78 70 72 20 69 73 20 61 20 64 75  if pExpr is a du
21e9d 70 6c 69 63 61 74 65 20 6f 66 20 61 6e 6f 74 68  plicate of anoth
21e9e 65 72 20 61 67 67 72 65 67 61 74 65 20 0a 20 20  er aggregate .  
21e9f 20 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f        ** functio
21ea0 6e 20 74 68 61 74 20 69 73 20 61 6c 72 65 61 64  n that is alread
21ea1 79 20 69 6e 20 74 68 65 20 70 41 67 67 49 6e 66  y in the pAggInf
21ea2 6f 20 73 74 72 75 63 74 75 72 65 0a 20 20 20 20  o structure.    
21ea3 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 73      */.        s
21ea4 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75  truct AggInfo_fu
21ea5 6e 63 20 2a 70 49 74 65 6d 20 3d 20 70 41 67 67  nc *pItem = pAgg
21ea6 49 6e 66 6f 2d 3e 61 46 75 6e 63 3b 0a 20 20 20  Info->aFunc;.   
21ea7 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
21ea8 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 3b  pAggInfo->nFunc;
21ea9 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a   i++, pItem++){.
21eaa 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71            if( sq
21eab 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65  lite3ExprCompare
21eac 28 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 70  (pItem->pExpr, p
21ead 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 20  Expr) ){.       
21eae 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
21eaf 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
21eb0 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 3e  }.        if( i>
21eb1 3d 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63  =pAggInfo->nFunc
21eb2 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a   ){.          /*
21eb3 20 70 45 78 70 72 20 69 73 20 6f 72 69 67 69 6e   pExpr is origin
21eb4 61 6c 2e 20 20 4d 61 6b 65 20 61 20 6e 65 77 20  al.  Make a new 
21eb5 65 6e 74 72 79 20 69 6e 20 70 41 67 67 49 6e 66  entry in pAggInf
21eb6 6f 2d 3e 61 46 75 6e 63 5b 5d 0a 20 20 20 20 20  o->aFunc[].     
21eb7 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
21eb8 20 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 70    u8 enc = ENC(p
21eb9 50 61 72 73 65 2d 3e 64 62 29 3b 0a 20 20 20 20  Parse->db);.    
21eba 20 20 20 20 20 20 69 20 3d 20 61 64 64 41 67 67        i = addAgg
21ebb 49 6e 66 6f 46 75 6e 63 28 70 50 61 72 73 65 2d  InfoFunc(pParse-
21ebc 3e 64 62 2c 20 70 41 67 67 49 6e 66 6f 29 3b 0a  >db, pAggInfo);.
21ebd 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 3e            if( i>
21ebe 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
21ebf 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48    assert( !ExprH
21ec0 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
21ec1 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20  , EP_xIsSelect) 
21ec2 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  );.            p
21ec3 49 74 65 6d 20 3d 20 26 70 41 67 67 49 6e 66 6f  Item = &pAggInfo
21ec4 2d 3e 61 46 75 6e 63 5b 69 5d 3b 0a 20 20 20 20  ->aFunc[i];.    
21ec5 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 70          pItem->p
21ec6 45 78 70 72 20 3d 20 70 45 78 70 72 3b 0a 20 20  Expr = pExpr;.  
21ec7 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d            pItem-
21ec8 3e 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65  >iMem = ++pParse
21ec9 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20  ->nMem;.        
21eca 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70      assert( !Exp
21ecb 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78  rHasProperty(pEx
21ecc 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29  pr, EP_IntValue)
21ecd 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   );.            
21ece 70 49 74 65 6d 2d 3e 70 46 75 6e 63 20 3d 20 73  pItem->pFunc = s
21ecf 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69  qlite3FindFuncti
21ed0 6f 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 0a 20  on(pParse->db,. 
21ed1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21ed2 20 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65    pExpr->u.zToke
21ed3 6e 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  n, sqlite3Strlen
21ed4 33 30 28 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b  30(pExpr->u.zTok
21ed5 65 6e 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  en),.           
21ed6 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 78          pExpr->x
21ed7 2e 70 4c 69 73 74 20 3f 20 70 45 78 70 72 2d 3e  .pList ? pExpr->
21ed8 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3a  x.pList->nExpr :
21ed9 20 30 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20 20   0, enc, 0);.   
21eda 20 20 20 20 20 20 20 20 20 69 66 28 20 70 45 78           if( pEx
21edb 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 44  pr->flags & EP_D
21edc 69 73 74 69 6e 63 74 20 29 7b 0a 20 20 20 20 20  istinct ){.     
21edd 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e           pItem->
21ede 69 44 69 73 74 69 6e 63 74 20 3d 20 70 50 61 72  iDistinct = pPar
21edf 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20  se->nTab++;.    
21ee0 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
21ee1 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49 74               pIt
21ee2 65 6d 2d 3e 69 44 69 73 74 69 6e 63 74 20 3d 20  em->iDistinct = 
21ee3 2d 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  -1;.            
21ee4 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  }.          }.  
21ee5 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
21ee6 2f 2a 20 4d 61 6b 65 20 70 45 78 70 72 20 70 6f  /* Make pExpr po
21ee7 69 6e 74 20 74 6f 20 74 68 65 20 61 70 70 72 6f  int to the appro
21ee8 70 72 69 61 74 65 20 70 41 67 67 49 6e 66 6f 2d  priate pAggInfo-
21ee9 3e 61 46 75 6e 63 5b 5d 20 65 6e 74 72 79 0a 20  >aFunc[] entry. 
21eea 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
21eeb 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48    assert( !ExprH
21eec 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45  asAnyProperty(pE
21eed 78 70 72 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c  xpr, EP_TokenOnl
21eee 79 7c 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b  y|EP_Reduced) );
21eef 0a 20 20 20 20 20 20 20 20 45 78 70 72 53 65 74  .        ExprSet
21ef0 49 72 72 65 64 75 63 69 62 6c 65 28 70 45 78 70  Irreducible(pExp
21ef1 72 29 3b 0a 20 20 20 20 20 20 20 20 70 45 78 70  r);.        pExp
21ef2 72 2d 3e 69 41 67 67 20 3d 20 28 69 31 36 29 69  r->iAgg = (i16)i
21ef3 3b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d  ;.        pExpr-
21ef4 3e 70 41 67 67 49 6e 66 6f 20 3d 20 70 41 67 67  >pAggInfo = pAgg
21ef5 49 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 72 65  Info;.        re
21ef6 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a  turn WRC_Prune;.
21ef7 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
21ef8 7d 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43  }.  return WRC_C
21ef9 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 73 74 61 74 69  ontinue;.}.stati
21efa 63 20 69 6e 74 20 61 6e 61 6c 79 7a 65 41 67 67  c int analyzeAgg
21efb 72 65 67 61 74 65 73 49 6e 53 65 6c 65 63 74 28  regatesInSelect(
21efc 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c  Walker *pWalker,
21efd 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
21efe 29 7b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74  ){.  NameContext
21eff 20 2a 70 4e 43 20 3d 20 70 57 61 6c 6b 65 72 2d   *pNC = pWalker-
21f00 3e 75 2e 70 4e 43 3b 0a 20 20 69 66 28 20 70 4e  >u.pNC;.  if( pN
21f01 43 2d 3e 6e 44 65 70 74 68 3d 3d 30 20 29 7b 0a  C->nDepth==0 ){.
21f02 20 20 20 20 70 4e 43 2d 3e 6e 44 65 70 74 68 2b      pNC->nDepth+
21f03 2b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 57 61  +;.    sqlite3Wa
21f04 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b 65 72  lkSelect(pWalker
21f05 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20  , pSelect);.    
21f06 70 4e 43 2d 3e 6e 44 65 70 74 68 2d 2d 3b 0a 20  pNC->nDepth--;. 
21f07 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72     return WRC_Pr
21f08 75 6e 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  une;.  }else{.  
21f09 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e    return WRC_Con
21f0a 74 69 6e 75 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  tinue;.  }.}../*
21f0b 0a 2a 2a 20 41 6e 61 6c 79 7a 65 20 74 68 65 20  .** Analyze the 
21f0c 67 69 76 65 6e 20 65 78 70 72 65 73 73 69 6f 6e  given expression
21f0d 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 67 67   looking for agg
21f0e 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73  regate functions
21f0f 20 61 6e 64 0a 2a 2a 20 66 6f 72 20 76 61 72 69   and.** for vari
21f10 61 62 6c 65 73 20 74 68 61 74 20 6e 65 65 64 20  ables that need 
21f11 74 6f 20 62 65 20 61 64 64 65 64 20 74 6f 20 74  to be added to t
21f12 68 65 20 70 50 61 72 73 65 2d 3e 61 41 67 67 5b  he pParse->aAgg[
21f13 5d 20 61 72 72 61 79 2e 0a 2a 2a 20 4d 61 6b 65  ] array..** Make
21f14 20 61 64 64 69 74 69 6f 6e 61 6c 20 65 6e 74 72   additional entr
21f15 69 65 73 20 74 6f 20 74 68 65 20 70 50 61 72 73  ies to the pPars
21f16 65 2d 3e 61 41 67 67 5b 5d 20 61 72 72 61 79 20  e->aAgg[] array 
21f17 61 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a  as necessary..**
21f18 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
21f19 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 20   should only be 
21f1a 63 61 6c 6c 65 64 20 61 66 74 65 72 20 74 68 65  called after the
21f1b 20 65 78 70 72 65 73 73 69 6f 6e 20 68 61 73 20   expression has 
21f1c 62 65 65 6e 0a 2a 2a 20 61 6e 61 6c 79 7a 65 64  been.** analyzed
21f1d 20 62 79 20 73 71 6c 69 74 65 33 52 65 73 6f 6c   by sqlite3Resol
21f1e 76 65 45 78 70 72 4e 61 6d 65 73 28 29 2e 0a 2a  veExprNames()..*
21f1f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
21f20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
21f21 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74  rAnalyzeAggregat
21f22 65 73 28 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a  es(NameContext *
21f23 70 4e 43 2c 20 45 78 70 72 20 2a 70 45 78 70 72  pNC, Expr *pExpr
21f24 29 7b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a 20  ){.  Walker w;. 
21f25 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b   w.xExprCallback
21f26 20 3d 20 61 6e 61 6c 79 7a 65 41 67 67 72 65 67   = analyzeAggreg
21f27 61 74 65 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74  ate;.  w.xSelect
21f28 43 61 6c 6c 62 61 63 6b 20 3d 20 61 6e 61 6c 79  Callback = analy
21f29 7a 65 41 67 67 72 65 67 61 74 65 73 49 6e 53 65  zeAggregatesInSe
21f2a 6c 65 63 74 3b 0a 20 20 77 2e 75 2e 70 4e 43 20  lect;.  w.u.pNC 
21f2b 3d 20 70 4e 43 3b 0a 20 20 61 73 73 65 72 74 28  = pNC;.  assert(
21f2c 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 21 3d   pNC->pSrcList!=
21f2d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 57 61  0 );.  sqlite3Wa
21f2e 6c 6b 45 78 70 72 28 26 77 2c 20 70 45 78 70 72  lkExpr(&w, pExpr
21f2f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c  );.}../*.** Call
21f30 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c   sqlite3ExprAnal
21f31 79 7a 65 41 67 67 72 65 67 61 74 65 73 28 29 20  yzeAggregates() 
21f32 66 6f 72 20 65 76 65 72 79 20 65 78 70 72 65 73  for every expres
21f33 73 69 6f 6e 20 69 6e 20 61 6e 0a 2a 2a 20 65 78  sion in an.** ex
21f34 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 20 20  pression list.  
21f35 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
21f36 72 20 6f 66 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a  r of errors..**.
21f37 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 69  ** If an error i
21f38 73 20 66 6f 75 6e 64 2c 20 74 68 65 20 61 6e 61  s found, the ana
21f39 6c 79 73 69 73 20 69 73 20 63 75 74 20 73 68 6f  lysis is cut sho
21f3a 72 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  rt..*/.SQLITE_PR
21f3b 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
21f3c 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67  e3ExprAnalyzeAgg
21f3d 4c 69 73 74 28 4e 61 6d 65 43 6f 6e 74 65 78 74  List(NameContext
21f3e 20 2a 70 4e 43 2c 20 45 78 70 72 4c 69 73 74 20   *pNC, ExprList 
21f3f 2a 70 4c 69 73 74 29 7b 0a 20 20 73 74 72 75 63  *pList){.  struc
21f40 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
21f41 2a 70 49 74 65 6d 3b 0a 20 20 69 6e 74 20 69 3b  *pItem;.  int i;
21f42 0a 20 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a  .  if( pList ){.
21f43 20 20 20 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c      for(pItem=pL
21f44 69 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 70  ist->a, i=0; i<p
21f45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b  List->nExpr; i++
21f46 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20  , pItem++){.    
21f47 20 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61    sqlite3ExprAna
21f48 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73 28 70  lyzeAggregates(p
21f49 4e 43 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72  NC, pItem->pExpr
21f4a 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  );.    }.  }.}..
21f4b 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61  /*.** Allocate a
21f4c 20 73 69 6e 67 6c 65 20 6e 65 77 20 72 65 67 69   single new regi
21f4d 73 74 65 72 20 66 6f 72 20 75 73 65 20 74 6f 20  ster for use to 
21f4e 68 6f 6c 64 20 73 6f 6d 65 20 69 6e 74 65 72 6d  hold some interm
21f4f 65 64 69 61 74 65 20 72 65 73 75 6c 74 2e 0a 2a  ediate result..*
21f50 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
21f51 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54   int sqlite3GetT
21f52 65 6d 70 52 65 67 28 50 61 72 73 65 20 2a 70 50  empReg(Parse *pP
21f53 61 72 73 65 29 7b 0a 20 20 69 66 28 20 70 50 61  arse){.  if( pPa
21f54 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 3d 3d 30  rse->nTempReg==0
21f55 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 2b   ){.    return +
21f56 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
21f57 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 50 61 72   }.  return pPar
21f58 73 65 2d 3e 61 54 65 6d 70 52 65 67 5b 2d 2d 70  se->aTempReg[--p
21f59 50 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 5d  Parse->nTempReg]
21f5a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 61 6c 6c  ;.}../*.** Deall
21f5b 6f 63 61 74 65 20 61 20 72 65 67 69 73 74 65 72  ocate a register
21f5c 2c 20 6d 61 6b 69 6e 67 20 61 76 61 69 6c 61 62  , making availab
21f5d 6c 65 20 66 6f 72 20 72 65 75 73 65 20 66 6f 72  le for reuse for
21f5e 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 70   some other.** p
21f5f 75 72 70 6f 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  urpose..**.** If
21f60 20 61 20 72 65 67 69 73 74 65 72 20 69 73 20 63   a register is c
21f61 75 72 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 75  urrently being u
21f62 73 65 64 20 62 79 20 74 68 65 20 63 6f 6c 75 6d  sed by the colum
21f63 6e 20 63 61 63 68 65 2c 20 74 68 65 6e 0a 2a 2a  n cache, then.**
21f64 20 74 68 65 20 64 61 6c 6c 6f 63 61 74 69 6f 6e   the dallocation
21f65 20 69 73 20 64 65 66 65 72 72 65 64 20 75 6e 74   is deferred unt
21f66 69 6c 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61  il the column ca
21f67 63 68 65 20 6c 69 6e 65 20 74 68 61 74 20 75 73  che line that us
21f68 65 73 0a 2a 2a 20 74 68 65 20 72 65 67 69 73 74  es.** the regist
21f69 65 72 20 62 65 63 6f 6d 65 73 20 73 74 61 6c 65  er becomes stale
21f6a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
21f6b 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
21f6c 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 50  ReleaseTempReg(P
21f6d 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e  arse *pParse, in
21f6e 74 20 69 52 65 67 29 7b 0a 20 20 69 66 28 20 69  t iReg){.  if( i
21f6f 52 65 67 20 26 26 20 70 50 61 72 73 65 2d 3e 6e  Reg && pParse->n
21f70 54 65 6d 70 52 65 67 3c 41 72 72 61 79 53 69 7a  TempReg<ArraySiz
21f71 65 28 70 50 61 72 73 65 2d 3e 61 54 65 6d 70 52  e(pParse->aTempR
21f72 65 67 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  eg) ){.    int i
21f73 3b 0a 20 20 20 20 73 74 72 75 63 74 20 79 43 6f  ;.    struct yCo
21f74 6c 43 61 63 68 65 20 2a 70 3b 0a 20 20 20 20 66  lCache *p;.    f
21f75 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65  or(i=0, p=pParse
21f76 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53  ->aColCache; i<S
21f77 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45  QLITE_N_COLCACHE
21f78 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20  ; i++, p++){.   
21f79 20 20 20 69 66 28 20 70 2d 3e 69 52 65 67 3d 3d     if( p->iReg==
21f7a 69 52 65 67 20 29 7b 0a 20 20 20 20 20 20 20 20  iReg ){.        
21f7b 70 2d 3e 74 65 6d 70 52 65 67 20 3d 20 31 3b 0a  p->tempReg = 1;.
21f7c 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a          return;.
21f7d 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
21f7e 20 20 70 50 61 72 73 65 2d 3e 61 54 65 6d 70 52    pParse->aTempR
21f7f 65 67 5b 70 50 61 72 73 65 2d 3e 6e 54 65 6d 70  eg[pParse->nTemp
21f80 52 65 67 2b 2b 5d 20 3d 20 69 52 65 67 3b 0a 20  Reg++] = iReg;. 
21f81 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f   }.}../*.** Allo
21f82 63 61 74 65 20 6f 72 20 64 65 61 6c 6c 6f 63 61  cate or dealloca
21f83 74 65 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6e 52  te a block of nR
21f84 65 67 20 63 6f 6e 73 65 63 75 74 69 76 65 20 72  eg consecutive r
21f85 65 67 69 73 74 65 72 73 0a 2a 2f 0a 53 51 4c 49  egisters.*/.SQLI
21f86 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
21f87 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e  qlite3GetTempRan
21f88 67 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ge(Parse *pParse
21f89 2c 20 69 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69  , int nReg){.  i
21f8a 6e 74 20 69 2c 20 6e 3b 0a 20 20 69 20 3d 20 70  nt i, n;.  i = p
21f8b 50 61 72 73 65 2d 3e 69 52 61 6e 67 65 52 65 67  Parse->iRangeReg
21f8c 3b 0a 20 20 6e 20 3d 20 70 50 61 72 73 65 2d 3e  ;.  n = pParse->
21f8d 6e 52 61 6e 67 65 52 65 67 3b 0a 20 20 69 66 28  nRangeReg;.  if(
21f8e 20 6e 52 65 67 3c 3d 6e 20 26 26 20 21 75 73 65   nReg<=n && !use
21f8f 64 41 73 43 6f 6c 75 6d 6e 43 61 63 68 65 28 70  dAsColumnCache(p
21f90 50 61 72 73 65 2c 20 69 2c 20 69 2b 6e 2d 31 29  Parse, i, i+n-1)
21f91 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e   ){.    pParse->
21f92 69 52 61 6e 67 65 52 65 67 20 2b 3d 20 6e 52 65  iRangeReg += nRe
21f93 67 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  g;.    pParse->n
21f94 52 61 6e 67 65 52 65 67 20 2d 3d 20 6e 52 65 67  RangeReg -= nReg
21f95 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  ;.  }else{.    i
21f96 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b   = pParse->nMem+
21f97 31 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  1;.    pParse->n
21f98 4d 65 6d 20 2b 3d 20 6e 52 65 67 3b 0a 20 20 7d  Mem += nReg;.  }
21f99 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 53  .  return i;.}.S
21f9a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
21f9b 69 64 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  id sqlite3Releas
21f9c 65 54 65 6d 70 52 61 6e 67 65 28 50 61 72 73 65  eTempRange(Parse
21f9d 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 52   *pParse, int iR
21f9e 65 67 2c 20 69 6e 74 20 6e 52 65 67 29 7b 0a 20  eg, int nReg){. 
21f9f 20 69 66 28 20 6e 52 65 67 3e 70 50 61 72 73 65   if( nReg>pParse
21fa0 2d 3e 6e 52 61 6e 67 65 52 65 67 20 29 7b 0a 20  ->nRangeReg ){. 
21fa1 20 20 20 70 50 61 72 73 65 2d 3e 6e 52 61 6e 67     pParse->nRang
21fa2 65 52 65 67 20 3d 20 6e 52 65 67 3b 0a 20 20 20  eReg = nReg;.   
21fa3 20 70 50 61 72 73 65 2d 3e 69 52 61 6e 67 65 52   pParse->iRangeR
21fa4 65 67 20 3d 20 69 52 65 67 3b 0a 20 20 7d 0a 7d  eg = iReg;.  }.}
21fa5 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
21fa6 2a 20 45 6e 64 20 6f 66 20 65 78 70 72 2e 63 20  * End of expr.c 
21fa7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21fa8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21fa9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21faa 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
21fab 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 61 6c 74  * Begin file alt
21fac 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  er.c ***********
21fad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21fae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21faf 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 46 65 62  /./*.** 2005 Feb
21fb0 72 75 61 72 79 20 31 35 0a 2a 2a 0a 2a 2a 20 54  ruary 15.**.** T
21fb1 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
21fb2 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
21fb3 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
21fb4 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
21fb5 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
21fb6 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
21fb7 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
21fb8 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
21fb9 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
21fba 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
21fbb 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
21fbc 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
21fbd 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
21fbe 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
21fbf 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
21fc0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
21fc1 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
21fc2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21fc3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21fc4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21fc5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
21fc6 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
21fc7 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20  file contains C 
21fc8 63 6f 64 65 20 72 6f 75 74 69 6e 65 73 20 74 68  code routines th
21fc9 61 74 20 75 73 65 64 20 74 6f 20 67 65 6e 65 72  at used to gener
21fca 61 74 65 20 56 44 42 45 20 63 6f 64 65 0a 2a 2a  ate VDBE code.**
21fcb 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73   that implements
21fcc 20 74 68 65 20 41 4c 54 45 52 20 54 41 42 4c 45   the ALTER TABLE
21fcd 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20   command..**.** 
21fce 24 49 64 3a 20 61 6c 74 65 72 2e 63 2c 76 20 31  $Id: alter.c,v 1
21fcf 2e 36 32 20 32 30 30 39 2f 30 37 2f 32 34 20 31  .62 2009/07/24 1
21fd0 37 3a 35 38 3a 35 33 20 64 61 6e 69 65 6c 6b 31  7:58:53 danielk1
21fd1 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  977 Exp $.*/../*
21fd2 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20  .** The code in 
21fd3 74 68 69 73 20 66 69 6c 65 20 6f 6e 6c 79 20 65  this file only e
21fd4 78 69 73 74 73 20 69 66 20 77 65 20 61 72 65 20  xists if we are 
21fd5 6e 6f 74 20 6f 6d 69 74 74 69 6e 67 20 74 68 65  not omitting the
21fd6 0a 2a 2a 20 41 4c 54 45 52 20 54 41 42 4c 45 20  .** ALTER TABLE 
21fd7 6c 6f 67 69 63 20 66 72 6f 6d 20 74 68 65 20 62  logic from the b
21fd8 75 69 6c 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  uild..*/.#ifndef
21fd9 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54   SQLITE_OMIT_ALT
21fda 45 52 54 41 42 4c 45 0a 0a 0a 2f 2a 0a 2a 2a 20  ERTABLE.../*.** 
21fdb 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
21fdc 20 75 73 65 64 20 62 79 20 53 51 4c 20 67 65 6e   used by SQL gen
21fdd 65 72 61 74 65 64 20 74 6f 20 69 6d 70 6c 65 6d  erated to implem
21fde 65 6e 74 20 74 68 65 20 0a 2a 2a 20 41 4c 54 45  ent the .** ALTE
21fdf 52 20 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 2e  R TABLE command.
21fe0 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   The first argum
21fe1 65 6e 74 20 69 73 20 74 68 65 20 74 65 78 74 20  ent is the text 
21fe2 6f 66 20 61 20 43 52 45 41 54 45 20 54 41 42 4c  of a CREATE TABL
21fe3 45 20 6f 72 0a 2a 2a 20 43 52 45 41 54 45 20 49  E or.** CREATE I
21fe4 4e 44 45 58 20 63 6f 6d 6d 61 6e 64 2e 20 54 68  NDEX command. Th
21fe5 65 20 73 65 63 6f 6e 64 20 69 73 20 61 20 74 61  e second is a ta
21fe6 62 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 74 61  ble name. The ta
21fe7 62 6c 65 20 6e 61 6d 65 20 69 6e 20 0a 2a 2a 20  ble name in .** 
21fe8 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45  the CREATE TABLE
21fe9 20 6f 72 20 43 52 45 41 54 45 20 49 4e 44 45 58   or CREATE INDEX
21fea 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 72 65   statement is re
21feb 70 6c 61 63 65 64 20 77 69 74 68 20 74 68 65 20  placed with the 
21fec 74 68 69 72 64 0a 2a 2a 20 61 72 67 75 6d 65 6e  third.** argumen
21fed 74 20 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74  t and the result
21fee 20 72 65 74 75 72 6e 65 64 2e 20 45 78 61 6d 70   returned. Examp
21fef 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74  les:.**.** sqlit
21ff0 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28 27  e_rename_table('
21ff1 43 52 45 41 54 45 20 54 41 42 4c 45 20 61 62 63  CREATE TABLE abc
21ff2 28 61 2c 20 62 2c 20 63 29 27 2c 20 27 64 65 66  (a, b, c)', 'def
21ff3 27 29 0a 2a 2a 20 20 20 20 20 2d 3e 20 27 43 52  ').**     -> 'CR
21ff4 45 41 54 45 20 54 41 42 4c 45 20 64 65 66 28 61  EATE TABLE def(a
21ff5 2c 20 62 2c 20 63 29 27 0a 2a 2a 0a 2a 2a 20 73  , b, c)'.**.** s
21ff6 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62  qlite_rename_tab
21ff7 6c 65 28 27 43 52 45 41 54 45 20 49 4e 44 45 58  le('CREATE INDEX
21ff8 20 69 20 4f 4e 20 61 62 63 28 61 29 27 2c 20 27   i ON abc(a)', '
21ff9 64 65 66 27 29 0a 2a 2a 20 20 20 20 20 2d 3e 20  def').**     -> 
21ffa 27 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 20  'CREATE INDEX i 
21ffb 4f 4e 20 64 65 66 28 61 2c 20 62 2c 20 63 29 27  ON def(a, b, c)'
21ffc 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
21ffd 72 65 6e 61 6d 65 54 61 62 6c 65 46 75 6e 63 28  renameTableFunc(
21ffe 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
21fff 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
22000 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71  nt NotUsed,.  sq
22001 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
22002 67 76 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64  gv.){.  unsigned
22003 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 53 71   char const *zSq
22004 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  l = sqlite3_valu
22005 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b  e_text(argv[0]);
22006 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
22007 20 63 6f 6e 73 74 20 2a 7a 54 61 62 6c 65 4e 61   const *zTableNa
22008 6d 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  me = sqlite3_val
22009 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29  ue_text(argv[1])
2200a 3b 0a 0a 20 20 69 6e 74 20 74 6f 6b 65 6e 3b 0a  ;..  int token;.
2200b 20 20 54 6f 6b 65 6e 20 74 6e 61 6d 65 3b 0a 20    Token tname;. 
2200c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63   unsigned char c
2200d 6f 6e 73 74 20 2a 7a 43 73 72 20 3d 20 7a 53 71  onst *zCsr = zSq
2200e 6c 3b 0a 20 20 69 6e 74 20 6c 65 6e 20 3d 20 30  l;.  int len = 0
2200f 3b 0a 20 20 63 68 61 72 20 2a 7a 52 65 74 3b 0a  ;.  char *zRet;.
22010 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
22011 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
22012 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65  _db_handle(conte
22013 78 74 29 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50  xt);..  UNUSED_P
22014 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
22015 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 70 72 69  );..  /* The pri
22016 6e 63 69 70 6c 65 20 75 73 65 64 20 74 6f 20 6c  nciple used to l
22017 6f 63 61 74 65 20 74 68 65 20 74 61 62 6c 65 20  ocate the table 
22018 6e 61 6d 65 20 69 6e 20 74 68 65 20 43 52 45 41  name in the CREA
22019 54 45 20 54 41 42 4c 45 20 0a 20 20 2a 2a 20 73  TE TABLE .  ** s
2201a 74 61 74 65 6d 65 6e 74 20 69 73 20 74 68 61 74  tatement is that
2201b 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20   the table name 
2201c 69 73 20 74 68 65 20 66 69 72 73 74 20 6e 6f 6e  is the first non
2201d 2d 73 70 61 63 65 20 74 6f 6b 65 6e 20 74 68 61  -space token tha
2201e 74 0a 20 20 2a 2a 20 69 73 20 69 6d 6d 65 64 69  t.  ** is immedi
2201f 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 65 64 20 62  ately followed b
22020 79 20 61 20 54 4b 5f 4c 50 20 6f 72 20 54 4b 5f  y a TK_LP or TK_
22021 55 53 49 4e 47 20 74 6f 6b 65 6e 2e 0a 20 20 2a  USING token..  *
22022 2f 0a 20 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a  /.  if( zSql ){.
22023 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 69      do {.      i
22024 66 28 20 21 2a 7a 43 73 72 20 29 7b 0a 20 20 20  f( !*zCsr ){.   
22025 20 20 20 20 20 2f 2a 20 52 61 6e 20 6f 75 74 20       /* Ran out 
22026 6f 66 20 69 6e 70 75 74 20 62 65 66 6f 72 65 20  of input before 
22027 66 69 6e 64 69 6e 67 20 61 6e 20 6f 70 65 6e 69  finding an openi
22028 6e 67 20 62 72 61 63 6b 65 74 2e 20 52 65 74 75  ng bracket. Retu
22029 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 20 20  rn NULL. */.    
2202a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
2202b 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 53 74    }..      /* St
2202c 6f 72 65 20 74 68 65 20 74 6f 6b 65 6e 20 74 68  ore the token th
2202d 61 74 20 7a 43 73 72 20 70 6f 69 6e 74 73 20 74  at zCsr points t
2202e 6f 20 69 6e 20 74 6e 61 6d 65 2e 20 2a 2f 0a 20  o in tname. */. 
2202f 20 20 20 20 20 74 6e 61 6d 65 2e 7a 20 3d 20 28       tname.z = (
22030 63 68 61 72 2a 29 7a 43 73 72 3b 0a 20 20 20 20  char*)zCsr;.    
22031 20 20 74 6e 61 6d 65 2e 6e 20 3d 20 6c 65 6e 3b    tname.n = len;
22032 0a 0a 20 20 20 20 20 20 2f 2a 20 41 64 76 61 6e  ..      /* Advan
22033 63 65 20 7a 43 73 72 20 74 6f 20 74 68 65 20 6e  ce zCsr to the n
22034 65 78 74 20 74 6f 6b 65 6e 2e 20 53 74 6f 72 65  ext token. Store
22035 20 74 68 61 74 20 74 6f 6b 65 6e 20 74 79 70 65   that token type
22036 20 69 6e 20 27 74 6f 6b 65 6e 27 2c 0a 20 20 20   in 'token',.   
22037 20 20 20 2a 2a 20 61 6e 64 20 69 74 73 20 6c 65     ** and its le
22038 6e 67 74 68 20 69 6e 20 27 6c 65 6e 27 20 28 74  ngth in 'len' (t
22039 6f 20 62 65 20 75 73 65 64 20 6e 65 78 74 20 69  o be used next i
2203a 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 69 73  teration of this
2203b 20 6c 6f 6f 70 29 2e 0a 20 20 20 20 20 20 2a 2f   loop)..      */
2203c 0a 20 20 20 20 20 20 64 6f 20 7b 0a 20 20 20 20  .      do {.    
2203d 20 20 20 20 7a 43 73 72 20 2b 3d 20 6c 65 6e 3b      zCsr += len;
2203e 0a 20 20 20 20 20 20 20 20 6c 65 6e 20 3d 20 73  .        len = s
2203f 71 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e 28 7a  qlite3GetToken(z
22040 43 73 72 2c 20 26 74 6f 6b 65 6e 29 3b 0a 20 20  Csr, &token);.  
22041 20 20 20 20 7d 20 77 68 69 6c 65 28 20 74 6f 6b      } while( tok
22042 65 6e 3d 3d 54 4b 5f 53 50 41 43 45 20 29 3b 0a  en==TK_SPACE );.
22043 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6c 65        assert( le
22044 6e 3e 30 20 29 3b 0a 20 20 20 20 7d 20 77 68 69  n>0 );.    } whi
22045 6c 65 28 20 74 6f 6b 65 6e 21 3d 54 4b 5f 4c 50  le( token!=TK_LP
22046 20 26 26 20 74 6f 6b 65 6e 21 3d 54 4b 5f 55 53   && token!=TK_US
22047 49 4e 47 20 29 3b 0a 0a 20 20 20 20 7a 52 65 74  ING );..    zRet
22048 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
22049 66 28 64 62 2c 20 22 25 2e 2a 73 5c 22 25 77 5c  f(db, "%.*s\"%w\
2204a 22 25 73 22 2c 20 28 28 75 38 2a 29 74 6e 61 6d  "%s", ((u8*)tnam
2204b 65 2e 7a 29 20 2d 20 7a 53 71 6c 2c 20 7a 53 71  e.z) - zSql, zSq
2204c 6c 2c 20 0a 20 20 20 20 20 20 20 7a 54 61 62 6c  l, .       zTabl
2204d 65 4e 61 6d 65 2c 20 74 6e 61 6d 65 2e 7a 2b 74  eName, tname.z+t
2204e 6e 61 6d 65 2e 6e 29 3b 0a 20 20 20 20 73 71 6c  name.n);.    sql
2204f 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
22050 28 63 6f 6e 74 65 78 74 2c 20 7a 52 65 74 2c 20  (context, zRet, 
22051 2d 31 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d  -1, SQLITE_DYNAM
22052 49 43 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e  IC);.  }.}..#ifn
22053 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
22054 54 52 49 47 47 45 52 0a 2f 2a 20 54 68 69 73 20  TRIGGER./* This 
22055 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64  function is used
22056 20 62 79 20 53 51 4c 20 67 65 6e 65 72 61 74 65   by SQL generate
22057 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74  d to implement t
22058 68 65 0a 2a 2a 20 41 4c 54 45 52 20 54 41 42 4c  he.** ALTER TABL
22059 45 20 63 6f 6d 6d 61 6e 64 2e 20 54 68 65 20 66  E command. The f
2205a 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73  irst argument is
2205b 20 74 68 65 20 74 65 78 74 20 6f 66 20 61 20 43   the text of a C
2205c 52 45 41 54 45 20 54 52 49 47 47 45 52 20 0a 2a  REATE TRIGGER .*
2205d 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 65  * statement. The
2205e 20 73 65 63 6f 6e 64 20 69 73 20 61 20 74 61 62   second is a tab
2205f 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 74 61 62  le name. The tab
22060 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 43  le name in the C
22061 52 45 41 54 45 20 0a 2a 2a 20 54 52 49 47 47 45  REATE .** TRIGGE
22062 52 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 72  R statement is r
22063 65 70 6c 61 63 65 64 20 77 69 74 68 20 74 68 65  eplaced with the
22064 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20   third argument 
22065 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74 20 0a  and the result .
22066 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69  ** returned. Thi
22067 73 20 69 73 20 61 6e 61 6c 61 67 6f 75 73 20 74  s is analagous t
22068 6f 20 72 65 6e 61 6d 65 54 61 62 6c 65 46 75 6e  o renameTableFun
22069 63 28 29 20 61 62 6f 76 65 2c 20 65 78 63 65 70  c() above, excep
2206a 74 20 66 6f 72 20 43 52 45 41 54 45 0a 2a 2a 20  t for CREATE.** 
2206b 54 52 49 47 47 45 52 2c 20 6e 6f 74 20 43 52 45  TRIGGER, not CRE
2206c 41 54 45 20 49 4e 44 45 58 20 61 6e 64 20 43 52  ATE INDEX and CR
2206d 45 41 54 45 20 54 41 42 4c 45 2e 0a 2a 2f 0a 73  EATE TABLE..*/.s
2206e 74 61 74 69 63 20 76 6f 69 64 20 72 65 6e 61 6d  tatic void renam
2206f 65 54 72 69 67 67 65 72 46 75 6e 63 28 0a 20 20  eTriggerFunc(.  
22070 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
22071 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
22072 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74  NotUsed,.  sqlit
22073 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
22074 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  ){.  unsigned ch
22075 61 72 20 63 6f 6e 73 74 20 2a 7a 53 71 6c 20 3d  ar const *zSql =
22076 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
22077 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ext(argv[0]);.  
22078 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f  unsigned char co
22079 6e 73 74 20 2a 7a 54 61 62 6c 65 4e 61 6d 65 20  nst *zTableName 
2207a 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
2207b 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 0a  text(argv[1]);..
2207c 20 20 69 6e 74 20 74 6f 6b 65 6e 3b 0a 20 20 54    int token;.  T
2207d 6f 6b 65 6e 20 74 6e 61 6d 65 3b 0a 20 20 69 6e  oken tname;.  in
2207e 74 20 64 69 73 74 20 3d 20 33 3b 0a 20 20 75 6e  t dist = 3;.  un
2207f 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73  signed char cons
22080 74 20 2a 7a 43 73 72 20 3d 20 7a 53 71 6c 3b 0a  t *zCsr = zSql;.
22081 20 20 69 6e 74 20 6c 65 6e 20 3d 20 30 3b 0a 20    int len = 0;. 
22082 20 63 68 61 72 20 2a 7a 52 65 74 3b 0a 20 20 73   char *zRet;.  s
22083 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c  qlite3 *db = sql
22084 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f  ite3_context_db_
22085 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b  handle(context);
22086 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
22087 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 0a  ETER(NotUsed);..
22088 20 20 2f 2a 20 54 68 65 20 70 72 69 6e 63 69 70    /* The princip
22089 6c 65 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74  le used to locat
2208a 65 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65  e the table name
2208b 20 69 6e 20 74 68 65 20 43 52 45 41 54 45 20 54   in the CREATE T
2208c 52 49 47 47 45 52 20 0a 20 20 2a 2a 20 73 74 61  RIGGER .  ** sta
2208d 74 65 6d 65 6e 74 20 69 73 20 74 68 61 74 20 74  tement is that t
2208e 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 73  he table name is
2208f 20 74 68 65 20 66 69 72 73 74 20 74 6f 6b 65 6e   the first token
22090 20 74 68 61 74 20 69 73 20 69 6d 6d 65 64 69 61   that is immedia
22091 74 65 64 6c 79 0a 20 20 2a 2a 20 70 72 65 63 65  tedly.  ** prece
22092 64 65 64 20 62 79 20 65 69 74 68 65 72 20 54 4b  ded by either TK
22093 5f 4f 4e 20 6f 72 20 54 4b 5f 44 4f 54 20 61 6e  _ON or TK_DOT an
22094 64 20 69 6d 6d 65 64 69 61 74 65 64 6c 79 20 66  d immediatedly f
22095 6f 6c 6c 6f 77 65 64 20 62 79 20 6f 6e 65 0a 20  ollowed by one. 
22096 20 2a 2a 20 6f 66 20 54 4b 5f 57 48 45 4e 2c 20   ** of TK_WHEN, 
22097 54 4b 5f 42 45 47 49 4e 20 6f 72 20 54 4b 5f 46  TK_BEGIN or TK_F
22098 4f 52 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a  OR..  */.  if( z
22099 53 71 6c 20 29 7b 0a 20 20 20 20 64 6f 20 7b 0a  Sql ){.    do {.
2209a 0a 20 20 20 20 20 20 69 66 28 20 21 2a 7a 43 73  .      if( !*zCs
2209b 72 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  r ){.        /* 
2209c 52 61 6e 20 6f 75 74 20 6f 66 20 69 6e 70 75 74  Ran out of input
2209d 20 62 65 66 6f 72 65 20 66 69 6e 64 69 6e 67 20   before finding 
2209e 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20  the table name. 
2209f 52 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a  Return NULL. */.
220a0 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a          return;.
220a1 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
220a2 2a 20 53 74 6f 72 65 20 74 68 65 20 74 6f 6b 65  * Store the toke
220a3 6e 20 74 68 61 74 20 7a 43 73 72 20 70 6f 69 6e  n that zCsr poin
220a4 74 73 20 74 6f 20 69 6e 20 74 6e 61 6d 65 2e 20  ts to in tname. 
220a5 2a 2f 0a 20 20 20 20 20 20 74 6e 61 6d 65 2e 7a  */.      tname.z
220a6 20 3d 20 28 63 68 61 72 2a 29 7a 43 73 72 3b 0a   = (char*)zCsr;.
220a7 20 20 20 20 20 20 74 6e 61 6d 65 2e 6e 20 3d 20        tname.n = 
220a8 6c 65 6e 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 41  len;..      /* A
220a9 64 76 61 6e 63 65 20 7a 43 73 72 20 74 6f 20 74  dvance zCsr to t
220aa 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e 2e 20 53  he next token. S
220ab 74 6f 72 65 20 74 68 61 74 20 74 6f 6b 65 6e 20  tore that token 
220ac 74 79 70 65 20 69 6e 20 27 74 6f 6b 65 6e 27 2c  type in 'token',
220ad 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 69 74  .      ** and it
220ae 73 20 6c 65 6e 67 74 68 20 69 6e 20 27 6c 65 6e  s length in 'len
220af 27 20 28 74 6f 20 62 65 20 75 73 65 64 20 6e 65  ' (to be used ne
220b0 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20  xt iteration of 
220b1 74 68 69 73 20 6c 6f 6f 70 29 2e 0a 20 20 20 20  this loop)..    
220b2 20 20 2a 2f 0a 20 20 20 20 20 20 64 6f 20 7b 0a    */.      do {.
220b3 20 20 20 20 20 20 20 20 7a 43 73 72 20 2b 3d 20          zCsr += 
220b4 6c 65 6e 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e  len;.        len
220b5 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b   = sqlite3GetTok
220b6 65 6e 28 7a 43 73 72 2c 20 26 74 6f 6b 65 6e 29  en(zCsr, &token)
220b7 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20  ;.      }while( 
220b8 74 6f 6b 65 6e 3d 3d 54 4b 5f 53 50 41 43 45 20  token==TK_SPACE 
220b9 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
220ba 20 6c 65 6e 3e 30 20 29 3b 0a 0a 20 20 20 20 20   len>0 );..     
220bb 20 2f 2a 20 56 61 72 69 61 62 6c 65 20 27 64 69   /* Variable 'di
220bc 73 74 27 20 73 74 6f 72 65 73 20 74 68 65 20 6e  st' stores the n
220bd 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20  umber of tokens 
220be 72 65 61 64 20 73 69 6e 63 65 20 74 68 65 20 6d  read since the m
220bf 6f 73 74 0a 20 20 20 20 20 20 2a 2a 20 72 65 63  ost.      ** rec
220c0 65 6e 74 20 54 4b 5f 44 4f 54 20 6f 72 20 54 4b  ent TK_DOT or TK
220c1 5f 4f 4e 2e 20 54 68 69 73 20 6d 65 61 6e 73 20  _ON. This means 
220c2 74 68 61 74 20 77 68 65 6e 20 61 20 57 48 45 4e  that when a WHEN
220c3 2c 20 46 4f 52 20 6f 72 20 42 45 47 49 4e 20 0a  , FOR or BEGIN .
220c4 20 20 20 20 20 20 2a 2a 20 74 6f 6b 65 6e 20 69        ** token i
220c5 73 20 72 65 61 64 20 61 6e 64 20 27 64 69 73 74  s read and 'dist
220c6 27 20 65 71 75 61 6c 73 20 32 2c 20 74 68 65 20  ' equals 2, the 
220c7 63 6f 6e 64 69 74 69 6f 6e 20 73 74 61 74 65 64  condition stated
220c8 20 61 62 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20   above.      ** 
220c9 74 6f 20 62 65 20 6d 65 74 2e 0a 20 20 20 20 20  to be met..     
220ca 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4e 6f 74   **.      ** Not
220cb 65 20 74 68 61 74 20 4f 4e 20 63 61 6e 6e 6f 74  e that ON cannot
220cc 20 62 65 20 61 20 64 61 74 61 62 61 73 65 2c 20   be a database, 
220cd 74 61 62 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e 20  table or column 
220ce 6e 61 6d 65 2c 20 73 6f 0a 20 20 20 20 20 20 2a  name, so.      *
220cf 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65  * there is no ne
220d0 65 64 20 74 6f 20 77 6f 72 72 79 20 61 62 6f 75  ed to worry abou
220d1 74 20 73 79 6e 74 61 78 20 6c 69 6b 65 20 0a 20  t syntax like . 
220d2 20 20 20 20 20 2a 2a 20 22 43 52 45 41 54 45 20       ** "CREATE 
220d3 54 52 49 47 47 45 52 20 2e 2e 2e 20 4f 4e 20 4f  TRIGGER ... ON O
220d4 4e 2e 4f 4e 20 42 45 47 49 4e 20 2e 2e 2e 22 20  N.ON BEGIN ..." 
220d5 65 74 63 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  etc..      */.  
220d6 20 20 20 20 64 69 73 74 2b 2b 3b 0a 20 20 20 20      dist++;.    
220d7 20 20 69 66 28 20 74 6f 6b 65 6e 3d 3d 54 4b 5f    if( token==TK_
220d8 44 4f 54 20 7c 7c 20 74 6f 6b 65 6e 3d 3d 54 4b  DOT || token==TK
220d9 5f 4f 4e 20 29 7b 0a 20 20 20 20 20 20 20 20 64  _ON ){.        d
220da 69 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  ist = 0;.      }
220db 0a 20 20 20 20 7d 20 77 68 69 6c 65 28 20 64 69  .    } while( di
220dc 73 74 21 3d 32 20 7c 7c 20 28 74 6f 6b 65 6e 21  st!=2 || (token!
220dd 3d 54 4b 5f 57 48 45 4e 20 26 26 20 74 6f 6b 65  =TK_WHEN && toke
220de 6e 21 3d 54 4b 5f 46 4f 52 20 26 26 20 74 6f 6b  n!=TK_FOR && tok
220df 65 6e 21 3d 54 4b 5f 42 45 47 49 4e 29 20 29 3b  en!=TK_BEGIN) );
220e0 0a 0a 20 20 20 20 2f 2a 20 56 61 72 69 61 62 6c  ..    /* Variabl
220e1 65 20 74 6e 61 6d 65 20 6e 6f 77 20 63 6f 6e 74  e tname now cont
220e2 61 69 6e 73 20 74 68 65 20 74 6f 6b 65 6e 20 74  ains the token t
220e3 68 61 74 20 69 73 20 74 68 65 20 6f 6c 64 20 74  hat is the old t
220e4 61 62 6c 65 2d 6e 61 6d 65 0a 20 20 20 20 2a 2a  able-name.    **
220e5 20 69 6e 20 74 68 65 20 43 52 45 41 54 45 20 54   in the CREATE T
220e6 52 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e 74  RIGGER statement
220e7 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7a 52 65  ..    */.    zRe
220e8 74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  t = sqlite3MPrin
220e9 74 66 28 64 62 2c 20 22 25 2e 2a 73 5c 22 25 77  tf(db, "%.*s\"%w
220ea 5c 22 25 73 22 2c 20 28 28 75 38 2a 29 74 6e 61  \"%s", ((u8*)tna
220eb 6d 65 2e 7a 29 20 2d 20 7a 53 71 6c 2c 20 7a 53  me.z) - zSql, zS
220ec 71 6c 2c 20 0a 20 20 20 20 20 20 20 7a 54 61 62  ql, .       zTab
220ed 6c 65 4e 61 6d 65 2c 20 74 6e 61 6d 65 2e 7a 2b  leName, tname.z+
220ee 74 6e 61 6d 65 2e 6e 29 3b 0a 20 20 20 20 73 71  tname.n);.    sq
220ef 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
220f0 74 28 63 6f 6e 74 65 78 74 2c 20 7a 52 65 74 2c  t(context, zRet,
220f1 20 2d 31 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41   -1, SQLITE_DYNA
220f2 4d 49 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64  MIC);.  }.}.#end
220f3 69 66 20 20 20 2f 2a 20 21 53 51 4c 49 54 45 5f  if   /* !SQLITE_
220f4 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 2a 2f 0a  OMIT_TRIGGER */.
220f5 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20  ./*.** Register 
220f6 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f  built-in functio
220f7 6e 73 20 75 73 65 64 20 74 6f 20 68 65 6c 70 20  ns used to help 
220f8 69 6d 70 6c 65 6d 65 6e 74 20 41 4c 54 45 52 20  implement ALTER 
220f9 54 41 42 4c 45 0a 2a 2f 0a 53 51 4c 49 54 45 5f  TABLE.*/.SQLITE_
220fa 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
220fb 69 74 65 33 41 6c 74 65 72 46 75 6e 63 74 69 6f  ite3AlterFunctio
220fc 6e 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  ns(sqlite3 *db){
220fd 0a 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65  .  sqlite3Create
220fe 46 75 6e 63 28 64 62 2c 20 22 73 71 6c 69 74 65  Func(db, "sqlite
220ff 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 22 2c 20  _rename_table", 
22100 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  2, SQLITE_UTF8, 
22101 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  0,.             
22102 20 20 20 20 20 20 20 20 20 20 20 20 72 65 6e 61              rena
22103 6d 65 54 61 62 6c 65 46 75 6e 63 2c 20 30 2c 20  meTableFunc, 0, 
22104 30 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  0);.#ifndef SQLI
22105 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a  TE_OMIT_TRIGGER.
22106 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46    sqlite3CreateF
22107 75 6e 63 28 64 62 2c 20 22 73 71 6c 69 74 65 5f  unc(db, "sqlite_
22108 72 65 6e 61 6d 65 5f 74 72 69 67 67 65 72 22 2c  rename_trigger",
22109 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   2, SQLITE_UTF8,
2210a 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20   0,.            
2210b 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 6e               ren
2210c 61 6d 65 54 72 69 67 67 65 72 46 75 6e 63 2c 20  ameTriggerFunc, 
2210d 30 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a  0, 0);.#endif.}.
2210e 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
2210f 74 68 65 20 74 65 78 74 20 6f 66 20 61 20 57 48  the text of a WH
22110 45 52 45 20 65 78 70 72 65 73 73 69 6f 6e 20 77  ERE expression w
22111 68 69 63 68 20 63 61 6e 20 62 65 20 75 73 65 64  hich can be used
22112 20 74 6f 20 73 65 6c 65 63 74 20 61 6c 6c 0a 2a   to select all.*
22113 2a 20 74 65 6d 70 6f 72 61 72 79 20 74 72 69 67  * temporary trig
22114 67 65 72 73 20 6f 6e 20 74 61 62 6c 65 20 70 54  gers on table pT
22115 61 62 20 66 72 6f 6d 20 74 68 65 20 73 71 6c 69  ab from the sqli
22116 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 20 74  te_temp_master t
22117 61 62 6c 65 2e 20 49 66 0a 2a 2a 20 74 61 62 6c  able. If.** tabl
22118 65 20 70 54 61 62 20 68 61 73 20 6e 6f 20 74 65  e pTab has no te
22119 6d 70 6f 72 61 72 79 20 74 72 69 67 67 65 72 73  mporary triggers
2211a 2c 20 6f 72 20 69 73 20 69 74 73 65 6c 66 20 73  , or is itself s
2211b 74 6f 72 65 64 20 69 6e 20 74 68 65 20 0a 2a 2a  tored in the .**
2211c 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62   temporary datab
2211d 61 73 65 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74  ase, NULL is ret
2211e 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  urned..*/.static
2211f 20 63 68 61 72 20 2a 77 68 65 72 65 54 65 6d 70   char *whereTemp
22120 54 72 69 67 67 65 72 73 28 50 61 72 73 65 20 2a  Triggers(Parse *
22121 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70  pParse, Table *p
22122 54 61 62 29 7b 0a 20 20 54 72 69 67 67 65 72 20  Tab){.  Trigger 
22123 2a 70 54 72 69 67 3b 0a 20 20 63 68 61 72 20 2a  *pTrig;.  char *
22124 7a 57 68 65 72 65 20 3d 20 30 3b 0a 20 20 63 68  zWhere = 0;.  ch
22125 61 72 20 2a 74 6d 70 20 3d 20 30 3b 0a 20 20 63  ar *tmp = 0;.  c
22126 6f 6e 73 74 20 53 63 68 65 6d 61 20 2a 70 54 65  onst Schema *pTe
22127 6d 70 53 63 68 65 6d 61 20 3d 20 70 50 61 72 73  mpSchema = pPars
22128 65 2d 3e 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53  e->db->aDb[1].pS
22129 63 68 65 6d 61 3b 20 2f 2a 20 54 65 6d 70 20 64  chema; /* Temp d
2212a 62 20 73 63 68 65 6d 61 20 2a 2f 0a 0a 20 20 2f  b schema */..  /
2212b 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 69  * If the table i
2212c 73 20 6e 6f 74 20 6c 6f 63 61 74 65 64 20 69 6e  s not located in
2212d 20 74 68 65 20 74 65 6d 70 2d 64 62 20 28 69 6e   the temp-db (in
2212e 20 77 68 69 63 68 20 63 61 73 65 20 4e 55 4c 4c   which case NULL
2212f 20 69 73 20 0a 20 20 2a 2a 20 72 65 74 75 72 6e   is .  ** return
22130 65 64 2c 20 6c 6f 6f 70 20 74 68 72 6f 75 67 68  ed, loop through
22131 20 74 68 65 20 74 61 62 6c 65 73 20 6c 69 73 74   the tables list
22132 20 6f 66 20 74 72 69 67 67 65 72 73 2e 20 46 6f   of triggers. Fo
22133 72 20 65 61 63 68 20 74 72 69 67 67 65 72 0a 20  r each trigger. 
22134 20 2a 2a 20 74 68 61 74 20 69 73 20 6e 6f 74 20   ** that is not 
22135 70 61 72 74 20 6f 66 20 74 68 65 20 74 65 6d 70  part of the temp
22136 2d 64 62 20 73 63 68 65 6d 61 2c 20 61 64 64 20  -db schema, add 
22137 61 20 63 6c 61 75 73 65 20 74 6f 20 74 68 65 20  a clause to the 
22138 57 48 45 52 45 20 0a 20 20 2a 2a 20 65 78 70 72  WHERE .  ** expr
22139 65 73 73 69 6f 6e 20 62 65 69 6e 67 20 62 75 69  ession being bui
2213a 6c 74 20 75 70 20 69 6e 20 7a 57 68 65 72 65 2e  lt up in zWhere.
2213b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62  .  */.  if( pTab
2213c 2d 3e 70 53 63 68 65 6d 61 21 3d 70 54 65 6d 70  ->pSchema!=pTemp
2213d 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 73 71  Schema ){.    sq
2213e 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
2213f 73 65 2d 3e 64 62 3b 0a 20 20 20 20 66 6f 72 28  se->db;.    for(
22140 70 54 72 69 67 3d 73 71 6c 69 74 65 33 54 72 69  pTrig=sqlite3Tri
22141 67 67 65 72 4c 69 73 74 28 70 50 61 72 73 65 2c  ggerList(pParse,
22142 20 70 54 61 62 29 3b 20 70 54 72 69 67 3b 20 70   pTab); pTrig; p
22143 54 72 69 67 3d 70 54 72 69 67 2d 3e 70 4e 65 78  Trig=pTrig->pNex
22144 74 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 54  t){.      if( pT
22145 72 69 67 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54  rig->pSchema==pT
22146 65 6d 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20  empSchema ){.   
22147 20 20 20 20 20 69 66 28 20 21 7a 57 68 65 72 65       if( !zWhere
22148 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 57   ){.          zW
22149 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 4d 50  here = sqlite3MP
2214a 72 69 6e 74 66 28 64 62 2c 20 22 6e 61 6d 65 3d  rintf(db, "name=
2214b 25 51 22 2c 20 70 54 72 69 67 2d 3e 7a 4e 61 6d  %Q", pTrig->zNam
2214c 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  e);.        }els
2214d 65 7b 0a 20 20 20 20 20 20 20 20 20 20 74 6d 70  e{.          tmp
2214e 20 3d 20 7a 57 68 65 72 65 3b 0a 20 20 20 20 20   = zWhere;.     
2214f 20 20 20 20 20 7a 57 68 65 72 65 20 3d 20 73 71       zWhere = sq
22150 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
22151 20 22 25 73 20 4f 52 20 6e 61 6d 65 3d 25 51 22   "%s OR name=%Q"
22152 2c 20 7a 57 68 65 72 65 2c 20 70 54 72 69 67 2d  , zWhere, pTrig-
22153 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20  >zName);.       
22154 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
22155 28 64 62 2c 20 74 6d 70 29 3b 0a 20 20 20 20 20  (db, tmp);.     
22156 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
22157 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
22158 7a 57 68 65 72 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  zWhere;.}../*.**
22159 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
2215a 6f 20 64 72 6f 70 20 61 6e 64 20 72 65 6c 6f 61  o drop and reloa
2215b 64 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72  d the internal r
2215c 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66  epresentation of
2215d 20 74 61 62 6c 65 0a 2a 2a 20 70 54 61 62 20 66   table.** pTab f
2215e 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65  rom the database
2215f 2c 20 69 6e 63 6c 75 64 69 6e 67 20 74 72 69 67  , including trig
22160 67 65 72 73 20 61 6e 64 20 74 65 6d 70 6f 72 61  gers and tempora
22161 72 79 20 74 72 69 67 67 65 72 73 2e 0a 2a 2a 20  ry triggers..** 
22162 41 72 67 75 6d 65 6e 74 20 7a 4e 61 6d 65 20 69  Argument zName i
22163 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  s the name of th
22164 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 64  e table in the d
22165 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 61  atabase schema a
22166 74 0a 2a 2a 20 74 68 65 20 74 69 6d 65 20 74 68  t.** the time th
22167 65 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65  e generated code
22168 20 69 73 20 65 78 65 63 75 74 65 64 2e 20 54 68   is executed. Th
22169 69 73 20 63 61 6e 20 62 65 20 64 69 66 66 65 72  is can be differ
2216a 65 6e 74 20 66 72 6f 6d 0a 2a 2a 20 70 54 61 62  ent from.** pTab
2216b 2d 3e 7a 4e 61 6d 65 20 69 66 20 74 68 69 73 20  ->zName if this 
2216c 66 75 6e 63 74 69 6f 6e 20 69 73 20 62 65 69 6e  function is bein
2216d 67 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f 64 65  g called to code
2216e 20 70 61 72 74 20 6f 66 20 61 6e 20 0a 2a 2a 20   part of an .** 
2216f 22 41 4c 54 45 52 20 54 41 42 4c 45 20 52 45 4e  "ALTER TABLE REN
22170 41 4d 45 20 54 4f 22 20 73 74 61 74 65 6d 65 6e  AME TO" statemen
22171 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  t..*/.static voi
22172 64 20 72 65 6c 6f 61 64 54 61 62 6c 65 53 63 68  d reloadTableSch
22173 65 6d 61 28 50 61 72 73 65 20 2a 70 50 61 72 73  ema(Parse *pPars
22174 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20  e, Table *pTab, 
22175 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
22176 65 29 7b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20  e){.  Vdbe *v;. 
22177 20 63 68 61 72 20 2a 7a 57 68 65 72 65 3b 0a 20   char *zWhere;. 
22178 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20   int iDb;       
22179 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
2217a 6e 64 65 78 20 6f 66 20 64 61 74 61 62 61 73 65  ndex of database
2217b 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 54 61 62   containing pTab
2217c 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   */.#ifndef SQLI
2217d 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a  TE_OMIT_TRIGGER.
2217e 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67    Trigger *pTrig
2217f 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 76 20 3d 20  ;.#endif..  v = 
22180 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
22181 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 4e 45  Parse);.  if( NE
22182 56 45 52 28 76 3d 3d 30 29 20 29 20 72 65 74 75  VER(v==0) ) retu
22183 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  rn;.  assert( sq
22184 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41  lite3BtreeHoldsA
22185 6c 6c 4d 75 74 65 78 65 73 28 70 50 61 72 73 65  llMutexes(pParse
22186 2d 3e 64 62 29 20 29 3b 0a 20 20 69 44 62 20 3d  ->db) );.  iDb =
22187 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
22188 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62  Index(pParse->db
22189 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29  , pTab->pSchema)
2218a 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e  ;.  assert( iDb>
2218b 3d 30 20 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53  =0 );..#ifndef S
2218c 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
2218d 45 52 0a 20 20 2f 2a 20 44 72 6f 70 20 61 6e 79  ER.  /* Drop any
2218e 20 74 61 62 6c 65 20 74 72 69 67 67 65 72 73 20   table triggers 
2218f 66 72 6f 6d 20 74 68 65 20 69 6e 74 65 72 6e 61  from the interna
22190 6c 20 73 63 68 65 6d 61 2e 20 2a 2f 0a 20 20 66  l schema. */.  f
22191 6f 72 28 70 54 72 69 67 3d 73 71 6c 69 74 65 33  or(pTrig=sqlite3
22192 54 72 69 67 67 65 72 4c 69 73 74 28 70 50 61 72  TriggerList(pPar
22193 73 65 2c 20 70 54 61 62 29 3b 20 70 54 72 69 67  se, pTab); pTrig
22194 3b 20 70 54 72 69 67 3d 70 54 72 69 67 2d 3e 70  ; pTrig=pTrig->p
22195 4e 65 78 74 29 7b 0a 20 20 20 20 69 6e 74 20 69  Next){.    int i
22196 54 72 69 67 44 62 20 3d 20 73 71 6c 69 74 65 33  TrigDb = sqlite3
22197 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50  SchemaToIndex(pP
22198 61 72 73 65 2d 3e 64 62 2c 20 70 54 72 69 67 2d  arse->db, pTrig-
22199 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 61  >pSchema);.    a
2219a 73 73 65 72 74 28 20 69 54 72 69 67 44 62 3d 3d  ssert( iTrigDb==
2219b 69 44 62 20 7c 7c 20 69 54 72 69 67 44 62 3d 3d  iDb || iTrigDb==
2219c 31 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  1 );.    sqlite3
2219d 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
2219e 5f 44 72 6f 70 54 72 69 67 67 65 72 2c 20 69 54  _DropTrigger, iT
2219f 72 69 67 44 62 2c 20 30 2c 20 30 2c 20 70 54 72  rigDb, 0, 0, pTr
221a0 69 67 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20  ig->zName, 0);. 
221a1 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20   }.#endif..  /* 
221a2 44 72 6f 70 20 74 68 65 20 74 61 62 6c 65 20 61  Drop the table a
221a3 6e 64 20 69 6e 64 65 78 20 66 72 6f 6d 20 74 68  nd index from th
221a4 65 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d  e internal schem
221a5 61 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64  a */.  sqlite3Vd
221a6 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 44  beAddOp4(v, OP_D
221a7 72 6f 70 54 61 62 6c 65 2c 20 69 44 62 2c 20 30  ropTable, iDb, 0
221a8 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  , 0, pTab->zName
221a9 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 52 65 6c 6f  , 0);..  /* Relo
221aa 61 64 20 74 68 65 20 74 61 62 6c 65 2c 20 69 6e  ad the table, in
221ab 64 65 78 20 61 6e 64 20 70 65 72 6d 61 6e 65 6e  dex and permanen
221ac 74 20 74 72 69 67 67 65 72 20 73 63 68 65 6d 61  t trigger schema
221ad 73 2e 20 2a 2f 0a 20 20 7a 57 68 65 72 65 20 3d  s. */.  zWhere =
221ae 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
221af 70 50 61 72 73 65 2d 3e 64 62 2c 20 22 74 62 6c  pParse->db, "tbl
221b0 5f 6e 61 6d 65 3d 25 51 22 2c 20 7a 4e 61 6d 65  _name=%Q", zName
221b1 29 3b 0a 20 20 69 66 28 20 21 7a 57 68 65 72 65  );.  if( !zWhere
221b2 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c   ) return;.  sql
221b3 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
221b4 2c 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61  , OP_ParseSchema
221b5 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20 7a 57 68  , iDb, 0, 0, zWh
221b6 65 72 65 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29  ere, P4_DYNAMIC)
221b7 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
221b8 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20  E_OMIT_TRIGGER. 
221b9 20 2f 2a 20 4e 6f 77 2c 20 69 66 20 74 68 65 20   /* Now, if the 
221ba 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 73 74 6f  table is not sto
221bb 72 65 64 20 69 6e 20 74 68 65 20 74 65 6d 70 20  red in the temp 
221bc 64 61 74 61 62 61 73 65 2c 20 72 65 6c 6f 61 64  database, reload
221bd 20 61 6e 79 20 74 65 6d 70 20 0a 20 20 2a 2a 20   any temp .  ** 
221be 74 72 69 67 67 65 72 73 2e 20 44 6f 6e 27 74 20  triggers. Don't 
221bf 75 73 65 20 49 4e 28 2e 2e 2e 29 20 69 6e 20 63  use IN(...) in c
221c0 61 73 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ase SQLITE_OMIT_
221c1 53 55 42 51 55 45 52 59 20 69 73 20 64 65 66 69  SUBQUERY is defi
221c2 6e 65 64 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28  ned. .  */.  if(
221c3 20 28 7a 57 68 65 72 65 3d 77 68 65 72 65 54 65   (zWhere=whereTe
221c4 6d 70 54 72 69 67 67 65 72 73 28 70 50 61 72 73  mpTriggers(pPars
221c5 65 2c 20 70 54 61 62 29 29 21 3d 30 20 29 7b 0a  e, pTab))!=0 ){.
221c6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
221c7 64 64 4f 70 34 28 76 2c 20 4f 50 5f 50 61 72 73  ddOp4(v, OP_Pars
221c8 65 53 63 68 65 6d 61 2c 20 31 2c 20 30 2c 20 30  eSchema, 1, 0, 0
221c9 2c 20 7a 57 68 65 72 65 2c 20 50 34 5f 44 59 4e  , zWhere, P4_DYN
221ca 41 4d 49 43 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  AMIC);.  }.#endi
221cb 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  f.}../*.** Gener
221cc 61 74 65 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c  ate code to impl
221cd 65 6d 65 6e 74 20 74 68 65 20 22 41 4c 54 45 52  ement the "ALTER
221ce 20 54 41 42 4c 45 20 78 78 78 20 52 45 4e 41 4d   TABLE xxx RENAM
221cf 45 20 54 4f 20 79 79 79 22 20 0a 2a 2a 20 63 6f  E TO yyy" .** co
221d0 6d 6d 61 6e 64 2e 20 0a 2a 2f 0a 53 51 4c 49 54  mmand. .*/.SQLIT
221d1 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
221d2 71 6c 69 74 65 33 41 6c 74 65 72 52 65 6e 61 6d  qlite3AlterRenam
221d3 65 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20  eTable(.  Parse 
221d4 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
221d5 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f      /* Parser co
221d6 6e 74 65 78 74 2e 20 2a 2f 0a 20 20 53 72 63 4c  ntext. */.  SrcL
221d7 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20 20  ist *pSrc,      
221d8 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62        /* The tab
221d9 6c 65 20 74 6f 20 72 65 6e 61 6d 65 2e 20 2a 2f  le to rename. */
221da 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20  .  Token *pName 
221db 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
221dc 54 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e 61  The new table na
221dd 6d 65 2e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  me. */.){.  int 
221de 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  iDb;            
221df 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
221e0 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20  e that contains 
221e1 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63  the table */.  c
221e2 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 20  har *zDb;       
221e3 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
221e4 20 6f 66 20 64 61 74 61 62 61 73 65 20 69 44 62   of database iDb
221e5 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
221e6 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
221e7 2f 2a 20 54 61 62 6c 65 20 62 65 69 6e 67 20 72  /* Table being r
221e8 65 6e 61 6d 65 64 20 2a 2f 0a 20 20 63 68 61 72  enamed */.  char
221e9 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 20 20 20 20   *zName = 0;    
221ea 20 20 20 20 20 20 2f 2a 20 4e 55 4c 4c 2d 74 65        /* NULL-te
221eb 72 6d 69 6e 61 74 65 64 20 76 65 72 73 69 6f 6e  rminated version
221ec 20 6f 66 20 70 4e 61 6d 65 20 2a 2f 20 0a 20 20   of pName */ .  
221ed 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
221ee 61 72 73 65 2d 3e 64 62 3b 20 2f 2a 20 44 61 74  arse->db; /* Dat
221ef 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
221f0 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 61 62 4e 61   */.  int nTabNa
221f1 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  me;             
221f2 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 55 54 46  /* Number of UTF
221f3 2d 38 20 63 68 61 72 61 63 74 65 72 73 20 69 6e  -8 characters in
221f4 20 7a 54 61 62 4e 61 6d 65 20 2a 2f 0a 20 20 63   zTabName */.  c
221f5 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 4e  onst char *zTabN
221f6 61 6d 65 3b 20 20 20 20 20 2f 2a 20 4f 72 69 67  ame;     /* Orig
221f7 69 6e 61 6c 20 6e 61 6d 65 20 6f 66 20 74 68 65  inal name of the
221f8 20 74 61 62 6c 65 20 2a 2f 0a 20 20 56 64 62 65   table */.  Vdbe
221f9 20 2a 76 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c   *v;.#ifndef SQL
221fa 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
221fb 0a 20 20 63 68 61 72 20 2a 7a 57 68 65 72 65 20  .  char *zWhere 
221fc 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  = 0;         /* 
221fd 57 68 65 72 65 20 63 6c 61 75 73 65 20 74 6f 20  Where clause to 
221fe 6c 6f 63 61 74 65 20 74 65 6d 70 20 74 72 69 67  locate temp trig
221ff 67 65 72 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20  gers */.#endif. 
22200 20 56 54 61 62 6c 65 20 2a 70 56 54 61 62 20 3d   VTable *pVTab =
22201 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f   0;        /* No
22202 6e 2d 7a 65 72 6f 20 69 66 20 74 68 69 73 20 69  n-zero if this i
22203 73 20 61 20 76 2d 74 61 62 20 77 69 74 68 20 61  s a v-tab with a
22204 6e 20 78 52 65 6e 61 6d 65 28 29 20 2a 2f 0a 20  n xRename() */. 
22205 20 0a 20 20 69 66 28 20 4e 45 56 45 52 28 64 62   .  if( NEVER(db
22206 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29 20  ->mallocFailed) 
22207 29 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61  ) goto exit_rena
22208 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 61 73 73 65  me_table;.  asse
22209 72 74 28 20 70 53 72 63 2d 3e 6e 53 72 63 3d 3d  rt( pSrc->nSrc==
2220a 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  1 );.  assert( s
2220b 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73  qlite3BtreeHolds
2220c 41 6c 6c 4d 75 74 65 78 65 73 28 70 50 61 72 73  AllMutexes(pPars
2220d 65 2d 3e 64 62 29 20 29 3b 0a 0a 20 20 70 54 61  e->db) );..  pTa
2220e 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74  b = sqlite3Locat
2220f 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30  eTable(pParse, 0
22210 2c 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 7a 4e 61  , pSrc->a[0].zNa
22211 6d 65 2c 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 7a  me, pSrc->a[0].z
22212 44 61 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28  Database);.  if(
22213 20 21 70 54 61 62 20 29 20 67 6f 74 6f 20 65 78   !pTab ) goto ex
22214 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b  it_rename_table;
22215 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33  .  iDb = sqlite3
22216 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50  SchemaToIndex(pP
22217 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e  arse->db, pTab->
22218 70 53 63 68 65 6d 61 29 3b 0a 20 20 7a 44 62 20  pSchema);.  zDb 
22219 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  = db->aDb[iDb].z
2221a 4e 61 6d 65 3b 0a 0a 20 20 2f 2a 20 47 65 74 20  Name;..  /* Get 
2221b 61 20 4e 55 4c 4c 20 74 65 72 6d 69 6e 61 74 65  a NULL terminate
2221c 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65  d version of the
2221d 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e   new table name.
2221e 20 2a 2f 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71   */.  zName = sq
2221f 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
22220 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20  en(db, pName);. 
22221 20 69 66 28 20 21 7a 4e 61 6d 65 20 29 20 67 6f   if( !zName ) go
22222 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74  to exit_rename_t
22223 61 62 6c 65 3b 0a 0a 20 20 2f 2a 20 43 68 65 63  able;..  /* Chec
22224 6b 20 74 68 61 74 20 61 20 74 61 62 6c 65 20 6f  k that a table o
22225 72 20 69 6e 64 65 78 20 6e 61 6d 65 64 20 27 7a  r index named 'z
22226 4e 61 6d 65 27 20 64 6f 65 73 20 6e 6f 74 20 61  Name' does not a
22227 6c 72 65 61 64 79 20 65 78 69 73 74 0a 20 20 2a  lready exist.  *
22228 2a 20 69 6e 20 64 61 74 61 62 61 73 65 20 69 44  * in database iD
22229 62 2e 20 49 66 20 73 6f 2c 20 74 68 69 73 20 69  b. If so, this i
2222a 73 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 2a 2f  s an error..  */
2222b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69  .  if( sqlite3Fi
2222c 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 4e 61 6d  ndTable(db, zNam
2222d 65 2c 20 7a 44 62 29 20 7c 7c 20 73 71 6c 69 74  e, zDb) || sqlit
2222e 65 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20  e3FindIndex(db, 
2222f 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 29 7b 0a 20  zName, zDb) ){. 
22230 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
22231 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20  sg(pParse, .    
22232 20 20 20 20 22 74 68 65 72 65 20 69 73 20 61 6c      "there is al
22233 72 65 61 64 79 20 61 6e 6f 74 68 65 72 20 74 61  ready another ta
22234 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 77 69 74  ble or index wit
22235 68 20 74 68 69 73 20 6e 61 6d 65 3a 20 25 73 22  h this name: %s"
22236 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f  , zName);.    go
22237 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74  to exit_rename_t
22238 61 62 6c 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  able;.  }..  /* 
22239 4d 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 20  Make sure it is 
2223a 6e 6f 74 20 61 20 73 79 73 74 65 6d 20 74 61 62  not a system tab
2223b 6c 65 20 62 65 69 6e 67 20 61 6c 74 65 72 65 64  le being altered
2223c 2c 20 6f 72 20 61 20 72 65 73 65 72 76 65 64 20  , or a reserved 
2223d 6e 61 6d 65 0a 20 20 2a 2a 20 74 68 61 74 20 74  name.  ** that t
2223e 68 65 20 74 61 62 6c 65 20 69 73 20 62 65 69 6e  he table is bein
2223f 67 20 72 65 6e 61 6d 65 64 20 74 6f 2e 0a 20 20  g renamed to..  
22240 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
22241 53 74 72 6c 65 6e 33 30 28 70 54 61 62 2d 3e 7a  Strlen30(pTab->z
22242 4e 61 6d 65 29 3e 36 20 0a 20 20 20 26 26 20 30  Name)>6 .   && 0
22243 3d 3d 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d  ==sqlite3StrNICm
22244 70 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22  p(pTab->zName, "
22245 73 71 6c 69 74 65 5f 22 2c 20 37 29 0a 20 20 29  sqlite_", 7).  )
22246 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
22247 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74  orMsg(pParse, "t
22248 61 62 6c 65 20 25 73 20 6d 61 79 20 6e 6f 74 20  able %s may not 
22249 62 65 20 61 6c 74 65 72 65 64 22 2c 20 70 54 61  be altered", pTa
2224a 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67  b->zName);.    g
2224b 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f  oto exit_rename_
2224c 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 69 66 28  table;.  }.  if(
2224d 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69   SQLITE_OK!=sqli
2224e 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61  te3CheckObjectNa
2224f 6d 65 28 70 50 61 72 73 65 2c 20 7a 4e 61 6d 65  me(pParse, zName
22250 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78  ) ){.    goto ex
22251 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b  it_rename_table;
22252 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  .  }..#ifndef SQ
22253 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20  LITE_OMIT_VIEW. 
22254 20 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c 65   if( pTab->pSele
22255 63 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ct ){.    sqlite
22256 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
22257 2c 20 22 76 69 65 77 20 25 73 20 6d 61 79 20 6e  , "view %s may n
22258 6f 74 20 62 65 20 61 6c 74 65 72 65 64 22 2c 20  ot be altered", 
22259 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  pTab->zName);.  
2225a 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61    goto exit_rena
2225b 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 23 65  me_table;.  }.#e
2225c 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
2225d 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52  LITE_OMIT_AUTHOR
2225e 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 49 6e 76  IZATION.  /* Inv
2225f 6f 6b 65 20 74 68 65 20 61 75 74 68 6f 72 69 7a  oke the authoriz
22260 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 2e 20  ation callback. 
22261 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
22262 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
22263 2c 20 53 51 4c 49 54 45 5f 41 4c 54 45 52 5f 54  , SQLITE_ALTER_T
22264 41 42 4c 45 2c 20 7a 44 62 2c 20 70 54 61 62 2d  ABLE, zDb, pTab-
22265 3e 7a 4e 61 6d 65 2c 20 30 29 20 29 7b 0a 20 20  >zName, 0) ){.  
22266 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61    goto exit_rena
22267 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 23 65  me_table;.  }.#e
22268 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
22269 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
2226a 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 73 71 6c  LTABLE.  if( sql
2226b 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d  ite3ViewGetColum
2226c 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70  nNames(pParse, p
2226d 54 61 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  Tab) ){.    goto
2226e 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62   exit_rename_tab
2226f 6c 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 49 73  le;.  }.  if( Is
22270 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b  Virtual(pTab) ){
22271 0a 20 20 20 20 70 56 54 61 62 20 3d 20 73 71 6c  .    pVTab = sql
22272 69 74 65 33 47 65 74 56 54 61 62 6c 65 28 64 62  ite3GetVTable(db
22273 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 69 66 28  , pTab);.    if(
22274 20 70 56 54 61 62 2d 3e 70 56 74 61 62 2d 3e 70   pVTab->pVtab->p
22275 4d 6f 64 75 6c 65 2d 3e 78 52 65 6e 61 6d 65 3d  Module->xRename=
22276 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 56 54 61  =0 ){.      pVTa
22277 62 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  b = 0;.    }.  }
22278 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 42 65  .#endif..  /* Be
22279 67 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f  gin a transactio
2227a 6e 20 61 6e 64 20 63 6f 64 65 20 74 68 65 20 56  n and code the V
2227b 65 72 69 66 79 43 6f 6f 6b 69 65 20 66 6f 72 20  erifyCookie for 
2227c 64 61 74 61 62 61 73 65 20 69 44 62 2e 20 0a 20  database iDb. . 
2227d 20 2a 2a 20 54 68 65 6e 20 6d 6f 64 69 66 79 20   ** Then modify 
2227e 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69  the schema cooki
2227f 65 20 28 73 69 6e 63 65 20 74 68 65 20 41 4c 54  e (since the ALT
22280 45 52 20 54 41 42 4c 45 20 6d 6f 64 69 66 69 65  ER TABLE modifie
22281 73 20 74 68 65 0a 20 20 2a 2a 20 73 63 68 65 6d  s the.  ** schem
22282 61 29 2e 20 4f 70 65 6e 20 61 20 73 74 61 74 65  a). Open a state
22283 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  ment transaction
22284 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 73   if the table is
22285 20 61 20 76 69 72 74 75 61 6c 0a 20 20 2a 2a 20   a virtual.  ** 
22286 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 76 20  table..  */.  v 
22287 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
22288 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20  (pParse);.  if( 
22289 76 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f  v==0 ){.    goto
2228a 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62   exit_rename_tab
2228b 6c 65 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  le;.  }.  sqlite
2228c 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61  3BeginWriteOpera
2228d 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 70 56 54  tion(pParse, pVT
2228e 61 62 21 3d 30 2c 20 69 44 62 29 3b 0a 20 20 73  ab!=0, iDb);.  s
2228f 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b  qlite3ChangeCook
22290 69 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b  ie(pParse, iDb);
22291 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69  ..  /* If this i
22292 73 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  s a virtual tabl
22293 65 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 78 52  e, invoke the xR
22294 65 6e 61 6d 65 28 29 20 66 75 6e 63 74 69 6f 6e  ename() function
22295 20 69 66 0a 20 20 2a 2a 20 6f 6e 65 20 69 73 20   if.  ** one is 
22296 64 65 66 69 6e 65 64 2e 20 54 68 65 20 78 52 65  defined. The xRe
22297 6e 61 6d 65 28 29 20 63 61 6c 6c 62 61 63 6b 20  name() callback 
22298 77 69 6c 6c 20 6d 6f 64 69 66 79 20 74 68 65 20  will modify the 
22299 6e 61 6d 65 73 0a 20 20 2a 2a 20 6f 66 20 61 6e  names.  ** of an
2229a 79 20 72 65 73 6f 75 72 63 65 73 20 75 73 65 64  y resources used
2229b 20 62 79 20 74 68 65 20 76 2d 74 61 62 6c 65 20   by the v-table 
2229c 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 28  implementation (
2229d 69 6e 63 6c 75 64 69 6e 67 20 6f 74 68 65 72 0a  including other.
2229e 20 20 2a 2a 20 53 51 4c 69 74 65 20 74 61 62 6c    ** SQLite tabl
2229f 65 73 29 20 74 68 61 74 20 61 72 65 20 69 64 65  es) that are ide
222a0 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 20 6e  ntified by the n
222a1 61 6d 65 20 6f 66 20 74 68 65 20 76 69 72 74 75  ame of the virtu
222a2 61 6c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 23  al table..  */.#
222a3 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
222a4 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
222a5 20 20 69 66 28 20 70 56 54 61 62 20 29 7b 0a 20    if( pVTab ){. 
222a6 20 20 20 69 6e 74 20 69 20 3d 20 2b 2b 70 50 61     int i = ++pPa
222a7 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73  rse->nMem;.    s
222a8 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
222a9 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20  (v, OP_String8, 
222aa 30 2c 20 69 2c 20 30 2c 20 7a 4e 61 6d 65 2c 20  0, i, 0, zName, 
222ab 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0);.    sqlite3V
222ac 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
222ad 56 52 65 6e 61 6d 65 2c 20 69 2c 20 30 2c 20 30  VRename, i, 0, 0
222ae 2c 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 56  ,(const char*)pV
222af 54 61 62 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20  Tab, P4_VTAB);. 
222b0 20 20 20 73 71 6c 69 74 65 33 4d 61 79 41 62 6f     sqlite3MayAbo
222b1 72 74 28 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a  rt(pParse);.  }.
222b2 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 66 69 67  #endif..  /* fig
222b3 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79  ure out how many
222b4 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72   UTF-8 character
222b5 73 20 61 72 65 20 69 6e 20 7a 4e 61 6d 65 20 2a  s are in zName *
222b6 2f 0a 20 20 7a 54 61 62 4e 61 6d 65 20 3d 20 70  /.  zTabName = p
222b7 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 6e 54  Tab->zName;.  nT
222b8 61 62 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33  abName = sqlite3
222b9 55 74 66 38 43 68 61 72 4c 65 6e 28 7a 54 61 62  Utf8CharLen(zTab
222ba 4e 61 6d 65 2c 20 2d 31 29 3b 0a 0a 20 20 2f 2a  Name, -1);..  /*
222bb 20 4d 6f 64 69 66 79 20 74 68 65 20 73 71 6c 69   Modify the sqli
222bc 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20  te_master table 
222bd 74 6f 20 75 73 65 20 74 68 65 20 6e 65 77 20 74  to use the new t
222be 61 62 6c 65 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20  able name. */.  
222bf 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72  sqlite3NestedPar
222c0 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20  se(pParse,.     
222c1 20 22 55 50 44 41 54 45 20 25 51 2e 25 73 20 53   "UPDATE %Q.%s S
222c2 45 54 20 22 0a 23 69 66 64 65 66 20 53 51 4c 49  ET ".#ifdef SQLI
222c3 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a  TE_OMIT_TRIGGER.
222c4 20 20 20 20 20 20 20 20 20 20 22 73 71 6c 20 3d            "sql =
222c5 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74   sqlite_rename_t
222c6 61 62 6c 65 28 73 71 6c 2c 20 25 51 29 2c 20 22  able(sql, %Q), "
222c7 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20 20  .#else.         
222c8 20 22 73 71 6c 20 3d 20 43 41 53 45 20 22 0a 20   "sql = CASE ". 
222c9 20 20 20 20 20 20 20 20 20 20 20 22 57 48 45 4e             "WHEN
222ca 20 74 79 70 65 20 3d 20 27 74 72 69 67 67 65 72   type = 'trigger
222cb 27 20 54 48 45 4e 20 73 71 6c 69 74 65 5f 72 65  ' THEN sqlite_re
222cc 6e 61 6d 65 5f 74 72 69 67 67 65 72 28 73 71 6c  name_trigger(sql
222cd 2c 20 25 51 29 22 0a 20 20 20 20 20 20 20 20 20  , %Q)".         
222ce 20 20 20 22 45 4c 53 45 20 73 71 6c 69 74 65 5f     "ELSE sqlite_
222cf 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28 73 71 6c  rename_table(sql
222d0 2c 20 25 51 29 20 45 4e 44 2c 20 22 0a 23 65 6e  , %Q) END, ".#en
222d1 64 69 66 0a 20 20 20 20 20 20 20 20 20 20 22 74  dif.          "t
222d2 62 6c 5f 6e 61 6d 65 20 3d 20 25 51 2c 20 22 0a  bl_name = %Q, ".
222d3 20 20 20 20 20 20 20 20 20 20 22 6e 61 6d 65 20            "name 
222d4 3d 20 43 41 53 45 20 22 0a 20 20 20 20 20 20 20  = CASE ".       
222d5 20 20 20 20 20 22 57 48 45 4e 20 74 79 70 65 3d       "WHEN type=
222d6 27 74 61 62 6c 65 27 20 54 48 45 4e 20 25 51 20  'table' THEN %Q 
222d7 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 57  ".            "W
222d8 48 45 4e 20 6e 61 6d 65 20 4c 49 4b 45 20 27 73  HEN name LIKE 's
222d9 71 6c 69 74 65 5f 61 75 74 6f 69 6e 64 65 78 25  qlite_autoindex%
222da 25 27 20 41 4e 44 20 74 79 70 65 3d 27 69 6e 64  %' AND type='ind
222db 65 78 27 20 54 48 45 4e 20 22 0a 20 20 20 20 20  ex' THEN ".     
222dc 20 20 20 20 20 20 20 20 22 27 73 71 6c 69 74 65          "'sqlite
222dd 5f 61 75 74 6f 69 6e 64 65 78 5f 27 20 7c 7c 20  _autoindex_' || 
222de 25 51 20 7c 7c 20 73 75 62 73 74 72 28 6e 61 6d  %Q || substr(nam
222df 65 2c 25 64 2b 31 38 29 20 22 0a 20 20 20 20 20  e,%d+18) ".     
222e0 20 20 20 20 20 20 20 22 45 4c 53 45 20 6e 61 6d         "ELSE nam
222e1 65 20 45 4e 44 20 22 0a 20 20 20 20 20 20 22 57  e END ".      "W
222e2 48 45 52 45 20 74 62 6c 5f 6e 61 6d 65 3d 25 51  HERE tbl_name=%Q
222e3 20 41 4e 44 20 22 0a 20 20 20 20 20 20 20 20 20   AND ".         
222e4 20 22 28 74 79 70 65 3d 27 74 61 62 6c 65 27 20   "(type='table' 
222e5 4f 52 20 74 79 70 65 3d 27 69 6e 64 65 78 27 20  OR type='index' 
222e6 4f 52 20 74 79 70 65 3d 27 74 72 69 67 67 65 72  OR type='trigger
222e7 27 29 3b 22 2c 20 0a 20 20 20 20 20 20 7a 44 62  ');", .      zDb
222e8 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69  , SCHEMA_TABLE(i
222e9 44 62 29 2c 20 7a 4e 61 6d 65 2c 20 7a 4e 61 6d  Db), zName, zNam
222ea 65 2c 20 7a 4e 61 6d 65 2c 20 0a 23 69 66 6e 64  e, zName, .#ifnd
222eb 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
222ec 52 49 47 47 45 52 0a 20 20 20 20 20 20 7a 4e 61  RIGGER.      zNa
222ed 6d 65 2c 0a 23 65 6e 64 69 66 0a 20 20 20 20 20  me,.#endif.     
222ee 20 7a 4e 61 6d 65 2c 20 6e 54 61 62 4e 61 6d 65   zName, nTabName
222ef 2c 20 7a 54 61 62 4e 61 6d 65 0a 20 20 29 3b 0a  , zTabName.  );.
222f0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
222f1 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45  OMIT_AUTOINCREME
222f2 4e 54 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73  NT.  /* If the s
222f3 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74  qlite_sequence t
222f4 61 62 6c 65 20 65 78 69 73 74 73 20 69 6e 20 74  able exists in t
222f5 68 69 73 20 64 61 74 61 62 61 73 65 2c 20 74 68  his database, th
222f6 65 6e 20 75 70 64 61 74 65 20 0a 20 20 2a 2a 20  en update .  ** 
222f7 69 74 20 77 69 74 68 20 74 68 65 20 6e 65 77 20  it with the new 
222f8 74 61 62 6c 65 20 6e 61 6d 65 2e 0a 20 20 2a 2f  table name..  */
222f9 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69  .  if( sqlite3Fi
222fa 6e 64 54 61 62 6c 65 28 64 62 2c 20 22 73 71 6c  ndTable(db, "sql
222fb 69 74 65 5f 73 65 71 75 65 6e 63 65 22 2c 20 7a  ite_sequence", z
222fc 44 62 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Db) ){.    sqlit
222fd 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50  e3NestedParse(pP
222fe 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 22 55  arse,.        "U
222ff 50 44 41 54 45 20 5c 22 25 77 5c 22 2e 73 71 6c  PDATE \"%w\".sql
22300 69 74 65 5f 73 65 71 75 65 6e 63 65 20 73 65 74  ite_sequence set
22301 20 6e 61 6d 65 20 3d 20 25 51 20 57 48 45 52 45   name = %Q WHERE
22302 20 6e 61 6d 65 20 3d 20 25 51 22 2c 0a 20 20 20   name = %Q",.   
22303 20 20 20 20 20 7a 44 62 2c 20 7a 4e 61 6d 65 2c       zDb, zName,
22304 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20   pTab->zName);. 
22305 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64   }.#endif..#ifnd
22306 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
22307 52 49 47 47 45 52 0a 20 20 2f 2a 20 49 66 20 74  RIGGER.  /* If t
22308 68 65 72 65 20 61 72 65 20 54 45 4d 50 20 74 72  here are TEMP tr
22309 69 67 67 65 72 73 20 6f 6e 20 74 68 69 73 20 74  iggers on this t
2230a 61 62 6c 65 2c 20 6d 6f 64 69 66 79 20 74 68 65  able, modify the
2230b 20 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73   sqlite_temp_mas
2230c 74 65 72 0a 20 20 2a 2a 20 74 61 62 6c 65 2e 20  ter.  ** table. 
2230d 44 6f 6e 27 74 20 64 6f 20 74 68 69 73 20 69 66  Don't do this if
2230e 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   the table being
2230f 20 41 4c 54 45 52 65 64 20 69 73 20 69 74 73 65   ALTERed is itse
22310 6c 66 20 6c 6f 63 61 74 65 64 20 69 6e 0a 20 20  lf located in.  
22311 2a 2a 20 74 68 65 20 74 65 6d 70 20 64 61 74 61  ** the temp data
22312 62 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  base..  */.  if(
22313 20 28 7a 57 68 65 72 65 3d 77 68 65 72 65 54 65   (zWhere=whereTe
22314 6d 70 54 72 69 67 67 65 72 73 28 70 50 61 72 73  mpTriggers(pPars
22315 65 2c 20 70 54 61 62 29 29 21 3d 30 20 29 7b 0a  e, pTab))!=0 ){.
22316 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65      sqlite3Neste
22317 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 20 0a  dParse(pParse, .
22318 20 20 20 20 20 20 20 20 22 55 50 44 41 54 45 20          "UPDATE 
22319 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74  sqlite_temp_mast
2231a 65 72 20 53 45 54 20 22 0a 20 20 20 20 20 20 20  er SET ".       
2231b 20 20 20 20 20 22 73 71 6c 20 3d 20 73 71 6c 69       "sql = sqli
2231c 74 65 5f 72 65 6e 61 6d 65 5f 74 72 69 67 67 65  te_rename_trigge
2231d 72 28 73 71 6c 2c 20 25 51 29 2c 20 22 0a 20 20  r(sql, %Q), ".  
2231e 20 20 20 20 20 20 20 20 20 20 22 74 62 6c 5f 6e            "tbl_n
2231f 61 6d 65 20 3d 20 25 51 20 22 0a 20 20 20 20 20  ame = %Q ".     
22320 20 20 20 20 20 20 20 22 57 48 45 52 45 20 25 73         "WHERE %s
22321 3b 22 2c 20 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65  ;", zName, zName
22322 2c 20 7a 57 68 65 72 65 29 3b 0a 20 20 20 20 73  , zWhere);.    s
22323 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
22324 20 7a 57 68 65 72 65 29 3b 0a 20 20 7d 0a 23 65   zWhere);.  }.#e
22325 6e 64 69 66 0a 0a 20 20 2f 2a 20 44 72 6f 70 20  ndif..  /* Drop 
22326 61 6e 64 20 72 65 6c 6f 61 64 20 74 68 65 20 69  and reload the i
22327 6e 74 65 72 6e 61 6c 20 74 61 62 6c 65 20 73 63  nternal table sc
22328 68 65 6d 61 2e 20 2a 2f 0a 20 20 72 65 6c 6f 61  hema. */.  reloa
22329 64 54 61 62 6c 65 53 63 68 65 6d 61 28 70 50 61  dTableSchema(pPa
2232a 72 73 65 2c 20 70 54 61 62 2c 20 7a 4e 61 6d 65  rse, pTab, zName
2232b 29 3b 0a 0a 65 78 69 74 5f 72 65 6e 61 6d 65 5f  );..exit_rename_
2232c 74 61 62 6c 65 3a 0a 20 20 73 71 6c 69 74 65 33  table:.  sqlite3
2232d 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62  SrcListDelete(db
2232e 2c 20 70 53 72 63 29 3b 0a 20 20 73 71 6c 69 74  , pSrc);.  sqlit
2232f 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e 61  e3DbFree(db, zNa
22330 6d 65 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47  me);.}.../*.** G
22331 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
22332 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 66 69  make sure the fi
22333 6c 65 20 66 6f 72 6d 61 74 20 6e 75 6d 62 65 72  le format number
22334 20 69 73 20 61 74 20 6c 65 61 73 74 20 6d 69 6e   is at least min
22335 46 6f 72 6d 61 74 2e 0a 2a 2a 20 54 68 65 20 67  Format..** The g
22336 65 6e 65 72 61 74 65 64 20 63 6f 64 65 20 77 69  enerated code wi
22337 6c 6c 20 69 6e 63 72 65 61 73 65 20 74 68 65 20  ll increase the 
22338 66 69 6c 65 20 66 6f 72 6d 61 74 20 6e 75 6d 62  file format numb
22339 65 72 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e  er if necessary.
2233a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2233b 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d  TE void sqlite3M
2233c 69 6e 69 6d 75 6d 46 69 6c 65 46 6f 72 6d 61 74  inimumFileFormat
2233d 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
2233e 69 6e 74 20 69 44 62 2c 20 69 6e 74 20 6d 69 6e  int iDb, int min
2233f 46 6f 72 6d 61 74 29 7b 0a 20 20 56 64 62 65 20  Format){.  Vdbe 
22340 2a 76 3b 0a 20 20 76 20 3d 20 73 71 6c 69 74 65  *v;.  v = sqlite
22341 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
22342 3b 0a 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20  ;.  /* The VDBE 
22343 73 68 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e  should have been
22344 20 61 6c 6c 6f 63 61 74 65 64 20 62 65 66 6f 72   allocated befor
22345 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  e this routine i
22346 73 20 63 61 6c 6c 65 64 2e 0a 20 20 2a 2a 20 49  s called..  ** I
22347 66 20 74 68 61 74 20 61 6c 6c 6f 63 61 74 69 6f  f that allocatio
22348 6e 20 66 61 69 6c 65 64 2c 20 77 65 20 77 6f 75  n failed, we wou
22349 6c 64 20 68 61 76 65 20 71 75 69 74 20 62 65 66  ld have quit bef
2234a 6f 72 65 20 72 65 61 63 68 69 6e 67 20 74 68 69  ore reaching thi
2234b 73 0a 20 20 2a 2a 20 70 6f 69 6e 74 20 2a 2f 0a  s.  ** point */.
2234c 20 20 69 66 28 20 41 4c 57 41 59 53 28 76 29 20    if( ALWAYS(v) 
2234d 29 7b 0a 20 20 20 20 69 6e 74 20 72 31 20 3d 20  ){.    int r1 = 
2234e 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
2234f 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69  g(pParse);.    i
22350 6e 74 20 72 32 20 3d 20 73 71 6c 69 74 65 33 47  nt r2 = sqlite3G
22351 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
22352 29 3b 0a 20 20 20 20 69 6e 74 20 6a 31 3b 0a 20  );.    int j1;. 
22353 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22354 64 4f 70 33 28 76 2c 20 4f 50 5f 52 65 61 64 43  dOp3(v, OP_ReadC
22355 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 72 31 2c 20  ookie, iDb, r1, 
22356 42 54 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41  BTREE_FILE_FORMA
22357 54 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  T);.    sqlite3V
22358 64 62 65 55 73 65 73 42 74 72 65 65 28 76 2c 20  dbeUsesBtree(v, 
22359 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  iDb);.    sqlite
2235a 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2235b 50 5f 49 6e 74 65 67 65 72 2c 20 6d 69 6e 46 6f  P_Integer, minFo
2235c 72 6d 61 74 2c 20 72 32 29 3b 0a 20 20 20 20 6a  rmat, r2);.    j
2235d 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  1 = sqlite3VdbeA
2235e 64 64 4f 70 33 28 76 2c 20 4f 50 5f 47 65 2c 20  ddOp3(v, OP_Ge, 
2235f 72 32 2c 20 30 2c 20 72 31 29 3b 0a 20 20 20 20  r2, 0, r1);.    
22360 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
22361 33 28 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b 69  3(v, OP_SetCooki
22362 65 2c 20 69 44 62 2c 20 42 54 52 45 45 5f 46 49  e, iDb, BTREE_FI
22363 4c 45 5f 46 4f 52 4d 41 54 2c 20 72 32 29 3b 0a  LE_FORMAT, r2);.
22364 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
22365 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a  umpHere(v, j1);.
22366 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61      sqlite3Relea
22367 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
22368 2c 20 72 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  , r1);.    sqlit
22369 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
2236a 28 70 50 61 72 73 65 2c 20 72 32 29 3b 0a 20 20  (pParse, r2);.  
2236b 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  }.}../*.** This 
2236c 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
2236d 65 64 20 61 66 74 65 72 20 61 6e 20 22 41 4c 54  ed after an "ALT
2236e 45 52 20 54 41 42 4c 45 20 2e 2e 2e 20 41 44 44  ER TABLE ... ADD
2236f 22 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 68  " statement.** h
22370 61 73 20 62 65 65 6e 20 70 61 72 73 65 64 2e 20  as been parsed. 
22371 41 72 67 75 6d 65 6e 74 20 70 43 6f 6c 44 65 66  Argument pColDef
22372 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 65   contains the te
22373 78 74 20 6f 66 20 74 68 65 20 6e 65 77 0a 2a 2a  xt of the new.**
22374 20 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69   column definiti
22375 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 54 61  on..**.** The Ta
22376 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 70 50  ble structure pP
22377 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20  arse->pNewTable 
22378 77 61 73 20 65 78 74 65 6e 64 65 64 20 74 6f 20  was extended to 
22379 69 6e 63 6c 75 64 65 0a 2a 2a 20 74 68 65 20 6e  include.** the n
2237a 65 77 20 63 6f 6c 75 6d 6e 20 64 75 72 69 6e 67  ew column during
2237b 20 70 61 72 73 69 6e 67 2e 0a 2a 2f 0a 53 51 4c   parsing..*/.SQL
2237c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2237d 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46 69 6e   sqlite3AlterFin
2237e 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72  ishAddColumn(Par
2237f 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65  se *pParse, Toke
22380 6e 20 2a 70 43 6f 6c 44 65 66 29 7b 0a 20 20 54  n *pColDef){.  T
22381 61 62 6c 65 20 2a 70 4e 65 77 3b 20 20 20 20 20  able *pNew;     
22382 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79           /* Copy
22383 20 6f 66 20 70 50 61 72 73 65 2d 3e 70 4e 65 77   of pParse->pNew
22384 54 61 62 6c 65 20 2a 2f 0a 20 20 54 61 62 6c 65  Table */.  Table
22385 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20   *pTab;         
22386 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 62 65       /* Table be
22387 69 6e 67 20 61 6c 74 65 72 65 64 20 2a 2f 0a 20  ing altered */. 
22388 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20   int iDb;       
22389 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
2238a 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 2a 2f  tabase number */
2238b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2238c 44 62 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  Db;          /* 
2238d 44 61 74 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f  Database name */
2238e 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2238f 54 61 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Tab;         /* 
22390 54 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20  Table name */.  
22391 63 68 61 72 20 2a 7a 43 6f 6c 3b 20 20 20 20 20  char *zCol;     
22392 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6c            /* Nul
22393 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 63 6f 6c  l-terminated col
22394 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 2a  umn definition *
22395 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c  /.  Column *pCol
22396 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
22397 20 54 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20   The new column 
22398 2a 2f 0a 20 20 45 78 70 72 20 2a 70 44 66 6c 74  */.  Expr *pDflt
22399 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
2239a 2a 20 44 65 66 61 75 6c 74 20 76 61 6c 75 65 20  * Default value 
2239b 66 6f 72 20 74 68 65 20 6e 65 77 20 63 6f 6c 75  for the new colu
2239c 6d 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  mn */.  sqlite3 
2239d 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  *db;            
2239e 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
2239f 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 3b 20 2a 2f  e connection; */
223a0 0a 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d  ..  db = pParse-
223a1 3e 64 62 3b 0a 20 20 69 66 28 20 70 50 61 72 73  >db;.  if( pPars
223a2 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d  e->nErr || db->m
223a3 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65  allocFailed ) re
223a4 74 75 72 6e 3b 0a 20 20 70 4e 65 77 20 3d 20 70  turn;.  pNew = p
223a5 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
223a6 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65 77  ;.  assert( pNew
223a7 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73   );..  assert( s
223a8 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73  qlite3BtreeHolds
223a9 41 6c 6c 4d 75 74 65 78 65 73 28 64 62 29 20 29  AllMutexes(db) )
223aa 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65  ;.  iDb = sqlite
223ab 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64  3SchemaToIndex(d
223ac 62 2c 20 70 4e 65 77 2d 3e 70 53 63 68 65 6d 61  b, pNew->pSchema
223ad 29 3b 0a 20 20 7a 44 62 20 3d 20 64 62 2d 3e 61  );.  zDb = db->a
223ae 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20  Db[iDb].zName;. 
223af 20 7a 54 61 62 20 3d 20 26 70 4e 65 77 2d 3e 7a   zTab = &pNew->z
223b0 4e 61 6d 65 5b 31 36 5d 3b 20 20 2f 2a 20 53 6b  Name[16];  /* Sk
223b1 69 70 20 74 68 65 20 22 73 71 6c 69 74 65 5f 61  ip the "sqlite_a
223b2 6c 74 65 72 74 61 62 5f 22 20 70 72 65 66 69 78  ltertab_" prefix
223b3 20 6f 6e 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a   on the name */.
223b4 20 20 70 43 6f 6c 20 3d 20 26 70 4e 65 77 2d 3e    pCol = &pNew->
223b5 61 43 6f 6c 5b 70 4e 65 77 2d 3e 6e 43 6f 6c 2d  aCol[pNew->nCol-
223b6 31 5d 3b 0a 20 20 70 44 66 6c 74 20 3d 20 70 43  1];.  pDflt = pC
223b7 6f 6c 2d 3e 70 44 66 6c 74 3b 0a 20 20 70 54 61  ol->pDflt;.  pTa
223b8 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54  b = sqlite3FindT
223b9 61 62 6c 65 28 64 62 2c 20 7a 54 61 62 2c 20 7a  able(db, zTab, z
223ba 44 62 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  Db);.  assert( p
223bb 54 61 62 20 29 3b 0a 0a 23 69 66 6e 64 65 66 20  Tab );..#ifndef 
223bc 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48  SQLITE_OMIT_AUTH
223bd 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 49  ORIZATION.  /* I
223be 6e 76 6f 6b 65 20 74 68 65 20 61 75 74 68 6f 72  nvoke the author
223bf 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b  ization callback
223c0 2e 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74  . */.  if( sqlit
223c1 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
223c2 73 65 2c 20 53 51 4c 49 54 45 5f 41 4c 54 45 52  se, SQLITE_ALTER
223c3 5f 54 41 42 4c 45 2c 20 7a 44 62 2c 20 70 54 61  _TABLE, zDb, pTa
223c4 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 20 29 7b 0a  b->zName, 0) ){.
223c5 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
223c6 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20  #endif..  /* If 
223c7 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75  the default valu
223c8 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20 63 6f  e for the new co
223c9 6c 75 6d 6e 20 77 61 73 20 73 70 65 63 69 66 69  lumn was specifi
223ca 65 64 20 77 69 74 68 20 61 20 0a 20 20 2a 2a 20  ed with a .  ** 
223cb 6c 69 74 65 72 61 6c 20 4e 55 4c 4c 2c 20 74 68  literal NULL, th
223cc 65 6e 20 73 65 74 20 70 44 66 6c 74 20 74 6f 20  en set pDflt to 
223cd 30 2e 20 54 68 69 73 20 73 69 6d 70 6c 69 66 69  0. This simplifi
223ce 65 73 20 63 68 65 63 6b 69 6e 67 0a 20 20 2a 2a  es checking.  **
223cf 20 66 6f 72 20 61 6e 20 53 51 4c 20 4e 55 4c 4c   for an SQL NULL
223d0 20 64 65 66 61 75 6c 74 20 62 65 6c 6f 77 2e 0a   default below..
223d1 20 20 2a 2f 0a 20 20 69 66 28 20 70 44 66 6c 74    */.  if( pDflt
223d2 20 26 26 20 70 44 66 6c 74 2d 3e 6f 70 3d 3d 54   && pDflt->op==T
223d3 4b 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 70 44  K_NULL ){.    pD
223d4 66 6c 74 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20  flt = 0;.  }..  
223d5 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68  /* Check that th
223d6 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 69 73 20  e new column is 
223d7 6e 6f 74 20 73 70 65 63 69 66 69 65 64 20 61 73  not specified as
223d8 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20   PRIMARY KEY or 
223d9 55 4e 49 51 55 45 2e 0a 20 20 2a 2a 20 49 66 20  UNIQUE..  ** If 
223da 74 68 65 72 65 20 69 73 20 61 20 4e 4f 54 20 4e  there is a NOT N
223db 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 2c 20  ULL constraint, 
223dc 74 68 65 6e 20 74 68 65 20 64 65 66 61 75 6c 74  then the default
223dd 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 0a 20   value for the. 
223de 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6d 75 73 74 20   ** column must 
223df 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e 0a 20 20 2a  not be NULL..  *
223e0 2f 0a 20 20 69 66 28 20 70 43 6f 6c 2d 3e 69 73  /.  if( pCol->is
223e1 50 72 69 6d 4b 65 79 20 29 7b 0a 20 20 20 20 73  PrimKey ){.    s
223e2 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
223e3 50 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74 20 61  Parse, "Cannot a
223e4 64 64 20 61 20 50 52 49 4d 41 52 59 20 4b 45 59  dd a PRIMARY KEY
223e5 20 63 6f 6c 75 6d 6e 22 29 3b 0a 20 20 20 20 72   column");.    r
223e6 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28  eturn;.  }.  if(
223e7 20 70 4e 65 77 2d 3e 70 49 6e 64 65 78 20 29 7b   pNew->pIndex ){
223e8 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
223e9 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 43 61  rMsg(pParse, "Ca
223ea 6e 6e 6f 74 20 61 64 64 20 61 20 55 4e 49 51 55  nnot add a UNIQU
223eb 45 20 63 6f 6c 75 6d 6e 22 29 3b 0a 20 20 20 20  E column");.    
223ec 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66  return;.  }.  if
223ed 28 20 70 43 6f 6c 2d 3e 6e 6f 74 4e 75 6c 6c 20  ( pCol->notNull 
223ee 26 26 20 21 70 44 66 6c 74 20 29 7b 0a 20 20 20  && !pDflt ){.   
223ef 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
223f0 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20  (pParse, .      
223f1 20 20 22 43 61 6e 6e 6f 74 20 61 64 64 20 61 20    "Cannot add a 
223f2 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6c 75 6d 6e 20  NOT NULL column 
223f3 77 69 74 68 20 64 65 66 61 75 6c 74 20 76 61 6c  with default val
223f4 75 65 20 4e 55 4c 4c 22 29 3b 0a 20 20 20 20 72  ue NULL");.    r
223f5 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  eturn;.  }..  /*
223f6 20 45 6e 73 75 72 65 20 74 68 65 20 64 65 66 61   Ensure the defa
223f7 75 6c 74 20 65 78 70 72 65 73 73 69 6f 6e 20 69  ult expression i
223f8 73 20 73 6f 6d 65 74 68 69 6e 67 20 74 68 61 74  s something that
223f9 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f   sqlite3ValueFro
223fa 6d 45 78 70 72 28 29 0a 20 20 2a 2a 20 63 61 6e  mExpr().  ** can
223fb 20 68 61 6e 64 6c 65 20 28 69 2e 65 2e 20 6e 6f   handle (i.e. no
223fc 74 20 43 55 52 52 45 4e 54 5f 54 49 4d 45 20 65  t CURRENT_TIME e
223fd 74 63 2e 29 0a 20 20 2a 2f 0a 20 20 69 66 28 20  tc.).  */.  if( 
223fe 70 44 66 6c 74 20 29 7b 0a 20 20 20 20 73 71 6c  pDflt ){.    sql
223ff 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c  ite3_value *pVal
22400 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
22401 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 64  3ValueFromExpr(d
22402 62 2c 20 70 44 66 6c 74 2c 20 53 51 4c 49 54 45  b, pDflt, SQLITE
22403 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 41 46  _UTF8, SQLITE_AF
22404 46 5f 4e 4f 4e 45 2c 20 26 70 56 61 6c 29 20 29  F_NONE, &pVal) )
22405 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c  {.      db->mall
22406 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
22407 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
22408 7d 0a 20 20 20 20 69 66 28 20 21 70 56 61 6c 20  }.    if( !pVal 
22409 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2240a 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
2240b 20 22 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 63   "Cannot add a c
2240c 6f 6c 75 6d 6e 20 77 69 74 68 20 6e 6f 6e 2d 63  olumn with non-c
2240d 6f 6e 73 74 61 6e 74 20 64 65 66 61 75 6c 74 22  onstant default"
2240e 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b  );.      return;
2240f 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
22410 65 33 56 61 6c 75 65 46 72 65 65 28 70 56 61 6c  e3ValueFree(pVal
22411 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 6f 64  );.  }..  /* Mod
22412 69 66 79 20 74 68 65 20 43 52 45 41 54 45 20 54  ify the CREATE T
22413 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 20  ABLE statement. 
22414 2a 2f 0a 20 20 7a 43 6f 6c 20 3d 20 73 71 6c 69  */.  zCol = sqli
22415 74 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c  te3DbStrNDup(db,
22416 20 28 63 68 61 72 2a 29 70 43 6f 6c 44 65 66 2d   (char*)pColDef-
22417 3e 7a 2c 20 70 43 6f 6c 44 65 66 2d 3e 6e 29 3b  >z, pColDef->n);
22418 0a 20 20 69 66 28 20 7a 43 6f 6c 20 29 7b 0a 20  .  if( zCol ){. 
22419 20 20 20 63 68 61 72 20 2a 7a 45 6e 64 20 3d 20     char *zEnd = 
2241a 26 7a 43 6f 6c 5b 70 43 6f 6c 44 65 66 2d 3e 6e  &zCol[pColDef->n
2241b 2d 31 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  -1];.    while( 
2241c 7a 45 6e 64 3e 7a 43 6f 6c 20 26 26 20 28 2a 7a  zEnd>zCol && (*z
2241d 45 6e 64 3d 3d 27 3b 27 20 7c 7c 20 73 71 6c 69  End==';' || sqli
2241e 74 65 33 49 73 73 70 61 63 65 28 2a 7a 45 6e 64  te3Isspace(*zEnd
2241f 29 29 20 29 7b 0a 20 20 20 20 20 20 2a 7a 45 6e  )) ){.      *zEn
22420 64 2d 2d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20  d-- = '\0';.    
22421 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73  }.    sqlite3Nes
22422 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c  tedParse(pParse,
22423 20 0a 20 20 20 20 20 20 20 20 22 55 50 44 41 54   .        "UPDAT
22424 45 20 5c 22 25 77 5c 22 2e 25 73 20 53 45 54 20  E \"%w\".%s SET 
22425 22 0a 20 20 20 20 20 20 20 20 20 20 22 73 71 6c  ".          "sql
22426 20 3d 20 73 75 62 73 74 72 28 73 71 6c 2c 31 2c   = substr(sql,1,
22427 25 64 29 20 7c 7c 20 27 2c 20 27 20 7c 7c 20 25  %d) || ', ' || %
22428 51 20 7c 7c 20 73 75 62 73 74 72 28 73 71 6c 2c  Q || substr(sql,
22429 25 64 29 20 22 0a 20 20 20 20 20 20 20 20 22 57  %d) ".        "W
2242a 48 45 52 45 20 74 79 70 65 20 3d 20 27 74 61 62  HERE type = 'tab
2242b 6c 65 27 20 41 4e 44 20 6e 61 6d 65 20 3d 20 25  le' AND name = %
2242c 51 22 2c 20 0a 20 20 20 20 20 20 7a 44 62 2c 20  Q", .      zDb, 
2242d 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62  SCHEMA_TABLE(iDb
2242e 29 2c 20 70 4e 65 77 2d 3e 61 64 64 43 6f 6c 4f  ), pNew->addColO
2242f 66 66 73 65 74 2c 20 7a 43 6f 6c 2c 20 70 4e 65  ffset, zCol, pNe
22430 77 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 2b  w->addColOffset+
22431 31 2c 0a 20 20 20 20 20 20 7a 54 61 62 0a 20 20  1,.      zTab.  
22432 20 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33    );.    sqlite3
22433 44 62 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c 29  DbFree(db, zCol)
22434 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  ;.  }..  /* If t
22435 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  he default value
22436 20 6f 66 20 74 68 65 20 6e 65 77 20 63 6f 6c 75   of the new colu
22437 6d 6e 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e  mn is NULL, then
22438 20 73 65 74 20 74 68 65 20 66 69 6c 65 0a 20 20   set the file.  
22439 2a 2a 20 66 6f 72 6d 61 74 20 74 6f 20 32 2e 20  ** format to 2. 
2243a 49 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 76  If the default v
2243b 61 6c 75 65 20 6f 66 20 74 68 65 20 6e 65 77 20  alue of the new 
2243c 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 4e 55  column is not NU
2243d 4c 4c 2c 0a 20 20 2a 2a 20 74 68 65 20 66 69 6c  LL,.  ** the fil
2243e 65 20 66 6f 72 6d 61 74 20 62 65 63 6f 6d 65 73  e format becomes
2243f 20 33 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74   3..  */.  sqlit
22440 65 33 4d 69 6e 69 6d 75 6d 46 69 6c 65 46 6f 72  e3MinimumFileFor
22441 6d 61 74 28 70 50 61 72 73 65 2c 20 69 44 62 2c  mat(pParse, iDb,
22442 20 70 44 66 6c 74 20 3f 20 33 20 3a 20 32 29 3b   pDflt ? 3 : 2);
22443 0a 0a 20 20 2f 2a 20 52 65 6c 6f 61 64 20 74 68  ..  /* Reload th
22444 65 20 73 63 68 65 6d 61 20 6f 66 20 74 68 65 20  e schema of the 
22445 6d 6f 64 69 66 69 65 64 20 74 61 62 6c 65 2e 20  modified table. 
22446 2a 2f 0a 20 20 72 65 6c 6f 61 64 54 61 62 6c 65  */.  reloadTable
22447 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 70  Schema(pParse, p
22448 54 61 62 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  Tab, pTab->zName
22449 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  );.}../*.** This
2244a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
2244b 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65  led by the parse
2244c 72 20 61 66 74 65 72 20 74 68 65 20 74 61 62 6c  r after the tabl
2244d 65 2d 6e 61 6d 65 20 69 6e 0a 2a 2a 20 61 6e 20  e-name in.** an 
2244e 22 41 4c 54 45 52 20 54 41 42 4c 45 20 3c 74 61  "ALTER TABLE <ta
2244f 62 6c 65 2d 6e 61 6d 65 3e 20 41 44 44 22 20 73  ble-name> ADD" s
22450 74 61 74 65 6d 65 6e 74 20 69 73 20 70 61 72 73  tatement is pars
22451 65 64 2e 20 41 72 67 75 6d 65 6e 74 20 0a 2a 2a  ed. Argument .**
22452 20 70 53 72 63 20 69 73 20 74 68 65 20 66 75 6c   pSrc is the ful
22453 6c 2d 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  l-name of the ta
22454 62 6c 65 20 62 65 69 6e 67 20 61 6c 74 65 72 65  ble being altere
22455 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  d..**.** This ro
22456 75 74 69 6e 65 20 6d 61 6b 65 73 20 61 20 28 70  utine makes a (p
22457 61 72 74 69 61 6c 29 20 63 6f 70 79 20 6f 66 20  artial) copy of 
22458 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74  the Table struct
22459 75 72 65 0a 2a 2a 20 66 6f 72 20 74 68 65 20 74  ure.** for the t
2245a 61 62 6c 65 20 62 65 69 6e 67 20 61 6c 74 65 72  able being alter
2245b 65 64 20 61 6e 64 20 73 65 74 73 20 50 61 72 73  ed and sets Pars
2245c 65 2e 70 4e 65 77 54 61 62 6c 65 20 74 6f 20 70  e.pNewTable to p
2245d 6f 69 6e 74 0a 2a 2a 20 74 6f 20 69 74 2e 20 52  oint.** to it. R
2245e 6f 75 74 69 6e 65 73 20 63 61 6c 6c 65 64 20 62  outines called b
2245f 79 20 74 68 65 20 70 61 72 73 65 72 20 61 73 20  y the parser as 
22460 74 68 65 20 63 6f 6c 75 6d 6e 20 64 65 66 69 6e  the column defin
22461 69 74 69 6f 6e 0a 2a 2a 20 69 73 20 70 61 72 73  ition.** is pars
22462 65 64 20 28 69 2e 65 2e 20 73 71 6c 69 74 65 33  ed (i.e. sqlite3
22463 41 64 64 43 6f 6c 75 6d 6e 28 29 29 20 61 64 64  AddColumn()) add
22464 20 74 68 65 20 6e 65 77 20 43 6f 6c 75 6d 6e 20   the new Column 
22465 64 61 74 61 20 74 6f 20 0a 2a 2a 20 74 68 65 20  data to .** the 
22466 63 6f 70 79 2e 20 54 68 65 20 63 6f 70 79 20 6f  copy. The copy o
22467 66 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75  f the Table stru
22468 63 74 75 72 65 20 69 73 20 64 65 6c 65 74 65 64  cture is deleted
22469 20 62 79 20 74 6f 6b 65 6e 69 7a 65 2e 63 20 0a   by tokenize.c .
2246a 2a 2a 20 61 66 74 65 72 20 70 61 72 73 69 6e 67  ** after parsing
2246b 20 69 73 20 66 69 6e 69 73 68 65 64 2e 0a 2a 2a   is finished..**
2246c 0a 2a 2a 20 52 6f 75 74 69 6e 65 20 73 71 6c 69  .** Routine sqli
2246d 74 65 33 41 6c 74 65 72 46 69 6e 69 73 68 41 64  te3AlterFinishAd
2246e 64 43 6f 6c 75 6d 6e 28 29 20 77 69 6c 6c 20 62  dColumn() will b
2246f 65 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f 6d 70  e called to comp
22470 6c 65 74 65 0a 2a 2a 20 63 6f 64 69 6e 67 20 74  lete.** coding t
22471 68 65 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20  he "ALTER TABLE 
22472 2e 2e 2e 20 41 44 44 22 20 73 74 61 74 65 6d 65  ... ADD" stateme
22473 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  nt..*/.SQLITE_PR
22474 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
22475 65 33 41 6c 74 65 72 42 65 67 69 6e 41 64 64 43  e3AlterBeginAddC
22476 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a 70 50 61  olumn(Parse *pPa
22477 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 53  rse, SrcList *pS
22478 72 63 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 4e  rc){.  Table *pN
22479 65 77 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61  ew;.  Table *pTa
2247a 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20  b;.  Vdbe *v;.  
2247b 69 6e 74 20 69 44 62 3b 0a 20 20 69 6e 74 20 69  int iDb;.  int i
2247c 3b 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 0a  ;.  int nAlloc;.
2247d 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
2247e 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 2f  pParse->db;..  /
2247f 2a 20 4c 6f 6f 6b 20 75 70 20 74 68 65 20 74 61  * Look up the ta
22480 62 6c 65 20 62 65 69 6e 67 20 61 6c 74 65 72 65  ble being altere
22481 64 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  d. */.  assert( 
22482 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
22483 65 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  e==0 );.  assert
22484 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f  ( sqlite3BtreeHo
22485 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 64 62  ldsAllMutexes(db
22486 29 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d  ) );.  if( db->m
22487 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f  allocFailed ) go
22488 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64  to exit_begin_ad
22489 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 70 54 61 62  d_column;.  pTab
2248a 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65   = sqlite3Locate
2248b 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c  Table(pParse, 0,
2248c 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d   pSrc->a[0].zNam
2248d 65 2c 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 7a 44  e, pSrc->a[0].zD
2248e 61 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20  atabase);.  if( 
2248f 21 70 54 61 62 20 29 20 67 6f 74 6f 20 65 78 69  !pTab ) goto exi
22490 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75  t_begin_add_colu
22491 6d 6e 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  mn;..#ifndef SQL
22492 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
22493 54 41 42 4c 45 0a 20 20 69 66 28 20 49 73 56 69  TABLE.  if( IsVi
22494 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20  rtual(pTab) ){. 
22495 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
22496 73 67 28 70 50 61 72 73 65 2c 20 22 76 69 72 74  sg(pParse, "virt
22497 75 61 6c 20 74 61 62 6c 65 73 20 6d 61 79 20 6e  ual tables may n
22498 6f 74 20 62 65 20 61 6c 74 65 72 65 64 22 29 3b  ot be altered");
22499 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 62  .    goto exit_b
2249a 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b  egin_add_column;
2249b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
2249c 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 69 73  * Make sure this
2249d 20 69 73 20 6e 6f 74 20 61 6e 20 61 74 74 65 6d   is not an attem
2249e 70 74 20 74 6f 20 41 4c 54 45 52 20 61 20 76 69  pt to ALTER a vi
2249f 65 77 2e 20 2a 2f 0a 20 20 69 66 28 20 70 54 61  ew. */.  if( pTa
224a0 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20  b->pSelect ){.  
224a1 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
224a2 67 28 70 50 61 72 73 65 2c 20 22 43 61 6e 6e 6f  g(pParse, "Canno
224a3 74 20 61 64 64 20 61 20 63 6f 6c 75 6d 6e 20 74  t add a column t
224a4 6f 20 61 20 76 69 65 77 22 29 3b 0a 20 20 20 20  o a view");.    
224a5 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f  goto exit_begin_
224a6 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a  add_column;.  }.
224a7 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d  .  assert( pTab-
224a8 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 3e 30 20  >addColOffset>0 
224a9 29 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74  );.  iDb = sqlit
224aa 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
224ab 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
224ac 61 29 3b 0a 0a 20 20 2f 2a 20 50 75 74 20 61 20  a);..  /* Put a 
224ad 63 6f 70 79 20 6f 66 20 74 68 65 20 54 61 62 6c  copy of the Tabl
224ae 65 20 73 74 72 75 63 74 20 69 6e 20 50 61 72 73  e struct in Pars
224af 65 2e 70 4e 65 77 54 61 62 6c 65 20 66 6f 72 20  e.pNewTable for 
224b0 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33  the.  ** sqlite3
224b1 41 64 64 43 6f 6c 75 6d 6e 28 29 20 66 75 6e 63  AddColumn() func
224b2 74 69 6f 6e 20 61 6e 64 20 66 72 69 65 6e 64 73  tion and friends
224b3 20 74 6f 20 6d 6f 64 69 66 79 2e 20 20 42 75 74   to modify.  But
224b4 20 6d 6f 64 69 66 79 0a 20 20 2a 2a 20 74 68 65   modify.  ** the
224b5 20 6e 61 6d 65 20 62 79 20 61 64 64 69 6e 67 20   name by adding 
224b6 61 6e 20 22 73 71 6c 69 74 65 5f 61 6c 74 65 72  an "sqlite_alter
224b7 74 61 62 5f 22 20 70 72 65 66 69 78 2e 20 20 42  tab_" prefix.  B
224b8 79 20 61 64 64 69 6e 67 20 74 68 69 73 0a 20 20  y adding this.  
224b9 2a 2a 20 70 72 65 66 69 78 2c 20 77 65 20 69 6e  ** prefix, we in
224ba 73 75 72 65 20 74 68 61 74 20 74 68 65 20 6e 61  sure that the na
224bb 6d 65 20 77 69 6c 6c 20 6e 6f 74 20 63 6f 6c 6c  me will not coll
224bc 69 64 65 20 77 69 74 68 20 61 6e 20 65 78 69 73  ide with an exis
224bd 74 69 6e 67 0a 20 20 2a 2a 20 74 61 62 6c 65 20  ting.  ** table 
224be 62 65 63 61 75 73 65 20 75 73 65 72 20 74 61 62  because user tab
224bf 6c 65 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77  le are not allow
224c0 65 64 20 74 6f 20 68 61 76 65 20 74 68 65 20 22  ed to have the "
224c1 73 71 6c 69 74 65 5f 22 0a 20 20 2a 2a 20 70 72  sqlite_".  ** pr
224c2 65 66 69 78 20 6f 6e 20 74 68 65 69 72 20 6e 61  efix on their na
224c3 6d 65 2e 0a 20 20 2a 2f 0a 20 20 70 4e 65 77 20  me..  */.  pNew 
224c4 3d 20 28 54 61 62 6c 65 2a 29 73 71 6c 69 74 65  = (Table*)sqlite
224c5 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
224c6 2c 20 73 69 7a 65 6f 66 28 54 61 62 6c 65 29 29  , sizeof(Table))
224c7 3b 0a 20 20 69 66 28 20 21 70 4e 65 77 20 29 20  ;.  if( !pNew ) 
224c8 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f  goto exit_begin_
224c9 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 70 50  add_column;.  pP
224ca 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20  arse->pNewTable 
224cb 3d 20 70 4e 65 77 3b 0a 20 20 70 4e 65 77 2d 3e  = pNew;.  pNew->
224cc 6e 52 65 66 20 3d 20 31 3b 0a 20 20 70 4e 65 77  nRef = 1;.  pNew
224cd 2d 3e 64 62 4d 65 6d 20 3d 20 70 54 61 62 2d 3e  ->dbMem = pTab->
224ce 64 62 4d 65 6d 3b 0a 20 20 70 4e 65 77 2d 3e 6e  dbMem;.  pNew->n
224cf 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c  Col = pTab->nCol
224d0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65 77  ;.  assert( pNew
224d1 2d 3e 6e 43 6f 6c 3e 30 20 29 3b 0a 20 20 6e 41  ->nCol>0 );.  nA
224d2 6c 6c 6f 63 20 3d 20 28 28 28 70 4e 65 77 2d 3e  lloc = (((pNew->
224d3 6e 43 6f 6c 2d 31 29 2f 38 29 2a 38 29 2b 38 3b  nCol-1)/8)*8)+8;
224d4 0a 20 20 61 73 73 65 72 74 28 20 6e 41 6c 6c 6f  .  assert( nAllo
224d5 63 3e 3d 70 4e 65 77 2d 3e 6e 43 6f 6c 20 26 26  c>=pNew->nCol &&
224d6 20 6e 41 6c 6c 6f 63 25 38 3d 3d 30 20 26 26 20   nAlloc%8==0 && 
224d7 6e 41 6c 6c 6f 63 2d 70 4e 65 77 2d 3e 6e 43 6f  nAlloc-pNew->nCo
224d8 6c 3c 38 20 29 3b 0a 20 20 70 4e 65 77 2d 3e 61  l<8 );.  pNew->a
224d9 43 6f 6c 20 3d 20 28 43 6f 6c 75 6d 6e 2a 29 73  Col = (Column*)s
224da 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
224db 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 43 6f  ro(db, sizeof(Co
224dc 6c 75 6d 6e 29 2a 6e 41 6c 6c 6f 63 29 3b 0a 20  lumn)*nAlloc);. 
224dd 20 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20 73   pNew->zName = s
224de 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
224df 2c 20 22 73 71 6c 69 74 65 5f 61 6c 74 65 72 74  , "sqlite_altert
224e0 61 62 5f 25 73 22 2c 20 70 54 61 62 2d 3e 7a 4e  ab_%s", pTab->zN
224e1 61 6d 65 29 3b 0a 20 20 69 66 28 20 21 70 4e 65  ame);.  if( !pNe
224e2 77 2d 3e 61 43 6f 6c 20 7c 7c 20 21 70 4e 65 77  w->aCol || !pNew
224e3 2d 3e 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 64  ->zName ){.    d
224e4 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
224e5 3d 20 31 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78  = 1;.    goto ex
224e6 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c  it_begin_add_col
224e7 75 6d 6e 3b 0a 20 20 7d 0a 20 20 6d 65 6d 63 70  umn;.  }.  memcp
224e8 79 28 70 4e 65 77 2d 3e 61 43 6f 6c 2c 20 70 54  y(pNew->aCol, pT
224e9 61 62 2d 3e 61 43 6f 6c 2c 20 73 69 7a 65 6f 66  ab->aCol, sizeof
224ea 28 43 6f 6c 75 6d 6e 29 2a 70 4e 65 77 2d 3e 6e  (Column)*pNew->n
224eb 43 6f 6c 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  Col);.  for(i=0;
224ec 20 69 3c 70 4e 65 77 2d 3e 6e 43 6f 6c 3b 20 69   i<pNew->nCol; i
224ed 2b 2b 29 7b 0a 20 20 20 20 43 6f 6c 75 6d 6e 20  ++){.    Column 
224ee 2a 70 43 6f 6c 20 3d 20 26 70 4e 65 77 2d 3e 61  *pCol = &pNew->a
224ef 43 6f 6c 5b 69 5d 3b 0a 20 20 20 20 70 43 6f 6c  Col[i];.    pCol
224f0 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65  ->zName = sqlite
224f1 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 43  3DbStrDup(db, pC
224f2 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ol->zName);.    
224f3 70 43 6f 6c 2d 3e 7a 43 6f 6c 6c 20 3d 20 30 3b  pCol->zColl = 0;
224f4 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 54 79 70 65  .    pCol->zType
224f5 20 3d 20 30 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e   = 0;.    pCol->
224f6 70 44 66 6c 74 20 3d 20 30 3b 0a 20 20 20 20 70  pDflt = 0;.    p
224f7 43 6f 6c 2d 3e 7a 44 66 6c 74 20 3d 20 30 3b 0a  Col->zDflt = 0;.
224f8 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 70 53 63 68    }.  pNew->pSch
224f9 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44  ema = db->aDb[iD
224fa 62 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20 70 4e  b].pSchema;.  pN
224fb 65 77 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74  ew->addColOffset
224fc 20 3d 20 70 54 61 62 2d 3e 61 64 64 43 6f 6c 4f   = pTab->addColO
224fd 66 66 73 65 74 3b 0a 20 20 70 4e 65 77 2d 3e 6e  ffset;.  pNew->n
224fe 52 65 66 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 42  Ref = 1;..  /* B
224ff 65 67 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69  egin a transacti
22500 6f 6e 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e 74  on and increment
22501 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b   the schema cook
22502 69 65 2e 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ie.  */.  sqlite
22503 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61  3BeginWriteOpera
22504 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20  tion(pParse, 0, 
22505 69 44 62 29 3b 0a 20 20 76 20 3d 20 73 71 6c 69  iDb);.  v = sqli
22506 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
22507 65 29 3b 0a 20 20 69 66 28 20 21 76 20 29 20 67  e);.  if( !v ) g
22508 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61  oto exit_begin_a
22509 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 73 71 6c  dd_column;.  sql
2250a 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65  ite3ChangeCookie
2250b 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 0a  (pParse, iDb);..
2250c 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63  exit_begin_add_c
2250d 6f 6c 75 6d 6e 3a 0a 20 20 73 71 6c 69 74 65 33  olumn:.  sqlite3
2250e 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62  SrcListDelete(db
2250f 2c 20 70 53 72 63 29 3b 0a 20 20 72 65 74 75 72  , pSrc);.  retur
22510 6e 3b 0a 7d 0a 23 65 6e 64 69 66 20 20 2f 2a 20  n;.}.#endif  /* 
22511 53 51 4c 49 54 45 5f 41 4c 54 45 52 5f 54 41 42  SQLITE_ALTER_TAB
22512 4c 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  LE */../********
22513 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 61 6c  ****** End of al
22514 74 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ter.c **********
22515 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22516 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22517 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
22518 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
22519 65 20 61 6e 61 6c 79 7a 65 2e 63 20 2a 2a 2a 2a  e analyze.c ****
2251a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2251b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2251c 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
2251d 35 20 4a 75 6c 79 20 38 0a 2a 2a 0a 2a 2a 20 54  5 July 8.**.** T
2251e 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
2251f 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
22520 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
22521 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
22522 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
22523 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
22524 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
22525 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
22526 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
22527 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
22528 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
22529 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
2252a 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
2252b 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
2252c 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
2252d 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
2252e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
2252f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22531 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22532 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22533 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
22534 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f  file contains co
22535 64 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  de associated wi
22536 74 68 20 74 68 65 20 41 4e 41 4c 59 5a 45 20 63  th the ANALYZE c
22537 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 40 28  ommand..**.** @(
22538 23 29 20 24 49 64 3a 20 61 6e 61 6c 79 7a 65 2e  #) $Id: analyze.
22539 63 2c 76 20 31 2e 35 32 20 32 30 30 39 2f 30 34  c,v 1.52 2009/04
2253a 2f 31 36 20 31 37 3a 34 35 3a 34 38 20 64 72 68  /16 17:45:48 drh
2253b 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65   Exp $.*/.#ifnde
2253c 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e  f SQLITE_OMIT_AN
2253d 41 4c 59 5a 45 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  ALYZE../*.** Thi
2253e 73 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61  s routine genera
2253f 74 65 73 20 63 6f 64 65 20 74 68 61 74 20 6f 70  tes code that op
22540 65 6e 73 20 74 68 65 20 73 71 6c 69 74 65 5f 73  ens the sqlite_s
22541 74 61 74 31 20 74 61 62 6c 65 20 66 6f 72 0a 2a  tat1 table for.*
22542 2a 20 77 72 69 74 69 6e 67 20 77 69 74 68 20 63  * writing with c
22543 75 72 73 6f 72 20 69 53 74 61 74 43 75 72 2e 20  ursor iStatCur. 
22544 49 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 77  If the library w
22545 61 73 20 62 75 69 6c 74 20 77 69 74 68 20 74 68  as built with th
22546 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42  e.** SQLITE_ENAB
22547 4c 45 5f 53 54 41 54 32 20 6d 61 63 72 6f 20 64  LE_STAT2 macro d
22548 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 74 68 65  efined, then the
22549 20 73 71 6c 69 74 65 5f 73 74 61 74 32 20 74 61   sqlite_stat2 ta
2254a 62 6c 65 20 69 73 0a 2a 2a 20 6f 70 65 6e 65 64  ble is.** opened
2254b 20 66 6f 72 20 77 72 69 74 69 6e 67 20 75 73 69   for writing usi
2254c 6e 67 20 63 75 72 73 6f 72 20 28 69 53 74 61 74  ng cursor (iStat
2254d 43 75 72 2b 31 29 0a 2a 2a 0a 2a 2a 20 49 66 20  Cur+1).**.** If 
2254e 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31  the sqlite_stat1
2254f 20 74 61 62 6c 65 73 20 64 6f 65 73 20 6e 6f 74   tables does not
22550 20 70 72 65 76 69 6f 75 73 6c 79 20 65 78 69 73   previously exis
22551 74 2c 20 69 74 20 69 73 20 63 72 65 61 74 65 64  t, it is created
22552 2e 0a 2a 2a 20 53 69 6d 69 6c 61 72 6c 79 2c 20  ..** Similarly, 
22553 69 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74  if the sqlite_st
22554 61 74 32 20 74 61 62 6c 65 20 64 6f 65 73 20 6e  at2 table does n
22555 6f 74 20 65 78 69 73 74 20 61 6e 64 20 74 68 65  ot exist and the
22556 20 6c 69 62 72 61 72 79 0a 2a 2a 20 69 73 20 63   library.** is c
22557 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 53 51 4c  ompiled with SQL
22558 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32  ITE_ENABLE_STAT2
22559 20 64 65 66 69 6e 65 64 2c 20 69 74 20 69 73 20   defined, it is 
2255a 63 72 65 61 74 65 64 2e 20 0a 2a 2a 0a 2a 2a 20  created. .**.** 
2255b 41 72 67 75 6d 65 6e 74 20 7a 57 68 65 72 65 20  Argument zWhere 
2255c 6d 61 79 20 62 65 20 61 20 70 6f 69 6e 74 65 72  may be a pointer
2255d 20 74 6f 20 61 20 62 75 66 66 65 72 20 63 6f 6e   to a buffer con
2255e 74 61 69 6e 69 6e 67 20 61 20 74 61 62 6c 65 20  taining a table 
2255f 6e 61 6d 65 2c 0a 2a 2a 20 6f 72 20 69 74 20 6d  name,.** or it m
22560 61 79 20 62 65 20 61 20 4e 55 4c 4c 20 70 6f 69  ay be a NULL poi
22561 6e 74 65 72 2e 20 49 66 20 69 74 20 69 73 20 6e  nter. If it is n
22562 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 6c  ot NULL, then al
22563 6c 20 65 6e 74 72 69 65 73 20 69 6e 0a 2a 2a 20  l entries in.** 
22564 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31  the sqlite_stat1
22565 20 61 6e 64 20 28 69 66 20 61 70 70 6c 69 63 61   and (if applica
22566 62 6c 65 29 20 73 71 6c 69 74 65 5f 73 74 61 74  ble) sqlite_stat
22567 32 20 74 61 62 6c 65 73 20 61 73 73 6f 63 69 61  2 tables associa
22568 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20  ted.** with the 
22569 6e 61 6d 65 64 20 74 61 62 6c 65 20 61 72 65 20  named table are 
2256a 64 65 6c 65 74 65 64 2e 20 49 66 20 7a 57 68 65  deleted. If zWhe
2256b 72 65 3d 3d 30 2c 20 74 68 65 6e 20 63 6f 64 65  re==0, then code
2256c 20 69 73 20 67 65 6e 65 72 61 74 65 64 0a 2a 2a   is generated.**
2256d 20 74 6f 20 64 65 6c 65 74 65 20 61 6c 6c 20 73   to delete all s
2256e 74 61 74 20 74 61 62 6c 65 20 65 6e 74 72 69 65  tat table entrie
2256f 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  s..*/.static voi
22570 64 20 6f 70 65 6e 53 74 61 74 54 61 62 6c 65 28  d openStatTable(
22571 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
22572 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61  ,          /* Pa
22573 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
22574 0a 20 20 69 6e 74 20 69 44 62 2c 20 20 20 20 20  .  int iDb,     
22575 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
22576 65 20 64 61 74 61 62 61 73 65 20 77 65 20 61 72  e database we ar
22577 65 20 6c 6f 6f 6b 69 6e 67 20 69 6e 20 2a 2f 0a  e looking in */.
22578 20 20 69 6e 74 20 69 53 74 61 74 43 75 72 2c 20    int iStatCur, 
22579 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65            /* Ope
2257a 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61  n the sqlite_sta
2257b 74 31 20 74 61 62 6c 65 20 6f 6e 20 74 68 69 73  t1 table on this
2257c 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 63 6f 6e   cursor */.  con
2257d 73 74 20 63 68 61 72 20 2a 7a 57 68 65 72 65 20  st char *zWhere 
2257e 20 20 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 65       /* Delete e
2257f 6e 74 72 69 65 73 20 61 73 73 6f 63 69 61 74 65  ntries associate
22580 64 20 77 69 74 68 20 74 68 69 73 20 74 61 62 6c  d with this tabl
22581 65 20 2a 2f 0a 29 7b 0a 20 20 73 74 61 74 69 63  e */.){.  static
22582 20 73 74 72 75 63 74 20 7b 0a 20 20 20 20 63 6f   struct {.    co
22583 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b  nst char *zName;
22584 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
22585 2a 7a 43 6f 6c 73 3b 0a 20 20 7d 20 61 54 61 62  *zCols;.  } aTab
22586 6c 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b 20 22  le[] = {.    { "
22587 73 71 6c 69 74 65 5f 73 74 61 74 31 22 2c 20 22  sqlite_stat1", "
22588 74 62 6c 2c 69 64 78 2c 73 74 61 74 22 20 7d 2c  tbl,idx,stat" },
22589 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
2258a 4e 41 42 4c 45 5f 53 54 41 54 32 0a 20 20 20 20  NABLE_STAT2.    
2258b 7b 20 22 73 71 6c 69 74 65 5f 73 74 61 74 32 22  { "sqlite_stat2"
2258c 2c 20 22 74 62 6c 2c 69 64 78 2c 73 61 6d 70 6c  , "tbl,idx,sampl
2258d 65 6e 6f 2c 73 61 6d 70 6c 65 22 20 7d 2c 0a 23  eno,sample" },.#
2258e 65 6e 64 69 66 0a 20 20 7d 3b 0a 0a 20 20 69 6e  endif.  };..  in
2258f 74 20 61 52 6f 6f 74 5b 5d 20 3d 20 7b 30 2c 20  t aRoot[] = {0, 
22590 30 7d 3b 0a 20 20 75 38 20 61 43 72 65 61 74 65  0};.  u8 aCreate
22591 54 62 6c 5b 5d 20 3d 20 7b 30 2c 20 30 7d 3b 0a  Tbl[] = {0, 0};.
22592 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 71 6c 69  .  int i;.  sqli
22593 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
22594 2d 3e 64 62 3b 0a 20 20 44 62 20 2a 70 44 62 3b  ->db;.  Db *pDb;
22595 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c  .  Vdbe *v = sql
22596 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
22597 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20  se);.  if( v==0 
22598 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65  ) return;.  asse
22599 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  rt( sqlite3Btree
2259a 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28  HoldsAllMutexes(
2259b 64 62 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  db) );.  assert(
2259c 20 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 76   sqlite3VdbeDb(v
2259d 29 3d 3d 64 62 20 29 3b 0a 20 20 70 44 62 20 3d  )==db );.  pDb =
2259e 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a   &db->aDb[iDb];.
2259f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72  .  for(i=0; i<Ar
225a0 72 61 79 53 69 7a 65 28 61 54 61 62 6c 65 29 3b  raySize(aTable);
225a1 20 69 2b 2b 29 7b 0a 20 20 20 20 63 6f 6e 73 74   i++){.    const
225a2 20 63 68 61 72 20 2a 7a 54 61 62 20 3d 20 61 54   char *zTab = aT
225a3 61 62 6c 65 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20  able[i].zName;. 
225a4 20 20 20 54 61 62 6c 65 20 2a 70 53 74 61 74 3b     Table *pStat;
225a5 0a 20 20 20 20 69 66 28 20 28 70 53 74 61 74 20  .    if( (pStat 
225a6 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62  = sqlite3FindTab
225a7 6c 65 28 64 62 2c 20 7a 54 61 62 2c 20 70 44 62  le(db, zTab, pDb
225a8 2d 3e 7a 4e 61 6d 65 29 29 3d 3d 30 20 29 7b 0a  ->zName))==0 ){.
225a9 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 71 6c        /* The sql
225aa 69 74 65 5f 73 74 61 74 5b 31 32 5d 20 74 61 62  ite_stat[12] tab
225ab 6c 65 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73  le does not exis
225ac 74 2e 20 43 72 65 61 74 65 20 69 74 2e 20 4e 6f  t. Create it. No
225ad 74 65 20 74 68 61 74 20 61 20 0a 20 20 20 20 20  te that a .     
225ae 20 2a 2a 20 73 69 64 65 2d 65 66 66 65 63 74 20   ** side-effect 
225af 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 54 41  of the CREATE TA
225b0 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 69 73  BLE statement is
225b1 20 74 6f 20 6c 65 61 76 65 20 74 68 65 20 72 6f   to leave the ro
225b2 6f 74 70 61 67 65 20 0a 20 20 20 20 20 20 2a 2a  otpage .      **
225b3 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c   of the new tabl
225b4 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 70 50  e in register pP
225b5 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74 2e 20 54  arse->regRoot. T
225b6 68 69 73 20 69 73 20 69 6d 70 6f 72 74 61 6e 74  his is important
225b7 20 0a 20 20 20 20 20 20 2a 2a 20 62 65 63 61 75   .      ** becau
225b8 73 65 20 74 68 65 20 4f 70 65 6e 57 72 69 74 65  se the OpenWrite
225b9 20 6f 70 63 6f 64 65 20 62 65 6c 6f 77 20 77 69   opcode below wi
225ba 6c 6c 20 62 65 20 6e 65 65 64 69 6e 67 20 69 74  ll be needing it
225bb 2e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74  . */.      sqlit
225bc 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50  e3NestedParse(pP
225bd 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 20 20  arse,.          
225be 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 25 51  "CREATE TABLE %Q
225bf 2e 25 73 28 25 73 29 22 2c 20 70 44 62 2d 3e 7a  .%s(%s)", pDb->z
225c0 4e 61 6d 65 2c 20 7a 54 61 62 2c 20 61 54 61 62  Name, zTab, aTab
225c1 6c 65 5b 69 5d 2e 7a 43 6f 6c 73 0a 20 20 20 20  le[i].zCols.    
225c2 20 20 29 3b 0a 20 20 20 20 20 20 61 52 6f 6f 74    );.      aRoot
225c3 5b 69 5d 20 3d 20 70 50 61 72 73 65 2d 3e 72 65  [i] = pParse->re
225c4 67 52 6f 6f 74 3b 0a 20 20 20 20 20 20 61 43 72  gRoot;.      aCr
225c5 65 61 74 65 54 62 6c 5b 69 5d 20 3d 20 31 3b 0a  eateTbl[i] = 1;.
225c6 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
225c7 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 61 6c   /* The table al
225c8 72 65 61 64 79 20 65 78 69 73 74 73 2e 20 49 66  ready exists. If
225c9 20 7a 57 68 65 72 65 20 69 73 20 6e 6f 74 20 4e   zWhere is not N
225ca 55 4c 4c 2c 20 64 65 6c 65 74 65 20 61 6c 6c 20  ULL, delete all 
225cb 65 6e 74 72 69 65 73 20 0a 20 20 20 20 20 20 2a  entries .      *
225cc 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  * associated wit
225cd 68 20 74 68 65 20 74 61 62 6c 65 20 7a 57 68 65  h the table zWhe
225ce 72 65 2e 20 49 66 20 7a 57 68 65 72 65 20 69 73  re. If zWhere is
225cf 20 4e 55 4c 4c 2c 20 64 65 6c 65 74 65 20 74 68   NULL, delete th
225d0 65 0a 20 20 20 20 20 20 2a 2a 20 65 6e 74 69 72  e.      ** entir
225d1 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  e contents of th
225d2 65 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20  e table. */.    
225d3 20 20 61 52 6f 6f 74 5b 69 5d 20 3d 20 70 53 74    aRoot[i] = pSt
225d4 61 74 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20 20 20  at->tnum;.      
225d5 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b  sqlite3TableLock
225d6 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 61 52  (pParse, iDb, aR
225d7 6f 6f 74 5b 69 5d 2c 20 31 2c 20 7a 54 61 62 29  oot[i], 1, zTab)
225d8 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 57 68 65  ;.      if( zWhe
225d9 72 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  re ){.        sq
225da 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
225db 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20  (pParse,.       
225dc 20 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d      "DELETE FROM
225dd 20 25 51 2e 25 73 20 57 48 45 52 45 20 74 62 6c   %Q.%s WHERE tbl
225de 3d 25 51 22 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65  =%Q", pDb->zName
225df 2c 20 7a 54 61 62 2c 20 7a 57 68 65 72 65 0a 20  , zTab, zWhere. 
225e0 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20         );.      
225e1 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f  }else{.        /
225e2 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 73 74 61  * The sqlite_sta
225e3 74 5b 31 32 5d 20 74 61 62 6c 65 20 61 6c 72 65  t[12] table alre
225e4 61 64 79 20 65 78 69 73 74 73 2e 20 20 44 65 6c  ady exists.  Del
225e5 65 74 65 20 61 6c 6c 20 72 6f 77 73 2e 20 2a 2f  ete all rows. */
225e6 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
225e7 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
225e8 5f 43 6c 65 61 72 2c 20 61 52 6f 6f 74 5b 69 5d  _Clear, aRoot[i]
225e9 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 7d 0a  , iDb);.      }.
225ea 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
225eb 4f 70 65 6e 20 74 68 65 20 73 71 6c 69 74 65 5f  Open the sqlite_
225ec 73 74 61 74 5b 31 32 5d 20 74 61 62 6c 65 73 20  stat[12] tables 
225ed 66 6f 72 20 77 72 69 74 69 6e 67 2e 20 2a 2f 0a  for writing. */.
225ee 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72    for(i=0; i<Arr
225ef 61 79 53 69 7a 65 28 61 54 61 62 6c 65 29 3b 20  aySize(aTable); 
225f0 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  i++){.    sqlite
225f1 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
225f2 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69 53 74  P_OpenWrite, iSt
225f3 61 74 43 75 72 2b 69 2c 20 61 52 6f 6f 74 5b 69  atCur+i, aRoot[i
225f4 5d 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c  ], iDb);.    sql
225f5 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34  ite3VdbeChangeP4
225f6 28 76 2c 20 2d 31 2c 20 28 63 68 61 72 20 2a 29  (v, -1, (char *)
225f7 33 2c 20 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20  3, P4_INT32);.  
225f8 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
225f9 6e 67 65 50 35 28 76 2c 20 61 43 72 65 61 74 65  ngeP5(v, aCreate
225fa 54 62 6c 5b 69 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a  Tbl[i]);.  }.}..
225fb 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
225fc 6f 64 65 20 74 6f 20 64 6f 20 61 6e 20 61 6e 61  ode to do an ana
225fd 6c 79 73 69 73 20 6f 66 20 61 6c 6c 20 69 6e 64  lysis of all ind
225fe 69 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20  ices associated 
225ff 77 69 74 68 0a 2a 2a 20 61 20 73 69 6e 67 6c 65  with.** a single
22600 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69   table..*/.stati
22601 63 20 76 6f 69 64 20 61 6e 61 6c 79 7a 65 4f 6e  c void analyzeOn
22602 65 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20  eTable(.  Parse 
22603 2a 70 50 61 72 73 65 2c 20 20 20 2f 2a 20 50 61  *pParse,   /* Pa
22604 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  rser context */.
22605 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20    Table *pTab,  
22606 20 20 20 2f 2a 20 54 61 62 6c 65 20 77 68 6f 73     /* Table whos
22607 65 20 69 6e 64 69 63 65 73 20 61 72 65 20 74 6f  e indices are to
22608 20 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a   be analyzed */.
22609 20 20 69 6e 74 20 69 53 74 61 74 43 75 72 2c 20    int iStatCur, 
2260a 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 56     /* Index of V
2260b 64 62 65 43 75 72 73 6f 72 20 74 68 61 74 20 77  dbeCursor that w
2260c 72 69 74 65 73 20 74 68 65 20 73 71 6c 69 74 65  rites the sqlite
2260d 5f 73 74 61 74 31 20 74 61 62 6c 65 20 2a 2f 0a  _stat1 table */.
2260e 20 20 69 6e 74 20 69 4d 65 6d 20 20 20 20 20 20    int iMem      
2260f 20 20 20 2f 2a 20 41 76 61 69 6c 61 62 6c 65 20     /* Available 
22610 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73  memory locations
22611 20 62 65 67 69 6e 20 68 65 72 65 20 2a 2f 0a 29   begin here */.)
22612 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  {.  sqlite3 *db 
22613 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20  = pParse->db;   
22614 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e   /* Database han
22615 64 6c 65 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a  dle */.  Index *
22616 70 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 20  pIdx;           
22617 20 20 20 20 20 20 2f 2a 20 41 6e 20 69 6e 64 65        /* An inde
22618 78 20 74 6f 20 62 65 69 6e 67 20 61 6e 61 6c 79  x to being analy
22619 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 49 64  zed */.  int iId
2261a 78 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 20  xCur;           
2261b 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20        /* Cursor 
2261c 6f 70 65 6e 20 6f 6e 20 69 6e 64 65 78 20 62 65  open on index be
2261d 69 6e 67 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a  ing analyzed */.
2261e 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20    Vdbe *v;      
2261f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
22620 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 6d 61  * The virtual ma
22621 63 68 69 6e 65 20 62 65 69 6e 67 20 62 75 69 6c  chine being buil
22622 74 20 75 70 20 2a 2f 0a 20 20 69 6e 74 20 69 3b  t up */.  int i;
22623 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22624 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
22625 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  ounter */.  int 
22626 74 6f 70 4f 66 4c 6f 6f 70 3b 20 20 20 20 20 20  topOfLoop;      
22627 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
22628 74 6f 70 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20  top of the loop 
22629 2a 2f 0a 20 20 69 6e 74 20 65 6e 64 4f 66 4c 6f  */.  int endOfLo
2262a 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  op;             
2262b 20 20 2f 2a 20 54 68 65 20 65 6e 64 20 6f 66 20    /* The end of 
2262c 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e  the loop */.  in
2262d 74 20 61 64 64 72 3b 20 20 20 20 20 20 20 20 20  t addr;         
2262e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2262f 65 20 61 64 64 72 65 73 73 20 6f 66 20 61 6e 20  e address of an 
22630 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20  instruction */. 
22631 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20   int iDb;       
22632 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
22633 20 49 6e 64 65 78 20 6f 66 20 64 61 74 61 62 61   Index of databa
22634 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 54  se containing pT
22635 61 62 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 54  ab */.  int regT
22636 61 62 6e 61 6d 65 20 3d 20 69 4d 65 6d 2b 2b 3b  abname = iMem++;
22637 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72       /* Register
22638 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c   containing tabl
22639 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20  e name */.  int 
2263a 72 65 67 49 64 78 6e 61 6d 65 20 3d 20 69 4d 65  regIdxname = iMe
2263b 6d 2b 2b 3b 20 20 20 20 20 2f 2a 20 52 65 67 69  m++;     /* Regi
2263c 73 74 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ster containing 
2263d 69 6e 64 65 78 20 6e 61 6d 65 20 2a 2f 0a 20 20  index name */.  
2263e 69 6e 74 20 72 65 67 53 61 6d 70 6c 65 6e 6f 20  int regSampleno 
2263f 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20 2f 2a 20  = iMem++;    /* 
22640 52 65 67 69 73 74 65 72 20 63 6f 6e 74 61 69 6e  Register contain
22641 69 6e 67 20 6e 65 78 74 20 73 61 6d 70 6c 65 20  ing next sample 
22642 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69 6e 74 20  number */.  int 
22643 72 65 67 43 6f 6c 20 3d 20 69 4d 65 6d 2b 2b 3b  regCol = iMem++;
22644 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74           /* Cont
22645 65 6e 74 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20  ent of a column 
22646 61 6e 61 6c 79 7a 65 64 20 74 61 62 6c 65 20 2a  analyzed table *
22647 2f 0a 20 20 69 6e 74 20 72 65 67 52 65 63 20 3d  /.  int regRec =
22648 20 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20 20   iMem++;        
22649 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c   /* Register hol
2264a 64 69 6e 67 20 63 6f 6d 70 6c 65 74 65 64 20 72  ding completed r
2264b 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 72  ecord */.  int r
2264c 65 67 54 65 6d 70 20 3d 20 69 4d 65 6d 2b 2b 3b  egTemp = iMem++;
2264d 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f          /* Tempo
2264e 72 61 72 79 20 75 73 65 20 72 65 67 69 73 74 65  rary use registe
2264f 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f  r */.  int regRo
22650 77 69 64 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20  wid = iMem++;   
22651 20 20 20 20 2f 2a 20 52 6f 77 69 64 20 66 6f 72      /* Rowid for
22652 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 72 65   the inserted re
22653 63 6f 72 64 20 2a 2f 0a 0a 23 69 66 64 65 66 20  cord */..#ifdef 
22654 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54  SQLITE_ENABLE_ST
22655 41 54 32 0a 20 20 69 6e 74 20 72 65 67 54 65 6d  AT2.  int regTem
22656 70 32 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20  p2 = iMem++;    
22657 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20     /* Temporary 
22658 75 73 65 20 72 65 67 69 73 74 65 72 20 2a 2f 0a  use register */.
22659 20 20 69 6e 74 20 72 65 67 53 61 6d 70 6c 65 72    int regSampler
2265a 65 63 6e 6f 20 3d 20 69 4d 65 6d 2b 2b 3b 20 2f  ecno = iMem++; /
2265b 2a 20 49 6e 64 65 78 20 6f 66 20 6e 65 78 74 20  * Index of next 
2265c 73 61 6d 70 6c 65 20 74 6f 20 72 65 63 6f 72 64  sample to record
2265d 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 65 63   */.  int regRec
2265e 6e 6f 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20  no = iMem++;    
2265f 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 73 61     /* Current sa
22660 6d 70 6c 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20  mple index */.  
22661 69 6e 74 20 72 65 67 4c 61 73 74 20 3d 20 69 4d  int regLast = iM
22662 65 6d 2b 2b 3b 20 20 20 20 20 20 20 20 2f 2a 20  em++;        /* 
22663 49 6e 64 65 78 20 6f 66 20 6c 61 73 74 20 73 61  Index of last sa
22664 6d 70 6c 65 20 74 6f 20 72 65 63 6f 72 64 20 2a  mple to record *
22665 2f 0a 20 20 69 6e 74 20 72 65 67 46 69 72 73 74  /.  int regFirst
22666 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 20   = iMem++;      
22667 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 66 69 72   /* Index of fir
22668 73 74 20 73 61 6d 70 6c 65 20 74 6f 20 72 65 63  st sample to rec
22669 6f 72 64 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20  ord */.#endif.. 
2266a 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   v = sqlite3GetV
2266b 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69  dbe(pParse);.  i
2266c 66 28 20 76 3d 3d 30 20 7c 7c 20 4e 45 56 45 52  f( v==0 || NEVER
2266d 28 70 54 61 62 3d 3d 30 29 20 7c 7c 20 70 54 61  (pTab==0) || pTa
2266e 62 2d 3e 70 49 6e 64 65 78 3d 3d 30 20 29 7b 0a  b->pIndex==0 ){.
2266f 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 20 61 6e 61      /* Do no ana
22670 6c 79 73 69 73 20 66 6f 72 20 74 61 62 6c 65 73  lysis for tables
22671 20 74 68 61 74 20 68 61 76 65 20 6e 6f 20 69 6e   that have no in
22672 64 69 63 65 73 20 2a 2f 0a 20 20 20 20 72 65 74  dices */.    ret
22673 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  urn;.  }.  asser
22674 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  t( sqlite3BtreeH
22675 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 64  oldsAllMutexes(d
22676 62 29 20 29 3b 0a 20 20 69 44 62 20 3d 20 73 71  b) );.  iDb = sq
22677 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64  lite3SchemaToInd
22678 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63  ex(db, pTab->pSc
22679 68 65 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 28  hema);.  assert(
2267a 20 69 44 62 3e 3d 30 20 29 3b 0a 23 69 66 6e 64   iDb>=0 );.#ifnd
2267b 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
2267c 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 69  UTHORIZATION.  i
2267d 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  f( sqlite3AuthCh
2267e 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49  eck(pParse, SQLI
2267f 54 45 5f 41 4e 41 4c 59 5a 45 2c 20 70 54 61 62  TE_ANALYZE, pTab
22680 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 0a 20 20 20 20  ->zName, 0,.    
22681 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a    db->aDb[iDb].z
22682 4e 61 6d 65 20 29 20 29 7b 0a 20 20 20 20 72 65  Name ) ){.    re
22683 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  turn;.  }.#endif
22684 0a 0a 20 20 2f 2a 20 45 73 74 61 62 6c 69 73 68  ..  /* Establish
22685 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20   a read-lock on 
22686 74 68 65 20 74 61 62 6c 65 20 61 74 20 74 68 65  the table at the
22687 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6c 65   shared-cache le
22688 76 65 6c 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65  vel. */.  sqlite
22689 33 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73  3TableLock(pPars
2268a 65 2c 20 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e  e, iDb, pTab->tn
2268b 75 6d 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61  um, 0, pTab->zNa
2268c 6d 65 29 3b 0a 0a 20 20 69 49 64 78 43 75 72 20  me);..  iIdxCur 
2268d 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b  = pParse->nTab++
2268e 3b 0a 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61  ;.  for(pIdx=pTa
2268f 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b  b->pIndex; pIdx;
22690 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
22691 74 29 7b 0a 20 20 20 20 69 6e 74 20 6e 43 6f 6c  t){.    int nCol
22692 20 3d 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e   = pIdx->nColumn
22693 3b 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70  ;.    KeyInfo *p
22694 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64  Key = sqlite3Ind
22695 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65  exKeyinfo(pParse
22696 2c 20 70 49 64 78 29 3b 0a 0a 20 20 20 20 69 66  , pIdx);..    if
22697 28 20 69 4d 65 6d 2b 31 2b 28 6e 43 6f 6c 2a 32  ( iMem+1+(nCol*2
22698 29 3e 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 29  )>pParse->nMem )
22699 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  {.      pParse->
2269a 6e 4d 65 6d 20 3d 20 69 4d 65 6d 2b 31 2b 28 6e  nMem = iMem+1+(n
2269b 43 6f 6c 2a 32 29 3b 0a 20 20 20 20 7d 0a 0a 20  Col*2);.    }.. 
2269c 20 20 20 2f 2a 20 4f 70 65 6e 20 61 20 63 75 72     /* Open a cur
2269d 73 6f 72 20 74 6f 20 74 68 65 20 69 6e 64 65 78  sor to the index
2269e 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 2e   to be analyzed.
2269f 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
226a0 69 44 62 3d 3d 73 71 6c 69 74 65 33 53 63 68 65  iDb==sqlite3Sche
226a1 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 49  maToIndex(db, pI
226a2 64 78 2d 3e 70 53 63 68 65 6d 61 29 20 29 3b 0a  dx->pSchema) );.
226a3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
226a4 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp4(v, OP_Open
226a5 52 65 61 64 2c 20 69 49 64 78 43 75 72 2c 20 70  Read, iIdxCur, p
226a6 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a  Idx->tnum, iDb,.
226a7 20 20 20 20 20 20 20 20 28 63 68 61 72 20 2a 29          (char *)
226a8 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f  pKey, P4_KEYINFO
226a9 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 56  _HANDOFF);.    V
226aa 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
226ab 25 73 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65  %s", pIdx->zName
226ac 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 50 6f 70 75  ));..    /* Popu
226ad 6c 61 74 65 20 74 68 65 20 72 65 67 69 73 74 65  late the registe
226ae 72 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  rs containing th
226af 65 20 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 65  e table and inde
226b0 78 20 6e 61 6d 65 73 2e 20 2a 2f 0a 20 20 20 20  x names. */.    
226b1 69 66 28 20 70 54 61 62 2d 3e 70 49 6e 64 65 78  if( pTab->pIndex
226b2 3d 3d 70 49 64 78 20 29 7b 0a 20 20 20 20 20 20  ==pIdx ){.      
226b3 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
226b4 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  4(v, OP_String8,
226b5 20 30 2c 20 72 65 67 54 61 62 6e 61 6d 65 2c 20   0, regTabname, 
226b6 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20  0, pTab->zName, 
226b7 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  0);.    }.    sq
226b8 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
226b9 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
226ba 2c 20 72 65 67 49 64 78 6e 61 6d 65 2c 20 30 2c  , regIdxname, 0,
226bb 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 30 29   pIdx->zName, 0)
226bc 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ;..#ifdef SQLITE
226bd 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 0a 0a 20  _ENABLE_STAT2.. 
226be 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 74     /* If this it
226bf 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c  eration of the l
226c0 6f 6f 70 20 69 73 20 67 65 6e 65 72 61 74 69 6e  oop is generatin
226c1 67 20 63 6f 64 65 20 74 6f 20 61 6e 61 6c 79 7a  g code to analyz
226c2 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 69 72  e the.    ** fir
226c3 73 74 20 69 6e 64 65 78 20 69 6e 20 74 68 65 20  st index in the 
226c4 70 54 61 62 2d 3e 70 49 6e 64 65 78 20 6c 69 73  pTab->pIndex lis
226c5 74 2c 20 74 68 65 6e 20 72 65 67 69 73 74 65 72  t, then register
226c6 20 72 65 67 4c 61 73 74 20 68 61 73 0a 20 20 20   regLast has.   
226c7 20 2a 2a 20 6e 6f 74 20 62 65 65 6e 20 70 6f 70   ** not been pop
226c8 75 6c 61 74 65 64 2e 20 49 6e 20 74 68 69 73 20  ulated. In this 
226c9 63 61 73 65 20 70 6f 70 75 6c 61 74 65 20 69 74  case populate it
226ca 20 6e 6f 77 2e 20 20 2a 2f 0a 20 20 20 20 69 66   now.  */.    if
226cb 28 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 3d 3d  ( pTab->pIndex==
226cc 70 49 64 78 20 29 7b 0a 20 20 20 20 20 20 73 71  pIdx ){.      sq
226cd 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
226ce 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 53  v, OP_Integer, S
226cf 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50  QLITE_INDEX_SAMP
226d0 4c 45 53 2c 20 72 65 67 53 61 6d 70 6c 65 72 65  LES, regSamplere
226d1 63 6e 6f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  cno);.      sqli
226d2 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
226d3 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 53 51 4c   OP_Integer, SQL
226d4 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45  ITE_INDEX_SAMPLE
226d5 53 2a 32 2d 31 2c 20 72 65 67 54 65 6d 70 29 3b  S*2-1, regTemp);
226d6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
226d7 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
226d8 6e 74 65 67 65 72 2c 20 53 51 4c 49 54 45 5f 49  nteger, SQLITE_I
226d9 4e 44 45 58 5f 53 41 4d 50 4c 45 53 2a 32 2c 20  NDEX_SAMPLES*2, 
226da 72 65 67 54 65 6d 70 32 29 3b 0a 0a 20 20 20 20  regTemp2);..    
226db 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
226dc 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 75 6e 74 2c  Op2(v, OP_Count,
226dd 20 69 49 64 78 43 75 72 2c 20 72 65 67 4c 61 73   iIdxCur, regLas
226de 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  t);.      sqlite
226df 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
226e0 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 46 69  P_Null, 0, regFi
226e1 72 73 74 29 3b 0a 20 20 20 20 20 20 61 64 64 72  rst);.      addr
226e2 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
226e3 64 4f 70 33 28 76 2c 20 4f 50 5f 4c 74 2c 20 72  dOp3(v, OP_Lt, r
226e4 65 67 53 61 6d 70 6c 65 72 65 63 6e 6f 2c 20 30  egSamplerecno, 0
226e5 2c 20 72 65 67 4c 61 73 74 29 3b 0a 20 20 20 20  , regLast);.    
226e6 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
226e7 4f 70 33 28 76 2c 20 4f 50 5f 44 69 76 69 64 65  Op3(v, OP_Divide
226e8 2c 20 72 65 67 54 65 6d 70 32 2c 20 72 65 67 4c  , regTemp2, regL
226e9 61 73 74 2c 20 72 65 67 46 69 72 73 74 29 3b 0a  ast, regFirst);.
226ea 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
226eb 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 75  eAddOp3(v, OP_Mu
226ec 6c 74 69 70 6c 79 2c 20 72 65 67 4c 61 73 74 2c  ltiply, regLast,
226ed 20 72 65 67 54 65 6d 70 2c 20 72 65 67 4c 61 73   regTemp, regLas
226ee 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  t);.      sqlite
226ef 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
226f0 50 5f 41 64 64 49 6d 6d 2c 20 72 65 67 4c 61 73  P_AddImm, regLas
226f1 74 2c 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f  t, SQLITE_INDEX_
226f2 53 41 4d 50 4c 45 53 2a 32 2d 32 29 3b 0a 20 20  SAMPLES*2-2);.  
226f3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
226f4 64 64 4f 70 33 28 76 2c 20 4f 50 5f 44 69 76 69  ddOp3(v, OP_Divi
226f5 64 65 2c 20 20 72 65 67 54 65 6d 70 32 2c 20 72  de,  regTemp2, r
226f6 65 67 4c 61 73 74 2c 20 72 65 67 4c 61 73 74 29  egLast, regLast)
226f7 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
226f8 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
226f9 64 64 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  ddr);.    }..   
226fa 20 2f 2a 20 5a 65 72 6f 20 74 68 65 20 72 65 67   /* Zero the reg
226fb 53 61 6d 70 6c 65 6e 6f 20 61 6e 64 20 72 65 67  Sampleno and reg
226fc 52 65 63 6e 6f 20 72 65 67 69 73 74 65 72 73 2e  Recno registers.
226fd 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56   */.    sqlite3V
226fe 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
226ff 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67 53  Integer, 0, regS
22700 61 6d 70 6c 65 6e 6f 29 3b 0a 20 20 20 20 73 71  ampleno);.    sq
22701 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
22702 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30  v, OP_Integer, 0
22703 2c 20 72 65 67 52 65 63 6e 6f 29 3b 0a 20 20 20  , regRecno);.   
22704 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22705 70 32 28 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 72  p2(v, OP_Copy, r
22706 65 67 46 69 72 73 74 2c 20 72 65 67 53 61 6d 70  egFirst, regSamp
22707 6c 65 72 65 63 6e 6f 29 3b 0a 23 65 6e 64 69 66  lerecno);.#endif
22708 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 62 6c 6f  ..    /* The blo
22709 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c  ck of memory cel
2270a 6c 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 68  ls initialized h
2270b 65 72 65 20 69 73 20 75 73 65 64 20 61 73 20 66  ere is used as f
2270c 6f 6c 6c 6f 77 73 2e 0a 20 20 20 20 2a 2a 0a 20  ollows..    **. 
2270d 20 20 20 2a 2a 20 20 20 20 69 4d 65 6d 3a 20 20     **    iMem:  
2270e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0a 20                . 
2270f 20 20 20 2a 2a 20 20 20 20 20 20 20 20 54 68 65     **        The
22710 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   total number of
22711 20 72 6f 77 73 20 69 6e 20 74 68 65 20 74 61 62   rows in the tab
22712 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  le..    **.    *
22713 2a 20 20 20 20 69 4d 65 6d 2b 31 20 2e 2e 20 69  *    iMem+1 .. i
22714 4d 65 6d 2b 6e 43 6f 6c 3a 20 0a 20 20 20 20 2a  Mem+nCol: .    *
22715 2a 20 20 20 20 20 20 20 20 4e 75 6d 62 65 72 20  *        Number 
22716 6f 66 20 64 69 73 74 69 6e 63 74 20 65 6e 74 72  of distinct entr
22717 69 65 73 20 69 6e 20 69 6e 64 65 78 20 63 6f 6e  ies in index con
22718 73 69 64 65 72 69 6e 67 20 74 68 65 20 0a 20 20  sidering the .  
22719 20 20 2a 2a 20 20 20 20 20 20 20 20 6c 65 66 74    **        left
2271a 2d 6d 6f 73 74 20 4e 20 63 6f 6c 75 6d 6e 73 20  -most N columns 
2271b 6f 6e 6c 79 2c 20 77 68 65 72 65 20 4e 20 69 73  only, where N is
2271c 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 6e   between 1 and n
2271d 43 6f 6c 2c 20 0a 20 20 20 20 2a 2a 20 20 20 20  Col, .    **    
2271e 20 20 20 20 69 6e 63 6c 75 73 69 76 65 2e 0a 20      inclusive.. 
2271f 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20     **.    **    
22720 69 4d 65 6d 2b 6e 43 6f 6c 2b 31 20 2e 2e 20 4d  iMem+nCol+1 .. M
22721 65 6d 2b 32 2a 6e 43 6f 6c 3a 20 20 0a 20 20 20  em+2*nCol:  .   
22722 20 2a 2a 20 20 20 20 20 20 20 20 50 72 65 76 69   **        Previ
22723 6f 75 73 20 76 61 6c 75 65 20 6f 66 20 69 6e 64  ous value of ind
22724 65 78 65 64 20 63 6f 6c 75 6d 6e 73 2c 20 66 72  exed columns, fr
22725 6f 6d 20 6c 65 66 74 20 74 6f 20 72 69 67 68 74  om left to right
22726 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
22727 43 65 6c 6c 73 20 69 4d 65 6d 20 74 68 72 6f 75  Cells iMem throu
22728 67 68 20 69 4d 65 6d 2b 6e 43 6f 6c 20 61 72 65  gh iMem+nCol are
22729 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20   initialized to 
2272a 30 2e 20 54 68 65 20 6f 74 68 65 72 73 20 61 72  0. The others ar
2272b 65 20 0a 20 20 20 20 2a 2a 20 69 6e 69 74 69 61  e .    ** initia
2272c 6c 69 7a 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e  lized to contain
2272d 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e 0a 20 20   an SQL NULL..  
2272e 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30    */.    for(i=0
2272f 3b 20 69 3c 3d 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  ; i<=nCol; i++){
22730 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
22731 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
22732 6e 74 65 67 65 72 2c 20 30 2c 20 69 4d 65 6d 2b  nteger, 0, iMem+
22733 69 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f  i);.    }.    fo
22734 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69  r(i=0; i<nCol; i
22735 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ++){.      sqlit
22736 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
22737 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 69 4d 65 6d  OP_Null, 0, iMem
22738 2b 6e 43 6f 6c 2b 69 2b 31 29 3b 0a 20 20 20 20  +nCol+i+1);.    
22739 7d 0a 0a 20 20 20 20 2f 2a 20 53 74 61 72 74 20  }..    /* Start 
2273a 74 68 65 20 61 6e 61 6c 79 73 69 73 20 6c 6f 6f  the analysis loo
2273b 70 2e 20 54 68 69 73 20 6c 6f 6f 70 20 72 75 6e  p. This loop run
2273c 73 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 74 68  s through all th
2273d 65 20 65 6e 74 72 69 65 73 20 69 6e 0a 20 20 20  e entries in.   
2273e 20 2a 2a 20 74 68 65 20 69 6e 64 65 78 20 62 2d   ** the index b-
2273f 74 72 65 65 2e 20 20 2a 2f 0a 20 20 20 20 65 6e  tree.  */.    en
22740 64 4f 66 4c 6f 6f 70 20 3d 20 73 71 6c 69 74 65  dOfLoop = sqlite
22741 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76  3VdbeMakeLabel(v
22742 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
22743 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
22744 65 77 69 6e 64 2c 20 69 49 64 78 43 75 72 2c 20  ewind, iIdxCur, 
22745 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 20  endOfLoop);.    
22746 74 6f 70 4f 66 4c 6f 6f 70 20 3d 20 73 71 6c 69  topOfLoop = sqli
22747 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
22748 64 72 28 76 29 3b 0a 20 20 20 20 73 71 6c 69 74  dr(v);.    sqlit
22749 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2274a 4f 50 5f 41 64 64 49 6d 6d 2c 20 69 4d 65 6d 2c  OP_AddImm, iMem,
2274b 20 31 29 3b 0a 0a 20 20 20 20 66 6f 72 28 69 3d   1);..    for(i=
2274c 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  0; i<nCol; i++){
2274d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2274e 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43  beAddOp3(v, OP_C
2274f 6f 6c 75 6d 6e 2c 20 69 49 64 78 43 75 72 2c 20  olumn, iIdxCur, 
22750 69 2c 20 72 65 67 43 6f 6c 29 3b 0a 23 69 66 64  i, regCol);.#ifd
22751 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
22752 5f 53 54 41 54 32 0a 20 20 20 20 20 20 69 66 28  _STAT2.      if(
22753 20 69 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20   i==0 ){.       
22754 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 74 68 65   /* Check if the
22755 20 72 65 63 6f 72 64 20 74 68 61 74 20 63 75 72   record that cur
22756 73 6f 72 20 69 49 64 78 43 75 72 20 70 6f 69 6e  sor iIdxCur poin
22757 74 73 20 74 6f 20 63 6f 6e 74 61 69 6e 73 20 61  ts to contains a
22758 0a 20 20 20 20 20 20 20 20 2a 2a 20 76 61 6c 75  .        ** valu
22759 65 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65  e that should be
2275a 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 73   stored in the s
2275b 71 6c 69 74 65 5f 73 74 61 74 32 20 74 61 62 6c  qlite_stat2 tabl
2275c 65 2e 20 49 66 20 73 6f 2c 0a 20 20 20 20 20 20  e. If so,.      
2275d 20 20 2a 2a 20 73 74 6f 72 65 20 69 74 2e 20 20    ** store it.  
2275e 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e  */.        int n
2275f 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  e = sqlite3VdbeA
22760 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 65 2c 20  ddOp3(v, OP_Ne, 
22761 72 65 67 52 65 63 6e 6f 2c 20 30 2c 20 72 65 67  regRecno, 0, reg
22762 53 61 6d 70 6c 65 72 65 63 6e 6f 29 3b 0a 20 20  Samplerecno);.  
22763 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 65        assert( re
22764 67 54 61 62 6e 61 6d 65 2b 31 3d 3d 72 65 67 49  gTabname+1==regI
22765 64 78 6e 61 6d 65 20 0a 20 20 20 20 20 20 20 20  dxname .        
22766 20 20 20 20 20 26 26 20 72 65 67 54 61 62 6e 61       && regTabna
22767 6d 65 2b 32 3d 3d 72 65 67 53 61 6d 70 6c 65 6e  me+2==regSamplen
22768 6f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 26  o.             &
22769 26 20 72 65 67 54 61 62 6e 61 6d 65 2b 33 3d 3d  & regTabname+3==
2276a 72 65 67 43 6f 6c 0a 20 20 20 20 20 20 20 20 29  regCol.        )
2276b 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2276c 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
2276d 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55   SQLITE_JUMPIFNU
2276e 4c 4c 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  LL);.        sql
2276f 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
22770 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c  , OP_MakeRecord,
22771 20 72 65 67 54 61 62 6e 61 6d 65 2c 20 34 2c 20   regTabname, 4, 
22772 72 65 67 52 65 63 2c 20 22 61 61 61 62 22 2c 20  regRec, "aaab", 
22773 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  0);.        sqli
22774 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
22775 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 69 53   OP_NewRowid, iS
22776 74 61 74 43 75 72 2b 31 2c 20 72 65 67 52 6f 77  tatCur+1, regRow
22777 69 64 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  id);.        sql
22778 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
22779 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 69 53 74  , OP_Insert, iSt
2277a 61 74 43 75 72 2b 31 2c 20 72 65 67 52 65 63 2c  atCur+1, regRec,
2277b 20 72 65 67 52 6f 77 69 64 29 3b 0a 0a 20 20 20   regRowid);..   
2277c 20 20 20 20 20 2f 2a 20 43 61 6c 63 75 6c 61 74       /* Calculat
2277d 65 20 6e 65 77 20 76 61 6c 75 65 73 20 66 6f 72  e new values for
2277e 20 72 65 67 53 61 6d 70 6c 65 72 65 63 6e 6f 20   regSamplerecno 
2277f 61 6e 64 20 72 65 67 53 61 6d 70 6c 65 6e 6f 2e  and regSampleno.
22780 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
22781 20 20 20 20 2a 2a 20 20 20 73 61 6d 70 6c 65 6e      **   samplen
22782 6f 20 3d 20 73 61 6d 70 6c 65 6e 6f 20 2b 20 31  o = sampleno + 1
22783 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 73 61  .        **   sa
22784 6d 70 6c 65 72 65 63 6e 6f 20 3d 20 73 61 6d 70  mplerecno = samp
22785 6c 65 72 65 63 6e 6f 2b 28 72 65 6d 61 69 6e 69  lerecno+(remaini
22786 6e 67 20 72 65 63 6f 72 64 73 29 2f 28 72 65 6d  ng records)/(rem
22787 61 69 6e 69 6e 67 20 73 61 6d 70 6c 65 73 29 0a  aining samples).
22788 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
22789 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2278a 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d  dOp2(v, OP_AddIm
2278b 6d 2c 20 72 65 67 53 61 6d 70 6c 65 6e 6f 2c 20  m, regSampleno, 
2278c 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  1);.        sqli
2278d 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
2278e 20 4f 50 5f 53 75 62 74 72 61 63 74 2c 20 72 65   OP_Subtract, re
2278f 67 52 65 63 6e 6f 2c 20 72 65 67 4c 61 73 74 2c  gRecno, regLast,
22790 20 72 65 67 54 65 6d 70 29 3b 0a 20 20 20 20 20   regTemp);.     
22791 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22792 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d  dOp2(v, OP_AddIm
22793 6d 2c 20 72 65 67 54 65 6d 70 2c 20 2d 31 29 3b  m, regTemp, -1);
22794 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
22795 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
22796 5f 49 6e 74 65 67 65 72 2c 20 53 51 4c 49 54 45  _Integer, SQLITE
22797 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 2c 20  _INDEX_SAMPLES, 
22798 72 65 67 54 65 6d 70 32 29 3b 0a 20 20 20 20 20  regTemp2);.     
22799 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2279a 64 4f 70 33 28 76 2c 20 4f 50 5f 53 75 62 74 72  dOp3(v, OP_Subtr
2279b 61 63 74 2c 20 72 65 67 53 61 6d 70 6c 65 6e 6f  act, regSampleno
2279c 2c 20 72 65 67 54 65 6d 70 32 2c 20 72 65 67 54  , regTemp2, regT
2279d 65 6d 70 32 29 3b 0a 20 20 20 20 20 20 20 20 73  emp2);.        s
2279e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2279f 28 76 2c 20 4f 50 5f 44 69 76 69 64 65 2c 20 72  (v, OP_Divide, r
227a0 65 67 54 65 6d 70 32 2c 20 72 65 67 54 65 6d 70  egTemp2, regTemp
227a1 2c 20 72 65 67 54 65 6d 70 29 3b 0a 20 20 20 20  , regTemp);.    
227a2 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
227a3 64 64 4f 70 33 28 76 2c 20 4f 50 5f 41 64 64 2c  ddOp3(v, OP_Add,
227a4 20 72 65 67 53 61 6d 70 6c 65 72 65 63 6e 6f 2c   regSamplerecno,
227a5 20 72 65 67 54 65 6d 70 2c 20 72 65 67 53 61 6d   regTemp, regSam
227a6 70 6c 65 72 65 63 6e 6f 29 3b 0a 0a 20 20 20 20  plerecno);..    
227a7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
227a8 75 6d 70 48 65 72 65 28 76 2c 20 6e 65 29 3b 0a  umpHere(v, ne);.
227a9 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
227aa 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
227ab 41 64 64 49 6d 6d 2c 20 72 65 67 52 65 63 6e 6f  AddImm, regRecno
227ac 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65  , 1);.      }.#e
227ad 6e 64 69 66 0a 0a 20 20 20 20 20 20 73 71 6c 69  ndif..      sqli
227ae 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
227af 20 4f 50 5f 4e 65 2c 20 72 65 67 43 6f 6c 2c 20   OP_Ne, regCol, 
227b0 30 2c 20 69 4d 65 6d 2b 6e 43 6f 6c 2b 69 2b 31  0, iMem+nCol+i+1
227b1 29 3b 0a 20 20 20 20 20 20 2f 2a 2a 2a 2a 20 54  );.      /**** T
227b2 4f 44 4f 3a 20 20 61 64 64 20 63 6f 6c 6c 61 74  ODO:  add collat
227b3 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2a 2a  ing sequence ***
227b4 2a 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65  **/.      sqlite
227b5 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
227b6 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55   SQLITE_JUMPIFNU
227b7 4c 4c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  LL);.    }.    i
227b8 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
227b9 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  led ){.      /* 
227ba 49 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c  If a malloc fail
227bb 75 72 65 20 68 61 73 20 6f 63 63 75 72 72 65 64  ure has occurred
227bc 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c  , then the resul
227bd 74 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73  t of the express
227be 69 6f 6e 20 0a 20 20 20 20 20 20 2a 2a 20 70 61  ion .      ** pa
227bf 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f  ssed as the seco
227c0 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  nd argument to t
227c1 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  he call to sqlit
227c2 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 29  e3VdbeJumpHere()
227c3 20 0a 20 20 20 20 20 20 2a 2a 20 62 65 6c 6f 77   .      ** below
227c4 20 6d 61 79 20 62 65 20 6e 65 67 61 74 69 76 65   may be negative
227c5 2e 20 57 68 69 63 68 20 63 61 75 73 65 73 20 61  . Which causes a
227c6 6e 20 61 73 73 65 72 74 28 29 20 74 6f 20 66 61  n assert() to fa
227c7 69 6c 20 28 6f 72 20 61 6e 0a 20 20 20 20 20 20  il (or an.      
227c8 2a 2a 20 6f 75 74 2d 6f 66 2d 62 6f 75 6e 64 73  ** out-of-bounds
227c9 20 77 72 69 74 65 20 69 66 20 53 51 4c 49 54 45   write if SQLITE
227ca 5f 44 45 42 55 47 20 69 73 20 6e 6f 74 20 64 65  _DEBUG is not de
227cb 66 69 6e 65 64 29 2e 20 20 2a 2f 0a 20 20 20 20  fined).  */.    
227cc 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a    return;.    }.
227cd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
227ce 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp2(v, OP_Goto
227cf 2c 20 30 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b  , 0, endOfLoop);
227d0 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
227d1 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
227d2 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
227d3 70 48 65 72 65 28 76 2c 20 73 71 6c 69 74 65 33  pHere(v, sqlite3
227d4 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
227d5 76 29 2d 28 6e 43 6f 6c 2a 32 29 29 3b 0a 20 20  v)-(nCol*2));.  
227d6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
227d7 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49  ddOp2(v, OP_AddI
227d8 6d 6d 2c 20 69 4d 65 6d 2b 69 2b 31 2c 20 31 29  mm, iMem+i+1, 1)
227d9 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
227da 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
227db 43 6f 6c 75 6d 6e 2c 20 69 49 64 78 43 75 72 2c  Column, iIdxCur,
227dc 20 69 2c 20 69 4d 65 6d 2b 6e 43 6f 6c 2b 69 2b   i, iMem+nCol+i+
227dd 31 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  1);.    }..    /
227de 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 61 6e 61  * End of the ana
227df 6c 79 73 69 73 20 6c 6f 6f 70 2e 20 2a 2f 0a 20  lysis loop. */. 
227e0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65     sqlite3VdbeRe
227e1 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 65 6e  solveLabel(v, en
227e2 64 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 20 73 71  dOfLoop);.    sq
227e3 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
227e4 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 69 49 64 78  v, OP_Next, iIdx
227e5 43 75 72 2c 20 74 6f 70 4f 66 4c 6f 6f 70 29 3b  Cur, topOfLoop);
227e6 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
227e7 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp1(v, OP_Clo
227e8 73 65 2c 20 69 49 64 78 43 75 72 29 3b 0a 0a 20  se, iIdxCur);.. 
227e9 20 20 20 2f 2a 20 53 74 6f 72 65 20 74 68 65 20     /* Store the 
227ea 72 65 73 75 6c 74 73 20 69 6e 20 73 71 6c 69 74  results in sqlit
227eb 65 5f 73 74 61 74 31 2e 0a 20 20 20 20 2a 2a 0a  e_stat1..    **.
227ec 20 20 20 20 2a 2a 20 54 68 65 20 72 65 73 75 6c      ** The resul
227ed 74 20 69 73 20 61 20 73 69 6e 67 6c 65 20 72 6f  t is a single ro
227ee 77 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f  w of the sqlite_
227ef 73 74 61 74 31 20 74 61 62 6c 65 2e 20 20 54 68  stat1 table.  Th
227f0 65 20 66 69 72 73 74 0a 20 20 20 20 2a 2a 20 74  e first.    ** t
227f1 77 6f 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 74  wo columns are t
227f2 68 65 20 6e 61 6d 65 73 20 6f 66 20 74 68 65 20  he names of the 
227f3 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 2e  table and index.
227f4 20 20 54 68 65 20 74 68 69 72 64 20 63 6f 6c 75    The third colu
227f5 6d 6e 0a 20 20 20 20 2a 2a 20 69 73 20 61 20 73  mn.    ** is a s
227f6 74 72 69 6e 67 20 63 6f 6d 70 6f 73 65 64 20 6f  tring composed o
227f7 66 20 61 20 6c 69 73 74 20 6f 66 20 69 6e 74 65  f a list of inte
227f8 67 65 72 20 73 74 61 74 69 73 74 69 63 73 20 61  ger statistics a
227f9 62 6f 75 74 20 74 68 65 0a 20 20 20 20 2a 2a 20  bout the.    ** 
227fa 69 6e 64 65 78 2e 20 20 54 68 65 20 66 69 72 73  index.  The firs
227fb 74 20 69 6e 74 65 67 65 72 20 69 6e 20 74 68 65  t integer in the
227fc 20 6c 69 73 74 20 69 73 20 74 68 65 20 74 6f 74   list is the tot
227fd 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74  al number of ent
227fe 72 69 65 73 0a 20 20 20 20 2a 2a 20 69 6e 20 74  ries.    ** in t
227ff 68 65 20 69 6e 64 65 78 2e 20 20 54 68 65 72 65  he index.  There
22800 20 69 73 20 6f 6e 65 20 61 64 64 69 74 69 6f 6e   is one addition
22801 61 6c 20 69 6e 74 65 67 65 72 20 69 6e 20 74 68  al integer in th
22802 65 20 6c 69 73 74 20 66 6f 72 20 65 61 63 68 0a  e list for each.
22803 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6f 66      ** column of
22804 20 74 68 65 20 74 61 62 6c 65 2e 20 20 54 68 69   the table.  Thi
22805 73 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 74  s additional int
22806 65 67 65 72 20 69 73 20 61 20 67 75 65 73 73 20  eger is a guess 
22807 6f 66 20 68 6f 77 20 6d 61 6e 79 0a 20 20 20 20  of how many.    
22808 2a 2a 20 72 6f 77 73 20 6f 66 20 74 68 65 20 74  ** rows of the t
22809 61 62 6c 65 20 74 68 65 20 69 6e 64 65 78 20 77  able the index w
2280a 69 6c 6c 20 73 65 6c 65 63 74 2e 20 20 49 66 20  ill select.  If 
2280b 44 20 69 73 20 74 68 65 20 63 6f 75 6e 74 20 6f  D is the count o
2280c 66 20 64 69 73 74 69 6e 63 74 0a 20 20 20 20 2a  f distinct.    *
2280d 2a 20 76 61 6c 75 65 73 20 61 6e 64 20 4b 20 69  * values and K i
2280e 73 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62  s the total numb
2280f 65 72 20 6f 66 20 72 6f 77 73 2c 20 74 68 65 6e  er of rows, then
22810 20 74 68 65 20 69 6e 74 65 67 65 72 20 69 73 20   the integer is 
22811 63 6f 6d 70 75 74 65 64 0a 20 20 20 20 2a 2a 20  computed.    ** 
22812 61 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  as:.    **.    *
22813 2a 20 20 20 20 20 20 20 20 49 20 3d 20 28 4b 2b  *        I = (K+
22814 44 2d 31 29 2f 44 0a 20 20 20 20 2a 2a 0a 20 20  D-1)/D.    **.  
22815 20 20 2a 2a 20 49 66 20 4b 3d 3d 30 20 74 68 65    ** If K==0 the
22816 6e 20 6e 6f 20 65 6e 74 72 79 20 69 73 20 6d 61  n no entry is ma
22817 64 65 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69  de into the sqli
22818 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 2e 20  te_stat1 table. 
22819 20 0a 20 20 20 20 2a 2a 20 49 66 20 4b 3e 30 20   .    ** If K>0 
2281a 74 68 65 6e 20 69 74 20 69 73 20 61 6c 77 61 79  then it is alway
2281b 73 20 74 68 65 20 63 61 73 65 20 74 68 65 20 44  s the case the D
2281c 3e 30 20 73 6f 20 64 69 76 69 73 69 6f 6e 20 62  >0 so division b
2281d 79 20 7a 65 72 6f 0a 20 20 20 20 2a 2a 20 69 73  y zero.    ** is
2281e 20 6e 65 76 65 72 20 70 6f 73 73 69 62 6c 65 2e   never possible.
2281f 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 64 64 72  .    */.    addr
22820 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
22821 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 4e 6f 74  dOp1(v, OP_IfNot
22822 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 73 71 6c  , iMem);.    sql
22823 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
22824 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 69 4d 65 6d  , OP_SCopy, iMem
22825 2c 20 72 65 67 53 61 6d 70 6c 65 6e 6f 29 3b 0a  , regSampleno);.
22826 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
22827 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Col; i++){.     
22828 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22829 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
2282a 2c 20 30 2c 20 72 65 67 54 65 6d 70 2c 20 30 2c  , 0, regTemp, 0,
2282b 20 22 20 22 2c 20 30 29 3b 0a 20 20 20 20 20 20   " ", 0);.      
2282c 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2282d 33 28 76 2c 20 4f 50 5f 43 6f 6e 63 61 74 2c 20  3(v, OP_Concat, 
2282e 72 65 67 54 65 6d 70 2c 20 72 65 67 53 61 6d 70  regTemp, regSamp
2282f 6c 65 6e 6f 2c 20 72 65 67 53 61 6d 70 6c 65 6e  leno, regSamplen
22830 6f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  o);.      sqlite
22831 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
22832 50 5f 41 64 64 2c 20 69 4d 65 6d 2c 20 69 4d 65  P_Add, iMem, iMe
22833 6d 2b 69 2b 31 2c 20 72 65 67 54 65 6d 70 29 3b  m+i+1, regTemp);
22834 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
22835 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41  beAddOp2(v, OP_A
22836 64 64 49 6d 6d 2c 20 72 65 67 54 65 6d 70 2c 20  ddImm, regTemp, 
22837 2d 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  -1);.      sqlit
22838 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
22839 4f 50 5f 44 69 76 69 64 65 2c 20 69 4d 65 6d 2b  OP_Divide, iMem+
2283a 69 2b 31 2c 20 72 65 67 54 65 6d 70 2c 20 72 65  i+1, regTemp, re
2283b 67 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 73 71  gTemp);.      sq
2283c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
2283d 76 2c 20 4f 50 5f 54 6f 49 6e 74 2c 20 72 65 67  v, OP_ToInt, reg
2283e 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 73 71 6c  Temp);.      sql
2283f 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
22840 2c 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 72 65 67  , OP_Concat, reg
22841 54 65 6d 70 2c 20 72 65 67 53 61 6d 70 6c 65 6e  Temp, regSamplen
22842 6f 2c 20 72 65 67 53 61 6d 70 6c 65 6e 6f 29 3b  o, regSampleno);
22843 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
22844 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
22845 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72  OP_MakeRecord, r
22846 65 67 54 61 62 6e 61 6d 65 2c 20 33 2c 20 72 65  egTabname, 3, re
22847 67 52 65 63 2c 20 22 61 61 61 22 2c 20 30 29 3b  gRec, "aaa", 0);
22848 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
22849 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77  AddOp2(v, OP_New
2284a 52 6f 77 69 64 2c 20 69 53 74 61 74 43 75 72 2c  Rowid, iStatCur,
2284b 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20   regRowid);.    
2284c 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2284d 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20  3(v, OP_Insert, 
2284e 69 53 74 61 74 43 75 72 2c 20 72 65 67 52 65 63  iStatCur, regRec
2284f 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20  , regRowid);.   
22850 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
22851 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41  geP5(v, OPFLAG_A
22852 50 50 45 4e 44 29 3b 0a 20 20 20 20 73 71 6c 69  PPEND);.    sqli
22853 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
22854 76 2c 20 61 64 64 72 29 3b 0a 20 20 7d 0a 7d 0a  v, addr);.  }.}.
22855 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
22856 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 63  code that will c
22857 61 75 73 65 20 74 68 65 20 6d 6f 73 74 20 72 65  ause the most re
22858 63 65 6e 74 20 69 6e 64 65 78 20 61 6e 61 6c 79  cent index analy
22859 73 69 73 20 74 6f 0a 2a 2a 20 62 65 20 6c 61 6f  sis to.** be lao
2285a 64 65 64 20 69 6e 74 6f 20 69 6e 74 65 72 6e 61  ded into interna
2285b 6c 20 68 61 73 68 20 74 61 62 6c 65 73 20 77 68  l hash tables wh
2285c 65 72 65 20 69 73 20 63 61 6e 20 62 65 20 75 73  ere is can be us
2285d 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ed..*/.static vo
2285e 69 64 20 6c 6f 61 64 41 6e 61 6c 79 73 69 73 28  id loadAnalysis(
2285f 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69  Parse *pParse, i
22860 6e 74 20 69 44 62 29 7b 0a 20 20 56 64 62 65 20  nt iDb){.  Vdbe 
22861 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56  *v = sqlite3GetV
22862 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69  dbe(pParse);.  i
22863 66 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69  f( v ){.    sqli
22864 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
22865 20 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 73   OP_LoadAnalysis
22866 2c 20 69 44 62 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  , iDb);.  }.}../
22867 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
22868 64 65 20 74 68 61 74 20 77 69 6c 6c 20 64 6f 20  de that will do 
22869 61 6e 20 61 6e 61 6c 79 73 69 73 20 6f 66 20 61  an analysis of a
2286a 6e 20 65 6e 74 69 72 65 20 64 61 74 61 62 61 73  n entire databas
2286b 65 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  e.*/.static void
2286c 20 61 6e 61 6c 79 7a 65 44 61 74 61 62 61 73 65   analyzeDatabase
2286d 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
2286e 69 6e 74 20 69 44 62 29 7b 0a 20 20 73 71 6c 69  int iDb){.  sqli
2286f 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
22870 2d 3e 64 62 3b 0a 20 20 53 63 68 65 6d 61 20 2a  ->db;.  Schema *
22871 70 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44  pSchema = db->aD
22872 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b 20  b[iDb].pSchema; 
22873 20 20 20 2f 2a 20 53 63 68 65 6d 61 20 6f 66 20     /* Schema of 
22874 64 61 74 61 62 61 73 65 20 69 44 62 20 2a 2f 0a  database iDb */.
22875 20 20 48 61 73 68 45 6c 65 6d 20 2a 6b 3b 0a 20    HashElem *k;. 
22876 20 69 6e 74 20 69 53 74 61 74 43 75 72 3b 0a 20   int iStatCur;. 
22877 20 69 6e 74 20 69 4d 65 6d 3b 0a 0a 20 20 73 71   int iMem;..  sq
22878 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f  lite3BeginWriteO
22879 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c  peration(pParse,
2287a 20 30 2c 20 69 44 62 29 3b 0a 20 20 69 53 74 61   0, iDb);.  iSta
2287b 74 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e  tCur = pParse->n
2287c 54 61 62 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e  Tab;.  pParse->n
2287d 54 61 62 20 2b 3d 20 32 3b 0a 20 20 6f 70 65 6e  Tab += 2;.  open
2287e 53 74 61 74 54 61 62 6c 65 28 70 50 61 72 73 65  StatTable(pParse
2287f 2c 20 69 44 62 2c 20 69 53 74 61 74 43 75 72 2c  , iDb, iStatCur,
22880 20 30 29 3b 0a 20 20 69 4d 65 6d 20 3d 20 70 50   0);.  iMem = pP
22881 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20  arse->nMem+1;.  
22882 66 6f 72 28 6b 3d 73 71 6c 69 74 65 48 61 73 68  for(k=sqliteHash
22883 46 69 72 73 74 28 26 70 53 63 68 65 6d 61 2d 3e  First(&pSchema->
22884 74 62 6c 48 61 73 68 29 3b 20 6b 3b 20 6b 3d 73  tblHash); k; k=s
22885 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 6b 29  qliteHashNext(k)
22886 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54  ){.    Table *pT
22887 61 62 20 3d 20 28 54 61 62 6c 65 2a 29 73 71 6c  ab = (Table*)sql
22888 69 74 65 48 61 73 68 44 61 74 61 28 6b 29 3b 0a  iteHashData(k);.
22889 20 20 20 20 61 6e 61 6c 79 7a 65 4f 6e 65 54 61      analyzeOneTa
2288a 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61 62  ble(pParse, pTab
2288b 2c 20 69 53 74 61 74 43 75 72 2c 20 69 4d 65 6d  , iStatCur, iMem
2288c 29 3b 0a 20 20 7d 0a 20 20 6c 6f 61 64 41 6e 61  );.  }.  loadAna
2288d 6c 79 73 69 73 28 70 50 61 72 73 65 2c 20 69 44  lysis(pParse, iD
2288e 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e  b);.}../*.** Gen
2288f 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20  erate code that 
22890 77 69 6c 6c 20 64 6f 20 61 6e 20 61 6e 61 6c 79  will do an analy
22891 73 69 73 20 6f 66 20 61 20 73 69 6e 67 6c 65 20  sis of a single 
22892 74 61 62 6c 65 20 69 6e 0a 2a 2a 20 61 20 64 61  table in.** a da
22893 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69  tabase..*/.stati
22894 63 20 76 6f 69 64 20 61 6e 61 6c 79 7a 65 54 61  c void analyzeTa
22895 62 6c 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  ble(Parse *pPars
22896 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b  e, Table *pTab){
22897 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 69 6e  .  int iDb;.  in
22898 74 20 69 53 74 61 74 43 75 72 3b 0a 0a 20 20 61  t iStatCur;..  a
22899 73 73 65 72 74 28 20 70 54 61 62 21 3d 30 20 29  ssert( pTab!=0 )
2289a 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
2289b 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c  te3BtreeHoldsAll
2289c 4d 75 74 65 78 65 73 28 70 50 61 72 73 65 2d 3e  Mutexes(pParse->
2289d 64 62 29 20 29 3b 0a 20 20 69 44 62 20 3d 20 73  db) );.  iDb = s
2289e 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
2289f 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  dex(pParse->db, 
228a0 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  pTab->pSchema);.
228a1 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72    sqlite3BeginWr
228a2 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61  iteOperation(pPa
228a3 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20  rse, 0, iDb);.  
228a4 69 53 74 61 74 43 75 72 20 3d 20 70 50 61 72 73  iStatCur = pPars
228a5 65 2d 3e 6e 54 61 62 3b 0a 20 20 70 50 61 72 73  e->nTab;.  pPars
228a6 65 2d 3e 6e 54 61 62 20 2b 3d 20 32 3b 0a 20 20  e->nTab += 2;.  
228a7 6f 70 65 6e 53 74 61 74 54 61 62 6c 65 28 70 50  openStatTable(pP
228a8 61 72 73 65 2c 20 69 44 62 2c 20 69 53 74 61 74  arse, iDb, iStat
228a9 43 75 72 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  Cur, pTab->zName
228aa 29 3b 0a 20 20 61 6e 61 6c 79 7a 65 4f 6e 65 54  );.  analyzeOneT
228ab 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61  able(pParse, pTa
228ac 62 2c 20 69 53 74 61 74 43 75 72 2c 20 70 50 61  b, iStatCur, pPa
228ad 72 73 65 2d 3e 6e 4d 65 6d 2b 31 29 3b 0a 20 20  rse->nMem+1);.  
228ae 6c 6f 61 64 41 6e 61 6c 79 73 69 73 28 70 50 61  loadAnalysis(pPa
228af 72 73 65 2c 20 69 44 62 29 3b 0a 7d 0a 0a 2f 2a  rse, iDb);.}../*
228b0 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
228b1 65 20 66 6f 72 20 74 68 65 20 41 4e 41 4c 59 5a  e for the ANALYZ
228b2 45 20 63 6f 6d 6d 61 6e 64 2e 20 20 54 68 65 20  E command.  The 
228b3 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69  parser calls thi
228b4 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 77 68 65  s routine.** whe
228b5 6e 20 69 74 20 72 65 63 6f 67 6e 69 7a 65 73 20  n it recognizes 
228b6 61 6e 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61  an ANALYZE comma
228b7 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  nd..**.**       
228b8 20 41 4e 41 4c 59 5a 45 20 20 20 20 20 20 20 20   ANALYZE        
228b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
228ba 20 20 20 20 2d 2d 20 31 0a 2a 2a 20 20 20 20 20      -- 1.**     
228bb 20 20 20 41 4e 41 4c 59 5a 45 20 20 3c 64 61 74     ANALYZE  <dat
228bc 61 62 61 73 65 3e 20 20 20 20 20 20 20 20 20 20  abase>          
228bd 20 20 20 20 20 20 2d 2d 20 32 0a 2a 2a 20 20 20        -- 2.**   
228be 20 20 20 20 20 41 4e 41 4c 59 5a 45 20 20 3f 3c       ANALYZE  ?<
228bf 64 61 74 61 62 61 73 65 3e 2e 3f 3c 74 61 62 6c  database>.?<tabl
228c0 65 6e 61 6d 65 3e 20 20 2d 2d 20 33 0a 2a 2a 0a  ename>  -- 3.**.
228c1 2a 2a 20 46 6f 72 6d 20 31 20 63 61 75 73 65 73  ** Form 1 causes
228c2 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 69 6e 20   all indices in 
228c3 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64 61 74  all attached dat
228c4 61 62 61 73 65 73 20 74 6f 20 62 65 20 61 6e 61  abases to be ana
228c5 6c 79 7a 65 64 2e 0a 2a 2a 20 46 6f 72 6d 20 32  lyzed..** Form 2
228c6 20 61 6e 61 6c 79 7a 65 73 20 61 6c 6c 20 69 6e   analyzes all in
228c7 64 69 63 65 73 20 74 68 65 20 73 69 6e 67 6c 65  dices the single
228c8 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 64 2e   database named.
228c9 0a 2a 2a 20 46 6f 72 6d 20 33 20 61 6e 61 6c 79  .** Form 3 analy
228ca 7a 65 73 20 61 6c 6c 20 69 6e 64 69 63 65 73 20  zes all indices 
228cb 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
228cc 74 68 65 20 6e 61 6d 65 64 20 74 61 62 6c 65 2e  the named table.
228cd 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
228ce 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
228cf 6e 61 6c 79 7a 65 28 50 61 72 73 65 20 2a 70 50  nalyze(Parse *pP
228d0 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61  arse, Token *pNa
228d1 6d 65 31 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d  me1, Token *pNam
228d2 65 32 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  e2){.  sqlite3 *
228d3 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
228d4 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 69 6e  .  int iDb;.  in
228d5 74 20 69 3b 0a 20 20 63 68 61 72 20 2a 7a 2c 20  t i;.  char *z, 
228d6 2a 7a 44 62 3b 0a 20 20 54 61 62 6c 65 20 2a 70  *zDb;.  Table *p
228d7 54 61 62 3b 0a 20 20 54 6f 6b 65 6e 20 2a 70 54  Tab;.  Token *pT
228d8 61 62 6c 65 4e 61 6d 65 3b 0a 0a 20 20 2f 2a 20  ableName;..  /* 
228d9 52 65 61 64 20 74 68 65 20 64 61 74 61 62 61 73  Read the databas
228da 65 20 73 63 68 65 6d 61 2e 20 49 66 20 61 6e 20  e schema. If an 
228db 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 6c 65  error occurs, le
228dc 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ave an error mes
228dd 73 61 67 65 0a 20 20 2a 2a 20 61 6e 64 20 63 6f  sage.  ** and co
228de 64 65 20 69 6e 20 70 50 61 72 73 65 20 61 6e 64  de in pParse and
228df 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f   return NULL. */
228e0 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
228e1 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d  e3BtreeHoldsAllM
228e2 75 74 65 78 65 73 28 70 50 61 72 73 65 2d 3e 64  utexes(pParse->d
228e3 62 29 20 29 3b 0a 20 20 69 66 28 20 53 51 4c 49  b) );.  if( SQLI
228e4 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65  TE_OK!=sqlite3Re
228e5 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29  adSchema(pParse)
228e6 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
228e7 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70    }..  assert( p
228e8 4e 61 6d 65 32 21 3d 30 20 7c 7c 20 70 4e 61 6d  Name2!=0 || pNam
228e9 65 31 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 70  e1==0 );.  if( p
228ea 4e 61 6d 65 31 3d 3d 30 20 29 7b 0a 20 20 20 20  Name1==0 ){.    
228eb 2f 2a 20 46 6f 72 6d 20 31 3a 20 20 41 6e 61 6c  /* Form 1:  Anal
228ec 79 7a 65 20 65 76 65 72 79 74 68 69 6e 67 20 2a  yze everything *
228ed 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  /.    for(i=0; i
228ee 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a  <db->nDb; i++){.
228ef 20 20 20 20 20 20 69 66 28 20 69 3d 3d 31 20 29        if( i==1 )
228f0 20 63 6f 6e 74 69 6e 75 65 3b 20 20 2f 2a 20 44   continue;  /* D
228f1 6f 20 6e 6f 74 20 61 6e 61 6c 79 7a 65 20 74 68  o not analyze th
228f2 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 20  e TEMP database 
228f3 2a 2f 0a 20 20 20 20 20 20 61 6e 61 6c 79 7a 65  */.      analyze
228f4 44 61 74 61 62 61 73 65 28 70 50 61 72 73 65 2c  Database(pParse,
228f5 20 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c   i);.    }.  }el
228f6 73 65 20 69 66 28 20 70 4e 61 6d 65 32 2d 3e 6e  se if( pName2->n
228f7 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 46 6f  ==0 ){.    /* Fo
228f8 72 6d 20 32 3a 20 20 41 6e 61 6c 79 7a 65 20 74  rm 2:  Analyze t
228f9 68 65 20 64 61 74 61 62 61 73 65 20 6f 72 20 74  he database or t
228fa 61 62 6c 65 20 6e 61 6d 65 64 20 2a 2f 0a 20 20  able named */.  
228fb 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 46    iDb = sqlite3F
228fc 69 6e 64 44 62 28 64 62 2c 20 70 4e 61 6d 65 31  indDb(db, pName1
228fd 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3e 3d  );.    if( iDb>=
228fe 30 20 29 7b 0a 20 20 20 20 20 20 61 6e 61 6c 79  0 ){.      analy
228ff 7a 65 44 61 74 61 62 61 73 65 28 70 50 61 72 73  zeDatabase(pPars
22900 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 7d 65 6c  e, iDb);.    }el
22901 73 65 7b 0a 20 20 20 20 20 20 7a 20 3d 20 73 71  se{.      z = sq
22902 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
22903 65 6e 28 64 62 2c 20 70 4e 61 6d 65 31 29 3b 0a  en(db, pName1);.
22904 20 20 20 20 20 20 69 66 28 20 7a 20 29 7b 0a 20        if( z ){. 
22905 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 73 71         pTab = sq
22906 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65  lite3LocateTable
22907 28 70 50 61 72 73 65 2c 20 30 2c 20 7a 2c 20 30  (pParse, 0, z, 0
22908 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
22909 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 29 3b  e3DbFree(db, z);
2290a 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 54 61  .        if( pTa
2290b 62 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61  b ){.          a
2290c 6e 61 6c 79 7a 65 54 61 62 6c 65 28 70 50 61 72  nalyzeTable(pPar
2290d 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 20  se, pTab);.     
2290e 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
2290f 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
22910 2f 2a 20 46 6f 72 6d 20 33 3a 20 41 6e 61 6c 79  /* Form 3: Analy
22911 7a 65 20 74 68 65 20 66 75 6c 6c 79 20 71 75 61  ze the fully qua
22912 6c 69 66 69 65 64 20 74 61 62 6c 65 20 6e 61 6d  lified table nam
22913 65 20 2a 2f 0a 20 20 20 20 69 44 62 20 3d 20 73  e */.    iDb = s
22914 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d  qlite3TwoPartNam
22915 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 31  e(pParse, pName1
22916 2c 20 70 4e 61 6d 65 32 2c 20 26 70 54 61 62 6c  , pName2, &pTabl
22917 65 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20  eName);.    if( 
22918 69 44 62 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20  iDb>=0 ){.      
22919 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44  zDb = db->aDb[iD
2291a 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20  b].zName;.      
2291b 7a 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  z = sqlite3NameF
2291c 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54 61  romToken(db, pTa
2291d 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  bleName);.      
2291e 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 20 20 20  if( z ){.       
2291f 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c   pTab = sqlite3L
22920 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73  ocateTable(pPars
22921 65 2c 20 30 2c 20 7a 2c 20 7a 44 62 29 3b 0a 20  e, 0, z, zDb);. 
22922 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62         sqlite3Db
22923 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20  Free(db, z);.   
22924 20 20 20 20 20 69 66 28 20 70 54 61 62 20 29 7b       if( pTab ){
22925 0a 20 20 20 20 20 20 20 20 20 20 61 6e 61 6c 79  .          analy
22926 7a 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  zeTable(pParse, 
22927 70 54 61 62 29 3b 0a 20 20 20 20 20 20 20 20 7d  pTab);.        }
22928 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 20  .      }.    }  
22929 20 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55   .  }.}../*.** U
2292a 73 65 64 20 74 6f 20 70 61 73 73 20 69 6e 66 6f  sed to pass info
2292b 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65  rmation from the
2292c 20 61 6e 61 6c 79 7a 65 72 20 72 65 61 64 65 72   analyzer reader
2292d 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 0a   through to the.
2292e 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74  ** callback rout
2292f 69 6e 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ine..*/.typedef 
22930 73 74 72 75 63 74 20 61 6e 61 6c 79 73 69 73 49  struct analysisI
22931 6e 66 6f 20 61 6e 61 6c 79 73 69 73 49 6e 66 6f  nfo analysisInfo
22932 3b 0a 73 74 72 75 63 74 20 61 6e 61 6c 79 73 69  ;.struct analysi
22933 73 49 6e 66 6f 20 7b 0a 20 20 73 71 6c 69 74 65  sInfo {.  sqlite
22934 33 20 2a 64 62 3b 0a 20 20 63 6f 6e 73 74 20 63  3 *db;.  const c
22935 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65 3b 0a  har *zDatabase;.
22936 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 63  };../*.** This c
22937 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b  allback is invok
22938 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68  ed once for each
22939 20 69 6e 64 65 78 20 77 68 65 6e 20 72 65 61 64   index when read
2293a 69 6e 67 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74  ing the.** sqlit
2293b 65 5f 73 74 61 74 31 20 74 61 62 6c 65 2e 20 20  e_stat1 table.  
2293c 0a 2a 2a 0a 2a 2a 20 20 20 20 20 61 72 67 76 5b  .**.**     argv[
2293d 30 5d 20 3d 20 6e 61 6d 65 20 6f 66 20 74 68 65  0] = name of the
2293e 20 69 6e 64 65 78 0a 2a 2a 20 20 20 20 20 61 72   index.**     ar
2293f 67 76 5b 31 5d 20 3d 20 72 65 73 75 6c 74 73 20  gv[1] = results 
22940 6f 66 20 61 6e 61 6c 79 73 69 73 20 2d 20 6f 6e  of analysis - on
22941 20 69 6e 74 65 67 65 72 20 66 6f 72 20 65 61 63   integer for eac
22942 68 20 63 6f 6c 75 6d 6e 0a 2a 2f 0a 73 74 61 74  h column.*/.stat
22943 69 63 20 69 6e 74 20 61 6e 61 6c 79 73 69 73 4c  ic int analysisL
22944 6f 61 64 65 72 28 76 6f 69 64 20 2a 70 44 61 74  oader(void *pDat
22945 61 2c 20 69 6e 74 20 61 72 67 63 2c 20 63 68 61  a, int argc, cha
22946 72 20 2a 2a 61 72 67 76 2c 20 63 68 61 72 20 2a  r **argv, char *
22947 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 61 6e 61  *NotUsed){.  ana
22948 6c 79 73 69 73 49 6e 66 6f 20 2a 70 49 6e 66 6f  lysisInfo *pInfo
22949 20 3d 20 28 61 6e 61 6c 79 73 69 73 49 6e 66 6f   = (analysisInfo
2294a 2a 29 70 44 61 74 61 3b 0a 20 20 49 6e 64 65 78  *)pData;.  Index
2294b 20 2a 70 49 6e 64 65 78 3b 0a 20 20 69 6e 74 20   *pIndex;.  int 
2294c 69 2c 20 63 3b 0a 20 20 75 6e 73 69 67 6e 65 64  i, c;.  unsigned
2294d 20 69 6e 74 20 76 3b 0a 20 20 63 6f 6e 73 74 20   int v;.  const 
2294e 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 61 73 73 65  char *z;..  asse
2294f 72 74 28 20 61 72 67 63 3d 3d 32 20 29 3b 0a 20  rt( argc==2 );. 
22950 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
22951 52 32 28 4e 6f 74 55 73 65 64 2c 20 61 72 67 63  R2(NotUsed, argc
22952 29 3b 0a 0a 20 20 69 66 28 20 61 72 67 76 3d 3d  );..  if( argv==
22953 30 20 7c 7c 20 61 72 67 76 5b 30 5d 3d 3d 30 20  0 || argv[0]==0 
22954 7c 7c 20 61 72 67 76 5b 31 5d 3d 3d 30 20 29 7b  || argv[1]==0 ){
22955 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
22956 20 7d 0a 20 20 70 49 6e 64 65 78 20 3d 20 73 71   }.  pIndex = sq
22957 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 70  lite3FindIndex(p
22958 49 6e 66 6f 2d 3e 64 62 2c 20 61 72 67 76 5b 30  Info->db, argv[0
22959 5d 2c 20 70 49 6e 66 6f 2d 3e 7a 44 61 74 61 62  ], pInfo->zDatab
2295a 61 73 65 29 3b 0a 20 20 69 66 28 20 70 49 6e 64  ase);.  if( pInd
2295b 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  ex==0 ){.    ret
2295c 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 20 3d  urn 0;.  }.  z =
2295d 20 61 72 67 76 5b 31 5d 3b 0a 20 20 66 6f 72 28   argv[1];.  for(
2295e 69 3d 30 3b 20 2a 7a 20 26 26 20 69 3c 3d 70 49  i=0; *z && i<=pI
2295f 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69  ndex->nColumn; i
22960 2b 2b 29 7b 0a 20 20 20 20 76 20 3d 20 30 3b 0a  ++){.    v = 0;.
22961 20 20 20 20 77 68 69 6c 65 28 20 28 63 3d 7a 5b      while( (c=z[
22962 30 5d 29 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27  0])>='0' && c<='
22963 39 27 20 29 7b 0a 20 20 20 20 20 20 76 20 3d 20  9' ){.      v = 
22964 76 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 3b 0a  v*10 + c - '0';.
22965 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d        z++;.    }
22966 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 69 52  .    pIndex->aiR
22967 6f 77 45 73 74 5b 69 5d 20 3d 20 76 3b 0a 20 20  owEst[i] = v;.  
22968 20 20 69 66 28 20 2a 7a 3d 3d 27 20 27 20 29 20    if( *z==' ' ) 
22969 7a 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  z++;.  }.  retur
2296a 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66  n 0;.}../*.** If
2296b 20 74 68 65 20 49 6e 64 65 78 2e 61 53 61 6d 70   the Index.aSamp
2296c 6c 65 20 76 61 72 69 61 62 6c 65 20 69 73 20 6e  le variable is n
2296d 6f 74 20 4e 55 4c 4c 2c 20 64 65 6c 65 74 65 20  ot NULL, delete 
2296e 74 68 65 20 61 53 61 6d 70 6c 65 5b 5d 20 61 72  the aSample[] ar
2296f 72 61 79 0a 2a 2a 20 61 6e 64 20 69 74 73 20 63  ray.** and its c
22970 6f 6e 74 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49  ontents..*/.SQLI
22971 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
22972 73 71 6c 69 74 65 33 44 65 6c 65 74 65 49 6e 64  sqlite3DeleteInd
22973 65 78 53 61 6d 70 6c 65 73 28 49 6e 64 65 78 20  exSamples(Index 
22974 2a 70 49 64 78 29 7b 0a 23 69 66 64 65 66 20 53  *pIdx){.#ifdef S
22975 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41  QLITE_ENABLE_STA
22976 54 32 0a 20 20 69 66 28 20 70 49 64 78 2d 3e 61  T2.  if( pIdx->a
22977 53 61 6d 70 6c 65 20 29 7b 0a 20 20 20 20 69 6e  Sample ){.    in
22978 74 20 6a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  t j;.    sqlite3
22979 20 2a 64 62 4d 65 6d 20 3d 20 70 49 64 78 2d 3e   *dbMem = pIdx->
2297a 70 54 61 62 6c 65 2d 3e 64 62 4d 65 6d 3b 0a 20  pTable->dbMem;. 
2297b 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 53 51     for(j=0; j<SQ
2297c 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c  LITE_INDEX_SAMPL
2297d 45 53 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  ES; j++){.      
2297e 49 6e 64 65 78 53 61 6d 70 6c 65 20 2a 70 20 3d  IndexSample *p =
2297f 20 26 70 49 64 78 2d 3e 61 53 61 6d 70 6c 65 5b   &pIdx->aSample[
22980 6a 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d  j];.      if( p-
22981 3e 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 54  >eType==SQLITE_T
22982 45 58 54 20 7c 7c 20 70 2d 3e 65 54 79 70 65 3d  EXT || p->eType=
22983 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b 0a  =SQLITE_BLOB ){.
22984 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
22985 62 46 72 65 65 28 70 49 64 78 2d 3e 70 54 61 62  bFree(pIdx->pTab
22986 6c 65 2d 3e 64 62 4d 65 6d 2c 20 70 2d 3e 75 2e  le->dbMem, p->u.
22987 7a 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  z);.      }.    
22988 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  }.    sqlite3DbF
22989 72 65 65 28 64 62 4d 65 6d 2c 20 70 49 64 78 2d  ree(dbMem, pIdx-
2298a 3e 61 53 61 6d 70 6c 65 29 3b 0a 20 20 20 20 70  >aSample);.    p
2298b 49 64 78 2d 3e 61 53 61 6d 70 6c 65 20 3d 20 30  Idx->aSample = 0
2298c 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 55 4e  ;.  }.#else.  UN
2298d 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70  USED_PARAMETER(p
2298e 49 64 78 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a  Idx);.#endif.}..
2298f 2f 2a 0a 2a 2a 20 4c 6f 61 64 20 74 68 65 20 63  /*.** Load the c
22990 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 73 71  ontent of the sq
22991 6c 69 74 65 5f 73 74 61 74 31 20 61 6e 64 20 73  lite_stat1 and s
22992 71 6c 69 74 65 5f 73 74 61 74 32 20 74 61 62 6c  qlite_stat2 tabl
22993 65 73 2e 20 54 68 65 0a 2a 2a 20 63 6f 6e 74 65  es. The.** conte
22994 6e 74 73 20 6f 66 20 73 71 6c 69 74 65 5f 73 74  nts of sqlite_st
22995 61 74 31 20 61 72 65 20 75 73 65 64 20 74 6f 20  at1 are used to 
22996 70 6f 70 75 6c 61 74 65 20 74 68 65 20 49 6e 64  populate the Ind
22997 65 78 2e 61 69 52 6f 77 45 73 74 5b 5d 0a 2a 2a  ex.aiRowEst[].**
22998 20 61 72 72 61 79 73 2e 20 54 68 65 20 63 6f 6e   arrays. The con
22999 74 65 6e 74 73 20 6f 66 20 73 71 6c 69 74 65 5f  tents of sqlite_
2299a 73 74 61 74 32 20 61 72 65 20 75 73 65 64 20 74  stat2 are used t
2299b 6f 20 70 6f 70 75 6c 61 74 65 20 74 68 65 0a 2a  o populate the.*
2299c 2a 20 49 6e 64 65 78 2e 61 53 61 6d 70 6c 65 5b  * Index.aSample[
2299d 5d 20 61 72 72 61 79 73 2e 0a 2a 2a 0a 2a 2a 20  ] arrays..**.** 
2299e 49 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74  If the sqlite_st
2299f 61 74 31 20 74 61 62 6c 65 20 69 73 20 6e 6f 74  at1 table is not
229a0 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20   present in the 
229a1 64 61 74 61 62 61 73 65 2c 20 53 51 4c 49 54 45  database, SQLITE
229a2 5f 45 52 52 4f 52 0a 2a 2a 20 69 73 20 72 65 74  _ERROR.** is ret
229a3 75 72 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 63  urned. In this c
229a4 61 73 65 2c 20 65 76 65 6e 20 69 66 20 53 51 4c  ase, even if SQL
229a5 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32  ITE_ENABLE_STAT2
229a6 20 77 61 73 20 64 65 66 69 6e 65 64 20 0a 2a 2a   was defined .**
229a7 20 64 75 72 69 6e 67 20 63 6f 6d 70 69 6c 61 74   during compilat
229a8 69 6f 6e 20 61 6e 64 20 74 68 65 20 73 71 6c 69  ion and the sqli
229a9 74 65 5f 73 74 61 74 32 20 74 61 62 6c 65 20 69  te_stat2 table i
229aa 73 20 70 72 65 73 65 6e 74 2c 20 6e 6f 20 64 61  s present, no da
229ab 74 61 20 69 73 20 0a 2a 2a 20 72 65 61 64 20 66  ta is .** read f
229ac 72 6f 6d 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 49 66  rom it..**.** If
229ad 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53   SQLITE_ENABLE_S
229ae 54 41 54 32 20 77 61 73 20 64 65 66 69 6e 65 64  TAT2 was defined
229af 20 64 75 72 69 6e 67 20 63 6f 6d 70 69 6c 61 74   during compilat
229b0 69 6f 6e 20 61 6e 64 20 74 68 65 20 0a 2a 2a 20  ion and the .** 
229b1 73 71 6c 69 74 65 5f 73 74 61 74 32 20 74 61 62  sqlite_stat2 tab
229b2 6c 65 20 69 73 20 6e 6f 74 20 70 72 65 73 65 6e  le is not presen
229b3 74 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  t in the databas
229b4 65 2c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20  e, SQLITE_ERROR 
229b5 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20  is.** returned. 
229b6 48 6f 77 65 76 65 72 2c 20 69 6e 20 74 68 69 73  However, in this
229b7 20 63 61 73 65 2c 20 64 61 74 61 20 69 73 20 72   case, data is r
229b8 65 61 64 20 66 72 6f 6d 20 74 68 65 20 73 71 6c  ead from the sql
229b9 69 74 65 5f 73 74 61 74 31 0a 2a 2a 20 74 61 62  ite_stat1.** tab
229ba 6c 65 20 28 69 66 20 69 74 20 69 73 20 70 72 65  le (if it is pre
229bb 73 65 6e 74 29 20 62 65 66 6f 72 65 20 72 65 74  sent) before ret
229bc 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66  urning..**.** If
229bd 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 20 6f 63   an OOM error oc
229be 63 75 72 73 2c 20 74 68 69 73 20 66 75 6e 63 74  curs, this funct
229bf 69 6f 6e 20 61 6c 77 61 79 73 20 73 65 74 73 20  ion always sets 
229c0 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
229c1 2e 0a 2a 2a 20 54 68 69 73 20 6d 65 61 6e 73 20  ..** This means 
229c2 69 66 20 74 68 65 20 63 61 6c 6c 65 72 20 64 6f  if the caller do
229c3 65 73 20 6e 6f 74 20 63 61 72 65 20 61 62 6f 75  es not care abou
229c4 74 20 6f 74 68 65 72 20 65 72 72 6f 72 73 2c 20  t other errors, 
229c5 74 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 63 6f  the return.** co
229c6 64 65 20 6d 61 79 20 62 65 20 69 67 6e 6f 72 65  de may be ignore
229c7 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
229c8 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
229c9 41 6e 61 6c 79 73 69 73 4c 6f 61 64 28 73 71 6c  AnalysisLoad(sql
229ca 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44  ite3 *db, int iD
229cb 62 29 7b 0a 20 20 61 6e 61 6c 79 73 69 73 49 6e  b){.  analysisIn
229cc 66 6f 20 73 49 6e 66 6f 3b 0a 20 20 48 61 73 68  fo sInfo;.  Hash
229cd 45 6c 65 6d 20 2a 69 3b 0a 20 20 63 68 61 72 20  Elem *i;.  char 
229ce 2a 7a 53 71 6c 3b 0a 20 20 69 6e 74 20 72 63 3b  *zSql;.  int rc;
229cf 0a 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e  ..  assert( iDb>
229d0 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44  =0 && iDb<db->nD
229d1 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64  b );.  assert( d
229d2 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 21  b->aDb[iDb].pBt!
229d3 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
229d4 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
229d5 73 4d 75 74 65 78 28 64 62 2d 3e 61 44 62 5b 69  sMutex(db->aDb[i
229d6 44 62 5d 2e 70 42 74 29 20 29 3b 0a 0a 20 20 2f  Db].pBt) );..  /
229d7 2a 20 43 6c 65 61 72 20 61 6e 79 20 70 72 69 6f  * Clear any prio
229d8 72 20 73 74 61 74 69 73 74 69 63 73 20 2a 2f 0a  r statistics */.
229d9 20 20 66 6f 72 28 69 3d 73 71 6c 69 74 65 48 61    for(i=sqliteHa
229da 73 68 46 69 72 73 74 28 26 64 62 2d 3e 61 44 62  shFirst(&db->aDb
229db 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 69  [iDb].pSchema->i
229dc 64 78 48 61 73 68 29 3b 69 3b 69 3d 73 71 6c 69  dxHash);i;i=sqli
229dd 74 65 48 61 73 68 4e 65 78 74 28 69 29 29 7b 0a  teHashNext(i)){.
229de 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20      Index *pIdx 
229df 3d 20 73 71 6c 69 74 65 48 61 73 68 44 61 74 61  = sqliteHashData
229e0 28 69 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  (i);.    sqlite3
229e1 44 65 66 61 75 6c 74 52 6f 77 45 73 74 28 70 49  DefaultRowEst(pI
229e2 64 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  dx);.    sqlite3
229e3 44 65 6c 65 74 65 49 6e 64 65 78 53 61 6d 70 6c  DeleteIndexSampl
229e4 65 73 28 70 49 64 78 29 3b 0a 20 20 7d 0a 0a 20  es(pIdx);.  }.. 
229e5 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b   /* Check to mak
229e6 65 20 73 75 72 65 20 74 68 65 20 73 71 6c 69 74  e sure the sqlit
229e7 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 65 78  e_stat1 table ex
229e8 69 73 74 73 20 2a 2f 0a 20 20 73 49 6e 66 6f 2e  ists */.  sInfo.
229e9 64 62 20 3d 20 64 62 3b 0a 20 20 73 49 6e 66 6f  db = db;.  sInfo
229ea 2e 7a 44 61 74 61 62 61 73 65 20 3d 20 64 62 2d  .zDatabase = db-
229eb 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b  >aDb[iDb].zName;
229ec 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69  .  if( sqlite3Fi
229ed 6e 64 54 61 62 6c 65 28 64 62 2c 20 22 73 71 6c  ndTable(db, "sql
229ee 69 74 65 5f 73 74 61 74 31 22 2c 20 73 49 6e 66  ite_stat1", sInf
229ef 6f 2e 7a 44 61 74 61 62 61 73 65 29 3d 3d 30 20  o.zDatabase)==0 
229f0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
229f1 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
229f2 0a 20 20 2f 2a 20 4c 6f 61 64 20 6e 65 77 20 73  .  /* Load new s
229f3 74 61 74 69 73 74 69 63 73 20 6f 75 74 20 6f 66  tatistics out of
229f4 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
229f5 31 20 74 61 62 6c 65 20 2a 2f 0a 20 20 7a 53 71  1 table */.  zSq
229f6 6c 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  l = sqlite3MPrin
229f7 74 66 28 64 62 2c 20 0a 20 20 20 20 20 20 22 53  tf(db, .      "S
229f8 45 4c 45 43 54 20 69 64 78 2c 20 73 74 61 74 20  ELECT idx, stat 
229f9 46 52 4f 4d 20 25 51 2e 73 71 6c 69 74 65 5f 73  FROM %Q.sqlite_s
229fa 74 61 74 31 22 2c 20 73 49 6e 66 6f 2e 7a 44 61  tat1", sInfo.zDa
229fb 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20 7a  tabase);.  if( z
229fc 53 71 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63  Sql==0 ){.    rc
229fd 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
229fe 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 28 76  .  }else{.    (v
229ff 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74  oid)sqlite3Safet
22a00 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 72 63  yOff(db);.    rc
22a01 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28   = sqlite3_exec(
22a02 64 62 2c 20 7a 53 71 6c 2c 20 61 6e 61 6c 79 73  db, zSql, analys
22a03 69 73 4c 6f 61 64 65 72 2c 20 26 73 49 6e 66 6f  isLoader, &sInfo
22a04 2c 20 30 29 3b 0a 20 20 20 20 28 76 6f 69 64 29  , 0);.    (void)
22a05 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
22a06 64 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  db);.    sqlite3
22a07 44 62 46 72 65 65 28 64 62 2c 20 7a 53 71 6c 29  DbFree(db, zSql)
22a08 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 4c 6f 61  ;.  }...  /* Loa
22a09 64 20 74 68 65 20 73 74 61 74 69 73 74 69 63 73  d the statistics
22a0a 20 66 72 6f 6d 20 74 68 65 20 73 71 6c 69 74 65   from the sqlite
22a0b 5f 73 74 61 74 32 20 74 61 62 6c 65 2e 20 2a 2f  _stat2 table. */
22a0c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
22a0d 4e 41 42 4c 45 5f 53 54 41 54 32 0a 20 20 69 66  NABLE_STAT2.  if
22a0e 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
22a0f 26 26 20 21 73 71 6c 69 74 65 33 46 69 6e 64 54  && !sqlite3FindT
22a10 61 62 6c 65 28 64 62 2c 20 22 73 71 6c 69 74 65  able(db, "sqlite
22a11 5f 73 74 61 74 32 22 2c 20 73 49 6e 66 6f 2e 7a  _stat2", sInfo.z
22a12 44 61 74 61 62 61 73 65 29 20 29 7b 0a 20 20 20  Database) ){.   
22a13 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
22a14 4f 52 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63  OR;.  }.  if( rc
22a15 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
22a16 20 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20     sqlite3_stmt 
22a17 2a 70 53 74 6d 74 20 3d 20 30 3b 0a 0a 20 20 20  *pStmt = 0;..   
22a18 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 4d   zSql = sqlite3M
22a19 50 72 69 6e 74 66 28 64 62 2c 20 0a 20 20 20 20  Printf(db, .    
22a1a 20 20 20 20 22 53 45 4c 45 43 54 20 69 64 78 2c      "SELECT idx,
22a1b 73 61 6d 70 6c 65 6e 6f 2c 73 61 6d 70 6c 65 20  sampleno,sample 
22a1c 46 52 4f 4d 20 25 51 2e 73 71 6c 69 74 65 5f 73  FROM %Q.sqlite_s
22a1d 74 61 74 32 22 2c 20 73 49 6e 66 6f 2e 7a 44 61  tat2", sInfo.zDa
22a1e 74 61 62 61 73 65 29 3b 0a 20 20 20 20 69 66 28  tabase);.    if(
22a1f 20 21 7a 53 71 6c 20 29 7b 0a 20 20 20 20 20 20   !zSql ){.      
22a20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
22a21 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  M;.    }else{.  
22a22 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65      (void)sqlite
22a23 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a  3SafetyOff(db);.
22a24 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
22a25 65 33 5f 70 72 65 70 61 72 65 28 64 62 2c 20 7a  e3_prepare(db, z
22a26 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c  Sql, -1, &pStmt,
22a27 20 30 29 3b 0a 20 20 20 20 20 20 28 76 6f 69 64   0);.      (void
22a28 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e  )sqlite3SafetyOn
22a29 28 64 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  (db);.      sqli
22a2a 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53  te3DbFree(db, zS
22a2b 71 6c 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  ql);.    }..    
22a2c 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
22a2d 4b 20 29 7b 0a 20 20 20 20 20 20 28 76 6f 69 64  K ){.      (void
22a2e 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66  )sqlite3SafetyOf
22a2f 66 28 64 62 29 3b 0a 20 20 20 20 20 20 77 68 69  f(db);.      whi
22a30 6c 65 28 20 73 71 6c 69 74 65 33 5f 73 74 65 70  le( sqlite3_step
22a31 28 70 53 74 6d 74 29 3d 3d 53 51 4c 49 54 45 5f  (pStmt)==SQLITE_
22a32 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 20 20 63  ROW ){.        c
22a33 68 61 72 20 2a 7a 49 6e 64 65 78 20 3d 20 28 63  har *zIndex = (c
22a34 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 63 6f  har *)sqlite3_co
22a35 6c 75 6d 6e 5f 74 65 78 74 28 70 53 74 6d 74 2c  lumn_text(pStmt,
22a36 20 30 29 3b 0a 20 20 20 20 20 20 20 20 49 6e 64   0);.        Ind
22a37 65 78 20 2a 70 49 64 78 20 3d 20 73 71 6c 69 74  ex *pIdx = sqlit
22a38 65 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20  e3FindIndex(db, 
22a39 7a 49 6e 64 65 78 2c 20 73 49 6e 66 6f 2e 7a 44  zIndex, sInfo.zD
22a3a 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20 20 20  atabase);.      
22a3b 20 20 69 66 28 20 70 49 64 78 20 29 7b 0a 20 20    if( pIdx ){.  
22a3c 20 20 20 20 20 20 20 20 69 6e 74 20 69 53 61 6d          int iSam
22a3d 70 6c 65 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  ple = sqlite3_co
22a3e 6c 75 6d 6e 5f 69 6e 74 28 70 53 74 6d 74 2c 20  lumn_int(pStmt, 
22a3f 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  1);.          sq
22a40 6c 69 74 65 33 20 2a 64 62 4d 65 6d 20 3d 20 70  lite3 *dbMem = p
22a41 49 64 78 2d 3e 70 54 61 62 6c 65 2d 3e 64 62 4d  Idx->pTable->dbM
22a42 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73  em;.          as
22a43 73 65 72 74 28 20 64 62 4d 65 6d 3d 3d 64 62 20  sert( dbMem==db 
22a44 7c 7c 20 64 62 4d 65 6d 3d 3d 30 20 29 3b 0a 20  || dbMem==0 );. 
22a45 20 20 20 20 20 20 20 20 20 69 66 28 20 69 53 61           if( iSa
22a46 6d 70 6c 65 3c 53 51 4c 49 54 45 5f 49 4e 44 45  mple<SQLITE_INDE
22a47 58 5f 53 41 4d 50 4c 45 53 20 26 26 20 69 53 61  X_SAMPLES && iSa
22a48 6d 70 6c 65 3e 3d 30 20 29 7b 0a 20 20 20 20 20  mple>=0 ){.     
22a49 20 20 20 20 20 20 20 69 6e 74 20 65 54 79 70 65         int eType
22a4a 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d   = sqlite3_colum
22a4b 6e 5f 74 79 70 65 28 70 53 74 6d 74 2c 20 32 29  n_type(pStmt, 2)
22a4c 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  ;..            i
22a4d 66 28 20 70 49 64 78 2d 3e 61 53 61 6d 70 6c 65  f( pIdx->aSample
22a4e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
22a4f 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73       static cons
22a50 74 20 69 6e 74 20 73 7a 20 3d 20 73 69 7a 65 6f  t int sz = sizeo
22a51 66 28 49 6e 64 65 78 53 61 6d 70 6c 65 29 2a 53  f(IndexSample)*S
22a52 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50  QLITE_INDEX_SAMP
22a53 4c 45 53 3b 0a 20 20 20 20 20 20 20 20 20 20 20  LES;.           
22a54 20 20 20 70 49 64 78 2d 3e 61 53 61 6d 70 6c 65     pIdx->aSample
22a55 20 3d 20 28 49 6e 64 65 78 53 61 6d 70 6c 65 20   = (IndexSample 
22a56 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  *)sqlite3DbMallo
22a57 63 5a 65 72 6f 28 64 62 4d 65 6d 2c 20 73 7a 29  cZero(dbMem, sz)
22a58 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
22a59 69 66 28 20 70 49 64 78 2d 3e 61 53 61 6d 70 6c  if( pIdx->aSampl
22a5a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  e==0 ){.        
22a5b 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c          db->mall
22a5c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
22a5d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72                br
22a5e 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20  eak;.           
22a5f 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
22a60 20 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 20 20   }..            
22a61 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e 61 53  assert( pIdx->aS
22a62 61 6d 70 6c 65 20 29 3b 0a 20 20 20 20 20 20 20  ample );.       
22a63 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 20       {.         
22a64 20 20 20 20 20 49 6e 64 65 78 53 61 6d 70 6c 65       IndexSample
22a65 20 2a 70 53 61 6d 70 6c 65 20 3d 20 26 70 49 64   *pSample = &pId
22a66 78 2d 3e 61 53 61 6d 70 6c 65 5b 69 53 61 6d 70  x->aSample[iSamp
22a67 6c 65 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 20  le];.           
22a68 20 20 20 70 53 61 6d 70 6c 65 2d 3e 65 54 79 70     pSample->eTyp
22a69 65 20 3d 20 28 75 38 29 65 54 79 70 65 3b 0a 20  e = (u8)eType;. 
22a6a 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28               if(
22a6b 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 49   eType==SQLITE_I
22a6c 4e 54 45 47 45 52 20 7c 7c 20 65 54 79 70 65 3d  NTEGER || eType=
22a6d 3d 53 51 4c 49 54 45 5f 46 4c 4f 41 54 20 29 7b  =SQLITE_FLOAT ){
22a6e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
22a6f 20 70 53 61 6d 70 6c 65 2d 3e 75 2e 72 20 3d 20   pSample->u.r = 
22a70 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64  sqlite3_column_d
22a71 6f 75 62 6c 65 28 70 53 74 6d 74 2c 20 32 29 3b  ouble(pStmt, 2);
22a72 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d  .              }
22a73 65 6c 73 65 20 69 66 28 20 65 54 79 70 65 3d 3d  else if( eType==
22a74 53 51 4c 49 54 45 5f 54 45 58 54 20 7c 7c 20 65  SQLITE_TEXT || e
22a75 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f  Type==SQLITE_BLO
22a76 42 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  B ){.           
22a77 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
22a78 2a 7a 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  *z = (const char
22a79 20 2a 29 28 0a 20 20 20 20 20 20 20 20 20 20 20   *)(.           
22a7a 20 20 20 20 20 20 20 20 20 28 65 54 79 70 65 3d           (eType=
22a7b 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 29 20 3f 0a  =SQLITE_BLOB) ?.
22a7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22a7d 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75      sqlite3_colu
22a7e 6d 6e 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 32  mn_blob(pStmt, 2
22a7f 29 3a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ):.             
22a80 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63         sqlite3_c
22a81 6f 6c 75 6d 6e 5f 74 65 78 74 28 70 53 74 6d 74  olumn_text(pStmt
22a82 2c 20 32 29 0a 20 20 20 20 20 20 20 20 20 20 20  , 2).           
22a83 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20       );.        
22a84 20 20 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20          int n = 
22a85 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
22a86 79 74 65 73 28 70 53 74 6d 74 2c 20 32 29 3b 0a  ytes(pStmt, 2);.
22a87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22a88 69 66 28 20 6e 3e 32 34 20 29 7b 0a 20 20 20 20  if( n>24 ){.    
22a89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 20                n 
22a8a 3d 20 32 34 3b 0a 20 20 20 20 20 20 20 20 20 20  = 24;.          
22a8b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
22a8c 20 20 20 20 20 20 20 20 70 53 61 6d 70 6c 65 2d          pSample-
22a8d 3e 6e 42 79 74 65 20 3d 20 28 75 38 29 6e 3b 0a  >nByte = (u8)n;.
22a8e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22a8f 70 53 61 6d 70 6c 65 2d 3e 75 2e 7a 20 3d 20 73  pSample->u.z = s
22a90 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
22a91 77 28 64 62 4d 65 6d 2c 20 6e 29 3b 0a 20 20 20  w(dbMem, n);.   
22a92 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28               if(
22a93 20 70 53 61 6d 70 6c 65 2d 3e 75 2e 7a 20 29 7b   pSample->u.z ){
22a94 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
22a95 20 20 20 6d 65 6d 63 70 79 28 70 53 61 6d 70 6c     memcpy(pSampl
22a96 65 2d 3e 75 2e 7a 2c 20 7a 2c 20 6e 29 3b 0a 20  e->u.z, z, n);. 
22a97 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d                 }
22a98 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
22a99 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c          db->mall
22a9a 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
22a9b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22a9c 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
22a9d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
22a9e 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
22a9f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
22aa0 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
22aa1 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20     }.      rc = 
22aa2 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
22aa3 28 70 53 74 6d 74 29 3b 0a 20 20 20 20 20 20 28  (pStmt);.      (
22aa4 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65  void)sqlite3Safe
22aa5 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 7d 0a  tyOn(db);.    }.
22aa6 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66    }.#endif..  if
22aa7 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  ( rc==SQLITE_NOM
22aa8 45 4d 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61  EM ){.    db->ma
22aa9 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
22aaa 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
22aab 0a 7d 0a 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53  .}...#endif /* S
22aac 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59  QLITE_OMIT_ANALY
22aad 5a 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ZE */../********
22aae 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 61 6e  ****** End of an
22aaf 61 6c 79 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  alyze.c ********
22ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22ab1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22ab2 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
22ab3 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
22ab4 65 20 61 74 74 61 63 68 2e 63 20 2a 2a 2a 2a 2a  e attach.c *****
22ab5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22ab6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22ab7 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
22ab8 33 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20  3 April 6.**.** 
22ab9 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
22aba 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
22abb 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
22abc 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
22abd 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
22abe 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
22abf 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
22ac0 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
22ac1 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
22ac2 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
22ac3 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
22ac4 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
22ac5 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
22ac6 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
22ac7 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
22ac8 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
22ac9 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
22aca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22acb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22acc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22acd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22ace 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
22acf 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
22ad0 6f 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c  ode used to impl
22ad1 65 6d 65 6e 74 20 74 68 65 20 41 54 54 41 43 48  ement the ATTACH
22ad2 20 61 6e 64 20 44 45 54 41 43 48 20 63 6f 6d 6d   and DETACH comm
22ad3 61 6e 64 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  ands..**.** $Id:
22ad4 20 61 74 74 61 63 68 2e 63 2c 76 20 31 2e 39 33   attach.c,v 1.93
22ad5 20 32 30 30 39 2f 30 35 2f 33 31 20 32 31 3a 32   2009/05/31 21:2
22ad6 31 3a 34 31 20 64 72 68 20 45 78 70 20 24 0a 2a  1:41 drh Exp $.*
22ad7 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
22ad8 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48 0a 2f 2a  E_OMIT_ATTACH./*
22ad9 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 61 6e 20 65  .** Resolve an e
22ada 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 77  xpression that w
22adb 61 73 20 70 61 72 74 20 6f 66 20 61 6e 20 41 54  as part of an AT
22adc 54 41 43 48 20 6f 72 20 44 45 54 41 43 48 20 73  TACH or DETACH s
22add 74 61 74 65 6d 65 6e 74 2e 20 54 68 69 73 0a 2a  tatement. This.*
22ade 2a 20 69 73 20 73 6c 69 67 68 74 6c 79 20 64 69  * is slightly di
22adf 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 72 65 73  fferent from res
22ae0 6f 6c 76 69 6e 67 20 61 20 6e 6f 72 6d 61 6c 20  olving a normal 
22ae1 53 51 4c 20 65 78 70 72 65 73 73 69 6f 6e 2c 20  SQL expression, 
22ae2 62 65 63 61 75 73 65 20 73 69 6d 70 6c 65 0a 2a  because simple.*
22ae3 2a 20 69 64 65 6e 74 69 66 69 65 72 73 20 61 72  * identifiers ar
22ae4 65 20 74 72 65 61 74 65 64 20 61 73 20 73 74 72  e treated as str
22ae5 69 6e 67 73 2c 20 6e 6f 74 20 70 6f 73 73 69 62  ings, not possib
22ae6 6c 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  le column names 
22ae7 6f 72 20 61 6c 69 61 73 65 73 2e 0a 2a 2a 0a 2a  or aliases..**.*
22ae8 2a 20 69 2e 65 2e 20 69 66 20 74 68 65 20 70 61  * i.e. if the pa
22ae9 72 73 65 72 20 73 65 65 73 3a 0a 2a 2a 0a 2a 2a  rser sees:.**.**
22aea 20 20 20 20 20 41 54 54 41 43 48 20 44 41 54 41       ATTACH DATA
22aeb 42 41 53 45 20 61 62 63 20 41 53 20 64 65 66 0a  BASE abc AS def.
22aec 2a 2a 0a 2a 2a 20 69 74 20 74 72 65 61 74 73 20  **.** it treats 
22aed 74 68 65 20 74 77 6f 20 65 78 70 72 65 73 73 69  the two expressi
22aee 6f 6e 73 20 61 73 20 6c 69 74 65 72 61 6c 20 73  ons as literal s
22aef 74 72 69 6e 67 73 20 27 61 62 63 27 20 61 6e 64  trings 'abc' and
22af0 20 27 64 65 66 27 20 69 6e 73 74 65 61 64 20 6f   'def' instead o
22af1 66 0a 2a 2a 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72  f.** looking for
22af2 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20   columns of the 
22af3 73 61 6d 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a  same name..**.**
22af4 20 54 68 69 73 20 6f 6e 6c 79 20 61 70 70 6c 69   This only appli
22af5 65 73 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 6e  es to the root n
22af6 6f 64 65 20 6f 66 20 70 45 78 70 72 2c 20 73 6f  ode of pExpr, so
22af7 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 3a 0a   the statement:.
22af8 2a 2a 0a 2a 2a 20 20 20 20 20 41 54 54 41 43 48  **.**     ATTACH
22af9 20 44 41 54 41 42 41 53 45 20 61 62 63 7c 7c 64   DATABASE abc||d
22afa 65 66 20 41 53 20 27 64 62 32 27 0a 2a 2a 0a 2a  ef AS 'db2'.**.*
22afb 2a 20 77 69 6c 6c 20 66 61 69 6c 20 62 65 63 61  * will fail beca
22afc 75 73 65 20 6e 65 69 74 68 65 72 20 61 62 63 20  use neither abc 
22afd 6f 72 20 64 65 66 20 63 61 6e 20 62 65 20 72 65  or def can be re
22afe 73 6f 6c 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  solved..*/.stati
22aff 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 41 74 74  c int resolveAtt
22b00 61 63 68 45 78 70 72 28 4e 61 6d 65 43 6f 6e 74  achExpr(NameCont
22b01 65 78 74 20 2a 70 4e 61 6d 65 2c 20 45 78 70 72  ext *pName, Expr
22b02 20 2a 70 45 78 70 72 29 0a 7b 0a 20 20 69 6e 74   *pExpr).{.  int
22b03 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
22b04 0a 20 20 69 66 28 20 70 45 78 70 72 20 29 7b 0a  .  if( pExpr ){.
22b05 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f      if( pExpr->o
22b06 70 21 3d 54 4b 5f 49 44 20 29 7b 0a 20 20 20 20  p!=TK_ID ){.    
22b07 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 52 65    rc = sqlite3Re
22b08 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 70  solveExprNames(p
22b09 4e 61 6d 65 2c 20 70 45 78 70 72 29 3b 0a 20 20  Name, pExpr);.  
22b0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
22b0b 54 45 5f 4f 4b 20 26 26 20 21 73 71 6c 69 74 65  TE_OK && !sqlite
22b0c 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 28  3ExprIsConstant(
22b0d 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20  pExpr) ){.      
22b0e 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
22b0f 67 28 70 4e 61 6d 65 2d 3e 70 50 61 72 73 65 2c  g(pName->pParse,
22b10 20 22 69 6e 76 61 6c 69 64 20 6e 61 6d 65 3a 20   "invalid name: 
22b11 5c 22 25 73 5c 22 22 2c 20 70 45 78 70 72 2d 3e  \"%s\"", pExpr->
22b12 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20  u.zToken);.     
22b13 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
22b14 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 7d 0a  _ERROR;.      }.
22b15 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
22b16 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f   pExpr->op = TK_
22b17 53 54 52 49 4e 47 3b 0a 20 20 20 20 7d 0a 20 20  STRING;.    }.  
22b18 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
22b19 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53 51 4c 20 75  ../*.** An SQL u
22b1a 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 72 65 67  ser-function reg
22b1b 69 73 74 65 72 65 64 20 74 6f 20 64 6f 20 74 68  istered to do th
22b1c 65 20 77 6f 72 6b 20 6f 66 20 61 6e 20 41 54 54  e work of an ATT
22b1d 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e 20 54  ACH statement. T
22b1e 68 65 0a 2a 2a 20 74 68 72 65 65 20 61 72 67 75  he.** three argu
22b1f 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 66 75 6e  ments to the fun
22b20 63 74 69 6f 6e 20 63 6f 6d 65 20 64 69 72 65 63  ction come direc
22b21 74 6c 79 20 66 72 6f 6d 20 61 6e 20 61 74 74 61  tly from an atta
22b22 63 68 20 73 74 61 74 65 6d 65 6e 74 3a 0a 2a 2a  ch statement:.**
22b23 0a 2a 2a 20 20 20 20 20 41 54 54 41 43 48 20 44  .**     ATTACH D
22b24 41 54 41 42 41 53 45 20 78 20 41 53 20 79 20 4b  ATABASE x AS y K
22b25 45 59 20 7a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53  EY z.**.**     S
22b26 45 4c 45 43 54 20 73 71 6c 69 74 65 5f 61 74 74  ELECT sqlite_att
22b27 61 63 68 28 78 2c 20 79 2c 20 7a 29 0a 2a 2a 0a  ach(x, y, z).**.
22b28 2a 2a 20 49 66 20 74 68 65 20 6f 70 74 69 6f 6e  ** If the option
22b29 61 6c 20 22 4b 45 59 20 7a 22 20 73 79 6e 74 61  al "KEY z" synta
22b2a 78 20 69 73 20 6f 6d 69 74 74 65 64 2c 20 61 6e  x is omitted, an
22b2b 20 53 51 4c 20 4e 55 4c 4c 20 69 73 20 70 61 73   SQL NULL is pas
22b2c 73 65 64 20 61 73 20 74 68 65 0a 2a 2a 20 74 68  sed as the.** th
22b2d 69 72 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f  ird argument..*/
22b2e 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 74 74  .static void att
22b2f 61 63 68 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  achFunc(.  sqlit
22b30 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
22b31 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73  ext,.  int NotUs
22b32 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  ed,.  sqlite3_va
22b33 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
22b34 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 72 63 20  int i;.  int rc 
22b35 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  = 0;.  sqlite3 *
22b36 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e  db = sqlite3_con
22b37 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63  text_db_handle(c
22b38 6f 6e 74 65 78 74 29 3b 0a 20 20 63 6f 6e 73 74  ontext);.  const
22b39 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20   char *zName;.  
22b3a 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
22b3b 65 3b 0a 20 20 44 62 20 2a 61 4e 65 77 3b 0a 20  e;.  Db *aNew;. 
22b3c 20 63 68 61 72 20 2a 7a 45 72 72 44 79 6e 20 3d   char *zErrDyn =
22b3d 20 30 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41   0;..  UNUSED_PA
22b3e 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
22b3f 3b 0a 0a 20 20 7a 46 69 6c 65 20 3d 20 28 63 6f  ;..  zFile = (co
22b40 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74  nst char *)sqlit
22b41 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
22b42 67 76 5b 30 5d 29 3b 0a 20 20 7a 4e 61 6d 65 20  gv[0]);.  zName 
22b43 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
22b44 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
22b45 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 69  xt(argv[1]);.  i
22b46 66 28 20 7a 46 69 6c 65 3d 3d 30 20 29 20 7a 46  f( zFile==0 ) zF
22b47 69 6c 65 20 3d 20 22 22 3b 0a 20 20 69 66 28 20  ile = "";.  if( 
22b48 7a 4e 61 6d 65 3d 3d 30 20 29 20 7a 4e 61 6d 65  zName==0 ) zName
22b49 20 3d 20 22 22 3b 0a 0a 20 20 2f 2a 20 43 68 65   = "";..  /* Che
22b4a 63 6b 20 66 6f 72 20 74 68 65 20 66 6f 6c 6c 6f  ck for the follo
22b4b 77 69 6e 67 20 65 72 72 6f 72 73 3a 0a 20 20 2a  wing errors:.  *
22b4c 2a 0a 20 20 2a 2a 20 20 20 20 20 2a 20 54 6f 6f  *.  **     * Too
22b4d 20 6d 61 6e 79 20 61 74 74 61 63 68 65 64 20 64   many attached d
22b4e 61 74 61 62 61 73 65 73 2c 0a 20 20 2a 2a 20 20  atabases,.  **  
22b4f 20 20 20 2a 20 54 72 61 6e 73 61 63 74 69 6f 6e     * Transaction
22b50 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 65 6e 0a   currently open.
22b51 20 20 2a 2a 20 20 20 20 20 2a 20 53 70 65 63 69    **     * Speci
22b52 66 69 65 64 20 64 61 74 61 62 61 73 65 20 6e 61  fied database na
22b53 6d 65 20 61 6c 72 65 61 64 79 20 62 65 69 6e 67  me already being
22b54 20 75 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66   used..  */.  if
22b55 28 20 64 62 2d 3e 6e 44 62 3e 3d 64 62 2d 3e 61  ( db->nDb>=db->a
22b56 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
22b57 49 54 5f 41 54 54 41 43 48 45 44 5d 2b 32 20 29  IT_ATTACHED]+2 )
22b58 7b 0a 20 20 20 20 7a 45 72 72 44 79 6e 20 3d 20  {.    zErrDyn = 
22b59 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
22b5a 62 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 61 74 74  b, "too many att
22b5b 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73 20  ached databases 
22b5c 2d 20 6d 61 78 20 25 64 22 2c 20 0a 20 20 20 20  - max %d", .    
22b5d 20 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c    db->aLimit[SQL
22b5e 49 54 45 5f 4c 49 4d 49 54 5f 41 54 54 41 43 48  ITE_LIMIT_ATTACH
22b5f 45 44 5d 0a 20 20 20 20 29 3b 0a 20 20 20 20 67  ED].    );.    g
22b60 6f 74 6f 20 61 74 74 61 63 68 5f 65 72 72 6f 72  oto attach_error
22b61 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 64 62 2d  ;.  }.  if( !db-
22b62 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20  >autoCommit ){. 
22b63 20 20 20 7a 45 72 72 44 79 6e 20 3d 20 73 71 6c     zErrDyn = sql
22b64 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
22b65 22 63 61 6e 6e 6f 74 20 41 54 54 41 43 48 20 64  "cannot ATTACH d
22b66 61 74 61 62 61 73 65 20 77 69 74 68 69 6e 20 74  atabase within t
22b67 72 61 6e 73 61 63 74 69 6f 6e 22 29 3b 0a 20 20  ransaction");.  
22b68 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 72    goto attach_er
22b69 72 6f 72 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69  ror;.  }.  for(i
22b6a 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69  =0; i<db->nDb; i
22b6b 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a  ++){.    char *z
22b6c 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e   = db->aDb[i].zN
22b6d 61 6d 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ame;.    assert(
22b6e 20 7a 20 26 26 20 7a 4e 61 6d 65 20 29 3b 0a 20   z && zName );. 
22b6f 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74     if( sqlite3St
22b70 72 49 43 6d 70 28 7a 2c 20 7a 4e 61 6d 65 29 3d  rICmp(z, zName)=
22b71 3d 30 20 29 7b 0a 20 20 20 20 20 20 7a 45 72 72  =0 ){.      zErr
22b72 44 79 6e 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  Dyn = sqlite3MPr
22b73 69 6e 74 66 28 64 62 2c 20 22 64 61 74 61 62 61  intf(db, "databa
22b74 73 65 20 25 73 20 69 73 20 61 6c 72 65 61 64 79  se %s is already
22b75 20 69 6e 20 75 73 65 22 2c 20 7a 4e 61 6d 65 29   in use", zName)
22b76 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 74 74  ;.      goto att
22b77 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d  ach_error;.    }
22b78 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63  .  }..  /* Alloc
22b79 61 74 65 20 74 68 65 20 6e 65 77 20 65 6e 74 72  ate the new entr
22b7a 79 20 69 6e 20 74 68 65 20 64 62 2d 3e 61 44 62  y in the db->aDb
22b7b 5b 5d 20 61 72 72 61 79 20 61 6e 64 20 69 6e 69  [] array and ini
22b7c 74 69 61 6c 69 73 65 20 74 68 65 20 73 63 68 65  tialise the sche
22b7d 6d 61 0a 20 20 2a 2a 20 68 61 73 68 20 74 61 62  ma.  ** hash tab
22b7e 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  les..  */.  if( 
22b7f 64 62 2d 3e 61 44 62 3d 3d 64 62 2d 3e 61 44 62  db->aDb==db->aDb
22b80 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20 61 4e  Static ){.    aN
22b81 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  ew = sqlite3DbMa
22b82 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65  llocRaw(db, size
22b83 6f 66 28 64 62 2d 3e 61 44 62 5b 30 5d 29 2a 33  of(db->aDb[0])*3
22b84 20 29 3b 0a 20 20 20 20 69 66 28 20 61 4e 65 77   );.    if( aNew
22b85 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
22b86 20 20 6d 65 6d 63 70 79 28 61 4e 65 77 2c 20 64    memcpy(aNew, d
22b87 62 2d 3e 61 44 62 2c 20 73 69 7a 65 6f 66 28 64  b->aDb, sizeof(d
22b88 62 2d 3e 61 44 62 5b 30 5d 29 2a 32 29 3b 0a 20  b->aDb[0])*2);. 
22b89 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 4e 65 77   }else{.    aNew
22b8a 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c   = sqlite3DbReal
22b8b 6c 6f 63 28 64 62 2c 20 64 62 2d 3e 61 44 62 2c  loc(db, db->aDb,
22b8c 20 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b   sizeof(db->aDb[
22b8d 30 5d 29 2a 28 64 62 2d 3e 6e 44 62 2b 31 29 20  0])*(db->nDb+1) 
22b8e 29 3b 0a 20 20 20 20 69 66 28 20 61 4e 65 77 3d  );.    if( aNew=
22b8f 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7d  =0 ) return;.  }
22b90 0a 20 20 64 62 2d 3e 61 44 62 20 3d 20 61 4e 65  .  db->aDb = aNe
22b91 77 3b 0a 20 20 61 4e 65 77 20 3d 20 26 64 62 2d  w;.  aNew = &db-
22b92 3e 61 44 62 5b 64 62 2d 3e 6e 44 62 5d 3b 0a 20  >aDb[db->nDb];. 
22b93 20 6d 65 6d 73 65 74 28 61 4e 65 77 2c 20 30 2c   memset(aNew, 0,
22b94 20 73 69 7a 65 6f 66 28 2a 61 4e 65 77 29 29 3b   sizeof(*aNew));
22b95 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20  ..  /* Open the 
22b96 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49  database file. I
22b97 66 20 74 68 65 20 62 74 72 65 65 20 69 73 20 73  f the btree is s
22b98 75 63 63 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e  uccessfully open
22b99 65 64 2c 20 75 73 65 0a 20 20 2a 2a 20 69 74 20  ed, use.  ** it 
22b9a 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 64 61  to obtain the da
22b9b 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20 41  tabase schema. A
22b9c 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65  t this point the
22b9d 20 73 63 68 65 6d 61 20 6d 61 79 0a 20 20 2a 2a   schema may.  **
22b9e 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 69   or may not be i
22b9f 6e 69 74 69 61 6c 69 73 65 64 2e 0a 20 20 2a 2f  nitialised..  */
22ba0 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42  .  rc = sqlite3B
22ba1 74 72 65 65 46 61 63 74 6f 72 79 28 64 62 2c 20  treeFactory(db, 
22ba2 7a 46 69 6c 65 2c 20 30 2c 20 53 51 4c 49 54 45  zFile, 0, SQLITE
22ba3 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53  _DEFAULT_CACHE_S
22ba4 49 5a 45 2c 0a 20 20 20 20 20 20 20 20 20 20 20  IZE,.           
22ba5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22ba6 64 62 2d 3e 6f 70 65 6e 46 6c 61 67 73 20 7c 20  db->openFlags | 
22ba7 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
22ba8 5f 44 42 2c 0a 20 20 20 20 20 20 20 20 20 20 20  _DB,.           
22ba9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22baa 26 61 4e 65 77 2d 3e 70 42 74 29 3b 0a 20 20 64  &aNew->pBt);.  d
22bab 62 2d 3e 6e 44 62 2b 2b 3b 0a 20 20 69 66 28 20  b->nDb++;.  if( 
22bac 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54  rc==SQLITE_CONST
22bad 52 41 49 4e 54 20 29 7b 0a 20 20 20 20 72 63 20  RAINT ){.    rc 
22bae 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
22baf 20 20 20 20 7a 45 72 72 44 79 6e 20 3d 20 73 71      zErrDyn = sq
22bb0 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
22bb1 20 22 64 61 74 61 62 61 73 65 20 69 73 20 61 6c   "database is al
22bb2 72 65 61 64 79 20 61 74 74 61 63 68 65 64 22 29  ready attached")
22bb3 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63  ;.  }else if( rc
22bb4 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
22bb5 20 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72     Pager *pPager
22bb6 3b 0a 20 20 20 20 61 4e 65 77 2d 3e 70 53 63 68  ;.    aNew->pSch
22bb7 65 6d 61 20 3d 20 73 71 6c 69 74 65 33 53 63 68  ema = sqlite3Sch
22bb8 65 6d 61 47 65 74 28 64 62 2c 20 61 4e 65 77 2d  emaGet(db, aNew-
22bb9 3e 70 42 74 29 3b 0a 20 20 20 20 69 66 28 20 21  >pBt);.    if( !
22bba 61 4e 65 77 2d 3e 70 53 63 68 65 6d 61 20 29 7b  aNew->pSchema ){
22bbb 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
22bbc 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65  TE_NOMEM;.    }e
22bbd 6c 73 65 20 69 66 28 20 61 4e 65 77 2d 3e 70 53  lse if( aNew->pS
22bbe 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d  chema->file_form
22bbf 61 74 20 26 26 20 61 4e 65 77 2d 3e 70 53 63 68  at && aNew->pSch
22bc0 65 6d 61 2d 3e 65 6e 63 21 3d 45 4e 43 28 64 62  ema->enc!=ENC(db
22bc1 29 20 29 7b 0a 20 20 20 20 20 20 7a 45 72 72 44  ) ){.      zErrD
22bc2 79 6e 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  yn = sqlite3MPri
22bc3 6e 74 66 28 64 62 2c 20 0a 20 20 20 20 20 20 20  ntf(db, .       
22bc4 20 22 61 74 74 61 63 68 65 64 20 64 61 74 61 62   "attached datab
22bc5 61 73 65 73 20 6d 75 73 74 20 75 73 65 20 74 68  ases must use th
22bc6 65 20 73 61 6d 65 20 74 65 78 74 20 65 6e 63 6f  e same text enco
22bc7 64 69 6e 67 20 61 73 20 6d 61 69 6e 20 64 61 74  ding as main dat
22bc8 61 62 61 73 65 22 29 3b 0a 20 20 20 20 20 20 72  abase");.      r
22bc9 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
22bca 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67  ;.    }.    pPag
22bcb 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  er = sqlite3Btre
22bcc 65 50 61 67 65 72 28 61 4e 65 77 2d 3e 70 42 74  ePager(aNew->pBt
22bcd 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61  );.    sqlite3Pa
22bce 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 70  gerLockingMode(p
22bcf 50 61 67 65 72 2c 20 64 62 2d 3e 64 66 6c 74 4c  Pager, db->dfltL
22bd0 6f 63 6b 4d 6f 64 65 29 3b 0a 20 20 20 20 73 71  ockMode);.    sq
22bd1 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61  lite3PagerJourna
22bd2 6c 4d 6f 64 65 28 70 50 61 67 65 72 2c 20 64 62  lMode(pPager, db
22bd3 2d 3e 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64  ->dfltJournalMod
22bd4 65 29 3b 0a 20 20 7d 0a 20 20 61 4e 65 77 2d 3e  e);.  }.  aNew->
22bd5 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44  zName = sqlite3D
22bd6 62 53 74 72 44 75 70 28 64 62 2c 20 7a 4e 61 6d  bStrDup(db, zNam
22bd7 65 29 3b 0a 20 20 61 4e 65 77 2d 3e 73 61 66 65  e);.  aNew->safe
22bd8 74 79 5f 6c 65 76 65 6c 20 3d 20 33 3b 0a 0a 23  ty_level = 3;..#
22bd9 69 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f  if SQLITE_HAS_CO
22bda 44 45 43 0a 20 20 7b 0a 20 20 20 20 65 78 74 65  DEC.  {.    exte
22bdb 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 43 6f  rn int sqlite3Co
22bdc 64 65 63 41 74 74 61 63 68 28 73 71 6c 69 74 65  decAttach(sqlite
22bdd 33 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 76  3*, int, const v
22bde 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 20 20 20 20  oid*, int);.    
22bdf 65 78 74 65 72 6e 20 76 6f 69 64 20 73 71 6c 69  extern void sqli
22be0 74 65 33 43 6f 64 65 63 47 65 74 4b 65 79 28 73  te3CodecGetKey(s
22be1 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 76 6f  qlite3*, int, vo
22be2 69 64 2a 2a 2c 20 69 6e 74 2a 29 3b 0a 20 20 20  id**, int*);.   
22be3 20 69 6e 74 20 6e 4b 65 79 3b 0a 20 20 20 20 63   int nKey;.    c
22be4 68 61 72 20 2a 7a 4b 65 79 3b 0a 20 20 20 20 69  har *zKey;.    i
22be5 6e 74 20 74 20 3d 20 73 71 6c 69 74 65 33 5f 76  nt t = sqlite3_v
22be6 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 32  alue_type(argv[2
22be7 5d 29 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20  ]);.    switch( 
22be8 74 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20  t ){.      case 
22be9 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 0a  SQLITE_INTEGER:.
22bea 20 20 20 20 20 20 63 61 73 65 20 53 51 4c 49 54        case SQLIT
22beb 45 5f 46 4c 4f 41 54 3a 0a 20 20 20 20 20 20 20  E_FLOAT:.       
22bec 20 7a 45 72 72 44 79 6e 20 3d 20 73 71 6c 69 74   zErrDyn = sqlit
22bed 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 22  e3DbStrDup(db, "
22bee 49 6e 76 61 6c 69 64 20 6b 65 79 20 76 61 6c 75  Invalid key valu
22bef 65 22 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  e");.        rc 
22bf0 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
22bf1 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
22bf2 20 20 20 20 20 20 20 0a 20 20 20 20 20 20 63 61         .      ca
22bf3 73 65 20 53 51 4c 49 54 45 5f 54 45 58 54 3a 0a  se SQLITE_TEXT:.
22bf4 20 20 20 20 20 20 63 61 73 65 20 53 51 4c 49 54        case SQLIT
22bf5 45 5f 42 4c 4f 42 3a 0a 20 20 20 20 20 20 20 20  E_BLOB:.        
22bf6 6e 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 5f 76  nKey = sqlite3_v
22bf7 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b  alue_bytes(argv[
22bf8 32 5d 29 3b 0a 20 20 20 20 20 20 20 20 7a 4b 65  2]);.        zKe
22bf9 79 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69  y = (char *)sqli
22bfa 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61  te3_value_blob(a
22bfb 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 20 20 20  rgv[2]);.       
22bfc 20 73 71 6c 69 74 65 33 43 6f 64 65 63 41 74 74   sqlite3CodecAtt
22bfd 61 63 68 28 64 62 2c 20 64 62 2d 3e 6e 44 62 2d  ach(db, db->nDb-
22bfe 31 2c 20 7a 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a  1, zKey, nKey);.
22bff 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a          break;..
22c00 20 20 20 20 20 20 63 61 73 65 20 53 51 4c 49 54        case SQLIT
22c01 45 5f 4e 55 4c 4c 3a 0a 20 20 20 20 20 20 20 20  E_NULL:.        
22c02 2f 2a 20 4e 6f 20 6b 65 79 20 73 70 65 63 69 66  /* No key specif
22c03 69 65 64 2e 20 20 55 73 65 20 74 68 65 20 6b 65  ied.  Use the ke
22c04 79 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20  y from the main 
22c05 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20 20  database */.    
22c06 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 63      sqlite3Codec
22c07 47 65 74 4b 65 79 28 64 62 2c 20 30 2c 20 28 76  GetKey(db, 0, (v
22c08 6f 69 64 2a 2a 29 26 7a 4b 65 79 2c 20 26 6e 4b  oid**)&zKey, &nK
22c09 65 79 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ey);.        sql
22c0a 69 74 65 33 43 6f 64 65 63 41 74 74 61 63 68 28  ite3CodecAttach(
22c0b 64 62 2c 20 64 62 2d 3e 6e 44 62 2d 31 2c 20 7a  db, db->nDb-1, z
22c0c 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 20 20  Key, nKey);.    
22c0d 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
22c0e 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
22c0f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 77 61  * If the file wa
22c10 73 20 6f 70 65 6e 65 64 20 73 75 63 63 65 73 73  s opened success
22c11 66 75 6c 6c 79 2c 20 72 65 61 64 20 74 68 65 20  fully, read the 
22c12 73 63 68 65 6d 61 20 66 6f 72 20 74 68 65 20 6e  schema for the n
22c13 65 77 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a  ew database..  *
22c14 2a 20 49 66 20 74 68 69 73 20 66 61 69 6c 73 2c  * If this fails,
22c15 20 6f 72 20 69 66 20 6f 70 65 6e 69 6e 67 20 74   or if opening t
22c16 68 65 20 66 69 6c 65 20 66 61 69 6c 65 64 2c 20  he file failed, 
22c17 74 68 65 6e 20 63 6c 6f 73 65 20 74 68 65 20 66  then close the f
22c18 69 6c 65 20 61 6e 64 20 0a 20 20 2a 2a 20 72 65  ile and .  ** re
22c19 6d 6f 76 65 20 74 68 65 20 65 6e 74 72 79 20 66  move the entry f
22c1a 72 6f 6d 20 74 68 65 20 64 62 2d 3e 61 44 62 5b  rom the db->aDb[
22c1b 5d 20 61 72 72 61 79 2e 20 69 2e 65 2e 20 70 75  ] array. i.e. pu
22c1c 74 20 65 76 65 72 79 74 68 69 6e 67 20 62 61 63  t everything bac
22c1d 6b 20 74 68 65 20 77 61 79 0a 20 20 2a 2a 20 77  k the way.  ** w
22c1e 65 20 66 6f 75 6e 64 20 69 74 2e 0a 20 20 2a 2f  e found it..  */
22c1f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
22c20 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 28 76 6f 69  E_OK ){.    (voi
22c21 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  d)sqlite3SafetyO
22c22 6e 28 64 62 29 3b 0a 20 20 20 20 73 71 6c 69 74  n(db);.    sqlit
22c23 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28  e3BtreeEnterAll(
22c24 64 62 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  db);.    rc = sq
22c25 6c 69 74 65 33 49 6e 69 74 28 64 62 2c 20 26 7a  lite3Init(db, &z
22c26 45 72 72 44 79 6e 29 3b 0a 20 20 20 20 73 71 6c  ErrDyn);.    sql
22c27 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c  ite3BtreeLeaveAl
22c28 6c 28 64 62 29 3b 0a 20 20 20 20 28 76 6f 69 64  l(db);.    (void
22c29 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66  )sqlite3SafetyOf
22c2a 66 28 64 62 29 3b 0a 20 20 7d 0a 20 20 69 66 28  f(db);.  }.  if(
22c2b 20 72 63 20 29 7b 0a 20 20 20 20 69 6e 74 20 69   rc ){.    int i
22c2c 44 62 20 3d 20 64 62 2d 3e 6e 44 62 20 2d 20 31  Db = db->nDb - 1
22c2d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 44  ;.    assert( iD
22c2e 62 3e 3d 32 20 29 3b 0a 20 20 20 20 69 66 28 20  b>=2 );.    if( 
22c2f 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74  db->aDb[iDb].pBt
22c30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
22c31 33 42 74 72 65 65 43 6c 6f 73 65 28 64 62 2d 3e  3BtreeClose(db->
22c32 61 44 62 5b 69 44 62 5d 2e 70 42 74 29 3b 0a 20  aDb[iDb].pBt);. 
22c33 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62       db->aDb[iDb
22c34 5d 2e 70 42 74 20 3d 20 30 3b 0a 20 20 20 20 20  ].pBt = 0;.     
22c35 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53   db->aDb[iDb].pS
22c36 63 68 65 6d 61 20 3d 20 30 3b 0a 20 20 20 20 7d  chema = 0;.    }
22c37 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 65  .    sqlite3Rese
22c38 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28  tInternalSchema(
22c39 64 62 2c 20 30 29 3b 0a 20 20 20 20 64 62 2d 3e  db, 0);.    db->
22c3a 6e 44 62 20 3d 20 69 44 62 3b 0a 20 20 20 20 69  nDb = iDb;.    i
22c3b 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  f( rc==SQLITE_NO
22c3c 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54  MEM || rc==SQLIT
22c3d 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b  E_IOERR_NOMEM ){
22c3e 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  .      db->mallo
22c3f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
22c40 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
22c41 28 64 62 2c 20 7a 45 72 72 44 79 6e 29 3b 0a 20  (db, zErrDyn);. 
22c42 20 20 20 20 20 7a 45 72 72 44 79 6e 20 3d 20 73       zErrDyn = s
22c43 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
22c44 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79  , "out of memory
22c45 22 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  ");.    }else if
22c46 28 20 7a 45 72 72 44 79 6e 3d 3d 30 20 29 7b 0a  ( zErrDyn==0 ){.
22c47 20 20 20 20 20 20 7a 45 72 72 44 79 6e 20 3d 20        zErrDyn = 
22c48 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
22c49 62 2c 20 22 75 6e 61 62 6c 65 20 74 6f 20 6f 70  b, "unable to op
22c4a 65 6e 20 64 61 74 61 62 61 73 65 3a 20 25 73 22  en database: %s"
22c4b 2c 20 7a 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a  , zFile);.    }.
22c4c 20 20 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f      goto attach_
22c4d 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 0a 20 20  error;.  }.  .  
22c4e 72 65 74 75 72 6e 3b 0a 0a 61 74 74 61 63 68 5f  return;..attach_
22c4f 65 72 72 6f 72 3a 0a 20 20 2f 2a 20 52 65 74 75  error:.  /* Retu
22c50 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 66 20 77  rn an error if w
22c51 65 20 67 65 74 20 68 65 72 65 20 2a 2f 0a 20 20  e get here */.  
22c52 69 66 28 20 7a 45 72 72 44 79 6e 20 29 7b 0a 20  if( zErrDyn ){. 
22c53 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
22c54 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c  t_error(context,
22c55 20 7a 45 72 72 44 79 6e 2c 20 2d 31 29 3b 0a 20   zErrDyn, -1);. 
22c56 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
22c57 28 64 62 2c 20 7a 45 72 72 44 79 6e 29 3b 0a 20  (db, zErrDyn);. 
22c58 20 7d 0a 20 20 69 66 28 20 72 63 20 29 20 73 71   }.  if( rc ) sq
22c59 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
22c5a 6f 72 5f 63 6f 64 65 28 63 6f 6e 74 65 78 74 2c  or_code(context,
22c5b 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41   rc);.}../*.** A
22c5c 6e 20 53 51 4c 20 75 73 65 72 2d 66 75 6e 63 74  n SQL user-funct
22c5d 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 74  ion registered t
22c5e 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b 20 6f 66  o do the work of
22c5f 20 61 6e 20 44 45 54 41 43 48 20 73 74 61 74 65   an DETACH state
22c60 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a 20 74 68 72  ment. The.** thr
22c61 65 65 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  ee arguments to 
22c62 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d  the function com
22c63 65 20 64 69 72 65 63 74 6c 79 20 66 72 6f 6d 20  e directly from 
22c64 61 20 64 65 74 61 63 68 20 73 74 61 74 65 6d 65  a detach stateme
22c65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 45  nt:.**.**     DE
22c66 54 41 43 48 20 44 41 54 41 42 41 53 45 20 78 0a  TACH DATABASE x.
22c67 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54  **.**     SELECT
22c68 20 73 71 6c 69 74 65 5f 64 65 74 61 63 68 28 78   sqlite_detach(x
22c69 29 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ).*/.static void
22c6a 20 64 65 74 61 63 68 46 75 6e 63 28 0a 20 20 73   detachFunc(.  s
22c6b 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
22c6c 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e  context,.  int N
22c6d 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65  otUsed,.  sqlite
22c6e 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
22c6f 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  {.  const char *
22c70 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 74 20 63  zName = (const c
22c71 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61  har *)sqlite3_va
22c72 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
22c73 29 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  );.  sqlite3 *db
22c74 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65   = sqlite3_conte
22c75 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e  xt_db_handle(con
22c76 74 65 78 74 29 3b 0a 20 20 69 6e 74 20 69 3b 0a  text);.  int i;.
22c77 20 20 44 62 20 2a 70 44 62 20 3d 20 30 3b 0a 20    Db *pDb = 0;. 
22c78 20 63 68 61 72 20 7a 45 72 72 5b 31 32 38 5d 3b   char zErr[128];
22c79 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
22c7a 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 0a  ETER(NotUsed);..
22c7b 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29    if( zName==0 )
22c7c 20 7a 4e 61 6d 65 20 3d 20 22 22 3b 0a 20 20 66   zName = "";.  f
22c7d 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44  or(i=0; i<db->nD
22c7e 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 44 62  b; i++){.    pDb
22c7f 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a   = &db->aDb[i];.
22c80 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70 42 74      if( pDb->pBt
22c81 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ==0 ) continue;.
22c82 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
22c83 74 72 49 43 6d 70 28 70 44 62 2d 3e 7a 4e 61 6d  trICmp(pDb->zNam
22c84 65 2c 20 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 62  e, zName)==0 ) b
22c85 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  reak;.  }..  if(
22c86 20 69 3e 3d 64 62 2d 3e 6e 44 62 20 29 7b 0a 20   i>=db->nDb ){. 
22c87 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
22c88 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29  ntf(sizeof(zErr)
22c89 2c 7a 45 72 72 2c 20 22 6e 6f 20 73 75 63 68 20  ,zErr, "no such 
22c8a 64 61 74 61 62 61 73 65 3a 20 25 73 22 2c 20 7a  database: %s", z
22c8b 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20  Name);.    goto 
22c8c 64 65 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20  detach_error;.  
22c8d 7d 0a 20 20 69 66 28 20 69 3c 32 20 29 7b 0a 20  }.  if( i<2 ){. 
22c8e 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
22c8f 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29  ntf(sizeof(zErr)
22c90 2c 7a 45 72 72 2c 20 22 63 61 6e 6e 6f 74 20 64  ,zErr, "cannot d
22c91 65 74 61 63 68 20 64 61 74 61 62 61 73 65 20 25  etach database %
22c92 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  s", zName);.    
22c93 67 6f 74 6f 20 64 65 74 61 63 68 5f 65 72 72 6f  goto detach_erro
22c94 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 64 62  r;.  }.  if( !db
22c95 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a  ->autoCommit ){.
22c96 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
22c97 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72  intf(sizeof(zErr
22c98 29 2c 20 7a 45 72 72 2c 0a 20 20 20 20 20 20 20  ), zErr,.       
22c99 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 63                "c
22c9a 61 6e 6e 6f 74 20 44 45 54 41 43 48 20 64 61 74  annot DETACH dat
22c9b 61 62 61 73 65 20 77 69 74 68 69 6e 20 74 72 61  abase within tra
22c9c 6e 73 61 63 74 69 6f 6e 22 29 3b 0a 20 20 20 20  nsaction");.    
22c9d 67 6f 74 6f 20 64 65 74 61 63 68 5f 65 72 72 6f  goto detach_erro
22c9e 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c  r;.  }.  if( sql
22c9f 69 74 65 33 42 74 72 65 65 49 73 49 6e 52 65 61  ite3BtreeIsInRea
22ca0 64 54 72 61 6e 73 28 70 44 62 2d 3e 70 42 74 29  dTrans(pDb->pBt)
22ca1 20 7c 7c 20 73 71 6c 69 74 65 33 42 74 72 65 65   || sqlite3Btree
22ca2 49 73 49 6e 42 61 63 6b 75 70 28 70 44 62 2d 3e  IsInBackup(pDb->
22ca3 70 42 74 29 20 29 7b 0a 20 20 20 20 73 71 6c 69  pBt) ){.    sqli
22ca4 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
22ca5 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72 72 2c 20  eof(zErr),zErr, 
22ca6 22 64 61 74 61 62 61 73 65 20 25 73 20 69 73 20  "database %s is 
22ca7 6c 6f 63 6b 65 64 22 2c 20 7a 4e 61 6d 65 29 3b  locked", zName);
22ca8 0a 20 20 20 20 67 6f 74 6f 20 64 65 74 61 63 68  .    goto detach
22ca9 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20 73  _error;.  }..  s
22caa 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65  qlite3BtreeClose
22cab 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 70 44  (pDb->pBt);.  pD
22cac 62 2d 3e 70 42 74 20 3d 20 30 3b 0a 20 20 70 44  b->pBt = 0;.  pD
22cad 62 2d 3e 70 53 63 68 65 6d 61 20 3d 20 30 3b 0a  b->pSchema = 0;.
22cae 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e    sqlite3ResetIn
22caf 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c  ternalSchema(db,
22cb0 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 0a   0);.  return;..
22cb1 64 65 74 61 63 68 5f 65 72 72 6f 72 3a 0a 20 20  detach_error:.  
22cb2 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
22cb3 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45  rror(context, zE
22cb4 72 72 2c 20 2d 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  rr, -1);.}../*.*
22cb5 2a 20 54 68 69 73 20 70 72 6f 63 65 64 75 72 65  * This procedure
22cb6 20 67 65 6e 65 72 61 74 65 73 20 56 44 42 45 20   generates VDBE 
22cb7 63 6f 64 65 20 66 6f 72 20 61 20 73 69 6e 67 6c  code for a singl
22cb8 65 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20  e invocation of 
22cb9 65 69 74 68 65 72 20 74 68 65 0a 2a 2a 20 73 71  either the.** sq
22cba 6c 69 74 65 5f 64 65 74 61 63 68 28 29 20 6f 72  lite_detach() or
22cbb 20 73 71 6c 69 74 65 5f 61 74 74 61 63 68 28 29   sqlite_attach()
22cbc 20 53 51 4c 20 75 73 65 72 20 66 75 6e 63 74 69   SQL user functi
22cbd 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ons..*/.static v
22cbe 6f 69 64 20 63 6f 64 65 41 74 74 61 63 68 28 0a  oid codeAttach(.
22cbf 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
22cc0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
22cc1 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  rser context */.
22cc2 20 20 69 6e 74 20 74 79 70 65 2c 20 20 20 20 20    int type,     
22cc3 20 20 20 20 20 20 20 2f 2a 20 45 69 74 68 65 72         /* Either
22cc4 20 53 51 4c 49 54 45 5f 41 54 54 41 43 48 20 6f   SQLITE_ATTACH o
22cc5 72 20 53 51 4c 49 54 45 5f 44 45 54 41 43 48 20  r SQLITE_DETACH 
22cc6 2a 2f 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 46  */.  FuncDef *pF
22cc7 75 6e 63 2c 20 20 20 20 20 20 2f 2a 20 46 75 6e  unc,      /* Fun
22cc8 63 44 65 66 20 77 72 61 70 70 65 72 20 66 6f 72  cDef wrapper for
22cc9 20 64 65 74 61 63 68 46 75 6e 63 28 29 20 6f 72   detachFunc() or
22cca 20 61 74 74 61 63 68 46 75 6e 63 28 29 20 2a 2f   attachFunc() */
22ccb 0a 20 20 45 78 70 72 20 2a 70 41 75 74 68 41 72  .  Expr *pAuthAr
22ccc 67 2c 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65  g,      /* Expre
22ccd 73 73 69 6f 6e 20 74 6f 20 70 61 73 73 20 74 6f  ssion to pass to
22cce 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63   authorization c
22ccf 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 45 78 70  allback */.  Exp
22cd0 72 20 2a 70 46 69 6c 65 6e 61 6d 65 2c 20 20 20  r *pFilename,   
22cd1 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74    /* Name of dat
22cd2 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20  abase file */.  
22cd3 45 78 70 72 20 2a 70 44 62 6e 61 6d 65 2c 20 20  Expr *pDbname,  
22cd4 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
22cd5 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20  the database to 
22cd6 75 73 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20 2a  use internally *
22cd7 2f 0a 20 20 45 78 70 72 20 2a 70 4b 65 79 20 20  /.  Expr *pKey  
22cd8 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
22cd9 62 61 73 65 20 6b 65 79 20 66 6f 72 20 65 6e 63  base key for enc
22cda 72 79 70 74 69 6f 6e 20 65 78 74 65 6e 73 69 6f  ryption extensio
22cdb 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  n */.){.  int rc
22cdc 3b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  ;.  NameContext 
22cdd 73 4e 61 6d 65 3b 0a 20 20 56 64 62 65 20 2a 76  sName;.  Vdbe *v
22cde 3b 0a 20 20 73 71 6c 69 74 65 33 2a 20 64 62 20  ;.  sqlite3* db 
22cdf 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
22ce0 69 6e 74 20 72 65 67 41 72 67 73 3b 0a 0a 20 20  int regArgs;..  
22ce1 6d 65 6d 73 65 74 28 26 73 4e 61 6d 65 2c 20 30  memset(&sName, 0
22ce2 2c 20 73 69 7a 65 6f 66 28 4e 61 6d 65 43 6f 6e  , sizeof(NameCon
22ce3 74 65 78 74 29 29 3b 0a 20 20 73 4e 61 6d 65 2e  text));.  sName.
22ce4 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b  pParse = pParse;
22ce5 0a 0a 20 20 69 66 28 20 0a 20 20 20 20 20 20 53  ..  if( .      S
22ce6 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20  QLITE_OK!=(rc = 
22ce7 72 65 73 6f 6c 76 65 41 74 74 61 63 68 45 78 70  resolveAttachExp
22ce8 72 28 26 73 4e 61 6d 65 2c 20 70 46 69 6c 65 6e  r(&sName, pFilen
22ce9 61 6d 65 29 29 20 7c 7c 0a 20 20 20 20 20 20 53  ame)) ||.      S
22cea 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20  QLITE_OK!=(rc = 
22ceb 72 65 73 6f 6c 76 65 41 74 74 61 63 68 45 78 70  resolveAttachExp
22cec 72 28 26 73 4e 61 6d 65 2c 20 70 44 62 6e 61 6d  r(&sName, pDbnam
22ced 65 29 29 20 7c 7c 0a 20 20 20 20 20 20 53 51 4c  e)) ||.      SQL
22cee 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65  ITE_OK!=(rc = re
22cef 73 6f 6c 76 65 41 74 74 61 63 68 45 78 70 72 28  solveAttachExpr(
22cf0 26 73 4e 61 6d 65 2c 20 70 4b 65 79 29 29 0a 20  &sName, pKey)). 
22cf1 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e   ){.    pParse->
22cf2 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f  nErr++;.    goto
22cf3 20 61 74 74 61 63 68 5f 65 6e 64 3b 0a 20 20 7d   attach_end;.  }
22cf4 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
22cf5 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
22cf6 49 4f 4e 0a 20 20 69 66 28 20 70 41 75 74 68 41  ION.  if( pAuthA
22cf7 72 67 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a  rg ){.    char *
22cf8 7a 41 75 74 68 41 72 67 20 3d 20 70 41 75 74 68  zAuthArg = pAuth
22cf9 41 72 67 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20  Arg->u.zToken;. 
22cfa 20 20 20 69 66 28 20 4e 45 56 45 52 28 7a 41 75     if( NEVER(zAu
22cfb 74 68 41 72 67 3d 3d 30 29 20 29 7b 0a 20 20 20  thArg==0) ){.   
22cfc 20 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65     goto attach_e
22cfd 6e 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63  nd;.    }.    rc
22cfe 20 3d 20 73 71 6c 69 74 65 33 41 75 74 68 43 68   = sqlite3AuthCh
22cff 65 63 6b 28 70 50 61 72 73 65 2c 20 74 79 70 65  eck(pParse, type
22d00 2c 20 7a 41 75 74 68 41 72 67 2c 20 30 2c 20 30  , zAuthArg, 0, 0
22d01 29 3b 0a 20 20 20 20 69 66 28 72 63 21 3d 53 51  );.    if(rc!=SQ
22d02 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
22d03 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 6e 64   goto attach_end
22d04 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
22d05 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
22d06 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 20  T_AUTHORIZATION 
22d07 2a 2f 0a 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74  */...  v = sqlit
22d08 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
22d09 29 3b 0a 20 20 72 65 67 41 72 67 73 20 3d 20 73  );.  regArgs = s
22d0a 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e  qlite3GetTempRan
22d0b 67 65 28 70 50 61 72 73 65 2c 20 34 29 3b 0a 20  ge(pParse, 4);. 
22d0c 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
22d0d 28 70 50 61 72 73 65 2c 20 70 46 69 6c 65 6e 61  (pParse, pFilena
22d0e 6d 65 2c 20 72 65 67 41 72 67 73 29 3b 0a 20 20  me, regArgs);.  
22d0f 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
22d10 70 50 61 72 73 65 2c 20 70 44 62 6e 61 6d 65 2c  pParse, pDbname,
22d11 20 72 65 67 41 72 67 73 2b 31 29 3b 0a 20 20 73   regArgs+1);.  s
22d12 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
22d13 50 61 72 73 65 2c 20 70 4b 65 79 2c 20 72 65 67  Parse, pKey, reg
22d14 41 72 67 73 2b 32 29 3b 0a 0a 20 20 61 73 73 65  Args+2);..  asse
22d15 72 74 28 20 76 20 7c 7c 20 64 62 2d 3e 6d 61 6c  rt( v || db->mal
22d16 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69  locFailed );.  i
22d17 66 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69  f( v ){.    sqli
22d18 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
22d19 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 2c 20 30 2c   OP_Function, 0,
22d1a 20 72 65 67 41 72 67 73 2b 33 2d 70 46 75 6e 63   regArgs+3-pFunc
22d1b 2d 3e 6e 41 72 67 2c 20 72 65 67 41 72 67 73 2b  ->nArg, regArgs+
22d1c 33 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  3);.    assert( 
22d1d 70 46 75 6e 63 2d 3e 6e 41 72 67 3d 3d 2d 31 20  pFunc->nArg==-1 
22d1e 7c 7c 20 28 70 46 75 6e 63 2d 3e 6e 41 72 67 26  || (pFunc->nArg&
22d1f 30 78 66 66 29 3d 3d 70 46 75 6e 63 2d 3e 6e 41  0xff)==pFunc->nA
22d20 72 67 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  rg );.    sqlite
22d21 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
22d22 20 28 75 38 29 28 70 46 75 6e 63 2d 3e 6e 41 72   (u8)(pFunc->nAr
22d23 67 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  g));.    sqlite3
22d24 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
22d25 2d 31 2c 20 28 63 68 61 72 20 2a 29 70 46 75 6e  -1, (char *)pFun
22d26 63 2c 20 50 34 5f 46 55 4e 43 44 45 46 29 3b 0a  c, P4_FUNCDEF);.
22d27 0a 20 20 20 20 2f 2a 20 43 6f 64 65 20 61 6e 20  .    /* Code an 
22d28 4f 50 5f 45 78 70 69 72 65 2e 20 46 6f 72 20 61  OP_Expire. For a
22d29 6e 20 41 54 54 41 43 48 20 73 74 61 74 65 6d 65  n ATTACH stateme
22d2a 6e 74 2c 20 73 65 74 20 50 31 20 74 6f 20 74 72  nt, set P1 to tr
22d2b 75 65 20 28 65 78 70 69 72 65 20 74 68 69 73 0a  ue (expire this.
22d2c 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74      ** statement
22d2d 20 6f 6e 6c 79 29 2e 20 46 6f 72 20 44 45 54 41   only). For DETA
22d2e 43 48 2c 20 73 65 74 20 69 74 20 74 6f 20 66 61  CH, set it to fa
22d2f 6c 73 65 20 28 65 78 70 69 72 65 20 61 6c 6c 20  lse (expire all 
22d30 65 78 69 73 74 69 6e 67 0a 20 20 20 20 2a 2a 20  existing.    ** 
22d31 73 74 61 74 65 6d 65 6e 74 73 29 2e 0a 20 20 20  statements)..   
22d32 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56   */.    sqlite3V
22d33 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
22d34 45 78 70 69 72 65 2c 20 28 74 79 70 65 3d 3d 53  Expire, (type==S
22d35 51 4c 49 54 45 5f 41 54 54 41 43 48 29 29 3b 0a  QLITE_ATTACH));.
22d36 20 20 7d 0a 20 20 0a 61 74 74 61 63 68 5f 65 6e    }.  .attach_en
22d37 64 3a 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  d:.  sqlite3Expr
22d38 44 65 6c 65 74 65 28 64 62 2c 20 70 46 69 6c 65  Delete(db, pFile
22d39 6e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  name);.  sqlite3
22d3a 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
22d3b 44 62 6e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74  Dbname);.  sqlit
22d3c 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
22d3d 20 70 4b 65 79 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   pKey);.}../*.**
22d3e 20 43 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70   Called by the p
22d3f 61 72 73 65 72 20 74 6f 20 63 6f 6d 70 69 6c 65  arser to compile
22d40 20 61 20 44 45 54 41 43 48 20 73 74 61 74 65 6d   a DETACH statem
22d41 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44  ent..**.**     D
22d42 45 54 41 43 48 20 70 44 62 6e 61 6d 65 0a 2a 2f  ETACH pDbname.*/
22d43 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
22d44 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 74 61  void sqlite3Deta
22d45 63 68 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ch(Parse *pParse
22d46 2c 20 45 78 70 72 20 2a 70 44 62 6e 61 6d 65 29  , Expr *pDbname)
22d47 7b 0a 20 20 73 74 61 74 69 63 20 46 75 6e 63 44  {.  static FuncD
22d48 65 66 20 64 65 74 61 63 68 5f 66 75 6e 63 20 3d  ef detach_func =
22d49 20 7b 0a 20 20 20 20 31 2c 20 20 20 20 20 20 20   {.    1,       
22d4a 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 41 72 67           /* nArg
22d4b 20 2a 2f 0a 20 20 20 20 53 51 4c 49 54 45 5f 55   */.    SQLITE_U
22d4c 54 46 38 2c 20 20 20 20 20 20 2f 2a 20 69 50 72  TF8,      /* iPr
22d4d 65 66 45 6e 63 20 2a 2f 0a 20 20 20 20 30 2c 20  efEnc */.    0, 
22d4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
22d4f 2a 20 66 6c 61 67 73 20 2a 2f 0a 20 20 20 20 30  * flags */.    0
22d50 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
22d51 20 2f 2a 20 70 55 73 65 72 44 61 74 61 20 2a 2f   /* pUserData */
22d52 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20  .    0,         
22d53 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74 20         /* pNext 
22d54 2a 2f 0a 20 20 20 20 64 65 74 61 63 68 46 75 6e  */.    detachFun
22d55 63 2c 20 20 20 20 20 20 20 2f 2a 20 78 46 75 6e  c,       /* xFun
22d56 63 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20  c */.    0,     
22d57 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53             /* xS
22d58 74 65 70 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20  tep */.    0,   
22d59 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
22d5a 78 46 69 6e 61 6c 69 7a 65 20 2a 2f 0a 20 20 20  xFinalize */.   
22d5b 20 22 73 71 6c 69 74 65 5f 64 65 74 61 63 68 22   "sqlite_detach"
22d5c 2c 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20  ,  /* zName */. 
22d5d 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20     0            
22d5e 20 20 20 20 20 2f 2a 20 70 48 61 73 68 20 2a 2f       /* pHash */
22d5f 0a 20 20 7d 3b 0a 20 20 63 6f 64 65 41 74 74 61  .  };.  codeAtta
22d60 63 68 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54  ch(pParse, SQLIT
22d61 45 5f 44 45 54 41 43 48 2c 20 26 64 65 74 61 63  E_DETACH, &detac
22d62 68 5f 66 75 6e 63 2c 20 70 44 62 6e 61 6d 65 2c  h_func, pDbname,
22d63 20 30 2c 20 30 2c 20 70 44 62 6e 61 6d 65 29 3b   0, 0, pDbname);
22d64 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 65 64  .}../*.** Called
22d65 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20 74   by the parser t
22d66 6f 20 63 6f 6d 70 69 6c 65 20 61 6e 20 41 54 54  o compile an ATT
22d67 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  ACH statement..*
22d68 2a 0a 2a 2a 20 20 20 20 20 41 54 54 41 43 48 20  *.**     ATTACH 
22d69 70 20 41 53 20 70 44 62 6e 61 6d 65 20 4b 45 59  p AS pDbname KEY
22d6a 20 70 4b 65 79 0a 2a 2f 0a 53 51 4c 49 54 45 5f   pKey.*/.SQLITE_
22d6b 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
22d6c 69 74 65 33 41 74 74 61 63 68 28 50 61 72 73 65  ite3Attach(Parse
22d6d 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a   *pParse, Expr *
22d6e 70 2c 20 45 78 70 72 20 2a 70 44 62 6e 61 6d 65  p, Expr *pDbname
22d6f 2c 20 45 78 70 72 20 2a 70 4b 65 79 29 7b 0a 20  , Expr *pKey){. 
22d70 20 73 74 61 74 69 63 20 46 75 6e 63 44 65 66 20   static FuncDef 
22d71 61 74 74 61 63 68 5f 66 75 6e 63 20 3d 20 7b 0a  attach_func = {.
22d72 20 20 20 20 33 2c 20 20 20 20 20 20 20 20 20 20      3,          
22d73 20 20 20 20 20 20 2f 2a 20 6e 41 72 67 20 2a 2f        /* nArg */
22d74 0a 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38  .    SQLITE_UTF8
22d75 2c 20 20 20 20 20 20 2f 2a 20 69 50 72 65 66 45  ,      /* iPrefE
22d76 6e 63 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20  nc */.    0,    
22d77 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66              /* f
22d78 6c 61 67 73 20 2a 2f 0a 20 20 20 20 30 2c 20 20  lags */.    0,  
22d79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
22d7a 20 70 55 73 65 72 44 61 74 61 20 2a 2f 0a 20 20   pUserData */.  
22d7b 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
22d7c 20 20 20 20 2f 2a 20 70 4e 65 78 74 20 2a 2f 0a      /* pNext */.
22d7d 20 20 20 20 61 74 74 61 63 68 46 75 6e 63 2c 20      attachFunc, 
22d7e 20 20 20 20 20 20 2f 2a 20 78 46 75 6e 63 20 2a        /* xFunc *
22d7f 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20  /.    0,        
22d80 20 20 20 20 20 20 20 20 2f 2a 20 78 53 74 65 70          /* xStep
22d81 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20   */.    0,      
22d82 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69            /* xFi
22d83 6e 61 6c 69 7a 65 20 2a 2f 0a 20 20 20 20 22 73  nalize */.    "s
22d84 71 6c 69 74 65 5f 61 74 74 61 63 68 22 2c 20 20  qlite_attach",  
22d85 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20  /* zName */.    
22d86 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0               
22d87 20 20 2f 2a 20 70 48 61 73 68 20 2a 2f 0a 20 20    /* pHash */.  
22d88 7d 3b 0a 20 20 63 6f 64 65 41 74 74 61 63 68 28  };.  codeAttach(
22d89 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 41  pParse, SQLITE_A
22d8a 54 54 41 43 48 2c 20 26 61 74 74 61 63 68 5f 66  TTACH, &attach_f
22d8b 75 6e 63 2c 20 70 2c 20 70 2c 20 70 44 62 6e 61  unc, p, p, pDbna
22d8c 6d 65 2c 20 70 4b 65 79 29 3b 0a 7d 0a 23 65 6e  me, pKey);.}.#en
22d8d 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
22d8e 49 54 5f 41 54 54 41 43 48 20 2a 2f 0a 0a 2f 2a  IT_ATTACH */../*
22d8f 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61  .** Initialize a
22d90 20 44 62 46 69 78 65 72 20 73 74 72 75 63 74 75   DbFixer structu
22d91 72 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  re.  This routin
22d92 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64  e must be called
22d93 20 70 72 69 6f 72 0a 2a 2a 20 74 6f 20 70 61 73   prior.** to pas
22d94 73 69 6e 67 20 74 68 65 20 73 74 72 75 63 74 75  sing the structu
22d95 72 65 20 74 6f 20 6f 6e 65 20 6f 66 20 74 68 65  re to one of the
22d96 20 73 71 6c 69 74 65 46 69 78 41 41 41 41 28 29   sqliteFixAAAA()
22d97 20 72 6f 75 74 69 6e 65 73 20 62 65 6c 6f 77 2e   routines below.
22d98 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72  .**.** The retur
22d99 6e 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65  n value indicate
22d9a 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  s whether or not
22d9b 20 66 69 78 61 74 69 6f 6e 20 69 73 20 72 65 71   fixation is req
22d9c 75 69 72 65 64 2e 20 20 54 52 55 45 0a 2a 2a 20  uired.  TRUE.** 
22d9d 6d 65 61 6e 73 20 77 65 20 64 6f 20 6e 65 65 64  means we do need
22d9e 20 74 6f 20 66 69 78 20 74 68 65 20 64 61 74 61   to fix the data
22d9f 62 61 73 65 20 72 65 66 65 72 65 6e 63 65 73 2c  base references,
22da0 20 46 41 4c 53 45 20 6d 65 61 6e 73 20 77 65 20   FALSE means we 
22da1 64 6f 20 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54  do not..*/.SQLIT
22da2 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
22da3 6c 69 74 65 33 46 69 78 49 6e 69 74 28 0a 20 20  lite3FixInit(.  
22da4 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c 20 20  DbFixer *pFix,  
22da5 20 20 20 20 2f 2a 20 54 68 65 20 66 69 78 65 72      /* The fixer
22da6 20 74 6f 20 62 65 20 69 6e 69 74 69 61 6c 69 7a   to be initializ
22da7 65 64 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70  ed */.  Parse *p
22da8 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 45  Parse,      /* E
22da9 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 77 69  rror messages wi
22daa 6c 6c 20 62 65 20 77 72 69 74 74 65 6e 20 68 65  ll be written he
22dab 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c  re */.  int iDb,
22dac 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
22dad 68 69 73 20 69 73 20 74 68 65 20 64 61 74 61 62  his is the datab
22dae 61 73 65 20 74 68 61 74 20 6d 75 73 74 20 62 65  ase that must be
22daf 20 75 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74   used */.  const
22db0 20 63 68 61 72 20 2a 7a 54 79 70 65 2c 20 20 2f   char *zType,  /
22db1 2a 20 22 76 69 65 77 22 2c 20 22 74 72 69 67 67  * "view", "trigg
22db2 65 72 22 2c 20 6f 72 20 22 69 6e 64 65 78 22 20  er", or "index" 
22db3 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b 65 6e  */.  const Token
22db4 20 2a 70 4e 61 6d 65 20 20 2f 2a 20 4e 61 6d 65   *pName  /* Name
22db5 20 6f 66 20 74 68 65 20 76 69 65 77 2c 20 74 72   of the view, tr
22db6 69 67 67 65 72 2c 20 6f 72 20 69 6e 64 65 78 20  igger, or index 
22db7 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  */.){.  sqlite3 
22db8 2a 64 62 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45  *db;..  if( NEVE
22db9 52 28 69 44 62 3c 30 29 20 7c 7c 20 69 44 62 3d  R(iDb<0) || iDb=
22dba 3d 31 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =1 ) return 0;. 
22dbb 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62   db = pParse->db
22dbc 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e  ;.  assert( db->
22dbd 6e 44 62 3e 69 44 62 20 29 3b 0a 20 20 70 46 69  nDb>iDb );.  pFi
22dbe 78 2d 3e 70 50 61 72 73 65 20 3d 20 70 50 61 72  x->pParse = pPar
22dbf 73 65 3b 0a 20 20 70 46 69 78 2d 3e 7a 44 62 20  se;.  pFix->zDb 
22dc0 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  = db->aDb[iDb].z
22dc1 4e 61 6d 65 3b 0a 20 20 70 46 69 78 2d 3e 7a 54  Name;.  pFix->zT
22dc2 79 70 65 20 3d 20 7a 54 79 70 65 3b 0a 20 20 70  ype = zType;.  p
22dc3 46 69 78 2d 3e 70 4e 61 6d 65 20 3d 20 70 4e 61  Fix->pName = pNa
22dc4 6d 65 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a  me;.  return 1;.
22dc5 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  }../*.** The fol
22dc6 6c 6f 77 69 6e 67 20 73 65 74 20 6f 66 20 72 6f  lowing set of ro
22dc7 75 74 69 6e 65 73 20 77 61 6c 6b 20 74 68 72 6f  utines walk thro
22dc8 75 67 68 20 74 68 65 20 70 61 72 73 65 20 74 72  ugh the parse tr
22dc9 65 65 20 61 6e 64 20 61 73 73 69 67 6e 0a 2a 2a  ee and assign.**
22dca 20 61 20 73 70 65 63 69 66 69 63 20 64 61 74 61   a specific data
22dcb 62 61 73 65 20 74 6f 20 61 6c 6c 20 74 61 62 6c  base to all tabl
22dcc 65 20 72 65 66 65 72 65 6e 63 65 73 20 77 68 65  e references whe
22dcd 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  re the database 
22dce 6e 61 6d 65 0a 2a 2a 20 77 61 73 20 6c 65 66 74  name.** was left
22dcf 20 75 6e 73 70 65 63 69 66 69 65 64 20 69 6e 20   unspecified in 
22dd0 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c  the original SQL
22dd1 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65   statement.  The
22dd2 20 70 46 69 78 20 73 74 72 75 63 74 75 72 65 0a   pFix structure.
22dd3 2a 2a 20 6d 75 73 74 20 68 61 76 65 20 62 65 65  ** must have bee
22dd4 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 62 79  n initialized by
22dd5 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f   a prior call to
22dd6 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74 28   sqlite3FixInit(
22dd7 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72  )..**.** These r
22dd8 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64  outines are used
22dd9 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
22dda 61 74 20 61 6e 20 69 6e 64 65 78 2c 20 74 72 69  at an index, tri
22ddb 67 67 65 72 2c 20 6f 72 0a 2a 2a 20 76 69 65 77  gger, or.** view
22ddc 20 69 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65   in one database
22ddd 20 64 6f 65 73 20 6e 6f 74 20 72 65 66 65 72 20   does not refer 
22dde 74 6f 20 6f 62 6a 65 63 74 73 20 69 6e 20 61 20  to objects in a 
22ddf 64 69 66 66 65 72 65 6e 74 20 64 61 74 61 62 61  different databa
22de0 73 65 2e 0a 2a 2a 20 28 45 78 63 65 70 74 69 6f  se..** (Exceptio
22de1 6e 3a 20 69 6e 64 69 63 65 73 2c 20 74 72 69 67  n: indices, trig
22de2 67 65 72 73 2c 20 61 6e 64 20 76 69 65 77 73 20  gers, and views 
22de3 69 6e 20 74 68 65 20 54 45 4d 50 20 64 61 74 61  in the TEMP data
22de4 62 61 73 65 20 61 72 65 0a 2a 2a 20 61 6c 6c 6f  base are.** allo
22de5 77 65 64 20 74 6f 20 72 65 66 65 72 20 74 6f 20  wed to refer to 
22de6 61 6e 79 74 68 69 6e 67 2e 29 20 20 49 66 20 61  anything.)  If a
22de7 20 72 65 66 65 72 65 6e 63 65 20 69 73 20 65 78   reference is ex
22de8 70 6c 69 63 69 74 6c 79 20 6d 61 64 65 0a 2a 2a  plicitly made.**
22de9 20 74 6f 20 61 6e 20 6f 62 6a 65 63 74 20 69 6e   to an object in
22dea 20 61 20 64 69 66 66 65 72 65 6e 74 20 64 61 74   a different dat
22deb 61 62 61 73 65 2c 20 61 6e 20 65 72 72 6f 72 20  abase, an error 
22dec 6d 65 73 73 61 67 65 20 69 73 20 61 64 64 65 64  message is added
22ded 20 74 6f 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 7a   to.** pParse->z
22dee 45 72 72 4d 73 67 20 61 6e 64 20 74 68 65 73 65  ErrMsg and these
22def 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e   routines return
22df0 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 49 66 20 65   non-zero.  If e
22df1 76 65 72 79 74 68 69 6e 67 0a 2a 2a 20 63 68 65  verything.** che
22df2 63 6b 73 20 6f 75 74 2c 20 74 68 65 73 65 20 72  cks out, these r
22df3 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 30  outines return 0
22df4 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
22df5 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
22df6 69 78 53 72 63 4c 69 73 74 28 0a 20 20 44 62 46  ixSrcList(.  DbF
22df7 69 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20 20  ixer *pFix,     
22df8 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20    /* Context of 
22df9 74 68 65 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0a  the fixation */.
22dfa 20 20 53 72 63 4c 69 73 74 20 2a 70 4c 69 73 74    SrcList *pList
22dfb 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 6f         /* The So
22dfc 75 72 63 65 20 6c 69 73 74 20 74 6f 20 63 68 65  urce list to che
22dfd 63 6b 20 61 6e 64 20 6d 6f 64 69 66 79 20 2a 2f  ck and modify */
22dfe 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63  .){.  int i;.  c
22dff 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 0a  onst char *zDb;.
22e00 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
22e01 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20  _item *pItem;.. 
22e02 20 69 66 28 20 4e 45 56 45 52 28 70 4c 69 73 74   if( NEVER(pList
22e03 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 20 30 3b  ==0) ) return 0;
22e04 0a 20 20 7a 44 62 20 3d 20 70 46 69 78 2d 3e 7a  .  zDb = pFix->z
22e05 44 62 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70  Db;.  for(i=0, p
22e06 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69  Item=pList->a; i
22e07 3c 70 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b  <pList->nSrc; i+
22e08 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20  +, pItem++){.   
22e09 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 44 61 74   if( pItem->zDat
22e0a 61 62 61 73 65 3d 3d 30 20 29 7b 0a 20 20 20 20  abase==0 ){.    
22e0b 20 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61    pItem->zDataba
22e0c 73 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  se = sqlite3DbSt
22e0d 72 44 75 70 28 70 46 69 78 2d 3e 70 50 61 72 73  rDup(pFix->pPars
22e0e 65 2d 3e 64 62 2c 20 7a 44 62 29 3b 0a 20 20 20  e->db, zDb);.   
22e0f 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74   }else if( sqlit
22e10 65 33 53 74 72 49 43 6d 70 28 70 49 74 65 6d 2d  e3StrICmp(pItem-
22e11 3e 7a 44 61 74 61 62 61 73 65 2c 7a 44 62 29 21  >zDatabase,zDb)!
22e12 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
22e13 74 65 33 45 72 72 6f 72 4d 73 67 28 70 46 69 78  te3ErrorMsg(pFix
22e14 2d 3e 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20  ->pParse,.      
22e15 20 20 20 22 25 73 20 25 54 20 63 61 6e 6e 6f 74     "%s %T cannot
22e16 20 72 65 66 65 72 65 6e 63 65 20 6f 62 6a 65 63   reference objec
22e17 74 73 20 69 6e 20 64 61 74 61 62 61 73 65 20 25  ts in database %
22e18 73 22 2c 0a 20 20 20 20 20 20 20 20 20 70 46 69  s",.         pFi
22e19 78 2d 3e 7a 54 79 70 65 2c 20 70 46 69 78 2d 3e  x->zType, pFix->
22e1a 70 4e 61 6d 65 2c 20 70 49 74 65 6d 2d 3e 7a 44  pName, pItem->zD
22e1b 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20 20 20  atabase);.      
22e1c 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
22e1d 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
22e1e 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c  ITE_OMIT_VIEW) |
22e1f 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  | !defined(SQLIT
22e20 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a  E_OMIT_TRIGGER).
22e21 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46      if( sqlite3F
22e22 69 78 53 65 6c 65 63 74 28 70 46 69 78 2c 20 70  ixSelect(pFix, p
22e23 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 29 20 29  Item->pSelect) )
22e24 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 69   return 1;.    i
22e25 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78 70  f( sqlite3FixExp
22e26 72 28 70 46 69 78 2c 20 70 49 74 65 6d 2d 3e 70  r(pFix, pItem->p
22e27 4f 6e 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  On) ) return 1;.
22e28 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72 65 74  #endif.  }.  ret
22e29 75 72 6e 20 30 3b 0a 7d 0a 23 69 66 20 21 64 65  urn 0;.}.#if !de
22e2a 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
22e2b 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69  T_VIEW) || !defi
22e2c 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
22e2d 54 52 49 47 47 45 52 29 0a 53 51 4c 49 54 45 5f  TRIGGER).SQLITE_
22e2e 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
22e2f 74 65 33 46 69 78 53 65 6c 65 63 74 28 0a 20 20  te3FixSelect(.  
22e30 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c 20 20  DbFixer *pFix,  
22e31 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20       /* Context 
22e32 6f 66 20 74 68 65 20 66 69 78 61 74 69 6f 6e 20  of the fixation 
22e33 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65  */.  Select *pSe
22e34 6c 65 63 74 20 20 20 20 20 20 2f 2a 20 54 68 65  lect      /* The
22e35 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
22e36 74 20 74 6f 20 62 65 20 66 69 78 65 64 20 74 6f  t to be fixed to
22e37 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 2a 2f   one database */
22e38 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20 70 53 65  .){.  while( pSe
22e39 6c 65 63 74 20 29 7b 0a 20 20 20 20 69 66 28 20  lect ){.    if( 
22e3a 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 4c 69  sqlite3FixExprLi
22e3b 73 74 28 70 46 69 78 2c 20 70 53 65 6c 65 63 74  st(pFix, pSelect
22e3c 2d 3e 70 45 4c 69 73 74 29 20 29 7b 0a 20 20 20  ->pEList) ){.   
22e3d 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
22e3e 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74   }.    if( sqlit
22e3f 65 33 46 69 78 53 72 63 4c 69 73 74 28 70 46 69  e3FixSrcList(pFi
22e40 78 2c 20 70 53 65 6c 65 63 74 2d 3e 70 53 72 63  x, pSelect->pSrc
22e41 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
22e42 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  n 1;.    }.    i
22e43 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78 70  f( sqlite3FixExp
22e44 72 28 70 46 69 78 2c 20 70 53 65 6c 65 63 74 2d  r(pFix, pSelect-
22e45 3e 70 57 68 65 72 65 29 20 29 7b 0a 20 20 20 20  >pWhere) ){.    
22e46 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
22e47 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  }.    if( sqlite
22e48 33 46 69 78 45 78 70 72 28 70 46 69 78 2c 20 70  3FixExpr(pFix, p
22e49 53 65 6c 65 63 74 2d 3e 70 48 61 76 69 6e 67 29  Select->pHaving)
22e4a 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
22e4b 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 53   1;.    }.    pS
22e4c 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63 74 2d  elect = pSelect-
22e4d 3e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 72  >pPrior;.  }.  r
22e4e 65 74 75 72 6e 20 30 3b 0a 7d 0a 53 51 4c 49 54  eturn 0;.}.SQLIT
22e4f 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
22e50 6c 69 74 65 33 46 69 78 45 78 70 72 28 0a 20 20  lite3FixExpr(.  
22e51 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c 20 20  DbFixer *pFix,  
22e52 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 6f 66     /* Context of
22e53 20 74 68 65 20 66 69 78 61 74 69 6f 6e 20 2a 2f   the fixation */
22e54 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 20  .  Expr *pExpr  
22e55 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70        /* The exp
22e56 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 66 69  ression to be fi
22e57 78 65 64 20 74 6f 20 6f 6e 65 20 64 61 74 61 62  xed to one datab
22e58 61 73 65 20 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c  ase */.){.  whil
22e59 65 28 20 70 45 78 70 72 20 29 7b 0a 20 20 20 20  e( pExpr ){.    
22e5a 69 66 28 20 45 78 70 72 48 61 73 41 6e 79 50 72  if( ExprHasAnyPr
22e5b 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
22e5c 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 20 62 72  _TokenOnly) ) br
22e5d 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 45 78 70  eak;.    if( Exp
22e5e 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78  rHasProperty(pEx
22e5f 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  pr, EP_xIsSelect
22e60 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73  ) ){.      if( s
22e61 71 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74 28  qlite3FixSelect(
22e62 70 46 69 78 2c 20 70 45 78 70 72 2d 3e 78 2e 70  pFix, pExpr->x.p
22e63 53 65 6c 65 63 74 29 20 29 20 72 65 74 75 72 6e  Select) ) return
22e64 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   1;.    }else{. 
22e65 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
22e66 46 69 78 45 78 70 72 4c 69 73 74 28 70 46 69 78  FixExprList(pFix
22e67 2c 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  , pExpr->x.pList
22e68 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  ) ) return 1;.  
22e69 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69    }.    if( sqli
22e6a 74 65 33 46 69 78 45 78 70 72 28 70 46 69 78 2c  te3FixExpr(pFix,
22e6b 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29 20   pExpr->pRight) 
22e6c 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
22e6d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 45 78  1;.    }.    pEx
22e6e 70 72 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66  pr = pExpr->pLef
22e6f 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  t;.  }.  return 
22e70 30 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  0;.}.SQLITE_PRIV
22e71 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
22e72 69 78 45 78 70 72 4c 69 73 74 28 0a 20 20 44 62  ixExprList(.  Db
22e73 46 69 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20  Fixer *pFix,    
22e74 20 2f 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20 74   /* Context of t
22e75 68 65 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20  he fixation */. 
22e76 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
22e77 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65      /* The expre
22e78 73 73 69 6f 6e 20 74 6f 20 62 65 20 66 69 78 65  ssion to be fixe
22e79 64 20 74 6f 20 6f 6e 65 20 64 61 74 61 62 61 73  d to one databas
22e7a 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b  e */.){.  int i;
22e7b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69  .  struct ExprLi
22e7c 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a  st_item *pItem;.
22e7d 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29    if( pList==0 )
22e7e 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 66 6f 72   return 0;.  for
22e7f 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4c 69 73  (i=0, pItem=pLis
22e80 74 2d 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e  t->a; i<pList->n
22e81 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d  Expr; i++, pItem
22e82 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c  ++){.    if( sql
22e83 69 74 65 33 46 69 78 45 78 70 72 28 70 46 69 78  ite3FixExpr(pFix
22e84 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 20  , pItem->pExpr) 
22e85 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
22e86 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  1;.    }.  }.  r
22e87 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69  eturn 0;.}.#endi
22e88 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  f..#ifndef SQLIT
22e89 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 53  E_OMIT_TRIGGER.S
22e8a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
22e8b 74 20 73 71 6c 69 74 65 33 46 69 78 54 72 69 67  t sqlite3FixTrig
22e8c 67 65 72 53 74 65 70 28 0a 20 20 44 62 46 69 78  gerStep(.  DbFix
22e8d 65 72 20 2a 70 46 69 78 2c 20 20 20 20 20 2f 2a  er *pFix,     /*
22e8e 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20   Context of the 
22e8f 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 54 72  fixation */.  Tr
22e90 69 67 67 65 72 53 74 65 70 20 2a 70 53 74 65 70  iggerStep *pStep
22e91 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72 20   /* The trigger 
22e92 73 74 65 70 20 62 65 20 66 69 78 65 64 20 74 6f  step be fixed to
22e93 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 2a 2f   one database */
22e94 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20 70 53 74  .){.  while( pSt
22e95 65 70 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71  ep ){.    if( sq
22e96 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74 28 70  lite3FixSelect(p
22e97 46 69 78 2c 20 70 53 74 65 70 2d 3e 70 53 65 6c  Fix, pStep->pSel
22e98 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65  ect) ){.      re
22e99 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
22e9a 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78    if( sqlite3Fix
22e9b 45 78 70 72 28 70 46 69 78 2c 20 70 53 74 65 70  Expr(pFix, pStep
22e9c 2d 3e 70 57 68 65 72 65 29 20 29 7b 0a 20 20 20  ->pWhere) ){.   
22e9d 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
22e9e 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74   }.    if( sqlit
22e9f 65 33 46 69 78 45 78 70 72 4c 69 73 74 28 70 46  e3FixExprList(pF
22ea0 69 78 2c 20 70 53 74 65 70 2d 3e 70 45 78 70 72  ix, pStep->pExpr
22ea1 4c 69 73 74 29 20 29 7b 0a 20 20 20 20 20 20 72  List) ){.      r
22ea2 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
22ea3 20 20 20 70 53 74 65 70 20 3d 20 70 53 74 65 70     pStep = pStep
22ea4 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 72  ->pNext;.  }.  r
22ea5 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69  eturn 0;.}.#endi
22ea6 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f../************
22ea7 2a 2a 20 45 6e 64 20 6f 66 20 61 74 74 61 63 68  ** End of attach
22ea8 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
22ea9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22eaa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22eab 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
22eac 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 61 75  ** Begin file au
22ead 74 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  th.c ***********
22eae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22eaf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22eb0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 4a 61  */./*.** 2003 Ja
22eb1 6e 75 61 72 79 20 31 31 0a 2a 2a 0a 2a 2a 20 54  nuary 11.**.** T
22eb2 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
22eb3 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
22eb4 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
22eb5 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
22eb6 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
22eb7 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
22eb8 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
22eb9 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
22eba 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
22ebb 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
22ebc 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
22ebd 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
22ebe 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
22ebf 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
22ec0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
22ec1 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
22ec2 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
22ec3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22ec4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22ec5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22ec6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22ec7 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
22ec8 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f  file contains co
22ec9 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  de used to imple
22eca 6d 65 6e 74 20 74 68 65 20 73 71 6c 69 74 65 33  ment the sqlite3
22ecb 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28  _set_authorizer(
22ecc 29 0a 2a 2a 20 41 50 49 2e 20 20 54 68 69 73 20  ).** API.  This 
22ecd 66 61 63 69 6c 69 74 79 20 69 73 20 61 6e 20 6f  facility is an o
22ece 70 74 69 6f 6e 61 6c 20 66 65 61 74 75 72 65 20  ptional feature 
22ecf 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20  of the library. 
22ed0 20 45 6d 62 65 64 64 65 64 0a 2a 2a 20 73 79 73   Embedded.** sys
22ed1 74 65 6d 73 20 74 68 61 74 20 64 6f 20 6e 6f 74  tems that do not
22ed2 20 6e 65 65 64 20 74 68 69 73 20 66 61 63 69 6c   need this facil
22ed3 69 74 79 20 6d 61 79 20 6f 6d 69 74 20 69 74 20  ity may omit it 
22ed4 62 79 20 72 65 63 6f 6d 70 69 6c 69 6e 67 0a 2a  by recompiling.*
22ed5 2a 20 74 68 65 20 6c 69 62 72 61 72 79 20 77 69  * the library wi
22ed6 74 68 20 2d 44 53 51 4c 49 54 45 5f 4f 4d 49 54  th -DSQLITE_OMIT
22ed7 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 3d 31  _AUTHORIZATION=1
22ed8 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 61 75 74 68  .**.** $Id: auth
22ed9 2e 63 2c 76 20 31 2e 33 32 20 32 30 30 39 2f 30  .c,v 1.32 2009/0
22eda 37 2f 30 32 20 31 38 3a 34 30 3a 33 35 20 64 61  7/02 18:40:35 da
22edb 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a  nielk1977 Exp $.
22edc 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f 66  */../*.** All of
22edd 20 74 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69   the code in thi
22ede 73 20 66 69 6c 65 20 6d 61 79 20 62 65 20 6f 6d  s file may be om
22edf 69 74 74 65 64 20 62 79 20 64 65 66 69 6e 69 6e  itted by definin
22ee0 67 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 6d 61  g a single.** ma
22ee1 63 72 6f 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  cro..*/.#ifndef 
22ee2 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48  SQLITE_OMIT_AUTH
22ee3 4f 52 49 5a 41 54 49 4f 4e 0a 0a 2f 2a 0a 2a 2a  ORIZATION../*.**
22ee4 20 53 65 74 20 6f 72 20 63 6c 65 61 72 20 74 68   Set or clear th
22ee5 65 20 61 63 63 65 73 73 20 61 75 74 68 6f 72 69  e access authori
22ee6 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 2e  zation function.
22ee7 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 63 63 65 73  .**.** The acces
22ee8 73 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  s authorization 
22ee9 66 75 6e 63 74 69 6f 6e 20 69 73 20 62 65 20 63  function is be c
22eea 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 74 68 65  alled during the
22eeb 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 0a 2a 2a 20   compilation.** 
22eec 70 68 61 73 65 20 74 6f 20 76 65 72 69 66 79 20  phase to verify 
22eed 74 68 61 74 20 74 68 65 20 75 73 65 72 20 68 61  that the user ha
22eee 73 20 72 65 61 64 20 61 6e 64 2f 6f 72 20 77 72  s read and/or wr
22eef 69 74 65 20 61 63 63 65 73 73 20 70 65 72 6d 69  ite access permi
22ef0 73 73 69 6f 6e 20 6f 6e 0a 2a 2a 20 76 61 72 69  ssion on.** vari
22ef1 6f 75 73 20 66 69 65 6c 64 73 20 6f 66 20 74 68  ous fields of th
22ef2 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 65  e database.  The
22ef3 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
22ef4 74 6f 20 74 68 65 20 61 75 74 68 20 66 75 6e 63  to the auth func
22ef5 74 69 6f 6e 0a 2a 2a 20 69 73 20 61 20 63 6f 70  tion.** is a cop
22ef6 79 20 6f 66 20 74 68 65 20 33 72 64 20 61 72 67  y of the 3rd arg
22ef7 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 72 6f  ument to this ro
22ef8 75 74 69 6e 65 2e 20 20 54 68 65 20 73 65 63 6f  utine.  The seco
22ef9 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74  nd argument.** t
22efa 6f 20 74 68 65 20 61 75 74 68 20 66 75 6e 63 74  o the auth funct
22efb 69 6f 6e 20 69 73 20 6f 6e 65 20 6f 66 20 74 68  ion is one of th
22efc 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a  ese constants:.*
22efd 2a 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54  *.**       SQLIT
22efe 45 5f 43 52 45 41 54 45 5f 49 4e 44 45 58 0a 2a  E_CREATE_INDEX.*
22eff 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43  *       SQLITE_C
22f00 52 45 41 54 45 5f 54 41 42 4c 45 0a 2a 2a 20 20  REATE_TABLE.**  
22f01 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41       SQLITE_CREA
22f02 54 45 5f 54 45 4d 50 5f 49 4e 44 45 58 0a 2a 2a  TE_TEMP_INDEX.**
22f03 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52         SQLITE_CR
22f04 45 41 54 45 5f 54 45 4d 50 5f 54 41 42 4c 45 0a  EATE_TEMP_TABLE.
22f05 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  **       SQLITE_
22f06 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 52 49 47  CREATE_TEMP_TRIG
22f07 47 45 52 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  GER.**       SQL
22f08 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f  ITE_CREATE_TEMP_
22f09 56 49 45 57 0a 2a 2a 20 20 20 20 20 20 20 53 51  VIEW.**       SQ
22f0a 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 52 49 47  LITE_CREATE_TRIG
22f0b 47 45 52 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  GER.**       SQL
22f0c 49 54 45 5f 43 52 45 41 54 45 5f 56 49 45 57 0a  ITE_CREATE_VIEW.
22f0d 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  **       SQLITE_
22f0e 44 45 4c 45 54 45 0a 2a 2a 20 20 20 20 20 20 20  DELETE.**       
22f0f 53 51 4c 49 54 45 5f 44 52 4f 50 5f 49 4e 44 45  SQLITE_DROP_INDE
22f10 58 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54  X.**       SQLIT
22f11 45 5f 44 52 4f 50 5f 54 41 42 4c 45 0a 2a 2a 20  E_DROP_TABLE.** 
22f12 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f        SQLITE_DRO
22f13 50 5f 54 45 4d 50 5f 49 4e 44 45 58 0a 2a 2a 20  P_TEMP_INDEX.** 
22f14 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f        SQLITE_DRO
22f15 50 5f 54 45 4d 50 5f 54 41 42 4c 45 0a 2a 2a 20  P_TEMP_TABLE.** 
22f16 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f        SQLITE_DRO
22f17 50 5f 54 45 4d 50 5f 54 52 49 47 47 45 52 0a 2a  P_TEMP_TRIGGER.*
22f18 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44  *       SQLITE_D
22f19 52 4f 50 5f 54 45 4d 50 5f 56 49 45 57 0a 2a 2a  ROP_TEMP_VIEW.**
22f1a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52         SQLITE_DR
22f1b 4f 50 5f 54 52 49 47 47 45 52 0a 2a 2a 20 20 20  OP_TRIGGER.**   
22f1c 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f      SQLITE_DROP_
22f1d 56 49 45 57 0a 2a 2a 20 20 20 20 20 20 20 53 51  VIEW.**       SQ
22f1e 4c 49 54 45 5f 49 4e 53 45 52 54 0a 2a 2a 20 20  LITE_INSERT.**  
22f1f 20 20 20 20 20 53 51 4c 49 54 45 5f 50 52 41 47       SQLITE_PRAG
22f20 4d 41 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49  MA.**       SQLI
22f21 54 45 5f 52 45 41 44 0a 2a 2a 20 20 20 20 20 20  TE_READ.**      
22f22 20 53 51 4c 49 54 45 5f 53 45 4c 45 43 54 0a 2a   SQLITE_SELECT.*
22f23 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 54  *       SQLITE_T
22f24 52 41 4e 53 41 43 54 49 4f 4e 0a 2a 2a 20 20 20  RANSACTION.**   
22f25 20 20 20 20 53 51 4c 49 54 45 5f 55 50 44 41 54      SQLITE_UPDAT
22f26 45 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72  E.**.** The thir
22f27 64 20 61 6e 64 20 66 6f 75 72 74 68 20 61 72 67  d and fourth arg
22f28 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 61 75  uments to the au
22f29 74 68 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20  th function are 
22f2a 74 68 65 20 6e 61 6d 65 20 6f 66 0a 2a 2a 20 74  the name of.** t
22f2b 68 65 20 74 61 62 6c 65 20 61 6e 64 20 74 68 65  he table and the
22f2c 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 61 72 65   column that are
22f2d 20 62 65 69 6e 67 20 61 63 63 65 73 73 65 64 2e   being accessed.
22f2e 20 20 54 68 65 20 61 75 74 68 20 66 75 6e 63 74    The auth funct
22f2f 69 6f 6e 0a 2a 2a 20 73 68 6f 75 6c 64 20 72 65  ion.** should re
22f30 74 75 72 6e 20 65 69 74 68 65 72 20 53 51 4c 49  turn either SQLI
22f31 54 45 5f 4f 4b 2c 20 53 51 4c 49 54 45 5f 44 45  TE_OK, SQLITE_DE
22f32 4e 59 2c 20 6f 72 20 53 51 4c 49 54 45 5f 49 47  NY, or SQLITE_IG
22f33 4e 4f 52 45 2e 20 20 49 66 0a 2a 2a 20 53 51 4c  NORE.  If.** SQL
22f34 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
22f35 65 64 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61  ed, it means tha
22f36 74 20 61 63 63 65 73 73 20 69 73 20 61 6c 6c 6f  t access is allo
22f37 77 65 64 2e 20 20 53 51 4c 49 54 45 5f 44 45 4e  wed.  SQLITE_DEN
22f38 59 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74 20  Y.** means that 
22f39 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
22f3a 74 20 77 69 6c 6c 20 6e 65 76 65 72 2d 72 75 6e  t will never-run
22f3b 20 2d 20 74 68 65 20 73 71 6c 69 74 65 33 5f 65   - the sqlite3_e
22f3c 78 65 63 28 29 20 63 61 6c 6c 0a 2a 2a 20 77 69  xec() call.** wi
22f3d 6c 6c 20 72 65 74 75 72 6e 20 77 69 74 68 20 61  ll return with a
22f3e 6e 20 65 72 72 6f 72 2e 20 20 53 51 4c 49 54 45  n error.  SQLITE
22f3f 5f 49 47 4e 4f 52 45 20 6d 65 61 6e 73 20 74 68  _IGNORE means th
22f40 61 74 20 74 68 65 20 53 51 4c 20 73 74 61 74 65  at the SQL state
22f41 6d 65 6e 74 0a 2a 2a 20 73 68 6f 75 6c 64 20 72  ment.** should r
22f42 75 6e 20 62 75 74 20 61 74 74 65 6d 70 74 73 20  un but attempts 
22f43 74 6f 20 72 65 61 64 20 74 68 65 20 73 70 65 63  to read the spec
22f44 69 66 69 65 64 20 63 6f 6c 75 6d 6e 20 77 69 6c  ified column wil
22f45 6c 20 72 65 74 75 72 6e 20 4e 55 4c 4c 0a 2a 2a  l return NULL.**
22f46 20 61 6e 64 20 61 74 74 65 6d 70 74 73 20 74 6f   and attempts to
22f47 20 77 72 69 74 65 20 74 68 65 20 63 6f 6c 75 6d   write the colum
22f48 6e 20 77 69 6c 6c 20 62 65 20 69 67 6e 6f 72 65  n will be ignore
22f49 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 74 74 69 6e 67  d..**.** Setting
22f4a 20 74 68 65 20 61 75 74 68 20 66 75 6e 63 74 69   the auth functi
22f4b 6f 6e 20 74 6f 20 4e 55 4c 4c 20 64 69 73 61 62  on to NULL disab
22f4c 6c 65 73 20 74 68 69 73 20 68 6f 6f 6b 2e 20 20  les this hook.  
22f4d 54 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 73  The default.** s
22f4e 65 74 74 69 6e 67 20 6f 66 20 74 68 65 20 61 75  etting of the au
22f4f 74 68 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e  th function is N
22f50 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ULL..*/.SQLITE_A
22f51 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73  PI int sqlite3_s
22f52 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 0a 20  et_authorizer(. 
22f53 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20   sqlite3 *db,.  
22f54 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76 6f 69  int (*xAuth)(voi
22f55 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61  d*,int,const cha
22f56 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63  r*,const char*,c
22f57 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74  onst char*,const
22f58 20 63 68 61 72 2a 29 2c 0a 20 20 76 6f 69 64 20   char*),.  void 
22f59 2a 70 41 72 67 0a 29 7b 0a 20 20 73 71 6c 69 74  *pArg.){.  sqlit
22f5a 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
22f5b 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64 62 2d  b->mutex);.  db-
22f5c 3e 78 41 75 74 68 20 3d 20 78 41 75 74 68 3b 0a  >xAuth = xAuth;.
22f5d 20 20 64 62 2d 3e 70 41 75 74 68 41 72 67 20 3d    db->pAuthArg =
22f5e 20 70 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33   pArg;.  sqlite3
22f5f 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53 74  ExpirePreparedSt
22f60 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20  atements(db);.  
22f61 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
22f62 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
22f63 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
22f64 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69  OK;.}../*.** Wri
22f65 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  te an error mess
22f66 61 67 65 20 69 6e 74 6f 20 70 50 61 72 73 65 2d  age into pParse-
22f67 3e 7a 45 72 72 4d 73 67 20 74 68 61 74 20 65 78  >zErrMsg that ex
22f68 70 6c 61 69 6e 73 20 74 68 61 74 20 74 68 65 0a  plains that the.
22f69 2a 2a 20 75 73 65 72 2d 73 75 70 70 6c 69 65 64  ** user-supplied
22f6a 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66   authorization f
22f6b 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 65 64  unction returned
22f6c 20 61 6e 20 69 6c 6c 65 67 61 6c 20 76 61 6c 75   an illegal valu
22f6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
22f6e 64 20 73 71 6c 69 74 65 41 75 74 68 42 61 64 52  d sqliteAuthBadR
22f6f 65 74 75 72 6e 43 6f 64 65 28 50 61 72 73 65 20  eturnCode(Parse 
22f70 2a 70 50 61 72 73 65 29 7b 0a 20 20 73 71 6c 69  *pParse){.  sqli
22f71 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
22f72 73 65 2c 20 22 61 75 74 68 6f 72 69 7a 65 72 20  se, "authorizer 
22f73 6d 61 6c 66 75 6e 63 74 69 6f 6e 22 29 3b 0a 20  malfunction");. 
22f74 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51   pParse->rc = SQ
22f75 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f  LITE_ERROR;.}../
22f76 2a 0a 2a 2a 20 54 68 65 20 70 45 78 70 72 20 73  *.** The pExpr s
22f77 68 6f 75 6c 64 20 62 65 20 61 20 54 4b 5f 43 4f  hould be a TK_CO
22f78 4c 55 4d 4e 20 65 78 70 72 65 73 73 69 6f 6e 2e  LUMN expression.
22f79 20 20 54 68 65 20 74 61 62 6c 65 20 72 65 66 65    The table refe
22f7a 72 72 65 64 20 74 6f 0a 2a 2a 20 69 73 20 69 6e  rred to.** is in
22f7b 20 70 54 61 62 4c 69 73 74 20 6f 72 20 65 6c 73   pTabList or els
22f7c 65 20 69 74 20 69 73 20 74 68 65 20 4e 45 57 20  e it is the NEW 
22f7d 6f 72 20 4f 4c 44 20 74 61 62 6c 65 20 6f 66 20  or OLD table of 
22f7e 61 20 74 72 69 67 67 65 72 2e 20 20 0a 2a 2a 20  a trigger.  .** 
22f7f 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  Check to see if 
22f80 69 74 20 69 73 20 4f 4b 20 74 6f 20 72 65 61 64  it is OK to read
22f81 20 74 68 69 73 20 70 61 72 74 69 63 75 6c 61 72   this particular
22f82 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49   column..**.** I
22f83 66 20 74 68 65 20 61 75 74 68 20 66 75 6e 63 74  f the auth funct
22f84 69 6f 6e 20 72 65 74 75 72 6e 73 20 53 51 4c 49  ion returns SQLI
22f85 54 45 5f 49 47 4e 4f 52 45 2c 20 63 68 61 6e 67  TE_IGNORE, chang
22f86 65 20 74 68 65 20 54 4b 5f 43 4f 4c 55 4d 4e 20  e the TK_COLUMN 
22f87 0a 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  .** instruction 
22f88 69 6e 74 6f 20 61 20 54 4b 5f 4e 55 4c 4c 2e 20  into a TK_NULL. 
22f89 20 49 66 20 74 68 65 20 61 75 74 68 20 66 75 6e   If the auth fun
22f8a 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 53 51  ction returns SQ
22f8b 4c 49 54 45 5f 44 45 4e 59 2c 0a 2a 2a 20 74 68  LITE_DENY,.** th
22f8c 65 6e 20 67 65 6e 65 72 61 74 65 20 61 6e 20 65  en generate an e
22f8d 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rror..*/.SQLITE_
22f8e 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
22f8f 69 74 65 33 41 75 74 68 52 65 61 64 28 0a 20 20  ite3AuthRead(.  
22f90 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
22f91 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
22f92 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ser context */. 
22f93 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20   Expr *pExpr,   
22f94 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78         /* The ex
22f95 70 72 65 73 73 69 6f 6e 20 74 6f 20 63 68 65 63  pression to chec
22f96 6b 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  k authorization 
22f97 6f 6e 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a  on */.  Schema *
22f98 70 53 63 68 65 6d 61 2c 20 20 20 20 20 20 2f 2a  pSchema,      /*
22f99 20 54 68 65 20 73 63 68 65 6d 61 20 6f 66 20 74   The schema of t
22f9a 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f  he expression */
22f9b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62  .  SrcList *pTab
22f9c 4c 69 73 74 20 20 20 20 20 2f 2a 20 41 6c 6c 20  List     /* All 
22f9d 74 61 62 6c 65 20 74 68 61 74 20 70 45 78 70 72  table that pExpr
22f9e 20 6d 69 67 68 74 20 72 65 66 65 72 20 74 6f 20   might refer to 
22f9f 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  */.){.  sqlite3 
22fa0 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
22fa1 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 54 61  ;.  int rc;.  Ta
22fa2 62 6c 65 20 2a 70 54 61 62 20 3d 20 30 3b 20 20  ble *pTab = 0;  
22fa3 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65      /* The table
22fa4 20 62 65 69 6e 67 20 72 65 61 64 20 2a 2f 0a 20   being read */. 
22fa5 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f   const char *zCo
22fa6 6c 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f  l;     /* Name o
22fa7 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 66 20  f the column of 
22fa8 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69  the table */.  i
22fa9 6e 74 20 69 53 72 63 3b 20 20 20 20 20 20 20 20  nt iSrc;        
22faa 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e       /* Index in
22fab 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 5d 20 6f   pTabList->a[] o
22fac 66 20 74 61 62 6c 65 20 62 65 69 6e 67 20 72 65  f table being re
22fad 61 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ad */.  const ch
22fae 61 72 20 2a 7a 44 42 61 73 65 3b 20 20 20 2f 2a  ar *zDBase;   /*
22faf 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73   Name of databas
22fb0 65 20 62 65 69 6e 67 20 61 63 63 65 73 73 65 64  e being accessed
22fb1 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20   */.  int iDb;  
22fb2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
22fb3 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
22fb4 64 61 74 61 62 61 73 65 20 74 68 65 20 65 78 70  database the exp
22fb5 72 65 73 73 69 6f 6e 20 72 65 66 65 72 73 20 74  ression refers t
22fb6 6f 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 3b  o */.  int iCol;
22fb7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
22fb8 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20  Index of column 
22fb9 69 6e 20 74 61 62 6c 65 20 2a 2f 0a 0a 20 20 69  in table */..  i
22fba 66 28 20 64 62 2d 3e 78 41 75 74 68 3d 3d 30 20  f( db->xAuth==0 
22fbb 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 44 62 20  ) return;.  iDb 
22fbc 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
22fbd 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64  oIndex(pParse->d
22fbe 62 2c 20 70 53 63 68 65 6d 61 29 3b 0a 20 20 69  b, pSchema);.  i
22fbf 66 28 20 69 44 62 3c 30 20 29 7b 0a 20 20 20 20  f( iDb<0 ){.    
22fc0 2f 2a 20 41 6e 20 61 74 74 65 6d 70 74 20 74 6f  /* An attempt to
22fc1 20 72 65 61 64 20 61 20 63 6f 6c 75 6d 6e 20 6f   read a column o
22fc2 75 74 20 6f 66 20 61 20 73 75 62 71 75 65 72 79  ut of a subquery
22fc3 20 6f 72 20 6f 74 68 65 72 0a 20 20 20 20 2a 2a   or other.    **
22fc4 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
22fc5 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 3b  . */.    return;
22fc6 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
22fc7 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f  pExpr->op==TK_CO
22fc8 4c 55 4d 4e 20 7c 7c 20 70 45 78 70 72 2d 3e 6f  LUMN || pExpr->o
22fc9 70 3d 3d 54 4b 5f 54 52 49 47 47 45 52 20 29 3b  p==TK_TRIGGER );
22fca 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70  .  if( pExpr->op
22fcb 3d 3d 54 4b 5f 54 52 49 47 47 45 52 20 29 7b 0a  ==TK_TRIGGER ){.
22fcc 20 20 20 20 70 54 61 62 20 3d 20 70 50 61 72 73      pTab = pPars
22fcd 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 3b 0a  e->pTriggerTab;.
22fce 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
22fcf 65 72 74 28 20 70 54 61 62 4c 69 73 74 20 29 3b  ert( pTabList );
22fd0 0a 20 20 20 20 66 6f 72 28 69 53 72 63 3d 30 3b  .    for(iSrc=0;
22fd1 20 41 4c 57 41 59 53 28 69 53 72 63 3c 70 54 61   ALWAYS(iSrc<pTa
22fd2 62 4c 69 73 74 2d 3e 6e 53 72 63 29 3b 20 69 53  bList->nSrc); iS
22fd3 72 63 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28  rc++){.      if(
22fd4 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d   pExpr->iTable==
22fd5 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 53 72 63  pTabList->a[iSrc
22fd6 5d 2e 69 43 75 72 73 6f 72 20 29 7b 0a 20 20 20  ].iCursor ){.   
22fd7 20 20 20 20 20 70 54 61 62 20 3d 20 70 54 61 62       pTab = pTab
22fd8 4c 69 73 74 2d 3e 61 5b 69 53 72 63 5d 2e 70 54  List->a[iSrc].pT
22fd9 61 62 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  ab;.        brea
22fda 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
22fdb 0a 20 20 7d 0a 20 20 69 43 6f 6c 20 3d 20 70 45  .  }.  iCol = pE
22fdc 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20  xpr->iColumn;.  
22fdd 69 66 28 20 4e 45 56 45 52 28 70 54 61 62 3d 3d  if( NEVER(pTab==
22fde 30 29 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20  0) ) return;..  
22fdf 69 66 28 20 69 43 6f 6c 3e 3d 30 20 29 7b 0a 20  if( iCol>=0 ){. 
22fe0 20 20 20 61 73 73 65 72 74 28 20 69 43 6f 6c 3c     assert( iCol<
22fe1 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20  pTab->nCol );.  
22fe2 20 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61    zCol = pTab->a
22fe3 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 3b  Col[iCol].zName;
22fe4 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 54 61  .  }else if( pTa
22fe5 62 2d 3e 69 50 4b 65 79 3e 3d 30 20 29 7b 0a 20  b->iPKey>=0 ){. 
22fe6 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d     assert( pTab-
22fe7 3e 69 50 4b 65 79 3c 70 54 61 62 2d 3e 6e 43 6f  >iPKey<pTab->nCo
22fe8 6c 20 29 3b 0a 20 20 20 20 7a 43 6f 6c 20 3d 20  l );.    zCol = 
22fe9 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 54 61 62 2d  pTab->aCol[pTab-
22fea 3e 69 50 4b 65 79 5d 2e 7a 4e 61 6d 65 3b 0a 20  >iPKey].zName;. 
22feb 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 43 6f 6c   }else{.    zCol
22fec 20 3d 20 22 52 4f 57 49 44 22 3b 0a 20 20 7d 0a   = "ROWID";.  }.
22fed 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30    assert( iDb>=0
22fee 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20   && iDb<db->nDb 
22fef 29 3b 0a 20 20 7a 44 42 61 73 65 20 3d 20 64 62  );.  zDBase = db
22ff0 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
22ff1 3b 0a 20 20 72 63 20 3d 20 64 62 2d 3e 78 41 75  ;.  rc = db->xAu
22ff2 74 68 28 64 62 2d 3e 70 41 75 74 68 41 72 67 2c  th(db->pAuthArg,
22ff3 20 53 51 4c 49 54 45 5f 52 45 41 44 2c 20 70 54   SQLITE_READ, pT
22ff4 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 2c  ab->zName, zCol,
22ff5 20 7a 44 42 61 73 65 2c 20 0a 20 20 20 20 20 20   zDBase, .      
22ff6 20 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73             pPars
22ff7 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 29  e->zAuthContext)
22ff8 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
22ff9 54 45 5f 49 47 4e 4f 52 45 20 29 7b 0a 20 20 20  TE_IGNORE ){.   
22ffa 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f   pExpr->op = TK_
22ffb 4e 55 4c 4c 3b 0a 20 20 7d 65 6c 73 65 20 69 66  NULL;.  }else if
22ffc 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 45 4e  ( rc==SQLITE_DEN
22ffd 59 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d  Y ){.    if( db-
22ffe 3e 6e 44 62 3e 32 20 7c 7c 20 69 44 62 21 3d 30  >nDb>2 || iDb!=0
22fff 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
23000 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
23001 2c 20 22 61 63 63 65 73 73 20 74 6f 20 25 73 2e  , "access to %s.
23002 25 73 2e 25 73 20 69 73 20 70 72 6f 68 69 62 69  %s.%s is prohibi
23003 74 65 64 22 2c 20 0a 20 20 20 20 20 20 20 20 20  ted", .         
23004 7a 44 42 61 73 65 2c 20 70 54 61 62 2d 3e 7a 4e  zDBase, pTab->zN
23005 61 6d 65 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20  ame, zCol);.    
23006 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
23007 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
23008 72 73 65 2c 20 22 61 63 63 65 73 73 20 74 6f 20  rse, "access to 
23009 25 73 2e 25 73 20 69 73 20 70 72 6f 68 69 62 69  %s.%s is prohibi
2300a 74 65 64 22 2c 70 54 61 62 2d 3e 7a 4e 61 6d 65  ted",pTab->zName
2300b 2c 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d 0a 20 20  ,zCol);.    }.  
2300c 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53    pParse->rc = S
2300d 51 4c 49 54 45 5f 41 55 54 48 3b 0a 20 20 7d 65  QLITE_AUTH;.  }e
2300e 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c 49  lse if( rc!=SQLI
2300f 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c  TE_OK ){.    sql
23010 69 74 65 41 75 74 68 42 61 64 52 65 74 75 72 6e  iteAuthBadReturn
23011 43 6f 64 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Code(pParse);.  
23012 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 6e  }.}../*.** Do an
23013 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63   authorization c
23014 68 65 63 6b 20 75 73 69 6e 67 20 74 68 65 20 63  heck using the c
23015 6f 64 65 20 61 6e 64 20 61 72 67 75 6d 65 6e 74  ode and argument
23016 73 20 67 69 76 65 6e 2e 20 20 52 65 74 75 72 6e  s given.  Return
23017 0a 2a 2a 20 65 69 74 68 65 72 20 53 51 4c 49 54  .** either SQLIT
23018 45 5f 4f 4b 20 28 7a 65 72 6f 29 20 6f 72 20 53  E_OK (zero) or S
23019 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 6f 72 20  QLITE_IGNORE or 
2301a 53 51 4c 49 54 45 5f 44 45 4e 59 2e 20 20 49 66  SQLITE_DENY.  If
2301b 20 53 51 4c 49 54 45 5f 44 45 4e 59 0a 2a 2a 20   SQLITE_DENY.** 
2301c 69 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65  is returned, the
2301d 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f 75 6e  n the error coun
2301e 74 20 61 6e 64 20 65 72 72 6f 72 20 6d 65 73 73  t and error mess
2301f 61 67 65 20 69 6e 20 70 50 61 72 73 65 20 61 72  age in pParse ar
23020 65 0a 2a 2a 20 6d 6f 64 69 66 69 65 64 20 61 70  e.** modified ap
23021 70 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2f 0a  propriately..*/.
23022 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
23023 6e 74 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  nt sqlite3AuthCh
23024 65 63 6b 28 0a 20 20 50 61 72 73 65 20 2a 70 50  eck(.  Parse *pP
23025 61 72 73 65 2c 0a 20 20 69 6e 74 20 63 6f 64 65  arse,.  int code
23026 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
23027 7a 41 72 67 31 2c 0a 20 20 63 6f 6e 73 74 20 63  zArg1,.  const c
23028 68 61 72 20 2a 7a 41 72 67 32 2c 0a 20 20 63 6f  har *zArg2,.  co
23029 6e 73 74 20 63 68 61 72 20 2a 7a 41 72 67 33 0a  nst char *zArg3.
2302a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
2302b 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
2302c 20 69 6e 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 44   int rc;..  /* D
2302d 6f 6e 27 74 20 64 6f 20 61 6e 79 20 61 75 74 68  on't do any auth
2302e 6f 72 69 7a 61 74 69 6f 6e 20 63 68 65 63 6b 73  orization checks
2302f 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65   if the database
23030 20 69 73 20 69 6e 69 74 69 61 6c 69 73 69 6e 67   is initialising
23031 0a 20 20 2a 2a 20 6f 72 20 69 66 20 74 68 65 20  .  ** or if the 
23032 70 61 72 73 65 72 20 69 73 20 62 65 69 6e 67 20  parser is being 
23033 69 6e 76 6f 6b 65 64 20 66 72 6f 6d 20 77 69 74  invoked from wit
23034 68 69 6e 20 73 71 6c 69 74 65 33 5f 64 65 63 6c  hin sqlite3_decl
23035 61 72 65 5f 76 74 61 62 2e 0a 20 20 2a 2f 0a 20  are_vtab..  */. 
23036 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75   if( db->init.bu
23037 73 79 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45  sy || IN_DECLARE
23038 5f 56 54 41 42 20 29 7b 0a 20 20 20 20 72 65 74  _VTAB ){.    ret
23039 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
2303a 20 7d 0a 0a 20 20 69 66 28 20 64 62 2d 3e 78 41   }..  if( db->xA
2303b 75 74 68 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  uth==0 ){.    re
2303c 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
2303d 20 20 7d 0a 20 20 72 63 20 3d 20 64 62 2d 3e 78    }.  rc = db->x
2303e 41 75 74 68 28 64 62 2d 3e 70 41 75 74 68 41 72  Auth(db->pAuthAr
2303f 67 2c 20 63 6f 64 65 2c 20 7a 41 72 67 31 2c 20  g, code, zArg1, 
23040 7a 41 72 67 32 2c 20 7a 41 72 67 33 2c 20 70 50  zArg2, zArg3, pP
23041 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65  arse->zAuthConte
23042 78 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  xt);.  if( rc==S
23043 51 4c 49 54 45 5f 44 45 4e 59 20 29 7b 0a 20 20  QLITE_DENY ){.  
23044 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
23045 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 74 20 61  g(pParse, "not a
23046 75 74 68 6f 72 69 7a 65 64 22 29 3b 0a 20 20 20  uthorized");.   
23047 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51   pParse->rc = SQ
23048 4c 49 54 45 5f 41 55 54 48 3b 0a 20 20 7d 65 6c  LITE_AUTH;.  }el
23049 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  se if( rc!=SQLIT
2304a 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51 4c 49  E_OK && rc!=SQLI
2304b 54 45 5f 49 47 4e 4f 52 45 20 29 7b 0a 20 20 20  TE_IGNORE ){.   
2304c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 45 4e   rc = SQLITE_DEN
2304d 59 3b 0a 20 20 20 20 73 71 6c 69 74 65 41 75 74  Y;.    sqliteAut
2304e 68 42 61 64 52 65 74 75 72 6e 43 6f 64 65 28 70  hBadReturnCode(p
2304f 50 61 72 73 65 29 3b 0a 20 20 7d 0a 20 20 72 65  Parse);.  }.  re
23050 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
23051 2a 20 50 75 73 68 20 61 6e 20 61 75 74 68 6f 72  * Push an author
23052 69 7a 61 74 69 6f 6e 20 63 6f 6e 74 65 78 74 2e  ization context.
23053 20 20 41 66 74 65 72 20 74 68 69 73 20 72 6f 75    After this rou
23054 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20  tine is called, 
23055 74 68 65 0a 2a 2a 20 7a 41 72 67 33 20 61 72 67  the.** zArg3 arg
23056 75 6d 65 6e 74 20 74 6f 20 61 75 74 68 6f 72 69  ument to authori
23057 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 73  zation callbacks
23058 20 77 69 6c 6c 20 62 65 20 7a 43 6f 6e 74 65 78   will be zContex
23059 74 20 75 6e 74 69 6c 0a 2a 2a 20 70 6f 70 70 65  t until.** poppe
2305a 64 2e 20 20 4f 72 20 69 66 20 70 50 61 72 73 65  d.  Or if pParse
2305b 3d 3d 30 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  ==0, this routin
2305c 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f  e is a no-op..*/
2305d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2305e 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 68  void sqlite3Auth
2305f 43 6f 6e 74 65 78 74 50 75 73 68 28 0a 20 20 50  ContextPush(.  P
23060 61 72 73 65 20 2a 70 50 61 72 73 65 2c 0a 20 20  arse *pParse,.  
23061 41 75 74 68 43 6f 6e 74 65 78 74 20 2a 70 43 6f  AuthContext *pCo
23062 6e 74 65 78 74 2c 20 0a 20 20 63 6f 6e 73 74 20  ntext, .  const 
23063 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74 0a 29  char *zContext.)
23064 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72  {.  assert( pPar
23065 73 65 20 29 3b 0a 20 20 70 43 6f 6e 74 65 78 74  se );.  pContext
23066 2d 3e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73  ->pParse = pPars
23067 65 3b 0a 20 20 70 43 6f 6e 74 65 78 74 2d 3e 7a  e;.  pContext->z
23068 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 70 50  AuthContext = pP
23069 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65  arse->zAuthConte
2306a 78 74 3b 0a 20 20 70 50 61 72 73 65 2d 3e 7a 41  xt;.  pParse->zA
2306b 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 7a 43 6f  uthContext = zCo
2306c 6e 74 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ntext;.}../*.** 
2306d 50 6f 70 20 61 6e 20 61 75 74 68 6f 72 69 7a 61  Pop an authoriza
2306e 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 74 68 61  tion context tha
2306f 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79  t was previously
23070 20 70 75 73 68 65 64 0a 2a 2a 20 62 79 20 73 71   pushed.** by sq
23071 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74  lite3AuthContext
23072 50 75 73 68 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  Push.*/.SQLITE_P
23073 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
23074 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 6f  te3AuthContextPo
23075 70 28 41 75 74 68 43 6f 6e 74 65 78 74 20 2a 70  p(AuthContext *p
23076 43 6f 6e 74 65 78 74 29 7b 0a 20 20 69 66 28 20  Context){.  if( 
23077 70 43 6f 6e 74 65 78 74 2d 3e 70 50 61 72 73 65  pContext->pParse
23078 20 29 7b 0a 20 20 20 20 70 43 6f 6e 74 65 78 74   ){.    pContext
23079 2d 3e 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43  ->pParse->zAuthC
2307a 6f 6e 74 65 78 74 20 3d 20 70 43 6f 6e 74 65 78  ontext = pContex
2307b 74 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b  t->zAuthContext;
2307c 0a 20 20 20 20 70 43 6f 6e 74 65 78 74 2d 3e 70  .    pContext->p
2307d 50 61 72 73 65 20 3d 20 30 3b 0a 20 20 7d 0a 7d  Parse = 0;.  }.}
2307e 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ..#endif /* SQLI
2307f 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
23080 41 54 49 4f 4e 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  ATION */../*****
23081 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
23082 20 61 75 74 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   auth.c ********
23083 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23084 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23085 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
23086 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
23087 66 69 6c 65 20 62 75 69 6c 64 2e 63 20 2a 2a 2a  file build.c ***
23088 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23089 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2308a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
2308b 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
2308c 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
2308d 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
2308e 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
2308f 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
23090 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
23091 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
23092 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
23093 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
23094 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
23095 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
23096 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
23097 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
23098 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
23099 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
2309a 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
2309b 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
2309c 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
2309d 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
2309e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2309f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
230a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
230a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
230a2 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
230a3 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20 72 6f  ntains C code ro
230a4 75 74 69 6e 65 73 20 74 68 61 74 20 61 72 65 20  utines that are 
230a5 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 53 51  called by the SQ
230a6 4c 69 74 65 20 70 61 72 73 65 72 0a 2a 2a 20 77  Lite parser.** w
230a7 68 65 6e 20 73 79 6e 74 61 78 20 72 75 6c 65 73  hen syntax rules
230a8 20 61 72 65 20 72 65 64 75 63 65 64 2e 20 20 54   are reduced.  T
230a9 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74  he routines in t
230aa 68 69 73 20 66 69 6c 65 20 68 61 6e 64 6c 65 20  his file handle 
230ab 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67  the.** following
230ac 20 6b 69 6e 64 73 20 6f 66 20 53 51 4c 20 73 79   kinds of SQL sy
230ad 6e 74 61 78 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ntax:.**.**     
230ae 43 52 45 41 54 45 20 54 41 42 4c 45 0a 2a 2a 20  CREATE TABLE.** 
230af 20 20 20 20 44 52 4f 50 20 54 41 42 4c 45 0a 2a      DROP TABLE.*
230b0 2a 20 20 20 20 20 43 52 45 41 54 45 20 49 4e 44  *     CREATE IND
230b1 45 58 0a 2a 2a 20 20 20 20 20 44 52 4f 50 20 49  EX.**     DROP I
230b2 4e 44 45 58 0a 2a 2a 20 20 20 20 20 63 72 65 61  NDEX.**     crea
230b3 74 69 6e 67 20 49 44 20 6c 69 73 74 73 0a 2a 2a  ting ID lists.**
230b4 20 20 20 20 20 42 45 47 49 4e 20 54 52 41 4e 53       BEGIN TRANS
230b5 41 43 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 43 4f  ACTION.**     CO
230b6 4d 4d 49 54 0a 2a 2a 20 20 20 20 20 52 4f 4c 4c  MMIT.**     ROLL
230b7 42 41 43 4b 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  BACK.**.** $Id: 
230b8 62 75 69 6c 64 2e 63 2c 76 20 31 2e 35 35 37 20  build.c,v 1.557 
230b9 32 30 30 39 2f 30 37 2f 32 34 20 31 37 3a 35 38  2009/07/24 17:58
230ba 3a 35 33 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :53 danielk1977 
230bb 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  Exp $.*/../*.** 
230bc 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
230bd 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 20 6e 65  called when a ne
230be 77 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  w SQL statement 
230bf 69 73 20 62 65 67 69 6e 6e 69 6e 67 20 74 6f 0a  is beginning to.
230c0 2a 2a 20 62 65 20 70 61 72 73 65 64 2e 20 20 49  ** be parsed.  I
230c1 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 70 50  nitialize the pP
230c2 61 72 73 65 20 73 74 72 75 63 74 75 72 65 20 61  arse structure a
230c3 73 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 53 51 4c  s needed..*/.SQL
230c4 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
230c5 20 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61 72   sqlite3BeginPar
230c6 73 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  se(Parse *pParse
230c7 2c 20 69 6e 74 20 65 78 70 6c 61 69 6e 46 6c 61  , int explainFla
230c8 67 29 7b 0a 20 20 70 50 61 72 73 65 2d 3e 65 78  g){.  pParse->ex
230c9 70 6c 61 69 6e 20 3d 20 28 75 38 29 65 78 70 6c  plain = (u8)expl
230ca 61 69 6e 46 6c 61 67 3b 0a 20 20 70 50 61 72 73  ainFlag;.  pPars
230cb 65 2d 3e 6e 56 61 72 20 3d 20 30 3b 0a 7d 0a 0a  e->nVar = 0;.}..
230cc 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
230cd 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
230ce 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 61 62 6c 65  ./*.** The Table
230cf 4c 6f 63 6b 20 73 74 72 75 63 74 75 72 65 20 69  Lock structure i
230d0 73 20 6f 6e 6c 79 20 75 73 65 64 20 62 79 20 74  s only used by t
230d1 68 65 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c  he sqlite3TableL
230d2 6f 63 6b 28 29 20 61 6e 64 0a 2a 2a 20 63 6f 64  ock() and.** cod
230d3 65 54 61 62 6c 65 4c 6f 63 6b 73 28 29 20 66 75  eTableLocks() fu
230d4 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 72 75  nctions..*/.stru
230d5 63 74 20 54 61 62 6c 65 4c 6f 63 6b 20 7b 0a 20  ct TableLock {. 
230d6 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20   int iDb;       
230d7 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
230d8 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67  abase containing
230d9 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65   the table to be
230da 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 69 6e 74   locked */.  int
230db 20 69 54 61 62 3b 20 20 20 20 20 20 20 20 20 20   iTab;          
230dc 20 20 2f 2a 20 54 68 65 20 72 6f 6f 74 20 70 61    /* The root pa
230dd 67 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  ge of the table 
230de 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a  to be locked */.
230df 20 20 75 38 20 69 73 57 72 69 74 65 4c 6f 63 6b    u8 isWriteLock
230e0 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66  ;      /* True f
230e1 6f 72 20 77 72 69 74 65 20 6c 6f 63 6b 2e 20 20  or write lock.  
230e2 46 61 6c 73 65 20 66 6f 72 20 61 20 72 65 61 64  False for a read
230e3 20 6c 6f 63 6b 20 2a 2f 0a 20 20 63 6f 6e 73 74   lock */.  const
230e4 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20   char *zName;   
230e5 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74  /* Name of the t
230e6 61 62 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  able */.};../*.*
230e7 2a 20 52 65 63 6f 72 64 20 74 68 65 20 66 61 63  * Record the fac
230e8 74 20 74 68 61 74 20 77 65 20 77 61 6e 74 20 74  t that we want t
230e9 6f 20 6c 6f 63 6b 20 61 20 74 61 62 6c 65 20 61  o lock a table a
230ea 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20 0a 2a 2a  t run-time.  .**
230eb 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f  .** The table to
230ec 20 62 65 20 6c 6f 63 6b 65 64 20 68 61 73 20 72   be locked has r
230ed 6f 6f 74 20 70 61 67 65 20 69 54 61 62 20 61 6e  oot page iTab an
230ee 64 20 69 73 20 66 6f 75 6e 64 20 69 6e 20 64 61  d is found in da
230ef 74 61 62 61 73 65 20 69 44 62 2e 0a 2a 2a 20 41  tabase iDb..** A
230f0 20 72 65 61 64 20 6f 72 20 61 20 77 72 69 74 65   read or a write
230f1 20 6c 6f 63 6b 20 63 61 6e 20 62 65 20 74 61 6b   lock can be tak
230f2 65 6e 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20  en depending on 
230f3 69 73 57 72 69 74 65 6c 6f 63 6b 2e 0a 2a 2a 0a  isWritelock..**.
230f4 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
230f5 6a 75 73 74 20 72 65 63 6f 72 64 73 20 74 68 65  just records the
230f6 20 66 61 63 74 20 74 68 61 74 20 74 68 65 20 6c   fact that the l
230f7 6f 63 6b 20 69 73 20 64 65 73 69 72 65 64 2e 20  ock is desired. 
230f8 20 54 68 65 0a 2a 2a 20 63 6f 64 65 20 74 6f 20   The.** code to 
230f9 6d 61 6b 65 20 74 68 65 20 6c 6f 63 6b 20 6f 63  make the lock oc
230fa 63 75 72 20 69 73 20 67 65 6e 65 72 61 74 65 64  cur is generated
230fb 20 62 79 20 61 20 6c 61 74 65 72 20 63 61 6c 6c   by a later call
230fc 20 74 6f 0a 2a 2a 20 63 6f 64 65 54 61 62 6c 65   to.** codeTable
230fd 4c 6f 63 6b 73 28 29 20 77 68 69 63 68 20 6f 63  Locks() which oc
230fe 63 75 72 73 20 64 75 72 69 6e 67 20 73 71 6c 69  curs during sqli
230ff 74 65 33 46 69 6e 69 73 68 43 6f 64 69 6e 67 28  te3FinishCoding(
23100 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
23101 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
23102 33 54 61 62 6c 65 4c 6f 63 6b 28 0a 20 20 50 61  3TableLock(.  Pa
23103 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
23104 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
23105 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62  ext */.  int iDb
23106 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  ,           /* I
23107 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61  ndex of the data
23108 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  base containing 
23109 74 68 65 20 74 61 62 6c 65 20 74 6f 20 6c 6f 63  the table to loc
2310a 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 2c  k */.  int iTab,
2310b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f            /* Roo
2310c 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66  t page number of
2310d 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65   the table to be
2310e 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 38 20   locked */.  u8 
2310f 69 73 57 72 69 74 65 4c 6f 63 6b 2c 20 20 20 20  isWriteLock,    
23110 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 77 72  /* True for a wr
23111 69 74 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 63 6f  ite lock */.  co
23112 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20  nst char *zName 
23113 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
23114 74 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b  table to be lock
23115 65 64 20 2a 2f 0a 29 7b 0a 20 20 50 61 72 73 65  ed */.){.  Parse
23116 20 2a 70 54 6f 70 6c 65 76 65 6c 20 3d 20 73 71   *pToplevel = sq
23117 6c 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76  lite3ParseToplev
23118 65 6c 28 70 50 61 72 73 65 29 3b 0a 20 20 69 6e  el(pParse);.  in
23119 74 20 69 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65  t i;.  int nByte
2311a 73 3b 0a 20 20 54 61 62 6c 65 4c 6f 63 6b 20 2a  s;.  TableLock *
2311b 70 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62  p;.  assert( iDb
2311c 3e 3d 30 20 29 3b 0a 0a 20 20 66 6f 72 28 69 3d  >=0 );..  for(i=
2311d 30 3b 20 69 3c 70 54 6f 70 6c 65 76 65 6c 2d 3e  0; i<pToplevel->
2311e 6e 54 61 62 6c 65 4c 6f 63 6b 3b 20 69 2b 2b 29  nTableLock; i++)
2311f 7b 0a 20 20 20 20 70 20 3d 20 26 70 54 6f 70 6c  {.    p = &pTopl
23120 65 76 65 6c 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b  evel->aTableLock
23121 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  [i];.    if( p->
23122 69 44 62 3d 3d 69 44 62 20 26 26 20 70 2d 3e 69  iDb==iDb && p->i
23123 54 61 62 3d 3d 69 54 61 62 20 29 7b 0a 20 20 20  Tab==iTab ){.   
23124 20 20 20 70 2d 3e 69 73 57 72 69 74 65 4c 6f 63     p->isWriteLoc
23125 6b 20 3d 20 28 70 2d 3e 69 73 57 72 69 74 65 4c  k = (p->isWriteL
23126 6f 63 6b 20 7c 7c 20 69 73 57 72 69 74 65 4c 6f  ock || isWriteLo
23127 63 6b 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  ck);.      retur
23128 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  n;.    }.  }..  
23129 6e 42 79 74 65 73 20 3d 20 73 69 7a 65 6f 66 28  nBytes = sizeof(
2312a 54 61 62 6c 65 4c 6f 63 6b 29 20 2a 20 28 70 54  TableLock) * (pT
2312b 6f 70 6c 65 76 65 6c 2d 3e 6e 54 61 62 6c 65 4c  oplevel->nTableL
2312c 6f 63 6b 2b 31 29 3b 0a 20 20 70 54 6f 70 6c 65  ock+1);.  pTople
2312d 76 65 6c 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 20  vel->aTableLock 
2312e 3d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  =.      sqlite3D
2312f 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70  bReallocOrFree(p
23130 54 6f 70 6c 65 76 65 6c 2d 3e 64 62 2c 20 70 54  Toplevel->db, pT
23131 6f 70 6c 65 76 65 6c 2d 3e 61 54 61 62 6c 65 4c  oplevel->aTableL
23132 6f 63 6b 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20  ock, nBytes);.  
23133 69 66 28 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 61  if( pToplevel->a
23134 54 61 62 6c 65 4c 6f 63 6b 20 29 7b 0a 20 20 20  TableLock ){.   
23135 20 70 20 3d 20 26 70 54 6f 70 6c 65 76 65 6c 2d   p = &pToplevel-
23136 3e 61 54 61 62 6c 65 4c 6f 63 6b 5b 70 54 6f 70  >aTableLock[pTop
23137 6c 65 76 65 6c 2d 3e 6e 54 61 62 6c 65 4c 6f 63  level->nTableLoc
23138 6b 2b 2b 5d 3b 0a 20 20 20 20 70 2d 3e 69 44 62  k++];.    p->iDb
23139 20 3d 20 69 44 62 3b 0a 20 20 20 20 70 2d 3e 69   = iDb;.    p->i
2313a 54 61 62 20 3d 20 69 54 61 62 3b 0a 20 20 20 20  Tab = iTab;.    
2313b 70 2d 3e 69 73 57 72 69 74 65 4c 6f 63 6b 20 3d  p->isWriteLock =
2313c 20 69 73 57 72 69 74 65 4c 6f 63 6b 3b 0a 20 20   isWriteLock;.  
2313d 20 20 70 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e 61    p->zName = zNa
2313e 6d 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  me;.  }else{.   
2313f 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e 54 61 62   pToplevel->nTab
23140 6c 65 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20  leLock = 0;.    
23141 70 54 6f 70 6c 65 76 65 6c 2d 3e 64 62 2d 3e 6d  pToplevel->db->m
23142 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
23143 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  .  }.}../*.** Co
23144 64 65 20 61 6e 20 4f 50 5f 54 61 62 6c 65 4c 6f  de an OP_TableLo
23145 63 6b 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 66  ck instruction f
23146 6f 72 20 65 61 63 68 20 74 61 62 6c 65 20 6c 6f  or each table lo
23147 63 6b 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 73  cked by the.** s
23148 74 61 74 65 6d 65 6e 74 20 28 63 6f 6e 66 69 67  tatement (config
23149 75 72 65 64 20 62 79 20 63 61 6c 6c 73 20 74 6f  ured by calls to
2314a 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63   sqlite3TableLoc
2314b 6b 28 29 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  k())..*/.static 
2314c 76 6f 69 64 20 63 6f 64 65 54 61 62 6c 65 4c 6f  void codeTableLo
2314d 63 6b 73 28 50 61 72 73 65 20 2a 70 50 61 72 73  cks(Parse *pPars
2314e 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 56  e){.  int i;.  V
2314f 64 62 65 20 2a 70 56 64 62 65 3b 20 0a 0a 20 20  dbe *pVdbe; ..  
23150 70 56 64 62 65 20 3d 20 73 71 6c 69 74 65 33 47  pVdbe = sqlite3G
23151 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
23152 20 20 61 73 73 65 72 74 28 20 70 56 64 62 65 21    assert( pVdbe!
23153 3d 30 20 29 3b 20 2f 2a 20 73 71 6c 69 74 65 33  =0 ); /* sqlite3
23154 47 65 74 56 64 62 65 20 63 61 6e 6e 6f 74 20 66  GetVdbe cannot f
23155 61 69 6c 3a 20 56 44 42 45 20 61 6c 72 65 61 64  ail: VDBE alread
23156 79 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 0a  y allocated */..
23157 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61    for(i=0; i<pPa
23158 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 3b  rse->nTableLock;
23159 20 69 2b 2b 29 7b 0a 20 20 20 20 54 61 62 6c 65   i++){.    Table
2315a 4c 6f 63 6b 20 2a 70 20 3d 20 26 70 50 61 72 73  Lock *p = &pPars
2315b 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 5b 69 5d  e->aTableLock[i]
2315c 3b 0a 20 20 20 20 69 6e 74 20 70 31 20 3d 20 70  ;.    int p1 = p
2315d 2d 3e 69 44 62 3b 0a 20 20 20 20 73 71 6c 69 74  ->iDb;.    sqlit
2315e 65 33 56 64 62 65 41 64 64 4f 70 34 28 70 56 64  e3VdbeAddOp4(pVd
2315f 62 65 2c 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b  be, OP_TableLock
23160 2c 20 70 31 2c 20 70 2d 3e 69 54 61 62 2c 20 70  , p1, p->iTab, p
23161 2d 3e 69 73 57 72 69 74 65 4c 6f 63 6b 2c 0a 20  ->isWriteLock,. 
23162 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23163 20 20 20 20 20 70 2d 3e 7a 4e 61 6d 65 2c 20 50       p->zName, P
23164 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 7d  4_STATIC);.  }.}
23165 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65  .#else.  #define
23166 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28   codeTableLocks(
23167 78 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  x).#endif../*.**
23168 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
23169 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 61 20   called after a 
2316a 73 69 6e 67 6c 65 20 53 51 4c 20 73 74 61 74 65  single SQL state
2316b 6d 65 6e 74 20 68 61 73 20 62 65 65 6e 0a 2a 2a  ment has been.**
2316c 20 70 61 72 73 65 64 20 61 6e 64 20 61 20 56 44   parsed and a VD
2316d 42 45 20 70 72 6f 67 72 61 6d 20 74 6f 20 65 78  BE program to ex
2316e 65 63 75 74 65 20 74 68 61 74 20 73 74 61 74 65  ecute that state
2316f 6d 65 6e 74 20 68 61 73 20 62 65 65 6e 0a 2a 2a  ment has been.**
23170 20 70 72 65 70 61 72 65 64 2e 20 20 54 68 69 73   prepared.  This
23171 20 72 6f 75 74 69 6e 65 20 70 75 74 73 20 74 68   routine puts th
23172 65 20 66 69 6e 69 73 68 69 6e 67 20 74 6f 75 63  e finishing touc
23173 68 65 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 56 44  hes on the.** VD
23174 42 45 20 70 72 6f 67 72 61 6d 20 61 6e 64 20 72  BE program and r
23175 65 73 65 74 73 20 74 68 65 20 70 50 61 72 73 65  esets the pParse
23176 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 74   structure for t
23177 68 65 20 6e 65 78 74 0a 2a 2a 20 70 61 72 73 65  he next.** parse
23178 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ..**.** Note tha
23179 74 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  t if an error oc
2317a 63 75 72 72 65 64 2c 20 69 74 20 6d 69 67 68 74  curred, it might
2317b 20 62 65 20 74 68 65 20 63 61 73 65 20 74 68 61   be the case tha
2317c 74 0a 2a 2a 20 6e 6f 20 56 44 42 45 20 63 6f 64  t.** no VDBE cod
2317d 65 20 77 61 73 20 67 65 6e 65 72 61 74 65 64 2e  e was generated.
2317e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2317f 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46  TE void sqlite3F
23180 69 6e 69 73 68 43 6f 64 69 6e 67 28 50 61 72 73  inishCoding(Pars
23181 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 73 71  e *pParse){.  sq
23182 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 56 64 62  lite3 *db;.  Vdb
23183 65 20 2a 76 3b 0a 0a 20 20 64 62 20 3d 20 70 50  e *v;..  db = pP
23184 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20  arse->db;.  if( 
23185 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
23186 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28   ) return;.  if(
23187 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 20   pParse->nested 
23188 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
23189 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 20 72  pParse->nErr ) r
2318a 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 42 65 67  eturn;..  /* Beg
2318b 69 6e 20 62 79 20 67 65 6e 65 72 61 74 69 6e 67  in by generating
2318c 20 73 6f 6d 65 20 74 65 72 6d 69 6e 61 74 69 6f   some terminatio
2318d 6e 20 63 6f 64 65 20 61 74 20 74 68 65 20 65 6e  n code at the en
2318e 64 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 76 64  d of the.  ** vd
2318f 62 65 20 70 72 6f 67 72 61 6d 0a 20 20 2a 2f 0a  be program.  */.
23190 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
23191 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
23192 61 73 73 65 72 74 28 20 21 70 50 61 72 73 65 2d  assert( !pParse-
23193 3e 69 73 4d 75 6c 74 69 57 72 69 74 65 20 0a 20  >isMultiWrite . 
23194 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33        || sqlite3
23195 56 64 62 65 41 73 73 65 72 74 4d 61 79 41 62 6f  VdbeAssertMayAbo
23196 72 74 28 76 2c 20 70 50 61 72 73 65 2d 3e 6d 61  rt(v, pParse->ma
23197 79 41 62 6f 72 74 29 29 3b 0a 20 20 69 66 28 20  yAbort));.  if( 
23198 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  v ){.    sqlite3
23199 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50  VdbeAddOp0(v, OP
2319a 5f 48 61 6c 74 29 3b 0a 0a 20 20 20 20 2f 2a 20  _Halt);..    /* 
2319b 54 68 65 20 63 6f 6f 6b 69 65 20 6d 61 73 6b 20  The cookie mask 
2319c 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 62 69 74  contains one bit
2319d 20 66 6f 72 20 65 61 63 68 20 64 61 74 61 62 61   for each databa
2319e 73 65 20 66 69 6c 65 20 6f 70 65 6e 2e 0a 20 20  se file open..  
2319f 20 20 2a 2a 20 28 42 69 74 20 30 20 69 73 20 66    ** (Bit 0 is f
231a0 6f 72 20 6d 61 69 6e 2c 20 62 69 74 20 31 20 69  or main, bit 1 i
231a1 73 20 66 6f 72 20 74 65 6d 70 2c 20 61 6e 64 20  s for temp, and 
231a2 73 6f 20 66 6f 72 74 68 2e 29 20 20 42 69 74 73  so forth.)  Bits
231a3 20 61 72 65 0a 20 20 20 20 2a 2a 20 73 65 74 20   are.    ** set 
231a4 66 6f 72 20 65 61 63 68 20 64 61 74 61 62 61 73  for each databas
231a5 65 20 74 68 61 74 20 69 73 20 75 73 65 64 2e 20  e that is used. 
231a6 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
231a7 6f 20 73 74 61 72 74 20 61 0a 20 20 20 20 2a 2a  o start a.    **
231a8 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20   transaction on 
231a9 65 61 63 68 20 75 73 65 64 20 64 61 74 61 62 61  each used databa
231aa 73 65 20 61 6e 64 20 74 6f 20 76 65 72 69 66 79  se and to verify
231ab 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b   the schema cook
231ac 69 65 0a 20 20 20 20 2a 2a 20 6f 6e 20 65 61 63  ie.    ** on eac
231ad 68 20 75 73 65 64 20 64 61 74 61 62 61 73 65 2e  h used database.
231ae 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
231af 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f  pParse->cookieGo
231b0 74 6f 3e 30 20 29 7b 0a 20 20 20 20 20 20 75 33  to>0 ){.      u3
231b1 32 20 6d 61 73 6b 3b 0a 20 20 20 20 20 20 69 6e  2 mask;.      in
231b2 74 20 69 44 62 3b 0a 20 20 20 20 20 20 73 71 6c  t iDb;.      sql
231b3 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
231b4 28 76 2c 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b  (v, pParse->cook
231b5 69 65 47 6f 74 6f 2d 31 29 3b 0a 20 20 20 20 20  ieGoto-1);.     
231b6 20 66 6f 72 28 69 44 62 3d 30 2c 20 6d 61 73 6b   for(iDb=0, mask
231b7 3d 31 3b 20 69 44 62 3c 64 62 2d 3e 6e 44 62 3b  =1; iDb<db->nDb;
231b8 20 6d 61 73 6b 3c 3c 3d 31 2c 20 69 44 62 2b 2b   mask<<=1, iDb++
231b9 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 28  ){.        if( (
231ba 6d 61 73 6b 20 26 20 70 50 61 72 73 65 2d 3e 63  mask & pParse->c
231bb 6f 6f 6b 69 65 4d 61 73 6b 29 3d 3d 30 20 29 20  ookieMask)==0 ) 
231bc 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
231bd 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65    sqlite3VdbeUse
231be 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0a  sBtree(v, iDb);.
231bf 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
231c0 64 62 65 41 64 64 4f 70 32 28 76 2c 4f 50 5f 54  dbeAddOp2(v,OP_T
231c1 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 44 62 2c  ransaction, iDb,
231c2 20 28 6d 61 73 6b 20 26 20 70 50 61 72 73 65 2d   (mask & pParse-
231c3 3e 77 72 69 74 65 4d 61 73 6b 29 21 3d 30 29 3b  >writeMask)!=0);
231c4 0a 20 20 20 20 20 20 20 20 69 66 28 20 64 62 2d  .        if( db-
231c5 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30 20 29 7b  >init.busy==0 ){
231c6 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
231c7 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 4f  e3VdbeAddOp2(v,O
231c8 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 2c 20  P_VerifyCookie, 
231c9 69 44 62 2c 20 70 50 61 72 73 65 2d 3e 63 6f 6f  iDb, pParse->coo
231ca 6b 69 65 56 61 6c 75 65 5b 69 44 62 5d 29 3b 0a  kieValue[iDb]);.
231cb 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
231cc 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
231cd 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
231ce 4c 45 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20  LE.      {.     
231cf 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20     int i;.      
231d0 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61    for(i=0; i<pPa
231d1 72 73 65 2d 3e 6e 56 74 61 62 4c 6f 63 6b 3b 20  rse->nVtabLock; 
231d2 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  i++){.          
231d3 63 68 61 72 20 2a 76 74 61 62 20 3d 20 28 63 68  char *vtab = (ch
231d4 61 72 20 2a 29 73 71 6c 69 74 65 33 47 65 74 56  ar *)sqlite3GetV
231d5 54 61 62 6c 65 28 64 62 2c 20 70 50 61 72 73 65  Table(db, pParse
231d6 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 5b 69 5d 29  ->apVtabLock[i])
231d7 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
231d8 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
231d9 20 4f 50 5f 56 42 65 67 69 6e 2c 20 30 2c 20 30   OP_VBegin, 0, 0
231da 2c 20 30 2c 20 76 74 61 62 2c 20 50 34 5f 56 54  , 0, vtab, P4_VT
231db 41 42 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  AB);.        }. 
231dc 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e         pParse->n
231dd 56 74 61 62 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20  VtabLock = 0;.  
231de 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20      }.#endif..  
231df 20 20 20 20 2f 2a 20 4f 6e 63 65 20 61 6c 6c 20      /* Once all 
231e0 74 68 65 20 63 6f 6f 6b 69 65 73 20 68 61 76 65  the cookies have
231e1 20 62 65 65 6e 20 76 65 72 69 66 69 65 64 20 61   been verified a
231e2 6e 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20  nd transactions 
231e3 6f 70 65 6e 65 64 2c 20 0a 20 20 20 20 20 20 2a  opened, .      *
231e4 2a 20 6f 62 74 61 69 6e 20 74 68 65 20 72 65 71  * obtain the req
231e5 75 69 72 65 64 20 74 61 62 6c 65 2d 6c 6f 63 6b  uired table-lock
231e6 73 2e 20 54 68 69 73 20 69 73 20 61 20 6e 6f 2d  s. This is a no-
231e7 6f 70 20 75 6e 6c 65 73 73 20 74 68 65 20 0a 20  op unless the . 
231e8 20 20 20 20 20 2a 2a 20 73 68 61 72 65 64 2d 63       ** shared-c
231e9 61 63 68 65 20 66 65 61 74 75 72 65 20 69 73 20  ache feature is 
231ea 65 6e 61 62 6c 65 64 2e 0a 20 20 20 20 20 20 2a  enabled..      *
231eb 2f 0a 20 20 20 20 20 20 63 6f 64 65 54 61 62 6c  /.      codeTabl
231ec 65 4c 6f 63 6b 73 28 70 50 61 72 73 65 29 3b 0a  eLocks(pParse);.
231ed 0a 20 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61  .      /* Initia
231ee 6c 69 7a 65 20 61 6e 79 20 41 55 54 4f 49 4e 43  lize any AUTOINC
231ef 52 45 4d 45 4e 54 20 64 61 74 61 20 73 74 72 75  REMENT data stru
231f0 63 74 75 72 65 73 20 72 65 71 75 69 72 65 64 2e  ctures required.
231f1 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
231f2 73 71 6c 69 74 65 33 41 75 74 6f 69 6e 63 72 65  sqlite3Autoincre
231f3 6d 65 6e 74 42 65 67 69 6e 28 70 50 61 72 73 65  mentBegin(pParse
231f4 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 46 69 6e  );..      /* Fin
231f5 61 6c 6c 79 2c 20 6a 75 6d 70 20 62 61 63 6b 20  ally, jump back 
231f6 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  to the beginning
231f7 20 6f 66 20 74 68 65 20 65 78 65 63 75 74 61 62   of the executab
231f8 6c 65 20 63 6f 64 65 2e 20 2a 2f 0a 20 20 20 20  le code. */.    
231f9 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
231fa 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
231fb 30 2c 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69  0, pParse->cooki
231fc 65 47 6f 74 6f 29 3b 0a 20 20 20 20 7d 0a 20 20  eGoto);.    }.  
231fd 7d 0a 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68 65  }...  /* Get the
231fe 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 72 65   VDBE program re
231ff 61 64 79 20 66 6f 72 20 65 78 65 63 75 74 69 6f  ady for executio
23200 6e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 76 20 26  n.  */.  if( v &
23201 26 20 41 4c 57 41 59 53 28 70 50 61 72 73 65 2d  & ALWAYS(pParse-
23202 3e 6e 45 72 72 3d 3d 30 29 20 26 26 20 21 64 62  >nErr==0) && !db
23203 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
23204 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  {.#ifdef SQLITE_
23205 44 45 42 55 47 0a 20 20 20 20 46 49 4c 45 20 2a  DEBUG.    FILE *
23206 74 72 61 63 65 20 3d 20 28 64 62 2d 3e 66 6c 61  trace = (db->fla
23207 67 73 20 26 20 53 51 4c 49 54 45 5f 56 64 62 65  gs & SQLITE_Vdbe
23208 54 72 61 63 65 29 21 3d 30 20 3f 20 73 74 64 6f  Trace)!=0 ? stdo
23209 75 74 20 3a 20 30 3b 0a 20 20 20 20 73 71 6c 69  ut : 0;.    sqli
2320a 74 65 33 56 64 62 65 54 72 61 63 65 28 76 2c 20  te3VdbeTrace(v, 
2320b 74 72 61 63 65 29 3b 0a 23 65 6e 64 69 66 0a 20  trace);.#endif. 
2320c 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73     assert( pPars
2320d 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3d 3d  e->iCacheLevel==
2320e 30 20 29 3b 20 20 2f 2a 20 44 69 73 61 62 6c 65  0 );  /* Disable
2320f 73 20 61 6e 64 20 72 65 2d 65 6e 61 62 6c 65 73  s and re-enables
23210 20 6d 61 74 63 68 20 2a 2f 0a 20 20 20 20 73 71   match */.    sq
23211 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61  lite3VdbeMakeRea
23212 64 79 28 76 2c 20 70 50 61 72 73 65 2d 3e 6e 56  dy(v, pParse->nV
23213 61 72 2c 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  ar, pParse->nMem
23214 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
23215 20 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73             pPars
23216 65 2d 3e 6e 54 61 62 2c 20 70 50 61 72 73 65 2d  e->nTab, pParse-
23217 3e 6e 4d 61 78 41 72 67 2c 20 70 50 61 72 73 65  >nMaxArg, pParse
23218 2d 3e 65 78 70 6c 61 69 6e 2c 0a 20 20 20 20 20  ->explain,.     
23219 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2321a 20 20 20 20 70 50 61 72 73 65 2d 3e 69 73 4d 75      pParse->isMu
2321b 6c 74 69 57 72 69 74 65 20 26 26 20 70 50 61 72  ltiWrite && pPar
2321c 73 65 2d 3e 6d 61 79 41 62 6f 72 74 29 3b 0a 20  se->mayAbort);. 
2321d 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20     pParse->rc = 
2321e 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20  SQLITE_DONE;.   
2321f 20 70 50 61 72 73 65 2d 3e 63 6f 6c 4e 61 6d 65   pParse->colName
23220 73 53 65 74 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  sSet = 0;.  }els
23221 65 20 69 66 28 20 70 50 61 72 73 65 2d 3e 72 63  e if( pParse->rc
23222 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
23223 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20     pParse->rc = 
23224 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
23225 7d 0a 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  }.  pParse->nTab
23226 20 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e   = 0;.  pParse->
23227 6e 4d 65 6d 20 3d 20 30 3b 0a 20 20 70 50 61 72  nMem = 0;.  pPar
23228 73 65 2d 3e 6e 53 65 74 20 3d 20 30 3b 0a 20 20  se->nSet = 0;.  
23229 70 50 61 72 73 65 2d 3e 6e 56 61 72 20 3d 20 30  pParse->nVar = 0
2322a 3b 0a 20 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b  ;.  pParse->cook
2322b 69 65 4d 61 73 6b 20 3d 20 30 3b 0a 20 20 70 50  ieMask = 0;.  pP
2322c 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f  arse->cookieGoto
2322d 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52   = 0;.}../*.** R
2322e 75 6e 20 74 68 65 20 70 61 72 73 65 72 20 61 6e  un the parser an
2322f 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72  d code generator
23230 20 72 65 63 75 72 73 69 76 65 6c 79 20 69 6e 20   recursively in 
23231 6f 72 64 65 72 20 74 6f 20 67 65 6e 65 72 61 74  order to generat
23232 65 0a 2a 2a 20 63 6f 64 65 20 66 6f 72 20 74 68  e.** code for th
23233 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  e SQL statement 
23234 67 69 76 65 6e 20 6f 6e 74 6f 20 74 68 65 20 65  given onto the e
23235 6e 64 20 6f 66 20 74 68 65 20 70 50 61 72 73 65  nd of the pParse
23236 20 63 6f 6e 74 65 78 74 0a 2a 2a 20 63 75 72 72   context.** curr
23237 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73  ently under cons
23238 74 72 75 63 74 69 6f 6e 2e 20 20 57 68 65 6e 20  truction.  When 
23239 74 68 65 20 70 61 72 73 65 72 20 69 73 20 72 75  the parser is ru
2323a 6e 20 72 65 63 75 72 73 69 76 65 6c 79 0a 2a 2a  n recursively.**
2323b 20 74 68 69 73 20 77 61 79 2c 20 74 68 65 20 66   this way, the f
2323c 69 6e 61 6c 20 4f 50 5f 48 61 6c 74 20 69 73 20  inal OP_Halt is 
2323d 6e 6f 74 20 61 70 70 65 6e 64 65 64 20 61 6e 64  not appended and
2323e 20 6f 74 68 65 72 20 69 6e 69 74 69 61 6c 69 7a   other initializ
2323f 61 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 66 69 6e  ation.** and fin
23240 61 6c 69 7a 61 74 69 6f 6e 20 73 74 65 70 73 20  alization steps 
23241 61 72 65 20 6f 6d 69 74 74 65 64 20 62 65 63 61  are omitted beca
23242 75 73 65 20 74 68 6f 73 65 20 61 72 65 20 68 61  use those are ha
23243 6e 64 6c 69 6e 67 20 62 79 20 74 68 65 0a 2a 2a  ndling by the.**
23244 20 6f 75 74 65 72 6d 6f 73 74 20 70 61 72 73 65   outermost parse
23245 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 20 65 76 65  r..**.** Not eve
23246 72 79 74 68 69 6e 67 20 69 73 20 6e 65 73 74 61  rything is nesta
23247 62 6c 65 2e 20 20 54 68 69 73 20 66 61 63 69 6c  ble.  This facil
23248 69 74 79 20 69 73 20 64 65 73 69 67 6e 65 64 20  ity is designed 
23249 74 6f 20 70 65 72 6d 69 74 0a 2a 2a 20 49 4e 53  to permit.** INS
2324a 45 52 54 2c 20 55 50 44 41 54 45 2c 20 61 6e 64  ERT, UPDATE, and
2324b 20 44 45 4c 45 54 45 20 6f 70 65 72 61 74 69 6f   DELETE operatio
2324c 6e 73 20 61 67 61 69 6e 73 74 20 53 51 4c 49 54  ns against SQLIT
2324d 45 5f 4d 41 53 54 45 52 2e 20 20 55 73 65 0a 2a  E_MASTER.  Use.*
2324e 2a 20 63 61 72 65 20 69 66 20 79 6f 75 20 64 65  * care if you de
2324f 63 69 64 65 20 74 6f 20 74 72 79 20 74 6f 20 75  cide to try to u
23250 73 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  se this routine 
23251 66 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 70  for some other p
23252 75 72 70 6f 73 65 73 2e 0a 2a 2f 0a 53 51 4c 49  urposes..*/.SQLI
23253 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
23254 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72  sqlite3NestedPar
23255 73 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  se(Parse *pParse
23256 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  , const char *zF
23257 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76  ormat, ...){.  v
23258 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61  a_list ap;.  cha
23259 72 20 2a 7a 53 71 6c 3b 0a 20 20 63 68 61 72 20  r *zSql;.  char 
2325a 2a 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20  *zErrMsg = 0;.  
2325b 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
2325c 61 72 73 65 2d 3e 64 62 3b 0a 23 20 64 65 66 69  arse->db;.# defi
2325d 6e 65 20 53 41 56 45 5f 53 5a 20 20 28 73 69 7a  ne SAVE_SZ  (siz
2325e 65 6f 66 28 50 61 72 73 65 29 20 2d 20 6f 66 66  eof(Parse) - off
2325f 73 65 74 6f 66 28 50 61 72 73 65 2c 6e 56 61 72  setof(Parse,nVar
23260 29 29 0a 20 20 63 68 61 72 20 73 61 76 65 42 75  )).  char saveBu
23261 66 5b 53 41 56 45 5f 53 5a 5d 3b 0a 0a 20 20 69  f[SAVE_SZ];..  i
23262 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20  f( pParse->nErr 
23263 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65  ) return;.  asse
23264 72 74 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74  rt( pParse->nest
23265 65 64 3c 31 30 20 29 3b 20 20 2f 2a 20 4e 65 73  ed<10 );  /* Nes
23266 74 69 6e 67 20 73 68 6f 75 6c 64 20 6f 6e 6c 79  ting should only
23267 20 62 65 20 6f 66 20 6c 69 6d 69 74 65 64 20 64   be of limited d
23268 65 70 74 68 20 2a 2f 0a 20 20 76 61 5f 73 74 61  epth */.  va_sta
23269 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b  rt(ap, zFormat);
2326a 0a 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65  .  zSql = sqlite
2326b 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46  3VMPrintf(db, zF
2326c 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61  ormat, ap);.  va
2326d 5f 65 6e 64 28 61 70 29 3b 0a 20 20 69 66 28 20  _end(ap);.  if( 
2326e 7a 53 71 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 72  zSql==0 ){.    r
2326f 65 74 75 72 6e 3b 20 20 20 2f 2a 20 41 20 6d 61  eturn;   /* A ma
23270 6c 6c 6f 63 20 6d 75 73 74 20 68 61 76 65 20 66  lloc must have f
23271 61 69 6c 65 64 20 2a 2f 0a 20 20 7d 0a 20 20 70  ailed */.  }.  p
23272 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 2b 2b 3b  Parse->nested++;
23273 0a 20 20 6d 65 6d 63 70 79 28 73 61 76 65 42 75  .  memcpy(saveBu
23274 66 2c 20 26 70 50 61 72 73 65 2d 3e 6e 56 61 72  f, &pParse->nVar
23275 2c 20 53 41 56 45 5f 53 5a 29 3b 0a 20 20 6d 65  , SAVE_SZ);.  me
23276 6d 73 65 74 28 26 70 50 61 72 73 65 2d 3e 6e 56  mset(&pParse->nV
23277 61 72 2c 20 30 2c 20 53 41 56 45 5f 53 5a 29 3b  ar, 0, SAVE_SZ);
23278 0a 20 20 73 71 6c 69 74 65 33 52 75 6e 50 61 72  .  sqlite3RunPar
23279 73 65 72 28 70 50 61 72 73 65 2c 20 7a 53 71 6c  ser(pParse, zSql
2327a 2c 20 26 7a 45 72 72 4d 73 67 29 3b 0a 20 20 73  , &zErrMsg);.  s
2327b 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2327c 20 7a 45 72 72 4d 73 67 29 3b 0a 20 20 73 71 6c   zErrMsg);.  sql
2327d 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
2327e 53 71 6c 29 3b 0a 20 20 6d 65 6d 63 70 79 28 26  Sql);.  memcpy(&
2327f 70 50 61 72 73 65 2d 3e 6e 56 61 72 2c 20 73 61  pParse->nVar, sa
23280 76 65 42 75 66 2c 20 53 41 56 45 5f 53 5a 29 3b  veBuf, SAVE_SZ);
23281 0a 20 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65  .  pParse->neste
23282 64 2d 2d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f  d--;.}../*.** Lo
23283 63 61 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f  cate the in-memo
23284 72 79 20 73 74 72 75 63 74 75 72 65 20 74 68 61  ry structure tha
23285 74 20 64 65 73 63 72 69 62 65 73 20 61 20 70 61  t describes a pa
23286 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73  rticular databas
23287 65 0a 2a 2a 20 74 61 62 6c 65 20 67 69 76 65 6e  e.** table given
23288 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 61   the name of tha
23289 74 20 74 61 62 6c 65 20 61 6e 64 20 28 6f 70 74  t table and (opt
2328a 69 6f 6e 61 6c 6c 79 29 20 74 68 65 20 6e 61 6d  ionally) the nam
2328b 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61  e of the.** data
2328c 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  base containing 
2328d 74 68 65 20 74 61 62 6c 65 2e 20 20 52 65 74 75  the table.  Retu
2328e 72 6e 20 4e 55 4c 4c 20 69 66 20 6e 6f 74 20 66  rn NULL if not f
2328f 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a  ound..**.** If z
23290 44 61 74 61 62 61 73 65 20 69 73 20 30 2c 20 61  Database is 0, a
23291 6c 6c 20 64 61 74 61 62 61 73 65 73 20 61 72 65  ll databases are
23292 20 73 65 61 72 63 68 65 64 20 66 6f 72 20 74 68   searched for th
23293 65 20 74 61 62 6c 65 20 61 6e 64 20 74 68 65 0a  e table and the.
23294 2a 2a 20 66 69 72 73 74 20 6d 61 74 63 68 69 6e  ** first matchin
23295 67 20 74 61 62 6c 65 20 69 73 20 72 65 74 75 72  g table is retur
23296 6e 65 64 2e 20 20 28 4e 6f 20 63 68 65 63 6b 69  ned.  (No checki
23297 6e 67 20 66 6f 72 20 64 75 70 6c 69 63 61 74 65  ng for duplicate
23298 20 74 61 62 6c 65 0a 2a 2a 20 6e 61 6d 65 73 20   table.** names 
23299 69 73 20 64 6f 6e 65 2e 29 20 20 54 68 65 20 73  is done.)  The s
2329a 65 61 72 63 68 20 6f 72 64 65 72 20 69 73 20 54  earch order is T
2329b 45 4d 50 20 66 69 72 73 74 2c 20 74 68 65 6e 20  EMP first, then 
2329c 4d 41 49 4e 2c 20 74 68 65 6e 20 61 6e 79 0a 2a  MAIN, then any.*
2329d 2a 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61  * auxiliary data
2329e 62 61 73 65 73 20 61 64 64 65 64 20 75 73 69 6e  bases added usin
2329f 67 20 74 68 65 20 41 54 54 41 43 48 20 63 6f 6d  g the ATTACH com
232a0 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  mand..**.** See 
232a1 61 6c 73 6f 20 73 71 6c 69 74 65 33 4c 6f 63 61  also sqlite3Loca
232a2 74 65 54 61 62 6c 65 28 29 2e 0a 2a 2f 0a 53 51  teTable()..*/.SQ
232a3 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 62  LITE_PRIVATE Tab
232a4 6c 65 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 54  le *sqlite3FindT
232a5 61 62 6c 65 28 73 71 6c 69 74 65 33 20 2a 64 62  able(sqlite3 *db
232a6 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  , const char *zN
232a7 61 6d 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  ame, const char 
232a8 2a 7a 44 61 74 61 62 61 73 65 29 7b 0a 20 20 54  *zDatabase){.  T
232a9 61 62 6c 65 20 2a 70 20 3d 20 30 3b 0a 20 20 69  able *p = 0;.  i
232aa 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d  nt i;.  int nNam
232ab 65 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 4e 61  e;.  assert( zNa
232ac 6d 65 21 3d 30 20 29 3b 0a 20 20 6e 4e 61 6d 65  me!=0 );.  nName
232ad 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
232ae 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 20 66 6f 72  30(zName);.  for
232af 28 69 3d 4f 4d 49 54 5f 54 45 4d 50 44 42 3b 20  (i=OMIT_TEMPDB; 
232b0 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
232b1 0a 20 20 20 20 69 6e 74 20 6a 20 3d 20 28 69 3c  .    int j = (i<
232b2 32 29 20 3f 20 69 5e 31 20 3a 20 69 3b 20 20 20  2) ? i^1 : i;   
232b3 2f 2a 20 53 65 61 72 63 68 20 54 45 4d 50 20 62  /* Search TEMP b
232b4 65 66 6f 72 65 20 4d 41 49 4e 20 2a 2f 0a 20 20  efore MAIN */.  
232b5 20 20 69 66 28 20 7a 44 61 74 61 62 61 73 65 21    if( zDatabase!
232b6 3d 30 20 26 26 20 73 71 6c 69 74 65 33 53 74 72  =0 && sqlite3Str
232b7 49 43 6d 70 28 7a 44 61 74 61 62 61 73 65 2c 20  ICmp(zDatabase, 
232b8 64 62 2d 3e 61 44 62 5b 6a 5d 2e 7a 4e 61 6d 65  db->aDb[j].zName
232b9 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  ) ) continue;.  
232ba 20 20 70 20 3d 20 73 71 6c 69 74 65 33 48 61 73    p = sqlite3Has
232bb 68 46 69 6e 64 28 26 64 62 2d 3e 61 44 62 5b 6a  hFind(&db->aDb[j
232bc 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61  ].pSchema->tblHa
232bd 73 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65  sh, zName, nName
232be 29 3b 0a 20 20 20 20 69 66 28 20 70 20 29 20 62  );.    if( p ) b
232bf 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75  reak;.  }.  retu
232c0 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c  rn p;.}../*.** L
232c1 6f 63 61 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d  ocate the in-mem
232c2 6f 72 79 20 73 74 72 75 63 74 75 72 65 20 74 68  ory structure th
232c3 61 74 20 64 65 73 63 72 69 62 65 73 20 61 20 70  at describes a p
232c4 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61  articular databa
232c5 73 65 0a 2a 2a 20 74 61 62 6c 65 20 67 69 76 65  se.** table give
232c6 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  n the name of th
232c7 61 74 20 74 61 62 6c 65 20 61 6e 64 20 28 6f 70  at table and (op
232c8 74 69 6f 6e 61 6c 6c 79 29 20 74 68 65 20 6e 61  tionally) the na
232c9 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74  me of the.** dat
232ca 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67  abase containing
232cb 20 74 68 65 20 74 61 62 6c 65 2e 20 20 52 65 74   the table.  Ret
232cc 75 72 6e 20 4e 55 4c 4c 20 69 66 20 6e 6f 74 20  urn NULL if not 
232cd 66 6f 75 6e 64 2e 20 20 41 6c 73 6f 20 6c 65 61  found.  Also lea
232ce 76 65 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 6d  ve an.** error m
232cf 65 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 65  essage in pParse
232d0 2d 3e 7a 45 72 72 4d 73 67 2e 0a 2a 2a 0a 2a 2a  ->zErrMsg..**.**
232d1 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 20   The difference 
232d2 62 65 74 77 65 65 6e 20 74 68 69 73 20 72 6f 75  between this rou
232d3 74 69 6e 65 20 61 6e 64 20 73 71 6c 69 74 65 33  tine and sqlite3
232d4 46 69 6e 64 54 61 62 6c 65 28 29 20 69 73 20 74  FindTable() is t
232d5 68 61 74 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74  hat this.** rout
232d6 69 6e 65 20 6c 65 61 76 65 73 20 61 6e 20 65 72  ine leaves an er
232d7 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70  ror message in p
232d8 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 77  Parse->zErrMsg w
232d9 68 65 72 65 0a 2a 2a 20 73 71 6c 69 74 65 33 46  here.** sqlite3F
232da 69 6e 64 54 61 62 6c 65 28 29 20 64 6f 65 73 20  indTable() does 
232db 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  not..*/.SQLITE_P
232dc 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71  RIVATE Table *sq
232dd 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65  lite3LocateTable
232de 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
232df 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f  e,         /* co
232e0 6e 74 65 78 74 20 69 6e 20 77 68 69 63 68 20 74  ntext in which t
232e1 6f 20 72 65 70 6f 72 74 20 65 72 72 6f 72 73 20  o report errors 
232e2 2a 2f 0a 20 20 69 6e 74 20 69 73 56 69 65 77 2c  */.  int isView,
232e3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
232e4 72 75 65 20 69 66 20 6c 6f 6f 6b 69 6e 67 20 66  rue if looking f
232e5 6f 72 20 61 20 56 49 45 57 20 72 61 74 68 65 72  or a VIEW rather
232e6 20 74 68 61 6e 20 61 20 54 41 42 4c 45 20 2a 2f   than a TABLE */
232e7 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
232e8 4e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 4e 61 6d  Name,     /* Nam
232e9 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 77  e of the table w
232ea 65 20 61 72 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f  e are looking fo
232eb 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  r */.  const cha
232ec 72 20 2a 7a 44 62 61 73 65 20 20 20 20 20 2f 2a  r *zDbase     /*
232ed 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74   Name of the dat
232ee 61 62 61 73 65 2e 20 20 4d 69 67 68 74 20 62 65  abase.  Might be
232ef 20 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 54 61   NULL */.){.  Ta
232f0 62 6c 65 20 2a 70 3b 0a 0a 20 20 2f 2a 20 52 65  ble *p;..  /* Re
232f1 61 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ad the database 
232f2 73 63 68 65 6d 61 2e 20 49 66 20 61 6e 20 65 72  schema. If an er
232f3 72 6f 72 20 6f 63 63 75 72 73 2c 20 6c 65 61 76  ror occurs, leav
232f4 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  e an error messa
232f5 67 65 0a 20 20 2a 2a 20 61 6e 64 20 63 6f 64 65  ge.  ** and code
232f6 20 69 6e 20 70 50 61 72 73 65 20 61 6e 64 20 72   in pParse and r
232f7 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a 20  eturn NULL. */. 
232f8 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
232f9 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d  sqlite3ReadSchem
232fa 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20  a(pParse) ){.   
232fb 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a   return 0;.  }..
232fc 20 20 70 20 3d 20 73 71 6c 69 74 65 33 46 69 6e    p = sqlite3Fin
232fd 64 54 61 62 6c 65 28 70 50 61 72 73 65 2d 3e 64  dTable(pParse->d
232fe 62 2c 20 7a 4e 61 6d 65 2c 20 7a 44 62 61 73 65  b, zName, zDbase
232ff 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b  );.  if( p==0 ){
23300 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
23301 2a 7a 4d 73 67 20 3d 20 69 73 56 69 65 77 20 3f  *zMsg = isView ?
23302 20 22 6e 6f 20 73 75 63 68 20 76 69 65 77 22 20   "no such view" 
23303 3a 20 22 6e 6f 20 73 75 63 68 20 74 61 62 6c 65  : "no such table
23304 22 3b 0a 20 20 20 20 69 66 28 20 7a 44 62 61 73  ";.    if( zDbas
23305 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  e ){.      sqlit
23306 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
23307 65 2c 20 22 25 73 3a 20 25 73 2e 25 73 22 2c 20  e, "%s: %s.%s", 
23308 7a 4d 73 67 2c 20 7a 44 62 61 73 65 2c 20 7a 4e  zMsg, zDbase, zN
23309 61 6d 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ame);.    }else{
2330a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
2330b 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
2330c 25 73 3a 20 25 73 22 2c 20 7a 4d 73 67 2c 20 7a  %s: %s", zMsg, z
2330d 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20  Name);.    }.   
2330e 20 70 50 61 72 73 65 2d 3e 63 68 65 63 6b 53 63   pParse->checkSc
2330f 68 65 6d 61 20 3d 20 31 3b 0a 20 20 7d 0a 20 20  hema = 1;.  }.  
23310 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a  return p;.}../*.
23311 2a 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 69 6e  ** Locate the in
23312 2d 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72  -memory structur
23313 65 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73  e that describes
23314 20 0a 2a 2a 20 61 20 70 61 72 74 69 63 75 6c 61   .** a particula
23315 72 20 69 6e 64 65 78 20 67 69 76 65 6e 20 74 68  r index given th
23316 65 20 6e 61 6d 65 20 6f 66 20 74 68 61 74 20 69  e name of that i
23317 6e 64 65 78 0a 2a 2a 20 61 6e 64 20 74 68 65 20  ndex.** and the 
23318 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  name of the data
23319 62 61 73 65 20 74 68 61 74 20 63 6f 6e 74 61 69  base that contai
2331a 6e 73 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a 2a  ns the index..**
2331b 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20   Return NULL if 
2331c 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a  not found..**.**
2331d 20 49 66 20 7a 44 61 74 61 62 61 73 65 20 69 73   If zDatabase is
2331e 20 30 2c 20 61 6c 6c 20 64 61 74 61 62 61 73 65   0, all database
2331f 73 20 61 72 65 20 73 65 61 72 63 68 65 64 20 66  s are searched f
23320 6f 72 20 74 68 65 0a 2a 2a 20 74 61 62 6c 65 20  or the.** table 
23321 61 6e 64 20 74 68 65 20 66 69 72 73 74 20 6d 61  and the first ma
23322 74 63 68 69 6e 67 20 69 6e 64 65 78 20 69 73 20  tching index is 
23323 72 65 74 75 72 6e 65 64 2e 20 20 28 4e 6f 20 63  returned.  (No c
23324 68 65 63 6b 69 6e 67 0a 2a 2a 20 66 6f 72 20 64  hecking.** for d
23325 75 70 6c 69 63 61 74 65 20 69 6e 64 65 78 20 6e  uplicate index n
23326 61 6d 65 73 20 69 73 20 64 6f 6e 65 2e 29 20 20  ames is done.)  
23327 54 68 65 20 73 65 61 72 63 68 20 6f 72 64 65 72  The search order
23328 20 69 73 0a 2a 2a 20 54 45 4d 50 20 66 69 72 73   is.** TEMP firs
23329 74 2c 20 74 68 65 6e 20 4d 41 49 4e 2c 20 74 68  t, then MAIN, th
2332a 65 6e 20 61 6e 79 20 61 75 78 69 6c 69 61 72 79  en any auxiliary
2332b 20 64 61 74 61 62 61 73 65 73 20 61 64 64 65 64   databases added
2332c 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 41 54  .** using the AT
2332d 54 41 43 48 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2f  TACH command..*/
2332e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2332f 49 6e 64 65 78 20 2a 73 71 6c 69 74 65 33 46 69  Index *sqlite3Fi
23330 6e 64 49 6e 64 65 78 28 73 71 6c 69 74 65 33 20  ndIndex(sqlite3 
23331 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  *db, const char 
23332 2a 7a 4e 61 6d 65 2c 20 63 6f 6e 73 74 20 63 68  *zName, const ch
23333 61 72 20 2a 7a 44 62 29 7b 0a 20 20 49 6e 64 65  ar *zDb){.  Inde
23334 78 20 2a 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20  x *p = 0;.  int 
23335 69 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d  i;.  int nName =
23336 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
23337 28 7a 4e 61 6d 65 29 3b 0a 20 20 66 6f 72 28 69  (zName);.  for(i
23338 3d 4f 4d 49 54 5f 54 45 4d 50 44 42 3b 20 69 3c  =OMIT_TEMPDB; i<
23339 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20  db->nDb; i++){. 
2333a 20 20 20 69 6e 74 20 6a 20 3d 20 28 69 3c 32 29     int j = (i<2)
2333b 20 3f 20 69 5e 31 20 3a 20 69 3b 20 20 2f 2a 20   ? i^1 : i;  /* 
2333c 53 65 61 72 63 68 20 54 45 4d 50 20 62 65 66 6f  Search TEMP befo
2333d 72 65 20 4d 41 49 4e 20 2a 2f 0a 20 20 20 20 53  re MAIN */.    S
2333e 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d  chema *pSchema =
2333f 20 64 62 2d 3e 61 44 62 5b 6a 5d 2e 70 53 63 68   db->aDb[j].pSch
23340 65 6d 61 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ema;.    assert(
23341 20 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20   pSchema );.    
23342 69 66 28 20 7a 44 62 20 26 26 20 73 71 6c 69 74  if( zDb && sqlit
23343 65 33 53 74 72 49 43 6d 70 28 7a 44 62 2c 20 64  e3StrICmp(zDb, d
23344 62 2d 3e 61 44 62 5b 6a 5d 2e 7a 4e 61 6d 65 29  b->aDb[j].zName)
23345 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
23346 20 70 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68   p = sqlite3Hash
23347 46 69 6e 64 28 26 70 53 63 68 65 6d 61 2d 3e 69  Find(&pSchema->i
23348 64 78 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 6e  dxHash, zName, n
23349 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 70  Name);.    if( p
2334a 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20   ) break;.  }.  
2334b 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a  return p;.}../*.
2334c 2a 2a 20 52 65 63 6c 61 69 6d 20 74 68 65 20 6d  ** Reclaim the m
2334d 65 6d 6f 72 79 20 75 73 65 64 20 62 79 20 61 6e  emory used by an
2334e 20 69 6e 64 65 78 0a 2a 2f 0a 73 74 61 74 69 63   index.*/.static
2334f 20 76 6f 69 64 20 66 72 65 65 49 6e 64 65 78 28   void freeIndex(
23350 49 6e 64 65 78 20 2a 70 29 7b 0a 20 20 73 71 6c  Index *p){.  sql
23351 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 70 54  ite3 *db = p->pT
23352 61 62 6c 65 2d 3e 64 62 4d 65 6d 3b 0a 23 69 66  able->dbMem;.#if
23353 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
23354 5f 41 4e 41 4c 59 5a 45 0a 20 20 73 71 6c 69 74  _ANALYZE.  sqlit
23355 65 33 44 65 6c 65 74 65 49 6e 64 65 78 53 61 6d  e3DeleteIndexSam
23356 70 6c 65 73 28 70 29 3b 0a 23 65 6e 64 69 66 0a  ples(p);.#endif.
23357 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
23358 64 62 2c 20 70 2d 3e 7a 43 6f 6c 41 66 66 29 3b  db, p->zColAff);
23359 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
2335a 28 64 62 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  (db, p);.}../*.*
2335b 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 67 69 76  * Remove the giv
2335c 65 6e 20 69 6e 64 65 78 20 66 72 6f 6d 20 74 68  en index from th
2335d 65 20 69 6e 64 65 78 20 68 61 73 68 20 74 61 62  e index hash tab
2335e 6c 65 2c 20 61 6e 64 20 66 72 65 65 0a 2a 2a 20  le, and free.** 
2335f 69 74 73 20 6d 65 6d 6f 72 79 20 73 74 72 75 63  its memory struc
23360 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tures..**.** The
23361 20 69 6e 64 65 78 20 69 73 20 72 65 6d 6f 76 65   index is remove
23362 64 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62  d from the datab
23363 61 73 65 20 68 61 73 68 20 74 61 62 6c 65 73 20  ase hash tables 
23364 62 75 74 0a 2a 2a 20 69 74 20 69 73 20 6e 6f 74  but.** it is not
23365 20 75 6e 6c 69 6e 6b 65 64 20 66 72 6f 6d 20 74   unlinked from t
23366 68 65 20 54 61 62 6c 65 20 74 68 61 74 20 69 74  he Table that it
23367 20 69 6e 64 65 78 65 73 2e 0a 2a 2a 20 55 6e 6c   indexes..** Unl
23368 69 6e 6b 69 6e 67 20 66 72 6f 6d 20 74 68 65 20  inking from the 
23369 54 61 62 6c 65 20 6d 75 73 74 20 62 65 20 64 6f  Table must be do
2336a 6e 65 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e  ne by the callin
2336b 67 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73  g function..*/.s
2336c 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74  tatic void sqlit
2336d 65 33 44 65 6c 65 74 65 49 6e 64 65 78 28 49 6e  e3DeleteIndex(In
2336e 64 65 78 20 2a 70 29 7b 0a 20 20 49 6e 64 65 78  dex *p){.  Index
2336f 20 2a 70 4f 6c 64 3b 0a 20 20 63 6f 6e 73 74 20   *pOld;.  const 
23370 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 2d  char *zName = p-
23371 3e 7a 4e 61 6d 65 3b 0a 0a 20 20 70 4f 6c 64 20  >zName;..  pOld 
23372 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73  = sqlite3HashIns
23373 65 72 74 28 26 70 2d 3e 70 53 63 68 65 6d 61 2d  ert(&p->pSchema-
23374 3e 69 64 78 48 61 73 68 2c 20 7a 4e 61 6d 65 2c  >idxHash, zName,
23375 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
23376 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
23377 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d  te3Strlen30(zNam
23378 65 29 2c 20 30 29 3b 0a 20 20 61 73 73 65 72 74  e), 0);.  assert
23379 28 20 70 4f 6c 64 3d 3d 30 20 7c 7c 20 70 4f 6c  ( pOld==0 || pOl
2337a 64 3d 3d 70 20 29 3b 0a 20 20 66 72 65 65 49 6e  d==p );.  freeIn
2337b 64 65 78 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  dex(p);.}../*.**
2337c 20 46 6f 72 20 74 68 65 20 69 6e 64 65 78 20 63   For the index c
2337d 61 6c 6c 65 64 20 7a 49 64 78 4e 61 6d 65 20 77  alled zIdxName w
2337e 68 69 63 68 20 69 73 20 66 6f 75 6e 64 20 69 6e  hich is found in
2337f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 44   the database iD
23380 62 2c 0a 2a 2a 20 75 6e 6c 69 6b 65 20 74 68 61  b,.** unlike tha
23381 74 20 69 6e 64 65 78 20 66 72 6f 6d 20 69 74 73  t index from its
23382 20 54 61 62 6c 65 20 74 68 65 6e 20 72 65 6d 6f   Table then remo
23383 76 65 20 74 68 65 20 69 6e 64 65 78 20 66 72 6f  ve the index fro
23384 6d 0a 2a 2a 20 74 68 65 20 69 6e 64 65 78 20 68  m.** the index h
23385 61 73 68 20 74 61 62 6c 65 20 61 6e 64 20 66 72  ash table and fr
23386 65 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 73 74  ee all memory st
23387 72 75 63 74 75 72 65 73 20 61 73 73 6f 63 69 61  ructures associa
23388 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20  ted.** with the 
23389 69 6e 64 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45  index..*/.SQLITE
2338a 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2338b 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65  lite3UnlinkAndDe
2338c 6c 65 74 65 49 6e 64 65 78 28 73 71 6c 69 74 65  leteIndex(sqlite
2338d 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c 20  3 *db, int iDb, 
2338e 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 64 78  const char *zIdx
2338f 4e 61 6d 65 29 7b 0a 20 20 49 6e 64 65 78 20 2a  Name){.  Index *
23390 70 49 6e 64 65 78 3b 0a 20 20 69 6e 74 20 6c 65  pIndex;.  int le
23391 6e 3b 0a 20 20 48 61 73 68 20 2a 70 48 61 73 68  n;.  Hash *pHash
23392 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d   = &db->aDb[iDb]
23393 2e 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73  .pSchema->idxHas
23394 68 3b 0a 0a 20 20 6c 65 6e 20 3d 20 73 71 6c 69  h;..  len = sqli
23395 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 49 64 78  te3Strlen30(zIdx
23396 4e 61 6d 65 29 3b 0a 20 20 70 49 6e 64 65 78 20  Name);.  pIndex 
23397 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73  = sqlite3HashIns
23398 65 72 74 28 70 48 61 73 68 2c 20 7a 49 64 78 4e  ert(pHash, zIdxN
23399 61 6d 65 2c 20 6c 65 6e 2c 20 30 29 3b 0a 20 20  ame, len, 0);.  
2339a 69 66 28 20 70 49 6e 64 65 78 20 29 7b 0a 20 20  if( pIndex ){.  
2339b 20 20 69 66 28 20 70 49 6e 64 65 78 2d 3e 70 54    if( pIndex->pT
2339c 61 62 6c 65 2d 3e 70 49 6e 64 65 78 3d 3d 70 49  able->pIndex==pI
2339d 6e 64 65 78 20 29 7b 0a 20 20 20 20 20 20 70 49  ndex ){.      pI
2339e 6e 64 65 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49  ndex->pTable->pI
2339f 6e 64 65 78 20 3d 20 70 49 6e 64 65 78 2d 3e 70  ndex = pIndex->p
233a0 4e 65 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  Next;.    }else{
233a1 0a 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 3b  .      Index *p;
233a2 0a 20 20 20 20 20 20 2f 2a 20 4a 75 73 74 69 66  .      /* Justif
233a3 69 63 61 74 69 6f 6e 20 6f 66 20 41 4c 57 41 59  ication of ALWAY
233a4 53 28 29 3b 20 20 54 68 65 20 69 6e 64 65 78 20  S();  The index 
233a5 6d 75 73 74 20 62 65 20 6f 6e 20 74 68 65 20 6c  must be on the l
233a6 69 73 74 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20  ist of.      ** 
233a7 69 6e 64 69 63 65 73 2e 20 2a 2f 0a 20 20 20 20  indices. */.    
233a8 20 20 70 20 3d 20 70 49 6e 64 65 78 2d 3e 70 54    p = pIndex->pT
233a9 61 62 6c 65 2d 3e 70 49 6e 64 65 78 3b 0a 20 20  able->pIndex;.  
233aa 20 20 20 20 77 68 69 6c 65 28 20 41 4c 57 41 59      while( ALWAY
233ab 53 28 70 29 20 26 26 20 70 2d 3e 70 4e 65 78 74  S(p) && p->pNext
233ac 21 3d 70 49 6e 64 65 78 20 29 7b 20 70 20 3d 20  !=pIndex ){ p = 
233ad 70 2d 3e 70 4e 65 78 74 3b 20 7d 0a 20 20 20 20  p->pNext; }.    
233ae 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 20 26    if( ALWAYS(p &
233af 26 20 70 2d 3e 70 4e 65 78 74 3d 3d 70 49 6e 64  & p->pNext==pInd
233b0 65 78 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ex) ){.        p
233b1 2d 3e 70 4e 65 78 74 20 3d 20 70 49 6e 64 65 78  ->pNext = pIndex
233b2 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d  ->pNext;.      }
233b3 0a 20 20 20 20 7d 0a 20 20 20 20 66 72 65 65 49  .    }.    freeI
233b4 6e 64 65 78 28 70 49 6e 64 65 78 29 3b 0a 20 20  ndex(pIndex);.  
233b5 7d 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d  }.  db->flags |=
233b6 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68   SQLITE_InternCh
233b7 61 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  anges;.}../*.** 
233b8 45 72 61 73 65 20 61 6c 6c 20 73 63 68 65 6d 61  Erase all schema
233b9 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f   information fro
233ba 6d 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20  m the in-memory 
233bb 68 61 73 68 20 74 61 62 6c 65 73 20 6f 66 0a 2a  hash tables of.*
233bc 2a 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62  * a single datab
233bd 61 73 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69  ase.  This routi
233be 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20  ne is called to 
233bf 72 65 63 6c 61 69 6d 20 6d 65 6d 6f 72 79 0a 2a  reclaim memory.*
233c0 2a 20 62 65 66 6f 72 65 20 74 68 65 20 64 61 74  * before the dat
233c1 61 62 61 73 65 20 63 6c 6f 73 65 73 2e 20 20 49  abase closes.  I
233c2 74 20 69 73 20 61 6c 73 6f 20 63 61 6c 6c 65 64  t is also called
233c3 20 64 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61   during a rollba
233c4 63 6b 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 77  ck.** if there w
233c5 65 72 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67  ere schema chang
233c6 65 73 20 64 75 72 69 6e 67 20 74 68 65 20 74 72  es during the tr
233c7 61 6e 73 61 63 74 69 6f 6e 20 6f 72 20 69 66 20  ansaction or if 
233c8 61 0a 2a 2a 20 73 63 68 65 6d 61 2d 63 6f 6f 6b  a.** schema-cook
233c9 69 65 20 6d 69 73 6d 61 74 63 68 20 6f 63 63 75  ie mismatch occu
233ca 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 44 62  rs..**.** If iDb
233cb 3d 3d 30 20 74 68 65 6e 20 72 65 73 65 74 20 74  ==0 then reset t
233cc 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65  he internal sche
233cd 6d 61 20 74 61 62 6c 65 73 20 66 6f 72 20 61 6c  ma tables for al
233ce 6c 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69  l database.** fi
233cf 6c 65 73 2e 20 20 49 66 20 69 44 62 3e 3d 31 20  les.  If iDb>=1 
233d0 74 68 65 6e 20 72 65 73 65 74 20 74 68 65 20 69  then reset the i
233d1 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 20 66  nternal schema f
233d2 6f 72 20 6f 6e 6c 79 20 74 68 65 0a 2a 2a 20 73  or only the.** s
233d3 69 6e 67 6c 65 20 66 69 6c 65 20 69 6e 64 69 63  ingle file indic
233d4 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ated..*/.SQLITE_
233d5 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
233d6 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61  ite3ResetInterna
233d7 6c 53 63 68 65 6d 61 28 73 71 6c 69 74 65 33 20  lSchema(sqlite3 
233d8 2a 64 62 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20  *db, int iDb){. 
233d9 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 61 73 73   int i, j;.  ass
233da 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69  ert( iDb>=0 && i
233db 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 0a 20  Db<db->nDb );.. 
233dc 20 69 66 28 20 69 44 62 3d 3d 30 20 29 7b 0a 20   if( iDb==0 ){. 
233dd 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45     sqlite3BtreeE
233de 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20 20 7d  nterAll(db);.  }
233df 0a 20 20 66 6f 72 28 69 3d 69 44 62 3b 20 69 3c  .  for(i=iDb; i<
233e0 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20  db->nDb; i++){. 
233e1 20 20 20 44 62 20 2a 70 44 62 20 3d 20 26 64 62     Db *pDb = &db
233e2 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 69 66  ->aDb[i];.    if
233e3 28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 20 29  ( pDb->pSchema )
233e4 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 69  {.      assert(i
233e5 3d 3d 31 20 7c 7c 20 28 70 44 62 2d 3e 70 42 74  ==1 || (pDb->pBt
233e6 20 26 26 20 73 71 6c 69 74 65 33 42 74 72 65 65   && sqlite3Btree
233e7 48 6f 6c 64 73 4d 75 74 65 78 28 70 44 62 2d 3e  HoldsMutex(pDb->
233e8 70 42 74 29 29 29 3b 0a 20 20 20 20 20 20 73 71  pBt)));.      sq
233e9 6c 69 74 65 33 53 63 68 65 6d 61 46 72 65 65 28  lite3SchemaFree(
233ea 70 44 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  pDb->pSchema);. 
233eb 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 44 62     }.    if( iDb
233ec 3e 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7d  >0 ) return;.  }
233ed 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3d 3d  .  assert( iDb==
233ee 30 20 29 3b 0a 20 20 64 62 2d 3e 66 6c 61 67 73  0 );.  db->flags
233ef 20 26 3d 20 7e 53 51 4c 49 54 45 5f 49 6e 74 65   &= ~SQLITE_Inte
233f0 72 6e 43 68 61 6e 67 65 73 3b 0a 20 20 73 71 6c  rnChanges;.  sql
233f1 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 4c 69  ite3VtabUnlockLi
233f2 73 74 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65  st(db);.  sqlite
233f3 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64  3BtreeLeaveAll(d
233f4 62 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e 65  b);..  /* If one
233f5 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 20   or more of the 
233f6 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61  auxiliary databa
233f7 73 65 20 66 69 6c 65 73 20 68 61 73 20 62 65 65  se files has bee
233f8 6e 20 63 6c 6f 73 65 64 2c 0a 20 20 2a 2a 20 74  n closed,.  ** t
233f9 68 65 6e 20 72 65 6d 6f 76 65 20 74 68 65 6d 20  hen remove them 
233fa 66 72 6f 6d 20 74 68 65 20 61 75 78 69 6c 69 61  from the auxilia
233fb 72 79 20 64 61 74 61 62 61 73 65 20 6c 69 73 74  ry database list
233fc 2e 20 20 57 65 20 74 61 6b 65 20 74 68 65 0a 20  .  We take the. 
233fd 20 2a 2a 20 6f 70 70 6f 72 74 75 6e 69 74 79 20   ** opportunity 
233fe 74 6f 20 64 6f 20 74 68 69 73 20 68 65 72 65 20  to do this here 
233ff 73 69 6e 63 65 20 77 65 20 68 61 76 65 20 6a 75  since we have ju
23400 73 74 20 64 65 6c 65 74 65 64 20 61 6c 6c 20 6f  st deleted all o
23401 66 20 74 68 65 0a 20 20 2a 2a 20 73 63 68 65 6d  f the.  ** schem
23402 61 20 68 61 73 68 20 74 61 62 6c 65 73 20 61 6e  a hash tables an
23403 64 20 74 68 65 72 65 66 6f 72 65 20 64 6f 20 6e  d therefore do n
23404 6f 74 20 68 61 76 65 20 74 6f 20 6d 61 6b 65 20  ot have to make 
23405 61 6e 79 20 63 68 61 6e 67 65 73 0a 20 20 2a 2a  any changes.  **
23406 20 74 6f 20 61 6e 79 20 6f 66 20 74 68 6f 73 65   to any of those
23407 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20   tables..  */.  
23408 66 6f 72 28 69 3d 6a 3d 32 3b 20 69 3c 64 62 2d  for(i=j=2; i<db-
23409 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nDb; i++){.    
2340a 73 74 72 75 63 74 20 44 62 20 2a 70 44 62 20 3d  struct Db *pDb =
2340b 20 26 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20   &db->aDb[i];.  
2340c 20 20 69 66 28 20 70 44 62 2d 3e 70 42 74 3d 3d    if( pDb->pBt==
2340d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
2340e 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 44 62  e3DbFree(db, pDb
2340f 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  ->zName);.      
23410 70 44 62 2d 3e 7a 4e 61 6d 65 20 3d 20 30 3b 0a  pDb->zName = 0;.
23411 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a        continue;.
23412 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6a 3c      }.    if( j<
23413 69 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 61  i ){.      db->a
23414 44 62 5b 6a 5d 20 3d 20 64 62 2d 3e 61 44 62 5b  Db[j] = db->aDb[
23415 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6a 2b  i];.    }.    j+
23416 2b 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28  +;.  }.  memset(
23417 26 64 62 2d 3e 61 44 62 5b 6a 5d 2c 20 30 2c 20  &db->aDb[j], 0, 
23418 28 64 62 2d 3e 6e 44 62 2d 6a 29 2a 73 69 7a 65  (db->nDb-j)*size
23419 6f 66 28 64 62 2d 3e 61 44 62 5b 6a 5d 29 29 3b  of(db->aDb[j]));
2341a 0a 20 20 64 62 2d 3e 6e 44 62 20 3d 20 6a 3b 0a  .  db->nDb = j;.
2341b 20 20 69 66 28 20 64 62 2d 3e 6e 44 62 3c 3d 32    if( db->nDb<=2
2341c 20 26 26 20 64 62 2d 3e 61 44 62 21 3d 64 62 2d   && db->aDb!=db-
2341d 3e 61 44 62 53 74 61 74 69 63 20 29 7b 0a 20 20  >aDbStatic ){.  
2341e 20 20 6d 65 6d 63 70 79 28 64 62 2d 3e 61 44 62    memcpy(db->aDb
2341f 53 74 61 74 69 63 2c 20 64 62 2d 3e 61 44 62 2c  Static, db->aDb,
23420 20 32 2a 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44   2*sizeof(db->aD
23421 62 5b 30 5d 29 29 3b 0a 20 20 20 20 73 71 6c 69  b[0]));.    sqli
23422 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 64 62  te3DbFree(db, db
23423 2d 3e 61 44 62 29 3b 0a 20 20 20 20 64 62 2d 3e  ->aDb);.    db->
23424 61 44 62 20 3d 20 64 62 2d 3e 61 44 62 53 74 61  aDb = db->aDbSta
23425 74 69 63 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  tic;.  }.}../*.*
23426 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
23427 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 20  s called when a 
23428 63 6f 6d 6d 69 74 20 6f 63 63 75 72 73 2e 0a 2a  commit occurs..*
23429 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2342a 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d   void sqlite3Com
2342b 6d 69 74 49 6e 74 65 72 6e 61 6c 43 68 61 6e 67  mitInternalChang
2342c 65 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  es(sqlite3 *db){
2342d 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 26 3d 20  .  db->flags &= 
2342e 7e 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68  ~SQLITE_InternCh
2342f 61 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  anges;.}../*.** 
23430 43 6c 65 61 72 20 74 68 65 20 63 6f 6c 75 6d 6e  Clear the column
23431 20 6e 61 6d 65 73 20 66 72 6f 6d 20 61 20 74 61   names from a ta
23432 62 6c 65 20 6f 72 20 76 69 65 77 2e 0a 2a 2f 0a  ble or view..*/.
23433 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69  static void sqli
23434 74 65 52 65 73 65 74 43 6f 6c 75 6d 6e 4e 61 6d  teResetColumnNam
23435 65 73 28 54 61 62 6c 65 20 2a 70 54 61 62 6c 65  es(Table *pTable
23436 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 43 6f  ){.  int i;.  Co
23437 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 73 71  lumn *pCol;.  sq
23438 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 54 61 62  lite3 *db = pTab
23439 6c 65 2d 3e 64 62 4d 65 6d 3b 0a 20 20 74 65 73  le->dbMem;.  tes
2343a 74 63 61 73 65 28 20 64 62 3d 3d 30 20 29 3b 0a  tcase( db==0 );.
2343b 20 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65    assert( pTable
2343c 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70 43  !=0 );.  if( (pC
2343d 6f 6c 20 3d 20 70 54 61 62 6c 65 2d 3e 61 43 6f  ol = pTable->aCo
2343e 6c 29 21 3d 30 20 29 7b 0a 20 20 20 20 66 6f 72  l)!=0 ){.    for
2343f 28 69 3d 30 3b 20 69 3c 70 54 61 62 6c 65 2d 3e  (i=0; i<pTable->
23440 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b  nCol; i++, pCol+
23441 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  +){.      sqlite
23442 33 44 62 46 72 65 65 28 64 62 2c 20 70 43 6f 6c  3DbFree(db, pCol
23443 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  ->zName);.      
23444 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
23445 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 70 44 66 6c  e(db, pCol->pDfl
23446 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  t);.      sqlite
23447 33 44 62 46 72 65 65 28 64 62 2c 20 70 43 6f 6c  3DbFree(db, pCol
23448 2d 3e 7a 44 66 6c 74 29 3b 0a 20 20 20 20 20 20  ->zDflt);.      
23449 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2344a 2c 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 29 3b 0a  , pCol->zType);.
2344b 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
2344c 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 43  ree(db, pCol->zC
2344d 6f 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  oll);.    }.    
2344e 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2344f 2c 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 29 3b  , pTable->aCol);
23450 0a 20 20 7d 0a 20 20 70 54 61 62 6c 65 2d 3e 61  .  }.  pTable->a
23451 43 6f 6c 20 3d 20 30 3b 0a 20 20 70 54 61 62 6c  Col = 0;.  pTabl
23452 65 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 7d 0a 0a  e->nCol = 0;.}..
23453 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65  /*.** Remove the
23454 20 6d 65 6d 6f 72 79 20 64 61 74 61 20 73 74 72   memory data str
23455 75 63 74 75 72 65 73 20 61 73 73 6f 63 69 61 74  uctures associat
23456 65 64 20 77 69 74 68 20 74 68 65 20 67 69 76 65  ed with the give
23457 6e 0a 2a 2a 20 54 61 62 6c 65 2e 20 20 4e 6f 20  n.** Table.  No 
23458 63 68 61 6e 67 65 73 20 61 72 65 20 6d 61 64 65  changes are made
23459 20 74 6f 20 64 69 73 6b 20 62 79 20 74 68 69 73   to disk by this
2345a 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20   routine..**.** 
2345b 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6a 75 73  This routine jus
2345c 74 20 64 65 6c 65 74 65 73 20 74 68 65 20 64 61  t deletes the da
2345d 74 61 20 73 74 72 75 63 74 75 72 65 2e 20 20 49  ta structure.  I
2345e 74 20 64 6f 65 73 20 6e 6f 74 20 75 6e 6c 69 6e  t does not unlin
2345f 6b 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 64  k.** the table d
23460 61 74 61 20 73 74 72 75 63 74 75 72 65 20 66 72  ata structure fr
23461 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  om the hash tabl
23462 65 2e 20 20 42 75 74 20 69 74 20 64 6f 65 73 20  e.  But it does 
23463 64 65 73 74 72 6f 79 0a 2a 2a 20 6d 65 6d 6f 72  destroy.** memor
23464 79 20 73 74 72 75 63 74 75 72 65 73 20 6f 66 20  y structures of 
23465 74 68 65 20 69 6e 64 69 63 65 73 20 61 6e 64 20  the indices and 
23466 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 61 73 73  foreign keys ass
23467 6f 63 69 61 74 65 64 20 77 69 74 68 20 0a 2a 2a  ociated with .**
23468 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53   the table..*/.S
23469 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2346a 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65  id sqlite3Delete
2346b 54 61 62 6c 65 28 54 61 62 6c 65 20 2a 70 54 61  Table(Table *pTa
2346c 62 6c 65 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70  ble){.  Index *p
2346d 49 6e 64 65 78 2c 20 2a 70 4e 65 78 74 3b 0a 20  Index, *pNext;. 
2346e 20 46 4b 65 79 20 2a 70 46 4b 65 79 2c 20 2a 70   FKey *pFKey, *p
2346f 4e 65 78 74 46 4b 65 79 3b 0a 20 20 73 71 6c 69  NextFKey;.  sqli
23470 74 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28 20  te3 *db;..  if( 
23471 70 54 61 62 6c 65 3d 3d 30 20 29 20 72 65 74 75  pTable==0 ) retu
23472 72 6e 3b 0a 20 20 64 62 20 3d 20 70 54 61 62 6c  rn;.  db = pTabl
23473 65 2d 3e 64 62 4d 65 6d 3b 0a 20 20 74 65 73 74  e->dbMem;.  test
23474 63 61 73 65 28 20 64 62 3d 3d 30 20 29 3b 0a 0a  case( db==0 );..
23475 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 64 65 6c 65    /* Do not dele
23476 74 65 20 74 68 65 20 74 61 62 6c 65 20 75 6e 74  te the table unt
23477 69 6c 20 74 68 65 20 72 65 66 65 72 65 6e 63 65  il the reference
23478 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a   count reaches z
23479 65 72 6f 2e 20 2a 2f 0a 20 20 70 54 61 62 6c 65  ero. */.  pTable
2347a 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20  ->nRef--;.  if( 
2347b 70 54 61 62 6c 65 2d 3e 6e 52 65 66 3e 30 20 29  pTable->nRef>0 )
2347c 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
2347d 7d 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62  }.  assert( pTab
2347e 6c 65 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 0a  le->nRef==0 );..
2347f 20 20 2f 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20    /* Delete all 
23480 69 6e 64 69 63 65 73 20 61 73 73 6f 63 69 61 74  indices associat
23481 65 64 20 77 69 74 68 20 74 68 69 73 20 74 61 62  ed with this tab
23482 6c 65 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 70 49  le.  */.  for(pI
23483 6e 64 65 78 20 3d 20 70 54 61 62 6c 65 2d 3e 70  ndex = pTable->p
23484 49 6e 64 65 78 3b 20 70 49 6e 64 65 78 3b 20 70  Index; pIndex; p
23485 49 6e 64 65 78 3d 70 4e 65 78 74 29 7b 0a 20 20  Index=pNext){.  
23486 20 20 70 4e 65 78 74 20 3d 20 70 49 6e 64 65 78    pNext = pIndex
23487 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 61 73 73  ->pNext;.    ass
23488 65 72 74 28 20 70 49 6e 64 65 78 2d 3e 70 53 63  ert( pIndex->pSc
23489 68 65 6d 61 3d 3d 70 54 61 62 6c 65 2d 3e 70 53  hema==pTable->pS
2348a 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 73 71 6c  chema );.    sql
2348b 69 74 65 33 44 65 6c 65 74 65 49 6e 64 65 78 28  ite3DeleteIndex(
2348c 70 49 6e 64 65 78 29 3b 0a 20 20 7d 0a 0a 23 69  pIndex);.  }..#i
2348d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2348e 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20  T_FOREIGN_KEY.  
2348f 2f 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20 66 6f  /* Delete all fo
23490 72 65 69 67 6e 20 6b 65 79 73 20 61 73 73 6f 63  reign keys assoc
23491 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20  iated with this 
23492 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 66 6f 72 28  table. */.  for(
23493 70 46 4b 65 79 3d 70 54 61 62 6c 65 2d 3e 70 46  pFKey=pTable->pF
23494 4b 65 79 3b 20 70 46 4b 65 79 3b 20 70 46 4b 65  Key; pFKey; pFKe
23495 79 3d 70 4e 65 78 74 46 4b 65 79 29 7b 0a 20 20  y=pNextFKey){.  
23496 20 20 70 4e 65 78 74 46 4b 65 79 20 3d 20 70 46    pNextFKey = pF
23497 4b 65 79 2d 3e 70 4e 65 78 74 46 72 6f 6d 3b 0a  Key->pNextFrom;.
23498 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
23499 65 28 64 62 2c 20 70 46 4b 65 79 29 3b 0a 20 20  e(db, pFKey);.  
2349a 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 44  }.#endif..  /* D
2349b 65 6c 65 74 65 20 74 68 65 20 54 61 62 6c 65 20  elete the Table 
2349c 73 74 72 75 63 74 75 72 65 20 69 74 73 65 6c 66  structure itself
2349d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 52  ..  */.  sqliteR
2349e 65 73 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28  esetColumnNames(
2349f 70 54 61 62 6c 65 29 3b 0a 20 20 73 71 6c 69 74  pTable);.  sqlit
234a0 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 61  e3DbFree(db, pTa
234a1 62 6c 65 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 73  ble->zName);.  s
234a2 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
234a3 20 70 54 61 62 6c 65 2d 3e 7a 43 6f 6c 41 66 66   pTable->zColAff
234a4 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65  );.  sqlite3Sele
234a5 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 54 61  ctDelete(db, pTa
234a6 62 6c 65 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 23  ble->pSelect);.#
234a7 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
234a8 49 54 5f 43 48 45 43 4b 0a 20 20 73 71 6c 69 74  IT_CHECK.  sqlit
234a9 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
234aa 20 70 54 61 62 6c 65 2d 3e 70 43 68 65 63 6b 29   pTable->pCheck)
234ab 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74  ;.#endif.  sqlit
234ac 65 33 56 74 61 62 43 6c 65 61 72 28 70 54 61 62  e3VtabClear(pTab
234ad 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  le);.  sqlite3Db
234ae 46 72 65 65 28 64 62 2c 20 70 54 61 62 6c 65 29  Free(db, pTable)
234af 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e  ;.}../*.** Unlin
234b0 6b 20 74 68 65 20 67 69 76 65 6e 20 74 61 62 6c  k the given tabl
234b1 65 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20  e from the hash 
234b2 74 61 62 6c 65 73 20 61 6e 64 20 74 68 65 20 64  tables and the d
234b3 65 6c 65 74 65 20 74 68 65 0a 2a 2a 20 74 61 62  elete the.** tab
234b4 6c 65 20 73 74 72 75 63 74 75 72 65 20 77 69 74  le structure wit
234b5 68 20 61 6c 6c 20 69 74 73 20 69 6e 64 69 63 65  h all its indice
234b6 73 20 61 6e 64 20 66 6f 72 65 69 67 6e 20 6b 65  s and foreign ke
234b7 79 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ys..*/.SQLITE_PR
234b8 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
234b9 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74  e3UnlinkAndDelet
234ba 65 54 61 62 6c 65 28 73 71 6c 69 74 65 33 20 2a  eTable(sqlite3 *
234bb 64 62 2c 20 69 6e 74 20 69 44 62 2c 20 63 6f 6e  db, int iDb, con
234bc 73 74 20 63 68 61 72 20 2a 7a 54 61 62 4e 61 6d  st char *zTabNam
234bd 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a  e){.  Table *p;.
234be 20 20 44 62 20 2a 70 44 62 3b 0a 0a 20 20 61 73    Db *pDb;..  as
234bf 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a 20  sert( db!=0 );. 
234c0 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20   assert( iDb>=0 
234c1 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29  && iDb<db->nDb )
234c2 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 54 61 62  ;.  assert( zTab
234c3 4e 61 6d 65 20 26 26 20 7a 54 61 62 4e 61 6d 65  Name && zTabName
234c4 5b 30 5d 20 29 3b 0a 20 20 70 44 62 20 3d 20 26  [0] );.  pDb = &
234c5 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20  db->aDb[iDb];.  
234c6 70 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49  p = sqlite3HashI
234c7 6e 73 65 72 74 28 26 70 44 62 2d 3e 70 53 63 68  nsert(&pDb->pSch
234c8 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 7a 54  ema->tblHash, zT
234c9 61 62 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20  abName,.        
234ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
234cb 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
234cc 7a 54 61 62 4e 61 6d 65 29 2c 30 29 3b 0a 20 20  zTabName),0);.  
234cd 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62  sqlite3DeleteTab
234ce 6c 65 28 70 29 3b 0a 20 20 64 62 2d 3e 66 6c 61  le(p);.  db->fla
234cf 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74  gs |= SQLITE_Int
234d0 65 72 6e 43 68 61 6e 67 65 73 3b 0a 7d 0a 0a 2f  ernChanges;.}../
234d1 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 74 6f 6b  *.** Given a tok
234d2 65 6e 2c 20 72 65 74 75 72 6e 20 61 20 73 74 72  en, return a str
234d3 69 6e 67 20 74 68 61 74 20 63 6f 6e 73 69 73 74  ing that consist
234d4 73 20 6f 66 20 74 68 65 20 74 65 78 74 20 6f 66  s of the text of
234d5 20 74 68 61 74 0a 2a 2a 20 74 6f 6b 65 6e 2e 20   that.** token. 
234d6 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74   Space to hold t
234d7 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69  he returned stri
234d8 6e 67 0a 2a 2a 20 69 73 20 6f 62 74 61 69 6e 65  ng.** is obtaine
234d9 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c  d from sqliteMal
234da 6c 6f 63 28 29 20 61 6e 64 20 6d 75 73 74 20 62  loc() and must b
234db 65 20 66 72 65 65 64 20 62 79 20 74 68 65 20 63  e freed by the c
234dc 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69  alling.** functi
234dd 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 71 75  on..**.** Any qu
234de 6f 74 61 74 69 6f 6e 20 6d 61 72 6b 73 20 28 65  otation marks (e
234df 78 3a 20 20 22 6e 61 6d 65 22 2c 20 27 6e 61 6d  x:  "name", 'nam
234e0 65 27 2c 20 5b 6e 61 6d 65 5d 2c 20 6f 72 20 60  e', [name], or `
234e1 6e 61 6d 65 60 29 20 74 68 61 74 0a 2a 2a 20 73  name`) that.** s
234e2 75 72 72 6f 75 6e 64 20 74 68 65 20 62 6f 64 79  urround the body
234e3 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 61 72   of the token ar
234e4 65 20 72 65 6d 6f 76 65 64 2e 0a 2a 2a 0a 2a 2a  e removed..**.**
234e5 20 54 6f 6b 65 6e 73 20 61 72 65 20 6f 66 74 65   Tokens are ofte
234e6 6e 20 6a 75 73 74 20 70 6f 69 6e 74 65 72 73 20  n just pointers 
234e7 69 6e 74 6f 20 74 68 65 20 6f 72 69 67 69 6e 61  into the origina
234e8 6c 20 53 51 4c 20 74 65 78 74 20 61 6e 64 20 73  l SQL text and s
234e9 6f 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 5c 30 30  o.** are not \00
234ea 30 20 74 65 72 6d 69 6e 61 74 65 64 20 61 6e 64  0 terminated and
234eb 20 61 72 65 20 6e 6f 74 20 70 65 72 73 69 73 74   are not persist
234ec 65 6e 74 2e 20 20 54 68 65 20 72 65 74 75 72 6e  ent.  The return
234ed 65 64 20 73 74 72 69 6e 67 0a 2a 2a 20 69 73 20  ed string.** is 
234ee 5c 30 30 30 20 74 65 72 6d 69 6e 61 74 65 64 20  \000 terminated 
234ef 61 6e 64 20 69 73 20 70 65 72 73 69 73 74 65 6e  and is persisten
234f0 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
234f1 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74  VATE char *sqlit
234f2 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
234f3 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 6f 6b  sqlite3 *db, Tok
234f4 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20 20 63 68  en *pName){.  ch
234f5 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 69 66 28  ar *zName;.  if(
234f6 20 70 4e 61 6d 65 20 29 7b 0a 20 20 20 20 7a 4e   pName ){.    zN
234f7 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53  ame = sqlite3DbS
234f8 74 72 4e 44 75 70 28 64 62 2c 20 28 63 68 61 72  trNDup(db, (char
234f9 2a 29 70 4e 61 6d 65 2d 3e 7a 2c 20 70 4e 61 6d  *)pName->z, pNam
234fa 65 2d 3e 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74  e->n);.    sqlit
234fb 65 33 44 65 71 75 6f 74 65 28 7a 4e 61 6d 65 29  e3Dequote(zName)
234fc 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a  ;.  }else{.    z
234fd 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  Name = 0;.  }.  
234fe 72 65 74 75 72 6e 20 7a 4e 61 6d 65 3b 0a 7d 0a  return zName;.}.
234ff 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20  ./*.** Open the 
23500 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61  sqlite_master ta
23501 62 6c 65 20 73 74 6f 72 65 64 20 69 6e 20 64 61  ble stored in da
23502 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 69 44  tabase number iD
23503 62 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e 67  b for.** writing
23504 2e 20 54 68 65 20 74 61 62 6c 65 20 69 73 20 6f  . The table is o
23505 70 65 6e 65 64 20 75 73 69 6e 67 20 63 75 72 73  pened using curs
23506 6f 72 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  or 0..*/.SQLITE_
23507 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
23508 69 74 65 33 4f 70 65 6e 4d 61 73 74 65 72 54 61  ite3OpenMasterTa
23509 62 6c 65 28 50 61 72 73 65 20 2a 70 2c 20 69 6e  ble(Parse *p, in
2350a 74 20 69 44 62 29 7b 0a 20 20 56 64 62 65 20 2a  t iDb){.  Vdbe *
2350b 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
2350c 62 65 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33  be(p);.  sqlite3
2350d 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 44 62  TableLock(p, iDb
2350e 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 31  , MASTER_ROOT, 1
2350f 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69  , SCHEMA_TABLE(i
23510 44 62 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  Db));.  sqlite3V
23511 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
23512 4f 70 65 6e 57 72 69 74 65 2c 20 30 2c 20 4d 41  OpenWrite, 0, MA
23513 53 54 45 52 5f 52 4f 4f 54 2c 20 69 44 62 29 3b  STER_ROOT, iDb);
23514 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  .  sqlite3VdbeCh
23515 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 28 63  angeP4(v, -1, (c
23516 68 61 72 20 2a 29 35 2c 20 50 34 5f 49 4e 54 33  har *)5, P4_INT3
23517 32 29 3b 20 20 2f 2a 20 35 20 63 6f 6c 75 6d 6e  2);  /* 5 column
23518 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20   table */.  if( 
23519 70 2d 3e 6e 54 61 62 3d 3d 30 20 29 7b 0a 20 20  p->nTab==0 ){.  
2351a 20 20 70 2d 3e 6e 54 61 62 20 3d 20 31 3b 0a 20    p->nTab = 1;. 
2351b 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61   }.}../*.** Para
2351c 6d 65 74 65 72 20 7a 4e 61 6d 65 20 70 6f 69 6e  meter zName poin
2351d 74 73 20 74 6f 20 61 20 6e 75 6c 2d 74 65 72 6d  ts to a nul-term
2351e 69 6e 61 74 65 64 20 62 75 66 66 65 72 20 63 6f  inated buffer co
2351f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6e 61 6d  ntaining the nam
23520 65 0a 2a 2a 20 6f 66 20 61 20 64 61 74 61 62 61  e.** of a databa
23521 73 65 20 28 22 6d 61 69 6e 22 2c 20 22 74 65 6d  se ("main", "tem
23522 70 22 20 6f 72 20 74 68 65 20 6e 61 6d 65 20 6f  p" or the name o
23523 66 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 62  f an attached db
23524 29 2e 20 54 68 69 73 0a 2a 2a 20 66 75 6e 63 74  ). This.** funct
23525 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20  ion returns the 
23526 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6e 61 6d  index of the nam
23527 65 64 20 64 61 74 61 62 61 73 65 20 69 6e 20 64  ed database in d
23528 62 2d 3e 61 44 62 5b 5d 2c 20 6f 72 0a 2a 2a 20  b->aDb[], or.** 
23529 2d 31 20 69 66 20 74 68 65 20 6e 61 6d 65 64 20  -1 if the named 
2352a 64 62 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75  db cannot be fou
2352b 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  nd..*/.SQLITE_PR
2352c 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
2352d 33 46 69 6e 64 44 62 4e 61 6d 65 28 73 71 6c 69  3FindDbName(sqli
2352e 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63  te3 *db, const c
2352f 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 69  har *zName){.  i
23530 6e 74 20 69 20 3d 20 2d 31 3b 20 20 20 20 20 20  nt i = -1;      
23531 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e     /* Database n
23532 75 6d 62 65 72 20 2a 2f 0a 20 20 69 66 28 20 7a  umber */.  if( z
23533 4e 61 6d 65 20 29 7b 0a 20 20 20 20 44 62 20 2a  Name ){.    Db *
23534 70 44 62 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d  pDb;.    int n =
23535 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
23536 28 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 66 6f 72  (zName);.    for
23537 28 69 3d 28 64 62 2d 3e 6e 44 62 2d 31 29 2c 20  (i=(db->nDb-1), 
23538 70 44 62 3d 26 64 62 2d 3e 61 44 62 5b 69 5d 3b  pDb=&db->aDb[i];
23539 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70 44 62 2d   i>=0; i--, pDb-
2353a 2d 29 7b 0a 20 20 20 20 20 20 69 66 28 20 28 21  -){.      if( (!
2353b 4f 4d 49 54 5f 54 45 4d 50 44 42 20 7c 7c 20 69  OMIT_TEMPDB || i
2353c 21 3d 31 20 29 20 26 26 20 6e 3d 3d 73 71 6c 69  !=1 ) && n==sqli
2353d 74 65 33 53 74 72 6c 65 6e 33 30 28 70 44 62 2d  te3Strlen30(pDb-
2353e 3e 7a 4e 61 6d 65 29 20 26 26 20 0a 20 20 20 20  >zName) && .    
2353f 20 20 20 20 20 20 30 3d 3d 73 71 6c 69 74 65 33        0==sqlite3
23540 53 74 72 49 43 6d 70 28 70 44 62 2d 3e 7a 4e 61  StrICmp(pDb->zNa
23541 6d 65 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a 20 20  me, zName) ){.  
23542 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
23543 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
23544 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a 2f 2a   return i;.}../*
23545 0a 2a 2a 20 54 68 65 20 74 6f 6b 65 6e 20 2a 70  .** The token *p
23546 4e 61 6d 65 20 63 6f 6e 74 61 69 6e 73 20 74 68  Name contains th
23547 65 20 6e 61 6d 65 20 6f 66 20 61 20 64 61 74 61  e name of a data
23548 62 61 73 65 20 28 65 69 74 68 65 72 20 22 6d 61  base (either "ma
23549 69 6e 22 20 6f 72 0a 2a 2a 20 22 74 65 6d 70 22  in" or.** "temp"
2354a 20 6f 72 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   or the name of 
2354b 61 6e 20 61 74 74 61 63 68 65 64 20 64 62 29 2e  an attached db).
2354c 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   This routine re
2354d 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 69 6e 64  turns the.** ind
2354e 65 78 20 6f 66 20 74 68 65 20 6e 61 6d 65 64 20  ex of the named 
2354f 64 61 74 61 62 61 73 65 20 69 6e 20 64 62 2d 3e  database in db->
23550 61 44 62 5b 5d 2c 20 6f 72 20 2d 31 20 69 66 20  aDb[], or -1 if 
23551 74 68 65 20 6e 61 6d 65 64 20 64 62 20 0a 2a 2a  the named db .**
23552 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2e   does not exist.
23553 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
23554 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69  TE int sqlite3Fi
23555 6e 64 44 62 28 73 71 6c 69 74 65 33 20 2a 64 62  ndDb(sqlite3 *db
23556 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b  , Token *pName){
23557 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
23558 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23559 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
2355a 61 73 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  ase number */.  
2355b 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20  char *zName;    
2355c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2355d 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 77 65 20       /* Name we 
2355e 61 72 65 20 73 65 61 72 63 68 69 6e 67 20 66 6f  are searching fo
2355f 72 20 2a 2f 0a 20 20 7a 4e 61 6d 65 20 3d 20 73  r */.  zName = s
23560 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
23561 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a  ken(db, pName);.
23562 20 20 69 20 3d 20 73 71 6c 69 74 65 33 46 69 6e    i = sqlite3Fin
23563 64 44 62 4e 61 6d 65 28 64 62 2c 20 7a 4e 61 6d  dDbName(db, zNam
23564 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  e);.  sqlite3DbF
23565 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a  ree(db, zName);.
23566 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a 2f    return i;.}../
23567 2a 20 54 68 65 20 74 61 62 6c 65 20 6f 72 20 76  * The table or v
23568 69 65 77 20 6f 72 20 74 72 69 67 67 65 72 20 6e  iew or trigger n
23569 61 6d 65 20 69 73 20 70 61 73 73 65 64 20 74 6f  ame is passed to
2356a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 76 69   this routine vi
2356b 61 20 74 6f 6b 65 6e 73 0a 2a 2a 20 70 4e 61 6d  a tokens.** pNam
2356c 65 31 20 61 6e 64 20 70 4e 61 6d 65 32 2e 20 49  e1 and pName2. I
2356d 66 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65  f the table name
2356e 20 77 61 73 20 66 75 6c 6c 79 20 71 75 61 6c 69   was fully quali
2356f 66 69 65 64 2c 20 66 6f 72 20 65 78 61 6d 70 6c  fied, for exampl
23570 65 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54 45 20  e:.**.** CREATE 
23571 54 41 42 4c 45 20 78 78 78 2e 79 79 79 20 28 2e  TABLE xxx.yyy (.
23572 2e 2e 29 3b 0a 2a 2a 20 0a 2a 2a 20 54 68 65 6e  ..);.** .** Then
23573 20 70 4e 61 6d 65 31 20 69 73 20 73 65 74 20 74   pName1 is set t
23574 6f 20 22 78 78 78 22 20 61 6e 64 20 70 4e 61 6d  o "xxx" and pNam
23575 65 32 20 22 79 79 79 22 2e 20 4f 6e 20 74 68 65  e2 "yyy". On the
23576 20 6f 74 68 65 72 20 68 61 6e 64 20 69 66 0a 2a   other hand if.*
23577 2a 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65  * the table name
23578 20 69 73 20 6e 6f 74 20 66 75 6c 6c 79 20 71 75   is not fully qu
23579 61 6c 69 66 69 65 64 2c 20 69 2e 65 2e 3a 0a 2a  alified, i.e.:.*
2357a 2a 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c  *.** CREATE TABL
2357b 45 20 79 79 79 28 2e 2e 2e 29 3b 0a 2a 2a 0a 2a  E yyy(...);.**.*
2357c 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31 20 69 73  * Then pName1 is
2357d 20 73 65 74 20 74 6f 20 22 79 79 79 22 20 61 6e   set to "yyy" an
2357e 64 20 70 4e 61 6d 65 32 20 69 73 20 22 22 2e 0a  d pName2 is ""..
2357f 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
23580 6e 65 20 73 65 74 73 20 74 68 65 20 2a 70 70 55  ne sets the *ppU
23581 6e 71 75 61 6c 20 70 6f 69 6e 74 65 72 20 74 6f  nqual pointer to
23582 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 74 6f   point at the to
23583 6b 65 6e 20 28 70 4e 61 6d 65 31 20 6f 72 0a 2a  ken (pName1 or.*
23584 2a 20 70 4e 61 6d 65 32 29 20 74 68 61 74 20 73  * pName2) that s
23585 74 6f 72 65 73 20 74 68 65 20 75 6e 71 75 61 6c  tores the unqual
23586 69 66 69 65 64 20 74 61 62 6c 65 20 6e 61 6d 65  ified table name
23587 2e 20 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20  .  The index of 
23588 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  the.** database 
23589 22 78 78 78 22 20 69 73 20 72 65 74 75 72 6e 65  "xxx" is returne
2358a 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
2358b 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
2358c 54 77 6f 50 61 72 74 4e 61 6d 65 28 0a 20 20 50  TwoPartName(.  P
2358d 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2358e 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e     /* Parsing an
2358f 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e  d code generatin
23590 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54  g context */.  T
23591 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 20  oken *pName1,   
23592 20 20 20 2f 2a 20 54 68 65 20 22 78 78 78 22 20     /* The "xxx" 
23593 69 6e 20 74 68 65 20 6e 61 6d 65 20 22 78 78 78  in the name "xxx
23594 2e 79 79 79 22 20 6f 72 20 22 78 78 78 22 20 2a  .yyy" or "xxx" *
23595 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  /.  Token *pName
23596 32 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22  2,      /* The "
23597 79 79 79 22 20 69 6e 20 74 68 65 20 6e 61 6d 65  yyy" in the name
23598 20 22 78 78 78 2e 79 79 79 22 20 2a 2f 0a 20 20   "xxx.yyy" */.  
23599 54 6f 6b 65 6e 20 2a 2a 70 55 6e 71 75 61 6c 20  Token **pUnqual 
2359a 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65      /* Write the
2359b 20 75 6e 71 75 61 6c 69 66 69 65 64 20 6f 62 6a   unqualified obj
2359c 65 63 74 20 6e 61 6d 65 20 68 65 72 65 20 2a 2f  ect name here */
2359d 0a 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20  .){.  int iDb;  
2359e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2359f 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 6f    /* Database ho
235a0 6c 64 69 6e 67 20 74 68 65 20 6f 62 6a 65 63 74  lding the object
235a1 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
235a2 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
235a3 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 4e  .  if( ALWAYS(pN
235a4 61 6d 65 32 21 3d 30 29 20 26 26 20 70 4e 61 6d  ame2!=0) && pNam
235a5 65 32 2d 3e 6e 3e 30 20 29 7b 0a 20 20 20 20 69  e2->n>0 ){.    i
235a6 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  f( db->init.busy
235a7 20 29 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74   ) {.      sqlit
235a8 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
235a9 65 2c 20 22 63 6f 72 72 75 70 74 20 64 61 74 61  e, "corrupt data
235aa 62 61 73 65 22 29 3b 0a 20 20 20 20 20 20 70 50  base");.      pP
235ab 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20  arse->nErr++;.  
235ac 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20      return -1;. 
235ad 20 20 20 7d 0a 20 20 20 20 2a 70 55 6e 71 75 61     }.    *pUnqua
235ae 6c 20 3d 20 70 4e 61 6d 65 32 3b 0a 20 20 20 20  l = pName2;.    
235af 69 44 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  iDb = sqlite3Fin
235b0 64 44 62 28 64 62 2c 20 70 4e 61 6d 65 31 29 3b  dDb(db, pName1);
235b1 0a 20 20 20 20 69 66 28 20 69 44 62 3c 30 20 29  .    if( iDb<0 )
235b2 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
235b3 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
235b4 22 75 6e 6b 6e 6f 77 6e 20 64 61 74 61 62 61 73  "unknown databas
235b5 65 20 25 54 22 2c 20 70 4e 61 6d 65 31 29 3b 0a  e %T", pName1);.
235b6 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45        pParse->nE
235b7 72 72 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74 75  rr++;.      retu
235b8 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  rn -1;.    }.  }
235b9 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
235ba 28 20 64 62 2d 3e 69 6e 69 74 2e 69 44 62 3d 3d  ( db->init.iDb==
235bb 30 20 7c 7c 20 64 62 2d 3e 69 6e 69 74 2e 62 75  0 || db->init.bu
235bc 73 79 20 29 3b 0a 20 20 20 20 69 44 62 20 3d 20  sy );.    iDb = 
235bd 64 62 2d 3e 69 6e 69 74 2e 69 44 62 3b 0a 20 20  db->init.iDb;.  
235be 20 20 2a 70 55 6e 71 75 61 6c 20 3d 20 70 4e 61    *pUnqual = pNa
235bf 6d 65 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  me1;.  }.  retur
235c0 6e 20 69 44 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n iDb;.}../*.** 
235c1 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
235c2 75 73 65 64 20 74 6f 20 63 68 65 63 6b 20 69 66  used to check if
235c3 20 74 68 65 20 55 54 46 2d 38 20 73 74 72 69 6e   the UTF-8 strin
235c4 67 20 7a 4e 61 6d 65 20 69 73 20 61 20 6c 65 67  g zName is a leg
235c5 61 6c 0a 2a 2a 20 75 6e 71 75 61 6c 69 66 69 65  al.** unqualifie
235c6 64 20 6e 61 6d 65 20 66 6f 72 20 61 20 6e 65 77  d name for a new
235c7 20 73 63 68 65 6d 61 20 6f 62 6a 65 63 74 20 28   schema object (
235c8 74 61 62 6c 65 2c 20 69 6e 64 65 78 2c 20 76 69  table, index, vi
235c9 65 77 20 6f 72 0a 2a 2a 20 74 72 69 67 67 65 72  ew or.** trigger
235ca 29 2e 20 41 6c 6c 20 6e 61 6d 65 73 20 61 72 65  ). All names are
235cb 20 6c 65 67 61 6c 20 65 78 63 65 70 74 20 74 68   legal except th
235cc 6f 73 65 20 74 68 61 74 20 62 65 67 69 6e 20 77  ose that begin w
235cd 69 74 68 20 74 68 65 20 73 74 72 69 6e 67 0a 2a  ith the string.*
235ce 2a 20 22 73 71 6c 69 74 65 5f 22 20 28 69 6e 20  * "sqlite_" (in 
235cf 75 70 70 65 72 2c 20 6c 6f 77 65 72 20 6f 72 20  upper, lower or 
235d0 6d 69 78 65 64 20 63 61 73 65 29 2e 20 54 68 69  mixed case). Thi
235d1 73 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65  s portion of the
235d2 20 6e 61 6d 65 73 70 61 63 65 0a 2a 2a 20 69 73   namespace.** is
235d3 20 72 65 73 65 72 76 65 64 20 66 6f 72 20 69 6e   reserved for in
235d4 74 65 72 6e 61 6c 20 75 73 65 2e 0a 2a 2f 0a 53  ternal use..*/.S
235d5 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
235d6 74 20 73 71 6c 69 74 65 33 43 68 65 63 6b 4f 62  t sqlite3CheckOb
235d7 6a 65 63 74 4e 61 6d 65 28 50 61 72 73 65 20 2a  jectName(Parse *
235d8 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68  pParse, const ch
235d9 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 69 66  ar *zName){.  if
235da 28 20 21 70 50 61 72 73 65 2d 3e 64 62 2d 3e 69  ( !pParse->db->i
235db 6e 69 74 2e 62 75 73 79 20 26 26 20 70 50 61 72  nit.busy && pPar
235dc 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 0a 20  se->nested==0 . 
235dd 20 20 20 20 20 20 20 20 20 26 26 20 28 70 50 61           && (pPa
235de 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26  rse->db->flags &
235df 20 53 51 4c 49 54 45 5f 57 72 69 74 65 53 63 68   SQLITE_WriteSch
235e0 65 6d 61 29 3d 3d 30 0a 20 20 20 20 20 20 20 20  ema)==0.        
235e1 20 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 53    && 0==sqlite3S
235e2 74 72 4e 49 43 6d 70 28 7a 4e 61 6d 65 2c 20 22  trNICmp(zName, "
235e3 73 71 6c 69 74 65 5f 22 2c 20 37 29 20 29 7b 0a  sqlite_", 7) ){.
235e4 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
235e5 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6f 62 6a  Msg(pParse, "obj
235e6 65 63 74 20 6e 61 6d 65 20 72 65 73 65 72 76 65  ect name reserve
235e7 64 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75  d for internal u
235e8 73 65 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b  se: %s", zName);
235e9 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
235ea 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20  TE_ERROR;.  }.  
235eb 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
235ec 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65 67 69 6e  ;.}../*.** Begin
235ed 20 63 6f 6e 73 74 72 75 63 74 69 6e 67 20 61 20   constructing a 
235ee 6e 65 77 20 74 61 62 6c 65 20 72 65 70 72 65 73  new table repres
235ef 65 6e 74 61 74 69 6f 6e 20 69 6e 20 6d 65 6d 6f  entation in memo
235f0 72 79 2e 20 20 54 68 69 73 20 69 73 0a 2a 2a 20  ry.  This is.** 
235f1 74 68 65 20 66 69 72 73 74 20 6f 66 20 73 65 76  the first of sev
235f2 65 72 61 6c 20 61 63 74 69 6f 6e 20 72 6f 75 74  eral action rout
235f3 69 6e 65 73 20 74 68 61 74 20 67 65 74 20 63 61  ines that get ca
235f4 6c 6c 65 64 20 69 6e 20 72 65 73 70 6f 6e 73 65  lled in response
235f5 0a 2a 2a 20 74 6f 20 61 20 43 52 45 41 54 45 20  .** to a CREATE 
235f6 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e  TABLE statement.
235f7 20 20 49 6e 20 70 61 72 74 69 63 75 6c 61 72 2c    In particular,
235f8 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
235f9 20 63 61 6c 6c 65 64 0a 2a 2a 20 61 66 74 65 72   called.** after
235fa 20 73 65 65 69 6e 67 20 74 6f 6b 65 6e 73 20 22   seeing tokens "
235fb 43 52 45 41 54 45 22 20 61 6e 64 20 22 54 41 42  CREATE" and "TAB
235fc 4c 45 22 20 61 6e 64 20 74 68 65 20 74 61 62 6c  LE" and the tabl
235fd 65 20 6e 61 6d 65 2e 20 54 68 65 20 69 73 54 65  e name. The isTe
235fe 6d 70 0a 2a 2a 20 66 6c 61 67 20 69 73 20 74 72  mp.** flag is tr
235ff 75 65 20 69 66 20 74 68 65 20 74 61 62 6c 65 20  ue if the table 
23600 73 68 6f 75 6c 64 20 62 65 20 73 74 6f 72 65 64  should be stored
23601 20 69 6e 20 74 68 65 20 61 75 78 69 6c 69 61 72   in the auxiliar
23602 79 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69  y database.** fi
23603 6c 65 20 69 6e 73 74 65 61 64 20 6f 66 20 69 6e  le instead of in
23604 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
23605 73 65 20 66 69 6c 65 2e 20 20 54 68 69 73 20 69  se file.  This i
23606 73 20 6e 6f 72 6d 61 6c 6c 79 20 74 68 65 20 63  s normally the c
23607 61 73 65 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20  ase.** when the 
23608 22 54 45 4d 50 22 20 6f 72 20 22 54 45 4d 50 4f  "TEMP" or "TEMPO
23609 52 41 52 59 22 20 6b 65 79 77 6f 72 64 20 6f 63  RARY" keyword oc
2360a 63 75 72 73 20 69 6e 20 62 65 74 77 65 65 6e 0a  curs in between.
2360b 2a 2a 20 43 52 45 41 54 45 20 61 6e 64 20 54 41  ** CREATE and TA
2360c 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e  BLE..**.** The n
2360d 65 77 20 74 61 62 6c 65 20 72 65 63 6f 72 64 20  ew table record 
2360e 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 61  is initialized a
2360f 6e 64 20 70 75 74 20 69 6e 20 70 50 61 72 73 65  nd put in pParse
23610 2d 3e 70 4e 65 77 54 61 62 6c 65 2e 0a 2a 2a 20  ->pNewTable..** 
23611 41 73 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 43  As more of the C
23612 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74  REATE TABLE stat
23613 65 6d 65 6e 74 20 69 73 20 70 61 72 73 65 64 2c  ement is parsed,
23614 20 61 64 64 69 74 69 6f 6e 61 6c 20 61 63 74 69   additional acti
23615 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 77  on.** routines w
23616 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 74 6f  ill be called to
23617 20 61 64 64 20 6d 6f 72 65 20 69 6e 66 6f 72 6d   add more inform
23618 61 74 69 6f 6e 20 74 6f 20 74 68 69 73 20 72 65  ation to this re
23619 63 6f 72 64 2e 0a 2a 2a 20 41 74 20 74 68 65 20  cord..** At the 
2361a 65 6e 64 20 6f 66 20 74 68 65 20 43 52 45 41 54  end of the CREAT
2361b 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e  E TABLE statemen
2361c 74 2c 20 74 68 65 20 73 71 6c 69 74 65 33 45 6e  t, the sqlite3En
2361d 64 54 61 62 6c 65 28 29 20 72 6f 75 74 69 6e 65  dTable() routine
2361e 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 74 6f  .** is called to
2361f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 63 6f   complete the co
23620 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 74 68  nstruction of th
23621 65 20 6e 65 77 20 74 61 62 6c 65 20 72 65 63 6f  e new table reco
23622 72 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  rd..*/.SQLITE_PR
23623 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
23624 65 33 53 74 61 72 74 54 61 62 6c 65 28 0a 20 20  e3StartTable(.  
23625 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
23626 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65   /* Parser conte
23627 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  xt */.  Token *p
23628 4e 61 6d 65 31 2c 20 20 20 2f 2a 20 46 69 72 73  Name1,   /* Firs
23629 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 6e 61  t part of the na
2362a 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
2362b 6f 72 20 76 69 65 77 20 2a 2f 0a 20 20 54 6f 6b  or view */.  Tok
2362c 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 2f 2a  en *pName2,   /*
2362d 20 53 65 63 6f 6e 64 20 70 61 72 74 20 6f 66 20   Second part of 
2362e 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
2362f 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 2a 2f  table or view */
23630 0a 20 20 69 6e 74 20 69 73 54 65 6d 70 2c 20 20  .  int isTemp,  
23631 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
23632 68 69 73 20 69 73 20 61 20 54 45 4d 50 20 74 61  his is a TEMP ta
23633 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 56  ble */.  int isV
23634 69 65 77 2c 20 20 20 20 20 20 2f 2a 20 54 72 75  iew,      /* Tru
23635 65 20 69 66 20 74 68 69 73 20 69 73 20 61 20 56  e if this is a V
23636 49 45 57 20 2a 2f 0a 20 20 69 6e 74 20 69 73 56  IEW */.  int isV
23637 69 72 74 75 61 6c 2c 20 20 20 2f 2a 20 54 72 75  irtual,   /* Tru
23638 65 20 69 66 20 74 68 69 73 20 69 73 20 61 20 56  e if this is a V
23639 49 52 54 55 41 4c 20 74 61 62 6c 65 20 2a 2f 0a  IRTUAL table */.
2363a 20 20 69 6e 74 20 6e 6f 45 72 72 20 20 20 20 20    int noErr     
2363b 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e 67     /* Do nothing
2363c 20 69 66 20 74 61 62 6c 65 20 61 6c 72 65 61 64   if table alread
2363d 79 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b 0a 20  y exists */.){. 
2363e 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 3b 0a   Table *pTable;.
2363f 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20    char *zName = 
23640 30 3b 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 6f  0; /* The name o
23641 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20  f the new table 
23642 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
23643 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
23644 20 56 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20   Vdbe *v;.  int 
23645 69 44 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  iDb;         /* 
23646 44 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20  Database number 
23647 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 74 61  to create the ta
23648 62 6c 65 20 69 6e 20 2a 2f 0a 20 20 54 6f 6b 65  ble in */.  Toke
23649 6e 20 2a 70 4e 61 6d 65 3b 20 20 20 20 2f 2a 20  n *pName;    /* 
2364a 55 6e 71 75 61 6c 69 66 69 65 64 20 6e 61 6d 65  Unqualified name
2364b 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f   of the table to
2364c 20 63 72 65 61 74 65 20 2a 2f 0a 0a 20 20 2f 2a   create */..  /*
2364d 20 54 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69   The table or vi
2364e 65 77 20 6e 61 6d 65 20 74 6f 20 63 72 65 61 74  ew name to creat
2364f 65 20 69 73 20 70 61 73 73 65 64 20 74 6f 20 74  e is passed to t
23650 68 69 73 20 72 6f 75 74 69 6e 65 20 76 69 61 20  his routine via 
23651 74 6f 6b 65 6e 73 0a 20 20 2a 2a 20 70 4e 61 6d  tokens.  ** pNam
23652 65 31 20 61 6e 64 20 70 4e 61 6d 65 32 2e 20 49  e1 and pName2. I
23653 66 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65  f the table name
23654 20 77 61 73 20 66 75 6c 6c 79 20 71 75 61 6c 69   was fully quali
23655 66 69 65 64 2c 20 66 6f 72 20 65 78 61 6d 70 6c  fied, for exampl
23656 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 43 52 45  e:.  **.  ** CRE
23657 41 54 45 20 54 41 42 4c 45 20 78 78 78 2e 79 79  ATE TABLE xxx.yy
23658 79 20 28 2e 2e 2e 29 3b 0a 20 20 2a 2a 20 0a 20  y (...);.  ** . 
23659 20 2a 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31 20   ** Then pName1 
2365a 69 73 20 73 65 74 20 74 6f 20 22 78 78 78 22 20  is set to "xxx" 
2365b 61 6e 64 20 70 4e 61 6d 65 32 20 22 79 79 79 22  and pName2 "yyy"
2365c 2e 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68  . On the other h
2365d 61 6e 64 20 69 66 0a 20 20 2a 2a 20 74 68 65 20  and if.  ** the 
2365e 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 20 6e 6f  table name is no
2365f 74 20 66 75 6c 6c 79 20 71 75 61 6c 69 66 69 65  t fully qualifie
23660 64 2c 20 69 2e 65 2e 3a 0a 20 20 2a 2a 0a 20 20  d, i.e.:.  **.  
23661 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20  ** CREATE TABLE 
23662 79 79 79 28 2e 2e 2e 29 3b 0a 20 20 2a 2a 0a 20  yyy(...);.  **. 
23663 20 2a 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31 20   ** Then pName1 
23664 69 73 20 73 65 74 20 74 6f 20 22 79 79 79 22 20  is set to "yyy" 
23665 61 6e 64 20 70 4e 61 6d 65 32 20 69 73 20 22 22  and pName2 is ""
23666 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  ..  **.  ** The 
23667 63 61 6c 6c 20 62 65 6c 6f 77 20 73 65 74 73 20  call below sets 
23668 74 68 65 20 70 4e 61 6d 65 20 70 6f 69 6e 74 65  the pName pointe
23669 72 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68  r to point at th
2366a 65 20 74 6f 6b 65 6e 20 28 70 4e 61 6d 65 31 20  e token (pName1 
2366b 6f 72 0a 20 20 2a 2a 20 70 4e 61 6d 65 32 29 20  or.  ** pName2) 
2366c 74 68 61 74 20 73 74 6f 72 65 73 20 74 68 65 20  that stores the 
2366d 75 6e 71 75 61 6c 69 66 69 65 64 20 74 61 62 6c  unqualified tabl
2366e 65 20 6e 61 6d 65 2e 20 54 68 65 20 76 61 72 69  e name. The vari
2366f 61 62 6c 65 20 69 44 62 20 69 73 0a 20 20 2a 2a  able iDb is.  **
23670 20 73 65 74 20 74 6f 20 74 68 65 20 69 6e 64 65   set to the inde
23671 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  x of the databas
23672 65 20 74 68 61 74 20 74 68 65 20 74 61 62 6c 65  e that the table
23673 20 6f 72 20 76 69 65 77 20 69 73 20 74 6f 20 62   or view is to b
23674 65 0a 20 20 2a 2a 20 63 72 65 61 74 65 64 20 69  e.  ** created i
23675 6e 2e 0a 20 20 2a 2f 0a 20 20 69 44 62 20 3d 20  n..  */.  iDb = 
23676 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61  sqlite3TwoPartNa
23677 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65  me(pParse, pName
23678 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4e 61 6d  1, pName2, &pNam
23679 65 29 3b 0a 20 20 69 66 28 20 69 44 62 3c 30 20  e);.  if( iDb<0 
2367a 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
2367b 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20  !OMIT_TEMPDB && 
2367c 69 73 54 65 6d 70 20 26 26 20 69 44 62 3e 31 20  isTemp && iDb>1 
2367d 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 63 72 65  ){.    /* If cre
2367e 61 74 69 6e 67 20 61 20 74 65 6d 70 20 74 61 62  ating a temp tab
2367f 6c 65 2c 20 74 68 65 20 6e 61 6d 65 20 6d 61 79  le, the name may
23680 20 6e 6f 74 20 62 65 20 71 75 61 6c 69 66 69 65   not be qualifie
23681 64 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  d */.    sqlite3
23682 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
23683 20 22 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c   "temporary tabl
23684 65 20 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 75  e name must be u
23685 6e 71 75 61 6c 69 66 69 65 64 22 29 3b 0a 20 20  nqualified");.  
23686 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
23687 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42  if( !OMIT_TEMPDB
23688 20 26 26 20 69 73 54 65 6d 70 20 29 20 69 44 62   && isTemp ) iDb
23689 20 3d 20 31 3b 0a 0a 20 20 70 50 61 72 73 65 2d   = 1;..  pParse-
2368a 3e 73 4e 61 6d 65 54 6f 6b 65 6e 20 3d 20 2a 70  >sNameToken = *p
2368b 4e 61 6d 65 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20  Name;.  zName = 
2368c 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54  sqlite3NameFromT
2368d 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b  oken(db, pName);
2368e 0a 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20  .  if( zName==0 
2368f 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
23690 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74  SQLITE_OK!=sqlit
23691 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d  e3CheckObjectNam
23692 65 28 70 50 61 72 73 65 2c 20 7a 4e 61 6d 65 29  e(pParse, zName)
23693 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 62 65 67   ){.    goto beg
23694 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a  in_table_error;.
23695 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e    }.  if( db->in
23696 69 74 2e 69 44 62 3d 3d 31 20 29 20 69 73 54 65  it.iDb==1 ) isTe
23697 6d 70 20 3d 20 31 3b 0a 23 69 66 6e 64 65 66 20  mp = 1;.#ifndef 
23698 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48  SQLITE_OMIT_AUTH
23699 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 61 73 73 65  ORIZATION.  asse
2369a 72 74 28 20 28 69 73 54 65 6d 70 20 26 20 31 29  rt( (isTemp & 1)
2369b 3d 3d 69 73 54 65 6d 70 20 29 3b 0a 20 20 7b 0a  ==isTemp );.  {.
2369c 20 20 20 20 69 6e 74 20 63 6f 64 65 3b 0a 20 20      int code;.  
2369d 20 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 64 62    char *zDb = db
2369e 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
2369f 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
236a0 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73  3AuthCheck(pPars
236a1 65 2c 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54  e, SQLITE_INSERT
236a2 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69  , SCHEMA_TABLE(i
236a3 73 54 65 6d 70 29 2c 20 30 2c 20 7a 44 62 29 20  sTemp), 0, zDb) 
236a4 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 65  ){.      goto be
236a5 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b  gin_table_error;
236a6 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69  .    }.    if( i
236a7 73 56 69 65 77 20 29 7b 0a 20 20 20 20 20 20 69  sView ){.      i
236a8 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20  f( !OMIT_TEMPDB 
236a9 26 26 20 69 73 54 65 6d 70 20 29 7b 0a 20 20 20  && isTemp ){.   
236aa 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49       code = SQLI
236ab 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 56  TE_CREATE_TEMP_V
236ac 49 45 57 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  IEW;.      }else
236ad 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d  {.        code =
236ae 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 56   SQLITE_CREATE_V
236af 49 45 57 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  IEW;.      }.   
236b0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66   }else{.      if
236b1 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26  ( !OMIT_TEMPDB &
236b2 26 20 69 73 54 65 6d 70 20 29 7b 0a 20 20 20 20  & isTemp ){.    
236b3 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54      code = SQLIT
236b4 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 41  E_CREATE_TEMP_TA
236b5 42 4c 45 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  BLE;.      }else
236b6 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d  {.        code =
236b7 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54   SQLITE_CREATE_T
236b8 41 42 4c 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ABLE;.      }.  
236b9 20 20 7d 0a 20 20 20 20 69 66 28 20 21 69 73 56    }.    if( !isV
236ba 69 72 74 75 61 6c 20 26 26 20 73 71 6c 69 74 65  irtual && sqlite
236bb 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73  3AuthCheck(pPars
236bc 65 2c 20 63 6f 64 65 2c 20 7a 4e 61 6d 65 2c 20  e, code, zName, 
236bd 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20  0, zDb) ){.     
236be 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c   goto begin_tabl
236bf 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20  e_error;.    }. 
236c0 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20   }.#endif..  /* 
236c1 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6e 65  Make sure the ne
236c2 77 20 74 61 62 6c 65 20 6e 61 6d 65 20 64 6f 65  w table name doe
236c3 73 20 6e 6f 74 20 63 6f 6c 6c 69 64 65 20 77 69  s not collide wi
236c4 74 68 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 20  th an existing. 
236c5 20 2a 2a 20 69 6e 64 65 78 20 6f 72 20 74 61 62   ** index or tab
236c6 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 73  le name in the s
236c7 61 6d 65 20 64 61 74 61 62 61 73 65 2e 20 20 49  ame database.  I
236c8 73 73 75 65 20 61 6e 20 65 72 72 6f 72 20 6d 65  ssue an error me
236c9 73 73 61 67 65 20 69 66 0a 20 20 2a 2a 20 69 74  ssage if.  ** it
236ca 20 64 6f 65 73 2e 20 54 68 65 20 65 78 63 65 70   does. The excep
236cb 74 69 6f 6e 20 69 73 20 69 66 20 74 68 65 20 73  tion is if the s
236cc 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 70  tatement being p
236cd 61 72 73 65 64 20 77 61 73 20 70 61 73 73 65 64  arsed was passed
236ce 0a 20 20 2a 2a 20 74 6f 20 61 6e 20 73 71 6c 69  .  ** to an sqli
236cf 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62  te3_declare_vtab
236d0 28 29 20 63 61 6c 6c 2e 20 49 6e 20 74 68 61 74  () call. In that
236d1 20 63 61 73 65 20 6f 6e 6c 79 20 74 68 65 20 63   case only the c
236d2 6f 6c 75 6d 6e 20 6e 61 6d 65 73 0a 20 20 2a 2a  olumn names.  **
236d3 20 61 6e 64 20 74 79 70 65 73 20 77 69 6c 6c 20   and types will 
236d4 62 65 20 75 73 65 64 2c 20 73 6f 20 74 68 65 72  be used, so ther
236d5 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20  e is no need to 
236d6 74 65 73 74 20 66 6f 72 20 6e 61 6d 65 73 70 61  test for namespa
236d7 63 65 0a 20 20 2a 2a 20 63 6f 6c 6c 69 73 69 6f  ce.  ** collisio
236d8 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  ns..  */.  if( !
236d9 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20  IN_DECLARE_VTAB 
236da 29 7b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54  ){.    if( SQLIT
236db 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61  E_OK!=sqlite3Rea
236dc 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20  dSchema(pParse) 
236dd 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 65  ){.      goto be
236de 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b  gin_table_error;
236df 0a 20 20 20 20 7d 0a 20 20 20 20 70 54 61 62 6c  .    }.    pTabl
236e0 65 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54  e = sqlite3FindT
236e1 61 62 6c 65 28 64 62 2c 20 7a 4e 61 6d 65 2c 20  able(db, zName, 
236e2 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
236e3 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61  me);.    if( pTa
236e4 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ble ){.      if(
236e5 20 21 6e 6f 45 72 72 20 29 7b 0a 20 20 20 20 20   !noErr ){.     
236e6 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
236e7 73 67 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c  sg(pParse, "tabl
236e8 65 20 25 54 20 61 6c 72 65 61 64 79 20 65 78 69  e %T already exi
236e9 73 74 73 22 2c 20 70 4e 61 6d 65 29 3b 0a 20 20  sts", pName);.  
236ea 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f      }.      goto
236eb 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72   begin_table_err
236ec 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  or;.    }.    if
236ed 28 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64  ( sqlite3FindInd
236ee 65 78 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 30 29  ex(db, zName, 0)
236ef 21 3d 30 20 26 26 20 28 69 44 62 3d 3d 30 20 7c  !=0 && (iDb==0 |
236f0 7c 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  | !db->init.busy
236f1 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ) ){.      sqlit
236f2 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
236f3 65 2c 20 22 74 68 65 72 65 20 69 73 20 61 6c 72  e, "there is alr
236f4 65 61 64 79 20 61 6e 20 69 6e 64 65 78 20 6e 61  eady an index na
236f5 6d 65 64 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b  med %s", zName);
236f6 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 65 67 69  .      goto begi
236f7 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20  n_table_error;. 
236f8 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 54 61 62     }.  }..  pTab
236f9 6c 65 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  le = sqlite3DbMa
236fa 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
236fb 65 6f 66 28 54 61 62 6c 65 29 29 3b 0a 20 20 69  eof(Table));.  i
236fc 66 28 20 70 54 61 62 6c 65 3d 3d 30 20 29 7b 0a  f( pTable==0 ){.
236fd 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
236fe 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 70 50  iled = 1;.    pP
236ff 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54  arse->rc = SQLIT
23700 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 70 50 61  E_NOMEM;.    pPa
23701 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20  rse->nErr++;.   
23702 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c   goto begin_tabl
23703 65 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 70  e_error;.  }.  p
23704 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 20 3d 20 7a  Table->zName = z
23705 4e 61 6d 65 3b 0a 20 20 70 54 61 62 6c 65 2d 3e  Name;.  pTable->
23706 69 50 4b 65 79 20 3d 20 2d 31 3b 0a 20 20 70 54  iPKey = -1;.  pT
23707 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61 20 3d 20  able->pSchema = 
23708 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63  db->aDb[iDb].pSc
23709 68 65 6d 61 3b 0a 20 20 70 54 61 62 6c 65 2d 3e  hema;.  pTable->
2370a 6e 52 65 66 20 3d 20 31 3b 0a 20 20 70 54 61 62  nRef = 1;.  pTab
2370b 6c 65 2d 3e 64 62 4d 65 6d 20 3d 20 30 3b 0a 20  le->dbMem = 0;. 
2370c 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d   assert( pParse-
2370d 3e 70 4e 65 77 54 61 62 6c 65 3d 3d 30 20 29 3b  >pNewTable==0 );
2370e 0a 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  .  pParse->pNewT
2370f 61 62 6c 65 20 3d 20 70 54 61 62 6c 65 3b 0a 0a  able = pTable;..
23710 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
23711 74 68 65 20 6d 61 67 69 63 20 73 71 6c 69 74 65  the magic sqlite
23712 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20  _sequence table 
23713 75 73 65 64 20 62 79 20 61 75 74 6f 69 6e 63 72  used by autoincr
23714 65 6d 65 6e 74 2c 0a 20 20 2a 2a 20 74 68 65 6e  ement,.  ** then
23715 20 72 65 63 6f 72 64 20 61 20 70 6f 69 6e 74 65   record a pointe
23716 72 20 74 6f 20 74 68 69 73 20 74 61 62 6c 65 20  r to this table 
23717 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  in the main data
23718 62 61 73 65 20 73 74 72 75 63 74 75 72 65 0a 20  base structure. 
23719 20 2a 2a 20 73 6f 20 74 68 61 74 20 49 4e 53 45   ** so that INSE
2371a 52 54 20 63 61 6e 20 66 69 6e 64 20 74 68 65 20  RT can find the 
2371b 74 61 62 6c 65 20 65 61 73 69 6c 79 2e 0a 20 20  table easily..  
2371c 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
2371d 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45  E_OMIT_AUTOINCRE
2371e 4d 45 4e 54 0a 20 20 69 66 28 20 21 70 50 61 72  MENT.  if( !pPar
2371f 73 65 2d 3e 6e 65 73 74 65 64 20 26 26 20 73 74  se->nested && st
23720 72 63 6d 70 28 7a 4e 61 6d 65 2c 20 22 73 71 6c  rcmp(zName, "sql
23721 69 74 65 5f 73 65 71 75 65 6e 63 65 22 29 3d 3d  ite_sequence")==
23722 30 20 29 7b 0a 20 20 20 20 70 54 61 62 6c 65 2d  0 ){.    pTable-
23723 3e 70 53 63 68 65 6d 61 2d 3e 70 53 65 71 54 61  >pSchema->pSeqTa
23724 62 20 3d 20 70 54 61 62 6c 65 3b 0a 20 20 7d 0a  b = pTable;.  }.
23725 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 42 65 67  #endif..  /* Beg
23726 69 6e 20 67 65 6e 65 72 61 74 69 6e 67 20 74 68  in generating th
23727 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c  e code that will
23728 20 69 6e 73 65 72 74 20 74 68 65 20 74 61 62 6c   insert the tabl
23729 65 20 72 65 63 6f 72 64 20 69 6e 74 6f 0a 20 20  e record into.  
2372a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41  ** the SQLITE_MA
2372b 53 54 45 52 20 74 61 62 6c 65 2e 20 20 4e 6f 74  STER table.  Not
2372c 65 20 69 6e 20 70 61 72 74 69 63 75 6c 61 72 20  e in particular 
2372d 74 68 61 74 20 77 65 20 6d 75 73 74 20 67 6f 20  that we must go 
2372e 61 68 65 61 64 0a 20 20 2a 2a 20 61 6e 64 20 61  ahead.  ** and a
2372f 6c 6c 6f 63 61 74 65 20 74 68 65 20 72 65 63 6f  llocate the reco
23730 72 64 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68  rd number for th
23731 65 20 74 61 62 6c 65 20 65 6e 74 72 79 20 6e 6f  e table entry no
23732 77 2e 20 20 42 65 66 6f 72 65 20 61 6e 79 0a 20  w.  Before any. 
23733 20 2a 2a 20 50 52 49 4d 41 52 59 20 4b 45 59 20   ** PRIMARY KEY 
23734 6f 72 20 55 4e 49 51 55 45 20 6b 65 79 77 6f 72  or UNIQUE keywor
23735 64 73 20 61 72 65 20 70 61 72 73 65 64 2e 20 20  ds are parsed.  
23736 54 68 6f 73 65 20 6b 65 79 77 6f 72 64 73 20 77  Those keywords w
23737 69 6c 6c 20 63 61 75 73 65 0a 20 20 2a 2a 20 69  ill cause.  ** i
23738 6e 64 69 63 65 73 20 74 6f 20 62 65 20 63 72 65  ndices to be cre
23739 61 74 65 64 20 61 6e 64 20 74 68 65 20 74 61 62  ated and the tab
2373a 6c 65 20 72 65 63 6f 72 64 20 6d 75 73 74 20 63  le record must c
2373b 6f 6d 65 20 62 65 66 6f 72 65 20 74 68 65 20 0a  ome before the .
2373c 20 20 2a 2a 20 69 6e 64 69 63 65 73 2e 20 20 48    ** indices.  H
2373d 65 6e 63 65 2c 20 74 68 65 20 72 65 63 6f 72 64  ence, the record
2373e 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20   number for the 
2373f 74 61 62 6c 65 20 6d 75 73 74 20 62 65 20 61 6c  table must be al
23740 6c 6f 63 61 74 65 64 0a 20 20 2a 2a 20 6e 6f 77  located.  ** now
23741 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 64 62  ..  */.  if( !db
23742 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 20 28  ->init.busy && (
23743 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
23744 62 65 28 70 50 61 72 73 65 29 29 21 3d 30 20 29  be(pParse))!=0 )
23745 7b 0a 20 20 20 20 69 6e 74 20 6a 31 3b 0a 20 20  {.    int j1;.  
23746 20 20 69 6e 74 20 66 69 6c 65 46 6f 72 6d 61 74    int fileFormat
23747 3b 0a 20 20 20 20 69 6e 74 20 72 65 67 31 2c 20  ;.    int reg1, 
23748 72 65 67 32 2c 20 72 65 67 33 3b 0a 20 20 20 20  reg2, reg3;.    
23749 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74  sqlite3BeginWrit
2374a 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73  eOperation(pPars
2374b 65 2c 20 30 2c 20 69 44 62 29 3b 0a 0a 23 69 66  e, 0, iDb);..#if
2374c 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2374d 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
2374e 20 20 69 66 28 20 69 73 56 69 72 74 75 61 6c 20    if( isVirtual 
2374f 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
23750 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50  VdbeAddOp0(v, OP
23751 5f 56 42 65 67 69 6e 29 3b 0a 20 20 20 20 7d 0a  _VBegin);.    }.
23752 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 49  #endif..    /* I
23753 66 20 74 68 65 20 66 69 6c 65 20 66 6f 72 6d 61  f the file forma
23754 74 20 61 6e 64 20 65 6e 63 6f 64 69 6e 67 20 69  t and encoding i
23755 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68  n the database h
23756 61 76 65 20 6e 6f 74 20 62 65 65 6e 20 73 65 74  ave not been set
23757 2c 20 0a 20 20 20 20 2a 2a 20 73 65 74 20 74 68  , .    ** set th
23758 65 6d 20 6e 6f 77 2e 0a 20 20 20 20 2a 2f 0a 20  em now..    */. 
23759 20 20 20 72 65 67 31 20 3d 20 70 50 61 72 73 65     reg1 = pParse
2375a 2d 3e 72 65 67 52 6f 77 69 64 20 3d 20 2b 2b 70  ->regRowid = ++p
2375b 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
2375c 20 72 65 67 32 20 3d 20 70 50 61 72 73 65 2d 3e   reg2 = pParse->
2375d 72 65 67 52 6f 6f 74 20 3d 20 2b 2b 70 50 61 72  regRoot = ++pPar
2375e 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 72 65  se->nMem;.    re
2375f 67 33 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  g3 = ++pParse->n
23760 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Mem;.    sqlite3
23761 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
23762 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 69 44 62  _ReadCookie, iDb
23763 2c 20 72 65 67 33 2c 20 42 54 52 45 45 5f 46 49  , reg3, BTREE_FI
23764 4c 45 5f 46 4f 52 4d 41 54 29 3b 0a 20 20 20 20  LE_FORMAT);.    
23765 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42  sqlite3VdbeUsesB
23766 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0a 20 20  tree(v, iDb);.  
23767 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64    j1 = sqlite3Vd
23768 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49  beAddOp1(v, OP_I
23769 66 2c 20 72 65 67 33 29 3b 0a 20 20 20 20 66 69  f, reg3);.    fi
2376a 6c 65 46 6f 72 6d 61 74 20 3d 20 28 64 62 2d 3e  leFormat = (db->
2376b 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4c  flags & SQLITE_L
2376c 65 67 61 63 79 46 69 6c 65 46 6d 74 29 21 3d 30  egacyFileFmt)!=0
2376d 20 3f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   ?.             
2376e 20 20 20 20 20 31 20 3a 20 53 51 4c 49 54 45 5f       1 : SQLITE_
2376f 4d 41 58 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 3b  MAX_FILE_FORMAT;
23770 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
23771 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
23772 65 67 65 72 2c 20 66 69 6c 65 46 6f 72 6d 61 74  eger, fileFormat
23773 2c 20 72 65 67 33 29 3b 0a 20 20 20 20 73 71 6c  , reg3);.    sql
23774 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
23775 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20  , OP_SetCookie, 
23776 69 44 62 2c 20 42 54 52 45 45 5f 46 49 4c 45 5f  iDb, BTREE_FILE_
23777 46 4f 52 4d 41 54 2c 20 72 65 67 33 29 3b 0a 20  FORMAT, reg3);. 
23778 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
23779 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
2377a 65 72 2c 20 45 4e 43 28 64 62 29 2c 20 72 65 67  er, ENC(db), reg
2377b 33 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  3);.    sqlite3V
2377c 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2377d 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20  SetCookie, iDb, 
2377e 42 54 52 45 45 5f 54 45 58 54 5f 45 4e 43 4f 44  BTREE_TEXT_ENCOD
2377f 49 4e 47 2c 20 72 65 67 33 29 3b 0a 20 20 20 20  ING, reg3);.    
23780 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
23781 65 72 65 28 76 2c 20 6a 31 29 3b 0a 0a 20 20 20  ere(v, j1);..   
23782 20 2f 2a 20 54 68 69 73 20 6a 75 73 74 20 63 72   /* This just cr
23783 65 61 74 65 73 20 61 20 70 6c 61 63 65 2d 68 6f  eates a place-ho
23784 6c 64 65 72 20 72 65 63 6f 72 64 20 69 6e 20 74  lder record in t
23785 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  he sqlite_master
23786 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2a 20 54   table..    ** T
23787 68 65 20 72 65 63 6f 72 64 20 63 72 65 61 74 65  he record create
23788 64 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 61  d does not conta
23789 69 6e 20 61 6e 79 74 68 69 6e 67 20 79 65 74 2e  in anything yet.
2378a 20 20 49 74 20 77 69 6c 6c 20 62 65 20 72 65 70    It will be rep
2378b 6c 61 63 65 64 0a 20 20 20 20 2a 2a 20 62 79 20  laced.    ** by 
2378c 74 68 65 20 72 65 61 6c 20 65 6e 74 72 79 20 69  the real entry i
2378d 6e 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64  n code generated
2378e 20 61 74 20 73 71 6c 69 74 65 33 45 6e 64 54 61   at sqlite3EndTa
2378f 62 6c 65 28 29 2e 0a 20 20 20 20 2a 2a 0a 20 20  ble()..    **.  
23790 20 20 2a 2a 20 54 68 65 20 72 6f 77 69 64 20 66    ** The rowid f
23791 6f 72 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79  or the new entry
23792 20 69 73 20 6c 65 66 74 20 69 6e 20 72 65 67 69   is left in regi
23793 73 74 65 72 20 70 50 61 72 73 65 2d 3e 72 65 67  ster pParse->reg
23794 52 6f 77 69 64 2e 0a 20 20 20 20 2a 2a 20 54 68  Rowid..    ** Th
23795 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62  e root page numb
23796 65 72 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61  er of the new ta
23797 62 6c 65 20 69 73 20 6c 65 66 74 20 69 6e 20 72  ble is left in r
23798 65 67 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f  eg pParse->regRo
23799 6f 74 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 72  ot..    ** The r
2379a 6f 77 69 64 20 61 6e 64 20 72 6f 6f 74 20 70 61  owid and root pa
2379b 67 65 20 6e 75 6d 62 65 72 20 76 61 6c 75 65 73  ge number values
2379c 20 61 72 65 20 6e 65 65 64 65 64 20 62 79 20 74   are needed by t
2379d 68 65 20 63 6f 64 65 20 74 68 61 74 0a 20 20 20  he code that.   
2379e 20 2a 2a 20 73 71 6c 69 74 65 33 45 6e 64 54 61   ** sqlite3EndTa
2379f 62 6c 65 20 77 69 6c 6c 20 67 65 6e 65 72 61 74  ble will generat
237a0 65 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 20 21 64  e..    */.#if !d
237a1 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
237a2 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66  IT_VIEW) || !def
237a3 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
237a4 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29 0a 20  _VIRTUALTABLE). 
237a5 20 20 20 69 66 28 20 69 73 56 69 65 77 20 7c 7c     if( isView ||
237a6 20 69 73 56 69 72 74 75 61 6c 20 29 7b 0a 20 20   isVirtual ){.  
237a7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
237a8 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
237a9 67 65 72 2c 20 30 2c 20 72 65 67 32 29 3b 0a 20  ger, 0, reg2);. 
237aa 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a     }else.#endif.
237ab 20 20 20 20 7b 0a 20 20 20 20 20 20 73 71 6c 69      {.      sqli
237ac 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
237ad 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 2c   OP_CreateTable,
237ae 20 69 44 62 2c 20 72 65 67 32 29 3b 0a 20 20 20   iDb, reg2);.   
237af 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 70   }.    sqlite3Op
237b0 65 6e 4d 61 73 74 65 72 54 61 62 6c 65 28 70 50  enMasterTable(pP
237b1 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20  arse, iDb);.    
237b2 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
237b3 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64  2(v, OP_NewRowid
237b4 2c 20 30 2c 20 72 65 67 31 29 3b 0a 20 20 20 20  , 0, reg1);.    
237b5 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
237b6 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c  2(v, OP_Null, 0,
237b7 20 72 65 67 33 29 3b 0a 20 20 20 20 73 71 6c 69   reg3);.    sqli
237b8 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
237b9 20 4f 50 5f 49 6e 73 65 72 74 2c 20 30 2c 20 72   OP_Insert, 0, r
237ba 65 67 33 2c 20 72 65 67 31 29 3b 0a 20 20 20 20  eg3, reg1);.    
237bb 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
237bc 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41 50  eP5(v, OPFLAG_AP
237bd 50 45 4e 44 29 3b 0a 20 20 20 20 73 71 6c 69 74  PEND);.    sqlit
237be 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20  e3VdbeAddOp0(v, 
237bf 4f 50 5f 43 6c 6f 73 65 29 3b 0a 20 20 7d 0a 0a  OP_Close);.  }..
237c0 20 20 2f 2a 20 4e 6f 72 6d 61 6c 20 28 6e 6f 6e    /* Normal (non
237c1 2d 65 72 72 6f 72 29 20 72 65 74 75 72 6e 2e 20  -error) return. 
237c2 2a 2f 0a 20 20 72 65 74 75 72 6e 3b 0a 0a 20 20  */.  return;..  
237c3 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  /* If an error o
237c4 63 63 75 72 73 2c 20 77 65 20 6a 75 6d 70 20 68  ccurs, we jump h
237c5 65 72 65 20 2a 2f 0a 62 65 67 69 6e 5f 74 61 62  ere */.begin_tab
237c6 6c 65 5f 65 72 72 6f 72 3a 0a 20 20 73 71 6c 69  le_error:.  sqli
237c7 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e  te3DbFree(db, zN
237c8 61 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a  ame);.  return;.
237c9 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 6d 61  }../*.** This ma
237ca 63 72 6f 20 69 73 20 75 73 65 64 20 74 6f 20 63  cro is used to c
237cb 6f 6d 70 61 72 65 20 74 77 6f 20 73 74 72 69 6e  ompare two strin
237cc 67 73 20 69 6e 20 61 20 63 61 73 65 2d 69 6e 73  gs in a case-ins
237cd 65 6e 73 69 74 69 76 65 20 6d 61 6e 6e 65 72 2e  ensitive manner.
237ce 0a 2a 2a 20 49 74 20 69 73 20 73 6c 69 67 68 74  .** It is slight
237cf 6c 79 20 66 61 73 74 65 72 20 74 68 61 6e 20 63  ly faster than c
237d0 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 53 74  alling sqlite3St
237d1 72 49 43 6d 70 28 29 20 64 69 72 65 63 74 6c 79  rICmp() directly
237d2 2c 20 62 75 74 0a 2a 2a 20 70 72 6f 64 75 63 65  , but.** produce
237d3 73 20 6c 61 72 67 65 72 20 63 6f 64 65 2e 0a 2a  s larger code..*
237d4 2a 0a 2a 2a 20 57 41 52 4e 49 4e 47 3a 20 54 68  *.** WARNING: Th
237d5 69 73 20 6d 61 63 72 6f 20 69 73 20 6e 6f 74 20  is macro is not 
237d6 63 6f 6d 70 61 74 69 62 6c 65 20 77 69 74 68 20  compatible with 
237d7 74 68 65 20 73 74 72 63 6d 70 28 29 20 66 61 6d  the strcmp() fam
237d8 69 6c 79 2e 20 49 74 0a 2a 2a 20 72 65 74 75 72  ily. It.** retur
237d9 6e 73 20 74 72 75 65 20 69 66 20 74 68 65 20 74  ns true if the t
237da 77 6f 20 73 74 72 69 6e 67 73 20 61 72 65 20 65  wo strings are e
237db 71 75 61 6c 2c 20 6f 74 68 65 72 77 69 73 65 20  qual, otherwise 
237dc 66 61 6c 73 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e  false..*/.#defin
237dd 65 20 53 54 52 49 43 4d 50 28 78 2c 20 79 29 20  e STRICMP(x, y) 
237de 28 5c 0a 73 71 6c 69 74 65 33 55 70 70 65 72 54  (\.sqlite3UpperT
237df 6f 4c 6f 77 65 72 5b 2a 28 75 6e 73 69 67 6e 65  oLower[*(unsigne
237e0 64 20 63 68 61 72 20 2a 29 28 78 29 5d 3d 3d 20  d char *)(x)]== 
237e1 20 20 5c 0a 73 71 6c 69 74 65 33 55 70 70 65 72    \.sqlite3Upper
237e2 54 6f 4c 6f 77 65 72 5b 2a 28 75 6e 73 69 67 6e  ToLower[*(unsign
237e3 65 64 20 63 68 61 72 20 2a 29 28 79 29 5d 20 20  ed char *)(y)]  
237e4 20 20 20 5c 0a 26 26 20 73 71 6c 69 74 65 33 53     \.&& sqlite3S
237e5 74 72 49 43 6d 70 28 28 78 29 2b 31 2c 28 79 29  trICmp((x)+1,(y)
237e6 2b 31 29 3d 3d 30 20 29 0a 0a 2f 2a 0a 2a 2a 20  +1)==0 )../*.** 
237e7 41 64 64 20 61 20 6e 65 77 20 63 6f 6c 75 6d 6e  Add a new column
237e8 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 63 75   to the table cu
237e9 72 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 63 6f  rrently being co
237ea 6e 73 74 72 75 63 74 65 64 2e 0a 2a 2a 0a 2a 2a  nstructed..**.**
237eb 20 54 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c   The parser call
237ec 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6f  s this routine o
237ed 6e 63 65 20 66 6f 72 20 65 61 63 68 20 63 6f 6c  nce for each col
237ee 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 0a  umn declaration.
237ef 2a 2a 20 69 6e 20 61 20 43 52 45 41 54 45 20 54  ** in a CREATE T
237f0 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 20  ABLE statement. 
237f1 20 73 71 6c 69 74 65 33 53 74 61 72 74 54 61 62   sqlite3StartTab
237f2 6c 65 28 29 20 67 65 74 73 20 63 61 6c 6c 65 64  le() gets called
237f3 0a 2a 2a 20 66 69 72 73 74 20 74 6f 20 67 65 74  .** first to get
237f4 20 74 68 69 6e 67 73 20 67 6f 69 6e 67 2e 20 20   things going.  
237f5 54 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  Then this routin
237f6 65 20 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 20  e is called for 
237f7 65 61 63 68 0a 2a 2a 20 63 6f 6c 75 6d 6e 2e 0a  each.** column..
237f8 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
237f9 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64  E void sqlite3Ad
237fa 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a 70  dColumn(Parse *p
237fb 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 4e  Parse, Token *pN
237fc 61 6d 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70  ame){.  Table *p
237fd 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 68 61  ;.  int i;.  cha
237fe 72 20 2a 7a 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a  r *z;.  Column *
237ff 70 43 6f 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20  pCol;.  sqlite3 
23800 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
23801 3b 0a 20 20 69 66 28 20 28 70 20 3d 20 70 50 61  ;.  if( (p = pPa
23802 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3d  rse->pNewTable)=
23803 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 23 69 66  =0 ) return;.#if
23804 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55   SQLITE_MAX_COLU
23805 4d 4e 0a 20 20 69 66 28 20 70 2d 3e 6e 43 6f 6c  MN.  if( p->nCol
23806 2b 31 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  +1>db->aLimit[SQ
23807 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d  LITE_LIMIT_COLUM
23808 4e 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  N] ){.    sqlite
23809 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2380a 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75  , "too many colu
2380b 6d 6e 73 20 6f 6e 20 25 73 22 2c 20 70 2d 3e 7a  mns on %s", p->z
2380c 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72  Name);.    retur
2380d 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  n;.  }.#endif.  
2380e 7a 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  z = sqlite3NameF
2380f 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61  romToken(db, pNa
23810 6d 65 29 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20  me);.  if( z==0 
23811 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28  ) return;.  for(
23812 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20  i=0; i<p->nCol; 
23813 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 53 54  i++){.    if( ST
23814 52 49 43 4d 50 28 7a 2c 20 70 2d 3e 61 43 6f 6c  RICMP(z, p->aCol
23815 5b 69 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20  [i].zName) ){.  
23816 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
23817 4d 73 67 28 70 50 61 72 73 65 2c 20 22 64 75 70  Msg(pParse, "dup
23818 6c 69 63 61 74 65 20 63 6f 6c 75 6d 6e 20 6e 61  licate column na
23819 6d 65 3a 20 25 73 22 2c 20 7a 29 3b 0a 20 20 20  me: %s", z);.   
2381a 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2381b 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20 20 20 72  (db, z);.      r
2381c 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d  eturn;.    }.  }
2381d 0a 20 20 69 66 28 20 28 70 2d 3e 6e 43 6f 6c 20  .  if( (p->nCol 
2381e 26 20 30 78 37 29 3d 3d 30 20 29 7b 0a 20 20 20  & 0x7)==0 ){.   
2381f 20 43 6f 6c 75 6d 6e 20 2a 61 4e 65 77 3b 0a 20   Column *aNew;. 
23820 20 20 20 61 4e 65 77 20 3d 20 73 71 6c 69 74 65     aNew = sqlite
23821 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 70 2d  3DbRealloc(db,p-
23822 3e 61 43 6f 6c 2c 28 70 2d 3e 6e 43 6f 6c 2b 38  >aCol,(p->nCol+8
23823 29 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 43 6f 6c  )*sizeof(p->aCol
23824 5b 30 5d 29 29 3b 0a 20 20 20 20 69 66 28 20 61  [0]));.    if( a
23825 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  New==0 ){.      
23826 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
23827 2c 20 7a 29 3b 0a 20 20 20 20 20 20 72 65 74 75  , z);.      retu
23828 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d  rn;.    }.    p-
23829 3e 61 43 6f 6c 20 3d 20 61 4e 65 77 3b 0a 20 20  >aCol = aNew;.  
2382a 7d 0a 20 20 70 43 6f 6c 20 3d 20 26 70 2d 3e 61  }.  pCol = &p->a
2382b 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c 5d 3b 0a 20 20  Col[p->nCol];.  
2382c 6d 65 6d 73 65 74 28 70 43 6f 6c 2c 20 30 2c 20  memset(pCol, 0, 
2382d 73 69 7a 65 6f 66 28 70 2d 3e 61 43 6f 6c 5b 30  sizeof(p->aCol[0
2382e 5d 29 29 3b 0a 20 20 70 43 6f 6c 2d 3e 7a 4e 61  ]));.  pCol->zNa
2382f 6d 65 20 3d 20 7a 3b 0a 20 0a 20 20 2f 2a 20 49  me = z;. .  /* I
23830 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 74 79  f there is no ty
23831 70 65 20 73 70 65 63 69 66 69 65 64 2c 20 63 6f  pe specified, co
23832 6c 75 6d 6e 73 20 68 61 76 65 20 74 68 65 20 64  lumns have the d
23833 65 66 61 75 6c 74 20 61 66 66 69 6e 69 74 79 0a  efault affinity.
23834 20 20 2a 2a 20 27 4e 4f 4e 45 27 2e 20 49 66 20    ** 'NONE'. If 
23835 74 68 65 72 65 20 69 73 20 61 20 74 79 70 65 20  there is a type 
23836 73 70 65 63 69 66 69 65 64 2c 20 74 68 65 6e 20  specified, then 
23837 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e  sqlite3AddColumn
23838 54 79 70 65 28 29 20 77 69 6c 6c 0a 20 20 2a 2a  Type() will.  **
23839 20 62 65 20 63 61 6c 6c 65 64 20 6e 65 78 74 20   be called next 
2383a 74 6f 20 73 65 74 20 70 43 6f 6c 2d 3e 61 66 66  to set pCol->aff
2383b 69 6e 69 74 79 20 63 6f 72 72 65 63 74 6c 79 2e  inity correctly.
2383c 0a 20 20 2a 2f 0a 20 20 70 43 6f 6c 2d 3e 61 66  .  */.  pCol->af
2383d 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f  finity = SQLITE_
2383e 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 70 2d 3e 6e  AFF_NONE;.  p->n
2383f 43 6f 6c 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Col++;.}../*.** 
23840 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
23841 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61  called by the pa
23842 72 73 65 72 20 77 68 69 6c 65 20 69 6e 20 74 68  rser while in th
23843 65 20 6d 69 64 64 6c 65 20 6f 66 0a 2a 2a 20 70  e middle of.** p
23844 61 72 73 69 6e 67 20 61 20 43 52 45 41 54 45 20  arsing a CREATE 
23845 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e  TABLE statement.
23846 20 20 41 20 22 4e 4f 54 20 4e 55 4c 4c 22 20 63    A "NOT NULL" c
23847 6f 6e 73 74 72 61 69 6e 74 20 68 61 73 0a 2a 2a  onstraint has.**
23848 20 62 65 65 6e 20 73 65 65 6e 20 6f 6e 20 61 20   been seen on a 
23849 63 6f 6c 75 6d 6e 2e 20 20 54 68 69 73 20 72 6f  column.  This ro
2384a 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 6e  utine sets the n
2384b 6f 74 4e 75 6c 6c 20 66 6c 61 67 20 6f 6e 0a 2a  otNull flag on.*
2384c 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 75 72  * the column cur
2384d 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e  rently under con
2384e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51  struction..*/.SQ
2384f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
23850 64 20 73 71 6c 69 74 65 33 41 64 64 4e 6f 74 4e  d sqlite3AddNotN
23851 75 6c 6c 28 50 61 72 73 65 20 2a 70 50 61 72 73  ull(Parse *pPars
23852 65 2c 20 69 6e 74 20 6f 6e 45 72 72 6f 72 29 7b  e, int onError){
23853 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 70  .  Table *p;.  p
23854 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54   = pParse->pNewT
23855 61 62 6c 65 3b 0a 20 20 69 66 28 20 70 3d 3d 30  able;.  if( p==0
23856 20 7c 7c 20 4e 45 56 45 52 28 70 2d 3e 6e 43 6f   || NEVER(p->nCo
23857 6c 3c 31 29 20 29 20 72 65 74 75 72 6e 3b 0a 20  l<1) ) return;. 
23858 20 70 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c   p->aCol[p->nCol
23859 2d 31 5d 2e 6e 6f 74 4e 75 6c 6c 20 3d 20 28 75  -1].notNull = (u
2385a 38 29 6f 6e 45 72 72 6f 72 3b 0a 7d 0a 0a 2f 2a  8)onError;.}../*
2385b 0a 2a 2a 20 53 63 61 6e 20 74 68 65 20 63 6f 6c  .** Scan the col
2385c 75 6d 6e 20 74 79 70 65 20 6e 61 6d 65 20 7a 54  umn type name zT
2385d 79 70 65 20 28 6c 65 6e 67 74 68 20 6e 54 79 70  ype (length nTyp
2385e 65 29 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68  e) and return th
2385f 65 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20  e.** associated 
23860 61 66 66 69 6e 69 74 79 20 74 79 70 65 2e 0a 2a  affinity type..*
23861 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
23862 65 20 64 6f 65 73 20 61 20 63 61 73 65 2d 69 6e  e does a case-in
23863 64 65 70 65 6e 64 65 6e 74 20 73 65 61 72 63 68  dependent search
23864 20 6f 66 20 7a 54 79 70 65 20 66 6f 72 20 74 68   of zType for th
23865 65 20 0a 2a 2a 20 73 75 62 73 74 72 69 6e 67 73  e .** substrings
23866 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   in the followin
23867 67 20 74 61 62 6c 65 2e 20 49 66 20 6f 6e 65 20  g table. If one 
23868 6f 66 20 74 68 65 20 73 75 62 73 74 72 69 6e 67  of the substring
23869 73 20 69 73 0a 2a 2a 20 66 6f 75 6e 64 2c 20 74  s is.** found, t
2386a 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
2386b 20 61 66 66 69 6e 69 74 79 20 69 73 20 72 65 74   affinity is ret
2386c 75 72 6e 65 64 2e 20 49 66 20 7a 54 79 70 65 20  urned. If zType 
2386d 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 6d 6f 72 65  contains.** more
2386e 20 74 68 61 6e 20 6f 6e 65 20 6f 66 20 74 68 65   than one of the
2386f 20 73 75 62 73 74 72 69 6e 67 73 2c 20 65 6e 74   substrings, ent
23870 72 69 65 73 20 74 6f 77 61 72 64 20 74 68 65 20  ries toward the 
23871 74 6f 70 20 6f 66 20 0a 2a 2a 20 74 68 65 20 74  top of .** the t
23872 61 62 6c 65 20 74 61 6b 65 20 70 72 69 6f 72 69  able take priori
23873 74 79 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  ty. For example,
23874 20 69 66 20 7a 54 79 70 65 20 69 73 20 27 42 4c   if zType is 'BL
23875 4f 42 49 4e 54 27 2c 20 0a 2a 2a 20 53 51 4c 49  OBINT', .** SQLI
23876 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 69  TE_AFF_INTEGER i
23877 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
23878 2a 20 53 75 62 73 74 72 69 6e 67 20 20 20 20 20  * Substring     
23879 7c 20 41 66 66 69 6e 69 74 79 0a 2a 2a 20 2d 2d  | Affinity.** --
2387a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2387b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a  --------------.*
2387c 2a 20 27 49 4e 54 27 20 20 20 20 20 20 20 20 20  * 'INT'         
2387d 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54  | SQLITE_AFF_INT
2387e 45 47 45 52 0a 2a 2a 20 27 43 48 41 52 27 20 20  EGER.** 'CHAR'  
2387f 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41        | SQLITE_A
23880 46 46 5f 54 45 58 54 0a 2a 2a 20 27 43 4c 4f 42  FF_TEXT.** 'CLOB
23881 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54  '        | SQLIT
23882 45 5f 41 46 46 5f 54 45 58 54 0a 2a 2a 20 27 54  E_AFF_TEXT.** 'T
23883 45 58 54 27 20 20 20 20 20 20 20 20 7c 20 53 51  EXT'        | SQ
23884 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 0a 2a 2a  LITE_AFF_TEXT.**
23885 20 27 42 4c 4f 42 27 20 20 20 20 20 20 20 20 7c   'BLOB'        |
23886 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45   SQLITE_AFF_NONE
23887 0a 2a 2a 20 27 52 45 41 4c 27 20 20 20 20 20 20  .** 'REAL'      
23888 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 52    | SQLITE_AFF_R
23889 45 41 4c 0a 2a 2a 20 27 46 4c 4f 41 27 20 20 20  EAL.** 'FLOA'   
2388a 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46       | SQLITE_AF
2388b 46 5f 52 45 41 4c 0a 2a 2a 20 27 44 4f 55 42 27  F_REAL.** 'DOUB'
2388c 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45          | SQLITE
2388d 5f 41 46 46 5f 52 45 41 4c 0a 2a 2a 0a 2a 2a 20  _AFF_REAL.**.** 
2388e 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 73  If none of the s
2388f 75 62 73 74 72 69 6e 67 73 20 69 6e 20 74 68 65  ubstrings in the
23890 20 61 62 6f 76 65 20 74 61 62 6c 65 20 61 72 65   above table are
23891 20 66 6f 75 6e 64 2c 0a 2a 2a 20 53 51 4c 49 54   found,.** SQLIT
23892 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 69 73  E_AFF_NUMERIC is
23893 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51   returned..*/.SQ
23894 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
23895 72 20 73 71 6c 69 74 65 33 41 66 66 69 6e 69 74  r sqlite3Affinit
23896 79 54 79 70 65 28 63 6f 6e 73 74 20 63 68 61 72  yType(const char
23897 20 2a 7a 49 6e 29 7b 0a 20 20 75 33 32 20 68 20   *zIn){.  u32 h 
23898 3d 20 30 3b 0a 20 20 63 68 61 72 20 61 66 66 20  = 0;.  char aff 
23899 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d  = SQLITE_AFF_NUM
2389a 45 52 49 43 3b 0a 0a 20 20 69 66 28 20 7a 49 6e  ERIC;..  if( zIn
2389b 20 29 20 77 68 69 6c 65 28 20 7a 49 6e 5b 30 5d   ) while( zIn[0]
2389c 20 29 7b 0a 20 20 20 20 68 20 3d 20 28 68 3c 3c   ){.    h = (h<<
2389d 38 29 20 2b 20 73 71 6c 69 74 65 33 55 70 70 65  8) + sqlite3Uppe
2389e 72 54 6f 4c 6f 77 65 72 5b 28 2a 7a 49 6e 29 26  rToLower[(*zIn)&
2389f 30 78 66 66 5d 3b 0a 20 20 20 20 7a 49 6e 2b 2b  0xff];.    zIn++
238a0 3b 0a 20 20 20 20 69 66 28 20 68 3d 3d 28 28 27  ;.    if( h==(('
238a1 63 27 3c 3c 32 34 29 2b 28 27 68 27 3c 3c 31 36  c'<<24)+('h'<<16
238a2 29 2b 28 27 61 27 3c 3c 38 29 2b 27 72 27 29 20  )+('a'<<8)+'r') 
238a3 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ){             /
238a4 2a 20 43 48 41 52 20 2a 2f 0a 20 20 20 20 20 20  * CHAR */.      
238a5 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46  aff = SQLITE_AFF
238a6 5f 54 45 58 54 3b 20 0a 20 20 20 20 7d 65 6c 73  _TEXT; .    }els
238a7 65 20 69 66 28 20 68 3d 3d 28 28 27 63 27 3c 3c  e if( h==(('c'<<
238a8 32 34 29 2b 28 27 6c 27 3c 3c 31 36 29 2b 28 27  24)+('l'<<16)+('
238a9 6f 27 3c 3c 38 29 2b 27 62 27 29 20 29 7b 20 20  o'<<8)+'b') ){  
238aa 20 20 20 20 20 2f 2a 20 43 4c 4f 42 20 2a 2f 0a       /* CLOB */.
238ab 20 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49        aff = SQLI
238ac 54 45 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20 20  TE_AFF_TEXT;.   
238ad 20 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 28   }else if( h==((
238ae 27 74 27 3c 3c 32 34 29 2b 28 27 65 27 3c 3c 31  't'<<24)+('e'<<1
238af 36 29 2b 28 27 78 27 3c 3c 38 29 2b 27 74 27 29  6)+('x'<<8)+'t')
238b0 20 29 7b 20 20 20 20 20 20 20 2f 2a 20 54 45 58   ){       /* TEX
238b1 54 20 2a 2f 0a 20 20 20 20 20 20 61 66 66 20 3d  T */.      aff =
238b2 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54   SQLITE_AFF_TEXT
238b3 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
238b4 68 3d 3d 28 28 27 62 27 3c 3c 32 34 29 2b 28 27  h==(('b'<<24)+('
238b5 6c 27 3c 3c 31 36 29 2b 28 27 6f 27 3c 3c 38 29  l'<<16)+('o'<<8)
238b6 2b 27 62 27 29 20 20 20 20 20 20 20 20 20 20 2f  +'b')          /
238b7 2a 20 42 4c 4f 42 20 2a 2f 0a 20 20 20 20 20 20  * BLOB */.      
238b8 20 20 26 26 20 28 61 66 66 3d 3d 53 51 4c 49 54    && (aff==SQLIT
238b9 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 7c 7c  E_AFF_NUMERIC ||
238ba 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46   aff==SQLITE_AFF
238bb 5f 52 45 41 4c 29 20 29 7b 0a 20 20 20 20 20 20  _REAL) ){.      
238bc 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46  aff = SQLITE_AFF
238bd 5f 4e 4f 4e 45 3b 0a 23 69 66 6e 64 65 66 20 53  _NONE;.#ifndef S
238be 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
238bf 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 7d 65  ING_POINT.    }e
238c0 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27 72 27  lse if( h==(('r'
238c1 3c 3c 32 34 29 2b 28 27 65 27 3c 3c 31 36 29 2b  <<24)+('e'<<16)+
238c2 28 27 61 27 3c 3c 38 29 2b 27 6c 27 29 20 20 20  ('a'<<8)+'l')   
238c3 20 20 20 20 20 20 20 2f 2a 20 52 45 41 4c 20 2a         /* REAL *
238c4 2f 0a 20 20 20 20 20 20 20 20 26 26 20 61 66 66  /.        && aff
238c5 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d  ==SQLITE_AFF_NUM
238c6 45 52 49 43 20 29 7b 0a 20 20 20 20 20 20 61 66  ERIC ){.      af
238c7 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 52  f = SQLITE_AFF_R
238c8 45 41 4c 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  EAL;.    }else i
238c9 66 28 20 68 3d 3d 28 28 27 66 27 3c 3c 32 34 29  f( h==(('f'<<24)
238ca 2b 28 27 6c 27 3c 3c 31 36 29 2b 28 27 6f 27 3c  +('l'<<16)+('o'<
238cb 3c 38 29 2b 27 61 27 29 20 20 20 20 20 20 20 20  <8)+'a')        
238cc 20 20 2f 2a 20 46 4c 4f 41 20 2a 2f 0a 20 20 20    /* FLOA */.   
238cd 20 20 20 20 20 26 26 20 61 66 66 3d 3d 53 51 4c       && aff==SQL
238ce 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20  ITE_AFF_NUMERIC 
238cf 29 7b 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53  ){.      aff = S
238d0 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 3b 0a  QLITE_AFF_REAL;.
238d1 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68 3d      }else if( h=
238d2 3d 28 28 27 64 27 3c 3c 32 34 29 2b 28 27 6f 27  =(('d'<<24)+('o'
238d3 3c 3c 31 36 29 2b 28 27 75 27 3c 3c 38 29 2b 27  <<16)+('u'<<8)+'
238d4 62 27 29 20 20 20 20 20 20 20 20 20 20 2f 2a 20  b')          /* 
238d5 44 4f 55 42 20 2a 2f 0a 20 20 20 20 20 20 20 20  DOUB */.        
238d6 26 26 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41  && aff==SQLITE_A
238d7 46 46 5f 4e 55 4d 45 52 49 43 20 29 7b 0a 20 20  FF_NUMERIC ){.  
238d8 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45      aff = SQLITE
238d9 5f 41 46 46 5f 52 45 41 4c 3b 0a 23 65 6e 64 69  _AFF_REAL;.#endi
238da 66 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  f.    }else if( 
238db 28 68 26 30 78 30 30 46 46 46 46 46 46 29 3d 3d  (h&0x00FFFFFF)==
238dc 28 28 27 69 27 3c 3c 31 36 29 2b 28 27 6e 27 3c  (('i'<<16)+('n'<
238dd 3c 38 29 2b 27 74 27 29 20 29 7b 20 20 20 20 2f  <8)+'t') ){    /
238de 2a 20 49 4e 54 20 2a 2f 0a 20 20 20 20 20 20 61  * INT */.      a
238df 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f  ff = SQLITE_AFF_
238e0 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 20 62  INTEGER;.      b
238e1 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
238e2 0a 20 20 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d  .  return aff;.}
238e3 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
238e4 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 62  tine is called b
238e5 79 20 74 68 65 20 70 61 72 73 65 72 20 77 68 69  y the parser whi
238e6 6c 65 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  le in the middle
238e7 20 6f 66 0a 2a 2a 20 70 61 72 73 69 6e 67 20 61   of.** parsing a
238e8 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74   CREATE TABLE st
238e9 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 70 46  atement.  The pF
238ea 69 72 73 74 20 74 6f 6b 65 6e 20 69 73 20 74 68  irst token is th
238eb 65 20 66 69 72 73 74 0a 2a 2a 20 74 6f 6b 65 6e  e first.** token
238ec 20 69 6e 20 74 68 65 20 73 65 71 75 65 6e 63 65   in the sequence
238ed 20 6f 66 20 74 6f 6b 65 6e 73 20 74 68 61 74 20   of tokens that 
238ee 64 65 73 63 72 69 62 65 20 74 68 65 20 74 79 70  describe the typ
238ef 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6c 75  e of the.** colu
238f0 6d 6e 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64  mn currently und
238f1 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e  er construction.
238f2 20 20 20 70 4c 61 73 74 20 69 73 20 74 68 65 20     pLast is the 
238f3 6c 61 73 74 20 74 6f 6b 65 6e 0a 2a 2a 20 69 6e  last token.** in
238f4 20 74 68 65 20 73 65 71 75 65 6e 63 65 2e 20 20   the sequence.  
238f5 55 73 65 20 74 68 69 73 20 69 6e 66 6f 72 6d 61  Use this informa
238f6 74 69 6f 6e 20 74 6f 20 63 6f 6e 73 74 72 75 63  tion to construc
238f7 74 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 74 68  t a string.** th
238f8 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  at contains the 
238f9 74 79 70 65 6e 61 6d 65 20 6f 66 20 74 68 65 20  typename of the 
238fa 63 6f 6c 75 6d 6e 20 61 6e 64 20 73 74 6f 72 65  column and store
238fb 20 74 68 61 74 20 73 74 72 69 6e 67 0a 2a 2a 20   that string.** 
238fc 69 6e 20 7a 54 79 70 65 2e 0a 2a 2f 20 0a 53 51  in zType..*/ .SQ
238fd 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
238fe 64 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75  d sqlite3AddColu
238ff 6d 6e 54 79 70 65 28 50 61 72 73 65 20 2a 70 50  mnType(Parse *pP
23900 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 54 79  arse, Token *pTy
23901 70 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b  pe){.  Table *p;
23902 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b  .  Column *pCol;
23903 0a 0a 20 20 70 20 3d 20 70 50 61 72 73 65 2d 3e  ..  p = pParse->
23904 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69 66 28  pNewTable;.  if(
23905 20 70 3d 3d 30 20 7c 7c 20 4e 45 56 45 52 28 70   p==0 || NEVER(p
23906 2d 3e 6e 43 6f 6c 3c 31 29 20 29 20 72 65 74 75  ->nCol<1) ) retu
23907 72 6e 3b 0a 20 20 70 43 6f 6c 20 3d 20 26 70 2d  rn;.  pCol = &p-
23908 3e 61 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c 2d 31 5d  >aCol[p->nCol-1]
23909 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 6f 6c  ;.  assert( pCol
2390a 2d 3e 7a 54 79 70 65 3d 3d 30 20 29 3b 0a 20 20  ->zType==0 );.  
2390b 70 43 6f 6c 2d 3e 7a 54 79 70 65 20 3d 20 73 71  pCol->zType = sq
2390c 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
2390d 65 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  en(pParse->db, p
2390e 54 79 70 65 29 3b 0a 20 20 70 43 6f 6c 2d 3e 61  Type);.  pCol->a
2390f 66 66 69 6e 69 74 79 20 3d 20 73 71 6c 69 74 65  ffinity = sqlite
23910 33 41 66 66 69 6e 69 74 79 54 79 70 65 28 70 43  3AffinityType(pC
23911 6f 6c 2d 3e 7a 54 79 70 65 29 3b 0a 7d 0a 0a 2f  ol->zType);.}../
23912 2a 0a 2a 2a 20 54 68 65 20 65 78 70 72 65 73 73  *.** The express
23913 69 6f 6e 20 69 73 20 74 68 65 20 64 65 66 61 75  ion is the defau
23914 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65  lt value for the
23915 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 61   most recently a
23916 64 64 65 64 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f  dded column.** o
23917 66 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 72  f the table curr
23918 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73  ently under cons
23919 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  truction..**.** 
2391a 44 65 66 61 75 6c 74 20 76 61 6c 75 65 20 65 78  Default value ex
2391b 70 72 65 73 73 69 6f 6e 73 20 6d 75 73 74 20 62  pressions must b
2391c 65 20 63 6f 6e 73 74 61 6e 74 2e 20 20 52 61 69  e constant.  Rai
2391d 73 65 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20  se an exception 
2391e 69 66 20 74 68 69 73 0a 2a 2a 20 69 73 20 6e 6f  if this.** is no
2391f 74 20 74 68 65 20 63 61 73 65 2e 0a 2a 2a 0a 2a  t the case..**.*
23920 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
23921 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20  s called by the 
23922 70 61 72 73 65 72 20 77 68 69 6c 65 20 69 6e 20  parser while in 
23923 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 0a 2a 2a  the middle of.**
23924 20 70 61 72 73 69 6e 67 20 61 20 43 52 45 41 54   parsing a CREAT
23925 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e  E TABLE statemen
23926 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
23927 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
23928 33 41 64 64 44 65 66 61 75 6c 74 56 61 6c 75 65  3AddDefaultValue
23929 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
2392a 45 78 70 72 53 70 61 6e 20 2a 70 53 70 61 6e 29  ExprSpan *pSpan)
2392b 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20  {.  Table *p;.  
2392c 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20  Column *pCol;.  
2392d 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
2392e 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70 20 3d 20  arse->db;.  p = 
2392f 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
23930 65 3b 0a 20 20 69 66 28 20 70 21 3d 30 20 29 7b  e;.  if( p!=0 ){
23931 0a 20 20 20 20 70 43 6f 6c 20 3d 20 26 28 70 2d  .    pCol = &(p-
23932 3e 61 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c 2d 31 5d  >aCol[p->nCol-1]
23933 29 3b 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69  );.    if( !sqli
23934 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e  te3ExprIsConstan
23935 74 4f 72 46 75 6e 63 74 69 6f 6e 28 70 53 70 61  tOrFunction(pSpa
23936 6e 2d 3e 70 45 78 70 72 29 20 29 7b 0a 20 20 20  n->pExpr) ){.   
23937 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
23938 73 67 28 70 50 61 72 73 65 2c 20 22 64 65 66 61  sg(pParse, "defa
23939 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 63 6f 6c  ult value of col
2393a 75 6d 6e 20 5b 25 73 5d 20 69 73 20 6e 6f 74 20  umn [%s] is not 
2393b 63 6f 6e 73 74 61 6e 74 22 2c 0a 20 20 20 20 20  constant",.     
2393c 20 20 20 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65       pCol->zName
2393d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
2393e 20 20 20 20 2f 2a 20 41 20 63 6f 70 79 20 6f 66      /* A copy of
2393f 20 70 45 78 70 72 20 69 73 20 75 73 65 64 20 69   pExpr is used i
23940 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 6f 72  nstead of the or
23941 69 67 69 6e 61 6c 2c 20 61 73 20 70 45 78 70 72  iginal, as pExpr
23942 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20 20 20 20   contains.      
23943 2a 2a 20 74 6f 6b 65 6e 73 20 74 68 61 74 20 70  ** tokens that p
23944 6f 69 6e 74 20 74 6f 20 76 6f 6c 61 74 69 6c 65  oint to volatile
23945 20 6d 65 6d 6f 72 79 2e 20 54 68 65 20 27 73 70   memory. The 'sp
23946 61 6e 27 20 6f 66 20 74 68 65 20 65 78 70 72 65  an' of the expre
23947 73 73 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 69  ssion.      ** i
23948 73 20 72 65 71 75 69 72 65 64 20 62 79 20 70 72  s required by pr
23949 61 67 6d 61 20 74 61 62 6c 65 5f 69 6e 66 6f 2e  agma table_info.
2394a 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
2394b 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
2394c 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 70 44 66 6c  e(db, pCol->pDfl
2394d 74 29 3b 0a 20 20 20 20 20 20 70 43 6f 6c 2d 3e  t);.      pCol->
2394e 70 44 66 6c 74 20 3d 20 73 71 6c 69 74 65 33 45  pDflt = sqlite3E
2394f 78 70 72 44 75 70 28 64 62 2c 20 70 53 70 61 6e  xprDup(db, pSpan
23950 2d 3e 70 45 78 70 72 2c 20 45 58 50 52 44 55 50  ->pExpr, EXPRDUP
23951 5f 52 45 44 55 43 45 29 3b 0a 20 20 20 20 20 20  _REDUCE);.      
23952 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
23953 2c 20 70 43 6f 6c 2d 3e 7a 44 66 6c 74 29 3b 0a  , pCol->zDflt);.
23954 20 20 20 20 20 20 70 43 6f 6c 2d 3e 7a 44 66 6c        pCol->zDfl
23955 74 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  t = sqlite3DbStr
23956 4e 44 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29  NDup(db, (char*)
23957 70 53 70 61 6e 2d 3e 7a 53 74 61 72 74 2c 0a 20  pSpan->zStart,. 
23958 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23959 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2395a 20 20 20 20 28 69 6e 74 29 28 70 53 70 61 6e 2d      (int)(pSpan-
2395b 3e 7a 45 6e 64 20 2d 20 70 53 70 61 6e 2d 3e 7a  >zEnd - pSpan->z
2395c 53 74 61 72 74 29 29 3b 0a 20 20 20 20 7d 0a 20  Start));.    }. 
2395d 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72   }.  sqlite3Expr
2395e 44 65 6c 65 74 65 28 64 62 2c 20 70 53 70 61 6e  Delete(db, pSpan
2395f 2d 3e 70 45 78 70 72 29 3b 0a 7d 0a 0a 2f 2a 0a  ->pExpr);.}../*.
23960 2a 2a 20 44 65 73 69 67 6e 61 74 65 20 74 68 65  ** Designate the
23961 20 50 52 49 4d 41 52 59 20 4b 45 59 20 66 6f 72   PRIMARY KEY for
23962 20 74 68 65 20 74 61 62 6c 65 2e 20 20 70 4c 69   the table.  pLi
23963 73 74 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20  st is a list of 
23964 6e 61 6d 65 73 20 0a 2a 2a 20 6f 66 20 63 6f 6c  names .** of col
23965 75 6d 6e 73 20 74 68 61 74 20 66 6f 72 6d 20 74  umns that form t
23966 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 2e 20  he primary key. 
23967 20 49 66 20 70 4c 69 73 74 20 69 73 20 4e 55 4c   If pList is NUL
23968 4c 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 6d  L, then the.** m
23969 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 61 64 64  ost recently add
2396a 65 64 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  ed column of the
2396b 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 70 72   table is the pr
2396c 69 6d 61 72 79 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a  imary key..**.**
2396d 20 41 20 74 61 62 6c 65 20 63 61 6e 20 68 61 76   A table can hav
2396e 65 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20 70 72  e at most one pr
2396f 69 6d 61 72 79 20 6b 65 79 2e 20 20 49 66 20 74  imary key.  If t
23970 68 65 20 74 61 62 6c 65 20 61 6c 72 65 61 64 79  he table already
23971 20 68 61 73 0a 2a 2a 20 61 20 70 72 69 6d 61 72   has.** a primar
23972 79 20 6b 65 79 20 28 61 6e 64 20 74 68 69 73 20  y key (and this 
23973 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 72  is the second pr
23974 69 6d 61 72 79 20 6b 65 79 29 20 74 68 65 6e 20  imary key) then 
23975 63 72 65 61 74 65 20 61 6e 0a 2a 2a 20 65 72 72  create an.** err
23976 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  or..**.** If the
23977 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69 73 20   PRIMARY KEY is 
23978 6f 6e 20 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75  on a single colu
23979 6d 6e 20 77 68 6f 73 65 20 64 61 74 61 74 79 70  mn whose datatyp
2397a 65 20 69 73 20 49 4e 54 45 47 45 52 2c 0a 2a 2a  e is INTEGER,.**
2397b 20 74 68 65 6e 20 77 65 20 77 69 6c 6c 20 74 72   then we will tr
2397c 79 20 74 6f 20 75 73 65 20 74 68 61 74 20 63 6f  y to use that co
2397d 6c 75 6d 6e 20 61 73 20 74 68 65 20 72 6f 77 69  lumn as the rowi
2397e 64 2e 20 20 53 65 74 20 74 68 65 20 54 61 62 6c  d.  Set the Tabl
2397f 65 2e 69 50 4b 65 79 0a 2a 2a 20 66 69 65 6c 64  e.iPKey.** field
23980 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 75 6e   of the table un
23981 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e  der construction
23982 20 74 6f 20 62 65 20 74 68 65 20 69 6e 64 65 78   to be the index
23983 20 6f 66 20 74 68 65 0a 2a 2a 20 49 4e 54 45 47   of the.** INTEG
23984 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63  ER PRIMARY KEY c
23985 6f 6c 75 6d 6e 2e 20 20 54 61 62 6c 65 2e 69 50  olumn.  Table.iP
23986 4b 65 79 20 69 73 20 73 65 74 20 74 6f 20 2d 31  Key is set to -1
23987 20 69 66 20 74 68 65 72 65 20 69 73 0a 2a 2a 20   if there is.** 
23988 6e 6f 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  no INTEGER PRIMA
23989 52 59 20 4b 45 59 2e 0a 2a 2a 0a 2a 2a 20 49 66  RY KEY..**.** If
2398a 20 74 68 65 20 6b 65 79 20 69 73 20 6e 6f 74 20   the key is not 
2398b 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  an INTEGER PRIMA
2398c 52 59 20 4b 45 59 2c 20 74 68 65 6e 20 63 72 65  RY KEY, then cre
2398d 61 74 65 20 61 20 75 6e 69 71 75 65 0a 2a 2a 20  ate a unique.** 
2398e 69 6e 64 65 78 20 66 6f 72 20 74 68 65 20 6b 65  index for the ke
2398f 79 2e 20 20 4e 6f 20 69 6e 64 65 78 20 69 73 20  y.  No index is 
23990 63 72 65 61 74 65 64 20 66 6f 72 20 49 4e 54 45  created for INTE
23991 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 73  GER PRIMARY KEYs
23992 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
23993 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
23994 41 64 64 50 72 69 6d 61 72 79 4b 65 79 28 0a 20  AddPrimaryKey(. 
23995 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
23996 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
23997 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c  ntext */.  ExprL
23998 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 2f 2a 20  ist *pList,  /* 
23999 4c 69 73 74 20 6f 66 20 66 69 65 6c 64 20 6e 61  List of field na
2399a 6d 65 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65  mes to be indexe
2399b 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 72  d */.  int onErr
2399c 6f 72 2c 20 20 20 20 20 20 2f 2a 20 57 68 61 74  or,      /* What
2399d 20 74 6f 20 64 6f 20 77 69 74 68 20 61 20 75 6e   to do with a un
2399e 69 71 75 65 6e 65 73 73 20 63 6f 6e 66 6c 69 63  iqueness conflic
2399f 74 20 2a 2f 0a 20 20 69 6e 74 20 61 75 74 6f 49  t */.  int autoI
239a0 6e 63 2c 20 20 20 20 20 20 2f 2a 20 54 72 75 65  nc,      /* True
239a1 20 69 66 20 74 68 65 20 41 55 54 4f 49 4e 43 52   if the AUTOINCR
239a2 45 4d 45 4e 54 20 6b 65 79 77 6f 72 64 20 69 73  EMENT keyword is
239a3 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 6e   present */.  in
239a4 74 20 73 6f 72 74 4f 72 64 65 72 20 20 20 20 20  t sortOrder     
239a5 2f 2a 20 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43  /* SQLITE_SO_ASC
239a6 20 6f 72 20 53 51 4c 49 54 45 5f 53 4f 5f 44 45   or SQLITE_SO_DE
239a7 53 43 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65  SC */.){.  Table
239a8 20 2a 70 54 61 62 20 3d 20 70 50 61 72 73 65 2d   *pTab = pParse-
239a9 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 63 68  >pNewTable;.  ch
239aa 61 72 20 2a 7a 54 79 70 65 20 3d 20 30 3b 0a 20  ar *zType = 0;. 
239ab 20 69 6e 74 20 69 43 6f 6c 20 3d 20 2d 31 2c 20   int iCol = -1, 
239ac 69 3b 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30  i;.  if( pTab==0
239ad 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56   || IN_DECLARE_V
239ae 54 41 42 20 29 20 67 6f 74 6f 20 70 72 69 6d 61  TAB ) goto prima
239af 72 79 5f 6b 65 79 5f 65 78 69 74 3b 0a 20 20 69  ry_key_exit;.  i
239b0 66 28 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67  f( pTab->tabFlag
239b1 73 20 26 20 54 46 5f 48 61 73 50 72 69 6d 61 72  s & TF_HasPrimar
239b2 79 4b 65 79 20 29 7b 0a 20 20 20 20 73 71 6c 69  yKey ){.    sqli
239b3 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
239b4 73 65 2c 20 0a 20 20 20 20 20 20 22 74 61 62 6c  se, .      "tabl
239b5 65 20 5c 22 25 73 5c 22 20 68 61 73 20 6d 6f 72  e \"%s\" has mor
239b6 65 20 74 68 61 6e 20 6f 6e 65 20 70 72 69 6d 61  e than one prima
239b7 72 79 20 6b 65 79 22 2c 20 70 54 61 62 2d 3e 7a  ry key", pTab->z
239b8 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20  Name);.    goto 
239b9 70 72 69 6d 61 72 79 5f 6b 65 79 5f 65 78 69 74  primary_key_exit
239ba 3b 0a 20 20 7d 0a 20 20 70 54 61 62 2d 3e 74 61  ;.  }.  pTab->ta
239bb 62 46 6c 61 67 73 20 7c 3d 20 54 46 5f 48 61 73  bFlags |= TF_Has
239bc 50 72 69 6d 61 72 79 4b 65 79 3b 0a 20 20 69 66  PrimaryKey;.  if
239bd 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20  ( pList==0 ){.  
239be 20 20 69 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 6e    iCol = pTab->n
239bf 43 6f 6c 20 2d 20 31 3b 0a 20 20 20 20 70 54 61  Col - 1;.    pTa
239c0 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 69 73  b->aCol[iCol].is
239c1 50 72 69 6d 4b 65 79 20 3d 20 31 3b 0a 20 20 7d  PrimKey = 1;.  }
239c2 65 6c 73 65 7b 0a 20 20 20 20 66 6f 72 28 69 3d  else{.    for(i=
239c3 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70  0; i<pList->nExp
239c4 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 66  r; i++){.      f
239c5 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c  or(iCol=0; iCol<
239c6 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c  pTab->nCol; iCol
239c7 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ++){.        if(
239c8 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
239c9 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d  pList->a[i].zNam
239ca 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43  e, pTab->aCol[iC
239cb 6f 6c 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b  ol].zName)==0 ){
239cc 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
239cd 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
239ce 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 43    }.      if( iC
239cf 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b  ol<pTab->nCol ){
239d0 0a 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e 61  .        pTab->a
239d1 43 6f 6c 5b 69 43 6f 6c 5d 2e 69 73 50 72 69 6d  Col[iCol].isPrim
239d2 4b 65 79 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d  Key = 1;.      }
239d3 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
239d4 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 31 20 29 20  List->nExpr>1 ) 
239d5 69 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 7d 0a 20  iCol = -1;.  }. 
239d6 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20 26 26 20   if( iCol>=0 && 
239d7 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20  iCol<pTab->nCol 
239d8 29 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 70  ){.    zType = p
239d9 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e  Tab->aCol[iCol].
239da 7a 54 79 70 65 3b 0a 20 20 7d 0a 20 20 69 66 28  zType;.  }.  if(
239db 20 7a 54 79 70 65 20 26 26 20 73 71 6c 69 74 65   zType && sqlite
239dc 33 53 74 72 49 43 6d 70 28 7a 54 79 70 65 2c 20  3StrICmp(zType, 
239dd 22 49 4e 54 45 47 45 52 22 29 3d 3d 30 0a 20 20  "INTEGER")==0.  
239de 20 20 20 20 20 20 26 26 20 73 6f 72 74 4f 72 64        && sortOrd
239df 65 72 3d 3d 53 51 4c 49 54 45 5f 53 4f 5f 41 53  er==SQLITE_SO_AS
239e0 43 20 29 7b 0a 20 20 20 20 70 54 61 62 2d 3e 69  C ){.    pTab->i
239e1 50 4b 65 79 20 3d 20 69 43 6f 6c 3b 0a 20 20 20  PKey = iCol;.   
239e2 20 70 54 61 62 2d 3e 6b 65 79 43 6f 6e 66 20 3d   pTab->keyConf =
239e3 20 28 75 38 29 6f 6e 45 72 72 6f 72 3b 0a 20 20   (u8)onError;.  
239e4 20 20 61 73 73 65 72 74 28 20 61 75 74 6f 49 6e    assert( autoIn
239e5 63 3d 3d 30 20 7c 7c 20 61 75 74 6f 49 6e 63 3d  c==0 || autoInc=
239e6 3d 31 20 29 3b 0a 20 20 20 20 70 54 61 62 2d 3e  =1 );.    pTab->
239e7 74 61 62 46 6c 61 67 73 20 7c 3d 20 61 75 74 6f  tabFlags |= auto
239e8 49 6e 63 2a 54 46 5f 41 75 74 6f 69 6e 63 72 65  Inc*TF_Autoincre
239e9 6d 65 6e 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66  ment;.  }else if
239ea 28 20 61 75 74 6f 49 6e 63 20 29 7b 0a 23 69 66  ( autoInc ){.#if
239eb 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
239ec 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20  _AUTOINCREMENT. 
239ed 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
239ee 73 67 28 70 50 61 72 73 65 2c 20 22 41 55 54 4f  sg(pParse, "AUTO
239ef 49 4e 43 52 45 4d 45 4e 54 20 69 73 20 6f 6e 6c  INCREMENT is onl
239f0 79 20 61 6c 6c 6f 77 65 64 20 6f 6e 20 61 6e 20  y allowed on an 
239f1 22 0a 20 20 20 20 20 20 20 22 49 4e 54 45 47 45  ".       "INTEGE
239f2 52 20 50 52 49 4d 41 52 59 20 4b 45 59 22 29 3b  R PRIMARY KEY");
239f3 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b  .#endif.  }else{
239f4 0a 20 20 20 20 73 71 6c 69 74 65 33 43 72 65 61  .    sqlite3Crea
239f5 74 65 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20  teIndex(pParse, 
239f6 30 2c 20 30 2c 20 30 2c 20 70 4c 69 73 74 2c 20  0, 0, 0, pList, 
239f7 6f 6e 45 72 72 6f 72 2c 20 30 2c 20 30 2c 20 73  onError, 0, 0, s
239f8 6f 72 74 4f 72 64 65 72 2c 20 30 29 3b 0a 20 20  ortOrder, 0);.  
239f9 20 20 70 4c 69 73 74 20 3d 20 30 3b 0a 20 20 7d    pList = 0;.  }
239fa 0a 0a 70 72 69 6d 61 72 79 5f 6b 65 79 5f 65 78  ..primary_key_ex
239fb 69 74 3a 0a 20 20 73 71 6c 69 74 65 33 45 78 70  it:.  sqlite3Exp
239fc 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72  rListDelete(pPar
239fd 73 65 2d 3e 64 62 2c 20 70 4c 69 73 74 29 3b 0a  se->db, pList);.
239fe 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a    return;.}../*.
239ff 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 43 48 45  ** Add a new CHE
23a00 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 74 6f  CK constraint to
23a01 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 72 65   the table curre
23a02 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74  ntly under const
23a03 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49  ruction..*/.SQLI
23a04 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
23a05 73 71 6c 69 74 65 33 41 64 64 43 68 65 63 6b 43  sqlite3AddCheckC
23a06 6f 6e 73 74 72 61 69 6e 74 28 0a 20 20 50 61 72  onstraint(.  Par
23a07 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f  se *pParse,    /
23a08 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
23a09 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 43 68  t */.  Expr *pCh
23a0a 65 63 6b 45 78 70 72 20 20 2f 2a 20 54 68 65 20  eckExpr  /* The 
23a0b 63 68 65 63 6b 20 65 78 70 72 65 73 73 69 6f 6e  check expression
23a0c 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   */.){.  sqlite3
23a0d 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
23a0e 62 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  b;.#ifndef SQLIT
23a0f 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 54  E_OMIT_CHECK.  T
23a10 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 50 61  able *pTab = pPa
23a11 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a  rse->pNewTable;.
23a12 20 20 69 66 28 20 70 54 61 62 20 26 26 20 21 49    if( pTab && !I
23a13 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 29  N_DECLARE_VTAB )
23a14 7b 0a 20 20 20 20 70 54 61 62 2d 3e 70 43 68 65  {.    pTab->pChe
23a15 63 6b 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ck = sqlite3Expr
23a16 41 6e 64 28 64 62 2c 20 70 54 61 62 2d 3e 70 43  And(db, pTab->pC
23a17 68 65 63 6b 2c 20 70 43 68 65 63 6b 45 78 70 72  heck, pCheckExpr
23a18 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69  );.  }else.#endi
23a19 66 0a 20 20 7b 0a 20 20 20 20 73 71 6c 69 74 65  f.  {.    sqlite
23a1a 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
23a1b 70 43 68 65 63 6b 45 78 70 72 29 3b 0a 20 20 7d  pCheckExpr);.  }
23a1c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68  .}../*.** Set th
23a1d 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63  e collation func
23a1e 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 73 74  tion of the most
23a1f 20 72 65 63 65 6e 74 6c 79 20 70 61 72 73 65 64   recently parsed
23a20 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 0a 2a 2a   table column.**
23a21 20 74 6f 20 74 68 65 20 43 6f 6c 6c 53 65 71 20   to the CollSeq 
23a22 67 69 76 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  given..*/.SQLITE
23a23 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
23a24 6c 69 74 65 33 41 64 64 43 6f 6c 6c 61 74 65 54  lite3AddCollateT
23a25 79 70 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  ype(Parse *pPars
23a26 65 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e  e, Token *pToken
23a27 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20  ){.  Table *p;. 
23a28 20 69 6e 74 20 69 3b 0a 20 20 63 68 61 72 20 2a   int i;.  char *
23a29 7a 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20 20  zColl;          
23a2a 20 20 20 20 2f 2a 20 44 65 71 75 6f 74 65 64 20      /* Dequoted 
23a2b 6e 61 6d 65 20 6f 66 20 63 6f 6c 6c 61 74 69 6f  name of collatio
23a2c 6e 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 20 20  n sequence */.  
23a2d 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20  sqlite3 *db;..  
23a2e 69 66 28 20 28 70 20 3d 20 70 50 61 72 73 65 2d  if( (p = pParse-
23a2f 3e 70 4e 65 77 54 61 62 6c 65 29 3d 3d 30 20 29  >pNewTable)==0 )
23a30 20 72 65 74 75 72 6e 3b 0a 20 20 69 20 3d 20 70   return;.  i = p
23a31 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 64 62 20 3d  ->nCol-1;.  db =
23a32 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 7a   pParse->db;.  z
23a33 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4e 61  Coll = sqlite3Na
23a34 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20  meFromToken(db, 
23a35 70 54 6f 6b 65 6e 29 3b 0a 20 20 69 66 28 20 21  pToken);.  if( !
23a36 7a 43 6f 6c 6c 20 29 20 72 65 74 75 72 6e 3b 0a  zColl ) return;.
23a37 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 4c 6f  .  if( sqlite3Lo
23a38 63 61 74 65 43 6f 6c 6c 53 65 71 28 70 50 61 72  cateCollSeq(pPar
23a39 73 65 2c 20 7a 43 6f 6c 6c 29 20 29 7b 0a 20 20  se, zColl) ){.  
23a3a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20    Index *pIdx;. 
23a3b 20 20 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43     p->aCol[i].zC
23a3c 6f 6c 6c 20 3d 20 7a 43 6f 6c 6c 3b 0a 20 20 0a  oll = zColl;.  .
23a3d 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 63 6f      /* If the co
23a3e 6c 75 6d 6e 20 69 73 20 64 65 63 6c 61 72 65 64  lumn is declared
23a3f 20 61 73 20 22 3c 6e 61 6d 65 3e 20 50 52 49 4d   as "<name> PRIM
23a40 41 52 59 20 4b 45 59 20 43 4f 4c 4c 41 54 45 20  ARY KEY COLLATE 
23a41 3c 74 79 70 65 3e 22 2c 0a 20 20 20 20 2a 2a 20  <type>",.    ** 
23a42 74 68 65 6e 20 61 6e 20 69 6e 64 65 78 20 6d 61  then an index ma
23a43 79 20 68 61 76 65 20 62 65 65 6e 20 63 72 65 61  y have been crea
23a44 74 65 64 20 6f 6e 20 74 68 69 73 20 63 6f 6c 75  ted on this colu
23a45 6d 6e 20 62 65 66 6f 72 65 20 74 68 65 0a 20 20  mn before the.  
23a46 20 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 74    ** collation t
23a47 79 70 65 20 77 61 73 20 61 64 64 65 64 2e 20 43  ype was added. C
23a48 6f 72 72 65 63 74 20 74 68 69 73 20 69 66 20 69  orrect this if i
23a49 74 20 69 73 20 74 68 65 20 63 61 73 65 2e 0a 20  t is the case.. 
23a4a 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 70 49     */.    for(pI
23a4b 64 78 3d 70 2d 3e 70 49 6e 64 65 78 3b 20 70 49  dx=p->pIndex; pI
23a4c 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70  dx; pIdx=pIdx->p
23a4d 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 61 73 73  Next){.      ass
23a4e 65 72 74 28 20 70 49 64 78 2d 3e 6e 43 6f 6c 75  ert( pIdx->nColu
23a4f 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 69  mn==1 );.      i
23a50 66 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d  f( pIdx->aiColum
23a51 6e 5b 30 5d 3d 3d 69 20 29 7b 0a 20 20 20 20 20  n[0]==i ){.     
23a52 20 20 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b     pIdx->azColl[
23a53 30 5d 20 3d 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e  0] = p->aCol[i].
23a54 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20  zColl;.      }. 
23a55 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
23a56 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
23a57 64 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 7d 0a  db, zColl);.  }.
23a58 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
23a59 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74  nction returns t
23a5a 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  he collation seq
23a5b 75 65 6e 63 65 20 66 6f 72 20 64 61 74 61 62 61  uence for databa
23a5c 73 65 20 6e 61 74 69 76 65 20 74 65 78 74 0a 2a  se native text.*
23a5d 2a 20 65 6e 63 6f 64 69 6e 67 20 69 64 65 6e 74  * encoding ident
23a5e 69 66 69 65 64 20 62 79 20 74 68 65 20 73 74 72  ified by the str
23a5f 69 6e 67 20 7a 4e 61 6d 65 2c 20 6c 65 6e 67 74  ing zName, lengt
23a60 68 20 6e 4e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49  h nName..**.** I
23a61 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  f the requested 
23a62 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
23a63 63 65 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61  ce is not availa
23a64 62 6c 65 2c 20 6f 72 20 6e 6f 74 20 61 76 61 69  ble, or not avai
23a65 6c 61 62 6c 65 0a 2a 2a 20 69 6e 20 74 68 65 20  lable.** in the 
23a66 64 61 74 61 62 61 73 65 20 6e 61 74 69 76 65 20  database native 
23a67 65 6e 63 6f 64 69 6e 67 2c 20 74 68 65 20 63 6f  encoding, the co
23a68 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79 20  llation factory 
23a69 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f 0a 2a 2a  is invoked to.**
23a6a 20 72 65 71 75 65 73 74 20 69 74 2e 20 49 66 20   request it. If 
23a6b 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61  the collation fa
23a6c 63 74 6f 72 79 20 64 6f 65 73 20 6e 6f 74 20 73  ctory does not s
23a6d 75 70 70 6c 79 20 73 75 63 68 20 61 20 73 65 71  upply such a seq
23a6e 75 65 6e 63 65 2c 0a 2a 2a 20 61 6e 64 20 74 68  uence,.** and th
23a6f 65 20 73 65 71 75 65 6e 63 65 20 69 73 20 61 76  e sequence is av
23a70 61 69 6c 61 62 6c 65 20 69 6e 20 61 6e 6f 74 68  ailable in anoth
23a71 65 72 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67  er text encoding
23a72 2c 20 74 68 65 6e 20 74 68 61 74 20 69 73 0a 2a  , then that is.*
23a73 2a 20 72 65 74 75 72 6e 65 64 20 69 6e 73 74 65  * returned inste
23a74 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20  ad..**.** If no 
23a75 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 20  versions of the 
23a76 72 65 71 75 65 73 74 65 64 20 63 6f 6c 6c 61 74  requested collat
23a77 69 6f 6e 73 20 73 65 71 75 65 6e 63 65 20 61 72  ions sequence ar
23a78 65 20 61 76 61 69 6c 61 62 6c 65 2c 20 6f 72 0a  e available, or.
23a79 2a 2a 20 61 6e 6f 74 68 65 72 20 65 72 72 6f 72  ** another error
23a7a 20 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69 73   occurs, NULL is
23a7b 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 61 6e   returned and an
23a7c 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77   error message w
23a7d 72 69 74 74 65 6e 20 69 6e 74 6f 0a 2a 2a 20 70  ritten into.** p
23a7e 50 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  Parse..**.** Thi
23a7f 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 77  s routine is a w
23a80 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 73 71  rapper around sq
23a81 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71  lite3FindCollSeq
23a82 28 29 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  ().  This routin
23a83 65 0a 2a 2a 20 69 6e 76 6f 6b 65 73 20 74 68 65  e.** invokes the
23a84 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f   collation facto
23a85 72 79 20 69 66 20 74 68 65 20 6e 61 6d 65 64 20  ry if the named 
23a86 63 6f 6c 6c 61 74 69 6f 6e 20 63 61 6e 6e 6f 74  collation cannot
23a87 20 62 65 20 66 6f 75 6e 64 0a 2a 2a 20 61 6e 64   be found.** and
23a88 20 67 65 6e 65 72 61 74 65 73 20 61 6e 20 65 72   generates an er
23a89 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a  ror message..**.
23a8a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 73 71 6c  ** See also: sql
23a8b 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28  ite3FindCollSeq(
23a8c 29 2c 20 73 71 6c 69 74 65 33 47 65 74 43 6f 6c  ), sqlite3GetCol
23a8d 6c 53 65 71 28 29 0a 2a 2f 0a 53 51 4c 49 54 45  lSeq().*/.SQLITE
23a8e 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71  _PRIVATE CollSeq
23a8f 20 2a 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43   *sqlite3LocateC
23a90 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 70 50  ollSeq(Parse *pP
23a91 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72  arse, const char
23a92 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 73 71 6c 69   *zName){.  sqli
23a93 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
23a94 2d 3e 64 62 3b 0a 20 20 75 38 20 65 6e 63 20 3d  ->db;.  u8 enc =
23a95 20 45 4e 43 28 64 62 29 3b 0a 20 20 75 38 20 69   ENC(db);.  u8 i
23a96 6e 69 74 62 75 73 79 20 3d 20 64 62 2d 3e 69 6e  nitbusy = db->in
23a97 69 74 2e 62 75 73 79 3b 0a 20 20 43 6f 6c 6c 53  it.busy;.  CollS
23a98 65 71 20 2a 70 43 6f 6c 6c 3b 0a 0a 20 20 70 43  eq *pColl;..  pC
23a99 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  oll = sqlite3Fin
23a9a 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 65 6e 63  dCollSeq(db, enc
23a9b 2c 20 7a 4e 61 6d 65 2c 20 69 6e 69 74 62 75 73  , zName, initbus
23a9c 79 29 3b 0a 20 20 69 66 28 20 21 69 6e 69 74 62  y);.  if( !initb
23a9d 75 73 79 20 26 26 20 28 21 70 43 6f 6c 6c 20 7c  usy && (!pColl |
23a9e 7c 20 21 70 43 6f 6c 6c 2d 3e 78 43 6d 70 29 20  | !pColl->xCmp) 
23a9f 29 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73  ){.    pColl = s
23aa0 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53 65 71  qlite3GetCollSeq
23aa1 28 64 62 2c 20 65 6e 63 2c 20 70 43 6f 6c 6c 2c  (db, enc, pColl,
23aa2 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28   zName);.    if(
23aa3 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20   !pColl ){.     
23aa4 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
23aa5 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63  (pParse, "no suc
23aa6 68 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  h collation sequ
23aa7 65 6e 63 65 3a 20 25 73 22 2c 20 7a 4e 61 6d 65  ence: %s", zName
23aa8 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
23aa9 72 65 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a  return pColl;.}.
23aaa 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
23aab 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20   code that will 
23aac 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 73 63  increment the sc
23aad 68 65 6d 61 20 63 6f 6f 6b 69 65 2e 0a 2a 2a 0a  hema cookie..**.
23aae 2a 2a 20 54 68 65 20 73 63 68 65 6d 61 20 63 6f  ** The schema co
23aaf 6f 6b 69 65 20 69 73 20 75 73 65 64 20 74 6f 20  okie is used to 
23ab0 64 65 74 65 72 6d 69 6e 65 20 77 68 65 6e 20 74  determine when t
23ab1 68 65 20 73 63 68 65 6d 61 20 66 6f 72 20 74 68  he schema for th
23ab2 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 68  e.** database ch
23ab3 61 6e 67 65 73 2e 20 20 41 66 74 65 72 20 65 61  anges.  After ea
23ab4 63 68 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65  ch schema change
23ab5 2c 20 74 68 65 20 63 6f 6f 6b 69 65 20 76 61 6c  , the cookie val
23ab6 75 65 0a 2a 2a 20 63 68 61 6e 67 65 73 2e 20 20  ue.** changes.  
23ab7 57 68 65 6e 20 61 20 70 72 6f 63 65 73 73 20 66  When a process f
23ab8 69 72 73 74 20 72 65 61 64 73 20 74 68 65 20 73  irst reads the s
23ab9 63 68 65 6d 61 20 69 74 20 72 65 63 6f 72 64 73  chema it records
23aba 20 74 68 65 0a 2a 2a 20 63 6f 6f 6b 69 65 2e 20   the.** cookie. 
23abb 20 54 68 65 72 65 61 66 74 65 72 2c 20 77 68 65   Thereafter, whe
23abc 6e 65 76 65 72 20 69 74 20 67 6f 65 73 20 74 6f  never it goes to
23abd 20 61 63 63 65 73 73 20 74 68 65 20 64 61 74 61   access the data
23abe 62 61 73 65 2c 0a 2a 2a 20 69 74 20 63 68 65 63  base,.** it chec
23abf 6b 73 20 74 68 65 20 63 6f 6f 6b 69 65 20 74 6f  ks the cookie to
23ac0 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 73   make sure the s
23ac1 63 68 65 6d 61 20 68 61 73 20 6e 6f 74 20 63 68  chema has not ch
23ac2 61 6e 67 65 64 0a 2a 2a 20 73 69 6e 63 65 20 69  anged.** since i
23ac3 74 20 77 61 73 20 6c 61 73 74 20 72 65 61 64 2e  t was last read.
23ac4 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 70 6c 61 6e  .**.** This plan
23ac5 20 69 73 20 6e 6f 74 20 63 6f 6d 70 6c 65 74 65   is not complete
23ac6 6c 79 20 62 75 6c 6c 65 74 2d 70 72 6f 6f 66 2e  ly bullet-proof.
23ac7 20 20 49 74 20 69 73 20 70 6f 73 73 69 62 6c 65    It is possible
23ac8 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73 63 68 65   for.** the sche
23ac9 6d 61 20 74 6f 20 63 68 61 6e 67 65 20 6d 75 6c  ma to change mul
23aca 74 69 70 6c 65 20 74 69 6d 65 73 20 61 6e 64 20  tiple times and 
23acb 66 6f 72 20 74 68 65 20 63 6f 6f 6b 69 65 20 74  for the cookie t
23acc 6f 20 62 65 0a 2a 2a 20 73 65 74 20 62 61 63 6b  o be.** set back
23acd 20 74 6f 20 70 72 69 6f 72 20 76 61 6c 75 65 2e   to prior value.
23ace 20 20 42 75 74 20 73 63 68 65 6d 61 20 63 68 61    But schema cha
23acf 6e 67 65 73 20 61 72 65 20 69 6e 66 72 65 71 75  nges are infrequ
23ad0 65 6e 74 0a 2a 2a 20 61 6e 64 20 74 68 65 20 70  ent.** and the p
23ad1 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 68 69  robability of hi
23ad2 74 74 69 6e 67 20 74 68 65 20 73 61 6d 65 20 63  tting the same c
23ad3 6f 6f 6b 69 65 20 76 61 6c 75 65 20 69 73 20 6f  ookie value is o
23ad4 6e 6c 79 0a 2a 2a 20 31 20 63 68 61 6e 63 65 20  nly.** 1 chance 
23ad5 69 6e 20 32 5e 33 32 2e 20 20 53 6f 20 77 65 27  in 2^32.  So we'
23ad6 72 65 20 73 61 66 65 20 65 6e 6f 75 67 68 2e 0a  re safe enough..
23ad7 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
23ad8 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 68  E void sqlite3Ch
23ad9 61 6e 67 65 43 6f 6f 6b 69 65 28 50 61 72 73 65  angeCookie(Parse
23ada 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 44   *pParse, int iD
23adb 62 29 7b 0a 20 20 69 6e 74 20 72 31 20 3d 20 73  b){.  int r1 = s
23adc 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
23add 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69  (pParse);.  sqli
23ade 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
23adf 2d 3e 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 20  ->db;.  Vdbe *v 
23ae0 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  = pParse->pVdbe;
23ae1 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
23ae2 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
23ae3 65 72 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  er, db->aDb[iDb]
23ae4 2e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61  .pSchema->schema
23ae5 5f 63 6f 6f 6b 69 65 2b 31 2c 20 72 31 29 3b 0a  _cookie+1, r1);.
23ae6 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
23ae7 4f 70 33 28 76 2c 20 4f 50 5f 53 65 74 43 6f 6f  Op3(v, OP_SetCoo
23ae8 6b 69 65 2c 20 69 44 62 2c 20 42 54 52 45 45 5f  kie, iDb, BTREE_
23ae9 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 2c 20  SCHEMA_VERSION, 
23aea 72 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65  r1);.  sqlite3Re
23aeb 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
23aec 72 73 65 2c 20 72 31 29 3b 0a 7d 0a 0a 2f 2a 0a  rse, r1);.}../*.
23aed 2a 2a 20 4d 65 61 73 75 72 65 20 74 68 65 20 6e  ** Measure the n
23aee 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74  umber of charact
23aef 65 72 73 20 6e 65 65 64 65 64 20 74 6f 20 6f 75  ers needed to ou
23af0 74 70 75 74 20 74 68 65 20 67 69 76 65 6e 0a 2a  tput the given.*
23af1 2a 20 69 64 65 6e 74 69 66 69 65 72 2e 20 20 54  * identifier.  T
23af2 68 65 20 6e 75 6d 62 65 72 20 72 65 74 75 72 6e  he number return
23af3 65 64 20 69 6e 63 6c 75 64 65 73 20 61 6e 79 20  ed includes any 
23af4 71 75 6f 74 65 73 20 75 73 65 64 0a 2a 2a 20 62  quotes used.** b
23af5 75 74 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c  ut does not incl
23af6 75 64 65 20 74 68 65 20 6e 75 6c 6c 20 74 65 72  ude the null ter
23af7 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54  minator..**.** T
23af8 68 65 20 65 73 74 69 6d 61 74 65 20 69 73 20 63  he estimate is c
23af9 6f 6e 73 65 72 76 61 74 69 76 65 2e 20 20 49 74  onservative.  It
23afa 20 6d 69 67 68 74 20 62 65 20 6c 61 72 67 65 72   might be larger
23afb 20 74 68 61 74 20 77 68 61 74 20 69 73 0a 2a 2a   that what is.**
23afc 20 72 65 61 6c 6c 79 20 6e 65 65 64 65 64 2e 0a   really needed..
23afd 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 64  */.static int id
23afe 65 6e 74 4c 65 6e 67 74 68 28 63 6f 6e 73 74 20  entLength(const 
23aff 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 6e 74 20  char *z){.  int 
23b00 6e 3b 0a 20 20 66 6f 72 28 6e 3d 30 3b 20 2a 7a  n;.  for(n=0; *z
23b01 3b 20 6e 2b 2b 2c 20 7a 2b 2b 29 7b 0a 20 20 20  ; n++, z++){.   
23b02 20 69 66 28 20 2a 7a 3d 3d 27 22 27 20 29 7b 20   if( *z=='"' ){ 
23b03 6e 2b 2b 3b 20 7d 0a 20 20 7d 0a 20 20 72 65 74  n++; }.  }.  ret
23b04 75 72 6e 20 6e 20 2b 20 32 3b 0a 7d 0a 0a 2f 2a  urn n + 2;.}../*
23b05 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61  .** The first pa
23b06 72 61 6d 65 74 65 72 20 69 73 20 61 20 70 6f 69  rameter is a poi
23b07 6e 74 65 72 20 74 6f 20 61 6e 20 6f 75 74 70 75  nter to an outpu
23b08 74 20 62 75 66 66 65 72 2e 20 54 68 65 20 73 65  t buffer. The se
23b09 63 6f 6e 64 20 0a 2a 2a 20 70 61 72 61 6d 65 74  cond .** paramet
23b0a 65 72 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  er is a pointer 
23b0b 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68  to an integer th
23b0c 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  at contains the 
23b0d 6f 66 66 73 65 74 20 61 74 0a 2a 2a 20 77 68 69  offset at.** whi
23b0e 63 68 20 74 6f 20 77 72 69 74 65 20 69 6e 74 6f  ch to write into
23b0f 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66   the output buff
23b10 65 72 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f  er. This functio
23b11 6e 20 63 6f 70 69 65 73 20 74 68 65 0a 2a 2a 20  n copies the.** 
23b12 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 73  nul-terminated s
23b13 74 72 69 6e 67 20 70 6f 69 6e 74 65 64 20 74 6f  tring pointed to
23b14 20 62 79 20 74 68 65 20 74 68 69 72 64 20 70 61   by the third pa
23b15 72 61 6d 65 74 65 72 2c 20 7a 53 69 67 6e 65 64  rameter, zSigned
23b16 49 64 65 6e 74 2c 0a 2a 2a 20 74 6f 20 74 68 65  Ident,.** to the
23b17 20 73 70 65 63 69 66 69 65 64 20 6f 66 66 73 65   specified offse
23b18 74 20 69 6e 20 74 68 65 20 62 75 66 66 65 72 20  t in the buffer 
23b19 61 6e 64 20 75 70 64 61 74 65 73 20 2a 70 49 64  and updates *pId
23b1a 78 20 74 6f 20 72 65 66 65 72 0a 2a 2a 20 74 6f  x to refer.** to
23b1b 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20   the first byte 
23b1c 61 66 74 65 72 20 74 68 65 20 6c 61 73 74 20 62  after the last b
23b1d 79 74 65 20 77 72 69 74 74 65 6e 20 62 65 66 6f  yte written befo
23b1e 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a  re returning..**
23b1f 20 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 72 69   .** If the stri
23b20 6e 67 20 7a 53 69 67 6e 65 64 49 64 65 6e 74 20  ng zSignedIdent 
23b21 63 6f 6e 73 69 73 74 73 20 65 6e 74 69 72 65 6c  consists entirel
23b22 79 20 6f 66 20 61 6c 70 68 61 2d 6e 75 6d 65 72  y of alpha-numer
23b23 69 63 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73  ic.** characters
23b24 2c 20 64 6f 65 73 20 6e 6f 74 20 62 65 67 69 6e  , does not begin
23b25 20 77 69 74 68 20 61 20 64 69 67 69 74 20 61 6e   with a digit an
23b26 64 20 69 73 20 6e 6f 74 20 61 6e 20 53 51 4c 20  d is not an SQL 
23b27 6b 65 79 77 6f 72 64 2c 0a 2a 2a 20 74 68 65 6e  keyword,.** then
23b28 20 69 74 20 69 73 20 63 6f 70 69 65 64 20 74 6f   it is copied to
23b29 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66   the output buff
23b2a 65 72 20 65 78 61 63 74 6c 79 20 61 73 20 69 74  er exactly as it
23b2b 20 69 73 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a   is. Otherwise,.
23b2c 2a 2a 20 69 74 20 69 73 20 71 75 6f 74 65 64 20  ** it is quoted 
23b2d 75 73 69 6e 67 20 64 6f 75 62 6c 65 2d 71 75 6f  using double-quo
23b2e 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  tes..*/.static v
23b2f 6f 69 64 20 69 64 65 6e 74 50 75 74 28 63 68 61  oid identPut(cha
23b30 72 20 2a 7a 2c 20 69 6e 74 20 2a 70 49 64 78 2c  r *z, int *pIdx,
23b31 20 63 68 61 72 20 2a 7a 53 69 67 6e 65 64 49 64   char *zSignedId
23b32 65 6e 74 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64  ent){.  unsigned
23b33 20 63 68 61 72 20 2a 7a 49 64 65 6e 74 20 3d 20   char *zIdent = 
23b34 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29  (unsigned char*)
23b35 7a 53 69 67 6e 65 64 49 64 65 6e 74 3b 0a 20 20  zSignedIdent;.  
23b36 69 6e 74 20 69 2c 20 6a 2c 20 6e 65 65 64 51 75  int i, j, needQu
23b37 6f 74 65 3b 0a 20 20 69 20 3d 20 2a 70 49 64 78  ote;.  i = *pIdx
23b38 3b 0a 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 7a 49  ;..  for(j=0; zI
23b39 64 65 6e 74 5b 6a 5d 3b 20 6a 2b 2b 29 7b 0a 20  dent[j]; j++){. 
23b3a 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49     if( !sqlite3I
23b3b 73 61 6c 6e 75 6d 28 7a 49 64 65 6e 74 5b 6a 5d  salnum(zIdent[j]
23b3c 29 20 26 26 20 7a 49 64 65 6e 74 5b 6a 5d 21 3d  ) && zIdent[j]!=
23b3d 27 5f 27 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d  '_' ) break;.  }
23b3e 0a 20 20 6e 65 65 64 51 75 6f 74 65 20 3d 20 73  .  needQuote = s
23b3f 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 7a 49  qlite3Isdigit(zI
23b40 64 65 6e 74 5b 30 5d 29 20 7c 7c 20 73 71 6c 69  dent[0]) || sqli
23b41 74 65 33 4b 65 79 77 6f 72 64 43 6f 64 65 28 7a  te3KeywordCode(z
23b42 49 64 65 6e 74 2c 20 6a 29 21 3d 54 4b 5f 49 44  Ident, j)!=TK_ID
23b43 3b 0a 20 20 69 66 28 20 21 6e 65 65 64 51 75 6f  ;.  if( !needQuo
23b44 74 65 20 29 7b 0a 20 20 20 20 6e 65 65 64 51 75  te ){.    needQu
23b45 6f 74 65 20 3d 20 7a 49 64 65 6e 74 5b 6a 5d 3b  ote = zIdent[j];
23b46 0a 20 20 7d 0a 0a 20 20 69 66 28 20 6e 65 65 64  .  }..  if( need
23b47 51 75 6f 74 65 20 29 20 7a 5b 69 2b 2b 5d 20 3d  Quote ) z[i++] =
23b48 20 27 22 27 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b   '"';.  for(j=0;
23b49 20 7a 49 64 65 6e 74 5b 6a 5d 3b 20 6a 2b 2b 29   zIdent[j]; j++)
23b4a 7b 0a 20 20 20 20 7a 5b 69 2b 2b 5d 20 3d 20 7a  {.    z[i++] = z
23b4b 49 64 65 6e 74 5b 6a 5d 3b 0a 20 20 20 20 69 66  Ident[j];.    if
23b4c 28 20 7a 49 64 65 6e 74 5b 6a 5d 3d 3d 27 22 27  ( zIdent[j]=='"'
23b4d 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20 27 22 27 3b   ) z[i++] = '"';
23b4e 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 65 64 51  .  }.  if( needQ
23b4f 75 6f 74 65 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20  uote ) z[i++] = 
23b50 27 22 27 3b 0a 20 20 7a 5b 69 5d 20 3d 20 30 3b  '"';.  z[i] = 0;
23b51 0a 20 20 2a 70 49 64 78 20 3d 20 69 3b 0a 7d 0a  .  *pIdx = i;.}.
23b52 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
23b53 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73  a CREATE TABLE s
23b54 74 61 74 65 6d 65 6e 74 20 61 70 70 72 6f 70 72  tatement appropr
23b55 69 61 74 65 20 66 6f 72 20 74 68 65 20 67 69 76  iate for the giv
23b56 65 6e 0a 2a 2a 20 74 61 62 6c 65 2e 20 20 4d 65  en.** table.  Me
23b57 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65  mory to hold the
23b58 20 74 65 78 74 20 6f 66 20 74 68 65 20 73 74 61   text of the sta
23b59 74 65 6d 65 6e 74 20 69 73 20 6f 62 74 61 69 6e  tement is obtain
23b5a 65 64 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74  ed.** from sqlit
23b5b 65 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 75  eMalloc() and mu
23b5c 73 74 20 62 65 20 66 72 65 65 64 20 62 79 20 74  st be freed by t
23b5d 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74  he calling funct
23b5e 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  ion..*/.static c
23b5f 68 61 72 20 2a 63 72 65 61 74 65 54 61 62 6c 65  har *createTable
23b60 53 74 6d 74 28 73 71 6c 69 74 65 33 20 2a 64 62  Stmt(sqlite3 *db
23b61 2c 20 54 61 62 6c 65 20 2a 70 29 7b 0a 20 20 69  , Table *p){.  i
23b62 6e 74 20 69 2c 20 6b 2c 20 6e 3b 0a 20 20 63 68  nt i, k, n;.  ch
23b63 61 72 20 2a 7a 53 74 6d 74 3b 0a 20 20 63 68 61  ar *zStmt;.  cha
23b64 72 20 2a 7a 53 65 70 2c 20 2a 7a 53 65 70 32 2c  r *zSep, *zSep2,
23b65 20 2a 7a 45 6e 64 3b 0a 20 20 43 6f 6c 75 6d 6e   *zEnd;.  Column
23b66 20 2a 70 43 6f 6c 3b 0a 20 20 6e 20 3d 20 30 3b   *pCol;.  n = 0;
23b67 0a 20 20 66 6f 72 28 70 43 6f 6c 20 3d 20 70 2d  .  for(pCol = p-
23b68 3e 61 43 6f 6c 2c 20 69 3d 30 3b 20 69 3c 70 2d  >aCol, i=0; i<p-
23b69 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c  >nCol; i++, pCol
23b6a 2b 2b 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 69 64  ++){.    n += id
23b6b 65 6e 74 4c 65 6e 67 74 68 28 70 43 6f 6c 2d 3e  entLength(pCol->
23b6c 7a 4e 61 6d 65 29 20 2b 20 35 3b 0a 20 20 7d 0a  zName) + 5;.  }.
23b6d 20 20 6e 20 2b 3d 20 69 64 65 6e 74 4c 65 6e 67    n += identLeng
23b6e 74 68 28 70 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  th(p->zName);.  
23b6f 69 66 28 20 6e 3c 35 30 20 29 7b 20 0a 20 20 20  if( n<50 ){ .   
23b70 20 7a 53 65 70 20 3d 20 22 22 3b 0a 20 20 20 20   zSep = "";.    
23b71 7a 53 65 70 32 20 3d 20 22 2c 22 3b 0a 20 20 20  zSep2 = ",";.   
23b72 20 7a 45 6e 64 20 3d 20 22 29 22 3b 0a 20 20 7d   zEnd = ")";.  }
23b73 65 6c 73 65 7b 0a 20 20 20 20 7a 53 65 70 20 3d  else{.    zSep =
23b74 20 22 5c 6e 20 20 22 3b 0a 20 20 20 20 7a 53 65   "\n  ";.    zSe
23b75 70 32 20 3d 20 22 2c 5c 6e 20 20 22 3b 0a 20 20  p2 = ",\n  ";.  
23b76 20 20 7a 45 6e 64 20 3d 20 22 5c 6e 29 22 3b 0a    zEnd = "\n)";.
23b77 20 20 7d 0a 20 20 6e 20 2b 3d 20 33 35 20 2b 20    }.  n += 35 + 
23b78 36 2a 70 2d 3e 6e 43 6f 6c 3b 0a 20 20 7a 53 74  6*p->nCol;.  zSt
23b79 6d 74 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  mt = sqlite3Mall
23b7a 6f 63 28 20 6e 20 29 3b 0a 20 20 69 66 28 20 7a  oc( n );.  if( z
23b7b 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 64  Stmt==0 ){.    d
23b7c 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
23b7d 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = 1;.    return 
23b7e 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  0;.  }.  sqlite3
23b7f 5f 73 6e 70 72 69 6e 74 66 28 6e 2c 20 7a 53 74  _snprintf(n, zSt
23b80 6d 74 2c 20 22 43 52 45 41 54 45 20 54 41 42 4c  mt, "CREATE TABL
23b81 45 20 22 29 3b 0a 20 20 6b 20 3d 20 73 71 6c 69  E ");.  k = sqli
23b82 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 53 74 6d  te3Strlen30(zStm
23b83 74 29 3b 0a 20 20 69 64 65 6e 74 50 75 74 28 7a  t);.  identPut(z
23b84 53 74 6d 74 2c 20 26 6b 2c 20 70 2d 3e 7a 4e 61  Stmt, &k, p->zNa
23b85 6d 65 29 3b 0a 20 20 7a 53 74 6d 74 5b 6b 2b 2b  me);.  zStmt[k++
23b86 5d 20 3d 20 27 28 27 3b 0a 20 20 66 6f 72 28 70  ] = '(';.  for(p
23b87 43 6f 6c 3d 70 2d 3e 61 43 6f 6c 2c 20 69 3d 30  Col=p->aCol, i=0
23b88 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b  ; i<p->nCol; i++
23b89 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 73  , pCol++){.    s
23b8a 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
23b8b 20 2a 20 63 6f 6e 73 74 20 61 7a 54 79 70 65 5b   * const azType[
23b8c 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a  ] = {.        /*
23b8d 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54   SQLITE_AFF_TEXT
23b8e 20 20 20 20 2a 2f 20 22 20 54 45 58 54 22 2c 0a      */ " TEXT",.
23b8f 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 49 54          /* SQLIT
23b90 45 5f 41 46 46 5f 4e 4f 4e 45 20 20 20 20 2a 2f  E_AFF_NONE    */
23b91 20 22 22 2c 0a 20 20 20 20 20 20 20 20 2f 2a 20   "",.        /* 
23b92 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52  SQLITE_AFF_NUMER
23b93 49 43 20 2a 2f 20 22 20 4e 55 4d 22 2c 0a 20 20  IC */ " NUM",.  
23b94 20 20 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f        /* SQLITE_
23b95 41 46 46 5f 49 4e 54 45 47 45 52 20 2a 2f 20 22  AFF_INTEGER */ "
23b96 20 49 4e 54 22 2c 0a 20 20 20 20 20 20 20 20 2f   INT",.        /
23b97 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41  * SQLITE_AFF_REA
23b98 4c 20 20 20 20 2a 2f 20 22 20 52 45 41 4c 22 0a  L    */ " REAL".
23b99 20 20 20 20 7d 3b 0a 20 20 20 20 69 6e 74 20 6c      };.    int l
23b9a 65 6e 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  en;.    const ch
23b9b 61 72 20 2a 7a 54 79 70 65 3b 0a 0a 20 20 20 20  ar *zType;..    
23b9c 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
23b9d 28 6e 2d 6b 2c 20 26 7a 53 74 6d 74 5b 6b 5d 2c  (n-k, &zStmt[k],
23b9e 20 7a 53 65 70 29 3b 0a 20 20 20 20 6b 20 2b 3d   zSep);.    k +=
23b9f 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
23ba0 28 26 7a 53 74 6d 74 5b 6b 5d 29 3b 0a 20 20 20  (&zStmt[k]);.   
23ba1 20 7a 53 65 70 20 3d 20 7a 53 65 70 32 3b 0a 20   zSep = zSep2;. 
23ba2 20 20 20 69 64 65 6e 74 50 75 74 28 7a 53 74 6d     identPut(zStm
23ba3 74 2c 20 26 6b 2c 20 70 43 6f 6c 2d 3e 7a 4e 61  t, &k, pCol->zNa
23ba4 6d 65 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  me);.    assert(
23ba5 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 2d   pCol->affinity-
23ba6 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20  SQLITE_AFF_TEXT 
23ba7 3e 3d 20 30 20 29 3b 0a 20 20 20 20 61 73 73 65  >= 0 );.    asse
23ba8 72 74 28 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69  rt( pCol->affini
23ba9 74 79 2d 53 51 4c 49 54 45 5f 41 46 46 5f 54 45  ty-SQLITE_AFF_TE
23baa 58 54 20 3c 20 73 69 7a 65 6f 66 28 61 7a 54 79  XT < sizeof(azTy
23bab 70 65 29 2f 73 69 7a 65 6f 66 28 61 7a 54 79 70  pe)/sizeof(azTyp
23bac 65 5b 30 5d 29 20 29 3b 0a 20 20 20 20 74 65 73  e[0]) );.    tes
23bad 74 63 61 73 65 28 20 70 43 6f 6c 2d 3e 61 66 66  tcase( pCol->aff
23bae 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46  inity==SQLITE_AF
23baf 46 5f 54 45 58 54 20 29 3b 0a 20 20 20 20 74 65  F_TEXT );.    te
23bb0 73 74 63 61 73 65 28 20 70 43 6f 6c 2d 3e 61 66  stcase( pCol->af
23bb1 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41  finity==SQLITE_A
23bb2 46 46 5f 4e 4f 4e 45 20 29 3b 0a 20 20 20 20 74  FF_NONE );.    t
23bb3 65 73 74 63 61 73 65 28 20 70 43 6f 6c 2d 3e 61  estcase( pCol->a
23bb4 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f  ffinity==SQLITE_
23bb5 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a 20  AFF_NUMERIC );. 
23bb6 20 20 20 74 65 73 74 63 61 73 65 28 20 70 43 6f     testcase( pCo
23bb7 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c  l->affinity==SQL
23bb8 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20  ITE_AFF_INTEGER 
23bb9 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
23bba 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 3d   pCol->affinity=
23bbb 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c  =SQLITE_AFF_REAL
23bbc 20 29 3b 0a 20 20 20 20 0a 20 20 20 20 7a 54 79   );.    .    zTy
23bbd 70 65 20 3d 20 61 7a 54 79 70 65 5b 70 43 6f 6c  pe = azType[pCol
23bbe 2d 3e 61 66 66 69 6e 69 74 79 20 2d 20 53 51 4c  ->affinity - SQL
23bbf 49 54 45 5f 41 46 46 5f 54 45 58 54 5d 3b 0a 20  ITE_AFF_TEXT];. 
23bc0 20 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33     len = sqlite3
23bc1 53 74 72 6c 65 6e 33 30 28 7a 54 79 70 65 29 3b  Strlen30(zType);
23bc2 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 6f  .    assert( pCo
23bc3 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c  l->affinity==SQL
23bc4 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 0a 20 20  ITE_AFF_NONE .  
23bc5 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 43 6f            || pCo
23bc6 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 73 71 6c  l->affinity==sql
23bc7 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 70 65  ite3AffinityType
23bc8 28 7a 54 79 70 65 29 20 29 3b 0a 20 20 20 20 6d  (zType) );.    m
23bc9 65 6d 63 70 79 28 26 7a 53 74 6d 74 5b 6b 5d 2c  emcpy(&zStmt[k],
23bca 20 7a 54 79 70 65 2c 20 6c 65 6e 29 3b 0a 20 20   zType, len);.  
23bcb 20 20 6b 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20    k += len;.    
23bcc 61 73 73 65 72 74 28 20 6b 3c 3d 6e 20 29 3b 0a  assert( k<=n );.
23bcd 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e    }.  sqlite3_sn
23bce 70 72 69 6e 74 66 28 6e 2d 6b 2c 20 26 7a 53 74  printf(n-k, &zSt
23bcf 6d 74 5b 6b 5d 2c 20 22 25 73 22 2c 20 7a 45 6e  mt[k], "%s", zEn
23bd0 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 53 74  d);.  return zSt
23bd1 6d 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  mt;.}../*.** Thi
23bd2 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
23bd3 6c 65 64 20 74 6f 20 72 65 70 6f 72 74 20 74 68  led to report th
23bd4 65 20 66 69 6e 61 6c 20 22 29 22 20 74 68 61 74  e final ")" that
23bd5 20 74 65 72 6d 69 6e 61 74 65 73 0a 2a 2a 20 61   terminates.** a
23bd6 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74   CREATE TABLE st
23bd7 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54  atement..**.** T
23bd8 68 65 20 74 61 62 6c 65 20 73 74 72 75 63 74 75  he table structu
23bd9 72 65 20 74 68 61 74 20 6f 74 68 65 72 20 61 63  re that other ac
23bda 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 68 61  tion routines ha
23bdb 76 65 20 62 65 65 6e 20 62 75 69 6c 64 69 6e 67  ve been building
23bdc 0a 2a 2a 20 69 73 20 61 64 64 65 64 20 74 6f 20  .** is added to 
23bdd 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 68 61 73  the internal has
23bde 68 20 74 61 62 6c 65 73 2c 20 61 73 73 75 6d 69  h tables, assumi
23bdf 6e 67 20 6e 6f 20 65 72 72 6f 72 73 20 68 61 76  ng no errors hav
23be0 65 0a 2a 2a 20 6f 63 63 75 72 72 65 64 2e 0a 2a  e.** occurred..*
23be1 2a 0a 2a 2a 20 41 6e 20 65 6e 74 72 79 20 66 6f  *.** An entry fo
23be2 72 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 6d  r the table is m
23be3 61 64 65 20 69 6e 20 74 68 65 20 6d 61 73 74 65  ade in the maste
23be4 72 20 74 61 62 6c 65 20 6f 6e 20 64 69 73 6b 2c  r table on disk,
23be5 20 75 6e 6c 65 73 73 0a 2a 2a 20 74 68 69 73 20   unless.** this 
23be6 69 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20 74  is a temporary t
23be7 61 62 6c 65 20 6f 72 20 64 62 2d 3e 69 6e 69 74  able or db->init
23be8 2e 62 75 73 79 3d 3d 31 2e 20 20 57 68 65 6e 20  .busy==1.  When 
23be9 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 31  db->init.busy==1
23bea 0a 2a 2a 20 69 74 20 6d 65 61 6e 73 20 77 65 20  .** it means we 
23beb 61 72 65 20 72 65 61 64 69 6e 67 20 74 68 65 20  are reading the 
23bec 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61  sqlite_master ta
23bed 62 6c 65 20 62 65 63 61 75 73 65 20 77 65 20 6a  ble because we j
23bee 75 73 74 0a 2a 2a 20 63 6f 6e 6e 65 63 74 65 64  ust.** connected
23bef 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
23bf0 20 6f 72 20 62 65 63 61 75 73 65 20 74 68 65 20   or because the 
23bf1 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61  sqlite_master ta
23bf2 62 6c 65 20 68 61 73 0a 2a 2a 20 72 65 63 65 6e  ble has.** recen
23bf3 74 6c 79 20 63 68 61 6e 67 65 64 2c 20 73 6f 20  tly changed, so 
23bf4 74 68 65 20 65 6e 74 72 79 20 66 6f 72 20 74 68  the entry for th
23bf5 69 73 20 74 61 62 6c 65 20 61 6c 72 65 61 64 79  is table already
23bf6 20 65 78 69 73 74 73 20 69 6e 0a 2a 2a 20 74 68   exists in.** th
23bf7 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  e sqlite_master 
23bf8 74 61 62 6c 65 2e 20 20 57 65 20 64 6f 20 6e 6f  table.  We do no
23bf9 74 20 77 61 6e 74 20 74 6f 20 63 72 65 61 74 65  t want to create
23bfa 20 69 74 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a   it again..**.**
23bfb 20 49 66 20 74 68 65 20 70 53 65 6c 65 63 74 20   If the pSelect 
23bfc 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 74 20  argument is not 
23bfd 4e 55 4c 4c 2c 20 69 74 20 6d 65 61 6e 73 20 74  NULL, it means t
23bfe 68 61 74 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hat this routine
23bff 0a 2a 2a 20 77 61 73 20 63 61 6c 6c 65 64 20 74  .** was called t
23c00 6f 20 63 72 65 61 74 65 20 61 20 74 61 62 6c 65  o create a table
23c01 20 67 65 6e 65 72 61 74 65 64 20 66 72 6f 6d 20   generated from 
23c02 61 20 0a 2a 2a 20 22 43 52 45 41 54 45 20 54 41  a .** "CREATE TA
23c03 42 4c 45 20 2e 2e 2e 20 41 53 20 53 45 4c 45 43  BLE ... AS SELEC
23c04 54 20 2e 2e 2e 22 20 73 74 61 74 65 6d 65 6e 74  T ..." statement
23c05 2e 20 20 54 68 65 20 63 6f 6c 75 6d 6e 20 6e 61  .  The column na
23c06 6d 65 73 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65  mes of.** the ne
23c07 77 20 74 61 62 6c 65 20 77 69 6c 6c 20 6d 61 74  w table will mat
23c08 63 68 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  ch the result se
23c09 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 2e  t of the SELECT.
23c0a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
23c0b 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
23c0c 6e 64 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65  ndTable(.  Parse
23c0d 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
23c0e 20 20 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74     /* Parse cont
23c0f 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  ext */.  Token *
23c10 70 43 6f 6e 73 2c 20 20 20 20 20 20 20 20 20 20  pCons,          
23c11 20 2f 2a 20 54 68 65 20 27 2c 27 20 74 6f 6b 65   /* The ',' toke
23c12 6e 20 61 66 74 65 72 20 74 68 65 20 6c 61 73 74  n after the last
23c13 20 63 6f 6c 75 6d 6e 20 64 65 66 6e 2e 20 2a 2f   column defn. */
23c14 0a 20 20 54 6f 6b 65 6e 20 2a 70 45 6e 64 2c 20  .  Token *pEnd, 
23c15 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
23c16 65 20 66 69 6e 61 6c 20 27 29 27 20 74 6f 6b 65  e final ')' toke
23c17 6e 20 69 6e 20 74 68 65 20 43 52 45 41 54 45 20  n in the CREATE 
23c18 54 41 42 4c 45 20 2a 2f 0a 20 20 53 65 6c 65 63  TABLE */.  Selec
23c19 74 20 2a 70 53 65 6c 65 63 74 20 20 20 20 20 20  t *pSelect      
23c1a 20 20 20 2f 2a 20 53 65 6c 65 63 74 20 66 72 6f     /* Select fro
23c1b 6d 20 61 20 22 43 52 45 41 54 45 20 2e 2e 2e 20  m a "CREATE ... 
23c1c 41 53 20 53 45 4c 45 43 54 22 20 2a 2f 0a 29 7b  AS SELECT" */.){
23c1d 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 73  .  Table *p;.  s
23c1e 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
23c1f 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69  rse->db;.  int i
23c20 44 62 3b 0a 0a 20 20 69 66 28 20 28 70 45 6e 64  Db;..  if( (pEnd
23c21 3d 3d 30 20 26 26 20 70 53 65 6c 65 63 74 3d 3d  ==0 && pSelect==
23c22 30 29 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  0) || db->malloc
23c23 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65  Failed ){.    re
23c24 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 20 3d 20  turn;.  }.  p = 
23c25 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
23c26 65 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20  e;.  if( p==0 ) 
23c27 72 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72  return;..  asser
23c28 74 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73  t( !db->init.bus
23c29 79 20 7c 7c 20 21 70 53 65 6c 65 63 74 20 29 3b  y || !pSelect );
23c2a 0a 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65  ..  iDb = sqlite
23c2b 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64  3SchemaToIndex(d
23c2c 62 2c 20 70 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  b, p->pSchema);.
23c2d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
23c2e 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 2f 2a 20  OMIT_CHECK.  /* 
23c2f 52 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e  Resolve names in
23c30 20 61 6c 6c 20 43 48 45 43 4b 20 63 6f 6e 73 74   all CHECK const
23c31 72 61 69 6e 74 20 65 78 70 72 65 73 73 69 6f 6e  raint expression
23c32 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d  s..  */.  if( p-
23c33 3e 70 43 68 65 63 6b 20 29 7b 0a 20 20 20 20 53  >pCheck ){.    S
23c34 72 63 4c 69 73 74 20 73 53 72 63 3b 20 20 20 20  rcList sSrc;    
23c35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
23c36 2a 20 46 61 6b 65 20 53 72 63 4c 69 73 74 20 66  * Fake SrcList f
23c37 6f 72 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  or pParse->pNewT
23c38 61 62 6c 65 20 2a 2f 0a 20 20 20 20 4e 61 6d 65  able */.    Name
23c39 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20 20 20  Context sNC;    
23c3a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
23c3b 61 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20  ame context for 
23c3c 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
23c3d 65 20 2a 2f 0a 0a 20 20 20 20 6d 65 6d 73 65 74  e */..    memset
23c3e 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66  (&sNC, 0, sizeof
23c3f 28 73 4e 43 29 29 3b 0a 20 20 20 20 6d 65 6d 73  (sNC));.    mems
23c40 65 74 28 26 73 53 72 63 2c 20 30 2c 20 73 69 7a  et(&sSrc, 0, siz
23c41 65 6f 66 28 73 53 72 63 29 29 3b 0a 20 20 20 20  eof(sSrc));.    
23c42 73 53 72 63 2e 6e 53 72 63 20 3d 20 31 3b 0a 20  sSrc.nSrc = 1;. 
23c43 20 20 20 73 53 72 63 2e 61 5b 30 5d 2e 7a 4e 61     sSrc.a[0].zNa
23c44 6d 65 20 3d 20 70 2d 3e 7a 4e 61 6d 65 3b 0a 20  me = p->zName;. 
23c45 20 20 20 73 53 72 63 2e 61 5b 30 5d 2e 70 54 61     sSrc.a[0].pTa
23c46 62 20 3d 20 70 3b 0a 20 20 20 20 73 53 72 63 2e  b = p;.    sSrc.
23c47 61 5b 30 5d 2e 69 43 75 72 73 6f 72 20 3d 20 2d  a[0].iCursor = -
23c48 31 3b 0a 20 20 20 20 73 4e 43 2e 70 50 61 72 73  1;.    sNC.pPars
23c49 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 20 20  e = pParse;.    
23c4a 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 26  sNC.pSrcList = &
23c4b 73 53 72 63 3b 0a 20 20 20 20 73 4e 43 2e 69 73  sSrc;.    sNC.is
23c4c 43 68 65 63 6b 20 3d 20 31 3b 0a 20 20 20 20 69  Check = 1;.    i
23c4d 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76  f( sqlite3Resolv
23c4e 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c  eExprNames(&sNC,
23c4f 20 70 2d 3e 70 43 68 65 63 6b 29 20 29 7b 0a 20   p->pCheck) ){. 
23c50 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
23c51 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a   }.  }.#endif /*
23c52 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
23c53 5f 4f 4d 49 54 5f 43 48 45 43 4b 29 20 2a 2f 0a  _OMIT_CHECK) */.
23c54 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 62 2d  .  /* If the db-
23c55 3e 69 6e 69 74 2e 62 75 73 79 20 69 73 20 31 20  >init.busy is 1 
23c56 69 74 20 6d 65 61 6e 73 20 77 65 20 61 72 65 20  it means we are 
23c57 72 65 61 64 69 6e 67 20 74 68 65 20 53 51 4c 20  reading the SQL 
23c58 6f 66 66 20 74 68 65 0a 20 20 2a 2a 20 22 73 71  off the.  ** "sq
23c59 6c 69 74 65 5f 6d 61 73 74 65 72 22 20 6f 72 20  lite_master" or 
23c5a 22 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73  "sqlite_temp_mas
23c5b 74 65 72 22 20 74 61 62 6c 65 20 6f 6e 20 74 68  ter" table on th
23c5c 65 20 64 69 73 6b 2e 0a 20 20 2a 2a 20 53 6f 20  e disk..  ** So 
23c5d 64 6f 20 6e 6f 74 20 77 72 69 74 65 20 74 6f 20  do not write to 
23c5e 74 68 65 20 64 69 73 6b 20 61 67 61 69 6e 2e 20  the disk again. 
23c5f 20 45 78 74 72 61 63 74 20 74 68 65 20 72 6f 6f   Extract the roo
23c60 74 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 20  t page number.  
23c61 2a 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65  ** for the table
23c62 20 66 72 6f 6d 20 74 68 65 20 64 62 2d 3e 69 6e   from the db->in
23c63 69 74 2e 6e 65 77 54 6e 75 6d 20 66 69 65 6c 64  it.newTnum field
23c64 2e 20 20 28 54 68 65 20 70 61 67 65 20 6e 75 6d  .  (The page num
23c65 62 65 72 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20  ber.  ** should 
23c66 68 61 76 65 20 62 65 65 6e 20 70 75 74 20 74 68  have been put th
23c67 65 72 65 20 62 79 20 74 68 65 20 73 71 6c 69 74  ere by the sqlit
23c68 65 4f 70 65 6e 43 62 20 72 6f 75 74 69 6e 65 2e  eOpenCb routine.
23c69 29 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d  ).  */.  if( db-
23c6a 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20  >init.busy ){.  
23c6b 20 20 70 2d 3e 74 6e 75 6d 20 3d 20 64 62 2d 3e    p->tnum = db->
23c6c 69 6e 69 74 2e 6e 65 77 54 6e 75 6d 3b 0a 20 20  init.newTnum;.  
23c6d 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 74 20 69  }..  /* If not i
23c6e 6e 69 74 69 61 6c 69 7a 69 6e 67 2c 20 74 68 65  nitializing, the
23c6f 6e 20 63 72 65 61 74 65 20 61 20 72 65 63 6f 72  n create a recor
23c70 64 20 66 6f 72 20 74 68 65 20 6e 65 77 20 74 61  d for the new ta
23c71 62 6c 65 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20  ble.  ** in the 
23c72 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61  SQLITE_MASTER ta
23c73 62 6c 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ble of the datab
23c74 61 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49  ase..  **.  ** I
23c75 66 20 74 68 69 73 20 69 73 20 61 20 54 45 4d 50  f this is a TEMP
23c76 4f 52 41 52 59 20 74 61 62 6c 65 2c 20 77 72 69  ORARY table, wri
23c77 74 65 20 74 68 65 20 65 6e 74 72 79 20 69 6e 74  te the entry int
23c78 6f 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 0a  o the auxiliary.
23c79 20 20 2a 2a 20 66 69 6c 65 20 69 6e 73 74 65 61    ** file instea
23c7a 64 20 6f 66 20 69 6e 74 6f 20 74 68 65 20 6d 61  d of into the ma
23c7b 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  in database file
23c7c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 64 62  ..  */.  if( !db
23c7d 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20  ->init.busy ){. 
23c7e 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 56 64     int n;.    Vd
23c7f 62 65 20 2a 76 3b 0a 20 20 20 20 63 68 61 72 20  be *v;.    char 
23c80 2a 7a 54 79 70 65 3b 20 20 20 20 2f 2a 20 22 76  *zType;    /* "v
23c81 69 65 77 22 20 6f 72 20 22 74 61 62 6c 65 22 20  iew" or "table" 
23c82 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 79  */.    char *zTy
23c83 70 65 32 3b 20 20 20 2f 2a 20 22 56 49 45 57 22  pe2;   /* "VIEW"
23c84 20 6f 72 20 22 54 41 42 4c 45 22 20 2a 2f 0a 20   or "TABLE" */. 
23c85 20 20 20 63 68 61 72 20 2a 7a 53 74 6d 74 3b 20     char *zStmt; 
23c86 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68     /* Text of th
23c87 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 6f  e CREATE TABLE o
23c88 72 20 43 52 45 41 54 45 20 56 49 45 57 20 73 74  r CREATE VIEW st
23c89 61 74 65 6d 65 6e 74 20 2a 2f 0a 0a 20 20 20 20  atement */..    
23c8a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
23c8b 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  be(pParse);.    
23c8c 69 66 28 20 4e 45 56 45 52 28 76 3d 3d 30 29 20  if( NEVER(v==0) 
23c8d 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 20 20 73  ) return;..    s
23c8e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
23c8f 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 30 29  (v, OP_Close, 0)
23c90 3b 0a 0a 20 20 20 20 2f 2a 20 0a 20 20 20 20 2a  ;..    /* .    *
23c91 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 7a 54 79  * Initialize zTy
23c92 70 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20 76  pe for the new v
23c93 69 65 77 20 6f 72 20 74 61 62 6c 65 2e 0a 20 20  iew or table..  
23c94 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e    */.    if( p->
23c95 70 53 65 6c 65 63 74 3d 3d 30 20 29 7b 0a 20 20  pSelect==0 ){.  
23c96 20 20 20 20 2f 2a 20 41 20 72 65 67 75 6c 61 72      /* A regular
23c97 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20   table */.      
23c98 7a 54 79 70 65 20 3d 20 22 74 61 62 6c 65 22 3b  zType = "table";
23c99 0a 20 20 20 20 20 20 7a 54 79 70 65 32 20 3d 20  .      zType2 = 
23c9a 22 54 41 42 4c 45 22 3b 0a 23 69 66 6e 64 65 66  "TABLE";.#ifndef
23c9b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45   SQLITE_OMIT_VIE
23c9c 57 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  W.    }else{.   
23c9d 20 20 20 2f 2a 20 41 20 76 69 65 77 20 2a 2f 0a     /* A view */.
23c9e 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 22 76        zType = "v
23c9f 69 65 77 22 3b 0a 20 20 20 20 20 20 7a 54 79 70  iew";.      zTyp
23ca0 65 32 20 3d 20 22 56 49 45 57 22 3b 0a 23 65 6e  e2 = "VIEW";.#en
23ca1 64 69 66 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  dif.    }..    /
23ca2 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 43  * If this is a C
23ca3 52 45 41 54 45 20 54 41 42 4c 45 20 78 78 20 41  REATE TABLE xx A
23ca4 53 20 53 45 4c 45 43 54 20 2e 2e 2e 2c 20 65 78  S SELECT ..., ex
23ca5 65 63 75 74 65 20 74 68 65 20 53 45 4c 45 43 54  ecute the SELECT
23ca6 0a 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e  .    ** statemen
23ca7 74 20 74 6f 20 70 6f 70 75 6c 61 74 65 20 74 68  t to populate th
23ca8 65 20 6e 65 77 20 74 61 62 6c 65 2e 20 54 68 65  e new table. The
23ca9 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65   root-page numbe
23caa 72 20 66 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a  r for the.    **
23cab 20 6e 65 77 20 74 61 62 6c 65 20 69 73 20 69 6e   new table is in
23cac 20 72 65 67 69 73 74 65 72 20 70 50 61 72 73 65   register pParse
23cad 2d 3e 72 65 67 52 6f 6f 74 2e 0a 20 20 20 20 2a  ->regRoot..    *
23cae 2a 0a 20 20 20 20 2a 2a 20 4f 6e 63 65 20 74 68  *.    ** Once th
23caf 65 20 53 45 4c 45 43 54 20 68 61 73 20 62 65 65  e SELECT has bee
23cb0 6e 20 63 6f 64 65 64 20 62 79 20 73 71 6c 69 74  n coded by sqlit
23cb1 65 33 53 65 6c 65 63 74 28 29 2c 20 69 74 20 69  e3Select(), it i
23cb2 73 20 69 6e 20 61 0a 20 20 20 20 2a 2a 20 73 75  s in a.    ** su
23cb3 69 74 61 62 6c 65 20 73 74 61 74 65 20 74 6f 20  itable state to 
23cb4 71 75 65 72 79 20 66 6f 72 20 74 68 65 20 63 6f  query for the co
23cb5 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 6e 64 20 74  lumn names and t
23cb6 79 70 65 73 20 74 6f 20 62 65 20 75 73 65 64 0a  ypes to be used.
23cb7 20 20 20 20 2a 2a 20 62 79 20 74 68 65 20 6e 65      ** by the ne
23cb8 77 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2a 0a  w table..    **.
23cb9 20 20 20 20 2a 2a 20 41 20 73 68 61 72 65 64 2d      ** A shared-
23cba 63 61 63 68 65 20 77 72 69 74 65 2d 6c 6f 63 6b  cache write-lock
23cbb 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64   is not required
23cbc 20 74 6f 20 77 72 69 74 65 20 74 6f 20 74 68 65   to write to the
23cbd 20 6e 65 77 20 74 61 62 6c 65 2c 0a 20 20 20 20   new table,.    
23cbe 2a 2a 20 61 73 20 61 20 73 63 68 65 6d 61 2d 6c  ** as a schema-l
23cbf 6f 63 6b 20 6d 75 73 74 20 68 61 76 65 20 61 6c  ock must have al
23cc0 72 65 61 64 79 20 62 65 65 6e 20 6f 62 74 61 69  ready been obtai
23cc1 6e 65 64 20 74 6f 20 63 72 65 61 74 65 20 69 74  ned to create it
23cc2 2e 20 53 69 6e 63 65 0a 20 20 20 20 2a 2a 20 61  . Since.    ** a
23cc3 20 73 63 68 65 6d 61 2d 6c 6f 63 6b 20 65 78 63   schema-lock exc
23cc4 6c 75 64 65 73 20 61 6c 6c 20 6f 74 68 65 72 20  ludes all other 
23cc5 64 61 74 61 62 61 73 65 20 75 73 65 72 73 2c 20  database users, 
23cc6 74 68 65 20 77 72 69 74 65 2d 6c 6f 63 6b 20 77  the write-lock w
23cc7 6f 75 6c 64 0a 20 20 20 20 2a 2a 20 62 65 20 72  ould.    ** be r
23cc8 65 64 75 6e 64 61 6e 74 2e 0a 20 20 20 20 2a 2f  edundant..    */
23cc9 0a 20 20 20 20 69 66 28 20 70 53 65 6c 65 63 74  .    if( pSelect
23cca 20 29 7b 0a 20 20 20 20 20 20 53 65 6c 65 63 74   ){.      Select
23ccb 44 65 73 74 20 64 65 73 74 3b 0a 20 20 20 20 20  Dest dest;.     
23ccc 20 54 61 62 6c 65 20 2a 70 53 65 6c 54 61 62 3b   Table *pSelTab;
23ccd 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 70  ..      assert(p
23cce 50 61 72 73 65 2d 3e 6e 54 61 62 3d 3d 31 29 3b  Parse->nTab==1);
23ccf 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
23cd0 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f  beAddOp3(v, OP_O
23cd1 70 65 6e 57 72 69 74 65 2c 20 31 2c 20 70 50 61  penWrite, 1, pPa
23cd2 72 73 65 2d 3e 72 65 67 52 6f 6f 74 2c 20 69 44  rse->regRoot, iD
23cd3 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  b);.      sqlite
23cd4 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
23cd5 20 31 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73   1);.      pPars
23cd6 65 2d 3e 6e 54 61 62 20 3d 20 32 3b 0a 20 20 20  e->nTab = 2;.   
23cd7 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74     sqlite3Select
23cd8 44 65 73 74 49 6e 69 74 28 26 64 65 73 74 2c 20  DestInit(&dest, 
23cd9 53 52 54 5f 54 61 62 6c 65 2c 20 31 29 3b 0a 20  SRT_Table, 1);. 
23cda 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65       sqlite3Sele
23cdb 63 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65  ct(pParse, pSele
23cdc 63 74 2c 20 26 64 65 73 74 29 3b 0a 20 20 20 20  ct, &dest);.    
23cdd 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
23cde 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  Op1(v, OP_Close,
23cdf 20 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70   1);.      if( p
23ce0 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 29  Parse->nErr==0 )
23ce1 7b 0a 20 20 20 20 20 20 20 20 70 53 65 6c 54 61  {.        pSelTa
23ce2 62 20 3d 20 73 71 6c 69 74 65 33 52 65 73 75 6c  b = sqlite3Resul
23ce3 74 53 65 74 4f 66 53 65 6c 65 63 74 28 70 50 61  tSetOfSelect(pPa
23ce4 72 73 65 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20  rse, pSelect);. 
23ce5 20 20 20 20 20 20 20 69 66 28 20 70 53 65 6c 54         if( pSelT
23ce6 61 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  ab==0 ) return;.
23ce7 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
23ce8 70 2d 3e 61 43 6f 6c 3d 3d 30 20 29 3b 0a 20 20  p->aCol==0 );.  
23ce9 20 20 20 20 20 20 70 2d 3e 6e 43 6f 6c 20 3d 20        p->nCol = 
23cea 70 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20  pSelTab->nCol;. 
23ceb 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6c 20 3d         p->aCol =
23cec 20 70 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 3b 0a   pSelTab->aCol;.
23ced 20 20 20 20 20 20 20 20 70 53 65 6c 54 61 62 2d          pSelTab-
23cee 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20  >nCol = 0;.     
23cef 20 20 20 70 53 65 6c 54 61 62 2d 3e 61 43 6f 6c     pSelTab->aCol
23cf0 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 73 71   = 0;.        sq
23cf1 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65  lite3DeleteTable
23cf2 28 70 53 65 6c 54 61 62 29 3b 0a 20 20 20 20 20  (pSelTab);.     
23cf3 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a   }.    }..    /*
23cf4 20 43 6f 6d 70 75 74 65 20 74 68 65 20 63 6f 6d   Compute the com
23cf5 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74 68  plete text of th
23cf6 65 20 43 52 45 41 54 45 20 73 74 61 74 65 6d 65  e CREATE stateme
23cf7 6e 74 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 53  nt */.    if( pS
23cf8 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 7a  elect ){.      z
23cf9 53 74 6d 74 20 3d 20 63 72 65 61 74 65 54 61 62  Stmt = createTab
23cfa 6c 65 53 74 6d 74 28 64 62 2c 20 70 29 3b 0a 20  leStmt(db, p);. 
23cfb 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
23cfc 6e 20 3d 20 28 69 6e 74 29 28 70 45 6e 64 2d 3e  n = (int)(pEnd->
23cfd 7a 20 2d 20 70 50 61 72 73 65 2d 3e 73 4e 61 6d  z - pParse->sNam
23cfe 65 54 6f 6b 65 6e 2e 7a 29 20 2b 20 31 3b 0a 20  eToken.z) + 1;. 
23cff 20 20 20 20 20 7a 53 74 6d 74 20 3d 20 73 71 6c       zStmt = sql
23d00 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
23d01 0a 20 20 20 20 20 20 20 20 20 20 22 43 52 45 41  .          "CREA
23d02 54 45 20 25 73 20 25 2e 2a 73 22 2c 20 7a 54 79  TE %s %.*s", zTy
23d03 70 65 32 2c 20 6e 2c 20 70 50 61 72 73 65 2d 3e  pe2, n, pParse->
23d04 73 4e 61 6d 65 54 6f 6b 65 6e 2e 7a 0a 20 20 20  sNameToken.z.   
23d05 20 20 20 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20     );.    }..   
23d06 20 2f 2a 20 41 20 73 6c 6f 74 20 66 6f 72 20 74   /* A slot for t
23d07 68 65 20 72 65 63 6f 72 64 20 68 61 73 20 61 6c  he record has al
23d08 72 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63  ready been alloc
23d09 61 74 65 64 20 69 6e 20 74 68 65 20 0a 20 20 20  ated in the .   
23d0a 20 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 53 54 45   ** SQLITE_MASTE
23d0b 52 20 74 61 62 6c 65 2e 20 20 57 65 20 6a 75 73  R table.  We jus
23d0c 74 20 6e 65 65 64 20 74 6f 20 75 70 64 61 74 65  t need to update
23d0d 20 74 68 61 74 20 73 6c 6f 74 20 77 69 74 68 20   that slot with 
23d0e 61 6c 6c 0a 20 20 20 20 2a 2a 20 74 68 65 20 69  all.    ** the i
23d0f 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 65 27 76 65  nformation we've
23d10 20 63 6f 6c 6c 65 63 74 65 64 2e 0a 20 20 20 20   collected..    
23d11 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65  */.    sqlite3Ne
23d12 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65  stedParse(pParse
23d13 2c 0a 20 20 20 20 20 20 22 55 50 44 41 54 45 20  ,.      "UPDATE 
23d14 25 51 2e 25 73 20 22 0a 20 20 20 20 20 20 20 20  %Q.%s ".        
23d15 20 22 53 45 54 20 74 79 70 65 3d 27 25 73 27 2c   "SET type='%s',
23d16 20 6e 61 6d 65 3d 25 51 2c 20 74 62 6c 5f 6e 61   name=%Q, tbl_na
23d17 6d 65 3d 25 51 2c 20 72 6f 6f 74 70 61 67 65 3d  me=%Q, rootpage=
23d18 23 25 64 2c 20 73 71 6c 3d 25 51 20 22 0a 20 20  #%d, sql=%Q ".  
23d19 20 20 20 20 20 22 57 48 45 52 45 20 72 6f 77 69       "WHERE rowi
23d1a 64 3d 23 25 64 22 2c 0a 20 20 20 20 20 20 64 62  d=#%d",.      db
23d1b 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
23d1c 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69  , SCHEMA_TABLE(i
23d1d 44 62 29 2c 0a 20 20 20 20 20 20 7a 54 79 70 65  Db),.      zType
23d1e 2c 0a 20 20 20 20 20 20 70 2d 3e 7a 4e 61 6d 65  ,.      p->zName
23d1f 2c 0a 20 20 20 20 20 20 70 2d 3e 7a 4e 61 6d 65  ,.      p->zName
23d20 2c 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  ,.      pParse->
23d21 72 65 67 52 6f 6f 74 2c 0a 20 20 20 20 20 20 7a  regRoot,.      z
23d22 53 74 6d 74 2c 0a 20 20 20 20 20 20 70 50 61 72  Stmt,.      pPar
23d23 73 65 2d 3e 72 65 67 52 6f 77 69 64 0a 20 20 20  se->regRowid.   
23d24 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44   );.    sqlite3D
23d25 62 46 72 65 65 28 64 62 2c 20 7a 53 74 6d 74 29  bFree(db, zStmt)
23d26 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68 61  ;.    sqlite3Cha
23d27 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65  ngeCookie(pParse
23d28 2c 20 69 44 62 29 3b 0a 0a 23 69 66 6e 64 65 66  , iDb);..#ifndef
23d29 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
23d2a 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 20 20 2f  OINCREMENT.    /
23d2b 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69  * Check to see i
23d2c 66 20 77 65 20 6e 65 65 64 20 74 6f 20 63 72 65  f we need to cre
23d2d 61 74 65 20 61 6e 20 73 71 6c 69 74 65 5f 73 65  ate an sqlite_se
23d2e 71 75 65 6e 63 65 20 74 61 62 6c 65 20 66 6f 72  quence table for
23d2f 0a 20 20 20 20 2a 2a 20 6b 65 65 70 69 6e 67 20  .    ** keeping 
23d30 74 72 61 63 6b 20 6f 66 20 61 75 74 6f 69 6e 63  track of autoinc
23d31 72 65 6d 65 6e 74 20 6b 65 79 73 2e 0a 20 20 20  rement keys..   
23d32 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 74   */.    if( p->t
23d33 61 62 46 6c 61 67 73 20 26 20 54 46 5f 41 75 74  abFlags & TF_Aut
23d34 6f 69 6e 63 72 65 6d 65 6e 74 20 29 7b 0a 20 20  oincrement ){.  
23d35 20 20 20 20 44 62 20 2a 70 44 62 20 3d 20 26 64      Db *pDb = &d
23d36 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 20  b->aDb[iDb];.   
23d37 20 20 20 69 66 28 20 70 44 62 2d 3e 70 53 63 68     if( pDb->pSch
23d38 65 6d 61 2d 3e 70 53 65 71 54 61 62 3d 3d 30 20  ema->pSeqTab==0 
23d39 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
23d3a 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50  e3NestedParse(pP
23d3b 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 20 20  arse,.          
23d3c 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 25 51  "CREATE TABLE %Q
23d3d 2e 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65  .sqlite_sequence
23d3e 28 6e 61 6d 65 2c 73 65 71 29 22 2c 0a 20 20 20  (name,seq)",.   
23d3f 20 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d         pDb->zNam
23d40 65 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20  e.        );.   
23d41 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69     }.    }.#endi
23d42 66 0a 0a 20 20 20 20 2f 2a 20 52 65 70 61 72 73  f..    /* Repars
23d43 65 20 65 76 65 72 79 74 68 69 6e 67 20 74 6f 20  e everything to 
23d44 75 70 64 61 74 65 20 6f 75 72 20 69 6e 74 65 72  update our inter
23d45 6e 61 6c 20 64 61 74 61 20 73 74 72 75 63 74 75  nal data structu
23d46 72 65 73 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  res */.    sqlit
23d47 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
23d48 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61 2c 20  OP_ParseSchema, 
23d49 69 44 62 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20  iDb, 0, 0,.     
23d4a 20 20 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74     sqlite3MPrint
23d4b 66 28 64 62 2c 20 22 74 62 6c 5f 6e 61 6d 65 3d  f(db, "tbl_name=
23d4c 27 25 71 27 22 2c 70 2d 3e 7a 4e 61 6d 65 29 2c  '%q'",p->zName),
23d4d 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20   P4_DYNAMIC);.  
23d4e 7d 0a 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68 65  }...  /* Add the
23d4f 20 74 61 62 6c 65 20 74 6f 20 74 68 65 20 69 6e   table to the in
23d50 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65 73 65 6e  -memory represen
23d51 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 61  tation of the da
23d52 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 69  tabase..  */.  i
23d53 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  f( db->init.busy
23d54 20 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70   ){.    Table *p
23d55 4f 6c 64 3b 0a 20 20 20 20 53 63 68 65 6d 61 20  Old;.    Schema 
23d56 2a 70 53 63 68 65 6d 61 20 3d 20 70 2d 3e 70 53  *pSchema = p->pS
23d57 63 68 65 6d 61 3b 0a 20 20 20 20 70 4f 6c 64 20  chema;.    pOld 
23d58 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73  = sqlite3HashIns
23d59 65 72 74 28 26 70 53 63 68 65 6d 61 2d 3e 74 62  ert(&pSchema->tb
23d5a 6c 48 61 73 68 2c 20 70 2d 3e 7a 4e 61 6d 65 2c  lHash, p->zName,
23d5b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
23d5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
23d5d 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 2d  lite3Strlen30(p-
23d5e 3e 7a 4e 61 6d 65 29 2c 70 29 3b 0a 20 20 20 20  >zName),p);.    
23d5f 69 66 28 20 70 4f 6c 64 20 29 7b 0a 20 20 20 20  if( pOld ){.    
23d60 20 20 61 73 73 65 72 74 28 20 70 3d 3d 70 4f 6c    assert( p==pOl
23d61 64 20 29 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20  d );  /* Malloc 
23d62 6d 75 73 74 20 68 61 76 65 20 66 61 69 6c 65 64  must have failed
23d63 20 69 6e 73 69 64 65 20 48 61 73 68 49 6e 73 65   inside HashInse
23d64 72 74 28 29 20 2a 2f 0a 20 20 20 20 20 20 64 62  rt() */.      db
23d65 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
23d66 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   1;.      return
23d67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72  ;.    }.    pPar
23d68 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 3d 20  se->pNewTable = 
23d69 30 3b 0a 20 20 20 20 64 62 2d 3e 6e 54 61 62 6c  0;.    db->nTabl
23d6a 65 2b 2b 3b 0a 20 20 20 20 64 62 2d 3e 66 6c 61  e++;.    db->fla
23d6b 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74  gs |= SQLITE_Int
23d6c 65 72 6e 43 68 61 6e 67 65 73 3b 0a 0a 23 69 66  ernChanges;..#if
23d6d 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
23d6e 5f 41 4c 54 45 52 54 41 42 4c 45 0a 20 20 20 20  _ALTERTABLE.    
23d6f 69 66 28 20 21 70 2d 3e 70 53 65 6c 65 63 74 20  if( !p->pSelect 
23d70 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63  ){.      const c
23d71 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 28 63 6f  har *zName = (co
23d72 6e 73 74 20 63 68 61 72 20 2a 29 70 50 61 72 73  nst char *)pPars
23d73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 7a 3b  e->sNameToken.z;
23d74 0a 20 20 20 20 20 20 69 6e 74 20 6e 4e 61 6d 65  .      int nName
23d75 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
23d76 21 70 53 65 6c 65 63 74 20 26 26 20 70 43 6f 6e  !pSelect && pCon
23d77 73 20 26 26 20 70 45 6e 64 20 29 3b 0a 20 20 20  s && pEnd );.   
23d78 20 20 20 69 66 28 20 70 43 6f 6e 73 2d 3e 7a 3d     if( pCons->z=
23d79 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43  =0 ){.        pC
23d7a 6f 6e 73 20 3d 20 70 45 6e 64 3b 0a 20 20 20 20  ons = pEnd;.    
23d7b 20 20 7d 0a 20 20 20 20 20 20 6e 4e 61 6d 65 20    }.      nName 
23d7c 3d 20 28 69 6e 74 29 28 28 63 6f 6e 73 74 20 63  = (int)((const c
23d7d 68 61 72 20 2a 29 70 43 6f 6e 73 2d 3e 7a 20 2d  har *)pCons->z -
23d7e 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 70   zName);.      p
23d7f 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 20 3d  ->addColOffset =
23d80 20 31 33 20 2b 20 73 71 6c 69 74 65 33 55 74 66   13 + sqlite3Utf
23d81 38 43 68 61 72 4c 65 6e 28 7a 4e 61 6d 65 2c 20  8CharLen(zName, 
23d82 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 23 65  nName);.    }.#e
23d83 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e  ndif.  }.}..#ifn
23d84 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
23d85 56 49 45 57 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70  VIEW./*.** The p
23d86 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73  arser calls this
23d87 20 72 6f 75 74 69 6e 65 20 69 6e 20 6f 72 64 65   routine in orde
23d88 72 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65  r to create a ne
23d89 77 20 56 49 45 57 0a 2a 2f 0a 53 51 4c 49 54 45  w VIEW.*/.SQLITE
23d8a 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
23d8b 6c 69 74 65 33 43 72 65 61 74 65 56 69 65 77 28  lite3CreateView(
23d8c 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
23d8d 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72  ,     /* The par
23d8e 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
23d8f 20 20 54 6f 6b 65 6e 20 2a 70 42 65 67 69 6e 2c    Token *pBegin,
23d90 20 20 20 20 20 2f 2a 20 54 68 65 20 43 52 45 41       /* The CREA
23d91 54 45 20 74 6f 6b 65 6e 20 74 68 61 74 20 62 65  TE token that be
23d92 67 69 6e 73 20 74 68 65 20 73 74 61 74 65 6d 65  gins the stateme
23d93 6e 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  nt */.  Token *p
23d94 4e 61 6d 65 31 2c 20 20 20 20 20 2f 2a 20 54 68  Name1,     /* Th
23d95 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 68 6f 6c  e token that hol
23d96 64 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  ds the name of t
23d97 68 65 20 76 69 65 77 20 2a 2f 0a 20 20 54 6f 6b  he view */.  Tok
23d98 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20  en *pName2,     
23d99 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 20 74 68 61  /* The token tha
23d9a 74 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65  t holds the name
23d9b 20 6f 66 20 74 68 65 20 76 69 65 77 20 2a 2f 0a   of the view */.
23d9c 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63    Select *pSelec
23d9d 74 2c 20 20 20 2f 2a 20 41 20 53 45 4c 45 43 54  t,   /* A SELECT
23d9e 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20   statement that 
23d9f 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20  will become the 
23da0 6e 65 77 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e  new view */.  in
23da1 74 20 69 73 54 65 6d 70 2c 20 20 20 20 20 20 20  t isTemp,       
23da2 20 2f 2a 20 54 52 55 45 20 66 6f 72 20 61 20 54   /* TRUE for a T
23da3 45 4d 50 4f 52 41 52 59 20 76 69 65 77 20 2a 2f  EMPORARY view */
23da4 0a 20 20 69 6e 74 20 6e 6f 45 72 72 20 20 20 20  .  int noErr    
23da5 20 20 20 20 20 20 2f 2a 20 53 75 70 70 72 65 73        /* Suppres
23da6 73 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73  s error messages
23da7 20 69 66 20 56 49 45 57 20 61 6c 72 65 61 64 79   if VIEW already
23da8 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b 0a 20 20   exists */.){.  
23da9 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74 20  Table *p;.  int 
23daa 6e 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  n;.  const char 
23dab 2a 7a 3b 0a 20 20 54 6f 6b 65 6e 20 73 45 6e 64  *z;.  Token sEnd
23dac 3b 0a 20 20 44 62 46 69 78 65 72 20 73 46 69 78  ;.  DbFixer sFix
23dad 3b 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  ;.  Token *pName
23dae 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 73  ;.  int iDb;.  s
23daf 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
23db0 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20  rse->db;..  if( 
23db1 70 50 61 72 73 65 2d 3e 6e 56 61 72 3e 30 20 29  pParse->nVar>0 )
23db2 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
23db3 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 70  orMsg(pParse, "p
23db4 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 6e 6f  arameters are no
23db5 74 20 61 6c 6c 6f 77 65 64 20 69 6e 20 76 69 65  t allowed in vie
23db6 77 73 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ws");.    sqlite
23db7 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62  3SelectDelete(db
23db8 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20  , pSelect);.    
23db9 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 73 71  return;.  }.  sq
23dba 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c 65 28  lite3StartTable(
23dbb 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20  pParse, pName1, 
23dbc 70 4e 61 6d 65 32 2c 20 69 73 54 65 6d 70 2c 20  pName2, isTemp, 
23dbd 31 2c 20 30 2c 20 6e 6f 45 72 72 29 3b 0a 20 20  1, 0, noErr);.  
23dbe 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  p = pParse->pNew
23dbf 54 61 62 6c 65 3b 0a 20 20 69 66 28 20 70 3d 3d  Table;.  if( p==
23dc0 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
23dc1 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c  SelectDelete(db,
23dc2 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 72   pSelect);.    r
23dc3 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73  eturn;.  }.  ass
23dc4 65 72 74 28 20 70 50 61 72 73 65 2d 3e 6e 45 72  ert( pParse->nEr
23dc5 72 3d 3d 30 20 29 3b 20 2f 2a 20 49 66 20 73 71  r==0 ); /* If sq
23dc6 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c 65 20  lite3StartTable 
23dc7 72 65 74 75 72 6e 20 6e 6f 6e 2d 4e 55 4c 4c 20  return non-NULL 
23dc8 74 68 65 6e 0a 20 20 20 20 20 20 20 20 20 20 20  then.           
23dc9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23dca 20 20 2a 2a 20 74 68 65 72 65 20 63 6f 75 6c 64    ** there could
23dcb 20 6e 6f 74 20 68 61 76 65 20 62 65 65 6e 20 61   not have been a
23dcc 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 73 71 6c  n error */.  sql
23dcd 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28  ite3TwoPartName(
23dce 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20  pParse, pName1, 
23dcf 70 4e 61 6d 65 32 2c 20 26 70 4e 61 6d 65 29 3b  pName2, &pName);
23dd0 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33  .  iDb = sqlite3
23dd1 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62  SchemaToIndex(db
23dd2 2c 20 70 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  , p->pSchema);. 
23dd3 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 49   if( sqlite3FixI
23dd4 6e 69 74 28 26 73 46 69 78 2c 20 70 50 61 72 73  nit(&sFix, pPars
23dd5 65 2c 20 69 44 62 2c 20 22 76 69 65 77 22 2c 20  e, iDb, "view", 
23dd6 70 4e 61 6d 65 29 0a 20 20 20 20 26 26 20 73 71  pName).    && sq
23dd7 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74 28 26  lite3FixSelect(&
23dd8 73 46 69 78 2c 20 70 53 65 6c 65 63 74 29 0a 20  sFix, pSelect). 
23dd9 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53   ){.    sqlite3S
23dda 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20  electDelete(db, 
23ddb 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 72 65  pSelect);.    re
23ddc 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  turn;.  }..  /* 
23ddd 4d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74  Make a copy of t
23dde 68 65 20 65 6e 74 69 72 65 20 53 45 4c 45 43 54  he entire SELECT
23ddf 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20   statement that 
23de0 64 65 66 69 6e 65 73 20 74 68 65 20 76 69 65 77  defines the view
23de1 2e 0a 20 20 2a 2a 20 54 68 69 73 20 77 69 6c 6c  ..  ** This will
23de2 20 66 6f 72 63 65 20 61 6c 6c 20 74 68 65 20 45   force all the E
23de3 78 70 72 2e 74 6f 6b 65 6e 2e 7a 20 76 61 6c 75  xpr.token.z valu
23de4 65 73 20 74 6f 20 62 65 20 64 79 6e 61 6d 69 63  es to be dynamic
23de5 61 6c 6c 79 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61  ally.  ** alloca
23de6 74 65 64 20 72 61 74 68 65 72 20 74 68 61 6e 20  ted rather than 
23de7 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 69 6e 70  point to the inp
23de8 75 74 20 73 74 72 69 6e 67 20 2d 20 77 68 69 63  ut string - whic
23de9 68 20 6d 65 61 6e 73 20 74 68 61 74 0a 20 20 2a  h means that.  *
23dea 2a 20 74 68 65 79 20 77 69 6c 6c 20 70 65 72 73  * they will pers
23deb 69 73 74 20 61 66 74 65 72 20 74 68 65 20 63 75  ist after the cu
23dec 72 72 65 6e 74 20 73 71 6c 69 74 65 33 5f 65 78  rrent sqlite3_ex
23ded 65 63 28 29 20 63 61 6c 6c 20 72 65 74 75 72 6e  ec() call return
23dee 73 2e 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70 53 65  s..  */.  p->pSe
23def 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65  lect = sqlite3Se
23df0 6c 65 63 74 44 75 70 28 64 62 2c 20 70 53 65 6c  lectDup(db, pSel
23df1 65 63 74 2c 20 45 58 50 52 44 55 50 5f 52 45 44  ect, EXPRDUP_RED
23df2 55 43 45 29 3b 0a 20 20 73 71 6c 69 74 65 33 53  UCE);.  sqlite3S
23df3 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20  electDelete(db, 
23df4 70 53 65 6c 65 63 74 29 3b 0a 20 20 69 66 28 20  pSelect);.  if( 
23df5 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
23df6 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
23df7 20 20 7d 0a 20 20 69 66 28 20 21 64 62 2d 3e 69    }.  if( !db->i
23df8 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20  nit.busy ){.    
23df9 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f  sqlite3ViewGetCo
23dfa 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65  lumnNames(pParse
23dfb 2c 20 70 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  , p);.  }..  /* 
23dfc 4c 6f 63 61 74 65 20 74 68 65 20 65 6e 64 20 6f  Locate the end o
23dfd 66 20 74 68 65 20 43 52 45 41 54 45 20 56 49 45  f the CREATE VIE
23dfe 57 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 4d 61  W statement.  Ma
23dff 6b 65 20 73 45 6e 64 20 70 6f 69 6e 74 20 74 6f  ke sEnd point to
23e00 0a 20 20 2a 2a 20 74 68 65 20 65 6e 64 2e 0a 20  .  ** the end.. 
23e01 20 2a 2f 0a 20 20 73 45 6e 64 20 3d 20 70 50 61   */.  sEnd = pPa
23e02 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 3b  rse->sLastToken;
23e03 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 73 45  .  if( ALWAYS(sE
23e04 6e 64 2e 7a 5b 30 5d 21 3d 30 29 20 26 26 20 73  nd.z[0]!=0) && s
23e05 45 6e 64 2e 7a 5b 30 5d 21 3d 27 3b 27 20 29 7b  End.z[0]!=';' ){
23e06 0a 20 20 20 20 73 45 6e 64 2e 7a 20 2b 3d 20 73  .    sEnd.z += s
23e07 45 6e 64 2e 6e 3b 0a 20 20 7d 0a 20 20 73 45 6e  End.n;.  }.  sEn
23e08 64 2e 6e 20 3d 20 30 3b 0a 20 20 6e 20 3d 20 28  d.n = 0;.  n = (
23e09 69 6e 74 29 28 73 45 6e 64 2e 7a 20 2d 20 70 42  int)(sEnd.z - pB
23e0a 65 67 69 6e 2d 3e 7a 29 3b 0a 20 20 7a 20 3d 20  egin->z);.  z = 
23e0b 70 42 65 67 69 6e 2d 3e 7a 3b 0a 20 20 77 68 69  pBegin->z;.  whi
23e0c 6c 65 28 20 41 4c 57 41 59 53 28 6e 3e 30 29 20  le( ALWAYS(n>0) 
23e0d 26 26 20 73 71 6c 69 74 65 33 49 73 73 70 61 63  && sqlite3Isspac
23e0e 65 28 7a 5b 6e 2d 31 5d 29 20 29 7b 20 6e 2d 2d  e(z[n-1]) ){ n--
23e0f 3b 20 7d 0a 20 20 73 45 6e 64 2e 7a 20 3d 20 26  ; }.  sEnd.z = &
23e10 7a 5b 6e 2d 31 5d 3b 0a 20 20 73 45 6e 64 2e 6e  z[n-1];.  sEnd.n
23e11 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 55 73 65 20   = 1;..  /* Use 
23e12 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 28  sqlite3EndTable(
23e13 29 20 74 6f 20 61 64 64 20 74 68 65 20 76 69 65  ) to add the vie
23e14 77 20 74 6f 20 74 68 65 20 53 51 4c 49 54 45 5f  w to the SQLITE_
23e15 4d 41 53 54 45 52 20 74 61 62 6c 65 20 2a 2f 0a  MASTER table */.
23e16 20 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c    sqlite3EndTabl
23e17 65 28 70 50 61 72 73 65 2c 20 30 2c 20 26 73 45  e(pParse, 0, &sE
23e18 6e 64 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e  nd, 0);.  return
23e19 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
23e1a 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 20 2a  LITE_OMIT_VIEW *
23e1b 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  /..#if !defined(
23e1c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
23e1d 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51  ) || !defined(SQ
23e1e 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
23e1f 4c 54 41 42 4c 45 29 0a 2f 2a 0a 2a 2a 20 54 68  LTABLE)./*.** Th
23e20 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72  e Table structur
23e21 65 20 70 54 61 62 6c 65 20 69 73 20 72 65 61 6c  e pTable is real
23e22 6c 79 20 61 20 56 49 45 57 2e 20 20 46 69 6c 6c  ly a VIEW.  Fill
23e23 20 69 6e 20 74 68 65 20 6e 61 6d 65 73 20 6f 66   in the names of
23e24 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20  .** the columns 
23e25 6f 66 20 74 68 65 20 76 69 65 77 20 69 6e 20 74  of the view in t
23e26 68 65 20 70 54 61 62 6c 65 20 73 74 72 75 63 74  he pTable struct
23e27 75 72 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65  ure.  Return the
23e28 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 65 72   number.** of er
23e29 72 6f 72 73 2e 20 20 49 66 20 61 6e 20 65 72 72  rors.  If an err
23e2a 6f 72 20 69 73 20 73 65 65 6e 20 6c 65 61 76 65  or is seen leave
23e2b 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
23e2c 65 20 69 6e 20 70 50 61 72 73 65 2d 3e 7a 45 72  e in pParse->zEr
23e2d 72 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rMsg..*/.SQLITE_
23e2e 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
23e2f 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e  te3ViewGetColumn
23e30 4e 61 6d 65 73 28 50 61 72 73 65 20 2a 70 50 61  Names(Parse *pPa
23e31 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62  rse, Table *pTab
23e32 6c 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 53  le){.  Table *pS
23e33 65 6c 54 61 62 3b 20 20 20 2f 2a 20 41 20 66 61  elTab;   /* A fa
23e34 6b 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 77 68  ke table from wh
23e35 69 63 68 20 77 65 20 67 65 74 20 74 68 65 20 72  ich we get the r
23e36 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 53  esult set */.  S
23e37 65 6c 65 63 74 20 2a 70 53 65 6c 3b 20 20 20 20  elect *pSel;    
23e38 20 2f 2a 20 43 6f 70 79 20 6f 66 20 74 68 65 20   /* Copy of the 
23e39 53 45 4c 45 43 54 20 74 68 61 74 20 69 6d 70 6c  SELECT that impl
23e3a 65 6d 65 6e 74 73 20 74 68 65 20 76 69 65 77 20  ements the view 
23e3b 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 20 3d 20  */.  int nErr = 
23e3c 30 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  0;     /* Number
23e3d 20 6f 66 20 65 72 72 6f 72 73 20 65 6e 63 6f 75   of errors encou
23e3e 6e 74 65 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20  ntered */.  int 
23e3f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  n;            /*
23e40 20 54 65 6d 70 6f 72 61 72 69 6c 79 20 68 6f 6c   Temporarily hol
23e41 64 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ds the number of
23e42 20 63 75 72 73 6f 72 73 20 61 73 73 69 67 6e 65   cursors assigne
23e43 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  d */.  sqlite3 *
23e44 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
23e45 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f    /* Database co
23e46 6e 6e 65 63 74 69 6f 6e 20 66 6f 72 20 6d 61 6c  nnection for mal
23e47 6c 6f 63 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20  loc errors */.  
23e48 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76 6f 69  int (*xAuth)(voi
23e49 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61  d*,int,const cha
23e4a 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63  r*,const char*,c
23e4b 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74  onst char*,const
23e4c 20 63 68 61 72 2a 29 3b 0a 0a 20 20 61 73 73 65   char*);..  asse
23e4d 72 74 28 20 70 54 61 62 6c 65 20 29 3b 0a 0a 23  rt( pTable );..#
23e4e 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
23e4f 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
23e50 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 74 61    if( sqlite3Vta
23e51 62 43 61 6c 6c 43 6f 6e 6e 65 63 74 28 70 50 61  bCallConnect(pPa
23e52 72 73 65 2c 20 70 54 61 62 6c 65 29 20 29 7b 0a  rse, pTable) ){.
23e53 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
23e54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 69  E_ERROR;.  }.  i
23e55 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  f( IsVirtual(pTa
23e56 62 6c 65 29 20 29 20 72 65 74 75 72 6e 20 30 3b  ble) ) return 0;
23e57 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66  .#endif..#ifndef
23e58 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45   SQLITE_OMIT_VIE
23e59 57 0a 20 20 2f 2a 20 41 20 70 6f 73 69 74 69 76  W.  /* A positiv
23e5a 65 20 6e 43 6f 6c 20 6d 65 61 6e 73 20 74 68 65  e nCol means the
23e5b 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 73 20 66   columns names f
23e5c 6f 72 20 74 68 69 73 20 76 69 65 77 20 61 72 65  or this view are
23e5d 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 6b 6e  .  ** already kn
23e5e 6f 77 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  own..  */.  if( 
23e5f 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3e 30 20 29  pTable->nCol>0 )
23e60 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 2f 2a   return 0;..  /*
23e61 20 41 20 6e 65 67 61 74 69 76 65 20 6e 43 6f 6c   A negative nCol
23e62 20 69 73 20 61 20 73 70 65 63 69 61 6c 20 6d 61   is a special ma
23e63 72 6b 65 72 20 6d 65 61 6e 69 6e 67 20 74 68 61  rker meaning tha
23e64 74 20 77 65 20 61 72 65 20 63 75 72 72 65 6e 74  t we are current
23e65 6c 79 0a 20 20 2a 2a 20 74 72 79 69 6e 67 20 74  ly.  ** trying t
23e66 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 20 63 6f  o compute the co
23e67 6c 75 6d 6e 20 6e 61 6d 65 73 2e 20 20 49 66 20  lumn names.  If 
23e68 77 65 20 65 6e 74 65 72 20 74 68 69 73 20 72 6f  we enter this ro
23e69 75 74 69 6e 65 20 77 69 74 68 0a 20 20 2a 2a 20  utine with.  ** 
23e6a 61 20 6e 65 67 61 74 69 76 65 20 6e 43 6f 6c 2c  a negative nCol,
23e6b 20 69 74 20 6d 65 61 6e 73 20 74 77 6f 20 6f 72   it means two or
23e6c 20 6d 6f 72 65 20 76 69 65 77 73 20 66 6f 72 6d   more views form
23e6d 20 61 20 6c 6f 6f 70 2c 20 6c 69 6b 65 20 74 68   a loop, like th
23e6e 69 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  is:.  **.  **   
23e6f 20 20 43 52 45 41 54 45 20 56 49 45 57 20 6f 6e    CREATE VIEW on
23e70 65 20 41 53 20 53 45 4c 45 43 54 20 2a 20 46 52  e AS SELECT * FR
23e71 4f 4d 20 74 77 6f 3b 0a 20 20 2a 2a 20 20 20 20  OM two;.  **    
23e72 20 43 52 45 41 54 45 20 56 49 45 57 20 74 77 6f   CREATE VIEW two
23e73 20 41 53 20 53 45 4c 45 43 54 20 2a 20 46 52 4f   AS SELECT * FRO
23e74 4d 20 6f 6e 65 3b 0a 20 20 2a 2a 0a 20 20 2a 2a  M one;.  **.  **
23e75 20 41 63 74 75 61 6c 6c 79 2c 20 74 68 65 20 65   Actually, the e
23e76 72 72 6f 72 20 61 62 6f 76 65 20 69 73 20 6e 6f  rror above is no
23e77 77 20 63 61 75 67 68 74 20 70 72 69 6f 72 20 74  w caught prior t
23e78 6f 20 72 65 61 63 68 69 6e 67 20 74 68 69 73 20  o reaching this 
23e79 70 6f 69 6e 74 2e 0a 20 20 2a 2a 20 42 75 74 20  point..  ** But 
23e7a 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 65  the following te
23e7b 73 74 20 69 73 20 73 74 69 6c 6c 20 69 6d 70 6f  st is still impo
23e7c 72 74 61 6e 74 20 61 73 20 69 74 20 64 6f 65 73  rtant as it does
23e7d 20 63 6f 6d 65 20 75 70 0a 20 20 2a 2a 20 69 6e   come up.  ** in
23e7e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
23e7f 20 20 2a 2a 20 0a 20 20 2a 2a 20 20 20 20 20 43    ** .  **     C
23e80 52 45 41 54 45 20 54 41 42 4c 45 20 6d 61 69 6e  REATE TABLE main
23e81 2e 65 78 31 28 61 29 3b 0a 20 20 2a 2a 20 20 20  .ex1(a);.  **   
23e82 20 20 43 52 45 41 54 45 20 54 45 4d 50 20 56 49    CREATE TEMP VI
23e83 45 57 20 65 78 31 20 41 53 20 53 45 4c 45 43 54  EW ex1 AS SELECT
23e84 20 61 20 46 52 4f 4d 20 65 78 31 3b 0a 20 20 2a   a FROM ex1;.  *
23e85 2a 20 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46  *     SELECT * F
23e86 52 4f 4d 20 74 65 6d 70 2e 65 78 31 3b 0a 20 20  ROM temp.ex1;.  
23e87 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 6c 65 2d  */.  if( pTable-
23e88 3e 6e 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 73  >nCol<0 ){.    s
23e89 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
23e8a 50 61 72 73 65 2c 20 22 76 69 65 77 20 25 73 20  Parse, "view %s 
23e8b 69 73 20 63 69 72 63 75 6c 61 72 6c 79 20 64 65  is circularly de
23e8c 66 69 6e 65 64 22 2c 20 70 54 61 62 6c 65 2d 3e  fined", pTable->
23e8d 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75  zName);.    retu
23e8e 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 61 73 73 65  rn 1;.  }.  asse
23e8f 72 74 28 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c  rt( pTable->nCol
23e90 3e 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  >=0 );..  /* If 
23e91 77 65 20 67 65 74 20 74 68 69 73 20 66 61 72 2c  we get this far,
23e92 20 69 74 20 6d 65 61 6e 73 20 77 65 20 6e 65 65   it means we nee
23e93 64 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 68 65  d to compute the
23e94 20 74 61 62 6c 65 20 6e 61 6d 65 73 2e 0a 20 20   table names..  
23e95 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65  ** Note that the
23e96 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
23e97 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 63  ResultSetOfSelec
23e98 74 28 29 20 77 69 6c 6c 20 65 78 70 61 6e 64 20  t() will expand 
23e99 61 6e 79 0a 20 20 2a 2a 20 22 2a 22 20 65 6c 65  any.  ** "*" ele
23e9a 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 72 65 73  ments in the res
23e9b 75 6c 74 73 20 73 65 74 20 6f 66 20 74 68 65 20  ults set of the 
23e9c 76 69 65 77 20 61 6e 64 20 77 69 6c 6c 20 61 73  view and will as
23e9d 73 69 67 6e 20 63 75 72 73 6f 72 73 0a 20 20 2a  sign cursors.  *
23e9e 2a 20 74 6f 20 74 68 65 20 65 6c 65 6d 65 6e 74  * to the element
23e9f 73 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c  s of the FROM cl
23ea0 61 75 73 65 2e 20 20 42 75 74 20 77 65 20 64 6f  ause.  But we do
23ea1 20 6e 6f 74 20 77 61 6e 74 20 74 68 65 73 65 20   not want these 
23ea2 63 68 61 6e 67 65 73 0a 20 20 2a 2a 20 74 6f 20  changes.  ** to 
23ea3 62 65 20 70 65 72 6d 61 6e 65 6e 74 2e 20 20 53  be permanent.  S
23ea4 6f 20 74 68 65 20 63 6f 6d 70 75 74 61 74 69 6f  o the computatio
23ea5 6e 20 69 73 20 64 6f 6e 65 20 6f 6e 20 61 20 63  n is done on a c
23ea6 6f 70 79 20 6f 66 20 74 68 65 20 53 45 4c 45 43  opy of the SELEC
23ea7 54 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74  T.  ** statement
23ea8 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68   that defines th
23ea9 65 20 76 69 65 77 2e 0a 20 20 2a 2f 0a 20 20 61  e view..  */.  a
23eaa 73 73 65 72 74 28 20 70 54 61 62 6c 65 2d 3e 70  ssert( pTable->p
23eab 53 65 6c 65 63 74 20 29 3b 0a 20 20 70 53 65 6c  Select );.  pSel
23eac 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
23ead 44 75 70 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e  Dup(db, pTable->
23eae 70 53 65 6c 65 63 74 2c 20 30 29 3b 0a 20 20 69  pSelect, 0);.  i
23eaf 66 28 20 70 53 65 6c 20 29 7b 0a 20 20 20 20 75  f( pSel ){.    u
23eb0 38 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69 64  8 enableLookasid
23eb1 65 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64  e = db->lookasid
23eb2 65 2e 62 45 6e 61 62 6c 65 64 3b 0a 20 20 20 20  e.bEnabled;.    
23eb3 6e 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  n = pParse->nTab
23eb4 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 72 63  ;.    sqlite3Src
23eb5 4c 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f 72  ListAssignCursor
23eb6 73 28 70 50 61 72 73 65 2c 20 70 53 65 6c 2d 3e  s(pParse, pSel->
23eb7 70 53 72 63 29 3b 0a 20 20 20 20 70 54 61 62 6c  pSrc);.    pTabl
23eb8 65 2d 3e 6e 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20  e->nCol = -1;.  
23eb9 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e    db->lookaside.
23eba 62 45 6e 61 62 6c 65 64 20 3d 20 30 3b 0a 23 69  bEnabled = 0;.#i
23ebb 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
23ebc 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a  T_AUTHORIZATION.
23ebd 20 20 20 20 78 41 75 74 68 20 3d 20 64 62 2d 3e      xAuth = db->
23ebe 78 41 75 74 68 3b 0a 20 20 20 20 64 62 2d 3e 78  xAuth;.    db->x
23ebf 41 75 74 68 20 3d 20 30 3b 0a 20 20 20 20 70 53  Auth = 0;.    pS
23ec0 65 6c 54 61 62 20 3d 20 73 71 6c 69 74 65 33 52  elTab = sqlite3R
23ec1 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 63 74  esultSetOfSelect
23ec2 28 70 50 61 72 73 65 2c 20 70 53 65 6c 29 3b 0a  (pParse, pSel);.
23ec3 20 20 20 20 64 62 2d 3e 78 41 75 74 68 20 3d 20      db->xAuth = 
23ec4 78 41 75 74 68 3b 0a 23 65 6c 73 65 0a 20 20 20  xAuth;.#else.   
23ec5 20 70 53 65 6c 54 61 62 20 3d 20 73 71 6c 69 74   pSelTab = sqlit
23ec6 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c  e3ResultSetOfSel
23ec7 65 63 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c  ect(pParse, pSel
23ec8 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 64 62  );.#endif.    db
23ec9 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61  ->lookaside.bEna
23eca 62 6c 65 64 20 3d 20 65 6e 61 62 6c 65 4c 6f 6f  bled = enableLoo
23ecb 6b 61 73 69 64 65 3b 0a 20 20 20 20 70 50 61 72  kaside;.    pPar
23ecc 73 65 2d 3e 6e 54 61 62 20 3d 20 6e 3b 0a 20 20  se->nTab = n;.  
23ecd 20 20 69 66 28 20 70 53 65 6c 54 61 62 20 29 7b    if( pSelTab ){
23ece 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
23ecf 54 61 62 6c 65 2d 3e 61 43 6f 6c 3d 3d 30 20 29  Table->aCol==0 )
23ed0 3b 0a 20 20 20 20 20 20 70 54 61 62 6c 65 2d 3e  ;.      pTable->
23ed1 6e 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 2d 3e  nCol = pSelTab->
23ed2 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 70 54 61 62  nCol;.      pTab
23ed3 6c 65 2d 3e 61 43 6f 6c 20 3d 20 70 53 65 6c 54  le->aCol = pSelT
23ed4 61 62 2d 3e 61 43 6f 6c 3b 0a 20 20 20 20 20 20  ab->aCol;.      
23ed5 70 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c 20 3d 20  pSelTab->nCol = 
23ed6 30 3b 0a 20 20 20 20 20 20 70 53 65 6c 54 61 62  0;.      pSelTab
23ed7 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20  ->aCol = 0;.    
23ed8 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54    sqlite3DeleteT
23ed9 61 62 6c 65 28 70 53 65 6c 54 61 62 29 3b 0a 20  able(pSelTab);. 
23eda 20 20 20 20 20 70 54 61 62 6c 65 2d 3e 70 53 63       pTable->pSc
23edb 68 65 6d 61 2d 3e 66 6c 61 67 73 20 7c 3d 20 44  hema->flags |= D
23edc 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 3b 0a  B_UnresetViews;.
23edd 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
23ede 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20   pTable->nCol = 
23edf 30 3b 0a 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b  0;.      nErr++;
23ee0 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
23ee1 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64  e3SelectDelete(d
23ee2 62 2c 20 70 53 65 6c 29 3b 0a 20 20 7d 20 65 6c  b, pSel);.  } el
23ee3 73 65 20 7b 0a 20 20 20 20 6e 45 72 72 2b 2b 3b  se {.    nErr++;
23ee4 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  .  }.#endif /* S
23ee5 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 20  QLITE_OMIT_VIEW 
23ee6 2a 2f 0a 20 20 72 65 74 75 72 6e 20 6e 45 72 72  */.  return nErr
23ee7 3b 20 20 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ;  .}.#endif /* 
23ee8 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
23ee9 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64  OMIT_VIEW) || !d
23eea 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
23eeb 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29  IT_VIRTUALTABLE)
23eec 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
23eed 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 2f 2a  ITE_OMIT_VIEW./*
23eee 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 63 6f  .** Clear the co
23eef 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 72 6f 6d 20  lumn names from 
23ef0 65 76 65 72 79 20 56 49 45 57 20 69 6e 20 64 61  every VIEW in da
23ef1 74 61 62 61 73 65 20 69 64 78 2e 0a 2a 2f 0a 73  tabase idx..*/.s
23ef2 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74  tatic void sqlit
23ef3 65 56 69 65 77 52 65 73 65 74 41 6c 6c 28 73 71  eViewResetAll(sq
23ef4 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69  lite3 *db, int i
23ef5 64 78 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20  dx){.  HashElem 
23ef6 2a 69 3b 0a 20 20 69 66 28 20 21 44 62 48 61 73  *i;.  if( !DbHas
23ef7 50 72 6f 70 65 72 74 79 28 64 62 2c 20 69 64 78  Property(db, idx
23ef8 2c 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77  , DB_UnresetView
23ef9 73 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66  s) ) return;.  f
23efa 6f 72 28 69 3d 73 71 6c 69 74 65 48 61 73 68 46  or(i=sqliteHashF
23efb 69 72 73 74 28 26 64 62 2d 3e 61 44 62 5b 69 64  irst(&db->aDb[id
23efc 78 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48  x].pSchema->tblH
23efd 61 73 68 29 3b 20 69 3b 69 3d 73 71 6c 69 74 65  ash); i;i=sqlite
23efe 48 61 73 68 4e 65 78 74 28 69 29 29 7b 0a 20 20  HashNext(i)){.  
23eff 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
23f00 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 69  sqliteHashData(i
23f01 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62 2d  );.    if( pTab-
23f02 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20  >pSelect ){.    
23f03 20 20 73 71 6c 69 74 65 52 65 73 65 74 43 6f 6c    sqliteResetCol
23f04 75 6d 6e 4e 61 6d 65 73 28 70 54 61 62 29 3b 0a  umnNames(pTab);.
23f05 20 20 20 20 7d 0a 20 20 7d 0a 20 20 44 62 43 6c      }.  }.  DbCl
23f06 65 61 72 50 72 6f 70 65 72 74 79 28 64 62 2c 20  earProperty(db, 
23f07 69 64 78 2c 20 44 42 5f 55 6e 72 65 73 65 74 56  idx, DB_UnresetV
23f08 69 65 77 73 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23  iews);.}.#else.#
23f09 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 56 69   define sqliteVi
23f0a 65 77 52 65 73 65 74 41 6c 6c 28 41 2c 42 29 0a  ewResetAll(A,B).
23f0b 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
23f0c 5f 4f 4d 49 54 5f 56 49 45 57 20 2a 2f 0a 0a 2f  _OMIT_VIEW */../
23f0d 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
23f0e 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20  on is called by 
23f0f 74 68 65 20 56 44 42 45 20 74 6f 20 61 64 6a 75  the VDBE to adju
23f10 73 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  st the internal 
23f11 73 63 68 65 6d 61 0a 2a 2a 20 75 73 65 64 20 62  schema.** used b
23f12 79 20 53 51 4c 69 74 65 20 77 68 65 6e 20 74 68  y SQLite when th
23f13 65 20 62 74 72 65 65 20 6c 61 79 65 72 20 6d 6f  e btree layer mo
23f14 76 65 73 20 61 20 74 61 62 6c 65 20 72 6f 6f 74  ves a table root
23f15 20 70 61 67 65 2e 20 54 68 65 0a 2a 2a 20 72 6f   page. The.** ro
23f16 6f 74 2d 70 61 67 65 20 6f 66 20 61 20 74 61 62  ot-page of a tab
23f17 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 6e 20 64  le or index in d
23f18 61 74 61 62 61 73 65 20 69 44 62 20 68 61 73 20  atabase iDb has 
23f19 63 68 61 6e 67 65 64 20 66 72 6f 6d 20 69 46 72  changed from iFr
23f1a 6f 6d 0a 2a 2a 20 74 6f 20 69 54 6f 2e 0a 2a 2a  om.** to iTo..**
23f1b 0a 2a 2a 20 54 69 63 6b 65 74 20 23 31 37 32 38  .** Ticket #1728
23f1c 3a 20 20 54 68 65 20 73 79 6d 62 6f 6c 20 74 61  :  The symbol ta
23f1d 62 6c 65 20 6d 69 67 68 74 20 73 74 69 6c 6c 20  ble might still 
23f1e 63 6f 6e 74 61 69 6e 20 69 6e 66 6f 72 6d 61 74  contain informat
23f1f 69 6f 6e 0a 2a 2a 20 6f 6e 20 74 61 62 6c 65 73  ion.** on tables
23f20 20 61 6e 64 2f 6f 72 20 69 6e 64 69 63 65 73 20   and/or indices 
23f21 74 68 61 74 20 61 72 65 20 74 68 65 20 70 72 6f  that are the pro
23f22 63 65 73 73 20 6f 66 20 62 65 69 6e 67 20 64 65  cess of being de
23f23 6c 65 74 65 64 2e 0a 2a 2a 20 49 66 20 79 6f 75  leted..** If you
23f24 20 61 72 65 20 75 6e 6c 75 63 6b 79 2c 20 6f 6e   are unlucky, on
23f25 65 20 6f 66 20 74 68 6f 73 65 20 64 65 6c 65 74  e of those delet
23f26 65 64 20 69 6e 64 69 63 65 73 20 6f 72 20 74 61  ed indices or ta
23f27 62 6c 65 73 20 6d 69 67 68 74 0a 2a 2a 20 68 61  bles might.** ha
23f28 76 65 20 74 68 65 20 73 61 6d 65 20 72 6f 6f 74  ve the same root
23f29 70 61 67 65 20 6e 75 6d 62 65 72 20 61 73 20 74  page number as t
23f2a 68 65 20 72 65 61 6c 20 74 61 62 6c 65 20 6f 72  he real table or
23f2b 20 69 6e 64 65 78 20 74 68 61 74 20 69 73 0a 2a   index that is.*
23f2c 2a 20 62 65 69 6e 67 20 6d 6f 76 65 64 2e 20 20  * being moved.  
23f2d 53 6f 20 77 65 20 63 61 6e 6e 6f 74 20 73 74 6f  So we cannot sto
23f2e 70 20 73 65 61 72 63 68 69 6e 67 20 61 66 74 65  p searching afte
23f2f 72 20 74 68 65 20 66 69 72 73 74 20 6d 61 74 63  r the first matc
23f30 68 20 0a 2a 2a 20 62 65 63 61 75 73 65 20 74 68  h .** because th
23f31 65 20 66 69 72 73 74 20 6d 61 74 63 68 20 6d 69  e first match mi
23f32 67 68 74 20 62 65 20 66 6f 72 20 6f 6e 65 20 6f  ght be for one o
23f33 66 20 74 68 65 20 64 65 6c 65 74 65 64 20 69 6e  f the deleted in
23f34 64 69 63 65 73 0a 2a 2a 20 6f 72 20 74 61 62 6c  dices.** or tabl
23f35 65 73 20 61 6e 64 20 6e 6f 74 20 74 68 65 20 74  es and not the t
23f36 61 62 6c 65 2f 69 6e 64 65 78 20 74 68 61 74 20  able/index that 
23f37 69 73 20 61 63 74 75 61 6c 6c 79 20 62 65 69 6e  is actually bein
23f38 67 20 6d 6f 76 65 64 2e 0a 2a 2a 20 57 65 20 6d  g moved..** We m
23f39 75 73 74 20 63 6f 6e 74 69 6e 75 65 20 6c 6f 6f  ust continue loo
23f3a 70 69 6e 67 20 75 6e 74 69 6c 20 61 6c 6c 20 74  ping until all t
23f3b 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63 65  ables and indice
23f3c 73 20 77 69 74 68 0a 2a 2a 20 72 6f 6f 74 70 61  s with.** rootpa
23f3d 67 65 3d 3d 69 46 72 6f 6d 20 68 61 76 65 20 62  ge==iFrom have b
23f3e 65 65 6e 20 63 6f 6e 76 65 72 74 65 64 20 74 6f  een converted to
23f3f 20 68 61 76 65 20 61 20 72 6f 6f 74 70 61 67 65   have a rootpage
23f40 20 6f 66 20 69 54 6f 0a 2a 2a 20 69 6e 20 6f 72   of iTo.** in or
23f41 64 65 72 20 74 6f 20 62 65 20 63 65 72 74 61 69  der to be certai
23f42 6e 20 74 68 61 74 20 77 65 20 67 6f 74 20 74 68  n that we got th
23f43 65 20 72 69 67 68 74 20 6f 6e 65 2e 0a 2a 2f 0a  e right one..*/.
23f44 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
23f45 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 53  MIT_AUTOVACUUM.S
23f46 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
23f47 69 64 20 73 71 6c 69 74 65 33 52 6f 6f 74 50 61  id sqlite3RootPa
23f48 67 65 4d 6f 76 65 64 28 44 62 20 2a 70 44 62 2c  geMoved(Db *pDb,
23f49 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74 20   int iFrom, int 
23f4a 69 54 6f 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d  iTo){.  HashElem
23f4b 20 2a 70 45 6c 65 6d 3b 0a 20 20 48 61 73 68 20   *pElem;.  Hash 
23f4c 2a 70 48 61 73 68 3b 0a 0a 20 20 70 48 61 73 68  *pHash;..  pHash
23f4d 20 3d 20 26 70 44 62 2d 3e 70 53 63 68 65 6d 61   = &pDb->pSchema
23f4e 2d 3e 74 62 6c 48 61 73 68 3b 0a 20 20 66 6f 72  ->tblHash;.  for
23f4f 28 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73  (pElem=sqliteHas
23f50 68 46 69 72 73 74 28 70 48 61 73 68 29 3b 20 70  hFirst(pHash); p
23f51 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d 73 71 6c 69  Elem; pElem=sqli
23f52 74 65 48 61 73 68 4e 65 78 74 28 70 45 6c 65 6d  teHashNext(pElem
23f53 29 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70  )){.    Table *p
23f54 54 61 62 20 3d 20 73 71 6c 69 74 65 48 61 73 68  Tab = sqliteHash
23f55 44 61 74 61 28 70 45 6c 65 6d 29 3b 0a 20 20 20  Data(pElem);.   
23f56 20 69 66 28 20 70 54 61 62 2d 3e 74 6e 75 6d 3d   if( pTab->tnum=
23f57 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20  =iFrom ){.      
23f58 70 54 61 62 2d 3e 74 6e 75 6d 20 3d 20 69 54 6f  pTab->tnum = iTo
23f59 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 48  ;.    }.  }.  pH
23f5a 61 73 68 20 3d 20 26 70 44 62 2d 3e 70 53 63 68  ash = &pDb->pSch
23f5b 65 6d 61 2d 3e 69 64 78 48 61 73 68 3b 0a 20 20  ema->idxHash;.  
23f5c 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c 69 74 65  for(pElem=sqlite
23f5d 48 61 73 68 46 69 72 73 74 28 70 48 61 73 68 29  HashFirst(pHash)
23f5e 3b 20 70 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d 73  ; pElem; pElem=s
23f5f 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70 45  qliteHashNext(pE
23f60 6c 65 6d 29 29 7b 0a 20 20 20 20 49 6e 64 65 78  lem)){.    Index
23f61 20 2a 70 49 64 78 20 3d 20 73 71 6c 69 74 65 48   *pIdx = sqliteH
23f62 61 73 68 44 61 74 61 28 70 45 6c 65 6d 29 3b 0a  ashData(pElem);.
23f63 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 74 6e      if( pIdx->tn
23f64 75 6d 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20  um==iFrom ){.   
23f65 20 20 20 70 49 64 78 2d 3e 74 6e 75 6d 20 3d 20     pIdx->tnum = 
23f66 69 54 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  iTo;.    }.  }.}
23f67 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57  .#endif../*.** W
23f68 72 69 74 65 20 63 6f 64 65 20 74 6f 20 65 72 61  rite code to era
23f69 73 65 20 74 68 65 20 74 61 62 6c 65 20 77 69 74  se the table wit
23f6a 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62  h root-page iTab
23f6b 6c 65 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65  le from database
23f6c 20 69 44 62 2e 0a 2a 2a 20 41 6c 73 6f 20 77 72   iDb..** Also wr
23f6d 69 74 65 20 63 6f 64 65 20 74 6f 20 6d 6f 64 69  ite code to modi
23f6e 66 79 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61  fy the sqlite_ma
23f6f 73 74 65 72 20 74 61 62 6c 65 20 61 6e 64 20 69  ster table and i
23f70 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 0a 2a  nternal schema.*
23f71 2a 20 69 66 20 61 20 72 6f 6f 74 2d 70 61 67 65  * if a root-page
23f72 20 6f 66 20 61 6e 6f 74 68 65 72 20 74 61 62 6c   of another tabl
23f73 65 20 69 73 20 6d 6f 76 65 64 20 62 79 20 74 68  e is moved by th
23f74 65 20 62 74 72 65 65 2d 6c 61 79 65 72 20 77 68  e btree-layer wh
23f75 69 6c 73 74 0a 2a 2a 20 65 72 61 73 69 6e 67 20  ilst.** erasing 
23f76 69 54 61 62 6c 65 20 28 74 68 69 73 20 63 61 6e  iTable (this can
23f77 20 68 61 70 70 65 6e 20 77 69 74 68 20 61 6e 20   happen with an 
23f78 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61  auto-vacuum data
23f79 62 61 73 65 29 2e 0a 2a 2f 20 0a 73 74 61 74 69  base)..*/ .stati
23f7a 63 20 76 6f 69 64 20 64 65 73 74 72 6f 79 52 6f  c void destroyRo
23f7b 6f 74 50 61 67 65 28 50 61 72 73 65 20 2a 70 50  otPage(Parse *pP
23f7c 61 72 73 65 2c 20 69 6e 74 20 69 54 61 62 6c 65  arse, int iTable
23f7d 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 56 64  , int iDb){.  Vd
23f7e 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47  be *v = sqlite3G
23f7f 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
23f80 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74    int r1 = sqlit
23f81 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
23f82 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  rse);.  sqlite3V
23f83 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
23f84 44 65 73 74 72 6f 79 2c 20 69 54 61 62 6c 65 2c  Destroy, iTable,
23f85 20 72 31 2c 20 69 44 62 29 3b 0a 20 20 73 71 6c   r1, iDb);.  sql
23f86 69 74 65 33 4d 61 79 41 62 6f 72 74 28 70 50 61  ite3MayAbort(pPa
23f87 72 73 65 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  rse);.#ifndef SQ
23f88 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
23f89 43 55 55 4d 0a 20 20 2f 2a 20 4f 50 5f 44 65 73  CUUM.  /* OP_Des
23f8a 74 72 6f 79 20 73 74 6f 72 65 73 20 61 6e 20 69  troy stores an i
23f8b 6e 20 69 6e 74 65 67 65 72 20 72 31 2e 20 49 66  n integer r1. If
23f8c 20 74 68 69 73 20 69 6e 74 65 67 65 72 0a 20 20   this integer.  
23f8d 2a 2a 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  ** is non-zero, 
23f8e 74 68 65 6e 20 69 74 20 69 73 20 74 68 65 20 72  then it is the r
23f8f 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20  oot page number 
23f90 6f 66 20 61 20 74 61 62 6c 65 20 6d 6f 76 65 64  of a table moved
23f91 20 74 6f 0a 20 20 2a 2a 20 6c 6f 63 61 74 69 6f   to.  ** locatio
23f92 6e 20 69 54 61 62 6c 65 2e 20 54 68 65 20 66 6f  n iTable. The fo
23f93 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 6d 6f 64  llowing code mod
23f94 69 66 69 65 73 20 74 68 65 20 73 71 6c 69 74 65  ifies the sqlite
23f95 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20 74 6f  _master table to
23f96 0a 20 20 2a 2a 20 72 65 66 6c 65 63 74 20 74 68  .  ** reflect th
23f97 69 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  is..  **.  ** Th
23f98 65 20 22 23 4e 4e 4e 22 20 69 6e 20 74 68 65 20  e "#NNN" in the 
23f99 53 51 4c 20 69 73 20 61 20 73 70 65 63 69 61 6c  SQL is a special
23f9a 20 63 6f 6e 73 74 61 6e 74 20 74 68 61 74 20 6d   constant that m
23f9b 65 61 6e 73 20 77 68 61 74 65 76 65 72 20 76 61  eans whatever va
23f9c 6c 75 65 0a 20 20 2a 2a 20 69 73 20 69 6e 20 72  lue.  ** is in r
23f9d 65 67 69 73 74 65 72 20 4e 4e 4e 2e 20 20 53 65  egister NNN.  Se
23f9e 65 20 67 72 61 6d 6d 61 72 20 72 75 6c 65 73 20  e grammar rules 
23f9f 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
23fa0 74 68 65 20 54 4b 5f 52 45 47 49 53 54 45 52 0a  the TK_REGISTER.
23fa1 20 20 2a 2a 20 74 6f 6b 65 6e 20 66 6f 72 20 61    ** token for a
23fa2 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
23fa3 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 73 71  ation..  */.  sq
23fa4 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
23fa5 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 22  (pParse, .     "
23fa6 55 50 44 41 54 45 20 25 51 2e 25 73 20 53 45 54  UPDATE %Q.%s SET
23fa7 20 72 6f 6f 74 70 61 67 65 3d 25 64 20 57 48 45   rootpage=%d WHE
23fa8 52 45 20 23 25 64 20 41 4e 44 20 72 6f 6f 74 70  RE #%d AND rootp
23fa9 61 67 65 3d 23 25 64 22 2c 0a 20 20 20 20 20 70  age=#%d",.     p
23faa 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 69  Parse->db->aDb[i
23fab 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d  Db].zName, SCHEM
23fac 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 69 54  A_TABLE(iDb), iT
23fad 61 62 6c 65 2c 20 72 31 2c 20 72 31 29 3b 0a 23  able, r1, r1);.#
23fae 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 52  endif.  sqlite3R
23faf 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
23fb0 61 72 73 65 2c 20 72 31 29 3b 0a 7d 0a 0a 2f 2a  arse, r1);.}../*
23fb1 0a 2a 2a 20 57 72 69 74 65 20 56 44 42 45 20 63  .** Write VDBE c
23fb2 6f 64 65 20 74 6f 20 65 72 61 73 65 20 74 61 62  ode to erase tab
23fb3 6c 65 20 70 54 61 62 20 61 6e 64 20 61 6c 6c 20  le pTab and all 
23fb4 61 73 73 6f 63 69 61 74 65 64 20 69 6e 64 69 63  associated indic
23fb5 65 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2a 20 43  es on disk..** C
23fb6 6f 64 65 20 74 6f 20 75 70 64 61 74 65 20 74 68  ode to update th
23fb7 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  e sqlite_master 
23fb8 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 74 65 72  tables and inter
23fb9 6e 61 6c 20 73 63 68 65 6d 61 20 64 65 66 69 6e  nal schema defin
23fba 69 74 69 6f 6e 73 0a 2a 2a 20 69 6e 20 63 61 73  itions.** in cas
23fbb 65 20 61 20 72 6f 6f 74 2d 70 61 67 65 20 62 65  e a root-page be
23fbc 6c 6f 6e 67 69 6e 67 20 74 6f 20 61 6e 6f 74 68  longing to anoth
23fbd 65 72 20 74 61 62 6c 65 20 69 73 20 6d 6f 76 65  er table is move
23fbe 64 20 62 79 20 74 68 65 20 62 74 72 65 65 20 6c  d by the btree l
23fbf 61 79 65 72 0a 2a 2a 20 69 73 20 61 6c 73 6f 20  ayer.** is also 
23fc0 61 64 64 65 64 20 28 74 68 69 73 20 63 61 6e 20  added (this can 
23fc1 68 61 70 70 65 6e 20 77 69 74 68 20 61 6e 20 61  happen with an a
23fc2 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62  uto-vacuum datab
23fc3 61 73 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ase)..*/.static 
23fc4 76 6f 69 64 20 64 65 73 74 72 6f 79 54 61 62 6c  void destroyTabl
23fc5 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
23fc6 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 23   Table *pTab){.#
23fc7 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
23fc8 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 49  T_AUTOVACUUM.  I
23fc9 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 69 6e  ndex *pIdx;.  in
23fca 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53  t iDb = sqlite3S
23fcb 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61  chemaToIndex(pPa
23fcc 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70  rse->db, pTab->p
23fcd 53 63 68 65 6d 61 29 3b 0a 20 20 64 65 73 74 72  Schema);.  destr
23fce 6f 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73  oyRootPage(pPars
23fcf 65 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 69  e, pTab->tnum, i
23fd0 44 62 29 3b 0a 20 20 66 6f 72 28 70 49 64 78 3d  Db);.  for(pIdx=
23fd1 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49  pTab->pIndex; pI
23fd2 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70  dx; pIdx=pIdx->p
23fd3 4e 65 78 74 29 7b 0a 20 20 20 20 64 65 73 74 72  Next){.    destr
23fd4 6f 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73  oyRootPage(pPars
23fd5 65 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69  e, pIdx->tnum, i
23fd6 44 62 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20  Db);.  }.#else. 
23fd7 20 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 62   /* If the datab
23fd8 61 73 65 20 6d 61 79 20 62 65 20 61 75 74 6f 2d  ase may be auto-
23fd9 76 61 63 75 75 6d 20 63 61 70 61 62 6c 65 20 28  vacuum capable (
23fda 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  if SQLITE_OMIT_A
23fdb 55 54 4f 56 41 43 55 55 4d 0a 20 20 2a 2a 20 69  UTOVACUUM.  ** i
23fdc 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c 20  s not defined), 
23fdd 74 68 65 6e 20 69 74 20 69 73 20 69 6d 70 6f 72  then it is impor
23fde 74 61 6e 74 20 74 6f 20 63 61 6c 6c 20 4f 50 5f  tant to call OP_
23fdf 44 65 73 74 72 6f 79 20 6f 6e 20 74 68 65 0a 20  Destroy on the. 
23fe0 20 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 69 6e   ** table and in
23fe1 64 65 78 20 72 6f 6f 74 2d 70 61 67 65 73 20 69  dex root-pages i
23fe2 6e 20 6f 72 64 65 72 2c 20 73 74 61 72 74 69 6e  n order, startin
23fe3 67 20 77 69 74 68 20 74 68 65 20 6e 75 6d 65 72  g with the numer
23fe4 69 63 61 6c 6c 79 20 0a 20 20 2a 2a 20 6c 61 72  ically .  ** lar
23fe5 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 20 6e  gest root-page n
23fe6 75 6d 62 65 72 2e 20 54 68 69 73 20 67 75 61 72  umber. This guar
23fe7 61 6e 74 65 65 73 20 74 68 61 74 20 6e 6f 6e 65  antees that none
23fe8 20 6f 66 20 74 68 65 20 72 6f 6f 74 2d 70 61 67   of the root-pag
23fe9 65 73 0a 20 20 2a 2a 20 74 6f 20 62 65 20 64 65  es.  ** to be de
23fea 73 74 72 6f 79 65 64 20 69 73 20 72 65 6c 6f 63  stroyed is reloc
23feb 61 74 65 64 20 62 79 20 61 6e 20 65 61 72 6c 69  ated by an earli
23fec 65 72 20 4f 50 5f 44 65 73 74 72 6f 79 2e 20 69  er OP_Destroy. i
23fed 2e 65 2e 20 69 66 20 74 68 65 0a 20 20 2a 2a 20  .e. if the.  ** 
23fee 66 6f 6c 6c 6f 77 69 6e 67 20 77 65 72 65 20 63  following were c
23fef 6f 64 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  oded:.  **.  ** 
23ff0 4f 50 5f 44 65 73 74 72 6f 79 20 34 20 30 0a 20  OP_Destroy 4 0. 
23ff1 20 2a 2a 20 2e 2e 2e 0a 20 20 2a 2a 20 4f 50 5f   ** ....  ** OP_
23ff2 44 65 73 74 72 6f 79 20 35 20 30 0a 20 20 2a 2a  Destroy 5 0.  **
23ff3 0a 20 20 2a 2a 20 61 6e 64 20 72 6f 6f 74 20 70  .  ** and root p
23ff4 61 67 65 20 35 20 68 61 70 70 65 6e 65 64 20 74  age 5 happened t
23ff5 6f 20 62 65 20 74 68 65 20 6c 61 72 67 65 73 74  o be the largest
23ff6 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65   root-page numbe
23ff7 72 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61  r in the.  ** da
23ff8 74 61 62 61 73 65 2c 20 74 68 65 6e 20 72 6f 6f  tabase, then roo
23ff9 74 20 70 61 67 65 20 35 20 77 6f 75 6c 64 20 62  t page 5 would b
23ffa 65 20 6d 6f 76 65 64 20 74 6f 20 70 61 67 65 20  e moved to page 
23ffb 34 20 62 79 20 74 68 65 20 0a 20 20 2a 2a 20 22  4 by the .  ** "
23ffc 4f 50 5f 44 65 73 74 72 6f 79 20 34 20 30 22 20  OP_Destroy 4 0" 
23ffd 6f 70 63 6f 64 65 2e 20 54 68 65 20 73 75 62 73  opcode. The subs
23ffe 65 71 75 65 6e 74 20 22 4f 50 5f 44 65 73 74 72  equent "OP_Destr
23fff 6f 79 20 35 20 30 22 20 77 6f 75 6c 64 20 68 69  oy 5 0" would hi
24000 74 0a 20 20 2a 2a 20 61 20 66 72 65 65 2d 6c 69  t.  ** a free-li
24001 73 74 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20  st page..  */.  
24002 69 6e 74 20 69 54 61 62 20 3d 20 70 54 61 62 2d  int iTab = pTab-
24003 3e 74 6e 75 6d 3b 0a 20 20 69 6e 74 20 69 44 65  >tnum;.  int iDe
24004 73 74 72 6f 79 65 64 20 3d 20 30 3b 0a 0a 20 20  stroyed = 0;..  
24005 77 68 69 6c 65 28 20 31 20 29 7b 0a 20 20 20 20  while( 1 ){.    
24006 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20  Index *pIdx;.   
24007 20 69 6e 74 20 69 4c 61 72 67 65 73 74 20 3d 20   int iLargest = 
24008 30 3b 0a 0a 20 20 20 20 69 66 28 20 69 44 65 73  0;..    if( iDes
24009 74 72 6f 79 65 64 3d 3d 30 20 7c 7c 20 69 54 61  troyed==0 || iTa
2400a 62 3c 69 44 65 73 74 72 6f 79 65 64 20 29 7b 0a  b<iDestroyed ){.
2400b 20 20 20 20 20 20 69 4c 61 72 67 65 73 74 20 3d        iLargest =
2400c 20 69 54 61 62 3b 0a 20 20 20 20 7d 0a 20 20 20   iTab;.    }.   
2400d 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e   for(pIdx=pTab->
2400e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49  pIndex; pIdx; pI
2400f 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b  dx=pIdx->pNext){
24010 0a 20 20 20 20 20 20 69 6e 74 20 69 49 64 78 20  .      int iIdx 
24011 3d 20 70 49 64 78 2d 3e 74 6e 75 6d 3b 0a 20 20  = pIdx->tnum;.  
24012 20 20 20 20 61 73 73 65 72 74 28 20 70 49 64 78      assert( pIdx
24013 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d  ->pSchema==pTab-
24014 3e 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20  >pSchema );.    
24015 20 20 69 66 28 20 28 69 44 65 73 74 72 6f 79 65    if( (iDestroye
24016 64 3d 3d 30 20 7c 7c 20 28 69 49 64 78 3c 69 44  d==0 || (iIdx<iD
24017 65 73 74 72 6f 79 65 64 29 29 20 26 26 20 69 49  estroyed)) && iI
24018 64 78 3e 69 4c 61 72 67 65 73 74 20 29 7b 0a 20  dx>iLargest ){. 
24019 20 20 20 20 20 20 20 69 4c 61 72 67 65 73 74 20         iLargest 
2401a 3d 20 69 49 64 78 3b 0a 20 20 20 20 20 20 7d 0a  = iIdx;.      }.
2401b 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 4c      }.    if( iL
2401c 61 72 67 65 73 74 3d 3d 30 20 29 7b 0a 20 20 20  argest==0 ){.   
2401d 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
2401e 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20  else{.      int 
2401f 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
24020 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73  emaToIndex(pPars
24021 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63  e->db, pTab->pSc
24022 68 65 6d 61 29 3b 0a 20 20 20 20 20 20 64 65 73  hema);.      des
24023 74 72 6f 79 52 6f 6f 74 50 61 67 65 28 70 50 61  troyRootPage(pPa
24024 72 73 65 2c 20 69 4c 61 72 67 65 73 74 2c 20 69  rse, iLargest, i
24025 44 62 29 3b 0a 20 20 20 20 20 20 69 44 65 73 74  Db);.      iDest
24026 72 6f 79 65 64 20 3d 20 69 4c 61 72 67 65 73 74  royed = iLargest
24027 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
24028 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  if.}../*.** This
24029 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
2402a 65 64 20 74 6f 20 64 6f 20 74 68 65 20 77 6f 72  ed to do the wor
2402b 6b 20 6f 66 20 61 20 44 52 4f 50 20 54 41 42 4c  k of a DROP TABL
2402c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20  E statement..** 
2402d 70 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d  pName is the nam
2402e 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74  e of the table t
2402f 6f 20 62 65 20 64 72 6f 70 70 65 64 2e 0a 2a 2f  o be dropped..*/
24030 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
24031 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70  void sqlite3Drop
24032 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70 50 61  Table(Parse *pPa
24033 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 4e  rse, SrcList *pN
24034 61 6d 65 2c 20 69 6e 74 20 69 73 56 69 65 77 2c  ame, int isView,
24035 20 69 6e 74 20 6e 6f 45 72 72 29 7b 0a 20 20 54   int noErr){.  T
24036 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 56 64  able *pTab;.  Vd
24037 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33  be *v;.  sqlite3
24038 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
24039 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 0a 20  b;.  int iDb;.. 
2403a 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
2403b 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74  ailed ){.    got
2403c 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c  o exit_drop_tabl
2403d 65 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  e;.  }.  assert(
2403e 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30   pParse->nErr==0
2403f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4e   );.  assert( pN
24040 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a  ame->nSrc==1 );.
24041 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33    pTab = sqlite3
24042 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 72  LocateTable(pPar
24043 73 65 2c 20 69 73 56 69 65 77 2c 20 0a 20 20 20  se, isView, .   
24044 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24045 20 20 20 20 20 20 20 20 20 70 4e 61 6d 65 2d 3e           pName->
24046 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70 4e 61 6d  a[0].zName, pNam
24047 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61 73  e->a[0].zDatabas
24048 65 29 3b 0a 0a 20 20 69 66 28 20 70 54 61 62 3d  e);..  if( pTab=
24049 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 6f  =0 ){.    if( no
2404a 45 72 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Err ){.      sql
2404b 69 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28 70  ite3ErrorClear(p
2404c 50 61 72 73 65 29 3b 0a 20 20 20 20 7d 0a 20 20  Parse);.    }.  
2404d 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70    goto exit_drop
2404e 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 69 44  _table;.  }.  iD
2404f 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
24050 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61  aToIndex(db, pTa
24051 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 61  b->pSchema);.  a
24052 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26  ssert( iDb>=0 &&
24053 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a   iDb<db->nDb );.
24054 0a 20 20 2f 2a 20 49 66 20 70 54 61 62 20 69 73  .  /* If pTab is
24055 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
24056 2c 20 63 61 6c 6c 20 56 69 65 77 47 65 74 43 6f  , call ViewGetCo
24057 6c 75 6d 6e 4e 61 6d 65 73 28 29 20 74 6f 20 65  lumnNames() to e
24058 6e 73 75 72 65 0a 20 20 2a 2a 20 69 74 20 69 73  nsure.  ** it is
24059 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 20 20   initialized..  
2405a 2a 2f 0a 20 20 69 66 28 20 49 73 56 69 72 74 75  */.  if( IsVirtu
2405b 61 6c 28 70 54 61 62 29 20 26 26 20 73 71 6c 69  al(pTab) && sqli
2405c 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e  te3ViewGetColumn
2405d 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70 54  Names(pParse, pT
2405e 61 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  ab) ){.    goto 
2405f 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b  exit_drop_table;
24060 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c  .  }.#ifndef SQL
24061 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49  ITE_OMIT_AUTHORI
24062 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20 20 69  ZATION.  {.    i
24063 6e 74 20 63 6f 64 65 3b 0a 20 20 20 20 63 6f 6e  nt code;.    con
24064 73 74 20 63 68 61 72 20 2a 7a 54 61 62 20 3d 20  st char *zTab = 
24065 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62  SCHEMA_TABLE(iDb
24066 29 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  );.    const cha
24067 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62  r *zDb = db->aDb
24068 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20  [iDb].zName;.   
24069 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 72   const char *zAr
2406a 67 32 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20  g2 = 0;.    if( 
2406b 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
2406c 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f  (pParse, SQLITE_
2406d 44 45 4c 45 54 45 2c 20 7a 54 61 62 2c 20 30 2c  DELETE, zTab, 0,
2406e 20 7a 44 62 29 29 7b 0a 20 20 20 20 20 20 67 6f   zDb)){.      go
2406f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62  to exit_drop_tab
24070 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  le;.    }.    if
24071 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20  ( isView ){.    
24072 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50    if( !OMIT_TEMP
24073 44 42 20 26 26 20 69 44 62 3d 3d 31 20 29 7b 0a  DB && iDb==1 ){.
24074 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53          code = S
24075 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f  QLITE_DROP_TEMP_
24076 56 49 45 57 3b 0a 20 20 20 20 20 20 7d 65 6c 73  VIEW;.      }els
24077 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20  e{.        code 
24078 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56 49  = SQLITE_DROP_VI
24079 45 57 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e  EW;.      }.#ifn
2407a 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2407b 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20  VIRTUALTABLE.   
2407c 20 7d 65 6c 73 65 20 69 66 28 20 49 73 56 69 72   }else if( IsVir
2407d 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20  tual(pTab) ){.  
2407e 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54      code = SQLIT
2407f 45 5f 44 52 4f 50 5f 56 54 41 42 4c 45 3b 0a 20  E_DROP_VTABLE;. 
24080 20 20 20 20 20 7a 41 72 67 32 20 3d 20 73 71 6c       zArg2 = sql
24081 69 74 65 33 47 65 74 56 54 61 62 6c 65 28 64 62  ite3GetVTable(db
24082 2c 20 70 54 61 62 29 2d 3e 70 4d 6f 64 2d 3e 7a  , pTab)->pMod->z
24083 4e 61 6d 65 3b 0a 23 65 6e 64 69 66 0a 20 20 20  Name;.#endif.   
24084 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66   }else{.      if
24085 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26  ( !OMIT_TEMPDB &
24086 26 20 69 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20  & iDb==1 ){.    
24087 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54      code = SQLIT
24088 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 41 42 4c  E_DROP_TEMP_TABL
24089 45 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  E;.      }else{.
2408a 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53          code = S
2408b 51 4c 49 54 45 5f 44 52 4f 50 5f 54 41 42 4c 45  QLITE_DROP_TABLE
2408c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2408d 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41      if( sqlite3A
2408e 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
2408f 20 63 6f 64 65 2c 20 70 54 61 62 2d 3e 7a 4e 61   code, pTab->zNa
24090 6d 65 2c 20 7a 41 72 67 32 2c 20 7a 44 62 29 20  me, zArg2, zDb) 
24091 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78  ){.      goto ex
24092 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20  it_drop_table;. 
24093 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c     }.    if( sql
24094 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
24095 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c  arse, SQLITE_DEL
24096 45 54 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ETE, pTab->zName
24097 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20  , 0, zDb) ){.   
24098 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f     goto exit_dro
24099 70 5f 74 61 62 6c 65 3b 0a 20 20 20 20 7d 0a 20  p_table;.    }. 
2409a 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20   }.#endif.  if( 
2409b 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
2409c 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71  pTab->zName, "sq
2409d 6c 69 74 65 5f 22 2c 20 37 29 3d 3d 30 20 29 7b  lite_", 7)==0 ){
2409e 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
2409f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 61  rMsg(pParse, "ta
240a0 62 6c 65 20 25 73 20 6d 61 79 20 6e 6f 74 20 62  ble %s may not b
240a1 65 20 64 72 6f 70 70 65 64 22 2c 20 70 54 61 62  e dropped", pTab
240a2 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f  ->zName);.    go
240a3 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62  to exit_drop_tab
240a4 6c 65 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66  le;.  }..#ifndef
240a5 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45   SQLITE_OMIT_VIE
240a6 57 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20 44 52  W.  /* Ensure DR
240a7 4f 50 20 54 41 42 4c 45 20 69 73 20 6e 6f 74 20  OP TABLE is not 
240a8 75 73 65 64 20 6f 6e 20 61 20 76 69 65 77 2c 20  used on a view, 
240a9 61 6e 64 20 44 52 4f 50 20 56 49 45 57 20 69 73  and DROP VIEW is
240aa 20 6e 6f 74 20 75 73 65 64 0a 20 20 2a 2a 20 6f   not used.  ** o
240ab 6e 20 61 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a  n a table..  */.
240ac 20 20 69 66 28 20 69 73 56 69 65 77 20 26 26 20    if( isView && 
240ad 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 3d 3d 30  pTab->pSelect==0
240ae 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
240af 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
240b0 22 75 73 65 20 44 52 4f 50 20 54 41 42 4c 45 20  "use DROP TABLE 
240b1 74 6f 20 64 65 6c 65 74 65 20 74 61 62 6c 65 20  to delete table 
240b2 25 73 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  %s", pTab->zName
240b3 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74  );.    goto exit
240b4 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d  _drop_table;.  }
240b5 0a 20 20 69 66 28 20 21 69 73 56 69 65 77 20 26  .  if( !isView &
240b6 26 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20  & pTab->pSelect 
240b7 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
240b8 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
240b9 75 73 65 20 44 52 4f 50 20 56 49 45 57 20 74 6f  use DROP VIEW to
240ba 20 64 65 6c 65 74 65 20 76 69 65 77 20 25 73 22   delete view %s"
240bb 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
240bc 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72      goto exit_dr
240bd 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 23 65  op_table;.  }.#e
240be 6e 64 69 66 0a 0a 20 20 2f 2a 20 47 65 6e 65 72  ndif..  /* Gener
240bf 61 74 65 20 63 6f 64 65 20 74 6f 20 72 65 6d 6f  ate code to remo
240c0 76 65 20 74 68 65 20 74 61 62 6c 65 20 66 72 6f  ve the table fro
240c1 6d 20 74 68 65 20 6d 61 73 74 65 72 20 74 61 62  m the master tab
240c2 6c 65 0a 20 20 2a 2a 20 6f 6e 20 64 69 73 6b 2e  le.  ** on disk.
240c3 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69  .  */.  v = sqli
240c4 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
240c5 65 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20  e);.  if( v ){. 
240c6 20 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69     Trigger *pTri
240c7 67 67 65 72 3b 0a 20 20 20 20 44 62 20 2a 70 44  gger;.    Db *pD
240c8 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62  b = &db->aDb[iDb
240c9 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65  ];.    sqlite3Be
240ca 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f  ginWriteOperatio
240cb 6e 28 70 50 61 72 73 65 2c 20 31 2c 20 69 44 62  n(pParse, 1, iDb
240cc 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  );..#ifndef SQLI
240cd 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
240ce 41 42 4c 45 0a 20 20 20 20 69 66 28 20 49 73 56  ABLE.    if( IsV
240cf 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a  irtual(pTab) ){.
240d0 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
240d1 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 56 42  eAddOp0(v, OP_VB
240d2 65 67 69 6e 29 3b 0a 20 20 20 20 7d 0a 23 65 6e  egin);.    }.#en
240d3 64 69 66 0a 0a 20 20 20 20 2f 2a 20 44 72 6f 70  dif..    /* Drop
240d4 20 61 6c 6c 20 74 72 69 67 67 65 72 73 20 61 73   all triggers as
240d5 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
240d6 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 64 72  e table being dr
240d7 6f 70 70 65 64 2e 20 43 6f 64 65 0a 20 20 20 20  opped. Code.    
240d8 2a 2a 20 69 73 20 67 65 6e 65 72 61 74 65 64 20  ** is generated 
240d9 74 6f 20 72 65 6d 6f 76 65 20 65 6e 74 72 69 65  to remove entrie
240da 73 20 66 72 6f 6d 20 73 71 6c 69 74 65 5f 6d 61  s from sqlite_ma
240db 73 74 65 72 20 61 6e 64 2f 6f 72 0a 20 20 20 20  ster and/or.    
240dc 2a 2a 20 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d  ** sqlite_temp_m
240dd 61 73 74 65 72 20 69 66 20 72 65 71 75 69 72 65  aster if require
240de 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 54  d..    */.    pT
240df 72 69 67 67 65 72 20 3d 20 73 71 6c 69 74 65 33  rigger = sqlite3
240e0 54 72 69 67 67 65 72 4c 69 73 74 28 70 50 61 72  TriggerList(pPar
240e1 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 77  se, pTab);.    w
240e2 68 69 6c 65 28 20 70 54 72 69 67 67 65 72 20 29  hile( pTrigger )
240e3 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
240e4 70 54 72 69 67 67 65 72 2d 3e 70 53 63 68 65 6d  pTrigger->pSchem
240e5 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  a==pTab->pSchema
240e6 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 20 20 70   || .          p
240e7 54 72 69 67 67 65 72 2d 3e 70 53 63 68 65 6d 61  Trigger->pSchema
240e8 3d 3d 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63  ==db->aDb[1].pSc
240e9 68 65 6d 61 20 29 3b 0a 20 20 20 20 20 20 73 71  hema );.      sq
240ea 6c 69 74 65 33 44 72 6f 70 54 72 69 67 67 65 72  lite3DropTrigger
240eb 50 74 72 28 70 50 61 72 73 65 2c 20 70 54 72 69  Ptr(pParse, pTri
240ec 67 67 65 72 29 3b 0a 20 20 20 20 20 20 70 54 72  gger);.      pTr
240ed 69 67 67 65 72 20 3d 20 70 54 72 69 67 67 65 72  igger = pTrigger
240ee 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 0a  ->pNext;.    }..
240ef 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
240f0 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e  MIT_AUTOINCREMEN
240f1 54 0a 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20  T.    /* Remove 
240f2 61 6e 79 20 65 6e 74 72 69 65 73 20 6f 66 20 74  any entries of t
240f3 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e  he sqlite_sequen
240f4 63 65 20 74 61 62 6c 65 20 61 73 73 6f 63 69 61  ce table associa
240f5 74 65 64 20 77 69 74 68 0a 20 20 20 20 2a 2a 20  ted with.    ** 
240f6 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20  the table being 
240f7 64 72 6f 70 70 65 64 2e 20 54 68 69 73 20 69 73  dropped. This is
240f8 20 64 6f 6e 65 20 62 65 66 6f 72 65 20 74 68 65   done before the
240f9 20 74 61 62 6c 65 20 69 73 20 64 72 6f 70 70 65   table is droppe
240fa 64 0a 20 20 20 20 2a 2a 20 61 74 20 74 68 65 20  d.    ** at the 
240fb 62 74 72 65 65 20 6c 65 76 65 6c 2c 20 69 6e 20  btree level, in 
240fc 63 61 73 65 20 74 68 65 20 73 71 6c 69 74 65 5f  case the sqlite_
240fd 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 6e  sequence table n
240fe 65 65 64 73 20 74 6f 0a 20 20 20 20 2a 2a 20 6d  eeds to.    ** m
240ff 6f 76 65 20 61 73 20 61 20 72 65 73 75 6c 74 20  ove as a result 
24100 6f 66 20 74 68 65 20 64 72 6f 70 20 28 63 61 6e  of the drop (can
24101 20 68 61 70 70 65 6e 20 69 6e 20 61 75 74 6f 2d   happen in auto-
24102 76 61 63 75 75 6d 20 6d 6f 64 65 29 2e 0a 20 20  vacuum mode)..  
24103 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 54 61    */.    if( pTa
24104 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46  b->tabFlags & TF
24105 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 29  _Autoincrement )
24106 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4e  {.      sqlite3N
24107 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73  estedParse(pPars
24108 65 2c 0a 20 20 20 20 20 20 20 20 22 44 45 4c 45  e,.        "DELE
24109 54 45 20 46 52 4f 4d 20 25 73 2e 73 71 6c 69 74  TE FROM %s.sqlit
2410a 65 5f 73 65 71 75 65 6e 63 65 20 57 48 45 52 45  e_sequence WHERE
2410b 20 6e 61 6d 65 3d 25 51 22 2c 0a 20 20 20 20 20   name=%Q",.     
2410c 20 20 20 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 70     pDb->zName, p
2410d 54 61 62 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 20  Tab->zName.     
2410e 20 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66   );.    }.#endif
2410f 0a 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 61 6c  ..    /* Drop al
24110 6c 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20  l SQLITE_MASTER 
24111 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 20  table and index 
24112 65 6e 74 72 69 65 73 20 74 68 61 74 20 72 65 66  entries that ref
24113 65 72 20 74 6f 20 74 68 65 0a 20 20 20 20 2a 2a  er to the.    **
24114 20 74 61 62 6c 65 2e 20 54 68 65 20 70 72 6f 67   table. The prog
24115 72 61 6d 20 6e 61 6d 65 20 6c 6f 6f 70 73 20 74  ram name loops t
24116 68 72 6f 75 67 68 20 74 68 65 20 6d 61 73 74 65  hrough the maste
24117 72 20 74 61 62 6c 65 20 61 6e 64 20 64 65 6c 65  r table and dele
24118 74 65 73 0a 20 20 20 20 2a 2a 20 65 76 65 72 79  tes.    ** every
24119 20 72 6f 77 20 74 68 61 74 20 72 65 66 65 72 73   row that refers
2411a 20 74 6f 20 61 20 74 61 62 6c 65 20 6f 66 20 74   to a table of t
2411b 68 65 20 73 61 6d 65 20 6e 61 6d 65 20 61 73 20  he same name as 
2411c 74 68 65 20 6f 6e 65 20 62 65 69 6e 67 0a 20 20  the one being.  
2411d 20 20 2a 2a 20 64 72 6f 70 70 65 64 2e 20 54 72    ** dropped. Tr
2411e 69 67 67 65 72 73 20 61 72 65 20 68 61 6e 64 6c  iggers are handl
2411f 65 64 20 73 65 70 65 72 61 74 65 6c 79 20 62 65  ed seperately be
24120 63 61 75 73 65 20 61 20 74 72 69 67 67 65 72 20  cause a trigger 
24121 63 61 6e 20 62 65 0a 20 20 20 20 2a 2a 20 63 72  can be.    ** cr
24122 65 61 74 65 64 20 69 6e 20 74 68 65 20 74 65 6d  eated in the tem
24123 70 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20  p database that 
24124 72 65 66 65 72 73 20 74 6f 20 61 20 74 61 62 6c  refers to a tabl
24125 65 20 69 6e 20 61 6e 6f 74 68 65 72 0a 20 20 20  e in another.   
24126 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 0a 20 20   ** database..  
24127 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33    */.    sqlite3
24128 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72  NestedParse(pPar
24129 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22 44 45  se, .        "DE
2412a 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e 25 73 20  LETE FROM %Q.%s 
2412b 57 48 45 52 45 20 74 62 6c 5f 6e 61 6d 65 3d 25  WHERE tbl_name=%
2412c 51 20 61 6e 64 20 74 79 70 65 21 3d 27 74 72 69  Q and type!='tri
2412d 67 67 65 72 27 22 2c 0a 20 20 20 20 20 20 20 20  gger'",.        
2412e 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 53 43 48 45  pDb->zName, SCHE
2412f 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 70  MA_TABLE(iDb), p
24130 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20  Tab->zName);..  
24131 20 20 2f 2a 20 44 72 6f 70 20 61 6e 79 20 73 74    /* Drop any st
24132 61 74 69 73 74 69 63 73 20 66 72 6f 6d 20 74 68  atistics from th
24133 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74  e sqlite_stat1 t
24134 61 62 6c 65 2c 20 69 66 20 69 74 20 65 78 69 73  able, if it exis
24135 74 73 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 71  ts */.    if( sq
24136 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64  lite3FindTable(d
24137 62 2c 20 22 73 71 6c 69 74 65 5f 73 74 61 74 31  b, "sqlite_stat1
24138 22 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  ", db->aDb[iDb].
24139 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 20  zName) ){.      
2413a 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72  sqlite3NestedPar
2413b 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20  se(pParse,.     
2413c 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20     "DELETE FROM 
2413d 25 51 2e 73 71 6c 69 74 65 5f 73 74 61 74 31 20  %Q.sqlite_stat1 
2413e 57 48 45 52 45 20 74 62 6c 3d 25 51 22 2c 20 70  WHERE tbl=%Q", p
2413f 44 62 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d  Db->zName, pTab-
24140 3e 7a 4e 61 6d 65 0a 20 20 20 20 20 20 29 3b 0a  >zName.      );.
24141 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 21      }..    if( !
24142 69 73 56 69 65 77 20 26 26 20 21 49 73 56 69 72  isView && !IsVir
24143 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20  tual(pTab) ){.  
24144 20 20 20 20 64 65 73 74 72 6f 79 54 61 62 6c 65      destroyTable
24145 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a  (pParse, pTab);.
24146 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65      }..    /* Re
24147 6d 6f 76 65 20 74 68 65 20 74 61 62 6c 65 20 65  move the table e
24148 6e 74 72 79 20 66 72 6f 6d 20 53 51 4c 69 74 65  ntry from SQLite
24149 27 73 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65  's internal sche
2414a 6d 61 20 61 6e 64 20 6d 6f 64 69 66 79 0a 20 20  ma and modify.  
2414b 20 20 2a 2a 20 74 68 65 20 73 63 68 65 6d 61 20    ** the schema 
2414c 63 6f 6f 6b 69 65 2e 0a 20 20 20 20 2a 2f 0a 20  cookie..    */. 
2414d 20 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c     if( IsVirtual
2414e 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20  (pTab) ){.      
2414f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
24150 34 28 76 2c 20 4f 50 5f 56 44 65 73 74 72 6f 79  4(v, OP_VDestroy
24151 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20 70 54 61  , iDb, 0, 0, pTa
24152 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20  b->zName, 0);.  
24153 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
24154 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
24155 44 72 6f 70 54 61 62 6c 65 2c 20 69 44 62 2c 20  DropTable, iDb, 
24156 30 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  0, 0, pTab->zNam
24157 65 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  e, 0);.    sqlit
24158 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70  e3ChangeCookie(p
24159 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 7d  Parse, iDb);.  }
2415a 0a 20 20 73 71 6c 69 74 65 56 69 65 77 52 65 73  .  sqliteViewRes
2415b 65 74 41 6c 6c 28 64 62 2c 20 69 44 62 29 3b 0a  etAll(db, iDb);.
2415c 0a 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65  .exit_drop_table
2415d 3a 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69  :.  sqlite3SrcLi
2415e 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4e 61  stDelete(db, pNa
2415f 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  me);.}../*.** Th
24160 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
24161 6c 6c 65 64 20 74 6f 20 63 72 65 61 74 65 20 61  lled to create a
24162 20 6e 65 77 20 66 6f 72 65 69 67 6e 20 6b 65 79   new foreign key
24163 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a   on the table.**
24164 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72   currently under
24165 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20   construction.  
24166 70 46 72 6f 6d 43 6f 6c 20 64 65 74 65 72 6d 69  pFromCol determi
24167 6e 65 73 20 77 68 69 63 68 20 63 6f 6c 75 6d 6e  nes which column
24168 73 0a 2a 2a 20 69 6e 20 74 68 65 20 63 75 72 72  s.** in the curr
24169 65 6e 74 20 74 61 62 6c 65 20 70 6f 69 6e 74 20  ent table point 
2416a 74 6f 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b  to the foreign k
2416b 65 79 2e 20 20 49 66 20 70 46 72 6f 6d 43 6f 6c  ey.  If pFromCol
2416c 3d 3d 30 20 74 68 65 6e 0a 2a 2a 20 63 6f 6e 6e  ==0 then.** conn
2416d 65 63 74 20 74 68 65 20 6b 65 79 20 74 6f 20 74  ect the key to t
2416e 68 65 20 6c 61 73 74 20 63 6f 6c 75 6d 6e 20 69  he last column i
2416f 6e 73 65 72 74 65 64 2e 20 20 70 54 6f 20 69 73  nserted.  pTo is
24170 20 74 68 65 20 6e 61 6d 65 20 6f 66 0a 2a 2a 20   the name of.** 
24171 74 68 65 20 74 61 62 6c 65 20 72 65 66 65 72 72  the table referr
24172 65 64 20 74 6f 2e 20 20 70 54 6f 43 6f 6c 20 69  ed to.  pToCol i
24173 73 20 61 20 6c 69 73 74 20 6f 66 20 74 61 62 6c  s a list of tabl
24174 65 73 20 69 6e 20 74 68 65 20 6f 74 68 65 72 0a  es in the other.
24175 2a 2a 20 70 54 6f 20 74 61 62 6c 65 20 74 68 61  ** pTo table tha
24176 74 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65  t the foreign ke
24177 79 20 70 6f 69 6e 74 73 20 74 6f 2e 20 20 66 6c  y points to.  fl
24178 61 67 73 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c  ags contains all
24179 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  .** information 
2417a 61 62 6f 75 74 20 74 68 65 20 63 6f 6e 66 6c 69  about the confli
2417b 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 61 6c  ct resolution al
2417c 67 6f 72 69 74 68 6d 73 20 73 70 65 63 69 66 69  gorithms specifi
2417d 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 4f 4e 20  ed.** in the ON 
2417e 44 45 4c 45 54 45 2c 20 4f 4e 20 55 50 44 41 54  DELETE, ON UPDAT
2417f 45 20 61 6e 64 20 4f 4e 20 49 4e 53 45 52 54 20  E and ON INSERT 
24180 63 6c 61 75 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 41  clauses..**.** A
24181 6e 20 46 4b 65 79 20 73 74 72 75 63 74 75 72 65  n FKey structure
24182 20 69 73 20 63 72 65 61 74 65 64 20 61 6e 64 20   is created and 
24183 61 64 64 65 64 20 74 6f 20 74 68 65 20 74 61 62  added to the tab
24184 6c 65 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20  le currently.** 
24185 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69  under constructi
24186 6f 6e 20 69 6e 20 74 68 65 20 70 50 61 72 73 65  on in the pParse
24187 2d 3e 70 4e 65 77 54 61 62 6c 65 20 66 69 65 6c  ->pNewTable fiel
24188 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 72  d..**.** The for
24189 65 69 67 6e 20 6b 65 79 20 69 73 20 73 65 74 20  eign key is set 
2418a 66 6f 72 20 49 4d 4d 45 44 49 41 54 45 20 70 72  for IMMEDIATE pr
2418b 6f 63 65 73 73 69 6e 67 2e 20 20 41 20 73 75 62  ocessing.  A sub
2418c 73 65 71 75 65 6e 74 20 63 61 6c 6c 0a 2a 2a 20  sequent call.** 
2418d 74 6f 20 73 71 6c 69 74 65 33 44 65 66 65 72 46  to sqlite3DeferF
2418e 6f 72 65 69 67 6e 4b 65 79 28 29 20 6d 69 67 68  oreignKey() migh
2418f 74 20 63 68 61 6e 67 65 20 74 68 69 73 20 74 6f  t change this to
24190 20 44 45 46 45 52 52 45 44 2e 0a 2a 2f 0a 53 51   DEFERRED..*/.SQ
24191 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
24192 64 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46  d sqlite3CreateF
24193 6f 72 65 69 67 6e 4b 65 79 28 0a 20 20 50 61 72  oreignKey(.  Par
24194 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
24195 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
24196 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69  text */.  ExprLi
24197 73 74 20 2a 70 46 72 6f 6d 43 6f 6c 2c 20 20 2f  st *pFromCol,  /
24198 2a 20 43 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 69  * Columns in thi
24199 73 20 74 61 62 6c 65 20 74 68 61 74 20 70 6f 69  s table that poi
2419a 6e 74 20 74 6f 20 6f 74 68 65 72 20 74 61 62 6c  nt to other tabl
2419b 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 54  e */.  Token *pT
2419c 6f 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  o,          /* N
2419d 61 6d 65 20 6f 66 20 74 68 65 20 6f 74 68 65 72  ame of the other
2419e 20 74 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72   table */.  Expr
2419f 4c 69 73 74 20 2a 70 54 6f 43 6f 6c 2c 20 20 20  List *pToCol,   
241a0 20 2f 2a 20 43 6f 6c 75 6d 6e 73 20 69 6e 20 74   /* Columns in t
241a1 68 65 20 6f 74 68 65 72 20 74 61 62 6c 65 20 2a  he other table *
241a2 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 20 20 20  /.  int flags   
241a3 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66           /* Conf
241a4 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20  lict resolution 
241a5 61 6c 67 6f 72 69 74 68 6d 73 2e 20 2a 2f 0a 29  algorithms. */.)
241a6 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  {.  sqlite3 *db 
241a7 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 23 69  = pParse->db;.#i
241a8 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
241a9 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20  T_FOREIGN_KEY.  
241aa 46 4b 65 79 20 2a 70 46 4b 65 79 20 3d 20 30 3b  FKey *pFKey = 0;
241ab 0a 20 20 54 61 62 6c 65 20 2a 70 20 3d 20 70 50  .  Table *p = pP
241ac 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b  arse->pNewTable;
241ad 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20  .  int nByte;.  
241ae 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 43 6f  int i;.  int nCo
241af 6c 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 0a 20  l;.  char *z;.. 
241b0 20 61 73 73 65 72 74 28 20 70 54 6f 21 3d 30 20   assert( pTo!=0 
241b1 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c  );.  if( p==0 ||
241b2 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42   IN_DECLARE_VTAB
241b3 20 29 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a   ) goto fk_end;.
241b4 20 20 69 66 28 20 70 46 72 6f 6d 43 6f 6c 3d 3d    if( pFromCol==
241b5 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 43 6f  0 ){.    int iCo
241b6 6c 20 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20  l = p->nCol-1;. 
241b7 20 20 20 69 66 28 20 4e 45 56 45 52 28 69 43 6f     if( NEVER(iCo
241b8 6c 3c 30 29 20 29 20 67 6f 74 6f 20 66 6b 5f 65  l<0) ) goto fk_e
241b9 6e 64 3b 0a 20 20 20 20 69 66 28 20 70 54 6f 43  nd;.    if( pToC
241ba 6f 6c 20 26 26 20 70 54 6f 43 6f 6c 2d 3e 6e 45  ol && pToCol->nE
241bb 78 70 72 21 3d 31 20 29 7b 0a 20 20 20 20 20 20  xpr!=1 ){.      
241bc 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
241bd 70 50 61 72 73 65 2c 20 22 66 6f 72 65 69 67 6e  pParse, "foreign
241be 20 6b 65 79 20 6f 6e 20 25 73 22 0a 20 20 20 20   key on %s".    
241bf 20 20 20 20 20 22 20 73 68 6f 75 6c 64 20 72 65       " should re
241c0 66 65 72 65 6e 63 65 20 6f 6e 6c 79 20 6f 6e 65  ference only one
241c1 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65   column of table
241c2 20 25 54 22 2c 0a 20 20 20 20 20 20 20 20 20 70   %T",.         p
241c3 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61  ->aCol[iCol].zNa
241c4 6d 65 2c 20 70 54 6f 29 3b 0a 20 20 20 20 20 20  me, pTo);.      
241c5 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 20  goto fk_end;.   
241c6 20 7d 0a 20 20 20 20 6e 43 6f 6c 20 3d 20 31 3b   }.    nCol = 1;
241c7 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 54 6f  .  }else if( pTo
241c8 43 6f 6c 20 26 26 20 70 54 6f 43 6f 6c 2d 3e 6e  Col && pToCol->n
241c9 45 78 70 72 21 3d 70 46 72 6f 6d 43 6f 6c 2d 3e  Expr!=pFromCol->
241ca 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 73 71 6c  nExpr ){.    sql
241cb 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
241cc 72 73 65 2c 0a 20 20 20 20 20 20 20 20 22 6e 75  rse,.        "nu
241cd 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
241ce 69 6e 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64  in foreign key d
241cf 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 74 68  oes not match th
241d0 65 20 6e 75 6d 62 65 72 20 6f 66 20 22 0a 20 20  e number of ".  
241d1 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e 73 20 69        "columns i
241d2 6e 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 64  n the referenced
241d3 20 74 61 62 6c 65 22 29 3b 0a 20 20 20 20 67 6f   table");.    go
241d4 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 7d 65 6c  to fk_end;.  }el
241d5 73 65 7b 0a 20 20 20 20 6e 43 6f 6c 20 3d 20 70  se{.    nCol = p
241d6 46 72 6f 6d 43 6f 6c 2d 3e 6e 45 78 70 72 3b 0a  FromCol->nExpr;.
241d7 20 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20 73 69    }.  nByte = si
241d8 7a 65 6f 66 28 2a 70 46 4b 65 79 29 20 2b 20 28  zeof(*pFKey) + (
241d9 6e 43 6f 6c 2d 31 29 2a 73 69 7a 65 6f 66 28 70  nCol-1)*sizeof(p
241da 46 4b 65 79 2d 3e 61 43 6f 6c 5b 30 5d 29 20 2b  FKey->aCol[0]) +
241db 20 70 54 6f 2d 3e 6e 20 2b 20 31 3b 0a 20 20 69   pTo->n + 1;.  i
241dc 66 28 20 70 54 6f 43 6f 6c 20 29 7b 0a 20 20 20  f( pToCol ){.   
241dd 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 6f 43   for(i=0; i<pToC
241de 6f 6c 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b  ol->nExpr; i++){
241df 0a 20 20 20 20 20 20 6e 42 79 74 65 20 2b 3d 20  .      nByte += 
241e0 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
241e1 70 54 6f 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61  pToCol->a[i].zNa
241e2 6d 65 29 20 2b 20 31 3b 0a 20 20 20 20 7d 0a 20  me) + 1;.    }. 
241e3 20 7d 0a 20 20 70 46 4b 65 79 20 3d 20 73 71 6c   }.  pFKey = sql
241e4 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
241e5 28 64 62 2c 20 6e 42 79 74 65 20 29 3b 0a 20 20  (db, nByte );.  
241e6 69 66 28 20 70 46 4b 65 79 3d 3d 30 20 29 7b 0a  if( pFKey==0 ){.
241e7 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b      goto fk_end;
241e8 0a 20 20 7d 0a 20 20 70 46 4b 65 79 2d 3e 70 46  .  }.  pFKey->pF
241e9 72 6f 6d 20 3d 20 70 3b 0a 20 20 70 46 4b 65 79  rom = p;.  pFKey
241ea 2d 3e 70 4e 65 78 74 46 72 6f 6d 20 3d 20 70 2d  ->pNextFrom = p-
241eb 3e 70 46 4b 65 79 3b 0a 20 20 7a 20 3d 20 28 63  >pFKey;.  z = (c
241ec 68 61 72 2a 29 26 70 46 4b 65 79 2d 3e 61 43 6f  har*)&pFKey->aCo
241ed 6c 5b 6e 43 6f 6c 5d 3b 0a 20 20 70 46 4b 65 79  l[nCol];.  pFKey
241ee 2d 3e 7a 54 6f 20 3d 20 7a 3b 0a 20 20 6d 65 6d  ->zTo = z;.  mem
241ef 63 70 79 28 7a 2c 20 70 54 6f 2d 3e 7a 2c 20 70  cpy(z, pTo->z, p
241f0 54 6f 2d 3e 6e 29 3b 0a 20 20 7a 5b 70 54 6f 2d  To->n);.  z[pTo-
241f1 3e 6e 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74  >n] = 0;.  sqlit
241f2 65 33 44 65 71 75 6f 74 65 28 7a 29 3b 0a 20 20  e3Dequote(z);.  
241f3 7a 20 2b 3d 20 70 54 6f 2d 3e 6e 2b 31 3b 0a 20  z += pTo->n+1;. 
241f4 20 70 46 4b 65 79 2d 3e 6e 43 6f 6c 20 3d 20 6e   pFKey->nCol = n
241f5 43 6f 6c 3b 0a 20 20 69 66 28 20 70 46 72 6f 6d  Col;.  if( pFrom
241f6 43 6f 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 70 46  Col==0 ){.    pF
241f7 4b 65 79 2d 3e 61 43 6f 6c 5b 30 5d 2e 69 46 72  Key->aCol[0].iFr
241f8 6f 6d 20 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a  om = p->nCol-1;.
241f9 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6f 72    }else{.    for
241fa 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b  (i=0; i<nCol; i+
241fb 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b  +){.      int j;
241fc 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20  .      for(j=0; 
241fd 6a 3c 70 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b  j<p->nCol; j++){
241fe 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c  .        if( sql
241ff 69 74 65 33 53 74 72 49 43 6d 70 28 70 2d 3e 61  ite3StrICmp(p->a
24200 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c 20 70 46  Col[j].zName, pF
24201 72 6f 6d 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61  romCol->a[i].zNa
24202 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  me)==0 ){.      
24203 20 20 20 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b      pFKey->aCol[
24204 69 5d 2e 69 46 72 6f 6d 20 3d 20 6a 3b 0a 20 20  i].iFrom = j;.  
24205 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
24206 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
24207 0a 20 20 20 20 20 20 69 66 28 20 6a 3e 3d 70 2d  .      if( j>=p-
24208 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20  >nCol ){.       
24209 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2420a 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20  (pParse, .      
2420b 20 20 20 20 22 75 6e 6b 6e 6f 77 6e 20 63 6f 6c      "unknown col
2420c 75 6d 6e 20 5c 22 25 73 5c 22 20 69 6e 20 66 6f  umn \"%s\" in fo
2420d 72 65 69 67 6e 20 6b 65 79 20 64 65 66 69 6e 69  reign key defini
2420e 74 69 6f 6e 22 2c 20 0a 20 20 20 20 20 20 20 20  tion", .        
2420f 20 20 70 46 72 6f 6d 43 6f 6c 2d 3e 61 5b 69 5d    pFromCol->a[i]
24210 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20  .zName);.       
24211 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20   goto fk_end;.  
24212 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
24213 20 20 69 66 28 20 70 54 6f 43 6f 6c 20 29 7b 0a    if( pToCol ){.
24214 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
24215 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Col; i++){.     
24216 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33   int n = sqlite3
24217 53 74 72 6c 65 6e 33 30 28 70 54 6f 43 6f 6c 2d  Strlen30(pToCol-
24218 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20  >a[i].zName);.  
24219 20 20 20 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b      pFKey->aCol[
2421a 69 5d 2e 7a 43 6f 6c 20 3d 20 7a 3b 0a 20 20 20  i].zCol = z;.   
2421b 20 20 20 6d 65 6d 63 70 79 28 7a 2c 20 70 54 6f     memcpy(z, pTo
2421c 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 2c  Col->a[i].zName,
2421d 20 6e 29 3b 0a 20 20 20 20 20 20 7a 5b 6e 5d 20   n);.      z[n] 
2421e 3d 20 30 3b 0a 20 20 20 20 20 20 7a 20 2b 3d 20  = 0;.      z += 
2421f 6e 2b 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  n+1;.    }.  }. 
24220 20 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72 72   pFKey->isDeferr
24221 65 64 20 3d 20 30 3b 0a 20 20 70 46 4b 65 79 2d  ed = 0;.  pFKey-
24222 3e 64 65 6c 65 74 65 43 6f 6e 66 20 3d 20 28 75  >deleteConf = (u
24223 38 29 28 66 6c 61 67 73 20 26 20 30 78 66 66 29  8)(flags & 0xff)
24224 3b 0a 20 20 70 46 4b 65 79 2d 3e 75 70 64 61 74  ;.  pFKey->updat
24225 65 43 6f 6e 66 20 3d 20 28 75 38 29 28 28 66 6c  eConf = (u8)((fl
24226 61 67 73 20 3e 3e 20 38 20 29 20 26 20 30 78 66  ags >> 8 ) & 0xf
24227 66 29 3b 0a 20 20 70 46 4b 65 79 2d 3e 69 6e 73  f);.  pFKey->ins
24228 65 72 74 43 6f 6e 66 20 3d 20 28 75 38 29 28 28  ertConf = (u8)((
24229 66 6c 61 67 73 20 3e 3e 20 31 36 20 29 20 26 20  flags >> 16 ) & 
2422a 30 78 66 66 29 3b 0a 0a 20 20 2f 2a 20 4c 69 6e  0xff);..  /* Lin
2422b 6b 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65  k the foreign ke
2422c 79 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 61  y to the table a
2422d 73 20 74 68 65 20 6c 61 73 74 20 73 74 65 70 2e  s the last step.
2422e 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70 46 4b 65 79  .  */.  p->pFKey
2422f 20 3d 20 70 46 4b 65 79 3b 0a 20 20 70 46 4b 65   = pFKey;.  pFKe
24230 79 20 3d 20 30 3b 0a 0a 66 6b 5f 65 6e 64 3a 0a  y = 0;..fk_end:.
24231 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
24232 64 62 2c 20 70 46 4b 65 79 29 3b 0a 23 65 6e 64  db, pFKey);.#end
24233 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53  if /* !defined(S
24234 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49  QLITE_OMIT_FOREI
24235 47 4e 5f 4b 45 59 29 20 2a 2f 0a 20 20 73 71 6c  GN_KEY) */.  sql
24236 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
24237 74 65 28 64 62 2c 20 70 46 72 6f 6d 43 6f 6c 29  te(db, pFromCol)
24238 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c  ;.  sqlite3ExprL
24239 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 54  istDelete(db, pT
2423a 6f 43 6f 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  oCol);.}../*.** 
2423b 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
2423c 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e 20 49  called when an I
2423d 4e 49 54 49 41 4c 4c 59 20 49 4d 4d 45 44 49 41  NITIALLY IMMEDIA
2423e 54 45 20 6f 72 20 49 4e 49 54 49 41 4c 4c 59 20  TE or INITIALLY 
2423f 44 45 46 45 52 52 45 44 0a 2a 2a 20 63 6c 61 75  DEFERRED.** clau
24240 73 65 20 69 73 20 73 65 65 6e 20 61 73 20 70 61  se is seen as pa
24241 72 74 20 6f 66 20 61 20 66 6f 72 65 69 67 6e 20  rt of a foreign 
24242 6b 65 79 20 64 65 66 69 6e 69 74 69 6f 6e 2e 20  key definition. 
24243 20 54 68 65 20 69 73 44 65 66 65 72 72 65 64 0a   The isDeferred.
24244 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  ** parameter is 
24245 31 20 66 6f 72 20 49 4e 49 54 49 41 4c 4c 59 20  1 for INITIALLY 
24246 44 45 46 45 52 52 45 44 20 61 6e 64 20 30 20 66  DEFERRED and 0 f
24247 6f 72 20 49 4e 49 54 49 41 4c 4c 59 20 49 4d 4d  or INITIALLY IMM
24248 45 44 49 41 54 45 2e 0a 2a 2a 20 54 68 65 20 62  EDIATE..** The b
24249 65 68 61 76 69 6f 72 20 6f 66 20 74 68 65 20 6d  ehavior of the m
2424a 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 63 72 65  ost recently cre
2424b 61 74 65 64 20 66 6f 72 65 69 67 6e 20 6b 65 79  ated foreign key
2424c 20 69 73 20 61 64 6a 75 73 74 65 64 0a 2a 2a 20   is adjusted.** 
2424d 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2f 0a  accordingly..*/.
2424e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2424f 6f 69 64 20 73 71 6c 69 74 65 33 44 65 66 65 72  oid sqlite3Defer
24250 46 6f 72 65 69 67 6e 4b 65 79 28 50 61 72 73 65  ForeignKey(Parse
24251 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 73   *pParse, int is
24252 44 65 66 65 72 72 65 64 29 7b 0a 23 69 66 6e 64  Deferred){.#ifnd
24253 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
24254 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 54 61 62  OREIGN_KEY.  Tab
24255 6c 65 20 2a 70 54 61 62 3b 0a 20 20 46 4b 65 79  le *pTab;.  FKey
24256 20 2a 70 46 4b 65 79 3b 0a 20 20 69 66 28 20 28   *pFKey;.  if( (
24257 70 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70  pTab = pParse->p
24258 4e 65 77 54 61 62 6c 65 29 3d 3d 30 20 7c 7c 20  NewTable)==0 || 
24259 28 70 46 4b 65 79 20 3d 20 70 54 61 62 2d 3e 70  (pFKey = pTab->p
2425a 46 4b 65 79 29 3d 3d 30 20 29 20 72 65 74 75 72  FKey)==0 ) retur
2425b 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 44  n;.  assert( isD
2425c 65 66 65 72 72 65 64 3d 3d 30 20 7c 7c 20 69 73  eferred==0 || is
2425d 44 65 66 65 72 72 65 64 3d 3d 31 20 29 3b 0a 20  Deferred==1 );. 
2425e 20 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72 72   pFKey->isDeferr
2425f 65 64 20 3d 20 28 75 38 29 69 73 44 65 66 65 72  ed = (u8)isDefer
24260 72 65 64 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f  red;.#endif.}../
24261 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
24262 64 65 20 74 68 61 74 20 77 69 6c 6c 20 65 72 61  de that will era
24263 73 65 20 61 6e 64 20 72 65 66 69 6c 6c 20 69 6e  se and refill in
24264 64 65 78 20 2a 70 49 64 78 2e 20 20 54 68 69 73  dex *pIdx.  This
24265 20 69 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69   is.** used to i
24266 6e 69 74 69 61 6c 69 7a 65 20 61 20 6e 65 77 6c  nitialize a newl
24267 79 20 63 72 65 61 74 65 64 20 69 6e 64 65 78 20  y created index 
24268 6f 72 20 74 6f 20 72 65 63 6f 6d 70 75 74 65 20  or to recompute 
24269 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6f  the.** content o
2426a 66 20 61 6e 20 69 6e 64 65 78 20 69 6e 20 72 65  f an index in re
2426b 73 70 6f 6e 73 65 20 74 6f 20 61 20 52 45 49 4e  sponse to a REIN
2426c 44 45 58 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a  DEX command..**.
2426d 2a 2a 20 69 66 20 6d 65 6d 52 6f 6f 74 50 61 67  ** if memRootPag
2426e 65 20 69 73 20 6e 6f 74 20 6e 65 67 61 74 69 76  e is not negativ
2426f 65 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74  e, it means that
24270 20 74 68 65 20 69 6e 64 65 78 20 69 73 20 6e 65   the index is ne
24271 77 6c 79 0a 2a 2a 20 63 72 65 61 74 65 64 2e 20  wly.** created. 
24272 20 54 68 65 20 72 65 67 69 73 74 65 72 20 73 70   The register sp
24273 65 63 69 66 69 65 64 20 62 79 20 6d 65 6d 52 6f  ecified by memRo
24274 6f 74 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 20  otPage contains 
24275 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65  the.** root page
24276 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 69   number of the i
24277 6e 64 65 78 2e 20 20 49 66 20 6d 65 6d 52 6f 6f  ndex.  If memRoo
24278 74 50 61 67 65 20 69 73 20 6e 65 67 61 74 69 76  tPage is negativ
24279 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 69  e, then.** the i
2427a 6e 64 65 78 20 61 6c 72 65 61 64 79 20 65 78 69  ndex already exi
2427b 73 74 73 20 61 6e 64 20 6d 75 73 74 20 62 65 20  sts and must be 
2427c 63 6c 65 61 72 65 64 20 62 65 66 6f 72 65 20 62  cleared before b
2427d 65 69 6e 67 20 72 65 66 69 6c 6c 65 64 20 61 6e  eing refilled an
2427e 64 0a 2a 2a 20 74 68 65 20 72 6f 6f 74 20 70 61  d.** the root pa
2427f 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  ge number of the
24280 20 69 6e 64 65 78 20 69 73 20 74 61 6b 65 6e 20   index is taken 
24281 66 72 6f 6d 20 70 49 6e 64 65 78 2d 3e 74 6e 75  from pIndex->tnu
24282 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  m..*/.static voi
24283 64 20 73 71 6c 69 74 65 33 52 65 66 69 6c 6c 49  d sqlite3RefillI
24284 6e 64 65 78 28 50 61 72 73 65 20 2a 70 50 61 72  ndex(Parse *pPar
24285 73 65 2c 20 49 6e 64 65 78 20 2a 70 49 6e 64 65  se, Index *pInde
24286 78 2c 20 69 6e 74 20 6d 65 6d 52 6f 6f 74 50 61  x, int memRootPa
24287 67 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54  ge){.  Table *pT
24288 61 62 20 3d 20 70 49 6e 64 65 78 2d 3e 70 54 61  ab = pIndex->pTa
24289 62 6c 65 3b 20 20 2f 2a 20 54 68 65 20 74 61 62  ble;  /* The tab
2428a 6c 65 20 74 68 61 74 20 69 73 20 69 6e 64 65 78  le that is index
2428b 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62  ed */.  int iTab
2428c 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b   = pParse->nTab+
2428d 2b 3b 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20  +;     /* Btree 
2428e 63 75 72 73 6f 72 20 75 73 65 64 20 66 6f 72 20  cursor used for 
2428f 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20 69 49  pTab */.  int iI
24290 64 78 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  dx = pParse->nTa
24291 62 2b 2b 3b 20 20 20 20 20 2f 2a 20 42 74 72 65  b++;     /* Btre
24292 65 20 63 75 72 73 6f 72 20 75 73 65 64 20 66 6f  e cursor used fo
24293 72 20 70 49 6e 64 65 78 20 2a 2f 0a 20 20 69 6e  r pIndex */.  in
24294 74 20 61 64 64 72 31 3b 20 20 20 20 20 20 20 20  t addr1;        
24295 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
24296 41 64 64 72 65 73 73 20 6f 66 20 74 6f 70 20 6f  Address of top o
24297 66 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20  f loop */.  int 
24298 74 6e 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20  tnum;           
24299 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f             /* Ro
2429a 6f 74 20 70 61 67 65 20 6f 66 20 69 6e 64 65 78  ot page of index
2429b 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20   */.  Vdbe *v;  
2429c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2429d 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65       /* Generate
2429e 20 63 6f 64 65 20 69 6e 74 6f 20 74 68 69 73 20   code into this 
2429f 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20  virtual machine 
242a0 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b  */.  KeyInfo *pK
242a1 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ey;             
242a2 20 20 20 20 2f 2a 20 4b 65 79 49 6e 66 6f 20 66      /* KeyInfo f
242a3 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e  or index */.  in
242a4 74 20 72 65 67 49 64 78 4b 65 79 3b 20 20 20 20  t regIdxKey;    
242a5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
242a6 52 65 67 69 73 74 65 72 73 20 63 6f 6e 74 61 69  Registers contai
242a7 6e 69 6e 67 20 74 68 65 20 69 6e 64 65 78 20 6b  ning the index k
242a8 65 79 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52  ey */.  int regR
242a9 65 63 6f 72 64 3b 20 20 20 20 20 20 20 20 20 20  ecord;          
242aa 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
242ab 65 72 20 68 6f 6c 64 69 6e 67 20 61 73 73 65 6d  er holding assem
242ac 62 6c 69 65 64 20 69 6e 64 65 78 20 72 65 63 6f  blied index reco
242ad 72 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  rd */.  sqlite3 
242ae 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
242af 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61  ;      /* The da
242b0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
242b1 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 20 3d  n */.  int iDb =
242b2 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
242b3 49 6e 64 65 78 28 64 62 2c 20 70 49 6e 64 65 78  Index(db, pIndex
242b4 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 23 69 66  ->pSchema);..#if
242b5 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
242b6 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20  _AUTHORIZATION. 
242b7 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68   if( sqlite3Auth
242b8 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51  Check(pParse, SQ
242b9 4c 49 54 45 5f 52 45 49 4e 44 45 58 2c 20 70 49  LITE_REINDEX, pI
242ba 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 0a  ndex->zName, 0,.
242bb 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44        db->aDb[iD
242bc 62 5d 2e 7a 4e 61 6d 65 20 29 20 29 7b 0a 20 20  b].zName ) ){.  
242bd 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 23 65    return;.  }.#e
242be 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 71 75 69  ndif..  /* Requi
242bf 72 65 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20  re a write-lock 
242c0 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20  on the table to 
242c1 70 65 72 66 6f 72 6d 20 74 68 69 73 20 6f 70 65  perform this ope
242c2 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69  ration */.  sqli
242c3 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61  te3TableLock(pPa
242c4 72 73 65 2c 20 69 44 62 2c 20 70 54 61 62 2d 3e  rse, iDb, pTab->
242c5 74 6e 75 6d 2c 20 31 2c 20 70 54 61 62 2d 3e 7a  tnum, 1, pTab->z
242c6 4e 61 6d 65 29 3b 0a 0a 20 20 76 20 3d 20 73 71  Name);..  v = sq
242c7 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
242c8 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30  rse);.  if( v==0
242c9 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28   ) return;.  if(
242ca 20 6d 65 6d 52 6f 6f 74 50 61 67 65 3e 3d 30 20   memRootPage>=0 
242cb 29 7b 0a 20 20 20 20 74 6e 75 6d 20 3d 20 6d 65  ){.    tnum = me
242cc 6d 52 6f 6f 74 50 61 67 65 3b 0a 20 20 7d 65 6c  mRootPage;.  }el
242cd 73 65 7b 0a 20 20 20 20 74 6e 75 6d 20 3d 20 70  se{.    tnum = p
242ce 49 6e 64 65 78 2d 3e 74 6e 75 6d 3b 0a 20 20 20  Index->tnum;.   
242cf 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
242d0 70 32 28 76 2c 20 4f 50 5f 43 6c 65 61 72 2c 20  p2(v, OP_Clear, 
242d1 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20 7d 0a  tnum, iDb);.  }.
242d2 20 20 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33    pKey = sqlite3
242d3 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61  IndexKeyinfo(pPa
242d4 72 73 65 2c 20 70 49 6e 64 65 78 29 3b 0a 20 20  rse, pIndex);.  
242d5 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
242d6 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74  4(v, OP_OpenWrit
242d7 65 2c 20 69 49 64 78 2c 20 74 6e 75 6d 2c 20 69  e, iIdx, tnum, i
242d8 44 62 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  Db, .           
242d9 20 20 20 20 20 20 20 20 20 28 63 68 61 72 20 2a           (char *
242da 29 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46  )pKey, P4_KEYINF
242db 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 69 66  O_HANDOFF);.  if
242dc 28 20 6d 65 6d 52 6f 6f 74 50 61 67 65 3e 3d 30  ( memRootPage>=0
242dd 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
242de 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 31  dbeChangeP5(v, 1
242df 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
242e0 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65  OpenTable(pParse
242e1 2c 20 69 54 61 62 2c 20 69 44 62 2c 20 70 54 61  , iTab, iDb, pTa
242e2 62 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29 3b  b, OP_OpenRead);
242e3 0a 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74  .  addr1 = sqlit
242e4 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
242e5 4f 50 5f 52 65 77 69 6e 64 2c 20 69 54 61 62 2c  OP_Rewind, iTab,
242e6 20 30 29 3b 0a 20 20 72 65 67 52 65 63 6f 72 64   0);.  regRecord
242e7 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
242e8 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
242e9 72 65 67 49 64 78 4b 65 79 20 3d 20 73 71 6c 69  regIdxKey = sqli
242ea 74 65 33 47 65 6e 65 72 61 74 65 49 6e 64 65 78  te3GenerateIndex
242eb 4b 65 79 28 70 50 61 72 73 65 2c 20 70 49 6e 64  Key(pParse, pInd
242ec 65 78 2c 20 69 54 61 62 2c 20 72 65 67 52 65 63  ex, iTab, regRec
242ed 6f 72 64 2c 20 31 29 3b 0a 20 20 69 66 28 20 70  ord, 1);.  if( p
242ee 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d  Index->onError!=
242ef 4f 45 5f 4e 6f 6e 65 20 29 7b 0a 20 20 20 20 63  OE_None ){.    c
242f0 6f 6e 73 74 20 69 6e 74 20 72 65 67 52 6f 77 69  onst int regRowi
242f1 64 20 3d 20 72 65 67 49 64 78 4b 65 79 20 2b 20  d = regIdxKey + 
242f2 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b  pIndex->nColumn;
242f3 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 6a  .    const int j
242f4 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  2 = sqlite3VdbeC
242f5 75 72 72 65 6e 74 41 64 64 72 28 76 29 20 2b 20  urrentAddr(v) + 
242f6 32 3b 0a 20 20 20 20 76 6f 69 64 20 2a 20 63 6f  2;.    void * co
242f7 6e 73 74 20 70 52 65 67 4b 65 79 20 3d 20 53 51  nst pRegKey = SQ
242f8 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28  LITE_INT_TO_PTR(
242f9 72 65 67 49 64 78 4b 65 79 29 3b 0a 0a 20 20 20  regIdxKey);..   
242fa 20 2f 2a 20 54 68 65 20 72 65 67 69 73 74 65 72   /* The register
242fb 73 20 61 63 63 65 73 73 65 64 20 62 79 20 74 68  s accessed by th
242fc 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 20 6f 70  e OP_IsUnique op
242fd 63 6f 64 65 20 77 65 72 65 20 61 6c 6c 6f 63 61  code were alloca
242fe 74 65 64 0a 20 20 20 20 2a 2a 20 75 73 69 6e 67  ted.    ** using
242ff 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
24300 61 6e 67 65 28 29 20 69 6e 73 69 64 65 20 6f 66  ange() inside of
24301 20 74 68 65 20 73 71 6c 69 74 65 33 47 65 6e 65   the sqlite3Gene
24302 72 61 74 65 49 6e 64 65 78 4b 65 79 28 29 0a 20  rateIndexKey(). 
24303 20 20 20 2a 2a 20 63 61 6c 6c 20 61 62 6f 76 65     ** call above
24304 2e 20 4a 75 73 74 20 62 65 66 6f 72 65 20 74 68  . Just before th
24305 61 74 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20  at function was 
24306 66 72 65 65 64 20 74 68 65 79 20 77 65 72 65 20  freed they were 
24307 72 65 6c 65 61 73 65 64 0a 20 20 20 20 2a 2a 20  released.    ** 
24308 28 6d 61 64 65 20 61 76 61 69 6c 61 62 6c 65 20  (made available 
24309 74 6f 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20  to the compiler 
2430a 66 6f 72 20 72 65 75 73 65 29 20 75 73 69 6e 67  for reuse) using
2430b 20 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33   .    ** sqlite3
2430c 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65  ReleaseTempRange
2430d 28 29 2e 20 53 6f 20 69 6e 20 73 6f 6d 65 20 77  (). So in some w
2430e 61 79 73 20 68 61 76 69 6e 67 20 74 68 65 20 4f  ays having the O
2430f 50 5f 49 73 55 6e 69 71 75 65 0a 20 20 20 20 2a  P_IsUnique.    *
24310 2a 20 6f 70 63 6f 64 65 20 75 73 65 20 74 68 65  * opcode use the
24311 20 76 61 6c 75 65 73 20 73 74 6f 72 65 64 20 77   values stored w
24312 69 74 68 69 6e 20 73 65 65 6d 73 20 64 61 6e 67  ithin seems dang
24313 65 72 6f 75 73 2e 20 48 6f 77 65 76 65 72 2c 20  erous. However, 
24314 73 69 6e 63 65 0a 20 20 20 20 2a 2a 20 77 65 20  since.    ** we 
24315 63 61 6e 20 62 65 20 73 75 72 65 20 74 68 61 74  can be sure that
24316 20 6e 6f 20 6f 74 68 65 72 20 74 65 6d 70 20 72   no other temp r
24317 65 67 69 73 74 65 72 73 20 68 61 76 65 20 62 65  egisters have be
24318 65 6e 20 61 6c 6c 6f 63 61 74 65 64 0a 20 20 20  en allocated.   
24319 20 2a 2a 20 73 69 6e 63 65 20 73 71 6c 69 74 65   ** since sqlite
2431a 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67  3ReleaseTempRang
2431b 65 28 29 20 77 61 73 20 63 61 6c 6c 65 64 2c 20  e() was called, 
2431c 69 74 20 69 73 20 73 61 66 65 20 74 6f 20 64 6f  it is safe to do
2431d 20 73 6f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20   so..    */.    
2431e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2431f 34 28 76 2c 20 4f 50 5f 49 73 55 6e 69 71 75 65  4(v, OP_IsUnique
24320 2c 20 69 49 64 78 2c 20 6a 32 2c 20 72 65 67 52  , iIdx, j2, regR
24321 6f 77 69 64 2c 20 70 52 65 67 4b 65 79 2c 20 50  owid, pRegKey, P
24322 34 5f 49 4e 54 33 32 29 3b 0a 20 20 20 20 73 71  4_INT32);.    sq
24323 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73 74 72 61  lite3HaltConstra
24324 69 6e 74 28 0a 20 20 20 20 20 20 20 20 70 50 61  int(.        pPa
24325 72 73 65 2c 20 4f 45 5f 41 62 6f 72 74 2c 20 22  rse, OE_Abort, "
24326 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 73 20  indexed columns 
24327 61 72 65 20 6e 6f 74 20 75 6e 69 71 75 65 22 2c  are not unique",
24328 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d   P4_STATIC);.  }
24329 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
2432a 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e  dOp2(v, OP_IdxIn
2432b 73 65 72 74 2c 20 69 49 64 78 2c 20 72 65 67 52  sert, iIdx, regR
2432c 65 63 6f 72 64 29 3b 0a 20 20 73 71 6c 69 74 65  ecord);.  sqlite
2432d 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
2432e 20 4f 50 46 4c 41 47 5f 55 53 45 53 45 45 4b 52   OPFLAG_USESEEKR
2432f 45 53 55 4c 54 29 3b 0a 20 20 73 71 6c 69 74 65  ESULT);.  sqlite
24330 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
24331 70 50 61 72 73 65 2c 20 72 65 67 52 65 63 6f 72  pParse, regRecor
24332 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  d);.  sqlite3Vdb
24333 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65  eAddOp2(v, OP_Ne
24334 78 74 2c 20 69 54 61 62 2c 20 61 64 64 72 31 2b  xt, iTab, addr1+
24335 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  1);.  sqlite3Vdb
24336 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
24337 72 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  r1);.  sqlite3Vd
24338 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43  beAddOp1(v, OP_C
24339 6c 6f 73 65 2c 20 69 54 61 62 29 3b 0a 20 20 73  lose, iTab);.  s
2433a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
2433b 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 49  (v, OP_Close, iI
2433c 64 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72  dx);.}../*.** Cr
2433d 65 61 74 65 20 61 20 6e 65 77 20 69 6e 64 65 78  eate a new index
2433e 20 66 6f 72 20 61 6e 20 53 51 4c 20 74 61 62 6c   for an SQL tabl
2433f 65 2e 20 20 70 4e 61 6d 65 31 2e 70 4e 61 6d 65  e.  pName1.pName
24340 32 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  2 is the name of
24341 20 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a 20 61   the index .** a
24342 6e 64 20 70 54 62 6c 4c 69 73 74 20 69 73 20 74  nd pTblList is t
24343 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74  he name of the t
24344 61 62 6c 65 20 74 68 61 74 20 69 73 20 74 6f 20  able that is to 
24345 62 65 20 69 6e 64 65 78 65 64 2e 20 20 42 6f 74  be indexed.  Bot
24346 68 20 77 69 6c 6c 20 0a 2a 2a 20 62 65 20 4e 55  h will .** be NU
24347 4c 4c 20 66 6f 72 20 61 20 70 72 69 6d 61 72 79  LL for a primary
24348 20 6b 65 79 20 6f 72 20 61 6e 20 69 6e 64 65 78   key or an index
24349 20 74 68 61 74 20 69 73 20 63 72 65 61 74 65 64   that is created
2434a 20 74 6f 20 73 61 74 69 73 66 79 20 61 0a 2a 2a   to satisfy a.**
2434b 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69   UNIQUE constrai
2434c 6e 74 2e 20 20 49 66 20 70 54 61 62 6c 65 20 61  nt.  If pTable a
2434d 6e 64 20 70 49 6e 64 65 78 20 61 72 65 20 4e 55  nd pIndex are NU
2434e 4c 4c 2c 20 75 73 65 20 70 50 61 72 73 65 2d 3e  LL, use pParse->
2434f 70 4e 65 77 54 61 62 6c 65 0a 2a 2a 20 61 73 20  pNewTable.** as 
24350 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20  the table to be 
24351 69 6e 64 65 78 65 64 2e 20 20 70 50 61 72 73 65  indexed.  pParse
24352 2d 3e 70 4e 65 77 54 61 62 6c 65 20 69 73 20 61  ->pNewTable is a
24353 20 74 61 62 6c 65 20 74 68 61 74 20 69 73 0a 2a   table that is.*
24354 2a 20 63 75 72 72 65 6e 74 6c 79 20 62 65 69 6e  * currently bein
24355 67 20 63 6f 6e 73 74 72 75 63 74 65 64 20 62 79  g constructed by
24356 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20   a CREATE TABLE 
24357 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a  statement..**.**
24358 20 70 4c 69 73 74 20 69 73 20 61 20 6c 69 73 74   pList is a list
24359 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 62   of columns to b
2435a 65 20 69 6e 64 65 78 65 64 2e 20 20 70 4c 69 73  e indexed.  pLis
2435b 74 20 77 69 6c 6c 20 62 65 20 4e 55 4c 4c 20 69  t will be NULL i
2435c 66 20 74 68 69 73 0a 2a 2a 20 69 73 20 61 20 70  f this.** is a p
2435d 72 69 6d 61 72 79 20 6b 65 79 20 6f 72 20 75 6e  rimary key or un
2435e 69 71 75 65 2d 63 6f 6e 73 74 72 61 69 6e 74 20  ique-constraint 
2435f 6f 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  on the most rece
24360 6e 74 20 63 6f 6c 75 6d 6e 20 61 64 64 65 64 0a  nt column added.
24361 2a 2a 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20  ** to the table 
24362 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20  currently under 
24363 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 0a  construction.  .
24364 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
24365 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72  E void sqlite3Cr
24366 65 61 74 65 49 6e 64 65 78 28 0a 20 20 50 61 72  eateIndex(.  Par
24367 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
24368 2f 2a 20 41 6c 6c 20 69 6e 66 6f 72 6d 61 74 69  /* All informati
24369 6f 6e 20 61 62 6f 75 74 20 74 68 69 73 20 70 61  on about this pa
2436a 72 73 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  rse */.  Token *
2436b 70 4e 61 6d 65 31 2c 20 20 20 20 20 2f 2a 20 46  pName1,     /* F
2436c 69 72 73 74 20 70 61 72 74 20 6f 66 20 69 6e 64  irst part of ind
2436d 65 78 20 6e 61 6d 65 2e 20 4d 61 79 20 62 65 20  ex name. May be 
2436e 4e 55 4c 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  NULL */.  Token 
2436f 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20 2f 2a 20  *pName2,     /* 
24370 53 65 63 6f 6e 64 20 70 61 72 74 20 6f 66 20 69  Second part of i
24371 6e 64 65 78 20 6e 61 6d 65 2e 20 4d 61 79 20 62  ndex name. May b
24372 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 53 72 63 4c  e NULL */.  SrcL
24373 69 73 74 20 2a 70 54 62 6c 4e 61 6d 65 2c 20 2f  ist *pTblName, /
24374 2a 20 54 61 62 6c 65 20 74 6f 20 69 6e 64 65 78  * Table to index
24375 2e 20 55 73 65 20 70 50 61 72 73 65 2d 3e 70 4e  . Use pParse->pN
24376 65 77 54 61 62 6c 65 20 69 66 20 30 20 2a 2f 0a  ewTable if 0 */.
24377 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73    ExprList *pLis
24378 74 2c 20 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f  t,   /* A list o
24379 66 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 62 65 20  f columns to be 
2437a 69 6e 64 65 78 65 64 20 2a 2f 0a 20 20 69 6e 74  indexed */.  int
2437b 20 6f 6e 45 72 72 6f 72 2c 20 20 20 20 20 20 20   onError,       
2437c 2f 2a 20 4f 45 5f 41 62 6f 72 74 2c 20 4f 45 5f  /* OE_Abort, OE_
2437d 49 67 6e 6f 72 65 2c 20 4f 45 5f 52 65 70 6c 61  Ignore, OE_Repla
2437e 63 65 2c 20 6f 72 20 4f 45 5f 4e 6f 6e 65 20 2a  ce, or OE_None *
2437f 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 53 74 61 72  /.  Token *pStar
24380 74 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 43 52  t,     /* The CR
24381 45 41 54 45 20 74 6f 6b 65 6e 20 74 68 61 74 20  EATE token that 
24382 62 65 67 69 6e 73 20 74 68 69 73 20 73 74 61 74  begins this stat
24383 65 6d 65 6e 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e  ement */.  Token
24384 20 2a 70 45 6e 64 2c 20 20 20 20 20 20 20 2f 2a   *pEnd,       /*
24385 20 54 68 65 20 22 29 22 20 74 68 61 74 20 63 6c   The ")" that cl
24386 6f 73 65 73 20 74 68 65 20 43 52 45 41 54 45 20  oses the CREATE 
24387 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74 20  INDEX statement 
24388 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 4f 72 64  */.  int sortOrd
24389 65 72 2c 20 20 20 20 20 2f 2a 20 53 6f 72 74 20  er,     /* Sort 
2438a 6f 72 64 65 72 20 6f 66 20 70 72 69 6d 61 72 79  order of primary
2438b 20 6b 65 79 20 77 68 65 6e 20 70 4c 69 73 74 3d   key when pList=
2438c 3d 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 69  =NULL */.  int i
2438d 66 4e 6f 74 45 78 69 73 74 20 20 20 20 20 2f 2a  fNotExist     /*
2438e 20 4f 6d 69 74 20 65 72 72 6f 72 20 69 66 20 69   Omit error if i
2438f 6e 64 65 78 20 61 6c 72 65 61 64 79 20 65 78 69  ndex already exi
24390 73 74 73 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c  sts */.){.  Tabl
24391 65 20 2a 70 54 61 62 20 3d 20 30 3b 20 20 20 20  e *pTab = 0;    
24392 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20 62 65 20   /* Table to be 
24393 69 6e 64 65 78 65 64 20 2a 2f 0a 20 20 49 6e 64  indexed */.  Ind
24394 65 78 20 2a 70 49 6e 64 65 78 20 3d 20 30 3b 20  ex *pIndex = 0; 
24395 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 74    /* The index t
24396 6f 20 62 65 20 63 72 65 61 74 65 64 20 2a 2f 0a  o be created */.
24397 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20    char *zName = 
24398 30 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f  0;     /* Name o
24399 66 20 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20  f the index */. 
2439a 20 69 6e 74 20 6e 4e 61 6d 65 3b 20 20 20 20 20   int nName;     
2439b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
2439c 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 69 6e  of characters in
2439d 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20   zName */.  int 
2439e 69 2c 20 6a 3b 0a 20 20 54 6f 6b 65 6e 20 6e 75  i, j;.  Token nu
2439f 6c 6c 49 64 3b 20 20 20 20 20 20 20 20 2f 2a 20  llId;        /* 
243a0 46 61 6b 65 20 74 6f 6b 65 6e 20 66 6f 72 20 61  Fake token for a
243a1 6e 20 65 6d 70 74 79 20 49 44 20 6c 69 73 74 20  n empty ID list 
243a2 2a 2f 0a 20 20 44 62 46 69 78 65 72 20 73 46 69  */.  DbFixer sFi
243a3 78 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72  x;        /* For
243a4 20 61 73 73 69 67 6e 69 6e 67 20 64 61 74 61 62   assigning datab
243a5 61 73 65 20 6e 61 6d 65 73 20 74 6f 20 70 54 61  ase names to pTa
243a6 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 73 6f 72  ble */.  int sor
243a7 74 4f 72 64 65 72 4d 61 73 6b 3b 20 20 20 2f 2a  tOrderMask;   /*
243a8 20 31 20 74 6f 20 68 6f 6e 6f 72 20 44 45 53 43   1 to honor DESC
243a9 20 69 6e 20 69 6e 64 65 78 2e 20 20 30 20 74 6f   in index.  0 to
243aa 20 69 67 6e 6f 72 65 2e 20 2a 2f 0a 20 20 73 71   ignore. */.  sq
243ab 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
243ac 73 65 2d 3e 64 62 3b 0a 20 20 44 62 20 2a 70 44  se->db;.  Db *pD
243ad 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  b;             /
243ae 2a 20 54 68 65 20 73 70 65 63 69 66 69 63 20 74  * The specific t
243af 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  able containing 
243b0 74 68 65 20 69 6e 64 65 78 65 64 20 64 61 74 61  the indexed data
243b1 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 44  base */.  int iD
243b2 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  b;             /
243b3 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 64  * Index of the d
243b4 61 74 61 62 61 73 65 20 74 68 61 74 20 69 73 20  atabase that is 
243b5 62 65 69 6e 67 20 77 72 69 74 74 65 6e 20 2a 2f  being written */
243b6 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20  .  Token *pName 
243b7 3d 20 30 3b 20 20 20 20 2f 2a 20 55 6e 71 75 61  = 0;    /* Unqua
243b8 6c 69 66 69 65 64 20 6e 61 6d 65 20 6f 66 20 74  lified name of t
243b9 68 65 20 69 6e 64 65 78 20 74 6f 20 63 72 65 61  he index to crea
243ba 74 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 45  te */.  struct E
243bb 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4c  xprList_item *pL
243bc 69 73 74 49 74 65 6d 3b 20 2f 2a 20 46 6f 72 20  istItem; /* For 
243bd 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 70 4c 69  looping over pLi
243be 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c  st */.  int nCol
243bf 3b 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 20 3d  ;.  int nExtra =
243c0 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a 45 78 74   0;.  char *zExt
243c1 72 61 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  ra;..  assert( p
243c2 53 74 61 72 74 3d 3d 30 20 7c 7c 20 70 45 6e 64  Start==0 || pEnd
243c3 21 3d 30 20 29 3b 20 2f 2a 20 70 45 6e 64 20 6d  !=0 ); /* pEnd m
243c4 75 73 74 20 62 65 20 6e 6f 6e 2d 4e 55 4c 4c 20  ust be non-NULL 
243c5 69 66 20 70 53 74 61 72 74 20 69 73 20 2a 2f 0a  if pStart is */.
243c6 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65    assert( pParse
243c7 2d 3e 6e 45 72 72 3d 3d 30 20 29 3b 20 20 20 20  ->nErr==0 );    
243c8 20 20 2f 2a 20 4e 65 76 65 72 20 63 61 6c 6c 65    /* Never calle
243c9 64 20 77 69 74 68 20 70 72 69 6f 72 20 65 72 72  d with prior err
243ca 6f 72 73 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d  ors */.  if( db-
243cb 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c  >mallocFailed ||
243cc 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42   IN_DECLARE_VTAB
243cd 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69   ){.    goto exi
243ce 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a  t_create_index;.
243cf 20 20 7d 0a 20 20 69 66 28 20 53 51 4c 49 54 45    }.  if( SQLITE
243d0 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61 64  _OK!=sqlite3Read
243d1 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29  Schema(pParse) )
243d2 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f  {.    goto exit_
243d3 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20  create_index;.  
243d4 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46 69 6e  }..  /*.  ** Fin
243d5 64 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74  d the table that
243d6 20 69 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65   is to be indexe
243d7 64 2e 20 20 52 65 74 75 72 6e 20 65 61 72 6c 79  d.  Return early
243d8 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 20   if not found.. 
243d9 20 2a 2f 0a 20 20 69 66 28 20 70 54 62 6c 4e 61   */.  if( pTblNa
243da 6d 65 21 3d 30 20 29 7b 0a 0a 20 20 20 20 2f 2a  me!=0 ){..    /*
243db 20 55 73 65 20 74 68 65 20 74 77 6f 2d 70 61 72   Use the two-par
243dc 74 20 69 6e 64 65 78 20 6e 61 6d 65 20 74 6f 20  t index name to 
243dd 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 64 61  determine the da
243de 74 61 62 61 73 65 20 0a 20 20 20 20 2a 2a 20 74  tabase .    ** t
243df 6f 20 73 65 61 72 63 68 20 66 6f 72 20 74 68 65  o search for the
243e0 20 74 61 62 6c 65 2e 20 27 46 69 78 27 20 74 68   table. 'Fix' th
243e1 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 74 6f 20  e table name to 
243e2 74 68 69 73 20 64 62 0a 20 20 20 20 2a 2a 20 62  this db.    ** b
243e3 65 66 6f 72 65 20 6c 6f 6f 6b 69 6e 67 20 75 70  efore looking up
243e4 20 74 68 65 20 74 61 62 6c 65 2e 0a 20 20 20 20   the table..    
243e5 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  */.    assert( p
243e6 4e 61 6d 65 31 20 26 26 20 70 4e 61 6d 65 32 20  Name1 && pName2 
243e7 29 3b 0a 20 20 20 20 69 44 62 20 3d 20 73 71 6c  );.    iDb = sql
243e8 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28  ite3TwoPartName(
243e9 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20  pParse, pName1, 
243ea 70 4e 61 6d 65 32 2c 20 26 70 4e 61 6d 65 29 3b  pName2, &pName);
243eb 0a 20 20 20 20 69 66 28 20 69 44 62 3c 30 20 29  .    if( iDb<0 )
243ec 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74   goto exit_creat
243ed 65 5f 69 6e 64 65 78 3b 0a 0a 23 69 66 6e 64 65  e_index;..#ifnde
243ee 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45  f SQLITE_OMIT_TE
243ef 4d 50 44 42 0a 20 20 20 20 2f 2a 20 49 66 20 74  MPDB.    /* If t
243f0 68 65 20 69 6e 64 65 78 20 6e 61 6d 65 20 77 61  he index name wa
243f1 73 20 75 6e 71 75 61 6c 69 66 69 65 64 2c 20 63  s unqualified, c
243f2 68 65 63 6b 20 69 66 20 74 68 65 20 74 68 65 20  heck if the the 
243f3 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20 69 73 20  table.    ** is 
243f4 61 20 74 65 6d 70 20 74 61 62 6c 65 2e 20 49 66  a temp table. If
243f5 20 73 6f 2c 20 73 65 74 20 74 68 65 20 64 61 74   so, set the dat
243f6 61 62 61 73 65 20 74 6f 20 31 2e 20 44 6f 20 6e  abase to 1. Do n
243f7 6f 74 20 64 6f 20 74 68 69 73 0a 20 20 20 20 2a  ot do this.    *
243f8 2a 20 69 66 20 69 6e 69 74 69 61 6c 69 73 69 6e  * if initialisin
243f9 67 20 61 20 64 61 74 61 62 61 73 65 20 73 63 68  g a database sch
243fa 65 6d 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ema..    */.    
243fb 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75  if( !db->init.bu
243fc 73 79 20 29 7b 0a 20 20 20 20 20 20 70 54 61 62  sy ){.      pTab
243fd 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73   = sqlite3SrcLis
243fe 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c 20  tLookup(pParse, 
243ff 70 54 62 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20  pTblName);.     
24400 20 69 66 28 20 70 4e 61 6d 65 32 2d 3e 6e 3d 3d   if( pName2->n==
24401 30 20 26 26 20 70 54 61 62 20 26 26 20 70 54 61  0 && pTab && pTa
24402 62 2d 3e 70 53 63 68 65 6d 61 3d 3d 64 62 2d 3e  b->pSchema==db->
24403 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29  aDb[1].pSchema )
24404 7b 0a 20 20 20 20 20 20 20 20 69 44 62 20 3d 20  {.        iDb = 
24405 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
24406 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 69 66 28  .#endif..    if(
24407 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74 28   sqlite3FixInit(
24408 26 73 46 69 78 2c 20 70 50 61 72 73 65 2c 20 69  &sFix, pParse, i
24409 44 62 2c 20 22 69 6e 64 65 78 22 2c 20 70 4e 61  Db, "index", pNa
2440a 6d 65 29 20 26 26 0a 20 20 20 20 20 20 20 20 73  me) &&.        s
2440b 71 6c 69 74 65 33 46 69 78 53 72 63 4c 69 73 74  qlite3FixSrcList
2440c 28 26 73 46 69 78 2c 20 70 54 62 6c 4e 61 6d 65  (&sFix, pTblName
2440d 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 2f  ).    ){.      /
2440e 2a 20 42 65 63 61 75 73 65 20 74 68 65 20 70 61  * Because the pa
2440f 72 73 65 72 20 63 6f 6e 73 74 72 75 63 74 73 20  rser constructs 
24410 70 54 62 6c 4e 61 6d 65 20 66 72 6f 6d 20 61 20  pTblName from a 
24411 73 69 6e 67 6c 65 20 69 64 65 6e 74 69 66 69 65  single identifie
24412 72 2c 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69  r,.      ** sqli
24413 74 65 33 46 69 78 53 72 63 4c 69 73 74 20 63 61  te3FixSrcList ca
24414 6e 20 6e 65 76 65 72 20 66 61 69 6c 2e 20 2a 2f  n never fail. */
24415 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 30 29  .      assert(0)
24416 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 54 61 62  ;.    }.    pTab
24417 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65   = sqlite3Locate
24418 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c  Table(pParse, 0,
24419 20 70 54 62 6c 4e 61 6d 65 2d 3e 61 5b 30 5d 2e   pTblName->a[0].
2441a 7a 4e 61 6d 65 2c 20 0a 20 20 20 20 20 20 20 20  zName, .        
2441b 70 54 62 6c 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a  pTblName->a[0].z
2441c 44 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20 69  Database);.    i
2441d 66 28 20 21 70 54 61 62 20 7c 7c 20 64 62 2d 3e  f( !pTab || db->
2441e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67  mallocFailed ) g
2441f 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f  oto exit_create_
24420 69 6e 64 65 78 3b 0a 20 20 20 20 61 73 73 65 72  index;.    asser
24421 74 28 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  t( db->aDb[iDb].
24422 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70  pSchema==pTab->p
24423 53 63 68 65 6d 61 20 29 3b 0a 20 20 7d 65 6c 73  Schema );.  }els
24424 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  e{.    assert( p
24425 4e 61 6d 65 3d 3d 30 20 29 3b 0a 20 20 20 20 70  Name==0 );.    p
24426 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e  Tab = pParse->pN
24427 65 77 54 61 62 6c 65 3b 0a 20 20 20 20 69 66 28  ewTable;.    if(
24428 20 21 70 54 61 62 20 29 20 67 6f 74 6f 20 65 78   !pTab ) goto ex
24429 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b  it_create_index;
2442a 0a 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74  .    iDb = sqlit
2442b 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
2442c 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
2442d 61 29 3b 0a 20 20 7d 0a 20 20 70 44 62 20 3d 20  a);.  }.  pDb = 
2442e 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 0a  &db->aDb[iDb];..
2442f 20 20 61 73 73 65 72 74 28 20 70 54 61 62 21 3d    assert( pTab!=
24430 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
24431 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 29  Parse->nErr==0 )
24432 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  ;.  if( sqlite3S
24433 74 72 4e 49 43 6d 70 28 70 54 61 62 2d 3e 7a 4e  trNICmp(pTab->zN
24434 61 6d 65 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20  ame, "sqlite_", 
24435 37 29 3d 3d 30 20 0a 20 20 20 20 20 20 20 26 26  7)==0 .       &&
24436 20 6d 65 6d 63 6d 70 28 26 70 54 61 62 2d 3e 7a   memcmp(&pTab->z
24437 4e 61 6d 65 5b 37 5d 2c 22 61 6c 74 65 72 74 61  Name[7],"alterta
24438 62 5f 22 2c 39 29 21 3d 30 20 29 7b 0a 20 20 20  b_",9)!=0 ){.   
24439 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2443a 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c 65 20  (pParse, "table 
2443b 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 69 6e  %s may not be in
2443c 64 65 78 65 64 22 2c 20 70 54 61 62 2d 3e 7a 4e  dexed", pTab->zN
2443d 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65  ame);.    goto e
2443e 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78  xit_create_index
2443f 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51  ;.  }.#ifndef SQ
24440 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20  LITE_OMIT_VIEW. 
24441 20 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c 65   if( pTab->pSele
24442 63 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ct ){.    sqlite
24443 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
24444 2c 20 22 76 69 65 77 73 20 6d 61 79 20 6e 6f 74  , "views may not
24445 20 62 65 20 69 6e 64 65 78 65 64 22 29 3b 0a 20   be indexed");. 
24446 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65     goto exit_cre
24447 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 23  ate_index;.  }.#
24448 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51  endif.#ifndef SQ
24449 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
2444a 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 49 73 56  LTABLE.  if( IsV
2444b 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a  irtual(pTab) ){.
2444c 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2444d 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76 69 72  Msg(pParse, "vir
2444e 74 75 61 6c 20 74 61 62 6c 65 73 20 6d 61 79 20  tual tables may 
2444f 6e 6f 74 20 62 65 20 69 6e 64 65 78 65 64 22 29  not be indexed")
24450 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f  ;.    goto exit_
24451 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20  create_index;.  
24452 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 0a 20  }.#endif..  /*. 
24453 20 2a 2a 20 46 69 6e 64 20 74 68 65 20 6e 61 6d   ** Find the nam
24454 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 2e 20  e of the index. 
24455 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 72 65   Make sure there
24456 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20   is not already 
24457 61 6e 6f 74 68 65 72 0a 20 20 2a 2a 20 69 6e 64  another.  ** ind
24458 65 78 20 6f 72 20 74 61 62 6c 65 20 77 69 74 68  ex or table with
24459 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2e 20   the same name. 
2445a 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 78 63 65   .  **.  ** Exce
2445b 70 74 69 6f 6e 3a 20 20 49 66 20 77 65 20 61 72  ption:  If we ar
2445c 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 6e 61  e reading the na
2445d 6d 65 73 20 6f 66 20 70 65 72 6d 61 6e 65 6e 74  mes of permanent
2445e 20 69 6e 64 69 63 65 73 20 66 72 6f 6d 20 74 68   indices from th
2445f 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f 6d 61  e.  ** sqlite_ma
24460 73 74 65 72 20 74 61 62 6c 65 20 28 62 65 63 61  ster table (beca
24461 75 73 65 20 73 6f 6d 65 20 6f 74 68 65 72 20 70  use some other p
24462 72 6f 63 65 73 73 20 63 68 61 6e 67 65 64 20 74  rocess changed t
24463 68 65 20 73 63 68 65 6d 61 29 20 61 6e 64 0a 20  he schema) and. 
24464 20 2a 2a 20 6f 6e 65 20 6f 66 20 74 68 65 20 69   ** one of the i
24465 6e 64 65 78 20 6e 61 6d 65 73 20 63 6f 6c 6c 69  ndex names colli
24466 64 65 73 20 77 69 74 68 20 74 68 65 20 6e 61 6d  des with the nam
24467 65 20 6f 66 20 61 20 74 65 6d 70 6f 72 61 72 79  e of a temporary
24468 20 74 61 62 6c 65 20 6f 72 0a 20 20 2a 2a 20 69   table or.  ** i
24469 6e 64 65 78 2c 20 74 68 65 6e 20 77 65 20 77 69  ndex, then we wi
2446a 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 70  ll continue to p
2446b 72 6f 63 65 73 73 20 74 68 69 73 20 69 6e 64 65  rocess this inde
2446c 78 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20  x..  **.  ** If 
2446d 70 4e 61 6d 65 3d 3d 30 20 69 74 20 6d 65 61 6e  pName==0 it mean
2446e 73 20 74 68 61 74 20 77 65 20 61 72 65 0a 20 20  s that we are.  
2446f 2a 2a 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20  ** dealing with 
24470 61 20 70 72 69 6d 61 72 79 20 6b 65 79 20 6f 72  a primary key or
24471 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69   UNIQUE constrai
24472 6e 74 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20  nt.  We have to 
24473 69 6e 76 65 6e 74 20 6f 75 72 0a 20 20 2a 2a 20  invent our.  ** 
24474 6f 77 6e 20 6e 61 6d 65 2e 0a 20 20 2a 2f 0a 20  own name..  */. 
24475 20 69 66 28 20 70 4e 61 6d 65 20 29 7b 0a 20 20   if( pName ){.  
24476 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65    zName = sqlite
24477 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64  3NameFromToken(d
24478 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 20 20 69  b, pName);.    i
24479 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29 20 67 6f  f( zName==0 ) go
2447a 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69  to exit_create_i
2447b 6e 64 65 78 3b 0a 20 20 20 20 69 66 28 20 53 51  ndex;.    if( SQ
2447c 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33  LITE_OK!=sqlite3
2447d 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28  CheckObjectName(
2447e 70 50 61 72 73 65 2c 20 7a 4e 61 6d 65 29 20 29  pParse, zName) )
2447f 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69  {.      goto exi
24480 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a  t_create_index;.
24481 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 64      }.    if( !d
24482 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a  b->init.busy ){.
24483 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
24484 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a  3FindTable(db, z
24485 4e 61 6d 65 2c 20 30 29 21 3d 30 20 29 7b 0a 20  Name, 0)!=0 ){. 
24486 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
24487 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
24488 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79  there is already
24489 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 25   a table named %
2448a 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  s", zName);.    
2448b 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72      goto exit_cr
2448c 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20  eate_index;.    
2448d 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
2448e 28 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64  ( sqlite3FindInd
2448f 65 78 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 70 44  ex(db, zName, pD
24490 62 2d 3e 7a 4e 61 6d 65 29 21 3d 30 20 29 7b 0a  b->zName)!=0 ){.
24491 20 20 20 20 20 20 69 66 28 20 21 69 66 4e 6f 74        if( !ifNot
24492 45 78 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20  Exist ){.       
24493 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
24494 28 70 50 61 72 73 65 2c 20 22 69 6e 64 65 78 20  (pParse, "index 
24495 25 73 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  %s already exist
24496 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  s", zName);.    
24497 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f 20 65    }.      goto e
24498 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78  xit_create_index
24499 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
2449a 0a 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20  .    int n;.    
2449b 49 6e 64 65 78 20 2a 70 4c 6f 6f 70 3b 0a 20 20  Index *pLoop;.  
2449c 20 20 66 6f 72 28 70 4c 6f 6f 70 3d 70 54 61 62    for(pLoop=pTab
2449d 2d 3e 70 49 6e 64 65 78 2c 20 6e 3d 31 3b 20 70  ->pIndex, n=1; p
2449e 4c 6f 6f 70 3b 20 70 4c 6f 6f 70 3d 70 4c 6f 6f  Loop; pLoop=pLoo
2449f 70 2d 3e 70 4e 65 78 74 2c 20 6e 2b 2b 29 7b 7d  p->pNext, n++){}
244a0 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c  .    zName = sql
244a1 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
244a2 22 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e 64 65  "sqlite_autoinde
244a3 78 5f 25 73 5f 25 64 22 2c 20 70 54 61 62 2d 3e  x_%s_%d", pTab->
244a4 7a 4e 61 6d 65 2c 20 6e 29 3b 0a 20 20 20 20 69  zName, n);.    i
244a5 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20  f( zName==0 ){. 
244a6 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63       goto exit_c
244a7 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20  reate_index;.   
244a8 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65   }.  }..  /* Che
244a9 63 6b 20 66 6f 72 20 61 75 74 68 6f 72 69 7a 61  ck for authoriza
244aa 74 69 6f 6e 20 74 6f 20 63 72 65 61 74 65 20 61  tion to create a
244ab 6e 20 69 6e 64 65 78 2e 0a 20 20 2a 2f 0a 23 69  n index..  */.#i
244ac 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
244ad 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a  T_AUTHORIZATION.
244ae 20 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68    {.    const ch
244af 61 72 20 2a 7a 44 62 20 3d 20 70 44 62 2d 3e 7a  ar *zDb = pDb->z
244b0 4e 61 6d 65 3b 0a 20 20 20 20 69 66 28 20 73 71  Name;.    if( sq
244b1 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
244b2 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 49 4e  Parse, SQLITE_IN
244b3 53 45 52 54 2c 20 53 43 48 45 4d 41 5f 54 41 42  SERT, SCHEMA_TAB
244b4 4c 45 28 69 44 62 29 2c 20 30 2c 20 7a 44 62 29  LE(iDb), 0, zDb)
244b5 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65   ){.      goto e
244b6 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78  xit_create_index
244b7 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 20 3d 20  ;.    }.    i = 
244b8 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 49 4e  SQLITE_CREATE_IN
244b9 44 45 58 3b 0a 20 20 20 20 69 66 28 20 21 4f 4d  DEX;.    if( !OM
244ba 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44 62  IT_TEMPDB && iDb
244bb 3d 3d 31 20 29 20 69 20 3d 20 53 51 4c 49 54 45  ==1 ) i = SQLITE
244bc 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 49 4e 44  _CREATE_TEMP_IND
244bd 45 58 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  EX;.    if( sqli
244be 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
244bf 72 73 65 2c 20 69 2c 20 7a 4e 61 6d 65 2c 20 70  rse, i, zName, p
244c0 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 44 62 29  Tab->zName, zDb)
244c1 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65   ){.      goto e
244c2 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78  xit_create_index
244c3 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
244c4 69 66 0a 0a 20 20 2f 2a 20 49 66 20 70 4c 69 73  if..  /* If pLis
244c5 74 3d 3d 30 2c 20 69 74 20 6d 65 61 6e 73 20 74  t==0, it means t
244c6 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20  his routine was 
244c7 63 61 6c 6c 65 64 20 74 6f 20 6d 61 6b 65 20 61  called to make a
244c8 20 70 72 69 6d 61 72 79 0a 20 20 2a 2a 20 6b 65   primary.  ** ke
244c9 79 20 6f 75 74 20 6f 66 20 74 68 65 20 6c 61 73  y out of the las
244ca 74 20 63 6f 6c 75 6d 6e 20 61 64 64 65 64 20 74  t column added t
244cb 6f 20 74 68 65 20 74 61 62 6c 65 20 75 6e 64 65  o the table unde
244cc 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 0a  r construction..
244cd 20 20 2a 2a 20 53 6f 20 63 72 65 61 74 65 20 61    ** So create a
244ce 20 66 61 6b 65 20 6c 69 73 74 20 74 6f 20 73 69   fake list to si
244cf 6d 75 6c 61 74 65 20 74 68 69 73 2e 0a 20 20 2a  mulate this..  *
244d0 2f 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30  /.  if( pList==0
244d1 20 29 7b 0a 20 20 20 20 6e 75 6c 6c 49 64 2e 7a   ){.    nullId.z
244d2 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 54   = pTab->aCol[pT
244d3 61 62 2d 3e 6e 43 6f 6c 2d 31 5d 2e 7a 4e 61 6d  ab->nCol-1].zNam
244d4 65 3b 0a 20 20 20 20 6e 75 6c 6c 49 64 2e 6e 20  e;.    nullId.n 
244d5 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
244d6 30 28 28 63 68 61 72 2a 29 6e 75 6c 6c 49 64 2e  0((char*)nullId.
244d7 7a 29 3b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20  z);.    pList = 
244d8 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
244d9 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 30 2c  ppend(pParse, 0,
244da 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 69   0);.    if( pLi
244db 73 74 3d 3d 30 20 29 20 67 6f 74 6f 20 65 78 69  st==0 ) goto exi
244dc 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a  t_create_index;.
244dd 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c      sqlite3ExprL
244de 69 73 74 53 65 74 4e 61 6d 65 28 70 50 61 72 73  istSetName(pPars
244df 65 2c 20 70 4c 69 73 74 2c 20 26 6e 75 6c 6c 49  e, pList, &nullI
244e0 64 2c 20 30 29 3b 0a 20 20 20 20 70 4c 69 73 74  d, 0);.    pList
244e1 2d 3e 61 5b 30 5d 2e 73 6f 72 74 4f 72 64 65 72  ->a[0].sortOrder
244e2 20 3d 20 28 75 38 29 73 6f 72 74 4f 72 64 65 72   = (u8)sortOrder
244e3 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 67 75  ;.  }..  /* Figu
244e4 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20  re out how many 
244e5 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 61  bytes of space a
244e6 72 65 20 72 65 71 75 69 72 65 64 20 74 6f 20 73  re required to s
244e7 74 6f 72 65 20 65 78 70 6c 69 63 69 74 6c 79 0a  tore explicitly.
244e8 20 20 2a 2a 20 73 70 65 63 69 66 69 65 64 20 63    ** specified c
244e9 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
244ea 65 20 6e 61 6d 65 73 2e 0a 20 20 2a 2f 0a 20 20  e names..  */.  
244eb 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74  for(i=0; i<pList
244ec 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20  ->nExpr; i++){. 
244ed 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 3d     Expr *pExpr =
244ee 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78   pList->a[i].pEx
244ef 70 72 3b 0a 20 20 20 20 69 66 28 20 70 45 78 70  pr;.    if( pExp
244f0 72 20 29 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53  r ){.      CollS
244f1 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 70 45 78 70  eq *pColl = pExp
244f2 72 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20  r->pColl;.      
244f3 2f 2a 20 45 69 74 68 65 72 20 70 43 6f 6c 6c 21  /* Either pColl!
244f4 3d 30 20 6f 72 20 74 68 65 72 65 20 77 61 73 20  =0 or there was 
244f5 61 6e 20 4f 4f 4d 20 66 61 69 6c 75 72 65 2e 20  an OOM failure. 
244f6 20 42 75 74 20 69 66 20 61 6e 20 4f 4f 4d 0a 20   But if an OOM. 
244f7 20 20 20 20 20 2a 2a 20 66 61 69 6c 75 72 65 20       ** failure 
244f8 77 65 20 68 61 76 65 20 71 75 69 74 20 62 65 66  we have quit bef
244f9 6f 72 65 20 72 65 61 63 68 69 6e 67 20 74 68 69  ore reaching thi
244fa 73 20 70 6f 69 6e 74 2e 20 2a 2f 0a 20 20 20 20  s point. */.    
244fb 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 43 6f    if( ALWAYS(pCo
244fc 6c 6c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 6e  ll) ){.        n
244fd 45 78 74 72 61 20 2b 3d 20 28 31 20 2b 20 73 71  Extra += (1 + sq
244fe 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 43  lite3Strlen30(pC
244ff 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20  oll->zName));.  
24500 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
24501 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 41 6c 6c 6f  .  /* .  ** Allo
24502 63 61 74 65 20 74 68 65 20 69 6e 64 65 78 20 73  cate the index s
24503 74 72 75 63 74 75 72 65 2e 20 0a 20 20 2a 2f 0a  tructure. .  */.
24504 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65    nName = sqlite
24505 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29  3Strlen30(zName)
24506 3b 0a 20 20 6e 43 6f 6c 20 3d 20 70 4c 69 73 74  ;.  nCol = pList
24507 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 49 6e 64 65  ->nExpr;.  pInde
24508 78 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  x = sqlite3DbMal
24509 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 0a 20 20 20  locZero(db, .   
2450a 20 20 20 73 69 7a 65 6f 66 28 49 6e 64 65 78 29     sizeof(Index)
2450b 20 2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20   +              
2450c 2f 2a 20 49 6e 64 65 78 20 73 74 72 75 63 74 75  /* Index structu
2450d 72 65 20 20 2a 2f 0a 20 20 20 20 20 20 73 69 7a  re  */.      siz
2450e 65 6f 66 28 69 6e 74 29 2a 6e 43 6f 6c 20 2b 20  eof(int)*nCol + 
2450f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
24510 65 78 2e 61 69 43 6f 6c 75 6d 6e 20 20 20 2a 2f  ex.aiColumn   */
24511 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 69 6e  .      sizeof(in
24512 74 29 2a 28 6e 43 6f 6c 2b 31 29 20 2b 20 20 20  t)*(nCol+1) +   
24513 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e 61 69 52      /* Index.aiR
24514 6f 77 45 73 74 20 20 20 2a 2f 0a 20 20 20 20 20  owEst   */.     
24515 20 73 69 7a 65 6f 66 28 63 68 61 72 20 2a 29 2a   sizeof(char *)*
24516 6e 43 6f 6c 20 2b 20 20 20 20 20 20 20 20 2f 2a  nCol +        /*
24517 20 49 6e 64 65 78 2e 61 7a 43 6f 6c 6c 20 20 20   Index.azColl   
24518 20 20 2a 2f 0a 20 20 20 20 20 20 73 69 7a 65 6f    */.      sizeo
24519 66 28 75 38 29 2a 6e 43 6f 6c 20 2b 20 20 20 20  f(u8)*nCol +    
2451a 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
2451b 2e 61 53 6f 72 74 4f 72 64 65 72 20 2a 2f 0a 20  .aSortOrder */. 
2451c 20 20 20 20 20 6e 4e 61 6d 65 20 2b 20 31 20 2b       nName + 1 +
2451d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2451e 20 20 2f 2a 20 49 6e 64 65 78 2e 7a 4e 61 6d 65    /* Index.zName
2451f 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 6e        */.      n
24520 45 78 74 72 61 20 20 20 20 20 20 20 20 20 20 20  Extra           
24521 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
24522 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
24523 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 29 3b 0a  e names */.  );.
24524 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
24525 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f  Failed ){.    go
24526 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69  to exit_create_i
24527 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 70 49 6e 64  ndex;.  }.  pInd
24528 65 78 2d 3e 61 7a 43 6f 6c 6c 20 3d 20 28 63 68  ex->azColl = (ch
24529 61 72 2a 2a 29 28 26 70 49 6e 64 65 78 5b 31 5d  ar**)(&pIndex[1]
2452a 29 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 61 69 43  );.  pIndex->aiC
2452b 6f 6c 75 6d 6e 20 3d 20 28 69 6e 74 20 2a 29 28  olumn = (int *)(
2452c 26 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 5b  &pIndex->azColl[
2452d 6e 43 6f 6c 5d 29 3b 0a 20 20 70 49 6e 64 65 78  nCol]);.  pIndex
2452e 2d 3e 61 69 52 6f 77 45 73 74 20 3d 20 28 75 6e  ->aiRowEst = (un
2452f 73 69 67 6e 65 64 20 2a 29 28 26 70 49 6e 64 65  signed *)(&pInde
24530 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 43 6f 6c  x->aiColumn[nCol
24531 5d 29 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 61 53  ]);.  pIndex->aS
24532 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38 20 2a  ortOrder = (u8 *
24533 29 28 26 70 49 6e 64 65 78 2d 3e 61 69 52 6f 77  )(&pIndex->aiRow
24534 45 73 74 5b 6e 43 6f 6c 2b 31 5d 29 3b 0a 20 20  Est[nCol+1]);.  
24535 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 20 3d 20  pIndex->zName = 
24536 28 63 68 61 72 20 2a 29 28 26 70 49 6e 64 65 78  (char *)(&pIndex
24537 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 6e 43 6f  ->aSortOrder[nCo
24538 6c 5d 29 3b 0a 20 20 7a 45 78 74 72 61 20 3d 20  l]);.  zExtra = 
24539 28 63 68 61 72 20 2a 29 28 26 70 49 6e 64 65 78  (char *)(&pIndex
2453a 2d 3e 7a 4e 61 6d 65 5b 6e 4e 61 6d 65 2b 31 5d  ->zName[nName+1]
2453b 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 49 6e 64  );.  memcpy(pInd
2453c 65 78 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65  ex->zName, zName
2453d 2c 20 6e 4e 61 6d 65 2b 31 29 3b 0a 20 20 70 49  , nName+1);.  pI
2453e 6e 64 65 78 2d 3e 70 54 61 62 6c 65 20 3d 20 70  ndex->pTable = p
2453f 54 61 62 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 6e  Tab;.  pIndex->n
24540 43 6f 6c 75 6d 6e 20 3d 20 70 4c 69 73 74 2d 3e  Column = pList->
24541 6e 45 78 70 72 3b 0a 20 20 70 49 6e 64 65 78 2d  nExpr;.  pIndex-
24542 3e 6f 6e 45 72 72 6f 72 20 3d 20 28 75 38 29 6f  >onError = (u8)o
24543 6e 45 72 72 6f 72 3b 0a 20 20 70 49 6e 64 65 78  nError;.  pIndex
24544 2d 3e 61 75 74 6f 49 6e 64 65 78 20 3d 20 28 75  ->autoIndex = (u
24545 38 29 28 70 4e 61 6d 65 3d 3d 30 29 3b 0a 20 20  8)(pName==0);.  
24546 70 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 20  pIndex->pSchema 
24547 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70  = db->aDb[iDb].p
24548 53 63 68 65 6d 61 3b 0a 0a 20 20 2f 2a 20 43 68  Schema;..  /* Ch
24549 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 77 65  eck to see if we
2454a 20 73 68 6f 75 6c 64 20 68 6f 6e 6f 72 20 44 45   should honor DE
2454b 53 43 20 72 65 71 75 65 73 74 73 20 6f 6e 20 69  SC requests on i
2454c 6e 64 65 78 20 63 6f 6c 75 6d 6e 73 0a 20 20 2a  ndex columns.  *
2454d 2f 0a 20 20 69 66 28 20 70 44 62 2d 3e 70 53 63  /.  if( pDb->pSc
2454e 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61  hema->file_forma
2454f 74 3e 3d 34 20 29 7b 0a 20 20 20 20 73 6f 72 74  t>=4 ){.    sort
24550 4f 72 64 65 72 4d 61 73 6b 20 3d 20 2d 31 3b 20  OrderMask = -1; 
24551 20 20 2f 2a 20 48 6f 6e 6f 72 20 44 45 53 43 20    /* Honor DESC 
24552 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  */.  }else{.    
24553 73 6f 72 74 4f 72 64 65 72 4d 61 73 6b 20 3d 20  sortOrderMask = 
24554 30 3b 20 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20  0;    /* Ignore 
24555 44 45 53 43 20 2a 2f 0a 20 20 7d 0a 0a 20 20 2f  DESC */.  }..  /
24556 2a 20 53 63 61 6e 20 74 68 65 20 6e 61 6d 65 73  * Scan the names
24557 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20   of the columns 
24558 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20  of the table to 
24559 62 65 20 69 6e 64 65 78 65 64 20 61 6e 64 0a 20  be indexed and. 
2455a 20 2a 2a 20 6c 6f 61 64 20 74 68 65 20 63 6f 6c   ** load the col
2455b 75 6d 6e 20 69 6e 64 69 63 65 73 20 69 6e 74 6f  umn indices into
2455c 20 74 68 65 20 49 6e 64 65 78 20 73 74 72 75 63   the Index struc
2455d 74 75 72 65 2e 20 20 52 65 70 6f 72 74 20 61 6e  ture.  Report an
2455e 20 65 72 72 6f 72 0a 20 20 2a 2a 20 69 66 20 61   error.  ** if a
2455f 6e 79 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74  ny column is not
24560 20 66 6f 75 6e 64 2e 0a 20 20 2a 2a 0a 20 20 2a   found..  **.  *
24561 2a 20 54 4f 44 4f 3a 20 20 41 64 64 20 61 20 74  * TODO:  Add a t
24562 65 73 74 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  est to make sure
24563 20 74 68 61 74 20 74 68 65 20 73 61 6d 65 20 63   that the same c
24564 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 6e 61 6d  olumn is not nam
24565 65 64 0a 20 20 2a 2a 20 6d 6f 72 65 20 74 68 61  ed.  ** more tha
24566 6e 20 6f 6e 63 65 20 77 69 74 68 69 6e 20 74 68  n once within th
24567 65 20 73 61 6d 65 20 69 6e 64 65 78 2e 20 20 4f  e same index.  O
24568 6e 6c 79 20 74 68 65 20 66 69 72 73 74 20 69 6e  nly the first in
24569 73 74 61 6e 63 65 20 6f 66 0a 20 20 2a 2a 20 74  stance of.  ** t
2456a 68 65 20 63 6f 6c 75 6d 6e 20 77 69 6c 6c 20 65  he column will e
2456b 76 65 72 20 62 65 20 75 73 65 64 20 62 79 20 74  ver be used by t
2456c 68 65 20 6f 70 74 69 6d 69 7a 65 72 2e 20 20 4e  he optimizer.  N
2456d 6f 74 65 20 74 68 61 74 20 75 73 69 6e 67 20 74  ote that using t
2456e 68 65 0a 20 20 2a 2a 20 73 61 6d 65 20 63 6f 6c  he.  ** same col
2456f 75 6d 6e 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e  umn more than on
24570 63 65 20 63 61 6e 6e 6f 74 20 62 65 20 61 6e 20  ce cannot be an 
24571 65 72 72 6f 72 20 62 65 63 61 75 73 65 20 74 68  error because th
24572 61 74 20 77 6f 75 6c 64 20 0a 20 20 2a 2a 20 62  at would .  ** b
24573 72 65 61 6b 20 62 61 63 6b 77 61 72 64 73 20 63  reak backwards c
24574 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 2d 20 69  ompatibility - i
24575 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 61 20  t needs to be a 
24576 77 61 72 6e 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20  warning..  */.  
24577 66 6f 72 28 69 3d 30 2c 20 70 4c 69 73 74 49 74  for(i=0, pListIt
24578 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3c 70  em=pList->a; i<p
24579 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b  List->nExpr; i++
2457a 2c 20 70 4c 69 73 74 49 74 65 6d 2b 2b 29 7b 0a  , pListItem++){.
2457b 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2457c 7a 43 6f 6c 4e 61 6d 65 20 3d 20 70 4c 69 73 74  zColName = pList
2457d 49 74 65 6d 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20  Item->zName;.   
2457e 20 43 6f 6c 75 6d 6e 20 2a 70 54 61 62 43 6f 6c   Column *pTabCol
2457f 3b 0a 20 20 20 20 69 6e 74 20 72 65 71 75 65 73  ;.    int reques
24580 74 65 64 53 6f 72 74 4f 72 64 65 72 3b 0a 20 20  tedSortOrder;.  
24581 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 20 20    char *zColl;  
24582 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24583 20 2f 2a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65   /* Collation se
24584 71 75 65 6e 63 65 20 6e 61 6d 65 20 2a 2f 0a 0a  quence name */..
24585 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 54 61      for(j=0, pTa
24586 62 43 6f 6c 3d 70 54 61 62 2d 3e 61 43 6f 6c 3b  bCol=pTab->aCol;
24587 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a   j<pTab->nCol; j
24588 2b 2b 2c 20 70 54 61 62 43 6f 6c 2b 2b 29 7b 0a  ++, pTabCol++){.
24589 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
2458a 33 53 74 72 49 43 6d 70 28 7a 43 6f 6c 4e 61 6d  3StrICmp(zColNam
2458b 65 2c 20 70 54 61 62 43 6f 6c 2d 3e 7a 4e 61 6d  e, pTabCol->zNam
2458c 65 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20  e)==0 ) break;. 
2458d 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6a 3e 3d     }.    if( j>=
2458e 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20  pTab->nCol ){.  
2458f 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
24590 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 61 62  Msg(pParse, "tab
24591 6c 65 20 25 73 20 68 61 73 20 6e 6f 20 63 6f 6c  le %s has no col
24592 75 6d 6e 20 6e 61 6d 65 64 20 25 73 22 2c 0a 20  umn named %s",. 
24593 20 20 20 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61         pTab->zNa
24594 6d 65 2c 20 7a 43 6f 6c 4e 61 6d 65 29 3b 0a 20  me, zColName);. 
24595 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63       goto exit_c
24596 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20  reate_index;.   
24597 20 7d 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e 61   }.    pIndex->a
24598 69 43 6f 6c 75 6d 6e 5b 69 5d 20 3d 20 6a 3b 0a  iColumn[i] = j;.
24599 20 20 20 20 2f 2a 20 4a 75 73 74 69 66 69 63 61      /* Justifica
2459a 74 69 6f 6e 20 6f 66 20 74 68 65 20 41 4c 57 41  tion of the ALWA
2459b 59 53 28 70 4c 69 73 74 49 74 65 6d 2d 3e 70 45  YS(pListItem->pE
2459c 78 70 72 2d 3e 70 43 6f 6c 6c 29 3a 20 20 42 65  xpr->pColl):  Be
2459d 63 61 75 73 65 20 6f 66 0a 20 20 20 20 2a 2a 20  cause of.    ** 
2459e 74 68 65 20 77 61 79 20 74 68 65 20 22 69 64 78  the way the "idx
2459f 6c 69 73 74 22 20 6e 6f 6e 2d 74 65 72 6d 69 6e  list" non-termin
245a0 61 6c 20 69 73 20 63 6f 6e 73 74 72 75 63 74 65  al is constructe
245a1 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 2c  d by the parser,
245a2 0a 20 20 20 20 2a 2a 20 69 66 20 70 4c 69 73 74  .    ** if pList
245a3 49 74 65 6d 2d 3e 70 45 78 70 72 20 69 73 20 6e  Item->pExpr is n
245a4 6f 74 20 6e 75 6c 6c 20 74 68 65 6e 20 65 69 74  ot null then eit
245a5 68 65 72 20 70 4c 69 73 74 49 74 65 6d 2d 3e 70  her pListItem->p
245a6 45 78 70 72 2d 3e 70 43 6f 6c 6c 0a 20 20 20 20  Expr->pColl.    
245a7 2a 2a 20 6d 75 73 74 20 65 78 69 73 74 20 6f 72  ** must exist or
245a8 20 65 6c 73 65 20 74 68 65 72 65 20 6d 75 73 74   else there must
245a9 20 68 61 76 65 20 62 65 65 6e 20 61 6e 20 4f 4f   have been an OO
245aa 4d 20 65 72 72 6f 72 2e 20 20 42 75 74 20 69 66  M error.  But if
245ab 20 74 68 65 72 65 0a 20 20 20 20 2a 2a 20 77 61   there.    ** wa
245ac 73 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 2c 20  s an OOM error, 
245ad 77 65 20 77 6f 75 6c 64 20 6e 65 76 65 72 20 72  we would never r
245ae 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74 2e  each this point.
245af 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4c 69 73   */.    if( pLis
245b0 74 49 74 65 6d 2d 3e 70 45 78 70 72 20 26 26 20  tItem->pExpr && 
245b1 41 4c 57 41 59 53 28 70 4c 69 73 74 49 74 65 6d  ALWAYS(pListItem
245b2 2d 3e 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 29 20  ->pExpr->pColl) 
245b3 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 43 6f  ){.      int nCo
245b4 6c 6c 3b 0a 20 20 20 20 20 20 7a 43 6f 6c 6c 20  ll;.      zColl 
245b5 3d 20 70 4c 69 73 74 49 74 65 6d 2d 3e 70 45 78  = pListItem->pEx
245b6 70 72 2d 3e 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65  pr->pColl->zName
245b7 3b 0a 20 20 20 20 20 20 6e 43 6f 6c 6c 20 3d 20  ;.      nColl = 
245b8 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
245b9 7a 43 6f 6c 6c 29 20 2b 20 31 3b 0a 20 20 20 20  zColl) + 1;.    
245ba 20 20 61 73 73 65 72 74 28 20 6e 45 78 74 72 61    assert( nExtra
245bb 3e 3d 6e 43 6f 6c 6c 20 29 3b 0a 20 20 20 20 20  >=nColl );.     
245bc 20 6d 65 6d 63 70 79 28 7a 45 78 74 72 61 2c 20   memcpy(zExtra, 
245bd 7a 43 6f 6c 6c 2c 20 6e 43 6f 6c 6c 29 3b 0a 20  zColl, nColl);. 
245be 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 7a 45 78       zColl = zEx
245bf 74 72 61 3b 0a 20 20 20 20 20 20 7a 45 78 74 72  tra;.      zExtr
245c0 61 20 2b 3d 20 6e 43 6f 6c 6c 3b 0a 20 20 20 20  a += nColl;.    
245c1 20 20 6e 45 78 74 72 61 20 2d 3d 20 6e 43 6f 6c    nExtra -= nCol
245c2 6c 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  l;.    }else{.  
245c3 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 70 54 61 62      zColl = pTab
245c4 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 43 6f 6c 6c 3b  ->aCol[j].zColl;
245c5 0a 20 20 20 20 20 20 69 66 28 20 21 7a 43 6f 6c  .      if( !zCol
245c6 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 43 6f  l ){.        zCo
245c7 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f  ll = db->pDfltCo
245c8 6c 6c 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20  ll->zName;.     
245c9 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
245ca 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20   !db->init.busy 
245cb 26 26 20 21 73 71 6c 69 74 65 33 4c 6f 63 61 74  && !sqlite3Locat
245cc 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  eCollSeq(pParse,
245cd 20 7a 43 6f 6c 6c 29 20 29 7b 0a 20 20 20 20 20   zColl) ){.     
245ce 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74   goto exit_creat
245cf 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20  e_index;.    }. 
245d0 20 20 20 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c     pIndex->azCol
245d1 6c 5b 69 5d 20 3d 20 7a 43 6f 6c 6c 3b 0a 20 20  l[i] = zColl;.  
245d2 20 20 72 65 71 75 65 73 74 65 64 53 6f 72 74 4f    requestedSortO
245d3 72 64 65 72 20 3d 20 70 4c 69 73 74 49 74 65 6d  rder = pListItem
245d4 2d 3e 73 6f 72 74 4f 72 64 65 72 20 26 20 73 6f  ->sortOrder & so
245d5 72 74 4f 72 64 65 72 4d 61 73 6b 3b 0a 20 20 20  rtOrderMask;.   
245d6 20 70 49 6e 64 65 78 2d 3e 61 53 6f 72 74 4f 72   pIndex->aSortOr
245d7 64 65 72 5b 69 5d 20 3d 20 28 75 38 29 72 65 71  der[i] = (u8)req
245d8 75 65 73 74 65 64 53 6f 72 74 4f 72 64 65 72 3b  uestedSortOrder;
245d9 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 65  .  }.  sqlite3De
245da 66 61 75 6c 74 52 6f 77 45 73 74 28 70 49 6e 64  faultRowEst(pInd
245db 65 78 29 3b 0a 0a 20 20 69 66 28 20 70 54 61 62  ex);..  if( pTab
245dc 3d 3d 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  ==pParse->pNewTa
245dd 62 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  ble ){.    /* Th
245de 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 62  is routine has b
245df 65 65 6e 20 63 61 6c 6c 65 64 20 74 6f 20 63 72  een called to cr
245e0 65 61 74 65 20 61 6e 20 61 75 74 6f 6d 61 74 69  eate an automati
245e1 63 20 69 6e 64 65 78 20 61 73 20 61 0a 20 20 20  c index as a.   
245e2 20 2a 2a 20 72 65 73 75 6c 74 20 6f 66 20 61 20   ** result of a 
245e3 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 55  PRIMARY KEY or U
245e4 4e 49 51 55 45 20 63 6c 61 75 73 65 20 6f 6e 20  NIQUE clause on 
245e5 61 20 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74  a column definit
245e6 69 6f 6e 2c 20 6f 72 0a 20 20 20 20 2a 2a 20 61  ion, or.    ** a
245e7 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20   PRIMARY KEY or 
245e8 55 4e 49 51 55 45 20 63 6c 61 75 73 65 20 66 6f  UNIQUE clause fo
245e9 6c 6c 6f 77 69 6e 67 20 74 68 65 20 63 6f 6c 75  llowing the colu
245ea 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e 0a  mn definitions..
245eb 20 20 20 20 2a 2a 20 69 2e 65 2e 20 6f 6e 65 20      ** i.e. one 
245ec 6f 66 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  of:.    **.    *
245ed 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74  * CREATE TABLE t
245ee 28 78 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20  (x PRIMARY KEY, 
245ef 79 29 3b 0a 20 20 20 20 2a 2a 20 43 52 45 41 54  y);.    ** CREAT
245f0 45 20 54 41 42 4c 45 20 74 28 78 2c 20 79 2c 20  E TABLE t(x, y, 
245f1 55 4e 49 51 55 45 28 78 2c 20 79 29 29 3b 0a 20  UNIQUE(x, y));. 
245f2 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 45 69 74     **.    ** Eit
245f3 68 65 72 20 77 61 79 2c 20 63 68 65 63 6b 20 74  her way, check t
245f4 6f 20 73 65 65 20 69 66 20 74 68 65 20 74 61 62  o see if the tab
245f5 6c 65 20 61 6c 72 65 61 64 79 20 68 61 73 20 73  le already has s
245f6 75 63 68 20 61 6e 20 69 6e 64 65 78 2e 20 49 66  uch an index. If
245f7 0a 20 20 20 20 2a 2a 20 73 6f 2c 20 64 6f 6e 27  .    ** so, don'
245f8 74 20 62 6f 74 68 65 72 20 63 72 65 61 74 69 6e  t bother creatin
245f9 67 20 74 68 69 73 20 6f 6e 65 2e 20 54 68 69 73  g this one. This
245fa 20 6f 6e 6c 79 20 61 70 70 6c 69 65 73 20 74 6f   only applies to
245fb 0a 20 20 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69  .    ** automati
245fc 63 61 6c 6c 79 20 63 72 65 61 74 65 64 20 69 6e  cally created in
245fd 64 69 63 65 73 2e 20 55 73 65 72 73 20 63 61 6e  dices. Users can
245fe 20 64 6f 20 61 73 20 74 68 65 79 20 77 69 73 68   do as they wish
245ff 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 65 78 70   with.    ** exp
24600 6c 69 63 69 74 20 69 6e 64 69 63 65 73 2e 0a 20  licit indices.. 
24601 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 77 6f     **.    ** Two
24602 20 55 4e 49 51 55 45 20 6f 72 20 50 52 49 4d 41   UNIQUE or PRIMA
24603 52 59 20 4b 45 59 20 63 6f 6e 73 74 72 61 69 6e  RY KEY constrain
24604 74 73 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65  ts are considere
24605 64 20 65 71 75 69 76 61 6c 65 6e 74 0a 20 20 20  d equivalent.   
24606 20 2a 2a 20 28 61 6e 64 20 74 68 75 73 20 73 75   ** (and thus su
24607 70 70 72 65 73 73 69 6e 67 20 74 68 65 20 73 65  ppressing the se
24608 63 6f 6e 64 20 6f 6e 65 29 20 65 76 65 6e 20 69  cond one) even i
24609 66 20 74 68 65 79 20 68 61 76 65 20 64 69 66 66  f they have diff
2460a 65 72 65 6e 74 0a 20 20 20 20 2a 2a 20 73 6f 72  erent.    ** sor
2460b 74 20 6f 72 64 65 72 73 2e 0a 20 20 20 20 2a 2a  t orders..    **
2460c 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 72 65  .    ** If there
2460d 20 61 72 65 20 64 69 66 66 65 72 65 6e 74 20 63   are different c
2460e 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
2460f 65 73 20 6f 72 20 69 66 20 74 68 65 20 63 6f 6c  es or if the col
24610 75 6d 6e 73 20 6f 66 0a 20 20 20 20 2a 2a 20 74  umns of.    ** t
24611 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f 63  he constraint oc
24612 63 75 72 20 69 6e 20 64 69 66 66 65 72 65 6e 74  cur in different
24613 20 6f 72 64 65 72 73 2c 20 74 68 65 6e 20 74 68   orders, then th
24614 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 72  e constraints ar
24615 65 0a 20 20 20 20 2a 2a 20 63 6f 6e 73 69 64 65  e.    ** conside
24616 72 65 64 20 64 69 73 74 69 6e 63 74 20 61 6e 64  red distinct and
24617 20 62 6f 74 68 20 72 65 73 75 6c 74 20 69 6e 20   both result in 
24618 73 65 70 61 72 61 74 65 20 69 6e 64 69 63 65 73  separate indices
24619 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 49 6e 64  ..    */.    Ind
2461a 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 66 6f  ex *pIdx;.    fo
2461b 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  r(pIdx=pTab->pIn
2461c 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
2461d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  pIdx->pNext){.  
2461e 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20 20 20 20      int k;.     
2461f 20 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e 6f   assert( pIdx->o
24620 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20  nError!=OE_None 
24621 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
24622 20 70 49 64 78 2d 3e 61 75 74 6f 49 6e 64 65 78   pIdx->autoIndex
24623 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
24624 28 20 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f  ( pIndex->onErro
24625 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29 3b 0a 0a 20  r!=OE_None );.. 
24626 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 6e       if( pIdx->n
24627 43 6f 6c 75 6d 6e 21 3d 70 49 6e 64 65 78 2d 3e  Column!=pIndex->
24628 6e 43 6f 6c 75 6d 6e 20 29 20 63 6f 6e 74 69 6e  nColumn ) contin
24629 75 65 3b 0a 20 20 20 20 20 20 66 6f 72 28 6b 3d  ue;.      for(k=
2462a 30 3b 20 6b 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75  0; k<pIdx->nColu
2462b 6d 6e 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 20 20  mn; k++){.      
2462c 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 31    const char *z1
2462d 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20  ;.        const 
2462e 63 68 61 72 20 2a 7a 32 3b 0a 20 20 20 20 20 20  char *z2;.      
2462f 20 20 69 66 28 20 70 49 64 78 2d 3e 61 69 43 6f    if( pIdx->aiCo
24630 6c 75 6d 6e 5b 6b 5d 21 3d 70 49 6e 64 65 78 2d  lumn[k]!=pIndex-
24631 3e 61 69 43 6f 6c 75 6d 6e 5b 6b 5d 20 29 20 62  >aiColumn[k] ) b
24632 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7a 31  reak;.        z1
24633 20 3d 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b   = pIdx->azColl[
24634 6b 5d 3b 0a 20 20 20 20 20 20 20 20 7a 32 20 3d  k];.        z2 =
24635 20 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 5b   pIndex->azColl[
24636 6b 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  k];.        if( 
24637 7a 31 21 3d 7a 32 20 26 26 20 73 71 6c 69 74 65  z1!=z2 && sqlite
24638 33 53 74 72 49 43 6d 70 28 7a 31 2c 20 7a 32 29  3StrICmp(z1, z2)
24639 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
2463a 7d 0a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 70  }.      if( k==p
2463b 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a  Idx->nColumn ){.
2463c 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64 78          if( pIdx
2463d 2d 3e 6f 6e 45 72 72 6f 72 21 3d 70 49 6e 64 65  ->onError!=pInde
2463e 78 2d 3e 6f 6e 45 72 72 6f 72 20 29 7b 0a 20 20  x->onError ){.  
2463f 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20          /* This 
24640 63 6f 6e 73 74 72 61 69 6e 74 20 63 72 65 61 74  constraint creat
24641 65 73 20 74 68 65 20 73 61 6d 65 20 69 6e 64 65  es the same inde
24642 78 20 61 73 20 61 20 70 72 65 76 69 6f 75 73 0a  x as a previous.
24643 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e            ** con
24644 73 74 72 61 69 6e 74 20 73 70 65 63 69 66 69 65  straint specifie
24645 64 20 73 6f 6d 65 77 68 65 72 65 20 69 6e 20 74  d somewhere in t
24646 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20  he CREATE TABLE 
24647 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20 20 20  statement..     
24648 20 20 20 20 20 2a 2a 20 48 6f 77 65 76 65 72 20       ** However 
24649 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20  the ON CONFLICT 
2464a 63 6c 61 75 73 65 73 20 61 72 65 20 64 69 66 66  clauses are diff
2464b 65 72 65 6e 74 2e 20 49 66 20 62 6f 74 68 20 74  erent. If both t
2464c 68 69 73 20 0a 20 20 20 20 20 20 20 20 20 20 2a  his .          *
2464d 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 61 6e 64  * constraint and
2464e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 65 71   the previous eq
2464f 75 69 76 61 6c 65 6e 74 20 63 6f 6e 73 74 72 61  uivalent constra
24650 69 6e 74 20 68 61 76 65 20 65 78 70 6c 69 63 69  int have explici
24651 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 4f  t.          ** O
24652 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c 61 75 73  N CONFLICT claus
24653 65 73 20 74 68 69 73 20 69 73 20 61 6e 20 65 72  es this is an er
24654 72 6f 72 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ror. Otherwise, 
24655 75 73 65 20 74 68 65 0a 20 20 20 20 20 20 20 20  use the.        
24656 20 20 2a 2a 20 65 78 70 6c 69 63 69 74 6c 79 20    ** explicitly 
24657 73 70 65 63 69 66 69 65 64 20 62 65 68 61 76 69  specified behavi
24658 6f 75 72 20 66 6f 72 20 74 68 65 20 69 6e 64 65  our for the inde
24659 78 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  x..          */.
2465a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 28            if( !(
2465b 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f  pIdx->onError==O
2465c 45 5f 44 65 66 61 75 6c 74 20 7c 7c 20 70 49 6e  E_Default || pIn
2465d 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45  dex->onError==OE
2465e 5f 44 65 66 61 75 6c 74 29 20 29 7b 0a 20 20 20  _Default) ){.   
2465f 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
24660 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
24661 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
24662 20 20 22 63 6f 6e 66 6c 69 63 74 69 6e 67 20 4f    "conflicting O
24663 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c 61 75 73  N CONFLICT claus
24664 65 73 20 73 70 65 63 69 66 69 65 64 22 2c 20 30  es specified", 0
24665 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  );.          }. 
24666 20 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64           if( pId
24667 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44  x->onError==OE_D
24668 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20  efault ){.      
24669 20 20 20 20 20 20 70 49 64 78 2d 3e 6f 6e 45 72        pIdx->onEr
2466a 72 6f 72 20 3d 20 70 49 6e 64 65 78 2d 3e 6f 6e  ror = pIndex->on
2466b 45 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 20  Error;.         
2466c 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
2466d 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63       goto exit_c
2466e 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20  reate_index;.   
2466f 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
24670 20 20 2f 2a 20 4c 69 6e 6b 20 74 68 65 20 6e 65    /* Link the ne
24671 77 20 49 6e 64 65 78 20 73 74 72 75 63 74 75 72  w Index structur
24672 65 20 74 6f 20 69 74 73 20 74 61 62 6c 65 20 61  e to its table a
24673 6e 64 20 74 6f 20 74 68 65 20 6f 74 68 65 72 0a  nd to the other.
24674 20 20 2a 2a 20 69 6e 2d 6d 65 6d 6f 72 79 20 64    ** in-memory d
24675 61 74 61 62 61 73 65 20 73 74 72 75 63 74 75 72  atabase structur
24676 65 73 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20  es. .  */.  if( 
24677 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b  db->init.busy ){
24678 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 3b 0a 20  .    Index *p;. 
24679 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 48 61     p = sqlite3Ha
2467a 73 68 49 6e 73 65 72 74 28 26 70 49 6e 64 65 78  shInsert(&pIndex
2467b 2d 3e 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61  ->pSchema->idxHa
2467c 73 68 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  sh, .           
2467d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
2467e 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 73 71  Index->zName, sq
2467f 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 49  lite3Strlen30(pI
24680 6e 64 65 78 2d 3e 7a 4e 61 6d 65 29 2c 0a 20 20  ndex->zName),.  
24681 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24682 20 20 20 20 20 20 20 20 70 49 6e 64 65 78 29 3b          pIndex);
24683 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20  .    if( p ){.  
24684 20 20 20 20 61 73 73 65 72 74 28 20 70 3d 3d 70      assert( p==p
24685 49 6e 64 65 78 20 29 3b 20 20 2f 2a 20 4d 61 6c  Index );  /* Mal
24686 6c 6f 63 20 6d 75 73 74 20 68 61 76 65 20 66 61  loc must have fa
24687 69 6c 65 64 20 2a 2f 0a 20 20 20 20 20 20 64 62  iled */.      db
24688 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
24689 20 31 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65   1;.      goto e
2468a 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78  xit_create_index
2468b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e  ;.    }.    db->
2468c 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f  flags |= SQLITE_
2468d 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 20  InternChanges;. 
2468e 20 20 20 69 66 28 20 70 54 62 6c 4e 61 6d 65 21     if( pTblName!
2468f 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 49 6e 64  =0 ){.      pInd
24690 65 78 2d 3e 74 6e 75 6d 20 3d 20 64 62 2d 3e 69  ex->tnum = db->i
24691 6e 69 74 2e 6e 65 77 54 6e 75 6d 3b 0a 20 20 20  nit.newTnum;.   
24692 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20   }.  }..  /* If 
24693 74 68 65 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73  the db->init.bus
24694 79 20 69 73 20 30 20 74 68 65 6e 20 63 72 65 61  y is 0 then crea
24695 74 65 20 74 68 65 20 69 6e 64 65 78 20 6f 6e 20  te the index on 
24696 64 69 73 6b 2e 20 20 54 68 69 73 0a 20 20 2a 2a  disk.  This.  **
24697 20 69 6e 76 6f 6c 76 65 73 20 77 72 69 74 69 6e   involves writin
24698 67 20 74 68 65 20 69 6e 64 65 78 20 69 6e 74 6f  g the index into
24699 20 74 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c   the master tabl
2469a 65 20 61 6e 64 20 66 69 6c 6c 69 6e 67 20 69 6e  e and filling in
2469b 20 74 68 65 0a 20 20 2a 2a 20 69 6e 64 65 78 20   the.  ** index 
2469c 77 69 74 68 20 74 68 65 20 63 75 72 72 65 6e 74  with the current
2469d 20 74 61 62 6c 65 20 63 6f 6e 74 65 6e 74 73 2e   table contents.
2469e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 64  .  **.  ** The d
2469f 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 69 73 20  b->init.busy is 
246a0 30 20 77 68 65 6e 20 74 68 65 20 75 73 65 72 20  0 when the user 
246a1 66 69 72 73 74 20 65 6e 74 65 72 73 20 61 20 43  first enters a C
246a2 52 45 41 54 45 20 49 4e 44 45 58 20 0a 20 20 2a  REATE INDEX .  *
246a3 2a 20 63 6f 6d 6d 61 6e 64 2e 20 20 64 62 2d 3e  * command.  db->
246a4 69 6e 69 74 2e 62 75 73 79 20 69 73 20 31 20 77  init.busy is 1 w
246a5 68 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 69  hen a database i
246a6 73 20 6f 70 65 6e 65 64 20 61 6e 64 20 0a 20 20  s opened and .  
246a7 2a 2a 20 43 52 45 41 54 45 20 49 4e 44 45 58 20  ** CREATE INDEX 
246a8 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 72  statements are r
246a9 65 61 64 20 6f 75 74 20 6f 66 20 74 68 65 20 6d  ead out of the m
246aa 61 73 74 65 72 20 74 61 62 6c 65 2e 20 20 49 6e  aster table.  In
246ab 0a 20 20 2a 2a 20 74 68 65 20 6c 61 74 74 65 72  .  ** the latter
246ac 20 63 61 73 65 20 74 68 65 20 69 6e 64 65 78 20   case the index 
246ad 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 6f  already exists o
246ae 6e 20 64 69 73 6b 2c 20 77 68 69 63 68 20 69 73  n disk, which is
246af 20 77 68 79 0a 20 20 2a 2a 20 77 65 20 64 6f 6e   why.  ** we don
246b0 27 74 20 77 61 6e 74 20 74 6f 20 72 65 63 72 65  't want to recre
246b1 61 74 65 20 69 74 2e 0a 20 20 2a 2a 0a 20 20 2a  ate it..  **.  *
246b2 2a 20 49 66 20 70 54 62 6c 4e 61 6d 65 3d 3d 30  * If pTblName==0
246b3 20 69 74 20 6d 65 61 6e 73 20 74 68 69 73 20 69   it means this i
246b4 6e 64 65 78 20 69 73 20 67 65 6e 65 72 61 74 65  ndex is generate
246b5 64 20 61 73 20 61 20 70 72 69 6d 61 72 79 20 6b  d as a primary k
246b6 65 79 0a 20 20 2a 2a 20 6f 72 20 55 4e 49 51 55  ey.  ** or UNIQU
246b7 45 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f 66 20  E constraint of 
246b8 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73  a CREATE TABLE s
246b9 74 61 74 65 6d 65 6e 74 2e 20 20 53 69 6e 63 65  tatement.  Since
246ba 20 74 68 65 20 74 61 62 6c 65 0a 20 20 2a 2a 20   the table.  ** 
246bb 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 63 72  has just been cr
246bc 65 61 74 65 64 2c 20 69 74 20 63 6f 6e 74 61 69  eated, it contai
246bd 6e 73 20 6e 6f 20 64 61 74 61 20 61 6e 64 20 74  ns no data and t
246be 68 65 20 69 6e 64 65 78 20 69 6e 69 74 69 61 6c  he index initial
246bf 69 7a 61 74 69 6f 6e 0a 20 20 2a 2a 20 73 74 65  ization.  ** ste
246c0 70 20 63 61 6e 20 62 65 20 73 6b 69 70 70 65 64  p can be skipped
246c1 2e 0a 20 20 2a 2f 0a 20 20 65 6c 73 65 7b 20 2f  ..  */.  else{ /
246c2 2a 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62  * if( db->init.b
246c3 75 73 79 3d 3d 30 20 29 20 2a 2f 0a 20 20 20 20  usy==0 ) */.    
246c4 56 64 62 65 20 2a 76 3b 0a 20 20 20 20 63 68 61  Vdbe *v;.    cha
246c5 72 20 2a 7a 53 74 6d 74 3b 0a 20 20 20 20 69 6e  r *zStmt;.    in
246c6 74 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 73  t iMem = ++pPars
246c7 65 2d 3e 6e 4d 65 6d 3b 0a 0a 20 20 20 20 76 20  e->nMem;..    v 
246c8 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
246c9 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69 66  (pParse);.    if
246ca 28 20 76 3d 3d 30 20 29 20 67 6f 74 6f 20 65 78  ( v==0 ) goto ex
246cb 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b  it_create_index;
246cc 0a 0a 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65  ...    /* Create
246cd 20 74 68 65 20 72 6f 6f 74 70 61 67 65 20 66 6f   the rootpage fo
246ce 72 20 74 68 65 20 69 6e 64 65 78 0a 20 20 20 20  r the index.    
246cf 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65  */.    sqlite3Be
246d0 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f  ginWriteOperatio
246d1 6e 28 70 50 61 72 73 65 2c 20 31 2c 20 69 44 62  n(pParse, 1, iDb
246d2 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
246d3 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43  beAddOp2(v, OP_C
246d4 72 65 61 74 65 49 6e 64 65 78 2c 20 69 44 62 2c  reateIndex, iDb,
246d5 20 69 4d 65 6d 29 3b 0a 0a 20 20 20 20 2f 2a 20   iMem);..    /* 
246d6 47 61 74 68 65 72 20 74 68 65 20 63 6f 6d 70 6c  Gather the compl
246d7 65 74 65 20 74 65 78 74 20 6f 66 20 74 68 65 20  ete text of the 
246d8 43 52 45 41 54 45 20 49 4e 44 45 58 20 73 74 61  CREATE INDEX sta
246d9 74 65 6d 65 6e 74 20 69 6e 74 6f 0a 20 20 20 20  tement into.    
246da 2a 2a 20 74 68 65 20 7a 53 74 6d 74 20 76 61 72  ** the zStmt var
246db 69 61 62 6c 65 0a 20 20 20 20 2a 2f 0a 20 20 20  iable.    */.   
246dc 20 69 66 28 20 70 53 74 61 72 74 20 29 7b 0a 20   if( pStart ){. 
246dd 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 6e       assert( pEn
246de 64 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 2f 2a  d!=0 );.      /*
246df 20 41 20 6e 61 6d 65 64 20 69 6e 64 65 78 20 77   A named index w
246e0 69 74 68 20 61 6e 20 65 78 70 6c 69 63 69 74 20  ith an explicit 
246e1 43 52 45 41 54 45 20 49 4e 44 45 58 20 73 74 61  CREATE INDEX sta
246e2 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 20 20  tement */.      
246e3 7a 53 74 6d 74 20 3d 20 73 71 6c 69 74 65 33 4d  zStmt = sqlite3M
246e4 50 72 69 6e 74 66 28 64 62 2c 20 22 43 52 45 41  Printf(db, "CREA
246e5 54 45 25 73 20 49 4e 44 45 58 20 25 2e 2a 73 22  TE%s INDEX %.*s"
246e6 2c 0a 20 20 20 20 20 20 20 20 6f 6e 45 72 72 6f  ,.        onErro
246e7 72 3d 3d 4f 45 5f 4e 6f 6e 65 20 3f 20 22 22 20  r==OE_None ? "" 
246e8 3a 20 22 20 55 4e 49 51 55 45 22 2c 0a 20 20 20  : " UNIQUE",.   
246e9 20 20 20 20 20 70 45 6e 64 2d 3e 7a 20 2d 20 70       pEnd->z - p
246ea 4e 61 6d 65 2d 3e 7a 20 2b 20 31 2c 0a 20 20 20  Name->z + 1,.   
246eb 20 20 20 20 20 70 4e 61 6d 65 2d 3e 7a 29 3b 0a       pName->z);.
246ec 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
246ed 20 2f 2a 20 41 6e 20 61 75 74 6f 6d 61 74 69 63   /* An automatic
246ee 20 69 6e 64 65 78 20 63 72 65 61 74 65 64 20 62   index created b
246ef 79 20 61 20 50 52 49 4d 41 52 59 20 4b 45 59 20  y a PRIMARY KEY 
246f0 6f 72 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72  or UNIQUE constr
246f1 61 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 2f 2a  aint */.      /*
246f2 20 7a 53 74 6d 74 20 3d 20 73 71 6c 69 74 65 33   zStmt = sqlite3
246f3 4d 50 72 69 6e 74 66 28 22 22 29 3b 20 2a 2f 0a  MPrintf(""); */.
246f4 20 20 20 20 20 20 7a 53 74 6d 74 20 3d 20 30 3b        zStmt = 0;
246f5 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41  .    }..    /* A
246f6 64 64 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 73  dd an entry in s
246f7 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 66 6f 72  qlite_master for
246f8 20 74 68 69 73 20 69 6e 64 65 78 0a 20 20 20 20   this index.    
246f9 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65  */.    sqlite3Ne
246fa 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65  stedParse(pParse
246fb 2c 20 0a 20 20 20 20 20 20 20 20 22 49 4e 53 45  , .        "INSE
246fc 52 54 20 49 4e 54 4f 20 25 51 2e 25 73 20 56 41  RT INTO %Q.%s VA
246fd 4c 55 45 53 28 27 69 6e 64 65 78 27 2c 25 51 2c  LUES('index',%Q,
246fe 25 51 2c 23 25 64 2c 25 51 29 3b 22 2c 0a 20 20  %Q,#%d,%Q);",.  
246ff 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44        db->aDb[iD
24700 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41  b].zName, SCHEMA
24701 5f 54 41 42 4c 45 28 69 44 62 29 2c 0a 20 20 20  _TABLE(iDb),.   
24702 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61       pIndex->zNa
24703 6d 65 2c 0a 20 20 20 20 20 20 20 20 70 54 61 62  me,.        pTab
24704 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20  ->zName,.       
24705 20 69 4d 65 6d 2c 0a 20 20 20 20 20 20 20 20 7a   iMem,.        z
24706 53 74 6d 74 0a 20 20 20 20 29 3b 0a 20 20 20 20  Stmt.    );.    
24707 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
24708 2c 20 7a 53 74 6d 74 29 3b 0a 0a 20 20 20 20 2f  , zStmt);..    /
24709 2a 20 46 69 6c 6c 20 74 68 65 20 69 6e 64 65 78  * Fill the index
2470a 20 77 69 74 68 20 64 61 74 61 20 61 6e 64 20 72   with data and r
2470b 65 70 61 72 73 65 20 74 68 65 20 73 63 68 65 6d  eparse the schem
2470c 61 2e 20 43 6f 64 65 20 61 6e 20 4f 50 5f 45 78  a. Code an OP_Ex
2470d 70 69 72 65 0a 20 20 20 20 2a 2a 20 74 6f 20 69  pire.    ** to i
2470e 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 70 72  nvalidate all pr
2470f 65 2d 63 6f 6d 70 69 6c 65 64 20 73 74 61 74 65  e-compiled state
24710 6d 65 6e 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  ments..    */.  
24711 20 20 69 66 28 20 70 54 62 6c 4e 61 6d 65 20 29    if( pTblName )
24712 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  {.      sqlite3R
24713 65 66 69 6c 6c 49 6e 64 65 78 28 70 50 61 72 73  efillIndex(pPars
24714 65 2c 20 70 49 6e 64 65 78 2c 20 69 4d 65 6d 29  e, pIndex, iMem)
24715 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 43  ;.      sqlite3C
24716 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72  hangeCookie(pPar
24717 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20  se, iDb);.      
24718 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
24719 34 28 76 2c 20 4f 50 5f 50 61 72 73 65 53 63 68  4(v, OP_ParseSch
2471a 65 6d 61 2c 20 69 44 62 2c 20 30 2c 20 30 2c 0a  ema, iDb, 0, 0,.
2471b 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2471c 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 61 6d  MPrintf(db, "nam
2471d 65 3d 27 25 71 27 22 2c 20 70 49 6e 64 65 78 2d  e='%q'", pIndex-
2471e 3e 7a 4e 61 6d 65 29 2c 20 50 34 5f 44 59 4e 41  >zName), P4_DYNA
2471f 4d 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  MIC);.      sqli
24720 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
24721 20 4f 50 5f 45 78 70 69 72 65 2c 20 30 29 3b 0a   OP_Expire, 0);.
24722 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
24723 57 68 65 6e 20 61 64 64 69 6e 67 20 61 6e 20 69  When adding an i
24724 6e 64 65 78 20 74 6f 20 74 68 65 20 6c 69 73 74  ndex to the list
24725 20 6f 66 20 69 6e 64 69 63 65 73 20 66 6f 72 20   of indices for 
24726 61 20 74 61 62 6c 65 2c 20 6d 61 6b 65 0a 20 20  a table, make.  
24727 2a 2a 20 73 75 72 65 20 61 6c 6c 20 69 6e 64 69  ** sure all indi
24728 63 65 73 20 6c 61 62 65 6c 65 64 20 4f 45 5f 52  ces labeled OE_R
24729 65 70 6c 61 63 65 20 63 6f 6d 65 20 61 66 74 65  eplace come afte
2472a 72 20 61 6c 6c 20 74 68 6f 73 65 20 6c 61 62 65  r all those labe
2472b 6c 65 64 0a 20 20 2a 2a 20 4f 45 5f 49 67 6e 6f  led.  ** OE_Igno
2472c 72 65 2e 20 20 54 68 69 73 20 69 73 20 6e 65 63  re.  This is nec
2472d 65 73 73 61 72 79 20 66 6f 72 20 74 68 65 20 63  essary for the c
2472e 6f 72 72 65 63 74 20 63 6f 6e 73 74 72 61 69 6e  orrect constrain
2472f 74 20 63 68 65 63 6b 0a 20 20 2a 2a 20 70 72 6f  t check.  ** pro
24730 63 65 73 73 69 6e 67 20 28 69 6e 20 73 71 6c 69  cessing (in sqli
24731 74 65 33 47 65 6e 65 72 61 74 65 43 6f 6e 73 74  te3GenerateConst
24732 72 61 69 6e 74 43 68 65 63 6b 73 28 29 29 20 61  raintChecks()) a
24733 73 20 70 61 72 74 20 6f 66 0a 20 20 2a 2a 20 55  s part of.  ** U
24734 50 44 41 54 45 20 61 6e 64 20 49 4e 53 45 52 54  PDATE and INSERT
24735 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 0a 20   statements.  . 
24736 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e   */.  if( db->in
24737 69 74 2e 62 75 73 79 20 7c 7c 20 70 54 62 6c 4e  it.busy || pTblN
24738 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66  ame==0 ){.    if
24739 28 20 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 52 65  ( onError!=OE_Re
2473a 70 6c 61 63 65 20 7c 7c 20 70 54 61 62 2d 3e 70  place || pTab->p
2473b 49 6e 64 65 78 3d 3d 30 0a 20 20 20 20 20 20 20  Index==0.       
2473c 20 20 7c 7c 20 70 54 61 62 2d 3e 70 49 6e 64 65    || pTab->pInde
2473d 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52  x->onError==OE_R
2473e 65 70 6c 61 63 65 29 7b 0a 20 20 20 20 20 20 70  eplace){.      p
2473f 49 6e 64 65 78 2d 3e 70 4e 65 78 74 20 3d 20 70  Index->pNext = p
24740 54 61 62 2d 3e 70 49 6e 64 65 78 3b 0a 20 20 20  Tab->pIndex;.   
24741 20 20 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 20     pTab->pIndex 
24742 3d 20 70 49 6e 64 65 78 3b 0a 20 20 20 20 7d 65  = pIndex;.    }e
24743 6c 73 65 7b 0a 20 20 20 20 20 20 49 6e 64 65 78  lse{.      Index
24744 20 2a 70 4f 74 68 65 72 20 3d 20 70 54 61 62 2d   *pOther = pTab-
24745 3e 70 49 6e 64 65 78 3b 0a 20 20 20 20 20 20 77  >pIndex;.      w
24746 68 69 6c 65 28 20 70 4f 74 68 65 72 2d 3e 70 4e  hile( pOther->pN
24747 65 78 74 20 26 26 20 70 4f 74 68 65 72 2d 3e 70  ext && pOther->p
24748 4e 65 78 74 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f  Next->onError!=O
24749 45 5f 52 65 70 6c 61 63 65 20 29 7b 0a 20 20 20  E_Replace ){.   
2474a 20 20 20 20 20 70 4f 74 68 65 72 20 3d 20 70 4f       pOther = pO
2474b 74 68 65 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  ther->pNext;.   
2474c 20 20 20 7d 0a 20 20 20 20 20 20 70 49 6e 64 65     }.      pInde
2474d 78 2d 3e 70 4e 65 78 74 20 3d 20 70 4f 74 68 65  x->pNext = pOthe
2474e 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20  r->pNext;.      
2474f 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 20 3d 20  pOther->pNext = 
24750 70 49 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20  pIndex;.    }.  
24751 20 20 70 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20    pIndex = 0;.  
24752 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e 20 75 70  }..  /* Clean up
24753 20 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20   before exiting 
24754 2a 2f 0a 65 78 69 74 5f 63 72 65 61 74 65 5f 69  */.exit_create_i
24755 6e 64 65 78 3a 0a 20 20 69 66 28 20 70 49 6e 64  ndex:.  if( pInd
24756 65 78 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ex ){.    sqlite
24757 33 5f 66 72 65 65 28 70 49 6e 64 65 78 2d 3e 7a  3_free(pIndex->z
24758 43 6f 6c 41 66 66 29 3b 0a 20 20 20 20 73 71 6c  ColAff);.    sql
24759 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
2475a 49 6e 64 65 78 29 3b 0a 20 20 7d 0a 20 20 73 71  Index);.  }.  sq
2475b 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c  lite3ExprListDel
2475c 65 74 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a  ete(db, pList);.
2475d 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74    sqlite3SrcList
2475e 44 65 6c 65 74 65 28 64 62 2c 20 70 54 62 6c 4e  Delete(db, pTblN
2475f 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  ame);.  sqlite3D
24760 62 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29  bFree(db, zName)
24761 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f  ;.  return;.}../
24762 2a 0a 2a 2a 20 46 69 6c 6c 20 74 68 65 20 49 6e  *.** Fill the In
24763 64 65 78 2e 61 69 52 6f 77 45 73 74 5b 5d 20 61  dex.aiRowEst[] a
24764 72 72 61 79 20 77 69 74 68 20 64 65 66 61 75 6c  rray with defaul
24765 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2d 20  t information - 
24766 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 74  information.** t
24767 6f 20 62 65 20 75 73 65 64 20 77 68 65 6e 20 77  o be used when w
24768 65 20 68 61 76 65 20 6e 6f 74 20 72 75 6e 20 74  e have not run t
24769 68 65 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61  he ANALYZE comma
2476a 6e 64 2e 0a 2a 2a 0a 2a 2a 20 61 69 52 6f 77 45  nd..**.** aiRowE
2476b 73 74 5b 30 5d 20 69 73 20 73 75 70 70 6f 73 65  st[0] is suppose
2476c 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65 20   to contain the 
2476d 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e  number of elemen
2476e 74 73 20 69 6e 20 74 68 65 20 69 6e 64 65 78 2e  ts in the index.
2476f 0a 2a 2a 20 53 69 6e 63 65 20 77 65 20 64 6f 20  .** Since we do 
24770 6e 6f 74 20 6b 6e 6f 77 2c 20 67 75 65 73 73 20  not know, guess 
24771 31 20 6d 69 6c 6c 69 6f 6e 2e 20 20 61 69 52 6f  1 million.  aiRo
24772 77 45 73 74 5b 31 5d 20 69 73 20 61 6e 20 65 73  wEst[1] is an es
24773 74 69 6d 61 74 65 20 6f 66 20 74 68 65 0a 2a 2a  timate of the.**
24774 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20   number of rows 
24775 69 6e 20 74 68 65 20 74 61 62 6c 65 20 74 68 61  in the table tha
24776 74 20 6d 61 74 63 68 20 61 6e 79 20 70 61 72 74  t match any part
24777 69 63 75 6c 61 72 20 76 61 6c 75 65 20 6f 66 20  icular value of 
24778 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 63 6f 6c  the.** first col
24779 75 6d 6e 20 6f 66 20 74 68 65 20 69 6e 64 65 78  umn of the index
2477a 2e 20 20 61 69 52 6f 77 45 73 74 5b 32 5d 20 69  .  aiRowEst[2] i
2477b 73 20 61 6e 20 65 73 74 69 6d 61 74 65 20 6f 66  s an estimate of
2477c 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f   the number.** o
2477d 66 20 72 6f 77 73 20 74 68 61 74 20 6d 61 74 63  f rows that matc
2477e 68 20 61 6e 79 20 70 61 72 74 69 63 75 6c 61 72  h any particular
2477f 20 63 6f 6d 62 69 6e 69 61 74 69 6f 6e 20 6f 66   combiniation of
24780 20 74 68 65 20 66 69 72 73 74 20 32 20 63 6f 6c   the first 2 col
24781 75 6d 6e 73 0a 2a 2a 20 6f 66 20 74 68 65 20 69  umns.** of the i
24782 6e 64 65 78 2e 20 20 41 6e 64 20 73 6f 20 66 6f  ndex.  And so fo
24783 72 74 68 2e 20 20 49 74 20 6d 75 73 74 20 61 6c  rth.  It must al
24784 77 61 79 73 20 62 65 20 74 68 65 20 63 61 73 65  ways be the case
24785 20 74 68 61 74 0a 2a 0a 2a 2a 20 20 20 20 20 20   that.*.**      
24786 20 20 20 20 20 61 69 52 6f 77 45 73 74 5b 4e 5d       aiRowEst[N]
24787 3c 3d 61 69 52 6f 77 45 73 74 5b 4e 2d 31 5d 0a  <=aiRowEst[N-1].
24788 2a 2a 20 20 20 20 20 20 20 20 20 20 20 61 69 52  **           aiR
24789 6f 77 45 73 74 5b 4e 5d 3e 3d 31 0a 2a 2a 0a 2a  owEst[N]>=1.**.*
2478a 2a 20 41 70 61 72 74 20 66 72 6f 6d 20 74 68 61  * Apart from tha
2478b 74 2c 20 77 65 20 68 61 76 65 20 6c 69 74 74 6c  t, we have littl
2478c 65 20 74 6f 20 67 6f 20 6f 6e 20 62 65 73 69 64  e to go on besid
2478d 65 73 20 69 6e 74 75 69 74 69 6f 6e 20 61 73 20  es intuition as 
2478e 74 6f 0a 2a 2a 20 68 6f 77 20 61 69 52 6f 77 45  to.** how aiRowE
2478f 73 74 5b 5d 20 73 68 6f 75 6c 64 20 62 65 20 69  st[] should be i
24790 6e 69 74 69 61 6c 69 7a 65 64 2e 20 20 54 68 65  nitialized.  The
24791 20 6e 75 6d 62 65 72 73 20 67 65 6e 65 72 61 74   numbers generat
24792 65 64 20 68 65 72 65 0a 2a 2a 20 61 72 65 20 62  ed here.** are b
24793 61 73 65 64 20 6f 6e 20 74 79 70 69 63 61 6c 20  ased on typical 
24794 76 61 6c 75 65 73 20 66 6f 75 6e 64 20 69 6e 20  values found in 
24795 61 63 74 75 61 6c 20 69 6e 64 69 63 65 73 2e 0a  actual indices..
24796 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
24797 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65  E void sqlite3De
24798 66 61 75 6c 74 52 6f 77 45 73 74 28 49 6e 64 65  faultRowEst(Inde
24799 78 20 2a 70 49 64 78 29 7b 0a 20 20 75 6e 73 69  x *pIdx){.  unsi
2479a 67 6e 65 64 20 2a 61 20 3d 20 70 49 64 78 2d 3e  gned *a = pIdx->
2479b 61 69 52 6f 77 45 73 74 3b 0a 20 20 69 6e 74 20  aiRowEst;.  int 
2479c 69 3b 0a 20 20 61 73 73 65 72 74 28 20 61 21 3d  i;.  assert( a!=
2479d 30 20 29 3b 0a 20 20 61 5b 30 5d 20 3d 20 31 30  0 );.  a[0] = 10
2479e 30 30 30 30 30 3b 0a 20 20 66 6f 72 28 69 3d 70  00000;.  for(i=p
2479f 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 3e  Idx->nColumn; i>
247a0 3d 35 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 61 5b  =5; i--){.    a[
247a1 69 5d 20 3d 20 35 3b 0a 20 20 7d 0a 20 20 77 68  i] = 5;.  }.  wh
247a2 69 6c 65 28 20 69 3e 3d 31 20 29 7b 0a 20 20 20  ile( i>=1 ){.   
247a3 20 61 5b 69 5d 20 3d 20 31 31 20 2d 20 69 3b 0a   a[i] = 11 - i;.
247a4 20 20 20 20 69 2d 2d 3b 0a 20 20 7d 0a 20 20 69      i--;.  }.  i
247a5 66 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72  f( pIdx->onError
247a6 21 3d 4f 45 5f 4e 6f 6e 65 20 29 7b 0a 20 20 20  !=OE_None ){.   
247a7 20 61 5b 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e   a[pIdx->nColumn
247a8 5d 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ] = 1;.  }.}../*
247a9 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
247aa 20 77 69 6c 6c 20 64 72 6f 70 20 61 6e 20 65 78   will drop an ex
247ab 69 73 74 69 6e 67 20 6e 61 6d 65 64 20 69 6e 64  isting named ind
247ac 65 78 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  ex.  This routin
247ad 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 73 20  e.** implements 
247ae 74 68 65 20 44 52 4f 50 20 49 4e 44 45 58 20 73  the DROP INDEX s
247af 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c  tatement..*/.SQL
247b0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
247b1 20 73 71 6c 69 74 65 33 44 72 6f 70 49 6e 64 65   sqlite3DropInde
247b2 78 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  x(Parse *pParse,
247b3 20 53 72 63 4c 69 73 74 20 2a 70 4e 61 6d 65 2c   SrcList *pName,
247b4 20 69 6e 74 20 69 66 45 78 69 73 74 73 29 7b 0a   int ifExists){.
247b5 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b    Index *pIndex;
247b6 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 73 71  .  Vdbe *v;.  sq
247b7 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
247b8 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44  se->db;.  int iD
247b9 62 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50  b;..  assert( pP
247ba 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 29 3b  arse->nErr==0 );
247bb 20 20 20 2f 2a 20 4e 65 76 65 72 20 63 61 6c 6c     /* Never call
247bc 65 64 20 77 69 74 68 20 70 72 69 6f 72 20 65 72  ed with prior er
247bd 72 6f 72 73 20 2a 2f 0a 20 20 69 66 28 20 64 62  rors */.  if( db
247be 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
247bf 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f  {.    goto exit_
247c0 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a  drop_index;.  }.
247c1 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65 2d    assert( pName-
247c2 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 69 66  >nSrc==1 );.  if
247c3 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c  ( SQLITE_OK!=sql
247c4 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70  ite3ReadSchema(p
247c5 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20 67 6f  Parse) ){.    go
247c6 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64  to exit_drop_ind
247c7 65 78 3b 0a 20 20 7d 0a 20 20 70 49 6e 64 65 78  ex;.  }.  pIndex
247c8 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e   = sqlite3FindIn
247c9 64 65 78 28 64 62 2c 20 70 4e 61 6d 65 2d 3e 61  dex(db, pName->a
247ca 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70 4e 61 6d 65  [0].zName, pName
247cb 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61 73 65  ->a[0].zDatabase
247cc 29 3b 0a 20 20 69 66 28 20 70 49 6e 64 65 78 3d  );.  if( pIndex=
247cd 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 21 69  =0 ){.    if( !i
247ce 66 45 78 69 73 74 73 20 29 7b 0a 20 20 20 20 20  fExists ){.     
247cf 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
247d0 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63  (pParse, "no suc
247d1 68 20 69 6e 64 65 78 3a 20 25 53 22 2c 20 70 4e  h index: %S", pN
247d2 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20  ame, 0);.    }. 
247d3 20 20 20 70 50 61 72 73 65 2d 3e 63 68 65 63 6b     pParse->check
247d4 53 63 68 65 6d 61 20 3d 20 31 3b 0a 20 20 20 20  Schema = 1;.    
247d5 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 69  goto exit_drop_i
247d6 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 69 66 28 20  ndex;.  }.  if( 
247d7 70 49 6e 64 65 78 2d 3e 61 75 74 6f 49 6e 64 65  pIndex->autoInde
247d8 78 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  x ){.    sqlite3
247d9 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
247da 20 22 69 6e 64 65 78 20 61 73 73 6f 63 69 61 74   "index associat
247db 65 64 20 77 69 74 68 20 55 4e 49 51 55 45 20 22  ed with UNIQUE "
247dc 0a 20 20 20 20 20 20 22 6f 72 20 50 52 49 4d 41  .      "or PRIMA
247dd 52 59 20 4b 45 59 20 63 6f 6e 73 74 72 61 69 6e  RY KEY constrain
247de 74 20 63 61 6e 6e 6f 74 20 62 65 20 64 72 6f 70  t cannot be drop
247df 70 65 64 22 2c 20 30 29 3b 0a 20 20 20 20 67 6f  ped", 0);.    go
247e0 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64  to exit_drop_ind
247e1 65 78 3b 0a 20 20 7d 0a 20 20 69 44 62 20 3d 20  ex;.  }.  iDb = 
247e2 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
247e3 6e 64 65 78 28 64 62 2c 20 70 49 6e 64 65 78 2d  ndex(db, pIndex-
247e4 3e 70 53 63 68 65 6d 61 29 3b 0a 23 69 66 6e 64  >pSchema);.#ifnd
247e5 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
247e6 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 7b  UTHORIZATION.  {
247e7 0a 20 20 20 20 69 6e 74 20 63 6f 64 65 20 3d 20  .    int code = 
247e8 53 51 4c 49 54 45 5f 44 52 4f 50 5f 49 4e 44 45  SQLITE_DROP_INDE
247e9 58 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54  X;.    Table *pT
247ea 61 62 20 3d 20 70 49 6e 64 65 78 2d 3e 70 54 61  ab = pIndex->pTa
247eb 62 6c 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63  ble;.    const c
247ec 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61  har *zDb = db->a
247ed 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20  Db[iDb].zName;. 
247ee 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
247ef 54 61 62 20 3d 20 53 43 48 45 4d 41 5f 54 41 42  Tab = SCHEMA_TAB
247f0 4c 45 28 69 44 62 29 3b 0a 20 20 20 20 69 66 28  LE(iDb);.    if(
247f1 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
247f2 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
247f3 5f 44 45 4c 45 54 45 2c 20 7a 54 61 62 2c 20 30  _DELETE, zTab, 0
247f4 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20  , zDb) ){.      
247f5 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 69  goto exit_drop_i
247f6 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ndex;.    }.    
247f7 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42  if( !OMIT_TEMPDB
247f8 20 26 26 20 69 44 62 20 29 20 63 6f 64 65 20 3d   && iDb ) code =
247f9 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d   SQLITE_DROP_TEM
247fa 50 5f 49 4e 44 45 58 3b 0a 20 20 20 20 69 66 28  P_INDEX;.    if(
247fb 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
247fc 6b 28 70 50 61 72 73 65 2c 20 63 6f 64 65 2c 20  k(pParse, code, 
247fd 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 70  pIndex->zName, p
247fe 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 44 62 29  Tab->zName, zDb)
247ff 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65   ){.      goto e
24800 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b 0a  xit_drop_index;.
24801 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
24802 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20  ..  /* Generate 
24803 63 6f 64 65 20 74 6f 20 72 65 6d 6f 76 65 20 74  code to remove t
24804 68 65 20 69 6e 64 65 78 20 61 6e 64 20 66 72 6f  he index and fro
24805 6d 20 74 68 65 20 6d 61 73 74 65 72 20 74 61 62  m the master tab
24806 6c 65 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69  le */.  v = sqli
24807 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
24808 65 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20  e);.  if( v ){. 
24809 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57     sqlite3BeginW
2480a 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50  riteOperation(pP
2480b 61 72 73 65 2c 20 31 2c 20 69 44 62 29 3b 0a 20  arse, 1, iDb);. 
2480c 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64     sqlite3Nested
2480d 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20  Parse(pParse,.  
2480e 20 20 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f       "DELETE FRO
2480f 4d 20 25 51 2e 25 73 20 57 48 45 52 45 20 6e 61  M %Q.%s WHERE na
24810 6d 65 3d 25 51 22 2c 0a 20 20 20 20 20 20 20 64  me=%Q",.       d
24811 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
24812 65 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28  e, SCHEMA_TABLE(
24813 69 44 62 29 2c 0a 20 20 20 20 20 20 20 70 49 6e  iDb),.       pIn
24814 64 65 78 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 29  dex->zName.    )
24815 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
24816 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 22  3FindTable(db, "
24817 73 71 6c 69 74 65 5f 73 74 61 74 31 22 2c 20 64  sqlite_stat1", d
24818 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
24819 65 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  e) ){.      sqli
2481a 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70  te3NestedParse(p
2481b 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 22  Parse,.        "
2481c 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e 73  DELETE FROM %Q.s
2481d 71 6c 69 74 65 5f 73 74 61 74 31 20 57 48 45 52  qlite_stat1 WHER
2481e 45 20 69 64 78 3d 25 51 22 2c 0a 20 20 20 20 20  E idx=%Q",.     
2481f 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e     db->aDb[iDb].
24820 7a 4e 61 6d 65 2c 20 70 49 6e 64 65 78 2d 3e 7a  zName, pIndex->z
24821 4e 61 6d 65 0a 20 20 20 20 20 20 29 3b 0a 20 20  Name.      );.  
24822 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 43    }.    sqlite3C
24823 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72  hangeCookie(pPar
24824 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 64 65  se, iDb);.    de
24825 73 74 72 6f 79 52 6f 6f 74 50 61 67 65 28 70 50  stroyRootPage(pP
24826 61 72 73 65 2c 20 70 49 6e 64 65 78 2d 3e 74 6e  arse, pIndex->tn
24827 75 6d 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71  um, iDb);.    sq
24828 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
24829 76 2c 20 4f 50 5f 44 72 6f 70 49 6e 64 65 78 2c  v, OP_DropIndex,
2482a 20 69 44 62 2c 20 30 2c 20 30 2c 20 70 49 6e 64   iDb, 0, 0, pInd
2482b 65 78 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20  ex->zName, 0);. 
2482c 20 7d 0a 0a 65 78 69 74 5f 64 72 6f 70 5f 69 6e   }..exit_drop_in
2482d 64 65 78 3a 0a 20 20 73 71 6c 69 74 65 33 53 72  dex:.  sqlite3Sr
2482e 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  cListDelete(db, 
2482f 70 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pName);.}../*.**
24830 20 70 41 72 72 61 79 20 69 73 20 61 20 70 6f 69   pArray is a poi
24831 6e 74 65 72 20 74 6f 20 61 6e 20 61 72 72 61 79  nter to an array
24832 20 6f 66 20 6f 62 6a 65 63 74 73 2e 20 20 45 61   of objects.  Ea
24833 63 68 20 6f 62 6a 65 63 74 20 69 6e 20 74 68 65  ch object in the
24834 0a 2a 2a 20 61 72 72 61 79 20 69 73 20 73 7a 45  .** array is szE
24835 6e 74 72 79 20 62 79 74 65 73 20 69 6e 20 73 69  ntry bytes in si
24836 7a 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  ze.  This routin
24837 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65  e allocates a ne
24838 77 0a 2a 2a 20 6f 62 6a 65 63 74 20 6f 6e 20 74  w.** object on t
24839 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 61 72  he end of the ar
2483a 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 2a 70 6e 45 6e  ray..**.** *pnEn
2483b 74 72 79 20 69 73 20 74 68 65 20 6e 75 6d 62 65  try is the numbe
2483c 72 20 6f 66 20 65 6e 74 72 69 65 73 20 61 6c 72  r of entries alr
2483d 65 61 64 79 20 69 6e 20 75 73 65 2e 20 20 2a 70  eady in use.  *p
2483e 6e 41 6c 6c 6f 63 20 69 73 0a 2a 2a 20 74 68 65  nAlloc is.** the
2483f 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f   previously allo
24840 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20 74 68  cated size of th
24841 65 20 61 72 72 61 79 2e 20 20 69 6e 69 74 53 69  e array.  initSi
24842 7a 65 20 69 73 20 74 68 65 0a 2a 2a 20 73 75 67  ze is the.** sug
24843 67 65 73 74 65 64 20 69 6e 69 74 69 61 6c 20 61  gested initial a
24844 72 72 61 79 20 73 69 7a 65 20 61 6c 6c 6f 63 61  rray size alloca
24845 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  tion..**.** The 
24846 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6e 65 77  index of the new
24847 20 65 6e 74 72 79 20 69 73 20 72 65 74 75 72 6e   entry is return
24848 65 64 20 69 6e 20 2a 70 49 64 78 2e 0a 2a 2a 0a  ed in *pIdx..**.
24849 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2484a 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
2484b 72 20 74 6f 20 74 68 65 20 61 72 72 61 79 20 6f  r to the array o
2484c 66 20 6f 62 6a 65 63 74 73 2e 20 20 54 68 69 73  f objects.  This
2484d 0a 2a 2a 20 6d 69 67 68 74 20 62 65 20 74 68 65  .** might be the
2484e 20 73 61 6d 65 20 61 73 20 74 68 65 20 70 41 72   same as the pAr
2484f 72 61 79 20 70 61 72 61 6d 65 74 65 72 20 6f 72  ray parameter or
24850 20 69 74 20 6d 69 67 68 74 20 62 65 20 61 20 64   it might be a d
24851 69 66 66 65 72 65 6e 74 0a 2a 2a 20 70 6f 69 6e  ifferent.** poin
24852 74 65 72 20 69 66 20 74 68 65 20 61 72 72 61 79  ter if the array
24853 20 77 61 73 20 72 65 73 69 7a 65 64 2e 0a 2a 2f   was resized..*/
24854 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
24855 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 41 72 72  void *sqlite3Arr
24856 61 79 41 6c 6c 6f 63 61 74 65 28 0a 20 20 73 71  ayAllocate(.  sq
24857 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
24858 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f  /* Connection to
24859 20 6e 6f 74 69 66 79 20 6f 66 20 6d 61 6c 6c 6f   notify of mallo
2485a 63 20 66 61 69 6c 75 72 65 73 20 2a 2f 0a 20 20  c failures */.  
2485b 76 6f 69 64 20 2a 70 41 72 72 61 79 2c 20 20 20  void *pArray,   
2485c 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 6f 62    /* Array of ob
2485d 6a 65 63 74 73 2e 20 20 4d 69 67 68 74 20 62 65  jects.  Might be
2485e 20 72 65 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a   reallocated */.
2485f 20 20 69 6e 74 20 73 7a 45 6e 74 72 79 2c 20 20    int szEntry,  
24860 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65      /* Size of e
24861 61 63 68 20 6f 62 6a 65 63 74 20 69 6e 20 74 68  ach object in th
24862 65 20 61 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74  e array */.  int
24863 20 69 6e 69 74 53 69 7a 65 2c 20 20 20 20 20 2f   initSize,     /
24864 2a 20 53 75 67 67 65 73 74 65 64 20 69 6e 69 74  * Suggested init
24865 69 61 6c 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20  ial allocation, 
24866 69 6e 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20  in elements */. 
24867 20 69 6e 74 20 2a 70 6e 45 6e 74 72 79 2c 20 20   int *pnEntry,  
24868 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
24869 6f 62 6a 65 63 74 73 20 63 75 72 72 65 6e 74 6c  objects currentl
2486a 79 20 69 6e 20 75 73 65 20 2a 2f 0a 20 20 69 6e  y in use */.  in
2486b 74 20 2a 70 6e 41 6c 6c 6f 63 2c 20 20 20 20 20  t *pnAlloc,     
2486c 2f 2a 20 43 75 72 72 65 6e 74 20 73 69 7a 65 20  /* Current size 
2486d 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f  of the allocatio
2486e 6e 2c 20 69 6e 20 65 6c 65 6d 65 6e 74 73 20 2a  n, in elements *
2486f 2f 0a 20 20 69 6e 74 20 2a 70 49 64 78 20 20 20  /.  int *pIdx   
24870 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74        /* Write t
24871 68 65 20 69 6e 64 65 78 20 6f 66 20 61 20 6e 65  he index of a ne
24872 77 20 73 6c 6f 74 20 68 65 72 65 20 2a 2f 0a 29  w slot here */.)
24873 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69  {.  char *z;.  i
24874 66 28 20 2a 70 6e 45 6e 74 72 79 20 3e 3d 20 2a  f( *pnEntry >= *
24875 70 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 76  pnAlloc ){.    v
24876 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20 20 20 69  oid *pNew;.    i
24877 6e 74 20 6e 65 77 53 69 7a 65 3b 0a 20 20 20 20  nt newSize;.    
24878 6e 65 77 53 69 7a 65 20 3d 20 28 2a 70 6e 41 6c  newSize = (*pnAl
24879 6c 6f 63 29 2a 32 20 2b 20 69 6e 69 74 53 69 7a  loc)*2 + initSiz
2487a 65 3b 0a 20 20 20 20 70 4e 65 77 20 3d 20 73 71  e;.    pNew = sq
2487b 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64  lite3DbRealloc(d
2487c 62 2c 20 70 41 72 72 61 79 2c 20 6e 65 77 53 69  b, pArray, newSi
2487d 7a 65 2a 73 7a 45 6e 74 72 79 29 3b 0a 20 20 20  ze*szEntry);.   
2487e 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a   if( pNew==0 ){.
2487f 20 20 20 20 20 20 2a 70 49 64 78 20 3d 20 2d 31        *pIdx = -1
24880 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 70  ;.      return p
24881 41 72 72 61 79 3b 0a 20 20 20 20 7d 0a 20 20 20  Array;.    }.   
24882 20 2a 70 6e 41 6c 6c 6f 63 20 3d 20 73 71 6c 69   *pnAlloc = sqli
24883 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28  te3DbMallocSize(
24884 64 62 2c 20 70 4e 65 77 29 2f 73 7a 45 6e 74 72  db, pNew)/szEntr
24885 79 3b 0a 20 20 20 20 70 41 72 72 61 79 20 3d 20  y;.    pArray = 
24886 70 4e 65 77 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20  pNew;.  }.  z = 
24887 28 63 68 61 72 2a 29 70 41 72 72 61 79 3b 0a 20  (char*)pArray;. 
24888 20 6d 65 6d 73 65 74 28 26 7a 5b 2a 70 6e 45 6e   memset(&z[*pnEn
24889 74 72 79 20 2a 20 73 7a 45 6e 74 72 79 5d 2c 20  try * szEntry], 
2488a 30 2c 20 73 7a 45 6e 74 72 79 29 3b 0a 20 20 2a  0, szEntry);.  *
2488b 70 49 64 78 20 3d 20 2a 70 6e 45 6e 74 72 79 3b  pIdx = *pnEntry;
2488c 0a 20 20 2b 2b 2a 70 6e 45 6e 74 72 79 3b 0a 20  .  ++*pnEntry;. 
2488d 20 72 65 74 75 72 6e 20 70 41 72 72 61 79 3b 0a   return pArray;.
2488e 7d 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20  }../*.** Append 
2488f 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20 74 6f  a new element to
24890 20 74 68 65 20 67 69 76 65 6e 20 49 64 4c 69 73   the given IdLis
24891 74 2e 20 20 43 72 65 61 74 65 20 61 20 6e 65 77  t.  Create a new
24892 20 49 64 4c 69 73 74 20 69 66 0a 2a 2a 20 6e 65   IdList if.** ne
24893 65 64 20 62 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e  ed be..**.** A n
24894 65 77 20 49 64 4c 69 73 74 20 69 73 20 72 65 74  ew IdList is ret
24895 75 72 6e 65 64 2c 20 6f 72 20 4e 55 4c 4c 20 69  urned, or NULL i
24896 66 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73  f malloc() fails
24897 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
24898 41 54 45 20 49 64 4c 69 73 74 20 2a 73 71 6c 69  ATE IdList *sqli
24899 74 65 33 49 64 4c 69 73 74 41 70 70 65 6e 64 28  te3IdListAppend(
2489a 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 49 64 4c  sqlite3 *db, IdL
2489b 69 73 74 20 2a 70 4c 69 73 74 2c 20 54 6f 6b 65  ist *pList, Toke
2489c 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20 69 6e  n *pToken){.  in
2489d 74 20 69 3b 0a 20 20 69 66 28 20 70 4c 69 73 74  t i;.  if( pList
2489e 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4c 69 73 74  ==0 ){.    pList
2489f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
248a0 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f  ocZero(db, sizeo
248a1 66 28 49 64 4c 69 73 74 29 20 29 3b 0a 20 20 20  f(IdList) );.   
248a2 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20   if( pList==0 ) 
248a3 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 70 4c  return 0;.    pL
248a4 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 30 3b  ist->nAlloc = 0;
248a5 0a 20 20 7d 0a 20 20 70 4c 69 73 74 2d 3e 61 20  .  }.  pList->a 
248a6 3d 20 73 71 6c 69 74 65 33 41 72 72 61 79 41 6c  = sqlite3ArrayAl
248a7 6c 6f 63 61 74 65 28 0a 20 20 20 20 20 20 64 62  locate(.      db
248a8 2c 0a 20 20 20 20 20 20 70 4c 69 73 74 2d 3e 61  ,.      pList->a
248a9 2c 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 70  ,.      sizeof(p
248aa 4c 69 73 74 2d 3e 61 5b 30 5d 29 2c 0a 20 20 20  List->a[0]),.   
248ab 20 20 20 35 2c 0a 20 20 20 20 20 20 26 70 4c 69     5,.      &pLi
248ac 73 74 2d 3e 6e 49 64 2c 0a 20 20 20 20 20 20 26  st->nId,.      &
248ad 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 2c 0a 20  pList->nAlloc,. 
248ae 20 20 20 20 20 26 69 0a 20 20 29 3b 0a 20 20 69       &i.  );.  i
248af 66 28 20 69 3c 30 20 29 7b 0a 20 20 20 20 73 71  f( i<0 ){.    sq
248b0 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74  lite3IdListDelet
248b1 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20  e(db, pList);.  
248b2 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
248b3 20 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e    pList->a[i].zN
248b4 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d  ame = sqlite3Nam
248b5 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70  eFromToken(db, p
248b6 54 6f 6b 65 6e 29 3b 0a 20 20 72 65 74 75 72 6e  Token);.  return
248b7 20 70 4c 69 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   pList;.}../*.**
248b8 20 44 65 6c 65 74 65 20 61 6e 20 49 64 4c 69 73   Delete an IdLis
248b9 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
248ba 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
248bb 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 73 71  3IdListDelete(sq
248bc 6c 69 74 65 33 20 2a 64 62 2c 20 49 64 4c 69 73  lite3 *db, IdLis
248bd 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74  t *pList){.  int
248be 20 69 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d   i;.  if( pList=
248bf 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66  =0 ) return;.  f
248c0 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d  or(i=0; i<pList-
248c1 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nId; i++){.    
248c2 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
248c3 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e  , pList->a[i].zN
248c4 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  ame);.  }.  sqli
248c5 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c  te3DbFree(db, pL
248c6 69 73 74 2d 3e 61 29 3b 0a 20 20 73 71 6c 69 74  ist->a);.  sqlit
248c7 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c 69  e3DbFree(db, pLi
248c8 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  st);.}../*.** Re
248c9 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 69  turn the index i
248ca 6e 20 70 4c 69 73 74 20 6f 66 20 74 68 65 20 69  n pList of the i
248cb 64 65 6e 74 69 66 69 65 72 20 6e 61 6d 65 64 20  dentifier named 
248cc 7a 49 64 2e 20 20 52 65 74 75 72 6e 20 2d 31 0a  zId.  Return -1.
248cd 2a 2a 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e  ** if not found.
248ce 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
248cf 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 64  TE int sqlite3Id
248d0 4c 69 73 74 49 6e 64 65 78 28 49 64 4c 69 73 74  ListIndex(IdList
248d1 20 2a 70 4c 69 73 74 2c 20 63 6f 6e 73 74 20 63   *pList, const c
248d2 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 69  har *zName){.  i
248d3 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 4c 69 73  nt i;.  if( pLis
248d4 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 2d 31  t==0 ) return -1
248d5 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
248d6 4c 69 73 74 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b  List->nId; i++){
248d7 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
248d8 53 74 72 49 43 6d 70 28 70 4c 69 73 74 2d 3e 61  StrICmp(pList->a
248d9 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65  [i].zName, zName
248da 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 69 3b  )==0 ) return i;
248db 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2d 31  .  }.  return -1
248dc 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 70 61 6e  ;.}../*.** Expan
248dd 64 20 74 68 65 20 73 70 61 63 65 20 61 6c 6c 6f  d the space allo
248de 63 61 74 65 64 20 66 6f 72 20 74 68 65 20 67 69  cated for the gi
248df 76 65 6e 20 53 72 63 4c 69 73 74 20 6f 62 6a 65  ven SrcList obje
248e0 63 74 20 62 79 0a 2a 2a 20 63 72 65 61 74 69 6e  ct by.** creatin
248e1 67 20 6e 45 78 74 72 61 20 6e 65 77 20 73 6c 6f  g nExtra new slo
248e2 74 73 20 62 65 67 69 6e 6e 69 6e 67 20 61 74 20  ts beginning at 
248e3 69 53 74 61 72 74 2e 20 20 69 53 74 61 72 74 20  iStart.  iStart 
248e4 69 73 20 7a 65 72 6f 20 62 61 73 65 64 2e 0a 2a  is zero based..*
248e5 2a 20 4e 65 77 20 73 6c 6f 74 73 20 61 72 65 20  * New slots are 
248e6 7a 65 72 6f 65 64 2e 0a 2a 2a 0a 2a 2a 20 46 6f  zeroed..**.** Fo
248e7 72 20 65 78 61 6d 70 6c 65 2c 20 73 75 70 70 6f  r example, suppo
248e8 73 65 20 61 20 53 72 63 4c 69 73 74 20 69 6e 69  se a SrcList ini
248e9 74 69 61 6c 6c 79 20 63 6f 6e 74 61 69 6e 73 20  tially contains 
248ea 74 77 6f 20 65 6e 74 72 69 65 73 3a 20 41 2c 42  two entries: A,B
248eb 2e 0a 2a 2a 20 54 6f 20 61 70 70 65 6e 64 20 33  ..** To append 3
248ec 20 6e 65 77 20 65 6e 74 72 69 65 73 20 6f 6e 74   new entries ont
248ed 6f 20 74 68 65 20 65 6e 64 2c 20 64 6f 20 74 68  o the end, do th
248ee 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 73 71 6c  is:.**.**    sql
248ef 69 74 65 33 53 72 63 4c 69 73 74 45 6e 6c 61 72  ite3SrcListEnlar
248f0 67 65 28 64 62 2c 20 70 53 72 63 6c 69 73 74 2c  ge(db, pSrclist,
248f1 20 33 2c 20 32 29 3b 0a 2a 2a 0a 2a 2a 20 41 66   3, 2);.**.** Af
248f2 74 65 72 20 74 68 65 20 63 61 6c 6c 20 61 62 6f  ter the call abo
248f3 76 65 20 69 74 20 77 6f 75 6c 64 20 63 6f 6e 74  ve it would cont
248f4 61 69 6e 3a 20 20 41 2c 20 42 2c 20 6e 69 6c 2c  ain:  A, B, nil,
248f5 20 6e 69 6c 2c 20 6e 69 6c 2e 0a 2a 2a 20 49 66   nil, nil..** If
248f6 20 74 68 65 20 69 53 74 61 72 74 20 61 72 67 75   the iStart argu
248f7 6d 65 6e 74 20 68 61 64 20 62 65 65 6e 20 31 20  ment had been 1 
248f8 69 6e 73 74 65 61 64 20 6f 66 20 32 2c 20 74 68  instead of 2, th
248f9 65 6e 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a  en the result.**
248fa 20 77 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e   would have been
248fb 3a 20 20 41 2c 20 6e 69 6c 2c 20 6e 69 6c 2c 20  :  A, nil, nil, 
248fc 6e 69 6c 2c 20 42 2e 20 20 54 6f 20 70 72 65 70  nil, B.  To prep
248fd 65 6e 64 20 74 68 65 20 6e 65 77 20 73 6c 6f 74  end the new slot
248fe 73 2c 0a 2a 2a 20 74 68 65 20 69 53 74 61 72 74  s,.** the iStart
248ff 20 76 61 6c 75 65 20 77 6f 75 6c 64 20 62 65 20   value would be 
24900 30 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20 74  0.  The result t
24901 68 65 6e 20 77 6f 75 6c 64 0a 2a 2a 20 62 65 3a  hen would.** be:
24902 20 6e 69 6c 2c 20 6e 69 6c 2c 20 6e 69 6c 2c 20   nil, nil, nil, 
24903 41 2c 20 42 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  A, B..**.** If a
24904 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
24905 6f 6e 20 66 61 69 6c 73 20 74 68 65 20 53 72 63  on fails the Src
24906 4c 69 73 74 20 69 73 20 75 6e 63 68 61 6e 67 65  List is unchange
24907 64 2e 20 20 54 68 65 0a 2a 2a 20 64 62 2d 3e 6d  d.  The.** db->m
24908 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67  allocFailed flag
24909 20 77 69 6c 6c 20 62 65 20 73 65 74 20 74 6f 20   will be set to 
2490a 74 72 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  true..*/.SQLITE_
2490b 50 52 49 56 41 54 45 20 53 72 63 4c 69 73 74 20  PRIVATE SrcList 
2490c 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 45  *sqlite3SrcListE
2490d 6e 6c 61 72 67 65 28 0a 20 20 73 71 6c 69 74 65  nlarge(.  sqlite
2490e 33 20 2a 64 62 2c 20 20 20 20 20 20 20 2f 2a 20  3 *db,       /* 
2490f 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  Database connect
24910 69 6f 6e 20 74 6f 20 6e 6f 74 69 66 79 20 6f 66  ion to notify of
24911 20 4f 4f 4d 20 65 72 72 6f 72 73 20 2a 2f 0a 20   OOM errors */. 
24912 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20   SrcList *pSrc, 
24913 20 20 20 20 2f 2a 20 54 68 65 20 53 72 63 4c 69      /* The SrcLi
24914 73 74 20 74 6f 20 62 65 20 65 6e 6c 61 72 67 65  st to be enlarge
24915 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 74 72  d */.  int nExtr
24916 61 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  a,        /* Num
24917 62 65 72 20 6f 66 20 6e 65 77 20 73 6c 6f 74 73  ber of new slots
24918 20 74 6f 20 61 64 64 20 74 6f 20 70 53 72 63 2d   to add to pSrc-
24919 3e 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 53  >a[] */.  int iS
2491a 74 61 72 74 20 20 20 20 20 20 20 20 20 2f 2a 20  tart         /* 
2491b 49 6e 64 65 78 20 69 6e 20 70 53 72 63 2d 3e 61  Index in pSrc->a
2491c 5b 5d 20 6f 66 20 66 69 72 73 74 20 6e 65 77 20  [] of first new 
2491d 73 6c 6f 74 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  slot */.){.  int
2491e 20 69 3b 0a 0a 20 20 2f 2a 20 53 61 6e 69 74 79   i;..  /* Sanity
2491f 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 63 61 6c   checking on cal
24920 6c 69 6e 67 20 70 61 72 61 6d 65 74 65 72 73 20  ling parameters 
24921 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 53 74  */.  assert( iSt
24922 61 72 74 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65  art>=0 );.  asse
24923 72 74 28 20 6e 45 78 74 72 61 3e 3d 31 20 29 3b  rt( nExtra>=1 );
24924 0a 20 20 61 73 73 65 72 74 28 20 70 53 72 63 21  .  assert( pSrc!
24925 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
24926 69 53 74 61 72 74 3c 3d 70 53 72 63 2d 3e 6e 53  iStart<=pSrc->nS
24927 72 63 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f  rc );..  /* Allo
24928 63 61 74 65 20 61 64 64 69 74 69 6f 6e 61 6c 20  cate additional 
24929 73 70 61 63 65 20 69 66 20 6e 65 65 64 65 64 20  space if needed 
2492a 2a 2f 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 6e  */.  if( pSrc->n
2492b 53 72 63 2b 6e 45 78 74 72 61 3e 70 53 72 63 2d  Src+nExtra>pSrc-
2492c 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 53  >nAlloc ){.    S
2492d 72 63 4c 69 73 74 20 2a 70 4e 65 77 3b 0a 20 20  rcList *pNew;.  
2492e 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 20 3d 20 70    int nAlloc = p
2492f 53 72 63 2d 3e 6e 53 72 63 2b 6e 45 78 74 72 61  Src->nSrc+nExtra
24930 3b 0a 20 20 20 20 69 6e 74 20 6e 47 6f 74 3b 0a  ;.    int nGot;.
24931 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74      pNew = sqlit
24932 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20  e3DbRealloc(db, 
24933 70 53 72 63 2c 0a 20 20 20 20 20 20 20 20 20 20  pSrc,.          
24934 20 20 20 20 20 73 69 7a 65 6f 66 28 2a 70 53 72       sizeof(*pSr
24935 63 29 20 2b 20 28 6e 41 6c 6c 6f 63 2d 31 29 2a  c) + (nAlloc-1)*
24936 73 69 7a 65 6f 66 28 70 53 72 63 2d 3e 61 5b 30  sizeof(pSrc->a[0
24937 5d 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4e  ]) );.    if( pN
24938 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61  ew==0 ){.      a
24939 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f  ssert( db->mallo
2493a 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 20  cFailed );.     
2493b 20 72 65 74 75 72 6e 20 70 53 72 63 3b 0a 20 20   return pSrc;.  
2493c 20 20 7d 0a 20 20 20 20 70 53 72 63 20 3d 20 70    }.    pSrc = p
2493d 4e 65 77 3b 0a 20 20 20 20 6e 47 6f 74 20 3d 20  New;.    nGot = 
2493e 28 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63  (sqlite3DbMalloc
2493f 53 69 7a 65 28 64 62 2c 20 70 4e 65 77 29 20 2d  Size(db, pNew) -
24940 20 73 69 7a 65 6f 66 28 2a 70 53 72 63 29 29 2f   sizeof(*pSrc))/
24941 73 69 7a 65 6f 66 28 70 53 72 63 2d 3e 61 5b 30  sizeof(pSrc->a[0
24942 5d 29 2b 31 3b 0a 20 20 20 20 70 53 72 63 2d 3e  ])+1;.    pSrc->
24943 6e 41 6c 6c 6f 63 20 3d 20 28 75 31 36 29 6e 47  nAlloc = (u16)nG
24944 6f 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 6f  ot;.  }..  /* Mo
24945 76 65 20 65 78 69 73 74 69 6e 67 20 73 6c 6f 74  ve existing slot
24946 73 20 74 68 61 74 20 63 6f 6d 65 20 61 66 74 65  s that come afte
24947 72 20 74 68 65 20 6e 65 77 6c 79 20 69 6e 73 65  r the newly inse
24948 72 74 65 64 20 73 6c 6f 74 73 0a 20 20 2a 2a 20  rted slots.  ** 
24949 6f 75 74 20 6f 66 20 74 68 65 20 77 61 79 20 2a  out of the way *
2494a 2f 0a 20 20 66 6f 72 28 69 3d 70 53 72 63 2d 3e  /.  for(i=pSrc->
2494b 6e 53 72 63 2d 31 3b 20 69 3e 3d 69 53 74 61 72  nSrc-1; i>=iStar
2494c 74 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 70 53 72  t; i--){.    pSr
2494d 63 2d 3e 61 5b 69 2b 6e 45 78 74 72 61 5d 20 3d  c->a[i+nExtra] =
2494e 20 70 53 72 63 2d 3e 61 5b 69 5d 3b 0a 20 20 7d   pSrc->a[i];.  }
2494f 0a 20 20 70 53 72 63 2d 3e 6e 53 72 63 20 2b 3d  .  pSrc->nSrc +=
24950 20 28 69 31 36 29 6e 45 78 74 72 61 3b 0a 0a 20   (i16)nExtra;.. 
24951 20 2f 2a 20 5a 65 72 6f 20 74 68 65 20 6e 65 77   /* Zero the new
24952 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 6c 6f  ly allocated slo
24953 74 73 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26  ts */.  memset(&
24954 70 53 72 63 2d 3e 61 5b 69 53 74 61 72 74 5d 2c  pSrc->a[iStart],
24955 20 30 2c 20 73 69 7a 65 6f 66 28 70 53 72 63 2d   0, sizeof(pSrc-
24956 3e 61 5b 30 5d 29 2a 6e 45 78 74 72 61 29 3b 0a  >a[0])*nExtra);.
24957 20 20 66 6f 72 28 69 3d 69 53 74 61 72 74 3b 20    for(i=iStart; 
24958 69 3c 69 53 74 61 72 74 2b 6e 45 78 74 72 61 3b  i<iStart+nExtra;
24959 20 69 2b 2b 29 7b 0a 20 20 20 20 70 53 72 63 2d   i++){.    pSrc-
2495a 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 20 3d 20  >a[i].iCursor = 
2495b 2d 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65  -1;.  }..  /* Re
2495c 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
2495d 6f 20 74 68 65 20 65 6e 6c 61 72 67 65 64 20 53  o the enlarged S
2495e 72 63 4c 69 73 74 20 2a 2f 0a 20 20 72 65 74 75  rcList */.  retu
2495f 72 6e 20 70 53 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a  rn pSrc;.}.../*.
24960 2a 2a 20 41 70 70 65 6e 64 20 61 20 6e 65 77 20  ** Append a new 
24961 74 61 62 6c 65 20 6e 61 6d 65 20 74 6f 20 74 68  table name to th
24962 65 20 67 69 76 65 6e 20 53 72 63 4c 69 73 74 2e  e given SrcList.
24963 20 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 53    Create a new S
24964 72 63 4c 69 73 74 20 69 66 0a 2a 2a 20 6e 65 65  rcList if.** nee
24965 64 20 62 65 2e 20 20 41 20 6e 65 77 20 65 6e 74  d be.  A new ent
24966 72 79 20 69 73 20 63 72 65 61 74 65 64 20 69 6e  ry is created in
24967 20 74 68 65 20 53 72 63 4c 69 73 74 20 65 76 65   the SrcList eve
24968 6e 20 69 66 20 70 54 61 62 6c 65 20 69 73 20 4e  n if pTable is N
24969 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 41 20 53 72 63  ULL..**.** A Src
2496a 4c 69 73 74 20 69 73 20 72 65 74 75 72 6e 65 64  List is returned
2496b 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 65  , or NULL if the
2496c 72 65 20 69 73 20 61 6e 20 4f 4f 4d 20 65 72 72  re is an OOM err
2496d 6f 72 2e 20 20 54 68 65 20 72 65 74 75 72 6e 65  or.  The returne
2496e 64 0a 2a 2a 20 53 72 63 4c 69 73 74 20 6d 69 67  d.** SrcList mig
2496f 68 74 20 62 65 20 74 68 65 20 73 61 6d 65 20 61  ht be the same a
24970 73 20 74 68 65 20 53 72 63 4c 69 73 74 20 74 68  s the SrcList th
24971 61 74 20 77 61 73 20 69 6e 70 75 74 20 6f 72 20  at was input or 
24972 69 74 20 6d 69 67 68 74 20 62 65 0a 2a 2a 20 61  it might be.** a
24973 20 6e 65 77 20 6f 6e 65 2e 20 20 49 66 20 61 6e   new one.  If an
24974 20 4f 4f 4d 20 65 72 72 6f 72 20 64 6f 65 73 20   OOM error does 
24975 6f 63 63 75 72 73 2c 20 74 68 65 6e 20 74 68 65  occurs, then the
24976 20 70 72 69 6f 72 20 76 61 6c 75 65 20 6f 66 20   prior value of 
24977 70 4c 69 73 74 0a 2a 2a 20 74 68 61 74 20 69 73  pList.** that is
24978 20 69 6e 70 75 74 20 74 6f 20 74 68 69 73 20 72   input to this r
24979 6f 75 74 69 6e 65 20 69 73 20 61 75 74 6f 6d 61  outine is automa
2497a 74 69 63 61 6c 6c 79 20 66 72 65 65 64 2e 0a 2a  tically freed..*
2497b 2a 0a 2a 2a 20 49 66 20 70 44 61 74 61 62 61 73  *.** If pDatabas
2497c 65 20 69 73 20 6e 6f 74 20 6e 75 6c 6c 2c 20 69  e is not null, i
2497d 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  t means that the
2497e 20 74 61 62 6c 65 20 68 61 73 20 61 6e 20 6f 70   table has an op
2497f 74 69 6f 6e 61 6c 0a 2a 2a 20 64 61 74 61 62 61  tional.** databa
24980 73 65 20 6e 61 6d 65 20 70 72 65 66 69 78 2e 20  se name prefix. 
24981 20 4c 69 6b 65 20 74 68 69 73 3a 20 20 22 64 61   Like this:  "da
24982 74 61 62 61 73 65 2e 74 61 62 6c 65 22 2e 20 20  tabase.table".  
24983 54 68 65 20 70 44 61 74 61 62 61 73 65 0a 2a 2a  The pDatabase.**
24984 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 74   points to the t
24985 61 62 6c 65 20 6e 61 6d 65 20 61 6e 64 20 74 68  able name and th
24986 65 20 70 54 61 62 6c 65 20 70 6f 69 6e 74 73 20  e pTable points 
24987 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
24988 6e 61 6d 65 2e 0a 2a 2a 20 54 68 65 20 53 72 63  name..** The Src
24989 4c 69 73 74 2e 61 5b 5d 2e 7a 4e 61 6d 65 20 66  List.a[].zName f
2498a 69 65 6c 64 20 69 73 20 66 69 6c 6c 65 64 20 77  ield is filled w
2498b 69 74 68 20 74 68 65 20 74 61 62 6c 65 20 6e 61  ith the table na
2498c 6d 65 20 77 68 69 63 68 20 6d 69 67 68 74 0a 2a  me which might.*
2498d 2a 20 63 6f 6d 65 20 66 72 6f 6d 20 70 54 61 62  * come from pTab
2498e 6c 65 20 28 69 66 20 70 44 61 74 61 62 61 73 65  le (if pDatabase
2498f 20 69 73 20 4e 55 4c 4c 29 20 6f 72 20 66 72 6f   is NULL) or fro
24990 6d 20 70 44 61 74 61 62 61 73 65 2e 20 20 0a 2a  m pDatabase.  .*
24991 2a 20 53 72 63 4c 69 73 74 2e 61 5b 5d 2e 7a 44  * SrcList.a[].zD
24992 61 74 61 62 61 73 65 20 69 73 20 66 69 6c 6c 65  atabase is fille
24993 64 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62  d with the datab
24994 61 73 65 20 6e 61 6d 65 20 66 72 6f 6d 20 70 54  ase name from pT
24995 61 62 6c 65 2c 0a 2a 2a 20 6f 72 20 77 69 74 68  able,.** or with
24996 20 4e 55 4c 4c 20 69 66 20 6e 6f 20 64 61 74 61   NULL if no data
24997 62 61 73 65 20 69 73 20 73 70 65 63 69 66 69 65  base is specifie
24998 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65  d..**.** In othe
24999 72 20 77 6f 72 64 73 2c 20 69 66 20 63 61 6c 6c  r words, if call
2499a 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a   like this:.**.*
2499b 2a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  *         sqlite
2499c 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 44  3SrcListAppend(D
2499d 2c 41 2c 42 2c 30 29 3b 0a 2a 2a 0a 2a 2a 20 54  ,A,B,0);.**.** T
2499e 68 65 6e 20 42 20 69 73 20 61 20 74 61 62 6c 65  hen B is a table
2499f 20 6e 61 6d 65 20 61 6e 64 20 74 68 65 20 64 61   name and the da
249a0 74 61 62 61 73 65 20 6e 61 6d 65 20 69 73 20 75  tabase name is u
249a1 6e 73 70 65 63 69 66 69 65 64 2e 20 20 49 66 20  nspecified.  If 
249a2 63 61 6c 6c 65 64 0a 2a 2a 20 6c 69 6b 65 20 74  called.** like t
249a3 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  his:.**.**      
249a4 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73     sqlite3SrcLis
249a5 74 41 70 70 65 6e 64 28 44 2c 41 2c 42 2c 43 29  tAppend(D,A,B,C)
249a6 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 43 20 69  ;.**.** Then C i
249a7 73 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65  s the table name
249a8 20 61 6e 64 20 42 20 69 73 20 74 68 65 20 64 61   and B is the da
249a9 74 61 62 61 73 65 20 6e 61 6d 65 2e 20 20 49 66  tabase name.  If
249aa 20 43 20 69 73 20 64 65 66 69 6e 65 64 0a 2a 2a   C is defined.**
249ab 20 74 68 65 6e 20 73 6f 20 69 73 20 42 2e 20 20   then so is B.  
249ac 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
249ad 77 65 20 6e 65 76 65 72 20 68 61 76 65 20 61 20  we never have a 
249ae 63 61 73 65 20 77 68 65 72 65 3a 0a 2a 2a 0a 2a  case where:.**.*
249af 2a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  *         sqlite
249b0 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 44  3SrcListAppend(D
249b1 2c 41 2c 30 2c 43 29 3b 0a 2a 2a 0a 2a 2a 20 42  ,A,0,C);.**.** B
249b2 6f 74 68 20 70 54 61 62 6c 65 20 61 6e 64 20 70  oth pTable and p
249b3 44 61 74 61 62 61 73 65 20 61 72 65 20 61 73 73  Database are ass
249b4 75 6d 65 64 20 74 6f 20 62 65 20 71 75 6f 74 65  umed to be quote
249b5 64 2e 20 20 54 68 65 79 20 61 72 65 20 64 65 71  d.  They are deq
249b6 75 6f 74 65 64 0a 2a 2a 20 62 65 66 6f 72 65 20  uoted.** before 
249b7 62 65 69 6e 67 20 61 64 64 65 64 20 74 6f 20 74  being added to t
249b8 68 65 20 53 72 63 4c 69 73 74 2e 0a 2a 2f 0a 53  he SrcList..*/.S
249b9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 72  QLITE_PRIVATE Sr
249ba 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72  cList *sqlite3Sr
249bb 63 4c 69 73 74 41 70 70 65 6e 64 28 0a 20 20 73  cListAppend(.  s
249bc 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
249bd 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e     /* Connection
249be 20 74 6f 20 6e 6f 74 69 66 79 20 6f 66 20 6d 61   to notify of ma
249bf 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 2a 2f  lloc failures */
249c0 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 4c 69 73  .  SrcList *pLis
249c1 74 2c 20 20 20 20 20 2f 2a 20 41 70 70 65 6e 64  t,     /* Append
249c2 20 74 6f 20 74 68 69 73 20 53 72 63 4c 69 73 74   to this SrcList
249c3 2e 20 4e 55 4c 4c 20 63 72 65 61 74 65 73 20 61  . NULL creates a
249c4 20 6e 65 77 20 53 72 63 4c 69 73 74 20 2a 2f 0a   new SrcList */.
249c5 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 2c    Token *pTable,
249c6 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74        /* Table t
249c7 6f 20 61 70 70 65 6e 64 20 2a 2f 0a 20 20 54 6f  o append */.  To
249c8 6b 65 6e 20 2a 70 44 61 74 61 62 61 73 65 20 20  ken *pDatabase  
249c9 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6f 66    /* Database of
249ca 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 29 7b   the table */.){
249cb 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
249cc 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20  t_item *pItem;. 
249cd 20 61 73 73 65 72 74 28 20 70 44 61 74 61 62 61   assert( pDataba
249ce 73 65 3d 3d 30 20 7c 7c 20 70 54 61 62 6c 65 21  se==0 || pTable!
249cf 3d 30 20 29 3b 20 20 2f 2a 20 43 61 6e 6e 6f 74  =0 );  /* Cannot
249d0 20 68 61 76 65 20 43 20 77 69 74 68 6f 75 74 20   have C without 
249d1 42 20 2a 2f 0a 20 20 69 66 28 20 70 4c 69 73 74  B */.  if( pList
249d2 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4c 69 73 74  ==0 ){.    pList
249d3 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
249d4 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f  ocZero(db, sizeo
249d5 66 28 53 72 63 4c 69 73 74 29 20 29 3b 0a 20 20  f(SrcList) );.  
249d6 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29    if( pList==0 )
249d7 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 70   return 0;.    p
249d8 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 31  List->nAlloc = 1
249d9 3b 0a 20 20 7d 0a 20 20 70 4c 69 73 74 20 3d 20  ;.  }.  pList = 
249da 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 45 6e  sqlite3SrcListEn
249db 6c 61 72 67 65 28 64 62 2c 20 70 4c 69 73 74 2c  large(db, pList,
249dc 20 31 2c 20 70 4c 69 73 74 2d 3e 6e 53 72 63 29   1, pList->nSrc)
249dd 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c  ;.  if( db->mall
249de 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
249df 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65  sqlite3SrcListDe
249e0 6c 65 74 65 28 64 62 2c 20 70 4c 69 73 74 29 3b  lete(db, pList);
249e1 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
249e2 20 7d 0a 20 20 70 49 74 65 6d 20 3d 20 26 70 4c   }.  pItem = &pL
249e3 69 73 74 2d 3e 61 5b 70 4c 69 73 74 2d 3e 6e 53  ist->a[pList->nS
249e4 72 63 2d 31 5d 3b 0a 20 20 69 66 28 20 70 44 61  rc-1];.  if( pDa
249e5 74 61 62 61 73 65 20 26 26 20 70 44 61 74 61 62  tabase && pDatab
249e6 61 73 65 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20  ase->z==0 ){.   
249e7 20 70 44 61 74 61 62 61 73 65 20 3d 20 30 3b 0a   pDatabase = 0;.
249e8 20 20 7d 0a 20 20 69 66 28 20 70 44 61 74 61 62    }.  if( pDatab
249e9 61 73 65 20 29 7b 0a 20 20 20 20 54 6f 6b 65 6e  ase ){.    Token
249ea 20 2a 70 54 65 6d 70 20 3d 20 70 44 61 74 61 62   *pTemp = pDatab
249eb 61 73 65 3b 0a 20 20 20 20 70 44 61 74 61 62 61  ase;.    pDataba
249ec 73 65 20 3d 20 70 54 61 62 6c 65 3b 0a 20 20 20  se = pTable;.   
249ed 20 70 54 61 62 6c 65 20 3d 20 70 54 65 6d 70 3b   pTable = pTemp;
249ee 0a 20 20 7d 0a 20 20 70 49 74 65 6d 2d 3e 7a 4e  .  }.  pItem->zN
249ef 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d  ame = sqlite3Nam
249f0 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70  eFromToken(db, p
249f1 54 61 62 6c 65 29 3b 0a 20 20 70 49 74 65 6d 2d  Table);.  pItem-
249f2 3e 7a 44 61 74 61 62 61 73 65 20 3d 20 73 71 6c  >zDatabase = sql
249f3 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
249f4 6e 28 64 62 2c 20 70 44 61 74 61 62 61 73 65 29  n(db, pDatabase)
249f5 3b 0a 20 20 72 65 74 75 72 6e 20 70 4c 69 73 74  ;.  return pList
249f6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 69 67  ;.}../*.** Assig
249f7 6e 20 56 64 62 65 43 75 72 73 6f 72 20 69 6e 64  n VdbeCursor ind
249f8 65 78 20 6e 75 6d 62 65 72 73 20 74 6f 20 61 6c  ex numbers to al
249f9 6c 20 74 61 62 6c 65 73 20 69 6e 20 61 20 53 72  l tables in a Sr
249fa 63 4c 69 73 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f  cList.*/.SQLITE_
249fb 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
249fc 69 74 65 33 53 72 63 4c 69 73 74 41 73 73 69 67  ite3SrcListAssig
249fd 6e 43 75 72 73 6f 72 73 28 50 61 72 73 65 20 2a  nCursors(Parse *
249fe 70 50 61 72 73 65 2c 20 53 72 63 4c 69 73 74 20  pParse, SrcList 
249ff 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69  *pList){.  int i
24a00 3b 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69  ;.  struct SrcLi
24a01 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a  st_item *pItem;.
24a02 20 20 61 73 73 65 72 74 28 70 4c 69 73 74 20 7c    assert(pList |
24a03 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61  | pParse->db->ma
24a04 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
24a05 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 20 20  if( pList ){.   
24a06 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d   for(i=0, pItem=
24a07 70 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 4c 69 73  pList->a; i<pLis
24a08 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 49  t->nSrc; i++, pI
24a09 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  tem++){.      if
24a0a 28 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72  ( pItem->iCursor
24a0b 3e 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  >=0 ) break;.   
24a0c 20 20 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f     pItem->iCurso
24a0d 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  r = pParse->nTab
24a0e 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49  ++;.      if( pI
24a0f 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a  tem->pSelect ){.
24a10 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
24a11 72 63 4c 69 73 74 41 73 73 69 67 6e 43 75 72 73  rcListAssignCurs
24a12 6f 72 73 28 70 50 61 72 73 65 2c 20 70 49 74 65  ors(pParse, pIte
24a13 6d 2d 3e 70 53 65 6c 65 63 74 2d 3e 70 53 72 63  m->pSelect->pSrc
24a14 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
24a15 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  .  }.}../*.** De
24a16 6c 65 74 65 20 61 6e 20 65 6e 74 69 72 65 20 53  lete an entire S
24a17 72 63 4c 69 73 74 20 69 6e 63 6c 75 64 69 6e 67  rcList including
24a18 20 61 6c 6c 20 69 74 73 20 73 75 62 73 74 72 75   all its substru
24a19 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  cture..*/.SQLITE
24a1a 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
24a1b 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65  lite3SrcListDele
24a1c 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  te(sqlite3 *db, 
24a1d 53 72 63 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b  SrcList *pList){
24a1e 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75  .  int i;.  stru
24a1f 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
24a20 2a 70 49 74 65 6d 3b 0a 20 20 69 66 28 20 70 4c  *pItem;.  if( pL
24a21 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ist==0 ) return;
24a22 0a 20 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c 69  .  for(pItem=pLi
24a23 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 70 4c  st->a, i=0; i<pL
24a24 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20  ist->nSrc; i++, 
24a25 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 73 71  pItem++){.    sq
24a26 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
24a27 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65  pItem->zDatabase
24a28 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  );.    sqlite3Db
24a29 46 72 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e  Free(db, pItem->
24a2a 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 73 71 6c 69  zName);.    sqli
24a2b 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 49  te3DbFree(db, pI
24a2c 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20 20  tem->zAlias);.  
24a2d 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
24a2e 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 49 6e 64 65  db, pItem->zInde
24a2f 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  x);.    sqlite3D
24a30 65 6c 65 74 65 54 61 62 6c 65 28 70 49 74 65 6d  eleteTable(pItem
24a31 2d 3e 70 54 61 62 29 3b 0a 20 20 20 20 73 71 6c  ->pTab);.    sql
24a32 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
24a33 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c  (db, pItem->pSel
24a34 65 63 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ect);.    sqlite
24a35 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
24a36 70 49 74 65 6d 2d 3e 70 4f 6e 29 3b 0a 20 20 20  pItem->pOn);.   
24a37 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65   sqlite3IdListDe
24a38 6c 65 74 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e  lete(db, pItem->
24a39 70 55 73 69 6e 67 29 3b 0a 20 20 7d 0a 20 20 73  pUsing);.  }.  s
24a3a 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
24a3b 20 70 4c 69 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a   pList);.}../*.*
24a3c 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
24a3d 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20  s called by the 
24a3e 70 61 72 73 65 72 20 74 6f 20 61 64 64 20 61 20  parser to add a 
24a3f 6e 65 77 20 74 65 72 6d 20 74 6f 20 74 68 65 0a  new term to the.
24a40 2a 2a 20 65 6e 64 20 6f 66 20 61 20 67 72 6f 77  ** end of a grow
24a41 69 6e 67 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e  ing FROM clause.
24a42 20 20 54 68 65 20 22 70 22 20 70 61 72 61 6d 65    The "p" parame
24a43 74 65 72 20 69 73 20 74 68 65 20 70 61 72 74 20  ter is the part 
24a44 6f 66 0a 2a 2a 20 74 68 65 20 46 52 4f 4d 20 63  of.** the FROM c
24a45 6c 61 75 73 65 20 74 68 61 74 20 68 61 73 20 61  lause that has a
24a46 6c 72 65 61 64 79 20 62 65 65 6e 20 63 6f 6e 73  lready been cons
24a47 74 72 75 63 74 65 64 2e 20 20 22 70 22 20 69 73  tructed.  "p" is
24a48 20 4e 55 4c 4c 0a 2a 2a 20 69 66 20 74 68 69 73   NULL.** if this
24a49 20 69 73 20 74 68 65 20 66 69 72 73 74 20 74 65   is the first te
24a4a 72 6d 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63  rm of the FROM c
24a4b 6c 61 75 73 65 2e 20 20 70 54 61 62 6c 65 20 61  lause.  pTable a
24a4c 6e 64 20 70 44 61 74 61 62 61 73 65 0a 2a 2a 20  nd pDatabase.** 
24a4d 61 72 65 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  are the name of 
24a4e 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 64 61  the table and da
24a4f 74 61 62 61 73 65 20 6e 61 6d 65 64 20 69 6e 20  tabase named in 
24a50 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
24a51 74 65 72 6d 2e 0a 2a 2a 20 70 44 61 74 61 62 61  term..** pDataba
24a52 73 65 20 69 73 20 4e 55 4c 4c 20 69 66 20 74 68  se is NULL if th
24a53 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  e database name 
24a54 71 75 61 6c 69 66 69 65 72 20 69 73 20 6d 69 73  qualifier is mis
24a55 73 69 6e 67 20 2d 20 74 68 65 0a 2a 2a 20 75 73  sing - the.** us
24a56 75 61 6c 20 63 61 73 65 2e 20 20 49 66 20 74 68  ual case.  If th
24a57 65 20 74 65 72 6d 20 68 61 73 20 61 20 61 6c 69  e term has a ali
24a58 61 73 2c 20 74 68 65 6e 20 70 41 6c 69 61 73 20  as, then pAlias 
24a59 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 0a 2a 2a  points to the.**
24a5a 20 61 6c 69 61 73 20 74 6f 6b 65 6e 2e 20 20 49   alias token.  I
24a5b 66 20 74 68 65 20 74 65 72 6d 20 69 73 20 61 20  f the term is a 
24a5c 73 75 62 71 75 65 72 79 2c 20 74 68 65 6e 20 70  subquery, then p
24a5d 53 75 62 71 75 65 72 79 20 69 73 20 74 68 65 0a  Subquery is the.
24a5e 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  ** SELECT statem
24a5f 65 6e 74 20 74 68 61 74 20 74 68 65 20 73 75 62  ent that the sub
24a60 71 75 65 72 79 20 65 6e 63 6f 64 65 73 2e 20 20  query encodes.  
24a61 54 68 65 20 70 54 61 62 6c 65 20 61 6e 64 0a 2a  The pTable and.*
24a62 2a 20 70 44 61 74 61 62 61 73 65 20 70 61 72 61  * pDatabase para
24a63 6d 65 74 65 72 73 20 61 72 65 20 4e 55 4c 4c 20  meters are NULL 
24a64 66 6f 72 20 73 75 62 71 75 65 72 69 65 73 2e 20  for subqueries. 
24a65 20 54 68 65 20 70 4f 6e 20 61 6e 64 20 70 55 73   The pOn and pUs
24a66 69 6e 67 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  ing.** parameter
24a67 73 20 61 72 65 20 74 68 65 20 63 6f 6e 74 65 6e  s are the conten
24a68 74 20 6f 66 20 74 68 65 20 4f 4e 20 61 6e 64 20  t of the ON and 
24a69 55 53 49 4e 47 20 63 6c 61 75 73 65 73 2e 0a 2a  USING clauses..*
24a6a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 6e 65  *.** Return a ne
24a6b 77 20 53 72 63 4c 69 73 74 20 77 68 69 63 68 20  w SrcList which 
24a6c 65 6e 63 6f 64 65 73 20 69 73 20 74 68 65 20 46  encodes is the F
24a6d 52 4f 4d 20 77 69 74 68 20 74 68 65 20 6e 65 77  ROM with the new
24a6e 0a 2a 2a 20 74 65 72 6d 20 61 64 64 65 64 2e 0a  .** term added..
24a6f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
24a70 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74  E SrcList *sqlit
24a71 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 46  e3SrcListAppendF
24a72 72 6f 6d 54 65 72 6d 28 0a 20 20 50 61 72 73 65  romTerm(.  Parse
24a73 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
24a74 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
24a75 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69  ntext */.  SrcLi
24a76 73 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  st *p,          
24a77 20 20 20 2f 2a 20 54 68 65 20 6c 65 66 74 20 70     /* The left p
24a78 61 72 74 20 6f 66 20 74 68 65 20 46 52 4f 4d 20  art of the FROM 
24a79 63 6c 61 75 73 65 20 61 6c 72 65 61 64 79 20 73  clause already s
24a7a 65 65 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  een */.  Token *
24a7b 70 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20  pTable,         
24a7c 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
24a7d 74 61 62 6c 65 20 74 6f 20 61 64 64 20 74 6f 20  table to add to 
24a7e 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
24a7f 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 44 61 74  */.  Token *pDat
24a80 61 62 61 73 65 2c 20 20 20 20 20 20 20 2f 2a 20  abase,       /* 
24a81 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  Name of the data
24a82 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  base containing 
24a83 70 54 61 62 6c 65 20 2a 2f 0a 20 20 54 6f 6b 65  pTable */.  Toke
24a84 6e 20 2a 70 41 6c 69 61 73 2c 20 20 20 20 20 20  n *pAlias,      
24a85 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68 74      /* The right
24a86 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74 68  -hand side of th
24a87 65 20 41 53 20 73 75 62 65 78 70 72 65 73 73 69  e AS subexpressi
24a88 6f 6e 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  on */.  Select *
24a89 70 53 75 62 71 75 65 72 79 2c 20 20 20 20 20 20  pSubquery,      
24a8a 2f 2a 20 41 20 73 75 62 71 75 65 72 79 20 75 73  /* A subquery us
24a8b 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 61  ed in place of a
24a8c 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20   table name */. 
24a8d 20 45 78 70 72 20 2a 70 4f 6e 2c 20 20 20 20 20   Expr *pOn,     
24a8e 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
24a8f 4f 4e 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a  ON clause of a j
24a90 6f 69 6e 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20  oin */.  IdList 
24a91 2a 70 55 73 69 6e 67 20 20 20 20 20 20 20 20 20  *pUsing         
24a92 20 2f 2a 20 54 68 65 20 55 53 49 4e 47 20 63 6c   /* The USING cl
24a93 61 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a  ause of a join *
24a94 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 53 72  /.){.  struct Sr
24a95 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  cList_item *pIte
24a96 6d 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  m;.  sqlite3 *db
24a97 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
24a98 20 69 66 28 20 21 70 20 26 26 20 28 70 4f 6e 20   if( !p && (pOn 
24a99 7c 7c 20 70 55 73 69 6e 67 29 20 29 7b 0a 20 20  || pUsing) ){.  
24a9a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
24a9b 67 28 70 50 61 72 73 65 2c 20 22 61 20 4a 4f 49  g(pParse, "a JOI
24a9c 4e 20 63 6c 61 75 73 65 20 69 73 20 72 65 71 75  N clause is requ
24a9d 69 72 65 64 20 62 65 66 6f 72 65 20 25 73 22 2c  ired before %s",
24a9e 20 0a 20 20 20 20 20 20 28 70 4f 6e 20 3f 20 22   .      (pOn ? "
24a9f 4f 4e 22 20 3a 20 22 55 53 49 4e 47 22 29 0a 20  ON" : "USING"). 
24aa0 20 20 20 29 3b 0a 20 20 20 20 67 6f 74 6f 20 61     );.    goto a
24aa1 70 70 65 6e 64 5f 66 72 6f 6d 5f 65 72 72 6f 72  ppend_from_error
24aa2 3b 0a 20 20 7d 0a 20 20 70 20 3d 20 73 71 6c 69  ;.  }.  p = sqli
24aa3 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64  te3SrcListAppend
24aa4 28 64 62 2c 20 70 2c 20 70 54 61 62 6c 65 2c 20  (db, p, pTable, 
24aa5 70 44 61 74 61 62 61 73 65 29 3b 0a 20 20 69 66  pDatabase);.  if
24aa6 28 20 70 3d 3d 30 20 7c 7c 20 4e 45 56 45 52 28  ( p==0 || NEVER(
24aa7 70 2d 3e 6e 53 72 63 3d 3d 30 29 20 29 7b 0a 20  p->nSrc==0) ){. 
24aa8 20 20 20 67 6f 74 6f 20 61 70 70 65 6e 64 5f 66     goto append_f
24aa9 72 6f 6d 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20  rom_error;.  }. 
24aaa 20 70 49 74 65 6d 20 3d 20 26 70 2d 3e 61 5b 70   pItem = &p->a[p
24aab 2d 3e 6e 53 72 63 2d 31 5d 3b 0a 20 20 61 73 73  ->nSrc-1];.  ass
24aac 65 72 74 28 20 70 41 6c 69 61 73 21 3d 30 20 29  ert( pAlias!=0 )
24aad 3b 0a 20 20 69 66 28 20 70 41 6c 69 61 73 2d 3e  ;.  if( pAlias->
24aae 6e 20 29 7b 0a 20 20 20 20 70 49 74 65 6d 2d 3e  n ){.    pItem->
24aaf 7a 41 6c 69 61 73 20 3d 20 73 71 6c 69 74 65 33  zAlias = sqlite3
24ab0 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62  NameFromToken(db
24ab1 2c 20 70 41 6c 69 61 73 29 3b 0a 20 20 7d 0a 20  , pAlias);.  }. 
24ab2 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20   pItem->pSelect 
24ab3 3d 20 70 53 75 62 71 75 65 72 79 3b 0a 20 20 70  = pSubquery;.  p
24ab4 49 74 65 6d 2d 3e 70 4f 6e 20 3d 20 70 4f 6e 3b  Item->pOn = pOn;
24ab5 0a 20 20 70 49 74 65 6d 2d 3e 70 55 73 69 6e 67  .  pItem->pUsing
24ab6 20 3d 20 70 55 73 69 6e 67 3b 0a 20 20 72 65 74   = pUsing;.  ret
24ab7 75 72 6e 20 70 3b 0a 0a 20 61 70 70 65 6e 64 5f  urn p;.. append_
24ab8 66 72 6f 6d 5f 65 72 72 6f 72 3a 0a 20 20 61 73  from_error:.  as
24ab9 73 65 72 74 28 20 70 3d 3d 30 20 29 3b 0a 20 20  sert( p==0 );.  
24aba 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
24abb 65 28 64 62 2c 20 70 4f 6e 29 3b 0a 20 20 73 71  e(db, pOn);.  sq
24abc 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74  lite3IdListDelet
24abd 65 28 64 62 2c 20 70 55 73 69 6e 67 29 3b 0a 20  e(db, pUsing);. 
24abe 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
24abf 6c 65 74 65 28 64 62 2c 20 70 53 75 62 71 75 65  lete(db, pSubque
24ac0 72 79 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b  ry);.  return 0;
24ac1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 6e  .}../*.** Add an
24ac2 20 49 4e 44 45 58 45 44 20 42 59 20 6f 72 20 4e   INDEXED BY or N
24ac3 4f 54 20 49 4e 44 45 58 45 44 20 63 6c 61 75 73  OT INDEXED claus
24ac4 65 20 74 6f 20 74 68 65 20 6d 6f 73 74 20 72 65  e to the most re
24ac5 63 65 6e 74 6c 79 20 61 64 64 65 64 20 0a 2a 2a  cently added .**
24ac6 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20   element of the 
24ac7 73 6f 75 72 63 65 2d 6c 69 73 74 20 70 61 73 73  source-list pass
24ac8 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64  ed as the second
24ac9 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51   argument..*/.SQ
24aca 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
24acb 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74  d sqlite3SrcList
24acc 49 6e 64 65 78 65 64 42 79 28 50 61 72 73 65 20  IndexedBy(Parse 
24acd 2a 70 50 61 72 73 65 2c 20 53 72 63 4c 69 73 74  *pParse, SrcList
24ace 20 2a 70 2c 20 54 6f 6b 65 6e 20 2a 70 49 6e 64   *p, Token *pInd
24acf 65 78 65 64 42 79 29 7b 0a 20 20 61 73 73 65 72  exedBy){.  asser
24ad0 74 28 20 70 49 6e 64 65 78 65 64 42 79 21 3d 30  t( pIndexedBy!=0
24ad1 20 29 3b 0a 20 20 69 66 28 20 70 20 26 26 20 41   );.  if( p && A
24ad2 4c 57 41 59 53 28 70 2d 3e 6e 53 72 63 3e 30 29  LWAYS(p->nSrc>0)
24ad3 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 53   ){.    struct S
24ad4 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  rcList_item *pIt
24ad5 65 6d 20 3d 20 26 70 2d 3e 61 5b 70 2d 3e 6e 53  em = &p->a[p->nS
24ad6 72 63 2d 31 5d 3b 0a 20 20 20 20 61 73 73 65 72  rc-1];.    asser
24ad7 74 28 20 70 49 74 65 6d 2d 3e 6e 6f 74 49 6e 64  t( pItem->notInd
24ad8 65 78 65 64 3d 3d 30 20 26 26 20 70 49 74 65 6d  exed==0 && pItem
24ad9 2d 3e 7a 49 6e 64 65 78 3d 3d 30 20 29 3b 0a 20  ->zIndex==0 );. 
24ada 20 20 20 69 66 28 20 70 49 6e 64 65 78 65 64 42     if( pIndexedB
24adb 79 2d 3e 6e 3d 3d 31 20 26 26 20 21 70 49 6e 64  y->n==1 && !pInd
24adc 65 78 65 64 42 79 2d 3e 7a 20 29 7b 0a 20 20 20  exedBy->z ){.   
24add 20 20 20 2f 2a 20 41 20 22 4e 4f 54 20 49 4e 44     /* A "NOT IND
24ade 45 58 45 44 22 20 63 6c 61 75 73 65 20 77 61 73  EXED" clause was
24adf 20 73 75 70 70 6c 69 65 64 2e 20 53 65 65 20 70   supplied. See p
24ae0 61 72 73 65 2e 79 20 0a 20 20 20 20 20 20 2a 2a  arse.y .      **
24ae1 20 63 6f 6e 73 74 72 75 63 74 20 22 69 6e 64 65   construct "inde
24ae2 78 65 64 5f 6f 70 74 22 20 66 6f 72 20 64 65 74  xed_opt" for det
24ae3 61 69 6c 73 2e 20 2a 2f 0a 20 20 20 20 20 20 70  ails. */.      p
24ae4 49 74 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65 64  Item->notIndexed
24ae5 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b   = 1;.    }else{
24ae6 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 7a 49  .      pItem->zI
24ae7 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33 4e 61  ndex = sqlite3Na
24ae8 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 70 50 61 72  meFromToken(pPar
24ae9 73 65 2d 3e 64 62 2c 20 70 49 6e 64 65 78 65 64  se->db, pIndexed
24aea 42 79 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  By);.    }.  }.}
24aeb 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 62 75 69  ../*.** When bui
24aec 6c 64 69 6e 67 20 75 70 20 61 20 46 52 4f 4d 20  lding up a FROM 
24aed 63 6c 61 75 73 65 20 69 6e 20 74 68 65 20 70 61  clause in the pa
24aee 72 73 65 72 2c 20 74 68 65 20 6a 6f 69 6e 20 6f  rser, the join o
24aef 70 65 72 61 74 6f 72 0a 2a 2a 20 69 73 20 69 6e  perator.** is in
24af0 69 74 69 61 6c 6c 79 20 61 74 74 61 63 68 65 64  itially attached
24af1 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 70 65   to the left ope
24af2 72 61 6e 64 2e 20 20 42 75 74 20 74 68 65 20 63  rand.  But the c
24af3 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 0a 2a 2a  ode generator.**
24af4 20 65 78 70 65 63 74 73 20 74 68 65 20 6a 6f 69   expects the joi
24af5 6e 20 6f 70 65 72 61 74 6f 72 20 74 6f 20 62 65  n operator to be
24af6 20 6f 6e 20 74 68 65 20 72 69 67 68 74 20 6f 70   on the right op
24af7 65 72 61 6e 64 2e 20 20 54 68 69 73 20 72 6f 75  erand.  This rou
24af8 74 69 6e 65 0a 2a 2a 20 53 68 69 66 74 73 20 61  tine.** Shifts a
24af9 6c 6c 20 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72  ll join operator
24afa 73 20 66 72 6f 6d 20 6c 65 66 74 20 74 6f 20 72  s from left to r
24afb 69 67 68 74 20 66 6f 72 20 61 6e 20 65 6e 74 69  ight for an enti
24afc 72 65 20 46 52 4f 4d 0a 2a 2a 20 63 6c 61 75 73  re FROM.** claus
24afd 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65  e..**.** Example
24afe 3a 20 53 75 70 70 6f 73 65 20 74 68 65 20 6a 6f  : Suppose the jo
24aff 69 6e 20 69 73 20 6c 69 6b 65 20 74 68 69 73 3a  in is like this:
24b00 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  .**.**          
24b01 20 41 20 6e 61 74 75 72 61 6c 20 63 72 6f 73 73   A natural cross
24b02 20 6a 6f 69 6e 20 42 0a 2a 2a 0a 2a 2a 20 54 68   join B.**.** Th
24b03 65 20 6f 70 65 72 61 74 6f 72 20 69 73 20 22 6e  e operator is "n
24b04 61 74 75 72 61 6c 20 63 72 6f 73 73 20 6a 6f 69  atural cross joi
24b05 6e 22 2e 20 20 54 68 65 20 41 20 61 6e 64 20 42  n".  The A and B
24b06 20 6f 70 65 72 61 6e 64 73 20 61 72 65 20 73 74   operands are st
24b07 6f 72 65 64 0a 2a 2a 20 69 6e 20 70 2d 3e 61 5b  ored.** in p->a[
24b08 30 5d 20 61 6e 64 20 70 2d 3e 61 5b 31 5d 2c 20  0] and p->a[1], 
24b09 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 20 54  respectively.  T
24b0a 68 65 20 70 61 72 73 65 72 20 69 6e 69 74 69 61  he parser initia
24b0b 6c 6c 79 20 73 74 6f 72 65 73 20 74 68 65 0a 2a  lly stores the.*
24b0c 2a 20 6f 70 65 72 61 74 6f 72 20 77 69 74 68 20  * operator with 
24b0d 41 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  A.  This routine
24b0e 20 73 68 69 66 74 73 20 74 68 61 74 20 6f 70 65   shifts that ope
24b0f 72 61 74 6f 72 20 6f 76 65 72 20 74 6f 20 42 2e  rator over to B.
24b10 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
24b11 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
24b12 72 63 4c 69 73 74 53 68 69 66 74 4a 6f 69 6e 54  rcListShiftJoinT
24b13 79 70 65 28 53 72 63 4c 69 73 74 20 2a 70 29 7b  ype(SrcList *p){
24b14 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e 61  .  if( p && p->a
24b15 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   ){.    int i;. 
24b16 20 20 20 66 6f 72 28 69 3d 70 2d 3e 6e 53 72 63     for(i=p->nSrc
24b17 2d 31 3b 20 69 3e 30 3b 20 69 2d 2d 29 7b 0a 20  -1; i>0; i--){. 
24b18 20 20 20 20 20 70 2d 3e 61 5b 69 5d 2e 6a 6f 69       p->a[i].joi
24b19 6e 74 79 70 65 20 3d 20 70 2d 3e 61 5b 69 2d 31  ntype = p->a[i-1
24b1a 5d 2e 6a 6f 69 6e 74 79 70 65 3b 0a 20 20 20 20  ].jointype;.    
24b1b 7d 0a 20 20 20 20 70 2d 3e 61 5b 30 5d 2e 6a 6f  }.    p->a[0].jo
24b1c 69 6e 74 79 70 65 20 3d 20 30 3b 0a 20 20 7d 0a  intype = 0;.  }.
24b1d 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65 67 69 6e 20 61  }../*.** Begin a
24b1e 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2f 0a   transaction.*/.
24b1f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
24b20 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e  oid sqlite3Begin
24b21 54 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73  Transaction(Pars
24b22 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 74  e *pParse, int t
24b23 79 70 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  ype){.  sqlite3 
24b24 2a 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a  *db;.  Vdbe *v;.
24b25 20 20 69 6e 74 20 69 3b 0a 0a 20 20 61 73 73 65    int i;..  asse
24b26 72 74 28 20 70 50 61 72 73 65 21 3d 30 20 29 3b  rt( pParse!=0 );
24b27 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  .  db = pParse->
24b28 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62  db;.  assert( db
24b29 21 3d 30 20 29 3b 0a 2f 2a 20 20 69 66 28 20 64  !=0 );./*  if( d
24b2a 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 3d 3d 30  b->aDb[0].pBt==0
24b2b 20 29 20 72 65 74 75 72 6e 3b 20 2a 2f 0a 20 20   ) return; */.  
24b2c 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43  if( sqlite3AuthC
24b2d 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c  heck(pParse, SQL
24b2e 49 54 45 5f 54 52 41 4e 53 41 43 54 49 4f 4e 2c  ITE_TRANSACTION,
24b2f 20 22 42 45 47 49 4e 22 2c 20 30 2c 20 30 29 20   "BEGIN", 0, 0) 
24b30 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  ){.    return;. 
24b31 20 7d 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33   }.  v = sqlite3
24b32 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
24b33 0a 20 20 69 66 28 20 21 76 20 29 20 72 65 74 75  .  if( !v ) retu
24b34 72 6e 3b 0a 20 20 69 66 28 20 74 79 70 65 21 3d  rn;.  if( type!=
24b35 54 4b 5f 44 45 46 45 52 52 45 44 20 29 7b 0a 20  TK_DEFERRED ){. 
24b36 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62     for(i=0; i<db
24b37 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nDb; i++){.   
24b38 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
24b39 64 4f 70 32 28 76 2c 20 4f 50 5f 54 72 61 6e 73  dOp2(v, OP_Trans
24b3a 61 63 74 69 6f 6e 2c 20 69 2c 20 28 74 79 70 65  action, i, (type
24b3b 3d 3d 54 4b 5f 45 58 43 4c 55 53 49 56 45 29 2b  ==TK_EXCLUSIVE)+
24b3c 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
24b3d 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28 76  3VdbeUsesBtree(v
24b3e 2c 20 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  , i);.    }.  }.
24b3f 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
24b40 4f 70 32 28 76 2c 20 4f 50 5f 41 75 74 6f 43 6f  Op2(v, OP_AutoCo
24b41 6d 6d 69 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a  mmit, 0, 0);.}..
24b42 2f 2a 0a 2a 2a 20 43 6f 6d 6d 69 74 20 61 20 74  /*.** Commit a t
24b43 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2f 0a 53 51  ransaction.*/.SQ
24b44 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
24b45 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 54  d sqlite3CommitT
24b46 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65  ransaction(Parse
24b47 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 73 71 6c   *pParse){.  sql
24b48 69 74 65 33 20 2a 64 62 3b 0a 20 20 56 64 62 65  ite3 *db;.  Vdbe
24b49 20 2a 76 3b 0a 0a 20 20 61 73 73 65 72 74 28 20   *v;..  assert( 
24b4a 70 50 61 72 73 65 21 3d 30 20 29 3b 0a 20 20 64  pParse!=0 );.  d
24b4b 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
24b4c 20 20 61 73 73 65 72 74 28 20 64 62 21 3d 30 20    assert( db!=0 
24b4d 29 3b 0a 2f 2a 20 20 69 66 28 20 64 62 2d 3e 61  );./*  if( db->a
24b4e 44 62 5b 30 5d 2e 70 42 74 3d 3d 30 20 29 20 72  Db[0].pBt==0 ) r
24b4f 65 74 75 72 6e 3b 20 2a 2f 0a 20 20 69 66 28 20  eturn; */.  if( 
24b50 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
24b51 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f  (pParse, SQLITE_
24b52 54 52 41 4e 53 41 43 54 49 4f 4e 2c 20 22 43 4f  TRANSACTION, "CO
24b53 4d 4d 49 54 22 2c 20 30 2c 20 30 29 20 29 7b 0a  MMIT", 0, 0) ){.
24b54 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
24b55 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
24b56 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
24b57 69 66 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c  if( v ){.    sql
24b58 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
24b59 2c 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 2c  , OP_AutoCommit,
24b5a 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f   1, 0);.  }.}../
24b5b 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 20  *.** Rollback a 
24b5c 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2f 0a 53  transaction.*/.S
24b5d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
24b5e 69 64 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61  id sqlite3Rollba
24b5f 63 6b 54 72 61 6e 73 61 63 74 69 6f 6e 28 50 61  ckTransaction(Pa
24b60 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20  rse *pParse){.  
24b61 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 56  sqlite3 *db;.  V
24b62 64 62 65 20 2a 76 3b 0a 0a 20 20 61 73 73 65 72  dbe *v;..  asser
24b63 74 28 20 70 50 61 72 73 65 21 3d 30 20 29 3b 0a  t( pParse!=0 );.
24b64 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64    db = pParse->d
24b65 62 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 21  b;.  assert( db!
24b66 3d 30 20 29 3b 0a 2f 2a 20 20 69 66 28 20 64 62  =0 );./*  if( db
24b67 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 3d 3d 30 20  ->aDb[0].pBt==0 
24b68 29 20 72 65 74 75 72 6e 3b 20 2a 2f 0a 20 20 69  ) return; */.  i
24b69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  f( sqlite3AuthCh
24b6a 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49  eck(pParse, SQLI
24b6b 54 45 5f 54 52 41 4e 53 41 43 54 49 4f 4e 2c 20  TE_TRANSACTION, 
24b6c 22 52 4f 4c 4c 42 41 43 4b 22 2c 20 30 2c 20 30  "ROLLBACK", 0, 0
24b6d 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b  ) ){.    return;
24b6e 0a 20 20 7d 0a 20 20 76 20 3d 20 73 71 6c 69 74  .  }.  v = sqlit
24b6f 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
24b70 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20  );.  if( v ){.  
24b71 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
24b72 4f 70 32 28 76 2c 20 4f 50 5f 41 75 74 6f 43 6f  Op2(v, OP_AutoCo
24b73 6d 6d 69 74 2c 20 31 2c 20 31 29 3b 0a 20 20 7d  mmit, 1, 1);.  }
24b74 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
24b75 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
24b76 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20  d by the parser 
24b77 77 68 65 6e 20 69 74 20 70 61 72 73 65 73 20 61  when it parses a
24b78 20 63 6f 6d 6d 61 6e 64 20 74 6f 20 63 72 65 61   command to crea
24b79 74 65 2c 0a 2a 2a 20 72 65 6c 65 61 73 65 20 6f  te,.** release o
24b7a 72 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 20 53 51  r rollback an SQ
24b7b 4c 20 73 61 76 65 70 6f 69 6e 74 2e 20 0a 2a 2f  L savepoint. .*/
24b7c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
24b7d 76 6f 69 64 20 73 71 6c 69 74 65 33 53 61 76 65  void sqlite3Save
24b7e 70 6f 69 6e 74 28 50 61 72 73 65 20 2a 70 50 61  point(Parse *pPa
24b7f 72 73 65 2c 20 69 6e 74 20 6f 70 2c 20 54 6f 6b  rse, int op, Tok
24b80 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20 20 63 68  en *pName){.  ch
24b81 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 73 71 6c 69  ar *zName = sqli
24b82 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e  te3NameFromToken
24b83 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4e 61  (pParse->db, pNa
24b84 6d 65 29 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65  me);.  if( zName
24b85 20 29 7b 0a 20 20 20 20 56 64 62 65 20 2a 76 20   ){.    Vdbe *v 
24b86 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
24b87 28 70 50 61 72 73 65 29 3b 0a 23 69 66 6e 64 65  (pParse);.#ifnde
24b88 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
24b89 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20  THORIZATION.    
24b8a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
24b8b 72 20 2a 61 7a 5b 5d 20 3d 20 7b 20 22 42 45 47  r *az[] = { "BEG
24b8c 49 4e 22 2c 20 22 52 45 4c 45 41 53 45 22 2c 20  IN", "RELEASE", 
24b8d 22 52 4f 4c 4c 42 41 43 4b 22 20 7d 3b 0a 20 20  "ROLLBACK" };.  
24b8e 20 20 61 73 73 65 72 74 28 20 21 53 41 56 45 50    assert( !SAVEP
24b8f 4f 49 4e 54 5f 42 45 47 49 4e 20 26 26 20 53 41  OINT_BEGIN && SA
24b90 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 3d  VEPOINT_RELEASE=
24b91 3d 31 20 26 26 20 53 41 56 45 50 4f 49 4e 54 5f  =1 && SAVEPOINT_
24b92 52 4f 4c 4c 42 41 43 4b 3d 3d 32 20 29 3b 0a 23  ROLLBACK==2 );.#
24b93 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20 21 76  endif.    if( !v
24b94 20 7c 7c 20 73 71 6c 69 74 65 33 41 75 74 68 43   || sqlite3AuthC
24b95 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c  heck(pParse, SQL
24b96 49 54 45 5f 53 41 56 45 50 4f 49 4e 54 2c 20 61  ITE_SAVEPOINT, a
24b97 7a 5b 6f 70 5d 2c 20 7a 4e 61 6d 65 2c 20 30 29  z[op], zName, 0)
24b98 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
24b99 33 44 62 46 72 65 65 28 70 50 61 72 73 65 2d 3e  3DbFree(pParse->
24b9a 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  db, zName);.    
24b9b 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a    return;.    }.
24b9c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
24b9d 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 61 76 65  ddOp4(v, OP_Save
24b9e 70 6f 69 6e 74 2c 20 6f 70 2c 20 30 2c 20 30 2c  point, op, 0, 0,
24b9f 20 7a 4e 61 6d 65 2c 20 50 34 5f 44 59 4e 41 4d   zName, P4_DYNAM
24ba0 49 43 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  IC);.  }.}../*.*
24ba1 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20  * Make sure the 
24ba2 54 45 4d 50 20 64 61 74 61 62 61 73 65 20 69 73  TEMP database is
24ba3 20 6f 70 65 6e 20 61 6e 64 20 61 76 61 69 6c 61   open and availa
24ba4 62 6c 65 20 66 6f 72 20 75 73 65 2e 20 20 52 65  ble for use.  Re
24ba5 74 75 72 6e 0a 2a 2a 20 74 68 65 20 6e 75 6d 62  turn.** the numb
24ba6 65 72 20 6f 66 20 65 72 72 6f 72 73 2e 20 20 4c  er of errors.  L
24ba7 65 61 76 65 20 61 6e 79 20 65 72 72 6f 72 20 6d  eave any error m
24ba8 65 73 73 61 67 65 73 20 69 6e 20 74 68 65 20 70  essages in the p
24ba9 50 61 72 73 65 20 73 74 72 75 63 74 75 72 65 2e  Parse structure.
24baa 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
24bab 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 70  TE int sqlite3Op
24bac 65 6e 54 65 6d 70 44 61 74 61 62 61 73 65 28 50  enTempDatabase(P
24bad 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20  arse *pParse){. 
24bae 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
24baf 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28  Parse->db;.  if(
24bb0 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 3d   db->aDb[1].pBt=
24bb1 3d 30 20 26 26 20 21 70 50 61 72 73 65 2d 3e 65  =0 && !pParse->e
24bb2 78 70 6c 61 69 6e 20 29 7b 0a 20 20 20 20 69 6e  xplain ){.    in
24bb3 74 20 72 63 3b 0a 20 20 20 20 73 74 61 74 69 63  t rc;.    static
24bb4 20 63 6f 6e 73 74 20 69 6e 74 20 66 6c 61 67 73   const int flags
24bb5 20 3d 20 0a 20 20 20 20 20 20 20 20 20 20 53 51   = .          SQ
24bb6 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
24bb7 49 54 45 20 7c 0a 20 20 20 20 20 20 20 20 20 20  ITE |.          
24bb8 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41  SQLITE_OPEN_CREA
24bb9 54 45 20 7c 0a 20 20 20 20 20 20 20 20 20 20 53  TE |.          S
24bba 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55  QLITE_OPEN_EXCLU
24bbb 53 49 56 45 20 7c 0a 20 20 20 20 20 20 20 20 20  SIVE |.         
24bbc 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c   SQLITE_OPEN_DEL
24bbd 45 54 45 4f 4e 43 4c 4f 53 45 20 7c 0a 20 20 20  ETEONCLOSE |.   
24bbe 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50         SQLITE_OP
24bbf 45 4e 5f 54 45 4d 50 5f 44 42 3b 0a 0a 20 20 20  EN_TEMP_DB;..   
24bc0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
24bc1 65 65 46 61 63 74 6f 72 79 28 64 62 2c 20 30 2c  eeFactory(db, 0,
24bc2 20 30 2c 20 53 51 4c 49 54 45 5f 44 45 46 41 55   0, SQLITE_DEFAU
24bc3 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 2c 20 66  LT_CACHE_SIZE, f
24bc4 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20 20  lags,.          
24bc5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24bc6 20 20 20 20 20 20 20 26 64 62 2d 3e 61 44 62 5b         &db->aDb[
24bc7 31 5d 2e 70 42 74 29 3b 0a 20 20 20 20 69 66 28  1].pBt);.    if(
24bc8 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
24bc9 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
24bca 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
24bcb 22 75 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20  "unable to open 
24bcc 61 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61  a temporary data
24bcd 62 61 73 65 20 22 0a 20 20 20 20 20 20 20 20 22  base ".        "
24bce 66 69 6c 65 20 66 6f 72 20 73 74 6f 72 69 6e 67  file for storing
24bcf 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
24bd0 73 22 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73  s");.      pPars
24bd1 65 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 20  e->rc = rc;.    
24bd2 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
24bd3 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 64  }.    assert( (d
24bd4 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54  b->flags & SQLIT
24bd5 45 5f 49 6e 54 72 61 6e 73 29 3d 3d 30 20 7c 7c  E_InTrans)==0 ||
24bd6 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20   db->autoCommit 
24bd7 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64  );.    assert( d
24bd8 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d  b->aDb[1].pSchem
24bd9 61 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  a );.    sqlite3
24bda 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65  PagerJournalMode
24bdb 28 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67  (sqlite3BtreePag
24bdc 65 72 28 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42  er(db->aDb[1].pB
24bdd 74 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  t),.            
24bde 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24bdf 64 62 2d 3e 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d  db->dfltJournalM
24be0 6f 64 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ode);.  }.  retu
24be1 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  rn 0;.}../*.** G
24be2 65 6e 65 72 61 74 65 20 56 44 42 45 20 63 6f 64  enerate VDBE cod
24be3 65 20 74 68 61 74 20 77 69 6c 6c 20 76 65 72 69  e that will veri
24be4 66 79 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f  fy the schema co
24be5 6f 6b 69 65 20 61 6e 64 20 73 74 61 72 74 0a 2a  okie and start.*
24be6 2a 20 61 20 72 65 61 64 2d 74 72 61 6e 73 61 63  * a read-transac
24be7 74 69 6f 6e 20 66 6f 72 20 61 6c 6c 20 6e 61 6d  tion for all nam
24be8 65 64 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ed database file
24be9 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69  s..**.** It is i
24bea 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20 61 6c  mportant that al
24beb 6c 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 73  l schema cookies
24bec 20 62 65 20 76 65 72 69 66 69 65 64 20 61 6e 64   be verified and
24bed 20 61 6c 6c 0a 2a 2a 20 72 65 61 64 20 74 72 61   all.** read tra
24bee 6e 73 61 63 74 69 6f 6e 73 20 62 65 20 73 74 61  nsactions be sta
24bef 72 74 65 64 20 62 65 66 6f 72 65 20 61 6e 79 74  rted before anyt
24bf0 68 69 6e 67 20 65 6c 73 65 20 68 61 70 70 65 6e  hing else happen
24bf1 73 20 69 6e 0a 2a 2a 20 74 68 65 20 56 44 42 45  s in.** the VDBE
24bf2 20 70 72 6f 67 72 61 6d 2e 20 20 42 75 74 20 74   program.  But t
24bf3 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20  his routine can 
24bf4 62 65 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20  be called after 
24bf5 6d 75 63 68 20 6f 74 68 65 72 0a 2a 2a 20 63 6f  much other.** co
24bf6 64 65 20 68 61 73 20 62 65 65 6e 20 67 65 6e 65  de has been gene
24bf7 72 61 74 65 64 2e 20 20 53 6f 20 68 65 72 65 20  rated.  So here 
24bf8 69 73 20 77 68 61 74 20 77 65 20 64 6f 3a 0a 2a  is what we do:.*
24bf9 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74  *.** The first t
24bfa 69 6d 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ime this routine
24bfb 20 69 73 20 63 61 6c 6c 65 64 2c 20 77 65 20 63   is called, we c
24bfc 6f 64 65 20 61 6e 20 4f 50 5f 47 6f 74 6f 20 74  ode an OP_Goto t
24bfd 68 61 74 0a 2a 2a 20 77 69 6c 6c 20 6a 75 6d 70  hat.** will jump
24bfe 20 74 6f 20 61 20 73 75 62 72 6f 75 74 69 6e 65   to a subroutine
24bff 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74   at the end of t
24c00 68 65 20 70 72 6f 67 72 61 6d 2e 20 20 54 68 65  he program.  The
24c01 6e 20 77 65 0a 2a 2a 20 72 65 63 6f 72 64 20 65  n we.** record e
24c02 76 65 72 79 20 64 61 74 61 62 61 73 65 20 74 68  very database th
24c03 61 74 20 6e 65 65 64 73 20 69 74 73 20 73 63 68  at needs its sch
24c04 65 6d 61 20 76 65 72 69 66 69 65 64 20 69 6e 20  ema verified in 
24c05 74 68 65 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 63  the.** pParse->c
24c06 6f 6f 6b 69 65 4d 61 73 6b 20 66 69 65 6c 64 2e  ookieMask field.
24c07 20 20 4c 61 74 65 72 2c 20 61 66 74 65 72 20 61    Later, after a
24c08 6c 6c 20 6f 74 68 65 72 20 63 6f 64 65 20 68 61  ll other code ha
24c09 73 20 62 65 65 6e 0a 2a 2a 20 67 65 6e 65 72 61  s been.** genera
24c0a 74 65 64 2c 20 74 68 65 20 73 75 62 72 6f 75 74  ted, the subrout
24c0b 69 6e 65 20 74 68 61 74 20 64 6f 65 73 20 74 68  ine that does th
24c0c 65 20 63 6f 6f 6b 69 65 20 76 65 72 69 66 69 63  e cookie verific
24c0d 61 74 69 6f 6e 73 20 61 6e 64 0a 2a 2a 20 73 74  ations and.** st
24c0e 61 72 74 73 20 74 68 65 20 74 72 61 6e 73 61 63  arts the transac
24c0f 74 69 6f 6e 73 20 77 69 6c 6c 20 62 65 20 63 6f  tions will be co
24c10 64 65 64 20 61 6e 64 20 74 68 65 20 4f 50 5f 47  ded and the OP_G
24c11 6f 74 6f 20 50 32 20 76 61 6c 75 65 0a 2a 2a 20  oto P2 value.** 
24c12 77 69 6c 6c 20 62 65 20 6d 61 64 65 20 74 6f 20  will be made to 
24c13 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 20 73 75  point to that su
24c14 62 72 6f 75 74 69 6e 65 2e 20 20 54 68 65 20 67  broutine.  The g
24c15 65 6e 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65  eneration of the
24c16 0a 2a 2a 20 63 6f 6f 6b 69 65 20 76 65 72 69 66  .** cookie verif
24c17 69 63 61 74 69 6f 6e 20 73 75 62 72 6f 75 74 69  ication subrouti
24c18 6e 65 20 63 6f 64 65 20 68 61 70 70 65 6e 73 20  ne code happens 
24c19 69 6e 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68  in sqlite3Finish
24c1a 43 6f 64 69 6e 67 28 29 2e 0a 2a 2a 0a 2a 2a 20  Coding()..**.** 
24c1b 49 66 20 69 44 62 3c 30 20 74 68 65 6e 20 63 6f  If iDb<0 then co
24c1c 64 65 20 74 68 65 20 4f 50 5f 47 6f 74 6f 20 6f  de the OP_Goto o
24c1d 6e 6c 79 20 2d 20 64 6f 6e 27 74 20 73 65 74 20  nly - don't set 
24c1e 66 6c 61 67 20 74 6f 20 76 65 72 69 66 79 20 74  flag to verify t
24c1f 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 6f 6e 20  he.** schema on 
24c20 61 6e 79 20 64 61 74 61 62 61 73 65 73 2e 20 20  any databases.  
24c21 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64  This can be used
24c22 20 74 6f 20 70 6f 73 69 74 69 6f 6e 20 74 68 65   to position the
24c23 20 4f 50 5f 47 6f 74 6f 0a 2a 2a 20 65 61 72 6c   OP_Goto.** earl
24c24 79 20 69 6e 20 74 68 65 20 63 6f 64 65 2c 20 62  y in the code, b
24c25 65 66 6f 72 65 20 77 65 20 6b 6e 6f 77 20 69 66  efore we know if
24c26 20 61 6e 79 20 64 61 74 61 62 61 73 65 20 74 61   any database ta
24c27 62 6c 65 73 20 77 69 6c 6c 20 62 65 20 75 73 65  bles will be use
24c28 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
24c29 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
24c2a 33 43 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d  3CodeVerifySchem
24c2b 61 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  a(Parse *pParse,
24c2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 50 61 72   int iDb){.  Par
24c2d 73 65 20 2a 70 54 6f 70 6c 65 76 65 6c 20 3d 20  se *pToplevel = 
24c2e 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f 70 6c  sqlite3ParseTopl
24c2f 65 76 65 6c 28 70 50 61 72 73 65 29 3b 0a 0a 20  evel(pParse);.. 
24c30 20 69 66 28 20 70 54 6f 70 6c 65 76 65 6c 2d 3e   if( pToplevel->
24c31 63 6f 6f 6b 69 65 47 6f 74 6f 3d 3d 30 20 29 7b  cookieGoto==0 ){
24c32 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 73  .    Vdbe *v = s
24c33 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 54  qlite3GetVdbe(pT
24c34 6f 70 6c 65 76 65 6c 29 3b 0a 20 20 20 20 69 66  oplevel);.    if
24c35 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ( v==0 ) return;
24c36 20 20 2f 2a 20 54 68 69 73 20 6f 6e 6c 79 20 68    /* This only h
24c37 61 70 70 65 6e 73 20 69 66 20 74 68 65 72 65 20  appens if there 
24c38 77 61 73 20 61 20 70 72 69 6f 72 20 65 72 72 6f  was a prior erro
24c39 72 20 2a 2f 0a 20 20 20 20 70 54 6f 70 6c 65 76  r */.    pToplev
24c3a 65 6c 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 20 3d  el->cookieGoto =
24c3b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
24c3c 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
24c3d 2c 20 30 29 2b 31 3b 0a 20 20 7d 0a 20 20 69 66  , 0)+1;.  }.  if
24c3e 28 20 69 44 62 3e 3d 30 20 29 7b 0a 20 20 20 20  ( iDb>=0 ){.    
24c3f 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 54  sqlite3 *db = pT
24c40 6f 70 6c 65 76 65 6c 2d 3e 64 62 3b 0a 20 20 20  oplevel->db;.   
24c41 20 69 6e 74 20 6d 61 73 6b 3b 0a 0a 20 20 20 20   int mask;..    
24c42 61 73 73 65 72 74 28 20 69 44 62 3c 64 62 2d 3e  assert( iDb<db->
24c43 6e 44 62 20 29 3b 0a 20 20 20 20 61 73 73 65 72  nDb );.    asser
24c44 74 28 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  t( db->aDb[iDb].
24c45 70 42 74 21 3d 30 20 7c 7c 20 69 44 62 3d 3d 31  pBt!=0 || iDb==1
24c46 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
24c47 69 44 62 3c 53 51 4c 49 54 45 5f 4d 41 58 5f 41  iDb<SQLITE_MAX_A
24c48 54 54 41 43 48 45 44 2b 32 20 29 3b 0a 20 20 20  TTACHED+2 );.   
24c49 20 6d 61 73 6b 20 3d 20 31 3c 3c 69 44 62 3b 0a   mask = 1<<iDb;.
24c4a 20 20 20 20 69 66 28 20 28 70 54 6f 70 6c 65 76      if( (pToplev
24c4b 65 6c 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20 26  el->cookieMask &
24c4c 20 6d 61 73 6b 29 3d 3d 30 20 29 7b 0a 20 20 20   mask)==0 ){.   
24c4d 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 63 6f     pToplevel->co
24c4e 6f 6b 69 65 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b  okieMask |= mask
24c4f 3b 0a 20 20 20 20 20 20 70 54 6f 70 6c 65 76 65  ;.      pTopleve
24c50 6c 2d 3e 63 6f 6f 6b 69 65 56 61 6c 75 65 5b 69  l->cookieValue[i
24c51 44 62 5d 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44  Db] = db->aDb[iD
24c52 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65  b].pSchema->sche
24c53 6d 61 5f 63 6f 6f 6b 69 65 3b 0a 20 20 20 20 20  ma_cookie;.     
24c54 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44   if( !OMIT_TEMPD
24c55 42 20 26 26 20 69 44 62 3d 3d 31 20 29 7b 0a 20  B && iDb==1 ){. 
24c56 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 70         sqlite3Op
24c57 65 6e 54 65 6d 70 44 61 74 61 62 61 73 65 28 70  enTempDatabase(p
24c58 54 6f 70 6c 65 76 65 6c 29 3b 0a 20 20 20 20 20  Toplevel);.     
24c59 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a   }.    }.  }.}..
24c5a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 56  /*.** Generate V
24c5b 44 42 45 20 63 6f 64 65 20 74 68 61 74 20 70 72  DBE code that pr
24c5c 65 70 61 72 65 73 20 66 6f 72 20 64 6f 69 6e 67  epares for doing
24c5d 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e 20 74 68   an operation th
24c5e 61 74 0a 2a 2a 20 6d 69 67 68 74 20 63 68 61 6e  at.** might chan
24c5f 67 65 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  ge the database.
24c60 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
24c61 69 6e 65 20 73 74 61 72 74 73 20 61 20 6e 65 77  ine starts a new
24c62 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 66 20   transaction if 
24c63 77 65 20 61 72 65 20 6e 6f 74 20 61 6c 72 65 61  we are not alrea
24c64 64 79 20 77 69 74 68 69 6e 0a 2a 2a 20 61 20 74  dy within.** a t
24c65 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 49 66 20  ransaction.  If 
24c66 77 65 20 61 72 65 20 61 6c 72 65 61 64 79 20 77  we are already w
24c67 69 74 68 69 6e 20 61 20 74 72 61 6e 73 61 63 74  ithin a transact
24c68 69 6f 6e 2c 20 74 68 65 6e 20 61 20 63 68 65 63  ion, then a chec
24c69 6b 70 6f 69 6e 74 0a 2a 2a 20 69 73 20 73 65 74  kpoint.** is set
24c6a 20 69 66 20 74 68 65 20 73 65 74 53 74 61 74 65   if the setState
24c6b 6d 65 6e 74 20 70 61 72 61 6d 65 74 65 72 20 69  ment parameter i
24c6c 73 20 74 72 75 65 2e 20 20 41 20 63 68 65 63 6b  s true.  A check
24c6d 70 6f 69 6e 74 20 73 68 6f 75 6c 64 0a 2a 2a 20  point should.** 
24c6e 62 65 20 73 65 74 20 66 6f 72 20 6f 70 65 72 61  be set for opera
24c6f 74 69 6f 6e 73 20 74 68 61 74 20 6d 69 67 68 74  tions that might
24c70 20 66 61 69 6c 20 28 64 75 65 20 74 6f 20 61 20   fail (due to a 
24c71 63 6f 6e 73 74 72 61 69 6e 74 29 20 70 61 72 74  constraint) part
24c72 20 6f 66 0a 2a 2a 20 74 68 65 20 77 61 79 20 74   of.** the way t
24c73 68 72 6f 75 67 68 20 61 6e 64 20 77 68 69 63 68  hrough and which
24c74 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f 20 75 6e   will need to un
24c75 64 6f 20 73 6f 6d 65 20 77 72 69 74 65 73 20 77  do some writes w
24c76 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f  ithout having to
24c77 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65  .** rollback the
24c78 20 77 68 6f 6c 65 20 74 72 61 6e 73 61 63 74 69   whole transacti
24c79 6f 6e 2e 20 20 46 6f 72 20 6f 70 65 72 61 74 69  on.  For operati
24c7a 6f 6e 73 20 77 68 65 72 65 20 61 6c 6c 20 63 6f  ons where all co
24c7b 6e 73 74 72 61 69 6e 74 73 0a 2a 2a 20 63 61 6e  nstraints.** can
24c7c 20 62 65 20 63 68 65 63 6b 65 64 20 62 65 66 6f   be checked befo
24c7d 72 65 20 61 6e 79 20 63 68 61 6e 67 65 73 20 61  re any changes a
24c7e 72 65 20 6d 61 64 65 20 74 6f 20 74 68 65 20 64  re made to the d
24c7f 61 74 61 62 61 73 65 2c 20 69 74 20 69 73 20 6e  atabase, it is n
24c80 65 76 65 72 0a 2a 2a 20 6e 65 63 65 73 73 61 72  ever.** necessar
24c81 79 20 74 6f 20 75 6e 64 6f 20 61 20 77 72 69 74  y to undo a writ
24c82 65 20 61 6e 64 20 74 68 65 20 63 68 65 63 6b 70  e and the checkp
24c83 6f 69 6e 74 20 73 68 6f 75 6c 64 20 6e 6f 74 20  oint should not 
24c84 62 65 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54  be set..*/.SQLIT
24c85 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
24c86 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65  qlite3BeginWrite
24c87 4f 70 65 72 61 74 69 6f 6e 28 50 61 72 73 65 20  Operation(Parse 
24c88 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 73 65 74  *pParse, int set
24c89 53 74 61 74 65 6d 65 6e 74 2c 20 69 6e 74 20 69  Statement, int i
24c8a 44 62 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 54  Db){.  Parse *pT
24c8b 6f 70 6c 65 76 65 6c 20 3d 20 73 71 6c 69 74 65  oplevel = sqlite
24c8c 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70  3ParseToplevel(p
24c8d 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65  Parse);.  sqlite
24c8e 33 43 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d  3CodeVerifySchem
24c8f 61 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a  a(pParse, iDb);.
24c90 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 77 72 69    pToplevel->wri
24c91 74 65 4d 61 73 6b 20 7c 3d 20 31 3c 3c 69 44 62  teMask |= 1<<iDb
24c92 3b 0a 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 69  ;.  pToplevel->i
24c93 73 4d 75 6c 74 69 57 72 69 74 65 20 7c 3d 20 73  sMultiWrite |= s
24c94 65 74 53 74 61 74 65 6d 65 6e 74 3b 0a 7d 0a 0a  etStatement;.}..
24c95 2f 2a 20 0a 2a 2a 20 53 65 74 20 74 68 65 20 22  /* .** Set the "
24c96 6d 61 79 20 74 68 72 6f 77 20 61 62 6f 72 74 20  may throw abort 
24c97 65 78 63 65 70 74 69 6f 6e 22 20 66 6c 61 67 20  exception" flag 
24c98 66 6f 72 20 74 68 65 20 73 74 61 74 65 6d 65 6e  for the statemen
24c99 74 20 63 75 72 72 65 6e 74 6c 79 20 0a 2a 2a 20  t currently .** 
24c9a 62 65 69 6e 67 20 63 6f 64 65 64 2e 0a 2a 2f 0a  being coded..*/.
24c9b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
24c9c 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 79 41 62  oid sqlite3MayAb
24c9d 6f 72 74 28 50 61 72 73 65 20 2a 70 50 61 72 73  ort(Parse *pPars
24c9e 65 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 54 6f  e){.  Parse *pTo
24c9f 70 6c 65 76 65 6c 20 3d 20 73 71 6c 69 74 65 33  plevel = sqlite3
24ca0 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70 50  ParseToplevel(pP
24ca1 61 72 73 65 29 3b 0a 20 20 70 54 6f 70 6c 65 76  arse);.  pToplev
24ca2 65 6c 2d 3e 6d 61 79 41 62 6f 72 74 20 3d 20 31  el->mayAbort = 1
24ca3 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 64 65 20  ;.}../*.** Code 
24ca4 61 6e 20 4f 50 5f 48 61 6c 74 20 74 68 61 74 20  an OP_Halt that 
24ca5 63 61 75 73 65 73 20 74 68 65 20 76 64 62 65 20  causes the vdbe 
24ca6 74 6f 20 72 65 74 75 72 6e 20 61 6e 20 53 51 4c  to return an SQL
24ca7 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 0a 2a  ITE_CONSTRAINT.*
24ca8 2a 20 65 72 72 6f 72 2e 20 54 68 65 20 6f 6e 45  * error. The onE
24ca9 72 72 6f 72 20 70 61 72 61 6d 65 74 65 72 20 64  rror parameter d
24caa 65 74 65 72 6d 69 6e 65 73 20 77 68 69 63 68 20  etermines which 
24cab 28 69 66 20 61 6e 79 29 20 6f 66 20 74 68 65 20  (if any) of the 
24cac 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 61 6e 64  statement.** and
24cad 2f 6f 72 20 63 75 72 72 65 6e 74 20 74 72 61 6e  /or current tran
24cae 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65  saction is rolle
24caf 64 20 62 61 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54  d back..*/.SQLIT
24cb0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
24cb1 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73 74 72  qlite3HaltConstr
24cb2 61 69 6e 74 28 50 61 72 73 65 20 2a 70 50 61 72  aint(Parse *pPar
24cb3 73 65 2c 20 69 6e 74 20 6f 6e 45 72 72 6f 72 2c  se, int onError,
24cb4 20 63 68 61 72 20 2a 70 34 2c 20 69 6e 74 20 70   char *p4, int p
24cb5 34 74 79 70 65 29 7b 0a 20 20 56 64 62 65 20 2a  4type){.  Vdbe *
24cb6 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
24cb7 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66  be(pParse);.  if
24cb8 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 41 62  ( onError==OE_Ab
24cb9 6f 72 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ort ){.    sqlit
24cba 65 33 4d 61 79 41 62 6f 72 74 28 70 50 61 72 73  e3MayAbort(pPars
24cbb 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  e);.  }.  sqlite
24cbc 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
24cbd 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54 45 5f 43  P_Halt, SQLITE_C
24cbe 4f 4e 53 54 52 41 49 4e 54 2c 20 6f 6e 45 72 72  ONSTRAINT, onErr
24cbf 6f 72 2c 20 30 2c 20 70 34 2c 20 70 34 74 79 70  or, 0, p4, p4typ
24cc0 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65  e);.}../*.** Che
24cc1 63 6b 20 74 6f 20 73 65 65 20 69 66 20 70 49 6e  ck to see if pIn
24cc2 64 65 78 20 75 73 65 73 20 74 68 65 20 63 6f 6c  dex uses the col
24cc3 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
24cc4 70 43 6f 6c 6c 2e 20 20 52 65 74 75 72 6e 0a 2a  pColl.  Return.*
24cc5 2a 20 74 72 75 65 20 69 66 20 69 74 20 64 6f 65  * true if it doe
24cc6 73 20 61 6e 64 20 66 61 6c 73 65 20 69 66 20 69  s and false if i
24cc7 74 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2f 0a 23  t does not..*/.#
24cc8 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
24cc9 49 54 5f 52 45 49 4e 44 45 58 0a 73 74 61 74 69  IT_REINDEX.stati
24cca 63 20 69 6e 74 20 63 6f 6c 6c 61 74 69 6f 6e 4d  c int collationM
24ccb 61 74 63 68 28 63 6f 6e 73 74 20 63 68 61 72 20  atch(const char 
24ccc 2a 7a 43 6f 6c 6c 2c 20 49 6e 64 65 78 20 2a 70  *zColl, Index *p
24ccd 49 6e 64 65 78 29 7b 0a 20 20 69 6e 74 20 69 3b  Index){.  int i;
24cce 0a 20 20 61 73 73 65 72 74 28 20 7a 43 6f 6c 6c  .  assert( zColl
24ccf 21 3d 30 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30  !=0 );.  for(i=0
24cd0 3b 20 69 3c 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c  ; i<pIndex->nCol
24cd1 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63  umn; i++){.    c
24cd2 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70  onst char *z = p
24cd3 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d  Index->azColl[i]
24cd4 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 7a 21  ;.    assert( z!
24cd5 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 30 3d  =0 );.    if( 0=
24cd6 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28  =sqlite3StrICmp(
24cd7 7a 2c 20 7a 43 6f 6c 6c 29 20 29 7b 0a 20 20 20  z, zColl) ){.   
24cd8 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
24cd9 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
24cda 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  0;.}.#endif../*.
24cdb 2a 2a 20 52 65 63 6f 6d 70 75 74 65 20 61 6c 6c  ** Recompute all
24cdc 20 69 6e 64 69 63 65 73 20 6f 66 20 70 54 61 62   indices of pTab
24cdd 20 74 68 61 74 20 75 73 65 20 74 68 65 20 63 6f   that use the co
24cde 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
24cdf 20 70 43 6f 6c 6c 2e 0a 2a 2a 20 49 66 20 70 43   pColl..** If pC
24ce0 6f 6c 6c 3d 3d 30 20 74 68 65 6e 20 72 65 63 6f  oll==0 then reco
24ce1 6d 70 75 74 65 20 61 6c 6c 20 69 6e 64 69 63 65  mpute all indice
24ce2 73 20 6f 66 20 70 54 61 62 2e 0a 2a 2f 0a 23 69  s of pTab..*/.#i
24ce3 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
24ce4 54 5f 52 45 49 4e 44 45 58 0a 73 74 61 74 69 63  T_REINDEX.static
24ce5 20 76 6f 69 64 20 72 65 69 6e 64 65 78 54 61 62   void reindexTab
24ce6 6c 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  le(Parse *pParse
24ce7 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 63  , Table *pTab, c
24ce8 68 61 72 20 63 6f 6e 73 74 20 2a 7a 43 6f 6c 6c  har const *zColl
24ce9 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64  ){.  Index *pInd
24cea 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ex;             
24ceb 20 2f 2a 20 41 6e 20 69 6e 64 65 78 20 61 73 73   /* An index ass
24cec 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 54 61  ociated with pTa
24ced 62 20 2a 2f 0a 0a 20 20 66 6f 72 28 70 49 6e 64  b */..  for(pInd
24cee 65 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  ex=pTab->pIndex;
24cef 20 70 49 6e 64 65 78 3b 20 70 49 6e 64 65 78 3d   pIndex; pIndex=
24cf0 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 29 7b 0a  pIndex->pNext){.
24cf1 20 20 20 20 69 66 28 20 7a 43 6f 6c 6c 3d 3d 30      if( zColl==0
24cf2 20 7c 7c 20 63 6f 6c 6c 61 74 69 6f 6e 4d 61 74   || collationMat
24cf3 63 68 28 7a 43 6f 6c 6c 2c 20 70 49 6e 64 65 78  ch(zColl, pIndex
24cf4 29 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  ) ){.      int i
24cf5 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
24cf6 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65  maToIndex(pParse
24cf7 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68  ->db, pTab->pSch
24cf8 65 6d 61 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ema);.      sqli
24cf9 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65  te3BeginWriteOpe
24cfa 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30  ration(pParse, 0
24cfb 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71  , iDb);.      sq
24cfc 6c 69 74 65 33 52 65 66 69 6c 6c 49 6e 64 65 78  lite3RefillIndex
24cfd 28 70 50 61 72 73 65 2c 20 70 49 6e 64 65 78 2c  (pParse, pIndex,
24cfe 20 2d 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   -1);.    }.  }.
24cff 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
24d00 52 65 63 6f 6d 70 75 74 65 20 61 6c 6c 20 69 6e  Recompute all in
24d01 64 69 63 65 73 20 6f 66 20 61 6c 6c 20 74 61 62  dices of all tab
24d02 6c 65 73 20 69 6e 20 61 6c 6c 20 64 61 74 61 62  les in all datab
24d03 61 73 65 73 20 77 68 65 72 65 20 74 68 65 0a 2a  ases where the.*
24d04 2a 20 69 6e 64 69 63 65 73 20 75 73 65 20 74 68  * indices use th
24d05 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  e collating sequ
24d06 65 6e 63 65 20 70 43 6f 6c 6c 2e 20 20 49 66 20  ence pColl.  If 
24d07 70 43 6f 6c 6c 3d 3d 30 20 74 68 65 6e 20 72 65  pColl==0 then re
24d08 63 6f 6d 70 75 74 65 0a 2a 2a 20 61 6c 6c 20 69  compute.** all i
24d09 6e 64 69 63 65 73 20 65 76 65 72 79 77 68 65 72  ndices everywher
24d0a 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  e..*/.#ifndef SQ
24d0b 4c 49 54 45 5f 4f 4d 49 54 5f 52 45 49 4e 44 45  LITE_OMIT_REINDE
24d0c 58 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65  X.static void re
24d0d 69 6e 64 65 78 44 61 74 61 62 61 73 65 73 28 50  indexDatabases(P
24d0e 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 68  arse *pParse, ch
24d0f 61 72 20 63 6f 6e 73 74 20 2a 7a 43 6f 6c 6c 29  ar const *zColl)
24d10 7b 0a 20 20 44 62 20 2a 70 44 62 3b 20 20 20 20  {.  Db *pDb;    
24d11 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24d12 2f 2a 20 41 20 73 69 6e 67 6c 65 20 64 61 74 61  /* A single data
24d13 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 44  base */.  int iD
24d14 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
24d15 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
24d16 61 62 61 73 65 20 69 6e 64 65 78 20 6e 75 6d 62  abase index numb
24d17 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  er */.  sqlite3 
24d18 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
24d19 3b 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62  ;   /* The datab
24d1a 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
24d1b 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 6b 3b  /.  HashElem *k;
24d1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24d1d 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f  /* For looping o
24d1e 76 65 72 20 74 61 62 6c 65 73 20 69 6e 20 70 44  ver tables in pD
24d1f 62 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  b */.  Table *pT
24d20 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ab;             
24d21 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 69 6e     /* A table in
24d22 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f   the database */
24d23 0a 0a 20 20 66 6f 72 28 69 44 62 3d 30 2c 20 70  ..  for(iDb=0, p
24d24 44 62 3d 64 62 2d 3e 61 44 62 3b 20 69 44 62 3c  Db=db->aDb; iDb<
24d25 64 62 2d 3e 6e 44 62 3b 20 69 44 62 2b 2b 2c 20  db->nDb; iDb++, 
24d26 70 44 62 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65  pDb++){.    asse
24d27 72 74 28 20 70 44 62 21 3d 30 20 29 3b 0a 20 20  rt( pDb!=0 );.  
24d28 20 20 66 6f 72 28 6b 3d 73 71 6c 69 74 65 48 61    for(k=sqliteHa
24d29 73 68 46 69 72 73 74 28 26 70 44 62 2d 3e 70 53  shFirst(&pDb->pS
24d2a 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 29 3b  chema->tblHash);
24d2b 20 20 6b 3b 20 6b 3d 73 71 6c 69 74 65 48 61 73    k; k=sqliteHas
24d2c 68 4e 65 78 74 28 6b 29 29 7b 0a 20 20 20 20 20  hNext(k)){.     
24d2d 20 70 54 61 62 20 3d 20 28 54 61 62 6c 65 2a 29   pTab = (Table*)
24d2e 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 6b  sqliteHashData(k
24d2f 29 3b 0a 20 20 20 20 20 20 72 65 69 6e 64 65 78  );.      reindex
24d30 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54  Table(pParse, pT
24d31 61 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20  ab, zColl);.    
24d32 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a  }.  }.}.#endif..
24d33 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
24d34 6f 64 65 20 66 6f 72 20 74 68 65 20 52 45 49 4e  ode for the REIN
24d35 44 45 58 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a  DEX command..**.
24d36 2a 2a 20 20 20 20 20 20 20 20 52 45 49 4e 44 45  **        REINDE
24d37 58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  X               
24d38 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20               -- 
24d39 31 0a 2a 2a 20 20 20 20 20 20 20 20 52 45 49 4e  1.**        REIN
24d3a 44 45 58 20 20 3c 63 6f 6c 6c 61 74 69 6f 6e 3e  DEX  <collation>
24d3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d                 -
24d3c 2d 20 32 0a 2a 2a 20 20 20 20 20 20 20 20 52 45  - 2.**        RE
24d3d 49 4e 44 45 58 20 20 3f 3c 64 61 74 61 62 61 73  INDEX  ?<databas
24d3e 65 3e 2e 3f 3c 74 61 62 6c 65 6e 61 6d 65 3e 20  e>.?<tablename> 
24d3f 20 2d 2d 20 33 0a 2a 2a 20 20 20 20 20 20 20 20   -- 3.**        
24d40 52 45 49 4e 44 45 58 20 20 3f 3c 64 61 74 61 62  REINDEX  ?<datab
24d41 61 73 65 3e 2e 3f 3c 69 6e 64 65 78 6e 61 6d 65  ase>.?<indexname
24d42 3e 20 20 2d 2d 20 34 0a 2a 2a 0a 2a 2a 20 46 6f  >  -- 4.**.** Fo
24d43 72 6d 20 31 20 63 61 75 73 65 73 20 61 6c 6c 20  rm 1 causes all 
24d44 69 6e 64 69 63 65 73 20 69 6e 20 61 6c 6c 20 61  indices in all a
24d45 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
24d46 73 20 74 6f 20 62 65 20 72 65 62 75 69 6c 74 2e  s to be rebuilt.
24d47 0a 2a 2a 20 46 6f 72 6d 20 32 20 72 65 62 75 69  .** Form 2 rebui
24d48 6c 64 73 20 61 6c 6c 20 69 6e 64 69 63 65 73 20  lds all indices 
24d49 69 6e 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73  in all databases
24d4a 20 74 68 61 74 20 75 73 65 20 74 68 65 20 6e 61   that use the na
24d4b 6d 65 64 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67  med.** collating
24d4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 46 6f 72 6d   function.  Form
24d4d 73 20 33 20 61 6e 64 20 34 20 72 65 62 75 69 6c  s 3 and 4 rebuil
24d4e 64 20 74 68 65 20 6e 61 6d 65 64 20 69 6e 64 65  d the named inde
24d4f 78 20 6f 72 20 61 6c 6c 0a 2a 2a 20 69 6e 64 69  x or all.** indi
24d50 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ces associated w
24d51 69 74 68 20 74 68 65 20 6e 61 6d 65 64 20 74 61  ith the named ta
24d52 62 6c 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ble..*/.#ifndef 
24d53 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 52 45 49 4e  SQLITE_OMIT_REIN
24d54 44 45 58 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  DEX.SQLITE_PRIVA
24d55 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52  TE void sqlite3R
24d56 65 69 6e 64 65 78 28 50 61 72 73 65 20 2a 70 50  eindex(Parse *pP
24d57 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61  arse, Token *pNa
24d58 6d 65 31 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d  me1, Token *pNam
24d59 65 32 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a  e2){.  CollSeq *
24d5a 70 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20 20  pColl;          
24d5b 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20     /* Collating 
24d5c 73 65 71 75 65 6e 63 65 20 74 6f 20 62 65 20 72  sequence to be r
24d5d 65 69 6e 64 65 78 65 64 2c 20 6f 72 20 4e 55 4c  eindexed, or NUL
24d5e 4c 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 3b 20  L */.  char *z; 
24d5f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24d60 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 61 20     /* Name of a 
24d61 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 2a  table or index *
24d62 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
24d63 7a 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  zDb;            
24d64 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64  /* Name of the d
24d65 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 54 61 62  atabase */.  Tab
24d66 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20  le *pTab;       
24d67 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 74 61           /* A ta
24d68 62 6c 65 20 69 6e 20 74 68 65 20 64 61 74 61 62  ble in the datab
24d69 61 73 65 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a  ase */.  Index *
24d6a 70 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20 20  pIndex;         
24d6b 20 20 20 20 20 2f 2a 20 41 6e 20 69 6e 64 65 78       /* An index
24d6c 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
24d6d 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20 69   pTab */.  int i
24d6e 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
24d6f 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
24d70 74 61 62 61 73 65 20 69 6e 64 65 78 20 6e 75 6d  tabase index num
24d71 62 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ber */.  sqlite3
24d72 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
24d73 62 3b 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61  b;   /* The data
24d74 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
24d75 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4f 62 6a  */.  Token *pObj
24d76 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20  Name;           
24d77 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
24d78 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 74  table or index t
24d79 6f 20 62 65 20 72 65 69 6e 64 65 78 65 64 20 2a  o be reindexed *
24d7a 2f 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65  /..  /* Read the
24d7b 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
24d7c 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  . If an error oc
24d7d 63 75 72 73 2c 20 6c 65 61 76 65 20 61 6e 20 65  curs, leave an e
24d7e 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 20 20 2a  rror message.  *
24d7f 2a 20 61 6e 64 20 63 6f 64 65 20 69 6e 20 70 50  * and code in pP
24d80 61 72 73 65 20 61 6e 64 20 72 65 74 75 72 6e 20  arse and return 
24d81 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 69 66 28 20 53  NULL. */.  if( S
24d82 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65  QLITE_OK!=sqlite
24d83 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72  3ReadSchema(pPar
24d84 73 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  se) ){.    retur
24d85 6e 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4e  n;.  }..  if( pN
24d86 61 6d 65 31 3d 3d 30 20 29 7b 0a 20 20 20 20 72  ame1==0 ){.    r
24d87 65 69 6e 64 65 78 44 61 74 61 62 61 73 65 73 28  eindexDatabases(
24d88 70 50 61 72 73 65 2c 20 30 29 3b 0a 20 20 20 20  pParse, 0);.    
24d89 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 20  return;.  }else 
24d8a 69 66 28 20 4e 45 56 45 52 28 70 4e 61 6d 65 32  if( NEVER(pName2
24d8b 3d 3d 30 29 20 7c 7c 20 70 4e 61 6d 65 32 2d 3e  ==0) || pName2->
24d8c 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 72  z==0 ){.    char
24d8d 20 2a 7a 43 6f 6c 6c 3b 0a 20 20 20 20 61 73 73   *zColl;.    ass
24d8e 65 72 74 28 20 70 4e 61 6d 65 31 2d 3e 7a 20 29  ert( pName1->z )
24d8f 3b 0a 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 73 71  ;.    zColl = sq
24d90 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
24d91 65 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  en(pParse->db, p
24d92 4e 61 6d 65 31 29 3b 0a 20 20 20 20 69 66 28 20  Name1);.    if( 
24d93 21 7a 43 6f 6c 6c 20 29 20 72 65 74 75 72 6e 3b  !zColl ) return;
24d94 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c  .    pColl = sql
24d95 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28  ite3FindCollSeq(
24d96 64 62 2c 20 45 4e 43 28 64 62 29 2c 20 7a 43 6f  db, ENC(db), zCo
24d97 6c 6c 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20  ll, 0);.    if( 
24d98 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 72  pColl ){.      r
24d99 65 69 6e 64 65 78 44 61 74 61 62 61 73 65 73 28  eindexDatabases(
24d9a 70 50 61 72 73 65 2c 20 7a 43 6f 6c 6c 29 3b 0a  pParse, zColl);.
24d9b 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
24d9c 72 65 65 28 64 62 2c 20 7a 43 6f 6c 6c 29 3b 0a  ree(db, zColl);.
24d9d 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
24d9e 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44    }.    sqlite3D
24d9f 62 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c 6c 29  bFree(db, zColl)
24da0 3b 0a 20 20 7d 0a 20 20 69 44 62 20 3d 20 73 71  ;.  }.  iDb = sq
24da1 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65  lite3TwoPartName
24da2 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c  (pParse, pName1,
24da3 20 70 4e 61 6d 65 32 2c 20 26 70 4f 62 6a 4e 61   pName2, &pObjNa
24da4 6d 65 29 3b 0a 20 20 69 66 28 20 69 44 62 3c 30  me);.  if( iDb<0
24da5 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7a 20 3d   ) return;.  z =
24da6 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d   sqlite3NameFrom
24da7 54 6f 6b 65 6e 28 64 62 2c 20 70 4f 62 6a 4e 61  Token(db, pObjNa
24da8 6d 65 29 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20  me);.  if( z==0 
24da9 29 20 72 65 74 75 72 6e 3b 0a 20 20 7a 44 62 20  ) return;.  zDb 
24daa 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  = db->aDb[iDb].z
24dab 4e 61 6d 65 3b 0a 20 20 70 54 61 62 20 3d 20 73  Name;.  pTab = s
24dac 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28  qlite3FindTable(
24dad 64 62 2c 20 7a 2c 20 7a 44 62 29 3b 0a 20 20 69  db, z, zDb);.  i
24dae 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20 72  f( pTab ){.    r
24daf 65 69 6e 64 65 78 54 61 62 6c 65 28 70 50 61 72  eindexTable(pPar
24db0 73 65 2c 20 70 54 61 62 2c 20 30 29 3b 0a 20 20  se, pTab, 0);.  
24db1 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
24db2 64 62 2c 20 7a 29 3b 0a 20 20 20 20 72 65 74 75  db, z);.    retu
24db3 72 6e 3b 0a 20 20 7d 0a 20 20 70 49 6e 64 65 78  rn;.  }.  pIndex
24db4 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e   = sqlite3FindIn
24db5 64 65 78 28 64 62 2c 20 7a 2c 20 7a 44 62 29 3b  dex(db, z, zDb);
24db6 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
24db7 28 64 62 2c 20 7a 29 3b 0a 20 20 69 66 28 20 70  (db, z);.  if( p
24db8 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 73 71 6c  Index ){.    sql
24db9 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70  ite3BeginWriteOp
24dba 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20  eration(pParse, 
24dbb 30 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c  0, iDb);.    sql
24dbc 69 74 65 33 52 65 66 69 6c 6c 49 6e 64 65 78 28  ite3RefillIndex(
24dbd 70 50 61 72 73 65 2c 20 70 49 6e 64 65 78 2c 20  pParse, pIndex, 
24dbe 2d 31 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  -1);.    return;
24dbf 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 72  .  }.  sqlite3Er
24dc0 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
24dc1 75 6e 61 62 6c 65 20 74 6f 20 69 64 65 6e 74 69  unable to identi
24dc2 66 79 20 74 68 65 20 6f 62 6a 65 63 74 20 74 6f  fy the object to
24dc3 20 62 65 20 72 65 69 6e 64 65 78 65 64 22 29 3b   be reindexed");
24dc4 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
24dc5 20 52 65 74 75 72 6e 20 61 20 64 79 6e 61 6d 69   Return a dynami
24dc6 63 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 4b 65  cly allocated Ke
24dc7 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  yInfo structure 
24dc8 74 68 61 74 20 63 61 6e 20 62 65 20 75 73 65 64  that can be used
24dc9 0a 2a 2a 20 77 69 74 68 20 4f 50 5f 4f 70 65 6e  .** with OP_Open
24dca 52 65 61 64 20 6f 72 20 4f 50 5f 4f 70 65 6e 57  Read or OP_OpenW
24dcb 72 69 74 65 20 74 6f 20 61 63 63 65 73 73 20 64  rite to access d
24dcc 61 74 61 62 61 73 65 20 69 6e 64 65 78 20 70 49  atabase index pI
24dcd 64 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63  dx..**.** If suc
24dce 63 65 73 73 66 75 6c 2c 20 61 20 70 6f 69 6e 74  cessful, a point
24dcf 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20 73 74  er to the new st
24dd0 72 75 63 74 75 72 65 20 69 73 20 72 65 74 75 72  ructure is retur
24dd1 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ned. In this cas
24dd2 65 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 20  e.** the caller 
24dd3 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66  is responsible f
24dd4 6f 72 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74  or calling sqlit
24dd5 65 33 44 62 46 72 65 65 28 64 62 2c 20 29 20 6f  e3DbFree(db, ) o
24dd6 6e 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 0a  n the returned .
24dd7 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 49 66 20 61  ** pointer. If a
24dd8 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 28  n error occurs (
24dd9 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 20 6f 72  out of memory or
24dda 20 6d 69 73 73 69 6e 67 20 63 6f 6c 6c 61 74 69   missing collati
24ddb 6f 6e 20 0a 2a 2a 20 73 65 71 75 65 6e 63 65 29  on .** sequence)
24ddc 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e  , NULL is return
24ddd 65 64 20 61 6e 64 20 74 68 65 20 73 74 61 74 65  ed and the state
24dde 20 6f 66 20 70 50 61 72 73 65 20 75 70 64 61 74   of pParse updat
24ddf 65 64 20 74 6f 20 72 65 66 6c 65 63 74 0a 2a 2a  ed to reflect.**
24de0 20 74 68 65 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53   the error..*/.S
24de1 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 4b 65  QLITE_PRIVATE Ke
24de2 79 49 6e 66 6f 20 2a 73 71 6c 69 74 65 33 49 6e  yInfo *sqlite3In
24de3 64 65 78 4b 65 79 69 6e 66 6f 28 50 61 72 73 65  dexKeyinfo(Parse
24de4 20 2a 70 50 61 72 73 65 2c 20 49 6e 64 65 78 20   *pParse, Index 
24de5 2a 70 49 64 78 29 7b 0a 20 20 69 6e 74 20 69 3b  *pIdx){.  int i;
24de6 0a 20 20 69 6e 74 20 6e 43 6f 6c 20 3d 20 70 49  .  int nCol = pI
24de7 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 69  dx->nColumn;.  i
24de8 6e 74 20 6e 42 79 74 65 73 20 3d 20 73 69 7a 65  nt nBytes = size
24de9 6f 66 28 4b 65 79 49 6e 66 6f 29 20 2b 20 28 6e  of(KeyInfo) + (n
24dea 43 6f 6c 2d 31 29 2a 73 69 7a 65 6f 66 28 43 6f  Col-1)*sizeof(Co
24deb 6c 6c 53 65 71 2a 29 20 2b 20 6e 43 6f 6c 3b 0a  llSeq*) + nCol;.
24dec 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
24ded 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 4b 65  pParse->db;.  Ke
24dee 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d 20 28 4b  yInfo *pKey = (K
24def 65 79 49 6e 66 6f 20 2a 29 73 71 6c 69 74 65 33  eyInfo *)sqlite3
24df0 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
24df1 20 6e 42 79 74 65 73 29 3b 0a 0a 20 20 69 66 28   nBytes);..  if(
24df2 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 70 4b 65   pKey ){.    pKe
24df3 79 2d 3e 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  y->db = pParse->
24df4 64 62 3b 0a 20 20 20 20 70 4b 65 79 2d 3e 61 53  db;.    pKey->aS
24df5 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38 20 2a  ortOrder = (u8 *
24df6 29 26 28 70 4b 65 79 2d 3e 61 43 6f 6c 6c 5b 6e  )&(pKey->aColl[n
24df7 43 6f 6c 5d 29 3b 0a 20 20 20 20 61 73 73 65 72  Col]);.    asser
24df8 74 28 20 26 70 4b 65 79 2d 3e 61 53 6f 72 74 4f  t( &pKey->aSortO
24df9 72 64 65 72 5b 6e 43 6f 6c 5d 3d 3d 26 28 28 28  rder[nCol]==&(((
24dfa 75 38 20 2a 29 70 4b 65 79 29 5b 6e 42 79 74 65  u8 *)pKey)[nByte
24dfb 73 5d 29 20 29 3b 0a 20 20 20 20 66 6f 72 28 69  s]) );.    for(i
24dfc 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29  =0; i<nCol; i++)
24dfd 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 43  {.      char *zC
24dfe 6f 6c 6c 20 3d 20 70 49 64 78 2d 3e 61 7a 43 6f  oll = pIdx->azCo
24dff 6c 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 61 73 73  ll[i];.      ass
24e00 65 72 74 28 20 7a 43 6f 6c 6c 20 29 3b 0a 20 20  ert( zColl );.  
24e01 20 20 20 20 70 4b 65 79 2d 3e 61 43 6f 6c 6c 5b      pKey->aColl[
24e02 69 5d 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61  i] = sqlite3Loca
24e03 74 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65  teCollSeq(pParse
24e04 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 20 20  , zColl);.      
24e05 70 4b 65 79 2d 3e 61 53 6f 72 74 4f 72 64 65 72  pKey->aSortOrder
24e06 5b 69 5d 20 3d 20 70 49 64 78 2d 3e 61 53 6f 72  [i] = pIdx->aSor
24e07 74 4f 72 64 65 72 5b 69 5d 3b 0a 20 20 20 20 7d  tOrder[i];.    }
24e08 0a 20 20 20 20 70 4b 65 79 2d 3e 6e 46 69 65 6c  .    pKey->nFiel
24e09 64 20 3d 20 28 75 31 36 29 6e 43 6f 6c 3b 0a 20  d = (u16)nCol;. 
24e0a 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65   }..  if( pParse
24e0b 2d 3e 6e 45 72 72 20 29 7b 0a 20 20 20 20 73 71  ->nErr ){.    sq
24e0c 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
24e0d 70 4b 65 79 29 3b 0a 20 20 20 20 70 4b 65 79 20  pKey);.    pKey 
24e0e 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  = 0;.  }.  retur
24e0f 6e 20 70 4b 65 79 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  n pKey;.}../****
24e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
24e11 66 20 62 75 69 6c 64 2e 63 20 2a 2a 2a 2a 2a 2a  f build.c ******
24e12 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24e13 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24e14 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
24e15 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
24e16 20 66 69 6c 65 20 63 61 6c 6c 62 61 63 6b 2e 63   file callback.c
24e17 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
24e18 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24e19 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
24e1a 20 32 30 30 35 20 4d 61 79 20 32 33 20 0a 2a 2a   2005 May 23 .**
24e1b 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
24e1c 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
24e1d 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
24e1e 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
24e1f 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
24e20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
24e21 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
24e22 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
24e23 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
24e24 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
24e25 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
24e26 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
24e27 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
24e28 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
24e29 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
24e2a 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
24e2b 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
24e2c 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
24e2d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24e2e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24e2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
24e31 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
24e32 74 61 69 6e 73 20 66 75 6e 63 74 69 6f 6e 73 20  tains functions 
24e33 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 74  used to access t
24e34 68 65 20 69 6e 74 65 72 6e 61 6c 20 68 61 73 68  he internal hash
24e35 20 74 61 62 6c 65 73 0a 2a 2a 20 6f 66 20 75 73   tables.** of us
24e36 65 72 20 64 65 66 69 6e 65 64 20 66 75 6e 63 74  er defined funct
24e37 69 6f 6e 73 20 61 6e 64 20 63 6f 6c 6c 61 74 69  ions and collati
24e38 6f 6e 20 73 65 71 75 65 6e 63 65 73 2e 0a 2a 2a  on sequences..**
24e39 0a 2a 2a 20 24 49 64 3a 20 63 61 6c 6c 62 61 63  .** $Id: callbac
24e3a 6b 2e 63 2c 76 20 31 2e 34 32 20 32 30 30 39 2f  k.c,v 1.42 2009/
24e3b 30 36 2f 31 37 20 30 30 3a 33 35 3a 33 31 20 64  06/17 00:35:31 d
24e3c 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a  rh Exp $.*/.../*
24e3d 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 27  .** Invoke the '
24e3e 63 6f 6c 6c 61 74 69 6f 6e 20 6e 65 65 64 65 64  collation needed
24e3f 27 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 72 65  ' callback to re
24e40 71 75 65 73 74 20 61 20 63 6f 6c 6c 61 74 69 6f  quest a collatio
24e41 6e 20 73 65 71 75 65 6e 63 65 0a 2a 2a 20 69 6e  n sequence.** in
24e42 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 65 6e   the encoding en
24e43 63 20 6f 66 20 6e 61 6d 65 20 7a 4e 61 6d 65 2c  c of name zName,
24e44 20 6c 65 6e 67 74 68 20 6e 4e 61 6d 65 2e 0a 2a   length nName..*
24e45 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 61  /.static void ca
24e46 6c 6c 43 6f 6c 6c 4e 65 65 64 65 64 28 73 71 6c  llCollNeeded(sql
24e47 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 65 6e  ite3 *db, int en
24e48 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  c, const char *z
24e49 4e 61 6d 65 29 7b 0a 20 20 61 73 73 65 72 74 28  Name){.  assert(
24e4a 20 21 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65   !db->xCollNeede
24e4b 64 20 7c 7c 20 21 64 62 2d 3e 78 43 6f 6c 6c 4e  d || !db->xCollN
24e4c 65 65 64 65 64 31 36 20 29 3b 0a 20 20 69 66 28  eeded16 );.  if(
24e4d 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64   db->xCollNeeded
24e4e 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 45   ){.    char *zE
24e4f 78 74 65 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65  xternal = sqlite
24e50 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 7a 4e  3DbStrDup(db, zN
24e51 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 21 7a  ame);.    if( !z
24e52 45 78 74 65 72 6e 61 6c 20 29 20 72 65 74 75 72  External ) retur
24e53 6e 3b 0a 20 20 20 20 64 62 2d 3e 78 43 6f 6c 6c  n;.    db->xColl
24e54 4e 65 65 64 65 64 28 64 62 2d 3e 70 43 6f 6c 6c  Needed(db->pColl
24e55 4e 65 65 64 65 64 41 72 67 2c 20 64 62 2c 20 65  NeededArg, db, e
24e56 6e 63 2c 20 7a 45 78 74 65 72 6e 61 6c 29 3b 0a  nc, zExternal);.
24e57 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
24e58 65 28 64 62 2c 20 7a 45 78 74 65 72 6e 61 6c 29  e(db, zExternal)
24e59 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51  ;.  }.#ifndef SQ
24e5a 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a  LITE_OMIT_UTF16.
24e5b 20 20 69 66 28 20 64 62 2d 3e 78 43 6f 6c 6c 4e    if( db->xCollN
24e5c 65 65 64 65 64 31 36 20 29 7b 0a 20 20 20 20 63  eeded16 ){.    c
24e5d 68 61 72 20 63 6f 6e 73 74 20 2a 7a 45 78 74 65  har const *zExte
24e5e 72 6e 61 6c 3b 0a 20 20 20 20 73 71 6c 69 74 65  rnal;.    sqlite
24e5f 33 5f 76 61 6c 75 65 20 2a 70 54 6d 70 20 3d 20  3_value *pTmp = 
24e60 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28  sqlite3ValueNew(
24e61 64 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  db);.    sqlite3
24e62 56 61 6c 75 65 53 65 74 53 74 72 28 70 54 6d 70  ValueSetStr(pTmp
24e63 2c 20 2d 31 2c 20 7a 4e 61 6d 65 2c 20 53 51 4c  , -1, zName, SQL
24e64 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45  ITE_UTF8, SQLITE
24e65 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 7a 45  _STATIC);.    zE
24e66 78 74 65 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65  xternal = sqlite
24e67 33 56 61 6c 75 65 54 65 78 74 28 70 54 6d 70 2c  3ValueText(pTmp,
24e68 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54   SQLITE_UTF16NAT
24e69 49 56 45 29 3b 0a 20 20 20 20 69 66 28 20 7a 45  IVE);.    if( zE
24e6a 78 74 65 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20  xternal ){.     
24e6b 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64   db->xCollNeeded
24e6c 31 36 28 64 62 2d 3e 70 43 6f 6c 6c 4e 65 65 64  16(db->pCollNeed
24e6d 65 64 41 72 67 2c 20 64 62 2c 20 28 69 6e 74 29  edArg, db, (int)
24e6e 45 4e 43 28 64 62 29 2c 20 7a 45 78 74 65 72 6e  ENC(db), zExtern
24e6f 61 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  al);.    }.    s
24e70 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28  qlite3ValueFree(
24e71 70 54 6d 70 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  pTmp);.  }.#endi
24e72 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  f.}../*.** This 
24e73 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
24e74 64 20 69 66 20 74 68 65 20 63 6f 6c 6c 61 74 69  d if the collati
24e75 6f 6e 20 66 61 63 74 6f 72 79 20 66 61 69 6c 73  on factory fails
24e76 20 74 6f 20 64 65 6c 69 76 65 72 20 61 0a 2a 2a   to deliver a.**
24e77 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74   collation funct
24e78 69 6f 6e 20 69 6e 20 74 68 65 20 62 65 73 74 20  ion in the best 
24e79 65 6e 63 6f 64 69 6e 67 20 62 75 74 20 74 68 65  encoding but the
24e7a 72 65 20 6d 61 79 20 62 65 20 6f 74 68 65 72 20  re may be other 
24e7b 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 6f 66 20 74  versions.** of t
24e7c 68 69 73 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 75  his collation fu
24e7d 6e 63 74 69 6f 6e 20 28 66 6f 72 20 6f 74 68 65  nction (for othe
24e7e 72 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 73  r text encodings
24e7f 29 20 61 76 61 69 6c 61 62 6c 65 2e 20 55 73 65  ) available. Use
24e80 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 73 65   one.** of these
24e81 20 69 6e 73 74 65 61 64 20 69 66 20 74 68 65 79   instead if they
24e82 20 65 78 69 73 74 2e 20 41 76 6f 69 64 20 61 20   exist. Avoid a 
24e83 55 54 46 2d 38 20 3c 2d 3e 20 55 54 46 2d 31 36  UTF-8 <-> UTF-16
24e84 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 66 0a 2a   conversion if.*
24e85 2a 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2f 0a 73  * possible..*/.s
24e86 74 61 74 69 63 20 69 6e 74 20 73 79 6e 74 68 43  tatic int synthC
24e87 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 20 2a  ollSeq(sqlite3 *
24e88 64 62 2c 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  db, CollSeq *pCo
24e89 6c 6c 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a  ll){.  CollSeq *
24e8a 70 43 6f 6c 6c 32 3b 0a 20 20 63 68 61 72 20 2a  pColl2;.  char *
24e8b 7a 20 3d 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65  z = pColl->zName
24e8c 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 61  ;.  int i;.  sta
24e8d 74 69 63 20 63 6f 6e 73 74 20 75 38 20 61 45 6e  tic const u8 aEn
24e8e 63 5b 5d 20 3d 20 7b 20 53 51 4c 49 54 45 5f 55  c[] = { SQLITE_U
24e8f 54 46 31 36 42 45 2c 20 53 51 4c 49 54 45 5f 55  TF16BE, SQLITE_U
24e90 54 46 31 36 4c 45 2c 20 53 51 4c 49 54 45 5f 55  TF16LE, SQLITE_U
24e91 54 46 38 20 7d 3b 0a 20 20 66 6f 72 28 69 3d 30  TF8 };.  for(i=0
24e92 3b 20 69 3c 33 3b 20 69 2b 2b 29 7b 0a 20 20 20  ; i<3; i++){.   
24e93 20 70 43 6f 6c 6c 32 20 3d 20 73 71 6c 69 74 65   pColl2 = sqlite
24e94 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c  3FindCollSeq(db,
24e95 20 61 45 6e 63 5b 69 5d 2c 20 7a 2c 20 30 29 3b   aEnc[i], z, 0);
24e96 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 32 2d  .    if( pColl2-
24e97 3e 78 43 6d 70 21 3d 30 20 29 7b 0a 20 20 20 20  >xCmp!=0 ){.    
24e98 20 20 6d 65 6d 63 70 79 28 70 43 6f 6c 6c 2c 20    memcpy(pColl, 
24e99 70 43 6f 6c 6c 32 2c 20 73 69 7a 65 6f 66 28 43  pColl2, sizeof(C
24e9a 6f 6c 6c 53 65 71 29 29 3b 0a 20 20 20 20 20 20  ollSeq));.      
24e9b 70 43 6f 6c 6c 2d 3e 78 44 65 6c 20 3d 20 30 3b  pColl->xDel = 0;
24e9c 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e           /* Do n
24e9d 6f 74 20 63 6f 70 79 20 74 68 65 20 64 65 73 74  ot copy the dest
24e9e 72 75 63 74 6f 72 20 2a 2f 0a 20 20 20 20 20 20  ructor */.      
24e9f 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
24ea0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
24ea1 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
24ea2 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  R;.}../*.** This
24ea3 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 73   function is res
24ea4 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 69 6e 76  ponsible for inv
24ea5 6f 6b 69 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74  oking the collat
24ea6 69 6f 6e 20 66 61 63 74 6f 72 79 20 63 61 6c 6c  ion factory call
24ea7 62 61 63 6b 0a 2a 2a 20 6f 72 20 73 75 62 73 74  back.** or subst
24ea8 69 74 75 74 69 6e 67 20 61 20 63 6f 6c 6c 61 74  ituting a collat
24ea9 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6f 66 20  ion sequence of 
24eaa 61 20 64 69 66 66 65 72 65 6e 74 20 65 6e 63 6f  a different enco
24eab 64 69 6e 67 20 77 68 65 6e 20 74 68 65 0a 2a 2a  ding when the.**
24eac 20 72 65 71 75 65 73 74 65 64 20 63 6f 6c 6c 61   requested colla
24ead 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 69 73  tion sequence is
24eae 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 69   not available i
24eaf 6e 20 74 68 65 20 64 65 73 69 72 65 64 20 65 6e  n the desired en
24eb0 63 6f 64 69 6e 67 2e 0a 2a 2a 20 0a 2a 2a 20 49  coding..** .** I
24eb1 66 20 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  f it is not NULL
24eb2 2c 20 74 68 65 6e 20 70 43 6f 6c 6c 20 6d 75 73  , then pColl mus
24eb3 74 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 64  t point to the d
24eb4 61 74 61 62 61 73 65 20 6e 61 74 69 76 65 20 65  atabase native e
24eb5 6e 63 6f 64 69 6e 67 20 0a 2a 2a 20 63 6f 6c 6c  ncoding .** coll
24eb6 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77  ation sequence w
24eb7 69 74 68 20 6e 61 6d 65 20 7a 4e 61 6d 65 2c 20  ith name zName, 
24eb8 6c 65 6e 67 74 68 20 6e 4e 61 6d 65 2e 0a 2a 2a  length nName..**
24eb9 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76  .** The return v
24eba 61 6c 75 65 20 69 73 20 65 69 74 68 65 72 20 74  alue is either t
24ebb 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  he collation seq
24ebc 75 65 6e 63 65 20 74 6f 20 62 65 20 75 73 65 64  uence to be used
24ebd 20 69 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20   in database.** 
24ebe 64 62 20 66 6f 72 20 63 6f 6c 6c 61 74 69 6f 6e  db for collation
24ebf 20 74 79 70 65 20 6e 61 6d 65 20 7a 4e 61 6d 65   type name zName
24ec0 2c 20 6c 65 6e 67 74 68 20 6e 4e 61 6d 65 2c 20  , length nName, 
24ec1 6f 72 20 4e 55 4c 4c 2c 20 69 66 20 6e 6f 20 63  or NULL, if no c
24ec2 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65 71 75  ollation.** sequ
24ec3 65 6e 63 65 20 63 61 6e 20 62 65 20 66 6f 75 6e  ence can be foun
24ec4 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  d..**.** See als
24ec5 6f 3a 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65  o: sqlite3Locate
24ec6 43 6f 6c 6c 53 65 71 28 29 2c 20 73 71 6c 69 74  CollSeq(), sqlit
24ec7 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 29 0a  e3FindCollSeq().
24ec8 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
24ec9 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74  E CollSeq *sqlit
24eca 65 33 47 65 74 43 6f 6c 6c 53 65 71 28 0a 20 20  e3GetCollSeq(.  
24ecb 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 20 20 20  sqlite3* db,    
24ecc 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
24ecd 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
24ece 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 20   */.  u8 enc,   
24ecf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
24ed0 68 65 20 64 65 73 69 72 65 64 20 65 6e 63 6f 64  he desired encod
24ed1 69 6e 67 20 66 6f 72 20 74 68 65 20 63 6f 6c 6c  ing for the coll
24ed2 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a  ating sequence *
24ed3 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  /.  CollSeq *pCo
24ed4 6c 6c 2c 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c  ll,       /* Col
24ed5 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
24ed6 77 69 74 68 20 6e 61 74 69 76 65 20 65 6e 63 6f  with native enco
24ed7 64 69 6e 67 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f  ding, or NULL */
24ed8 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
24ed9 4e 61 6d 65 20 20 20 20 20 2f 2a 20 43 6f 6c 6c  Name     /* Coll
24eda 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6e  ating sequence n
24edb 61 6d 65 20 2a 2f 0a 29 7b 0a 20 20 43 6f 6c 6c  ame */.){.  Coll
24edc 53 65 71 20 2a 70 3b 0a 0a 20 20 70 20 3d 20 70  Seq *p;..  p = p
24edd 43 6f 6c 6c 3b 0a 20 20 69 66 28 20 21 70 20 29  Coll;.  if( !p )
24ede 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65  {.    p = sqlite
24edf 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c  3FindCollSeq(db,
24ee0 20 65 6e 63 2c 20 7a 4e 61 6d 65 2c 20 30 29 3b   enc, zName, 0);
24ee1 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 20 7c 7c  .  }.  if( !p ||
24ee2 20 21 70 2d 3e 78 43 6d 70 20 29 7b 0a 20 20 20   !p->xCmp ){.   
24ee3 20 2f 2a 20 4e 6f 20 63 6f 6c 6c 61 74 69 6f 6e   /* No collation
24ee4 20 73 65 71 75 65 6e 63 65 20 6f 66 20 74 68 69   sequence of thi
24ee5 73 20 74 79 70 65 20 66 6f 72 20 74 68 69 73 20  s type for this 
24ee6 65 6e 63 6f 64 69 6e 67 20 69 73 20 72 65 67 69  encoding is regi
24ee7 73 74 65 72 65 64 2e 0a 20 20 20 20 2a 2a 20 43  stered..    ** C
24ee8 61 6c 6c 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f  all the collatio
24ee9 6e 20 66 61 63 74 6f 72 79 20 74 6f 20 73 65 65  n factory to see
24eea 20 69 66 20 69 74 20 63 61 6e 20 73 75 70 70 6c   if it can suppl
24eeb 79 20 75 73 20 77 69 74 68 20 6f 6e 65 2e 0a 20  y us with one.. 
24eec 20 20 20 2a 2f 0a 20 20 20 20 63 61 6c 6c 43 6f     */.    callCo
24eed 6c 6c 4e 65 65 64 65 64 28 64 62 2c 20 65 6e 63  llNeeded(db, enc
24eee 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 20  , zName);.    p 
24eef 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c  = sqlite3FindCol
24ef0 6c 53 65 71 28 64 62 2c 20 65 6e 63 2c 20 7a 4e  lSeq(db, enc, zN
24ef1 61 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69  ame, 0);.  }.  i
24ef2 66 28 20 70 20 26 26 20 21 70 2d 3e 78 43 6d 70  f( p && !p->xCmp
24ef3 20 26 26 20 73 79 6e 74 68 43 6f 6c 6c 53 65 71   && synthCollSeq
24ef4 28 64 62 2c 20 70 29 20 29 7b 0a 20 20 20 20 70  (db, p) ){.    p
24ef5 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65   = 0;.  }.  asse
24ef6 72 74 28 20 21 70 20 7c 7c 20 70 2d 3e 78 43 6d  rt( !p || p->xCm
24ef7 70 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b  p );.  return p;
24ef8 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
24ef9 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
24efa 20 6f 6e 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20   on a collation 
24efb 73 65 71 75 65 6e 63 65 20 62 65 66 6f 72 65 20  sequence before 
24efc 69 74 20 69 73 20 75 73 65 64 20 74 6f 0a 2a 2a  it is used to.**
24efd 20 63 68 65 63 6b 20 74 68 61 74 20 69 74 20 69   check that it i
24efe 73 20 64 65 66 69 6e 65 64 2e 20 41 6e 20 75 6e  s defined. An un
24eff 64 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6f  defined collatio
24f00 6e 20 73 65 71 75 65 6e 63 65 20 65 78 69 73 74  n sequence exist
24f01 73 20 77 68 65 6e 0a 2a 2a 20 61 20 64 61 74 61  s when.** a data
24f02 62 61 73 65 20 69 73 20 6c 6f 61 64 65 64 20 74  base is loaded t
24f03 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 72 65 66  hat contains ref
24f04 65 72 65 6e 63 65 73 20 74 6f 20 63 6f 6c 6c 61  erences to colla
24f05 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 0a 2a  tion sequences.*
24f06 2a 20 74 68 61 74 20 68 61 76 65 20 6e 6f 74 20  * that have not 
24f07 62 65 65 6e 20 64 65 66 69 6e 65 64 20 62 79 20  been defined by 
24f08 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63  sqlite3_create_c
24f09 6f 6c 6c 61 74 69 6f 6e 28 29 20 65 74 63 2e 0a  ollation() etc..
24f0a 2a 2a 0a 2a 2a 20 49 66 20 72 65 71 75 69 72 65  **.** If require
24f0b 64 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  d, this routine 
24f0c 63 61 6c 6c 73 20 74 68 65 20 27 63 6f 6c 6c 61  calls the 'colla
24f0d 74 69 6f 6e 20 6e 65 65 64 65 64 27 20 63 61 6c  tion needed' cal
24f0e 6c 62 61 63 6b 20 74 6f 0a 2a 2a 20 72 65 71 75  lback to.** requ
24f0f 65 73 74 20 61 20 64 65 66 69 6e 69 74 69 6f 6e  est a definition
24f10 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e   of the collatin
24f11 67 20 73 65 71 75 65 6e 63 65 2e 20 49 66 20 74  g sequence. If t
24f12 68 69 73 20 64 6f 65 73 6e 27 74 20 77 6f 72 6b  his doesn't work
24f13 2c 20 0a 2a 2a 20 61 6e 20 65 71 75 69 76 61 6c  , .** an equival
24f14 65 6e 74 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  ent collating se
24f15 71 75 65 6e 63 65 20 74 68 61 74 20 75 73 65 73  quence that uses
24f16 20 61 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67   a text encoding
24f17 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66 72   different.** fr
24f18 6f 6d 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  om the main data
24f19 62 61 73 65 20 69 73 20 73 75 62 73 74 69 74 75  base is substitu
24f1a 74 65 64 2c 20 69 66 20 6f 6e 65 20 69 73 20 61  ted, if one is a
24f1b 76 61 69 6c 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c  vailable..*/.SQL
24f1c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
24f1d 73 71 6c 69 74 65 33 43 68 65 63 6b 43 6f 6c 6c  sqlite3CheckColl
24f1e 53 65 71 28 50 61 72 73 65 20 2a 70 50 61 72 73  Seq(Parse *pPars
24f1f 65 2c 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c  e, CollSeq *pCol
24f20 6c 29 7b 0a 20 20 69 66 28 20 70 43 6f 6c 6c 20  l){.  if( pColl 
24f21 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  ){.    const cha
24f22 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 43 6f 6c 6c  r *zName = pColl
24f23 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 73 71 6c  ->zName;.    sql
24f24 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
24f25 65 2d 3e 64 62 3b 0a 20 20 20 20 43 6f 6c 6c 53  e->db;.    CollS
24f26 65 71 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 47  eq *p = sqlite3G
24f27 65 74 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e  etCollSeq(db, EN
24f28 43 28 64 62 29 2c 20 70 43 6f 6c 6c 2c 20 7a 4e  C(db), pColl, zN
24f29 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 21 70  ame);.    if( !p
24f2a 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
24f2b 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
24f2c 2c 20 22 6e 6f 20 73 75 63 68 20 63 6f 6c 6c 61  , "no such colla
24f2d 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 3a 20 25  tion sequence: %
24f2e 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  s", zName);.    
24f2f 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b    pParse->nErr++
24f30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
24f31 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
24f32 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70   }.    assert( p
24f33 3d 3d 70 43 6f 6c 6c 20 29 3b 0a 20 20 7d 0a 20  ==pColl );.  }. 
24f34 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
24f35 4b 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4c 6f  K;.}..../*.** Lo
24f36 63 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20  cate and return 
24f37 61 6e 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68  an entry from th
24f38 65 20 64 62 2e 61 43 6f 6c 6c 53 65 71 20 68 61  e db.aCollSeq ha
24f39 73 68 20 74 61 62 6c 65 2e 20 49 66 20 74 68 65  sh table. If the
24f3a 20 65 6e 74 72 79 0a 2a 2a 20 73 70 65 63 69 66   entry.** specif
24f3b 69 65 64 20 62 79 20 7a 4e 61 6d 65 20 61 6e 64  ied by zName and
24f3c 20 6e 4e 61 6d 65 20 69 73 20 6e 6f 74 20 66 6f   nName is not fo
24f3d 75 6e 64 20 61 6e 64 20 70 61 72 61 6d 65 74 65  und and paramete
24f3e 72 20 27 63 72 65 61 74 65 27 20 69 73 0a 2a 2a  r 'create' is.**
24f3f 20 74 72 75 65 2c 20 74 68 65 6e 20 63 72 65 61   true, then crea
24f40 74 65 20 61 20 6e 65 77 20 65 6e 74 72 79 2e 20  te a new entry. 
24f41 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 72 6e  Otherwise return
24f42 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 45 61 63   NULL..**.** Eac
24f43 68 20 70 6f 69 6e 74 65 72 20 73 74 6f 72 65 64  h pointer stored
24f44 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 2e   in the sqlite3.
24f45 61 43 6f 6c 6c 53 65 71 20 68 61 73 68 20 74 61  aCollSeq hash ta
24f46 62 6c 65 20 63 6f 6e 74 61 69 6e 73 20 61 6e 0a  ble contains an.
24f47 2a 2a 20 61 72 72 61 79 20 6f 66 20 74 68 72 65  ** array of thre
24f48 65 20 43 6f 6c 6c 53 65 71 20 73 74 72 75 63 74  e CollSeq struct
24f49 75 72 65 73 2e 20 54 68 65 20 66 69 72 73 74 20  ures. The first 
24f4a 69 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  is the collation
24f4b 20 73 65 71 75 65 6e 63 65 0a 2a 2a 20 70 72 65   sequence.** pre
24f4c 66 66 65 72 72 65 64 20 66 6f 72 20 55 54 46 2d  fferred for UTF-
24f4d 38 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 55 54  8, the second UT
24f4e 46 2d 31 36 6c 65 2c 20 61 6e 64 20 74 68 65 20  F-16le, and the 
24f4f 74 68 69 72 64 20 55 54 46 2d 31 36 62 65 2e 0a  third UTF-16be..
24f50 2a 2a 0a 2a 2a 20 53 74 6f 72 65 64 20 69 6d 6d  **.** Stored imm
24f51 65 64 69 61 74 65 6c 79 20 61 66 74 65 72 20 74  ediately after t
24f52 68 65 20 74 68 72 65 65 20 63 6f 6c 6c 61 74 69  he three collati
24f53 6f 6e 20 73 65 71 75 65 6e 63 65 73 20 69 73 20  on sequences is 
24f54 61 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65  a copy of.** the
24f55 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
24f56 6e 63 65 20 6e 61 6d 65 2e 20 41 20 70 6f 69 6e  nce name. A poin
24f57 74 65 72 20 74 6f 20 74 68 69 73 20 73 74 72 69  ter to this stri
24f58 6e 67 20 69 73 20 73 74 6f 72 65 64 20 69 6e 0a  ng is stored in.
24f59 2a 2a 20 65 61 63 68 20 63 6f 6c 6c 61 74 69 6f  ** each collatio
24f5a 6e 20 73 65 71 75 65 6e 63 65 20 73 74 72 75 63  n sequence struc
24f5b 74 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ture..*/.static 
24f5c 43 6f 6c 6c 53 65 71 20 2a 66 69 6e 64 43 6f 6c  CollSeq *findCol
24f5d 6c 53 65 71 45 6e 74 72 79 28 0a 20 20 73 71 6c  lSeqEntry(.  sql
24f5e 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
24f5f 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63     /* Database c
24f60 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63  onnection */.  c
24f61 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
24f62 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20  ,    /* Name of 
24f63 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  the collating se
24f64 71 75 65 6e 63 65 20 2a 2f 0a 20 20 69 6e 74 20  quence */.  int 
24f65 63 72 65 61 74 65 20 20 20 20 20 20 20 20 20 20  create          
24f66 20 20 2f 2a 20 43 72 65 61 74 65 20 61 20 6e 65    /* Create a ne
24f67 77 20 65 6e 74 72 79 20 69 66 20 74 72 75 65 20  w entry if true 
24f68 2a 2f 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20  */.){.  CollSeq 
24f69 2a 70 43 6f 6c 6c 3b 0a 20 20 69 6e 74 20 6e 4e  *pColl;.  int nN
24f6a 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72  ame = sqlite3Str
24f6b 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 20  len30(zName);.  
24f6c 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 48  pColl = sqlite3H
24f6d 61 73 68 46 69 6e 64 28 26 64 62 2d 3e 61 43 6f  ashFind(&db->aCo
24f6e 6c 6c 53 65 71 2c 20 7a 4e 61 6d 65 2c 20 6e 4e  llSeq, zName, nN
24f6f 61 6d 65 29 3b 0a 0a 20 20 69 66 28 20 30 3d 3d  ame);..  if( 0==
24f70 70 43 6f 6c 6c 20 26 26 20 63 72 65 61 74 65 20  pColl && create 
24f71 29 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73  ){.    pColl = s
24f72 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
24f73 72 6f 28 64 62 2c 20 33 2a 73 69 7a 65 6f 66 28  ro(db, 3*sizeof(
24f74 2a 70 43 6f 6c 6c 29 20 2b 20 6e 4e 61 6d 65 20  *pColl) + nName 
24f75 2b 20 31 20 29 3b 0a 20 20 20 20 69 66 28 20 70  + 1 );.    if( p
24f76 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 43 6f  Coll ){.      Co
24f77 6c 6c 53 65 71 20 2a 70 44 65 6c 20 3d 20 30 3b  llSeq *pDel = 0;
24f78 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 30 5d 2e  .      pColl[0].
24f79 7a 4e 61 6d 65 20 3d 20 28 63 68 61 72 2a 29 26  zName = (char*)&
24f7a 70 43 6f 6c 6c 5b 33 5d 3b 0a 20 20 20 20 20 20  pColl[3];.      
24f7b 70 43 6f 6c 6c 5b 30 5d 2e 65 6e 63 20 3d 20 53  pColl[0].enc = S
24f7c 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 20  QLITE_UTF8;.    
24f7d 20 20 70 43 6f 6c 6c 5b 31 5d 2e 7a 4e 61 6d 65    pColl[1].zName
24f7e 20 3d 20 28 63 68 61 72 2a 29 26 70 43 6f 6c 6c   = (char*)&pColl
24f7f 5b 33 5d 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c  [3];.      pColl
24f80 5b 31 5d 2e 65 6e 63 20 3d 20 53 51 4c 49 54 45  [1].enc = SQLITE
24f81 5f 55 54 46 31 36 4c 45 3b 0a 20 20 20 20 20 20  _UTF16LE;.      
24f82 70 43 6f 6c 6c 5b 32 5d 2e 7a 4e 61 6d 65 20 3d  pColl[2].zName =
24f83 20 28 63 68 61 72 2a 29 26 70 43 6f 6c 6c 5b 33   (char*)&pColl[3
24f84 5d 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 32  ];.      pColl[2
24f85 5d 2e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55  ].enc = SQLITE_U
24f86 54 46 31 36 42 45 3b 0a 20 20 20 20 20 20 6d 65  TF16BE;.      me
24f87 6d 63 70 79 28 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e  mcpy(pColl[0].zN
24f88 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  ame, zName, nNam
24f89 65 29 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b  e);.      pColl[
24f8a 30 5d 2e 7a 4e 61 6d 65 5b 6e 4e 61 6d 65 5d 20  0].zName[nName] 
24f8b 3d 20 30 3b 0a 20 20 20 20 20 20 70 44 65 6c 20  = 0;.      pDel 
24f8c 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73  = sqlite3HashIns
24f8d 65 72 74 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65  ert(&db->aCollSe
24f8e 71 2c 20 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d  q, pColl[0].zNam
24f8f 65 2c 20 6e 4e 61 6d 65 2c 20 70 43 6f 6c 6c 29  e, nName, pColl)
24f90 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 61  ;..      /* If a
24f91 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72   malloc() failur
24f92 65 20 6f 63 63 75 72 72 65 64 20 69 6e 20 73 71  e occurred in sq
24f93 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28  lite3HashInsert(
24f94 29 2c 20 69 74 20 77 69 6c 6c 20 0a 20 20 20 20  ), it will .    
24f95 20 20 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20    ** return the 
24f96 70 43 6f 6c 6c 20 70 6f 69 6e 74 65 72 20 74 6f  pColl pointer to
24f97 20 62 65 20 64 65 6c 65 74 65 64 20 28 62 65 63   be deleted (bec
24f98 61 75 73 65 20 69 74 20 77 61 73 6e 27 74 20 61  ause it wasn't a
24f99 64 64 65 64 0a 20 20 20 20 20 20 2a 2a 20 74 6f  dded.      ** to
24f9a 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 29   the hash table)
24f9b 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
24f9c 20 61 73 73 65 72 74 28 20 70 44 65 6c 3d 3d 30   assert( pDel==0
24f9d 20 7c 7c 20 70 44 65 6c 3d 3d 70 43 6f 6c 6c 20   || pDel==pColl 
24f9e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 44 65  );.      if( pDe
24f9f 6c 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  l!=0 ){.        
24fa0 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
24fa1 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 73 71   = 1;.        sq
24fa2 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
24fa3 70 44 65 6c 29 3b 0a 20 20 20 20 20 20 20 20 70  pDel);.        p
24fa4 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20  Coll = 0;.      
24fa5 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  }.    }.  }.  re
24fa6 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f  turn pColl;.}../
24fa7 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 7a  *.** Parameter z
24fa8 4e 61 6d 65 20 70 6f 69 6e 74 73 20 74 6f 20 61  Name points to a
24fa9 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 73   UTF-8 encoded s
24faa 74 72 69 6e 67 20 6e 4e 61 6d 65 20 62 79 74 65  tring nName byte
24fab 73 20 6c 6f 6e 67 2e 0a 2a 2a 20 52 65 74 75 72  s long..** Retur
24fac 6e 20 74 68 65 20 43 6f 6c 6c 53 65 71 2a 20 70  n the CollSeq* p
24fad 6f 69 6e 74 65 72 20 66 6f 72 20 74 68 65 20 63  ointer for the c
24fae 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
24faf 65 20 6e 61 6d 65 64 20 7a 4e 61 6d 65 0a 2a 2a  e named zName.**
24fb0 20 66 6f 72 20 74 68 65 20 65 6e 63 6f 64 69 6e   for the encodin
24fb1 67 20 27 65 6e 63 27 20 66 72 6f 6d 20 74 68 65  g 'enc' from the
24fb2 20 64 61 74 61 62 61 73 65 20 27 64 62 27 2e 0a   database 'db'..
24fb3 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 6e 74  **.** If the ent
24fb4 72 79 20 73 70 65 63 69 66 69 65 64 20 69 73 20  ry specified is 
24fb5 6e 6f 74 20 66 6f 75 6e 64 20 61 6e 64 20 27 63  not found and 'c
24fb6 72 65 61 74 65 27 20 69 73 20 74 72 75 65 2c 20  reate' is true, 
24fb7 74 68 65 6e 20 63 72 65 61 74 65 20 61 0a 2a 2a  then create a.**
24fb8 20 6e 65 77 20 65 6e 74 72 79 2e 20 20 4f 74 68   new entry.  Oth
24fb9 65 72 77 69 73 65 20 72 65 74 75 72 6e 20 4e 55  erwise return NU
24fba 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 65 70 61  LL..**.** A sepa
24fbb 72 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 73 71  rate function sq
24fbc 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53  lite3LocateCollS
24fbd 65 71 28 29 20 69 73 20 61 20 77 72 61 70 70 65  eq() is a wrappe
24fbe 72 20 61 72 6f 75 6e 64 0a 2a 2a 20 74 68 69 73  r around.** this
24fbf 20 72 6f 75 74 69 6e 65 2e 20 20 73 71 6c 69 74   routine.  sqlit
24fc0 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28  e3LocateCollSeq(
24fc1 29 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 63 6f  ) invokes the co
24fc2 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79 0a  llation factory.
24fc3 2a 2a 20 69 66 20 6e 65 63 65 73 73 61 72 79 20  ** if necessary 
24fc4 61 6e 64 20 67 65 6e 65 72 61 74 65 73 20 61 6e  and generates an
24fc5 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69   error message i
24fc6 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20  f the collating 
24fc7 73 65 71 75 65 6e 63 65 0a 2a 2a 20 63 61 6e 6e  sequence.** cann
24fc8 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2a 0a  ot be found..**.
24fc9 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 73 71 6c  ** See also: sql
24fca 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65  ite3LocateCollSe
24fcb 71 28 29 2c 20 73 71 6c 69 74 65 33 47 65 74 43  q(), sqlite3GetC
24fcc 6f 6c 6c 53 65 71 28 29 0a 2a 2f 0a 53 51 4c 49  ollSeq().*/.SQLI
24fcd 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53  TE_PRIVATE CollS
24fce 65 71 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 43  eq *sqlite3FindC
24fcf 6f 6c 6c 53 65 71 28 0a 20 20 73 71 6c 69 74 65  ollSeq(.  sqlite
24fd0 33 20 2a 64 62 2c 0a 20 20 75 38 20 65 6e 63 2c  3 *db,.  u8 enc,
24fd1 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
24fd2 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 63 72 65 61  Name,.  int crea
24fd3 74 65 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20  te.){.  CollSeq 
24fd4 2a 70 43 6f 6c 6c 3b 0a 20 20 69 66 28 20 7a 4e  *pColl;.  if( zN
24fd5 61 6d 65 20 29 7b 0a 20 20 20 20 70 43 6f 6c 6c  ame ){.    pColl
24fd6 20 3d 20 66 69 6e 64 43 6f 6c 6c 53 65 71 45 6e   = findCollSeqEn
24fd7 74 72 79 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 63  try(db, zName, c
24fd8 72 65 61 74 65 29 3b 0a 20 20 7d 65 6c 73 65 7b  reate);.  }else{
24fd9 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 64 62 2d  .    pColl = db-
24fda 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20 7d 0a  >pDfltColl;.  }.
24fdb 20 20 61 73 73 65 72 74 28 20 53 51 4c 49 54 45    assert( SQLITE
24fdc 5f 55 54 46 38 3d 3d 31 20 26 26 20 53 51 4c 49  _UTF8==1 && SQLI
24fdd 54 45 5f 55 54 46 31 36 4c 45 3d 3d 32 20 26 26  TE_UTF16LE==2 &&
24fde 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3d   SQLITE_UTF16BE=
24fdf 3d 33 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =3 );.  assert( 
24fe0 65 6e 63 3e 3d 53 51 4c 49 54 45 5f 55 54 46 38  enc>=SQLITE_UTF8
24fe1 20 26 26 20 65 6e 63 3c 3d 53 51 4c 49 54 45 5f   && enc<=SQLITE_
24fe2 55 54 46 31 36 42 45 20 29 3b 0a 20 20 69 66 28  UTF16BE );.  if(
24fe3 20 70 43 6f 6c 6c 20 29 20 70 43 6f 6c 6c 20 2b   pColl ) pColl +
24fe4 3d 20 65 6e 63 2d 31 3b 0a 20 20 72 65 74 75 72  = enc-1;.  retur
24fe5 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 20 44  n pColl;.}../* D
24fe6 75 72 69 6e 67 20 74 68 65 20 73 65 61 72 63 68  uring the search
24fe7 20 66 6f 72 20 74 68 65 20 62 65 73 74 20 66 75   for the best fu
24fe8 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f  nction definitio
24fe9 6e 2c 20 74 68 69 73 20 70 72 6f 63 65 64 75 72  n, this procedur
24fea 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 74  e.** is called t
24feb 6f 20 74 65 73 74 20 68 6f 77 20 77 65 6c 6c 20  o test how well 
24fec 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 70 61 73  the function pas
24fed 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74  sed as the first
24fee 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 6d 61 74   argument.** mat
24fef 63 68 65 73 20 74 68 65 20 72 65 71 75 65 73 74  ches the request
24ff0 20 66 6f 72 20 61 20 66 75 6e 63 74 69 6f 6e 20   for a function 
24ff1 77 69 74 68 20 6e 41 72 67 20 61 72 67 75 6d 65  with nArg argume
24ff2 6e 74 73 20 69 6e 20 61 20 73 79 73 74 65 6d 0a  nts in a system.
24ff3 2a 2a 20 74 68 61 74 20 75 73 65 73 20 65 6e 63  ** that uses enc
24ff4 6f 64 69 6e 67 20 65 6e 63 2e 20 54 68 65 20 76  oding enc. The v
24ff5 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 6e  alue returned in
24ff6 64 69 63 61 74 65 73 20 68 6f 77 20 77 65 6c 6c  dicates how well
24ff7 20 74 68 65 0a 2a 2a 20 72 65 71 75 65 73 74 20   the.** request 
24ff8 69 73 20 6d 61 74 63 68 65 64 2e 20 41 20 68 69  is matched. A hi
24ff9 67 68 65 72 20 76 61 6c 75 65 20 69 6e 64 69 63  gher value indic
24ffa 61 74 65 73 20 61 20 62 65 74 74 65 72 20 6d 61  ates a better ma
24ffb 74 63 68 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  tch..**.** The r
24ffc 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73  eturned value is
24ffd 20 61 6c 77 61 79 73 20 62 65 74 77 65 65 6e 20   always between 
24ffe 30 20 61 6e 64 20 36 2c 20 61 73 20 66 6f 6c 6c  0 and 6, as foll
24fff 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 30 3a 20 4e 6f  ows:.**.** 0: No
25000 74 20 61 20 6d 61 74 63 68 2c 20 6f 72 20 69 66  t a match, or if
25001 20 6e 41 72 67 3c 30 20 61 6e 64 20 74 68 65 20   nArg<0 and the 
25002 66 75 6e 63 74 69 6f 6e 20 69 73 20 68 61 73 20  function is has 
25003 6e 6f 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  no implementatio
25004 6e 2e 0a 2a 2a 20 31 3a 20 41 20 76 61 72 69 61  n..** 1: A varia
25005 62 6c 65 20 61 72 67 75 6d 65 6e 74 73 20 66 75  ble arguments fu
25006 6e 63 74 69 6f 6e 20 74 68 61 74 20 70 72 65 66  nction that pref
25007 65 72 73 20 55 54 46 2d 38 20 77 68 65 6e 20 61  ers UTF-8 when a
25008 20 55 54 46 2d 31 36 0a 2a 2a 20 20 20 20 65 6e   UTF-16.**    en
25009 63 6f 64 69 6e 67 20 69 73 20 72 65 71 75 65 73  coding is reques
2500a 74 65 64 2c 20 6f 72 20 76 69 63 65 20 76 65 72  ted, or vice ver
2500b 73 61 2e 0a 2a 2a 20 32 3a 20 41 20 76 61 72 69  sa..** 2: A vari
2500c 61 62 6c 65 20 61 72 67 75 6d 65 6e 74 73 20 66  able arguments f
2500d 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 75 73 65  unction that use
2500e 73 20 55 54 46 2d 31 36 42 45 20 77 68 65 6e 20  s UTF-16BE when 
2500f 55 54 46 2d 31 36 4c 45 20 69 73 0a 2a 2a 20 20  UTF-16LE is.**  
25010 20 20 72 65 71 75 65 73 74 65 64 2c 20 6f 72 20    requested, or 
25011 76 69 63 65 20 76 65 72 73 61 2e 0a 2a 2a 20 33  vice versa..** 3
25012 3a 20 41 20 76 61 72 69 61 62 6c 65 20 61 72 67  : A variable arg
25013 75 6d 65 6e 74 73 20 66 75 6e 63 74 69 6f 6e 20  uments function 
25014 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 74  using the same t
25015 65 78 74 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a  ext encoding..**
25016 20 34 3a 20 41 20 66 75 6e 63 74 69 6f 6e 20 77   4: A function w
25017 69 74 68 20 74 68 65 20 65 78 61 63 74 20 6e 75  ith the exact nu
25018 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
25019 73 20 72 65 71 75 65 73 74 65 64 20 74 68 61 74  s requested that
2501a 0a 2a 2a 20 20 20 20 70 72 65 66 65 72 73 20 55  .**    prefers U
2501b 54 46 2d 38 20 77 68 65 6e 20 61 20 55 54 46 2d  TF-8 when a UTF-
2501c 31 36 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 72  16 encoding is r
2501d 65 71 75 65 73 74 65 64 2c 20 6f 72 20 76 69 63  equested, or vic
2501e 65 20 76 65 72 73 61 2e 0a 2a 2a 20 35 3a 20 41  e versa..** 5: A
2501f 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74   function with t
25020 68 65 20 65 78 61 63 74 20 6e 75 6d 62 65 72 20  he exact number 
25021 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 72 65 71  of arguments req
25022 75 65 73 74 65 64 20 74 68 61 74 0a 2a 2a 20 20  uested that.**  
25023 20 20 70 72 65 66 65 72 73 20 55 54 46 2d 31 36    prefers UTF-16
25024 4c 45 20 77 68 65 6e 20 55 54 46 2d 31 36 42 45  LE when UTF-16BE
25025 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 6f   is requested, o
25026 72 20 76 69 63 65 20 76 65 72 73 61 2e 0a 2a 2a  r vice versa..**
25027 20 36 3a 20 41 6e 20 65 78 61 63 74 20 6d 61 74   6: An exact mat
25028 63 68 2e 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63  ch..**.*/.static
25029 20 69 6e 74 20 6d 61 74 63 68 51 75 61 6c 69 74   int matchQualit
2502a 79 28 46 75 6e 63 44 65 66 20 2a 70 2c 20 69 6e  y(FuncDef *p, in
2502b 74 20 6e 41 72 67 2c 20 75 38 20 65 6e 63 29 7b  t nArg, u8 enc){
2502c 0a 20 20 69 6e 74 20 6d 61 74 63 68 20 3d 20 30  .  int match = 0
2502d 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 41 72 67 3d  ;.  if( p->nArg=
2502e 3d 2d 31 20 7c 7c 20 70 2d 3e 6e 41 72 67 3d 3d  =-1 || p->nArg==
2502f 6e 41 72 67 20 0a 20 20 20 7c 7c 20 28 6e 41 72  nArg .   || (nAr
25030 67 3d 3d 2d 31 20 26 26 20 28 70 2d 3e 78 46 75  g==-1 && (p->xFu
25031 6e 63 21 3d 30 20 7c 7c 20 70 2d 3e 78 53 74 65  nc!=0 || p->xSte
25032 70 21 3d 30 29 29 0a 20 20 29 7b 0a 20 20 20 20  p!=0)).  ){.    
25033 6d 61 74 63 68 20 3d 20 31 3b 0a 20 20 20 20 69  match = 1;.    i
25034 66 28 20 70 2d 3e 6e 41 72 67 3d 3d 6e 41 72 67  f( p->nArg==nArg
25035 20 7c 7c 20 6e 41 72 67 3d 3d 2d 31 20 29 7b 0a   || nArg==-1 ){.
25036 20 20 20 20 20 20 6d 61 74 63 68 20 3d 20 34 3b        match = 4;
25037 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 65  .    }.    if( e
25038 6e 63 3d 3d 70 2d 3e 69 50 72 65 66 45 6e 63 20  nc==p->iPrefEnc 
25039 29 7b 0a 20 20 20 20 20 20 6d 61 74 63 68 20 2b  ){.      match +
2503a 3d 20 32 3b 0a 20 20 20 20 7d 0a 20 20 20 20 65  = 2;.    }.    e
2503b 6c 73 65 20 69 66 28 20 28 65 6e 63 3d 3d 53 51  lse if( (enc==SQ
2503c 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 26 26 20  LITE_UTF16LE && 
2503d 70 2d 3e 69 50 72 65 66 45 6e 63 3d 3d 53 51 4c  p->iPrefEnc==SQL
2503e 49 54 45 5f 55 54 46 31 36 42 45 29 20 7c 7c 0a  ITE_UTF16BE) ||.
2503f 20 20 20 20 20 20 20 20 20 20 20 20 20 28 65 6e               (en
25040 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42  c==SQLITE_UTF16B
25041 45 20 26 26 20 70 2d 3e 69 50 72 65 66 45 6e 63  E && p->iPrefEnc
25042 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45  ==SQLITE_UTF16LE
25043 29 20 29 7b 0a 20 20 20 20 20 20 6d 61 74 63 68  ) ){.      match
25044 20 2b 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d   += 1;.    }.  }
25045 0a 20 20 72 65 74 75 72 6e 20 6d 61 74 63 68 3b  .  return match;
25046 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 61 72 63 68  .}../*.** Search
25047 20 61 20 46 75 6e 63 44 65 66 48 61 73 68 20 66   a FuncDefHash f
25048 6f 72 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69  or a function wi
25049 74 68 20 74 68 65 20 67 69 76 65 6e 20 6e 61 6d  th the given nam
2504a 65 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20  e.  Return.** a 
2504b 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d  pointer to the m
2504c 61 74 63 68 69 6e 67 20 46 75 6e 63 44 65 66 20  atching FuncDef 
2504d 69 66 20 66 6f 75 6e 64 2c 20 6f 72 20 30 20 69  if found, or 0 i
2504e 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61  f there is no ma
2504f 74 63 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 46  tch..*/.static F
25050 75 6e 63 44 65 66 20 2a 66 75 6e 63 74 69 6f 6e  uncDef *function
25051 53 65 61 72 63 68 28 0a 20 20 46 75 6e 63 44 65  Search(.  FuncDe
25052 66 48 61 73 68 20 2a 70 48 61 73 68 2c 20 20 2f  fHash *pHash,  /
25053 2a 20 48 61 73 68 20 74 61 62 6c 65 20 74 6f 20  * Hash table to 
25054 73 65 61 72 63 68 20 2a 2f 0a 20 20 69 6e 74 20  search */.  int 
25055 68 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h,              
25056 20 2f 2a 20 48 61 73 68 20 6f 66 20 74 68 65 20   /* Hash of the 
25057 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  name */.  const 
25058 63 68 61 72 20 2a 7a 46 75 6e 63 2c 20 20 20 2f  char *zFunc,   /
25059 2a 20 4e 61 6d 65 20 6f 66 20 66 75 6e 63 74 69  * Name of functi
2505a 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 75 6e  on */.  int nFun
2505b 63 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  c            /* 
2505c 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
2505d 69 6e 20 7a 46 75 6e 63 20 2a 2f 0a 29 7b 0a 20  in zFunc */.){. 
2505e 20 46 75 6e 63 44 65 66 20 2a 70 3b 0a 20 20 66   FuncDef *p;.  f
2505f 6f 72 28 70 3d 70 48 61 73 68 2d 3e 61 5b 68 5d  or(p=pHash->a[h]
25060 3b 20 70 3b 20 70 3d 70 2d 3e 70 48 61 73 68 29  ; p; p=p->pHash)
25061 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
25062 33 53 74 72 4e 49 43 6d 70 28 70 2d 3e 7a 4e 61  3StrNICmp(p->zNa
25063 6d 65 2c 20 7a 46 75 6e 63 2c 20 6e 46 75 6e 63  me, zFunc, nFunc
25064 29 3d 3d 30 20 26 26 20 70 2d 3e 7a 4e 61 6d 65  )==0 && p->zName
25065 5b 6e 46 75 6e 63 5d 3d 3d 30 20 29 7b 0a 20 20  [nFunc]==0 ){.  
25066 20 20 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 20      return p;.  
25067 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
25068 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73   0;.}../*.** Ins
25069 65 72 74 20 61 20 6e 65 77 20 46 75 6e 63 44 65  ert a new FuncDe
2506a 66 20 69 6e 74 6f 20 61 20 46 75 6e 63 44 65 66  f into a FuncDef
2506b 48 61 73 68 20 68 61 73 68 20 74 61 62 6c 65 2e  Hash hash table.
2506c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2506d 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46  TE void sqlite3F
2506e 75 6e 63 44 65 66 49 6e 73 65 72 74 28 0a 20 20  uncDefInsert(.  
2506f 46 75 6e 63 44 65 66 48 61 73 68 20 2a 70 48 61  FuncDefHash *pHa
25070 73 68 2c 20 20 2f 2a 20 54 68 65 20 68 61 73 68  sh,  /* The hash
25071 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63   table into whic
25072 68 20 74 6f 20 69 6e 73 65 72 74 20 2a 2f 0a 20  h to insert */. 
25073 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 20 20   FuncDef *pDef  
25074 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 75 6e        /* The fun
25075 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e  ction definition
25076 20 74 6f 20 69 6e 73 65 72 74 20 2a 2f 0a 29 7b   to insert */.){
25077 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 4f 74 68  .  FuncDef *pOth
25078 65 72 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 20  er;.  int nName 
25079 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
2507a 30 28 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 3b 0a  0(pDef->zName);.
2507b 20 20 75 38 20 63 31 20 3d 20 28 75 38 29 70 44    u8 c1 = (u8)pD
2507c 65 66 2d 3e 7a 4e 61 6d 65 5b 30 5d 3b 0a 20 20  ef->zName[0];.  
2507d 69 6e 74 20 68 20 3d 20 28 73 71 6c 69 74 65 33  int h = (sqlite3
2507e 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 63 31 5d  UpperToLower[c1]
2507f 20 2b 20 6e 4e 61 6d 65 29 20 25 20 41 72 72 61   + nName) % Arra
25080 79 53 69 7a 65 28 70 48 61 73 68 2d 3e 61 29 3b  ySize(pHash->a);
25081 0a 20 20 70 4f 74 68 65 72 20 3d 20 66 75 6e 63  .  pOther = func
25082 74 69 6f 6e 53 65 61 72 63 68 28 70 48 61 73 68  tionSearch(pHash
25083 2c 20 68 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d 65  , h, pDef->zName
25084 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20  , nName);.  if( 
25085 70 4f 74 68 65 72 20 29 7b 0a 20 20 20 20 61 73  pOther ){.    as
25086 73 65 72 74 28 20 70 4f 74 68 65 72 21 3d 70 44  sert( pOther!=pD
25087 65 66 20 26 26 20 70 4f 74 68 65 72 2d 3e 70 4e  ef && pOther->pN
25088 65 78 74 21 3d 70 44 65 66 20 29 3b 0a 20 20 20  ext!=pDef );.   
25089 20 70 44 65 66 2d 3e 70 4e 65 78 74 20 3d 20 70   pDef->pNext = p
2508a 4f 74 68 65 72 2d 3e 70 4e 65 78 74 3b 0a 20 20  Other->pNext;.  
2508b 20 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 20    pOther->pNext 
2508c 3d 20 70 44 65 66 3b 0a 20 20 7d 65 6c 73 65 7b  = pDef;.  }else{
2508d 0a 20 20 20 20 70 44 65 66 2d 3e 70 4e 65 78 74  .    pDef->pNext
2508e 20 3d 20 30 3b 0a 20 20 20 20 70 44 65 66 2d 3e   = 0;.    pDef->
2508f 70 48 61 73 68 20 3d 20 70 48 61 73 68 2d 3e 61  pHash = pHash->a
25090 5b 68 5d 3b 0a 20 20 20 20 70 48 61 73 68 2d 3e  [h];.    pHash->
25091 61 5b 68 5d 20 3d 20 70 44 65 66 3b 0a 20 20 7d  a[h] = pDef;.  }
25092 0a 7d 0a 20 20 0a 20 20 0a 0a 2f 2a 0a 2a 2a 20  .}.  .  ../*.** 
25093 4c 6f 63 61 74 65 20 61 20 75 73 65 72 20 66 75  Locate a user fu
25094 6e 63 74 69 6f 6e 20 67 69 76 65 6e 20 61 20 6e  nction given a n
25095 61 6d 65 2c 20 61 20 6e 75 6d 62 65 72 20 6f 66  ame, a number of
25096 20 61 72 67 75 6d 65 6e 74 73 20 61 6e 64 20 61   arguments and a
25097 20 66 6c 61 67 0a 2a 2a 20 69 6e 64 69 63 61 74   flag.** indicat
25098 69 6e 67 20 77 68 65 74 68 65 72 20 74 68 65 20  ing whether the 
25099 66 75 6e 63 74 69 6f 6e 20 70 72 65 66 65 72 73  function prefers
2509a 20 55 54 46 2d 31 36 20 6f 76 65 72 20 55 54 46   UTF-16 over UTF
2509b 2d 38 2e 20 20 52 65 74 75 72 6e 20 61 0a 2a 2a  -8.  Return a.**
2509c 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
2509d 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 75 72  FuncDef structur
2509e 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74  e that defines t
2509f 68 61 74 20 66 75 6e 63 74 69 6f 6e 2c 20 6f 72  hat function, or
250a0 20 72 65 74 75 72 6e 0a 2a 2a 20 4e 55 4c 4c 20   return.** NULL 
250a1 69 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  if the function 
250a2 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 0a  does not exist..
250a3 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 72 65  **.** If the cre
250a4 61 74 65 46 6c 61 67 20 61 72 67 75 6d 65 6e 74  ateFlag argument
250a5 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 61   is true, then a
250a6 20 6e 65 77 20 28 62 6c 61 6e 6b 29 20 46 75 6e   new (blank) Fun
250a7 63 44 65 66 0a 2a 2a 20 73 74 72 75 63 74 75 72  cDef.** structur
250a8 65 20 69 73 20 63 72 65 61 74 65 64 20 61 6e 64  e is created and
250a9 20 6c 69 6b 65 64 20 69 6e 74 6f 20 74 68 65 20   liked into the 
250aa 22 64 62 22 20 73 74 72 75 63 74 75 72 65 20 69  "db" structure i
250ab 66 20 61 0a 2a 2a 20 6e 6f 20 6d 61 74 63 68 69  f a.** no matchi
250ac 6e 67 20 66 75 6e 63 74 69 6f 6e 20 70 72 65 76  ng function prev
250ad 69 6f 75 73 6c 79 20 65 78 69 73 74 65 64 2e 20  iously existed. 
250ae 20 57 68 65 6e 20 63 72 65 61 74 65 46 6c 61 67   When createFlag
250af 20 69 73 20 74 72 75 65 0a 2a 2a 20 61 6e 64 20   is true.** and 
250b0 74 68 65 20 6e 41 72 67 20 70 61 72 61 6d 65 74  the nArg paramet
250b1 65 72 20 69 73 20 2d 31 2c 20 74 68 65 6e 20 6f  er is -1, then o
250b2 6e 6c 79 20 61 20 66 75 6e 63 74 69 6f 6e 20 74  nly a function t
250b3 68 61 74 20 61 63 63 65 70 74 73 0a 2a 2a 20 61  hat accepts.** a
250b4 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67  ny number of arg
250b5 75 6d 65 6e 74 73 20 77 69 6c 6c 20 62 65 20 72  uments will be r
250b6 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  eturned..**.** I
250b7 66 20 63 72 65 61 74 65 46 6c 61 67 20 69 73 20  f createFlag is 
250b8 66 61 6c 73 65 20 61 6e 64 20 6e 41 72 67 20 69  false and nArg i
250b9 73 20 2d 31 2c 20 74 68 65 6e 20 74 68 65 20 66  s -1, then the f
250ba 69 72 73 74 20 76 61 6c 69 64 0a 2a 2a 20 66 75  irst valid.** fu
250bb 6e 63 74 69 6f 6e 20 66 6f 75 6e 64 20 69 73 20  nction found is 
250bc 72 65 74 75 72 6e 65 64 2e 20 20 41 20 66 75 6e  returned.  A fun
250bd 63 74 69 6f 6e 20 69 73 20 76 61 6c 69 64 20 69  ction is valid i
250be 66 20 65 69 74 68 65 72 20 78 46 75 6e 63 0a 2a  f either xFunc.*
250bf 2a 20 6f 72 20 78 53 74 65 70 20 69 73 20 6e 6f  * or xStep is no
250c0 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66  n-zero..**.** If
250c1 20 63 72 65 61 74 65 46 6c 61 67 20 69 73 20 66   createFlag is f
250c2 61 6c 73 65 2c 20 74 68 65 6e 20 61 20 66 75 6e  alse, then a fun
250c3 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20 72  ction with the r
250c4 65 71 75 69 72 65 64 20 6e 61 6d 65 20 61 6e 64  equired name and
250c5 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 61 72  .** number of ar
250c6 67 75 6d 65 6e 74 73 20 6d 61 79 20 62 65 20 72  guments may be r
250c7 65 74 75 72 6e 65 64 20 65 76 65 6e 20 69 66 20  eturned even if 
250c8 74 68 65 20 65 54 65 78 74 52 65 70 20 66 6c 61  the eTextRep fla
250c9 67 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6d 61  g does not.** ma
250ca 74 63 68 20 74 68 61 74 20 72 65 71 75 65 73 74  tch that request
250cb 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
250cc 49 56 41 54 45 20 46 75 6e 63 44 65 66 20 2a 73  IVATE FuncDef *s
250cd 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69  qlite3FindFuncti
250ce 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  on(.  sqlite3 *d
250cf 62 2c 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f  b,       /* An o
250d0 70 65 6e 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  pen database */.
250d1 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
250d2 61 6d 65 2c 20 2f 2a 20 4e 61 6d 65 20 6f 66 20  ame, /* Name of 
250d3 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 4e  the function.  N
250d4 6f 74 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74  ot null-terminat
250d5 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d  ed */.  int nNam
250d6 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  e,         /* Nu
250d7 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65  mber of characte
250d8 72 73 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 2a  rs in the name *
250d9 2f 0a 20 20 69 6e 74 20 6e 41 72 67 2c 20 20 20  /.  int nArg,   
250da 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
250db 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e 20 20   of arguments.  
250dc 2d 31 20 6d 65 61 6e 73 20 61 6e 79 20 6e 75 6d  -1 means any num
250dd 62 65 72 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c  ber */.  u8 enc,
250de 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
250df 72 65 66 65 72 72 65 64 20 74 65 78 74 20 65 6e  referred text en
250e0 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20  coding */.  int 
250e1 63 72 65 61 74 65 46 6c 61 67 20 20 20 20 20 2f  createFlag     /
250e2 2a 20 43 72 65 61 74 65 20 6e 65 77 20 65 6e 74  * Create new ent
250e3 72 79 20 69 66 20 74 72 75 65 20 61 6e 64 20 64  ry if true and d
250e4 6f 65 73 20 6e 6f 74 20 6f 74 68 65 72 77 69 73  oes not otherwis
250e5 65 20 65 78 69 73 74 20 2a 2f 0a 29 7b 0a 20 20  e exist */.){.  
250e6 46 75 6e 63 44 65 66 20 2a 70 3b 20 20 20 20 20  FuncDef *p;     
250e7 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20      /* Iterator 
250e8 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 46 75  variable */.  Fu
250e9 6e 63 44 65 66 20 2a 70 42 65 73 74 20 3d 20 30  ncDef *pBest = 0
250ea 3b 20 2f 2a 20 42 65 73 74 20 6d 61 74 63 68 20  ; /* Best match 
250eb 66 6f 75 6e 64 20 73 6f 20 66 61 72 20 2a 2f 0a  found so far */.
250ec 20 20 69 6e 74 20 62 65 73 74 53 63 6f 72 65 20    int bestScore 
250ed 3d 20 30 3b 20 20 2f 2a 20 53 63 6f 72 65 20 6f  = 0;  /* Score o
250ee 66 20 62 65 73 74 20 6d 61 74 63 68 20 2a 2f 0a  f best match */.
250ef 20 20 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20    int h;        
250f0 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 76 61        /* Hash va
250f1 6c 75 65 20 2a 2f 0a 0a 0a 20 20 61 73 73 65 72  lue */...  asser
250f2 74 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  t( enc==SQLITE_U
250f3 54 46 38 20 7c 7c 20 65 6e 63 3d 3d 53 51 4c 49  TF8 || enc==SQLI
250f4 54 45 5f 55 54 46 31 36 4c 45 20 7c 7c 20 65 6e  TE_UTF16LE || en
250f5 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42  c==SQLITE_UTF16B
250f6 45 20 29 3b 0a 20 20 68 20 3d 20 28 73 71 6c 69  E );.  h = (sqli
250f7 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b  te3UpperToLower[
250f8 28 75 38 29 7a 4e 61 6d 65 5b 30 5d 5d 20 2b 20  (u8)zName[0]] + 
250f9 6e 4e 61 6d 65 29 20 25 20 41 72 72 61 79 53 69  nName) % ArraySi
250fa 7a 65 28 64 62 2d 3e 61 46 75 6e 63 2e 61 29 3b  ze(db->aFunc.a);
250fb 0a 0a 20 20 2f 2a 20 46 69 72 73 74 20 73 65 61  ..  /* First sea
250fc 72 63 68 20 66 6f 72 20 61 20 6d 61 74 63 68 20  rch for a match 
250fd 61 6d 6f 6e 67 73 74 20 74 68 65 20 61 70 70 6c  amongst the appl
250fe 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20  ication-defined 
250ff 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a  functions..  */.
25100 20 20 70 20 3d 20 66 75 6e 63 74 69 6f 6e 53 65    p = functionSe
25101 61 72 63 68 28 26 64 62 2d 3e 61 46 75 6e 63 2c  arch(&db->aFunc,
25102 20 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65   h, zName, nName
25103 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b  );.  while( p ){
25104 0a 20 20 20 20 69 6e 74 20 73 63 6f 72 65 20 3d  .    int score =
25105 20 6d 61 74 63 68 51 75 61 6c 69 74 79 28 70 2c   matchQuality(p,
25106 20 6e 41 72 67 2c 20 65 6e 63 29 3b 0a 20 20 20   nArg, enc);.   
25107 20 69 66 28 20 73 63 6f 72 65 3e 62 65 73 74 53   if( score>bestS
25108 63 6f 72 65 20 29 7b 0a 20 20 20 20 20 20 70 42  core ){.      pB
25109 65 73 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 62  est = p;.      b
2510a 65 73 74 53 63 6f 72 65 20 3d 20 73 63 6f 72 65  estScore = score
2510b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20  ;.    }.    p = 
2510c 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 0a 20  p->pNext;.  }.. 
2510d 20 2f 2a 20 49 66 20 6e 6f 20 6d 61 74 63 68 20   /* If no match 
2510e 69 73 20 66 6f 75 6e 64 2c 20 73 65 61 72 63 68  is found, search
2510f 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 66 75   the built-in fu
25110 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20  nctions..  **.  
25111 2a 2a 20 45 78 63 65 70 74 2c 20 69 66 20 63 72  ** Except, if cr
25112 65 61 74 65 46 6c 61 67 20 69 73 20 74 72 75 65  eateFlag is true
25113 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61  , that means tha
25114 74 20 77 65 20 61 72 65 20 74 72 79 69 6e 67 20  t we are trying 
25115 74 6f 0a 20 20 2a 2a 20 69 6e 73 74 61 6c 6c 20  to.  ** install 
25116 61 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 2e 20  a new function. 
25117 20 57 68 61 74 65 76 65 72 20 46 75 6e 63 44 65   Whatever FuncDe
25118 66 20 73 74 72 75 63 74 75 72 65 20 69 73 20 72  f structure is r
25119 65 74 75 72 6e 65 64 20 77 69 6c 6c 0a 20 20 2a  eturned will.  *
2511a 2a 20 68 61 76 65 20 66 69 65 6c 64 73 20 6f 76  * have fields ov
2511b 65 72 77 72 69 74 74 65 6e 20 77 69 74 68 20 6e  erwritten with n
2511c 65 77 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  ew information a
2511d 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 74  ppropriate for t
2511e 68 65 0a 20 20 2a 2a 20 6e 65 77 20 66 75 6e 63  he.  ** new func
2511f 74 69 6f 6e 2e 20 20 42 75 74 20 74 68 65 20 46  tion.  But the F
25120 75 6e 63 44 65 66 73 20 66 6f 72 20 62 75 69 6c  uncDefs for buil
25121 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e 73 20 61  t-in functions a
25122 72 65 20 72 65 61 64 2d 6f 6e 6c 79 2e 0a 20 20  re read-only..  
25123 2a 2a 20 53 6f 20 77 65 20 6d 75 73 74 20 6e 6f  ** So we must no
25124 74 20 73 65 61 72 63 68 20 66 6f 72 20 62 75 69  t search for bui
25125 6c 74 2d 69 6e 73 20 77 68 65 6e 20 63 72 65 61  lt-ins when crea
25126 74 69 6e 67 20 61 20 6e 65 77 20 66 75 6e 63 74  ting a new funct
25127 69 6f 6e 2e 0a 20 20 2a 2f 20 0a 20 20 69 66 28  ion..  */ .  if(
25128 20 21 63 72 65 61 74 65 46 6c 61 67 20 26 26 20   !createFlag && 
25129 21 70 42 65 73 74 20 29 7b 0a 20 20 20 20 46 75  !pBest ){.    Fu
2512a 6e 63 44 65 66 48 61 73 68 20 2a 70 48 61 73 68  ncDefHash *pHash
2512b 20 3d 20 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44   = &GLOBAL(FuncD
2512c 65 66 48 61 73 68 2c 20 73 71 6c 69 74 65 33 47  efHash, sqlite3G
2512d 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 29 3b  lobalFunctions);
2512e 0a 20 20 20 20 70 20 3d 20 66 75 6e 63 74 69 6f  .    p = functio
2512f 6e 53 65 61 72 63 68 28 70 48 61 73 68 2c 20 68  nSearch(pHash, h
25130 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b  , zName, nName);
25131 0a 20 20 20 20 77 68 69 6c 65 28 20 70 20 29 7b  .    while( p ){
25132 0a 20 20 20 20 20 20 69 6e 74 20 73 63 6f 72 65  .      int score
25133 20 3d 20 6d 61 74 63 68 51 75 61 6c 69 74 79 28   = matchQuality(
25134 70 2c 20 6e 41 72 67 2c 20 65 6e 63 29 3b 0a 20  p, nArg, enc);. 
25135 20 20 20 20 20 69 66 28 20 73 63 6f 72 65 3e 62       if( score>b
25136 65 73 74 53 63 6f 72 65 20 29 7b 0a 20 20 20 20  estScore ){.    
25137 20 20 20 20 70 42 65 73 74 20 3d 20 70 3b 0a 20      pBest = p;. 
25138 20 20 20 20 20 20 20 62 65 73 74 53 63 6f 72 65         bestScore
25139 20 3d 20 73 63 6f 72 65 3b 0a 20 20 20 20 20 20   = score;.      
2513a 7d 0a 20 20 20 20 20 20 70 20 3d 20 70 2d 3e 70  }.      p = p->p
2513b 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Next;.    }.  }.
2513c 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 72 65  .  /* If the cre
2513d 61 74 65 46 6c 61 67 20 70 61 72 61 6d 65 74 65  ateFlag paramete
2513e 72 20 69 73 20 74 72 75 65 20 61 6e 64 20 74 68  r is true and th
2513f 65 20 73 65 61 72 63 68 20 64 69 64 20 6e 6f 74  e search did not
25140 20 72 65 76 65 61 6c 20 61 6e 0a 20 20 2a 2a 20   reveal an.  ** 
25141 65 78 61 63 74 20 6d 61 74 63 68 20 66 6f 72 20  exact match for 
25142 74 68 65 20 6e 61 6d 65 2c 20 6e 75 6d 62 65 72  the name, number
25143 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 61 6e   of arguments an
25144 64 20 65 6e 63 6f 64 69 6e 67 2c 20 74 68 65 6e  d encoding, then
25145 20 61 64 64 20 61 0a 20 20 2a 2a 20 6e 65 77 20   add a.  ** new 
25146 65 6e 74 72 79 20 74 6f 20 74 68 65 20 68 61 73  entry to the has
25147 68 20 74 61 62 6c 65 20 61 6e 64 20 72 65 74 75  h table and retu
25148 72 6e 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66  rn it..  */.  if
25149 28 20 63 72 65 61 74 65 46 6c 61 67 20 26 26 20  ( createFlag && 
2514a 28 62 65 73 74 53 63 6f 72 65 3c 36 20 7c 7c 20  (bestScore<6 || 
2514b 70 42 65 73 74 2d 3e 6e 41 72 67 21 3d 6e 41 72  pBest->nArg!=nAr
2514c 67 29 20 26 26 20 0a 20 20 20 20 20 20 28 70 42  g) && .      (pB
2514d 65 73 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  est = sqlite3DbM
2514e 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  allocZero(db, si
2514f 7a 65 6f 66 28 2a 70 42 65 73 74 29 2b 6e 4e 61  zeof(*pBest)+nNa
25150 6d 65 2b 31 29 29 21 3d 30 20 29 7b 0a 20 20 20  me+1))!=0 ){.   
25151 20 70 42 65 73 74 2d 3e 7a 4e 61 6d 65 20 3d 20   pBest->zName = 
25152 28 63 68 61 72 20 2a 29 26 70 42 65 73 74 5b 31  (char *)&pBest[1
25153 5d 3b 0a 20 20 20 20 70 42 65 73 74 2d 3e 6e 41  ];.    pBest->nA
25154 72 67 20 3d 20 28 75 31 36 29 6e 41 72 67 3b 0a  rg = (u16)nArg;.
25155 20 20 20 20 70 42 65 73 74 2d 3e 69 50 72 65 66      pBest->iPref
25156 45 6e 63 20 3d 20 65 6e 63 3b 0a 20 20 20 20 6d  Enc = enc;.    m
25157 65 6d 63 70 79 28 70 42 65 73 74 2d 3e 7a 4e 61  emcpy(pBest->zNa
25158 6d 65 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65  me, zName, nName
25159 29 3b 0a 20 20 20 20 70 42 65 73 74 2d 3e 7a 4e  );.    pBest->zN
2515a 61 6d 65 5b 6e 4e 61 6d 65 5d 20 3d 20 30 3b 0a  ame[nName] = 0;.
2515b 20 20 20 20 73 71 6c 69 74 65 33 46 75 6e 63 44      sqlite3FuncD
2515c 65 66 49 6e 73 65 72 74 28 26 64 62 2d 3e 61 46  efInsert(&db->aF
2515d 75 6e 63 2c 20 70 42 65 73 74 29 3b 0a 20 20 7d  unc, pBest);.  }
2515e 0a 0a 20 20 69 66 28 20 70 42 65 73 74 20 26 26  ..  if( pBest &&
2515f 20 28 70 42 65 73 74 2d 3e 78 53 74 65 70 20 7c   (pBest->xStep |
25160 7c 20 70 42 65 73 74 2d 3e 78 46 75 6e 63 20 7c  | pBest->xFunc |
25161 7c 20 63 72 65 61 74 65 46 6c 61 67 29 20 29 7b  | createFlag) ){
25162 0a 20 20 20 20 72 65 74 75 72 6e 20 70 42 65 73  .    return pBes
25163 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  t;.  }.  return 
25164 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65  0;.}../*.** Free
25165 20 61 6c 6c 20 72 65 73 6f 75 72 63 65 73 20 68   all resources h
25166 65 6c 64 20 62 79 20 74 68 65 20 73 63 68 65 6d  eld by the schem
25167 61 20 73 74 72 75 63 74 75 72 65 2e 20 54 68 65  a structure. The
25168 20 76 6f 69 64 2a 20 61 72 67 75 6d 65 6e 74 20   void* argument 
25169 70 6f 69 6e 74 73 0a 2a 2a 20 61 74 20 61 20 53  points.** at a S
2516a 63 68 65 6d 61 20 73 74 72 75 63 74 2e 20 54 68  chema struct. Th
2516b 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73  is function does
2516c 20 6e 6f 74 20 63 61 6c 6c 20 73 71 6c 69 74 65   not call sqlite
2516d 33 44 62 46 72 65 65 28 64 62 2c 20 29 20 6f 6e  3DbFree(db, ) on
2516e 20 74 68 65 20 0a 2a 2a 20 70 6f 69 6e 74 65 72   the .** pointer
2516f 20 69 74 73 65 6c 66 2c 20 69 74 20 6a 75 73 74   itself, it just
25170 20 63 6c 65 61 6e 73 20 75 70 20 73 75 62 73 69   cleans up subsi
25171 64 75 61 72 79 20 72 65 73 6f 75 72 63 65 73 20  duary resources 
25172 28 69 2e 65 2e 20 74 68 65 20 63 6f 6e 74 65 6e  (i.e. the conten
25173 74 73 0a 2a 2a 20 6f 66 20 74 68 65 20 73 63 68  ts.** of the sch
25174 65 6d 61 20 68 61 73 68 20 74 61 62 6c 65 73 29  ema hash tables)
25175 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 63 68 65  ..**.** The Sche
25176 6d 61 2e 63 61 63 68 65 5f 73 69 7a 65 20 76 61  ma.cache_size va
25177 72 69 61 62 6c 65 20 69 73 20 6e 6f 74 20 63 6c  riable is not cl
25178 65 61 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  eared..*/.SQLITE
25179 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2517a 6c 69 74 65 33 53 63 68 65 6d 61 46 72 65 65 28  lite3SchemaFree(
2517b 76 6f 69 64 20 2a 70 29 7b 0a 20 20 48 61 73 68  void *p){.  Hash
2517c 20 74 65 6d 70 31 3b 0a 20 20 48 61 73 68 20 74   temp1;.  Hash t
2517d 65 6d 70 32 3b 0a 20 20 48 61 73 68 45 6c 65 6d  emp2;.  HashElem
2517e 20 2a 70 45 6c 65 6d 3b 0a 20 20 53 63 68 65 6d   *pElem;.  Schem
2517f 61 20 2a 70 53 63 68 65 6d 61 20 3d 20 28 53 63  a *pSchema = (Sc
25180 68 65 6d 61 20 2a 29 70 3b 0a 0a 20 20 74 65 6d  hema *)p;..  tem
25181 70 31 20 3d 20 70 53 63 68 65 6d 61 2d 3e 74 62  p1 = pSchema->tb
25182 6c 48 61 73 68 3b 0a 20 20 74 65 6d 70 32 20 3d  lHash;.  temp2 =
25183 20 70 53 63 68 65 6d 61 2d 3e 74 72 69 67 48 61   pSchema->trigHa
25184 73 68 3b 0a 20 20 73 71 6c 69 74 65 33 48 61 73  sh;.  sqlite3Has
25185 68 49 6e 69 74 28 26 70 53 63 68 65 6d 61 2d 3e  hInit(&pSchema->
25186 74 72 69 67 48 61 73 68 29 3b 0a 20 20 73 71 6c  trigHash);.  sql
25187 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 26 70  ite3HashClear(&p
25188 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 29  Schema->idxHash)
25189 3b 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d 73 71  ;.  for(pElem=sq
2518a 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 26 74  liteHashFirst(&t
2518b 65 6d 70 32 29 3b 20 70 45 6c 65 6d 3b 20 70 45  emp2); pElem; pE
2518c 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e 65  lem=sqliteHashNe
2518d 78 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20 20  xt(pElem)){.    
2518e 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69  sqlite3DeleteTri
2518f 67 67 65 72 28 30 2c 20 28 54 72 69 67 67 65 72  gger(0, (Trigger
25190 2a 29 73 71 6c 69 74 65 48 61 73 68 44 61 74 61  *)sqliteHashData
25191 28 70 45 6c 65 6d 29 29 3b 0a 20 20 7d 0a 20 20  (pElem));.  }.  
25192 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72  sqlite3HashClear
25193 28 26 74 65 6d 70 32 29 3b 0a 20 20 73 71 6c 69  (&temp2);.  sqli
25194 74 65 33 48 61 73 68 49 6e 69 74 28 26 70 53 63  te3HashInit(&pSc
25195 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 29 3b 0a  hema->tblHash);.
25196 20 20 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c 69    for(pElem=sqli
25197 74 65 48 61 73 68 46 69 72 73 74 28 26 74 65 6d  teHashFirst(&tem
25198 70 31 29 3b 20 70 45 6c 65 6d 3b 20 70 45 6c 65  p1); pElem; pEle
25199 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74  m=sqliteHashNext
2519a 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20 20 54 61  (pElem)){.    Ta
2519b 62 6c 65 20 2a 70 54 61 62 20 3d 20 73 71 6c 69  ble *pTab = sqli
2519c 74 65 48 61 73 68 44 61 74 61 28 70 45 6c 65 6d  teHashData(pElem
2519d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
2519e 54 61 62 2d 3e 64 62 4d 65 6d 3d 3d 30 20 29 3b  Tab->dbMem==0 );
2519f 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65  .    sqlite3Dele
251a0 74 65 54 61 62 6c 65 28 70 54 61 62 29 3b 0a 20  teTable(pTab);. 
251a1 20 7d 0a 20 20 73 71 6c 69 74 65 33 48 61 73 68   }.  sqlite3Hash
251a2 43 6c 65 61 72 28 26 74 65 6d 70 31 29 3b 0a 20  Clear(&temp1);. 
251a3 20 70 53 63 68 65 6d 61 2d 3e 70 53 65 71 54 61   pSchema->pSeqTa
251a4 62 20 3d 20 30 3b 0a 20 20 70 53 63 68 65 6d 61  b = 0;.  pSchema
251a5 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 44 42 5f 53  ->flags &= ~DB_S
251a6 63 68 65 6d 61 4c 6f 61 64 65 64 3b 0a 7d 0a 0a  chemaLoaded;.}..
251a7 2f 2a 0a 2a 2a 20 46 69 6e 64 20 61 6e 64 20 72  /*.** Find and r
251a8 65 74 75 72 6e 20 74 68 65 20 73 63 68 65 6d 61  eturn the schema
251a9 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
251aa 20 61 20 42 54 72 65 65 2e 20 20 43 72 65 61 74   a BTree.  Creat
251ab 65 0a 2a 2a 20 61 20 6e 65 77 20 6f 6e 65 20 69  e.** a new one i
251ac 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a  f necessary..*/.
251ad 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53  SQLITE_PRIVATE S
251ae 63 68 65 6d 61 20 2a 73 71 6c 69 74 65 33 53 63  chema *sqlite3Sc
251af 68 65 6d 61 47 65 74 28 73 71 6c 69 74 65 33 20  hemaGet(sqlite3 
251b0 2a 64 62 2c 20 42 74 72 65 65 20 2a 70 42 74 29  *db, Btree *pBt)
251b1 7b 0a 20 20 53 63 68 65 6d 61 20 2a 20 70 3b 0a  {.  Schema * p;.
251b2 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20    if( pBt ){.   
251b3 20 70 20 3d 20 28 53 63 68 65 6d 61 20 2a 29 73   p = (Schema *)s
251b4 71 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 6d  qlite3BtreeSchem
251b5 61 28 70 42 74 2c 20 73 69 7a 65 6f 66 28 53 63  a(pBt, sizeof(Sc
251b6 68 65 6d 61 29 2c 20 73 71 6c 69 74 65 33 53 63  hema), sqlite3Sc
251b7 68 65 6d 61 46 72 65 65 29 3b 0a 20 20 7d 65 6c  hemaFree);.  }el
251b8 73 65 7b 0a 20 20 20 20 70 20 3d 20 28 53 63 68  se{.    p = (Sch
251b9 65 6d 61 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c  ema *)sqlite3Mal
251ba 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 6f 66 28 53  locZero(sizeof(S
251bb 63 68 65 6d 61 29 29 3b 0a 20 20 7d 0a 20 20 69  chema));.  }.  i
251bc 66 28 20 21 70 20 29 7b 0a 20 20 20 20 64 62 2d  f( !p ){.    db-
251bd 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
251be 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 20 28 20  1;.  }else if ( 
251bf 30 3d 3d 70 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61  0==p->file_forma
251c0 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
251c1 48 61 73 68 49 6e 69 74 28 26 70 2d 3e 74 62 6c  HashInit(&p->tbl
251c2 48 61 73 68 29 3b 0a 20 20 20 20 73 71 6c 69 74  Hash);.    sqlit
251c3 65 33 48 61 73 68 49 6e 69 74 28 26 70 2d 3e 69  e3HashInit(&p->i
251c4 64 78 48 61 73 68 29 3b 0a 20 20 20 20 73 71 6c  dxHash);.    sql
251c5 69 74 65 33 48 61 73 68 49 6e 69 74 28 26 70 2d  ite3HashInit(&p-
251c6 3e 74 72 69 67 48 61 73 68 29 3b 0a 20 20 20 20  >trigHash);.    
251c7 70 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f  p->enc = SQLITE_
251c8 55 54 46 38 3b 0a 20 20 7d 0a 20 20 72 65 74 75  UTF8;.  }.  retu
251c9 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  rn p;.}../******
251ca 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
251cb 63 61 6c 6c 62 61 63 6b 2e 63 20 2a 2a 2a 2a 2a  callback.c *****
251cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
251cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
251ce 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
251cf 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
251d0 69 6c 65 20 64 65 6c 65 74 65 2e 63 20 2a 2a 2a  ile delete.c ***
251d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
251d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
251d3 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
251d4 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35  001 September 15
251d5 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
251d6 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
251d7 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
251d8 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
251d9 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
251da 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
251db 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
251dc 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
251dd 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
251de 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
251df 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
251e0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
251e1 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
251e2 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
251e3 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
251e4 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
251e5 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
251e6 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
251e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
251e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
251e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
251ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
251eb 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
251ec 74 61 69 6e 73 20 43 20 63 6f 64 65 20 72 6f 75  tains C code rou
251ed 74 69 6e 65 73 20 74 68 61 74 20 61 72 65 20 63  tines that are c
251ee 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72  alled by the par
251ef 73 65 72 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20  ser.** in order 
251f0 74 6f 20 67 65 6e 65 72 61 74 65 20 63 6f 64 65  to generate code
251f1 20 66 6f 72 20 44 45 4c 45 54 45 20 46 52 4f 4d   for DELETE FROM
251f2 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a   statements..**.
251f3 2a 2a 20 24 49 64 3a 20 64 65 6c 65 74 65 2e 63  ** $Id: delete.c
251f4 2c 76 20 31 2e 32 30 37 20 32 30 30 39 2f 30 38  ,v 1.207 2009/08
251f5 2f 30 38 20 31 38 3a 30 31 3a 30 38 20 64 72 68  /08 18:01:08 drh
251f6 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a   Exp $.*/../*.**
251f7 20 4c 6f 6f 6b 20 75 70 20 65 76 65 72 79 20 74   Look up every t
251f8 61 62 6c 65 20 74 68 61 74 20 69 73 20 6e 61 6d  able that is nam
251f9 65 64 20 69 6e 20 70 53 72 63 2e 20 20 49 66 20  ed in pSrc.  If 
251fa 61 6e 79 20 74 61 62 6c 65 20 69 73 20 6e 6f 74  any table is not
251fb 20 66 6f 75 6e 64 2c 0a 2a 2a 20 61 64 64 20 61   found,.** add a
251fc 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
251fd 74 6f 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d  to pParse->zErrM
251fe 73 67 20 61 6e 64 20 72 65 74 75 72 6e 20 4e 55  sg and return NU
251ff 4c 4c 2e 20 20 49 66 20 61 6c 6c 20 74 61 62 6c  LL.  If all tabl
25200 65 73 0a 2a 2a 20 61 72 65 20 66 6f 75 6e 64 2c  es.** are found,
25201 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   return a pointe
25202 72 20 74 6f 20 74 68 65 20 6c 61 73 74 20 74 61  r to the last ta
25203 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ble..*/.SQLITE_P
25204 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71  RIVATE Table *sq
25205 6c 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b  lite3SrcListLook
25206 75 70 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  up(Parse *pParse
25207 2c 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 29  , SrcList *pSrc)
25208 7b 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69  {.  struct SrcLi
25209 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d  st_item *pItem =
2520a 20 70 53 72 63 2d 3e 61 3b 0a 20 20 54 61 62 6c   pSrc->a;.  Tabl
2520b 65 20 2a 70 54 61 62 3b 0a 20 20 61 73 73 65 72  e *pTab;.  asser
2520c 74 28 20 70 49 74 65 6d 20 26 26 20 70 53 72 63  t( pItem && pSrc
2520d 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 70  ->nSrc==1 );.  p
2520e 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63  Tab = sqlite3Loc
2520f 61 74 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c  ateTable(pParse,
25210 20 30 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65   0, pItem->zName
25211 2c 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61  , pItem->zDataba
25212 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65  se);.  sqlite3De
25213 6c 65 74 65 54 61 62 6c 65 28 70 49 74 65 6d 2d  leteTable(pItem-
25214 3e 70 54 61 62 29 3b 0a 20 20 70 49 74 65 6d 2d  >pTab);.  pItem-
25215 3e 70 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20  >pTab = pTab;.  
25216 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20  if( pTab ){.    
25217 70 54 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20  pTab->nRef++;.  
25218 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 49  }.  if( sqlite3I
25219 6e 64 65 78 65 64 42 79 4c 6f 6f 6b 75 70 28 70  ndexedByLookup(p
2521a 50 61 72 73 65 2c 20 70 49 74 65 6d 29 20 29 7b  Parse, pItem) ){
2521b 0a 20 20 20 20 70 54 61 62 20 3d 20 30 3b 0a 20  .    pTab = 0;. 
2521c 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 54 61 62   }.  return pTab
2521d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b  ;.}../*.** Check
2521e 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
2521f 65 20 67 69 76 65 6e 20 74 61 62 6c 65 20 69 73  e given table is
25220 20 77 72 69 74 61 62 6c 65 2e 20 20 49 66 20 69   writable.  If i
25221 74 20 69 73 20 6e 6f 74 0a 2a 2a 20 77 72 69 74  t is not.** writ
25222 61 62 6c 65 2c 20 67 65 6e 65 72 61 74 65 20 61  able, generate a
25223 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
25224 61 6e 64 20 72 65 74 75 72 6e 20 31 2e 20 20 49  and return 1.  I
25225 66 20 69 74 20 69 73 0a 2a 2a 20 77 72 69 74 61  f it is.** writa
25226 62 6c 65 20 72 65 74 75 72 6e 20 30 3b 0a 2a 2f  ble return 0;.*/
25227 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
25228 69 6e 74 20 73 71 6c 69 74 65 33 49 73 52 65 61  int sqlite3IsRea
25229 64 4f 6e 6c 79 28 50 61 72 73 65 20 2a 70 50 61  dOnly(Parse *pPa
2522a 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62  rse, Table *pTab
2522b 2c 20 69 6e 74 20 76 69 65 77 4f 6b 29 7b 0a 20  , int viewOk){. 
2522c 20 2f 2a 20 41 20 74 61 62 6c 65 20 69 73 20 6e   /* A table is n
2522d 6f 74 20 77 72 69 74 61 62 6c 65 20 75 6e 64 65  ot writable unde
2522e 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  r the following 
2522f 63 69 72 63 75 6d 73 74 61 6e 63 65 73 3a 0a 20  circumstances:. 
25230 20 2a 2a 0a 20 20 2a 2a 20 20 20 31 29 20 49 74   **.  **   1) It
25231 20 69 73 20 61 20 76 69 72 74 75 61 6c 20 74 61   is a virtual ta
25232 62 6c 65 20 61 6e 64 20 6e 6f 20 69 6d 70 6c 65  ble and no imple
25233 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
25234 20 78 55 70 64 61 74 65 20 6d 65 74 68 6f 64 0a   xUpdate method.
25235 20 20 2a 2a 20 20 20 20 20 20 68 61 73 20 62 65    **      has be
25236 65 6e 20 70 72 6f 76 69 64 65 64 2c 20 6f 72 0a  en provided, or.
25237 20 20 2a 2a 20 20 20 32 29 20 49 74 20 69 73 20    **   2) It is 
25238 61 20 73 79 73 74 65 6d 20 74 61 62 6c 65 20 28  a system table (
25239 69 2e 65 2e 20 73 71 6c 69 74 65 5f 6d 61 73 74  i.e. sqlite_mast
2523a 65 72 29 2c 20 74 68 69 73 20 63 61 6c 6c 20 69  er), this call i
2523b 73 20 6e 6f 74 0a 20 20 2a 2a 20 20 20 20 20 20  s not.  **      
2523c 70 61 72 74 20 6f 66 20 61 20 6e 65 73 74 65 64  part of a nested
2523d 20 70 61 72 73 65 20 61 6e 64 20 77 72 69 74 61   parse and writa
2523e 62 6c 65 5f 73 63 68 65 6d 61 20 70 72 61 67 6d  ble_schema pragm
2523f 61 20 68 61 73 20 6e 6f 74 20 0a 20 20 2a 2a 20  a has not .  ** 
25240 20 20 20 20 20 62 65 65 6e 20 73 70 65 63 69 66       been specif
25241 69 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49  ied..  **.  ** I
25242 6e 20 65 69 74 68 65 72 20 63 61 73 65 20 6c 65  n either case le
25243 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ave an error mes
25244 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 20 61  sage in pParse a
25245 6e 64 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65  nd return non-ze
25246 72 6f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  ro..  */.  if( (
25247 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
25248 20 0a 20 20 20 20 20 26 26 20 73 71 6c 69 74 65   .     && sqlite
25249 33 47 65 74 56 54 61 62 6c 65 28 70 50 61 72 73  3GetVTable(pPars
2524a 65 2d 3e 64 62 2c 20 70 54 61 62 29 2d 3e 70 4d  e->db, pTab)->pM
2524b 6f 64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 55 70  od->pModule->xUp
2524c 64 61 74 65 3d 3d 30 20 29 0a 20 20 20 7c 7c 20  date==0 ).   || 
2524d 28 20 28 70 54 61 62 2d 3e 74 61 62 46 6c 61 67  ( (pTab->tabFlag
2524e 73 20 26 20 54 46 5f 52 65 61 64 6f 6e 6c 79 29  s & TF_Readonly)
2524f 21 3d 30 0a 20 20 20 20 20 26 26 20 28 70 50 61  !=0.     && (pPa
25250 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26  rse->db->flags &
25251 20 53 51 4c 49 54 45 5f 57 72 69 74 65 53 63 68   SQLITE_WriteSch
25252 65 6d 61 29 3d 3d 30 0a 20 20 20 20 20 26 26 20  ema)==0.     && 
25253 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d  pParse->nested==
25254 30 20 29 0a 20 20 29 7b 0a 20 20 20 20 73 71 6c  0 ).  ){.    sql
25255 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
25256 72 73 65 2c 20 22 74 61 62 6c 65 20 25 73 20 6d  rse, "table %s m
25257 61 79 20 6e 6f 74 20 62 65 20 6d 6f 64 69 66 69  ay not be modifi
25258 65 64 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ed", pTab->zName
25259 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  );.    return 1;
2525a 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  .  }..#ifndef SQ
2525b 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20  LITE_OMIT_VIEW. 
2525c 20 69 66 28 20 21 76 69 65 77 4f 6b 20 26 26 20   if( !viewOk && 
2525d 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b  pTab->pSelect ){
2525e 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
2525f 72 4d 73 67 28 70 50 61 72 73 65 2c 22 63 61 6e  rMsg(pParse,"can
25260 6e 6f 74 20 6d 6f 64 69 66 79 20 25 73 20 62 65  not modify %s be
25261 63 61 75 73 65 20 69 74 20 69 73 20 61 20 76 69  cause it is a vi
25262 65 77 22 2c 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  ew",pTab->zName)
25263 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  ;.    return 1;.
25264 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74    }.#endif.  ret
25265 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 23 69 66 20 21  urn 0;.}...#if !
25266 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
25267 4d 49 54 5f 56 49 45 57 29 20 26 26 20 21 64 65  MIT_VIEW) && !de
25268 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
25269 54 5f 54 52 49 47 47 45 52 29 0a 2f 2a 0a 2a 2a  T_TRIGGER)./*.**
2526a 20 45 76 61 6c 75 61 74 65 20 61 20 76 69 65 77   Evaluate a view
2526b 20 61 6e 64 20 73 74 6f 72 65 20 69 74 73 20 72   and store its r
2526c 65 73 75 6c 74 20 69 6e 20 61 6e 20 65 70 68 65  esult in an ephe
2526d 6d 65 72 61 6c 20 74 61 62 6c 65 2e 20 20 54 68  meral table.  Th
2526e 65 0a 2a 2a 20 70 57 68 65 72 65 20 61 72 67 75  e.** pWhere argu
2526f 6d 65 6e 74 20 69 73 20 61 6e 20 6f 70 74 69 6f  ment is an optio
25270 6e 61 6c 20 57 48 45 52 45 20 63 6c 61 75 73 65  nal WHERE clause
25271 20 74 68 61 74 20 72 65 73 74 72 69 63 74 73 20   that restricts 
25272 74 68 65 0a 2a 2a 20 73 65 74 20 6f 66 20 72 6f  the.** set of ro
25273 77 73 20 69 6e 20 74 68 65 20 76 69 65 77 20 74  ws in the view t
25274 68 61 74 20 61 72 65 20 74 6f 20 62 65 20 61 64  hat are to be ad
25275 64 65 64 20 74 6f 20 74 68 65 20 65 70 68 65 6d  ded to the ephem
25276 65 72 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53  eral table..*/.S
25277 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
25278 69 64 20 73 71 6c 69 74 65 33 4d 61 74 65 72 69  id sqlite3Materi
25279 61 6c 69 7a 65 56 69 65 77 28 0a 20 20 50 61 72  alizeView(.  Par
2527a 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
2527b 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
2527c 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20  text */.  Table 
2527d 2a 70 56 69 65 77 2c 20 20 20 20 20 20 20 20 2f  *pView,        /
2527e 2a 20 56 69 65 77 20 64 65 66 69 6e 69 74 69 6f  * View definitio
2527f 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68  n */.  Expr *pWh
25280 65 72 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 4f  ere,        /* O
25281 70 74 69 6f 6e 61 6c 20 57 48 45 52 45 20 63 6c  ptional WHERE cl
25282 61 75 73 65 20 74 6f 20 62 65 20 61 64 64 65 64  ause to be added
25283 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 20 20   */.  int iCur  
25284 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75             /* Cu
25285 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20  rsor number for 
25286 65 70 68 65 6d 65 72 69 61 6c 20 74 61 62 6c 65  ephemerial table
25287 20 2a 2f 0a 29 7b 0a 20 20 53 65 6c 65 63 74 44   */.){.  SelectD
25288 65 73 74 20 64 65 73 74 3b 0a 20 20 53 65 6c 65  est dest;.  Sele
25289 63 74 20 2a 70 44 75 70 3b 0a 20 20 73 71 6c 69  ct *pDup;.  sqli
2528a 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
2528b 2d 3e 64 62 3b 0a 0a 20 20 70 44 75 70 20 3d 20  ->db;..  pDup = 
2528c 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70  sqlite3SelectDup
2528d 28 64 62 2c 20 70 56 69 65 77 2d 3e 70 53 65 6c  (db, pView->pSel
2528e 65 63 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70  ect, 0);.  if( p
2528f 57 68 65 72 65 20 29 7b 0a 20 20 20 20 53 72 63  Where ){.    Src
25290 4c 69 73 74 20 2a 70 46 72 6f 6d 3b 0a 20 20 20  List *pFrom;.   
25291 20 0a 20 20 20 20 70 57 68 65 72 65 20 3d 20 73   .    pWhere = s
25292 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
25293 2c 20 70 57 68 65 72 65 2c 20 30 29 3b 0a 20 20  , pWhere, 0);.  
25294 20 20 70 46 72 6f 6d 20 3d 20 73 71 6c 69 74 65    pFrom = sqlite
25295 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 64  3SrcListAppend(d
25296 62 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20  b, 0, 0, 0);.   
25297 20 69 66 28 20 70 46 72 6f 6d 20 29 7b 0a 20 20   if( pFrom ){.  
25298 20 20 20 20 61 73 73 65 72 74 28 20 70 46 72 6f      assert( pFro
25299 6d 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20  m->nSrc==1 );.  
2529a 20 20 20 20 70 46 72 6f 6d 2d 3e 61 5b 30 5d 2e      pFrom->a[0].
2529b 7a 41 6c 69 61 73 20 3d 20 73 71 6c 69 74 65 33  zAlias = sqlite3
2529c 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 56 69  DbStrDup(db, pVi
2529d 65 77 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ew->zName);.    
2529e 20 20 70 46 72 6f 6d 2d 3e 61 5b 30 5d 2e 70 53    pFrom->a[0].pS
2529f 65 6c 65 63 74 20 3d 20 70 44 75 70 3b 0a 20 20  elect = pDup;.  
252a0 20 20 20 20 61 73 73 65 72 74 28 20 70 46 72 6f      assert( pFro
252a1 6d 2d 3e 61 5b 30 5d 2e 70 4f 6e 3d 3d 30 20 29  m->a[0].pOn==0 )
252a2 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
252a3 70 46 72 6f 6d 2d 3e 61 5b 30 5d 2e 70 55 73 69  pFrom->a[0].pUsi
252a4 6e 67 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c  ng==0 );.    }el
252a5 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
252a6 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62  3SelectDelete(db
252a7 2c 20 70 44 75 70 29 3b 0a 20 20 20 20 7d 0a 20  , pDup);.    }. 
252a8 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65     pDup = sqlite
252a9 33 53 65 6c 65 63 74 4e 65 77 28 70 50 61 72 73  3SelectNew(pPars
252aa 65 2c 20 30 2c 20 70 46 72 6f 6d 2c 20 70 57 68  e, 0, pFrom, pWh
252ab 65 72 65 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  ere, 0, 0, 0, 0,
252ac 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 73 71   0, 0);.  }.  sq
252ad 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 49  lite3SelectDestI
252ae 6e 69 74 28 26 64 65 73 74 2c 20 53 52 54 5f 45  nit(&dest, SRT_E
252af 70 68 65 6d 54 61 62 2c 20 69 43 75 72 29 3b 0a  phemTab, iCur);.
252b0 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28    sqlite3Select(
252b1 70 50 61 72 73 65 2c 20 70 44 75 70 2c 20 26 64  pParse, pDup, &d
252b2 65 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 53  est);.  sqlite3S
252b3 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20  electDelete(db, 
252b4 70 44 75 70 29 3b 0a 7d 0a 23 65 6e 64 69 66 20  pDup);.}.#endif 
252b5 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  /* !defined(SQLI
252b6 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 26 26  TE_OMIT_VIEW) &&
252b7 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
252b8 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 20 2a  _OMIT_TRIGGER) *
252b9 2f 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  /..#if defined(S
252ba 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 50 44  QLITE_ENABLE_UPD
252bb 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d 49 54  ATE_DELETE_LIMIT
252bc 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
252bd 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
252be 52 59 29 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  RY)./*.** Genera
252bf 74 65 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  te an expression
252c0 20 74 72 65 65 20 74 6f 20 69 6d 70 6c 65 6d 65   tree to impleme
252c1 6e 74 20 74 68 65 20 57 48 45 52 45 2c 20 4f 52  nt the WHERE, OR
252c2 44 45 52 20 42 59 2c 0a 2a 2a 20 61 6e 64 20 4c  DER BY,.** and L
252c3 49 4d 49 54 2f 4f 46 46 53 45 54 20 70 6f 72 74  IMIT/OFFSET port
252c4 69 6f 6e 20 6f 66 20 44 45 4c 45 54 45 20 61 6e  ion of DELETE an
252c5 64 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65  d UPDATE stateme
252c6 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44  nts..**.**     D
252c7 45 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c 65  ELETE FROM table
252c8 5f 77 78 79 7a 20 57 48 45 52 45 20 61 3c 35 20  _wxyz WHERE a<5 
252c9 4f 52 44 45 52 20 42 59 20 61 20 4c 49 4d 49 54  ORDER BY a LIMIT
252ca 20 31 3b 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   1;.**          
252cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
252cc 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f    \_____________
252cd 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a  _____________/.*
252ce 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
252cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
252d0 70 4c 69 6d 69 74 57 68 65 72 65 20 28 70 49 6e  pLimitWhere (pIn
252d1 43 6c 61 75 73 65 29 0a 2a 2f 0a 53 51 4c 49 54  Clause).*/.SQLIT
252d2 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a  E_PRIVATE Expr *
252d3 73 71 6c 69 74 65 33 4c 69 6d 69 74 57 68 65 72  sqlite3LimitWher
252d4 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  e(.  Parse *pPar
252d5 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  se,             
252d6 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20    /* The parser 
252d7 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63  context */.  Src
252d8 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20  List *pSrc,     
252d9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65            /* the
252da 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2d 2d 20   FROM clause -- 
252db 77 68 69 63 68 20 74 61 62 6c 65 73 20 74 6f 20  which tables to 
252dc 73 63 61 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a  scan */.  Expr *
252dd 70 57 68 65 72 65 2c 20 20 20 20 20 20 20 20 20  pWhere,         
252de 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48         /* The WH
252df 45 52 45 20 63 6c 61 75 73 65 2e 20 20 4d 61 79  ERE clause.  May
252e0 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a 20 20 45 78   be null */.  Ex
252e1 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79  prList *pOrderBy
252e2 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ,          /* Th
252e3 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
252e4 65 2e 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c 20  e.  May be null 
252e5 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d 69  */.  Expr *pLimi
252e6 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
252e7 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20 63    /* The LIMIT c
252e8 6c 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20 6e  lause.  May be n
252e9 75 6c 6c 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ull */.  Expr *p
252ea 4f 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20  Offset,         
252eb 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 46 46        /* The OFF
252ec 53 45 54 20 63 6c 61 75 73 65 2e 20 20 4d 61 79  SET clause.  May
252ed 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a 20 20 63 68   be null */.  ch
252ee 61 72 20 2a 7a 53 74 6d 74 54 79 70 65 20 20 20  ar *zStmtType   
252ef 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 69             /* Ei
252f0 74 68 65 72 20 44 45 4c 45 54 45 20 6f 72 20 55  ther DELETE or U
252f1 50 44 41 54 45 2e 20 20 46 6f 72 20 65 72 72 6f  PDATE.  For erro
252f2 72 20 6d 65 73 73 61 67 65 73 2e 20 2a 2f 0a 29  r messages. */.)
252f3 7b 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65  {.  Expr *pWhere
252f4 52 6f 77 69 64 20 3d 20 4e 55 4c 4c 3b 20 20 20  Rowid = NULL;   
252f5 20 2f 2a 20 57 48 45 52 45 20 72 6f 77 69 64 20   /* WHERE rowid 
252f6 2e 2e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 49  .. */.  Expr *pI
252f7 6e 43 6c 61 75 73 65 20 3d 20 4e 55 4c 4c 3b 20  nClause = NULL; 
252f8 20 20 20 20 20 2f 2a 20 57 48 45 52 45 20 72 6f       /* WHERE ro
252f9 77 69 64 20 49 4e 20 28 20 73 65 6c 65 63 74 20  wid IN ( select 
252fa 29 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 53 65  ) */.  Expr *pSe
252fb 6c 65 63 74 52 6f 77 69 64 20 3d 20 4e 55 4c 4c  lectRowid = NULL
252fc 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 72 6f  ;   /* SELECT ro
252fd 77 69 64 20 2e 2e 2e 20 2a 2f 0a 20 20 45 78 70  wid ... */.  Exp
252fe 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20 3d 20  rList *pEList = 
252ff 4e 55 4c 4c 3b 20 20 20 20 20 2f 2a 20 45 78 70  NULL;     /* Exp
25300 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 63 6f 6e  ression list con
25301 74 61 6e 69 6e 67 20 6f 6e 6c 79 20 70 53 65 6c  taning only pSel
25302 65 63 74 52 6f 77 69 64 20 2a 2f 0a 20 20 53 72  ectRowid */.  Sr
25303 63 4c 69 73 74 20 2a 70 53 65 6c 65 63 74 53 72  cList *pSelectSr
25304 63 20 3d 20 4e 55 4c 4c 3b 20 20 2f 2a 20 53 45  c = NULL;  /* SE
25305 4c 45 43 54 20 72 6f 77 69 64 20 46 52 4f 4d 20  LECT rowid FROM 
25306 78 20 2e 2e 2e 20 28 64 75 70 20 6f 66 20 70 53  x ... (dup of pS
25307 72 63 29 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  rc) */.  Select 
25308 2a 70 53 65 6c 65 63 74 20 3d 20 4e 55 4c 4c 3b  *pSelect = NULL;
25309 20 20 20 20 20 20 2f 2a 20 43 6f 6d 70 6c 65 74        /* Complet
2530a 65 20 53 45 4c 45 43 54 20 74 72 65 65 20 2a 2f  e SELECT tree */
2530b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61  ..  /* Check tha
2530c 74 20 74 68 65 72 65 20 69 73 6e 27 74 20 61 6e  t there isn't an
2530d 20 4f 52 44 45 52 20 42 59 20 77 69 74 68 6f 75   ORDER BY withou
2530e 74 20 61 20 4c 49 4d 49 54 20 63 6c 61 75 73 65  t a LIMIT clause
2530f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4f 72  ..  */.  if( pOr
25310 64 65 72 42 79 20 26 26 20 28 70 4c 69 6d 69 74  derBy && (pLimit
25311 20 3d 3d 20 30 29 20 29 20 7b 0a 20 20 20 20 73   == 0) ) {.    s
25312 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
25313 50 61 72 73 65 2c 20 22 4f 52 44 45 52 20 42 59  Parse, "ORDER BY
25314 20 77 69 74 68 6f 75 74 20 4c 49 4d 49 54 20 6f   without LIMIT o
25315 6e 20 25 73 22 2c 20 7a 53 74 6d 74 54 79 70 65  n %s", zStmtType
25316 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 70  );.    pParse->p
25317 61 72 73 65 45 72 72 6f 72 20 3d 20 31 3b 0a 20  arseError = 1;. 
25318 20 20 20 67 6f 74 6f 20 6c 69 6d 69 74 5f 77 68     goto limit_wh
25319 65 72 65 5f 63 6c 65 61 6e 75 70 5f 32 3b 0a 20  ere_cleanup_2;. 
2531a 20 7d 0a 0a 20 20 2f 2a 20 57 65 20 6f 6e 6c 79   }..  /* We only
2531b 20 6e 65 65 64 20 74 6f 20 67 65 6e 65 72 61 74   need to generat
2531c 65 20 61 20 73 65 6c 65 63 74 20 65 78 70 72 65  e a select expre
2531d 73 73 69 6f 6e 20 69 66 20 74 68 65 72 65 0a 20  ssion if there. 
2531e 20 2a 2a 20 69 73 20 61 20 6c 69 6d 69 74 2f 6f   ** is a limit/o
2531f 66 66 73 65 74 20 74 65 72 6d 20 74 6f 20 65 6e  ffset term to en
25320 66 6f 72 63 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  force..  */.  if
25321 28 20 70 4c 69 6d 69 74 20 3d 3d 20 30 20 29 20  ( pLimit == 0 ) 
25322 7b 0a 20 20 20 20 2f 2a 20 69 66 20 70 4c 69 6d  {.    /* if pLim
25323 69 74 20 69 73 20 6e 75 6c 6c 2c 20 70 4f 66 66  it is null, pOff
25324 73 65 74 20 77 69 6c 6c 20 61 6c 77 61 79 73 20  set will always 
25325 62 65 20 6e 75 6c 6c 20 61 73 20 77 65 6c 6c 2e  be null as well.
25326 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
25327 70 4f 66 66 73 65 74 20 3d 3d 20 30 20 29 3b 0a  pOffset == 0 );.
25328 20 20 20 20 72 65 74 75 72 6e 20 70 57 68 65 72      return pWher
25329 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e  e;.  }..  /* Gen
2532a 65 72 61 74 65 20 61 20 73 65 6c 65 63 74 20 65  erate a select e
2532b 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 74  xpression tree t
2532c 6f 20 65 6e 66 6f 72 63 65 20 74 68 65 20 6c 69  o enforce the li
2532d 6d 69 74 2f 6f 66 66 73 65 74 20 0a 20 20 2a 2a  mit/offset .  **
2532e 20 74 65 72 6d 20 66 6f 72 20 74 68 65 20 44 45   term for the DE
2532f 4c 45 54 45 20 6f 72 20 55 50 44 41 54 45 20 73  LETE or UPDATE s
25330 74 61 74 65 6d 65 6e 74 2e 20 20 46 6f 72 20 65  tatement.  For e
25331 78 61 6d 70 6c 65 3a 0a 20 20 2a 2a 20 20 20 44  xample:.  **   D
25332 45 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c 65  ELETE FROM table
25333 5f 61 20 57 48 45 52 45 20 63 6f 6c 31 3d 31 20  _a WHERE col1=1 
25334 4f 52 44 45 52 20 42 59 20 63 6f 6c 32 20 4c 49  ORDER BY col2 LI
25335 4d 49 54 20 31 20 4f 46 46 53 45 54 20 31 0a 20  MIT 1 OFFSET 1. 
25336 20 2a 2a 20 62 65 63 6f 6d 65 73 3a 0a 20 20 2a   ** becomes:.  *
25337 2a 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20  *   DELETE FROM 
25338 74 61 62 6c 65 5f 61 20 57 48 45 52 45 20 72 6f  table_a WHERE ro
25339 77 69 64 20 49 4e 20 28 20 0a 20 20 2a 2a 20 20  wid IN ( .  **  
2533a 20 20 20 53 45 4c 45 43 54 20 72 6f 77 69 64 20     SELECT rowid 
2533b 46 52 4f 4d 20 74 61 62 6c 65 5f 61 20 57 48 45  FROM table_a WHE
2533c 52 45 20 63 6f 6c 31 3d 31 20 4f 52 44 45 52 20  RE col1=1 ORDER 
2533d 42 59 20 63 6f 6c 32 20 4c 49 4d 49 54 20 31 20  BY col2 LIMIT 1 
2533e 4f 46 46 53 45 54 20 31 0a 20 20 2a 2a 20 20 20  OFFSET 1.  **   
2533f 29 3b 0a 20 20 2a 2f 0a 0a 20 20 70 53 65 6c 65  );.  */..  pSele
25340 63 74 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65  ctRowid = sqlite
25341 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
25342 4b 5f 52 4f 57 2c 20 30 2c 20 30 2c 20 30 29 3b  K_ROW, 0, 0, 0);
25343 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 52 6f  .  if( pSelectRo
25344 77 69 64 20 3d 3d 20 30 20 29 20 67 6f 74 6f 20  wid == 0 ) goto 
25345 6c 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c 65 61  limit_where_clea
25346 6e 75 70 5f 32 3b 0a 20 20 70 45 4c 69 73 74 20  nup_2;.  pEList 
25347 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
25348 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20  tAppend(pParse, 
25349 30 2c 20 70 53 65 6c 65 63 74 52 6f 77 69 64 29  0, pSelectRowid)
2534a 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74 20 3d  ;.  if( pEList =
2534b 3d 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d 69 74  = 0 ) goto limit
2534c 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f 32  _where_cleanup_2
2534d 3b 0a 0a 20 20 2f 2a 20 64 75 70 6c 69 63 61 74  ;..  /* duplicat
2534e 65 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  e the FROM claus
2534f 65 20 61 73 20 69 74 20 69 73 20 6e 65 65 64 65  e as it is neede
25350 64 20 62 79 20 62 6f 74 68 20 74 68 65 20 44 45  d by both the DE
25351 4c 45 54 45 2f 55 50 44 41 54 45 20 74 72 65 65  LETE/UPDATE tree
25352 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 53 45  .  ** and the SE
25353 4c 45 43 54 20 73 75 62 74 72 65 65 2e 20 2a 2f  LECT subtree. */
25354 0a 20 20 70 53 65 6c 65 63 74 53 72 63 20 3d 20  .  pSelectSrc = 
25355 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75  sqlite3SrcListDu
25356 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 53  p(pParse->db, pS
25357 72 63 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 53  rc, 0);.  if( pS
25358 65 6c 65 63 74 53 72 63 20 3d 3d 20 30 20 29 20  electSrc == 0 ) 
25359 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  {.    sqlite3Exp
2535a 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72  rListDelete(pPar
2535b 73 65 2d 3e 64 62 2c 20 70 45 4c 69 73 74 29 3b  se->db, pEList);
2535c 0a 20 20 20 20 67 6f 74 6f 20 6c 69 6d 69 74 5f  .    goto limit_
2535d 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f 32 3b  where_cleanup_2;
2535e 0a 20 20 7d 0a 0a 20 20 2f 2a 20 67 65 6e 65 72  .  }..  /* gener
2535f 61 74 65 20 74 68 65 20 53 45 4c 45 43 54 20 65  ate the SELECT e
25360 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20  xpression tree. 
25361 2a 2f 0a 20 20 70 53 65 6c 65 63 74 20 3d 20 73  */.  pSelect = s
25362 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28  qlite3SelectNew(
25363 70 50 61 72 73 65 2c 70 45 4c 69 73 74 2c 70 53  pParse,pEList,pS
25364 65 6c 65 63 74 53 72 63 2c 70 57 68 65 72 65 2c  electSrc,pWhere,
25365 30 2c 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20  0,0,.           
25366 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25367 20 20 70 4f 72 64 65 72 42 79 2c 30 2c 70 4c 69    pOrderBy,0,pLi
25368 6d 69 74 2c 70 4f 66 66 73 65 74 29 3b 0a 20 20  mit,pOffset);.  
25369 69 66 28 20 70 53 65 6c 65 63 74 20 3d 3d 20 30  if( pSelect == 0
2536a 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20   ) return 0;..  
2536b 2f 2a 20 6e 6f 77 20 67 65 6e 65 72 61 74 65 20  /* now generate 
2536c 74 68 65 20 6e 65 77 20 57 48 45 52 45 20 72 6f  the new WHERE ro
2536d 77 69 64 20 49 4e 20 63 6c 61 75 73 65 20 66 6f  wid IN clause fo
2536e 72 20 74 68 65 20 44 45 4c 45 54 45 2f 55 44 50  r the DELETE/UDP
2536f 41 54 45 20 2a 2f 0a 20 20 70 57 68 65 72 65 52  ATE */.  pWhereR
25370 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33 50 45  owid = sqlite3PE
25371 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 52  xpr(pParse, TK_R
25372 4f 57 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20  OW, 0, 0, 0);.  
25373 69 66 28 20 70 57 68 65 72 65 52 6f 77 69 64 20  if( pWhereRowid 
25374 3d 3d 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d 69  == 0 ) goto limi
25375 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f  t_where_cleanup_
25376 31 3b 0a 20 20 70 49 6e 43 6c 61 75 73 65 20 3d  1;.  pInClause =
25377 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
25378 61 72 73 65 2c 20 54 4b 5f 49 4e 2c 20 70 57 68  arse, TK_IN, pWh
25379 65 72 65 52 6f 77 69 64 2c 20 30 2c 20 30 29 3b  ereRowid, 0, 0);
2537a 0a 20 20 69 66 28 20 70 49 6e 43 6c 61 75 73 65  .  if( pInClause
2537b 20 3d 3d 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d   == 0 ) goto lim
2537c 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70  it_where_cleanup
2537d 5f 31 3b 0a 0a 20 20 70 49 6e 43 6c 61 75 73 65  _1;..  pInClause
2537e 2d 3e 78 2e 70 53 65 6c 65 63 74 20 3d 20 70 53  ->x.pSelect = pS
2537f 65 6c 65 63 74 3b 0a 20 20 70 49 6e 43 6c 61 75  elect;.  pInClau
25380 73 65 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f  se->flags |= EP_
25381 78 49 73 53 65 6c 65 63 74 3b 0a 20 20 73 71 6c  xIsSelect;.  sql
25382 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68  ite3ExprSetHeigh
25383 74 28 70 50 61 72 73 65 2c 20 70 49 6e 43 6c 61  t(pParse, pInCla
25384 75 73 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  use);.  return p
25385 49 6e 43 6c 61 75 73 65 3b 0a 0a 20 20 2f 2a 20  InClause;..  /* 
25386 73 6f 6d 65 74 68 69 6e 67 20 77 65 6e 74 20 77  something went w
25387 72 6f 6e 67 2e 20 63 6c 65 61 6e 20 75 70 20 61  rong. clean up a
25388 6e 79 74 68 69 6e 67 20 61 6c 6c 6f 63 61 74 65  nything allocate
25389 64 2e 20 2a 2f 0a 6c 69 6d 69 74 5f 77 68 65 72  d. */.limit_wher
2538a 65 5f 63 6c 65 61 6e 75 70 5f 31 3a 0a 20 20 73  e_cleanup_1:.  s
2538b 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
2538c 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  te(pParse->db, p
2538d 53 65 6c 65 63 74 29 3b 0a 20 20 72 65 74 75 72  Select);.  retur
2538e 6e 20 30 3b 0a 0a 6c 69 6d 69 74 5f 77 68 65 72  n 0;..limit_wher
2538f 65 5f 63 6c 65 61 6e 75 70 5f 32 3a 0a 20 20 73  e_cleanup_2:.  s
25390 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
25391 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 57 68  (pParse->db, pWh
25392 65 72 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  ere);.  sqlite3E
25393 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50  xprListDelete(pP
25394 61 72 73 65 2d 3e 64 62 2c 20 70 4f 72 64 65 72  arse->db, pOrder
25395 42 79 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  By);.  sqlite3Ex
25396 70 72 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d  prDelete(pParse-
25397 3e 64 62 2c 20 70 4c 69 6d 69 74 29 3b 0a 20 20  >db, pLimit);.  
25398 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
25399 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4f  e(pParse->db, pO
2539a 66 66 73 65 74 29 3b 0a 20 20 72 65 74 75 72 6e  ffset);.  return
2539b 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20   0;.}.#endif /* 
2539c 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
2539d 4e 41 42 4c 45 5f 55 50 44 41 54 45 5f 44 45 4c  NABLE_UPDATE_DEL
2539e 45 54 45 5f 4c 49 4d 49 54 29 20 26 26 20 21 64  ETE_LIMIT) && !d
2539f 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
253a0 49 54 5f 53 55 42 51 55 45 52 59 29 20 2a 2f 0a  IT_SUBQUERY) */.
253a1 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
253a2 63 6f 64 65 20 66 6f 72 20 61 20 44 45 4c 45 54  code for a DELET
253a3 45 20 46 52 4f 4d 20 73 74 61 74 65 6d 65 6e 74  E FROM statement
253a4 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 45 4c 45  ..**.**     DELE
253a5 54 45 20 46 52 4f 4d 20 74 61 62 6c 65 5f 77 78  TE FROM table_wx
253a6 79 7a 20 57 48 45 52 45 20 61 3c 35 20 41 4e 44  yz WHERE a<5 AND
253a7 20 62 20 4e 4f 54 20 4e 55 4c 4c 3b 0a 2a 2a 20   b NOT NULL;.** 
253a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
253a9 5c 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 20  \________/      
253aa 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f   \______________
253ab 5f 5f 2f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  __/.**          
253ac 20 20 20 20 20 20 20 20 70 54 61 62 4c 69 73 74          pTabList
253ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 57                pW
253ae 68 65 72 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  here.*/.SQLITE_P
253af 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
253b0 74 65 33 44 65 6c 65 74 65 46 72 6f 6d 28 0a 20  te3DeleteFrom(. 
253b1 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
253b2 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
253b3 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
253b4 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62  .  SrcList *pTab
253b5 4c 69 73 74 2c 20 20 20 20 20 2f 2a 20 54 68 65  List,     /* The
253b6 20 74 61 62 6c 65 20 66 72 6f 6d 20 77 68 69 63   table from whic
253b7 68 20 77 65 20 73 68 6f 75 6c 64 20 64 65 6c 65  h we should dele
253b8 74 65 20 74 68 69 6e 67 73 20 2a 2f 0a 20 20 45  te things */.  E
253b9 78 70 72 20 2a 70 57 68 65 72 65 20 20 20 20 20  xpr *pWhere     
253ba 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45        /* The WHE
253bb 52 45 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20  RE clause.  May 
253bc 62 65 20 6e 75 6c 6c 20 2a 2f 0a 29 7b 0a 20 20  be null */.){.  
253bd 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20  Vdbe *v;        
253be 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69         /* The vi
253bf 72 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 65  rtual database e
253c0 6e 67 69 6e 65 20 2a 2f 0a 20 20 54 61 62 6c 65  ngine */.  Table
253c1 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20   *pTab;         
253c2 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 66    /* The table f
253c3 72 6f 6d 20 77 68 69 63 68 20 72 65 63 6f 72 64  rom which record
253c4 73 20 77 69 6c 6c 20 62 65 20 64 65 6c 65 74 65  s will be delete
253c5 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  d */.  const cha
253c6 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 20 2f 2a  r *zDb;       /*
253c7 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73   Name of databas
253c8 65 20 68 6f 6c 64 69 6e 67 20 70 54 61 62 20 2a  e holding pTab *
253c9 2f 0a 20 20 69 6e 74 20 65 6e 64 2c 20 61 64 64  /.  int end, add
253ca 72 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 41 20  r = 0;     /* A 
253cb 63 6f 75 70 6c 65 20 61 64 64 72 65 73 73 65 73  couple addresses
253cc 20 6f 66 20 67 65 6e 65 72 61 74 65 64 20 63 6f   of generated co
253cd 64 65 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20  de */.  int i;  
253ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
253cf 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
253d0 2f 0a 20 20 57 68 65 72 65 49 6e 66 6f 20 2a 70  /.  WhereInfo *p
253d1 57 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20 49 6e  WInfo;     /* In
253d2 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
253d3 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
253d4 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64   */.  Index *pId
253d5 78 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  x;           /* 
253d6 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72  For looping over
253d7 20 69 6e 64 69 63 65 73 20 6f 66 20 74 68 65 20   indices of the 
253d8 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69  table */.  int i
253d9 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  Cur;            
253da 20 20 2f 2a 20 56 44 42 45 20 43 75 72 73 6f 72    /* VDBE Cursor
253db 20 6e 75 6d 62 65 72 20 66 6f 72 20 70 54 61 62   number for pTab
253dc 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
253dd 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  b;           /* 
253de 4d 61 69 6e 20 64 61 74 61 62 61 73 65 20 73 74  Main database st
253df 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 41 75 74  ructure */.  Aut
253e0 68 43 6f 6e 74 65 78 74 20 73 43 6f 6e 74 65 78  hContext sContex
253e1 74 3b 20 20 2f 2a 20 41 75 74 68 6f 72 69 7a 61  t;  /* Authoriza
253e2 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  tion context */.
253e3 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e    NameContext sN
253e4 43 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  C;       /* Name
253e5 20 63 6f 6e 74 65 78 74 20 74 6f 20 72 65 73 6f   context to reso
253e6 6c 76 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20  lve expressions 
253e7 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b  in */.  int iDb;
253e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
253e9 2a 20 44 61 74 61 62 61 73 65 20 6e 75 6d 62 65  * Database numbe
253ea 72 20 2a 2f 0a 20 20 69 6e 74 20 6d 65 6d 43 6e  r */.  int memCn
253eb 74 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 2f 2a  t = -1;       /*
253ec 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73 65   Memory cell use
253ed 64 20 66 6f 72 20 63 68 61 6e 67 65 20 63 6f 75  d for change cou
253ee 6e 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 72  nting */.  int r
253ef 63 61 75 74 68 3b 20 20 20 20 20 20 20 20 20 20  cauth;          
253f0 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 74 75 72    /* Value retur
253f1 6e 65 64 20 62 79 20 61 75 74 68 6f 72 69 7a 61  ned by authoriza
253f2 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 20 2a 2f  tion callback */
253f3 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
253f4 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20  _OMIT_TRIGGER.  
253f5 69 6e 74 20 69 73 56 69 65 77 3b 20 20 20 20 20  int isView;     
253f6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
253f7 54 72 75 65 20 69 66 20 61 74 74 65 6d 70 74 69  True if attempti
253f8 6e 67 20 74 6f 20 64 65 6c 65 74 65 20 66 72 6f  ng to delete fro
253f9 6d 20 61 20 76 69 65 77 20 2a 2f 0a 20 20 54 72  m a view */.  Tr
253fa 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b  igger *pTrigger;
253fb 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69             /* Li
253fc 73 74 20 6f 66 20 74 61 62 6c 65 20 74 72 69 67  st of table trig
253fd 67 65 72 73 2c 20 69 66 20 72 65 71 75 69 72 65  gers, if require
253fe 64 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 6d  d */.#endif..  m
253ff 65 6d 73 65 74 28 26 73 43 6f 6e 74 65 78 74 2c  emset(&sContext,
25400 20 30 2c 20 73 69 7a 65 6f 66 28 73 43 6f 6e 74   0, sizeof(sCont
25401 65 78 74 29 29 3b 0a 20 20 64 62 20 3d 20 70 50  ext));.  db = pP
25402 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20  arse->db;.  if( 
25403 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20  pParse->nErr || 
25404 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
25405 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c   ){.    goto del
25406 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70  ete_from_cleanup
25407 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
25408 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d  pTabList->nSrc==
25409 31 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74  1 );..  /* Locat
2540a 65 20 74 68 65 20 74 61 62 6c 65 20 77 68 69 63  e the table whic
2540b 68 20 77 65 20 77 61 6e 74 20 74 6f 20 64 65 6c  h we want to del
2540c 65 74 65 2e 20 20 54 68 69 73 20 74 61 62 6c 65  ete.  This table
2540d 20 68 61 73 20 74 6f 20 62 65 0a 20 20 2a 2a 20   has to be.  ** 
2540e 70 75 74 20 69 6e 20 61 6e 20 53 72 63 4c 69 73  put in an SrcLis
2540f 74 20 73 74 72 75 63 74 75 72 65 20 62 65 63 61  t structure beca
25410 75 73 65 20 73 6f 6d 65 20 6f 66 20 74 68 65 20  use some of the 
25411 73 75 62 72 6f 75 74 69 6e 65 73 20 77 65 0a 20  subroutines we. 
25412 20 2a 2a 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c   ** will be call
25413 69 6e 67 20 61 72 65 20 64 65 73 69 67 6e 65 64  ing are designed
25414 20 74 6f 20 77 6f 72 6b 20 77 69 74 68 20 6d 75   to work with mu
25415 6c 74 69 70 6c 65 20 74 61 62 6c 65 73 20 61 6e  ltiple tables an
25416 64 20 65 78 70 65 63 74 0a 20 20 2a 2a 20 61 6e  d expect.  ** an
25417 20 53 72 63 4c 69 73 74 2a 20 70 61 72 61 6d 65   SrcList* parame
25418 74 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 6a  ter instead of j
25419 75 73 74 20 61 20 54 61 62 6c 65 2a 20 70 61 72  ust a Table* par
2541a 61 6d 65 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 70  ameter..  */.  p
2541b 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53 72 63  Tab = sqlite3Src
2541c 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73  ListLookup(pPars
2541d 65 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a 20 20  e, pTabList);.  
2541e 69 66 28 20 70 54 61 62 3d 3d 30 20 29 20 20 67  if( pTab==0 )  g
2541f 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f  oto delete_from_
25420 63 6c 65 61 6e 75 70 3b 0a 0a 20 20 2f 2a 20 46  cleanup;..  /* F
25421 69 67 75 72 65 20 6f 75 74 20 69 66 20 77 65 20  igure out if we 
25422 68 61 76 65 20 61 6e 79 20 74 72 69 67 67 65 72  have any trigger
25423 73 20 61 6e 64 20 69 66 20 74 68 65 20 74 61 62  s and if the tab
25424 6c 65 20 62 65 69 6e 67 0a 20 20 2a 2a 20 64 65  le being.  ** de
25425 6c 65 74 65 64 20 66 72 6f 6d 20 69 73 20 61 20  leted from is a 
25426 76 69 65 77 0a 20 20 2a 2f 0a 23 69 66 6e 64 65  view.  */.#ifnde
25427 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
25428 49 47 47 45 52 0a 20 20 70 54 72 69 67 67 65 72  IGGER.  pTrigger
25429 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65   = sqlite3Trigge
2542a 72 73 45 78 69 73 74 28 70 50 61 72 73 65 2c 20  rsExist(pParse, 
2542b 70 54 61 62 2c 20 54 4b 5f 44 45 4c 45 54 45 2c  pTab, TK_DELETE,
2542c 20 30 2c 20 30 29 3b 0a 20 20 69 73 56 69 65 77   0, 0);.  isView
2542d 20 3d 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74   = pTab->pSelect
2542e 21 3d 30 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66  !=0;.#else.# def
2542f 69 6e 65 20 70 54 72 69 67 67 65 72 20 30 0a 23  ine pTrigger 0.#
25430 20 64 65 66 69 6e 65 20 69 73 56 69 65 77 20 30   define isView 0
25431 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
25432 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a  QLITE_OMIT_VIEW.
25433 23 20 75 6e 64 65 66 20 69 73 56 69 65 77 0a 23  # undef isView.#
25434 20 64 65 66 69 6e 65 20 69 73 56 69 65 77 20 30   define isView 0
25435 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66  .#endif..  /* If
25436 20 70 54 61 62 20 69 73 20 72 65 61 6c 6c 79 20   pTab is really 
25437 61 20 76 69 65 77 2c 20 6d 61 6b 65 20 73 75 72  a view, make sur
25438 65 20 69 74 20 68 61 73 20 62 65 65 6e 20 69 6e  e it has been in
25439 69 74 69 61 6c 69 7a 65 64 2e 0a 20 20 2a 2f 0a  itialized..  */.
2543a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 69 65    if( sqlite3Vie
2543b 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28  wGetColumnNames(
2543c 70 50 61 72 73 65 2c 20 70 54 61 62 29 20 29 7b  pParse, pTab) ){
2543d 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c 65 74 65  .    goto delete
2543e 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20  _from_cleanup;. 
2543f 20 7d 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65   }..  if( sqlite
25440 33 49 73 52 65 61 64 4f 6e 6c 79 28 70 50 61 72  3IsReadOnly(pPar
25441 73 65 2c 20 70 54 61 62 2c 20 28 70 54 72 69 67  se, pTab, (pTrig
25442 67 65 72 3f 31 3a 30 29 29 20 29 7b 0a 20 20 20  ger?1:0)) ){.   
25443 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f   goto delete_fro
25444 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20  m_cleanup;.  }. 
25445 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
25446 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20  hemaToIndex(db, 
25447 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  pTab->pSchema);.
25448 20 20 61 73 73 65 72 74 28 20 69 44 62 3c 64 62    assert( iDb<db
25449 2d 3e 6e 44 62 20 29 3b 0a 20 20 7a 44 62 20 3d  ->nDb );.  zDb =
2544a 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
2544b 61 6d 65 3b 0a 20 20 72 63 61 75 74 68 20 3d 20  ame;.  rcauth = 
2544c 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
2544d 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f  (pParse, SQLITE_
2544e 44 45 4c 45 54 45 2c 20 70 54 61 62 2d 3e 7a 4e  DELETE, pTab->zN
2544f 61 6d 65 2c 20 30 2c 20 7a 44 62 29 3b 0a 20 20  ame, 0, zDb);.  
25450 61 73 73 65 72 74 28 20 72 63 61 75 74 68 3d 3d  assert( rcauth==
25451 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 61  SQLITE_OK || rca
25452 75 74 68 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59  uth==SQLITE_DENY
25453 20 7c 7c 20 72 63 61 75 74 68 3d 3d 53 51 4c 49   || rcauth==SQLI
25454 54 45 5f 49 47 4e 4f 52 45 20 29 3b 0a 20 20 69  TE_IGNORE );.  i
25455 66 28 20 72 63 61 75 74 68 3d 3d 53 51 4c 49 54  f( rcauth==SQLIT
25456 45 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20 67 6f  E_DENY ){.    go
25457 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63  to delete_from_c
25458 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 61 73  leanup;.  }.  as
25459 73 65 72 74 28 21 69 73 56 69 65 77 20 7c 7c 20  sert(!isView || 
2545a 70 54 72 69 67 67 65 72 29 3b 0a 0a 20 20 2f 2a  pTrigger);..  /*
2545b 20 41 73 73 69 67 6e 20 20 63 75 72 73 6f 72 20   Assign  cursor 
2545c 6e 75 6d 62 65 72 20 74 6f 20 74 68 65 20 74 61  number to the ta
2545d 62 6c 65 20 61 6e 64 20 61 6c 6c 20 69 74 73 20  ble and all its 
2545e 69 6e 64 69 63 65 73 2e 0a 20 20 2a 2f 0a 20 20  indices..  */.  
2545f 61 73 73 65 72 74 28 20 70 54 61 62 4c 69 73 74  assert( pTabList
25460 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 69  ->nSrc==1 );.  i
25461 43 75 72 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e  Cur = pTabList->
25462 61 5b 30 5d 2e 69 43 75 72 73 6f 72 20 3d 20 70  a[0].iCursor = p
25463 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20  Parse->nTab++;. 
25464 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e   for(pIdx=pTab->
25465 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49  pIndex; pIdx; pI
25466 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b  dx=pIdx->pNext){
25467 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 54 61  .    pParse->nTa
25468 62 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53  b++;.  }..  /* S
25469 74 61 72 74 20 74 68 65 20 76 69 65 77 20 63 6f  tart the view co
2546a 6e 74 65 78 74 0a 20 20 2a 2f 0a 20 20 69 66 28  ntext.  */.  if(
2546b 20 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 73   isView ){.    s
2546c 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78  qlite3AuthContex
2546d 74 50 75 73 68 28 70 50 61 72 73 65 2c 20 26 73  tPush(pParse, &s
2546e 43 6f 6e 74 65 78 74 2c 20 70 54 61 62 2d 3e 7a  Context, pTab->z
2546f 4e 61 6d 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  Name);.  }..  /*
25470 20 42 65 67 69 6e 20 67 65 6e 65 72 61 74 69 6e   Begin generatin
25471 67 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 76  g code..  */.  v
25472 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
25473 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28  e(pParse);.  if(
25474 20 76 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74   v==0 ){.    got
25475 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c  o delete_from_cl
25476 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 66 28  eanup;.  }.  if(
25477 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d   pParse->nested=
25478 3d 30 20 29 20 73 71 6c 69 74 65 33 56 64 62 65  =0 ) sqlite3Vdbe
25479 43 6f 75 6e 74 43 68 61 6e 67 65 73 28 76 29 3b  CountChanges(v);
2547a 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57  .  sqlite3BeginW
2547b 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50  riteOperation(pP
2547c 61 72 73 65 2c 20 28 70 54 72 69 67 67 65 72 3f  arse, (pTrigger?
2547d 31 3a 30 29 2c 20 69 44 62 29 3b 0a 0a 20 20 2f  1:0), iDb);..  /
2547e 2a 20 49 66 20 77 65 20 61 72 65 20 74 72 79 69  * If we are tryi
2547f 6e 67 20 74 6f 20 64 65 6c 65 74 65 20 66 72 6f  ng to delete fro
25480 6d 20 61 20 76 69 65 77 2c 20 72 65 61 6c 69 7a  m a view, realiz
25481 65 20 74 68 61 74 20 76 69 65 77 20 69 6e 74 6f  e that view into
25482 0a 20 20 2a 2a 20 61 20 65 70 68 65 6d 65 72 61  .  ** a ephemera
25483 6c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 23 69  l table..  */.#i
25484 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
25485 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 26 26 20  E_OMIT_VIEW) && 
25486 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
25487 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a 20 20  OMIT_TRIGGER).  
25488 69 66 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20  if( isView ){.  
25489 20 20 73 71 6c 69 74 65 33 4d 61 74 65 72 69 61    sqlite3Materia
2548a 6c 69 7a 65 56 69 65 77 28 70 50 61 72 73 65 2c  lizeView(pParse,
2548b 20 70 54 61 62 2c 20 70 57 68 65 72 65 2c 20 69   pTab, pWhere, i
2548c 43 75 72 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  Cur);.  }.#endif
2548d 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74  ..  /* Resolve t
2548e 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  he column names 
2548f 69 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  in the WHERE cla
25490 75 73 65 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73  use..  */.  mems
25491 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65  et(&sNC, 0, size
25492 6f 66 28 73 4e 43 29 29 3b 0a 20 20 73 4e 43 2e  of(sNC));.  sNC.
25493 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b  pParse = pParse;
25494 0a 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74 20  .  sNC.pSrcList 
25495 3d 20 70 54 61 62 4c 69 73 74 3b 0a 20 20 69 66  = pTabList;.  if
25496 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65  ( sqlite3Resolve
25497 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20  ExprNames(&sNC, 
25498 70 57 68 65 72 65 29 20 29 7b 0a 20 20 20 20 67  pWhere) ){.    g
25499 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f  oto delete_from_
2549a 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20  cleanup;.  }..  
2549b 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68  /* Initialize th
2549c 65 20 63 6f 75 6e 74 65 72 20 6f 66 20 74 68 65  e counter of the
2549d 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20   number of rows 
2549e 64 65 6c 65 74 65 64 2c 20 69 66 0a 20 20 2a 2a  deleted, if.  **
2549f 20 77 65 20 61 72 65 20 63 6f 75 6e 74 69 6e 67   we are counting
254a0 20 72 6f 77 73 2e 0a 20 20 2a 2f 0a 20 20 69 66   rows..  */.  if
254a1 28 20 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51  ( db->flags & SQ
254a2 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 29  LITE_CountRows )
254a3 7b 0a 20 20 20 20 6d 65 6d 43 6e 74 20 3d 20 2b  {.    memCnt = +
254a4 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
254a5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
254a6 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
254a7 65 72 2c 20 30 2c 20 6d 65 6d 43 6e 74 29 3b 0a  er, 0, memCnt);.
254a8 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c    }..#ifndef SQL
254a9 49 54 45 5f 4f 4d 49 54 5f 54 52 55 4e 43 41 54  ITE_OMIT_TRUNCAT
254aa 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 0a 20  E_OPTIMIZATION. 
254ab 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73 65   /* Special case
254ac 3a 20 41 20 44 45 4c 45 54 45 20 77 69 74 68 6f  : A DELETE witho
254ad 75 74 20 61 20 57 48 45 52 45 20 63 6c 61 75 73  ut a WHERE claus
254ae 65 20 64 65 6c 65 74 65 73 20 65 76 65 72 79 74  e deletes everyt
254af 68 69 6e 67 2e 0a 20 20 2a 2a 20 49 74 20 69 73  hing..  ** It is
254b0 20 65 61 73 69 65 72 20 6a 75 73 74 20 74 6f 20   easier just to 
254b1 65 72 61 73 65 20 74 68 65 20 77 68 6f 6c 65 20  erase the whole 
254b2 74 61 62 6c 65 2e 20 50 72 69 6f 72 20 74 6f 20  table. Prior to 
254b3 76 65 72 73 69 6f 6e 20 33 2e 36 2e 35 2c 0a 20  version 3.6.5,. 
254b4 20 2a 2a 20 74 68 69 73 20 6f 70 74 69 6d 69 7a   ** this optimiz
254b5 61 74 69 6f 6e 20 63 61 75 73 65 64 20 74 68 65  ation caused the
254b6 20 72 6f 77 20 63 68 61 6e 67 65 20 63 6f 75 6e   row change coun
254b7 74 20 28 74 68 65 20 76 61 6c 75 65 20 72 65 74  t (the value ret
254b8 75 72 6e 65 64 20 62 79 20 0a 20 20 2a 2a 20 41  urned by .  ** A
254b9 50 49 20 66 75 6e 63 74 69 6f 6e 20 73 71 6c 69  PI function sqli
254ba 74 65 33 5f 63 6f 75 6e 74 5f 63 68 61 6e 67 65  te3_count_change
254bb 73 29 20 74 6f 20 62 65 20 73 65 74 20 69 6e 63  s) to be set inc
254bc 6f 72 72 65 63 74 6c 79 2e 20 20 2a 2f 0a 20 20  orrectly.  */.  
254bd 69 66 28 20 72 63 61 75 74 68 3d 3d 53 51 4c 49  if( rcauth==SQLI
254be 54 45 5f 4f 4b 20 26 26 20 70 57 68 65 72 65 3d  TE_OK && pWhere=
254bf 3d 30 20 26 26 20 21 70 54 72 69 67 67 65 72 20  =0 && !pTrigger 
254c0 26 26 20 21 49 73 56 69 72 74 75 61 6c 28 70 54  && !IsVirtual(pT
254c1 61 62 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ab) ){.    asser
254c2 74 28 20 21 69 73 56 69 65 77 20 29 3b 0a 20 20  t( !isView );.  
254c3 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
254c4 4f 70 34 28 76 2c 20 4f 50 5f 43 6c 65 61 72 2c  Op4(v, OP_Clear,
254c5 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 69 44 62   pTab->tnum, iDb
254c6 2c 20 6d 65 6d 43 6e 74 2c 0a 20 20 20 20 20 20  , memCnt,.      
254c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
254c8 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f  pTab->zName, P4_
254c9 53 54 41 54 49 43 29 3b 0a 20 20 20 20 66 6f 72  STATIC);.    for
254ca 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64  (pIdx=pTab->pInd
254cb 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70  ex; pIdx; pIdx=p
254cc 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  Idx->pNext){.   
254cd 20 20 20 61 73 73 65 72 74 28 20 70 49 64 78 2d     assert( pIdx-
254ce 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e  >pSchema==pTab->
254cf 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 20  pSchema );.     
254d0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
254d1 70 32 28 76 2c 20 4f 50 5f 43 6c 65 61 72 2c 20  p2(v, OP_Clear, 
254d2 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 29  pIdx->tnum, iDb)
254d3 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a  ;.    }.  }else.
254d4 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
254d5 5f 4f 4d 49 54 5f 54 52 55 4e 43 41 54 45 5f 4f  _OMIT_TRUNCATE_O
254d6 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a 20  PTIMIZATION */. 
254d7 20 2f 2a 20 54 68 65 20 75 73 75 61 6c 20 63 61   /* The usual ca
254d8 73 65 3a 20 54 68 65 72 65 20 69 73 20 61 20 57  se: There is a W
254d9 48 45 52 45 20 63 6c 61 75 73 65 20 73 6f 20 77  HERE clause so w
254da 65 20 68 61 76 65 20 74 6f 20 73 63 61 6e 20 74  e have to scan t
254db 68 72 6f 75 67 68 0a 20 20 2a 2a 20 74 68 65 20  hrough.  ** the 
254dc 74 61 62 6c 65 20 61 6e 64 20 70 69 63 6b 20 77  table and pick w
254dd 68 69 63 68 20 72 65 63 6f 72 64 73 20 74 6f 20  hich records to 
254de 64 65 6c 65 74 65 2e 0a 20 20 2a 2f 0a 20 20 7b  delete..  */.  {
254df 0a 20 20 20 20 69 6e 74 20 69 52 6f 77 53 65 74  .    int iRowSet
254e0 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
254e1 6d 3b 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72  m;   /* Register
254e2 20 66 6f 72 20 72 6f 77 73 65 74 20 6f 66 20 72   for rowset of r
254e3 6f 77 73 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f  ows to delete */
254e4 0a 20 20 20 20 69 6e 74 20 69 52 6f 77 69 64 20  .    int iRowid 
254e5 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
254e6 3b 20 20 20 20 2f 2a 20 55 73 65 64 20 66 6f 72  ;    /* Used for
254e7 20 73 74 6f 72 69 6e 67 20 72 6f 77 69 64 20 76   storing rowid v
254e8 61 6c 75 65 73 2e 20 2a 2f 0a 20 20 20 20 69 6e  alues. */.    in
254e9 74 20 72 65 67 52 6f 77 69 64 3b 20 20 20 20 20  t regRowid;     
254ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
254eb 20 41 63 74 75 61 6c 20 72 65 67 69 73 74 65 72   Actual register
254ec 20 63 6f 6e 74 61 69 6e 69 6e 67 20 72 6f 77 69   containing rowi
254ed 64 73 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 43 6f  ds */..    /* Co
254ee 6c 6c 65 63 74 20 72 6f 77 69 64 73 20 6f 66 20  llect rowids of 
254ef 65 76 65 72 79 20 72 6f 77 20 74 6f 20 62 65 20  every row to be 
254f0 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a 2f 0a  deleted..    */.
254f1 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
254f2 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp2(v, OP_Null
254f3 2c 20 30 2c 20 69 52 6f 77 53 65 74 29 3b 0a 20  , 0, iRowSet);. 
254f4 20 20 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69     pWInfo = sqli
254f5 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 70 50  te3WhereBegin(pP
254f6 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20  arse, pTabList, 
254f7 70 57 68 65 72 65 2c 30 2c 57 48 45 52 45 5f 44  pWhere,0,WHERE_D
254f8 55 50 4c 49 43 41 54 45 53 5f 4f 4b 29 3b 0a 20  UPLICATES_OK);. 
254f9 20 20 20 69 66 28 20 70 57 49 6e 66 6f 3d 3d 30     if( pWInfo==0
254fa 20 29 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66   ) goto delete_f
254fb 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20  rom_cleanup;.   
254fc 20 72 65 67 52 6f 77 69 64 20 3d 20 73 71 6c 69   regRowid = sqli
254fd 74 65 33 45 78 70 72 43 6f 64 65 47 65 74 43 6f  te3ExprCodeGetCo
254fe 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20 70 54 61  lumn(pParse, pTa
254ff 62 2c 20 2d 31 2c 20 69 43 75 72 2c 20 69 52 6f  b, -1, iCur, iRo
25500 77 69 64 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  wid, 0);.    sql
25501 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
25502 2c 20 4f 50 5f 52 6f 77 53 65 74 41 64 64 2c 20  , OP_RowSetAdd, 
25503 69 52 6f 77 53 65 74 2c 20 72 65 67 52 6f 77 69  iRowSet, regRowi
25504 64 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e  d);.    if( db->
25505 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 43  flags & SQLITE_C
25506 6f 75 6e 74 52 6f 77 73 20 29 7b 0a 20 20 20 20  ountRows ){.    
25507 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
25508 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d  Op2(v, OP_AddImm
25509 2c 20 6d 65 6d 43 6e 74 2c 20 31 29 3b 0a 20 20  , memCnt, 1);.  
2550a 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 57    }.    sqlite3W
2550b 68 65 72 65 45 6e 64 28 70 57 49 6e 66 6f 29 3b  hereEnd(pWInfo);
2550c 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65 74 65 20  ..    /* Delete 
2550d 65 76 65 72 79 20 69 74 65 6d 20 77 68 6f 73 65  every item whose
2550e 20 6b 65 79 20 77 61 73 20 77 72 69 74 74 65 6e   key was written
2550f 20 74 6f 20 74 68 65 20 6c 69 73 74 20 64 75 72   to the list dur
25510 69 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a 20 64  ing the.    ** d
25511 61 74 61 62 61 73 65 20 73 63 61 6e 2e 20 20 57  atabase scan.  W
25512 65 20 68 61 76 65 20 74 6f 20 64 65 6c 65 74 65  e have to delete
25513 20 69 74 65 6d 73 20 61 66 74 65 72 20 74 68 65   items after the
25514 20 73 63 61 6e 20 69 73 20 63 6f 6d 70 6c 65 74   scan is complet
25515 65 0a 20 20 20 20 2a 2a 20 62 65 63 61 75 73 65  e.    ** because
25516 20 64 65 6c 65 74 69 6e 67 20 61 6e 20 69 74 65   deleting an ite
25517 6d 20 63 61 6e 20 63 68 61 6e 67 65 20 74 68 65  m can change the
25518 20 73 63 61 6e 20 6f 72 64 65 72 2e 20 20 2a 2f   scan order.  */
25519 0a 20 20 20 20 65 6e 64 20 3d 20 73 71 6c 69 74  .    end = sqlit
2551a 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
2551b 76 29 3b 0a 0a 20 20 20 20 2f 2a 20 55 6e 6c 65  v);..    /* Unle
2551c 73 73 20 74 68 69 73 20 69 73 20 61 20 76 69 65  ss this is a vie
2551d 77 2c 20 6f 70 65 6e 20 63 75 72 73 6f 72 73 20  w, open cursors 
2551e 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 77 65  for the table we
2551f 20 61 72 65 20 0a 20 20 20 20 2a 2a 20 64 65 6c   are .    ** del
25520 65 74 69 6e 67 20 66 72 6f 6d 20 61 6e 64 20 61  eting from and a
25521 6c 6c 20 69 74 73 20 69 6e 64 69 63 65 73 2e 20  ll its indices. 
25522 49 66 20 74 68 69 73 20 69 73 20 61 20 76 69 65  If this is a vie
25523 77 2c 20 74 68 65 6e 20 74 68 65 0a 20 20 20 20  w, then the.    
25524 2a 2a 20 6f 6e 6c 79 20 65 66 66 65 63 74 20 74  ** only effect t
25525 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 68 61  his statement ha
25526 73 20 69 73 20 74 6f 20 66 69 72 65 20 74 68 65  s is to fire the
25527 20 49 4e 53 54 45 41 44 20 4f 46 20 0a 20 20 20   INSTEAD OF .   
25528 20 2a 2a 20 74 72 69 67 67 65 72 73 2e 20 20 2a   ** triggers.  *
25529 2f 0a 20 20 20 20 69 66 28 20 21 69 73 56 69 65  /.    if( !isVie
2552a 77 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  w ){.      sqlit
2552b 65 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64 49 6e  e3OpenTableAndIn
2552c 64 69 63 65 73 28 70 50 61 72 73 65 2c 20 70 54  dices(pParse, pT
2552d 61 62 2c 20 69 43 75 72 2c 20 4f 50 5f 4f 70 65  ab, iCur, OP_Ope
2552e 6e 57 72 69 74 65 29 3b 0a 20 20 20 20 7d 0a 0a  nWrite);.    }..
2552f 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74      addr = sqlit
25530 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
25531 4f 50 5f 52 6f 77 53 65 74 52 65 61 64 2c 20 69  OP_RowSetRead, i
25532 52 6f 77 53 65 74 2c 20 65 6e 64 2c 20 69 52 6f  RowSet, end, iRo
25533 77 69 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 65  wid);..    /* De
25534 6c 65 74 65 20 74 68 65 20 72 6f 77 20 2a 2f 0a  lete the row */.
25535 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
25536 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
25537 0a 20 20 20 20 69 66 28 20 49 73 56 69 72 74 75  .    if( IsVirtu
25538 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20  al(pTab) ){.    
25539 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 56    const char *pV
2553a 54 61 62 20 3d 20 28 63 6f 6e 73 74 20 63 68 61  Tab = (const cha
2553b 72 20 2a 29 73 71 6c 69 74 65 33 47 65 74 56 54  r *)sqlite3GetVT
2553c 61 62 6c 65 28 64 62 2c 20 70 54 61 62 29 3b 0a  able(db, pTab);.
2553d 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 74 61        sqlite3Vta
2553e 62 4d 61 6b 65 57 72 69 74 61 62 6c 65 28 70 50  bMakeWritable(pP
2553f 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20  arse, pTab);.   
25540 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
25541 64 4f 70 34 28 76 2c 20 4f 50 5f 56 55 70 64 61  dOp4(v, OP_VUpda
25542 74 65 2c 20 30 2c 20 31 2c 20 69 52 6f 77 69 64  te, 0, 1, iRowid
25543 2c 20 70 56 54 61 62 2c 20 50 34 5f 56 54 41 42  , pVTab, P4_VTAB
25544 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
25545 4d 61 79 41 62 6f 72 74 28 70 50 61 72 73 65 29  MayAbort(pParse)
25546 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64  ;.    }else.#end
25547 69 66 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 69  if.    {.      i
25548 6e 74 20 63 6f 75 6e 74 20 3d 20 28 70 50 61 72  nt count = (pPar
25549 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 29 3b 20  se->nested==0); 
2554a 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 63 6f     /* True to co
2554b 75 6e 74 20 63 68 61 6e 67 65 73 20 2a 2f 0a 20  unt changes */. 
2554c 20 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65       sqlite3Gene
2554d 72 61 74 65 52 6f 77 44 65 6c 65 74 65 28 70 50  rateRowDelete(pP
2554e 61 72 73 65 2c 20 70 54 61 62 2c 20 69 43 75 72  arse, pTab, iCur
2554f 2c 20 69 52 6f 77 69 64 2c 20 63 6f 75 6e 74 2c  , iRowid, count,
25550 20 70 54 72 69 67 67 65 72 2c 20 4f 45 5f 44 65   pTrigger, OE_De
25551 66 61 75 6c 74 29 3b 0a 20 20 20 20 7d 0a 0a 20  fault);.    }.. 
25552 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 74 68 65     /* End of the
25553 20 64 65 6c 65 74 65 20 6c 6f 6f 70 20 2a 2f 0a   delete loop */.
25554 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
25555 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp2(v, OP_Goto
25556 2c 20 30 2c 20 61 64 64 72 29 3b 0a 20 20 20 20  , 0, addr);.    
25557 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c  sqlite3VdbeResol
25558 76 65 4c 61 62 65 6c 28 76 2c 20 65 6e 64 29 3b  veLabel(v, end);
25559 0a 0a 20 20 20 20 2f 2a 20 43 6c 6f 73 65 20 74  ..    /* Close t
2555a 68 65 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20  he cursors open 
2555b 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64  on the table and
2555c 20 69 74 73 20 69 6e 64 65 78 65 73 2e 20 2a 2f   its indexes. */
2555d 0a 20 20 20 20 69 66 28 20 21 69 73 56 69 65 77  .    if( !isView
2555e 20 26 26 20 21 49 73 56 69 72 74 75 61 6c 28 70   && !IsVirtual(p
2555f 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 66 6f  Tab) ){.      fo
25560 72 28 69 3d 31 2c 20 70 49 64 78 3d 70 54 61 62  r(i=1, pIdx=pTab
25561 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
25562 69 2b 2b 2c 20 70 49 64 78 3d 70 49 64 78 2d 3e  i++, pIdx=pIdx->
25563 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20  pNext){.        
25564 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
25565 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69  2(v, OP_Close, i
25566 43 75 72 20 2b 20 69 2c 20 70 49 64 78 2d 3e 74  Cur + i, pIdx->t
25567 6e 75 6d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  num);.      }.  
25568 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
25569 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp1(v, OP_Clos
2556a 65 2c 20 69 43 75 72 29 3b 0a 20 20 20 20 7d 0a  e, iCur);.    }.
2556b 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65    }..  /* Update
2556c 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75   the sqlite_sequ
2556d 65 6e 63 65 20 74 61 62 6c 65 20 62 79 20 73 74  ence table by st
2556e 6f 72 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e  oring the conten
2556f 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 6d 61  t of the.  ** ma
25570 78 69 6d 75 6d 20 72 6f 77 69 64 20 63 6f 75 6e  ximum rowid coun
25571 74 65 72 20 76 61 6c 75 65 73 20 72 65 63 6f 72  ter values recor
25572 64 65 64 20 77 68 69 6c 65 20 69 6e 73 65 72 74  ded while insert
25573 69 6e 67 20 69 6e 74 6f 0a 20 20 2a 2a 20 61 75  ing into.  ** au
25574 74 6f 69 6e 63 72 65 6d 65 6e 74 20 74 61 62 6c  toincrement tabl
25575 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  es..  */.  if( p
25576 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30  Parse->nested==0
25577 20 26 26 20 70 50 61 72 73 65 2d 3e 70 54 72 69   && pParse->pTri
25578 67 67 65 72 54 61 62 3d 3d 30 20 29 7b 0a 20 20  ggerTab==0 ){.  
25579 20 20 73 71 6c 69 74 65 33 41 75 74 6f 69 6e 63    sqlite3Autoinc
2557a 72 65 6d 65 6e 74 45 6e 64 28 70 50 61 72 73 65  rementEnd(pParse
2557b 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 74  );.  }..  /* Ret
2557c 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
2557d 66 20 72 6f 77 73 20 74 68 61 74 20 77 65 72 65  f rows that were
2557e 20 64 65 6c 65 74 65 64 2e 20 49 66 20 74 68 69   deleted. If thi
2557f 73 20 72 6f 75 74 69 6e 65 20 69 73 20 0a 20 20  s routine is .  
25580 2a 2a 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f  ** generating co
25581 64 65 20 62 65 63 61 75 73 65 20 6f 66 20 61 20  de because of a 
25582 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 4e  call to sqlite3N
25583 65 73 74 65 64 50 61 72 73 65 28 29 2c 20 64 6f  estedParse(), do
25584 20 6e 6f 74 0a 20 20 2a 2a 20 69 6e 76 6f 6b 65   not.  ** invoke
25585 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75   the callback fu
25586 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69  nction..  */.  i
25587 66 28 20 28 64 62 2d 3e 66 6c 61 67 73 26 53 51  f( (db->flags&SQ
25588 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 29 20  LITE_CountRows) 
25589 26 26 20 21 70 50 61 72 73 65 2d 3e 6e 65 73 74  && !pParse->nest
2558a 65 64 20 26 26 20 21 70 50 61 72 73 65 2d 3e 70  ed && !pParse->p
2558b 54 72 69 67 67 65 72 54 61 62 20 29 7b 0a 20 20  TriggerTab ){.  
2558c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2558d 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74  Op2(v, OP_Result
2558e 52 6f 77 2c 20 6d 65 6d 43 6e 74 2c 20 31 29 3b  Row, memCnt, 1);
2558f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
25590 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29  SetNumCols(v, 1)
25591 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
25592 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30  eSetColName(v, 0
25593 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
25594 22 72 6f 77 73 20 64 65 6c 65 74 65 64 22 2c 20  "rows deleted", 
25595 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
25596 20 20 7d 0a 0a 64 65 6c 65 74 65 5f 66 72 6f 6d    }..delete_from
25597 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71 6c 69  _cleanup:.  sqli
25598 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 6f  te3AuthContextPo
25599 70 28 26 73 43 6f 6e 74 65 78 74 29 3b 0a 20 20  p(&sContext);.  
2559a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65  sqlite3SrcListDe
2559b 6c 65 74 65 28 64 62 2c 20 70 54 61 62 4c 69 73  lete(db, pTabLis
2559c 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  t);.  sqlite3Exp
2559d 72 44 65 6c 65 74 65 28 64 62 2c 20 70 57 68 65  rDelete(db, pWhe
2559e 72 65 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d  re);.  return;.}
2559f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
255a0 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20 56  tine generates V
255a1 44 42 45 20 63 6f 64 65 20 74 68 61 74 20 63 61  DBE code that ca
255a2 75 73 65 73 20 61 20 73 69 6e 67 6c 65 20 72 6f  uses a single ro
255a3 77 20 6f 66 20 61 0a 2a 2a 20 73 69 6e 67 6c 65  w of a.** single
255a4 20 74 61 62 6c 65 20 74 6f 20 62 65 20 64 65 6c   table to be del
255a5 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  eted..**.** The 
255a6 56 44 42 45 20 6d 75 73 74 20 62 65 20 69 6e 20  VDBE must be in 
255a7 61 20 70 61 72 74 69 63 75 6c 61 72 20 73 74 61  a particular sta
255a8 74 65 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75  te when this rou
255a9 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a  tine is called..
255aa 2a 2a 20 54 68 65 73 65 20 61 72 65 20 74 68 65  ** These are the
255ab 20 72 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   requirements:.*
255ac 2a 0a 2a 2a 20 20 20 31 2e 20 20 41 20 72 65 61  *.**   1.  A rea
255ad 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 20 70  d/write cursor p
255ae 6f 69 6e 74 69 6e 67 20 74 6f 20 70 54 61 62 2c  ointing to pTab,
255af 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6e 74 61   the table conta
255b0 69 6e 69 6e 67 20 74 68 65 20 72 6f 77 0a 2a 2a  ining the row.**
255b1 20 20 20 20 20 20 20 74 6f 20 62 65 20 64 65 6c         to be del
255b2 65 74 65 64 2c 20 6d 75 73 74 20 62 65 20 6f 70  eted, must be op
255b3 65 6e 65 64 20 61 73 20 63 75 72 73 6f 72 20 6e  ened as cursor n
255b4 75 6d 62 65 72 20 22 62 61 73 65 22 2e 0a 2a 2a  umber "base"..**
255b5 0a 2a 2a 20 20 20 32 2e 20 20 52 65 61 64 2f 77  .**   2.  Read/w
255b6 72 69 74 65 20 63 75 72 73 6f 72 73 20 66 6f 72  rite cursors for
255b7 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66 20   all indices of 
255b8 70 54 61 62 20 6d 75 73 74 20 62 65 20 6f 70 65  pTab must be ope
255b9 6e 20 61 73 0a 2a 2a 20 20 20 20 20 20 20 63 75  n as.**       cu
255ba 72 73 6f 72 20 6e 75 6d 62 65 72 20 62 61 73 65  rsor number base
255bb 2b 69 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20  +i for the i-th 
255bc 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 20 20 33  index..**.**   3
255bd 2e 20 20 54 68 65 20 72 65 63 6f 72 64 20 6e 75  .  The record nu
255be 6d 62 65 72 20 6f 66 20 74 68 65 20 72 6f 77 20  mber of the row 
255bf 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20 6d 75  to be deleted mu
255c0 73 74 20 62 65 20 73 74 6f 72 65 64 20 69 6e 0a  st be stored in.
255c1 2a 2a 20 20 20 20 20 20 20 6d 65 6d 6f 72 79 20  **       memory 
255c2 63 65 6c 6c 20 69 52 6f 77 69 64 2e 0a 2a 2a 0a  cell iRowid..**.
255c3 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
255c4 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20 74  generates code t
255c5 6f 20 72 65 6d 6f 76 65 20 62 6f 74 68 20 74 68  o remove both th
255c6 65 20 74 61 62 6c 65 20 72 65 63 6f 72 64 20 61  e table record a
255c7 6e 64 20 61 6c 6c 20 0a 2a 2a 20 69 6e 64 65 78  nd all .** index
255c8 20 65 6e 74 72 69 65 73 20 74 68 61 74 20 70 6f   entries that po
255c9 69 6e 74 20 74 6f 20 74 68 61 74 20 72 65 63 6f  int to that reco
255ca 72 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  rd..*/.SQLITE_PR
255cb 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
255cc 65 33 47 65 6e 65 72 61 74 65 52 6f 77 44 65 6c  e3GenerateRowDel
255cd 65 74 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ete(.  Parse *pP
255ce 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72  arse,     /* Par
255cf 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
255d0 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20    Table *pTab,  
255d1 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 63 6f       /* Table co
255d2 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 72 6f 77  ntaining the row
255d3 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20 2a   to be deleted *
255d4 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c 20 20 20  /.  int iCur,   
255d5 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72         /* Cursor
255d6 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20   number for the 
255d7 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69  table */.  int i
255d8 52 6f 77 69 64 2c 20 20 20 20 20 20 20 20 2f 2a  Rowid,        /*
255d9 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68 61   Memory cell tha
255da 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 72  t contains the r
255db 6f 77 69 64 20 74 6f 20 64 65 6c 65 74 65 20 2a  owid to delete *
255dc 2f 0a 20 20 69 6e 74 20 63 6f 75 6e 74 2c 20 20  /.  int count,  
255dd 20 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f 6e         /* If non
255de 2d 7a 65 72 6f 2c 20 69 6e 63 72 65 6d 65 6e 74  -zero, increment
255df 20 74 68 65 20 72 6f 77 20 63 68 61 6e 67 65 20   the row change 
255e0 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 54 72 69  counter */.  Tri
255e1 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 2c 20  gger *pTrigger, 
255e2 2f 2a 20 4c 69 73 74 20 6f 66 20 74 72 69 67 67  /* List of trigg
255e3 65 72 73 20 74 6f 20 28 70 6f 74 65 6e 74 69 61  ers to (potentia
255e4 6c 6c 79 29 20 66 69 72 65 20 2a 2f 0a 20 20 69  lly) fire */.  i
255e5 6e 74 20 6f 6e 63 6f 6e 66 20 20 20 20 20 20 20  nt onconf       
255e6 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 4f 4e 20    /* Default ON 
255e7 43 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79 20  CONFLICT policy 
255e8 66 6f 72 20 74 72 69 67 67 65 72 73 20 2a 2f 0a  for triggers */.
255e9 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70  ){.  Vdbe *v = p
255ea 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 20 20  Parse->pVdbe;   
255eb 20 20 20 20 20 2f 2a 20 56 64 62 65 20 2a 2f 0a       /* Vdbe */.
255ec 20 20 69 6e 74 20 69 4f 6c 64 20 3d 20 30 3b 20    int iOld = 0; 
255ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
255ee 20 20 2f 2a 20 46 69 72 73 74 20 72 65 67 69 73    /* First regis
255ef 74 65 72 20 69 6e 20 4f 4c 44 2e 2a 20 61 72 72  ter in OLD.* arr
255f0 61 79 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 61 62  ay */.  int iLab
255f1 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  el;             
255f2 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 62 65 6c          /* Label
255f3 20 72 65 73 6f 6c 76 65 64 20 74 6f 20 65 6e 64   resolved to end
255f4 20 6f 66 20 67 65 6e 65 72 61 74 65 64 20 63 6f   of generated co
255f5 64 65 20 2a 2f 0a 0a 20 20 2f 2a 20 56 64 62 65  de */..  /* Vdbe
255f6 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74   is guaranteed t
255f7 6f 20 68 61 76 65 20 62 65 65 6e 20 61 6c 6c 6f  o have been allo
255f8 63 61 74 65 64 20 62 79 20 74 68 69 73 20 73 74  cated by this st
255f9 61 67 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74  age. */.  assert
255fa 28 20 76 20 29 3b 0a 0a 20 20 2f 2a 20 53 65 65  ( v );..  /* See
255fb 6b 20 63 75 72 73 6f 72 20 69 43 75 72 20 74 6f  k cursor iCur to
255fc 20 74 68 65 20 72 6f 77 20 74 6f 20 64 65 6c 65   the row to dele
255fd 74 65 2e 20 49 66 20 74 68 69 73 20 72 6f 77 20  te. If this row 
255fe 6e 6f 20 6c 6f 6e 67 65 72 20 65 78 69 73 74 73  no longer exists
255ff 20 0a 20 20 2a 2a 20 28 74 68 69 73 20 63 61 6e   .  ** (this can
25600 20 68 61 70 70 65 6e 20 69 66 20 61 20 74 72 69   happen if a tri
25601 67 67 65 72 20 70 72 6f 67 72 61 6d 20 68 61 73  gger program has
25602 20 61 6c 72 65 61 64 79 20 64 65 6c 65 74 65 64   already deleted
25603 20 69 74 29 2c 20 64 6f 0a 20 20 2a 2a 20 6e 6f   it), do.  ** no
25604 74 20 61 74 74 65 6d 70 74 20 74 6f 20 64 65 6c  t attempt to del
25605 65 74 65 20 69 74 20 6f 72 20 66 69 72 65 20 61  ete it or fire a
25606 6e 79 20 44 45 4c 45 54 45 20 74 72 69 67 67 65  ny DELETE trigge
25607 72 73 2e 20 20 2a 2f 0a 20 20 69 4c 61 62 65 6c  rs.  */.  iLabel
25608 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
25609 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 73 71  keLabel(v);.  sq
2560a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2560b 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c  v, OP_NotExists,
2560c 20 69 43 75 72 2c 20 69 4c 61 62 65 6c 2c 20 69   iCur, iLabel, i
2560d 52 6f 77 69 64 29 3b 0a 20 0a 20 20 2f 2a 20 49  Rowid);. .  /* I
2560e 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20  f there are any 
2560f 74 72 69 67 67 65 72 73 20 74 6f 20 66 69 72 65  triggers to fire
25610 2c 20 61 6c 6c 6f 63 61 74 65 20 61 20 72 61 6e  , allocate a ran
25611 67 65 20 6f 66 20 72 65 67 69 73 74 65 72 73 20  ge of registers 
25612 74 6f 0a 20 20 2a 2a 20 75 73 65 20 66 6f 72 20  to.  ** use for 
25613 74 68 65 20 6f 6c 64 2e 2a 20 72 65 66 65 72 65  the old.* refere
25614 6e 63 65 73 20 69 6e 20 74 68 65 20 74 72 69 67  nces in the trig
25615 67 65 72 73 2e 20 20 2a 2f 0a 20 20 69 66 28 20  gers.  */.  if( 
25616 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20  pTrigger ){.    
25617 75 33 32 20 6d 61 73 6b 3b 20 20 20 20 20 20 20  u32 mask;       
25618 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
25619 20 4d 61 73 6b 20 6f 66 20 4f 4c 44 2e 2a 20 63   Mask of OLD.* c
2561a 6f 6c 75 6d 6e 73 20 69 6e 20 75 73 65 20 2a 2f  olumns in use */
2561b 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 3b 20 20  .    int iCol;  
2561c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2561d 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 75     /* Iterator u
2561e 73 65 64 20 77 68 69 6c 65 20 70 6f 70 75 6c 61  sed while popula
2561f 74 69 6e 67 20 4f 4c 44 2e 2a 20 2a 2f 0a 0a 20  ting OLD.* */.. 
25620 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 43 6f 75 6c     /* TODO: Coul
25621 64 20 75 73 65 20 74 65 6d 70 6f 72 61 72 79 20  d use temporary 
25622 72 65 67 69 73 74 65 72 73 20 68 65 72 65 2e 20  registers here. 
25623 41 6c 73 6f 20 63 6f 75 6c 64 20 61 74 74 65 6d  Also could attem
25624 70 74 20 74 6f 0a 20 20 20 20 2a 2a 20 61 76 6f  pt to.    ** avo
25625 69 64 20 63 6f 70 79 69 6e 67 20 74 68 65 20 63  id copying the c
25626 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 72  ontents of the r
25627 6f 77 69 64 20 72 65 67 69 73 74 65 72 2e 20 20  owid register.  
25628 2a 2f 0a 20 20 20 20 6d 61 73 6b 20 3d 20 73 71  */.    mask = sq
25629 6c 69 74 65 33 54 72 69 67 67 65 72 4f 6c 64 6d  lite3TriggerOldm
2562a 61 73 6b 28 70 50 61 72 73 65 2c 20 70 54 72 69  ask(pParse, pTri
2562b 67 67 65 72 2c 20 54 4b 5f 44 45 4c 45 54 45 2c  gger, TK_DELETE,
2562c 20 30 2c 20 70 54 61 62 2c 20 6f 6e 63 6f 6e 66   0, pTab, onconf
2562d 29 3b 0a 20 20 20 20 69 4f 6c 64 20 3d 20 70 50  );.    iOld = pP
2562e 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20  arse->nMem+1;.  
2562f 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b    pParse->nMem +
25630 3d 20 28 31 20 2b 20 70 54 61 62 2d 3e 6e 43 6f  = (1 + pTab->nCo
25631 6c 29 3b 0a 0a 20 20 20 20 2f 2a 20 50 6f 70 75  l);..    /* Popu
25632 6c 61 74 65 20 74 68 65 20 4f 4c 44 2e 2a 20 70  late the OLD.* p
25633 73 65 75 64 6f 2d 74 61 62 6c 65 20 72 65 67 69  seudo-table regi
25634 73 74 65 72 20 61 72 72 61 79 2e 20 54 68 65 73  ster array. Thes
25635 65 20 76 61 6c 75 65 73 20 77 69 6c 6c 20 62 65  e values will be
25636 20 0a 20 20 20 20 2a 2a 20 75 73 65 64 20 62 79   .    ** used by
25637 20 61 6e 79 20 42 45 46 4f 52 45 20 61 6e 64 20   any BEFORE and 
25638 41 46 54 45 52 20 74 72 69 67 67 65 72 73 20 74  AFTER triggers t
25639 68 61 74 20 65 78 69 73 74 2e 20 20 2a 2f 0a 20  hat exist.  */. 
2563a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2563b 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 70 79 2c  dOp2(v, OP_Copy,
2563c 20 69 52 6f 77 69 64 2c 20 69 4f 6c 64 29 3b 0a   iRowid, iOld);.
2563d 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20      for(iCol=0; 
2563e 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b  iCol<pTab->nCol;
2563f 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20   iCol++){.      
25640 69 66 28 20 6d 61 73 6b 3d 3d 30 78 66 66 66 66  if( mask==0xffff
25641 66 66 66 66 20 7c 7c 20 6d 61 73 6b 26 28 31 3c  ffff || mask&(1<
25642 3c 69 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20  <iCol) ){.      
25643 20 20 69 6e 74 20 69 54 61 72 67 65 74 20 3d 20    int iTarget = 
25644 69 4f 6c 64 20 2b 20 69 43 6f 6c 20 2b 20 31 3b  iOld + iCol + 1;
25645 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
25646 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
25647 5f 43 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 69  _Column, iCur, i
25648 43 6f 6c 2c 20 69 54 61 72 67 65 74 29 3b 0a 20  Col, iTarget);. 
25649 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f         sqlite3Co
2564a 6c 75 6d 6e 44 65 66 61 75 6c 74 28 76 2c 20 70  lumnDefault(v, p
2564b 54 61 62 2c 20 69 43 6f 6c 2c 20 69 54 61 72 67  Tab, iCol, iTarg
2564c 65 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  et);.      }.   
2564d 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 6e 76 6f 6b   }..    /* Invok
2564e 65 20 61 6e 79 20 42 45 46 4f 52 45 20 74 72 69  e any BEFORE tri
2564f 67 67 65 72 20 70 72 6f 67 72 61 6d 73 20 2a 2f  gger programs */
25650 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65  .    sqlite3Code
25651 52 6f 77 54 72 69 67 67 65 72 28 70 50 61 72 73  RowTrigger(pPars
25652 65 2c 20 70 54 72 69 67 67 65 72 2c 20 0a 20 20  e, pTrigger, .  
25653 20 20 20 20 20 20 54 4b 5f 44 45 4c 45 54 45 2c        TK_DELETE,
25654 20 30 2c 20 54 52 49 47 47 45 52 5f 42 45 46 4f   0, TRIGGER_BEFO
25655 52 45 2c 20 70 54 61 62 2c 20 2d 31 2c 20 69 4f  RE, pTab, -1, iO
25656 6c 64 2c 20 6f 6e 63 6f 6e 66 2c 20 69 4c 61 62  ld, onconf, iLab
25657 65 6c 0a 20 20 20 20 29 3b 0a 0a 20 20 20 20 2f  el.    );..    /
25658 2a 20 53 65 65 6b 20 74 68 65 20 63 75 72 73 6f  * Seek the curso
25659 72 20 74 6f 20 74 68 65 20 72 6f 77 20 74 6f 20  r to the row to 
2565a 62 65 20 64 65 6c 65 74 65 64 20 61 67 61 69 6e  be deleted again
2565b 2e 20 49 74 20 6d 61 79 20 62 65 20 74 68 61 74  . It may be that
2565c 0a 20 20 20 20 2a 2a 20 74 68 65 20 42 45 46 4f  .    ** the BEFO
2565d 52 45 20 74 72 69 67 67 65 72 73 20 63 6f 64 65  RE triggers code
2565e 64 20 61 62 6f 76 65 20 68 61 76 65 20 61 6c 72  d above have alr
2565f 65 61 64 79 20 72 65 6d 6f 76 65 64 20 74 68 65  eady removed the
25660 20 72 6f 77 0a 20 20 20 20 2a 2a 20 62 65 69 6e   row.    ** bein
25661 67 20 64 65 6c 65 74 65 64 2e 20 44 6f 20 6e 6f  g deleted. Do no
25662 74 20 61 74 74 65 6d 70 74 20 74 6f 20 64 65 6c  t attempt to del
25663 65 74 65 20 74 68 65 20 72 6f 77 20 61 20 73 65  ete the row a se
25664 63 6f 6e 64 20 74 69 6d 65 2c 20 61 6e 64 20 0a  cond time, and .
25665 20 20 20 20 2a 2a 20 64 6f 20 6e 6f 74 20 66 69      ** do not fi
25666 72 65 20 41 46 54 45 52 20 74 72 69 67 67 65 72  re AFTER trigger
25667 73 2e 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  s.  */.    sqlit
25668 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
25669 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20 69 43  OP_NotExists, iC
2566a 75 72 2c 20 69 4c 61 62 65 6c 2c 20 69 52 6f 77  ur, iLabel, iRow
2566b 69 64 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44  id);.  }..  /* D
2566c 65 6c 65 74 65 20 74 68 65 20 69 6e 64 65 78 20  elete the index 
2566d 61 6e 64 20 74 61 62 6c 65 20 65 6e 74 72 69 65  and table entrie
2566e 73 2e 20 53 6b 69 70 20 74 68 69 73 20 73 74 65  s. Skip this ste
2566f 70 20 69 66 20 70 54 61 62 20 69 73 20 72 65 61  p if pTab is rea
25670 6c 6c 79 0a 20 20 2a 2a 20 61 20 76 69 65 77 20  lly.  ** a view 
25671 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 74  (in which case t
25672 68 65 20 6f 6e 6c 79 20 65 66 66 65 63 74 20 6f  he only effect o
25673 66 20 74 68 65 20 44 45 4c 45 54 45 20 73 74 61  f the DELETE sta
25674 74 65 6d 65 6e 74 20 69 73 20 74 6f 0a 20 20 2a  tement is to.  *
25675 2a 20 66 69 72 65 20 74 68 65 20 49 4e 53 54 45  * fire the INSTE
25676 41 44 20 4f 46 20 74 72 69 67 67 65 72 73 29 2e  AD OF triggers).
25677 20 20 2a 2f 20 0a 20 20 69 66 28 20 70 54 61 62    */ .  if( pTab
25678 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29 7b 0a  ->pSelect==0 ){.
25679 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72      sqlite3Gener
2567a 61 74 65 52 6f 77 49 6e 64 65 78 44 65 6c 65 74  ateRowIndexDelet
2567b 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  e(pParse, pTab, 
2567c 69 43 75 72 2c 20 30 29 3b 0a 20 20 20 20 73 71  iCur, 0);.    sq
2567d 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2567e 76 2c 20 4f 50 5f 44 65 6c 65 74 65 2c 20 69 43  v, OP_Delete, iC
2567f 75 72 2c 20 28 63 6f 75 6e 74 3f 4f 50 46 4c 41  ur, (count?OPFLA
25680 47 5f 4e 43 48 41 4e 47 45 3a 30 29 29 3b 0a 20  G_NCHANGE:0));. 
25681 20 20 20 69 66 28 20 63 6f 75 6e 74 20 29 7b 0a     if( count ){.
25682 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
25683 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c  eChangeP4(v, -1,
25684 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 50 34   pTab->zName, P4
25685 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 7d 0a  _STATIC);.    }.
25686 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65    }..  /* Invoke
25687 20 41 46 54 45 52 20 74 72 69 67 67 65 72 73 2e   AFTER triggers.
25688 20 2a 2f 0a 20 20 69 66 28 20 70 54 72 69 67 67   */.  if( pTrigg
25689 65 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  er ){.    sqlite
2568a 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28  3CodeRowTrigger(
2568b 70 50 61 72 73 65 2c 20 70 54 72 69 67 67 65 72  pParse, pTrigger
2568c 2c 20 0a 20 20 20 20 20 20 20 20 54 4b 5f 44 45  , .        TK_DE
2568d 4c 45 54 45 2c 20 30 2c 20 54 52 49 47 47 45 52  LETE, 0, TRIGGER
2568e 5f 41 46 54 45 52 2c 20 70 54 61 62 2c 20 2d 31  _AFTER, pTab, -1
2568f 2c 20 69 4f 6c 64 2c 20 6f 6e 63 6f 6e 66 2c 20  , iOld, onconf, 
25690 69 4c 61 62 65 6c 0a 20 20 20 20 29 3b 0a 20 20  iLabel.    );.  
25691 7d 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72  }..  /* Jump her
25692 65 20 69 66 20 74 68 65 20 72 6f 77 20 68 61 64  e if the row had
25693 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 64 65   already been de
25694 6c 65 74 65 64 20 62 65 66 6f 72 65 20 61 6e 79  leted before any
25695 20 42 45 46 4f 52 45 0a 20 20 2a 2a 20 74 72 69   BEFORE.  ** tri
25696 67 67 65 72 20 70 72 6f 67 72 61 6d 73 20 77 65  gger programs we
25697 72 65 20 69 6e 76 6f 6b 65 64 2e 20 4f 72 20 69  re invoked. Or i
25698 66 20 61 20 74 72 69 67 67 65 72 20 70 72 6f 67  f a trigger prog
25699 72 61 6d 20 74 68 72 6f 77 73 20 61 20 0a 20 20  ram throws a .  
2569a 2a 2a 20 52 41 49 53 45 28 49 47 4e 4f 52 45 29  ** RAISE(IGNORE)
2569b 20 65 78 63 65 70 74 69 6f 6e 2e 20 20 2a 2f 0a   exception.  */.
2569c 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
2569d 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 4c 61  olveLabel(v, iLa
2569e 62 65 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  bel);.}../*.** T
2569f 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65 6e 65  his routine gene
256a0 72 61 74 65 73 20 56 44 42 45 20 63 6f 64 65 20  rates VDBE code 
256a1 74 68 61 74 20 63 61 75 73 65 73 20 74 68 65 20  that causes the 
256a2 64 65 6c 65 74 69 6f 6e 20 6f 66 20 61 6c 6c 0a  deletion of all.
256a3 2a 2a 20 69 6e 64 65 78 20 65 6e 74 72 69 65 73  ** index entries
256a4 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
256a5 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66   a single row of
256a6 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 2e   a single table.
256a7 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 44 42 45 20  .**.** The VDBE 
256a8 6d 75 73 74 20 62 65 20 69 6e 20 61 20 70 61 72  must be in a par
256a9 74 69 63 75 6c 61 72 20 73 74 61 74 65 20 77 68  ticular state wh
256aa 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  en this routine 
256ab 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 20 54 68  is called..** Th
256ac 65 73 65 20 61 72 65 20 74 68 65 20 72 65 71 75  ese are the requ
256ad 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20  irements:.**.** 
256ae 20 20 31 2e 20 20 41 20 72 65 61 64 2f 77 72 69    1.  A read/wri
256af 74 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 69  te cursor pointi
256b0 6e 67 20 74 6f 20 70 54 61 62 2c 20 74 68 65 20  ng to pTab, the 
256b1 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67  table containing
256b2 20 74 68 65 20 72 6f 77 0a 2a 2a 20 20 20 20 20   the row.**     
256b3 20 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 2c    to be deleted,
256b4 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 65 64 20   must be opened 
256b5 61 73 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  as cursor number
256b6 20 22 69 43 75 72 22 2e 0a 2a 2a 0a 2a 2a 20 20   "iCur"..**.**  
256b7 20 32 2e 20 20 52 65 61 64 2f 77 72 69 74 65 20   2.  Read/write 
256b8 63 75 72 73 6f 72 73 20 66 6f 72 20 61 6c 6c 20  cursors for all 
256b9 69 6e 64 69 63 65 73 20 6f 66 20 70 54 61 62 20  indices of pTab 
256ba 6d 75 73 74 20 62 65 20 6f 70 65 6e 20 61 73 0a  must be open as.
256bb 2a 2a 20 20 20 20 20 20 20 63 75 72 73 6f 72 20  **       cursor 
256bc 6e 75 6d 62 65 72 20 69 43 75 72 2b 69 20 66 6f  number iCur+i fo
256bd 72 20 74 68 65 20 69 2d 74 68 20 69 6e 64 65 78  r the i-th index
256be 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 2e 20 20 54 68  ..**.**   3.  Th
256bf 65 20 22 69 43 75 72 22 20 63 75 72 73 6f 72 20  e "iCur" cursor 
256c0 6d 75 73 74 20 62 65 20 70 6f 69 6e 74 69 6e 67  must be pointing
256c1 20 74 6f 20 74 68 65 20 72 6f 77 20 74 68 61 74   to the row that
256c2 20 69 73 20 74 6f 20 62 65 0a 2a 2a 20 20 20 20   is to be.**    
256c3 20 20 20 64 65 6c 65 74 65 64 2e 0a 2a 2f 0a 53     deleted..*/.S
256c4 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
256c5 69 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61  id sqlite3Genera
256c6 74 65 52 6f 77 49 6e 64 65 78 44 65 6c 65 74 65  teRowIndexDelete
256c7 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
256c8 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e  e,     /* Parsin
256c9 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72  g and code gener
256ca 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  ating context */
256cb 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20  .  Table *pTab, 
256cc 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 63        /* Table c
256cd 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 72 6f  ontaining the ro
256ce 77 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20  w to be deleted 
256cf 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c 20 20  */.  int iCur,  
256d0 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f          /* Curso
256d1 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65  r number for the
256d2 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20   table */.  int 
256d3 2a 61 52 65 67 49 64 78 20 20 20 20 20 20 20 2f  *aRegIdx       /
256d4 2a 20 4f 6e 6c 79 20 64 65 6c 65 74 65 20 69 66  * Only delete if
256d5 20 61 52 65 67 49 64 78 21 3d 30 20 26 26 20 61   aRegIdx!=0 && a
256d6 52 65 67 49 64 78 5b 69 5d 3e 30 20 2a 2f 0a 29  RegIdx[i]>0 */.)
256d7 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 49 6e 64  {.  int i;.  Ind
256d8 65 78 20 2a 70 49 64 78 3b 0a 20 20 69 6e 74 20  ex *pIdx;.  int 
256d9 72 31 3b 0a 0a 20 20 66 6f 72 28 69 3d 31 2c 20  r1;..  for(i=1, 
256da 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65  pIdx=pTab->pInde
256db 78 3b 20 70 49 64 78 3b 20 69 2b 2b 2c 20 70 49  x; pIdx; i++, pI
256dc 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b  dx=pIdx->pNext){
256dd 0a 20 20 20 20 69 66 28 20 61 52 65 67 49 64 78  .    if( aRegIdx
256de 21 3d 30 20 26 26 20 61 52 65 67 49 64 78 5b 69  !=0 && aRegIdx[i
256df 2d 31 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75  -1]==0 ) continu
256e0 65 3b 0a 20 20 20 20 72 31 20 3d 20 73 71 6c 69  e;.    r1 = sqli
256e1 74 65 33 47 65 6e 65 72 61 74 65 49 6e 64 65 78  te3GenerateIndex
256e2 4b 65 79 28 70 50 61 72 73 65 2c 20 70 49 64 78  Key(pParse, pIdx
256e3 2c 20 69 43 75 72 2c 20 30 2c 20 30 29 3b 0a 20  , iCur, 0, 0);. 
256e4 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
256e5 64 4f 70 33 28 70 50 61 72 73 65 2d 3e 70 56 64  dOp3(pParse->pVd
256e6 62 65 2c 20 4f 50 5f 49 64 78 44 65 6c 65 74 65  be, OP_IdxDelete
256e7 2c 20 69 43 75 72 2b 69 2c 20 72 31 2c 70 49 64  , iCur+i, r1,pId
256e8 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b 0a 20  x->nColumn+1);. 
256e9 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65   }.}../*.** Gene
256ea 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77  rate code that w
256eb 69 6c 6c 20 61 73 73 65 6d 62 6c 65 20 61 6e 20  ill assemble an 
256ec 69 6e 64 65 78 20 6b 65 79 20 61 6e 64 20 70 75  index key and pu
256ed 74 20 69 74 20 69 6e 20 72 65 67 69 73 74 65 72  t it in register
256ee 0a 2a 2a 20 72 65 67 4f 75 74 2e 20 20 54 68 65  .** regOut.  The
256ef 20 6b 65 79 20 77 69 74 68 20 62 65 20 66 6f 72   key with be for
256f0 20 69 6e 64 65 78 20 70 49 64 78 20 77 68 69 63   index pIdx whic
256f1 68 20 69 73 20 61 6e 20 69 6e 64 65 78 20 6f 6e  h is an index on
256f2 20 70 54 61 62 2e 0a 2a 2a 20 69 43 75 72 20 69   pTab..** iCur i
256f3 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 61  s the index of a
256f4 20 63 75 72 73 6f 72 20 6f 70 65 6e 20 6f 6e 20   cursor open on 
256f5 74 68 65 20 70 54 61 62 20 74 61 62 6c 65 20 61  the pTab table a
256f6 6e 64 20 70 6f 69 6e 74 69 6e 67 20 74 6f 0a 2a  nd pointing to.*
256f7 2a 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74  * the entry that
256f8 20 6e 65 65 64 73 20 69 6e 64 65 78 69 6e 67 2e   needs indexing.
256f9 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  .**.** Return a 
256fa 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20  register number 
256fb 77 68 69 63 68 20 69 73 20 74 68 65 20 66 69 72  which is the fir
256fc 73 74 20 69 6e 20 61 20 62 6c 6f 63 6b 20 6f 66  st in a block of
256fd 0a 2a 2a 20 72 65 67 69 73 74 65 72 73 20 74 68  .** registers th
256fe 61 74 20 68 6f 6c 64 73 20 74 68 65 20 65 6c 65  at holds the ele
256ff 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 69 6e 64  ments of the ind
25700 65 78 20 6b 65 79 2e 20 20 54 68 65 0a 2a 2a 20  ex key.  The.** 
25701 62 6c 6f 63 6b 20 6f 66 20 72 65 67 69 73 74 65  block of registe
25702 72 73 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  rs has already b
25703 65 65 6e 20 64 65 61 6c 6c 6f 63 61 74 65 64 20  een deallocated 
25704 62 79 20 74 68 65 20 74 69 6d 65 0a 2a 2a 20 74  by the time.** t
25705 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
25706 72 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  rns..*/.SQLITE_P
25707 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
25708 65 33 47 65 6e 65 72 61 74 65 49 6e 64 65 78 4b  e3GenerateIndexK
25709 65 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  ey(.  Parse *pPa
2570a 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73  rse,     /* Pars
2570b 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
2570c 20 49 6e 64 65 78 20 2a 70 49 64 78 2c 20 20 20   Index *pIdx,   
2570d 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78      /* The index
2570e 20 66 6f 72 20 77 68 69 63 68 20 74 6f 20 67 65   for which to ge
2570f 6e 65 72 61 74 65 20 61 20 6b 65 79 20 2a 2f 0a  nerate a key */.
25710 20 20 69 6e 74 20 69 43 75 72 2c 20 20 20 20 20    int iCur,     
25711 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e       /* Cursor n
25712 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 70 49  umber for the pI
25713 64 78 2d 3e 70 54 61 62 6c 65 20 74 61 62 6c 65  dx->pTable table
25714 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4f 75 74   */.  int regOut
25715 2c 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74  ,        /* Writ
25716 65 20 74 68 65 20 6e 65 77 20 69 6e 64 65 78 20  e the new index 
25717 6b 65 79 20 74 6f 20 74 68 69 73 20 72 65 67 69  key to this regi
25718 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 64 6f  ster */.  int do
25719 4d 61 6b 65 52 65 63 20 20 20 20 20 20 2f 2a 20  MakeRec      /* 
2571a 52 75 6e 20 74 68 65 20 4f 50 5f 4d 61 6b 65 52  Run the OP_MakeR
2571b 65 63 6f 72 64 20 69 6e 73 74 72 75 63 74 69 6f  ecord instructio
2571c 6e 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a  n if true */.){.
2571d 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
2571e 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74  se->pVdbe;.  int
2571f 20 6a 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   j;.  Table *pTa
25720 62 20 3d 20 70 49 64 78 2d 3e 70 54 61 62 6c 65  b = pIdx->pTable
25721 3b 0a 20 20 69 6e 74 20 72 65 67 42 61 73 65 3b  ;.  int regBase;
25722 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a 0a 20 20  .  int nCol;..  
25723 6e 43 6f 6c 20 3d 20 70 49 64 78 2d 3e 6e 43 6f  nCol = pIdx->nCo
25724 6c 75 6d 6e 3b 0a 20 20 72 65 67 42 61 73 65 20  lumn;.  regBase 
25725 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
25726 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 6e 43  Range(pParse, nC
25727 6f 6c 2b 31 29 3b 0a 20 20 73 71 6c 69 74 65 33  ol+1);.  sqlite3
25728 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
25729 5f 52 6f 77 69 64 2c 20 69 43 75 72 2c 20 72 65  _Rowid, iCur, re
2572a 67 42 61 73 65 2b 6e 43 6f 6c 29 3b 0a 20 20 66  gBase+nCol);.  f
2572b 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 43 6f 6c 3b 20  or(j=0; j<nCol; 
2572c 6a 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 69 64  j++){.    int id
2572d 78 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75  x = pIdx->aiColu
2572e 6d 6e 5b 6a 5d 3b 0a 20 20 20 20 69 66 28 20 69  mn[j];.    if( i
2572f 64 78 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20  dx==pTab->iPKey 
25730 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
25731 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
25732 5f 53 43 6f 70 79 2c 20 72 65 67 42 61 73 65 2b  _SCopy, regBase+
25733 6e 43 6f 6c 2c 20 72 65 67 42 61 73 65 2b 6a 29  nCol, regBase+j)
25734 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
25735 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
25736 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d  dOp3(v, OP_Colum
25737 6e 2c 20 69 43 75 72 2c 20 69 64 78 2c 20 72 65  n, iCur, idx, re
25738 67 42 61 73 65 2b 6a 29 3b 0a 20 20 20 20 20 20  gBase+j);.      
25739 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66  sqlite3ColumnDef
2573a 61 75 6c 74 28 76 2c 20 70 54 61 62 2c 20 69 64  ault(v, pTab, id
2573b 78 2c 20 2d 31 29 3b 0a 20 20 20 20 7d 0a 20 20  x, -1);.    }.  
2573c 7d 0a 20 20 69 66 28 20 64 6f 4d 61 6b 65 52 65  }.  if( doMakeRe
2573d 63 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  c ){.    sqlite3
2573e 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2573f 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67  _MakeRecord, reg
25740 42 61 73 65 2c 20 6e 43 6f 6c 2b 31 2c 20 72 65  Base, nCol+1, re
25741 67 4f 75 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  gOut);.    sqlit
25742 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
25743 2c 20 2d 31 2c 20 73 71 6c 69 74 65 33 49 6e 64  , -1, sqlite3Ind
25744 65 78 41 66 66 69 6e 69 74 79 53 74 72 28 76 2c  exAffinityStr(v,
25745 20 70 49 64 78 29 2c 20 30 29 3b 0a 20 20 20 20   pIdx), 0);.    
25746 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
25747 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70  AffinityChange(p
25748 50 61 72 73 65 2c 20 72 65 67 42 61 73 65 2c 20  Parse, regBase, 
25749 6e 43 6f 6c 2b 31 29 3b 0a 20 20 7d 0a 20 20 73  nCol+1);.  }.  s
2574a 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
2574b 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72  pRange(pParse, r
2574c 65 67 42 61 73 65 2c 20 6e 43 6f 6c 2b 31 29 3b  egBase, nCol+1);
2574d 0a 20 20 72 65 74 75 72 6e 20 72 65 67 42 61 73  .  return regBas
2574e 65 3b 0a 7d 0a 0a 2f 2a 20 4d 61 6b 65 20 73 75  e;.}../* Make su
2574f 72 65 20 22 69 73 56 69 65 77 22 20 67 65 74 73  re "isView" gets
25750 20 75 6e 64 65 66 69 6e 65 64 20 69 6e 20 63 61   undefined in ca
25751 73 65 20 74 68 69 73 20 66 69 6c 65 20 62 65 63  se this file bec
25752 6f 6d 65 73 20 70 61 72 74 20 6f 66 0a 2a 2a 20  omes part of.** 
25753 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e  the amalgamation
25754 20 2d 20 73 6f 20 74 68 61 74 20 73 75 62 73 65   - so that subse
25755 71 75 65 6e 74 20 66 69 6c 65 73 20 64 6f 20 6e  quent files do n
25756 6f 74 20 73 65 65 20 69 73 56 69 65 77 20 61 73  ot see isView as
25757 20 61 0a 2a 2a 20 6d 61 63 72 6f 2e 20 2a 2f 0a   a.** macro. */.
25758 23 75 6e 64 65 66 20 69 73 56 69 65 77 0a 0a 2f  #undef isView../
25759 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
2575a 6e 64 20 6f 66 20 64 65 6c 65 74 65 2e 63 20 2a  nd of delete.c *
2575b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2575c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2575d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
2575e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
2575f 65 67 69 6e 20 66 69 6c 65 20 66 75 6e 63 2e 63  egin file func.c
25760 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
25761 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25762 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
25763 2a 0a 2a 2a 20 32 30 30 32 20 46 65 62 72 75 61  *.** 2002 Februa
25764 72 79 20 32 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ry 23.**.** The 
25765 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
25766 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
25767 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
25768 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
25769 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
2576a 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
2576b 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
2576c 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
2576d 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
2576e 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
2576f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
25770 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
25771 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
25772 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
25773 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
25774 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
25775 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
25776 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25777 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25778 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25779 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2577a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
2577b 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43  e contains the C
2577c 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20   functions that 
2577d 69 6d 70 6c 65 6d 65 6e 74 20 76 61 72 69 6f 75  implement variou
2577e 73 20 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f  s SQL.** functio
2577f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 0a  ns of SQLite.  .
25780 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6f  **.** There is o
25781 6e 6c 79 20 6f 6e 65 20 65 78 70 6f 72 74 65 64  nly one exported
25782 20 73 79 6d 62 6f 6c 20 69 6e 20 74 68 69 73 20   symbol in this 
25783 66 69 6c 65 20 2d 20 74 68 65 20 66 75 6e 63 74  file - the funct
25784 69 6f 6e 0a 2a 2a 20 73 71 6c 69 74 65 52 65 67  ion.** sqliteReg
25785 69 73 74 65 72 42 75 69 6c 64 69 6e 46 75 6e 63  isterBuildinFunc
25786 74 69 6f 6e 73 28 29 20 66 6f 75 6e 64 20 61 74  tions() found at
25787 20 74 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20 74   the bottom of t
25788 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 41 6c 6c 20  he file..** All 
25789 6f 74 68 65 72 20 63 6f 64 65 20 68 61 73 20 66  other code has f
2578a 69 6c 65 20 73 63 6f 70 65 2e 0a 2a 2f 0a 0a 2f  ile scope..*/../
2578b 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
2578c 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69  collating functi
2578d 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  on associated wi
2578e 74 68 20 61 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  th a function..*
2578f 2f 0a 73 74 61 74 69 63 20 43 6f 6c 6c 53 65 71  /.static CollSeq
25790 20 2a 73 71 6c 69 74 65 33 47 65 74 46 75 6e 63   *sqlite3GetFunc
25791 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 5f  CollSeq(sqlite3_
25792 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
25793 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6e 74  ){.  return cont
25794 65 78 74 2d 3e 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f  ext->pColl;.}../
25795 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
25796 69 6f 6e 20 6f 66 20 74 68 65 20 6e 6f 6e 2d 61  ion of the non-a
25797 67 67 72 65 67 61 74 65 20 6d 69 6e 28 29 20 61  ggregate min() a
25798 6e 64 20 6d 61 78 28 29 20 66 75 6e 63 74 69 6f  nd max() functio
25799 6e 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  ns.*/.static voi
2579a 64 20 6d 69 6e 6d 61 78 46 75 6e 63 28 0a 20 20  d minmaxFunc(.  
2579b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
2579c 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
2579d 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f  argc,.  sqlite3_
2579e 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
2579f 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6d    int i;.  int m
257a0 61 73 6b 3b 20 20 20 20 2f 2a 20 30 20 66 6f 72  ask;    /* 0 for
257a1 20 6d 69 6e 28 29 20 6f 72 20 30 78 66 66 66 66   min() or 0xffff
257a2 66 66 66 66 20 66 6f 72 20 6d 61 78 28 29 20 2a  ffff for max() *
257a3 2f 0a 20 20 69 6e 74 20 69 42 65 73 74 3b 0a 20  /.  int iBest;. 
257a4 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b   CollSeq *pColl;
257a5 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63  ..  assert( argc
257a6 3e 31 20 29 3b 0a 20 20 6d 61 73 6b 20 3d 20 73  >1 );.  mask = s
257a7 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61  qlite3_user_data
257a8 28 63 6f 6e 74 65 78 74 29 3d 3d 30 20 3f 20 30  (context)==0 ? 0
257a9 20 3a 20 2d 31 3b 0a 20 20 70 43 6f 6c 6c 20 3d   : -1;.  pColl =
257aa 20 73 71 6c 69 74 65 33 47 65 74 46 75 6e 63 43   sqlite3GetFuncC
257ab 6f 6c 6c 53 65 71 28 63 6f 6e 74 65 78 74 29 3b  ollSeq(context);
257ac 0a 20 20 61 73 73 65 72 74 28 20 70 43 6f 6c 6c  .  assert( pColl
257ad 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 61   );.  assert( ma
257ae 73 6b 3d 3d 2d 31 20 7c 7c 20 6d 61 73 6b 3d 3d  sk==-1 || mask==
257af 30 20 29 3b 0a 20 20 69 42 65 73 74 20 3d 20 30  0 );.  iBest = 0
257b0 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  ;.  if( sqlite3_
257b1 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b  value_type(argv[
257b2 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c  0])==SQLITE_NULL
257b3 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72   ) return;.  for
257b4 28 69 3d 31 3b 20 69 3c 61 72 67 63 3b 20 69 2b  (i=1; i<argc; i+
257b5 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  +){.    if( sqli
257b6 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
257b7 72 67 76 5b 69 5d 29 3d 3d 53 51 4c 49 54 45 5f  rgv[i])==SQLITE_
257b8 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a 20  NULL ) return;. 
257b9 20 20 20 69 66 28 20 28 73 71 6c 69 74 65 33 4d     if( (sqlite3M
257ba 65 6d 43 6f 6d 70 61 72 65 28 61 72 67 76 5b 69  emCompare(argv[i
257bb 42 65 73 74 5d 2c 20 61 72 67 76 5b 69 5d 2c 20  Best], argv[i], 
257bc 70 43 6f 6c 6c 29 5e 6d 61 73 6b 29 3e 3d 30 20  pColl)^mask)>=0 
257bd 29 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  ){.      testcas
257be 65 28 20 6d 61 73 6b 3d 3d 30 20 29 3b 0a 20 20  e( mask==0 );.  
257bf 20 20 20 20 69 42 65 73 74 20 3d 20 69 3b 0a 20      iBest = i;. 
257c0 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74     }.  }.  sqlit
257c1 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28  e3_result_value(
257c2 63 6f 6e 74 65 78 74 2c 20 61 72 67 76 5b 69 42  context, argv[iB
257c3 65 73 74 5d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  est]);.}../*.** 
257c4 52 65 74 75 72 6e 20 74 68 65 20 74 79 70 65 20  Return the type 
257c5 6f 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e  of the argument.
257c6 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
257c7 74 79 70 65 6f 66 46 75 6e 63 28 0a 20 20 73 71  typeofFunc(.  sq
257c8 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
257c9 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f  ontext,.  int No
257ca 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33  tUsed,.  sqlite3
257cb 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
257cc 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
257cd 20 3d 20 30 3b 0a 20 20 55 4e 55 53 45 44 5f 50   = 0;.  UNUSED_P
257ce 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
257cf 29 3b 0a 20 20 73 77 69 74 63 68 28 20 73 71 6c  );.  switch( sql
257d0 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
257d1 61 72 67 76 5b 30 5d 29 20 29 7b 0a 20 20 20 20  argv[0]) ){.    
257d2 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 45  case SQLITE_INTE
257d3 47 45 52 3a 20 7a 20 3d 20 22 69 6e 74 65 67 65  GER: z = "intege
257d4 72 22 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 63  r"; break;.    c
257d5 61 73 65 20 53 51 4c 49 54 45 5f 54 45 58 54 3a  ase SQLITE_TEXT:
257d6 20 20 20 20 7a 20 3d 20 22 74 65 78 74 22 3b 20      z = "text"; 
257d7 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
257d8 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a  se SQLITE_FLOAT:
257d9 20 20 20 7a 20 3d 20 22 72 65 61 6c 22 3b 20 20     z = "real";  
257da 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
257db 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a 20 20  e SQLITE_BLOB:  
257dc 20 20 7a 20 3d 20 22 62 6c 6f 62 22 3b 20 20 20    z = "blob";   
257dd 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65 66 61   break;.    defa
257de 75 6c 74 3a 20 20 20 20 20 20 20 20 20 20 20 20  ult:            
257df 20 7a 20 3d 20 22 6e 75 6c 6c 22 3b 20 20 20 20   z = "null";    
257e0 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 73 71 6c  break;.  }.  sql
257e1 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
257e2 28 63 6f 6e 74 65 78 74 2c 20 7a 2c 20 2d 31 2c  (context, z, -1,
257e3 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
257e4 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  .}.../*.** Imple
257e5 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
257e6 20 6c 65 6e 67 74 68 28 29 20 66 75 6e 63 74 69   length() functi
257e7 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  on.*/.static voi
257e8 64 20 6c 65 6e 67 74 68 46 75 6e 63 28 0a 20 20  d lengthFunc(.  
257e9 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
257ea 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
257eb 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f  argc,.  sqlite3_
257ec 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
257ed 20 20 69 6e 74 20 6c 65 6e 3b 0a 0a 20 20 61 73    int len;..  as
257ee 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b  sert( argc==1 );
257ef 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
257f0 54 45 52 28 61 72 67 63 29 3b 0a 20 20 73 77 69  TER(argc);.  swi
257f1 74 63 68 28 20 73 71 6c 69 74 65 33 5f 76 61 6c  tch( sqlite3_val
257f2 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29  ue_type(argv[0])
257f3 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c   ){.    case SQL
257f4 49 54 45 5f 42 4c 4f 42 3a 0a 20 20 20 20 63 61  ITE_BLOB:.    ca
257f5 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45  se SQLITE_INTEGE
257f6 52 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  R:.    case SQLI
257f7 54 45 5f 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 20  TE_FLOAT: {.    
257f8 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
257f9 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 73 71  _int(context, sq
257fa 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
257fb 73 28 61 72 67 76 5b 30 5d 29 29 3b 0a 20 20 20  s(argv[0]));.   
257fc 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
257fd 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
257fe 54 45 58 54 3a 20 7b 0a 20 20 20 20 20 20 63 6f  TEXT: {.      co
257ff 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
25800 72 20 2a 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76  r *z = sqlite3_v
25801 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
25802 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 3d  ]);.      if( z=
25803 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20  =0 ) return;.   
25804 20 20 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20     len = 0;.    
25805 20 20 77 68 69 6c 65 28 20 2a 7a 20 29 7b 0a 20    while( *z ){. 
25806 20 20 20 20 20 20 20 6c 65 6e 2b 2b 3b 0a 20 20         len++;.  
25807 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49        SQLITE_SKI
25808 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 20  P_UTF8(z);.     
25809 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
2580a 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74  _result_int(cont
2580b 65 78 74 2c 20 6c 65 6e 29 3b 0a 20 20 20 20 20  ext, len);.     
2580c 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
2580d 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
2580e 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
2580f 74 5f 6e 75 6c 6c 28 63 6f 6e 74 65 78 74 29 3b  t_null(context);
25810 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
25811 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
25812 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
25813 6f 66 20 74 68 65 20 61 62 73 28 29 20 66 75 6e  of the abs() fun
25814 63 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20  ction.*/.static 
25815 76 6f 69 64 20 61 62 73 46 75 6e 63 28 73 71 6c  void absFunc(sql
25816 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
25817 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c  ntext, int argc,
25818 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
25819 2a 61 72 67 76 29 7b 0a 20 20 61 73 73 65 72 74  *argv){.  assert
2581a 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55  ( argc==1 );.  U
2581b 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
2581c 61 72 67 63 29 3b 0a 20 20 73 77 69 74 63 68 28  argc);.  switch(
2581d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
2581e 79 70 65 28 61 72 67 76 5b 30 5d 29 20 29 7b 0a  ype(argv[0]) ){.
2581f 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
25820 49 4e 54 45 47 45 52 3a 20 7b 0a 20 20 20 20 20  INTEGER: {.     
25821 20 69 36 34 20 69 56 61 6c 20 3d 20 73 71 6c 69   i64 iVal = sqli
25822 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28  te3_value_int64(
25823 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20  argv[0]);.      
25824 69 66 28 20 69 56 61 6c 3c 30 20 29 7b 0a 20 20  if( iVal<0 ){.  
25825 20 20 20 20 20 20 69 66 28 20 28 69 56 61 6c 3c        if( (iVal<
25826 3c 31 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  <1)==0 ){.      
25827 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
25828 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74  lt_error(context
25829 2c 20 22 69 6e 74 65 67 65 72 20 6f 76 65 72 66  , "integer overf
2582a 6c 6f 77 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20  low", -1);.     
2582b 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
2582c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
2582d 56 61 6c 20 3d 20 2d 69 56 61 6c 3b 0a 20 20 20  Val = -iVal;.   
2582e 20 20 20 7d 20 0a 20 20 20 20 20 20 73 71 6c 69     } .      sqli
2582f 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34  te3_result_int64
25830 28 63 6f 6e 74 65 78 74 2c 20 69 56 61 6c 29 3b  (context, iVal);
25831 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
25832 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c    }.    case SQL
25833 49 54 45 5f 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20  ITE_NULL: {.    
25834 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
25835 5f 6e 75 6c 6c 28 63 6f 6e 74 65 78 74 29 3b 0a  _null(context);.
25836 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
25837 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20   }.    default: 
25838 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72  {.      double r
25839 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  Val = sqlite3_va
2583a 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b  lue_double(argv[
2583b 30 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  0]);.      if( r
2583c 56 61 6c 3c 30 20 29 20 72 56 61 6c 20 3d 20 2d  Val<0 ) rVal = -
2583d 72 56 61 6c 3b 0a 20 20 20 20 20 20 73 71 6c 69  rVal;.      sqli
2583e 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c  te3_result_doubl
2583f 65 28 63 6f 6e 74 65 78 74 2c 20 72 56 61 6c 29  e(context, rVal)
25840 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
25841 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
25842 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
25843 20 6f 66 20 74 68 65 20 73 75 62 73 74 72 28 29   of the substr()
25844 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   function..**.**
25845 20 73 75 62 73 74 72 28 78 2c 70 31 2c 70 32 29   substr(x,p1,p2)
25846 20 20 72 65 74 75 72 6e 73 20 70 32 20 63 68 61    returns p2 cha
25847 72 61 63 74 65 72 73 20 6f 66 20 78 5b 5d 20 62  racters of x[] b
25848 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 70 31  eginning with p1
25849 2e 0a 2a 2a 20 70 31 20 69 73 20 31 2d 69 6e 64  ..** p1 is 1-ind
2584a 65 78 65 64 2e 20 20 53 6f 20 73 75 62 73 74 72  exed.  So substr
2584b 28 78 2c 31 2c 31 29 20 72 65 74 75 72 6e 73 20  (x,1,1) returns 
2584c 74 68 65 20 66 69 72 73 74 20 63 68 61 72 61 63  the first charac
2584d 74 65 72 0a 2a 2a 20 6f 66 20 78 2e 20 20 49 66  ter.** of x.  If
2584e 20 78 20 69 73 20 74 65 78 74 2c 20 74 68 65 6e   x is text, then
2584f 20 77 65 20 61 63 74 75 61 6c 6c 79 20 63 6f 75   we actually cou
25850 6e 74 20 55 54 46 2d 38 20 63 68 61 72 61 63 74  nt UTF-8 charact
25851 65 72 73 2e 0a 2a 2a 20 49 66 20 78 20 69 73 20  ers..** If x is 
25852 61 20 62 6c 6f 62 2c 20 74 68 65 6e 20 77 65 20  a blob, then we 
25853 63 6f 75 6e 74 20 62 79 74 65 73 2e 0a 2a 2a 0a  count bytes..**.
25854 2a 2a 20 49 66 20 70 31 20 69 73 20 6e 65 67 61  ** If p1 is nega
25855 74 69 76 65 2c 20 74 68 65 6e 20 77 65 20 62 65  tive, then we be
25856 67 69 6e 20 61 62 73 28 70 31 29 20 66 72 6f 6d  gin abs(p1) from
25857 20 74 68 65 20 65 6e 64 20 6f 66 20 78 5b 5d 2e   the end of x[].
25858 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
25859 73 75 62 73 74 72 46 75 6e 63 28 0a 20 20 73 71  substrFunc(.  sq
2585a 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
2585b 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
2585c 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  gc,.  sqlite3_va
2585d 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
2585e 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
2585f 68 61 72 20 2a 7a 3b 0a 20 20 63 6f 6e 73 74 20  har *z;.  const 
25860 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
25861 32 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20  2;.  int len;.  
25862 69 6e 74 20 70 30 74 79 70 65 3b 0a 20 20 69 36  int p0type;.  i6
25863 34 20 70 31 2c 20 70 32 3b 0a 20 20 69 6e 74 20  4 p1, p2;.  int 
25864 6e 65 67 50 32 20 3d 20 30 3b 0a 0a 20 20 61 73  negP2 = 0;..  as
25865 73 65 72 74 28 20 61 72 67 63 3d 3d 33 20 7c 7c  sert( argc==3 ||
25866 20 61 72 67 63 3d 3d 32 20 29 3b 0a 20 20 69 66   argc==2 );.  if
25867 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ( sqlite3_value_
25868 74 79 70 65 28 61 72 67 76 5b 31 5d 29 3d 3d 53  type(argv[1])==S
25869 51 4c 49 54 45 5f 4e 55 4c 4c 0a 20 20 20 7c 7c  QLITE_NULL.   ||
2586a 20 28 61 72 67 63 3d 3d 33 20 26 26 20 73 71 6c   (argc==3 && sql
2586b 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
2586c 61 72 67 76 5b 32 5d 29 3d 3d 53 51 4c 49 54 45  argv[2])==SQLITE
2586d 5f 4e 55 4c 4c 29 0a 20 20 29 7b 0a 20 20 20 20  _NULL).  ){.    
2586e 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 30  return;.  }.  p0
2586f 74 79 70 65 20 3d 20 73 71 6c 69 74 65 33 5f 76  type = sqlite3_v
25870 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30  alue_type(argv[0
25871 5d 29 3b 0a 20 20 69 66 28 20 70 30 74 79 70 65  ]);.  if( p0type
25872 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b  ==SQLITE_BLOB ){
25873 0a 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74  .    len = sqlit
25874 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
25875 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 7a 20 3d  rgv[0]);.    z =
25876 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
25877 6c 6f 62 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  lob(argv[0]);.  
25878 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74    if( z==0 ) ret
25879 75 72 6e 3b 0a 20 20 20 20 61 73 73 65 72 74 28  urn;.    assert(
2587a 20 6c 65 6e 3d 3d 73 71 6c 69 74 65 33 5f 76 61   len==sqlite3_va
2587b 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30  lue_bytes(argv[0
2587c 5d 29 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ]) );.  }else{. 
2587d 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76     z = sqlite3_v
2587e 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
2587f 5d 29 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30  ]);.    if( z==0
25880 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 6c   ) return;.    l
25881 65 6e 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28  en = 0;.    for(
25882 7a 32 3d 7a 3b 20 2a 7a 32 3b 20 6c 65 6e 2b 2b  z2=z; *z2; len++
25883 29 7b 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f  ){.      SQLITE_
25884 53 4b 49 50 5f 55 54 46 38 28 7a 32 29 3b 0a 20  SKIP_UTF8(z2);. 
25885 20 20 20 7d 0a 20 20 7d 0a 20 20 70 31 20 3d 20     }.  }.  p1 = 
25886 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
25887 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 69 66  t(argv[1]);.  if
25888 28 20 61 72 67 63 3d 3d 33 20 29 7b 0a 20 20 20  ( argc==3 ){.   
25889 20 70 32 20 3d 20 73 71 6c 69 74 65 33 5f 76 61   p2 = sqlite3_va
2588a 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b 32 5d 29  lue_int(argv[2])
2588b 3b 0a 20 20 20 20 69 66 28 20 70 32 3c 30 20 29  ;.    if( p2<0 )
2588c 7b 0a 20 20 20 20 20 20 70 32 20 3d 20 2d 70 32  {.      p2 = -p2
2588d 3b 0a 20 20 20 20 20 20 6e 65 67 50 32 20 3d 20  ;.      negP2 = 
2588e 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  1;.    }.  }else
2588f 7b 0a 20 20 20 20 70 32 20 3d 20 73 71 6c 69 74  {.    p2 = sqlit
25890 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  e3_context_db_ha
25891 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 2d 3e 61  ndle(context)->a
25892 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
25893 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20 7d 0a  IT_LENGTH];.  }.
25894 20 20 69 66 28 20 70 31 3c 30 20 29 7b 0a 20 20    if( p1<0 ){.  
25895 20 20 70 31 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20    p1 += len;.   
25896 20 69 66 28 20 70 31 3c 30 20 29 7b 0a 20 20 20   if( p1<0 ){.   
25897 20 20 20 70 32 20 2b 3d 20 70 31 3b 0a 20 20 20     p2 += p1;.   
25898 20 20 20 69 66 28 20 70 32 3c 30 20 29 20 70 32     if( p2<0 ) p2
25899 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 31 20 3d   = 0;.      p1 =
2589a 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73   0;.    }.  }els
2589b 65 20 69 66 28 20 70 31 3e 30 20 29 7b 0a 20 20  e if( p1>0 ){.  
2589c 20 20 70 31 2d 2d 3b 0a 20 20 7d 65 6c 73 65 20    p1--;.  }else 
2589d 69 66 28 20 70 32 3e 30 20 29 7b 0a 20 20 20 20  if( p2>0 ){.    
2589e 70 32 2d 2d 3b 0a 20 20 7d 0a 20 20 69 66 28 20  p2--;.  }.  if( 
2589f 6e 65 67 50 32 20 29 7b 0a 20 20 20 20 70 31 20  negP2 ){.    p1 
258a0 2d 3d 20 70 32 3b 0a 20 20 20 20 69 66 28 20 70  -= p2;.    if( p
258a1 31 3c 30 20 29 7b 0a 20 20 20 20 20 20 70 32 20  1<0 ){.      p2 
258a2 2b 3d 20 70 31 3b 0a 20 20 20 20 20 20 70 31 20  += p1;.      p1 
258a3 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  = 0;.    }.  }. 
258a4 20 61 73 73 65 72 74 28 20 70 31 3e 3d 30 20 26   assert( p1>=0 &
258a5 26 20 70 32 3e 3d 30 20 29 3b 0a 20 20 69 66 28  & p2>=0 );.  if(
258a6 20 70 31 2b 70 32 3e 6c 65 6e 20 29 7b 0a 20 20   p1+p2>len ){.  
258a7 20 20 70 32 20 3d 20 6c 65 6e 2d 70 31 3b 0a 20    p2 = len-p1;. 
258a8 20 20 20 69 66 28 20 70 32 3c 30 20 29 20 70 32     if( p2<0 ) p2
258a9 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20   = 0;.  }.  if( 
258aa 70 30 74 79 70 65 21 3d 53 51 4c 49 54 45 5f 42  p0type!=SQLITE_B
258ab 4c 4f 42 20 29 7b 0a 20 20 20 20 77 68 69 6c 65  LOB ){.    while
258ac 28 20 2a 7a 20 26 26 20 70 31 20 29 7b 0a 20 20  ( *z && p1 ){.  
258ad 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f      SQLITE_SKIP_
258ae 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 20 20 70  UTF8(z);.      p
258af 31 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66  1--;.    }.    f
258b0 6f 72 28 7a 32 3d 7a 3b 20 2a 7a 32 20 26 26 20  or(z2=z; *z2 && 
258b1 70 32 3b 20 70 32 2d 2d 29 7b 0a 20 20 20 20 20  p2; p2--){.     
258b2 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46   SQLITE_SKIP_UTF
258b3 38 28 7a 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20  8(z2);.    }.   
258b4 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
258b5 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 28 63  text(context, (c
258b6 68 61 72 2a 29 7a 2c 20 28 69 6e 74 29 28 7a 32  har*)z, (int)(z2
258b7 2d 7a 29 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e  -z), SQLITE_TRAN
258b8 53 49 45 4e 54 29 3b 0a 20 20 7d 65 6c 73 65 7b  SIENT);.  }else{
258b9 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
258ba 75 6c 74 5f 62 6c 6f 62 28 63 6f 6e 74 65 78 74  ult_blob(context
258bb 2c 20 28 63 68 61 72 2a 29 26 7a 5b 70 31 5d 2c  , (char*)&z[p1],
258bc 20 28 69 6e 74 29 70 32 2c 20 53 51 4c 49 54 45   (int)p2, SQLITE
258bd 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d  _TRANSIENT);.  }
258be 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
258bf 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
258c0 72 6f 75 6e 64 28 29 20 66 75 6e 63 74 69 6f 6e  round() function
258c1 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
258c2 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
258c3 5f 50 4f 49 4e 54 0a 73 74 61 74 69 63 20 76 6f  _POINT.static vo
258c4 69 64 20 72 6f 75 6e 64 46 75 6e 63 28 73 71 6c  id roundFunc(sql
258c5 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
258c6 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c  ntext, int argc,
258c7 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
258c8 2a 61 72 67 76 29 7b 0a 20 20 69 6e 74 20 6e 20  *argv){.  int n 
258c9 3d 20 30 3b 0a 20 20 64 6f 75 62 6c 65 20 72 3b  = 0;.  double r;
258ca 0a 20 20 63 68 61 72 20 2a 7a 42 75 66 3b 0a 20  .  char *zBuf;. 
258cb 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31   assert( argc==1
258cc 20 7c 7c 20 61 72 67 63 3d 3d 32 20 29 3b 0a 20   || argc==2 );. 
258cd 20 69 66 28 20 61 72 67 63 3d 3d 32 20 29 7b 0a   if( argc==2 ){.
258ce 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e      if( SQLITE_N
258cf 55 4c 4c 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c  ULL==sqlite3_val
258d0 75 65 5f 74 79 70 65 28 61 72 67 76 5b 31 5d 29  ue_type(argv[1])
258d1 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 6e   ) return;.    n
258d2 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
258d3 5f 69 6e 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20  _int(argv[1]);. 
258d4 20 20 20 69 66 28 20 6e 3e 33 30 20 29 20 6e 20     if( n>30 ) n 
258d5 3d 20 33 30 3b 0a 20 20 20 20 69 66 28 20 6e 3c  = 30;.    if( n<
258d6 30 20 29 20 6e 20 3d 20 30 3b 0a 20 20 7d 0a 20  0 ) n = 0;.  }. 
258d7 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c   if( sqlite3_val
258d8 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29  ue_type(argv[0])
258d9 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20  ==SQLITE_NULL ) 
258da 72 65 74 75 72 6e 3b 0a 20 20 72 20 3d 20 73 71  return;.  r = sq
258db 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62  lite3_value_doub
258dc 6c 65 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 7a  le(argv[0]);.  z
258dd 42 75 66 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  Buf = sqlite3_mp
258de 72 69 6e 74 66 28 22 25 2e 2a 66 22 2c 6e 2c 72  rintf("%.*f",n,r
258df 29 3b 0a 20 20 69 66 28 20 7a 42 75 66 3d 3d 30  );.  if( zBuf==0
258e0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
258e1 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d  result_error_nom
258e2 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 7d  em(context);.  }
258e3 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
258e4 33 41 74 6f 46 28 7a 42 75 66 2c 20 26 72 29 3b  3AtoF(zBuf, &r);
258e5 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
258e6 65 28 7a 42 75 66 29 3b 0a 20 20 20 20 73 71 6c  e(zBuf);.    sql
258e7 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62  ite3_result_doub
258e8 6c 65 28 63 6f 6e 74 65 78 74 2c 20 72 29 3b 0a  le(context, r);.
258e9 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a    }.}.#endif../*
258ea 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79  .** Allocate nBy
258eb 74 65 20 62 79 74 65 73 20 6f 66 20 73 70 61 63  te bytes of spac
258ec 65 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f  e using sqlite3_
258ed 6d 61 6c 6c 6f 63 28 29 2e 20 49 66 20 74 68 65  malloc(). If the
258ee 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66  .** allocation f
258ef 61 69 6c 73 2c 20 63 61 6c 6c 20 73 71 6c 69 74  ails, call sqlit
258f0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
258f1 6e 6f 6d 65 6d 28 29 20 74 6f 20 6e 6f 74 69 66  nomem() to notif
258f2 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73  y.** the databas
258f3 65 20 68 61 6e 64 6c 65 20 74 68 61 74 20 6d 61  e handle that ma
258f4 6c 6c 6f 63 28 29 20 68 61 73 20 66 61 69 6c 65  lloc() has faile
258f5 64 20 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c  d and return NUL
258f6 4c 2e 0a 2a 2a 20 49 66 20 6e 42 79 74 65 20 69  L..** If nByte i
258f7 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68  s larger than th
258f8 65 20 6d 61 78 69 6d 75 6d 20 73 74 72 69 6e 67  e maximum string
258f9 20 6f 72 20 62 6c 6f 62 20 6c 65 6e 67 74 68 2c   or blob length,
258fa 20 74 68 65 6e 0a 2a 2a 20 72 61 69 73 65 20 61   then.** raise a
258fb 6e 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20  n SQLITE_TOOBIG 
258fc 65 78 63 65 70 74 69 6f 6e 20 61 6e 64 20 72 65  exception and re
258fd 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74  turn NULL..*/.st
258fe 61 74 69 63 20 76 6f 69 64 20 2a 63 6f 6e 74 65  atic void *conte
258ff 78 74 4d 61 6c 6c 6f 63 28 73 71 6c 69 74 65 33  xtMalloc(sqlite3
25900 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
25901 74 2c 20 69 36 34 20 6e 42 79 74 65 29 7b 0a 20  t, i64 nByte){. 
25902 20 63 68 61 72 20 2a 7a 3b 0a 20 20 73 71 6c 69   char *z;.  sqli
25903 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65  te3 *db = sqlite
25904 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
25905 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  dle(context);.  
25906 61 73 73 65 72 74 28 20 6e 42 79 74 65 3e 30 20  assert( nByte>0 
25907 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e  );.  testcase( n
25908 42 79 74 65 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74  Byte==db->aLimit
25909 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45  [SQLITE_LIMIT_LE
2590a 4e 47 54 48 5d 20 29 3b 0a 20 20 74 65 73 74 63  NGTH] );.  testc
2590b 61 73 65 28 20 6e 42 79 74 65 3d 3d 64 62 2d 3e  ase( nByte==db->
2590c 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
2590d 4d 49 54 5f 4c 45 4e 47 54 48 5d 2b 31 20 29 3b  MIT_LENGTH]+1 );
2590e 0a 20 20 69 66 28 20 6e 42 79 74 65 3e 64 62 2d  .  if( nByte>db-
2590f 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
25910 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a  IMIT_LENGTH] ){.
25911 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
25912 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28  lt_error_toobig(
25913 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 7a 20  context);.    z 
25914 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  = 0;.  }else{.  
25915 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c    z = sqlite3Mal
25916 6c 6f 63 28 28 69 6e 74 29 6e 42 79 74 65 29 3b  loc((int)nByte);
25917 0a 20 20 20 20 69 66 28 20 21 7a 20 29 7b 0a 20  .    if( !z ){. 
25918 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
25919 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28  ult_error_nomem(
2591a 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 7d 0a  context);.    }.
2591b 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a    }.  return z;.
2591c 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  }../*.** Impleme
2591d 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 75  ntation of the u
2591e 70 70 65 72 28 29 20 61 6e 64 20 6c 6f 77 65 72  pper() and lower
2591f 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73  () SQL functions
25920 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
25921 20 75 70 70 65 72 46 75 6e 63 28 73 71 6c 69 74   upperFunc(sqlit
25922 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
25923 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73  ext, int argc, s
25924 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
25925 72 67 76 29 7b 0a 20 20 63 68 61 72 20 2a 7a 31  rgv){.  char *z1
25926 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
25927 7a 32 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a  z2;.  int i, n;.
25928 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
25929 45 52 28 61 72 67 63 29 3b 0a 20 20 7a 32 20 3d  ER(argc);.  z2 =
2592a 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f   (char*)sqlite3_
2592b 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
2592c 30 5d 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74  0]);.  n = sqlit
2592d 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
2592e 72 67 76 5b 30 5d 29 3b 0a 20 20 2f 2a 20 56 65  rgv[0]);.  /* Ve
2592f 72 69 66 79 20 74 68 61 74 20 74 68 65 20 63 61  rify that the ca
25930 6c 6c 20 74 6f 20 5f 62 79 74 65 73 28 29 20 64  ll to _bytes() d
25931 6f 65 73 20 6e 6f 74 20 69 6e 76 61 6c 69 64 61  oes not invalida
25932 74 65 20 74 68 65 20 5f 74 65 78 74 28 29 20 70  te the _text() p
25933 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 61 73 73 65  ointer */.  asse
25934 72 74 28 20 7a 32 3d 3d 28 63 68 61 72 2a 29 73  rt( z2==(char*)s
25935 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
25936 74 28 61 72 67 76 5b 30 5d 29 20 29 3b 0a 20 20  t(argv[0]) );.  
25937 69 66 28 20 7a 32 20 29 7b 0a 20 20 20 20 7a 31  if( z2 ){.    z1
25938 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63   = contextMalloc
25939 28 63 6f 6e 74 65 78 74 2c 20 28 28 69 36 34 29  (context, ((i64)
2593a 6e 29 2b 31 29 3b 0a 20 20 20 20 69 66 28 20 7a  n)+1);.    if( z
2593b 31 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70  1 ){.      memcp
2593c 79 28 7a 31 2c 20 7a 32 2c 20 6e 2b 31 29 3b 0a  y(z1, z2, n+1);.
2593d 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 7a        for(i=0; z
2593e 31 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  1[i]; i++){.    
2593f 20 20 20 20 7a 31 5b 69 5d 20 3d 20 28 63 68 61      z1[i] = (cha
25940 72 29 73 71 6c 69 74 65 33 54 6f 75 70 70 65 72  r)sqlite3Toupper
25941 28 7a 31 5b 69 5d 29 3b 0a 20 20 20 20 20 20 7d  (z1[i]);.      }
25942 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
25943 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65  esult_text(conte
25944 78 74 2c 20 7a 31 2c 20 2d 31 2c 20 73 71 6c 69  xt, z1, -1, sqli
25945 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 20 20 7d  te3_free);.    }
25946 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f  .  }.}.static vo
25947 69 64 20 6c 6f 77 65 72 46 75 6e 63 28 73 71 6c  id lowerFunc(sql
25948 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
25949 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c  ntext, int argc,
2594a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
2594b 2a 61 72 67 76 29 7b 0a 20 20 75 38 20 2a 7a 31  *argv){.  u8 *z1
2594c 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
2594d 7a 32 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a  z2;.  int i, n;.
2594e 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
2594f 45 52 28 61 72 67 63 29 3b 0a 20 20 7a 32 20 3d  ER(argc);.  z2 =
25950 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f   (char*)sqlite3_
25951 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
25952 30 5d 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74  0]);.  n = sqlit
25953 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
25954 72 67 76 5b 30 5d 29 3b 0a 20 20 2f 2a 20 56 65  rgv[0]);.  /* Ve
25955 72 69 66 79 20 74 68 61 74 20 74 68 65 20 63 61  rify that the ca
25956 6c 6c 20 74 6f 20 5f 62 79 74 65 73 28 29 20 64  ll to _bytes() d
25957 6f 65 73 20 6e 6f 74 20 69 6e 76 61 6c 69 64 61  oes not invalida
25958 74 65 20 74 68 65 20 5f 74 65 78 74 28 29 20 70  te the _text() p
25959 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 61 73 73 65  ointer */.  asse
2595a 72 74 28 20 7a 32 3d 3d 28 63 68 61 72 2a 29 73  rt( z2==(char*)s
2595b 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
2595c 74 28 61 72 67 76 5b 30 5d 29 20 29 3b 0a 20 20  t(argv[0]) );.  
2595d 69 66 28 20 7a 32 20 29 7b 0a 20 20 20 20 7a 31  if( z2 ){.    z1
2595e 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63   = contextMalloc
2595f 28 63 6f 6e 74 65 78 74 2c 20 28 28 69 36 34 29  (context, ((i64)
25960 6e 29 2b 31 29 3b 0a 20 20 20 20 69 66 28 20 7a  n)+1);.    if( z
25961 31 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70  1 ){.      memcp
25962 79 28 7a 31 2c 20 7a 32 2c 20 6e 2b 31 29 3b 0a  y(z1, z2, n+1);.
25963 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 7a        for(i=0; z
25964 31 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  1[i]; i++){.    
25965 20 20 20 20 7a 31 5b 69 5d 20 3d 20 73 71 6c 69      z1[i] = sqli
25966 74 65 33 54 6f 6c 6f 77 65 72 28 7a 31 5b 69 5d  te3Tolower(z1[i]
25967 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
25968 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
25969 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 28 63  text(context, (c
2596a 68 61 72 20 2a 29 7a 31 2c 20 2d 31 2c 20 73 71  har *)z1, -1, sq
2596b 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 20  lite3_free);.   
2596c 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
2596d 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
2596e 66 20 74 68 65 20 49 46 4e 55 4c 4c 28 29 2c 20  f the IFNULL(), 
2596f 4e 56 4c 28 29 2c 20 61 6e 64 20 43 4f 41 4c 45  NVL(), and COALE
25970 53 43 45 28 29 20 66 75 6e 63 74 69 6f 6e 73 2e  SCE() functions.
25971 20 20 0a 2a 2a 20 41 6c 6c 20 74 68 72 65 65 20    .** All three 
25972 64 6f 20 74 68 65 20 73 61 6d 65 20 74 68 69 6e  do the same thin
25973 67 2e 20 20 54 68 65 79 20 72 65 74 75 72 6e 20  g.  They return 
25974 74 68 65 20 66 69 72 73 74 20 6e 6f 6e 2d 4e 55  the first non-NU
25975 4c 4c 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 0a  LL.** argument..
25976 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69  */.static void i
25977 66 6e 75 6c 6c 46 75 6e 63 28 0a 20 20 73 71 6c  fnullFunc(.  sql
25978 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
25979 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67  ntext,.  int arg
2597a 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  c,.  sqlite3_val
2597b 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69  ue **argv.){.  i
2597c 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  nt i;.  for(i=0;
2597d 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20   i<argc; i++){. 
2597e 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e 55     if( SQLITE_NU
2597f 4c 4c 21 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75  LL!=sqlite3_valu
25980 65 5f 74 79 70 65 28 61 72 67 76 5b 69 5d 29 20  e_type(argv[i]) 
25981 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
25982 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f  _result_value(co
25983 6e 74 65 78 74 2c 20 61 72 67 76 5b 69 5d 29 3b  ntext, argv[i]);
25984 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
25985 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
25986 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
25987 6f 66 20 72 61 6e 64 6f 6d 28 29 2e 20 20 52 65  of random().  Re
25988 74 75 72 6e 20 61 20 72 61 6e 64 6f 6d 20 69 6e  turn a random in
25989 74 65 67 65 72 2e 20 20 0a 2a 2f 0a 73 74 61 74  teger.  .*/.stat
2598a 69 63 20 76 6f 69 64 20 72 61 6e 64 6f 6d 46 75  ic void randomFu
2598b 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
2598c 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
2598d 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20    int NotUsed,. 
2598e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
2598f 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 73  *NotUsed2.){.  s
25990 71 6c 69 74 65 5f 69 6e 74 36 34 20 72 3b 0a 20  qlite_int64 r;. 
25991 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
25992 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55  R2(NotUsed, NotU
25993 73 65 64 32 29 3b 0a 20 20 73 71 6c 69 74 65 33  sed2);.  sqlite3
25994 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65  _randomness(size
25995 6f 66 28 72 29 2c 20 26 72 29 3b 0a 20 20 69 66  of(r), &r);.  if
25996 28 20 72 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20  ( r<0 ){.    /* 
25997 57 65 20 6e 65 65 64 20 74 6f 20 70 72 65 76 65  We need to preve
25998 6e 74 20 61 20 72 61 6e 64 6f 6d 20 6e 75 6d 62  nt a random numb
25999 65 72 20 6f 66 20 30 78 38 30 30 30 30 30 30 30  er of 0x80000000
2599a 30 30 30 30 30 30 30 30 20 0a 20 20 20 20 2a 2a  00000000 .    **
2599b 20 28 6f 72 20 2d 39 32 32 33 33 37 32 30 33 36   (or -9223372036
2599c 38 35 34 37 37 35 38 30 38 29 20 73 69 6e 63 65  854775808) since
2599d 20 77 68 65 6e 20 79 6f 75 20 64 6f 20 61 62 73   when you do abs
2599e 28 29 20 6f 66 20 74 68 61 74 0a 20 20 20 20 2a  () of that.    *
2599f 2a 20 6e 75 6d 62 65 72 20 6f 66 20 79 6f 75 20  * number of you 
259a0 67 65 74 20 74 68 65 20 73 61 6d 65 20 76 61 6c  get the same val
259a1 75 65 20 62 61 63 6b 20 61 67 61 69 6e 2e 20 20  ue back again.  
259a2 54 6f 20 64 6f 20 74 68 69 73 0a 20 20 20 20 2a  To do this.    *
259a3 2a 20 69 6e 20 61 20 77 61 79 20 74 68 61 74 20  * in a way that 
259a4 69 73 20 74 65 73 74 61 62 6c 65 2c 20 6d 61 73  is testable, mas
259a5 6b 20 74 68 65 20 73 69 67 6e 20 62 69 74 20 6f  k the sign bit o
259a6 66 66 20 6f 66 20 6e 65 67 61 74 69 76 65 0a 20  ff of negative. 
259a7 20 20 20 2a 2a 20 76 61 6c 75 65 73 2c 20 72 65     ** values, re
259a8 73 75 6c 74 69 6e 67 20 69 6e 20 61 20 70 6f 73  sulting in a pos
259a9 69 74 69 76 65 20 76 61 6c 75 65 2e 20 20 54 68  itive value.  Th
259aa 65 6e 20 74 61 6b 65 20 74 68 65 20 0a 20 20 20  en take the .   
259ab 20 2a 2a 20 32 73 20 63 6f 6d 70 6c 65 6d 65 6e   ** 2s complemen
259ac 74 20 6f 66 20 74 68 61 74 20 70 6f 73 69 74 69  t of that positi
259ad 76 65 20 76 61 6c 75 65 2e 20 20 54 68 65 20 65  ve value.  The e
259ae 6e 64 20 72 65 73 75 6c 74 20 63 61 6e 0a 20 20  nd result can.  
259af 20 20 2a 2a 20 74 68 65 72 65 66 6f 72 65 20 62    ** therefore b
259b0 65 20 6e 6f 20 6c 65 73 73 20 74 68 61 6e 20 2d  e no less than -
259b1 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35  9223372036854775
259b2 38 30 37 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  807..    */.    
259b3 72 20 3d 20 2d 28 72 20 5e 20 28 28 28 73 71 6c  r = -(r ^ (((sql
259b4 69 74 65 33 5f 69 6e 74 36 34 29 31 29 3c 3c 36  ite3_int64)1)<<6
259b5 33 29 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  3));.  }.  sqlit
259b6 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28  e3_result_int64(
259b7 63 6f 6e 74 65 78 74 2c 20 72 29 3b 0a 7d 0a 0a  context, r);.}..
259b8 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
259b9 74 69 6f 6e 20 6f 66 20 72 61 6e 64 6f 6d 62 6c  tion of randombl
259ba 6f 62 28 4e 29 2e 20 20 52 65 74 75 72 6e 20 61  ob(N).  Return a
259bb 20 72 61 6e 64 6f 6d 20 62 6c 6f 62 0a 2a 2a 20   random blob.** 
259bc 74 68 61 74 20 69 73 20 4e 20 62 79 74 65 73 20  that is N bytes 
259bd 6c 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  long..*/.static 
259be 76 6f 69 64 20 72 61 6e 64 6f 6d 42 6c 6f 62 28  void randomBlob(
259bf 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
259c0 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
259c1 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74  nt argc,.  sqlit
259c2 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
259c3 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 75 6e  ){.  int n;.  un
259c4 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 3b 0a  signed char *p;.
259c5 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d    assert( argc==
259c6 31 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  1 );.  UNUSED_PA
259c7 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20  RAMETER(argc);. 
259c8 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c   n = sqlite3_val
259c9 75 65 5f 69 6e 74 28 61 72 67 76 5b 30 5d 29 3b  ue_int(argv[0]);
259ca 0a 20 20 69 66 28 20 6e 3c 31 20 29 7b 0a 20 20  .  if( n<1 ){.  
259cb 20 20 6e 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 70    n = 1;.  }.  p
259cc 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63   = contextMalloc
259cd 28 63 6f 6e 74 65 78 74 2c 20 6e 29 3b 0a 20 20  (context, n);.  
259ce 69 66 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c  if( p ){.    sql
259cf 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28  ite3_randomness(
259d0 6e 2c 20 70 29 3b 0a 20 20 20 20 73 71 6c 69 74  n, p);.    sqlit
259d1 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 63  e3_result_blob(c
259d2 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a 29 70  ontext, (char*)p
259d3 2c 20 6e 2c 20 73 71 6c 69 74 65 33 5f 66 72 65  , n, sqlite3_fre
259d4 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  e);.  }.}../*.**
259d5 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
259d6 6f 66 20 74 68 65 20 6c 61 73 74 5f 69 6e 73 65  of the last_inse
259d7 72 74 5f 72 6f 77 69 64 28 29 20 53 51 4c 20 66  rt_rowid() SQL f
259d8 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 72 65  unction.  The re
259d9 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 69 73  turn.** value is
259da 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65   the same as the
259db 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e   sqlite3_last_in
259dc 73 65 72 74 5f 72 6f 77 69 64 28 29 20 41 50 49  sert_rowid() API
259dd 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74   function..*/.st
259de 61 74 69 63 20 76 6f 69 64 20 6c 61 73 74 5f 69  atic void last_i
259df 6e 73 65 72 74 5f 72 6f 77 69 64 28 0a 20 20 73  nsert_rowid(.  s
259e0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
259e1 63 6f 6e 74 65 78 74 2c 20 0a 20 20 69 6e 74 20  context, .  int 
259e2 4e 6f 74 55 73 65 64 2c 20 0a 20 20 73 71 6c 69  NotUsed, .  sqli
259e3 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55  te3_value **NotU
259e4 73 65 64 32 0a 29 7b 0a 20 20 73 71 6c 69 74 65  sed2.){.  sqlite
259e5 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f  3 *db = sqlite3_
259e6 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
259e7 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 55 4e  e(context);.  UN
259e8 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28  USED_PARAMETER2(
259e9 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64  NotUsed, NotUsed
259ea 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  2);.  sqlite3_re
259eb 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65  sult_int64(conte
259ec 78 74 2c 20 73 71 6c 69 74 65 33 5f 6c 61 73 74  xt, sqlite3_last
259ed 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 64 62  _insert_rowid(db
259ee 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70  ));.}../*.** Imp
259ef 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
259f0 68 65 20 63 68 61 6e 67 65 73 28 29 20 53 51 4c  he changes() SQL
259f1 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20   function.  The 
259f2 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20  return value is 
259f3 74 68 65 0a 2a 2a 20 73 61 6d 65 20 61 73 20 74  the.** same as t
259f4 68 65 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67  he sqlite3_chang
259f5 65 73 28 29 20 41 50 49 20 66 75 6e 63 74 69 6f  es() API functio
259f6 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  n..*/.static voi
259f7 64 20 63 68 61 6e 67 65 73 28 0a 20 20 73 71 6c  d changes(.  sql
259f8 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
259f9 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74  ntext,.  int Not
259fa 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f  Used,.  sqlite3_
259fb 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32  value **NotUsed2
259fc 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  .){.  sqlite3 *d
259fd 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  b = sqlite3_cont
259fe 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f  ext_db_handle(co
259ff 6e 74 65 78 74 29 3b 0a 20 20 55 4e 55 53 45 44  ntext);.  UNUSED
25a00 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55  _PARAMETER2(NotU
25a01 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a  sed, NotUsed2);.
25a02 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
25a03 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 73 71  _int(context, sq
25a04 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 64 62  lite3_changes(db
25a05 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70  ));.}../*.** Imp
25a06 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
25a07 68 65 20 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  he total_changes
25a08 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e  () SQL function.
25a09 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c    The return val
25a0a 75 65 20 69 73 0a 2a 2a 20 74 68 65 20 73 61 6d  ue is.** the sam
25a0b 65 20 61 73 20 74 68 65 20 73 71 6c 69 74 65 33  e as the sqlite3
25a0c 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29  _total_changes()
25a0d 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a   API function..*
25a0e 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 6f  /.static void to
25a0f 74 61 6c 5f 63 68 61 6e 67 65 73 28 0a 20 20 73  tal_changes(.  s
25a10 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
25a11 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e  context,.  int N
25a12 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65  otUsed,.  sqlite
25a13 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65  3_value **NotUse
25a14 64 32 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  d2.){.  sqlite3 
25a15 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  *db = sqlite3_co
25a16 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28  ntext_db_handle(
25a17 63 6f 6e 74 65 78 74 29 3b 0a 20 20 55 4e 55 53  context);.  UNUS
25a18 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f  ED_PARAMETER2(No
25a19 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29  tUsed, NotUsed2)
25a1a 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ;.  sqlite3_resu
25a1b 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20  lt_int(context, 
25a1c 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68  sqlite3_total_ch
25a1d 61 6e 67 65 73 28 64 62 29 29 3b 0a 7d 0a 0a 2f  anges(db));.}../
25a1e 2a 0a 2a 2a 20 41 20 73 74 72 75 63 74 75 72 65  *.** A structure
25a1f 20 64 65 66 69 6e 69 6e 67 20 68 6f 77 20 74 6f   defining how to
25a20 20 64 6f 20 47 4c 4f 42 2d 73 74 79 6c 65 20 63   do GLOB-style c
25a21 6f 6d 70 61 72 69 73 6f 6e 73 2e 0a 2a 2f 0a 73  omparisons..*/.s
25a22 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66  truct compareInf
25a23 6f 20 7b 0a 20 20 75 38 20 6d 61 74 63 68 41 6c  o {.  u8 matchAl
25a24 6c 3b 0a 20 20 75 38 20 6d 61 74 63 68 4f 6e 65  l;.  u8 matchOne
25a25 3b 0a 20 20 75 38 20 6d 61 74 63 68 53 65 74 3b  ;.  u8 matchSet;
25a26 0a 20 20 75 38 20 6e 6f 43 61 73 65 3b 0a 7d 3b  .  u8 noCase;.};
25a27 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 4c 49 4b 45  ../*.** For LIKE
25a28 20 61 6e 64 20 47 4c 4f 42 20 6d 61 74 63 68 69   and GLOB matchi
25a29 6e 67 20 6f 6e 20 45 42 43 44 49 43 20 6d 61 63  ng on EBCDIC mac
25a2a 68 69 6e 65 73 2c 20 61 73 73 75 6d 65 20 74 68  hines, assume th
25a2b 61 74 20 65 76 65 72 79 0a 2a 2a 20 63 68 61 72  at every.** char
25a2c 61 63 74 65 72 20 69 73 20 65 78 61 63 74 6c 79  acter is exactly
25a2d 20 6f 6e 65 20 62 79 74 65 20 69 6e 20 73 69 7a   one byte in siz
25a2e 65 2e 20 20 41 6c 73 6f 2c 20 61 6c 6c 20 63 68  e.  Also, all ch
25a2f 61 72 61 63 74 65 72 73 20 61 72 65 0a 2a 2a 20  aracters are.** 
25a30 61 62 6c 65 20 74 6f 20 70 61 72 74 69 63 69 70  able to particip
25a31 61 74 65 20 69 6e 20 75 70 70 65 72 2d 63 61 73  ate in upper-cas
25a32 65 2d 74 6f 2d 6c 6f 77 65 72 2d 63 61 73 65 20  e-to-lower-case 
25a33 6d 61 70 70 69 6e 67 73 20 69 6e 20 45 42 43 44  mappings in EBCD
25a34 49 43 0a 2a 2a 20 77 68 65 72 65 61 73 20 6f 6e  IC.** whereas on
25a35 6c 79 20 63 68 61 72 61 63 74 65 72 73 20 6c 65  ly characters le
25a36 73 73 20 74 68 61 6e 20 30 78 38 30 20 64 6f 20  ss than 0x80 do 
25a37 69 6e 20 41 53 43 49 49 2e 0a 2a 2f 0a 23 69 66  in ASCII..*/.#if
25a38 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
25a39 45 42 43 44 49 43 29 0a 23 20 64 65 66 69 6e 65  EBCDIC).# define
25a3a 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64   sqlite3Utf8Read
25a3b 28 41 2c 43 29 20 20 20 20 28 2a 28 41 2b 2b 29  (A,C)    (*(A++)
25a3c 29 0a 23 20 64 65 66 69 6e 65 20 47 6c 6f 67 55  ).# define GlogU
25a3d 70 70 65 72 54 6f 4c 6f 77 65 72 28 41 29 20 20  pperToLower(A)  
25a3e 20 20 20 41 20 3d 20 73 71 6c 69 74 65 33 55 70     A = sqlite3Up
25a3f 70 65 72 54 6f 4c 6f 77 65 72 5b 41 5d 0a 23 65  perToLower[A].#e
25a40 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 47 6c 6f  lse.# define Glo
25a41 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28 41 29  gUpperToLower(A)
25a42 20 20 20 20 20 69 66 28 20 41 3c 30 78 38 30 20       if( A<0x80 
25a43 29 7b 20 41 20 3d 20 73 71 6c 69 74 65 33 55 70  ){ A = sqlite3Up
25a44 70 65 72 54 6f 4c 6f 77 65 72 5b 41 5d 3b 20 7d  perToLower[A]; }
25a45 0a 23 65 6e 64 69 66 0a 0a 73 74 61 74 69 63 20  .#endif..static 
25a46 63 6f 6e 73 74 20 73 74 72 75 63 74 20 63 6f 6d  const struct com
25a47 70 61 72 65 49 6e 66 6f 20 67 6c 6f 62 49 6e 66  pareInfo globInf
25a48 6f 20 3d 20 7b 20 27 2a 27 2c 20 27 3f 27 2c 20  o = { '*', '?', 
25a49 27 5b 27 2c 20 30 20 7d 3b 0a 2f 2a 20 54 68 65  '[', 0 };./* The
25a4a 20 63 6f 72 72 65 63 74 20 53 51 4c 2d 39 32 20   correct SQL-92 
25a4b 62 65 68 61 76 69 6f 72 20 69 73 20 66 6f 72 20  behavior is for 
25a4c 74 68 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f  the LIKE operato
25a4d 72 20 74 6f 20 69 67 6e 6f 72 65 0a 2a 2a 20 63  r to ignore.** c
25a4e 61 73 65 2e 20 20 54 68 75 73 20 20 27 61 27 20  ase.  Thus  'a' 
25a4f 4c 49 4b 45 20 27 41 27 20 77 6f 75 6c 64 20 62  LIKE 'A' would b
25a50 65 20 74 72 75 65 2e 20 2a 2f 0a 73 74 61 74 69  e true. */.stati
25a51 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 63  c const struct c
25a52 6f 6d 70 61 72 65 49 6e 66 6f 20 6c 69 6b 65 49  ompareInfo likeI
25a53 6e 66 6f 4e 6f 72 6d 20 3d 20 7b 20 27 25 27 2c  nfoNorm = { '%',
25a54 20 27 5f 27 2c 20 20 20 30 2c 20 31 20 7d 3b 0a   '_',   0, 1 };.
25a55 2f 2a 20 49 66 20 53 51 4c 49 54 45 5f 43 41 53  /* If SQLITE_CAS
25a56 45 5f 53 45 4e 53 49 54 49 56 45 5f 4c 49 4b 45  E_SENSITIVE_LIKE
25a57 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65   is defined, the
25a58 6e 20 74 68 65 20 4c 49 4b 45 20 6f 70 65 72 61  n the LIKE opera
25a59 74 6f 72 0a 2a 2a 20 69 73 20 63 61 73 65 20 73  tor.** is case s
25a5a 65 6e 73 69 74 69 76 65 20 63 61 75 73 69 6e 67  ensitive causing
25a5b 20 27 61 27 20 4c 49 4b 45 20 27 41 27 20 74 6f   'a' LIKE 'A' to
25a5c 20 62 65 20 66 61 6c 73 65 20 2a 2f 0a 73 74 61   be false */.sta
25a5d 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74  tic const struct
25a5e 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 6c 69 6b   compareInfo lik
25a5f 65 49 6e 66 6f 41 6c 74 20 3d 20 7b 20 27 25 27  eInfoAlt = { '%'
25a60 2c 20 27 5f 27 2c 20 20 20 30 2c 20 30 20 7d 3b  , '_',   0, 0 };
25a61 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20  ../*.** Compare 
25a62 74 77 6f 20 55 54 46 2d 38 20 73 74 72 69 6e 67  two UTF-8 string
25a63 73 20 66 6f 72 20 65 71 75 61 6c 69 74 79 20 77  s for equality w
25a64 68 65 72 65 20 74 68 65 20 66 69 72 73 74 20 73  here the first s
25a65 74 72 69 6e 67 20 63 61 6e 0a 2a 2a 20 70 6f 74  tring can.** pot
25a66 65 6e 74 69 61 6c 6c 79 20 62 65 20 61 20 22 67  entially be a "g
25a67 6c 6f 62 22 20 65 78 70 72 65 73 73 69 6f 6e 2e  lob" expression.
25a68 20 20 52 65 74 75 72 6e 20 74 72 75 65 20 28 31    Return true (1
25a69 29 20 69 66 20 74 68 65 79 0a 2a 2a 20 61 72 65  ) if they.** are
25a6a 20 74 68 65 20 73 61 6d 65 20 61 6e 64 20 66 61   the same and fa
25a6b 6c 73 65 20 28 30 29 20 69 66 20 74 68 65 79 20  lse (0) if they 
25a6c 61 72 65 20 64 69 66 66 65 72 65 6e 74 2e 0a 2a  are different..*
25a6d 2a 0a 2a 2a 20 47 6c 6f 62 62 69 6e 67 20 72 75  *.** Globbing ru
25a6e 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  les:.**.**      
25a6f 27 2a 27 20 20 20 20 20 20 20 4d 61 74 63 68 65  '*'       Matche
25a70 73 20 61 6e 79 20 73 65 71 75 65 6e 63 65 20 6f  s any sequence o
25a71 66 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 63  f zero or more c
25a72 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a  haracters..**.**
25a73 20 20 20 20 20 20 27 3f 27 20 20 20 20 20 20 20        '?'       
25a74 4d 61 74 63 68 65 73 20 65 78 61 63 74 6c 79 20  Matches exactly 
25a75 6f 6e 65 20 63 68 61 72 61 63 74 65 72 2e 0a 2a  one character..*
25a76 2a 0a 2a 2a 20 20 20 20 20 5b 2e 2e 2e 5d 20 20  *.**     [...]  
25a77 20 20 20 20 4d 61 74 63 68 65 73 20 6f 6e 65 20      Matches one 
25a78 63 68 61 72 61 63 74 65 72 20 66 72 6f 6d 20 74  character from t
25a79 68 65 20 65 6e 63 6c 6f 73 65 64 20 6c 69 73 74  he enclosed list
25a7a 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   of.**          
25a7b 20 20 20 20 20 20 63 68 61 72 61 63 74 65 72 73        characters
25a7c 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 5b 5e 2e 2e  ..**.**     [^..
25a7d 2e 5d 20 20 20 20 20 4d 61 74 63 68 65 73 20 6f  .]     Matches o
25a7e 6e 65 20 63 68 61 72 61 63 74 65 72 20 6e 6f 74  ne character not
25a7f 20 69 6e 20 74 68 65 20 65 6e 63 6c 6f 73 65 64   in the enclosed
25a80 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 57 69 74   list..**.** Wit
25a81 68 20 74 68 65 20 5b 2e 2e 2e 5d 20 61 6e 64 20  h the [...] and 
25a82 5b 5e 2e 2e 2e 5d 20 6d 61 74 63 68 69 6e 67 2c  [^...] matching,
25a83 20 61 20 27 5d 27 20 63 68 61 72 61 63 74 65 72   a ']' character
25a84 20 63 61 6e 20 62 65 20 69 6e 63 6c 75 64 65 64   can be included
25a85 0a 2a 2a 20 69 6e 20 74 68 65 20 6c 69 73 74 20  .** in the list 
25a86 62 79 20 6d 61 6b 69 6e 67 20 69 74 20 74 68 65  by making it the
25a87 20 66 69 72 73 74 20 63 68 61 72 61 63 74 65 72   first character
25a88 20 61 66 74 65 72 20 27 5b 27 20 6f 72 20 27 5e   after '[' or '^
25a89 27 2e 20 20 41 0a 2a 2a 20 72 61 6e 67 65 20 6f  '.  A.** range o
25a8a 66 20 63 68 61 72 61 63 74 65 72 73 20 63 61 6e  f characters can
25a8b 20 62 65 20 73 70 65 63 69 66 69 65 64 20 75 73   be specified us
25a8c 69 6e 67 20 27 2d 27 2e 20 20 45 78 61 6d 70 6c  ing '-'.  Exampl
25a8d 65 3a 0a 2a 2a 20 22 5b 61 2d 7a 5d 22 20 6d 61  e:.** "[a-z]" ma
25a8e 74 63 68 65 73 20 61 6e 79 20 73 69 6e 67 6c 65  tches any single
25a8f 20 6c 6f 77 65 72 2d 63 61 73 65 20 6c 65 74 74   lower-case lett
25a90 65 72 2e 20 20 54 6f 20 6d 61 74 63 68 20 61 20  er.  To match a 
25a91 27 2d 27 2c 20 6d 61 6b 65 0a 2a 2a 20 69 74 20  '-', make.** it 
25a92 74 68 65 20 6c 61 73 74 20 63 68 61 72 61 63 74  the last charact
25a93 65 72 20 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a  er in the list..
25a94 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
25a95 6e 65 20 69 73 20 75 73 75 61 6c 6c 79 20 71 75  ne is usually qu
25a96 69 63 6b 2c 20 62 75 74 20 63 61 6e 20 62 65 20  ick, but can be 
25a97 4e 2a 2a 32 20 69 6e 20 74 68 65 20 77 6f 72 73  N**2 in the wors
25a98 74 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 48 69  t case..**.** Hi
25a99 6e 74 73 3a 20 74 6f 20 6d 61 74 63 68 20 27 2a  nts: to match '*
25a9a 27 20 6f 72 20 27 3f 27 2c 20 70 75 74 20 74 68  ' or '?', put th
25a9b 65 6d 20 69 6e 20 22 5b 5d 22 2e 20 20 4c 69 6b  em in "[]".  Lik
25a9c 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  e this:.**.**   
25a9d 20 20 20 20 20 20 61 62 63 5b 2a 5d 78 79 7a 20        abc[*]xyz 
25a9e 20 20 20 20 20 20 20 4d 61 74 63 68 65 73 20 22         Matches "
25a9f 61 62 63 2a 78 79 7a 22 20 6f 6e 6c 79 0a 2a 2f  abc*xyz" only.*/
25aa0 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 74 74  .static int patt
25aa1 65 72 6e 43 6f 6d 70 61 72 65 28 0a 20 20 63 6f  ernCompare(.  co
25aa2 6e 73 74 20 75 38 20 2a 7a 50 61 74 74 65 72 6e  nst u8 *zPattern
25aa3 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
25aa4 2a 20 54 68 65 20 67 6c 6f 62 20 70 61 74 74 65  * The glob patte
25aa5 72 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 38  rn */.  const u8
25aa6 20 2a 7a 53 74 72 69 6e 67 2c 20 20 20 20 20 20   *zString,      
25aa7 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
25aa8 73 74 72 69 6e 67 20 74 6f 20 63 6f 6d 70 61 72  string to compar
25aa9 65 20 61 67 61 69 6e 73 74 20 74 68 65 20 67 6c  e against the gl
25aaa 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 74  ob */.  const st
25aab 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f  ruct compareInfo
25aac 20 2a 70 49 6e 66 6f 2c 20 2f 2a 20 49 6e 66 6f   *pInfo, /* Info
25aad 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 68 6f  rmation about ho
25aae 77 20 74 6f 20 64 6f 20 74 68 65 20 63 6f 6d 70  w to do the comp
25aaf 61 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 69  are */.  const i
25ab0 6e 74 20 65 73 63 20 20 20 20 20 20 20 20 20 20  nt esc          
25ab1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
25ab2 20 65 73 63 61 70 65 20 63 68 61 72 61 63 74 65   escape characte
25ab3 72 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 2c  r */.){.  int c,
25ab4 20 63 32 3b 0a 20 20 69 6e 74 20 69 6e 76 65 72   c2;.  int inver
25ab5 74 3b 0a 20 20 69 6e 74 20 73 65 65 6e 3b 0a 20  t;.  int seen;. 
25ab6 20 75 38 20 6d 61 74 63 68 4f 6e 65 20 3d 20 70   u8 matchOne = p
25ab7 49 6e 66 6f 2d 3e 6d 61 74 63 68 4f 6e 65 3b 0a  Info->matchOne;.
25ab8 20 20 75 38 20 6d 61 74 63 68 41 6c 6c 20 3d 20    u8 matchAll = 
25ab9 70 49 6e 66 6f 2d 3e 6d 61 74 63 68 41 6c 6c 3b  pInfo->matchAll;
25aba 0a 20 20 75 38 20 6d 61 74 63 68 53 65 74 20 3d  .  u8 matchSet =
25abb 20 70 49 6e 66 6f 2d 3e 6d 61 74 63 68 53 65 74   pInfo->matchSet
25abc 3b 0a 20 20 75 38 20 6e 6f 43 61 73 65 20 3d 20  ;.  u8 noCase = 
25abd 70 49 6e 66 6f 2d 3e 6e 6f 43 61 73 65 3b 20 0a  pInfo->noCase; .
25abe 20 20 69 6e 74 20 70 72 65 76 45 73 63 61 70 65    int prevEscape
25abf 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54 72 75   = 0;     /* Tru
25ac0 65 20 69 66 20 74 68 65 20 70 72 65 76 69 6f 75  e if the previou
25ac1 73 20 63 68 61 72 61 63 74 65 72 20 77 61 73 20  s character was 
25ac2 27 65 73 63 61 70 65 27 20 2a 2f 0a 0a 20 20 77  'escape' */..  w
25ac3 68 69 6c 65 28 20 28 63 20 3d 20 73 71 6c 69 74  hile( (c = sqlit
25ac4 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 74 74  e3Utf8Read(zPatt
25ac5 65 72 6e 2c 26 7a 50 61 74 74 65 72 6e 29 29 21  ern,&zPattern))!
25ac6 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 21 70  =0 ){.    if( !p
25ac7 72 65 76 45 73 63 61 70 65 20 26 26 20 63 3d 3d  revEscape && c==
25ac8 6d 61 74 63 68 41 6c 6c 20 29 7b 0a 20 20 20 20  matchAll ){.    
25ac9 20 20 77 68 69 6c 65 28 20 28 63 3d 73 71 6c 69    while( (c=sqli
25aca 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 74  te3Utf8Read(zPat
25acb 74 65 72 6e 2c 26 7a 50 61 74 74 65 72 6e 29 29  tern,&zPattern))
25acc 20 3d 3d 20 6d 61 74 63 68 41 6c 6c 0a 20 20 20   == matchAll.   
25acd 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 63              || c
25ace 20 3d 3d 20 6d 61 74 63 68 4f 6e 65 20 29 7b 0a   == matchOne ){.
25acf 20 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d 6d          if( c==m
25ad0 61 74 63 68 4f 6e 65 20 26 26 20 73 71 6c 69 74  atchOne && sqlit
25ad1 65 33 55 74 66 38 52 65 61 64 28 7a 53 74 72 69  e3Utf8Read(zStri
25ad2 6e 67 2c 20 26 7a 53 74 72 69 6e 67 29 3d 3d 30  ng, &zString)==0
25ad3 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65   ){.          re
25ad4 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 20 20  turn 0;.        
25ad5 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
25ad6 69 66 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20  if( c==0 ){.    
25ad7 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
25ad8 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d      }else if( c=
25ad9 3d 65 73 63 20 29 7b 0a 20 20 20 20 20 20 20 20  =esc ){.        
25ada 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52  c = sqlite3Utf8R
25adb 65 61 64 28 7a 50 61 74 74 65 72 6e 2c 20 26 7a  ead(zPattern, &z
25adc 50 61 74 74 65 72 6e 29 3b 0a 20 20 20 20 20 20  Pattern);.      
25add 20 20 69 66 28 20 63 3d 3d 30 20 29 7b 0a 20 20    if( c==0 ){.  
25ade 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30          return 0
25adf 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
25ae0 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 6d    }else if( c==m
25ae1 61 74 63 68 53 65 74 20 29 7b 0a 20 20 20 20 20  atchSet ){.     
25ae2 20 20 20 61 73 73 65 72 74 28 20 65 73 63 3d 3d     assert( esc==
25ae3 30 20 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  0 );         /* 
25ae4 54 68 69 73 20 69 73 20 47 4c 4f 42 2c 20 6e 6f  This is GLOB, no
25ae5 74 20 4c 49 4b 45 20 2a 2f 0a 20 20 20 20 20 20  t LIKE */.      
25ae6 20 20 61 73 73 65 72 74 28 20 6d 61 74 63 68 53    assert( matchS
25ae7 65 74 3c 30 78 38 30 20 29 3b 20 20 2f 2a 20 27  et<0x80 );  /* '
25ae8 5b 27 20 69 73 20 61 20 73 69 6e 67 6c 65 2d 62  [' is a single-b
25ae9 79 74 65 20 63 68 61 72 61 63 74 65 72 20 2a 2f  yte character */
25aea 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
25aeb 2a 7a 53 74 72 69 6e 67 20 26 26 20 70 61 74 74  *zString && patt
25aec 65 72 6e 43 6f 6d 70 61 72 65 28 26 7a 50 61 74  ernCompare(&zPat
25aed 74 65 72 6e 5b 2d 31 5d 2c 7a 53 74 72 69 6e 67  tern[-1],zString
25aee 2c 70 49 6e 66 6f 2c 65 73 63 29 3d 3d 30 20 29  ,pInfo,esc)==0 )
25aef 7b 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49  {.          SQLI
25af0 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 53 74  TE_SKIP_UTF8(zSt
25af1 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 20 20 7d  ring);.        }
25af2 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
25af3 2a 7a 53 74 72 69 6e 67 21 3d 30 3b 0a 20 20 20  *zString!=0;.   
25af4 20 20 20 7d 0a 20 20 20 20 20 20 77 68 69 6c 65     }.      while
25af5 28 20 28 63 32 20 3d 20 73 71 6c 69 74 65 33 55  ( (c2 = sqlite3U
25af6 74 66 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c  tf8Read(zString,
25af7 26 7a 53 74 72 69 6e 67 29 29 21 3d 30 20 29 7b  &zString))!=0 ){
25af8 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 6f 43  .        if( noC
25af9 61 73 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ase ){.         
25afa 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65   GlogUpperToLowe
25afb 72 28 63 32 29 3b 0a 20 20 20 20 20 20 20 20 20  r(c2);.         
25afc 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65   GlogUpperToLowe
25afd 72 28 63 29 3b 0a 20 20 20 20 20 20 20 20 20 20  r(c);.          
25afe 77 68 69 6c 65 28 20 63 32 20 21 3d 20 30 20 26  while( c2 != 0 &
25aff 26 20 63 32 20 21 3d 20 63 20 29 7b 0a 20 20 20  & c2 != c ){.   
25b00 20 20 20 20 20 20 20 20 20 63 32 20 3d 20 73 71           c2 = sq
25b01 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 53  lite3Utf8Read(zS
25b02 74 72 69 6e 67 2c 20 26 7a 53 74 72 69 6e 67 29  tring, &zString)
25b03 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 47 6c  ;.            Gl
25b04 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28 63  ogUpperToLower(c
25b05 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  2);.          }.
25b06 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
25b07 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20           while( 
25b08 63 32 20 21 3d 20 30 20 26 26 20 63 32 20 21 3d  c2 != 0 && c2 !=
25b09 20 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20   c ){.          
25b0a 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74    c2 = sqlite3Ut
25b0b 66 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 20  f8Read(zString, 
25b0c 26 7a 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20  &zString);.     
25b0d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
25b0e 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 32 3d  .        if( c2=
25b0f 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
25b10 20 20 20 20 20 20 20 69 66 28 20 70 61 74 74 65         if( patte
25b11 72 6e 43 6f 6d 70 61 72 65 28 7a 50 61 74 74 65  rnCompare(zPatte
25b12 72 6e 2c 7a 53 74 72 69 6e 67 2c 70 49 6e 66 6f  rn,zString,pInfo
25b13 2c 65 73 63 29 20 29 20 72 65 74 75 72 6e 20 31  ,esc) ) return 1
25b14 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
25b15 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 65  return 0;.    }e
25b16 6c 73 65 20 69 66 28 20 21 70 72 65 76 45 73 63  lse if( !prevEsc
25b17 61 70 65 20 26 26 20 63 3d 3d 6d 61 74 63 68 4f  ape && c==matchO
25b18 6e 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ne ){.      if( 
25b19 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28  sqlite3Utf8Read(
25b1a 7a 53 74 72 69 6e 67 2c 20 26 7a 53 74 72 69 6e  zString, &zStrin
25b1b 67 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  g)==0 ){.       
25b1c 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20   return 0;.     
25b1d 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28   }.    }else if(
25b1e 20 63 3d 3d 6d 61 74 63 68 53 65 74 20 29 7b 0a   c==matchSet ){.
25b1f 20 20 20 20 20 20 69 6e 74 20 70 72 69 6f 72 5f        int prior_
25b20 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 61 73 73  c = 0;.      ass
25b21 65 72 74 28 20 65 73 63 3d 3d 30 20 29 3b 20 20  ert( esc==0 );  
25b22 20 20 2f 2a 20 54 68 69 73 20 6f 6e 6c 79 20 6f    /* This only o
25b23 63 63 75 72 73 20 66 6f 72 20 47 4c 4f 42 2c 20  ccurs for GLOB, 
25b24 6e 6f 74 20 4c 49 4b 45 20 2a 2f 0a 20 20 20 20  not LIKE */.    
25b25 20 20 73 65 65 6e 20 3d 20 30 3b 0a 20 20 20 20    seen = 0;.    
25b26 20 20 69 6e 76 65 72 74 20 3d 20 30 3b 0a 20 20    invert = 0;.  
25b27 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55      c = sqlite3U
25b28 74 66 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c  tf8Read(zString,
25b29 20 26 7a 53 74 72 69 6e 67 29 3b 0a 20 20 20 20   &zString);.    
25b2a 20 20 69 66 28 20 63 3d 3d 30 20 29 20 72 65 74    if( c==0 ) ret
25b2b 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 63 32 20  urn 0;.      c2 
25b2c 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  = sqlite3Utf8Rea
25b2d 64 28 7a 50 61 74 74 65 72 6e 2c 20 26 7a 50 61  d(zPattern, &zPa
25b2e 74 74 65 72 6e 29 3b 0a 20 20 20 20 20 20 69 66  ttern);.      if
25b2f 28 20 63 32 3d 3d 27 5e 27 20 29 7b 0a 20 20 20  ( c2=='^' ){.   
25b30 20 20 20 20 20 69 6e 76 65 72 74 20 3d 20 31 3b       invert = 1;
25b31 0a 20 20 20 20 20 20 20 20 63 32 20 3d 20 73 71  .        c2 = sq
25b32 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 50  lite3Utf8Read(zP
25b33 61 74 74 65 72 6e 2c 20 26 7a 50 61 74 74 65 72  attern, &zPatter
25b34 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  n);.      }.    
25b35 20 20 69 66 28 20 63 32 3d 3d 27 5d 27 20 29 7b    if( c2==']' ){
25b36 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d  .        if( c==
25b37 27 5d 27 20 29 20 73 65 65 6e 20 3d 20 31 3b 0a  ']' ) seen = 1;.
25b38 20 20 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c          c2 = sql
25b39 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61  ite3Utf8Read(zPa
25b3a 74 74 65 72 6e 2c 20 26 7a 50 61 74 74 65 72 6e  ttern, &zPattern
25b3b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
25b3c 20 77 68 69 6c 65 28 20 63 32 20 26 26 20 63 32   while( c2 && c2
25b3d 21 3d 27 5d 27 20 29 7b 0a 20 20 20 20 20 20 20  !=']' ){.       
25b3e 20 69 66 28 20 63 32 3d 3d 27 2d 27 20 26 26 20   if( c2=='-' && 
25b3f 7a 50 61 74 74 65 72 6e 5b 30 5d 21 3d 27 5d 27  zPattern[0]!=']'
25b40 20 26 26 20 7a 50 61 74 74 65 72 6e 5b 30 5d 21   && zPattern[0]!
25b41 3d 30 20 26 26 20 70 72 69 6f 72 5f 63 3e 30 20  =0 && prior_c>0 
25b42 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 32 20  ){.          c2 
25b43 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  = sqlite3Utf8Rea
25b44 64 28 7a 50 61 74 74 65 72 6e 2c 20 26 7a 50 61  d(zPattern, &zPa
25b45 74 74 65 72 6e 29 3b 0a 20 20 20 20 20 20 20 20  ttern);.        
25b46 20 20 69 66 28 20 63 3e 3d 70 72 69 6f 72 5f 63    if( c>=prior_c
25b47 20 26 26 20 63 3c 3d 63 32 20 29 20 73 65 65 6e   && c<=c2 ) seen
25b48 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20   = 1;.          
25b49 70 72 69 6f 72 5f 63 20 3d 20 30 3b 0a 20 20 20  prior_c = 0;.   
25b4a 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
25b4b 20 20 20 20 20 20 69 66 28 20 63 3d 3d 63 32 20        if( c==c2 
25b4c 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  ){.            s
25b4d 65 65 6e 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  een = 1;.       
25b4e 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70     }.          p
25b4f 72 69 6f 72 5f 63 20 3d 20 63 32 3b 0a 20 20 20  rior_c = c2;.   
25b50 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63       }.        c
25b51 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52  2 = sqlite3Utf8R
25b52 65 61 64 28 7a 50 61 74 74 65 72 6e 2c 20 26 7a  ead(zPattern, &z
25b53 50 61 74 74 65 72 6e 29 3b 0a 20 20 20 20 20 20  Pattern);.      
25b54 7d 0a 20 20 20 20 20 20 69 66 28 20 63 32 3d 3d  }.      if( c2==
25b55 30 20 7c 7c 20 28 73 65 65 6e 20 5e 20 69 6e 76  0 || (seen ^ inv
25b56 65 72 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ert)==0 ){.     
25b57 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20     return 0;.   
25b58 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69     }.    }else i
25b59 66 28 20 65 73 63 3d 3d 63 20 26 26 20 21 70 72  f( esc==c && !pr
25b5a 65 76 45 73 63 61 70 65 20 29 7b 0a 20 20 20 20  evEscape ){.    
25b5b 20 20 70 72 65 76 45 73 63 61 70 65 20 3d 20 31    prevEscape = 1
25b5c 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
25b5d 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33 55     c2 = sqlite3U
25b5e 74 66 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c  tf8Read(zString,
25b5f 20 26 7a 53 74 72 69 6e 67 29 3b 0a 20 20 20 20   &zString);.    
25b60 20 20 69 66 28 20 6e 6f 43 61 73 65 20 29 7b 0a    if( noCase ){.
25b61 20 20 20 20 20 20 20 20 47 6c 6f 67 55 70 70 65          GlogUppe
25b62 72 54 6f 4c 6f 77 65 72 28 63 29 3b 0a 20 20 20  rToLower(c);.   
25b63 20 20 20 20 20 47 6c 6f 67 55 70 70 65 72 54 6f       GlogUpperTo
25b64 4c 6f 77 65 72 28 63 32 29 3b 0a 20 20 20 20 20  Lower(c2);.     
25b65 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63 21 3d   }.      if( c!=
25b66 63 32 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  c2 ){.        re
25b67 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a  turn 0;.      }.
25b68 20 20 20 20 20 20 70 72 65 76 45 73 63 61 70 65        prevEscape
25b69 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 0;.    }.  }.
25b6a 20 20 72 65 74 75 72 6e 20 2a 7a 53 74 72 69 6e    return *zStrin
25b6b 67 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  g==0;.}../*.** C
25b6c 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20  ount the number 
25b6d 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20 74 68  of times that th
25b6e 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20  e LIKE operator 
25b6f 28 6f 72 20 47 4c 4f 42 20 77 68 69 63 68 20 69  (or GLOB which i
25b70 73 0a 2a 2a 20 6a 75 73 74 20 61 20 76 61 72 69  s.** just a vari
25b71 61 74 69 6f 6e 20 6f 66 20 4c 49 4b 45 29 20 67  ation of LIKE) g
25b72 65 74 73 20 63 61 6c 6c 65 64 2e 20 20 54 68 69  ets called.  Thi
25b73 73 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65  s is used for te
25b74 73 74 69 6e 67 0a 2a 2a 20 6f 6e 6c 79 2e 0a 2a  sting.** only..*
25b75 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
25b76 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20  TEST.SQLITE_API 
25b77 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c 69 6b 65  int sqlite3_like
25b78 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64  _count = 0;.#end
25b79 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  if.../*.** Imple
25b7a 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
25b7b 20 6c 69 6b 65 28 29 20 53 51 4c 20 66 75 6e 63   like() SQL func
25b7c 74 69 6f 6e 2e 20 20 54 68 69 73 20 66 75 6e 63  tion.  This func
25b7d 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 0a  tion implements.
25b7e 2a 2a 20 74 68 65 20 62 75 69 6c 64 2d 69 6e 20  ** the build-in 
25b7f 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 2e 20 20  LIKE operator.  
25b80 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  The first argume
25b81 6e 74 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69  nt to the functi
25b82 6f 6e 20 69 73 20 74 68 65 0a 2a 2a 20 70 61 74  on is the.** pat
25b83 74 65 72 6e 20 61 6e 64 20 74 68 65 20 73 65 63  tern and the sec
25b84 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ond argument is 
25b85 74 68 65 20 73 74 72 69 6e 67 2e 20 20 53 6f 2c  the string.  So,
25b86 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65   the SQL stateme
25b87 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  nts:.**.**      
25b88 20 41 20 4c 49 4b 45 20 42 0a 2a 2a 0a 2a 2a 20   A LIKE B.**.** 
25b89 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61  is implemented a
25b8a 73 20 6c 69 6b 65 28 42 2c 41 29 2e 0a 2a 2a 0a  s like(B,A)..**.
25b8b 2a 2a 20 54 68 69 73 20 73 61 6d 65 20 66 75 6e  ** This same fun
25b8c 63 74 69 6f 6e 20 28 77 69 74 68 20 61 20 64 69  ction (with a di
25b8d 66 66 65 72 65 6e 74 20 63 6f 6d 70 61 72 65 49  fferent compareI
25b8e 6e 66 6f 20 73 74 72 75 63 74 75 72 65 29 20 63  nfo structure) c
25b8f 6f 6d 70 75 74 65 73 0a 2a 2a 20 74 68 65 20 47  omputes.** the G
25b90 4c 4f 42 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f  LOB operator..*/
25b91 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 69 6b  .static void lik
25b92 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  eFunc(.  sqlite3
25b93 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
25b94 74 2c 20 0a 20 20 69 6e 74 20 61 72 67 63 2c 20  t, .  int argc, 
25b95 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
25b96 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e   **argv.){.  con
25b97 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
25b98 20 2a 7a 41 2c 20 2a 7a 42 3b 0a 20 20 69 6e 74   *zA, *zB;.  int
25b99 20 65 73 63 61 70 65 20 3d 20 30 3b 0a 20 20 69   escape = 0;.  i
25b9a 6e 74 20 6e 50 61 74 3b 0a 20 20 73 71 6c 69 74  nt nPat;.  sqlit
25b9b 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33  e3 *db = sqlite3
25b9c 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
25b9d 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 0a 20 20  le(context);..  
25b9e 7a 42 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  zB = sqlite3_val
25b9f 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
25ba0 3b 0a 20 20 7a 41 20 3d 20 73 71 6c 69 74 65 33  ;.  zA = sqlite3
25ba1 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
25ba2 5b 31 5d 29 3b 0a 0a 20 20 2f 2a 20 4c 69 6d 69  [1]);..  /* Limi
25ba3 74 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20  t the length of 
25ba4 74 68 65 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42  the LIKE or GLOB
25ba5 20 70 61 74 74 65 72 6e 20 74 6f 20 61 76 6f 69   pattern to avoi
25ba6 64 20 70 72 6f 62 6c 65 6d 73 0a 20 20 2a 2a 20  d problems.  ** 
25ba7 6f 66 20 64 65 65 70 20 72 65 63 75 72 73 69 6f  of deep recursio
25ba8 6e 20 61 6e 64 20 4e 2a 4e 20 62 65 68 61 76 69  n and N*N behavi
25ba9 6f 72 20 69 6e 20 70 61 74 74 65 72 6e 43 6f 6d  or in patternCom
25baa 70 61 72 65 28 29 2e 0a 20 20 2a 2f 0a 20 20 6e  pare()..  */.  n
25bab 50 61 74 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  Pat = sqlite3_va
25bac 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30  lue_bytes(argv[0
25bad 5d 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20  ]);.  testcase( 
25bae 6e 50 61 74 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74  nPat==db->aLimit
25baf 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 49  [SQLITE_LIMIT_LI
25bb0 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54  KE_PATTERN_LENGT
25bb1 48 5d 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65  H] );.  testcase
25bb2 28 20 6e 50 61 74 3d 3d 64 62 2d 3e 61 4c 69 6d  ( nPat==db->aLim
25bb3 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
25bb4 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e  LIKE_PATTERN_LEN
25bb5 47 54 48 5d 2b 31 20 29 3b 0a 20 20 69 66 28 20  GTH]+1 );.  if( 
25bb6 6e 50 61 74 20 3e 20 64 62 2d 3e 61 4c 69 6d 69  nPat > db->aLimi
25bb7 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
25bb8 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47  IKE_PATTERN_LENG
25bb9 54 48 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  TH] ){.    sqlit
25bba 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
25bbb 63 6f 6e 74 65 78 74 2c 20 22 4c 49 4b 45 20 6f  context, "LIKE o
25bbc 72 20 47 4c 4f 42 20 70 61 74 74 65 72 6e 20 74  r GLOB pattern t
25bbd 6f 6f 20 63 6f 6d 70 6c 65 78 22 2c 20 2d 31 29  oo complex", -1)
25bbe 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
25bbf 7d 0a 20 20 61 73 73 65 72 74 28 20 7a 42 3d 3d  }.  assert( zB==
25bc0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
25bc1 78 74 28 61 72 67 76 5b 30 5d 29 20 29 3b 20 20  xt(argv[0]) );  
25bc2 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 64 69 64 20  /* Encoding did 
25bc3 6e 6f 74 20 63 68 61 6e 67 65 20 2a 2f 0a 0a 20  not change */.. 
25bc4 20 69 66 28 20 61 72 67 63 3d 3d 33 20 29 7b 0a   if( argc==3 ){.
25bc5 20 20 20 20 2f 2a 20 54 68 65 20 65 73 63 61 70      /* The escap
25bc6 65 20 63 68 61 72 61 63 74 65 72 20 73 74 72 69  e character stri
25bc7 6e 67 20 6d 75 73 74 20 63 6f 6e 73 69 73 74 20  ng must consist 
25bc8 6f 66 20 61 20 73 69 6e 67 6c 65 20 55 54 46 2d  of a single UTF-
25bc9 38 20 63 68 61 72 61 63 74 65 72 2e 0a 20 20 20  8 character..   
25bca 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 72   ** Otherwise, r
25bcb 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a  eturn an error..
25bcc 20 20 20 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74      */.    const
25bcd 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
25bce 7a 45 73 63 20 3d 20 73 71 6c 69 74 65 33 5f 76  zEsc = sqlite3_v
25bcf 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 32  alue_text(argv[2
25bd0 5d 29 3b 0a 20 20 20 20 69 66 28 20 7a 45 73 63  ]);.    if( zEsc
25bd1 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
25bd2 20 20 69 66 28 20 73 71 6c 69 74 65 33 55 74 66    if( sqlite3Utf
25bd3 38 43 68 61 72 4c 65 6e 28 28 63 68 61 72 2a 29  8CharLen((char*)
25bd4 7a 45 73 63 2c 20 2d 31 29 21 3d 31 20 29 7b 0a  zEsc, -1)!=1 ){.
25bd5 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
25bd6 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65  sult_error(conte
25bd7 78 74 2c 20 0a 20 20 20 20 20 20 20 20 20 20 22  xt, .          "
25bd8 45 53 43 41 50 45 20 65 78 70 72 65 73 73 69 6f  ESCAPE expressio
25bd9 6e 20 6d 75 73 74 20 62 65 20 61 20 73 69 6e 67  n must be a sing
25bda 6c 65 20 63 68 61 72 61 63 74 65 72 22 2c 20 2d  le character", -
25bdb 31 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  1);.      return
25bdc 3b 0a 20 20 20 20 7d 0a 20 20 20 20 65 73 63 61  ;.    }.    esca
25bdd 70 65 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38  pe = sqlite3Utf8
25bde 52 65 61 64 28 7a 45 73 63 2c 20 26 7a 45 73 63  Read(zEsc, &zEsc
25bdf 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 41 20  );.  }.  if( zA 
25be0 26 26 20 7a 42 20 29 7b 0a 20 20 20 20 73 74 72  && zB ){.    str
25be1 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20  uct compareInfo 
25be2 2a 70 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33  *pInfo = sqlite3
25be3 5f 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65  _user_data(conte
25be4 78 74 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  xt);.#ifdef SQLI
25be5 54 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c 69  TE_TEST.    sqli
25be6 74 65 33 5f 6c 69 6b 65 5f 63 6f 75 6e 74 2b 2b  te3_like_count++
25be7 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 0a 20 20  ;.#endif.    .  
25be8 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
25be9 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 70 61  _int(context, pa
25bea 74 74 65 72 6e 43 6f 6d 70 61 72 65 28 7a 42 2c  tternCompare(zB,
25beb 20 7a 41 2c 20 70 49 6e 66 6f 2c 20 65 73 63 61   zA, pInfo, esca
25bec 70 65 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  pe));.  }.}../*.
25bed 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
25bee 6e 20 6f 66 20 74 68 65 20 4e 55 4c 4c 49 46 28  n of the NULLIF(
25bef 78 2c 79 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20  x,y) function.  
25bf0 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 74 68  The result is th
25bf1 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d  e first.** argum
25bf2 65 6e 74 20 69 66 20 74 68 65 20 61 72 67 75 6d  ent if the argum
25bf3 65 6e 74 73 20 61 72 65 20 64 69 66 66 65 72 65  ents are differe
25bf4 6e 74 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20  nt.  The result 
25bf5 69 73 20 4e 55 4c 4c 20 69 66 20 74 68 65 0a 2a  is NULL if the.*
25bf6 2a 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20  * arguments are 
25bf7 65 71 75 61 6c 20 74 6f 20 65 61 63 68 20 6f 74  equal to each ot
25bf8 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  her..*/.static v
25bf9 6f 69 64 20 6e 75 6c 6c 69 66 46 75 6e 63 28 0a  oid nullifFunc(.
25bfa 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
25bfb 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
25bfc 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c  t NotUsed,.  sql
25bfd 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
25bfe 76 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a  v.){.  CollSeq *
25bff 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47  pColl = sqlite3G
25c00 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 63 6f  etFuncCollSeq(co
25c01 6e 74 65 78 74 29 3b 0a 20 20 55 4e 55 53 45 44  ntext);.  UNUSED
25c02 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73  _PARAMETER(NotUs
25c03 65 64 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  ed);.  if( sqlit
25c04 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 61 72 67  e3MemCompare(arg
25c05 76 5b 30 5d 2c 20 61 72 67 76 5b 31 5d 2c 20 70  v[0], argv[1], p
25c06 43 6f 6c 6c 29 21 3d 30 20 29 7b 0a 20 20 20 20  Coll)!=0 ){.    
25c07 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76  sqlite3_result_v
25c08 61 6c 75 65 28 63 6f 6e 74 65 78 74 2c 20 61 72  alue(context, ar
25c09 67 76 5b 30 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  gv[0]);.  }.}../
25c0a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
25c0b 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74  ion of the sqlit
25c0c 65 5f 76 65 72 73 69 6f 6e 28 29 20 66 75 6e 63  e_version() func
25c0d 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 73 75 6c  tion.  The resul
25c0e 74 20 69 73 20 74 68 65 20 76 65 72 73 69 6f 6e  t is the version
25c0f 0a 2a 2a 20 6f 66 20 74 68 65 20 53 51 4c 69 74  .** of the SQLit
25c10 65 20 6c 69 62 72 61 72 79 20 74 68 61 74 20 69  e library that i
25c11 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2f 0a 73 74  s running..*/.st
25c12 61 74 69 63 20 76 6f 69 64 20 76 65 72 73 69 6f  atic void versio
25c13 6e 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  nFunc(.  sqlite3
25c14 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
25c15 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64  t,.  int NotUsed
25c16 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
25c17 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a  e **NotUsed2.){.
25c18 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
25c19 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74  ER2(NotUsed, Not
25c1a 55 73 65 64 32 29 3b 0a 20 20 73 71 6c 69 74 65  Used2);.  sqlite
25c1b 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
25c1c 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f 76  ntext, sqlite3_v
25c1d 65 72 73 69 6f 6e 2c 20 2d 31 2c 20 53 51 4c 49  ersion, -1, SQLI
25c1e 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a 2f  TE_STATIC);.}../
25c1f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
25c20 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74  ion of the sqlit
25c21 65 5f 73 6f 75 72 63 65 5f 69 64 28 29 20 66 75  e_source_id() fu
25c22 6e 63 74 69 6f 6e 2e 20 54 68 65 20 72 65 73 75  nction. The resu
25c23 6c 74 20 69 73 20 61 20 73 74 72 69 6e 67 0a 2a  lt is a string.*
25c24 2a 20 74 68 61 74 20 69 64 65 6e 74 69 66 69 65  * that identifie
25c25 73 20 74 68 65 20 70 61 72 74 69 63 75 6c 61 72  s the particular
25c26 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20   version of the 
25c27 73 6f 75 72 63 65 20 63 6f 64 65 20 75 73 65 64  source code used
25c28 20 74 6f 20 62 75 69 6c 64 0a 2a 2a 20 53 51 4c   to build.** SQL
25c29 69 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ite..*/.static v
25c2a 6f 69 64 20 73 6f 75 72 63 65 69 64 46 75 6e 63  oid sourceidFunc
25c2b 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
25c2c 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
25c2d 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73  int NotUsed,.  s
25c2e 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e  qlite3_value **N
25c2f 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55  otUsed2.){.  UNU
25c30 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e  SED_PARAMETER2(N
25c31 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32  otUsed, NotUsed2
25c32 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  );.  sqlite3_res
25c33 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
25c34 2c 20 53 51 4c 49 54 45 5f 53 4f 55 52 43 45 5f  , SQLITE_SOURCE_
25c35 49 44 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53  ID, -1, SQLITE_S
25c36 54 41 54 49 43 29 3b 0a 7d 0a 0a 2f 2a 20 41 72  TATIC);.}../* Ar
25c37 72 61 79 20 66 6f 72 20 63 6f 6e 76 65 72 74 69  ray for converti
25c38 6e 67 20 66 72 6f 6d 20 68 61 6c 66 2d 62 79 74  ng from half-byt
25c39 65 73 20 28 6e 79 62 62 6c 65 73 29 20 69 6e 74  es (nybbles) int
25c3a 6f 20 41 53 43 49 49 20 68 65 78 0a 2a 2a 20 64  o ASCII hex.** d
25c3b 69 67 69 74 73 2e 20 2a 2f 0a 73 74 61 74 69 63  igits. */.static
25c3c 20 63 6f 6e 73 74 20 63 68 61 72 20 68 65 78 64   const char hexd
25c3d 69 67 69 74 73 5b 5d 20 3d 20 7b 0a 20 20 27 30  igits[] = {.  '0
25c3e 27 2c 20 27 31 27 2c 20 27 32 27 2c 20 27 33 27  ', '1', '2', '3'
25c3f 2c 20 27 34 27 2c 20 27 35 27 2c 20 27 36 27 2c  , '4', '5', '6',
25c40 20 27 37 27 2c 0a 20 20 27 38 27 2c 20 27 39 27   '7',.  '8', '9'
25c41 2c 20 27 41 27 2c 20 27 42 27 2c 20 27 43 27 2c  , 'A', 'B', 'C',
25c42 20 27 44 27 2c 20 27 45 27 2c 20 27 46 27 20 0a   'D', 'E', 'F' .
25c43 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 58 50 45 52 49  };../*.** EXPERI
25c44 4d 45 4e 54 41 4c 20 2d 20 54 68 69 73 20 69 73  MENTAL - This is
25c45 20 6e 6f 74 20 61 6e 20 6f 66 66 69 63 69 61 6c   not an official
25c46 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20   function.  The 
25c47 69 6e 74 65 72 66 61 63 65 20 6d 61 79 0a 2a 2a  interface may.**
25c48 20 63 68 61 6e 67 65 2e 20 20 54 68 69 73 20 66   change.  This f
25c49 75 6e 63 74 69 6f 6e 20 6d 61 79 20 64 69 73 61  unction may disa
25c4a 70 70 65 61 72 2e 20 20 44 6f 20 6e 6f 74 20 77  ppear.  Do not w
25c4b 72 69 74 65 20 63 6f 64 65 20 74 68 61 74 20 64  rite code that d
25c4c 65 70 65 6e 64 73 0a 2a 2a 20 6f 6e 20 74 68 69  epends.** on thi
25c4d 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  s function..**.*
25c4e 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
25c4f 20 6f 66 20 74 68 65 20 51 55 4f 54 45 28 29 20   of the QUOTE() 
25c50 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69 73 20  function.  This 
25c51 66 75 6e 63 74 69 6f 6e 20 74 61 6b 65 73 20 61  function takes a
25c52 20 73 69 6e 67 6c 65 0a 2a 2a 20 61 72 67 75 6d   single.** argum
25c53 65 6e 74 2e 20 20 49 66 20 74 68 65 20 61 72 67  ent.  If the arg
25c54 75 6d 65 6e 74 20 69 73 20 6e 75 6d 65 72 69 63  ument is numeric
25c55 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  , the return val
25c56 75 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 61  ue is the same a
25c57 73 0a 2a 2a 20 74 68 65 20 61 72 67 75 6d 65 6e  s.** the argumen
25c58 74 2e 20 20 49 66 20 74 68 65 20 61 72 67 75 6d  t.  If the argum
25c59 65 6e 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65  ent is NULL, the
25c5a 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73   return value is
25c5b 20 74 68 65 20 73 74 72 69 6e 67 0a 2a 2a 20 22   the string.** "
25c5c 4e 55 4c 4c 22 2e 20 20 4f 74 68 65 72 77 69 73  NULL".  Otherwis
25c5d 65 2c 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20  e, the argument 
25c5e 69 73 20 65 6e 63 6c 6f 73 65 64 20 69 6e 20 73  is enclosed in s
25c5f 69 6e 67 6c 65 20 71 75 6f 74 65 73 20 77 69 74  ingle quotes wit
25c60 68 0a 2a 2a 20 73 69 6e 67 6c 65 2d 71 75 6f 74  h.** single-quot
25c61 65 20 65 73 63 61 70 65 73 2e 0a 2a 2f 0a 73 74  e escapes..*/.st
25c62 61 74 69 63 20 76 6f 69 64 20 71 75 6f 74 65 46  atic void quoteF
25c63 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  unc(sqlite3_cont
25c64 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e  ext *context, in
25c65 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
25c66 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20  value **argv){. 
25c67 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31   assert( argc==1
25c68 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52   );.  UNUSED_PAR
25c69 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20  AMETER(argc);.  
25c6a 73 77 69 74 63 68 28 20 73 71 6c 69 74 65 33 5f  switch( sqlite3_
25c6b 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b  value_type(argv[
25c6c 30 5d 29 20 29 7b 0a 20 20 20 20 63 61 73 65 20  0]) ){.    case 
25c6d 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 0a  SQLITE_INTEGER:.
25c6e 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
25c6f 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 73  FLOAT: {.      s
25c70 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61  qlite3_result_va
25c71 6c 75 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67  lue(context, arg
25c72 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 62 72 65  v[0]);.      bre
25c73 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
25c74 73 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a 20  se SQLITE_BLOB: 
25c75 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54  {.      char *zT
25c76 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 63  ext = 0;.      c
25c77 68 61 72 20 63 6f 6e 73 74 20 2a 7a 42 6c 6f 62  har const *zBlob
25c78 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
25c79 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 3b 0a  _blob(argv[0]);.
25c7a 20 20 20 20 20 20 69 6e 74 20 6e 42 6c 6f 62 20        int nBlob 
25c7b 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
25c7c 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a  bytes(argv[0]);.
25c7d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 7a 42        assert( zB
25c7e 6c 6f 62 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c  lob==sqlite3_val
25c7f 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29  ue_blob(argv[0])
25c80 20 29 3b 20 2f 2a 20 4e 6f 20 65 6e 63 6f 64 69   ); /* No encodi
25c81 6e 67 20 63 68 61 6e 67 65 20 2a 2f 0a 20 20 20  ng change */.   
25c82 20 20 20 7a 54 65 78 74 20 3d 20 28 63 68 61 72     zText = (char
25c83 20 2a 29 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63   *)contextMalloc
25c84 28 63 6f 6e 74 65 78 74 2c 20 28 32 2a 28 69 36  (context, (2*(i6
25c85 34 29 6e 42 6c 6f 62 29 2b 34 29 3b 20 0a 20 20  4)nBlob)+4); .  
25c86 20 20 20 20 69 66 28 20 7a 54 65 78 74 20 29 7b      if( zText ){
25c87 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 3b 0a  .        int i;.
25c88 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b          for(i=0;
25c89 20 69 3c 6e 42 6c 6f 62 3b 20 69 2b 2b 29 7b 0a   i<nBlob; i++){.
25c8a 20 20 20 20 20 20 20 20 20 20 7a 54 65 78 74 5b            zText[
25c8b 28 69 2a 32 29 2b 32 5d 20 3d 20 68 65 78 64 69  (i*2)+2] = hexdi
25c8c 67 69 74 73 5b 28 7a 42 6c 6f 62 5b 69 5d 3e 3e  gits[(zBlob[i]>>
25c8d 34 29 26 30 78 30 46 5d 3b 0a 20 20 20 20 20 20  4)&0x0F];.      
25c8e 20 20 20 20 7a 54 65 78 74 5b 28 69 2a 32 29 2b      zText[(i*2)+
25c8f 33 5d 20 3d 20 68 65 78 64 69 67 69 74 73 5b 28  3] = hexdigits[(
25c90 7a 42 6c 6f 62 5b 69 5d 29 26 30 78 30 46 5d 3b  zBlob[i])&0x0F];
25c91 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
25c92 20 20 20 7a 54 65 78 74 5b 28 6e 42 6c 6f 62 2a     zText[(nBlob*
25c93 32 29 2b 32 5d 20 3d 20 27 5c 27 27 3b 0a 20 20  2)+2] = '\'';.  
25c94 20 20 20 20 20 20 7a 54 65 78 74 5b 28 6e 42 6c        zText[(nBl
25c95 6f 62 2a 32 29 2b 33 5d 20 3d 20 27 5c 30 27 3b  ob*2)+3] = '\0';
25c96 0a 20 20 20 20 20 20 20 20 7a 54 65 78 74 5b 30  .        zText[0
25c97 5d 20 3d 20 27 58 27 3b 0a 20 20 20 20 20 20 20  ] = 'X';.       
25c98 20 7a 54 65 78 74 5b 31 5d 20 3d 20 27 5c 27 27   zText[1] = '\''
25c99 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
25c9a 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
25c9b 6e 74 65 78 74 2c 20 7a 54 65 78 74 2c 20 2d 31  ntext, zText, -1
25c9c 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  , SQLITE_TRANSIE
25c9d 4e 54 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  NT);.        sql
25c9e 69 74 65 33 5f 66 72 65 65 28 7a 54 65 78 74 29  ite3_free(zText)
25c9f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
25ca0 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
25ca1 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 58   case SQLITE_TEX
25ca2 54 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  T: {.      int i
25ca3 2c 6a 3b 0a 20 20 20 20 20 20 75 36 34 20 6e 3b  ,j;.      u64 n;
25ca4 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 75 6e 73  .      const uns
25ca5 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 41 72 67  igned char *zArg
25ca6 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
25ca7 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a  _text(argv[0]);.
25ca8 20 20 20 20 20 20 63 68 61 72 20 2a 7a 3b 0a 0a        char *z;..
25ca9 20 20 20 20 20 20 69 66 28 20 7a 41 72 67 3d 3d        if( zArg==
25caa 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  0 ) return;.    
25cab 20 20 66 6f 72 28 69 3d 30 2c 20 6e 3d 30 3b 20    for(i=0, n=0; 
25cac 7a 41 72 67 5b 69 5d 3b 20 69 2b 2b 29 7b 20 69  zArg[i]; i++){ i
25cad 66 28 20 7a 41 72 67 5b 69 5d 3d 3d 27 5c 27 27  f( zArg[i]=='\''
25cae 20 29 20 6e 2b 2b 3b 20 7d 0a 20 20 20 20 20 20   ) n++; }.      
25caf 7a 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f  z = contextMallo
25cb0 63 28 63 6f 6e 74 65 78 74 2c 20 28 28 69 36 34  c(context, ((i64
25cb1 29 69 29 2b 28 28 69 36 34 29 6e 29 2b 33 29 3b  )i)+((i64)n)+3);
25cb2 0a 20 20 20 20 20 20 69 66 28 20 7a 20 29 7b 0a  .      if( z ){.
25cb3 20 20 20 20 20 20 20 20 7a 5b 30 5d 20 3d 20 27          z[0] = '
25cb4 5c 27 27 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  \'';.        for
25cb5 28 69 3d 30 2c 20 6a 3d 31 3b 20 7a 41 72 67 5b  (i=0, j=1; zArg[
25cb6 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  i]; i++){.      
25cb7 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 41 72      z[j++] = zAr
25cb8 67 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 20 20  g[i];.          
25cb9 69 66 28 20 7a 41 72 67 5b 69 5d 3d 3d 27 5c 27  if( zArg[i]=='\'
25cba 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ' ){.           
25cbb 20 7a 5b 6a 2b 2b 5d 20 3d 20 27 5c 27 27 3b 0a   z[j++] = '\'';.
25cbc 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
25cbd 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7a 5b      }.        z[
25cbe 6a 2b 2b 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20  j++] = '\'';.   
25cbf 20 20 20 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20       z[j] = 0;. 
25cc0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72         sqlite3_r
25cc1 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65  esult_text(conte
25cc2 78 74 2c 20 7a 2c 20 6a 2c 20 73 71 6c 69 74 65  xt, z, j, sqlite
25cc3 33 5f 66 72 65 65 29 3b 0a 20 20 20 20 20 20 7d  3_free);.      }
25cc4 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
25cc5 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a    }.    default:
25cc6 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   {.      assert(
25cc7 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
25cc8 79 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51  ype(argv[0])==SQ
25cc9 4c 49 54 45 5f 4e 55 4c 4c 20 29 3b 0a 20 20 20  LITE_NULL );.   
25cca 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
25ccb 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  t_text(context, 
25ccc 22 4e 55 4c 4c 22 2c 20 34 2c 20 53 51 4c 49 54  "NULL", 4, SQLIT
25ccd 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  E_STATIC);.     
25cce 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
25ccf 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68  }.}../*.** The h
25cd0 65 78 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20  ex() function.  
25cd1 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 61 72  Interpret the ar
25cd2 67 75 6d 65 6e 74 20 61 73 20 61 20 62 6c 6f 62  gument as a blob
25cd3 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20 68  .  Return.** a h
25cd4 65 78 61 64 65 63 69 6d 61 6c 20 72 65 6e 64 65  exadecimal rende
25cd5 72 69 6e 67 20 61 73 20 74 65 78 74 2e 0a 2a 2f  ring as text..*/
25cd6 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 65 78  .static void hex
25cd7 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
25cd8 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
25cd9 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
25cda 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
25cdb 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 69 2c  argv.){.  int i,
25cdc 20 6e 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69   n;.  const unsi
25cdd 67 6e 65 64 20 63 68 61 72 20 2a 70 42 6c 6f 62  gned char *pBlob
25cde 3b 0a 20 20 63 68 61 72 20 2a 7a 48 65 78 2c 20  ;.  char *zHex, 
25cdf 2a 7a 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72  *z;.  assert( ar
25ce0 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45  gc==1 );.  UNUSE
25ce1 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63  D_PARAMETER(argc
25ce2 29 3b 0a 20 20 70 42 6c 6f 62 20 3d 20 73 71 6c  );.  pBlob = sql
25ce3 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28  ite3_value_blob(
25ce4 61 72 67 76 5b 30 5d 29 3b 0a 20 20 6e 20 3d 20  argv[0]);.  n = 
25ce5 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
25ce6 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  tes(argv[0]);.  
25ce7 61 73 73 65 72 74 28 20 70 42 6c 6f 62 3d 3d 73  assert( pBlob==s
25ce8 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
25ce9 62 28 61 72 67 76 5b 30 5d 29 20 29 3b 20 20 2f  b(argv[0]) );  /
25cea 2a 20 4e 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68  * No encoding ch
25ceb 61 6e 67 65 20 2a 2f 0a 20 20 7a 20 3d 20 7a 48  ange */.  z = zH
25cec 65 78 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c  ex = contextMall
25ced 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28 28 69 36  oc(context, ((i6
25cee 34 29 6e 29 2a 32 20 2b 20 31 29 3b 0a 20 20 69  4)n)*2 + 1);.  i
25cef 66 28 20 7a 48 65 78 20 29 7b 0a 20 20 20 20 66  f( zHex ){.    f
25cf0 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b  or(i=0; i<n; i++
25cf1 2c 20 70 42 6c 6f 62 2b 2b 29 7b 0a 20 20 20 20  , pBlob++){.    
25cf2 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
25cf3 63 20 3d 20 2a 70 42 6c 6f 62 3b 0a 20 20 20 20  c = *pBlob;.    
25cf4 20 20 2a 28 7a 2b 2b 29 20 3d 20 68 65 78 64 69    *(z++) = hexdi
25cf5 67 69 74 73 5b 28 63 3e 3e 34 29 26 30 78 66 5d  gits[(c>>4)&0xf]
25cf6 3b 0a 20 20 20 20 20 20 2a 28 7a 2b 2b 29 20 3d  ;.      *(z++) =
25cf7 20 68 65 78 64 69 67 69 74 73 5b 63 26 30 78 66   hexdigits[c&0xf
25cf8 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 7a 20  ];.    }.    *z 
25cf9 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  = 0;.    sqlite3
25cfa 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e  _result_text(con
25cfb 74 65 78 74 2c 20 7a 48 65 78 2c 20 6e 2a 32 2c  text, zHex, n*2,
25cfc 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a   sqlite3_free);.
25cfd 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65    }.}../*.** The
25cfe 20 7a 65 72 6f 62 6c 6f 62 28 4e 29 20 66 75 6e   zeroblob(N) fun
25cff 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 20  ction returns a 
25d00 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 62 6c 6f 62  zero-filled blob
25d01 20 6f 66 20 73 69 7a 65 20 4e 20 62 79 74 65 73   of size N bytes
25d02 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
25d03 20 7a 65 72 6f 62 6c 6f 62 46 75 6e 63 28 0a 20   zeroblobFunc(. 
25d04 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
25d05 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
25d06 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33   argc,.  sqlite3
25d07 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
25d08 0a 20 20 69 36 34 20 6e 3b 0a 20 20 73 71 6c 69  .  i64 n;.  sqli
25d09 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65  te3 *db = sqlite
25d0a 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
25d0b 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  dle(context);.  
25d0c 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20  assert( argc==1 
25d0d 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
25d0e 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 6e  METER(argc);.  n
25d0f 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
25d10 5f 69 6e 74 36 34 28 61 72 67 76 5b 30 5d 29 3b  _int64(argv[0]);
25d11 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 3d 3d  .  testcase( n==
25d12 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
25d13 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20  E_LIMIT_LENGTH] 
25d14 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e  );.  testcase( n
25d15 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  ==db->aLimit[SQL
25d16 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48  ITE_LIMIT_LENGTH
25d17 5d 2b 31 20 29 3b 0a 20 20 69 66 28 20 6e 3e 64  ]+1 );.  if( n>d
25d18 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
25d19 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29  _LIMIT_LENGTH] )
25d1a 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
25d1b 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69  sult_error_toobi
25d1c 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 65  g(context);.  }e
25d1d 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
25d1e 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62  _result_zeroblob
25d1f 28 63 6f 6e 74 65 78 74 2c 20 28 69 6e 74 29 6e  (context, (int)n
25d20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
25d21 54 68 65 20 72 65 70 6c 61 63 65 28 29 20 66 75  The replace() fu
25d22 6e 63 74 69 6f 6e 2e 20 20 54 68 72 65 65 20 61  nction.  Three a
25d23 72 67 75 6d 65 6e 74 73 20 61 72 65 20 61 6c 6c  rguments are all
25d24 20 73 74 72 69 6e 67 73 3a 20 63 61 6c 6c 0a 2a   strings: call.*
25d25 2a 20 74 68 65 6d 20 41 2c 20 42 2c 20 61 6e 64  * them A, B, and
25d26 20 43 2e 20 54 68 65 20 72 65 73 75 6c 74 20 69   C. The result i
25d27 73 20 61 6c 73 6f 20 61 20 73 74 72 69 6e 67 20  s also a string 
25d28 77 68 69 63 68 20 69 73 20 64 65 72 69 76 65 64  which is derived
25d29 0a 2a 2a 20 66 72 6f 6d 20 41 20 62 79 20 72 65  .** from A by re
25d2a 70 6c 61 63 69 6e 67 20 65 76 65 72 79 20 6f 63  placing every oc
25d2b 63 75 72 61 6e 63 65 20 6f 66 20 42 20 77 69 74  curance of B wit
25d2c 68 20 43 2e 20 20 54 68 65 20 6d 61 74 63 68 0a  h C.  The match.
25d2d 2a 2a 20 6d 75 73 74 20 62 65 20 65 78 61 63 74  ** must be exact
25d2e 2e 20 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71  .  Collating seq
25d2f 75 65 6e 63 65 73 20 61 72 65 20 6e 6f 74 20 75  uences are not u
25d30 73 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  sed..*/.static v
25d31 6f 69 64 20 72 65 70 6c 61 63 65 46 75 6e 63 28  oid replaceFunc(
25d32 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
25d33 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
25d34 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74  nt argc,.  sqlit
25d35 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
25d36 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  ){.  const unsig
25d37 6e 65 64 20 63 68 61 72 20 2a 7a 53 74 72 3b 20  ned char *zStr; 
25d38 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e         /* The in
25d39 70 75 74 20 73 74 72 69 6e 67 20 41 20 2a 2f 0a  put string A */.
25d3a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
25d3b 20 63 68 61 72 20 2a 7a 50 61 74 74 65 72 6e 3b   char *zPattern;
25d3c 20 20 20 20 2f 2a 20 54 68 65 20 70 61 74 74 65      /* The patte
25d3d 72 6e 20 73 74 72 69 6e 67 20 42 20 2a 2f 0a 20  rn string B */. 
25d3e 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
25d3f 63 68 61 72 20 2a 7a 52 65 70 3b 20 20 20 20 20  char *zRep;     
25d40 20 20 20 2f 2a 20 54 68 65 20 72 65 70 6c 61 63     /* The replac
25d41 65 6d 65 6e 74 20 73 74 72 69 6e 67 20 43 20 2a  ement string C *
25d42 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  /.  unsigned cha
25d43 72 20 2a 7a 4f 75 74 3b 20 20 20 20 20 20 20 20  r *zOut;        
25d44 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 75 74        /* The out
25d45 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 74  put */.  int nSt
25d46 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
25d47 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 53 74    /* Size of zSt
25d48 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 74 74  r */.  int nPatt
25d49 65 72 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  ern;            
25d4a 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 50 61 74 74  /* Size of zPatt
25d4b 65 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65  ern */.  int nRe
25d4c 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
25d4d 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 52 65    /* Size of zRe
25d4e 70 20 2a 2f 0a 20 20 69 36 34 20 6e 4f 75 74 3b  p */.  i64 nOut;
25d4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25d50 2f 2a 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20  /* Maximum size 
25d51 6f 66 20 7a 4f 75 74 20 2a 2f 0a 20 20 69 6e 74  of zOut */.  int
25d52 20 6c 6f 6f 70 4c 69 6d 69 74 3b 20 20 20 20 20   loopLimit;     
25d53 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 7a 53        /* Last zS
25d54 74 72 5b 5d 20 74 68 61 74 20 6d 69 67 68 74 20  tr[] that might 
25d55 6d 61 74 63 68 20 7a 50 61 74 74 65 72 6e 5b 5d  match zPattern[]
25d56 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20   */.  int i, j; 
25d57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
25d58 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20  * Loop counters 
25d59 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72  */..  assert( ar
25d5a 67 63 3d 3d 33 20 29 3b 0a 20 20 55 4e 55 53 45  gc==3 );.  UNUSE
25d5b 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63  D_PARAMETER(argc
25d5c 29 3b 0a 20 20 7a 53 74 72 20 3d 20 73 71 6c 69  );.  zStr = sqli
25d5d 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
25d5e 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20 7a  rgv[0]);.  if( z
25d5f 53 74 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  Str==0 ) return;
25d60 0a 20 20 6e 53 74 72 20 3d 20 73 71 6c 69 74 65  .  nStr = sqlite
25d61 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72  3_value_bytes(ar
25d62 67 76 5b 30 5d 29 3b 0a 20 20 61 73 73 65 72 74  gv[0]);.  assert
25d63 28 20 7a 53 74 72 3d 3d 73 71 6c 69 74 65 33 5f  ( zStr==sqlite3_
25d64 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
25d65 30 5d 29 20 29 3b 20 20 2f 2a 20 4e 6f 20 65 6e  0]) );  /* No en
25d66 63 6f 64 69 6e 67 20 63 68 61 6e 67 65 20 2a 2f  coding change */
25d67 0a 20 20 7a 50 61 74 74 65 72 6e 20 3d 20 73 71  .  zPattern = sq
25d68 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
25d69 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 69 66 28  (argv[1]);.  if(
25d6a 20 7a 50 61 74 74 65 72 6e 3d 3d 30 20 29 7b 0a   zPattern==0 ){.
25d6b 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
25d6c 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
25d6d 72 67 76 5b 31 5d 29 3d 3d 53 51 4c 49 54 45 5f  rgv[1])==SQLITE_
25d6e 4e 55 4c 4c 0a 20 20 20 20 20 20 20 20 20 20 20  NULL.           
25d6f 20 7c 7c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74   || sqlite3_cont
25d70 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f  ext_db_handle(co
25d71 6e 74 65 78 74 29 2d 3e 6d 61 6c 6c 6f 63 46 61  ntext)->mallocFa
25d72 69 6c 65 64 20 29 3b 0a 20 20 20 20 72 65 74 75  iled );.    retu
25d73 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 50  rn;.  }.  if( zP
25d74 61 74 74 65 72 6e 5b 30 5d 3d 3d 30 20 29 7b 0a  attern[0]==0 ){.
25d75 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
25d76 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
25d77 72 67 76 5b 31 5d 29 21 3d 53 51 4c 49 54 45 5f  rgv[1])!=SQLITE_
25d78 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 73 71 6c 69  NULL );.    sqli
25d79 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65  te3_result_value
25d7a 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 76 5b 30  (context, argv[0
25d7b 5d 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  ]);.    return;.
25d7c 20 20 7d 0a 20 20 6e 50 61 74 74 65 72 6e 20 3d    }.  nPattern =
25d7d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
25d7e 79 74 65 73 28 61 72 67 76 5b 31 5d 29 3b 0a 20  ytes(argv[1]);. 
25d7f 20 61 73 73 65 72 74 28 20 7a 50 61 74 74 65 72   assert( zPatter
25d80 6e 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  n==sqlite3_value
25d81 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 20 29  _text(argv[1]) )
25d82 3b 20 20 2f 2a 20 4e 6f 20 65 6e 63 6f 64 69 6e  ;  /* No encodin
25d83 67 20 63 68 61 6e 67 65 20 2a 2f 0a 20 20 7a 52  g change */.  zR
25d84 65 70 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  ep = sqlite3_val
25d85 75 65 5f 74 65 78 74 28 61 72 67 76 5b 32 5d 29  ue_text(argv[2])
25d86 3b 0a 20 20 69 66 28 20 7a 52 65 70 3d 3d 30 20  ;.  if( zRep==0 
25d87 29 20 72 65 74 75 72 6e 3b 0a 20 20 6e 52 65 70  ) return;.  nRep
25d88 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
25d89 5f 62 79 74 65 73 28 61 72 67 76 5b 32 5d 29 3b  _bytes(argv[2]);
25d8a 0a 20 20 61 73 73 65 72 74 28 20 7a 52 65 70 3d  .  assert( zRep=
25d8b 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  =sqlite3_value_t
25d8c 65 78 74 28 61 72 67 76 5b 32 5d 29 20 29 3b 0a  ext(argv[2]) );.
25d8d 20 20 6e 4f 75 74 20 3d 20 6e 53 74 72 20 2b 20    nOut = nStr + 
25d8e 31 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 4f 75  1;.  assert( nOu
25d8f 74 3c 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e  t<SQLITE_MAX_LEN
25d90 47 54 48 20 29 3b 0a 20 20 7a 4f 75 74 20 3d 20  GTH );.  zOut = 
25d91 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f  contextMalloc(co
25d92 6e 74 65 78 74 2c 20 28 69 36 34 29 6e 4f 75 74  ntext, (i64)nOut
25d93 29 3b 0a 20 20 69 66 28 20 7a 4f 75 74 3d 3d 30  );.  if( zOut==0
25d94 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
25d95 20 20 7d 0a 20 20 6c 6f 6f 70 4c 69 6d 69 74 20    }.  loopLimit 
25d96 3d 20 6e 53 74 72 20 2d 20 6e 50 61 74 74 65 72  = nStr - nPatter
25d97 6e 3b 20 20 0a 20 20 66 6f 72 28 69 3d 6a 3d 30  n;  .  for(i=j=0
25d98 3b 20 69 3c 3d 6c 6f 6f 70 4c 69 6d 69 74 3b 20  ; i<=loopLimit; 
25d99 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 53  i++){.    if( zS
25d9a 74 72 5b 69 5d 21 3d 7a 50 61 74 74 65 72 6e 5b  tr[i]!=zPattern[
25d9b 30 5d 20 7c 7c 20 6d 65 6d 63 6d 70 28 26 7a 53  0] || memcmp(&zS
25d9c 74 72 5b 69 5d 2c 20 7a 50 61 74 74 65 72 6e 2c  tr[i], zPattern,
25d9d 20 6e 50 61 74 74 65 72 6e 29 20 29 7b 0a 20 20   nPattern) ){.  
25d9e 20 20 20 20 7a 4f 75 74 5b 6a 2b 2b 5d 20 3d 20      zOut[j++] = 
25d9f 7a 53 74 72 5b 69 5d 3b 0a 20 20 20 20 7d 65 6c  zStr[i];.    }el
25da0 73 65 7b 0a 20 20 20 20 20 20 75 38 20 2a 7a 4f  se{.      u8 *zO
25da1 6c 64 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ld;.      sqlite
25da2 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f  3 *db = sqlite3_
25da3 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
25da4 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20  e(context);.    
25da5 20 20 6e 4f 75 74 20 2b 3d 20 6e 52 65 70 20 2d    nOut += nRep -
25da6 20 6e 50 61 74 74 65 72 6e 3b 0a 20 20 20 20 20   nPattern;.     
25da7 20 74 65 73 74 63 61 73 65 28 20 6e 4f 75 74 2d   testcase( nOut-
25da8 31 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  1==db->aLimit[SQ
25da9 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
25daa 48 5d 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  H] );.      test
25dab 63 61 73 65 28 20 6e 4f 75 74 2d 32 3d 3d 64 62  case( nOut-2==db
25dac 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
25dad 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 3b  LIMIT_LENGTH] );
25dae 0a 20 20 20 20 20 20 69 66 28 20 6e 4f 75 74 2d  .      if( nOut-
25daf 31 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  1>db->aLimit[SQL
25db0 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48  ITE_LIMIT_LENGTH
25db1 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  ] ){.        sql
25db2 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
25db3 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78 74  r_toobig(context
25db4 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
25db5 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4f 75  e3DbFree(db, zOu
25db6 74 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  t);.        retu
25db7 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
25db8 20 20 7a 4f 6c 64 20 3d 20 7a 4f 75 74 3b 0a 20    zOld = zOut;. 
25db9 20 20 20 20 20 7a 4f 75 74 20 3d 20 73 71 6c 69       zOut = sqli
25dba 74 65 33 5f 72 65 61 6c 6c 6f 63 28 7a 4f 75 74  te3_realloc(zOut
25dbb 2c 20 28 69 6e 74 29 6e 4f 75 74 29 3b 0a 20 20  , (int)nOut);.  
25dbc 20 20 20 20 69 66 28 20 7a 4f 75 74 3d 3d 30 20      if( zOut==0 
25dbd 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
25dbe 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
25dbf 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a  nomem(context);.
25dc0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
25dc1 62 46 72 65 65 28 64 62 2c 20 7a 4f 6c 64 29 3b  bFree(db, zOld);
25dc2 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b  .        return;
25dc3 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d  .      }.      m
25dc4 65 6d 63 70 79 28 26 7a 4f 75 74 5b 6a 5d 2c 20  emcpy(&zOut[j], 
25dc5 7a 52 65 70 2c 20 6e 52 65 70 29 3b 0a 20 20 20  zRep, nRep);.   
25dc6 20 20 20 6a 20 2b 3d 20 6e 52 65 70 3b 0a 20 20     j += nRep;.  
25dc7 20 20 20 20 69 20 2b 3d 20 6e 50 61 74 74 65 72      i += nPatter
25dc8 6e 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  n-1;.    }.  }. 
25dc9 20 61 73 73 65 72 74 28 20 6a 2b 6e 53 74 72 2d   assert( j+nStr-
25dca 69 2b 31 3d 3d 6e 4f 75 74 20 29 3b 0a 20 20 6d  i+1==nOut );.  m
25dcb 65 6d 63 70 79 28 26 7a 4f 75 74 5b 6a 5d 2c 20  emcpy(&zOut[j], 
25dcc 26 7a 53 74 72 5b 69 5d 2c 20 6e 53 74 72 2d 69  &zStr[i], nStr-i
25dcd 29 3b 0a 20 20 6a 20 2b 3d 20 6e 53 74 72 20 2d  );.  j += nStr -
25dce 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 6a 3c   i;.  assert( j<
25dcf 3d 6e 4f 75 74 20 29 3b 0a 20 20 7a 4f 75 74 5b  =nOut );.  zOut[
25dd0 6a 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65  j] = 0;.  sqlite
25dd1 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
25dd2 6e 74 65 78 74 2c 20 28 63 68 61 72 2a 29 7a 4f  ntext, (char*)zO
25dd3 75 74 2c 20 6a 2c 20 73 71 6c 69 74 65 33 5f 66  ut, j, sqlite3_f
25dd4 72 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  ree);.}../*.** I
25dd5 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
25dd6 20 74 68 65 20 54 52 49 4d 28 29 2c 20 4c 54 52   the TRIM(), LTR
25dd7 49 4d 28 29 2c 20 61 6e 64 20 52 54 52 49 4d 28  IM(), and RTRIM(
25dd8 29 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20  ) functions..** 
25dd9 54 68 65 20 75 73 65 72 64 61 74 61 20 69 73 20  The userdata is 
25dda 30 78 31 20 66 6f 72 20 6c 65 66 74 20 74 72 69  0x1 for left tri
25ddb 6d 2c 20 30 78 32 20 66 6f 72 20 72 69 67 68 74  m, 0x2 for right
25ddc 20 74 72 69 6d 2c 20 30 78 33 20 66 6f 72 20 62   trim, 0x3 for b
25ddd 6f 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  oth..*/.static v
25dde 6f 69 64 20 74 72 69 6d 46 75 6e 63 28 0a 20 20  oid trimFunc(.  
25ddf 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
25de0 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
25de1 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f  argc,.  sqlite3_
25de2 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
25de3 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
25de4 20 63 68 61 72 20 2a 7a 49 6e 3b 20 20 20 20 20   char *zIn;     
25de5 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 73 74 72      /* Input str
25de6 69 6e 67 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75  ing */.  const u
25de7 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 43  nsigned char *zC
25de8 68 61 72 53 65 74 3b 20 20 20 20 2f 2a 20 53 65  harSet;    /* Se
25de9 74 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20  t of characters 
25dea 74 6f 20 74 72 69 6d 20 2a 2f 0a 20 20 69 6e 74  to trim */.  int
25deb 20 6e 49 6e 3b 20 20 20 20 20 20 20 20 20 20 20   nIn;           
25dec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
25ded 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
25dee 73 20 69 6e 20 69 6e 70 75 74 20 2a 2f 0a 20 20  s in input */.  
25def 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20  int flags;      
25df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25df1 20 20 2f 2a 20 31 3a 20 74 72 69 6d 6c 65 66 74    /* 1: trimleft
25df2 20 20 32 3a 20 74 72 69 6d 72 69 67 68 74 20 20    2: trimright  
25df3 33 3a 20 74 72 69 6d 20 2a 2f 0a 20 20 69 6e 74  3: trim */.  int
25df4 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
25df5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
25df6 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
25df7 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  /.  unsigned cha
25df8 72 20 2a 61 4c 65 6e 20 3d 20 30 3b 20 20 20 20  r *aLen = 0;    
25df9 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20        /* Length 
25dfa 6f 66 20 65 61 63 68 20 63 68 61 72 61 63 74 65  of each characte
25dfb 72 20 69 6e 20 7a 43 68 61 72 53 65 74 20 2a 2f  r in zCharSet */
25dfc 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
25dfd 20 2a 2a 61 7a 43 68 61 72 20 3d 20 30 3b 20 20   **azChar = 0;  
25dfe 20 20 20 20 20 2f 2a 20 49 6e 64 69 76 69 64 75       /* Individu
25dff 61 6c 20 63 68 61 72 61 63 74 65 72 73 20 69 6e  al characters in
25e00 20 7a 43 68 61 72 53 65 74 20 2a 2f 0a 20 20 69   zCharSet */.  i
25e01 6e 74 20 6e 43 68 61 72 3b 20 20 20 20 20 20 20  nt nChar;       
25e02 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25e03 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68   /* Number of ch
25e04 61 72 61 63 74 65 72 73 20 69 6e 20 7a 43 68 61  aracters in zCha
25e05 72 53 65 74 20 2a 2f 0a 0a 20 20 69 66 28 20 73  rSet */..  if( s
25e06 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
25e07 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49  e(argv[0])==SQLI
25e08 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 72  TE_NULL ){.    r
25e09 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 7a 49 6e  eturn;.  }.  zIn
25e0a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
25e0b 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a  _text(argv[0]);.
25e0c 20 20 69 66 28 20 7a 49 6e 3d 3d 30 20 29 20 72    if( zIn==0 ) r
25e0d 65 74 75 72 6e 3b 0a 20 20 6e 49 6e 20 3d 20 73  eturn;.  nIn = s
25e0e 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
25e0f 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 61  es(argv[0]);.  a
25e10 73 73 65 72 74 28 20 7a 49 6e 3d 3d 73 71 6c 69  ssert( zIn==sqli
25e11 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
25e12 72 67 76 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28  rgv[0]) );.  if(
25e13 20 61 72 67 63 3d 3d 31 20 29 7b 0a 20 20 20 20   argc==1 ){.    
25e14 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73  static const uns
25e15 69 67 6e 65 64 20 63 68 61 72 20 6c 65 6e 4f 6e  igned char lenOn
25e16 65 5b 5d 20 3d 20 7b 20 31 20 7d 3b 0a 20 20 20  e[] = { 1 };.   
25e17 20 73 74 61 74 69 63 20 75 6e 73 69 67 6e 65 64   static unsigned
25e18 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 61 7a   char * const az
25e19 4f 6e 65 5b 5d 20 3d 20 7b 20 28 75 38 2a 29 22  One[] = { (u8*)"
25e1a 20 22 20 7d 3b 0a 20 20 20 20 6e 43 68 61 72 20   " };.    nChar 
25e1b 3d 20 31 3b 0a 20 20 20 20 61 4c 65 6e 20 3d 20  = 1;.    aLen = 
25e1c 28 75 38 2a 29 6c 65 6e 4f 6e 65 3b 0a 20 20 20  (u8*)lenOne;.   
25e1d 20 61 7a 43 68 61 72 20 3d 20 28 75 6e 73 69 67   azChar = (unsig
25e1e 6e 65 64 20 63 68 61 72 20 2a 2a 29 61 7a 4f 6e  ned char **)azOn
25e1f 65 3b 0a 20 20 20 20 7a 43 68 61 72 53 65 74 20  e;.    zCharSet 
25e20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  = 0;.  }else if(
25e21 20 28 7a 43 68 61 72 53 65 74 20 3d 20 73 71 6c   (zCharSet = sql
25e22 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
25e23 61 72 67 76 5b 31 5d 29 29 3d 3d 30 20 29 7b 0a  argv[1]))==0 ){.
25e24 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 65      return;.  }e
25e25 6c 73 65 7b 0a 20 20 20 20 63 6f 6e 73 74 20 75  lse{.    const u
25e26 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b  nsigned char *z;
25e27 0a 20 20 20 20 66 6f 72 28 7a 3d 7a 43 68 61 72  .    for(z=zChar
25e28 53 65 74 2c 20 6e 43 68 61 72 3d 30 3b 20 2a 7a  Set, nChar=0; *z
25e29 3b 20 6e 43 68 61 72 2b 2b 29 7b 0a 20 20 20 20  ; nChar++){.    
25e2a 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54    SQLITE_SKIP_UT
25e2b 46 38 28 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 20  F8(z);.    }.   
25e2c 20 69 66 28 20 6e 43 68 61 72 3e 30 20 29 7b 0a   if( nChar>0 ){.
25e2d 20 20 20 20 20 20 61 7a 43 68 61 72 20 3d 20 63        azChar = c
25e2e 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e  ontextMalloc(con
25e2f 74 65 78 74 2c 20 28 28 69 36 34 29 6e 43 68 61  text, ((i64)nCha
25e30 72 29 2a 28 73 69 7a 65 6f 66 28 63 68 61 72 2a  r)*(sizeof(char*
25e31 29 2b 31 29 29 3b 0a 20 20 20 20 20 20 69 66 28  )+1));.      if(
25e32 20 61 7a 43 68 61 72 3d 3d 30 20 29 7b 0a 20 20   azChar==0 ){.  
25e33 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
25e34 20 20 20 20 7d 0a 20 20 20 20 20 20 61 4c 65 6e      }.      aLen
25e35 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61   = (unsigned cha
25e36 72 2a 29 26 61 7a 43 68 61 72 5b 6e 43 68 61 72  r*)&azChar[nChar
25e37 5d 3b 0a 20 20 20 20 20 20 66 6f 72 28 7a 3d 7a  ];.      for(z=z
25e38 43 68 61 72 53 65 74 2c 20 6e 43 68 61 72 3d 30  CharSet, nChar=0
25e39 3b 20 2a 7a 3b 20 6e 43 68 61 72 2b 2b 29 7b 0a  ; *z; nChar++){.
25e3a 20 20 20 20 20 20 20 20 61 7a 43 68 61 72 5b 6e          azChar[n
25e3b 43 68 61 72 5d 20 3d 20 28 75 6e 73 69 67 6e 65  Char] = (unsigne
25e3c 64 20 63 68 61 72 20 2a 29 7a 3b 0a 20 20 20 20  d char *)z;.    
25e3d 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f      SQLITE_SKIP_
25e3e 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 20 20 20  UTF8(z);.       
25e3f 20 61 4c 65 6e 5b 6e 43 68 61 72 5d 20 3d 20 28   aLen[nChar] = (
25e40 75 38 29 28 7a 20 2d 20 61 7a 43 68 61 72 5b 6e  u8)(z - azChar[n
25e41 43 68 61 72 5d 29 3b 0a 20 20 20 20 20 20 7d 0a  Char]);.      }.
25e42 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
25e43 6e 43 68 61 72 3e 30 20 29 7b 0a 20 20 20 20 66  nChar>0 ){.    f
25e44 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 50 54  lags = SQLITE_PT
25e45 52 5f 54 4f 5f 49 4e 54 28 73 71 6c 69 74 65 33  R_TO_INT(sqlite3
25e46 5f 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65  _user_data(conte
25e47 78 74 29 29 3b 0a 20 20 20 20 69 66 28 20 66 6c  xt));.    if( fl
25e48 61 67 73 20 26 20 31 20 29 7b 0a 20 20 20 20 20  ags & 1 ){.     
25e49 20 77 68 69 6c 65 28 20 6e 49 6e 3e 30 20 29 7b   while( nIn>0 ){
25e4a 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6c 65 6e  .        int len
25e4b 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 66 6f   = 0;.        fo
25e4c 72 28 69 3d 30 3b 20 69 3c 6e 43 68 61 72 3b 20  r(i=0; i<nChar; 
25e4d 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  i++){.          
25e4e 6c 65 6e 20 3d 20 61 4c 65 6e 5b 69 5d 3b 0a 20  len = aLen[i];. 
25e4f 20 20 20 20 20 20 20 20 20 69 66 28 20 6c 65 6e           if( len
25e50 3c 3d 6e 49 6e 20 26 26 20 6d 65 6d 63 6d 70 28  <=nIn && memcmp(
25e51 7a 49 6e 2c 20 61 7a 43 68 61 72 5b 69 5d 2c 20  zIn, azChar[i], 
25e52 6c 65 6e 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b  len)==0 ) break;
25e53 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
25e54 20 20 20 69 66 28 20 69 3e 3d 6e 43 68 61 72 20     if( i>=nChar 
25e55 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20  ) break;.       
25e56 20 7a 49 6e 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20   zIn += len;.   
25e57 20 20 20 20 20 6e 49 6e 20 2d 3d 20 6c 65 6e 3b       nIn -= len;
25e58 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
25e59 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 32     if( flags & 2
25e5a 20 29 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 28   ){.      while(
25e5b 20 6e 49 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20   nIn>0 ){.      
25e5c 20 20 69 6e 74 20 6c 65 6e 20 3d 20 30 3b 0a 20    int len = 0;. 
25e5d 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20         for(i=0; 
25e5e 69 3c 6e 43 68 61 72 3b 20 69 2b 2b 29 7b 0a 20  i<nChar; i++){. 
25e5f 20 20 20 20 20 20 20 20 20 6c 65 6e 20 3d 20 61           len = a
25e60 4c 65 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20  Len[i];.        
25e61 20 20 69 66 28 20 6c 65 6e 3c 3d 6e 49 6e 20 26    if( len<=nIn &
25e62 26 20 6d 65 6d 63 6d 70 28 26 7a 49 6e 5b 6e 49  & memcmp(&zIn[nI
25e63 6e 2d 6c 65 6e 5d 2c 61 7a 43 68 61 72 5b 69 5d  n-len],azChar[i]
25e64 2c 6c 65 6e 29 3d 3d 30 20 29 20 62 72 65 61 6b  ,len)==0 ) break
25e65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
25e66 20 20 20 20 69 66 28 20 69 3e 3d 6e 43 68 61 72      if( i>=nChar
25e67 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
25e68 20 20 6e 49 6e 20 2d 3d 20 6c 65 6e 3b 0a 20 20    nIn -= len;.  
25e69 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
25e6a 69 66 28 20 7a 43 68 61 72 53 65 74 20 29 7b 0a  if( zCharSet ){.
25e6b 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
25e6c 65 65 28 61 7a 43 68 61 72 29 3b 0a 20 20 20 20  ee(azChar);.    
25e6d 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  }.  }.  sqlite3_
25e6e 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
25e6f 65 78 74 2c 20 28 63 68 61 72 2a 29 7a 49 6e 2c  ext, (char*)zIn,
25e70 20 6e 49 6e 2c 20 53 51 4c 49 54 45 5f 54 52 41   nIn, SQLITE_TRA
25e71 4e 53 49 45 4e 54 29 3b 0a 7d 0a 0a 0a 23 69 66  NSIENT);.}...#if
25e72 64 65 66 20 53 51 4c 49 54 45 5f 53 4f 55 4e 44  def SQLITE_SOUND
25e73 45 58 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65  EX./*.** Compute
25e74 20 74 68 65 20 73 6f 75 6e 64 65 78 20 65 6e 63   the soundex enc
25e75 6f 64 69 6e 67 20 6f 66 20 61 20 77 6f 72 64 2e  oding of a word.
25e76 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
25e77 73 6f 75 6e 64 65 78 46 75 6e 63 28 0a 20 20 73  soundexFunc(.  s
25e78 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
25e79 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
25e7a 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
25e7b 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
25e7c 20 63 68 61 72 20 7a 52 65 73 75 6c 74 5b 38 5d   char zResult[8]
25e7d 3b 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 49  ;.  const u8 *zI
25e7e 6e 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20  n;.  int i, j;. 
25e7f 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e   static const un
25e80 73 69 67 6e 65 64 20 63 68 61 72 20 69 43 6f 64  signed char iCod
25e81 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 30 2c 20 30  e[] = {.    0, 0
25e82 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
25e83 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
25e84 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20  0, 0, 0, 0,.    
25e85 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
25e86 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
25e87 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a   0, 0, 0, 0, 0,.
25e88 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20      0, 0, 0, 0, 
25e89 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
25e8a 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
25e8b 20 30 2c 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c   0,.    0, 0, 0,
25e8c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
25e8d 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
25e8e 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30  , 0, 0,.    0, 0
25e8f 2c 20 31 2c 20 32 2c 20 33 2c 20 30 2c 20 31 2c  , 1, 2, 3, 0, 1,
25e90 20 32 2c 20 30 2c 20 30 2c 20 32 2c 20 32 2c 20   2, 0, 0, 2, 2, 
25e91 34 2c 20 35 2c 20 35 2c 20 30 2c 0a 20 20 20 20  4, 5, 5, 0,.    
25e92 31 2c 20 32 2c 20 36 2c 20 32 2c 20 33 2c 20 30  1, 2, 6, 2, 3, 0
25e93 2c 20 31 2c 20 30 2c 20 32 2c 20 30 2c 20 32 2c  , 1, 0, 2, 0, 2,
25e94 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a   0, 0, 0, 0, 0,.
25e95 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 32 2c 20      0, 0, 1, 2, 
25e96 33 2c 20 30 2c 20 31 2c 20 32 2c 20 30 2c 20 30  3, 0, 1, 2, 0, 0
25e97 2c 20 32 2c 20 32 2c 20 34 2c 20 35 2c 20 35 2c  , 2, 2, 4, 5, 5,
25e98 20 30 2c 0a 20 20 20 20 31 2c 20 32 2c 20 36 2c   0,.    1, 2, 6,
25e99 20 32 2c 20 33 2c 20 30 2c 20 31 2c 20 30 2c 20   2, 3, 0, 1, 0, 
25e9a 32 2c 20 30 2c 20 32 2c 20 30 2c 20 30 2c 20 30  2, 0, 2, 0, 0, 0
25e9b 2c 20 30 2c 20 30 2c 0a 20 20 7d 3b 0a 20 20 61  , 0, 0,.  };.  a
25e9c 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29  ssert( argc==1 )
25e9d 3b 0a 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29 73  ;.  zIn = (u8*)s
25e9e 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
25e9f 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66  t(argv[0]);.  if
25ea0 28 20 7a 49 6e 3d 3d 30 20 29 20 7a 49 6e 20 3d  ( zIn==0 ) zIn =
25ea1 20 28 75 38 2a 29 22 22 3b 0a 20 20 66 6f 72 28   (u8*)"";.  for(
25ea2 69 3d 30 3b 20 7a 49 6e 5b 69 5d 20 26 26 20 21  i=0; zIn[i] && !
25ea3 73 71 6c 69 74 65 33 49 73 61 6c 70 68 61 28 7a  sqlite3Isalpha(z
25ea4 49 6e 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20  In[i]); i++){}. 
25ea5 20 69 66 28 20 7a 49 6e 5b 69 5d 20 29 7b 0a 20   if( zIn[i] ){. 
25ea6 20 20 20 75 38 20 70 72 65 76 63 6f 64 65 20 3d     u8 prevcode =
25ea7 20 69 43 6f 64 65 5b 7a 49 6e 5b 69 5d 26 30 78   iCode[zIn[i]&0x
25ea8 37 66 5d 3b 0a 20 20 20 20 7a 52 65 73 75 6c 74  7f];.    zResult
25ea9 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33 54 6f 75  [0] = sqlite3Tou
25eaa 70 70 65 72 28 7a 49 6e 5b 69 5d 29 3b 0a 20 20  pper(zIn[i]);.  
25eab 20 20 66 6f 72 28 6a 3d 31 3b 20 6a 3c 34 20 26    for(j=1; j<4 &
25eac 26 20 7a 49 6e 5b 69 5d 3b 20 69 2b 2b 29 7b 0a  & zIn[i]; i++){.
25ead 20 20 20 20 20 20 69 6e 74 20 63 6f 64 65 20 3d        int code =
25eae 20 69 43 6f 64 65 5b 7a 49 6e 5b 69 5d 26 30 78   iCode[zIn[i]&0x
25eaf 37 66 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 63  7f];.      if( c
25eb0 6f 64 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 20  ode>0 ){.       
25eb1 20 69 66 28 20 63 6f 64 65 21 3d 70 72 65 76 63   if( code!=prevc
25eb2 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ode ){.         
25eb3 20 70 72 65 76 63 6f 64 65 20 3d 20 63 6f 64 65   prevcode = code
25eb4 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 52 65 73  ;.          zRes
25eb5 75 6c 74 5b 6a 2b 2b 5d 20 3d 20 63 6f 64 65 20  ult[j++] = code 
25eb6 2b 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 7d  + '0';.        }
25eb7 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
25eb8 20 20 20 20 20 20 70 72 65 76 63 6f 64 65 20 3d        prevcode =
25eb9 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
25eba 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 6a 3c 34  }.    while( j<4
25ebb 20 29 7b 0a 20 20 20 20 20 20 7a 52 65 73 75 6c   ){.      zResul
25ebc 74 5b 6a 2b 2b 5d 20 3d 20 27 30 27 3b 0a 20 20  t[j++] = '0';.  
25ebd 20 20 7d 0a 20 20 20 20 7a 52 65 73 75 6c 74 5b    }.    zResult[
25ebe 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69  j] = 0;.    sqli
25ebf 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
25ec0 63 6f 6e 74 65 78 74 2c 20 7a 52 65 73 75 6c 74  context, zResult
25ec1 2c 20 34 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e  , 4, SQLITE_TRAN
25ec2 53 49 45 4e 54 29 3b 0a 20 20 7d 65 6c 73 65 7b  SIENT);.  }else{
25ec3 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
25ec4 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
25ec5 2c 20 22 3f 30 30 30 22 2c 20 34 2c 20 53 51 4c  , "?000", 4, SQL
25ec6 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d  ITE_STATIC);.  }
25ec7 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64  .}.#endif..#ifnd
25ec8 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c  ef SQLITE_OMIT_L
25ec9 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a  OAD_EXTENSION./*
25eca 0a 2a 2a 20 41 20 66 75 6e 63 74 69 6f 6e 20 74  .** A function t
25ecb 68 61 74 20 6c 6f 61 64 73 20 61 20 73 68 61 72  hat loads a shar
25ecc 65 64 2d 6c 69 62 72 61 72 79 20 65 78 74 65 6e  ed-library exten
25ecd 73 69 6f 6e 20 74 68 65 6e 20 72 65 74 75 72 6e  sion then return
25ece 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69  s NULL..*/.stati
25ecf 63 20 76 6f 69 64 20 6c 6f 61 64 45 78 74 28 73  c void loadExt(s
25ed0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
25ed1 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67  context, int arg
25ed2 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  c, sqlite3_value
25ed3 20 2a 2a 61 72 67 76 29 7b 0a 20 20 63 6f 6e 73   **argv){.  cons
25ed4 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 20 3d 20  t char *zFile = 
25ed5 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71  (const char *)sq
25ed6 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
25ed7 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 63 6f 6e  (argv[0]);.  con
25ed8 73 74 20 63 68 61 72 20 2a 7a 50 72 6f 63 3b 0a  st char *zProc;.
25ed9 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
25eda 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f  sqlite3_context_
25edb 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78  db_handle(contex
25edc 74 29 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72  t);.  char *zErr
25edd 4d 73 67 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20  Msg = 0;..  if( 
25ede 61 72 67 63 3d 3d 32 20 29 7b 0a 20 20 20 20 7a  argc==2 ){.    z
25edf 50 72 6f 63 20 3d 20 28 63 6f 6e 73 74 20 63 68  Proc = (const ch
25ee0 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  ar *)sqlite3_val
25ee1 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29  ue_text(argv[1])
25ee2 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a  ;.  }else{.    z
25ee3 50 72 6f 63 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  Proc = 0;.  }.  
25ee4 69 66 28 20 7a 46 69 6c 65 20 26 26 20 73 71 6c  if( zFile && sql
25ee5 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73  ite3_load_extens
25ee6 69 6f 6e 28 64 62 2c 20 7a 46 69 6c 65 2c 20 7a  ion(db, zFile, z
25ee7 50 72 6f 63 2c 20 26 7a 45 72 72 4d 73 67 29 20  Proc, &zErrMsg) 
25ee8 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  ){.    sqlite3_r
25ee9 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74  esult_error(cont
25eea 65 78 74 2c 20 7a 45 72 72 4d 73 67 2c 20 2d 31  ext, zErrMsg, -1
25eeb 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
25eec 72 65 65 28 7a 45 72 72 4d 73 67 29 3b 0a 20 20  ree(zErrMsg);.  
25eed 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a  }.}.#endif.../*.
25eee 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
25eef 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
25ef0 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20  structure holds 
25ef1 74 68 65 20 63 6f 6e 74 65 78 74 20 6f 66 20 61  the context of a
25ef2 0a 2a 2a 20 73 75 6d 28 29 20 6f 72 20 61 76 67  .** sum() or avg
25ef3 28 29 20 61 67 67 72 65 67 61 74 65 20 63 6f 6d  () aggregate com
25ef4 70 75 74 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70  putation..*/.typ
25ef5 65 64 65 66 20 73 74 72 75 63 74 20 53 75 6d 43  edef struct SumC
25ef6 74 78 20 53 75 6d 43 74 78 3b 0a 73 74 72 75 63  tx SumCtx;.struc
25ef7 74 20 53 75 6d 43 74 78 20 7b 0a 20 20 64 6f 75  t SumCtx {.  dou
25ef8 62 6c 65 20 72 53 75 6d 3b 20 20 20 20 20 20 2f  ble rSum;      /
25ef9 2a 20 46 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  * Floating point
25efa 20 73 75 6d 20 2a 2f 0a 20 20 69 36 34 20 69 53   sum */.  i64 iS
25efb 75 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49  um;         /* I
25efc 6e 74 65 67 65 72 20 73 75 6d 20 2a 2f 20 20 20  nteger sum */   
25efd 0a 20 20 69 36 34 20 63 6e 74 3b 20 20 20 20 20  .  i64 cnt;     
25efe 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
25eff 66 20 65 6c 65 6d 65 6e 74 73 20 73 75 6d 6d 65  f elements summe
25f00 64 20 2a 2f 0a 20 20 75 38 20 6f 76 65 72 66 6c  d */.  u8 overfl
25f01 6f 77 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65  ow;      /* True
25f02 20 69 66 20 69 6e 74 65 67 65 72 20 6f 76 65 72   if integer over
25f03 66 6c 6f 77 20 73 65 65 6e 20 2a 2f 0a 20 20 75  flow seen */.  u
25f04 38 20 61 70 70 72 6f 78 3b 20 20 20 20 20 20 20  8 approx;       
25f05 20 2f 2a 20 54 72 75 65 20 69 66 20 6e 6f 6e 2d   /* True if non-
25f06 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 77 61  integer value wa
25f07 73 20 69 6e 70 75 74 20 74 6f 20 74 68 65 20 73  s input to the s
25f08 75 6d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  um */.};../*.** 
25f09 52 6f 75 74 69 6e 65 73 20 75 73 65 64 20 74 6f  Routines used to
25f0a 20 63 6f 6d 70 75 74 65 20 74 68 65 20 73 75 6d   compute the sum
25f0b 2c 20 61 76 65 72 61 67 65 2c 20 61 6e 64 20 74  , average, and t
25f0c 6f 74 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  otal..**.** The 
25f0d 53 55 4d 28 29 20 66 75 6e 63 74 69 6f 6e 20 66  SUM() function f
25f0e 6f 6c 6c 6f 77 73 20 74 68 65 20 28 62 72 6f 6b  ollows the (brok
25f0f 65 6e 29 20 53 51 4c 20 73 74 61 6e 64 61 72 64  en) SQL standard
25f10 20 77 68 69 63 68 20 6d 65 61 6e 73 0a 2a 2a 20   which means.** 
25f11 74 68 61 74 20 69 74 20 72 65 74 75 72 6e 73 20  that it returns 
25f12 4e 55 4c 4c 20 69 66 20 69 74 20 73 75 6d 73 20  NULL if it sums 
25f13 6f 76 65 72 20 6e 6f 20 69 6e 70 75 74 73 2e 20  over no inputs. 
25f14 20 54 4f 54 41 4c 20 72 65 74 75 72 6e 73 0a 2a   TOTAL returns.*
25f15 2a 20 30 2e 30 20 69 6e 20 74 68 61 74 20 63 61  * 0.0 in that ca
25f16 73 65 2e 20 20 49 6e 20 61 64 64 69 74 69 6f 6e  se.  In addition
25f17 2c 20 54 4f 54 41 4c 20 61 6c 77 61 79 73 20 72  , TOTAL always r
25f18 65 74 75 72 6e 73 20 61 20 66 6c 6f 61 74 20 77  eturns a float w
25f19 68 65 72 65 0a 2a 2a 20 53 55 4d 20 6d 69 67 68  here.** SUM migh
25f1a 74 20 72 65 74 75 72 6e 20 61 6e 20 69 6e 74 65  t return an inte
25f1b 67 65 72 20 69 66 20 69 74 20 6e 65 76 65 72 20  ger if it never 
25f1c 65 6e 63 6f 75 6e 74 65 72 73 20 61 20 66 6c 6f  encounters a flo
25f1d 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 76  ating point.** v
25f1e 61 6c 75 65 2e 20 20 54 4f 54 41 4c 20 6e 65 76  alue.  TOTAL nev
25f1f 65 72 20 66 61 69 6c 73 2c 20 62 75 74 20 53 55  er fails, but SU
25f20 4d 20 6d 69 67 68 74 20 74 68 72 6f 75 67 68 20  M might through 
25f21 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 69 66 0a  an exception if.
25f22 2a 2a 20 69 74 20 6f 76 65 72 66 6c 6f 77 73 20  ** it overflows 
25f23 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 73  an integer..*/.s
25f24 74 61 74 69 63 20 76 6f 69 64 20 73 75 6d 53 74  tatic void sumSt
25f25 65 70 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ep(sqlite3_conte
25f26 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74  xt *context, int
25f27 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76   argc, sqlite3_v
25f28 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20  alue **argv){.  
25f29 53 75 6d 43 74 78 20 2a 70 3b 0a 20 20 69 6e 74  SumCtx *p;.  int
25f2a 20 74 79 70 65 3b 0a 20 20 61 73 73 65 72 74 28   type;.  assert(
25f2b 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e   argc==1 );.  UN
25f2c 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61  USED_PARAMETER(a
25f2d 72 67 63 29 3b 0a 20 20 70 20 3d 20 73 71 6c 69  rgc);.  p = sqli
25f2e 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
25f2f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 73  ntext(context, s
25f30 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 20 74 79  izeof(*p));.  ty
25f31 70 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  pe = sqlite3_val
25f32 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28  ue_numeric_type(
25f33 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20  argv[0]);.  if( 
25f34 70 20 26 26 20 74 79 70 65 21 3d 53 51 4c 49 54  p && type!=SQLIT
25f35 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 70 2d  E_NULL ){.    p-
25f36 3e 63 6e 74 2b 2b 3b 0a 20 20 20 20 69 66 28 20  >cnt++;.    if( 
25f37 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f 49 4e 54  type==SQLITE_INT
25f38 45 47 45 52 20 29 7b 0a 20 20 20 20 20 20 69 36  EGER ){.      i6
25f39 34 20 76 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  4 v = sqlite3_va
25f3a 6c 75 65 5f 69 6e 74 36 34 28 61 72 67 76 5b 30  lue_int64(argv[0
25f3b 5d 29 3b 0a 20 20 20 20 20 20 70 2d 3e 72 53 75  ]);.      p->rSu
25f3c 6d 20 2b 3d 20 76 3b 0a 20 20 20 20 20 20 69 66  m += v;.      if
25f3d 28 20 28 70 2d 3e 61 70 70 72 6f 78 7c 70 2d 3e  ( (p->approx|p->
25f3e 6f 76 65 72 66 6c 6f 77 29 3d 3d 30 20 29 7b 0a  overflow)==0 ){.
25f3f 20 20 20 20 20 20 20 20 69 36 34 20 69 4e 65 77          i64 iNew
25f40 53 75 6d 20 3d 20 70 2d 3e 69 53 75 6d 20 2b 20  Sum = p->iSum + 
25f41 76 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 73  v;.        int s
25f42 31 20 3d 20 28 69 6e 74 29 28 70 2d 3e 69 53 75  1 = (int)(p->iSu
25f43 6d 20 3e 3e 20 28 73 69 7a 65 6f 66 28 69 36 34  m >> (sizeof(i64
25f44 29 2a 38 2d 31 29 29 3b 0a 20 20 20 20 20 20 20  )*8-1));.       
25f45 20 69 6e 74 20 73 32 20 3d 20 28 69 6e 74 29 28   int s2 = (int)(
25f46 76 20 20 20 20 20 20 20 3e 3e 20 28 73 69 7a 65  v       >> (size
25f47 6f 66 28 69 36 34 29 2a 38 2d 31 29 29 3b 0a 20  of(i64)*8-1));. 
25f48 20 20 20 20 20 20 20 69 6e 74 20 73 33 20 3d 20         int s3 = 
25f49 28 69 6e 74 29 28 69 4e 65 77 53 75 6d 20 3e 3e  (int)(iNewSum >>
25f4a 20 28 73 69 7a 65 6f 66 28 69 36 34 29 2a 38 2d   (sizeof(i64)*8-
25f4b 31 29 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  1));.        p->
25f4c 6f 76 65 72 66 6c 6f 77 20 3d 20 28 28 73 31 26  overflow = ((s1&
25f4d 73 32 26 7e 73 33 29 20 7c 20 28 7e 73 31 26 7e  s2&~s3) | (~s1&~
25f4e 73 32 26 73 33 29 29 3f 31 3a 30 3b 0a 20 20 20  s2&s3))?1:0;.   
25f4f 20 20 20 20 20 70 2d 3e 69 53 75 6d 20 3d 20 69       p->iSum = i
25f50 4e 65 77 53 75 6d 3b 0a 20 20 20 20 20 20 7d 0a  NewSum;.      }.
25f51 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
25f52 20 70 2d 3e 72 53 75 6d 20 2b 3d 20 73 71 6c 69   p->rSum += sqli
25f53 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65  te3_value_double
25f54 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20  (argv[0]);.     
25f55 20 70 2d 3e 61 70 70 72 6f 78 20 3d 20 31 3b 0a   p->approx = 1;.
25f56 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74      }.  }.}.stat
25f57 69 63 20 76 6f 69 64 20 73 75 6d 46 69 6e 61 6c  ic void sumFinal
25f58 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ize(sqlite3_cont
25f59 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20  ext *context){. 
25f5a 20 53 75 6d 43 74 78 20 2a 70 3b 0a 20 20 70 20   SumCtx *p;.  p 
25f5b 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67  = sqlite3_aggreg
25f5c 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74  ate_context(cont
25f5d 65 78 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70  ext, 0);.  if( p
25f5e 20 26 26 20 70 2d 3e 63 6e 74 3e 30 20 29 7b 0a   && p->cnt>0 ){.
25f5f 20 20 20 20 69 66 28 20 70 2d 3e 6f 76 65 72 66      if( p->overf
25f60 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  low ){.      sql
25f61 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
25f62 72 28 63 6f 6e 74 65 78 74 2c 22 69 6e 74 65 67  r(context,"integ
25f63 65 72 20 6f 76 65 72 66 6c 6f 77 22 2c 2d 31 29  er overflow",-1)
25f64 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
25f65 70 2d 3e 61 70 70 72 6f 78 20 29 7b 0a 20 20 20  p->approx ){.   
25f66 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
25f67 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74  t_double(context
25f68 2c 20 70 2d 3e 72 53 75 6d 29 3b 0a 20 20 20 20  , p->rSum);.    
25f69 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
25f6a 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36  ite3_result_int6
25f6b 34 28 63 6f 6e 74 65 78 74 2c 20 70 2d 3e 69 53  4(context, p->iS
25f6c 75 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  um);.    }.  }.}
25f6d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 76 67  .static void avg
25f6e 46 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33  Finalize(sqlite3
25f6f 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
25f70 74 29 7b 0a 20 20 53 75 6d 43 74 78 20 2a 70 3b  t){.  SumCtx *p;
25f71 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61  .  p = sqlite3_a
25f72 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74  ggregate_context
25f73 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20  (context, 0);.  
25f74 69 66 28 20 70 20 26 26 20 70 2d 3e 63 6e 74 3e  if( p && p->cnt>
25f75 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
25f76 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63  _result_double(c
25f77 6f 6e 74 65 78 74 2c 20 70 2d 3e 72 53 75 6d 2f  ontext, p->rSum/
25f78 28 64 6f 75 62 6c 65 29 70 2d 3e 63 6e 74 29 3b  (double)p->cnt);
25f79 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f  .  }.}.static vo
25f7a 69 64 20 74 6f 74 61 6c 46 69 6e 61 6c 69 7a 65  id totalFinalize
25f7b 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
25f7c 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 53 75   *context){.  Su
25f7d 6d 43 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73  mCtx *p;.  p = s
25f7e 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65  qlite3_aggregate
25f7f 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74  _context(context
25f80 2c 20 30 29 3b 0a 20 20 2f 2a 20 28 64 6f 75 62  , 0);.  /* (doub
25f81 6c 65 29 30 20 49 6e 20 63 61 73 65 20 6f 66 20  le)0 In case of 
25f82 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41  SQLITE_OMIT_FLOA
25f83 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f  TING_POINT... */
25f84 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
25f85 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74  t_double(context
25f86 2c 20 70 20 3f 20 70 2d 3e 72 53 75 6d 20 3a 20  , p ? p->rSum : 
25f87 28 64 6f 75 62 6c 65 29 30 29 3b 0a 7d 0a 0a 2f  (double)0);.}../
25f88 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
25f89 6e 67 20 73 74 72 75 63 74 75 72 65 20 6b 65 65  ng structure kee
25f8a 70 73 20 74 72 61 63 6b 20 6f 66 20 73 74 61 74  ps track of stat
25f8b 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f  e information fo
25f8c 72 20 74 68 65 0a 2a 2a 20 63 6f 75 6e 74 28 29  r the.** count()
25f8d 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
25f8e 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ion..*/.typedef 
25f8f 73 74 72 75 63 74 20 43 6f 75 6e 74 43 74 78 20  struct CountCtx 
25f90 43 6f 75 6e 74 43 74 78 3b 0a 73 74 72 75 63 74  CountCtx;.struct
25f91 20 43 6f 75 6e 74 43 74 78 20 7b 0a 20 20 69 36   CountCtx {.  i6
25f92 34 20 6e 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52  4 n;.};../*.** R
25f93 6f 75 74 69 6e 65 73 20 74 6f 20 69 6d 70 6c 65  outines to imple
25f94 6d 65 6e 74 20 74 68 65 20 63 6f 75 6e 74 28 29  ment the count()
25f95 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
25f96 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ion..*/.static v
25f97 6f 69 64 20 63 6f 75 6e 74 53 74 65 70 28 73 71  oid countStep(sq
25f98 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
25f99 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
25f9a 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
25f9b 2a 2a 61 72 67 76 29 7b 0a 20 20 43 6f 75 6e 74  **argv){.  Count
25f9c 43 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71  Ctx *p;.  p = sq
25f9d 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f  lite3_aggregate_
25f9e 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  context(context,
25f9f 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 20   sizeof(*p));.  
25fa0 69 66 28 20 28 61 72 67 63 3d 3d 30 20 7c 7c 20  if( (argc==0 || 
25fa1 53 51 4c 49 54 45 5f 4e 55 4c 4c 21 3d 73 71 6c  SQLITE_NULL!=sql
25fa2 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
25fa3 61 72 67 76 5b 30 5d 29 29 20 26 26 20 70 20 29  argv[0])) && p )
25fa4 7b 0a 20 20 20 20 70 2d 3e 6e 2b 2b 3b 0a 20 20  {.    p->n++;.  
25fa5 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
25fa6 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45  E_OMIT_DEPRECATE
25fa7 44 0a 20 20 2f 2a 20 54 68 65 20 73 71 6c 69 74  D.  /* The sqlit
25fa8 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 75  e3_aggregate_cou
25fa9 6e 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73  nt() function is
25faa 20 64 65 70 72 65 63 61 74 65 64 2e 20 20 42 75   deprecated.  Bu
25fab 74 20 6a 75 73 74 20 74 6f 20 6d 61 6b 65 0a 20  t just to make. 
25fac 20 2a 2a 20 73 75 72 65 20 69 74 20 73 74 69 6c   ** sure it stil
25fad 6c 20 6f 70 65 72 61 74 65 73 20 63 6f 72 72 65  l operates corre
25fae 63 74 6c 79 2c 20 76 65 72 69 66 79 20 74 68 61  ctly, verify tha
25faf 74 20 69 74 73 20 63 6f 75 6e 74 20 61 67 72 65  t its count agre
25fb0 65 73 20 77 69 74 68 20 6f 75 72 20 0a 20 20 2a  es with our .  *
25fb1 2a 20 69 6e 74 65 72 6e 61 6c 20 63 6f 75 6e 74  * internal count
25fb2 20 77 68 65 6e 20 75 73 69 6e 67 20 63 6f 75 6e   when using coun
25fb3 74 28 2a 29 20 61 6e 64 20 77 68 65 6e 20 74 68  t(*) and when th
25fb4 65 20 74 6f 74 61 6c 20 63 6f 75 6e 74 20 63 61  e total count ca
25fb5 6e 20 62 65 0a 20 20 2a 2a 20 65 78 70 72 65 73  n be.  ** expres
25fb6 73 65 64 20 61 73 20 61 20 33 32 2d 62 69 74 20  sed as a 32-bit 
25fb7 69 6e 74 65 67 65 72 2e 20 2a 2f 0a 20 20 61 73  integer. */.  as
25fb8 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 7c 7c  sert( argc==1 ||
25fb9 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 3e 30 78   p==0 || p->n>0x
25fba 37 66 66 66 66 66 66 66 0a 20 20 20 20 20 20 20  7fffffff.       
25fbb 20 20 20 7c 7c 20 70 2d 3e 6e 3d 3d 73 71 6c 69     || p->n==sqli
25fbc 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
25fbd 75 6e 74 28 63 6f 6e 74 65 78 74 29 20 29 3b 0a  unt(context) );.
25fbe 23 65 6e 64 69 66 0a 7d 20 20 20 0a 73 74 61 74  #endif.}   .stat
25fbf 69 63 20 76 6f 69 64 20 63 6f 75 6e 74 46 69 6e  ic void countFin
25fc0 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f  alize(sqlite3_co
25fc1 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b  ntext *context){
25fc2 0a 20 20 43 6f 75 6e 74 43 74 78 20 2a 70 3b 0a  .  CountCtx *p;.
25fc3 20 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67    p = sqlite3_ag
25fc4 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28  gregate_context(
25fc5 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 73  context, 0);.  s
25fc6 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
25fc7 74 36 34 28 63 6f 6e 74 65 78 74 2c 20 70 20 3f  t64(context, p ?
25fc8 20 70 2d 3e 6e 20 3a 20 30 29 3b 0a 7d 0a 0a 2f   p->n : 0);.}../
25fc9 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20 74 6f  *.** Routines to
25fca 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 69 6e 28 29   implement min()
25fcb 20 61 6e 64 20 6d 61 78 28 29 20 61 67 67 72 65   and max() aggre
25fcc 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a  gate functions..
25fcd 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d  */.static void m
25fce 69 6e 6d 61 78 53 74 65 70 28 0a 20 20 73 71 6c  inmaxStep(.  sql
25fcf 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
25fd0 6e 74 65 78 74 2c 20 0a 20 20 69 6e 74 20 4e 6f  ntext, .  int No
25fd1 74 55 73 65 64 2c 20 0a 20 20 73 71 6c 69 74 65  tUsed, .  sqlite
25fd2 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
25fd3 7b 0a 20 20 4d 65 6d 20 2a 70 41 72 67 20 20 3d  {.  Mem *pArg  =
25fd4 20 28 4d 65 6d 20 2a 29 61 72 67 76 5b 30 5d 3b   (Mem *)argv[0];
25fd5 0a 20 20 4d 65 6d 20 2a 70 42 65 73 74 3b 0a 20  .  Mem *pBest;. 
25fd6 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
25fd7 52 28 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 20 69  R(NotUsed);..  i
25fd8 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f( sqlite3_value
25fd9 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d  _type(argv[0])==
25fda 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65  SQLITE_NULL ) re
25fdb 74 75 72 6e 3b 0a 20 20 70 42 65 73 74 20 3d 20  turn;.  pBest = 
25fdc 28 4d 65 6d 20 2a 29 73 71 6c 69 74 65 33 5f 61  (Mem *)sqlite3_a
25fdd 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74  ggregate_context
25fde 28 63 6f 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66  (context, sizeof
25fdf 28 2a 70 42 65 73 74 29 29 3b 0a 20 20 69 66 28  (*pBest));.  if(
25fe0 20 21 70 42 65 73 74 20 29 20 72 65 74 75 72 6e   !pBest ) return
25fe1 3b 0a 0a 20 20 69 66 28 20 70 42 65 73 74 2d 3e  ;..  if( pBest->
25fe2 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 69 6e 74  flags ){.    int
25fe3 20 6d 61 78 3b 0a 20 20 20 20 69 6e 74 20 63 6d   max;.    int cm
25fe4 70 3b 0a 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a  p;.    CollSeq *
25fe5 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47  pColl = sqlite3G
25fe6 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 63 6f  etFuncCollSeq(co
25fe7 6e 74 65 78 74 29 3b 0a 20 20 20 20 2f 2a 20 54  ntext);.    /* T
25fe8 68 69 73 20 73 74 65 70 20 66 75 6e 63 74 69 6f  his step functio
25fe9 6e 20 69 73 20 75 73 65 64 20 66 6f 72 20 62 6f  n is used for bo
25fea 74 68 20 74 68 65 20 6d 69 6e 28 29 20 61 6e 64  th the min() and
25feb 20 6d 61 78 28 29 20 61 67 67 72 65 67 61 74 65   max() aggregate
25fec 73 2c 0a 20 20 20 20 2a 2a 20 74 68 65 20 6f 6e  s,.    ** the on
25fed 6c 79 20 64 69 66 66 65 72 65 6e 63 65 20 62 65  ly difference be
25fee 74 77 65 65 6e 20 74 68 65 20 74 77 6f 20 62 65  tween the two be
25fef 69 6e 67 20 74 68 61 74 20 74 68 65 20 73 65 6e  ing that the sen
25ff0 73 65 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a  se of the.    **
25ff1 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 69   comparison is i
25ff2 6e 76 65 72 74 65 64 2e 20 46 6f 72 20 74 68 65  nverted. For the
25ff3 20 6d 61 78 28 29 20 61 67 67 72 65 67 61 74 65   max() aggregate
25ff4 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 73 71 6c  , the.    ** sql
25ff5 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29  ite3_user_data()
25ff6 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
25ff7 73 20 28 76 6f 69 64 20 2a 29 2d 31 2e 20 46 6f  s (void *)-1. Fo
25ff8 72 20 6d 69 6e 28 29 20 69 74 0a 20 20 20 20 2a  r min() it.    *
25ff9 2a 20 72 65 74 75 72 6e 73 20 28 76 6f 69 64 20  * returns (void 
25ffa 2a 29 64 62 2c 20 77 68 65 72 65 20 64 62 20 69  *)db, where db i
25ffb 73 20 74 68 65 20 73 71 6c 69 74 65 33 2a 20 64  s the sqlite3* d
25ffc 61 74 61 62 61 73 65 20 70 6f 69 6e 74 65 72 2e  atabase pointer.
25ffd 0a 20 20 20 20 2a 2a 20 54 68 65 72 65 66 6f 72  .    ** Therefor
25ffe 65 20 74 68 65 20 6e 65 78 74 20 73 74 61 74 65  e the next state
25fff 6d 65 6e 74 20 73 65 74 73 20 76 61 72 69 61 62  ment sets variab
26000 6c 65 20 27 6d 61 78 27 20 74 6f 20 31 20 66 6f  le 'max' to 1 fo
26001 72 20 74 68 65 20 6d 61 78 28 29 0a 20 20 20 20  r the max().    
26002 2a 2a 20 61 67 67 72 65 67 61 74 65 2c 20 6f 72  ** aggregate, or
26003 20 30 20 66 6f 72 20 6d 69 6e 28 29 2e 0a 20 20   0 for min()..  
26004 20 20 2a 2f 0a 20 20 20 20 6d 61 78 20 3d 20 73    */.    max = s
26005 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61  qlite3_user_data
26006 28 63 6f 6e 74 65 78 74 29 21 3d 30 3b 0a 20 20  (context)!=0;.  
26007 20 20 63 6d 70 20 3d 20 73 71 6c 69 74 65 33 4d    cmp = sqlite3M
26008 65 6d 43 6f 6d 70 61 72 65 28 70 42 65 73 74 2c  emCompare(pBest,
26009 20 70 41 72 67 2c 20 70 43 6f 6c 6c 29 3b 0a 20   pArg, pColl);. 
2600a 20 20 20 69 66 28 20 28 6d 61 78 20 26 26 20 63     if( (max && c
2600b 6d 70 3c 30 29 20 7c 7c 20 28 21 6d 61 78 20 26  mp<0) || (!max &
2600c 26 20 63 6d 70 3e 30 29 20 29 7b 0a 20 20 20 20  & cmp>0) ){.    
2600d 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
2600e 43 6f 70 79 28 70 42 65 73 74 2c 20 70 41 72 67  Copy(pBest, pArg
2600f 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
26010 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
26011 65 4d 65 6d 43 6f 70 79 28 70 42 65 73 74 2c 20  eMemCopy(pBest, 
26012 70 41 72 67 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61  pArg);.  }.}.sta
26013 74 69 63 20 76 6f 69 64 20 6d 69 6e 4d 61 78 46  tic void minMaxF
26014 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f  inalize(sqlite3_
26015 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
26016 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  ){.  sqlite3_val
26017 75 65 20 2a 70 52 65 73 3b 0a 20 20 70 52 65 73  ue *pRes;.  pRes
26018 20 3d 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75   = (sqlite3_valu
26019 65 20 2a 29 73 71 6c 69 74 65 33 5f 61 67 67 72  e *)sqlite3_aggr
2601a 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f  egate_context(co
2601b 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 69 66 28  ntext, 0);.  if(
2601c 20 70 52 65 73 20 29 7b 0a 20 20 20 20 69 66 28   pRes ){.    if(
2601d 20 41 4c 57 41 59 53 28 70 52 65 73 2d 3e 66 6c   ALWAYS(pRes->fl
2601e 61 67 73 29 20 29 7b 0a 20 20 20 20 20 20 73 71  ags) ){.      sq
2601f 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c  lite3_result_val
26020 75 65 28 63 6f 6e 74 65 78 74 2c 20 70 52 65 73  ue(context, pRes
26021 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
26022 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
26023 73 65 28 70 52 65 73 29 3b 0a 20 20 7d 0a 7d 0a  se(pRes);.  }.}.
26024 0a 2f 2a 0a 2a 2a 20 67 72 6f 75 70 5f 63 6f 6e  ./*.** group_con
26025 63 61 74 28 45 58 50 52 2c 20 3f 53 45 50 41 52  cat(EXPR, ?SEPAR
26026 41 54 4f 52 3f 29 0a 2a 2f 0a 73 74 61 74 69 63  ATOR?).*/.static
26027 20 76 6f 69 64 20 67 72 6f 75 70 43 6f 6e 63 61   void groupConca
26028 74 53 74 65 70 28 0a 20 20 73 71 6c 69 74 65 33  tStep(.  sqlite3
26029 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
2602a 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20  t,.  int argc,. 
2602b 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
2602c 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74  *argv.){.  const
2602d 20 63 68 61 72 20 2a 7a 56 61 6c 3b 0a 20 20 53   char *zVal;.  S
2602e 74 72 41 63 63 75 6d 20 2a 70 41 63 63 75 6d 3b  trAccum *pAccum;
2602f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
26030 53 65 70 3b 0a 20 20 69 6e 74 20 6e 56 61 6c 2c  Sep;.  int nVal,
26031 20 6e 53 65 70 3b 0a 20 20 61 73 73 65 72 74 28   nSep;.  assert(
26032 20 61 72 67 63 3d 3d 31 20 7c 7c 20 61 72 67 63   argc==1 || argc
26033 3d 3d 32 20 29 3b 0a 20 20 69 66 28 20 73 71 6c  ==2 );.  if( sql
26034 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
26035 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45  argv[0])==SQLITE
26036 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a  _NULL ) return;.
26037 20 20 70 41 63 63 75 6d 20 3d 20 28 53 74 72 41    pAccum = (StrA
26038 63 63 75 6d 2a 29 73 71 6c 69 74 65 33 5f 61 67  ccum*)sqlite3_ag
26039 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28  gregate_context(
2603a 63 6f 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 28  context, sizeof(
2603b 2a 70 41 63 63 75 6d 29 29 3b 0a 0a 20 20 69 66  *pAccum));..  if
2603c 28 20 70 41 63 63 75 6d 20 29 7b 0a 20 20 20 20  ( pAccum ){.    
2603d 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71  sqlite3 *db = sq
2603e 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62  lite3_context_db
2603f 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29  _handle(context)
26040 3b 0a 20 20 20 20 69 6e 74 20 66 69 72 73 74 54  ;.    int firstT
26041 65 72 6d 20 3d 20 70 41 63 63 75 6d 2d 3e 75 73  erm = pAccum->us
26042 65 4d 61 6c 6c 6f 63 3d 3d 30 3b 0a 20 20 20 20  eMalloc==0;.    
26043 70 41 63 63 75 6d 2d 3e 75 73 65 4d 61 6c 6c 6f  pAccum->useMallo
26044 63 20 3d 20 31 3b 0a 20 20 20 20 70 41 63 63 75  c = 1;.    pAccu
26045 6d 2d 3e 6d 78 41 6c 6c 6f 63 20 3d 20 64 62 2d  m->mxAlloc = db-
26046 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
26047 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20  IMIT_LENGTH];.  
26048 20 20 69 66 28 20 21 66 69 72 73 74 54 65 72 6d    if( !firstTerm
26049 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 72   ){.      if( ar
2604a 67 63 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20 20  gc==2 ){.       
2604b 20 7a 53 65 70 20 3d 20 28 63 68 61 72 2a 29 73   zSep = (char*)s
2604c 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
2604d 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20  t(argv[1]);.    
2604e 20 20 20 20 6e 53 65 70 20 3d 20 73 71 6c 69 74      nSep = sqlit
2604f 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
26050 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 20 20 7d  rgv[1]);.      }
26051 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 53  else{.        zS
26052 65 70 20 3d 20 22 2c 22 3b 0a 20 20 20 20 20 20  ep = ",";.      
26053 20 20 6e 53 65 70 20 3d 20 31 3b 0a 20 20 20 20    nSep = 1;.    
26054 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
26055 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28  3StrAccumAppend(
26056 70 41 63 63 75 6d 2c 20 7a 53 65 70 2c 20 6e 53  pAccum, zSep, nS
26057 65 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a  ep);.    }.    z
26058 56 61 6c 20 3d 20 28 63 68 61 72 2a 29 73 71 6c  Val = (char*)sql
26059 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
2605a 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 56  argv[0]);.    nV
2605b 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  al = sqlite3_val
2605c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d  ue_bytes(argv[0]
2605d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74  );.    sqlite3St
2605e 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63  rAccumAppend(pAc
2605f 63 75 6d 2c 20 7a 56 61 6c 2c 20 6e 56 61 6c 29  cum, zVal, nVal)
26060 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76  ;.  }.}.static v
26061 6f 69 64 20 67 72 6f 75 70 43 6f 6e 63 61 74 46  oid groupConcatF
26062 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f  inalize(sqlite3_
26063 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
26064 29 7b 0a 20 20 53 74 72 41 63 63 75 6d 20 2a 70  ){.  StrAccum *p
26065 41 63 63 75 6d 3b 0a 20 20 70 41 63 63 75 6d 20  Accum;.  pAccum 
26066 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67  = sqlite3_aggreg
26067 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74  ate_context(cont
26068 65 78 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70  ext, 0);.  if( p
26069 41 63 63 75 6d 20 29 7b 0a 20 20 20 20 69 66 28  Accum ){.    if(
2606a 20 70 41 63 63 75 6d 2d 3e 74 6f 6f 42 69 67 20   pAccum->tooBig 
2606b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2606c 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f  _result_error_to
2606d 6f 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20  obig(context);. 
2606e 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 41 63     }else if( pAc
2606f 63 75 6d 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  cum->mallocFaile
26070 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  d ){.      sqlit
26071 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
26072 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a  nomem(context);.
26073 20 20 20 20 7d 65 6c 73 65 7b 20 20 20 20 0a 20      }else{    . 
26074 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
26075 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
26076 2c 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75  , sqlite3StrAccu
26077 6d 46 69 6e 69 73 68 28 70 41 63 63 75 6d 29 2c  mFinish(pAccum),
26078 20 2d 31 2c 20 0a 20 20 20 20 20 20 20 20 20 20   -1, .          
26079 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2607a 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20  sqlite3_free);. 
2607b 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
2607c 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
2607d 72 65 67 69 73 74 65 72 65 64 20 61 6c 6c 20 6f  registered all o
2607e 66 20 74 68 65 20 61 62 6f 76 65 20 43 20 66 75  f the above C fu
2607f 6e 63 74 69 6f 6e 73 20 61 73 20 53 51 4c 0a 2a  nctions as SQL.*
26080 2a 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68  * functions.  Th
26081 69 73 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65  is should be the
26082 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e   only routine in
26083 20 74 68 69 73 20 66 69 6c 65 20 77 69 74 68 0a   this file with.
26084 2a 2a 20 65 78 74 65 72 6e 61 6c 20 6c 69 6e 6b  ** external link
26085 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  age..*/.SQLITE_P
26086 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
26087 74 65 33 52 65 67 69 73 74 65 72 42 75 69 6c 74  te3RegisterBuilt
26088 69 6e 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69  inFunctions(sqli
26089 74 65 33 20 2a 64 62 29 7b 0a 23 69 66 6e 64 65  te3 *db){.#ifnde
2608a 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c  f SQLITE_OMIT_AL
2608b 54 45 52 54 41 42 4c 45 0a 20 20 73 71 6c 69 74  TERTABLE.  sqlit
2608c 65 33 41 6c 74 65 72 46 75 6e 63 74 69 6f 6e 73  e3AlterFunctions
2608d 28 64 62 29 3b 0a 23 65 6e 64 69 66 0a 20 20 69  (db);.#endif.  i
2608e 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  f( !db->mallocFa
2608f 69 6c 65 64 20 29 7b 0a 20 20 20 20 69 6e 74 20  iled ){.    int 
26090 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 6f 76 65  rc = sqlite3_ove
26091 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 64  rload_function(d
26092 62 2c 20 22 4d 41 54 43 48 22 2c 20 32 29 3b 0a  b, "MATCH", 2);.
26093 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d      assert( rc==
26094 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20  SQLITE_NOMEM || 
26095 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc==SQLITE_OK );
26096 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
26097 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20  ITE_NOMEM ){.   
26098 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
26099 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  led = 1;.    }. 
2609a 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20   }.}../*.** Set 
2609b 74 68 65 20 4c 49 4b 45 4f 50 54 20 66 6c 61 67  the LIKEOPT flag
2609c 20 6f 6e 20 74 68 65 20 32 2d 61 72 67 75 6d 65   on the 2-argume
2609d 6e 74 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68  nt function with
2609e 20 74 68 65 20 67 69 76 65 6e 20 6e 61 6d 65 2e   the given name.
2609f 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
260a0 73 65 74 4c 69 6b 65 4f 70 74 46 6c 61 67 28 73  setLikeOptFlag(s
260a1 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73  qlite3 *db, cons
260a2 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 75  t char *zName, u
260a3 38 20 66 6c 61 67 56 61 6c 29 7b 0a 20 20 46 75  8 flagVal){.  Fu
260a4 6e 63 44 65 66 20 2a 70 44 65 66 3b 0a 20 20 70  ncDef *pDef;.  p
260a5 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  Def = sqlite3Fin
260a6 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 4e  dFunction(db, zN
260a7 61 6d 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c  ame, sqlite3Strl
260a8 65 6e 33 30 28 7a 4e 61 6d 65 29 2c 0a 20 20 20  en30(zName),.   
260a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
260aa 20 20 20 20 20 20 20 20 20 20 32 2c 20 53 51 4c            2, SQL
260ab 49 54 45 5f 55 54 46 38 2c 20 30 29 3b 0a 20 20  ITE_UTF8, 0);.  
260ac 69 66 28 20 41 4c 57 41 59 53 28 70 44 65 66 29  if( ALWAYS(pDef)
260ad 20 29 7b 0a 20 20 20 20 70 44 65 66 2d 3e 66 6c   ){.    pDef->fl
260ae 61 67 73 20 3d 20 66 6c 61 67 56 61 6c 3b 0a 20  ags = flagVal;. 
260af 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69   }.}../*.** Regi
260b0 73 74 65 72 20 74 68 65 20 62 75 69 6c 74 2d 69  ster the built-i
260b1 6e 20 4c 49 4b 45 20 61 6e 64 20 47 4c 4f 42 20  n LIKE and GLOB 
260b2 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 65 20  functions.  The 
260b3 63 61 73 65 53 65 6e 73 69 74 69 76 65 0a 2a 2a  caseSensitive.**
260b4 20 70 61 72 61 6d 65 74 65 72 20 64 65 74 65 72   parameter deter
260b5 6d 69 6e 65 73 20 77 68 65 74 68 65 72 20 6f 72  mines whether or
260b6 20 6e 6f 74 20 74 68 65 20 4c 49 4b 45 20 6f 70   not the LIKE op
260b7 65 72 61 74 6f 72 20 69 73 20 63 61 73 65 0a 2a  erator is case.*
260b8 2a 20 73 65 6e 73 69 74 69 76 65 2e 20 20 47 4c  * sensitive.  GL
260b9 4f 42 20 69 73 20 61 6c 77 61 79 73 20 63 61 73  OB is always cas
260ba 65 20 73 65 6e 73 69 74 69 76 65 2e 0a 2a 2f 0a  e sensitive..*/.
260bb 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
260bc 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73  oid sqlite3Regis
260bd 74 65 72 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 73  terLikeFunctions
260be 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
260bf 74 20 63 61 73 65 53 65 6e 73 69 74 69 76 65 29  t caseSensitive)
260c0 7b 0a 20 20 73 74 72 75 63 74 20 63 6f 6d 70 61  {.  struct compa
260c1 72 65 49 6e 66 6f 20 2a 70 49 6e 66 6f 3b 0a 20  reInfo *pInfo;. 
260c2 20 69 66 28 20 63 61 73 65 53 65 6e 73 69 74 69   if( caseSensiti
260c3 76 65 20 29 7b 0a 20 20 20 20 70 49 6e 66 6f 20  ve ){.    pInfo 
260c4 3d 20 28 73 74 72 75 63 74 20 63 6f 6d 70 61 72  = (struct compar
260c5 65 49 6e 66 6f 2a 29 26 6c 69 6b 65 49 6e 66 6f  eInfo*)&likeInfo
260c6 41 6c 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  Alt;.  }else{.  
260c7 20 20 70 49 6e 66 6f 20 3d 20 28 73 74 72 75 63    pInfo = (struc
260c8 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 2a 29 26  t compareInfo*)&
260c9 6c 69 6b 65 49 6e 66 6f 4e 6f 72 6d 3b 0a 20 20  likeInfoNorm;.  
260ca 7d 0a 20 20 73 71 6c 69 74 65 33 43 72 65 61 74  }.  sqlite3Creat
260cb 65 46 75 6e 63 28 64 62 2c 20 22 6c 69 6b 65 22  eFunc(db, "like"
260cc 2c 20 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c  , 2, SQLITE_ANY,
260cd 20 70 49 6e 66 6f 2c 20 6c 69 6b 65 46 75 6e 63   pInfo, likeFunc
260ce 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74  , 0, 0);.  sqlit
260cf 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c  e3CreateFunc(db,
260d0 20 22 6c 69 6b 65 22 2c 20 33 2c 20 53 51 4c 49   "like", 3, SQLI
260d1 54 45 5f 41 4e 59 2c 20 70 49 6e 66 6f 2c 20 6c  TE_ANY, pInfo, l
260d2 69 6b 65 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a  ikeFunc, 0, 0);.
260d3 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46    sqlite3CreateF
260d4 75 6e 63 28 64 62 2c 20 22 67 6c 6f 62 22 2c 20  unc(db, "glob", 
260d5 32 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 0a  2, SQLITE_ANY, .
260d6 20 20 20 20 20 20 28 73 74 72 75 63 74 20 63 6f        (struct co
260d7 6d 70 61 72 65 49 6e 66 6f 2a 29 26 67 6c 6f 62  mpareInfo*)&glob
260d8 49 6e 66 6f 2c 20 6c 69 6b 65 46 75 6e 63 2c 20  Info, likeFunc, 
260d9 30 2c 30 29 3b 0a 20 20 73 65 74 4c 69 6b 65 4f  0,0);.  setLikeO
260da 70 74 46 6c 61 67 28 64 62 2c 20 22 67 6c 6f 62  ptFlag(db, "glob
260db 22 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c  ", SQLITE_FUNC_L
260dc 49 4b 45 20 7c 20 53 51 4c 49 54 45 5f 46 55 4e  IKE | SQLITE_FUN
260dd 43 5f 43 41 53 45 29 3b 0a 20 20 73 65 74 4c 69  C_CASE);.  setLi
260de 6b 65 4f 70 74 46 6c 61 67 28 64 62 2c 20 22 6c  keOptFlag(db, "l
260df 69 6b 65 22 2c 20 0a 20 20 20 20 20 20 63 61 73  ike", .      cas
260e0 65 53 65 6e 73 69 74 69 76 65 20 3f 20 28 53 51  eSensitive ? (SQ
260e1 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 20 7c  LITE_FUNC_LIKE |
260e2 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53   SQLITE_FUNC_CAS
260e3 45 29 20 3a 20 53 51 4c 49 54 45 5f 46 55 4e 43  E) : SQLITE_FUNC
260e4 5f 4c 49 4b 45 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  _LIKE);.}../*.**
260e5 20 70 45 78 70 72 20 70 6f 69 6e 74 73 20 74 6f   pExpr points to
260e6 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 77   an expression w
260e7 68 69 63 68 20 69 6d 70 6c 65 6d 65 6e 74 73 20  hich implements 
260e8 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 49 66 0a  a function.  If.
260e9 2a 2a 20 69 74 20 69 73 20 61 70 70 72 6f 70 72  ** it is appropr
260ea 69 61 74 65 20 74 6f 20 61 70 70 6c 79 20 74 68  iate to apply th
260eb 65 20 4c 49 4b 45 20 6f 70 74 69 6d 69 7a 61 74  e LIKE optimizat
260ec 69 6f 6e 20 74 6f 20 74 68 61 74 20 66 75 6e 63  ion to that func
260ed 74 69 6f 6e 0a 2a 2a 20 74 68 65 6e 20 73 65 74  tion.** then set
260ee 20 61 57 63 5b 30 5d 20 74 68 72 6f 75 67 68 20   aWc[0] through 
260ef 61 57 63 5b 32 5d 20 74 6f 20 74 68 65 20 77 69  aWc[2] to the wi
260f0 6c 64 63 61 72 64 20 63 68 61 72 61 63 74 65 72  ldcard character
260f1 73 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20  s and.** return 
260f2 54 52 55 45 2e 20 20 49 66 20 74 68 65 20 66 75  TRUE.  If the fu
260f3 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 61 20  nction is not a 
260f4 4c 49 4b 45 2d 73 74 79 6c 65 20 66 75 6e 63 74  LIKE-style funct
260f5 69 6f 6e 20 74 68 65 6e 0a 2a 2a 20 72 65 74 75  ion then.** retu
260f6 72 6e 20 46 41 4c 53 45 2e 0a 2a 2f 0a 53 51 4c  rn FALSE..*/.SQL
260f7 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
260f8 73 71 6c 69 74 65 33 49 73 4c 69 6b 65 46 75 6e  sqlite3IsLikeFun
260f9 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64  ction(sqlite3 *d
260fa 62 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20  b, Expr *pExpr, 
260fb 69 6e 74 20 2a 70 49 73 4e 6f 63 61 73 65 2c 20  int *pIsNocase, 
260fc 63 68 61 72 20 2a 61 57 63 29 7b 0a 20 20 46 75  char *aWc){.  Fu
260fd 6e 63 44 65 66 20 2a 70 44 65 66 3b 0a 20 20 69  ncDef *pDef;.  i
260fe 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b  f( pExpr->op!=TK
260ff 5f 46 55 4e 43 54 49 4f 4e 20 0a 20 20 20 7c 7c  _FUNCTION .   ||
26100 20 21 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74   !pExpr->x.pList
26101 20 0a 20 20 20 7c 7c 20 70 45 78 70 72 2d 3e 78   .   || pExpr->x
26102 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 32  .pList->nExpr!=2
26103 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  .  ){.    return
26104 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74   0;.  }.  assert
26105 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72  ( !ExprHasProper
26106 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73  ty(pExpr, EP_xIs
26107 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 70 44 65  Select) );.  pDe
26108 66 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46  f = sqlite3FindF
26109 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 45 78 70  unction(db, pExp
2610a 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 0a 20 20  r->u.zToken, .  
2610b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2610c 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2610d 65 33 53 74 72 6c 65 6e 33 30 28 70 45 78 70 72  e3Strlen30(pExpr
2610e 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 2c 0a 20 20 20  ->u.zToken),.   
2610f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26110 20 20 20 20 20 20 20 20 20 20 32 2c 20 53 51 4c            2, SQL
26111 49 54 45 5f 55 54 46 38 2c 20 30 29 3b 0a 20 20  ITE_UTF8, 0);.  
26112 69 66 28 20 4e 45 56 45 52 28 70 44 65 66 3d 3d  if( NEVER(pDef==
26113 30 29 20 7c 7c 20 28 70 44 65 66 2d 3e 66 6c 61  0) || (pDef->fla
26114 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43  gs & SQLITE_FUNC
26115 5f 4c 49 4b 45 29 3d 3d 30 20 29 7b 0a 20 20 20  _LIKE)==0 ){.   
26116 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a   return 0;.  }..
26117 20 20 2f 2a 20 54 68 65 20 6d 65 6d 63 70 79 28    /* The memcpy(
26118 29 20 73 74 61 74 65 6d 65 6e 74 20 61 73 73 75  ) statement assu
26119 6d 65 73 20 74 68 61 74 20 74 68 65 20 77 69 6c  mes that the wil
2611a 64 63 61 72 64 20 63 68 61 72 61 63 74 65 72 73  dcard characters
2611b 20 61 72 65 0a 20 20 2a 2a 20 74 68 65 20 66 69   are.  ** the fi
2611c 72 73 74 20 74 68 72 65 65 20 73 74 61 74 65 6d  rst three statem
2611d 65 6e 74 73 20 69 6e 20 74 68 65 20 63 6f 6d 70  ents in the comp
2611e 61 72 65 49 6e 66 6f 20 73 74 72 75 63 74 75 72  areInfo structur
2611f 65 2e 20 20 54 68 65 0a 20 20 2a 2a 20 61 73 73  e.  The.  ** ass
26120 65 72 74 73 28 29 20 74 68 61 74 20 66 6f 6c 6c  erts() that foll
26121 6f 77 20 76 65 72 69 66 79 20 74 68 61 74 20 61  ow verify that a
26122 73 73 75 6d 70 74 69 6f 6e 0a 20 20 2a 2f 0a 20  ssumption.  */. 
26123 20 6d 65 6d 63 70 79 28 61 57 63 2c 20 70 44 65   memcpy(aWc, pDe
26124 66 2d 3e 70 55 73 65 72 44 61 74 61 2c 20 33 29  f->pUserData, 3)
26125 3b 0a 20 20 61 73 73 65 72 74 28 20 28 63 68 61  ;.  assert( (cha
26126 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 20  r*)&likeInfoAlt 
26127 3d 3d 20 28 63 68 61 72 2a 29 26 6c 69 6b 65 49  == (char*)&likeI
26128 6e 66 6f 41 6c 74 2e 6d 61 74 63 68 41 6c 6c 20  nfoAlt.matchAll 
26129 29 3b 0a 20 20 61 73 73 65 72 74 28 20 26 28 28  );.  assert( &((
2612a 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41  char*)&likeInfoA
2612b 6c 74 29 5b 31 5d 20 3d 3d 20 28 63 68 61 72 2a  lt)[1] == (char*
2612c 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 2e 6d 61  )&likeInfoAlt.ma
2612d 74 63 68 4f 6e 65 20 29 3b 0a 20 20 61 73 73 65  tchOne );.  asse
2612e 72 74 28 20 26 28 28 63 68 61 72 2a 29 26 6c 69  rt( &((char*)&li
2612f 6b 65 49 6e 66 6f 41 6c 74 29 5b 32 5d 20 3d 3d  keInfoAlt)[2] ==
26130 20 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66   (char*)&likeInf
26131 6f 41 6c 74 2e 6d 61 74 63 68 53 65 74 20 29 3b  oAlt.matchSet );
26132 0a 20 20 2a 70 49 73 4e 6f 63 61 73 65 20 3d 20  .  *pIsNocase = 
26133 28 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53  (pDef->flags & S
26134 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29  QLITE_FUNC_CASE)
26135 3d 3d 30 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b  ==0;.  return 1;
26136 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 61 6c  .}../*.** All al
26137 6c 20 6f 66 20 74 68 65 20 46 75 6e 63 44 65 66  l of the FuncDef
26138 20 73 74 72 75 63 74 75 72 65 73 20 69 6e 20 74   structures in t
26139 68 65 20 61 42 75 69 6c 74 69 6e 46 75 6e 63 5b  he aBuiltinFunc[
2613a 5d 20 61 72 72 61 79 20 61 62 6f 76 65 0a 2a 2a  ] array above.**
2613b 20 74 6f 20 74 68 65 20 67 6c 6f 62 61 6c 20 66   to the global f
2613c 75 6e 63 74 69 6f 6e 20 68 61 73 68 20 74 61 62  unction hash tab
2613d 6c 65 2e 20 20 54 68 69 73 20 6f 63 63 75 72 73  le.  This occurs
2613e 20 61 74 20 73 74 61 72 74 2d 74 69 6d 65 20 28   at start-time (
2613f 61 73 0a 2a 2a 20 61 20 63 6f 6e 73 65 71 75 65  as.** a conseque
26140 6e 63 65 20 6f 66 20 63 61 6c 6c 69 6e 67 20 73  nce of calling s
26141 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
26142 65 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65  e())..**.** Afte
26143 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72  r this routine r
26144 75 6e 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  uns.*/.SQLITE_PR
26145 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
26146 65 33 52 65 67 69 73 74 65 72 47 6c 6f 62 61 6c  e3RegisterGlobal
26147 46 75 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29 7b  Functions(void){
26148 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 66  .  /*.  ** The f
26149 6f 6c 6c 6f 77 69 6e 67 20 61 72 72 61 79 20 68  ollowing array h
2614a 6f 6c 64 73 20 46 75 6e 63 44 65 66 20 73 74 72  olds FuncDef str
2614b 75 63 74 75 72 65 73 20 66 6f 72 20 61 6c 6c 20  uctures for all 
2614c 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 73  of the functions
2614d 0a 20 20 2a 2a 20 64 65 66 69 6e 65 64 20 69 6e  .  ** defined in
2614e 20 74 68 69 73 20 66 69 6c 65 2e 0a 20 20 2a 2a   this file..  **
2614f 0a 20 20 2a 2a 20 54 68 65 20 61 72 72 61 79 20  .  ** The array 
26150 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 73 74 61  cannot be consta
26151 6e 74 20 73 69 6e 63 65 20 63 68 61 6e 67 65 73  nt since changes
26152 20 61 72 65 20 6d 61 64 65 20 74 6f 20 74 68 65   are made to the
26153 0a 20 20 2a 2a 20 46 75 6e 63 44 65 66 2e 70 48  .  ** FuncDef.pH
26154 61 73 68 20 65 6c 65 6d 65 6e 74 73 20 61 74 20  ash elements at 
26155 73 74 61 72 74 2d 74 69 6d 65 2e 20 20 54 68 65  start-time.  The
26156 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 69   elements of thi
26157 73 20 61 72 72 61 79 0a 20 20 2a 2a 20 61 72 65  s array.  ** are
26158 20 72 65 61 64 2d 6f 6e 6c 79 20 61 66 74 65 72   read-only after
26159 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20   initialization 
2615a 69 73 20 63 6f 6d 70 6c 65 74 65 2e 0a 20 20 2a  is complete..  *
2615b 2f 0a 20 20 73 74 61 74 69 63 20 53 51 4c 49 54  /.  static SQLIT
2615c 45 5f 57 53 44 20 46 75 6e 63 44 65 66 20 61 42  E_WSD FuncDef aB
2615d 75 69 6c 74 69 6e 46 75 6e 63 5b 5d 20 3d 20 7b  uiltinFunc[] = {
2615e 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c 74  .    FUNCTION(lt
2615f 72 69 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20  rim,            
26160 20 20 31 2c 20 31 2c 20 30 2c 20 74 72 69 6d 46    1, 1, 0, trimF
26161 75 6e 63 20 20 20 20 20 20 20 20 20 29 2c 0a 20  unc         ),. 
26162 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c 74 72 69     FUNCTION(ltri
26163 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m,              
26164 32 2c 20 31 2c 20 30 2c 20 74 72 69 6d 46 75 6e  2, 1, 0, trimFun
26165 63 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20  c         ),.   
26166 20 46 55 4e 43 54 49 4f 4e 28 72 74 72 69 6d 2c   FUNCTION(rtrim,
26167 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c                1,
26168 20 32 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 20   2, 0, trimFunc 
26169 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46          ),.    F
2616a 55 4e 43 54 49 4f 4e 28 72 74 72 69 6d 2c 20 20  UNCTION(rtrim,  
2616b 20 20 20 20 20 20 20 20 20 20 20 20 32 2c 20 32              2, 2
2616c 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 20  , 0, trimFunc   
2616d 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e        ),.    FUN
2616e 43 54 49 4f 4e 28 74 72 69 6d 2c 20 20 20 20 20  CTION(trim,     
2616f 20 20 20 20 20 20 20 20 20 20 31 2c 20 33 2c 20            1, 3, 
26170 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 20 20 20  0, trimFunc     
26171 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54      ),.    FUNCT
26172 49 4f 4e 28 74 72 69 6d 2c 20 20 20 20 20 20 20  ION(trim,       
26173 20 20 20 20 20 20 20 20 32 2c 20 33 2c 20 30 2c          2, 3, 0,
26174 20 74 72 69 6d 46 75 6e 63 20 20 20 20 20 20 20   trimFunc       
26175 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
26176 4e 28 6d 69 6e 2c 20 20 20 20 20 20 20 20 20 20  N(min,          
26177 20 20 20 20 20 2d 31 2c 20 30 2c 20 31 2c 20 6d       -1, 0, 1, m
26178 69 6e 6d 61 78 46 75 6e 63 20 20 20 20 20 20 20  inmaxFunc       
26179 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
2617a 6d 69 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20  min,            
2617b 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 30 20 20      0, 0, 1, 0  
2617c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 29 2c                ),
2617d 0a 20 20 20 20 41 47 47 52 45 47 41 54 45 28 6d  .    AGGREGATE(m
2617e 69 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  in,             
2617f 20 20 31 2c 20 30 2c 20 31 2c 20 6d 69 6e 6d 61    1, 0, 1, minma
26180 78 53 74 65 70 2c 20 20 20 20 20 20 6d 69 6e 4d  xStep,      minM
26181 61 78 46 69 6e 61 6c 69 7a 65 20 29 2c 0a 20 20  axFinalize ),.  
26182 20 20 46 55 4e 43 54 49 4f 4e 28 6d 61 78 2c 20    FUNCTION(max, 
26183 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 31                -1
26184 2c 20 31 2c 20 31 2c 20 6d 69 6e 6d 61 78 46 75  , 1, 1, minmaxFu
26185 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20  nc       ),.    
26186 46 55 4e 43 54 49 4f 4e 28 6d 61 78 2c 20 20 20  FUNCTION(max,   
26187 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20               0, 
26188 31 2c 20 31 2c 20 30 20 20 20 20 20 20 20 20 20  1, 1, 0         
26189 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 41 47         ),.    AG
2618a 47 52 45 47 41 54 45 28 6d 61 78 2c 20 20 20 20  GREGATE(max,    
2618b 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 31 2c             1, 1,
2618c 20 31 2c 20 6d 69 6e 6d 61 78 53 74 65 70 2c 20   1, minmaxStep, 
2618d 20 20 20 20 20 6d 69 6e 4d 61 78 46 69 6e 61 6c       minMaxFinal
2618e 69 7a 65 20 29 2c 0a 20 20 20 20 46 55 4e 43 54  ize ),.    FUNCT
2618f 49 4f 4e 28 74 79 70 65 6f 66 2c 20 20 20 20 20  ION(typeof,     
26190 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c          1, 0, 0,
26191 20 74 79 70 65 6f 66 46 75 6e 63 20 20 20 20 20   typeofFunc     
26192 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
26193 4e 28 6c 65 6e 67 74 68 2c 20 20 20 20 20 20 20  N(length,       
26194 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 6c        1, 0, 0, l
26195 65 6e 67 74 68 46 75 6e 63 20 20 20 20 20 20 20  engthFunc       
26196 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
26197 73 75 62 73 74 72 2c 20 20 20 20 20 20 20 20 20  substr,         
26198 20 20 20 20 32 2c 20 30 2c 20 30 2c 20 73 75 62      2, 0, 0, sub
26199 73 74 72 46 75 6e 63 20 20 20 20 20 20 20 29 2c  strFunc       ),
2619a 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 75  .    FUNCTION(su
2619b 62 73 74 72 2c 20 20 20 20 20 20 20 20 20 20 20  bstr,           
2619c 20 20 33 2c 20 30 2c 20 30 2c 20 73 75 62 73 74    3, 0, 0, subst
2619d 72 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20  rFunc       ),. 
2619e 20 20 20 46 55 4e 43 54 49 4f 4e 28 61 62 73 2c     FUNCTION(abs,
2619f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
261a0 31 2c 20 30 2c 20 30 2c 20 61 62 73 46 75 6e 63  1, 0, 0, absFunc
261a1 20 20 20 20 20 20 20 20 20 20 29 2c 0a 23 69 66            ),.#if
261a2 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
261a3 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a  _FLOATING_POINT.
261a4 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 72 6f 75      FUNCTION(rou
261a5 6e 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  nd,             
261a6 20 31 2c 20 30 2c 20 30 2c 20 72 6f 75 6e 64 46   1, 0, 0, roundF
261a7 75 6e 63 20 20 20 20 20 20 20 20 29 2c 0a 20 20  unc        ),.  
261a8 20 20 46 55 4e 43 54 49 4f 4e 28 72 6f 75 6e 64    FUNCTION(round
261a9 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32  ,              2
261aa 2c 20 30 2c 20 30 2c 20 72 6f 75 6e 64 46 75 6e  , 0, 0, roundFun
261ab 63 20 20 20 20 20 20 20 20 29 2c 0a 23 65 6e 64  c        ),.#end
261ac 69 66 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  if.    FUNCTION(
261ad 75 70 70 65 72 2c 20 20 20 20 20 20 20 20 20 20  upper,          
261ae 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 75 70 70      1, 0, 0, upp
261af 65 72 46 75 6e 63 20 20 20 20 20 20 20 20 29 2c  erFunc        ),
261b0 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c 6f  .    FUNCTION(lo
261b1 77 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  wer,            
261b2 20 20 31 2c 20 30 2c 20 30 2c 20 6c 6f 77 65 72    1, 0, 0, lower
261b3 46 75 6e 63 20 20 20 20 20 20 20 20 29 2c 0a 20  Func        ),. 
261b4 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 6f 61 6c     FUNCTION(coal
261b5 65 73 63 65 2c 20 20 20 20 20 20 20 20 20 20 20  esce,           
261b6 31 2c 20 30 2c 20 30 2c 20 30 20 20 20 20 20 20  1, 0, 0, 0      
261b7 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20            ),.   
261b8 20 46 55 4e 43 54 49 4f 4e 28 63 6f 61 6c 65 73   FUNCTION(coales
261b9 63 65 2c 20 20 20 20 20 20 20 20 20 20 2d 31 2c  ce,          -1,
261ba 20 30 2c 20 30 2c 20 69 66 6e 75 6c 6c 46 75 6e   0, 0, ifnullFun
261bb 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46  c       ),.    F
261bc 55 4e 43 54 49 4f 4e 28 63 6f 61 6c 65 73 63 65  UNCTION(coalesce
261bd 2c 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 30  ,           0, 0
261be 2c 20 30 2c 20 30 20 20 20 20 20 20 20 20 20 20  , 0, 0          
261bf 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e        ),.    FUN
261c0 43 54 49 4f 4e 28 68 65 78 2c 20 20 20 20 20 20  CTION(hex,      
261c1 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20            1, 0, 
261c2 30 2c 20 68 65 78 46 75 6e 63 20 20 20 20 20 20  0, hexFunc      
261c3 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54      ),.    FUNCT
261c4 49 4f 4e 28 69 66 6e 75 6c 6c 2c 20 20 20 20 20  ION(ifnull,     
261c5 20 20 20 20 20 20 20 20 32 2c 20 30 2c 20 31 2c          2, 0, 1,
261c6 20 69 66 6e 75 6c 6c 46 75 6e 63 20 20 20 20 20   ifnullFunc     
261c7 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
261c8 4e 28 72 61 6e 64 6f 6d 2c 20 20 20 20 20 20 20  N(random,       
261c9 20 20 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 72        0, 0, 0, r
261ca 61 6e 64 6f 6d 46 75 6e 63 20 20 20 20 20 20 20  andomFunc       
261cb 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
261cc 72 61 6e 64 6f 6d 62 6c 6f 62 2c 20 20 20 20 20  randomblob,     
261cd 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 72 61 6e      1, 0, 0, ran
261ce 64 6f 6d 42 6c 6f 62 20 20 20 20 20 20 20 29 2c  domBlob       ),
261cf 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6e 75  .    FUNCTION(nu
261d0 6c 6c 69 66 2c 20 20 20 20 20 20 20 20 20 20 20  llif,           
261d1 20 20 32 2c 20 30 2c 20 31 2c 20 6e 75 6c 6c 69    2, 0, 1, nulli
261d2 66 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20  fFunc       ),. 
261d3 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 71 6c 69     FUNCTION(sqli
261d4 74 65 5f 76 65 72 73 69 6f 6e 2c 20 20 20 20 20  te_version,     
261d5 30 2c 20 30 2c 20 30 2c 20 76 65 72 73 69 6f 6e  0, 0, 0, version
261d6 46 75 6e 63 20 20 20 20 20 20 29 2c 0a 20 20 20  Func      ),.   
261d7 20 46 55 4e 43 54 49 4f 4e 28 73 71 6c 69 74 65   FUNCTION(sqlite
261d8 5f 73 6f 75 72 63 65 5f 69 64 2c 20 20 20 30 2c  _source_id,   0,
261d9 20 30 2c 20 30 2c 20 73 6f 75 72 63 65 69 64 46   0, 0, sourceidF
261da 75 6e 63 20 20 20 20 20 29 2c 0a 20 20 20 20 46  unc     ),.    F
261db 55 4e 43 54 49 4f 4e 28 71 75 6f 74 65 2c 20 20  UNCTION(quote,  
261dc 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30              1, 0
261dd 2c 20 30 2c 20 71 75 6f 74 65 46 75 6e 63 20 20  , 0, quoteFunc  
261de 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e        ),.    FUN
261df 43 54 49 4f 4e 28 6c 61 73 74 5f 69 6e 73 65 72  CTION(last_inser
261e0 74 5f 72 6f 77 69 64 2c 20 20 30 2c 20 30 2c 20  t_rowid,  0, 0, 
261e1 30 2c 20 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72  0, last_insert_r
261e2 6f 77 69 64 29 2c 0a 20 20 20 20 46 55 4e 43 54  owid),.    FUNCT
261e3 49 4f 4e 28 63 68 61 6e 67 65 73 2c 20 20 20 20  ION(changes,    
261e4 20 20 20 20 20 20 20 20 30 2c 20 30 2c 20 30 2c          0, 0, 0,
261e5 20 63 68 61 6e 67 65 73 20 20 20 20 20 20 20 20   changes        
261e6 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
261e7 4e 28 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 2c  N(total_changes,
261e8 20 20 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 74        0, 0, 0, t
261e9 6f 74 61 6c 5f 63 68 61 6e 67 65 73 20 20 20 20  otal_changes    
261ea 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
261eb 72 65 70 6c 61 63 65 2c 20 20 20 20 20 20 20 20  replace,        
261ec 20 20 20 20 33 2c 20 30 2c 20 30 2c 20 72 65 70      3, 0, 0, rep
261ed 6c 61 63 65 46 75 6e 63 20 20 20 20 20 20 29 2c  laceFunc      ),
261ee 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 7a 65  .    FUNCTION(ze
261ef 72 6f 62 6c 6f 62 2c 20 20 20 20 20 20 20 20 20  roblob,         
261f0 20 20 31 2c 20 30 2c 20 30 2c 20 7a 65 72 6f 62    1, 0, 0, zerob
261f1 6c 6f 62 46 75 6e 63 20 20 20 20 20 29 2c 0a 20  lobFunc     ),. 
261f2 20 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53   #ifdef SQLITE_S
261f3 4f 55 4e 44 45 58 0a 20 20 20 20 46 55 4e 43 54  OUNDEX.    FUNCT
261f4 49 4f 4e 28 73 6f 75 6e 64 65 78 2c 20 20 20 20  ION(soundex,    
261f5 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c          1, 0, 0,
261f6 20 73 6f 75 6e 64 65 78 46 75 6e 63 20 20 20 20   soundexFunc    
261f7 20 20 29 2c 0a 20 20 23 65 6e 64 69 66 0a 20 20    ),.  #endif.  
261f8 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
261f9 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49  MIT_LOAD_EXTENSI
261fa 4f 4e 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ON.    FUNCTION(
261fb 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 2c 20  load_extension, 
261fc 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 6c 6f 61      1, 0, 0, loa
261fd 64 45 78 74 20 20 20 20 20 20 20 20 20 20 29 2c  dExt          ),
261fe 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c 6f  .    FUNCTION(lo
261ff 61 64 5f 65 78 74 65 6e 73 69 6f 6e 2c 20 20 20  ad_extension,   
26200 20 20 32 2c 20 30 2c 20 30 2c 20 6c 6f 61 64 45    2, 0, 0, loadE
26201 78 74 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20  xt          ),. 
26202 20 23 65 6e 64 69 66 0a 20 20 20 20 41 47 47 52   #endif.    AGGR
26203 45 47 41 54 45 28 73 75 6d 2c 20 20 20 20 20 20  EGATE(sum,      
26204 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30           1, 0, 0
26205 2c 20 73 75 6d 53 74 65 70 2c 20 20 20 20 20 20  , sumStep,      
26206 20 20 20 73 75 6d 46 69 6e 61 6c 69 7a 65 20 20     sumFinalize  
26207 20 20 29 2c 0a 20 20 20 20 41 47 47 52 45 47 41    ),.    AGGREGA
26208 54 45 28 74 6f 74 61 6c 2c 20 20 20 20 20 20 20  TE(total,       
26209 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 73        1, 0, 0, s
2620a 75 6d 53 74 65 70 2c 20 20 20 20 20 20 20 20 20  umStep,         
2620b 74 6f 74 61 6c 46 69 6e 61 6c 69 7a 65 20 20 20  totalFinalize   
2620c 20 29 2c 0a 20 20 20 20 41 47 47 52 45 47 41 54   ),.    AGGREGAT
2620d 45 28 61 76 67 2c 20 20 20 20 20 20 20 20 20 20  E(avg,          
2620e 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 73 75       1, 0, 0, su
2620f 6d 53 74 65 70 2c 20 20 20 20 20 20 20 20 20 61  mStep,         a
26210 76 67 46 69 6e 61 6c 69 7a 65 20 20 20 20 29 2c  vgFinalize    ),
26211 0a 20 2f 2a 20 41 47 47 52 45 47 41 54 45 28 63  . /* AGGREGATE(c
26212 6f 75 6e 74 2c 20 20 20 20 20 20 20 20 20 20 20  ount,           
26213 20 20 30 2c 20 30 2c 20 30 2c 20 63 6f 75 6e 74    0, 0, 0, count
26214 53 74 65 70 2c 20 20 20 20 20 20 20 63 6f 75 6e  Step,       coun
26215 74 46 69 6e 61 6c 69 7a 65 20 20 29 2c 20 2a 2f  tFinalize  ), */
26216 0a 20 20 20 20 7b 30 2c 53 51 4c 49 54 45 5f 55  .    {0,SQLITE_U
26217 54 46 38 2c 53 51 4c 49 54 45 5f 46 55 4e 43 5f  TF8,SQLITE_FUNC_
26218 43 4f 55 4e 54 2c 30 2c 30 2c 30 2c 63 6f 75 6e  COUNT,0,0,0,coun
26219 74 53 74 65 70 2c 63 6f 75 6e 74 46 69 6e 61 6c  tStep,countFinal
2621a 69 7a 65 2c 22 63 6f 75 6e 74 22 2c 30 7d 2c 0a  ize,"count",0},.
2621b 20 20 20 20 41 47 47 52 45 47 41 54 45 28 63 6f      AGGREGATE(co
2621c 75 6e 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  unt,            
2621d 20 31 2c 20 30 2c 20 30 2c 20 63 6f 75 6e 74 53   1, 0, 0, countS
2621e 74 65 70 2c 20 20 20 20 20 20 20 63 6f 75 6e 74  tep,       count
2621f 46 69 6e 61 6c 69 7a 65 20 20 29 2c 0a 20 20 20  Finalize  ),.   
26220 20 41 47 47 52 45 47 41 54 45 28 67 72 6f 75 70   AGGREGATE(group
26221 5f 63 6f 6e 63 61 74 2c 20 20 20 20 20 20 31 2c  _concat,      1,
26222 20 30 2c 20 30 2c 20 67 72 6f 75 70 43 6f 6e 63   0, 0, groupConc
26223 61 74 53 74 65 70 2c 20 67 72 6f 75 70 43 6f 6e  atStep, groupCon
26224 63 61 74 46 69 6e 61 6c 69 7a 65 29 2c 0a 20 20  catFinalize),.  
26225 20 20 41 47 47 52 45 47 41 54 45 28 67 72 6f 75    AGGREGATE(grou
26226 70 5f 63 6f 6e 63 61 74 2c 20 20 20 20 20 20 32  p_concat,      2
26227 2c 20 30 2c 20 30 2c 20 67 72 6f 75 70 43 6f 6e  , 0, 0, groupCon
26228 63 61 74 53 74 65 70 2c 20 67 72 6f 75 70 43 6f  catStep, groupCo
26229 6e 63 61 74 46 69 6e 61 6c 69 7a 65 29 2c 0a 20  ncatFinalize),. 
2622a 20 0a 20 20 20 20 4c 49 4b 45 46 55 4e 43 28 67   .    LIKEFUNC(g
2622b 6c 6f 62 2c 20 32 2c 20 26 67 6c 6f 62 49 6e 66  lob, 2, &globInf
2622c 6f 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c  o, SQLITE_FUNC_L
2622d 49 4b 45 7c 53 51 4c 49 54 45 5f 46 55 4e 43 5f  IKE|SQLITE_FUNC_
2622e 43 41 53 45 29 2c 0a 20 20 23 69 66 64 65 66 20  CASE),.  #ifdef 
2622f 53 51 4c 49 54 45 5f 43 41 53 45 5f 53 45 4e 53  SQLITE_CASE_SENS
26230 49 54 49 56 45 5f 4c 49 4b 45 0a 20 20 20 20 4c  ITIVE_LIKE.    L
26231 49 4b 45 46 55 4e 43 28 6c 69 6b 65 2c 20 32 2c  IKEFUNC(like, 2,
26232 20 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 2c 20 53   &likeInfoAlt, S
26233 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 7c  QLITE_FUNC_LIKE|
26234 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45  SQLITE_FUNC_CASE
26235 29 2c 0a 20 20 20 20 4c 49 4b 45 46 55 4e 43 28  ),.    LIKEFUNC(
26236 6c 69 6b 65 2c 20 33 2c 20 26 6c 69 6b 65 49 6e  like, 3, &likeIn
26237 66 6f 41 6c 74 2c 20 53 51 4c 49 54 45 5f 46 55  foAlt, SQLITE_FU
26238 4e 43 5f 4c 49 4b 45 7c 53 51 4c 49 54 45 5f 46  NC_LIKE|SQLITE_F
26239 55 4e 43 5f 43 41 53 45 29 2c 0a 20 20 23 65 6c  UNC_CASE),.  #el
2623a 73 65 0a 20 20 20 20 4c 49 4b 45 46 55 4e 43 28  se.    LIKEFUNC(
2623b 6c 69 6b 65 2c 20 32 2c 20 26 6c 69 6b 65 49 6e  like, 2, &likeIn
2623c 66 6f 4e 6f 72 6d 2c 20 53 51 4c 49 54 45 5f 46  foNorm, SQLITE_F
2623d 55 4e 43 5f 4c 49 4b 45 29 2c 0a 20 20 20 20 4c  UNC_LIKE),.    L
2623e 49 4b 45 46 55 4e 43 28 6c 69 6b 65 2c 20 33 2c  IKEFUNC(like, 3,
2623f 20 26 6c 69 6b 65 49 6e 66 6f 4e 6f 72 6d 2c 20   &likeInfoNorm, 
26240 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45  SQLITE_FUNC_LIKE
26241 29 2c 0a 20 20 23 65 6e 64 69 66 0a 20 20 7d 3b  ),.  #endif.  };
26242 0a 0a 20 20 69 6e 74 20 69 3b 0a 20 20 46 75 6e  ..  int i;.  Fun
26243 63 44 65 66 48 61 73 68 20 2a 70 48 61 73 68 20  cDefHash *pHash 
26244 3d 20 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65  = &GLOBAL(FuncDe
26245 66 48 61 73 68 2c 20 73 71 6c 69 74 65 33 47 6c  fHash, sqlite3Gl
26246 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 29 3b 0a  obalFunctions);.
26247 20 20 46 75 6e 63 44 65 66 20 2a 61 46 75 6e 63    FuncDef *aFunc
26248 20 3d 20 28 46 75 6e 63 44 65 66 2a 29 26 47 4c   = (FuncDef*)&GL
26249 4f 42 41 4c 28 46 75 6e 63 44 65 66 2c 20 61 42  OBAL(FuncDef, aB
2624a 75 69 6c 74 69 6e 46 75 6e 63 29 3b 0a 0a 20 20  uiltinFunc);..  
2624b 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 61 79  for(i=0; i<Array
2624c 53 69 7a 65 28 61 42 75 69 6c 74 69 6e 46 75 6e  Size(aBuiltinFun
2624d 63 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71  c); i++){.    sq
2624e 6c 69 74 65 33 46 75 6e 63 44 65 66 49 6e 73 65  lite3FuncDefInse
2624f 72 74 28 70 48 61 73 68 2c 20 26 61 46 75 6e 63  rt(pHash, &aFunc
26250 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  [i]);.  }.  sqli
26251 74 65 33 52 65 67 69 73 74 65 72 44 61 74 65 54  te3RegisterDateT
26252 69 6d 65 46 75 6e 63 74 69 6f 6e 73 28 29 3b 0a  imeFunctions();.
26253 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
26254 2a 2a 20 45 6e 64 20 6f 66 20 66 75 6e 63 2e 63  ** End of func.c
26255 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
26256 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26257 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26258 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
26259 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 69 6e  ** Begin file in
2625a 73 65 72 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  sert.c *********
2625b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2625c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2625d 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65  */./*.** 2001 Se
2625e 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a  ptember 15.**.**
2625f 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
26260 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
26261 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
26262 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
26263 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
26264 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
26265 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
26266 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
26267 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
26268 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
26269 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
2626a 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
2626b 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
2626c 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
2626d 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
2626e 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
2626f 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
26270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26271 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26272 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26273 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26274 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
26275 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
26276 43 20 63 6f 64 65 20 72 6f 75 74 69 6e 65 73 20  C code routines 
26277 74 68 61 74 20 61 72 65 20 63 61 6c 6c 65 64 20  that are called 
26278 62 79 20 74 68 65 20 70 61 72 73 65 72 0a 2a 2a  by the parser.**
26279 20 74 6f 20 68 61 6e 64 6c 65 20 49 4e 53 45 52   to handle INSER
2627a 54 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20  T statements in 
2627b 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49  SQLite..**.** $I
2627c 64 3a 20 69 6e 73 65 72 74 2e 63 2c 76 20 31 2e  d: insert.c,v 1.
2627d 32 37 30 20 32 30 30 39 2f 30 37 2f 32 34 20 31  270 2009/07/24 1
2627e 37 3a 35 38 3a 35 33 20 64 61 6e 69 65 6c 6b 31  7:58:53 danielk1
2627f 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  977 Exp $.*/../*
26280 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
26281 65 20 74 68 61 74 20 77 69 6c 6c 20 6f 70 65 6e  e that will open
26282 20 61 20 74 61 62 6c 65 20 66 6f 72 20 72 65 61   a table for rea
26283 64 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ding..*/.SQLITE_
26284 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
26285 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 0a 20  ite3OpenTable(. 
26286 20 50 61 72 73 65 20 2a 70 2c 20 20 20 20 20 20   Parse *p,      
26287 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64   /* Generate cod
26288 65 20 69 6e 74 6f 20 74 68 69 73 20 56 44 42 45  e into this VDBE
26289 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c 20   */.  int iCur, 
2628a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72        /* The cur
2628b 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  sor number of th
2628c 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  e table */.  int
2628d 20 69 44 62 2c 20 20 20 20 20 20 20 20 2f 2a 20   iDb,        /* 
2628e 54 68 65 20 64 61 74 61 62 61 73 65 20 69 6e 64  The database ind
2628f 65 78 20 69 6e 20 73 71 6c 69 74 65 33 2e 61 44  ex in sqlite3.aD
26290 62 5b 5d 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  b[] */.  Table *
26291 70 54 61 62 2c 20 20 20 20 2f 2a 20 54 68 65 20  pTab,    /* The 
26292 74 61 62 6c 65 20 74 6f 20 62 65 20 6f 70 65 6e  table to be open
26293 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 63 6f  ed */.  int opco
26294 64 65 20 20 20 20 20 20 2f 2a 20 4f 50 5f 4f 70  de      /* OP_Op
26295 65 6e 52 65 61 64 20 6f 72 20 4f 50 5f 4f 70 65  enRead or OP_Ope
26296 6e 57 72 69 74 65 20 2a 2f 0a 29 7b 0a 20 20 56  nWrite */.){.  V
26297 64 62 65 20 2a 76 3b 0a 20 20 69 66 28 20 49 73  dbe *v;.  if( Is
26298 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 20  Virtual(pTab) ) 
26299 72 65 74 75 72 6e 3b 0a 20 20 76 20 3d 20 73 71  return;.  v = sq
2629a 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 29 3b  lite3GetVdbe(p);
2629b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 63 6f 64  .  assert( opcod
2629c 65 3d 3d 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20  e==OP_OpenWrite 
2629d 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70  || opcode==OP_Op
2629e 65 6e 52 65 61 64 20 29 3b 0a 20 20 73 71 6c 69  enRead );.  sqli
2629f 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20  te3TableLock(p, 
262a0 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c  iDb, pTab->tnum,
262a1 20 28 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65   (opcode==OP_Ope
262a2 6e 57 72 69 74 65 29 3f 31 3a 30 2c 20 70 54 61  nWrite)?1:0, pTa
262a3 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c  b->zName);.  sql
262a4 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
262a5 2c 20 6f 70 63 6f 64 65 2c 20 69 43 75 72 2c 20  , opcode, iCur, 
262a6 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 69 44 62 29  pTab->tnum, iDb)
262a7 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43  ;.  sqlite3VdbeC
262a8 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 53  hangeP4(v, -1, S
262a9 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52  QLITE_INT_TO_PTR
262aa 28 70 54 61 62 2d 3e 6e 43 6f 6c 29 2c 20 50 34  (pTab->nCol), P4
262ab 5f 49 4e 54 33 32 29 3b 0a 20 20 56 64 62 65 43  _INT32);.  VdbeC
262ac 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c  omment((v, "%s",
262ad 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 29 3b 0a   pTab->zName));.
262ae 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
262af 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
262b0 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79   column affinity
262b1 20 73 74 72 69 6e 67 20 61 73 73 6f 63 69 61 74   string associat
262b2 65 64 20 77 69 74 68 20 69 6e 64 65 78 0a 2a 2a  ed with index.**
262b3 20 70 49 64 78 2e 20 41 20 63 6f 6c 75 6d 6e 20   pIdx. A column 
262b4 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20  affinity string 
262b5 68 61 73 20 6f 6e 65 20 63 68 61 72 61 63 74 65  has one characte
262b6 72 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d  r for each colum
262b7 6e 20 69 6e 20 0a 2a 2a 20 74 68 65 20 74 61 62  n in .** the tab
262b8 6c 65 2c 20 61 63 63 6f 72 64 69 6e 67 20 74 6f  le, according to
262b9 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66   the affinity of
262ba 20 74 68 65 20 63 6f 6c 75 6d 6e 3a 0a 2a 2a 0a   the column:.**.
262bb 2a 2a 20 20 43 68 61 72 61 63 74 65 72 20 20 20  **  Character   
262bc 20 20 20 43 6f 6c 75 6d 6e 20 61 66 66 69 6e 69     Column affini
262bd 74 79 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  ty.**  ---------
262be 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
262bf 2d 2d 2d 2d 2d 0a 2a 2a 20 20 27 61 27 20 20 20  -----.**  'a'   
262c0 20 20 20 20 20 20 20 20 20 54 45 58 54 0a 2a 2a           TEXT.**
262c1 20 20 27 62 27 20 20 20 20 20 20 20 20 20 20 20    'b'           
262c2 20 4e 4f 4e 45 0a 2a 2a 20 20 27 63 27 20 20 20   NONE.**  'c'   
262c3 20 20 20 20 20 20 20 20 20 4e 55 4d 45 52 49 43           NUMERIC
262c4 0a 2a 2a 20 20 27 64 27 20 20 20 20 20 20 20 20  .**  'd'        
262c5 20 20 20 20 49 4e 54 45 47 45 52 0a 2a 2a 20 20      INTEGER.**  
262c6 27 65 27 20 20 20 20 20 20 20 20 20 20 20 20 52  'e'            R
262c7 45 41 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 74  EAL.**.** An ext
262c8 72 61 20 27 62 27 20 69 73 20 61 70 70 65 6e 64  ra 'b' is append
262c9 65 64 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66  ed to the end of
262ca 20 74 68 65 20 73 74 72 69 6e 67 20 74 6f 20 63   the string to c
262cb 6f 76 65 72 20 74 68 65 0a 2a 2a 20 72 6f 77 69  over the.** rowi
262cc 64 20 74 68 61 74 20 61 70 70 65 61 72 73 20 61  d that appears a
262cd 73 20 74 68 65 20 6c 61 73 74 20 63 6f 6c 75 6d  s the last colum
262ce 6e 20 69 6e 20 65 76 65 72 79 20 69 6e 64 65 78  n in every index
262cf 2e 0a 2a 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 20 66  ..**.** Memory f
262d0 6f 72 20 74 68 65 20 62 75 66 66 65 72 20 63 6f  or the buffer co
262d1 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 63 6f 6c  ntaining the col
262d2 75 6d 6e 20 69 6e 64 65 78 20 61 66 66 69 6e 69  umn index affini
262d3 74 79 20 73 74 72 69 6e 67 0a 2a 2a 20 69 73 20  ty string.** is 
262d4 6d 61 6e 61 67 65 64 20 61 6c 6f 6e 67 20 77 69  managed along wi
262d5 74 68 20 74 68 65 20 72 65 73 74 20 6f 66 20 74  th the rest of t
262d6 68 65 20 49 6e 64 65 78 20 73 74 72 75 63 74 75  he Index structu
262d7 72 65 2e 20 49 74 20 77 69 6c 6c 20 62 65 0a 2a  re. It will be.*
262d8 2a 20 72 65 6c 65 61 73 65 64 20 77 68 65 6e 20  * released when 
262d9 73 71 6c 69 74 65 33 44 65 6c 65 74 65 49 6e 64  sqlite3DeleteInd
262da 65 78 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 0a  ex() is called..
262db 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
262dc 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  E const char *sq
262dd 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69  lite3IndexAffini
262de 74 79 53 74 72 28 56 64 62 65 20 2a 76 2c 20 49  tyStr(Vdbe *v, I
262df 6e 64 65 78 20 2a 70 49 64 78 29 7b 0a 20 20 69  ndex *pIdx){.  i
262e0 66 28 20 21 70 49 64 78 2d 3e 7a 43 6f 6c 41 66  f( !pIdx->zColAf
262e1 66 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  f ){.    /* The 
262e2 66 69 72 73 74 20 74 69 6d 65 20 61 20 63 6f 6c  first time a col
262e3 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72  umn affinity str
262e4 69 6e 67 20 66 6f 72 20 61 20 70 61 72 74 69 63  ing for a partic
262e5 75 6c 61 72 20 69 6e 64 65 78 20 69 73 0a 20 20  ular index is.  
262e6 20 20 2a 2a 20 72 65 71 75 69 72 65 64 2c 20 69    ** required, i
262e7 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61  t is allocated a
262e8 6e 64 20 70 6f 70 75 6c 61 74 65 64 20 68 65 72  nd populated her
262e9 65 2e 20 49 74 20 69 73 20 74 68 65 6e 20 73 74  e. It is then st
262ea 6f 72 65 64 20 61 73 0a 20 20 20 20 2a 2a 20 61  ored as.    ** a
262eb 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20 49   member of the I
262ec 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 20 66  ndex structure f
262ed 6f 72 20 73 75 62 73 65 71 75 65 6e 74 20 75 73  or subsequent us
262ee 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  e..    **.    **
262ef 20 54 68 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69   The column affi
262f0 6e 69 74 79 20 73 74 72 69 6e 67 20 77 69 6c 6c  nity string will
262f1 20 65 76 65 6e 74 75 61 6c 6c 79 20 62 65 20 64   eventually be d
262f2 65 6c 65 74 65 64 20 62 79 0a 20 20 20 20 2a 2a  eleted by.    **
262f3 20 73 71 6c 69 74 65 44 65 6c 65 74 65 49 6e 64   sqliteDeleteInd
262f4 65 78 28 29 20 77 68 65 6e 20 74 68 65 20 49 6e  ex() when the In
262f5 64 65 78 20 73 74 72 75 63 74 75 72 65 20 69 74  dex structure it
262f6 73 65 6c 66 20 69 73 20 63 6c 65 61 6e 65 64 0a  self is cleaned.
262f7 20 20 20 20 2a 2a 20 75 70 2e 0a 20 20 20 20 2a      ** up..    *
262f8 2f 0a 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20  /.    int n;.   
262f9 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70   Table *pTab = p
262fa 49 64 78 2d 3e 70 54 61 62 6c 65 3b 0a 20 20 20  Idx->pTable;.   
262fb 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73   sqlite3 *db = s
262fc 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 3b  qlite3VdbeDb(v);
262fd 0a 20 20 20 20 70 49 64 78 2d 3e 7a 43 6f 6c 41  .    pIdx->zColA
262fe 66 66 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c  ff = (char *)sql
262ff 69 74 65 33 4d 61 6c 6c 6f 63 28 70 49 64 78 2d  ite3Malloc(pIdx-
26300 3e 6e 43 6f 6c 75 6d 6e 2b 32 29 3b 0a 20 20 20  >nColumn+2);.   
26301 20 69 66 28 20 21 70 49 64 78 2d 3e 7a 43 6f 6c   if( !pIdx->zCol
26302 41 66 66 20 29 7b 0a 20 20 20 20 20 20 64 62 2d  Aff ){.      db-
26303 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
26304 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  1;.      return 
26305 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72  0;.    }.    for
26306 28 6e 3d 30 3b 20 6e 3c 70 49 64 78 2d 3e 6e 43  (n=0; n<pIdx->nC
26307 6f 6c 75 6d 6e 3b 20 6e 2b 2b 29 7b 0a 20 20 20  olumn; n++){.   
26308 20 20 20 70 49 64 78 2d 3e 7a 43 6f 6c 41 66 66     pIdx->zColAff
26309 5b 6e 5d 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c  [n] = pTab->aCol
2630a 5b 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b  [pIdx->aiColumn[
2630b 6e 5d 5d 2e 61 66 66 69 6e 69 74 79 3b 0a 20 20  n]].affinity;.  
2630c 20 20 7d 0a 20 20 20 20 70 49 64 78 2d 3e 7a 43    }.    pIdx->zC
2630d 6f 6c 41 66 66 5b 6e 2b 2b 5d 20 3d 20 53 51 4c  olAff[n++] = SQL
2630e 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20  ITE_AFF_NONE;.  
2630f 20 20 70 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 5b    pIdx->zColAff[
26310 6e 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 0a 20 20  n] = 0;.  }. .  
26311 72 65 74 75 72 6e 20 70 49 64 78 2d 3e 7a 43 6f  return pIdx->zCo
26312 6c 41 66 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  lAff;.}../*.** S
26313 65 74 20 50 34 20 6f 66 20 74 68 65 20 6d 6f 73  et P4 of the mos
26314 74 20 72 65 63 65 6e 74 6c 79 20 69 6e 73 65 72  t recently inser
26315 74 65 64 20 6f 70 63 6f 64 65 20 74 6f 20 61 20  ted opcode to a 
26316 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 0a  column affinity.
26317 2a 2a 20 73 74 72 69 6e 67 20 66 6f 72 20 74 61  ** string for ta
26318 62 6c 65 20 70 54 61 62 2e 20 41 20 63 6f 6c 75  ble pTab. A colu
26319 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72 69  mn affinity stri
2631a 6e 67 20 68 61 73 20 6f 6e 65 20 63 68 61 72 61  ng has one chara
2631b 63 74 65 72 0a 2a 2a 20 66 6f 72 20 65 61 63 68  cter.** for each
2631c 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 65 64 20   column indexed 
2631d 62 79 20 74 68 65 20 69 6e 64 65 78 2c 20 61 63  by the index, ac
2631e 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 61  cording to the a
2631f 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65 0a 2a  ffinity of the.*
26320 2a 20 63 6f 6c 75 6d 6e 3a 0a 2a 2a 0a 2a 2a 20  * column:.**.** 
26321 20 43 68 61 72 61 63 74 65 72 20 20 20 20 20 20   Character      
26322 43 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 0a  Column affinity.
26323 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  **  ------------
26324 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26325 2d 2d 0a 2a 2a 20 20 27 61 27 20 20 20 20 20 20  --.**  'a'      
26326 20 20 20 20 20 20 54 45 58 54 0a 2a 2a 20 20 27        TEXT.**  '
26327 62 27 20 20 20 20 20 20 20 20 20 20 20 20 4e 4f  b'            NO
26328 4e 45 0a 2a 2a 20 20 27 63 27 20 20 20 20 20 20  NE.**  'c'      
26329 20 20 20 20 20 20 4e 55 4d 45 52 49 43 0a 2a 2a        NUMERIC.**
2632a 20 20 27 64 27 20 20 20 20 20 20 20 20 20 20 20    'd'           
2632b 20 49 4e 54 45 47 45 52 0a 2a 2a 20 20 27 65 27   INTEGER.**  'e'
2632c 20 20 20 20 20 20 20 20 20 20 20 20 52 45 41 4c              REAL
2632d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2632e 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 54  TE void sqlite3T
2632f 61 62 6c 65 41 66 66 69 6e 69 74 79 53 74 72 28  ableAffinityStr(
26330 56 64 62 65 20 2a 76 2c 20 54 61 62 6c 65 20 2a  Vdbe *v, Table *
26331 70 54 61 62 29 7b 0a 20 20 2f 2a 20 54 68 65 20  pTab){.  /* The 
26332 66 69 72 73 74 20 74 69 6d 65 20 61 20 63 6f 6c  first time a col
26333 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72  umn affinity str
26334 69 6e 67 20 66 6f 72 20 61 20 70 61 72 74 69 63  ing for a partic
26335 75 6c 61 72 20 74 61 62 6c 65 0a 20 20 2a 2a 20  ular table.  ** 
26336 69 73 20 72 65 71 75 69 72 65 64 2c 20 69 74 20  is required, it 
26337 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64  is allocated and
26338 20 70 6f 70 75 6c 61 74 65 64 20 68 65 72 65 2e   populated here.
26339 20 49 74 20 69 73 20 74 68 65 6e 20 0a 20 20 2a   It is then .  *
2633a 2a 20 73 74 6f 72 65 64 20 61 73 20 61 20 6d 65  * stored as a me
2633b 6d 62 65 72 20 6f 66 20 74 68 65 20 54 61 62 6c  mber of the Tabl
2633c 65 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20  e structure for 
2633d 73 75 62 73 65 71 75 65 6e 74 20 75 73 65 2e 0a  subsequent use..
2633e 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 63 6f    **.  ** The co
2633f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74  lumn affinity st
26340 72 69 6e 67 20 77 69 6c 6c 20 65 76 65 6e 74 75  ring will eventu
26341 61 6c 6c 79 20 62 65 20 64 65 6c 65 74 65 64 20  ally be deleted 
26342 62 79 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 44  by.  ** sqlite3D
26343 65 6c 65 74 65 54 61 62 6c 65 28 29 20 77 68 65  eleteTable() whe
26344 6e 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75  n the Table stru
26345 63 74 75 72 65 20 69 74 73 65 6c 66 20 69 73 20  cture itself is 
26346 63 6c 65 61 6e 65 64 20 75 70 2e 0a 20 20 2a 2f  cleaned up..  */
26347 0a 20 20 69 66 28 20 21 70 54 61 62 2d 3e 7a 43  .  if( !pTab->zC
26348 6f 6c 41 66 66 20 29 7b 0a 20 20 20 20 63 68 61  olAff ){.    cha
26349 72 20 2a 7a 43 6f 6c 41 66 66 3b 0a 20 20 20 20  r *zColAff;.    
2634a 69 6e 74 20 69 3b 0a 20 20 20 20 73 71 6c 69 74  int i;.    sqlit
2634b 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33  e3 *db = sqlite3
2634c 56 64 62 65 44 62 28 76 29 3b 0a 0a 20 20 20 20  VdbeDb(v);..    
2634d 7a 43 6f 6c 41 66 66 20 3d 20 28 63 68 61 72 20  zColAff = (char 
2634e 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28  *)sqlite3Malloc(
2634f 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 29 3b 0a 20  pTab->nCol+1);. 
26350 20 20 20 69 66 28 20 21 7a 43 6f 6c 41 66 66 20     if( !zColAff 
26351 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c  ){.      db->mal
26352 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
26353 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
26354 20 7d 0a 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   }..    for(i=0;
26355 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69   i<pTab->nCol; i
26356 2b 2b 29 7b 0a 20 20 20 20 20 20 7a 43 6f 6c 41  ++){.      zColA
26357 66 66 5b 69 5d 20 3d 20 70 54 61 62 2d 3e 61 43  ff[i] = pTab->aC
26358 6f 6c 5b 69 5d 2e 61 66 66 69 6e 69 74 79 3b 0a  ol[i].affinity;.
26359 20 20 20 20 7d 0a 20 20 20 20 7a 43 6f 6c 41 66      }.    zColAf
2635a 66 5b 70 54 61 62 2d 3e 6e 43 6f 6c 5d 20 3d 20  f[pTab->nCol] = 
2635b 27 5c 30 27 3b 0a 0a 20 20 20 20 70 54 61 62 2d  '\0';..    pTab-
2635c 3e 7a 43 6f 6c 41 66 66 20 3d 20 7a 43 6f 6c 41  >zColAff = zColA
2635d 66 66 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74  ff;.  }..  sqlit
2635e 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
2635f 2c 20 2d 31 2c 20 70 54 61 62 2d 3e 7a 43 6f 6c  , -1, pTab->zCol
26360 41 66 66 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  Aff, 0);.}../*.*
26361 2a 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72  * Return non-zer
26362 6f 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 70  o if the table p
26363 54 61 62 20 69 6e 20 64 61 74 61 62 61 73 65 20  Tab in database 
26364 69 44 62 20 6f 72 20 61 6e 79 20 6f 66 20 69 74  iDb or any of it
26365 73 20 69 6e 64 69 63 65 73 0a 2a 2a 20 68 61 76  s indices.** hav
26366 65 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 61 74  e been opened at
26367 20 61 6e 79 20 70 6f 69 6e 74 20 69 6e 20 74 68   any point in th
26368 65 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 62  e VDBE program b
26369 65 67 69 6e 6e 69 6e 67 20 61 74 20 6c 6f 63 61  eginning at loca
2636a 74 69 6f 6e 0a 2a 2a 20 69 53 74 61 72 74 41 64  tion.** iStartAd
2636b 64 72 20 74 68 72 6f 75 67 68 74 20 74 68 65 20  dr throught the 
2636c 65 6e 64 20 6f 66 20 74 68 65 20 70 72 6f 67 72  end of the progr
2636d 61 6d 2e 20 20 54 68 69 73 20 69 73 20 75 73 65  am.  This is use
2636e 64 20 74 6f 20 73 65 65 20 69 66 20 0a 2a 2a 20  d to see if .** 
2636f 61 20 73 74 61 74 65 6d 65 6e 74 20 6f 66 20 74  a statement of t
26370 68 65 20 66 6f 72 6d 20 20 22 49 4e 53 45 52 54  he form  "INSERT
26371 20 49 4e 54 4f 20 3c 69 44 62 2c 20 70 54 61 62   INTO <iDb, pTab
26372 3e 20 53 45 4c 45 43 54 20 2e 2e 2e 22 20 63 61  > SELECT ..." ca
26373 6e 20 0a 2a 2a 20 72 75 6e 20 77 69 74 68 6f 75  n .** run withou
26374 74 20 75 73 69 6e 67 20 74 65 6d 70 6f 72 61 72  t using temporar
26375 79 20 74 61 62 6c 65 20 66 6f 72 20 74 68 65 20  y table for the 
26376 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 53  results of the S
26377 45 4c 45 43 54 2e 20 0a 2a 2f 0a 73 74 61 74 69  ELECT. .*/.stati
26378 63 20 69 6e 74 20 72 65 61 64 73 54 61 62 6c 65  c int readsTable
26379 28 50 61 72 73 65 20 2a 70 2c 20 69 6e 74 20 69  (Parse *p, int i
2637a 53 74 61 72 74 41 64 64 72 2c 20 69 6e 74 20 69  StartAddr, int i
2637b 44 62 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 29  Db, Table *pTab)
2637c 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71  {.  Vdbe *v = sq
2637d 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 29 3b  lite3GetVdbe(p);
2637e 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
2637f 69 45 6e 64 20 3d 20 73 71 6c 69 74 65 33 56 64  iEnd = sqlite3Vd
26380 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
26381 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
26382 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
26383 4c 45 0a 20 20 56 54 61 62 6c 65 20 2a 70 56 54  LE.  VTable *pVT
26384 61 62 20 3d 20 49 73 56 69 72 74 75 61 6c 28 70  ab = IsVirtual(p
26385 54 61 62 29 20 3f 20 73 71 6c 69 74 65 33 47 65  Tab) ? sqlite3Ge
26386 74 56 54 61 62 6c 65 28 70 2d 3e 64 62 2c 20 70  tVTable(p->db, p
26387 54 61 62 29 20 3a 20 30 3b 0a 23 65 6e 64 69 66  Tab) : 0;.#endif
26388 0a 0a 20 20 66 6f 72 28 69 3d 69 53 74 61 72 74  ..  for(i=iStart
26389 41 64 64 72 3b 20 69 3c 69 45 6e 64 3b 20 69 2b  Addr; i<iEnd; i+
2638a 2b 29 7b 0a 20 20 20 20 56 64 62 65 4f 70 20 2a  +){.    VdbeOp *
2638b 70 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62  pOp = sqlite3Vdb
2638c 65 47 65 74 4f 70 28 76 2c 20 69 29 3b 0a 20 20  eGetOp(v, i);.  
2638d 20 20 61 73 73 65 72 74 28 20 70 4f 70 21 3d 30    assert( pOp!=0
2638e 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d   );.    if( pOp-
2638f 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e  >opcode==OP_Open
26390 52 65 61 64 20 26 26 20 70 4f 70 2d 3e 70 33 3d  Read && pOp->p3=
26391 3d 69 44 62 20 29 7b 0a 20 20 20 20 20 20 49 6e  =iDb ){.      In
26392 64 65 78 20 2a 70 49 6e 64 65 78 3b 0a 20 20 20  dex *pIndex;.   
26393 20 20 20 69 6e 74 20 74 6e 75 6d 20 3d 20 70 4f     int tnum = pO
26394 70 2d 3e 70 32 3b 0a 20 20 20 20 20 20 69 66 28  p->p2;.      if(
26395 20 74 6e 75 6d 3d 3d 70 54 61 62 2d 3e 74 6e 75   tnum==pTab->tnu
26396 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  m ){.        ret
26397 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  urn 1;.      }. 
26398 20 20 20 20 20 66 6f 72 28 70 49 6e 64 65 78 3d       for(pIndex=
26399 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49  pTab->pIndex; pI
2639a 6e 64 65 78 3b 20 70 49 6e 64 65 78 3d 70 49 6e  ndex; pIndex=pIn
2639b 64 65 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  dex->pNext){.   
2639c 20 20 20 20 20 69 66 28 20 74 6e 75 6d 3d 3d 70       if( tnum==p
2639d 49 6e 64 65 78 2d 3e 74 6e 75 6d 20 29 7b 0a 20  Index->tnum ){. 
2639e 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
2639f 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  1;.        }.   
263a0 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 6e 64     }.    }.#ifnd
263a1 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
263a2 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20  IRTUALTABLE.    
263a3 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d  if( pOp->opcode=
263a4 3d 4f 50 5f 56 4f 70 65 6e 20 26 26 20 70 4f 70  =OP_VOpen && pOp
263a5 2d 3e 70 34 2e 70 56 74 61 62 3d 3d 70 56 54 61  ->p4.pVtab==pVTa
263a6 62 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  b ){.      asser
263a7 74 28 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62  t( pOp->p4.pVtab
263a8 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  !=0 );.      ass
263a9 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65  ert( pOp->p4type
263aa 3d 3d 50 34 5f 56 54 41 42 20 29 3b 0a 20 20 20  ==P4_VTAB );.   
263ab 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
263ac 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20   }.#endif.  }.  
263ad 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 23 69 66  return 0;.}..#if
263ae 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
263af 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 2f  _AUTOINCREMENT./
263b0 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 6f 72 20 63  *.** Locate or c
263b1 72 65 61 74 65 20 61 6e 20 41 75 74 6f 69 6e 63  reate an Autoinc
263b2 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 61  Info structure a
263b3 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
263b4 61 62 6c 65 20 70 54 61 62 0a 2a 2a 20 77 68 69  able pTab.** whi
263b5 63 68 20 69 73 20 69 6e 20 64 61 74 61 62 61 73  ch is in databas
263b6 65 20 69 44 62 2e 20 20 52 65 74 75 72 6e 20 74  e iDb.  Return t
263b7 68 65 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62  he register numb
263b8 65 72 20 66 6f 72 20 74 68 65 20 72 65 67 69 73  er for the regis
263b9 74 65 72 0a 2a 2a 20 74 68 61 74 20 68 6f 6c 64  ter.** that hold
263ba 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 72 6f  s the maximum ro
263bb 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65  wid..**.** There
263bc 20 69 73 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20   is at most one 
263bd 41 75 74 6f 69 6e 63 49 6e 66 6f 20 73 74 72 75  AutoincInfo stru
263be 63 74 75 72 65 20 70 65 72 20 74 61 62 6c 65 20  cture per table 
263bf 65 76 65 6e 20 69 66 20 74 68 65 0a 2a 2a 20 73  even if the.** s
263c0 61 6d 65 20 74 61 62 6c 65 20 69 73 20 61 75 74  ame table is aut
263c1 6f 69 6e 63 72 65 6d 65 6e 74 65 64 20 6d 75 6c  oincremented mul
263c2 74 69 70 6c 65 20 74 69 6d 65 73 20 64 75 65 20  tiple times due 
263c3 74 6f 20 69 6e 73 65 72 74 73 20 77 69 74 68 69  to inserts withi
263c4 6e 0a 2a 2a 20 74 72 69 67 67 65 72 73 2e 20 20  n.** triggers.  
263c5 41 20 6e 65 77 20 41 75 74 6f 69 6e 63 49 6e 66  A new AutoincInf
263c6 6f 20 73 74 72 75 63 74 75 72 65 20 69 73 20 63  o structure is c
263c7 72 65 61 74 65 64 20 69 66 20 74 68 69 73 20 69  reated if this i
263c8 73 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 75  s the.** first u
263c9 73 65 20 6f 66 20 74 61 62 6c 65 20 70 54 61 62  se of table pTab
263ca 2e 20 20 4f 6e 20 32 6e 64 20 61 6e 64 20 73 75  .  On 2nd and su
263cb 62 73 65 71 75 65 6e 74 20 75 73 65 73 2c 20 74  bsequent uses, t
263cc 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 41  he original.** A
263cd 75 74 6f 69 6e 63 49 6e 66 6f 20 73 74 72 75 63  utoincInfo struc
263ce 74 75 72 65 20 69 73 20 75 73 65 64 2e 0a 2a 2a  ture is used..**
263cf 0a 2a 2a 20 54 68 72 65 65 20 6d 65 6d 6f 72 79  .** Three memory
263d0 20 6c 6f 63 61 74 69 6f 6e 73 20 61 72 65 20 61   locations are a
263d1 6c 6c 6f 63 61 74 65 64 3a 0a 2a 2a 0a 2a 2a 20  llocated:.**.** 
263d2 20 20 28 31 29 20 20 52 65 67 69 73 74 65 72 20    (1)  Register 
263d3 74 6f 20 68 6f 6c 64 20 74 68 65 20 6e 61 6d 65  to hold the name
263d4 20 6f 66 20 74 68 65 20 70 54 61 62 20 74 61 62   of the pTab tab
263d5 6c 65 2e 0a 2a 2a 20 20 20 28 32 29 20 20 52 65  le..**   (2)  Re
263d6 67 69 73 74 65 72 20 74 6f 20 68 6f 6c 64 20 74  gister to hold t
263d7 68 65 20 6d 61 78 69 6d 75 6d 20 52 4f 57 49 44  he maximum ROWID
263d8 20 6f 66 20 70 54 61 62 2e 0a 2a 2a 20 20 20 28   of pTab..**   (
263d9 33 29 20 20 52 65 67 69 73 74 65 72 20 74 6f 20  3)  Register to 
263da 68 6f 6c 64 20 74 68 65 20 72 6f 77 69 64 20 69  hold the rowid i
263db 6e 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  n sqlite_sequenc
263dc 65 20 6f 66 20 70 54 61 62 0a 2a 2a 0a 2a 2a 20  e of pTab.**.** 
263dd 54 68 65 20 32 6e 64 20 72 65 67 69 73 74 65 72  The 2nd register
263de 20 69 73 20 74 68 65 20 6f 6e 65 20 74 68 61 74   is the one that
263df 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 54   is returned.  T
263e0 68 61 74 20 69 73 20 61 6c 6c 20 74 68 65 0a 2a  hat is all the.*
263e1 2a 20 69 6e 73 65 72 74 20 72 6f 75 74 69 6e 65  * insert routine
263e2 20 6e 65 65 64 73 20 74 6f 20 6b 6e 6f 77 20 61   needs to know a
263e3 62 6f 75 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  bout..*/.static 
263e4 69 6e 74 20 61 75 74 6f 49 6e 63 42 65 67 69 6e  int autoIncBegin
263e5 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
263e6 65 2c 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69  e,      /* Parsi
263e7 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
263e8 69 6e 74 20 69 44 62 2c 20 20 20 20 20 20 20 20  int iDb,        
263e9 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
263ea 74 68 65 20 64 61 74 61 62 61 73 65 20 68 6f 6c  the database hol
263eb 64 69 6e 67 20 70 54 61 62 20 2a 2f 0a 20 20 54  ding pTab */.  T
263ec 61 62 6c 65 20 2a 70 54 61 62 20 20 20 20 20 20  able *pTab      
263ed 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20     /* The table 
263ee 77 65 20 61 72 65 20 77 72 69 74 69 6e 67 20 74  we are writing t
263ef 6f 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6d 65  o */.){.  int me
263f0 6d 49 64 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a  mId = 0;      /*
263f1 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e   Register holdin
263f2 67 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64 20  g maximum rowid 
263f3 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 74  */.  if( pTab->t
263f4 61 62 46 6c 61 67 73 20 26 20 54 46 5f 41 75 74  abFlags & TF_Aut
263f5 6f 69 6e 63 72 65 6d 65 6e 74 20 29 7b 0a 20 20  oincrement ){.  
263f6 20 20 50 61 72 73 65 20 2a 70 54 6f 70 6c 65 76    Parse *pToplev
263f7 65 6c 20 3d 20 73 71 6c 69 74 65 33 50 61 72 73  el = sqlite3Pars
263f8 65 54 6f 70 6c 65 76 65 6c 28 70 50 61 72 73 65  eToplevel(pParse
263f9 29 3b 0a 20 20 20 20 41 75 74 6f 69 6e 63 49 6e  );.    AutoincIn
263fa 66 6f 20 2a 70 49 6e 66 6f 3b 0a 0a 20 20 20 20  fo *pInfo;..    
263fb 70 49 6e 66 6f 20 3d 20 70 54 6f 70 6c 65 76 65  pInfo = pTopleve
263fc 6c 2d 3e 70 41 69 6e 63 3b 0a 20 20 20 20 77 68  l->pAinc;.    wh
263fd 69 6c 65 28 20 70 49 6e 66 6f 20 26 26 20 70 49  ile( pInfo && pI
263fe 6e 66 6f 2d 3e 70 54 61 62 21 3d 70 54 61 62 20  nfo->pTab!=pTab 
263ff 29 7b 20 70 49 6e 66 6f 20 3d 20 70 49 6e 66 6f  ){ pInfo = pInfo
26400 2d 3e 70 4e 65 78 74 3b 20 7d 0a 20 20 20 20 69  ->pNext; }.    i
26401 66 28 20 70 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20  f( pInfo==0 ){. 
26402 20 20 20 20 20 70 49 6e 66 6f 20 3d 20 73 71 6c       pInfo = sql
26403 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
26404 70 50 61 72 73 65 2d 3e 64 62 2c 20 73 69 7a 65  pParse->db, size
26405 6f 66 28 2a 70 49 6e 66 6f 29 29 3b 0a 20 20 20  of(*pInfo));.   
26406 20 20 20 69 66 28 20 70 49 6e 66 6f 3d 3d 30 20     if( pInfo==0 
26407 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20  ) return 0;.    
26408 20 20 70 49 6e 66 6f 2d 3e 70 4e 65 78 74 20 3d    pInfo->pNext =
26409 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 70 41 69 6e   pToplevel->pAin
2640a 63 3b 0a 20 20 20 20 20 20 70 54 6f 70 6c 65 76  c;.      pToplev
2640b 65 6c 2d 3e 70 41 69 6e 63 20 3d 20 70 49 6e 66  el->pAinc = pInf
2640c 6f 3b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e  o;.      pInfo->
2640d 70 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 20  pTab = pTab;.   
2640e 20 20 20 70 49 6e 66 6f 2d 3e 69 44 62 20 3d 20     pInfo->iDb = 
2640f 69 44 62 3b 0a 20 20 20 20 20 20 70 54 6f 70 6c  iDb;.      pTopl
26410 65 76 65 6c 2d 3e 6e 4d 65 6d 2b 2b 3b 20 20 20  evel->nMem++;   
26411 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
26412 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20 68 6f  * Register to ho
26413 6c 64 20 6e 61 6d 65 20 6f 66 20 74 61 62 6c 65  ld name of table
26414 20 2a 2f 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d   */.      pInfo-
26415 3e 72 65 67 43 74 72 20 3d 20 2b 2b 70 54 6f 70  >regCtr = ++pTop
26416 6c 65 76 65 6c 2d 3e 6e 4d 65 6d 3b 20 20 2f 2a  level->nMem;  /*
26417 20 4d 61 78 20 72 6f 77 69 64 20 72 65 67 69 73   Max rowid regis
26418 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 54 6f  ter */.      pTo
26419 70 6c 65 76 65 6c 2d 3e 6e 4d 65 6d 2b 2b 3b 20  plevel->nMem++; 
2641a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2641b 20 2f 2a 20 52 6f 77 69 64 20 69 6e 20 73 71 6c   /* Rowid in sql
2641c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 2a 2f 0a  ite_sequence */.
2641d 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 49 64 20      }.    memId 
2641e 3d 20 70 49 6e 66 6f 2d 3e 72 65 67 43 74 72 3b  = pInfo->regCtr;
2641f 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6d 65  .  }.  return me
26420 6d 49 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  mId;.}../*.** Th
26421 69 73 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72  is routine gener
26422 61 74 65 73 20 63 6f 64 65 20 74 68 61 74 20 77  ates code that w
26423 69 6c 6c 20 69 6e 69 74 69 61 6c 69 7a 65 20 61  ill initialize a
26424 6c 6c 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65 67  ll of the.** reg
26425 69 73 74 65 72 20 75 73 65 64 20 62 79 20 74 68  ister used by th
26426 65 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20  e autoincrement 
26427 74 72 61 63 6b 65 72 2e 20 20 0a 2a 2f 0a 53 51  tracker.  .*/.SQ
26428 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
26429 64 20 73 71 6c 69 74 65 33 41 75 74 6f 69 6e 63  d sqlite3Autoinc
2642a 72 65 6d 65 6e 74 42 65 67 69 6e 28 50 61 72 73  rementBegin(Pars
2642b 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 41 75  e *pParse){.  Au
2642c 74 6f 69 6e 63 49 6e 66 6f 20 2a 70 3b 20 20 20  toincInfo *p;   
2642d 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f           /* Info
2642e 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 6e  rmation about an
2642f 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a   AUTOINCREMENT *
26430 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  /.  sqlite3 *db 
26431 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 2f  = pParse->db;  /
26432 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  * The database c
26433 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 44  onnection */.  D
26434 62 20 2a 70 44 62 3b 20 20 20 20 20 20 20 20 20  b *pDb;         
26435 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
26436 61 62 61 73 65 20 6f 6e 6c 79 20 61 75 74 6f 69  abase only autoi
26437 6e 63 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  nc table */.  in
26438 74 20 6d 65 6d 49 64 3b 20 20 20 20 20 20 20 20  t memId;        
26439 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69           /* Regi
2643a 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 6d 61 78  ster holding max
2643b 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69 6e 74 20   rowid */.  int 
2643c 61 64 64 72 3b 20 20 20 20 20 20 20 20 20 20 20  addr;           
2643d 20 20 20 20 20 20 20 2f 2a 20 41 20 56 44 42 45         /* A VDBE
2643e 20 61 64 64 72 65 73 73 20 2a 2f 0a 20 20 56 64   address */.  Vd
2643f 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
26440 70 56 64 62 65 3b 20 20 20 2f 2a 20 56 44 42 45  pVdbe;   /* VDBE
26441 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74   under construct
26442 69 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69  ion */..  /* Thi
26443 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 65 76  s routine is nev
26444 65 72 20 63 61 6c 6c 65 64 20 64 75 72 69 6e 67  er called during
26445 20 74 72 69 67 67 65 72 2d 67 65 6e 65 72 61 74   trigger-generat
26446 69 6f 6e 2e 20 20 49 74 20 69 73 0a 20 20 2a 2a  ion.  It is.  **
26447 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72 6f   only called fro
26448 6d 20 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c 20  m the top-level 
26449 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  */.  assert( pPa
2644a 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62  rse->pTriggerTab
2644b 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
2644c 20 70 50 61 72 73 65 3d 3d 73 71 6c 69 74 65 33   pParse==sqlite3
2644d 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70 50  ParseToplevel(pP
2644e 61 72 73 65 29 20 29 3b 0a 0a 20 20 61 73 73 65  arse) );..  asse
2644f 72 74 28 20 76 20 29 3b 20 20 20 2f 2a 20 57 65  rt( v );   /* We
26450 20 66 61 69 6c 65 64 20 6c 6f 6e 67 20 61 67 6f   failed long ago
26451 20 69 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20   if this is not 
26452 73 6f 20 2a 2f 0a 20 20 66 6f 72 28 70 20 3d 20  so */.  for(p = 
26453 70 50 61 72 73 65 2d 3e 70 41 69 6e 63 3b 20 70  pParse->pAinc; p
26454 3b 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 29 7b  ; p = p->pNext){
26455 0a 20 20 20 20 70 44 62 20 3d 20 26 64 62 2d 3e  .    pDb = &db->
26456 61 44 62 5b 70 2d 3e 69 44 62 5d 3b 0a 20 20 20  aDb[p->iDb];.   
26457 20 6d 65 6d 49 64 20 3d 20 70 2d 3e 72 65 67 43   memId = p->regC
26458 74 72 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f  tr;.    sqlite3O
26459 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c  penTable(pParse,
2645a 20 30 2c 20 70 2d 3e 69 44 62 2c 20 70 44 62 2d   0, p->iDb, pDb-
2645b 3e 70 53 63 68 65 6d 61 2d 3e 70 53 65 71 54 61  >pSchema->pSeqTa
2645c 62 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29 3b  b, OP_OpenRead);
2645d 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69  .    addr = sqli
2645e 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
2645f 64 72 28 76 29 3b 0a 20 20 20 20 73 71 6c 69 74  dr(v);.    sqlit
26460 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
26461 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 6d  OP_String8, 0, m
26462 65 6d 49 64 2d 31 2c 20 30 2c 20 70 2d 3e 70 54  emId-1, 0, p->pT
26463 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20  ab->zName, 0);. 
26464 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
26465 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e  dOp2(v, OP_Rewin
26466 64 2c 20 30 2c 20 61 64 64 72 2b 39 29 3b 0a 20  d, 0, addr+9);. 
26467 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
26468 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d  dOp3(v, OP_Colum
26469 6e 2c 20 30 2c 20 30 2c 20 6d 65 6d 49 64 29 3b  n, 0, 0, memId);
2646a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2646b 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 65 2c  AddOp3(v, OP_Ne,
2646c 20 6d 65 6d 49 64 2d 31 2c 20 61 64 64 72 2b 37   memId-1, addr+7
2646d 2c 20 6d 65 6d 49 64 29 3b 0a 20 20 20 20 73 71  , memId);.    sq
2646e 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
2646f 35 28 76 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50  5(v, SQLITE_JUMP
26470 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 73 71 6c  IFNULL);.    sql
26471 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
26472 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 30 2c 20 6d  , OP_Rowid, 0, m
26473 65 6d 49 64 2b 31 29 3b 0a 20 20 20 20 73 71 6c  emId+1);.    sql
26474 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
26475 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 30 2c 20  , OP_Column, 0, 
26476 31 2c 20 6d 65 6d 49 64 29 3b 0a 20 20 20 20 73  1, memId);.    s
26477 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
26478 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
26479 61 64 64 72 2b 39 29 3b 0a 20 20 20 20 73 71 6c  addr+9);.    sql
2647a 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2647b 2c 20 4f 50 5f 4e 65 78 74 2c 20 30 2c 20 61 64  , OP_Next, 0, ad
2647c 64 72 2b 32 29 3b 0a 20 20 20 20 73 71 6c 69 74  dr+2);.    sqlit
2647d 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2647e 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 6d  OP_Integer, 0, m
2647f 65 6d 49 64 29 3b 0a 20 20 20 20 73 71 6c 69 74  emId);.    sqlit
26480 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20  e3VdbeAddOp0(v, 
26481 4f 50 5f 43 6c 6f 73 65 29 3b 0a 20 20 7d 0a 7d  OP_Close);.  }.}
26482 0a 0a 2f 2a 0a 2a 2a 20 55 70 64 61 74 65 20 74  ../*.** Update t
26483 68 65 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64  he maximum rowid
26484 20 66 6f 72 20 61 6e 20 61 75 74 6f 69 6e 63 72   for an autoincr
26485 65 6d 65 6e 74 20 63 61 6c 63 75 6c 61 74 69 6f  ement calculatio
26486 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  n..**.** This ro
26487 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 62 65 20  utine should be 
26488 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 20  called when the 
26489 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63 6b  top of the stack
2648a 20 68 6f 6c 64 73 20 61 0a 2a 2a 20 6e 65 77 20   holds a.** new 
2648b 72 6f 77 69 64 20 74 68 61 74 20 69 73 20 61 62  rowid that is ab
2648c 6f 75 74 20 74 6f 20 62 65 20 69 6e 73 65 72 74  out to be insert
2648d 65 64 2e 20 20 49 66 20 74 68 61 74 20 6e 65 77  ed.  If that new
2648e 20 72 6f 77 69 64 20 69 73 0a 2a 2a 20 6c 61 72   rowid is.** lar
2648f 67 65 72 20 74 68 61 6e 20 74 68 65 20 6d 61 78  ger than the max
26490 69 6d 75 6d 20 72 6f 77 69 64 20 69 6e 20 74 68  imum rowid in th
26491 65 20 6d 65 6d 49 64 20 6d 65 6d 6f 72 79 20 63  e memId memory c
26492 65 6c 6c 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a  ell, then the.**
26493 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 73 20   memory cell is 
26494 75 70 64 61 74 65 64 2e 20 20 54 68 65 20 73 74  updated.  The st
26495 61 63 6b 20 69 73 20 75 6e 63 68 61 6e 67 65 64  ack is unchanged
26496 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
26497 20 61 75 74 6f 49 6e 63 53 74 65 70 28 50 61 72   autoIncStep(Par
26498 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
26499 6d 65 6d 49 64 2c 20 69 6e 74 20 72 65 67 52 6f  memId, int regRo
2649a 77 69 64 29 7b 0a 20 20 69 66 28 20 6d 65 6d 49  wid){.  if( memI
2649b 64 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  d>0 ){.    sqlit
2649c 65 33 56 64 62 65 41 64 64 4f 70 32 28 70 50 61  e3VdbeAddOp2(pPa
2649d 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 4d  rse->pVdbe, OP_M
2649e 65 6d 4d 61 78 2c 20 6d 65 6d 49 64 2c 20 72 65  emMax, memId, re
2649f 67 52 6f 77 69 64 29 3b 0a 20 20 7d 0a 7d 0a 0a  gRowid);.  }.}..
264a0 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
264a1 6e 65 20 67 65 6e 65 72 61 74 65 73 20 74 68 65  ne generates the
264a2 20 63 6f 64 65 20 6e 65 65 64 65 64 20 74 6f 20   code needed to 
264a3 77 72 69 74 65 20 61 75 74 6f 69 6e 63 72 65 6d  write autoincrem
264a4 65 6e 74 0a 2a 2a 20 6d 61 78 69 6d 75 6d 20 72  ent.** maximum r
264a5 6f 77 69 64 20 76 61 6c 75 65 73 20 62 61 63 6b  owid values back
264a6 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65   into the sqlite
264a7 5f 73 65 71 75 65 6e 63 65 20 72 65 67 69 73 74  _sequence regist
264a8 65 72 2e 0a 2a 2a 20 45 76 65 72 79 20 73 74 61  er..** Every sta
264a9 74 65 6d 65 6e 74 20 74 68 61 74 20 6d 69 67 68  tement that migh
264aa 74 20 64 6f 20 61 6e 20 49 4e 53 45 52 54 20 69  t do an INSERT i
264ab 6e 74 6f 20 61 6e 20 61 75 74 6f 69 6e 63 72 65  nto an autoincre
264ac 6d 65 6e 74 0a 2a 2a 20 74 61 62 6c 65 20 28 65  ment.** table (e
264ad 69 74 68 65 72 20 64 69 72 65 63 74 6c 79 20 6f  ither directly o
264ae 72 20 74 68 72 6f 75 67 68 20 74 72 69 67 67 65  r through trigge
264af 72 73 29 20 6e 65 65 64 73 20 74 6f 20 63 61 6c  rs) needs to cal
264b0 6c 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e  l this.** routin
264b1 65 20 6a 75 73 74 20 62 65 66 6f 72 65 20 74 68  e just before th
264b2 65 20 22 65 78 69 74 22 20 63 6f 64 65 2e 0a 2a  e "exit" code..*
264b3 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
264b4 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74   void sqlite3Aut
264b5 6f 69 6e 63 72 65 6d 65 6e 74 45 6e 64 28 50 61  oincrementEnd(Pa
264b6 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20  rse *pParse){.  
264b7 41 75 74 6f 69 6e 63 49 6e 66 6f 20 2a 70 3b 0a  AutoincInfo *p;.
264b8 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
264b9 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 73 71 6c  se->pVdbe;.  sql
264ba 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
264bb 65 2d 3e 64 62 3b 0a 0a 20 20 61 73 73 65 72 74  e->db;..  assert
264bc 28 20 76 20 29 3b 0a 20 20 66 6f 72 28 70 20 3d  ( v );.  for(p =
264bd 20 70 50 61 72 73 65 2d 3e 70 41 69 6e 63 3b 20   pParse->pAinc; 
264be 70 3b 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 29  p; p = p->pNext)
264bf 7b 0a 20 20 20 20 44 62 20 2a 70 44 62 20 3d 20  {.    Db *pDb = 
264c0 26 64 62 2d 3e 61 44 62 5b 70 2d 3e 69 44 62 5d  &db->aDb[p->iDb]
264c1 3b 0a 20 20 20 20 69 6e 74 20 6a 31 2c 20 6a 32  ;.    int j1, j2
264c2 2c 20 6a 33 2c 20 6a 34 2c 20 6a 35 3b 0a 20 20  , j3, j4, j5;.  
264c3 20 20 69 6e 74 20 69 52 65 63 3b 0a 20 20 20 20    int iRec;.    
264c4 69 6e 74 20 6d 65 6d 49 64 20 3d 20 70 2d 3e 72  int memId = p->r
264c5 65 67 43 74 72 3b 0a 0a 20 20 20 20 69 52 65 63  egCtr;..    iRec
264c6 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
264c7 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
264c8 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62    sqlite3OpenTab
264c9 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 70 2d  le(pParse, 0, p-
264ca 3e 69 44 62 2c 20 70 44 62 2d 3e 70 53 63 68 65  >iDb, pDb->pSche
264cb 6d 61 2d 3e 70 53 65 71 54 61 62 2c 20 4f 50 5f  ma->pSeqTab, OP_
264cc 4f 70 65 6e 57 72 69 74 65 29 3b 0a 20 20 20 20  OpenWrite);.    
264cd 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j1 = sqlite3Vdbe
264ce 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74  AddOp1(v, OP_Not
264cf 4e 75 6c 6c 2c 20 6d 65 6d 49 64 2b 31 29 3b 0a  Null, memId+1);.
264d0 20 20 20 20 6a 32 20 3d 20 73 71 6c 69 74 65 33      j2 = sqlite3
264d1 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50  VdbeAddOp0(v, OP
264d2 5f 52 65 77 69 6e 64 29 3b 0a 20 20 20 20 6a 33  _Rewind);.    j3
264d3 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
264d4 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d  dOp3(v, OP_Colum
264d5 6e 2c 20 30 2c 20 30 2c 20 69 52 65 63 29 3b 0a  n, 0, 0, iRec);.
264d6 20 20 20 20 6a 34 20 3d 20 73 71 6c 69 74 65 33      j4 = sqlite3
264d7 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
264d8 5f 45 71 2c 20 6d 65 6d 49 64 2d 31 2c 20 30 2c  _Eq, memId-1, 0,
264d9 20 69 52 65 63 29 3b 0a 20 20 20 20 73 71 6c 69   iRec);.    sqli
264da 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
264db 20 4f 50 5f 4e 65 78 74 2c 20 30 2c 20 6a 33 29   OP_Next, 0, j3)
264dc 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
264dd 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 32 29  eJumpHere(v, j2)
264de 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
264df 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65  eAddOp2(v, OP_Ne
264e0 77 52 6f 77 69 64 2c 20 30 2c 20 6d 65 6d 49 64  wRowid, 0, memId
264e1 2b 31 29 3b 0a 20 20 20 20 6a 35 20 3d 20 73 71  +1);.    j5 = sq
264e2 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28  lite3VdbeAddOp0(
264e3 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0a 20 20 20  v, OP_Goto);.   
264e4 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
264e5 48 65 72 65 28 76 2c 20 6a 34 29 3b 0a 20 20 20  Here(v, j4);.   
264e6 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
264e7 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20  p2(v, OP_Rowid, 
264e8 30 2c 20 6d 65 6d 49 64 2b 31 29 3b 0a 20 20 20  0, memId+1);.   
264e9 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
264ea 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20  Here(v, j1);.   
264eb 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
264ec 48 65 72 65 28 76 2c 20 6a 35 29 3b 0a 20 20 20  Here(v, j5);.   
264ed 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
264ee 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63  p3(v, OP_MakeRec
264ef 6f 72 64 2c 20 6d 65 6d 49 64 2d 31 2c 20 32 2c  ord, memId-1, 2,
264f0 20 69 52 65 63 29 3b 0a 20 20 20 20 73 71 6c 69   iRec);.    sqli
264f1 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
264f2 20 4f 50 5f 49 6e 73 65 72 74 2c 20 30 2c 20 69   OP_Insert, 0, i
264f3 52 65 63 2c 20 6d 65 6d 49 64 2b 31 29 3b 0a 20  Rec, memId+1);. 
264f4 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
264f5 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47  angeP5(v, OPFLAG
264f6 5f 41 50 50 45 4e 44 29 3b 0a 20 20 20 20 73 71  _APPEND);.    sq
264f7 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28  lite3VdbeAddOp0(
264f8 76 2c 20 4f 50 5f 43 6c 6f 73 65 29 3b 0a 20 20  v, OP_Close);.  
264f9 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
264fa 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
264fb 69 52 65 63 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c  iRec);.  }.}.#el
264fc 73 65 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49  se./*.** If SQLI
264fd 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52  TE_OMIT_AUTOINCR
264fe 45 4d 45 4e 54 20 69 73 20 64 65 66 69 6e 65 64  EMENT is defined
264ff 2c 20 74 68 65 6e 20 74 68 65 20 74 68 72 65 65  , then the three
26500 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 61 62 6f   routines.** abo
26501 76 65 20 61 72 65 20 61 6c 6c 20 6e 6f 2d 6f 70  ve are all no-op
26502 73 0a 2a 2f 0a 23 20 64 65 66 69 6e 65 20 61 75  s.*/.# define au
26503 74 6f 49 6e 63 42 65 67 69 6e 28 41 2c 42 2c 43  toIncBegin(A,B,C
26504 29 20 28 30 29 0a 23 20 64 65 66 69 6e 65 20 61  ) (0).# define a
26505 75 74 6f 49 6e 63 53 74 65 70 28 41 2c 42 2c 43  utoIncStep(A,B,C
26506 29 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ).#endif /* SQLI
26507 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52  TE_OMIT_AUTOINCR
26508 45 4d 45 4e 54 20 2a 2f 0a 0a 0a 2f 2a 20 46 6f  EMENT */.../* Fo
26509 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f  rward declaratio
2650a 6e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  n */.static int 
2650b 78 66 65 72 4f 70 74 69 6d 69 7a 61 74 69 6f 6e  xferOptimization
2650c 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2650d 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72  e,        /* Par
2650e 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ser context */. 
2650f 20 54 61 62 6c 65 20 2a 70 44 65 73 74 2c 20 20   Table *pDest,  
26510 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61         /* The ta
26511 62 6c 65 20 77 65 20 61 72 65 20 69 6e 73 65 72  ble we are inser
26512 74 69 6e 67 20 69 6e 74 6f 20 2a 2f 0a 20 20 53  ting into */.  S
26513 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20  elect *pSelect, 
26514 20 20 20 20 20 2f 2a 20 41 20 53 45 4c 45 43 54       /* A SELECT
26515 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 75 73   statement to us
26516 65 20 61 73 20 74 68 65 20 64 61 74 61 20 73 6f  e as the data so
26517 75 72 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e  urce */.  int on
26518 45 72 72 6f 72 2c 20 20 20 20 20 20 20 20 20 20  Error,          
26519 2f 2a 20 48 6f 77 20 74 6f 20 68 61 6e 64 6c 65  /* How to handle
2651a 20 63 6f 6e 73 74 72 61 69 6e 74 20 65 72 72 6f   constraint erro
2651b 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 44  rs */.  int iDbD
2651c 65 73 74 20 20 20 20 20 20 20 20 20 20 20 2f 2a  est           /*
2651d 20 54 68 65 20 64 61 74 61 62 61 73 65 20 6f 66   The database of
2651e 20 70 44 65 73 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a   pDest */.);../*
2651f 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
26520 20 69 73 20 63 61 6c 6c 20 74 6f 20 68 61 6e 64   is call to hand
26521 6c 65 20 53 51 4c 20 6f 66 20 74 68 65 20 66 6f  le SQL of the fo
26522 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 73 3a 0a 2a  llowing forms:.*
26523 2a 0a 2a 2a 20 20 20 20 69 6e 73 65 72 74 20 69  *.**    insert i
26524 6e 74 6f 20 54 41 42 4c 45 20 28 49 44 4c 49 53  nto TABLE (IDLIS
26525 54 29 20 76 61 6c 75 65 73 28 45 58 50 52 4c 49  T) values(EXPRLI
26526 53 54 29 0a 2a 2a 20 20 20 20 69 6e 73 65 72 74  ST).**    insert
26527 20 69 6e 74 6f 20 54 41 42 4c 45 20 28 49 44 4c   into TABLE (IDL
26528 49 53 54 29 20 73 65 6c 65 63 74 0a 2a 2a 0a 2a  IST) select.**.*
26529 2a 20 54 68 65 20 49 44 4c 49 53 54 20 66 6f 6c  * The IDLIST fol
2652a 6c 6f 77 69 6e 67 20 74 68 65 20 74 61 62 6c 65  lowing the table
2652b 20 6e 61 6d 65 20 69 73 20 61 6c 77 61 79 73 20   name is always 
2652c 6f 70 74 69 6f 6e 61 6c 2e 20 20 49 66 20 6f 6d  optional.  If om
2652d 69 74 74 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 61  itted,.** then a
2652e 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 63 6f 6c   list of all col
2652f 75 6d 6e 73 20 66 6f 72 20 74 68 65 20 74 61 62  umns for the tab
26530 6c 65 20 69 73 20 73 75 62 73 74 69 74 75 74 65  le is substitute
26531 64 2e 20 20 54 68 65 20 49 44 4c 49 53 54 0a 2a  d.  The IDLIST.*
26532 2a 20 61 70 70 65 61 72 73 20 69 6e 20 74 68 65  * appears in the
26533 20 70 43 6f 6c 75 6d 6e 20 70 61 72 61 6d 65 74   pColumn paramet
26534 65 72 2e 20 20 70 43 6f 6c 75 6d 6e 20 69 73 20  er.  pColumn is 
26535 4e 55 4c 4c 20 69 66 20 49 44 4c 49 53 54 20 69  NULL if IDLIST i
26536 73 20 6f 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a  s omitted..**.**
26537 20 54 68 65 20 70 4c 69 73 74 20 70 61 72 61 6d   The pList param
26538 65 74 65 72 20 68 6f 6c 64 73 20 45 58 50 52 4c  eter holds EXPRL
26539 49 53 54 20 69 6e 20 74 68 65 20 66 69 72 73 74  IST in the first
2653a 20 66 6f 72 6d 20 6f 66 20 74 68 65 20 49 4e 53   form of the INS
2653b 45 52 54 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  ERT.** statement
2653c 20 61 62 6f 76 65 2c 20 61 6e 64 20 70 53 65 6c   above, and pSel
2653d 65 63 74 20 69 73 20 4e 55 4c 4c 2e 20 20 46 6f  ect is NULL.  Fo
2653e 72 20 74 68 65 20 73 65 63 6f 6e 64 20 66 6f 72  r the second for
2653f 6d 2c 20 70 4c 69 73 74 20 69 73 0a 2a 2a 20 4e  m, pList is.** N
26540 55 4c 4c 20 61 6e 64 20 70 53 65 6c 65 63 74 20  ULL and pSelect 
26541 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
26542 74 68 65 20 73 65 6c 65 63 74 20 73 74 61 74 65  the select state
26543 6d 65 6e 74 20 75 73 65 64 20 74 6f 20 67 65 6e  ment used to gen
26544 65 72 61 74 65 0a 2a 2a 20 64 61 74 61 20 66 6f  erate.** data fo
26545 72 20 74 68 65 20 69 6e 73 65 72 74 2e 0a 2a 2a  r the insert..**
26546 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 67 65 6e  .** The code gen
26547 65 72 61 74 65 64 20 66 6f 6c 6c 6f 77 73 20 6f  erated follows o
26548 6e 65 20 6f 66 20 66 6f 75 72 20 74 65 6d 70 6c  ne of four templ
26549 61 74 65 73 2e 20 20 46 6f 72 20 61 20 73 69 6d  ates.  For a sim
2654a 70 6c 65 0a 2a 2a 20 73 65 6c 65 63 74 20 77 69  ple.** select wi
2654b 74 68 20 64 61 74 61 20 63 6f 6d 69 6e 67 20 66  th data coming f
2654c 72 6f 6d 20 61 20 56 41 4c 55 45 53 20 63 6c 61  rom a VALUES cla
2654d 75 73 65 2c 20 74 68 65 20 63 6f 64 65 20 65 78  use, the code ex
2654e 65 63 75 74 65 73 0a 2a 2a 20 6f 6e 63 65 20 73  ecutes.** once s
2654f 74 72 61 69 67 68 74 20 64 6f 77 6e 20 74 68 72  traight down thr
26550 6f 75 67 68 2e 20 20 50 73 65 75 64 6f 2d 63 6f  ough.  Pseudo-co
26551 64 65 20 66 6f 6c 6c 6f 77 73 20 28 77 65 20 63  de follows (we c
26552 61 6c 6c 20 74 68 69 73 0a 2a 2a 20 74 68 65 20  all this.** the 
26553 22 31 73 74 20 74 65 6d 70 6c 61 74 65 22 29 3a  "1st template"):
26554 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 6f  .**.**         o
26555 70 65 6e 20 77 72 69 74 65 20 63 75 72 73 6f 72  pen write cursor
26556 20 74 6f 20 3c 74 61 62 6c 65 3e 20 61 6e 64 20   to <table> and 
26557 69 74 73 20 69 6e 64 69 63 65 73 0a 2a 2a 20 20  its indices.**  
26558 20 20 20 20 20 20 20 70 75 74 73 20 56 41 4c 55         puts VALU
26559 45 53 20 63 6c 61 75 73 65 20 65 78 70 72 65 73  ES clause expres
2655a 73 69 6f 6e 73 20 6f 6e 74 6f 20 74 68 65 20 73  sions onto the s
2655b 74 61 63 6b 0a 2a 2a 20 20 20 20 20 20 20 20 20  tack.**         
2655c 77 72 69 74 65 20 74 68 65 20 72 65 73 75 6c 74  write the result
2655d 69 6e 67 20 72 65 63 6f 72 64 20 69 6e 74 6f 20  ing record into 
2655e 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20 20 20 20 20  <table>.**      
2655f 20 20 20 63 6c 65 61 6e 75 70 0a 2a 2a 0a 2a 2a     cleanup.**.**
26560 20 54 68 65 20 74 68 72 65 65 20 72 65 6d 61 69   The three remai
26561 6e 69 6e 67 20 74 65 6d 70 6c 61 74 65 73 20 61  ning templates a
26562 73 73 75 6d 65 20 74 68 65 20 73 74 61 74 65 6d  ssume the statem
26563 65 6e 74 20 69 73 20 6f 66 20 74 68 65 20 66 6f  ent is of the fo
26564 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 49 4e 53 45 52  rm.**.**   INSER
26565 54 20 49 4e 54 4f 20 3c 74 61 62 6c 65 3e 20 53  T INTO <table> S
26566 45 4c 45 43 54 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20  ELECT ....**.** 
26567 49 66 20 74 68 65 20 53 45 4c 45 43 54 20 63 6c  If the SELECT cl
26568 61 75 73 65 20 69 73 20 6f 66 20 74 68 65 20 72  ause is of the r
26569 65 73 74 72 69 63 74 65 64 20 66 6f 72 6d 20 22  estricted form "
2656a 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 3c 74  SELECT * FROM <t
2656b 61 62 6c 65 32 3e 22 20 2d 0a 2a 2a 20 69 6e 20  able2>" -.** in 
2656c 6f 74 68 65 72 20 77 6f 72 64 73 20 69 66 20 74  other words if t
2656d 68 65 20 53 45 4c 45 43 54 20 70 75 6c 6c 73 20  he SELECT pulls 
2656e 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 66 72 6f 6d  all columns from
2656f 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 0a   a single table.
26570 2a 2a 20 61 6e 64 20 74 68 65 72 65 20 69 73 20  ** and there is 
26571 6e 6f 20 57 48 45 52 45 20 6f 72 20 4c 49 4d 49  no WHERE or LIMI
26572 54 20 6f 72 20 47 52 4f 55 50 20 42 59 20 6f 72  T or GROUP BY or
26573 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
26574 73 2c 20 61 6e 64 0a 2a 2a 20 69 66 20 3c 74 61  s, and.** if <ta
26575 62 6c 65 32 3e 20 61 6e 64 20 3c 74 61 62 6c 65  ble2> and <table
26576 31 3e 20 61 72 65 20 64 69 73 74 69 6e 63 74 20  1> are distinct 
26577 74 61 62 6c 65 73 20 62 75 74 20 68 61 76 65 20  tables but have 
26578 69 64 65 6e 74 69 63 61 6c 0a 2a 2a 20 73 63 68  identical.** sch
26579 65 6d 61 73 2c 20 69 6e 63 6c 75 64 69 6e 67 20  emas, including 
2657a 61 6c 6c 20 74 68 65 20 73 61 6d 65 20 69 6e 64  all the same ind
2657b 69 63 65 73 2c 20 74 68 65 6e 20 61 20 73 70 65  ices, then a spe
2657c 63 69 61 6c 20 6f 70 74 69 6d 69 7a 61 74 69 6f  cial optimizatio
2657d 6e 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65 64 20  n.** is invoked 
2657e 74 68 61 74 20 63 6f 70 69 65 73 20 72 61 77 20  that copies raw 
2657f 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 3c 74 61  records from <ta
26580 62 6c 65 32 3e 20 6f 76 65 72 20 74 6f 20 3c 74  ble2> over to <t
26581 61 62 6c 65 31 3e 2e 0a 2a 2a 20 53 65 65 20 74  able1>..** See t
26582 68 65 20 78 66 65 72 4f 70 74 69 6d 69 7a 61 74  he xferOptimizat
26583 69 6f 6e 28 29 20 66 75 6e 63 74 69 6f 6e 20 66  ion() function f
26584 6f 72 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  or the implement
26585 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 0a 2a 2a  ation of this.**
26586 20 74 65 6d 70 6c 61 74 65 2e 20 20 54 68 69 73   template.  This
26587 20 69 73 20 74 68 65 20 32 6e 64 20 74 65 6d 70   is the 2nd temp
26588 6c 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  late..**.**     
26589 20 20 20 20 6f 70 65 6e 20 61 20 77 72 69 74 65      open a write
2658a 20 63 75 72 73 6f 72 20 74 6f 20 3c 74 61 62 6c   cursor to <tabl
2658b 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20 20 6f 70  e>.**         op
2658c 65 6e 20 72 65 61 64 20 63 75 72 73 6f 72 20 6f  en read cursor o
2658d 6e 20 3c 74 61 62 6c 65 32 3e 0a 2a 2a 20 20 20  n <table2>.**   
2658e 20 20 20 20 20 20 74 72 61 6e 73 66 65 72 20 61        transfer a
2658f 6c 6c 20 72 65 63 6f 72 64 73 20 69 6e 20 3c 74  ll records in <t
26590 61 62 6c 65 32 3e 20 6f 76 65 72 20 74 6f 20 3c  able2> over to <
26591 74 61 62 6c 65 3e 0a 2a 2a 20 20 20 20 20 20 20  table>.**       
26592 20 20 63 6c 6f 73 65 20 63 75 72 73 6f 72 73 0a    close cursors.
26593 2a 2a 20 20 20 20 20 20 20 20 20 66 6f 72 65 61  **         forea
26594 63 68 20 69 6e 64 65 78 20 6f 6e 20 3c 74 61 62  ch index on <tab
26595 6c 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  le>.**          
26596 20 6f 70 65 6e 20 61 20 77 72 69 74 65 20 63 75   open a write cu
26597 72 73 6f 72 20 6f 6e 20 74 68 65 20 3c 74 61 62  rsor on the <tab
26598 6c 65 3e 20 69 6e 64 65 78 0a 2a 2a 20 20 20 20  le> index.**    
26599 20 20 20 20 20 20 20 6f 70 65 6e 20 61 20 72 65         open a re
2659a 61 64 20 63 75 72 73 6f 72 20 6f 6e 20 74 68 65  ad cursor on the
2659b 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 3c   corresponding <
2659c 74 61 62 6c 65 32 3e 20 69 6e 64 65 78 0a 2a 2a  table2> index.**
2659d 20 20 20 20 20 20 20 20 20 20 20 74 72 61 6e 73             trans
2659e 66 65 72 20 61 6c 6c 20 72 65 63 6f 72 64 73 20  fer all records 
2659f 66 72 6f 6d 20 74 68 65 20 72 65 61 64 20 74 6f  from the read to
265a0 20 74 68 65 20 77 72 69 74 65 20 63 75 72 73 6f   the write curso
265a1 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  rs.**           
265a2 63 6c 6f 73 65 20 63 75 72 73 6f 72 73 0a 2a 2a  close cursors.**
265a3 20 20 20 20 20 20 20 20 20 65 6e 64 20 66 6f 72           end for
265a4 65 61 63 68 0a 2a 2a 0a 2a 2a 20 54 68 65 20 33  each.**.** The 3
265a5 72 64 20 74 65 6d 70 6c 61 74 65 20 69 73 20 66  rd template is f
265a6 6f 72 20 77 68 65 6e 20 74 68 65 20 73 65 63 6f  or when the seco
265a7 6e 64 20 74 65 6d 70 6c 61 74 65 20 64 6f 65 73  nd template does
265a8 20 6e 6f 74 20 61 70 70 6c 79 0a 2a 2a 20 61 6e   not apply.** an
265a9 64 20 74 68 65 20 53 45 4c 45 43 54 20 63 6c 61  d the SELECT cla
265aa 75 73 65 20 64 6f 65 73 20 6e 6f 74 20 72 65 61  use does not rea
265ab 64 20 66 72 6f 6d 20 3c 74 61 62 6c 65 3e 20 61  d from <table> a
265ac 74 20 61 6e 79 20 74 69 6d 65 2e 0a 2a 2a 20 54  t any time..** T
265ad 68 65 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64  he generated cod
265ae 65 20 66 6f 6c 6c 6f 77 73 20 74 68 69 73 20 74  e follows this t
265af 65 6d 70 6c 61 74 65 3a 0a 2a 2a 0a 2a 2a 20 20  emplate:.**.**  
265b0 20 20 20 20 20 20 20 45 4f 46 20 3c 2d 20 30 0a         EOF <- 0.
265b1 2a 2a 20 20 20 20 20 20 20 20 20 58 20 3c 2d 20  **         X <- 
265b2 41 0a 2a 2a 20 20 20 20 20 20 20 20 20 67 6f 74  A.**         got
265b3 6f 20 42 0a 2a 2a 20 20 20 20 20 20 41 3a 20 73  o B.**      A: s
265b4 65 74 75 70 20 66 6f 72 20 74 68 65 20 53 45 4c  etup for the SEL
265b5 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 6c  ECT.**         l
265b6 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 72 6f 77  oop over the row
265b7 73 20 69 6e 20 74 68 65 20 53 45 4c 45 43 54 0a  s in the SELECT.
265b8 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6c 6f 61  **           loa
265b9 64 20 76 61 6c 75 65 73 20 69 6e 74 6f 20 72 65  d values into re
265ba 67 69 73 74 65 72 73 20 52 2e 2e 52 2b 6e 0a 2a  gisters R..R+n.*
265bb 2a 20 20 20 20 20 20 20 20 20 20 20 79 69 65 6c  *           yiel
265bc 64 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 20 65  d X.**         e
265bd 6e 64 20 6c 6f 6f 70 0a 2a 2a 20 20 20 20 20 20  nd loop.**      
265be 20 20 20 63 6c 65 61 6e 75 70 20 61 66 74 65 72     cleanup after
265bf 20 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20   the SELECT.**  
265c0 20 20 20 20 20 20 20 45 4f 46 20 3c 2d 20 31 0a         EOF <- 1.
265c1 2a 2a 20 20 20 20 20 20 20 20 20 79 69 65 6c 64  **         yield
265c2 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 20 67 6f   X.**         go
265c3 74 6f 20 41 0a 2a 2a 20 20 20 20 20 20 42 3a 20  to A.**      B: 
265c4 6f 70 65 6e 20 77 72 69 74 65 20 63 75 72 73 6f  open write curso
265c5 72 20 74 6f 20 3c 74 61 62 6c 65 3e 20 61 6e 64  r to <table> and
265c6 20 69 74 73 20 69 6e 64 69 63 65 73 0a 2a 2a 20   its indices.** 
265c7 20 20 20 20 20 43 3a 20 79 69 65 6c 64 20 58 0a       C: yield X.
265c8 2a 2a 20 20 20 20 20 20 20 20 20 69 66 20 45 4f  **         if EO
265c9 46 20 67 6f 74 6f 20 44 0a 2a 2a 20 20 20 20 20  F goto D.**     
265ca 20 20 20 20 69 6e 73 65 72 74 20 74 68 65 20 73      insert the s
265cb 65 6c 65 63 74 20 72 65 73 75 6c 74 20 69 6e 74  elect result int
265cc 6f 20 3c 74 61 62 6c 65 3e 20 66 72 6f 6d 20 52  o <table> from R
265cd 2e 2e 52 2b 6e 0a 2a 2a 20 20 20 20 20 20 20 20  ..R+n.**        
265ce 20 67 6f 74 6f 20 43 0a 2a 2a 20 20 20 20 20 20   goto C.**      
265cf 44 3a 20 63 6c 65 61 6e 75 70 0a 2a 2a 0a 2a 2a  D: cleanup.**.**
265d0 20 54 68 65 20 34 74 68 20 74 65 6d 70 6c 61 74   The 4th templat
265d1 65 20 69 73 20 75 73 65 64 20 69 66 20 74 68 65  e is used if the
265d2 20 69 6e 73 65 72 74 20 73 74 61 74 65 6d 65 6e   insert statemen
265d3 74 20 74 61 6b 65 73 20 69 74 73 0a 2a 2a 20 76  t takes its.** v
265d4 61 6c 75 65 73 20 66 72 6f 6d 20 61 20 53 45 4c  alues from a SEL
265d5 45 43 54 20 62 75 74 20 74 68 65 20 64 61 74 61  ECT but the data
265d6 20 69 73 20 62 65 69 6e 67 20 69 6e 73 65 72 74   is being insert
265d7 65 64 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 0a  ed into a table.
265d8 2a 2a 20 74 68 61 74 20 69 73 20 61 6c 73 6f 20  ** that is also 
265d9 72 65 61 64 20 61 73 20 70 61 72 74 20 6f 66 20  read as part of 
265da 74 68 65 20 53 45 4c 45 43 54 2e 20 20 49 6e 20  the SELECT.  In 
265db 74 68 65 20 74 68 69 72 64 20 66 6f 72 6d 2c 0a  the third form,.
265dc 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 75 73  ** we have to us
265dd 65 20 61 20 69 6e 74 65 72 6d 65 64 69 61 74 65  e a intermediate
265de 20 74 61 62 6c 65 20 74 6f 20 73 74 6f 72 65 20   table to store 
265df 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 0a 2a  the results of.*
265e0 2a 20 74 68 65 20 73 65 6c 65 63 74 2e 20 20 54  * the select.  T
265e1 68 65 20 74 65 6d 70 6c 61 74 65 20 69 73 20 6c  he template is l
265e2 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20  ike this:.**.** 
265e3 20 20 20 20 20 20 20 20 45 4f 46 20 3c 2d 20 30          EOF <- 0
265e4 0a 2a 2a 20 20 20 20 20 20 20 20 20 58 20 3c 2d  .**         X <-
265e5 20 41 0a 2a 2a 20 20 20 20 20 20 20 20 20 67 6f   A.**         go
265e6 74 6f 20 42 0a 2a 2a 20 20 20 20 20 20 41 3a 20  to B.**      A: 
265e7 73 65 74 75 70 20 66 6f 72 20 74 68 65 20 53 45  setup for the SE
265e8 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20  LECT.**         
265e9 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 74 61  loop over the ta
265ea 62 6c 65 73 20 69 6e 20 74 68 65 20 53 45 4c 45  bles in the SELE
265eb 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  CT.**           
265ec 6c 6f 61 64 20 76 61 6c 75 65 20 69 6e 74 6f 20  load value into 
265ed 72 65 67 69 73 74 65 72 20 52 2e 2e 52 2b 6e 0a  register R..R+n.
265ee 2a 2a 20 20 20 20 20 20 20 20 20 20 20 79 69 65  **           yie
265ef 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 20  ld X.**         
265f0 65 6e 64 20 6c 6f 6f 70 0a 2a 2a 20 20 20 20 20  end loop.**     
265f1 20 20 20 20 63 6c 65 61 6e 75 70 20 61 66 74 65      cleanup afte
265f2 72 20 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20  r the SELECT.** 
265f3 20 20 20 20 20 20 20 20 45 4f 46 20 3c 2d 20 31          EOF <- 1
265f4 0a 2a 2a 20 20 20 20 20 20 20 20 20 79 69 65 6c  .**         yiel
265f5 64 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 20 68  d X.**         h
265f6 61 6c 74 2d 65 72 72 6f 72 0a 2a 2a 20 20 20 20  alt-error.**    
265f7 20 20 42 3a 20 6f 70 65 6e 20 74 65 6d 70 20 74    B: open temp t
265f8 61 62 6c 65 0a 2a 2a 20 20 20 20 20 20 4c 3a 20  able.**      L: 
265f9 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20  yield X.**      
265fa 20 20 20 69 66 20 45 4f 46 20 67 6f 74 6f 20 4d     if EOF goto M
265fb 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 73 65  .**         inse
265fc 72 74 20 72 6f 77 20 66 72 6f 6d 20 52 2e 2e 52  rt row from R..R
265fd 2b 6e 20 69 6e 74 6f 20 74 65 6d 70 20 74 61 62  +n into temp tab
265fe 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 67 6f  le.**         go
265ff 74 6f 20 4c 0a 2a 2a 20 20 20 20 20 20 4d 3a 20  to L.**      M: 
26600 6f 70 65 6e 20 77 72 69 74 65 20 63 75 72 73 6f  open write curso
26601 72 20 74 6f 20 3c 74 61 62 6c 65 3e 20 61 6e 64  r to <table> and
26602 20 69 74 73 20 69 6e 64 69 63 65 73 0a 2a 2a 20   its indices.** 
26603 20 20 20 20 20 20 20 20 72 65 77 69 6e 64 20 74          rewind t
26604 65 6d 70 20 74 61 62 6c 65 0a 2a 2a 20 20 20 20  emp table.**    
26605 20 20 43 3a 20 6c 6f 6f 70 20 6f 76 65 72 20 72    C: loop over r
26606 6f 77 73 20 6f 66 20 69 6e 74 65 72 6d 65 64 69  ows of intermedi
26607 61 74 65 20 74 61 62 6c 65 0a 2a 2a 20 20 20 20  ate table.**    
26608 20 20 20 20 20 20 20 74 72 61 6e 73 66 65 72 20         transfer 
26609 76 61 6c 75 65 73 20 66 6f 72 6d 20 69 6e 74 65  values form inte
2660a 72 6d 65 64 69 61 74 65 20 74 61 62 6c 65 20 69  rmediate table i
2660b 6e 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20  nto <table>.**  
2660c 20 20 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70 0a         end loop.
2660d 2a 2a 20 20 20 20 20 20 44 3a 20 63 6c 65 61 6e  **      D: clean
2660e 75 70 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  up.*/.SQLITE_PRI
2660f 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
26610 33 49 6e 73 65 72 74 28 0a 20 20 50 61 72 73 65  3Insert(.  Parse
26611 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
26612 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65   /* Parser conte
26613 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20  xt */.  SrcList 
26614 2a 70 54 61 62 4c 69 73 74 2c 20 20 20 20 2f 2a  *pTabList,    /*
26615 20 4e 61 6d 65 20 6f 66 20 74 61 62 6c 65 20 69   Name of table i
26616 6e 74 6f 20 77 68 69 63 68 20 77 65 20 61 72 65  nto which we are
26617 20 69 6e 73 65 72 74 69 6e 67 20 2a 2f 0a 20 20   inserting */.  
26618 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c  ExprList *pList,
26619 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66        /* List of
2661a 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 69 6e   values to be in
2661b 73 65 72 74 65 64 20 2a 2f 0a 20 20 53 65 6c 65  serted */.  Sele
2661c 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 20  ct *pSelect,    
2661d 20 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74    /* A SELECT st
2661e 61 74 65 6d 65 6e 74 20 74 6f 20 75 73 65 20 61  atement to use a
2661f 73 20 74 68 65 20 64 61 74 61 20 73 6f 75 72 63  s the data sourc
26620 65 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70  e */.  IdList *p
26621 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 2f 2a 20  Column,      /* 
26622 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 63 6f 72  Column names cor
26623 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 49 44  responding to ID
26624 4c 49 53 54 2e 20 2a 2f 0a 20 20 69 6e 74 20 6f  LIST. */.  int o
26625 6e 45 72 72 6f 72 20 20 20 20 20 20 20 20 20 20  nError          
26626 20 2f 2a 20 48 6f 77 20 74 6f 20 68 61 6e 64 6c   /* How to handl
26627 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 65 72 72  e constraint err
26628 6f 72 73 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69  ors */.){.  sqli
26629 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20  te3 *db;        
2662a 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 61    /* The main da
2662b 74 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65  tabase structure
2662c 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
2662d 62 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  b;          /* T
2662e 68 65 20 74 61 62 6c 65 20 74 6f 20 69 6e 73 65  he table to inse
2662f 72 74 20 69 6e 74 6f 2e 20 20 61 6b 61 20 54 41  rt into.  aka TA
26630 42 4c 45 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  BLE */.  char *z
26631 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f  Tab;           /
26632 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  * Name of the ta
26633 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77  ble into which w
26634 65 20 61 72 65 20 69 6e 73 65 72 74 69 6e 67 20  e are inserting 
26635 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
26636 2a 7a 44 62 3b 20 20 20 20 20 20 2f 2a 20 4e 61  *zDb;      /* Na
26637 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  me of the databa
26638 73 65 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20  se holding this 
26639 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69  table */.  int i
2663a 2c 20 6a 2c 20 69 64 78 3b 20 20 20 20 20 20 20  , j, idx;       
2663b 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
2663c 73 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20  s */.  Vdbe *v; 
2663d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2663e 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 69 6e  Generate code in
2663f 74 6f 20 74 68 69 73 20 76 69 72 74 75 61 6c 20  to this virtual 
26640 6d 61 63 68 69 6e 65 20 2a 2f 0a 20 20 49 6e 64  machine */.  Ind
26641 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20  ex *pIdx;       
26642 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e     /* For loopin
26643 67 20 6f 76 65 72 20 69 6e 64 69 63 65 73 20 6f  g over indices o
26644 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20  f the table */. 
26645 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20   int nColumn;   
26646 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
26647 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74   of columns in t
26648 68 65 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74  he data */.  int
26649 20 6e 48 69 64 64 65 6e 20 3d 20 30 3b 20 20 20   nHidden = 0;   
2664a 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
2664b 68 69 64 64 65 6e 20 63 6f 6c 75 6d 6e 73 20 69  hidden columns i
2664c 66 20 54 41 42 4c 45 20 69 73 20 76 69 72 74 75  f TABLE is virtu
2664d 61 6c 20 2a 2f 0a 20 20 69 6e 74 20 62 61 73 65  al */.  int base
2664e 43 75 72 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a  Cur = 0;      /*
2664f 20 56 44 42 45 20 43 75 72 73 6f 72 20 6e 75 6d   VDBE Cursor num
26650 62 65 72 20 66 6f 72 20 70 54 61 62 20 2a 2f 0a  ber for pTab */.
26651 20 20 69 6e 74 20 6b 65 79 43 6f 6c 75 6d 6e 20    int keyColumn 
26652 3d 20 2d 31 3b 20 20 20 2f 2a 20 43 6f 6c 75 6d  = -1;   /* Colum
26653 6e 20 74 68 61 74 20 69 73 20 74 68 65 20 49 4e  n that is the IN
26654 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
26655 59 20 2a 2f 0a 20 20 69 6e 74 20 65 6e 64 4f 66  Y */.  int endOf
26656 4c 6f 6f 70 3b 20 20 20 20 20 20 20 20 2f 2a 20  Loop;        /* 
26657 4c 61 62 65 6c 20 66 6f 72 20 74 68 65 20 65 6e  Label for the en
26658 64 20 6f 66 20 74 68 65 20 69 6e 73 65 72 74 69  d of the inserti
26659 6f 6e 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74  on loop */.  int
2665a 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20 3d 20   useTempTable = 
2665b 30 3b 20 2f 2a 20 53 74 6f 72 65 20 53 45 4c 45  0; /* Store SELE
2665c 43 54 20 72 65 73 75 6c 74 73 20 69 6e 20 69 6e  CT results in in
2665d 74 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c 65  termediate table
2665e 20 2a 2f 0a 20 20 69 6e 74 20 73 72 63 54 61 62   */.  int srcTab
2665f 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 44   = 0;       /* D
26660 61 74 61 20 63 6f 6d 65 73 20 66 72 6f 6d 20 74  ata comes from t
26661 68 69 73 20 74 65 6d 70 6f 72 61 72 79 20 63 75  his temporary cu
26662 72 73 6f 72 20 69 66 20 3e 3d 30 20 2a 2f 0a 20  rsor if >=0 */. 
26663 20 69 6e 74 20 61 64 64 72 49 6e 73 54 6f 70 20   int addrInsTop 
26664 3d 20 30 3b 20 20 20 2f 2a 20 4a 75 6d 70 20 74  = 0;   /* Jump t
26665 6f 20 6c 61 62 65 6c 20 22 44 22 20 2a 2f 0a 20  o label "D" */. 
26666 20 69 6e 74 20 61 64 64 72 43 6f 6e 74 20 3d 20   int addrCont = 
26667 30 3b 20 20 20 20 20 2f 2a 20 54 6f 70 20 6f 66  0;     /* Top of
26668 20 69 6e 73 65 72 74 20 6c 6f 6f 70 2e 20 4c 61   insert loop. La
26669 62 65 6c 20 22 43 22 20 69 6e 20 74 65 6d 70 6c  bel "C" in templ
2666a 61 74 65 73 20 33 20 61 6e 64 20 34 20 2a 2f 0a  ates 3 and 4 */.
2666b 20 20 69 6e 74 20 61 64 64 72 53 65 6c 65 63 74    int addrSelect
2666c 20 3d 20 30 3b 20 20 20 2f 2a 20 41 64 64 72 65   = 0;   /* Addre
2666d 73 73 20 6f 66 20 63 6f 72 6f 75 74 69 6e 65 20  ss of coroutine 
2666e 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20  that implements 
2666f 74 68 65 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20  the SELECT */.  
26670 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b  SelectDest dest;
26671 20 20 20 20 20 20 2f 2a 20 44 65 73 74 69 6e 61        /* Destina
26672 74 69 6f 6e 20 66 6f 72 20 53 45 4c 45 43 54 20  tion for SELECT 
26673 6f 6e 20 72 68 73 20 6f 66 20 49 4e 53 45 52 54  on rhs of INSERT
26674 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20   */.  int iDb;  
26675 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
26676 6e 64 65 78 20 6f 66 20 64 61 74 61 62 61 73 65  ndex of database
26677 20 68 6f 6c 64 69 6e 67 20 54 41 42 4c 45 20 2a   holding TABLE *
26678 2f 0a 20 20 44 62 20 2a 70 44 62 3b 20 20 20 20  /.  Db *pDb;    
26679 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2667a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69   database contai
2667b 6e 69 6e 67 20 74 61 62 6c 65 20 62 65 69 6e 67  ning table being
2667c 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 2a   inserted into *
2667d 2f 0a 20 20 69 6e 74 20 61 70 70 65 6e 64 46 6c  /.  int appendFl
2667e 61 67 20 3d 20 30 3b 20 20 20 2f 2a 20 54 72 75  ag = 0;   /* Tru
2667f 65 20 69 66 20 74 68 65 20 69 6e 73 65 72 74 20  e if the insert 
26680 69 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20  is likely to be 
26681 61 6e 20 61 70 70 65 6e 64 20 2a 2f 0a 0a 20 20  an append */..  
26682 2f 2a 20 52 65 67 69 73 74 65 72 20 61 6c 6c 6f  /* Register allo
26683 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74  cations */.  int
26684 20 72 65 67 46 72 6f 6d 53 65 6c 65 63 74 20 3d   regFromSelect =
26685 20 30 3b 2f 2a 20 42 61 73 65 20 72 65 67 69 73   0;/* Base regis
26686 74 65 72 20 66 6f 72 20 64 61 74 61 20 63 6f 6d  ter for data com
26687 69 6e 67 20 66 72 6f 6d 20 53 45 4c 45 43 54 20  ing from SELECT 
26688 2a 2f 0a 20 20 69 6e 74 20 72 65 67 41 75 74 6f  */.  int regAuto
26689 69 6e 63 20 3d 20 30 3b 20 20 20 2f 2a 20 52 65  inc = 0;   /* Re
2668a 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 74  gister holding t
2668b 68 65 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54  he AUTOINCREMENT
2668c 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e   counter */.  in
2668d 74 20 72 65 67 52 6f 77 43 6f 75 6e 74 20 3d 20  t regRowCount = 
2668e 30 3b 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65  0;  /* Memory ce
2668f 6c 6c 20 75 73 65 64 20 66 6f 72 20 74 68 65 20  ll used for the 
26690 72 6f 77 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  row counter */. 
26691 20 69 6e 74 20 72 65 67 49 6e 73 3b 20 20 20 20   int regIns;    
26692 20 20 20 20 20 20 20 2f 2a 20 42 6c 6f 63 6b 20         /* Block 
26693 6f 66 20 72 65 67 73 20 68 6f 6c 64 69 6e 67 20  of regs holding 
26694 72 6f 77 69 64 2b 64 61 74 61 20 62 65 69 6e 67  rowid+data being
26695 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 69   inserted */.  i
26696 6e 74 20 72 65 67 52 6f 77 69 64 3b 20 20 20 20  nt regRowid;    
26697 20 20 20 20 20 2f 2a 20 72 65 67 69 73 74 65 72       /* register
26698 73 20 68 6f 6c 64 69 6e 67 20 69 6e 73 65 72 74  s holding insert
26699 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69 6e 74 20   rowid */.  int 
2669a 72 65 67 44 61 74 61 3b 20 20 20 20 20 20 20 20  regData;        
2669b 20 20 2f 2a 20 72 65 67 69 73 74 65 72 20 68 6f    /* register ho
2669c 6c 64 69 6e 67 20 66 69 72 73 74 20 63 6f 6c 75  lding first colu
2669d 6d 6e 20 74 6f 20 69 6e 73 65 72 74 20 2a 2f 0a  mn to insert */.
2669e 20 20 69 6e 74 20 72 65 67 52 65 63 6f 72 64 3b    int regRecord;
2669f 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 6c 64 73          /* Holds
266a0 20 74 68 65 20 61 73 73 65 6d 62 6c 69 65 64 20   the assemblied 
266a1 72 6f 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  row record */.  
266a2 69 6e 74 20 72 65 67 45 6f 66 20 3d 20 30 3b 20  int regEof = 0; 
266a3 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
266a4 72 20 72 65 63 6f 72 64 69 6e 67 20 65 6e 64 20  r recording end 
266a5 6f 66 20 53 45 4c 45 43 54 20 64 61 74 61 20 2a  of SELECT data *
266a6 2f 0a 20 20 69 6e 74 20 2a 61 52 65 67 49 64 78  /.  int *aRegIdx
266a7 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4f 6e 65   = 0;     /* One
266a8 20 72 65 67 69 73 74 65 72 20 61 6c 6c 6f 63 61   register alloca
266a9 74 65 64 20 74 6f 20 65 61 63 68 20 69 6e 64 65  ted to each inde
266aa 78 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  x */..#ifndef SQ
266ab 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
266ac 52 0a 20 20 69 6e 74 20 69 73 56 69 65 77 3b 20  R.  int isView; 
266ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
266ae 2f 2a 20 54 72 75 65 20 69 66 20 61 74 74 65 6d  /* True if attem
266af 70 74 69 6e 67 20 74 6f 20 69 6e 73 65 72 74 20  pting to insert 
266b0 69 6e 74 6f 20 61 20 76 69 65 77 20 2a 2f 0a 20  into a view */. 
266b1 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67   Trigger *pTrigg
266b2 65 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  er;          /* 
266b3 4c 69 73 74 20 6f 66 20 74 72 69 67 67 65 72 73  List of triggers
266b4 20 6f 6e 20 70 54 61 62 2c 20 69 66 20 72 65 71   on pTab, if req
266b5 75 69 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 74  uired */.  int t
266b6 6d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20  mask;           
266b7 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f         /* Mask o
266b8 66 20 74 72 69 67 67 65 72 20 74 69 6d 65 73 20  f trigger times 
266b9 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 64 62 20  */.#endif..  db 
266ba 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
266bb 6d 65 6d 73 65 74 28 26 64 65 73 74 2c 20 30 2c  memset(&dest, 0,
266bc 20 73 69 7a 65 6f 66 28 64 65 73 74 29 29 3b 0a   sizeof(dest));.
266bd 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45    if( pParse->nE
266be 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  rr || db->malloc
266bf 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f  Failed ){.    go
266c0 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75  to insert_cleanu
266c1 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 63  p;.  }..  /* Loc
266c2 61 74 65 20 74 68 65 20 74 61 62 6c 65 20 69 6e  ate the table in
266c3 74 6f 20 77 68 69 63 68 20 77 65 20 77 69 6c 6c  to which we will
266c4 20 62 65 20 69 6e 73 65 72 74 69 6e 67 20 6e 65   be inserting ne
266c5 77 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 20  w information.. 
266c6 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 54   */.  assert( pT
266c7 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31 20  abList->nSrc==1 
266c8 29 3b 0a 20 20 7a 54 61 62 20 3d 20 70 54 61 62  );.  zTab = pTab
266c9 4c 69 73 74 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65  List->a[0].zName
266ca 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 7a 54  ;.  if( NEVER(zT
266cb 61 62 3d 3d 30 29 20 29 20 67 6f 74 6f 20 69 6e  ab==0) ) goto in
266cc 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  sert_cleanup;.  
266cd 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53 72  pTab = sqlite3Sr
266ce 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72  cListLookup(pPar
266cf 73 65 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a 20  se, pTabList);. 
266d0 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29 7b 0a   if( pTab==0 ){.
266d1 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f      goto insert_
266d2 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69  cleanup;.  }.  i
266d3 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
266d4 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54  maToIndex(db, pT
266d5 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  ab->pSchema);.  
266d6 61 73 73 65 72 74 28 20 69 44 62 3c 64 62 2d 3e  assert( iDb<db->
266d7 6e 44 62 20 29 3b 0a 20 20 70 44 62 20 3d 20 26  nDb );.  pDb = &
266d8 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20  db->aDb[iDb];.  
266d9 7a 44 62 20 3d 20 70 44 62 2d 3e 7a 4e 61 6d 65  zDb = pDb->zName
266da 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 41  ;.  if( sqlite3A
266db 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
266dc 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 2c 20   SQLITE_INSERT, 
266dd 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 20  pTab->zName, 0, 
266de 7a 44 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  zDb) ){.    goto
266df 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b   insert_cleanup;
266e0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72  .  }..  /* Figur
266e1 65 20 6f 75 74 20 69 66 20 77 65 20 68 61 76 65  e out if we have
266e2 20 61 6e 79 20 74 72 69 67 67 65 72 73 20 61 6e   any triggers an
266e3 64 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 62  d if the table b
266e4 65 69 6e 67 0a 20 20 2a 2a 20 69 6e 73 65 72 74  eing.  ** insert
266e5 65 64 20 69 6e 74 6f 20 69 73 20 61 20 76 69 65  ed into is a vie
266e6 77 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  w.  */.#ifndef S
266e7 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
266e8 45 52 0a 20 20 70 54 72 69 67 67 65 72 20 3d 20  ER.  pTrigger = 
266e9 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 73 45  sqlite3TriggersE
266ea 78 69 73 74 28 70 50 61 72 73 65 2c 20 70 54 61  xist(pParse, pTa
266eb 62 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20 30 2c  b, TK_INSERT, 0,
266ec 20 26 74 6d 61 73 6b 29 3b 0a 20 20 69 73 56 69   &tmask);.  isVi
266ed 65 77 20 3d 20 70 54 61 62 2d 3e 70 53 65 6c 65  ew = pTab->pSele
266ee 63 74 21 3d 30 3b 0a 23 65 6c 73 65 0a 23 20 64  ct!=0;.#else.# d
266ef 65 66 69 6e 65 20 70 54 72 69 67 67 65 72 20 30  efine pTrigger 0
266f0 0a 23 20 64 65 66 69 6e 65 20 74 6d 61 73 6b 20  .# define tmask 
266f1 30 0a 23 20 64 65 66 69 6e 65 20 69 73 56 69 65  0.# define isVie
266f2 77 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 64 65  w 0.#endif.#ifde
266f3 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
266f4 45 57 0a 23 20 75 6e 64 65 66 20 69 73 56 69 65  EW.# undef isVie
266f5 77 0a 23 20 64 65 66 69 6e 65 20 69 73 56 69 65  w.# define isVie
266f6 77 20 30 0a 23 65 6e 64 69 66 0a 20 20 61 73 73  w 0.#endif.  ass
266f7 65 72 74 28 20 28 70 54 72 69 67 67 65 72 20 26  ert( (pTrigger &
266f8 26 20 74 6d 61 73 6b 29 20 7c 7c 20 28 70 54 72  & tmask) || (pTr
266f9 69 67 67 65 72 3d 3d 30 20 26 26 20 74 6d 61 73  igger==0 && tmas
266fa 6b 3d 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 49  k==0) );..  /* I
266fb 66 20 70 54 61 62 20 69 73 20 72 65 61 6c 6c 79  f pTab is really
266fc 20 61 20 76 69 65 77 2c 20 6d 61 6b 65 20 73 75   a view, make su
266fd 72 65 20 69 74 20 68 61 73 20 62 65 65 6e 20 69  re it has been i
266fe 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 20 20 2a 2a  nitialized..  **
266ff 20 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61   ViewGetColumnNa
26700 6d 65 73 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70  mes() is a no-op
26701 20 69 66 20 70 54 61 62 20 69 73 20 6e 6f 74 20   if pTab is not 
26702 61 20 76 69 65 77 20 28 6f 72 20 76 69 72 74 75  a view (or virtu
26703 61 6c 20 0a 20 20 2a 2a 20 6d 6f 64 75 6c 65 20  al .  ** module 
26704 74 61 62 6c 65 29 2e 0a 20 20 2a 2f 0a 20 20 69  table)..  */.  i
26705 66 28 20 73 71 6c 69 74 65 33 56 69 65 77 47 65  f( sqlite3ViewGe
26706 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61  tColumnNames(pPa
26707 72 73 65 2c 20 70 54 61 62 29 20 29 7b 0a 20 20  rse, pTab) ){.  
26708 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c    goto insert_cl
26709 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  eanup;.  }..  /*
2670a 20 45 6e 73 75 72 65 20 74 68 61 74 3a 0a 20 20   Ensure that:.  
2670b 2a 20 20 28 61 29 20 74 68 65 20 74 61 62 6c 65  *  (a) the table
2670c 20 69 73 20 6e 6f 74 20 72 65 61 64 2d 6f 6e 6c   is not read-onl
2670d 79 2c 20 0a 20 20 2a 20 20 28 62 29 20 74 68 61  y, .  *  (b) tha
2670e 74 20 69 66 20 69 74 20 69 73 20 61 20 76 69 65  t if it is a vie
2670f 77 20 74 68 65 6e 20 4f 4e 20 49 4e 53 45 52 54  w then ON INSERT
26710 20 74 72 69 67 67 65 72 73 20 65 78 69 73 74 0a   triggers exist.
26711 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
26712 65 33 49 73 52 65 61 64 4f 6e 6c 79 28 70 50 61  e3IsReadOnly(pPa
26713 72 73 65 2c 20 70 54 61 62 2c 20 74 6d 61 73 6b  rse, pTab, tmask
26714 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69 6e  ) ){.    goto in
26715 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  sert_cleanup;.  
26716 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  }..  /* Allocate
26717 20 61 20 56 44 42 45 0a 20 20 2a 2f 0a 20 20 76   a VDBE.  */.  v
26718 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
26719 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28  e(pParse);.  if(
2671a 20 76 3d 3d 30 20 29 20 67 6f 74 6f 20 69 6e 73   v==0 ) goto ins
2671b 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 69  ert_cleanup;.  i
2671c 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65  f( pParse->neste
2671d 64 3d 3d 30 20 29 20 73 71 6c 69 74 65 33 56 64  d==0 ) sqlite3Vd
2671e 62 65 43 6f 75 6e 74 43 68 61 6e 67 65 73 28 76  beCountChanges(v
2671f 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69  );.  sqlite3Begi
26720 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28  nWriteOperation(
26721 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 20  pParse, pSelect 
26722 7c 7c 20 70 54 72 69 67 67 65 72 2c 20 69 44 62  || pTrigger, iDb
26723 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  );..#ifndef SQLI
26724 54 45 5f 4f 4d 49 54 5f 58 46 45 52 5f 4f 50 54  TE_OMIT_XFER_OPT
26725 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73 74 61  .  /* If the sta
26726 74 65 6d 65 6e 74 20 69 73 20 6f 66 20 74 68 65  tement is of the
26727 20 66 6f 72 6d 0a 20 20 2a 2a 0a 20 20 2a 2a 20   form.  **.  ** 
26728 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54        INSERT INT
26729 4f 20 3c 74 61 62 6c 65 31 3e 20 53 45 4c 45 43  O <table1> SELEC
2672a 54 20 2a 20 46 52 4f 4d 20 3c 74 61 62 6c 65 32  T * FROM <table2
2672b 3e 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  >;.  **.  ** The
2672c 6e 20 73 70 65 63 69 61 6c 20 6f 70 74 69 6d 69  n special optimi
2672d 7a 61 74 69 6f 6e 73 20 63 61 6e 20 62 65 20 61  zations can be a
2672e 70 70 6c 69 65 64 20 74 68 61 74 20 6d 61 6b 65  pplied that make
2672f 20 74 68 65 20 74 72 61 6e 73 66 65 72 0a 20 20   the transfer.  
26730 2a 2a 20 76 65 72 79 20 66 61 73 74 20 61 6e 64  ** very fast and
26731 20 77 68 69 63 68 20 72 65 64 75 63 65 20 66 72   which reduce fr
26732 61 67 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 69  agmentation of i
26733 6e 64 69 63 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a  ndices..  **.  *
26734 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 32 6e  * This is the 2n
26735 64 20 74 65 6d 70 6c 61 74 65 2e 0a 20 20 2a 2f  d template..  */
26736 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 3d 3d  .  if( pColumn==
26737 30 20 26 26 20 78 66 65 72 4f 70 74 69 6d 69 7a  0 && xferOptimiz
26738 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 70 54  ation(pParse, pT
26739 61 62 2c 20 70 53 65 6c 65 63 74 2c 20 6f 6e 45  ab, pSelect, onE
2673a 72 72 6f 72 2c 20 69 44 62 29 20 29 7b 0a 20 20  rror, iDb) ){.  
2673b 20 20 61 73 73 65 72 74 28 20 21 70 54 72 69 67    assert( !pTrig
2673c 67 65 72 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ger );.    asser
2673d 74 28 20 70 4c 69 73 74 3d 3d 30 20 29 3b 0a 20  t( pList==0 );. 
2673e 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 65     goto insert_e
2673f 6e 64 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f  nd;.  }.#endif /
26740 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 58 46  * SQLITE_OMIT_XF
26741 45 52 5f 4f 50 54 20 2a 2f 0a 0a 20 20 2f 2a 20  ER_OPT */..  /* 
26742 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 41 55  If this is an AU
26743 54 4f 49 4e 43 52 45 4d 45 4e 54 20 74 61 62 6c  TOINCREMENT tabl
26744 65 2c 20 6c 6f 6f 6b 20 75 70 20 74 68 65 20 73  e, look up the s
26745 65 71 75 65 6e 63 65 20 6e 75 6d 62 65 72 20 69  equence number i
26746 6e 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74  n the.  ** sqlit
26747 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65  e_sequence table
26748 20 61 6e 64 20 73 74 6f 72 65 20 69 74 20 69 6e   and store it in
26749 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 72 65 67   memory cell reg
2674a 41 75 74 6f 69 6e 63 2e 0a 20 20 2a 2f 0a 20 20  Autoinc..  */.  
2674b 72 65 67 41 75 74 6f 69 6e 63 20 3d 20 61 75 74  regAutoinc = aut
2674c 6f 49 6e 63 42 65 67 69 6e 28 70 50 61 72 73 65  oIncBegin(pParse
2674d 2c 20 69 44 62 2c 20 70 54 61 62 29 3b 0a 0a 20  , iDb, pTab);.. 
2674e 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68   /* Figure out h
2674f 6f 77 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73 20  ow many columns 
26750 6f 66 20 64 61 74 61 20 61 72 65 20 73 75 70 70  of data are supp
26751 6c 69 65 64 2e 20 20 49 66 20 74 68 65 20 64 61  lied.  If the da
26752 74 61 0a 20 20 2a 2a 20 69 73 20 63 6f 6d 69 6e  ta.  ** is comin
26753 67 20 66 72 6f 6d 20 61 20 53 45 4c 45 43 54 20  g from a SELECT 
26754 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20  statement, then 
26755 67 65 6e 65 72 61 74 65 20 61 20 63 6f 2d 72 6f  generate a co-ro
26756 75 74 69 6e 65 20 74 68 61 74 0a 20 20 2a 2a 20  utine that.  ** 
26757 70 72 6f 64 75 63 65 73 20 61 20 73 69 6e 67 6c  produces a singl
26758 65 20 72 6f 77 20 6f 66 20 74 68 65 20 53 45 4c  e row of the SEL
26759 45 43 54 20 6f 6e 20 65 61 63 68 20 69 6e 76 6f  ECT on each invo
2675a 63 61 74 69 6f 6e 2e 20 20 54 68 65 0a 20 20 2a  cation.  The.  *
2675b 2a 20 63 6f 2d 72 6f 75 74 69 6e 65 20 69 73 20  * co-routine is 
2675c 74 68 65 20 63 6f 6d 6d 6f 6e 20 68 65 61 64 65  the common heade
2675d 72 20 74 6f 20 74 68 65 20 33 72 64 20 61 6e 64  r to the 3rd and
2675e 20 34 74 68 20 74 65 6d 70 6c 61 74 65 73 2e 0a   4th templates..
2675f 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 65 6c 65    */.  if( pSele
26760 63 74 20 29 7b 0a 20 20 20 20 2f 2a 20 44 61 74  ct ){.    /* Dat
26761 61 20 69 73 20 63 6f 6d 69 6e 67 20 66 72 6f 6d  a is coming from
26762 20 61 20 53 45 4c 45 43 54 2e 20 20 47 65 6e 65   a SELECT.  Gene
26763 72 61 74 65 20 63 6f 64 65 20 74 6f 20 69 6d 70  rate code to imp
26764 6c 65 6d 65 6e 74 20 74 68 61 74 20 53 45 4c 45  lement that SELE
26765 43 54 0a 20 20 20 20 2a 2a 20 61 73 20 61 20 63  CT.    ** as a c
26766 6f 2d 72 6f 75 74 69 6e 65 2e 20 20 54 68 65 20  o-routine.  The 
26767 63 6f 64 65 20 69 73 20 63 6f 6d 6d 6f 6e 20 74  code is common t
26768 6f 20 62 6f 74 68 20 74 68 65 20 33 72 64 20 61  o both the 3rd a
26769 6e 64 20 34 74 68 0a 20 20 20 20 2a 2a 20 74 65  nd 4th.    ** te
2676a 6d 70 6c 61 74 65 73 3a 0a 20 20 20 20 2a 2a 0a  mplates:.    **.
2676b 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 45      **         E
2676c 4f 46 20 3c 2d 20 30 0a 20 20 20 20 2a 2a 20 20  OF <- 0.    **  
2676d 20 20 20 20 20 20 20 58 20 3c 2d 20 41 0a 20 20         X <- A.  
2676e 20 20 2a 2a 20 20 20 20 20 20 20 20 20 67 6f 74    **         got
2676f 6f 20 42 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  o B.    **      
26770 41 3a 20 73 65 74 75 70 20 66 6f 72 20 74 68 65  A: setup for the
26771 20 53 45 4c 45 43 54 0a 20 20 20 20 2a 2a 20 20   SELECT.    **  
26772 20 20 20 20 20 20 20 6c 6f 6f 70 20 6f 76 65 72         loop over
26773 20 74 68 65 20 74 61 62 6c 65 73 20 69 6e 20 74   the tables in t
26774 68 65 20 53 45 4c 45 43 54 0a 20 20 20 20 2a 2a  he SELECT.    **
26775 20 20 20 20 20 20 20 20 20 20 20 6c 6f 61 64 20             load 
26776 76 61 6c 75 65 20 69 6e 74 6f 20 72 65 67 69 73  value into regis
26777 74 65 72 20 52 2e 2e 52 2b 6e 0a 20 20 20 20 2a  ter R..R+n.    *
26778 2a 20 20 20 20 20 20 20 20 20 20 20 79 69 65 6c  *           yiel
26779 64 20 58 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  d X.    **      
2677a 20 20 20 65 6e 64 20 6c 6f 6f 70 0a 20 20 20 20     end loop.    
2677b 2a 2a 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e  **         clean
2677c 75 70 20 61 66 74 65 72 20 74 68 65 20 53 45 4c  up after the SEL
2677d 45 43 54 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  ECT.    **      
2677e 20 20 20 45 4f 46 20 3c 2d 20 31 0a 20 20 20 20     EOF <- 1.    
2677f 2a 2a 20 20 20 20 20 20 20 20 20 79 69 65 6c 64  **         yield
26780 20 58 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20   X.    **       
26781 20 20 68 61 6c 74 2d 65 72 72 6f 72 0a 20 20 20    halt-error.   
26782 20 2a 2a 0a 20 20 20 20 2a 2a 20 4f 6e 20 65 61   **.    ** On ea
26783 63 68 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66  ch invocation of
26784 20 74 68 65 20 63 6f 2d 72 6f 75 74 69 6e 65 2c   the co-routine,
26785 20 69 74 20 70 75 74 73 20 61 20 73 69 6e 67 6c   it puts a singl
26786 65 20 72 6f 77 20 6f 66 20 74 68 65 0a 20 20 20  e row of the.   
26787 20 2a 2a 20 53 45 4c 45 43 54 20 72 65 73 75 6c   ** SELECT resul
26788 74 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 73  t into registers
26789 20 64 65 73 74 2e 69 4d 65 6d 2e 2e 2e 64 65 73   dest.iMem...des
2678a 74 2e 69 4d 65 6d 2b 64 65 73 74 2e 6e 4d 65 6d  t.iMem+dest.nMem
2678b 2d 31 2e 0a 20 20 20 20 2a 2a 20 28 54 68 65 73  -1..    ** (Thes
2678c 65 20 6f 75 74 70 75 74 20 72 65 67 69 73 74 65  e output registe
2678d 72 73 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64  rs are allocated
2678e 20 62 79 20 73 71 6c 69 74 65 33 53 65 6c 65 63   by sqlite3Selec
2678f 74 28 29 2e 29 20 20 57 68 65 6e 0a 20 20 20 20  t().)  When.    
26790 2a 2a 20 74 68 65 20 53 45 4c 45 43 54 20 63 6f  ** the SELECT co
26791 6d 70 6c 65 74 65 73 2c 20 69 74 20 73 65 74 73  mpletes, it sets
26792 20 74 68 65 20 45 4f 46 20 66 6c 61 67 20 73 74   the EOF flag st
26793 6f 72 65 64 20 69 6e 20 72 65 67 45 6f 66 2e 0a  ored in regEof..
26794 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72      */.    int r
26795 63 2c 20 6a 31 3b 0a 0a 20 20 20 20 72 65 67 45  c, j1;..    regE
26796 6f 66 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  of = ++pParse->n
26797 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Mem;.    sqlite3
26798 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
26799 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67  _Integer, 0, reg
2679a 45 6f 66 29 3b 20 20 20 20 20 20 2f 2a 20 45 4f  Eof);      /* EO
2679b 46 20 3c 2d 20 30 20 2a 2f 0a 20 20 20 20 56 64  F <- 0 */.    Vd
2679c 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 53  beComment((v, "S
2679d 45 4c 45 43 54 20 65 6f 66 20 66 6c 61 67 22 29  ELECT eof flag")
2679e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65  );.    sqlite3Se
2679f 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26 64 65  lectDestInit(&de
267a0 73 74 2c 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e  st, SRT_Coroutin
267a1 65 2c 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65  e, ++pParse->nMe
267a2 6d 29 3b 0a 20 20 20 20 61 64 64 72 53 65 6c 65  m);.    addrSele
267a3 63 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  ct = sqlite3Vdbe
267a4 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 2b 32  CurrentAddr(v)+2
267a5 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
267a6 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
267a7 74 65 67 65 72 2c 20 61 64 64 72 53 65 6c 65 63  teger, addrSelec
267a8 74 2d 31 2c 20 64 65 73 74 2e 69 50 61 72 6d 29  t-1, dest.iParm)
267a9 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74  ;.    j1 = sqlit
267aa 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
267ab 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 30 29 3b 0a  OP_Goto, 0, 0);.
267ac 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
267ad 28 76 2c 20 22 4a 75 6d 70 20 6f 76 65 72 20 53  (v, "Jump over S
267ae 45 4c 45 43 54 20 63 6f 72 6f 75 74 69 6e 65 22  ELECT coroutine"
267af 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 73 6f  ));..    /* Reso
267b0 6c 76 65 20 74 68 65 20 65 78 70 72 65 73 73 69  lve the expressi
267b1 6f 6e 73 20 69 6e 20 74 68 65 20 53 45 4c 45 43  ons in the SELEC
267b2 54 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20  T statement and 
267b3 65 78 65 63 75 74 65 20 69 74 2e 20 2a 2f 0a 20  execute it. */. 
267b4 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53     rc = sqlite3S
267b5 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 53  elect(pParse, pS
267b6 65 6c 65 63 74 2c 20 26 64 65 73 74 29 3b 0a 20  elect, &dest);. 
267b7 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73     assert( pPars
267b8 65 2d 3e 6e 45 72 72 3d 3d 30 20 7c 7c 20 72 63  e->nErr==0 || rc
267b9 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 7c   );.    if( rc |
267ba 7c 20 4e 45 56 45 52 28 70 50 61 72 73 65 2d 3e  | NEVER(pParse->
267bb 6e 45 72 72 29 20 7c 7c 20 64 62 2d 3e 6d 61 6c  nErr) || db->mal
267bc 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
267bd 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63     goto insert_c
267be 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20  leanup;.    }.  
267bf 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
267c0 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
267c1 72 2c 20 31 2c 20 72 65 67 45 6f 66 29 3b 20 20  r, 1, regEof);  
267c2 20 20 20 20 20 20 20 2f 2a 20 45 4f 46 20 3c 2d         /* EOF <-
267c3 20 31 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65   1 */.    sqlite
267c4 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
267c5 50 5f 59 69 65 6c 64 2c 20 64 65 73 74 2e 69 50  P_Yield, dest.iP
267c6 61 72 6d 29 3b 20 20 20 2f 2a 20 79 69 65 6c 64  arm);   /* yield
267c7 20 58 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65   X */.    sqlite
267c8 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
267c9 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54 45 5f 49  P_Halt, SQLITE_I
267ca 4e 54 45 52 4e 41 4c 2c 20 4f 45 5f 41 62 6f 72  NTERNAL, OE_Abor
267cb 74 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d  t);.    VdbeComm
267cc 65 6e 74 28 28 76 2c 20 22 45 6e 64 20 6f 66 20  ent((v, "End of 
267cd 53 45 4c 45 43 54 20 63 6f 72 6f 75 74 69 6e 65  SELECT coroutine
267ce 22 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  "));.    sqlite3
267cf 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
267d0 6a 31 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  j1);            
267d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
267d2 20 6c 61 62 65 6c 20 42 3a 20 2a 2f 0a 0a 20 20   label B: */..  
267d3 20 20 72 65 67 46 72 6f 6d 53 65 6c 65 63 74 20    regFromSelect 
267d4 3d 20 64 65 73 74 2e 69 4d 65 6d 3b 0a 20 20 20  = dest.iMem;.   
267d5 20 61 73 73 65 72 74 28 20 70 53 65 6c 65 63 74   assert( pSelect
267d6 2d 3e 70 45 4c 69 73 74 20 29 3b 0a 20 20 20 20  ->pEList );.    
267d7 6e 43 6f 6c 75 6d 6e 20 3d 20 70 53 65 6c 65 63  nColumn = pSelec
267d8 74 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  t->pEList->nExpr
267d9 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 65  ;.    assert( de
267da 73 74 2e 6e 4d 65 6d 3d 3d 6e 43 6f 6c 75 6d 6e  st.nMem==nColumn
267db 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20   );..    /* Set 
267dc 75 73 65 54 65 6d 70 54 61 62 6c 65 20 74 6f 20  useTempTable to 
267dd 54 52 55 45 20 69 66 20 74 68 65 20 72 65 73 75  TRUE if the resu
267de 6c 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  lt of the SELECT
267df 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 20 20 2a   statement.    *
267e0 2a 20 73 68 6f 75 6c 64 20 62 65 20 77 72 69 74  * should be writ
267e1 74 65 6e 20 69 6e 74 6f 20 61 20 74 65 6d 70 6f  ten into a tempo
267e2 72 61 72 79 20 74 61 62 6c 65 20 28 74 65 6d 70  rary table (temp
267e3 6c 61 74 65 20 34 29 2e 20 20 53 65 74 20 74 6f  late 4).  Set to
267e4 0a 20 20 20 20 2a 2a 20 46 41 4c 53 45 20 69 66  .    ** FALSE if
267e5 20 65 61 63 68 2a 20 72 6f 77 20 6f 66 20 74 68   each* row of th
267e6 65 20 53 45 4c 45 43 54 20 63 61 6e 20 62 65 20  e SELECT can be 
267e7 77 72 69 74 74 65 6e 20 64 69 72 65 63 74 6c 79  written directly
267e8 20 69 6e 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65   into.    ** the
267e9 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 74 61 62   destination tab
267ea 6c 65 20 28 74 65 6d 70 6c 61 74 65 20 33 29 2e  le (template 3).
267eb 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 41  .    **.    ** A
267ec 20 74 65 6d 70 20 74 61 62 6c 65 20 6d 75 73 74   temp table must
267ed 20 62 65 20 75 73 65 64 20 69 66 20 74 68 65 20   be used if the 
267ee 74 61 62 6c 65 20 62 65 69 6e 67 20 75 70 64 61  table being upda
267ef 74 65 64 20 69 73 20 61 6c 73 6f 20 6f 6e 65 0a  ted is also one.
267f0 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 74 61      ** of the ta
267f1 62 6c 65 73 20 62 65 69 6e 67 20 72 65 61 64 20  bles being read 
267f2 62 79 20 74 68 65 20 53 45 4c 45 43 54 20 73 74  by the SELECT st
267f3 61 74 65 6d 65 6e 74 2e 20 20 41 6c 73 6f 20 75  atement.  Also u
267f4 73 65 20 61 20 0a 20 20 20 20 2a 2a 20 74 65 6d  se a .    ** tem
267f5 70 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 63  p table in the c
267f6 61 73 65 20 6f 66 20 72 6f 77 20 74 72 69 67 67  ase of row trigg
267f7 65 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ers..    */.    
267f8 69 66 28 20 70 54 72 69 67 67 65 72 20 7c 7c 20  if( pTrigger || 
267f9 72 65 61 64 73 54 61 62 6c 65 28 70 50 61 72 73  readsTable(pPars
267fa 65 2c 20 61 64 64 72 53 65 6c 65 63 74 2c 20 69  e, addrSelect, i
267fb 44 62 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20  Db, pTab) ){.   
267fc 20 20 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20     useTempTable 
267fd 3d 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  = 1;.    }..    
267fe 69 66 28 20 75 73 65 54 65 6d 70 54 61 62 6c 65  if( useTempTable
267ff 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e 76   ){.      /* Inv
26800 6f 6b 65 20 74 68 65 20 63 6f 72 6f 75 74 69 6e  oke the coroutin
26801 65 20 74 6f 20 65 78 74 72 61 63 74 20 69 6e 66  e to extract inf
26802 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68  ormation from th
26803 65 20 53 45 4c 45 43 54 0a 20 20 20 20 20 20 2a  e SELECT.      *
26804 2a 20 61 6e 64 20 61 64 64 20 69 74 20 74 6f 20  * and add it to 
26805 61 20 74 72 61 6e 73 69 65 6e 74 20 74 61 62 6c  a transient tabl
26806 65 20 73 72 63 54 61 62 2e 20 20 54 68 65 20 63  e srcTab.  The c
26807 6f 64 65 20 67 65 6e 65 72 61 74 65 64 0a 20 20  ode generated.  
26808 20 20 20 20 2a 2a 20 68 65 72 65 20 69 73 20 66      ** here is f
26809 72 6f 6d 20 74 68 65 20 34 74 68 20 74 65 6d 70  rom the 4th temp
2680a 6c 61 74 65 3a 0a 20 20 20 20 20 20 2a 2a 0a 20  late:.      **. 
2680b 20 20 20 20 20 2a 2a 20 20 20 20 20 20 42 3a 20       **      B: 
2680c 6f 70 65 6e 20 74 65 6d 70 20 74 61 62 6c 65 0a  open temp table.
2680d 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 4c 3a        **      L:
2680e 20 79 69 65 6c 64 20 58 0a 20 20 20 20 20 20 2a   yield X.      *
2680f 2a 20 20 20 20 20 20 20 20 20 69 66 20 45 4f 46  *         if EOF
26810 20 67 6f 74 6f 20 4d 0a 20 20 20 20 20 20 2a 2a   goto M.      **
26811 20 20 20 20 20 20 20 20 20 69 6e 73 65 72 74 20           insert 
26812 72 6f 77 20 66 72 6f 6d 20 52 2e 2e 52 2b 6e 20  row from R..R+n 
26813 69 6e 74 6f 20 74 65 6d 70 20 74 61 62 6c 65 0a  into temp table.
26814 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20        **        
26815 20 67 6f 74 6f 20 4c 0a 20 20 20 20 20 20 2a 2a   goto L.      **
26816 20 20 20 20 20 20 4d 3a 20 2e 2e 2e 0a 20 20 20        M: ....   
26817 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20     */.      int 
26818 72 65 67 52 65 63 3b 20 20 20 20 20 20 20 20 20  regRec;         
26819 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20   /* Register to 
2681a 68 6f 6c 64 20 70 61 63 6b 65 64 20 72 65 63 6f  hold packed reco
2681b 72 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  rd */.      int 
2681c 72 65 67 54 65 6d 70 52 6f 77 69 64 3b 20 20 20  regTempRowid;   
2681d 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20   /* Register to 
2681e 68 6f 6c 64 20 74 65 6d 70 20 74 61 62 6c 65 20  hold temp table 
2681f 52 4f 57 49 44 20 2a 2f 0a 20 20 20 20 20 20 69  ROWID */.      i
26820 6e 74 20 61 64 64 72 54 6f 70 3b 20 20 20 20 20  nt addrTop;     
26821 20 20 20 20 2f 2a 20 4c 61 62 65 6c 20 22 4c 22      /* Label "L"
26822 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 64   */.      int ad
26823 64 72 49 66 3b 20 20 20 20 20 20 20 20 20 20 2f  drIf;          /
26824 2a 20 41 64 64 72 65 73 73 20 6f 66 20 6a 75 6d  * Address of jum
26825 70 20 74 6f 20 4d 20 2a 2f 0a 0a 20 20 20 20 20  p to M */..     
26826 20 73 72 63 54 61 62 20 3d 20 70 50 61 72 73 65   srcTab = pParse
26827 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20  ->nTab++;.      
26828 72 65 67 52 65 63 20 3d 20 73 71 6c 69 74 65 33  regRec = sqlite3
26829 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
2682a 65 29 3b 0a 20 20 20 20 20 20 72 65 67 54 65 6d  e);.      regTem
2682b 70 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33  pRowid = sqlite3
2682c 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
2682d 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  e);.      sqlite
2682e 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2682f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c  P_OpenEphemeral,
26830 20 73 72 63 54 61 62 2c 20 6e 43 6f 6c 75 6d 6e   srcTab, nColumn
26831 29 3b 0a 20 20 20 20 20 20 61 64 64 72 54 6f 70  );.      addrTop
26832 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
26833 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64  dOp1(v, OP_Yield
26834 2c 20 64 65 73 74 2e 69 50 61 72 6d 29 3b 0a 20  , dest.iParm);. 
26835 20 20 20 20 20 61 64 64 72 49 66 20 3d 20 73 71       addrIf = sq
26836 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
26837 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66  v, OP_If, regEof
26838 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
26839 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2683a 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67  _MakeRecord, reg
2683b 46 72 6f 6d 53 65 6c 65 63 74 2c 20 6e 43 6f 6c  FromSelect, nCol
2683c 75 6d 6e 2c 20 72 65 67 52 65 63 29 3b 0a 20 20  umn, regRec);.  
2683d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2683e 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52  ddOp2(v, OP_NewR
2683f 6f 77 69 64 2c 20 73 72 63 54 61 62 2c 20 72 65  owid, srcTab, re
26840 67 54 65 6d 70 52 6f 77 69 64 29 3b 0a 20 20 20  gTempRowid);.   
26841 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
26842 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72  dOp3(v, OP_Inser
26843 74 2c 20 73 72 63 54 61 62 2c 20 72 65 67 52 65  t, srcTab, regRe
26844 63 2c 20 72 65 67 54 65 6d 70 52 6f 77 69 64 29  c, regTempRowid)
26845 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
26846 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
26847 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72 54 6f 70  Goto, 0, addrTop
26848 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
26849 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
2684a 61 64 64 72 49 66 29 3b 0a 20 20 20 20 20 20 73  addrIf);.      s
2684b 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
2684c 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67  pReg(pParse, reg
2684d 52 65 63 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  Rec);.      sqli
2684e 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
2684f 67 28 70 50 61 72 73 65 2c 20 72 65 67 54 65 6d  g(pParse, regTem
26850 70 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a 20  pRowid);.    }. 
26851 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54   }else{.    /* T
26852 68 69 73 20 69 73 20 74 68 65 20 63 61 73 65 20  his is the case 
26853 69 66 20 74 68 65 20 64 61 74 61 20 66 6f 72 20  if the data for 
26854 74 68 65 20 49 4e 53 45 52 54 20 69 73 20 63 6f  the INSERT is co
26855 6d 69 6e 67 20 66 72 6f 6d 20 61 20 56 41 4c 55  ming from a VALU
26856 45 53 0a 20 20 20 20 2a 2a 20 63 6c 61 75 73 65  ES.    ** clause
26857 0a 20 20 20 20 2a 2f 0a 20 20 20 20 4e 61 6d 65  .    */.    Name
26858 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a 20 20 20  Context sNC;.   
26859 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c   memset(&sNC, 0,
2685a 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20   sizeof(sNC));. 
2685b 20 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20     sNC.pParse = 
2685c 70 50 61 72 73 65 3b 0a 20 20 20 20 73 72 63 54  pParse;.    srcT
2685d 61 62 20 3d 20 2d 31 3b 0a 20 20 20 20 61 73 73  ab = -1;.    ass
2685e 65 72 74 28 20 75 73 65 54 65 6d 70 54 61 62 6c  ert( useTempTabl
2685f 65 3d 3d 30 20 29 3b 0a 20 20 20 20 6e 43 6f 6c  e==0 );.    nCol
26860 75 6d 6e 20 3d 20 70 4c 69 73 74 20 3f 20 70 4c  umn = pList ? pL
26861 69 73 74 2d 3e 6e 45 78 70 72 20 3a 20 30 3b 0a  ist->nExpr : 0;.
26862 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
26863 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Column; i++){.  
26864 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52      if( sqlite3R
26865 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28  esolveExprNames(
26866 26 73 4e 43 2c 20 70 4c 69 73 74 2d 3e 61 5b 69  &sNC, pList->a[i
26867 5d 2e 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20  ].pExpr) ){.    
26868 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f      goto insert_
26869 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 7d  cleanup;.      }
2686a 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
2686b 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6e   Make sure the n
2686c 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
2686d 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64   in the source d
2686e 61 74 61 20 6d 61 74 63 68 65 73 20 74 68 65 20  ata matches the 
2686f 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 6f 66 20 63  number.  ** of c
26870 6f 6c 75 6d 6e 73 20 74 6f 20 62 65 20 69 6e 73  olumns to be ins
26871 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20 74  erted into the t
26872 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  able..  */.  if(
26873 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
26874 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   ){.    for(i=0;
26875 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69   i<pTab->nCol; i
26876 2b 2b 29 7b 0a 20 20 20 20 20 20 6e 48 69 64 64  ++){.      nHidd
26877 65 6e 20 2b 3d 20 28 49 73 48 69 64 64 65 6e 43  en += (IsHiddenC
26878 6f 6c 75 6d 6e 28 26 70 54 61 62 2d 3e 61 43 6f  olumn(&pTab->aCo
26879 6c 5b 69 5d 29 20 3f 20 31 20 3a 20 30 29 3b 0a  l[i]) ? 1 : 0);.
2687a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
2687b 70 43 6f 6c 75 6d 6e 3d 3d 30 20 26 26 20 6e 43  pColumn==0 && nC
2687c 6f 6c 75 6d 6e 20 26 26 20 6e 43 6f 6c 75 6d 6e  olumn && nColumn
2687d 21 3d 28 70 54 61 62 2d 3e 6e 43 6f 6c 2d 6e 48  !=(pTab->nCol-nH
2687e 69 64 64 65 6e 29 20 29 7b 0a 20 20 20 20 73 71  idden) ){.    sq
2687f 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
26880 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 22 74  arse, .       "t
26881 61 62 6c 65 20 25 53 20 68 61 73 20 25 64 20 63  able %S has %d c
26882 6f 6c 75 6d 6e 73 20 62 75 74 20 25 64 20 76 61  olumns but %d va
26883 6c 75 65 73 20 77 65 72 65 20 73 75 70 70 6c 69  lues were suppli
26884 65 64 22 2c 0a 20 20 20 20 20 20 20 70 54 61 62  ed",.       pTab
26885 4c 69 73 74 2c 20 30 2c 20 70 54 61 62 2d 3e 6e  List, 0, pTab->n
26886 43 6f 6c 2d 6e 48 69 64 64 65 6e 2c 20 6e 43 6f  Col-nHidden, nCo
26887 6c 75 6d 6e 29 3b 0a 20 20 20 20 67 6f 74 6f 20  lumn);.    goto 
26888 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a  insert_cleanup;.
26889 20 20 7d 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d    }.  if( pColum
2688a 6e 21 3d 30 20 26 26 20 6e 43 6f 6c 75 6d 6e 21  n!=0 && nColumn!
2688b 3d 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 20 29 7b  =pColumn->nId ){
2688c 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
2688d 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 64  rMsg(pParse, "%d
2688e 20 76 61 6c 75 65 73 20 66 6f 72 20 25 64 20 63   values for %d c
2688f 6f 6c 75 6d 6e 73 22 2c 20 6e 43 6f 6c 75 6d 6e  olumns", nColumn
26890 2c 20 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 29 3b  , pColumn->nId);
26891 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74  .    goto insert
26892 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20  _cleanup;.  }.. 
26893 20 2f 2a 20 49 66 20 74 68 65 20 49 4e 53 45 52   /* If the INSER
26894 54 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 63 6c  T statement incl
26895 75 64 65 64 20 61 6e 20 49 44 4c 49 53 54 20 74  uded an IDLIST t
26896 65 72 6d 2c 20 74 68 65 6e 20 6d 61 6b 65 20 73  erm, then make s
26897 75 72 65 0a 20 20 2a 2a 20 61 6c 6c 20 65 6c 65  ure.  ** all ele
26898 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 49 44 4c  ments of the IDL
26899 49 53 54 20 72 65 61 6c 6c 79 20 61 72 65 20 63  IST really are c
2689a 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 74 61  olumns of the ta
2689b 62 6c 65 20 61 6e 64 20 0a 20 20 2a 2a 20 72 65  ble and .  ** re
2689c 6d 65 6d 62 65 72 20 74 68 65 20 63 6f 6c 75 6d  member the colum
2689d 6e 20 69 6e 64 69 63 65 73 2e 0a 20 20 2a 2a 0a  n indices..  **.
2689e 20 20 2a 2a 20 49 66 20 74 68 65 20 74 61 62 6c    ** If the tabl
2689f 65 20 68 61 73 20 61 6e 20 49 4e 54 45 47 45 52  e has an INTEGER
268a0 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c   PRIMARY KEY col
268a1 75 6d 6e 20 61 6e 64 20 74 68 61 74 20 63 6f 6c  umn and that col
268a2 75 6d 6e 0a 20 20 2a 2a 20 69 73 20 6e 61 6d 65  umn.  ** is name
268a3 64 20 69 6e 20 74 68 65 20 49 44 4c 49 53 54 2c  d in the IDLIST,
268a4 20 74 68 65 6e 20 72 65 63 6f 72 64 20 69 6e 20   then record in 
268a5 74 68 65 20 6b 65 79 43 6f 6c 75 6d 6e 20 76 61  the keyColumn va
268a6 72 69 61 62 6c 65 0a 20 20 2a 2a 20 74 68 65 20  riable.  ** the 
268a7 69 6e 64 65 78 20 69 6e 74 6f 20 49 44 4c 49 53  index into IDLIS
268a8 54 20 6f 66 20 74 68 65 20 70 72 69 6d 61 72 79  T of the primary
268a9 20 6b 65 79 20 63 6f 6c 75 6d 6e 2e 20 20 6b 65   key column.  ke
268aa 79 43 6f 6c 75 6d 6e 20 69 73 0a 20 20 2a 2a 20  yColumn is.  ** 
268ab 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
268ac 20 70 72 69 6d 61 72 79 20 6b 65 79 20 61 73 20   primary key as 
268ad 69 74 20 61 70 70 65 61 72 73 20 69 6e 20 49 44  it appears in ID
268ae 4c 49 53 54 2c 20 6e 6f 74 20 61 73 0a 20 20 2a  LIST, not as.  *
268af 2a 20 69 73 20 61 70 70 65 61 72 73 20 69 6e 20  * is appears in 
268b0 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 74 61 62  the original tab
268b1 6c 65 2e 20 20 28 54 68 65 20 69 6e 64 65 78 20  le.  (The index 
268b2 6f 66 20 74 68 65 20 70 72 69 6d 61 72 79 0a 20  of the primary. 
268b3 20 2a 2a 20 6b 65 79 20 69 6e 20 74 68 65 20 6f   ** key in the o
268b4 72 69 67 69 6e 61 6c 20 74 61 62 6c 65 20 69 73  riginal table is
268b5 20 70 54 61 62 2d 3e 69 50 4b 65 79 2e 29 0a 20   pTab->iPKey.). 
268b6 20 2a 2f 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d   */.  if( pColum
268b7 6e 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30  n ){.    for(i=0
268b8 3b 20 69 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64  ; i<pColumn->nId
268b9 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 43  ; i++){.      pC
268ba 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 69 64 78 20  olumn->a[i].idx 
268bb 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  = -1;.    }.    
268bc 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43 6f 6c 75  for(i=0; i<pColu
268bd 6d 6e 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20  mn->nId; i++){. 
268be 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
268bf 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29  pTab->nCol; j++)
268c0 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71  {.        if( sq
268c1 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f  lite3StrICmp(pCo
268c2 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65  lumn->a[i].zName
268c3 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e  , pTab->aCol[j].
268c4 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20  zName)==0 ){.   
268c5 20 20 20 20 20 20 20 70 43 6f 6c 75 6d 6e 2d 3e         pColumn->
268c6 61 5b 69 5d 2e 69 64 78 20 3d 20 6a 3b 0a 20 20  a[i].idx = j;.  
268c7 20 20 20 20 20 20 20 20 69 66 28 20 6a 3d 3d 70          if( j==p
268c8 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20  Tab->iPKey ){.  
268c9 20 20 20 20 20 20 20 20 20 20 6b 65 79 43 6f 6c            keyCol
268ca 75 6d 6e 20 3d 20 69 3b 0a 20 20 20 20 20 20 20  umn = i;.       
268cb 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62     }.          b
268cc 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
268cd 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
268ce 28 20 6a 3e 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20  ( j>=pTab->nCol 
268cf 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73  ){.        if( s
268d0 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 70 43  qlite3IsRowid(pC
268d1 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d  olumn->a[i].zNam
268d2 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  e) ){.          
268d3 6b 65 79 43 6f 6c 75 6d 6e 20 3d 20 69 3b 0a 20  keyColumn = i;. 
268d4 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
268d5 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
268d6 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
268d7 22 74 61 62 6c 65 20 25 53 20 68 61 73 20 6e 6f  "table %S has no
268d8 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 25 73   column named %s
268d9 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
268da 20 70 54 61 62 4c 69 73 74 2c 20 30 2c 20 70 43   pTabList, 0, pC
268db 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d  olumn->a[i].zNam
268dc 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50  e);.          pP
268dd 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20  arse->nErr++;.  
268de 20 20 20 20 20 20 20 20 67 6f 74 6f 20 69 6e 73          goto ins
268df 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20  ert_cleanup;.   
268e0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
268e1 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49     }.  }..  /* I
268e2 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 49 44  f there is no ID
268e3 4c 49 53 54 20 74 65 72 6d 20 62 75 74 20 74 68  LIST term but th
268e4 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e 20 69  e table has an i
268e5 6e 74 65 67 65 72 20 70 72 69 6d 61 72 79 0a 20  nteger primary. 
268e6 20 2a 2a 20 6b 65 79 2c 20 74 68 65 20 73 65 74   ** key, the set
268e7 20 74 68 65 20 6b 65 79 43 6f 6c 75 6d 6e 20 76   the keyColumn v
268e8 61 72 69 61 62 6c 65 20 74 6f 20 74 68 65 20 70  ariable to the p
268e9 72 69 6d 61 72 79 20 6b 65 79 20 63 6f 6c 75 6d  rimary key colum
268ea 6e 20 69 6e 64 65 78 0a 20 20 2a 2a 20 69 6e 20  n index.  ** in 
268eb 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 74 61 62  the original tab
268ec 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 20  le definition.. 
268ed 20 2a 2f 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d   */.  if( pColum
268ee 6e 3d 3d 30 20 26 26 20 6e 43 6f 6c 75 6d 6e 3e  n==0 && nColumn>
268ef 30 20 29 7b 0a 20 20 20 20 6b 65 79 43 6f 6c 75  0 ){.    keyColu
268f0 6d 6e 20 3d 20 70 54 61 62 2d 3e 69 50 4b 65 79  mn = pTab->iPKey
268f1 3b 0a 20 20 7d 0a 20 20 20 20 0a 20 20 2f 2a 20  ;.  }.    .  /* 
268f2 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63  Initialize the c
268f3 6f 75 6e 74 20 6f 66 20 72 6f 77 73 20 74 6f 20  ount of rows to 
268f4 62 65 20 69 6e 73 65 72 74 65 64 0a 20 20 2a 2f  be inserted.  */
268f5 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73  .  if( db->flags
268f6 20 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52   & SQLITE_CountR
268f7 6f 77 73 20 29 7b 0a 20 20 20 20 72 65 67 52 6f  ows ){.    regRo
268f8 77 43 6f 75 6e 74 20 3d 20 2b 2b 70 50 61 72 73  wCount = ++pPars
268f9 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c  e->nMem;.    sql
268fa 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
268fb 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c  , OP_Integer, 0,
268fc 20 72 65 67 52 6f 77 43 6f 75 6e 74 29 3b 0a 20   regRowCount);. 
268fd 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73   }..  /* If this
268fe 20 69 73 20 6e 6f 74 20 61 20 76 69 65 77 2c 20   is not a view, 
268ff 6f 70 65 6e 20 74 68 65 20 74 61 62 6c 65 20 61  open the table a
26900 6e 64 20 61 6e 64 20 61 6c 6c 20 69 6e 64 69 63  nd and all indic
26901 65 73 20 2a 2f 0a 20 20 69 66 28 20 21 69 73 56  es */.  if( !isV
26902 69 65 77 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e  iew ){.    int n
26903 49 64 78 3b 0a 0a 20 20 20 20 62 61 73 65 43 75  Idx;..    baseCu
26904 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  r = pParse->nTab
26905 3b 0a 20 20 20 20 6e 49 64 78 20 3d 20 73 71 6c  ;.    nIdx = sql
26906 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64  ite3OpenTableAnd
26907 49 6e 64 69 63 65 73 28 70 50 61 72 73 65 2c 20  Indices(pParse, 
26908 70 54 61 62 2c 20 62 61 73 65 43 75 72 2c 20 4f  pTab, baseCur, O
26909 50 5f 4f 70 65 6e 57 72 69 74 65 29 3b 0a 20 20  P_OpenWrite);.  
2690a 20 20 61 52 65 67 49 64 78 20 3d 20 73 71 6c 69    aRegIdx = sqli
2690b 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
2690c 62 2c 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 28  b, sizeof(int)*(
2690d 6e 49 64 78 2b 31 29 29 3b 0a 20 20 20 20 69 66  nIdx+1));.    if
2690e 28 20 61 52 65 67 49 64 78 3d 3d 30 20 29 7b 0a  ( aRegIdx==0 ){.
2690f 20 20 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72        goto inser
26910 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d  t_cleanup;.    }
26911 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
26912 6e 49 64 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nIdx; i++){.    
26913 20 20 61 52 65 67 49 64 78 5b 69 5d 20 3d 20 2b    aRegIdx[i] = +
26914 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
26915 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54     }.  }..  /* T
26916 68 69 73 20 69 73 20 74 68 65 20 74 6f 70 20 6f  his is the top o
26917 66 20 74 68 65 20 6d 61 69 6e 20 69 6e 73 65 72  f the main inser
26918 74 69 6f 6e 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69  tion loop */.  i
26919 66 28 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20  f( useTempTable 
2691a 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 62  ){.    /* This b
2691b 6c 6f 63 6b 20 63 6f 64 65 73 20 74 68 65 20 74  lock codes the t
2691c 6f 70 20 6f 66 20 6c 6f 6f 70 20 6f 6e 6c 79 2e  op of loop only.
2691d 20 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 6c    The complete l
2691e 6f 6f 70 20 69 73 20 74 68 65 0a 20 20 20 20 2a  oop is the.    *
2691f 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 73 65 75  * following pseu
26920 64 6f 63 6f 64 65 20 28 74 65 6d 70 6c 61 74 65  docode (template
26921 20 34 29 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20   4):.    **.    
26922 2a 2a 20 20 20 20 20 20 20 20 20 72 65 77 69 6e  **         rewin
26923 64 20 74 65 6d 70 20 74 61 62 6c 65 0a 20 20 20  d temp table.   
26924 20 2a 2a 20 20 20 20 20 20 43 3a 20 6c 6f 6f 70   **      C: loop
26925 20 6f 76 65 72 20 72 6f 77 73 20 6f 66 20 69 6e   over rows of in
26926 74 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c 65  termediate table
26927 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
26928 20 20 74 72 61 6e 73 66 65 72 20 76 61 6c 75 65    transfer value
26929 73 20 66 6f 72 6d 20 69 6e 74 65 72 6d 65 64 69  s form intermedi
2692a 61 74 65 20 74 61 62 6c 65 20 69 6e 74 6f 20 3c  ate table into <
2692b 74 61 62 6c 65 3e 0a 20 20 20 20 2a 2a 20 20 20  table>.    **   
2692c 20 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70 0a 20        end loop. 
2692d 20 20 20 2a 2a 20 20 20 20 20 20 44 3a 20 2e 2e     **      D: ..
2692e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 64 64  ..    */.    add
2692f 72 49 6e 73 54 6f 70 20 3d 20 73 71 6c 69 74 65  rInsTop = sqlite
26930 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
26931 50 5f 52 65 77 69 6e 64 2c 20 73 72 63 54 61 62  P_Rewind, srcTab
26932 29 3b 0a 20 20 20 20 61 64 64 72 43 6f 6e 74 20  );.    addrCont 
26933 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
26934 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 7d  rentAddr(v);.  }
26935 65 6c 73 65 20 69 66 28 20 70 53 65 6c 65 63 74  else if( pSelect
26936 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20   ){.    /* This 
26937 62 6c 6f 63 6b 20 63 6f 64 65 73 20 74 68 65 20  block codes the 
26938 74 6f 70 20 6f 66 20 6c 6f 6f 70 20 6f 6e 6c 79  top of loop only
26939 2e 20 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20  .  The complete 
2693a 6c 6f 6f 70 20 69 73 20 74 68 65 0a 20 20 20 20  loop is the.    
2693b 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 73 65  ** following pse
2693c 75 64 6f 63 6f 64 65 20 28 74 65 6d 70 6c 61 74  udocode (templat
2693d 65 20 33 29 3a 0a 20 20 20 20 2a 2a 0a 20 20 20  e 3):.    **.   
2693e 20 2a 2a 20 20 20 20 20 20 43 3a 20 79 69 65 6c   **      C: yiel
2693f 64 20 58 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  d X.    **      
26940 20 20 20 69 66 20 45 4f 46 20 67 6f 74 6f 20 44     if EOF goto D
26941 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
26942 69 6e 73 65 72 74 20 74 68 65 20 73 65 6c 65 63  insert the selec
26943 74 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 3c 74  t result into <t
26944 61 62 6c 65 3e 20 66 72 6f 6d 20 52 2e 2e 52 2b  able> from R..R+
26945 6e 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  n.    **        
26946 20 67 6f 74 6f 20 43 0a 20 20 20 20 2a 2a 20 20   goto C.    **  
26947 20 20 20 20 44 3a 20 2e 2e 2e 0a 20 20 20 20 2a      D: ....    *
26948 2f 0a 20 20 20 20 61 64 64 72 43 6f 6e 74 20 3d  /.    addrCont =
26949 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2694a 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20  p1(v, OP_Yield, 
2694b 64 65 73 74 2e 69 50 61 72 6d 29 3b 0a 20 20 20  dest.iParm);.   
2694c 20 61 64 64 72 49 6e 73 54 6f 70 20 3d 20 73 71   addrInsTop = sq
2694d 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
2694e 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66  v, OP_If, regEof
2694f 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c  );.  }..  /* All
26950 6f 63 61 74 65 20 72 65 67 69 73 74 65 72 73 20  ocate registers 
26951 66 6f 72 20 68 6f 6c 64 69 6e 67 20 74 68 65 20  for holding the 
26952 72 6f 77 69 64 20 6f 66 20 74 68 65 20 6e 65 77  rowid of the new
26953 20 72 6f 77 2c 0a 20 20 2a 2a 20 74 68 65 20 63   row,.  ** the c
26954 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 6e 65  ontent of the ne
26955 77 20 72 6f 77 2c 20 61 6e 64 20 74 68 65 20 61  w row, and the a
26956 73 73 65 6d 62 6c 69 65 64 20 72 6f 77 20 72 65  ssemblied row re
26957 63 6f 72 64 2e 0a 20 20 2a 2f 0a 20 20 72 65 67  cord..  */.  reg
26958 52 65 63 6f 72 64 20 3d 20 2b 2b 70 50 61 72 73  Record = ++pPars
26959 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65 67 52 6f  e->nMem;.  regRo
2695a 77 69 64 20 3d 20 72 65 67 49 6e 73 20 3d 20 70  wid = regIns = p
2695b 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20  Parse->nMem+1;. 
2695c 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d   pParse->nMem +=
2695d 20 70 54 61 62 2d 3e 6e 43 6f 6c 20 2b 20 31 3b   pTab->nCol + 1;
2695e 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c  .  if( IsVirtual
2695f 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 72 65  (pTab) ){.    re
26960 67 52 6f 77 69 64 2b 2b 3b 0a 20 20 20 20 70 50  gRowid++;.    pP
26961 61 72 73 65 2d 3e 6e 4d 65 6d 2b 2b 3b 0a 20 20  arse->nMem++;.  
26962 7d 0a 20 20 72 65 67 44 61 74 61 20 3d 20 72 65  }.  regData = re
26963 67 52 6f 77 69 64 2b 31 3b 0a 0a 20 20 2f 2a 20  gRowid+1;..  /* 
26964 52 75 6e 20 74 68 65 20 42 45 46 4f 52 45 20 61  Run the BEFORE a
26965 6e 64 20 49 4e 53 54 45 41 44 20 4f 46 20 74 72  nd INSTEAD OF tr
26966 69 67 67 65 72 73 2c 20 69 66 20 74 68 65 72 65  iggers, if there
26967 20 61 72 65 20 61 6e 79 0a 20 20 2a 2f 0a 20 20   are any.  */.  
26968 65 6e 64 4f 66 4c 6f 6f 70 20 3d 20 73 71 6c 69  endOfLoop = sqli
26969 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
2696a 28 76 29 3b 0a 20 20 69 66 28 20 74 6d 61 73 6b  (v);.  if( tmask
2696b 20 26 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52   & TRIGGER_BEFOR
2696c 45 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 67  E ){.    int reg
2696d 43 6f 6c 73 20 3d 20 73 71 6c 69 74 65 33 47 65  Cols = sqlite3Ge
2696e 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73  tTempRange(pPars
2696f 65 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 29  e, pTab->nCol+1)
26970 3b 0a 0a 20 20 20 20 2f 2a 20 62 75 69 6c 64 20  ;..    /* build 
26971 74 68 65 20 4e 45 57 2e 2a 20 72 65 66 65 72 65  the NEW.* refere
26972 6e 63 65 20 72 6f 77 2e 20 20 4e 6f 74 65 20 74  nce row.  Note t
26973 68 61 74 20 69 66 20 74 68 65 72 65 20 69 73 20  hat if there is 
26974 61 6e 20 49 4e 54 45 47 45 52 0a 20 20 20 20 2a  an INTEGER.    *
26975 2a 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69 6e  * PRIMARY KEY in
26976 74 6f 20 77 68 69 63 68 20 61 20 4e 55 4c 4c 20  to which a NULL 
26977 69 73 20 62 65 69 6e 67 20 69 6e 73 65 72 74 65  is being inserte
26978 64 2c 20 74 68 61 74 20 4e 55 4c 4c 20 77 69 6c  d, that NULL wil
26979 6c 20 62 65 0a 20 20 20 20 2a 2a 20 74 72 61 6e  l be.    ** tran
2697a 73 6c 61 74 65 64 20 69 6e 74 6f 20 61 20 75 6e  slated into a un
2697b 69 71 75 65 20 49 44 20 66 6f 72 20 74 68 65 20  ique ID for the 
2697c 72 6f 77 2e 20 20 42 75 74 20 6f 6e 20 61 20 42  row.  But on a B
2697d 45 46 4f 52 45 20 74 72 69 67 67 65 72 2c 0a 20  EFORE trigger,. 
2697e 20 20 20 2a 2a 20 77 65 20 64 6f 20 6e 6f 74 20     ** we do not 
2697f 6b 6e 6f 77 20 77 68 61 74 20 74 68 65 20 75 6e  know what the un
26980 69 71 75 65 20 49 44 20 77 69 6c 6c 20 62 65 20  ique ID will be 
26981 28 62 65 63 61 75 73 65 20 74 68 65 20 69 6e 73  (because the ins
26982 65 72 74 20 68 61 73 0a 20 20 20 20 2a 2a 20 6e  ert has.    ** n
26983 6f 74 20 68 61 70 70 65 6e 65 64 20 79 65 74 29  ot happened yet)
26984 20 73 6f 20 77 65 20 73 75 62 73 74 69 74 75 74   so we substitut
26985 65 20 61 20 72 6f 77 69 64 20 6f 66 20 2d 31 0a  e a rowid of -1.
26986 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6b      */.    if( k
26987 65 79 43 6f 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20  eyColumn<0 ){.  
26988 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
26989 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
2698a 67 65 72 2c 20 2d 31 2c 20 72 65 67 43 6f 6c 73  ger, -1, regCols
2698b 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
2698c 20 20 20 20 69 6e 74 20 6a 31 3b 0a 20 20 20 20      int j1;.    
2698d 20 20 69 66 28 20 75 73 65 54 65 6d 70 54 61 62    if( useTempTab
2698e 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  le ){.        sq
2698f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
26990 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73 72  v, OP_Column, sr
26991 63 54 61 62 2c 20 6b 65 79 43 6f 6c 75 6d 6e 2c  cTab, keyColumn,
26992 20 72 65 67 43 6f 6c 73 29 3b 0a 20 20 20 20 20   regCols);.     
26993 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
26994 61 73 73 65 72 74 28 20 70 53 65 6c 65 63 74 3d  assert( pSelect=
26995 3d 30 20 29 3b 20 20 2f 2a 20 4f 74 68 65 72 77  =0 );  /* Otherw
26996 69 73 65 20 75 73 65 54 65 6d 70 54 61 62 6c 65  ise useTempTable
26997 20 69 73 20 74 72 75 65 20 2a 2f 0a 20 20 20 20   is true */.    
26998 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
26999 6f 64 65 28 70 50 61 72 73 65 2c 20 70 4c 69 73  ode(pParse, pLis
2699a 74 2d 3e 61 5b 6b 65 79 43 6f 6c 75 6d 6e 5d 2e  t->a[keyColumn].
2699b 70 45 78 70 72 2c 20 72 65 67 43 6f 6c 73 29 3b  pExpr, regCols);
2699c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6a  .      }.      j
2699d 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  1 = sqlite3VdbeA
2699e 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e  ddOp1(v, OP_NotN
2699f 75 6c 6c 2c 20 72 65 67 43 6f 6c 73 29 3b 0a 20  ull, regCols);. 
269a0 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
269a1 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
269a2 65 67 65 72 2c 20 2d 31 2c 20 72 65 67 43 6f 6c  eger, -1, regCol
269a3 73 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  s);.      sqlite
269a4 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
269a5 20 6a 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69   j1);.      sqli
269a6 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
269a7 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 72   OP_MustBeInt, r
269a8 65 67 43 6f 6c 73 29 3b 0a 20 20 20 20 7d 0a 0a  egCols);.    }..
269a9 20 20 20 20 2f 2a 20 43 61 6e 6e 6f 74 20 68 61      /* Cannot ha
269aa 76 65 20 74 72 69 67 67 65 72 73 20 6f 6e 20 61  ve triggers on a
269ab 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20   virtual table. 
269ac 49 66 20 69 74 20 77 65 72 65 20 70 6f 73 73 69  If it were possi
269ad 62 6c 65 2c 0a 20 20 20 20 2a 2a 20 74 68 69 73  ble,.    ** this
269ae 20 62 6c 6f 63 6b 20 77 6f 75 6c 64 20 68 61 76   block would hav
269af 65 20 74 6f 20 61 63 63 6f 75 6e 74 20 66 6f 72  e to account for
269b0 20 68 69 64 64 65 6e 20 63 6f 6c 75 6d 6e 2e 0a   hidden column..
269b1 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72      */.    asser
269b2 74 28 20 21 49 73 56 69 72 74 75 61 6c 28 70 54  t( !IsVirtual(pT
269b3 61 62 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 43  ab) );..    /* C
269b4 72 65 61 74 65 20 74 68 65 20 6e 65 77 20 63 6f  reate the new co
269b5 6c 75 6d 6e 20 64 61 74 61 0a 20 20 20 20 2a 2f  lumn data.    */
269b6 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
269b7 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29  pTab->nCol; i++)
269b8 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c  {.      if( pCol
269b9 75 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  umn==0 ){.      
269ba 20 20 6a 20 3d 20 69 3b 0a 20 20 20 20 20 20 7d    j = i;.      }
269bb 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 66 6f  else{.        fo
269bc 72 28 6a 3d 30 3b 20 6a 3c 70 43 6f 6c 75 6d 6e  r(j=0; j<pColumn
269bd 2d 3e 6e 49 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20  ->nId; j++){.   
269be 20 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 75         if( pColu
269bf 6d 6e 2d 3e 61 5b 6a 5d 2e 69 64 78 3d 3d 69 20  mn->a[j].idx==i 
269c0 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20  ) break;.       
269c1 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
269c2 20 69 66 28 20 70 43 6f 6c 75 6d 6e 20 26 26 20   if( pColumn && 
269c3 6a 3e 3d 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 20  j>=pColumn->nId 
269c4 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
269c5 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
269c6 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d  e, pTab->aCol[i]
269c7 2e 70 44 66 6c 74 2c 20 72 65 67 43 6f 6c 73 2b  .pDflt, regCols+
269c8 69 2b 31 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  i+1);.      }els
269c9 65 20 69 66 28 20 75 73 65 54 65 6d 70 54 61 62  e if( useTempTab
269ca 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  le ){.        sq
269cb 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
269cc 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73 72  v, OP_Column, sr
269cd 63 54 61 62 2c 20 6a 2c 20 72 65 67 43 6f 6c 73  cTab, j, regCols
269ce 2b 69 2b 31 29 3b 20 0a 20 20 20 20 20 20 7d 65  +i+1); .      }e
269cf 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73  lse{.        ass
269d0 65 72 74 28 20 70 53 65 6c 65 63 74 3d 3d 30 20  ert( pSelect==0 
269d1 29 3b 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20  ); /* Otherwise 
269d2 75 73 65 54 65 6d 70 54 61 62 6c 65 20 69 73 20  useTempTable is 
269d3 74 72 75 65 20 2a 2f 0a 20 20 20 20 20 20 20 20  true */.        
269d4 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 41  sqlite3ExprCodeA
269d5 6e 64 43 61 63 68 65 28 70 50 61 72 73 65 2c 20  ndCache(pParse, 
269d6 70 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 45 78 70  pList->a[j].pExp
269d7 72 2c 20 72 65 67 43 6f 6c 73 2b 69 2b 31 29 3b  r, regCols+i+1);
269d8 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
269d9 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69      /* If this i
269da 73 20 61 6e 20 49 4e 53 45 52 54 20 6f 6e 20 61  s an INSERT on a
269db 20 76 69 65 77 20 77 69 74 68 20 61 6e 20 49 4e   view with an IN
269dc 53 54 45 41 44 20 4f 46 20 49 4e 53 45 52 54 20  STEAD OF INSERT 
269dd 74 72 69 67 67 65 72 2c 0a 20 20 20 20 2a 2a 20  trigger,.    ** 
269de 64 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 61  do not attempt a
269df 6e 79 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 62  ny conversions b
269e0 65 66 6f 72 65 20 61 73 73 65 6d 62 6c 69 6e 67  efore assembling
269e1 20 74 68 65 20 72 65 63 6f 72 64 2e 0a 20 20 20   the record..   
269e2 20 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 61   ** If this is a
269e3 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 61 74 74   real table, att
269e4 65 6d 70 74 20 63 6f 6e 76 65 72 73 69 6f 6e 73  empt conversions
269e5 20 61 73 20 72 65 71 75 69 72 65 64 20 62 79 20   as required by 
269e6 74 68 65 0a 20 20 20 20 2a 2a 20 74 61 62 6c 65  the.    ** table
269e7 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 69   column affiniti
269e8 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  es..    */.    i
269e9 66 28 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20  f( !isView ){.  
269ea 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
269eb 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 66 66 69  ddOp2(v, OP_Affi
269ec 6e 69 74 79 2c 20 72 65 67 43 6f 6c 73 2b 31 2c  nity, regCols+1,
269ed 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20   pTab->nCol);.  
269ee 20 20 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65      sqlite3Table
269ef 41 66 66 69 6e 69 74 79 53 74 72 28 76 2c 20 70  AffinityStr(v, p
269f0 54 61 62 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  Tab);.    }..   
269f1 20 2f 2a 20 46 69 72 65 20 42 45 46 4f 52 45 20   /* Fire BEFORE 
269f2 6f 72 20 49 4e 53 54 45 41 44 20 4f 46 20 74 72  or INSTEAD OF tr
269f3 69 67 67 65 72 73 20 2a 2f 0a 20 20 20 20 73 71  iggers */.    sq
269f4 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67  lite3CodeRowTrig
269f5 67 65 72 28 70 50 61 72 73 65 2c 20 70 54 72 69  ger(pParse, pTri
269f6 67 67 65 72 2c 20 54 4b 5f 49 4e 53 45 52 54 2c  gger, TK_INSERT,
269f7 20 30 2c 20 54 52 49 47 47 45 52 5f 42 45 46 4f   0, TRIGGER_BEFO
269f8 52 45 2c 20 0a 20 20 20 20 20 20 20 20 70 54 61  RE, .        pTa
269f9 62 2c 20 2d 31 2c 20 72 65 67 43 6f 6c 73 2d 70  b, -1, regCols-p
269fa 54 61 62 2d 3e 6e 43 6f 6c 2d 31 2c 20 6f 6e 45  Tab->nCol-1, onE
269fb 72 72 6f 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29  rror, endOfLoop)
269fc 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65  ;..    sqlite3Re
269fd 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70  leaseTempRange(p
269fe 50 61 72 73 65 2c 20 72 65 67 43 6f 6c 73 2c 20  Parse, regCols, 
269ff 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 29 3b 0a 20  pTab->nCol+1);. 
26a00 20 7d 0a 0a 20 20 2f 2a 20 50 75 73 68 20 74 68   }..  /* Push th
26a01 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20  e record number 
26a02 66 6f 72 20 74 68 65 20 6e 65 77 20 65 6e 74 72  for the new entr
26a03 79 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b  y onto the stack
26a04 2e 20 20 54 68 65 0a 20 20 2a 2a 20 72 65 63 6f  .  The.  ** reco
26a05 72 64 20 6e 75 6d 62 65 72 20 69 73 20 61 20 72  rd number is a r
26a06 61 6e 64 6f 6d 6c 79 20 67 65 6e 65 72 61 74 65  andomly generate
26a07 20 69 6e 74 65 67 65 72 20 63 72 65 61 74 65 64   integer created
26a08 20 62 79 20 4e 65 77 52 6f 77 69 64 0a 20 20 2a   by NewRowid.  *
26a09 2a 20 65 78 63 65 70 74 20 77 68 65 6e 20 74 68  * except when th
26a0a 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e 20 49  e table has an I
26a0b 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
26a0c 45 59 20 63 6f 6c 75 6d 6e 2c 20 69 6e 20 77 68  EY column, in wh
26a0d 69 63 68 0a 20 20 2a 2a 20 63 61 73 65 20 74 68  ich.  ** case th
26a0e 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20  e record number 
26a0f 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74  is the same as t
26a10 68 61 74 20 63 6f 6c 75 6d 6e 2e 20 0a 20 20 2a  hat column. .  *
26a11 2f 0a 20 20 69 66 28 20 21 69 73 56 69 65 77 20  /.  if( !isView 
26a12 29 7b 0a 20 20 20 20 69 66 28 20 49 73 56 69 72  ){.    if( IsVir
26a13 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20  tual(pTab) ){.  
26a14 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 20 74      /* The row t
26a15 68 61 74 20 74 68 65 20 56 55 70 64 61 74 65 20  hat the VUpdate 
26a16 6f 70 63 6f 64 65 20 77 69 6c 6c 20 64 65 6c 65  opcode will dele
26a17 74 65 3a 20 6e 6f 6e 65 20 2a 2f 0a 20 20 20 20  te: none */.    
26a18 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
26a19 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20  Op2(v, OP_Null, 
26a1a 30 2c 20 72 65 67 49 6e 73 29 3b 0a 20 20 20 20  0, regIns);.    
26a1b 7d 0a 20 20 20 20 69 66 28 20 6b 65 79 43 6f 6c  }.    if( keyCol
26a1c 75 6d 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20  umn>=0 ){.      
26a1d 69 66 28 20 75 73 65 54 65 6d 70 54 61 62 6c 65  if( useTempTable
26a1e 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
26a1f 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
26a20 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73 72 63 54   OP_Column, srcT
26a21 61 62 2c 20 6b 65 79 43 6f 6c 75 6d 6e 2c 20 72  ab, keyColumn, r
26a22 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20  egRowid);.      
26a23 7d 65 6c 73 65 20 69 66 28 20 70 53 65 6c 65 63  }else if( pSelec
26a24 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  t ){.        sql
26a25 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
26a26 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67 46  , OP_SCopy, regF
26a27 72 6f 6d 53 65 6c 65 63 74 2b 6b 65 79 43 6f 6c  romSelect+keyCol
26a28 75 6d 6e 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a  umn, regRowid);.
26a29 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
26a2a 20 20 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 70       VdbeOp *pOp
26a2b 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
26a2c 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
26a2d 2c 20 70 4c 69 73 74 2d 3e 61 5b 6b 65 79 43 6f  , pList->a[keyCo
26a2e 6c 75 6d 6e 5d 2e 70 45 78 70 72 2c 20 72 65 67  lumn].pExpr, reg
26a2f 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 20 20  Rowid);.        
26a30 70 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62  pOp = sqlite3Vdb
26a31 65 47 65 74 4f 70 28 76 2c 20 2d 31 29 3b 0a 20  eGetOp(v, -1);. 
26a32 20 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 59         if( ALWAY
26a33 53 28 70 4f 70 29 20 26 26 20 70 4f 70 2d 3e 6f  S(pOp) && pOp->o
26a34 70 63 6f 64 65 3d 3d 4f 50 5f 4e 75 6c 6c 20 26  pcode==OP_Null &
26a35 26 20 21 49 73 56 69 72 74 75 61 6c 28 70 54 61  & !IsVirtual(pTa
26a36 62 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  b) ){.          
26a37 61 70 70 65 6e 64 46 6c 61 67 20 3d 20 31 3b 0a  appendFlag = 1;.
26a38 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 6f            pOp->o
26a39 70 63 6f 64 65 20 3d 20 4f 50 5f 4e 65 77 52 6f  pcode = OP_NewRo
26a3a 77 69 64 3b 0a 20 20 20 20 20 20 20 20 20 20 70  wid;.          p
26a3b 4f 70 2d 3e 70 31 20 3d 20 62 61 73 65 43 75 72  Op->p1 = baseCur
26a3c 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d  ;.          pOp-
26a3d 3e 70 32 20 3d 20 72 65 67 52 6f 77 69 64 3b 0a  >p2 = regRowid;.
26a3e 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 70            pOp->p
26a3f 33 20 3d 20 72 65 67 41 75 74 6f 69 6e 63 3b 0a  3 = regAutoinc;.
26a40 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
26a41 7d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68  }.      /* If th
26a42 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 65 78  e PRIMARY KEY ex
26a43 70 72 65 73 73 69 6f 6e 20 69 73 20 4e 55 4c 4c  pression is NULL
26a44 2c 20 74 68 65 6e 20 75 73 65 20 4f 50 5f 4e 65  , then use OP_Ne
26a45 77 52 6f 77 69 64 0a 20 20 20 20 20 20 2a 2a 20  wRowid.      ** 
26a46 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20 75 6e  to generate a un
26a47 69 71 75 65 20 70 72 69 6d 61 72 79 20 6b 65 79  ique primary key
26a48 20 76 61 6c 75 65 2e 0a 20 20 20 20 20 20 2a 2f   value..      */
26a49 0a 20 20 20 20 20 20 69 66 28 20 21 61 70 70 65  .      if( !appe
26a4a 6e 64 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20  ndFlag ){.      
26a4b 20 20 69 6e 74 20 6a 31 3b 0a 20 20 20 20 20 20    int j1;.      
26a4c 20 20 69 66 28 20 21 49 73 56 69 72 74 75 61 6c    if( !IsVirtual
26a4d 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20  (pTab) ){.      
26a4e 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33      j1 = sqlite3
26a4f 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
26a50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 65 67 52 6f 77  _NotNull, regRow
26a51 69 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  id);.          s
26a52 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
26a53 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c  (v, OP_NewRowid,
26a54 20 62 61 73 65 43 75 72 2c 20 72 65 67 52 6f 77   baseCur, regRow
26a55 69 64 2c 20 72 65 67 41 75 74 6f 69 6e 63 29 3b  id, regAutoinc);
26a56 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
26a57 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
26a58 2c 20 6a 31 29 3b 0a 20 20 20 20 20 20 20 20 7d  , j1);.        }
26a59 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
26a5a 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j1 = sqlite3Vdbe
26a5b 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a  CurrentAddr(v);.
26a5c 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
26a5d 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
26a5e 50 5f 49 73 4e 75 6c 6c 2c 20 72 65 67 52 6f 77  P_IsNull, regRow
26a5f 69 64 2c 20 6a 31 2b 32 29 3b 0a 20 20 20 20 20  id, j1+2);.     
26a60 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c     }.        sql
26a61 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
26a62 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20  , OP_MustBeInt, 
26a63 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20  regRowid);.     
26a64 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28   }.    }else if(
26a65 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
26a66 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
26a67 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
26a68 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 52 6f  P_Null, 0, regRo
26a69 77 69 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  wid);.    }else{
26a6a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
26a6b 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e  beAddOp3(v, OP_N
26a6c 65 77 52 6f 77 69 64 2c 20 62 61 73 65 43 75 72  ewRowid, baseCur
26a6d 2c 20 72 65 67 52 6f 77 69 64 2c 20 72 65 67 41  , regRowid, regA
26a6e 75 74 6f 69 6e 63 29 3b 0a 20 20 20 20 20 20 61  utoinc);.      a
26a6f 70 70 65 6e 64 46 6c 61 67 20 3d 20 31 3b 0a 20  ppendFlag = 1;. 
26a70 20 20 20 7d 0a 20 20 20 20 61 75 74 6f 49 6e 63     }.    autoInc
26a71 53 74 65 70 28 70 50 61 72 73 65 2c 20 72 65 67  Step(pParse, reg
26a72 41 75 74 6f 69 6e 63 2c 20 72 65 67 52 6f 77 69  Autoinc, regRowi
26a73 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 50 75 73 68  d);..    /* Push
26a74 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 2c   onto the stack,
26a75 20 64 61 74 61 20 66 6f 72 20 61 6c 6c 20 63 6f   data for all co
26a76 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 6e 65 77  lumns of the new
26a77 20 65 6e 74 72 79 2c 20 62 65 67 69 6e 6e 69 6e   entry, beginnin
26a78 67 0a 20 20 20 20 2a 2a 20 77 69 74 68 20 74 68  g.    ** with th
26a79 65 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e 2e 0a  e first column..
26a7a 20 20 20 20 2a 2f 0a 20 20 20 20 6e 48 69 64 64      */.    nHidd
26a7b 65 6e 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28  en = 0;.    for(
26a7c 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f  i=0; i<pTab->nCo
26a7d 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  l; i++){.      i
26a7e 6e 74 20 69 52 65 67 53 74 6f 72 65 20 3d 20 72  nt iRegStore = r
26a7f 65 67 52 6f 77 69 64 2b 31 2b 69 3b 0a 20 20 20  egRowid+1+i;.   
26a80 20 20 20 69 66 28 20 69 3d 3d 70 54 61 62 2d 3e     if( i==pTab->
26a81 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20  iPKey ){.       
26a82 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66   /* The value of
26a83 20 74 68 65 20 49 4e 54 45 47 45 52 20 50 52 49   the INTEGER PRI
26a84 4d 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e 20  MARY KEY column 
26a85 69 73 20 61 6c 77 61 79 73 20 61 20 4e 55 4c 4c  is always a NULL
26a86 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 57 68 65  ..        ** Whe
26a87 6e 65 76 65 72 20 74 68 69 73 20 63 6f 6c 75 6d  never this colum
26a88 6e 20 69 73 20 72 65 61 64 2c 20 74 68 65 20 72  n is read, the r
26a89 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 77 69 6c  ecord number wil
26a8a 6c 20 62 65 20 73 75 62 73 74 69 74 75 74 65 64  l be substituted
26a8b 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 20 69  .        ** in i
26a8c 74 73 20 70 6c 61 63 65 2e 20 20 53 6f 20 77 69  ts place.  So wi
26a8d 6c 6c 20 66 69 6c 6c 20 74 68 69 73 20 63 6f 6c  ll fill this col
26a8e 75 6d 6e 20 77 69 74 68 20 61 20 4e 55 4c 4c 20  umn with a NULL 
26a8f 74 6f 20 61 76 6f 69 64 0a 20 20 20 20 20 20 20  to avoid.       
26a90 20 2a 2a 20 74 61 6b 69 6e 67 20 75 70 20 64 61   ** taking up da
26a91 74 61 20 73 70 61 63 65 20 77 69 74 68 20 69 6e  ta space with in
26a92 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74 20 77  formation that w
26a93 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 75 73 65  ill never be use
26a94 64 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71  d. */.        sq
26a95 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
26a96 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 69  v, OP_Null, 0, i
26a97 52 65 67 53 74 6f 72 65 29 3b 0a 20 20 20 20 20  RegStore);.     
26a98 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
26a99 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70     }.      if( p
26a9a 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20  Column==0 ){.   
26a9b 20 20 20 20 20 69 66 28 20 49 73 48 69 64 64 65       if( IsHidde
26a9c 6e 43 6f 6c 75 6d 6e 28 26 70 54 61 62 2d 3e 61  nColumn(&pTab->a
26a9d 43 6f 6c 5b 69 5d 29 20 29 7b 0a 20 20 20 20 20  Col[i]) ){.     
26a9e 20 20 20 20 20 61 73 73 65 72 74 28 20 49 73 56       assert( IsV
26a9f 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 3b 0a  irtual(pTab) );.
26aa0 20 20 20 20 20 20 20 20 20 20 6a 20 3d 20 2d 31            j = -1
26aa1 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 48 69 64  ;.          nHid
26aa2 64 65 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d  den++;.        }
26aa3 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
26aa4 6a 20 3d 20 69 20 2d 20 6e 48 69 64 64 65 6e 3b  j = i - nHidden;
26aa5 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
26aa6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
26aa7 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 43 6f 6c 75  for(j=0; j<pColu
26aa8 6d 6e 2d 3e 6e 49 64 3b 20 6a 2b 2b 29 7b 0a 20  mn->nId; j++){. 
26aa9 20 20 20 20 20 20 20 20 20 69 66 28 20 70 43 6f           if( pCo
26aaa 6c 75 6d 6e 2d 3e 61 5b 6a 5d 2e 69 64 78 3d 3d  lumn->a[j].idx==
26aab 69 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  i ) break;.     
26aac 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
26aad 20 20 20 69 66 28 20 6a 3c 30 20 7c 7c 20 6e 43     if( j<0 || nC
26aae 6f 6c 75 6d 6e 3d 3d 30 20 7c 7c 20 28 70 43 6f  olumn==0 || (pCo
26aaf 6c 75 6d 6e 20 26 26 20 6a 3e 3d 70 43 6f 6c 75  lumn && j>=pColu
26ab0 6d 6e 2d 3e 6e 49 64 29 20 29 7b 0a 20 20 20 20  mn->nId) ){.    
26ab1 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
26ab2 6f 64 65 28 70 50 61 72 73 65 2c 20 70 54 61 62  ode(pParse, pTab
26ab3 2d 3e 61 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74 2c  ->aCol[i].pDflt,
26ab4 20 69 52 65 67 53 74 6f 72 65 29 3b 0a 20 20 20   iRegStore);.   
26ab5 20 20 20 7d 65 6c 73 65 20 69 66 28 20 75 73 65     }else if( use
26ab6 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20  TempTable ){.   
26ab7 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
26ab8 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c  AddOp3(v, OP_Col
26ab9 75 6d 6e 2c 20 73 72 63 54 61 62 2c 20 6a 2c 20  umn, srcTab, j, 
26aba 69 52 65 67 53 74 6f 72 65 29 3b 20 0a 20 20 20  iRegStore); .   
26abb 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 53 65     }else if( pSe
26abc 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20  lect ){.        
26abd 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
26abe 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72  2(v, OP_SCopy, r
26abf 65 67 46 72 6f 6d 53 65 6c 65 63 74 2b 6a 2c 20  egFromSelect+j, 
26ac0 69 52 65 67 53 74 6f 72 65 29 3b 0a 20 20 20 20  iRegStore);.    
26ac1 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
26ac2 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
26ac3 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2d 3e  (pParse, pList->
26ac4 61 5b 6a 5d 2e 70 45 78 70 72 2c 20 69 52 65 67  a[j].pExpr, iReg
26ac5 53 74 6f 72 65 29 3b 0a 20 20 20 20 20 20 7d 0a  Store);.      }.
26ac6 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 47 65      }..    /* Ge
26ac7 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 63  nerate code to c
26ac8 68 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73  heck constraints
26ac9 20 61 6e 64 20 67 65 6e 65 72 61 74 65 20 69 6e   and generate in
26aca 64 65 78 20 6b 65 79 73 20 61 6e 64 0a 20 20 20  dex keys and.   
26acb 20 2a 2a 20 64 6f 20 74 68 65 20 69 6e 73 65 72   ** do the inser
26acc 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 23 69 66  tion..    */.#if
26acd 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
26ace 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
26acf 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28    if( IsVirtual(
26ad0 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 63  pTab) ){.      c
26ad1 6f 6e 73 74 20 63 68 61 72 20 2a 70 56 54 61 62  onst char *pVTab
26ad2 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a   = (const char *
26ad3 29 73 71 6c 69 74 65 33 47 65 74 56 54 61 62 6c  )sqlite3GetVTabl
26ad4 65 28 64 62 2c 20 70 54 61 62 29 3b 0a 20 20 20  e(db, pTab);.   
26ad5 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 4d 61     sqlite3VtabMa
26ad6 6b 65 57 72 69 74 61 62 6c 65 28 70 50 61 72 73  keWritable(pPars
26ad7 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 20 20  e, pTab);.      
26ad8 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
26ad9 34 28 76 2c 20 4f 50 5f 56 55 70 64 61 74 65 2c  4(v, OP_VUpdate,
26ada 20 31 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2b 32   1, pTab->nCol+2
26adb 2c 20 72 65 67 49 6e 73 2c 20 70 56 54 61 62 2c  , regIns, pVTab,
26adc 20 50 34 5f 56 54 41 42 29 3b 0a 20 20 20 20 20   P4_VTAB);.     
26add 20 73 71 6c 69 74 65 33 4d 61 79 41 62 6f 72 74   sqlite3MayAbort
26ade 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 7d 65  (pParse);.    }e
26adf 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b  lse.#endif.    {
26ae0 0a 20 20 20 20 20 20 69 6e 74 20 69 73 52 65 70  .      int isRep
26ae1 6c 61 63 65 3b 20 20 20 20 2f 2a 20 53 65 74 20  lace;    /* Set 
26ae2 74 6f 20 74 72 75 65 20 69 66 20 63 6f 6e 73 74  to true if const
26ae3 72 61 69 6e 74 73 20 6d 61 79 20 63 61 75 73 65  raints may cause
26ae4 20 61 20 72 65 70 6c 61 63 65 20 2a 2f 0a 20 20   a replace */.  
26ae5 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72      sqlite3Gener
26ae6 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 68 65  ateConstraintChe
26ae7 63 6b 73 28 70 50 61 72 73 65 2c 20 70 54 61 62  cks(pParse, pTab
26ae8 2c 20 62 61 73 65 43 75 72 2c 20 72 65 67 49 6e  , baseCur, regIn
26ae9 73 2c 20 61 52 65 67 49 64 78 2c 0a 20 20 20 20  s, aRegIdx,.    
26aea 20 20 20 20 20 20 6b 65 79 43 6f 6c 75 6d 6e 3e        keyColumn>
26aeb 3d 30 2c 20 30 2c 20 6f 6e 45 72 72 6f 72 2c 20  =0, 0, onError, 
26aec 65 6e 64 4f 66 4c 6f 6f 70 2c 20 26 69 73 52 65  endOfLoop, &isRe
26aed 70 6c 61 63 65 0a 20 20 20 20 20 20 29 3b 0a 20  place.      );. 
26aee 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6d 70       sqlite3Comp
26aef 6c 65 74 65 49 6e 73 65 72 74 69 6f 6e 28 0a 20  leteInsertion(. 
26af0 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2c           pParse,
26af1 20 70 54 61 62 2c 20 62 61 73 65 43 75 72 2c 20   pTab, baseCur, 
26af2 72 65 67 49 6e 73 2c 20 61 52 65 67 49 64 78 2c  regIns, aRegIdx,
26af3 20 30 2c 20 61 70 70 65 6e 64 46 6c 61 67 2c 20   0, appendFlag, 
26af4 69 73 52 65 70 6c 61 63 65 3d 3d 30 0a 20 20 20  isReplace==0.   
26af5 20 20 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a     );.    }.  }.
26af6 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65  .  /* Update the
26af7 20 63 6f 75 6e 74 20 6f 66 20 72 6f 77 73 20 74   count of rows t
26af8 68 61 74 20 61 72 65 20 69 6e 73 65 72 74 65 64  hat are inserted
26af9 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 64 62 2d  .  */.  if( (db-
26afa 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
26afb 43 6f 75 6e 74 52 6f 77 73 29 21 3d 30 20 29 7b  CountRows)!=0 ){
26afc 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
26afd 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64  AddOp2(v, OP_Add
26afe 49 6d 6d 2c 20 72 65 67 52 6f 77 43 6f 75 6e 74  Imm, regRowCount
26aff 2c 20 31 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  , 1);.  }..  if(
26b00 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20   pTrigger ){.   
26b01 20 2f 2a 20 43 6f 64 65 20 41 46 54 45 52 20 74   /* Code AFTER t
26b02 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 20 20 73  riggers */.    s
26b03 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69  qlite3CodeRowTri
26b04 67 67 65 72 28 70 50 61 72 73 65 2c 20 70 54 72  gger(pParse, pTr
26b05 69 67 67 65 72 2c 20 54 4b 5f 49 4e 53 45 52 54  igger, TK_INSERT
26b06 2c 20 30 2c 20 54 52 49 47 47 45 52 5f 41 46 54  , 0, TRIGGER_AFT
26b07 45 52 2c 20 0a 20 20 20 20 20 20 20 20 70 54 61  ER, .        pTa
26b08 62 2c 20 2d 31 2c 20 72 65 67 44 61 74 61 2d 32  b, -1, regData-2
26b09 2d 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20 6f 6e 45  -pTab->nCol, onE
26b0a 72 72 6f 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29  rror, endOfLoop)
26b0b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20  ;.  }..  /* The 
26b0c 62 6f 74 74 6f 6d 20 6f 66 20 74 68 65 20 6d 61  bottom of the ma
26b0d 69 6e 20 69 6e 73 65 72 74 69 6f 6e 20 6c 6f 6f  in insertion loo
26b0e 70 2c 20 69 66 20 74 68 65 20 64 61 74 61 20 73  p, if the data s
26b0f 6f 75 72 63 65 0a 20 20 2a 2a 20 69 73 20 61 20  ource.  ** is a 
26b10 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
26b11 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
26b12 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
26b13 28 76 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a  (v, endOfLoop);.
26b14 20 20 69 66 28 20 75 73 65 54 65 6d 70 54 61 62    if( useTempTab
26b15 6c 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  le ){.    sqlite
26b16 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
26b17 50 5f 4e 65 78 74 2c 20 73 72 63 54 61 62 2c 20  P_Next, srcTab, 
26b18 61 64 64 72 43 6f 6e 74 29 3b 0a 20 20 20 20 73  addrCont);.    s
26b19 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
26b1a 72 65 28 76 2c 20 61 64 64 72 49 6e 73 54 6f 70  re(v, addrInsTop
26b1b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
26b1c 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43  beAddOp1(v, OP_C
26b1d 6c 6f 73 65 2c 20 73 72 63 54 61 62 29 3b 0a 20  lose, srcTab);. 
26b1e 20 7d 65 6c 73 65 20 69 66 28 20 70 53 65 6c 65   }else if( pSele
26b1f 63 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ct ){.    sqlite
26b20 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
26b21 50 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72 43  P_Goto, 0, addrC
26b22 6f 6e 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ont);.    sqlite
26b23 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
26b24 20 61 64 64 72 49 6e 73 54 6f 70 29 3b 0a 20 20   addrInsTop);.  
26b25 7d 0a 0a 20 20 69 66 28 20 21 49 73 56 69 72 74  }..  if( !IsVirt
26b26 75 61 6c 28 70 54 61 62 29 20 26 26 20 21 69 73  ual(pTab) && !is
26b27 56 69 65 77 20 29 7b 0a 20 20 20 20 2f 2a 20 43  View ){.    /* C
26b28 6c 6f 73 65 20 61 6c 6c 20 74 61 62 6c 65 73 20  lose all tables 
26b29 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 20 20 73 71  opened */.    sq
26b2a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
26b2b 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 62 61 73  v, OP_Close, bas
26b2c 65 43 75 72 29 3b 0a 20 20 20 20 66 6f 72 28 69  eCur);.    for(i
26b2d 64 78 3d 31 2c 20 70 49 64 78 3d 70 54 61 62 2d  dx=1, pIdx=pTab-
26b2e 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70  >pIndex; pIdx; p
26b2f 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c  Idx=pIdx->pNext,
26b30 20 69 64 78 2b 2b 29 7b 0a 20 20 20 20 20 20 73   idx++){.      s
26b31 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
26b32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 64  (v, OP_Close, id
26b33 78 2b 62 61 73 65 43 75 72 29 3b 0a 20 20 20 20  x+baseCur);.    
26b34 7d 0a 20 20 7d 0a 0a 69 6e 73 65 72 74 5f 65 6e  }.  }..insert_en
26b35 64 3a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 74  d:.  /* Update t
26b36 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e  he sqlite_sequen
26b37 63 65 20 74 61 62 6c 65 20 62 79 20 73 74 6f 72  ce table by stor
26b38 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  ing the content 
26b39 6f 66 20 74 68 65 0a 20 20 2a 2a 20 6d 61 78 69  of the.  ** maxi
26b3a 6d 75 6d 20 72 6f 77 69 64 20 63 6f 75 6e 74 65  mum rowid counte
26b3b 72 20 76 61 6c 75 65 73 20 72 65 63 6f 72 64 65  r values recorde
26b3c 64 20 77 68 69 6c 65 20 69 6e 73 65 72 74 69 6e  d while insertin
26b3d 67 20 69 6e 74 6f 0a 20 20 2a 2a 20 61 75 74 6f  g into.  ** auto
26b3e 69 6e 63 72 65 6d 65 6e 74 20 74 61 62 6c 65 73  increment tables
26b3f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61  ..  */.  if( pPa
26b40 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 26  rse->nested==0 &
26b41 26 20 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67  & pParse->pTrigg
26b42 65 72 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20  erTab==0 ){.    
26b43 73 71 6c 69 74 65 33 41 75 74 6f 69 6e 63 72 65  sqlite3Autoincre
26b44 6d 65 6e 74 45 6e 64 28 70 50 61 72 73 65 29 3b  mentEnd(pParse);
26b45 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  .  }..  /*.  ** 
26b46 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
26b47 72 20 6f 66 20 72 6f 77 73 20 69 6e 73 65 72 74  r of rows insert
26b48 65 64 2e 20 49 66 20 74 68 69 73 20 72 6f 75 74  ed. If this rout
26b49 69 6e 65 20 69 73 20 0a 20 20 2a 2a 20 67 65 6e  ine is .  ** gen
26b4a 65 72 61 74 69 6e 67 20 63 6f 64 65 20 62 65 63  erating code bec
26b4b 61 75 73 65 20 6f 66 20 61 20 63 61 6c 6c 20 74  ause of a call t
26b4c 6f 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50  o sqlite3NestedP
26b4d 61 72 73 65 28 29 2c 20 64 6f 20 6e 6f 74 0a 20  arse(), do not. 
26b4e 20 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 63   ** invoke the c
26b4f 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
26b50 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 64 62  ..  */.  if( (db
26b51 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 43  ->flags&SQLITE_C
26b52 6f 75 6e 74 52 6f 77 73 29 20 26 26 20 21 70 50  ountRows) && !pP
26b53 61 72 73 65 2d 3e 6e 65 73 74 65 64 20 26 26 20  arse->nested && 
26b54 21 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65  !pParse->pTrigge
26b55 72 54 61 62 20 29 7b 0a 20 20 20 20 73 71 6c 69  rTab ){.    sqli
26b56 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
26b57 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 72   OP_ResultRow, r
26b58 65 67 52 6f 77 43 6f 75 6e 74 2c 20 31 29 3b 0a  egRowCount, 1);.
26b59 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
26b5a 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b  etNumCols(v, 1);
26b5b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
26b5c 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c  SetColName(v, 0,
26b5d 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
26b5e 72 6f 77 73 20 69 6e 73 65 72 74 65 64 22 2c 20  rows inserted", 
26b5f 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
26b60 20 20 7d 0a 0a 69 6e 73 65 72 74 5f 63 6c 65 61    }..insert_clea
26b61 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 53 72  nup:.  sqlite3Sr
26b62 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  cListDelete(db, 
26b63 70 54 61 62 4c 69 73 74 29 3b 0a 20 20 73 71 6c  pTabList);.  sql
26b64 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
26b65 74 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20  te(db, pList);. 
26b66 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
26b67 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74  lete(db, pSelect
26b68 29 3b 0a 20 20 73 71 6c 69 74 65 33 49 64 4c 69  );.  sqlite3IdLi
26b69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 43 6f  stDelete(db, pCo
26b6a 6c 75 6d 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33  lumn);.  sqlite3
26b6b 44 62 46 72 65 65 28 64 62 2c 20 61 52 65 67 49  DbFree(db, aRegI
26b6c 64 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  dx);.}../*.** Ge
26b6d 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 64  nerate code to d
26b6e 6f 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 68 65  o constraint che
26b6f 63 6b 73 20 70 72 69 6f 72 20 74 6f 20 61 6e 20  cks prior to an 
26b70 49 4e 53 45 52 54 20 6f 72 20 61 6e 20 55 50 44  INSERT or an UPD
26b71 41 54 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69  ATE..**.** The i
26b72 6e 70 75 74 20 69 73 20 61 20 72 61 6e 67 65 20  nput is a range 
26b73 6f 66 20 63 6f 6e 73 65 63 75 74 69 76 65 20 72  of consecutive r
26b74 65 67 69 73 74 65 72 73 20 61 73 20 66 6f 6c 6c  egisters as foll
26b75 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 31 2e  ows:.**.**    1.
26b76 20 20 54 68 65 20 72 6f 77 69 64 20 6f 66 20 74    The rowid of t
26b77 68 65 20 72 6f 77 20 61 66 74 65 72 20 74 68 65  he row after the
26b78 20 75 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20   update..**.**  
26b79 20 20 32 2e 20 20 54 68 65 20 64 61 74 61 20 69    2.  The data i
26b7a 6e 20 74 68 65 20 66 69 72 73 74 20 63 6f 6c 75  n the first colu
26b7b 6d 6e 20 6f 66 20 74 68 65 20 65 6e 74 72 79 20  mn of the entry 
26b7c 61 66 74 65 72 20 74 68 65 20 75 70 64 61 74 65  after the update
26b7d 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 69 2e 20 20 44  ..**.**    i.  D
26b7e 61 74 61 20 66 72 6f 6d 20 6d 69 64 64 6c 65 20  ata from middle 
26b7f 63 6f 6c 75 6d 6e 73 2e 2e 2e 0a 2a 2a 0a 2a 2a  columns....**.**
26b80 20 20 20 20 4e 2e 20 20 54 68 65 20 64 61 74 61      N.  The data
26b81 20 69 6e 20 74 68 65 20 6c 61 73 74 20 63 6f 6c   in the last col
26b82 75 6d 6e 20 6f 66 20 74 68 65 20 65 6e 74 72 79  umn of the entry
26b83 20 61 66 74 65 72 20 74 68 65 20 75 70 64 61 74   after the updat
26b84 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 67  e..**.** The reg
26b85 52 6f 77 69 64 20 70 61 72 61 6d 65 74 65 72 20  Rowid parameter 
26b86 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  is the index of 
26b87 74 68 65 20 72 65 67 69 73 74 65 72 20 63 6f 6e  the register con
26b88 74 61 69 6e 69 6e 67 20 28 31 29 2e 0a 2a 2a 0a  taining (1)..**.
26b89 2a 2a 20 49 66 20 69 73 55 70 64 61 74 65 20 69  ** If isUpdate i
26b8a 73 20 74 72 75 65 20 61 6e 64 20 72 6f 77 69 64  s true and rowid
26b8b 43 68 6e 67 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  Chng is non-zero
26b8c 2c 20 74 68 65 6e 20 72 6f 77 69 64 43 68 6e 67  , then rowidChng
26b8d 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65   contains.** the
26b8e 20 61 64 64 72 65 73 73 20 6f 66 20 61 20 72 65   address of a re
26b8f 67 69 73 74 65 72 20 63 6f 6e 74 61 69 6e 69 6e  gister containin
26b90 67 20 74 68 65 20 72 6f 77 69 64 20 62 65 66 6f  g the rowid befo
26b91 72 65 20 74 68 65 20 75 70 64 61 74 65 20 74 61  re the update ta
26b92 6b 65 73 0a 2a 2a 20 70 6c 61 63 65 2e 20 69 73  kes.** place. is
26b93 55 70 64 61 74 65 20 69 73 20 74 72 75 65 20 66  Update is true f
26b94 6f 72 20 55 50 44 41 54 45 73 20 61 6e 64 20 66  or UPDATEs and f
26b95 61 6c 73 65 20 66 6f 72 20 49 4e 53 45 52 54 73  alse for INSERTs
26b96 2e 20 49 66 20 69 73 55 70 64 61 74 65 0a 2a 2a  . If isUpdate.**
26b97 20 69 73 20 66 61 6c 73 65 2c 20 69 6e 64 69 63   is false, indic
26b98 61 74 69 6e 67 20 61 6e 20 49 4e 53 45 52 54 20  ating an INSERT 
26b99 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20  statement, then 
26b9a 61 20 6e 6f 6e 2d 7a 65 72 6f 20 72 6f 77 69 64  a non-zero rowid
26b9b 43 68 6e 67 20 0a 2a 2a 20 69 6e 64 69 63 61 74  Chng .** indicat
26b9c 65 73 20 74 68 61 74 20 74 68 65 20 72 6f 77 69  es that the rowi
26b9d 64 20 77 61 73 20 65 78 70 6c 69 63 69 74 6c 79  d was explicitly
26b9e 20 73 70 65 63 69 66 69 65 64 20 61 73 20 70 61   specified as pa
26b9f 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 49 4e 53  rt of the.** INS
26ba0 45 52 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 49  ERT statement. I
26ba1 66 20 72 6f 77 69 64 43 68 6e 67 20 69 73 20 66  f rowidChng is f
26ba2 61 6c 73 65 2c 20 69 74 20 6d 65 61 6e 73 20 74  alse, it means t
26ba3 68 61 74 20 20 74 68 65 20 72 6f 77 69 64 20 69  hat  the rowid i
26ba4 73 0a 2a 2a 20 63 6f 6d 70 75 74 65 64 20 61 75  s.** computed au
26ba5 74 6f 6d 61 74 69 63 61 6c 6c 79 20 69 6e 20 61  tomatically in a
26ba6 6e 20 69 6e 73 65 72 74 20 6f 72 20 74 68 61 74  n insert or that
26ba7 20 74 68 65 20 72 6f 77 69 64 20 76 61 6c 75 65   the rowid value
26ba8 20 69 73 20 6e 6f 74 20 0a 2a 2a 20 6d 6f 64 69   is not .** modi
26ba9 66 69 65 64 20 62 79 20 61 6e 20 75 70 64 61 74  fied by an updat
26baa 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64  e..**.** The cod
26bab 65 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 74  e generated by t
26bac 68 69 73 20 72 6f 75 74 69 6e 65 20 73 74 6f 72  his routine stor
26bad 65 20 6e 65 77 20 69 6e 64 65 78 20 65 6e 74 72  e new index entr
26bae 69 65 73 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69  ies into.** regi
26baf 73 74 65 72 73 20 69 64 65 6e 74 69 66 69 65 64  sters identified
26bb0 20 62 79 20 61 52 65 67 49 64 78 5b 5d 2e 20 20   by aRegIdx[].  
26bb1 4e 6f 20 69 6e 64 65 78 20 65 6e 74 72 79 20 69  No index entry i
26bb2 73 20 63 72 65 61 74 65 64 20 66 6f 72 0a 2a 2a  s created for.**
26bb3 20 69 6e 64 69 63 65 73 20 77 68 65 72 65 20 61   indices where a
26bb4 52 65 67 49 64 78 5b 69 5d 3d 3d 30 2e 20 20 54  RegIdx[i]==0.  T
26bb5 68 65 20 6f 72 64 65 72 20 6f 66 20 69 6e 64 69  he order of indi
26bb6 63 65 73 20 69 6e 20 61 52 65 67 49 64 78 5b 5d  ces in aRegIdx[]
26bb7 20 69 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20   is.** the same 
26bb8 61 73 20 74 68 65 20 6f 72 64 65 72 20 6f 66 20  as the order of 
26bb9 69 6e 64 69 63 65 73 20 6f 6e 20 74 68 65 20 6c  indices on the l
26bba 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 69 6e  inked list of in
26bbb 64 69 63 65 73 0a 2a 2a 20 61 74 74 61 63 68 65  dices.** attache
26bbc 64 20 74 6f 20 74 68 65 20 74 61 62 6c 65 2e 0a  d to the table..
26bbd 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
26bbe 6e 65 20 61 6c 73 6f 20 67 65 6e 65 72 61 74 65  ne also generate
26bbf 73 20 63 6f 64 65 20 74 6f 20 63 68 65 63 6b 20  s code to check 
26bc0 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 20 4e 4f  constraints.  NO
26bc1 54 20 4e 55 4c 4c 2c 0a 2a 2a 20 43 48 45 43 4b  T NULL,.** CHECK
26bc2 2c 20 61 6e 64 20 55 4e 49 51 55 45 20 63 6f 6e  , and UNIQUE con
26bc3 73 74 72 61 69 6e 74 73 20 61 72 65 20 61 6c 6c  straints are all
26bc4 20 63 68 65 63 6b 65 64 2e 20 20 49 66 20 61 20   checked.  If a 
26bc5 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 73  constraint fails
26bc6 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 61 70  ,.** then the ap
26bc7 70 72 6f 70 72 69 61 74 65 20 61 63 74 69 6f 6e  propriate action
26bc8 20 69 73 20 70 65 72 66 6f 72 6d 65 64 2e 20 20   is performed.  
26bc9 54 68 65 72 65 20 61 72 65 20 66 69 76 65 20 70  There are five p
26bca 6f 73 73 69 62 6c 65 0a 2a 2a 20 61 63 74 69 6f  ossible.** actio
26bcb 6e 73 3a 20 52 4f 4c 4c 42 41 43 4b 2c 20 41 42  ns: ROLLBACK, AB
26bcc 4f 52 54 2c 20 46 41 49 4c 2c 20 52 45 50 4c 41  ORT, FAIL, REPLA
26bcd 43 45 2c 20 61 6e 64 20 49 47 4e 4f 52 45 2e 0a  CE, and IGNORE..
26bce 2a 2a 0a 2a 2a 20 20 43 6f 6e 73 74 72 61 69 6e  **.**  Constrain
26bcf 74 20 74 79 70 65 20 20 41 63 74 69 6f 6e 20 20  t type  Action  
26bd0 20 20 20 20 20 57 68 61 74 20 48 61 70 70 65 6e       What Happen
26bd1 73 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  s.**  ----------
26bd2 2d 2d 2d 2d 2d 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  -----  ---------
26bd3 2d 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  -   ------------
26bd4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26bd5 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20  ------------.** 
26bd6 20 61 6e 79 20 20 20 20 20 20 20 20 20 20 20 20   any            
26bd7 20 20 52 4f 4c 4c 42 41 43 4b 20 20 20 20 20 54    ROLLBACK     T
26bd8 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73  he current trans
26bd9 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64  action is rolled
26bda 20 62 61 63 6b 20 61 6e 64 0a 2a 2a 20 20 20 20   back and.**    
26bdb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26bdc 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
26bdd 74 65 33 5f 65 78 65 63 28 29 20 72 65 74 75 72  te3_exec() retur
26bde 6e 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 77  ns immediately w
26bdf 69 74 68 20 61 0a 2a 2a 20 20 20 20 20 20 20 20  ith a.**        
26be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26be1 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 63          return c
26be2 6f 64 65 20 6f 66 20 53 51 4c 49 54 45 5f 43 4f  ode of SQLITE_CO
26be3 4e 53 54 52 41 49 4e 54 2e 0a 2a 2a 0a 2a 2a 20  NSTRAINT..**.** 
26be4 20 61 6e 79 20 20 20 20 20 20 20 20 20 20 20 20   any            
26be5 20 20 41 42 4f 52 54 20 20 20 20 20 20 20 20 42    ABORT        B
26be6 61 63 6b 20 6f 75 74 20 63 68 61 6e 67 65 73 20  ack out changes 
26be7 66 72 6f 6d 20 74 68 65 20 63 75 72 72 65 6e 74  from the current
26be8 20 63 6f 6d 6d 61 6e 64 0a 2a 2a 20 20 20 20 20   command.**     
26be9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26bea 20 20 20 20 20 20 20 20 20 20 20 6f 6e 6c 79 20             only 
26beb 28 64 6f 20 6e 6f 74 20 64 6f 20 61 20 63 6f 6d  (do not do a com
26bec 70 6c 65 74 65 20 72 6f 6c 6c 62 61 63 6b 29 20  plete rollback) 
26bed 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  then.**         
26bee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26bef 20 20 20 20 20 20 20 63 61 75 73 65 20 73 71 6c         cause sql
26bf0 69 74 65 33 5f 65 78 65 63 28 29 20 74 6f 20 72  ite3_exec() to r
26bf1 65 74 75 72 6e 20 69 6d 6d 65 64 69 61 74 65 6c  eturn immediatel
26bf2 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  y.**            
26bf3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26bf4 20 20 20 20 77 69 74 68 20 53 51 4c 49 54 45 5f      with SQLITE_
26bf5 43 4f 4e 53 54 52 41 49 4e 54 2e 0a 2a 2a 0a 2a  CONSTRAINT..**.*
26bf6 2a 20 20 61 6e 79 20 20 20 20 20 20 20 20 20 20  *  any          
26bf7 20 20 20 20 46 41 49 4c 20 20 20 20 20 20 20 20      FAIL        
26bf8 20 53 71 6c 69 74 65 5f 65 78 65 63 28 29 20 72   Sqlite_exec() r
26bf9 65 74 75 72 6e 73 20 69 6d 6d 65 64 69 61 74 65  eturns immediate
26bfa 6c 79 20 77 69 74 68 20 61 0a 2a 2a 20 20 20 20  ly with a.**    
26bfb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26bfc 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75              retu
26bfd 72 6e 20 63 6f 64 65 20 6f 66 20 53 51 4c 49 54  rn code of SQLIT
26bfe 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2e 20 20 54  E_CONSTRAINT.  T
26bff 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  he.**           
26c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26c01 20 20 20 20 20 74 72 61 6e 73 61 63 74 69 6f 6e       transaction
26c02 20 69 73 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 62   is not rolled b
26c03 61 63 6b 20 61 6e 64 20 61 6e 79 0a 2a 2a 20 20  ack and any.**  
26c04 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26c05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 72                pr
26c06 69 6f 72 20 63 68 61 6e 67 65 73 20 61 72 65 20  ior changes are 
26c07 72 65 74 61 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  retained..**.** 
26c08 20 61 6e 79 20 20 20 20 20 20 20 20 20 20 20 20   any            
26c09 20 20 49 47 4e 4f 52 45 20 20 20 20 20 20 20 54    IGNORE       T
26c0a 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  he record number
26c0b 20 61 6e 64 20 64 61 74 61 20 69 73 20 70 6f 70   and data is pop
26c0c 70 65 64 20 66 72 6f 6d 0a 2a 2a 20 20 20 20 20  ped from.**     
26c0d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26c0e 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 73             the s
26c0f 74 61 63 6b 20 61 6e 64 20 74 68 65 72 65 20 69  tack and there i
26c10 73 20 61 6e 20 69 6d 6d 65 64 69 61 74 65 20 6a  s an immediate j
26c11 75 6d 70 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ump.**          
26c12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26c13 20 20 20 20 20 20 74 6f 20 6c 61 62 65 6c 20 69        to label i
26c14 67 6e 6f 72 65 44 65 73 74 2e 0a 2a 2a 0a 2a 2a  gnoreDest..**.**
26c15 20 20 4e 4f 54 20 4e 55 4c 4c 20 20 20 20 20 20    NOT NULL      
26c16 20 20 20 52 45 50 4c 41 43 45 20 20 20 20 20 20     REPLACE      
26c17 54 68 65 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69  The NULL value i
26c18 73 20 72 65 70 6c 61 63 65 20 62 79 20 74 68 65  s replace by the
26c19 20 64 65 66 61 75 6c 74 0a 2a 2a 20 20 20 20 20   default.**     
26c1a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26c1b 20 20 20 20 20 20 20 20 20 20 20 76 61 6c 75 65             value
26c1c 20 66 6f 72 20 74 68 61 74 20 63 6f 6c 75 6d 6e   for that column
26c1d 2e 20 20 49 66 20 74 68 65 20 64 65 66 61 75 6c  .  If the defaul
26c1e 74 20 76 61 6c 75 65 0a 2a 2a 20 20 20 20 20 20  t value.**      
26c1f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26c20 20 20 20 20 20 20 20 20 20 20 69 73 20 4e 55 4c            is NUL
26c21 4c 2c 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73  L, the action is
26c22 20 74 68 65 20 73 61 6d 65 20 61 73 20 41 42 4f   the same as ABO
26c23 52 54 2e 0a 2a 2a 0a 2a 2a 20 20 55 4e 49 51 55  RT..**.**  UNIQU
26c24 45 20 20 20 20 20 20 20 20 20 20 20 52 45 50 4c  E           REPL
26c25 41 43 45 20 20 20 20 20 20 54 68 65 20 6f 74 68  ACE      The oth
26c26 65 72 20 72 6f 77 20 74 68 61 74 20 63 6f 6e 66  er row that conf
26c27 6c 69 63 74 73 20 77 69 74 68 20 74 68 65 20 72  licts with the r
26c28 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ow.**           
26c29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26c2a 20 20 20 20 20 62 65 69 6e 67 20 69 6e 73 65 72       being inser
26c2b 74 65 64 20 69 73 20 72 65 6d 6f 76 65 64 2e 0a  ted is removed..
26c2c 2a 2a 0a 2a 2a 20 20 43 48 45 43 4b 20 20 20 20  **.**  CHECK    
26c2d 20 20 20 20 20 20 20 20 52 45 50 4c 41 43 45 20          REPLACE 
26c2e 20 20 20 20 20 49 6c 6c 65 67 61 6c 2e 20 20 54       Illegal.  T
26c2f 68 65 20 72 65 73 75 6c 74 73 20 69 6e 20 61 6e  he results in an
26c30 20 65 78 63 65 70 74 69 6f 6e 2e 0a 2a 2a 0a 2a   exception..**.*
26c31 2a 20 57 68 69 63 68 20 61 63 74 69 6f 6e 20 74  * Which action t
26c32 6f 20 74 61 6b 65 20 69 73 20 64 65 74 65 72 6d  o take is determ
26c33 69 6e 65 64 20 62 79 20 74 68 65 20 6f 76 65 72  ined by the over
26c34 72 69 64 65 45 72 72 6f 72 20 70 61 72 61 6d 65  rideError parame
26c35 74 65 72 2e 0a 2a 2a 20 4f 72 20 69 66 20 6f 76  ter..** Or if ov
26c36 65 72 72 69 64 65 45 72 72 6f 72 3d 3d 4f 45 5f  errideError==OE_
26c37 44 65 66 61 75 6c 74 2c 20 74 68 65 6e 20 74 68  Default, then th
26c38 65 20 70 50 61 72 73 65 2d 3e 6f 6e 45 72 72 6f  e pParse->onErro
26c39 72 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 69  r parameter.** i
26c3a 73 20 75 73 65 64 2e 20 20 4f 72 20 69 66 20 70  s used.  Or if p
26c3b 50 61 72 73 65 2d 3e 6f 6e 45 72 72 6f 72 3d 3d  Parse->onError==
26c3c 4f 45 5f 44 65 66 61 75 6c 74 20 74 68 65 6e 20  OE_Default then 
26c3d 74 68 65 20 6f 6e 45 72 72 6f 72 20 76 61 6c 75  the onError valu
26c3e 65 0a 2a 2a 20 66 6f 72 20 74 68 65 20 63 6f 6e  e.** for the con
26c3f 73 74 72 61 69 6e 74 20 69 73 20 75 73 65 64 2e  straint is used.
26c40 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69  .**.** The calli
26c41 6e 67 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20  ng routine must 
26c42 6f 70 65 6e 20 61 20 72 65 61 64 2f 77 72 69 74  open a read/writ
26c43 65 20 63 75 72 73 6f 72 20 66 6f 72 20 70 54 61  e cursor for pTa
26c44 62 20 77 69 74 68 0a 2a 2a 20 63 75 72 73 6f 72  b with.** cursor
26c45 20 6e 75 6d 62 65 72 20 22 62 61 73 65 43 75 72   number "baseCur
26c46 22 2e 20 20 41 6c 6c 20 69 6e 64 69 63 65 73 20  ".  All indices 
26c47 6f 66 20 70 54 61 62 20 6d 75 73 74 20 61 6c 73  of pTab must als
26c48 6f 20 68 61 76 65 20 6f 70 65 6e 0a 2a 2a 20 72  o have open.** r
26c49 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72  ead/write cursor
26c4a 73 20 77 69 74 68 20 63 75 72 73 6f 72 20 6e 75  s with cursor nu
26c4b 6d 62 65 72 20 62 61 73 65 43 75 72 2b 69 20 66  mber baseCur+i f
26c4c 6f 72 20 74 68 65 20 69 2d 74 68 20 63 75 72 73  or the i-th curs
26c4d 6f 72 2e 0a 2a 2a 20 45 78 63 65 70 74 2c 20 69  or..** Except, i
26c4e 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 70 6f  f there is no po
26c4f 73 73 69 62 69 6c 69 74 79 20 6f 66 20 61 20 52  ssibility of a R
26c50 45 50 4c 41 43 45 20 61 63 74 69 6f 6e 20 74 68  EPLACE action th
26c51 65 6e 0a 2a 2a 20 63 75 72 73 6f 72 73 20 64 6f  en.** cursors do
26c52 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20   not need to be 
26c53 6f 70 65 6e 20 66 6f 72 20 69 6e 64 69 63 65 73  open for indices
26c54 20 77 68 65 72 65 20 61 52 65 67 49 64 78 5b 69   where aRegIdx[i
26c55 5d 3d 3d 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ]==0..*/.SQLITE_
26c56 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
26c57 69 74 65 33 47 65 6e 65 72 61 74 65 43 6f 6e 73  ite3GenerateCons
26c58 74 72 61 69 6e 74 43 68 65 63 6b 73 28 0a 20 20  traintChecks(.  
26c59 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
26c5a 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65      /* The parse
26c5b 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54  r context */.  T
26c5c 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20  able *pTab,     
26c5d 20 20 20 2f 2a 20 74 68 65 20 74 61 62 6c 65 20     /* the table 
26c5e 69 6e 74 6f 20 77 68 69 63 68 20 77 65 20 61 72  into which we ar
26c5f 65 20 69 6e 73 65 72 74 69 6e 67 20 2a 2f 0a 20  e inserting */. 
26c60 20 69 6e 74 20 62 61 73 65 43 75 72 2c 20 20 20   int baseCur,   
26c61 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
26c62 20 61 20 72 65 61 64 2f 77 72 69 74 65 20 63 75   a read/write cu
26c63 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 61 74  rsor pointing at
26c64 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20 72   pTab */.  int r
26c65 65 67 52 6f 77 69 64 2c 20 20 20 20 20 20 20 2f  egRowid,       /
26c66 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 72  * Index of the r
26c67 61 6e 67 65 20 6f 66 20 69 6e 70 75 74 20 72 65  ange of input re
26c68 67 69 73 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74  gisters */.  int
26c69 20 2a 61 52 65 67 49 64 78 2c 20 20 20 20 20 20   *aRegIdx,      
26c6a 20 2f 2a 20 52 65 67 69 73 74 65 72 20 75 73 65   /* Register use
26c6b 64 20 62 79 20 65 61 63 68 20 69 6e 64 65 78 2e  d by each index.
26c6c 20 20 30 20 66 6f 72 20 75 6e 75 73 65 64 20 69    0 for unused i
26c6d 6e 64 69 63 65 73 20 2a 2f 0a 20 20 69 6e 74 20  ndices */.  int 
26c6e 72 6f 77 69 64 43 68 6e 67 2c 20 20 20 20 20 20  rowidChng,      
26c6f 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 72  /* True if the r
26c70 6f 77 69 64 20 6d 69 67 68 74 20 63 6f 6c 6c 69  owid might colli
26c71 64 65 20 77 69 74 68 20 65 78 69 73 74 69 6e 67  de with existing
26c72 20 65 6e 74 72 79 20 2a 2f 0a 20 20 69 6e 74 20   entry */.  int 
26c73 69 73 55 70 64 61 74 65 2c 20 20 20 20 20 20 20  isUpdate,       
26c74 2f 2a 20 54 72 75 65 20 66 6f 72 20 55 50 44 41  /* True for UPDA
26c75 54 45 2c 20 46 61 6c 73 65 20 66 6f 72 20 49 4e  TE, False for IN
26c76 53 45 52 54 20 2a 2f 0a 20 20 69 6e 74 20 6f 76  SERT */.  int ov
26c77 65 72 72 69 64 65 45 72 72 6f 72 2c 20 20 2f 2a  errideError,  /*
26c78 20 4f 76 65 72 72 69 64 65 20 6f 6e 45 72 72 6f   Override onErro
26c79 72 20 74 6f 20 74 68 69 73 20 69 66 20 6e 6f 74  r to this if not
26c7a 20 4f 45 5f 44 65 66 61 75 6c 74 20 2a 2f 0a 20   OE_Default */. 
26c7b 20 69 6e 74 20 69 67 6e 6f 72 65 44 65 73 74 2c   int ignoreDest,
26c7c 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20       /* Jump to 
26c7d 74 68 69 73 20 6c 61 62 65 6c 20 6f 6e 20 61 6e  this label on an
26c7e 20 4f 45 5f 49 67 6e 6f 72 65 20 72 65 73 6f 6c   OE_Ignore resol
26c7f 75 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a  ution */.  int *
26c80 70 62 4d 61 79 52 65 70 6c 61 63 65 20 20 20 2f  pbMayReplace   /
26c81 2a 20 4f 55 54 3a 20 53 65 74 20 74 6f 20 74 72  * OUT: Set to tr
26c82 75 65 20 69 66 20 63 6f 6e 73 74 72 61 69 6e 74  ue if constraint
26c83 20 6d 61 79 20 63 61 75 73 65 20 61 20 72 65 70   may cause a rep
26c84 6c 61 63 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  lace */.){.  int
26c85 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
26c86 20 2f 2a 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* loop counter
26c87 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20   */.  Vdbe *v;  
26c88 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 44 42            /* VDB
26c89 45 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 74  E under construt
26c8a 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f  ion */.  int nCo
26c8b 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  l;           /* 
26c8c 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  Number of column
26c8d 73 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 72  s */.  int onErr
26c8e 6f 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f  or;        /* Co
26c8f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f  nflict resolutio
26c90 6e 20 73 74 72 61 74 65 67 79 20 2a 2f 0a 20 20  n strategy */.  
26c91 69 6e 74 20 6a 31 3b 20 20 20 20 20 20 20 20 20  int j1;         
26c92 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 73 20      /* Addresss 
26c93 6f 66 20 6a 75 6d 70 20 69 6e 73 74 72 75 63 74  of jump instruct
26c94 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6a 32 20  ion */.  int j2 
26c95 3d 20 30 2c 20 6a 33 3b 20 20 20 20 20 2f 2a 20  = 0, j3;     /* 
26c96 41 64 64 72 65 73 73 65 73 20 6f 66 20 6a 75 6d  Addresses of jum
26c97 70 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 2a  p instructions *
26c98 2f 0a 20 20 69 6e 74 20 72 65 67 44 61 74 61 3b  /.  int regData;
26c99 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73          /* Regis
26c9a 74 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 66  ter containing f
26c9b 69 72 73 74 20 64 61 74 61 20 63 6f 6c 75 6d 6e  irst data column
26c9c 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 3b 20   */.  int iCur; 
26c9d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62            /* Tab
26c9e 6c 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  le cursor number
26c9f 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64   */.  Index *pId
26ca0 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f  x;         /* Po
26ca1 69 6e 74 65 72 20 74 6f 20 6f 6e 65 20 6f 66 20  inter to one of 
26ca2 74 68 65 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20  the indices */. 
26ca3 20 69 6e 74 20 73 65 65 6e 52 65 70 6c 61 63 65   int seenReplace
26ca4 20 3d 20 30 3b 20 2f 2a 20 54 72 75 65 20 69 66   = 0; /* True if
26ca5 20 52 45 50 4c 41 43 45 20 69 73 20 75 73 65 64   REPLACE is used
26ca6 20 74 6f 20 72 65 73 6f 6c 76 65 20 49 4e 54 20   to resolve INT 
26ca7 50 4b 20 63 6f 6e 66 6c 69 63 74 20 2a 2f 0a 20  PK conflict */. 
26ca8 20 69 6e 74 20 72 65 67 4f 6c 64 52 6f 77 69 64   int regOldRowid
26ca9 20 3d 20 28 72 6f 77 69 64 43 68 6e 67 20 26 26   = (rowidChng &&
26caa 20 69 73 55 70 64 61 74 65 29 20 3f 20 72 6f 77   isUpdate) ? row
26cab 69 64 43 68 6e 67 20 3a 20 72 65 67 52 6f 77 69  idChng : regRowi
26cac 64 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65  d;..  v = sqlite
26cad 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
26cae 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d 30  ;.  assert( v!=0
26caf 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54   );.  assert( pT
26cb0 61 62 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29  ab->pSelect==0 )
26cb1 3b 20 20 2f 2a 20 54 68 69 73 20 74 61 62 6c 65  ;  /* This table
26cb2 20 69 73 20 6e 6f 74 20 61 20 56 49 45 57 20 2a   is not a VIEW *
26cb3 2f 0a 20 20 6e 43 6f 6c 20 3d 20 70 54 61 62 2d  /.  nCol = pTab-
26cb4 3e 6e 43 6f 6c 3b 0a 20 20 72 65 67 44 61 74 61  >nCol;.  regData
26cb5 20 3d 20 72 65 67 52 6f 77 69 64 20 2b 20 31 3b   = regRowid + 1;
26cb6 0a 0a 20 20 2f 2a 20 54 65 73 74 20 61 6c 6c 20  ..  /* Test all 
26cb7 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61  NOT NULL constra
26cb8 69 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  ints..  */.  for
26cb9 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b  (i=0; i<nCol; i+
26cba 2b 29 7b 0a 20 20 20 20 69 66 28 20 69 3d 3d 70  +){.    if( i==p
26cbb 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20  Tab->iPKey ){.  
26cbc 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20      continue;.  
26cbd 20 20 7d 0a 20 20 20 20 6f 6e 45 72 72 6f 72 20    }.    onError 
26cbe 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e  = pTab->aCol[i].
26cbf 6e 6f 74 4e 75 6c 6c 3b 0a 20 20 20 20 69 66 28  notNull;.    if(
26cc0 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 4e 6f 6e   onError==OE_Non
26cc1 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  e ) continue;.  
26cc2 20 20 69 66 28 20 6f 76 65 72 72 69 64 65 45 72    if( overrideEr
26cc3 72 6f 72 21 3d 4f 45 5f 44 65 66 61 75 6c 74 20  ror!=OE_Default 
26cc4 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f 72  ){.      onError
26cc5 20 3d 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72   = overrideError
26cc6 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
26cc7 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61  onError==OE_Defa
26cc8 75 6c 74 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45  ult ){.      onE
26cc9 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b  rror = OE_Abort;
26cca 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6f  .    }.    if( o
26ccb 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61  nError==OE_Repla
26ccc 63 65 20 26 26 20 70 54 61 62 2d 3e 61 43 6f 6c  ce && pTab->aCol
26ccd 5b 69 5d 2e 70 44 66 6c 74 3d 3d 30 20 29 7b 0a  [i].pDflt==0 ){.
26cce 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20        onError = 
26ccf 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a  OE_Abort;.    }.
26cd0 20 20 20 20 61 73 73 65 72 74 28 20 6f 6e 45 72      assert( onEr
26cd1 72 6f 72 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b  ror==OE_Rollback
26cd2 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f   || onError==OE_
26cd3 41 62 6f 72 74 20 7c 7c 20 6f 6e 45 72 72 6f 72  Abort || onError
26cd4 3d 3d 4f 45 5f 46 61 69 6c 0a 20 20 20 20 20 20  ==OE_Fail.      
26cd5 20 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45    || onError==OE
26cd6 5f 49 67 6e 6f 72 65 20 7c 7c 20 6f 6e 45 72 72  _Ignore || onErr
26cd7 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29  or==OE_Replace )
26cd8 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20 6f 6e  ;.    switch( on
26cd9 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 20 20 63  Error ){.      c
26cda 61 73 65 20 4f 45 5f 41 62 6f 72 74 3a 0a 20 20  ase OE_Abort:.  
26cdb 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 61 79        sqlite3May
26cdc 41 62 6f 72 74 28 70 50 61 72 73 65 29 3b 0a 20  Abort(pParse);. 
26cdd 20 20 20 20 20 63 61 73 65 20 4f 45 5f 52 6f 6c       case OE_Rol
26cde 6c 62 61 63 6b 3a 0a 20 20 20 20 20 20 63 61 73  lback:.      cas
26cdf 65 20 4f 45 5f 46 61 69 6c 3a 20 7b 0a 20 20 20  e OE_Fail: {.   
26ce0 20 20 20 20 20 63 68 61 72 20 2a 7a 4d 73 67 3b       char *zMsg;
26ce1 0a 20 20 20 20 20 20 20 20 6a 31 20 3d 20 73 71  .        j1 = sq
26ce2 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
26ce3 76 2c 20 4f 50 5f 48 61 6c 74 49 66 4e 75 6c 6c  v, OP_HaltIfNull
26ce4 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
26ce5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26ce6 20 20 20 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54      SQLITE_CONST
26ce7 52 41 49 4e 54 2c 20 6f 6e 45 72 72 6f 72 2c 20  RAINT, onError, 
26ce8 72 65 67 44 61 74 61 2b 69 29 3b 0a 20 20 20 20  regData+i);.    
26ce9 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74      zMsg = sqlit
26cea 65 33 4d 50 72 69 6e 74 66 28 70 50 61 72 73 65  e3MPrintf(pParse
26ceb 2d 3e 64 62 2c 20 22 25 73 2e 25 73 20 6d 61 79  ->db, "%s.%s may
26cec 20 6e 6f 74 20 62 65 20 4e 55 4c 4c 22 2c 0a 20   not be NULL",. 
26ced 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26cee 20 20 20 20 20 20 20 20 20 20 20 20 20 70 54 61               pTa
26cef 62 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e  b->zName, pTab->
26cf0 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a  aCol[i].zName);.
26cf1 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
26cf2 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d  dbeChangeP4(v, -
26cf3 31 2c 20 7a 4d 73 67 2c 20 50 34 5f 44 59 4e 41  1, zMsg, P4_DYNA
26cf4 4d 49 43 29 3b 0a 20 20 20 20 20 20 20 20 62 72  MIC);.        br
26cf5 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
26cf6 20 20 20 63 61 73 65 20 4f 45 5f 49 67 6e 6f 72     case OE_Ignor
26cf7 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  e: {.        sql
26cf8 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
26cf9 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65 67  , OP_IsNull, reg
26cfa 44 61 74 61 2b 69 2c 20 69 67 6e 6f 72 65 44 65  Data+i, ignoreDe
26cfb 73 74 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65  st);.        bre
26cfc 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
26cfd 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
26cfe 20 20 20 20 20 61 73 73 65 72 74 28 20 6f 6e 45       assert( onE
26cff 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65  rror==OE_Replace
26d00 20 29 3b 0a 20 20 20 20 20 20 20 20 6a 31 20 3d   );.        j1 =
26d01 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26d02 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c  p1(v, OP_NotNull
26d03 2c 20 72 65 67 44 61 74 61 2b 69 29 3b 0a 20 20  , regData+i);.  
26d04 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
26d05 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 54  rCode(pParse, pT
26d06 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 70 44 66 6c  ab->aCol[i].pDfl
26d07 74 2c 20 72 65 67 44 61 74 61 2b 69 29 3b 0a 20  t, regData+i);. 
26d08 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
26d09 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31  beJumpHere(v, j1
26d0a 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  );.        break
26d0b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
26d0c 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73 74 20 61    }..  /* Test a
26d0d 6c 6c 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61  ll CHECK constra
26d0e 69 6e 74 73 0a 20 20 2a 2f 0a 23 69 66 6e 64 65  ints.  */.#ifnde
26d0f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48  f SQLITE_OMIT_CH
26d10 45 43 4b 0a 20 20 69 66 28 20 70 54 61 62 2d 3e  ECK.  if( pTab->
26d11 70 43 68 65 63 6b 20 26 26 20 28 70 50 61 72 73  pCheck && (pPars
26d12 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53  e->db->flags & S
26d13 51 4c 49 54 45 5f 49 67 6e 6f 72 65 43 68 65 63  QLITE_IgnoreChec
26d14 6b 73 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e  ks)==0 ){.    in
26d15 74 20 61 6c 6c 4f 6b 20 3d 20 73 71 6c 69 74 65  t allOk = sqlite
26d16 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76  3VdbeMakeLabel(v
26d17 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 63  );.    pParse->c
26d18 6b 42 61 73 65 20 3d 20 72 65 67 44 61 74 61 3b  kBase = regData;
26d19 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
26d1a 49 66 54 72 75 65 28 70 50 61 72 73 65 2c 20 70  IfTrue(pParse, p
26d1b 54 61 62 2d 3e 70 43 68 65 63 6b 2c 20 61 6c 6c  Tab->pCheck, all
26d1c 4f 6b 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49  Ok, SQLITE_JUMPI
26d1d 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 6f 6e 45 72  FNULL);.    onEr
26d1e 72 6f 72 20 3d 20 6f 76 65 72 72 69 64 65 45 72  ror = overrideEr
26d1f 72 6f 72 21 3d 4f 45 5f 44 65 66 61 75 6c 74 20  ror!=OE_Default 
26d20 3f 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72 20  ? overrideError 
26d21 3a 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20  : OE_Abort;.    
26d22 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f  if( onError==OE_
26d23 49 67 6e 6f 72 65 20 29 7b 0a 20 20 20 20 20 20  Ignore ){.      
26d24 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
26d25 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
26d26 20 69 67 6e 6f 72 65 44 65 73 74 29 3b 0a 20 20   ignoreDest);.  
26d27 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
26d28 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73 74 72  qlite3HaltConstr
26d29 61 69 6e 74 28 70 50 61 72 73 65 2c 20 6f 6e 45  aint(pParse, onE
26d2a 72 72 6f 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20  rror, 0, 0);.   
26d2b 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64   }.    sqlite3Vd
26d2c 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
26d2d 2c 20 61 6c 6c 4f 6b 29 3b 0a 20 20 7d 0a 23 65  , allOk);.  }.#e
26d2e 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
26d2f 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45  (SQLITE_OMIT_CHE
26d30 43 4b 29 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20  CK) */..  /* If 
26d31 77 65 20 68 61 76 65 20 61 6e 20 49 4e 54 45 47  we have an INTEG
26d32 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20  ER PRIMARY KEY, 
26d33 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 70 72  make sure the pr
26d34 69 6d 61 72 79 20 6b 65 79 0a 20 20 2a 2a 20 6f  imary key.  ** o
26d35 66 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64  f the new record
26d36 20 64 6f 65 73 20 6e 6f 74 20 70 72 65 76 69 6f   does not previo
26d37 75 73 6c 79 20 65 78 69 73 74 2e 20 20 45 78 63  usly exist.  Exc
26d38 65 70 74 2c 20 69 66 20 74 68 69 73 0a 20 20 2a  ept, if this.  *
26d39 2a 20 69 73 20 61 6e 20 55 50 44 41 54 45 20 61  * is an UPDATE a
26d3a 6e 64 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b  nd the primary k
26d3b 65 79 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 69  ey is not changi
26d3c 6e 67 2c 20 74 68 61 74 20 69 73 20 4f 4b 2e 0a  ng, that is OK..
26d3d 20 20 2a 2f 0a 20 20 69 66 28 20 72 6f 77 69 64    */.  if( rowid
26d3e 43 68 6e 67 20 29 7b 0a 20 20 20 20 6f 6e 45 72  Chng ){.    onEr
26d3f 72 6f 72 20 3d 20 70 54 61 62 2d 3e 6b 65 79 43  ror = pTab->keyC
26d40 6f 6e 66 3b 0a 20 20 20 20 69 66 28 20 6f 76 65  onf;.    if( ove
26d41 72 72 69 64 65 45 72 72 6f 72 21 3d 4f 45 5f 44  rrideError!=OE_D
26d42 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20  efault ){.      
26d43 6f 6e 45 72 72 6f 72 20 3d 20 6f 76 65 72 72 69  onError = overri
26d44 64 65 45 72 72 6f 72 3b 0a 20 20 20 20 7d 65 6c  deError;.    }el
26d45 73 65 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d  se if( onError==
26d46 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20 20  OE_Default ){.  
26d47 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45      onError = OE
26d48 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20  _Abort;.    }.  
26d49 20 20 0a 20 20 20 20 69 66 28 20 6f 6e 45 72 72    .    if( onErr
26d4a 6f 72 21 3d 4f 45 5f 52 65 70 6c 61 63 65 20 7c  or!=OE_Replace |
26d4b 7c 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 20 29  | pTab->pIndex )
26d4c 7b 0a 20 20 20 20 20 20 69 66 28 20 69 73 55 70  {.      if( isUp
26d4d 64 61 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20  date ){.        
26d4e 6a 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j2 = sqlite3Vdbe
26d4f 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 45 71 2c  AddOp3(v, OP_Eq,
26d50 20 72 65 67 52 6f 77 69 64 2c 20 30 2c 20 72 6f   regRowid, 0, ro
26d51 77 69 64 43 68 6e 67 29 3b 0a 20 20 20 20 20 20  widChng);.      
26d52 7d 0a 20 20 20 20 20 20 6a 33 20 3d 20 73 71 6c  }.      j3 = sql
26d53 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
26d54 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20  , OP_NotExists, 
26d55 62 61 73 65 43 75 72 2c 20 30 2c 20 72 65 67 52  baseCur, 0, regR
26d56 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 73 77 69  owid);.      swi
26d57 74 63 68 28 20 6f 6e 45 72 72 6f 72 20 29 7b 0a  tch( onError ){.
26d58 20 20 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a          default:
26d59 20 7b 0a 20 20 20 20 20 20 20 20 20 20 6f 6e 45   {.          onE
26d5a 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b  rror = OE_Abort;
26d5b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 61  .          /* Fa
26d5c 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20 74 68 65  ll thru into the
26d5d 20 6e 65 78 74 20 63 61 73 65 20 2a 2f 0a 20 20   next case */.  
26d5e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
26d5f 63 61 73 65 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b  case OE_Rollback
26d60 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 4f  :.        case O
26d61 45 5f 41 62 6f 72 74 3a 0a 20 20 20 20 20 20 20  E_Abort:.       
26d62 20 63 61 73 65 20 4f 45 5f 46 61 69 6c 3a 20 7b   case OE_Fail: {
26d63 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
26d64 65 33 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e 74  e3HaltConstraint
26d65 28 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 50  (.            pP
26d66 61 72 73 65 2c 20 6f 6e 45 72 72 6f 72 2c 20 22  arse, onError, "
26d67 50 52 49 4d 41 52 59 20 4b 45 59 20 6d 75 73 74  PRIMARY KEY must
26d68 20 62 65 20 75 6e 69 71 75 65 22 2c 20 50 34 5f   be unique", P4_
26d69 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20  STATIC);.       
26d6a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
26d6b 20 20 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65    }.        case
26d6c 20 4f 45 5f 52 65 70 6c 61 63 65 3a 20 7b 0a 20   OE_Replace: {. 
26d6d 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74           /* If t
26d6e 68 65 72 65 20 61 72 65 20 44 45 4c 45 54 45 20  here are DELETE 
26d6f 74 72 69 67 67 65 72 73 20 6f 6e 20 74 68 69 73  triggers on this
26d70 20 74 61 62 6c 65 20 61 6e 64 20 74 68 65 0a 20   table and the. 
26d71 20 20 20 20 20 20 20 20 20 2a 2a 20 72 65 63 75           ** recu
26d72 72 73 69 76 65 2d 74 72 69 67 67 65 72 73 20 66  rsive-triggers f
26d73 6c 61 67 20 69 73 20 73 65 74 2c 20 63 61 6c 6c  lag is set, call
26d74 20 47 65 6e 65 72 61 74 65 52 6f 77 44 65 6c 65   GenerateRowDele
26d75 74 65 28 29 20 74 6f 0a 20 20 20 20 20 20 20 20  te() to.        
26d76 20 20 2a 2a 20 72 65 6d 6f 76 65 20 74 68 65 20    ** remove the 
26d77 63 6f 6e 66 6c 69 63 74 69 6e 67 20 72 6f 77 20  conflicting row 
26d78 66 72 6f 6d 20 74 68 65 20 74 68 65 20 74 61 62  from the the tab
26d79 6c 65 2e 20 54 68 69 73 20 77 69 6c 6c 20 66 69  le. This will fi
26d7a 72 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  re.          ** 
26d7b 74 68 65 20 74 72 69 67 67 65 72 73 20 61 6e 64  the triggers and
26d7c 20 72 65 6d 6f 76 65 20 62 6f 74 68 20 74 68 65   remove both the
26d7d 20 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 65 78   table and index
26d7e 20 62 2d 74 72 65 65 20 65 6e 74 72 69 65 73 2e   b-tree entries.
26d7f 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 0a 20 20  .          **.  
26d80 20 20 20 20 20 20 20 20 2a 2a 20 4f 74 68 65 72          ** Other
26d81 77 69 73 65 2c 20 69 66 20 74 68 65 72 65 20 61  wise, if there a
26d82 72 65 20 6e 6f 20 74 72 69 67 67 65 72 73 20 6f  re no triggers o
26d83 72 20 74 68 65 20 72 65 63 75 72 73 69 76 65 2d  r the recursive-
26d84 74 72 69 67 67 65 72 73 0a 20 20 20 20 20 20 20  triggers.       
26d85 20 20 20 2a 2a 20 66 6c 61 67 20 69 73 20 6e 6f     ** flag is no
26d86 74 20 73 65 74 2c 20 63 61 6c 6c 20 47 65 6e 65  t set, call Gene
26d87 72 61 74 65 52 6f 77 49 6e 64 65 78 44 65 6c 65  rateRowIndexDele
26d88 74 65 28 29 2e 20 54 68 69 73 20 72 65 6d 6f 76  te(). This remov
26d89 65 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  es.          ** 
26d8a 74 68 65 20 69 6e 64 65 78 20 62 2d 74 72 65 65  the index b-tree
26d8b 20 65 6e 74 72 69 65 73 20 6f 6e 6c 79 2e 20 54   entries only. T
26d8c 68 65 20 74 61 62 6c 65 20 62 2d 74 72 65 65 20  he table b-tree 
26d8d 65 6e 74 72 79 20 77 69 6c 6c 20 62 65 20 0a 20  entry will be . 
26d8e 20 20 20 20 20 20 20 20 20 2a 2a 20 72 65 70 6c           ** repl
26d8f 61 63 65 64 20 62 79 20 74 68 65 20 6e 65 77 20  aced by the new 
26d90 65 6e 74 72 79 20 77 68 65 6e 20 69 74 20 69 73  entry when it is
26d91 20 69 6e 73 65 72 74 65 64 2e 20 20 2a 2f 0a 20   inserted.  */. 
26d92 20 20 20 20 20 20 20 20 20 54 72 69 67 67 65 72           Trigger
26d93 20 2a 70 54 72 69 67 67 65 72 20 3d 20 30 3b 0a   *pTrigger = 0;.
26d94 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 50            if( pP
26d95 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 26  arse->db->flags&
26d96 53 51 4c 49 54 45 5f 52 65 63 54 72 69 67 67 65  SQLITE_RecTrigge
26d97 72 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  rs ){.          
26d98 20 20 70 54 72 69 67 67 65 72 20 3d 20 73 71 6c    pTrigger = sql
26d99 69 74 65 33 54 72 69 67 67 65 72 73 45 78 69 73  ite3TriggersExis
26d9a 74 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  t(pParse, pTab, 
26d9b 54 4b 5f 44 45 4c 45 54 45 2c 20 30 2c 20 30 29  TK_DELETE, 0, 0)
26d9c 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
26d9d 20 20 20 20 20 20 20 20 69 66 28 20 70 54 72 69          if( pTri
26d9e 67 67 65 72 20 29 7b 0a 20 20 20 20 20 20 20 20  gger ){.        
26d9f 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72      sqlite3Gener
26da0 61 74 65 52 6f 77 44 65 6c 65 74 65 28 0a 20 20  ateRowDelete(.  
26da1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 50                pP
26da2 61 72 73 65 2c 20 70 54 61 62 2c 20 62 61 73 65  arse, pTab, base
26da3 43 75 72 2c 20 72 65 67 52 6f 77 69 64 2c 20 30  Cur, regRowid, 0
26da4 2c 20 70 54 72 69 67 67 65 72 2c 20 4f 45 5f 52  , pTrigger, OE_R
26da5 65 70 6c 61 63 65 0a 20 20 20 20 20 20 20 20 20  eplace.         
26da6 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20     );.          
26da7 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
26da8 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61     sqlite3Genera
26da9 74 65 52 6f 77 49 6e 64 65 78 44 65 6c 65 74 65  teRowIndexDelete
26daa 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 62  (pParse, pTab, b
26dab 61 73 65 43 75 72 2c 20 30 29 3b 0a 20 20 20 20  aseCur, 0);.    
26dac 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
26dad 20 20 73 65 65 6e 52 65 70 6c 61 63 65 20 3d 20    seenReplace = 
26dae 31 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  1;.          bre
26daf 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
26db0 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 49 67        case OE_Ig
26db1 6e 6f 72 65 3a 20 7b 0a 20 20 20 20 20 20 20 20  nore: {.        
26db2 20 20 61 73 73 65 72 74 28 20 73 65 65 6e 52 65    assert( seenRe
26db3 70 6c 61 63 65 3d 3d 30 20 29 3b 0a 20 20 20 20  place==0 );.    
26db4 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
26db5 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
26db6 74 6f 2c 20 30 2c 20 69 67 6e 6f 72 65 44 65 73  to, 0, ignoreDes
26db7 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72  t);.          br
26db8 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
26db9 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
26dba 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
26dbb 28 76 2c 20 6a 33 29 3b 0a 20 20 20 20 20 20 69  (v, j3);.      i
26dbc 66 28 20 69 73 55 70 64 61 74 65 20 29 7b 0a 20  f( isUpdate ){. 
26dbd 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
26dbe 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 32  beJumpHere(v, j2
26dbf 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
26dc0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73 74 20  .  }..  /* Test 
26dc1 61 6c 6c 20 55 4e 49 51 55 45 20 63 6f 6e 73 74  all UNIQUE const
26dc2 72 61 69 6e 74 73 20 62 79 20 63 72 65 61 74 69  raints by creati
26dc3 6e 67 20 65 6e 74 72 69 65 73 20 66 6f 72 20 65  ng entries for e
26dc4 61 63 68 20 55 4e 49 51 55 45 0a 20 20 2a 2a 20  ach UNIQUE.  ** 
26dc5 69 6e 64 65 78 20 61 6e 64 20 6d 61 6b 69 6e 67  index and making
26dc6 20 73 75 72 65 20 74 68 61 74 20 64 75 70 6c 69   sure that dupli
26dc7 63 61 74 65 20 65 6e 74 72 69 65 73 20 64 6f 20  cate entries do 
26dc8 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73  not already exis
26dc9 74 2e 0a 20 20 2a 2a 20 41 64 64 20 74 68 65 20  t..  ** Add the 
26dca 6e 65 77 20 72 65 63 6f 72 64 73 20 74 6f 20 74  new records to t
26dcb 68 65 20 69 6e 64 69 63 65 73 20 61 73 20 77 65  he indices as we
26dcc 20 67 6f 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28   go..  */.  for(
26dcd 69 43 75 72 3d 30 2c 20 70 49 64 78 3d 70 54 61  iCur=0, pIdx=pTa
26dce 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b  b->pIndex; pIdx;
26dcf 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
26dd0 74 2c 20 69 43 75 72 2b 2b 29 7b 0a 20 20 20 20  t, iCur++){.    
26dd1 69 6e 74 20 72 65 67 49 64 78 3b 0a 20 20 20 20  int regIdx;.    
26dd2 69 6e 74 20 72 65 67 52 3b 0a 0a 20 20 20 20 69  int regR;..    i
26dd3 66 28 20 61 52 65 67 49 64 78 5b 69 43 75 72 5d  f( aRegIdx[iCur]
26dd4 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 20  ==0 ) continue; 
26dd5 20 2f 2a 20 53 6b 69 70 20 75 6e 75 73 65 64 20   /* Skip unused 
26dd6 69 6e 64 69 63 65 73 20 2a 2f 0a 0a 20 20 20 20  indices */..    
26dd7 2f 2a 20 43 72 65 61 74 65 20 61 20 6b 65 79 20  /* Create a key 
26dd8 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20 74 68  for accessing th
26dd9 65 20 69 6e 64 65 78 20 65 6e 74 72 79 20 2a 2f  e index entry */
26dda 0a 20 20 20 20 72 65 67 49 64 78 20 3d 20 73 71  .    regIdx = sq
26ddb 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67  lite3GetTempRang
26ddc 65 28 70 50 61 72 73 65 2c 20 70 49 64 78 2d 3e  e(pParse, pIdx->
26ddd 6e 43 6f 6c 75 6d 6e 2b 31 29 3b 0a 20 20 20 20  nColumn+1);.    
26dde 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 64 78 2d  for(i=0; i<pIdx-
26ddf 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a  >nColumn; i++){.
26de0 20 20 20 20 20 20 69 6e 74 20 69 64 78 20 3d 20        int idx = 
26de1 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69  pIdx->aiColumn[i
26de2 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 69 64 78  ];.      if( idx
26de3 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b  ==pTab->iPKey ){
26de4 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
26de5 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
26de6 5f 53 43 6f 70 79 2c 20 72 65 67 52 6f 77 69 64  _SCopy, regRowid
26de7 2c 20 72 65 67 49 64 78 2b 69 29 3b 0a 20 20 20  , regIdx+i);.   
26de8 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
26de9 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
26dea 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c  Op2(v, OP_SCopy,
26deb 20 72 65 67 44 61 74 61 2b 69 64 78 2c 20 72 65   regData+idx, re
26dec 67 49 64 78 2b 69 29 3b 0a 20 20 20 20 20 20 7d  gIdx+i);.      }
26ded 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
26dee 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
26def 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67 52 6f 77  OP_SCopy, regRow
26df0 69 64 2c 20 72 65 67 49 64 78 2b 69 29 3b 0a 20  id, regIdx+i);. 
26df1 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
26df2 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52  dOp3(v, OP_MakeR
26df3 65 63 6f 72 64 2c 20 72 65 67 49 64 78 2c 20 70  ecord, regIdx, p
26df4 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 2c 20  Idx->nColumn+1, 
26df5 61 52 65 67 49 64 78 5b 69 43 75 72 5d 29 3b 0a  aRegIdx[iCur]);.
26df6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
26df7 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 73  hangeP4(v, -1, s
26df8 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e  qlite3IndexAffin
26df9 69 74 79 53 74 72 28 76 2c 20 70 49 64 78 29 2c  ityStr(v, pIdx),
26dfa 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
26dfb 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74  ExprCacheAffinit
26dfc 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20  yChange(pParse, 
26dfd 72 65 67 49 64 78 2c 20 70 49 64 78 2d 3e 6e 43  regIdx, pIdx->nC
26dfe 6f 6c 75 6d 6e 2b 31 29 3b 0a 0a 20 20 20 20 2f  olumn+1);..    /
26dff 2a 20 46 69 6e 64 20 6f 75 74 20 77 68 61 74 20  * Find out what 
26e00 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65 20 69  action to take i
26e01 6e 20 63 61 73 65 20 74 68 65 72 65 20 69 73 20  n case there is 
26e02 61 6e 20 69 6e 64 65 78 69 6e 67 20 63 6f 6e 66  an indexing conf
26e03 6c 69 63 74 20 2a 2f 0a 20 20 20 20 6f 6e 45 72  lict */.    onEr
26e04 72 6f 72 20 3d 20 70 49 64 78 2d 3e 6f 6e 45 72  ror = pIdx->onEr
26e05 72 6f 72 3b 0a 20 20 20 20 69 66 28 20 6f 6e 45  ror;.    if( onE
26e06 72 72 6f 72 3d 3d 4f 45 5f 4e 6f 6e 65 20 29 7b  rror==OE_None ){
26e07 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52   .      sqlite3R
26e08 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28  eleaseTempRange(
26e09 70 50 61 72 73 65 2c 20 72 65 67 49 64 78 2c 20  pParse, regIdx, 
26e0a 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29  pIdx->nColumn+1)
26e0b 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65  ;.      continue
26e0c 3b 20 20 2f 2a 20 70 49 64 78 20 69 73 20 6e 6f  ;  /* pIdx is no
26e0d 74 20 61 20 55 4e 49 51 55 45 20 69 6e 64 65 78  t a UNIQUE index
26e0e 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   */.    }.    if
26e0f 28 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72 21  ( overrideError!
26e10 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20  =OE_Default ){. 
26e11 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 6f       onError = o
26e12 76 65 72 72 69 64 65 45 72 72 6f 72 3b 0a 20 20  verrideError;.  
26e13 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 6e 45 72    }else if( onEr
26e14 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20  ror==OE_Default 
26e15 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f 72  ){.      onError
26e16 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20   = OE_Abort;.   
26e17 20 7d 0a 20 20 20 20 69 66 28 20 73 65 65 6e 52   }.    if( seenR
26e18 65 70 6c 61 63 65 20 29 7b 0a 20 20 20 20 20 20  eplace ){.      
26e19 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f  if( onError==OE_
26e1a 49 67 6e 6f 72 65 20 29 20 6f 6e 45 72 72 6f 72  Ignore ) onError
26e1b 20 3d 20 4f 45 5f 52 65 70 6c 61 63 65 3b 0a 20   = OE_Replace;. 
26e1c 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 6f 6e       else if( on
26e1d 45 72 72 6f 72 3d 3d 4f 45 5f 46 61 69 6c 20 29  Error==OE_Fail )
26e1e 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62   onError = OE_Ab
26e1f 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a  ort;.    }.    .
26e20 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20      /* Check to 
26e21 73 65 65 20 69 66 20 74 68 65 20 6e 65 77 20 69  see if the new i
26e22 6e 64 65 78 20 65 6e 74 72 79 20 77 69 6c 6c 20  ndex entry will 
26e23 62 65 20 75 6e 69 71 75 65 20 2a 2f 0a 20 20 20  be unique */.   
26e24 20 72 65 67 52 20 3d 20 73 71 6c 69 74 65 33 47   regR = sqlite3G
26e25 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
26e26 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
26e27 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53  beAddOp2(v, OP_S
26e28 43 6f 70 79 2c 20 72 65 67 4f 6c 64 52 6f 77 69  Copy, regOldRowi
26e29 64 2c 20 72 65 67 52 29 3b 0a 20 20 20 20 6a 33  d, regR);.    j3
26e2a 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
26e2b 64 4f 70 34 28 76 2c 20 4f 50 5f 49 73 55 6e 69  dOp4(v, OP_IsUni
26e2c 71 75 65 2c 20 62 61 73 65 43 75 72 2b 69 43 75  que, baseCur+iCu
26e2d 72 2b 31 2c 20 30 2c 0a 20 20 20 20 20 20 20 20  r+1, 0,.        
26e2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26e2f 20 20 20 72 65 67 52 2c 20 53 51 4c 49 54 45 5f     regR, SQLITE_
26e30 49 4e 54 5f 54 4f 5f 50 54 52 28 72 65 67 49 64  INT_TO_PTR(regId
26e31 78 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  x),.            
26e32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50                 P
26e33 34 5f 49 4e 54 33 32 29 3b 0a 20 20 20 20 73 71  4_INT32);.    sq
26e34 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
26e35 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65  Range(pParse, re
26e36 67 49 64 78 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c  gIdx, pIdx->nCol
26e37 75 6d 6e 2b 31 29 3b 0a 0a 20 20 20 20 2f 2a 20  umn+1);..    /* 
26e38 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68  Generate code th
26e39 61 74 20 65 78 65 63 75 74 65 73 20 69 66 20 74  at executes if t
26e3a 68 65 20 6e 65 77 20 69 6e 64 65 78 20 65 6e 74  he new index ent
26e3b 72 79 20 69 73 20 6e 6f 74 20 75 6e 69 71 75 65  ry is not unique
26e3c 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
26e3d 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 6f 6c 6c  onError==OE_Roll
26e3e 62 61 63 6b 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d  back || onError=
26e3f 3d 4f 45 5f 41 62 6f 72 74 20 7c 7c 20 6f 6e 45  =OE_Abort || onE
26e40 72 72 6f 72 3d 3d 4f 45 5f 46 61 69 6c 0a 20 20  rror==OE_Fail.  
26e41 20 20 20 20 20 20 7c 7c 20 6f 6e 45 72 72 6f 72        || onError
26e42 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20 7c 7c 20 6f  ==OE_Ignore || o
26e43 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61  nError==OE_Repla
26e44 63 65 20 29 3b 0a 20 20 20 20 73 77 69 74 63 68  ce );.    switch
26e45 28 20 6f 6e 45 72 72 6f 72 20 29 7b 0a 20 20 20  ( onError ){.   
26e46 20 20 20 63 61 73 65 20 4f 45 5f 52 6f 6c 6c 62     case OE_Rollb
26e47 61 63 6b 3a 0a 20 20 20 20 20 20 63 61 73 65 20  ack:.      case 
26e48 4f 45 5f 41 62 6f 72 74 3a 0a 20 20 20 20 20 20  OE_Abort:.      
26e49 63 61 73 65 20 4f 45 5f 46 61 69 6c 3a 20 7b 0a  case OE_Fail: {.
26e4a 20 20 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20          int j;. 
26e4b 20 20 20 20 20 20 20 53 74 72 41 63 63 75 6d 20         StrAccum 
26e4c 65 72 72 4d 73 67 3b 0a 20 20 20 20 20 20 20 20  errMsg;.        
26e4d 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 65 70  const char *zSep
26e4e 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a  ;.        char *
26e4f 7a 45 72 72 3b 0a 0a 20 20 20 20 20 20 20 20 73  zErr;..        s
26e50 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e  qlite3StrAccumIn
26e51 69 74 28 26 65 72 72 4d 73 67 2c 20 30 2c 20 30  it(&errMsg, 0, 0
26e52 2c 20 32 30 30 29 3b 0a 20 20 20 20 20 20 20 20  , 200);.        
26e53 65 72 72 4d 73 67 2e 64 62 20 3d 20 70 50 61 72  errMsg.db = pPar
26e54 73 65 2d 3e 64 62 3b 0a 20 20 20 20 20 20 20 20  se->db;.        
26e55 7a 53 65 70 20 3d 20 70 49 64 78 2d 3e 6e 43 6f  zSep = pIdx->nCo
26e56 6c 75 6d 6e 3e 31 20 3f 20 22 63 6f 6c 75 6d 6e  lumn>1 ? "column
26e57 73 20 22 20 3a 20 22 63 6f 6c 75 6d 6e 20 22 3b  s " : "column ";
26e58 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30  .        for(j=0
26e59 3b 20 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  ; j<pIdx->nColum
26e5a 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  n; j++){.       
26e5b 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20 3d 20     char *zCol = 
26e5c 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 49 64 78 2d  pTab->aCol[pIdx-
26e5d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 5d 2e 7a 4e  >aiColumn[j]].zN
26e5e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 20 20 73  ame;.          s
26e5f 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70  qlite3StrAccumAp
26e60 70 65 6e 64 28 26 65 72 72 4d 73 67 2c 20 7a 53  pend(&errMsg, zS
26e61 65 70 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 20  ep, -1);.       
26e62 20 20 20 7a 53 65 70 20 3d 20 22 2c 20 22 3b 0a     zSep = ", ";.
26e63 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
26e64 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28  3StrAccumAppend(
26e65 26 65 72 72 4d 73 67 2c 20 7a 43 6f 6c 2c 20 2d  &errMsg, zCol, -
26e66 31 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  1);.        }.  
26e67 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72        sqlite3Str
26e68 41 63 63 75 6d 41 70 70 65 6e 64 28 26 65 72 72  AccumAppend(&err
26e69 4d 73 67 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Msg,.           
26e6a 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 31   pIdx->nColumn>1
26e6b 20 3f 20 22 20 61 72 65 20 6e 6f 74 20 75 6e 69   ? " are not uni
26e6c 71 75 65 22 20 3a 20 22 20 69 73 20 6e 6f 74 20  que" : " is not 
26e6d 75 6e 69 71 75 65 22 2c 20 2d 31 29 3b 0a 20 20  unique", -1);.  
26e6e 20 20 20 20 20 20 7a 45 72 72 20 3d 20 73 71 6c        zErr = sql
26e6f 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69  ite3StrAccumFini
26e70 73 68 28 26 65 72 72 4d 73 67 29 3b 0a 20 20 20  sh(&errMsg);.   
26e71 20 20 20 20 20 73 71 6c 69 74 65 33 48 61 6c 74       sqlite3Halt
26e72 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 72 73  Constraint(pPars
26e73 65 2c 20 6f 6e 45 72 72 6f 72 2c 20 7a 45 72 72  e, onError, zErr
26e74 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71  , 0);.        sq
26e75 6c 69 74 65 33 44 62 46 72 65 65 28 65 72 72 4d  lite3DbFree(errM
26e76 73 67 2e 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20  sg.db, zErr);.  
26e77 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
26e78 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20     }.      case 
26e79 4f 45 5f 49 67 6e 6f 72 65 3a 20 7b 0a 20 20 20  OE_Ignore: {.   
26e7a 20 20 20 20 20 61 73 73 65 72 74 28 20 73 65 65       assert( see
26e7b 6e 52 65 70 6c 61 63 65 3d 3d 30 20 29 3b 0a 20  nReplace==0 );. 
26e7c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
26e7d 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47  beAddOp2(v, OP_G
26e7e 6f 74 6f 2c 20 30 2c 20 69 67 6e 6f 72 65 44 65  oto, 0, ignoreDe
26e7f 73 74 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65  st);.        bre
26e80 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
26e81 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
26e82 20 20 20 20 20 54 72 69 67 67 65 72 20 2a 70 54       Trigger *pT
26e83 72 69 67 67 65 72 20 3d 20 30 3b 0a 20 20 20 20  rigger = 0;.    
26e84 20 20 20 20 61 73 73 65 72 74 28 20 6f 6e 45 72      assert( onEr
26e85 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 20  ror==OE_Replace 
26e86 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
26e87 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73  Parse->db->flags
26e88 26 53 51 4c 49 54 45 5f 52 65 63 54 72 69 67 67  &SQLITE_RecTrigg
26e89 65 72 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ers ){.         
26e8a 20 70 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69   pTrigger = sqli
26e8b 74 65 33 54 72 69 67 67 65 72 73 45 78 69 73 74  te3TriggersExist
26e8c 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 54  (pParse, pTab, T
26e8d 4b 5f 44 45 4c 45 54 45 2c 20 30 2c 20 30 29 3b  K_DELETE, 0, 0);
26e8e 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
26e8f 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61     sqlite3Genera
26e90 74 65 52 6f 77 44 65 6c 65 74 65 28 0a 20 20 20  teRowDelete(.   
26e91 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2c           pParse,
26e92 20 70 54 61 62 2c 20 62 61 73 65 43 75 72 2c 20   pTab, baseCur, 
26e93 72 65 67 52 2c 20 30 2c 20 70 54 72 69 67 67 65  regR, 0, pTrigge
26e94 72 2c 20 4f 45 5f 52 65 70 6c 61 63 65 0a 20 20  r, OE_Replace.  
26e95 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20        );.       
26e96 20 73 65 65 6e 52 65 70 6c 61 63 65 20 3d 20 31   seenReplace = 1
26e97 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
26e98 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
26e99 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
26e9a 6d 70 48 65 72 65 28 76 2c 20 6a 33 29 3b 0a 20  mpHere(v, j3);. 
26e9b 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
26e9c 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
26e9d 20 72 65 67 52 29 3b 0a 20 20 7d 0a 0a 20 20 69   regR);.  }..  i
26e9e 66 28 20 70 62 4d 61 79 52 65 70 6c 61 63 65 20  f( pbMayReplace 
26e9f 29 7b 0a 20 20 20 20 2a 70 62 4d 61 79 52 65 70  ){.    *pbMayRep
26ea0 6c 61 63 65 20 3d 20 73 65 65 6e 52 65 70 6c 61  lace = seenRepla
26ea1 63 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  ce;.  }.}../*.**
26ea2 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65   This routine ge
26ea3 6e 65 72 61 74 65 73 20 63 6f 64 65 20 74 6f 20  nerates code to 
26ea4 66 69 6e 69 73 68 20 74 68 65 20 49 4e 53 45 52  finish the INSER
26ea5 54 20 6f 72 20 55 50 44 41 54 45 20 6f 70 65 72  T or UPDATE oper
26ea6 61 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 77 61  ation.** that wa
26ea7 73 20 73 74 61 72 74 65 64 20 62 79 20 61 20 70  s started by a p
26ea8 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71 6c  rior call to sql
26ea9 69 74 65 33 47 65 6e 65 72 61 74 65 43 6f 6e 73  ite3GenerateCons
26eaa 74 72 61 69 6e 74 43 68 65 63 6b 73 2e 0a 2a 2a  traintChecks..**
26eab 20 41 20 63 6f 6e 73 65 63 75 74 69 76 65 20 72   A consecutive r
26eac 61 6e 67 65 20 6f 66 20 72 65 67 69 73 74 65 72  ange of register
26ead 73 20 73 74 61 72 74 69 6e 67 20 61 74 20 72 65  s starting at re
26eae 67 52 6f 77 69 64 20 63 6f 6e 74 61 69 6e 73 20  gRowid contains 
26eaf 74 68 65 0a 2a 2a 20 72 6f 77 69 64 20 61 6e 64  the.** rowid and
26eb0 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 74 6f 20   the content to 
26eb1 62 65 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2a 0a  be inserted..**.
26eb2 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 73  ** The arguments
26eb3 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65   to this routine
26eb4 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65 20 73   should be the s
26eb5 61 6d 65 20 61 73 20 74 68 65 20 66 69 72 73 74  ame as the first
26eb6 20 73 69 78 0a 2a 2a 20 61 72 67 75 6d 65 6e 74   six.** argument
26eb7 73 20 74 6f 20 73 71 6c 69 74 65 33 47 65 6e 65  s to sqlite3Gene
26eb8 72 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 68  rateConstraintCh
26eb9 65 63 6b 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ecks..*/.SQLITE_
26eba 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
26ebb 69 74 65 33 43 6f 6d 70 6c 65 74 65 49 6e 73 65  ite3CompleteInse
26ebc 72 74 69 6f 6e 28 0a 20 20 50 61 72 73 65 20 2a  rtion(.  Parse *
26ebd 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20  pParse,      /* 
26ebe 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65  The parser conte
26ebf 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  xt */.  Table *p
26ec0 54 61 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 74  Tab,        /* t
26ec1 68 65 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68  he table into wh
26ec2 69 63 68 20 77 65 20 61 72 65 20 69 6e 73 65 72  ich we are inser
26ec3 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 62 61  ting */.  int ba
26ec4 73 65 43 75 72 2c 20 20 20 20 20 20 20 20 2f 2a  seCur,        /*
26ec5 20 49 6e 64 65 78 20 6f 66 20 61 20 72 65 61 64   Index of a read
26ec6 2f 77 72 69 74 65 20 63 75 72 73 6f 72 20 70 6f  /write cursor po
26ec7 69 6e 74 69 6e 67 20 61 74 20 70 54 61 62 20 2a  inting at pTab *
26ec8 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64  /.  int regRowid
26ec9 2c 20 20 20 20 20 20 20 2f 2a 20 52 61 6e 67 65  ,       /* Range
26eca 20 6f 66 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20   of content */. 
26ecb 20 69 6e 74 20 2a 61 52 65 67 49 64 78 2c 20 20   int *aRegIdx,  
26ecc 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72       /* Register
26ecd 20 75 73 65 64 20 62 79 20 65 61 63 68 20 69 6e   used by each in
26ece 64 65 78 2e 20 20 30 20 66 6f 72 20 75 6e 75 73  dex.  0 for unus
26ecf 65 64 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20  ed indices */.  
26ed0 69 6e 74 20 69 73 55 70 64 61 74 65 2c 20 20 20  int isUpdate,   
26ed1 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20      /* True for 
26ed2 55 50 44 41 54 45 2c 20 46 61 6c 73 65 20 66 6f  UPDATE, False fo
26ed3 72 20 49 4e 53 45 52 54 20 2a 2f 0a 20 20 69 6e  r INSERT */.  in
26ed4 74 20 61 70 70 65 6e 64 42 69 61 73 2c 20 20 20  t appendBias,   
26ed5 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69    /* True if thi
26ed6 73 20 69 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62  s is likely to b
26ed7 65 20 61 6e 20 61 70 70 65 6e 64 20 2a 2f 0a 20  e an append */. 
26ed8 20 69 6e 74 20 75 73 65 53 65 65 6b 52 65 73 75   int useSeekResu
26ed9 6c 74 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20  lt   /* True to 
26eda 73 65 74 20 74 68 65 20 55 53 45 53 45 45 4b 52  set the USESEEKR
26edb 45 53 55 4c 54 20 66 6c 61 67 20 6f 6e 20 4f 50  ESULT flag on OP
26edc 5f 5b 49 64 78 5d 49 6e 73 65 72 74 20 2a 2f 0a  _[Idx]Insert */.
26edd 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 56 64  ){.  int i;.  Vd
26ede 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 6e 49 64  be *v;.  int nId
26edf 78 3b 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78  x;.  Index *pIdx
26ee0 3b 0a 20 20 75 38 20 70 69 6b 5f 66 6c 61 67 73  ;.  u8 pik_flags
26ee1 3b 0a 20 20 69 6e 74 20 72 65 67 44 61 74 61 3b  ;.  int regData;
26ee2 0a 20 20 69 6e 74 20 72 65 67 52 65 63 3b 0a 0a  .  int regRec;..
26ee3 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
26ee4 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
26ee5 61 73 73 65 72 74 28 20 76 21 3d 30 20 29 3b 0a  assert( v!=0 );.
26ee6 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e    assert( pTab->
26ee7 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b 20 20 2f  pSelect==0 );  /
26ee8 2a 20 54 68 69 73 20 74 61 62 6c 65 20 69 73 20  * This table is 
26ee9 6e 6f 74 20 61 20 56 49 45 57 20 2a 2f 0a 20 20  not a VIEW */.  
26eea 66 6f 72 28 6e 49 64 78 3d 30 2c 20 70 49 64 78  for(nIdx=0, pIdx
26eeb 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
26eec 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
26eed 70 4e 65 78 74 2c 20 6e 49 64 78 2b 2b 29 7b 7d  pNext, nIdx++){}
26eee 0a 20 20 66 6f 72 28 69 3d 6e 49 64 78 2d 31 3b  .  for(i=nIdx-1;
26eef 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20   i>=0; i--){.   
26ef0 20 69 66 28 20 61 52 65 67 49 64 78 5b 69 5d 3d   if( aRegIdx[i]=
26ef1 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  =0 ) continue;. 
26ef2 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
26ef3 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e  dOp2(v, OP_IdxIn
26ef4 73 65 72 74 2c 20 62 61 73 65 43 75 72 2b 69 2b  sert, baseCur+i+
26ef5 31 2c 20 61 52 65 67 49 64 78 5b 69 5d 29 3b 0a  1, aRegIdx[i]);.
26ef6 20 20 20 20 69 66 28 20 75 73 65 53 65 65 6b 52      if( useSeekR
26ef7 65 73 75 6c 74 20 29 7b 0a 20 20 20 20 20 20 73  esult ){.      s
26ef8 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
26ef9 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 55 53 45  P5(v, OPFLAG_USE
26efa 53 45 45 4b 52 45 53 55 4c 54 29 3b 0a 20 20 20  SEEKRESULT);.   
26efb 20 7d 0a 20 20 7d 0a 20 20 72 65 67 44 61 74 61   }.  }.  regData
26efc 20 3d 20 72 65 67 52 6f 77 69 64 20 2b 20 31 3b   = regRowid + 1;
26efd 0a 20 20 72 65 67 52 65 63 20 3d 20 73 71 6c 69  .  regRec = sqli
26efe 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
26eff 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  arse);.  sqlite3
26f00 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
26f01 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67  _MakeRecord, reg
26f02 44 61 74 61 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c  Data, pTab->nCol
26f03 2c 20 72 65 67 52 65 63 29 3b 0a 20 20 73 71 6c  , regRec);.  sql
26f04 69 74 65 33 54 61 62 6c 65 41 66 66 69 6e 69 74  ite3TableAffinit
26f05 79 53 74 72 28 76 2c 20 70 54 61 62 29 3b 0a 20  yStr(v, pTab);. 
26f06 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
26f07 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28  eAffinityChange(
26f08 70 50 61 72 73 65 2c 20 72 65 67 44 61 74 61 2c  pParse, regData,
26f09 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20   pTab->nCol);.  
26f0a 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74  if( pParse->nest
26f0b 65 64 20 29 7b 0a 20 20 20 20 70 69 6b 5f 66 6c  ed ){.    pik_fl
26f0c 61 67 73 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  ags = 0;.  }else
26f0d 7b 0a 20 20 20 20 70 69 6b 5f 66 6c 61 67 73 20  {.    pik_flags 
26f0e 3d 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45  = OPFLAG_NCHANGE
26f0f 3b 0a 20 20 20 20 70 69 6b 5f 66 6c 61 67 73 20  ;.    pik_flags 
26f10 7c 3d 20 28 69 73 55 70 64 61 74 65 3f 4f 50 46  |= (isUpdate?OPF
26f11 4c 41 47 5f 49 53 55 50 44 41 54 45 3a 4f 50 46  LAG_ISUPDATE:OPF
26f12 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44 29 3b 0a  LAG_LASTROWID);.
26f13 20 20 7d 0a 20 20 69 66 28 20 61 70 70 65 6e 64    }.  if( append
26f14 42 69 61 73 20 29 7b 0a 20 20 20 20 70 69 6b 5f  Bias ){.    pik_
26f15 66 6c 61 67 73 20 7c 3d 20 4f 50 46 4c 41 47 5f  flags |= OPFLAG_
26f16 41 50 50 45 4e 44 3b 0a 20 20 7d 0a 20 20 69 66  APPEND;.  }.  if
26f17 28 20 75 73 65 53 65 65 6b 52 65 73 75 6c 74 20  ( useSeekResult 
26f18 29 7b 0a 20 20 20 20 70 69 6b 5f 66 6c 61 67 73  ){.    pik_flags
26f19 20 7c 3d 20 4f 50 46 4c 41 47 5f 55 53 45 53 45   |= OPFLAG_USESE
26f1a 45 4b 52 45 53 55 4c 54 3b 0a 20 20 7d 0a 20 20  EKRESULT;.  }.  
26f1b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
26f1c 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20  3(v, OP_Insert, 
26f1d 62 61 73 65 43 75 72 2c 20 72 65 67 52 65 63 2c  baseCur, regRec,
26f1e 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 69 66   regRowid);.  if
26f1f 28 20 21 70 50 61 72 73 65 2d 3e 6e 65 73 74 65  ( !pParse->neste
26f20 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  d ){.    sqlite3
26f21 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
26f22 2d 31 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c  -1, pTab->zName,
26f23 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d   P4_STATIC);.  }
26f24 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  .  sqlite3VdbeCh
26f25 61 6e 67 65 50 35 28 76 2c 20 70 69 6b 5f 66 6c  angeP5(v, pik_fl
26f26 61 67 73 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  ags);.}../*.** G
26f27 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61  enerate code tha
26f28 74 20 77 69 6c 6c 20 6f 70 65 6e 20 63 75 72 73  t will open curs
26f29 6f 72 73 20 66 6f 72 20 61 20 74 61 62 6c 65 20  ors for a table 
26f2a 61 6e 64 20 66 6f 72 20 61 6c 6c 0a 2a 2a 20 69  and for all.** i
26f2b 6e 64 69 63 65 73 20 6f 66 20 74 68 61 74 20 74  ndices of that t
26f2c 61 62 6c 65 2e 20 20 54 68 65 20 22 62 61 73 65  able.  The "base
26f2d 43 75 72 22 20 70 61 72 61 6d 65 74 65 72 20 69  Cur" parameter i
26f2e 73 20 74 68 65 20 63 75 72 73 6f 72 20 6e 75 6d  s the cursor num
26f2f 62 65 72 20 75 73 65 64 0a 2a 2a 20 66 6f 72 20  ber used.** for 
26f30 74 68 65 20 74 61 62 6c 65 2e 20 20 49 6e 64 69  the table.  Indi
26f31 63 65 73 20 61 72 65 20 6f 70 65 6e 65 64 20 6f  ces are opened o
26f32 6e 20 73 75 62 73 65 71 75 65 6e 74 20 63 75 72  n subsequent cur
26f33 73 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  sors..**.** Retu
26f34 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
26f35 20 69 6e 64 69 63 65 73 20 6f 6e 20 74 68 65 20   indices on the 
26f36 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  table..*/.SQLITE
26f37 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
26f38 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64  ite3OpenTableAnd
26f39 49 6e 64 69 63 65 73 28 0a 20 20 50 61 72 73 65  Indices(.  Parse
26f3a 20 2a 70 50 61 72 73 65 2c 20 20 20 2f 2a 20 50   *pParse,   /* P
26f3b 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
26f3c 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c  /.  Table *pTab,
26f3d 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f       /* Table to
26f3e 20 62 65 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20   be opened */.  
26f3f 69 6e 74 20 62 61 73 65 43 75 72 2c 20 20 20 20  int baseCur,    
26f40 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65   /* Cursor numbe
26f41 72 20 61 73 73 69 67 6e 65 64 20 74 6f 20 74 68  r assigned to th
26f42 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  e table */.  int
26f43 20 6f 70 20 20 20 20 20 20 20 20 20 20 20 2f 2a   op           /*
26f44 20 4f 50 5f 4f 70 65 6e 52 65 61 64 20 6f 72 20   OP_OpenRead or 
26f45 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 2a 2f 0a  OP_OpenWrite */.
26f46 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  ){.  int i;.  in
26f47 74 20 69 44 62 3b 0a 20 20 49 6e 64 65 78 20 2a  t iDb;.  Index *
26f48 70 49 64 78 3b 0a 20 20 56 64 62 65 20 2a 76 3b  pIdx;.  Vdbe *v;
26f49 0a 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61  ..  if( IsVirtua
26f4a 6c 28 70 54 61 62 29 20 29 20 72 65 74 75 72 6e  l(pTab) ) return
26f4b 20 30 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69   0;.  iDb = sqli
26f4c 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
26f4d 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61  (pParse->db, pTa
26f4e 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 76  b->pSchema);.  v
26f4f 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
26f50 65 28 70 50 61 72 73 65 29 3b 0a 20 20 61 73 73  e(pParse);.  ass
26f51 65 72 74 28 20 76 21 3d 30 20 29 3b 0a 20 20 73  ert( v!=0 );.  s
26f52 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28  qlite3OpenTable(
26f53 70 50 61 72 73 65 2c 20 62 61 73 65 43 75 72 2c  pParse, baseCur,
26f54 20 69 44 62 2c 20 70 54 61 62 2c 20 6f 70 29 3b   iDb, pTab, op);
26f55 0a 20 20 66 6f 72 28 69 3d 31 2c 20 70 49 64 78  .  for(i=1, pIdx
26f56 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
26f57 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
26f58 70 4e 65 78 74 2c 20 69 2b 2b 29 7b 0a 20 20 20  pNext, i++){.   
26f59 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d   KeyInfo *pKey =
26f5a 20 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79   sqlite3IndexKey
26f5b 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 49 64  info(pParse, pId
26f5c 78 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  x);.    assert( 
26f5d 70 49 64 78 2d 3e 70 53 63 68 65 6d 61 3d 3d 70  pIdx->pSchema==p
26f5e 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0a  Tab->pSchema );.
26f5f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
26f60 64 64 4f 70 34 28 76 2c 20 6f 70 2c 20 69 2b 62  ddOp4(v, op, i+b
26f61 61 73 65 43 75 72 2c 20 70 49 64 78 2d 3e 74 6e  aseCur, pIdx->tn
26f62 75 6d 2c 20 69 44 62 2c 0a 20 20 20 20 20 20 20  um, iDb,.       
26f63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
26f64 63 68 61 72 2a 29 70 4b 65 79 2c 20 50 34 5f 4b  char*)pKey, P4_K
26f65 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b  EYINFO_HANDOFF);
26f66 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74  .    VdbeComment
26f67 28 28 76 2c 20 22 25 73 22 2c 20 70 49 64 78 2d  ((v, "%s", pIdx-
26f68 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 7d 0a 20 20  >zName));.  }.  
26f69 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  if( pParse->nTab
26f6a 3c 62 61 73 65 43 75 72 2b 69 20 29 7b 0a 20 20  <baseCur+i ){.  
26f6b 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 20 3d    pParse->nTab =
26f6c 20 62 61 73 65 43 75 72 2b 69 3b 0a 20 20 7d 0a   baseCur+i;.  }.
26f6d 20 20 72 65 74 75 72 6e 20 69 2d 31 3b 0a 7d 0a    return i-1;.}.
26f6e 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
26f6f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  TEST./*.** The f
26f70 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20  ollowing global 
26f71 76 61 72 69 61 62 6c 65 20 69 73 20 69 6e 63 72  variable is incr
26f72 65 6d 65 6e 74 65 64 20 77 68 65 6e 65 76 65 72  emented whenever
26f73 20 74 68 65 0a 2a 2a 20 74 72 61 6e 73 66 65 72   the.** transfer
26f74 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73   optimization is
26f75 20 75 73 65 64 2e 20 20 54 68 69 73 20 69 73 20   used.  This is 
26f76 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67  used for testing
26f77 0a 2a 2a 20 70 75 72 70 6f 73 65 73 20 6f 6e 6c  .** purposes onl
26f78 79 20 2d 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  y - to make sure
26f79 20 74 68 65 20 74 72 61 6e 73 66 65 72 20 6f 70   the transfer op
26f7a 74 69 6d 69 7a 61 74 69 6f 6e 20 72 65 61 6c 6c  timization reall
26f7b 79 0a 2a 2a 20 69 73 20 68 61 70 70 65 6e 69 6e  y.** is happenin
26f7c 67 20 77 68 65 6e 20 69 74 20 69 73 20 73 75 70  g when it is sup
26f7d 70 6f 73 65 20 74 6f 2e 0a 2a 2f 0a 53 51 4c 49  pose to..*/.SQLI
26f7e 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
26f7f 65 33 5f 78 66 65 72 6f 70 74 5f 63 6f 75 6e 74  e3_xferopt_count
26f80 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ;.#endif /* SQLI
26f81 54 45 5f 54 45 53 54 20 2a 2f 0a 0a 0a 23 69 66  TE_TEST */...#if
26f82 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
26f83 5f 58 46 45 52 5f 4f 50 54 0a 2f 2a 0a 2a 2a 20  _XFER_OPT./*.** 
26f84 43 68 65 63 6b 20 74 6f 20 63 6f 6c 6c 61 74 69  Check to collati
26f85 6f 6e 20 6e 61 6d 65 73 20 74 6f 20 73 65 65 20  on names to see 
26f86 69 66 20 74 68 65 79 20 61 72 65 20 63 6f 6d 70  if they are comp
26f87 61 74 69 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  atible..*/.stati
26f88 63 20 69 6e 74 20 78 66 65 72 43 6f 6d 70 61 74  c int xferCompat
26f89 69 62 6c 65 43 6f 6c 6c 61 74 69 6f 6e 28 63 6f  ibleCollation(co
26f8a 6e 73 74 20 63 68 61 72 20 2a 7a 31 2c 20 63 6f  nst char *z1, co
26f8b 6e 73 74 20 63 68 61 72 20 2a 7a 32 29 7b 0a 20  nst char *z2){. 
26f8c 20 69 66 28 20 7a 31 3d 3d 30 20 29 7b 0a 20 20   if( z1==0 ){.  
26f8d 20 20 72 65 74 75 72 6e 20 7a 32 3d 3d 30 3b 0a    return z2==0;.
26f8e 20 20 7d 0a 20 20 69 66 28 20 7a 32 3d 3d 30 20    }.  if( z2==0 
26f8f 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
26f90 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71  .  }.  return sq
26f91 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 31 2c  lite3StrICmp(z1,
26f92 20 7a 32 29 3d 3d 30 3b 0a 7d 0a 0a 0a 2f 2a 0a   z2)==0;.}.../*.
26f93 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20  ** Check to see 
26f94 69 66 20 69 6e 64 65 78 20 70 53 72 63 20 69 73  if index pSrc is
26f95 20 63 6f 6d 70 61 74 69 62 6c 65 20 61 73 20 61   compatible as a
26f96 20 73 6f 75 72 63 65 20 6f 66 20 64 61 74 61 0a   source of data.
26f97 2a 2a 20 66 6f 72 20 69 6e 64 65 78 20 70 44 65  ** for index pDe
26f98 73 74 20 69 6e 20 61 6e 20 69 6e 73 65 72 74 20  st in an insert 
26f99 74 72 61 6e 73 66 65 72 20 6f 70 74 69 6d 69 7a  transfer optimiz
26f9a 61 74 69 6f 6e 2e 20 20 54 68 65 20 72 75 6c 65  ation.  The rule
26f9b 73 0a 2a 2a 20 66 6f 72 20 61 20 63 6f 6d 70 61  s.** for a compa
26f9c 74 69 62 6c 65 20 69 6e 64 65 78 3a 0a 2a 2a 0a  tible index:.**.
26f9d 2a 2a 20 20 20 20 2a 20 20 20 54 68 65 20 69 6e  **    *   The in
26f9e 64 65 78 20 69 73 20 6f 76 65 72 20 74 68 65 20  dex is over the 
26f9f 73 61 6d 65 20 73 65 74 20 6f 66 20 63 6f 6c 75  same set of colu
26fa0 6d 6e 73 0a 2a 2a 20 20 20 20 2a 20 20 20 54 68  mns.**    *   Th
26fa1 65 20 73 61 6d 65 20 44 45 53 43 20 61 6e 64 20  e same DESC and 
26fa2 41 53 43 20 6d 61 72 6b 69 6e 67 73 20 6f 63 63  ASC markings occ
26fa3 75 72 73 20 6f 6e 20 61 6c 6c 20 63 6f 6c 75 6d  urs on all colum
26fa4 6e 73 0a 2a 2a 20 20 20 20 2a 20 20 20 54 68 65  ns.**    *   The
26fa5 20 73 61 6d 65 20 6f 6e 45 72 72 6f 72 20 70 72   same onError pr
26fa6 6f 63 65 73 73 69 6e 67 20 28 4f 45 5f 41 62 6f  ocessing (OE_Abo
26fa7 72 74 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 65  rt, OE_Ignore, e
26fa8 74 63 29 0a 2a 2a 20 20 20 20 2a 20 20 20 54 68  tc).**    *   Th
26fa9 65 20 73 61 6d 65 20 63 6f 6c 6c 61 74 69 6e 67  e same collating
26faa 20 73 65 71 75 65 6e 63 65 20 6f 6e 20 65 61 63   sequence on eac
26fab 68 20 63 6f 6c 75 6d 6e 0a 2a 2f 0a 73 74 61 74  h column.*/.stat
26fac 69 63 20 69 6e 74 20 78 66 65 72 43 6f 6d 70 61  ic int xferCompa
26fad 74 69 62 6c 65 49 6e 64 65 78 28 49 6e 64 65 78  tibleIndex(Index
26fae 20 2a 70 44 65 73 74 2c 20 49 6e 64 65 78 20 2a   *pDest, Index *
26faf 70 53 72 63 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  pSrc){.  int i;.
26fb0 20 20 61 73 73 65 72 74 28 20 70 44 65 73 74 20    assert( pDest 
26fb1 26 26 20 70 53 72 63 20 29 3b 0a 20 20 61 73 73  && pSrc );.  ass
26fb2 65 72 74 28 20 70 44 65 73 74 2d 3e 70 54 61 62  ert( pDest->pTab
26fb3 6c 65 21 3d 70 53 72 63 2d 3e 70 54 61 62 6c 65  le!=pSrc->pTable
26fb4 20 29 3b 0a 20 20 69 66 28 20 70 44 65 73 74 2d   );.  if( pDest-
26fb5 3e 6e 43 6f 6c 75 6d 6e 21 3d 70 53 72 63 2d 3e  >nColumn!=pSrc->
26fb6 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 72  nColumn ){.    r
26fb7 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 44 69  eturn 0;   /* Di
26fb8 66 66 65 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f  fferent number o
26fb9 66 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 7d  f columns */.  }
26fba 0a 20 20 69 66 28 20 70 44 65 73 74 2d 3e 6f 6e  .  if( pDest->on
26fbb 45 72 72 6f 72 21 3d 70 53 72 63 2d 3e 6f 6e 45  Error!=pSrc->onE
26fbc 72 72 6f 72 20 29 7b 0a 20 20 20 20 72 65 74 75  rror ){.    retu
26fbd 72 6e 20 30 3b 20 20 20 2f 2a 20 44 69 66 66 65  rn 0;   /* Diffe
26fbe 72 65 6e 74 20 63 6f 6e 66 6c 69 63 74 20 72 65  rent conflict re
26fbf 73 6f 6c 75 74 69 6f 6e 20 73 74 72 61 74 65 67  solution strateg
26fc0 69 65 73 20 2a 2f 0a 20 20 7d 0a 20 20 66 6f 72  ies */.  }.  for
26fc1 28 69 3d 30 3b 20 69 3c 70 53 72 63 2d 3e 6e 43  (i=0; i<pSrc->nC
26fc2 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20  olumn; i++){.   
26fc3 20 69 66 28 20 70 53 72 63 2d 3e 61 69 43 6f 6c   if( pSrc->aiCol
26fc4 75 6d 6e 5b 69 5d 21 3d 70 44 65 73 74 2d 3e 61  umn[i]!=pDest->a
26fc5 69 43 6f 6c 75 6d 6e 5b 69 5d 20 29 7b 0a 20 20  iColumn[i] ){.  
26fc6 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
26fc7 2f 2a 20 44 69 66 66 65 72 65 6e 74 20 63 6f 6c  /* Different col
26fc8 75 6d 6e 73 20 69 6e 64 65 78 65 64 20 2a 2f 0a  umns indexed */.
26fc9 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 53      }.    if( pS
26fca 72 63 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69  rc->aSortOrder[i
26fcb 5d 21 3d 70 44 65 73 74 2d 3e 61 53 6f 72 74 4f  ]!=pDest->aSortO
26fcc 72 64 65 72 5b 69 5d 20 29 7b 0a 20 20 20 20 20  rder[i] ){.     
26fcd 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
26fce 44 69 66 66 65 72 65 6e 74 20 73 6f 72 74 20 6f  Different sort o
26fcf 72 64 65 72 73 20 2a 2f 0a 20 20 20 20 7d 0a 20  rders */.    }. 
26fd0 20 20 20 69 66 28 20 21 78 66 65 72 43 6f 6d 70     if( !xferComp
26fd1 61 74 69 62 6c 65 43 6f 6c 6c 61 74 69 6f 6e 28  atibleCollation(
26fd2 70 53 72 63 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 2c  pSrc->azColl[i],
26fd3 70 44 65 73 74 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d  pDest->azColl[i]
26fd4 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
26fd5 6e 20 30 3b 20 20 20 2f 2a 20 44 69 66 66 65 72  n 0;   /* Differ
26fd6 65 6e 74 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  ent collating se
26fd7 71 75 65 6e 63 65 73 20 2a 2f 0a 20 20 20 20 7d  quences */.    }
26fd8 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f  .  }..  /* If no
26fd9 20 74 65 73 74 20 61 62 6f 76 65 20 66 61 69 6c   test above fail
26fda 73 20 74 68 65 6e 20 74 68 65 20 69 6e 64 69 63  s then the indic
26fdb 65 73 20 6d 75 73 74 20 62 65 20 63 6f 6d 70 61  es must be compa
26fdc 74 69 62 6c 65 20 2a 2f 0a 20 20 72 65 74 75 72  tible */.  retur
26fdd 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74  n 1;.}../*.** At
26fde 74 65 6d 70 74 20 74 68 65 20 74 72 61 6e 73 66  tempt the transf
26fdf 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20  er optimization 
26fe0 6f 6e 20 49 4e 53 45 52 54 73 20 6f 66 20 74 68  on INSERTs of th
26fe1 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 20  e form.**.**    
26fe2 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 62   INSERT INTO tab
26fe3 31 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20  1 SELECT * FROM 
26fe4 74 61 62 32 3b 0a 2a 2a 0a 2a 2a 20 54 68 69 73  tab2;.**.** This
26fe5 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73   optimization is
26fe6 20 6f 6e 6c 79 20 61 74 74 65 6d 70 74 65 64 20   only attempted 
26fe7 69 66 0a 2a 2a 0a 2a 2a 20 20 20 20 28 31 29 20  if.**.**    (1) 
26fe8 20 74 61 62 31 20 61 6e 64 20 74 61 62 32 20 68   tab1 and tab2 h
26fe9 61 76 65 20 69 64 65 6e 74 69 63 61 6c 20 73 63  ave identical sc
26fea 68 65 6d 61 73 20 69 6e 63 6c 75 64 69 6e 67 20  hemas including 
26feb 61 6c 6c 20 74 68 65 0a 2a 2a 20 20 20 20 20 20  all the.**      
26fec 20 20 20 73 61 6d 65 20 69 6e 64 69 63 65 73 20     same indices 
26fed 61 6e 64 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a  and constraints.
26fee 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20 20 74 61  **.**    (2)  ta
26fef 62 31 20 61 6e 64 20 74 61 62 32 20 61 72 65 20  b1 and tab2 are 
26ff0 64 69 66 66 65 72 65 6e 74 20 74 61 62 6c 65 73  different tables
26ff1 0a 2a 2a 0a 2a 2a 20 20 20 20 28 33 29 20 20 54  .**.**    (3)  T
26ff2 68 65 72 65 20 6d 75 73 74 20 62 65 20 6e 6f 20  here must be no 
26ff3 74 72 69 67 67 65 72 73 20 6f 6e 20 74 61 62 31  triggers on tab1
26ff4 0a 2a 2a 0a 2a 2a 20 20 20 20 28 34 29 20 20 54  .**.**    (4)  T
26ff5 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66  he result set of
26ff6 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   the SELECT stat
26ff7 65 6d 65 6e 74 20 69 73 20 22 2a 22 0a 2a 2a 0a  ement is "*".**.
26ff8 2a 2a 20 20 20 20 28 35 29 20 20 54 68 65 20 53  **    (5)  The S
26ff9 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
26ffa 68 61 73 20 6e 6f 20 57 48 45 52 45 2c 20 48 41  has no WHERE, HA
26ffb 56 49 4e 47 2c 20 4f 52 44 45 52 20 42 59 2c 20  VING, ORDER BY, 
26ffc 47 52 4f 55 50 20 42 59 2c 0a 2a 2a 20 20 20 20  GROUP BY,.**    
26ffd 20 20 20 20 20 6f 72 20 4c 49 4d 49 54 20 63 6c       or LIMIT cl
26ffe 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28  ause..**.**    (
26fff 36 29 20 20 54 68 65 20 53 45 4c 45 43 54 20 73  6)  The SELECT s
27000 74 61 74 65 6d 65 6e 74 20 69 73 20 61 20 73 69  tatement is a si
27001 6d 70 6c 65 20 28 6e 6f 74 20 61 20 63 6f 6d 70  mple (not a comp
27002 6f 75 6e 64 29 20 73 65 6c 65 63 74 20 74 68 61  ound) select tha
27003 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 63 6f 6e  t.**         con
27004 74 61 69 6e 73 20 6f 6e 6c 79 20 74 61 62 32 20  tains only tab2 
27005 69 6e 20 69 74 73 20 46 52 4f 4d 20 63 6c 61 75  in its FROM clau
27006 73 65 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 65  se.**.** This me
27007 74 68 6f 64 20 66 6f 72 20 69 6d 70 6c 65 6d 65  thod for impleme
27008 6e 74 69 6e 67 20 74 68 65 20 49 4e 53 45 52 54  nting the INSERT
27009 20 74 72 61 6e 73 66 65 72 73 20 72 61 77 20 72   transfers raw r
2700a 65 63 6f 72 64 73 20 66 72 6f 6d 0a 2a 2a 20 74  ecords from.** t
2700b 61 62 32 20 6f 76 65 72 20 74 6f 20 74 61 62 31  ab2 over to tab1
2700c 2e 20 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20 61  .  The columns a
2700d 72 65 20 6e 6f 74 20 64 65 63 6f 64 65 64 2e 20  re not decoded. 
2700e 20 52 61 77 20 72 65 63 6f 72 64 73 20 66 72 6f   Raw records fro
2700f 6d 0a 2a 2a 20 74 68 65 20 69 6e 64 69 63 65 73  m.** the indices
27010 20 6f 66 20 74 61 62 32 20 61 72 65 20 74 72 61   of tab2 are tra
27011 6e 73 66 65 72 65 64 20 74 6f 20 74 61 62 31 20  nsfered to tab1 
27012 61 73 20 77 65 6c 6c 2e 20 20 49 6e 20 73 6f 20  as well.  In so 
27013 64 6f 69 6e 67 2c 0a 2a 2a 20 74 68 65 20 72 65  doing,.** the re
27014 73 75 6c 74 69 6e 67 20 74 61 62 31 20 68 61 73  sulting tab1 has
27015 20 6d 75 63 68 20 6c 65 73 73 20 66 72 61 67 6d   much less fragm
27016 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  entation..**.** 
27017 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  This routine ret
27018 75 72 6e 73 20 54 52 55 45 20 69 66 20 74 68 65  urns TRUE if the
27019 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73   optimization is
2701a 20 61 74 74 65 6d 70 74 65 64 2e 20 20 49 66 20   attempted.  If 
2701b 61 6e 79 0a 2a 2a 20 6f 66 20 74 68 65 20 63 6f  any.** of the co
2701c 6e 64 69 74 69 6f 6e 73 20 61 62 6f 76 65 20 66  nditions above f
2701d 61 69 6c 20 73 6f 20 74 68 61 74 20 74 68 65 20  ail so that the 
2701e 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 73 68 6f  optimization sho
2701f 75 6c 64 20 6e 6f 74 0a 2a 2a 20 62 65 20 61 74  uld not.** be at
27020 74 65 6d 70 74 65 64 2c 20 74 68 65 6e 20 74 68  tempted, then th
27021 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  is routine retur
27022 6e 73 20 46 41 4c 53 45 2e 0a 2a 2f 0a 73 74 61  ns FALSE..*/.sta
27023 74 69 63 20 69 6e 74 20 78 66 65 72 4f 70 74 69  tic int xferOpti
27024 6d 69 7a 61 74 69 6f 6e 28 0a 20 20 50 61 72 73  mization(.  Pars
27025 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
27026 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74    /* Parser cont
27027 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ext */.  Table *
27028 70 44 65 73 74 2c 20 20 20 20 20 20 20 20 20 2f  pDest,         /
27029 2a 20 54 68 65 20 74 61 62 6c 65 20 77 65 20 61  * The table we a
2702a 72 65 20 69 6e 73 65 72 74 69 6e 67 20 69 6e 74  re inserting int
2702b 6f 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  o */.  Select *p
2702c 53 65 6c 65 63 74 2c 20 20 20 20 20 20 2f 2a 20  Select,      /* 
2702d 41 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  A SELECT stateme
2702e 6e 74 20 74 6f 20 75 73 65 20 61 73 20 74 68 65  nt to use as the
2702f 20 64 61 74 61 20 73 6f 75 72 63 65 20 2a 2f 0a   data source */.
27030 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 2c 20 20    int onError,  
27031 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 74          /* How t
27032 6f 20 68 61 6e 64 6c 65 20 63 6f 6e 73 74 72 61  o handle constra
27033 69 6e 74 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20  int errors */.  
27034 69 6e 74 20 69 44 62 44 65 73 74 20 20 20 20 20  int iDbDest     
27035 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
27036 61 62 61 73 65 20 6f 66 20 70 44 65 73 74 20 2a  abase of pDest *
27037 2f 0a 29 7b 0a 20 20 45 78 70 72 4c 69 73 74 20  /.){.  ExprList 
27038 2a 70 45 4c 69 73 74 3b 20 20 20 20 20 20 20 20  *pEList;        
27039 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72          /* The r
2703a 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65  esult set of the
2703b 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 54 61 62   SELECT */.  Tab
2703c 6c 65 20 2a 70 53 72 63 3b 20 20 20 20 20 20 20  le *pSrc;       
2703d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2703e 20 54 68 65 20 74 61 62 6c 65 20 69 6e 20 74 68   The table in th
2703f 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66  e FROM clause of
27040 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 49 6e 64   SELECT */.  Ind
27041 65 78 20 2a 70 53 72 63 49 64 78 2c 20 2a 70 44  ex *pSrcIdx, *pD
27042 65 73 74 49 64 78 3b 20 20 20 20 20 20 20 2f 2a  estIdx;       /*
27043 20 53 6f 75 72 63 65 20 61 6e 64 20 64 65 73 74   Source and dest
27044 69 6e 61 74 69 6f 6e 20 69 6e 64 69 63 65 73 20  ination indices 
27045 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c  */.  struct SrcL
27046 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
27047 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 6c 65 6d        /* An elem
27048 65 6e 74 20 6f 66 20 70 53 65 6c 65 63 74 2d 3e  ent of pSelect->
27049 70 53 72 63 20 2a 2f 0a 20 20 69 6e 74 20 69 3b  pSrc */.  int i;
2704a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2704b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
2704c 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
2704d 69 6e 74 20 69 44 62 53 72 63 3b 20 20 20 20 20  int iDbSrc;     
2704e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2704f 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
27050 20 6f 66 20 70 53 72 63 20 2a 2f 0a 20 20 69 6e   of pSrc */.  in
27051 74 20 69 53 72 63 2c 20 69 44 65 73 74 3b 20 20  t iSrc, iDest;  
27052 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
27053 2a 20 43 75 72 73 6f 72 73 20 66 72 6f 6d 20 73  * Cursors from s
27054 6f 75 72 63 65 20 61 6e 64 20 64 65 73 74 69 6e  ource and destin
27055 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 61  ation */.  int a
27056 64 64 72 31 2c 20 61 64 64 72 32 3b 20 20 20 20  ddr1, addr2;    
27057 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
27058 6f 6f 70 20 61 64 64 72 65 73 73 65 73 20 2a 2f  oop addresses */
27059 0a 20 20 69 6e 74 20 65 6d 70 74 79 44 65 73 74  .  int emptyDest
2705a 54 65 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  Test;           
2705b 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
2705c 66 20 74 65 73 74 20 66 6f 72 20 65 6d 70 74 79  f test for empty
2705d 20 70 44 65 73 74 20 2a 2f 0a 20 20 69 6e 74 20   pDest */.  int 
2705e 65 6d 70 74 79 53 72 63 54 65 73 74 3b 20 20 20  emptySrcTest;   
2705f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
27060 41 64 64 72 65 73 73 20 6f 66 20 74 65 73 74 20  Address of test 
27061 66 6f 72 20 65 6d 70 74 79 20 70 53 72 63 20 2a  for empty pSrc *
27062 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20  /.  Vdbe *v;    
27063 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27064 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45       /* The VDBE
27065 20 77 65 20 61 72 65 20 62 75 69 6c 64 69 6e 67   we are building
27066 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70   */.  KeyInfo *p
27067 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 20  Key;            
27068 20 20 20 20 20 20 20 2f 2a 20 4b 65 79 20 69 6e         /* Key in
27069 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 6e  formation for an
2706a 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20   index */.  int 
2706b 72 65 67 41 75 74 6f 69 6e 63 3b 20 20 20 20 20  regAutoinc;     
2706c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2706d 4d 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 20  Memory register 
2706e 75 73 65 64 20 62 79 20 41 55 54 4f 49 4e 43 20  used by AUTOINC 
2706f 2a 2f 0a 20 20 69 6e 74 20 64 65 73 74 48 61 73  */.  int destHas
27070 55 6e 69 71 75 65 49 64 78 20 3d 20 30 3b 20 20  UniqueIdx = 0;  
27071 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
27072 20 70 44 65 73 74 20 68 61 73 20 61 20 55 4e 49   pDest has a UNI
27073 51 55 45 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69  QUE index */.  i
27074 6e 74 20 72 65 67 44 61 74 61 2c 20 72 65 67 52  nt regData, regR
27075 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20 20 20  owid;           
27076 2f 2a 20 52 65 67 69 73 74 65 72 73 20 68 6f 6c  /* Registers hol
27077 64 69 6e 67 20 64 61 74 61 20 61 6e 64 20 72 6f  ding data and ro
27078 77 69 64 20 2a 2f 0a 0a 20 20 69 66 28 20 70 53  wid */..  if( pS
27079 65 6c 65 63 74 3d 3d 30 20 29 7b 0a 20 20 20 20  elect==0 ){.    
2707a 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 4d  return 0;   /* M
2707b 75 73 74 20 62 65 20 6f 66 20 74 68 65 20 66 6f  ust be of the fo
2707c 72 6d 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20  rm  INSERT INTO 
2707d 2e 2e 2e 20 53 45 4c 45 43 54 20 2e 2e 2e 20 2a  ... SELECT ... *
2707e 2f 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69  /.  }.  if( sqli
2707f 74 65 33 54 72 69 67 67 65 72 4c 69 73 74 28 70  te3TriggerList(p
27080 50 61 72 73 65 2c 20 70 44 65 73 74 29 20 29 7b  Parse, pDest) ){
27081 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20  .    return 0;  
27082 20 2f 2a 20 74 61 62 31 20 6d 75 73 74 20 6e 6f   /* tab1 must no
27083 74 20 68 61 76 65 20 74 72 69 67 67 65 72 73 20  t have triggers 
27084 2a 2f 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  */.  }.#ifndef S
27085 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
27086 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 70 44  ALTABLE.  if( pD
27087 65 73 74 2d 3e 74 61 62 46 6c 61 67 73 20 26 20  est->tabFlags & 
27088 54 46 5f 56 69 72 74 75 61 6c 20 29 7b 0a 20 20  TF_Virtual ){.  
27089 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a    return 0;   /*
2708a 20 74 61 62 31 20 6d 75 73 74 20 6e 6f 74 20 62   tab1 must not b
2708b 65 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  e a virtual tabl
2708c 65 20 2a 2f 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  e */.  }.#endif.
2708d 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f    if( onError==O
2708e 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20 20 20  E_Default ){.   
2708f 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62   onError = OE_Ab
27090 6f 72 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6f  ort;.  }.  if( o
27091 6e 45 72 72 6f 72 21 3d 4f 45 5f 41 62 6f 72 74  nError!=OE_Abort
27092 20 26 26 20 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f   && onError!=OE_
27093 52 6f 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20  Rollback ){.    
27094 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 43  return 0;   /* C
27095 61 6e 6e 6f 74 20 64 6f 20 4f 52 20 52 45 50 4c  annot do OR REPL
27096 41 43 45 20 6f 72 20 4f 52 20 49 47 4e 4f 52 45  ACE or OR IGNORE
27097 20 6f 72 20 4f 52 20 46 41 49 4c 20 2a 2f 0a 20   or OR FAIL */. 
27098 20 7d 0a 20 20 61 73 73 65 72 74 28 70 53 65 6c   }.  assert(pSel
27099 65 63 74 2d 3e 70 53 72 63 29 3b 20 20 20 2f 2a  ect->pSrc);   /*
2709a 20 61 6c 6c 6f 63 61 74 65 64 20 65 76 65 6e 20   allocated even 
2709b 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 46  if there is no F
2709c 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ROM clause */.  
2709d 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 70 53 72  if( pSelect->pSr
2709e 63 2d 3e 6e 53 72 63 21 3d 31 20 29 7b 0a 20 20  c->nSrc!=1 ){.  
2709f 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a    return 0;   /*
270a0 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6d 75 73   FROM clause mus
270a1 74 20 68 61 76 65 20 65 78 61 63 74 6c 79 20 6f  t have exactly o
270a2 6e 65 20 74 65 72 6d 20 2a 2f 0a 20 20 7d 0a 20  ne term */.  }. 
270a3 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 70 53   if( pSelect->pS
270a4 72 63 2d 3e 61 5b 30 5d 2e 70 53 65 6c 65 63 74  rc->a[0].pSelect
270a5 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
270a6 3b 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75  ;   /* FROM clau
270a7 73 65 20 63 61 6e 6e 6f 74 20 63 6f 6e 74 61 69  se cannot contai
270a8 6e 20 61 20 73 75 62 71 75 65 72 79 20 2a 2f 0a  n a subquery */.
270a9 20 20 7d 0a 20 20 69 66 28 20 70 53 65 6c 65 63    }.  if( pSelec
270aa 74 2d 3e 70 57 68 65 72 65 20 29 7b 0a 20 20 20  t->pWhere ){.   
270ab 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
270ac 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20 68  SELECT may not h
270ad 61 76 65 20 61 20 57 48 45 52 45 20 63 6c 61 75  ave a WHERE clau
270ae 73 65 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20  se */.  }.  if( 
270af 70 53 65 6c 65 63 74 2d 3e 70 4f 72 64 65 72 42  pSelect->pOrderB
270b0 79 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  y ){.    return 
270b1 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 6d  0;   /* SELECT m
270b2 61 79 20 6e 6f 74 20 68 61 76 65 20 61 6e 20 4f  ay not have an O
270b3 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a  RDER BY clause *
270b4 2f 0a 20 20 7d 0a 20 20 2f 2a 20 44 6f 20 6e 6f  /.  }.  /* Do no
270b5 74 20 6e 65 65 64 20 74 6f 20 74 65 73 74 20 66  t need to test f
270b6 6f 72 20 61 20 48 41 56 49 4e 47 20 63 6c 61 75  or a HAVING clau
270b7 73 65 2e 20 20 49 66 20 48 41 56 49 4e 47 20 69  se.  If HAVING i
270b8 73 20 70 72 65 73 65 6e 74 20 62 75 74 0a 20 20  s present but.  
270b9 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 4f  ** there is no O
270ba 52 44 45 52 20 42 59 2c 20 77 65 20 77 69 6c 6c  RDER BY, we will
270bb 20 67 65 74 20 61 6e 20 65 72 72 6f 72 2e 20 2a   get an error. *
270bc 2f 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d  /.  if( pSelect-
270bd 3e 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20  >pGroupBy ){.   
270be 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
270bf 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20 68  SELECT may not h
270c0 61 76 65 20 61 20 47 52 4f 55 50 20 42 59 20 63  ave a GROUP BY c
270c1 6c 61 75 73 65 20 2a 2f 0a 20 20 7d 0a 20 20 69  lause */.  }.  i
270c2 66 28 20 70 53 65 6c 65 63 74 2d 3e 70 4c 69 6d  f( pSelect->pLim
270c3 69 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  it ){.    return
270c4 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54 20   0;   /* SELECT 
270c5 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 61 20 4c  may not have a L
270c6 49 4d 49 54 20 63 6c 61 75 73 65 20 2a 2f 0a 20  IMIT clause */. 
270c7 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 53 65   }.  assert( pSe
270c8 6c 65 63 74 2d 3e 70 4f 66 66 73 65 74 3d 3d 30  lect->pOffset==0
270c9 20 29 3b 20 20 2f 2a 20 4d 75 73 74 20 62 65 20   );  /* Must be 
270ca 73 6f 20 69 66 20 70 4c 69 6d 69 74 3d 3d 30 20  so if pLimit==0 
270cb 2a 2f 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74  */.  if( pSelect
270cc 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20 20 20  ->pPrior ){.    
270cd 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 53  return 0;   /* S
270ce 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20 62 65  ELECT may not be
270cf 20 61 20 63 6f 6d 70 6f 75 6e 64 20 71 75 65 72   a compound quer
270d0 79 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70  y */.  }.  if( p
270d1 53 65 6c 65 63 74 2d 3e 73 65 6c 46 6c 61 67 73  Select->selFlags
270d2 20 26 20 53 46 5f 44 69 73 74 69 6e 63 74 20 29   & SF_Distinct )
270d3 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  {.    return 0; 
270d4 20 20 2f 2a 20 53 45 4c 45 43 54 20 6d 61 79 20    /* SELECT may 
270d5 6e 6f 74 20 62 65 20 44 49 53 54 49 4e 43 54 20  not be DISTINCT 
270d6 2a 2f 0a 20 20 7d 0a 20 20 70 45 4c 69 73 74 20  */.  }.  pEList 
270d7 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73  = pSelect->pELis
270d8 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 4c  t;.  assert( pEL
270d9 69 73 74 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  ist!=0 );.  if( 
270da 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31  pEList->nExpr!=1
270db 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
270dc 3b 20 20 20 2f 2a 20 54 68 65 20 72 65 73 75 6c  ;   /* The resul
270dd 74 20 73 65 74 20 6d 75 73 74 20 68 61 76 65 20  t set must have 
270de 65 78 61 63 74 6c 79 20 6f 6e 65 20 63 6f 6c 75  exactly one colu
270df 6d 6e 20 2a 2f 0a 20 20 7d 0a 20 20 61 73 73 65  mn */.  }.  asse
270e0 72 74 28 20 70 45 4c 69 73 74 2d 3e 61 5b 30 5d  rt( pEList->a[0]
270e1 2e 70 45 78 70 72 20 29 3b 0a 20 20 69 66 28 20  .pExpr );.  if( 
270e2 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78  pEList->a[0].pEx
270e3 70 72 2d 3e 6f 70 21 3d 54 4b 5f 41 4c 4c 20 29  pr->op!=TK_ALL )
270e4 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  {.    return 0; 
270e5 20 20 2f 2a 20 54 68 65 20 72 65 73 75 6c 74 20    /* The result 
270e6 73 65 74 20 6d 75 73 74 20 62 65 20 74 68 65 20  set must be the 
270e7 73 70 65 63 69 61 6c 20 6f 70 65 72 61 74 6f 72  special operator
270e8 20 22 2a 22 20 2a 2f 0a 20 20 7d 0a 0a 20 20 2f   "*" */.  }..  /
270e9 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20  * At this point 
270ea 77 65 20 68 61 76 65 20 65 73 74 61 62 6c 69 73  we have establis
270eb 68 65 64 20 74 68 61 74 20 74 68 65 20 73 74 61  hed that the sta
270ec 74 65 6d 65 6e 74 20 69 73 20 6f 66 20 74 68 65  tement is of the
270ed 0a 20 20 2a 2a 20 63 6f 72 72 65 63 74 20 73 79  .  ** correct sy
270ee 6e 74 61 63 74 69 63 20 66 6f 72 6d 20 74 6f 20  ntactic form to 
270ef 70 61 72 74 69 63 69 70 61 74 65 20 69 6e 20 74  participate in t
270f0 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  his optimization
270f1 2e 20 20 4e 6f 77 0a 20 20 2a 2a 20 77 65 20 68  .  Now.  ** we h
270f2 61 76 65 20 74 6f 20 63 68 65 63 6b 20 74 68 65  ave to check the
270f3 20 73 65 6d 61 6e 74 69 63 73 2e 0a 20 20 2a 2f   semantics..  */
270f4 0a 20 20 70 49 74 65 6d 20 3d 20 70 53 65 6c 65  .  pItem = pSele
270f5 63 74 2d 3e 70 53 72 63 2d 3e 61 3b 0a 20 20 70  ct->pSrc->a;.  p
270f6 53 72 63 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63  Src = sqlite3Loc
270f7 61 74 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c  ateTable(pParse,
270f8 20 30 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65   0, pItem->zName
270f9 2c 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61  , pItem->zDataba
270fa 73 65 29 3b 0a 20 20 69 66 28 20 70 53 72 63 3d  se);.  if( pSrc=
270fb 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
270fc 20 30 3b 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c   0;   /* FROM cl
270fd 61 75 73 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f  ause does not co
270fe 6e 74 61 69 6e 20 61 20 72 65 61 6c 20 74 61 62  ntain a real tab
270ff 6c 65 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20  le */.  }.  if( 
27100 70 53 72 63 3d 3d 70 44 65 73 74 20 29 7b 0a 20  pSrc==pDest ){. 
27101 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
27102 2a 20 74 61 62 31 20 61 6e 64 20 74 61 62 32 20  * tab1 and tab2 
27103 6d 61 79 20 6e 6f 74 20 62 65 20 74 68 65 20 73  may not be the s
27104 61 6d 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 7d  ame table */.  }
27105 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
27106 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
27107 45 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 74 61  E.  if( pSrc->ta
27108 62 46 6c 61 67 73 20 26 20 54 46 5f 56 69 72 74  bFlags & TF_Virt
27109 75 61 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ual ){.    retur
2710a 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62 32 20 6d  n 0;   /* tab2 m
2710b 75 73 74 20 6e 6f 74 20 62 65 20 61 20 76 69 72  ust not be a vir
2710c 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20  tual table */.  
2710d 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70  }.#endif.  if( p
2710e 53 72 63 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a  Src->pSelect ){.
2710f 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
27110 2f 2a 20 74 61 62 32 20 6d 61 79 20 6e 6f 74 20  /* tab2 may not 
27111 62 65 20 61 20 76 69 65 77 20 2a 2f 0a 20 20 7d  be a view */.  }
27112 0a 20 20 69 66 28 20 70 44 65 73 74 2d 3e 6e 43  .  if( pDest->nC
27113 6f 6c 21 3d 70 53 72 63 2d 3e 6e 43 6f 6c 20 29  ol!=pSrc->nCol )
27114 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  {.    return 0; 
27115 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
27116 6f 6c 75 6d 6e 73 20 6d 75 73 74 20 62 65 20 74  olumns must be t
27117 68 65 20 73 61 6d 65 20 69 6e 20 74 61 62 31 20  he same in tab1 
27118 61 6e 64 20 74 61 62 32 20 2a 2f 0a 20 20 7d 0a  and tab2 */.  }.
27119 20 20 69 66 28 20 70 44 65 73 74 2d 3e 69 50 4b    if( pDest->iPK
2711a 65 79 21 3d 70 53 72 63 2d 3e 69 50 4b 65 79 20  ey!=pSrc->iPKey 
2711b 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
2711c 20 20 20 2f 2a 20 42 6f 74 68 20 74 61 62 6c 65     /* Both table
2711d 73 20 6d 75 73 74 20 68 61 76 65 20 74 68 65 20  s must have the 
2711e 73 61 6d 65 20 49 4e 54 45 47 45 52 20 50 52 49  same INTEGER PRI
2711f 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20 7d 0a  MARY KEY */.  }.
27120 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 44 65    for(i=0; i<pDe
27121 73 74 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a  st->nCol; i++){.
27122 20 20 20 20 69 66 28 20 70 44 65 73 74 2d 3e 61      if( pDest->a
27123 43 6f 6c 5b 69 5d 2e 61 66 66 69 6e 69 74 79 21  Col[i].affinity!
27124 3d 70 53 72 63 2d 3e 61 43 6f 6c 5b 69 5d 2e 61  =pSrc->aCol[i].a
27125 66 66 69 6e 69 74 79 20 29 7b 0a 20 20 20 20 20  ffinity ){.     
27126 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 2f 2a   return 0;    /*
27127 20 41 66 66 69 6e 69 74 79 20 6d 75 73 74 20 62   Affinity must b
27128 65 20 74 68 65 20 73 61 6d 65 20 6f 6e 20 61 6c  e the same on al
27129 6c 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 20  l columns */.   
2712a 20 7d 0a 20 20 20 20 69 66 28 20 21 78 66 65 72   }.    if( !xfer
2712b 43 6f 6d 70 61 74 69 62 6c 65 43 6f 6c 6c 61 74  CompatibleCollat
2712c 69 6f 6e 28 70 44 65 73 74 2d 3e 61 43 6f 6c 5b  ion(pDest->aCol[
2712d 69 5d 2e 7a 43 6f 6c 6c 2c 20 70 53 72 63 2d 3e  i].zColl, pSrc->
2712e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c 29 20 29  aCol[i].zColl) )
2712f 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  {.      return 0
27130 3b 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e  ;    /* Collatin
27131 67 20 73 65 71 75 65 6e 63 65 20 6d 75 73 74 20  g sequence must 
27132 62 65 20 74 68 65 20 73 61 6d 65 20 6f 6e 20 61  be the same on a
27133 6c 6c 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20  ll columns */.  
27134 20 20 7d 0a 20 20 20 20 69 66 28 20 70 44 65 73    }.    if( pDes
27135 74 2d 3e 61 43 6f 6c 5b 69 5d 2e 6e 6f 74 4e 75  t->aCol[i].notNu
27136 6c 6c 20 26 26 20 21 70 53 72 63 2d 3e 61 43 6f  ll && !pSrc->aCo
27137 6c 5b 69 5d 2e 6e 6f 74 4e 75 6c 6c 20 29 7b 0a  l[i].notNull ){.
27138 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20        return 0; 
27139 20 20 20 2f 2a 20 74 61 62 32 20 6d 75 73 74 20     /* tab2 must 
2713a 62 65 20 4e 4f 54 20 4e 55 4c 4c 20 69 66 20 74  be NOT NULL if t
2713b 61 62 31 20 69 73 20 2a 2f 0a 20 20 20 20 7d 0a  ab1 is */.    }.
2713c 20 20 7d 0a 20 20 66 6f 72 28 70 44 65 73 74 49    }.  for(pDestI
2713d 64 78 3d 70 44 65 73 74 2d 3e 70 49 6e 64 65 78  dx=pDest->pIndex
2713e 3b 20 70 44 65 73 74 49 64 78 3b 20 70 44 65 73  ; pDestIdx; pDes
2713f 74 49 64 78 3d 70 44 65 73 74 49 64 78 2d 3e 70  tIdx=pDestIdx->p
27140 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70  Next){.    if( p
27141 44 65 73 74 49 64 78 2d 3e 6f 6e 45 72 72 6f 72  DestIdx->onError
27142 21 3d 4f 45 5f 4e 6f 6e 65 20 29 7b 0a 20 20 20  !=OE_None ){.   
27143 20 20 20 64 65 73 74 48 61 73 55 6e 69 71 75 65     destHasUnique
27144 49 64 78 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  Idx = 1;.    }. 
27145 20 20 20 66 6f 72 28 70 53 72 63 49 64 78 3d 70     for(pSrcIdx=p
27146 53 72 63 2d 3e 70 49 6e 64 65 78 3b 20 70 53 72  Src->pIndex; pSr
27147 63 49 64 78 3b 20 70 53 72 63 49 64 78 3d 70 53  cIdx; pSrcIdx=pS
27148 72 63 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20  rcIdx->pNext){. 
27149 20 20 20 20 20 69 66 28 20 78 66 65 72 43 6f 6d       if( xferCom
2714a 70 61 74 69 62 6c 65 49 6e 64 65 78 28 70 44 65  patibleIndex(pDe
2714b 73 74 49 64 78 2c 20 70 53 72 63 49 64 78 29 20  stIdx, pSrcIdx) 
2714c 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20  ) break;.    }. 
2714d 20 20 20 69 66 28 20 70 53 72 63 49 64 78 3d 3d     if( pSrcIdx==
2714e 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  0 ){.      retur
2714f 6e 20 30 3b 20 20 20 20 2f 2a 20 70 44 65 73 74  n 0;    /* pDest
27150 49 64 78 20 68 61 73 20 6e 6f 20 63 6f 72 72 65  Idx has no corre
27151 73 70 6f 6e 64 69 6e 67 20 69 6e 64 65 78 20 69  sponding index i
27152 6e 20 70 53 72 63 20 2a 2f 0a 20 20 20 20 7d 0a  n pSrc */.    }.
27153 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
27154 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20  TE_OMIT_CHECK.  
27155 69 66 28 20 70 44 65 73 74 2d 3e 70 43 68 65 63  if( pDest->pChec
27156 6b 20 26 26 20 21 73 71 6c 69 74 65 33 45 78 70  k && !sqlite3Exp
27157 72 43 6f 6d 70 61 72 65 28 70 53 72 63 2d 3e 70  rCompare(pSrc->p
27158 43 68 65 63 6b 2c 20 70 44 65 73 74 2d 3e 70 43  Check, pDest->pC
27159 68 65 63 6b 29 20 29 7b 0a 20 20 20 20 72 65 74  heck) ){.    ret
2715a 75 72 6e 20 30 3b 20 20 20 2f 2a 20 54 61 62 6c  urn 0;   /* Tabl
2715b 65 73 20 68 61 76 65 20 64 69 66 66 65 72 65 6e  es have differen
2715c 74 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69  t CHECK constrai
2715d 6e 74 73 2e 20 20 54 69 63 6b 65 74 20 23 32 32  nts.  Ticket #22
2715e 35 32 20 2a 2f 0a 20 20 7d 0a 23 65 6e 64 69 66  52 */.  }.#endif
2715f 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 67 65 74  ..  /* If we get
27160 20 74 68 69 73 20 66 61 72 2c 20 69 74 20 6d 65   this far, it me
27161 61 6e 73 20 65 69 74 68 65 72 3a 0a 20 20 2a 2a  ans either:.  **
27162 0a 20 20 2a 2a 20 20 20 20 2a 20 20 20 57 65 20  .  **    *   We 
27163 63 61 6e 20 61 6c 77 61 79 73 20 64 6f 20 74 68  can always do th
27164 65 20 74 72 61 6e 73 66 65 72 20 69 66 20 74 68  e transfer if th
27165 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 73  e table contains
27166 20 61 6e 0a 20 20 2a 2a 20 20 20 20 20 20 20 20   an.  **        
27167 61 6e 20 69 6e 74 65 67 65 72 20 70 72 69 6d 61  an integer prima
27168 72 79 20 6b 65 79 0a 20 20 2a 2a 0a 20 20 2a 2a  ry key.  **.  **
27169 20 20 20 20 2a 20 20 20 57 65 20 63 61 6e 20 63      *   We can c
2716a 6f 6e 64 69 74 69 6f 6e 61 6c 6c 79 20 64 6f 20  onditionally do 
2716b 74 68 65 20 74 72 61 6e 73 66 65 72 20 69 66 20  the transfer if 
2716c 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 0a  the destination.
2716d 20 20 2a 2a 20 20 20 20 20 20 20 20 74 61 62 6c    **        tabl
2716e 65 20 69 73 20 65 6d 70 74 79 2e 0a 20 20 2a 2f  e is empty..  */
2716f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
27170 45 53 54 0a 20 20 73 71 6c 69 74 65 33 5f 78 66  EST.  sqlite3_xf
27171 65 72 6f 70 74 5f 63 6f 75 6e 74 2b 2b 3b 0a 23  eropt_count++;.#
27172 65 6e 64 69 66 0a 20 20 69 44 62 53 72 63 20 3d  endif.  iDbSrc =
27173 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
27174 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62  Index(pParse->db
27175 2c 20 70 53 72 63 2d 3e 70 53 63 68 65 6d 61 29  , pSrc->pSchema)
27176 3b 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  ;.  v = sqlite3G
27177 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
27178 20 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72    sqlite3CodeVer
27179 69 66 79 53 63 68 65 6d 61 28 70 50 61 72 73 65  ifySchema(pParse
2717a 2c 20 69 44 62 53 72 63 29 3b 0a 20 20 69 53 72  , iDbSrc);.  iSr
2717b 63 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  c = pParse->nTab
2717c 2b 2b 3b 0a 20 20 69 44 65 73 74 20 3d 20 70 50  ++;.  iDest = pP
2717d 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20  arse->nTab++;.  
2717e 72 65 67 41 75 74 6f 69 6e 63 20 3d 20 61 75 74  regAutoinc = aut
2717f 6f 49 6e 63 42 65 67 69 6e 28 70 50 61 72 73 65  oIncBegin(pParse
27180 2c 20 69 44 62 44 65 73 74 2c 20 70 44 65 73 74  , iDbDest, pDest
27181 29 3b 0a 20 20 73 71 6c 69 74 65 33 4f 70 65 6e  );.  sqlite3Open
27182 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44  Table(pParse, iD
27183 65 73 74 2c 20 69 44 62 44 65 73 74 2c 20 70 44  est, iDbDest, pD
27184 65 73 74 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74  est, OP_OpenWrit
27185 65 29 3b 0a 20 20 69 66 28 20 28 70 44 65 73 74  e);.  if( (pDest
27186 2d 3e 69 50 4b 65 79 3c 30 20 26 26 20 70 44 65  ->iPKey<0 && pDe
27187 73 74 2d 3e 70 49 6e 64 65 78 21 3d 30 29 20 7c  st->pIndex!=0) |
27188 7c 20 64 65 73 74 48 61 73 55 6e 69 71 75 65 49  | destHasUniqueI
27189 64 78 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20  dx ){.    /* If 
2718a 74 61 62 6c 65 73 20 64 6f 20 6e 6f 74 20 68 61  tables do not ha
2718b 76 65 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52  ve an INTEGER PR
2718c 49 4d 41 52 59 20 4b 45 59 20 61 6e 64 20 74 68  IMARY KEY and th
2718d 65 72 65 0a 20 20 20 20 2a 2a 20 61 72 65 20 69  ere.    ** are i
2718e 6e 64 69 63 65 73 20 74 6f 20 62 65 20 63 6f 70  ndices to be cop
2718f 69 65 64 20 61 6e 64 20 74 68 65 20 64 65 73 74  ied and the dest
27190 69 6e 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20 65  ination is not e
27191 6d 70 74 79 2c 0a 20 20 20 20 2a 2a 20 77 65 20  mpty,.    ** we 
27192 68 61 76 65 20 74 6f 20 64 69 73 61 6c 6c 6f 77  have to disallow
27193 20 74 68 65 20 74 72 61 6e 73 66 65 72 20 6f 70   the transfer op
27194 74 69 6d 69 7a 61 74 69 6f 6e 20 62 65 63 61 75  timization becau
27195 73 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 68  se the.    ** th
27196 65 20 72 6f 77 69 64 73 20 6d 69 67 68 74 20 63  e rowids might c
27197 68 61 6e 67 65 20 77 68 69 63 68 20 77 69 6c 6c  hange which will
27198 20 6d 65 73 73 20 75 70 20 69 6e 64 65 78 69 6e   mess up indexin
27199 67 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  g..    **.    **
2719a 20 4f 72 20 69 66 20 74 68 65 20 64 65 73 74 69   Or if the desti
2719b 6e 61 74 69 6f 6e 20 68 61 73 20 61 20 55 4e 49  nation has a UNI
2719c 51 55 45 20 69 6e 64 65 78 20 61 6e 64 20 69 73  QUE index and is
2719d 20 6e 6f 74 20 65 6d 70 74 79 2c 0a 20 20 20 20   not empty,.    
2719e 2a 2a 20 77 65 20 61 6c 73 6f 20 64 69 73 61 6c  ** we also disal
2719f 6c 6f 77 20 74 68 65 20 74 72 61 6e 73 66 65 72  low the transfer
271a0 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 62 65   optimization be
271a1 63 61 75 73 65 20 77 65 20 63 61 6e 6e 6f 74 0a  cause we cannot.
271a2 20 20 20 20 2a 2a 20 69 6e 73 75 72 65 20 74 68      ** insure th
271a3 61 74 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 69  at all entries i
271a4 6e 20 74 68 65 20 75 6e 69 6f 6e 20 6f 66 20 44  n the union of D
271a5 45 53 54 20 61 6e 64 20 53 52 43 20 77 69 6c 6c  EST and SRC will
271a6 20 62 65 0a 20 20 20 20 2a 2a 20 75 6e 69 71 75   be.    ** uniqu
271a7 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 64  e..    */.    ad
271a8 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62  dr1 = sqlite3Vdb
271a9 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
271aa 77 69 6e 64 2c 20 69 44 65 73 74 2c 20 30 29 3b  wind, iDest, 0);
271ab 0a 20 20 20 20 65 6d 70 74 79 44 65 73 74 54 65  .    emptyDestTe
271ac 73 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  st = sqlite3Vdbe
271ad 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74  AddOp2(v, OP_Got
271ae 6f 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  o, 0, 0);.    sq
271af 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
271b0 65 28 76 2c 20 61 64 64 72 31 29 3b 0a 20 20 7d  e(v, addr1);.  }
271b1 65 6c 73 65 7b 0a 20 20 20 20 65 6d 70 74 79 44  else{.    emptyD
271b2 65 73 74 54 65 73 74 20 3d 20 30 3b 0a 20 20 7d  estTest = 0;.  }
271b3 0a 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61  .  sqlite3OpenTa
271b4 62 6c 65 28 70 50 61 72 73 65 2c 20 69 53 72 63  ble(pParse, iSrc
271b5 2c 20 69 44 62 53 72 63 2c 20 70 53 72 63 2c 20  , iDbSrc, pSrc, 
271b6 4f 50 5f 4f 70 65 6e 52 65 61 64 29 3b 0a 20 20  OP_OpenRead);.  
271b7 65 6d 70 74 79 53 72 63 54 65 73 74 20 3d 20 73  emptySrcTest = s
271b8 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
271b9 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69  (v, OP_Rewind, i
271ba 53 72 63 2c 20 30 29 3b 0a 20 20 72 65 67 44 61  Src, 0);.  regDa
271bb 74 61 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  ta = sqlite3GetT
271bc 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
271bd 20 20 72 65 67 52 6f 77 69 64 20 3d 20 73 71 6c    regRowid = sql
271be 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
271bf 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 70 44  Parse);.  if( pD
271c0 65 73 74 2d 3e 69 50 4b 65 79 3e 3d 30 20 29 7b  est->iPKey>=0 ){
271c1 0a 20 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c  .    addr1 = sql
271c2 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
271c3 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69 53 72 63  , OP_Rowid, iSrc
271c4 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20  , regRowid);.   
271c5 20 61 64 64 72 32 20 3d 20 73 71 6c 69 74 65 33   addr2 = sqlite3
271c6 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
271c7 5f 4e 6f 74 45 78 69 73 74 73 2c 20 69 44 65 73  _NotExists, iDes
271c8 74 2c 20 30 2c 20 72 65 67 52 6f 77 69 64 29 3b  t, 0, regRowid);
271c9 0a 20 20 20 20 73 71 6c 69 74 65 33 48 61 6c 74  .    sqlite3Halt
271ca 43 6f 6e 73 74 72 61 69 6e 74 28 0a 20 20 20 20  Constraint(.    
271cb 20 20 20 20 70 50 61 72 73 65 2c 20 6f 6e 45 72      pParse, onEr
271cc 72 6f 72 2c 20 22 50 52 49 4d 41 52 59 20 4b 45  ror, "PRIMARY KE
271cd 59 20 6d 75 73 74 20 62 65 20 75 6e 69 71 75 65  Y must be unique
271ce 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20  ", P4_STATIC);. 
271cf 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
271d0 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 32 29  mpHere(v, addr2)
271d1 3b 0a 20 20 20 20 61 75 74 6f 49 6e 63 53 74 65  ;.    autoIncSte
271d2 70 28 70 50 61 72 73 65 2c 20 72 65 67 41 75 74  p(pParse, regAut
271d3 6f 69 6e 63 2c 20 72 65 67 52 6f 77 69 64 29 3b  oinc, regRowid);
271d4 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 44 65  .  }else if( pDe
271d5 73 74 2d 3e 70 49 6e 64 65 78 3d 3d 30 20 29 7b  st->pIndex==0 ){
271d6 0a 20 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c  .    addr1 = sql
271d7 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
271d8 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 69  , OP_NewRowid, i
271d9 44 65 73 74 2c 20 72 65 67 52 6f 77 69 64 29 3b  Dest, regRowid);
271da 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 64  .  }else{.    ad
271db 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62  dr1 = sqlite3Vdb
271dc 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f  eAddOp2(v, OP_Ro
271dd 77 69 64 2c 20 69 53 72 63 2c 20 72 65 67 52 6f  wid, iSrc, regRo
271de 77 69 64 29 3b 0a 20 20 20 20 61 73 73 65 72 74  wid);.    assert
271df 28 20 28 70 44 65 73 74 2d 3e 74 61 62 46 6c 61  ( (pDest->tabFla
271e0 67 73 20 26 20 54 46 5f 41 75 74 6f 69 6e 63 72  gs & TF_Autoincr
271e1 65 6d 65 6e 74 29 3d 3d 30 20 29 3b 0a 20 20 7d  ement)==0 );.  }
271e2 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
271e3 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 44 61  dOp2(v, OP_RowDa
271e4 74 61 2c 20 69 53 72 63 2c 20 72 65 67 44 61 74  ta, iSrc, regDat
271e5 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  a);.  sqlite3Vdb
271e6 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e  eAddOp3(v, OP_In
271e7 73 65 72 74 2c 20 69 44 65 73 74 2c 20 72 65 67  sert, iDest, reg
271e8 44 61 74 61 2c 20 72 65 67 52 6f 77 69 64 29 3b  Data, regRowid);
271e9 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  .  sqlite3VdbeCh
271ea 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47  angeP5(v, OPFLAG
271eb 5f 4e 43 48 41 4e 47 45 7c 4f 50 46 4c 41 47 5f  _NCHANGE|OPFLAG_
271ec 4c 41 53 54 52 4f 57 49 44 7c 4f 50 46 4c 41 47  LASTROWID|OPFLAG
271ed 5f 41 50 50 45 4e 44 29 3b 0a 20 20 73 71 6c 69  _APPEND);.  sqli
271ee 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
271ef 76 2c 20 2d 31 2c 20 70 44 65 73 74 2d 3e 7a 4e  v, -1, pDest->zN
271f0 61 6d 65 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74  ame, 0);.  sqlit
271f1 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
271f2 4f 50 5f 4e 65 78 74 2c 20 69 53 72 63 2c 20 61  OP_Next, iSrc, a
271f3 64 64 72 31 29 3b 0a 20 20 66 6f 72 28 70 44 65  ddr1);.  for(pDe
271f4 73 74 49 64 78 3d 70 44 65 73 74 2d 3e 70 49 6e  stIdx=pDest->pIn
271f5 64 65 78 3b 20 70 44 65 73 74 49 64 78 3b 20 70  dex; pDestIdx; p
271f6 44 65 73 74 49 64 78 3d 70 44 65 73 74 49 64 78  DestIdx=pDestIdx
271f7 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 66 6f  ->pNext){.    fo
271f8 72 28 70 53 72 63 49 64 78 3d 70 53 72 63 2d 3e  r(pSrcIdx=pSrc->
271f9 70 49 6e 64 65 78 3b 20 41 4c 57 41 59 53 28 70  pIndex; ALWAYS(p
271fa 53 72 63 49 64 78 29 3b 20 70 53 72 63 49 64 78  SrcIdx); pSrcIdx
271fb 3d 70 53 72 63 49 64 78 2d 3e 70 4e 65 78 74 29  =pSrcIdx->pNext)
271fc 7b 0a 20 20 20 20 20 20 69 66 28 20 78 66 65 72  {.      if( xfer
271fd 43 6f 6d 70 61 74 69 62 6c 65 49 6e 64 65 78 28  CompatibleIndex(
271fe 70 44 65 73 74 49 64 78 2c 20 70 53 72 63 49 64  pDestIdx, pSrcId
271ff 78 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  x) ) break;.    
27200 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 53  }.    assert( pS
27201 72 63 49 64 78 20 29 3b 0a 20 20 20 20 73 71 6c  rcIdx );.    sql
27202 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
27203 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 53 72 63  , OP_Close, iSrc
27204 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
27205 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
27206 50 5f 43 6c 6f 73 65 2c 20 69 44 65 73 74 2c 20  P_Close, iDest, 
27207 30 29 3b 0a 20 20 20 20 70 4b 65 79 20 3d 20 73  0);.    pKey = s
27208 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e  qlite3IndexKeyin
27209 66 6f 28 70 50 61 72 73 65 2c 20 70 53 72 63 49  fo(pParse, pSrcI
2720a 64 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  dx);.    sqlite3
2720b 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
2720c 5f 4f 70 65 6e 52 65 61 64 2c 20 69 53 72 63 2c  _OpenRead, iSrc,
2720d 20 70 53 72 63 49 64 78 2d 3e 74 6e 75 6d 2c 20   pSrcIdx->tnum, 
2720e 69 44 62 53 72 63 2c 0a 20 20 20 20 20 20 20 20  iDbSrc,.        
2720f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63                (c
27210 68 61 72 2a 29 70 4b 65 79 2c 20 50 34 5f 4b 45  har*)pKey, P4_KE
27211 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a  YINFO_HANDOFF);.
27212 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
27213 28 76 2c 20 22 25 73 22 2c 20 70 53 72 63 49 64  (v, "%s", pSrcId
27214 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20  x->zName));.    
27215 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e  pKey = sqlite3In
27216 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73  dexKeyinfo(pPars
27217 65 2c 20 70 44 65 73 74 49 64 78 29 3b 0a 20 20  e, pDestIdx);.  
27218 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27219 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 57 72  Op4(v, OP_OpenWr
2721a 69 74 65 2c 20 69 44 65 73 74 2c 20 70 44 65 73  ite, iDest, pDes
2721b 74 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 44  tIdx->tnum, iDbD
2721c 65 73 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20  est,.           
2721d 20 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72             (char
2721e 2a 29 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e  *)pKey, P4_KEYIN
2721f 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20  FO_HANDOFF);.   
27220 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
27221 20 22 25 73 22 2c 20 70 44 65 73 74 49 64 78 2d   "%s", pDestIdx-
27222 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 61 64  >zName));.    ad
27223 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62  dr1 = sqlite3Vdb
27224 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
27225 77 69 6e 64 2c 20 69 53 72 63 2c 20 30 29 3b 0a  wind, iSrc, 0);.
27226 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
27227 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 4b  ddOp2(v, OP_RowK
27228 65 79 2c 20 69 53 72 63 2c 20 72 65 67 44 61 74  ey, iSrc, regDat
27229 61 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  a);.    sqlite3V
2722a 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2722b 49 64 78 49 6e 73 65 72 74 2c 20 69 44 65 73 74  IdxInsert, iDest
2722c 2c 20 72 65 67 44 61 74 61 2c 20 31 29 3b 0a 20  , regData, 1);. 
2722d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2722e 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c  dOp2(v, OP_Next,
2722f 20 69 53 72 63 2c 20 61 64 64 72 31 2b 31 29 3b   iSrc, addr1+1);
27230 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
27231 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
27232 31 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  1);.  }.  sqlite
27233 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
27234 20 65 6d 70 74 79 53 72 63 54 65 73 74 29 3b 0a   emptySrcTest);.
27235 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
27236 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
27237 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 73 71 6c  regRowid);.  sql
27238 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
27239 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 44 61  eg(pParse, regDa
2723a 74 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  ta);.  sqlite3Vd
2723b 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43  beAddOp2(v, OP_C
2723c 6c 6f 73 65 2c 20 69 53 72 63 2c 20 30 29 3b 0a  lose, iSrc, 0);.
2723d 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2723e 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  Op2(v, OP_Close,
2723f 20 69 44 65 73 74 2c 20 30 29 3b 0a 20 20 69 66   iDest, 0);.  if
27240 28 20 65 6d 70 74 79 44 65 73 74 54 65 73 74 20  ( emptyDestTest 
27241 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
27242 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 48  beAddOp2(v, OP_H
27243 61 6c 74 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20  alt, SQLITE_OK, 
27244 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0);.    sqlite3V
27245 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 65  dbeJumpHere(v, e
27246 6d 70 74 79 44 65 73 74 54 65 73 74 29 3b 0a 20  mptyDestTest);. 
27247 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
27248 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65  dOp2(v, OP_Close
27249 2c 20 69 44 65 73 74 2c 20 30 29 3b 0a 20 20 20  , iDest, 0);.   
2724a 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c   return 0;.  }el
2724b 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  se{.    return 1
2724c 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f  ;.  }.}.#endif /
2724d 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 58 46  * SQLITE_OMIT_XF
2724e 45 52 5f 4f 50 54 20 2a 2f 0a 0a 2f 2a 20 4d 61  ER_OPT */../* Ma
2724f 6b 65 20 73 75 72 65 20 22 69 73 56 69 65 77 22  ke sure "isView"
27250 20 67 65 74 73 20 75 6e 64 65 66 69 6e 65 64 20   gets undefined 
27251 69 6e 20 63 61 73 65 20 74 68 69 73 20 66 69 6c  in case this fil
27252 65 20 62 65 63 6f 6d 65 73 20 70 61 72 74 20 6f  e becomes part o
27253 66 0a 2a 2a 20 74 68 65 20 61 6d 61 6c 67 61 6d  f.** the amalgam
27254 61 74 69 6f 6e 20 2d 20 73 6f 20 74 68 61 74 20  ation - so that 
27255 73 75 62 73 65 71 75 65 6e 74 20 66 69 6c 65 73  subsequent files
27256 20 64 6f 20 6e 6f 74 20 73 65 65 20 69 73 56 69   do not see isVi
27257 65 77 20 61 73 20 61 0a 2a 2a 20 6d 61 63 72 6f  ew as a.** macro
27258 2e 20 2a 2f 0a 23 75 6e 64 65 66 20 69 73 56 69  . */.#undef isVi
27259 65 77 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ew../***********
2725a 2a 2a 2a 20 45 6e 64 20 6f 66 20 69 6e 73 65 72  *** End of inser
2725b 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.c ************
2725c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2725d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2725e 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
2725f 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6c  *** Begin file l
27260 65 67 61 63 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  egacy.c ********
27261 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27262 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27263 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53  **/./*.** 2001 S
27264 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a  eptember 15.**.*
27265 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
27266 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
27267 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
27268 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
27269 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
2726a 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
2726b 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
2726c 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
2726d 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
2726e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
2726f 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
27270 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
27271 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
27272 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
27273 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
27274 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
27275 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
27276 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
27277 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27278 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27279 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2727a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 4d 61  **********.** Ma
2727b 69 6e 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20  in file for the 
2727c 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 20  SQLite library. 
2727d 20 54 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e   The routines in
2727e 20 74 68 69 73 20 66 69 6c 65 0a 2a 2a 20 69 6d   this file.** im
2727f 70 6c 65 6d 65 6e 74 20 74 68 65 20 70 72 6f 67  plement the prog
27280 72 61 6d 6d 65 72 20 69 6e 74 65 72 66 61 63 65  rammer interface
27281 20 74 6f 20 74 68 65 20 6c 69 62 72 61 72 79 2e   to the library.
27282 20 20 52 6f 75 74 69 6e 65 73 20 69 6e 0a 2a 2a    Routines in.**
27283 20 6f 74 68 65 72 20 66 69 6c 65 73 20 61 72 65   other files are
27284 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73   for internal us
27285 65 20 62 79 20 53 51 4c 69 74 65 20 61 6e 64 20  e by SQLite and 
27286 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a  should not be.**
27287 20 61 63 63 65 73 73 65 64 20 62 79 20 75 73 65   accessed by use
27288 72 73 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72  rs of the librar
27289 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6c 65  y..**.** $Id: le
2728a 67 61 63 79 2e 63 2c 76 20 31 2e 33 35 20 32 30  gacy.c,v 1.35 20
2728b 30 39 2f 30 38 2f 30 37 20 31 36 3a 35 36 3a 30  09/08/07 16:56:0
2728c 30 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78  0 danielk1977 Ex
2728d 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45  p $.*/.../*.** E
2728e 78 65 63 75 74 65 20 53 51 4c 20 63 6f 64 65 2e  xecute SQL code.
2728f 20 20 52 65 74 75 72 6e 20 6f 6e 65 20 6f 66 20    Return one of 
27290 74 68 65 20 53 51 4c 49 54 45 5f 20 73 75 63 63  the SQLITE_ succ
27291 65 73 73 2f 66 61 69 6c 75 72 65 0a 2a 2a 20 63  ess/failure.** c
27292 6f 64 65 73 2e 20 20 41 6c 73 6f 20 77 72 69 74  odes.  Also writ
27293 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  e an error messa
27294 67 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f  ge into memory o
27295 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20  btained from.** 
27296 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 61 6b  malloc() and mak
27297 65 20 2a 70 7a 45 72 72 4d 73 67 20 70 6f 69 6e  e *pzErrMsg poin
27298 74 20 74 6f 20 74 68 61 74 20 6d 65 73 73 61 67  t to that messag
27299 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
2729a 53 51 4c 20 69 73 20 61 20 71 75 65 72 79 2c 20  SQL is a query, 
2729b 74 68 65 6e 20 66 6f 72 20 65 61 63 68 20 72 6f  then for each ro
2729c 77 20 69 6e 20 74 68 65 20 71 75 65 72 79 20 72  w in the query r
2729d 65 73 75 6c 74 0a 2a 2a 20 74 68 65 20 78 43 61  esult.** the xCa
2729e 6c 6c 62 61 63 6b 28 29 20 66 75 6e 63 74 69 6f  llback() functio
2729f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 70 41  n is called.  pA
272a0 72 67 20 62 65 63 6f 6d 65 73 20 74 68 65 20 66  rg becomes the f
272a1 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  irst.** argument
272a2 20 74 6f 20 78 43 61 6c 6c 62 61 63 6b 28 29 2e   to xCallback().
272a3 20 20 49 66 20 78 43 61 6c 6c 62 61 63 6b 3d 4e    If xCallback=N
272a4 55 4c 4c 20 74 68 65 6e 20 6e 6f 20 63 61 6c 6c  ULL then no call
272a5 62 61 63 6b 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b  back.** is invok
272a6 65 64 2c 20 65 76 65 6e 20 66 6f 72 20 71 75 65  ed, even for que
272a7 72 69 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ries..*/.SQLITE_
272a8 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
272a9 65 78 65 63 28 0a 20 20 73 71 6c 69 74 65 33 20  exec(.  sqlite3 
272aa 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
272ab 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
272ac 61 73 65 20 6f 6e 20 77 68 69 63 68 20 74 68 65  ase on which the
272ad 20 53 51 4c 20 65 78 65 63 75 74 65 73 20 2a 2f   SQL executes */
272ae 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
272af 53 71 6c 2c 20 20 20 20 20 20 20 20 20 20 20 2f  Sql,           /
272b0 2a 20 54 68 65 20 53 51 4c 20 74 6f 20 62 65 20  * The SQL to be 
272b1 65 78 65 63 75 74 65 64 20 2a 2f 0a 20 20 73 71  executed */.  sq
272b2 6c 69 74 65 33 5f 63 61 6c 6c 62 61 63 6b 20 78  lite3_callback x
272b3 43 61 6c 6c 62 61 63 6b 2c 20 2f 2a 20 49 6e 76  Callback, /* Inv
272b4 6f 6b 65 20 74 68 69 73 20 63 61 6c 6c 62 61 63  oke this callbac
272b5 6b 20 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 76  k routine */.  v
272b6 6f 69 64 20 2a 70 41 72 67 2c 20 20 20 20 20 20  oid *pArg,      
272b7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
272b8 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
272b9 78 43 61 6c 6c 62 61 63 6b 28 29 20 2a 2f 0a 20  xCallback() */. 
272ba 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67   char **pzErrMsg
272bb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
272bc 57 72 69 74 65 20 65 72 72 6f 72 20 6d 65 73 73  Write error mess
272bd 61 67 65 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a  ages here */.){.
272be 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
272bf 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 2f 2a  E_OK;         /*
272c0 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
272c1 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c    const char *zL
272c2 65 66 74 6f 76 65 72 3b 20 20 20 20 20 20 2f 2a  eftover;      /*
272c3 20 54 61 69 6c 20 6f 66 20 75 6e 70 72 6f 63 65   Tail of unproce
272c4 73 73 65 64 20 53 51 4c 20 2a 2f 0a 20 20 73 71  ssed SQL */.  sq
272c5 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
272c6 74 20 3d 20 30 3b 20 20 20 20 2f 2a 20 54 68 65  t = 0;    /* The
272c7 20 63 75 72 72 65 6e 74 20 53 51 4c 20 73 74 61   current SQL sta
272c8 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 68 61 72  tement */.  char
272c9 20 2a 2a 61 7a 43 6f 6c 73 20 3d 20 30 3b 20 20   **azCols = 0;  
272ca 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 73          /* Names
272cb 20 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d   of result colum
272cc 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 74  ns */.  int nRet
272cd 72 79 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  ry = 0;         
272ce 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
272cf 20 72 65 74 72 79 20 61 74 74 65 6d 70 74 73 20   retry attempts 
272d0 2a 2f 0a 20 20 69 6e 74 20 63 61 6c 6c 62 61 63  */.  int callbac
272d1 6b 49 73 49 6e 69 74 3b 20 20 20 20 20 20 20 20  kIsInit;        
272d2 20 2f 2a 20 54 72 75 65 20 69 66 20 63 61 6c 6c   /* True if call
272d3 62 61 63 6b 20 64 61 74 61 20 69 73 20 69 6e 69  back data is ini
272d4 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 0a 20 20 69  tialized */..  i
272d5 66 28 20 7a 53 71 6c 3d 3d 30 20 29 20 7a 53 71  f( zSql==0 ) zSq
272d6 6c 20 3d 20 22 22 3b 0a 0a 20 20 73 71 6c 69 74  l = "";..  sqlit
272d7 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
272d8 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c  b->mutex);.  sql
272d9 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51  ite3Error(db, SQ
272da 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 77  LITE_OK, 0);.  w
272db 68 69 6c 65 28 20 28 72 63 3d 3d 53 51 4c 49 54  hile( (rc==SQLIT
272dc 45 5f 4f 4b 20 7c 7c 20 28 72 63 3d 3d 53 51 4c  E_OK || (rc==SQL
272dd 49 54 45 5f 53 43 48 45 4d 41 20 26 26 20 28 2b  ITE_SCHEMA && (+
272de 2b 6e 52 65 74 72 79 29 3c 32 29 29 20 26 26 20  +nRetry)<2)) && 
272df 7a 53 71 6c 5b 30 5d 20 29 7b 0a 20 20 20 20 69  zSql[0] ){.    i
272e0 6e 74 20 6e 43 6f 6c 3b 0a 20 20 20 20 63 68 61  nt nCol;.    cha
272e1 72 20 2a 2a 61 7a 56 61 6c 73 20 3d 20 30 3b 0a  r **azVals = 0;.
272e2 0a 20 20 20 20 70 53 74 6d 74 20 3d 20 30 3b 0a  .    pStmt = 0;.
272e3 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
272e4 5f 70 72 65 70 61 72 65 28 64 62 2c 20 7a 53 71  _prepare(db, zSq
272e5 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20 26  l, -1, &pStmt, &
272e6 7a 4c 65 66 74 6f 76 65 72 29 3b 0a 20 20 20 20  zLeftover);.    
272e7 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
272e8 54 45 5f 4f 4b 20 7c 7c 20 70 53 74 6d 74 3d 3d  TE_OK || pStmt==
272e9 30 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  0 );.    if( rc!
272ea 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
272eb 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20      continue;.  
272ec 20 20 7d 0a 20 20 20 20 69 66 28 20 21 70 53 74    }.    if( !pSt
272ed 6d 74 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 74  mt ){.      /* t
272ee 68 69 73 20 68 61 70 70 65 6e 73 20 66 6f 72 20  his happens for 
272ef 61 20 63 6f 6d 6d 65 6e 74 20 6f 72 20 77 68 69  a comment or whi
272f0 74 65 2d 73 70 61 63 65 20 2a 2f 0a 20 20 20 20  te-space */.    
272f1 20 20 7a 53 71 6c 20 3d 20 7a 4c 65 66 74 6f 76    zSql = zLeftov
272f2 65 72 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e  er;.      contin
272f3 75 65 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63  ue;.    }..    c
272f4 61 6c 6c 62 61 63 6b 49 73 49 6e 69 74 20 3d 20  allbackIsInit = 
272f5 30 3b 0a 20 20 20 20 6e 43 6f 6c 20 3d 20 73 71  0;.    nCol = sq
272f6 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75  lite3_column_cou
272f7 6e 74 28 70 53 74 6d 74 29 3b 0a 0a 20 20 20 20  nt(pStmt);..    
272f8 77 68 69 6c 65 28 20 31 20 29 7b 0a 20 20 20 20  while( 1 ){.    
272f9 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 72    int i;.      r
272fa 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  c = sqlite3_step
272fb 28 70 53 74 6d 74 29 3b 0a 0a 20 20 20 20 20 20  (pStmt);..      
272fc 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 63 61  /* Invoke the ca
272fd 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20  llback function 
272fe 69 66 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20  if required */. 
272ff 20 20 20 20 20 69 66 28 20 78 43 61 6c 6c 62 61       if( xCallba
27300 63 6b 20 26 26 20 28 53 51 4c 49 54 45 5f 52 4f  ck && (SQLITE_RO
27301 57 3d 3d 72 63 20 7c 7c 20 0a 20 20 20 20 20 20  W==rc || .      
27302 20 20 20 20 28 53 51 4c 49 54 45 5f 44 4f 4e 45      (SQLITE_DONE
27303 3d 3d 72 63 20 26 26 20 21 63 61 6c 6c 62 61 63  ==rc && !callbac
27304 6b 49 73 49 6e 69 74 0a 20 20 20 20 20 20 20 20  kIsInit.        
27305 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27306 20 20 20 26 26 20 64 62 2d 3e 66 6c 61 67 73 26     && db->flags&
27307 53 51 4c 49 54 45 5f 4e 75 6c 6c 43 61 6c 6c 62  SQLITE_NullCallb
27308 61 63 6b 29 29 20 29 7b 0a 20 20 20 20 20 20 20  ack)) ){.       
27309 20 69 66 28 20 21 63 61 6c 6c 62 61 63 6b 49 73   if( !callbackIs
2730a 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Init ){.        
2730b 20 20 61 7a 43 6f 6c 73 20 3d 20 73 71 6c 69 74    azCols = sqlit
2730c 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
2730d 62 2c 20 32 2a 6e 43 6f 6c 2a 73 69 7a 65 6f 66  b, 2*nCol*sizeof
2730e 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 20 2b 20  (const char*) + 
2730f 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  1);.          if
27310 28 20 61 7a 43 6f 6c 73 3d 3d 30 20 29 7b 0a 20  ( azCols==0 ){. 
27311 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20             goto 
27312 65 78 65 63 5f 6f 75 74 3b 0a 20 20 20 20 20 20  exec_out;.      
27313 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
27314 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b  for(i=0; i<nCol;
27315 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   i++){.         
27316 20 20 20 61 7a 43 6f 6c 73 5b 69 5d 20 3d 20 28     azCols[i] = (
27317 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 63  char *)sqlite3_c
27318 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 70 53 74 6d 74  olumn_name(pStmt
27319 2c 20 69 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , i);.          
2731a 20 20 2f 2a 20 73 71 6c 69 74 65 33 56 64 62 65    /* sqlite3Vdbe
2731b 53 65 74 43 6f 6c 4e 61 6d 65 28 29 20 69 6e 73  SetColName() ins
2731c 74 61 6c 6c 73 20 63 6f 6c 75 6d 6e 20 6e 61 6d  talls column nam
2731d 65 73 20 61 73 20 55 54 46 38 0a 20 20 20 20 20  es as UTF8.     
2731e 20 20 20 20 20 20 20 2a 2a 20 73 74 72 69 6e 67         ** string
2731f 73 20 73 6f 20 74 68 65 72 65 20 69 73 20 6e 6f  s so there is no
27320 20 77 61 79 20 66 6f 72 20 73 71 6c 69 74 65 33   way for sqlite3
27321 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 29 20 74  _column_name() t
27322 6f 20 66 61 69 6c 2e 20 2a 2f 0a 20 20 20 20 20  o fail. */.     
27323 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 61         assert( a
27324 7a 43 6f 6c 73 5b 69 5d 21 3d 30 20 29 3b 0a 20  zCols[i]!=0 );. 
27325 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
27326 20 20 20 20 20 63 61 6c 6c 62 61 63 6b 49 73 49       callbackIsI
27327 6e 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  nit = 1;.       
27328 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72   }.        if( r
27329 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b  c==SQLITE_ROW ){
2732a 0a 20 20 20 20 20 20 20 20 20 20 61 7a 56 61 6c  .          azVal
2732b 73 20 3d 20 26 61 7a 43 6f 6c 73 5b 6e 43 6f 6c  s = &azCols[nCol
2732c 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72  ];.          for
2732d 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b  (i=0; i<nCol; i+
2732e 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  +){.            
2732f 61 7a 56 61 6c 73 5b 69 5d 20 3d 20 28 63 68 61  azVals[i] = (cha
27330 72 20 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75  r *)sqlite3_colu
27331 6d 6e 5f 74 65 78 74 28 70 53 74 6d 74 2c 20 69  mn_text(pStmt, i
27332 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  );.            i
27333 66 28 20 21 61 7a 56 61 6c 73 5b 69 5d 20 26 26  f( !azVals[i] &&
27334 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
27335 74 79 70 65 28 70 53 74 6d 74 2c 20 69 29 21 3d  type(pStmt, i)!=
27336 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20  SQLITE_NULL ){. 
27337 20 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2d               db-
27338 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
27339 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  1;.             
2733a 20 67 6f 74 6f 20 65 78 65 63 5f 6f 75 74 3b 0a   goto exec_out;.
2733b 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
2733c 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2733d 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
2733e 78 43 61 6c 6c 62 61 63 6b 28 70 41 72 67 2c 20  xCallback(pArg, 
2733f 6e 43 6f 6c 2c 20 61 7a 56 61 6c 73 2c 20 61 7a  nCol, azVals, az
27340 43 6f 6c 73 29 20 29 7b 0a 20 20 20 20 20 20 20  Cols) ){.       
27341 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 41     rc = SQLITE_A
27342 42 4f 52 54 3b 0a 20 20 20 20 20 20 20 20 20 20  BORT;.          
27343 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c  sqlite3VdbeFinal
27344 69 7a 65 28 28 56 64 62 65 20 2a 29 70 53 74 6d  ize((Vdbe *)pStm
27345 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 53  t);.          pS
27346 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  tmt = 0;.       
27347 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28     sqlite3Error(
27348 64 62 2c 20 53 51 4c 49 54 45 5f 41 42 4f 52 54  db, SQLITE_ABORT
27349 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 0);.          
2734a 67 6f 74 6f 20 65 78 65 63 5f 6f 75 74 3b 0a 20  goto exec_out;. 
2734b 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
2734c 0a 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ..      if( rc!=
2734d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20  SQLITE_ROW ){.  
2734e 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
2734f 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 28  e3VdbeFinalize((
27350 56 64 62 65 20 2a 29 70 53 74 6d 74 29 3b 0a 20  Vdbe *)pStmt);. 
27351 20 20 20 20 20 20 20 70 53 74 6d 74 20 3d 20 30         pStmt = 0
27352 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
27353 21 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20  !=SQLITE_SCHEMA 
27354 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 52 65  ){.          nRe
27355 74 72 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  try = 0;.       
27356 20 20 20 7a 53 71 6c 20 3d 20 7a 4c 65 66 74 6f     zSql = zLefto
27357 76 65 72 3b 0a 20 20 20 20 20 20 20 20 20 20 77  ver;.          w
27358 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73  hile( sqlite3Iss
27359 70 61 63 65 28 7a 53 71 6c 5b 30 5d 29 20 29 20  pace(zSql[0]) ) 
2735a 7a 53 71 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20  zSql++;.        
2735b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2735c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
2735d 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2735e 65 28 64 62 2c 20 61 7a 43 6f 6c 73 29 3b 0a 20  e(db, azCols);. 
2735f 20 20 20 61 7a 43 6f 6c 73 20 3d 20 30 3b 0a 20     azCols = 0;. 
27360 20 7d 0a 0a 65 78 65 63 5f 6f 75 74 3a 0a 20 20   }..exec_out:.  
27361 69 66 28 20 70 53 74 6d 74 20 29 20 73 71 6c 69  if( pStmt ) sqli
27362 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28  te3VdbeFinalize(
27363 28 56 64 62 65 20 2a 29 70 53 74 6d 74 29 3b 0a  (Vdbe *)pStmt);.
27364 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
27365 64 62 2c 20 61 7a 43 6f 6c 73 29 3b 0a 0a 20 20  db, azCols);..  
27366 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45  rc = sqlite3ApiE
27367 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 69  xit(db, rc);.  i
27368 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
27369 20 26 26 20 41 4c 57 41 59 53 28 72 63 3d 3d 73   && ALWAYS(rc==s
2736a 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 64  qlite3_errcode(d
2736b 62 29 29 20 26 26 20 70 7a 45 72 72 4d 73 67 20  b)) && pzErrMsg 
2736c 29 7b 0a 20 20 20 20 69 6e 74 20 6e 45 72 72 4d  ){.    int nErrM
2736d 73 67 20 3d 20 31 20 2b 20 73 71 6c 69 74 65 33  sg = 1 + sqlite3
2736e 53 74 72 6c 65 6e 33 30 28 73 71 6c 69 74 65 33  Strlen30(sqlite3
2736f 5f 65 72 72 6d 73 67 28 64 62 29 29 3b 0a 20 20  _errmsg(db));.  
27370 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 73 71    *pzErrMsg = sq
27371 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 45 72 72  lite3Malloc(nErr
27372 4d 73 67 29 3b 0a 20 20 20 20 69 66 28 20 2a 70  Msg);.    if( *p
27373 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20  zErrMsg ){.     
27374 20 6d 65 6d 63 70 79 28 2a 70 7a 45 72 72 4d 73   memcpy(*pzErrMs
27375 67 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  g, sqlite3_errms
27376 67 28 64 62 29 2c 20 6e 45 72 72 4d 73 67 29 3b  g(db), nErrMsg);
27377 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
27378 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
27379 4d 45 4d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  MEM;.      sqlit
2737a 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49  e3Error(db, SQLI
2737b 54 45 5f 4e 4f 4d 45 4d 2c 20 30 29 3b 0a 20 20  TE_NOMEM, 0);.  
2737c 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20    }.  }else if( 
2737d 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20  pzErrMsg ){.    
2737e 2a 70 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20  *pzErrMsg = 0;. 
2737f 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 28 72   }..  assert( (r
27380 63 26 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3d 3d  c&db->errMask)==
27381 72 63 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  rc );.  sqlite3_
27382 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
27383 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
27384 20 72 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a   rc;.}../*******
27385 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6c  ******* End of l
27386 65 67 61 63 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  egacy.c ********
27387 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27388 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27389 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
2738a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
2738b 6c 65 20 6c 6f 61 64 65 78 74 2e 63 20 2a 2a 2a  le loadext.c ***
2738c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2738d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2738e 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
2738f 30 36 20 4a 75 6e 65 20 37 0a 2a 2a 0a 2a 2a 20  06 June 7.**.** 
27390 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
27391 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
27392 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
27393 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
27394 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
27395 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
27396 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
27397 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
27398 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
27399 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
2739a 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
2739b 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
2739c 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
2739d 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
2739e 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
2739f 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
273a0 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
273a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
273a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
273a3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
273a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
273a5 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
273a6 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
273a7 6f 64 65 20 75 73 65 64 20 74 6f 20 64 79 6e 61  ode used to dyna
273a8 6d 69 63 61 6c 6c 79 20 6c 6f 61 64 20 65 78 74  mically load ext
273a9 65 6e 73 69 6f 6e 73 20 69 6e 74 6f 0a 2a 2a 20  ensions into.** 
273aa 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61  the SQLite libra
273ab 72 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6c  ry..**.** $Id: l
273ac 6f 61 64 65 78 74 2e 63 2c 76 20 31 2e 36 30 20  oadext.c,v 1.60 
273ad 32 30 30 39 2f 30 36 2f 30 33 20 30 31 3a 32 34  2009/06/03 01:24
273ae 3a 35 34 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :54 drh Exp $.*/
273af 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
273b0 5f 43 4f 52 45 0a 20 20 23 64 65 66 69 6e 65 20  _CORE.  #define 
273b1 53 51 4c 49 54 45 5f 43 4f 52 45 20 31 20 20 2f  SQLITE_CORE 1  /
273b2 2a 20 44 69 73 61 62 6c 65 20 74 68 65 20 41 50  * Disable the AP
273b3 49 20 72 65 64 65 66 69 6e 69 74 69 6f 6e 20 69  I redefinition i
273b4 6e 20 73 71 6c 69 74 65 33 65 78 74 2e 68 20 2a  n sqlite3ext.h *
273b5 2f 0a 23 65 6e 64 69 66 0a 2f 2a 2a 2a 2a 2a 2a  /.#endif./******
273b6 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65  ******** Include
273b7 20 73 71 6c 69 74 65 33 65 78 74 2e 68 20 69 6e   sqlite3ext.h in
273b8 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6c   the middle of l
273b9 6f 61 64 65 78 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a  oadext.c *******
273ba 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
273bb 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
273bc 69 6c 65 20 73 71 6c 69 74 65 33 65 78 74 2e 68  ile sqlite3ext.h
273bd 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
273be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
273bf 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
273c0 30 30 36 20 4a 75 6e 65 20 37 0a 2a 2a 0a 2a 2a  006 June 7.**.**
273c1 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
273c2 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
273c3 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
273c4 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
273c5 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
273c6 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
273c7 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
273c8 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
273c9 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
273ca 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
273cb 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
273cc 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
273cd 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
273ce 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
273cf 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
273d0 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
273d1 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
273d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
273d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
273d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
273d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
273d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
273d7 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 65  s header file de
273d8 66 69 6e 65 73 20 74 68 65 20 53 51 4c 69 74 65  fines the SQLite
273d9 20 69 6e 74 65 72 66 61 63 65 20 66 6f 72 20 75   interface for u
273da 73 65 20 62 79 0a 2a 2a 20 73 68 61 72 65 64 20  se by.** shared 
273db 6c 69 62 72 61 72 69 65 73 20 74 68 61 74 20 77  libraries that w
273dc 61 6e 74 20 74 6f 20 62 65 20 69 6d 70 6f 72 74  ant to be import
273dd 65 64 20 61 73 20 65 78 74 65 6e 73 69 6f 6e 73  ed as extensions
273de 20 69 6e 74 6f 0a 2a 2a 20 61 6e 20 53 51 4c 69   into.** an SQLi
273df 74 65 20 69 6e 73 74 61 6e 63 65 2e 20 20 53 68  te instance.  Sh
273e0 61 72 65 64 20 6c 69 62 72 61 72 69 65 73 20 74  ared libraries t
273e1 68 61 74 20 69 6e 74 65 6e 64 20 74 6f 20 62 65  hat intend to be
273e2 20 6c 6f 61 64 65 64 0a 2a 2a 20 61 73 20 65 78   loaded.** as ex
273e3 74 65 6e 73 69 6f 6e 73 20 62 79 20 53 51 4c 69  tensions by SQLi
273e4 74 65 20 73 68 6f 75 6c 64 20 23 69 6e 63 6c 75  te should #inclu
273e5 64 65 20 74 68 69 73 20 66 69 6c 65 20 69 6e 73  de this file ins
273e6 74 65 61 64 20 6f 66 20 0a 2a 2a 20 73 71 6c 69  tead of .** sqli
273e7 74 65 33 2e 68 2e 0a 2a 2a 0a 2a 2a 20 40 28 23  te3.h..**.** @(#
273e8 29 20 24 49 64 3a 20 73 71 6c 69 74 65 33 65 78  ) $Id: sqlite3ex
273e9 74 2e 68 2c 76 20 31 2e 32 35 20 32 30 30 38 2f  t.h,v 1.25 2008/
273ea 31 30 2f 31 32 20 30 30 3a 32 37 3a 35 34 20 73  10/12 00:27:54 s
273eb 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69  hane Exp $.*/.#i
273ec 66 6e 64 65 66 20 5f 53 51 4c 49 54 45 33 45 58  fndef _SQLITE3EX
273ed 54 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51  T_H_.#define _SQ
273ee 4c 49 54 45 33 45 58 54 5f 48 5f 0a 0a 74 79 70  LITE3EXT_H_..typ
273ef 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69  edef struct sqli
273f0 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73  te3_api_routines
273f1 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75   sqlite3_api_rou
273f2 74 69 6e 65 73 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68  tines;../*.** Th
273f3 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
273f4 63 74 75 72 65 20 68 6f 6c 64 73 20 70 6f 69 6e  cture holds poin
273f5 74 65 72 73 20 74 6f 20 61 6c 6c 20 6f 66 20 74  ters to all of t
273f6 68 65 20 53 51 4c 69 74 65 20 41 50 49 0a 2a 2a  he SQLite API.**
273f7 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a   routines..**.**
273f8 20 57 41 52 4e 49 4e 47 3a 20 20 49 6e 20 6f 72   WARNING:  In or
273f9 64 65 72 20 74 6f 20 6d 61 69 6e 74 61 69 6e 20  der to maintain 
273fa 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74  backwards compat
273fb 69 62 69 6c 69 74 79 2c 20 61 64 64 20 6e 65 77  ibility, add new
273fc 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 20 74  .** interfaces t
273fd 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 69  o the end of thi
273fe 73 20 73 74 72 75 63 74 75 72 65 20 6f 6e 6c 79  s structure only
273ff 2e 20 20 49 66 20 79 6f 75 20 69 6e 73 65 72 74  .  If you insert
27400 20 6e 65 77 0a 2a 2a 20 69 6e 74 65 72 66 61 63   new.** interfac
27401 65 73 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  es in the middle
27402 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
27403 72 65 2c 20 74 68 65 6e 20 6f 6c 64 65 72 20 64  re, then older d
27404 69 66 66 65 72 65 6e 74 0a 2a 2a 20 76 65 72 73  ifferent.** vers
27405 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 77  ions of SQLite w
27406 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20  ill not be able 
27407 74 6f 20 6c 6f 61 64 20 65 61 63 68 20 6f 74 68  to load each oth
27408 65 72 73 27 20 73 68 61 72 65 64 0a 2a 2a 20 6c  ers' shared.** l
27409 69 62 72 61 72 69 65 73 21 0a 2a 2f 0a 73 74 72  ibraries!.*/.str
2740a 75 63 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f  uct sqlite3_api_
2740b 72 6f 75 74 69 6e 65 73 20 7b 0a 20 20 76 6f 69  routines {.  voi
2740c 64 20 2a 20 28 2a 61 67 67 72 65 67 61 74 65 5f  d * (*aggregate_
2740d 63 6f 6e 74 65 78 74 29 28 73 71 6c 69 74 65 33  context)(sqlite3
2740e 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 20 6e 42  _context*,int nB
2740f 79 74 65 73 29 3b 0a 20 20 69 6e 74 20 20 28 2a  ytes);.  int  (*
27410 61 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 29  aggregate_count)
27411 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
27412 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e  *);.  int  (*bin
27413 64 5f 62 6c 6f 62 29 28 73 71 6c 69 74 65 33 5f  d_blob)(sqlite3_
27414 73 74 6d 74 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  stmt*,int,const 
27415 76 6f 69 64 2a 2c 69 6e 74 20 6e 2c 76 6f 69 64  void*,int n,void
27416 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 69  (*)(void*));.  i
27417 6e 74 20 20 28 2a 62 69 6e 64 5f 64 6f 75 62 6c  nt  (*bind_doubl
27418 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  e)(sqlite3_stmt*
27419 2c 69 6e 74 2c 64 6f 75 62 6c 65 29 3b 0a 20 20  ,int,double);.  
2741a 69 6e 74 20 20 28 2a 62 69 6e 64 5f 69 6e 74 29  int  (*bind_int)
2741b 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
2741c 6e 74 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 20  nt,int);.  int  
2741d 28 2a 62 69 6e 64 5f 69 6e 74 36 34 29 28 73 71  (*bind_int64)(sq
2741e 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c  lite3_stmt*,int,
2741f 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 3b 0a 20  sqlite_int64);. 
27420 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 6e 75 6c   int  (*bind_nul
27421 6c 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  l)(sqlite3_stmt*
27422 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a  ,int);.  int  (*
27423 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63  bind_parameter_c
27424 6f 75 6e 74 29 28 73 71 6c 69 74 65 33 5f 73 74  ount)(sqlite3_st
27425 6d 74 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62  mt*);.  int  (*b
27426 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e  ind_parameter_in
27427 64 65 78 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  dex)(sqlite3_stm
27428 74 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 7a 4e  t*,const char*zN
27429 61 6d 65 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68  ame);.  const ch
2742a 61 72 20 2a 20 28 2a 62 69 6e 64 5f 70 61 72 61  ar * (*bind_para
2742b 6d 65 74 65 72 5f 6e 61 6d 65 29 28 73 71 6c 69  meter_name)(sqli
2742c 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a  te3_stmt*,int);.
2742d 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 74 65    int  (*bind_te
2742e 78 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  xt)(sqlite3_stmt
2742f 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72  *,int,const char
27430 2a 2c 69 6e 74 20 6e 2c 76 6f 69 64 28 2a 29 28  *,int n,void(*)(
27431 76 6f 69 64 2a 29 29 3b 0a 20 20 69 6e 74 20 20  void*));.  int  
27432 28 2a 62 69 6e 64 5f 74 65 78 74 31 36 29 28 73  (*bind_text16)(s
27433 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
27434 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74  ,const void*,int
27435 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29  ,void(*)(void*))
27436 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f  ;.  int  (*bind_
27437 76 61 6c 75 65 29 28 73 71 6c 69 74 65 33 5f 73  value)(sqlite3_s
27438 74 6d 74 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 73  tmt*,int,const s
27439 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a  qlite3_value*);.
2743a 20 20 69 6e 74 20 20 28 2a 62 75 73 79 5f 68 61    int  (*busy_ha
2743b 6e 64 6c 65 72 29 28 73 71 6c 69 74 65 33 2a 2c  ndler)(sqlite3*,
2743c 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74  int(*)(void*,int
2743d 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20  ),void*);.  int 
2743e 20 28 2a 62 75 73 79 5f 74 69 6d 65 6f 75 74 29   (*busy_timeout)
2743f 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 6d 73  (sqlite3*,int ms
27440 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 68 61 6e  );.  int  (*chan
27441 67 65 73 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a  ges)(sqlite3*);.
27442 20 20 69 6e 74 20 20 28 2a 63 6c 6f 73 65 29 28    int  (*close)(
27443 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 69 6e 74  sqlite3*);.  int
27444 20 20 28 2a 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65    (*collation_ne
27445 65 64 65 64 29 28 73 71 6c 69 74 65 33 2a 2c 76  eded)(sqlite3*,v
27446 6f 69 64 2a 2c 76 6f 69 64 28 2a 29 28 76 6f 69  oid*,void(*)(voi
27447 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20  d*,sqlite3*,int 
27448 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 63  eTextRep,const c
27449 68 61 72 2a 29 29 3b 0a 20 20 69 6e 74 20 20 28  har*));.  int  (
2744a 2a 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65  *collation_neede
2744b 64 31 36 29 28 73 71 6c 69 74 65 33 2a 2c 76 6f  d16)(sqlite3*,vo
2744c 69 64 2a 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64  id*,void(*)(void
2744d 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65  *,sqlite3*,int e
2744e 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 76 6f  TextRep,const vo
2744f 69 64 2a 29 29 3b 0a 20 20 63 6f 6e 73 74 20 76  id*));.  const v
27450 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 62  oid * (*column_b
27451 6c 6f 62 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  lob)(sqlite3_stm
27452 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20  t*,int iCol);.  
27453 69 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f 62 79  int  (*column_by
27454 74 65 73 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  tes)(sqlite3_stm
27455 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20  t*,int iCol);.  
27456 69 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f 62 79  int  (*column_by
27457 74 65 73 31 36 29 28 73 71 6c 69 74 65 33 5f 73  tes16)(sqlite3_s
27458 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a  tmt*,int iCol);.
27459 20 20 69 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f    int  (*column_
2745a 63 6f 75 6e 74 29 28 73 71 6c 69 74 65 33 5f 73  count)(sqlite3_s
2745b 74 6d 74 2a 70 53 74 6d 74 29 3b 0a 20 20 63 6f  tmt*pStmt);.  co
2745c 6e 73 74 20 63 68 61 72 20 2a 20 28 2a 63 6f 6c  nst char * (*col
2745d 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d  umn_database_nam
2745e 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  e)(sqlite3_stmt*
2745f 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20 76  ,int);.  const v
27460 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 64  oid * (*column_d
27461 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 29 28  atabase_name16)(
27462 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
27463 74 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  t);.  const char
27464 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 64 65 63 6c   * (*column_decl
27465 74 79 70 65 29 28 73 71 6c 69 74 65 33 5f 73 74  type)(sqlite3_st
27466 6d 74 2a 2c 69 6e 74 20 69 29 3b 0a 20 20 63 6f  mt*,int i);.  co
27467 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c  nst void * (*col
27468 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 29 28  umn_decltype16)(
27469 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
2746a 74 29 3b 0a 20 20 64 6f 75 62 6c 65 20 20 28 2a  t);.  double  (*
2746b 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 29 28 73  column_double)(s
2746c 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
2746d 20 69 43 6f 6c 29 3b 0a 20 20 69 6e 74 20 20 28   iCol);.  int  (
2746e 2a 63 6f 6c 75 6d 6e 5f 69 6e 74 29 28 73 71 6c  *column_int)(sql
2746f 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69  ite3_stmt*,int i
27470 43 6f 6c 29 3b 0a 20 20 73 71 6c 69 74 65 5f 69  Col);.  sqlite_i
27471 6e 74 36 34 20 20 28 2a 63 6f 6c 75 6d 6e 5f 69  nt64  (*column_i
27472 6e 74 36 34 29 28 73 71 6c 69 74 65 33 5f 73 74  nt64)(sqlite3_st
27473 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20  mt*,int iCol);. 
27474 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 28 2a   const char * (*
27475 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 29 28 73 71 6c  column_name)(sql
27476 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b  ite3_stmt*,int);
27477 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20  .  const void * 
27478 28 2a 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 29  (*column_name16)
27479 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
2747a 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  nt);.  const cha
2747b 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 6f 72 69  r * (*column_ori
2747c 67 69 6e 5f 6e 61 6d 65 29 28 73 71 6c 69 74 65  gin_name)(sqlite
2747d 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20  3_stmt*,int);.  
2747e 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 63  const void * (*c
2747f 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d  olumn_origin_nam
27480 65 31 36 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  e16)(sqlite3_stm
27481 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74  t*,int);.  const
27482 20 63 68 61 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e   char * (*column
27483 5f 74 61 62 6c 65 5f 6e 61 6d 65 29 28 73 71 6c  _table_name)(sql
27484 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b  ite3_stmt*,int);
27485 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20  .  const void * 
27486 28 2a 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e  (*column_table_n
27487 61 6d 65 31 36 29 28 73 71 6c 69 74 65 33 5f 73  ame16)(sqlite3_s
27488 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e  tmt*,int);.  con
27489 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
2748a 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 74 65 78 74   * (*column_text
2748b 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
2748c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 63 6f 6e  int iCol);.  con
2748d 73 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75  st void * (*colu
2748e 6d 6e 5f 74 65 78 74 31 36 29 28 73 71 6c 69 74  mn_text16)(sqlit
2748f 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f  e3_stmt*,int iCo
27490 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6c  l);.  int  (*col
27491 75 6d 6e 5f 74 79 70 65 29 28 73 71 6c 69 74 65  umn_type)(sqlite
27492 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c  3_stmt*,int iCol
27493 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  );.  sqlite3_val
27494 75 65 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 76 61 6c  ue* (*column_val
27495 75 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ue)(sqlite3_stmt
27496 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 76  *,int iCol);.  v
27497 6f 69 64 20 2a 20 28 2a 63 6f 6d 6d 69 74 5f 68  oid * (*commit_h
27498 6f 6f 6b 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e  ook)(sqlite3*,in
27499 74 28 2a 29 28 76 6f 69 64 2a 29 2c 76 6f 69 64  t(*)(void*),void
2749a 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6d  *);.  int  (*com
2749b 70 6c 65 74 65 29 28 63 6f 6e 73 74 20 63 68 61  plete)(const cha
2749c 72 2a 73 71 6c 29 3b 0a 20 20 69 6e 74 20 20 28  r*sql);.  int  (
2749d 2a 63 6f 6d 70 6c 65 74 65 31 36 29 28 63 6f 6e  *complete16)(con
2749e 73 74 20 76 6f 69 64 2a 73 71 6c 29 3b 0a 20 20  st void*sql);.  
2749f 69 6e 74 20 20 28 2a 63 72 65 61 74 65 5f 63 6f  int  (*create_co
274a0 6c 6c 61 74 69 6f 6e 29 28 73 71 6c 69 74 65 33  llation)(sqlite3
274a1 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e  *,const char*,in
274a2 74 2c 76 6f 69 64 2a 2c 69 6e 74 28 2a 29 28 76  t,void*,int(*)(v
274a3 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  oid*,int,const v
274a4 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  oid*,int,const v
274a5 6f 69 64 2a 29 29 3b 0a 20 20 69 6e 74 20 20 28  oid*));.  int  (
274a6 2a 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  *create_collatio
274a7 6e 31 36 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f  n16)(sqlite3*,co
274a8 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f  nst void*,int,vo
274a9 69 64 2a 2c 69 6e 74 28 2a 29 28 76 6f 69 64 2a  id*,int(*)(void*
274aa 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ,int,const void*
274ab 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ,int,const void*
274ac 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 72 65  ));.  int  (*cre
274ad 61 74 65 5f 66 75 6e 63 74 69 6f 6e 29 28 73 71  ate_function)(sq
274ae 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61  lite3*,const cha
274af 72 2a 2c 69 6e 74 2c 69 6e 74 2c 76 6f 69 64 2a  r*,int,int,void*
274b0 2c 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73  ,void (*xFunc)(s
274b1 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
274b2 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
274b3 65 2a 2a 29 2c 76 6f 69 64 20 28 2a 78 53 74 65  e**),void (*xSte
274b4 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  p)(sqlite3_conte
274b5 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  xt*,int,sqlite3_
274b6 76 61 6c 75 65 2a 2a 29 2c 76 6f 69 64 20 28 2a  value**),void (*
274b7 78 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f  xFinal)(sqlite3_
274b8 63 6f 6e 74 65 78 74 2a 29 29 3b 0a 20 20 69 6e  context*));.  in
274b9 74 20 20 28 2a 63 72 65 61 74 65 5f 66 75 6e 63  t  (*create_func
274ba 74 69 6f 6e 31 36 29 28 73 71 6c 69 74 65 33 2a  tion16)(sqlite3*
274bb 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74  ,const void*,int
274bc 2c 69 6e 74 2c 76 6f 69 64 2a 2c 76 6f 69 64 20  ,int,void*,void 
274bd 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33  (*xFunc)(sqlite3
274be 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71  _context*,int,sq
274bf 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 76  lite3_value**),v
274c0 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c  oid (*xStep)(sql
274c1 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
274c2 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  t,sqlite3_value*
274c3 2a 29 2c 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c  *),void (*xFinal
274c4 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
274c5 74 2a 29 29 3b 0a 20 20 69 6e 74 20 28 2a 63 72  t*));.  int (*cr
274c6 65 61 74 65 5f 6d 6f 64 75 6c 65 29 28 73 71 6c  eate_module)(sql
274c7 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72  ite3*,const char
274c8 2a 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  *,const sqlite3_
274c9 6d 6f 64 75 6c 65 2a 2c 76 6f 69 64 2a 29 3b 0a  module*,void*);.
274ca 20 20 69 6e 74 20 20 28 2a 64 61 74 61 5f 63 6f    int  (*data_co
274cb 75 6e 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  unt)(sqlite3_stm
274cc 74 2a 70 53 74 6d 74 29 3b 0a 20 20 73 71 6c 69  t*pStmt);.  sqli
274cd 74 65 33 20 2a 20 28 2a 64 62 5f 68 61 6e 64 6c  te3 * (*db_handl
274ce 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  e)(sqlite3_stmt*
274cf 29 3b 0a 20 20 69 6e 74 20 28 2a 64 65 63 6c 61  );.  int (*decla
274d0 72 65 5f 76 74 61 62 29 28 73 71 6c 69 74 65 33  re_vtab)(sqlite3
274d1 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a  *,const char*);.
274d2 20 20 69 6e 74 20 20 28 2a 65 6e 61 62 6c 65 5f    int  (*enable_
274d3 73 68 61 72 65 64 5f 63 61 63 68 65 29 28 69 6e  shared_cache)(in
274d4 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a 65 72 72  t);.  int  (*err
274d5 63 6f 64 65 29 28 73 71 6c 69 74 65 33 2a 64 62  code)(sqlite3*db
274d6 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  );.  const char 
274d7 2a 20 28 2a 65 72 72 6d 73 67 29 28 73 71 6c 69  * (*errmsg)(sqli
274d8 74 65 33 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 76  te3*);.  const v
274d9 6f 69 64 20 2a 20 28 2a 65 72 72 6d 73 67 31 36  oid * (*errmsg16
274da 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 69  )(sqlite3*);.  i
274db 6e 74 20 20 28 2a 65 78 65 63 29 28 73 71 6c 69  nt  (*exec)(sqli
274dc 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  te3*,const char*
274dd 2c 73 71 6c 69 74 65 33 5f 63 61 6c 6c 62 61 63  ,sqlite3_callbac
274de 6b 2c 76 6f 69 64 2a 2c 63 68 61 72 2a 2a 29 3b  k,void*,char**);
274df 0a 20 20 69 6e 74 20 20 28 2a 65 78 70 69 72 65  .  int  (*expire
274e0 64 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  d)(sqlite3_stmt*
274e1 29 3b 0a 20 20 69 6e 74 20 20 28 2a 66 69 6e 61  );.  int  (*fina
274e2 6c 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 73 74  lize)(sqlite3_st
274e3 6d 74 2a 70 53 74 6d 74 29 3b 0a 20 20 76 6f 69  mt*pStmt);.  voi
274e4 64 20 20 28 2a 66 72 65 65 29 28 76 6f 69 64 2a  d  (*free)(void*
274e5 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 66 72 65  );.  void  (*fre
274e6 65 5f 74 61 62 6c 65 29 28 63 68 61 72 2a 2a 72  e_table)(char**r
274e7 65 73 75 6c 74 29 3b 0a 20 20 69 6e 74 20 20 28  esult);.  int  (
274e8 2a 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 29  *get_autocommit)
274e9 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 76 6f  (sqlite3*);.  vo
274ea 69 64 20 2a 20 28 2a 67 65 74 5f 61 75 78 64 61  id * (*get_auxda
274eb 74 61 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ta)(sqlite3_cont
274ec 65 78 74 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74  ext*,int);.  int
274ed 20 20 28 2a 67 65 74 5f 74 61 62 6c 65 29 28 73    (*get_table)(s
274ee 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68  qlite3*,const ch
274ef 61 72 2a 2c 63 68 61 72 2a 2a 2a 2c 69 6e 74 2a  ar*,char***,int*
274f0 2c 69 6e 74 2a 2c 63 68 61 72 2a 2a 29 3b 0a 20  ,int*,char**);. 
274f1 20 69 6e 74 20 20 28 2a 67 6c 6f 62 61 6c 5f 72   int  (*global_r
274f2 65 63 6f 76 65 72 29 28 76 6f 69 64 29 3b 0a 20  ecover)(void);. 
274f3 20 76 6f 69 64 20 20 28 2a 69 6e 74 65 72 72 75   void  (*interru
274f4 70 74 78 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a  ptx)(sqlite3*);.
274f5 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 20    sqlite_int64  
274f6 28 2a 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f  (*last_insert_ro
274f7 77 69 64 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a  wid)(sqlite3*);.
274f8 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 28    const char * (
274f9 2a 6c 69 62 76 65 72 73 69 6f 6e 29 28 76 6f 69  *libversion)(voi
274fa 64 29 3b 0a 20 20 69 6e 74 20 20 28 2a 6c 69 62  d);.  int  (*lib
274fb 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 29 28  version_number)(
274fc 76 6f 69 64 29 3b 0a 20 20 76 6f 69 64 20 2a 28  void);.  void *(
274fd 2a 6d 61 6c 6c 6f 63 29 28 69 6e 74 29 3b 0a 20  *malloc)(int);. 
274fe 20 63 68 61 72 20 2a 20 28 2a 6d 70 72 69 6e 74   char * (*mprint
274ff 66 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e  f)(const char*,.
27500 2e 2e 29 3b 0a 20 20 69 6e 74 20 20 28 2a 6f 70  ..);.  int  (*op
27501 65 6e 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c  en)(const char*,
27502 73 71 6c 69 74 65 33 2a 2a 29 3b 0a 20 20 69 6e  sqlite3**);.  in
27503 74 20 20 28 2a 6f 70 65 6e 31 36 29 28 63 6f 6e  t  (*open16)(con
27504 73 74 20 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33  st void*,sqlite3
27505 2a 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 70 72  **);.  int  (*pr
27506 65 70 61 72 65 29 28 73 71 6c 69 74 65 33 2a 2c  epare)(sqlite3*,
27507 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c  const char*,int,
27508 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2a 2c 63  sqlite3_stmt**,c
27509 6f 6e 73 74 20 63 68 61 72 2a 2a 29 3b 0a 20 20  onst char**);.  
2750a 69 6e 74 20 20 28 2a 70 72 65 70 61 72 65 31 36  int  (*prepare16
2750b 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74  )(sqlite3*,const
2750c 20 76 6f 69 64 2a 2c 69 6e 74 2c 73 71 6c 69 74   void*,int,sqlit
2750d 65 33 5f 73 74 6d 74 2a 2a 2c 63 6f 6e 73 74 20  e3_stmt**,const 
2750e 76 6f 69 64 2a 2a 29 3b 0a 20 20 76 6f 69 64 20  void**);.  void 
2750f 2a 20 28 2a 70 72 6f 66 69 6c 65 29 28 73 71 6c  * (*profile)(sql
27510 69 74 65 33 2a 2c 76 6f 69 64 28 2a 29 28 76 6f  ite3*,void(*)(vo
27511 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  id*,const char*,
27512 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 2c 76  sqlite_uint64),v
27513 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 20 28  oid*);.  void  (
27514 2a 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65  *progress_handle
27515 72 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c  r)(sqlite3*,int,
27516 69 6e 74 28 2a 29 28 76 6f 69 64 2a 29 2c 76 6f  int(*)(void*),vo
27517 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 28 2a  id*);.  void *(*
27518 72 65 61 6c 6c 6f 63 29 28 76 6f 69 64 2a 2c 69  realloc)(void*,i
27519 6e 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a 72 65  nt);.  int  (*re
2751a 73 65 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  set)(sqlite3_stm
2751b 74 2a 70 53 74 6d 74 29 3b 0a 20 20 76 6f 69 64  t*pStmt);.  void
2751c 20 20 28 2a 72 65 73 75 6c 74 5f 62 6c 6f 62 29    (*result_blob)
2751d 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
2751e 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  *,const void*,in
2751f 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29  t,void(*)(void*)
27520 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73  );.  void  (*res
27521 75 6c 74 5f 64 6f 75 62 6c 65 29 28 73 71 6c 69  ult_double)(sqli
27522 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 64 6f 75  te3_context*,dou
27523 62 6c 65 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a  ble);.  void  (*
27524 72 65 73 75 6c 74 5f 65 72 72 6f 72 29 28 73 71  result_error)(sq
27525 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63  lite3_context*,c
27526 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 29 3b  onst char*,int);
27527 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c  .  void  (*resul
27528 74 5f 65 72 72 6f 72 31 36 29 28 73 71 6c 69 74  t_error16)(sqlit
27529 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73  e3_context*,cons
2752a 74 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 0a 20 20  t void*,int);.  
2752b 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 69  void  (*result_i
2752c 6e 74 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  nt)(sqlite3_cont
2752d 65 78 74 2a 2c 69 6e 74 29 3b 0a 20 20 76 6f 69  ext*,int);.  voi
2752e 64 20 20 28 2a 72 65 73 75 6c 74 5f 69 6e 74 36  d  (*result_int6
2752f 34 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  4)(sqlite3_conte
27530 78 74 2a 2c 73 71 6c 69 74 65 5f 69 6e 74 36 34  xt*,sqlite_int64
27531 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73  );.  void  (*res
27532 75 6c 74 5f 6e 75 6c 6c 29 28 73 71 6c 69 74 65  ult_null)(sqlite
27533 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 76  3_context*);.  v
27534 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 74 65  oid  (*result_te
27535 78 74 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  xt)(sqlite3_cont
27536 65 78 74 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  ext*,const char*
27537 2c 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69  ,int,void(*)(voi
27538 64 2a 29 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a  d*));.  void  (*
27539 72 65 73 75 6c 74 5f 74 65 78 74 31 36 29 28 73  result_text16)(s
2753a 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
2753b 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c  const void*,int,
2753c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b  void(*)(void*));
2753d 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c  .  void  (*resul
2753e 74 5f 74 65 78 74 31 36 62 65 29 28 73 71 6c 69  t_text16be)(sqli
2753f 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e  te3_context*,con
27540 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69  st void*,int,voi
27541 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20  d(*)(void*));.  
27542 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 74  void  (*result_t
27543 65 78 74 31 36 6c 65 29 28 73 71 6c 69 74 65 33  ext16le)(sqlite3
27544 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20  _context*,const 
27545 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a  void*,int,void(*
27546 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 76 6f 69  )(void*));.  voi
27547 64 20 20 28 2a 72 65 73 75 6c 74 5f 76 61 6c 75  d  (*result_valu
27548 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  e)(sqlite3_conte
27549 78 74 2a 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  xt*,sqlite3_valu
2754a 65 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a  e*);.  void * (*
2754b 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 29 28 73  rollback_hook)(s
2754c 71 6c 69 74 65 33 2a 2c 76 6f 69 64 28 2a 29 28  qlite3*,void(*)(
2754d 76 6f 69 64 2a 29 2c 76 6f 69 64 2a 29 3b 0a 20  void*),void*);. 
2754e 20 69 6e 74 20 20 28 2a 73 65 74 5f 61 75 74 68   int  (*set_auth
2754f 6f 72 69 7a 65 72 29 28 73 71 6c 69 74 65 33 2a  orizer)(sqlite3*
27550 2c 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e  ,int(*)(void*,in
27551 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f  t,const char*,co
27552 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20  nst char*,const 
27553 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72  char*,const char
27554 2a 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69  *),void*);.  voi
27555 64 20 20 28 2a 73 65 74 5f 61 75 78 64 61 74 61  d  (*set_auxdata
27556 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
27557 74 2a 2c 69 6e 74 2c 76 6f 69 64 2a 2c 76 6f 69  t*,int,void*,voi
27558 64 20 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20  d (*)(void*));. 
27559 20 63 68 61 72 20 2a 20 28 2a 73 6e 70 72 69 6e   char * (*snprin
2755a 74 66 29 28 69 6e 74 2c 63 68 61 72 2a 2c 63 6f  tf)(int,char*,co
2755b 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a  nst char*,...);.
2755c 20 20 69 6e 74 20 20 28 2a 73 74 65 70 29 28 73    int  (*step)(s
2755d 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 20  qlite3_stmt*);. 
2755e 20 69 6e 74 20 20 28 2a 74 61 62 6c 65 5f 63 6f   int  (*table_co
2755f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61 29 28 73  lumn_metadata)(s
27560 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68  qlite3*,const ch
27561 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  ar*,const char*,
27562 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 68 61 72  const char*,char
27563 20 63 6f 6e 73 74 2a 2a 2c 63 68 61 72 20 63 6f   const**,char co
27564 6e 73 74 2a 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 2c  nst**,int*,int*,
27565 69 6e 74 2a 29 3b 0a 20 20 76 6f 69 64 20 20 28  int*);.  void  (
27566 2a 74 68 72 65 61 64 5f 63 6c 65 61 6e 75 70 29  *thread_cleanup)
27567 28 76 6f 69 64 29 3b 0a 20 20 69 6e 74 20 20 28  (void);.  int  (
27568 2a 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 29 28  *total_changes)(
27569 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 76 6f 69  sqlite3*);.  voi
2756a 64 20 2a 20 28 2a 74 72 61 63 65 29 28 73 71 6c  d * (*trace)(sql
2756b 69 74 65 33 2a 2c 76 6f 69 64 28 2a 78 54 72 61  ite3*,void(*xTra
2756c 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20  ce)(void*,const 
2756d 63 68 61 72 2a 29 2c 76 6f 69 64 2a 29 3b 0a 20  char*),void*);. 
2756e 20 69 6e 74 20 20 28 2a 74 72 61 6e 73 66 65 72   int  (*transfer
2756f 5f 62 69 6e 64 69 6e 67 73 29 28 73 71 6c 69 74  _bindings)(sqlit
27570 65 33 5f 73 74 6d 74 2a 2c 73 71 6c 69 74 65 33  e3_stmt*,sqlite3
27571 5f 73 74 6d 74 2a 29 3b 0a 20 20 76 6f 69 64 20  _stmt*);.  void 
27572 2a 20 28 2a 75 70 64 61 74 65 5f 68 6f 6f 6b 29  * (*update_hook)
27573 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64 28 2a  (sqlite3*,void(*
27574 29 28 76 6f 69 64 2a 2c 69 6e 74 20 2c 63 68 61  )(void*,int ,cha
27575 72 20 63 6f 6e 73 74 2a 2c 63 68 61 72 20 63 6f  r const*,char co
27576 6e 73 74 2a 2c 73 71 6c 69 74 65 5f 69 6e 74 36  nst*,sqlite_int6
27577 34 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69  4),void*);.  voi
27578 64 20 2a 20 28 2a 75 73 65 72 5f 64 61 74 61 29  d * (*user_data)
27579 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
2757a 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  *);.  const void
2757b 20 2a 20 28 2a 76 61 6c 75 65 5f 62 6c 6f 62 29   * (*value_blob)
2757c 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
2757d 3b 0a 20 20 69 6e 74 20 20 28 2a 76 61 6c 75 65  ;.  int  (*value
2757e 5f 62 79 74 65 73 29 28 73 71 6c 69 74 65 33 5f  _bytes)(sqlite3_
2757f 76 61 6c 75 65 2a 29 3b 0a 20 20 69 6e 74 20 20  value*);.  int  
27580 28 2a 76 61 6c 75 65 5f 62 79 74 65 73 31 36 29  (*value_bytes16)
27581 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
27582 3b 0a 20 20 64 6f 75 62 6c 65 20 20 28 2a 76 61  ;.  double  (*va
27583 6c 75 65 5f 64 6f 75 62 6c 65 29 28 73 71 6c 69  lue_double)(sqli
27584 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 69  te3_value*);.  i
27585 6e 74 20 20 28 2a 76 61 6c 75 65 5f 69 6e 74 29  nt  (*value_int)
27586 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
27587 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  ;.  sqlite_int64
27588 20 20 28 2a 76 61 6c 75 65 5f 69 6e 74 36 34 29    (*value_int64)
27589 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
2758a 3b 0a 20 20 69 6e 74 20 20 28 2a 76 61 6c 75 65  ;.  int  (*value
2758b 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 29 28 73  _numeric_type)(s
2758c 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a  qlite3_value*);.
2758d 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
2758e 20 63 68 61 72 20 2a 20 28 2a 76 61 6c 75 65 5f   char * (*value_
2758f 74 65 78 74 29 28 73 71 6c 69 74 65 33 5f 76 61  text)(sqlite3_va
27590 6c 75 65 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 76  lue*);.  const v
27591 6f 69 64 20 2a 20 28 2a 76 61 6c 75 65 5f 74 65  oid * (*value_te
27592 78 74 31 36 29 28 73 71 6c 69 74 65 33 5f 76 61  xt16)(sqlite3_va
27593 6c 75 65 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 76  lue*);.  const v
27594 6f 69 64 20 2a 20 28 2a 76 61 6c 75 65 5f 74 65  oid * (*value_te
27595 78 74 31 36 62 65 29 28 73 71 6c 69 74 65 33 5f  xt16be)(sqlite3_
27596 76 61 6c 75 65 2a 29 3b 0a 20 20 63 6f 6e 73 74  value*);.  const
27597 20 76 6f 69 64 20 2a 20 28 2a 76 61 6c 75 65 5f   void * (*value_
27598 74 65 78 74 31 36 6c 65 29 28 73 71 6c 69 74 65  text16le)(sqlite
27599 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 69 6e 74  3_value*);.  int
2759a 20 20 28 2a 76 61 6c 75 65 5f 74 79 70 65 29 28    (*value_type)(
2759b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
2759c 0a 20 20 63 68 61 72 20 2a 28 2a 76 6d 70 72 69  .  char *(*vmpri
2759d 6e 74 66 29 28 63 6f 6e 73 74 20 63 68 61 72 2a  ntf)(const char*
2759e 2c 76 61 5f 6c 69 73 74 29 3b 0a 20 20 2f 2a 20  ,va_list);.  /* 
2759f 41 64 64 65 64 20 3f 3f 3f 20 2a 2f 0a 20 20 69  Added ??? */.  i
275a0 6e 74 20 28 2a 6f 76 65 72 6c 6f 61 64 5f 66 75  nt (*overload_fu
275a1 6e 63 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 2a  nction)(sqlite3*
275a2 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  , const char *zF
275a3 75 6e 63 4e 61 6d 65 2c 20 69 6e 74 20 6e 41 72  uncName, int nAr
275a4 67 29 3b 0a 20 20 2f 2a 20 41 64 64 65 64 20 62  g);.  /* Added b
275a5 79 20 33 2e 33 2e 31 33 20 2a 2f 0a 20 20 69 6e  y 3.3.13 */.  in
275a6 74 20 28 2a 70 72 65 70 61 72 65 5f 76 32 29 28  t (*prepare_v2)(
275a7 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63  sqlite3*,const c
275a8 68 61 72 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  har*,int,sqlite3
275a9 5f 73 74 6d 74 2a 2a 2c 63 6f 6e 73 74 20 63 68  _stmt**,const ch
275aa 61 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 70  ar**);.  int (*p
275ab 72 65 70 61 72 65 31 36 5f 76 32 29 28 73 71 6c  repare16_v2)(sql
275ac 69 74 65 33 2a 2c 63 6f 6e 73 74 20 76 6f 69 64  ite3*,const void
275ad 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 73 74  *,int,sqlite3_st
275ae 6d 74 2a 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  mt**,const void*
275af 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 63 6c 65 61  *);.  int (*clea
275b0 72 5f 62 69 6e 64 69 6e 67 73 29 28 73 71 6c 69  r_bindings)(sqli
275b1 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20 2f 2a  te3_stmt*);.  /*
275b2 20 41 64 64 65 64 20 62 79 20 33 2e 34 2e 31 20   Added by 3.4.1 
275b3 2a 2f 0a 20 20 69 6e 74 20 28 2a 63 72 65 61 74  */.  int (*creat
275b4 65 5f 6d 6f 64 75 6c 65 5f 76 32 29 28 73 71 6c  e_module_v2)(sql
275b5 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72  ite3*,const char
275b6 2a 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  *,const sqlite3_
275b7 6d 6f 64 75 6c 65 2a 2c 76 6f 69 64 2a 2c 76 6f  module*,void*,vo
275b8 69 64 20 28 2a 78 44 65 73 74 72 6f 79 29 28 76  id (*xDestroy)(v
275b9 6f 69 64 20 2a 29 29 3b 0a 20 20 2f 2a 20 41 64  oid *));.  /* Ad
275ba 64 65 64 20 62 79 20 33 2e 35 2e 30 20 2a 2f 0a  ded by 3.5.0 */.
275bb 20 20 69 6e 74 20 28 2a 62 69 6e 64 5f 7a 65 72    int (*bind_zer
275bc 6f 62 6c 6f 62 29 28 73 71 6c 69 74 65 33 5f 73  oblob)(sqlite3_s
275bd 74 6d 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20  tmt*,int,int);. 
275be 20 69 6e 74 20 28 2a 62 6c 6f 62 5f 62 79 74 65   int (*blob_byte
275bf 73 29 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a  s)(sqlite3_blob*
275c0 29 3b 0a 20 20 69 6e 74 20 28 2a 62 6c 6f 62 5f  );.  int (*blob_
275c1 63 6c 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 62  close)(sqlite3_b
275c2 6c 6f 62 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 62  lob*);.  int (*b
275c3 6c 6f 62 5f 6f 70 65 6e 29 28 73 71 6c 69 74 65  lob_open)(sqlite
275c4 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63  3*,const char*,c
275c5 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74  onst char*,const
275c6 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 5f 69   char*,sqlite3_i
275c7 6e 74 36 34 2c 69 6e 74 2c 73 71 6c 69 74 65 33  nt64,int,sqlite3
275c8 5f 62 6c 6f 62 2a 2a 29 3b 0a 20 20 69 6e 74 20  _blob**);.  int 
275c9 28 2a 62 6c 6f 62 5f 72 65 61 64 29 28 73 71 6c  (*blob_read)(sql
275ca 69 74 65 33 5f 62 6c 6f 62 2a 2c 76 6f 69 64 2a  ite3_blob*,void*
275cb 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20 20 69 6e 74  ,int,int);.  int
275cc 20 28 2a 62 6c 6f 62 5f 77 72 69 74 65 29 28 73   (*blob_write)(s
275cd 71 6c 69 74 65 33 5f 62 6c 6f 62 2a 2c 63 6f 6e  qlite3_blob*,con
275ce 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74  st void*,int,int
275cf 29 3b 0a 20 20 69 6e 74 20 28 2a 63 72 65 61 74  );.  int (*creat
275d0 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 29 28  e_collation_v2)(
275d1 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63  sqlite3*,const c
275d2 68 61 72 2a 2c 69 6e 74 2c 76 6f 69 64 2a 2c 69  har*,int,void*,i
275d3 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 2c  nt(*)(void*,int,
275d4 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c  const void*,int,
275d5 63 6f 6e 73 74 20 76 6f 69 64 2a 29 2c 76 6f 69  const void*),voi
275d6 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20  d(*)(void*));.  
275d7 69 6e 74 20 28 2a 66 69 6c 65 5f 63 6f 6e 74 72  int (*file_contr
275d8 6f 6c 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e  ol)(sqlite3*,con
275d9 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 76 6f 69  st char*,int,voi
275da 64 2a 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69  d*);.  sqlite3_i
275db 6e 74 36 34 20 28 2a 6d 65 6d 6f 72 79 5f 68 69  nt64 (*memory_hi
275dc 67 68 77 61 74 65 72 29 28 69 6e 74 29 3b 0a 20  ghwater)(int);. 
275dd 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 28   sqlite3_int64 (
275de 2a 6d 65 6d 6f 72 79 5f 75 73 65 64 29 28 76 6f  *memory_used)(vo
275df 69 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  id);.  sqlite3_m
275e0 75 74 65 78 20 2a 28 2a 6d 75 74 65 78 5f 61 6c  utex *(*mutex_al
275e1 6c 6f 63 29 28 69 6e 74 29 3b 0a 20 20 76 6f 69  loc)(int);.  voi
275e2 64 20 28 2a 6d 75 74 65 78 5f 65 6e 74 65 72 29  d (*mutex_enter)
275e3 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29  (sqlite3_mutex*)
275e4 3b 0a 20 20 76 6f 69 64 20 28 2a 6d 75 74 65 78  ;.  void (*mutex
275e5 5f 66 72 65 65 29 28 73 71 6c 69 74 65 33 5f 6d  _free)(sqlite3_m
275e6 75 74 65 78 2a 29 3b 0a 20 20 76 6f 69 64 20 28  utex*);.  void (
275e7 2a 6d 75 74 65 78 5f 6c 65 61 76 65 29 28 73 71  *mutex_leave)(sq
275e8 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 20  lite3_mutex*);. 
275e9 20 69 6e 74 20 28 2a 6d 75 74 65 78 5f 74 72 79   int (*mutex_try
275ea 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a  )(sqlite3_mutex*
275eb 29 3b 0a 20 20 69 6e 74 20 28 2a 6f 70 65 6e 5f  );.  int (*open_
275ec 76 32 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c  v2)(const char*,
275ed 73 71 6c 69 74 65 33 2a 2a 2c 69 6e 74 2c 63 6f  sqlite3**,int,co
275ee 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 69 6e  nst char*);.  in
275ef 74 20 28 2a 72 65 6c 65 61 73 65 5f 6d 65 6d 6f  t (*release_memo
275f0 72 79 29 28 69 6e 74 29 3b 0a 20 20 76 6f 69 64  ry)(int);.  void
275f1 20 28 2a 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f   (*result_error_
275f2 6e 6f 6d 65 6d 29 28 73 71 6c 69 74 65 33 5f 63  nomem)(sqlite3_c
275f3 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 76 6f 69 64  ontext*);.  void
275f4 20 28 2a 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f   (*result_error_
275f5 74 6f 6f 62 69 67 29 28 73 71 6c 69 74 65 33 5f  toobig)(sqlite3_
275f6 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 69 6e 74  context*);.  int
275f7 20 28 2a 73 6c 65 65 70 29 28 69 6e 74 29 3b 0a   (*sleep)(int);.
275f8 20 20 76 6f 69 64 20 28 2a 73 6f 66 74 5f 68 65    void (*soft_he
275f9 61 70 5f 6c 69 6d 69 74 29 28 69 6e 74 29 3b 0a  ap_limit)(int);.
275fa 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 28    sqlite3_vfs *(
275fb 2a 76 66 73 5f 66 69 6e 64 29 28 63 6f 6e 73 74  *vfs_find)(const
275fc 20 63 68 61 72 2a 29 3b 0a 20 20 69 6e 74 20 28   char*);.  int (
275fd 2a 76 66 73 5f 72 65 67 69 73 74 65 72 29 28 73  *vfs_register)(s
275fe 71 6c 69 74 65 33 5f 76 66 73 2a 2c 69 6e 74 29  qlite3_vfs*,int)
275ff 3b 0a 20 20 69 6e 74 20 28 2a 76 66 73 5f 75 6e  ;.  int (*vfs_un
27600 72 65 67 69 73 74 65 72 29 28 73 71 6c 69 74 65  register)(sqlite
27601 33 5f 76 66 73 2a 29 3b 0a 20 20 69 6e 74 20 28  3_vfs*);.  int (
27602 2a 78 74 68 72 65 61 64 73 61 66 65 29 28 76 6f  *xthreadsafe)(vo
27603 69 64 29 3b 0a 20 20 76 6f 69 64 20 28 2a 72 65  id);.  void (*re
27604 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 29 28 73  sult_zeroblob)(s
27605 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
27606 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 28 2a 72  int);.  void (*r
27607 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65  esult_error_code
27608 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
27609 74 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28  t*,int);.  int (
2760a 2a 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 29 28 69  *test_control)(i
2760b 6e 74 2c 20 2e 2e 2e 29 3b 0a 20 20 76 6f 69 64  nt, ...);.  void
2760c 20 28 2a 72 61 6e 64 6f 6d 6e 65 73 73 29 28 69   (*randomness)(i
2760d 6e 74 2c 76 6f 69 64 2a 29 3b 0a 20 20 73 71 6c  nt,void*);.  sql
2760e 69 74 65 33 20 2a 28 2a 63 6f 6e 74 65 78 74 5f  ite3 *(*context_
2760f 64 62 5f 68 61 6e 64 6c 65 29 28 73 71 6c 69 74  db_handle)(sqlit
27610 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20  e3_context*);.  
27611 69 6e 74 20 28 2a 65 78 74 65 6e 64 65 64 5f 72  int (*extended_r
27612 65 73 75 6c 74 5f 63 6f 64 65 73 29 28 73 71 6c  esult_codes)(sql
27613 69 74 65 33 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e  ite3*,int);.  in
27614 74 20 28 2a 6c 69 6d 69 74 29 28 73 71 6c 69 74  t (*limit)(sqlit
27615 65 33 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20 20  e3*,int,int);.  
27616 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 28 2a  sqlite3_stmt *(*
27617 6e 65 78 74 5f 73 74 6d 74 29 28 73 71 6c 69 74  next_stmt)(sqlit
27618 65 33 2a 2c 73 71 6c 69 74 65 33 5f 73 74 6d 74  e3*,sqlite3_stmt
27619 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  *);.  const char
2761a 20 2a 28 2a 73 71 6c 29 28 73 71 6c 69 74 65 33   *(*sql)(sqlite3
2761b 5f 73 74 6d 74 2a 29 3b 0a 20 20 69 6e 74 20 28  _stmt*);.  int (
2761c 2a 73 74 61 74 75 73 29 28 69 6e 74 2c 69 6e 74  *status)(int,int
2761d 2a 2c 69 6e 74 2a 2c 69 6e 74 29 3b 0a 7d 3b 0a  *,int*,int);.};.
2761e 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
2761f 77 69 6e 67 20 6d 61 63 72 6f 73 20 72 65 64 65  wing macros rede
27620 66 69 6e 65 20 74 68 65 20 41 50 49 20 72 6f 75  fine the API rou
27621 74 69 6e 65 73 20 73 6f 20 74 68 61 74 20 74 68  tines so that th
27622 65 79 20 61 72 65 0a 2a 2a 20 72 65 64 69 72 65  ey are.** redire
27623 63 74 65 64 20 74 68 72 6f 75 67 68 74 20 74 68  cted throught th
27624 65 20 67 6c 6f 62 61 6c 20 73 71 6c 69 74 65 33  e global sqlite3
27625 5f 61 70 69 20 73 74 72 75 63 74 75 72 65 2e 0a  _api structure..
27626 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65  **.** This heade
27627 72 20 66 69 6c 65 20 69 73 20 61 6c 73 6f 20 75  r file is also u
27628 73 65 64 20 62 79 20 74 68 65 20 6c 6f 61 64 65  sed by the loade
27629 78 74 2e 63 20 73 6f 75 72 63 65 20 66 69 6c 65  xt.c source file
2762a 0a 2a 2a 20 28 70 61 72 74 20 6f 66 20 74 68 65  .** (part of the
2762b 20 6d 61 69 6e 20 53 51 4c 69 74 65 20 6c 69 62   main SQLite lib
2762c 72 61 72 79 20 2d 20 6e 6f 74 20 61 6e 20 65 78  rary - not an ex
2762d 74 65 6e 73 69 6f 6e 29 20 73 6f 20 74 68 61 74  tension) so that
2762e 0a 2a 2a 20 69 74 20 63 61 6e 20 67 65 74 20 61  .** it can get a
2762f 63 63 65 73 73 20 74 6f 20 74 68 65 20 73 71 6c  ccess to the sql
27630 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65  ite3_api_routine
27631 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 64  s structure.** d
27632 65 66 69 6e 69 74 69 6f 6e 2e 20 20 42 75 74 20  efinition.  But 
27633 74 68 65 20 6d 61 69 6e 20 6c 69 62 72 61 72 79  the main library
27634 20 64 6f 65 73 20 6e 6f 74 20 77 61 6e 74 20 74   does not want t
27635 6f 20 72 65 64 65 66 69 6e 65 0a 2a 2a 20 74 68  o redefine.** th
27636 65 20 41 50 49 2e 20 20 53 6f 20 74 68 65 20 72  e API.  So the r
27637 65 64 65 66 69 6e 69 74 69 6f 6e 20 6d 61 63 72  edefinition macr
27638 6f 73 20 61 72 65 20 6f 6e 6c 79 20 76 61 6c 69  os are only vali
27639 64 20 69 66 20 74 68 65 0a 2a 2a 20 53 51 4c 49  d if the.** SQLI
2763a 54 45 5f 43 4f 52 45 20 6d 61 63 72 6f 73 20 69  TE_CORE macros i
2763b 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a  s undefined..*/.
2763c 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 43  #ifndef SQLITE_C
2763d 4f 52 45 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ORE.#define sqli
2763e 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
2763f 6e 74 65 78 74 20 20 20 20 20 20 73 71 6c 69 74  ntext      sqlit
27640 65 33 5f 61 70 69 2d 3e 61 67 67 72 65 67 61 74  e3_api->aggregat
27641 65 5f 63 6f 6e 74 65 78 74 0a 23 69 66 6e 64 65  e_context.#ifnde
27642 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45  f SQLITE_OMIT_DE
27643 50 52 45 43 41 54 45 44 0a 23 64 65 66 69 6e 65  PRECATED.#define
27644 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61   sqlite3_aggrega
27645 74 65 5f 63 6f 75 6e 74 20 20 20 20 20 20 20 20  te_count        
27646 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 61 67 67  sqlite3_api->agg
27647 72 65 67 61 74 65 5f 63 6f 75 6e 74 0a 23 65 6e  regate_count.#en
27648 64 69 66 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  dif.#define sqli
27649 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 20 20  te3_bind_blob   
2764a 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2764b 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 62 6c 6f  e3_api->bind_blo
2764c 62 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  b.#define sqlite
2764d 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 20 20 20  3_bind_double   
2764e 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2764f 5f 61 70 69 2d 3e 62 69 6e 64 5f 64 6f 75 62 6c  _api->bind_doubl
27650 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
27651 33 5f 62 69 6e 64 5f 69 6e 74 20 20 20 20 20 20  3_bind_int      
27652 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
27653 5f 61 70 69 2d 3e 62 69 6e 64 5f 69 6e 74 0a 23  _api->bind_int.#
27654 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62  define sqlite3_b
27655 69 6e 64 5f 69 6e 74 36 34 20 20 20 20 20 20 20  ind_int64       
27656 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
27657 69 2d 3e 62 69 6e 64 5f 69 6e 74 36 34 0a 23 64  i->bind_int64.#d
27658 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69  efine sqlite3_bi
27659 6e 64 5f 6e 75 6c 6c 20 20 20 20 20 20 20 20 20  nd_null         
2765a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
2765b 2d 3e 62 69 6e 64 5f 6e 75 6c 6c 0a 23 64 65 66  ->bind_null.#def
2765c 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ine sqlite3_bind
2765d 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74  _parameter_count
2765e 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
2765f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63  bind_parameter_c
27660 6f 75 6e 74 0a 23 64 65 66 69 6e 65 20 73 71 6c  ount.#define sql
27661 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
27662 74 65 72 5f 69 6e 64 65 78 20 20 20 73 71 6c 69  ter_index   sqli
27663 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 70 61  te3_api->bind_pa
27664 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 0a 23 64  rameter_index.#d
27665 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69  efine sqlite3_bi
27666 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d  nd_parameter_nam
27667 65 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69  e    sqlite3_api
27668 2d 3e 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72  ->bind_parameter
27669 5f 6e 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71  _name.#define sq
2766a 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 20  lite3_bind_text 
2766b 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
2766c 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 74  ite3_api->bind_t
2766d 65 78 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ext.#define sqli
2766e 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 20  te3_bind_text16 
2766f 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
27670 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 74 65 78  e3_api->bind_tex
27671 74 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  t16.#define sqli
27672 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 20 20  te3_bind_value  
27673 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
27674 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 76 61 6c  e3_api->bind_val
27675 75 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ue.#define sqlit
27676 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 20  e3_busy_handler 
27677 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
27678 33 5f 61 70 69 2d 3e 62 75 73 79 5f 68 61 6e 64  3_api->busy_hand
27679 6c 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ler.#define sqli
2767a 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74  te3_busy_timeout
2767b 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2767c 65 33 5f 61 70 69 2d 3e 62 75 73 79 5f 74 69 6d  e3_api->busy_tim
2767d 65 6f 75 74 0a 23 64 65 66 69 6e 65 20 73 71 6c  eout.#define sql
2767e 69 74 65 33 5f 63 68 61 6e 67 65 73 20 20 20 20  ite3_changes    
2767f 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
27680 74 65 33 5f 61 70 69 2d 3e 63 68 61 6e 67 65 73  te3_api->changes
27681 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
27682 5f 63 6c 6f 73 65 20 20 20 20 20 20 20 20 20 20  _close          
27683 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
27684 61 70 69 2d 3e 63 6c 6f 73 65 0a 23 64 65 66 69  api->close.#defi
27685 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61  ne sqlite3_colla
27686 74 69 6f 6e 5f 6e 65 65 64 65 64 20 20 20 20 20  tion_needed     
27687 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
27688 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 0a  ollation_needed.
27689 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
2768a 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
2768b 31 36 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61  16     sqlite3_a
2768c 70 69 2d 3e 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65  pi->collation_ne
2768d 65 64 65 64 31 36 0a 23 64 65 66 69 6e 65 20 73  eded16.#define s
2768e 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c  qlite3_column_bl
2768f 6f 62 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ob            sq
27690 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d  lite3_api->colum
27691 6e 5f 62 6c 6f 62 0a 23 64 65 66 69 6e 65 20 73  n_blob.#define s
27692 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
27693 74 65 73 20 20 20 20 20 20 20 20 20 20 20 73 71  tes           sq
27694 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d  lite3_api->colum
27695 6e 5f 62 79 74 65 73 0a 23 64 65 66 69 6e 65 20  n_bytes.#define 
27696 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
27697 79 74 65 73 31 36 20 20 20 20 20 20 20 20 20 73  ytes16         s
27698 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75  qlite3_api->colu
27699 6d 6e 5f 62 79 74 65 73 31 36 0a 23 64 65 66 69  mn_bytes16.#defi
2769a 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
2769b 6e 5f 63 6f 75 6e 74 20 20 20 20 20 20 20 20 20  n_count         
2769c 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
2769d 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 0a 23 64 65 66  olumn_count.#def
2769e 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
2769f 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65  mn_database_name
276a0 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
276a1 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f  column_database_
276a2 6e 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  name.#define sql
276a3 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61  ite3_column_data
276a4 62 61 73 65 5f 6e 61 6d 65 31 36 20 73 71 6c 69  base_name16 sqli
276a5 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f  te3_api->column_
276a6 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 0a  database_name16.
276a7 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
276a8 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 20  column_decltype 
276a9 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
276aa 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74  pi->column_declt
276ab 79 70 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ype.#define sqli
276ac 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74  te3_column_declt
276ad 79 70 65 31 36 20 20 20 20 20 20 73 71 6c 69 74  ype16      sqlit
276ae 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64  e3_api->column_d
276af 65 63 6c 74 79 70 65 31 36 0a 23 64 65 66 69 6e  ecltype16.#defin
276b0 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
276b1 5f 64 6f 75 62 6c 65 20 20 20 20 20 20 20 20 20  _double         
276b2 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f   sqlite3_api->co
276b3 6c 75 6d 6e 5f 64 6f 75 62 6c 65 0a 23 64 65 66  lumn_double.#def
276b4 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
276b5 6d 6e 5f 69 6e 74 20 20 20 20 20 20 20 20 20 20  mn_int          
276b6 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
276b7 63 6f 6c 75 6d 6e 5f 69 6e 74 0a 23 64 65 66 69  column_int.#defi
276b8 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
276b9 6e 5f 69 6e 74 36 34 20 20 20 20 20 20 20 20 20  n_int64         
276ba 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
276bb 6f 6c 75 6d 6e 5f 69 6e 74 36 34 0a 23 64 65 66  olumn_int64.#def
276bc 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
276bd 6d 6e 5f 6e 61 6d 65 20 20 20 20 20 20 20 20 20  mn_name         
276be 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
276bf 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 0a 23 64 65 66  column_name.#def
276c0 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
276c1 6d 6e 5f 6e 61 6d 65 31 36 20 20 20 20 20 20 20  mn_name16       
276c2 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
276c3 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 0a 23 64  column_name16.#d
276c4 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
276c5 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65  lumn_origin_name
276c6 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
276c7 2d 3e 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f  ->column_origin_
276c8 6e 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  name.#define sql
276c9 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67  ite3_column_orig
276ca 69 6e 5f 6e 61 6d 65 31 36 20 20 20 73 71 6c 69  in_name16   sqli
276cb 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f  te3_api->column_
276cc 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36 0a 23 64  origin_name16.#d
276cd 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
276ce 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 20  lumn_table_name 
276cf 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
276d0 2d 3e 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e  ->column_table_n
276d1 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ame.#define sqli
276d2 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65  te3_column_table
276d3 5f 6e 61 6d 65 31 36 20 20 20 20 73 71 6c 69 74  _name16    sqlit
276d4 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 74  e3_api->column_t
276d5 61 62 6c 65 5f 6e 61 6d 65 31 36 0a 23 64 65 66  able_name16.#def
276d6 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
276d7 6d 6e 5f 74 65 78 74 20 20 20 20 20 20 20 20 20  mn_text         
276d8 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
276d9 63 6f 6c 75 6d 6e 5f 74 65 78 74 0a 23 64 65 66  column_text.#def
276da 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
276db 6d 6e 5f 74 65 78 74 31 36 20 20 20 20 20 20 20  mn_text16       
276dc 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
276dd 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 0a 23 64  column_text16.#d
276de 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
276df 6c 75 6d 6e 5f 74 79 70 65 20 20 20 20 20 20 20  lumn_type       
276e0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
276e1 2d 3e 63 6f 6c 75 6d 6e 5f 74 79 70 65 0a 23 64  ->column_type.#d
276e2 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
276e3 6c 75 6d 6e 5f 76 61 6c 75 65 20 20 20 20 20 20  lumn_value      
276e4 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
276e5 2d 3e 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 0a 23  ->column_value.#
276e6 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
276e7 6f 6d 6d 69 74 5f 68 6f 6f 6b 20 20 20 20 20 20  ommit_hook      
276e8 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
276e9 69 2d 3e 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 0a 23  i->commit_hook.#
276ea 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
276eb 6f 6d 70 6c 65 74 65 20 20 20 20 20 20 20 20 20  omplete         
276ec 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
276ed 69 2d 3e 63 6f 6d 70 6c 65 74 65 0a 23 64 65 66  i->complete.#def
276ee 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70  ine sqlite3_comp
276ef 6c 65 74 65 31 36 20 20 20 20 20 20 20 20 20 20  lete16          
276f0 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
276f1 63 6f 6d 70 6c 65 74 65 31 36 0a 23 64 65 66 69  complete16.#defi
276f2 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  ne sqlite3_creat
276f3 65 5f 63 6f 6c 6c 61 74 69 6f 6e 20 20 20 20 20  e_collation     
276f4 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
276f5 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 0a  reate_collation.
276f6 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
276f7 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
276f8 31 36 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61  16     sqlite3_a
276f9 70 69 2d 3e 63 72 65 61 74 65 5f 63 6f 6c 6c 61  pi->create_colla
276fa 74 69 6f 6e 31 36 0a 23 64 65 66 69 6e 65 20 73  tion16.#define s
276fb 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
276fc 6e 63 74 69 6f 6e 20 20 20 20 20 20 20 20 73 71  nction        sq
276fd 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72 65 61 74  lite3_api->creat
276fe 65 5f 66 75 6e 63 74 69 6f 6e 0a 23 64 65 66 69  e_function.#defi
276ff 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  ne sqlite3_creat
27700 65 5f 66 75 6e 63 74 69 6f 6e 31 36 20 20 20 20  e_function16    
27701 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
27702 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36  reate_function16
27703 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
27704 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 20 20  _create_module  
27705 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
27706 61 70 69 2d 3e 63 72 65 61 74 65 5f 6d 6f 64 75  api->create_modu
27707 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  le.#define sqlit
27708 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
27709 5f 76 32 20 20 20 20 20 20 20 73 71 6c 69 74 65  _v2       sqlite
2770a 33 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f 6d 6f  3_api->create_mo
2770b 64 75 6c 65 5f 76 32 0a 23 64 65 66 69 6e 65 20  dule_v2.#define 
2770c 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75  sqlite3_data_cou
2770d 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 73  nt             s
2770e 71 6c 69 74 65 33 5f 61 70 69 2d 3e 64 61 74 61  qlite3_api->data
2770f 5f 63 6f 75 6e 74 0a 23 64 65 66 69 6e 65 20 73  _count.#define s
27710 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65  qlite3_db_handle
27711 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
27712 6c 69 74 65 33 5f 61 70 69 2d 3e 64 62 5f 68 61  lite3_api->db_ha
27713 6e 64 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  ndle.#define sql
27714 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61  ite3_declare_vta
27715 62 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  b           sqli
27716 74 65 33 5f 61 70 69 2d 3e 64 65 63 6c 61 72 65  te3_api->declare
27717 5f 76 74 61 62 0a 23 64 65 66 69 6e 65 20 73 71  _vtab.#define sq
27718 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61  lite3_enable_sha
27719 72 65 64 5f 63 61 63 68 65 20 20 20 20 73 71 6c  red_cache    sql
2771a 69 74 65 33 5f 61 70 69 2d 3e 65 6e 61 62 6c 65  ite3_api->enable
2771b 5f 73 68 61 72 65 64 5f 63 61 63 68 65 0a 23 64  _shared_cache.#d
2771c 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 72  efine sqlite3_er
2771d 72 63 6f 64 65 20 20 20 20 20 20 20 20 20 20 20  rcode           
2771e 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
2771f 2d 3e 65 72 72 63 6f 64 65 0a 23 64 65 66 69 6e  ->errcode.#defin
27720 65 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67  e sqlite3_errmsg
27721 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27722 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65 72   sqlite3_api->er
27723 72 6d 73 67 0a 23 64 65 66 69 6e 65 20 73 71 6c  rmsg.#define sql
27724 69 74 65 33 5f 65 72 72 6d 73 67 31 36 20 20 20  ite3_errmsg16   
27725 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
27726 74 65 33 5f 61 70 69 2d 3e 65 72 72 6d 73 67 31  te3_api->errmsg1
27727 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  6.#define sqlite
27728 33 5f 65 78 65 63 20 20 20 20 20 20 20 20 20 20  3_exec          
27729 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2772a 5f 61 70 69 2d 3e 65 78 65 63 0a 23 69 66 6e 64  _api->exec.#ifnd
2772b 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
2772c 45 50 52 45 43 41 54 45 44 0a 23 64 65 66 69 6e  EPRECATED.#defin
2772d 65 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72 65  e sqlite3_expire
2772e 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d               
2772f 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65 78   sqlite3_api->ex
27730 70 69 72 65 64 0a 23 65 6e 64 69 66 0a 23 64 65  pired.#endif.#de
27731 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 66 69 6e  fine sqlite3_fin
27732 61 6c 69 7a 65 20 20 20 20 20 20 20 20 20 20 20  alize           
27733 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
27734 3e 66 69 6e 61 6c 69 7a 65 0a 23 64 65 66 69 6e  >finalize.#defin
27735 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65 20 20  e sqlite3_free  
27736 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27737 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 66 72   sqlite3_api->fr
27738 65 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ee.#define sqlit
27739 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 20 20 20  e3_free_table   
2773a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2773b 33 5f 61 70 69 2d 3e 66 72 65 65 5f 74 61 62 6c  3_api->free_tabl
2773c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
2773d 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74  3_get_autocommit
2773e 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2773f 5f 61 70 69 2d 3e 67 65 74 5f 61 75 74 6f 63 6f  _api->get_autoco
27740 6d 6d 69 74 0a 23 64 65 66 69 6e 65 20 73 71 6c  mmit.#define sql
27741 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61  ite3_get_auxdata
27742 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
27743 74 65 33 5f 61 70 69 2d 3e 67 65 74 5f 61 75 78  te3_api->get_aux
27744 64 61 74 61 0a 23 64 65 66 69 6e 65 20 73 71 6c  data.#define sql
27745 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 20 20  ite3_get_table  
27746 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
27747 74 65 33 5f 61 70 69 2d 3e 67 65 74 5f 74 61 62  te3_api->get_tab
27748 6c 65 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  le.#ifndef SQLIT
27749 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45  E_OMIT_DEPRECATE
2774a 44 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  D.#define sqlite
2774b 33 5f 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72  3_global_recover
2774c 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2774d 5f 61 70 69 2d 3e 67 6c 6f 62 61 6c 5f 72 65 63  _api->global_rec
2774e 6f 76 65 72 0a 23 65 6e 64 69 66 0a 23 64 65 66  over.#endif.#def
2774f 69 6e 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65  ine sqlite3_inte
27750 72 72 75 70 74 20 20 20 20 20 20 20 20 20 20 20  rrupt           
27751 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
27752 69 6e 74 65 72 72 75 70 74 78 0a 23 64 65 66 69  interruptx.#defi
27753 6e 65 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f  ne sqlite3_last_
27754 69 6e 73 65 72 74 5f 72 6f 77 69 64 20 20 20 20  insert_rowid    
27755 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6c    sqlite3_api->l
27756 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64  ast_insert_rowid
27757 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
27758 5f 6c 69 62 76 65 72 73 69 6f 6e 20 20 20 20 20  _libversion     
27759 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
2775a 61 70 69 2d 3e 6c 69 62 76 65 72 73 69 6f 6e 0a  api->libversion.
2775b 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
2775c 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65  libversion_numbe
2775d 72 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61  r      sqlite3_a
2775e 70 69 2d 3e 6c 69 62 76 65 72 73 69 6f 6e 5f 6e  pi->libversion_n
2775f 75 6d 62 65 72 0a 23 64 65 66 69 6e 65 20 73 71  umber.#define sq
27760 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 20 20 20  lite3_malloc    
27761 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
27762 69 74 65 33 5f 61 70 69 2d 3e 6d 61 6c 6c 6f 63  ite3_api->malloc
27763 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
27764 5f 6d 70 72 69 6e 74 66 20 20 20 20 20 20 20 20  _mprintf        
27765 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
27766 61 70 69 2d 3e 6d 70 72 69 6e 74 66 0a 23 64 65  api->mprintf.#de
27767 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6f 70 65  fine sqlite3_ope
27768 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n               
27769 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
2776a 3e 6f 70 65 6e 0a 23 64 65 66 69 6e 65 20 73 71  >open.#define sq
2776b 6c 69 74 65 33 5f 6f 70 65 6e 31 36 20 20 20 20  lite3_open16    
2776c 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
2776d 69 74 65 33 5f 61 70 69 2d 3e 6f 70 65 6e 31 36  ite3_api->open16
2776e 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
2776f 5f 70 72 65 70 61 72 65 20 20 20 20 20 20 20 20  _prepare        
27770 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
27771 61 70 69 2d 3e 70 72 65 70 61 72 65 0a 23 64 65  api->prepare.#de
27772 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 65  fine sqlite3_pre
27773 70 61 72 65 31 36 20 20 20 20 20 20 20 20 20 20  pare16          
27774 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
27775 3e 70 72 65 70 61 72 65 31 36 0a 23 64 65 66 69  >prepare16.#defi
27776 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  ne sqlite3_prepa
27777 72 65 5f 76 32 20 20 20 20 20 20 20 20 20 20 20  re_v2           
27778 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70    sqlite3_api->p
27779 72 65 70 61 72 65 5f 76 32 0a 23 64 65 66 69 6e  repare_v2.#defin
2777a 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  e sqlite3_prepar
2777b 65 31 36 5f 76 32 20 20 20 20 20 20 20 20 20 20  e16_v2          
2777c 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72   sqlite3_api->pr
2777d 65 70 61 72 65 31 36 5f 76 32 0a 23 64 65 66 69  epare16_v2.#defi
2777e 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 6f 66 69  ne sqlite3_profi
2777f 6c 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  le              
27780 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70    sqlite3_api->p
27781 72 6f 66 69 6c 65 0a 23 64 65 66 69 6e 65 20 73  rofile.#define s
27782 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f  qlite3_progress_
27783 68 61 6e 64 6c 65 72 20 20 20 20 20 20 20 73 71  handler       sq
27784 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72 6f 67 72  lite3_api->progr
27785 65 73 73 5f 68 61 6e 64 6c 65 72 0a 23 64 65 66  ess_handler.#def
27786 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 61 6c  ine sqlite3_real
27787 6c 6f 63 20 20 20 20 20 20 20 20 20 20 20 20 20  loc             
27788 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
27789 72 65 61 6c 6c 6f 63 0a 23 64 65 66 69 6e 65 20  realloc.#define 
2778a 73 71 6c 69 74 65 33 5f 72 65 73 65 74 20 20 20  sqlite3_reset   
2778b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
2778c 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 65  qlite3_api->rese
2778d 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  t.#define sqlite
2778e 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 20 20  3_result_blob   
2778f 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
27790 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 62 6c 6f  _api->result_blo
27791 62 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  b.#define sqlite
27792 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 20  3_result_double 
27793 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
27794 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 64 6f 75  _api->result_dou
27795 62 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ble.#define sqli
27796 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
27797 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
27798 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 65  e3_api->result_e
27799 72 72 6f 72 0a 23 64 65 66 69 6e 65 20 73 71 6c  rror.#define sql
2779a 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
2779b 72 31 36 20 20 20 20 20 20 20 20 20 73 71 6c 69  r16         sqli
2779c 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f  te3_api->result_
2779d 65 72 72 6f 72 31 36 0a 23 64 65 66 69 6e 65 20  error16.#define 
2779e 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
2779f 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 73  nt             s
277a0 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75  qlite3_api->resu
277a1 6c 74 5f 69 6e 74 0a 23 64 65 66 69 6e 65 20 73  lt_int.#define s
277a2 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
277a3 74 36 34 20 20 20 20 20 20 20 20 20 20 20 73 71  t64           sq
277a4 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c  lite3_api->resul
277a5 74 5f 69 6e 74 36 34 0a 23 64 65 66 69 6e 65 20  t_int64.#define 
277a6 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
277a7 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 73  ull            s
277a8 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75  qlite3_api->resu
277a9 6c 74 5f 6e 75 6c 6c 0a 23 64 65 66 69 6e 65 20  lt_null.#define 
277aa 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
277ab 65 78 74 20 20 20 20 20 20 20 20 20 20 20 20 73  ext            s
277ac 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75  qlite3_api->resu
277ad 6c 74 5f 74 65 78 74 0a 23 64 65 66 69 6e 65 20  lt_text.#define 
277ae 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
277af 65 78 74 31 36 20 20 20 20 20 20 20 20 20 20 73  ext16          s
277b0 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75  qlite3_api->resu
277b1 6c 74 5f 74 65 78 74 31 36 0a 23 64 65 66 69 6e  lt_text16.#defin
277b2 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
277b3 5f 74 65 78 74 31 36 62 65 20 20 20 20 20 20 20  _text16be       
277b4 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65   sqlite3_api->re
277b5 73 75 6c 74 5f 74 65 78 74 31 36 62 65 0a 23 64  sult_text16be.#d
277b6 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65  efine sqlite3_re
277b7 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 20 20 20  sult_text16le   
277b8 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
277b9 2d 3e 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c  ->result_text16l
277ba 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
277bb 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 20 20  3_result_value  
277bc 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
277bd 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 76 61 6c  _api->result_val
277be 75 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ue.#define sqlit
277bf 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b  e3_rollback_hook
277c0 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
277c1 33 5f 61 70 69 2d 3e 72 6f 6c 6c 62 61 63 6b 5f  3_api->rollback_
277c2 68 6f 6f 6b 0a 23 64 65 66 69 6e 65 20 73 71 6c  hook.#define sql
277c3 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69  ite3_set_authori
277c4 7a 65 72 20 20 20 20 20 20 20 20 20 73 71 6c 69  zer         sqli
277c5 74 65 33 5f 61 70 69 2d 3e 73 65 74 5f 61 75 74  te3_api->set_aut
277c6 68 6f 72 69 7a 65 72 0a 23 64 65 66 69 6e 65 20  horizer.#define 
277c7 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64  sqlite3_set_auxd
277c8 61 74 61 20 20 20 20 20 20 20 20 20 20 20 20 73  ata            s
277c9 71 6c 69 74 65 33 5f 61 70 69 2d 3e 73 65 74 5f  qlite3_api->set_
277ca 61 75 78 64 61 74 61 0a 23 64 65 66 69 6e 65 20  auxdata.#define 
277cb 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
277cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
277cd 71 6c 69 74 65 33 5f 61 70 69 2d 3e 73 6e 70 72  qlite3_api->snpr
277ce 69 6e 74 66 0a 23 64 65 66 69 6e 65 20 73 71 6c  intf.#define sql
277cf 69 74 65 33 5f 73 74 65 70 20 20 20 20 20 20 20  ite3_step       
277d0 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
277d1 74 65 33 5f 61 70 69 2d 3e 73 74 65 70 0a 23 64  te3_api->step.#d
277d2 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 61  efine sqlite3_ta
277d3 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64  ble_column_metad
277d4 61 74 61 20 20 73 71 6c 69 74 65 33 5f 61 70 69  ata  sqlite3_api
277d5 2d 3e 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d  ->table_column_m
277d6 65 74 61 64 61 74 61 0a 23 64 65 66 69 6e 65 20  etadata.#define 
277d7 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 5f 63  sqlite3_thread_c
277d8 6c 65 61 6e 75 70 20 20 20 20 20 20 20 20 20 73  leanup         s
277d9 71 6c 69 74 65 33 5f 61 70 69 2d 3e 74 68 72 65  qlite3_api->thre
277da 61 64 5f 63 6c 65 61 6e 75 70 0a 23 64 65 66 69  ad_cleanup.#defi
277db 6e 65 20 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c  ne sqlite3_total
277dc 5f 63 68 61 6e 67 65 73 20 20 20 20 20 20 20 20  _changes        
277dd 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 74    sqlite3_api->t
277de 6f 74 61 6c 5f 63 68 61 6e 67 65 73 0a 23 64 65  otal_changes.#de
277df 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 72 61  fine sqlite3_tra
277e0 63 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ce              
277e1 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
277e2 3e 74 72 61 63 65 0a 23 69 66 6e 64 65 66 20 53  >trace.#ifndef S
277e3 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45  QLITE_OMIT_DEPRE
277e4 43 41 54 45 44 0a 23 64 65 66 69 6e 65 20 73 71  CATED.#define sq
277e5 6c 69 74 65 33 5f 74 72 61 6e 73 66 65 72 5f 62  lite3_transfer_b
277e6 69 6e 64 69 6e 67 73 20 20 20 20 20 20 73 71 6c  indings      sql
277e7 69 74 65 33 5f 61 70 69 2d 3e 74 72 61 6e 73 66  ite3_api->transf
277e8 65 72 5f 62 69 6e 64 69 6e 67 73 0a 23 65 6e 64  er_bindings.#end
277e9 69 66 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  if.#define sqlit
277ea 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 20 20  e3_update_hook  
277eb 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
277ec 33 5f 61 70 69 2d 3e 75 70 64 61 74 65 5f 68 6f  3_api->update_ho
277ed 6f 6b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ok.#define sqlit
277ee 65 33 5f 75 73 65 72 5f 64 61 74 61 20 20 20 20  e3_user_data    
277ef 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
277f0 33 5f 61 70 69 2d 3e 75 73 65 72 5f 64 61 74 61  3_api->user_data
277f1 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
277f2 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 20 20 20 20  _value_blob     
277f3 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
277f4 61 70 69 2d 3e 76 61 6c 75 65 5f 62 6c 6f 62 0a  api->value_blob.
277f5 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
277f6 76 61 6c 75 65 5f 62 79 74 65 73 20 20 20 20 20  value_bytes     
277f7 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
277f8 70 69 2d 3e 76 61 6c 75 65 5f 62 79 74 65 73 0a  pi->value_bytes.
277f9 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
277fa 76 61 6c 75 65 5f 62 79 74 65 73 31 36 20 20 20  value_bytes16   
277fb 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
277fc 70 69 2d 3e 76 61 6c 75 65 5f 62 79 74 65 73 31  pi->value_bytes1
277fd 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  6.#define sqlite
277fe 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20 20  3_value_double  
277ff 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
27800 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 64 6f 75 62  _api->value_doub
27801 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  le.#define sqlit
27802 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 20 20 20  e3_value_int    
27803 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
27804 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 69 6e 74  3_api->value_int
27805 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
27806 5f 76 61 6c 75 65 5f 69 6e 74 36 34 20 20 20 20  _value_int64    
27807 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
27808 61 70 69 2d 3e 76 61 6c 75 65 5f 69 6e 74 36 34  api->value_int64
27809 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
2780a 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74  _value_numeric_t
2780b 79 70 65 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ype     sqlite3_
2780c 61 70 69 2d 3e 76 61 6c 75 65 5f 6e 75 6d 65 72  api->value_numer
2780d 69 63 5f 74 79 70 65 0a 23 64 65 66 69 6e 65 20  ic_type.#define 
2780e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
2780f 78 74 20 20 20 20 20 20 20 20 20 20 20 20 20 73  xt             s
27810 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75  qlite3_api->valu
27811 65 5f 74 65 78 74 0a 23 64 65 66 69 6e 65 20 73  e_text.#define s
27812 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
27813 74 31 36 20 20 20 20 20 20 20 20 20 20 20 73 71  t16           sq
27814 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65  lite3_api->value
27815 5f 74 65 78 74 31 36 0a 23 64 65 66 69 6e 65 20  _text16.#define 
27816 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
27817 78 74 31 36 62 65 20 20 20 20 20 20 20 20 20 73  xt16be         s
27818 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75  qlite3_api->valu
27819 65 5f 74 65 78 74 31 36 62 65 0a 23 64 65 66 69  e_text16be.#defi
2781a 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ne sqlite3_value
2781b 5f 74 65 78 74 31 36 6c 65 20 20 20 20 20 20 20  _text16le       
2781c 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76    sqlite3_api->v
2781d 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 0a 23 64  alue_text16le.#d
2781e 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61  efine sqlite3_va
2781f 6c 75 65 5f 74 79 70 65 20 20 20 20 20 20 20 20  lue_type        
27820 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
27821 2d 3e 76 61 6c 75 65 5f 74 79 70 65 0a 23 64 65  ->value_type.#de
27822 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 6d 70  fine sqlite3_vmp
27823 72 69 6e 74 66 20 20 20 20 20 20 20 20 20 20 20  rintf           
27824 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
27825 3e 76 6d 70 72 69 6e 74 66 0a 23 64 65 66 69 6e  >vmprintf.#defin
27826 65 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f  e sqlite3_overlo
27827 61 64 5f 66 75 6e 63 74 69 6f 6e 20 20 20 20 20  ad_function     
27828 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6f 76   sqlite3_api->ov
27829 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 0a  erload_function.
2782a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
2782b 70 72 65 70 61 72 65 5f 76 32 20 20 20 20 20 20  prepare_v2      
2782c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
2782d 70 69 2d 3e 70 72 65 70 61 72 65 5f 76 32 0a 23  pi->prepare_v2.#
2782e 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70  define sqlite3_p
2782f 72 65 70 61 72 65 31 36 5f 76 32 20 20 20 20 20  repare16_v2     
27830 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
27831 69 2d 3e 70 72 65 70 61 72 65 31 36 5f 76 32 0a  i->prepare16_v2.
27832 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
27833 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 20 20  clear_bindings  
27834 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
27835 70 69 2d 3e 63 6c 65 61 72 5f 62 69 6e 64 69 6e  pi->clear_bindin
27836 67 73 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  gs.#define sqlit
27837 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62  e3_bind_zeroblob
27838 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
27839 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 7a 65 72 6f  3_api->bind_zero
2783a 62 6c 6f 62 0a 23 64 65 66 69 6e 65 20 73 71 6c  blob.#define sql
2783b 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 20  ite3_blob_bytes 
2783c 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
2783d 74 65 33 5f 61 70 69 2d 3e 62 6c 6f 62 5f 62 79  te3_api->blob_by
2783e 74 65 73 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  tes.#define sqli
2783f 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 20 20  te3_blob_close  
27840 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
27841 65 33 5f 61 70 69 2d 3e 62 6c 6f 62 5f 63 6c 6f  e3_api->blob_clo
27842 73 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  se.#define sqlit
27843 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 20 20 20 20  e3_blob_open    
27844 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
27845 33 5f 61 70 69 2d 3e 62 6c 6f 62 5f 6f 70 65 6e  3_api->blob_open
27846 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
27847 5f 62 6c 6f 62 5f 72 65 61 64 20 20 20 20 20 20  _blob_read      
27848 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
27849 61 70 69 2d 3e 62 6c 6f 62 5f 72 65 61 64 0a 23  api->blob_read.#
2784a 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62  define sqlite3_b
2784b 6c 6f 62 5f 77 72 69 74 65 20 20 20 20 20 20 20  lob_write       
2784c 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
2784d 69 2d 3e 62 6c 6f 62 5f 77 72 69 74 65 0a 23 64  i->blob_write.#d
2784e 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 72  efine sqlite3_cr
2784f 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76  eate_collation_v
27850 32 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69  2    sqlite3_api
27851 2d 3e 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69  ->create_collati
27852 6f 6e 5f 76 32 0a 23 64 65 66 69 6e 65 20 73 71  on_v2.#define sq
27853 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72  lite3_file_contr
27854 6f 6c 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  ol           sql
27855 69 74 65 33 5f 61 70 69 2d 3e 66 69 6c 65 5f 63  ite3_api->file_c
27856 6f 6e 74 72 6f 6c 0a 23 64 65 66 69 6e 65 20 73  ontrol.#define s
27857 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69  qlite3_memory_hi
27858 67 68 77 61 74 65 72 20 20 20 20 20 20 20 73 71  ghwater       sq
27859 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 65 6d 6f 72  lite3_api->memor
2785a 79 5f 68 69 67 68 77 61 74 65 72 0a 23 64 65 66  y_highwater.#def
2785b 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f  ine sqlite3_memo
2785c 72 79 5f 75 73 65 64 20 20 20 20 20 20 20 20 20  ry_used         
2785d 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
2785e 6d 65 6d 6f 72 79 5f 75 73 65 64 0a 23 64 65 66  memory_used.#def
2785f 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ine sqlite3_mute
27860 78 5f 61 6c 6c 6f 63 20 20 20 20 20 20 20 20 20  x_alloc         
27861 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
27862 6d 75 74 65 78 5f 61 6c 6c 6f 63 0a 23 64 65 66  mutex_alloc.#def
27863 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ine sqlite3_mute
27864 78 5f 65 6e 74 65 72 20 20 20 20 20 20 20 20 20  x_enter         
27865 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
27866 6d 75 74 65 78 5f 65 6e 74 65 72 0a 23 64 65 66  mutex_enter.#def
27867 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ine sqlite3_mute
27868 78 5f 66 72 65 65 20 20 20 20 20 20 20 20 20 20  x_free          
27869 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
2786a 6d 75 74 65 78 5f 66 72 65 65 0a 23 64 65 66 69  mutex_free.#defi
2786b 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ne sqlite3_mutex
2786c 5f 6c 65 61 76 65 20 20 20 20 20 20 20 20 20 20  _leave          
2786d 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d    sqlite3_api->m
2786e 75 74 65 78 5f 6c 65 61 76 65 0a 23 64 65 66 69  utex_leave.#defi
2786f 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ne sqlite3_mutex
27870 5f 74 72 79 20 20 20 20 20 20 20 20 20 20 20 20  _try            
27871 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d    sqlite3_api->m
27872 75 74 65 78 5f 74 72 79 0a 23 64 65 66 69 6e 65  utex_try.#define
27873 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32   sqlite3_open_v2
27874 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27875 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6f 70 65  sqlite3_api->ope
27876 6e 5f 76 32 0a 23 64 65 66 69 6e 65 20 73 71 6c  n_v2.#define sql
27877 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d  ite3_release_mem
27878 6f 72 79 20 20 20 20 20 20 20 20 20 73 71 6c 69  ory         sqli
27879 74 65 33 5f 61 70 69 2d 3e 72 65 6c 65 61 73 65  te3_api->release
2787a 5f 6d 65 6d 6f 72 79 0a 23 64 65 66 69 6e 65 20  _memory.#define 
2787b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
2787c 72 72 6f 72 5f 6e 6f 6d 65 6d 20 20 20 20 20 73  rror_nomem     s
2787d 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75  qlite3_api->resu
2787e 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 0a 23  lt_error_nomem.#
2787f 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72  define sqlite3_r
27880 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62  esult_error_toob
27881 69 67 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70  ig    sqlite3_ap
27882 69 2d 3e 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  i->result_error_
27883 74 6f 6f 62 69 67 0a 23 64 65 66 69 6e 65 20 73  toobig.#define s
27884 71 6c 69 74 65 33 5f 73 6c 65 65 70 20 20 20 20  qlite3_sleep    
27885 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
27886 6c 69 74 65 33 5f 61 70 69 2d 3e 73 6c 65 65 70  lite3_api->sleep
27887 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
27888 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74  _soft_heap_limit
27889 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
2788a 61 70 69 2d 3e 73 6f 66 74 5f 68 65 61 70 5f 6c  api->soft_heap_l
2788b 69 6d 69 74 0a 23 64 65 66 69 6e 65 20 73 71 6c  imit.#define sql
2788c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 20 20 20  ite3_vfs_find   
2788d 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
2788e 74 65 33 5f 61 70 69 2d 3e 76 66 73 5f 66 69 6e  te3_api->vfs_fin
2788f 64 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  d.#define sqlite
27890 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 20 20  3_vfs_register  
27891 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
27892 5f 61 70 69 2d 3e 76 66 73 5f 72 65 67 69 73 74  _api->vfs_regist
27893 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  er.#define sqlit
27894 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65  e3_vfs_unregiste
27895 72 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  r         sqlite
27896 33 5f 61 70 69 2d 3e 76 66 73 5f 75 6e 72 65 67  3_api->vfs_unreg
27897 69 73 74 65 72 0a 23 64 65 66 69 6e 65 20 73 71  ister.#define sq
27898 6c 69 74 65 33 5f 74 68 72 65 61 64 73 61 66 65  lite3_threadsafe
27899 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
2789a 69 74 65 33 5f 61 70 69 2d 3e 78 74 68 72 65 61  ite3_api->xthrea
2789b 64 73 61 66 65 0a 23 64 65 66 69 6e 65 20 73 71  dsafe.#define sq
2789c 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72  lite3_result_zer
2789d 6f 62 6c 6f 62 20 20 20 20 20 20 20 20 73 71 6c  oblob        sql
2789e 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74  ite3_api->result
2789f 5f 7a 65 72 6f 62 6c 6f 62 0a 23 64 65 66 69 6e  _zeroblob.#defin
278a0 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
278a1 5f 65 72 72 6f 72 5f 63 6f 64 65 20 20 20 20 20  _error_code     
278a2 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65   sqlite3_api->re
278a3 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 0a  sult_error_code.
278a4 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
278a5 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 20 20 20 20  test_control    
278a6 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
278a7 70 69 2d 3e 74 65 73 74 5f 63 6f 6e 74 72 6f 6c  pi->test_control
278a8 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
278a9 5f 72 61 6e 64 6f 6d 6e 65 73 73 20 20 20 20 20  _randomness     
278aa 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
278ab 61 70 69 2d 3e 72 61 6e 64 6f 6d 6e 65 73 73 0a  api->randomness.
278ac 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
278ad 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
278ae 65 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61  e      sqlite3_a
278af 70 69 2d 3e 63 6f 6e 74 65 78 74 5f 64 62 5f 68  pi->context_db_h
278b0 61 6e 64 6c 65 0a 23 64 65 66 69 6e 65 20 73 71  andle.#define sq
278b1 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 72  lite3_extended_r
278b2 65 73 75 6c 74 5f 63 6f 64 65 73 20 20 73 71 6c  esult_codes  sql
278b3 69 74 65 33 5f 61 70 69 2d 3e 65 78 74 65 6e 64  ite3_api->extend
278b4 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 0a  ed_result_codes.
278b5 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
278b6 6c 69 6d 69 74 20 20 20 20 20 20 20 20 20 20 20  limit           
278b7 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
278b8 70 69 2d 3e 6c 69 6d 69 74 0a 23 64 65 66 69 6e  pi->limit.#defin
278b9 65 20 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73  e sqlite3_next_s
278ba 74 6d 74 20 20 20 20 20 20 20 20 20 20 20 20 20  tmt             
278bb 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6e 65   sqlite3_api->ne
278bc 78 74 5f 73 74 6d 74 0a 23 64 65 66 69 6e 65 20  xt_stmt.#define 
278bd 73 71 6c 69 74 65 33 5f 73 71 6c 20 20 20 20 20  sqlite3_sql     
278be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
278bf 71 6c 69 74 65 33 5f 61 70 69 2d 3e 73 71 6c 0a  qlite3_api->sql.
278c0 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
278c1 73 74 61 74 75 73 20 20 20 20 20 20 20 20 20 20  status          
278c2 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
278c3 70 69 2d 3e 73 74 61 74 75 73 0a 23 65 6e 64 69  pi->status.#endi
278c4 66 20 2f 2a 20 53 51 4c 49 54 45 5f 43 4f 52 45  f /* SQLITE_CORE
278c5 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 53 51 4c   */..#define SQL
278c6 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e  ITE_EXTENSION_IN
278c7 49 54 31 20 20 20 20 20 63 6f 6e 73 74 20 73 71  IT1     const sq
278c8 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e  lite3_api_routin
278c9 65 73 20 2a 73 71 6c 69 74 65 33 5f 61 70 69 20  es *sqlite3_api 
278ca 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c  = 0;.#define SQL
278cb 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e  ITE_EXTENSION_IN
278cc 49 54 32 28 76 29 20 20 73 71 6c 69 74 65 33 5f  IT2(v)  sqlite3_
278cd 61 70 69 20 3d 20 76 3b 0a 0a 23 65 6e 64 69 66  api = v;..#endif
278ce 20 2f 2a 20 5f 53 51 4c 49 54 45 33 45 58 54 5f   /* _SQLITE3EXT_
278cf 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  H_ */../********
278d0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71  ****** End of sq
278d1 6c 69 74 65 33 65 78 74 2e 68 20 2a 2a 2a 2a 2a  lite3ext.h *****
278d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
278d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
278d4 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
278d5 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e  ****** Continuin
278d6 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20  g where we left 
278d7 6f 66 66 20 69 6e 20 6c 6f 61 64 65 78 74 2e 63  off in loadext.c
278d8 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
278d9 2a 2a 2a 2a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  *****/..#ifndef 
278da 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44  SQLITE_OMIT_LOAD
278db 5f 45 58 54 45 4e 53 49 4f 4e 0a 0a 2f 2a 0a 2a  _EXTENSION../*.*
278dc 2a 20 53 6f 6d 65 20 41 50 49 20 72 6f 75 74 69  * Some API routi
278dd 6e 65 73 20 61 72 65 20 6f 6d 69 74 74 65 64 20  nes are omitted 
278de 77 68 65 6e 20 76 61 72 69 6f 75 73 20 66 65 61  when various fea
278df 74 75 72 65 73 20 61 72 65 0a 2a 2a 20 65 78 63  tures are.** exc
278e0 6c 75 64 65 64 20 66 72 6f 6d 20 61 20 62 75 69  luded from a bui
278e1 6c 64 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 53  ld of SQLite.  S
278e2 75 62 73 74 69 74 75 74 65 20 61 20 4e 55 4c 4c  ubstitute a NULL
278e3 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 66 6f 72 20   pointer.** for 
278e4 61 6e 79 20 6d 69 73 73 69 6e 67 20 41 50 49 73  any missing APIs
278e5 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
278e6 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d  ITE_ENABLE_COLUM
278e7 4e 5f 4d 45 54 41 44 41 54 41 0a 23 20 64 65 66  N_METADATA.# def
278e8 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
278e9 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65  mn_database_name
278ea 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71     0.# define sq
278eb 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74  lite3_column_dat
278ec 61 62 61 73 65 5f 6e 61 6d 65 31 36 20 30 0a 23  abase_name16 0.#
278ed 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
278ee 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d  column_table_nam
278ef 65 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e  e      0.# defin
278f0 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
278f1 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 20 20 20  _table_name16   
278f2 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69   0.# define sqli
278f3 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69  te3_column_origi
278f4 6e 5f 6e 61 6d 65 20 20 20 20 20 30 0a 23 20 64  n_name     0.# d
278f5 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
278f6 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65  lumn_origin_name
278f7 31 36 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20  16   0.# define 
278f8 73 71 6c 69 74 65 33 5f 74 61 62 6c 65 5f 63 6f  sqlite3_table_co
278f9 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61 20 20 30  lumn_metadata  0
278fa 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
278fb 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48  SQLITE_OMIT_AUTH
278fc 4f 52 49 5a 41 54 49 4f 4e 0a 23 20 64 65 66 69  ORIZATION.# defi
278fd 6e 65 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61  ne sqlite3_set_a
278fe 75 74 68 6f 72 69 7a 65 72 20 20 20 20 20 20 20  uthorizer       
278ff 20 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64    0.#endif..#ifd
27900 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  ef SQLITE_OMIT_U
27901 54 46 31 36 0a 23 20 64 65 66 69 6e 65 20 73 71  TF16.# define sq
27902 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31  lite3_bind_text1
27903 36 20 20 20 20 20 20 20 20 20 20 20 20 30 0a 23  6            0.#
27904 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
27905 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
27906 31 36 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e  16     0.# defin
27907 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
27908 5f 64 65 63 6c 74 79 70 65 31 36 20 20 20 20 20  _decltype16     
27909 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69   0.# define sqli
2790a 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31  te3_column_name1
2790b 36 20 20 20 20 20 20 20 20 20 20 30 0a 23 20 64  6          0.# d
2790c 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
2790d 6c 75 6d 6e 5f 74 65 78 74 31 36 20 20 20 20 20  lumn_text16     
2790e 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20       0.# define 
2790f 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65  sqlite3_complete
27910 31 36 20 20 20 20 20 20 20 20 20 20 20 20 20 30  16             0
27911 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
27912 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69  3_create_collati
27913 6f 6e 31 36 20 20 20 20 20 30 0a 23 20 64 65 66  on16     0.# def
27914 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61  ine sqlite3_crea
27915 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 20 20 20  te_function16   
27916 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71     0.# define sq
27917 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 20 20  lite3_errmsg16  
27918 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0a 23               0.#
27919 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
2791a 6f 70 65 6e 31 36 20 20 20 20 20 20 20 20 20 20  open16          
2791b 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e         0.# defin
2791c 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  e sqlite3_prepar
2791d 65 31 36 20 20 20 20 20 20 20 20 20 20 20 20 20  e16             
2791e 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69   0.# define sqli
2791f 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32  te3_prepare16_v2
27920 20 20 20 20 20 20 20 20 20 20 20 30 0a 23 20 64             0.# d
27921 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65  efine sqlite3_re
27922 73 75 6c 74 5f 65 72 72 6f 72 31 36 20 20 20 20  sult_error16    
27923 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20       0.# define 
27924 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
27925 65 78 74 31 36 20 20 20 20 20 20 20 20 20 20 30  ext16          0
27926 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
27927 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62  3_result_text16b
27928 65 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66  e        0.# def
27929 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ine sqlite3_resu
2792a 6c 74 5f 74 65 78 74 31 36 6c 65 20 20 20 20 20  lt_text16le     
2792b 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71     0.# define sq
2792c 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
2792d 31 36 20 20 20 20 20 20 20 20 20 20 20 30 0a 23  16           0.#
2792e 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
2792f 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65 20 20  value_text16be  
27930 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e         0.# defin
27931 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  e sqlite3_value_
27932 74 65 78 74 31 36 6c 65 20 20 20 20 20 20 20 20  text16le        
27933 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69   0.# define sqli
27934 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62  te3_column_datab
27935 61 73 65 5f 6e 61 6d 65 31 36 20 30 0a 23 20 64  ase_name16 0.# d
27936 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
27937 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31  lumn_table_name1
27938 36 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20  6    0.# define 
27939 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f  sqlite3_column_o
2793a 72 69 67 69 6e 5f 6e 61 6d 65 31 36 20 20 20 30  rigin_name16   0
2793b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
2793c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50  SQLITE_OMIT_COMP
2793d 4c 45 54 45 0a 23 20 64 65 66 69 6e 65 20 73 71  LETE.# define sq
2793e 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 20 30  lite3_complete 0
2793f 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
27940 33 5f 63 6f 6d 70 6c 65 74 65 31 36 20 30 0a 23  3_complete16 0.#
27941 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
27942 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45  LITE_OMIT_PROGRE
27943 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 23 20 64 65  SS_CALLBACK.# de
27944 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 6f  fine sqlite3_pro
27945 67 72 65 73 73 5f 68 61 6e 64 6c 65 72 20 30 0a  gress_handler 0.
27946 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
27947 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
27948 41 4c 54 41 42 4c 45 0a 23 20 64 65 66 69 6e 65  ALTABLE.# define
27949 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
2794a 6d 6f 64 75 6c 65 20 30 0a 23 20 64 65 66 69 6e  module 0.# defin
2794b 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  e sqlite3_create
2794c 5f 6d 6f 64 75 6c 65 5f 76 32 20 30 0a 23 20 64  _module_v2 0.# d
2794d 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 64 65  efine sqlite3_de
2794e 63 6c 61 72 65 5f 76 74 61 62 20 30 0a 23 65 6e  clare_vtab 0.#en
2794f 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
27950 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
27951 41 43 48 45 0a 23 20 64 65 66 69 6e 65 20 73 71  ACHE.# define sq
27952 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61  lite3_enable_sha
27953 72 65 64 5f 63 61 63 68 65 20 30 0a 23 65 6e 64  red_cache 0.#end
27954 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
27955 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 23 20 64  E_OMIT_TRACE.# d
27956 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72  efine sqlite3_pr
27957 6f 66 69 6c 65 20 20 20 20 20 20 20 30 0a 23 20  ofile       0.# 
27958 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74  define sqlite3_t
27959 72 61 63 65 20 20 20 20 20 20 20 20 20 30 0a 23  race         0.#
2795a 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
2795b 4c 49 54 45 5f 4f 4d 49 54 5f 47 45 54 5f 54 41  LITE_OMIT_GET_TA
2795c 42 4c 45 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  BLE.# define sql
2795d 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 20  ite3_free_table 
2795e 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71     0.# define sq
2795f 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 20  lite3_get_table 
27960 20 20 20 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69      0.#endif..#i
27961 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
27962 5f 49 4e 43 52 42 4c 4f 42 0a 23 64 65 66 69 6e  _INCRBLOB.#defin
27963 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a  e sqlite3_bind_z
27964 65 72 6f 62 6c 6f 62 20 20 30 0a 23 64 65 66 69  eroblob  0.#defi
27965 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  ne sqlite3_blob_
27966 62 79 74 65 73 20 20 20 20 20 30 0a 23 64 65 66  bytes     0.#def
27967 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ine sqlite3_blob
27968 5f 63 6c 6f 73 65 20 20 20 20 20 30 0a 23 64 65  _close     0.#de
27969 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f  fine sqlite3_blo
2796a 62 5f 6f 70 65 6e 20 20 20 20 20 20 30 0a 23 64  b_open      0.#d
2796b 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c  efine sqlite3_bl
2796c 6f 62 5f 72 65 61 64 20 20 20 20 20 20 30 0a 23  ob_read      0.#
2796d 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62  define sqlite3_b
2796e 6c 6f 62 5f 77 72 69 74 65 20 20 20 20 20 30 0a  lob_write     0.
2796f 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
27970 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
27971 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 70  cture contains p
27972 6f 69 6e 74 65 72 73 20 74 6f 20 61 6c 6c 20 53  ointers to all S
27973 51 4c 69 74 65 20 41 50 49 20 72 6f 75 74 69 6e  QLite API routin
27974 65 73 2e 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72  es..** A pointer
27975 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75   to this structu
27976 72 65 20 69 73 20 70 61 73 73 65 64 20 69 6e 74  re is passed int
27977 6f 20 65 78 74 65 6e 73 69 6f 6e 73 20 77 68 65  o extensions whe
27978 6e 20 74 68 65 79 20 61 72 65 0a 2a 2a 20 6c 6f  n they are.** lo
27979 61 64 65 64 20 73 6f 20 74 68 61 74 20 74 68 65  aded so that the
2797a 20 65 78 74 65 6e 73 69 6f 6e 20 63 61 6e 20 6d   extension can m
2797b 61 6b 65 20 63 61 6c 6c 73 20 62 61 63 6b 20 69  ake calls back i
2797c 6e 74 6f 20 74 68 65 20 53 51 4c 69 74 65 0a 2a  nto the SQLite.*
2797d 2a 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a  * library..**.**
2797e 20 57 68 65 6e 20 61 64 64 69 6e 67 20 6e 65 77   When adding new
2797f 20 41 50 49 73 2c 20 61 64 64 20 74 68 65 6d 20   APIs, add them 
27980 74 6f 20 74 68 65 20 62 6f 74 74 6f 6d 20 6f 66  to the bottom of
27981 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 0a   this structure.
27982 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 70  ** in order to p
27983 72 65 73 65 72 76 65 20 62 61 63 6b 77 61 72 64  reserve backward
27984 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e  s compatibility.
27985 0a 2a 2a 0a 2a 2a 20 45 78 74 65 6e 73 69 6f 6e  .**.** Extension
27986 73 20 74 68 61 74 20 75 73 65 20 6e 65 77 65 72  s that use newer
27987 20 41 50 49 73 20 73 68 6f 75 6c 64 20 66 69 72   APIs should fir
27988 73 74 20 63 61 6c 6c 20 74 68 65 0a 2a 2a 20 73  st call the.** s
27989 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f  qlite3_libversio
2798a 6e 5f 6e 75 6d 62 65 72 28 29 20 74 6f 20 6d 61  n_number() to ma
2798b 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65  ke sure that the
2798c 20 41 50 49 20 74 68 65 79 0a 2a 2a 20 69 6e 74   API they.** int
2798d 65 6e 64 20 74 6f 20 75 73 65 20 69 73 20 73 75  end to use is su
2798e 70 70 6f 72 74 65 64 20 62 79 20 74 68 65 20 6c  pported by the l
2798f 69 62 72 61 72 79 2e 20 20 45 78 74 65 6e 73 69  ibrary.  Extensi
27990 6f 6e 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 61 6c  ons should.** al
27991 73 6f 20 63 68 65 63 6b 20 74 6f 20 6d 61 6b 65  so check to make
27992 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 70   sure that the p
27993 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66 75  ointer to the fu
27994 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 6e 6f 74  nction is.** not
27995 20 4e 55 4c 4c 20 62 65 66 6f 72 65 20 63 61 6c   NULL before cal
27996 6c 69 6e 67 20 69 74 2e 0a 2a 2f 0a 73 74 61 74  ling it..*/.stat
27997 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
27998 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20 73 71  _api_routines sq
27999 6c 69 74 65 33 41 70 69 73 20 3d 20 7b 0a 20 20  lite3Apis = {.  
2799a 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
2799b 65 5f 63 6f 6e 74 65 78 74 2c 0a 23 69 66 6e 64  e_context,.#ifnd
2799c 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
2799d 45 50 52 45 43 41 54 45 44 0a 20 20 73 71 6c 69  EPRECATED.  sqli
2799e 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
2799f 75 6e 74 2c 0a 23 65 6c 73 65 0a 20 20 30 2c 0a  unt,.#else.  0,.
279a0 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33  #endif.  sqlite3
279a1 5f 62 69 6e 64 5f 62 6c 6f 62 2c 0a 20 20 73 71  _bind_blob,.  sq
279a2 6c 69 74 65 33 5f 62 69 6e 64 5f 64 6f 75 62 6c  lite3_bind_doubl
279a3 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  e,.  sqlite3_bin
279a4 64 5f 69 6e 74 2c 0a 20 20 73 71 6c 69 74 65 33  d_int,.  sqlite3
279a5 5f 62 69 6e 64 5f 69 6e 74 36 34 2c 0a 20 20 73  _bind_int64,.  s
279a6 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c  qlite3_bind_null
279a7 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ,.  sqlite3_bind
279a8 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74  _parameter_count
279a9 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ,.  sqlite3_bind
279aa 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78  _parameter_index
279ab 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ,.  sqlite3_bind
279ac 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 2c  _parameter_name,
279ad 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
279ae 74 65 78 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f  text,.  sqlite3_
279af 62 69 6e 64 5f 74 65 78 74 31 36 2c 0a 20 20 73  bind_text16,.  s
279b0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75  qlite3_bind_valu
279b1 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 75 73  e,.  sqlite3_bus
279b2 79 5f 68 61 6e 64 6c 65 72 2c 0a 20 20 73 71 6c  y_handler,.  sql
279b3 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75  ite3_busy_timeou
279b4 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 68 61  t,.  sqlite3_cha
279b5 6e 67 65 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f  nges,.  sqlite3_
279b6 63 6c 6f 73 65 2c 0a 20 20 73 71 6c 69 74 65 33  close,.  sqlite3
279b7 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65  _collation_neede
279b8 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c  d,.  sqlite3_col
279b9 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 2c  lation_needed16,
279ba 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  .  sqlite3_colum
279bb 6e 5f 62 6c 6f 62 2c 0a 20 20 73 71 6c 69 74 65  n_blob,.  sqlite
279bc 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 2c 0a  3_column_bytes,.
279bd 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
279be 5f 62 79 74 65 73 31 36 2c 0a 20 20 73 71 6c 69  _bytes16,.  sqli
279bf 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74  te3_column_count
279c0 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ,.  sqlite3_colu
279c1 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65  mn_database_name
279c2 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ,.  sqlite3_colu
279c3 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65  mn_database_name
279c4 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  16,.  sqlite3_co
279c5 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 2c 0a 20  lumn_decltype,. 
279c6 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
279c7 64 65 63 6c 74 79 70 65 31 36 2c 0a 20 20 73 71  decltype16,.  sq
279c8 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75  lite3_column_dou
279c9 62 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  ble,.  sqlite3_c
279ca 6f 6c 75 6d 6e 5f 69 6e 74 2c 0a 20 20 73 71 6c  olumn_int,.  sql
279cb 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36  ite3_column_int6
279cc 34 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c  4,.  sqlite3_col
279cd 75 6d 6e 5f 6e 61 6d 65 2c 0a 20 20 73 71 6c 69  umn_name,.  sqli
279ce 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31  te3_column_name1
279cf 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c  6,.  sqlite3_col
279d0 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 2c  umn_origin_name,
279d1 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  .  sqlite3_colum
279d2 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36 2c  n_origin_name16,
279d3 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  .  sqlite3_colum
279d4 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 2c 0a 20 20  n_table_name,.  
279d5 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
279d6 61 62 6c 65 5f 6e 61 6d 65 31 36 2c 0a 20 20 73  able_name16,.  s
279d7 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
279d8 78 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  xt,.  sqlite3_co
279d9 6c 75 6d 6e 5f 74 65 78 74 31 36 2c 0a 20 20 73  lumn_text16,.  s
279da 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79  qlite3_column_ty
279db 70 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  pe,.  sqlite3_co
279dc 6c 75 6d 6e 5f 76 61 6c 75 65 2c 0a 20 20 73 71  lumn_value,.  sq
279dd 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f  lite3_commit_hoo
279de 6b 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6d  k,.  sqlite3_com
279df 70 6c 65 74 65 2c 0a 20 20 73 71 6c 69 74 65 33  plete,.  sqlite3
279e0 5f 63 6f 6d 70 6c 65 74 65 31 36 2c 0a 20 20 73  _complete16,.  s
279e1 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
279e2 6c 6c 61 74 69 6f 6e 2c 0a 20 20 73 71 6c 69 74  llation,.  sqlit
279e3 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
279e4 69 6f 6e 31 36 2c 0a 20 20 73 71 6c 69 74 65 33  ion16,.  sqlite3
279e5 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
279e6 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61  ,.  sqlite3_crea
279e7 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 2c 0a 20  te_function16,. 
279e8 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
279e9 6d 6f 64 75 6c 65 2c 0a 20 20 73 71 6c 69 74 65  module,.  sqlite
279ea 33 5f 64 61 74 61 5f 63 6f 75 6e 74 2c 0a 20 20  3_data_count,.  
279eb 73 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c  sqlite3_db_handl
279ec 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 64 65 63  e,.  sqlite3_dec
279ed 6c 61 72 65 5f 76 74 61 62 2c 0a 20 20 73 71 6c  lare_vtab,.  sql
279ee 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72  ite3_enable_shar
279ef 65 64 5f 63 61 63 68 65 2c 0a 20 20 73 71 6c 69  ed_cache,.  sqli
279f0 74 65 33 5f 65 72 72 63 6f 64 65 2c 0a 20 20 73  te3_errcode,.  s
279f1 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 2c 0a 20  qlite3_errmsg,. 
279f2 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31   sqlite3_errmsg1
279f3 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 65 78 65  6,.  sqlite3_exe
279f4 63 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  c,.#ifndef SQLIT
279f5 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45  E_OMIT_DEPRECATE
279f6 44 0a 20 20 73 71 6c 69 74 65 33 5f 65 78 70 69  D.  sqlite3_expi
279f7 72 65 64 2c 0a 23 65 6c 73 65 0a 20 20 30 2c 0a  red,.#else.  0,.
279f8 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33  #endif.  sqlite3
279f9 5f 66 69 6e 61 6c 69 7a 65 2c 0a 20 20 73 71 6c  _finalize,.  sql
279fa 69 74 65 33 5f 66 72 65 65 2c 0a 20 20 73 71 6c  ite3_free,.  sql
279fb 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 2c  ite3_free_table,
279fc 0a 20 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61  .  sqlite3_get_a
279fd 75 74 6f 63 6f 6d 6d 69 74 2c 0a 20 20 73 71 6c  utocommit,.  sql
279fe 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61  ite3_get_auxdata
279ff 2c 0a 20 20 73 71 6c 69 74 65 33 5f 67 65 74 5f  ,.  sqlite3_get_
27a00 74 61 62 6c 65 2c 0a 20 20 30 2c 20 20 20 20 20  table,.  0,     
27a01 2f 2a 20 57 61 73 20 73 71 6c 69 74 65 33 5f 67  /* Was sqlite3_g
27a02 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72 28 29 2c  lobal_recover(),
27a03 20 62 75 74 20 74 68 61 74 20 66 75 6e 63 74 69   but that functi
27a04 6f 6e 20 69 73 20 64 65 70 72 65 63 61 74 65 64  on is deprecated
27a05 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e   */.  sqlite3_in
27a06 74 65 72 72 75 70 74 2c 0a 20 20 73 71 6c 69 74  terrupt,.  sqlit
27a07 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72  e3_last_insert_r
27a08 6f 77 69 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f  owid,.  sqlite3_
27a09 6c 69 62 76 65 72 73 69 6f 6e 2c 0a 20 20 73 71  libversion,.  sq
27a0a 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e  lite3_libversion
27a0b 5f 6e 75 6d 62 65 72 2c 0a 20 20 73 71 6c 69 74  _number,.  sqlit
27a0c 65 33 5f 6d 61 6c 6c 6f 63 2c 0a 20 20 73 71 6c  e3_malloc,.  sql
27a0d 69 74 65 33 5f 6d 70 72 69 6e 74 66 2c 0a 20 20  ite3_mprintf,.  
27a0e 73 71 6c 69 74 65 33 5f 6f 70 65 6e 2c 0a 20 20  sqlite3_open,.  
27a0f 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 2c 0a  sqlite3_open16,.
27a10 20 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72    sqlite3_prepar
27a11 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 70 72 65  e,.  sqlite3_pre
27a12 70 61 72 65 31 36 2c 0a 20 20 73 71 6c 69 74 65  pare16,.  sqlite
27a13 33 5f 70 72 6f 66 69 6c 65 2c 0a 20 20 73 71 6c  3_profile,.  sql
27a14 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68 61  ite3_progress_ha
27a15 6e 64 6c 65 72 2c 0a 20 20 73 71 6c 69 74 65 33  ndler,.  sqlite3
27a16 5f 72 65 61 6c 6c 6f 63 2c 0a 20 20 73 71 6c 69  _realloc,.  sqli
27a17 74 65 33 5f 72 65 73 65 74 2c 0a 20 20 73 71 6c  te3_reset,.  sql
27a18 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62  ite3_result_blob
27a19 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ,.  sqlite3_resu
27a1a 6c 74 5f 64 6f 75 62 6c 65 2c 0a 20 20 73 71 6c  lt_double,.  sql
27a1b 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
27a1c 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  r,.  sqlite3_res
27a1d 75 6c 74 5f 65 72 72 6f 72 31 36 2c 0a 20 20 73  ult_error16,.  s
27a1e 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
27a1f 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  t,.  sqlite3_res
27a20 75 6c 74 5f 69 6e 74 36 34 2c 0a 20 20 73 71 6c  ult_int64,.  sql
27a21 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
27a22 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ,.  sqlite3_resu
27a23 6c 74 5f 74 65 78 74 2c 0a 20 20 73 71 6c 69 74  lt_text,.  sqlit
27a24 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36  e3_result_text16
27a25 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ,.  sqlite3_resu
27a26 6c 74 5f 74 65 78 74 31 36 62 65 2c 0a 20 20 73  lt_text16be,.  s
27a27 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
27a28 78 74 31 36 6c 65 2c 0a 20 20 73 71 6c 69 74 65  xt16le,.  sqlite
27a29 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 2c 0a  3_result_value,.
27a2a 20 20 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62 61    sqlite3_rollba
27a2b 63 6b 5f 68 6f 6f 6b 2c 0a 20 20 73 71 6c 69 74  ck_hook,.  sqlit
27a2c 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65  e3_set_authorize
27a2d 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 73 65 74  r,.  sqlite3_set
27a2e 5f 61 75 78 64 61 74 61 2c 0a 20 20 73 71 6c 69  _auxdata,.  sqli
27a2f 74 65 33 5f 73 6e 70 72 69 6e 74 66 2c 0a 20 20  te3_snprintf,.  
27a30 73 71 6c 69 74 65 33 5f 73 74 65 70 2c 0a 20 20  sqlite3_step,.  
27a31 73 71 6c 69 74 65 33 5f 74 61 62 6c 65 5f 63 6f  sqlite3_table_co
27a32 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61 2c 0a 23  lumn_metadata,.#
27a33 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
27a34 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 20 20  IT_DEPRECATED.  
27a35 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 5f 63  sqlite3_thread_c
27a36 6c 65 61 6e 75 70 2c 0a 23 65 6c 73 65 0a 20 20  leanup,.#else.  
27a37 30 2c 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69  0,.#endif.  sqli
27a38 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65  te3_total_change
27a39 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 74 72 61  s,.  sqlite3_tra
27a3a 63 65 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ce,.#ifndef SQLI
27a3b 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54  TE_OMIT_DEPRECAT
27a3c 45 44 0a 20 20 73 71 6c 69 74 65 33 5f 74 72 61  ED.  sqlite3_tra
27a3d 6e 73 66 65 72 5f 62 69 6e 64 69 6e 67 73 2c 0a  nsfer_bindings,.
27a3e 23 65 6c 73 65 0a 20 20 30 2c 0a 23 65 6e 64 69  #else.  0,.#endi
27a3f 66 0a 20 20 73 71 6c 69 74 65 33 5f 75 70 64 61  f.  sqlite3_upda
27a40 74 65 5f 68 6f 6f 6b 2c 0a 20 20 73 71 6c 69 74  te_hook,.  sqlit
27a41 65 33 5f 75 73 65 72 5f 64 61 74 61 2c 0a 20 20  e3_user_data,.  
27a42 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
27a43 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  ob,.  sqlite3_va
27a44 6c 75 65 5f 62 79 74 65 73 2c 0a 20 20 73 71 6c  lue_bytes,.  sql
27a45 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
27a46 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  16,.  sqlite3_va
27a47 6c 75 65 5f 64 6f 75 62 6c 65 2c 0a 20 20 73 71  lue_double,.  sq
27a48 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 2c  lite3_value_int,
27a49 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
27a4a 5f 69 6e 74 36 34 2c 0a 20 20 73 71 6c 69 74 65  _int64,.  sqlite
27a4b 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f  3_value_numeric_
27a4c 74 79 70 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  type,.  sqlite3_
27a4d 76 61 6c 75 65 5f 74 65 78 74 2c 0a 20 20 73 71  value_text,.  sq
27a4e 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
27a4f 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  16,.  sqlite3_va
27a50 6c 75 65 5f 74 65 78 74 31 36 62 65 2c 0a 20 20  lue_text16be,.  
27a51 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
27a52 78 74 31 36 6c 65 2c 0a 20 20 73 71 6c 69 74 65  xt16le,.  sqlite
27a53 33 5f 76 61 6c 75 65 5f 74 79 70 65 2c 0a 20 20  3_value_type,.  
27a54 73 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66  sqlite3_vmprintf
27a55 2c 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20  ,.  /*.  ** The 
27a56 6f 72 69 67 69 6e 61 6c 20 41 50 49 20 73 65 74  original API set
27a57 20 65 6e 64 73 20 68 65 72 65 2e 20 20 41 6c 6c   ends here.  All
27a58 20 65 78 74 65 6e 73 69 6f 6e 73 20 63 61 6e 20   extensions can 
27a59 63 61 6c 6c 20 61 6e 79 0a 20 20 2a 2a 20 6f 66  call any.  ** of
27a5a 20 74 68 65 20 41 50 49 73 20 61 62 6f 76 65 20   the APIs above 
27a5b 70 72 6f 76 69 64 65 64 20 74 68 61 74 20 74 68  provided that th
27a5c 65 20 70 6f 69 6e 74 65 72 20 69 73 20 6e 6f 74  e pointer is not
27a5d 20 4e 55 4c 4c 2e 20 20 42 75 74 0a 20 20 2a 2a   NULL.  But.  **
27a5e 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20   before calling 
27a5f 41 50 49 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77  APIs that follow
27a60 2c 20 65 78 74 65 6e 73 69 6f 6e 20 73 68 6f 75  , extension shou
27a61 6c 64 20 63 68 65 63 6b 20 74 68 65 0a 20 20 2a  ld check the.  *
27a62 2a 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72  * sqlite3_libver
27a63 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 29 20 74 6f  sion_number() to
27a64 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 79 20   make sure they 
27a65 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68  are dealing with
27a66 0a 20 20 2a 2a 20 61 20 6c 69 62 72 61 72 79 20  .  ** a library 
27a67 74 68 61 74 20 69 73 20 6e 65 77 20 65 6e 6f 75  that is new enou
27a68 67 68 20 74 6f 20 73 75 70 70 6f 72 74 20 74 68  gh to support th
27a69 61 74 20 41 50 49 2e 0a 20 20 2a 2a 2a 2a 2a 2a  at API..  ******
27a6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27a6b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27a6c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27a6d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27a6e 2a 2a 2a 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  ***.  */.  sqlit
27a6f 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63  e3_overload_func
27a70 74 69 6f 6e 2c 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  tion,..  /*.  **
27a71 20 41 64 64 65 64 20 61 66 74 65 72 20 33 2e 33   Added after 3.3
27a72 2e 31 33 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  .13.  */.  sqlit
27a73 65 33 5f 70 72 65 70 61 72 65 5f 76 32 2c 0a 20  e3_prepare_v2,. 
27a74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
27a75 31 36 5f 76 32 2c 0a 20 20 73 71 6c 69 74 65 33  16_v2,.  sqlite3
27a76 5f 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 2c  _clear_bindings,
27a77 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 64 64 65  ..  /*.  ** Adde
27a78 64 20 66 6f 72 20 33 2e 34 2e 31 0a 20 20 2a 2f  d for 3.4.1.  */
27a79 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  .  sqlite3_creat
27a7a 65 5f 6d 6f 64 75 6c 65 5f 76 32 2c 0a 0a 20 20  e_module_v2,..  
27a7b 2f 2a 0a 20 20 2a 2a 20 41 64 64 65 64 20 66 6f  /*.  ** Added fo
27a7c 72 20 33 2e 35 2e 30 0a 20 20 2a 2f 0a 20 20 73  r 3.5.0.  */.  s
27a7d 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f  qlite3_bind_zero
27a7e 62 6c 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f  blob,.  sqlite3_
27a7f 62 6c 6f 62 5f 62 79 74 65 73 2c 0a 20 20 73 71  blob_bytes,.  sq
27a80 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65  lite3_blob_close
27a81 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ,.  sqlite3_blob
27a82 5f 6f 70 65 6e 2c 0a 20 20 73 71 6c 69 74 65 33  _open,.  sqlite3
27a83 5f 62 6c 6f 62 5f 72 65 61 64 2c 0a 20 20 73 71  _blob_read,.  sq
27a84 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65  lite3_blob_write
27a85 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61  ,.  sqlite3_crea
27a86 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 2c  te_collation_v2,
27a87 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f  .  sqlite3_file_
27a88 63 6f 6e 74 72 6f 6c 2c 0a 20 20 73 71 6c 69 74  control,.  sqlit
27a89 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61  e3_memory_highwa
27a8a 74 65 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ter,.  sqlite3_m
27a8b 65 6d 6f 72 79 5f 75 73 65 64 2c 0a 23 69 66 64  emory_used,.#ifd
27a8c 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ef SQLITE_MUTEX_
27a8d 4f 4d 49 54 0a 20 20 30 2c 20 0a 20 20 30 2c 20  OMIT.  0, .  0, 
27a8e 0a 20 20 30 2c 0a 20 20 30 2c 0a 20 20 30 2c 0a  .  0,.  0,.  0,.
27a8f 23 65 6c 73 65 0a 20 20 73 71 6c 69 74 65 33 5f  #else.  sqlite3_
27a90 6d 75 74 65 78 5f 61 6c 6c 6f 63 2c 0a 20 20 73  mutex_alloc,.  s
27a91 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
27a92 65 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  er,.  sqlite3_mu
27a93 74 65 78 5f 66 72 65 65 2c 0a 20 20 73 71 6c 69  tex_free,.  sqli
27a94 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 2c  te3_mutex_leave,
27a95 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
27a96 5f 74 72 79 2c 0a 23 65 6e 64 69 66 0a 20 20 73  _try,.#endif.  s
27a97 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 2c 0a  qlite3_open_v2,.
27a98 20 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73    sqlite3_releas
27a99 65 5f 6d 65 6d 6f 72 79 2c 0a 20 20 73 71 6c 69  e_memory,.  sqli
27a9a 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
27a9b 5f 6e 6f 6d 65 6d 2c 0a 20 20 73 71 6c 69 74 65  _nomem,.  sqlite
27a9c 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74  3_result_error_t
27a9d 6f 6f 62 69 67 2c 0a 20 20 73 71 6c 69 74 65 33  oobig,.  sqlite3
27a9e 5f 73 6c 65 65 70 2c 0a 20 20 73 71 6c 69 74 65  _sleep,.  sqlite
27a9f 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69  3_soft_heap_limi
27aa0 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  t,.  sqlite3_vfs
27aa1 5f 66 69 6e 64 2c 0a 20 20 73 71 6c 69 74 65 33  _find,.  sqlite3
27aa2 5f 76 66 73 5f 72 65 67 69 73 74 65 72 2c 0a 20  _vfs_register,. 
27aa3 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72   sqlite3_vfs_unr
27aa4 65 67 69 73 74 65 72 2c 0a 0a 20 20 2f 2a 0a 20  egister,..  /*. 
27aa5 20 2a 2a 20 41 64 64 65 64 20 66 6f 72 20 33 2e   ** Added for 3.
27aa6 35 2e 38 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  5.8.  */.  sqlit
27aa7 65 33 5f 74 68 72 65 61 64 73 61 66 65 2c 0a 20  e3_threadsafe,. 
27aa8 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
27aa9 7a 65 72 6f 62 6c 6f 62 2c 0a 20 20 73 71 6c 69  zeroblob,.  sqli
27aaa 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
27aab 5f 63 6f 64 65 2c 0a 20 20 73 71 6c 69 74 65 33  _code,.  sqlite3
27aac 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 2c 0a 20  _test_control,. 
27aad 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e   sqlite3_randomn
27aae 65 73 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  ess,.  sqlite3_c
27aaf 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65  ontext_db_handle
27ab0 2c 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 64 64  ,..  /*.  ** Add
27ab1 65 64 20 66 6f 72 20 33 2e 36 2e 30 0a 20 20 2a  ed for 3.6.0.  *
27ab2 2f 0a 20 20 73 71 6c 69 74 65 33 5f 65 78 74 65  /.  sqlite3_exte
27ab3 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65  nded_result_code
27ab4 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6c 69 6d  s,.  sqlite3_lim
27ab5 69 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6e 65  it,.  sqlite3_ne
27ab6 78 74 5f 73 74 6d 74 2c 0a 20 20 73 71 6c 69 74  xt_stmt,.  sqlit
27ab7 65 33 5f 73 71 6c 2c 0a 20 20 73 71 6c 69 74 65  e3_sql,.  sqlite
27ab8 33 5f 73 74 61 74 75 73 2c 0a 7d 3b 0a 0a 2f 2a  3_status,.};../*
27ab9 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6c  .** Attempt to l
27aba 6f 61 64 20 61 6e 20 53 51 4c 69 74 65 20 65 78  oad an SQLite ex
27abb 74 65 6e 73 69 6f 6e 20 6c 69 62 72 61 72 79 20  tension library 
27abc 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65  contained in the
27abd 20 66 69 6c 65 0a 2a 2a 20 7a 46 69 6c 65 2e 20   file.** zFile. 
27abe 20 54 68 65 20 65 6e 74 72 79 20 70 6f 69 6e 74   The entry point
27abf 20 69 73 20 7a 50 72 6f 63 2e 20 20 7a 50 72 6f   is zProc.  zPro
27ac0 63 20 6d 61 79 20 62 65 20 30 20 69 6e 20 77 68  c may be 0 in wh
27ac1 69 63 68 20 63 61 73 65 20 61 0a 2a 2a 20 64 65  ich case a.** de
27ac2 66 61 75 6c 74 20 65 6e 74 72 79 20 70 6f 69 6e  fault entry poin
27ac3 74 20 6e 61 6d 65 20 28 73 71 6c 69 74 65 33 5f  t name (sqlite3_
27ac4 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 29 20  extension_init) 
27ac5 69 73 20 75 73 65 64 2e 20 20 55 73 65 0a 2a 2a  is used.  Use.**
27ac6 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74 20   of the default 
27ac7 6e 61 6d 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e  name is recommen
27ac8 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  ded..**.** Retur
27ac9 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73  n SQLITE_OK on s
27aca 75 63 63 65 73 73 20 61 6e 64 20 53 51 4c 49 54  uccess and SQLIT
27acb 45 5f 45 52 52 4f 52 20 69 66 20 73 6f 6d 65 74  E_ERROR if somet
27acc 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e  hing goes wrong.
27acd 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72  .**.** If an err
27ace 6f 72 20 6f 63 63 75 72 73 20 61 6e 64 20 70 7a  or occurs and pz
27acf 45 72 72 4d 73 67 20 69 73 20 6e 6f 74 20 30 2c  ErrMsg is not 0,
27ad0 20 74 68 65 6e 20 66 69 6c 6c 20 2a 70 7a 45 72   then fill *pzEr
27ad1 72 4d 73 67 20 77 69 74 68 20 0a 2a 2a 20 65 72  rMsg with .** er
27ad2 72 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78 74  ror message text
27ad3 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66  .  The calling f
27ad4 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 66  unction should f
27ad5 72 65 65 20 74 68 69 73 20 6d 65 6d 6f 72 79 0a  ree this memory.
27ad6 2a 2a 20 62 79 20 63 61 6c 6c 69 6e 67 20 73 71  ** by calling sq
27ad7 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
27ad8 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
27ad9 20 73 71 6c 69 74 65 33 4c 6f 61 64 45 78 74 65   sqlite3LoadExte
27ada 6e 73 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33  nsion(.  sqlite3
27adb 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 2f   *db,          /
27adc 2a 20 4c 6f 61 64 20 74 68 65 20 65 78 74 65 6e  * Load the exten
27add 73 69 6f 6e 20 69 6e 74 6f 20 74 68 69 73 20 64  sion into this d
27ade 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
27adf 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  on */.  const ch
27ae0 61 72 20 2a 7a 46 69 6c 65 2c 20 20 20 20 2f 2a  ar *zFile,    /*
27ae1 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 73 68 61   Name of the sha
27ae2 72 65 64 20 6c 69 62 72 61 72 79 20 63 6f 6e 74  red library cont
27ae3 61 69 6e 69 6e 67 20 65 78 74 65 6e 73 69 6f 6e  aining extension
27ae4 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
27ae5 20 2a 7a 50 72 6f 63 2c 20 20 20 20 2f 2a 20 45   *zProc,    /* E
27ae6 6e 74 72 79 20 70 6f 69 6e 74 2e 20 20 55 73 65  ntry point.  Use
27ae7 20 22 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73   "sqlite3_extens
27ae8 69 6f 6e 5f 69 6e 69 74 22 20 69 66 20 30 20 2a  ion_init" if 0 *
27ae9 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72  /.  char **pzErr
27aea 4d 73 67 20 20 20 20 20 20 20 2f 2a 20 50 75 74  Msg       /* Put
27aeb 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 68   error message h
27aec 65 72 65 20 69 66 20 6e 6f 74 20 30 20 2a 2f 0a  ere if not 0 */.
27aed 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  ){.  sqlite3_vfs
27aee 20 2a 70 56 66 73 20 3d 20 64 62 2d 3e 70 56 66   *pVfs = db->pVf
27aef 73 3b 0a 20 20 76 6f 69 64 20 2a 68 61 6e 64 6c  s;.  void *handl
27af0 65 3b 0a 20 20 69 6e 74 20 28 2a 78 49 6e 69 74  e;.  int (*xInit
27af1 29 28 73 71 6c 69 74 65 33 2a 2c 63 68 61 72 2a  )(sqlite3*,char*
27af2 2a 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  *,const sqlite3_
27af3 61 70 69 5f 72 6f 75 74 69 6e 65 73 2a 29 3b 0a  api_routines*);.
27af4 20 20 63 68 61 72 20 2a 7a 45 72 72 6d 73 67 20    char *zErrmsg 
27af5 3d 20 30 3b 0a 20 20 76 6f 69 64 20 2a 2a 61 48  = 0;.  void **aH
27af6 61 6e 64 6c 65 3b 0a 20 20 63 6f 6e 73 74 20 69  andle;.  const i
27af7 6e 74 20 6e 4d 73 67 20 3d 20 33 30 30 3b 0a 0a  nt nMsg = 300;..
27af8 20 20 69 66 28 20 70 7a 45 72 72 4d 73 67 20 29    if( pzErrMsg )
27af9 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a   *pzErrMsg = 0;.
27afa 0a 20 20 2f 2a 20 54 69 63 6b 65 74 20 23 31 38  .  /* Ticket #18
27afb 36 33 2e 20 20 54 6f 20 61 76 6f 69 64 20 61 20  63.  To avoid a 
27afc 63 72 65 61 74 69 6e 67 20 73 65 63 75 72 69 74  creating securit
27afd 79 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20 6f  y problems for o
27afe 6c 64 65 72 0a 20 20 2a 2a 20 61 70 70 6c 69 63  lder.  ** applic
27aff 61 74 69 6f 6e 73 20 74 68 61 74 20 72 65 6c 69  ations that reli
27b00 6e 6b 20 61 67 61 69 6e 73 74 20 6e 65 77 65 72  nk against newer
27b01 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c   versions of SQL
27b02 69 74 65 2c 20 74 68 65 0a 20 20 2a 2a 20 61 62  ite, the.  ** ab
27b03 69 6c 69 74 79 20 74 6f 20 72 75 6e 20 6c 6f 61  ility to run loa
27b04 64 5f 65 78 74 65 6e 73 69 6f 6e 20 69 73 20 74  d_extension is t
27b05 75 72 6e 65 64 20 6f 66 66 20 62 79 20 64 65 66  urned off by def
27b06 61 75 6c 74 2e 20 20 4f 6e 65 0a 20 20 2a 2a 20  ault.  One.  ** 
27b07 6d 75 73 74 20 63 61 6c 6c 20 73 71 6c 69 74 65  must call sqlite
27b08 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65 78  3_enable_load_ex
27b09 74 65 6e 73 69 6f 6e 28 29 20 74 6f 20 74 75 72  tension() to tur
27b0a 6e 20 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 0a 20  n on extension. 
27b0b 20 2a 2a 20 6c 6f 61 64 69 6e 67 2e 20 20 4f 74   ** loading.  Ot
27b0c 68 65 72 77 69 73 65 20 79 6f 75 20 67 65 74 20  herwise you get 
27b0d 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 72  the following er
27b0e 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ror..  */.  if( 
27b0f 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  (db->flags & SQL
27b10 49 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f  ITE_LoadExtensio
27b11 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28  n)==0 ){.    if(
27b12 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20   pzErrMsg ){.   
27b13 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 73     *pzErrMsg = s
27b14 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
27b15 6e 6f 74 20 61 75 74 68 6f 72 69 7a 65 64 22 29  not authorized")
27b16 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ;.    }.    retu
27b17 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
27b18 0a 20 20 7d 0a 0a 20 20 69 66 28 20 7a 50 72 6f  .  }..  if( zPro
27b19 63 3d 3d 30 20 29 7b 0a 20 20 20 20 7a 50 72 6f  c==0 ){.    zPro
27b1a 63 20 3d 20 22 73 71 6c 69 74 65 33 5f 65 78 74  c = "sqlite3_ext
27b1b 65 6e 73 69 6f 6e 5f 69 6e 69 74 22 3b 0a 20 20  ension_init";.  
27b1c 7d 0a 0a 20 20 68 61 6e 64 6c 65 20 3d 20 73 71  }..  handle = sq
27b1d 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28 70 56  lite3OsDlOpen(pV
27b1e 66 73 2c 20 7a 46 69 6c 65 29 3b 0a 20 20 69 66  fs, zFile);.  if
27b1f 28 20 68 61 6e 64 6c 65 3d 3d 30 20 29 7b 0a 20  ( handle==0 ){. 
27b20 20 20 20 69 66 28 20 70 7a 45 72 72 4d 73 67 20     if( pzErrMsg 
27b21 29 7b 0a 20 20 20 20 20 20 7a 45 72 72 6d 73 67  ){.      zErrmsg
27b22 20 3d 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41   = sqlite3StackA
27b23 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 6e 4d 73  llocZero(db, nMs
27b24 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 45  g);.      if( zE
27b25 72 72 6d 73 67 20 29 7b 0a 20 20 20 20 20 20 20  rrmsg ){.       
27b26 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
27b27 66 28 6e 4d 73 67 2c 20 7a 45 72 72 6d 73 67 2c  f(nMsg, zErrmsg,
27b28 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 75   .            "u
27b29 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 73 68  nable to open sh
27b2a 61 72 65 64 20 6c 69 62 72 61 72 79 20 5b 25 73  ared library [%s
27b2b 5d 22 2c 20 7a 46 69 6c 65 29 3b 0a 20 20 20 20  ]", zFile);.    
27b2c 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 45      sqlite3OsDlE
27b2d 72 72 6f 72 28 70 56 66 73 2c 20 6e 4d 73 67 2d  rror(pVfs, nMsg-
27b2e 31 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20  1, zErrmsg);.   
27b2f 20 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d       *pzErrMsg =
27b30 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70   sqlite3DbStrDup
27b31 28 30 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20  (0, zErrmsg);.  
27b32 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61        sqlite3Sta
27b33 63 6b 46 72 65 65 28 64 62 2c 20 7a 45 72 72 6d  ckFree(db, zErrm
27b34 73 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  sg);.      }.   
27b35 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51   }.    return SQ
27b36 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
27b37 20 20 78 49 6e 69 74 20 3d 20 28 69 6e 74 28 2a    xInit = (int(*
27b38 29 28 73 71 6c 69 74 65 33 2a 2c 63 68 61 72 2a  )(sqlite3*,char*
27b39 2a 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  *,const sqlite3_
27b3a 61 70 69 5f 72 6f 75 74 69 6e 65 73 2a 29 29 0a  api_routines*)).
27b3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27b3c 20 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 53 79     sqlite3OsDlSy
27b3d 6d 28 70 56 66 73 2c 20 68 61 6e 64 6c 65 2c 20  m(pVfs, handle, 
27b3e 7a 50 72 6f 63 29 3b 0a 20 20 69 66 28 20 78 49  zProc);.  if( xI
27b3f 6e 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66  nit==0 ){.    if
27b40 28 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20  ( pzErrMsg ){.  
27b41 20 20 20 20 7a 45 72 72 6d 73 67 20 3d 20 73 71      zErrmsg = sq
27b42 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 5a  lite3StackAllocZ
27b43 65 72 6f 28 64 62 2c 20 6e 4d 73 67 29 3b 0a 20  ero(db, nMsg);. 
27b44 20 20 20 20 20 69 66 28 20 7a 45 72 72 6d 73 67       if( zErrmsg
27b45 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
27b46 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 4d 73  te3_snprintf(nMs
27b47 67 2c 20 7a 45 72 72 6d 73 67 2c 0a 20 20 20 20  g, zErrmsg,.    
27b48 20 20 20 20 20 20 20 20 22 6e 6f 20 65 6e 74 72          "no entr
27b49 79 20 70 6f 69 6e 74 20 5b 25 73 5d 20 69 6e 20  y point [%s] in 
27b4a 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20 5b  shared library [
27b4b 25 73 5d 22 2c 20 7a 50 72 6f 63 2c 7a 46 69 6c  %s]", zProc,zFil
27b4c 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  e);.        sqli
27b4d 74 65 33 4f 73 44 6c 45 72 72 6f 72 28 70 56 66  te3OsDlError(pVf
27b4e 73 2c 20 6e 4d 73 67 2d 31 2c 20 7a 45 72 72 6d  s, nMsg-1, zErrm
27b4f 73 67 29 3b 0a 20 20 20 20 20 20 20 20 2a 70 7a  sg);.        *pz
27b50 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33  ErrMsg = sqlite3
27b51 44 62 53 74 72 44 75 70 28 30 2c 20 7a 45 72 72  DbStrDup(0, zErr
27b52 6d 73 67 29 3b 0a 20 20 20 20 20 20 20 20 73 71  msg);.        sq
27b53 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65 28 64  lite3StackFree(d
27b54 62 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20  b, zErrmsg);.   
27b55 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
27b56 65 33 4f 73 44 6c 43 6c 6f 73 65 28 70 56 66 73  e3OsDlClose(pVfs
27b57 2c 20 68 61 6e 64 6c 65 29 3b 0a 20 20 20 20 7d  , handle);.    }
27b58 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
27b59 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 65 6c 73  TE_ERROR;.  }els
27b5a 65 20 69 66 28 20 78 49 6e 69 74 28 64 62 2c 20  e if( xInit(db, 
27b5b 26 7a 45 72 72 6d 73 67 2c 20 26 73 71 6c 69 74  &zErrmsg, &sqlit
27b5c 65 33 41 70 69 73 29 20 29 7b 0a 20 20 20 20 69  e3Apis) ){.    i
27b5d 66 28 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20  f( pzErrMsg ){. 
27b5e 20 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d       *pzErrMsg =
27b5f 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
27b60 28 22 65 72 72 6f 72 20 64 75 72 69 6e 67 20 69  ("error during i
27b61 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 3a 20 25  nitialization: %
27b62 73 22 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20  s", zErrmsg);.  
27b63 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f    }.    sqlite3_
27b64 66 72 65 65 28 7a 45 72 72 6d 73 67 29 3b 0a 20  free(zErrmsg);. 
27b65 20 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c     sqlite3OsDlCl
27b66 6f 73 65 28 70 56 66 73 2c 20 68 61 6e 64 6c 65  ose(pVfs, handle
27b67 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
27b68 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
27b69 0a 20 20 2f 2a 20 41 70 70 65 6e 64 20 74 68 65  .  /* Append the
27b6a 20 6e 65 77 20 73 68 61 72 65 64 20 6c 69 62 72   new shared libr
27b6b 61 72 79 20 68 61 6e 64 6c 65 20 74 6f 20 74 68  ary handle to th
27b6c 65 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e  e db->aExtension
27b6d 20 61 72 72 61 79 2e 20 2a 2f 0a 20 20 61 48 61   array. */.  aHa
27b6e 6e 64 6c 65 20 3d 20 73 71 6c 69 74 65 33 44 62  ndle = sqlite3Db
27b6f 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
27b70 69 7a 65 6f 66 28 68 61 6e 64 6c 65 29 2a 28 64  izeof(handle)*(d
27b71 62 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e 2b 31 29  b->nExtension+1)
27b72 29 3b 0a 20 20 69 66 28 20 61 48 61 6e 64 6c 65  );.  if( aHandle
27b73 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
27b74 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
27b75 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6e 45    }.  if( db->nE
27b76 78 74 65 6e 73 69 6f 6e 3e 30 20 29 7b 0a 20 20  xtension>0 ){.  
27b77 20 20 6d 65 6d 63 70 79 28 61 48 61 6e 64 6c 65    memcpy(aHandle
27b78 2c 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e  , db->aExtension
27b79 2c 20 73 69 7a 65 6f 66 28 68 61 6e 64 6c 65 29  , sizeof(handle)
27b7a 2a 64 62 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e 29  *db->nExtension)
27b7b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44  ;.  }.  sqlite3D
27b7c 62 46 72 65 65 28 64 62 2c 20 64 62 2d 3e 61 45  bFree(db, db->aE
27b7d 78 74 65 6e 73 69 6f 6e 29 3b 0a 20 20 64 62 2d  xtension);.  db-
27b7e 3e 61 45 78 74 65 6e 73 69 6f 6e 20 3d 20 61 48  >aExtension = aH
27b7f 61 6e 64 6c 65 3b 0a 0a 20 20 64 62 2d 3e 61 45  andle;..  db->aE
27b80 78 74 65 6e 73 69 6f 6e 5b 64 62 2d 3e 6e 45 78  xtension[db->nEx
27b81 74 65 6e 73 69 6f 6e 2b 2b 5d 20 3d 20 68 61 6e  tension++] = han
27b82 64 6c 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  dle;.  return SQ
27b83 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49 54  LITE_OK;.}.SQLIT
27b84 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
27b85 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e  3_load_extension
27b86 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
27b87 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 61            /* Loa
27b88 64 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e 20  d the extension 
27b89 69 6e 74 6f 20 74 68 69 73 20 64 61 74 61 62 61  into this databa
27b8a 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
27b8b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
27b8c 46 69 6c 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65  File,    /* Name
27b8d 20 6f 66 20 74 68 65 20 73 68 61 72 65 64 20 6c   of the shared l
27b8e 69 62 72 61 72 79 20 63 6f 6e 74 61 69 6e 69 6e  ibrary containin
27b8f 67 20 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 20  g extension */. 
27b90 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 72   const char *zPr
27b91 6f 63 2c 20 20 20 20 2f 2a 20 45 6e 74 72 79 20  oc,    /* Entry 
27b92 70 6f 69 6e 74 2e 20 20 55 73 65 20 22 73 71 6c  point.  Use "sql
27b93 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69  ite3_extension_i
27b94 6e 69 74 22 20 69 66 20 30 20 2a 2f 0a 20 20 63  nit" if 0 */.  c
27b95 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20  har **pzErrMsg  
27b96 20 20 20 20 20 2f 2a 20 50 75 74 20 65 72 72 6f       /* Put erro
27b97 72 20 6d 65 73 73 61 67 65 20 68 65 72 65 20 69  r message here i
27b98 66 20 6e 6f 74 20 30 20 2a 2f 0a 29 7b 0a 20 20  f not 0 */.){.  
27b99 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65  int rc;.  sqlite
27b9a 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
27b9b 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 63 20 3d  ->mutex);.  rc =
27b9c 20 73 71 6c 69 74 65 33 4c 6f 61 64 45 78 74 65   sqlite3LoadExte
27b9d 6e 73 69 6f 6e 28 64 62 2c 20 7a 46 69 6c 65 2c  nsion(db, zFile,
27b9e 20 7a 50 72 6f 63 2c 20 70 7a 45 72 72 4d 73 67   zProc, pzErrMsg
27b9f 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
27ba0 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29  3ApiExit(db, rc)
27ba1 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
27ba2 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
27ba3 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  x);.  return rc;
27ba4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 74  .}../*.** Call t
27ba5 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e  his routine when
27ba6 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
27ba7 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c 6f 73  nnection is clos
27ba8 69 6e 67 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20  ing in order.** 
27ba9 74 6f 20 63 6c 65 61 6e 20 75 70 20 6c 6f 61 64  to clean up load
27baa 65 64 20 65 78 74 65 6e 73 69 6f 6e 73 0a 2a 2f  ed extensions.*/
27bab 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
27bac 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6c 6f 73  void sqlite3Clos
27bad 65 45 78 74 65 6e 73 69 6f 6e 73 28 73 71 6c 69  eExtensions(sqli
27bae 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20  te3 *db){.  int 
27baf 69 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  i;.  assert( sql
27bb0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
27bb1 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
27bb2 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e  for(i=0; i<db->n
27bb3 45 78 74 65 6e 73 69 6f 6e 3b 20 69 2b 2b 29 7b  Extension; i++){
27bb4 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44 6c  .    sqlite3OsDl
27bb5 43 6c 6f 73 65 28 64 62 2d 3e 70 56 66 73 2c 20  Close(db->pVfs, 
27bb6 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e 5b 69  db->aExtension[i
27bb7 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  ]);.  }.  sqlite
27bb8 33 44 62 46 72 65 65 28 64 62 2c 20 64 62 2d 3e  3DbFree(db, db->
27bb9 61 45 78 74 65 6e 73 69 6f 6e 29 3b 0a 7d 0a 0a  aExtension);.}..
27bba 2f 2a 0a 2a 2a 20 45 6e 61 62 6c 65 20 6f 72 20  /*.** Enable or 
27bbb 64 69 73 61 62 6c 65 20 65 78 74 65 6e 73 69 6f  disable extensio
27bbc 6e 20 6c 6f 61 64 69 6e 67 2e 20 20 45 78 74 65  n loading.  Exte
27bbd 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 69 73  nsion loading is
27bbe 20 64 69 73 61 62 6c 65 64 20 62 79 0a 2a 2a 20   disabled by.** 
27bbf 64 65 66 61 75 6c 74 20 73 6f 20 61 73 20 6e 6f  default so as no
27bc0 74 20 74 6f 20 6f 70 65 6e 20 73 65 63 75 72 69  t to open securi
27bc1 74 79 20 68 6f 6c 65 73 20 69 6e 20 6f 6c 64 65  ty holes in olde
27bc2 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e 0a  r applications..
27bc3 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
27bc4 74 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65  t sqlite3_enable
27bc5 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28  _load_extension(
27bc6 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74  sqlite3 *db, int
27bc7 20 6f 6e 6f 66 66 29 7b 0a 20 20 73 71 6c 69 74   onoff){.  sqlit
27bc8 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
27bc9 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28  b->mutex);.  if(
27bca 20 6f 6e 6f 66 66 20 29 7b 0a 20 20 20 20 64 62   onoff ){.    db
27bcb 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54  ->flags |= SQLIT
27bcc 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 3b  E_LoadExtension;
27bcd 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 62  .  }else{.    db
27bce 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49  ->flags &= ~SQLI
27bcf 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e  TE_LoadExtension
27bd0 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
27bd1 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
27bd2 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
27bd3 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23   SQLITE_OK;.}..#
27bd4 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
27bd5 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53  OMIT_LOAD_EXTENS
27bd6 49 4f 4e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  ION */../*.** Th
27bd7 65 20 61 75 74 6f 2d 65 78 74 65 6e 73 69 6f 6e  e auto-extension
27bd8 20 63 6f 64 65 20 61 64 64 65 64 20 72 65 67 61   code added rega
27bd9 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65  rdless of whethe
27bda 72 20 6f 72 20 6e 6f 74 20 65 78 74 65 6e 73 69  r or not extensi
27bdb 6f 6e 0a 2a 2a 20 6c 6f 61 64 69 6e 67 20 69 73  on.** loading is
27bdc 20 73 75 70 70 6f 72 74 65 64 2e 20 20 57 65 20   supported.  We 
27bdd 6e 65 65 64 20 61 20 64 75 6d 6d 79 20 73 71 6c  need a dummy sql
27bde 69 74 65 33 41 70 69 73 20 70 6f 69 6e 74 65 72  ite3Apis pointer
27bdf 20 66 6f 72 20 74 68 61 74 0a 2a 2a 20 63 6f 64   for that.** cod
27be0 65 20 69 66 20 72 65 67 75 6c 61 72 20 65 78 74  e if regular ext
27be1 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 69  ension loading i
27be2 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2e  s not available.
27be3 20 20 54 68 69 73 20 69 73 20 74 68 61 74 0a 2a    This is that.*
27be4 2a 20 64 75 6d 6d 79 20 70 6f 69 6e 74 65 72 2e  * dummy pointer.
27be5 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
27be6 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45  E_OMIT_LOAD_EXTE
27be7 4e 53 49 4f 4e 0a 73 74 61 74 69 63 20 63 6f 6e  NSION.static con
27be8 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72  st sqlite3_api_r
27be9 6f 75 74 69 6e 65 73 20 73 71 6c 69 74 65 33 41  outines sqlite3A
27bea 70 69 73 20 3d 20 7b 20 30 20 7d 3b 0a 23 65 6e  pis = { 0 };.#en
27beb 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  dif.../*.** The 
27bec 66 6f 6c 6c 6f 77 69 6e 67 20 6f 62 6a 65 63 74  following object
27bed 20 68 6f 6c 64 73 20 74 68 65 20 6c 69 73 74 20   holds the list 
27bee 6f 66 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  of automatically
27bef 20 6c 6f 61 64 65 64 0a 2a 2a 20 65 78 74 65 6e   loaded.** exten
27bf0 73 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  sions..**.** Thi
27bf1 73 20 6c 69 73 74 20 69 73 20 73 68 61 72 65 64  s list is shared
27bf2 20 61 63 72 6f 73 73 20 74 68 72 65 61 64 73 2e   across threads.
27bf3 20 20 54 68 65 20 53 51 4c 49 54 45 5f 4d 55 54    The SQLITE_MUT
27bf4 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
27bf5 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62  .** mutex must b
27bf6 65 20 68 65 6c 64 20 77 68 69 6c 65 20 61 63 63  e held while acc
27bf7 65 73 73 69 6e 67 20 74 68 69 73 20 6c 69 73 74  essing this list
27bf8 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
27bf9 75 63 74 20 73 71 6c 69 74 65 33 41 75 74 6f 45  uct sqlite3AutoE
27bfa 78 74 4c 69 73 74 20 73 71 6c 69 74 65 33 41 75  xtList sqlite3Au
27bfb 74 6f 45 78 74 4c 69 73 74 3b 0a 73 74 61 74 69  toExtList;.stati
27bfc 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72  c SQLITE_WSD str
27bfd 75 63 74 20 73 71 6c 69 74 65 33 41 75 74 6f 45  uct sqlite3AutoE
27bfe 78 74 4c 69 73 74 20 7b 0a 20 20 69 6e 74 20 6e  xtList {.  int n
27bff 45 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  Ext;            
27c00 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
27c01 6e 74 72 69 65 73 20 69 6e 20 61 45 78 74 5b 5d  ntries in aExt[]
27c02 20 2a 2f 20 20 20 20 20 20 20 20 20 20 0a 20 20   */          .  
27c03 76 6f 69 64 20 28 2a 2a 61 45 78 74 29 28 76 6f  void (**aExt)(vo
27c04 69 64 29 3b 20 20 20 2f 2a 20 50 6f 69 6e 74 65  id);   /* Pointe
27c05 72 73 20 74 6f 20 74 68 65 20 65 78 74 65 6e 73  rs to the extens
27c06 69 6f 6e 20 69 6e 69 74 20 66 75 6e 63 74 69 6f  ion init functio
27c07 6e 73 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 33 41  ns */.} sqlite3A
27c08 75 74 6f 65 78 74 20 3d 20 7b 20 30 2c 20 30 20  utoext = { 0, 0 
27c09 7d 3b 0a 0a 2f 2a 20 54 68 65 20 22 77 73 64 41  };../* The "wsdA
27c0a 75 74 6f 65 78 74 22 20 6d 61 63 72 6f 20 77 69  utoext" macro wi
27c0b 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68  ll resolve to th
27c0c 65 20 61 75 74 6f 65 78 74 65 6e 73 69 6f 6e 0a  e autoextension.
27c0d 2a 2a 20 73 74 61 74 65 20 76 65 63 74 6f 72 2e  ** state vector.
27c0e 20 20 49 66 20 77 72 69 74 61 62 6c 65 20 73 74    If writable st
27c0f 61 74 69 63 20 64 61 74 61 20 69 73 20 75 6e 73  atic data is uns
27c10 75 70 70 6f 72 74 65 64 20 6f 6e 20 74 68 65 20  upported on the 
27c11 74 61 72 67 65 74 2c 0a 2a 2a 20 77 65 20 68 61  target,.** we ha
27c12 76 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65  ve to locate the
27c13 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 61 74   state vector at
27c14 20 72 75 6e 2d 74 69 6d 65 2e 20 20 49 6e 20 74   run-time.  In t
27c15 68 65 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0a 2a  he more common.*
27c16 2a 20 63 61 73 65 20 77 68 65 72 65 20 77 72 69  * case where wri
27c17 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 74  table static dat
27c18 61 20 69 73 20 73 75 70 70 6f 72 74 65 64 2c 20  a is supported, 
27c19 77 73 64 53 74 61 74 20 63 61 6e 20 72 65 66 65  wsdStat can refe
27c1a 72 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 74 6f  r directly.** to
27c1b 20 74 68 65 20 22 73 71 6c 69 74 65 33 41 75 74   the "sqlite3Aut
27c1c 6f 65 78 74 22 20 73 74 61 74 65 20 76 65 63 74  oext" state vect
27c1d 6f 72 20 64 65 63 6c 61 72 65 64 20 61 62 6f 76  or declared abov
27c1e 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  e..*/.#ifdef SQL
27c1f 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 23 20 64  ITE_OMIT_WSD.# d
27c20 65 66 69 6e 65 20 77 73 64 41 75 74 6f 65 78 74  efine wsdAutoext
27c21 49 6e 69 74 20 5c 0a 20 20 73 71 6c 69 74 65 33  Init \.  sqlite3
27c22 41 75 74 6f 45 78 74 4c 69 73 74 20 2a 78 20 3d  AutoExtList *x =
27c23 20 26 47 4c 4f 42 41 4c 28 73 71 6c 69 74 65 33   &GLOBAL(sqlite3
27c24 41 75 74 6f 45 78 74 4c 69 73 74 2c 73 71 6c 69  AutoExtList,sqli
27c25 74 65 33 41 75 74 6f 65 78 74 29 0a 23 20 64 65  te3Autoext).# de
27c26 66 69 6e 65 20 77 73 64 41 75 74 6f 65 78 74 20  fine wsdAutoext 
27c27 78 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65 66  x[0].#else.# def
27c28 69 6e 65 20 77 73 64 41 75 74 6f 65 78 74 49 6e  ine wsdAutoextIn
27c29 69 74 0a 23 20 64 65 66 69 6e 65 20 77 73 64 41  it.# define wsdA
27c2a 75 74 6f 65 78 74 20 73 71 6c 69 74 65 33 41 75  utoext sqlite3Au
27c2b 74 6f 65 78 74 0a 23 65 6e 64 69 66 0a 0a 0a 2f  toext.#endif.../
27c2c 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20  *.** Register a 
27c2d 73 74 61 74 69 63 61 6c 6c 79 20 6c 69 6e 6b 65  statically linke
27c2e 64 20 65 78 74 65 6e 73 69 6f 6e 20 74 68 61 74  d extension that
27c2f 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c   is automaticall
27c30 79 0a 2a 2a 20 6c 6f 61 64 65 64 20 62 79 20 65  y.** loaded by e
27c31 76 65 72 79 20 6e 65 77 20 64 61 74 61 62 61 73  very new databas
27c32 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f  e connection..*/
27c33 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
27c34 73 71 6c 69 74 65 33 5f 61 75 74 6f 5f 65 78 74  sqlite3_auto_ext
27c35 65 6e 73 69 6f 6e 28 76 6f 69 64 20 28 2a 78 49  ension(void (*xI
27c36 6e 69 74 29 28 76 6f 69 64 29 29 7b 0a 20 20 69  nit)(void)){.  i
27c37 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
27c38 4b 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  K;.#ifndef SQLIT
27c39 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a  E_OMIT_AUTOINIT.
27c3a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 69    rc = sqlite3_i
27c3b 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20 69  nitialize();.  i
27c3c 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74  f( rc ){.    ret
27c3d 75 72 6e 20 72 63 3b 0a 20 20 7d 65 6c 73 65 0a  urn rc;.  }else.
27c3e 23 65 6e 64 69 66 0a 20 20 7b 0a 20 20 20 20 69  #endif.  {.    i
27c3f 6e 74 20 69 3b 0a 23 69 66 20 53 51 4c 49 54 45  nt i;.#if SQLITE
27c40 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 20 20  _THREADSAFE.    
27c41 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d  sqlite3_mutex *m
27c42 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75  utex = sqlite3Mu
27c43 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  texAlloc(SQLITE_
27c44 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53  MUTEX_STATIC_MAS
27c45 54 45 52 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20  TER);.#endif.   
27c46 20 77 73 64 41 75 74 6f 65 78 74 49 6e 69 74 3b   wsdAutoextInit;
27c47 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
27c48 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b  ex_enter(mutex);
27c49 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
27c4a 77 73 64 41 75 74 6f 65 78 74 2e 6e 45 78 74 3b  wsdAutoext.nExt;
27c4b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   i++){.      if(
27c4c 20 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78 74   wsdAutoext.aExt
27c4d 5b 69 5d 3d 3d 78 49 6e 69 74 20 29 20 62 72 65  [i]==xInit ) bre
27c4e 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ak;.    }.    if
27c4f 28 20 69 3d 3d 77 73 64 41 75 74 6f 65 78 74 2e  ( i==wsdAutoext.
27c50 6e 45 78 74 20 29 7b 0a 20 20 20 20 20 20 69 6e  nExt ){.      in
27c51 74 20 6e 42 79 74 65 20 3d 20 28 77 73 64 41 75  t nByte = (wsdAu
27c52 74 6f 65 78 74 2e 6e 45 78 74 2b 31 29 2a 73 69  toext.nExt+1)*si
27c53 7a 65 6f 66 28 77 73 64 41 75 74 6f 65 78 74 2e  zeof(wsdAutoext.
27c54 61 45 78 74 5b 30 5d 29 3b 0a 20 20 20 20 20 20  aExt[0]);.      
27c55 76 6f 69 64 20 28 2a 2a 61 4e 65 77 29 28 76 6f  void (**aNew)(vo
27c56 69 64 29 3b 0a 20 20 20 20 20 20 61 4e 65 77 20  id);.      aNew 
27c57 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f  = sqlite3_reallo
27c58 63 28 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78  c(wsdAutoext.aEx
27c59 74 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20  t, nByte);.     
27c5a 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 7b 0a   if( aNew==0 ){.
27c5b 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
27c5c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
27c5d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
27c5e 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78 74 20  wsdAutoext.aExt 
27c5f 3d 20 61 4e 65 77 3b 0a 20 20 20 20 20 20 20 20  = aNew;.        
27c60 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78 74 5b  wsdAutoext.aExt[
27c61 77 73 64 41 75 74 6f 65 78 74 2e 6e 45 78 74 5d  wsdAutoext.nExt]
27c62 20 3d 20 78 49 6e 69 74 3b 0a 20 20 20 20 20 20   = xInit;.      
27c63 20 20 77 73 64 41 75 74 6f 65 78 74 2e 6e 45 78    wsdAutoext.nEx
27c64 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  t++;.      }.   
27c65 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d   }.    sqlite3_m
27c66 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78  utex_leave(mutex
27c67 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  );.    assert( (
27c68 72 63 26 30 78 66 66 29 3d 3d 72 63 20 29 3b 0a  rc&0xff)==rc );.
27c69 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
27c6a 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 65   }.}../*.** Rese
27c6b 74 20 74 68 65 20 61 75 74 6f 6d 61 74 69 63 20  t the automatic 
27c6c 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e  extension loadin
27c6d 67 20 6d 65 63 68 61 6e 69 73 6d 2e 0a 2a 2f 0a  g mechanism..*/.
27c6e 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
27c6f 73 71 6c 69 74 65 33 5f 72 65 73 65 74 5f 61 75  sqlite3_reset_au
27c70 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69  to_extension(voi
27c71 64 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  d){.#ifndef SQLI
27c72 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54  TE_OMIT_AUTOINIT
27c73 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69  .  if( sqlite3_i
27c74 6e 69 74 69 61 6c 69 7a 65 28 29 3d 3d 53 51 4c  nitialize()==SQL
27c75 49 54 45 5f 4f 4b 20 29 0a 23 65 6e 64 69 66 0a  ITE_OK ).#endif.
27c76 20 20 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54    {.#if SQLITE_T
27c77 48 52 45 41 44 53 41 46 45 0a 20 20 20 20 73 71  HREADSAFE.    sq
27c78 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74  lite3_mutex *mut
27c79 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65  ex = sqlite3Mute
27c7a 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
27c7b 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45  TEX_STATIC_MASTE
27c7c 52 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 77  R);.#endif.    w
27c7d 73 64 41 75 74 6f 65 78 74 49 6e 69 74 3b 0a 20  sdAutoextInit;. 
27c7e 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
27c7f 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20  _enter(mutex);. 
27c80 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
27c81 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78 74 29  wsdAutoext.aExt)
27c82 3b 0a 20 20 20 20 77 73 64 41 75 74 6f 65 78 74  ;.    wsdAutoext
27c83 2e 61 45 78 74 20 3d 20 30 3b 0a 20 20 20 20 77  .aExt = 0;.    w
27c84 73 64 41 75 74 6f 65 78 74 2e 6e 45 78 74 20 3d  sdAutoext.nExt =
27c85 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   0;.    sqlite3_
27c86 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65  mutex_leave(mute
27c87 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  x);.  }.}../*.**
27c88 20 4c 6f 61 64 20 61 6c 6c 20 61 75 74 6f 6d 61   Load all automa
27c89 74 69 63 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a  tic extensions..
27c8a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 74 68 69 6e  **.** If anythin
27c8b 67 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20 73 65  g goes wrong, se
27c8c 74 20 61 6e 20 65 72 72 6f 72 20 69 6e 20 74 68  t an error in th
27c8d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
27c8e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ction..*/.SQLITE
27c8f 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
27c90 6c 69 74 65 33 41 75 74 6f 4c 6f 61 64 45 78 74  lite3AutoLoadExt
27c91 65 6e 73 69 6f 6e 73 28 73 71 6c 69 74 65 33 20  ensions(sqlite3 
27c92 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  *db){.  int i;. 
27c93 20 69 6e 74 20 67 6f 20 3d 20 31 3b 0a 20 20 69   int go = 1;.  i
27c94 6e 74 20 28 2a 78 49 6e 69 74 29 28 73 71 6c 69  nt (*xInit)(sqli
27c95 74 65 33 2a 2c 63 68 61 72 2a 2a 2c 63 6f 6e 73  te3*,char**,cons
27c96 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f  t sqlite3_api_ro
27c97 75 74 69 6e 65 73 2a 29 3b 0a 0a 20 20 77 73 64  utines*);..  wsd
27c98 41 75 74 6f 65 78 74 49 6e 69 74 3b 0a 20 20 69  AutoextInit;.  i
27c99 66 28 20 77 73 64 41 75 74 6f 65 78 74 2e 6e 45  f( wsdAutoext.nE
27c9a 78 74 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20  xt==0 ){.    /* 
27c9b 43 6f 6d 6d 6f 6e 20 63 61 73 65 3a 20 65 61 72  Common case: ear
27c9c 6c 79 20 6f 75 74 20 77 69 74 68 6f 75 74 20 65  ly out without e
27c9d 76 65 72 79 20 68 61 76 69 6e 67 20 74 6f 20 61  very having to a
27c9e 63 71 75 69 72 65 20 61 20 6d 75 74 65 78 20 2a  cquire a mutex *
27c9f 2f 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  /.    return;.  
27ca0 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 67 6f 3b  }.  for(i=0; go;
27ca1 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72 20   i++){.    char 
27ca2 2a 7a 45 72 72 6d 73 67 3b 0a 23 69 66 20 53 51  *zErrmsg;.#if SQ
27ca3 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a  LITE_THREADSAFE.
27ca4 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
27ca5 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c 69 74  x *mutex = sqlit
27ca6 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
27ca7 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
27ca8 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e 64 69 66  _MASTER);.#endif
27ca9 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
27caa 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b  ex_enter(mutex);
27cab 0a 20 20 20 20 69 66 28 20 69 3e 3d 77 73 64 41  .    if( i>=wsdA
27cac 75 74 6f 65 78 74 2e 6e 45 78 74 20 29 7b 0a 20  utoext.nExt ){. 
27cad 20 20 20 20 20 78 49 6e 69 74 20 3d 20 30 3b 0a       xInit = 0;.
27cae 20 20 20 20 20 20 67 6f 20 3d 20 30 3b 0a 20 20        go = 0;.  
27caf 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 78    }else{.      x
27cb0 49 6e 69 74 20 3d 20 28 69 6e 74 28 2a 29 28 73  Init = (int(*)(s
27cb1 71 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2a 2c 63  qlite3*,char**,c
27cb2 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69  onst sqlite3_api
27cb3 5f 72 6f 75 74 69 6e 65 73 2a 29 29 0a 20 20 20  _routines*)).   
27cb4 20 20 20 20 20 20 20 20 20 20 20 77 73 64 41 75             wsdAu
27cb5 74 6f 65 78 74 2e 61 45 78 74 5b 69 5d 3b 0a 20  toext.aExt[i];. 
27cb6 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
27cb7 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74  _mutex_leave(mut
27cb8 65 78 29 3b 0a 20 20 20 20 7a 45 72 72 6d 73 67  ex);.    zErrmsg
27cb9 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 78 49   = 0;.    if( xI
27cba 6e 69 74 20 26 26 20 78 49 6e 69 74 28 64 62 2c  nit && xInit(db,
27cbb 20 26 7a 45 72 72 6d 73 67 2c 20 26 73 71 6c 69   &zErrmsg, &sqli
27cbc 74 65 33 41 70 69 73 29 20 29 7b 0a 20 20 20 20  te3Apis) ){.    
27cbd 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
27cbe 62 2c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c  b, SQLITE_ERROR,
27cbf 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 61 75  .            "au
27cc0 74 6f 6d 61 74 69 63 20 65 78 74 65 6e 73 69 6f  tomatic extensio
27cc1 6e 20 6c 6f 61 64 69 6e 67 20 66 61 69 6c 65 64  n loading failed
27cc2 3a 20 25 73 22 2c 20 7a 45 72 72 6d 73 67 29 3b  : %s", zErrmsg);
27cc3 0a 20 20 20 20 20 20 67 6f 20 3d 20 30 3b 0a 20  .      go = 0;. 
27cc4 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
27cc5 5f 66 72 65 65 28 7a 45 72 72 6d 73 67 29 3b 0a  _free(zErrmsg);.
27cc6 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a    }.}../********
27cc7 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6c 6f  ****** End of lo
27cc8 61 64 65 78 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  adext.c ********
27cc9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27cca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27ccb 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
27ccc 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
27ccd 65 20 70 72 61 67 6d 61 2e 63 20 2a 2a 2a 2a 2a  e pragma.c *****
27cce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27ccf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27cd0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
27cd1 33 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20  3 April 6.**.** 
27cd2 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
27cd3 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
27cd4 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
27cd5 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
27cd6 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
27cd7 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
27cd8 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
27cd9 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
27cda 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
27cdb 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
27cdc 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
27cdd 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
27cde 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
27cdf 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
27ce0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
27ce1 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
27ce2 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
27ce3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27ce4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27ce5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27ce6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27ce7 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
27ce8 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
27ce9 6f 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c  ode used to impl
27cea 65 6d 65 6e 74 20 74 68 65 20 50 52 41 47 4d 41  ement the PRAGMA
27ceb 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20   command..**.** 
27cec 24 49 64 3a 20 70 72 61 67 6d 61 2e 63 2c 76 20  $Id: pragma.c,v 
27ced 31 2e 32 31 34 20 32 30 30 39 2f 30 37 2f 30 32  1.214 2009/07/02
27cee 20 30 37 3a 34 37 3a 33 33 20 64 61 6e 69 65 6c   07:47:33 daniel
27cef 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a  k1977 Exp $.*/..
27cf0 2f 2a 20 49 67 6e 6f 72 65 20 74 68 69 73 20 77  /* Ignore this w
27cf1 68 6f 6c 65 20 66 69 6c 65 20 69 66 20 70 72 61  hole file if pra
27cf2 67 6d 61 73 20 61 72 65 20 64 69 73 61 62 6c 65  gmas are disable
27cf3 64 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65  d.*/.#if !define
27cf4 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52  d(SQLITE_OMIT_PR
27cf5 41 47 4d 41 29 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74  AGMA)../*.** Int
27cf6 65 72 70 72 65 74 20 74 68 65 20 67 69 76 65 6e  erpret the given
27cf7 20 73 74 72 69 6e 67 20 61 73 20 61 20 73 61 66   string as a saf
27cf8 65 74 79 20 6c 65 76 65 6c 2e 20 20 52 65 74 75  ety level.  Retu
27cf9 72 6e 20 30 20 66 6f 72 20 4f 46 46 2c 0a 2a 2a  rn 0 for OFF,.**
27cfa 20 31 20 66 6f 72 20 4f 4e 20 6f 72 20 4e 4f 52   1 for ON or NOR
27cfb 4d 41 4c 20 61 6e 64 20 32 20 66 6f 72 20 46 55  MAL and 2 for FU
27cfc 4c 4c 2e 20 20 52 65 74 75 72 6e 20 31 20 66 6f  LL.  Return 1 fo
27cfd 72 20 61 6e 20 65 6d 70 74 79 20 6f 72 20 0a 2a  r an empty or .*
27cfe 2a 20 75 6e 72 65 63 6f 67 6e 69 7a 65 64 20 73  * unrecognized s
27cff 74 72 69 6e 67 20 61 72 67 75 6d 65 6e 74 2e 0a  tring argument..
27d00 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  **.** Note that 
27d01 74 68 65 20 76 61 6c 75 65 73 20 72 65 74 75 72  the values retur
27d02 6e 65 64 20 61 72 65 20 6f 6e 65 20 6c 65 73 73  ned are one less
27d03 20 74 68 61 74 20 74 68 65 20 76 61 6c 75 65 73   that the values
27d04 20 74 68 61 74 0a 2a 2a 20 73 68 6f 75 6c 64 20   that.** should 
27d05 62 65 20 70 61 73 73 65 64 20 69 6e 74 6f 20 73  be passed into s
27d06 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 53 61  qlite3BtreeSetSa
27d07 66 65 74 79 4c 65 76 65 6c 28 29 2e 20 20 54 68  fetyLevel().  Th
27d08 65 20 69 73 20 64 6f 6e 65 0a 2a 2a 20 74 6f 20  e is done.** to 
27d09 73 75 70 70 6f 72 74 20 6c 65 67 61 63 79 20 53  support legacy S
27d0a 51 4c 20 63 6f 64 65 2e 20 20 54 68 65 20 73 61  QL code.  The sa
27d0b 66 65 74 79 20 6c 65 76 65 6c 20 75 73 65 64 20  fety level used 
27d0c 74 6f 20 62 65 20 62 6f 6f 6c 65 61 6e 0a 2a 2a  to be boolean.**
27d0d 20 61 6e 64 20 6f 6c 64 65 72 20 73 63 72 69 70   and older scrip
27d0e 74 73 20 6d 61 79 20 68 61 76 65 20 75 73 65 64  ts may have used
27d0f 20 6e 75 6d 62 65 72 73 20 30 20 66 6f 72 20 4f   numbers 0 for O
27d10 46 46 20 61 6e 64 20 31 20 66 6f 72 20 4f 4e 2e  FF and 1 for ON.
27d11 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 67 65  .*/.static u8 ge
27d12 74 53 61 66 65 74 79 4c 65 76 65 6c 28 63 6f 6e  tSafetyLevel(con
27d13 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 20  st char *z){.   
27d14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27d15 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 32 33            /* 123
27d16 34 35 36 37 38 39 20 31 32 33 34 35 36 37 38 39  456789 123456789
27d17 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e   */.  static con
27d18 73 74 20 63 68 61 72 20 7a 54 65 78 74 5b 5d 20  st char zText[] 
27d19 3d 20 22 6f 6e 6f 66 66 61 6c 73 65 79 65 73 74  = "onoffalseyest
27d1a 72 75 65 66 75 6c 6c 22 3b 0a 20 20 73 74 61 74  ruefull";.  stat
27d1b 69 63 20 63 6f 6e 73 74 20 75 38 20 69 4f 66 66  ic const u8 iOff
27d1c 73 65 74 5b 5d 20 3d 20 7b 30 2c 20 31 2c 20 32  set[] = {0, 1, 2
27d1d 2c 20 34 2c 20 39 2c 20 31 32 2c 20 31 36 7d 3b  , 4, 9, 12, 16};
27d1e 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
27d1f 75 38 20 69 4c 65 6e 67 74 68 5b 5d 20 3d 20 7b  u8 iLength[] = {
27d20 32 2c 20 32 2c 20 33 2c 20 35 2c 20 33 2c 20 34  2, 2, 3, 5, 3, 4
27d21 2c 20 34 7d 3b 0a 20 20 73 74 61 74 69 63 20 63  , 4};.  static c
27d22 6f 6e 73 74 20 75 38 20 69 56 61 6c 75 65 5b 5d  onst u8 iValue[]
27d23 20 3d 20 20 7b 31 2c 20 30 2c 20 30 2c 20 30 2c   =  {1, 0, 0, 0,
27d24 20 31 2c 20 31 2c 20 32 7d 3b 0a 20 20 69 6e 74   1, 1, 2};.  int
27d25 20 69 2c 20 6e 3b 0a 20 20 69 66 28 20 73 71 6c   i, n;.  if( sql
27d26 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20  ite3Isdigit(*z) 
27d27 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 75  ){.    return (u
27d28 38 29 61 74 6f 69 28 7a 29 3b 0a 20 20 7d 0a 20  8)atoi(z);.  }. 
27d29 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c   n = sqlite3Strl
27d2a 65 6e 33 30 28 7a 29 3b 0a 20 20 66 6f 72 28 69  en30(z);.  for(i
27d2b 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28  =0; i<ArraySize(
27d2c 69 4c 65 6e 67 74 68 29 3b 20 69 2b 2b 29 7b 0a  iLength); i++){.
27d2d 20 20 20 20 69 66 28 20 69 4c 65 6e 67 74 68 5b      if( iLength[
27d2e 69 5d 3d 3d 6e 20 26 26 20 73 71 6c 69 74 65 33  i]==n && sqlite3
27d2f 53 74 72 4e 49 43 6d 70 28 26 7a 54 65 78 74 5b  StrNICmp(&zText[
27d30 69 4f 66 66 73 65 74 5b 69 5d 5d 2c 7a 2c 6e 29  iOffset[i]],z,n)
27d31 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ==0 ){.      ret
27d32 75 72 6e 20 69 56 61 6c 75 65 5b 69 5d 3b 0a 20  urn iValue[i];. 
27d33 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
27d34 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  n 1;.}../*.** In
27d35 74 65 72 70 72 65 74 20 74 68 65 20 67 69 76 65  terpret the give
27d36 6e 20 73 74 72 69 6e 67 20 61 73 20 61 20 62 6f  n string as a bo
27d37 6f 6c 65 61 6e 20 76 61 6c 75 65 2e 0a 2a 2f 0a  olean value..*/.
27d38 73 74 61 74 69 63 20 75 38 20 67 65 74 42 6f 6f  static u8 getBoo
27d39 6c 65 61 6e 28 63 6f 6e 73 74 20 63 68 61 72 20  lean(const char 
27d3a 2a 7a 29 7b 0a 20 20 72 65 74 75 72 6e 20 67 65  *z){.  return ge
27d3b 74 53 61 66 65 74 79 4c 65 76 65 6c 28 7a 29 26  tSafetyLevel(z)&
27d3c 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65  1;.}../*.** Inte
27d3d 72 70 72 65 74 20 74 68 65 20 67 69 76 65 6e 20  rpret the given 
27d3e 73 74 72 69 6e 67 20 61 73 20 61 20 6c 6f 63 6b  string as a lock
27d3f 69 6e 67 20 6d 6f 64 65 20 76 61 6c 75 65 2e 0a  ing mode value..
27d40 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65  */.static int ge
27d41 74 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 63 6f 6e  tLockingMode(con
27d42 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69  st char *z){.  i
27d43 66 28 20 7a 20 29 7b 0a 20 20 20 20 69 66 28 20  f( z ){.    if( 
27d44 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d  0==sqlite3StrICm
27d45 70 28 7a 2c 20 22 65 78 63 6c 75 73 69 76 65 22  p(z, "exclusive"
27d46 29 20 29 20 72 65 74 75 72 6e 20 50 41 47 45 52  ) ) return PAGER
27d47 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43  _LOCKINGMODE_EXC
27d48 4c 55 53 49 56 45 3b 0a 20 20 20 20 69 66 28 20  LUSIVE;.    if( 
27d49 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d  0==sqlite3StrICm
27d4a 70 28 7a 2c 20 22 6e 6f 72 6d 61 6c 22 29 20 29  p(z, "normal") )
27d4b 20 72 65 74 75 72 6e 20 50 41 47 45 52 5f 4c 4f   return PAGER_LO
27d4c 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c  CKINGMODE_NORMAL
27d4d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 50  ;.  }.  return P
27d4e 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
27d4f 5f 51 55 45 52 59 3b 0a 7d 0a 0a 23 69 66 6e 64  _QUERY;.}..#ifnd
27d50 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
27d51 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20  UTOVACUUM./*.** 
27d52 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 67 69  Interpret the gi
27d53 76 65 6e 20 73 74 72 69 6e 67 20 61 73 20 61 6e  ven string as an
27d54 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64   auto-vacuum mod
27d55 65 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54  e value..**.** T
27d56 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
27d57 69 6e 67 73 2c 20 22 6e 6f 6e 65 22 2c 20 22 66  ings, "none", "f
27d58 75 6c 6c 22 20 61 6e 64 20 22 69 6e 63 72 65 6d  ull" and "increm
27d59 65 6e 74 61 6c 22 20 61 72 65 20 0a 2a 2a 20 61  ental" are .** a
27d5a 63 63 65 70 74 61 62 6c 65 2c 20 61 73 20 61 72  cceptable, as ar
27d5b 65 20 74 68 65 69 72 20 6e 75 6d 65 72 69 63 20  e their numeric 
27d5c 65 71 75 69 76 61 6c 65 6e 74 73 3a 20 30 2c 20  equivalents: 0, 
27d5d 31 20 61 6e 64 20 32 20 72 65 73 70 65 63 74 69  1 and 2 respecti
27d5e 76 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  vely..*/.static 
27d5f 69 6e 74 20 67 65 74 41 75 74 6f 56 61 63 75 75  int getAutoVacuu
27d60 6d 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29  m(const char *z)
27d61 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28  {.  int i;.  if(
27d62 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43   0==sqlite3StrIC
27d63 6d 70 28 7a 2c 20 22 6e 6f 6e 65 22 29 20 29 20  mp(z, "none") ) 
27d64 72 65 74 75 72 6e 20 42 54 52 45 45 5f 41 55 54  return BTREE_AUT
27d65 4f 56 41 43 55 55 4d 5f 4e 4f 4e 45 3b 0a 20 20  OVACUUM_NONE;.  
27d66 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 53 74  if( 0==sqlite3St
27d67 72 49 43 6d 70 28 7a 2c 20 22 66 75 6c 6c 22 29  rICmp(z, "full")
27d68 20 29 20 72 65 74 75 72 6e 20 42 54 52 45 45 5f   ) return BTREE_
27d69 41 55 54 4f 56 41 43 55 55 4d 5f 46 55 4c 4c 3b  AUTOVACUUM_FULL;
27d6a 0a 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65  .  if( 0==sqlite
27d6b 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 69 6e 63  3StrICmp(z, "inc
27d6c 72 65 6d 65 6e 74 61 6c 22 29 20 29 20 72 65 74  remental") ) ret
27d6d 75 72 6e 20 42 54 52 45 45 5f 41 55 54 4f 56 41  urn BTREE_AUTOVA
27d6e 43 55 55 4d 5f 49 4e 43 52 3b 0a 20 20 69 20 3d  CUUM_INCR;.  i =
27d6f 20 61 74 6f 69 28 7a 29 3b 0a 20 20 72 65 74 75   atoi(z);.  retu
27d70 72 6e 20 28 75 38 29 28 28 69 3e 3d 30 26 26 69  rn (u8)((i>=0&&i
27d71 3c 3d 32 29 3f 69 3a 30 29 3b 0a 7d 0a 23 65 6e  <=2)?i:0);.}.#en
27d72 64 69 66 20 2f 2a 20 69 66 6e 64 65 66 20 53 51  dif /* ifndef SQ
27d73 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
27d74 43 55 55 4d 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  CUUM */..#ifndef
27d75 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47   SQLITE_OMIT_PAG
27d76 45 52 5f 50 52 41 47 4d 41 53 0a 2f 2a 0a 2a 2a  ER_PRAGMAS./*.**
27d77 20 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 67   Interpret the g
27d78 69 76 65 6e 20 73 74 72 69 6e 67 20 61 73 20 61  iven string as a
27d79 20 74 65 6d 70 20 64 62 20 6c 6f 63 61 74 69 6f   temp db locatio
27d7a 6e 2e 20 52 65 74 75 72 6e 20 31 20 66 6f 72 20  n. Return 1 for 
27d7b 66 69 6c 65 0a 2a 2a 20 62 61 63 6b 65 64 20 74  file.** backed t
27d7c 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73  emporary databas
27d7d 65 73 2c 20 32 20 66 6f 72 20 74 68 65 20 52 65  es, 2 for the Re
27d7e 64 2d 42 6c 61 63 6b 20 74 72 65 65 20 69 6e 20  d-Black tree in 
27d7f 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 0a  memory database.
27d80 2a 2a 20 61 6e 64 20 30 20 74 6f 20 75 73 65 20  ** and 0 to use 
27d81 74 68 65 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65  the compile-time
27d82 20 64 65 66 61 75 6c 74 2e 0a 2a 2f 0a 73 74 61   default..*/.sta
27d83 74 69 63 20 69 6e 74 20 67 65 74 54 65 6d 70 53  tic int getTempS
27d84 74 6f 72 65 28 63 6f 6e 73 74 20 63 68 61 72 20  tore(const char 
27d85 2a 7a 29 7b 0a 20 20 69 66 28 20 7a 5b 30 5d 3e  *z){.  if( z[0]>
27d86 3d 27 30 27 20 26 26 20 7a 5b 30 5d 3c 3d 27 32  ='0' && z[0]<='2
27d87 27 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ' ){.    return 
27d88 7a 5b 30 5d 20 2d 20 27 30 27 3b 0a 20 20 7d 65  z[0] - '0';.  }e
27d89 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 53  lse if( sqlite3S
27d8a 74 72 49 43 6d 70 28 7a 2c 20 22 66 69 6c 65 22  trICmp(z, "file"
27d8b 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  )==0 ){.    retu
27d8c 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66  rn 1;.  }else if
27d8d 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
27d8e 28 7a 2c 20 22 6d 65 6d 6f 72 79 22 29 3d 3d 30  (z, "memory")==0
27d8f 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 32   ){.    return 2
27d90 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
27d91 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 23  eturn 0;.  }.}.#
27d92 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
27d93 50 41 47 45 52 5f 50 52 41 47 4d 41 53 20 2a 2f  PAGER_PRAGMAS */
27d94 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
27d95 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47  _OMIT_PAGER_PRAG
27d96 4d 41 53 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69  MAS./*.** Invali
27d97 64 61 74 65 20 74 65 6d 70 20 73 74 6f 72 61 67  date temp storag
27d98 65 2c 20 65 69 74 68 65 72 20 77 68 65 6e 20 74  e, either when t
27d99 68 65 20 74 65 6d 70 20 73 74 6f 72 61 67 65 20  he temp storage 
27d9a 69 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 66 72  is changed.** fr
27d9b 6f 6d 20 64 65 66 61 75 6c 74 2c 20 6f 72 20 77  om default, or w
27d9c 68 65 6e 20 27 66 69 6c 65 27 20 61 6e 64 20 74  hen 'file' and t
27d9d 68 65 20 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69  he temp_store_di
27d9e 72 65 63 74 6f 72 79 20 68 61 73 20 63 68 61 6e  rectory has chan
27d9f 67 65 64 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ged.*/.static in
27da0 74 20 69 6e 76 61 6c 69 64 61 74 65 54 65 6d 70  t invalidateTemp
27da1 53 74 6f 72 61 67 65 28 50 61 72 73 65 20 2a 70  Storage(Parse *p
27da2 50 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65  Parse){.  sqlite
27da3 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
27da4 64 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e 61 44  db;.  if( db->aD
27da5 62 5b 31 5d 2e 70 42 74 21 3d 30 20 29 7b 0a 20  b[1].pBt!=0 ){. 
27da6 20 20 20 69 66 28 20 21 64 62 2d 3e 61 75 74 6f     if( !db->auto
27da7 43 6f 6d 6d 69 74 20 7c 7c 20 73 71 6c 69 74 65  Commit || sqlite
27da8 33 42 74 72 65 65 49 73 49 6e 52 65 61 64 54 72  3BtreeIsInReadTr
27da9 61 6e 73 28 64 62 2d 3e 61 44 62 5b 31 5d 2e 70  ans(db->aDb[1].p
27daa 42 74 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Bt) ){.      sql
27dab 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
27dac 72 73 65 2c 20 22 74 65 6d 70 6f 72 61 72 79 20  rse, "temporary 
27dad 73 74 6f 72 61 67 65 20 63 61 6e 6e 6f 74 20 62  storage cannot b
27dae 65 20 63 68 61 6e 67 65 64 20 22 0a 20 20 20 20  e changed ".    
27daf 20 20 20 20 22 66 72 6f 6d 20 77 69 74 68 69 6e      "from within
27db0 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 22 29   a transaction")
27db1 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
27db2 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
27db3 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74   }.    sqlite3Bt
27db4 72 65 65 43 6c 6f 73 65 28 64 62 2d 3e 61 44 62  reeClose(db->aDb
27db5 5b 31 5d 2e 70 42 74 29 3b 0a 20 20 20 20 64 62  [1].pBt);.    db
27db6 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 20 3d 20 30  ->aDb[1].pBt = 0
27db7 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 73  ;.    sqlite3Res
27db8 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61  etInternalSchema
27db9 28 64 62 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72  (db, 0);.  }.  r
27dba 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
27dbb 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
27dbc 49 54 45 5f 50 41 47 45 52 5f 50 52 41 47 4d 41  ITE_PAGER_PRAGMA
27dbd 53 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  S */..#ifndef SQ
27dbe 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f  LITE_OMIT_PAGER_
27dbf 50 52 41 47 4d 41 53 0a 2f 2a 0a 2a 2a 20 49 66  PRAGMAS./*.** If
27dc0 20 74 68 65 20 54 45 4d 50 20 64 61 74 61 62 61   the TEMP databa
27dc1 73 65 20 69 73 20 6f 70 65 6e 2c 20 63 6c 6f 73  se is open, clos
27dc2 65 20 69 74 20 61 6e 64 20 6d 61 72 6b 20 74 68  e it and mark th
27dc3 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  e database schem
27dc4 61 0a 2a 2a 20 61 73 20 6e 65 65 64 69 6e 67 20  a.** as needing 
27dc5 72 65 6c 6f 61 64 69 6e 67 2e 20 20 54 68 69 73  reloading.  This
27dc6 20 6d 75 73 74 20 62 65 20 64 6f 6e 65 20 77 68   must be done wh
27dc7 65 6e 20 75 73 69 6e 67 20 74 68 65 20 53 51 4c  en using the SQL
27dc8 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 0a 2a  ITE_TEMP_STORE.*
27dc9 2a 20 6f 72 20 44 45 46 41 55 4c 54 5f 54 45 4d  * or DEFAULT_TEM
27dca 50 5f 53 54 4f 52 45 20 70 72 61 67 6d 61 73 2e  P_STORE pragmas.
27dcb 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
27dcc 68 61 6e 67 65 54 65 6d 70 53 74 6f 72 61 67 65  hangeTempStorage
27dcd 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
27dce 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 6f  const char *zSto
27dcf 72 61 67 65 54 79 70 65 29 7b 0a 20 20 69 6e 74  rageType){.  int
27dd0 20 74 73 20 3d 20 67 65 74 54 65 6d 70 53 74 6f   ts = getTempSto
27dd1 72 65 28 7a 53 74 6f 72 61 67 65 54 79 70 65 29  re(zStorageType)
27dd2 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
27dd3 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
27dd4 69 66 28 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f  if( db->temp_sto
27dd5 72 65 3d 3d 74 73 20 29 20 72 65 74 75 72 6e 20  re==ts ) return 
27dd6 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28  SQLITE_OK;.  if(
27dd7 20 69 6e 76 61 6c 69 64 61 74 65 54 65 6d 70 53   invalidateTempS
27dd8 74 6f 72 61 67 65 28 20 70 50 61 72 73 65 20 29  torage( pParse )
27dd9 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 20 29 7b   != SQLITE_OK ){
27dda 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
27ddb 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20  TE_ERROR;.  }.  
27ddc 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 20 3d  db->temp_store =
27ddd 20 28 75 38 29 74 73 3b 0a 20 20 72 65 74 75 72   (u8)ts;.  retur
27dde 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23  n SQLITE_OK;.}.#
27ddf 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
27de0 50 41 47 45 52 5f 50 52 41 47 4d 41 53 20 2a 2f  PAGER_PRAGMAS */
27de1 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
27de2 20 63 6f 64 65 20 74 6f 20 72 65 74 75 72 6e 20   code to return 
27de3 61 20 73 69 6e 67 6c 65 20 69 6e 74 65 67 65 72  a single integer
27de4 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69   value..*/.stati
27de5 63 20 76 6f 69 64 20 72 65 74 75 72 6e 53 69 6e  c void returnSin
27de6 67 6c 65 49 6e 74 28 50 61 72 73 65 20 2a 70 50  gleInt(Parse *pP
27de7 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72  arse, const char
27de8 20 2a 7a 4c 61 62 65 6c 2c 20 69 36 34 20 76 61   *zLabel, i64 va
27de9 6c 75 65 29 7b 0a 20 20 56 64 62 65 20 2a 76 20  lue){.  Vdbe *v 
27dea 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
27deb 28 70 50 61 72 73 65 29 3b 0a 20 20 69 6e 74 20  (pParse);.  int 
27dec 6d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  mem = ++pParse->
27ded 6e 4d 65 6d 3b 0a 20 20 69 36 34 20 2a 70 49 36  nMem;.  i64 *pI6
27dee 34 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  4 = sqlite3DbMal
27def 6c 6f 63 52 61 77 28 70 50 61 72 73 65 2d 3e 64  locRaw(pParse->d
27df0 62 2c 20 73 69 7a 65 6f 66 28 76 61 6c 75 65 29  b, sizeof(value)
27df1 29 3b 0a 20 20 69 66 28 20 70 49 36 34 20 29 7b  );.  if( pI64 ){
27df2 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 49 36 34  .    memcpy(pI64
27df3 2c 20 26 76 61 6c 75 65 2c 20 73 69 7a 65 6f 66  , &value, sizeof
27df4 28 76 61 6c 75 65 29 29 3b 0a 20 20 7d 0a 20 20  (value));.  }.  
27df5 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
27df6 34 28 76 2c 20 4f 50 5f 49 6e 74 36 34 2c 20 30  4(v, OP_Int64, 0
27df7 2c 20 6d 65 6d 2c 20 30 2c 20 28 63 68 61 72 2a  , mem, 0, (char*
27df8 29 70 49 36 34 2c 20 50 34 5f 49 4e 54 36 34 29  )pI64, P4_INT64)
27df9 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 53  ;.  sqlite3VdbeS
27dfa 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b  etNumCols(v, 1);
27dfb 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  .  sqlite3VdbeSe
27dfc 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43  tColName(v, 0, C
27dfd 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4c 61  OLNAME_NAME, zLa
27dfe 62 65 6c 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  bel, SQLITE_STAT
27dff 49 43 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  IC);.  sqlite3Vd
27e00 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
27e01 65 73 75 6c 74 52 6f 77 2c 20 6d 65 6d 2c 20 31  esultRow, mem, 1
27e02 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  );.}..#ifndef SQ
27e03 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 41 47 5f 50  LITE_OMIT_FLAG_P
27e04 52 41 47 4d 41 53 0a 2f 2a 0a 2a 2a 20 43 68 65  RAGMAS./*.** Che
27e05 63 6b 20 74 6f 20 73 65 65 20 69 66 20 7a 52 69  ck to see if zRi
27e06 67 68 74 20 61 6e 64 20 7a 4c 65 66 74 20 72 65  ght and zLeft re
27e07 66 65 72 20 74 6f 20 61 20 70 72 61 67 6d 61 20  fer to a pragma 
27e08 74 68 61 74 20 71 75 65 72 69 65 73 0a 2a 2a 20  that queries.** 
27e09 6f 72 20 63 68 61 6e 67 65 73 20 6f 6e 65 20 6f  or changes one o
27e0a 66 20 74 68 65 20 66 6c 61 67 73 20 69 6e 20 64  f the flags in d
27e0b 62 2d 3e 66 6c 61 67 73 2e 20 20 52 65 74 75 72  b->flags.  Retur
27e0c 6e 20 31 20 69 66 20 73 6f 20 61 6e 64 20 30 20  n 1 if so and 0 
27e0d 69 66 20 6e 6f 74 2e 0a 2a 2a 20 41 6c 73 6f 2c  if not..** Also,
27e0e 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 70   implement the p
27e0f 72 61 67 6d 61 2e 0a 2a 2f 0a 73 74 61 74 69 63  ragma..*/.static
27e10 20 69 6e 74 20 66 6c 61 67 50 72 61 67 6d 61 28   int flagPragma(
27e11 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63  Parse *pParse, c
27e12 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65 66 74  onst char *zLeft
27e13 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52  , const char *zR
27e14 69 67 68 74 29 7b 0a 20 20 73 74 61 74 69 63 20  ight){.  static 
27e15 63 6f 6e 73 74 20 73 74 72 75 63 74 20 73 50 72  const struct sPr
27e16 61 67 6d 61 54 79 70 65 20 7b 0a 20 20 20 20 63  agmaType {.    c
27e17 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
27e18 3b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68  ;  /* Name of th
27e19 65 20 70 72 61 67 6d 61 20 2a 2f 0a 20 20 20 20  e pragma */.    
27e1a 69 6e 74 20 6d 61 73 6b 3b 20 20 20 20 20 20 20  int mask;       
27e1b 20 20 20 20 2f 2a 20 4d 61 73 6b 20 66 6f 72 20      /* Mask for 
27e1c 74 68 65 20 64 62 2d 3e 66 6c 61 67 73 20 76 61  the db->flags va
27e1d 6c 75 65 20 2a 2f 0a 20 20 7d 20 61 50 72 61 67  lue */.  } aPrag
27e1e 6d 61 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b 20 22  ma[] = {.    { "
27e1f 66 75 6c 6c 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65  full_column_name
27e20 73 22 2c 20 20 20 20 20 20 20 20 53 51 4c 49 54  s",        SQLIT
27e21 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73 20 20  E_FullColNames  
27e22 7d 2c 0a 20 20 20 20 7b 20 22 73 68 6f 72 74 5f  },.    { "short_
27e23 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 73 22 2c 20 20  column_names",  
27e24 20 20 20 20 20 53 51 4c 49 54 45 5f 53 68 6f 72       SQLITE_Shor
27e25 74 43 6f 6c 4e 61 6d 65 73 20 7d 2c 0a 20 20 20  tColNames },.   
27e26 20 7b 20 22 63 6f 75 6e 74 5f 63 68 61 6e 67 65   { "count_change
27e27 73 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 53  s",            S
27e28 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20  QLITE_CountRows 
27e29 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 65 6d      },.    { "em
27e2a 70 74 79 5f 72 65 73 75 6c 74 5f 63 61 6c 6c 62  pty_result_callb
27e2b 61 63 6b 73 22 2c 20 20 20 53 51 4c 49 54 45 5f  acks",   SQLITE_
27e2c 4e 75 6c 6c 43 61 6c 6c 62 61 63 6b 20 20 7d 2c  NullCallback  },
27e2d 0a 20 20 20 20 7b 20 22 6c 65 67 61 63 79 5f 66  .    { "legacy_f
27e2e 69 6c 65 5f 66 6f 72 6d 61 74 22 2c 20 20 20 20  ile_format",    
27e2f 20 20 20 53 51 4c 49 54 45 5f 4c 65 67 61 63 79     SQLITE_Legacy
27e30 46 69 6c 65 46 6d 74 20 7d 2c 0a 20 20 20 20 7b  FileFmt },.    {
27e31 20 22 66 75 6c 6c 66 73 79 6e 63 22 2c 20 20 20   "fullfsync",   
27e32 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c               SQL
27e33 49 54 45 5f 46 75 6c 6c 46 53 79 6e 63 20 20 20  ITE_FullFSync   
27e34 20 20 7d 2c 0a 20 20 20 20 7b 20 22 72 65 76 65    },.    { "reve
27e35 72 73 65 5f 75 6e 6f 72 64 65 72 65 64 5f 73 65  rse_unordered_se
27e36 6c 65 63 74 73 22 2c 20 53 51 4c 49 54 45 5f 52  lects", SQLITE_R
27e37 65 76 65 72 73 65 4f 72 64 65 72 20 20 7d 2c 0a  everseOrder  },.
27e38 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
27e39 42 55 47 0a 20 20 20 20 7b 20 22 73 71 6c 5f 74  BUG.    { "sql_t
27e3a 72 61 63 65 22 2c 20 20 20 20 20 20 20 20 20 20  race",          
27e3b 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 71 6c        SQLITE_Sql
27e3c 54 72 61 63 65 20 20 20 20 20 20 7d 2c 0a 20 20  Trace      },.  
27e3d 20 20 7b 20 22 76 64 62 65 5f 6c 69 73 74 69 6e    { "vdbe_listin
27e3e 67 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  g",             
27e3f 53 51 4c 49 54 45 5f 56 64 62 65 4c 69 73 74 69  SQLITE_VdbeListi
27e40 6e 67 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 76  ng   },.    { "v
27e41 64 62 65 5f 74 72 61 63 65 22 2c 20 20 20 20 20  dbe_trace",     
27e42 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
27e43 5f 56 64 62 65 54 72 61 63 65 20 20 20 20 20 7d  _VdbeTrace     }
27e44 2c 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66  ,.#endif.#ifndef
27e45 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45   SQLITE_OMIT_CHE
27e46 43 4b 0a 20 20 20 20 7b 20 22 69 67 6e 6f 72 65  CK.    { "ignore
27e47 5f 63 68 65 63 6b 5f 63 6f 6e 73 74 72 61 69 6e  _check_constrain
27e48 74 73 22 2c 20 53 51 4c 49 54 45 5f 49 67 6e 6f  ts", SQLITE_Igno
27e49 72 65 43 68 65 63 6b 73 20 20 7d 2c 0a 23 65 6e  reChecks  },.#en
27e4a 64 69 66 0a 20 20 20 20 2f 2a 20 54 68 65 20 66  dif.    /* The f
27e4b 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 56 45 52 59  ollowing is VERY
27e4c 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 2a 2f   experimental */
27e4d 0a 20 20 20 20 7b 20 22 77 72 69 74 61 62 6c 65  .    { "writable
27e4e 5f 73 63 68 65 6d 61 22 2c 20 20 20 20 20 20 20  _schema",       
27e4f 20 20 20 53 51 4c 49 54 45 5f 57 72 69 74 65 53     SQLITE_WriteS
27e50 63 68 65 6d 61 7c 53 51 4c 49 54 45 5f 52 65 63  chema|SQLITE_Rec
27e51 6f 76 65 72 79 4d 6f 64 65 20 7d 2c 0a 20 20 20  overyMode },.   
27e52 20 7b 20 22 6f 6d 69 74 5f 72 65 61 64 6c 6f 63   { "omit_readloc
27e53 6b 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 53  k",            S
27e54 51 4c 49 54 45 5f 4e 6f 52 65 61 64 6c 6f 63 6b  QLITE_NoReadlock
27e55 20 20 20 20 7d 2c 0a 0a 20 20 20 20 2f 2a 20 54      },..    /* T
27e56 4f 44 4f 3a 20 4d 61 79 62 65 20 69 74 20 73 68  ODO: Maybe it sh
27e57 6f 75 6c 64 6e 27 74 20 62 65 20 70 6f 73 73 69  ouldn't be possi
27e58 62 6c 65 20 74 6f 20 63 68 61 6e 67 65 20 74 68  ble to change th
27e59 65 20 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65  e ReadUncommitte
27e5a 64 0a 20 20 20 20 2a 2a 20 66 6c 61 67 20 69 66  d.    ** flag if
27e5b 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 61   there are any a
27e5c 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e 74 73  ctive statements
27e5d 2e 20 2a 2f 0a 20 20 20 20 7b 20 22 72 65 61 64  . */.    { "read
27e5e 5f 75 6e 63 6f 6d 6d 69 74 74 65 64 22 2c 20 20  _uncommitted",  
27e5f 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 52 65         SQLITE_Re
27e60 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20 7d 2c  adUncommitted },
27e61 0a 20 20 20 20 7b 20 22 72 65 63 75 72 73 69 76  .    { "recursiv
27e62 65 5f 74 72 69 67 67 65 72 73 22 2c 20 20 20 20  e_triggers",    
27e63 20 20 20 53 51 4c 49 54 45 5f 52 65 63 54 72 69     SQLITE_RecTri
27e64 67 67 65 72 73 20 7d 2c 0a 20 20 7d 3b 0a 20 20  ggers },.  };.  
27e65 69 6e 74 20 69 3b 0a 20 20 63 6f 6e 73 74 20 73  int i;.  const s
27e66 74 72 75 63 74 20 73 50 72 61 67 6d 61 54 79 70  truct sPragmaTyp
27e67 65 20 2a 70 3b 0a 20 20 66 6f 72 28 69 3d 30 2c  e *p;.  for(i=0,
27e68 20 70 3d 61 50 72 61 67 6d 61 3b 20 69 3c 41 72   p=aPragma; i<Ar
27e69 72 61 79 53 69 7a 65 28 61 50 72 61 67 6d 61 29  raySize(aPragma)
27e6a 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20  ; i++, p++){.   
27e6b 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
27e6c 43 6d 70 28 7a 4c 65 66 74 2c 20 70 2d 3e 7a 4e  Cmp(zLeft, p->zN
27e6d 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ame)==0 ){.     
27e6e 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
27e6f 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 20  Parse->db;.     
27e70 20 56 64 62 65 20 2a 76 3b 0a 20 20 20 20 20 20   Vdbe *v;.      
27e71 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
27e72 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  be(pParse);.    
27e73 20 20 61 73 73 65 72 74 28 20 76 21 3d 30 20 29    assert( v!=0 )
27e74 3b 20 20 2f 2a 20 41 6c 72 65 61 64 79 20 61 6c  ;  /* Already al
27e75 6c 6f 63 61 74 65 64 20 62 79 20 73 71 6c 69 74  located by sqlit
27e76 65 33 50 72 61 67 6d 61 28 29 20 2a 2f 0a 20 20  e3Pragma() */.  
27e77 20 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 76      if( ALWAYS(v
27e78 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ) ){.        if(
27e79 20 7a 52 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20   zRight==0 ){.  
27e7a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 53 69          returnSi
27e7b 6e 67 6c 65 49 6e 74 28 70 50 61 72 73 65 2c 20  ngleInt(pParse, 
27e7c 70 2d 3e 7a 4e 61 6d 65 2c 20 28 64 62 2d 3e 66  p->zName, (db->f
27e7d 6c 61 67 73 20 26 20 70 2d 3e 6d 61 73 6b 29 21  lags & p->mask)!
27e7e 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 7d 65  =0 );.        }e
27e7f 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 69  lse{.          i
27e80 66 28 20 67 65 74 42 6f 6f 6c 65 61 6e 28 7a 52  f( getBoolean(zR
27e81 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20 20  ight) ){.       
27e82 20 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c       db->flags |
27e83 3d 20 70 2d 3e 6d 61 73 6b 3b 0a 20 20 20 20 20  = p->mask;.     
27e84 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
27e85 20 20 20 20 20 20 20 20 64 62 2d 3e 66 6c 61 67          db->flag
27e86 73 20 26 3d 20 7e 70 2d 3e 6d 61 73 6b 3b 0a 20  s &= ~p->mask;. 
27e87 20 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20           }..    
27e88 20 20 20 20 20 20 2f 2a 20 4d 61 6e 79 20 6f 66        /* Many of
27e89 20 74 68 65 20 66 6c 61 67 2d 70 72 61 67 6d 61   the flag-pragma
27e8a 73 20 6d 6f 64 69 66 79 20 74 68 65 20 63 6f 64  s modify the cod
27e8b 65 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 74  e generated by t
27e8c 68 65 20 53 51 4c 20 0a 20 20 20 20 20 20 20 20  he SQL .        
27e8d 20 20 2a 2a 20 63 6f 6d 70 69 6c 65 72 20 28 65    ** compiler (e
27e8e 67 2e 20 63 6f 75 6e 74 5f 63 68 61 6e 67 65 73  g. count_changes
27e8f 29 2e 20 53 6f 20 61 64 64 20 61 6e 20 6f 70 63  ). So add an opc
27e90 6f 64 65 20 74 6f 20 65 78 70 69 72 65 20 61 6c  ode to expire al
27e91 6c 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63  l.          ** c
27e92 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 74 61 74  ompiled SQL stat
27e93 65 6d 65 6e 74 73 20 61 66 74 65 72 20 6d 6f 64  ements after mod
27e94 69 66 79 69 6e 67 20 61 20 70 72 61 67 6d 61 20  ifying a pragma 
27e95 76 61 6c 75 65 2e 0a 20 20 20 20 20 20 20 20 20  value..         
27e96 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73 71   */.          sq
27e97 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
27e98 76 2c 20 4f 50 5f 45 78 70 69 72 65 2c 20 30 2c  v, OP_Expire, 0,
27e99 20 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20   0);.        }. 
27e9a 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 72 65       }..      re
27e9b 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
27e9c 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
27e9d 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
27e9e 5f 4f 4d 49 54 5f 46 4c 41 47 5f 50 52 41 47 4d  _OMIT_FLAG_PRAGM
27e9f 41 53 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  AS */../*.** Ret
27ea0 75 72 6e 20 61 20 68 75 6d 61 6e 2d 72 65 61 64  urn a human-read
27ea1 61 62 6c 65 20 6e 61 6d 65 20 66 6f 72 20 61 20  able name for a 
27ea2 63 6f 6e 73 74 72 61 69 6e 74 20 72 65 73 6f 6c  constraint resol
27ea3 75 74 69 6f 6e 20 61 63 74 69 6f 6e 2e 0a 2a 2f  ution action..*/
27ea4 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68  .static const ch
27ea5 61 72 20 2a 61 63 74 69 6f 6e 4e 61 6d 65 28 75  ar *actionName(u
27ea6 38 20 61 63 74 69 6f 6e 29 7b 0a 20 20 63 6f 6e  8 action){.  con
27ea7 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a  st char *zName;.
27ea8 20 20 73 77 69 74 63 68 28 20 61 63 74 69 6f 6e    switch( action
27ea9 20 29 7b 0a 20 20 20 20 63 61 73 65 20 4f 45 5f   ){.    case OE_
27eaa 53 65 74 4e 75 6c 6c 3a 20 20 7a 4e 61 6d 65 20  SetNull:  zName 
27eab 3d 20 22 53 45 54 20 4e 55 4c 4c 22 3b 20 20 20  = "SET NULL";   
27eac 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
27ead 20 20 20 20 63 61 73 65 20 4f 45 5f 53 65 74 44      case OE_SetD
27eae 66 6c 74 3a 20 20 7a 4e 61 6d 65 20 3d 20 22 53  flt:  zName = "S
27eaf 45 54 20 44 45 46 41 55 4c 54 22 3b 20 20 20 20  ET DEFAULT";    
27eb0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
27eb1 63 61 73 65 20 4f 45 5f 43 61 73 63 61 64 65 3a  case OE_Cascade:
27eb2 20 20 7a 4e 61 6d 65 20 3d 20 22 43 41 53 43 41    zName = "CASCA
27eb3 44 45 22 3b 20 20 20 20 20 20 20 20 20 20 20 20  DE";            
27eb4 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65 66 61   break;.    defa
27eb5 75 6c 74 3a 20 20 20 20 20 20 20 20 20 20 7a 4e  ult:          zN
27eb6 61 6d 65 20 3d 20 22 52 45 53 54 52 49 43 54 22  ame = "RESTRICT"
27eb7 3b 20 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  ;  .            
27eb8 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
27eb9 28 20 61 63 74 69 6f 6e 3d 3d 4f 45 5f 52 65 73  ( action==OE_Res
27eba 74 72 69 63 74 20 29 3b 20 62 72 65 61 6b 3b 0a  trict ); break;.
27ebb 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4e 61    }.  return zNa
27ebc 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f  me;.}../*.** Pro
27ebd 63 65 73 73 20 61 20 70 72 61 67 6d 61 20 73 74  cess a pragma st
27ebe 61 74 65 6d 65 6e 74 2e 20 20 0a 2a 2a 0a 2a 2a  atement.  .**.**
27ebf 20 50 72 61 67 6d 61 73 20 61 72 65 20 6f 66 20   Pragmas are of 
27ec0 74 68 69 73 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a  this form:.**.**
27ec1 20 20 20 20 20 20 50 52 41 47 4d 41 20 5b 64 61        PRAGMA [da
27ec2 74 61 62 61 73 65 2e 5d 69 64 20 5b 3d 20 76 61  tabase.]id [= va
27ec3 6c 75 65 5d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69  lue].**.** The i
27ec4 64 65 6e 74 69 66 69 65 72 20 6d 69 67 68 74 20  dentifier might 
27ec5 61 6c 73 6f 20 62 65 20 61 20 73 74 72 69 6e 67  also be a string
27ec6 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 20  .  The value is 
27ec7 61 20 73 74 72 69 6e 67 2c 20 61 6e 64 0a 2a 2a  a string, and.**
27ec8 20 69 64 65 6e 74 69 66 69 65 72 2c 20 6f 72 20   identifier, or 
27ec9 61 20 6e 75 6d 62 65 72 2e 20 20 49 66 20 6d 69  a number.  If mi
27eca 6e 75 73 46 6c 61 67 20 69 73 20 74 72 75 65 2c  nusFlag is true,
27ecb 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20   then the value 
27ecc 69 73 0a 2a 2a 20 61 20 6e 75 6d 62 65 72 20 74  is.** a number t
27ecd 68 61 74 20 77 61 73 20 70 72 65 63 65 64 65 64  hat was preceded
27ece 20 62 79 20 61 20 6d 69 6e 75 73 20 73 69 67 6e   by a minus sign
27ecf 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c  ..**.** If the l
27ed0 65 66 74 20 73 69 64 65 20 69 73 20 22 64 61 74  eft side is "dat
27ed1 61 62 61 73 65 2e 69 64 22 20 74 68 65 6e 20 70  abase.id" then p
27ed2 49 64 31 20 69 73 20 74 68 65 20 64 61 74 61 62  Id1 is the datab
27ed3 61 73 65 20 6e 61 6d 65 0a 2a 2a 20 61 6e 64 20  ase name.** and 
27ed4 70 49 64 32 20 69 73 20 74 68 65 20 69 64 2e 20  pId2 is the id. 
27ed5 20 49 66 20 74 68 65 20 6c 65 66 74 20 73 69 64   If the left sid
27ed6 65 20 69 73 20 6a 75 73 74 20 22 69 64 22 20 74  e is just "id" t
27ed7 68 65 6e 20 70 49 64 31 20 69 73 20 74 68 65 0a  hen pId1 is the.
27ed8 2a 2a 20 69 64 20 61 6e 64 20 70 49 64 32 20 69  ** id and pId2 i
27ed9 73 20 61 6e 79 20 65 6d 70 74 79 20 73 74 72 69  s any empty stri
27eda 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ng..*/.SQLITE_PR
27edb 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
27edc 65 33 50 72 61 67 6d 61 28 0a 20 20 50 61 72 73  e3Pragma(.  Pars
27edd 65 20 2a 70 50 61 72 73 65 2c 20 0a 20 20 54 6f  e *pParse, .  To
27ede 6b 65 6e 20 2a 70 49 64 31 2c 20 20 20 20 20 20  ken *pId1,      
27edf 20 20 2f 2a 20 46 69 72 73 74 20 70 61 72 74 20    /* First part 
27ee0 6f 66 20 5b 64 61 74 61 62 61 73 65 2e 5d 69 64  of [database.]id
27ee1 20 66 69 65 6c 64 20 2a 2f 0a 20 20 54 6f 6b 65   field */.  Toke
27ee2 6e 20 2a 70 49 64 32 2c 20 20 20 20 20 20 20 20  n *pId2,        
27ee3 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 74 20 6f  /* Second part o
27ee4 66 20 5b 64 61 74 61 62 61 73 65 2e 5d 69 64 20  f [database.]id 
27ee5 66 69 65 6c 64 2c 20 6f 72 20 4e 55 4c 4c 20 2a  field, or NULL *
27ee6 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 56 61 6c 75  /.  Token *pValu
27ee7 65 2c 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e  e,      /* Token
27ee8 20 66 6f 72 20 3c 76 61 6c 75 65 3e 2c 20 6f 72   for <value>, or
27ee9 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 6d   NULL */.  int m
27eea 69 6e 75 73 46 6c 61 67 20 20 20 20 20 20 20 2f  inusFlag       /
27eeb 2a 20 54 72 75 65 20 69 66 20 61 20 27 2d 27 20  * True if a '-' 
27eec 73 69 67 6e 20 70 72 65 63 65 64 65 64 20 3c 76  sign preceded <v
27eed 61 6c 75 65 3e 20 2a 2f 0a 29 7b 0a 20 20 63 68  alue> */.){.  ch
27eee 61 72 20 2a 7a 4c 65 66 74 20 3d 20 30 3b 20 20  ar *zLeft = 0;  
27eef 20 20 20 20 20 2f 2a 20 4e 75 6c 2d 74 65 72 6d       /* Nul-term
27ef0 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74 72  inated UTF-8 str
27ef1 69 6e 67 20 3c 69 64 3e 20 2a 2f 0a 20 20 63 68  ing <id> */.  ch
27ef2 61 72 20 2a 7a 52 69 67 68 74 20 3d 20 30 3b 20  ar *zRight = 0; 
27ef3 20 20 20 20 20 2f 2a 20 4e 75 6c 2d 74 65 72 6d       /* Nul-term
27ef4 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74 72  inated UTF-8 str
27ef5 69 6e 67 20 3c 76 61 6c 75 65 3e 2c 20 6f 72 20  ing <value>, or 
27ef6 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  NULL */.  const 
27ef7 63 68 61 72 20 2a 7a 44 62 20 3d 20 30 3b 20 20  char *zDb = 0;  
27ef8 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
27ef9 20 6e 61 6d 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e   name */.  Token
27efa 20 2a 70 49 64 3b 20 20 20 20 20 20 20 20 20 20   *pId;          
27efb 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
27efc 3c 69 64 3e 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20  <id> token */.  
27efd 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20  int iDb;        
27efe 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
27eff 73 65 20 69 6e 64 65 78 20 66 6f 72 20 3c 64 61  se index for <da
27f00 74 61 62 61 73 65 3e 20 2a 2f 0a 20 20 73 71 6c  tabase> */.  sql
27f01 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
27f02 65 2d 3e 64 62 3b 0a 20 20 44 62 20 2a 70 44 62  e->db;.  Db *pDb
27f03 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  ;.  Vdbe *v = pP
27f04 61 72 73 65 2d 3e 70 56 64 62 65 20 3d 20 73 71  arse->pVdbe = sq
27f05 6c 69 74 65 33 56 64 62 65 43 72 65 61 74 65 28  lite3VdbeCreate(
27f06 64 62 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20  db);.  if( v==0 
27f07 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 50 61 72  ) return;.  pPar
27f08 73 65 2d 3e 6e 4d 65 6d 20 3d 20 32 3b 0a 0a 20  se->nMem = 2;.. 
27f09 20 2f 2a 20 49 6e 74 65 72 70 72 65 74 20 74 68   /* Interpret th
27f0a 65 20 5b 64 61 74 61 62 61 73 65 2e 5d 20 70 61  e [database.] pa
27f0b 72 74 20 6f 66 20 74 68 65 20 70 72 61 67 6d 61  rt of the pragma
27f0c 20 73 74 61 74 65 6d 65 6e 74 2e 20 69 44 62 20   statement. iDb 
27f0d 69 73 20 74 68 65 0a 20 20 2a 2a 20 69 6e 64 65  is the.  ** inde
27f0e 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  x of the databas
27f0f 65 20 74 68 69 73 20 70 72 61 67 6d 61 20 69 73  e this pragma is
27f10 20 62 65 69 6e 67 20 61 70 70 6c 69 65 64 20 74   being applied t
27f11 6f 20 69 6e 20 64 62 2e 61 44 62 5b 5d 2e 20 2a  o in db.aDb[]. *
27f12 2f 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65  /.  iDb = sqlite
27f13 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 70 50 61  3TwoPartName(pPa
27f14 72 73 65 2c 20 70 49 64 31 2c 20 70 49 64 32 2c  rse, pId1, pId2,
27f15 20 26 70 49 64 29 3b 0a 20 20 69 66 28 20 69 44   &pId);.  if( iD
27f16 62 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  b<0 ) return;.  
27f17 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69  pDb = &db->aDb[i
27f18 44 62 5d 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  Db];..  /* If th
27f19 65 20 74 65 6d 70 20 64 61 74 61 62 61 73 65 20  e temp database 
27f1a 68 61 73 20 62 65 65 6e 20 65 78 70 6c 69 63 69  has been explici
27f1b 74 6c 79 20 6e 61 6d 65 64 20 61 73 20 70 61 72  tly named as par
27f1c 74 20 6f 66 20 74 68 65 20 0a 20 20 2a 2a 20 70  t of the .  ** p
27f1d 72 61 67 6d 61 2c 20 6d 61 6b 65 20 73 75 72 65  ragma, make sure
27f1e 20 69 74 20 69 73 20 6f 70 65 6e 2e 20 0a 20 20   it is open. .  
27f1f 2a 2f 0a 20 20 69 66 28 20 69 44 62 3d 3d 31 20  */.  if( iDb==1 
27f20 26 26 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 65  && sqlite3OpenTe
27f21 6d 70 44 61 74 61 62 61 73 65 28 70 50 61 72 73  mpDatabase(pPars
27f22 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  e) ){.    return
27f23 3b 0a 20 20 7d 0a 0a 20 20 7a 4c 65 66 74 20 3d  ;.  }..  zLeft =
27f24 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d   sqlite3NameFrom
27f25 54 6f 6b 65 6e 28 64 62 2c 20 70 49 64 29 3b 0a  Token(db, pId);.
27f26 20 20 69 66 28 20 21 7a 4c 65 66 74 20 29 20 72    if( !zLeft ) r
27f27 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 6d 69 6e  eturn;.  if( min
27f28 75 73 46 6c 61 67 20 29 7b 0a 20 20 20 20 7a 52  usFlag ){.    zR
27f29 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33 4d 50  ight = sqlite3MP
27f2a 72 69 6e 74 66 28 64 62 2c 20 22 2d 25 54 22 2c  rintf(db, "-%T",
27f2b 20 70 56 61 6c 75 65 29 3b 0a 20 20 7d 65 6c 73   pValue);.  }els
27f2c 65 7b 0a 20 20 20 20 7a 52 69 67 68 74 20 3d 20  e{.    zRight = 
27f2d 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54  sqlite3NameFromT
27f2e 6f 6b 65 6e 28 64 62 2c 20 70 56 61 6c 75 65 29  oken(db, pValue)
27f2f 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28  ;.  }..  assert(
27f30 20 70 49 64 32 20 29 3b 0a 20 20 7a 44 62 20 3d   pId2 );.  zDb =
27f31 20 70 49 64 32 2d 3e 6e 3e 30 20 3f 20 70 44 62   pId2->n>0 ? pDb
27f32 2d 3e 7a 4e 61 6d 65 20 3a 20 30 3b 0a 20 20 69  ->zName : 0;.  i
27f33 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  f( sqlite3AuthCh
27f34 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49  eck(pParse, SQLI
27f35 54 45 5f 50 52 41 47 4d 41 2c 20 7a 4c 65 66 74  TE_PRAGMA, zLeft
27f36 2c 20 7a 52 69 67 68 74 2c 20 7a 44 62 29 20 29  , zRight, zDb) )
27f37 7b 0a 20 20 20 20 67 6f 74 6f 20 70 72 61 67 6d  {.    goto pragm
27f38 61 5f 6f 75 74 3b 0a 20 20 7d 0a 20 0a 23 69 66  a_out;.  }. .#if
27f39 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
27f3a 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 20  _PAGER_PRAGMAS. 
27f3b 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41   /*.  **  PRAGMA
27f3c 20 5b 64 61 74 61 62 61 73 65 2e 5d 64 65 66 61   [database.]defa
27f3d 75 6c 74 5f 63 61 63 68 65 5f 73 69 7a 65 0a 20  ult_cache_size. 
27f3e 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74   **  PRAGMA [dat
27f3f 61 62 61 73 65 2e 5d 64 65 66 61 75 6c 74 5f 63  abase.]default_c
27f40 61 63 68 65 5f 73 69 7a 65 3d 4e 0a 20 20 2a 2a  ache_size=N.  **
27f41 0a 20 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20  .  ** The first 
27f42 66 6f 72 6d 20 72 65 70 6f 72 74 73 20 74 68 65  form reports the
27f43 20 63 75 72 72 65 6e 74 20 70 65 72 73 69 73 74   current persist
27f44 65 6e 74 20 73 65 74 74 69 6e 67 20 66 6f 72 20  ent setting for 
27f45 74 68 65 0a 20 20 2a 2a 20 70 61 67 65 20 63 61  the.  ** page ca
27f46 63 68 65 20 73 69 7a 65 2e 20 20 54 68 65 20 76  che size.  The v
27f47 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73  alue returned is
27f48 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d   the maximum num
27f49 62 65 72 20 6f 66 0a 20 20 2a 2a 20 70 61 67 65  ber of.  ** page
27f4a 73 20 69 6e 20 74 68 65 20 70 61 67 65 20 63 61  s in the page ca
27f4b 63 68 65 2e 20 20 54 68 65 20 73 65 63 6f 6e 64  che.  The second
27f4c 20 66 6f 72 6d 20 73 65 74 73 20 62 6f 74 68 20   form sets both 
27f4d 74 68 65 20 63 75 72 72 65 6e 74 0a 20 20 2a 2a  the current.  **
27f4e 20 70 61 67 65 20 63 61 63 68 65 20 73 69 7a 65   page cache size
27f4f 20 76 61 6c 75 65 20 61 6e 64 20 74 68 65 20 70   value and the p
27f50 65 72 73 69 73 74 65 6e 74 20 70 61 67 65 20 63  ersistent page c
27f51 61 63 68 65 20 73 69 7a 65 20 76 61 6c 75 65 0a  ache size value.
27f52 20 20 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 74    ** stored in t
27f53 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
27f54 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  ..  **.  ** The 
27f55 64 65 66 61 75 6c 74 20 63 61 63 68 65 20 73 69  default cache si
27f56 7a 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  ze is stored in 
27f57 6d 65 74 61 2d 76 61 6c 75 65 20 32 20 6f 66 20  meta-value 2 of 
27f58 70 61 67 65 20 31 20 6f 66 20 74 68 65 0a 20 20  page 1 of the.  
27f59 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ** database file
27f5a 2e 20 20 54 68 65 20 63 61 63 68 65 20 73 69 7a  .  The cache siz
27f5b 65 20 69 73 20 61 63 74 75 61 6c 6c 79 20 74 68  e is actually th
27f5c 65 20 61 62 73 6f 6c 75 74 65 20 76 61 6c 75 65  e absolute value
27f5d 20 6f 66 0a 20 20 2a 2a 20 74 68 69 73 20 6d 65   of.  ** this me
27f5e 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 2e 20 20  mory location.  
27f5f 54 68 65 20 73 69 67 6e 20 6f 66 20 6d 65 74 61  The sign of meta
27f60 2d 76 61 6c 75 65 20 32 20 64 65 74 65 72 6d 69  -value 2 determi
27f61 6e 65 73 20 74 68 65 0a 20 20 2a 2a 20 73 79 6e  nes the.  ** syn
27f62 63 68 72 6f 6e 6f 75 73 20 73 65 74 74 69 6e 67  chronous setting
27f63 2e 20 20 41 20 6e 65 67 61 74 69 76 65 20 76 61  .  A negative va
27f64 6c 75 65 20 6d 65 61 6e 73 20 73 79 6e 63 68 72  lue means synchr
27f65 6f 6e 6f 75 73 20 69 73 20 6f 66 66 0a 20 20 2a  onous is off.  *
27f66 2a 20 61 6e 64 20 61 20 70 6f 73 69 74 69 76 65  * and a positive
27f67 20 76 61 6c 75 65 20 6d 65 61 6e 73 20 73 79 6e   value means syn
27f68 63 68 72 6f 6e 6f 75 73 20 69 73 20 6f 6e 2e 0a  chronous is on..
27f69 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
27f6a 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
27f6b 22 64 65 66 61 75 6c 74 5f 63 61 63 68 65 5f 73  "default_cache_s
27f6c 69 7a 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ize")==0 ){.    
27f6d 73 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62  static const Vdb
27f6e 65 4f 70 4c 69 73 74 20 67 65 74 43 61 63 68 65  eOpList getCache
27f6f 53 69 7a 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 20  Size[] = {.     
27f70 20 7b 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f   { OP_Transactio
27f71 6e 2c 20 30 2c 20 30 2c 20 20 20 20 20 20 20 20  n, 0, 0,        
27f72 30 7d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  0},             
27f73 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30              /* 0
27f74 20 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f 52   */.      { OP_R
27f75 65 61 64 43 6f 6f 6b 69 65 2c 20 20 30 2c 20 31  eadCookie,  0, 1
27f76 2c 20 20 20 20 20 20 20 20 42 54 52 45 45 5f 44  ,        BTREE_D
27f77 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a  EFAULT_CACHE_SIZ
27f78 45 7d 2c 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20  E},  /* 1 */.   
27f79 20 20 20 7b 20 4f 50 5f 49 66 50 6f 73 2c 20 20     { OP_IfPos,  
27f7a 20 20 20 20 20 31 2c 20 37 2c 20 20 20 20 20 20       1, 7,      
27f7b 20 20 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50    0},.      { OP
27f7c 5f 49 6e 74 65 67 65 72 2c 20 20 20 20 20 30 2c  _Integer,     0,
27f7d 20 32 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a 20   2,        0},. 
27f7e 20 20 20 20 20 7b 20 4f 50 5f 53 75 62 74 72 61       { OP_Subtra
27f7f 63 74 2c 20 20 20 20 31 2c 20 32 2c 20 20 20 20  ct,    1, 2,    
27f80 20 20 20 20 31 7d 2c 0a 20 20 20 20 20 20 7b 20      1},.      { 
27f81 4f 50 5f 49 66 50 6f 73 2c 20 20 20 20 20 20 20  OP_IfPos,       
27f82 31 2c 20 37 2c 20 20 20 20 20 20 20 20 30 7d 2c  1, 7,        0},
27f83 0a 20 20 20 20 20 20 7b 20 4f 50 5f 49 6e 74 65  .      { OP_Inte
27f84 67 65 72 2c 20 20 20 20 20 30 2c 20 31 2c 20 20  ger,     0, 1,  
27f85 20 20 20 20 20 20 30 7d 2c 20 20 20 20 20 20 20        0},       
27f86 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27f87 20 20 2f 2a 20 36 20 2a 2f 0a 20 20 20 20 20 20    /* 6 */.      
27f88 7b 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20  { OP_ResultRow, 
27f89 20 20 31 2c 20 31 2c 20 20 20 20 20 20 20 20 30    1, 1,        0
27f8a 7d 2c 0a 20 20 20 20 7d 3b 0a 20 20 20 20 69 6e  },.    };.    in
27f8b 74 20 61 64 64 72 3b 0a 20 20 20 20 69 66 28 20  t addr;.    if( 
27f8c 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d  sqlite3ReadSchem
27f8d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f  a(pParse) ) goto
27f8e 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20   pragma_out;.   
27f8f 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73   sqlite3VdbeUses
27f90 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0a 20  Btree(v, iDb);. 
27f91 20 20 20 69 66 28 20 21 7a 52 69 67 68 74 20 29     if( !zRight )
27f92 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
27f93 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c  dbeSetNumCols(v,
27f94 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   1);.      sqlit
27f95 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
27f96 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 0, COLNAME_N
27f97 41 4d 45 2c 20 22 63 61 63 68 65 5f 73 69 7a 65  AME, "cache_size
27f98 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
27f99 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  );.      pParse-
27f9a 3e 6e 4d 65 6d 20 2b 3d 20 32 3b 0a 20 20 20 20  >nMem += 2;.    
27f9b 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33    addr = sqlite3
27f9c 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76 2c  VdbeAddOpList(v,
27f9d 20 41 72 72 61 79 53 69 7a 65 28 67 65 74 43 61   ArraySize(getCa
27f9e 63 68 65 53 69 7a 65 29 2c 20 67 65 74 43 61 63  cheSize), getCac
27f9f 68 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 73  heSize);.      s
27fa0 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
27fa1 50 31 28 76 2c 20 61 64 64 72 2c 20 69 44 62 29  P1(v, addr, iDb)
27fa2 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
27fa3 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61  dbeChangeP1(v, a
27fa4 64 64 72 2b 31 2c 20 69 44 62 29 3b 0a 20 20 20  ddr+1, iDb);.   
27fa5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
27fa6 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2b 36  angeP1(v, addr+6
27fa7 2c 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  , SQLITE_DEFAULT
27fa8 5f 43 41 43 48 45 5f 53 49 5a 45 29 3b 0a 20 20  _CACHE_SIZE);.  
27fa9 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
27faa 6e 74 20 73 69 7a 65 20 3d 20 61 74 6f 69 28 7a  nt size = atoi(z
27fab 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20 69 66  Right);.      if
27fac 28 20 73 69 7a 65 3c 30 20 29 20 73 69 7a 65 20  ( size<0 ) size 
27fad 3d 20 2d 73 69 7a 65 3b 0a 20 20 20 20 20 20 73  = -size;.      s
27fae 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65  qlite3BeginWrite
27faf 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65  Operation(pParse
27fb0 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 20 20 20  , 0, iDb);.     
27fb1 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27fb2 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
27fb3 2c 20 73 69 7a 65 2c 20 31 29 3b 0a 20 20 20 20  , size, 1);.    
27fb4 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27fb5 4f 70 33 28 76 2c 20 4f 50 5f 52 65 61 64 43 6f  Op3(v, OP_ReadCo
27fb6 6f 6b 69 65 2c 20 69 44 62 2c 20 32 2c 20 42 54  okie, iDb, 2, BT
27fb7 52 45 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48  REE_DEFAULT_CACH
27fb8 45 5f 53 49 5a 45 29 3b 0a 20 20 20 20 20 20 61  E_SIZE);.      a
27fb9 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
27fba 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66  eAddOp2(v, OP_If
27fbb 50 6f 73 2c 20 32 2c 20 30 29 3b 0a 20 20 20 20  Pos, 2, 0);.    
27fbc 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27fbd 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
27fbe 72 2c 20 2d 73 69 7a 65 2c 20 31 29 3b 0a 20 20  r, -size, 1);.  
27fbf 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
27fc0 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 29  umpHere(v, addr)
27fc1 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
27fc2 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
27fc3 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20  SetCookie, iDb, 
27fc4 42 54 52 45 45 5f 44 45 46 41 55 4c 54 5f 43 41  BTREE_DEFAULT_CA
27fc5 43 48 45 5f 53 49 5a 45 2c 20 31 29 3b 0a 20 20  CHE_SIZE, 1);.  
27fc6 20 20 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61      pDb->pSchema
27fc7 2d 3e 63 61 63 68 65 5f 73 69 7a 65 20 3d 20 73  ->cache_size = s
27fc8 69 7a 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ize;.      sqlit
27fc9 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65 53  e3BtreeSetCacheS
27fca 69 7a 65 28 70 44 62 2d 3e 70 42 74 2c 20 70 44  ize(pDb->pBt, pD
27fcb 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63 68  b->pSchema->cach
27fcc 65 5f 73 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 20  e_size);.    }. 
27fcd 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a   }else..  /*.  *
27fce 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62  *  PRAGMA [datab
27fcf 61 73 65 2e 5d 70 61 67 65 5f 73 69 7a 65 0a 20  ase.]page_size. 
27fd0 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74   **  PRAGMA [dat
27fd1 61 62 61 73 65 2e 5d 70 61 67 65 5f 73 69 7a 65  abase.]page_size
27fd2 3d 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  =N.  **.  ** The
27fd3 20 66 69 72 73 74 20 66 6f 72 6d 20 72 65 70 6f   first form repo
27fd4 72 74 73 20 74 68 65 20 63 75 72 72 65 6e 74 20  rts the current 
27fd5 73 65 74 74 69 6e 67 20 66 6f 72 20 74 68 65 0a  setting for the.
27fd6 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 70 61    ** database pa
27fd7 67 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73  ge size in bytes
27fd8 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 66 6f  .  The second fo
27fd9 72 6d 20 73 65 74 73 20 74 68 65 0a 20 20 2a 2a  rm sets the.  **
27fda 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 73   database page s
27fdb 69 7a 65 20 76 61 6c 75 65 2e 20 20 54 68 65 20  ize value.  The 
27fdc 76 61 6c 75 65 20 63 61 6e 20 6f 6e 6c 79 20 62  value can only b
27fdd 65 20 73 65 74 20 69 66 0a 20 20 2a 2a 20 74 68  e set if.  ** th
27fde 65 20 64 61 74 61 62 61 73 65 20 68 61 73 20 6e  e database has n
27fdf 6f 74 20 79 65 74 20 62 65 65 6e 20 63 72 65 61  ot yet been crea
27fe0 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ted..  */.  if( 
27fe1 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
27fe2 4c 65 66 74 2c 22 70 61 67 65 5f 73 69 7a 65 22  Left,"page_size"
27fe3 29 3d 3d 30 20 29 7b 0a 20 20 20 20 42 74 72 65  )==0 ){.    Btre
27fe4 65 20 2a 70 42 74 20 3d 20 70 44 62 2d 3e 70 42  e *pBt = pDb->pB
27fe5 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  t;.    assert( p
27fe6 42 74 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28  Bt!=0 );.    if(
27fe7 20 21 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20   !zRight ){.    
27fe8 20 20 69 6e 74 20 73 69 7a 65 20 3d 20 41 4c 57    int size = ALW
27fe9 41 59 53 28 70 42 74 29 20 3f 20 73 71 6c 69 74  AYS(pBt) ? sqlit
27fea 65 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69  e3BtreeGetPageSi
27feb 7a 65 28 70 42 74 29 20 3a 20 30 3b 0a 20 20 20  ze(pBt) : 0;.   
27fec 20 20 20 72 65 74 75 72 6e 53 69 6e 67 6c 65 49     returnSingleI
27fed 6e 74 28 70 50 61 72 73 65 2c 20 22 70 61 67 65  nt(pParse, "page
27fee 5f 73 69 7a 65 22 2c 20 73 69 7a 65 29 3b 0a 20  _size", size);. 
27fef 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
27ff0 2f 2a 20 4d 61 6c 6c 6f 63 20 6d 61 79 20 66 61  /* Malloc may fa
27ff1 69 6c 20 77 68 65 6e 20 73 65 74 74 69 6e 67 20  il when setting 
27ff2 74 68 65 20 70 61 67 65 2d 73 69 7a 65 2c 20 61  the page-size, a
27ff3 73 20 74 68 65 72 65 20 69 73 20 61 6e 20 69 6e  s there is an in
27ff4 74 65 72 6e 61 6c 0a 20 20 20 20 20 20 2a 2a 20  ternal.      ** 
27ff5 62 75 66 66 65 72 20 74 68 61 74 20 74 68 65 20  buffer that the 
27ff6 70 61 67 65 72 20 6d 6f 64 75 6c 65 20 72 65 73  pager module res
27ff7 69 7a 65 73 20 75 73 69 6e 67 20 73 71 6c 69 74  izes using sqlit
27ff8 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a 20 20  e3_realloc()..  
27ff9 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 62 2d      */.      db-
27ffa 3e 6e 65 78 74 50 61 67 65 73 69 7a 65 20 3d 20  >nextPagesize = 
27ffb 61 74 6f 69 28 7a 52 69 67 68 74 29 3b 0a 20 20  atoi(zRight);.  
27ffc 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e      if( SQLITE_N
27ffd 4f 4d 45 4d 3d 3d 73 71 6c 69 74 65 33 42 74 72  OMEM==sqlite3Btr
27ffe 65 65 53 65 74 50 61 67 65 53 69 7a 65 28 70 42  eeSetPageSize(pB
27fff 74 2c 20 64 62 2d 3e 6e 65 78 74 50 61 67 65 73  t, db->nextPages
28000 69 7a 65 2c 20 2d 31 2c 20 30 29 20 29 7b 0a 20  ize, -1, 0) ){. 
28001 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f         db->mallo
28002 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
28003 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c     }.    }.  }el
28004 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50  se..  /*.  **  P
28005 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
28006 5d 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 0a  ]max_page_count.
28007 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61    **  PRAGMA [da
28008 74 61 62 61 73 65 2e 5d 6d 61 78 5f 70 61 67 65  tabase.]max_page
28009 5f 63 6f 75 6e 74 3d 4e 0a 20 20 2a 2a 0a 20 20  _count=N.  **.  
2800a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 66 6f 72  ** The first for
2800b 6d 20 72 65 70 6f 72 74 73 20 74 68 65 20 63 75  m reports the cu
2800c 72 72 65 6e 74 20 73 65 74 74 69 6e 67 20 66 6f  rrent setting fo
2800d 72 20 74 68 65 0a 20 20 2a 2a 20 6d 61 78 69 6d  r the.  ** maxim
2800e 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  um number of pag
2800f 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  es in the databa
28010 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20 0a 20  se file.  The . 
28011 20 2a 2a 20 73 65 63 6f 6e 64 20 66 6f 72 6d 20   ** second form 
28012 61 74 74 65 6d 70 74 73 20 74 6f 20 63 68 61 6e  attempts to chan
28013 67 65 20 74 68 69 73 20 73 65 74 74 69 6e 67 2e  ge this setting.
28014 20 20 42 6f 74 68 0a 20 20 2a 2a 20 66 6f 72 6d    Both.  ** form
28015 73 20 72 65 74 75 72 6e 20 74 68 65 20 63 75 72  s return the cur
28016 72 65 6e 74 20 73 65 74 74 69 6e 67 2e 0a 20 20  rent setting..  
28017 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
28018 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 6d  StrICmp(zLeft,"m
28019 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 22 29 3d  ax_page_count")=
2801a 3d 30 20 29 7b 0a 20 20 20 20 42 74 72 65 65 20  =0 ){.    Btree 
2801b 2a 70 42 74 20 3d 20 70 44 62 2d 3e 70 42 74 3b  *pBt = pDb->pBt;
2801c 0a 20 20 20 20 69 6e 74 20 6e 65 77 4d 61 78 20  .    int newMax 
2801d 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28  = 0;.    assert(
2801e 20 70 42 74 21 3d 30 20 29 3b 0a 20 20 20 20 69   pBt!=0 );.    i
2801f 66 28 20 7a 52 69 67 68 74 20 29 7b 0a 20 20 20  f( zRight ){.   
28020 20 20 20 6e 65 77 4d 61 78 20 3d 20 61 74 6f 69     newMax = atoi
28021 28 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a  (zRight);.    }.
28022 20 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 70      if( ALWAYS(p
28023 42 74 29 20 29 7b 0a 20 20 20 20 20 20 6e 65 77  Bt) ){.      new
28024 4d 61 78 20 3d 20 73 71 6c 69 74 65 33 42 74 72  Max = sqlite3Btr
28025 65 65 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 70  eeMaxPageCount(p
28026 42 74 2c 20 6e 65 77 4d 61 78 29 3b 0a 20 20 20  Bt, newMax);.   
28027 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 53 69 6e   }.    returnSin
28028 67 6c 65 49 6e 74 28 70 50 61 72 73 65 2c 20 22  gleInt(pParse, "
28029 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 22 2c  max_page_count",
2802a 20 6e 65 77 4d 61 78 29 3b 0a 20 20 7d 65 6c 73   newMax);.  }els
2802b 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52  e..  /*.  **  PR
2802c 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d  AGMA [database.]
2802d 70 61 67 65 5f 63 6f 75 6e 74 0a 20 20 2a 2a 0a  page_count.  **.
2802e 20 20 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20    ** Return the 
2802f 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
28030 69 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64  in the specified
28031 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a   database..  */.
28032 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
28033 49 43 6d 70 28 7a 4c 65 66 74 2c 22 70 61 67 65  ICmp(zLeft,"page
28034 5f 63 6f 75 6e 74 22 29 3d 3d 30 20 29 7b 0a 20  _count")==0 ){. 
28035 20 20 20 69 6e 74 20 69 52 65 67 3b 0a 20 20 20     int iReg;.   
28036 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 61 64   if( sqlite3Read
28037 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29  Schema(pParse) )
28038 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74   goto pragma_out
28039 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64  ;.    sqlite3Cod
2803a 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 70 50  eVerifySchema(pP
2803b 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20  arse, iDb);.    
2803c 69 52 65 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d  iReg = ++pParse-
2803d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74  >nMem;.    sqlit
2803e 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2803f 4f 50 5f 50 61 67 65 63 6f 75 6e 74 2c 20 69 44  OP_Pagecount, iD
28040 62 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 73 71  b, iReg);.    sq
28041 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
28042 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c  v, OP_ResultRow,
28043 20 69 52 65 67 2c 20 31 29 3b 0a 20 20 20 20 73   iReg, 1);.    s
28044 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
28045 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20  Cols(v, 1);.    
28046 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
28047 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
28048 41 4d 45 5f 4e 41 4d 45 2c 20 22 70 61 67 65 5f  AME_NAME, "page_
28049 63 6f 75 6e 74 22 2c 20 53 51 4c 49 54 45 5f 53  count", SQLITE_S
2804a 54 41 54 49 43 29 3b 0a 20 20 7d 65 6c 73 65 0a  TATIC);.  }else.
2804b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47  .  /*.  **  PRAG
2804c 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f  MA [database.]lo
2804d 63 6b 69 6e 67 5f 6d 6f 64 65 0a 20 20 2a 2a 20  cking_mode.  ** 
2804e 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
2804f 65 2e 5d 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 20  e.]locking_mode 
28050 3d 20 28 6e 6f 72 6d 61 6c 7c 65 78 63 6c 75 73  = (normal|exclus
28051 69 76 65 29 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ive).  */.  if( 
28052 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
28053 4c 65 66 74 2c 22 6c 6f 63 6b 69 6e 67 5f 6d 6f  Left,"locking_mo
28054 64 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63  de")==0 ){.    c
28055 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 65 74 20  onst char *zRet 
28056 3d 20 22 6e 6f 72 6d 61 6c 22 3b 0a 20 20 20 20  = "normal";.    
28057 69 6e 74 20 65 4d 6f 64 65 20 3d 20 67 65 74 4c  int eMode = getL
28058 6f 63 6b 69 6e 67 4d 6f 64 65 28 7a 52 69 67 68  ockingMode(zRigh
28059 74 29 3b 0a 0a 20 20 20 20 69 66 28 20 70 49 64  t);..    if( pId
2805a 32 2d 3e 6e 3d 3d 30 20 26 26 20 65 4d 6f 64 65  2->n==0 && eMode
2805b 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d  ==PAGER_LOCKINGM
2805c 4f 44 45 5f 51 55 45 52 59 20 29 7b 0a 20 20 20  ODE_QUERY ){.   
2805d 20 20 20 2f 2a 20 53 69 6d 70 6c 65 20 22 50 52     /* Simple "PR
2805e 41 47 4d 41 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64  AGMA locking_mod
2805f 65 3b 22 20 73 74 61 74 65 6d 65 6e 74 2e 20 54  e;" statement. T
28060 68 69 73 20 69 73 20 61 20 71 75 65 72 79 20 66  his is a query f
28061 6f 72 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  or.      ** the 
28062 63 75 72 72 65 6e 74 20 64 65 66 61 75 6c 74 20  current default 
28063 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 28 77 68  locking mode (wh
28064 69 63 68 20 6d 61 79 20 62 65 20 64 69 66 66 65  ich may be diffe
28065 72 65 6e 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a  rent to.      **
28066 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64   the locking-mod
28067 65 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 64 61  e of the main da
28068 74 61 62 61 73 65 29 2e 0a 20 20 20 20 20 20 2a  tabase)..      *
28069 2f 0a 20 20 20 20 20 20 65 4d 6f 64 65 20 3d 20  /.      eMode = 
2806a 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65  db->dfltLockMode
2806b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
2806c 20 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72     Pager *pPager
2806d 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 64 32  ;.      if( pId2
2806e 2d 3e 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ->n==0 ){.      
2806f 20 20 2f 2a 20 54 68 69 73 20 69 6e 64 69 63 61    /* This indica
28070 74 65 73 20 74 68 61 74 20 6e 6f 20 64 61 74 61  tes that no data
28071 62 61 73 65 20 6e 61 6d 65 20 77 61 73 20 73 70  base name was sp
28072 65 63 69 66 69 65 64 20 61 73 20 70 61 72 74 0a  ecified as part.
28073 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68          ** of th
28074 65 20 50 52 41 47 4d 41 20 63 6f 6d 6d 61 6e 64  e PRAGMA command
28075 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74  . In this case t
28076 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20  he locking-mode 
28077 6d 75 73 74 20 62 65 0a 20 20 20 20 20 20 20 20  must be.        
28078 2a 2a 20 73 65 74 20 6f 6e 20 61 6c 6c 20 61 74  ** set on all at
28079 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73  tached databases
2807a 2c 20 61 73 20 77 65 6c 6c 20 61 73 20 74 68 65  , as well as the
2807b 20 6d 61 69 6e 20 64 62 20 66 69 6c 65 2e 0a 20   main db file.. 
2807c 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20         **.      
2807d 20 20 2a 2a 20 41 6c 73 6f 2c 20 74 68 65 20 73    ** Also, the s
2807e 71 6c 69 74 65 33 2e 64 66 6c 74 4c 6f 63 6b 4d  qlite3.dfltLockM
2807f 6f 64 65 20 76 61 72 69 61 62 6c 65 20 69 73 20  ode variable is 
28080 73 65 74 20 73 6f 20 74 68 61 74 0a 20 20 20 20  set so that.    
28081 20 20 20 20 2a 2a 20 61 6e 79 20 73 75 62 73 65      ** any subse
28082 71 75 65 6e 74 6c 79 20 61 74 74 61 63 68 65 64  quently attached
28083 20 64 61 74 61 62 61 73 65 73 20 61 6c 73 6f 20   databases also 
28084 75 73 65 20 74 68 65 20 73 70 65 63 69 66 69 65  use the specifie
28085 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 6c 6f 63  d.        ** loc
28086 6b 69 6e 67 20 6d 6f 64 65 2e 0a 20 20 20 20 20  king mode..     
28087 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e     */.        in
28088 74 20 69 69 3b 0a 20 20 20 20 20 20 20 20 61 73  t ii;.        as
28089 73 65 72 74 28 70 44 62 3d 3d 26 64 62 2d 3e 61  sert(pDb==&db->a
2808a 44 62 5b 30 5d 29 3b 0a 20 20 20 20 20 20 20 20  Db[0]);.        
2808b 66 6f 72 28 69 69 3d 32 3b 20 69 69 3c 64 62 2d  for(ii=2; ii<db-
2808c 3e 6e 44 62 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  >nDb; ii++){.   
2808d 20 20 20 20 20 20 20 70 50 61 67 65 72 20 3d 20         pPager = 
2808e 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65  sqlite3BtreePage
2808f 72 28 64 62 2d 3e 61 44 62 5b 69 69 5d 2e 70 42  r(db->aDb[ii].pB
28090 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  t);.          sq
28091 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e  lite3PagerLockin
28092 67 4d 6f 64 65 28 70 50 61 67 65 72 2c 20 65 4d  gMode(pPager, eM
28093 6f 64 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ode);.        }.
28094 20 20 20 20 20 20 20 20 64 62 2d 3e 64 66 6c 74          db->dflt
28095 4c 6f 63 6b 4d 6f 64 65 20 3d 20 28 75 38 29 65  LockMode = (u8)e
28096 4d 6f 64 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Mode;.      }.  
28097 20 20 20 20 70 50 61 67 65 72 20 3d 20 73 71 6c      pPager = sql
28098 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70  ite3BtreePager(p
28099 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20 20 20  Db->pBt);.      
2809a 65 4d 6f 64 65 20 3d 20 73 71 6c 69 74 65 33 50  eMode = sqlite3P
2809b 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28  agerLockingMode(
2809c 70 50 61 67 65 72 2c 20 65 4d 6f 64 65 29 3b 0a  pPager, eMode);.
2809d 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73 65 72      }..    asser
2809e 74 28 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c  t(eMode==PAGER_L
2809f 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41  OCKINGMODE_NORMA
280a0 4c 7c 7c 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  L||eMode==PAGER_
280a1 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c  LOCKINGMODE_EXCL
280a2 55 53 49 56 45 29 3b 0a 20 20 20 20 69 66 28 20  USIVE);.    if( 
280a3 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43  eMode==PAGER_LOC
280a4 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49  KINGMODE_EXCLUSI
280a5 56 45 20 29 7b 0a 20 20 20 20 20 20 7a 52 65 74  VE ){.      zRet
280a6 20 3d 20 22 65 78 63 6c 75 73 69 76 65 22 3b 0a   = "exclusive";.
280a7 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
280a8 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28  3VdbeSetNumCols(
280a9 76 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  v, 1);.    sqlit
280aa 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
280ab 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 0, COLNAME_N
280ac 41 4d 45 2c 20 22 6c 6f 63 6b 69 6e 67 5f 6d 6f  AME, "locking_mo
280ad 64 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  de", SQLITE_STAT
280ae 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  IC);.    sqlite3
280af 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
280b0 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 31 2c 20  _String8, 0, 1, 
280b1 30 2c 20 7a 52 65 74 2c 20 30 29 3b 0a 20 20 20  0, zRet, 0);.   
280b2 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
280b3 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52  p2(v, OP_ResultR
280b4 6f 77 2c 20 31 2c 20 31 29 3b 0a 20 20 7d 65 6c  ow, 1, 1);.  }el
280b5 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50  se..  /*.  **  P
280b6 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
280b7 5d 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 0a 20 20  ]journal_mode.  
280b8 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61  **  PRAGMA [data
280b9 62 61 73 65 2e 5d 6a 6f 75 72 6e 61 6c 5f 6d 6f  base.]journal_mo
280ba 64 65 20 3d 20 28 64 65 6c 65 74 65 7c 70 65 72  de = (delete|per
280bb 73 69 73 74 7c 6f 66 66 7c 74 72 75 6e 63 61 74  sist|off|truncat
280bc 65 7c 6d 65 6d 6f 72 79 29 0a 20 20 2a 2f 0a 20  e|memory).  */. 
280bd 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
280be 43 6d 70 28 7a 4c 65 66 74 2c 22 6a 6f 75 72 6e  Cmp(zLeft,"journ
280bf 61 6c 5f 6d 6f 64 65 22 29 3d 3d 30 20 29 7b 0a  al_mode")==0 ){.
280c0 20 20 20 20 69 6e 74 20 65 4d 6f 64 65 3b 0a 20      int eMode;. 
280c1 20 20 20 73 74 61 74 69 63 20 63 68 61 72 20 2a     static char *
280c2 20 63 6f 6e 73 74 20 61 7a 4d 6f 64 65 4e 61 6d   const azModeNam
280c3 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 22 64  e[] = {.      "d
280c4 65 6c 65 74 65 22 2c 20 22 70 65 72 73 69 73 74  elete", "persist
280c5 22 2c 20 22 6f 66 66 22 2c 20 22 74 72 75 6e 63  ", "off", "trunc
280c6 61 74 65 22 2c 20 22 6d 65 6d 6f 72 79 22 0a 20  ate", "memory". 
280c7 20 20 20 7d 3b 0a 0a 20 20 20 20 69 66 28 20 7a     };..    if( z
280c8 52 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20  Right==0 ){.    
280c9 20 20 65 4d 6f 64 65 20 3d 20 50 41 47 45 52 5f    eMode = PAGER_
280ca 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52  JOURNALMODE_QUER
280cb 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  Y;.    }else{.  
280cc 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69      int n = sqli
280cd 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 52 69 67  te3Strlen30(zRig
280ce 68 74 29 3b 0a 20 20 20 20 20 20 65 4d 6f 64 65  ht);.      eMode
280cf 20 3d 20 73 69 7a 65 6f 66 28 61 7a 4d 6f 64 65   = sizeof(azMode
280d0 4e 61 6d 65 29 2f 73 69 7a 65 6f 66 28 61 7a 4d  Name)/sizeof(azM
280d1 6f 64 65 4e 61 6d 65 5b 30 5d 29 20 2d 20 31 3b  odeName[0]) - 1;
280d2 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 65 4d  .      while( eM
280d3 6f 64 65 3e 3d 30 20 26 26 20 73 71 6c 69 74 65  ode>=0 && sqlite
280d4 33 53 74 72 4e 49 43 6d 70 28 7a 52 69 67 68 74  3StrNICmp(zRight
280d5 2c 20 61 7a 4d 6f 64 65 4e 61 6d 65 5b 65 4d 6f  , azModeName[eMo
280d6 64 65 5d 2c 20 6e 29 21 3d 30 20 29 7b 0a 20 20  de], n)!=0 ){.  
280d7 20 20 20 20 20 20 65 4d 6f 64 65 2d 2d 3b 0a 20        eMode--;. 
280d8 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
280d9 20 69 66 28 20 70 49 64 32 2d 3e 6e 3d 3d 30 20   if( pId2->n==0 
280da 26 26 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  && eMode==PAGER_
280db 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52  JOURNALMODE_QUER
280dc 59 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53 69  Y ){.      /* Si
280dd 6d 70 6c 65 20 22 50 52 41 47 4d 41 20 6a 6f 75  mple "PRAGMA jou
280de 72 6e 61 6c 5f 6d 6f 64 65 3b 22 20 73 74 61 74  rnal_mode;" stat
280df 65 6d 65 6e 74 2e 20 54 68 69 73 20 69 73 20 61  ement. This is a
280e0 20 71 75 65 72 79 20 66 6f 72 0a 20 20 20 20 20   query for.     
280e1 20 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20   ** the current 
280e2 64 65 66 61 75 6c 74 20 6a 6f 75 72 6e 61 6c 20  default journal 
280e3 6d 6f 64 65 20 28 77 68 69 63 68 20 6d 61 79 20  mode (which may 
280e4 62 65 20 64 69 66 66 65 72 65 6e 74 20 74 6f 0a  be different to.
280e5 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75        ** the jou
280e6 72 6e 61 6c 2d 6d 6f 64 65 20 6f 66 20 74 68 65  rnal-mode of the
280e7 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 29 2e   main database).
280e8 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
280e9 65 4d 6f 64 65 20 3d 20 64 62 2d 3e 64 66 6c 74  eMode = db->dflt
280ea 4a 6f 75 72 6e 61 6c 4d 6f 64 65 3b 0a 20 20 20  JournalMode;.   
280eb 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 50 61   }else{.      Pa
280ec 67 65 72 20 2a 70 50 61 67 65 72 3b 0a 20 20 20  ger *pPager;.   
280ed 20 20 20 69 66 28 20 70 49 64 32 2d 3e 6e 3d 3d     if( pId2->n==
280ee 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  0 ){.        /* 
280ef 54 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 74  This indicates t
280f0 68 61 74 20 6e 6f 20 64 61 74 61 62 61 73 65 20  hat no database 
280f1 6e 61 6d 65 20 77 61 73 20 73 70 65 63 69 66 69  name was specifi
280f2 65 64 20 61 73 20 70 61 72 74 0a 20 20 20 20 20  ed as part.     
280f3 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 50 52 41     ** of the PRA
280f4 47 4d 41 20 63 6f 6d 6d 61 6e 64 2e 20 49 6e 20  GMA command. In 
280f5 74 68 69 73 20 63 61 73 65 20 74 68 65 20 6a 6f  this case the jo
280f6 75 72 6e 61 6c 2d 6d 6f 64 65 20 6d 75 73 74 20  urnal-mode must 
280f7 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 65  be.        ** se
280f8 74 20 6f 6e 20 61 6c 6c 20 61 74 74 61 63 68 65  t on all attache
280f9 64 20 64 61 74 61 62 61 73 65 73 2c 20 61 73 20  d databases, as 
280fa 77 65 6c 6c 20 61 73 20 74 68 65 20 6d 61 69 6e  well as the main
280fb 20 64 62 20 66 69 6c 65 2e 0a 20 20 20 20 20 20   db file..      
280fc 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20    **.        ** 
280fd 41 6c 73 6f 2c 20 74 68 65 20 73 71 6c 69 74 65  Also, the sqlite
280fe 33 2e 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64  3.dfltJournalMod
280ff 65 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65  e variable is se
28100 74 20 73 6f 20 74 68 61 74 0a 20 20 20 20 20 20  t so that.      
28101 20 20 2a 2a 20 61 6e 79 20 73 75 62 73 65 71 75    ** any subsequ
28102 65 6e 74 6c 79 20 61 74 74 61 63 68 65 64 20 64  ently attached d
28103 61 74 61 62 61 73 65 73 20 61 6c 73 6f 20 75 73  atabases also us
28104 65 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a  e the specified.
28105 20 20 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e          ** journ
28106 61 6c 20 6d 6f 64 65 2e 0a 20 20 20 20 20 20 20  al mode..       
28107 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20   */.        int 
28108 69 69 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  ii;.        asse
28109 72 74 28 70 44 62 3d 3d 26 64 62 2d 3e 61 44 62  rt(pDb==&db->aDb
2810a 5b 30 5d 29 3b 0a 20 20 20 20 20 20 20 20 66 6f  [0]);.        fo
2810b 72 28 69 69 3d 31 3b 20 69 69 3c 64 62 2d 3e 6e  r(ii=1; ii<db->n
2810c 44 62 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20  Db; ii++){.     
2810d 20 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44 62       if( db->aDb
2810e 5b 69 69 5d 2e 70 42 74 20 29 7b 0a 20 20 20 20  [ii].pBt ){.    
2810f 20 20 20 20 20 20 20 20 70 50 61 67 65 72 20 3d          pPager =
28110 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67   sqlite3BtreePag
28111 65 72 28 64 62 2d 3e 61 44 62 5b 69 69 5d 2e 70  er(db->aDb[ii].p
28112 42 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  Bt);.           
28113 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75   sqlite3PagerJou
28114 72 6e 61 6c 4d 6f 64 65 28 70 50 61 67 65 72 2c  rnalMode(pPager,
28115 20 65 4d 6f 64 65 29 3b 0a 20 20 20 20 20 20 20   eMode);.       
28116 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
28117 20 20 20 20 20 20 20 64 62 2d 3e 64 66 6c 74 4a         db->dfltJ
28118 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d 20 28 75 38  ournalMode = (u8
28119 29 65 4d 6f 64 65 3b 0a 20 20 20 20 20 20 7d 0a  )eMode;.      }.
2811a 20 20 20 20 20 20 70 50 61 67 65 72 20 3d 20 73        pPager = s
2811b 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72  qlite3BtreePager
2811c 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20  (pDb->pBt);.    
2811d 20 20 65 4d 6f 64 65 20 3d 20 73 71 6c 69 74 65    eMode = sqlite
2811e 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64  3PagerJournalMod
2811f 65 28 70 50 61 67 65 72 2c 20 65 4d 6f 64 65 29  e(pPager, eMode)
28120 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  ;.    }.    asse
28121 72 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52  rt( eMode==PAGER
28122 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c  _JOURNALMODE_DEL
28123 45 54 45 0a 20 20 20 20 20 20 20 20 20 20 20 20  ETE.            
28124 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45    || eMode==PAGE
28125 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52  R_JOURNALMODE_TR
28126 55 4e 43 41 54 45 0a 20 20 20 20 20 20 20 20 20  UNCATE.         
28127 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50       || eMode==P
28128 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
28129 5f 50 45 52 53 49 53 54 0a 20 20 20 20 20 20 20  _PERSIST.       
2812a 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d         || eMode=
2812b 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
2812c 44 45 5f 4f 46 46 0a 20 20 20 20 20 20 20 20 20  DE_OFF.         
2812d 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50       || eMode==P
2812e 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
2812f 5f 4d 45 4d 4f 52 59 20 29 3b 0a 20 20 20 20 73  _MEMORY );.    s
28130 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
28131 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20  Cols(v, 1);.    
28132 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
28133 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
28134 41 4d 45 5f 4e 41 4d 45 2c 20 22 6a 6f 75 72 6e  AME_NAME, "journ
28135 61 6c 5f 6d 6f 64 65 22 2c 20 53 51 4c 49 54 45  al_mode", SQLITE
28136 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73 71  _STATIC);.    sq
28137 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
28138 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
28139 2c 20 31 2c 20 30 2c 20 0a 20 20 20 20 20 20 20  , 1, 0, .       
2813a 20 20 20 20 61 7a 4d 6f 64 65 4e 61 6d 65 5b 65      azModeName[e
2813b 4d 6f 64 65 5d 2c 20 50 34 5f 53 54 41 54 49 43  Mode], P4_STATIC
2813c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2813d 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
2813e 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 31 29 3b  esultRow, 1, 1);
2813f 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20  .  }else..  /*. 
28140 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74   **  PRAGMA [dat
28141 61 62 61 73 65 2e 5d 6a 6f 75 72 6e 61 6c 5f 73  abase.]journal_s
28142 69 7a 65 5f 6c 69 6d 69 74 0a 20 20 2a 2a 20 20  ize_limit.  **  
28143 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65  PRAGMA [database
28144 2e 5d 6a 6f 75 72 6e 61 6c 5f 73 69 7a 65 5f 6c  .]journal_size_l
28145 69 6d 69 74 3d 4e 0a 20 20 2a 2a 0a 20 20 2a 2a  imit=N.  **.  **
28146 20 47 65 74 20 6f 72 20 73 65 74 20 74 68 65 20   Get or set the 
28147 73 69 7a 65 20 6c 69 6d 69 74 20 6f 6e 20 72 6f  size limit on ro
28148 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 66  llback journal f
28149 69 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  iles..  */.  if(
2814a 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
2814b 7a 4c 65 66 74 2c 22 6a 6f 75 72 6e 61 6c 5f 73  zLeft,"journal_s
2814c 69 7a 65 5f 6c 69 6d 69 74 22 29 3d 3d 30 20 29  ize_limit")==0 )
2814d 7b 0a 20 20 20 20 50 61 67 65 72 20 2a 70 50 61  {.    Pager *pPa
2814e 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72  ger = sqlite3Btr
2814f 65 65 50 61 67 65 72 28 70 44 62 2d 3e 70 42 74  eePager(pDb->pBt
28150 29 3b 0a 20 20 20 20 69 36 34 20 69 4c 69 6d 69  );.    i64 iLimi
28151 74 20 3d 20 2d 32 3b 0a 20 20 20 20 69 66 28 20  t = -2;.    if( 
28152 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20  zRight ){.      
28153 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 7a 52  sqlite3Atoi64(zR
28154 69 67 68 74 2c 20 26 69 4c 69 6d 69 74 29 3b 0a  ight, &iLimit);.
28155 20 20 20 20 20 20 69 66 28 20 69 4c 69 6d 69 74        if( iLimit
28156 3c 2d 31 20 29 20 69 4c 69 6d 69 74 20 3d 20 2d  <-1 ) iLimit = -
28157 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 4c 69  1;.    }.    iLi
28158 6d 69 74 20 3d 20 73 71 6c 69 74 65 33 50 61 67  mit = sqlite3Pag
28159 65 72 4a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d  erJournalSizeLim
2815a 69 74 28 70 50 61 67 65 72 2c 20 69 4c 69 6d 69  it(pPager, iLimi
2815b 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 53 69  t);.    returnSi
2815c 6e 67 6c 65 49 6e 74 28 70 50 61 72 73 65 2c 20  ngleInt(pParse, 
2815d 22 6a 6f 75 72 6e 61 6c 5f 73 69 7a 65 5f 6c 69  "journal_size_li
2815e 6d 69 74 22 2c 20 69 4c 69 6d 69 74 29 3b 0a 20  mit", iLimit);. 
2815f 20 7d 65 6c 73 65 0a 0a 23 65 6e 64 69 66 20 2f   }else..#endif /
28160 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41  * SQLITE_OMIT_PA
28161 47 45 52 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a  GER_PRAGMAS */..
28162 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d    /*.  **  PRAGM
28163 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 61 75 74  A [database.]aut
28164 6f 5f 76 61 63 75 75 6d 0a 20 20 2a 2a 20 20 50  o_vacuum.  **  P
28165 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
28166 5d 61 75 74 6f 5f 76 61 63 75 75 6d 3d 4e 0a 20  ]auto_vacuum=N. 
28167 20 2a 2a 0a 20 20 2a 2a 20 47 65 74 20 6f 72 20   **.  ** Get or 
28168 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66  set the value of
28169 20 74 68 65 20 64 61 74 61 62 61 73 65 20 27 61   the database 'a
2816a 75 74 6f 2d 76 61 63 75 75 6d 27 20 70 61 72 61  uto-vacuum' para
2816b 6d 65 74 65 72 2e 0a 20 20 2a 2a 20 54 68 65 20  meter..  ** The 
2816c 76 61 6c 75 65 20 69 73 20 6f 6e 65 20 6f 66 3a  value is one of:
2816d 20 20 30 20 4e 4f 4e 45 20 31 20 46 55 4c 4c 20    0 NONE 1 FULL 
2816e 32 20 49 4e 43 52 45 4d 45 4e 54 41 4c 0a 20 20  2 INCREMENTAL.  
2816f 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
28170 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
28171 4d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  M.  if( sqlite3S
28172 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 61 75  trICmp(zLeft,"au
28173 74 6f 5f 76 61 63 75 75 6d 22 29 3d 3d 30 20 29  to_vacuum")==0 )
28174 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 42 74  {.    Btree *pBt
28175 20 3d 20 70 44 62 2d 3e 70 42 74 3b 0a 20 20 20   = pDb->pBt;.   
28176 20 61 73 73 65 72 74 28 20 70 42 74 21 3d 30 20   assert( pBt!=0 
28177 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  );.    if( sqlit
28178 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61  e3ReadSchema(pPa
28179 72 73 65 29 20 29 7b 0a 20 20 20 20 20 20 67 6f  rse) ){.      go
2817a 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20  to pragma_out;. 
2817b 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 7a 52     }.    if( !zR
2817c 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 69 6e  ight ){.      in
2817d 74 20 61 75 74 6f 5f 76 61 63 75 75 6d 3b 0a 20  t auto_vacuum;. 
2817e 20 20 20 20 20 69 66 28 20 41 4c 57 41 59 53 28       if( ALWAYS(
2817f 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20  pBt) ){.        
28180 20 61 75 74 6f 5f 76 61 63 75 75 6d 20 3d 20 73   auto_vacuum = s
28181 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 41 75  qlite3BtreeGetAu
28182 74 6f 56 61 63 75 75 6d 28 70 42 74 29 3b 0a 20  toVacuum(pBt);. 
28183 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
28184 20 20 20 20 20 61 75 74 6f 5f 76 61 63 75 75 6d       auto_vacuum
28185 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c   = SQLITE_DEFAUL
28186 54 5f 41 55 54 4f 56 41 43 55 55 4d 3b 0a 20 20  T_AUTOVACUUM;.  
28187 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75      }.      retu
28188 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50 61 72  rnSingleInt(pPar
28189 73 65 2c 20 22 61 75 74 6f 5f 76 61 63 75 75 6d  se, "auto_vacuum
2818a 22 2c 20 61 75 74 6f 5f 76 61 63 75 75 6d 29 3b  ", auto_vacuum);
2818b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2818c 20 20 69 6e 74 20 65 41 75 74 6f 20 3d 20 67 65    int eAuto = ge
2818d 74 41 75 74 6f 56 61 63 75 75 6d 28 7a 52 69 67  tAutoVacuum(zRig
2818e 68 74 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  ht);.      asser
2818f 74 28 20 65 41 75 74 6f 3e 3d 30 20 26 26 20 65  t( eAuto>=0 && e
28190 41 75 74 6f 3c 3d 32 20 29 3b 0a 20 20 20 20 20  Auto<=2 );.     
28191 20 64 62 2d 3e 6e 65 78 74 41 75 74 6f 76 61 63   db->nextAutovac
28192 20 3d 20 28 75 38 29 65 41 75 74 6f 3b 0a 20 20   = (u8)eAuto;.  
28193 20 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 65      if( ALWAYS(e
28194 41 75 74 6f 3e 3d 30 29 20 29 7b 0a 20 20 20 20  Auto>=0) ){.    
28195 20 20 20 20 2f 2a 20 43 61 6c 6c 20 53 65 74 41      /* Call SetA
28196 75 74 6f 56 61 63 75 75 6d 28 29 20 74 6f 20 73  utoVacuum() to s
28197 65 74 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68  et initialize th
28198 65 20 69 6e 74 65 72 6e 61 6c 20 61 75 74 6f 20  e internal auto 
28199 61 6e 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 69  and.        ** i
2819a 6e 63 72 2d 76 61 63 75 75 6d 20 66 6c 61 67 73  ncr-vacuum flags
2819b 2e 20 54 68 69 73 20 69 73 20 72 65 71 75 69 72  . This is requir
2819c 65 64 20 69 6e 20 63 61 73 65 20 74 68 69 73 20  ed in case this 
2819d 63 6f 6e 6e 65 63 74 69 6f 6e 0a 20 20 20 20 20  connection.     
2819e 20 20 20 2a 2a 20 63 72 65 61 74 65 73 20 74 68     ** creates th
2819f 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
281a0 20 49 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74   It is important
281a1 20 74 68 61 74 20 69 74 20 69 73 20 63 72 65 61   that it is crea
281a2 74 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 61  ted.        ** a
281a3 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d  s an auto-vacuum
281a4 20 63 61 70 61 62 6c 65 20 64 62 2e 0a 20 20 20   capable db..   
281a5 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
281a6 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33  int rc = sqlite3
281a7 42 74 72 65 65 53 65 74 41 75 74 6f 56 61 63 75  BtreeSetAutoVacu
281a8 75 6d 28 70 42 74 2c 20 65 41 75 74 6f 29 3b 0a  um(pBt, eAuto);.
281a9 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
281aa 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 28 65 41  SQLITE_OK && (eA
281ab 75 74 6f 3d 3d 31 20 7c 7c 20 65 41 75 74 6f 3d  uto==1 || eAuto=
281ac 3d 32 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  =2) ){.         
281ad 20 2f 2a 20 57 68 65 6e 20 73 65 74 74 69 6e 67   /* When setting
281ae 20 74 68 65 20 61 75 74 6f 5f 76 61 63 75 75 6d   the auto_vacuum
281af 20 6d 6f 64 65 20 74 6f 20 65 69 74 68 65 72 20   mode to either 
281b0 22 66 75 6c 6c 22 20 6f 72 20 0a 20 20 20 20 20  "full" or .     
281b1 20 20 20 20 20 2a 2a 20 22 69 6e 63 72 65 6d 65       ** "increme
281b2 6e 74 61 6c 22 2c 20 77 72 69 74 65 20 74 68 65  ntal", write the
281b3 20 76 61 6c 75 65 20 6f 66 20 6d 65 74 61 5b 36   value of meta[6
281b4 5d 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  ] in the databas
281b5 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66  e.          ** f
281b6 69 6c 65 2e 20 42 65 66 6f 72 65 20 77 72 69 74  ile. Before writ
281b7 69 6e 67 20 74 6f 20 6d 65 74 61 5b 36 5d 2c 20  ing to meta[6], 
281b8 63 68 65 63 6b 20 74 68 61 74 20 6d 65 74 61 5b  check that meta[
281b9 33 5d 20 69 6e 64 69 63 61 74 65 73 0a 20 20 20  3] indicates.   
281ba 20 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 74         ** that t
281bb 68 69 73 20 72 65 61 6c 6c 79 20 69 73 20 61 6e  his really is an
281bc 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 63 61 70   auto-vacuum cap
281bd 61 62 6c 65 20 64 61 74 61 62 61 73 65 2e 0a 20  able database.. 
281be 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20           */.    
281bf 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e        static con
281c0 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20 73 65  st VdbeOpList se
281c1 74 4d 65 74 61 36 5b 5d 20 3d 20 7b 0a 20 20 20  tMeta6[] = {.   
281c2 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 54 72           { OP_Tr
281c3 61 6e 73 61 63 74 69 6f 6e 2c 20 20 20 20 30 2c  ansaction,    0,
281c4 20 20 20 20 20 20 20 20 20 31 2c 20 20 20 20 20           1,     
281c5 20 20 20 20 20 20 20 20 20 20 20 20 30 7d 2c 20              0}, 
281c6 20 20 20 2f 2a 20 30 20 2a 2f 0a 20 20 20 20 20     /* 0 */.     
281c7 20 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 61 64         { OP_Read
281c8 43 6f 6f 6b 69 65 2c 20 20 20 20 20 30 2c 20 20  Cookie,     0,  
281c9 20 20 20 20 20 20 20 31 2c 20 20 20 20 20 20 20         1,       
281ca 20 20 42 54 52 45 45 5f 4c 41 52 47 45 53 54 5f    BTREE_LARGEST_
281cb 52 4f 4f 54 5f 50 41 47 45 7d 2c 0a 20 20 20 20  ROOT_PAGE},.    
281cc 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 49 66 2c          { OP_If,
281cd 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20               1, 
281ce 20 20 20 20 20 20 20 20 30 2c 20 20 20 20 20 20          0,      
281cf 20 20 20 20 20 20 20 20 20 20 20 30 7d 2c 20 20             0},  
281d0 20 20 2f 2a 20 32 20 2a 2f 0a 20 20 20 20 20 20    /* 2 */.      
281d1 20 20 20 20 20 20 7b 20 4f 50 5f 48 61 6c 74 2c        { OP_Halt,
281d2 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
281d3 45 5f 4f 4b 2c 20 4f 45 5f 41 62 6f 72 74 2c 20  E_OK, OE_Abort, 
281d4 20 20 20 20 20 20 20 20 20 30 7d 2c 20 20 20 20           0},    
281d5 2f 2a 20 33 20 2a 2f 0a 20 20 20 20 20 20 20 20  /* 3 */.        
281d6 20 20 20 20 7b 20 4f 50 5f 49 6e 74 65 67 65 72      { OP_Integer
281d7 2c 20 20 20 20 20 20 20 20 30 2c 20 20 20 20 20  ,        0,     
281d8 20 20 20 20 31 2c 20 20 20 20 20 20 20 20 20 20      1,          
281d9 20 20 20 20 20 20 20 30 7d 2c 20 20 20 20 2f 2a         0},    /*
281da 20 34 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20   4 */.          
281db 20 20 7b 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65    { OP_SetCookie
281dc 2c 20 20 20 20 20 20 30 2c 20 20 20 20 20 20 20  ,      0,       
281dd 20 20 42 54 52 45 45 5f 49 4e 43 52 5f 56 41 43    BTREE_INCR_VAC
281de 55 55 4d 2c 20 31 7d 2c 20 20 20 20 2f 2a 20 35  UUM, 1},    /* 5
281df 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 7d 3b   */.          };
281e0 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69  .          int i
281e1 41 64 64 72 3b 0a 20 20 20 20 20 20 20 20 20 20  Addr;.          
281e2 69 41 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56  iAddr = sqlite3V
281e3 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76 2c 20  dbeAddOpList(v, 
281e4 41 72 72 61 79 53 69 7a 65 28 73 65 74 4d 65 74  ArraySize(setMet
281e5 61 36 29 2c 20 73 65 74 4d 65 74 61 36 29 3b 0a  a6), setMeta6);.
281e6 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
281e7 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c  3VdbeChangeP1(v,
281e8 20 69 41 64 64 72 2c 20 69 44 62 29 3b 0a 20 20   iAddr, iDb);.  
281e9 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
281ea 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 69  dbeChangeP1(v, i
281eb 41 64 64 72 2b 31 2c 20 69 44 62 29 3b 0a 20 20  Addr+1, iDb);.  
281ec 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
281ed 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 69  dbeChangeP2(v, i
281ee 41 64 64 72 2b 32 2c 20 69 41 64 64 72 2b 34 29  Addr+2, iAddr+4)
281ef 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
281f0 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28  te3VdbeChangeP1(
281f1 76 2c 20 69 41 64 64 72 2b 34 2c 20 65 41 75 74  v, iAddr+4, eAut
281f2 6f 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20  o-1);.          
281f3 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
281f4 65 50 31 28 76 2c 20 69 41 64 64 72 2b 35 2c 20  eP1(v, iAddr+5, 
281f5 69 44 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20  iDb);.          
281f6 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42  sqlite3VdbeUsesB
281f7 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0a 20 20  tree(v, iDb);.  
281f8 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
281f9 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65      }.  }else.#e
281fa 6e 64 69 66 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  ndif..  /*.  ** 
281fb 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
281fc 65 2e 5d 69 6e 63 72 65 6d 65 6e 74 61 6c 5f 76  e.]incremental_v
281fd 61 63 75 75 6d 28 4e 29 0a 20 20 2a 2a 0a 20 20  acuum(N).  **.  
281fe 2a 2a 20 44 6f 20 4e 20 73 74 65 70 73 20 6f 66  ** Do N steps of
281ff 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63   incremental vac
28200 75 75 6d 69 6e 67 20 6f 6e 20 61 20 64 61 74 61  uuming on a data
28201 62 61 73 65 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64  base..  */.#ifnd
28202 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
28203 55 54 4f 56 41 43 55 55 4d 0a 20 20 69 66 28 20  UTOVACUUM.  if( 
28204 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
28205 4c 65 66 74 2c 22 69 6e 63 72 65 6d 65 6e 74 61  Left,"incrementa
28206 6c 5f 76 61 63 75 75 6d 22 29 3d 3d 30 20 29 7b  l_vacuum")==0 ){
28207 0a 20 20 20 20 69 6e 74 20 69 4c 69 6d 69 74 2c  .    int iLimit,
28208 20 61 64 64 72 3b 0a 20 20 20 20 69 66 28 20 73   addr;.    if( s
28209 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61  qlite3ReadSchema
2820a 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20  (pParse) ){.    
2820b 20 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75    goto pragma_ou
2820c 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  t;.    }.    if(
2820d 20 7a 52 69 67 68 74 3d 3d 30 20 7c 7c 20 21 73   zRight==0 || !s
2820e 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32 28 7a  qlite3GetInt32(z
2820f 52 69 67 68 74 2c 20 26 69 4c 69 6d 69 74 29 20  Right, &iLimit) 
28210 7c 7c 20 69 4c 69 6d 69 74 3c 3d 30 20 29 7b 0a  || iLimit<=0 ){.
28211 20 20 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 30        iLimit = 0
28212 78 37 66 66 66 66 66 66 66 3b 0a 20 20 20 20 7d  x7fffffff;.    }
28213 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69  .    sqlite3Begi
28214 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28  nWriteOperation(
28215 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b  pParse, 0, iDb);
28216 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
28217 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
28218 65 67 65 72 2c 20 69 4c 69 6d 69 74 2c 20 31 29  eger, iLimit, 1)
28219 3b 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c  ;.    addr = sql
2821a 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2821b 2c 20 4f 50 5f 49 6e 63 72 56 61 63 75 75 6d 2c  , OP_IncrVacuum,
2821c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74   iDb);.    sqlit
2821d 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
2821e 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 29  OP_ResultRow, 1)
2821f 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
28220 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64  eAddOp2(v, OP_Ad
28221 64 49 6d 6d 2c 20 31 2c 20 2d 31 29 3b 0a 20 20  dImm, 1, -1);.  
28222 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
28223 4f 70 32 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c  Op2(v, OP_IfPos,
28224 20 31 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 73   1, addr);.    s
28225 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
28226 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 20 20 7d  re(v, addr);.  }
28227 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 23 69 66  else.#endif..#if
28228 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
28229 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 20  _PAGER_PRAGMAS. 
2822a 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41   /*.  **  PRAGMA
2822b 20 5b 64 61 74 61 62 61 73 65 2e 5d 63 61 63 68   [database.]cach
2822c 65 5f 73 69 7a 65 0a 20 20 2a 2a 20 20 50 52 41  e_size.  **  PRA
2822d 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 63  GMA [database.]c
2822e 61 63 68 65 5f 73 69 7a 65 3d 4e 0a 20 20 2a 2a  ache_size=N.  **
2822f 0a 20 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20  .  ** The first 
28230 66 6f 72 6d 20 72 65 70 6f 72 74 73 20 74 68 65  form reports the
28231 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 6c 20 73   current local s
28232 65 74 74 69 6e 67 20 66 6f 72 20 74 68 65 0a 20  etting for the. 
28233 20 2a 2a 20 70 61 67 65 20 63 61 63 68 65 20 73   ** page cache s
28234 69 7a 65 2e 20 20 54 68 65 20 6c 6f 63 61 6c 20  ize.  The local 
28235 73 65 74 74 69 6e 67 20 63 61 6e 20 62 65 20 64  setting can be d
28236 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 0a 20 20  ifferent from.  
28237 2a 2a 20 74 68 65 20 70 65 72 73 69 73 74 65 6e  ** the persisten
28238 74 20 63 61 63 68 65 20 73 69 7a 65 20 76 61 6c  t cache size val
28239 75 65 20 74 68 61 74 20 69 73 20 73 74 6f 72 65  ue that is store
2823a 64 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  d in the databas
2823b 65 0a 20 20 2a 2a 20 66 69 6c 65 20 69 74 73 65  e.  ** file itse
2823c 6c 66 2e 20 20 54 68 65 20 76 61 6c 75 65 20 72  lf.  The value r
2823d 65 74 75 72 6e 65 64 20 69 73 20 74 68 65 20 6d  eturned is the m
2823e 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66  aximum number of
2823f 0a 20 20 2a 2a 20 70 61 67 65 73 20 69 6e 20 74  .  ** pages in t
28240 68 65 20 70 61 67 65 20 63 61 63 68 65 2e 20 20  he page cache.  
28241 54 68 65 20 73 65 63 6f 6e 64 20 66 6f 72 6d 20  The second form 
28242 73 65 74 73 20 74 68 65 20 6c 6f 63 61 6c 0a 20  sets the local. 
28243 20 2a 2a 20 70 61 67 65 20 63 61 63 68 65 20 73   ** page cache s
28244 69 7a 65 20 76 61 6c 75 65 2e 20 20 49 74 20 64  ize value.  It d
28245 6f 65 73 20 6e 6f 74 20 63 68 61 6e 67 65 20 74  oes not change t
28246 68 65 20 70 65 72 73 69 73 74 65 6e 74 0a 20 20  he persistent.  
28247 2a 2a 20 63 61 63 68 65 20 73 69 7a 65 20 73 74  ** cache size st
28248 6f 72 65 64 20 6f 6e 20 74 68 65 20 64 69 73 6b  ored on the disk
28249 20 73 6f 20 74 68 65 20 63 61 63 68 65 20 73 69   so the cache si
2824a 7a 65 20 77 69 6c 6c 20 72 65 76 65 72 74 0a 20  ze will revert. 
2824b 20 2a 2a 20 74 6f 20 69 74 73 20 64 65 66 61 75   ** to its defau
2824c 6c 74 20 76 61 6c 75 65 20 77 68 65 6e 20 74 68  lt value when th
2824d 65 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6c  e database is cl
2824e 6f 73 65 64 20 61 6e 64 20 72 65 6f 70 65 6e 65  osed and reopene
2824f 64 2e 0a 20 20 2a 2a 20 4e 20 73 68 6f 75 6c 64  d..  ** N should
28250 20 62 65 20 61 20 70 6f 73 69 74 69 76 65 20 69   be a positive i
28251 6e 74 65 67 65 72 2e 0a 20 20 2a 2f 0a 20 20 69  nteger..  */.  i
28252 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
28253 70 28 7a 4c 65 66 74 2c 22 63 61 63 68 65 5f 73  p(zLeft,"cache_s
28254 69 7a 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ize")==0 ){.    
28255 69 66 28 20 73 71 6c 69 74 65 33 52 65 61 64 53  if( sqlite3ReadS
28256 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 20  chema(pParse) ) 
28257 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b  goto pragma_out;
28258 0a 20 20 20 20 69 66 28 20 21 7a 52 69 67 68 74  .    if( !zRight
28259 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
2825a 53 69 6e 67 6c 65 49 6e 74 28 70 50 61 72 73 65  SingleInt(pParse
2825b 2c 20 22 63 61 63 68 65 5f 73 69 7a 65 22 2c 20  , "cache_size", 
2825c 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61  pDb->pSchema->ca
2825d 63 68 65 5f 73 69 7a 65 29 3b 0a 20 20 20 20 7d  che_size);.    }
2825e 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20  else{.      int 
2825f 73 69 7a 65 20 3d 20 61 74 6f 69 28 7a 52 69 67  size = atoi(zRig
28260 68 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 73  ht);.      if( s
28261 69 7a 65 3c 30 20 29 20 73 69 7a 65 20 3d 20 2d  ize<0 ) size = -
28262 73 69 7a 65 3b 0a 20 20 20 20 20 20 70 44 62 2d  size;.      pDb-
28263 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f  >pSchema->cache_
28264 73 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 20  size = size;.   
28265 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 53     sqlite3BtreeS
28266 65 74 43 61 63 68 65 53 69 7a 65 28 70 44 62 2d  etCacheSize(pDb-
28267 3e 70 42 74 2c 20 70 44 62 2d 3e 70 53 63 68 65  >pBt, pDb->pSche
28268 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65 29 3b  ma->cache_size);
28269 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a  .    }.  }else..
2826a 20 20 2f 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47    /*.  **   PRAG
2826b 4d 41 20 74 65 6d 70 5f 73 74 6f 72 65 0a 20 20  MA temp_store.  
2826c 2a 2a 20 20 20 50 52 41 47 4d 41 20 74 65 6d 70  **   PRAGMA temp
2826d 5f 73 74 6f 72 65 20 3d 20 22 64 65 66 61 75 6c  _store = "defaul
2826e 74 22 7c 22 6d 65 6d 6f 72 79 22 7c 22 66 69 6c  t"|"memory"|"fil
2826f 65 22 0a 20 20 2a 2a 0a 20 20 2a 2a 20 52 65 74  e".  **.  ** Ret
28270 75 72 6e 20 6f 72 20 73 65 74 20 74 68 65 20 6c  urn or set the l
28271 6f 63 61 6c 20 76 61 6c 75 65 20 6f 66 20 74 68  ocal value of th
28272 65 20 74 65 6d 70 5f 73 74 6f 72 65 20 66 6c 61  e temp_store fla
28273 67 2e 20 20 43 68 61 6e 67 69 6e 67 0a 20 20 2a  g.  Changing.  *
28274 2a 20 74 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75  * the local valu
28275 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61 6b 65 20  e does not make 
28276 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 64  changes to the d
28277 69 73 6b 20 66 69 6c 65 20 61 6e 64 20 74 68 65  isk file and the
28278 20 64 65 66 61 75 6c 74 0a 20 20 2a 2a 20 76 61   default.  ** va
28279 6c 75 65 20 77 69 6c 6c 20 62 65 20 72 65 73 74  lue will be rest
2827a 6f 72 65 64 20 74 68 65 20 6e 65 78 74 20 74 69  ored the next ti
2827b 6d 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  me the database 
2827c 69 73 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2a 0a  is opened..  **.
2827d 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 69    ** Note that i
2827e 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 66 6f  t is possible fo
2827f 72 20 74 68 65 20 6c 69 62 72 61 72 79 20 63 6f  r the library co
28280 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f  mpile-time optio
28281 6e 73 20 74 6f 0a 20 20 2a 2a 20 6f 76 65 72 72  ns to.  ** overr
28282 69 64 65 20 74 68 69 73 20 73 65 74 74 69 6e 67  ide this setting
28283 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
28284 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
28285 2c 20 22 74 65 6d 70 5f 73 74 6f 72 65 22 29 3d  , "temp_store")=
28286 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 21 7a  =0 ){.    if( !z
28287 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 72  Right ){.      r
28288 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70  eturnSingleInt(p
28289 50 61 72 73 65 2c 20 22 74 65 6d 70 5f 73 74 6f  Parse, "temp_sto
2828a 72 65 22 2c 20 64 62 2d 3e 74 65 6d 70 5f 73 74  re", db->temp_st
2828b 6f 72 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ore);.    }else{
2828c 0a 20 20 20 20 20 20 63 68 61 6e 67 65 54 65 6d  .      changeTem
2828d 70 53 74 6f 72 61 67 65 28 70 50 61 72 73 65 2c  pStorage(pParse,
2828e 20 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a   zRight);.    }.
2828f 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20    }else..  /*.  
28290 2a 2a 20 20 20 50 52 41 47 4d 41 20 74 65 6d 70  **   PRAGMA temp
28291 5f 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79  _store_directory
28292 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 74  .  **   PRAGMA t
28293 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63 74  emp_store_direct
28294 6f 72 79 20 3d 20 22 22 7c 22 64 69 72 65 63 74  ory = ""|"direct
28295 6f 72 79 5f 6e 61 6d 65 22 0a 20 20 2a 2a 0a 20  ory_name".  **. 
28296 20 2a 2a 20 52 65 74 75 72 6e 20 6f 72 20 73 65   ** Return or se
28297 74 20 74 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75  t the local valu
28298 65 20 6f 66 20 74 68 65 20 74 65 6d 70 5f 73 74  e of the temp_st
28299 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 20 66 6c  ore_directory fl
2829a 61 67 2e 20 20 43 68 61 6e 67 69 6e 67 0a 20 20  ag.  Changing.  
2829b 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 73 65 74  ** the value set
2829c 73 20 61 20 73 70 65 63 69 66 69 63 20 64 69 72  s a specific dir
2829d 65 63 74 6f 72 79 20 74 6f 20 62 65 20 75 73 65  ectory to be use
2829e 64 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20  d for temporary 
2829f 66 69 6c 65 73 2e 0a 20 20 2a 2a 20 53 65 74 74  files..  ** Sett
282a0 69 6e 67 20 74 6f 20 61 20 6e 75 6c 6c 20 73 74  ing to a null st
282a1 72 69 6e 67 20 72 65 76 65 72 74 73 20 74 6f 20  ring reverts to 
282a2 74 68 65 20 64 65 66 61 75 6c 74 20 74 65 6d 70  the default temp
282a3 6f 72 61 72 79 20 64 69 72 65 63 74 6f 72 79 20  orary directory 
282a4 73 65 61 72 63 68 2e 0a 20 20 2a 2a 20 49 66 20  search..  ** If 
282a5 74 65 6d 70 6f 72 61 72 79 20 64 69 72 65 63 74  temporary direct
282a6 6f 72 79 20 69 73 20 63 68 61 6e 67 65 64 2c 20  ory is changed, 
282a7 74 68 65 6e 20 69 6e 76 61 6c 69 64 61 74 65 54  then invalidateT
282a8 65 6d 70 53 74 6f 72 61 67 65 2e 0a 20 20 2a 2a  empStorage..  **
282a9 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
282aa 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
282ab 2c 20 22 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69  , "temp_store_di
282ac 72 65 63 74 6f 72 79 22 29 3d 3d 30 20 29 7b 0a  rectory")==0 ){.
282ad 20 20 20 20 69 66 28 20 21 7a 52 69 67 68 74 20      if( !zRight 
282ae 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  ){.      if( sql
282af 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74  ite3_temp_direct
282b0 6f 72 79 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ory ){.        s
282b1 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
282b2 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20  Cols(v, 1);.    
282b3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
282b4 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20  etColName(v, 0, 
282b5 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 0a 20  COLNAME_NAME, . 
282b6 20 20 20 20 20 20 20 20 20 20 20 22 74 65 6d 70             "temp
282b7 5f 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79  _store_directory
282b8 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
282b9 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
282ba 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
282bb 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 31  OP_String8, 0, 1
282bc 2c 20 30 2c 20 73 71 6c 69 74 65 33 5f 74 65 6d  , 0, sqlite3_tem
282bd 70 5f 64 69 72 65 63 74 6f 72 79 2c 20 30 29 3b  p_directory, 0);
282be 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
282bf 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
282c0 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 31  _ResultRow, 1, 1
282c1 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
282c2 65 6c 73 65 7b 0a 23 69 66 6e 64 65 66 20 53 51  else{.#ifndef SQ
282c3 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 20 20  LITE_OMIT_WSD.  
282c4 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 5b 30      if( zRight[0
282c5 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  ] ){.        int
282c6 20 72 63 3b 0a 20 20 20 20 20 20 20 20 69 6e 74   rc;.        int
282c7 20 72 65 73 3b 0a 20 20 20 20 20 20 20 20 72 63   res;.        rc
282c8 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65   = sqlite3OsAcce
282c9 73 73 28 64 62 2d 3e 70 56 66 73 2c 20 7a 52 69  ss(db->pVfs, zRi
282ca 67 68 74 2c 20 53 51 4c 49 54 45 5f 41 43 43 45  ght, SQLITE_ACCE
282cb 53 53 5f 52 45 41 44 57 52 49 54 45 2c 20 26 72  SS_READWRITE, &r
282cc 65 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  es);.        if(
282cd 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc!=SQLITE_OK |
282ce 7c 20 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20  | res==0 ){.    
282cf 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
282d0 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e  orMsg(pParse, "n
282d1 6f 74 20 61 20 77 72 69 74 61 62 6c 65 20 64 69  ot a writable di
282d2 72 65 63 74 6f 72 79 22 29 3b 0a 20 20 20 20 20  rectory");.     
282d3 20 20 20 20 20 67 6f 74 6f 20 70 72 61 67 6d 61       goto pragma
282d4 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  _out;.        }.
282d5 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
282d6 28 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54  ( SQLITE_TEMP_ST
282d7 4f 52 45 3d 3d 30 0a 20 20 20 20 20 20 20 7c 7c  ORE==0.       ||
282d8 20 28 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54   (SQLITE_TEMP_ST
282d9 4f 52 45 3d 3d 31 20 26 26 20 64 62 2d 3e 74 65  ORE==1 && db->te
282da 6d 70 5f 73 74 6f 72 65 3c 3d 31 29 0a 20 20 20  mp_store<=1).   
282db 20 20 20 20 7c 7c 20 28 53 51 4c 49 54 45 5f 54      || (SQLITE_T
282dc 45 4d 50 5f 53 54 4f 52 45 3d 3d 32 20 26 26 20  EMP_STORE==2 && 
282dd 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 3d 3d  db->temp_store==
282de 31 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20  1).      ){.    
282df 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 54 65      invalidateTe
282e0 6d 70 53 74 6f 72 61 67 65 28 70 50 61 72 73 65  mpStorage(pParse
282e1 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
282e2 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 73 71   sqlite3_free(sq
282e3 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63  lite3_temp_direc
282e4 74 6f 72 79 29 3b 0a 20 20 20 20 20 20 69 66 28  tory);.      if(
282e5 20 7a 52 69 67 68 74 5b 30 5d 20 29 7b 0a 20 20   zRight[0] ){.  
282e6 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 74 65        sqlite3_te
282e7 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 3d 20 73  mp_directory = s
282e8 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 30  qlite3DbStrDup(0
282e9 2c 20 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 20  , zRight);.     
282ea 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
282eb 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72  sqlite3_temp_dir
282ec 65 63 74 6f 72 79 20 3d 20 30 3b 0a 20 20 20 20  ectory = 0;.    
282ed 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51    }.#endif /* SQ
282ee 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 20 2a 2f  LITE_OMIT_WSD */
282ef 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a  .    }.  }else..
282f0 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
282f1 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
282f2 4e 47 5f 53 54 59 4c 45 29 0a 23 20 20 69 66 20  NG_STYLE).#  if 
282f3 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f  defined(__APPLE_
282f4 5f 29 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53  _).#    define S
282f5 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
282f6 4b 49 4e 47 5f 53 54 59 4c 45 20 31 0a 23 20 20  KING_STYLE 1.#  
282f7 65 6c 73 65 0a 23 20 20 20 20 64 65 66 69 6e 65  else.#    define
282f8 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
282f9 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 30 0a 23  OCKING_STYLE 0.#
282fa 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23    endif.#endif.#
282fb 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  if SQLITE_ENABLE
282fc 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20  _LOCKING_STYLE. 
282fd 20 2f 2a 0a 20 20 20 2a 2a 20 20 20 50 52 41 47   /*.   **   PRAG
282fe 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f  MA [database.]lo
282ff 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 0a 20 20  ck_proxy_file.  
28300 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b 64 61   **   PRAGMA [da
28301 74 61 62 61 73 65 2e 5d 6c 6f 63 6b 5f 70 72 6f  tabase.]lock_pro
28302 78 79 5f 66 69 6c 65 20 3d 20 22 3a 61 75 74 6f  xy_file = ":auto
28303 3a 22 7c 22 6c 6f 63 6b 5f 66 69 6c 65 5f 70 61  :"|"lock_file_pa
28304 74 68 22 0a 20 20 20 2a 2a 0a 20 20 20 2a 2a 20  th".   **.   ** 
28305 52 65 74 75 72 6e 20 6f 72 20 73 65 74 20 74 68  Return or set th
28306 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6c  e value of the l
28307 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 20 66  ock_proxy_file f
28308 6c 61 67 2e 20 20 43 68 61 6e 67 69 6e 67 0a 20  lag.  Changing. 
28309 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 73    ** the value s
2830a 65 74 73 20 61 20 73 70 65 63 69 66 69 63 20 66  ets a specific f
2830b 69 6c 65 20 74 6f 20 62 65 20 75 73 65 64 20 66  ile to be used f
2830c 6f 72 20 64 61 74 61 62 61 73 65 20 61 63 63 65  or database acce
2830d 73 73 20 6c 6f 63 6b 73 2e 0a 20 20 20 2a 2a 0a  ss locks..   **.
2830e 20 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69     */.  if( sqli
2830f 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
28310 2c 20 22 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69  , "lock_proxy_fi
28311 6c 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69  le")==0 ){.    i
28312 66 28 20 21 7a 52 69 67 68 74 20 29 7b 0a 20 20  f( !zRight ){.  
28313 20 20 20 20 50 61 67 65 72 20 2a 70 50 61 67 65      Pager *pPage
28314 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  r = sqlite3Btree
28315 50 61 67 65 72 28 70 44 62 2d 3e 70 42 74 29 3b  Pager(pDb->pBt);
28316 0a 20 20 20 20 20 20 63 68 61 72 20 2a 70 72 6f  .      char *pro
28317 78 79 5f 66 69 6c 65 5f 70 61 74 68 20 3d 20 4e  xy_file_path = N
28318 55 4c 4c 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ULL;.      sqlit
28319 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 20 3d  e3_file *pFile =
2831a 20 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c   sqlite3PagerFil
2831b 65 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20  e(pPager);.     
2831c 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f   sqlite3OsFileCo
2831d 6e 74 72 6f 6c 28 70 46 69 6c 65 2c 20 53 51 4c  ntrol(pFile, SQL
2831e 49 54 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58  ITE_GET_LOCKPROX
2831f 59 46 49 4c 45 2c 20 0a 20 20 20 20 20 20 20 20  YFILE, .        
28320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28321 20 20 20 26 70 72 6f 78 79 5f 66 69 6c 65 5f 70     &proxy_file_p
28322 61 74 68 29 3b 0a 20 20 20 20 20 20 0a 20 20 20  ath);.      .   
28323 20 20 20 69 66 28 20 70 72 6f 78 79 5f 66 69 6c     if( proxy_fil
28324 65 5f 70 61 74 68 20 29 7b 0a 20 20 20 20 20 20  e_path ){.      
28325 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
28326 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20  NumCols(v, 1);. 
28327 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
28328 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
28329 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  0, COLNAME_NAME,
2832a 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
2832b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2832c 22 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65  "lock_proxy_file
2832d 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
2832e 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2832f 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
28330 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 31  OP_String8, 0, 1
28331 2c 20 30 2c 20 70 72 6f 78 79 5f 66 69 6c 65 5f  , 0, proxy_file_
28332 70 61 74 68 2c 20 30 29 3b 0a 20 20 20 20 20 20  path, 0);.      
28333 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
28334 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74  Op2(v, OP_Result
28335 52 6f 77 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20  Row, 1, 1);.    
28336 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
28337 20 20 20 20 20 50 61 67 65 72 20 2a 70 50 61 67       Pager *pPag
28338 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  er = sqlite3Btre
28339 65 50 61 67 65 72 28 70 44 62 2d 3e 70 42 74 29  ePager(pDb->pBt)
2833a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
2833b 66 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 73 71  file *pFile = sq
2833c 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 28 70  lite3PagerFile(p
2833d 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 69 6e  Pager);.      in
2833e 74 20 72 65 73 3b 0a 20 20 20 20 20 20 69 66 28  t res;.      if(
2833f 20 7a 52 69 67 68 74 5b 30 5d 20 29 7b 0a 20 20   zRight[0] ){.  
28340 20 20 20 20 20 20 72 65 73 3d 73 71 6c 69 74 65        res=sqlite
28341 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 70  3OsFileControl(p
28342 46 69 6c 65 2c 20 53 51 4c 49 54 45 5f 53 45 54  File, SQLITE_SET
28343 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 2c 20  _LOCKPROXYFILE, 
28344 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
28345 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28346 20 20 20 20 20 20 7a 52 69 67 68 74 29 3b 0a 20        zRight);. 
28347 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20       } else {.  
28348 20 20 20 20 20 20 72 65 73 3d 73 71 6c 69 74 65        res=sqlite
28349 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 70  3OsFileControl(p
2834a 46 69 6c 65 2c 20 53 51 4c 49 54 45 5f 53 45 54  File, SQLITE_SET
2834b 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 2c 20  _LOCKPROXYFILE, 
2834c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2834d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2834e 20 20 20 20 20 20 4e 55 4c 4c 29 3b 0a 20 20 20        NULL);.   
2834f 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72     }.      if( r
28350 65 73 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  es!=SQLITE_OK ){
28351 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
28352 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
28353 20 22 66 61 69 6c 65 64 20 74 6f 20 73 65 74 20   "failed to set 
28354 6c 6f 63 6b 20 70 72 6f 78 79 20 66 69 6c 65 22  lock proxy file"
28355 29 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  );.        goto 
28356 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20  pragma_out;.    
28357 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73    }.    }.  }els
28358 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  e.#endif /* SQLI
28359 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
2835a 47 5f 53 54 59 4c 45 20 2a 2f 20 20 20 20 20 20  G_STYLE */      
2835b 0a 20 20 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20  .    .  /*.  ** 
2835c 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
2835d 73 65 2e 5d 73 79 6e 63 68 72 6f 6e 6f 75 73 0a  se.]synchronous.
2835e 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b 64    **   PRAGMA [d
2835f 61 74 61 62 61 73 65 2e 5d 73 79 6e 63 68 72 6f  atabase.]synchro
28360 6e 6f 75 73 3d 4f 46 46 7c 4f 4e 7c 4e 4f 52 4d  nous=OFF|ON|NORM
28361 41 4c 7c 46 55 4c 4c 0a 20 20 2a 2a 0a 20 20 2a  AL|FULL.  **.  *
28362 2a 20 52 65 74 75 72 6e 20 6f 72 20 73 65 74 20  * Return or set 
28363 74 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65 20  the local value 
28364 6f 66 20 74 68 65 20 73 79 6e 63 68 72 6f 6e 6f  of the synchrono
28365 75 73 20 66 6c 61 67 2e 20 20 43 68 61 6e 67 69  us flag.  Changi
28366 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6c 6f 63 61  ng.  ** the loca
28367 6c 20 76 61 6c 75 65 20 64 6f 65 73 20 6e 6f 74  l value does not
28368 20 6d 61 6b 65 20 63 68 61 6e 67 65 73 20 74 6f   make changes to
28369 20 74 68 65 20 64 69 73 6b 20 66 69 6c 65 20 61   the disk file a
2836a 6e 64 20 74 68 65 0a 20 20 2a 2a 20 64 65 66 61  nd the.  ** defa
2836b 75 6c 74 20 76 61 6c 75 65 20 77 69 6c 6c 20 62  ult value will b
2836c 65 20 72 65 73 74 6f 72 65 64 20 74 68 65 20 6e  e restored the n
2836d 65 78 74 20 74 69 6d 65 20 74 68 65 20 64 61 74  ext time the dat
2836e 61 62 61 73 65 20 69 73 0a 20 20 2a 2a 20 6f 70  abase is.  ** op
2836f 65 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ened..  */.  if(
28370 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
28371 7a 4c 65 66 74 2c 22 73 79 6e 63 68 72 6f 6e 6f  zLeft,"synchrono
28372 75 73 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69  us")==0 ){.    i
28373 66 28 20 73 71 6c 69 74 65 33 52 65 61 64 53 63  f( sqlite3ReadSc
28374 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 20 67  hema(pParse) ) g
28375 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a  oto pragma_out;.
28376 20 20 20 20 69 66 28 20 21 7a 52 69 67 68 74 20      if( !zRight 
28377 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 53  ){.      returnS
28378 69 6e 67 6c 65 49 6e 74 28 70 50 61 72 73 65 2c  ingleInt(pParse,
28379 20 22 73 79 6e 63 68 72 6f 6e 6f 75 73 22 2c 20   "synchronous", 
2837a 70 44 62 2d 3e 73 61 66 65 74 79 5f 6c 65 76 65  pDb->safety_leve
2837b 6c 2d 31 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  l-1);.    }else{
2837c 0a 20 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e  .      if( !db->
2837d 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20  autoCommit ){.  
2837e 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
2837f 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20  orMsg(pParse, . 
28380 20 20 20 20 20 20 20 20 20 20 20 22 53 61 66 65             "Safe
28381 74 79 20 6c 65 76 65 6c 20 6d 61 79 20 6e 6f 74  ty level may not
28382 20 62 65 20 63 68 61 6e 67 65 64 20 69 6e 73 69   be changed insi
28383 64 65 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  de a transaction
28384 22 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ");.      }else{
28385 0a 20 20 20 20 20 20 20 20 70 44 62 2d 3e 73 61  .        pDb->sa
28386 66 65 74 79 5f 6c 65 76 65 6c 20 3d 20 67 65 74  fety_level = get
28387 53 61 66 65 74 79 4c 65 76 65 6c 28 7a 52 69 67  SafetyLevel(zRig
28388 68 74 29 2b 31 3b 0a 20 20 20 20 20 20 7d 0a 20  ht)+1;.      }. 
28389 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e     }.  }else.#en
2838a 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
2838b 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53  IT_PAGER_PRAGMAS
2838c 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
2838d 49 54 45 5f 4f 4d 49 54 5f 46 4c 41 47 5f 50 52  ITE_OMIT_FLAG_PR
2838e 41 47 4d 41 53 0a 20 20 69 66 28 20 66 6c 61 67  AGMAS.  if( flag
2838f 50 72 61 67 6d 61 28 70 50 61 72 73 65 2c 20 7a  Pragma(pParse, z
28390 4c 65 66 74 2c 20 7a 52 69 67 68 74 29 20 29 7b  Left, zRight) ){
28391 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6c 61 67  .    /* The flag
28392 50 72 61 67 6d 61 28 29 20 73 75 62 72 6f 75 74  Pragma() subrout
28393 69 6e 65 20 61 6c 73 6f 20 67 65 6e 65 72 61 74  ine also generat
28394 65 73 20 61 6e 79 20 6e 65 63 65 73 73 61 72 79  es any necessary
28395 20 63 6f 64 65 0a 20 20 20 20 2a 2a 20 74 68 65   code.    ** the
28396 72 65 20 69 73 20 6e 6f 74 68 69 6e 67 20 6d 6f  re is nothing mo
28397 72 65 20 74 6f 20 64 6f 20 68 65 72 65 20 2a 2f  re to do here */
28398 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20  .  }else.#endif 
28399 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  /* SQLITE_OMIT_F
2839a 4c 41 47 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a  LAG_PRAGMAS */..
2839b 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2839c 4d 49 54 5f 53 43 48 45 4d 41 5f 50 52 41 47 4d  MIT_SCHEMA_PRAGM
2839d 41 53 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 20 50  AS.  /*.  **   P
2839e 52 41 47 4d 41 20 74 61 62 6c 65 5f 69 6e 66 6f  RAGMA table_info
2839f 28 3c 74 61 62 6c 65 3e 29 0a 20 20 2a 2a 0a 20  (<table>).  **. 
283a0 20 2a 2a 20 52 65 74 75 72 6e 20 61 20 73 69 6e   ** Return a sin
283a1 67 6c 65 20 72 6f 77 20 66 6f 72 20 65 61 63 68  gle row for each
283a2 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 6e   column of the n
283a3 61 6d 65 64 20 74 61 62 6c 65 2e 20 54 68 65 20  amed table. The 
283a4 63 6f 6c 75 6d 6e 73 20 6f 66 0a 20 20 2a 2a 20  columns of.  ** 
283a5 74 68 65 20 72 65 74 75 72 6e 65 64 20 64 61 74  the returned dat
283a6 61 20 73 65 74 20 61 72 65 3a 0a 20 20 2a 2a 0a  a set are:.  **.
283a7 20 20 2a 2a 20 63 69 64 3a 20 20 20 20 20 20 20    ** cid:       
283a8 20 43 6f 6c 75 6d 6e 20 69 64 20 28 6e 75 6d 62   Column id (numb
283a9 65 72 65 64 20 66 72 6f 6d 20 6c 65 66 74 20 74  ered from left t
283aa 6f 20 72 69 67 68 74 2c 20 73 74 61 72 74 69 6e  o right, startin
283ab 67 20 61 74 20 30 29 0a 20 20 2a 2a 20 6e 61 6d  g at 0).  ** nam
283ac 65 3a 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20  e:       Column 
283ad 6e 61 6d 65 0a 20 20 2a 2a 20 74 79 70 65 3a 20  name.  ** type: 
283ae 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 64 65 63        Column dec
283af 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 2e 0a 20  laration type.. 
283b0 20 2a 2a 20 6e 6f 74 6e 75 6c 6c 3a 20 20 20 20   ** notnull:    
283b1 54 72 75 65 20 69 66 20 27 4e 4f 54 20 4e 55 4c  True if 'NOT NUL
283b2 4c 27 20 69 73 20 70 61 72 74 20 6f 66 20 63 6f  L' is part of co
283b3 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e  lumn declaration
283b4 0a 20 20 2a 2a 20 64 66 6c 74 5f 76 61 6c 75 65  .  ** dflt_value
283b5 3a 20 54 68 65 20 64 65 66 61 75 6c 74 20 76 61  : The default va
283b6 6c 75 65 20 66 6f 72 20 74 68 65 20 63 6f 6c 75  lue for the colu
283b7 6d 6e 2c 20 69 66 20 61 6e 79 2e 0a 20 20 2a 2f  mn, if any..  */
283b8 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
283b9 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 74 61  rICmp(zLeft, "ta
283ba 62 6c 65 5f 69 6e 66 6f 22 29 3d 3d 30 20 26 26  ble_info")==0 &&
283bb 20 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 54   zRight ){.    T
283bc 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20  able *pTab;.    
283bd 69 66 28 20 73 71 6c 69 74 65 33 52 65 61 64 53  if( sqlite3ReadS
283be 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 20  chema(pParse) ) 
283bf 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b  goto pragma_out;
283c0 0a 20 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69  .    pTab = sqli
283c1 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c  te3FindTable(db,
283c2 20 7a 52 69 67 68 74 2c 20 7a 44 62 29 3b 0a 20   zRight, zDb);. 
283c3 20 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20     if( pTab ){. 
283c4 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20       int i;.    
283c5 20 20 69 6e 74 20 6e 48 69 64 64 65 6e 20 3d 20    int nHidden = 
283c6 30 3b 0a 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20  0;.      Column 
283c7 2a 70 43 6f 6c 3b 0a 20 20 20 20 20 20 73 71 6c  *pCol;.      sql
283c8 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f  ite3VdbeSetNumCo
283c9 6c 73 28 76 2c 20 36 29 3b 0a 20 20 20 20 20 20  ls(v, 6);.      
283ca 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 36  pParse->nMem = 6
283cb 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
283cc 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
283cd 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   0, COLNAME_NAME
283ce 2c 20 22 63 69 64 22 2c 20 53 51 4c 49 54 45 5f  , "cid", SQLITE_
283cf 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73  STATIC);.      s
283d0 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
283d1 4e 61 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e 41  Name(v, 1, COLNA
283d2 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d 65 22 2c  ME_NAME, "name",
283d3 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
283d4 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
283d5 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
283d6 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  2, COLNAME_NAME,
283d7 20 22 74 79 70 65 22 2c 20 53 51 4c 49 54 45 5f   "type", SQLITE_
283d8 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73  STATIC);.      s
283d9 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
283da 4e 61 6d 65 28 76 2c 20 33 2c 20 43 4f 4c 4e 41  Name(v, 3, COLNA
283db 4d 45 5f 4e 41 4d 45 2c 20 22 6e 6f 74 6e 75 6c  ME_NAME, "notnul
283dc 6c 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  l", SQLITE_STATI
283dd 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  C);.      sqlite
283de 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
283df 76 2c 20 34 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 4, COLNAME_NA
283e0 4d 45 2c 20 22 64 66 6c 74 5f 76 61 6c 75 65 22  ME, "dflt_value"
283e1 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
283e2 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
283e3 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
283e4 20 35 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   5, COLNAME_NAME
283e5 2c 20 22 70 6b 22 2c 20 53 51 4c 49 54 45 5f 53  , "pk", SQLITE_S
283e6 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71  TATIC);.      sq
283e7 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75  lite3ViewGetColu
283e8 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20  mnNames(pParse, 
283e9 70 54 61 62 29 3b 0a 20 20 20 20 20 20 66 6f 72  pTab);.      for
283ea 28 69 3d 30 2c 20 70 43 6f 6c 3d 70 54 61 62 2d  (i=0, pCol=pTab-
283eb 3e 61 43 6f 6c 3b 20 69 3c 70 54 61 62 2d 3e 6e  >aCol; i<pTab->n
283ec 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b  Col; i++, pCol++
283ed 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 49  ){.        if( I
283ee 73 48 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 70 43  sHiddenColumn(pC
283ef 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ol) ){.         
283f0 20 6e 48 69 64 64 65 6e 2b 2b 3b 0a 20 20 20 20   nHidden++;.    
283f1 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a        continue;.
283f2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
283f3 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
283f4 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
283f5 72 2c 20 69 2d 6e 48 69 64 64 65 6e 2c 20 31 29  r, i-nHidden, 1)
283f6 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
283f7 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
283f8 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 32 2c  P_String8, 0, 2,
283f9 20 30 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c   0, pCol->zName,
283fa 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c   0);.        sql
283fb 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
283fc 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c  , OP_String8, 0,
283fd 20 33 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20   3, 0,.         
283fe 20 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 20 3f 20    pCol->zType ? 
283ff 70 43 6f 6c 2d 3e 7a 54 79 70 65 20 3a 20 22 22  pCol->zType : ""
28400 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71  , 0);.        sq
28401 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
28402 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 28  v, OP_Integer, (
28403 70 43 6f 6c 2d 3e 6e 6f 74 4e 75 6c 6c 20 3f 20  pCol->notNull ? 
28404 31 20 3a 20 30 29 2c 20 34 29 3b 0a 20 20 20 20  1 : 0), 4);.    
28405 20 20 20 20 69 66 28 20 70 43 6f 6c 2d 3e 7a 44      if( pCol->zD
28406 66 6c 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20  flt ){.         
28407 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
28408 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
28409 2c 20 30 2c 20 35 2c 20 30 2c 20 28 63 68 61 72  , 0, 5, 0, (char
2840a 2a 29 70 43 6f 6c 2d 3e 7a 44 66 6c 74 2c 20 30  *)pCol->zDflt, 0
2840b 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  );.        }else
2840c 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
2840d 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2840e 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 35 29 3b   OP_Null, 0, 5);
2840f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
28410 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
28411 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
28412 65 72 2c 20 70 43 6f 6c 2d 3e 69 73 50 72 69 6d  er, pCol->isPrim
28413 4b 65 79 2c 20 36 29 3b 0a 20 20 20 20 20 20 20  Key, 6);.       
28414 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
28415 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52  p2(v, OP_ResultR
28416 6f 77 2c 20 31 2c 20 36 29 3b 0a 20 20 20 20 20  ow, 1, 6);.     
28417 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65   }.    }.  }else
28418 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  ..  if( sqlite3S
28419 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 69  trICmp(zLeft, "i
2841a 6e 64 65 78 5f 69 6e 66 6f 22 29 3d 3d 30 20 26  ndex_info")==0 &
2841b 26 20 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20  & zRight ){.    
2841c 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20  Index *pIdx;.   
2841d 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20   Table *pTab;.  
2841e 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 61    if( sqlite3Rea
2841f 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20  dSchema(pParse) 
28420 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75  ) goto pragma_ou
28421 74 3b 0a 20 20 20 20 70 49 64 78 20 3d 20 73 71  t;.    pIdx = sq
28422 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 64  lite3FindIndex(d
28423 62 2c 20 7a 52 69 67 68 74 2c 20 7a 44 62 29 3b  b, zRight, zDb);
28424 0a 20 20 20 20 69 66 28 20 70 49 64 78 20 29 7b  .    if( pIdx ){
28425 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  .      int i;.  
28426 20 20 20 20 70 54 61 62 20 3d 20 70 49 64 78 2d      pTab = pIdx-
28427 3e 70 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 73  >pTable;.      s
28428 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
28429 43 6f 6c 73 28 76 2c 20 33 29 3b 0a 20 20 20 20  Cols(v, 3);.    
2842a 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d    pParse->nMem =
2842b 20 33 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   3;.      sqlite
2842c 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
2842d 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 0, COLNAME_NA
2842e 4d 45 2c 20 22 73 65 71 6e 6f 22 2c 20 53 51 4c  ME, "seqno", SQL
2842f 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
28430 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
28431 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c 20 43  tColName(v, 1, C
28432 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 63 69  OLNAME_NAME, "ci
28433 64 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  d", SQLITE_STATI
28434 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  C);.      sqlite
28435 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
28436 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 2, COLNAME_NA
28437 4d 45 2c 20 22 6e 61 6d 65 22 2c 20 53 51 4c 49  ME, "name", SQLI
28438 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
28439 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 64    for(i=0; i<pId
2843a 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29  x->nColumn; i++)
2843b 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 63 6e  {.        int cn
2843c 75 6d 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c  um = pIdx->aiCol
2843d 75 6d 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20  umn[i];.        
2843e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2843f 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
28440 20 69 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20   i, 1);.        
28441 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
28442 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
28443 20 63 6e 75 6d 2c 20 32 29 3b 0a 20 20 20 20 20   cnum, 2);.     
28444 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d     assert( pTab-
28445 3e 6e 43 6f 6c 3e 63 6e 75 6d 20 29 3b 0a 20 20  >nCol>cnum );.  
28446 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
28447 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74  eAddOp4(v, OP_St
28448 72 69 6e 67 38 2c 20 30 2c 20 33 2c 20 30 2c 20  ring8, 0, 3, 0, 
28449 70 54 61 62 2d 3e 61 43 6f 6c 5b 63 6e 75 6d 5d  pTab->aCol[cnum]
2844a 2e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20  .zName, 0);.    
2844b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2844c 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75  ddOp2(v, OP_Resu
2844d 6c 74 52 6f 77 2c 20 31 2c 20 33 29 3b 0a 20 20  ltRow, 1, 3);.  
2844e 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65      }.    }.  }e
2844f 6c 73 65 0a 0a 20 20 69 66 28 20 73 71 6c 69 74  lse..  if( sqlit
28450 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
28451 20 22 69 6e 64 65 78 5f 6c 69 73 74 22 29 3d 3d   "index_list")==
28452 30 20 26 26 20 7a 52 69 67 68 74 20 29 7b 0a 20  0 && zRight ){. 
28453 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a     Index *pIdx;.
28454 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b      Table *pTab;
28455 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
28456 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73  ReadSchema(pPars
28457 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61  e) ) goto pragma
28458 5f 6f 75 74 3b 0a 20 20 20 20 70 54 61 62 20 3d  _out;.    pTab =
28459 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c   sqlite3FindTabl
2845a 65 28 64 62 2c 20 7a 52 69 67 68 74 2c 20 7a 44  e(db, zRight, zD
2845b 62 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62  b);.    if( pTab
2845c 20 29 7b 0a 20 20 20 20 20 20 76 20 3d 20 73 71   ){.      v = sq
2845d 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
2845e 72 73 65 29 3b 0a 20 20 20 20 20 20 70 49 64 78  rse);.      pIdx
2845f 20 3d 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b   = pTab->pIndex;
28460 0a 20 20 20 20 20 20 69 66 28 20 70 49 64 78 20  .      if( pIdx 
28461 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  ){.        int i
28462 20 3d 20 30 3b 20 0a 20 20 20 20 20 20 20 20 73   = 0; .        s
28463 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
28464 43 6f 6c 73 28 76 2c 20 33 29 3b 0a 20 20 20 20  Cols(v, 3);.    
28465 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d      pParse->nMem
28466 20 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 73 71   = 3;.        sq
28467 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
28468 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d  ame(v, 0, COLNAM
28469 45 5f 4e 41 4d 45 2c 20 22 73 65 71 22 2c 20 53  E_NAME, "seq", S
2846a 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
2846b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2846c 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
2846d 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  1, COLNAME_NAME,
2846e 20 22 6e 61 6d 65 22 2c 20 53 51 4c 49 54 45 5f   "name", SQLITE_
2846f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20  STATIC);.       
28470 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
28471 6f 6c 4e 61 6d 65 28 76 2c 20 32 2c 20 43 4f 4c  olName(v, 2, COL
28472 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 75 6e 69 71  NAME_NAME, "uniq
28473 75 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  ue", SQLITE_STAT
28474 49 43 29 3b 0a 20 20 20 20 20 20 20 20 77 68 69  IC);.        whi
28475 6c 65 28 70 49 64 78 29 7b 0a 20 20 20 20 20 20  le(pIdx){.      
28476 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
28477 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
28478 67 65 72 2c 20 69 2c 20 31 29 3b 0a 20 20 20 20  ger, i, 1);.    
28479 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2847a 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74  eAddOp4(v, OP_St
2847b 72 69 6e 67 38 2c 20 30 2c 20 32 2c 20 30 2c 20  ring8, 0, 2, 0, 
2847c 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b  pIdx->zName, 0);
2847d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2847e 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2847f 4f 50 5f 49 6e 74 65 67 65 72 2c 20 70 49 64 78  OP_Integer, pIdx
28480 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f  ->onError!=OE_No
28481 6e 65 2c 20 33 29 3b 0a 20 20 20 20 20 20 20 20  ne, 3);.        
28482 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
28483 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74  Op2(v, OP_Result
28484 52 6f 77 2c 20 31 2c 20 33 29 3b 0a 20 20 20 20  Row, 1, 3);.    
28485 20 20 20 20 20 20 2b 2b 69 3b 0a 20 20 20 20 20        ++i;.     
28486 20 20 20 20 20 70 49 64 78 20 3d 20 70 49 64 78       pIdx = pIdx
28487 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20  ->pNext;.       
28488 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
28489 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 69 66 28 20  .  }else..  if( 
2848a 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
2848b 4c 65 66 74 2c 20 22 64 61 74 61 62 61 73 65 5f  Left, "database_
2848c 6c 69 73 74 22 29 3d 3d 30 20 29 7b 0a 20 20 20  list")==0 ){.   
2848d 20 69 6e 74 20 69 3b 0a 20 20 20 20 69 66 28 20   int i;.    if( 
2848e 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d  sqlite3ReadSchem
2848f 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f  a(pParse) ) goto
28490 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20   pragma_out;.   
28491 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e   sqlite3VdbeSetN
28492 75 6d 43 6f 6c 73 28 76 2c 20 33 29 3b 0a 20 20  umCols(v, 3);.  
28493 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d    pParse->nMem =
28494 20 33 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   3;.    sqlite3V
28495 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
28496 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   0, COLNAME_NAME
28497 2c 20 22 73 65 71 22 2c 20 53 51 4c 49 54 45 5f  , "seq", SQLITE_
28498 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73 71 6c  STATIC);.    sql
28499 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
2849a 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45  me(v, 1, COLNAME
2849b 5f 4e 41 4d 45 2c 20 22 6e 61 6d 65 22 2c 20 53  _NAME, "name", S
2849c 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
2849d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
2849e 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 32 2c 20 43  tColName(v, 2, C
2849f 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 66 69  OLNAME_NAME, "fi
284a0 6c 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  le", SQLITE_STAT
284a1 49 43 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  IC);.    for(i=0
284a2 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
284a3 29 7b 0a 20 20 20 20 20 20 69 66 28 20 64 62 2d  ){.      if( db-
284a4 3e 61 44 62 5b 69 5d 2e 70 42 74 3d 3d 30 20 29  >aDb[i].pBt==0 )
284a5 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
284a6 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62   assert( db->aDb
284a7 5b 69 5d 2e 7a 4e 61 6d 65 21 3d 30 20 29 3b 0a  [i].zName!=0 );.
284a8 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
284a9 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
284aa 74 65 67 65 72 2c 20 69 2c 20 31 29 3b 0a 20 20  teger, i, 1);.  
284ab 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
284ac 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69  ddOp4(v, OP_Stri
284ad 6e 67 38 2c 20 30 2c 20 32 2c 20 30 2c 20 64 62  ng8, 0, 2, 0, db
284ae 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 2c 20  ->aDb[i].zName, 
284af 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  0);.      sqlite
284b0 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
284b1 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 33 2c  P_String8, 0, 3,
284b2 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 73   0,.           s
284b3 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 46 69  qlite3BtreeGetFi
284b4 6c 65 6e 61 6d 65 28 64 62 2d 3e 61 44 62 5b 69  lename(db->aDb[i
284b5 5d 2e 70 42 74 29 2c 20 30 29 3b 0a 20 20 20 20  ].pBt), 0);.    
284b6 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
284b7 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74  Op2(v, OP_Result
284b8 52 6f 77 2c 20 31 2c 20 33 29 3b 0a 20 20 20 20  Row, 1, 3);.    
284b9 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 69 66 28  }.  }else..  if(
284ba 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
284bb 7a 4c 65 66 74 2c 20 22 63 6f 6c 6c 61 74 69 6f  zLeft, "collatio
284bc 6e 5f 6c 69 73 74 22 29 3d 3d 30 20 29 7b 0a 20  n_list")==0 ){. 
284bd 20 20 20 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20     int i = 0;.  
284be 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 3b 0a 20    HashElem *p;. 
284bf 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
284c0 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 32 29 3b 0a  tNumCols(v, 2);.
284c1 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d      pParse->nMem
284c2 20 3d 20 32 3b 0a 20 20 20 20 73 71 6c 69 74 65   = 2;.    sqlite
284c3 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
284c4 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 0, COLNAME_NA
284c5 4d 45 2c 20 22 73 65 71 22 2c 20 53 51 4c 49 54  ME, "seq", SQLIT
284c6 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73  E_STATIC);.    s
284c7 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
284c8 4e 61 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e 41  Name(v, 1, COLNA
284c9 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d 65 22 2c  ME_NAME, "name",
284ca 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
284cb 0a 20 20 20 20 66 6f 72 28 70 3d 73 71 6c 69 74  .    for(p=sqlit
284cc 65 48 61 73 68 46 69 72 73 74 28 26 64 62 2d 3e  eHashFirst(&db->
284cd 61 43 6f 6c 6c 53 65 71 29 3b 20 70 3b 20 70 3d  aCollSeq); p; p=
284ce 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70  sqliteHashNext(p
284cf 29 29 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65  )){.      CollSe
284d0 71 20 2a 70 43 6f 6c 6c 20 3d 20 28 43 6f 6c 6c  q *pColl = (Coll
284d1 53 65 71 20 2a 29 73 71 6c 69 74 65 48 61 73 68  Seq *)sqliteHash
284d2 44 61 74 61 28 70 29 3b 0a 20 20 20 20 20 20 73  Data(p);.      s
284d3 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
284d4 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
284d5 69 2b 2b 2c 20 31 29 3b 0a 20 20 20 20 20 20 73  i++, 1);.      s
284d6 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
284d7 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20  (v, OP_String8, 
284d8 30 2c 20 32 2c 20 30 2c 20 70 43 6f 6c 6c 2d 3e  0, 2, 0, pColl->
284d9 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20  zName, 0);.     
284da 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
284db 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52  p2(v, OP_ResultR
284dc 6f 77 2c 20 31 2c 20 32 29 3b 0a 20 20 20 20 7d  ow, 1, 2);.    }
284dd 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20  .  }else.#endif 
284de 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  /* SQLITE_OMIT_S
284df 43 48 45 4d 41 5f 50 52 41 47 4d 41 53 20 2a 2f  CHEMA_PRAGMAS */
284e0 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
284e1 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45  _OMIT_FOREIGN_KE
284e2 59 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  Y.  if( sqlite3S
284e3 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 66  trICmp(zLeft, "f
284e4 6f 72 65 69 67 6e 5f 6b 65 79 5f 6c 69 73 74 22  oreign_key_list"
284e5 29 3d 3d 30 20 26 26 20 7a 52 69 67 68 74 20 29  )==0 && zRight )
284e6 7b 0a 20 20 20 20 46 4b 65 79 20 2a 70 46 4b 3b  {.    FKey *pFK;
284e7 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62  .    Table *pTab
284e8 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
284e9 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72  3ReadSchema(pPar
284ea 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d  se) ) goto pragm
284eb 61 5f 6f 75 74 3b 0a 20 20 20 20 70 54 61 62 20  a_out;.    pTab 
284ec 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62  = sqlite3FindTab
284ed 6c 65 28 64 62 2c 20 7a 52 69 67 68 74 2c 20 7a  le(db, zRight, z
284ee 44 62 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61  Db);.    if( pTa
284ef 62 20 29 7b 0a 20 20 20 20 20 20 76 20 3d 20 73  b ){.      v = s
284f0 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
284f1 61 72 73 65 29 3b 0a 20 20 20 20 20 20 70 46 4b  arse);.      pFK
284f2 20 3d 20 70 54 61 62 2d 3e 70 46 4b 65 79 3b 0a   = pTab->pFKey;.
284f3 20 20 20 20 20 20 69 66 28 20 70 46 4b 20 29 7b        if( pFK ){
284f4 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 20 3d  .        int i =
284f5 20 30 3b 20 0a 20 20 20 20 20 20 20 20 73 71 6c   0; .        sql
284f6 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f  ite3VdbeSetNumCo
284f7 6c 73 28 76 2c 20 38 29 3b 0a 20 20 20 20 20 20  ls(v, 8);.      
284f8 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d    pParse->nMem =
284f9 20 38 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   8;.        sqli
284fa 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
284fb 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 0, COLNAME_
284fc 4e 41 4d 45 2c 20 22 69 64 22 2c 20 53 51 4c 49  NAME, "id", SQLI
284fd 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
284fe 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
284ff 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c 20  etColName(v, 1, 
28500 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73  COLNAME_NAME, "s
28501 65 71 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  eq", SQLITE_STAT
28502 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  IC);.        sql
28503 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
28504 6d 65 28 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45  me(v, 2, COLNAME
28505 5f 4e 41 4d 45 2c 20 22 74 61 62 6c 65 22 2c 20  _NAME, "table", 
28506 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
28507 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
28508 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
28509 20 33 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   3, COLNAME_NAME
2850a 2c 20 22 66 72 6f 6d 22 2c 20 53 51 4c 49 54 45  , "from", SQLITE
2850b 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
2850c 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
2850d 43 6f 6c 4e 61 6d 65 28 76 2c 20 34 2c 20 43 4f  ColName(v, 4, CO
2850e 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 74 6f 22  LNAME_NAME, "to"
2850f 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
28510 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
28511 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
28512 76 2c 20 35 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 5, COLNAME_NA
28513 4d 45 2c 20 22 6f 6e 5f 75 70 64 61 74 65 22 2c  ME, "on_update",
28514 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
28515 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
28516 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
28517 2c 20 36 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 6, COLNAME_NAM
28518 45 2c 20 22 6f 6e 5f 64 65 6c 65 74 65 22 2c 20  E, "on_delete", 
28519 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
2851a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2851b 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
2851c 20 37 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   7, COLNAME_NAME
2851d 2c 20 22 6d 61 74 63 68 22 2c 20 53 51 4c 49 54  , "match", SQLIT
2851e 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  E_STATIC);.     
2851f 20 20 20 77 68 69 6c 65 28 70 46 4b 29 7b 0a 20     while(pFK){. 
28520 20 20 20 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a           int j;.
28521 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d            for(j=
28522 30 3b 20 6a 3c 70 46 4b 2d 3e 6e 43 6f 6c 3b 20  0; j<pFK->nCol; 
28523 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  j++){.          
28524 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20 3d 20 70    char *zCol = p
28525 46 4b 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 43 6f 6c  FK->aCol[j].zCol
28526 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 68  ;.            ch
28527 61 72 20 2a 7a 4f 6e 55 70 64 61 74 65 20 3d 20  ar *zOnUpdate = 
28528 28 63 68 61 72 20 2a 29 61 63 74 69 6f 6e 4e 61  (char *)actionNa
28529 6d 65 28 70 46 4b 2d 3e 75 70 64 61 74 65 43 6f  me(pFK->updateCo
2852a 6e 66 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  nf);.           
2852b 20 63 68 61 72 20 2a 7a 4f 6e 44 65 6c 65 74 65   char *zOnDelete
2852c 20 3d 20 28 63 68 61 72 20 2a 29 61 63 74 69 6f   = (char *)actio
2852d 6e 4e 61 6d 65 28 70 46 4b 2d 3e 64 65 6c 65 74  nName(pFK->delet
2852e 65 43 6f 6e 66 29 3b 0a 20 20 20 20 20 20 20 20  eConf);.        
2852f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
28530 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
28531 67 65 72 2c 20 69 2c 20 31 29 3b 0a 20 20 20 20  ger, i, 1);.    
28532 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
28533 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
28534 49 6e 74 65 67 65 72 2c 20 6a 2c 20 32 29 3b 0a  Integer, j, 2);.
28535 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
28536 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
28537 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20   OP_String8, 0, 
28538 33 2c 20 30 2c 20 70 46 4b 2d 3e 7a 54 6f 2c 20  3, 0, pFK->zTo, 
28539 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  0);.            
2853a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2853b 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  4(v, OP_String8,
2853c 20 30 2c 20 34 2c 20 30 2c 0a 20 20 20 20 20 20   0, 4, 0,.      
2853d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2853e 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e 61 43          pTab->aC
2853f 6f 6c 5b 70 46 4b 2d 3e 61 43 6f 6c 5b 6a 5d 2e  ol[pFK->aCol[j].
28540 69 46 72 6f 6d 5d 2e 7a 4e 61 6d 65 2c 20 30 29  iFrom].zName, 0)
28541 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ;.            sq
28542 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
28543 76 2c 20 7a 43 6f 6c 20 3f 20 4f 50 5f 53 74 72  v, zCol ? OP_Str
28544 69 6e 67 38 20 3a 20 4f 50 5f 4e 75 6c 6c 2c 20  ing8 : OP_Null, 
28545 30 2c 20 35 2c 20 30 2c 20 7a 43 6f 6c 2c 20 30  0, 5, 0, zCol, 0
28546 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  );.            s
28547 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
28548 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20  (v, OP_String8, 
28549 30 2c 20 36 2c 20 30 2c 20 7a 4f 6e 55 70 64 61  0, 6, 0, zOnUpda
2854a 74 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  te, 0);.        
2854b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2854c 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69  ddOp4(v, OP_Stri
2854d 6e 67 38 2c 20 30 2c 20 37 2c 20 30 2c 20 7a 4f  ng8, 0, 7, 0, zO
2854e 6e 44 65 6c 65 74 65 2c 20 30 29 3b 0a 20 20 20  nDelete, 0);.   
2854f 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28550 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
28551 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 38 2c 20  _String8, 0, 8, 
28552 30 2c 20 22 4e 4f 4e 45 22 2c 20 30 29 3b 0a 20  0, "NONE", 0);. 
28553 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28554 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
28555 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c  OP_ResultRow, 1,
28556 20 38 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d   8);.          }
28557 0a 20 20 20 20 20 20 20 20 20 20 2b 2b 69 3b 0a  .          ++i;.
28558 20 20 20 20 20 20 20 20 20 20 70 46 4b 20 3d 20            pFK = 
28559 70 46 4b 2d 3e 70 4e 65 78 74 46 72 6f 6d 3b 0a  pFK->pNextFrom;.
2855a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2855b 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a  }.    }.  }else.
2855c 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e  #endif /* !defin
2855d 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ed(SQLITE_OMIT_F
2855e 4f 52 45 49 47 4e 5f 4b 45 59 29 20 2a 2f 0a 0a  OREIGN_KEY) */..
2855f 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
28560 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
28561 43 6d 70 28 7a 4c 65 66 74 2c 20 22 70 61 72 73  Cmp(zLeft, "pars
28562 65 72 5f 74 72 61 63 65 22 29 3d 3d 30 20 29 7b  er_trace")==0 ){
28563 0a 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 20  .    if( zRight 
28564 29 7b 0a 20 20 20 20 20 20 69 66 28 20 67 65 74  ){.      if( get
28565 42 6f 6f 6c 65 61 6e 28 7a 52 69 67 68 74 29 20  Boolean(zRight) 
28566 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
28567 65 33 50 61 72 73 65 72 54 72 61 63 65 28 73 74  e3ParserTrace(st
28568 64 65 72 72 2c 20 22 70 61 72 73 65 72 3a 20 22  derr, "parser: "
28569 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
2856a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50          sqlite3P
2856b 61 72 73 65 72 54 72 61 63 65 28 30 2c 20 30 29  arserTrace(0, 0)
2856c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2856d 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a    }else.#endif..
2856e 20 20 2f 2a 20 52 65 69 6e 73 74 61 6c 6c 20 74    /* Reinstall t
2856f 68 65 20 4c 49 4b 45 20 61 6e 64 20 47 4c 4f 42  he LIKE and GLOB
28570 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 65   functions.  The
28571 20 76 61 72 69 61 6e 74 20 6f 66 20 4c 49 4b 45   variant of LIKE
28572 0a 20 20 2a 2a 20 75 73 65 64 20 77 69 6c 6c 20  .  ** used will 
28573 62 65 20 63 61 73 65 20 73 65 6e 73 69 74 69 76  be case sensitiv
28574 65 20 6f 72 20 6e 6f 74 20 64 65 70 65 6e 64 69  e or not dependi
28575 6e 67 20 6f 6e 20 74 68 65 20 52 48 53 2e 0a 20  ng on the RHS.. 
28576 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
28577 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20  3StrICmp(zLeft, 
28578 22 63 61 73 65 5f 73 65 6e 73 69 74 69 76 65 5f  "case_sensitive_
28579 6c 69 6b 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20  like")==0 ){.   
2857a 20 69 66 28 20 7a 52 69 67 68 74 20 29 7b 0a 20   if( zRight ){. 
2857b 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 67 69       sqlite3Regi
2857c 73 74 65 72 4c 69 6b 65 46 75 6e 63 74 69 6f 6e  sterLikeFunction
2857d 73 28 64 62 2c 20 67 65 74 42 6f 6f 6c 65 61 6e  s(db, getBoolean
2857e 28 7a 52 69 67 68 74 29 29 3b 0a 20 20 20 20 7d  (zRight));.    }
2857f 0a 20 20 7d 65 6c 73 65 0a 0a 23 69 66 6e 64 65  .  }else..#ifnde
28580 66 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 52 49  f SQLITE_INTEGRI
28581 54 59 5f 43 48 45 43 4b 5f 45 52 52 4f 52 5f 4d  TY_CHECK_ERROR_M
28582 41 58 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  AX.# define SQLI
28583 54 45 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45  TE_INTEGRITY_CHE
28584 43 4b 5f 45 52 52 4f 52 5f 4d 41 58 20 31 30 30  CK_ERROR_MAX 100
28585 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66  .#endif..#ifndef
28586 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54   SQLITE_OMIT_INT
28587 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 20 20 2f  EGRITY_CHECK.  /
28588 2a 20 50 72 61 67 6d 61 20 22 71 75 69 63 6b 5f  * Pragma "quick_
28589 63 68 65 63 6b 22 20 69 73 20 61 6e 20 65 78 70  check" is an exp
2858a 65 72 69 6d 65 6e 74 61 6c 20 72 65 64 75 63 65  erimental reduce
2858b 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 0a 20 20  d version of .  
2858c 2a 2a 20 69 6e 74 65 67 72 69 74 79 5f 63 68 65  ** integrity_che
2858d 63 6b 20 64 65 73 69 67 6e 65 64 20 74 6f 20 64  ck designed to d
2858e 65 74 65 63 74 20 6d 6f 73 74 20 64 61 74 61 62  etect most datab
2858f 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 0a 20  ase corruption. 
28590 20 2a 2a 20 77 69 74 68 6f 75 74 20 6d 6f 73 74   ** without most
28591 20 6f 66 20 74 68 65 20 6f 76 65 72 68 65 61 64   of the overhead
28592 20 6f 66 20 61 20 66 75 6c 6c 20 69 6e 74 65 67   of a full integ
28593 72 69 74 79 2d 63 68 65 63 6b 2e 0a 20 20 2a 2f  rity-check..  */
28594 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
28595 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 69 6e  rICmp(zLeft, "in
28596 74 65 67 72 69 74 79 5f 63 68 65 63 6b 22 29 3d  tegrity_check")=
28597 3d 30 0a 20 20 20 7c 7c 20 73 71 6c 69 74 65 33  =0.   || sqlite3
28598 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22  StrICmp(zLeft, "
28599 71 75 69 63 6b 5f 63 68 65 63 6b 22 29 3d 3d 30  quick_check")==0
2859a 20 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20 69   .  ){.    int i
2859b 2c 20 6a 2c 20 61 64 64 72 2c 20 6d 78 45 72 72  , j, addr, mxErr
2859c 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f 64 65 20 74  ;..    /* Code t
2859d 68 61 74 20 61 70 70 65 61 72 73 20 61 74 20 74  hat appears at t
2859e 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69 6e  he end of the in
2859f 74 65 67 72 69 74 79 20 63 68 65 63 6b 2e 20 20  tegrity check.  
285a0 49 66 20 6e 6f 20 65 72 72 6f 72 0a 20 20 20 20  If no error.    
285a1 2a 2a 20 6d 65 73 73 61 67 65 73 20 68 61 76 65  ** messages have
285a2 20 62 65 65 6e 20 67 65 6e 65 72 61 74 65 64 2c   been generated,
285a3 20 6f 75 74 70 75 74 20 4f 4b 2e 20 20 4f 74 68   output OK.  Oth
285a4 65 72 77 69 73 65 20 6f 75 74 70 75 74 20 74 68  erwise output th
285a5 65 0a 20 20 20 20 2a 2a 20 65 72 72 6f 72 20 6d  e.    ** error m
285a6 65 73 73 61 67 65 0a 20 20 20 20 2a 2f 0a 20 20  essage.    */.  
285a7 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 56    static const V
285a8 64 62 65 4f 70 4c 69 73 74 20 65 6e 64 43 6f 64  dbeOpList endCod
285a9 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 7b 20  e[] = {.      { 
285aa 4f 50 5f 41 64 64 49 6d 6d 2c 20 20 20 20 20 20  OP_AddImm,      
285ab 31 2c 20 30 2c 20 20 20 20 20 20 20 20 30 7d 2c  1, 0,        0},
285ac 20 20 20 20 2f 2a 20 30 20 2a 2f 0a 20 20 20 20      /* 0 */.    
285ad 20 20 7b 20 4f 50 5f 49 66 4e 65 67 2c 20 20 20    { OP_IfNeg,   
285ae 20 20 20 20 31 2c 20 30 2c 20 20 20 20 20 20 20      1, 0,       
285af 20 30 7d 2c 20 20 20 20 2f 2a 20 31 20 2a 2f 0a   0},    /* 1 */.
285b0 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e        { OP_Strin
285b1 67 38 2c 20 20 20 20 20 30 2c 20 33 2c 20 20 20  g8,     0, 3,   
285b2 20 20 20 20 20 30 7d 2c 20 20 20 20 2f 2a 20 32       0},    /* 2
285b3 20 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f 52   */.      { OP_R
285b4 65 73 75 6c 74 52 6f 77 2c 20 20 20 33 2c 20 31  esultRow,   3, 1
285b5 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a 20 20 20  ,        0},.   
285b6 20 7d 3b 0a 0a 20 20 20 20 69 6e 74 20 69 73 51   };..    int isQ
285b7 75 69 63 6b 20 3d 20 28 7a 4c 65 66 74 5b 30 5d  uick = (zLeft[0]
285b8 3d 3d 27 71 27 29 3b 0a 0a 20 20 20 20 2f 2a 20  =='q');..    /* 
285b9 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 56  Initialize the V
285ba 44 42 45 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20  DBE program */. 
285bb 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65     if( sqlite3Re
285bc 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29  adSchema(pParse)
285bd 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f   ) goto pragma_o
285be 75 74 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  ut;.    pParse->
285bf 6e 4d 65 6d 20 3d 20 36 3b 0a 20 20 20 20 73 71  nMem = 6;.    sq
285c0 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43  lite3VdbeSetNumC
285c1 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 73  ols(v, 1);.    s
285c2 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
285c3 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41  Name(v, 0, COLNA
285c4 4d 45 5f 4e 41 4d 45 2c 20 22 69 6e 74 65 67 72  ME_NAME, "integr
285c5 69 74 79 5f 63 68 65 63 6b 22 2c 20 53 51 4c 49  ity_check", SQLI
285c6 54 45 5f 53 54 41 54 49 43 29 3b 0a 0a 20 20 20  TE_STATIC);..   
285c7 20 2f 2a 20 53 65 74 20 74 68 65 20 6d 61 78 69   /* Set the maxi
285c8 6d 75 6d 20 65 72 72 6f 72 20 63 6f 75 6e 74 20  mum error count 
285c9 2a 2f 0a 20 20 20 20 6d 78 45 72 72 20 3d 20 53  */.    mxErr = S
285ca 51 4c 49 54 45 5f 49 4e 54 45 47 52 49 54 59 5f  QLITE_INTEGRITY_
285cb 43 48 45 43 4b 5f 45 52 52 4f 52 5f 4d 41 58 3b  CHECK_ERROR_MAX;
285cc 0a 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 20  .    if( zRight 
285cd 29 7b 0a 20 20 20 20 20 20 6d 78 45 72 72 20 3d  ){.      mxErr =
285ce 20 61 74 6f 69 28 7a 52 69 67 68 74 29 3b 0a 20   atoi(zRight);. 
285cf 20 20 20 20 20 69 66 28 20 6d 78 45 72 72 3c 3d       if( mxErr<=
285d0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 78 45  0 ){.        mxE
285d1 72 72 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45  rr = SQLITE_INTE
285d2 47 52 49 54 59 5f 43 48 45 43 4b 5f 45 52 52 4f  GRITY_CHECK_ERRO
285d3 52 5f 4d 41 58 3b 0a 20 20 20 20 20 20 7d 0a 20  R_MAX;.      }. 
285d4 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
285d5 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
285d6 5f 49 6e 74 65 67 65 72 2c 20 6d 78 45 72 72 2c  _Integer, mxErr,
285d7 20 31 29 3b 20 20 2f 2a 20 72 65 67 5b 31 5d 20   1);  /* reg[1] 
285d8 68 6f 6c 64 73 20 65 72 72 6f 72 73 20 6c 65 66  holds errors lef
285d9 74 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 44 6f 20  t */..    /* Do 
285da 61 6e 20 69 6e 74 65 67 72 69 74 79 20 63 68 65  an integrity che
285db 63 6b 20 6f 6e 20 65 61 63 68 20 64 61 74 61 62  ck on each datab
285dc 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20  ase file */.    
285dd 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e  for(i=0; i<db->n
285de 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  Db; i++){.      
285df 48 61 73 68 45 6c 65 6d 20 2a 78 3b 0a 20 20 20  HashElem *x;.   
285e0 20 20 20 48 61 73 68 20 2a 70 54 62 6c 73 3b 0a     Hash *pTbls;.
285e1 20 20 20 20 20 20 69 6e 74 20 63 6e 74 20 3d 20        int cnt = 
285e2 30 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 4f 4d  0;..      if( OM
285e3 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 3d 3d  IT_TEMPDB && i==
285e4 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 0a 20  1 ) continue;.. 
285e5 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65       sqlite3Code
285e6 56 65 72 69 66 79 53 63 68 65 6d 61 28 70 50 61  VerifySchema(pPa
285e7 72 73 65 2c 20 69 29 3b 0a 20 20 20 20 20 20 61  rse, i);.      a
285e8 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
285e9 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66  eAddOp1(v, OP_If
285ea 50 6f 73 2c 20 31 29 3b 20 2f 2a 20 48 61 6c 74  Pos, 1); /* Halt
285eb 20 69 66 20 6f 75 74 20 6f 66 20 65 72 72 6f 72   if out of error
285ec 73 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74  s */.      sqlit
285ed 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
285ee 4f 50 5f 48 61 6c 74 2c 20 30 2c 20 30 29 3b 0a  OP_Halt, 0, 0);.
285ef 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
285f0 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
285f1 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 44 6f  r);..      /* Do
285f2 20 61 6e 20 69 6e 74 65 67 72 69 74 79 20 63 68   an integrity ch
285f3 65 63 6b 20 6f 66 20 74 68 65 20 42 2d 54 72 65  eck of the B-Tre
285f4 65 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  e.      **.     
285f5 20 2a 2a 20 42 65 67 69 6e 20 62 79 20 66 69 6c   ** Begin by fil
285f6 6c 69 6e 67 20 72 65 67 69 73 74 65 72 73 20 32  ling registers 2
285f7 2c 20 33 2c 20 2e 2e 2e 20 77 69 74 68 20 74 68  , 3, ... with th
285f8 65 20 72 6f 6f 74 20 70 61 67 65 73 20 6e 75 6d  e root pages num
285f9 62 65 72 73 0a 20 20 20 20 20 20 2a 2a 20 66 6f  bers.      ** fo
285fa 72 20 61 6c 6c 20 74 61 62 6c 65 73 20 61 6e 64  r all tables and
285fb 20 69 6e 64 69 63 65 73 20 69 6e 20 74 68 65 20   indices in the 
285fc 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 20 20  database..      
285fd 2a 2f 0a 20 20 20 20 20 20 70 54 62 6c 73 20 3d  */.      pTbls =
285fe 20 26 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 53 63   &db->aDb[i].pSc
285ff 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 3b 0a 20  hema->tblHash;. 
28600 20 20 20 20 20 66 6f 72 28 78 3d 73 71 6c 69 74       for(x=sqlit
28601 65 48 61 73 68 46 69 72 73 74 28 70 54 62 6c 73  eHashFirst(pTbls
28602 29 3b 20 78 3b 20 78 3d 73 71 6c 69 74 65 48 61  ); x; x=sqliteHa
28603 73 68 4e 65 78 74 28 78 29 29 7b 0a 20 20 20 20  shNext(x)){.    
28604 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20      Table *pTab 
28605 3d 20 73 71 6c 69 74 65 48 61 73 68 44 61 74 61  = sqliteHashData
28606 28 78 29 3b 0a 20 20 20 20 20 20 20 20 49 6e 64  (x);.        Ind
28607 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 20 20  ex *pIdx;.      
28608 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
28609 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
2860a 72 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 32  r, pTab->tnum, 2
2860b 2b 63 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 63  +cnt);.        c
2860c 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 66 6f  nt++;.        fo
2860d 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  r(pIdx=pTab->pIn
2860e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
2860f 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  pIdx->pNext){.  
28610 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
28611 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
28612 49 6e 74 65 67 65 72 2c 20 70 49 64 78 2d 3e 74  Integer, pIdx->t
28613 6e 75 6d 2c 20 32 2b 63 6e 74 29 3b 0a 20 20 20  num, 2+cnt);.   
28614 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20         cnt++;.  
28615 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
28616 0a 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73  .      /* Make s
28617 75 72 65 20 73 75 66 66 69 63 69 65 6e 74 20 6e  ure sufficient n
28618 75 6d 62 65 72 20 6f 66 20 72 65 67 69 73 74 65  umber of registe
28619 72 73 20 68 61 76 65 20 62 65 65 6e 20 61 6c 6c  rs have been all
2861a 6f 63 61 74 65 64 20 2a 2f 0a 20 20 20 20 20 20  ocated */.      
2861b 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  if( pParse->nMem
2861c 20 3c 20 63 6e 74 2b 34 20 29 7b 0a 20 20 20 20   < cnt+4 ){.    
2861d 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d      pParse->nMem
2861e 20 3d 20 63 6e 74 2b 34 3b 0a 20 20 20 20 20 20   = cnt+4;.      
2861f 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 44 6f 20 74  }..      /* Do t
28620 68 65 20 62 2d 74 72 65 65 20 69 6e 74 65 67 72  he b-tree integr
28621 69 74 79 20 63 68 65 63 6b 73 20 2a 2f 0a 20 20  ity checks */.  
28622 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
28623 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp3(v, OP_Inte
28624 67 72 69 74 79 43 6b 2c 20 32 2c 20 63 6e 74 2c  grityCk, 2, cnt,
28625 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   1);.      sqlit
28626 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76  e3VdbeChangeP5(v
28627 2c 20 28 75 38 29 69 29 3b 0a 20 20 20 20 20 20  , (u8)i);.      
28628 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
28629 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49  beAddOp1(v, OP_I
2862a 73 4e 75 6c 6c 2c 20 32 29 3b 0a 20 20 20 20 20  sNull, 2);.     
2862b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2862c 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
2862d 2c 20 30 2c 20 33 2c 20 30 2c 0a 20 20 20 20 20  , 0, 3, 0,.     
2862e 20 20 20 20 73 71 6c 69 74 65 33 4d 50 72 69 6e      sqlite3MPrin
2862f 74 66 28 64 62 2c 20 22 2a 2a 2a 20 69 6e 20 64  tf(db, "*** in d
28630 61 74 61 62 61 73 65 20 25 73 20 2a 2a 2a 5c 6e  atabase %s ***\n
28631 22 2c 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e  ", db->aDb[i].zN
28632 61 6d 65 29 2c 0a 20 20 20 20 20 20 20 20 20 50  ame),.         P
28633 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20  4_DYNAMIC);.    
28634 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
28635 4f 70 33 28 76 2c 20 4f 50 5f 4d 6f 76 65 2c 20  Op3(v, OP_Move, 
28636 32 2c 20 34 2c 20 31 29 3b 0a 20 20 20 20 20 20  2, 4, 1);.      
28637 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
28638 33 28 76 2c 20 4f 50 5f 43 6f 6e 63 61 74 2c 20  3(v, OP_Concat, 
28639 34 2c 20 33 2c 20 32 29 3b 0a 20 20 20 20 20 20  4, 3, 2);.      
2863a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2863b 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f  2(v, OP_ResultRo
2863c 77 2c 20 32 2c 20 31 29 3b 0a 20 20 20 20 20 20  w, 2, 1);.      
2863d 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
2863e 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 0a 20  ere(v, addr);.. 
2863f 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72       /* Make sur
28640 65 20 61 6c 6c 20 74 68 65 20 69 6e 64 69 63 65  e all the indice
28641 73 20 61 72 65 20 63 6f 6e 73 74 72 75 63 74 65  s are constructe
28642 64 20 63 6f 72 72 65 63 74 6c 79 2e 0a 20 20 20  d correctly..   
28643 20 20 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28     */.      for(
28644 78 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73  x=sqliteHashFirs
28645 74 28 70 54 62 6c 73 29 3b 20 78 20 26 26 20 21  t(pTbls); x && !
28646 69 73 51 75 69 63 6b 3b 20 78 3d 73 71 6c 69 74  isQuick; x=sqlit
28647 65 48 61 73 68 4e 65 78 74 28 78 29 29 7b 0a 20  eHashNext(x)){. 
28648 20 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54         Table *pT
28649 61 62 20 3d 20 73 71 6c 69 74 65 48 61 73 68 44  ab = sqliteHashD
2864a 61 74 61 28 78 29 3b 0a 20 20 20 20 20 20 20 20  ata(x);.        
2864b 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20  Index *pIdx;.   
2864c 20 20 20 20 20 69 6e 74 20 6c 6f 6f 70 54 6f 70       int loopTop
2864d 3b 0a 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  ;..        if( p
2864e 54 61 62 2d 3e 70 49 6e 64 65 78 3d 3d 30 20 29  Tab->pIndex==0 )
2864f 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
28650 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
28651 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
28652 50 5f 49 66 50 6f 73 2c 20 31 29 3b 20 20 2f 2a  P_IfPos, 1);  /*
28653 20 53 74 6f 70 20 69 66 20 6f 75 74 20 6f 66 20   Stop if out of 
28654 65 72 72 6f 72 73 20 2a 2f 0a 20 20 20 20 20 20  errors */.      
28655 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
28656 4f 70 32 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20  Op2(v, OP_Halt, 
28657 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73  0, 0);.        s
28658 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
28659 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 20 20 20  re(v, addr);.   
2865a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e       sqlite3Open
2865b 54 61 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 28  TableAndIndices(
2865c 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 31 2c  pParse, pTab, 1,
2865d 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29 3b 0a 20   OP_OpenRead);. 
2865e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2865f 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
28660 6e 74 65 67 65 72 2c 20 30 2c 20 32 29 3b 20 20  nteger, 0, 2);  
28661 2f 2a 20 72 65 67 28 32 29 20 77 69 6c 6c 20 63  /* reg(2) will c
28662 6f 75 6e 74 20 65 6e 74 72 69 65 73 20 2a 2f 0a  ount entries */.
28663 20 20 20 20 20 20 20 20 6c 6f 6f 70 54 6f 70 20          loopTop 
28664 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
28665 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64  Op2(v, OP_Rewind
28666 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  , 1, 0);.       
28667 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
28668 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c  p2(v, OP_AddImm,
28669 20 32 2c 20 31 29 3b 20 20 20 2f 2a 20 69 6e 63   2, 1);   /* inc
2866a 72 65 6d 65 6e 74 20 65 6e 74 72 79 20 63 6f 75  rement entry cou
2866b 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f  nt */.        fo
2866c 72 28 6a 3d 30 2c 20 70 49 64 78 3d 70 54 61 62  r(j=0, pIdx=pTab
2866d 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
2866e 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
2866f 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  , j++){.        
28670 20 20 69 6e 74 20 6a 6d 70 32 3b 0a 20 20 20 20    int jmp2;.    
28671 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e        static con
28672 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20 69 64  st VdbeOpList id
28673 78 45 72 72 5b 5d 20 3d 20 7b 0a 20 20 20 20 20  xErr[] = {.     
28674 20 20 20 20 20 20 20 7b 20 4f 50 5f 41 64 64 49         { OP_AddI
28675 6d 6d 2c 20 20 20 20 20 20 31 2c 20 2d 31 2c 20  mm,      1, -1, 
28676 20 30 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20   0},.           
28677 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20   { OP_String8,  
28678 20 20 20 30 2c 20 20 33 2c 20 20 30 7d 2c 20 20     0,  3,  0},  
28679 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20 20 20    /* 1 */.      
2867a 20 20 20 20 20 20 7b 20 4f 50 5f 52 6f 77 69 64        { OP_Rowid
2867b 2c 20 20 20 20 20 20 20 31 2c 20 20 34 2c 20 20  ,       1,  4,  
2867c 30 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  0},.            
2867d 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20 20  { OP_String8,   
2867e 20 20 30 2c 20 20 35 2c 20 20 30 7d 2c 20 20 20    0,  5,  0},   
2867f 20 2f 2a 20 33 20 2a 2f 0a 20 20 20 20 20 20 20   /* 3 */.       
28680 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67       { OP_String
28681 38 2c 20 20 20 20 20 30 2c 20 20 36 2c 20 20 30  8,     0,  6,  0
28682 7d 2c 20 20 20 20 2f 2a 20 34 20 2a 2f 0a 20 20  },    /* 4 */.  
28683 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 43            { OP_C
28684 6f 6e 63 61 74 2c 20 20 20 20 20 20 34 2c 20 20  oncat,      4,  
28685 33 2c 20 20 33 7d 2c 0a 20 20 20 20 20 20 20 20  3,  3},.        
28686 20 20 20 20 7b 20 4f 50 5f 43 6f 6e 63 61 74 2c      { OP_Concat,
28687 20 20 20 20 20 20 35 2c 20 20 33 2c 20 20 33 7d        5,  3,  3}
28688 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  ,.            { 
28689 4f 50 5f 43 6f 6e 63 61 74 2c 20 20 20 20 20 20  OP_Concat,      
2868a 36 2c 20 20 33 2c 20 20 33 7d 2c 0a 20 20 20 20  6,  3,  3},.    
2868b 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 73          { OP_Res
2868c 75 6c 74 52 6f 77 2c 20 20 20 33 2c 20 20 31 2c  ultRow,   3,  1,
2868d 20 20 30 7d 2c 0a 20 20 20 20 20 20 20 20 20 20    0},.          
2868e 20 20 7b 20 4f 50 5f 49 66 50 6f 73 2c 20 20 20    { OP_IfPos,   
2868f 20 20 20 20 31 2c 20 20 30 2c 20 20 30 7d 2c 20      1,  0,  0}, 
28690 20 20 20 2f 2a 20 39 20 2a 2f 0a 20 20 20 20 20     /* 9 */.     
28691 20 20 20 20 20 20 20 7b 20 4f 50 5f 48 61 6c 74         { OP_Halt
28692 2c 20 20 20 20 20 20 20 20 30 2c 20 20 30 2c 20  ,        0,  0, 
28693 20 30 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 7d   0},.          }
28694 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
28695 74 65 33 47 65 6e 65 72 61 74 65 49 6e 64 65 78  te3GenerateIndex
28696 4b 65 79 28 70 50 61 72 73 65 2c 20 70 49 64 78  Key(pParse, pIdx
28697 2c 20 31 2c 20 33 2c 20 31 29 3b 0a 20 20 20 20  , 1, 3, 1);.    
28698 20 20 20 20 20 20 6a 6d 70 32 20 3d 20 73 71 6c        jmp2 = sql
28699 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2869a 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 6a 2b 32 2c  , OP_Found, j+2,
2869b 20 30 2c 20 33 29 3b 0a 20 20 20 20 20 20 20 20   0, 3);.        
2869c 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33    addr = sqlite3
2869d 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76 2c  VdbeAddOpList(v,
2869e 20 41 72 72 61 79 53 69 7a 65 28 69 64 78 45 72   ArraySize(idxEr
2869f 72 29 2c 20 69 64 78 45 72 72 29 3b 0a 20 20 20  r), idxErr);.   
286a0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
286a1 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64  beChangeP4(v, ad
286a2 64 72 2b 31 2c 20 22 72 6f 77 69 64 20 22 2c 20  dr+1, "rowid ", 
286a3 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  P4_STATIC);.    
286a4 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
286a5 65 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64  eChangeP4(v, add
286a6 72 2b 33 2c 20 22 20 6d 69 73 73 69 6e 67 20 66  r+3, " missing f
286a7 72 6f 6d 20 69 6e 64 65 78 20 22 2c 20 50 34 5f  rom index ", P4_
286a8 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20  STATIC);.       
286a9 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
286aa 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2b 34  angeP4(v, addr+4
286ab 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 50  , pIdx->zName, P
286ac 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  4_STATIC);.     
286ad 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
286ae 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
286af 2b 39 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  +9);.          s
286b0 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
286b1 72 65 28 76 2c 20 6a 6d 70 32 29 3b 0a 20 20 20  re(v, jmp2);.   
286b2 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73       }.        s
286b3 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
286b4 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 31 2c 20  (v, OP_Next, 1, 
286b5 6c 6f 6f 70 54 6f 70 2b 31 29 3b 0a 20 20 20 20  loopTop+1);.    
286b6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
286b7 75 6d 70 48 65 72 65 28 76 2c 20 6c 6f 6f 70 54  umpHere(v, loopT
286b8 6f 70 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  op);.        for
286b9 28 6a 3d 30 2c 20 70 49 64 78 3d 70 54 61 62 2d  (j=0, pIdx=pTab-
286ba 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70  >pIndex; pIdx; p
286bb 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c  Idx=pIdx->pNext,
286bc 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   j++){.         
286bd 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64   static const Vd
286be 62 65 4f 70 4c 69 73 74 20 63 6e 74 49 64 78 5b  beOpList cntIdx[
286bf 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 20 20  ] = {.          
286c0 20 20 20 7b 20 4f 50 5f 49 6e 74 65 67 65 72 2c     { OP_Integer,
286c1 20 20 20 20 20 20 30 2c 20 20 33 2c 20 20 30 7d        0,  3,  0}
286c2 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 7b  ,.             {
286c3 20 4f 50 5f 52 65 77 69 6e 64 2c 20 20 20 20 20   OP_Rewind,     
286c4 20 20 30 2c 20 20 30 2c 20 20 30 7d 2c 20 20 2f    0,  0,  0},  /
286c5 2a 20 31 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  * 1 */.         
286c6 20 20 20 20 7b 20 4f 50 5f 41 64 64 49 6d 6d 2c      { OP_AddImm,
286c7 20 20 20 20 20 20 20 33 2c 20 20 31 2c 20 20 30         3,  1,  0
286c8 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  },.             
286c9 7b 20 4f 50 5f 4e 65 78 74 2c 20 20 20 20 20 20  { OP_Next,      
286ca 20 20 20 30 2c 20 20 30 2c 20 20 30 7d 2c 20 20     0,  0,  0},  
286cb 2f 2a 20 33 20 2a 2f 0a 20 20 20 20 20 20 20 20  /* 3 */.        
286cc 20 20 20 20 20 7b 20 4f 50 5f 45 71 2c 20 20 20       { OP_Eq,   
286cd 20 20 20 20 20 20 20 20 32 2c 20 20 30 2c 20 20          2,  0,  
286ce 33 7d 2c 20 20 2f 2a 20 34 20 2a 2f 0a 20 20 20  3},  /* 4 */.   
286cf 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 41            { OP_A
286d0 64 64 49 6d 6d 2c 20 20 20 20 20 20 20 31 2c 20  ddImm,       1, 
286d1 2d 31 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20 20  -1,  0},.       
286d2 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e        { OP_Strin
286d3 67 38 2c 20 20 20 20 20 20 30 2c 20 20 32 2c 20  g8,      0,  2, 
286d4 20 30 7d 2c 20 20 2f 2a 20 36 20 2a 2f 0a 20 20   0},  /* 6 */.  
286d5 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
286d6 53 74 72 69 6e 67 38 2c 20 20 20 20 20 20 30 2c  String8,      0,
286d7 20 20 33 2c 20 20 30 7d 2c 20 20 2f 2a 20 37 20    3,  0},  /* 7 
286d8 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  */.             
286d9 7b 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 20 20 20  { OP_Concat,    
286da 20 20 20 33 2c 20 20 32 2c 20 20 32 7d 2c 0a 20     3,  2,  2},. 
286db 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
286dc 5f 52 65 73 75 6c 74 52 6f 77 2c 20 20 20 20 32  _ResultRow,    2
286dd 2c 20 20 31 2c 20 20 30 7d 2c 0a 20 20 20 20 20  ,  1,  0},.     
286de 20 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 20 20       };.        
286df 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33    addr = sqlite3
286e0 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
286e1 5f 49 66 50 6f 73 2c 20 31 29 3b 0a 20 20 20 20  _IfPos, 1);.    
286e2 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
286e3 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 48 61  eAddOp2(v, OP_Ha
286e4 6c 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20  lt, 0, 0);.     
286e5 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
286e6 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
286e7 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 64 64  );.          add
286e8 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
286e9 64 64 4f 70 4c 69 73 74 28 76 2c 20 41 72 72 61  ddOpList(v, Arra
286ea 79 53 69 7a 65 28 63 6e 74 49 64 78 29 2c 20 63  ySize(cntIdx), c
286eb 6e 74 49 64 78 29 3b 0a 20 20 20 20 20 20 20 20  ntIdx);.        
286ec 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
286ed 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2b 31 2c  ngeP1(v, addr+1,
286ee 20 6a 2b 32 29 3b 0a 20 20 20 20 20 20 20 20 20   j+2);.         
286ef 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
286f0 67 65 50 32 28 76 2c 20 61 64 64 72 2b 31 2c 20  geP2(v, addr+1, 
286f1 61 64 64 72 2b 34 29 3b 0a 20 20 20 20 20 20 20  addr+4);.       
286f2 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
286f3 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2b 33  angeP1(v, addr+3
286f4 2c 20 6a 2b 32 29 3b 0a 20 20 20 20 20 20 20 20  , j+2);.        
286f5 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
286f6 6e 67 65 50 32 28 76 2c 20 61 64 64 72 2b 33 2c  ngeP2(v, addr+3,
286f7 20 61 64 64 72 2b 32 29 3b 0a 20 20 20 20 20 20   addr+2);.      
286f8 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
286f9 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 2b  umpHere(v, addr+
286fa 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  4);.          sq
286fb 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
286fc 34 28 76 2c 20 61 64 64 72 2b 36 2c 20 0a 20 20  4(v, addr+6, .  
286fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
286fe 20 20 20 22 77 72 6f 6e 67 20 23 20 6f 66 20 65     "wrong # of e
286ff 6e 74 72 69 65 73 20 69 6e 20 69 6e 64 65 78 20  ntries in index 
28700 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20  ", P4_STATIC);. 
28701 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28702 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
28703 61 64 64 72 2b 37 2c 20 70 49 64 78 2d 3e 7a 4e  addr+7, pIdx->zN
28704 61 6d 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b  ame, P4_STATIC);
28705 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
28706 20 7d 20 0a 20 20 20 20 7d 0a 20 20 20 20 61 64   } .    }.    ad
28707 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
28708 41 64 64 4f 70 4c 69 73 74 28 76 2c 20 41 72 72  AddOpList(v, Arr
28709 61 79 53 69 7a 65 28 65 6e 64 43 6f 64 65 29 2c  aySize(endCode),
2870a 20 65 6e 64 43 6f 64 65 29 3b 0a 20 20 20 20 73   endCode);.    s
2870b 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
2870c 50 32 28 76 2c 20 61 64 64 72 2c 20 2d 6d 78 45  P2(v, addr, -mxE
2870d 72 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  rr);.    sqlite3
2870e 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
2870f 61 64 64 72 2b 31 29 3b 0a 20 20 20 20 73 71 6c  addr+1);.    sql
28710 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34  ite3VdbeChangeP4
28711 28 76 2c 20 61 64 64 72 2b 32 2c 20 22 6f 6b 22  (v, addr+2, "ok"
28712 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20  , P4_STATIC);.  
28713 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20  }else.#endif /* 
28714 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45  SQLITE_OMIT_INTE
28715 47 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a  GRITY_CHECK */..
28716 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
28717 4d 49 54 5f 55 54 46 31 36 0a 20 20 2f 2a 0a 20  MIT_UTF16.  /*. 
28718 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 65 6e 63   **   PRAGMA enc
28719 6f 64 69 6e 67 0a 20 20 2a 2a 20 20 20 50 52 41  oding.  **   PRA
2871a 47 4d 41 20 65 6e 63 6f 64 69 6e 67 20 3d 20 22  GMA encoding = "
2871b 75 74 66 2d 38 22 7c 22 75 74 66 2d 31 36 22 7c  utf-8"|"utf-16"|
2871c 22 75 74 66 2d 31 36 6c 65 22 7c 22 75 74 66 2d  "utf-16le"|"utf-
2871d 31 36 62 65 22 0a 20 20 2a 2a 0a 20 20 2a 2a 20  16be".  **.  ** 
2871e 49 6e 20 69 74 73 20 66 69 72 73 74 20 66 6f 72  In its first for
2871f 6d 2c 20 74 68 69 73 20 70 72 61 67 6d 61 20 72  m, this pragma r
28720 65 74 75 72 6e 73 20 74 68 65 20 65 6e 63 6f 64  eturns the encod
28721 69 6e 67 20 6f 66 20 74 68 65 20 6d 61 69 6e 0a  ing of the main.
28722 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 49    ** database. I
28723 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  f the database i
28724 73 20 6e 6f 74 20 69 6e 69 74 69 61 6c 69 7a 65  s not initialize
28725 64 2c 20 69 74 20 69 73 20 69 6e 69 74 69 61 6c  d, it is initial
28726 69 7a 65 64 20 6e 6f 77 2e 0a 20 20 2a 2a 0a 20  ized now..  **. 
28727 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 66   ** The second f
28728 6f 72 6d 20 6f 66 20 74 68 69 73 20 70 72 61 67  orm of this prag
28729 6d 61 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66  ma is a no-op if
2872a 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
2872b 73 65 20 66 69 6c 65 0a 20 20 2a 2a 20 68 61 73  se file.  ** has
2872c 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 62 65 65   not already bee
2872d 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 49  n initialized. I
2872e 6e 20 74 68 69 73 20 63 61 73 65 20 69 74 20 73  n this case it s
2872f 65 74 73 20 74 68 65 20 64 65 66 61 75 6c 74 0a  ets the default.
28730 20 20 2a 2a 20 65 6e 63 6f 64 69 6e 67 20 74 68    ** encoding th
28731 61 74 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20  at will be used 
28732 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74  for the main dat
28733 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 61 20  abase file if a 
28734 6e 65 77 20 66 69 6c 65 0a 20 20 2a 2a 20 69 73  new file.  ** is
28735 20 63 72 65 61 74 65 64 2e 20 49 66 20 61 6e 20   created. If an 
28736 65 78 69 73 74 69 6e 67 20 6d 61 69 6e 20 64 61  existing main da
28737 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 6f  tabase file is o
28738 70 65 6e 65 64 2c 20 74 68 65 6e 20 74 68 65 0a  pened, then the.
28739 20 20 2a 2a 20 64 65 66 61 75 6c 74 20 74 65 78    ** default tex
2873a 74 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74  t encoding for t
2873b 68 65 20 65 78 69 73 74 69 6e 67 20 64 61 74 61  he existing data
2873c 62 61 73 65 20 69 73 20 75 73 65 64 2e 0a 20 20  base is used..  
2873d 2a 2a 20 0a 20 20 2a 2a 20 49 6e 20 61 6c 6c 20  ** .  ** In all 
2873e 63 61 73 65 73 20 6e 65 77 20 64 61 74 61 62 61  cases new databa
2873f 73 65 73 20 63 72 65 61 74 65 64 20 75 73 69 6e  ses created usin
28740 67 20 74 68 65 20 41 54 54 41 43 48 20 63 6f 6d  g the ATTACH com
28741 6d 61 6e 64 20 61 72 65 0a 20 20 2a 2a 20 63 72  mand are.  ** cr
28742 65 61 74 65 64 20 74 6f 20 75 73 65 20 74 68 65  eated to use the
28743 20 73 61 6d 65 20 64 65 66 61 75 6c 74 20 74 65   same default te
28744 78 74 20 65 6e 63 6f 64 69 6e 67 20 61 73 20 74  xt encoding as t
28745 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
28746 2e 20 49 66 0a 20 20 2a 2a 20 74 68 65 20 6d 61  . If.  ** the ma
28747 69 6e 20 64 61 74 61 62 61 73 65 20 68 61 73 20  in database has 
28748 6e 6f 74 20 62 65 65 6e 20 69 6e 69 74 69 61 6c  not been initial
28749 69 7a 65 64 20 61 6e 64 2f 6f 72 20 63 72 65 61  ized and/or crea
2874a 74 65 64 20 77 68 65 6e 20 41 54 54 41 43 48 0a  ted when ATTACH.
2874b 20 20 2a 2a 20 69 73 20 65 78 65 63 75 74 65 64    ** is executed
2874c 2c 20 74 68 69 73 20 69 73 20 64 6f 6e 65 20 62  , this is done b
2874d 65 66 6f 72 65 20 74 68 65 20 41 54 54 41 43 48  efore the ATTACH
2874e 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 20 20 2a 2a   operation..  **
2874f 0a 20 20 2a 2a 20 49 6e 20 74 68 65 20 73 65 63  .  ** In the sec
28750 6f 6e 64 20 66 6f 72 6d 20 74 68 69 73 20 70 72  ond form this pr
28751 61 67 6d 61 20 73 65 74 73 20 74 68 65 20 74 65  agma sets the te
28752 78 74 20 65 6e 63 6f 64 69 6e 67 20 74 6f 20 62  xt encoding to b
28753 65 20 75 73 65 64 20 69 6e 0a 20 20 2a 2a 20 6e  e used in.  ** n
28754 65 77 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ew database file
28755 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20  s created using 
28756 74 68 69 73 20 64 61 74 61 62 61 73 65 20 68 61  this database ha
28757 6e 64 6c 65 2e 20 49 74 20 69 73 20 6f 6e 6c 79  ndle. It is only
28758 0a 20 20 2a 2a 20 75 73 65 66 75 6c 20 69 66 20  .  ** useful if 
28759 69 6e 76 6f 6b 65 64 20 69 6d 6d 65 64 69 61 74  invoked immediat
2875a 65 6c 79 20 61 66 74 65 72 20 74 68 65 20 6d 61  ely after the ma
2875b 69 6e 20 64 61 74 61 62 61 73 65 20 69 0a 20 20  in database i.  
2875c 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
2875d 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22  StrICmp(zLeft, "
2875e 65 6e 63 6f 64 69 6e 67 22 29 3d 3d 30 20 29 7b  encoding")==0 ){
2875f 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73  .    static cons
28760 74 20 73 74 72 75 63 74 20 45 6e 63 4e 61 6d 65  t struct EncName
28761 20 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a   {.      char *z
28762 4e 61 6d 65 3b 0a 20 20 20 20 20 20 75 38 20 65  Name;.      u8 e
28763 6e 63 3b 0a 20 20 20 20 7d 20 65 6e 63 6e 61 6d  nc;.    } encnam
28764 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 7b  es[] = {.      {
28765 20 22 55 54 46 38 22 2c 20 20 20 20 20 53 51 4c   "UTF8",     SQL
28766 49 54 45 5f 55 54 46 38 20 20 20 20 20 20 20 20  ITE_UTF8        
28767 7d 2c 0a 20 20 20 20 20 20 7b 20 22 55 54 46 2d  },.      { "UTF-
28768 38 22 2c 20 20 20 20 53 51 4c 49 54 45 5f 55 54  8",    SQLITE_UT
28769 46 38 20 20 20 20 20 20 20 20 7d 2c 20 20 2f 2a  F8        },  /*
2876a 20 4d 75 73 74 20 62 65 20 65 6c 65 6d 65 6e 74   Must be element
2876b 20 5b 31 5d 20 2a 2f 0a 20 20 20 20 20 20 7b 20   [1] */.      { 
2876c 22 55 54 46 2d 31 36 6c 65 22 2c 20 53 51 4c 49  "UTF-16le", SQLI
2876d 54 45 5f 55 54 46 31 36 4c 45 20 20 20 20 20 7d  TE_UTF16LE     }
2876e 2c 20 20 2f 2a 20 4d 75 73 74 20 62 65 20 65 6c  ,  /* Must be el
2876f 65 6d 65 6e 74 20 5b 32 5d 20 2a 2f 0a 20 20 20  ement [2] */.   
28770 20 20 20 7b 20 22 55 54 46 2d 31 36 62 65 22 2c     { "UTF-16be",
28771 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20   SQLITE_UTF16BE 
28772 20 20 20 20 7d 2c 20 20 2f 2a 20 4d 75 73 74 20      },  /* Must 
28773 62 65 20 65 6c 65 6d 65 6e 74 20 5b 33 5d 20 2a  be element [3] *
28774 2f 0a 20 20 20 20 20 20 7b 20 22 55 54 46 31 36  /.      { "UTF16
28775 6c 65 22 2c 20 20 53 51 4c 49 54 45 5f 55 54 46  le",  SQLITE_UTF
28776 31 36 4c 45 20 20 20 20 20 7d 2c 0a 20 20 20 20  16LE     },.    
28777 20 20 7b 20 22 55 54 46 31 36 62 65 22 2c 20 20    { "UTF16be",  
28778 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 20  SQLITE_UTF16BE  
28779 20 20 20 7d 2c 0a 20 20 20 20 20 20 7b 20 22 55     },.      { "U
2877a 54 46 2d 31 36 22 2c 20 20 20 30 20 20 20 20 20  TF-16",   0     
2877b 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 2c 20               }, 
2877c 2f 2a 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e  /* SQLITE_UTF16N
2877d 41 54 49 56 45 20 2a 2f 0a 20 20 20 20 20 20 7b  ATIVE */.      {
2877e 20 22 55 54 46 31 36 22 2c 20 20 20 20 30 20 20   "UTF16",    0  
2877f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28780 7d 2c 20 2f 2a 20 53 51 4c 49 54 45 5f 55 54 46  }, /* SQLITE_UTF
28781 31 36 4e 41 54 49 56 45 20 2a 2f 0a 20 20 20 20  16NATIVE */.    
28782 20 20 7b 20 30 2c 20 30 20 7d 0a 20 20 20 20 7d    { 0, 0 }.    }
28783 3b 0a 20 20 20 20 63 6f 6e 73 74 20 73 74 72 75  ;.    const stru
28784 63 74 20 45 6e 63 4e 61 6d 65 20 2a 70 45 6e 63  ct EncName *pEnc
28785 3b 0a 20 20 20 20 69 66 28 20 21 7a 52 69 67 68  ;.    if( !zRigh
28786 74 20 29 7b 20 20 20 20 2f 2a 20 22 50 52 41 47  t ){    /* "PRAG
28787 4d 41 20 65 6e 63 6f 64 69 6e 67 22 20 2a 2f 0a  MA encoding" */.
28788 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
28789 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72  3ReadSchema(pPar
2878a 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d  se) ) goto pragm
2878b 61 5f 6f 75 74 3b 0a 20 20 20 20 20 20 73 71 6c  a_out;.      sql
2878c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f  ite3VdbeSetNumCo
2878d 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 20 20  ls(v, 1);.      
2878e 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
2878f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
28790 41 4d 45 5f 4e 41 4d 45 2c 20 22 65 6e 63 6f 64  AME_NAME, "encod
28791 69 6e 67 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  ing", SQLITE_STA
28792 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  TIC);.      sqli
28793 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
28794 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20   OP_String8, 0, 
28795 31 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  1);.      assert
28796 28 20 65 6e 63 6e 61 6d 65 73 5b 53 51 4c 49 54  ( encnames[SQLIT
28797 45 5f 55 54 46 38 5d 2e 65 6e 63 3d 3d 53 51 4c  E_UTF8].enc==SQL
28798 49 54 45 5f 55 54 46 38 20 29 3b 0a 20 20 20 20  ITE_UTF8 );.    
28799 20 20 61 73 73 65 72 74 28 20 65 6e 63 6e 61 6d    assert( encnam
2879a 65 73 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  es[SQLITE_UTF16L
2879b 45 5d 2e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  E].enc==SQLITE_U
2879c 54 46 31 36 4c 45 20 29 3b 0a 20 20 20 20 20 20  TF16LE );.      
2879d 61 73 73 65 72 74 28 20 65 6e 63 6e 61 6d 65 73  assert( encnames
2879e 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 5d  [SQLITE_UTF16BE]
2879f 2e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46  .enc==SQLITE_UTF
287a0 31 36 42 45 20 29 3b 0a 20 20 20 20 20 20 73 71  16BE );.      sq
287a1 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
287a2 34 28 76 2c 20 2d 31 2c 20 65 6e 63 6e 61 6d 65  4(v, -1, encname
287a3 73 5b 45 4e 43 28 70 50 61 72 73 65 2d 3e 64 62  s[ENC(pParse->db
287a4 29 5d 2e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41  )].zName, P4_STA
287a5 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  TIC);.      sqli
287a6 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
287a7 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31   OP_ResultRow, 1
287a8 2c 20 31 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  , 1);.    }else{
287a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
287aa 20 20 20 20 20 20 20 20 2f 2a 20 22 50 52 41 47          /* "PRAG
287ab 4d 41 20 65 6e 63 6f 64 69 6e 67 20 3d 20 58 58  MA encoding = XX
287ac 58 22 20 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 4f  X" */.      /* O
287ad 6e 6c 79 20 63 68 61 6e 67 65 20 74 68 65 20 76  nly change the v
287ae 61 6c 75 65 20 6f 66 20 73 71 6c 69 74 65 2e 65  alue of sqlite.e
287af 6e 63 20 69 66 20 74 68 65 20 64 61 74 61 62 61  nc if the databa
287b0 73 65 20 68 61 6e 64 6c 65 20 69 73 20 6e 6f 74  se handle is not
287b1 0a 20 20 20 20 20 20 2a 2a 20 69 6e 69 74 69 61  .      ** initia
287b2 6c 69 7a 65 64 2e 20 49 66 20 74 68 65 20 6d 61  lized. If the ma
287b3 69 6e 20 64 61 74 61 62 61 73 65 20 65 78 69 73  in database exis
287b4 74 73 2c 20 74 68 65 20 6e 65 77 20 73 71 6c 69  ts, the new sqli
287b5 74 65 2e 65 6e 63 20 76 61 6c 75 65 0a 20 20 20  te.enc value.   
287b6 20 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 6f 76     ** will be ov
287b7 65 72 77 72 69 74 74 65 6e 20 77 68 65 6e 20 74  erwritten when t
287b8 68 65 20 73 63 68 65 6d 61 20 69 73 20 6e 65 78  he schema is nex
287b9 74 20 6c 6f 61 64 65 64 2e 20 49 66 20 69 74 20  t loaded. If it 
287ba 64 6f 65 73 20 6e 6f 74 0a 20 20 20 20 20 20 2a  does not.      *
287bb 2a 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73  * already exists
287bc 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 63 72 65  , it will be cre
287bd 61 74 65 64 20 74 6f 20 75 73 65 20 74 68 65 20  ated to use the 
287be 6e 65 77 20 65 6e 63 6f 64 69 6e 67 20 76 61 6c  new encoding val
287bf 75 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ue..      */.   
287c0 20 20 20 69 66 28 20 0a 20 20 20 20 20 20 20 20     if( .        
287c1 21 28 44 62 48 61 73 50 72 6f 70 65 72 74 79 28  !(DbHasProperty(
287c2 64 62 2c 20 30 2c 20 44 42 5f 53 63 68 65 6d 61  db, 0, DB_Schema
287c3 4c 6f 61 64 65 64 29 29 20 7c 7c 20 0a 20 20 20  Loaded)) || .   
287c4 20 20 20 20 20 44 62 48 61 73 50 72 6f 70 65 72       DbHasProper
287c5 74 79 28 64 62 2c 20 30 2c 20 44 42 5f 45 6d 70  ty(db, 0, DB_Emp
287c6 74 79 29 20 0a 20 20 20 20 20 20 29 7b 0a 20 20  ty) .      ){.  
287c7 20 20 20 20 20 20 66 6f 72 28 70 45 6e 63 3d 26        for(pEnc=&
287c8 65 6e 63 6e 61 6d 65 73 5b 30 5d 3b 20 70 45 6e  encnames[0]; pEn
287c9 63 2d 3e 7a 4e 61 6d 65 3b 20 70 45 6e 63 2b 2b  c->zName; pEnc++
287ca 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
287cb 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43   0==sqlite3StrIC
287cc 6d 70 28 7a 52 69 67 68 74 2c 20 70 45 6e 63 2d  mp(zRight, pEnc-
287cd 3e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20  >zName) ){.     
287ce 20 20 20 20 20 20 20 45 4e 43 28 70 50 61 72 73         ENC(pPars
287cf 65 2d 3e 64 62 29 20 3d 20 70 45 6e 63 2d 3e 65  e->db) = pEnc->e
287d0 6e 63 20 3f 20 70 45 6e 63 2d 3e 65 6e 63 20 3a  nc ? pEnc->enc :
287d1 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54   SQLITE_UTF16NAT
287d2 49 56 45 3b 0a 20 20 20 20 20 20 20 20 20 20 20  IVE;.           
287d3 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
287d4 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
287d5 20 20 20 20 20 20 69 66 28 20 21 70 45 6e 63 2d        if( !pEnc-
287d6 3e 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20  >zName ){.      
287d7 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
287d8 4d 73 67 28 70 50 61 72 73 65 2c 20 22 75 6e 73  Msg(pParse, "uns
287d9 75 70 70 6f 72 74 65 64 20 65 6e 63 6f 64 69 6e  upported encodin
287da 67 3a 20 25 73 22 2c 20 7a 52 69 67 68 74 29 3b  g: %s", zRight);
287db 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
287dc 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65   }.    }.  }else
287dd 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
287de 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a  E_OMIT_UTF16 */.
287df 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
287e0 4f 4d 49 54 5f 53 43 48 45 4d 41 5f 56 45 52 53  OMIT_SCHEMA_VERS
287e1 49 4f 4e 5f 50 52 41 47 4d 41 53 0a 20 20 2f 2a  ION_PRAGMAS.  /*
287e2 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b  .  **   PRAGMA [
287e3 64 61 74 61 62 61 73 65 2e 5d 73 63 68 65 6d 61  database.]schema
287e4 5f 76 65 72 73 69 6f 6e 0a 20 20 2a 2a 20 20 20  _version.  **   
287e5 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65  PRAGMA [database
287e6 2e 5d 73 63 68 65 6d 61 5f 76 65 72 73 69 6f 6e  .]schema_version
287e7 20 3d 20 3c 69 6e 74 65 67 65 72 3e 0a 20 20 2a   = <integer>.  *
287e8 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20  *.  **   PRAGMA 
287e9 5b 64 61 74 61 62 61 73 65 2e 5d 75 73 65 72 5f  [database.]user_
287ea 76 65 72 73 69 6f 6e 0a 20 20 2a 2a 20 20 20 50  version.  **   P
287eb 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
287ec 5d 75 73 65 72 5f 76 65 72 73 69 6f 6e 20 3d 20  ]user_version = 
287ed 3c 69 6e 74 65 67 65 72 3e 0a 20 20 2a 2a 0a 20  <integer>.  **. 
287ee 20 2a 2a 20 54 68 65 20 70 72 61 67 6d 61 27 73   ** The pragma's
287ef 20 73 63 68 65 6d 61 5f 76 65 72 73 69 6f 6e 20   schema_version 
287f0 61 6e 64 20 75 73 65 72 5f 76 65 72 73 69 6f 6e  and user_version
287f1 20 61 72 65 20 75 73 65 64 20 74 6f 20 73 65 74   are used to set
287f2 20 6f 72 20 67 65 74 0a 20 20 2a 2a 20 74 68 65   or get.  ** the
287f3 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 73 63   value of the sc
287f4 68 65 6d 61 2d 76 65 72 73 69 6f 6e 20 61 6e 64  hema-version and
287f5 20 75 73 65 72 2d 76 65 72 73 69 6f 6e 2c 20 72   user-version, r
287f6 65 73 70 65 63 74 69 76 65 6c 79 2e 20 42 6f 74  espectively. Bot
287f7 68 0a 20 20 2a 2a 20 74 68 65 20 73 63 68 65 6d  h.  ** the schem
287f8 61 2d 76 65 72 73 69 6f 6e 20 61 6e 64 20 74 68  a-version and th
287f9 65 20 75 73 65 72 2d 76 65 72 73 69 6f 6e 20 61  e user-version a
287fa 72 65 20 33 32 2d 62 69 74 20 73 69 67 6e 65 64  re 32-bit signed
287fb 20 69 6e 74 65 67 65 72 73 0a 20 20 2a 2a 20 73   integers.  ** s
287fc 74 6f 72 65 64 20 69 6e 20 74 68 65 20 64 61 74  tored in the dat
287fd 61 62 61 73 65 20 68 65 61 64 65 72 2e 0a 20 20  abase header..  
287fe 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 63 68 65  **.  ** The sche
287ff 6d 61 2d 63 6f 6f 6b 69 65 20 69 73 20 75 73 75  ma-cookie is usu
28800 61 6c 6c 79 20 6f 6e 6c 79 20 6d 61 6e 69 70 75  ally only manipu
28801 6c 61 74 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79  lated internally
28802 20 62 79 20 53 51 4c 69 74 65 2e 20 49 74 0a 20   by SQLite. It. 
28803 20 2a 2a 20 69 73 20 69 6e 63 72 65 6d 65 6e 74   ** is increment
28804 65 64 20 62 79 20 53 51 4c 69 74 65 20 77 68 65  ed by SQLite whe
28805 6e 65 76 65 72 20 74 68 65 20 64 61 74 61 62 61  never the databa
28806 73 65 20 73 63 68 65 6d 61 20 69 73 20 6d 6f 64  se schema is mod
28807 69 66 69 65 64 20 28 62 79 0a 20 20 2a 2a 20 63  ified (by.  ** c
28808 72 65 61 74 69 6e 67 20 6f 72 20 64 72 6f 70 70  reating or dropp
28809 69 6e 67 20 61 20 74 61 62 6c 65 20 6f 72 20 69  ing a table or i
2880a 6e 64 65 78 29 2e 20 54 68 65 20 73 63 68 65 6d  ndex). The schem
2880b 61 20 76 65 72 73 69 6f 6e 20 69 73 20 75 73 65  a version is use
2880c 64 20 62 79 0a 20 20 2a 2a 20 53 51 4c 69 74 65  d by.  ** SQLite
2880d 20 65 61 63 68 20 74 69 6d 65 20 61 20 71 75 65   each time a que
2880e 72 79 20 69 73 20 65 78 65 63 75 74 65 64 20 74  ry is executed t
2880f 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68  o ensure that th
28810 65 20 69 6e 74 65 72 6e 61 6c 20 63 61 63 68 65  e internal cache
28811 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 73 63 68  .  ** of the sch
28812 65 6d 61 20 75 73 65 64 20 77 68 65 6e 20 63 6f  ema used when co
28813 6d 70 69 6c 69 6e 67 20 74 68 65 20 53 51 4c 20  mpiling the SQL 
28814 71 75 65 72 79 20 6d 61 74 63 68 65 73 20 74 68  query matches th
28815 65 20 73 63 68 65 6d 61 20 6f 66 0a 20 20 2a 2a  e schema of.  **
28816 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 67   the database ag
28817 61 69 6e 73 74 20 77 68 69 63 68 20 74 68 65 20  ainst which the 
28818 63 6f 6d 70 69 6c 65 64 20 71 75 65 72 79 20 69  compiled query i
28819 73 20 61 63 74 75 61 6c 6c 79 20 65 78 65 63 75  s actually execu
2881a 74 65 64 2e 0a 20 20 2a 2a 20 53 75 62 76 65 72  ted..  ** Subver
2881b 74 69 6e 67 20 74 68 69 73 20 6d 65 63 68 61 6e  ting this mechan
2881c 69 73 6d 20 62 79 20 75 73 69 6e 67 20 22 50 52  ism by using "PR
2881d 41 47 4d 41 20 73 63 68 65 6d 61 5f 76 65 72 73  AGMA schema_vers
2881e 69 6f 6e 22 20 74 6f 20 6d 6f 64 69 66 79 0a 20  ion" to modify. 
2881f 20 2a 2a 20 74 68 65 20 73 63 68 65 6d 61 2d 76   ** the schema-v
28820 65 72 73 69 6f 6e 20 69 73 20 70 6f 74 65 6e 74  ersion is potent
28821 69 61 6c 6c 79 20 64 61 6e 67 65 72 6f 75 73 20  ially dangerous 
28822 61 6e 64 20 6d 61 79 20 6c 65 61 64 20 74 6f 20  and may lead to 
28823 70 72 6f 67 72 61 6d 0a 20 20 2a 2a 20 63 72 61  program.  ** cra
28824 73 68 65 73 20 6f 72 20 64 61 74 61 62 61 73 65  shes or database
28825 20 63 6f 72 72 75 70 74 69 6f 6e 2e 20 55 73 65   corruption. Use
28826 20 77 69 74 68 20 63 61 75 74 69 6f 6e 21 0a 20   with caution!. 
28827 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 75 73 65   **.  ** The use
28828 72 2d 76 65 72 73 69 6f 6e 20 69 73 20 6e 6f 74  r-version is not
28829 20 75 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79   used internally
2882a 20 62 79 20 53 51 4c 69 74 65 2e 20 49 74 20 6d   by SQLite. It m
2882b 61 79 20 62 65 20 75 73 65 64 20 62 79 0a 20 20  ay be used by.  
2882c 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20  ** applications 
2882d 66 6f 72 20 61 6e 79 20 70 75 72 70 6f 73 65 2e  for any purpose.
2882e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
2882f 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
28830 2c 20 22 73 63 68 65 6d 61 5f 76 65 72 73 69 6f  , "schema_versio
28831 6e 22 29 3d 3d 30 20 0a 20 20 20 7c 7c 20 73 71  n")==0 .   || sq
28832 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
28833 66 74 2c 20 22 75 73 65 72 5f 76 65 72 73 69 6f  ft, "user_versio
28834 6e 22 29 3d 3d 30 20 0a 20 20 20 7c 7c 20 73 71  n")==0 .   || sq
28835 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
28836 66 74 2c 20 22 66 72 65 65 6c 69 73 74 5f 63 6f  ft, "freelist_co
28837 75 6e 74 22 29 3d 3d 30 20 0a 20 20 29 7b 0a 20  unt")==0 .  ){. 
28838 20 20 20 69 6e 74 20 69 43 6f 6f 6b 69 65 3b 20     int iCookie; 
28839 20 20 2f 2a 20 43 6f 6f 6b 69 65 20 69 6e 64 65    /* Cookie inde
2883a 78 2e 20 31 20 66 6f 72 20 73 63 68 65 6d 61 2d  x. 1 for schema-
2883b 63 6f 6f 6b 69 65 2c 20 36 20 66 6f 72 20 75 73  cookie, 6 for us
2883c 65 72 2d 63 6f 6f 6b 69 65 2e 20 2a 2f 0a 20 20  er-cookie. */.  
2883d 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65    sqlite3VdbeUse
2883e 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0a  sBtree(v, iDb);.
2883f 20 20 20 20 73 77 69 74 63 68 28 20 7a 4c 65 66      switch( zLef
28840 74 5b 30 5d 20 29 7b 0a 20 20 20 20 20 20 63 61  t[0] ){.      ca
28841 73 65 20 27 66 27 3a 20 63 61 73 65 20 27 46 27  se 'f': case 'F'
28842 3a 0a 20 20 20 20 20 20 20 20 69 43 6f 6f 6b 69  :.        iCooki
28843 65 20 3d 20 42 54 52 45 45 5f 46 52 45 45 5f 50  e = BTREE_FREE_P
28844 41 47 45 5f 43 4f 55 4e 54 3b 0a 20 20 20 20 20  AGE_COUNT;.     
28845 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
28846 63 61 73 65 20 27 73 27 3a 20 63 61 73 65 20 27  case 's': case '
28847 53 27 3a 0a 20 20 20 20 20 20 20 20 69 43 6f 6f  S':.        iCoo
28848 6b 69 65 20 3d 20 42 54 52 45 45 5f 53 43 48 45  kie = BTREE_SCHE
28849 4d 41 5f 56 45 52 53 49 4f 4e 3b 0a 20 20 20 20  MA_VERSION;.    
2884a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2884b 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20   default:.      
2884c 20 20 69 43 6f 6f 6b 69 65 20 3d 20 42 54 52 45    iCookie = BTRE
2884d 45 5f 55 53 45 52 5f 56 45 52 53 49 4f 4e 3b 0a  E_USER_VERSION;.
2884e 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2884f 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 7a 52     }..    if( zR
28850 69 67 68 74 20 26 26 20 69 43 6f 6f 6b 69 65 21  ight && iCookie!
28851 3d 42 54 52 45 45 5f 46 52 45 45 5f 50 41 47 45  =BTREE_FREE_PAGE
28852 5f 43 4f 55 4e 54 20 29 7b 0a 20 20 20 20 20 20  _COUNT ){.      
28853 2f 2a 20 57 72 69 74 65 20 74 68 65 20 73 70 65  /* Write the spe
28854 63 69 66 69 65 64 20 63 6f 6f 6b 69 65 20 76 61  cified cookie va
28855 6c 75 65 20 2a 2f 0a 20 20 20 20 20 20 73 74 61  lue */.      sta
28856 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70  tic const VdbeOp
28857 4c 69 73 74 20 73 65 74 43 6f 6f 6b 69 65 5b 5d  List setCookie[]
28858 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 7b 20 4f   = {.        { O
28859 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c 20 20  P_Transaction,  
2885a 20 20 30 2c 20 20 31 2c 20 20 30 7d 2c 20 20 20    0,  1,  0},   
2885b 20 2f 2a 20 30 20 2a 2f 0a 20 20 20 20 20 20 20   /* 0 */.       
2885c 20 7b 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 20   { OP_Integer,  
2885d 20 20 20 20 20 20 30 2c 20 20 31 2c 20 20 30 7d        0,  1,  0}
2885e 2c 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20  ,    /* 1 */.   
2885f 20 20 20 20 20 7b 20 4f 50 5f 53 65 74 43 6f 6f       { OP_SetCoo
28860 6b 69 65 2c 20 20 20 20 20 20 30 2c 20 20 30 2c  kie,      0,  0,
28861 20 20 31 7d 2c 20 20 20 20 2f 2a 20 32 20 2a 2f    1},    /* 2 */
28862 0a 20 20 20 20 20 20 7d 3b 0a 20 20 20 20 20 20  .      };.      
28863 69 6e 74 20 61 64 64 72 20 3d 20 73 71 6c 69 74  int addr = sqlit
28864 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28  e3VdbeAddOpList(
28865 76 2c 20 41 72 72 61 79 53 69 7a 65 28 73 65 74  v, ArraySize(set
28866 43 6f 6f 6b 69 65 29 2c 20 73 65 74 43 6f 6f 6b  Cookie), setCook
28867 69 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ie);.      sqlit
28868 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76  e3VdbeChangeP1(v
28869 2c 20 61 64 64 72 2c 20 69 44 62 29 3b 0a 20 20  , addr, iDb);.  
2886a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
2886b 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2b  hangeP1(v, addr+
2886c 31 2c 20 61 74 6f 69 28 7a 52 69 67 68 74 29 29  1, atoi(zRight))
2886d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2886e 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61  dbeChangeP1(v, a
2886f 64 64 72 2b 32 2c 20 69 44 62 29 3b 0a 20 20 20  ddr+2, iDb);.   
28870 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
28871 61 6e 67 65 50 32 28 76 2c 20 61 64 64 72 2b 32  angeP2(v, addr+2
28872 2c 20 69 43 6f 6f 6b 69 65 29 3b 0a 20 20 20 20  , iCookie);.    
28873 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20  }else{.      /* 
28874 52 65 61 64 20 74 68 65 20 73 70 65 63 69 66 69  Read the specifi
28875 65 64 20 63 6f 6f 6b 69 65 20 76 61 6c 75 65 20  ed cookie value 
28876 2a 2f 0a 20 20 20 20 20 20 73 74 61 74 69 63 20  */.      static 
28877 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74  const VdbeOpList
28878 20 72 65 61 64 43 6f 6f 6b 69 65 5b 5d 20 3d 20   readCookie[] = 
28879 7b 0a 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 54  {.        { OP_T
2887a 72 61 6e 73 61 63 74 69 6f 6e 2c 20 20 20 20 20  ransaction,     
2887b 30 2c 20 20 30 2c 20 20 30 7d 2c 20 20 20 20 2f  0,  0,  0},    /
2887c 2a 20 30 20 2a 2f 0a 20 20 20 20 20 20 20 20 7b  * 0 */.        {
2887d 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20   OP_ReadCookie, 
2887e 20 20 20 20 20 30 2c 20 20 31 2c 20 20 30 7d 2c       0,  1,  0},
2887f 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20      /* 1 */.    
28880 20 20 20 20 7b 20 4f 50 5f 52 65 73 75 6c 74 52      { OP_ResultR
28881 6f 77 2c 20 20 20 20 20 20 20 31 2c 20 20 31 2c  ow,       1,  1,
28882 20 20 30 7d 0a 20 20 20 20 20 20 7d 3b 0a 20 20    0}.      };.  
28883 20 20 20 20 69 6e 74 20 61 64 64 72 20 3d 20 73      int addr = s
28884 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c  qlite3VdbeAddOpL
28885 69 73 74 28 76 2c 20 41 72 72 61 79 53 69 7a 65  ist(v, ArraySize
28886 28 72 65 61 64 43 6f 6f 6b 69 65 29 2c 20 72 65  (readCookie), re
28887 61 64 43 6f 6f 6b 69 65 29 3b 0a 20 20 20 20 20  adCookie);.     
28888 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
28889 67 65 50 31 28 76 2c 20 61 64 64 72 2c 20 69 44  geP1(v, addr, iD
2888a 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  b);.      sqlite
2888b 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c  3VdbeChangeP1(v,
2888c 20 61 64 64 72 2b 31 2c 20 69 44 62 29 3b 0a 20   addr+1, iDb);. 
2888d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2888e 43 68 61 6e 67 65 50 33 28 76 2c 20 61 64 64 72  ChangeP3(v, addr
2888f 2b 31 2c 20 69 43 6f 6f 6b 69 65 29 3b 0a 20 20  +1, iCookie);.  
28890 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
28891 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b  etNumCols(v, 1);
28892 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
28893 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
28894 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  0, COLNAME_NAME,
28895 20 7a 4c 65 66 74 2c 20 53 51 4c 49 54 45 5f 54   zLeft, SQLITE_T
28896 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 7d  RANSIENT);.    }
28897 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20  .  }else.#endif 
28898 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  /* SQLITE_OMIT_S
28899 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 5f 50 52  CHEMA_VERSION_PR
2889a 41 47 4d 41 53 20 2a 2f 0a 0a 23 69 66 20 64 65  AGMAS */..#if de
2889b 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42  fined(SQLITE_DEB
2889c 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  UG) || defined(S
2889d 51 4c 49 54 45 5f 54 45 53 54 29 0a 20 20 2f 2a  QLITE_TEST).  /*
2889e 0a 20 20 2a 2a 20 52 65 70 6f 72 74 20 74 68 65  .  ** Report the
2889f 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f   current state o
288a0 66 20 66 69 6c 65 20 6c 6f 67 73 20 66 6f 72 20  f file logs for 
288a1 61 6c 6c 20 64 61 74 61 62 61 73 65 73 0a 20 20  all databases.  
288a2 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
288a3 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22  StrICmp(zLeft, "
288a4 6c 6f 63 6b 5f 73 74 61 74 75 73 22 29 3d 3d 30  lock_status")==0
288a5 20 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63   ){.    static c
288a6 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74  onst char *const
288a7 20 61 7a 4c 6f 63 6b 4e 61 6d 65 5b 5d 20 3d 20   azLockName[] = 
288a8 7b 0a 20 20 20 20 20 20 22 75 6e 6c 6f 63 6b 65  {.      "unlocke
288a9 64 22 2c 20 22 73 68 61 72 65 64 22 2c 20 22 72  d", "shared", "r
288aa 65 73 65 72 76 65 64 22 2c 20 22 70 65 6e 64 69  eserved", "pendi
288ab 6e 67 22 2c 20 22 65 78 63 6c 75 73 69 76 65 22  ng", "exclusive"
288ac 0a 20 20 20 20 7d 3b 0a 20 20 20 20 69 6e 74 20  .    };.    int 
288ad 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  i;.    sqlite3Vd
288ae 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20  beSetNumCols(v, 
288af 32 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  2);.    pParse->
288b0 6e 4d 65 6d 20 3d 20 32 3b 0a 20 20 20 20 73 71  nMem = 2;.    sq
288b1 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
288b2 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d  ame(v, 0, COLNAM
288b3 45 5f 4e 41 4d 45 2c 20 22 64 61 74 61 62 61 73  E_NAME, "databas
288b4 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  e", SQLITE_STATI
288b5 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  C);.    sqlite3V
288b6 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
288b7 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   1, COLNAME_NAME
288b8 2c 20 22 73 74 61 74 75 73 22 2c 20 53 51 4c 49  , "status", SQLI
288b9 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
288ba 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e  for(i=0; i<db->n
288bb 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  Db; i++){.      
288bc 42 74 72 65 65 20 2a 70 42 74 3b 0a 20 20 20 20  Btree *pBt;.    
288bd 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b    Pager *pPager;
288be 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61  .      const cha
288bf 72 20 2a 7a 53 74 61 74 65 20 3d 20 22 75 6e 6b  r *zState = "unk
288c0 6e 6f 77 6e 22 3b 0a 20 20 20 20 20 20 69 6e 74  nown";.      int
288c1 20 6a 3b 0a 20 20 20 20 20 20 69 66 28 20 64 62   j;.      if( db
288c2 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 3d 3d  ->aDb[i].zName==
288c3 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  0 ) continue;.  
288c4 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
288c5 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69  ddOp4(v, OP_Stri
288c6 6e 67 38 2c 20 30 2c 20 31 2c 20 30 2c 20 64 62  ng8, 0, 1, 0, db
288c7 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 2c 20  ->aDb[i].zName, 
288c8 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  P4_STATIC);.    
288c9 20 20 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b    pBt = db->aDb[
288ca 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66  i].pBt;.      if
288cb 28 20 70 42 74 3d 3d 30 20 7c 7c 20 28 70 50 61  ( pBt==0 || (pPa
288cc 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72  ger = sqlite3Btr
288cd 65 65 50 61 67 65 72 28 70 42 74 29 29 3d 3d 30  eePager(pBt))==0
288ce 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 53 74 61   ){.        zSta
288cf 74 65 20 3d 20 22 63 6c 6f 73 65 64 22 3b 0a 20  te = "closed";. 
288d0 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73       }else if( s
288d1 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74  qlite3_file_cont
288d2 72 6f 6c 28 64 62 2c 20 69 20 3f 20 64 62 2d 3e  rol(db, i ? db->
288d3 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 20 3a 20 30  aDb[i].zName : 0
288d4 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
288d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
288d6 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 46          SQLITE_F
288d7 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 2c 20  CNTL_LOCKSTATE, 
288d8 26 6a 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  &j)==SQLITE_OK )
288d9 7b 0a 20 20 20 20 20 20 20 20 20 7a 53 74 61 74  {.         zStat
288da 65 20 3d 20 61 7a 4c 6f 63 6b 4e 61 6d 65 5b 6a  e = azLockName[j
288db 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  ];.      }.     
288dc 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
288dd 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
288de 2c 20 30 2c 20 32 2c 20 30 2c 20 7a 53 74 61 74  , 0, 2, 0, zStat
288df 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20  e, P4_STATIC);. 
288e0 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
288e1 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73  AddOp2(v, OP_Res
288e2 75 6c 74 52 6f 77 2c 20 31 2c 20 32 29 3b 0a 20  ultRow, 1, 2);. 
288e3 20 20 20 7d 0a 0a 20 20 7d 65 6c 73 65 0a 23 65     }..  }else.#e
288e4 6e 64 69 66 0a 0a 23 69 66 20 53 51 4c 49 54 45  ndif..#if SQLITE
288e5 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 69 66 28  _HAS_CODEC.  if(
288e6 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
288e7 7a 4c 65 66 74 2c 20 22 6b 65 79 22 29 3d 3d 30  zLeft, "key")==0
288e8 20 26 26 20 7a 52 69 67 68 74 20 29 7b 0a 20 20   && zRight ){.  
288e9 20 20 73 71 6c 69 74 65 33 5f 6b 65 79 28 64 62    sqlite3_key(db
288ea 2c 20 7a 52 69 67 68 74 2c 20 73 71 6c 69 74 65  , zRight, sqlite
288eb 33 53 74 72 6c 65 6e 33 30 28 7a 52 69 67 68 74  3Strlen30(zRight
288ec 29 29 3b 0a 20 20 7d 65 6c 73 65 0a 20 20 69 66  ));.  }else.  if
288ed 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
288ee 28 7a 4c 65 66 74 2c 20 22 72 65 6b 65 79 22 29  (zLeft, "rekey")
288ef 3d 3d 30 20 26 26 20 7a 52 69 67 68 74 20 29 7b  ==0 && zRight ){
288f0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 6b  .    sqlite3_rek
288f1 65 79 28 64 62 2c 20 7a 52 69 67 68 74 2c 20 73  ey(db, zRight, s
288f2 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
288f3 52 69 67 68 74 29 29 3b 0a 20 20 7d 65 6c 73 65  Right));.  }else
288f4 0a 20 20 69 66 28 20 7a 52 69 67 68 74 20 26 26  .  if( zRight &&
288f5 20 28 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70   (sqlite3StrICmp
288f6 28 7a 4c 65 66 74 2c 20 22 68 65 78 6b 65 79 22  (zLeft, "hexkey"
288f7 29 3d 3d 30 20 7c 7c 0a 20 20 20 20 20 20 20 20  )==0 ||.        
288f8 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
288f9 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22  StrICmp(zLeft, "
288fa 68 65 78 72 65 6b 65 79 22 29 3d 3d 30 29 20 29  hexrekey")==0) )
288fb 7b 0a 20 20 20 20 69 6e 74 20 69 2c 20 68 31 2c  {.    int i, h1,
288fc 20 68 32 3b 0a 20 20 20 20 63 68 61 72 20 7a 4b   h2;.    char zK
288fd 65 79 5b 34 30 5d 3b 0a 20 20 20 20 66 6f 72 28  ey[40];.    for(
288fe 69 3d 30 3b 20 28 68 31 20 3d 20 7a 52 69 67 68  i=0; (h1 = zRigh
288ff 74 5b 69 5d 29 21 3d 30 20 26 26 20 28 68 32 20  t[i])!=0 && (h2 
28900 3d 20 7a 52 69 67 68 74 5b 69 2b 31 5d 29 21 3d  = zRight[i+1])!=
28901 30 3b 20 69 2b 3d 32 29 7b 0a 20 20 20 20 20 20  0; i+=2){.      
28902 68 31 20 2b 3d 20 39 2a 28 31 26 28 68 31 3e 3e  h1 += 9*(1&(h1>>
28903 36 29 29 3b 0a 20 20 20 20 20 20 68 32 20 2b 3d  6));.      h2 +=
28904 20 39 2a 28 31 26 28 68 32 3e 3e 36 29 29 3b 0a   9*(1&(h2>>6));.
28905 20 20 20 20 20 20 7a 4b 65 79 5b 69 2f 32 5d 20        zKey[i/2] 
28906 3d 20 28 68 32 20 26 20 30 78 30 66 29 20 7c 20  = (h2 & 0x0f) | 
28907 28 28 68 31 20 26 20 30 78 66 29 3c 3c 34 29 3b  ((h1 & 0xf)<<4);
28908 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28  .    }.    if( (
28909 7a 4c 65 66 74 5b 33 5d 20 26 20 30 78 66 29 3d  zLeft[3] & 0xf)=
2890a 3d 30 78 62 20 29 7b 0a 20 20 20 20 20 20 73 71  =0xb ){.      sq
2890b 6c 69 74 65 33 5f 6b 65 79 28 64 62 2c 20 7a 4b  lite3_key(db, zK
2890c 65 79 2c 20 69 2f 32 29 3b 0a 20 20 20 20 7d 65  ey, i/2);.    }e
2890d 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
2890e 65 33 5f 72 65 6b 65 79 28 64 62 2c 20 7a 4b 65  e3_rekey(db, zKe
2890f 79 2c 20 69 2f 32 29 3b 0a 20 20 20 20 7d 0a 20  y, i/2);.    }. 
28910 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 23 69   }else.#endif.#i
28911 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44  f SQLITE_HAS_COD
28912 45 43 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  EC || defined(SQ
28913 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 45 52 4f  LITE_ENABLE_CERO
28914 44 29 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  D).  if( sqlite3
28915 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22  StrICmp(zLeft, "
28916 61 63 74 69 76 61 74 65 5f 65 78 74 65 6e 73 69  activate_extensi
28917 6f 6e 73 22 29 3d 3d 30 20 29 7b 0a 23 69 66 20  ons")==0 ){.#if 
28918 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43  SQLITE_HAS_CODEC
28919 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
2891a 53 74 72 4e 49 43 6d 70 28 7a 52 69 67 68 74 2c  StrNICmp(zRight,
2891b 20 22 73 65 65 2d 22 2c 20 34 29 3d 3d 30 20 29   "see-", 4)==0 )
2891c 7b 0a 20 20 20 20 20 20 65 78 74 65 72 6e 20 76  {.      extern v
2891d 6f 69 64 20 73 71 6c 69 74 65 33 5f 61 63 74 69  oid sqlite3_acti
2891e 76 61 74 65 5f 73 65 65 28 63 6f 6e 73 74 20 63  vate_see(const c
2891f 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 73 71 6c  har*);.      sql
28920 69 74 65 33 5f 61 63 74 69 76 61 74 65 5f 73 65  ite3_activate_se
28921 65 28 26 7a 52 69 67 68 74 5b 34 5d 29 3b 0a 20  e(&zRight[4]);. 
28922 20 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66 64     }.#endif.#ifd
28923 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
28924 5f 43 45 52 4f 44 0a 20 20 20 20 69 66 28 20 73  _CEROD.    if( s
28925 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a  qlite3StrNICmp(z
28926 52 69 67 68 74 2c 20 22 63 65 72 6f 64 2d 22 2c  Right, "cerod-",
28927 20 36 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20   6)==0 ){.      
28928 65 78 74 65 72 6e 20 76 6f 69 64 20 73 71 6c 69  extern void sqli
28929 74 65 33 5f 61 63 74 69 76 61 74 65 5f 63 65 72  te3_activate_cer
2892a 6f 64 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  od(const char*);
2892b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61  .      sqlite3_a
2892c 63 74 69 76 61 74 65 5f 63 65 72 6f 64 28 26 7a  ctivate_cerod(&z
2892d 52 69 67 68 74 5b 36 5d 29 3b 0a 20 20 20 20 7d  Right[6]);.    }
2892e 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 0a  .#endif.  }else.
2892f 23 65 6e 64 69 66 0a 0a 20 0a 20 20 7b 2f 2a 20  #endif.. .  {/* 
28930 45 6d 70 74 79 20 45 4c 53 45 20 63 6c 61 75 73  Empty ELSE claus
28931 65 20 2a 2f 7d 0a 0a 20 20 2f 2a 20 43 6f 64 65  e */}..  /* Code
28932 20 61 6e 20 4f 50 5f 45 78 70 69 72 65 20 61 74   an OP_Expire at
28933 20 74 68 65 20 65 6e 64 20 6f 66 20 65 61 63 68   the end of each
28934 20 50 52 41 47 4d 41 20 70 72 6f 67 72 61 6d 20   PRAGMA program 
28935 74 6f 20 63 61 75 73 65 0a 20 20 2a 2a 20 74 68  to cause.  ** th
28936 65 20 56 44 42 45 20 69 6d 70 6c 65 6d 65 6e 74  e VDBE implement
28937 69 6e 67 20 74 68 65 20 70 72 61 67 6d 61 20 74  ing the pragma t
28938 6f 20 65 78 70 69 72 65 2e 20 4d 6f 73 74 20 28  o expire. Most (
28939 61 6c 6c 3f 29 20 70 72 61 67 6d 61 73 0a 20 20  all?) pragmas.  
2893a 2a 2a 20 61 72 65 20 6f 6e 6c 79 20 76 61 6c 69  ** are only vali
2893b 64 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 65  d for a single e
2893c 78 65 63 75 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20  xecution..  */. 
2893d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2893e 70 32 28 76 2c 20 4f 50 5f 45 78 70 69 72 65 2c  p2(v, OP_Expire,
2893f 20 31 2c 20 30 29 3b 0a 0a 20 20 2f 2a 0a 20 20   1, 0);..  /*.  
28940 2a 2a 20 52 65 73 65 74 20 74 68 65 20 73 61 66  ** Reset the saf
28941 65 74 79 20 6c 65 76 65 6c 2c 20 69 6e 20 63 61  ety level, in ca
28942 73 65 20 74 68 65 20 66 75 6c 6c 66 73 79 6e 63  se the fullfsync
28943 20 66 6c 61 67 20 6f 72 20 73 79 6e 63 68 72 6f   flag or synchro
28944 6e 6f 75 73 0a 20 20 2a 2a 20 73 65 74 74 69 6e  nous.  ** settin
28945 67 20 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a  g changed..  */.
28946 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
28947 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41  MIT_PAGER_PRAGMA
28948 53 0a 20 20 69 66 28 20 64 62 2d 3e 61 75 74 6f  S.  if( db->auto
28949 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 73 71  Commit ){.    sq
2894a 6c 69 74 65 33 42 74 72 65 65 53 65 74 53 61 66  lite3BtreeSetSaf
2894b 65 74 79 4c 65 76 65 6c 28 70 44 62 2d 3e 70 42  etyLevel(pDb->pB
2894c 74 2c 20 70 44 62 2d 3e 73 61 66 65 74 79 5f 6c  t, pDb->safety_l
2894d 65 76 65 6c 2c 0a 20 20 20 20 20 20 20 20 20 20  evel,.          
2894e 20 20 20 20 20 28 64 62 2d 3e 66 6c 61 67 73 26       (db->flags&
2894f 53 51 4c 49 54 45 5f 46 75 6c 6c 46 53 79 6e 63  SQLITE_FullFSync
28950 29 21 3d 30 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  )!=0);.  }.#endi
28951 66 0a 70 72 61 67 6d 61 5f 6f 75 74 3a 0a 20 20  f.pragma_out:.  
28952 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
28953 2c 20 7a 4c 65 66 74 29 3b 0a 20 20 73 71 6c 69  , zLeft);.  sqli
28954 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 52  te3DbFree(db, zR
28955 69 67 68 74 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66  ight);.}..#endif
28956 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
28957 50 52 41 47 4d 41 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  PRAGMA */../****
28958 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
28959 66 20 70 72 61 67 6d 61 2e 63 20 2a 2a 2a 2a 2a  f pragma.c *****
2895a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2895b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2895c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
2895d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
2895e 20 66 69 6c 65 20 70 72 65 70 61 72 65 2e 63 20   file prepare.c 
2895f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28961 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
28962 20 32 30 30 35 20 4d 61 79 20 32 35 0a 2a 2a 0a   2005 May 25.**.
28963 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
28964 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
28965 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
28966 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
28967 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
28968 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
28969 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
2896a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
2896b 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
2896c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
2896d 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
2896e 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
2896f 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
28970 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
28971 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
28972 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
28973 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
28974 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
28975 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28976 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28977 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28978 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
28979 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
2897a 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  s the implementa
2897b 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69  tion of the sqli
2897c 74 65 33 5f 70 72 65 70 61 72 65 28 29 0a 2a 2a  te3_prepare().**
2897d 20 69 6e 74 65 72 66 61 63 65 2c 20 61 6e 64 20   interface, and 
2897e 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 63 6f  routines that co
2897f 6e 74 72 69 62 75 74 65 20 74 6f 20 6c 6f 61 64  ntribute to load
28980 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65  ing the database
28981 20 73 63 68 65 6d 61 0a 2a 2a 20 66 72 6f 6d 20   schema.** from 
28982 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  disk..**.** $Id:
28983 20 70 72 65 70 61 72 65 2e 63 2c 76 20 31 2e 31   prepare.c,v 1.1
28984 33 31 20 32 30 30 39 2f 30 38 2f 30 36 20 31 37  31 2009/08/06 17
28985 3a 34 33 3a 33 31 20 64 72 68 20 45 78 70 20 24  :43:31 drh Exp $
28986 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46 69 6c 6c 20  .*/../*.** Fill 
28987 74 68 65 20 49 6e 69 74 44 61 74 61 20 73 74 72  the InitData str
28988 75 63 74 75 72 65 20 77 69 74 68 20 61 6e 20 65  ucture with an e
28989 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 68 61  rror message tha
2898a 74 20 69 6e 64 69 63 61 74 65 73 0a 2a 2a 20 74  t indicates.** t
2898b 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65  hat the database
2898c 20 69 73 20 63 6f 72 72 75 70 74 2e 0a 2a 2f 0a   is corrupt..*/.
2898d 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 72 72  static void corr
2898e 75 70 74 53 63 68 65 6d 61 28 0a 20 20 49 6e 69  uptSchema(.  Ini
2898f 74 44 61 74 61 20 2a 70 44 61 74 61 2c 20 20 20  tData *pData,   
28990 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 61 74    /* Initializat
28991 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ion context */. 
28992 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 62   const char *zOb
28993 6a 2c 20 20 20 20 2f 2a 20 4f 62 6a 65 63 74 20  j,    /* Object 
28994 62 65 69 6e 67 20 70 61 72 73 65 64 20 61 74 20  being parsed at 
28995 74 68 65 20 70 6f 69 6e 74 20 6f 66 20 65 72 72  the point of err
28996 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  or */.  const ch
28997 61 72 20 2a 7a 45 78 74 72 61 20 20 20 2f 2a 20  ar *zExtra   /* 
28998 45 72 72 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f  Error informatio
28999 6e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65  n */.){.  sqlite
2899a 33 20 2a 64 62 20 3d 20 70 44 61 74 61 2d 3e 64  3 *db = pData->d
2899b 62 3b 0a 20 20 69 66 28 20 21 64 62 2d 3e 6d 61  b;.  if( !db->ma
2899c 6c 6c 6f 63 46 61 69 6c 65 64 20 26 26 20 28 64  llocFailed && (d
2899d 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54  b->flags & SQLIT
2899e 45 5f 52 65 63 6f 76 65 72 79 4d 6f 64 65 29 3d  E_RecoveryMode)=
2899f 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 7a 4f  =0 ){.    if( zO
289a0 62 6a 3d 3d 30 20 29 20 7a 4f 62 6a 20 3d 20 22  bj==0 ) zObj = "
289a1 3f 22 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53  ?";.    sqlite3S
289a2 65 74 53 74 72 69 6e 67 28 70 44 61 74 61 2d 3e  etString(pData->
289a3 70 7a 45 72 72 4d 73 67 2c 20 64 62 2c 0a 20 20  pzErrMsg, db,.  
289a4 20 20 20 20 22 6d 61 6c 66 6f 72 6d 65 64 20 64      "malformed d
289a5 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 28  atabase schema (
289a6 25 73 29 22 2c 20 7a 4f 62 6a 29 3b 0a 20 20 20  %s)", zObj);.   
289a7 20 69 66 28 20 7a 45 78 74 72 61 20 29 7b 0a 20   if( zExtra ){. 
289a8 20 20 20 20 20 2a 70 44 61 74 61 2d 3e 70 7a 45       *pData->pzE
289a9 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d  rrMsg = sqlite3M
289aa 41 70 70 65 6e 64 66 28 64 62 2c 20 2a 70 44 61  Appendf(db, *pDa
289ab 74 61 2d 3e 70 7a 45 72 72 4d 73 67 2c 20 0a 20  ta->pzErrMsg, . 
289ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
289ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
289ae 22 25 73 20 2d 20 25 73 22 2c 20 2a 70 44 61 74  "%s - %s", *pDat
289af 61 2d 3e 70 7a 45 72 72 4d 73 67 2c 20 7a 45 78  a->pzErrMsg, zEx
289b0 74 72 61 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  tra);.    }.  }.
289b1 20 20 70 44 61 74 61 2d 3e 72 63 20 3d 20 64 62    pData->rc = db
289b2 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3f  ->mallocFailed ?
289b3 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 3a 20   SQLITE_NOMEM : 
289b4 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 3b 0a  SQLITE_CORRUPT;.
289b5 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73  }../*.** This is
289b6 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 6f   the callback ro
289b7 75 74 69 6e 65 20 66 6f 72 20 74 68 65 20 63 6f  utine for the co
289b8 64 65 20 74 68 61 74 20 69 6e 69 74 69 61 6c 69  de that initiali
289b9 7a 65 73 20 74 68 65 0a 2a 2a 20 64 61 74 61 62  zes the.** datab
289ba 61 73 65 2e 20 20 53 65 65 20 73 71 6c 69 74 65  ase.  See sqlite
289bb 33 49 6e 69 74 28 29 20 62 65 6c 6f 77 20 66 6f  3Init() below fo
289bc 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66  r additional inf
289bd 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69  ormation..** Thi
289be 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 6c 73  s routine is als
289bf 6f 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68  o called from th
289c0 65 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61  e OP_ParseSchema
289c1 20 6f 70 63 6f 64 65 20 6f 66 20 74 68 65 20 56   opcode of the V
289c2 44 42 45 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20  DBE..**.** Each 
289c3 63 61 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e  callback contain
289c4 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  s the following 
289c5 69 6e 66 6f 72 6d 61 74 69 6f 6e 3a 0a 2a 2a 0a  information:.**.
289c6 2a 2a 20 20 20 20 20 61 72 67 76 5b 30 5d 20 3d  **     argv[0] =
289c7 20 6e 61 6d 65 20 6f 66 20 74 68 69 6e 67 20 62   name of thing b
289c8 65 69 6e 67 20 63 72 65 61 74 65 64 0a 2a 2a 20  eing created.** 
289c9 20 20 20 20 61 72 67 76 5b 31 5d 20 3d 20 72 6f      argv[1] = ro
289ca 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66  ot page number f
289cb 6f 72 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65  or table or inde
289cc 78 2e 20 30 20 66 6f 72 20 74 72 69 67 67 65 72  x. 0 for trigger
289cd 20 6f 72 20 76 69 65 77 2e 0a 2a 2a 20 20 20 20   or view..**    
289ce 20 61 72 67 76 5b 32 5d 20 3d 20 53 51 4c 20 74   argv[2] = SQL t
289cf 65 78 74 20 66 6f 72 20 74 68 65 20 43 52 45 41  ext for the CREA
289d0 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a  TE statement..**
289d1 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
289d2 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e  TE int sqlite3In
289d3 69 74 43 61 6c 6c 62 61 63 6b 28 76 6f 69 64 20  itCallback(void 
289d4 2a 70 49 6e 69 74 2c 20 69 6e 74 20 61 72 67 63  *pInit, int argc
289d5 2c 20 63 68 61 72 20 2a 2a 61 72 67 76 2c 20 63  , char **argv, c
289d6 68 61 72 20 2a 2a 4e 6f 74 55 73 65 64 29 7b 0a  har **NotUsed){.
289d7 20 20 49 6e 69 74 44 61 74 61 20 2a 70 44 61 74    InitData *pDat
289d8 61 20 3d 20 28 49 6e 69 74 44 61 74 61 2a 29 70  a = (InitData*)p
289d9 49 6e 69 74 3b 0a 20 20 73 71 6c 69 74 65 33 20  Init;.  sqlite3 
289da 2a 64 62 20 3d 20 70 44 61 74 61 2d 3e 64 62 3b  *db = pData->db;
289db 0a 20 20 69 6e 74 20 69 44 62 20 3d 20 70 44 61  .  int iDb = pDa
289dc 74 61 2d 3e 69 44 62 3b 0a 0a 20 20 61 73 73 65  ta->iDb;..  asse
289dd 72 74 28 20 61 72 67 63 3d 3d 33 20 29 3b 0a 20  rt( argc==3 );. 
289de 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
289df 52 32 28 4e 6f 74 55 73 65 64 2c 20 61 72 67 63  R2(NotUsed, argc
289e0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
289e1 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
289e2 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
289e3 44 62 43 6c 65 61 72 50 72 6f 70 65 72 74 79 28  DbClearProperty(
289e4 64 62 2c 20 69 44 62 2c 20 44 42 5f 45 6d 70 74  db, iDb, DB_Empt
289e5 79 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61  y);.  if( db->ma
289e6 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
289e7 20 20 63 6f 72 72 75 70 74 53 63 68 65 6d 61 28    corruptSchema(
289e8 70 44 61 74 61 2c 20 61 72 67 76 5b 30 5d 2c 20  pData, argv[0], 
289e9 30 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  0);.    return 1
289ea 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28  ;.  }..  assert(
289eb 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64   iDb>=0 && iDb<d
289ec 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 69 66 28 20  b->nDb );.  if( 
289ed 61 72 67 76 3d 3d 30 20 29 20 72 65 74 75 72 6e  argv==0 ) return
289ee 20 30 3b 20 20 20 2f 2a 20 4d 69 67 68 74 20 68   0;   /* Might h
289ef 61 70 70 65 6e 20 69 66 20 45 4d 50 54 59 5f 52  appen if EMPTY_R
289f0 45 53 55 4c 54 5f 43 41 4c 4c 42 41 43 4b 53 20  ESULT_CALLBACKS 
289f1 61 72 65 20 6f 6e 20 2a 2f 0a 20 20 69 66 28 20  are on */.  if( 
289f2 61 72 67 76 5b 31 5d 3d 3d 30 20 29 7b 0a 20 20  argv[1]==0 ){.  
289f3 20 20 63 6f 72 72 75 70 74 53 63 68 65 6d 61 28    corruptSchema(
289f4 70 44 61 74 61 2c 20 61 72 67 76 5b 30 5d 2c 20  pData, argv[0], 
289f5 30 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  0);.  }else if( 
289f6 61 72 67 76 5b 32 5d 20 26 26 20 61 72 67 76 5b  argv[2] && argv[
289f7 32 5d 5b 30 5d 20 29 7b 0a 20 20 20 20 2f 2a 20  2][0] ){.    /* 
289f8 43 61 6c 6c 20 74 68 65 20 70 61 72 73 65 72 20  Call the parser 
289f9 74 6f 20 70 72 6f 63 65 73 73 20 61 20 43 52 45  to process a CRE
289fa 41 54 45 20 54 41 42 4c 45 2c 20 49 4e 44 45 58  ATE TABLE, INDEX
289fb 20 6f 72 20 56 49 45 57 2e 0a 20 20 20 20 2a 2a   or VIEW..    **
289fc 20 42 75 74 20 62 65 63 61 75 73 65 20 64 62 2d   But because db-
289fd 3e 69 6e 69 74 2e 62 75 73 79 20 69 73 20 73 65  >init.busy is se
289fe 74 20 74 6f 20 31 2c 20 6e 6f 20 56 44 42 45 20  t to 1, no VDBE 
289ff 63 6f 64 65 20 69 73 20 67 65 6e 65 72 61 74 65  code is generate
28a00 64 0a 20 20 20 20 2a 2a 20 6f 72 20 65 78 65 63  d.    ** or exec
28a01 75 74 65 64 2e 20 20 41 6c 6c 20 74 68 65 20 70  uted.  All the p
28a02 61 72 73 65 72 20 64 6f 65 73 20 69 73 20 62 75  arser does is bu
28a03 69 6c 64 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  ild the internal
28a04 20 64 61 74 61 0a 20 20 20 20 2a 2a 20 73 74 72   data.    ** str
28a05 75 63 74 75 72 65 73 20 74 68 61 74 20 64 65 73  uctures that des
28a06 63 72 69 62 65 20 74 68 65 20 74 61 62 6c 65 2c  cribe the table,
28a07 20 69 6e 64 65 78 2c 20 6f 72 20 76 69 65 77 2e   index, or view.
28a08 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 68 61 72  .    */.    char
28a09 20 2a 7a 45 72 72 3b 0a 20 20 20 20 69 6e 74 20   *zErr;.    int 
28a0a 72 63 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  rc;.    assert( 
28a0b 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 3b  db->init.busy );
28a0c 0a 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e 69 44  .    db->init.iD
28a0d 62 20 3d 20 69 44 62 3b 0a 20 20 20 20 64 62 2d  b = iDb;.    db-
28a0e 3e 69 6e 69 74 2e 6e 65 77 54 6e 75 6d 20 3d 20  >init.newTnum = 
28a0f 61 74 6f 69 28 61 72 67 76 5b 31 5d 29 3b 0a 20  atoi(argv[1]);. 
28a10 20 20 20 64 62 2d 3e 69 6e 69 74 2e 6f 72 70 68     db->init.orph
28a11 61 6e 54 72 69 67 67 65 72 20 3d 20 30 3b 0a 20  anTrigger = 0;. 
28a12 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
28a13 65 78 65 63 28 64 62 2c 20 61 72 67 76 5b 32 5d  exec(db, argv[2]
28a14 2c 20 30 2c 20 30 2c 20 26 7a 45 72 72 29 3b 0a  , 0, 0, &zErr);.
28a15 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e 69 44 62      db->init.iDb
28a16 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74   = 0;.    assert
28a17 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
28a18 7c 7c 20 7a 45 72 72 3d 3d 30 20 29 3b 0a 20 20  || zErr==0 );.  
28a19 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21    if( SQLITE_OK!
28a1a 3d 72 63 20 29 7b 0a 20 20 20 20 20 20 69 66 28  =rc ){.      if(
28a1b 20 64 62 2d 3e 69 6e 69 74 2e 6f 72 70 68 61 6e   db->init.orphan
28a1c 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20 20  Trigger ){.     
28a1d 20 20 20 61 73 73 65 72 74 28 20 69 44 62 3d 3d     assert( iDb==
28a1e 31 20 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  1 );.      }else
28a1f 7b 0a 20 20 20 20 20 20 20 20 70 44 61 74 61 2d  {.        pData-
28a20 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 20 20  >rc = rc;.      
28a21 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
28a22 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20  _NOMEM ){.      
28a23 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
28a24 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  iled = 1;.      
28a25 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 21 3d    }else if( rc!=
28a26 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54  SQLITE_INTERRUPT
28a27 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 4c   && rc!=SQLITE_L
28a28 4f 43 4b 45 44 20 29 7b 0a 20 20 20 20 20 20 20  OCKED ){.       
28a29 20 20 20 63 6f 72 72 75 70 74 53 63 68 65 6d 61     corruptSchema
28a2a 28 70 44 61 74 61 2c 20 61 72 67 76 5b 30 5d 2c  (pData, argv[0],
28a2b 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 20 20   zErr);.        
28a2c 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
28a2d 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
28a2e 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 7d 0a 20  , zErr);.    }. 
28a2f 20 7d 65 6c 73 65 20 69 66 28 20 61 72 67 76 5b   }else if( argv[
28a30 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6f 72  0]==0 ){.    cor
28a31 72 75 70 74 53 63 68 65 6d 61 28 70 44 61 74 61  ruptSchema(pData
28a32 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65  , 0, 0);.  }else
28a33 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20  {.    /* If the 
28a34 53 51 4c 20 63 6f 6c 75 6d 6e 20 69 73 20 62 6c  SQL column is bl
28a35 61 6e 6b 20 69 74 20 6d 65 61 6e 73 20 74 68 69  ank it means thi
28a36 73 20 69 73 20 61 6e 20 69 6e 64 65 78 20 74 68  s is an index th
28a37 61 74 0a 20 20 20 20 2a 2a 20 77 61 73 20 63 72  at.    ** was cr
28a38 65 61 74 65 64 20 74 6f 20 62 65 20 74 68 65 20  eated to be the 
28a39 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 74  PRIMARY KEY or t
28a3a 6f 20 66 75 6c 66 69 6c 6c 20 61 20 55 4e 49 51  o fulfill a UNIQ
28a3b 55 45 0a 20 20 20 20 2a 2a 20 63 6f 6e 73 74 72  UE.    ** constr
28a3c 61 69 6e 74 20 66 6f 72 20 61 20 43 52 45 41 54  aint for a CREAT
28a3d 45 20 54 41 42 4c 45 2e 20 20 54 68 65 20 69 6e  E TABLE.  The in
28a3e 64 65 78 20 73 68 6f 75 6c 64 20 68 61 76 65 20  dex should have 
28a3f 61 6c 72 65 61 64 79 0a 20 20 20 20 2a 2a 20 62  already.    ** b
28a40 65 65 6e 20 63 72 65 61 74 65 64 20 77 68 65 6e  een created when
28a41 20 77 65 20 70 72 6f 63 65 73 73 65 64 20 74 68   we processed th
28a42 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 2e 20  e CREATE TABLE. 
28a43 20 41 6c 6c 20 77 65 20 68 61 76 65 0a 20 20 20   All we have.   
28a44 20 2a 2a 20 74 6f 20 64 6f 20 68 65 72 65 20 69   ** to do here i
28a45 73 20 72 65 63 6f 72 64 20 74 68 65 20 72 6f 6f  s record the roo
28a46 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66 6f  t page number fo
28a47 72 20 74 68 61 74 20 69 6e 64 65 78 2e 0a 20 20  r that index..  
28a48 20 20 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a    */.    Index *
28a49 70 49 6e 64 65 78 3b 0a 20 20 20 20 70 49 6e 64  pIndex;.    pInd
28a4a 65 78 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  ex = sqlite3Find
28a4b 49 6e 64 65 78 28 64 62 2c 20 61 72 67 76 5b 30  Index(db, argv[0
28a4c 5d 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  ], db->aDb[iDb].
28a4d 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20  zName);.    if( 
28a4e 70 49 6e 64 65 78 3d 3d 30 20 29 7b 0a 20 20 20  pIndex==0 ){.   
28a4f 20 20 20 2f 2a 20 54 68 69 73 20 63 61 6e 20 6f     /* This can o
28a50 63 63 75 72 20 69 66 20 74 68 65 72 65 20 65 78  ccur if there ex
28a51 69 73 74 73 20 61 6e 20 69 6e 64 65 78 20 6f 6e  ists an index on
28a52 20 61 20 54 45 4d 50 20 74 61 62 6c 65 20 77 68   a TEMP table wh
28a53 69 63 68 0a 20 20 20 20 20 20 2a 2a 20 68 61 73  ich.      ** has
28a54 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 20 61   the same name a
28a55 73 20 61 6e 6f 74 68 65 72 20 69 6e 64 65 78 20  s another index 
28a56 6f 6e 20 61 20 70 65 72 6d 61 6e 65 6e 74 20 69  on a permanent i
28a57 6e 64 65 78 2e 20 20 53 69 6e 63 65 0a 20 20 20  ndex.  Since.   
28a58 20 20 20 2a 2a 20 74 68 65 20 70 65 72 6d 61 6e     ** the perman
28a59 65 6e 74 20 74 61 62 6c 65 20 69 73 20 68 69 64  ent table is hid
28a5a 64 65 6e 20 62 79 20 74 68 65 20 54 45 4d 50 20  den by the TEMP 
28a5b 74 61 62 6c 65 2c 20 77 65 20 63 61 6e 20 61 6c  table, we can al
28a5c 73 6f 0a 20 20 20 20 20 20 2a 2a 20 73 61 66 65  so.      ** safe
28a5d 6c 79 20 69 67 6e 6f 72 65 20 74 68 65 20 69 6e  ly ignore the in
28a5e 64 65 78 20 6f 6e 20 74 68 65 20 70 65 72 6d 61  dex on the perma
28a5f 6e 65 6e 74 20 74 61 62 6c 65 2e 0a 20 20 20 20  nent table..    
28a60 20 20 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 44 6f    */.      /* Do
28a61 20 4e 6f 74 68 69 6e 67 20 2a 2f 3b 0a 20 20 20   Nothing */;.   
28a62 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74   }else if( sqlit
28a63 65 33 47 65 74 49 6e 74 33 32 28 61 72 67 76 5b  e3GetInt32(argv[
28a64 31 5d 2c 20 26 70 49 6e 64 65 78 2d 3e 74 6e 75  1], &pIndex->tnu
28a65 6d 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 63  m)==0 ){.      c
28a66 6f 72 72 75 70 74 53 63 68 65 6d 61 28 70 44 61  orruptSchema(pDa
28a67 74 61 2c 20 61 72 67 76 5b 30 5d 2c 20 22 69 6e  ta, argv[0], "in
28a68 76 61 6c 69 64 20 72 6f 6f 74 70 61 67 65 22 29  valid rootpage")
28a69 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
28a6a 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
28a6b 20 41 74 74 65 6d 70 74 20 74 6f 20 72 65 61 64   Attempt to read
28a6c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
28a6d 68 65 6d 61 20 61 6e 64 20 69 6e 69 74 69 61 6c  hema and initial
28a6e 69 7a 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20  ize internal.** 
28a6f 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20  data structures 
28a70 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 64 61 74  for a single dat
28a71 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65  abase file.  The
28a72 20 69 6e 64 65 78 20 6f 66 20 74 68 65 0a 2a 2a   index of the.**
28a73 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
28a74 73 20 67 69 76 65 6e 20 62 79 20 69 44 62 2e 20  s given by iDb. 
28a75 20 69 44 62 3d 3d 30 20 69 73 20 75 73 65 64 20   iDb==0 is used 
28a76 66 6f 72 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20  for the main.** 
28a77 64 61 74 61 62 61 73 65 2e 20 20 69 44 62 3d 3d  database.  iDb==
28a78 31 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62  1 should never b
28a79 65 20 75 73 65 64 2e 20 20 69 44 62 3e 3d 32 20  e used.  iDb>=2 
28a7a 69 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 61  is used for.** a
28a7b 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73  uxiliary databas
28a7c 65 73 2e 20 20 52 65 74 75 72 6e 20 6f 6e 65 20  es.  Return one 
28a7d 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f 20 65  of the SQLITE_ e
28a7e 72 72 6f 72 20 63 6f 64 65 73 20 74 6f 0a 2a 2a  rror codes to.**
28a7f 20 69 6e 64 69 63 61 74 65 20 73 75 63 63 65 73   indicate succes
28a80 73 20 6f 72 20 66 61 69 6c 75 72 65 2e 0a 2a 2f  s or failure..*/
28a81 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69  .static int sqli
28a82 74 65 33 49 6e 69 74 4f 6e 65 28 73 71 6c 69 74  te3InitOne(sqlit
28a83 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c  e3 *db, int iDb,
28a84 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67   char **pzErrMsg
28a85 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69  ){.  int rc;.  i
28a86 6e 74 20 69 3b 0a 20 20 69 6e 74 20 73 69 7a 65  nt i;.  int size
28a87 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b  ;.  Table *pTab;
28a88 0a 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 63 68  .  Db *pDb;.  ch
28a89 61 72 20 63 6f 6e 73 74 20 2a 61 7a 41 72 67 5b  ar const *azArg[
28a8a 34 5d 3b 0a 20 20 69 6e 74 20 6d 65 74 61 5b 35  4];.  int meta[5
28a8b 5d 3b 0a 20 20 49 6e 69 74 44 61 74 61 20 69 6e  ];.  InitData in
28a8c 69 74 44 61 74 61 3b 0a 20 20 63 68 61 72 20 63  itData;.  char c
28a8d 6f 6e 73 74 20 2a 7a 4d 61 73 74 65 72 53 63 68  onst *zMasterSch
28a8e 65 6d 61 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73  ema;.  char cons
28a8f 74 20 2a 7a 4d 61 73 74 65 72 4e 61 6d 65 20 3d  t *zMasterName =
28a90 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44   SCHEMA_TABLE(iD
28a91 62 29 3b 0a 20 20 69 6e 74 20 6f 70 65 6e 65 64  b);.  int opened
28a92 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 30 3b  Transaction = 0;
28a93 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20  ..  /*.  ** The 
28a94 6d 61 73 74 65 72 20 64 61 74 61 62 61 73 65 20  master database 
28a95 74 61 62 6c 65 20 68 61 73 20 61 20 73 74 72 75  table has a stru
28a96 63 74 75 72 65 20 6c 69 6b 65 20 74 68 69 73 0a  cture like this.
28a97 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f    */.  static co
28a98 6e 73 74 20 63 68 61 72 20 6d 61 73 74 65 72 5f  nst char master_
28a99 73 63 68 65 6d 61 5b 5d 20 3d 20 0a 20 20 20 20  schema[] = .    
28a9a 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 73   "CREATE TABLE s
28a9b 71 6c 69 74 65 5f 6d 61 73 74 65 72 28 5c 6e 22  qlite_master(\n"
28a9c 0a 20 20 20 20 20 22 20 20 74 79 70 65 20 74 65  .     "  type te
28a9d 78 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20 6e  xt,\n".     "  n
28a9e 61 6d 65 20 74 65 78 74 2c 5c 6e 22 0a 20 20 20  ame text,\n".   
28a9f 20 20 22 20 20 74 62 6c 5f 6e 61 6d 65 20 74 65    "  tbl_name te
28aa0 78 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20 72  xt,\n".     "  r
28aa1 6f 6f 74 70 61 67 65 20 69 6e 74 65 67 65 72 2c  ootpage integer,
28aa2 5c 6e 22 0a 20 20 20 20 20 22 20 20 73 71 6c 20  \n".     "  sql 
28aa3 74 65 78 74 5c 6e 22 0a 20 20 20 20 20 22 29 22  text\n".     ")"
28aa4 0a 20 20 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  .  ;.#ifndef SQL
28aa5 49 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 0a  ITE_OMIT_TEMPDB.
28aa6 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63    static const c
28aa7 68 61 72 20 74 65 6d 70 5f 6d 61 73 74 65 72 5f  har temp_master_
28aa8 73 63 68 65 6d 61 5b 5d 20 3d 20 0a 20 20 20 20  schema[] = .    
28aa9 20 22 43 52 45 41 54 45 20 54 45 4d 50 20 54 41   "CREATE TEMP TA
28aaa 42 4c 45 20 73 71 6c 69 74 65 5f 74 65 6d 70 5f  BLE sqlite_temp_
28aab 6d 61 73 74 65 72 28 5c 6e 22 0a 20 20 20 20 20  master(\n".     
28aac 22 20 20 74 79 70 65 20 74 65 78 74 2c 5c 6e 22  "  type text,\n"
28aad 0a 20 20 20 20 20 22 20 20 6e 61 6d 65 20 74 65  .     "  name te
28aae 78 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20 74  xt,\n".     "  t
28aaf 62 6c 5f 6e 61 6d 65 20 74 65 78 74 2c 5c 6e 22  bl_name text,\n"
28ab0 0a 20 20 20 20 20 22 20 20 72 6f 6f 74 70 61 67  .     "  rootpag
28ab1 65 20 69 6e 74 65 67 65 72 2c 5c 6e 22 0a 20 20  e integer,\n".  
28ab2 20 20 20 22 20 20 73 71 6c 20 74 65 78 74 5c 6e     "  sql text\n
28ab3 22 0a 20 20 20 20 20 22 29 22 0a 20 20 3b 0a 23  ".     ")".  ;.#
28ab4 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 74  else.  #define t
28ab5 65 6d 70 5f 6d 61 73 74 65 72 5f 73 63 68 65 6d  emp_master_schem
28ab6 61 20 30 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73  a 0.#endif..  as
28ab7 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20  sert( iDb>=0 && 
28ab8 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20  iDb<db->nDb );. 
28ab9 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62   assert( db->aDb
28aba 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 20 29 3b  [iDb].pSchema );
28abb 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
28abc 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62  e3_mutex_held(db
28abd 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
28abe 73 65 72 74 28 20 69 44 62 3d 3d 31 20 7c 7c 20  sert( iDb==1 || 
28abf 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
28ac0 73 4d 75 74 65 78 28 64 62 2d 3e 61 44 62 5b 69  sMutex(db->aDb[i
28ac1 44 62 5d 2e 70 42 74 29 20 29 3b 0a 0a 20 20 2f  Db].pBt) );..  /
28ac2 2a 20 7a 4d 61 73 74 65 72 53 63 68 65 6d 61 20  * zMasterSchema 
28ac3 61 6e 64 20 7a 49 6e 69 74 53 63 72 69 70 74 20  and zInitScript 
28ac4 61 72 65 20 73 65 74 20 74 6f 20 70 6f 69 6e 74  are set to point
28ac5 20 61 74 20 74 68 65 20 6d 61 73 74 65 72 20 73   at the master s
28ac6 63 68 65 6d 61 0a 20 20 2a 2a 20 61 6e 64 20 69  chema.  ** and i
28ac7 6e 69 74 69 61 6c 69 73 61 74 69 6f 6e 20 73 63  nitialisation sc
28ac8 72 69 70 74 20 61 70 70 72 6f 70 72 69 61 74 65  ript appropriate
28ac9 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73   for the databas
28aca 65 20 62 65 69 6e 67 0a 20 20 2a 2a 20 69 6e 69  e being.  ** ini
28acb 74 69 61 6c 69 73 65 64 2e 20 7a 4d 61 73 74 65  tialised. zMaste
28acc 72 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d  rName is the nam
28acd 65 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20  e of the master 
28ace 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  table..  */.  if
28acf 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26  ( !OMIT_TEMPDB &
28ad0 26 20 69 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20  & iDb==1 ){.    
28ad1 7a 4d 61 73 74 65 72 53 63 68 65 6d 61 20 3d 20  zMasterSchema = 
28ad2 74 65 6d 70 5f 6d 61 73 74 65 72 5f 73 63 68 65  temp_master_sche
28ad3 6d 61 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ma;.  }else{.   
28ad4 20 7a 4d 61 73 74 65 72 53 63 68 65 6d 61 20 3d   zMasterSchema =
28ad5 20 6d 61 73 74 65 72 5f 73 63 68 65 6d 61 3b 0a   master_schema;.
28ad6 20 20 7d 0a 20 20 7a 4d 61 73 74 65 72 4e 61 6d    }.  zMasterNam
28ad7 65 20 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c 45  e = SCHEMA_TABLE
28ad8 28 69 44 62 29 3b 0a 0a 20 20 2f 2a 20 43 6f 6e  (iDb);..  /* Con
28ad9 73 74 72 75 63 74 20 74 68 65 20 73 63 68 65 6d  struct the schem
28ada 61 20 74 61 62 6c 65 73 2e 20 20 2a 2f 0a 20 20  a tables.  */.  
28adb 61 7a 41 72 67 5b 30 5d 20 3d 20 7a 4d 61 73 74  azArg[0] = zMast
28adc 65 72 4e 61 6d 65 3b 0a 20 20 61 7a 41 72 67 5b  erName;.  azArg[
28add 31 5d 20 3d 20 22 31 22 3b 0a 20 20 61 7a 41 72  1] = "1";.  azAr
28ade 67 5b 32 5d 20 3d 20 7a 4d 61 73 74 65 72 53 63  g[2] = zMasterSc
28adf 68 65 6d 61 3b 0a 20 20 61 7a 41 72 67 5b 33 5d  hema;.  azArg[3]
28ae0 20 3d 20 30 3b 0a 20 20 69 6e 69 74 44 61 74 61   = 0;.  initData
28ae1 2e 64 62 20 3d 20 64 62 3b 0a 20 20 69 6e 69 74  .db = db;.  init
28ae2 44 61 74 61 2e 69 44 62 20 3d 20 69 44 62 3b 0a  Data.iDb = iDb;.
28ae3 20 20 69 6e 69 74 44 61 74 61 2e 72 63 20 3d 20    initData.rc = 
28ae4 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 69  SQLITE_OK;.  ini
28ae5 74 44 61 74 61 2e 70 7a 45 72 72 4d 73 67 20 3d  tData.pzErrMsg =
28ae6 20 70 7a 45 72 72 4d 73 67 3b 0a 20 20 28 76 6f   pzErrMsg;.  (vo
28ae7 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79  id)sqlite3Safety
28ae8 4f 66 66 28 64 62 29 3b 0a 20 20 73 71 6c 69 74  Off(db);.  sqlit
28ae9 65 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 28 26  e3InitCallback(&
28aea 69 6e 69 74 44 61 74 61 2c 20 33 2c 20 28 63 68  initData, 3, (ch
28aeb 61 72 20 2a 2a 29 61 7a 41 72 67 2c 20 30 29 3b  ar **)azArg, 0);
28aec 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33  .  (void)sqlite3
28aed 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20  SafetyOn(db);.  
28aee 69 66 28 20 69 6e 69 74 44 61 74 61 2e 72 63 20  if( initData.rc 
28aef 29 7b 0a 20 20 20 20 72 63 20 3d 20 69 6e 69 74  ){.    rc = init
28af0 44 61 74 61 2e 72 63 3b 0a 20 20 20 20 67 6f 74  Data.rc;.    got
28af1 6f 20 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d  o error_out;.  }
28af2 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65  .  pTab = sqlite
28af3 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a  3FindTable(db, z
28af4 4d 61 73 74 65 72 4e 61 6d 65 2c 20 64 62 2d 3e  MasterName, db->
28af5 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b  aDb[iDb].zName);
28af6 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 54  .  if( ALWAYS(pT
28af7 61 62 29 20 29 7b 0a 20 20 20 20 70 54 61 62 2d  ab) ){.    pTab-
28af8 3e 74 61 62 46 6c 61 67 73 20 7c 3d 20 54 46 5f  >tabFlags |= TF_
28af9 52 65 61 64 6f 6e 6c 79 3b 0a 20 20 7d 0a 0a 20  Readonly;.  }.. 
28afa 20 2f 2a 20 43 72 65 61 74 65 20 61 20 63 75 72   /* Create a cur
28afb 73 6f 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  sor to hold the 
28afc 64 61 74 61 62 61 73 65 20 6f 70 65 6e 0a 20 20  database open.  
28afd 2a 2f 0a 20 20 70 44 62 20 3d 20 26 64 62 2d 3e  */.  pDb = &db->
28afe 61 44 62 5b 69 44 62 5d 3b 0a 20 20 69 66 28 20  aDb[iDb];.  if( 
28aff 70 44 62 2d 3e 70 42 74 3d 3d 30 20 29 7b 0a 20  pDb->pBt==0 ){. 
28b00 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d     if( !OMIT_TEM
28b01 50 44 42 20 26 26 20 41 4c 57 41 59 53 28 69 44  PDB && ALWAYS(iD
28b02 62 3d 3d 31 29 20 29 7b 0a 20 20 20 20 20 20 44  b==1) ){.      D
28b03 62 53 65 74 50 72 6f 70 65 72 74 79 28 64 62 2c  bSetProperty(db,
28b04 20 31 2c 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61   1, DB_SchemaLoa
28b05 64 65 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ded);.    }.    
28b06 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
28b07 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  ;.  }..  /* If t
28b08 68 65 72 65 20 69 73 20 6e 6f 74 20 61 6c 72 65  here is not alre
28b09 61 64 79 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20  ady a read-only 
28b0a 28 6f 72 20 72 65 61 64 2d 77 72 69 74 65 29 20  (or read-write) 
28b0b 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e  transaction open
28b0c 65 64 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 62  ed.  ** on the b
28b0d 2d 74 72 65 65 20 64 61 74 61 62 61 73 65 2c 20  -tree database, 
28b0e 6f 70 65 6e 20 6f 6e 65 20 6e 6f 77 2e 20 49 66  open one now. If
28b0f 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69   a transaction i
28b10 73 20 6f 70 65 6e 65 64 2c 20 69 74 20 0a 20 20  s opened, it .  
28b11 2a 2a 20 77 69 6c 6c 20 62 65 20 63 6c 6f 73 65  ** will be close
28b12 64 20 62 65 66 6f 72 65 20 74 68 69 73 20 66 75  d before this fu
28b13 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e 20  nction returns. 
28b14 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 74 72   */.  sqlite3Btr
28b15 65 65 45 6e 74 65 72 28 70 44 62 2d 3e 70 42 74  eeEnter(pDb->pBt
28b16 29 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65  );.  if( !sqlite
28b17 33 42 74 72 65 65 49 73 49 6e 52 65 61 64 54 72  3BtreeIsInReadTr
28b18 61 6e 73 28 70 44 62 2d 3e 70 42 74 29 20 29 7b  ans(pDb->pBt) ){
28b19 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
28b1a 33 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73  3BtreeBeginTrans
28b1b 28 70 44 62 2d 3e 70 42 74 2c 20 30 29 3b 0a 20  (pDb->pBt, 0);. 
28b1c 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
28b1d 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71  E_OK ){.      sq
28b1e 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 70  lite3SetString(p
28b1f 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73  zErrMsg, db, "%s
28b20 22 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74 72  ", sqlite3ErrStr
28b21 28 72 63 29 29 3b 0a 20 20 20 20 20 20 67 6f 74  (rc));.      got
28b22 6f 20 69 6e 69 74 6f 6e 65 5f 65 72 72 6f 72 5f  o initone_error_
28b23 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6f  out;.    }.    o
28b24 70 65 6e 65 64 54 72 61 6e 73 61 63 74 69 6f 6e  penedTransaction
28b25 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20   = 1;.  }..  /* 
28b26 47 65 74 20 74 68 65 20 64 61 74 61 62 61 73 65  Get the database
28b27 20 6d 65 74 61 20 69 6e 66 6f 72 6d 61 74 69 6f   meta informatio
28b28 6e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4d 65 74  n..  **.  ** Met
28b29 61 20 76 61 6c 75 65 73 20 61 72 65 20 61 73 20  a values are as 
28b2a 66 6f 6c 6c 6f 77 73 3a 0a 20 20 2a 2a 20 20 20  follows:.  **   
28b2b 20 6d 65 74 61 5b 30 5d 20 20 20 53 63 68 65 6d   meta[0]   Schem
28b2c 61 20 63 6f 6f 6b 69 65 2e 20 20 43 68 61 6e 67  a cookie.  Chang
28b2d 65 73 20 77 69 74 68 20 65 61 63 68 20 73 63 68  es with each sch
28b2e 65 6d 61 20 63 68 61 6e 67 65 2e 0a 20 20 2a 2a  ema change..  **
28b2f 20 20 20 20 6d 65 74 61 5b 31 5d 20 20 20 46 69      meta[1]   Fi
28b30 6c 65 20 66 6f 72 6d 61 74 20 6f 66 20 73 63 68  le format of sch
28b31 65 6d 61 20 6c 61 79 65 72 2e 0a 20 20 2a 2a 20  ema layer..  ** 
28b32 20 20 20 6d 65 74 61 5b 32 5d 20 20 20 53 69 7a     meta[2]   Siz
28b33 65 20 6f 66 20 74 68 65 20 70 61 67 65 20 63 61  e of the page ca
28b34 63 68 65 2e 0a 20 20 2a 2a 20 20 20 20 6d 65 74  che..  **    met
28b35 61 5b 33 5d 20 20 20 4c 61 72 67 65 73 74 20 72  a[3]   Largest r
28b36 6f 6f 74 70 61 67 65 20 28 61 75 74 6f 2f 69 6e  ootpage (auto/in
28b37 63 72 5f 76 61 63 75 75 6d 20 6d 6f 64 65 29 0a  cr_vacuum mode).
28b38 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 34 5d 20    **    meta[4] 
28b39 20 20 44 62 20 74 65 78 74 20 65 6e 63 6f 64 69    Db text encodi
28b3a 6e 67 2e 20 31 3a 55 54 46 2d 38 20 32 3a 55 54  ng. 1:UTF-8 2:UT
28b3b 46 2d 31 36 4c 45 20 33 3a 55 54 46 2d 31 36 42  F-16LE 3:UTF-16B
28b3c 45 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 35  E.  **    meta[5
28b3d 5d 20 20 20 55 73 65 72 20 76 65 72 73 69 6f 6e  ]   User version
28b3e 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 36 5d  .  **    meta[6]
28b3f 20 20 20 49 6e 63 72 65 6d 65 6e 74 61 6c 20 76     Incremental v
28b40 61 63 75 75 6d 20 6d 6f 64 65 0a 20 20 2a 2a 20  acuum mode.  ** 
28b41 20 20 20 6d 65 74 61 5b 37 5d 20 20 20 75 6e 75     meta[7]   unu
28b42 73 65 64 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61  sed.  **    meta
28b43 5b 38 5d 20 20 20 75 6e 75 73 65 64 0a 20 20 2a  [8]   unused.  *
28b44 2a 20 20 20 20 6d 65 74 61 5b 39 5d 20 20 20 75  *    meta[9]   u
28b45 6e 75 73 65 64 0a 20 20 2a 2a 0a 20 20 2a 2a 20  nused.  **.  ** 
28b46 4e 6f 74 65 3a 20 54 68 65 20 23 64 65 66 69 6e  Note: The #defin
28b47 65 64 20 53 51 4c 49 54 45 5f 55 54 46 2a 20 73  ed SQLITE_UTF* s
28b48 79 6d 62 6f 6c 73 20 69 6e 20 73 71 6c 69 74 65  ymbols in sqlite
28b49 49 6e 74 2e 68 20 63 6f 72 72 65 73 70 6f 6e 64  Int.h correspond
28b4a 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20 70 6f 73   to.  ** the pos
28b4b 73 69 62 6c 65 20 76 61 6c 75 65 73 20 6f 66 20  sible values of 
28b4c 6d 65 74 61 5b 34 5d 2e 0a 20 20 2a 2f 0a 20 20  meta[4]..  */.  
28b4d 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 61 79  for(i=0; i<Array
28b4e 53 69 7a 65 28 6d 65 74 61 29 3b 20 69 2b 2b 29  Size(meta); i++)
28b4f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  {.    sqlite3Btr
28b50 65 65 47 65 74 4d 65 74 61 28 70 44 62 2d 3e 70  eeGetMeta(pDb->p
28b51 42 74 2c 20 69 2b 31 2c 20 28 75 33 32 20 2a 29  Bt, i+1, (u32 *)
28b52 26 6d 65 74 61 5b 69 5d 29 3b 0a 20 20 7d 0a 20  &meta[i]);.  }. 
28b53 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 73   pDb->pSchema->s
28b54 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 20 3d 20 6d  chema_cookie = m
28b55 65 74 61 5b 42 54 52 45 45 5f 53 43 48 45 4d 41  eta[BTREE_SCHEMA
28b56 5f 56 45 52 53 49 4f 4e 2d 31 5d 3b 0a 0a 20 20  _VERSION-1];..  
28b57 2f 2a 20 49 66 20 6f 70 65 6e 69 6e 67 20 61 20  /* If opening a 
28b58 6e 6f 6e 2d 65 6d 70 74 79 20 64 61 74 61 62 61  non-empty databa
28b59 73 65 2c 20 63 68 65 63 6b 20 74 68 65 20 74 65  se, check the te
28b5a 78 74 20 65 6e 63 6f 64 69 6e 67 2e 20 46 6f 72  xt encoding. For
28b5b 20 74 68 65 0a 20 20 2a 2a 20 6d 61 69 6e 20 64   the.  ** main d
28b5c 61 74 61 62 61 73 65 2c 20 73 65 74 20 73 71 6c  atabase, set sql
28b5d 69 74 65 33 2e 65 6e 63 20 74 6f 20 74 68 65 20  ite3.enc to the 
28b5e 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20  encoding of the 
28b5f 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2e 0a 20  main database.. 
28b60 20 2a 2a 20 46 6f 72 20 61 6e 20 61 74 74 61 63   ** For an attac
28b61 68 65 64 20 64 62 2c 20 69 74 20 69 73 20 61 6e  hed db, it is an
28b62 20 65 72 72 6f 72 20 69 66 20 74 68 65 20 65 6e   error if the en
28b63 63 6f 64 69 6e 67 20 69 73 20 6e 6f 74 20 74 68  coding is not th
28b64 65 20 73 61 6d 65 0a 20 20 2a 2a 20 61 73 20 73  e same.  ** as s
28b65 71 6c 69 74 65 33 2e 65 6e 63 2e 0a 20 20 2a 2f  qlite3.enc..  */
28b66 0a 20 20 69 66 28 20 6d 65 74 61 5b 42 54 52 45  .  if( meta[BTRE
28b67 45 5f 54 45 58 54 5f 45 4e 43 4f 44 49 4e 47 2d  E_TEXT_ENCODING-
28b68 31 5d 20 29 7b 20 20 2f 2a 20 74 65 78 74 20 65  1] ){  /* text e
28b69 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 20 20 69  ncoding */.    i
28b6a 66 28 20 69 44 62 3d 3d 30 20 29 7b 0a 20 20 20  f( iDb==0 ){.   
28b6b 20 20 20 75 38 20 65 6e 63 6f 64 69 6e 67 3b 0a     u8 encoding;.
28b6c 20 20 20 20 20 20 2f 2a 20 49 66 20 6f 70 65 6e        /* If open
28b6d 69 6e 67 20 74 68 65 20 6d 61 69 6e 20 64 61 74  ing the main dat
28b6e 61 62 61 73 65 2c 20 73 65 74 20 45 4e 43 28 64  abase, set ENC(d
28b6f 62 29 2e 20 2a 2f 0a 20 20 20 20 20 20 65 6e 63  b). */.      enc
28b70 6f 64 69 6e 67 20 3d 20 28 75 38 29 6d 65 74 61  oding = (u8)meta
28b71 5b 42 54 52 45 45 5f 54 45 58 54 5f 45 4e 43 4f  [BTREE_TEXT_ENCO
28b72 44 49 4e 47 2d 31 5d 20 26 20 33 3b 0a 20 20 20  DING-1] & 3;.   
28b73 20 20 20 69 66 28 20 65 6e 63 6f 64 69 6e 67 3d     if( encoding=
28b74 3d 30 20 29 20 65 6e 63 6f 64 69 6e 67 20 3d 20  =0 ) encoding = 
28b75 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20  SQLITE_UTF8;.   
28b76 20 20 20 45 4e 43 28 64 62 29 20 3d 20 65 6e 63     ENC(db) = enc
28b77 6f 64 69 6e 67 3b 0a 20 20 20 20 20 20 64 62 2d  oding;.      db-
28b78 3e 70 44 66 6c 74 43 6f 6c 6c 20 3d 20 73 71 6c  >pDfltColl = sql
28b79 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28  ite3FindCollSeq(
28b7a 64 62 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c  db, SQLITE_UTF8,
28b7b 20 22 42 49 4e 41 52 59 22 2c 20 30 29 3b 0a 20   "BINARY", 0);. 
28b7c 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
28b7d 2f 2a 20 49 66 20 6f 70 65 6e 69 6e 67 20 61 6e  /* If opening an
28b7e 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61   attached databa
28b7f 73 65 2c 20 74 68 65 20 65 6e 63 6f 64 69 6e 67  se, the encoding
28b80 20 6d 75 63 68 20 6d 61 74 63 68 20 45 4e 43 28   much match ENC(
28b81 64 62 29 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  db) */.      if(
28b82 20 6d 65 74 61 5b 42 54 52 45 45 5f 54 45 58 54   meta[BTREE_TEXT
28b83 5f 45 4e 43 4f 44 49 4e 47 2d 31 5d 21 3d 45 4e  _ENCODING-1]!=EN
28b84 43 28 64 62 29 20 29 7b 0a 20 20 20 20 20 20 20  C(db) ){.       
28b85 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e   sqlite3SetStrin
28b86 67 28 70 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20  g(pzErrMsg, db, 
28b87 22 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61  "attached databa
28b88 73 65 73 20 6d 75 73 74 20 75 73 65 20 74 68 65  ses must use the
28b89 20 73 61 6d 65 22 0a 20 20 20 20 20 20 20 20 20   same".         
28b8a 20 20 20 22 20 74 65 78 74 20 65 6e 63 6f 64 69     " text encodi
28b8b 6e 67 20 61 73 20 6d 61 69 6e 20 64 61 74 61 62  ng as main datab
28b8c 61 73 65 22 29 3b 0a 20 20 20 20 20 20 20 20 72  ase");.        r
28b8d 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
28b8e 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 69  ;.        goto i
28b8f 6e 69 74 6f 6e 65 5f 65 72 72 6f 72 5f 6f 75 74  nitone_error_out
28b90 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
28b91 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 44 62 53    }else{.    DbS
28b92 65 74 50 72 6f 70 65 72 74 79 28 64 62 2c 20 69  etProperty(db, i
28b93 44 62 2c 20 44 42 5f 45 6d 70 74 79 29 3b 0a 20  Db, DB_Empty);. 
28b94 20 7d 0a 20 20 70 44 62 2d 3e 70 53 63 68 65 6d   }.  pDb->pSchem
28b95 61 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 64 62 29  a->enc = ENC(db)
28b96 3b 0a 0a 20 20 69 66 28 20 70 44 62 2d 3e 70 53  ;..  if( pDb->pS
28b97 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a  chema->cache_siz
28b98 65 3d 3d 30 20 29 7b 0a 20 20 20 20 73 69 7a 65  e==0 ){.    size
28b99 20 3d 20 6d 65 74 61 5b 42 54 52 45 45 5f 44 45   = meta[BTREE_DE
28b9a 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45  FAULT_CACHE_SIZE
28b9b 2d 31 5d 3b 0a 20 20 20 20 69 66 28 20 73 69 7a  -1];.    if( siz
28b9c 65 3d 3d 30 20 29 7b 20 73 69 7a 65 20 3d 20 53  e==0 ){ size = S
28b9d 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41  QLITE_DEFAULT_CA
28b9e 43 48 45 5f 53 49 5a 45 3b 20 7d 0a 20 20 20 20  CHE_SIZE; }.    
28b9f 69 66 28 20 73 69 7a 65 3c 30 20 29 20 73 69 7a  if( size<0 ) siz
28ba0 65 20 3d 20 2d 73 69 7a 65 3b 0a 20 20 20 20 70  e = -size;.    p
28ba1 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63  Db->pSchema->cac
28ba2 68 65 5f 73 69 7a 65 20 3d 20 73 69 7a 65 3b 0a  he_size = size;.
28ba3 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
28ba4 53 65 74 43 61 63 68 65 53 69 7a 65 28 70 44 62  SetCacheSize(pDb
28ba5 2d 3e 70 42 74 2c 20 70 44 62 2d 3e 70 53 63 68  ->pBt, pDb->pSch
28ba6 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65 29  ema->cache_size)
28ba7 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  ;.  }..  /*.  **
28ba8 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3d 3d 31 20   file_format==1 
28ba9 20 20 20 56 65 72 73 69 6f 6e 20 33 2e 30 2e 30     Version 3.0.0
28baa 2e 0a 20 20 2a 2a 20 66 69 6c 65 5f 66 6f 72 6d  ..  ** file_form
28bab 61 74 3d 3d 32 20 20 20 20 56 65 72 73 69 6f 6e  at==2    Version
28bac 20 33 2e 31 2e 33 2e 20 20 2f 2f 20 41 4c 54 45   3.1.3.  // ALTE
28bad 52 20 54 41 42 4c 45 20 41 44 44 20 43 4f 4c 55  R TABLE ADD COLU
28bae 4d 4e 0a 20 20 2a 2a 20 66 69 6c 65 5f 66 6f 72  MN.  ** file_for
28baf 6d 61 74 3d 3d 33 20 20 20 20 56 65 72 73 69 6f  mat==3    Versio
28bb0 6e 20 33 2e 31 2e 34 2e 20 20 2f 2f 20 64 69 74  n 3.1.4.  // dit
28bb1 74 6f 20 62 75 74 20 77 69 74 68 20 6e 6f 6e 2d  to but with non-
28bb2 4e 55 4c 4c 20 64 65 66 61 75 6c 74 73 0a 20 20  NULL defaults.  
28bb3 2a 2a 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3d 3d  ** file_format==
28bb4 34 20 20 20 20 56 65 72 73 69 6f 6e 20 33 2e 33  4    Version 3.3
28bb5 2e 30 2e 20 20 2f 2f 20 44 45 53 43 20 69 6e 64  .0.  // DESC ind
28bb6 69 63 65 73 2e 20 20 42 6f 6f 6c 65 61 6e 20 63  ices.  Boolean c
28bb7 6f 6e 73 74 61 6e 74 73 0a 20 20 2a 2f 0a 20 20  onstants.  */.  
28bb8 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69  pDb->pSchema->fi
28bb9 6c 65 5f 66 6f 72 6d 61 74 20 3d 20 28 75 38 29  le_format = (u8)
28bba 6d 65 74 61 5b 42 54 52 45 45 5f 46 49 4c 45 5f  meta[BTREE_FILE_
28bbb 46 4f 52 4d 41 54 2d 31 5d 3b 0a 20 20 69 66 28  FORMAT-1];.  if(
28bbc 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66   pDb->pSchema->f
28bbd 69 6c 65 5f 66 6f 72 6d 61 74 3d 3d 30 20 29 7b  ile_format==0 ){
28bbe 0a 20 20 20 20 70 44 62 2d 3e 70 53 63 68 65 6d  .    pDb->pSchem
28bbf 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 20 3d  a->file_format =
28bc0 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 44   1;.  }.  if( pD
28bc1 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65  b->pSchema->file
28bc2 5f 66 6f 72 6d 61 74 3e 53 51 4c 49 54 45 5f 4d  _format>SQLITE_M
28bc3 41 58 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 20 29  AX_FILE_FORMAT )
28bc4 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74  {.    sqlite3Set
28bc5 53 74 72 69 6e 67 28 70 7a 45 72 72 4d 73 67 2c  String(pzErrMsg,
28bc6 20 64 62 2c 20 22 75 6e 73 75 70 70 6f 72 74 65   db, "unsupporte
28bc7 64 20 66 69 6c 65 20 66 6f 72 6d 61 74 22 29 3b  d file format");
28bc8 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
28bc9 5f 45 52 52 4f 52 3b 0a 20 20 20 20 67 6f 74 6f  _ERROR;.    goto
28bca 20 69 6e 69 74 6f 6e 65 5f 65 72 72 6f 72 5f 6f   initone_error_o
28bcb 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 69  ut;.  }..  /* Ti
28bcc 63 6b 65 74 20 23 32 38 30 34 3a 20 20 57 68 65  cket #2804:  Whe
28bcd 6e 20 77 65 20 6f 70 65 6e 20 61 20 64 61 74 61  n we open a data
28bce 62 61 73 65 20 69 6e 20 74 68 65 20 6e 65 77 65  base in the newe
28bcf 72 20 66 69 6c 65 20 66 6f 72 6d 61 74 2c 0a 20  r file format,. 
28bd0 20 2a 2a 20 63 6c 65 61 72 20 74 68 65 20 6c 65   ** clear the le
28bd1 67 61 63 79 5f 66 69 6c 65 5f 66 6f 72 6d 61 74  gacy_file_format
28bd2 20 70 72 61 67 6d 61 20 66 6c 61 67 20 73 6f 20   pragma flag so 
28bd3 74 68 61 74 20 61 20 56 41 43 55 55 4d 20 77 69  that a VACUUM wi
28bd4 6c 6c 0a 20 20 2a 2a 20 6e 6f 74 20 64 6f 77 6e  ll.  ** not down
28bd5 67 72 61 64 65 20 74 68 65 20 64 61 74 61 62 61  grade the databa
28bd6 73 65 20 61 6e 64 20 74 68 75 73 20 69 6e 76 61  se and thus inva
28bd7 6c 69 64 61 74 65 20 61 6e 79 20 64 65 73 63 65  lidate any desce
28bd8 6e 64 69 6e 67 0a 20 20 2a 2a 20 69 6e 64 69 63  nding.  ** indic
28bd9 65 73 20 74 68 61 74 20 74 68 65 20 75 73 65 72  es that the user
28bda 20 6d 69 67 68 74 20 68 61 76 65 20 63 72 65 61   might have crea
28bdb 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ted..  */.  if( 
28bdc 69 44 62 3d 3d 30 20 26 26 20 6d 65 74 61 5b 42  iDb==0 && meta[B
28bdd 54 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54  TREE_FILE_FORMAT
28bde 2d 31 5d 3e 3d 34 20 29 7b 0a 20 20 20 20 64 62  -1]>=4 ){.    db
28bdf 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49  ->flags &= ~SQLI
28be0 54 45 5f 4c 65 67 61 63 79 46 69 6c 65 46 6d 74  TE_LegacyFileFmt
28be1 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 64  ;.  }..  /* Read
28be2 20 74 68 65 20 73 63 68 65 6d 61 20 69 6e 66 6f   the schema info
28be3 72 6d 61 74 69 6f 6e 20 6f 75 74 20 6f 66 20 74  rmation out of t
28be4 68 65 20 73 63 68 65 6d 61 20 74 61 62 6c 65 73  he schema tables
28be5 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
28be6 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 3b  db->init.busy );
28be7 0a 20 20 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a  .  {.    char *z
28be8 53 71 6c 3b 0a 20 20 20 20 7a 53 71 6c 20 3d 20  Sql;.    zSql = 
28be9 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
28bea 62 2c 20 0a 20 20 20 20 20 20 20 20 22 53 45 4c  b, .        "SEL
28beb 45 43 54 20 6e 61 6d 65 2c 20 72 6f 6f 74 70 61  ECT name, rootpa
28bec 67 65 2c 20 73 71 6c 20 46 52 4f 4d 20 27 25 71  ge, sql FROM '%q
28bed 27 2e 25 73 22 2c 0a 20 20 20 20 20 20 20 20 64  '.%s",.        d
28bee 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
28bef 65 2c 20 7a 4d 61 73 74 65 72 4e 61 6d 65 29 3b  e, zMasterName);
28bf0 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74  .    (void)sqlit
28bf1 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b  e3SafetyOff(db);
28bf2 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
28bf3 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49  OMIT_AUTHORIZATI
28bf4 4f 4e 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 69  ON.    {.      i
28bf5 6e 74 20 28 2a 78 41 75 74 68 29 28 76 6f 69 64  nt (*xAuth)(void
28bf6 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72  *,int,const char
28bf7 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f  *,const char*,co
28bf8 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20  nst char*,const 
28bf9 63 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 78 41  char*);.      xA
28bfa 75 74 68 20 3d 20 64 62 2d 3e 78 41 75 74 68 3b  uth = db->xAuth;
28bfb 0a 20 20 20 20 20 20 64 62 2d 3e 78 41 75 74 68  .      db->xAuth
28bfc 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 20   = 0;.#endif.   
28bfd 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
28bfe 65 78 65 63 28 64 62 2c 20 7a 53 71 6c 2c 20 73  exec(db, zSql, s
28bff 71 6c 69 74 65 33 49 6e 69 74 43 61 6c 6c 62 61  qlite3InitCallba
28c00 63 6b 2c 20 26 69 6e 69 74 44 61 74 61 2c 20 30  ck, &initData, 0
28c01 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
28c02 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41  E_OMIT_AUTHORIZA
28c03 54 49 4f 4e 0a 20 20 20 20 20 20 64 62 2d 3e 78  TION.      db->x
28c04 41 75 74 68 20 3d 20 78 41 75 74 68 3b 0a 20 20  Auth = xAuth;.  
28c05 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 69    }.#endif.    i
28c06 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
28c07 20 29 20 72 63 20 3d 20 69 6e 69 74 44 61 74 61   ) rc = initData
28c08 2e 72 63 3b 0a 20 20 20 20 28 76 6f 69 64 29 73  .rc;.    (void)s
28c09 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64  qlite3SafetyOn(d
28c0a 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  b);.    sqlite3D
28c0b 62 46 72 65 65 28 64 62 2c 20 7a 53 71 6c 29 3b  bFree(db, zSql);
28c0c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
28c0d 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 0a 20 20 20  OMIT_ANALYZE.   
28c0e 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
28c0f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  OK ){.      sqli
28c10 74 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61 64 28  te3AnalysisLoad(
28c11 64 62 2c 20 69 44 62 29 3b 0a 20 20 20 20 7d 0a  db, iDb);.    }.
28c12 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 69 66 28  #endif.  }.  if(
28c13 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
28c14 64 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  d ){.    rc = SQ
28c15 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
28c16 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65  sqlite3ResetInte
28c17 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30  rnalSchema(db, 0
28c18 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d  );.  }.  if( rc=
28c19 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 28 64  =SQLITE_OK || (d
28c1a 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f  b->flags&SQLITE_
28c1b 52 65 63 6f 76 65 72 79 4d 6f 64 65 29 29 7b 0a  RecoveryMode)){.
28c1c 20 20 20 20 2f 2a 20 42 6c 61 63 6b 20 6d 61 67      /* Black mag
28c1d 69 63 3a 20 49 66 20 74 68 65 20 53 51 4c 49 54  ic: If the SQLIT
28c1e 45 5f 52 65 63 6f 76 65 72 79 4d 6f 64 65 20 66  E_RecoveryMode f
28c1f 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e  lag is set, then
28c20 20 63 6f 6e 73 69 64 65 72 0a 20 20 20 20 2a 2a   consider.    **
28c21 20 74 68 65 20 73 63 68 65 6d 61 20 6c 6f 61 64   the schema load
28c22 65 64 2c 20 65 76 65 6e 20 69 66 20 65 72 72 6f  ed, even if erro
28c23 72 73 20 6f 63 63 75 72 72 65 64 2e 20 49 6e 20  rs occurred. In 
28c24 74 68 69 73 20 73 69 74 75 61 74 69 6f 6e 20 74  this situation t
28c25 68 65 20 0a 20 20 20 20 2a 2a 20 63 75 72 72 65  he .    ** curre
28c26 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  nt sqlite3_prepa
28c27 72 65 28 29 20 6f 70 65 72 61 74 69 6f 6e 20 77  re() operation w
28c28 69 6c 6c 20 66 61 69 6c 2c 20 62 75 74 20 74 68  ill fail, but th
28c29 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 6e 65 0a  e following one.
28c2a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 61 74 74 65      ** will atte
28c2b 6d 70 74 20 74 6f 20 63 6f 6d 70 69 6c 65 20 74  mpt to compile t
28c2c 68 65 20 73 75 70 70 6c 69 65 64 20 73 74 61 74  he supplied stat
28c2d 65 6d 65 6e 74 20 61 67 61 69 6e 73 74 20 77 68  ement against wh
28c2e 61 74 65 76 65 72 20 73 75 62 73 65 74 0a 20 20  atever subset.  
28c2f 20 20 2a 2a 20 6f 66 20 74 68 65 20 73 63 68 65    ** of the sche
28c30 6d 61 20 77 61 73 20 6c 6f 61 64 65 64 20 62 65  ma was loaded be
28c31 66 6f 72 65 20 74 68 65 20 65 72 72 6f 72 20 6f  fore the error o
28c32 63 63 75 72 72 65 64 2e 20 54 68 65 20 70 72 69  ccurred. The pri
28c33 6d 61 72 79 0a 20 20 20 20 2a 2a 20 70 75 72 70  mary.    ** purp
28c34 6f 73 65 20 6f 66 20 74 68 69 73 20 69 73 20 74  ose of this is t
28c35 6f 20 61 6c 6c 6f 77 20 61 63 63 65 73 73 20 74  o allow access t
28c36 6f 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73  o the sqlite_mas
28c37 74 65 72 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a  ter table.    **
28c38 20 65 76 65 6e 20 77 68 65 6e 20 69 74 73 20 63   even when its c
28c39 6f 6e 74 65 6e 74 73 20 68 61 76 65 20 62 65 65  ontents have bee
28c3a 6e 20 63 6f 72 72 75 70 74 65 64 2e 0a 20 20 20  n corrupted..   
28c3b 20 2a 2f 0a 20 20 20 20 44 62 53 65 74 50 72 6f   */.    DbSetPro
28c3c 70 65 72 74 79 28 64 62 2c 20 69 44 62 2c 20 44  perty(db, iDb, D
28c3d 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 3b  B_SchemaLoaded);
28c3e 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
28c3f 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4a  _OK;.  }..  /* J
28c40 75 6d 70 20 68 65 72 65 20 66 6f 72 20 61 6e 20  ump here for an 
28c41 65 72 72 6f 72 20 74 68 61 74 20 6f 63 63 75 72  error that occur
28c42 73 20 61 66 74 65 72 20 73 75 63 63 65 73 73 66  s after successf
28c43 75 6c 6c 79 20 61 6c 6c 6f 63 61 74 69 6e 67 0a  ully allocating.
28c44 20 20 2a 2a 20 63 75 72 4d 61 69 6e 20 61 6e 64    ** curMain and
28c45 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33   calling sqlite3
28c46 42 74 72 65 65 45 6e 74 65 72 28 29 2e 20 46 6f  BtreeEnter(). Fo
28c47 72 20 61 6e 20 65 72 72 6f 72 20 74 68 61 74 20  r an error that 
28c48 6f 63 63 75 72 73 0a 20 20 2a 2a 20 62 65 66 6f  occurs.  ** befo
28c49 72 65 20 74 68 61 74 20 70 6f 69 6e 74 2c 20 6a  re that point, j
28c4a 75 6d 70 20 74 6f 20 65 72 72 6f 72 5f 6f 75 74  ump to error_out
28c4b 2e 0a 20 20 2a 2f 0a 69 6e 69 74 6f 6e 65 5f 65  ..  */.initone_e
28c4c 72 72 6f 72 5f 6f 75 74 3a 0a 20 20 69 66 28 20  rror_out:.  if( 
28c4d 6f 70 65 6e 65 64 54 72 61 6e 73 61 63 74 69 6f  openedTransactio
28c4e 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  n ){.    sqlite3
28c4f 42 74 72 65 65 43 6f 6d 6d 69 74 28 70 44 62 2d  BtreeCommit(pDb-
28c50 3e 70 42 74 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  >pBt);.  }.  sql
28c51 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
28c52 44 62 2d 3e 70 42 74 29 3b 0a 0a 65 72 72 6f 72  Db->pBt);..error
28c53 5f 6f 75 74 3a 0a 20 20 69 66 28 20 72 63 3d 3d  _out:.  if( rc==
28c54 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20  SQLITE_NOMEM || 
28c55 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52  rc==SQLITE_IOERR
28c56 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 64 62  _NOMEM ){.    db
28c57 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
28c58 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   1;.  }.  return
28c59 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e   rc;.}../*.** In
28c5a 69 74 69 61 6c 69 7a 65 20 61 6c 6c 20 64 61 74  itialize all dat
28c5b 61 62 61 73 65 20 66 69 6c 65 73 20 2d 20 74 68  abase files - th
28c5c 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
28c5d 66 69 6c 65 2c 20 74 68 65 20 66 69 6c 65 0a 2a  file, the file.*
28c5e 2a 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20  * used to store 
28c5f 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73  temporary tables
28c60 2c 20 61 6e 64 20 61 6e 79 20 61 64 64 69 74 69  , and any additi
28c61 6f 6e 61 6c 20 64 61 74 61 62 61 73 65 20 66 69  onal database fi
28c62 6c 65 73 0a 2a 2a 20 63 72 65 61 74 65 64 20 75  les.** created u
28c63 73 69 6e 67 20 41 54 54 41 43 48 20 73 74 61 74  sing ATTACH stat
28c64 65 6d 65 6e 74 73 2e 20 20 52 65 74 75 72 6e 20  ements.  Return 
28c65 61 20 73 75 63 63 65 73 73 20 63 6f 64 65 2e 20  a success code. 
28c66 20 49 66 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20   If an.** error 
28c67 6f 63 63 75 72 73 2c 20 77 72 69 74 65 20 61 6e  occurs, write an
28c68 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69   error message i
28c69 6e 74 6f 20 2a 70 7a 45 72 72 4d 73 67 2e 0a 2a  nto *pzErrMsg..*
28c6a 2a 0a 2a 2a 20 41 66 74 65 72 20 61 20 64 61 74  *.** After a dat
28c6b 61 62 61 73 65 20 69 73 20 69 6e 69 74 69 61 6c  abase is initial
28c6c 69 7a 65 64 2c 20 74 68 65 20 44 42 5f 53 63 68  ized, the DB_Sch
28c6d 65 6d 61 4c 6f 61 64 65 64 20 62 69 74 20 69 73  emaLoaded bit is
28c6e 20 73 65 74 0a 2a 2a 20 62 69 74 20 69 73 20 73   set.** bit is s
28c6f 65 74 20 69 6e 20 74 68 65 20 66 6c 61 67 73 20  et in the flags 
28c70 66 69 65 6c 64 20 6f 66 20 74 68 65 20 44 62 20  field of the Db 
28c71 73 74 72 75 63 74 75 72 65 2e 20 49 66 20 74 68  structure. If th
28c72 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69  e database.** fi
28c73 6c 65 20 77 61 73 20 6f 66 20 7a 65 72 6f 2d 6c  le was of zero-l
28c74 65 6e 67 74 68 2c 20 74 68 65 6e 20 74 68 65 20  ength, then the 
28c75 44 42 5f 45 6d 70 74 79 20 66 6c 61 67 20 69 73  DB_Empty flag is
28c76 20 61 6c 73 6f 20 73 65 74 2e 0a 2a 2f 0a 53 51   also set..*/.SQ
28c77 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
28c78 20 73 71 6c 69 74 65 33 49 6e 69 74 28 73 71 6c   sqlite3Init(sql
28c79 69 74 65 33 20 2a 64 62 2c 20 63 68 61 72 20 2a  ite3 *db, char *
28c7a 2a 70 7a 45 72 72 4d 73 67 29 7b 0a 20 20 69 6e  *pzErrMsg){.  in
28c7b 74 20 69 2c 20 72 63 3b 0a 20 20 69 6e 74 20 63  t i, rc;.  int c
28c7c 6f 6d 6d 69 74 5f 69 6e 74 65 72 6e 61 6c 20 3d  ommit_internal =
28c7d 20 21 28 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c   !(db->flags&SQL
28c7e 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65  ITE_InternChange
28c7f 73 29 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28  s);.  .  assert(
28c80 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
28c81 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29  eld(db->mutex) )
28c82 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  ;.  rc = SQLITE_
28c83 4f 4b 3b 0a 20 20 64 62 2d 3e 69 6e 69 74 2e 62  OK;.  db->init.b
28c84 75 73 79 20 3d 20 31 3b 0a 20 20 66 6f 72 28 69  usy = 1;.  for(i
28c85 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  =0; rc==SQLITE_O
28c86 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  K && i<db->nDb; 
28c87 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 44 62  i++){.    if( Db
28c88 48 61 73 50 72 6f 70 65 72 74 79 28 64 62 2c 20  HasProperty(db, 
28c89 69 2c 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64  i, DB_SchemaLoad
28c8a 65 64 29 20 7c 7c 20 69 3d 3d 31 20 29 20 63 6f  ed) || i==1 ) co
28c8b 6e 74 69 6e 75 65 3b 0a 20 20 20 20 72 63 20 3d  ntinue;.    rc =
28c8c 20 73 71 6c 69 74 65 33 49 6e 69 74 4f 6e 65 28   sqlite3InitOne(
28c8d 64 62 2c 20 69 2c 20 70 7a 45 72 72 4d 73 67 29  db, i, pzErrMsg)
28c8e 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a  ;.    if( rc ){.
28c8f 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 73        sqlite3Res
28c90 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61  etInternalSchema
28c91 28 64 62 2c 20 69 29 3b 0a 20 20 20 20 7d 0a 20  (db, i);.    }. 
28c92 20 7d 0a 0a 20 20 2f 2a 20 4f 6e 63 65 20 61 6c   }..  /* Once al
28c93 6c 20 74 68 65 20 6f 74 68 65 72 20 64 61 74 61  l the other data
28c94 62 61 73 65 73 20 68 61 76 65 20 62 65 65 6e 20  bases have been 
28c95 69 6e 69 74 69 61 6c 69 73 65 64 2c 20 6c 6f 61  initialised, loa
28c96 64 20 74 68 65 20 73 63 68 65 6d 61 0a 20 20 2a  d the schema.  *
28c97 2a 20 66 6f 72 20 74 68 65 20 54 45 4d 50 20 64  * for the TEMP d
28c98 61 74 61 62 61 73 65 2e 20 54 68 69 73 20 69 73  atabase. This is
28c99 20 6c 6f 61 64 65 64 20 6c 61 73 74 2c 20 61 73   loaded last, as
28c9a 20 74 68 65 20 54 45 4d 50 20 64 61 74 61 62 61   the TEMP databa
28c9b 73 65 0a 20 20 2a 2a 20 73 63 68 65 6d 61 20 6d  se.  ** schema m
28c9c 61 79 20 63 6f 6e 74 61 69 6e 20 72 65 66 65 72  ay contain refer
28c9d 65 6e 63 65 73 20 74 6f 20 6f 62 6a 65 63 74 73  ences to objects
28c9e 20 69 6e 20 6f 74 68 65 72 20 64 61 74 61 62 61   in other databa
28c9f 73 65 73 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65  ses..  */.#ifnde
28ca0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45  f SQLITE_OMIT_TE
28ca1 4d 50 44 42 0a 20 20 69 66 28 20 72 63 3d 3d 53  MPDB.  if( rc==S
28ca2 51 4c 49 54 45 5f 4f 4b 20 26 26 20 41 4c 57 41  QLITE_OK && ALWA
28ca3 59 53 28 64 62 2d 3e 6e 44 62 3e 31 29 0a 20 20  YS(db->nDb>1).  
28ca4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28ca5 20 20 26 26 20 21 44 62 48 61 73 50 72 6f 70 65    && !DbHasPrope
28ca6 72 74 79 28 64 62 2c 20 31 2c 20 44 42 5f 53 63  rty(db, 1, DB_Sc
28ca7 68 65 6d 61 4c 6f 61 64 65 64 29 20 29 7b 0a 20  hemaLoaded) ){. 
28ca8 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 49     rc = sqlite3I
28ca9 6e 69 74 4f 6e 65 28 64 62 2c 20 31 2c 20 70 7a  nitOne(db, 1, pz
28caa 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 69 66 28  ErrMsg);.    if(
28cab 20 72 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c   rc ){.      sql
28cac 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61  ite3ResetInterna
28cad 6c 53 63 68 65 6d 61 28 64 62 2c 20 31 29 3b 0a  lSchema(db, 1);.
28cae 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
28caf 0a 0a 20 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73  ..  db->init.bus
28cb0 79 20 3d 20 30 3b 0a 20 20 69 66 28 20 72 63 3d  y = 0;.  if( rc=
28cb1 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 63 6f  =SQLITE_OK && co
28cb2 6d 6d 69 74 5f 69 6e 74 65 72 6e 61 6c 20 29 7b  mmit_internal ){
28cb3 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6d 6d  .    sqlite3Comm
28cb4 69 74 49 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65  itInternalChange
28cb5 73 28 64 62 29 3b 0a 20 20 7d 0a 0a 20 20 72 65  s(db);.  }..  re
28cb6 74 75 72 6e 20 72 63 3b 20 0a 7d 0a 0a 2f 2a 0a  turn rc; .}../*.
28cb7 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
28cb8 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68  is a no-op if th
28cb9 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  e database schem
28cba 61 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 69  a is already ini
28cbb 74 69 61 6c 69 73 65 64 2e 0a 2a 2a 20 4f 74 68  tialised..** Oth
28cbc 65 72 77 69 73 65 2c 20 74 68 65 20 73 63 68 65  erwise, the sche
28cbd 6d 61 20 69 73 20 6c 6f 61 64 65 64 2e 20 41 6e  ma is loaded. An
28cbe 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72   error code is r
28cbf 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49  eturned..*/.SQLI
28cc0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
28cc1 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61  qlite3ReadSchema
28cc2 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b  (Parse *pParse){
28cc3 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
28cc4 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c 69 74 65 33  TE_OK;.  sqlite3
28cc5 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
28cc6 62 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  b;.  assert( sql
28cc7 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
28cc8 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
28cc9 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75  if( !db->init.bu
28cca 73 79 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  sy ){.    rc = s
28ccb 71 6c 69 74 65 33 49 6e 69 74 28 64 62 2c 20 26  qlite3Init(db, &
28ccc 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 29  pParse->zErrMsg)
28ccd 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 21 3d  ;.  }.  if( rc!=
28cce 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
28ccf 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 72 63   pParse->rc = rc
28cd0 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45  ;.    pParse->nE
28cd1 72 72 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rr++;.  }.  retu
28cd2 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  rn rc;.}.../*.**
28cd3 20 43 68 65 63 6b 20 73 63 68 65 6d 61 20 63 6f   Check schema co
28cd4 6f 6b 69 65 73 20 69 6e 20 61 6c 6c 20 64 61 74  okies in all dat
28cd5 61 62 61 73 65 73 2e 20 20 49 66 20 61 6e 79 20  abases.  If any 
28cd6 63 6f 6f 6b 69 65 20 69 73 20 6f 75 74 0a 2a 2a  cookie is out.**
28cd7 20 6f 66 20 64 61 74 65 20 73 65 74 20 70 50 61   of date set pPa
28cd8 72 73 65 2d 3e 72 63 20 74 6f 20 53 51 4c 49 54  rse->rc to SQLIT
28cd9 45 5f 53 43 48 45 4d 41 2e 20 20 49 66 20 61 6c  E_SCHEMA.  If al
28cda 6c 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 73  l schema cookies
28cdb 0a 2a 2a 20 6d 61 6b 65 20 6e 6f 20 63 68 61 6e  .** make no chan
28cdc 67 65 73 20 74 6f 20 70 50 61 72 73 65 2d 3e 72  ges to pParse->r
28cdd 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  c..*/.static voi
28cde 64 20 73 63 68 65 6d 61 49 73 56 61 6c 69 64 28  d schemaIsValid(
28cdf 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a  Parse *pParse){.
28ce0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
28ce1 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e  pParse->db;.  in
28ce2 74 20 69 44 62 3b 0a 20 20 69 6e 74 20 72 63 3b  t iDb;.  int rc;
28ce3 0a 20 20 69 6e 74 20 63 6f 6f 6b 69 65 3b 0a 0a  .  int cookie;..
28ce4 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65    assert( pParse
28ce5 2d 3e 63 68 65 63 6b 53 63 68 65 6d 61 20 29 3b  ->checkSchema );
28ce6 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
28ce7 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62  e3_mutex_held(db
28ce8 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f  ->mutex) );.  fo
28ce9 72 28 69 44 62 3d 30 3b 20 69 44 62 3c 64 62 2d  r(iDb=0; iDb<db-
28cea 3e 6e 44 62 3b 20 69 44 62 2b 2b 29 7b 0a 20 20  >nDb; iDb++){.  
28ceb 20 20 69 6e 74 20 6f 70 65 6e 65 64 54 72 61 6e    int openedTran
28cec 73 61 63 74 69 6f 6e 20 3d 20 30 3b 20 20 20 20  saction = 0;    
28ced 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
28cee 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  a transaction is
28cef 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 20 20 42   opened */.    B
28cf0 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e  tree *pBt = db->
28cf1 61 44 62 5b 69 44 62 5d 2e 70 42 74 3b 20 20 20  aDb[iDb].pBt;   
28cf2 20 20 2f 2a 20 42 74 72 65 65 20 64 61 74 61 62    /* Btree datab
28cf3 61 73 65 20 74 6f 20 72 65 61 64 20 63 6f 6f 6b  ase to read cook
28cf4 69 65 20 66 72 6f 6d 20 2a 2f 0a 20 20 20 20 69  ie from */.    i
28cf5 66 28 20 70 42 74 3d 3d 30 20 29 20 63 6f 6e 74  f( pBt==0 ) cont
28cf6 69 6e 75 65 3b 0a 0a 20 20 20 20 2f 2a 20 49 66  inue;..    /* If
28cf7 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20 61 6c   there is not al
28cf8 72 65 61 64 79 20 61 20 72 65 61 64 2d 6f 6e 6c  ready a read-onl
28cf9 79 20 28 6f 72 20 72 65 61 64 2d 77 72 69 74 65  y (or read-write
28cfa 29 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70  ) transaction op
28cfb 65 6e 65 64 0a 20 20 20 20 2a 2a 20 6f 6e 20 74  ened.    ** on t
28cfc 68 65 20 62 2d 74 72 65 65 20 64 61 74 61 62 61  he b-tree databa
28cfd 73 65 2c 20 6f 70 65 6e 20 6f 6e 65 20 6e 6f 77  se, open one now
28cfe 2e 20 49 66 20 61 20 74 72 61 6e 73 61 63 74 69  . If a transacti
28cff 6f 6e 20 69 73 20 6f 70 65 6e 65 64 2c 20 69 74  on is opened, it
28d00 20 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62 65   .    ** will be
28d01 20 63 6c 6f 73 65 64 20 69 6d 6d 65 64 69 61 74   closed immediat
28d02 65 6c 79 20 61 66 74 65 72 20 72 65 61 64 69 6e  ely after readin
28d03 67 20 74 68 65 20 6d 65 74 61 2d 76 61 6c 75 65  g the meta-value
28d04 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 73 71  . */.    if( !sq
28d05 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 52 65  lite3BtreeIsInRe
28d06 61 64 54 72 61 6e 73 28 70 42 74 29 20 29 7b 0a  adTrans(pBt) ){.
28d07 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
28d08 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e  e3BtreeBeginTran
28d09 73 28 70 42 74 2c 20 30 29 3b 0a 20 20 20 20 20  s(pBt, 0);.     
28d0a 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
28d0b 4e 4f 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c  NOMEM || rc==SQL
28d0c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20  ITE_IOERR_NOMEM 
28d0d 29 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6d  ){.        db->m
28d0e 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
28d0f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
28d10 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
28d11 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20   ) return;.     
28d12 20 6f 70 65 6e 65 64 54 72 61 6e 73 61 63 74 69   openedTransacti
28d13 6f 6e 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 0a 20  on = 1;.    }.. 
28d14 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 73     /* Read the s
28d15 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 66 72 6f  chema cookie fro
28d16 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  m the database. 
28d17 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 6d  If it does not m
28d18 61 74 63 68 20 74 68 65 20 0a 20 20 20 20 2a 2a  atch the .    **
28d19 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 61 73   value stored as
28d1a 20 70 61 72 74 20 6f 66 20 74 68 65 20 69 6e 20   part of the in 
28d1b 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 63  the in-memory sc
28d1c 68 65 6d 61 20 72 65 70 72 65 73 65 6e 74 61 74  hema representat
28d1d 69 6f 6e 2c 0a 20 20 20 20 2a 2a 20 73 65 74 20  ion,.    ** set 
28d1e 50 61 72 73 65 2e 72 63 20 74 6f 20 53 51 4c 49  Parse.rc to SQLI
28d1f 54 45 5f 53 43 48 45 4d 41 2e 20 2a 2f 0a 20 20  TE_SCHEMA. */.  
28d20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65    sqlite3BtreeGe
28d21 74 4d 65 74 61 28 70 42 74 2c 20 42 54 52 45 45  tMeta(pBt, BTREE
28d22 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 2c  _SCHEMA_VERSION,
28d23 20 28 75 33 32 20 2a 29 26 63 6f 6f 6b 69 65 29   (u32 *)&cookie)
28d24 3b 0a 20 20 20 20 69 66 28 20 63 6f 6f 6b 69 65  ;.    if( cookie
28d25 21 3d 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70  !=db->aDb[iDb].p
28d26 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63  Schema->schema_c
28d27 6f 6f 6b 69 65 20 29 7b 0a 20 20 20 20 20 20 70  ookie ){.      p
28d28 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49  Parse->rc = SQLI
28d29 54 45 5f 53 43 48 45 4d 41 3b 0a 20 20 20 20 7d  TE_SCHEMA;.    }
28d2a 0a 0a 20 20 20 20 2f 2a 20 43 6c 6f 73 65 20 74  ..    /* Close t
28d2b 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20  he transaction, 
28d2c 69 66 20 6f 6e 65 20 77 61 73 20 6f 70 65 6e 65  if one was opene
28d2d 64 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 6f 70  d. */.    if( op
28d2e 65 6e 65 64 54 72 61 6e 73 61 63 74 69 6f 6e 20  enedTransaction 
28d2f 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
28d30 42 74 72 65 65 43 6f 6d 6d 69 74 28 70 42 74 29  BtreeCommit(pBt)
28d31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  ;.    }.  }.}../
28d32 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 73  *.** Convert a s
28d33 63 68 65 6d 61 20 70 6f 69 6e 74 65 72 20 69 6e  chema pointer in
28d34 74 6f 20 74 68 65 20 69 44 62 20 69 6e 64 65 78  to the iDb index
28d35 20 74 68 61 74 20 69 6e 64 69 63 61 74 65 73 0a   that indicates.
28d36 2a 2a 20 77 68 69 63 68 20 64 61 74 61 62 61 73  ** which databas
28d37 65 20 66 69 6c 65 20 69 6e 20 64 62 2d 3e 61 44  e file in db->aD
28d38 62 5b 5d 20 74 68 65 20 73 63 68 65 6d 61 20 72  b[] the schema r
28d39 65 66 65 72 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20  efers to..**.** 
28d3a 49 66 20 74 68 65 20 73 61 6d 65 20 64 61 74 61  If the same data
28d3b 62 61 73 65 20 69 73 20 61 74 74 61 63 68 65 64  base is attached
28d3c 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c   more than once,
28d3d 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 74   the first.** at
28d3e 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 20  tached database 
28d3f 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a  is returned..*/.
28d40 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
28d41 6e 74 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61  nt sqlite3Schema
28d42 54 6f 49 6e 64 65 78 28 73 71 6c 69 74 65 33 20  ToIndex(sqlite3 
28d43 2a 64 62 2c 20 53 63 68 65 6d 61 20 2a 70 53 63  *db, Schema *pSc
28d44 68 65 6d 61 29 7b 0a 20 20 69 6e 74 20 69 20 3d  hema){.  int i =
28d45 20 2d 31 30 30 30 30 30 30 3b 0a 0a 20 20 2f 2a   -1000000;..  /*
28d46 20 49 66 20 70 53 63 68 65 6d 61 20 69 73 20 4e   If pSchema is N
28d47 55 4c 4c 2c 20 74 68 65 6e 20 72 65 74 75 72 6e  ULL, then return
28d48 20 2d 31 30 30 30 30 30 30 2e 20 54 68 69 73 20   -1000000. This 
28d49 68 61 70 70 65 6e 73 20 77 68 65 6e 20 63 6f 64  happens when cod
28d4a 65 20 69 6e 20 0a 20 20 2a 2a 20 65 78 70 72 2e  e in .  ** expr.
28d4b 63 20 69 73 20 74 72 79 69 6e 67 20 74 6f 20 72  c is trying to r
28d4c 65 73 6f 6c 76 65 20 61 20 72 65 66 65 72 65 6e  esolve a referen
28d4d 63 65 20 74 6f 20 61 20 74 72 61 6e 73 69 65 6e  ce to a transien
28d4e 74 20 74 61 62 6c 65 20 28 69 2e 65 2e 20 6f 6e  t table (i.e. on
28d4f 65 0a 20 20 2a 2a 20 63 72 65 61 74 65 64 20 62  e.  ** created b
28d50 79 20 61 20 73 75 62 2d 73 65 6c 65 63 74 29 2e  y a sub-select).
28d51 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68   In this case th
28d52 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f  e return value o
28d53 66 20 74 68 69 73 20 0a 20 20 2a 2a 20 66 75 6e  f this .  ** fun
28d54 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65 76  ction should nev
28d55 65 72 20 62 65 20 75 73 65 64 2e 0a 20 20 2a 2a  er be used..  **
28d56 0a 20 20 2a 2a 20 57 65 20 72 65 74 75 72 6e 20  .  ** We return 
28d57 2d 31 30 30 30 30 30 30 20 69 6e 73 74 65 61 64  -1000000 instead
28d58 20 6f 66 20 74 68 65 20 6d 6f 72 65 20 75 73 75   of the more usu
28d59 61 6c 20 2d 31 20 73 69 6d 70 6c 79 20 62 65 63  al -1 simply bec
28d5a 61 75 73 65 20 75 73 69 6e 67 0a 20 20 2a 2a 20  ause using.  ** 
28d5b 2d 31 30 30 30 30 30 30 20 61 73 20 74 68 65 20  -1000000 as the 
28d5c 69 6e 63 6f 72 72 65 63 74 20 69 6e 64 65 78 20  incorrect index 
28d5d 69 6e 74 6f 20 64 62 2d 3e 61 44 62 5b 5d 20 69  into db->aDb[] i
28d5e 73 20 6d 75 63 68 20 0a 20 20 2a 2a 20 6d 6f 72  s much .  ** mor
28d5f 65 20 6c 69 6b 65 6c 79 20 74 6f 20 63 61 75 73  e likely to caus
28d60 65 20 61 20 73 65 67 66 61 75 6c 74 20 74 68 61  e a segfault tha
28d61 6e 20 2d 31 20 28 6f 66 20 63 6f 75 72 73 65 20  n -1 (of course 
28d62 74 68 65 72 65 20 61 72 65 20 61 73 73 65 72 74  there are assert
28d63 28 29 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e  ().  ** statemen
28d64 74 73 20 74 6f 6f 2c 20 62 75 74 20 69 74 20 6e  ts too, but it n
28d65 65 76 65 72 20 68 75 72 74 73 20 74 6f 20 70 6c  ever hurts to pl
28d66 61 79 20 74 68 65 20 6f 64 64 73 29 2e 0a 20 20  ay the odds)..  
28d67 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  */.  assert( sql
28d68 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
28d69 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
28d6a 69 66 28 20 70 53 63 68 65 6d 61 20 29 7b 0a 20  if( pSchema ){. 
28d6b 20 20 20 66 6f 72 28 69 3d 30 3b 20 41 4c 57 41     for(i=0; ALWA
28d6c 59 53 28 69 3c 64 62 2d 3e 6e 44 62 29 3b 20 69  YS(i<db->nDb); i
28d6d 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 64  ++){.      if( d
28d6e 62 2d 3e 61 44 62 5b 69 5d 2e 70 53 63 68 65 6d  b->aDb[i].pSchem
28d6f 61 3d 3d 70 53 63 68 65 6d 61 20 29 7b 0a 20 20  a==pSchema ){.  
28d70 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
28d71 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61     }.    }.    a
28d72 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69  ssert( i>=0 && i
28d73 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 7d 0a  <db->nDb );.  }.
28d74 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a 2f    return i;.}../
28d75 2a 0a 2a 2a 20 43 6f 6d 70 69 6c 65 20 74 68 65  *.** Compile the
28d76 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 53   UTF-8 encoded S
28d77 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 7a 53 71  QL statement zSq
28d78 6c 20 69 6e 74 6f 20 61 20 73 74 61 74 65 6d 65  l into a stateme
28d79 6e 74 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74  nt handle..*/.st
28d7a 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33  atic int sqlite3
28d7b 50 72 65 70 61 72 65 28 0a 20 20 73 71 6c 69 74  Prepare(.  sqlit
28d7c 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
28d7d 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
28d7e 20 68 61 6e 64 6c 65 2e 20 2a 2f 0a 20 20 63 6f   handle. */.  co
28d7f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20  nst char *zSql, 
28d80 20 20 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38          /* UTF-8
28d81 20 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61   encoded SQL sta
28d82 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74  tement. */.  int
28d83 20 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20 20   nBytes,        
28d84 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68         /* Length
28d85 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65   of zSql in byte
28d86 73 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65  s. */.  int save
28d87 53 71 6c 46 6c 61 67 2c 20 20 20 20 20 20 20 20  SqlFlag,        
28d88 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 63 6f 70    /* True to cop
28d89 79 20 53 51 4c 20 74 65 78 74 20 69 6e 74 6f 20  y SQL text into 
28d8a 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  the sqlite3_stmt
28d8b 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74   */.  sqlite3_st
28d8c 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20  mt **ppStmt,    
28d8d 2f 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65  /* OUT: A pointe
28d8e 72 20 74 6f 20 74 68 65 20 70 72 65 70 61 72 65  r to the prepare
28d8f 64 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  d statement */. 
28d90 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a   const char **pz
28d91 54 61 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55  Tail       /* OU
28d92 54 3a 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64  T: End of parsed
28d93 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20   string */.){.  
28d94 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20  Parse *pParse;  
28d95 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72            /* Par
28d96 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
28d97 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 20    char *zErrMsg 
28d98 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 45  = 0;        /* E
28d99 72 72 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f 0a  rror message */.
28d9a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
28d9b 45 5f 4f 4b 3b 20 20 20 20 20 20 20 2f 2a 20 52  E_OK;       /* R
28d9c 65 73 75 6c 74 20 63 6f 64 65 20 2a 2f 0a 20 20  esult code */.  
28d9d 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
28d9e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
28d9f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20  p counter */..  
28da0 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20  /* Allocate the 
28da1 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  parsing context 
28da2 2a 2f 0a 20 20 70 50 61 72 73 65 20 3d 20 73 71  */.  pParse = sq
28da3 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 5a  lite3StackAllocZ
28da4 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a  ero(db, sizeof(*
28da5 70 50 61 72 73 65 29 29 3b 0a 20 20 69 66 28 20  pParse));.  if( 
28da6 70 50 61 72 73 65 3d 3d 30 20 29 7b 0a 20 20 20  pParse==0 ){.   
28da7 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
28da8 45 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64  EM;.    goto end
28da9 5f 70 72 65 70 61 72 65 3b 0a 20 20 7d 0a 0a 20  _prepare;.  }.. 
28daa 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65   if( sqlite3Safe
28dab 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20  tyOn(db) ){.    
28dac 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55  rc = SQLITE_MISU
28dad 53 45 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64  SE;.    goto end
28dae 5f 70 72 65 70 61 72 65 3b 0a 20 20 7d 0a 20 20  _prepare;.  }.  
28daf 61 73 73 65 72 74 28 20 70 70 53 74 6d 74 20 26  assert( ppStmt &
28db0 26 20 2a 70 70 53 74 6d 74 3d 3d 30 20 29 3b 0a  & *ppStmt==0 );.
28db1 20 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d    assert( !db->m
28db2 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
28db3 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
28db4 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e  _mutex_held(db->
28db5 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20  mutex) );..  /* 
28db6 43 68 65 63 6b 20 74 6f 20 76 65 72 69 66 79 20  Check to verify 
28db7 74 68 61 74 20 69 74 20 69 73 20 70 6f 73 73 69  that it is possi
28db8 62 6c 65 20 74 6f 20 67 65 74 20 61 20 72 65 61  ble to get a rea
28db9 64 20 6c 6f 63 6b 20 6f 6e 20 61 6c 6c 0a 20 20  d lock on all.  
28dba 2a 2a 20 64 61 74 61 62 61 73 65 20 73 63 68 65  ** database sche
28dbb 6d 61 73 2e 20 20 54 68 65 20 69 6e 61 62 69 6c  mas.  The inabil
28dbc 69 74 79 20 74 6f 20 67 65 74 20 61 20 72 65 61  ity to get a rea
28dbd 64 20 6c 6f 63 6b 20 69 6e 64 69 63 61 74 65 73  d lock indicates
28dbe 20 74 68 61 74 0a 20 20 2a 2a 20 73 6f 6d 65 20   that.  ** some 
28dbf 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63  other database c
28dc0 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 68 6f 6c  onnection is hol
28dc1 64 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63  ding a write-loc
28dc2 6b 2c 20 77 68 69 63 68 20 69 6e 0a 20 20 2a 2a  k, which in.  **
28dc3 20 74 75 72 6e 20 6d 65 61 6e 73 20 74 68 61 74   turn means that
28dc4 20 74 68 65 20 6f 74 68 65 72 20 63 6f 6e 6e 65   the other conne
28dc5 63 74 69 6f 6e 20 68 61 73 20 6d 61 64 65 20 75  ction has made u
28dc6 6e 63 6f 6d 6d 69 74 74 65 64 20 63 68 61 6e 67  ncommitted chang
28dc7 65 73 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 73  es.  ** to the s
28dc8 63 68 65 6d 61 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  chema..  **.  **
28dc9 20 57 65 72 65 20 77 65 20 74 6f 20 70 72 6f 63   Were we to proc
28dca 65 65 64 20 61 6e 64 20 70 72 65 70 61 72 65 20  eed and prepare 
28dcb 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 61 67  the statement ag
28dcc 61 69 6e 73 74 20 74 68 65 20 75 6e 63 6f 6d 6d  ainst the uncomm
28dcd 69 74 74 65 64 0a 20 20 2a 2a 20 73 63 68 65 6d  itted.  ** schem
28dce 61 20 63 68 61 6e 67 65 73 20 61 6e 64 20 69 66  a changes and if
28dcf 20 74 68 6f 73 65 20 73 63 68 65 6d 61 20 63 68   those schema ch
28dd0 61 6e 67 65 73 20 61 72 65 20 73 75 62 73 65 71  anges are subseq
28dd1 75 65 6e 74 6c 79 20 72 6f 6c 6c 65 64 0a 20 20  uently rolled.  
28dd2 2a 2a 20 62 61 63 6b 20 61 6e 64 20 64 69 66 66  ** back and diff
28dd3 65 72 65 6e 74 20 63 68 61 6e 67 65 73 20 61 72  erent changes ar
28dd4 65 20 6d 61 64 65 20 69 6e 20 74 68 65 69 72 20  e made in their 
28dd5 70 6c 61 63 65 2c 20 74 68 65 6e 20 77 68 65 6e  place, then when
28dd6 20 74 68 69 73 0a 20 20 2a 2a 20 70 72 65 70 61   this.  ** prepa
28dd7 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 67 6f  red statement go
28dd8 65 73 20 74 6f 20 72 75 6e 20 74 68 65 20 73 63  es to run the sc
28dd9 68 65 6d 61 20 63 6f 6f 6b 69 65 20 77 6f 75 6c  hema cookie woul
28dda 64 20 66 61 69 6c 20 74 6f 20 64 65 74 65 63 74  d fail to detect
28ddb 0a 20 20 2a 2a 20 74 68 65 20 73 63 68 65 6d 61  .  ** the schema
28ddc 20 63 68 61 6e 67 65 2e 20 20 44 69 73 61 73 74   change.  Disast
28ddd 65 72 20 77 6f 75 6c 64 20 66 6f 6c 6c 6f 77 2e  er would follow.
28dde 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20  .  **.  ** This 
28ddf 74 68 72 65 61 64 20 69 73 20 63 75 72 72 65 6e  thread is curren
28de0 74 6c 79 20 68 6f 6c 64 69 6e 67 20 6d 75 74 65  tly holding mute
28de1 78 65 73 20 6f 6e 20 61 6c 6c 20 42 74 72 65 65  xes on all Btree
28de2 73 20 28 62 65 63 61 75 73 65 0a 20 20 2a 2a 20  s (because.  ** 
28de3 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 42 74  of the sqlite3Bt
28de4 72 65 65 45 6e 74 65 72 41 6c 6c 28 29 20 69 6e  reeEnterAll() in
28de5 20 73 71 6c 69 74 65 33 4c 6f 63 6b 41 6e 64 50   sqlite3LockAndP
28de6 72 65 70 61 72 65 28 29 29 20 73 6f 20 69 74 0a  repare()) so it.
28de7 20 20 2a 2a 20 69 73 20 6e 6f 74 20 70 6f 73 73    ** is not poss
28de8 69 62 6c 65 20 66 6f 72 20 61 6e 6f 74 68 65 72  ible for another
28de9 20 74 68 72 65 61 64 20 74 6f 20 73 74 61 72 74   thread to start
28dea 20 61 20 6e 65 77 20 73 63 68 65 6d 61 20 63 68   a new schema ch
28deb 61 6e 67 65 0a 20 20 2a 2a 20 77 68 69 6c 65 20  ange.  ** while 
28dec 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
28ded 72 75 6e 6e 69 6e 67 2e 20 20 48 65 6e 63 65 2c  running.  Hence,
28dee 20 77 65 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20   we do not need 
28def 74 6f 20 68 6f 6c 64 20 0a 20 20 2a 2a 20 6c 6f  to hold .  ** lo
28df0 63 6b 73 20 6f 6e 20 74 68 65 20 73 63 68 65 6d  cks on the schem
28df1 61 2c 20 77 65 20 6a 75 73 74 20 6e 65 65 64 20  a, we just need 
28df2 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 6e 6f 62  to make sure nob
28df3 6f 64 79 20 65 6c 73 65 20 69 73 20 0a 20 20 2a  ody else is .  *
28df4 2a 20 68 6f 6c 64 69 6e 67 20 74 68 65 6d 2e 0a  * holding them..
28df5 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20 74    **.  ** Note t
28df6 68 61 74 20 73 65 74 74 69 6e 67 20 52 45 41 44  hat setting READ
28df7 5f 55 4e 43 4f 4d 4d 49 54 54 45 44 20 6f 76 65  _UNCOMMITTED ove
28df8 72 72 69 64 65 73 20 6d 6f 73 74 20 6c 6f 63 6b  rrides most lock
28df9 20 64 65 74 65 63 74 69 6f 6e 2c 0a 20 20 2a 2a   detection,.  **
28dfa 20 62 75 74 20 69 74 20 64 6f 65 73 20 2a 6e 6f   but it does *no
28dfb 74 2a 20 6f 76 65 72 72 69 64 65 20 73 63 68 65  t* override sche
28dfc 6d 61 20 6c 6f 63 6b 20 64 65 74 65 63 74 69 6f  ma lock detectio
28dfd 6e 2c 20 73 6f 20 74 68 69 73 20 61 6c 6c 20 73  n, so this all s
28dfe 74 69 6c 6c 0a 20 20 2a 2a 20 77 6f 72 6b 73 20  till.  ** works 
28dff 65 76 65 6e 20 69 66 20 52 45 41 44 5f 55 4e 43  even if READ_UNC
28e00 4f 4d 4d 49 54 54 45 44 20 69 73 20 73 65 74 2e  OMMITTED is set.
28e01 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b  .  */.  for(i=0;
28e02 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29   i<db->nDb; i++)
28e03 20 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 42   {.    Btree *pB
28e04 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70  t = db->aDb[i].p
28e05 42 74 3b 0a 20 20 20 20 69 66 28 20 70 42 74 20  Bt;.    if( pBt 
28e06 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
28e07 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c   sqlite3BtreeHol
28e08 64 73 4d 75 74 65 78 28 70 42 74 29 20 29 3b 0a  dsMutex(pBt) );.
28e09 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
28e0a 65 33 42 74 72 65 65 53 63 68 65 6d 61 4c 6f 63  e3BtreeSchemaLoc
28e0b 6b 65 64 28 70 42 74 29 3b 0a 20 20 20 20 20 20  ked(pBt);.      
28e0c 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
28e0d 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
28e0e 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a  b = db->aDb[i].z
28e0f 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 73 71  Name;.        sq
28e10 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72  lite3Error(db, r
28e11 63 2c 20 22 64 61 74 61 62 61 73 65 20 73 63 68  c, "database sch
28e12 65 6d 61 20 69 73 20 6c 6f 63 6b 65 64 3a 20 25  ema is locked: %
28e13 73 22 2c 20 7a 44 62 29 3b 0a 20 20 20 20 20 20  s", zDb);.      
28e14 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
28e15 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20  afetyOff(db);.  
28e16 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
28e17 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
28e18 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74  TE_ReadUncommitt
28e19 65 64 20 29 3b 0a 20 20 20 20 20 20 20 20 67 6f  ed );.        go
28e1a 74 6f 20 65 6e 64 5f 70 72 65 70 61 72 65 3b 0a  to end_prepare;.
28e1b 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
28e1c 7d 0a 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62  }..  sqlite3Vtab
28e1d 55 6e 6c 6f 63 6b 4c 69 73 74 28 64 62 29 3b 0a  UnlockList(db);.
28e1e 0a 20 20 70 50 61 72 73 65 2d 3e 64 62 20 3d 20  .  pParse->db = 
28e1f 64 62 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73  db;.  if( nBytes
28e20 3e 3d 30 20 26 26 20 28 6e 42 79 74 65 73 3d 3d  >=0 && (nBytes==
28e21 30 20 7c 7c 20 7a 53 71 6c 5b 6e 42 79 74 65 73  0 || zSql[nBytes
28e22 2d 31 5d 21 3d 30 29 20 29 7b 0a 20 20 20 20 63  -1]!=0) ){.    c
28e23 68 61 72 20 2a 7a 53 71 6c 43 6f 70 79 3b 0a 20  har *zSqlCopy;. 
28e24 20 20 20 69 6e 74 20 6d 78 4c 65 6e 20 3d 20 64     int mxLen = d
28e25 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
28e26 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47 54  _LIMIT_SQL_LENGT
28e27 48 5d 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65  H];.    testcase
28e28 28 20 6e 42 79 74 65 73 3d 3d 6d 78 4c 65 6e 20  ( nBytes==mxLen 
28e29 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
28e2a 20 6e 42 79 74 65 73 3d 3d 6d 78 4c 65 6e 2b 31   nBytes==mxLen+1
28e2b 20 29 3b 0a 20 20 20 20 69 66 28 20 6e 42 79 74   );.    if( nByt
28e2c 65 73 3e 6d 78 4c 65 6e 20 29 7b 0a 20 20 20 20  es>mxLen ){.    
28e2d 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
28e2e 62 2c 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47  b, SQLITE_TOOBIG
28e2f 2c 20 22 73 74 61 74 65 6d 65 6e 74 20 74 6f 6f  , "statement too
28e30 20 6c 6f 6e 67 22 29 3b 0a 20 20 20 20 20 20 28   long");.      (
28e31 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65  void)sqlite3Safe
28e32 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20  tyOff(db);.     
28e33 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69   rc = sqlite3Api
28e34 45 78 69 74 28 64 62 2c 20 53 51 4c 49 54 45 5f  Exit(db, SQLITE_
28e35 54 4f 4f 42 49 47 29 3b 0a 20 20 20 20 20 20 67  TOOBIG);.      g
28e36 6f 74 6f 20 65 6e 64 5f 70 72 65 70 61 72 65 3b  oto end_prepare;
28e37 0a 20 20 20 20 7d 0a 20 20 20 20 7a 53 71 6c 43  .    }.    zSqlC
28e38 6f 70 79 20 3d 20 73 71 6c 69 74 65 33 44 62 53  opy = sqlite3DbS
28e39 74 72 4e 44 75 70 28 64 62 2c 20 7a 53 71 6c 2c  trNDup(db, zSql,
28e3a 20 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 69 66   nBytes);.    if
28e3b 28 20 7a 53 71 6c 43 6f 70 79 20 29 7b 0a 20 20  ( zSqlCopy ){.  
28e3c 20 20 20 20 73 71 6c 69 74 65 33 52 75 6e 50 61      sqlite3RunPa
28e3d 72 73 65 72 28 70 50 61 72 73 65 2c 20 7a 53 71  rser(pParse, zSq
28e3e 6c 43 6f 70 79 2c 20 26 7a 45 72 72 4d 73 67 29  lCopy, &zErrMsg)
28e3f 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  ;.      sqlite3D
28e40 62 46 72 65 65 28 64 62 2c 20 7a 53 71 6c 43 6f  bFree(db, zSqlCo
28e41 70 79 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73  py);.      pPars
28e42 65 2d 3e 7a 54 61 69 6c 20 3d 20 26 7a 53 71 6c  e->zTail = &zSql
28e43 5b 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c 2d 7a  [pParse->zTail-z
28e44 53 71 6c 43 6f 70 79 5d 3b 0a 20 20 20 20 7d 65  SqlCopy];.    }e
28e45 6c 73 65 7b 0a 20 20 20 20 20 20 70 50 61 72 73  lse{.      pPars
28e46 65 2d 3e 7a 54 61 69 6c 20 3d 20 26 7a 53 71 6c  e->zTail = &zSql
28e47 5b 6e 42 79 74 65 73 5d 3b 0a 20 20 20 20 7d 0a  [nBytes];.    }.
28e48 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
28e49 69 74 65 33 52 75 6e 50 61 72 73 65 72 28 70 50  ite3RunParser(pP
28e4a 61 72 73 65 2c 20 7a 53 71 6c 2c 20 26 7a 45 72  arse, zSql, &zEr
28e4b 72 4d 73 67 29 3b 0a 20 20 7d 0a 0a 20 20 69 66  rMsg);.  }..  if
28e4c 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
28e4d 65 64 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65  ed ){.    pParse
28e4e 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f  ->rc = SQLITE_NO
28e4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  MEM;.  }.  if( p
28e50 50 61 72 73 65 2d 3e 72 63 3d 3d 53 51 4c 49 54  Parse->rc==SQLIT
28e51 45 5f 44 4f 4e 45 20 29 20 70 50 61 72 73 65 2d  E_DONE ) pParse-
28e52 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b  >rc = SQLITE_OK;
28e53 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 63  .  if( pParse->c
28e54 68 65 63 6b 53 63 68 65 6d 61 20 29 7b 0a 20 20  heckSchema ){.  
28e55 20 20 73 63 68 65 6d 61 49 73 56 61 6c 69 64 28    schemaIsValid(
28e56 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a 20 20 69  pParse);.  }.  i
28e57 66 28 20 70 50 61 72 73 65 2d 3e 72 63 3d 3d 53  f( pParse->rc==S
28e58 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 29 7b 0a  QLITE_SCHEMA ){.
28e59 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 65 74      sqlite3Reset
28e5a 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64  InternalSchema(d
28e5b 62 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28  b, 0);.  }.  if(
28e5c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
28e5d 64 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d  d ){.    pParse-
28e5e 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d  >rc = SQLITE_NOM
28e5f 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 7a  EM;.  }.  if( pz
28e60 54 61 69 6c 20 29 7b 0a 20 20 20 20 2a 70 7a 54  Tail ){.    *pzT
28e61 61 69 6c 20 3d 20 70 50 61 72 73 65 2d 3e 7a 54  ail = pParse->zT
28e62 61 69 6c 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20  ail;.  }.  rc = 
28e63 70 50 61 72 73 65 2d 3e 72 63 3b 0a 0a 23 69 66  pParse->rc;..#if
28e64 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
28e65 5f 45 58 50 4c 41 49 4e 0a 20 20 69 66 28 20 72  _EXPLAIN.  if( r
28e66 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
28e67 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20 26 26  pParse->pVdbe &&
28e68 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e   pParse->explain
28e69 20 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63   ){.    static c
28e6a 6f 6e 73 74 20 63 68 61 72 20 2a 20 63 6f 6e 73  onst char * cons
28e6b 74 20 61 7a 43 6f 6c 4e 61 6d 65 5b 5d 20 3d 20  t azColName[] = 
28e6c 7b 0a 20 20 20 20 20 20 20 22 61 64 64 72 22 2c  {.       "addr",
28e6d 20 22 6f 70 63 6f 64 65 22 2c 20 22 70 31 22 2c   "opcode", "p1",
28e6e 20 22 70 32 22 2c 20 22 70 33 22 2c 20 22 70 34   "p2", "p3", "p4
28e6f 22 2c 20 22 70 35 22 2c 20 22 63 6f 6d 6d 65 6e  ", "p5", "commen
28e70 74 22 2c 0a 20 20 20 20 20 20 20 22 6f 72 64 65  t",.       "orde
28e71 72 22 2c 20 22 66 72 6f 6d 22 2c 20 22 64 65 74  r", "from", "det
28e72 61 69 6c 22 0a 20 20 20 20 7d 3b 0a 20 20 20 20  ail".    };.    
28e73 69 6e 74 20 69 46 69 72 73 74 2c 20 6d 78 3b 0a  int iFirst, mx;.
28e74 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e      if( pParse->
28e75 65 78 70 6c 61 69 6e 3d 3d 32 20 29 7b 0a 20 20  explain==2 ){.  
28e76 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
28e77 65 74 4e 75 6d 43 6f 6c 73 28 70 50 61 72 73 65  etNumCols(pParse
28e78 2d 3e 70 56 64 62 65 2c 20 33 29 3b 0a 20 20 20  ->pVdbe, 3);.   
28e79 20 20 20 69 46 69 72 73 74 20 3d 20 38 3b 0a 20     iFirst = 8;. 
28e7a 20 20 20 20 20 6d 78 20 3d 20 31 31 3b 0a 20 20       mx = 11;.  
28e7b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
28e7c 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
28e7d 43 6f 6c 73 28 70 50 61 72 73 65 2d 3e 70 56 64  Cols(pParse->pVd
28e7e 62 65 2c 20 38 29 3b 0a 20 20 20 20 20 20 69 46  be, 8);.      iF
28e7f 69 72 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  irst = 0;.      
28e80 6d 78 20 3d 20 38 3b 0a 20 20 20 20 7d 0a 20 20  mx = 8;.    }.  
28e81 20 20 66 6f 72 28 69 3d 69 46 69 72 73 74 3b 20    for(i=iFirst; 
28e82 69 3c 6d 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  i<mx; i++){.    
28e83 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
28e84 43 6f 6c 4e 61 6d 65 28 70 50 61 72 73 65 2d 3e  ColName(pParse->
28e85 70 56 64 62 65 2c 20 69 2d 69 46 69 72 73 74 2c  pVdbe, i-iFirst,
28e86 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 0a 20   COLNAME_NAME,. 
28e87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28e88 20 20 20 20 20 20 20 20 20 20 20 61 7a 43 6f 6c             azCol
28e89 4e 61 6d 65 5b 69 5d 2c 20 53 51 4c 49 54 45 5f  Name[i], SQLITE_
28e8a 53 54 41 54 49 43 29 3b 0a 20 20 20 20 7d 0a 20  STATIC);.    }. 
28e8b 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28   }.#endif..  if(
28e8c 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
28e8d 66 28 64 62 29 20 29 7b 0a 20 20 20 20 72 63 20  f(db) ){.    rc 
28e8e 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  = SQLITE_MISUSE;
28e8f 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
28e90 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30  db->init.busy==0
28e91 20 7c 7c 20 73 61 76 65 53 71 6c 46 6c 61 67 3d   || saveSqlFlag=
28e92 3d 30 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e  =0 );.  if( db->
28e93 69 6e 69 74 2e 62 75 73 79 3d 3d 30 20 29 7b 0a  init.busy==0 ){.
28e94 20 20 20 20 56 64 62 65 20 2a 70 56 64 62 65 20      Vdbe *pVdbe 
28e95 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  = pParse->pVdbe;
28e96 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
28e97 53 65 74 53 71 6c 28 70 56 64 62 65 2c 20 7a 53  SetSql(pVdbe, zS
28e98 71 6c 2c 20 28 69 6e 74 29 28 70 50 61 72 73 65  ql, (int)(pParse
28e99 2d 3e 7a 54 61 69 6c 2d 7a 53 71 6c 29 2c 20 73  ->zTail-zSql), s
28e9a 61 76 65 53 71 6c 46 6c 61 67 29 3b 0a 20 20 7d  aveSqlFlag);.  }
28e9b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 70  .  if( pParse->p
28e9c 56 64 62 65 20 26 26 20 28 72 63 21 3d 53 51 4c  Vdbe && (rc!=SQL
28e9d 49 54 45 5f 4f 4b 20 7c 7c 20 64 62 2d 3e 6d 61  ITE_OK || db->ma
28e9e 6c 6c 6f 63 46 61 69 6c 65 64 29 20 29 7b 0a 20  llocFailed) ){. 
28e9f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 69     sqlite3VdbeFi
28ea0 6e 61 6c 69 7a 65 28 70 50 61 72 73 65 2d 3e 70  nalize(pParse->p
28ea1 56 64 62 65 29 3b 0a 20 20 20 20 61 73 73 65 72  Vdbe);.    asser
28ea2 74 28 21 28 2a 70 70 53 74 6d 74 29 29 3b 0a 20  t(!(*ppStmt));. 
28ea3 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 53   }else{.    *ppS
28ea4 74 6d 74 20 3d 20 28 73 71 6c 69 74 65 33 5f 73  tmt = (sqlite3_s
28ea5 74 6d 74 2a 29 70 50 61 72 73 65 2d 3e 70 56 64  tmt*)pParse->pVd
28ea6 62 65 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 7a  be;.  }..  if( z
28ea7 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 73 71  ErrMsg ){.    sq
28ea8 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72  lite3Error(db, r
28ea9 63 2c 20 22 25 73 22 2c 20 7a 45 72 72 4d 73 67  c, "%s", zErrMsg
28eaa 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  );.    sqlite3Db
28eab 46 72 65 65 28 64 62 2c 20 7a 45 72 72 4d 73 67  Free(db, zErrMsg
28eac 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
28ead 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
28eae 20 72 63 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20   rc, 0);.  }..  
28eaf 2f 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 54 72  /* Delete any Tr
28eb0 69 67 67 65 72 50 72 67 20 73 74 72 75 63 74 75  iggerPrg structu
28eb1 72 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 77 68  res allocated wh
28eb2 69 6c 65 20 70 61 72 73 69 6e 67 20 74 68 69 73  ile parsing this
28eb3 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20   statement. */. 
28eb4 20 77 68 69 6c 65 28 20 70 50 61 72 73 65 2d 3e   while( pParse->
28eb5 70 54 72 69 67 67 65 72 50 72 67 20 29 7b 0a 20  pTriggerPrg ){. 
28eb6 20 20 20 54 72 69 67 67 65 72 50 72 67 20 2a 70     TriggerPrg *p
28eb7 54 20 3d 20 70 50 61 72 73 65 2d 3e 70 54 72 69  T = pParse->pTri
28eb8 67 67 65 72 50 72 67 3b 0a 20 20 20 20 70 50 61  ggerPrg;.    pPa
28eb9 72 73 65 2d 3e 70 54 72 69 67 67 65 72 50 72 67  rse->pTriggerPrg
28eba 20 3d 20 70 54 2d 3e 70 4e 65 78 74 3b 0a 20 20   = pT->pNext;.  
28ebb 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 6f    sqlite3VdbePro
28ebc 67 72 61 6d 44 65 6c 65 74 65 28 64 62 2c 20 70  gramDelete(db, p
28ebd 54 2d 3e 70 50 72 6f 67 72 61 6d 2c 20 30 29 3b  T->pProgram, 0);
28ebe 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
28ebf 65 65 28 64 62 2c 20 70 54 29 3b 0a 20 20 7d 0a  ee(db, pT);.  }.
28ec0 0a 65 6e 64 5f 70 72 65 70 61 72 65 3a 0a 0a 20  .end_prepare:.. 
28ec1 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65   sqlite3StackFre
28ec2 65 28 64 62 2c 20 70 50 61 72 73 65 29 3b 0a 20  e(db, pParse);. 
28ec3 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69   rc = sqlite3Api
28ec4 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20  Exit(db, rc);.  
28ec5 61 73 73 65 72 74 28 20 28 72 63 26 64 62 2d 3e  assert( (rc&db->
28ec6 65 72 72 4d 61 73 6b 29 3d 3d 72 63 20 29 3b 0a  errMask)==rc );.
28ec7 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 73    return rc;.}.s
28ec8 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65  tatic int sqlite
28ec9 33 4c 6f 63 6b 41 6e 64 50 72 65 70 61 72 65 28  3LockAndPrepare(
28eca 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
28ecb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
28ecc 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e  Database handle.
28ecd 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
28ece 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 20   *zSql,         
28ecf 2f 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64  /* UTF-8 encoded
28ed0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20   SQL statement. 
28ed1 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 2c  */.  int nBytes,
28ed2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
28ed3 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c  * Length of zSql
28ed4 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20   in bytes. */.  
28ed5 69 6e 74 20 73 61 76 65 53 71 6c 46 6c 61 67 2c  int saveSqlFlag,
28ed6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
28ed7 65 20 74 6f 20 63 6f 70 79 20 53 51 4c 20 74 65  e to copy SQL te
28ed8 78 74 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69  xt into the sqli
28ed9 74 65 33 5f 73 74 6d 74 20 2a 2f 0a 20 20 73 71  te3_stmt */.  sq
28eda 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53  lite3_stmt **ppS
28edb 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20  tmt,    /* OUT: 
28edc 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  A pointer to the
28edd 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
28ede 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ent */.  const c
28edf 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20  har **pzTail    
28ee0 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f     /* OUT: End o
28ee1 66 20 70 61 72 73 65 64 20 73 74 72 69 6e 67 20  f parsed string 
28ee2 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  */.){.  int rc;.
28ee3 20 20 61 73 73 65 72 74 28 20 70 70 53 74 6d 74    assert( ppStmt
28ee4 21 3d 30 20 29 3b 0a 20 20 2a 70 70 53 74 6d 74  !=0 );.  *ppStmt
28ee5 20 3d 20 30 3b 0a 20 20 69 66 28 20 21 73 71 6c   = 0;.  if( !sql
28ee6 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 4f  ite3SafetyCheckO
28ee7 6b 28 64 62 29 20 29 7b 0a 20 20 20 20 72 65 74  k(db) ){.    ret
28ee8 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  urn SQLITE_MISUS
28ee9 45 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  E;.  }.  sqlite3
28eea 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d  _mutex_enter(db-
28eeb 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74  >mutex);.  sqlit
28eec 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28  e3BtreeEnterAll(
28eed 64 62 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  db);.  rc = sqli
28eee 74 65 33 50 72 65 70 61 72 65 28 64 62 2c 20 7a  te3Prepare(db, z
28eef 53 71 6c 2c 20 6e 42 79 74 65 73 2c 20 73 61 76  Sql, nBytes, sav
28ef0 65 53 71 6c 46 6c 61 67 2c 20 70 70 53 74 6d 74  eSqlFlag, ppStmt
28ef1 2c 20 70 7a 54 61 69 6c 29 3b 0a 20 20 69 66 28  , pzTail);.  if(
28ef2 20 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48 45   rc==SQLITE_SCHE
28ef3 4d 41 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  MA ){.    sqlite
28ef4 33 5f 66 69 6e 61 6c 69 7a 65 28 2a 70 70 53 74  3_finalize(*ppSt
28ef5 6d 74 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  mt);.    rc = sq
28ef6 6c 69 74 65 33 50 72 65 70 61 72 65 28 64 62 2c  lite3Prepare(db,
28ef7 20 7a 53 71 6c 2c 20 6e 42 79 74 65 73 2c 20 73   zSql, nBytes, s
28ef8 61 76 65 53 71 6c 46 6c 61 67 2c 20 70 70 53 74  aveSqlFlag, ppSt
28ef9 6d 74 2c 20 70 7a 54 61 69 6c 29 3b 0a 20 20 7d  mt, pzTail);.  }
28efa 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
28efb 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 73  eaveAll(db);.  s
28efc 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
28efd 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
28efe 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
28eff 2a 0a 2a 2a 20 52 65 72 75 6e 20 74 68 65 20 63  *.** Rerun the c
28f00 6f 6d 70 69 6c 61 74 69 6f 6e 20 6f 66 20 61 20  ompilation of a 
28f01 73 74 61 74 65 6d 65 6e 74 20 61 66 74 65 72 20  statement after 
28f02 61 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 2e  a schema change.
28f03 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 74  .**.** If the st
28f04 61 74 65 6d 65 6e 74 20 69 73 20 73 75 63 63 65  atement is succe
28f05 73 73 66 75 6c 6c 79 20 72 65 63 6f 6d 70 69 6c  ssfully recompil
28f06 65 64 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ed, return SQLIT
28f07 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65 2c  E_OK. Otherwise,
28f08 0a 2a 2a 20 69 66 20 74 68 65 20 73 74 61 74 65  .** if the state
28f09 6d 65 6e 74 20 63 61 6e 6e 6f 74 20 62 65 20 72  ment cannot be r
28f0a 65 63 6f 6d 70 69 6c 65 64 20 62 65 63 61 75 73  ecompiled becaus
28f0b 65 20 61 6e 6f 74 68 65 72 20 63 6f 6e 6e 65 63  e another connec
28f0c 74 69 6f 6e 20 68 61 73 0a 2a 2a 20 6c 6f 63 6b  tion has.** lock
28f0d 65 64 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6d  ed the sqlite3_m
28f0e 61 73 74 65 72 20 74 61 62 6c 65 2c 20 72 65 74  aster table, ret
28f0f 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  urn SQLITE_LOCKE
28f10 44 2e 20 49 66 20 61 6e 79 20 6f 74 68 65 72 20  D. If any other 
28f11 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 73 2c  error.** occurs,
28f12 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 53   return SQLITE_S
28f13 43 48 45 4d 41 2e 0a 2a 2f 0a 53 51 4c 49 54 45  CHEMA..*/.SQLITE
28f14 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
28f15 69 74 65 33 52 65 70 72 65 70 61 72 65 28 56 64  ite3Reprepare(Vd
28f16 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63  be *p){.  int rc
28f17 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  ;.  sqlite3_stmt
28f18 20 2a 70 4e 65 77 3b 0a 20 20 63 6f 6e 73 74 20   *pNew;.  const 
28f19 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 73 71  char *zSql;.  sq
28f1a 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 61 73  lite3 *db;..  as
28f1b 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
28f1c 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 33  tex_held(sqlite3
28f1d 56 64 62 65 44 62 28 70 29 2d 3e 6d 75 74 65 78  VdbeDb(p)->mutex
28f1e 29 20 29 3b 0a 20 20 7a 53 71 6c 20 3d 20 73 71  ) );.  zSql = sq
28f1f 6c 69 74 65 33 5f 73 71 6c 28 28 73 71 6c 69 74  lite3_sql((sqlit
28f20 65 33 5f 73 74 6d 74 20 2a 29 70 29 3b 0a 20 20  e3_stmt *)p);.  
28f21 61 73 73 65 72 74 28 20 7a 53 71 6c 21 3d 30 20  assert( zSql!=0 
28f22 29 3b 20 20 2f 2a 20 52 65 70 72 65 70 61 72 65  );  /* Reprepare
28f23 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 6f 72   only called for
28f24 20 70 72 65 70 61 72 65 5f 76 32 28 29 20 73 74   prepare_v2() st
28f25 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 64 62  atements */.  db
28f26 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 44 62   = sqlite3VdbeDb
28f27 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  (p);.  assert( s
28f28 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
28f29 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  d(db->mutex) );.
28f2a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4c 6f    rc = sqlite3Lo
28f2b 63 6b 41 6e 64 50 72 65 70 61 72 65 28 64 62 2c  ckAndPrepare(db,
28f2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 30 2c 20 26 70   zSql, -1, 0, &p
28f2d 4e 65 77 2c 20 30 29 3b 0a 20 20 69 66 28 20 72  New, 0);.  if( r
28f2e 63 20 29 7b 0a 20 20 20 20 69 66 28 20 72 63 3d  c ){.    if( rc=
28f2f 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b  =SQLITE_NOMEM ){
28f30 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  .      db->mallo
28f31 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
28f32 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70   }.    assert( p
28f33 4e 65 77 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65  New==0 );.    re
28f34 74 75 72 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45  turn (rc==SQLITE
28f35 5f 4c 4f 43 4b 45 44 29 20 3f 20 53 51 4c 49 54  _LOCKED) ? SQLIT
28f36 45 5f 4c 4f 43 4b 45 44 20 3a 20 53 51 4c 49 54  E_LOCKED : SQLIT
28f37 45 5f 53 43 48 45 4d 41 3b 0a 20 20 7d 65 6c 73  E_SCHEMA;.  }els
28f38 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  e{.    assert( p
28f39 4e 65 77 21 3d 30 20 29 3b 0a 20 20 7d 0a 20 20  New!=0 );.  }.  
28f3a 73 71 6c 69 74 65 33 56 64 62 65 53 77 61 70 28  sqlite3VdbeSwap(
28f3b 28 56 64 62 65 2a 29 70 4e 65 77 2c 20 70 29 3b  (Vdbe*)pNew, p);
28f3c 0a 20 20 73 71 6c 69 74 65 33 54 72 61 6e 73 66  .  sqlite3Transf
28f3d 65 72 42 69 6e 64 69 6e 67 73 28 70 4e 65 77 2c  erBindings(pNew,
28f3e 20 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29   (sqlite3_stmt*)
28f3f 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  p);.  sqlite3Vdb
28f40 65 52 65 73 65 74 53 74 65 70 52 65 73 75 6c 74  eResetStepResult
28f41 28 28 56 64 62 65 2a 29 70 4e 65 77 29 3b 0a 20  ((Vdbe*)pNew);. 
28f42 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61   sqlite3VdbeFina
28f43 6c 69 7a 65 28 28 56 64 62 65 2a 29 70 4e 65 77  lize((Vdbe*)pNew
28f44 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
28f45 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  TE_OK;.}.../*.**
28f46 20 54 77 6f 20 76 65 72 73 69 6f 6e 73 20 6f 66   Two versions of
28f47 20 74 68 65 20 6f 66 66 69 63 69 61 6c 20 41 50   the official AP
28f48 49 2e 20 20 4c 65 67 61 63 79 20 61 6e 64 20 6e  I.  Legacy and n
28f49 65 77 20 75 73 65 2e 20 20 49 6e 20 74 68 65 20  ew use.  In the 
28f4a 6c 65 67 61 63 79 0a 2a 2a 20 76 65 72 73 69 6f  legacy.** versio
28f4b 6e 2c 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  n, the original 
28f4c 53 51 4c 20 74 65 78 74 20 69 73 20 6e 6f 74 20  SQL text is not 
28f4d 73 61 76 65 64 20 69 6e 20 74 68 65 20 70 72 65  saved in the pre
28f4e 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a  pared statement.
28f4f 2a 2a 20 61 6e 64 20 73 6f 20 69 66 20 61 20 73  ** and so if a s
28f50 63 68 65 6d 61 20 63 68 61 6e 67 65 20 6f 63 63  chema change occ
28f51 75 72 73 2c 20 53 51 4c 49 54 45 5f 53 43 48 45  urs, SQLITE_SCHE
28f52 4d 41 20 69 73 20 72 65 74 75 72 6e 65 64 20 62  MA is returned b
28f53 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 74 65  y.** sqlite3_ste
28f54 70 28 29 2e 20 20 49 6e 20 74 68 65 20 6e 65 77  p().  In the new
28f55 20 76 65 72 73 69 6f 6e 2c 20 74 68 65 20 6f 72   version, the or
28f56 69 67 69 6e 61 6c 20 53 51 4c 20 74 65 78 74 20  iginal SQL text 
28f57 69 73 20 72 65 74 61 69 6e 65 64 0a 2a 2a 20 61  is retained.** a
28f58 6e 64 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  nd the statement
28f59 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c   is automaticall
28f5a 79 20 72 65 63 6f 6d 70 69 6c 65 64 20 69 66 20  y recompiled if 
28f5b 61 6e 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65  an schema change
28f5c 0a 2a 2a 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53  .** occurs..*/.S
28f5d 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
28f5e 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 0a 20  lite3_prepare(. 
28f5f 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
28f60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
28f61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a  tabase handle. *
28f62 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
28f63 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a  zSql,         /*
28f64 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 53   UTF-8 encoded S
28f65 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f  QL statement. */
28f66 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20  .  int nBytes,  
28f67 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
28f68 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69  Length of zSql i
28f69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71  n bytes. */.  sq
28f6a 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53  lite3_stmt **ppS
28f6b 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20  tmt,    /* OUT: 
28f6c 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  A pointer to the
28f6d 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
28f6e 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ent */.  const c
28f6f 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20  har **pzTail    
28f70 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f     /* OUT: End o
28f71 66 20 70 61 72 73 65 64 20 73 74 72 69 6e 67 20  f parsed string 
28f72 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  */.){.  int rc;.
28f73 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4c 6f    rc = sqlite3Lo
28f74 63 6b 41 6e 64 50 72 65 70 61 72 65 28 64 62 2c  ckAndPrepare(db,
28f75 7a 53 71 6c 2c 6e 42 79 74 65 73 2c 30 2c 70 70  zSql,nBytes,0,pp
28f76 53 74 6d 74 2c 70 7a 54 61 69 6c 29 3b 0a 20 20  Stmt,pzTail);.  
28f77 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
28f78 54 45 5f 4f 4b 20 7c 7c 20 70 70 53 74 6d 74 3d  TE_OK || ppStmt=
28f79 3d 30 20 7c 7c 20 2a 70 70 53 74 6d 74 3d 3d 30  =0 || *ppStmt==0
28f7a 20 29 3b 20 20 2f 2a 20 56 45 52 49 46 59 3a 20   );  /* VERIFY: 
28f7b 46 31 33 30 32 31 20 2a 2f 0a 20 20 72 65 74 75  F13021 */.  retu
28f7c 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f  rn rc;.}.SQLITE_
28f7d 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
28f7e 70 72 65 70 61 72 65 5f 76 32 28 0a 20 20 73 71  prepare_v2(.  sq
28f7f 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
28f80 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
28f81 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 0a 20  ase handle. */. 
28f82 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71   const char *zSq
28f83 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 55 54  l,         /* UT
28f84 46 2d 38 20 65 6e 63 6f 64 65 64 20 53 51 4c 20  F-8 encoded SQL 
28f85 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20  statement. */.  
28f86 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20 20 20  int nBytes,     
28f87 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e            /* Len
28f88 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62  gth of zSql in b
28f89 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74  ytes. */.  sqlit
28f8a 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74  e3_stmt **ppStmt
28f8b 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41 20 70  ,    /* OUT: A p
28f8c 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 72  ointer to the pr
28f8d 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
28f8e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
28f8f 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 20 20   **pzTail       
28f90 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f 66 20 70  /* OUT: End of p
28f91 61 72 73 65 64 20 73 74 72 69 6e 67 20 2a 2f 0a  arsed string */.
28f92 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 72  ){.  int rc;.  r
28f93 63 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 6b 41  c = sqlite3LockA
28f94 6e 64 50 72 65 70 61 72 65 28 64 62 2c 7a 53 71  ndPrepare(db,zSq
28f95 6c 2c 6e 42 79 74 65 73 2c 31 2c 70 70 53 74 6d  l,nBytes,1,ppStm
28f96 74 2c 70 7a 54 61 69 6c 29 3b 0a 20 20 61 73 73  t,pzTail);.  ass
28f97 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
28f98 4f 4b 20 7c 7c 20 70 70 53 74 6d 74 3d 3d 30 20  OK || ppStmt==0 
28f99 7c 7c 20 2a 70 70 53 74 6d 74 3d 3d 30 20 29 3b  || *ppStmt==0 );
28f9a 20 20 2f 2a 20 56 45 52 49 46 59 3a 20 46 31 33    /* VERIFY: F13
28f9b 30 32 31 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20  021 */.  return 
28f9c 72 63 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20  rc;.}...#ifndef 
28f9d 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
28f9e 36 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 69 6c 65 20  6./*.** Compile 
28f9f 74 68 65 20 55 54 46 2d 31 36 20 65 6e 63 6f 64  the UTF-16 encod
28fa0 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ed SQL statement
28fa1 20 7a 53 71 6c 20 69 6e 74 6f 20 61 20 73 74 61   zSql into a sta
28fa2 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 2e 0a 2a  tement handle..*
28fa3 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c  /.static int sql
28fa4 69 74 65 33 50 72 65 70 61 72 65 31 36 28 0a 20  ite3Prepare16(. 
28fa5 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
28fa6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
28fa7 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a  tabase handle. *
28fa8 2f 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  / .  const void 
28fa9 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 20 2f  *zSql,         /
28faa 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20  * UTF-8 encoded 
28fab 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a  SQL statement. *
28fac 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 2c 20  /.  int nBytes, 
28fad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
28fae 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20   Length of zSql 
28faf 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 69  in bytes. */.  i
28fb0 6e 74 20 73 61 76 65 53 71 6c 46 6c 61 67 2c 20  nt saveSqlFlag, 
28fb1 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
28fb2 20 74 6f 20 73 61 76 65 20 53 51 4c 20 74 65 78   to save SQL tex
28fb3 74 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74  t into the sqlit
28fb4 65 33 5f 73 74 6d 74 20 2a 2f 0a 20 20 73 71 6c  e3_stmt */.  sql
28fb5 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74  ite3_stmt **ppSt
28fb6 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41  mt,    /* OUT: A
28fb7 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
28fb8 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
28fb9 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f  nt */.  const vo
28fba 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20  id **pzTail     
28fbb 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f 66    /* OUT: End of
28fbc 20 70 61 72 73 65 64 20 73 74 72 69 6e 67 20 2a   parsed string *
28fbd 2f 0a 29 7b 0a 20 20 2f 2a 20 54 68 69 73 20 66  /.){.  /* This f
28fbe 75 6e 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c  unction currentl
28fbf 79 20 77 6f 72 6b 73 20 62 79 20 66 69 72 73 74  y works by first
28fc0 20 74 72 61 6e 73 66 6f 72 6d 69 6e 67 20 74 68   transforming th
28fc1 65 20 55 54 46 2d 31 36 0a 20 20 2a 2a 20 65 6e  e UTF-16.  ** en
28fc2 63 6f 64 65 64 20 73 74 72 69 6e 67 20 74 6f 20  coded string to 
28fc3 55 54 46 2d 38 2c 20 74 68 65 6e 20 69 6e 76 6f  UTF-8, then invo
28fc4 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f 70 72 65  king sqlite3_pre
28fc5 70 61 72 65 28 29 2e 20 54 68 65 0a 20 20 2a 2a  pare(). The.  **
28fc6 20 74 72 69 63 6b 79 20 62 69 74 20 69 73 20 66   tricky bit is f
28fc7 69 67 75 72 69 6e 67 20 6f 75 74 20 74 68 65 20  iguring out the 
28fc8 70 6f 69 6e 74 65 72 20 74 6f 20 72 65 74 75 72  pointer to retur
28fc9 6e 20 69 6e 20 2a 70 7a 54 61 69 6c 2e 0a 20 20  n in *pzTail..  
28fca 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 38  */.  char *zSql8
28fcb 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
28fcc 7a 54 61 69 6c 38 20 3d 20 30 3b 0a 20 20 69 6e  zTail8 = 0;.  in
28fcd 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
28fce 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 70 53  ;..  assert( ppS
28fcf 74 6d 74 20 29 3b 0a 20 20 2a 70 70 53 74 6d 74  tmt );.  *ppStmt
28fd0 20 3d 20 30 3b 0a 20 20 69 66 28 20 21 73 71 6c   = 0;.  if( !sql
28fd1 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 4f  ite3SafetyCheckO
28fd2 6b 28 64 62 29 20 29 7b 0a 20 20 20 20 72 65 74  k(db) ){.    ret
28fd3 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  urn SQLITE_MISUS
28fd4 45 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  E;.  }.  sqlite3
28fd5 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d  _mutex_enter(db-
28fd6 3e 6d 75 74 65 78 29 3b 0a 20 20 7a 53 71 6c 38  >mutex);.  zSql8
28fd7 20 3d 20 73 71 6c 69 74 65 33 55 74 66 31 36 74   = sqlite3Utf16t
28fd8 6f 38 28 64 62 2c 20 7a 53 71 6c 2c 20 6e 42 79  o8(db, zSql, nBy
28fd9 74 65 73 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c  tes);.  if( zSql
28fda 38 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  8 ){.    rc = sq
28fdb 6c 69 74 65 33 4c 6f 63 6b 41 6e 64 50 72 65 70  lite3LockAndPrep
28fdc 61 72 65 28 64 62 2c 20 7a 53 71 6c 38 2c 20 2d  are(db, zSql8, -
28fdd 31 2c 20 73 61 76 65 53 71 6c 46 6c 61 67 2c 20  1, saveSqlFlag, 
28fde 70 70 53 74 6d 74 2c 20 26 7a 54 61 69 6c 38 29  ppStmt, &zTail8)
28fdf 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 7a 54 61  ;.  }..  if( zTa
28fe0 69 6c 38 20 26 26 20 70 7a 54 61 69 6c 20 29 7b  il8 && pzTail ){
28fe1 0a 20 20 20 20 2f 2a 20 49 66 20 73 71 6c 69 74  .    /* If sqlit
28fe2 65 33 5f 70 72 65 70 61 72 65 20 72 65 74 75 72  e3_prepare retur
28fe3 6e 73 20 61 20 74 61 69 6c 20 70 6f 69 6e 74 65  ns a tail pointe
28fe4 72 2c 20 77 65 20 63 61 6c 63 75 6c 61 74 65 20  r, we calculate 
28fe5 74 68 65 0a 20 20 20 20 2a 2a 20 65 71 75 69 76  the.    ** equiv
28fe6 61 6c 65 6e 74 20 70 6f 69 6e 74 65 72 20 69 6e  alent pointer in
28fe7 74 6f 20 74 68 65 20 55 54 46 2d 31 36 20 73 74  to the UTF-16 st
28fe8 72 69 6e 67 20 62 79 20 63 6f 75 6e 74 69 6e 67  ring by counting
28fe9 20 74 68 65 20 75 6e 69 63 6f 64 65 0a 20 20 20   the unicode.   
28fea 20 2a 2a 20 63 68 61 72 61 63 74 65 72 73 20 62   ** characters b
28feb 65 74 77 65 65 6e 20 7a 53 71 6c 38 20 61 6e 64  etween zSql8 and
28fec 20 7a 54 61 69 6c 38 2c 20 61 6e 64 20 74 68 65   zTail8, and the
28fed 6e 20 72 65 74 75 72 6e 69 6e 67 20 61 20 70 6f  n returning a po
28fee 69 6e 74 65 72 0a 20 20 20 20 2a 2a 20 74 68 65  inter.    ** the
28fef 20 73 61 6d 65 20 6e 75 6d 62 65 72 20 6f 66 20   same number of 
28ff0 63 68 61 72 61 63 74 65 72 73 20 69 6e 74 6f 20  characters into 
28ff1 74 68 65 20 55 54 46 2d 31 36 20 73 74 72 69 6e  the UTF-16 strin
28ff2 67 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e  g..    */.    in
28ff3 74 20 63 68 61 72 73 5f 70 61 72 73 65 64 20 3d  t chars_parsed =
28ff4 20 73 71 6c 69 74 65 33 55 74 66 38 43 68 61 72   sqlite3Utf8Char
28ff5 4c 65 6e 28 7a 53 71 6c 38 2c 20 28 69 6e 74 29  Len(zSql8, (int)
28ff6 28 7a 54 61 69 6c 38 2d 7a 53 71 6c 38 29 29 3b  (zTail8-zSql8));
28ff7 0a 20 20 20 20 2a 70 7a 54 61 69 6c 20 3d 20 28  .    *pzTail = (
28ff8 75 38 20 2a 29 7a 53 71 6c 20 2b 20 73 71 6c 69  u8 *)zSql + sqli
28ff9 74 65 33 55 74 66 31 36 42 79 74 65 4c 65 6e 28  te3Utf16ByteLen(
28ffa 7a 53 71 6c 2c 20 63 68 61 72 73 5f 70 61 72 73  zSql, chars_pars
28ffb 65 64 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  ed);.  }.  sqlit
28ffc 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53 71  e3DbFree(db, zSq
28ffd 6c 38 29 3b 20 0a 20 20 72 63 20 3d 20 73 71 6c  l8); .  rc = sql
28ffe 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20  ite3ApiExit(db, 
28fff 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  rc);.  sqlite3_m
29000 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
29001 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
29002 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 77 6f  rc;.}../*.** Two
29003 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65   versions of the
29004 20 6f 66 66 69 63 69 61 6c 20 41 50 49 2e 20 20   official API.  
29005 4c 65 67 61 63 79 20 61 6e 64 20 6e 65 77 20 75  Legacy and new u
29006 73 65 2e 20 20 49 6e 20 74 68 65 20 6c 65 67 61  se.  In the lega
29007 63 79 0a 2a 2a 20 76 65 72 73 69 6f 6e 2c 20 74  cy.** version, t
29008 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20  he original SQL 
29009 74 65 78 74 20 69 73 20 6e 6f 74 20 73 61 76 65  text is not save
2900a 64 20 69 6e 20 74 68 65 20 70 72 65 70 61 72 65  d in the prepare
2900b 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 61  d statement.** a
2900c 6e 64 20 73 6f 20 69 66 20 61 20 73 63 68 65 6d  nd so if a schem
2900d 61 20 63 68 61 6e 67 65 20 6f 63 63 75 72 73 2c  a change occurs,
2900e 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 69   SQLITE_SCHEMA i
2900f 73 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a  s returned by.**
29010 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e   sqlite3_step().
29011 20 20 49 6e 20 74 68 65 20 6e 65 77 20 76 65 72    In the new ver
29012 73 69 6f 6e 2c 20 74 68 65 20 6f 72 69 67 69 6e  sion, the origin
29013 61 6c 20 53 51 4c 20 74 65 78 74 20 69 73 20 72  al SQL text is r
29014 65 74 61 69 6e 65 64 0a 2a 2a 20 61 6e 64 20 74  etained.** and t
29015 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  he statement is 
29016 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 65  automatically re
29017 63 6f 6d 70 69 6c 65 64 20 69 66 20 61 6e 20 73  compiled if an s
29018 63 68 65 6d 61 20 63 68 61 6e 67 65 0a 2a 2a 20  chema change.** 
29019 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54  occurs..*/.SQLIT
2901a 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
2901b 33 5f 70 72 65 70 61 72 65 31 36 28 0a 20 20 73  3_prepare16(.  s
2901c 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
2901d 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
2901e 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 20  base handle. */ 
2901f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  .  const void *z
29020 53 71 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  Sql,         /* 
29021 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 53 51  UTF-8 encoded SQ
29022 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a  L statement. */.
29023 20 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20    int nBytes,   
29024 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
29025 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e  ength of zSql in
29026 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c   bytes. */.  sql
29027 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74  ite3_stmt **ppSt
29028 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41  mt,    /* OUT: A
29029 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
2902a 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
2902b 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f  nt */.  const vo
2902c 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20  id **pzTail     
2902d 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f 66    /* OUT: End of
2902e 20 70 61 72 73 65 64 20 73 74 72 69 6e 67 20 2a   parsed string *
2902f 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  /.){.  int rc;. 
29030 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 72 65   rc = sqlite3Pre
29031 70 61 72 65 31 36 28 64 62 2c 7a 53 71 6c 2c 6e  pare16(db,zSql,n
29032 42 79 74 65 73 2c 30 2c 70 70 53 74 6d 74 2c 70  Bytes,0,ppStmt,p
29033 7a 54 61 69 6c 29 3b 0a 20 20 61 73 73 65 72 74  zTail);.  assert
29034 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
29035 7c 7c 20 70 70 53 74 6d 74 3d 3d 30 20 7c 7c 20  || ppStmt==0 || 
29036 2a 70 70 53 74 6d 74 3d 3d 30 20 29 3b 20 20 2f  *ppStmt==0 );  /
29037 2a 20 56 45 52 49 46 59 3a 20 46 31 33 30 32 31  * VERIFY: F13021
29038 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   */.  return rc;
29039 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  .}.SQLITE_API in
2903a 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  t sqlite3_prepar
2903b 65 31 36 5f 76 32 28 0a 20 20 73 71 6c 69 74 65  e16_v2(.  sqlite
2903c 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
2903d 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
2903e 68 61 6e 64 6c 65 2e 20 2a 2f 20 0a 20 20 63 6f  handle. */ .  co
2903f 6e 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c 2c 20  nst void *zSql, 
29040 20 20 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38          /* UTF-8
29041 20 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61   encoded SQL sta
29042 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74  tement. */.  int
29043 20 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20 20   nBytes,        
29044 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68         /* Length
29045 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65   of zSql in byte
29046 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  s. */.  sqlite3_
29047 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20  stmt **ppStmt,  
29048 20 20 2f 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e    /* OUT: A poin
29049 74 65 72 20 74 6f 20 74 68 65 20 70 72 65 70 61  ter to the prepa
2904a 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  red statement */
2904b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2a  .  const void **
2904c 70 7a 54 61 69 6c 20 20 20 20 20 20 20 2f 2a 20  pzTail       /* 
2904d 4f 55 54 3a 20 45 6e 64 20 6f 66 20 70 61 72 73  OUT: End of pars
2904e 65 64 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a  ed string */.){.
2904f 20 20 69 6e 74 20 72 63 3b 0a 20 20 72 63 20 3d    int rc;.  rc =
29050 20 73 71 6c 69 74 65 33 50 72 65 70 61 72 65 31   sqlite3Prepare1
29051 36 28 64 62 2c 7a 53 71 6c 2c 6e 42 79 74 65 73  6(db,zSql,nBytes
29052 2c 31 2c 70 70 53 74 6d 74 2c 70 7a 54 61 69 6c  ,1,ppStmt,pzTail
29053 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d  );.  assert( rc=
29054 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 70  =SQLITE_OK || pp
29055 53 74 6d 74 3d 3d 30 20 7c 7c 20 2a 70 70 53 74  Stmt==0 || *ppSt
29056 6d 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 56 45 52  mt==0 );  /* VER
29057 49 46 59 3a 20 46 31 33 30 32 31 20 2a 2f 0a 20  IFY: F13021 */. 
29058 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23   return rc;.}..#
29059 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
2905a 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f  OMIT_UTF16 */../
2905b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
2905c 6e 64 20 6f 66 20 70 72 65 70 61 72 65 2e 63 20  nd of prepare.c 
2905d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2905e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2905f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
29060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
29061 65 67 69 6e 20 66 69 6c 65 20 73 65 6c 65 63 74  egin file select
29062 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
29063 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29064 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
29065 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d  *.** 2001 Septem
29066 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65  ber 15.**.** The
29067 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
29068 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
29069 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
2906a 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
2906b 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
2906c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
2906d 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
2906e 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
2906f 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
29070 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
29071 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
29072 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
29073 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
29074 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
29075 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
29076 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
29077 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
29078 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29079 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2907a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2907b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2907c 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
2907d 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f  le contains C co
2907e 64 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74  de routines that
2907f 20 61 72 65 20 63 61 6c 6c 65 64 20 62 79 20 74   are called by t
29080 68 65 20 70 61 72 73 65 72 0a 2a 2a 20 74 6f 20  he parser.** to 
29081 68 61 6e 64 6c 65 20 53 45 4c 45 43 54 20 73 74  handle SELECT st
29082 61 74 65 6d 65 6e 74 73 20 69 6e 20 53 51 4c 69  atements in SQLi
29083 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 73  te..**.** $Id: s
29084 65 6c 65 63 74 2e 63 2c 76 20 31 2e 35 32 36 20  elect.c,v 1.526 
29085 32 30 30 39 2f 30 38 2f 30 31 20 31 35 3a 30 39  2009/08/01 15:09
29086 3a 35 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :58 drh Exp $.*/
29087 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20  .../*.** Delete 
29088 61 6c 6c 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  all the content 
29089 6f 66 20 61 20 53 65 6c 65 63 74 20 73 74 72 75  of a Select stru
2908a 63 74 75 72 65 20 62 75 74 20 64 6f 20 6e 6f 74  cture but do not
2908b 20 64 65 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 74   deallocate.** t
2908c 68 65 20 73 65 6c 65 63 74 20 73 74 72 75 63 74  he select struct
2908d 75 72 65 20 69 74 73 65 6c 66 2e 0a 2a 2f 0a 73  ure itself..*/.s
2908e 74 61 74 69 63 20 76 6f 69 64 20 63 6c 65 61 72  tatic void clear
2908f 53 65 6c 65 63 74 28 73 71 6c 69 74 65 33 20 2a  Select(sqlite3 *
29090 64 62 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a  db, Select *p){.
29091 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
29092 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70  tDelete(db, p->p
29093 45 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65  EList);.  sqlite
29094 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64  3SrcListDelete(d
29095 62 2c 20 70 2d 3e 70 53 72 63 29 3b 0a 20 20 73  b, p->pSrc);.  s
29096 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
29097 28 64 62 2c 20 70 2d 3e 70 57 68 65 72 65 29 3b  (db, p->pWhere);
29098 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69  .  sqlite3ExprLi
29099 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e  stDelete(db, p->
2909a 70 47 72 6f 75 70 42 79 29 3b 0a 20 20 73 71 6c  pGroupBy);.  sql
2909b 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
2909c 62 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29 3b 0a  b, p->pHaving);.
2909d 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
2909e 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70  tDelete(db, p->p
2909f 4f 72 64 65 72 42 79 29 3b 0a 20 20 73 71 6c 69  OrderBy);.  sqli
290a0 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
290a1 64 62 2c 20 70 2d 3e 70 50 72 69 6f 72 29 3b 0a  db, p->pPrior);.
290a2 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
290a3 65 74 65 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69  ete(db, p->pLimi
290a4 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  t);.  sqlite3Exp
290a5 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70  rDelete(db, p->p
290a6 4f 66 66 73 65 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  Offset);.}../*.*
290a7 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 20 53  * Initialize a S
290a8 65 6c 65 63 74 44 65 73 74 20 73 74 72 75 63 74  electDest struct
290a9 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ure..*/.SQLITE_P
290aa 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
290ab 74 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69  te3SelectDestIni
290ac 74 28 53 65 6c 65 63 74 44 65 73 74 20 2a 70 44  t(SelectDest *pD
290ad 65 73 74 2c 20 69 6e 74 20 65 44 65 73 74 2c 20  est, int eDest, 
290ae 69 6e 74 20 69 50 61 72 6d 29 7b 0a 20 20 70 44  int iParm){.  pD
290af 65 73 74 2d 3e 65 44 65 73 74 20 3d 20 28 75 38  est->eDest = (u8
290b0 29 65 44 65 73 74 3b 0a 20 20 70 44 65 73 74 2d  )eDest;.  pDest-
290b1 3e 69 50 61 72 6d 20 3d 20 69 50 61 72 6d 3b 0a  >iParm = iParm;.
290b2 20 20 70 44 65 73 74 2d 3e 61 66 66 69 6e 69 74    pDest->affinit
290b3 79 20 3d 20 30 3b 0a 20 20 70 44 65 73 74 2d 3e  y = 0;.  pDest->
290b4 69 4d 65 6d 20 3d 20 30 3b 0a 20 20 70 44 65 73  iMem = 0;.  pDes
290b5 74 2d 3e 6e 4d 65 6d 20 3d 20 30 3b 0a 7d 0a 0a  t->nMem = 0;.}..
290b6 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20  ./*.** Allocate 
290b7 61 20 6e 65 77 20 53 65 6c 65 63 74 20 73 74 72  a new Select str
290b8 75 63 74 75 72 65 20 61 6e 64 20 72 65 74 75 72  ucture and retur
290b9 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
290ba 68 61 74 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  hat.** structure
290bb 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
290bc 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69  ATE Select *sqli
290bd 74 65 33 53 65 6c 65 63 74 4e 65 77 28 0a 20 20  te3SelectNew(.  
290be 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
290bf 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67        /* Parsing
290c0 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78   context */.  Ex
290c1 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20  prList *pEList, 
290c2 20 20 20 20 2f 2a 20 77 68 69 63 68 20 63 6f 6c      /* which col
290c3 75 6d 6e 73 20 74 6f 20 69 6e 63 6c 75 64 65 20  umns to include 
290c4 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 2a 2f  in the result */
290c5 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63  .  SrcList *pSrc
290c6 2c 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20  ,        /* the 
290c7 46 52 4f 4d 20 63 6c 61 75 73 65 20 2d 2d 20 77  FROM clause -- w
290c8 68 69 63 68 20 74 61 62 6c 65 73 20 74 6f 20 73  hich tables to s
290c9 63 61 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  can */.  Expr *p
290ca 57 68 65 72 65 2c 20 20 20 20 20 20 20 20 20 2f  Where,         /
290cb 2a 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  * the WHERE clau
290cc 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  se */.  ExprList
290cd 20 2a 70 47 72 6f 75 70 42 79 2c 20 20 20 2f 2a   *pGroupBy,   /*
290ce 20 74 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c   the GROUP BY cl
290cf 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a  ause */.  Expr *
290d0 70 48 61 76 69 6e 67 2c 20 20 20 20 20 20 20 20  pHaving,        
290d1 2f 2a 20 74 68 65 20 48 41 56 49 4e 47 20 63 6c  /* the HAVING cl
290d2 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69  ause */.  ExprLi
290d3 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20  st *pOrderBy,   
290d4 2f 2a 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  /* the ORDER BY 
290d5 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20  clause */.  int 
290d6 69 73 44 69 73 74 69 6e 63 74 2c 20 20 20 20 20  isDistinct,     
290d7 20 20 2f 2a 20 74 72 75 65 20 69 66 20 74 68 65    /* true if the
290d8 20 44 49 53 54 49 4e 43 54 20 6b 65 79 77 6f 72   DISTINCT keywor
290d9 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a  d is present */.
290da 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 2c 20    Expr *pLimit, 
290db 20 20 20 20 20 20 20 20 2f 2a 20 4c 49 4d 49 54          /* LIMIT
290dc 20 76 61 6c 75 65 2e 20 20 4e 55 4c 4c 20 6d 65   value.  NULL me
290dd 61 6e 73 20 6e 6f 74 20 75 73 65 64 20 2a 2f 0a  ans not used */.
290de 20 20 45 78 70 72 20 2a 70 4f 66 66 73 65 74 20    Expr *pOffset 
290df 20 20 20 20 20 20 20 20 2f 2a 20 4f 46 46 53 45          /* OFFSE
290e0 54 20 76 61 6c 75 65 2e 20 20 4e 55 4c 4c 20 6d  T value.  NULL m
290e1 65 61 6e 73 20 6e 6f 20 6f 66 66 73 65 74 20 2a  eans no offset *
290e2 2f 0a 29 7b 0a 20 20 53 65 6c 65 63 74 20 2a 70  /.){.  Select *p
290e3 4e 65 77 3b 0a 20 20 53 65 6c 65 63 74 20 73 74  New;.  Select st
290e4 61 6e 64 69 6e 3b 0a 20 20 73 71 6c 69 74 65 33  andin;.  sqlite3
290e5 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
290e6 62 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69  b;.  pNew = sqli
290e7 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
290e8 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77  db, sizeof(*pNew
290e9 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64  ) );.  assert( d
290ea 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
290eb 7c 7c 20 21 70 4f 66 66 73 65 74 20 7c 7c 20 70  || !pOffset || p
290ec 4c 69 6d 69 74 20 29 3b 20 2f 2a 20 4f 46 46 53  Limit ); /* OFFS
290ed 45 54 20 69 6d 70 6c 69 65 73 20 4c 49 4d 49 54  ET implies LIMIT
290ee 20 2a 2f 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d   */.  if( pNew==
290ef 30 20 29 7b 0a 20 20 20 20 70 4e 65 77 20 3d 20  0 ){.    pNew = 
290f0 26 73 74 61 6e 64 69 6e 3b 0a 20 20 20 20 6d 65  &standin;.    me
290f1 6d 73 65 74 28 70 4e 65 77 2c 20 30 2c 20 73 69  mset(pNew, 0, si
290f2 7a 65 6f 66 28 2a 70 4e 65 77 29 29 3b 0a 20 20  zeof(*pNew));.  
290f3 7d 0a 20 20 69 66 28 20 70 45 4c 69 73 74 3d 3d  }.  if( pEList==
290f4 30 20 29 7b 0a 20 20 20 20 70 45 4c 69 73 74 20  0 ){.    pEList 
290f5 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
290f6 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20  tAppend(pParse, 
290f7 30 2c 20 73 71 6c 69 74 65 33 45 78 70 72 28 64  0, sqlite3Expr(d
290f8 62 2c 54 4b 5f 41 4c 4c 2c 30 29 29 3b 0a 20 20  b,TK_ALL,0));.  
290f9 7d 0a 20 20 70 4e 65 77 2d 3e 70 45 4c 69 73 74  }.  pNew->pEList
290fa 20 3d 20 70 45 4c 69 73 74 3b 0a 20 20 70 4e 65   = pEList;.  pNe
290fb 77 2d 3e 70 53 72 63 20 3d 20 70 53 72 63 3b 0a  w->pSrc = pSrc;.
290fc 20 20 70 4e 65 77 2d 3e 70 57 68 65 72 65 20 3d    pNew->pWhere =
290fd 20 70 57 68 65 72 65 3b 0a 20 20 70 4e 65 77 2d   pWhere;.  pNew-
290fe 3e 70 47 72 6f 75 70 42 79 20 3d 20 70 47 72 6f  >pGroupBy = pGro
290ff 75 70 42 79 3b 0a 20 20 70 4e 65 77 2d 3e 70 48  upBy;.  pNew->pH
29100 61 76 69 6e 67 20 3d 20 70 48 61 76 69 6e 67 3b  aving = pHaving;
29101 0a 20 20 70 4e 65 77 2d 3e 70 4f 72 64 65 72 42  .  pNew->pOrderB
29102 79 20 3d 20 70 4f 72 64 65 72 42 79 3b 0a 20 20  y = pOrderBy;.  
29103 70 4e 65 77 2d 3e 73 65 6c 46 6c 61 67 73 20 3d  pNew->selFlags =
29104 20 69 73 44 69 73 74 69 6e 63 74 20 3f 20 53 46   isDistinct ? SF
29105 5f 44 69 73 74 69 6e 63 74 20 3a 20 30 3b 0a 20  _Distinct : 0;. 
29106 20 70 4e 65 77 2d 3e 6f 70 20 3d 20 54 4b 5f 53   pNew->op = TK_S
29107 45 4c 45 43 54 3b 0a 20 20 70 4e 65 77 2d 3e 70  ELECT;.  pNew->p
29108 4c 69 6d 69 74 20 3d 20 70 4c 69 6d 69 74 3b 0a  Limit = pLimit;.
29109 20 20 70 4e 65 77 2d 3e 70 4f 66 66 73 65 74 20    pNew->pOffset 
2910a 3d 20 70 4f 66 66 73 65 74 3b 0a 20 20 61 73 73  = pOffset;.  ass
2910b 65 72 74 28 20 70 4f 66 66 73 65 74 3d 3d 30 20  ert( pOffset==0 
2910c 7c 7c 20 70 4c 69 6d 69 74 21 3d 30 20 29 3b 0a  || pLimit!=0 );.
2910d 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e    pNew->addrOpen
2910e 45 70 68 6d 5b 30 5d 20 3d 20 2d 31 3b 0a 20 20  Ephm[0] = -1;.  
2910f 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70  pNew->addrOpenEp
29110 68 6d 5b 31 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e  hm[1] = -1;.  pN
29111 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d  ew->addrOpenEphm
29112 5b 32 5d 20 3d 20 2d 31 3b 0a 20 20 69 66 28 20  [2] = -1;.  if( 
29113 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
29114 20 29 20 7b 0a 20 20 20 20 63 6c 65 61 72 53 65   ) {.    clearSe
29115 6c 65 63 74 28 64 62 2c 20 70 4e 65 77 29 3b 0a  lect(db, pNew);.
29116 20 20 20 20 69 66 28 20 70 4e 65 77 21 3d 26 73      if( pNew!=&s
29117 74 61 6e 64 69 6e 20 29 20 73 71 6c 69 74 65 33  tandin ) sqlite3
29118 44 62 46 72 65 65 28 64 62 2c 20 70 4e 65 77 29  DbFree(db, pNew)
29119 3b 0a 20 20 20 20 70 4e 65 77 20 3d 20 30 3b 0a  ;.    pNew = 0;.
2911a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65    }.  return pNe
2911b 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65  w;.}../*.** Dele
2911c 74 65 20 74 68 65 20 67 69 76 65 6e 20 53 65 6c  te the given Sel
2911d 65 63 74 20 73 74 72 75 63 74 75 72 65 20 61 6e  ect structure an
2911e 64 20 61 6c 6c 20 6f 66 20 69 74 73 20 73 75 62  d all of its sub
2911f 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a 53  structures..*/.S
29120 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
29121 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  id sqlite3Select
29122 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a  Delete(sqlite3 *
29123 64 62 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a  db, Select *p){.
29124 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 63    if( p ){.    c
29125 6c 65 61 72 53 65 6c 65 63 74 28 64 62 2c 20 70  learSelect(db, p
29126 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  );.    sqlite3Db
29127 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 20 7d  Free(db, p);.  }
29128 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20  .}../*.** Given 
29129 31 20 74 6f 20 33 20 69 64 65 6e 74 69 66 69 65  1 to 3 identifie
2912a 72 73 20 70 72 65 63 65 65 64 69 6e 67 20 74 68  rs preceeding th
2912b 65 20 4a 4f 49 4e 20 6b 65 79 77 6f 72 64 2c 20  e JOIN keyword, 
2912c 64 65 74 65 72 6d 69 6e 65 20 74 68 65 0a 2a 2a  determine the.**
2912d 20 74 79 70 65 20 6f 66 20 6a 6f 69 6e 2e 20 20   type of join.  
2912e 52 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67 65  Return an intege
2912f 72 20 63 6f 6e 73 74 61 6e 74 20 74 68 61 74 20  r constant that 
29130 65 78 70 72 65 73 73 65 73 20 74 68 61 74 20 74  expresses that t
29131 79 70 65 0a 2a 2a 20 69 6e 20 74 65 72 6d 73 20  ype.** in terms 
29132 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
29133 20 62 69 74 20 76 61 6c 75 65 73 3a 0a 2a 2a 0a   bit values:.**.
29134 2a 2a 20 20 20 20 20 4a 54 5f 49 4e 4e 45 52 0a  **     JT_INNER.
29135 2a 2a 20 20 20 20 20 4a 54 5f 43 52 4f 53 53 0a  **     JT_CROSS.
29136 2a 2a 20 20 20 20 20 4a 54 5f 4f 55 54 45 52 0a  **     JT_OUTER.
29137 2a 2a 20 20 20 20 20 4a 54 5f 4e 41 54 55 52 41  **     JT_NATURA
29138 4c 0a 2a 2a 20 20 20 20 20 4a 54 5f 4c 45 46 54  L.**     JT_LEFT
29139 0a 2a 2a 20 20 20 20 20 4a 54 5f 52 49 47 48 54  .**     JT_RIGHT
2913a 0a 2a 2a 0a 2a 2a 20 41 20 66 75 6c 6c 20 6f 75  .**.** A full ou
2913b 74 65 72 20 6a 6f 69 6e 20 69 73 20 74 68 65 20  ter join is the 
2913c 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 4a  combination of J
2913d 54 5f 4c 45 46 54 20 61 6e 64 20 4a 54 5f 52 49  T_LEFT and JT_RI
2913e 47 48 54 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  GHT..**.** If an
2913f 20 69 6c 6c 65 67 61 6c 20 6f 72 20 75 6e 73 75   illegal or unsu
29140 70 70 6f 72 74 65 64 20 6a 6f 69 6e 20 74 79 70  pported join typ
29141 65 20 69 73 20 73 65 65 6e 2c 20 74 68 65 6e 20  e is seen, then 
29142 73 74 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20  still return.** 
29143 61 20 6a 6f 69 6e 20 74 79 70 65 2c 20 62 75 74  a join type, but
29144 20 70 75 74 20 61 6e 20 65 72 72 6f 72 20 69 6e   put an error in
29145 20 74 68 65 20 70 50 61 72 73 65 20 73 74 72 75   the pParse stru
29146 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  cture..*/.SQLITE
29147 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
29148 69 74 65 33 4a 6f 69 6e 54 79 70 65 28 50 61 72  ite3JoinType(Par
29149 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65  se *pParse, Toke
2914a 6e 20 2a 70 41 2c 20 54 6f 6b 65 6e 20 2a 70 42  n *pA, Token *pB
2914b 2c 20 54 6f 6b 65 6e 20 2a 70 43 29 7b 0a 20 20  , Token *pC){.  
2914c 69 6e 74 20 6a 6f 69 6e 74 79 70 65 20 3d 20 30  int jointype = 0
2914d 3b 0a 20 20 54 6f 6b 65 6e 20 2a 61 70 41 6c 6c  ;.  Token *apAll
2914e 5b 33 5d 3b 0a 20 20 54 6f 6b 65 6e 20 2a 70 3b  [3];.  Token *p;
2914f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
29150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
29151 20 20 20 30 31 32 33 34 35 36 37 38 39 20 31 32     0123456789 12
29152 33 34 35 36 37 38 39 20 31 32 33 34 35 36 37 38  3456789 12345678
29153 39 20 31 32 33 20 2a 2f 0a 20 20 73 74 61 74 69  9 123 */.  stati
29154 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 4b 65  c const char zKe
29155 79 54 65 78 74 5b 5d 20 3d 20 22 6e 61 74 75 72  yText[] = "natur
29156 61 6c 65 66 74 6f 75 74 65 72 69 67 68 74 66 75  aleftouterightfu
29157 6c 6c 69 6e 6e 65 72 63 72 6f 73 73 22 3b 0a 20  llinnercross";. 
29158 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74   static const st
29159 72 75 63 74 20 7b 0a 20 20 20 20 75 38 20 69 3b  ruct {.    u8 i;
2915a 20 20 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e          /* Begin
2915b 6e 69 6e 67 20 6f 66 20 6b 65 79 77 6f 72 64 20  ning of keyword 
2915c 74 65 78 74 20 69 6e 20 7a 4b 65 79 54 65 78 74  text in zKeyText
2915d 5b 5d 20 2a 2f 0a 20 20 20 20 75 38 20 6e 43 68  [] */.    u8 nCh
2915e 61 72 3b 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68  ar;    /* Length
2915f 20 6f 66 20 74 68 65 20 6b 65 79 77 6f 72 64 20   of the keyword 
29160 69 6e 20 63 68 61 72 61 63 74 65 72 73 20 2a 2f  in characters */
29161 0a 20 20 20 20 75 38 20 63 6f 64 65 3b 20 20 20  .    u8 code;   
29162 20 20 2f 2a 20 4a 6f 69 6e 20 74 79 70 65 20 6d    /* Join type m
29163 61 73 6b 20 2a 2f 0a 20 20 7d 20 61 4b 65 79 77  ask */.  } aKeyw
29164 6f 72 64 5b 5d 20 3d 20 7b 0a 20 20 20 20 2f 2a  ord[] = {.    /*
29165 20 6e 61 74 75 72 61 6c 20 2a 2f 20 7b 20 30 2c   natural */ { 0,
29166 20 20 37 2c 20 4a 54 5f 4e 41 54 55 52 41 4c 20    7, JT_NATURAL 
29167 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d                 }
29168 2c 0a 20 20 20 20 2f 2a 20 6c 65 66 74 20 20 20  ,.    /* left   
29169 20 2a 2f 20 7b 20 36 2c 20 20 34 2c 20 4a 54 5f   */ { 6,  4, JT_
2916a 4c 45 46 54 7c 4a 54 5f 4f 55 54 45 52 20 20 20  LEFT|JT_OUTER   
2916b 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 2f 2a         },.    /*
2916c 20 6f 75 74 65 72 20 20 20 2a 2f 20 7b 20 31 30   outer   */ { 10
2916d 2c 20 35 2c 20 4a 54 5f 4f 55 54 45 52 20 20 20  , 5, JT_OUTER   
2916e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d                 }
2916f 2c 0a 20 20 20 20 2f 2a 20 72 69 67 68 74 20 20  ,.    /* right  
29170 20 2a 2f 20 7b 20 31 34 2c 20 35 2c 20 4a 54 5f   */ { 14, 5, JT_
29171 52 49 47 48 54 7c 4a 54 5f 4f 55 54 45 52 20 20  RIGHT|JT_OUTER  
29172 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 2f 2a         },.    /*
29173 20 66 75 6c 6c 20 20 20 20 2a 2f 20 7b 20 31 39   full    */ { 19
29174 2c 20 34 2c 20 4a 54 5f 4c 45 46 54 7c 4a 54 5f  , 4, JT_LEFT|JT_
29175 52 49 47 48 54 7c 4a 54 5f 4f 55 54 45 52 20 7d  RIGHT|JT_OUTER }
29176 2c 0a 20 20 20 20 2f 2a 20 69 6e 6e 65 72 20 20  ,.    /* inner  
29177 20 2a 2f 20 7b 20 32 33 2c 20 35 2c 20 4a 54 5f   */ { 23, 5, JT_
29178 49 4e 4e 45 52 20 20 20 20 20 20 20 20 20 20 20  INNER           
29179 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 2f 2a         },.    /*
2917a 20 63 72 6f 73 73 20 20 20 2a 2f 20 7b 20 32 38   cross   */ { 28
2917b 2c 20 35 2c 20 4a 54 5f 49 4e 4e 45 52 7c 4a 54  , 5, JT_INNER|JT
2917c 5f 43 52 4f 53 53 20 20 20 20 20 20 20 20 20 7d  _CROSS         }
2917d 2c 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69 2c 20  ,.  };.  int i, 
2917e 6a 3b 0a 20 20 61 70 41 6c 6c 5b 30 5d 20 3d 20  j;.  apAll[0] = 
2917f 70 41 3b 0a 20 20 61 70 41 6c 6c 5b 31 5d 20 3d  pA;.  apAll[1] =
29180 20 70 42 3b 0a 20 20 61 70 41 6c 6c 5b 32 5d 20   pB;.  apAll[2] 
29181 3d 20 70 43 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  = pC;.  for(i=0;
29182 20 69 3c 33 20 26 26 20 61 70 41 6c 6c 5b 69 5d   i<3 && apAll[i]
29183 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20  ; i++){.    p = 
29184 61 70 41 6c 6c 5b 69 5d 3b 0a 20 20 20 20 66 6f  apAll[i];.    fo
29185 72 28 6a 3d 30 3b 20 6a 3c 41 72 72 61 79 53 69  r(j=0; j<ArraySi
29186 7a 65 28 61 4b 65 79 77 6f 72 64 29 3b 20 6a 2b  ze(aKeyword); j+
29187 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 2d  +){.      if( p-
29188 3e 6e 3d 3d 61 4b 65 79 77 6f 72 64 5b 6a 5d 2e  >n==aKeyword[j].
29189 6e 43 68 61 72 20 0a 20 20 20 20 20 20 20 20 20  nChar .         
2918a 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49   && sqlite3StrNI
2918b 43 6d 70 28 28 63 68 61 72 2a 29 70 2d 3e 7a 2c  Cmp((char*)p->z,
2918c 20 26 7a 4b 65 79 54 65 78 74 5b 61 4b 65 79 77   &zKeyText[aKeyw
2918d 6f 72 64 5b 6a 5d 2e 69 5d 2c 20 70 2d 3e 6e 29  ord[j].i], p->n)
2918e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 6a  ==0 ){.        j
2918f 6f 69 6e 74 79 70 65 20 7c 3d 20 61 4b 65 79 77  ointype |= aKeyw
29190 6f 72 64 5b 6a 5d 2e 63 6f 64 65 3b 0a 20 20 20  ord[j].code;.   
29191 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
29192 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 74 65    }.    }.    te
29193 73 74 63 61 73 65 28 20 6a 3d 3d 30 20 7c 7c 20  stcase( j==0 || 
29194 6a 3d 3d 31 20 7c 7c 20 6a 3d 3d 32 20 7c 7c 20  j==1 || j==2 || 
29195 6a 3d 3d 33 20 7c 7c 20 6a 3d 3d 34 20 7c 7c 20  j==3 || j==4 || 
29196 6a 3d 3d 35 20 7c 7c 20 6a 3d 3d 36 20 29 3b 0a  j==5 || j==6 );.
29197 20 20 20 20 69 66 28 20 6a 3e 3d 41 72 72 61 79      if( j>=Array
29198 53 69 7a 65 28 61 4b 65 79 77 6f 72 64 29 20 29  Size(aKeyword) )
29199 7b 0a 20 20 20 20 20 20 6a 6f 69 6e 74 79 70 65  {.      jointype
2919a 20 7c 3d 20 4a 54 5f 45 52 52 4f 52 3b 0a 20 20   |= JT_ERROR;.  
2919b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
2919c 0a 20 20 7d 0a 20 20 69 66 28 0a 20 20 20 20 20  .  }.  if(.     
2919d 28 6a 6f 69 6e 74 79 70 65 20 26 20 28 4a 54 5f  (jointype & (JT_
2919e 49 4e 4e 45 52 7c 4a 54 5f 4f 55 54 45 52 29 29  INNER|JT_OUTER))
2919f 3d 3d 28 4a 54 5f 49 4e 4e 45 52 7c 4a 54 5f 4f  ==(JT_INNER|JT_O
291a0 55 54 45 52 29 20 7c 7c 0a 20 20 20 20 20 28 6a  UTER) ||.     (j
291a1 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 45 52 52  ointype & JT_ERR
291a2 4f 52 29 21 3d 30 0a 20 20 29 7b 0a 20 20 20 20  OR)!=0.  ){.    
291a3 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 70 20  const char *zSp 
291a4 3d 20 22 20 22 3b 0a 20 20 20 20 61 73 73 65 72  = " ";.    asser
291a5 74 28 20 70 42 21 3d 30 20 29 3b 0a 20 20 20 20  t( pB!=0 );.    
291a6 69 66 28 20 70 43 3d 3d 30 20 29 7b 20 7a 53 70  if( pC==0 ){ zSp
291a7 2b 2b 3b 20 7d 0a 20 20 20 20 73 71 6c 69 74 65  ++; }.    sqlite
291a8 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
291a9 2c 20 22 75 6e 6b 6e 6f 77 6e 20 6f 72 20 75 6e  , "unknown or un
291aa 73 75 70 70 6f 72 74 65 64 20 6a 6f 69 6e 20 74  supported join t
291ab 79 70 65 3a 20 22 0a 20 20 20 20 20 20 20 22 25  ype: ".       "%
291ac 54 20 25 54 25 73 25 54 22 2c 20 70 41 2c 20 70  T %T%s%T", pA, p
291ad 42 2c 20 7a 53 70 2c 20 70 43 29 3b 0a 20 20 20  B, zSp, pC);.   
291ae 20 6a 6f 69 6e 74 79 70 65 20 3d 20 4a 54 5f 49   jointype = JT_I
291af 4e 4e 45 52 3b 0a 20 20 7d 65 6c 73 65 20 69 66  NNER;.  }else if
291b0 28 20 28 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54  ( (jointype & JT
291b1 5f 4f 55 54 45 52 29 21 3d 30 20 0a 20 20 20 20  _OUTER)!=0 .    
291b2 20 20 20 20 20 26 26 20 28 6a 6f 69 6e 74 79 70       && (jointyp
291b3 65 20 26 20 28 4a 54 5f 4c 45 46 54 7c 4a 54 5f  e & (JT_LEFT|JT_
291b4 52 49 47 48 54 29 29 21 3d 4a 54 5f 4c 45 46 54  RIGHT))!=JT_LEFT
291b5 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
291b6 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
291b7 0a 20 20 20 20 20 20 22 52 49 47 48 54 20 61 6e  .      "RIGHT an
291b8 64 20 46 55 4c 4c 20 4f 55 54 45 52 20 4a 4f 49  d FULL OUTER JOI
291b9 4e 73 20 61 72 65 20 6e 6f 74 20 63 75 72 72 65  Ns are not curre
291ba 6e 74 6c 79 20 73 75 70 70 6f 72 74 65 64 22 29  ntly supported")
291bb 3b 0a 20 20 20 20 6a 6f 69 6e 74 79 70 65 20 3d  ;.    jointype =
291bc 20 4a 54 5f 49 4e 4e 45 52 3b 0a 20 20 7d 0a 20   JT_INNER;.  }. 
291bd 20 72 65 74 75 72 6e 20 6a 6f 69 6e 74 79 70 65   return jointype
291be 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
291bf 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 61  n the index of a
291c0 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 74 61 62   column in a tab
291c1 6c 65 2e 20 20 52 65 74 75 72 6e 20 2d 31 20 69  le.  Return -1 i
291c2 66 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20  f the column.** 
291c3 69 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 65 64  is not contained
291c4 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a   in the table..*
291c5 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6c  /.static int col
291c6 75 6d 6e 49 6e 64 65 78 28 54 61 62 6c 65 20 2a  umnIndex(Table *
291c7 70 54 61 62 2c 20 63 6f 6e 73 74 20 63 68 61 72  pTab, const char
291c8 20 2a 7a 43 6f 6c 29 7b 0a 20 20 69 6e 74 20 69   *zCol){.  int i
291c9 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
291ca 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  Tab->nCol; i++){
291cb 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
291cc 53 74 72 49 43 6d 70 28 70 54 61 62 2d 3e 61 43  StrICmp(pTab->aC
291cd 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 7a 43 6f  ol[i].zName, zCo
291ce 6c 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 69  l)==0 ) return i
291cf 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2d  ;.  }.  return -
291d0 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61  1;.}../*.** Crea
291d1 74 65 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  te an expression
291d2 20 6e 6f 64 65 20 66 6f 72 20 61 6e 20 69 64 65   node for an ide
291d3 6e 74 69 66 69 65 72 20 77 69 74 68 20 74 68 65  ntifier with the
291d4 20 6e 61 6d 65 20 6f 66 20 7a 4e 61 6d 65 0a 2a   name of zName.*
291d5 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
291d6 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 43 72   Expr *sqlite3Cr
291d7 65 61 74 65 49 64 45 78 70 72 28 50 61 72 73 65  eateIdExpr(Parse
291d8 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20   *pParse, const 
291d9 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20  char *zName){.  
291da 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 45 78  return sqlite3Ex
291db 70 72 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 54  pr(pParse->db, T
291dc 4b 5f 49 44 2c 20 7a 4e 61 6d 65 29 3b 0a 7d 0a  K_ID, zName);.}.
291dd 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 74 65 72  ./*.** Add a ter
291de 6d 20 74 6f 20 74 68 65 20 57 48 45 52 45 20 65  m to the WHERE e
291df 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 2a 70 70  xpression in *pp
291e0 45 78 70 72 20 74 68 61 74 20 72 65 71 75 69 72  Expr that requir
291e1 65 73 20 74 68 65 0a 2a 2a 20 7a 43 6f 6c 20 63  es the.** zCol c
291e2 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 65 71 75 61  olumn to be equa
291e3 6c 20 69 6e 20 74 68 65 20 74 77 6f 20 74 61 62  l in the two tab
291e4 6c 65 73 20 70 54 61 62 31 20 61 6e 64 20 70 54  les pTab1 and pT
291e5 61 62 32 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ab2..*/.static v
291e6 6f 69 64 20 61 64 64 57 68 65 72 65 54 65 72 6d  oid addWhereTerm
291e7 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
291e8 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e,           /* 
291e9 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
291ea 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
291eb 2a 7a 43 6f 6c 2c 20 20 20 20 20 20 20 20 2f 2a  *zCol,        /*
291ec 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c   Name of the col
291ed 75 6d 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54  umn */.  const T
291ee 61 62 6c 65 20 2a 70 54 61 62 31 2c 20 20 20 20  able *pTab1,    
291ef 20 20 2f 2a 20 46 69 72 73 74 20 74 61 62 6c 65    /* First table
291f0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
291f1 20 2a 7a 41 6c 69 61 73 31 2c 20 20 20 20 20 2f   *zAlias1,     /
291f2 2a 20 41 6c 69 61 73 20 66 6f 72 20 66 69 72 73  * Alias for firs
291f3 74 20 74 61 62 6c 65 2e 20 20 4d 61 79 20 62 65  t table.  May be
291f4 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74   NULL */.  const
291f5 20 54 61 62 6c 65 20 2a 70 54 61 62 32 2c 20 20   Table *pTab2,  
291f6 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 74 61      /* Second ta
291f7 62 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ble */.  const c
291f8 68 61 72 20 2a 7a 41 6c 69 61 73 32 2c 20 20 20  har *zAlias2,   
291f9 20 20 2f 2a 20 41 6c 69 61 73 20 66 6f 72 20 73    /* Alias for s
291fa 65 63 6f 6e 64 20 74 61 62 6c 65 2e 20 20 4d 61  econd table.  Ma
291fb 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69  y be NULL */.  i
291fc 6e 74 20 69 52 69 67 68 74 4a 6f 69 6e 54 61 62  nt iRightJoinTab
291fd 6c 65 2c 20 20 20 20 20 2f 2a 20 56 44 42 45 20  le,     /* VDBE 
291fe 63 75 72 73 6f 72 20 66 6f 72 20 74 68 65 20 72  cursor for the r
291ff 69 67 68 74 20 74 61 62 6c 65 20 2a 2f 0a 20 20  ight table */.  
29200 45 78 70 72 20 2a 2a 70 70 45 78 70 72 2c 20 20  Expr **ppExpr,  
29201 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20           /* Add 
29202 74 68 65 20 65 71 75 61 6c 69 74 79 20 74 65 72  the equality ter
29203 6d 20 74 6f 20 74 68 69 73 20 65 78 70 72 65 73  m to this expres
29204 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 73  sion */.  int is
29205 4f 75 74 65 72 4a 6f 69 6e 20 20 20 20 20 20 20  OuterJoin       
29206 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 64 65     /* True if de
29207 61 6c 69 6e 67 20 77 69 74 68 20 61 6e 20 4f 55  aling with an OU
29208 54 45 52 20 6a 6f 69 6e 20 2a 2f 0a 29 7b 0a 20  TER join */.){. 
29209 20 45 78 70 72 20 2a 70 45 31 61 2c 20 2a 70 45   Expr *pE1a, *pE
2920a 31 62 2c 20 2a 70 45 31 63 3b 0a 20 20 45 78 70  1b, *pE1c;.  Exp
2920b 72 20 2a 70 45 32 61 2c 20 2a 70 45 32 62 2c 20  r *pE2a, *pE2b, 
2920c 2a 70 45 32 63 3b 0a 20 20 45 78 70 72 20 2a 70  *pE2c;.  Expr *p
2920d 45 3b 0a 0a 20 20 70 45 31 61 20 3d 20 73 71 6c  E;..  pE1a = sql
2920e 69 74 65 33 43 72 65 61 74 65 49 64 45 78 70 72  ite3CreateIdExpr
2920f 28 70 50 61 72 73 65 2c 20 7a 43 6f 6c 29 3b 0a  (pParse, zCol);.
29210 20 20 70 45 32 61 20 3d 20 73 71 6c 69 74 65 33    pE2a = sqlite3
29211 43 72 65 61 74 65 49 64 45 78 70 72 28 70 50 61  CreateIdExpr(pPa
29212 72 73 65 2c 20 7a 43 6f 6c 29 3b 0a 20 20 69 66  rse, zCol);.  if
29213 28 20 7a 41 6c 69 61 73 31 3d 3d 30 20 29 7b 0a  ( zAlias1==0 ){.
29214 20 20 20 20 7a 41 6c 69 61 73 31 20 3d 20 70 54      zAlias1 = pT
29215 61 62 31 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 7d 0a  ab1->zName;.  }.
29216 20 20 70 45 31 62 20 3d 20 73 71 6c 69 74 65 33    pE1b = sqlite3
29217 43 72 65 61 74 65 49 64 45 78 70 72 28 70 50 61  CreateIdExpr(pPa
29218 72 73 65 2c 20 7a 41 6c 69 61 73 31 29 3b 0a 20  rse, zAlias1);. 
29219 20 69 66 28 20 7a 41 6c 69 61 73 32 3d 3d 30 20   if( zAlias2==0 
2921a 29 7b 0a 20 20 20 20 7a 41 6c 69 61 73 32 20 3d  ){.    zAlias2 =
2921b 20 70 54 61 62 32 2d 3e 7a 4e 61 6d 65 3b 0a 20   pTab2->zName;. 
2921c 20 7d 0a 20 20 70 45 32 62 20 3d 20 73 71 6c 69   }.  pE2b = sqli
2921d 74 65 33 43 72 65 61 74 65 49 64 45 78 70 72 28  te3CreateIdExpr(
2921e 70 50 61 72 73 65 2c 20 7a 41 6c 69 61 73 32 29  pParse, zAlias2)
2921f 3b 0a 20 20 70 45 31 63 20 3d 20 73 71 6c 69 74  ;.  pE1c = sqlit
29220 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
29221 54 4b 5f 44 4f 54 2c 20 70 45 31 62 2c 20 70 45  TK_DOT, pE1b, pE
29222 31 61 2c 20 30 29 3b 0a 20 20 70 45 32 63 20 3d  1a, 0);.  pE2c =
29223 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
29224 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 70 45  arse, TK_DOT, pE
29225 32 62 2c 20 70 45 32 61 2c 20 30 29 3b 0a 20 20  2b, pE2a, 0);.  
29226 70 45 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  pE = sqlite3PExp
29227 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 45 51 2c  r(pParse, TK_EQ,
29228 20 70 45 31 63 2c 20 70 45 32 63 2c 20 30 29 3b   pE1c, pE2c, 0);
29229 0a 20 20 69 66 28 20 70 45 20 26 26 20 69 73 4f  .  if( pE && isO
2922a 75 74 65 72 4a 6f 69 6e 20 29 7b 0a 20 20 20 20  uterJoin ){.    
2922b 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79 28  ExprSetProperty(
2922c 70 45 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29  pE, EP_FromJoin)
2922d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 45  ;.    assert( !E
2922e 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74  xprHasAnyPropert
2922f 79 28 70 45 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e  y(pE, EP_TokenOn
29230 6c 79 7c 45 50 5f 52 65 64 75 63 65 64 29 20 29  ly|EP_Reduced) )
29231 3b 0a 20 20 20 20 45 78 70 72 53 65 74 49 72 72  ;.    ExprSetIrr
29232 65 64 75 63 69 62 6c 65 28 70 45 29 3b 0a 20 20  educible(pE);.  
29233 20 20 70 45 2d 3e 69 52 69 67 68 74 4a 6f 69 6e    pE->iRightJoin
29234 54 61 62 6c 65 20 3d 20 28 69 31 36 29 69 52 69  Table = (i16)iRi
29235 67 68 74 4a 6f 69 6e 54 61 62 6c 65 3b 0a 20 20  ghtJoinTable;.  
29236 7d 0a 20 20 2a 70 70 45 78 70 72 20 3d 20 73 71  }.  *ppExpr = sq
29237 6c 69 74 65 33 45 78 70 72 41 6e 64 28 70 50 61  lite3ExprAnd(pPa
29238 72 73 65 2d 3e 64 62 2c 2a 70 70 45 78 70 72 2c  rse->db,*ppExpr,
29239 20 70 45 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53   pE);.}../*.** S
2923a 65 74 20 74 68 65 20 45 50 5f 46 72 6f 6d 4a 6f  et the EP_FromJo
2923b 69 6e 20 70 72 6f 70 65 72 74 79 20 6f 6e 20 61  in property on a
2923c 6c 6c 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20  ll terms of the 
2923d 67 69 76 65 6e 20 65 78 70 72 65 73 73 69 6f 6e  given expression
2923e 2e 0a 2a 2a 20 41 6e 64 20 73 65 74 20 74 68 65  ..** And set the
2923f 20 45 78 70 72 2e 69 52 69 67 68 74 4a 6f 69 6e   Expr.iRightJoin
29240 54 61 62 6c 65 20 74 6f 20 69 54 61 62 6c 65 20  Table to iTable 
29241 66 6f 72 20 65 76 65 72 79 20 74 65 72 6d 20 69  for every term i
29242 6e 20 74 68 65 0a 2a 2a 20 65 78 70 72 65 73 73  n the.** express
29243 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 45  ion..**.** The E
29244 50 5f 46 72 6f 6d 4a 6f 69 6e 20 70 72 6f 70 65  P_FromJoin prope
29245 72 74 79 20 69 73 20 75 73 65 64 20 6f 6e 20 74  rty is used on t
29246 65 72 6d 73 20 6f 66 20 61 6e 20 65 78 70 72 65  erms of an expre
29247 73 73 69 6f 6e 20 74 6f 20 74 65 6c 6c 0a 2a 2a  ssion to tell.**
29248 20 74 68 65 20 4c 45 46 54 20 4f 55 54 45 52 20   the LEFT OUTER 
29249 4a 4f 49 4e 20 70 72 6f 63 65 73 73 69 6e 67 20  JOIN processing 
2924a 6c 6f 67 69 63 20 74 68 61 74 20 74 68 69 73 20  logic that this 
2924b 74 65 72 6d 20 69 73 20 70 61 72 74 20 6f 66 20  term is part of 
2924c 74 68 65 0a 2a 2a 20 6a 6f 69 6e 20 72 65 73 74  the.** join rest
2924d 72 69 63 74 69 6f 6e 20 73 70 65 63 69 66 69 65  riction specifie
2924e 64 20 69 6e 20 74 68 65 20 4f 4e 20 6f 72 20 55  d in the ON or U
2924f 53 49 4e 47 20 63 6c 61 75 73 65 20 61 6e 64 20  SING clause and 
29250 6e 6f 74 20 61 20 70 61 72 74 0a 2a 2a 20 6f 66  not a part.** of
29251 20 74 68 65 20 6d 6f 72 65 20 67 65 6e 65 72 61   the more genera
29252 6c 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20  l WHERE clause. 
29253 20 54 68 65 73 65 20 74 65 72 6d 73 20 61 72 65   These terms are
29254 20 6d 6f 76 65 64 20 6f 76 65 72 20 74 6f 20 74   moved over to t
29255 68 65 0a 2a 2a 20 57 48 45 52 45 20 63 6c 61 75  he.** WHERE clau
29256 73 65 20 64 75 72 69 6e 67 20 6a 6f 69 6e 20 70  se during join p
29257 72 6f 63 65 73 73 69 6e 67 20 62 75 74 20 77 65  rocessing but we
29258 20 6e 65 65 64 20 74 6f 20 72 65 6d 65 6d 62 65   need to remembe
29259 72 20 74 68 61 74 20 74 68 65 79 0a 2a 2a 20 6f  r that they.** o
2925a 72 69 67 69 6e 61 74 65 64 20 69 6e 20 74 68 65  riginated in the
2925b 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61   ON or USING cla
2925c 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 45  use..**.** The E
2925d 78 70 72 2e 69 52 69 67 68 74 4a 6f 69 6e 54 61  xpr.iRightJoinTa
2925e 62 6c 65 20 74 65 6c 6c 73 20 74 68 65 20 57 48  ble tells the WH
2925f 45 52 45 20 63 6c 61 75 73 65 20 70 72 6f 63 65  ERE clause proce
29260 73 73 69 6e 67 20 74 68 61 74 20 74 68 65 0a 2a  ssing that the.*
29261 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 64 65 70  * expression dep
29262 65 6e 64 73 20 6f 6e 20 74 61 62 6c 65 20 69 52  ends on table iR
29263 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 65 76  ightJoinTable ev
29264 65 6e 20 69 66 20 74 68 61 74 20 74 61 62 6c 65  en if that table
29265 20 69 73 20 6e 6f 74 0a 2a 2a 20 65 78 70 6c 69   is not.** expli
29266 63 69 74 6c 79 20 6d 65 6e 74 69 6f 6e 65 64 20  citly mentioned 
29267 69 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  in the expressio
29268 6e 2e 20 20 54 68 61 74 20 69 6e 66 6f 72 6d 61  n.  That informa
29269 74 69 6f 6e 20 69 73 20 6e 65 65 64 65 64 0a 2a  tion is needed.*
2926a 2a 20 66 6f 72 20 63 61 73 65 73 20 6c 69 6b 65  * for cases like
2926b 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   this:.**.**    
2926c 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31  SELECT * FROM t1
2926d 20 4c 45 46 54 20 4a 4f 49 4e 20 74 32 20 4f 4e   LEFT JOIN t2 ON
2926e 20 74 31 2e 61 3d 74 32 2e 62 20 41 4e 44 20 74   t1.a=t2.b AND t
2926f 31 2e 78 3d 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  1.x=5.**.** The 
29270 77 68 65 72 65 20 63 6c 61 75 73 65 20 6e 65 65  where clause nee
29271 64 73 20 74 6f 20 64 65 66 65 72 20 74 68 65 20  ds to defer the 
29272 68 61 6e 64 6c 69 6e 67 20 6f 66 20 74 68 65 20  handling of the 
29273 74 31 2e 78 3d 35 0a 2a 2a 20 74 65 72 6d 20 75  t1.x=5.** term u
29274 6e 74 69 6c 20 61 66 74 65 72 20 74 68 65 20 74  ntil after the t
29275 32 20 6c 6f 6f 70 20 6f 66 20 74 68 65 20 6a 6f  2 loop of the jo
29276 69 6e 2e 20 20 49 6e 20 74 68 61 74 20 77 61 79  in.  In that way
29277 2c 20 61 0a 2a 2a 20 4e 55 4c 4c 20 74 32 20 72  , a.** NULL t2 r
29278 6f 77 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 72  ow will be inser
29279 74 65 64 20 77 68 65 6e 65 76 65 72 20 74 31 2e  ted whenever t1.
2927a 78 21 3d 35 2e 20 20 49 66 20 77 65 20 64 6f 20  x!=5.  If we do 
2927b 6e 6f 74 0a 2a 2a 20 64 65 66 65 72 20 74 68 65  not.** defer the
2927c 20 68 61 6e 64 6c 69 6e 67 20 6f 66 20 74 31 2e   handling of t1.
2927d 78 3d 35 2c 20 69 74 20 77 69 6c 6c 20 62 65 20  x=5, it will be 
2927e 70 72 6f 63 65 73 73 65 64 20 69 6d 6d 65 64 69  processed immedi
2927f 61 74 65 6c 79 0a 2a 2a 20 61 66 74 65 72 20 74  ately.** after t
29280 68 65 20 74 31 20 6c 6f 6f 70 20 61 6e 64 20 72  he t1 loop and r
29281 6f 77 73 20 77 69 74 68 20 74 31 2e 78 21 3d 35  ows with t1.x!=5
29282 20 77 69 6c 6c 20 6e 65 76 65 72 20 61 70 70 65   will never appe
29283 61 72 20 69 6e 0a 2a 2a 20 74 68 65 20 6f 75 74  ar in.** the out
29284 70 75 74 2c 20 77 68 69 63 68 20 69 73 20 69 6e  put, which is in
29285 63 6f 72 72 65 63 74 2e 0a 2a 2f 0a 73 74 61 74  correct..*/.stat
29286 69 63 20 76 6f 69 64 20 73 65 74 4a 6f 69 6e 45  ic void setJoinE
29287 78 70 72 28 45 78 70 72 20 2a 70 2c 20 69 6e 74  xpr(Expr *p, int
29288 20 69 54 61 62 6c 65 29 7b 0a 20 20 77 68 69 6c   iTable){.  whil
29289 65 28 20 70 20 29 7b 0a 20 20 20 20 45 78 70 72  e( p ){.    Expr
2928a 53 65 74 50 72 6f 70 65 72 74 79 28 70 2c 20 45  SetProperty(p, E
2928b 50 5f 46 72 6f 6d 4a 6f 69 6e 29 3b 0a 20 20 20  P_FromJoin);.   
2928c 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61   assert( !ExprHa
2928d 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 2c 20  sAnyProperty(p, 
2928e 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f  EP_TokenOnly|EP_
2928f 52 65 64 75 63 65 64 29 20 29 3b 0a 20 20 20 20  Reduced) );.    
29290 45 78 70 72 53 65 74 49 72 72 65 64 75 63 69 62  ExprSetIrreducib
29291 6c 65 28 70 29 3b 0a 20 20 20 20 70 2d 3e 69 52  le(p);.    p->iR
29292 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 3d 20  ightJoinTable = 
29293 28 69 31 36 29 69 54 61 62 6c 65 3b 0a 20 20 20  (i16)iTable;.   
29294 20 73 65 74 4a 6f 69 6e 45 78 70 72 28 70 2d 3e   setJoinExpr(p->
29295 70 4c 65 66 74 2c 20 69 54 61 62 6c 65 29 3b 0a  pLeft, iTable);.
29296 20 20 20 20 70 20 3d 20 70 2d 3e 70 52 69 67 68      p = p->pRigh
29297 74 3b 0a 20 20 7d 20 0a 7d 0a 0a 2f 2a 0a 2a 2a  t;.  } .}../*.**
29298 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 70 72   This routine pr
29299 6f 63 65 73 73 65 73 20 74 68 65 20 6a 6f 69 6e  ocesses the join
2929a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72   information for
2929b 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
2929c 65 6e 74 2e 0a 2a 2a 20 4f 4e 20 61 6e 64 20 55  ent..** ON and U
2929d 53 49 4e 47 20 63 6c 61 75 73 65 73 20 61 72 65  SING clauses are
2929e 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20   converted into 
2929f 65 78 74 72 61 20 74 65 72 6d 73 20 6f 66 20 74  extra terms of t
292a0 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e  he WHERE clause.
292a1 0a 2a 2a 20 4e 41 54 55 52 41 4c 20 6a 6f 69 6e  .** NATURAL join
292a2 73 20 61 6c 73 6f 20 63 72 65 61 74 65 20 65 78  s also create ex
292a3 74 72 61 20 57 48 45 52 45 20 63 6c 61 75 73 65  tra WHERE clause
292a4 20 74 65 72 6d 73 2e 0a 2a 2a 0a 2a 2a 20 54 68   terms..**.** Th
292a5 65 20 74 65 72 6d 73 20 6f 66 20 61 20 46 52 4f  e terms of a FRO
292a6 4d 20 63 6c 61 75 73 65 20 61 72 65 20 63 6f 6e  M clause are con
292a7 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 53 65  tained in the Se
292a8 6c 65 63 74 2e 70 53 72 63 20 73 74 72 75 63 74  lect.pSrc struct
292a9 75 72 65 2e 0a 2a 2a 20 54 68 65 20 6c 65 66 74  ure..** The left
292aa 20 6d 6f 73 74 20 74 61 62 6c 65 20 69 73 20 74   most table is t
292ab 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 69  he first entry i
292ac 6e 20 53 65 6c 65 63 74 2e 70 53 72 63 2e 20 20  n Select.pSrc.  
292ad 54 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 0a 2a  The right-most.*
292ae 2a 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 6c  * table is the l
292af 61 73 74 20 65 6e 74 72 79 2e 20 20 54 68 65 20  ast entry.  The 
292b0 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72 20 69 73  join operator is
292b1 20 68 65 6c 64 20 69 6e 20 74 68 65 20 65 6e 74   held in the ent
292b2 72 79 20 74 6f 0a 2a 2a 20 74 68 65 20 6c 65 66  ry to.** the lef
292b3 74 2e 20 20 54 68 75 73 20 65 6e 74 72 79 20 30  t.  Thus entry 0
292b4 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6a 6f   contains the jo
292b5 69 6e 20 6f 70 65 72 61 74 6f 72 20 66 6f 72 20  in operator for 
292b6 74 68 65 20 6a 6f 69 6e 20 62 65 74 77 65 65 6e  the join between
292b7 0a 2a 2a 20 65 6e 74 72 69 65 73 20 30 20 61 6e  .** entries 0 an
292b8 64 20 31 2e 20 20 41 6e 79 20 4f 4e 20 6f 72 20  d 1.  Any ON or 
292b9 55 53 49 4e 47 20 63 6c 61 75 73 65 73 20 61 73  USING clauses as
292ba 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
292bb 65 20 6a 6f 69 6e 20 61 72 65 0a 2a 2a 20 61 6c  e join are.** al
292bc 73 6f 20 61 74 74 61 63 68 65 64 20 74 6f 20 74  so attached to t
292bd 68 65 20 6c 65 66 74 20 65 6e 74 72 79 2e 0a 2a  he left entry..*
292be 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
292bf 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75  e returns the nu
292c0 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20 65  mber of errors e
292c1 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2f 0a 73  ncountered..*/.s
292c2 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65  tatic int sqlite
292c3 50 72 6f 63 65 73 73 4a 6f 69 6e 28 50 61 72 73  ProcessJoin(Pars
292c4 65 20 2a 70 50 61 72 73 65 2c 20 53 65 6c 65 63  e *pParse, Selec
292c5 74 20 2a 70 29 7b 0a 20 20 53 72 63 4c 69 73 74  t *p){.  SrcList
292c6 20 2a 70 53 72 63 3b 20 20 20 20 20 20 20 20 20   *pSrc;         
292c7 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20           /* All 
292c8 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 46 52  tables in the FR
292c9 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69  OM clause */.  i
292ca 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20  nt i, j;        
292cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
292cc 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20  * Loop counters 
292cd 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c  */.  struct SrcL
292ce 69 73 74 5f 69 74 65 6d 20 2a 70 4c 65 66 74 3b  ist_item *pLeft;
292cf 20 20 20 20 20 2f 2a 20 4c 65 66 74 20 74 61 62       /* Left tab
292d0 6c 65 20 62 65 69 6e 67 20 6a 6f 69 6e 65 64 20  le being joined 
292d1 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c  */.  struct SrcL
292d2 69 73 74 5f 69 74 65 6d 20 2a 70 52 69 67 68 74  ist_item *pRight
292d3 3b 20 20 20 20 2f 2a 20 52 69 67 68 74 20 74 61  ;    /* Right ta
292d4 62 6c 65 20 62 65 69 6e 67 20 6a 6f 69 6e 65 64  ble being joined
292d5 20 2a 2f 0a 0a 20 20 70 53 72 63 20 3d 20 70 2d   */..  pSrc = p-
292d6 3e 70 53 72 63 3b 0a 20 20 70 4c 65 66 74 20 3d  >pSrc;.  pLeft =
292d7 20 26 70 53 72 63 2d 3e 61 5b 30 5d 3b 0a 20 20   &pSrc->a[0];.  
292d8 70 52 69 67 68 74 20 3d 20 26 70 4c 65 66 74 5b  pRight = &pLeft[
292d9 31 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  1];.  for(i=0; i
292da 3c 70 53 72 63 2d 3e 6e 53 72 63 2d 31 3b 20 69  <pSrc->nSrc-1; i
292db 2b 2b 2c 20 70 52 69 67 68 74 2b 2b 2c 20 70 4c  ++, pRight++, pL
292dc 65 66 74 2b 2b 29 7b 0a 20 20 20 20 54 61 62 6c  eft++){.    Tabl
292dd 65 20 2a 70 4c 65 66 74 54 61 62 20 3d 20 70 4c  e *pLeftTab = pL
292de 65 66 74 2d 3e 70 54 61 62 3b 0a 20 20 20 20 54  eft->pTab;.    T
292df 61 62 6c 65 20 2a 70 52 69 67 68 74 54 61 62 20  able *pRightTab 
292e0 3d 20 70 52 69 67 68 74 2d 3e 70 54 61 62 3b 0a  = pRight->pTab;.
292e1 20 20 20 20 69 6e 74 20 69 73 4f 75 74 65 72 3b      int isOuter;
292e2 0a 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28  ..    if( NEVER(
292e3 70 4c 65 66 74 54 61 62 3d 3d 30 20 7c 7c 20 70  pLeftTab==0 || p
292e4 52 69 67 68 74 54 61 62 3d 3d 30 29 20 29 20 63  RightTab==0) ) c
292e5 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 73 4f  ontinue;.    isO
292e6 75 74 65 72 20 3d 20 28 70 52 69 67 68 74 2d 3e  uter = (pRight->
292e7 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 4f 55  jointype & JT_OU
292e8 54 45 52 29 21 3d 30 3b 0a 0a 20 20 20 20 2f 2a  TER)!=0;..    /*
292e9 20 57 68 65 6e 20 74 68 65 20 4e 41 54 55 52 41   When the NATURA
292ea 4c 20 6b 65 79 77 6f 72 64 20 69 73 20 70 72 65  L keyword is pre
292eb 73 65 6e 74 2c 20 61 64 64 20 57 48 45 52 45 20  sent, add WHERE 
292ec 63 6c 61 75 73 65 20 74 65 72 6d 73 20 66 6f 72  clause terms for
292ed 0a 20 20 20 20 2a 2a 20 65 76 65 72 79 20 63 6f  .    ** every co
292ee 6c 75 6d 6e 20 74 68 61 74 20 74 68 65 20 74 77  lumn that the tw
292ef 6f 20 74 61 62 6c 65 73 20 68 61 76 65 20 69 6e  o tables have in
292f0 20 63 6f 6d 6d 6f 6e 2e 0a 20 20 20 20 2a 2f 0a   common..    */.
292f1 20 20 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e      if( pRight->
292f2 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 4e 41  jointype & JT_NA
292f3 54 55 52 41 4c 20 29 7b 0a 20 20 20 20 20 20 69  TURAL ){.      i
292f4 66 28 20 70 52 69 67 68 74 2d 3e 70 4f 6e 20 7c  f( pRight->pOn |
292f5 7c 20 70 52 69 67 68 74 2d 3e 70 55 73 69 6e 67  | pRight->pUsing
292f6 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
292f7 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
292f8 73 65 2c 20 22 61 20 4e 41 54 55 52 41 4c 20 6a  se, "a NATURAL j
292f9 6f 69 6e 20 6d 61 79 20 6e 6f 74 20 68 61 76 65  oin may not have
292fa 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 22 61   ".           "a
292fb 6e 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c  n ON or USING cl
292fc 61 75 73 65 22 2c 20 30 29 3b 0a 20 20 20 20 20  ause", 0);.     
292fd 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
292fe 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 6a     }.      for(j
292ff 3d 30 3b 20 6a 3c 70 4c 65 66 74 54 61 62 2d 3e  =0; j<pLeftTab->
29300 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20  nCol; j++){.    
29301 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20      char *zName 
29302 3d 20 70 4c 65 66 74 54 61 62 2d 3e 61 43 6f 6c  = pLeftTab->aCol
29303 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20  [j].zName;.     
29304 20 20 20 69 66 28 20 63 6f 6c 75 6d 6e 49 6e 64     if( columnInd
29305 65 78 28 70 52 69 67 68 74 54 61 62 2c 20 7a 4e  ex(pRightTab, zN
29306 61 6d 65 29 3e 3d 30 20 29 7b 0a 20 20 20 20 20  ame)>=0 ){.     
29307 20 20 20 20 20 61 64 64 57 68 65 72 65 54 65 72       addWhereTer
29308 6d 28 70 50 61 72 73 65 2c 20 7a 4e 61 6d 65 2c  m(pParse, zName,
29309 20 70 4c 65 66 74 54 61 62 2c 20 70 4c 65 66 74   pLeftTab, pLeft
2930a 2d 3e 7a 41 6c 69 61 73 2c 20 0a 20 20 20 20 20  ->zAlias, .     
2930b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2930c 20 20 20 20 20 20 20 20 20 70 52 69 67 68 74 54           pRightT
2930d 61 62 2c 20 70 52 69 67 68 74 2d 3e 7a 41 6c 69  ab, pRight->zAli
2930e 61 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  as,.            
2930f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29310 20 20 70 52 69 67 68 74 2d 3e 69 43 75 72 73 6f    pRight->iCurso
29311 72 2c 20 26 70 2d 3e 70 57 68 65 72 65 2c 20 69  r, &p->pWhere, i
29312 73 4f 75 74 65 72 29 3b 0a 20 20 20 20 20 20 20  sOuter);.       
29313 20 20 20 0a 20 20 20 20 20 20 20 20 7d 0a 20 20     .        }.  
29314 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
29315 20 2f 2a 20 44 69 73 61 6c 6c 6f 77 20 62 6f 74   /* Disallow bot
29316 68 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20 63  h ON and USING c
29317 6c 61 75 73 65 73 20 69 6e 20 74 68 65 20 73 61  lauses in the sa
29318 6d 65 20 6a 6f 69 6e 0a 20 20 20 20 2a 2f 0a 20  me join.    */. 
29319 20 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 70     if( pRight->p
2931a 4f 6e 20 26 26 20 70 52 69 67 68 74 2d 3e 70 55  On && pRight->pU
2931b 73 69 6e 67 20 29 7b 0a 20 20 20 20 20 20 73 71  sing ){.      sq
2931c 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2931d 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20 68 61  arse, "cannot ha
2931e 76 65 20 62 6f 74 68 20 4f 4e 20 61 6e 64 20 55  ve both ON and U
2931f 53 49 4e 47 20 22 0a 20 20 20 20 20 20 20 20 22  SING ".        "
29320 63 6c 61 75 73 65 73 20 69 6e 20 74 68 65 20 73  clauses in the s
29321 61 6d 65 20 6a 6f 69 6e 22 29 3b 0a 20 20 20 20  ame join");.    
29322 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
29323 7d 0a 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 68  }..    /* Add th
29324 65 20 4f 4e 20 63 6c 61 75 73 65 20 74 6f 20 74  e ON clause to t
29325 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 57 48  he end of the WH
29326 45 52 45 20 63 6c 61 75 73 65 2c 20 63 6f 6e 6e  ERE clause, conn
29327 65 63 74 65 64 20 62 79 0a 20 20 20 20 2a 2a 20  ected by.    ** 
29328 61 6e 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e  an AND operator.
29329 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
2932a 70 52 69 67 68 74 2d 3e 70 4f 6e 20 29 7b 0a 20  pRight->pOn ){. 
2932b 20 20 20 20 20 69 66 28 20 69 73 4f 75 74 65 72       if( isOuter
2932c 20 29 20 73 65 74 4a 6f 69 6e 45 78 70 72 28 70   ) setJoinExpr(p
2932d 52 69 67 68 74 2d 3e 70 4f 6e 2c 20 70 52 69 67  Right->pOn, pRig
2932e 68 74 2d 3e 69 43 75 72 73 6f 72 29 3b 0a 20 20  ht->iCursor);.  
2932f 20 20 20 20 70 2d 3e 70 57 68 65 72 65 20 3d 20      p->pWhere = 
29330 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 70  sqlite3ExprAnd(p
29331 50 61 72 73 65 2d 3e 64 62 2c 20 70 2d 3e 70 57  Parse->db, p->pW
29332 68 65 72 65 2c 20 70 52 69 67 68 74 2d 3e 70 4f  here, pRight->pO
29333 6e 29 3b 0a 20 20 20 20 20 20 70 52 69 67 68 74  n);.      pRight
29334 2d 3e 70 4f 6e 20 3d 20 30 3b 0a 20 20 20 20 7d  ->pOn = 0;.    }
29335 0a 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20  ..    /* Create 
29336 65 78 74 72 61 20 74 65 72 6d 73 20 6f 6e 20 74  extra terms on t
29337 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
29338 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20  for each column 
29339 6e 61 6d 65 64 0a 20 20 20 20 2a 2a 20 69 6e 20  named.    ** in 
2933a 74 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65  the USING clause
2933b 2e 20 20 45 78 61 6d 70 6c 65 3a 20 49 66 20 74  .  Example: If t
2933c 68 65 20 74 77 6f 20 74 61 62 6c 65 73 20 74 6f  he two tables to
2933d 20 62 65 20 6a 6f 69 6e 65 64 20 61 72 65 20 0a   be joined are .
2933e 20 20 20 20 2a 2a 20 41 20 61 6e 64 20 42 20 61      ** A and B a
2933f 6e 64 20 74 68 65 20 55 53 49 4e 47 20 63 6c 61  nd the USING cla
29340 75 73 65 20 6e 61 6d 65 73 20 58 2c 20 59 2c 20  use names X, Y, 
29341 61 6e 64 20 5a 2c 20 74 68 65 6e 20 61 64 64 20  and Z, then add 
29342 74 68 69 73 0a 20 20 20 20 2a 2a 20 74 6f 20 74  this.    ** to t
29343 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 3a  he WHERE clause:
29344 20 20 20 20 41 2e 58 3d 42 2e 58 20 41 4e 44 20      A.X=B.X AND 
29345 41 2e 59 3d 42 2e 59 20 41 4e 44 20 41 2e 5a 3d  A.Y=B.Y AND A.Z=
29346 42 2e 5a 0a 20 20 20 20 2a 2a 20 52 65 70 6f 72  B.Z.    ** Repor
29347 74 20 61 6e 20 65 72 72 6f 72 20 69 66 20 61 6e  t an error if an
29348 79 20 63 6f 6c 75 6d 6e 20 6d 65 6e 74 69 6f 6e  y column mention
29349 65 64 20 69 6e 20 74 68 65 20 55 53 49 4e 47 20  ed in the USING 
2934a 63 6c 61 75 73 65 20 69 73 0a 20 20 20 20 2a 2a  clause is.    **
2934b 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 65 64 20 69   not contained i
2934c 6e 20 62 6f 74 68 20 74 61 62 6c 65 73 20 74 6f  n both tables to
2934d 20 62 65 20 6a 6f 69 6e 65 64 2e 0a 20 20 20 20   be joined..    
2934e 2a 2f 0a 20 20 20 20 69 66 28 20 70 52 69 67 68  */.    if( pRigh
2934f 74 2d 3e 70 55 73 69 6e 67 20 29 7b 0a 20 20 20  t->pUsing ){.   
29350 20 20 20 49 64 4c 69 73 74 20 2a 70 4c 69 73 74     IdList *pList
29351 20 3d 20 70 52 69 67 68 74 2d 3e 70 55 73 69 6e   = pRight->pUsin
29352 67 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30  g;.      for(j=0
29353 3b 20 6a 3c 70 4c 69 73 74 2d 3e 6e 49 64 3b 20  ; j<pList->nId; 
29354 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 63 68  j++){.        ch
29355 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 4c 69 73  ar *zName = pLis
29356 74 2d 3e 61 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20  t->a[j].zName;. 
29357 20 20 20 20 20 20 20 69 66 28 20 63 6f 6c 75 6d         if( colum
29358 6e 49 6e 64 65 78 28 70 4c 65 66 74 54 61 62 2c  nIndex(pLeftTab,
29359 20 7a 4e 61 6d 65 29 3c 30 20 7c 7c 20 63 6f 6c   zName)<0 || col
2935a 75 6d 6e 49 6e 64 65 78 28 70 52 69 67 68 74 54  umnIndex(pRightT
2935b 61 62 2c 20 7a 4e 61 6d 65 29 3c 30 20 29 7b 0a  ab, zName)<0 ){.
2935c 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2935d 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2935e 2c 20 22 63 61 6e 6e 6f 74 20 6a 6f 69 6e 20 75  , "cannot join u
2935f 73 69 6e 67 20 63 6f 6c 75 6d 6e 20 25 73 20 2d  sing column %s -
29360 20 63 6f 6c 75 6d 6e 20 22 0a 20 20 20 20 20 20   column ".      
29361 20 20 20 20 20 20 22 6e 6f 74 20 70 72 65 73 65        "not prese
29362 6e 74 20 69 6e 20 62 6f 74 68 20 74 61 62 6c 65  nt in both table
29363 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  s", zName);.    
29364 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
29365 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
29366 20 20 61 64 64 57 68 65 72 65 54 65 72 6d 28 70    addWhereTerm(p
29367 50 61 72 73 65 2c 20 7a 4e 61 6d 65 2c 20 70 4c  Parse, zName, pL
29368 65 66 74 54 61 62 2c 20 70 4c 65 66 74 2d 3e 7a  eftTab, pLeft->z
29369 41 6c 69 61 73 2c 20 0a 20 20 20 20 20 20 20 20  Alias, .        
2936a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2936b 20 20 20 20 70 52 69 67 68 74 54 61 62 2c 20 70      pRightTab, p
2936c 52 69 67 68 74 2d 3e 7a 41 6c 69 61 73 2c 0a 20  Right->zAlias,. 
2936d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2936e 20 20 20 20 20 20 20 20 20 20 20 70 52 69 67 68             pRigh
2936f 74 2d 3e 69 43 75 72 73 6f 72 2c 20 26 70 2d 3e  t->iCursor, &p->
29370 70 57 68 65 72 65 2c 20 69 73 4f 75 74 65 72 29  pWhere, isOuter)
29371 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
29372 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
29373 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20  }../*.** Insert 
29374 63 6f 64 65 20 69 6e 74 6f 20 22 76 22 20 74 68  code into "v" th
29375 61 74 20 77 69 6c 6c 20 70 75 73 68 20 74 68 65  at will push the
29376 20 72 65 63 6f 72 64 20 6f 6e 20 74 68 65 20 74   record on the t
29377 6f 70 20 6f 66 20 74 68 65 0a 2a 2a 20 73 74 61  op of the.** sta
29378 63 6b 20 69 6e 74 6f 20 74 68 65 20 73 6f 72 74  ck into the sort
29379 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  er..*/.static vo
2937a 69 64 20 70 75 73 68 4f 6e 74 6f 53 6f 72 74 65  id pushOntoSorte
2937b 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  r(.  Parse *pPar
2937c 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50  se,         /* P
2937d 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
2937e 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72  .  ExprList *pOr
2937f 64 65 72 42 79 2c 20 20 20 20 2f 2a 20 54 68 65  derBy,    /* The
29380 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
29381 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53   */.  Select *pS
29382 65 6c 65 63 74 2c 20 20 20 20 20 20 20 2f 2a 20  elect,       /* 
29383 54 68 65 20 77 68 6f 6c 65 20 53 45 4c 45 43 54  The whole SELECT
29384 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
29385 69 6e 74 20 72 65 67 44 61 74 61 20 20 20 20 20  int regData     
29386 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
29387 65 72 20 68 6f 6c 64 69 6e 67 20 64 61 74 61 20  er holding data 
29388 74 6f 20 62 65 20 73 6f 72 74 65 64 20 2a 2f 0a  to be sorted */.
29389 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70  ){.  Vdbe *v = p
2938a 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20  Parse->pVdbe;.  
2938b 69 6e 74 20 6e 45 78 70 72 20 3d 20 70 4f 72 64  int nExpr = pOrd
2938c 65 72 42 79 2d 3e 6e 45 78 70 72 3b 0a 20 20 69  erBy->nExpr;.  i
2938d 6e 74 20 72 65 67 42 61 73 65 20 3d 20 73 71 6c  nt regBase = sql
2938e 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65  ite3GetTempRange
2938f 28 70 50 61 72 73 65 2c 20 6e 45 78 70 72 2b 32  (pParse, nExpr+2
29390 29 3b 0a 20 20 69 6e 74 20 72 65 67 52 65 63 6f  );.  int regReco
29391 72 64 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  rd = sqlite3GetT
29392 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
29393 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
29394 68 65 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b  heClear(pParse);
29395 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  .  sqlite3ExprCo
29396 64 65 45 78 70 72 4c 69 73 74 28 70 50 61 72 73  deExprList(pPars
29397 65 2c 20 70 4f 72 64 65 72 42 79 2c 20 72 65 67  e, pOrderBy, reg
29398 42 61 73 65 2c 20 30 29 3b 0a 20 20 73 71 6c 69  Base, 0);.  sqli
29399 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2939a 20 4f 50 5f 53 65 71 75 65 6e 63 65 2c 20 70 4f   OP_Sequence, pO
2939b 72 64 65 72 42 79 2d 3e 69 45 43 75 72 73 6f 72  rderBy->iECursor
2939c 2c 20 72 65 67 42 61 73 65 2b 6e 45 78 70 72 29  , regBase+nExpr)
2939d 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43  ;.  sqlite3ExprC
2939e 6f 64 65 4d 6f 76 65 28 70 50 61 72 73 65 2c 20  odeMove(pParse, 
2939f 72 65 67 44 61 74 61 2c 20 72 65 67 42 61 73 65  regData, regBase
293a0 2b 6e 45 78 70 72 2b 31 2c 20 31 29 3b 0a 20 20  +nExpr+1, 1);.  
293a1 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
293a2 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f  3(v, OP_MakeReco
293a3 72 64 2c 20 72 65 67 42 61 73 65 2c 20 6e 45 78  rd, regBase, nEx
293a4 70 72 20 2b 20 32 2c 20 72 65 67 52 65 63 6f 72  pr + 2, regRecor
293a5 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  d);.  sqlite3Vdb
293a6 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64  eAddOp2(v, OP_Id
293a7 78 49 6e 73 65 72 74 2c 20 70 4f 72 64 65 72 42  xInsert, pOrderB
293a8 79 2d 3e 69 45 43 75 72 73 6f 72 2c 20 72 65 67  y->iECursor, reg
293a9 52 65 63 6f 72 64 29 3b 0a 20 20 73 71 6c 69 74  Record);.  sqlit
293aa 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
293ab 28 70 50 61 72 73 65 2c 20 72 65 67 52 65 63 6f  (pParse, regReco
293ac 72 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65  rd);.  sqlite3Re
293ad 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70  leaseTempRange(p
293ae 50 61 72 73 65 2c 20 72 65 67 42 61 73 65 2c 20  Parse, regBase, 
293af 6e 45 78 70 72 2b 32 29 3b 0a 20 20 69 66 28 20  nExpr+2);.  if( 
293b0 70 53 65 6c 65 63 74 2d 3e 69 4c 69 6d 69 74 20  pSelect->iLimit 
293b1 29 7b 0a 20 20 20 20 69 6e 74 20 61 64 64 72 31  ){.    int addr1
293b2 2c 20 61 64 64 72 32 3b 0a 20 20 20 20 69 6e 74  , addr2;.    int
293b3 20 69 4c 69 6d 69 74 3b 0a 20 20 20 20 69 66 28   iLimit;.    if(
293b4 20 70 53 65 6c 65 63 74 2d 3e 69 4f 66 66 73 65   pSelect->iOffse
293b5 74 20 29 7b 0a 20 20 20 20 20 20 69 4c 69 6d 69  t ){.      iLimi
293b6 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 69 4f 66  t = pSelect->iOf
293b7 66 73 65 74 2b 31 3b 0a 20 20 20 20 7d 65 6c 73  fset+1;.    }els
293b8 65 7b 0a 20 20 20 20 20 20 69 4c 69 6d 69 74 20  e{.      iLimit 
293b9 3d 20 70 53 65 6c 65 63 74 2d 3e 69 4c 69 6d 69  = pSelect->iLimi
293ba 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 64 64  t;.    }.    add
293bb 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  r1 = sqlite3Vdbe
293bc 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 5a  AddOp1(v, OP_IfZ
293bd 65 72 6f 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20  ero, iLimit);.  
293be 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
293bf 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d  Op2(v, OP_AddImm
293c0 2c 20 69 4c 69 6d 69 74 2c 20 2d 31 29 3b 0a 20  , iLimit, -1);. 
293c1 20 20 20 61 64 64 72 32 20 3d 20 73 71 6c 69 74     addr2 = sqlit
293c2 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20  e3VdbeAddOp0(v, 
293c3 4f 50 5f 47 6f 74 6f 29 3b 0a 20 20 20 20 73 71  OP_Goto);.    sq
293c4 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
293c5 65 28 76 2c 20 61 64 64 72 31 29 3b 0a 20 20 20  e(v, addr1);.   
293c6 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
293c7 70 31 28 76 2c 20 4f 50 5f 4c 61 73 74 2c 20 70  p1(v, OP_Last, p
293c8 4f 72 64 65 72 42 79 2d 3e 69 45 43 75 72 73 6f  OrderBy->iECurso
293c9 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  r);.    sqlite3V
293ca 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
293cb 44 65 6c 65 74 65 2c 20 70 4f 72 64 65 72 42 79  Delete, pOrderBy
293cc 2d 3e 69 45 43 75 72 73 6f 72 29 3b 0a 20 20 20  ->iECursor);.   
293cd 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
293ce 48 65 72 65 28 76 2c 20 61 64 64 72 32 29 3b 0a  Here(v, addr2);.
293cf 20 20 20 20 70 53 65 6c 65 63 74 2d 3e 69 4c 69      pSelect->iLi
293d0 6d 69 74 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a  mit = 0;.  }.}..
293d1 2f 2a 0a 2a 2a 20 41 64 64 20 63 6f 64 65 20 74  /*.** Add code t
293d2 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  o implement the 
293d3 4f 46 46 53 45 54 0a 2a 2f 0a 73 74 61 74 69 63  OFFSET.*/.static
293d4 20 76 6f 69 64 20 63 6f 64 65 4f 66 66 73 65 74   void codeOffset
293d5 28 0a 20 20 56 64 62 65 20 2a 76 2c 20 20 20 20  (.  Vdbe *v,    
293d6 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74        /* Generat
293d7 65 20 63 6f 64 65 20 69 6e 74 6f 20 74 68 69 73  e code into this
293d8 20 56 4d 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20   VM */.  Select 
293d9 2a 70 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68  *p,        /* Th
293da 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
293db 6e 74 20 62 65 69 6e 67 20 63 6f 64 65 64 20 2a  nt being coded *
293dc 2f 0a 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75  /.  int iContinu
293dd 65 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65  e     /* Jump he
293de 72 65 20 74 6f 20 73 6b 69 70 20 74 68 65 20 63  re to skip the c
293df 75 72 72 65 6e 74 20 72 65 63 6f 72 64 20 2a 2f  urrent record */
293e0 0a 29 7b 0a 20 20 69 66 28 20 70 2d 3e 69 4f 66  .){.  if( p->iOf
293e1 66 73 65 74 20 26 26 20 69 43 6f 6e 74 69 6e 75  fset && iContinu
293e2 65 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  e!=0 ){.    int 
293e3 61 64 64 72 3b 0a 20 20 20 20 73 71 6c 69 74 65  addr;.    sqlite
293e4 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
293e5 50 5f 41 64 64 49 6d 6d 2c 20 70 2d 3e 69 4f 66  P_AddImm, p->iOf
293e6 66 73 65 74 2c 20 2d 31 29 3b 0a 20 20 20 20 61  fset, -1);.    a
293e7 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
293e8 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66  eAddOp1(v, OP_If
293e9 4e 65 67 2c 20 70 2d 3e 69 4f 66 66 73 65 74 29  Neg, p->iOffset)
293ea 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
293eb 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
293ec 74 6f 2c 20 30 2c 20 69 43 6f 6e 74 69 6e 75 65  to, 0, iContinue
293ed 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  );.    VdbeComme
293ee 6e 74 28 28 76 2c 20 22 73 6b 69 70 20 4f 46 46  nt((v, "skip OFF
293ef 53 45 54 20 72 65 63 6f 72 64 73 22 29 29 3b 0a  SET records"));.
293f0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
293f1 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 29  umpHere(v, addr)
293f2 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  ;.  }.}../*.** A
293f3 64 64 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c  dd code that wil
293f4 6c 20 63 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20  l check to make 
293f5 73 75 72 65 20 74 68 65 20 4e 20 72 65 67 69 73  sure the N regis
293f6 74 65 72 73 20 73 74 61 72 74 69 6e 67 20 61 74  ters starting at
293f7 20 69 4d 65 6d 0a 2a 2a 20 66 6f 72 6d 20 61 20   iMem.** form a 
293f8 64 69 73 74 69 6e 63 74 20 65 6e 74 72 79 2e 20  distinct entry. 
293f9 20 69 54 61 62 20 69 73 20 61 20 73 6f 72 74 69   iTab is a sorti
293fa 6e 67 20 69 6e 64 65 78 20 74 68 61 74 20 68 6f  ng index that ho
293fb 6c 64 73 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a  lds previously.*
293fc 2a 20 73 65 65 6e 20 63 6f 6d 62 69 6e 61 74 69  * seen combinati
293fd 6f 6e 73 20 6f 66 20 74 68 65 20 4e 20 76 61 6c  ons of the N val
293fe 75 65 73 2e 20 20 41 20 6e 65 77 20 65 6e 74 72  ues.  A new entr
293ff 79 20 69 73 20 6d 61 64 65 20 69 6e 20 69 54 61  y is made in iTa
29400 62 0a 2a 2a 20 69 66 20 74 68 65 20 63 75 72 72  b.** if the curr
29401 65 6e 74 20 4e 20 76 61 6c 75 65 73 20 61 72 65  ent N values are
29402 20 6e 65 77 2e 0a 2a 2a 0a 2a 2a 20 41 20 6a 75   new..**.** A ju
29403 6d 70 20 74 6f 20 61 64 64 72 52 65 70 65 61 74  mp to addrRepeat
29404 20 69 73 20 6d 61 64 65 20 61 6e 64 20 74 68 65   is made and the
29405 20 4e 2b 31 20 76 61 6c 75 65 73 20 61 72 65 20   N+1 values are 
29406 70 6f 70 70 65 64 20 66 72 6f 6d 20 74 68 65 0a  popped from the.
29407 2a 2a 20 73 74 61 63 6b 20 69 66 20 74 68 65 20  ** stack if the 
29408 74 6f 70 20 4e 20 65 6c 65 6d 65 6e 74 73 20 61  top N elements a
29409 72 65 20 6e 6f 74 20 64 69 73 74 69 6e 63 74 2e  re not distinct.
2940a 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2940b 63 6f 64 65 44 69 73 74 69 6e 63 74 28 0a 20 20  codeDistinct(.  
2940c 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2940d 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e     /* Parsing an
2940e 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e  d code generatin
2940f 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69  g context */.  i
29410 6e 74 20 69 54 61 62 2c 20 20 20 20 20 20 20 20  nt iTab,        
29411 20 20 2f 2a 20 41 20 73 6f 72 74 69 6e 67 20 69    /* A sorting i
29412 6e 64 65 78 20 75 73 65 64 20 74 6f 20 74 65 73  ndex used to tes
29413 74 20 66 6f 72 20 64 69 73 74 69 6e 63 74 6e 65  t for distinctne
29414 73 73 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  ss */.  int addr
29415 52 65 70 65 61 74 2c 20 20 20 20 2f 2a 20 4a 75  Repeat,    /* Ju
29416 6d 70 20 74 6f 20 68 65 72 65 20 69 66 20 6e 6f  mp to here if no
29417 74 20 64 69 73 74 69 6e 63 74 20 2a 2f 0a 20 20  t distinct */.  
29418 69 6e 74 20 4e 2c 20 20 20 20 20 20 20 20 20 20  int N,          
29419 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
2941a 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 69 6e  elements */.  in
2941b 74 20 69 4d 65 6d 20 20 20 20 20 20 20 20 20 20  t iMem          
2941c 20 2f 2a 20 46 69 72 73 74 20 65 6c 65 6d 65 6e   /* First elemen
2941d 74 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a  t */.){.  Vdbe *
2941e 76 3b 0a 20 20 69 6e 74 20 72 31 3b 0a 0a 20 20  v;.  int r1;..  
2941f 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
29420 65 3b 0a 20 20 72 31 20 3d 20 73 71 6c 69 74 65  e;.  r1 = sqlite
29421 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
29422 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  se);.  sqlite3Vd
29423 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d  beAddOp3(v, OP_M
29424 61 6b 65 52 65 63 6f 72 64 2c 20 69 4d 65 6d 2c  akeRecord, iMem,
29425 20 4e 2c 20 72 31 29 3b 0a 20 20 73 71 6c 69 74   N, r1);.  sqlit
29426 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
29427 4f 50 5f 46 6f 75 6e 64 2c 20 69 54 61 62 2c 20  OP_Found, iTab, 
29428 61 64 64 72 52 65 70 65 61 74 2c 20 72 31 29 3b  addrRepeat, r1);
29429 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
2942a 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e  dOp2(v, OP_IdxIn
2942b 73 65 72 74 2c 20 69 54 61 62 2c 20 72 31 29 3b  sert, iTab, r1);
2942c 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  .  sqlite3Releas
2942d 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
2942e 20 72 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47   r1);.}../*.** G
2942f 65 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72  enerate an error
29430 20 6d 65 73 73 61 67 65 20 77 68 65 6e 20 61 20   message when a 
29431 53 45 4c 45 43 54 20 69 73 20 75 73 65 64 20 77  SELECT is used w
29432 69 74 68 69 6e 20 61 20 73 75 62 65 78 70 72 65  ithin a subexpre
29433 73 73 69 6f 6e 0a 2a 2a 20 28 65 78 61 6d 70 6c  ssion.** (exampl
29434 65 3a 20 20 22 61 20 49 4e 20 28 53 45 4c 45 43  e:  "a IN (SELEC
29435 54 20 2a 20 46 52 4f 4d 20 74 61 62 6c 65 29 22  T * FROM table)"
29436 29 20 62 75 74 20 69 74 20 68 61 73 20 6d 6f 72  ) but it has mor
29437 65 20 74 68 61 6e 20 31 20 72 65 73 75 6c 74 0a  e than 1 result.
29438 2a 2a 20 63 6f 6c 75 6d 6e 2e 20 20 57 65 20 64  ** column.  We d
29439 6f 20 74 68 69 73 20 69 6e 20 61 20 73 75 62 72  o this in a subr
2943a 6f 75 74 69 6e 65 20 62 65 63 61 75 73 65 20 74  outine because t
2943b 68 65 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  he error occurs 
2943c 69 6e 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 70  in multiple.** p
2943d 6c 61 63 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  laces..*/.static
2943e 20 69 6e 74 20 63 68 65 63 6b 46 6f 72 4d 75 6c   int checkForMul
2943f 74 69 43 6f 6c 75 6d 6e 53 65 6c 65 63 74 45 72  tiColumnSelectEr
29440 72 6f 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ror(.  Parse *pP
29441 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 50  arse,       /* P
29442 61 72 73 65 20 63 6f 6e 74 65 78 74 2e 20 2a 2f  arse context. */
29443 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 2a 70  .  SelectDest *p
29444 44 65 73 74 2c 20 20 20 2f 2a 20 44 65 73 74 69  Dest,   /* Desti
29445 6e 61 74 69 6f 6e 20 6f 66 20 53 45 4c 45 43 54  nation of SELECT
29446 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20 69 6e   results */.  in
29447 74 20 6e 45 78 70 72 20 20 20 20 20 20 20 20 20  t nExpr         
29448 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
29449 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 72  result columns r
2944a 65 74 75 72 6e 65 64 20 62 79 20 53 45 4c 45 43  eturned by SELEC
2944b 54 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 65 44  T */.){.  int eD
2944c 65 73 74 20 3d 20 70 44 65 73 74 2d 3e 65 44 65  est = pDest->eDe
2944d 73 74 3b 0a 20 20 69 66 28 20 6e 45 78 70 72 3e  st;.  if( nExpr>
2944e 31 20 26 26 20 28 65 44 65 73 74 3d 3d 53 52 54  1 && (eDest==SRT
2944f 5f 4d 65 6d 20 7c 7c 20 65 44 65 73 74 3d 3d 53  _Mem || eDest==S
29450 52 54 5f 53 65 74 29 20 29 7b 0a 20 20 20 20 73  RT_Set) ){.    s
29451 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
29452 50 61 72 73 65 2c 20 22 6f 6e 6c 79 20 61 20 73  Parse, "only a s
29453 69 6e 67 6c 65 20 72 65 73 75 6c 74 20 61 6c 6c  ingle result all
29454 6f 77 65 64 20 66 6f 72 20 22 0a 20 20 20 20 20  owed for ".     
29455 20 20 22 61 20 53 45 4c 45 43 54 20 74 68 61 74    "a SELECT that
29456 20 69 73 20 70 61 72 74 20 6f 66 20 61 6e 20 65   is part of an e
29457 78 70 72 65 73 73 69 6f 6e 22 29 3b 0a 20 20 20  xpression");.   
29458 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c   return 1;.  }el
29459 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30  se{.    return 0
2945a 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
2945b 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65 6e 65  his routine gene
2945c 72 61 74 65 73 20 74 68 65 20 63 6f 64 65 20 66  rates the code f
2945d 6f 72 20 74 68 65 20 69 6e 73 69 64 65 20 6f 66  or the inside of
2945e 20 74 68 65 20 69 6e 6e 65 72 20 6c 6f 6f 70 0a   the inner loop.
2945f 2a 2a 20 6f 66 20 61 20 53 45 4c 45 43 54 2e 0a  ** of a SELECT..
29460 2a 2a 0a 2a 2a 20 49 66 20 73 72 63 54 61 62 20  **.** If srcTab 
29461 61 6e 64 20 6e 43 6f 6c 75 6d 6e 20 61 72 65 20  and nColumn are 
29462 62 6f 74 68 20 7a 65 72 6f 2c 20 74 68 65 6e 20  both zero, then 
29463 74 68 65 20 70 45 4c 69 73 74 20 65 78 70 72 65  the pEList expre
29464 73 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 65 76  ssions.** are ev
29465 61 6c 75 61 74 65 64 20 69 6e 20 6f 72 64 65 72  aluated in order
29466 20 74 6f 20 67 65 74 20 74 68 65 20 64 61 74 61   to get the data
29467 20 66 6f 72 20 74 68 69 73 20 72 6f 77 2e 20 20   for this row.  
29468 49 66 20 6e 43 6f 6c 75 6d 6e 3e 30 0a 2a 2a 20  If nColumn>0.** 
29469 74 68 65 6e 20 64 61 74 61 20 69 73 20 70 75 6c  then data is pul
2946a 6c 65 64 20 66 72 6f 6d 20 73 72 63 54 61 62 20  led from srcTab 
2946b 61 6e 64 20 70 45 4c 69 73 74 20 69 73 20 75 73  and pEList is us
2946c 65 64 20 6f 6e 6c 79 20 74 6f 20 67 65 74 20 74  ed only to get t
2946d 68 65 0a 2a 2a 20 64 61 74 61 74 79 70 65 73 20  he.** datatypes 
2946e 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 2e  for each column.
2946f 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
29470 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70 28  selectInnerLoop(
29471 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
29472 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ,          /* Th
29473 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74  e parser context
29474 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c   */.  Select *p,
29475 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
29476 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 73 65   The complete se
29477 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 62  lect statement b
29478 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a 20 20  eing coded */.  
29479 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
2947a 2c 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20  ,       /* List 
2947b 6f 66 20 76 61 6c 75 65 73 20 62 65 69 6e 67 20  of values being 
2947c 65 78 74 72 61 63 74 65 64 20 2a 2f 0a 20 20 69  extracted */.  i
2947d 6e 74 20 73 72 63 54 61 62 2c 20 20 20 20 20 20  nt srcTab,      
2947e 20 20 20 20 20 20 20 2f 2a 20 50 75 6c 6c 20 64         /* Pull d
2947f 61 74 61 20 66 72 6f 6d 20 74 68 69 73 20 74 61  ata from this ta
29480 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f  ble */.  int nCo
29481 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 20 20 20  lumn,           
29482 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f   /* Number of co
29483 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 73 6f 75  lumns in the sou
29484 72 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 45  rce table */.  E
29485 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42  xprList *pOrderB
29486 79 2c 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f 74  y,     /* If not
29487 20 4e 55 4c 4c 2c 20 73 6f 72 74 20 72 65 73 75   NULL, sort resu
29488 6c 74 73 20 75 73 69 6e 67 20 74 68 69 73 20 6b  lts using this k
29489 65 79 20 2a 2f 0a 20 20 69 6e 74 20 64 69 73 74  ey */.  int dist
2948a 69 6e 63 74 2c 20 20 20 20 20 20 20 20 20 20 20  inct,           
2948b 2f 2a 20 49 66 20 3e 3d 30 2c 20 6d 61 6b 65 20  /* If >=0, make 
2948c 73 75 72 65 20 72 65 73 75 6c 74 73 20 61 72 65  sure results are
2948d 20 64 69 73 74 69 6e 63 74 20 2a 2f 0a 20 20 53   distinct */.  S
2948e 65 6c 65 63 74 44 65 73 74 20 2a 70 44 65 73 74  electDest *pDest
2948f 2c 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f  ,      /* How to
29490 20 64 69 73 70 6f 73 65 20 6f 66 20 74 68 65 20   dispose of the 
29491 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20 69 6e 74  results */.  int
29492 20 69 43 6f 6e 74 69 6e 75 65 2c 20 20 20 20 20   iContinue,     
29493 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72       /* Jump her
29494 65 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69  e to continue wi
29495 74 68 20 6e 65 78 74 20 72 6f 77 20 2a 2f 0a 20  th next row */. 
29496 20 69 6e 74 20 69 42 72 65 61 6b 20 20 20 20 20   int iBreak     
29497 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70           /* Jump
29498 20 68 65 72 65 20 74 6f 20 62 72 65 61 6b 20 6f   here to break o
29499 75 74 20 6f 66 20 74 68 65 20 69 6e 6e 65 72 20  ut of the inner 
2949a 6c 6f 6f 70 20 2a 2f 0a 29 7b 0a 20 20 56 64 62  loop */.){.  Vdb
2949b 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70  e *v = pParse->p
2949c 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  Vdbe;.  int i;. 
2949d 20 69 6e 74 20 68 61 73 44 69 73 74 69 6e 63 74   int hasDistinct
2949e 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65  ;        /* True
2949f 20 69 66 20 74 68 65 20 44 49 53 54 49 4e 43 54   if the DISTINCT
294a0 20 6b 65 79 77 6f 72 64 20 69 73 20 70 72 65 73   keyword is pres
294a1 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  ent */.  int reg
294a2 52 65 73 75 6c 74 3b 20 20 20 20 20 20 20 20 20  Result;         
294a3 20 20 20 20 20 2f 2a 20 53 74 61 72 74 20 6f 66       /* Start of
294a4 20 6d 65 6d 6f 72 79 20 68 6f 6c 64 69 6e 67 20   memory holding 
294a5 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20  result set */.  
294a6 69 6e 74 20 65 44 65 73 74 20 3d 20 70 44 65 73  int eDest = pDes
294a7 74 2d 3e 65 44 65 73 74 3b 20 20 20 2f 2a 20 48  t->eDest;   /* H
294a8 6f 77 20 74 6f 20 64 69 73 70 6f 73 65 20 6f 66  ow to dispose of
294a9 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20 69 6e   results */.  in
294aa 74 20 69 50 61 72 6d 20 3d 20 70 44 65 73 74 2d  t iParm = pDest-
294ab 3e 69 50 61 72 6d 3b 20 20 20 2f 2a 20 46 69 72  >iParm;   /* Fir
294ac 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 64  st argument to d
294ad 69 73 70 6f 73 61 6c 20 6d 65 74 68 6f 64 20 2a  isposal method *
294ae 2f 0a 20 20 69 6e 74 20 6e 52 65 73 75 6c 74 43  /.  int nResultC
294af 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ol;             
294b0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 73  /* Number of res
294b1 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 0a  ult columns */..
294b2 20 20 61 73 73 65 72 74 28 20 76 20 29 3b 0a 20    assert( v );. 
294b3 20 69 66 28 20 4e 45 56 45 52 28 76 3d 3d 30 29   if( NEVER(v==0)
294b4 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73   ) return;.  ass
294b5 65 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29  ert( pEList!=0 )
294b6 3b 0a 20 20 68 61 73 44 69 73 74 69 6e 63 74 20  ;.  hasDistinct 
294b7 3d 20 64 69 73 74 69 6e 63 74 3e 3d 30 3b 0a 20  = distinct>=0;. 
294b8 20 69 66 28 20 70 4f 72 64 65 72 42 79 3d 3d 30   if( pOrderBy==0
294b9 20 26 26 20 21 68 61 73 44 69 73 74 69 6e 63 74   && !hasDistinct
294ba 20 29 7b 0a 20 20 20 20 63 6f 64 65 4f 66 66 73   ){.    codeOffs
294bb 65 74 28 76 2c 20 70 2c 20 69 43 6f 6e 74 69 6e  et(v, p, iContin
294bc 75 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 50  ue);.  }..  /* P
294bd 75 6c 6c 20 74 68 65 20 72 65 71 75 65 73 74 65  ull the requeste
294be 64 20 63 6f 6c 75 6d 6e 73 2e 0a 20 20 2a 2f 0a  d columns..  */.
294bf 20 20 69 66 28 20 6e 43 6f 6c 75 6d 6e 3e 30 20    if( nColumn>0 
294c0 29 7b 0a 20 20 20 20 6e 52 65 73 75 6c 74 43 6f  ){.    nResultCo
294c1 6c 20 3d 20 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 7d  l = nColumn;.  }
294c2 65 6c 73 65 7b 0a 20 20 20 20 6e 52 65 73 75 6c  else{.    nResul
294c3 74 43 6f 6c 20 3d 20 70 45 4c 69 73 74 2d 3e 6e  tCol = pEList->n
294c4 45 78 70 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20  Expr;.  }.  if( 
294c5 70 44 65 73 74 2d 3e 69 4d 65 6d 3d 3d 30 20 29  pDest->iMem==0 )
294c6 7b 0a 20 20 20 20 70 44 65 73 74 2d 3e 69 4d 65  {.    pDest->iMe
294c7 6d 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  m = pParse->nMem
294c8 2b 31 3b 0a 20 20 20 20 70 44 65 73 74 2d 3e 6e  +1;.    pDest->n
294c9 4d 65 6d 20 3d 20 6e 52 65 73 75 6c 74 43 6f 6c  Mem = nResultCol
294ca 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d  ;.    pParse->nM
294cb 65 6d 20 2b 3d 20 6e 52 65 73 75 6c 74 43 6f 6c  em += nResultCol
294cc 3b 0a 20 20 7d 65 6c 73 65 7b 20 0a 20 20 20 20  ;.  }else{ .    
294cd 61 73 73 65 72 74 28 20 70 44 65 73 74 2d 3e 6e  assert( pDest->n
294ce 4d 65 6d 3d 3d 6e 52 65 73 75 6c 74 43 6f 6c 20  Mem==nResultCol 
294cf 29 3b 0a 20 20 7d 0a 20 20 72 65 67 52 65 73 75  );.  }.  regResu
294d0 6c 74 20 3d 20 70 44 65 73 74 2d 3e 69 4d 65 6d  lt = pDest->iMem
294d1 3b 0a 20 20 69 66 28 20 6e 43 6f 6c 75 6d 6e 3e  ;.  if( nColumn>
294d2 30 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30  0 ){.    for(i=0
294d3 3b 20 69 3c 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b  ; i<nColumn; i++
294d4 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
294d5 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
294d6 5f 43 6f 6c 75 6d 6e 2c 20 73 72 63 54 61 62 2c  _Column, srcTab,
294d7 20 69 2c 20 72 65 67 52 65 73 75 6c 74 2b 69 29   i, regResult+i)
294d8 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20  ;.    }.  }else 
294d9 69 66 28 20 65 44 65 73 74 21 3d 53 52 54 5f 45  if( eDest!=SRT_E
294da 78 69 73 74 73 20 29 7b 0a 20 20 20 20 2f 2a 20  xists ){.    /* 
294db 49 66 20 74 68 65 20 64 65 73 74 69 6e 61 74 69  If the destinati
294dc 6f 6e 20 69 73 20 61 6e 20 45 58 49 53 54 53 28  on is an EXISTS(
294dd 2e 2e 2e 29 20 65 78 70 72 65 73 73 69 6f 6e 2c  ...) expression,
294de 20 74 68 65 20 61 63 74 75 61 6c 0a 20 20 20 20   the actual.    
294df 2a 2a 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e  ** values return
294e0 65 64 20 62 79 20 74 68 65 20 53 45 4c 45 43 54  ed by the SELECT
294e1 20 61 72 65 20 6e 6f 74 20 72 65 71 75 69 72 65   are not require
294e2 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  d..    */.    sq
294e3 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 43 6c  lite3ExprCacheCl
294e4 65 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20 20  ear(pParse);.   
294e5 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
294e6 45 78 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c  ExprList(pParse,
294e7 20 70 45 4c 69 73 74 2c 20 72 65 67 52 65 73 75   pEList, regResu
294e8 6c 74 2c 20 65 44 65 73 74 3d 3d 53 52 54 5f 4f  lt, eDest==SRT_O
294e9 75 74 70 75 74 29 3b 0a 20 20 7d 0a 20 20 6e 43  utput);.  }.  nC
294ea 6f 6c 75 6d 6e 20 3d 20 6e 52 65 73 75 6c 74 43  olumn = nResultC
294eb 6f 6c 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65  ol;..  /* If the
294ec 20 44 49 53 54 49 4e 43 54 20 6b 65 79 77 6f 72   DISTINCT keywor
294ed 64 20 77 61 73 20 70 72 65 73 65 6e 74 20 6f 6e  d was present on
294ee 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   the SELECT stat
294ef 65 6d 65 6e 74 0a 20 20 2a 2a 20 61 6e 64 20 74  ement.  ** and t
294f0 68 69 73 20 72 6f 77 20 68 61 73 20 62 65 65 6e  his row has been
294f1 20 73 65 65 6e 20 62 65 66 6f 72 65 2c 20 74 68   seen before, th
294f2 65 6e 20 64 6f 20 6e 6f 74 20 6d 61 6b 65 20 74  en do not make t
294f3 68 69 73 20 72 6f 77 0a 20 20 2a 2a 20 70 61 72  his row.  ** par
294f4 74 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 2e  t of the result.
294f5 0a 20 20 2a 2f 0a 20 20 69 66 28 20 68 61 73 44  .  */.  if( hasD
294f6 69 73 74 69 6e 63 74 20 29 7b 0a 20 20 20 20 61  istinct ){.    a
294f7 73 73 65 72 74 28 20 70 45 4c 69 73 74 21 3d 30  ssert( pEList!=0
294f8 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
294f9 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 6e  pEList->nExpr==n
294fa 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 20 20 63 6f  Column );.    co
294fb 64 65 44 69 73 74 69 6e 63 74 28 70 50 61 72 73  deDistinct(pPars
294fc 65 2c 20 64 69 73 74 69 6e 63 74 2c 20 69 43 6f  e, distinct, iCo
294fd 6e 74 69 6e 75 65 2c 20 6e 43 6f 6c 75 6d 6e 2c  ntinue, nColumn,
294fe 20 72 65 67 52 65 73 75 6c 74 29 3b 0a 20 20 20   regResult);.   
294ff 20 69 66 28 20 70 4f 72 64 65 72 42 79 3d 3d 30   if( pOrderBy==0
29500 20 29 7b 0a 20 20 20 20 20 20 63 6f 64 65 4f 66   ){.      codeOf
29501 66 73 65 74 28 76 2c 20 70 2c 20 69 43 6f 6e 74  fset(v, p, iCont
29502 69 6e 75 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  inue);.    }.  }
29503 0a 0a 20 20 69 66 28 20 63 68 65 63 6b 46 6f 72  ..  if( checkFor
29504 4d 75 6c 74 69 43 6f 6c 75 6d 6e 53 65 6c 65 63  MultiColumnSelec
29505 74 45 72 72 6f 72 28 70 50 61 72 73 65 2c 20 70  tError(pParse, p
29506 44 65 73 74 2c 20 70 45 4c 69 73 74 2d 3e 6e 45  Dest, pEList->nE
29507 78 70 72 29 20 29 7b 0a 20 20 20 20 72 65 74 75  xpr) ){.    retu
29508 72 6e 3b 0a 20 20 7d 0a 0a 20 20 73 77 69 74 63  rn;.  }..  switc
29509 68 28 20 65 44 65 73 74 20 29 7b 0a 20 20 20 20  h( eDest ){.    
2950a 2f 2a 20 49 6e 20 74 68 69 73 20 6d 6f 64 65 2c  /* In this mode,
2950b 20 77 72 69 74 65 20 65 61 63 68 20 71 75 65 72   write each quer
2950c 79 20 72 65 73 75 6c 74 20 74 6f 20 74 68 65 20  y result to the 
2950d 6b 65 79 20 6f 66 20 74 68 65 20 74 65 6d 70 6f  key of the tempo
2950e 72 61 72 79 0a 20 20 20 20 2a 2a 20 74 61 62 6c  rary.    ** tabl
2950f 65 20 69 50 61 72 6d 2e 0a 20 20 20 20 2a 2f 0a  e iParm..    */.
29510 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
29511 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c  MIT_COMPOUND_SEL
29512 45 43 54 0a 20 20 20 20 63 61 73 65 20 53 52 54  ECT.    case SRT
29513 5f 55 6e 69 6f 6e 3a 20 7b 0a 20 20 20 20 20 20  _Union: {.      
29514 69 6e 74 20 72 31 3b 0a 20 20 20 20 20 20 72 31  int r1;.      r1
29515 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
29516 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
29517 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29518 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65  ddOp3(v, OP_Make
29519 52 65 63 6f 72 64 2c 20 72 65 67 52 65 73 75 6c  Record, regResul
2951a 74 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 72 31 29 3b  t, nColumn, r1);
2951b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2951c 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2951d 64 78 49 6e 73 65 72 74 2c 20 69 50 61 72 6d 2c  dxInsert, iParm,
2951e 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69   r1);.      sqli
2951f 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
29520 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20  g(pParse, r1);. 
29521 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
29522 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 6e 73 74 72  }..    /* Constr
29523 75 63 74 20 61 20 72 65 63 6f 72 64 20 66 72 6f  uct a record fro
29524 6d 20 74 68 65 20 71 75 65 72 79 20 72 65 73 75  m the query resu
29525 6c 74 2c 20 62 75 74 20 69 6e 73 74 65 61 64 20  lt, but instead 
29526 6f 66 0a 20 20 20 20 2a 2a 20 73 61 76 69 6e 67  of.    ** saving
29527 20 74 68 61 74 20 72 65 63 6f 72 64 2c 20 75 73   that record, us
29528 65 20 69 74 20 61 73 20 61 20 6b 65 79 20 74 6f  e it as a key to
29529 20 64 65 6c 65 74 65 20 65 6c 65 6d 65 6e 74 73   delete elements
2952a 20 66 72 6f 6d 0a 20 20 20 20 2a 2a 20 74 68 65   from.    ** the
2952b 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
2952c 20 69 50 61 72 6d 2e 0a 20 20 20 20 2a 2f 0a 20   iParm..    */. 
2952d 20 20 20 63 61 73 65 20 53 52 54 5f 45 78 63 65     case SRT_Exce
2952e 70 74 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69  pt: {.      sqli
2952f 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
29530 20 4f 50 5f 49 64 78 44 65 6c 65 74 65 2c 20 69   OP_IdxDelete, i
29531 50 61 72 6d 2c 20 72 65 67 52 65 73 75 6c 74 2c  Parm, regResult,
29532 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20   nColumn);.     
29533 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65   break;.    }.#e
29534 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 53 74 6f  ndif..    /* Sto
29535 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 61 73  re the result as
29536 20 64 61 74 61 20 75 73 69 6e 67 20 61 20 75 6e   data using a un
29537 69 71 75 65 20 6b 65 79 2e 0a 20 20 20 20 2a 2f  ique key..    */
29538 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 54 61  .    case SRT_Ta
29539 62 6c 65 3a 0a 20 20 20 20 63 61 73 65 20 53 52  ble:.    case SR
2953a 54 5f 45 70 68 65 6d 54 61 62 3a 20 7b 0a 20 20  T_EphemTab: {.  
2953b 20 20 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c      int r1 = sql
2953c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
2953d 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 74 65  Parse);.      te
2953e 73 74 63 61 73 65 28 20 65 44 65 73 74 3d 3d 53  stcase( eDest==S
2953f 52 54 5f 54 61 62 6c 65 20 29 3b 0a 20 20 20 20  RT_Table );.    
29540 20 20 74 65 73 74 63 61 73 65 28 20 65 44 65 73    testcase( eDes
29541 74 3d 3d 53 52 54 5f 45 70 68 65 6d 54 61 62 20  t==SRT_EphemTab 
29542 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
29543 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
29544 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67  _MakeRecord, reg
29545 52 65 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 2c  Result, nColumn,
29546 20 72 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20   r1);.      if( 
29547 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20  pOrderBy ){.    
29548 20 20 20 20 70 75 73 68 4f 6e 74 6f 53 6f 72 74      pushOntoSort
29549 65 72 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65  er(pParse, pOrde
2954a 72 42 79 2c 20 70 2c 20 72 31 29 3b 0a 20 20 20  rBy, p, r1);.   
2954b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2954c 20 20 69 6e 74 20 72 32 20 3d 20 73 71 6c 69 74    int r2 = sqlit
2954d 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
2954e 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71  rse);.        sq
2954f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
29550 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20  v, OP_NewRowid, 
29551 69 50 61 72 6d 2c 20 72 32 29 3b 0a 20 20 20 20  iParm, r2);.    
29552 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29553 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65  ddOp3(v, OP_Inse
29554 72 74 2c 20 69 50 61 72 6d 2c 20 72 31 2c 20 72  rt, iParm, r1, r
29555 32 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  2);.        sqli
29556 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28  te3VdbeChangeP5(
29557 76 2c 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44  v, OPFLAG_APPEND
29558 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
29559 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
2955a 28 70 50 61 72 73 65 2c 20 72 32 29 3b 0a 20 20  (pParse, r2);.  
2955b 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
2955c 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
2955d 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20  g(pParse, r1);. 
2955e 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2955f 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
29560 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a  E_OMIT_SUBQUERY.
29561 20 20 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65      /* If we are
29562 20 63 72 65 61 74 69 6e 67 20 61 20 73 65 74 20   creating a set 
29563 66 6f 72 20 61 6e 20 22 65 78 70 72 20 49 4e 20  for an "expr IN 
29564 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22 20 63 6f  (SELECT ...)" co
29565 6e 73 74 72 75 63 74 2c 0a 20 20 20 20 2a 2a 20  nstruct,.    ** 
29566 74 68 65 6e 20 74 68 65 72 65 20 73 68 6f 75 6c  then there shoul
29567 64 20 62 65 20 61 20 73 69 6e 67 6c 65 20 69 74  d be a single it
29568 65 6d 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 2e  em on the stack.
29569 20 20 57 72 69 74 65 20 74 68 69 73 0a 20 20 20    Write this.   
2956a 20 2a 2a 20 69 74 65 6d 20 69 6e 74 6f 20 74 68   ** item into th
2956b 65 20 73 65 74 20 74 61 62 6c 65 20 77 69 74 68  e set table with
2956c 20 62 6f 67 75 73 20 64 61 74 61 2e 0a 20 20 20   bogus data..   
2956d 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54   */.    case SRT
2956e 5f 53 65 74 3a 20 7b 0a 20 20 20 20 20 20 61 73  _Set: {.      as
2956f 73 65 72 74 28 20 6e 43 6f 6c 75 6d 6e 3d 3d 31  sert( nColumn==1
29570 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 66 66   );.      p->aff
29571 69 6e 69 74 79 20 3d 20 73 71 6c 69 74 65 33 43  inity = sqlite3C
29572 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 70  ompareAffinity(p
29573 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70  EList->a[0].pExp
29574 72 2c 20 70 44 65 73 74 2d 3e 61 66 66 69 6e 69  r, pDest->affini
29575 74 79 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  ty);.      if( p
29576 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 20  OrderBy ){.     
29577 20 20 20 2f 2a 20 41 74 20 66 69 72 73 74 20 67     /* At first g
29578 6c 61 6e 63 65 20 79 6f 75 20 77 6f 75 6c 64 20  lance you would 
29579 74 68 69 6e 6b 20 77 65 20 63 6f 75 6c 64 20 6f  think we could o
2957a 70 74 69 6d 69 7a 65 20 6f 75 74 20 74 68 65 0a  ptimize out the.
2957b 20 20 20 20 20 20 20 20 2a 2a 20 4f 52 44 45 52          ** ORDER
2957c 20 42 59 20 69 6e 20 74 68 69 73 20 63 61 73 65   BY in this case
2957d 20 73 69 6e 63 65 20 74 68 65 20 6f 72 64 65 72   since the order
2957e 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74   of entries in t
2957f 68 65 20 73 65 74 0a 20 20 20 20 20 20 20 20 2a  he set.        *
29580 2a 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 74 65  * does not matte
29581 72 2e 20 20 42 75 74 20 74 68 65 72 65 20 6d 69  r.  But there mi
29582 67 68 74 20 62 65 20 61 20 4c 49 4d 49 54 20 63  ght be a LIMIT c
29583 6c 61 75 73 65 2c 20 69 6e 20 77 68 69 63 68 0a  lause, in which.
29584 20 20 20 20 20 20 20 20 2a 2a 20 63 61 73 65 20          ** case 
29585 74 68 65 20 6f 72 64 65 72 20 64 6f 65 73 20 6d  the order does m
29586 61 74 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 20  atter */.       
29587 20 70 75 73 68 4f 6e 74 6f 53 6f 72 74 65 72 28   pushOntoSorter(
29588 70 50 61 72 73 65 2c 20 70 4f 72 64 65 72 42 79  pParse, pOrderBy
29589 2c 20 70 2c 20 72 65 67 52 65 73 75 6c 74 29 3b  , p, regResult);
2958a 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
2958b 20 20 20 20 20 20 69 6e 74 20 72 31 20 3d 20 73        int r1 = s
2958c 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
2958d 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
2958e 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2958f 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65  Op4(v, OP_MakeRe
29590 63 6f 72 64 2c 20 72 65 67 52 65 73 75 6c 74 2c  cord, regResult,
29591 20 31 2c 20 72 31 2c 20 26 70 2d 3e 61 66 66 69   1, r1, &p->affi
29592 6e 69 74 79 2c 20 31 29 3b 0a 20 20 20 20 20 20  nity, 1);.      
29593 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
29594 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65  heAffinityChange
29595 28 70 50 61 72 73 65 2c 20 72 65 67 52 65 73 75  (pParse, regResu
29596 6c 74 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20  lt, 1);.        
29597 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
29598 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72  2(v, OP_IdxInser
29599 74 2c 20 69 50 61 72 6d 2c 20 72 31 29 3b 0a 20  t, iParm, r1);. 
2959a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65         sqlite3Re
2959b 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
2959c 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 20  rse, r1);.      
2959d 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
2959e 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20     }..    /* If 
2959f 61 6e 79 20 72 6f 77 20 65 78 69 73 74 20 69 6e  any row exist in
295a0 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2c   the result set,
295a1 20 72 65 63 6f 72 64 20 74 68 61 74 20 66 61 63   record that fac
295a2 74 20 61 6e 64 20 61 62 6f 72 74 2e 0a 20 20 20  t and abort..   
295a3 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54   */.    case SRT
295a4 5f 45 78 69 73 74 73 3a 20 7b 0a 20 20 20 20 20  _Exists: {.     
295a5 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
295a6 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
295a7 2c 20 31 2c 20 69 50 61 72 6d 29 3b 0a 20 20 20  , 1, iParm);.   
295a8 20 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20     /* The LIMIT 
295a9 63 6c 61 75 73 65 20 77 69 6c 6c 20 74 65 72 6d  clause will term
295aa 69 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70 20 66  inate the loop f
295ab 6f 72 20 75 73 20 2a 2f 0a 20 20 20 20 20 20 62  or us */.      b
295ac 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  reak;.    }..   
295ad 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61   /* If this is a
295ae 20 73 63 61 6c 61 72 20 73 65 6c 65 63 74 20 74   scalar select t
295af 68 61 74 20 69 73 20 70 61 72 74 20 6f 66 20 61  hat is part of a
295b0 6e 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 74 68  n expression, th
295b1 65 6e 0a 20 20 20 20 2a 2a 20 73 74 6f 72 65 20  en.    ** store 
295b2 74 68 65 20 72 65 73 75 6c 74 73 20 69 6e 20 74  the results in t
295b3 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 6d  he appropriate m
295b4 65 6d 6f 72 79 20 63 65 6c 6c 20 61 6e 64 20 62  emory cell and b
295b5 72 65 61 6b 20 6f 75 74 0a 20 20 20 20 2a 2a 20  reak out.    ** 
295b6 6f 66 20 74 68 65 20 73 63 61 6e 20 6c 6f 6f 70  of the scan loop
295b7 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73  ..    */.    cas
295b8 65 20 53 52 54 5f 4d 65 6d 3a 20 7b 0a 20 20 20  e SRT_Mem: {.   
295b9 20 20 20 61 73 73 65 72 74 28 20 6e 43 6f 6c 75     assert( nColu
295ba 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 69  mn==1 );.      i
295bb 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b 0a 20  f( pOrderBy ){. 
295bc 20 20 20 20 20 20 20 70 75 73 68 4f 6e 74 6f 53         pushOntoS
295bd 6f 72 74 65 72 28 70 50 61 72 73 65 2c 20 70 4f  orter(pParse, pO
295be 72 64 65 72 42 79 2c 20 70 2c 20 72 65 67 52 65  rderBy, p, regRe
295bf 73 75 6c 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c  sult);.      }el
295c0 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  se{.        sqli
295c1 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 28  te3ExprCodeMove(
295c2 70 50 61 72 73 65 2c 20 72 65 67 52 65 73 75 6c  pParse, regResul
295c3 74 2c 20 69 50 61 72 6d 2c 20 31 29 3b 0a 20 20  t, iParm, 1);.  
295c4 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4c 49 4d        /* The LIM
295c5 49 54 20 63 6c 61 75 73 65 20 77 69 6c 6c 20 6a  IT clause will j
295c6 75 6d 70 20 6f 75 74 20 6f 66 20 74 68 65 20 6c  ump out of the l
295c7 6f 6f 70 20 66 6f 72 20 75 73 20 2a 2f 0a 20 20  oop for us */.  
295c8 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
295c9 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20  k;.    }.#endif 
295ca 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /* #ifndef SQLIT
295cb 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20  E_OMIT_SUBQUERY 
295cc 2a 2f 0a 0a 20 20 20 20 2f 2a 20 53 65 6e 64 20  */..    /* Send 
295cd 74 68 65 20 64 61 74 61 20 74 6f 20 74 68 65 20  the data to the 
295ce 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f  callback functio
295cf 6e 20 6f 72 20 74 6f 20 61 20 73 75 62 72 6f 75  n or to a subrou
295d0 74 69 6e 65 2e 20 20 49 6e 20 74 68 65 0a 20 20  tine.  In the.  
295d1 20 20 2a 2a 20 63 61 73 65 20 6f 66 20 61 20 73    ** case of a s
295d2 75 62 72 6f 75 74 69 6e 65 2c 20 74 68 65 20 73  ubroutine, the s
295d3 75 62 72 6f 75 74 69 6e 65 20 69 74 73 65 6c 66  ubroutine itself
295d4 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20   is responsible 
295d5 66 6f 72 0a 20 20 20 20 2a 2a 20 70 6f 70 70 69  for.    ** poppi
295d6 6e 67 20 74 68 65 20 64 61 74 61 20 66 72 6f 6d  ng the data from
295d7 20 74 68 65 20 73 74 61 63 6b 2e 0a 20 20 20 20   the stack..    
295d8 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f  */.    case SRT_
295d9 43 6f 72 6f 75 74 69 6e 65 3a 0a 20 20 20 20 63  Coroutine:.    c
295da 61 73 65 20 53 52 54 5f 4f 75 74 70 75 74 3a 20  ase SRT_Output: 
295db 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  {.      testcase
295dc 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 43 6f 72  ( eDest==SRT_Cor
295dd 6f 75 74 69 6e 65 20 29 3b 0a 20 20 20 20 20 20  outine );.      
295de 74 65 73 74 63 61 73 65 28 20 65 44 65 73 74 3d  testcase( eDest=
295df 3d 53 52 54 5f 4f 75 74 70 75 74 20 29 3b 0a 20  =SRT_Output );. 
295e0 20 20 20 20 20 69 66 28 20 70 4f 72 64 65 72 42       if( pOrderB
295e1 79 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  y ){.        int
295e2 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74   r1 = sqlite3Get
295e3 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
295e4 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
295e5 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
295e6 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67  _MakeRecord, reg
295e7 52 65 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 2c  Result, nColumn,
295e8 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20 70 75   r1);.        pu
295e9 73 68 4f 6e 74 6f 53 6f 72 74 65 72 28 70 50 61  shOntoSorter(pPa
295ea 72 73 65 2c 20 70 4f 72 64 65 72 42 79 2c 20 70  rse, pOrderBy, p
295eb 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20 73  , r1);.        s
295ec 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
295ed 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29  pReg(pParse, r1)
295ee 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
295ef 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 43 6f 72  ( eDest==SRT_Cor
295f0 6f 75 74 69 6e 65 20 29 7b 0a 20 20 20 20 20 20  outine ){.      
295f1 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
295f2 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c  Op1(v, OP_Yield,
295f3 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 29 3b 0a   pDest->iParm);.
295f4 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
295f5 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
295f6 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73  AddOp2(v, OP_Res
295f7 75 6c 74 52 6f 77 2c 20 72 65 67 52 65 73 75 6c  ultRow, regResul
295f8 74 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20  t, nColumn);.   
295f9 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
295fa 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61  CacheAffinityCha
295fb 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 52  nge(pParse, regR
295fc 65 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 29 3b  esult, nColumn);
295fd 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
295fe 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66  reak;.    }..#if
295ff 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
29600 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a 20  _OMIT_TRIGGER). 
29601 20 20 20 2f 2a 20 44 69 73 63 61 72 64 20 74 68     /* Discard th
29602 65 20 72 65 73 75 6c 74 73 2e 20 20 54 68 69 73  e results.  This
29603 20 69 73 20 75 73 65 64 20 66 6f 72 20 53 45 4c   is used for SEL
29604 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 69  ECT statements i
29605 6e 73 69 64 65 0a 20 20 20 20 2a 2a 20 74 68 65  nside.    ** the
29606 20 62 6f 64 79 20 6f 66 20 61 20 54 52 49 47 47   body of a TRIGG
29607 45 52 2e 20 20 54 68 65 20 70 75 72 70 6f 73 65  ER.  The purpose
29608 20 6f 66 20 73 75 63 68 20 73 65 6c 65 63 74 73   of such selects
29609 20 69 73 20 74 6f 20 63 61 6c 6c 0a 20 20 20 20   is to call.    
2960a 2a 2a 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20  ** user-defined 
2960b 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 68  functions that h
2960c 61 76 65 20 73 69 64 65 20 65 66 66 65 63 74 73  ave side effects
2960d 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20 63 61 72  .  We do not car
2960e 65 0a 20 20 20 20 2a 2a 20 61 62 6f 75 74 20 74  e.    ** about t
2960f 68 65 20 61 63 74 75 61 6c 20 72 65 73 75 6c 74  he actual result
29610 73 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74 2e  s of the select.
29611 0a 20 20 20 20 2a 2f 0a 20 20 20 20 64 65 66 61  .    */.    defa
29612 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73  ult: {.      ass
29613 65 72 74 28 20 65 44 65 73 74 3d 3d 53 52 54 5f  ert( eDest==SRT_
29614 44 69 73 63 61 72 64 20 29 3b 0a 20 20 20 20 20  Discard );.     
29615 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65   break;.    }.#e
29616 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4a  ndif.  }..  /* J
29617 75 6d 70 20 74 6f 20 74 68 65 20 65 6e 64 20 6f  ump to the end o
29618 66 20 74 68 65 20 6c 6f 6f 70 20 69 66 20 74 68  f the loop if th
29619 65 20 4c 49 4d 49 54 20 69 73 20 72 65 61 63 68  e LIMIT is reach
2961a 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  ed..  */.  if( p
2961b 2d 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20  ->iLimit ){.    
2961c 61 73 73 65 72 74 28 20 70 4f 72 64 65 72 42 79  assert( pOrderBy
2961d 3d 3d 30 20 29 3b 20 20 2f 2a 20 49 66 20 74 68  ==0 );  /* If th
2961e 65 72 65 20 69 73 20 61 6e 20 4f 52 44 45 52 20  ere is an ORDER 
2961f 42 59 2c 20 74 68 65 20 63 61 6c 6c 20 74 6f 0a  BY, the call to.
29620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29621 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70              ** p
29622 75 73 68 4f 6e 74 6f 53 6f 72 74 65 72 28 29 20  ushOntoSorter() 
29623 77 6f 75 6c 64 20 68 61 76 65 20 63 6c 65 61 72  would have clear
29624 65 64 20 70 2d 3e 69 4c 69 6d 69 74 20 2a 2f 0a  ed p->iLimit */.
29625 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29626 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49  ddOp2(v, OP_AddI
29627 6d 6d 2c 20 70 2d 3e 69 4c 69 6d 69 74 2c 20 2d  mm, p->iLimit, -
29628 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
29629 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2962a 49 66 5a 65 72 6f 2c 20 70 2d 3e 69 4c 69 6d 69  IfZero, p->iLimi
2962b 74 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 7d 0a  t, iBreak);.  }.
2962c 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61  }../*.** Given a
2962d 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  n expression lis
2962e 74 2c 20 67 65 6e 65 72 61 74 65 20 61 20 4b 65  t, generate a Ke
2962f 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  yInfo structure 
29630 74 68 61 74 20 72 65 63 6f 72 64 73 0a 2a 2a 20  that records.** 
29631 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  the collating se
29632 71 75 65 6e 63 65 20 66 6f 72 20 65 61 63 68 20  quence for each 
29633 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68  expression in th
29634 61 74 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69  at expression li
29635 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  st..**.** If the
29636 20 45 78 70 72 4c 69 73 74 20 69 73 20 61 6e 20   ExprList is an 
29637 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55  ORDER BY or GROU
29638 50 20 42 59 20 63 6c 61 75 73 65 20 74 68 65 6e  P BY clause then
29639 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 0a 2a   the resulting.*
2963a 2a 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74  * KeyInfo struct
2963b 75 72 65 20 69 73 20 61 70 70 72 6f 70 72 69 61  ure is appropria
2963c 74 65 20 66 6f 72 20 69 6e 69 74 69 61 6c 69 7a  te for initializ
2963d 69 6e 67 20 61 20 76 69 72 74 75 61 6c 20 69 6e  ing a virtual in
2963e 64 65 78 20 74 6f 0a 2a 2a 20 69 6d 70 6c 65 6d  dex to.** implem
2963f 65 6e 74 20 74 68 61 74 20 63 6c 61 75 73 65 2e  ent that clause.
29640 20 20 49 66 20 74 68 65 20 45 78 70 72 4c 69 73    If the ExprLis
29641 74 20 69 73 20 74 68 65 20 72 65 73 75 6c 74 20  t is the result 
29642 73 65 74 20 6f 66 20 61 20 53 45 4c 45 43 54 0a  set of a SELECT.
29643 2a 2a 20 74 68 65 6e 20 74 68 65 20 4b 65 79 49  ** then the KeyI
29644 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 69 73  nfo structure is
29645 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72   appropriate for
29646 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 61 20   initializing a 
29647 76 69 72 74 75 61 6c 0a 2a 2a 20 69 6e 64 65 78  virtual.** index
29648 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20   to implement a 
29649 44 49 53 54 49 4e 43 54 20 74 65 73 74 2e 0a 2a  DISTINCT test..*
2964a 2a 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 6f  *.** Space to ho
2964b 6c 64 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73  ld the KeyInfo s
2964c 74 72 75 63 74 75 72 65 20 69 73 20 6f 62 74 61  tructure is obta
2964d 69 6e 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 20  in from malloc. 
2964e 20 54 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20   The calling.** 
2964f 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 73 70  function is resp
29650 6f 6e 73 69 62 6c 65 20 66 6f 72 20 73 65 65 69  onsible for seei
29651 6e 67 20 74 68 61 74 20 74 68 69 73 20 73 74 72  ng that this str
29652 75 63 74 75 72 65 20 69 73 20 65 76 65 6e 74 75  ucture is eventu
29653 61 6c 6c 79 0a 2a 2a 20 66 72 65 65 64 2e 20 20  ally.** freed.  
29654 41 64 64 20 74 68 65 20 4b 65 79 49 6e 66 6f 20  Add the KeyInfo 
29655 73 74 72 75 63 74 75 72 65 20 74 6f 20 74 68 65  structure to the
29656 20 50 34 20 66 69 65 6c 64 20 6f 66 20 61 6e 20   P4 field of an 
29657 6f 70 63 6f 64 65 20 75 73 69 6e 67 0a 2a 2a 20  opcode using.** 
29658 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f  P4_KEYINFO_HANDO
29659 46 46 20 69 73 20 74 68 65 20 75 73 75 61 6c 20  FF is the usual 
2965a 77 61 79 20 6f 66 20 64 65 61 6c 69 6e 67 20 77  way of dealing w
2965b 69 74 68 20 74 68 69 73 2e 0a 2a 2f 0a 73 74 61  ith this..*/.sta
2965c 74 69 63 20 4b 65 79 49 6e 66 6f 20 2a 6b 65 79  tic KeyInfo *key
2965d 49 6e 66 6f 46 72 6f 6d 45 78 70 72 4c 69 73 74  InfoFromExprList
2965e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
2965f 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 29  ExprList *pList)
29660 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  {.  sqlite3 *db 
29661 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
29662 69 6e 74 20 6e 45 78 70 72 3b 0a 20 20 4b 65 79  int nExpr;.  Key
29663 49 6e 66 6f 20 2a 70 49 6e 66 6f 3b 0a 20 20 73  Info *pInfo;.  s
29664 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
29665 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 6e  tem *pItem;.  in
29666 74 20 69 3b 0a 0a 20 20 6e 45 78 70 72 20 3d 20  t i;..  nExpr = 
29667 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20  pList->nExpr;.  
29668 70 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 44  pInfo = sqlite3D
29669 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
2966a 73 69 7a 65 6f 66 28 2a 70 49 6e 66 6f 29 20 2b  sizeof(*pInfo) +
2966b 20 6e 45 78 70 72 2a 28 73 69 7a 65 6f 66 28 43   nExpr*(sizeof(C
2966c 6f 6c 6c 53 65 71 2a 29 2b 31 29 20 29 3b 0a 20  ollSeq*)+1) );. 
2966d 20 69 66 28 20 70 49 6e 66 6f 20 29 7b 0a 20 20   if( pInfo ){.  
2966e 20 20 70 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72    pInfo->aSortOr
2966f 64 65 72 20 3d 20 28 75 38 2a 29 26 70 49 6e 66  der = (u8*)&pInf
29670 6f 2d 3e 61 43 6f 6c 6c 5b 6e 45 78 70 72 5d 3b  o->aColl[nExpr];
29671 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 46 69 65  .    pInfo->nFie
29672 6c 64 20 3d 20 28 75 31 36 29 6e 45 78 70 72 3b  ld = (u16)nExpr;
29673 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 65 6e 63 20  .    pInfo->enc 
29674 3d 20 45 4e 43 28 64 62 29 3b 0a 20 20 20 20 70  = ENC(db);.    p
29675 49 6e 66 6f 2d 3e 64 62 20 3d 20 64 62 3b 0a 20  Info->db = db;. 
29676 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65     for(i=0, pIte
29677 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3c 6e 45  m=pList->a; i<nE
29678 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b  xpr; i++, pItem+
29679 2b 29 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65  +){.      CollSe
2967a 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20  q *pColl;.      
2967b 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45  pColl = sqlite3E
2967c 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73  xprCollSeq(pPars
2967d 65 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29  e, pItem->pExpr)
2967e 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 43 6f  ;.      if( !pCo
2967f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43  ll ){.        pC
29680 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43  oll = db->pDfltC
29681 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  oll;.      }.   
29682 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b     pInfo->aColl[
29683 69 5d 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20  i] = pColl;.    
29684 20 20 70 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72    pInfo->aSortOr
29685 64 65 72 5b 69 5d 20 3d 20 70 49 74 65 6d 2d 3e  der[i] = pItem->
29686 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 7d  sortOrder;.    }
29687 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 49  .  }.  return pI
29688 6e 66 6f 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49  nfo;.}.../*.** I
29689 66 20 74 68 65 20 69 6e 6e 65 72 20 6c 6f 6f 70  f the inner loop
2968a 20 77 61 73 20 67 65 6e 65 72 61 74 65 64 20 75   was generated u
2968b 73 69 6e 67 20 61 20 6e 6f 6e 2d 6e 75 6c 6c 20  sing a non-null 
2968c 70 4f 72 64 65 72 42 79 20 61 72 67 75 6d 65 6e  pOrderBy argumen
2968d 74 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 72  t,.** then the r
2968e 65 73 75 6c 74 73 20 77 65 72 65 20 70 6c 61 63  esults were plac
2968f 65 64 20 69 6e 20 61 20 73 6f 72 74 65 72 2e 20  ed in a sorter. 
29690 20 41 66 74 65 72 20 74 68 65 20 6c 6f 6f 70 20   After the loop 
29691 69 73 20 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a  is terminated.**
29692 20 77 65 20 6e 65 65 64 20 74 6f 20 72 75 6e 20   we need to run 
29693 74 68 65 20 73 6f 72 74 65 72 20 61 6e 64 20 6f  the sorter and o
29694 75 74 70 75 74 20 74 68 65 20 72 65 73 75 6c 74  utput the result
29695 73 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  s.  The followin
29696 67 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 67 65 6e  g.** routine gen
29697 65 72 61 74 65 73 20 74 68 65 20 63 6f 64 65 20  erates the code 
29698 6e 65 65 64 65 64 20 74 6f 20 64 6f 20 74 68 61  needed to do tha
29699 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  t..*/.static voi
2969a 64 20 67 65 6e 65 72 61 74 65 53 6f 72 74 54 61  d generateSortTa
2969b 69 6c 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  il(.  Parse *pPa
2969c 72 73 65 2c 20 20 20 20 2f 2a 20 50 61 72 73 69  rse,    /* Parsi
2969d 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
2969e 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20  Select *p,      
2969f 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20    /* The SELECT 
296a0 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 56  statement */.  V
296a1 64 62 65 20 2a 76 2c 20 20 20 20 20 20 20 20 20  dbe *v,         
296a2 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64   /* Generate cod
296a3 65 20 69 6e 74 6f 20 74 68 69 73 20 56 44 42 45  e into this VDBE
296a4 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d   */.  int nColum
296a5 6e 2c 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  n,      /* Numbe
296a6 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20  r of columns of 
296a7 64 61 74 61 20 2a 2f 0a 20 20 53 65 6c 65 63 74  data */.  Select
296a8 44 65 73 74 20 2a 70 44 65 73 74 20 2f 2a 20 57  Dest *pDest /* W
296a9 72 69 74 65 20 74 68 65 20 73 6f 72 74 65 64 20  rite the sorted 
296aa 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a  results here */.
296ab 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 42 72 65  ){.  int addrBre
296ac 61 6b 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  ak = sqlite3Vdbe
296ad 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 20 20 20  MakeLabel(v);   
296ae 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74    /* Jump here t
296af 6f 20 65 78 69 74 20 6c 6f 6f 70 20 2a 2f 0a 20  o exit loop */. 
296b0 20 69 6e 74 20 61 64 64 72 43 6f 6e 74 69 6e 75   int addrContinu
296b1 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  e = sqlite3VdbeM
296b2 61 6b 65 4c 61 62 65 6c 28 76 29 3b 20 20 2f 2a  akeLabel(v);  /*
296b3 20 4a 75 6d 70 20 68 65 72 65 20 66 6f 72 20 6e   Jump here for n
296b4 65 78 74 20 63 79 63 6c 65 20 2a 2f 0a 20 20 69  ext cycle */.  i
296b5 6e 74 20 61 64 64 72 3b 0a 20 20 69 6e 74 20 69  nt addr;.  int i
296b6 54 61 62 3b 0a 20 20 69 6e 74 20 70 73 65 75 64  Tab;.  int pseud
296b7 6f 54 61 62 20 3d 20 30 3b 0a 20 20 45 78 70 72  oTab = 0;.  Expr
296b8 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 20 3d  List *pOrderBy =
296b9 20 70 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 0a 20   p->pOrderBy;.. 
296ba 20 69 6e 74 20 65 44 65 73 74 20 3d 20 70 44 65   int eDest = pDe
296bb 73 74 2d 3e 65 44 65 73 74 3b 0a 20 20 69 6e 74  st->eDest;.  int
296bc 20 69 50 61 72 6d 20 3d 20 70 44 65 73 74 2d 3e   iParm = pDest->
296bd 69 50 61 72 6d 3b 0a 0a 20 20 69 6e 74 20 72 65  iParm;..  int re
296be 67 52 6f 77 3b 0a 20 20 69 6e 74 20 72 65 67 52  gRow;.  int regR
296bf 6f 77 69 64 3b 0a 0a 20 20 69 54 61 62 20 3d 20  owid;..  iTab = 
296c0 70 4f 72 64 65 72 42 79 2d 3e 69 45 43 75 72 73  pOrderBy->iECurs
296c1 6f 72 3b 0a 20 20 72 65 67 52 6f 77 20 3d 20 73  or;.  regRow = s
296c2 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
296c3 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20  (pParse);.  if( 
296c4 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75  eDest==SRT_Outpu
296c5 74 20 7c 7c 20 65 44 65 73 74 3d 3d 53 52 54 5f  t || eDest==SRT_
296c6 43 6f 72 6f 75 74 69 6e 65 20 29 7b 0a 20 20 20  Coroutine ){.   
296c7 20 70 73 65 75 64 6f 54 61 62 20 3d 20 70 50 61   pseudoTab = pPa
296c8 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20  rse->nTab++;.   
296c9 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
296ca 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 50 73 65  p3(v, OP_OpenPse
296cb 75 64 6f 2c 20 70 73 65 75 64 6f 54 61 62 2c 20  udo, pseudoTab, 
296cc 72 65 67 52 6f 77 2c 20 6e 43 6f 6c 75 6d 6e 29  regRow, nColumn)
296cd 3b 0a 20 20 20 20 72 65 67 52 6f 77 69 64 20 3d  ;.    regRowid =
296ce 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
296cf 20 72 65 67 52 6f 77 69 64 20 3d 20 73 71 6c 69   regRowid = sqli
296d0 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
296d1 61 72 73 65 29 3b 0a 20 20 7d 0a 20 20 61 64 64  arse);.  }.  add
296d2 72 20 3d 20 31 20 2b 20 73 71 6c 69 74 65 33 56  r = 1 + sqlite3V
296d3 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
296d4 53 6f 72 74 2c 20 69 54 61 62 2c 20 61 64 64 72  Sort, iTab, addr
296d5 42 72 65 61 6b 29 3b 0a 20 20 63 6f 64 65 4f 66  Break);.  codeOf
296d6 66 73 65 74 28 76 2c 20 70 2c 20 61 64 64 72 43  fset(v, p, addrC
296d7 6f 6e 74 69 6e 75 65 29 3b 0a 20 20 73 71 6c 69  ontinue);.  sqli
296d8 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
296d9 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 54 61 62   OP_Column, iTab
296da 2c 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70  , pOrderBy->nExp
296db 72 20 2b 20 31 2c 20 72 65 67 52 6f 77 29 3b 0a  r + 1, regRow);.
296dc 20 20 73 77 69 74 63 68 28 20 65 44 65 73 74 20    switch( eDest 
296dd 29 7b 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f  ){.    case SRT_
296de 54 61 62 6c 65 3a 0a 20 20 20 20 63 61 73 65 20  Table:.    case 
296df 53 52 54 5f 45 70 68 65 6d 54 61 62 3a 20 7b 0a  SRT_EphemTab: {.
296e0 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
296e1 65 44 65 73 74 3d 3d 53 52 54 5f 54 61 62 6c 65  eDest==SRT_Table
296e2 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
296e3 73 65 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 45  se( eDest==SRT_E
296e4 70 68 65 6d 54 61 62 20 29 3b 0a 20 20 20 20 20  phemTab );.     
296e5 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
296e6 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69  p2(v, OP_NewRowi
296e7 64 2c 20 69 50 61 72 6d 2c 20 72 65 67 52 6f 77  d, iParm, regRow
296e8 69 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  id);.      sqlit
296e9 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
296ea 4f 50 5f 49 6e 73 65 72 74 2c 20 69 50 61 72 6d  OP_Insert, iParm
296eb 2c 20 72 65 67 52 6f 77 2c 20 72 65 67 52 6f 77  , regRow, regRow
296ec 69 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  id);.      sqlit
296ed 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76  e3VdbeChangeP5(v
296ee 2c 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29  , OPFLAG_APPEND)
296ef 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
296f0 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c     }.#ifndef SQL
296f1 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
296f2 59 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 53  Y.    case SRT_S
296f3 65 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65  et: {.      asse
296f4 72 74 28 20 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29  rt( nColumn==1 )
296f5 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
296f6 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
296f7 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 52  MakeRecord, regR
296f8 6f 77 2c 20 31 2c 20 72 65 67 52 6f 77 69 64 2c  ow, 1, regRowid,
296f9 20 26 70 2d 3e 61 66 66 69 6e 69 74 79 2c 20 31   &p->affinity, 1
296fa 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
296fb 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74  ExprCacheAffinit
296fc 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20  yChange(pParse, 
296fd 72 65 67 52 6f 77 2c 20 31 29 3b 0a 20 20 20 20  regRow, 1);.    
296fe 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
296ff 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73  Op2(v, OP_IdxIns
29700 65 72 74 2c 20 69 50 61 72 6d 2c 20 72 65 67 52  ert, iParm, regR
29701 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 62 72 65  owid);.      bre
29702 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
29703 73 65 20 53 52 54 5f 4d 65 6d 3a 20 7b 0a 20 20  se SRT_Mem: {.  
29704 20 20 20 20 61 73 73 65 72 74 28 20 6e 43 6f 6c      assert( nCol
29705 75 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20  umn==1 );.      
29706 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 4d  sqlite3ExprCodeM
29707 6f 76 65 28 70 50 61 72 73 65 2c 20 72 65 67 52  ove(pParse, regR
29708 6f 77 2c 20 69 50 61 72 6d 2c 20 31 29 3b 0a 20  ow, iParm, 1);. 
29709 20 20 20 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49       /* The LIMI
2970a 54 20 63 6c 61 75 73 65 20 77 69 6c 6c 20 74 65  T clause will te
2970b 72 6d 69 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70  rminate the loop
2970c 20 66 6f 72 20 75 73 20 2a 2f 0a 20 20 20 20 20   for us */.     
2970d 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65   break;.    }.#e
2970e 6e 64 69 66 0a 20 20 20 20 64 65 66 61 75 6c 74  ndif.    default
2970f 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b  : {.      int i;
29710 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 65  .      assert( e
29711 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74  Dest==SRT_Output
29712 20 7c 7c 20 65 44 65 73 74 3d 3d 53 52 54 5f 43   || eDest==SRT_C
29713 6f 72 6f 75 74 69 6e 65 20 29 3b 20 0a 20 20 20  oroutine ); .   
29714 20 20 20 74 65 73 74 63 61 73 65 28 20 65 44 65     testcase( eDe
29715 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 29  st==SRT_Output )
29716 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
29717 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 43 6f 72  ( eDest==SRT_Cor
29718 6f 75 74 69 6e 65 20 29 3b 0a 20 20 20 20 20 20  outine );.      
29719 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 75  for(i=0; i<nColu
2971a 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  mn; i++){.      
2971b 20 20 61 73 73 65 72 74 28 20 72 65 67 52 6f 77    assert( regRow
2971c 21 3d 70 44 65 73 74 2d 3e 69 4d 65 6d 2b 69 20  !=pDest->iMem+i 
2971d 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2971e 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2971f 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 70 73 65 75 64  OP_Column, pseud
29720 6f 54 61 62 2c 20 69 2c 20 70 44 65 73 74 2d 3e  oTab, i, pDest->
29721 69 4d 65 6d 2b 69 29 3b 0a 20 20 20 20 20 20 20  iMem+i);.       
29722 20 69 66 28 20 69 3d 3d 30 20 29 7b 0a 20 20 20   if( i==0 ){.   
29723 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
29724 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50  beChangeP5(v, OP
29725 46 4c 41 47 5f 43 4c 45 41 52 43 41 43 48 45 29  FLAG_CLEARCACHE)
29726 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
29727 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 65 44    }.      if( eD
29728 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20  est==SRT_Output 
29729 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
2972a 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2972b 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 70 44  OP_ResultRow, pD
2972c 65 73 74 2d 3e 69 4d 65 6d 2c 20 6e 43 6f 6c 75  est->iMem, nColu
2972d 6d 6e 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  mn);.        sql
2972e 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66  ite3ExprCacheAff
2972f 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72  inityChange(pPar
29730 73 65 2c 20 70 44 65 73 74 2d 3e 69 4d 65 6d 2c  se, pDest->iMem,
29731 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20   nColumn);.     
29732 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
29733 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
29734 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 70  1(v, OP_Yield, p
29735 44 65 73 74 2d 3e 69 50 61 72 6d 29 3b 0a 20 20  Dest->iParm);.  
29736 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
29737 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73  k;.    }.  }.  s
29738 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
29739 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67  pReg(pParse, reg
2973a 52 6f 77 29 3b 0a 20 20 73 71 6c 69 74 65 33 52  Row);.  sqlite3R
2973b 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
2973c 61 72 73 65 2c 20 72 65 67 52 6f 77 69 64 29 3b  arse, regRowid);
2973d 0a 0a 20 20 2f 2a 20 4c 49 4d 49 54 20 68 61 73  ..  /* LIMIT has
2973e 20 62 65 65 6e 20 69 6d 70 6c 65 6d 65 6e 74 65   been implemente
2973f 64 20 62 79 20 74 68 65 20 70 75 73 68 4f 6e 74  d by the pushOnt
29740 6f 53 6f 72 74 65 72 28 29 20 72 6f 75 74 69 6e  oSorter() routin
29741 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  e..  */.  assert
29742 28 20 70 2d 3e 69 4c 69 6d 69 74 3d 3d 30 20 29  ( p->iLimit==0 )
29743 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 62 6f 74 74  ;..  /* The bott
29744 6f 6d 20 6f 66 20 74 68 65 20 6c 6f 6f 70 0a 20  om of the loop. 
29745 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62   */.  sqlite3Vdb
29746 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
29747 20 61 64 64 72 43 6f 6e 74 69 6e 75 65 29 3b 0a   addrContinue);.
29748 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
29749 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20  Op2(v, OP_Next, 
2974a 69 54 61 62 2c 20 61 64 64 72 29 3b 0a 20 20 73  iTab, addr);.  s
2974b 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
2974c 65 4c 61 62 65 6c 28 76 2c 20 61 64 64 72 42 72  eLabel(v, addrBr
2974d 65 61 6b 29 3b 0a 20 20 69 66 28 20 65 44 65 73  eak);.  if( eDes
2974e 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 7c 7c  t==SRT_Output ||
2974f 20 65 44 65 73 74 3d 3d 53 52 54 5f 43 6f 72 6f   eDest==SRT_Coro
29750 75 74 69 6e 65 20 29 7b 0a 20 20 20 20 73 71 6c  utine ){.    sql
29751 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
29752 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 70 73 65 75  , OP_Close, pseu
29753 64 6f 54 61 62 2c 20 30 29 3b 0a 20 20 7d 0a 7d  doTab, 0);.  }.}
29754 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ../*.** Return a
29755 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74   pointer to a st
29756 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ring containing 
29757 74 68 65 20 27 64 65 63 6c 61 72 61 74 69 6f 6e  the 'declaration
29758 20 74 79 70 65 27 20 6f 66 20 74 68 65 0a 2a 2a   type' of the.**
29759 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70   expression pExp
2975a 72 2e 20 54 68 65 20 73 74 72 69 6e 67 20 6d 61  r. The string ma
2975b 79 20 62 65 20 74 72 65 61 74 65 64 20 61 73 20  y be treated as 
2975c 73 74 61 74 69 63 20 62 79 20 74 68 65 20 63 61  static by the ca
2975d 6c 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ller..**.** The 
2975e 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65  declaration type
2975f 20 69 73 20 74 68 65 20 65 78 61 63 74 20 64 61   is the exact da
29760 74 61 74 79 70 65 20 64 65 66 69 6e 69 74 69 6f  tatype definitio
29761 6e 20 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d  n extracted from
29762 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c   the.** original
29763 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74   CREATE TABLE st
29764 61 74 65 6d 65 6e 74 20 69 66 20 74 68 65 20 65  atement if the e
29765 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 63  xpression is a c
29766 6f 6c 75 6d 6e 2e 20 54 68 65 0a 2a 2a 20 64 65  olumn. The.** de
29767 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20 66  claration type f
29768 6f 72 20 61 20 52 4f 57 49 44 20 66 69 65 6c 64  or a ROWID field
29769 20 69 73 20 49 4e 54 45 47 45 52 2e 20 45 78 61   is INTEGER. Exa
2976a 63 74 6c 79 20 77 68 65 6e 20 61 6e 20 65 78 70  ctly when an exp
2976b 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 63 6f  ression.** is co
2976c 6e 73 69 64 65 72 65 64 20 61 20 63 6f 6c 75 6d  nsidered a colum
2976d 6e 20 63 61 6e 20 62 65 20 63 6f 6d 70 6c 65 78  n can be complex
2976e 20 69 6e 20 74 68 65 20 70 72 65 73 65 6e 63 65   in the presence
2976f 20 6f 66 20 73 75 62 71 75 65 72 69 65 73 2e 20   of subqueries. 
29770 54 68 65 0a 2a 2a 20 72 65 73 75 6c 74 2d 73 65  The.** result-se
29771 74 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20  t expression in 
29772 61 6c 6c 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  all of the follo
29773 77 69 6e 67 20 53 45 4c 45 43 54 20 73 74 61 74  wing SELECT stat
29774 65 6d 65 6e 74 73 20 69 73 20 0a 2a 2a 20 63 6f  ements is .** co
29775 6e 73 69 64 65 72 65 64 20 61 20 63 6f 6c 75 6d  nsidered a colum
29776 6e 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69  n by this functi
29777 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45  on..**.**   SELE
29778 43 54 20 63 6f 6c 20 46 52 4f 4d 20 74 62 6c 3b  CT col FROM tbl;
29779 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 28 53 45  .**   SELECT (SE
2977a 4c 45 43 54 20 63 6f 6c 20 46 52 4f 4d 20 74 62  LECT col FROM tb
2977b 6c 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 28  l;.**   SELECT (
2977c 53 45 4c 45 43 54 20 63 6f 6c 20 46 52 4f 4d 20  SELECT col FROM 
2977d 74 62 6c 29 3b 0a 2a 2a 20 20 20 53 45 4c 45 43  tbl);.**   SELEC
2977e 54 20 61 62 63 20 46 52 4f 4d 20 28 53 45 4c 45  T abc FROM (SELE
2977f 43 54 20 63 6f 6c 20 41 53 20 61 62 63 20 46 52  CT col AS abc FR
29780 4f 4d 20 74 62 6c 29 3b 0a 2a 2a 20 0a 2a 2a 20  OM tbl);.** .** 
29781 54 68 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 20  The declaration 
29782 74 79 70 65 20 66 6f 72 20 61 6e 79 20 65 78 70  type for any exp
29783 72 65 73 73 69 6f 6e 20 6f 74 68 65 72 20 74 68  ression other th
29784 61 6e 20 61 20 63 6f 6c 75 6d 6e 20 69 73 20 4e  an a column is N
29785 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  ULL..*/.static c
29786 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6c 75 6d  onst char *colum
29787 6e 54 79 70 65 28 0a 20 20 4e 61 6d 65 43 6f 6e  nType(.  NameCon
29788 74 65 78 74 20 2a 70 4e 43 2c 20 0a 20 20 45 78  text *pNC, .  Ex
29789 70 72 20 2a 70 45 78 70 72 2c 0a 20 20 63 6f 6e  pr *pExpr,.  con
2978a 73 74 20 63 68 61 72 20 2a 2a 70 7a 4f 72 69 67  st char **pzOrig
2978b 69 6e 44 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68  inDb,.  const ch
2978c 61 72 20 2a 2a 70 7a 4f 72 69 67 69 6e 54 61 62  ar **pzOriginTab
2978d 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
2978e 2a 70 7a 4f 72 69 67 69 6e 43 6f 6c 0a 29 7b 0a  *pzOriginCol.){.
2978f 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54    char const *zT
29790 79 70 65 20 3d 20 30 3b 0a 20 20 63 68 61 72 20  ype = 0;.  char 
29791 63 6f 6e 73 74 20 2a 7a 4f 72 69 67 69 6e 44 62  const *zOriginDb
29792 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 63 6f 6e   = 0;.  char con
29793 73 74 20 2a 7a 4f 72 69 67 69 6e 54 61 62 20 3d  st *zOriginTab =
29794 20 30 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74   0;.  char const
29795 20 2a 7a 4f 72 69 67 69 6e 43 6f 6c 20 3d 20 30   *zOriginCol = 0
29796 3b 0a 20 20 69 6e 74 20 6a 3b 0a 20 20 69 66 28  ;.  int j;.  if(
29797 20 4e 45 56 45 52 28 70 45 78 70 72 3d 3d 30 29   NEVER(pExpr==0)
29798 20 7c 7c 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73   || pNC->pSrcLis
29799 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  t==0 ) return 0;
2979a 0a 0a 20 20 73 77 69 74 63 68 28 20 70 45 78 70  ..  switch( pExp
2979b 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73  r->op ){.    cas
2979c 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3a  e TK_AGG_COLUMN:
2979d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4c  .    case TK_COL
2979e 55 4d 4e 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20  UMN: {.      /* 
2979f 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  The expression i
297a0 73 20 61 20 63 6f 6c 75 6d 6e 2e 20 4c 6f 63 61  s a column. Loca
297a1 74 65 20 74 68 65 20 74 61 62 6c 65 20 74 68 65  te the table the
297a2 20 63 6f 6c 75 6d 6e 20 69 73 20 62 65 69 6e 67   column is being
297a3 0a 20 20 20 20 20 20 2a 2a 20 65 78 74 72 61 63  .      ** extrac
297a4 74 65 64 20 66 72 6f 6d 20 69 6e 20 4e 61 6d 65  ted from in Name
297a5 43 6f 6e 74 65 78 74 2e 70 53 72 63 4c 69 73 74  Context.pSrcList
297a6 2e 20 54 68 69 73 20 74 61 62 6c 65 20 6d 61 79  . This table may
297a7 20 62 65 20 72 65 61 6c 0a 20 20 20 20 20 20 2a   be real.      *
297a8 2a 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65  * database table
297a9 20 6f 72 20 61 20 73 75 62 71 75 65 72 79 2e 0a   or a subquery..
297aa 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 54        */.      T
297ab 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 30 3b 20  able *pTab = 0; 
297ac 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61             /* Ta
297ad 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 63 6f  ble structure co
297ae 6c 75 6d 6e 20 69 73 20 65 78 74 72 61 63 74 65  lumn is extracte
297af 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 20 20 20 20  d from */.      
297b0 53 65 6c 65 63 74 20 2a 70 53 20 3d 20 30 3b 20  Select *pS = 0; 
297b1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
297b2 65 6c 65 63 74 20 74 68 65 20 63 6f 6c 75 6d 6e  elect the column
297b3 20 69 73 20 65 78 74 72 61 63 74 65 64 20 66 72   is extracted fr
297b4 6f 6d 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  om */.      int 
297b5 69 43 6f 6c 20 3d 20 70 45 78 70 72 2d 3e 69 43  iCol = pExpr->iC
297b6 6f 6c 75 6d 6e 3b 20 20 2f 2a 20 49 6e 64 65 78  olumn;  /* Index
297b7 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 70 54   of column in pT
297b8 61 62 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  ab */.      test
297b9 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d  case( pExpr->op=
297ba 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 29  =TK_AGG_COLUMN )
297bb 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
297bc 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ( pExpr->op==TK_
297bd 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20  COLUMN );.      
297be 77 68 69 6c 65 28 20 70 4e 43 20 26 26 20 21 70  while( pNC && !p
297bf 54 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20 53  Tab ){.        S
297c0 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74  rcList *pTabList
297c1 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74   = pNC->pSrcList
297c2 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d  ;.        for(j=
297c3 30 3b 6a 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53  0;j<pTabList->nS
297c4 72 63 20 26 26 20 70 54 61 62 4c 69 73 74 2d 3e  rc && pTabList->
297c5 61 5b 6a 5d 2e 69 43 75 72 73 6f 72 21 3d 70 45  a[j].iCursor!=pE
297c6 78 70 72 2d 3e 69 54 61 62 6c 65 3b 6a 2b 2b 29  xpr->iTable;j++)
297c7 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6a 3c  ;.        if( j<
297c8 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 20 29  pTabList->nSrc )
297c9 7b 0a 20 20 20 20 20 20 20 20 20 20 70 54 61 62  {.          pTab
297ca 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 6a   = pTabList->a[j
297cb 5d 2e 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20  ].pTab;.        
297cc 20 20 70 53 20 3d 20 70 54 61 62 4c 69 73 74 2d    pS = pTabList-
297cd 3e 61 5b 6a 5d 2e 70 53 65 6c 65 63 74 3b 0a 20  >a[j].pSelect;. 
297ce 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
297cf 20 20 20 20 20 20 20 20 70 4e 43 20 3d 20 70 4e          pNC = pN
297d0 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20  C->pNext;.      
297d1 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20    }.      }..   
297d2 20 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29     if( pTab==0 )
297d3 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 74 20  {.        /* At 
297d4 6f 6e 65 20 74 69 6d 65 2c 20 63 6f 64 65 20 73  one time, code s
297d5 75 63 68 20 61 73 20 22 53 45 4c 45 43 54 20 6e  uch as "SELECT n
297d6 65 77 2e 78 22 20 77 69 74 68 69 6e 20 61 20 74  ew.x" within a t
297d7 72 69 67 67 65 72 20 77 6f 75 6c 64 0a 20 20 20  rigger would.   
297d8 20 20 20 20 20 2a 2a 20 63 61 75 73 65 20 74 68       ** cause th
297d9 69 73 20 63 6f 6e 64 69 74 69 6f 6e 20 74 6f 20  is condition to 
297da 72 75 6e 2e 20 20 53 69 6e 63 65 20 74 68 65 6e  run.  Since then
297db 2c 20 77 65 20 68 61 76 65 20 72 65 73 74 72 75  , we have restru
297dc 63 74 75 72 65 64 20 68 6f 77 0a 20 20 20 20 20  ctured how.     
297dd 20 20 20 2a 2a 20 74 72 69 67 67 65 72 20 63 6f     ** trigger co
297de 64 65 20 69 73 20 67 65 6e 65 72 61 74 65 64 20  de is generated 
297df 61 6e 64 20 73 6f 20 74 68 69 73 20 63 6f 6e 64  and so this cond
297e0 69 74 69 6f 6e 20 69 73 20 6e 6f 20 6c 6f 6e 67  ition is no long
297e1 65 72 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 70  er .        ** p
297e2 6f 73 73 69 62 6c 65 2e 20 48 6f 77 65 76 65 72  ossible. However
297e3 2c 20 69 74 20 63 61 6e 20 73 74 69 6c 6c 20 62  , it can still b
297e4 65 20 74 72 75 65 20 66 6f 72 20 73 74 61 74 65  e true for state
297e5 6d 65 6e 74 73 20 6c 69 6b 65 0a 20 20 20 20 20  ments like.     
297e6 20 20 20 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77     ** the follow
297e7 69 6e 67 3a 0a 20 20 20 20 20 20 20 20 2a 2a 0a  ing:.        **.
297e8 20 20 20 20 20 20 20 20 2a 2a 20 20 20 43 52 45          **   CRE
297e9 41 54 45 20 54 41 42 4c 45 20 74 31 28 63 6f 6c  ATE TABLE t1(col
297ea 20 49 4e 54 45 47 45 52 29 3b 0a 20 20 20 20 20   INTEGER);.     
297eb 20 20 20 2a 2a 20 20 20 53 45 4c 45 43 54 20 28     **   SELECT (
297ec 53 45 4c 45 43 54 20 74 31 2e 63 6f 6c 29 20 46  SELECT t1.col) F
297ed 52 4f 4d 20 46 52 4f 4d 20 74 31 3b 0a 20 20 20  ROM FROM t1;.   
297ee 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20       **.        
297ef 2a 2a 20 77 68 65 6e 20 63 6f 6c 75 6d 6e 54 79  ** when columnTy
297f0 70 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20 6f  pe() is called o
297f1 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  n the expression
297f2 20 22 74 31 2e 63 6f 6c 22 20 69 6e 20 74 68 65   "t1.col" in the
297f3 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 75 62   .        ** sub
297f4 2d 73 65 6c 65 63 74 2e 20 49 6e 20 74 68 69 73  -select. In this
297f5 20 63 61 73 65 2c 20 73 65 74 20 74 68 65 20 63   case, set the c
297f6 6f 6c 75 6d 6e 20 74 79 70 65 20 74 6f 20 4e 55  olumn type to NU
297f7 4c 4c 2c 20 65 76 65 6e 0a 20 20 20 20 20 20 20  LL, even.       
297f8 20 2a 2a 20 74 68 6f 75 67 68 20 69 74 20 73 68   ** though it sh
297f9 6f 75 6c 64 20 72 65 61 6c 6c 79 20 62 65 20 22  ould really be "
297fa 49 4e 54 45 47 45 52 22 2e 0a 20 20 20 20 20 20  INTEGER"..      
297fb 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20    **.        ** 
297fc 54 68 69 73 20 69 73 20 6e 6f 74 20 61 20 70 72  This is not a pr
297fd 6f 62 6c 65 6d 2c 20 61 73 20 74 68 65 20 63 6f  oblem, as the co
297fe 6c 75 6d 6e 20 74 79 70 65 20 6f 66 20 22 74 31  lumn type of "t1
297ff 2e 63 6f 6c 22 20 69 73 20 6e 65 76 65 72 0a 20  .col" is never. 
29800 20 20 20 20 20 20 20 2a 2a 20 75 73 65 64 2e 20         ** used. 
29801 57 68 65 6e 20 63 6f 6c 75 6d 6e 54 79 70 65 28  When columnType(
29802 29 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 74  ) is called on t
29803 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 0a 20  he expression . 
29804 20 20 20 20 20 20 20 2a 2a 20 22 28 53 45 4c 45         ** "(SELE
29805 43 54 20 74 31 2e 63 6f 6c 29 22 2c 20 74 68 65  CT t1.col)", the
29806 20 63 6f 72 72 65 63 74 20 74 79 70 65 20 69 73   correct type is
29807 20 72 65 74 75 72 6e 65 64 20 28 73 65 65 20 74   returned (see t
29808 68 65 20 54 4b 5f 53 45 4c 45 43 54 0a 20 20 20  he TK_SELECT.   
29809 20 20 20 20 20 2a 2a 20 62 72 61 6e 63 68 20 62       ** branch b
2980a 65 6c 6f 77 2e 20 20 2a 2f 0a 20 20 20 20 20 20  elow.  */.      
2980b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
2980c 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
2980d 70 54 61 62 20 26 26 20 70 45 78 70 72 2d 3e 70  pTab && pExpr->p
2980e 54 61 62 3d 3d 70 54 61 62 20 29 3b 0a 20 20 20  Tab==pTab );.   
2980f 20 20 20 69 66 28 20 70 53 20 29 7b 0a 20 20 20     if( pS ){.   
29810 20 20 20 20 20 2f 2a 20 54 68 65 20 22 74 61 62       /* The "tab
29811 6c 65 22 20 69 73 20 61 63 74 75 61 6c 6c 79 20  le" is actually 
29812 61 20 73 75 62 2d 73 65 6c 65 63 74 20 6f 72 20  a sub-select or 
29813 61 20 76 69 65 77 20 69 6e 20 74 68 65 20 46 52  a view in the FR
29814 4f 4d 20 63 6c 61 75 73 65 0a 20 20 20 20 20 20  OM clause.      
29815 20 20 2a 2a 20 6f 66 20 74 68 65 20 53 45 4c 45    ** of the SELE
29816 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 52 65  CT statement. Re
29817 74 75 72 6e 20 74 68 65 20 64 65 63 6c 61 72 61  turn the declara
29818 74 69 6f 6e 20 74 79 70 65 20 61 6e 64 20 6f 72  tion type and or
29819 69 67 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20  igin.        ** 
2981a 64 61 74 61 20 66 6f 72 20 74 68 65 20 72 65 73  data for the res
2981b 75 6c 74 2d 73 65 74 20 63 6f 6c 75 6d 6e 20 6f  ult-set column o
2981c 66 20 74 68 65 20 73 75 62 2d 73 65 6c 65 63 74  f the sub-select
2981d 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
2981e 20 20 20 20 20 69 66 28 20 41 4c 57 41 59 53 28       if( ALWAYS(
2981f 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43 6f 6c 3c  iCol>=0 && iCol<
29820 70 53 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70  pS->pEList->nExp
29821 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  r) ){.          
29822 2f 2a 20 49 66 20 69 43 6f 6c 20 69 73 20 6c 65  /* If iCol is le
29823 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68  ss than zero, th
29824 65 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  en the expressio
29825 6e 20 72 65 71 75 65 73 74 73 20 74 68 65 0a 20  n requests the. 
29826 20 20 20 20 20 20 20 20 20 2a 2a 20 72 6f 77 69           ** rowi
29827 64 20 6f 66 20 74 68 65 20 73 75 62 2d 73 65 6c  d of the sub-sel
29828 65 63 74 20 6f 72 20 76 69 65 77 2e 20 54 68 69  ect or view. Thi
29829 73 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  s expression is 
2982a 6c 65 67 61 6c 20 28 73 65 65 20 0a 20 20 20 20  legal (see .    
2982b 20 20 20 20 20 20 2a 2a 20 74 65 73 74 20 63 61        ** test ca
2982c 73 65 20 6d 69 73 63 32 2e 32 2e 32 29 20 2d 20  se misc2.2.2) - 
2982d 69 74 20 61 6c 77 61 79 73 20 65 76 61 6c 75 61  it always evalua
2982e 74 65 73 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20 20  tes to NULL..   
2982f 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
29830 20 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20      NameContext 
29831 73 4e 43 3b 0a 20 20 20 20 20 20 20 20 20 20 45  sNC;.          E
29832 78 70 72 20 2a 70 20 3d 20 70 53 2d 3e 70 45 4c  xpr *p = pS->pEL
29833 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 70 45 78  ist->a[iCol].pEx
29834 70 72 3b 0a 20 20 20 20 20 20 20 20 20 20 73 4e  pr;.          sN
29835 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 53 2d  C.pSrcList = pS-
29836 3e 70 53 72 63 3b 0a 20 20 20 20 20 20 20 20 20  >pSrc;.         
29837 20 73 4e 43 2e 70 4e 65 78 74 20 3d 20 70 4e 43   sNC.pNext = pNC
29838 3b 0a 20 20 20 20 20 20 20 20 20 20 73 4e 43 2e  ;.          sNC.
29839 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50  pParse = pNC->pP
2983a 61 72 73 65 3b 0a 20 20 20 20 20 20 20 20 20 20  arse;.          
2983b 7a 54 79 70 65 20 3d 20 63 6f 6c 75 6d 6e 54 79  zType = columnTy
2983c 70 65 28 26 73 4e 43 2c 20 70 2c 20 26 7a 4f 72  pe(&sNC, p, &zOr
2983d 69 67 69 6e 44 62 2c 20 26 7a 4f 72 69 67 69 6e  iginDb, &zOrigin
2983e 54 61 62 2c 20 26 7a 4f 72 69 67 69 6e 43 6f 6c  Tab, &zOriginCol
2983f 29 3b 20 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ); .        }.  
29840 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 41 4c      }else if( AL
29841 57 41 59 53 28 70 54 61 62 2d 3e 70 53 63 68 65  WAYS(pTab->pSche
29842 6d 61 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ma) ){.        /
29843 2a 20 41 20 72 65 61 6c 20 74 61 62 6c 65 20 2a  * A real table *
29844 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  /.        assert
29845 28 20 21 70 53 20 29 3b 0a 20 20 20 20 20 20 20  ( !pS );.       
29846 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 20 69 43   if( iCol<0 ) iC
29847 6f 6c 20 3d 20 70 54 61 62 2d 3e 69 50 4b 65 79  ol = pTab->iPKey
29848 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
29849 28 20 69 43 6f 6c 3d 3d 2d 31 20 7c 7c 20 28 69  ( iCol==-1 || (i
2984a 43 6f 6c 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70  Col>=0 && iCol<p
2984b 54 61 62 2d 3e 6e 43 6f 6c 29 20 29 3b 0a 20 20  Tab->nCol) );.  
2984c 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30        if( iCol<0
2984d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 54   ){.          zT
2984e 79 70 65 20 3d 20 22 49 4e 54 45 47 45 52 22 3b  ype = "INTEGER";
2984f 0a 20 20 20 20 20 20 20 20 20 20 7a 4f 72 69 67  .          zOrig
29850 69 6e 43 6f 6c 20 3d 20 22 72 6f 77 69 64 22 3b  inCol = "rowid";
29851 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
29852 20 20 20 20 20 20 20 20 20 20 7a 54 79 70 65 20            zType 
29853 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f  = pTab->aCol[iCo
29854 6c 5d 2e 7a 54 79 70 65 3b 0a 20 20 20 20 20 20  l].zType;.      
29855 20 20 20 20 7a 4f 72 69 67 69 6e 43 6f 6c 20 3d      zOriginCol =
29856 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c   pTab->aCol[iCol
29857 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20  ].zName;.       
29858 20 7d 0a 20 20 20 20 20 20 20 20 7a 4f 72 69 67   }.        zOrig
29859 69 6e 54 61 62 20 3d 20 70 54 61 62 2d 3e 7a 4e  inTab = pTab->zN
2985a 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ame;.        if(
2985b 20 70 4e 43 2d 3e 70 50 61 72 73 65 20 29 7b 0a   pNC->pParse ){.
2985c 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 44            int iD
2985d 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
2985e 61 54 6f 49 6e 64 65 78 28 70 4e 43 2d 3e 70 50  aToIndex(pNC->pP
2985f 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e  arse->db, pTab->
29860 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 20  pSchema);.      
29861 20 20 20 20 7a 4f 72 69 67 69 6e 44 62 20 3d 20      zOriginDb = 
29862 70 4e 43 2d 3e 70 50 61 72 73 65 2d 3e 64 62 2d  pNC->pParse->db-
29863 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b  >aDb[iDb].zName;
29864 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
29865 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
29866 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51      }.#ifndef SQ
29867 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
29868 52 59 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53  RY.    case TK_S
29869 45 4c 45 43 54 3a 20 7b 0a 20 20 20 20 20 20 2f  ELECT: {.      /
2986a 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e  * The expression
2986b 20 69 73 20 61 20 73 75 62 2d 73 65 6c 65 63 74   is a sub-select
2986c 2e 20 52 65 74 75 72 6e 20 74 68 65 20 64 65 63  . Return the dec
2986d 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20 61 6e  laration type an
2986e 64 0a 20 20 20 20 20 20 2a 2a 20 6f 72 69 67 69  d.      ** origi
2986f 6e 20 69 6e 66 6f 20 66 6f 72 20 74 68 65 20 73  n info for the s
29870 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 69 6e 20  ingle column in 
29871 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f  the result set o
29872 66 20 74 68 65 20 53 45 4c 45 43 54 0a 20 20 20  f the SELECT.   
29873 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e     ** statement.
29874 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
29875 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b  NameContext sNC;
29876 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70  .      Select *p
29877 53 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 53 65  S = pExpr->x.pSe
29878 6c 65 63 74 3b 0a 20 20 20 20 20 20 45 78 70 72  lect;.      Expr
29879 20 2a 70 20 3d 20 70 53 2d 3e 70 45 4c 69 73 74   *p = pS->pEList
2987a 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20  ->a[0].pExpr;.  
2987b 20 20 20 20 61 73 73 65 72 74 28 20 45 78 70 72      assert( Expr
2987c 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
2987d 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  r, EP_xIsSelect)
2987e 20 29 3b 0a 20 20 20 20 20 20 73 4e 43 2e 70 53   );.      sNC.pS
2987f 72 63 4c 69 73 74 20 3d 20 70 53 2d 3e 70 53 72  rcList = pS->pSr
29880 63 3b 0a 20 20 20 20 20 20 73 4e 43 2e 70 4e 65  c;.      sNC.pNe
29881 78 74 20 3d 20 70 4e 43 3b 0a 20 20 20 20 20 20  xt = pNC;.      
29882 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 4e 43  sNC.pParse = pNC
29883 2d 3e 70 50 61 72 73 65 3b 0a 20 20 20 20 20 20  ->pParse;.      
29884 7a 54 79 70 65 20 3d 20 63 6f 6c 75 6d 6e 54 79  zType = columnTy
29885 70 65 28 26 73 4e 43 2c 20 70 2c 20 26 7a 4f 72  pe(&sNC, p, &zOr
29886 69 67 69 6e 44 62 2c 20 26 7a 4f 72 69 67 69 6e  iginDb, &zOrigin
29887 54 61 62 2c 20 26 7a 4f 72 69 67 69 6e 43 6f 6c  Tab, &zOriginCol
29888 29 3b 20 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ); .      break;
29889 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
2988a 7d 0a 20 20 0a 20 20 69 66 28 20 70 7a 4f 72 69  }.  .  if( pzOri
2988b 67 69 6e 44 62 20 29 7b 0a 20 20 20 20 61 73 73  ginDb ){.    ass
2988c 65 72 74 28 20 70 7a 4f 72 69 67 69 6e 54 61 62  ert( pzOriginTab
2988d 20 26 26 20 70 7a 4f 72 69 67 69 6e 43 6f 6c 20   && pzOriginCol 
2988e 29 3b 0a 20 20 20 20 2a 70 7a 4f 72 69 67 69 6e  );.    *pzOrigin
2988f 44 62 20 3d 20 7a 4f 72 69 67 69 6e 44 62 3b 0a  Db = zOriginDb;.
29890 20 20 20 20 2a 70 7a 4f 72 69 67 69 6e 54 61 62      *pzOriginTab
29891 20 3d 20 7a 4f 72 69 67 69 6e 54 61 62 3b 0a 20   = zOriginTab;. 
29892 20 20 20 2a 70 7a 4f 72 69 67 69 6e 43 6f 6c 20     *pzOriginCol 
29893 3d 20 7a 4f 72 69 67 69 6e 43 6f 6c 3b 0a 20 20  = zOriginCol;.  
29894 7d 0a 20 20 72 65 74 75 72 6e 20 7a 54 79 70 65  }.  return zType
29895 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  ;.}../*.** Gener
29896 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69  ate code that wi
29897 6c 6c 20 74 65 6c 6c 20 74 68 65 20 56 44 42 45  ll tell the VDBE
29898 20 74 68 65 20 64 65 63 6c 61 72 61 74 69 6f 6e   the declaration
29899 20 74 79 70 65 73 20 6f 66 20 63 6f 6c 75 6d 6e   types of column
2989a 73 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 75  s.** in the resu
2989b 6c 74 20 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69  lt set..*/.stati
2989c 63 20 76 6f 69 64 20 67 65 6e 65 72 61 74 65 43  c void generateC
2989d 6f 6c 75 6d 6e 54 79 70 65 73 28 0a 20 20 50 61  olumnTypes(.  Pa
2989e 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
2989f 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74    /* Parser cont
298a0 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74  ext */.  SrcList
298a1 20 2a 70 54 61 62 4c 69 73 74 2c 20 20 2f 2a 20   *pTabList,  /* 
298a2 4c 69 73 74 20 6f 66 20 74 61 62 6c 65 73 20 2a  List of tables *
298a3 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
298a4 4c 69 73 74 20 20 20 20 2f 2a 20 45 78 70 72 65  List    /* Expre
298a5 73 73 69 6f 6e 73 20 64 65 66 69 6e 69 6e 67 20  ssions defining 
298a6 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a  the result set *
298a7 2f 0a 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c  /.){.#ifndef SQL
298a8 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50  ITE_OMIT_DECLTYP
298a9 45 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  E.  Vdbe *v = pP
298aa 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69  arse->pVdbe;.  i
298ab 6e 74 20 69 3b 0a 20 20 4e 61 6d 65 43 6f 6e 74  nt i;.  NameCont
298ac 65 78 74 20 73 4e 43 3b 0a 20 20 73 4e 43 2e 70  ext sNC;.  sNC.p
298ad 53 72 63 4c 69 73 74 20 3d 20 70 54 61 62 4c 69  SrcList = pTabLi
298ae 73 74 3b 0a 20 20 73 4e 43 2e 70 50 61 72 73 65  st;.  sNC.pParse
298af 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 66 6f 72   = pParse;.  for
298b0 28 69 3d 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e  (i=0; i<pEList->
298b1 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20  nExpr; i++){.   
298b2 20 45 78 70 72 20 2a 70 20 3d 20 70 45 4c 69 73   Expr *p = pELis
298b3 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20  t->a[i].pExpr;. 
298b4 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
298b5 54 79 70 65 3b 0a 23 69 66 64 65 66 20 53 51 4c  Type;.#ifdef SQL
298b6 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d  ITE_ENABLE_COLUM
298b7 4e 5f 4d 45 54 41 44 41 54 41 0a 20 20 20 20 63  N_METADATA.    c
298b8 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 72 69 67  onst char *zOrig
298b9 44 62 20 3d 20 30 3b 0a 20 20 20 20 63 6f 6e 73  Db = 0;.    cons
298ba 74 20 63 68 61 72 20 2a 7a 4f 72 69 67 54 61 62  t char *zOrigTab
298bb 20 3d 20 30 3b 0a 20 20 20 20 63 6f 6e 73 74 20   = 0;.    const 
298bc 63 68 61 72 20 2a 7a 4f 72 69 67 43 6f 6c 20 3d  char *zOrigCol =
298bd 20 30 3b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20   0;.    zType = 
298be 63 6f 6c 75 6d 6e 54 79 70 65 28 26 73 4e 43 2c  columnType(&sNC,
298bf 20 70 2c 20 26 7a 4f 72 69 67 44 62 2c 20 26 7a   p, &zOrigDb, &z
298c0 4f 72 69 67 54 61 62 2c 20 26 7a 4f 72 69 67 43  OrigTab, &zOrigC
298c1 6f 6c 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65  ol);..    /* The
298c2 20 76 64 62 65 20 6d 75 73 74 20 6d 61 6b 65 20   vdbe must make 
298c3 69 74 73 20 6f 77 6e 20 63 6f 70 79 20 6f 66 20  its own copy of 
298c4 74 68 65 20 63 6f 6c 75 6d 6e 2d 74 79 70 65 20  the column-type 
298c5 61 6e 64 20 6f 74 68 65 72 20 0a 20 20 20 20 2a  and other .    *
298c6 2a 20 63 6f 6c 75 6d 6e 20 73 70 65 63 69 66 69  * column specifi
298c7 63 20 73 74 72 69 6e 67 73 2c 20 69 6e 20 63 61  c strings, in ca
298c8 73 65 20 74 68 65 20 73 63 68 65 6d 61 20 69 73  se the schema is
298c9 20 72 65 73 65 74 20 62 65 66 6f 72 65 20 74 68   reset before th
298ca 69 73 0a 20 20 20 20 2a 2a 20 76 69 72 74 75 61  is.    ** virtua
298cb 6c 20 6d 61 63 68 69 6e 65 20 69 73 20 64 65 6c  l machine is del
298cc 65 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  eted..    */.   
298cd 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
298ce 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c  olName(v, i, COL
298cf 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 2c 20 7a  NAME_DATABASE, z
298d0 4f 72 69 67 44 62 2c 20 53 51 4c 49 54 45 5f 54  OrigDb, SQLITE_T
298d1 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 73  RANSIENT);.    s
298d2 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
298d3 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41  Name(v, i, COLNA
298d4 4d 45 5f 54 41 42 4c 45 2c 20 7a 4f 72 69 67 54  ME_TABLE, zOrigT
298d5 61 62 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  ab, SQLITE_TRANS
298d6 49 45 4e 54 29 3b 0a 20 20 20 20 73 71 6c 69 74  IENT);.    sqlit
298d7 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
298d8 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 43  (v, i, COLNAME_C
298d9 4f 4c 55 4d 4e 2c 20 7a 4f 72 69 67 43 6f 6c 2c  OLUMN, zOrigCol,
298da 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
298db 54 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 7a 54  T);.#else.    zT
298dc 79 70 65 20 3d 20 63 6f 6c 75 6d 6e 54 79 70 65  ype = columnType
298dd 28 26 73 4e 43 2c 20 70 2c 20 30 2c 20 30 2c 20  (&sNC, p, 0, 0, 
298de 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 73  0);.#endif.    s
298df 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
298e0 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41  Name(v, i, COLNA
298e1 4d 45 5f 44 45 43 4c 54 59 50 45 2c 20 7a 54 79  ME_DECLTYPE, zTy
298e2 70 65 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  pe, SQLITE_TRANS
298e3 49 45 4e 54 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  IENT);.  }.#endi
298e4 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
298e5 5f 44 45 43 4c 54 59 50 45 20 2a 2f 0a 7d 0a 0a  _DECLTYPE */.}..
298e6 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
298e7 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 74 65  ode that will te
298e8 6c 6c 20 74 68 65 20 56 44 42 45 20 74 68 65 20  ll the VDBE the 
298e9 6e 61 6d 65 73 20 6f 66 20 63 6f 6c 75 6d 6e 73  names of columns
298ea 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 75 6c  .** in the resul
298eb 74 20 73 65 74 2e 20 20 54 68 69 73 20 69 6e 66  t set.  This inf
298ec 6f 72 6d 61 74 69 6f 6e 20 69 73 20 75 73 65 64  ormation is used
298ed 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68 65 0a   to provide the.
298ee 2a 2a 20 61 7a 43 6f 6c 5b 5d 20 76 61 6c 75 65  ** azCol[] value
298ef 73 20 69 6e 20 74 68 65 20 63 61 6c 6c 62 61 63  s in the callbac
298f0 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  k..*/.static voi
298f1 64 20 67 65 6e 65 72 61 74 65 43 6f 6c 75 6d 6e  d generateColumn
298f2 4e 61 6d 65 73 28 0a 20 20 50 61 72 73 65 20 2a  Names(.  Parse *
298f3 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20  pParse,      /* 
298f4 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a  Parser context *
298f5 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61  /.  SrcList *pTa
298f6 62 4c 69 73 74 2c 20 20 2f 2a 20 4c 69 73 74 20  bList,  /* List 
298f7 6f 66 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 45  of tables */.  E
298f8 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20  xprList *pEList 
298f9 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e     /* Expression
298fa 73 20 64 65 66 69 6e 69 6e 67 20 74 68 65 20 72  s defining the r
298fb 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 29 7b 0a  esult set */.){.
298fc 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
298fd 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74  se->pVdbe;.  int
298fe 20 69 2c 20 6a 3b 0a 20 20 73 71 6c 69 74 65 33   i, j;.  sqlite3
298ff 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
29900 62 3b 0a 20 20 69 6e 74 20 66 75 6c 6c 4e 61 6d  b;.  int fullNam
29901 65 73 2c 20 73 68 6f 72 74 4e 61 6d 65 73 3b 0a  es, shortNames;.
29902 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
29903 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 20 20 2f  OMIT_EXPLAIN.  /
29904 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20  * If this is an 
29905 45 58 50 4c 41 49 4e 2c 20 73 6b 69 70 20 74 68  EXPLAIN, skip th
29906 69 73 20 73 74 65 70 20 2a 2f 0a 20 20 69 66 28  is step */.  if(
29907 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e   pParse->explain
29908 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
29909 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66    }.#endif..  if
2990a 28 20 70 50 61 72 73 65 2d 3e 63 6f 6c 4e 61 6d  ( pParse->colNam
2990b 65 73 53 65 74 20 7c 7c 20 4e 45 56 45 52 28 76  esSet || NEVER(v
2990c 3d 3d 30 29 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  ==0) || db->mall
2990d 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72  ocFailed ) retur
2990e 6e 3b 0a 20 20 70 50 61 72 73 65 2d 3e 63 6f 6c  n;.  pParse->col
2990f 4e 61 6d 65 73 53 65 74 20 3d 20 31 3b 0a 20 20  NamesSet = 1;.  
29910 66 75 6c 6c 4e 61 6d 65 73 20 3d 20 28 64 62 2d  fullNames = (db-
29911 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
29912 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73 29 21 3d 30  FullColNames)!=0
29913 3b 0a 20 20 73 68 6f 72 74 4e 61 6d 65 73 20 3d  ;.  shortNames =
29914 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51   (db->flags & SQ
29915 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d  LITE_ShortColNam
29916 65 73 29 21 3d 30 3b 0a 20 20 73 71 6c 69 74 65  es)!=0;.  sqlite
29917 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28  3VdbeSetNumCols(
29918 76 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  v, pEList->nExpr
29919 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
2991a 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69  pEList->nExpr; i
2991b 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70  ++){.    Expr *p
2991c 3b 0a 20 20 20 20 70 20 3d 20 70 45 4c 69 73 74  ;.    p = pEList
2991d 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20  ->a[i].pExpr;.  
2991e 20 20 69 66 28 20 4e 45 56 45 52 28 70 3d 3d 30    if( NEVER(p==0
2991f 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  ) ) continue;.  
29920 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e 61 5b    if( pEList->a[
29921 69 5d 2e 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20  i].zName ){.    
29922 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20    char *zName = 
29923 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61  pEList->a[i].zNa
29924 6d 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  me;.      sqlite
29925 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
29926 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, i, COLNAME_NA
29927 4d 45 2c 20 7a 4e 61 6d 65 2c 20 53 51 4c 49 54  ME, zName, SQLIT
29928 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20  E_TRANSIENT);.  
29929 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 2d 3e    }else if( (p->
2992a 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c  op==TK_COLUMN ||
2992b 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43   p->op==TK_AGG_C
2992c 4f 4c 55 4d 4e 29 20 26 26 20 70 54 61 62 4c 69  OLUMN) && pTabLi
2992d 73 74 20 29 7b 0a 20 20 20 20 20 20 54 61 62 6c  st ){.      Tabl
2992e 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 20 20 63  e *pTab;.      c
2992f 68 61 72 20 2a 7a 43 6f 6c 3b 0a 20 20 20 20 20  har *zCol;.     
29930 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 2d 3e 69   int iCol = p->i
29931 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 66 6f  Column;.      fo
29932 72 28 6a 3d 30 3b 20 41 4c 57 41 59 53 28 6a 3c  r(j=0; ALWAYS(j<
29933 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 29 3b  pTabList->nSrc);
29934 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69   j++){.        i
29935 66 28 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 6a  f( pTabList->a[j
29936 5d 2e 69 43 75 72 73 6f 72 3d 3d 70 2d 3e 69 54  ].iCursor==p->iT
29937 61 62 6c 65 20 29 20 62 72 65 61 6b 3b 0a 20 20  able ) break;.  
29938 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65      }.      asse
29939 72 74 28 20 6a 3c 70 54 61 62 4c 69 73 74 2d 3e  rt( j<pTabList->
2993a 6e 53 72 63 20 29 3b 0a 20 20 20 20 20 20 70 54  nSrc );.      pT
2993b 61 62 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61  ab = pTabList->a
2993c 5b 6a 5d 2e 70 54 61 62 3b 0a 20 20 20 20 20 20  [j].pTab;.      
2993d 69 66 28 20 69 43 6f 6c 3c 30 20 29 20 69 43 6f  if( iCol<0 ) iCo
2993e 6c 20 3d 20 70 54 61 62 2d 3e 69 50 4b 65 79 3b  l = pTab->iPKey;
2993f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69  .      assert( i
29940 43 6f 6c 3d 3d 2d 31 20 7c 7c 20 28 69 43 6f 6c  Col==-1 || (iCol
29941 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70 54 61 62  >=0 && iCol<pTab
29942 2d 3e 6e 43 6f 6c 29 20 29 3b 0a 20 20 20 20 20  ->nCol) );.     
29943 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b 0a 20   if( iCol<0 ){. 
29944 20 20 20 20 20 20 20 7a 43 6f 6c 20 3d 20 22 72         zCol = "r
29945 6f 77 69 64 22 3b 0a 20 20 20 20 20 20 7d 65 6c  owid";.      }el
29946 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c  se{.        zCol
29947 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43   = pTab->aCol[iC
29948 6f 6c 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20  ol].zName;.     
29949 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21 73 68   }.      if( !sh
2994a 6f 72 74 4e 61 6d 65 73 20 26 26 20 21 66 75 6c  ortNames && !ful
2994b 6c 4e 61 6d 65 73 20 29 7b 0a 20 20 20 20 20 20  lNames ){.      
2994c 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
2994d 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f  ColName(v, i, CO
2994e 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 0a 20 20 20  LNAME_NAME, .   
2994f 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
29950 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 45 4c  DbStrDup(db, pEL
29951 69 73 74 2d 3e 61 5b 69 5d 2e 7a 53 70 61 6e 29  ist->a[i].zSpan)
29952 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43  , SQLITE_DYNAMIC
29953 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  );.      }else i
29954 66 28 20 66 75 6c 6c 4e 61 6d 65 73 20 29 7b 0a  f( fullNames ){.
29955 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e          char *zN
29956 61 6d 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  ame = 0;.       
29957 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   zName = sqlite3
29958 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73 2e  MPrintf(db, "%s.
29959 25 73 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  %s", pTab->zName
2995a 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 20 20 20  , zCol);.       
2995b 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
2995c 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c  olName(v, i, COL
2995d 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4e 61 6d 65  NAME_NAME, zName
2995e 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43  , SQLITE_DYNAMIC
2995f 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
29960 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
29961 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
29962 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   i, COLNAME_NAME
29963 2c 20 7a 43 6f 6c 2c 20 53 51 4c 49 54 45 5f 54  , zCol, SQLITE_T
29964 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 20  RANSIENT);.     
29965 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
29966 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
29967 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20  etColName(v, i, 
29968 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 0a 20  COLNAME_NAME, . 
29969 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2996a 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 45 4c  DbStrDup(db, pEL
2996b 69 73 74 2d 3e 61 5b 69 5d 2e 7a 53 70 61 6e 29  ist->a[i].zSpan)
2996c 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43  , SQLITE_DYNAMIC
2996d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 67  );.    }.  }.  g
2996e 65 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 54 79 70  enerateColumnTyp
2996f 65 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c  es(pParse, pTabL
29970 69 73 74 2c 20 70 45 4c 69 73 74 29 3b 0a 7d 0a  ist, pEList);.}.
29971 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
29972 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45  OMIT_COMPOUND_SE
29973 4c 45 43 54 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 20  LECT./*.** Name 
29974 6f 66 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f  of the connectio
29975 6e 20 6f 70 65 72 61 74 6f 72 2c 20 75 73 65 64  n operator, used
29976 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 61   for error messa
29977 67 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  ges..*/.static c
29978 6f 6e 73 74 20 63 68 61 72 20 2a 73 65 6c 65 63  onst char *selec
29979 74 4f 70 4e 61 6d 65 28 69 6e 74 20 69 64 29 7b  tOpName(int id){
2997a 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 73 77  .  char *z;.  sw
2997b 69 74 63 68 28 20 69 64 20 29 7b 0a 20 20 20 20  itch( id ){.    
2997c 63 61 73 65 20 54 4b 5f 41 4c 4c 3a 20 20 20 20  case TK_ALL:    
2997d 20 20 20 7a 20 3d 20 22 55 4e 49 4f 4e 20 41 4c     z = "UNION AL
2997e 4c 22 3b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  L";   break;.   
2997f 20 63 61 73 65 20 54 4b 5f 49 4e 54 45 52 53 45   case TK_INTERSE
29980 43 54 3a 20 7a 20 3d 20 22 49 4e 54 45 52 53 45  CT: z = "INTERSE
29981 43 54 22 3b 20 20 20 62 72 65 61 6b 3b 0a 20 20  CT";   break;.  
29982 20 20 63 61 73 65 20 54 4b 5f 45 58 43 45 50 54    case TK_EXCEPT
29983 3a 20 20 20 20 7a 20 3d 20 22 45 58 43 45 50 54  :    z = "EXCEPT
29984 22 3b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ";      break;. 
29985 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20 20     default:     
29986 20 20 20 20 20 20 7a 20 3d 20 22 55 4e 49 4f 4e        z = "UNION
29987 22 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  ";       break;.
29988 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a    }.  return z;.
29989 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
2998a 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44  TE_OMIT_COMPOUND
2998b 5f 53 45 4c 45 43 54 20 2a 2f 0a 0a 2f 2a 0a 2a  _SELECT */../*.*
2998c 2a 20 47 69 76 65 6e 20 61 20 61 6e 20 65 78 70  * Given a an exp
2998d 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 28 77 68  ression list (wh
2998e 69 63 68 20 69 73 20 72 65 61 6c 6c 79 20 74 68  ich is really th
2998f 65 20 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73  e list of expres
29990 73 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 66 6f  sions.** that fo
29991 72 6d 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  rm the result se
29992 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74  t of a SELECT st
29993 61 74 65 6d 65 6e 74 29 20 63 6f 6d 70 75 74 65  atement) compute
29994 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20   appropriate.** 
29995 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 6f 72  column names for
29996 20 61 20 74 61 62 6c 65 20 74 68 61 74 20 77 6f   a table that wo
29997 75 6c 64 20 68 6f 6c 64 20 74 68 65 20 65 78 70  uld hold the exp
29998 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2a  ression list..**
29999 0a 2a 2a 20 41 6c 6c 20 63 6f 6c 75 6d 6e 20 6e  .** All column n
2999a 61 6d 65 73 20 77 69 6c 6c 20 62 65 20 75 6e 69  ames will be uni
2999b 71 75 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20  que..**.** Only 
2999c 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  the column names
2999d 20 61 72 65 20 63 6f 6d 70 75 74 65 64 2e 20 20   are computed.  
2999e 43 6f 6c 75 6d 6e 2e 7a 54 79 70 65 2c 20 43 6f  Column.zType, Co
2999f 6c 75 6d 6e 2e 7a 43 6f 6c 6c 2c 0a 2a 2a 20 61  lumn.zColl,.** a
299a0 6e 64 20 6f 74 68 65 72 20 66 69 65 6c 64 73 20  nd other fields 
299a1 6f 66 20 43 6f 6c 75 6d 6e 20 61 72 65 20 7a 65  of Column are ze
299a2 72 6f 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  roed..**.** Retu
299a3 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20  rn SQLITE_OK on 
299a4 73 75 63 63 65 73 73 2e 20 20 49 66 20 61 20 6d  success.  If a m
299a5 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
299a6 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 0a 2a   error occurs,.*
299a7 2a 20 73 74 6f 72 65 20 4e 55 4c 4c 20 69 6e 20  * store NULL in 
299a8 2a 70 61 43 6f 6c 20 61 6e 64 20 30 20 69 6e 20  *paCol and 0 in 
299a9 2a 70 6e 43 6f 6c 20 61 6e 64 20 72 65 74 75 72  *pnCol and retur
299aa 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a  n SQLITE_NOMEM..
299ab 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65  */.static int se
299ac 6c 65 63 74 43 6f 6c 75 6d 6e 73 46 72 6f 6d 45  lectColumnsFromE
299ad 78 70 72 4c 69 73 74 28 0a 20 20 50 61 72 73 65  xprList(.  Parse
299ae 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
299af 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
299b0 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c  ntext */.  ExprL
299b1 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 20 20  ist *pEList,    
299b2 20 20 20 2f 2a 20 45 78 70 72 20 6c 69 73 74 20     /* Expr list 
299b3 66 72 6f 6d 20 77 68 69 63 68 20 74 6f 20 64 65  from which to de
299b4 72 69 76 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65  rive column name
299b5 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 6f  s */.  int *pnCo
299b6 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  l,             /
299b7 2a 20 57 72 69 74 65 20 74 68 65 20 6e 75 6d 62  * Write the numb
299b8 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 68 65  er of columns he
299b9 72 65 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a  re */.  Column *
299ba 2a 70 61 43 6f 6c 20 20 20 20 20 20 20 20 20 20  *paCol          
299bb 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 65 77  /* Write the new
299bc 20 63 6f 6c 75 6d 6e 20 6c 69 73 74 20 68 65 72   column list her
299bd 65 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65  e */.){.  sqlite
299be 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
299bf 64 62 3b 20 20 20 2f 2a 20 44 61 74 61 62 61 73  db;   /* Databas
299c0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
299c1 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20    int i, j;     
299c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
299c3 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a   Loop counters *
299c4 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20  /.  int cnt;    
299c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
299c6 2f 2a 20 49 6e 64 65 78 20 61 64 64 65 64 20 74  /* Index added t
299c7 6f 20 6d 61 6b 65 20 74 68 65 20 6e 61 6d 65 20  o make the name 
299c8 75 6e 69 71 75 65 20 2a 2f 0a 20 20 43 6f 6c 75  unique */.  Colu
299c9 6d 6e 20 2a 61 43 6f 6c 2c 20 2a 70 43 6f 6c 3b  mn *aCol, *pCol;
299ca 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c          /* For l
299cb 6f 6f 70 69 6e 67 20 6f 76 65 72 20 72 65 73 75  ooping over resu
299cc 6c 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20  lt columns */.  
299cd 69 6e 74 20 6e 43 6f 6c 3b 20 20 20 20 20 20 20  int nCol;       
299ce 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
299cf 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
299d0 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
299d1 65 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 3b  et */.  Expr *p;
299d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
299d3 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f      /* Expressio
299d4 6e 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 72  n for a single r
299d5 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 2a 2f 0a  esult column */.
299d6 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20    char *zName;  
299d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
299d8 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a   Column name */.
299d9 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 20 20 20 20    int nName;    
299da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
299db 20 53 69 7a 65 20 6f 66 20 6e 61 6d 65 20 69 6e   Size of name in
299dc 20 7a 4e 61 6d 65 5b 5d 20 2a 2f 0a 0a 20 20 2a   zName[] */..  *
299dd 70 6e 43 6f 6c 20 3d 20 6e 43 6f 6c 20 3d 20 70  pnCol = nCol = p
299de 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20  EList->nExpr;.  
299df 61 43 6f 6c 20 3d 20 2a 70 61 43 6f 6c 20 3d 20  aCol = *paCol = 
299e0 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
299e1 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 61  ero(db, sizeof(a
299e2 43 6f 6c 5b 30 5d 29 2a 6e 43 6f 6c 29 3b 0a 20  Col[0])*nCol);. 
299e3 20 69 66 28 20 61 43 6f 6c 3d 3d 30 20 29 20 72   if( aCol==0 ) r
299e4 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
299e5 45 4d 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70  EM;.  for(i=0, p
299e6 43 6f 6c 3d 61 43 6f 6c 3b 20 69 3c 6e 43 6f 6c  Col=aCol; i<nCol
299e7 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a  ; i++, pCol++){.
299e8 20 20 20 20 2f 2a 20 47 65 74 20 61 6e 20 61 70      /* Get an ap
299e9 70 72 6f 70 72 69 61 74 65 20 6e 61 6d 65 20 66  propriate name f
299ea 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 20 20  or the column.  
299eb 20 20 2a 2f 0a 20 20 20 20 70 20 3d 20 70 45 4c    */.    p = pEL
299ec 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b  ist->a[i].pExpr;
299ed 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e  .    assert( p->
299ee 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20 45 78 70  pRight==0 || Exp
299ef 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2d 3e  rHasProperty(p->
299f0 70 52 69 67 68 74 2c 20 45 50 5f 49 6e 74 56 61  pRight, EP_IntVa
299f1 6c 75 65 29 0a 20 20 20 20 20 20 20 20 20 20 20  lue).           
299f2 20 20 20 20 7c 7c 20 70 2d 3e 70 52 69 67 68 74      || p->pRight
299f3 2d 3e 75 2e 7a 54 6f 6b 65 6e 3d 3d 30 20 7c 7c  ->u.zToken==0 ||
299f4 20 70 2d 3e 70 52 69 67 68 74 2d 3e 75 2e 7a 54   p->pRight->u.zT
299f5 6f 6b 65 6e 5b 30 5d 21 3d 30 20 29 3b 0a 20 20  oken[0]!=0 );.  
299f6 20 20 69 66 28 20 28 7a 4e 61 6d 65 20 3d 20 70    if( (zName = p
299f7 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d  EList->a[i].zNam
299f8 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f  e)!=0 ){.      /
299f9 2a 20 49 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20  * If the column 
299fa 63 6f 6e 74 61 69 6e 73 20 61 6e 20 22 41 53 20  contains an "AS 
299fb 3c 6e 61 6d 65 3e 22 20 70 68 72 61 73 65 2c 20  <name>" phrase, 
299fc 75 73 65 20 3c 6e 61 6d 65 3e 20 61 73 20 74 68  use <name> as th
299fd 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20  e name */.      
299fe 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44  zName = sqlite3D
299ff 62 53 74 72 44 75 70 28 64 62 2c 20 7a 4e 61 6d  bStrDup(db, zNam
29a00 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  e);.    }else{. 
29a01 20 20 20 20 20 45 78 70 72 20 2a 70 43 6f 6c 45       Expr *pColE
29a02 78 70 72 20 3d 20 70 3b 20 20 2f 2a 20 54 68 65  xpr = p;  /* The
29a03 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74   expression that
29a04 20 69 73 20 74 68 65 20 72 65 73 75 6c 74 20 63   is the result c
29a05 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20  olumn name */.  
29a06 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b      Table *pTab;
29a07 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c           /* Tabl
29a08 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  e associated wit
29a09 68 20 74 68 69 73 20 65 78 70 72 65 73 73 69 6f  h this expressio
29a0a 6e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65  n */.      while
29a0b 28 20 70 43 6f 6c 45 78 70 72 2d 3e 6f 70 3d 3d  ( pColExpr->op==
29a0c 54 4b 5f 44 4f 54 20 29 20 70 43 6f 6c 45 78 70  TK_DOT ) pColExp
29a0d 72 20 3d 20 70 43 6f 6c 45 78 70 72 2d 3e 70 52  r = pColExpr->pR
29a0e 69 67 68 74 3b 0a 20 20 20 20 20 20 69 66 28 20  ight;.      if( 
29a0f 70 43 6f 6c 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  pColExpr->op==TK
29a10 5f 43 4f 4c 55 4d 4e 20 26 26 20 41 4c 57 41 59  _COLUMN && ALWAY
29a11 53 28 70 43 6f 6c 45 78 70 72 2d 3e 70 54 61 62  S(pColExpr->pTab
29a12 21 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20  !=0) ){.        
29a13 2f 2a 20 46 6f 72 20 63 6f 6c 75 6d 6e 73 20 75  /* For columns u
29a14 73 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61  se the column na
29a15 6d 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20  me name */.     
29a16 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 43     int iCol = pC
29a17 6f 6c 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b  olExpr->iColumn;
29a18 0a 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20  .        pTab = 
29a19 70 43 6f 6c 45 78 70 72 2d 3e 70 54 61 62 3b 0a  pColExpr->pTab;.
29a1a 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c          if( iCol
29a1b 3c 30 20 29 20 69 43 6f 6c 20 3d 20 70 54 61 62  <0 ) iCol = pTab
29a1c 2d 3e 69 50 4b 65 79 3b 0a 20 20 20 20 20 20 20  ->iPKey;.       
29a1d 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   zName = sqlite3
29a1e 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73 22  MPrintf(db, "%s"
29a1f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
29a20 20 20 20 69 43 6f 6c 3e 3d 30 20 3f 20 70 54 61     iCol>=0 ? pTa
29a21 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e  b->aCol[iCol].zN
29a22 61 6d 65 20 3a 20 22 72 6f 77 69 64 22 29 3b 0a  ame : "rowid");.
29a23 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
29a24 70 43 6f 6c 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  pColExpr->op==TK
29a25 5f 49 44 20 29 7b 0a 20 20 20 20 20 20 20 20 61  _ID ){.        a
29a26 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50  ssert( !ExprHasP
29a27 72 6f 70 65 72 74 79 28 70 43 6f 6c 45 78 70 72  roperty(pColExpr
29a28 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29  , EP_IntValue) )
29a29 3b 0a 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 20  ;.        zName 
29a2a 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
29a2b 28 64 62 2c 20 22 25 73 22 2c 20 70 43 6f 6c 45  (db, "%s", pColE
29a2c 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a  xpr->u.zToken);.
29a2d 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
29a2e 20 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65 20       /* Use the 
29a2f 6f 72 69 67 69 6e 61 6c 20 74 65 78 74 20 6f 66  original text of
29a30 20 74 68 65 20 63 6f 6c 75 6d 6e 20 65 78 70 72   the column expr
29a31 65 73 73 69 6f 6e 20 61 73 20 69 74 73 20 6e 61  ession as its na
29a32 6d 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 7a 4e  me */.        zN
29a33 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  ame = sqlite3MPr
29a34 69 6e 74 66 28 64 62 2c 20 22 25 73 22 2c 20 70  intf(db, "%s", p
29a35 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 53 70 61  EList->a[i].zSpa
29a36 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  n);.      }.    
29a37 7d 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61  }.    if( db->ma
29a38 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
29a39 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
29a3a 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20  e(db, zName);.  
29a3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
29a3c 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75  ..    /* Make su
29a3d 72 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61  re the column na
29a3e 6d 65 20 69 73 20 75 6e 69 71 75 65 2e 20 20 49  me is unique.  I
29a3f 66 20 74 68 65 20 6e 61 6d 65 20 69 73 20 6e 6f  f the name is no
29a40 74 20 75 6e 69 71 75 65 2c 0a 20 20 20 20 2a 2a  t unique,.    **
29a41 20 61 70 70 65 6e 64 20 61 20 69 6e 74 65 67 65   append a intege
29a42 72 20 74 6f 20 74 68 65 20 6e 61 6d 65 20 73 6f  r to the name so
29a43 20 74 68 61 74 20 69 74 20 62 65 63 6f 6d 65 73   that it becomes
29a44 20 75 6e 69 71 75 65 2e 0a 20 20 20 20 2a 2f 0a   unique..    */.
29a45 20 20 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69      nName = sqli
29a46 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d  te3Strlen30(zNam
29a47 65 29 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 63 6e  e);.    for(j=cn
29a48 74 3d 30 3b 20 6a 3c 69 3b 20 6a 2b 2b 29 7b 0a  t=0; j<i; j++){.
29a49 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
29a4a 33 53 74 72 49 43 6d 70 28 61 43 6f 6c 5b 6a 5d  3StrICmp(aCol[j]
29a4b 2e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 3d 3d  .zName, zName)==
29a4c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61  0 ){.        cha
29a4d 72 20 2a 7a 4e 65 77 4e 61 6d 65 3b 0a 20 20 20  r *zNewName;.   
29a4e 20 20 20 20 20 7a 4e 61 6d 65 5b 6e 4e 61 6d 65       zName[nName
29a4f 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7a  ] = 0;.        z
29a50 4e 65 77 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65  NewName = sqlite
29a51 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73  3MPrintf(db, "%s
29a52 3a 25 64 22 2c 20 7a 4e 61 6d 65 2c 20 2b 2b 63  :%d", zName, ++c
29a53 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  nt);.        sql
29a54 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
29a55 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 7a  Name);.        z
29a56 4e 61 6d 65 20 3d 20 7a 4e 65 77 4e 61 6d 65 3b  Name = zNewName;
29a57 0a 20 20 20 20 20 20 20 20 6a 20 3d 20 2d 31 3b  .        j = -1;
29a58 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 4e 61  .        if( zNa
29a59 6d 65 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20  me==0 ) break;. 
29a5a 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
29a5b 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d 20 7a   pCol->zName = z
29a5c 4e 61 6d 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20  Name;.  }.  if( 
29a5d 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
29a5e 20 29 7b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b   ){.    for(j=0;
29a5f 20 6a 3c 69 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20   j<i; j++){.    
29a60 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
29a61 64 62 2c 20 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d  db, aCol[j].zNam
29a62 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  e);.    }.    sq
29a63 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
29a64 61 43 6f 6c 29 3b 0a 20 20 20 20 2a 70 61 43 6f  aCol);.    *paCo
29a65 6c 20 3d 20 30 3b 0a 20 20 20 20 2a 70 6e 43 6f  l = 0;.    *pnCo
29a66 6c 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72  l = 0;.    retur
29a67 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
29a68 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
29a69 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
29a6a 20 41 64 64 20 74 79 70 65 20 61 6e 64 20 63 6f   Add type and co
29a6b 6c 6c 61 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74  llation informat
29a6c 69 6f 6e 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20  ion to a column 
29a6d 6c 69 73 74 20 62 61 73 65 64 20 6f 6e 0a 2a 2a  list based on.**
29a6e 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
29a6f 65 6e 74 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20  ent..** .** The 
29a70 63 6f 6c 75 6d 6e 20 6c 69 73 74 20 70 72 65 73  column list pres
29a71 75 6d 61 62 6c 79 20 63 61 6d 65 20 66 72 6f 6d  umably came from
29a72 20 73 65 6c 65 63 74 43 6f 6c 75 6d 6e 4e 61 6d   selectColumnNam
29a73 65 73 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 29  esFromExprList()
29a74 2e 0a 2a 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20  ..** The column 
29a75 6c 69 73 74 20 68 61 73 20 6f 6e 6c 79 20 6e 61  list has only na
29a76 6d 65 73 2c 20 6e 6f 74 20 74 79 70 65 73 20 6f  mes, not types o
29a77 72 20 63 6f 6c 6c 61 74 69 6f 6e 73 2e 20 20 54  r collations.  T
29a78 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 67  his.** routine g
29a79 6f 65 73 20 74 68 72 6f 75 67 68 20 61 6e 64 20  oes through and 
29a7a 61 64 64 73 20 74 68 65 20 74 79 70 65 73 20 61  adds the types a
29a7b 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 73 2e 0a 2a  nd collations..*
29a7c 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
29a7d 65 20 72 65 71 75 69 72 65 73 20 74 68 61 74 20  e requires that 
29a7e 61 6c 6c 20 69 64 65 6e 74 69 66 69 65 72 73 20  all identifiers 
29a7f 69 6e 20 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a  in the SELECT.**
29a80 20 73 74 61 74 65 6d 65 6e 74 20 62 65 20 72 65   statement be re
29a81 73 6f 6c 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  solved..*/.stati
29a82 63 20 76 6f 69 64 20 73 65 6c 65 63 74 41 64 64  c void selectAdd
29a83 43 6f 6c 75 6d 6e 54 79 70 65 41 6e 64 43 6f 6c  ColumnTypeAndCol
29a84 6c 61 74 69 6f 6e 28 0a 20 20 50 61 72 73 65 20  lation(.  Parse 
29a85 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
29a86 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
29a87 78 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f  xts */.  int nCo
29a88 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  l,             /
29a89 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  * Number of colu
29a8a 6d 6e 73 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e 20  mns */.  Column 
29a8b 2a 61 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 2f  *aCol,         /
29a8c 2a 20 4c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e  * List of column
29a8d 73 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  s */.  Select *p
29a8e 53 65 6c 65 63 74 20 20 20 20 20 20 20 2f 2a 20  Select       /* 
29a8f 53 45 4c 45 43 54 20 75 73 65 64 20 74 6f 20 64  SELECT used to d
29a90 65 74 65 72 6d 69 6e 65 20 74 79 70 65 73 20 61  etermine types a
29a91 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 73 20 2a 2f  nd collations */
29a92 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  .){.  sqlite3 *d
29a93 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
29a94 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e    NameContext sN
29a95 43 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f  C;.  Column *pCo
29a96 6c 3b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43  l;.  CollSeq *pC
29a97 6f 6c 6c 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  oll;.  int i;.  
29a98 45 78 70 72 20 2a 70 3b 0a 20 20 73 74 72 75 63  Expr *p;.  struc
29a99 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
29a9a 2a 61 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  *a;..  assert( p
29a9b 53 65 6c 65 63 74 21 3d 30 20 29 3b 0a 20 20 61  Select!=0 );.  a
29a9c 73 73 65 72 74 28 20 28 70 53 65 6c 65 63 74 2d  ssert( (pSelect-
29a9d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 52  >selFlags & SF_R
29a9e 65 73 6f 6c 76 65 64 29 21 3d 30 20 29 3b 0a 20  esolved)!=0 );. 
29a9f 20 61 73 73 65 72 74 28 20 6e 43 6f 6c 3d 3d 70   assert( nCol==p
29aa0 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e  Select->pEList->
29aa1 6e 45 78 70 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c  nExpr || db->mal
29aa2 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69  locFailed );.  i
29aa3 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
29aa4 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  led ) return;.  
29aa5 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c 20  memset(&sNC, 0, 
29aa6 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20  sizeof(sNC));.  
29aa7 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70  sNC.pSrcList = p
29aa8 53 65 6c 65 63 74 2d 3e 70 53 72 63 3b 0a 20 20  Select->pSrc;.  
29aa9 61 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c  a = pSelect->pEL
29aaa 69 73 74 2d 3e 61 3b 0a 20 20 66 6f 72 28 69 3d  ist->a;.  for(i=
29aab 30 2c 20 70 43 6f 6c 3d 61 43 6f 6c 3b 20 69 3c  0, pCol=aCol; i<
29aac 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b  nCol; i++, pCol+
29aad 2b 29 7b 0a 20 20 20 20 70 20 3d 20 61 5b 69 5d  +){.    p = a[i]
29aae 2e 70 45 78 70 72 3b 0a 20 20 20 20 70 43 6f 6c  .pExpr;.    pCol
29aaf 2d 3e 7a 54 79 70 65 20 3d 20 73 71 6c 69 74 65  ->zType = sqlite
29ab0 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 63 6f  3DbStrDup(db, co
29ab1 6c 75 6d 6e 54 79 70 65 28 26 73 4e 43 2c 20 70  lumnType(&sNC, p
29ab2 2c 20 30 2c 20 30 2c 20 30 29 29 3b 0a 20 20 20  , 0, 0, 0));.   
29ab3 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 20   pCol->affinity 
29ab4 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66  = sqlite3ExprAff
29ab5 69 6e 69 74 79 28 70 29 3b 0a 20 20 20 20 69 66  inity(p);.    if
29ab6 28 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79  ( pCol->affinity
29ab7 3d 3d 30 20 29 20 70 43 6f 6c 2d 3e 61 66 66 69  ==0 ) pCol->affi
29ab8 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 46  nity = SQLITE_AF
29ab9 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 70 43 6f 6c  F_NONE;.    pCol
29aba 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  l = sqlite3ExprC
29abb 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70  ollSeq(pParse, p
29abc 29 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c  );.    if( pColl
29abd 20 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c 2d 3e   ){.      pCol->
29abe 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 44  zColl = sqlite3D
29abf 62 53 74 72 44 75 70 28 64 62 2c 20 70 43 6f 6c  bStrDup(db, pCol
29ac0 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d  l->zName);.    }
29ac1 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69  .  }.}../*.** Gi
29ac2 76 65 6e 20 61 20 53 45 4c 45 43 54 20 73 74 61  ven a SELECT sta
29ac3 74 65 6d 65 6e 74 2c 20 67 65 6e 65 72 61 74 65  tement, generate
29ac4 20 61 20 54 61 62 6c 65 20 73 74 72 75 63 74 75   a Table structu
29ac5 72 65 20 74 68 61 74 20 64 65 73 63 72 69 62 65  re that describe
29ac6 73 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20  s.** the result 
29ac7 73 65 74 20 6f 66 20 74 68 61 74 20 53 45 4c 45  set of that SELE
29ac8 43 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  CT..*/.SQLITE_PR
29ac9 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c  IVATE Table *sql
29aca 69 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53  ite3ResultSetOfS
29acb 65 6c 65 63 74 28 50 61 72 73 65 20 2a 70 50 61  elect(Parse *pPa
29acc 72 73 65 2c 20 53 65 6c 65 63 74 20 2a 70 53 65  rse, Select *pSe
29acd 6c 65 63 74 29 7b 0a 20 20 54 61 62 6c 65 20 2a  lect){.  Table *
29ace 70 54 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 20  pTab;.  sqlite3 
29acf 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
29ad0 3b 0a 20 20 69 6e 74 20 73 61 76 65 64 46 6c 61  ;.  int savedFla
29ad1 67 73 3b 0a 0a 20 20 73 61 76 65 64 46 6c 61 67  gs;..  savedFlag
29ad2 73 20 3d 20 64 62 2d 3e 66 6c 61 67 73 3b 0a 20  s = db->flags;. 
29ad3 20 64 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 53   db->flags &= ~S
29ad4 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d  QLITE_FullColNam
29ad5 65 73 3b 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20  es;.  db->flags 
29ad6 7c 3d 20 53 51 4c 49 54 45 5f 53 68 6f 72 74 43  |= SQLITE_ShortC
29ad7 6f 6c 4e 61 6d 65 73 3b 0a 20 20 73 71 6c 69 74  olNames;.  sqlit
29ad8 65 33 53 65 6c 65 63 74 50 72 65 70 28 70 50 61  e3SelectPrep(pPa
29ad9 72 73 65 2c 20 70 53 65 6c 65 63 74 2c 20 30 29  rse, pSelect, 0)
29ada 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ;.  if( pParse->
29adb 6e 45 72 72 20 29 20 72 65 74 75 72 6e 20 30 3b  nErr ) return 0;
29adc 0a 20 20 77 68 69 6c 65 28 20 70 53 65 6c 65 63  .  while( pSelec
29add 74 2d 3e 70 50 72 69 6f 72 20 29 20 70 53 65 6c  t->pPrior ) pSel
29ade 65 63 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70  ect = pSelect->p
29adf 50 72 69 6f 72 3b 0a 20 20 64 62 2d 3e 66 6c 61  Prior;.  db->fla
29ae0 67 73 20 3d 20 73 61 76 65 64 46 6c 61 67 73 3b  gs = savedFlags;
29ae1 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65  .  pTab = sqlite
29ae2 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
29ae3 2c 20 73 69 7a 65 6f 66 28 54 61 62 6c 65 29 20  , sizeof(Table) 
29ae4 29 3b 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30  );.  if( pTab==0
29ae5 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
29ae6 3b 0a 20 20 7d 0a 20 20 2f 2a 20 54 68 65 20 73  ;.  }.  /* The s
29ae7 71 6c 69 74 65 33 52 65 73 75 6c 74 53 65 74 4f  qlite3ResultSetO
29ae8 66 53 65 6c 65 63 74 28 29 20 69 73 20 6f 6e 6c  fSelect() is onl
29ae9 79 20 75 73 65 64 20 6e 20 63 6f 6e 74 65 78 74  y used n context
29aea 73 20 77 68 65 72 65 20 6c 6f 6f 6b 61 73 69 64  s where lookasid
29aeb 65 0a 20 20 2a 2a 20 69 73 20 64 69 73 61 62 6c  e.  ** is disabl
29aec 65 64 2c 20 73 6f 20 77 65 20 6d 69 67 68 74 20  ed, so we might 
29aed 61 73 20 77 65 6c 6c 20 68 61 72 64 2d 63 6f 64  as well hard-cod
29aee 65 20 70 54 61 62 2d 3e 64 62 4d 65 6d 20 74 6f  e pTab->dbMem to
29aef 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 61 73 73 65   NULL. */.  asse
29af0 72 74 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64  rt( db->lookasid
29af1 65 2e 62 45 6e 61 62 6c 65 64 3d 3d 30 20 29 3b  e.bEnabled==0 );
29af2 0a 20 20 70 54 61 62 2d 3e 64 62 4d 65 6d 20 3d  .  pTab->dbMem =
29af3 20 30 3b 0a 20 20 70 54 61 62 2d 3e 6e 52 65 66   0;.  pTab->nRef
29af4 20 3d 20 31 3b 0a 20 20 70 54 61 62 2d 3e 7a 4e   = 1;.  pTab->zN
29af5 61 6d 65 20 3d 20 30 3b 0a 20 20 73 65 6c 65 63  ame = 0;.  selec
29af6 74 43 6f 6c 75 6d 6e 73 46 72 6f 6d 45 78 70 72  tColumnsFromExpr
29af7 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 53 65  List(pParse, pSe
29af8 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2c 20 26 70  lect->pEList, &p
29af9 54 61 62 2d 3e 6e 43 6f 6c 2c 20 26 70 54 61 62  Tab->nCol, &pTab
29afa 2d 3e 61 43 6f 6c 29 3b 0a 20 20 73 65 6c 65 63  ->aCol);.  selec
29afb 74 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 41 6e  tAddColumnTypeAn
29afc 64 43 6f 6c 6c 61 74 69 6f 6e 28 70 50 61 72 73  dCollation(pPars
29afd 65 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20 70  e, pTab->nCol, p
29afe 54 61 62 2d 3e 61 43 6f 6c 2c 20 70 53 65 6c 65  Tab->aCol, pSele
29aff 63 74 29 3b 0a 20 20 70 54 61 62 2d 3e 69 50 4b  ct);.  pTab->iPK
29b00 65 79 20 3d 20 2d 31 3b 0a 20 20 69 66 28 20 64  ey = -1;.  if( d
29b01 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
29b02 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  ){.    sqlite3De
29b03 6c 65 74 65 54 61 62 6c 65 28 70 54 61 62 29 3b  leteTable(pTab);
29b04 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
29b05 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 54 61 62   }.  return pTab
29b06 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61  ;.}../*.** Get a
29b07 20 56 44 42 45 20 66 6f 72 20 74 68 65 20 67 69   VDBE for the gi
29b08 76 65 6e 20 70 61 72 73 65 72 20 63 6f 6e 74 65  ven parser conte
29b09 78 74 2e 20 20 43 72 65 61 74 65 20 61 20 6e 65  xt.  Create a ne
29b0a 77 20 6f 6e 65 20 69 66 20 6e 65 63 65 73 73 61  w one if necessa
29b0b 72 79 2e 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72  ry..** If an err
29b0c 6f 72 20 6f 63 63 75 72 73 2c 20 72 65 74 75 72  or occurs, retur
29b0d 6e 20 4e 55 4c 4c 20 61 6e 64 20 6c 65 61 76 65  n NULL and leave
29b0e 20 61 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50   a message in pP
29b0f 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  arse..*/.SQLITE_
29b10 50 52 49 56 41 54 45 20 56 64 62 65 20 2a 73 71  PRIVATE Vdbe *sq
29b11 6c 69 74 65 33 47 65 74 56 64 62 65 28 50 61 72  lite3GetVdbe(Par
29b12 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 56  se *pParse){.  V
29b13 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d  dbe *v = pParse-
29b14 3e 70 56 64 62 65 3b 0a 20 20 69 66 28 20 76 3d  >pVdbe;.  if( v=
29b15 3d 30 20 29 7b 0a 20 20 20 20 76 20 3d 20 70 50  =0 ){.    v = pP
29b16 61 72 73 65 2d 3e 70 56 64 62 65 20 3d 20 73 71  arse->pVdbe = sq
29b17 6c 69 74 65 33 56 64 62 65 43 72 65 61 74 65 28  lite3VdbeCreate(
29b18 70 50 61 72 73 65 2d 3e 64 62 29 3b 0a 23 69 66  pParse->db);.#if
29b19 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
29b1a 5f 54 52 41 43 45 0a 20 20 20 20 69 66 28 20 76  _TRACE.    if( v
29b1b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
29b1c 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f  3VdbeAddOp0(v, O
29b1d 50 5f 54 72 61 63 65 29 3b 0a 20 20 20 20 7d 0a  P_Trace);.    }.
29b1e 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72 65 74  #endif.  }.  ret
29b1f 75 72 6e 20 76 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  urn v;.}.../*.**
29b20 20 43 6f 6d 70 75 74 65 20 74 68 65 20 69 4c 69   Compute the iLi
29b21 6d 69 74 20 61 6e 64 20 69 4f 66 66 73 65 74 20  mit and iOffset 
29b22 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 53 45  fields of the SE
29b23 4c 45 43 54 20 62 61 73 65 64 20 6f 6e 20 74 68  LECT based on th
29b24 65 0a 2a 2a 20 70 4c 69 6d 69 74 20 61 6e 64 20  e.** pLimit and 
29b25 70 4f 66 66 73 65 74 20 65 78 70 72 65 73 73 69  pOffset expressi
29b26 6f 6e 73 2e 20 20 70 4c 69 6d 69 74 20 61 6e 64  ons.  pLimit and
29b27 20 70 4f 66 66 73 65 74 20 68 6f 6c 64 20 74 68   pOffset hold th
29b28 65 20 65 78 70 72 65 73 73 69 6f 6e 73 0a 2a 2a  e expressions.**
29b29 20 74 68 61 74 20 61 70 70 65 61 72 20 69 6e 20   that appear in 
29b2a 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c  the original SQL
29b2b 20 73 74 61 74 65 6d 65 6e 74 20 61 66 74 65 72   statement after
29b2c 20 74 68 65 20 4c 49 4d 49 54 20 61 6e 64 20 4f   the LIMIT and O
29b2d 46 46 53 45 54 0a 2a 2a 20 6b 65 79 77 6f 72 64  FFSET.** keyword
29b2e 73 2e 20 20 4f 72 20 4e 55 4c 4c 20 69 66 20 74  s.  Or NULL if t
29b2f 68 6f 73 65 20 6b 65 79 77 6f 72 64 73 20 61 72  hose keywords ar
29b30 65 20 6f 6d 69 74 74 65 64 2e 20 69 4c 69 6d 69  e omitted. iLimi
29b31 74 20 61 6e 64 20 69 4f 66 66 73 65 74 20 0a 2a  t and iOffset .*
29b32 2a 20 61 72 65 20 74 68 65 20 69 6e 74 65 67 65  * are the intege
29b33 72 20 6d 65 6d 6f 72 79 20 72 65 67 69 73 74 65  r memory registe
29b34 72 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 63 6f  r numbers for co
29b35 75 6e 74 65 72 73 20 75 73 65 64 20 74 6f 20 63  unters used to c
29b36 6f 6d 70 75 74 65 20 0a 2a 2a 20 74 68 65 20 6c  ompute .** the l
29b37 69 6d 69 74 20 61 6e 64 20 6f 66 66 73 65 74 2e  imit and offset.
29b38 20 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f    If there is no
29b39 20 6c 69 6d 69 74 20 61 6e 64 2f 6f 72 20 6f 66   limit and/or of
29b3a 66 73 65 74 2c 20 74 68 65 6e 20 0a 2a 2a 20 69  fset, then .** i
29b3b 4c 69 6d 69 74 20 61 6e 64 20 69 4f 66 66 73 65  Limit and iOffse
29b3c 74 20 61 72 65 20 6e 65 67 61 74 69 76 65 2e 0a  t are negative..
29b3d 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
29b3e 6e 65 20 63 68 61 6e 67 65 73 20 74 68 65 20 76  ne changes the v
29b3f 61 6c 75 65 73 20 6f 66 20 69 4c 69 6d 69 74 20  alues of iLimit 
29b40 61 6e 64 20 69 4f 66 66 73 65 74 20 6f 6e 6c 79  and iOffset only
29b41 20 69 66 0a 2a 2a 20 61 20 6c 69 6d 69 74 20 6f   if.** a limit o
29b42 72 20 6f 66 66 73 65 74 20 69 73 20 64 65 66 69  r offset is defi
29b43 6e 65 64 20 62 79 20 70 4c 69 6d 69 74 20 61 6e  ned by pLimit an
29b44 64 20 70 4f 66 66 73 65 74 2e 20 20 69 4c 69 6d  d pOffset.  iLim
29b45 69 74 20 61 6e 64 0a 2a 2a 20 69 4f 66 66 73 65  it and.** iOffse
29b46 74 20 73 68 6f 75 6c 64 20 68 61 76 65 20 62 65  t should have be
29b47 65 6e 20 70 72 65 73 65 74 20 74 6f 20 61 70 70  en preset to app
29b48 72 6f 70 72 69 61 74 65 20 64 65 66 61 75 6c 74  ropriate default
29b49 20 76 61 6c 75 65 73 0a 2a 2a 20 28 75 73 75 61   values.** (usua
29b4a 6c 6c 79 20 62 75 74 20 6e 6f 74 20 61 6c 77 61  lly but not alwa
29b4b 79 73 20 2d 31 29 20 70 72 69 6f 72 20 74 6f 20  ys -1) prior to 
29b4c 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75  calling this rou
29b4d 74 69 6e 65 2e 0a 2a 2a 20 4f 6e 6c 79 20 69 66  tine..** Only if
29b4e 20 70 4c 69 6d 69 74 21 3d 30 20 6f 72 20 70 4f   pLimit!=0 or pO
29b4f 66 66 73 65 74 21 3d 30 20 64 6f 20 74 68 65 20  ffset!=0 do the 
29b50 6c 69 6d 69 74 20 72 65 67 69 73 74 65 72 73 20  limit registers 
29b51 67 65 74 0a 2a 2a 20 72 65 64 65 66 69 6e 65 64  get.** redefined
29b52 2e 20 20 54 68 65 20 55 4e 49 4f 4e 20 41 4c 4c  .  The UNION ALL
29b53 20 6f 70 65 72 61 74 6f 72 20 75 73 65 73 20 74   operator uses t
29b54 68 69 73 20 70 72 6f 70 65 72 74 79 20 74 6f 20  his property to 
29b55 66 6f 72 63 65 0a 2a 2a 20 74 68 65 20 72 65 75  force.** the reu
29b56 73 65 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6c  se of the same l
29b57 69 6d 69 74 20 61 6e 64 20 6f 66 66 73 65 74 20  imit and offset 
29b58 72 65 67 69 73 74 65 72 73 20 61 63 72 6f 73 73  registers across
29b59 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 53 45 4c   multiple.** SEL
29b5a 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  ECT statements..
29b5b 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
29b5c 6f 6d 70 75 74 65 4c 69 6d 69 74 52 65 67 69 73  omputeLimitRegis
29b5d 74 65 72 73 28 50 61 72 73 65 20 2a 70 50 61 72  ters(Parse *pPar
29b5e 73 65 2c 20 53 65 6c 65 63 74 20 2a 70 2c 20 69  se, Select *p, i
29b5f 6e 74 20 69 42 72 65 61 6b 29 7b 0a 20 20 56 64  nt iBreak){.  Vd
29b60 62 65 20 2a 76 20 3d 20 30 3b 0a 20 20 69 6e 74  be *v = 0;.  int
29b61 20 69 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 69   iLimit = 0;.  i
29b62 6e 74 20 69 4f 66 66 73 65 74 3b 0a 20 20 69 6e  nt iOffset;.  in
29b63 74 20 61 64 64 72 31 3b 0a 20 20 69 66 28 20 70  t addr1;.  if( p
29b64 2d 3e 69 4c 69 6d 69 74 20 29 20 72 65 74 75 72  ->iLimit ) retur
29b65 6e 3b 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 22  n;..  /* .  ** "
29b66 4c 49 4d 49 54 20 2d 31 22 20 61 6c 77 61 79 73  LIMIT -1" always
29b67 20 73 68 6f 77 73 20 61 6c 6c 20 72 6f 77 73 2e   shows all rows.
29b68 20 20 54 68 65 72 65 20 69 73 20 73 6f 6d 65 0a    There is some.
29b69 20 20 2a 2a 20 63 6f 6e 74 72 61 76 65 72 73 79    ** contraversy
29b6a 20 61 62 6f 75 74 20 77 68 61 74 20 74 68 65 20   about what the 
29b6b 63 6f 72 72 65 63 74 20 62 65 68 61 76 69 6f 72  correct behavior
29b6c 20 73 68 6f 75 6c 64 20 62 65 2e 0a 20 20 2a 2a   should be..  **
29b6d 20 54 68 65 20 63 75 72 72 65 6e 74 20 69 6d 70   The current imp
29b6e 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 74 65  lementation inte
29b6f 72 70 72 65 74 73 20 22 4c 49 4d 49 54 20 30 22  rprets "LIMIT 0"
29b70 20 74 6f 20 6d 65 61 6e 0a 20 20 2a 2a 20 6e 6f   to mean.  ** no
29b71 20 72 6f 77 73 2e 0a 20 20 2a 2f 0a 20 20 73 71   rows..  */.  sq
29b72 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 43 6c  lite3ExprCacheCl
29b73 65 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20 61  ear(pParse);.  a
29b74 73 73 65 72 74 28 20 70 2d 3e 70 4f 66 66 73 65  ssert( p->pOffse
29b75 74 3d 3d 30 20 7c 7c 20 70 2d 3e 70 4c 69 6d 69  t==0 || p->pLimi
29b76 74 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d  t!=0 );.  if( p-
29b77 3e 70 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 70  >pLimit ){.    p
29b78 2d 3e 69 4c 69 6d 69 74 20 3d 20 69 4c 69 6d 69  ->iLimit = iLimi
29b79 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  t = ++pParse->nM
29b7a 65 6d 3b 0a 20 20 20 20 76 20 3d 20 73 71 6c 69  em;.    v = sqli
29b7b 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
29b7c 65 29 3b 0a 20 20 20 20 69 66 28 20 4e 45 56 45  e);.    if( NEVE
29b7d 52 28 76 3d 3d 30 29 20 29 20 72 65 74 75 72 6e  R(v==0) ) return
29b7e 3b 20 20 2f 2a 20 56 44 42 45 20 73 68 6f 75 6c  ;  /* VDBE shoul
29b7f 64 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62  d have already b
29b80 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f  een allocated */
29b81 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
29b82 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 2d 3e  Code(pParse, p->
29b83 70 4c 69 6d 69 74 2c 20 69 4c 69 6d 69 74 29 3b  pLimit, iLimit);
29b84 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
29b85 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75 73  AddOp1(v, OP_Mus
29b86 74 42 65 49 6e 74 2c 20 69 4c 69 6d 69 74 29 3b  tBeInt, iLimit);
29b87 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74  .    VdbeComment
29b88 28 28 76 2c 20 22 4c 49 4d 49 54 20 63 6f 75 6e  ((v, "LIMIT coun
29b89 74 65 72 22 29 29 3b 0a 20 20 20 20 73 71 6c 69  ter"));.    sqli
29b8a 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
29b8b 20 4f 50 5f 49 66 5a 65 72 6f 2c 20 69 4c 69 6d   OP_IfZero, iLim
29b8c 69 74 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 20  it, iBreak);.   
29b8d 20 69 66 28 20 70 2d 3e 70 4f 66 66 73 65 74 20   if( p->pOffset 
29b8e 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 4f 66 66  ){.      p->iOff
29b8f 73 65 74 20 3d 20 69 4f 66 66 73 65 74 20 3d 20  set = iOffset = 
29b90 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
29b91 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d        pParse->nM
29b92 65 6d 2b 2b 3b 20 20 20 2f 2a 20 41 6c 6c 6f 63  em++;   /* Alloc
29b93 61 74 65 20 61 6e 20 65 78 74 72 61 20 72 65 67  ate an extra reg
29b94 69 73 74 65 72 20 66 6f 72 20 6c 69 6d 69 74 2b  ister for limit+
29b95 6f 66 66 73 65 74 20 2a 2f 0a 20 20 20 20 20 20  offset */.      
29b96 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
29b97 70 50 61 72 73 65 2c 20 70 2d 3e 70 4f 66 66 73  pParse, p->pOffs
29b98 65 74 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20 20  et, iOffset);.  
29b99 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29b9a 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74  ddOp1(v, OP_Must
29b9b 42 65 49 6e 74 2c 20 69 4f 66 66 73 65 74 29 3b  BeInt, iOffset);
29b9c 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  .      VdbeComme
29b9d 6e 74 28 28 76 2c 20 22 4f 46 46 53 45 54 20 63  nt((v, "OFFSET c
29b9e 6f 75 6e 74 65 72 22 29 29 3b 0a 20 20 20 20 20  ounter"));.     
29b9f 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74 65 33   addr1 = sqlite3
29ba0 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
29ba1 5f 49 66 50 6f 73 2c 20 69 4f 66 66 73 65 74 29  _IfPos, iOffset)
29ba2 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
29ba3 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
29ba4 49 6e 74 65 67 65 72 2c 20 30 2c 20 69 4f 66 66  Integer, 0, iOff
29ba5 73 65 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  set);.      sqli
29ba6 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
29ba7 76 2c 20 61 64 64 72 31 29 3b 0a 20 20 20 20 20  v, addr1);.     
29ba8 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29ba9 70 33 28 76 2c 20 4f 50 5f 41 64 64 2c 20 69 4c  p3(v, OP_Add, iL
29baa 69 6d 69 74 2c 20 69 4f 66 66 73 65 74 2c 20 69  imit, iOffset, i
29bab 4f 66 66 73 65 74 2b 31 29 3b 0a 20 20 20 20 20  Offset+1);.     
29bac 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
29bad 20 22 4c 49 4d 49 54 2b 4f 46 46 53 45 54 22 29   "LIMIT+OFFSET")
29bae 29 3b 0a 20 20 20 20 20 20 61 64 64 72 31 20 3d  );.      addr1 =
29baf 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29bb0 70 31 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20  p1(v, OP_IfPos, 
29bb1 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20 20 20 73  iLimit);.      s
29bb2 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
29bb3 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
29bb4 2d 31 2c 20 69 4f 66 66 73 65 74 2b 31 29 3b 0a  -1, iOffset+1);.
29bb5 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29bb6 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
29bb7 72 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  r1);.    }.  }.}
29bb8 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
29bb9 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53  _OMIT_COMPOUND_S
29bba 45 4c 45 43 54 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ELECT./*.** Retu
29bbb 72 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  rn the appropria
29bbc 74 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  te collating seq
29bbd 75 65 6e 63 65 20 66 6f 72 20 74 68 65 20 69 43  uence for the iC
29bbe 6f 6c 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 0a  ol-th column of.
29bbf 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  ** the result se
29bc0 74 20 66 6f 72 20 74 68 65 20 63 6f 6d 70 6f 75  t for the compou
29bc1 6e 64 2d 73 65 6c 65 63 74 20 73 74 61 74 65 6d  nd-select statem
29bc2 65 6e 74 20 22 70 22 2e 20 20 52 65 74 75 72 6e  ent "p".  Return
29bc3 20 4e 55 4c 4c 20 69 66 0a 2a 2a 20 74 68 65 20   NULL if.** the 
29bc4 63 6f 6c 75 6d 6e 20 68 61 73 20 6e 6f 20 64 65  column has no de
29bc5 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6e 67 20  fault collating 
29bc6 73 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20  sequence..**.** 
29bc7 54 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  The collating se
29bc8 71 75 65 6e 63 65 20 66 6f 72 20 74 68 65 20 63  quence for the c
29bc9 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 20 69  ompound select i
29bca 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65  s taken from the
29bcb 0a 2a 2a 20 6c 65 66 74 2d 6d 6f 73 74 20 74 65  .** left-most te
29bcc 72 6d 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74  rm of the select
29bcd 20 74 68 61 74 20 68 61 73 20 61 20 63 6f 6c 6c   that has a coll
29bce 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 2e 0a  ating sequence..
29bcf 2a 2f 0a 73 74 61 74 69 63 20 43 6f 6c 6c 53 65  */.static CollSe
29bd0 71 20 2a 6d 75 6c 74 69 53 65 6c 65 63 74 43 6f  q *multiSelectCo
29bd1 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 70 50 61  llSeq(Parse *pPa
29bd2 72 73 65 2c 20 53 65 6c 65 63 74 20 2a 70 2c 20  rse, Select *p, 
29bd3 69 6e 74 20 69 43 6f 6c 29 7b 0a 20 20 43 6f 6c  int iCol){.  Col
29bd4 6c 53 65 71 20 2a 70 52 65 74 3b 0a 20 20 69 66  lSeq *pRet;.  if
29bd5 28 20 70 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20  ( p->pPrior ){. 
29bd6 20 20 20 70 52 65 74 20 3d 20 6d 75 6c 74 69 53     pRet = multiS
29bd7 65 6c 65 63 74 43 6f 6c 6c 53 65 71 28 70 50 61  electCollSeq(pPa
29bd8 72 73 65 2c 20 70 2d 3e 70 50 72 69 6f 72 2c 20  rse, p->pPrior, 
29bd9 69 43 6f 6c 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  iCol);.  }else{.
29bda 20 20 20 20 70 52 65 74 20 3d 20 30 3b 0a 20 20      pRet = 0;.  
29bdb 7d 0a 20 20 61 73 73 65 72 74 28 20 69 43 6f 6c  }.  assert( iCol
29bdc 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 52 65  >=0 );.  if( pRe
29bdd 74 3d 3d 30 20 26 26 20 69 43 6f 6c 3c 70 2d 3e  t==0 && iCol<p->
29bde 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b  pEList->nExpr ){
29bdf 0a 20 20 20 20 70 52 65 74 20 3d 20 73 71 6c 69  .    pRet = sqli
29be0 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70  te3ExprCollSeq(p
29be1 50 61 72 73 65 2c 20 70 2d 3e 70 45 4c 69 73 74  Parse, p->pEList
29be2 2d 3e 61 5b 69 43 6f 6c 5d 2e 70 45 78 70 72 29  ->a[iCol].pExpr)
29be3 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
29be4 52 65 74 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  Ret;.}.#endif /*
29be5 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d   SQLITE_OMIT_COM
29be6 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 2a 2f 0a  POUND_SELECT */.
29be7 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65  ./* Forward refe
29be8 72 65 6e 63 65 20 2a 2f 0a 73 74 61 74 69 63 20  rence */.static 
29be9 69 6e 74 20 6d 75 6c 74 69 53 65 6c 65 63 74 4f  int multiSelectO
29bea 72 64 65 72 42 79 28 0a 20 20 50 61 72 73 65 20  rderBy(.  Parse 
29beb 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
29bec 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
29bed 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  xt */.  Select *
29bee 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  p,            /*
29bef 20 54 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20   The right-most 
29bf0 6f 66 20 53 45 4c 45 43 54 73 20 74 6f 20 62 65  of SELECTs to be
29bf1 20 63 6f 64 65 64 20 2a 2f 0a 20 20 53 65 6c 65   coded */.  Sele
29bf2 63 74 44 65 73 74 20 2a 70 44 65 73 74 20 20 20  ctDest *pDest   
29bf3 20 20 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f 20    /* What to do 
29bf4 77 69 74 68 20 71 75 65 72 79 20 72 65 73 75 6c  with query resul
29bf5 74 73 20 2a 2f 0a 29 3b 0a 0a 0a 23 69 66 6e 64  ts */.);...#ifnd
29bf6 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ef SQLITE_OMIT_C
29bf7 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a 2f  OMPOUND_SELECT./
29bf8 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
29bf9 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 70  e is called to p
29bfa 72 6f 63 65 73 73 20 61 20 63 6f 6d 70 6f 75 6e  rocess a compoun
29bfb 64 20 71 75 65 72 79 20 66 6f 72 6d 20 66 72 6f  d query form fro
29bfc 6d 0a 2a 2a 20 74 77 6f 20 6f 72 20 6d 6f 72 65  m.** two or more
29bfd 20 73 65 70 61 72 61 74 65 20 71 75 65 72 69 65   separate querie
29bfe 73 20 75 73 69 6e 67 20 55 4e 49 4f 4e 2c 20 55  s using UNION, U
29bff 4e 49 4f 4e 20 41 4c 4c 2c 20 45 58 43 45 50 54  NION ALL, EXCEPT
29c00 2c 20 6f 72 0a 2a 2a 20 49 4e 54 45 52 53 45 43  , or.** INTERSEC
29c01 54 0a 2a 2a 0a 2a 2a 20 22 70 22 20 70 6f 69 6e  T.**.** "p" poin
29c02 74 73 20 74 6f 20 74 68 65 20 72 69 67 68 74 2d  ts to the right-
29c03 6d 6f 73 74 20 6f 66 20 74 68 65 20 74 77 6f 20  most of the two 
29c04 71 75 65 72 69 65 73 2e 20 20 74 68 65 20 71 75  queries.  the qu
29c05 65 72 79 20 6f 6e 20 74 68 65 0a 2a 2a 20 6c 65  ery on the.** le
29c06 66 74 20 69 73 20 70 2d 3e 70 50 72 69 6f 72 2e  ft is p->pPrior.
29c07 20 20 54 68 65 20 6c 65 66 74 20 71 75 65 72 79    The left query
29c08 20 63 6f 75 6c 64 20 61 6c 73 6f 20 62 65 20 61   could also be a
29c09 20 63 6f 6d 70 6f 75 6e 64 20 71 75 65 72 79 0a   compound query.
29c0a 2a 2a 20 69 6e 20 77 68 69 63 68 20 63 61 73 65  ** in which case
29c0b 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69   this routine wi
29c0c 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 72 65 63  ll be called rec
29c0d 75 72 73 69 76 65 6c 79 2e 20 0a 2a 2a 0a 2a 2a  ursively. .**.**
29c0e 20 54 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20   The results of 
29c0f 74 68 65 20 74 6f 74 61 6c 20 71 75 65 72 79 20  the total query 
29c10 61 72 65 20 74 6f 20 62 65 20 77 72 69 74 74 65  are to be writte
29c11 6e 20 69 6e 74 6f 20 61 20 64 65 73 74 69 6e 61  n into a destina
29c12 74 69 6f 6e 0a 2a 2a 20 6f 66 20 74 79 70 65 20  tion.** of type 
29c13 65 44 65 73 74 20 77 69 74 68 20 70 61 72 61 6d  eDest with param
29c14 65 74 65 72 20 69 50 61 72 6d 2e 0a 2a 2a 0a 2a  eter iParm..**.*
29c15 2a 20 45 78 61 6d 70 6c 65 20 31 3a 20 20 43 6f  * Example 1:  Co
29c16 6e 73 69 64 65 72 20 61 20 74 68 72 65 65 2d 77  nsider a three-w
29c17 61 79 20 63 6f 6d 70 6f 75 6e 64 20 53 51 4c 20  ay compound SQL 
29c18 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a  statement..**.**
29c19 20 20 20 20 20 53 45 4c 45 43 54 20 61 20 46 52       SELECT a FR
29c1a 4f 4d 20 74 31 20 55 4e 49 4f 4e 20 53 45 4c 45  OM t1 UNION SELE
29c1b 43 54 20 62 20 46 52 4f 4d 20 74 32 20 55 4e 49  CT b FROM t2 UNI
29c1c 4f 4e 20 53 45 4c 45 43 54 20 63 20 46 52 4f 4d  ON SELECT c FROM
29c1d 20 74 33 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73   t3.**.** This s
29c1e 74 61 74 65 6d 65 6e 74 20 69 73 20 70 61 72 73  tatement is pars
29c1f 65 64 20 75 70 20 61 73 20 66 6f 6c 6c 6f 77 73  ed up as follows
29c20 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45  :.**.**     SELE
29c21 43 54 20 63 20 46 52 4f 4d 20 74 33 0a 2a 2a 20  CT c FROM t3.** 
29c22 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20 20 60       |.**      `
29c23 2d 2d 2d 2d 2d 3e 20 20 53 45 4c 45 43 54 20 62  ----->  SELECT b
29c24 20 46 52 4f 4d 20 74 32 0a 2a 2a 20 20 20 20 20   FROM t2.**     
29c25 20 20 20 20 20 20 20 20 20 20 20 7c 0a 2a 2a 20             |.** 
29c26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 60                 `
29c27 2d 2d 2d 2d 2d 2d 3e 20 20 53 45 4c 45 43 54 20  ------>  SELECT 
29c28 61 20 46 52 4f 4d 20 74 31 0a 2a 2a 0a 2a 2a 20  a FROM t1.**.** 
29c29 54 68 65 20 61 72 72 6f 77 73 20 69 6e 20 74 68  The arrows in th
29c2a 65 20 64 69 61 67 72 61 6d 20 61 62 6f 76 65 20  e diagram above 
29c2b 72 65 70 72 65 73 65 6e 74 20 74 68 65 20 53 65  represent the Se
29c2c 6c 65 63 74 2e 70 50 72 69 6f 72 20 70 6f 69 6e  lect.pPrior poin
29c2d 74 65 72 2e 0a 2a 2a 20 53 6f 20 69 66 20 74 68  ter..** So if th
29c2e 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
29c2f 6c 6c 65 64 20 77 69 74 68 20 70 20 65 71 75 61  lled with p equa
29c30 6c 20 74 6f 20 74 68 65 20 74 33 20 71 75 65 72  l to the t3 quer
29c31 79 2c 20 74 68 65 6e 0a 2a 2a 20 70 50 72 69 6f  y, then.** pPrio
29c32 72 20 77 69 6c 6c 20 62 65 20 74 68 65 20 74 32  r will be the t2
29c33 20 71 75 65 72 79 2e 20 20 70 2d 3e 6f 70 20 77   query.  p->op w
29c34 69 6c 6c 20 62 65 20 54 4b 5f 55 4e 49 4f 4e 20  ill be TK_UNION 
29c35 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a  in this case..**
29c36 0a 2a 2a 20 4e 6f 74 69 63 65 20 74 68 61 74 20  .** Notice that 
29c37 62 65 63 61 75 73 65 20 6f 66 20 74 68 65 20 77  because of the w
29c38 61 79 20 53 51 4c 69 74 65 20 70 61 72 73 65 73  ay SQLite parses
29c39 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54   compound SELECT
29c3a 73 2c 20 74 68 65 0a 2a 2a 20 69 6e 64 69 76 69  s, the.** indivi
29c3b 64 75 61 6c 20 73 65 6c 65 63 74 73 20 61 6c 77  dual selects alw
29c3c 61 79 73 20 67 72 6f 75 70 20 66 72 6f 6d 20 6c  ays group from l
29c3d 65 66 74 20 74 6f 20 72 69 67 68 74 2e 0a 2a 2f  eft to right..*/
29c3e 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 75 6c 74  .static int mult
29c3f 69 53 65 6c 65 63 74 28 0a 20 20 50 61 72 73 65  iSelect(.  Parse
29c40 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
29c41 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
29c42 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  ext */.  Select 
29c43 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  *p,            /
29c44 2a 20 54 68 65 20 72 69 67 68 74 2d 6d 6f 73 74  * The right-most
29c45 20 6f 66 20 53 45 4c 45 43 54 73 20 74 6f 20 62   of SELECTs to b
29c46 65 20 63 6f 64 65 64 20 2a 2f 0a 20 20 53 65 6c  e coded */.  Sel
29c47 65 63 74 44 65 73 74 20 2a 70 44 65 73 74 20 20  ectDest *pDest  
29c48 20 20 20 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f     /* What to do
29c49 20 77 69 74 68 20 71 75 65 72 79 20 72 65 73 75   with query resu
29c4a 6c 74 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  lts */.){.  int 
29c4b 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  rc = SQLITE_OK; 
29c4c 20 20 2f 2a 20 53 75 63 63 65 73 73 20 63 6f 64    /* Success cod
29c4d 65 20 66 72 6f 6d 20 61 20 73 75 62 72 6f 75 74  e from a subrout
29c4e 69 6e 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  ine */.  Select 
29c4f 2a 70 50 72 69 6f 72 3b 20 20 20 20 20 20 20 2f  *pPrior;       /
29c50 2a 20 41 6e 6f 74 68 65 72 20 53 45 4c 45 43 54  * Another SELECT
29c51 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20   immediately to 
29c52 6f 75 72 20 6c 65 66 74 20 2a 2f 0a 20 20 56 64  our left */.  Vd
29c53 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20  be *v;          
29c54 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20      /* Generate 
29c55 63 6f 64 65 20 74 6f 20 74 68 69 73 20 56 44 42  code to this VDB
29c56 45 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73  E */.  SelectDes
29c57 74 20 64 65 73 74 3b 20 20 20 20 20 20 2f 2a 20  t dest;      /* 
29c58 41 6c 74 65 72 6e 61 74 69 76 65 20 64 61 74 61  Alternative data
29c59 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 2a 2f 0a   destination */.
29c5a 20 20 53 65 6c 65 63 74 20 2a 70 44 65 6c 65 74    Select *pDelet
29c5b 65 20 3d 20 30 3b 20 20 2f 2a 20 43 68 61 69 6e  e = 0;  /* Chain
29c5c 20 6f 66 20 73 69 6d 70 6c 65 20 73 65 6c 65 63   of simple selec
29c5d 74 73 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a  ts to delete */.
29c5e 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
29c5f 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
29c60 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
29c61 2f 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72  /..  /* Make sur
29c62 65 20 74 68 65 72 65 20 69 73 20 6e 6f 20 4f 52  e there is no OR
29c63 44 45 52 20 42 59 20 6f 72 20 4c 49 4d 49 54 20  DER BY or LIMIT 
29c64 63 6c 61 75 73 65 20 6f 6e 20 70 72 69 6f 72 20  clause on prior 
29c65 53 45 4c 45 43 54 73 2e 20 20 4f 6e 6c 79 0a 20  SELECTs.  Only. 
29c66 20 2a 2a 20 74 68 65 20 6c 61 73 74 20 28 72 69   ** the last (ri
29c67 67 68 74 2d 6d 6f 73 74 29 20 53 45 4c 45 43 54  ght-most) SELECT
29c68 20 69 6e 20 74 68 65 20 73 65 72 69 65 73 20 6d   in the series m
29c69 61 79 20 68 61 76 65 20 61 6e 20 4f 52 44 45 52  ay have an ORDER
29c6a 20 42 59 20 6f 72 20 4c 49 4d 49 54 2e 0a 20 20   BY or LIMIT..  
29c6b 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 20 26  */.  assert( p &
29c6c 26 20 70 2d 3e 70 50 72 69 6f 72 20 29 3b 20 20  & p->pPrior );  
29c6d 2f 2a 20 43 61 6c 6c 69 6e 67 20 66 75 6e 63 74  /* Calling funct
29c6e 69 6f 6e 20 67 75 61 72 61 6e 74 65 65 73 20 74  ion guarantees t
29c6f 68 69 73 20 6d 75 63 68 20 2a 2f 0a 20 20 64 62  his much */.  db
29c70 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
29c71 20 70 50 72 69 6f 72 20 3d 20 70 2d 3e 70 50 72   pPrior = p->pPr
29c72 69 6f 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ior;.  assert( p
29c73 50 72 69 6f 72 2d 3e 70 52 69 67 68 74 6d 6f 73  Prior->pRightmos
29c74 74 21 3d 70 50 72 69 6f 72 20 29 3b 0a 20 20 61  t!=pPrior );.  a
29c75 73 73 65 72 74 28 20 70 50 72 69 6f 72 2d 3e 70  ssert( pPrior->p
29c76 52 69 67 68 74 6d 6f 73 74 3d 3d 70 2d 3e 70 52  Rightmost==p->pR
29c77 69 67 68 74 6d 6f 73 74 20 29 3b 0a 20 20 64 65  ightmost );.  de
29c78 73 74 20 3d 20 2a 70 44 65 73 74 3b 0a 20 20 69  st = *pDest;.  i
29c79 66 28 20 70 50 72 69 6f 72 2d 3e 70 4f 72 64 65  f( pPrior->pOrde
29c7a 72 42 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  rBy ){.    sqlit
29c7b 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
29c7c 65 2c 22 4f 52 44 45 52 20 42 59 20 63 6c 61 75  e,"ORDER BY clau
29c7d 73 65 20 73 68 6f 75 6c 64 20 63 6f 6d 65 20 61  se should come a
29c7e 66 74 65 72 20 25 73 20 6e 6f 74 20 62 65 66 6f  fter %s not befo
29c7f 72 65 22 2c 0a 20 20 20 20 20 20 73 65 6c 65 63  re",.      selec
29c80 74 4f 70 4e 61 6d 65 28 70 2d 3e 6f 70 29 29 3b  tOpName(p->op));
29c81 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20  .    rc = 1;.   
29c82 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65   goto multi_sele
29c83 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a 20 20 69 66  ct_end;.  }.  if
29c84 28 20 70 50 72 69 6f 72 2d 3e 70 4c 69 6d 69 74  ( pPrior->pLimit
29c85 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
29c86 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 22  rrorMsg(pParse,"
29c87 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 73 68 6f  LIMIT clause sho
29c88 75 6c 64 20 63 6f 6d 65 20 61 66 74 65 72 20 25  uld come after %
29c89 73 20 6e 6f 74 20 62 65 66 6f 72 65 22 2c 0a 20  s not before",. 
29c8a 20 20 20 20 20 73 65 6c 65 63 74 4f 70 4e 61 6d       selectOpNam
29c8b 65 28 70 2d 3e 6f 70 29 29 3b 0a 20 20 20 20 72  e(p->op));.    r
29c8c 63 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74 6f 20  c = 1;.    goto 
29c8d 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64  multi_select_end
29c8e 3b 0a 20 20 7d 0a 0a 20 20 76 20 3d 20 73 71 6c  ;.  }..  v = sql
29c8f 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
29c90 73 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 76  se);.  assert( v
29c91 21 3d 30 20 29 3b 20 20 2f 2a 20 54 68 65 20 56  !=0 );  /* The V
29c92 44 42 45 20 61 6c 72 65 61 64 79 20 63 72 65 61  DBE already crea
29c93 74 65 64 20 62 79 20 63 61 6c 6c 69 6e 67 20 66  ted by calling f
29c94 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a  unction */..  /*
29c95 20 43 72 65 61 74 65 20 74 68 65 20 64 65 73 74   Create the dest
29c96 69 6e 61 74 69 6f 6e 20 74 65 6d 70 6f 72 61 72  ination temporar
29c97 79 20 74 61 62 6c 65 20 69 66 20 6e 65 63 65 73  y table if neces
29c98 73 61 72 79 0a 20 20 2a 2f 0a 20 20 69 66 28 20  sary.  */.  if( 
29c99 64 65 73 74 2e 65 44 65 73 74 3d 3d 53 52 54 5f  dest.eDest==SRT_
29c9a 45 70 68 65 6d 54 61 62 20 29 7b 0a 20 20 20 20  EphemTab ){.    
29c9b 61 73 73 65 72 74 28 20 70 2d 3e 70 45 4c 69 73  assert( p->pELis
29c9c 74 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  t );.    sqlite3
29c9d 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
29c9e 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20  _OpenEphemeral, 
29c9f 64 65 73 74 2e 69 50 61 72 6d 2c 20 70 2d 3e 70  dest.iParm, p->p
29ca0 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 3b 0a 20  EList->nExpr);. 
29ca1 20 20 20 64 65 73 74 2e 65 44 65 73 74 20 3d 20     dest.eDest = 
29ca2 53 52 54 5f 54 61 62 6c 65 3b 0a 20 20 7d 0a 0a  SRT_Table;.  }..
29ca3 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 61    /* Make sure a
29ca4 6c 6c 20 53 45 4c 45 43 54 73 20 69 6e 20 74 68  ll SELECTs in th
29ca5 65 20 73 74 61 74 65 6d 65 6e 74 20 68 61 76 65  e statement have
29ca6 20 74 68 65 20 73 61 6d 65 20 6e 75 6d 62 65 72   the same number
29ca7 20 6f 66 20 65 6c 65 6d 65 6e 74 73 0a 20 20 2a   of elements.  *
29ca8 2a 20 69 6e 20 74 68 65 69 72 20 72 65 73 75 6c  * in their resul
29ca9 74 20 73 65 74 73 2e 0a 20 20 2a 2f 0a 20 20 61  t sets..  */.  a
29caa 73 73 65 72 74 28 20 70 2d 3e 70 45 4c 69 73 74  ssert( p->pEList
29cab 20 26 26 20 70 50 72 69 6f 72 2d 3e 70 45 4c 69   && pPrior->pELi
29cac 73 74 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70  st );.  if( p->p
29cad 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 70 50  EList->nExpr!=pP
29cae 72 69 6f 72 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45  rior->pEList->nE
29caf 78 70 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  xpr ){.    sqlit
29cb0 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
29cb1 65 2c 20 22 53 45 4c 45 43 54 73 20 74 6f 20 74  e, "SELECTs to t
29cb2 68 65 20 6c 65 66 74 20 61 6e 64 20 72 69 67 68  he left and righ
29cb3 74 20 6f 66 20 25 73 22 0a 20 20 20 20 20 20 22  t of %s".      "
29cb4 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 74 68 65   do not have the
29cb5 20 73 61 6d 65 20 6e 75 6d 62 65 72 20 6f 66 20   same number of 
29cb6 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 22 2c  result columns",
29cb7 20 73 65 6c 65 63 74 4f 70 4e 61 6d 65 28 70 2d   selectOpName(p-
29cb8 3e 6f 70 29 29 3b 0a 20 20 20 20 72 63 20 3d 20  >op));.    rc = 
29cb9 31 3b 0a 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74  1;.    goto mult
29cba 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20  i_select_end;.  
29cbb 7d 0a 0a 20 20 2f 2a 20 43 6f 6d 70 6f 75 6e 64  }..  /* Compound
29cbc 20 53 45 4c 45 43 54 73 20 74 68 61 74 20 68 61   SELECTs that ha
29cbd 76 65 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63  ve an ORDER BY c
29cbe 6c 61 75 73 65 20 61 72 65 20 68 61 6e 64 6c 65  lause are handle
29cbf 64 20 73 65 70 61 72 61 74 65 6c 79 2e 0a 20 20  d separately..  
29cc0 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 4f 72 64  */.  if( p->pOrd
29cc1 65 72 42 79 20 29 7b 0a 20 20 20 20 72 65 74 75  erBy ){.    retu
29cc2 72 6e 20 6d 75 6c 74 69 53 65 6c 65 63 74 4f 72  rn multiSelectOr
29cc3 64 65 72 42 79 28 70 50 61 72 73 65 2c 20 70 2c  derBy(pParse, p,
29cc4 20 70 44 65 73 74 29 3b 0a 20 20 7d 0a 0a 20 20   pDest);.  }..  
29cc5 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
29cc6 20 66 6f 72 20 74 68 65 20 6c 65 66 74 20 61 6e   for the left an
29cc7 64 20 72 69 67 68 74 20 53 45 4c 45 43 54 20 73  d right SELECT s
29cc8 74 61 74 65 6d 65 6e 74 73 2e 0a 20 20 2a 2f 0a  tatements..  */.
29cc9 20 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70 20    switch( p->op 
29cca 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41  ){.    case TK_A
29ccb 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  LL: {.      int 
29ccc 61 64 64 72 20 3d 20 30 3b 0a 20 20 20 20 20 20  addr = 0;.      
29ccd 61 73 73 65 72 74 28 20 21 70 50 72 69 6f 72 2d  assert( !pPrior-
29cce 3e 70 4c 69 6d 69 74 20 29 3b 0a 20 20 20 20 20  >pLimit );.     
29ccf 20 70 50 72 69 6f 72 2d 3e 70 4c 69 6d 69 74 20   pPrior->pLimit 
29cd0 3d 20 70 2d 3e 70 4c 69 6d 69 74 3b 0a 20 20 20  = p->pLimit;.   
29cd1 20 20 20 70 50 72 69 6f 72 2d 3e 70 4f 66 66 73     pPrior->pOffs
29cd2 65 74 20 3d 20 70 2d 3e 70 4f 66 66 73 65 74 3b  et = p->pOffset;
29cd3 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
29cd4 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65  te3Select(pParse
29cd5 2c 20 70 50 72 69 6f 72 2c 20 26 64 65 73 74 29  , pPrior, &dest)
29cd6 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d 69  ;.      p->pLimi
29cd7 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e  t = 0;.      p->
29cd8 70 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20  pOffset = 0;.   
29cd9 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20     if( rc ){.   
29cda 20 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f       goto multi_
29cdb 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20  select_end;.    
29cdc 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e 70 50 72    }.      p->pPr
29cdd 69 6f 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  ior = 0;.      p
29cde 2d 3e 69 4c 69 6d 69 74 20 3d 20 70 50 72 69 6f  ->iLimit = pPrio
29cdf 72 2d 3e 69 4c 69 6d 69 74 3b 0a 20 20 20 20 20  r->iLimit;.     
29ce0 20 70 2d 3e 69 4f 66 66 73 65 74 20 3d 20 70 50   p->iOffset = pP
29ce1 72 69 6f 72 2d 3e 69 4f 66 66 73 65 74 3b 0a 20  rior->iOffset;. 
29ce2 20 20 20 20 20 69 66 28 20 70 2d 3e 69 4c 69 6d       if( p->iLim
29ce3 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 61 64  it ){.        ad
29ce4 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
29ce5 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 5a  AddOp1(v, OP_IfZ
29ce6 65 72 6f 2c 20 70 2d 3e 69 4c 69 6d 69 74 29 3b  ero, p->iLimit);
29ce7 0a 20 20 20 20 20 20 20 20 56 64 62 65 43 6f 6d  .        VdbeCom
29ce8 6d 65 6e 74 28 28 76 2c 20 22 4a 75 6d 70 20 61  ment((v, "Jump a
29ce9 68 65 61 64 20 69 66 20 4c 49 4d 49 54 20 72 65  head if LIMIT re
29cea 61 63 68 65 64 22 29 29 3b 0a 20 20 20 20 20 20  ached"));.      
29ceb 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  }.      rc = sql
29cec 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73  ite3Select(pPars
29ced 65 2c 20 70 2c 20 26 64 65 73 74 29 3b 0a 20 20  e, p, &dest);.  
29cee 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63      testcase( rc
29cef 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20  !=SQLITE_OK );. 
29cf0 20 20 20 20 20 70 44 65 6c 65 74 65 20 3d 20 70       pDelete = p
29cf1 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20  ->pPrior;.      
29cf2 70 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50 72 69  p->pPrior = pPri
29cf3 6f 72 3b 0a 20 20 20 20 20 20 69 66 28 20 61 64  or;.      if( ad
29cf4 64 72 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  dr ){.        sq
29cf5 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
29cf6 65 28 76 2c 20 61 64 64 72 29 3b 0a 20 20 20 20  e(v, addr);.    
29cf7 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b    }.      break;
29cf8 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
29cf9 54 4b 5f 45 58 43 45 50 54 3a 0a 20 20 20 20 63  TK_EXCEPT:.    c
29cfa 61 73 65 20 54 4b 5f 55 4e 49 4f 4e 3a 20 7b 0a  ase TK_UNION: {.
29cfb 20 20 20 20 20 20 69 6e 74 20 75 6e 69 6f 6e 54        int unionT
29cfc 61 62 3b 20 20 20 20 2f 2a 20 43 75 72 73 6f 72  ab;    /* Cursor
29cfd 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 74   number of the t
29cfe 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 68  emporary table h
29cff 6f 6c 64 69 6e 67 20 72 65 73 75 6c 74 20 2a 2f  olding result */
29d00 0a 20 20 20 20 20 20 75 38 20 6f 70 20 3d 20 30  .      u8 op = 0
29d01 3b 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f  ;       /* One o
29d02 66 20 74 68 65 20 53 52 54 5f 20 6f 70 65 72 61  f the SRT_ opera
29d03 74 69 6f 6e 73 20 74 6f 20 61 70 70 6c 79 20 74  tions to apply t
29d04 6f 20 73 65 6c 66 20 2a 2f 0a 20 20 20 20 20 20  o self */.      
29d05 69 6e 74 20 70 72 69 6f 72 4f 70 3b 20 20 20 20  int priorOp;    
29d06 20 2f 2a 20 54 68 65 20 53 52 54 5f 20 6f 70 65   /* The SRT_ ope
29d07 72 61 74 69 6f 6e 20 74 6f 20 61 70 70 6c 79 20  ration to apply 
29d08 74 6f 20 70 72 69 6f 72 20 73 65 6c 65 63 74 73  to prior selects
29d09 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a   */.      Expr *
29d0a 70 4c 69 6d 69 74 2c 20 2a 70 4f 66 66 73 65 74  pLimit, *pOffset
29d0b 3b 20 2f 2a 20 53 61 76 65 64 20 76 61 6c 75 65  ; /* Saved value
29d0c 73 20 6f 66 20 70 2d 3e 6e 4c 69 6d 69 74 20 61  s of p->nLimit a
29d0d 6e 64 20 70 2d 3e 6e 4f 66 66 73 65 74 20 2a 2f  nd p->nOffset */
29d0e 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 3b  .      int addr;
29d0f 0a 20 20 20 20 20 20 53 65 6c 65 63 74 44 65 73  .      SelectDes
29d10 74 20 75 6e 69 6f 6e 64 65 73 74 3b 0a 0a 20 20  t uniondest;..  
29d11 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 2d      testcase( p-
29d12 3e 6f 70 3d 3d 54 4b 5f 45 58 43 45 50 54 20 29  >op==TK_EXCEPT )
29d13 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
29d14 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 55 4e 49 4f  ( p->op==TK_UNIO
29d15 4e 20 29 3b 0a 20 20 20 20 20 20 70 72 69 6f 72  N );.      prior
29d16 4f 70 20 3d 20 53 52 54 5f 55 6e 69 6f 6e 3b 0a  Op = SRT_Union;.
29d17 20 20 20 20 20 20 69 66 28 20 64 65 73 74 2e 65        if( dest.e
29d18 44 65 73 74 3d 3d 70 72 69 6f 72 4f 70 20 26 26  Dest==priorOp &&
29d19 20 41 4c 57 41 59 53 28 21 70 2d 3e 70 4c 69 6d   ALWAYS(!p->pLim
29d1a 69 74 20 26 26 21 70 2d 3e 70 4f 66 66 73 65 74  it &&!p->pOffset
29d1b 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  ) ){.        /* 
29d1c 57 65 20 63 61 6e 20 72 65 75 73 65 20 61 20 74  We can reuse a t
29d1d 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 67  emporary table g
29d1e 65 6e 65 72 61 74 65 64 20 62 79 20 61 20 53 45  enerated by a SE
29d1f 4c 45 43 54 20 74 6f 20 6f 75 72 0a 20 20 20 20  LECT to our.    
29d20 20 20 20 20 2a 2a 20 72 69 67 68 74 2e 0a 20 20      ** right..  
29d21 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
29d22 20 61 73 73 65 72 74 28 20 70 2d 3e 70 52 69 67   assert( p->pRig
29d23 68 74 6d 6f 73 74 21 3d 70 20 29 3b 20 20 2f 2a  htmost!=p );  /*
29d24 20 43 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e   Can only happen
29d25 20 66 6f 72 20 6c 65 66 74 77 61 72 64 20 65 6c   for leftward el
29d26 65 6d 65 6e 74 73 0a 20 20 20 20 20 20 20 20 20  ements.         
29d27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29d28 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f              ** o
29d29 66 20 61 20 33 2d 77 61 79 20 6f 72 20 6d 6f 72  f a 3-way or mor
29d2a 65 20 63 6f 6d 70 6f 75 6e 64 20 2a 2f 0a 20 20  e compound */.  
29d2b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
29d2c 3e 70 4c 69 6d 69 74 3d 3d 30 20 29 3b 20 20 20  >pLimit==0 );   
29d2d 20 20 20 2f 2a 20 4e 6f 74 20 61 6c 6c 6f 77 65     /* Not allowe
29d2e 64 20 6f 6e 20 6c 65 66 74 77 61 72 64 20 65 6c  d on leftward el
29d2f 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20  ements */.      
29d30 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4f 66    assert( p->pOf
29d31 66 73 65 74 3d 3d 30 20 29 3b 20 20 20 20 20 2f  fset==0 );     /
29d32 2a 20 4e 6f 74 20 61 6c 6c 6f 77 65 64 20 6f 6e  * Not allowed on
29d33 20 6c 65 66 74 77 61 72 64 20 65 6c 65 6d 65 6e   leftward elemen
29d34 74 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 6e  ts */.        un
29d35 69 6f 6e 54 61 62 20 3d 20 64 65 73 74 2e 69 50  ionTab = dest.iP
29d36 61 72 6d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  arm;.      }else
29d37 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20  {.        /* We 
29d38 77 69 6c 6c 20 6e 65 65 64 20 74 6f 20 63 72 65  will need to cre
29d39 61 74 65 20 6f 75 72 20 6f 77 6e 20 74 65 6d 70  ate our own temp
29d3a 6f 72 61 72 79 20 74 61 62 6c 65 20 74 6f 20 68  orary table to h
29d3b 6f 6c 64 20 74 68 65 0a 20 20 20 20 20 20 20 20  old the.        
29d3c 2a 2a 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20  ** intermediate 
29d3d 72 65 73 75 6c 74 73 2e 0a 20 20 20 20 20 20 20  results..       
29d3e 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 6e 69 6f   */.        unio
29d3f 6e 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e  nTab = pParse->n
29d40 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20 20 61  Tab++;.        a
29d41 73 73 65 72 74 28 20 70 2d 3e 70 4f 72 64 65 72  ssert( p->pOrder
29d42 42 79 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  By==0 );.       
29d43 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
29d44 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
29d45 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 75  OpenEphemeral, u
29d46 6e 69 6f 6e 54 61 62 2c 20 30 29 3b 0a 20 20 20  nionTab, 0);.   
29d47 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
29d48 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20  addrOpenEphm[0] 
29d49 3d 3d 20 2d 31 20 29 3b 0a 20 20 20 20 20 20 20  == -1 );.       
29d4a 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d   p->addrOpenEphm
29d4b 5b 30 5d 20 3d 20 61 64 64 72 3b 0a 20 20 20 20  [0] = addr;.    
29d4c 20 20 20 20 70 2d 3e 70 52 69 67 68 74 6d 6f 73      p->pRightmos
29d4d 74 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53  t->selFlags |= S
29d4e 46 5f 55 73 65 73 45 70 68 65 6d 65 72 61 6c 3b  F_UsesEphemeral;
29d4f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
29d50 20 70 2d 3e 70 45 4c 69 73 74 20 29 3b 0a 20 20   p->pEList );.  
29d51 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20      }..      /* 
29d52 43 6f 64 65 20 74 68 65 20 53 45 4c 45 43 54 20  Code the SELECT 
29d53 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 6f 75  statements to ou
29d54 72 20 6c 65 66 74 0a 20 20 20 20 20 20 2a 2f 0a  r left.      */.
29d55 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70        assert( !p
29d56 50 72 69 6f 72 2d 3e 70 4f 72 64 65 72 42 79 20  Prior->pOrderBy 
29d57 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
29d58 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26  SelectDestInit(&
29d59 75 6e 69 6f 6e 64 65 73 74 2c 20 70 72 69 6f 72  uniondest, prior
29d5a 4f 70 2c 20 75 6e 69 6f 6e 54 61 62 29 3b 0a 20  Op, unionTab);. 
29d5b 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
29d5c 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20  3Select(pParse, 
29d5d 70 50 72 69 6f 72 2c 20 26 75 6e 69 6f 6e 64 65  pPrior, &unionde
29d5e 73 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  st);.      if( r
29d5f 63 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74  c ){.        got
29d60 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65  o multi_select_e
29d61 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  nd;.      }..   
29d62 20 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 63     /* Code the c
29d63 75 72 72 65 6e 74 20 53 45 4c 45 43 54 20 73 74  urrent SELECT st
29d64 61 74 65 6d 65 6e 74 0a 20 20 20 20 20 20 2a 2f  atement.      */
29d65 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 6f 70  .      if( p->op
29d66 3d 3d 54 4b 5f 45 58 43 45 50 54 20 29 7b 0a 20  ==TK_EXCEPT ){. 
29d67 20 20 20 20 20 20 20 6f 70 20 3d 20 53 52 54 5f         op = SRT_
29d68 45 78 63 65 70 74 3b 0a 20 20 20 20 20 20 7d 65  Except;.      }e
29d69 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73  lse{.        ass
29d6a 65 72 74 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 55  ert( p->op==TK_U
29d6b 4e 49 4f 4e 20 29 3b 0a 20 20 20 20 20 20 20 20  NION );.        
29d6c 6f 70 20 3d 20 53 52 54 5f 55 6e 69 6f 6e 3b 0a  op = SRT_Union;.
29d6d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 2d        }.      p-
29d6e 3e 70 50 72 69 6f 72 20 3d 20 30 3b 0a 20 20 20  >pPrior = 0;.   
29d6f 20 20 20 70 4c 69 6d 69 74 20 3d 20 70 2d 3e 70     pLimit = p->p
29d70 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 70 2d 3e  Limit;.      p->
29d71 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20  pLimit = 0;.    
29d72 20 20 70 4f 66 66 73 65 74 20 3d 20 70 2d 3e 70    pOffset = p->p
29d73 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 70 2d  Offset;.      p-
29d74 3e 70 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20  >pOffset = 0;.  
29d75 20 20 20 20 75 6e 69 6f 6e 64 65 73 74 2e 65 44      uniondest.eD
29d76 65 73 74 20 3d 20 6f 70 3b 0a 20 20 20 20 20 20  est = op;.      
29d77 72 63 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  rc = sqlite3Sele
29d78 63 74 28 70 50 61 72 73 65 2c 20 70 2c 20 26 75  ct(pParse, p, &u
29d79 6e 69 6f 6e 64 65 73 74 29 3b 0a 20 20 20 20 20  niondest);.     
29d7a 20 74 65 73 74 63 61 73 65 28 20 72 63 21 3d 53   testcase( rc!=S
29d7b 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20  QLITE_OK );.    
29d7c 20 20 2f 2a 20 51 75 65 72 79 20 66 6c 61 74 74    /* Query flatt
29d7d 65 6e 69 6e 67 20 69 6e 20 73 71 6c 69 74 65 33  ening in sqlite3
29d7e 53 65 6c 65 63 74 28 29 20 6d 69 67 68 74 20 72  Select() might r
29d7f 65 66 69 6c 6c 20 70 2d 3e 70 4f 72 64 65 72 42  efill p->pOrderB
29d80 79 2e 0a 20 20 20 20 20 20 2a 2a 20 42 65 20 73  y..      ** Be s
29d81 75 72 65 20 74 6f 20 64 65 6c 65 74 65 20 70 2d  ure to delete p-
29d82 3e 70 4f 72 64 65 72 42 79 2c 20 74 68 65 72 65  >pOrderBy, there
29d83 66 6f 72 65 2c 20 74 6f 20 61 76 6f 69 64 20 61  fore, to avoid a
29d84 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 2e 20 2a 2f   memory leak. */
29d85 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
29d86 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  prListDelete(db,
29d87 20 70 2d 3e 70 4f 72 64 65 72 42 79 29 3b 0a 20   p->pOrderBy);. 
29d88 20 20 20 20 20 70 44 65 6c 65 74 65 20 3d 20 70       pDelete = p
29d89 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20  ->pPrior;.      
29d8a 70 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50 72 69  p->pPrior = pPri
29d8b 6f 72 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4f 72  or;.      p->pOr
29d8c 64 65 72 42 79 20 3d 20 30 3b 0a 20 20 20 20 20  derBy = 0;.     
29d8d 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
29d8e 74 65 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74  te(db, p->pLimit
29d8f 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d  );.      p->pLim
29d90 69 74 20 3d 20 70 4c 69 6d 69 74 3b 0a 20 20 20  it = pLimit;.   
29d91 20 20 20 70 2d 3e 70 4f 66 66 73 65 74 20 3d 20     p->pOffset = 
29d92 70 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 70  pOffset;.      p
29d93 2d 3e 69 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20  ->iLimit = 0;.  
29d94 20 20 20 20 70 2d 3e 69 4f 66 66 73 65 74 20 3d      p->iOffset =
29d95 20 30 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f   0;..      /* Co
29d96 6e 76 65 72 74 20 74 68 65 20 64 61 74 61 20 69  nvert the data i
29d97 6e 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20  n the temporary 
29d98 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 61 74 65  table into whate
29d99 76 65 72 20 66 6f 72 6d 0a 20 20 20 20 20 20 2a  ver form.      *
29d9a 2a 20 69 74 20 69 73 20 74 68 61 74 20 77 65 20  * it is that we 
29d9b 63 75 72 72 65 6e 74 6c 79 20 6e 65 65 64 2e 0a  currently need..
29d9c 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61        */.      a
29d9d 73 73 65 72 74 28 20 75 6e 69 6f 6e 54 61 62 3d  ssert( unionTab=
29d9e 3d 64 65 73 74 2e 69 50 61 72 6d 20 7c 7c 20 64  =dest.iParm || d
29d9f 65 73 74 2e 65 44 65 73 74 21 3d 70 72 69 6f 72  est.eDest!=prior
29da0 4f 70 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  Op );.      if( 
29da1 64 65 73 74 2e 65 44 65 73 74 21 3d 70 72 69 6f  dest.eDest!=prio
29da2 72 4f 70 20 29 7b 0a 20 20 20 20 20 20 20 20 69  rOp ){.        i
29da3 6e 74 20 69 43 6f 6e 74 2c 20 69 42 72 65 61 6b  nt iCont, iBreak
29da4 2c 20 69 53 74 61 72 74 3b 0a 20 20 20 20 20 20  , iStart;.      
29da5 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 45 4c    assert( p->pEL
29da6 69 73 74 20 29 3b 0a 20 20 20 20 20 20 20 20 69  ist );.        i
29da7 66 28 20 64 65 73 74 2e 65 44 65 73 74 3d 3d 53  f( dest.eDest==S
29da8 52 54 5f 4f 75 74 70 75 74 20 29 7b 0a 20 20 20  RT_Output ){.   
29da9 20 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70         Select *p
29daa 46 69 72 73 74 20 3d 20 70 3b 0a 20 20 20 20 20  First = p;.     
29dab 20 20 20 20 20 77 68 69 6c 65 28 20 70 46 69 72       while( pFir
29dac 73 74 2d 3e 70 50 72 69 6f 72 20 29 20 70 46 69  st->pPrior ) pFi
29dad 72 73 74 20 3d 20 70 46 69 72 73 74 2d 3e 70 50  rst = pFirst->pP
29dae 72 69 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 20  rior;.          
29daf 67 65 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 4e 61  generateColumnNa
29db0 6d 65 73 28 70 50 61 72 73 65 2c 20 30 2c 20 70  mes(pParse, 0, p
29db1 46 69 72 73 74 2d 3e 70 45 4c 69 73 74 29 3b 0a  First->pEList);.
29db2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
29db3 20 20 69 42 72 65 61 6b 20 3d 20 73 71 6c 69 74    iBreak = sqlit
29db4 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
29db5 76 29 3b 0a 20 20 20 20 20 20 20 20 69 43 6f 6e  v);.        iCon
29db6 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  t = sqlite3VdbeM
29db7 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20  akeLabel(v);.   
29db8 20 20 20 20 20 63 6f 6d 70 75 74 65 4c 69 6d 69       computeLimi
29db9 74 52 65 67 69 73 74 65 72 73 28 70 50 61 72 73  tRegisters(pPars
29dba 65 2c 20 70 2c 20 69 42 72 65 61 6b 29 3b 0a 20  e, p, iBreak);. 
29dbb 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
29dbc 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
29dbd 65 77 69 6e 64 2c 20 75 6e 69 6f 6e 54 61 62 2c  ewind, unionTab,
29dbe 20 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 20 20   iBreak);.      
29dbf 20 20 69 53 74 61 72 74 20 3d 20 73 71 6c 69 74    iStart = sqlit
29dc0 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
29dc1 72 28 76 29 3b 0a 20 20 20 20 20 20 20 20 73 65  r(v);.        se
29dc2 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70 28 70 50  lectInnerLoop(pP
29dc3 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70 45 4c 69  arse, p, p->pELi
29dc4 73 74 2c 20 75 6e 69 6f 6e 54 61 62 2c 20 70 2d  st, unionTab, p-
29dc5 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 0a  >pEList->nExpr,.
29dc6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29dc7 20 20 20 20 20 20 20 20 30 2c 20 2d 31 2c 20 26          0, -1, &
29dc8 64 65 73 74 2c 20 69 43 6f 6e 74 2c 20 69 42 72  dest, iCont, iBr
29dc9 65 61 6b 29 3b 0a 20 20 20 20 20 20 20 20 73 71  eak);.        sq
29dca 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
29dcb 4c 61 62 65 6c 28 76 2c 20 69 43 6f 6e 74 29 3b  Label(v, iCont);
29dcc 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
29dcd 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
29dce 5f 4e 65 78 74 2c 20 75 6e 69 6f 6e 54 61 62 2c  _Next, unionTab,
29dcf 20 69 53 74 61 72 74 29 3b 0a 20 20 20 20 20 20   iStart);.      
29dd0 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
29dd1 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 42 72  olveLabel(v, iBr
29dd2 65 61 6b 29 3b 0a 20 20 20 20 20 20 20 20 73 71  eak);.        sq
29dd3 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
29dd4 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 75 6e 69  v, OP_Close, uni
29dd5 6f 6e 54 61 62 2c 20 30 29 3b 0a 20 20 20 20 20  onTab, 0);.     
29dd6 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
29dd7 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c      }.    defaul
29dd8 74 3a 20 61 73 73 65 72 74 28 20 70 2d 3e 6f 70  t: assert( p->op
29dd9 3d 3d 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 29  ==TK_INTERSECT )
29dda 3b 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 61  ; {.      int ta
29ddb 62 31 2c 20 74 61 62 32 3b 0a 20 20 20 20 20 20  b1, tab2;.      
29ddc 69 6e 74 20 69 43 6f 6e 74 2c 20 69 42 72 65 61  int iCont, iBrea
29ddd 6b 2c 20 69 53 74 61 72 74 3b 0a 20 20 20 20 20  k, iStart;.     
29dde 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 2c 20 2a   Expr *pLimit, *
29ddf 70 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 69  pOffset;.      i
29de0 6e 74 20 61 64 64 72 3b 0a 20 20 20 20 20 20 53  nt addr;.      S
29de1 65 6c 65 63 74 44 65 73 74 20 69 6e 74 65 72 73  electDest inters
29de2 65 63 74 64 65 73 74 3b 0a 20 20 20 20 20 20 69  ectdest;.      i
29de3 6e 74 20 72 31 3b 0a 0a 20 20 20 20 20 20 2f 2a  nt r1;..      /*
29de4 20 49 4e 54 45 52 53 45 43 54 20 69 73 20 64 69   INTERSECT is di
29de5 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 74 68 65  fferent from the
29de6 20 6f 74 68 65 72 73 20 73 69 6e 63 65 20 69 74   others since it
29de7 20 72 65 71 75 69 72 65 73 0a 20 20 20 20 20 20   requires.      
29de8 2a 2a 20 74 77 6f 20 74 65 6d 70 6f 72 61 72 79  ** two temporary
29de9 20 74 61 62 6c 65 73 2e 20 20 48 65 6e 63 65 20   tables.  Hence 
29dea 69 74 20 68 61 73 20 69 74 73 20 6f 77 6e 20 63  it has its own c
29deb 61 73 65 2e 20 20 42 65 67 69 6e 0a 20 20 20 20  ase.  Begin.    
29dec 20 20 2a 2a 20 62 79 20 61 6c 6c 6f 63 61 74 69    ** by allocati
29ded 6e 67 20 74 68 65 20 74 61 62 6c 65 73 20 77 65  ng the tables we
29dee 20 77 69 6c 6c 20 6e 65 65 64 2e 0a 20 20 20 20   will need..    
29def 20 20 2a 2f 0a 20 20 20 20 20 20 74 61 62 31 20    */.      tab1 
29df0 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b  = pParse->nTab++
29df1 3b 0a 20 20 20 20 20 20 74 61 62 32 20 3d 20 70  ;.      tab2 = p
29df2 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20  Parse->nTab++;. 
29df3 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
29df4 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 3b 0a 0a  pOrderBy==0 );..
29df5 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c        addr = sql
29df6 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
29df7 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72  , OP_OpenEphemer
29df8 61 6c 2c 20 74 61 62 31 2c 20 30 29 3b 0a 20 20  al, tab1, 0);.  
29df9 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61      assert( p->a
29dfa 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d  ddrOpenEphm[0] =
29dfb 3d 20 2d 31 20 29 3b 0a 20 20 20 20 20 20 70 2d  = -1 );.      p-
29dfc 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d  >addrOpenEphm[0]
29dfd 20 3d 20 61 64 64 72 3b 0a 20 20 20 20 20 20 70   = addr;.      p
29dfe 2d 3e 70 52 69 67 68 74 6d 6f 73 74 2d 3e 73 65  ->pRightmost->se
29dff 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f 55 73 65  lFlags |= SF_Use
29e00 73 45 70 68 65 6d 65 72 61 6c 3b 0a 20 20 20 20  sEphemeral;.    
29e01 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 45 4c    assert( p->pEL
29e02 69 73 74 20 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  ist );..      /*
29e03 20 43 6f 64 65 20 74 68 65 20 53 45 4c 45 43 54   Code the SELECT
29e04 73 20 74 6f 20 6f 75 72 20 6c 65 66 74 20 69 6e  s to our left in
29e05 74 6f 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  to temporary tab
29e06 6c 65 20 22 74 61 62 31 22 2e 0a 20 20 20 20 20  le "tab1"..     
29e07 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
29e08 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28  3SelectDestInit(
29e09 26 69 6e 74 65 72 73 65 63 74 64 65 73 74 2c 20  &intersectdest, 
29e0a 53 52 54 5f 55 6e 69 6f 6e 2c 20 74 61 62 31 29  SRT_Union, tab1)
29e0b 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
29e0c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73  ite3Select(pPars
29e0d 65 2c 20 70 50 72 69 6f 72 2c 20 26 69 6e 74 65  e, pPrior, &inte
29e0e 72 73 65 63 74 64 65 73 74 29 3b 0a 20 20 20 20  rsectdest);.    
29e0f 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
29e10 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73      goto multi_s
29e11 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20 20  elect_end;.     
29e12 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 64   }..      /* Cod
29e13 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 53 45  e the current SE
29e14 4c 45 43 54 20 69 6e 74 6f 20 74 65 6d 70 6f 72  LECT into tempor
29e15 61 72 79 20 74 61 62 6c 65 20 22 74 61 62 32 22  ary table "tab2"
29e16 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
29e17 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
29e18 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f  beAddOp2(v, OP_O
29e19 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 74 61  penEphemeral, ta
29e1a 62 32 2c 20 30 29 3b 0a 20 20 20 20 20 20 61 73  b2, 0);.      as
29e1b 73 65 72 74 28 20 70 2d 3e 61 64 64 72 4f 70 65  sert( p->addrOpe
29e1c 6e 45 70 68 6d 5b 31 5d 20 3d 3d 20 2d 31 20 29  nEphm[1] == -1 )
29e1d 3b 0a 20 20 20 20 20 20 70 2d 3e 61 64 64 72 4f  ;.      p->addrO
29e1e 70 65 6e 45 70 68 6d 5b 31 5d 20 3d 20 61 64 64  penEphm[1] = add
29e1f 72 3b 0a 20 20 20 20 20 20 70 2d 3e 70 50 72 69  r;.      p->pPri
29e20 6f 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4c  or = 0;.      pL
29e21 69 6d 69 74 20 3d 20 70 2d 3e 70 4c 69 6d 69 74  imit = p->pLimit
29e22 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d 69  ;.      p->pLimi
29e23 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4f 66  t = 0;.      pOf
29e24 66 73 65 74 20 3d 20 70 2d 3e 70 4f 66 66 73 65  fset = p->pOffse
29e25 74 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4f 66 66  t;.      p->pOff
29e26 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 69  set = 0;.      i
29e27 6e 74 65 72 73 65 63 74 64 65 73 74 2e 69 50 61  ntersectdest.iPa
29e28 72 6d 20 3d 20 74 61 62 32 3b 0a 20 20 20 20 20  rm = tab2;.     
29e29 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53 65 6c   rc = sqlite3Sel
29e2a 65 63 74 28 70 50 61 72 73 65 2c 20 70 2c 20 26  ect(pParse, p, &
29e2b 69 6e 74 65 72 73 65 63 74 64 65 73 74 29 3b 0a  intersectdest);.
29e2c 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
29e2d 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc!=SQLITE_OK );
29e2e 0a 20 20 20 20 20 20 70 44 65 6c 65 74 65 20 3d  .      pDelete =
29e2f 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20   p->pPrior;.    
29e30 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50    p->pPrior = pP
29e31 72 69 6f 72 3b 0a 20 20 20 20 20 20 73 71 6c 69  rior;.      sqli
29e32 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
29e33 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20 20  , p->pLimit);.  
29e34 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20      p->pLimit = 
29e35 70 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 70 2d  pLimit;.      p-
29e36 3e 70 4f 66 66 73 65 74 20 3d 20 70 4f 66 66 73  >pOffset = pOffs
29e37 65 74 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 47 65  et;..      /* Ge
29e38 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 74  nerate code to t
29e39 61 6b 65 20 74 68 65 20 69 6e 74 65 72 73 65 63  ake the intersec
29e3a 74 69 6f 6e 20 6f 66 20 74 68 65 20 74 77 6f 20  tion of the two 
29e3b 74 65 6d 70 6f 72 61 72 79 0a 20 20 20 20 20 20  temporary.      
29e3c 2a 2a 20 74 61 62 6c 65 73 2e 0a 20 20 20 20 20  ** tables..     
29e3d 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74   */.      assert
29e3e 28 20 70 2d 3e 70 45 4c 69 73 74 20 29 3b 0a 20  ( p->pEList );. 
29e3f 20 20 20 20 20 69 66 28 20 64 65 73 74 2e 65 44       if( dest.eD
29e40 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20  est==SRT_Output 
29e41 29 7b 0a 20 20 20 20 20 20 20 20 53 65 6c 65 63  ){.        Selec
29e42 74 20 2a 70 46 69 72 73 74 20 3d 20 70 3b 0a 20  t *pFirst = p;. 
29e43 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 46         while( pF
29e44 69 72 73 74 2d 3e 70 50 72 69 6f 72 20 29 20 70  irst->pPrior ) p
29e45 46 69 72 73 74 20 3d 20 70 46 69 72 73 74 2d 3e  First = pFirst->
29e46 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 20 20  pPrior;.        
29e47 67 65 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 4e 61  generateColumnNa
29e48 6d 65 73 28 70 50 61 72 73 65 2c 20 30 2c 20 70  mes(pParse, 0, p
29e49 46 69 72 73 74 2d 3e 70 45 4c 69 73 74 29 3b 0a  First->pEList);.
29e4a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 42        }.      iB
29e4b 72 65 61 6b 20 3d 20 73 71 6c 69 74 65 33 56 64  reak = sqlite3Vd
29e4c 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
29e4d 20 20 20 20 20 20 69 43 6f 6e 74 20 3d 20 73 71        iCont = sq
29e4e 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
29e4f 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 63 6f 6d  el(v);.      com
29e50 70 75 74 65 4c 69 6d 69 74 52 65 67 69 73 74 65  puteLimitRegiste
29e51 72 73 28 70 50 61 72 73 65 2c 20 70 2c 20 69 42  rs(pParse, p, iB
29e52 72 65 61 6b 29 3b 0a 20 20 20 20 20 20 73 71 6c  reak);.      sql
29e53 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
29e54 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 74 61 62  , OP_Rewind, tab
29e55 31 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 20 20  1, iBreak);.    
29e56 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65    r1 = sqlite3Ge
29e57 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
29e58 3b 0a 20 20 20 20 20 20 69 53 74 61 72 74 20 3d  ;.      iStart =
29e59 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29e5a 70 32 28 76 2c 20 4f 50 5f 52 6f 77 4b 65 79 2c  p2(v, OP_RowKey,
29e5b 20 74 61 62 31 2c 20 72 31 29 3b 0a 20 20 20 20   tab1, r1);.    
29e5c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
29e5d 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 46 6f 75  Op3(v, OP_NotFou
29e5e 6e 64 2c 20 74 61 62 32 2c 20 69 43 6f 6e 74 2c  nd, tab2, iCont,
29e5f 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69   r1);.      sqli
29e60 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
29e61 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20  g(pParse, r1);. 
29e62 20 20 20 20 20 73 65 6c 65 63 74 49 6e 6e 65 72       selectInner
29e63 4c 6f 6f 70 28 70 50 61 72 73 65 2c 20 70 2c 20  Loop(pParse, p, 
29e64 70 2d 3e 70 45 4c 69 73 74 2c 20 74 61 62 31 2c  p->pEList, tab1,
29e65 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70   p->pEList->nExp
29e66 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r,.             
29e67 20 20 20 20 20 20 20 20 20 30 2c 20 2d 31 2c 20           0, -1, 
29e68 26 64 65 73 74 2c 20 69 43 6f 6e 74 2c 20 69 42  &dest, iCont, iB
29e69 72 65 61 6b 29 3b 0a 20 20 20 20 20 20 73 71 6c  reak);.      sql
29e6a 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
29e6b 61 62 65 6c 28 76 2c 20 69 43 6f 6e 74 29 3b 0a  abel(v, iCont);.
29e6c 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29e6d 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65  eAddOp2(v, OP_Ne
29e6e 78 74 2c 20 74 61 62 31 2c 20 69 53 74 61 72 74  xt, tab1, iStart
29e6f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
29e70 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
29e71 28 76 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 20  (v, iBreak);.   
29e72 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
29e73 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65  dOp2(v, OP_Close
29e74 2c 20 74 61 62 32 2c 20 30 29 3b 0a 20 20 20 20  , tab2, 0);.    
29e75 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
29e76 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  Op2(v, OP_Close,
29e77 20 74 61 62 31 2c 20 30 29 3b 0a 20 20 20 20 20   tab1, 0);.     
29e78 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
29e79 7d 0a 0a 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20  }..  /* Compute 
29e7a 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
29e7b 63 65 73 20 75 73 65 64 20 62 79 20 0a 20 20 2a  ces used by .  *
29e7c 2a 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  * temporary tabl
29e7d 65 73 20 6e 65 65 64 65 64 20 74 6f 20 69 6d 70  es needed to imp
29e7e 6c 65 6d 65 6e 74 20 74 68 65 20 63 6f 6d 70 6f  lement the compo
29e7f 75 6e 64 20 73 65 6c 65 63 74 2e 0a 20 20 2a 2a  und select..  **
29e80 20 41 74 74 61 63 68 20 74 68 65 20 4b 65 79 49   Attach the KeyI
29e81 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 74 6f  nfo structure to
29e82 20 61 6c 6c 20 74 65 6d 70 6f 72 61 72 79 20 74   all temporary t
29e83 61 62 6c 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  ables..  **.  **
29e84 20 54 68 69 73 20 73 65 63 74 69 6f 6e 20 69 73   This section is
29e85 20 72 75 6e 20 62 79 20 74 68 65 20 72 69 67 68   run by the righ
29e86 74 2d 6d 6f 73 74 20 53 45 4c 45 43 54 20 73 74  t-most SELECT st
29e87 61 74 65 6d 65 6e 74 20 6f 6e 6c 79 2e 0a 20 20  atement only..  
29e88 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  ** SELECT statem
29e89 65 6e 74 73 20 74 6f 20 74 68 65 20 6c 65 66 74  ents to the left
29e8a 20 61 6c 77 61 79 73 20 73 6b 69 70 20 74 68 69   always skip thi
29e8b 73 20 70 61 72 74 2e 20 20 54 68 65 20 72 69 67  s part.  The rig
29e8c 68 74 2d 6d 6f 73 74 0a 20 20 2a 2a 20 53 45 4c  ht-most.  ** SEL
29e8d 45 43 54 20 6d 69 67 68 74 20 61 6c 73 6f 20 73  ECT might also s
29e8e 6b 69 70 20 74 68 69 73 20 70 61 72 74 20 69 66  kip this part if
29e8f 20 69 74 20 68 61 73 20 6e 6f 20 4f 52 44 45 52   it has no ORDER
29e90 20 42 59 20 63 6c 61 75 73 65 20 61 6e 64 0a 20   BY clause and. 
29e91 20 2a 2a 20 6e 6f 20 74 65 6d 70 20 74 61 62 6c   ** no temp tabl
29e92 65 73 20 61 72 65 20 72 65 71 75 69 72 65 64 2e  es are required.
29e93 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 73  .  */.  if( p->s
29e94 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 55 73 65  elFlags & SF_Use
29e95 73 45 70 68 65 6d 65 72 61 6c 20 29 7b 0a 20 20  sEphemeral ){.  
29e96 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
29e97 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29e98 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
29e99 2a 2f 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a  */.    KeyInfo *
29e9a 70 4b 65 79 49 6e 66 6f 3b 20 20 20 20 20 20 20  pKeyInfo;       
29e9b 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e       /* Collatin
29e9c 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74  g sequence for t
29e9d 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f  he result set */
29e9e 0a 20 20 20 20 53 65 6c 65 63 74 20 2a 70 4c 6f  .    Select *pLo
29e9f 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  op;             
29ea0 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e     /* For loopin
29ea1 67 20 74 68 72 6f 75 67 68 20 53 45 4c 45 43 54  g through SELECT
29ea2 20 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 20   statements */. 
29ea3 20 20 20 43 6f 6c 6c 53 65 71 20 2a 2a 61 70 43     CollSeq **apC
29ea4 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  oll;            
29ea5 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20   /* For looping 
29ea6 74 68 72 6f 75 67 68 20 70 4b 65 79 49 6e 66 6f  through pKeyInfo
29ea7 2d 3e 61 43 6f 6c 6c 5b 5d 20 2a 2f 0a 20 20 20  ->aColl[] */.   
29ea8 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20 20 20 20   int nCol;      
29ea9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
29eaa 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  * Number of colu
29eab 6d 6e 73 20 69 6e 20 72 65 73 75 6c 74 20 73 65  mns in result se
29eac 74 20 2a 2f 0a 0a 20 20 20 20 61 73 73 65 72 74  t */..    assert
29ead 28 20 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74 3d  ( p->pRightmost=
29eae 3d 70 20 29 3b 0a 20 20 20 20 6e 43 6f 6c 20 3d  =p );.    nCol =
29eaf 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70   p->pEList->nExp
29eb0 72 3b 0a 20 20 20 20 70 4b 65 79 49 6e 66 6f 20  r;.    pKeyInfo 
29eb1 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
29eb2 63 5a 65 72 6f 28 64 62 2c 0a 20 20 20 20 20 20  cZero(db,.      
29eb3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29eb4 20 73 69 7a 65 6f 66 28 2a 70 4b 65 79 49 6e 66   sizeof(*pKeyInf
29eb5 6f 29 2b 6e 43 6f 6c 2a 28 73 69 7a 65 6f 66 28  o)+nCol*(sizeof(
29eb6 43 6f 6c 6c 53 65 71 2a 29 20 2b 20 31 29 29 3b  CollSeq*) + 1));
29eb7 0a 20 20 20 20 69 66 28 20 21 70 4b 65 79 49 6e  .    if( !pKeyIn
29eb8 66 6f 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  fo ){.      rc =
29eb9 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
29eba 20 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f       goto multi_
29ebb 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20  select_end;.    
29ebc 7d 0a 0a 20 20 20 20 70 4b 65 79 49 6e 66 6f 2d  }..    pKeyInfo-
29ebd 3e 65 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b 0a  >enc = ENC(db);.
29ebe 20 20 20 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46      pKeyInfo->nF
29ebf 69 65 6c 64 20 3d 20 28 75 31 36 29 6e 43 6f 6c  ield = (u16)nCol
29ec0 3b 0a 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20  ;..    for(i=0, 
29ec1 61 70 43 6f 6c 6c 3d 70 4b 65 79 49 6e 66 6f 2d  apColl=pKeyInfo-
29ec2 3e 61 43 6f 6c 6c 3b 20 69 3c 6e 43 6f 6c 3b 20  >aColl; i<nCol; 
29ec3 69 2b 2b 2c 20 61 70 43 6f 6c 6c 2b 2b 29 7b 0a  i++, apColl++){.
29ec4 20 20 20 20 20 20 2a 61 70 43 6f 6c 6c 20 3d 20        *apColl = 
29ec5 6d 75 6c 74 69 53 65 6c 65 63 74 43 6f 6c 6c 53  multiSelectCollS
29ec6 65 71 28 70 50 61 72 73 65 2c 20 70 2c 20 69 29  eq(pParse, p, i)
29ec7 3b 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d 2a  ;.      if( 0==*
29ec8 61 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20  apColl ){.      
29ec9 20 20 2a 61 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e    *apColl = db->
29eca 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20 20 20 20  pDfltColl;.     
29ecb 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 66 6f   }.    }..    fo
29ecc 72 28 70 4c 6f 6f 70 3d 70 3b 20 70 4c 6f 6f 70  r(pLoop=p; pLoop
29ecd 3b 20 70 4c 6f 6f 70 3d 70 4c 6f 6f 70 2d 3e 70  ; pLoop=pLoop->p
29ece 50 72 69 6f 72 29 7b 0a 20 20 20 20 20 20 66 6f  Prior){.      fo
29ecf 72 28 69 3d 30 3b 20 69 3c 32 3b 20 69 2b 2b 29  r(i=0; i<2; i++)
29ed0 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 61 64  {.        int ad
29ed1 64 72 20 3d 20 70 4c 6f 6f 70 2d 3e 61 64 64 72  dr = pLoop->addr
29ed2 4f 70 65 6e 45 70 68 6d 5b 69 5d 3b 0a 20 20 20  OpenEphm[i];.   
29ed3 20 20 20 20 20 69 66 28 20 61 64 64 72 3c 30 20       if( addr<0 
29ed4 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ){.          /* 
29ed5 49 66 20 5b 30 5d 20 69 73 20 75 6e 75 73 65 64  If [0] is unused
29ed6 20 74 68 65 6e 20 5b 31 5d 20 69 73 20 61 6c 73   then [1] is als
29ed7 6f 20 75 6e 75 73 65 64 2e 20 20 53 6f 20 77 65  o unused.  So we
29ed8 20 63 61 6e 0a 20 20 20 20 20 20 20 20 20 20 2a   can.          *
29ed9 2a 20 61 6c 77 61 79 73 20 73 61 66 65 6c 79 20  * always safely 
29eda 61 62 6f 72 74 20 61 73 20 73 6f 6f 6e 20 61 73  abort as soon as
29edb 20 74 68 65 20 66 69 72 73 74 20 75 6e 75 73 65   the first unuse
29edc 64 20 73 6c 6f 74 20 69 73 20 66 6f 75 6e 64 20  d slot is found 
29edd 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  */.          ass
29ede 65 72 74 28 20 70 4c 6f 6f 70 2d 3e 61 64 64 72  ert( pLoop->addr
29edf 4f 70 65 6e 45 70 68 6d 5b 31 5d 3c 30 20 29 3b  OpenEphm[1]<0 );
29ee0 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
29ee1 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
29ee2 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
29ee3 68 61 6e 67 65 50 32 28 76 2c 20 61 64 64 72 2c  hangeP2(v, addr,
29ee4 20 6e 43 6f 6c 29 3b 0a 20 20 20 20 20 20 20 20   nCol);.        
29ee5 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
29ee6 65 50 34 28 76 2c 20 61 64 64 72 2c 20 28 63 68  eP4(v, addr, (ch
29ee7 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c 20 50 34  ar*)pKeyInfo, P4
29ee8 5f 4b 45 59 49 4e 46 4f 29 3b 0a 20 20 20 20 20  _KEYINFO);.     
29ee9 20 20 20 70 4c 6f 6f 70 2d 3e 61 64 64 72 4f 70     pLoop->addrOp
29eea 65 6e 45 70 68 6d 5b 69 5d 20 3d 20 2d 31 3b 0a  enEphm[i] = -1;.
29eeb 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
29eec 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
29eed 64 62 2c 20 70 4b 65 79 49 6e 66 6f 29 3b 0a 20  db, pKeyInfo);. 
29eee 20 7d 0a 0a 6d 75 6c 74 69 5f 73 65 6c 65 63 74   }..multi_select
29eef 5f 65 6e 64 3a 0a 20 20 70 44 65 73 74 2d 3e 69  _end:.  pDest->i
29ef0 4d 65 6d 20 3d 20 64 65 73 74 2e 69 4d 65 6d 3b  Mem = dest.iMem;
29ef1 0a 20 20 70 44 65 73 74 2d 3e 6e 4d 65 6d 20 3d  .  pDest->nMem =
29ef2 20 64 65 73 74 2e 6e 4d 65 6d 3b 0a 20 20 73 71   dest.nMem;.  sq
29ef3 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74  lite3SelectDelet
29ef4 65 28 64 62 2c 20 70 44 65 6c 65 74 65 29 3b 0a  e(db, pDelete);.
29ef5 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23    return rc;.}.#
29ef6 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
29ef7 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45  OMIT_COMPOUND_SE
29ef8 4c 45 43 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43  LECT */../*.** C
29ef9 6f 64 65 20 61 6e 20 6f 75 74 70 75 74 20 73 75  ode an output su
29efa 62 72 6f 75 74 69 6e 65 20 66 6f 72 20 61 20 63  broutine for a c
29efb 6f 72 6f 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65  oroutine impleme
29efc 6e 74 61 74 69 6f 6e 20 6f 66 20 61 0a 2a 2a 20  ntation of a.** 
29efd 53 45 4c 45 43 54 20 73 74 61 74 6d 65 6e 74 2e  SELECT statment.
29efe 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 61 74 61 20  .**.** The data 
29eff 74 6f 20 62 65 20 6f 75 74 70 75 74 20 69 73 20  to be output is 
29f00 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 70 49 6e  contained in pIn
29f01 2d 3e 69 4d 65 6d 2e 20 20 54 68 65 72 65 20 61  ->iMem.  There a
29f02 72 65 0a 2a 2a 20 70 49 6e 2d 3e 6e 4d 65 6d 20  re.** pIn->nMem 
29f03 63 6f 6c 75 6d 6e 73 20 74 6f 20 62 65 20 6f 75  columns to be ou
29f04 74 70 75 74 2e 20 20 70 44 65 73 74 20 69 73 20  tput.  pDest is 
29f05 77 68 65 72 65 20 74 68 65 20 6f 75 74 70 75 74  where the output
29f06 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 73 65   should.** be se
29f07 6e 74 2e 0a 2a 2a 0a 2a 2a 20 72 65 67 52 65 74  nt..**.** regRet
29f08 75 72 6e 20 69 73 20 74 68 65 20 6e 75 6d 62 65  urn is the numbe
29f09 72 20 6f 66 20 74 68 65 20 72 65 67 69 73 74 65  r of the registe
29f0a 72 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 73 75  r holding the su
29f0b 62 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 74 75  broutine.** retu
29f0c 72 6e 20 61 64 64 72 65 73 73 2e 0a 2a 2a 0a 2a  rn address..**.*
29f0d 2a 20 49 66 20 72 65 67 50 72 65 76 3e 30 20 74  * If regPrev>0 t
29f0e 68 65 6e 20 69 74 20 69 73 20 61 20 74 68 65 20  hen it is a the 
29f0f 66 69 72 73 74 20 72 65 67 69 73 74 65 72 20 69  first register i
29f10 6e 20 61 20 76 65 63 74 6f 72 20 74 68 61 74 0a  n a vector that.
29f11 2a 2a 20 72 65 63 6f 72 64 73 20 74 68 65 20 70  ** records the p
29f12 72 65 76 69 6f 75 73 20 6f 75 74 70 75 74 2e 20  revious output. 
29f13 20 6d 65 6d 5b 72 65 67 50 72 65 76 5d 20 69 73   mem[regPrev] is
29f14 20 61 20 66 6c 61 67 20 74 68 61 74 20 69 73 20   a flag that is 
29f15 66 61 6c 73 65 0a 2a 2a 20 69 66 20 74 68 65 72  false.** if ther
29f16 65 20 68 61 73 20 62 65 65 6e 20 6e 6f 20 70 72  e has been no pr
29f17 65 76 69 6f 75 73 20 6f 75 74 70 75 74 2e 20 20  evious output.  
29f18 49 66 20 72 65 67 50 72 65 76 3e 30 20 74 68 65  If regPrev>0 the
29f19 6e 20 63 6f 64 65 20 69 73 0a 2a 2a 20 67 65 6e  n code is.** gen
29f1a 65 72 61 74 65 64 20 74 6f 20 73 75 70 70 72 65  erated to suppre
29f1b 73 73 20 64 75 70 6c 69 63 61 74 65 73 2e 20 20  ss duplicates.  
29f1c 70 4b 65 79 49 6e 66 6f 20 69 73 20 75 73 65 64  pKeyInfo is used
29f1d 20 66 6f 72 20 63 6f 6d 70 61 72 69 6e 67 0a 2a   for comparing.*
29f1e 2a 20 6b 65 79 73 2e 0a 2a 2a 0a 2a 2a 20 49 66  * keys..**.** If
29f1f 20 74 68 65 20 4c 49 4d 49 54 20 66 6f 75 6e 64   the LIMIT found
29f20 20 69 6e 20 70 2d 3e 69 4c 69 6d 69 74 20 69 73   in p->iLimit is
29f21 20 72 65 61 63 68 65 64 2c 20 6a 75 6d 70 20 69   reached, jump i
29f22 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 0a 2a 2a  mmediately to.**
29f23 20 69 42 72 65 61 6b 2e 0a 2a 2f 0a 73 74 61 74   iBreak..*/.stat
29f24 69 63 20 69 6e 74 20 67 65 6e 65 72 61 74 65 4f  ic int generateO
29f25 75 74 70 75 74 53 75 62 72 6f 75 74 69 6e 65 28  utputSubroutine(
29f26 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
29f27 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61  ,          /* Pa
29f28 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
29f29 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20  .  Select *p,   
29f2a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
29f2b 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
29f2c 6e 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65  nt */.  SelectDe
29f2d 73 74 20 2a 70 49 6e 2c 20 20 20 20 20 20 20 20  st *pIn,        
29f2e 2f 2a 20 43 6f 72 6f 75 74 69 6e 65 20 73 75 70  /* Coroutine sup
29f2f 70 6c 79 69 6e 67 20 64 61 74 61 20 2a 2f 0a 20  plying data */. 
29f30 20 53 65 6c 65 63 74 44 65 73 74 20 2a 70 44 65   SelectDest *pDe
29f31 73 74 2c 20 20 20 20 20 20 2f 2a 20 57 68 65 72  st,      /* Wher
29f32 65 20 74 6f 20 73 65 6e 64 20 74 68 65 20 64 61  e to send the da
29f33 74 61 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52  ta */.  int regR
29f34 65 74 75 72 6e 2c 20 20 20 20 20 20 20 20 20 20  eturn,          
29f35 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 20 61 64  /* The return ad
29f36 64 72 65 73 73 20 72 65 67 69 73 74 65 72 20 2a  dress register *
29f37 2f 0a 20 20 69 6e 74 20 72 65 67 50 72 65 76 2c  /.  int regPrev,
29f38 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
29f39 72 65 76 69 6f 75 73 20 72 65 73 75 6c 74 20 72  revious result r
29f3a 65 67 69 73 74 65 72 2e 20 20 4e 6f 20 75 6e 69  egister.  No uni
29f3b 71 75 65 6e 65 73 73 20 69 66 20 30 20 2a 2f 0a  queness if 0 */.
29f3c 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49    KeyInfo *pKeyI
29f3d 6e 66 6f 2c 20 20 20 20 20 20 2f 2a 20 46 6f 72  nfo,      /* For
29f3e 20 63 6f 6d 70 61 72 69 6e 67 20 77 69 74 68 20   comparing with 
29f3f 70 72 65 76 69 6f 75 73 20 65 6e 74 72 79 20 2a  previous entry *
29f40 2f 0a 20 20 69 6e 74 20 70 34 74 79 70 65 2c 20  /.  int p4type, 
29f41 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
29f42 68 65 20 70 34 20 74 79 70 65 20 66 6f 72 20 70  he p4 type for p
29f43 4b 65 79 49 6e 66 6f 20 2a 2f 0a 20 20 69 6e 74  KeyInfo */.  int
29f44 20 69 42 72 65 61 6b 20 20 20 20 20 20 20 20 20   iBreak         
29f45 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72       /* Jump her
29f46 65 20 69 66 20 77 65 20 68 69 74 20 74 68 65 20  e if we hit the 
29f47 4c 49 4d 49 54 20 2a 2f 0a 29 7b 0a 20 20 56 64  LIMIT */.){.  Vd
29f48 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
29f49 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 43 6f  pVdbe;.  int iCo
29f4a 6e 74 69 6e 75 65 3b 0a 20 20 69 6e 74 20 61 64  ntinue;.  int ad
29f4b 64 72 3b 0a 0a 20 20 61 64 64 72 20 3d 20 73 71  dr;..  addr = sq
29f4c 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
29f4d 41 64 64 72 28 76 29 3b 0a 20 20 69 43 6f 6e 74  Addr(v);.  iCont
29f4e 69 6e 75 65 20 3d 20 73 71 6c 69 74 65 33 56 64  inue = sqlite3Vd
29f4f 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
29f50 0a 20 20 2f 2a 20 53 75 70 70 72 65 73 73 20 64  .  /* Suppress d
29f51 75 70 6c 69 63 61 74 65 73 20 66 6f 72 20 55 4e  uplicates for UN
29f52 49 4f 4e 2c 20 45 58 43 45 50 54 2c 20 61 6e 64  ION, EXCEPT, and
29f53 20 49 4e 54 45 52 53 45 43 54 20 0a 20 20 2a 2f   INTERSECT .  */
29f54 0a 20 20 69 66 28 20 72 65 67 50 72 65 76 20 29  .  if( regPrev )
29f55 7b 0a 20 20 20 20 69 6e 74 20 6a 31 2c 20 6a 32  {.    int j1, j2
29f56 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74  ;.    j1 = sqlit
29f57 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
29f58 4f 50 5f 49 66 4e 6f 74 2c 20 72 65 67 50 72 65  OP_IfNot, regPre
29f59 76 29 3b 0a 20 20 20 20 6a 32 20 3d 20 73 71 6c  v);.    j2 = sql
29f5a 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
29f5b 2c 20 4f 50 5f 43 6f 6d 70 61 72 65 2c 20 70 49  , OP_Compare, pI
29f5c 6e 2d 3e 69 4d 65 6d 2c 20 72 65 67 50 72 65 76  n->iMem, regPrev
29f5d 2b 31 2c 20 70 49 6e 2d 3e 6e 4d 65 6d 2c 0a 20  +1, pIn->nMem,. 
29f5e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29f5f 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63 68               (ch
29f60 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c 20 70 34  ar*)pKeyInfo, p4
29f61 74 79 70 65 29 3b 0a 20 20 20 20 73 71 6c 69 74  type);.    sqlit
29f62 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
29f63 4f 50 5f 4a 75 6d 70 2c 20 6a 32 2b 32 2c 20 69  OP_Jump, j2+2, i
29f64 43 6f 6e 74 69 6e 75 65 2c 20 6a 32 2b 32 29 3b  Continue, j2+2);
29f65 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
29f66 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b  JumpHere(v, j1);
29f67 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
29f68 43 6f 64 65 43 6f 70 79 28 70 50 61 72 73 65 2c  CodeCopy(pParse,
29f69 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20 72 65 67 50   pIn->iMem, regP
29f6a 72 65 76 2b 31 2c 20 70 49 6e 2d 3e 6e 4d 65 6d  rev+1, pIn->nMem
29f6b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
29f6c 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
29f6d 6e 74 65 67 65 72 2c 20 31 2c 20 72 65 67 50 72  nteger, 1, regPr
29f6e 65 76 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  ev);.  }.  if( p
29f6f 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  Parse->db->mallo
29f70 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e  cFailed ) return
29f71 20 30 3b 0a 0a 20 20 2f 2a 20 53 75 70 70 72 65   0;..  /* Suppre
29f72 73 73 20 74 68 65 20 74 68 65 20 66 69 72 73 74  ss the the first
29f73 20 4f 46 46 53 45 54 20 65 6e 74 72 69 65 73 20   OFFSET entries 
29f74 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 4f  if there is an O
29f75 46 46 53 45 54 20 63 6c 61 75 73 65 0a 20 20 2a  FFSET clause.  *
29f76 2f 0a 20 20 63 6f 64 65 4f 66 66 73 65 74 28 76  /.  codeOffset(v
29f77 2c 20 70 2c 20 69 43 6f 6e 74 69 6e 75 65 29 3b  , p, iContinue);
29f78 0a 0a 20 20 73 77 69 74 63 68 28 20 70 44 65 73  ..  switch( pDes
29f79 74 2d 3e 65 44 65 73 74 20 29 7b 0a 20 20 20 20  t->eDest ){.    
29f7a 2f 2a 20 53 74 6f 72 65 20 74 68 65 20 72 65 73  /* Store the res
29f7b 75 6c 74 20 61 73 20 64 61 74 61 20 75 73 69 6e  ult as data usin
29f7c 67 20 61 20 75 6e 69 71 75 65 20 6b 65 79 2e 0a  g a unique key..
29f7d 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20      */.    case 
29f7e 53 52 54 5f 54 61 62 6c 65 3a 0a 20 20 20 20 63  SRT_Table:.    c
29f7f 61 73 65 20 53 52 54 5f 45 70 68 65 6d 54 61 62  ase SRT_EphemTab
29f80 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 31  : {.      int r1
29f81 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
29f82 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
29f83 20 20 20 20 69 6e 74 20 72 32 20 3d 20 73 71 6c      int r2 = sql
29f84 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
29f85 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 74 65  Parse);.      te
29f86 73 74 63 61 73 65 28 20 70 44 65 73 74 2d 3e 65  stcase( pDest->e
29f87 44 65 73 74 3d 3d 53 52 54 5f 54 61 62 6c 65 20  Dest==SRT_Table 
29f88 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
29f89 65 28 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3d  e( pDest->eDest=
29f8a 3d 53 52 54 5f 45 70 68 65 6d 54 61 62 20 29 3b  =SRT_EphemTab );
29f8b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
29f8c 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d  beAddOp3(v, OP_M
29f8d 61 6b 65 52 65 63 6f 72 64 2c 20 70 49 6e 2d 3e  akeRecord, pIn->
29f8e 69 4d 65 6d 2c 20 70 49 6e 2d 3e 6e 4d 65 6d 2c  iMem, pIn->nMem,
29f8f 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69   r1);.      sqli
29f90 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
29f91 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 70 44   OP_NewRowid, pD
29f92 65 73 74 2d 3e 69 50 61 72 6d 2c 20 72 32 29 3b  est->iParm, r2);
29f93 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
29f94 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49  beAddOp3(v, OP_I
29f95 6e 73 65 72 74 2c 20 70 44 65 73 74 2d 3e 69 50  nsert, pDest->iP
29f96 61 72 6d 2c 20 72 31 2c 20 72 32 29 3b 0a 20 20  arm, r1, r2);.  
29f97 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
29f98 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41  hangeP5(v, OPFLA
29f99 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20 20 20 20  G_APPEND);.     
29f9a 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
29f9b 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
29f9c 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  2);.      sqlite
29f9d 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
29f9e 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20  pParse, r1);.   
29f9f 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
29fa0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
29fa1 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20  OMIT_SUBQUERY.  
29fa2 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 63    /* If we are c
29fa3 72 65 61 74 69 6e 67 20 61 20 73 65 74 20 66 6f  reating a set fo
29fa4 72 20 61 6e 20 22 65 78 70 72 20 49 4e 20 28 53  r an "expr IN (S
29fa5 45 4c 45 43 54 20 2e 2e 2e 29 22 20 63 6f 6e 73  ELECT ...)" cons
29fa6 74 72 75 63 74 2c 0a 20 20 20 20 2a 2a 20 74 68  truct,.    ** th
29fa7 65 6e 20 74 68 65 72 65 20 73 68 6f 75 6c 64 20  en there should 
29fa8 62 65 20 61 20 73 69 6e 67 6c 65 20 69 74 65 6d  be a single item
29fa9 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 2e 20 20   on the stack.  
29faa 57 72 69 74 65 20 74 68 69 73 0a 20 20 20 20 2a  Write this.    *
29fab 2a 20 69 74 65 6d 20 69 6e 74 6f 20 74 68 65 20  * item into the 
29fac 73 65 74 20 74 61 62 6c 65 20 77 69 74 68 20 62  set table with b
29fad 6f 67 75 73 20 64 61 74 61 2e 0a 20 20 20 20 2a  ogus data..    *
29fae 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 53  /.    case SRT_S
29faf 65 74 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  et: {.      int 
29fb0 72 31 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  r1;.      assert
29fb1 28 20 70 49 6e 2d 3e 6e 4d 65 6d 3d 3d 31 20 29  ( pIn->nMem==1 )
29fb2 3b 0a 20 20 20 20 20 20 70 2d 3e 61 66 66 69 6e  ;.      p->affin
29fb3 69 74 79 20 3d 20 0a 20 20 20 20 20 20 20 20 20  ity = .         
29fb4 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66  sqlite3CompareAf
29fb5 66 69 6e 69 74 79 28 70 2d 3e 70 45 4c 69 73 74  finity(p->pEList
29fb6 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2c 20 70 44  ->a[0].pExpr, pD
29fb7 65 73 74 2d 3e 61 66 66 69 6e 69 74 79 29 3b 0a  est->affinity);.
29fb8 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74        r1 = sqlit
29fb9 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
29fba 72 73 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  rse);.      sqli
29fbb 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
29fbc 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20   OP_MakeRecord, 
29fbd 70 49 6e 2d 3e 69 4d 65 6d 2c 20 31 2c 20 72 31  pIn->iMem, 1, r1
29fbe 2c 20 26 70 2d 3e 61 66 66 69 6e 69 74 79 2c 20  , &p->affinity, 
29fbf 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
29fc0 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69  3ExprCacheAffini
29fc1 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c  tyChange(pParse,
29fc2 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20 31 29 3b 0a   pIn->iMem, 1);.
29fc3 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29fc4 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64  eAddOp2(v, OP_Id
29fc5 78 49 6e 73 65 72 74 2c 20 70 44 65 73 74 2d 3e  xInsert, pDest->
29fc6 69 50 61 72 6d 2c 20 72 31 29 3b 0a 20 20 20 20  iParm, r1);.    
29fc7 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
29fc8 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
29fc9 72 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  r1);.      break
29fca 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 20 30 20 20  ;.    }..#if 0  
29fcb 2f 2a 20 4e 65 76 65 72 20 6f 63 63 75 72 73 20  /* Never occurs 
29fcc 6f 6e 20 61 6e 20 4f 52 44 45 52 20 42 59 20 71  on an ORDER BY q
29fcd 75 65 72 79 20 2a 2f 0a 20 20 20 20 2f 2a 20 49  uery */.    /* I
29fce 66 20 61 6e 79 20 72 6f 77 20 65 78 69 73 74 20  f any row exist 
29fcf 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  in the result se
29fd0 74 2c 20 72 65 63 6f 72 64 20 74 68 61 74 20 66  t, record that f
29fd1 61 63 74 20 61 6e 64 20 61 62 6f 72 74 2e 0a 20  act and abort.. 
29fd2 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53     */.    case S
29fd3 52 54 5f 45 78 69 73 74 73 3a 20 7b 0a 20 20 20  RT_Exists: {.   
29fd4 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
29fd5 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
29fd6 65 72 2c 20 31 2c 20 70 44 65 73 74 2d 3e 69 50  er, 1, pDest->iP
29fd7 61 72 6d 29 3b 0a 20 20 20 20 20 20 2f 2a 20 54  arm);.      /* T
29fd8 68 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20  he LIMIT clause 
29fd9 77 69 6c 6c 20 74 65 72 6d 69 6e 61 74 65 20 74  will terminate t
29fda 68 65 20 6c 6f 6f 70 20 66 6f 72 20 75 73 20 2a  he loop for us *
29fdb 2f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  /.      break;. 
29fdc 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20     }.#endif..   
29fdd 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61   /* If this is a
29fde 20 73 63 61 6c 61 72 20 73 65 6c 65 63 74 20 74   scalar select t
29fdf 68 61 74 20 69 73 20 70 61 72 74 20 6f 66 20 61  hat is part of a
29fe0 6e 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 74 68  n expression, th
29fe1 65 6e 0a 20 20 20 20 2a 2a 20 73 74 6f 72 65 20  en.    ** store 
29fe2 74 68 65 20 72 65 73 75 6c 74 73 20 69 6e 20 74  the results in t
29fe3 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 6d  he appropriate m
29fe4 65 6d 6f 72 79 20 63 65 6c 6c 20 61 6e 64 20 62  emory cell and b
29fe5 72 65 61 6b 20 6f 75 74 0a 20 20 20 20 2a 2a 20  reak out.    ** 
29fe6 6f 66 20 74 68 65 20 73 63 61 6e 20 6c 6f 6f 70  of the scan loop
29fe7 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73  ..    */.    cas
29fe8 65 20 53 52 54 5f 4d 65 6d 3a 20 7b 0a 20 20 20  e SRT_Mem: {.   
29fe9 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 2d 3e     assert( pIn->
29fea 6e 4d 65 6d 3d 3d 31 20 29 3b 0a 20 20 20 20 20  nMem==1 );.     
29feb 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
29fec 4d 6f 76 65 28 70 50 61 72 73 65 2c 20 70 49 6e  Move(pParse, pIn
29fed 2d 3e 69 4d 65 6d 2c 20 70 44 65 73 74 2d 3e 69  ->iMem, pDest->i
29fee 50 61 72 6d 2c 20 31 29 3b 0a 20 20 20 20 20 20  Parm, 1);.      
29fef 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20 63 6c 61  /* The LIMIT cla
29ff0 75 73 65 20 77 69 6c 6c 20 6a 75 6d 70 20 6f 75  use will jump ou
29ff1 74 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 66 6f  t of the loop fo
29ff2 72 20 75 73 20 2a 2f 0a 20 20 20 20 20 20 62 72  r us */.      br
29ff3 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  eak;.    }.#endi
29ff4 66 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c  f /* #ifndef SQL
29ff5 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
29ff6 59 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 54 68 65  Y */..    /* The
29ff7 20 72 65 73 75 6c 74 73 20 61 72 65 20 73 74 6f   results are sto
29ff8 72 65 64 20 69 6e 20 61 20 73 65 71 75 65 6e 63  red in a sequenc
29ff9 65 20 6f 66 20 72 65 67 69 73 74 65 72 73 0a 20  e of registers. 
29ffa 20 20 20 2a 2a 20 73 74 61 72 74 69 6e 67 20 61     ** starting a
29ffb 74 20 70 44 65 73 74 2d 3e 69 4d 65 6d 2e 20 20  t pDest->iMem.  
29ffc 54 68 65 6e 20 74 68 65 20 63 6f 2d 72 6f 75 74  Then the co-rout
29ffd 69 6e 65 20 79 69 65 6c 64 73 2e 0a 20 20 20 20  ine yields..    
29ffe 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f  */.    case SRT_
29fff 43 6f 72 6f 75 74 69 6e 65 3a 20 7b 0a 20 20 20  Coroutine: {.   
2a000 20 20 20 69 66 28 20 70 44 65 73 74 2d 3e 69 4d     if( pDest->iM
2a001 65 6d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  em==0 ){.       
2a002 20 70 44 65 73 74 2d 3e 69 4d 65 6d 20 3d 20 73   pDest->iMem = s
2a003 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e  qlite3GetTempRan
2a004 67 65 28 70 50 61 72 73 65 2c 20 70 49 6e 2d 3e  ge(pParse, pIn->
2a005 6e 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 70  nMem);.        p
2a006 44 65 73 74 2d 3e 6e 4d 65 6d 20 3d 20 70 49 6e  Dest->nMem = pIn
2a007 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 7d 0a  ->nMem;.      }.
2a008 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2a009 72 43 6f 64 65 4d 6f 76 65 28 70 50 61 72 73 65  rCodeMove(pParse
2a00a 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20 70 44 65  , pIn->iMem, pDe
2a00b 73 74 2d 3e 69 4d 65 6d 2c 20 70 44 65 73 74 2d  st->iMem, pDest-
2a00c 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20 20 20 73 71  >nMem);.      sq
2a00d 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
2a00e 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 70 44 65  v, OP_Yield, pDe
2a00f 73 74 2d 3e 69 50 61 72 6d 29 3b 0a 20 20 20 20  st->iParm);.    
2a010 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a    break;.    }..
2a011 20 20 20 20 2f 2a 20 49 66 20 6e 6f 6e 65 20 6f      /* If none o
2a012 66 20 74 68 65 20 61 62 6f 76 65 2c 20 74 68 65  f the above, the
2a013 6e 20 74 68 65 20 72 65 73 75 6c 74 20 64 65 73  n the result des
2a014 74 69 6e 61 74 69 6f 6e 20 6d 75 73 74 20 62 65  tination must be
2a015 0a 20 20 20 20 2a 2a 20 53 52 54 5f 4f 75 74 70  .    ** SRT_Outp
2a016 75 74 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  ut.  This routin
2a017 65 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65  e is never calle
2a018 64 20 77 69 74 68 20 61 6e 79 20 6f 74 68 65 72  d with any other
2a019 0a 20 20 20 20 2a 2a 20 64 65 73 74 69 6e 61 74  .    ** destinat
2a01a 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 74  ion other than t
2a01b 68 65 20 6f 6e 65 73 20 68 61 6e 64 6c 65 64 20  he ones handled 
2a01c 61 62 6f 76 65 20 6f 72 20 53 52 54 5f 4f 75 74  above or SRT_Out
2a01d 70 75 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  put..    **.    
2a01e 2a 2a 20 46 6f 72 20 53 52 54 5f 4f 75 74 70 75  ** For SRT_Outpu
2a01f 74 2c 20 72 65 73 75 6c 74 73 20 61 72 65 20 73  t, results are s
2a020 74 6f 72 65 64 20 69 6e 20 61 20 73 65 71 75 65  tored in a seque
2a021 6e 63 65 20 6f 66 20 72 65 67 69 73 74 65 72 73  nce of registers
2a022 2e 20 20 0a 20 20 20 20 2a 2a 20 54 68 65 6e 20  .  .    ** Then 
2a023 74 68 65 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77  the OP_ResultRow
2a024 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65 64 20   opcode is used 
2a025 74 6f 20 63 61 75 73 65 20 73 71 6c 69 74 65 33  to cause sqlite3
2a026 5f 73 74 65 70 28 29 20 74 6f 0a 20 20 20 20 2a  _step() to.    *
2a027 2a 20 72 65 74 75 72 6e 20 74 68 65 20 6e 65 78  * return the nex
2a028 74 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 2e  t row of result.
2a029 0a 20 20 20 20 2a 2f 0a 20 20 20 20 64 65 66 61  .    */.    defa
2a02a 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73  ult: {.      ass
2a02b 65 72 74 28 20 70 44 65 73 74 2d 3e 65 44 65 73  ert( pDest->eDes
2a02c 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 29 3b  t==SRT_Output );
2a02d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2a02e 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
2a02f 65 73 75 6c 74 52 6f 77 2c 20 70 49 6e 2d 3e 69  esultRow, pIn->i
2a030 4d 65 6d 2c 20 70 49 6e 2d 3e 6e 4d 65 6d 29 3b  Mem, pIn->nMem);
2a031 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2a032 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43  prCacheAffinityC
2a033 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 70 49  hange(pParse, pI
2a034 6e 2d 3e 69 4d 65 6d 2c 20 70 49 6e 2d 3e 6e 4d  n->iMem, pIn->nM
2a035 65 6d 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  em);.      break
2a036 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
2a037 2a 20 4a 75 6d 70 20 74 6f 20 74 68 65 20 65 6e  * Jump to the en
2a038 64 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 69 66  d of the loop if
2a039 20 74 68 65 20 4c 49 4d 49 54 20 69 73 20 72 65   the LIMIT is re
2a03a 61 63 68 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66  ached..  */.  if
2a03b 28 20 70 2d 3e 69 4c 69 6d 69 74 20 29 7b 0a 20  ( p->iLimit ){. 
2a03c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2a03d 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d  dOp2(v, OP_AddIm
2a03e 6d 2c 20 70 2d 3e 69 4c 69 6d 69 74 2c 20 2d 31  m, p->iLimit, -1
2a03f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2a040 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2a041 66 5a 65 72 6f 2c 20 70 2d 3e 69 4c 69 6d 69 74  fZero, p->iLimit
2a042 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 7d 0a 0a  , iBreak);.  }..
2a043 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 74 68    /* Generate th
2a044 65 20 73 75 62 72 6f 75 74 69 6e 65 20 72 65 74  e subroutine ret
2a045 75 72 6e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  urn.  */.  sqlit
2a046 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
2a047 65 6c 28 76 2c 20 69 43 6f 6e 74 69 6e 75 65 29  el(v, iContinue)
2a048 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
2a049 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 74 75  ddOp1(v, OP_Retu
2a04a 72 6e 2c 20 72 65 67 52 65 74 75 72 6e 29 3b 0a  rn, regReturn);.
2a04b 0a 20 20 72 65 74 75 72 6e 20 61 64 64 72 3b 0a  .  return addr;.
2a04c 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 74 65 72 6e 61  }../*.** Alterna
2a04d 74 69 76 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65  tive compound se
2a04e 6c 65 63 74 20 63 6f 64 65 20 67 65 6e 65 72 61  lect code genera
2a04f 74 6f 72 20 66 6f 72 20 63 61 73 65 73 20 77 68  tor for cases wh
2a050 65 6e 20 74 68 65 72 65 0a 2a 2a 20 69 73 20 61  en there.** is a
2a051 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  n ORDER BY claus
2a052 65 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 61 73 73 75  e..**.** We assu
2a053 6d 65 20 61 20 71 75 65 72 79 20 6f 66 20 74 68  me a query of th
2a054 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d  e following form
2a055 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 3c 73 65  :.**.**      <se
2a056 6c 65 63 74 41 3e 20 20 3c 6f 70 65 72 61 74 6f  lectA>  <operato
2a057 72 3e 20 20 3c 73 65 6c 65 63 74 42 3e 20 20 4f  r>  <selectB>  O
2a058 52 44 45 52 20 42 59 20 3c 6f 72 64 65 72 62 79  RDER BY <orderby
2a059 6c 69 73 74 3e 0a 2a 2a 0a 2a 2a 20 3c 6f 70 65  list>.**.** <ope
2a05a 72 61 74 6f 72 3e 20 69 73 20 6f 6e 65 20 6f 66  rator> is one of
2a05b 20 55 4e 49 4f 4e 20 41 4c 4c 2c 20 55 4e 49 4f   UNION ALL, UNIO
2a05c 4e 2c 20 45 58 43 45 50 54 2c 20 6f 72 20 49 4e  N, EXCEPT, or IN
2a05d 54 45 52 53 45 43 54 2e 20 20 54 68 65 20 69 64  TERSECT.  The id
2a05e 65 61 0a 2a 2a 20 69 73 20 74 6f 20 63 6f 64 65  ea.** is to code
2a05f 20 62 6f 74 68 20 3c 73 65 6c 65 63 74 41 3e 20   both <selectA> 
2a060 61 6e 64 20 3c 73 65 6c 65 63 74 42 3e 20 77 69  and <selectB> wi
2a061 74 68 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  th the ORDER BY 
2a062 63 6c 61 75 73 65 20 61 73 0a 2a 2a 20 63 6f 2d  clause as.** co-
2a063 72 6f 75 74 69 6e 65 73 2e 20 20 54 68 65 6e 20  routines.  Then 
2a064 72 75 6e 20 74 68 65 20 63 6f 2d 72 6f 75 74 69  run the co-routi
2a065 6e 65 73 20 69 6e 20 70 61 72 61 6c 6c 65 6c 20  nes in parallel 
2a066 61 6e 64 20 6d 65 72 67 65 20 74 68 65 20 72 65  and merge the re
2a067 73 75 6c 74 73 0a 2a 2a 20 69 6e 74 6f 20 74 68  sults.** into th
2a068 65 20 6f 75 74 70 75 74 2e 20 20 49 6e 20 61 64  e output.  In ad
2a069 64 69 74 69 6f 6e 20 74 6f 20 74 68 65 20 74 77  dition to the tw
2a06a 6f 20 63 6f 72 6f 75 74 69 6e 65 73 20 28 63 61  o coroutines (ca
2a06b 6c 6c 65 64 20 73 65 6c 65 63 74 41 20 61 6e 64  lled selectA and
2a06c 0a 2a 2a 20 73 65 6c 65 63 74 42 29 20 74 68 65  .** selectB) the
2a06d 72 65 20 61 72 65 20 37 20 73 75 62 72 6f 75 74  re are 7 subrout
2a06e 69 6e 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 6f  ines:.**.**    o
2a06f 75 74 41 3a 20 20 20 20 4d 6f 76 65 20 74 68 65  utA:    Move the
2a070 20 6f 75 74 70 75 74 20 6f 66 20 74 68 65 20 73   output of the s
2a071 65 6c 65 63 74 41 20 63 6f 72 6f 75 74 69 6e 65  electA coroutine
2a072 20 69 6e 74 6f 20 74 68 65 20 6f 75 74 70 75 74   into the output
2a073 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
2a074 6f 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20  of the compound 
2a075 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  query..**.**    
2a076 6f 75 74 42 3a 20 20 20 20 4d 6f 76 65 20 74 68  outB:    Move th
2a077 65 20 6f 75 74 70 75 74 20 6f 66 20 74 68 65 20  e output of the 
2a078 73 65 6c 65 63 74 42 20 63 6f 72 6f 75 74 69 6e  selectB coroutin
2a079 65 20 69 6e 74 6f 20 74 68 65 20 6f 75 74 70 75  e into the outpu
2a07a 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
2a07b 20 6f 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64   of the compound
2a07c 20 71 75 65 72 79 2e 20 20 28 4f 6e 6c 79 20 67   query.  (Only g
2a07d 65 6e 65 72 61 74 65 64 20 66 6f 72 20 55 4e 49  enerated for UNI
2a07e 4f 4e 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20  ON and.**       
2a07f 20 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c 4c 2e        UNION ALL.
2a080 20 20 45 58 43 45 50 54 20 61 6e 64 20 49 4e 53    EXCEPT and INS
2a081 45 52 54 53 45 43 54 20 6e 65 76 65 72 20 6f 75  ERTSECT never ou
2a082 74 70 75 74 20 61 20 72 6f 77 20 74 68 61 74 0a  tput a row that.
2a083 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 61  **             a
2a084 70 70 65 61 72 73 20 6f 6e 6c 79 20 69 6e 20 42  ppears only in B
2a085 2e 29 0a 2a 2a 0a 2a 2a 20 20 20 20 41 6c 74 42  .).**.**    AltB
2a086 3a 20 20 20 20 43 61 6c 6c 65 64 20 77 68 65 6e  :    Called when
2a087 20 74 68 65 72 65 20 69 73 20 64 61 74 61 20 66   there is data f
2a088 72 6f 6d 20 62 6f 74 68 20 63 6f 72 6f 75 74 69  rom both corouti
2a089 6e 65 73 20 61 6e 64 20 41 3c 42 2e 0a 2a 2a 0a  nes and A<B..**.
2a08a 2a 2a 20 20 20 20 41 65 71 42 3a 20 20 20 20 43  **    AeqB:    C
2a08b 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 72 65  alled when there
2a08c 20 69 73 20 64 61 74 61 20 66 72 6f 6d 20 62 6f   is data from bo
2a08d 74 68 20 63 6f 72 6f 75 74 69 6e 65 73 20 61 6e  th coroutines an
2a08e 64 20 41 3d 3d 42 2e 0a 2a 2a 0a 2a 2a 20 20 20  d A==B..**.**   
2a08f 20 41 67 74 42 3a 20 20 20 20 43 61 6c 6c 65 64   AgtB:    Called
2a090 20 77 68 65 6e 20 74 68 65 72 65 20 69 73 20 64   when there is d
2a091 61 74 61 20 66 72 6f 6d 20 62 6f 74 68 20 63 6f  ata from both co
2a092 72 6f 75 74 69 6e 65 73 20 61 6e 64 20 41 3e 42  routines and A>B
2a093 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 45 6f 66 41 3a  ..**.**    EofA:
2a094 20 20 20 20 43 61 6c 6c 65 64 20 77 68 65 6e 20      Called when 
2a095 64 61 74 61 20 69 73 20 65 78 68 61 75 73 74 65  data is exhauste
2a096 64 20 66 72 6f 6d 20 73 65 6c 65 63 74 41 2e 0a  d from selectA..
2a097 2a 2a 0a 2a 2a 20 20 20 20 45 6f 66 42 3a 20 20  **.**    EofB:  
2a098 20 20 43 61 6c 6c 65 64 20 77 68 65 6e 20 64 61    Called when da
2a099 74 61 20 69 73 20 65 78 68 61 75 73 74 65 64 20  ta is exhausted 
2a09a 66 72 6f 6d 20 73 65 6c 65 63 74 42 2e 0a 2a 2a  from selectB..**
2a09b 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e  .** The implemen
2a09c 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 61  tation of the la
2a09d 74 74 65 72 20 66 69 76 65 20 73 75 62 72 6f 75  tter five subrou
2a09e 74 69 6e 65 73 20 64 65 70 65 6e 64 20 6f 6e 20  tines depend on 
2a09f 77 68 69 63 68 20 0a 2a 2a 20 3c 6f 70 65 72 61  which .** <opera
2a0a0 74 6f 72 3e 20 69 73 20 75 73 65 64 3a 0a 2a 2a  tor> is used:.**
2a0a1 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  .**.**          
2a0a2 20 20 20 55 4e 49 4f 4e 20 41 4c 4c 20 20 20 20     UNION ALL    
2a0a3 20 20 20 20 20 55 4e 49 4f 4e 20 20 20 20 20 20       UNION      
2a0a4 20 20 20 20 20 20 45 58 43 45 50 54 20 20 20 20        EXCEPT    
2a0a5 20 20 20 20 20 20 49 4e 54 45 52 53 45 43 54 0a        INTERSECT.
2a0a6 2a 2a 20 20 20 20 20 20 20 20 20 20 2d 2d 2d 2d  **          ----
2a0a7 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 2d 2d 2d 2d 2d  ---------  -----
2a0a8 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 2d 2d  ------------  --
2a0a9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 2d 2d  ------------  --
2a0aa 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
2a0ab 2a 2a 20 20 20 41 6c 74 42 3a 20 20 20 6f 75 74  **   AltB:   out
2a0ac 41 2c 20 6e 65 78 74 41 20 20 20 20 20 20 6f 75  A, nextA      ou
2a0ad 74 41 2c 20 6e 65 78 74 41 20 20 20 20 20 20 20  tA, nextA       
2a0ae 6f 75 74 41 2c 20 6e 65 78 74 41 20 20 20 20 20  outA, nextA     
2a0af 20 20 20 20 6e 65 78 74 41 0a 2a 2a 0a 2a 2a 20      nextA.**.** 
2a0b0 20 20 41 65 71 42 3a 20 20 20 6f 75 74 41 2c 20    AeqB:   outA, 
2a0b1 6e 65 78 74 41 20 20 20 20 20 20 20 20 20 6e 65  nextA         ne
2a0b2 78 74 41 20 20 20 20 20 20 20 20 20 20 20 20 20  xtA             
2a0b3 6e 65 78 74 41 20 20 20 20 20 20 20 20 20 6f 75  nextA         ou
2a0b4 74 41 2c 20 6e 65 78 74 41 0a 2a 2a 0a 2a 2a 20  tA, nextA.**.** 
2a0b5 20 20 41 67 74 42 3a 20 20 20 6f 75 74 42 2c 20    AgtB:   outB, 
2a0b6 6e 65 78 74 42 20 20 20 20 20 20 6f 75 74 42 2c  nextB      outB,
2a0b7 20 6e 65 78 74 42 20 20 20 20 20 20 20 20 20 20   nextB          
2a0b8 6e 65 78 74 42 20 20 20 20 20 20 20 20 20 20 20  nextB           
2a0b9 20 6e 65 78 74 42 0a 2a 2a 0a 2a 2a 20 20 20 45   nextB.**.**   E
2a0ba 6f 66 41 3a 20 20 20 6f 75 74 42 2c 20 6e 65 78  ofA:   outB, nex
2a0bb 74 42 20 20 20 20 20 20 6f 75 74 42 2c 20 6e 65  tB      outB, ne
2a0bc 78 74 42 20 20 20 20 20 20 20 20 20 20 68 61 6c  xtB          hal
2a0bd 74 20 20 20 20 20 20 20 20 20 20 20 20 20 68 61  t             ha
2a0be 6c 74 0a 2a 2a 0a 2a 2a 20 20 20 45 6f 66 42 3a  lt.**.**   EofB:
2a0bf 20 20 20 6f 75 74 41 2c 20 6e 65 78 74 41 20 20     outA, nextA  
2a0c0 20 20 20 20 6f 75 74 41 2c 20 6e 65 78 74 41 20      outA, nextA 
2a0c1 20 20 20 20 20 20 6f 75 74 41 2c 20 6e 65 78 74        outA, next
2a0c2 41 20 20 20 20 20 20 20 20 20 68 61 6c 74 0a 2a  A         halt.*
2a0c3 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 41 6c 74 42  *.** In the AltB
2a0c4 2c 20 41 65 71 42 2c 20 61 6e 64 20 41 67 74 42  , AeqB, and AgtB
2a0c5 20 73 75 62 72 6f 75 74 69 6e 65 73 2c 20 61 6e   subroutines, an
2a0c6 20 45 4f 46 20 6f 6e 20 41 20 66 6f 6c 6c 6f 77   EOF on A follow
2a0c7 69 6e 67 20 6e 65 78 74 41 0a 2a 2a 20 63 61 75  ing nextA.** cau
2a0c8 73 65 73 20 61 6e 20 69 6d 6d 65 64 69 61 74 65  ses an immediate
2a0c9 20 6a 75 6d 70 20 74 6f 20 45 6f 66 41 20 61 6e   jump to EofA an
2a0ca 64 20 61 6e 20 45 4f 46 20 6f 6e 20 42 20 66 6f  d an EOF on B fo
2a0cb 6c 6c 6f 77 69 6e 67 20 6e 65 78 74 42 20 63 61  llowing nextB ca
2a0cc 75 73 65 73 0a 2a 2a 20 61 6e 20 69 6d 6d 65 64  uses.** an immed
2a0cd 69 61 74 65 20 6a 75 6d 70 20 74 6f 20 45 6f 66  iate jump to Eof
2a0ce 42 2e 20 20 57 69 74 68 69 6e 20 45 6f 66 41 20  B.  Within EofA 
2a0cf 61 6e 64 20 45 6f 66 42 2c 20 61 6e 64 20 45 4f  and EofB, and EO
2a0d0 46 20 6f 6e 20 65 6e 74 72 79 20 6f 72 0a 2a 2a  F on entry or.**
2a0d1 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e 65 78 74 58   following nextX
2a0d2 20 63 61 75 73 65 73 20 61 20 6a 75 6d 70 20 74   causes a jump t
2a0d3 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  o the end of the
2a0d4 20 73 65 6c 65 63 74 20 70 72 6f 63 65 73 73 69   select processi
2a0d5 6e 67 2e 0a 2a 2a 0a 2a 2a 20 44 75 70 6c 69 63  ng..**.** Duplic
2a0d6 61 74 65 20 72 65 6d 6f 76 61 6c 20 69 6e 20 74  ate removal in t
2a0d7 68 65 20 55 4e 49 4f 4e 2c 20 45 58 43 45 50 54  he UNION, EXCEPT
2a0d8 2c 20 61 6e 64 20 49 4e 54 45 52 53 45 43 54 20  , and INTERSECT 
2a0d9 63 61 73 65 73 20 69 73 20 68 61 6e 64 6c 65 64  cases is handled
2a0da 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 6f  .** within the o
2a0db 75 74 70 75 74 20 73 75 62 72 6f 75 74 69 6e 65  utput subroutine
2a0dc 2e 20 20 54 68 65 20 72 65 67 50 72 65 76 20 72  .  The regPrev r
2a0dd 65 67 69 73 74 65 72 20 73 65 74 20 68 6f 6c 64  egister set hold
2a0de 73 20 74 68 65 20 70 72 65 76 69 6f 75 73 6c 79  s the previously
2a0df 0a 2a 2a 20 6f 75 74 70 75 74 20 76 61 6c 75 65  .** output value
2a0e0 2e 20 20 41 20 63 6f 6d 70 61 72 69 73 6f 6e 20  .  A comparison 
2a0e1 69 73 20 6d 61 64 65 20 61 67 61 69 6e 73 74 20  is made against 
2a0e2 74 68 69 73 20 76 61 6c 75 65 20 61 6e 64 20 74  this value and t
2a0e3 68 65 20 6f 75 74 70 75 74 0a 2a 2a 20 69 73 20  he output.** is 
2a0e4 73 6b 69 70 70 65 64 20 69 66 20 74 68 65 20 6e  skipped if the n
2a0e5 65 78 74 20 72 65 73 75 6c 74 73 20 77 6f 75 6c  ext results woul
2a0e6 64 20 62 65 20 74 68 65 20 73 61 6d 65 20 61 73  d be the same as
2a0e7 20 74 68 65 20 70 72 65 76 69 6f 75 73 2e 0a 2a   the previous..*
2a0e8 2a 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65 6d 65  *.** The impleme
2a0e9 6e 74 61 74 69 6f 6e 20 70 6c 61 6e 20 69 73 20  ntation plan is 
2a0ea 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
2a0eb 20 74 77 6f 20 63 6f 72 6f 75 74 69 6e 65 73 20   two coroutines 
2a0ec 61 6e 64 20 73 65 76 65 6e 0a 2a 2a 20 73 75 62  and seven.** sub
2a0ed 72 6f 75 74 69 6e 65 73 20 66 69 72 73 74 2c 20  routines first, 
2a0ee 74 68 65 6e 20 70 75 74 20 74 68 65 20 63 6f 6e  then put the con
2a0ef 74 72 6f 6c 20 6c 6f 67 69 63 20 61 74 20 74 68  trol logic at th
2a0f0 65 20 62 6f 74 74 6f 6d 2e 20 20 4c 69 6b 65 20  e bottom.  Like 
2a0f1 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  this:.**.**     
2a0f2 20 20 20 20 20 67 6f 74 6f 20 49 6e 69 74 0a 2a       goto Init.*
2a0f3 2a 20 20 20 20 20 63 6f 41 3a 20 63 6f 72 6f 75  *     coA: corou
2a0f4 74 69 6e 65 20 66 6f 72 20 6c 65 66 74 20 71 75  tine for left qu
2a0f5 65 72 79 20 28 41 29 0a 2a 2a 20 20 20 20 20 63  ery (A).**     c
2a0f6 6f 42 3a 20 63 6f 72 6f 75 74 69 6e 65 20 66 6f  oB: coroutine fo
2a0f7 72 20 72 69 67 68 74 20 71 75 65 72 79 20 28 42  r right query (B
2a0f8 29 0a 2a 2a 20 20 20 20 6f 75 74 41 3a 20 6f 75  ).**    outA: ou
2a0f9 74 70 75 74 20 6f 6e 65 20 72 6f 77 20 6f 66 20  tput one row of 
2a0fa 41 0a 2a 2a 20 20 20 20 6f 75 74 42 3a 20 6f 75  A.**    outB: ou
2a0fb 74 70 75 74 20 6f 6e 65 20 72 6f 77 20 6f 66 20  tput one row of 
2a0fc 42 20 28 55 4e 49 4f 4e 20 61 6e 64 20 55 4e 49  B (UNION and UNI
2a0fd 4f 4e 20 41 4c 4c 20 6f 6e 6c 79 29 0a 2a 2a 20  ON ALL only).** 
2a0fe 20 20 20 45 6f 66 41 3a 20 2e 2e 2e 0a 2a 2a 20     EofA: ....** 
2a0ff 20 20 20 45 6f 66 42 3a 20 2e 2e 2e 0a 2a 2a 20     EofB: ....** 
2a100 20 20 20 41 6c 74 42 3a 20 2e 2e 2e 0a 2a 2a 20     AltB: ....** 
2a101 20 20 20 41 65 71 42 3a 20 2e 2e 2e 0a 2a 2a 20     AeqB: ....** 
2a102 20 20 20 41 67 74 42 3a 20 2e 2e 2e 0a 2a 2a 20     AgtB: ....** 
2a103 20 20 20 49 6e 69 74 3a 20 69 6e 69 74 69 61 6c     Init: initial
2a104 69 7a 65 20 63 6f 72 6f 75 74 69 6e 65 20 72 65  ize coroutine re
2a105 67 69 73 74 65 72 73 0a 2a 2a 20 20 20 20 20 20  gisters.**      
2a106 20 20 20 20 79 69 65 6c 64 20 63 6f 41 0a 2a 2a      yield coA.**
2a107 20 20 20 20 20 20 20 20 20 20 69 66 20 65 6f 66            if eof
2a108 28 41 29 20 67 6f 74 6f 20 45 6f 66 41 0a 2a 2a  (A) goto EofA.**
2a109 20 20 20 20 20 20 20 20 20 20 79 69 65 6c 64 20            yield 
2a10a 63 6f 42 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  coB.**          
2a10b 69 66 20 65 6f 66 28 42 29 20 67 6f 74 6f 20 45  if eof(B) goto E
2a10c 6f 66 42 0a 2a 2a 20 20 20 20 43 6d 70 72 3a 20  ofB.**    Cmpr: 
2a10d 43 6f 6d 70 61 72 65 20 41 2c 20 42 0a 2a 2a 20  Compare A, B.** 
2a10e 20 20 20 20 20 20 20 20 20 4a 75 6d 70 20 41 6c           Jump Al
2a10f 74 42 2c 20 41 65 71 42 2c 20 41 67 74 42 0a 2a  tB, AeqB, AgtB.*
2a110 2a 20 20 20 20 20 45 6e 64 3a 20 2e 2e 2e 0a 2a  *     End: ....*
2a111 2a 0a 2a 2a 20 57 65 20 63 61 6c 6c 20 41 6c 74  *.** We call Alt
2a112 42 2c 20 41 65 71 42 2c 20 41 67 74 42 2c 20 45  B, AeqB, AgtB, E
2a113 6f 66 41 2c 20 61 6e 64 20 45 6f 66 42 20 22 73  ofA, and EofB "s
2a114 75 62 72 6f 75 74 69 6e 65 73 22 20 62 75 74 20  ubroutines" but 
2a115 74 68 65 79 20 61 72 65 20 6e 6f 74 0a 2a 2a 20  they are not.** 
2a116 61 63 74 75 61 6c 6c 79 20 63 61 6c 6c 65 64 20  actually called 
2a117 75 73 69 6e 67 20 47 6f 73 75 62 20 61 6e 64 20  using Gosub and 
2a118 74 68 65 79 20 64 6f 20 6e 6f 74 20 52 65 74 75  they do not Retu
2a119 72 6e 2e 20 20 45 6f 66 41 20 61 6e 64 20 45 6f  rn.  EofA and Eo
2a11a 66 42 20 6c 6f 6f 70 0a 2a 2a 20 75 6e 74 69 6c  fB loop.** until
2a11b 20 61 6c 6c 20 64 61 74 61 20 69 73 20 65 78 68   all data is exh
2a11c 61 75 73 74 65 64 20 74 68 65 6e 20 6a 75 6d 70  austed then jump
2a11d 20 74 6f 20 74 68 65 20 22 65 6e 64 22 20 6c 61   to the "end" la
2a11e 62 65 2e 20 20 41 6c 74 42 2c 20 41 65 71 42 2c  be.  AltB, AeqB,
2a11f 0a 2a 2a 20 61 6e 64 20 41 67 74 42 20 6a 75 6d  .** and AgtB jum
2a120 70 20 74 6f 20 65 69 74 68 65 72 20 4c 32 20 6f  p to either L2 o
2a121 72 20 74 6f 20 6f 6e 65 20 6f 66 20 45 6f 66 41  r to one of EofA
2a122 20 6f 72 20 45 6f 66 42 2e 0a 2a 2f 0a 23 69 66   or EofB..*/.#if
2a123 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2a124 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54  _COMPOUND_SELECT
2a125 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 75 6c 74  .static int mult
2a126 69 53 65 6c 65 63 74 4f 72 64 65 72 42 79 28 0a  iSelectOrderBy(.
2a127 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2a128 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69          /* Parsi
2a129 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
2a12a 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20  Select *p,      
2a12b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67        /* The rig
2a12c 68 74 2d 6d 6f 73 74 20 6f 66 20 53 45 4c 45 43  ht-most of SELEC
2a12d 54 73 20 74 6f 20 62 65 20 63 6f 64 65 64 20 2a  Ts to be coded *
2a12e 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 2a  /.  SelectDest *
2a12f 70 44 65 73 74 20 20 20 20 20 2f 2a 20 57 68 61  pDest     /* Wha
2a130 74 20 74 6f 20 64 6f 20 77 69 74 68 20 71 75 65  t to do with que
2a131 72 79 20 72 65 73 75 6c 74 73 20 2a 2f 0a 29 7b  ry results */.){
2a132 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20  .  int i, j;    
2a133 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
2a134 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 53   counters */.  S
2a135 65 6c 65 63 74 20 2a 70 50 72 69 6f 72 3b 20 20  elect *pPrior;  
2a136 20 20 20 20 20 2f 2a 20 41 6e 6f 74 68 65 72 20       /* Another 
2a137 53 45 4c 45 43 54 20 69 6d 6d 65 64 69 61 74 65  SELECT immediate
2a138 6c 79 20 74 6f 20 6f 75 72 20 6c 65 66 74 20 2a  ly to our left *
2a139 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20  /.  Vdbe *v;    
2a13a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47 65 6e            /* Gen
2a13b 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 74 68  erate code to th
2a13c 69 73 20 56 44 42 45 20 2a 2f 0a 20 20 53 65 6c  is VDBE */.  Sel
2a13d 65 63 74 44 65 73 74 20 64 65 73 74 41 3b 20 20  ectDest destA;  
2a13e 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f     /* Destinatio
2a13f 6e 20 66 6f 72 20 63 6f 72 6f 75 74 69 6e 65 20  n for coroutine 
2a140 41 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73  A */.  SelectDes
2a141 74 20 64 65 73 74 42 3b 20 20 20 20 20 2f 2a 20  t destB;     /* 
2a142 44 65 73 74 69 6e 61 74 69 6f 6e 20 66 6f 72 20  Destination for 
2a143 63 6f 72 6f 75 74 69 6e 65 20 42 20 2a 2f 0a 20  coroutine B */. 
2a144 20 69 6e 74 20 72 65 67 41 64 64 72 41 3b 20 20   int regAddrA;  
2a145 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
2a146 73 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 73  s register for s
2a147 65 6c 65 63 74 2d 41 20 63 6f 72 6f 75 74 69 6e  elect-A coroutin
2a148 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 45 6f  e */.  int regEo
2a149 66 41 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  fA;          /* 
2a14a 46 6c 61 67 20 74 6f 20 69 6e 64 69 63 61 74 65  Flag to indicate
2a14b 20 77 68 65 6e 20 73 65 6c 65 63 74 2d 41 20 69   when select-A i
2a14c 73 20 63 6f 6d 70 6c 65 74 65 20 2a 2f 0a 20 20  s complete */.  
2a14d 69 6e 74 20 72 65 67 41 64 64 72 42 3b 20 20 20  int regAddrB;   
2a14e 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73        /* Address
2a14f 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 73 65   register for se
2a150 6c 65 63 74 2d 42 20 63 6f 72 6f 75 74 69 6e 65  lect-B coroutine
2a151 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 45 6f 66   */.  int regEof
2a152 42 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46  B;          /* F
2a153 6c 61 67 20 74 6f 20 69 6e 64 69 63 61 74 65 20  lag to indicate 
2a154 77 68 65 6e 20 73 65 6c 65 63 74 2d 42 20 69 73  when select-B is
2a155 20 63 6f 6d 70 6c 65 74 65 20 2a 2f 0a 20 20 69   complete */.  i
2a156 6e 74 20 61 64 64 72 53 65 6c 65 63 74 41 3b 20  nt addrSelectA; 
2a157 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20       /* Address 
2a158 6f 66 20 74 68 65 20 73 65 6c 65 63 74 2d 41 20  of the select-A 
2a159 63 6f 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69  coroutine */.  i
2a15a 6e 74 20 61 64 64 72 53 65 6c 65 63 74 42 3b 20  nt addrSelectB; 
2a15b 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20       /* Address 
2a15c 6f 66 20 74 68 65 20 73 65 6c 65 63 74 2d 42 20  of the select-B 
2a15d 63 6f 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69  coroutine */.  i
2a15e 6e 74 20 72 65 67 4f 75 74 41 3b 20 20 20 20 20  nt regOutA;     
2a15f 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20       /* Address 
2a160 72 65 67 69 73 74 65 72 20 66 6f 72 20 74 68 65  register for the
2a161 20 6f 75 74 70 75 74 2d 41 20 73 75 62 72 6f 75   output-A subrou
2a162 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65  tine */.  int re
2a163 67 4f 75 74 42 3b 20 20 20 20 20 20 20 20 20 20  gOutB;          
2a164 2f 2a 20 41 64 64 72 65 73 73 20 72 65 67 69 73  /* Address regis
2a165 74 65 72 20 66 6f 72 20 74 68 65 20 6f 75 74 70  ter for the outp
2a166 75 74 2d 42 20 73 75 62 72 6f 75 74 69 6e 65 20  ut-B subroutine 
2a167 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 4f 75 74  */.  int addrOut
2a168 41 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64  A;         /* Ad
2a169 64 72 65 73 73 20 6f 66 20 74 68 65 20 6f 75 74  dress of the out
2a16a 70 75 74 2d 41 20 73 75 62 72 6f 75 74 69 6e 65  put-A subroutine
2a16b 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 4f 75   */.  int addrOu
2a16c 74 42 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 41  tB = 0;     /* A
2a16d 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 6f 75  ddress of the ou
2a16e 74 70 75 74 2d 42 20 73 75 62 72 6f 75 74 69 6e  tput-B subroutin
2a16f 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 45  e */.  int addrE
2a170 6f 66 41 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ofA;         /* 
2a171 41 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 73  Address of the s
2a172 65 6c 65 63 74 2d 41 2d 65 78 68 61 75 73 74 65  elect-A-exhauste
2a173 64 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a  d subroutine */.
2a174 20 20 69 6e 74 20 61 64 64 72 45 6f 66 42 3b 20    int addrEofB; 
2a175 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
2a176 73 73 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74  ss of the select
2a177 2d 42 2d 65 78 68 61 75 73 74 65 64 20 73 75 62  -B-exhausted sub
2a178 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74  routine */.  int
2a179 20 61 64 64 72 41 6c 74 42 3b 20 20 20 20 20 20   addrAltB;      
2a17a 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66     /* Address of
2a17b 20 74 68 65 20 41 3c 42 20 73 75 62 72 6f 75 74   the A<B subrout
2a17c 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  ine */.  int add
2a17d 72 41 65 71 42 3b 20 20 20 20 20 20 20 20 20 2f  rAeqB;         /
2a17e 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65  * Address of the
2a17f 20 41 3d 3d 42 20 73 75 62 72 6f 75 74 69 6e 65   A==B subroutine
2a180 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 41 67   */.  int addrAg
2a181 74 42 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41  tB;         /* A
2a182 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 41 3e  ddress of the A>
2a183 42 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a  B subroutine */.
2a184 20 20 69 6e 74 20 72 65 67 4c 69 6d 69 74 41 3b    int regLimitA;
2a185 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 6d 69 74          /* Limit
2a186 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 73 65   register for se
2a187 6c 65 63 74 2d 41 20 2a 2f 0a 20 20 69 6e 74 20  lect-A */.  int 
2a188 72 65 67 4c 69 6d 69 74 42 3b 20 20 20 20 20 20  regLimitB;      
2a189 20 20 2f 2a 20 4c 69 6d 69 74 20 72 65 67 69 73    /* Limit regis
2a18a 74 65 72 20 66 6f 72 20 73 65 6c 65 63 74 2d 41  ter for select-A
2a18b 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 50 72 65   */.  int regPre
2a18c 76 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41  v;          /* A
2a18d 20 72 61 6e 67 65 20 6f 66 20 72 65 67 69 73 74   range of regist
2a18e 65 72 73 20 74 6f 20 68 6f 6c 64 20 70 72 65 76  ers to hold prev
2a18f 69 6f 75 73 20 6f 75 74 70 75 74 20 2a 2f 0a 20  ious output */. 
2a190 20 69 6e 74 20 73 61 76 65 64 4c 69 6d 69 74 3b   int savedLimit;
2a191 20 20 20 20 20 20 20 2f 2a 20 53 61 76 65 64 20         /* Saved 
2a192 76 61 6c 75 65 20 6f 66 20 70 2d 3e 69 4c 69 6d  value of p->iLim
2a193 69 74 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65  it */.  int save
2a194 64 4f 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a  dOffset;      /*
2a195 20 53 61 76 65 64 20 76 61 6c 75 65 20 6f 66 20   Saved value of 
2a196 70 2d 3e 69 4f 66 66 73 65 74 20 2a 2f 0a 20 20  p->iOffset */.  
2a197 69 6e 74 20 6c 61 62 65 6c 43 6d 70 72 3b 20 20  int labelCmpr;  
2a198 20 20 20 20 20 20 2f 2a 20 4c 61 62 65 6c 20 66        /* Label f
2a199 6f 72 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  or the start of 
2a19a 74 68 65 20 6d 65 72 67 65 20 61 6c 67 6f 72 69  the merge algori
2a19b 74 68 6d 20 2a 2f 0a 20 20 69 6e 74 20 6c 61 62  thm */.  int lab
2a19c 65 6c 45 6e 64 3b 20 20 20 20 20 20 20 20 20 2f  elEnd;         /
2a19d 2a 20 4c 61 62 65 6c 20 66 6f 72 20 74 68 65 20  * Label for the 
2a19e 65 6e 64 20 6f 66 20 74 68 65 20 6f 76 65 72 61  end of the overa
2a19f 6c 6c 20 53 45 4c 45 43 54 20 73 74 6d 74 20 2a  ll SELECT stmt *
2a1a0 2f 0a 20 20 69 6e 74 20 6a 31 3b 20 20 20 20 20  /.  int j1;     
2a1a1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d            /* Jum
2a1a2 70 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 74  p instructions t
2a1a3 68 61 74 20 67 65 74 20 72 65 74 61 72 67 65 74  hat get retarget
2a1a4 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 3b  ted */.  int op;
2a1a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2a1a6 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 41 4c 4c 2c  * One of TK_ALL,
2a1a7 20 54 4b 5f 55 4e 49 4f 4e 2c 20 54 4b 5f 45 58   TK_UNION, TK_EX
2a1a8 43 45 50 54 2c 20 54 4b 5f 49 4e 54 45 52 53 45  CEPT, TK_INTERSE
2a1a9 43 54 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20  CT */.  KeyInfo 
2a1aa 2a 70 4b 65 79 44 75 70 20 3d 20 30 3b 20 2f 2a  *pKeyDup = 0; /*
2a1ab 20 43 6f 6d 70 61 72 69 73 6f 6e 20 69 6e 66 6f   Comparison info
2a1ac 72 6d 61 74 69 6f 6e 20 66 6f 72 20 64 75 70 6c  rmation for dupl
2a1ad 69 63 61 74 65 20 72 65 6d 6f 76 61 6c 20 2a 2f  icate removal */
2a1ae 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79  .  KeyInfo *pKey
2a1af 4d 65 72 67 65 3b 20 20 20 2f 2a 20 43 6f 6d 70  Merge;   /* Comp
2a1b0 61 72 69 73 6f 6e 20 69 6e 66 6f 72 6d 61 74 69  arison informati
2a1b1 6f 6e 20 66 6f 72 20 6d 65 72 67 69 6e 67 20 72  on for merging r
2a1b2 6f 77 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ows */.  sqlite3
2a1b3 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 2f   *db;          /
2a1b4 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * Database conne
2a1b5 63 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 4c  ction */.  ExprL
2a1b6 69 73 74 20 2a 70 4f 72 64 65 72 42 79 3b 20 20  ist *pOrderBy;  
2a1b7 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59   /* The ORDER BY
2a1b8 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74   clause */.  int
2a1b9 20 6e 4f 72 64 65 72 42 79 3b 20 20 20 20 20 20   nOrderBy;      
2a1ba 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
2a1bb 74 65 72 6d 73 20 69 6e 20 74 68 65 20 4f 52 44  terms in the ORD
2a1bc 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a  ER BY clause */.
2a1bd 20 20 69 6e 74 20 2a 61 50 65 72 6d 75 74 65 3b    int *aPermute;
2a1be 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 70 70 69          /* Mappi
2a1bf 6e 67 20 66 72 6f 6d 20 4f 52 44 45 52 20 42 59  ng from ORDER BY
2a1c0 20 74 65 72 6d 73 20 74 6f 20 72 65 73 75 6c 74   terms to result
2a1c1 20 73 65 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a   set columns */.
2a1c2 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4f  .  assert( p->pO
2a1c3 72 64 65 72 42 79 21 3d 30 20 29 3b 0a 20 20 61  rderBy!=0 );.  a
2a1c4 73 73 65 72 74 28 20 70 4b 65 79 44 75 70 3d 3d  ssert( pKeyDup==
2a1c5 30 20 29 3b 20 2f 2a 20 22 4d 61 6e 61 67 65 64  0 ); /* "Managed
2a1c6 22 20 63 6f 64 65 20 6e 65 65 64 73 20 74 68 69  " code needs thi
2a1c7 73 2e 20 20 54 69 63 6b 65 74 20 23 33 33 38 32  s.  Ticket #3382
2a1c8 2e 20 2a 2f 0a 20 20 64 62 20 3d 20 70 50 61 72  . */.  db = pPar
2a1c9 73 65 2d 3e 64 62 3b 0a 20 20 76 20 3d 20 70 50  se->db;.  v = pP
2a1ca 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 61  arse->pVdbe;.  a
2a1cb 73 73 65 72 74 28 20 76 21 3d 30 20 29 3b 20 20  ssert( v!=0 );  
2a1cc 20 20 20 20 20 2f 2a 20 41 6c 72 65 61 64 79 20       /* Already 
2a1cd 74 68 72 6f 77 6e 20 74 68 65 20 65 72 72 6f 72  thrown the error
2a1ce 20 69 66 20 56 44 42 45 20 61 6c 6c 6f 63 20 66   if VDBE alloc f
2a1cf 61 69 6c 65 64 20 2a 2f 0a 20 20 6c 61 62 65 6c  ailed */.  label
2a1d0 45 6e 64 20 3d 20 73 71 6c 69 74 65 33 56 64 62  End = sqlite3Vdb
2a1d1 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20  eMakeLabel(v);. 
2a1d2 20 6c 61 62 65 6c 43 6d 70 72 20 3d 20 73 71 6c   labelCmpr = sql
2a1d3 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
2a1d4 6c 28 76 29 3b 0a 0a 0a 20 20 2f 2a 20 50 61 74  l(v);...  /* Pat
2a1d5 63 68 20 75 70 20 74 68 65 20 4f 52 44 45 52 20  ch up the ORDER 
2a1d6 42 59 20 63 6c 61 75 73 65 0a 20 20 2a 2f 0a 20  BY clause.  */. 
2a1d7 20 6f 70 20 3d 20 70 2d 3e 6f 70 3b 20 20 0a 20   op = p->op;  . 
2a1d8 20 70 50 72 69 6f 72 20 3d 20 70 2d 3e 70 50 72   pPrior = p->pPr
2a1d9 69 6f 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ior;.  assert( p
2a1da 50 72 69 6f 72 2d 3e 70 4f 72 64 65 72 42 79 3d  Prior->pOrderBy=
2a1db 3d 30 20 29 3b 0a 20 20 70 4f 72 64 65 72 42 79  =0 );.  pOrderBy
2a1dc 20 3d 20 70 2d 3e 70 4f 72 64 65 72 42 79 3b 0a   = p->pOrderBy;.
2a1dd 20 20 61 73 73 65 72 74 28 20 70 4f 72 64 65 72    assert( pOrder
2a1de 42 79 20 29 3b 0a 20 20 6e 4f 72 64 65 72 42 79  By );.  nOrderBy
2a1df 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78   = pOrderBy->nEx
2a1e0 70 72 3b 0a 0a 20 20 2f 2a 20 46 6f 72 20 6f 70  pr;..  /* For op
2a1e1 65 72 61 74 6f 72 73 20 6f 74 68 65 72 20 74 68  erators other th
2a1e2 61 6e 20 55 4e 49 4f 4e 20 41 4c 4c 20 77 65 20  an UNION ALL we 
2a1e3 68 61 76 65 20 74 6f 20 6d 61 6b 65 20 73 75 72  have to make sur
2a1e4 65 20 74 68 61 74 0a 20 20 2a 2a 20 74 68 65 20  e that.  ** the 
2a1e5 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
2a1e6 63 6f 76 65 72 73 20 65 76 65 72 79 20 74 65 72  covers every ter
2a1e7 6d 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20  m of the result 
2a1e8 73 65 74 2e 20 20 41 64 64 0a 20 20 2a 2a 20 74  set.  Add.  ** t
2a1e9 65 72 6d 73 20 74 6f 20 74 68 65 20 4f 52 44 45  erms to the ORDE
2a1ea 52 20 42 59 20 63 6c 61 75 73 65 20 61 73 20 6e  R BY clause as n
2a1eb 65 63 65 73 73 61 72 79 2e 0a 20 20 2a 2f 0a 20  ecessary..  */. 
2a1ec 20 69 66 28 20 6f 70 21 3d 54 4b 5f 41 4c 4c 20   if( op!=TK_ALL 
2a1ed 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b 20  ){.    for(i=1; 
2a1ee 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2a1ef 3d 3d 30 20 26 26 20 69 3c 3d 70 2d 3e 70 45 4c  ==0 && i<=p->pEL
2a1f0 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29  ist->nExpr; i++)
2a1f1 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 45  {.      struct E
2a1f2 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  xprList_item *pI
2a1f3 74 65 6d 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a  tem;.      for(j
2a1f4 3d 30 2c 20 70 49 74 65 6d 3d 70 4f 72 64 65 72  =0, pItem=pOrder
2a1f5 42 79 2d 3e 61 3b 20 6a 3c 6e 4f 72 64 65 72 42  By->a; j<nOrderB
2a1f6 79 3b 20 6a 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29  y; j++, pItem++)
2a1f7 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
2a1f8 28 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 3e 30 20  ( pItem->iCol>0 
2a1f9 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
2a1fa 49 74 65 6d 2d 3e 69 43 6f 6c 3d 3d 69 20 29 20  Item->iCol==i ) 
2a1fb 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
2a1fc 20 20 20 20 20 69 66 28 20 6a 3d 3d 6e 4f 72 64       if( j==nOrd
2a1fd 65 72 42 79 20 29 7b 0a 20 20 20 20 20 20 20 20  erBy ){.        
2a1fe 45 78 70 72 20 2a 70 4e 65 77 20 3d 20 73 71 6c  Expr *pNew = sql
2a1ff 69 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f  ite3Expr(db, TK_
2a200 49 4e 54 45 47 45 52 2c 20 30 29 3b 0a 20 20 20  INTEGER, 0);.   
2a201 20 20 20 20 20 69 66 28 20 70 4e 65 77 3d 3d 30       if( pNew==0
2a202 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
2a203 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20  _NOMEM;.        
2a204 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45  pNew->flags |= E
2a205 50 5f 49 6e 74 56 61 6c 75 65 3b 0a 20 20 20 20  P_IntValue;.    
2a206 20 20 20 20 70 4e 65 77 2d 3e 75 2e 69 56 61 6c      pNew->u.iVal
2a207 75 65 20 3d 20 69 3b 0a 20 20 20 20 20 20 20 20  ue = i;.        
2a208 70 4f 72 64 65 72 42 79 20 3d 20 73 71 6c 69 74  pOrderBy = sqlit
2a209 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
2a20a 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65 72 42  (pParse, pOrderB
2a20b 79 2c 20 70 4e 65 77 29 3b 0a 20 20 20 20 20 20  y, pNew);.      
2a20c 20 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 6e 4f    pOrderBy->a[nO
2a20d 72 64 65 72 42 79 2b 2b 5d 2e 69 43 6f 6c 20 3d  rderBy++].iCol =
2a20e 20 28 75 31 36 29 69 3b 0a 20 20 20 20 20 20 7d   (u16)i;.      }
2a20f 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
2a210 20 43 6f 6d 70 75 74 65 20 74 68 65 20 63 6f 6d   Compute the com
2a211 70 61 72 69 73 6f 6e 20 70 65 72 6d 75 74 61 74  parison permutat
2a212 69 6f 6e 20 61 6e 64 20 6b 65 79 69 6e 66 6f 20  ion and keyinfo 
2a213 74 68 61 74 20 69 73 20 75 73 65 64 20 77 69 74  that is used wit
2a214 68 0a 20 20 2a 2a 20 74 68 65 20 70 65 72 6d 75  h.  ** the permu
2a215 74 61 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 64  tation used to d
2a216 65 74 65 72 6d 69 6e 65 20 69 66 20 74 68 65 20  etermine if the 
2a217 6e 65 78 74 0a 20 20 2a 2a 20 72 6f 77 20 6f 66  next.  ** row of
2a218 20 72 65 73 75 6c 74 73 20 63 6f 6d 65 73 20 66   results comes f
2a219 72 6f 6d 20 73 65 6c 65 63 74 41 20 6f 72 20 73  rom selectA or s
2a21a 65 6c 65 63 74 42 2e 20 20 41 6c 73 6f 20 61 64  electB.  Also ad
2a21b 64 20 65 78 70 6c 69 63 69 74 0a 20 20 2a 2a 20  d explicit.  ** 
2a21c 63 6f 6c 6c 61 74 69 6f 6e 73 20 74 6f 20 74 68  collations to th
2a21d 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
2a21e 65 20 74 65 72 6d 73 20 73 6f 20 74 68 61 74 20  e terms so that 
2a21f 77 68 65 6e 20 74 68 65 20 73 75 62 71 75 65 72  when the subquer
2a220 69 65 73 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20  ies.  ** to the 
2a221 72 69 67 68 74 20 61 6e 64 20 74 68 65 20 6c 65  right and the le
2a222 66 74 20 61 72 65 20 65 76 61 6c 75 61 74 65 64  ft are evaluated
2a223 2c 20 74 68 65 79 20 75 73 65 20 74 68 65 20 63  , they use the c
2a224 6f 72 72 65 63 74 0a 20 20 2a 2a 20 63 6f 6c 6c  orrect.  ** coll
2a225 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 61 50  ation..  */.  aP
2a226 65 72 6d 75 74 65 20 3d 20 73 71 6c 69 74 65 33  ermute = sqlite3
2a227 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
2a228 73 69 7a 65 6f 66 28 69 6e 74 29 2a 6e 4f 72 64  sizeof(int)*nOrd
2a229 65 72 42 79 29 3b 0a 20 20 69 66 28 20 61 50 65  erBy);.  if( aPe
2a22a 72 6d 75 74 65 20 29 7b 0a 20 20 20 20 73 74 72  rmute ){.    str
2a22b 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
2a22c 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 66 6f  m *pItem;.    fo
2a22d 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4f 72  r(i=0, pItem=pOr
2a22e 64 65 72 42 79 2d 3e 61 3b 20 69 3c 6e 4f 72 64  derBy->a; i<nOrd
2a22f 65 72 42 79 3b 20 69 2b 2b 2c 20 70 49 74 65 6d  erBy; i++, pItem
2a230 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  ++){.      asser
2a231 74 28 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 3e 30  t( pItem->iCol>0
2a232 20 20 26 26 20 70 49 74 65 6d 2d 3e 69 43 6f 6c    && pItem->iCol
2a233 3c 3d 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78  <=p->pEList->nEx
2a234 70 72 20 29 3b 0a 20 20 20 20 20 20 61 50 65 72  pr );.      aPer
2a235 6d 75 74 65 5b 69 5d 20 3d 20 70 49 74 65 6d 2d  mute[i] = pItem-
2a236 3e 69 43 6f 6c 20 2d 20 31 3b 0a 20 20 20 20 7d  >iCol - 1;.    }
2a237 0a 20 20 20 20 70 4b 65 79 4d 65 72 67 65 20 3d  .    pKeyMerge =
2a238 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
2a239 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69  MallocRaw(db, si
2a23a 7a 65 6f 66 28 2a 70 4b 65 79 4d 65 72 67 65 29  zeof(*pKeyMerge)
2a23b 2b 6e 4f 72 64 65 72 42 79 2a 28 73 69 7a 65 6f  +nOrderBy*(sizeo
2a23c 66 28 43 6f 6c 6c 53 65 71 2a 29 2b 31 29 29 3b  f(CollSeq*)+1));
2a23d 0a 20 20 20 20 69 66 28 20 70 4b 65 79 4d 65 72  .    if( pKeyMer
2a23e 67 65 20 29 7b 0a 20 20 20 20 20 20 70 4b 65 79  ge ){.      pKey
2a23f 4d 65 72 67 65 2d 3e 61 53 6f 72 74 4f 72 64 65  Merge->aSortOrde
2a240 72 20 3d 20 28 75 38 2a 29 26 70 4b 65 79 4d 65  r = (u8*)&pKeyMe
2a241 72 67 65 2d 3e 61 43 6f 6c 6c 5b 6e 4f 72 64 65  rge->aColl[nOrde
2a242 72 42 79 5d 3b 0a 20 20 20 20 20 20 70 4b 65 79  rBy];.      pKey
2a243 4d 65 72 67 65 2d 3e 6e 46 69 65 6c 64 20 3d 20  Merge->nField = 
2a244 28 75 31 36 29 6e 4f 72 64 65 72 42 79 3b 0a 20  (u16)nOrderBy;. 
2a245 20 20 20 20 20 70 4b 65 79 4d 65 72 67 65 2d 3e       pKeyMerge->
2a246 65 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b 0a 20  enc = ENC(db);. 
2a247 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
2a248 6e 4f 72 64 65 72 42 79 3b 20 69 2b 2b 29 7b 0a  nOrderBy; i++){.
2a249 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20          CollSeq 
2a24a 2a 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20  *pColl;.        
2a24b 45 78 70 72 20 2a 70 54 65 72 6d 20 3d 20 70 4f  Expr *pTerm = pO
2a24c 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 70 45 78  rderBy->a[i].pEx
2a24d 70 72 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  pr;.        if( 
2a24e 70 54 65 72 6d 2d 3e 66 6c 61 67 73 20 26 20 45  pTerm->flags & E
2a24f 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a  P_ExpCollate ){.
2a250 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20            pColl 
2a251 3d 20 70 54 65 72 6d 2d 3e 70 43 6f 6c 6c 3b 0a  = pTerm->pColl;.
2a252 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
2a253 20 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d           pColl =
2a254 20 6d 75 6c 74 69 53 65 6c 65 63 74 43 6f 6c 6c   multiSelectColl
2a255 53 65 71 28 70 50 61 72 73 65 2c 20 70 2c 20 61  Seq(pParse, p, a
2a256 50 65 72 6d 75 74 65 5b 69 5d 29 3b 0a 20 20 20  Permute[i]);.   
2a257 20 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 66 6c         pTerm->fl
2a258 61 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c  ags |= EP_ExpCol
2a259 6c 61 74 65 3b 0a 20 20 20 20 20 20 20 20 20 20  late;.          
2a25a 70 54 65 72 6d 2d 3e 70 43 6f 6c 6c 20 3d 20 70  pTerm->pColl = p
2a25b 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 7d 0a  Coll;.        }.
2a25c 20 20 20 20 20 20 20 20 70 4b 65 79 4d 65 72 67          pKeyMerg
2a25d 65 2d 3e 61 43 6f 6c 6c 5b 69 5d 20 3d 20 70 43  e->aColl[i] = pC
2a25e 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 70 4b 65  oll;.        pKe
2a25f 79 4d 65 72 67 65 2d 3e 61 53 6f 72 74 4f 72 64  yMerge->aSortOrd
2a260 65 72 5b 69 5d 20 3d 20 70 4f 72 64 65 72 42 79  er[i] = pOrderBy
2a261 2d 3e 61 5b 69 5d 2e 73 6f 72 74 4f 72 64 65 72  ->a[i].sortOrder
2a262 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2a263 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4b 65    }else{.    pKe
2a264 79 4d 65 72 67 65 20 3d 20 30 3b 0a 20 20 7d 0a  yMerge = 0;.  }.
2a265 0a 20 20 2f 2a 20 52 65 61 74 74 61 63 68 20 74  .  /* Reattach t
2a266 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
2a267 73 65 20 74 6f 20 74 68 65 20 71 75 65 72 79 2e  se to the query.
2a268 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70 4f 72 64 65  .  */.  p->pOrde
2a269 72 42 79 20 3d 20 70 4f 72 64 65 72 42 79 3b 0a  rBy = pOrderBy;.
2a26a 20 20 70 50 72 69 6f 72 2d 3e 70 4f 72 64 65 72    pPrior->pOrder
2a26b 42 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  By = sqlite3Expr
2a26c 4c 69 73 74 44 75 70 28 70 50 61 72 73 65 2d 3e  ListDup(pParse->
2a26d 64 62 2c 20 70 4f 72 64 65 72 42 79 2c 20 30 29  db, pOrderBy, 0)
2a26e 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  ;..  /* Allocate
2a26f 20 61 20 72 61 6e 67 65 20 6f 66 20 74 65 6d 70   a range of temp
2a270 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 73 20  orary registers 
2a271 61 6e 64 20 74 68 65 20 4b 65 79 49 6e 66 6f 20  and the KeyInfo 
2a272 6e 65 65 64 65 64 0a 20 20 2a 2a 20 66 6f 72 20  needed.  ** for 
2a273 74 68 65 20 6c 6f 67 69 63 20 74 68 61 74 20 72  the logic that r
2a274 65 6d 6f 76 65 73 20 64 75 70 6c 69 63 61 74 65  emoves duplicate
2a275 20 72 65 73 75 6c 74 20 72 6f 77 73 20 77 68 65   result rows whe
2a276 6e 20 74 68 65 0a 20 20 2a 2a 20 6f 70 65 72 61  n the.  ** opera
2a277 74 6f 72 20 69 73 20 55 4e 49 4f 4e 2c 20 45 58  tor is UNION, EX
2a278 43 45 50 54 2c 20 6f 72 20 49 4e 54 45 52 53 45  CEPT, or INTERSE
2a279 43 54 20 28 62 75 74 20 6e 6f 74 20 55 4e 49 4f  CT (but not UNIO
2a27a 4e 20 41 4c 4c 29 2e 0a 20 20 2a 2f 0a 20 20 69  N ALL)..  */.  i
2a27b 66 28 20 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 29 7b  f( op==TK_ALL ){
2a27c 0a 20 20 20 20 72 65 67 50 72 65 76 20 3d 20 30  .    regPrev = 0
2a27d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  ;.  }else{.    i
2a27e 6e 74 20 6e 45 78 70 72 20 3d 20 70 2d 3e 70 45  nt nExpr = p->pE
2a27f 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20  List->nExpr;.   
2a280 20 61 73 73 65 72 74 28 20 6e 4f 72 64 65 72 42   assert( nOrderB
2a281 79 3e 3d 6e 45 78 70 72 20 7c 7c 20 64 62 2d 3e  y>=nExpr || db->
2a282 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a  mallocFailed );.
2a283 20 20 20 20 72 65 67 50 72 65 76 20 3d 20 73 71      regPrev = sq
2a284 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67  lite3GetTempRang
2a285 65 28 70 50 61 72 73 65 2c 20 6e 45 78 70 72 2b  e(pParse, nExpr+
2a286 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
2a287 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2a288 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67 50  Integer, 0, regP
2a289 72 65 76 29 3b 0a 20 20 20 20 70 4b 65 79 44 75  rev);.    pKeyDu
2a28a 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  p = sqlite3DbMal
2a28b 6c 6f 63 5a 65 72 6f 28 64 62 2c 0a 20 20 20 20  locZero(db,.    
2a28c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 69                si
2a28d 7a 65 6f 66 28 2a 70 4b 65 79 44 75 70 29 20 2b  zeof(*pKeyDup) +
2a28e 20 6e 45 78 70 72 2a 28 73 69 7a 65 6f 66 28 43   nExpr*(sizeof(C
2a28f 6f 6c 6c 53 65 71 2a 29 2b 31 29 20 29 3b 0a 20  ollSeq*)+1) );. 
2a290 20 20 20 69 66 28 20 70 4b 65 79 44 75 70 20 29     if( pKeyDup )
2a291 7b 0a 20 20 20 20 20 20 70 4b 65 79 44 75 70 2d  {.      pKeyDup-
2a292 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 28 75  >aSortOrder = (u
2a293 38 2a 29 26 70 4b 65 79 44 75 70 2d 3e 61 43 6f  8*)&pKeyDup->aCo
2a294 6c 6c 5b 6e 45 78 70 72 5d 3b 0a 20 20 20 20 20  ll[nExpr];.     
2a295 20 70 4b 65 79 44 75 70 2d 3e 6e 46 69 65 6c 64   pKeyDup->nField
2a296 20 3d 20 28 75 31 36 29 6e 45 78 70 72 3b 0a 20   = (u16)nExpr;. 
2a297 20 20 20 20 20 70 4b 65 79 44 75 70 2d 3e 65 6e       pKeyDup->en
2a298 63 20 3d 20 45 4e 43 28 64 62 29 3b 0a 20 20 20  c = ENC(db);.   
2a299 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 45     for(i=0; i<nE
2a29a 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  xpr; i++){.     
2a29b 20 20 20 70 4b 65 79 44 75 70 2d 3e 61 43 6f 6c     pKeyDup->aCol
2a29c 6c 5b 69 5d 20 3d 20 6d 75 6c 74 69 53 65 6c 65  l[i] = multiSele
2a29d 63 74 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65  ctCollSeq(pParse
2a29e 2c 20 70 2c 20 69 29 3b 0a 20 20 20 20 20 20 20  , p, i);.       
2a29f 20 70 4b 65 79 44 75 70 2d 3e 61 53 6f 72 74 4f   pKeyDup->aSortO
2a2a0 72 64 65 72 5b 69 5d 20 3d 20 30 3b 0a 20 20 20  rder[i] = 0;.   
2a2a1 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
2a2a2 0a 20 20 2f 2a 20 53 65 70 61 72 61 74 65 20 74  .  /* Separate t
2a2a3 68 65 20 6c 65 66 74 20 61 6e 64 20 74 68 65 20  he left and the 
2a2a4 72 69 67 68 74 20 71 75 65 72 79 20 66 72 6f 6d  right query from
2a2a5 20 6f 6e 65 20 61 6e 6f 74 68 65 72 0a 20 20 2a   one another.  *
2a2a6 2f 0a 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20  /.  p->pPrior = 
2a2a7 30 3b 0a 20 20 70 50 72 69 6f 72 2d 3e 70 52 69  0;.  pPrior->pRi
2a2a8 67 68 74 6d 6f 73 74 20 3d 20 30 3b 0a 20 20 73  ghtmost = 0;.  s
2a2a9 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 64  qlite3ResolveOrd
2a2aa 65 72 47 72 6f 75 70 42 79 28 70 50 61 72 73 65  erGroupBy(pParse
2a2ab 2c 20 70 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79  , p, p->pOrderBy
2a2ac 2c 20 22 4f 52 44 45 52 22 29 3b 0a 20 20 69 66  , "ORDER");.  if
2a2ad 28 20 70 50 72 69 6f 72 2d 3e 70 50 72 69 6f 72  ( pPrior->pPrior
2a2ae 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
2a2af 65 33 52 65 73 6f 6c 76 65 4f 72 64 65 72 47 72  e3ResolveOrderGr
2a2b0 6f 75 70 42 79 28 70 50 61 72 73 65 2c 20 70 50  oupBy(pParse, pP
2a2b1 72 69 6f 72 2c 20 70 50 72 69 6f 72 2d 3e 70 4f  rior, pPrior->pO
2a2b2 72 64 65 72 42 79 2c 20 22 4f 52 44 45 52 22 29  rderBy, "ORDER")
2a2b3 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6d 70  ;.  }..  /* Comp
2a2b4 75 74 65 20 74 68 65 20 6c 69 6d 69 74 20 72 65  ute the limit re
2a2b5 67 69 73 74 65 72 73 20 2a 2f 0a 20 20 63 6f 6d  gisters */.  com
2a2b6 70 75 74 65 4c 69 6d 69 74 52 65 67 69 73 74 65  puteLimitRegiste
2a2b7 72 73 28 70 50 61 72 73 65 2c 20 70 2c 20 6c 61  rs(pParse, p, la
2a2b8 62 65 6c 45 6e 64 29 3b 0a 20 20 69 66 28 20 70  belEnd);.  if( p
2a2b9 2d 3e 69 4c 69 6d 69 74 20 26 26 20 6f 70 3d 3d  ->iLimit && op==
2a2ba 54 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 72 65  TK_ALL ){.    re
2a2bb 67 4c 69 6d 69 74 41 20 3d 20 2b 2b 70 50 61 72  gLimitA = ++pPar
2a2bc 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 72 65  se->nMem;.    re
2a2bd 67 4c 69 6d 69 74 42 20 3d 20 2b 2b 70 50 61 72  gLimitB = ++pPar
2a2be 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71  se->nMem;.    sq
2a2bf 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2a2c0 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 70 2d 3e 69  v, OP_Copy, p->i
2a2c1 4f 66 66 73 65 74 20 3f 20 70 2d 3e 69 4f 66 66  Offset ? p->iOff
2a2c2 73 65 74 2b 31 20 3a 20 70 2d 3e 69 4c 69 6d 69  set+1 : p->iLimi
2a2c3 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t,.             
2a2c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a2c5 20 20 20 20 20 72 65 67 4c 69 6d 69 74 41 29 3b       regLimitA);
2a2c6 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2a2c7 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 70  AddOp2(v, OP_Cop
2a2c8 79 2c 20 72 65 67 4c 69 6d 69 74 41 2c 20 72 65  y, regLimitA, re
2a2c9 67 4c 69 6d 69 74 42 29 3b 0a 20 20 7d 65 6c 73  gLimitB);.  }els
2a2ca 65 7b 0a 20 20 20 20 72 65 67 4c 69 6d 69 74 41  e{.    regLimitA
2a2cb 20 3d 20 72 65 67 4c 69 6d 69 74 42 20 3d 20 30   = regLimitB = 0
2a2cc 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45  ;.  }.  sqlite3E
2a2cd 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d  xprDelete(db, p-
2a2ce 3e 70 4c 69 6d 69 74 29 3b 0a 20 20 70 2d 3e 70  >pLimit);.  p->p
2a2cf 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 73 71 6c  Limit = 0;.  sql
2a2d0 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
2a2d1 62 2c 20 70 2d 3e 70 4f 66 66 73 65 74 29 3b 0a  b, p->pOffset);.
2a2d2 20 20 70 2d 3e 70 4f 66 66 73 65 74 20 3d 20 30    p->pOffset = 0
2a2d3 3b 0a 0a 20 20 72 65 67 41 64 64 72 41 20 3d 20  ;..  regAddrA = 
2a2d4 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
2a2d5 20 20 72 65 67 45 6f 66 41 20 3d 20 2b 2b 70 50    regEofA = ++pP
2a2d6 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65  arse->nMem;.  re
2a2d7 67 41 64 64 72 42 20 3d 20 2b 2b 70 50 61 72 73  gAddrB = ++pPars
2a2d8 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65 67 45 6f  e->nMem;.  regEo
2a2d9 66 42 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  fB = ++pParse->n
2a2da 4d 65 6d 3b 0a 20 20 72 65 67 4f 75 74 41 20 3d  Mem;.  regOutA =
2a2db 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
2a2dc 0a 20 20 72 65 67 4f 75 74 42 20 3d 20 2b 2b 70  .  regOutB = ++p
2a2dd 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 73  Parse->nMem;.  s
2a2de 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74  qlite3SelectDest
2a2df 49 6e 69 74 28 26 64 65 73 74 41 2c 20 53 52 54  Init(&destA, SRT
2a2e0 5f 43 6f 72 6f 75 74 69 6e 65 2c 20 72 65 67 41  _Coroutine, regA
2a2e1 64 64 72 41 29 3b 0a 20 20 73 71 6c 69 74 65 33  ddrA);.  sqlite3
2a2e2 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26  SelectDestInit(&
2a2e3 64 65 73 74 42 2c 20 53 52 54 5f 43 6f 72 6f 75  destB, SRT_Corou
2a2e4 74 69 6e 65 2c 20 72 65 67 41 64 64 72 42 29 3b  tine, regAddrB);
2a2e5 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 70 61 73 74  ..  /* Jump past
2a2e6 20 74 68 65 20 76 61 72 69 6f 75 73 20 73 75 62   the various sub
2a2e7 72 6f 75 74 69 6e 65 73 20 61 6e 64 20 63 6f 72  routines and cor
2a2e8 6f 75 74 69 6e 65 73 20 74 6f 20 74 68 65 20 6d  outines to the m
2a2e9 61 69 6e 0a 20 20 2a 2a 20 6d 65 72 67 65 20 6c  ain.  ** merge l
2a2ea 6f 6f 70 0a 20 20 2a 2f 0a 20 20 6a 31 20 3d 20  oop.  */.  j1 = 
2a2eb 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2a2ec 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0a 20  0(v, OP_Goto);. 
2a2ed 20 61 64 64 72 53 65 6c 65 63 74 41 20 3d 20 73   addrSelectA = s
2a2ee 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
2a2ef 74 41 64 64 72 28 76 29 3b 0a 0a 0a 20 20 2f 2a  tAddr(v);...  /*
2a2f0 20 47 65 6e 65 72 61 74 65 20 61 20 63 6f 72 6f   Generate a coro
2a2f1 75 74 69 6e 65 20 74 6f 20 65 76 61 6c 75 61 74  utine to evaluat
2a2f2 65 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61  e the SELECT sta
2a2f3 74 65 6d 65 6e 74 20 74 6f 20 74 68 65 0a 20 20  tement to the.  
2a2f4 2a 2a 20 6c 65 66 74 20 6f 66 20 74 68 65 20 63  ** left of the c
2a2f5 6f 6d 70 6f 75 6e 64 20 6f 70 65 72 61 74 6f 72  ompound operator
2a2f6 20 2d 20 74 68 65 20 22 41 22 20 73 65 6c 65 63   - the "A" selec
2a2f7 74 2e 0a 20 20 2a 2f 0a 20 20 56 64 62 65 4e 6f  t..  */.  VdbeNo
2a2f8 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 42  opComment((v, "B
2a2f9 65 67 69 6e 20 63 6f 72 6f 75 74 69 6e 65 20 66  egin coroutine f
2a2fa 6f 72 20 6c 65 66 74 20 53 45 4c 45 43 54 22 29  or left SELECT")
2a2fb 29 3b 0a 20 20 70 50 72 69 6f 72 2d 3e 69 4c 69  );.  pPrior->iLi
2a2fc 6d 69 74 20 3d 20 72 65 67 4c 69 6d 69 74 41 3b  mit = regLimitA;
2a2fd 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  .  sqlite3Select
2a2fe 28 70 50 61 72 73 65 2c 20 70 50 72 69 6f 72 2c  (pParse, pPrior,
2a2ff 20 26 64 65 73 74 41 29 3b 0a 20 20 73 71 6c 69   &destA);.  sqli
2a300 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2a301 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20   OP_Integer, 1, 
2a302 72 65 67 45 6f 66 41 29 3b 0a 20 20 73 71 6c 69  regEofA);.  sqli
2a303 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
2a304 20 4f 50 5f 59 69 65 6c 64 2c 20 72 65 67 41 64   OP_Yield, regAd
2a305 64 72 41 29 3b 0a 20 20 56 64 62 65 4e 6f 6f 70  drA);.  VdbeNoop
2a306 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 45 6e 64  Comment((v, "End
2a307 20 63 6f 72 6f 75 74 69 6e 65 20 66 6f 72 20 6c   coroutine for l
2a308 65 66 74 20 53 45 4c 45 43 54 22 29 29 3b 0a 0a  eft SELECT"));..
2a309 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20    /* Generate a 
2a30a 63 6f 72 6f 75 74 69 6e 65 20 74 6f 20 65 76 61  coroutine to eva
2a30b 6c 75 61 74 65 20 74 68 65 20 53 45 4c 45 43 54  luate the SELECT
2a30c 20 73 74 61 74 65 6d 65 6e 74 20 6f 6e 20 0a 20   statement on . 
2a30d 20 2a 2a 20 74 68 65 20 72 69 67 68 74 20 2d 20   ** the right - 
2a30e 74 68 65 20 22 42 22 20 73 65 6c 65 63 74 0a 20  the "B" select. 
2a30f 20 2a 2f 0a 20 20 61 64 64 72 53 65 6c 65 63 74   */.  addrSelect
2a310 42 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  B = sqlite3VdbeC
2a311 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20  urrentAddr(v);. 
2a312 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74   VdbeNoopComment
2a313 28 28 76 2c 20 22 42 65 67 69 6e 20 63 6f 72 6f  ((v, "Begin coro
2a314 75 74 69 6e 65 20 66 6f 72 20 72 69 67 68 74 20  utine for right 
2a315 53 45 4c 45 43 54 22 29 29 3b 0a 20 20 73 61 76  SELECT"));.  sav
2a316 65 64 4c 69 6d 69 74 20 3d 20 70 2d 3e 69 4c 69  edLimit = p->iLi
2a317 6d 69 74 3b 0a 20 20 73 61 76 65 64 4f 66 66 73  mit;.  savedOffs
2a318 65 74 20 3d 20 70 2d 3e 69 4f 66 66 73 65 74 3b  et = p->iOffset;
2a319 0a 20 20 70 2d 3e 69 4c 69 6d 69 74 20 3d 20 72  .  p->iLimit = r
2a31a 65 67 4c 69 6d 69 74 42 3b 0a 20 20 70 2d 3e 69  egLimitB;.  p->i
2a31b 4f 66 66 73 65 74 20 3d 20 30 3b 20 20 0a 20 20  Offset = 0;  .  
2a31c 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50  sqlite3Select(pP
2a31d 61 72 73 65 2c 20 70 2c 20 26 64 65 73 74 42 29  arse, p, &destB)
2a31e 3b 0a 20 20 70 2d 3e 69 4c 69 6d 69 74 20 3d 20  ;.  p->iLimit = 
2a31f 73 61 76 65 64 4c 69 6d 69 74 3b 0a 20 20 70 2d  savedLimit;.  p-
2a320 3e 69 4f 66 66 73 65 74 20 3d 20 73 61 76 65 64  >iOffset = saved
2a321 4f 66 66 73 65 74 3b 0a 20 20 73 71 6c 69 74 65  Offset;.  sqlite
2a322 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2a323 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 72 65  P_Integer, 1, re
2a324 67 45 6f 66 42 29 3b 0a 20 20 73 71 6c 69 74 65  gEofB);.  sqlite
2a325 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
2a326 50 5f 59 69 65 6c 64 2c 20 72 65 67 41 64 64 72  P_Yield, regAddr
2a327 42 29 3b 0a 20 20 56 64 62 65 4e 6f 6f 70 43 6f  B);.  VdbeNoopCo
2a328 6d 6d 65 6e 74 28 28 76 2c 20 22 45 6e 64 20 63  mment((v, "End c
2a329 6f 72 6f 75 74 69 6e 65 20 66 6f 72 20 72 69 67  oroutine for rig
2a32a 68 74 20 53 45 4c 45 43 54 22 29 29 3b 0a 0a 20  ht SELECT"));.. 
2a32b 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20 73   /* Generate a s
2a32c 75 62 72 6f 75 74 69 6e 65 20 74 68 61 74 20 6f  ubroutine that o
2a32d 75 74 70 75 74 73 20 74 68 65 20 63 75 72 72 65  utputs the curre
2a32e 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20 41 0a  nt row of the A.
2a32f 20 20 2a 2a 20 73 65 6c 65 63 74 20 61 73 20 74    ** select as t
2a330 68 65 20 6e 65 78 74 20 6f 75 74 70 75 74 20 72  he next output r
2a331 6f 77 20 6f 66 20 74 68 65 20 63 6f 6d 70 6f 75  ow of the compou
2a332 6e 64 20 73 65 6c 65 63 74 2e 0a 20 20 2a 2f 0a  nd select..  */.
2a333 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e    VdbeNoopCommen
2a334 74 28 28 76 2c 20 22 4f 75 74 70 75 74 20 72 6f  t((v, "Output ro
2a335 75 74 69 6e 65 20 66 6f 72 20 41 22 29 29 3b 0a  utine for A"));.
2a336 20 20 61 64 64 72 4f 75 74 41 20 3d 20 67 65 6e    addrOutA = gen
2a337 65 72 61 74 65 4f 75 74 70 75 74 53 75 62 72 6f  erateOutputSubro
2a338 75 74 69 6e 65 28 70 50 61 72 73 65 2c 0a 20 20  utine(pParse,.  
2a339 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
2a33a 2c 20 26 64 65 73 74 41 2c 20 70 44 65 73 74 2c  , &destA, pDest,
2a33b 20 72 65 67 4f 75 74 41 2c 0a 20 20 20 20 20 20   regOutA,.      
2a33c 20 20 20 20 20 20 20 20 20 20 20 72 65 67 50 72             regPr
2a33d 65 76 2c 20 70 4b 65 79 44 75 70 2c 20 50 34 5f  ev, pKeyDup, P4_
2a33e 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 2c  KEYINFO_HANDOFF,
2a33f 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 20 20 0a 20   labelEnd);.  . 
2a340 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20 73   /* Generate a s
2a341 75 62 72 6f 75 74 69 6e 65 20 74 68 61 74 20 6f  ubroutine that o
2a342 75 74 70 75 74 73 20 74 68 65 20 63 75 72 72 65  utputs the curre
2a343 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20 42 0a  nt row of the B.
2a344 20 20 2a 2a 20 73 65 6c 65 63 74 20 61 73 20 74    ** select as t
2a345 68 65 20 6e 65 78 74 20 6f 75 74 70 75 74 20 72  he next output r
2a346 6f 77 20 6f 66 20 74 68 65 20 63 6f 6d 70 6f 75  ow of the compou
2a347 6e 64 20 73 65 6c 65 63 74 2e 0a 20 20 2a 2f 0a  nd select..  */.
2a348 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 41 4c 4c    if( op==TK_ALL
2a349 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 55 4e 49 4f 4e   || op==TK_UNION
2a34a 20 29 7b 0a 20 20 20 20 56 64 62 65 4e 6f 6f 70   ){.    VdbeNoop
2a34b 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4f 75 74  Comment((v, "Out
2a34c 70 75 74 20 72 6f 75 74 69 6e 65 20 66 6f 72 20  put routine for 
2a34d 42 22 29 29 3b 0a 20 20 20 20 61 64 64 72 4f 75  B"));.    addrOu
2a34e 74 42 20 3d 20 67 65 6e 65 72 61 74 65 4f 75 74  tB = generateOut
2a34f 70 75 74 53 75 62 72 6f 75 74 69 6e 65 28 70 50  putSubroutine(pP
2a350 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 20 20  arse,.          
2a351 20 20 20 20 20 20 20 70 2c 20 26 64 65 73 74 42         p, &destB
2a352 2c 20 70 44 65 73 74 2c 20 72 65 67 4f 75 74 42  , pDest, regOutB
2a353 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2a354 20 20 20 72 65 67 50 72 65 76 2c 20 70 4b 65 79     regPrev, pKey
2a355 44 75 70 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f  Dup, P4_KEYINFO_
2a356 53 54 41 54 49 43 2c 20 6c 61 62 65 6c 45 6e 64  STATIC, labelEnd
2a357 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e  );.  }..  /* Gen
2a358 65 72 61 74 65 20 61 20 73 75 62 72 6f 75 74 69  erate a subrouti
2a359 6e 65 20 74 6f 20 72 75 6e 20 77 68 65 6e 20 74  ne to run when t
2a35a 68 65 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 20  he results from 
2a35b 73 65 6c 65 63 74 20 41 0a 20 20 2a 2a 20 61 72  select A.  ** ar
2a35c 65 20 65 78 68 61 75 73 74 65 64 20 61 6e 64 20  e exhausted and 
2a35d 6f 6e 6c 79 20 64 61 74 61 20 69 6e 20 73 65 6c  only data in sel
2a35e 65 63 74 20 42 20 72 65 6d 61 69 6e 73 2e 0a 20  ect B remains.. 
2a35f 20 2a 2f 0a 20 20 56 64 62 65 4e 6f 6f 70 43 6f   */.  VdbeNoopCo
2a360 6d 6d 65 6e 74 28 28 76 2c 20 22 65 6f 66 2d 41  mment((v, "eof-A
2a361 20 73 75 62 72 6f 75 74 69 6e 65 22 29 29 3b 0a   subroutine"));.
2a362 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 45 58 43    if( op==TK_EXC
2a363 45 50 54 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 49 4e  EPT || op==TK_IN
2a364 54 45 52 53 45 43 54 20 29 7b 0a 20 20 20 20 61  TERSECT ){.    a
2a365 64 64 72 45 6f 66 41 20 3d 20 73 71 6c 69 74 65  ddrEofA = sqlite
2a366 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2a367 50 5f 47 6f 74 6f 2c 20 30 2c 20 6c 61 62 65 6c  P_Goto, 0, label
2a368 45 6e 64 29 3b 0a 20 20 7d 65 6c 73 65 7b 20 20  End);.  }else{  
2a369 0a 20 20 20 20 61 64 64 72 45 6f 66 41 20 3d 20  .    addrEofA = 
2a36a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2a36b 32 28 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45  2(v, OP_If, regE
2a36c 6f 66 42 2c 20 6c 61 62 65 6c 45 6e 64 29 3b 0a  ofB, labelEnd);.
2a36d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2a36e 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75  ddOp2(v, OP_Gosu
2a36f 62 2c 20 72 65 67 4f 75 74 42 2c 20 61 64 64 72  b, regOutB, addr
2a370 4f 75 74 42 29 3b 0a 20 20 20 20 73 71 6c 69 74  OutB);.    sqlit
2a371 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
2a372 4f 50 5f 59 69 65 6c 64 2c 20 72 65 67 41 64 64  OP_Yield, regAdd
2a373 72 42 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  rB);.    sqlite3
2a374 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2a375 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72 45 6f  _Goto, 0, addrEo
2a376 66 41 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47  fA);.  }..  /* G
2a377 65 6e 65 72 61 74 65 20 61 20 73 75 62 72 6f 75  enerate a subrou
2a378 74 69 6e 65 20 74 6f 20 72 75 6e 20 77 68 65 6e  tine to run when
2a379 20 74 68 65 20 72 65 73 75 6c 74 73 20 66 72 6f   the results fro
2a37a 6d 20 73 65 6c 65 63 74 20 42 0a 20 20 2a 2a 20  m select B.  ** 
2a37b 61 72 65 20 65 78 68 61 75 73 74 65 64 20 61 6e  are exhausted an
2a37c 64 20 6f 6e 6c 79 20 64 61 74 61 20 69 6e 20 73  d only data in s
2a37d 65 6c 65 63 74 20 41 20 72 65 6d 61 69 6e 73 2e  elect A remains.
2a37e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6f 70 3d 3d  .  */.  if( op==
2a37f 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 29 7b 0a  TK_INTERSECT ){.
2a380 20 20 20 20 61 64 64 72 45 6f 66 42 20 3d 20 61      addrEofB = a
2a381 64 64 72 45 6f 66 41 3b 0a 20 20 7d 65 6c 73 65  ddrEofA;.  }else
2a382 7b 20 20 0a 20 20 20 20 56 64 62 65 4e 6f 6f 70  {  .    VdbeNoop
2a383 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 65 6f 66  Comment((v, "eof
2a384 2d 42 20 73 75 62 72 6f 75 74 69 6e 65 22 29 29  -B subroutine"))
2a385 3b 0a 20 20 20 20 61 64 64 72 45 6f 66 42 20 3d  ;.    addrEofB =
2a386 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2a387 70 32 28 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67  p2(v, OP_If, reg
2a388 45 6f 66 41 2c 20 6c 61 62 65 6c 45 6e 64 29 3b  EofA, labelEnd);
2a389 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2a38a 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73  AddOp2(v, OP_Gos
2a38b 75 62 2c 20 72 65 67 4f 75 74 41 2c 20 61 64 64  ub, regOutA, add
2a38c 72 4f 75 74 41 29 3b 0a 20 20 20 20 73 71 6c 69  rOutA);.    sqli
2a38d 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
2a38e 20 4f 50 5f 59 69 65 6c 64 2c 20 72 65 67 41 64   OP_Yield, regAd
2a38f 64 72 41 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  drA);.    sqlite
2a390 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2a391 50 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72 45  P_Goto, 0, addrE
2a392 6f 66 42 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ofB);.  }..  /* 
2a393 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
2a394 20 68 61 6e 64 6c 65 20 74 68 65 20 63 61 73 65   handle the case
2a395 20 6f 66 20 41 3c 42 0a 20 20 2a 2f 0a 20 20 56   of A<B.  */.  V
2a396 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28  dbeNoopComment((
2a397 76 2c 20 22 41 2d 6c 74 2d 42 20 73 75 62 72 6f  v, "A-lt-B subro
2a398 75 74 69 6e 65 22 29 29 3b 0a 20 20 61 64 64 72  utine"));.  addr
2a399 41 6c 74 42 20 3d 20 73 71 6c 69 74 65 33 56 64  AltB = sqlite3Vd
2a39a 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47  beAddOp2(v, OP_G
2a39b 6f 73 75 62 2c 20 72 65 67 4f 75 74 41 2c 20 61  osub, regOutA, a
2a39c 64 64 72 4f 75 74 41 29 3b 0a 20 20 73 71 6c 69  ddrOutA);.  sqli
2a39d 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
2a39e 20 4f 50 5f 59 69 65 6c 64 2c 20 72 65 67 41 64   OP_Yield, regAd
2a39f 64 72 41 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  drA);.  sqlite3V
2a3a0 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2a3a1 49 66 2c 20 72 65 67 45 6f 66 41 2c 20 61 64 64  If, regEofA, add
2a3a2 72 45 6f 66 41 29 3b 0a 20 20 73 71 6c 69 74 65  rEofA);.  sqlite
2a3a3 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2a3a4 50 5f 47 6f 74 6f 2c 20 30 2c 20 6c 61 62 65 6c  P_Goto, 0, label
2a3a5 43 6d 70 72 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e  Cmpr);..  /* Gen
2a3a6 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 68 61  erate code to ha
2a3a7 6e 64 6c 65 20 74 68 65 20 63 61 73 65 20 6f 66  ndle the case of
2a3a8 20 41 3d 3d 42 0a 20 20 2a 2f 0a 20 20 69 66 28   A==B.  */.  if(
2a3a9 20 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 29 7b 0a 20   op==TK_ALL ){. 
2a3aa 20 20 20 61 64 64 72 41 65 71 42 20 3d 20 61 64     addrAeqB = ad
2a3ab 64 72 41 6c 74 42 3b 0a 20 20 7d 65 6c 73 65 20  drAltB;.  }else 
2a3ac 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 4e 54 45 52  if( op==TK_INTER
2a3ad 53 45 43 54 20 29 7b 0a 20 20 20 20 61 64 64 72  SECT ){.    addr
2a3ae 41 65 71 42 20 3d 20 61 64 64 72 41 6c 74 42 3b  AeqB = addrAltB;
2a3af 0a 20 20 20 20 61 64 64 72 41 6c 74 42 2b 2b 3b  .    addrAltB++;
2a3b0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 56 64  .  }else{.    Vd
2a3b1 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76  beNoopComment((v
2a3b2 2c 20 22 41 2d 65 71 2d 42 20 73 75 62 72 6f 75  , "A-eq-B subrou
2a3b3 74 69 6e 65 22 29 29 3b 0a 20 20 20 20 61 64 64  tine"));.    add
2a3b4 72 41 65 71 42 20 3d 0a 20 20 20 20 73 71 6c 69  rAeqB =.    sqli
2a3b5 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
2a3b6 20 4f 50 5f 59 69 65 6c 64 2c 20 72 65 67 41 64   OP_Yield, regAd
2a3b7 64 72 41 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  drA);.    sqlite
2a3b8 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2a3b9 50 5f 49 66 2c 20 72 65 67 45 6f 66 41 2c 20 61  P_If, regEofA, a
2a3ba 64 64 72 45 6f 66 41 29 3b 0a 20 20 20 20 73 71  ddrEofA);.    sq
2a3bb 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2a3bc 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 6c  v, OP_Goto, 0, l
2a3bd 61 62 65 6c 43 6d 70 72 29 3b 0a 20 20 7d 0a 0a  abelCmpr);.  }..
2a3be 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f    /* Generate co
2a3bf 64 65 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 65  de to handle the
2a3c0 20 63 61 73 65 20 6f 66 20 41 3e 42 0a 20 20 2a   case of A>B.  *
2a3c1 2f 0a 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d  /.  VdbeNoopComm
2a3c2 65 6e 74 28 28 76 2c 20 22 41 2d 67 74 2d 42 20  ent((v, "A-gt-B 
2a3c3 73 75 62 72 6f 75 74 69 6e 65 22 29 29 3b 0a 20  subroutine"));. 
2a3c4 20 61 64 64 72 41 67 74 42 20 3d 20 73 71 6c 69   addrAgtB = sqli
2a3c5 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
2a3c6 64 72 28 76 29 3b 0a 20 20 69 66 28 20 6f 70 3d  dr(v);.  if( op=
2a3c7 3d 54 4b 5f 41 4c 4c 20 7c 7c 20 6f 70 3d 3d 54  =TK_ALL || op==T
2a3c8 4b 5f 55 4e 49 4f 4e 20 29 7b 0a 20 20 20 20 73  K_UNION ){.    s
2a3c9 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2a3ca 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65  (v, OP_Gosub, re
2a3cb 67 4f 75 74 42 2c 20 61 64 64 72 4f 75 74 42 29  gOutB, addrOutB)
2a3cc 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56  ;.  }.  sqlite3V
2a3cd 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2a3ce 59 69 65 6c 64 2c 20 72 65 67 41 64 64 72 42 29  Yield, regAddrB)
2a3cf 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
2a3d0 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 2c 20  ddOp2(v, OP_If, 
2a3d1 72 65 67 45 6f 66 42 2c 20 61 64 64 72 45 6f 66  regEofB, addrEof
2a3d2 42 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  B);.  sqlite3Vdb
2a3d3 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
2a3d4 74 6f 2c 20 30 2c 20 6c 61 62 65 6c 43 6d 70 72  to, 0, labelCmpr
2a3d5 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 63 6f  );..  /* This co
2a3d6 64 65 20 72 75 6e 73 20 6f 6e 63 65 20 74 6f 20  de runs once to 
2a3d7 69 6e 69 74 69 61 6c 69 7a 65 20 65 76 65 72 79  initialize every
2a3d8 74 68 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 73 71  thing..  */.  sq
2a3d9 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
2a3da 65 28 76 2c 20 6a 31 29 3b 0a 20 20 73 71 6c 69  e(v, j1);.  sqli
2a3db 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2a3dc 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20   OP_Integer, 0, 
2a3dd 72 65 67 45 6f 66 41 29 3b 0a 20 20 73 71 6c 69  regEofA);.  sqli
2a3de 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2a3df 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20   OP_Integer, 0, 
2a3e0 72 65 67 45 6f 66 42 29 3b 0a 20 20 73 71 6c 69  regEofB);.  sqli
2a3e1 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2a3e2 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 41 64   OP_Gosub, regAd
2a3e3 64 72 41 2c 20 61 64 64 72 53 65 6c 65 63 74 41  drA, addrSelectA
2a3e4 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
2a3e5 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73  AddOp2(v, OP_Gos
2a3e6 75 62 2c 20 72 65 67 41 64 64 72 42 2c 20 61 64  ub, regAddrB, ad
2a3e7 64 72 53 65 6c 65 63 74 42 29 3b 0a 20 20 73 71  drSelectB);.  sq
2a3e8 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2a3e9 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66  v, OP_If, regEof
2a3ea 41 2c 20 61 64 64 72 45 6f 66 41 29 3b 0a 20 20  A, addrEofA);.  
2a3eb 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2a3ec 32 28 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45  2(v, OP_If, regE
2a3ed 6f 66 42 2c 20 61 64 64 72 45 6f 66 42 29 3b 0a  ofB, addrEofB);.
2a3ee 0a 20 20 2f 2a 20 49 6d 70 6c 65 6d 65 6e 74 20  .  /* Implement 
2a3ef 74 68 65 20 6d 61 69 6e 20 6d 65 72 67 65 20 6c  the main merge l
2a3f0 6f 6f 70 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  oop.  */.  sqlit
2a3f1 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
2a3f2 65 6c 28 76 2c 20 6c 61 62 65 6c 43 6d 70 72 29  el(v, labelCmpr)
2a3f3 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
2a3f4 64 64 4f 70 34 28 76 2c 20 4f 50 5f 50 65 72 6d  ddOp4(v, OP_Perm
2a3f5 75 74 61 74 69 6f 6e 2c 20 30 2c 20 30 2c 20 30  utation, 0, 0, 0
2a3f6 2c 20 28 63 68 61 72 2a 29 61 50 65 72 6d 75 74  , (char*)aPermut
2a3f7 65 2c 20 50 34 5f 49 4e 54 41 52 52 41 59 29 3b  e, P4_INTARRAY);
2a3f8 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
2a3f9 64 4f 70 34 28 76 2c 20 4f 50 5f 43 6f 6d 70 61  dOp4(v, OP_Compa
2a3fa 72 65 2c 20 64 65 73 74 41 2e 69 4d 65 6d 2c 20  re, destA.iMem, 
2a3fb 64 65 73 74 42 2e 69 4d 65 6d 2c 20 6e 4f 72 64  destB.iMem, nOrd
2a3fc 65 72 42 79 2c 0a 20 20 20 20 20 20 20 20 20 20  erBy,.          
2a3fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
2a3fe 63 68 61 72 2a 29 70 4b 65 79 4d 65 72 67 65 2c  char*)pKeyMerge,
2a3ff 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44   P4_KEYINFO_HAND
2a400 4f 46 46 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  OFF);.  sqlite3V
2a401 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2a402 4a 75 6d 70 2c 20 61 64 64 72 41 6c 74 42 2c 20  Jump, addrAltB, 
2a403 61 64 64 72 41 65 71 42 2c 20 61 64 64 72 41 67  addrAeqB, addrAg
2a404 74 42 29 3b 0a 0a 20 20 2f 2a 20 52 65 6c 65 61  tB);..  /* Relea
2a405 73 65 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67  se temporary reg
2a406 69 73 74 65 72 73 0a 20 20 2a 2f 0a 20 20 69 66  isters.  */.  if
2a407 28 20 72 65 67 50 72 65 76 20 29 7b 0a 20 20 20  ( regPrev ){.   
2a408 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
2a409 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c  empRange(pParse,
2a40a 20 72 65 67 50 72 65 76 2c 20 6e 4f 72 64 65 72   regPrev, nOrder
2a40b 42 79 2b 31 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  By+1);.  }..  /*
2a40c 20 4a 75 6d 70 20 74 6f 20 74 68 65 20 74 68 69   Jump to the thi
2a40d 73 20 70 6f 69 6e 74 20 69 6e 20 6f 72 64 65 72  s point in order
2a40e 20 74 6f 20 74 65 72 6d 69 6e 61 74 65 20 74 68   to terminate th
2a40f 65 20 71 75 65 72 79 2e 0a 20 20 2a 2f 0a 20 20  e query..  */.  
2a410 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c  sqlite3VdbeResol
2a411 76 65 4c 61 62 65 6c 28 76 2c 20 6c 61 62 65 6c  veLabel(v, label
2a412 45 6e 64 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20  End);..  /* Set 
2a413 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 75  the number of ou
2a414 74 70 75 74 20 63 6f 6c 75 6d 6e 73 0a 20 20 2a  tput columns.  *
2a415 2f 0a 20 20 69 66 28 20 70 44 65 73 74 2d 3e 65  /.  if( pDest->e
2a416 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74  Dest==SRT_Output
2a417 20 29 7b 0a 20 20 20 20 53 65 6c 65 63 74 20 2a   ){.    Select *
2a418 70 46 69 72 73 74 20 3d 20 70 50 72 69 6f 72 3b  pFirst = pPrior;
2a419 0a 20 20 20 20 77 68 69 6c 65 28 20 70 46 69 72  .    while( pFir
2a41a 73 74 2d 3e 70 50 72 69 6f 72 20 29 20 70 46 69  st->pPrior ) pFi
2a41b 72 73 74 20 3d 20 70 46 69 72 73 74 2d 3e 70 50  rst = pFirst->pP
2a41c 72 69 6f 72 3b 0a 20 20 20 20 67 65 6e 65 72 61  rior;.    genera
2a41d 74 65 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50  teColumnNames(pP
2a41e 61 72 73 65 2c 20 30 2c 20 70 46 69 72 73 74 2d  arse, 0, pFirst-
2a41f 3e 70 45 4c 69 73 74 29 3b 0a 20 20 7d 0a 0a 20  >pEList);.  }.. 
2a420 20 2f 2a 20 52 65 61 73 73 65 6d 62 6c 79 20 74   /* Reassembly t
2a421 68 65 20 63 6f 6d 70 6f 75 6e 64 20 71 75 65 72  he compound quer
2a422 79 20 73 6f 20 74 68 61 74 20 69 74 20 77 69 6c  y so that it wil
2a423 6c 20 62 65 20 66 72 65 65 64 20 63 6f 72 72 65  l be freed corre
2a424 63 74 6c 79 0a 20 20 2a 2a 20 62 79 20 74 68 65  ctly.  ** by the
2a425 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f   calling functio
2a426 6e 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 50  n */.  if( p->pP
2a427 72 69 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c 69  rior ){.    sqli
2a428 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
2a429 64 62 2c 20 70 2d 3e 70 50 72 69 6f 72 29 3b 0a  db, p->pPrior);.
2a42a 20 20 7d 0a 20 20 70 2d 3e 70 50 72 69 6f 72 20    }.  p->pPrior 
2a42b 3d 20 70 50 72 69 6f 72 3b 0a 0a 20 20 2f 2a 2a  = pPrior;..  /**
2a42c 2a 20 54 42 44 3a 20 20 49 6e 73 65 72 74 20 73  * TBD:  Insert s
2a42d 75 62 72 6f 75 74 69 6e 65 20 63 61 6c 6c 73 20  ubroutine calls 
2a42e 74 6f 20 63 6c 6f 73 65 20 63 75 72 73 6f 72 73  to close cursors
2a42f 20 6f 6e 20 69 6e 63 6f 6d 70 6c 65 74 65 0a 20   on incomplete. 
2a430 20 2a 2a 2a 2a 20 73 75 62 71 75 65 72 69 65 73   **** subqueries
2a431 20 2a 2a 2a 2a 2f 0a 20 20 72 65 74 75 72 6e 20   ****/.  return 
2a432 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e  SQLITE_OK;.}.#en
2a433 64 69 66 0a 0a 23 69 66 20 21 64 65 66 69 6e 65  dif..#if !define
2a434 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  d(SQLITE_OMIT_SU
2a435 42 51 55 45 52 59 29 20 7c 7c 20 21 64 65 66 69  BQUERY) || !defi
2a436 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
2a437 56 49 45 57 29 0a 2f 2a 20 46 6f 72 77 61 72 64  VIEW)./* Forward
2a438 20 44 65 63 6c 61 72 61 74 69 6f 6e 73 20 2a 2f   Declarations */
2a439 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 75 62  .static void sub
2a43a 73 74 45 78 70 72 4c 69 73 74 28 73 71 6c 69 74  stExprList(sqlit
2a43b 65 33 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20  e3*, ExprList*, 
2a43c 69 6e 74 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b  int, ExprList*);
2a43d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 75 62  .static void sub
2a43e 73 74 53 65 6c 65 63 74 28 73 71 6c 69 74 65 33  stSelect(sqlite3
2a43f 2a 2c 20 53 65 6c 65 63 74 20 2a 2c 20 69 6e 74  *, Select *, int
2a440 2c 20 45 78 70 72 4c 69 73 74 20 2a 29 3b 0a 0a  , ExprList *);..
2a441 2f 2a 0a 2a 2a 20 53 63 61 6e 20 74 68 72 6f 75  /*.** Scan throu
2a442 67 68 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  gh the expressio
2a443 6e 20 70 45 78 70 72 2e 20 20 52 65 70 6c 61 63  n pExpr.  Replac
2a444 65 20 65 76 65 72 79 20 72 65 66 65 72 65 6e 63  e every referenc
2a445 65 20 74 6f 0a 2a 2a 20 61 20 63 6f 6c 75 6d 6e  e to.** a column
2a446 20 69 6e 20 74 61 62 6c 65 20 6e 75 6d 62 65 72   in table number
2a447 20 69 54 61 62 6c 65 20 77 69 74 68 20 61 20 63   iTable with a c
2a448 6f 70 79 20 6f 66 20 74 68 65 20 69 43 6f 6c 75  opy of the iColu
2a449 6d 6e 2d 74 68 0a 2a 2a 20 65 6e 74 72 79 20 69  mn-th.** entry i
2a44a 6e 20 70 45 4c 69 73 74 2e 20 20 28 42 75 74 20  n pEList.  (But 
2a44b 6c 65 61 76 65 20 72 65 66 65 72 65 6e 63 65 73  leave references
2a44c 20 74 6f 20 74 68 65 20 52 4f 57 49 44 20 63 6f   to the ROWID co
2a44d 6c 75 6d 6e 20 0a 2a 2a 20 75 6e 63 68 61 6e 67  lumn .** unchang
2a44e 65 64 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ed.).**.** This 
2a44f 72 6f 75 74 69 6e 65 20 69 73 20 70 61 72 74 20  routine is part 
2a450 6f 66 20 74 68 65 20 66 6c 61 74 74 65 6e 69 6e  of the flattenin
2a451 67 20 70 72 6f 63 65 64 75 72 65 2e 20 20 41 20  g procedure.  A 
2a452 73 75 62 71 75 65 72 79 0a 2a 2a 20 77 68 6f 73  subquery.** whos
2a453 65 20 72 65 73 75 6c 74 20 73 65 74 20 69 73 20  e result set is 
2a454 64 65 66 69 6e 65 64 20 62 79 20 70 45 4c 69 73  defined by pELis
2a455 74 20 61 70 70 65 61 72 73 20 61 73 20 65 6e 74  t appears as ent
2a456 72 79 20 69 6e 20 74 68 65 0a 2a 2a 20 46 52 4f  ry in the.** FRO
2a457 4d 20 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45  M clause of a SE
2a458 4c 45 43 54 20 73 75 63 68 20 74 68 61 74 20 74  LECT such that t
2a459 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 61  he VDBE cursor a
2a45a 73 73 69 67 6e 65 64 20 74 6f 20 74 68 61 74 0a  ssigned to that.
2a45b 2a 2a 20 46 4f 52 4d 20 63 6c 61 75 73 65 20 65  ** FORM clause e
2a45c 6e 74 72 79 20 69 73 20 69 54 61 62 6c 65 2e 20  ntry is iTable. 
2a45d 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 61   This routine ma
2a45e 6b 65 20 74 68 65 20 6e 65 63 65 73 73 61 72 79  ke the necessary
2a45f 20 0a 2a 2a 20 63 68 61 6e 67 65 73 20 74 6f 20   .** changes to 
2a460 70 45 78 70 72 20 73 6f 20 74 68 61 74 20 69 74  pExpr so that it
2a461 20 72 65 66 65 72 73 20 64 69 72 65 63 74 6c 79   refers directly
2a462 20 74 6f 20 74 68 65 20 73 6f 75 72 63 65 20 74   to the source t
2a463 61 62 6c 65 0a 2a 2a 20 6f 66 20 74 68 65 20 73  able.** of the s
2a464 75 62 71 75 65 72 79 20 72 61 74 68 65 72 20 74  ubquery rather t
2a465 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66  he result set of
2a466 20 74 68 65 20 73 75 62 71 75 65 72 79 2e 0a 2a   the subquery..*
2a467 2f 0a 73 74 61 74 69 63 20 45 78 70 72 20 2a 73  /.static Expr *s
2a468 75 62 73 74 45 78 70 72 28 0a 20 20 73 71 6c 69  ubstExpr(.  sqli
2a469 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
2a46a 2f 2a 20 52 65 70 6f 72 74 20 6d 61 6c 6c 6f 63  /* Report malloc
2a46b 20 65 72 72 6f 72 73 20 74 6f 20 74 68 69 73 20   errors to this 
2a46c 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
2a46d 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20  Expr *pExpr,    
2a46e 20 20 20 20 2f 2a 20 45 78 70 72 20 69 6e 20 77      /* Expr in w
2a46f 68 69 63 68 20 73 75 62 73 74 69 74 75 74 69 6f  hich substitutio
2a470 6e 20 6f 63 63 75 72 73 20 2a 2f 0a 20 20 69 6e  n occurs */.  in
2a471 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20  t iTable,       
2a472 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20 62 65    /* Table to be
2a473 20 73 75 62 73 74 69 74 75 74 65 64 20 2a 2f 0a   substituted */.
2a474 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69    ExprList *pELi
2a475 73 74 20 20 20 20 2f 2a 20 53 75 62 73 74 69 74  st    /* Substit
2a476 75 74 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20  ute expressions 
2a477 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 70 45 78 70  */.){.  if( pExp
2a478 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  r==0 ) return 0;
2a479 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70  .  if( pExpr->op
2a47a 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 70  ==TK_COLUMN && p
2a47b 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 69 54  Expr->iTable==iT
2a47c 61 62 6c 65 20 29 7b 0a 20 20 20 20 69 66 28 20  able ){.    if( 
2a47d 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3c 30  pExpr->iColumn<0
2a47e 20 29 7b 0a 20 20 20 20 20 20 70 45 78 70 72 2d   ){.      pExpr-
2a47f 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a 20  >op = TK_NULL;. 
2a480 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2a481 45 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20 20 20  Expr *pNew;.    
2a482 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 73 74    assert( pEList
2a483 21 3d 30 20 26 26 20 70 45 78 70 72 2d 3e 69 43  !=0 && pExpr->iC
2a484 6f 6c 75 6d 6e 3c 70 45 4c 69 73 74 2d 3e 6e 45  olumn<pEList->nE
2a485 78 70 72 20 29 3b 0a 20 20 20 20 20 20 61 73 73  xpr );.      ass
2a486 65 72 74 28 20 70 45 78 70 72 2d 3e 70 4c 65 66  ert( pExpr->pLef
2a487 74 3d 3d 30 20 26 26 20 70 45 78 70 72 2d 3e 70  t==0 && pExpr->p
2a488 52 69 67 68 74 3d 3d 30 20 29 3b 0a 20 20 20 20  Right==0 );.    
2a489 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    pNew = sqlite3
2a48a 45 78 70 72 44 75 70 28 64 62 2c 20 70 45 4c 69  ExprDup(db, pELi
2a48b 73 74 2d 3e 61 5b 70 45 78 70 72 2d 3e 69 43 6f  st->a[pExpr->iCo
2a48c 6c 75 6d 6e 5d 2e 70 45 78 70 72 2c 20 30 29 3b  lumn].pExpr, 0);
2a48d 0a 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 20  .      if( pNew 
2a48e 26 26 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 20  && pExpr->pColl 
2a48f 29 7b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d  ){.        pNew-
2a490 3e 70 43 6f 6c 6c 20 3d 20 70 45 78 70 72 2d 3e  >pColl = pExpr->
2a491 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20  pColl;.      }. 
2a492 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2a493 44 65 6c 65 74 65 28 64 62 2c 20 70 45 78 70 72  Delete(db, pExpr
2a494 29 3b 0a 20 20 20 20 20 20 70 45 78 70 72 20 3d  );.      pExpr =
2a495 20 70 4e 65 77 3b 0a 20 20 20 20 7d 0a 20 20 7d   pNew;.    }.  }
2a496 65 6c 73 65 7b 0a 20 20 20 20 70 45 78 70 72 2d  else{.    pExpr-
2a497 3e 70 4c 65 66 74 20 3d 20 73 75 62 73 74 45 78  >pLeft = substEx
2a498 70 72 28 64 62 2c 20 70 45 78 70 72 2d 3e 70 4c  pr(db, pExpr->pL
2a499 65 66 74 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c  eft, iTable, pEL
2a49a 69 73 74 29 3b 0a 20 20 20 20 70 45 78 70 72 2d  ist);.    pExpr-
2a49b 3e 70 52 69 67 68 74 20 3d 20 73 75 62 73 74 45  >pRight = substE
2a49c 78 70 72 28 64 62 2c 20 70 45 78 70 72 2d 3e 70  xpr(db, pExpr->p
2a49d 52 69 67 68 74 2c 20 69 54 61 62 6c 65 2c 20 70  Right, iTable, p
2a49e 45 4c 69 73 74 29 3b 0a 20 20 20 20 69 66 28 20  EList);.    if( 
2a49f 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
2a4a0 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c  pExpr, EP_xIsSel
2a4a1 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 73 75  ect) ){.      su
2a4a2 62 73 74 53 65 6c 65 63 74 28 64 62 2c 20 70 45  bstSelect(db, pE
2a4a3 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 2c 20  xpr->x.pSelect, 
2a4a4 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b  iTable, pEList);
2a4a5 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2a4a6 20 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 28    substExprList(
2a4a7 64 62 2c 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69  db, pExpr->x.pLi
2a4a8 73 74 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69  st, iTable, pELi
2a4a9 73 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  st);.    }.  }. 
2a4aa 20 72 65 74 75 72 6e 20 70 45 78 70 72 3b 0a 7d   return pExpr;.}
2a4ab 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 75 62  .static void sub
2a4ac 73 74 45 78 70 72 4c 69 73 74 28 0a 20 20 73 71  stExprList(.  sq
2a4ad 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
2a4ae 20 20 20 2f 2a 20 52 65 70 6f 72 74 20 6d 61 6c     /* Report mal
2a4af 6c 6f 63 20 65 72 72 6f 72 73 20 68 65 72 65 20  loc errors here 
2a4b0 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
2a4b1 4c 69 73 74 2c 20 20 20 20 20 2f 2a 20 4c 69 73  List,     /* Lis
2a4b2 74 20 74 6f 20 73 63 61 6e 20 61 6e 64 20 69 6e  t to scan and in
2a4b3 20 77 68 69 63 68 20 74 6f 20 6d 61 6b 65 20 73   which to make s
2a4b4 75 62 73 74 69 74 75 74 65 73 20 2a 2f 0a 20 20  ubstitutes */.  
2a4b5 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20  int iTable,     
2a4b6 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f       /* Table to
2a4b7 20 62 65 20 73 75 62 73 74 69 74 75 74 65 64 20   be substituted 
2a4b8 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
2a4b9 45 4c 69 73 74 20 20 20 20 20 2f 2a 20 53 75 62  EList     /* Sub
2a4ba 73 74 69 74 75 74 65 20 76 61 6c 75 65 73 20 2a  stitute values *
2a4bb 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  /.){.  int i;.  
2a4bc 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72  if( pList==0 ) r
2a4bd 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 30  eturn;.  for(i=0
2a4be 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72  ; i<pList->nExpr
2a4bf 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 4c 69 73  ; i++){.    pLis
2a4c0 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 20 3d 20  t->a[i].pExpr = 
2a4c1 73 75 62 73 74 45 78 70 72 28 64 62 2c 20 70 4c  substExpr(db, pL
2a4c2 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 2c  ist->a[i].pExpr,
2a4c3 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29   iTable, pEList)
2a4c4 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76  ;.  }.}.static v
2a4c5 6f 69 64 20 73 75 62 73 74 53 65 6c 65 63 74 28  oid substSelect(
2a4c6 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
2a4c7 20 20 20 20 20 20 20 20 2f 2a 20 52 65 70 6f 72          /* Repor
2a4c8 74 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 73 20  t malloc errors 
2a4c9 68 65 72 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74  here */.  Select
2a4ca 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 2f   *p,           /
2a4cb 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  * SELECT stateme
2a4cc 6e 74 20 69 6e 20 77 68 69 63 68 20 74 6f 20 6d  nt in which to m
2a4cd 61 6b 65 20 73 75 62 73 74 69 74 75 74 69 6f 6e  ake substitution
2a4ce 73 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c  s */.  int iTabl
2a4cf 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  e,          /* T
2a4d0 61 62 6c 65 20 74 6f 20 62 65 20 72 65 70 6c 61  able to be repla
2a4d1 63 65 64 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ced */.  ExprLis
2a4d2 74 20 2a 70 45 4c 69 73 74 20 20 20 20 20 2f 2a  t *pEList     /*
2a4d3 20 53 75 62 73 74 69 74 75 74 65 20 76 61 6c 75   Substitute valu
2a4d4 65 73 20 2a 2f 0a 29 7b 0a 20 20 53 72 63 4c 69  es */.){.  SrcLi
2a4d5 73 74 20 2a 70 53 72 63 3b 0a 20 20 73 74 72 75  st *pSrc;.  stru
2a4d6 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
2a4d7 2a 70 49 74 65 6d 3b 0a 20 20 69 6e 74 20 69 3b  *pItem;.  int i;
2a4d8 0a 20 20 69 66 28 20 21 70 20 29 20 72 65 74 75  .  if( !p ) retu
2a4d9 72 6e 3b 0a 20 20 73 75 62 73 74 45 78 70 72 4c  rn;.  substExprL
2a4da 69 73 74 28 64 62 2c 20 70 2d 3e 70 45 4c 69 73  ist(db, p->pELis
2a4db 74 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73  t, iTable, pELis
2a4dc 74 29 3b 0a 20 20 73 75 62 73 74 45 78 70 72 4c  t);.  substExprL
2a4dd 69 73 74 28 64 62 2c 20 70 2d 3e 70 47 72 6f 75  ist(db, p->pGrou
2a4de 70 42 79 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c  pBy, iTable, pEL
2a4df 69 73 74 29 3b 0a 20 20 73 75 62 73 74 45 78 70  ist);.  substExp
2a4e0 72 4c 69 73 74 28 64 62 2c 20 70 2d 3e 70 4f 72  rList(db, p->pOr
2a4e1 64 65 72 42 79 2c 20 69 54 61 62 6c 65 2c 20 70  derBy, iTable, p
2a4e2 45 4c 69 73 74 29 3b 0a 20 20 70 2d 3e 70 48 61  EList);.  p->pHa
2a4e3 76 69 6e 67 20 3d 20 73 75 62 73 74 45 78 70 72  ving = substExpr
2a4e4 28 64 62 2c 20 70 2d 3e 70 48 61 76 69 6e 67 2c  (db, p->pHaving,
2a4e5 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29   iTable, pEList)
2a4e6 3b 0a 20 20 70 2d 3e 70 57 68 65 72 65 20 3d 20  ;.  p->pWhere = 
2a4e7 73 75 62 73 74 45 78 70 72 28 64 62 2c 20 70 2d  substExpr(db, p-
2a4e8 3e 70 57 68 65 72 65 2c 20 69 54 61 62 6c 65 2c  >pWhere, iTable,
2a4e9 20 70 45 4c 69 73 74 29 3b 0a 20 20 73 75 62 73   pEList);.  subs
2a4ea 74 53 65 6c 65 63 74 28 64 62 2c 20 70 2d 3e 70  tSelect(db, p->p
2a4eb 50 72 69 6f 72 2c 20 69 54 61 62 6c 65 2c 20 70  Prior, iTable, p
2a4ec 45 4c 69 73 74 29 3b 0a 20 20 70 53 72 63 20 3d  EList);.  pSrc =
2a4ed 20 70 2d 3e 70 53 72 63 3b 0a 20 20 61 73 73 65   p->pSrc;.  asse
2a4ee 72 74 28 20 70 53 72 63 20 29 3b 20 20 2f 2a 20  rt( pSrc );  /* 
2a4ef 45 76 65 6e 20 66 6f 72 20 28 53 45 4c 45 43 54  Even for (SELECT
2a4f0 20 31 29 20 77 65 20 68 61 76 65 3a 20 70 53 72   1) we have: pSr
2a4f1 63 21 3d 30 20 62 75 74 20 70 53 72 63 2d 3e 6e  c!=0 but pSrc->n
2a4f2 53 72 63 3d 3d 30 20 2a 2f 0a 20 20 69 66 28 20  Src==0 */.  if( 
2a4f3 41 4c 57 41 59 53 28 70 53 72 63 29 20 29 7b 0a  ALWAYS(pSrc) ){.
2a4f4 20 20 20 20 66 6f 72 28 69 3d 70 53 72 63 2d 3e      for(i=pSrc->
2a4f5 6e 53 72 63 2c 20 70 49 74 65 6d 3d 70 53 72 63  nSrc, pItem=pSrc
2a4f6 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70  ->a; i>0; i--, p
2a4f7 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 73  Item++){.      s
2a4f8 75 62 73 74 53 65 6c 65 63 74 28 64 62 2c 20 70  ubstSelect(db, p
2a4f9 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 2c 20 69  Item->pSelect, i
2a4fa 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a  Table, pEList);.
2a4fb 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64      }.  }.}.#end
2a4fc 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53  if /* !defined(S
2a4fd 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
2a4fe 45 52 59 29 20 7c 7c 20 21 64 65 66 69 6e 65 64  ERY) || !defined
2a4ff 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45  (SQLITE_OMIT_VIE
2a500 57 29 20 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69  W) */..#if !defi
2a501 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
2a502 53 55 42 51 55 45 52 59 29 20 7c 7c 20 21 64 65  SUBQUERY) || !de
2a503 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
2a504 54 5f 56 49 45 57 29 0a 2f 2a 0a 2a 2a 20 54 68  T_VIEW)./*.** Th
2a505 69 73 20 72 6f 75 74 69 6e 65 20 61 74 74 65 6d  is routine attem
2a506 70 74 73 20 74 6f 20 66 6c 61 74 74 65 6e 20 73  pts to flatten s
2a507 75 62 71 75 65 72 69 65 73 20 69 6e 20 6f 72 64  ubqueries in ord
2a508 65 72 20 74 6f 20 73 70 65 65 64 0a 2a 2a 20 65  er to speed.** e
2a509 78 65 63 75 74 69 6f 6e 2e 20 20 49 74 20 72 65  xecution.  It re
2a50a 74 75 72 6e 73 20 31 20 69 66 20 69 74 20 6d 61  turns 1 if it ma
2a50b 6b 65 73 20 63 68 61 6e 67 65 73 20 61 6e 64 20  kes changes and 
2a50c 30 20 69 66 20 6e 6f 20 66 6c 61 74 74 65 6e 69  0 if no flatteni
2a50d 6e 67 0a 2a 2a 20 6f 63 63 75 72 73 2e 0a 2a 2a  ng.** occurs..**
2a50e 0a 2a 2a 20 54 6f 20 75 6e 64 65 72 73 74 61 6e  .** To understan
2a50f 64 20 74 68 65 20 63 6f 6e 63 65 70 74 20 6f 66  d the concept of
2a510 20 66 6c 61 74 74 65 6e 69 6e 67 2c 20 63 6f 6e   flattening, con
2a511 73 69 64 65 72 20 74 68 65 20 66 6f 6c 6c 6f 77  sider the follow
2a512 69 6e 67 0a 2a 2a 20 71 75 65 72 79 3a 0a 2a 2a  ing.** query:.**
2a513 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 61  .**     SELECT a
2a514 20 46 52 4f 4d 20 28 53 45 4c 45 43 54 20 78 2b   FROM (SELECT x+
2a515 79 20 41 53 20 61 20 46 52 4f 4d 20 74 31 20 57  y AS a FROM t1 W
2a516 48 45 52 45 20 7a 3c 31 30 30 29 20 57 48 45 52  HERE z<100) WHER
2a517 45 20 61 3e 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  E a>5.**.** The 
2a518 64 65 66 61 75 6c 74 20 77 61 79 20 6f 66 20 69  default way of i
2a519 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 74 68 69 73  mplementing this
2a51a 20 71 75 65 72 79 20 69 73 20 74 6f 20 65 78 65   query is to exe
2a51b 63 75 74 65 20 74 68 65 0a 2a 2a 20 73 75 62 71  cute the.** subq
2a51c 75 65 72 79 20 66 69 72 73 74 20 61 6e 64 20 73  uery first and s
2a51d 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 73  tore the results
2a51e 20 69 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20   in a temporary 
2a51f 74 61 62 6c 65 2c 20 74 68 65 6e 0a 2a 2a 20 72  table, then.** r
2a520 75 6e 20 74 68 65 20 6f 75 74 65 72 20 71 75 65  un the outer que
2a521 72 79 20 6f 6e 20 74 68 61 74 20 74 65 6d 70 6f  ry on that tempo
2a522 72 61 72 79 20 74 61 62 6c 65 2e 20 20 54 68 69  rary table.  Thi
2a523 73 20 72 65 71 75 69 72 65 73 20 74 77 6f 0a 2a  s requires two.*
2a524 2a 20 70 61 73 73 65 73 20 6f 76 65 72 20 74 68  * passes over th
2a525 65 20 64 61 74 61 2e 20 20 46 75 72 74 68 65 72  e data.  Further
2a526 6d 6f 72 65 2c 20 62 65 63 61 75 73 65 20 74 68  more, because th
2a527 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  e temporary tabl
2a528 65 0a 2a 2a 20 68 61 73 20 6e 6f 20 69 6e 64 69  e.** has no indi
2a529 63 65 73 2c 20 74 68 65 20 57 48 45 52 45 20 63  ces, the WHERE c
2a52a 6c 61 75 73 65 20 6f 6e 20 74 68 65 20 6f 75 74  lause on the out
2a52b 65 72 20 71 75 65 72 79 20 63 61 6e 6e 6f 74 20  er query cannot 
2a52c 62 65 0a 2a 2a 20 6f 70 74 69 6d 69 7a 65 64 2e  be.** optimized.
2a52d 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
2a52e 69 6e 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20  ine attempts to 
2a52f 72 65 77 72 69 74 65 20 71 75 65 72 69 65 73 20  rewrite queries 
2a530 73 75 63 68 20 61 73 20 74 68 65 20 61 62 6f 76  such as the abov
2a531 65 20 69 6e 74 6f 0a 2a 2a 20 61 20 73 69 6e 67  e into.** a sing
2a532 6c 65 20 66 6c 61 74 20 73 65 6c 65 63 74 2c 20  le flat select, 
2a533 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a  like this:.**.**
2a534 20 20 20 20 20 53 45 4c 45 43 54 20 78 2b 79 20       SELECT x+y 
2a535 41 53 20 61 20 46 52 4f 4d 20 74 31 20 57 48 45  AS a FROM t1 WHE
2a536 52 45 20 7a 3c 31 30 30 20 41 4e 44 20 61 3e 35  RE z<100 AND a>5
2a537 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20  .**.** The code 
2a538 67 65 6e 65 72 61 74 65 64 20 66 6f 72 20 74 68  generated for th
2a539 69 73 20 73 69 6d 70 69 66 69 63 61 74 69 6f 6e  is simpification
2a53a 20 67 69 76 65 73 20 74 68 65 20 73 61 6d 65 20   gives the same 
2a53b 72 65 73 75 6c 74 0a 2a 2a 20 62 75 74 20 6f 6e  result.** but on
2a53c 6c 79 20 68 61 73 20 74 6f 20 73 63 61 6e 20 74  ly has to scan t
2a53d 68 65 20 64 61 74 61 20 6f 6e 63 65 2e 20 20 41  he data once.  A
2a53e 6e 64 20 62 65 63 61 75 73 65 20 69 6e 64 69 63  nd because indic
2a53f 65 73 20 6d 69 67 68 74 20 0a 2a 2a 20 65 78 69  es might .** exi
2a540 73 74 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20  st on the table 
2a541 74 31 2c 20 61 20 63 6f 6d 70 6c 65 74 65 20 73  t1, a complete s
2a542 63 61 6e 20 6f 66 20 74 68 65 20 64 61 74 61 20  can of the data 
2a543 6d 69 67 68 74 20 62 65 0a 2a 2a 20 61 76 6f 69  might be.** avoi
2a544 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 46 6c 61 74 74  ded..**.** Flatt
2a545 65 6e 69 6e 67 20 69 73 20 6f 6e 6c 79 20 61 74  ening is only at
2a546 74 65 6d 70 74 65 64 20 69 66 20 61 6c 6c 20 6f  tempted if all o
2a547 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
2a548 61 72 65 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20  are true:.**.** 
2a549 20 20 28 31 29 20 20 54 68 65 20 73 75 62 71 75    (1)  The subqu
2a54a 65 72 79 20 61 6e 64 20 74 68 65 20 6f 75 74 65  ery and the oute
2a54b 72 20 71 75 65 72 79 20 64 6f 20 6e 6f 74 20 62  r query do not b
2a54c 6f 74 68 20 75 73 65 20 61 67 67 72 65 67 61 74  oth use aggregat
2a54d 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 32 29 20  es..**.**   (2) 
2a54e 20 54 68 65 20 73 75 62 71 75 65 72 79 20 69 73   The subquery is
2a54f 20 6e 6f 74 20 61 6e 20 61 67 67 72 65 67 61 74   not an aggregat
2a550 65 20 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71  e or the outer q
2a551 75 65 72 79 20 69 73 20 6e 6f 74 20 61 20 6a 6f  uery is not a jo
2a552 69 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 33 29 20  in..**.**   (3) 
2a553 20 54 68 65 20 73 75 62 71 75 65 72 79 20 69 73   The subquery is
2a554 20 6e 6f 74 20 74 68 65 20 72 69 67 68 74 20 6f   not the right o
2a555 70 65 72 61 6e 64 20 6f 66 20 61 20 6c 65 66 74  perand of a left
2a556 20 6f 75 74 65 72 20 6a 6f 69 6e 0a 2a 2a 20 20   outer join.**  
2a557 20 20 20 20 20 20 28 4f 72 69 67 69 6e 61 6c 6c        (Originall
2a558 79 20 74 69 63 6b 65 74 20 23 33 30 36 2e 20 20  y ticket #306.  
2a559 53 74 72 65 6e 67 68 74 65 6e 65 64 20 62 79 20  Strenghtened by 
2a55a 74 69 63 6b 65 74 20 23 33 33 30 30 29 0a 2a 2a  ticket #3300).**
2a55b 0a 2a 2a 20 20 20 28 34 29 20 20 54 68 65 20 73  .**   (4)  The s
2a55c 75 62 71 75 65 72 79 20 69 73 20 6e 6f 74 20 44  ubquery is not D
2a55d 49 53 54 49 4e 43 54 20 6f 72 20 74 68 65 20 6f  ISTINCT or the o
2a55e 75 74 65 72 20 71 75 65 72 79 20 69 73 20 6e 6f  uter query is no
2a55f 74 20 61 20 6a 6f 69 6e 2e 0a 2a 2a 0a 2a 2a 20  t a join..**.** 
2a560 20 20 28 35 29 20 20 54 68 65 20 73 75 62 71 75    (5)  The subqu
2a561 65 72 79 20 69 73 20 6e 6f 74 20 44 49 53 54 49  ery is not DISTI
2a562 4e 43 54 20 6f 72 20 74 68 65 20 6f 75 74 65 72  NCT or the outer
2a563 20 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20   query does not 
2a564 75 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 61 67  use.**        ag
2a565 67 72 65 67 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20  gregates..**.** 
2a566 20 20 28 36 29 20 20 54 68 65 20 73 75 62 71 75    (6)  The subqu
2a567 65 72 79 20 64 6f 65 73 20 6e 6f 74 20 75 73 65  ery does not use
2a568 20 61 67 67 72 65 67 61 74 65 73 20 6f 72 20 74   aggregates or t
2a569 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 69  he outer query i
2a56a 73 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 20 20  s not.**        
2a56b 44 49 53 54 49 4e 43 54 2e 0a 2a 2a 0a 2a 2a 20  DISTINCT..**.** 
2a56c 20 20 28 37 29 20 20 54 68 65 20 73 75 62 71 75    (7)  The subqu
2a56d 65 72 79 20 68 61 73 20 61 20 46 52 4f 4d 20 63  ery has a FROM c
2a56e 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 28  lause..**.**   (
2a56f 38 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79  8)  The subquery
2a570 20 64 6f 65 73 20 6e 6f 74 20 75 73 65 20 4c 49   does not use LI
2a571 4d 49 54 20 6f 72 20 74 68 65 20 6f 75 74 65 72  MIT or the outer
2a572 20 71 75 65 72 79 20 69 73 20 6e 6f 74 20 61 20   query is not a 
2a573 6a 6f 69 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 39  join..**.**   (9
2a574 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79 20  )  The subquery 
2a575 64 6f 65 73 20 6e 6f 74 20 75 73 65 20 4c 49 4d  does not use LIM
2a576 49 54 20 6f 72 20 74 68 65 20 6f 75 74 65 72 20  IT or the outer 
2a577 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20 75  query does not u
2a578 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 61 67 67  se.**        agg
2a579 72 65 67 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20  regates..**.**  
2a57a 28 31 30 29 20 20 54 68 65 20 73 75 62 71 75 65  (10)  The subque
2a57b 72 79 20 64 6f 65 73 20 6e 6f 74 20 75 73 65 20  ry does not use 
2a57c 61 67 67 72 65 67 61 74 65 73 20 6f 72 20 74 68  aggregates or th
2a57d 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 64 6f  e outer query do
2a57e 65 73 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 20  es not.**       
2a57f 20 75 73 65 20 4c 49 4d 49 54 2e 0a 2a 2a 0a 2a   use LIMIT..**.*
2a580 2a 20 20 28 31 31 29 20 20 54 68 65 20 73 75 62  *  (11)  The sub
2a581 71 75 65 72 79 20 61 6e 64 20 74 68 65 20 6f 75  query and the ou
2a582 74 65 72 20 71 75 65 72 79 20 64 6f 20 6e 6f 74  ter query do not
2a583 20 62 6f 74 68 20 68 61 76 65 20 4f 52 44 45 52   both have ORDER
2a584 20 42 59 20 63 6c 61 75 73 65 73 2e 0a 2a 2a 0a   BY clauses..**.
2a585 2a 2a 20 20 28 31 32 29 20 20 4e 6f 74 20 69 6d  **  (12)  Not im
2a586 70 6c 65 6d 65 6e 74 65 64 2e 20 20 53 75 62 73  plemented.  Subs
2a587 75 6d 65 64 20 69 6e 74 6f 20 72 65 73 74 72 69  umed into restri
2a588 63 74 69 6f 6e 20 28 33 29 2e 20 20 57 61 73 20  ction (3).  Was 
2a589 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 20 20  previously.**   
2a58a 20 20 20 20 20 61 20 73 65 70 61 72 61 74 65 20       a separate 
2a58b 72 65 73 74 72 69 63 74 69 6f 6e 20 64 65 72 69  restriction deri
2a58c 76 69 6e 67 20 66 72 6f 6d 20 74 69 63 6b 65 74  ving from ticket
2a58d 20 23 33 35 30 2e 0a 2a 2a 0a 2a 2a 20 20 28 31   #350..**.**  (1
2a58e 33 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79  3)  The subquery
2a58f 20 61 6e 64 20 6f 75 74 65 72 20 71 75 65 72 79   and outer query
2a590 20 64 6f 20 6e 6f 74 20 62 6f 74 68 20 75 73 65   do not both use
2a591 20 4c 49 4d 49 54 0a 2a 2a 0a 2a 2a 20 20 28 31   LIMIT.**.**  (1
2a592 34 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79  4)  The subquery
2a593 20 64 6f 65 73 20 6e 6f 74 20 75 73 65 20 4f 46   does not use OF
2a594 46 53 45 54 0a 2a 2a 0a 2a 2a 20 20 28 31 35 29  FSET.**.**  (15)
2a595 20 20 54 68 65 20 6f 75 74 65 72 20 71 75 65 72    The outer quer
2a596 79 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66  y is not part of
2a597 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65   a compound sele
2a598 63 74 20 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20  ct or the.**    
2a599 20 20 20 20 73 75 62 71 75 65 72 79 20 64 6f 65      subquery doe
2a59a 73 20 6e 6f 74 20 68 61 76 65 20 62 6f 74 68 20  s not have both 
2a59b 61 6e 20 4f 52 44 45 52 20 42 59 20 61 6e 64 20  an ORDER BY and 
2a59c 61 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e 0a  a LIMIT clause..
2a59d 2a 2a 20 20 20 20 20 20 20 20 28 53 65 65 20 74  **        (See t
2a59e 69 63 6b 65 74 20 23 32 33 33 39 29 0a 2a 2a 0a  icket #2339).**.
2a59f 2a 2a 20 20 28 31 36 29 20 20 54 68 65 20 6f 75  **  (16)  The ou
2a5a0 74 65 72 20 71 75 65 72 79 20 69 73 20 6e 6f 74  ter query is not
2a5a1 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 6f 72   an aggregate or
2a5a2 20 74 68 65 20 73 75 62 71 75 65 72 79 20 64 6f   the subquery do
2a5a3 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 6e 6f 74  es.**        not
2a5a4 20 63 6f 6e 74 61 69 6e 20 4f 52 44 45 52 20 42   contain ORDER B
2a5a5 59 2e 20 20 28 54 69 63 6b 65 74 20 23 32 39 34  Y.  (Ticket #294
2a5a6 32 29 20 20 54 68 69 73 20 75 73 65 64 20 74 6f  2)  This used to
2a5a7 20 6e 6f 74 20 6d 61 74 74 65 72 0a 2a 2a 20 20   not matter.**  
2a5a8 20 20 20 20 20 20 75 6e 74 69 6c 20 77 65 20 69        until we i
2a5a9 6e 74 72 6f 64 75 63 65 64 20 74 68 65 20 67 72  ntroduced the gr
2a5aa 6f 75 70 5f 63 6f 6e 63 61 74 28 29 20 66 75 6e  oup_concat() fun
2a5ab 63 74 69 6f 6e 2e 20 20 0a 2a 2a 0a 2a 2a 20 20  ction.  .**.**  
2a5ac 28 31 37 29 20 20 54 68 65 20 73 75 62 2d 71 75  (17)  The sub-qu
2a5ad 65 72 79 20 69 73 20 6e 6f 74 20 61 20 63 6f 6d  ery is not a com
2a5ae 70 6f 75 6e 64 20 73 65 6c 65 63 74 2c 20 6f 72  pound select, or
2a5af 20 69 74 20 69 73 20 61 20 55 4e 49 4f 4e 20 41   it is a UNION A
2a5b0 4c 4c 20 0a 2a 2a 20 20 20 20 20 20 20 20 63 6f  LL .**        co
2a5b1 6d 70 6f 75 6e 64 20 63 6c 61 75 73 65 20 6d 61  mpound clause ma
2a5b2 64 65 20 75 70 20 65 6e 74 69 72 65 6c 79 20 6f  de up entirely o
2a5b3 66 20 6e 6f 6e 2d 61 67 67 72 65 67 61 74 65 20  f non-aggregate 
2a5b4 71 75 65 72 69 65 73 2c 20 61 6e 64 20 0a 2a 2a  queries, and .**
2a5b5 20 20 20 20 20 20 20 20 74 68 65 20 70 61 72 65          the pare
2a5b6 6e 74 20 71 75 65 72 79 3a 0a 2a 2a 0a 2a 2a 20  nt query:.**.** 
2a5b7 20 20 20 20 20 20 20 20 20 2a 20 69 73 20 6e 6f           * is no
2a5b8 74 20 69 74 73 65 6c 66 20 70 61 72 74 20 6f 66  t itself part of
2a5b9 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65   a compound sele
2a5ba 63 74 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ct,.**          
2a5bb 2a 20 69 73 20 6e 6f 74 20 61 6e 20 61 67 67 72  * is not an aggr
2a5bc 65 67 61 74 65 20 6f 72 20 44 49 53 54 49 4e 43  egate or DISTINC
2a5bd 54 20 71 75 65 72 79 2c 20 61 6e 64 0a 2a 2a 20  T query, and.** 
2a5be 20 20 20 20 20 20 20 20 20 2a 20 68 61 73 20 6e           * has n
2a5bf 6f 20 6f 74 68 65 72 20 74 61 62 6c 65 73 20 6f  o other tables o
2a5c0 72 20 73 75 62 2d 73 65 6c 65 63 74 73 20 69 6e  r sub-selects in
2a5c1 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
2a5c2 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 54  ..**.**        T
2a5c3 68 65 20 70 61 72 65 6e 74 20 61 6e 64 20 73 75  he parent and su
2a5c4 62 2d 71 75 65 72 79 20 6d 61 79 20 63 6f 6e 74  b-query may cont
2a5c5 61 69 6e 20 57 48 45 52 45 20 63 6c 61 75 73 65  ain WHERE clause
2a5c6 73 2e 20 53 75 62 6a 65 63 74 20 74 6f 0a 2a 2a  s. Subject to.**
2a5c7 20 20 20 20 20 20 20 20 72 75 6c 65 73 20 28 31          rules (1
2a5c8 31 29 2c 20 28 31 33 29 20 61 6e 64 20 28 31 34  1), (13) and (14
2a5c9 29 2c 20 74 68 65 79 20 6d 61 79 20 61 6c 73 6f  ), they may also
2a5ca 20 63 6f 6e 74 61 69 6e 20 4f 52 44 45 52 20 42   contain ORDER B
2a5cb 59 2c 0a 2a 2a 20 20 20 20 20 20 20 20 4c 49 4d  Y,.**        LIM
2a5cc 49 54 20 61 6e 64 20 4f 46 46 53 45 54 20 63 6c  IT and OFFSET cl
2a5cd 61 75 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 28 31  auses..**.**  (1
2a5ce 38 29 20 20 49 66 20 74 68 65 20 73 75 62 2d 71  8)  If the sub-q
2a5cf 75 65 72 79 20 69 73 20 61 20 63 6f 6d 70 6f 75  uery is a compou
2a5d0 6e 64 20 73 65 6c 65 63 74 2c 20 74 68 65 6e 20  nd select, then 
2a5d1 61 6c 6c 20 74 65 72 6d 73 20 6f 66 20 74 68 65  all terms of the
2a5d2 0a 2a 2a 20 20 20 20 20 20 20 20 4f 52 44 45 52  .**        ORDER
2a5d3 20 62 79 20 63 6c 61 75 73 65 20 6f 66 20 74 68   by clause of th
2a5d4 65 20 70 61 72 65 6e 74 20 6d 75 73 74 20 62 65  e parent must be
2a5d5 20 73 69 6d 70 6c 65 20 72 65 66 65 72 65 6e 63   simple referenc
2a5d6 65 73 20 74 6f 20 0a 2a 2a 20 20 20 20 20 20 20  es to .**       
2a5d7 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20   columns of the 
2a5d8 73 75 62 2d 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a  sub-query..**.**
2a5d9 20 20 28 31 39 29 20 20 54 68 65 20 73 75 62 71    (19)  The subq
2a5da 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20 75 73  uery does not us
2a5db 65 20 4c 49 4d 49 54 20 6f 72 20 74 68 65 20 6f  e LIMIT or the o
2a5dc 75 74 65 72 20 71 75 65 72 79 20 64 6f 65 73 20  uter query does 
2a5dd 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 20 20 68 61  not.**        ha
2a5de 76 65 20 61 20 57 48 45 52 45 20 63 6c 61 75 73  ve a WHERE claus
2a5df 65 2e 0a 2a 2a 0a 2a 2a 20 20 28 32 30 29 20 20  e..**.**  (20)  
2a5e0 49 66 20 74 68 65 20 73 75 62 2d 71 75 65 72 79  If the sub-query
2a5e1 20 69 73 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73   is a compound s
2a5e2 65 6c 65 63 74 2c 20 74 68 65 6e 20 69 74 20 6d  elect, then it m
2a5e3 75 73 74 20 6e 6f 74 20 75 73 65 0a 2a 2a 20 20  ust not use.**  
2a5e4 20 20 20 20 20 20 61 6e 20 4f 52 44 45 52 20 42        an ORDER B
2a5e5 59 20 63 6c 61 75 73 65 2e 20 20 54 69 63 6b 65  Y clause.  Ticke
2a5e6 74 20 23 33 37 37 33 2e 20 20 57 65 20 63 6f 75  t #3773.  We cou
2a5e7 6c 64 20 72 65 6c 61 78 20 74 68 69 73 20 63 6f  ld relax this co
2a5e8 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 20 20 20 20  nstraint.**     
2a5e9 20 20 20 73 6f 6d 65 77 68 61 74 20 62 79 20 73     somewhat by s
2a5ea 61 79 69 6e 67 20 74 68 61 74 20 74 68 65 20 74  aying that the t
2a5eb 65 72 6d 73 20 6f 66 20 74 68 65 20 4f 52 44 45  erms of the ORDE
2a5ec 52 20 42 59 20 63 6c 61 75 73 65 20 6d 75 73 74  R BY clause must
2a5ed 0a 2a 2a 20 20 20 20 20 20 20 20 61 70 70 65 61  .**        appea
2a5ee 72 20 61 73 20 75 6e 6d 6f 64 69 66 69 65 64 20  r as unmodified 
2a5ef 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 69  result columns i
2a5f0 6e 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72  n the outer quer
2a5f1 79 2e 20 20 42 75 74 0a 2a 2a 20 20 20 20 20 20  y.  But.**      
2a5f2 20 20 68 61 76 65 20 6f 74 68 65 72 20 6f 70 74    have other opt
2a5f3 69 6d 69 7a 61 74 69 6f 6e 73 20 69 6e 20 6d 69  imizations in mi
2a5f4 6e 64 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20  nd to deal with 
2a5f5 74 68 61 74 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a  that case..**.**
2a5f6 20 49 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65   In this routine
2a5f7 2c 20 74 68 65 20 22 70 22 20 70 61 72 61 6d 65  , the "p" parame
2a5f8 74 65 72 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ter is a pointer
2a5f9 20 74 6f 20 74 68 65 20 6f 75 74 65 72 20 71 75   to the outer qu
2a5fa 65 72 79 2e 0a 2a 2a 20 54 68 65 20 73 75 62 71  ery..** The subq
2a5fb 75 65 72 79 20 69 73 20 70 2d 3e 70 53 72 63 2d  uery is p->pSrc-
2a5fc 3e 61 5b 69 46 72 6f 6d 5d 2e 20 20 69 73 41 67  >a[iFrom].  isAg
2a5fd 67 20 69 73 20 74 72 75 65 20 69 66 20 74 68 65  g is true if the
2a5fe 20 6f 75 74 65 72 20 71 75 65 72 79 0a 2a 2a 20   outer query.** 
2a5ff 75 73 65 73 20 61 67 67 72 65 67 61 74 65 73 20  uses aggregates 
2a600 61 6e 64 20 73 75 62 71 75 65 72 79 49 73 41 67  and subqueryIsAg
2a601 67 20 69 73 20 74 72 75 65 20 69 66 20 74 68 65  g is true if the
2a602 20 73 75 62 71 75 65 72 79 20 75 73 65 73 20 61   subquery uses a
2a603 67 67 72 65 67 61 74 65 73 2e 0a 2a 2a 0a 2a 2a  ggregates..**.**
2a604 20 49 66 20 66 6c 61 74 74 65 6e 69 6e 67 20 69   If flattening i
2a605 73 20 6e 6f 74 20 61 74 74 65 6d 70 74 65 64 2c  s not attempted,
2a606 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
2a607 20 61 20 6e 6f 2d 6f 70 20 61 6e 64 20 72 65 74   a no-op and ret
2a608 75 72 6e 73 20 30 2e 0a 2a 2a 20 49 66 20 66 6c  urns 0..** If fl
2a609 61 74 74 65 6e 69 6e 67 20 69 73 20 61 74 74 65  attening is atte
2a60a 6d 70 74 65 64 20 74 68 69 73 20 72 6f 75 74 69  mpted this routi
2a60b 6e 65 20 72 65 74 75 72 6e 73 20 31 2e 0a 2a 2a  ne returns 1..**
2a60c 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 65  .** All of the e
2a60d 78 70 72 65 73 73 69 6f 6e 20 61 6e 61 6c 79 73  xpression analys
2a60e 69 73 20 6d 75 73 74 20 6f 63 63 75 72 20 6f 6e  is must occur on
2a60f 20 62 6f 74 68 20 74 68 65 20 6f 75 74 65 72 20   both the outer 
2a610 71 75 65 72 79 20 61 6e 64 0a 2a 2a 20 74 68 65  query and.** the
2a611 20 73 75 62 71 75 65 72 79 20 62 65 66 6f 72 65   subquery before
2a612 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 75   this routine ru
2a613 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ns..*/.static in
2a614 74 20 66 6c 61 74 74 65 6e 53 75 62 71 75 65 72  t flattenSubquer
2a615 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  y(.  Parse *pPar
2a616 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 50 61 72  se,       /* Par
2a617 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
2a618 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20    Select *p,    
2a619 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
2a61a 72 65 6e 74 20 6f 72 20 6f 75 74 65 72 20 53 45  rent or outer SE
2a61b 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a  LECT statement *
2a61c 2f 0a 20 20 69 6e 74 20 69 46 72 6f 6d 2c 20 20  /.  int iFrom,  
2a61d 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
2a61e 78 20 69 6e 20 70 2d 3e 70 53 72 63 2d 3e 61 5b  x in p->pSrc->a[
2a61f 5d 20 6f 66 20 74 68 65 20 69 6e 6e 65 72 20 73  ] of the inner s
2a620 75 62 71 75 65 72 79 20 2a 2f 0a 20 20 69 6e 74  ubquery */.  int
2a621 20 69 73 41 67 67 2c 20 20 20 20 20 20 20 20 20   isAgg,         
2a622 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6f 75 74    /* True if out
2a623 65 72 20 53 45 4c 45 43 54 20 75 73 65 73 20 61  er SELECT uses a
2a624 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
2a625 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 73 75 62 71  ns */.  int subq
2a626 75 65 72 79 49 73 41 67 67 20 20 20 20 2f 2a 20  ueryIsAgg    /* 
2a627 54 72 75 65 20 69 66 20 74 68 65 20 73 75 62 71  True if the subq
2a628 75 65 72 79 20 75 73 65 73 20 61 67 67 72 65 67  uery uses aggreg
2a629 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f  ate functions */
2a62a 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  .){.  const char
2a62b 20 2a 7a 53 61 76 65 64 41 75 74 68 43 6f 6e 74   *zSavedAuthCont
2a62c 65 78 74 20 3d 20 70 50 61 72 73 65 2d 3e 7a 41  ext = pParse->zA
2a62d 75 74 68 43 6f 6e 74 65 78 74 3b 0a 20 20 53 65  uthContext;.  Se
2a62e 6c 65 63 74 20 2a 70 50 61 72 65 6e 74 3b 0a 20  lect *pParent;. 
2a62f 20 53 65 6c 65 63 74 20 2a 70 53 75 62 3b 20 20   Select *pSub;  
2a630 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 6e 65       /* The inne
2a631 72 20 71 75 65 72 79 20 6f 72 20 22 73 75 62 71  r query or "subq
2a632 75 65 72 79 22 20 2a 2f 0a 20 20 53 65 6c 65 63  uery" */.  Selec
2a633 74 20 2a 70 53 75 62 31 3b 20 20 20 20 20 20 2f  t *pSub1;      /
2a634 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  * Pointer to the
2a635 20 72 69 67 68 74 6d 6f 73 74 20 73 65 6c 65 63   rightmost selec
2a636 74 20 69 6e 20 73 75 62 2d 71 75 65 72 79 20 2a  t in sub-query *
2a637 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  /.  SrcList *pSr
2a638 63 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 46  c;      /* The F
2a639 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 68  ROM clause of th
2a63a 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 2a 2f  e outer query */
2a63b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 75 62  .  SrcList *pSub
2a63c 53 72 63 3b 20 20 20 2f 2a 20 54 68 65 20 46 52  Src;   /* The FR
2a63d 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65  OM clause of the
2a63e 20 73 75 62 71 75 65 72 79 20 2a 2f 0a 20 20 45   subquery */.  E
2a63f 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 3b 20  xprList *pList; 
2a640 20 20 20 2f 2a 20 54 68 65 20 72 65 73 75 6c 74     /* The result
2a641 20 73 65 74 20 6f 66 20 74 68 65 20 6f 75 74 65   set of the oute
2a642 72 20 71 75 65 72 79 20 2a 2f 0a 20 20 69 6e 74  r query */.  int
2a643 20 69 50 61 72 65 6e 74 3b 20 20 20 20 20 20 20   iParent;       
2a644 20 2f 2a 20 56 44 42 45 20 63 75 72 73 6f 72 20   /* VDBE cursor 
2a645 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 53  number of the pS
2a646 75 62 20 72 65 73 75 6c 74 20 73 65 74 20 74 65  ub result set te
2a647 6d 70 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  mp table */.  in
2a648 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
2a649 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
2a64a 72 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68  r */.  Expr *pWh
2a64b 65 72 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ere;            
2a64c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57          /* The W
2a64d 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20  HERE clause */. 
2a64e 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
2a64f 69 74 65 6d 20 2a 70 53 75 62 69 74 65 6d 3b 20  item *pSubitem; 
2a650 20 20 2f 2a 20 54 68 65 20 73 75 62 71 75 65 72    /* The subquer
2a651 79 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  y */.  sqlite3 *
2a652 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2a653 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20  ..  /* Check to 
2a654 73 65 65 20 69 66 20 66 6c 61 74 74 65 6e 69 6e  see if flattenin
2a655 67 20 69 73 20 70 65 72 6d 69 74 74 65 64 2e 20  g is permitted. 
2a656 20 52 65 74 75 72 6e 20 30 20 69 66 20 6e 6f 74   Return 0 if not
2a657 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
2a658 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72   p!=0 );.  asser
2a659 74 28 20 70 2d 3e 70 50 72 69 6f 72 3d 3d 30 20  t( p->pPrior==0 
2a65a 29 3b 20 20 2f 2a 20 55 6e 61 62 6c 65 20 74 6f  );  /* Unable to
2a65b 20 66 6c 61 74 74 65 6e 20 63 6f 6d 70 6f 75 6e   flatten compoun
2a65c 64 20 71 75 65 72 69 65 73 20 2a 2f 0a 20 20 70  d queries */.  p
2a65d 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20  Src = p->pSrc;. 
2a65e 20 61 73 73 65 72 74 28 20 70 53 72 63 20 26 26   assert( pSrc &&
2a65f 20 69 46 72 6f 6d 3e 3d 30 20 26 26 20 69 46 72   iFrom>=0 && iFr
2a660 6f 6d 3c 70 53 72 63 2d 3e 6e 53 72 63 20 29 3b  om<pSrc->nSrc );
2a661 0a 20 20 70 53 75 62 69 74 65 6d 20 3d 20 26 70  .  pSubitem = &p
2a662 53 72 63 2d 3e 61 5b 69 46 72 6f 6d 5d 3b 0a 20  Src->a[iFrom];. 
2a663 20 69 50 61 72 65 6e 74 20 3d 20 70 53 75 62 69   iParent = pSubi
2a664 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20  tem->iCursor;.  
2a665 70 53 75 62 20 3d 20 70 53 75 62 69 74 65 6d 2d  pSub = pSubitem-
2a666 3e 70 53 65 6c 65 63 74 3b 0a 20 20 61 73 73 65  >pSelect;.  asse
2a667 72 74 28 20 70 53 75 62 21 3d 30 20 29 3b 0a 20  rt( pSub!=0 );. 
2a668 20 69 66 28 20 69 73 41 67 67 20 26 26 20 73 75   if( isAgg && su
2a669 62 71 75 65 72 79 49 73 41 67 67 20 29 20 72 65  bqueryIsAgg ) re
2a66a 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20  turn 0;         
2a66b 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72          /* Restr
2a66c 69 63 74 69 6f 6e 20 28 31 29 20 20 2a 2f 0a 20  iction (1)  */. 
2a66d 20 69 66 28 20 73 75 62 71 75 65 72 79 49 73 41   if( subqueryIsA
2a66e 67 67 20 26 26 20 70 53 72 63 2d 3e 6e 53 72 63  gg && pSrc->nSrc
2a66f 3e 31 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20  >1 ) return 0;  
2a670 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72          /* Restr
2a671 69 63 74 69 6f 6e 20 28 32 29 20 20 2a 2f 0a 20  iction (2)  */. 
2a672 20 70 53 75 62 53 72 63 20 3d 20 70 53 75 62 2d   pSubSrc = pSub-
2a673 3e 70 53 72 63 3b 0a 20 20 61 73 73 65 72 74 28  >pSrc;.  assert(
2a674 20 70 53 75 62 53 72 63 20 29 3b 0a 20 20 2f 2a   pSubSrc );.  /*
2a675 20 50 72 69 6f 72 20 74 6f 20 76 65 72 73 69 6f   Prior to versio
2a676 6e 20 33 2e 31 2e 32 2c 20 77 68 65 6e 20 4c 49  n 3.1.2, when LI
2a677 4d 49 54 20 61 6e 64 20 4f 46 46 53 45 54 20 68  MIT and OFFSET h
2a678 61 64 20 74 6f 20 62 65 20 73 69 6d 70 6c 65 20  ad to be simple 
2a679 63 6f 6e 73 74 61 6e 74 73 2c 0a 20 20 2a 2a 20  constants,.  ** 
2a67a 6e 6f 74 20 61 72 62 69 74 72 61 72 79 20 65 78  not arbitrary ex
2a67b 70 72 65 73 73 73 69 6f 6e 73 2c 20 77 65 20 61  presssions, we a
2a67c 6c 6c 6f 77 65 64 20 73 6f 6d 65 20 63 6f 6d 62  llowed some comb
2a67d 69 6e 69 6e 67 20 6f 66 20 4c 49 4d 49 54 20 61  ining of LIMIT a
2a67e 6e 64 20 4f 46 46 53 45 54 0a 20 20 2a 2a 20 62  nd OFFSET.  ** b
2a67f 65 63 61 75 73 65 20 74 68 65 79 20 63 6f 75 6c  ecause they coul
2a680 64 20 62 65 20 63 6f 6d 70 75 74 65 64 20 61 74  d be computed at
2a681 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 20 20   compile-time.  
2a682 42 75 74 20 77 68 65 6e 20 4c 49 4d 49 54 20 61  But when LIMIT a
2a683 6e 64 20 4f 46 46 53 45 54 0a 20 20 2a 2a 20 62  nd OFFSET.  ** b
2a684 65 63 61 6d 65 20 61 72 62 69 74 72 61 72 79 20  ecame arbitrary 
2a685 65 78 70 72 65 73 73 69 6f 6e 73 2c 20 77 65 20  expressions, we 
2a686 77 65 72 65 20 66 6f 72 63 65 64 20 74 6f 20 61  were forced to a
2a687 64 64 20 72 65 73 74 72 69 63 74 69 6f 6e 73 20  dd restrictions 
2a688 28 31 33 29 0a 20 20 2a 2a 20 61 6e 64 20 28 31  (13).  ** and (1
2a689 34 29 2e 20 2a 2f 0a 20 20 69 66 28 20 70 53 75  4). */.  if( pSu
2a68a 62 2d 3e 70 4c 69 6d 69 74 20 26 26 20 70 2d 3e  b->pLimit && p->
2a68b 70 4c 69 6d 69 74 20 29 20 72 65 74 75 72 6e 20  pLimit ) return 
2a68c 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
2a68d 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28  /* Restriction (
2a68e 31 33 29 20 2a 2f 0a 20 20 69 66 28 20 70 53 75  13) */.  if( pSu
2a68f 62 2d 3e 70 4f 66 66 73 65 74 20 29 20 72 65 74  b->pOffset ) ret
2a690 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20  urn 0;          
2a691 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a692 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28  /* Restriction (
2a693 31 34 29 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e  14) */.  if( p->
2a694 70 52 69 67 68 74 6d 6f 73 74 20 26 26 20 70 53  pRightmost && pS
2a695 75 62 2d 3e 70 4c 69 6d 69 74 20 26 26 20 70 53  ub->pLimit && pS
2a696 75 62 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a  ub->pOrderBy ){.
2a697 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
2a698 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a699 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a69a 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74           /* Rest
2a69b 72 69 63 74 69 6f 6e 20 28 31 35 29 20 2a 2f 0a  riction (15) */.
2a69c 20 20 7d 0a 20 20 69 66 28 20 70 53 75 62 53 72    }.  if( pSubSr
2a69d 63 2d 3e 6e 53 72 63 3d 3d 30 20 29 20 72 65 74  c->nSrc==0 ) ret
2a69e 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20  urn 0;          
2a69f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2a6a0 52 65 73 74 72 69 63 74 69 6f 6e 20 28 37 29 20  Restriction (7) 
2a6a1 20 2a 2f 0a 20 20 69 66 28 20 28 28 70 53 75 62   */.  if( ((pSub
2a6a2 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f  ->selFlags & SF_
2a6a3 44 69 73 74 69 6e 63 74 29 21 3d 30 20 7c 7c 20  Distinct)!=0 || 
2a6a4 70 53 75 62 2d 3e 70 4c 69 6d 69 74 29 20 0a 20  pSub->pLimit) . 
2a6a5 20 20 20 20 20 20 20 20 26 26 20 28 70 53 72 63          && (pSrc
2a6a6 2d 3e 6e 53 72 63 3e 31 20 7c 7c 20 69 73 41 67  ->nSrc>1 || isAg
2a6a7 67 29 20 29 7b 20 20 20 20 20 20 20 20 20 20 2f  g) ){          /
2a6a8 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 73 20 28  * Restrictions (
2a6a9 34 29 28 35 29 28 38 29 28 39 29 20 2a 2f 0a 20  4)(5)(8)(9) */. 
2a6aa 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
2a6ab 20 20 20 20 0a 20 20 7d 0a 20 20 69 66 28 20 28      .  }.  if( (
2a6ac 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46  p->selFlags & SF
2a6ad 5f 44 69 73 74 69 6e 63 74 29 21 3d 30 20 26 26  _Distinct)!=0 &&
2a6ae 20 73 75 62 71 75 65 72 79 49 73 41 67 67 20 29   subqueryIsAgg )
2a6af 7b 0a 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  {.     return 0;
2a6b0 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74           /* Rest
2a6b1 72 69 63 74 69 6f 6e 20 28 36 29 20 20 2a 2f 0a  riction (6)  */.
2a6b2 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 4f 72    }.  if( p->pOr
2a6b3 64 65 72 42 79 20 26 26 20 70 53 75 62 2d 3e 70  derBy && pSub->p
2a6b4 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 20  OrderBy ){.     
2a6b5 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20  return 0;       
2a6b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a6b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a6b8 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69      /* Restricti
2a6b9 6f 6e 20 28 31 31 29 20 2a 2f 0a 20 20 7d 0a 20  on (11) */.  }. 
2a6ba 20 69 66 28 20 69 73 41 67 67 20 26 26 20 70 53   if( isAgg && pS
2a6bb 75 62 2d 3e 70 4f 72 64 65 72 42 79 20 29 20 72  ub->pOrderBy ) r
2a6bc 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20  eturn 0;        
2a6bd 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72          /* Restr
2a6be 69 63 74 69 6f 6e 20 28 31 36 29 20 2a 2f 0a 20  iction (16) */. 
2a6bf 20 69 66 28 20 70 53 75 62 2d 3e 70 4c 69 6d 69   if( pSub->pLimi
2a6c0 74 20 26 26 20 70 2d 3e 70 57 68 65 72 65 20 29  t && p->pWhere )
2a6c1 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20   return 0;      
2a6c2 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72          /* Restr
2a6c3 69 63 74 69 6f 6e 20 28 31 39 29 20 2a 2f 0a 0a  iction (19) */..
2a6c4 20 20 2f 2a 20 4f 42 53 4f 4c 45 54 45 20 43 4f    /* OBSOLETE CO
2a6c5 4d 4d 45 4e 54 20 31 3a 0a 20 20 2a 2a 20 52 65  MMENT 1:.  ** Re
2a6c6 73 74 72 69 63 74 69 6f 6e 20 33 3a 20 20 49 66  striction 3:  If
2a6c7 20 74 68 65 20 73 75 62 71 75 65 72 79 20 69 73   the subquery is
2a6c8 20 61 20 6a 6f 69 6e 2c 20 6d 61 6b 65 20 73 75   a join, make su
2a6c9 72 65 20 74 68 65 20 73 75 62 71 75 65 72 79 20  re the subquery 
2a6ca 69 73 20 0a 20 20 2a 2a 20 6e 6f 74 20 75 73 65  is .  ** not use
2a6cb 64 20 61 73 20 74 68 65 20 72 69 67 68 74 20 6f  d as the right o
2a6cc 70 65 72 61 6e 64 20 6f 66 20 61 6e 20 6f 75 74  perand of an out
2a6cd 65 72 20 6a 6f 69 6e 2e 20 20 45 78 61 6d 70 6c  er join.  Exampl
2a6ce 65 73 20 6f 66 20 77 68 79 20 74 68 69 73 0a 20  es of why this. 
2a6cf 20 2a 2a 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 77   ** is not allow
2a6d0 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  ed:.  **.  **   
2a6d1 20 20 20 20 20 20 74 31 20 4c 45 46 54 20 4f 55        t1 LEFT OU
2a6d2 54 45 52 20 4a 4f 49 4e 20 28 74 32 20 4a 4f 49  TER JOIN (t2 JOI
2a6d3 4e 20 74 33 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20  N t3).  **.  ** 
2a6d4 49 66 20 77 65 20 66 6c 61 74 74 65 6e 20 74 68  If we flatten th
2a6d5 65 20 61 62 6f 76 65 2c 20 77 65 20 77 6f 75 6c  e above, we woul
2a6d6 64 20 67 65 74 0a 20 20 2a 2a 0a 20 20 2a 2a 20  d get.  **.  ** 
2a6d7 20 20 20 20 20 20 20 20 28 74 31 20 4c 45 46 54          (t1 LEFT
2a6d8 20 4f 55 54 45 52 20 4a 4f 49 4e 20 74 32 29 20   OUTER JOIN t2) 
2a6d9 4a 4f 49 4e 20 74 33 0a 20 20 2a 2a 0a 20 20 2a  JOIN t3.  **.  *
2a6da 2a 20 77 68 69 63 68 20 69 73 20 6e 6f 74 20 61  * which is not a
2a6db 74 20 61 6c 6c 20 74 68 65 20 73 61 6d 65 20 74  t all the same t
2a6dc 68 69 6e 67 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  hing..  **.  ** 
2a6dd 4f 42 53 4f 4c 45 54 45 20 43 4f 4d 4d 45 4e 54  OBSOLETE COMMENT
2a6de 20 32 3a 0a 20 20 2a 2a 20 52 65 73 74 72 69 63   2:.  ** Restric
2a6df 74 69 6f 6e 20 31 32 3a 20 20 49 66 20 74 68 65  tion 12:  If the
2a6e0 20 73 75 62 71 75 65 72 79 20 69 73 20 74 68 65   subquery is the
2a6e1 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20 6f   right operand o
2a6e2 66 20 61 20 6c 65 66 74 20 6f 75 74 65 72 0a 20  f a left outer. 
2a6e3 20 2a 2a 20 6a 6f 69 6e 2c 20 6d 61 6b 65 20 73   ** join, make s
2a6e4 75 72 65 20 74 68 65 20 73 75 62 71 75 65 72 79  ure the subquery
2a6e5 20 68 61 73 20 6e 6f 20 57 48 45 52 45 20 63 6c   has no WHERE cl
2a6e6 61 75 73 65 2e 0a 20 20 2a 2a 20 41 6e 20 65 78  ause..  ** An ex
2a6e7 61 6d 70 6c 65 73 20 6f 66 20 77 68 79 20 74 68  amples of why th
2a6e8 69 73 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 77 65  is is not allowe
2a6e9 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  d:.  **.  **    
2a6ea 20 20 20 20 20 74 31 20 4c 45 46 54 20 4f 55 54       t1 LEFT OUT
2a6eb 45 52 20 4a 4f 49 4e 20 28 53 45 4c 45 43 54 20  ER JOIN (SELECT 
2a6ec 2a 20 46 52 4f 4d 20 74 32 20 57 48 45 52 45 20  * FROM t2 WHERE 
2a6ed 74 32 2e 78 3e 30 29 0a 20 20 2a 2a 0a 20 20 2a  t2.x>0).  **.  *
2a6ee 2a 20 49 66 20 77 65 20 66 6c 61 74 74 65 6e 20  * If we flatten 
2a6ef 74 68 65 20 61 62 6f 76 65 2c 20 77 65 20 77 6f  the above, we wo
2a6f0 75 6c 64 20 67 65 74 0a 20 20 2a 2a 0a 20 20 2a  uld get.  **.  *
2a6f1 2a 20 20 20 20 20 20 20 20 20 28 74 31 20 4c 45  *         (t1 LE
2a6f2 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20 74 32  FT OUTER JOIN t2
2a6f3 29 20 57 48 45 52 45 20 74 32 2e 78 3e 30 0a 20  ) WHERE t2.x>0. 
2a6f4 20 2a 2a 0a 20 20 2a 2a 20 42 75 74 20 74 68 65   **.  ** But the
2a6f5 20 74 32 2e 78 3e 30 20 74 65 73 74 20 77 69 6c   t2.x>0 test wil
2a6f6 6c 20 61 6c 77 61 79 73 20 66 61 69 6c 20 6f 6e  l always fail on
2a6f7 20 61 20 4e 55 4c 4c 20 72 6f 77 20 6f 66 20 74   a NULL row of t
2a6f8 32 2c 20 77 68 69 63 68 0a 20 20 2a 2a 20 65 66  2, which.  ** ef
2a6f9 66 65 63 74 69 76 65 6c 79 20 63 6f 6e 76 65 72  fectively conver
2a6fa 74 73 20 74 68 65 20 4f 55 54 45 52 20 4a 4f 49  ts the OUTER JOI
2a6fb 4e 20 69 6e 74 6f 20 61 6e 20 49 4e 4e 45 52 20  N into an INNER 
2a6fc 4a 4f 49 4e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  JOIN..  **.  ** 
2a6fd 54 48 49 53 20 4f 56 45 52 52 49 44 45 53 20 4f  THIS OVERRIDES O
2a6fe 42 53 4f 4c 45 54 45 20 43 4f 4d 4d 45 4e 54 53  BSOLETE COMMENTS
2a6ff 20 31 20 41 4e 44 20 32 20 41 42 4f 56 45 3a 0a   1 AND 2 ABOVE:.
2a700 20 20 2a 2a 20 54 69 63 6b 65 74 20 23 33 33 30    ** Ticket #330
2a701 30 20 73 68 6f 77 73 20 74 68 61 74 20 66 6c 61  0 shows that fla
2a702 74 74 65 6e 69 6e 67 20 74 68 65 20 72 69 67 68  ttening the righ
2a703 74 20 74 65 72 6d 20 6f 66 20 61 20 4c 45 46 54  t term of a LEFT
2a704 20 4a 4f 49 4e 0a 20 20 2a 2a 20 69 73 20 66 72   JOIN.  ** is fr
2a705 61 75 67 68 74 20 77 69 74 68 20 64 61 6e 67 65  aught with dange
2a706 72 2e 20 20 42 65 73 74 20 74 6f 20 61 76 6f 69  r.  Best to avoi
2a707 64 20 74 68 65 20 77 68 6f 6c 65 20 74 68 69 6e  d the whole thin
2a708 67 2e 20 20 49 66 20 74 68 65 0a 20 20 2a 2a 20  g.  If the.  ** 
2a709 73 75 62 71 75 65 72 79 20 69 73 20 74 68 65 20  subquery is the 
2a70a 72 69 67 68 74 20 74 65 72 6d 20 6f 66 20 61 20  right term of a 
2a70b 4c 45 46 54 20 4a 4f 49 4e 2c 20 74 68 65 6e 20  LEFT JOIN, then 
2a70c 64 6f 20 6e 6f 74 20 66 6c 61 74 74 65 6e 2e 0a  do not flatten..
2a70d 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 53 75 62    */.  if( (pSub
2a70e 69 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65 20 26  item->jointype &
2a70f 20 4a 54 5f 4f 55 54 45 52 29 21 3d 30 20 29 7b   JT_OUTER)!=0 ){
2a710 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
2a711 20 7d 0a 0a 20 20 2f 2a 20 52 65 73 74 72 69 63   }..  /* Restric
2a712 74 69 6f 6e 20 31 37 3a 20 49 66 20 74 68 65 20  tion 17: If the 
2a713 73 75 62 2d 71 75 65 72 79 20 69 73 20 61 20 63  sub-query is a c
2a714 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 2c 20  ompound SELECT, 
2a715 74 68 65 6e 20 69 74 20 6d 75 73 74 0a 20 20 2a  then it must.  *
2a716 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20 55  * use only the U
2a717 4e 49 4f 4e 20 41 4c 4c 20 6f 70 65 72 61 74 6f  NION ALL operato
2a718 72 2e 20 41 6e 64 20 6e 6f 6e 65 20 6f 66 20 74  r. And none of t
2a719 68 65 20 73 69 6d 70 6c 65 20 73 65 6c 65 63 74  he simple select
2a71a 20 71 75 65 72 69 65 73 0a 20 20 2a 2a 20 74 68   queries.  ** th
2a71b 61 74 20 6d 61 6b 65 20 75 70 20 74 68 65 20 63  at make up the c
2a71c 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 61  ompound SELECT a
2a71d 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 65  re allowed to be
2a71e 20 61 67 67 72 65 67 61 74 65 20 6f 72 20 64 69   aggregate or di
2a71f 73 74 69 6e 63 74 0a 20 20 2a 2a 20 71 75 65 72  stinct.  ** quer
2a720 69 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ies..  */.  if( 
2a721 70 53 75 62 2d 3e 70 50 72 69 6f 72 20 29 7b 0a  pSub->pPrior ){.
2a722 20 20 20 20 69 66 28 20 70 53 75 62 2d 3e 70 4f      if( pSub->pO
2a723 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 20 20  rderBy ){.      
2a724 72 65 74 75 72 6e 20 30 3b 20 20 2f 2a 20 52 65  return 0;  /* Re
2a725 73 74 72 69 63 74 69 6f 6e 20 32 30 20 2a 2f 0a  striction 20 */.
2a726 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73      }.    if( is
2a727 41 67 67 20 7c 7c 20 28 70 2d 3e 73 65 6c 46 6c  Agg || (p->selFl
2a728 61 67 73 20 26 20 53 46 5f 44 69 73 74 69 6e 63  ags & SF_Distinc
2a729 74 29 21 3d 30 20 7c 7c 20 70 53 72 63 2d 3e 6e  t)!=0 || pSrc->n
2a72a 53 72 63 21 3d 31 20 29 7b 0a 20 20 20 20 20 20  Src!=1 ){.      
2a72b 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a  return 0;.    }.
2a72c 20 20 20 20 66 6f 72 28 70 53 75 62 31 3d 70 53      for(pSub1=pS
2a72d 75 62 3b 20 70 53 75 62 31 3b 20 70 53 75 62 31  ub; pSub1; pSub1
2a72e 3d 70 53 75 62 31 2d 3e 70 50 72 69 6f 72 29 7b  =pSub1->pPrior){
2a72f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2a730 20 28 70 53 75 62 31 2d 3e 73 65 6c 46 6c 61 67   (pSub1->selFlag
2a731 73 20 26 20 28 53 46 5f 44 69 73 74 69 6e 63 74  s & (SF_Distinct
2a732 7c 53 46 5f 41 67 67 72 65 67 61 74 65 29 29 3d  |SF_Aggregate))=
2a733 3d 53 46 5f 44 69 73 74 69 6e 63 74 20 29 3b 0a  =SF_Distinct );.
2a734 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2a735 28 70 53 75 62 31 2d 3e 73 65 6c 46 6c 61 67 73  (pSub1->selFlags
2a736 20 26 20 28 53 46 5f 44 69 73 74 69 6e 63 74 7c   & (SF_Distinct|
2a737 53 46 5f 41 67 67 72 65 67 61 74 65 29 29 3d 3d  SF_Aggregate))==
2a738 53 46 5f 41 67 67 72 65 67 61 74 65 20 29 3b 0a  SF_Aggregate );.
2a739 20 20 20 20 20 20 69 66 28 20 28 70 53 75 62 31        if( (pSub1
2a73a 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28 53 46  ->selFlags & (SF
2a73b 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41 67 67  _Distinct|SF_Agg
2a73c 72 65 67 61 74 65 29 29 21 3d 30 0a 20 20 20 20  regate))!=0.    
2a73d 20 20 20 7c 7c 20 28 70 53 75 62 31 2d 3e 70 50     || (pSub1->pP
2a73e 72 69 6f 72 20 26 26 20 70 53 75 62 31 2d 3e 6f  rior && pSub1->o
2a73f 70 21 3d 54 4b 5f 41 4c 4c 29 20 0a 20 20 20 20  p!=TK_ALL) .    
2a740 20 20 20 7c 7c 20 4e 45 56 45 52 28 70 53 75 62     || NEVER(pSub
2a741 31 2d 3e 70 53 72 63 3d 3d 30 29 20 7c 7c 20 70  1->pSrc==0) || p
2a742 53 75 62 31 2d 3e 70 53 72 63 2d 3e 6e 53 72 63  Sub1->pSrc->nSrc
2a743 21 3d 31 0a 20 20 20 20 20 20 29 7b 0a 20 20 20  !=1.      ){.   
2a744 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
2a745 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
2a746 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e    /* Restriction
2a747 20 31 38 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20   18. */.    if( 
2a748 70 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20  p->pOrderBy ){. 
2a749 20 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20       int ii;.   
2a74a 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c     for(ii=0; ii<
2a74b 70 2d 3e 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78  p->pOrderBy->nEx
2a74c 70 72 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20  pr; ii++){.     
2a74d 20 20 20 69 66 28 20 70 2d 3e 70 4f 72 64 65 72     if( p->pOrder
2a74e 42 79 2d 3e 61 5b 69 69 5d 2e 69 43 6f 6c 3d 3d  By->a[ii].iCol==
2a74f 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
2a750 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
2a751 0a 20 20 2f 2a 2a 2a 2a 2a 20 49 66 20 77 65 20  .  /***** If we 
2a752 72 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74  reach this point
2a753 2c 20 66 6c 61 74 74 65 6e 69 6e 67 20 69 73 20  , flattening is 
2a754 70 65 72 6d 69 74 74 65 64 2e 20 2a 2a 2a 2a 2a  permitted. *****
2a755 2f 0a 0a 20 20 2f 2a 20 41 75 74 68 6f 72 69 7a  /..  /* Authoriz
2a756 65 20 74 68 65 20 73 75 62 71 75 65 72 79 20 2a  e the subquery *
2a757 2f 0a 20 20 70 50 61 72 73 65 2d 3e 7a 41 75 74  /.  pParse->zAut
2a758 68 43 6f 6e 74 65 78 74 20 3d 20 70 53 75 62 69  hContext = pSubi
2a759 74 65 6d 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 73 71  tem->zName;.  sq
2a75a 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
2a75b 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 53 45  Parse, SQLITE_SE
2a75c 4c 45 43 54 2c 20 30 2c 20 30 2c 20 30 29 3b 0a  LECT, 0, 0, 0);.
2a75d 20 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43    pParse->zAuthC
2a75e 6f 6e 74 65 78 74 20 3d 20 7a 53 61 76 65 64 41  ontext = zSavedA
2a75f 75 74 68 43 6f 6e 74 65 78 74 3b 0a 0a 20 20 2f  uthContext;..  /
2a760 2a 20 49 66 20 74 68 65 20 73 75 62 2d 71 75 65  * If the sub-que
2a761 72 79 20 69 73 20 61 20 63 6f 6d 70 6f 75 6e 64  ry is a compound
2a762 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2a763 74 2c 20 74 68 65 6e 20 28 62 79 20 72 65 73 74  t, then (by rest
2a764 72 69 63 74 69 6f 6e 73 0a 20 20 2a 2a 20 31 37  rictions.  ** 17
2a765 20 61 6e 64 20 31 38 20 61 62 6f 76 65 29 20 69   and 18 above) i
2a766 74 20 6d 75 73 74 20 62 65 20 61 20 55 4e 49 4f  t must be a UNIO
2a767 4e 20 41 4c 4c 20 61 6e 64 20 74 68 65 20 70 61  N ALL and the pa
2a768 72 65 6e 74 20 71 75 65 72 79 20 6d 75 73 74 20  rent query must 
2a769 0a 20 20 2a 2a 20 62 65 20 6f 66 20 74 68 65 20  .  ** be of the 
2a76a 66 6f 72 6d 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  form:.  **.  ** 
2a76b 20 20 20 20 53 45 4c 45 43 54 20 3c 65 78 70 72      SELECT <expr
2a76c 2d 6c 69 73 74 3e 20 46 52 4f 4d 20 28 3c 73 75  -list> FROM (<su
2a76d 62 2d 71 75 65 72 79 3e 29 20 3c 77 68 65 72 65  b-query>) <where
2a76e 2d 63 6c 61 75 73 65 3e 20 0a 20 20 2a 2a 0a 20  -clause> .  **. 
2a76f 20 2a 2a 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20   ** followed by 
2a770 61 6e 79 20 4f 52 44 45 52 20 42 59 2c 20 4c 49  any ORDER BY, LI
2a771 4d 49 54 20 61 6e 64 2f 6f 72 20 4f 46 46 53 45  MIT and/or OFFSE
2a772 54 20 63 6c 61 75 73 65 73 2e 20 54 68 69 73 20  T clauses. This 
2a773 62 6c 6f 63 6b 0a 20 20 2a 2a 20 63 72 65 61 74  block.  ** creat
2a774 65 73 20 4e 2d 31 20 63 6f 70 69 65 73 20 6f 66  es N-1 copies of
2a775 20 74 68 65 20 70 61 72 65 6e 74 20 71 75 65 72   the parent quer
2a776 79 20 77 69 74 68 6f 75 74 20 61 6e 79 20 4f 52  y without any OR
2a777 44 45 52 20 42 59 2c 20 4c 49 4d 49 54 20 6f 72  DER BY, LIMIT or
2a778 20 0a 20 20 2a 2a 20 4f 46 46 53 45 54 20 63 6c   .  ** OFFSET cl
2a779 61 75 73 65 73 20 61 6e 64 20 6a 6f 69 6e 73 20  auses and joins 
2a77a 74 68 65 6d 20 74 6f 20 74 68 65 20 6c 65 66 74  them to the left
2a77b 2d 68 61 6e 64 2d 73 69 64 65 20 6f 66 20 74 68  -hand-side of th
2a77c 65 20 6f 72 69 67 69 6e 61 6c 0a 20 20 2a 2a 20  e original.  ** 
2a77d 75 73 69 6e 67 20 55 4e 49 4f 4e 20 41 4c 4c 20  using UNION ALL 
2a77e 6f 70 65 72 61 74 6f 72 73 2e 20 49 6e 20 74 68  operators. In th
2a77f 69 73 20 63 61 73 65 20 4e 20 69 73 20 74 68 65  is case N is the
2a780 20 6e 75 6d 62 65 72 20 6f 66 20 73 69 6d 70 6c   number of simpl
2a781 65 0a 20 20 2a 2a 20 73 65 6c 65 63 74 20 73 74  e.  ** select st
2a782 61 74 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20  atements in the 
2a783 63 6f 6d 70 6f 75 6e 64 20 73 75 62 2d 71 75 65  compound sub-que
2a784 72 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 78  ry..  **.  ** Ex
2a785 61 6d 70 6c 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  ample:.  **.  **
2a786 20 20 20 20 20 53 45 4c 45 43 54 20 61 2b 31 20       SELECT a+1 
2a787 46 52 4f 4d 20 28 0a 20 20 2a 2a 20 20 20 20 20  FROM (.  **     
2a788 20 20 20 53 45 4c 45 43 54 20 78 20 46 52 4f 4d     SELECT x FROM
2a789 20 74 61 62 0a 20 20 2a 2a 20 20 20 20 20 20 20   tab.  **       
2a78a 20 55 4e 49 4f 4e 20 41 4c 4c 0a 20 20 2a 2a 20   UNION ALL.  ** 
2a78b 20 20 20 20 20 20 20 53 45 4c 45 43 54 20 79 20         SELECT y 
2a78c 46 52 4f 4d 20 74 61 62 0a 20 20 2a 2a 20 20 20  FROM tab.  **   
2a78d 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c 4c 0a 20       UNION ALL. 
2a78e 20 2a 2a 20 20 20 20 20 20 20 20 53 45 4c 45 43   **        SELEC
2a78f 54 20 61 62 73 28 7a 2a 32 29 20 46 52 4f 4d 20  T abs(z*2) FROM 
2a790 74 61 62 32 0a 20 20 2a 2a 20 20 20 20 20 29 20  tab2.  **     ) 
2a791 57 48 45 52 45 20 61 21 3d 35 20 4f 52 44 45 52  WHERE a!=5 ORDER
2a792 20 42 59 20 31 0a 20 20 2a 2a 0a 20 20 2a 2a 20   BY 1.  **.  ** 
2a793 54 72 61 6e 73 66 6f 72 6d 65 64 20 69 6e 74 6f  Transformed into
2a794 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20  :.  **.  **     
2a795 53 45 4c 45 43 54 20 78 2b 31 20 46 52 4f 4d 20  SELECT x+1 FROM 
2a796 74 61 62 20 57 48 45 52 45 20 78 2b 31 21 3d 35  tab WHERE x+1!=5
2a797 0a 20 20 2a 2a 20 20 20 20 20 55 4e 49 4f 4e 20  .  **     UNION 
2a798 41 4c 4c 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c  ALL.  **     SEL
2a799 45 43 54 20 79 2b 31 20 46 52 4f 4d 20 74 61 62  ECT y+1 FROM tab
2a79a 20 57 48 45 52 45 20 79 2b 31 21 3d 35 0a 20 20   WHERE y+1!=5.  
2a79b 2a 2a 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c 4c  **     UNION ALL
2a79c 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54  .  **     SELECT
2a79d 20 61 62 73 28 7a 2a 32 29 2b 31 20 46 52 4f 4d   abs(z*2)+1 FROM
2a79e 20 74 61 62 32 20 57 48 45 52 45 20 61 62 73 28   tab2 WHERE abs(
2a79f 7a 2a 32 29 2b 31 21 3d 35 0a 20 20 2a 2a 20 20  z*2)+1!=5.  **  
2a7a0 20 20 20 4f 52 44 45 52 20 42 59 20 31 0a 20 20     ORDER BY 1.  
2a7a1 2a 2a 0a 20 20 2a 2a 20 57 65 20 63 61 6c 6c 20  **.  ** We call 
2a7a2 74 68 69 73 20 74 68 65 20 22 63 6f 6d 70 6f 75  this the "compou
2a7a3 6e 64 2d 73 75 62 71 75 65 72 79 20 66 6c 61 74  nd-subquery flat
2a7a4 74 65 6e 69 6e 67 22 2e 0a 20 20 2a 2f 0a 20 20  tening"..  */.  
2a7a5 66 6f 72 28 70 53 75 62 3d 70 53 75 62 2d 3e 70  for(pSub=pSub->p
2a7a6 50 72 69 6f 72 3b 20 70 53 75 62 3b 20 70 53 75  Prior; pSub; pSu
2a7a7 62 3d 70 53 75 62 2d 3e 70 50 72 69 6f 72 29 7b  b=pSub->pPrior){
2a7a8 0a 20 20 20 20 53 65 6c 65 63 74 20 2a 70 4e 65  .    Select *pNe
2a7a9 77 3b 0a 20 20 20 20 45 78 70 72 4c 69 73 74 20  w;.    ExprList 
2a7aa 2a 70 4f 72 64 65 72 42 79 20 3d 20 70 2d 3e 70  *pOrderBy = p->p
2a7ab 4f 72 64 65 72 42 79 3b 0a 20 20 20 20 45 78 70  OrderBy;.    Exp
2a7ac 72 20 2a 70 4c 69 6d 69 74 20 3d 20 70 2d 3e 70  r *pLimit = p->p
2a7ad 4c 69 6d 69 74 3b 0a 20 20 20 20 53 65 6c 65 63  Limit;.    Selec
2a7ae 74 20 2a 70 50 72 69 6f 72 20 3d 20 70 2d 3e 70  t *pPrior = p->p
2a7af 50 72 69 6f 72 3b 0a 20 20 20 20 70 2d 3e 70 4f  Prior;.    p->pO
2a7b0 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20 20 20  rderBy = 0;.    
2a7b1 70 2d 3e 70 53 72 63 20 3d 20 30 3b 0a 20 20 20  p->pSrc = 0;.   
2a7b2 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20 30 3b 0a   p->pPrior = 0;.
2a7b3 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20      p->pLimit = 
2a7b4 30 3b 0a 20 20 20 20 70 4e 65 77 20 3d 20 73 71  0;.    pNew = sq
2a7b5 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 64  lite3SelectDup(d
2a7b6 62 2c 20 70 2c 20 30 29 3b 0a 20 20 20 20 70 2d  b, p, 0);.    p-
2a7b7 3e 70 4c 69 6d 69 74 20 3d 20 70 4c 69 6d 69 74  >pLimit = pLimit
2a7b8 3b 0a 20 20 20 20 70 2d 3e 70 4f 72 64 65 72 42  ;.    p->pOrderB
2a7b9 79 20 3d 20 70 4f 72 64 65 72 42 79 3b 0a 20 20  y = pOrderBy;.  
2a7ba 20 20 70 2d 3e 70 53 72 63 20 3d 20 70 53 72 63    p->pSrc = pSrc
2a7bb 3b 0a 20 20 20 20 70 2d 3e 6f 70 20 3d 20 54 4b  ;.    p->op = TK
2a7bc 5f 41 4c 4c 3b 0a 20 20 20 20 70 2d 3e 70 52 69  _ALL;.    p->pRi
2a7bd 67 68 74 6d 6f 73 74 20 3d 20 30 3b 0a 20 20 20  ghtmost = 0;.   
2a7be 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a   if( pNew==0 ){.
2a7bf 20 20 20 20 20 20 70 4e 65 77 20 3d 20 70 50 72        pNew = pPr
2a7c0 69 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ior;.    }else{.
2a7c1 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 50 72 69        pNew->pPri
2a7c2 6f 72 20 3d 20 70 50 72 69 6f 72 3b 0a 20 20 20  or = pPrior;.   
2a7c3 20 20 20 70 4e 65 77 2d 3e 70 52 69 67 68 74 6d     pNew->pRightm
2a7c4 6f 73 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  ost = 0;.    }. 
2a7c5 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20 70     p->pPrior = p
2a7c6 4e 65 77 3b 0a 20 20 20 20 69 66 28 20 64 62 2d  New;.    if( db-
2a7c7 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20  >mallocFailed ) 
2a7c8 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20  return 1;.  }.. 
2a7c9 20 2f 2a 20 42 65 67 69 6e 20 66 6c 61 74 74 65   /* Begin flatte
2a7ca 6e 69 6e 67 20 74 68 65 20 69 46 72 6f 6d 2d 74  ning the iFrom-t
2a7cb 68 20 65 6e 74 72 79 20 6f 66 20 74 68 65 20 46  h entry of the F
2a7cc 52 4f 4d 20 63 6c 61 75 73 65 20 0a 20 20 2a 2a  ROM clause .  **
2a7cd 20 69 6e 20 74 68 65 20 6f 75 74 65 72 20 71 75   in the outer qu
2a7ce 65 72 79 2e 0a 20 20 2a 2f 0a 20 20 70 53 75 62  ery..  */.  pSub
2a7cf 20 3d 20 70 53 75 62 31 20 3d 20 70 53 75 62 69   = pSub1 = pSubi
2a7d0 74 65 6d 2d 3e 70 53 65 6c 65 63 74 3b 0a 0a 20  tem->pSelect;.. 
2a7d1 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20 74   /* Delete the t
2a7d2 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 20 73  ransient table s
2a7d3 74 72 75 63 74 75 72 65 20 61 73 73 6f 63 69 61  tructure associa
2a7d4 74 65 64 20 77 69 74 68 20 74 68 65 0a 20 20 2a  ted with the.  *
2a7d5 2a 20 73 75 62 71 75 65 72 79 0a 20 20 2a 2f 0a  * subquery.  */.
2a7d6 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2a7d7 64 62 2c 20 70 53 75 62 69 74 65 6d 2d 3e 7a 44  db, pSubitem->zD
2a7d8 61 74 61 62 61 73 65 29 3b 0a 20 20 73 71 6c 69  atabase);.  sqli
2a7d9 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 53  te3DbFree(db, pS
2a7da 75 62 69 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a  ubitem->zName);.
2a7db 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2a7dc 64 62 2c 20 70 53 75 62 69 74 65 6d 2d 3e 7a 41  db, pSubitem->zA
2a7dd 6c 69 61 73 29 3b 0a 20 20 70 53 75 62 69 74 65  lias);.  pSubite
2a7de 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20 3d 20 30  m->zDatabase = 0
2a7df 3b 0a 20 20 70 53 75 62 69 74 65 6d 2d 3e 7a 4e  ;.  pSubitem->zN
2a7e0 61 6d 65 20 3d 20 30 3b 0a 20 20 70 53 75 62 69  ame = 0;.  pSubi
2a7e1 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 3d 20 30 3b  tem->zAlias = 0;
2a7e2 0a 20 20 70 53 75 62 69 74 65 6d 2d 3e 70 53 65  .  pSubitem->pSe
2a7e3 6c 65 63 74 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20  lect = 0;..  /* 
2a7e4 44 65 66 65 72 20 64 65 6c 65 74 69 6e 67 20 74  Defer deleting t
2a7e5 68 65 20 54 61 62 6c 65 20 6f 62 6a 65 63 74 20  he Table object 
2a7e6 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
2a7e7 74 68 65 0a 20 20 2a 2a 20 73 75 62 71 75 65 72  the.  ** subquer
2a7e8 79 20 75 6e 74 69 6c 20 63 6f 64 65 20 67 65 6e  y until code gen
2a7e9 65 72 61 74 69 6f 6e 20 69 73 0a 20 20 2a 2a 20  eration is.  ** 
2a7ea 63 6f 6d 70 6c 65 74 65 2c 20 73 69 6e 63 65 20  complete, since 
2a7eb 74 68 65 72 65 20 6d 61 79 20 73 74 69 6c 6c 20  there may still 
2a7ec 65 78 69 73 74 20 45 78 70 72 2e 70 54 61 62 20  exist Expr.pTab 
2a7ed 65 6e 74 72 69 65 73 20 74 68 61 74 0a 20 20 2a  entries that.  *
2a7ee 2a 20 72 65 66 65 72 20 74 6f 20 74 68 65 20 73  * refer to the s
2a7ef 75 62 71 75 65 72 79 20 65 76 65 6e 20 61 66 74  ubquery even aft
2a7f0 65 72 20 66 6c 61 74 74 65 6e 69 6e 67 2e 20 20  er flattening.  
2a7f1 54 69 63 6b 65 74 20 23 33 33 34 36 2e 0a 20 20  Ticket #3346..  
2a7f2 2a 2a 0a 20 20 2a 2a 20 70 53 75 62 69 74 65 6d  **.  ** pSubitem
2a7f3 2d 3e 70 54 61 62 20 69 73 20 61 6c 77 61 79 73  ->pTab is always
2a7f4 20 6e 6f 6e 2d 4e 55 4c 4c 20 62 79 20 74 65 73   non-NULL by tes
2a7f5 74 20 72 65 73 74 72 69 63 74 69 6f 6e 73 20 61  t restrictions a
2a7f6 6e 64 20 74 65 73 74 73 20 61 62 6f 76 65 2e 0a  nd tests above..
2a7f7 20 20 2a 2f 0a 20 20 69 66 28 20 41 4c 57 41 59    */.  if( ALWAY
2a7f8 53 28 70 53 75 62 69 74 65 6d 2d 3e 70 54 61 62  S(pSubitem->pTab
2a7f9 21 3d 30 29 20 29 7b 0a 20 20 20 20 54 61 62 6c  !=0) ){.    Tabl
2a7fa 65 20 2a 70 54 61 62 54 6f 44 65 6c 20 3d 20 70  e *pTabToDel = p
2a7fb 53 75 62 69 74 65 6d 2d 3e 70 54 61 62 3b 0a 20  Subitem->pTab;. 
2a7fc 20 20 20 69 66 28 20 70 54 61 62 54 6f 44 65 6c     if( pTabToDel
2a7fd 2d 3e 6e 52 65 66 3d 3d 31 20 29 7b 0a 20 20 20  ->nRef==1 ){.   
2a7fe 20 20 20 50 61 72 73 65 20 2a 70 54 6f 70 6c 65     Parse *pTople
2a7ff 76 65 6c 20 3d 20 73 71 6c 69 74 65 33 50 61 72  vel = sqlite3Par
2a800 73 65 54 6f 70 6c 65 76 65 6c 28 70 50 61 72 73  seToplevel(pPars
2a801 65 29 3b 0a 20 20 20 20 20 20 70 54 61 62 54 6f  e);.      pTabTo
2a802 44 65 6c 2d 3e 70 4e 65 78 74 5a 6f 6d 62 69 65  Del->pNextZombie
2a803 20 3d 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 70 5a   = pToplevel->pZ
2a804 6f 6d 62 69 65 54 61 62 3b 0a 20 20 20 20 20 20  ombieTab;.      
2a805 70 54 6f 70 6c 65 76 65 6c 2d 3e 70 5a 6f 6d 62  pToplevel->pZomb
2a806 69 65 54 61 62 20 3d 20 70 54 61 62 54 6f 44 65  ieTab = pTabToDe
2a807 6c 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  l;.    }else{.  
2a808 20 20 20 20 70 54 61 62 54 6f 44 65 6c 2d 3e 6e      pTabToDel->n
2a809 52 65 66 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20  Ref--;.    }.   
2a80a 20 70 53 75 62 69 74 65 6d 2d 3e 70 54 61 62 20   pSubitem->pTab 
2a80b 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54  = 0;.  }..  /* T
2a80c 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6c 6f 6f  he following loo
2a80d 70 20 72 75 6e 73 20 6f 6e 63 65 20 66 6f 72 20  p runs once for 
2a80e 65 61 63 68 20 74 65 72 6d 20 69 6e 20 61 20 63  each term in a c
2a80f 6f 6d 70 6f 75 6e 64 2d 73 75 62 71 75 65 72 79  ompound-subquery
2a810 0a 20 20 2a 2a 20 66 6c 61 74 74 65 6e 69 6e 67  .  ** flattening
2a811 20 28 61 73 20 64 65 73 63 72 69 62 65 64 20 61   (as described a
2a812 62 6f 76 65 29 2e 20 20 49 66 20 77 65 20 61 72  bove).  If we ar
2a813 65 20 64 6f 69 6e 67 20 61 20 64 69 66 66 65 72  e doing a differ
2a814 65 6e 74 20 6b 69 6e 64 0a 20 20 2a 2a 20 6f 66  ent kind.  ** of
2a815 20 66 6c 61 74 74 65 6e 69 6e 67 20 2d 20 61 20   flattening - a 
2a816 66 6c 61 74 74 65 6e 69 6e 67 20 6f 74 68 65 72  flattening other
2a817 20 74 68 61 6e 20 61 20 63 6f 6d 70 6f 75 6e 64   than a compound
2a818 2d 73 75 62 71 75 65 72 79 20 66 6c 61 74 74 65  -subquery flatte
2a819 6e 69 6e 67 20 2d 0a 20 20 2a 2a 20 74 68 65 6e  ning -.  ** then
2a81a 20 74 68 69 73 20 6c 6f 6f 70 20 6f 6e 6c 79 20   this loop only 
2a81b 72 75 6e 73 20 6f 6e 63 65 2e 0a 20 20 2a 2a 0a  runs once..  **.
2a81c 20 20 2a 2a 20 54 68 69 73 20 6c 6f 6f 70 20 6d    ** This loop m
2a81d 6f 76 65 73 20 61 6c 6c 20 6f 66 20 74 68 65 20  oves all of the 
2a81e 46 52 4f 4d 20 65 6c 65 6d 65 6e 74 73 20 6f 66  FROM elements of
2a81f 20 74 68 65 20 73 75 62 71 75 65 72 79 20 69 6e   the subquery in
2a820 74 6f 20 74 68 65 0a 20 20 2a 2a 20 74 68 65 20  to the.  ** the 
2a821 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74  FROM clause of t
2a822 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 2e 20  he outer query. 
2a823 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 74 68   Before doing th
2a824 69 73 2c 20 72 65 6d 65 6d 62 65 72 0a 20 20 2a  is, remember.  *
2a825 2a 20 74 68 65 20 63 75 72 73 6f 72 20 6e 75 6d  * the cursor num
2a826 62 65 72 20 66 6f 72 20 74 68 65 20 6f 72 69 67  ber for the orig
2a827 69 6e 61 6c 20 6f 75 74 65 72 20 71 75 65 72 79  inal outer query
2a828 20 46 52 4f 4d 20 65 6c 65 6d 65 6e 74 20 69 6e   FROM element in
2a829 0a 20 20 2a 2a 20 69 50 61 72 65 6e 74 2e 20 20  .  ** iParent.  
2a82a 54 68 65 20 69 50 61 72 65 6e 74 20 63 75 72 73  The iParent curs
2a82b 6f 72 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65  or will never be
2a82c 20 75 73 65 64 2e 20 20 53 75 62 73 65 71 75 65   used.  Subseque
2a82d 6e 74 20 63 6f 64 65 0a 20 20 2a 2a 20 77 69 6c  nt code.  ** wil
2a82e 6c 20 73 63 61 6e 20 65 78 70 72 65 73 73 69 6f  l scan expressio
2a82f 6e 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 69  ns looking for i
2a830 50 61 72 65 6e 74 20 72 65 66 65 72 65 6e 63 65  Parent reference
2a831 73 20 61 6e 64 20 72 65 70 6c 61 63 65 0a 20 20  s and replace.  
2a832 2a 2a 20 74 68 6f 73 65 20 72 65 66 65 72 65 6e  ** those referen
2a833 63 65 73 20 77 69 74 68 20 65 78 70 72 65 73 73  ces with express
2a834 69 6f 6e 73 20 74 68 61 74 20 72 65 73 6f 6c 76  ions that resolv
2a835 65 20 74 6f 20 74 68 65 20 73 75 62 71 75 65 72  e to the subquer
2a836 79 20 46 52 4f 4d 0a 20 20 2a 2a 20 65 6c 65 6d  y FROM.  ** elem
2a837 65 6e 74 73 20 77 65 20 61 72 65 20 6e 6f 77 20  ents we are now 
2a838 63 6f 70 79 69 6e 67 20 69 6e 2e 0a 20 20 2a 2f  copying in..  */
2a839 0a 20 20 66 6f 72 28 70 50 61 72 65 6e 74 3d 70  .  for(pParent=p
2a83a 3b 20 70 50 61 72 65 6e 74 3b 20 70 50 61 72 65  ; pParent; pPare
2a83b 6e 74 3d 70 50 61 72 65 6e 74 2d 3e 70 50 72 69  nt=pParent->pPri
2a83c 6f 72 2c 20 70 53 75 62 3d 70 53 75 62 2d 3e 70  or, pSub=pSub->p
2a83d 50 72 69 6f 72 29 7b 0a 20 20 20 20 69 6e 74 20  Prior){.    int 
2a83e 6e 53 75 62 53 72 63 3b 0a 20 20 20 20 75 38 20  nSubSrc;.    u8 
2a83f 6a 6f 69 6e 74 79 70 65 20 3d 20 30 3b 0a 20 20  jointype = 0;.  
2a840 20 20 70 53 75 62 53 72 63 20 3d 20 70 53 75 62    pSubSrc = pSub
2a841 2d 3e 70 53 72 63 3b 20 20 20 20 20 2f 2a 20 46  ->pSrc;     /* F
2a842 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 73 75  ROM clause of su
2a843 62 71 75 65 72 79 20 2a 2f 0a 20 20 20 20 6e 53  bquery */.    nS
2a844 75 62 53 72 63 20 3d 20 70 53 75 62 53 72 63 2d  ubSrc = pSubSrc-
2a845 3e 6e 53 72 63 3b 20 20 2f 2a 20 4e 75 6d 62 65  >nSrc;  /* Numbe
2a846 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 73 75  r of terms in su
2a847 62 71 75 65 72 79 20 46 52 4f 4d 20 63 6c 61 75  bquery FROM clau
2a848 73 65 20 2a 2f 0a 20 20 20 20 70 53 72 63 20 3d  se */.    pSrc =
2a849 20 70 50 61 72 65 6e 74 2d 3e 70 53 72 63 3b 20   pParent->pSrc; 
2a84a 20 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75      /* FROM clau
2a84b 73 65 20 6f 66 20 74 68 65 20 6f 75 74 65 72 20  se of the outer 
2a84c 71 75 65 72 79 20 2a 2f 0a 0a 20 20 20 20 69 66  query */..    if
2a84d 28 20 70 53 72 63 20 29 7b 0a 20 20 20 20 20 20  ( pSrc ){.      
2a84e 61 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 3d  assert( pParent=
2a84f 3d 70 20 29 3b 20 20 2f 2a 20 46 69 72 73 74 20  =p );  /* First 
2a850 74 69 6d 65 20 74 68 72 6f 75 67 68 20 74 68 65  time through the
2a851 20 6c 6f 6f 70 20 2a 2f 0a 20 20 20 20 20 20 6a   loop */.      j
2a852 6f 69 6e 74 79 70 65 20 3d 20 70 53 75 62 69 74  ointype = pSubit
2a853 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65 3b 0a 20 20  em->jointype;.  
2a854 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61    }else{.      a
2a855 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 21 3d  ssert( pParent!=
2a856 70 20 29 3b 20 20 2f 2a 20 32 6e 64 20 61 6e 64  p );  /* 2nd and
2a857 20 73 75 62 73 65 71 75 65 6e 74 20 74 69 6d 65   subsequent time
2a858 73 20 74 68 72 6f 75 67 68 20 74 68 65 20 6c 6f  s through the lo
2a859 6f 70 20 2a 2f 0a 20 20 20 20 20 20 70 53 72 63  op */.      pSrc
2a85a 20 3d 20 70 50 61 72 65 6e 74 2d 3e 70 53 72 63   = pParent->pSrc
2a85b 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73   = sqlite3SrcLis
2a85c 74 41 70 70 65 6e 64 28 64 62 2c 20 30 2c 20 30  tAppend(db, 0, 0
2a85d 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , 0);.      if( 
2a85e 70 53 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20  pSrc==0 ){.     
2a85f 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d     assert( db->m
2a860 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
2a861 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2a862 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
2a863 20 2f 2a 20 54 68 65 20 73 75 62 71 75 65 72 79   /* The subquery
2a864 20 75 73 65 73 20 61 20 73 69 6e 67 6c 65 20 73   uses a single s
2a865 6c 6f 74 20 6f 66 20 74 68 65 20 46 52 4f 4d 20  lot of the FROM 
2a866 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 6f 75  clause of the ou
2a867 74 65 72 0a 20 20 20 20 2a 2a 20 71 75 65 72 79  ter.    ** query
2a868 2e 20 20 49 66 20 74 68 65 20 73 75 62 71 75 65  .  If the subque
2a869 72 79 20 68 61 73 20 6d 6f 72 65 20 74 68 61 6e  ry has more than
2a86a 20 6f 6e 65 20 65 6c 65 6d 65 6e 74 20 69 6e 20   one element in 
2a86b 69 74 73 20 46 52 4f 4d 20 63 6c 61 75 73 65 2c  its FROM clause,
2a86c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 65 78 70  .    ** then exp
2a86d 61 6e 64 20 74 68 65 20 6f 75 74 65 72 20 71 75  and the outer qu
2a86e 65 72 79 20 74 6f 20 6d 61 6b 65 20 73 70 61 63  ery to make spac
2a86f 65 20 66 6f 72 20 69 74 20 74 6f 20 68 6f 6c 64  e for it to hold
2a870 20 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 0a 20 20   all elements.  
2a871 20 20 2a 2a 20 6f 66 20 74 68 65 20 73 75 62 71    ** of the subq
2a872 75 65 72 79 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  uery..    **.   
2a873 20 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a 20 20 20   ** Example:.   
2a874 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 53 45   **.    **    SE
2a875 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 61 62 41  LECT * FROM tabA
2a876 2c 20 28 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d  , (SELECT * FROM
2a877 20 73 75 62 31 2c 20 73 75 62 32 29 2c 20 74 61   sub1, sub2), ta
2a878 62 42 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  bB;.    **.    *
2a879 2a 20 54 68 65 20 6f 75 74 65 72 20 71 75 65 72  * The outer quer
2a87a 79 20 68 61 73 20 33 20 73 6c 6f 74 73 20 69 6e  y has 3 slots in
2a87b 20 69 74 73 20 46 52 4f 4d 20 63 6c 61 75 73 65   its FROM clause
2a87c 2e 20 20 4f 6e 65 20 73 6c 6f 74 20 6f 66 20 74  .  One slot of t
2a87d 68 65 0a 20 20 20 20 2a 2a 20 6f 75 74 65 72 20  he.    ** outer 
2a87e 71 75 65 72 79 20 28 74 68 65 20 6d 69 64 64 6c  query (the middl
2a87f 65 20 73 6c 6f 74 29 20 69 73 20 75 73 65 64 20  e slot) is used 
2a880 62 79 20 74 68 65 20 73 75 62 71 75 65 72 79 2e  by the subquery.
2a881 20 20 54 68 65 20 6e 65 78 74 0a 20 20 20 20 2a    The next.    *
2a882 2a 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 20  * block of code 
2a883 77 69 6c 6c 20 65 78 70 61 6e 64 20 74 68 65 20  will expand the 
2a884 6f 75 74 20 71 75 65 72 79 20 74 6f 20 34 20 73  out query to 4 s
2a885 6c 6f 74 73 2e 20 20 54 68 65 20 6d 69 64 64 6c  lots.  The middl
2a886 65 0a 20 20 20 20 2a 2a 20 73 6c 6f 74 20 69 73  e.    ** slot is
2a887 20 65 78 70 61 6e 64 65 64 20 74 6f 20 74 77 6f   expanded to two
2a888 20 73 6c 6f 74 73 20 69 6e 20 6f 72 64 65 72 20   slots in order 
2a889 74 6f 20 6d 61 6b 65 20 73 70 61 63 65 20 66 6f  to make space fo
2a88a 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 77 6f  r the.    ** two
2a88b 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65   elements in the
2a88c 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20   FROM clause of 
2a88d 74 68 65 20 73 75 62 71 75 65 72 79 2e 0a 20 20  the subquery..  
2a88e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 53 75    */.    if( nSu
2a88f 62 53 72 63 3e 31 20 29 7b 0a 20 20 20 20 20 20  bSrc>1 ){.      
2a890 70 50 61 72 65 6e 74 2d 3e 70 53 72 63 20 3d 20  pParent->pSrc = 
2a891 70 53 72 63 20 3d 20 73 71 6c 69 74 65 33 53 72  pSrc = sqlite3Sr
2a892 63 4c 69 73 74 45 6e 6c 61 72 67 65 28 64 62 2c  cListEnlarge(db,
2a893 20 70 53 72 63 2c 20 6e 53 75 62 53 72 63 2d 31   pSrc, nSubSrc-1
2a894 2c 69 46 72 6f 6d 2b 31 29 3b 0a 20 20 20 20 20  ,iFrom+1);.     
2a895 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
2a896 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20  ailed ){.       
2a897 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
2a898 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 72      }..    /* Tr
2a899 61 6e 73 66 65 72 20 74 68 65 20 46 52 4f 4d 20  ansfer the FROM 
2a89a 63 6c 61 75 73 65 20 74 65 72 6d 73 20 66 72 6f  clause terms fro
2a89b 6d 20 74 68 65 20 73 75 62 71 75 65 72 79 20 69  m the subquery i
2a89c 6e 74 6f 20 74 68 65 0a 20 20 20 20 2a 2a 20 6f  nto the.    ** o
2a89d 75 74 65 72 20 71 75 65 72 79 2e 0a 20 20 20 20  uter query..    
2a89e 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  */.    for(i=0; 
2a89f 69 3c 6e 53 75 62 53 72 63 3b 20 69 2b 2b 29 7b  i<nSubSrc; i++){
2a8a0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 49 64  .      sqlite3Id
2a8a1 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
2a8a2 53 72 63 2d 3e 61 5b 69 2b 69 46 72 6f 6d 5d 2e  Src->a[i+iFrom].
2a8a3 70 55 73 69 6e 67 29 3b 0a 20 20 20 20 20 20 70  pUsing);.      p
2a8a4 53 72 63 2d 3e 61 5b 69 2b 69 46 72 6f 6d 5d 20  Src->a[i+iFrom] 
2a8a5 3d 20 70 53 75 62 53 72 63 2d 3e 61 5b 69 5d 3b  = pSubSrc->a[i];
2a8a6 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 70  .      memset(&p
2a8a7 53 75 62 53 72 63 2d 3e 61 5b 69 5d 2c 20 30 2c  SubSrc->a[i], 0,
2a8a8 20 73 69 7a 65 6f 66 28 70 53 75 62 53 72 63 2d   sizeof(pSubSrc-
2a8a9 3e 61 5b 69 5d 29 29 3b 0a 20 20 20 20 7d 0a 20  >a[i]));.    }. 
2a8aa 20 20 20 70 53 72 63 2d 3e 61 5b 69 46 72 6f 6d     pSrc->a[iFrom
2a8ab 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d 20 6a 6f 69  ].jointype = joi
2a8ac 6e 74 79 70 65 3b 0a 20 20 0a 20 20 20 20 2f 2a  ntype;.  .    /*
2a8ad 20 4e 6f 77 20 62 65 67 69 6e 20 73 75 62 73 74   Now begin subst
2a8ae 69 74 75 74 69 6e 67 20 73 75 62 71 75 65 72 79  ituting subquery
2a8af 20 72 65 73 75 6c 74 20 73 65 74 20 65 78 70 72   result set expr
2a8b0 65 73 73 69 6f 6e 73 20 66 6f 72 20 0a 20 20 20  essions for .   
2a8b1 20 2a 2a 20 72 65 66 65 72 65 6e 63 65 73 20 74   ** references t
2a8b2 6f 20 74 68 65 20 69 50 61 72 65 6e 74 20 69 6e  o the iParent in
2a8b3 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79   the outer query
2a8b4 2e 0a 20 20 20 20 2a 2a 20 0a 20 20 20 20 2a 2a  ..    ** .    **
2a8b5 20 45 78 61 6d 70 6c 65 3a 0a 20 20 20 20 2a 2a   Example:.    **
2a8b6 0a 20 20 20 20 2a 2a 20 20 20 53 45 4c 45 43 54  .    **   SELECT
2a8b7 20 61 2b 35 2c 20 62 2a 31 30 20 46 52 4f 4d 20   a+5, b*10 FROM 
2a8b8 28 53 45 4c 45 43 54 20 78 2a 33 20 41 53 20 61  (SELECT x*3 AS a
2a8b9 2c 20 79 2b 31 30 20 41 53 20 62 20 46 52 4f 4d  , y+10 AS b FROM
2a8ba 20 74 31 29 20 57 48 45 52 45 20 61 3e 62 3b 0a   t1) WHERE a>b;.
2a8bb 20 20 20 20 2a 2a 20 20 20 5c 20 20 20 20 20 20      **   \      
2a8bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
2a8bd 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 20 73 75  _____________ su
2a8be 62 71 75 65 72 79 20 5f 5f 5f 5f 5f 5f 5f 5f 5f  bquery _________
2a8bf 5f 2f 20 20 20 20 20 20 20 20 20 20 2f 0a 20 20  _/          /.  
2a8c0 20 20 2a 2a 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f    **    \_______
2a8c1 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 20 6f  ______________ o
2a8c2 75 74 65 72 20 71 75 65 72 79 20 5f 5f 5f 5f 5f  uter query _____
2a8c3 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
2a8c4 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 20 20 20 20 2a  _________/.    *
2a8c5 2a 0a 20 20 20 20 2a 2a 20 57 65 20 6c 6f 6f 6b  *.    ** We look
2a8c6 20 61 74 20 65 76 65 72 79 20 65 78 70 72 65 73   at every expres
2a8c7 73 69 6f 6e 20 69 6e 20 74 68 65 20 6f 75 74 65  sion in the oute
2a8c8 72 20 71 75 65 72 79 20 61 6e 64 20 65 76 65 72  r query and ever
2a8c9 79 20 70 6c 61 63 65 20 77 65 20 73 65 65 0a 20  y place we see. 
2a8ca 20 20 20 2a 2a 20 22 61 22 20 77 65 20 73 75 62     ** "a" we sub
2a8cb 73 74 69 74 75 74 65 20 22 78 2a 33 22 20 61 6e  stitute "x*3" an
2a8cc 64 20 65 76 65 72 79 20 70 6c 61 63 65 20 77 65  d every place we
2a8cd 20 73 65 65 20 22 62 22 20 77 65 20 73 75 62 73   see "b" we subs
2a8ce 74 69 74 75 74 65 20 22 79 2b 31 30 22 2e 0a 20  titute "y+10".. 
2a8cf 20 20 20 2a 2f 0a 20 20 20 20 70 4c 69 73 74 20     */.    pList 
2a8d0 3d 20 70 50 61 72 65 6e 74 2d 3e 70 45 4c 69 73  = pParent->pELis
2a8d1 74 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  t;.    for(i=0; 
2a8d2 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20  i<pList->nExpr; 
2a8d3 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  i++){.      if( 
2a8d4 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d  pList->a[i].zNam
2a8d5 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  e==0 ){.        
2a8d6 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 70 61  const char *zSpa
2a8d7 6e 20 3d 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e  n = pList->a[i].
2a8d8 7a 53 70 61 6e 3b 0a 20 20 20 20 20 20 20 20 69  zSpan;.        i
2a8d9 66 28 20 41 4c 57 41 59 53 28 7a 53 70 61 6e 29  f( ALWAYS(zSpan)
2a8da 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4c   ){.          pL
2a8db 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 20  ist->a[i].zName 
2a8dc 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
2a8dd 70 28 64 62 2c 20 7a 53 70 61 6e 29 3b 0a 20 20  p(db, zSpan);.  
2a8de 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
2a8df 20 20 20 20 7d 0a 20 20 20 20 73 75 62 73 74 45      }.    substE
2a8e0 78 70 72 4c 69 73 74 28 64 62 2c 20 70 50 61 72  xprList(db, pPar
2a8e1 65 6e 74 2d 3e 70 45 4c 69 73 74 2c 20 69 50 61  ent->pEList, iPa
2a8e2 72 65 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c 69  rent, pSub->pELi
2a8e3 73 74 29 3b 0a 20 20 20 20 69 66 28 20 69 73 41  st);.    if( isA
2a8e4 67 67 20 29 7b 0a 20 20 20 20 20 20 73 75 62 73  gg ){.      subs
2a8e5 74 45 78 70 72 4c 69 73 74 28 64 62 2c 20 70 50  tExprList(db, pP
2a8e6 61 72 65 6e 74 2d 3e 70 47 72 6f 75 70 42 79 2c  arent->pGroupBy,
2a8e7 20 69 50 61 72 65 6e 74 2c 20 70 53 75 62 2d 3e   iParent, pSub->
2a8e8 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 70  pEList);.      p
2a8e9 50 61 72 65 6e 74 2d 3e 70 48 61 76 69 6e 67 20  Parent->pHaving 
2a8ea 3d 20 73 75 62 73 74 45 78 70 72 28 64 62 2c 20  = substExpr(db, 
2a8eb 70 50 61 72 65 6e 74 2d 3e 70 48 61 76 69 6e 67  pParent->pHaving
2a8ec 2c 20 69 50 61 72 65 6e 74 2c 20 70 53 75 62 2d  , iParent, pSub-
2a8ed 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 7d 0a  >pEList);.    }.
2a8ee 20 20 20 20 69 66 28 20 70 53 75 62 2d 3e 70 4f      if( pSub->pO
2a8ef 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 20 20  rderBy ){.      
2a8f0 61 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 2d  assert( pParent-
2a8f1 3e 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 3b 0a  >pOrderBy==0 );.
2a8f2 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70        pParent->p
2a8f3 4f 72 64 65 72 42 79 20 3d 20 70 53 75 62 2d 3e  OrderBy = pSub->
2a8f4 70 4f 72 64 65 72 42 79 3b 0a 20 20 20 20 20 20  pOrderBy;.      
2a8f5 70 53 75 62 2d 3e 70 4f 72 64 65 72 42 79 20 3d  pSub->pOrderBy =
2a8f6 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66   0;.    }else if
2a8f7 28 20 70 50 61 72 65 6e 74 2d 3e 70 4f 72 64 65  ( pParent->pOrde
2a8f8 72 42 79 20 29 7b 0a 20 20 20 20 20 20 73 75 62  rBy ){.      sub
2a8f9 73 74 45 78 70 72 4c 69 73 74 28 64 62 2c 20 70  stExprList(db, p
2a8fa 50 61 72 65 6e 74 2d 3e 70 4f 72 64 65 72 42 79  Parent->pOrderBy
2a8fb 2c 20 69 50 61 72 65 6e 74 2c 20 70 53 75 62 2d  , iParent, pSub-
2a8fc 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 7d 0a  >pEList);.    }.
2a8fd 20 20 20 20 69 66 28 20 70 53 75 62 2d 3e 70 57      if( pSub->pW
2a8fe 68 65 72 65 20 29 7b 0a 20 20 20 20 20 20 70 57  here ){.      pW
2a8ff 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78  here = sqlite3Ex
2a900 70 72 44 75 70 28 64 62 2c 20 70 53 75 62 2d 3e  prDup(db, pSub->
2a901 70 57 68 65 72 65 2c 20 30 29 3b 0a 20 20 20 20  pWhere, 0);.    
2a902 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 57 68  }else{.      pWh
2a903 65 72 65 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  ere = 0;.    }. 
2a904 20 20 20 69 66 28 20 73 75 62 71 75 65 72 79 49     if( subqueryI
2a905 73 41 67 67 20 29 7b 0a 20 20 20 20 20 20 61 73  sAgg ){.      as
2a906 73 65 72 74 28 20 70 50 61 72 65 6e 74 2d 3e 70  sert( pParent->p
2a907 48 61 76 69 6e 67 3d 3d 30 20 29 3b 0a 20 20 20  Having==0 );.   
2a908 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 48 61 76     pParent->pHav
2a909 69 6e 67 20 3d 20 70 50 61 72 65 6e 74 2d 3e 70  ing = pParent->p
2a90a 57 68 65 72 65 3b 0a 20 20 20 20 20 20 70 50 61  Where;.      pPa
2a90b 72 65 6e 74 2d 3e 70 57 68 65 72 65 20 3d 20 70  rent->pWhere = p
2a90c 57 68 65 72 65 3b 0a 20 20 20 20 20 20 70 50 61  Where;.      pPa
2a90d 72 65 6e 74 2d 3e 70 48 61 76 69 6e 67 20 3d 20  rent->pHaving = 
2a90e 73 75 62 73 74 45 78 70 72 28 64 62 2c 20 70 50  substExpr(db, pP
2a90f 61 72 65 6e 74 2d 3e 70 48 61 76 69 6e 67 2c 20  arent->pHaving, 
2a910 69 50 61 72 65 6e 74 2c 20 70 53 75 62 2d 3e 70  iParent, pSub->p
2a911 45 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 70 50  EList);.      pP
2a912 61 72 65 6e 74 2d 3e 70 48 61 76 69 6e 67 20 3d  arent->pHaving =
2a913 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28   sqlite3ExprAnd(
2a914 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 48 61  db, pParent->pHa
2a915 76 69 6e 67 2c 20 0a 20 20 20 20 20 20 20 20 20  ving, .         
2a916 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a917 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2a918 45 78 70 72 44 75 70 28 64 62 2c 20 70 53 75 62  ExprDup(db, pSub
2a919 2d 3e 70 48 61 76 69 6e 67 2c 20 30 29 29 3b 0a  ->pHaving, 0));.
2a91a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
2a91b 61 72 65 6e 74 2d 3e 70 47 72 6f 75 70 42 79 3d  arent->pGroupBy=
2a91c 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 50 61 72  =0 );.      pPar
2a91d 65 6e 74 2d 3e 70 47 72 6f 75 70 42 79 20 3d 20  ent->pGroupBy = 
2a91e 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
2a91f 75 70 28 64 62 2c 20 70 53 75 62 2d 3e 70 47 72  up(db, pSub->pGr
2a920 6f 75 70 42 79 2c 20 30 29 3b 0a 20 20 20 20 7d  oupBy, 0);.    }
2a921 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 50 61 72  else{.      pPar
2a922 65 6e 74 2d 3e 70 57 68 65 72 65 20 3d 20 73 75  ent->pWhere = su
2a923 62 73 74 45 78 70 72 28 64 62 2c 20 70 50 61 72  bstExpr(db, pPar
2a924 65 6e 74 2d 3e 70 57 68 65 72 65 2c 20 69 50 61  ent->pWhere, iPa
2a925 72 65 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c 69  rent, pSub->pELi
2a926 73 74 29 3b 0a 20 20 20 20 20 20 70 50 61 72 65  st);.      pPare
2a927 6e 74 2d 3e 70 57 68 65 72 65 20 3d 20 73 71 6c  nt->pWhere = sql
2a928 69 74 65 33 45 78 70 72 41 6e 64 28 64 62 2c 20  ite3ExprAnd(db, 
2a929 70 50 61 72 65 6e 74 2d 3e 70 57 68 65 72 65 2c  pParent->pWhere,
2a92a 20 70 57 68 65 72 65 29 3b 0a 20 20 20 20 7d 0a   pWhere);.    }.
2a92b 20 20 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6c    .    /* The fl
2a92c 61 74 74 65 6e 65 64 20 71 75 65 72 79 20 69 73  attened query is
2a92d 20 64 69 73 74 69 6e 63 74 20 69 66 20 65 69 74   distinct if eit
2a92e 68 65 72 20 74 68 65 20 69 6e 6e 65 72 20 6f 72  her the inner or
2a92f 20 74 68 65 0a 20 20 20 20 2a 2a 20 6f 75 74 65   the.    ** oute
2a930 72 20 71 75 65 72 79 20 69 73 20 64 69 73 74 69  r query is disti
2a931 6e 63 74 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20  nct. .    */.   
2a932 20 70 50 61 72 65 6e 74 2d 3e 73 65 6c 46 6c 61   pParent->selFla
2a933 67 73 20 7c 3d 20 70 53 75 62 2d 3e 73 65 6c 46  gs |= pSub->selF
2a934 6c 61 67 73 20 26 20 53 46 5f 44 69 73 74 69 6e  lags & SF_Distin
2a935 63 74 3b 0a 20 20 0a 20 20 20 20 2f 2a 0a 20 20  ct;.  .    /*.  
2a936 20 20 2a 2a 20 53 45 4c 45 43 54 20 2e 2e 2e 20    ** SELECT ... 
2a937 46 52 4f 4d 20 28 53 45 4c 45 43 54 20 2e 2e 2e  FROM (SELECT ...
2a938 20 4c 49 4d 49 54 20 61 20 4f 46 46 53 45 54 20   LIMIT a OFFSET 
2a939 62 29 20 4c 49 4d 49 54 20 78 20 4f 46 46 53 45  b) LIMIT x OFFSE
2a93a 54 20 79 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20  T y;.    **.    
2a93b 2a 2a 20 4f 6e 65 20 69 73 20 74 65 6d 70 74 65  ** One is tempte
2a93c 64 20 74 6f 20 74 72 79 20 74 6f 20 61 64 64 20  d to try to add 
2a93d 61 20 61 6e 64 20 62 20 74 6f 20 63 6f 6d 62 69  a and b to combi
2a93e 6e 65 20 74 68 65 20 6c 69 6d 69 74 73 2e 20 20  ne the limits.  
2a93f 42 75 74 20 74 68 69 73 0a 20 20 20 20 2a 2a 20  But this.    ** 
2a940 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 69 66  does not work if
2a941 20 65 69 74 68 65 72 20 6c 69 6d 69 74 20 69 73   either limit is
2a942 20 6e 65 67 61 74 69 76 65 2e 0a 20 20 20 20 2a   negative..    *
2a943 2f 0a 20 20 20 20 69 66 28 20 70 53 75 62 2d 3e  /.    if( pSub->
2a944 70 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 20 20  pLimit ){.      
2a945 70 50 61 72 65 6e 74 2d 3e 70 4c 69 6d 69 74 20  pParent->pLimit 
2a946 3d 20 70 53 75 62 2d 3e 70 4c 69 6d 69 74 3b 0a  = pSub->pLimit;.
2a947 20 20 20 20 20 20 70 53 75 62 2d 3e 70 4c 69 6d        pSub->pLim
2a948 69 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  it = 0;.    }.  
2a949 7d 0a 0a 20 20 2f 2a 20 46 69 6e 69 61 6c 6c 79  }..  /* Finially
2a94a 2c 20 64 65 6c 65 74 65 20 77 68 61 74 20 69 73  , delete what is
2a94b 20 6c 65 66 74 20 6f 66 20 74 68 65 20 73 75 62   left of the sub
2a94c 71 75 65 72 79 20 61 6e 64 20 72 65 74 75 72 6e  query and return
2a94d 0a 20 20 2a 2a 20 73 75 63 63 65 73 73 2e 0a 20  .  ** success.. 
2a94e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 53 65 6c   */.  sqlite3Sel
2a94f 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53  ectDelete(db, pS
2a950 75 62 31 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  ub1);..  return 
2a951 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21  1;.}.#endif /* !
2a952 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
2a953 4d 49 54 5f 53 55 42 51 55 45 52 59 29 20 7c 7c  MIT_SUBQUERY) ||
2a954 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
2a955 5f 4f 4d 49 54 5f 56 49 45 57 29 20 2a 2f 0a 0a  _OMIT_VIEW) */..
2a956 2f 2a 0a 2a 2a 20 41 6e 61 6c 79 7a 65 20 74 68  /*.** Analyze th
2a957 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
2a958 6e 74 20 70 61 73 73 65 64 20 61 73 20 61 6e 20  nt passed as an 
2a959 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 65 65 20  argument to see 
2a95a 69 66 20 69 74 0a 2a 2a 20 69 73 20 61 20 6d 69  if it.** is a mi
2a95b 6e 28 29 20 6f 72 20 6d 61 78 28 29 20 71 75 65  n() or max() que
2a95c 72 79 2e 20 52 65 74 75 72 6e 20 57 48 45 52 45  ry. Return WHERE
2a95d 5f 4f 52 44 45 52 42 59 5f 4d 49 4e 20 6f 72 20  _ORDERBY_MIN or 
2a95e 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 41  WHERE_ORDERBY_MA
2a95f 58 20 69 66 20 0a 2a 2a 20 69 74 20 69 73 2c 20  X if .** it is, 
2a960 6f 72 20 30 20 6f 74 68 65 72 77 69 73 65 2e 20  or 0 otherwise. 
2a961 41 74 20 70 72 65 73 65 6e 74 2c 20 61 20 71 75  At present, a qu
2a962 65 72 79 20 69 73 20 63 6f 6e 73 69 64 65 72 65  ery is considere
2a963 64 20 74 6f 20 62 65 0a 2a 2a 20 61 20 6d 69 6e  d to be.** a min
2a964 28 29 2f 6d 61 78 28 29 20 71 75 65 72 79 20 69  ()/max() query i
2a965 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 54 68  f:.**.**   1. Th
2a966 65 72 65 20 69 73 20 61 20 73 69 6e 67 6c 65 20  ere is a single 
2a967 6f 62 6a 65 63 74 20 69 6e 20 74 68 65 20 46 52  object in the FR
2a968 4f 4d 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a  OM clause..**.**
2a969 20 20 20 32 2e 20 54 68 65 72 65 20 69 73 20 61     2. There is a
2a96a 20 73 69 6e 67 6c 65 20 65 78 70 72 65 73 73 69   single expressi
2a96b 6f 6e 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  on in the result
2a96c 20 73 65 74 2c 20 61 6e 64 20 69 74 20 69 73 0a   set, and it is.
2a96d 2a 2a 20 20 20 20 20 20 65 69 74 68 65 72 20 6d  **      either m
2a96e 69 6e 28 78 29 20 6f 72 20 6d 61 78 28 78 29 2c  in(x) or max(x),
2a96f 20 77 68 65 72 65 20 78 20 69 73 20 61 20 63 6f   where x is a co
2a970 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 2e 0a  lumn reference..
2a971 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 6d 69 6e  */.static u8 min
2a972 4d 61 78 51 75 65 72 79 28 53 65 6c 65 63 74 20  MaxQuery(Select 
2a973 2a 70 29 7b 0a 20 20 45 78 70 72 20 2a 70 45 78  *p){.  Expr *pEx
2a974 70 72 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a  pr;.  ExprList *
2a975 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69  pEList = p->pELi
2a976 73 74 3b 0a 0a 20 20 69 66 28 20 70 45 4c 69 73  st;..  if( pELis
2a977 74 2d 3e 6e 45 78 70 72 21 3d 31 20 29 20 72 65  t->nExpr!=1 ) re
2a978 74 75 72 6e 20 57 48 45 52 45 5f 4f 52 44 45 52  turn WHERE_ORDER
2a979 42 59 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 70 45 78  BY_NORMAL;.  pEx
2a97a 70 72 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 30  pr = pEList->a[0
2a97b 5d 2e 70 45 78 70 72 3b 0a 20 20 69 66 28 20 70  ].pExpr;.  if( p
2a97c 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 41 47 47  Expr->op!=TK_AGG
2a97d 5f 46 55 4e 43 54 49 4f 4e 20 29 20 72 65 74 75  _FUNCTION ) retu
2a97e 72 6e 20 30 3b 0a 20 20 69 66 28 20 4e 45 56 45  rn 0;.  if( NEVE
2a97f 52 28 45 78 70 72 48 61 73 50 72 6f 70 65 72 74  R(ExprHasPropert
2a980 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53  y(pExpr, EP_xIsS
2a981 65 6c 65 63 74 29 29 20 29 20 72 65 74 75 72 6e  elect)) ) return
2a982 20 30 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20 70   0;.  pEList = p
2a983 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20  Expr->x.pList;. 
2a984 20 69 66 28 20 70 45 4c 69 73 74 3d 3d 30 20 7c   if( pEList==0 |
2a985 7c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21  | pEList->nExpr!
2a986 3d 31 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =1 ) return 0;. 
2a987 20 69 66 28 20 70 45 4c 69 73 74 2d 3e 61 5b 30   if( pEList->a[0
2a988 5d 2e 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f  ].pExpr->op!=TK_
2a989 41 47 47 5f 43 4f 4c 55 4d 4e 20 29 20 72 65 74  AGG_COLUMN ) ret
2a98a 75 72 6e 20 57 48 45 52 45 5f 4f 52 44 45 52 42  urn WHERE_ORDERB
2a98b 59 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 61 73 73 65  Y_NORMAL;.  asse
2a98c 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70  rt( !ExprHasProp
2a98d 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49  erty(pExpr, EP_I
2a98e 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 69 66  ntValue) );.  if
2a98f 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
2a990 28 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e  (pExpr->u.zToken
2a991 2c 22 6d 69 6e 22 29 3d 3d 30 20 29 7b 0a 20 20  ,"min")==0 ){.  
2a992 20 20 72 65 74 75 72 6e 20 57 48 45 52 45 5f 4f    return WHERE_O
2a993 52 44 45 52 42 59 5f 4d 49 4e 3b 0a 20 20 7d 65  RDERBY_MIN;.  }e
2a994 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 53  lse if( sqlite3S
2a995 74 72 49 43 6d 70 28 70 45 78 70 72 2d 3e 75 2e  trICmp(pExpr->u.
2a996 7a 54 6f 6b 65 6e 2c 22 6d 61 78 22 29 3d 3d 30  zToken,"max")==0
2a997 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57   ){.    return W
2a998 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 41 58  HERE_ORDERBY_MAX
2a999 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 57  ;.  }.  return W
2a99a 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4e 4f 52  HERE_ORDERBY_NOR
2a99b 4d 41 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  MAL;.}../*.** Th
2a99c 65 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65  e select stateme
2a99d 6e 74 20 70 61 73 73 65 64 20 61 73 20 74 68 65  nt passed as the
2a99e 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
2a99f 69 73 20 61 6e 20 61 67 67 72 65 67 61 74 65 20  is an aggregate 
2a9a0 71 75 65 72 79 2e 0a 2a 2a 20 54 68 65 20 73 65  query..** The se
2a9a1 63 6f 6e 64 20 61 72 67 6d 65 6e 74 20 69 73 20  cond argment is 
2a9a2 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 61  the associated a
2a9a3 67 67 72 65 67 61 74 65 2d 69 6e 66 6f 20 6f 62  ggregate-info ob
2a9a4 6a 65 63 74 2e 20 54 68 69 73 20 0a 2a 2a 20 66  ject. This .** f
2a9a5 75 6e 63 74 69 6f 6e 20 74 65 73 74 73 20 69 66  unction tests if
2a9a6 20 74 68 65 20 53 45 4c 45 43 54 20 69 73 20 6f   the SELECT is o
2a9a7 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a  f the form:.**.*
2a9a8 2a 20 20 20 53 45 4c 45 43 54 20 63 6f 75 6e 74  *   SELECT count
2a9a9 28 2a 29 20 46 52 4f 4d 20 3c 74 62 6c 3e 0a 2a  (*) FROM <tbl>.*
2a9aa 2a 0a 2a 2a 20 77 68 65 72 65 20 74 61 62 6c 65  *.** where table
2a9ab 20 69 73 20 61 20 64 61 74 61 62 61 73 65 20 74   is a database t
2a9ac 61 62 6c 65 2c 20 6e 6f 74 20 61 20 73 75 62 2d  able, not a sub-
2a9ad 73 65 6c 65 63 74 20 6f 72 20 76 69 65 77 2e 20  select or view. 
2a9ae 49 66 20 74 68 65 20 71 75 65 72 79 0a 2a 2a 20  If the query.** 
2a9af 64 6f 65 73 20 6d 61 74 63 68 20 74 68 69 73 20  does match this 
2a9b0 70 61 74 74 65 72 6e 2c 20 74 68 65 6e 20 61 20  pattern, then a 
2a9b1 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 54  pointer to the T
2a9b2 61 62 6c 65 20 6f 62 6a 65 63 74 20 72 65 70 72  able object repr
2a9b3 65 73 65 6e 74 69 6e 67 0a 2a 2a 20 3c 74 62 6c  esenting.** <tbl
2a9b4 3e 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f  > is returned. O
2a9b5 74 68 65 72 77 69 73 65 2c 20 30 20 69 73 20 72  therwise, 0 is r
2a9b6 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74  eturned..*/.stat
2a9b7 69 63 20 54 61 62 6c 65 20 2a 69 73 53 69 6d 70  ic Table *isSimp
2a9b8 6c 65 43 6f 75 6e 74 28 53 65 6c 65 63 74 20 2a  leCount(Select *
2a9b9 70 2c 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 67  p, AggInfo *pAgg
2a9ba 49 6e 66 6f 29 7b 0a 20 20 54 61 62 6c 65 20 2a  Info){.  Table *
2a9bb 70 54 61 62 3b 0a 20 20 45 78 70 72 20 2a 70 45  pTab;.  Expr *pE
2a9bc 78 70 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  xpr;..  assert( 
2a9bd 21 70 2d 3e 70 47 72 6f 75 70 42 79 20 29 3b 0a  !p->pGroupBy );.
2a9be 0a 20 20 69 66 28 20 70 2d 3e 70 57 68 65 72 65  .  if( p->pWhere
2a9bf 20 7c 7c 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e   || p->pEList->n
2a9c0 45 78 70 72 21 3d 31 20 0a 20 20 20 7c 7c 20 70  Expr!=1 .   || p
2a9c1 2d 3e 70 53 72 63 2d 3e 6e 53 72 63 21 3d 31 20  ->pSrc->nSrc!=1 
2a9c2 7c 7c 20 70 2d 3e 70 53 72 63 2d 3e 61 5b 30 5d  || p->pSrc->a[0]
2a9c3 2e 70 53 65 6c 65 63 74 0a 20 20 29 7b 0a 20 20  .pSelect.  ){.  
2a9c4 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
2a9c5 20 20 70 54 61 62 20 3d 20 70 2d 3e 70 53 72 63    pTab = p->pSrc
2a9c6 2d 3e 61 5b 30 5d 2e 70 54 61 62 3b 0a 20 20 70  ->a[0].pTab;.  p
2a9c7 45 78 70 72 20 3d 20 70 2d 3e 70 45 4c 69 73 74  Expr = p->pEList
2a9c8 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20  ->a[0].pExpr;.  
2a9c9 61 73 73 65 72 74 28 20 70 54 61 62 20 26 26 20  assert( pTab && 
2a9ca 21 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 26  !pTab->pSelect &
2a9cb 26 20 70 45 78 70 72 20 29 3b 0a 0a 20 20 69 66  & pExpr );..  if
2a9cc 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  ( IsVirtual(pTab
2a9cd 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  ) ) return 0;.  
2a9ce 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54  if( pExpr->op!=T
2a9cf 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 29  K_AGG_FUNCTION )
2a9d0 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28   return 0;.  if(
2a9d1 20 28 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e   (pAggInfo->aFun
2a9d2 63 5b 30 5d 2e 70 46 75 6e 63 2d 3e 66 6c 61 67  c[0].pFunc->flag
2a9d3 73 26 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 4f  s&SQLITE_FUNC_CO
2a9d4 55 4e 54 29 3d 3d 30 20 29 20 72 65 74 75 72 6e  UNT)==0 ) return
2a9d5 20 30 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d   0;.  if( pExpr-
2a9d6 3e 66 6c 61 67 73 26 45 50 5f 44 69 73 74 69 6e  >flags&EP_Distin
2a9d7 63 74 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a  ct ) return 0;..
2a9d8 20 20 72 65 74 75 72 6e 20 70 54 61 62 3b 0a 7d    return pTab;.}
2a9d9 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73  ../*.** If the s
2a9da 6f 75 72 63 65 2d 6c 69 73 74 20 69 74 65 6d 20  ource-list item 
2a9db 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 67  passed as an arg
2a9dc 75 6d 65 6e 74 20 77 61 73 20 61 75 67 6d 65 6e  ument was augmen
2a9dd 74 65 64 20 77 69 74 68 20 61 6e 0a 2a 2a 20 49  ted with an.** I
2a9de 4e 44 45 58 45 44 20 42 59 20 63 6c 61 75 73 65  NDEXED BY clause
2a9df 2c 20 74 68 65 6e 20 74 72 79 20 74 6f 20 6c 6f  , then try to lo
2a9e0 63 61 74 65 20 74 68 65 20 73 70 65 63 69 66 69  cate the specifi
2a9e1 65 64 20 69 6e 64 65 78 2e 20 49 66 20 74 68 65  ed index. If the
2a9e2 72 65 0a 2a 2a 20 77 61 73 20 73 75 63 68 20 61  re.** was such a
2a9e3 20 63 6c 61 75 73 65 20 61 6e 64 20 74 68 65 20   clause and the 
2a9e4 6e 61 6d 65 64 20 69 6e 64 65 78 20 63 61 6e 6e  named index cann
2a9e5 6f 74 20 62 65 20 66 6f 75 6e 64 2c 20 72 65 74  ot be found, ret
2a9e6 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 45  urn .** SQLITE_E
2a9e7 52 52 4f 52 20 61 6e 64 20 6c 65 61 76 65 20 61  RROR and leave a
2a9e8 6e 20 65 72 72 6f 72 20 69 6e 20 70 50 61 72 73  n error in pPars
2a9e9 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 70 6f  e. Otherwise, po
2a9ea 70 75 6c 61 74 65 20 0a 2a 2a 20 70 46 72 6f 6d  pulate .** pFrom
2a9eb 2d 3e 70 49 6e 64 65 78 20 61 6e 64 20 72 65 74  ->pIndex and ret
2a9ec 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a  urn SQLITE_OK..*
2a9ed 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2a9ee 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 64 65   int sqlite3Inde
2a9ef 78 65 64 42 79 4c 6f 6f 6b 75 70 28 50 61 72 73  xedByLookup(Pars
2a9f0 65 20 2a 70 50 61 72 73 65 2c 20 73 74 72 75 63  e *pParse, struc
2a9f1 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
2a9f2 70 46 72 6f 6d 29 7b 0a 20 20 69 66 28 20 70 46  pFrom){.  if( pF
2a9f3 72 6f 6d 2d 3e 70 54 61 62 20 26 26 20 70 46 72  rom->pTab && pFr
2a9f4 6f 6d 2d 3e 7a 49 6e 64 65 78 20 29 7b 0a 20 20  om->zIndex ){.  
2a9f5 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
2a9f6 70 46 72 6f 6d 2d 3e 70 54 61 62 3b 0a 20 20 20  pFrom->pTab;.   
2a9f7 20 63 68 61 72 20 2a 7a 49 6e 64 65 78 20 3d 20   char *zIndex = 
2a9f8 70 46 72 6f 6d 2d 3e 7a 49 6e 64 65 78 3b 0a 20  pFrom->zIndex;. 
2a9f9 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a     Index *pIdx;.
2a9fa 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61      for(pIdx=pTa
2a9fb 62 2d 3e 70 49 6e 64 65 78 3b 20 0a 20 20 20 20  b->pIndex; .    
2a9fc 20 20 20 20 70 49 64 78 20 26 26 20 73 71 6c 69      pIdx && sqli
2a9fd 74 65 33 53 74 72 49 43 6d 70 28 70 49 64 78 2d  te3StrICmp(pIdx-
2a9fe 3e 7a 4e 61 6d 65 2c 20 7a 49 6e 64 65 78 29 3b  >zName, zIndex);
2a9ff 20 0a 20 20 20 20 20 20 20 20 70 49 64 78 3d 70   .        pIdx=p
2aa00 49 64 78 2d 3e 70 4e 65 78 74 0a 20 20 20 20 29  Idx->pNext.    )
2aa01 3b 0a 20 20 20 20 69 66 28 20 21 70 49 64 78 20  ;.    if( !pIdx 
2aa02 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2aa03 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
2aa04 20 22 6e 6f 20 73 75 63 68 20 69 6e 64 65 78 3a   "no such index:
2aa05 20 25 73 22 2c 20 7a 49 6e 64 65 78 2c 20 30 29   %s", zIndex, 0)
2aa06 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
2aa07 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
2aa08 20 7d 0a 20 20 20 20 70 46 72 6f 6d 2d 3e 70 49   }.    pFrom->pI
2aa09 6e 64 65 78 20 3d 20 70 49 64 78 3b 0a 20 20 7d  ndex = pIdx;.  }
2aa0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
2aa0b 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  _OK;.}../*.** Th
2aa0c 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
2aa0d 57 61 6c 6b 65 72 20 63 61 6c 6c 62 61 63 6b 20  Walker callback 
2aa0e 66 6f 72 20 22 65 78 70 61 6e 64 69 6e 67 22 20  for "expanding" 
2aa0f 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  a SELECT stateme
2aa10 6e 74 2e 0a 2a 2a 20 22 45 78 70 61 6e 64 69 6e  nt..** "Expandin
2aa11 67 22 20 6d 65 61 6e 73 20 74 6f 20 64 6f 20 74  g" means to do t
2aa12 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a  he following:.**
2aa13 0a 2a 2a 20 20 20 20 28 31 29 20 20 4d 61 6b 65  .**    (1)  Make
2aa14 20 73 75 72 65 20 56 44 42 45 20 63 75 72 73 6f   sure VDBE curso
2aa15 72 20 6e 75 6d 62 65 72 73 20 68 61 76 65 20 62  r numbers have b
2aa16 65 65 6e 20 61 73 73 69 67 6e 65 64 20 74 6f 20  een assigned to 
2aa17 65 76 65 72 79 0a 2a 2a 20 20 20 20 20 20 20 20  every.**        
2aa18 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20   element of the 
2aa19 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a  FROM clause..**.
2aa1a 2a 2a 20 20 20 20 28 32 29 20 20 46 69 6c 6c 20  **    (2)  Fill 
2aa1b 69 6e 20 74 68 65 20 70 54 61 62 4c 69 73 74 2d  in the pTabList-
2aa1c 3e 61 5b 5d 2e 70 54 61 62 20 66 69 65 6c 64 73  >a[].pTab fields
2aa1d 20 69 6e 20 74 68 65 20 53 72 63 4c 69 73 74 20   in the SrcList 
2aa1e 74 68 61 74 20 0a 2a 2a 20 20 20 20 20 20 20 20  that .**        
2aa1f 20 64 65 66 69 6e 65 73 20 46 52 4f 4d 20 63 6c   defines FROM cl
2aa20 61 75 73 65 2e 20 20 57 68 65 6e 20 76 69 65 77  ause.  When view
2aa21 73 20 61 70 70 65 61 72 20 69 6e 20 74 68 65 20  s appear in the 
2aa22 46 52 4f 4d 20 63 6c 61 75 73 65 2c 0a 2a 2a 20  FROM clause,.** 
2aa23 20 20 20 20 20 20 20 20 66 69 6c 6c 20 70 54 61          fill pTa
2aa24 62 4c 69 73 74 2d 3e 61 5b 5d 2e 70 53 65 6c 65  bList->a[].pSele
2aa25 63 74 20 77 69 74 68 20 61 20 63 6f 70 79 20 6f  ct with a copy o
2aa26 66 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61  f the SELECT sta
2aa27 74 65 6d 65 6e 74 0a 2a 2a 20 20 20 20 20 20 20  tement.**       
2aa28 20 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74    that implement
2aa29 73 20 74 68 65 20 76 69 65 77 2e 20 20 41 20 63  s the view.  A c
2aa2a 6f 70 79 20 69 73 20 6d 61 64 65 20 6f 66 20 74  opy is made of t
2aa2b 68 65 20 76 69 65 77 27 73 20 53 45 4c 45 43 54  he view's SELECT
2aa2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 73 74 61 74  .**         stat
2aa2d 65 6d 65 6e 74 20 73 6f 20 74 68 61 74 20 77 65  ement so that we
2aa2e 20 63 61 6e 20 66 72 65 65 6c 79 20 6d 6f 64 69   can freely modi
2aa2f 66 79 20 6f 72 20 64 65 6c 65 74 65 20 74 68 61  fy or delete tha
2aa30 74 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 20  t statement.**  
2aa31 20 20 20 20 20 20 20 77 69 74 68 6f 75 74 20 77         without w
2aa32 6f 72 72 79 69 6e 67 20 61 62 6f 75 74 20 6d 65  orrying about me
2aa33 73 73 69 6e 67 20 75 70 20 74 68 65 20 70 72 65  ssing up the pre
2aa34 73 69 73 74 65 6e 74 20 72 65 70 72 65 73 65 6e  sistent represen
2aa35 74 61 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20  tation.**       
2aa36 20 20 6f 66 20 74 68 65 20 76 69 65 77 2e 0a 2a    of the view..*
2aa37 2a 0a 2a 2a 20 20 20 20 28 33 29 20 20 41 64 64  *.**    (3)  Add
2aa38 20 74 65 72 6d 73 20 74 6f 20 74 68 65 20 57 48   terms to the WH
2aa39 45 52 45 20 63 6c 61 75 73 65 20 74 6f 20 61 63  ERE clause to ac
2aa3a 63 6f 6d 6f 64 61 74 65 20 74 68 65 20 4e 41 54  comodate the NAT
2aa3b 55 52 41 4c 20 6b 65 79 77 6f 72 64 0a 2a 2a 20  URAL keyword.** 
2aa3c 20 20 20 20 20 20 20 20 6f 6e 20 6a 6f 69 6e 73          on joins
2aa3d 20 61 6e 64 20 74 68 65 20 4f 4e 20 61 6e 64 20   and the ON and 
2aa3e 55 53 49 4e 47 20 63 6c 61 75 73 65 20 6f 66 20  USING clause of 
2aa3f 6a 6f 69 6e 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  joins..**.**    
2aa40 28 34 29 20 20 53 63 61 6e 20 74 68 65 20 6c 69  (4)  Scan the li
2aa41 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  st of columns in
2aa42 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
2aa43 28 70 45 4c 69 73 74 29 20 6c 6f 6f 6b 69 6e 67  (pEList) looking
2aa44 0a 2a 2a 20 20 20 20 20 20 20 20 20 66 6f 72 20  .**         for 
2aa45 69 6e 73 74 61 6e 63 65 73 20 6f 66 20 74 68 65  instances of the
2aa46 20 22 2a 22 20 6f 70 65 72 61 74 6f 72 20 6f 72   "*" operator or
2aa47 20 74 68 65 20 54 41 42 4c 45 2e 2a 20 6f 70 65   the TABLE.* ope
2aa48 72 61 74 6f 72 2e 0a 2a 2a 20 20 20 20 20 20 20  rator..**       
2aa49 20 20 49 66 20 66 6f 75 6e 64 2c 20 65 78 70 61    If found, expa
2aa4a 6e 64 20 65 61 63 68 20 22 2a 22 20 74 6f 20 62  nd each "*" to b
2aa4b 65 20 65 76 65 72 79 20 63 6f 6c 75 6d 6e 20 69  e every column i
2aa4c 6e 20 65 76 65 72 79 20 74 61 62 6c 65 0a 2a 2a  n every table.**
2aa4d 20 20 20 20 20 20 20 20 20 61 6e 64 20 54 41 42           and TAB
2aa4e 4c 45 2e 2a 20 74 6f 20 62 65 20 65 76 65 72 79  LE.* to be every
2aa4f 20 63 6f 6c 75 6d 6e 20 69 6e 20 54 41 42 4c 45   column in TABLE
2aa50 2e 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ..**.*/.static i
2aa51 6e 74 20 73 65 6c 65 63 74 45 78 70 61 6e 64 65  nt selectExpande
2aa52 72 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65  r(Walker *pWalke
2aa53 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20  r, Select *p){. 
2aa54 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 20 3d   Parse *pParse =
2aa55 20 70 57 61 6c 6b 65 72 2d 3e 70 50 61 72 73 65   pWalker->pParse
2aa56 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6b 3b  ;.  int i, j, k;
2aa57 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62  .  SrcList *pTab
2aa58 4c 69 73 74 3b 0a 20 20 45 78 70 72 4c 69 73 74  List;.  ExprList
2aa59 20 2a 70 45 4c 69 73 74 3b 0a 20 20 73 74 72 75   *pEList;.  stru
2aa5a 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
2aa5b 2a 70 46 72 6f 6d 3b 0a 20 20 73 71 6c 69 74 65  *pFrom;.  sqlite
2aa5c 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
2aa5d 64 62 3b 0a 0a 20 20 69 66 28 20 64 62 2d 3e 6d  db;..  if( db->m
2aa5e 61 6c 6c 6f 63 46 61 69 6c 65 64 20 20 29 7b 0a  allocFailed  ){.
2aa5f 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41      return WRC_A
2aa60 62 6f 72 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20  bort;.  }.  if( 
2aa61 4e 45 56 45 52 28 70 2d 3e 70 53 72 63 3d 3d 30  NEVER(p->pSrc==0
2aa62 29 20 7c 7c 20 28 70 2d 3e 73 65 6c 46 6c 61 67  ) || (p->selFlag
2aa63 73 20 26 20 53 46 5f 45 78 70 61 6e 64 65 64 29  s & SF_Expanded)
2aa64 21 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  !=0 ){.    retur
2aa65 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d  n WRC_Prune;.  }
2aa66 0a 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 7c  .  p->selFlags |
2aa67 3d 20 53 46 5f 45 78 70 61 6e 64 65 64 3b 0a 20  = SF_Expanded;. 
2aa68 20 70 54 61 62 4c 69 73 74 20 3d 20 70 2d 3e 70   pTabList = p->p
2aa69 53 72 63 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20  Src;.  pEList = 
2aa6a 70 2d 3e 70 45 4c 69 73 74 3b 0a 0a 20 20 2f 2a  p->pEList;..  /*
2aa6b 20 4d 61 6b 65 20 73 75 72 65 20 63 75 72 73 6f   Make sure curso
2aa6c 72 20 6e 75 6d 62 65 72 73 20 68 61 76 65 20 62  r numbers have b
2aa6d 65 65 6e 20 61 73 73 69 67 6e 65 64 20 74 6f 20  een assigned to 
2aa6e 61 6c 6c 20 65 6e 74 72 69 65 73 20 69 6e 0a 20  all entries in. 
2aa6f 20 2a 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   ** the FROM cla
2aa70 75 73 65 20 6f 66 20 74 68 65 20 53 45 4c 45 43  use of the SELEC
2aa71 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 2a  T statement..  *
2aa72 2f 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69  /.  sqlite3SrcLi
2aa73 73 74 41 73 73 69 67 6e 43 75 72 73 6f 72 73 28  stAssignCursors(
2aa74 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74  pParse, pTabList
2aa75 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 6b 20 75 70  );..  /* Look up
2aa76 20 65 76 65 72 79 20 74 61 62 6c 65 20 6e 61 6d   every table nam
2aa77 65 64 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  ed in the FROM c
2aa78 6c 61 75 73 65 20 6f 66 20 74 68 65 20 73 65 6c  lause of the sel
2aa79 65 63 74 2e 20 20 49 66 0a 20 20 2a 2a 20 61 6e  ect.  If.  ** an
2aa7a 20 65 6e 74 72 79 20 6f 66 20 74 68 65 20 46 52   entry of the FR
2aa7b 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 61 20 73  OM clause is a s
2aa7c 75 62 71 75 65 72 79 20 69 6e 73 74 65 61 64 20  ubquery instead 
2aa7d 6f 66 20 61 20 74 61 62 6c 65 20 6f 72 20 76 69  of a table or vi
2aa7e 65 77 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 63 72  ew,.  ** then cr
2aa7f 65 61 74 65 20 61 20 74 72 61 6e 73 69 65 6e 74  eate a transient
2aa80 20 74 61 62 6c 65 20 73 74 72 75 63 74 75 72 65   table structure
2aa81 20 74 6f 20 64 65 73 63 72 69 62 65 20 74 68 65   to describe the
2aa82 20 73 75 62 71 75 65 72 79 2e 0a 20 20 2a 2f 0a   subquery..  */.
2aa83 20 20 66 6f 72 28 69 3d 30 2c 20 70 46 72 6f 6d    for(i=0, pFrom
2aa84 3d 70 54 61 62 4c 69 73 74 2d 3e 61 3b 20 69 3c  =pTabList->a; i<
2aa85 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20  pTabList->nSrc; 
2aa86 69 2b 2b 2c 20 70 46 72 6f 6d 2b 2b 29 7b 0a 20  i++, pFrom++){. 
2aa87 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a     Table *pTab;.
2aa88 20 20 20 20 69 66 28 20 70 46 72 6f 6d 2d 3e 70      if( pFrom->p
2aa89 54 61 62 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  Tab!=0 ){.      
2aa8a 2f 2a 20 54 68 69 73 20 73 74 61 74 65 6d 65 6e  /* This statemen
2aa8b 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  t has already be
2aa8c 65 6e 20 70 72 65 70 61 72 65 64 2e 20 20 54 68  en prepared.  Th
2aa8d 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 0a 20  ere is no need. 
2aa8e 20 20 20 20 20 2a 2a 20 74 6f 20 67 6f 20 66 75       ** to go fu
2aa8f 72 74 68 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20  rther. */.      
2aa90 61 73 73 65 72 74 28 20 69 3d 3d 30 20 29 3b 0a  assert( i==0 );.
2aa91 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43        return WRC
2aa92 5f 50 72 75 6e 65 3b 0a 20 20 20 20 7d 0a 20 20  _Prune;.    }.  
2aa93 20 20 69 66 28 20 70 46 72 6f 6d 2d 3e 7a 4e 61    if( pFrom->zNa
2aa94 6d 65 3d 3d 30 20 29 7b 0a 23 69 66 6e 64 65 66  me==0 ){.#ifndef
2aa95 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42   SQLITE_OMIT_SUB
2aa96 51 55 45 52 59 0a 20 20 20 20 20 20 53 65 6c 65  QUERY.      Sele
2aa97 63 74 20 2a 70 53 65 6c 20 3d 20 70 46 72 6f 6d  ct *pSel = pFrom
2aa98 2d 3e 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 20  ->pSelect;.     
2aa99 20 2f 2a 20 41 20 73 75 62 2d 71 75 65 72 79 20   /* A sub-query 
2aa9a 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  in the FROM clau
2aa9b 73 65 20 6f 66 20 61 20 53 45 4c 45 43 54 20 2a  se of a SELECT *
2aa9c 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  /.      assert( 
2aa9d 70 53 65 6c 21 3d 30 20 29 3b 0a 20 20 20 20 20  pSel!=0 );.     
2aa9e 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e   assert( pFrom->
2aa9f 70 54 61 62 3d 3d 30 20 29 3b 0a 20 20 20 20 20  pTab==0 );.     
2aaa0 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65   sqlite3WalkSele
2aaa1 63 74 28 70 57 61 6c 6b 65 72 2c 20 70 53 65 6c  ct(pWalker, pSel
2aaa2 29 3b 0a 20 20 20 20 20 20 70 46 72 6f 6d 2d 3e  );.      pFrom->
2aaa3 70 54 61 62 20 3d 20 70 54 61 62 20 3d 20 73 71  pTab = pTab = sq
2aaa4 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
2aaa5 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 61 62  o(db, sizeof(Tab
2aaa6 6c 65 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20  le));.      if( 
2aaa7 70 54 61 62 3d 3d 30 20 29 20 72 65 74 75 72 6e  pTab==0 ) return
2aaa8 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20   WRC_Abort;.    
2aaa9 20 20 70 54 61 62 2d 3e 64 62 4d 65 6d 20 3d 20    pTab->dbMem = 
2aaaa 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45  db->lookaside.bE
2aaab 6e 61 62 6c 65 64 20 3f 20 64 62 20 3a 20 30 3b  nabled ? db : 0;
2aaac 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 6e 52 65  .      pTab->nRe
2aaad 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 54 61  f = 1;.      pTa
2aaae 62 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74  b->zName = sqlit
2aaaf 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 73  e3MPrintf(db, "s
2aab0 71 6c 69 74 65 5f 73 75 62 71 75 65 72 79 5f 25  qlite_subquery_%
2aab1 70 5f 22 2c 20 28 76 6f 69 64 2a 29 70 54 61 62  p_", (void*)pTab
2aab2 29 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  );.      while( 
2aab3 70 53 65 6c 2d 3e 70 50 72 69 6f 72 20 29 7b 20  pSel->pPrior ){ 
2aab4 70 53 65 6c 20 3d 20 70 53 65 6c 2d 3e 70 50 72  pSel = pSel->pPr
2aab5 69 6f 72 3b 20 7d 0a 20 20 20 20 20 20 73 65 6c  ior; }.      sel
2aab6 65 63 74 43 6f 6c 75 6d 6e 73 46 72 6f 6d 45 78  ectColumnsFromEx
2aab7 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70  prList(pParse, p
2aab8 53 65 6c 2d 3e 70 45 4c 69 73 74 2c 20 26 70 54  Sel->pEList, &pT
2aab9 61 62 2d 3e 6e 43 6f 6c 2c 20 26 70 54 61 62 2d  ab->nCol, &pTab-
2aaba 3e 61 43 6f 6c 29 3b 0a 20 20 20 20 20 20 70 54  >aCol);.      pT
2aabb 61 62 2d 3e 69 50 4b 65 79 20 3d 20 2d 31 3b 0a  ab->iPKey = -1;.
2aabc 20 20 20 20 20 20 70 54 61 62 2d 3e 74 61 62 46        pTab->tabF
2aabd 6c 61 67 73 20 7c 3d 20 54 46 5f 45 70 68 65 6d  lags |= TF_Ephem
2aabe 65 72 61 6c 3b 0a 23 65 6e 64 69 66 0a 20 20 20  eral;.#endif.   
2aabf 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a   }else{.      /*
2aac0 20 41 6e 20 6f 72 64 69 6e 61 72 79 20 74 61 62   An ordinary tab
2aac1 6c 65 20 6f 72 20 76 69 65 77 20 6e 61 6d 65 20  le or view name 
2aac2 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  in the FROM clau
2aac3 73 65 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65  se */.      asse
2aac4 72 74 28 20 70 46 72 6f 6d 2d 3e 70 54 61 62 3d  rt( pFrom->pTab=
2aac5 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 46 72 6f  =0 );.      pFro
2aac6 6d 2d 3e 70 54 61 62 20 3d 20 70 54 61 62 20 3d  m->pTab = pTab =
2aac7 20 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65   .        sqlite
2aac8 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61  3LocateTable(pPa
2aac9 72 73 65 2c 30 2c 70 46 72 6f 6d 2d 3e 7a 4e 61  rse,0,pFrom->zNa
2aaca 6d 65 2c 70 46 72 6f 6d 2d 3e 7a 44 61 74 61 62  me,pFrom->zDatab
2aacb 61 73 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ase);.      if( 
2aacc 70 54 61 62 3d 3d 30 20 29 20 72 65 74 75 72 6e  pTab==0 ) return
2aacd 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20   WRC_Abort;.    
2aace 20 20 70 54 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a    pTab->nRef++;.
2aacf 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
2aad0 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c  ITE_OMIT_VIEW) |
2aad1 7c 20 21 64 65 66 69 6e 65 64 20 28 53 51 4c 49  | !defined (SQLI
2aad2 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
2aad3 41 42 4c 45 29 0a 20 20 20 20 20 20 69 66 28 20  ABLE).      if( 
2aad4 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 7c 7c  pTab->pSelect ||
2aad5 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
2aad6 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 57   ){.        /* W
2aad7 65 20 72 65 61 63 68 20 68 65 72 65 20 69 66 20  e reach here if 
2aad8 74 68 65 20 6e 61 6d 65 64 20 74 61 62 6c 65 20  the named table 
2aad9 69 73 20 61 20 72 65 61 6c 6c 79 20 61 20 76 69  is a really a vi
2aada 65 77 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66  ew */.        if
2aadb 28 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74  ( sqlite3ViewGet
2aadc 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72  ColumnNames(pPar
2aadd 73 65 2c 20 70 54 61 62 29 20 29 20 72 65 74 75  se, pTab) ) retu
2aade 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20  rn WRC_Abort;.  
2aadf 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 46        assert( pF
2aae0 72 6f 6d 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20  rom->pSelect==0 
2aae1 29 3b 0a 20 20 20 20 20 20 20 20 70 46 72 6f 6d  );.        pFrom
2aae2 2d 3e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69  ->pSelect = sqli
2aae3 74 65 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c  te3SelectDup(db,
2aae4 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 2c 20   pTab->pSelect, 
2aae5 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  0);.        sqli
2aae6 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 70 57  te3WalkSelect(pW
2aae7 61 6c 6b 65 72 2c 20 70 46 72 6f 6d 2d 3e 70 53  alker, pFrom->pS
2aae8 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 7d 0a  elect);.      }.
2aae9 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 0a 20 20  #endif.    }..  
2aaea 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20    /* Locate the 
2aaeb 69 6e 64 65 78 20 6e 61 6d 65 64 20 62 79 20 74  index named by t
2aaec 68 65 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c  he INDEXED BY cl
2aaed 61 75 73 65 2c 20 69 66 20 61 6e 79 2e 20 2a 2f  ause, if any. */
2aaee 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
2aaef 49 6e 64 65 78 65 64 42 79 4c 6f 6f 6b 75 70 28  IndexedByLookup(
2aaf0 70 50 61 72 73 65 2c 20 70 46 72 6f 6d 29 20 29  pParse, pFrom) )
2aaf1 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 57  {.      return W
2aaf2 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a  RC_Abort;.    }.
2aaf3 20 20 7d 0a 0a 20 20 2f 2a 20 50 72 6f 63 65 73    }..  /* Proces
2aaf4 73 20 4e 41 54 55 52 41 4c 20 6b 65 79 77 6f 72  s NATURAL keywor
2aaf5 64 73 2c 20 61 6e 64 20 4f 4e 20 61 6e 64 20 55  ds, and ON and U
2aaf6 53 49 4e 47 20 63 6c 61 75 73 65 73 20 6f 66 20  SING clauses of 
2aaf7 6a 6f 69 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66  joins..  */.  if
2aaf8 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
2aaf9 65 64 20 7c 7c 20 73 71 6c 69 74 65 50 72 6f 63  ed || sqliteProc
2aafa 65 73 73 4a 6f 69 6e 28 70 50 61 72 73 65 2c 20  essJoin(pParse, 
2aafb 70 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  p) ){.    return
2aafc 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a   WRC_Abort;.  }.
2aafd 0a 20 20 2f 2a 20 46 6f 72 20 65 76 65 72 79 20  .  /* For every 
2aafe 22 2a 22 20 74 68 61 74 20 6f 63 63 75 72 73 20  "*" that occurs 
2aaff 69 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6c 69  in the column li
2ab00 73 74 2c 20 69 6e 73 65 72 74 20 74 68 65 20 6e  st, insert the n
2ab01 61 6d 65 73 20 6f 66 0a 20 20 2a 2a 20 61 6c 6c  ames of.  ** all
2ab02 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 6c 6c 20   columns in all 
2ab03 74 61 62 6c 65 73 2e 20 20 41 6e 64 20 66 6f 72  tables.  And for
2ab04 20 65 76 65 72 79 20 54 41 42 4c 45 2e 2a 20 69   every TABLE.* i
2ab05 6e 73 65 72 74 20 74 68 65 20 6e 61 6d 65 73 0a  nsert the names.
2ab06 20 20 2a 2a 20 6f 66 20 61 6c 6c 20 63 6f 6c 75    ** of all colu
2ab07 6d 6e 73 20 69 6e 20 54 41 42 4c 45 2e 20 20 54  mns in TABLE.  T
2ab08 68 65 20 70 61 72 73 65 72 20 69 6e 73 65 72 74  he parser insert
2ab09 65 64 20 61 20 73 70 65 63 69 61 6c 20 65 78 70  ed a special exp
2ab0a 72 65 73 73 69 6f 6e 0a 20 20 2a 2a 20 77 69 74  ression.  ** wit
2ab0b 68 20 74 68 65 20 54 4b 5f 41 4c 4c 20 6f 70 65  h the TK_ALL ope
2ab0c 72 61 74 6f 72 20 66 6f 72 20 65 61 63 68 20 22  rator for each "
2ab0d 2a 22 20 74 68 61 74 20 69 74 20 66 6f 75 6e 64  *" that it found
2ab0e 20 69 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6c   in the column l
2ab0f 69 73 74 2e 0a 20 20 2a 2a 20 54 68 65 20 66 6f  ist..  ** The fo
2ab10 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 6a 75 73  llowing code jus
2ab11 74 20 68 61 73 20 74 6f 20 6c 6f 63 61 74 65 20  t has to locate 
2ab12 74 68 65 20 54 4b 5f 41 4c 4c 20 65 78 70 72 65  the TK_ALL expre
2ab13 73 73 69 6f 6e 73 20 61 6e 64 20 65 78 70 61 6e  ssions and expan
2ab14 64 0a 20 20 2a 2a 20 65 61 63 68 20 6f 6e 65 20  d.  ** each one 
2ab15 74 6f 20 74 68 65 20 6c 69 73 74 20 6f 66 20 61  to the list of a
2ab16 6c 6c 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 6c  ll columns in al
2ab17 6c 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2a 0a 20  l tables..  **. 
2ab18 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20 6c 6f   ** The first lo
2ab19 6f 70 20 6a 75 73 74 20 63 68 65 63 6b 73 20 74  op just checks t
2ab1a 6f 20 73 65 65 20 69 66 20 74 68 65 72 65 20 61  o see if there a
2ab1b 72 65 20 61 6e 79 20 22 2a 22 20 6f 70 65 72 61  re any "*" opera
2ab1c 74 6f 72 73 0a 20 20 2a 2a 20 74 68 61 74 20 6e  tors.  ** that n
2ab1d 65 65 64 20 65 78 70 61 6e 64 69 6e 67 2e 0a 20  eed expanding.. 
2ab1e 20 2a 2f 0a 20 20 66 6f 72 28 6b 3d 30 3b 20 6b   */.  for(k=0; k
2ab1f 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20  <pEList->nExpr; 
2ab20 6b 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a  k++){.    Expr *
2ab21 70 45 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 6b  pE = pEList->a[k
2ab22 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 69 66 28  ].pExpr;.    if(
2ab23 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 41 4c 4c 20   pE->op==TK_ALL 
2ab24 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 61 73 73  ) break;.    ass
2ab25 65 72 74 28 20 70 45 2d 3e 6f 70 21 3d 54 4b 5f  ert( pE->op!=TK_
2ab26 44 4f 54 20 7c 7c 20 70 45 2d 3e 70 52 69 67 68  DOT || pE->pRigh
2ab27 74 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65  t!=0 );.    asse
2ab28 72 74 28 20 70 45 2d 3e 6f 70 21 3d 54 4b 5f 44  rt( pE->op!=TK_D
2ab29 4f 54 20 7c 7c 20 28 70 45 2d 3e 70 4c 65 66 74  OT || (pE->pLeft
2ab2a 21 3d 30 20 26 26 20 70 45 2d 3e 70 4c 65 66 74  !=0 && pE->pLeft
2ab2b 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 29 20 29 3b 0a  ->op==TK_ID) );.
2ab2c 20 20 20 20 69 66 28 20 70 45 2d 3e 6f 70 3d 3d      if( pE->op==
2ab2d 54 4b 5f 44 4f 54 20 26 26 20 70 45 2d 3e 70 52  TK_DOT && pE->pR
2ab2e 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 41 4c 4c  ight->op==TK_ALL
2ab2f 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20   ) break;.  }.  
2ab30 69 66 28 20 6b 3c 70 45 4c 69 73 74 2d 3e 6e 45  if( k<pEList->nE
2ab31 78 70 72 20 29 7b 0a 20 20 20 20 2f 2a 0a 20 20  xpr ){.    /*.  
2ab32 20 20 2a 2a 20 49 66 20 77 65 20 67 65 74 20 68    ** If we get h
2ab33 65 72 65 20 69 74 20 6d 65 61 6e 73 20 74 68 65  ere it means the
2ab34 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f 6e 74   result set cont
2ab35 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  ains one or more
2ab36 20 22 2a 22 0a 20 20 20 20 2a 2a 20 6f 70 65 72   "*".    ** oper
2ab37 61 74 6f 72 73 20 74 68 61 74 20 6e 65 65 64 20  ators that need 
2ab38 74 6f 20 62 65 20 65 78 70 61 6e 64 65 64 2e 20  to be expanded. 
2ab39 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 65 61   Loop through ea
2ab3a 63 68 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 20  ch expression.  
2ab3b 20 20 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 75    ** in the resu
2ab3c 6c 74 20 73 65 74 20 61 6e 64 20 65 78 70 61 6e  lt set and expan
2ab3d 64 20 74 68 65 6d 20 6f 6e 65 20 62 79 20 6f 6e  d them one by on
2ab3e 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 74  e..    */.    st
2ab3f 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74  ruct ExprList_it
2ab40 65 6d 20 2a 61 20 3d 20 70 45 4c 69 73 74 2d 3e  em *a = pEList->
2ab41 61 3b 0a 20 20 20 20 45 78 70 72 4c 69 73 74 20  a;.    ExprList 
2ab42 2a 70 4e 65 77 20 3d 20 30 3b 0a 20 20 20 20 69  *pNew = 0;.    i
2ab43 6e 74 20 66 6c 61 67 73 20 3d 20 70 50 61 72 73  nt flags = pPars
2ab44 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 3b 0a 20 20  e->db->flags;.  
2ab45 20 20 69 6e 74 20 6c 6f 6e 67 4e 61 6d 65 73 20    int longNames 
2ab46 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54  = (flags & SQLIT
2ab47 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73 29 21  E_FullColNames)!
2ab48 3d 30 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  =0.             
2ab49 20 20 20 20 20 20 20 20 20 26 26 20 28 66 6c 61           && (fla
2ab4a 67 73 20 26 20 53 51 4c 49 54 45 5f 53 68 6f 72  gs & SQLITE_Shor
2ab4b 74 43 6f 6c 4e 61 6d 65 73 29 3d 3d 30 3b 0a 0a  tColNames)==0;..
2ab4c 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70      for(k=0; k<p
2ab4d 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 6b 2b  EList->nExpr; k+
2ab4e 2b 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a  +){.      Expr *
2ab4f 70 45 20 3d 20 61 5b 6b 5d 2e 70 45 78 70 72 3b  pE = a[k].pExpr;
2ab50 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
2ab51 45 2d 3e 6f 70 21 3d 54 4b 5f 44 4f 54 20 7c 7c  E->op!=TK_DOT ||
2ab52 20 70 45 2d 3e 70 52 69 67 68 74 21 3d 30 20 29   pE->pRight!=0 )
2ab53 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45 2d 3e  ;.      if( pE->
2ab54 6f 70 21 3d 54 4b 5f 41 4c 4c 20 26 26 20 28 70  op!=TK_ALL && (p
2ab55 45 2d 3e 6f 70 21 3d 54 4b 5f 44 4f 54 20 7c 7c  E->op!=TK_DOT ||
2ab56 20 70 45 2d 3e 70 52 69 67 68 74 2d 3e 6f 70 21   pE->pRight->op!
2ab57 3d 54 4b 5f 41 4c 4c 29 20 29 7b 0a 20 20 20 20  =TK_ALL) ){.    
2ab58 20 20 20 20 2f 2a 20 54 68 69 73 20 70 61 72 74      /* This part
2ab59 69 63 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f  icular expressio
2ab5a 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20  n does not need 
2ab5b 74 6f 20 62 65 20 65 78 70 61 6e 64 65 64 2e 0a  to be expanded..
2ab5c 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
2ab5d 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65     pNew = sqlite
2ab5e 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
2ab5f 70 50 61 72 73 65 2c 20 70 4e 65 77 2c 20 61 5b  pParse, pNew, a[
2ab60 6b 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 20  k].pExpr);.     
2ab61 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20     if( pNew ){. 
2ab62 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 61           pNew->a
2ab63 5b 70 4e 65 77 2d 3e 6e 45 78 70 72 2d 31 5d 2e  [pNew->nExpr-1].
2ab64 7a 4e 61 6d 65 20 3d 20 61 5b 6b 5d 2e 7a 4e 61  zName = a[k].zNa
2ab65 6d 65 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4e  me;.          pN
2ab66 65 77 2d 3e 61 5b 70 4e 65 77 2d 3e 6e 45 78 70  ew->a[pNew->nExp
2ab67 72 2d 31 5d 2e 7a 53 70 61 6e 20 3d 20 61 5b 6b  r-1].zSpan = a[k
2ab68 5d 2e 7a 53 70 61 6e 3b 0a 20 20 20 20 20 20 20  ].zSpan;.       
2ab69 20 20 20 61 5b 6b 5d 2e 7a 4e 61 6d 65 20 3d 20     a[k].zName = 
2ab6a 30 3b 0a 20 20 20 20 20 20 20 20 20 20 61 5b 6b  0;.          a[k
2ab6b 5d 2e 7a 53 70 61 6e 20 3d 20 30 3b 0a 20 20 20  ].zSpan = 0;.   
2ab6c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61       }.        a
2ab6d 5b 6b 5d 2e 70 45 78 70 72 20 3d 20 30 3b 0a 20  [k].pExpr = 0;. 
2ab6e 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2ab6f 20 20 20 20 2f 2a 20 54 68 69 73 20 65 78 70 72      /* This expr
2ab70 65 73 73 69 6f 6e 20 69 73 20 61 20 22 2a 22 20  ession is a "*" 
2ab71 6f 72 20 61 20 22 54 41 42 4c 45 2e 2a 22 20 61  or a "TABLE.*" a
2ab72 6e 64 20 6e 65 65 64 73 20 74 6f 20 62 65 0a 20  nd needs to be. 
2ab73 20 20 20 20 20 20 20 2a 2a 20 65 78 70 61 6e 64         ** expand
2ab74 65 64 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69  ed. */.        i
2ab75 6e 74 20 74 61 62 6c 65 53 65 65 6e 20 3d 20 30  nt tableSeen = 0
2ab76 3b 20 20 20 20 20 20 2f 2a 20 53 65 74 20 74 6f  ;      /* Set to
2ab77 20 31 20 77 68 65 6e 20 54 41 42 4c 45 20 6d 61   1 when TABLE ma
2ab78 74 63 68 65 73 20 2a 2f 0a 20 20 20 20 20 20 20  tches */.       
2ab79 20 63 68 61 72 20 2a 7a 54 4e 61 6d 65 3b 20 20   char *zTName;  
2ab7a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 65 78            /* tex
2ab7b 74 20 6f 66 20 6e 61 6d 65 20 6f 66 20 54 41 42  t of name of TAB
2ab7c 4c 45 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66  LE */.        if
2ab7d 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54  ( pE->op==TK_DOT
2ab7e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73   ){.          as
2ab7f 73 65 72 74 28 20 70 45 2d 3e 70 4c 65 66 74 21  sert( pE->pLeft!
2ab80 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  =0 );.          
2ab81 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73  assert( !ExprHas
2ab82 50 72 6f 70 65 72 74 79 28 70 45 2d 3e 70 4c 65  Property(pE->pLe
2ab83 66 74 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29  ft, EP_IntValue)
2ab84 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 54   );.          zT
2ab85 4e 61 6d 65 20 3d 20 70 45 2d 3e 70 4c 65 66 74  Name = pE->pLeft
2ab86 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20  ->u.zToken;.    
2ab87 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2ab88 20 20 20 20 20 7a 54 4e 61 6d 65 20 3d 20 30 3b       zTName = 0;
2ab89 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2ab8a 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 46 72 6f     for(i=0, pFro
2ab8b 6d 3d 70 54 61 62 4c 69 73 74 2d 3e 61 3b 20 69  m=pTabList->a; i
2ab8c 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b  <pTabList->nSrc;
2ab8d 20 69 2b 2b 2c 20 70 46 72 6f 6d 2b 2b 29 7b 0a   i++, pFrom++){.
2ab8e 20 20 20 20 20 20 20 20 20 20 54 61 62 6c 65 20            Table 
2ab8f 2a 70 54 61 62 20 3d 20 70 46 72 6f 6d 2d 3e 70  *pTab = pFrom->p
2ab90 54 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20 63  Tab;.          c
2ab91 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65 20 3d 20  har *zTabName = 
2ab92 70 46 72 6f 6d 2d 3e 7a 41 6c 69 61 73 3b 0a 20  pFrom->zAlias;. 
2ab93 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 54 61           if( zTa
2ab94 62 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20  bName==0 ){.    
2ab95 20 20 20 20 20 20 20 20 7a 54 61 62 4e 61 6d 65          zTabName
2ab96 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a   = pTab->zName;.
2ab97 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
2ab98 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61        if( db->ma
2ab99 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 62 72 65  llocFailed ) bre
2ab9a 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  ak;.          if
2ab9b 28 20 7a 54 4e 61 6d 65 20 26 26 20 73 71 6c 69  ( zTName && sqli
2ab9c 74 65 33 53 74 72 49 43 6d 70 28 7a 54 4e 61 6d  te3StrICmp(zTNam
2ab9d 65 2c 20 7a 54 61 62 4e 61 6d 65 29 21 3d 30 20  e, zTabName)!=0 
2ab9e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63  ){.            c
2ab9f 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20  ontinue;.       
2aba0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 74     }.          t
2aba1 61 62 6c 65 53 65 65 6e 20 3d 20 31 3b 0a 20 20  ableSeen = 1;.  
2aba2 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b          for(j=0;
2aba3 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a   j<pTab->nCol; j
2aba4 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ++){.           
2aba5 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 2a 70   Expr *pExpr, *p
2aba6 52 69 67 68 74 3b 0a 20 20 20 20 20 20 20 20 20  Right;.         
2aba7 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d     char *zName =
2aba8 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a   pTab->aCol[j].z
2aba9 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 20 20  Name;.          
2abaa 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6e 61 6d 65    char *zColname
2abab 3b 20 20 2f 2a 20 54 68 65 20 63 6f 6d 70 75 74  ;  /* The comput
2abac 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a  ed column name *
2abad 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 68  /.            ch
2abae 61 72 20 2a 7a 54 6f 46 72 65 65 3b 20 20 20 2f  ar *zToFree;   /
2abaf 2a 20 4d 61 6c 6c 6f 63 65 64 20 73 74 72 69 6e  * Malloced strin
2abb0 67 20 74 68 61 74 20 6e 65 65 64 73 20 74 6f 20  g that needs to 
2abb1 62 65 20 66 72 65 65 64 20 2a 2f 0a 20 20 20 20  be freed */.    
2abb2 20 20 20 20 20 20 20 20 54 6f 6b 65 6e 20 73 43          Token sC
2abb3 6f 6c 6e 61 6d 65 3b 20 20 2f 2a 20 43 6f 6d 70  olname;  /* Comp
2abb4 75 74 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65  uted column name
2abb5 20 61 73 20 61 20 74 6f 6b 65 6e 20 2a 2f 0a 0a   as a token */..
2abb6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
2abb7 66 20 61 20 63 6f 6c 75 6d 6e 20 69 73 20 6d 61  f a column is ma
2abb8 72 6b 65 64 20 61 73 20 27 68 69 64 64 65 6e 27  rked as 'hidden'
2abb9 20 28 63 75 72 72 65 6e 74 6c 79 20 6f 6e 6c 79   (currently only
2abba 20 70 6f 73 73 69 62 6c 65 0a 20 20 20 20 20 20   possible.      
2abbb 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 76 69 72        ** for vir
2abbc 74 75 61 6c 20 74 61 62 6c 65 73 29 2c 20 64 6f  tual tables), do
2abbd 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 69 74 20   not include it 
2abbe 69 6e 20 74 68 65 20 65 78 70 61 6e 64 65 64 0a  in the expanded.
2abbf 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 72              ** r
2abc0 65 73 75 6c 74 2d 73 65 74 20 6c 69 73 74 2e 0a  esult-set list..
2abc1 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20              */. 
2abc2 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 49             if( I
2abc3 73 48 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 26 70  sHiddenColumn(&p
2abc4 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 29 20 29 7b  Tab->aCol[j]) ){
2abc5 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61  .              a
2abc6 73 73 65 72 74 28 49 73 56 69 72 74 75 61 6c 28  ssert(IsVirtual(
2abc7 70 54 61 62 29 29 3b 0a 20 20 20 20 20 20 20 20  pTab));.        
2abc8 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a        continue;.
2abc9 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 0a 20              }.. 
2abca 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69             if( i
2abcb 3e 30 20 26 26 20 7a 54 4e 61 6d 65 3d 3d 30 20  >0 && zTName==0 
2abcc 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
2abcd 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
2abce 69 74 65 6d 20 2a 70 4c 65 66 74 20 3d 20 26 70  item *pLeft = &p
2abcf 54 61 62 4c 69 73 74 2d 3e 61 5b 69 2d 31 5d 3b  TabList->a[i-1];
2abd0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69  .              i
2abd1 66 28 20 28 70 4c 65 66 74 5b 31 5d 2e 6a 6f 69  f( (pLeft[1].joi
2abd2 6e 74 79 70 65 20 26 20 4a 54 5f 4e 41 54 55 52  ntype & JT_NATUR
2abd3 41 4c 29 21 3d 30 20 26 26 0a 20 20 20 20 20 20  AL)!=0 &&.      
2abd4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2abd5 20 20 63 6f 6c 75 6d 6e 49 6e 64 65 78 28 70 4c    columnIndex(pL
2abd6 65 66 74 2d 3e 70 54 61 62 2c 20 7a 4e 61 6d 65  eft->pTab, zName
2abd7 29 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )>=0 ){.        
2abd8 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 61 20          /* In a 
2abd9 4e 41 54 55 52 41 4c 20 6a 6f 69 6e 2c 20 6f 6d  NATURAL join, om
2abda 69 74 20 74 68 65 20 6a 6f 69 6e 20 63 6f 6c 75  it the join colu
2abdb 6d 6e 73 20 66 72 6f 6d 20 74 68 65 20 0a 20 20  mns from the .  
2abdc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a                **
2abdd 20 74 61 62 6c 65 20 6f 6e 20 74 68 65 20 72 69   table on the ri
2abde 67 68 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  ght */.         
2abdf 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b         continue;
2abe0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d  .              }
2abe1 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69  .              i
2abe2 66 28 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74  f( sqlite3IdList
2abe3 49 6e 64 65 78 28 70 4c 65 66 74 5b 31 5d 2e 70  Index(pLeft[1].p
2abe4 55 73 69 6e 67 2c 20 7a 4e 61 6d 65 29 3e 3d 30  Using, zName)>=0
2abe5 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
2abe6 20 20 20 20 2f 2a 20 49 6e 20 61 20 6a 6f 69 6e      /* In a join
2abe7 20 77 69 74 68 20 61 20 55 53 49 4e 47 20 63 6c   with a USING cl
2abe8 61 75 73 65 2c 20 6f 6d 69 74 20 63 6f 6c 75 6d  ause, omit colum
2abe9 6e 73 20 69 6e 20 74 68 65 0a 20 20 20 20 20 20  ns in the.      
2abea 20 20 20 20 20 20 20 20 20 20 2a 2a 20 75 73 69            ** usi
2abeb 6e 67 20 63 6c 61 75 73 65 20 66 72 6f 6d 20 74  ng clause from t
2abec 68 65 20 74 61 62 6c 65 20 6f 6e 20 74 68 65 20  he table on the 
2abed 72 69 67 68 74 2e 20 2a 2f 0a 20 20 20 20 20 20  right. */.      
2abee 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e            contin
2abef 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ue;.            
2abf0 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
2abf1 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 52  }.            pR
2abf2 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33 45 78  ight = sqlite3Ex
2abf3 70 72 28 64 62 2c 20 54 4b 5f 49 44 2c 20 7a 4e  pr(db, TK_ID, zN
2abf4 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ame);.          
2abf5 20 20 7a 43 6f 6c 6e 61 6d 65 20 3d 20 7a 4e 61    zColname = zNa
2abf6 6d 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  me;.            
2abf7 7a 54 6f 46 72 65 65 20 3d 20 30 3b 0a 20 20 20  zToFree = 0;.   
2abf8 20 20 20 20 20 20 20 20 20 69 66 28 20 6c 6f 6e           if( lon
2abf9 67 4e 61 6d 65 73 20 7c 7c 20 70 54 61 62 4c 69  gNames || pTabLi
2abfa 73 74 2d 3e 6e 53 72 63 3e 31 20 29 7b 0a 20 20  st->nSrc>1 ){.  
2abfb 20 20 20 20 20 20 20 20 20 20 20 20 45 78 70 72              Expr
2abfc 20 2a 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 20   *pLeft;.       
2abfd 20 20 20 20 20 20 20 70 4c 65 66 74 20 3d 20 73         pLeft = s
2abfe 71 6c 69 74 65 33 45 78 70 72 28 64 62 2c 20 54  qlite3Expr(db, T
2abff 4b 5f 49 44 2c 20 7a 54 61 62 4e 61 6d 65 29 3b  K_ID, zTabName);
2ac00 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
2ac01 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45  Expr = sqlite3PE
2ac02 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44  xpr(pParse, TK_D
2ac03 4f 54 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68  OT, pLeft, pRigh
2ac04 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20  t, 0);.         
2ac05 20 20 20 20 20 69 66 28 20 6c 6f 6e 67 4e 61 6d       if( longNam
2ac06 65 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  es ){.          
2ac07 20 20 20 20 20 20 7a 43 6f 6c 6e 61 6d 65 20 3d        zColname =
2ac08 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
2ac09 64 62 2c 20 22 25 73 2e 25 73 22 2c 20 7a 54 61  db, "%s.%s", zTa
2ac0a 62 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 3b 0a 20  bName, zName);. 
2ac0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a                 z
2ac0c 54 6f 46 72 65 65 20 3d 20 7a 43 6f 6c 6e 61 6d  ToFree = zColnam
2ac0d 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e;.             
2ac0e 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d   }.            }
2ac0f 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
2ac10 20 20 20 20 70 45 78 70 72 20 3d 20 70 52 69 67      pExpr = pRig
2ac11 68 74 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ht;.            
2ac12 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 4e  }.            pN
2ac13 65 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ew = sqlite3Expr
2ac14 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
2ac15 65 2c 20 70 4e 65 77 2c 20 70 45 78 70 72 29 3b  e, pNew, pExpr);
2ac16 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 43 6f  .            sCo
2ac17 6c 6e 61 6d 65 2e 7a 20 3d 20 7a 43 6f 6c 6e 61  lname.z = zColna
2ac18 6d 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  me;.            
2ac19 73 43 6f 6c 6e 61 6d 65 2e 6e 20 3d 20 73 71 6c  sColname.n = sql
2ac1a 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 43 6f  ite3Strlen30(zCo
2ac1b 6c 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20  lname);.        
2ac1c 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c      sqlite3ExprL
2ac1d 69 73 74 53 65 74 4e 61 6d 65 28 70 50 61 72 73  istSetName(pPars
2ac1e 65 2c 20 70 4e 65 77 2c 20 26 73 43 6f 6c 6e 61  e, pNew, &sColna
2ac1f 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  me, 0);.        
2ac20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2ac21 65 28 64 62 2c 20 7a 54 6f 46 72 65 65 29 3b 0a  e(db, zToFree);.
2ac22 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
2ac23 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
2ac24 28 20 21 74 61 62 6c 65 53 65 65 6e 20 29 7b 0a  ( !tableSeen ){.
2ac25 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 54            if( zT
2ac26 4e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 20 20  Name ){.        
2ac27 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2ac28 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20  Msg(pParse, "no 
2ac29 73 75 63 68 20 74 61 62 6c 65 3a 20 25 73 22 2c  such table: %s",
2ac2a 20 7a 54 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20   zTName);.      
2ac2b 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2ac2c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
2ac2d 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
2ac2e 6e 6f 20 74 61 62 6c 65 73 20 73 70 65 63 69 66  no tables specif
2ac2f 69 65 64 22 29 3b 0a 20 20 20 20 20 20 20 20 20  ied");.         
2ac30 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
2ac31 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73     }.    }.    s
2ac32 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65  qlite3ExprListDe
2ac33 6c 65 74 65 28 64 62 2c 20 70 45 4c 69 73 74 29  lete(db, pEList)
2ac34 3b 0a 20 20 20 20 70 2d 3e 70 45 4c 69 73 74 20  ;.    p->pEList 
2ac35 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 23 69 66 20  = pNew;.  }.#if 
2ac36 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d  SQLITE_MAX_COLUM
2ac37 4e 0a 20 20 69 66 28 20 70 2d 3e 70 45 4c 69 73  N.  if( p->pELis
2ac38 74 20 26 26 20 70 2d 3e 70 45 4c 69 73 74 2d 3e  t && p->pEList->
2ac39 6e 45 78 70 72 3e 64 62 2d 3e 61 4c 69 6d 69 74  nExpr>db->aLimit
2ac3a 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f  [SQLITE_LIMIT_CO
2ac3b 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20 20 73 71 6c  LUMN] ){.    sql
2ac3c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2ac3d 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 63  rse, "too many c
2ac3e 6f 6c 75 6d 6e 73 20 69 6e 20 72 65 73 75 6c 74  olumns in result
2ac3f 20 73 65 74 22 29 3b 0a 20 20 7d 0a 23 65 6e 64   set");.  }.#end
2ac40 69 66 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f  if.  return WRC_
2ac41 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a  Continue;.}../*.
2ac42 2a 2a 20 4e 6f 2d 6f 70 20 72 6f 75 74 69 6e 65  ** No-op routine
2ac43 20 66 6f 72 20 74 68 65 20 70 61 72 73 65 2d 74   for the parse-t
2ac44 72 65 65 20 77 61 6c 6b 65 72 2e 0a 2a 2a 0a 2a  ree walker..**.*
2ac45 2a 20 57 68 65 6e 20 74 68 69 73 20 72 6f 75 74  * When this rout
2ac46 69 6e 65 20 69 73 20 74 68 65 20 57 61 6c 6b 65  ine is the Walke
2ac47 72 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20  r.xExprCallback 
2ac48 74 68 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  then expression 
2ac49 74 72 65 65 73 0a 2a 2a 20 61 72 65 20 77 61 6c  trees.** are wal
2ac4a 6b 65 64 20 77 69 74 68 6f 75 74 20 61 6e 79 20  ked without any 
2ac4b 61 63 74 69 6f 6e 73 20 62 65 69 6e 67 20 74 61  actions being ta
2ac4c 6b 65 6e 20 61 74 20 65 61 63 68 20 6e 6f 64 65  ken at each node
2ac4d 2e 20 20 50 72 65 73 75 6d 61 62 6c 79 2c 0a 2a  .  Presumably,.*
2ac4e 2a 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74  * when this rout
2ac4f 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f 72 20  ine is used for 
2ac50 57 61 6c 6b 65 72 2e 78 45 78 70 72 43 61 6c 6c  Walker.xExprCall
2ac51 62 61 63 6b 20 74 68 65 6e 20 0a 2a 2a 20 57 61  back then .** Wa
2ac52 6c 6b 65 72 2e 78 53 65 6c 65 63 74 43 61 6c 6c  lker.xSelectCall
2ac53 62 61 63 6b 20 69 73 20 73 65 74 20 74 6f 20 64  back is set to d
2ac54 6f 20 73 6f 6d 65 74 68 69 6e 67 20 75 73 65 66  o something usef
2ac55 75 6c 20 66 6f 72 20 65 76 65 72 79 20 0a 2a 2a  ul for every .**
2ac56 20 73 75 62 71 75 65 72 79 20 69 6e 20 74 68 65   subquery in the
2ac57 20 70 61 72 73 65 72 20 74 72 65 65 2e 0a 2a 2f   parser tree..*/
2ac58 0a 73 74 61 74 69 63 20 69 6e 74 20 65 78 70 72  .static int expr
2ac59 57 61 6c 6b 4e 6f 6f 70 28 57 61 6c 6b 65 72 20  WalkNoop(Walker 
2ac5a 2a 4e 6f 74 55 73 65 64 2c 20 45 78 70 72 20 2a  *NotUsed, Expr *
2ac5b 4e 6f 74 55 73 65 64 32 29 7b 0a 20 20 55 4e 55  NotUsed2){.  UNU
2ac5c 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e  SED_PARAMETER2(N
2ac5d 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32  otUsed, NotUsed2
2ac5e 29 3b 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f  );.  return WRC_
2ac5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a  Continue;.}../*.
2ac60 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2ac61 22 65 78 70 61 6e 64 73 22 20 61 20 53 45 4c 45  "expands" a SELE
2ac62 43 54 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64  CT statement and
2ac63 20 61 6c 6c 20 6f 66 20 69 74 73 20 73 75 62 71   all of its subq
2ac64 75 65 72 69 65 73 2e 0a 2a 2a 20 46 6f 72 20 61  ueries..** For a
2ac65 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
2ac66 61 74 69 6f 6e 20 6f 6e 20 77 68 61 74 20 69 74  ation on what it
2ac67 20 6d 65 61 6e 73 20 74 6f 20 22 65 78 70 61 6e   means to "expan
2ac68 64 22 20 61 20 53 45 4c 45 43 54 0a 2a 2a 20 73  d" a SELECT.** s
2ac69 74 61 74 65 6d 65 6e 74 2c 20 73 65 65 20 74 68  tatement, see th
2ac6a 65 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 74 68 65  e comment on the
2ac6b 20 73 65 6c 65 63 74 45 78 70 61 6e 64 20 77 6f   selectExpand wo
2ac6c 72 6b 65 72 20 63 61 6c 6c 62 61 63 6b 20 61 62  rker callback ab
2ac6d 6f 76 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 61 6e  ove..**.** Expan
2ac6e 64 69 6e 67 20 61 20 53 45 4c 45 43 54 20 73 74  ding a SELECT st
2ac6f 61 74 65 6d 65 6e 74 20 69 73 20 74 68 65 20 66  atement is the f
2ac70 69 72 73 74 20 73 74 65 70 20 69 6e 20 70 72 6f  irst step in pro
2ac71 63 65 73 73 69 6e 67 20 61 0a 2a 2a 20 53 45 4c  cessing a.** SEL
2ac72 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  ECT statement.  
2ac73 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65  The SELECT state
2ac74 6d 65 6e 74 20 6d 75 73 74 20 62 65 20 65 78 70  ment must be exp
2ac75 61 6e 64 65 64 20 62 65 66 6f 72 65 0a 2a 2a 20  anded before.** 
2ac76 6e 61 6d 65 20 72 65 73 6f 6c 75 74 69 6f 6e 20  name resolution 
2ac77 69 73 20 70 65 72 66 6f 72 6d 65 64 2e 0a 2a 2a  is performed..**
2ac78 0a 2a 2a 20 49 66 20 61 6e 79 74 68 69 6e 67 20  .** If anything 
2ac79 67 6f 65 73 20 77 72 6f 6e 67 2c 20 61 6e 20 65  goes wrong, an e
2ac7a 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20  rror message is 
2ac7b 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 70 50 61  written into pPa
2ac7c 72 73 65 2e 0a 2a 2a 20 54 68 65 20 63 61 6c 6c  rse..** The call
2ac7d 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e  ing function can
2ac7e 20 64 65 74 65 63 74 20 74 68 65 20 70 72 6f 62   detect the prob
2ac7f 6c 65 6d 20 62 79 20 6c 6f 6f 6b 69 6e 67 20 61  lem by looking a
2ac80 74 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 0a 2a  t pParse->nErr.*
2ac81 2a 20 61 6e 64 2f 6f 72 20 70 50 61 72 73 65 2d  * and/or pParse-
2ac82 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
2ac83 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  d..*/.static voi
2ac84 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45  d sqlite3SelectE
2ac85 78 70 61 6e 64 28 50 61 72 73 65 20 2a 70 50 61  xpand(Parse *pPa
2ac86 72 73 65 2c 20 53 65 6c 65 63 74 20 2a 70 53 65  rse, Select *pSe
2ac87 6c 65 63 74 29 7b 0a 20 20 57 61 6c 6b 65 72 20  lect){.  Walker 
2ac88 77 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61  w;.  w.xSelectCa
2ac89 6c 6c 62 61 63 6b 20 3d 20 73 65 6c 65 63 74 45  llback = selectE
2ac8a 78 70 61 6e 64 65 72 3b 0a 20 20 77 2e 78 45 78  xpander;.  w.xEx
2ac8b 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 65 78 70  prCallback = exp
2ac8c 72 57 61 6c 6b 4e 6f 6f 70 3b 0a 20 20 77 2e 70  rWalkNoop;.  w.p
2ac8d 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a  Parse = pParse;.
2ac8e 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c    sqlite3WalkSel
2ac8f 65 63 74 28 26 77 2c 20 70 53 65 6c 65 63 74 29  ect(&w, pSelect)
2ac90 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51  ;.}...#ifndef SQ
2ac91 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
2ac92 52 59 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73  RY./*.** This is
2ac93 20 61 20 57 61 6c 6b 65 72 2e 78 53 65 6c 65 63   a Walker.xSelec
2ac94 74 43 61 6c 6c 62 61 63 6b 20 63 61 6c 6c 62 61  tCallback callba
2ac95 63 6b 20 66 6f 72 20 74 68 65 20 73 71 6c 69 74  ck for the sqlit
2ac96 65 33 53 65 6c 65 63 74 54 79 70 65 49 6e 66 6f  e3SelectTypeInfo
2ac97 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e  ().** interface.
2ac98 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 61 63 68 20  .**.** For each 
2ac99 46 52 4f 4d 2d 63 6c 61 75 73 65 20 73 75 62 71  FROM-clause subq
2ac9a 75 65 72 79 2c 20 61 64 64 20 43 6f 6c 75 6d 6e  uery, add Column
2ac9b 2e 7a 54 79 70 65 20 61 6e 64 20 43 6f 6c 75 6d  .zType and Colum
2ac9c 6e 2e 7a 43 6f 6c 6c 0a 2a 2a 20 69 6e 66 6f 72  n.zColl.** infor
2ac9d 6d 61 74 69 6f 6e 20 74 6f 20 74 68 65 20 54 61  mation to the Ta
2ac9e 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 74 68  ble structure th
2ac9f 61 74 20 72 65 70 72 65 73 65 6e 74 73 20 74 68  at represents th
2aca0 65 20 72 65 73 75 6c 74 20 73 65 74 0a 2a 2a 20  e result set.** 
2aca1 6f 66 20 74 68 61 74 20 73 75 62 71 75 65 72 79  of that subquery
2aca2 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 54 61 62 6c  ..**.** The Tabl
2aca3 65 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  e structure that
2aca4 20 72 65 70 72 65 73 65 6e 74 73 20 74 68 65 20   represents the 
2aca5 72 65 73 75 6c 74 20 73 65 74 20 77 61 73 20 63  result set was c
2aca6 6f 6e 73 74 72 75 63 74 65 64 0a 2a 2a 20 62 79  onstructed.** by
2aca7 20 73 65 6c 65 63 74 45 78 70 61 6e 64 65 72 28   selectExpander(
2aca8 29 20 62 75 74 20 74 68 65 20 74 79 70 65 20 61  ) but the type a
2aca9 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 69 6e 66  nd collation inf
2acaa 6f 72 6d 61 74 69 6f 6e 20 77 61 73 20 6f 6d 69  ormation was omi
2acab 74 74 65 64 0a 2a 2a 20 61 74 20 74 68 61 74 20  tted.** at that 
2acac 70 6f 69 6e 74 20 62 65 63 61 75 73 65 20 69 64  point because id
2acad 65 6e 74 69 66 69 65 72 73 20 68 61 64 20 6e 6f  entifiers had no
2acae 74 20 79 65 74 20 62 65 65 6e 20 72 65 73 6f 6c  t yet been resol
2acaf 76 65 64 2e 20 20 54 68 69 73 0a 2a 2a 20 72 6f  ved.  This.** ro
2acb0 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
2acb1 61 66 74 65 72 20 69 64 65 6e 74 69 66 69 65 72  after identifier
2acb2 20 72 65 73 6f 6c 75 74 69 6f 6e 2e 0a 2a 2f 0a   resolution..*/.
2acb3 73 74 61 74 69 63 20 69 6e 74 20 73 65 6c 65 63  static int selec
2acb4 74 41 64 64 53 75 62 71 75 65 72 79 54 79 70 65  tAddSubqueryType
2acb5 49 6e 66 6f 28 57 61 6c 6b 65 72 20 2a 70 57 61  Info(Walker *pWa
2acb6 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29  lker, Select *p)
2acb7 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  {.  Parse *pPars
2acb8 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 53 72  e;.  int i;.  Sr
2acb9 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 3b  cList *pTabList;
2acba 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
2acbb 74 5f 69 74 65 6d 20 2a 70 46 72 6f 6d 3b 0a 0a  t_item *pFrom;..
2acbc 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 65 6c    assert( p->sel
2acbd 46 6c 61 67 73 20 26 20 53 46 5f 52 65 73 6f 6c  Flags & SF_Resol
2acbe 76 65 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ved );.  assert(
2acbf 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20   (p->selFlags & 
2acc0 53 46 5f 48 61 73 54 79 70 65 49 6e 66 6f 29 3d  SF_HasTypeInfo)=
2acc1 3d 30 20 29 3b 0a 20 20 70 2d 3e 73 65 6c 46 6c  =0 );.  p->selFl
2acc2 61 67 73 20 7c 3d 20 53 46 5f 48 61 73 54 79 70  ags |= SF_HasTyp
2acc3 65 49 6e 66 6f 3b 0a 20 20 70 50 61 72 73 65 20  eInfo;.  pParse 
2acc4 3d 20 70 57 61 6c 6b 65 72 2d 3e 70 50 61 72 73  = pWalker->pPars
2acc5 65 3b 0a 20 20 70 54 61 62 4c 69 73 74 20 3d 20  e;.  pTabList = 
2acc6 70 2d 3e 70 53 72 63 3b 0a 20 20 66 6f 72 28 69  p->pSrc;.  for(i
2acc7 3d 30 2c 20 70 46 72 6f 6d 3d 70 54 61 62 4c 69  =0, pFrom=pTabLi
2acc8 73 74 2d 3e 61 3b 20 69 3c 70 54 61 62 4c 69 73  st->a; i<pTabLis
2acc9 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 46  t->nSrc; i++, pF
2acca 72 6f 6d 2b 2b 29 7b 0a 20 20 20 20 54 61 62 6c  rom++){.    Tabl
2accb 65 20 2a 70 54 61 62 20 3d 20 70 46 72 6f 6d 2d  e *pTab = pFrom-
2accc 3e 70 54 61 62 3b 0a 20 20 20 20 69 66 28 20 41  >pTab;.    if( A
2accd 4c 57 41 59 53 28 70 54 61 62 21 3d 30 29 20 26  LWAYS(pTab!=0) &
2acce 26 20 28 70 54 61 62 2d 3e 74 61 62 46 6c 61 67  & (pTab->tabFlag
2accf 73 20 26 20 54 46 5f 45 70 68 65 6d 65 72 61 6c  s & TF_Ephemeral
2acd0 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a  )!=0 ){.      /*
2acd1 20 41 20 73 75 62 2d 71 75 65 72 79 20 69 6e 20   A sub-query in 
2acd2 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
2acd3 6f 66 20 61 20 53 45 4c 45 43 54 20 2a 2f 0a 20  of a SELECT */. 
2acd4 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 65       Select *pSe
2acd5 6c 20 3d 20 70 46 72 6f 6d 2d 3e 70 53 65 6c 65  l = pFrom->pSele
2acd6 63 74 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  ct;.      assert
2acd7 28 20 70 53 65 6c 20 29 3b 0a 20 20 20 20 20 20  ( pSel );.      
2acd8 77 68 69 6c 65 28 20 70 53 65 6c 2d 3e 70 50 72  while( pSel->pPr
2acd9 69 6f 72 20 29 20 70 53 65 6c 20 3d 20 70 53 65  ior ) pSel = pSe
2acda 6c 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 20  l->pPrior;.     
2acdb 20 73 65 6c 65 63 74 41 64 64 43 6f 6c 75 6d 6e   selectAddColumn
2acdc 54 79 70 65 41 6e 64 43 6f 6c 6c 61 74 69 6f 6e  TypeAndCollation
2acdd 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 6e  (pParse, pTab->n
2acde 43 6f 6c 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 2c  Col, pTab->aCol,
2acdf 20 70 53 65 6c 29 3b 0a 20 20 20 20 7d 0a 20 20   pSel);.    }.  
2ace0 7d 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43  }.  return WRC_C
2ace1 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 23 65 6e 64 69  ontinue;.}.#endi
2ace2 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  f.../*.** This r
2ace3 6f 75 74 69 6e 65 20 61 64 64 73 20 64 61 74 61  outine adds data
2ace4 74 79 70 65 20 61 6e 64 20 63 6f 6c 6c 61 74 69  type and collati
2ace5 6e 67 20 73 65 71 75 65 6e 63 65 20 69 6e 66 6f  ng sequence info
2ace6 72 6d 61 74 69 6f 6e 20 74 6f 0a 2a 2a 20 74 68  rmation to.** th
2ace7 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72  e Table structur
2ace8 65 73 20 6f 66 20 61 6c 6c 20 46 52 4f 4d 2d 63  es of all FROM-c
2ace9 6c 61 75 73 65 20 73 75 62 71 75 65 72 69 65 73  lause subqueries
2acea 20 69 6e 20 61 0a 2a 2a 20 53 45 4c 45 43 54 20   in a.** SELECT 
2aceb 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a  statement..**.**
2acec 20 55 73 65 20 74 68 69 73 20 72 6f 75 74 69 6e   Use this routin
2aced 65 20 61 66 74 65 72 20 6e 61 6d 65 20 72 65 73  e after name res
2acee 6f 6c 75 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  olution..*/.stat
2acef 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  ic void sqlite3S
2acf0 65 6c 65 63 74 41 64 64 54 79 70 65 49 6e 66 6f  electAddTypeInfo
2acf1 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
2acf2 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 29  Select *pSelect)
2acf3 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  {.#ifndef SQLITE
2acf4 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20  _OMIT_SUBQUERY. 
2acf5 20 57 61 6c 6b 65 72 20 77 3b 0a 20 20 77 2e 78   Walker w;.  w.x
2acf6 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d  SelectCallback =
2acf7 20 73 65 6c 65 63 74 41 64 64 53 75 62 71 75 65   selectAddSubque
2acf8 72 79 54 79 70 65 49 6e 66 6f 3b 0a 20 20 77 2e  ryTypeInfo;.  w.
2acf9 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20  xExprCallback = 
2acfa 65 78 70 72 57 61 6c 6b 4e 6f 6f 70 3b 0a 20 20  exprWalkNoop;.  
2acfb 77 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73  w.pParse = pPars
2acfc 65 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b  e;.  sqlite3Walk
2acfd 53 65 6c 65 63 74 28 26 77 2c 20 70 53 65 6c 65  Select(&w, pSele
2acfe 63 74 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 0a  ct);.#endif.}...
2acff 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
2ad00 6e 65 20 73 65 74 73 20 6f 66 20 61 20 53 45 4c  ne sets of a SEL
2ad01 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 66 6f  ECT statement fo
2ad02 72 20 70 72 6f 63 65 73 73 69 6e 67 2e 20 20 54  r processing.  T
2ad03 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20  he.** following 
2ad04 69 73 20 61 63 63 6f 6d 70 6c 69 73 68 65 64 3a  is accomplished:
2ad05 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 20 56 44  .**.**     *  VD
2ad06 42 45 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72  BE Cursor number
2ad07 73 20 61 72 65 20 61 73 73 69 67 6e 65 64 20 74  s are assigned t
2ad08 6f 20 61 6c 6c 20 46 52 4f 4d 2d 63 6c 61 75 73  o all FROM-claus
2ad09 65 20 74 65 72 6d 73 2e 0a 2a 2a 20 20 20 20 20  e terms..**     
2ad0a 2a 20 20 45 70 68 65 6d 65 72 61 6c 20 54 61 62  *  Ephemeral Tab
2ad0b 6c 65 20 6f 62 6a 65 63 74 73 20 61 72 65 20 63  le objects are c
2ad0c 72 65 61 74 65 64 20 66 6f 72 20 61 6c 6c 20 46  reated for all F
2ad0d 52 4f 4d 2d 63 6c 61 75 73 65 20 73 75 62 71 75  ROM-clause subqu
2ad0e 65 72 69 65 73 2e 0a 2a 2a 20 20 20 20 20 2a 20  eries..**     * 
2ad0f 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20 63 6c   ON and USING cl
2ad10 61 75 73 65 73 20 61 72 65 20 73 68 69 66 74 65  auses are shifte
2ad11 64 20 69 6e 74 6f 20 57 48 45 52 45 20 73 74 61  d into WHERE sta
2ad12 74 65 6d 65 6e 74 73 0a 2a 2a 20 20 20 20 20 2a  tements.**     *
2ad13 20 20 57 69 6c 64 63 61 72 64 73 20 22 2a 22 20    Wildcards "*" 
2ad14 61 6e 64 20 22 54 41 42 4c 45 2e 2a 22 20 69 6e  and "TABLE.*" in
2ad15 20 72 65 73 75 6c 74 20 73 65 74 73 20 61 72 65   result sets are
2ad16 20 65 78 70 61 6e 64 65 64 2e 0a 2a 2a 20 20 20   expanded..**   
2ad17 20 20 2a 20 20 49 64 65 6e 74 69 66 69 65 72 73    *  Identifiers
2ad18 20 69 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 61   in expression a
2ad19 72 65 20 6d 61 74 63 68 65 64 20 74 6f 20 74 61  re matched to ta
2ad1a 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  bles..**.** This
2ad1b 20 72 6f 75 74 69 6e 65 20 61 63 74 73 20 72 65   routine acts re
2ad1c 63 75 72 73 69 76 65 6c 79 20 6f 6e 20 61 6c 6c  cursively on all
2ad1d 20 73 75 62 71 75 65 72 69 65 73 20 77 69 74 68   subqueries with
2ad1e 69 6e 20 74 68 65 20 53 45 4c 45 43 54 2e 0a 2a  in the SELECT..*
2ad1f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2ad20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c   void sqlite3Sel
2ad21 65 63 74 50 72 65 70 28 0a 20 20 50 61 72 73 65  ectPrep(.  Parse
2ad22 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
2ad23 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20    /* The parser 
2ad24 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c  context */.  Sel
2ad25 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20  ect *p,         
2ad26 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43      /* The SELEC
2ad27 54 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e  T statement bein
2ad28 67 20 63 6f 64 65 64 2e 20 2a 2f 0a 20 20 4e 61  g coded. */.  Na
2ad29 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4f 75 74 65  meContext *pOute
2ad2a 72 4e 43 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e  rNC  /* Name con
2ad2b 74 65 78 74 20 66 6f 72 20 63 6f 6e 74 61 69 6e  text for contain
2ad2c 65 72 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74  er */.){.  sqlit
2ad2d 65 33 20 2a 64 62 3b 0a 20 20 69 66 28 20 4e 45  e3 *db;.  if( NE
2ad2e 56 45 52 28 70 3d 3d 30 29 20 29 20 72 65 74 75  VER(p==0) ) retu
2ad2f 72 6e 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73  rn;.  db = pPars
2ad30 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 2d 3e  e->db;.  if( p->
2ad31 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 48 61  selFlags & SF_Ha
2ad32 73 54 79 70 65 49 6e 66 6f 20 29 20 72 65 74 75  sTypeInfo ) retu
2ad33 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c  rn;.  sqlite3Sel
2ad34 65 63 74 45 78 70 61 6e 64 28 70 50 61 72 73 65  ectExpand(pParse
2ad35 2c 20 70 29 3b 0a 20 20 69 66 28 20 70 50 61 72  , p);.  if( pPar
2ad36 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e  se->nErr || db->
2ad37 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72  mallocFailed ) r
2ad38 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33  eturn;.  sqlite3
2ad39 52 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61 6d  ResolveSelectNam
2ad3a 65 73 28 70 50 61 72 73 65 2c 20 70 2c 20 70 4f  es(pParse, p, pO
2ad3b 75 74 65 72 4e 43 29 3b 0a 20 20 69 66 28 20 70  uterNC);.  if( p
2ad3c 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64  Parse->nErr || d
2ad3d 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2ad3e 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69  ) return;.  sqli
2ad3f 74 65 33 53 65 6c 65 63 74 41 64 64 54 79 70 65  te3SelectAddType
2ad40 49 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 29 3b  Info(pParse, p);
2ad41 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74 20  .}../*.** Reset 
2ad42 74 68 65 20 61 67 67 72 65 67 61 74 65 20 61 63  the aggregate ac
2ad43 63 75 6d 75 6c 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a  cumulator..**.**
2ad44 20 54 68 65 20 61 67 67 72 65 67 61 74 65 20 61   The aggregate a
2ad45 63 63 75 6d 75 6c 61 74 6f 72 20 69 73 20 61 20  ccumulator is a 
2ad46 73 65 74 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65  set of memory ce
2ad47 6c 6c 73 20 74 68 61 74 20 68 6f 6c 64 0a 2a 2a  lls that hold.**
2ad48 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 72 65   intermediate re
2ad49 73 75 6c 74 73 20 77 68 69 6c 65 20 63 61 6c 63  sults while calc
2ad4a 75 6c 61 74 69 6e 67 20 61 6e 20 61 67 67 72 65  ulating an aggre
2ad4b 67 61 74 65 2e 20 20 54 68 69 73 0a 2a 2a 20 72  gate.  This.** r
2ad4c 6f 75 74 69 6e 65 20 73 69 6d 70 6c 79 20 73 74  outine simply st
2ad4d 6f 72 65 73 20 4e 55 4c 4c 73 20 69 6e 20 61 6c  ores NULLs in al
2ad4e 6c 20 6f 66 20 74 68 6f 73 65 20 6d 65 6d 6f 72  l of those memor
2ad4f 79 20 63 65 6c 6c 73 2e 0a 2a 2f 0a 73 74 61 74  y cells..*/.stat
2ad50 69 63 20 76 6f 69 64 20 72 65 73 65 74 41 63 63  ic void resetAcc
2ad51 75 6d 75 6c 61 74 6f 72 28 50 61 72 73 65 20 2a  umulator(Parse *
2ad52 70 50 61 72 73 65 2c 20 41 67 67 49 6e 66 6f 20  pParse, AggInfo 
2ad53 2a 70 41 67 67 49 6e 66 6f 29 7b 0a 20 20 56 64  *pAggInfo){.  Vd
2ad54 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
2ad55 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a  pVdbe;.  int i;.
2ad56 20 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f    struct AggInfo
2ad57 5f 66 75 6e 63 20 2a 70 46 75 6e 63 3b 0a 20 20  _func *pFunc;.  
2ad58 69 66 28 20 70 41 67 67 49 6e 66 6f 2d 3e 6e 46  if( pAggInfo->nF
2ad59 75 6e 63 2b 70 41 67 67 49 6e 66 6f 2d 3e 6e 43  unc+pAggInfo->nC
2ad5a 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20 20  olumn==0 ){.    
2ad5b 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f  return;.  }.  fo
2ad5c 72 28 69 3d 30 3b 20 69 3c 70 41 67 67 49 6e 66  r(i=0; i<pAggInf
2ad5d 6f 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29  o->nColumn; i++)
2ad5e 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
2ad5f 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75  eAddOp2(v, OP_Nu
2ad60 6c 6c 2c 20 30 2c 20 70 41 67 67 49 6e 66 6f 2d  ll, 0, pAggInfo-
2ad61 3e 61 43 6f 6c 5b 69 5d 2e 69 4d 65 6d 29 3b 0a  >aCol[i].iMem);.
2ad62 20 20 7d 0a 20 20 66 6f 72 28 70 46 75 6e 63 3d    }.  for(pFunc=
2ad63 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 2c  pAggInfo->aFunc,
2ad64 20 69 3d 30 3b 20 69 3c 70 41 67 67 49 6e 66 6f   i=0; i<pAggInfo
2ad65 2d 3e 6e 46 75 6e 63 3b 20 69 2b 2b 2c 20 70 46  ->nFunc; i++, pF
2ad66 75 6e 63 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69  unc++){.    sqli
2ad67 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2ad68 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 70 46 75   OP_Null, 0, pFu
2ad69 6e 63 2d 3e 69 4d 65 6d 29 3b 0a 20 20 20 20 69  nc->iMem);.    i
2ad6a 66 28 20 70 46 75 6e 63 2d 3e 69 44 69 73 74 69  f( pFunc->iDisti
2ad6b 6e 63 74 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20  nct>=0 ){.      
2ad6c 45 78 70 72 20 2a 70 45 20 3d 20 70 46 75 6e 63  Expr *pE = pFunc
2ad6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 61  ->pExpr;.      a
2ad6e 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50  ssert( !ExprHasP
2ad6f 72 6f 70 65 72 74 79 28 70 45 2c 20 45 50 5f 78  roperty(pE, EP_x
2ad70 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20  IsSelect) );.   
2ad71 20 20 20 69 66 28 20 70 45 2d 3e 78 2e 70 4c 69     if( pE->x.pLi
2ad72 73 74 3d 3d 30 20 7c 7c 20 70 45 2d 3e 78 2e 70  st==0 || pE->x.p
2ad73 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31 20 29  List->nExpr!=1 )
2ad74 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2ad75 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2ad76 2c 20 22 44 49 53 54 49 4e 43 54 20 61 67 67 72  , "DISTINCT aggr
2ad77 65 67 61 74 65 73 20 6d 75 73 74 20 68 61 76 65  egates must have
2ad78 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 22 0a 20   exactly one ". 
2ad79 20 20 20 20 20 20 20 20 20 20 22 61 72 67 75 6d            "argum
2ad7a 65 6e 74 22 29 3b 0a 20 20 20 20 20 20 20 20 70  ent");.        p
2ad7b 46 75 6e 63 2d 3e 69 44 69 73 74 69 6e 63 74 20  Func->iDistinct 
2ad7c 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73  = -1;.      }els
2ad7d 65 7b 0a 20 20 20 20 20 20 20 20 4b 65 79 49 6e  e{.        KeyIn
2ad7e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 20 3d 20 6b  fo *pKeyInfo = k
2ad7f 65 79 49 6e 66 6f 46 72 6f 6d 45 78 70 72 4c 69  eyInfoFromExprLi
2ad80 73 74 28 70 50 61 72 73 65 2c 20 70 45 2d 3e 78  st(pParse, pE->x
2ad81 2e 70 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 20  .pList);.       
2ad82 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2ad83 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68  p4(v, OP_OpenEph
2ad84 65 6d 65 72 61 6c 2c 20 70 46 75 6e 63 2d 3e 69  emeral, pFunc->i
2ad85 44 69 73 74 69 6e 63 74 2c 20 30 2c 20 30 2c 0a  Distinct, 0, 0,.
2ad86 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ad87 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a            (char*
2ad88 29 70 4b 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45  )pKeyInfo, P4_KE
2ad89 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a  YINFO_HANDOFF);.
2ad8a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
2ad8b 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b  }.}../*.** Invok
2ad8c 65 20 74 68 65 20 4f 50 5f 41 67 67 46 69 6e 61  e the OP_AggFina
2ad8d 6c 69 7a 65 20 6f 70 63 6f 64 65 20 66 6f 72 20  lize opcode for 
2ad8e 65 76 65 72 79 20 61 67 67 72 65 67 61 74 65 20  every aggregate 
2ad8f 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 6e 20 74  function.** in t
2ad90 68 65 20 41 67 67 49 6e 66 6f 20 73 74 72 75 63  he AggInfo struc
2ad91 74 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ture..*/.static 
2ad92 76 6f 69 64 20 66 69 6e 61 6c 69 7a 65 41 67 67  void finalizeAgg
2ad93 46 75 6e 63 74 69 6f 6e 73 28 50 61 72 73 65 20  Functions(Parse 
2ad94 2a 70 50 61 72 73 65 2c 20 41 67 67 49 6e 66 6f  *pParse, AggInfo
2ad95 20 2a 70 41 67 67 49 6e 66 6f 29 7b 0a 20 20 56   *pAggInfo){.  V
2ad96 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d  dbe *v = pParse-
2ad97 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b  >pVdbe;.  int i;
2ad98 0a 20 20 73 74 72 75 63 74 20 41 67 67 49 6e 66  .  struct AggInf
2ad99 6f 5f 66 75 6e 63 20 2a 70 46 3b 0a 20 20 66 6f  o_func *pF;.  fo
2ad9a 72 28 69 3d 30 2c 20 70 46 3d 70 41 67 67 49 6e  r(i=0, pF=pAggIn
2ad9b 66 6f 2d 3e 61 46 75 6e 63 3b 20 69 3c 70 41 67  fo->aFunc; i<pAg
2ad9c 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 3b 20 69 2b  gInfo->nFunc; i+
2ad9d 2b 2c 20 70 46 2b 2b 29 7b 0a 20 20 20 20 45 78  +, pF++){.    Ex
2ad9e 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20  prList *pList = 
2ad9f 70 46 2d 3e 70 45 78 70 72 2d 3e 78 2e 70 4c 69  pF->pExpr->x.pLi
2ada0 73 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  st;.    assert( 
2ada1 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79  !ExprHasProperty
2ada2 28 70 46 2d 3e 70 45 78 70 72 2c 20 45 50 5f 78  (pF->pExpr, EP_x
2ada3 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20  IsSelect) );.   
2ada4 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2ada5 70 34 28 76 2c 20 4f 50 5f 41 67 67 46 69 6e 61  p4(v, OP_AggFina
2ada6 6c 2c 20 70 46 2d 3e 69 4d 65 6d 2c 20 70 4c 69  l, pF->iMem, pLi
2ada7 73 74 20 3f 20 70 4c 69 73 74 2d 3e 6e 45 78 70  st ? pList->nExp
2ada8 72 20 3a 20 30 2c 20 30 2c 0a 20 20 20 20 20 20  r : 0, 0,.      
2ada9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2adaa 28 76 6f 69 64 2a 29 70 46 2d 3e 70 46 75 6e 63  (void*)pF->pFunc
2adab 2c 20 50 34 5f 46 55 4e 43 44 45 46 29 3b 0a 20  , P4_FUNCDEF);. 
2adac 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 70 64 61   }.}../*.** Upda
2adad 74 65 20 74 68 65 20 61 63 63 75 6d 75 6c 61 74  te the accumulat
2adae 6f 72 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20  or memory cells 
2adaf 66 6f 72 20 61 6e 20 61 67 67 72 65 67 61 74 65  for an aggregate
2adb0 20 62 61 73 65 64 20 6f 6e 0a 2a 2a 20 74 68 65   based on.** the
2adb1 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f 72 20   current cursor 
2adb2 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  position..*/.sta
2adb3 74 69 63 20 76 6f 69 64 20 75 70 64 61 74 65 41  tic void updateA
2adb4 63 63 75 6d 75 6c 61 74 6f 72 28 50 61 72 73 65  ccumulator(Parse
2adb5 20 2a 70 50 61 72 73 65 2c 20 41 67 67 49 6e 66   *pParse, AggInf
2adb6 6f 20 2a 70 41 67 67 49 6e 66 6f 29 7b 0a 20 20  o *pAggInfo){.  
2adb7 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65  Vdbe *v = pParse
2adb8 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69  ->pVdbe;.  int i
2adb9 3b 0a 20 20 73 74 72 75 63 74 20 41 67 67 49 6e  ;.  struct AggIn
2adba 66 6f 5f 66 75 6e 63 20 2a 70 46 3b 0a 20 20 73  fo_func *pF;.  s
2adbb 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 63 6f  truct AggInfo_co
2adbc 6c 20 2a 70 43 3b 0a 0a 20 20 70 41 67 67 49 6e  l *pC;..  pAggIn
2adbd 66 6f 2d 3e 64 69 72 65 63 74 4d 6f 64 65 20 3d  fo->directMode =
2adbe 20 31 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70   1;.  sqlite3Exp
2adbf 72 43 61 63 68 65 43 6c 65 61 72 28 70 50 61 72  rCacheClear(pPar
2adc0 73 65 29 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20  se);.  for(i=0, 
2adc1 70 46 3d 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75  pF=pAggInfo->aFu
2adc2 6e 63 3b 20 69 3c 70 41 67 67 49 6e 66 6f 2d 3e  nc; i<pAggInfo->
2adc3 6e 46 75 6e 63 3b 20 69 2b 2b 2c 20 70 46 2b 2b  nFunc; i++, pF++
2adc4 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41 72 67 3b  ){.    int nArg;
2adc5 0a 20 20 20 20 69 6e 74 20 61 64 64 72 4e 65 78  .    int addrNex
2adc6 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 72  t = 0;.    int r
2adc7 65 67 41 67 67 3b 0a 20 20 20 20 45 78 70 72 4c  egAgg;.    ExprL
2adc8 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 46 2d  ist *pList = pF-
2adc9 3e 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b  >pExpr->x.pList;
2adca 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78  .    assert( !Ex
2adcb 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 46  prHasProperty(pF
2adcc 2d 3e 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53  ->pExpr, EP_xIsS
2adcd 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 69 66  elect) );.    if
2adce 28 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 20  ( pList ){.     
2adcf 20 6e 41 72 67 20 3d 20 70 4c 69 73 74 2d 3e 6e   nArg = pList->n
2add0 45 78 70 72 3b 0a 20 20 20 20 20 20 72 65 67 41  Expr;.      regA
2add1 67 67 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  gg = sqlite3GetT
2add2 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c  empRange(pParse,
2add3 20 6e 41 72 67 29 3b 0a 20 20 20 20 20 20 73 71   nArg);.      sq
2add4 6c 69 74 65 33 45 78 70 72 43 6f 64 65 45 78 70  lite3ExprCodeExp
2add5 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 4c  rList(pParse, pL
2add6 69 73 74 2c 20 72 65 67 41 67 67 2c 20 30 29 3b  ist, regAgg, 0);
2add7 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2add8 20 20 6e 41 72 67 20 3d 20 30 3b 0a 20 20 20 20    nArg = 0;.    
2add9 20 20 72 65 67 41 67 67 20 3d 20 30 3b 0a 20 20    regAgg = 0;.  
2adda 20 20 7d 0a 20 20 20 20 69 66 28 20 70 46 2d 3e    }.    if( pF->
2addb 69 44 69 73 74 69 6e 63 74 3e 3d 30 20 29 7b 0a  iDistinct>=0 ){.
2addc 20 20 20 20 20 20 61 64 64 72 4e 65 78 74 20 3d        addrNext =
2addd 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
2adde 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20  Label(v);.      
2addf 61 73 73 65 72 74 28 20 6e 41 72 67 3d 3d 31 20  assert( nArg==1 
2ade0 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 44 69 73  );.      codeDis
2ade1 74 69 6e 63 74 28 70 50 61 72 73 65 2c 20 70 46  tinct(pParse, pF
2ade2 2d 3e 69 44 69 73 74 69 6e 63 74 2c 20 61 64 64  ->iDistinct, add
2ade3 72 4e 65 78 74 2c 20 31 2c 20 72 65 67 41 67 67  rNext, 1, regAgg
2ade4 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
2ade5 20 70 46 2d 3e 70 46 75 6e 63 2d 3e 66 6c 61 67   pF->pFunc->flag
2ade6 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f  s & SQLITE_FUNC_
2ade7 4e 45 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20  NEEDCOLL ){.    
2ade8 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
2ade9 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 74 72 75   = 0;.      stru
2adea 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d  ct ExprList_item
2adeb 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 20 20 69   *pItem;.      i
2adec 6e 74 20 6a 3b 0a 20 20 20 20 20 20 61 73 73 65  nt j;.      asse
2aded 72 74 28 20 70 4c 69 73 74 21 3d 30 20 29 3b 20  rt( pList!=0 ); 
2adee 20 2f 2a 20 70 4c 69 73 74 21 3d 30 20 69 66 20   /* pList!=0 if 
2adef 70 46 2d 3e 70 46 75 6e 63 20 68 61 73 20 4e 45  pF->pFunc has NE
2adf0 45 44 43 4f 4c 4c 20 2a 2f 0a 20 20 20 20 20 20  EDCOLL */.      
2adf1 66 6f 72 28 6a 3d 30 2c 20 70 49 74 65 6d 3d 70  for(j=0, pItem=p
2adf2 4c 69 73 74 2d 3e 61 3b 20 21 70 43 6f 6c 6c 20  List->a; !pColl 
2adf3 26 26 20 6a 3c 6e 41 72 67 3b 20 6a 2b 2b 2c 20  && j<nArg; j++, 
2adf4 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20  pItem++){.      
2adf5 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    pColl = sqlite
2adf6 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61  3ExprCollSeq(pPa
2adf7 72 73 65 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70  rse, pItem->pExp
2adf8 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  r);.      }.    
2adf9 20 20 69 66 28 20 21 70 43 6f 6c 6c 20 29 7b 0a    if( !pColl ){.
2adfa 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20          pColl = 
2adfb 70 50 61 72 73 65 2d 3e 64 62 2d 3e 70 44 66 6c  pParse->db->pDfl
2adfc 74 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20  tColl;.      }. 
2adfd 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2adfe 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 43 6f 6c  AddOp4(v, OP_Col
2adff 6c 53 65 71 2c 20 30 2c 20 30 2c 20 30 2c 20 28  lSeq, 0, 0, 0, (
2ae00 63 68 61 72 20 2a 29 70 43 6f 6c 6c 2c 20 50 34  char *)pColl, P4
2ae01 5f 43 4f 4c 4c 53 45 51 29 3b 0a 20 20 20 20 7d  _COLLSEQ);.    }
2ae02 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2ae03 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 41 67 67  AddOp4(v, OP_Agg
2ae04 53 74 65 70 2c 20 30 2c 20 72 65 67 41 67 67 2c  Step, 0, regAgg,
2ae05 20 70 46 2d 3e 69 4d 65 6d 2c 0a 20 20 20 20 20   pF->iMem,.     
2ae06 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ae07 20 28 76 6f 69 64 2a 29 70 46 2d 3e 70 46 75 6e   (void*)pF->pFun
2ae08 63 2c 20 50 34 5f 46 55 4e 43 44 45 46 29 3b 0a  c, P4_FUNCDEF);.
2ae09 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
2ae0a 68 61 6e 67 65 50 35 28 76 2c 20 28 75 38 29 6e  hangeP5(v, (u8)n
2ae0b 41 72 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Arg);.    sqlite
2ae0c 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67  3ReleaseTempRang
2ae0d 65 28 70 50 61 72 73 65 2c 20 72 65 67 41 67 67  e(pParse, regAgg
2ae0e 2c 20 6e 41 72 67 29 3b 0a 20 20 20 20 73 71 6c  , nArg);.    sql
2ae0f 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66  ite3ExprCacheAff
2ae10 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72  inityChange(pPar
2ae11 73 65 2c 20 72 65 67 41 67 67 2c 20 6e 41 72 67  se, regAgg, nArg
2ae12 29 3b 0a 20 20 20 20 69 66 28 20 61 64 64 72 4e  );.    if( addrN
2ae13 65 78 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ext ){.      sql
2ae14 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
2ae15 61 62 65 6c 28 76 2c 20 61 64 64 72 4e 65 78 74  abel(v, addrNext
2ae16 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2ae17 45 78 70 72 43 61 63 68 65 43 6c 65 61 72 28 70  ExprCacheClear(p
2ae18 50 61 72 73 65 29 3b 0a 20 20 20 20 7d 0a 20 20  Parse);.    }.  
2ae19 7d 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 43 3d  }.  for(i=0, pC=
2ae1a 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 3b 20  pAggInfo->aCol; 
2ae1b 69 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 41 63 63  i<pAggInfo->nAcc
2ae1c 75 6d 75 6c 61 74 6f 72 3b 20 69 2b 2b 2c 20 70  umulator; i++, p
2ae1d 43 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  C++){.    sqlite
2ae1e 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
2ae1f 2c 20 70 43 2d 3e 70 45 78 70 72 2c 20 70 43 2d  , pC->pExpr, pC-
2ae20 3e 69 4d 65 6d 29 3b 0a 20 20 7d 0a 20 20 70 41  >iMem);.  }.  pA
2ae21 67 67 49 6e 66 6f 2d 3e 64 69 72 65 63 74 4d 6f  ggInfo->directMo
2ae22 64 65 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65  de = 0;.  sqlite
2ae23 33 45 78 70 72 43 61 63 68 65 43 6c 65 61 72 28  3ExprCacheClear(
2ae24 70 50 61 72 73 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  pParse);.}../*.*
2ae25 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
2ae26 66 6f 72 20 74 68 65 20 53 45 4c 45 43 54 20 73  for the SELECT s
2ae27 74 61 74 65 6d 65 6e 74 20 67 69 76 65 6e 20 69  tatement given i
2ae28 6e 20 74 68 65 20 70 20 61 72 67 75 6d 65 6e 74  n the p argument
2ae29 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65  .  .**.** The re
2ae2a 73 75 6c 74 73 20 61 72 65 20 64 69 73 74 72 69  sults are distri
2ae2b 62 75 74 65 64 20 69 6e 20 76 61 72 69 6f 75 73  buted in various
2ae2c 20 77 61 79 73 20 64 65 70 65 6e 64 69 6e 67 20   ways depending 
2ae2d 6f 6e 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e  on the.** conten
2ae2e 74 73 20 6f 66 20 74 68 65 20 53 65 6c 65 63 74  ts of the Select
2ae2f 44 65 73 74 20 73 74 72 75 63 74 75 72 65 20 70  Dest structure p
2ae30 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 61 72 67  ointed to by arg
2ae31 75 6d 65 6e 74 20 70 44 65 73 74 0a 2a 2a 20 61  ument pDest.** a
2ae32 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
2ae33 20 20 20 20 20 70 44 65 73 74 2d 3e 65 44 65 73       pDest->eDes
2ae34 74 20 20 20 20 52 65 73 75 6c 74 0a 2a 2a 20 20  t    Result.**  
2ae35 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20     ------------ 
2ae36 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     -------------
2ae37 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2ae38 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a  --------------.*
2ae39 2a 20 20 20 20 20 53 52 54 5f 4f 75 74 70 75 74  *     SRT_Output
2ae3a 20 20 20 20 20 20 47 65 6e 65 72 61 74 65 20 61        Generate a
2ae3b 20 72 6f 77 20 6f 66 20 6f 75 74 70 75 74 20 28   row of output (
2ae3c 75 73 69 6e 67 20 74 68 65 20 4f 50 5f 52 65 73  using the OP_Res
2ae3d 75 6c 74 52 6f 77 0a 2a 2a 20 20 20 20 20 20 20  ultRow.**       
2ae3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 70                op
2ae3f 63 6f 64 65 29 20 66 6f 72 20 65 61 63 68 20 72  code) for each r
2ae40 6f 77 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  ow in the result
2ae41 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20   set..**.**     
2ae42 53 52 54 5f 4d 65 6d 20 20 20 20 20 20 20 20 20  SRT_Mem         
2ae43 4f 6e 6c 79 20 76 61 6c 69 64 20 69 66 20 74 68  Only valid if th
2ae44 65 20 72 65 73 75 6c 74 20 69 73 20 61 20 73 69  e result is a si
2ae45 6e 67 6c 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20  ngle column..** 
2ae46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ae47 20 20 20 20 53 74 6f 72 65 20 74 68 65 20 66 69      Store the fi
2ae48 72 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  rst column of th
2ae49 65 20 66 69 72 73 74 20 72 65 73 75 6c 74 20 72  e first result r
2ae4a 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ow.**           
2ae4b 20 20 20 20 20 20 20 20 20 20 69 6e 20 72 65 67            in reg
2ae4c 69 73 74 65 72 20 70 44 65 73 74 2d 3e 69 50 61  ister pDest->iPa
2ae4d 72 6d 20 74 68 65 6e 20 61 62 61 6e 64 6f 6e 20  rm then abandon 
2ae4e 74 68 65 20 72 65 73 74 0a 2a 2a 20 20 20 20 20  the rest.**     
2ae4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ae50 6f 66 20 74 68 65 20 71 75 65 72 79 2e 20 20 54  of the query.  T
2ae51 68 69 73 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  his destination 
2ae52 69 6d 70 6c 69 65 73 20 22 4c 49 4d 49 54 20 31  implies "LIMIT 1
2ae53 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54  "..**.**     SRT
2ae54 5f 53 65 74 20 20 20 20 20 20 20 20 20 54 68 65  _Set         The
2ae55 20 72 65 73 75 6c 74 20 6d 75 73 74 20 62 65 20   result must be 
2ae56 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 2e  a single column.
2ae57 20 20 53 74 6f 72 65 20 65 61 63 68 0a 2a 2a 20    Store each.** 
2ae58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ae59 20 20 20 20 72 6f 77 20 6f 66 20 72 65 73 75 6c      row of resul
2ae5a 74 20 61 73 20 74 68 65 20 6b 65 79 20 69 6e 20  t as the key in 
2ae5b 74 61 62 6c 65 20 70 44 65 73 74 2d 3e 69 50 61  table pDest->iPa
2ae5c 72 6d 2e 20 0a 2a 2a 20 20 20 20 20 20 20 20 20  rm. .**         
2ae5d 20 20 20 20 20 20 20 20 20 20 20 20 41 70 70 6c              Appl
2ae5e 79 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 70  y the affinity p
2ae5f 44 65 73 74 2d 3e 61 66 66 69 6e 69 74 79 20 62  Dest->affinity b
2ae60 65 66 6f 72 65 20 73 74 6f 72 69 6e 67 0a 2a 2a  efore storing.**
2ae61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ae62 20 20 20 20 20 72 65 73 75 6c 74 73 2e 20 20 55       results.  U
2ae63 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  sed to implement
2ae64 20 22 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e   "IN (SELECT ...
2ae65 29 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52  )"..**.**     SR
2ae66 54 5f 55 6e 69 6f 6e 20 20 20 20 20 20 20 53 74  T_Union       St
2ae67 6f 72 65 20 72 65 73 75 6c 74 73 20 61 73 20 61  ore results as a
2ae68 20 6b 65 79 20 69 6e 20 61 20 74 65 6d 70 6f 72   key in a tempor
2ae69 61 72 79 20 74 61 62 6c 65 20 70 44 65 73 74 2d  ary table pDest-
2ae6a 3e 69 50 61 72 6d 2e 0a 2a 2a 0a 2a 2a 20 20 20  >iParm..**.**   
2ae6b 20 20 53 52 54 5f 45 78 63 65 70 74 20 20 20 20    SRT_Except    
2ae6c 20 20 52 65 6d 6f 76 65 20 72 65 73 75 6c 74 73    Remove results
2ae6d 20 66 72 6f 6d 20 74 68 65 20 74 65 6d 70 6f 72   from the tempor
2ae6e 61 72 79 20 74 61 62 6c 65 20 70 44 65 73 74 2d  ary table pDest-
2ae6f 3e 69 50 61 72 6d 2e 0a 2a 2a 0a 2a 2a 20 20 20  >iParm..**.**   
2ae70 20 20 53 52 54 5f 54 61 62 6c 65 20 20 20 20 20    SRT_Table     
2ae71 20 20 53 74 6f 72 65 20 72 65 73 75 6c 74 73 20    Store results 
2ae72 69 6e 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  in temporary tab
2ae73 6c 65 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 2e  le pDest->iParm.
2ae74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
2ae75 20 20 20 20 20 20 20 20 54 68 69 73 20 69 73 20          This is 
2ae76 6c 69 6b 65 20 53 52 54 5f 45 70 68 65 6d 54 61  like SRT_EphemTa
2ae77 62 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68  b except that th
2ae78 65 20 74 61 62 6c 65 0a 2a 2a 20 20 20 20 20 20  e table.**      
2ae79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
2ae7a 73 20 61 73 73 75 6d 65 64 20 74 6f 20 61 6c 72  s assumed to alr
2ae7b 65 61 64 79 20 62 65 20 6f 70 65 6e 2e 0a 2a 2a  eady be open..**
2ae7c 0a 2a 2a 20 20 20 20 20 53 52 54 5f 45 70 68 65  .**     SRT_Ephe
2ae7d 6d 54 61 62 20 20 20 20 43 72 65 61 74 65 20 61  mTab    Create a
2ae7e 6e 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  n temporary tabl
2ae7f 65 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 20 61  e pDest->iParm a
2ae80 6e 64 20 73 74 6f 72 65 0a 2a 2a 20 20 20 20 20  nd store.**     
2ae81 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ae82 74 68 65 20 72 65 73 75 6c 74 20 74 68 65 72 65  the result there
2ae83 2e 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20  . The cursor is 
2ae84 6c 65 66 74 20 6f 70 65 6e 20 61 66 74 65 72 0a  left open after.
2ae85 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
2ae86 20 20 20 20 20 20 20 72 65 74 75 72 6e 69 6e 67         returning
2ae87 2e 20 20 54 68 69 73 20 69 73 20 6c 69 6b 65 20  .  This is like 
2ae88 53 52 54 5f 54 61 62 6c 65 20 65 78 63 65 70 74  SRT_Table except
2ae89 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20   that.**        
2ae8a 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 69               thi
2ae8b 73 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 75 73  s destination us
2ae8c 65 73 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  es OP_OpenEpheme
2ae8d 72 61 6c 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a  ral to create.**
2ae8e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ae8f 20 20 20 20 20 74 68 65 20 74 61 62 6c 65 20 66       the table f
2ae90 69 72 73 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  irst..**.**     
2ae91 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 20 20 20  SRT_Coroutine   
2ae92 47 65 6e 65 72 61 74 65 20 61 20 63 6f 2d 72 6f  Generate a co-ro
2ae93 75 74 69 6e 65 20 74 68 61 74 20 72 65 74 75 72  utine that retur
2ae94 6e 73 20 61 20 6e 65 77 20 72 6f 77 20 6f 66 0a  ns a new row of.
2ae95 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
2ae96 20 20 20 20 20 20 20 72 65 73 75 6c 74 73 20 65         results e
2ae97 61 63 68 20 74 69 6d 65 20 69 74 20 69 73 20 69  ach time it is i
2ae98 6e 76 6f 6b 65 64 2e 20 20 54 68 65 20 65 6e 74  nvoked.  The ent
2ae99 72 79 20 70 6f 69 6e 74 0a 2a 2a 20 20 20 20 20  ry point.**     
2ae9a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ae9b 6f 66 20 74 68 65 20 63 6f 2d 72 6f 75 74 69 6e  of the co-routin
2ae9c 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72  e is stored in r
2ae9d 65 67 69 73 74 65 72 20 70 44 65 73 74 2d 3e 69  egister pDest->i
2ae9e 50 61 72 6d 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  Parm..**.**     
2ae9f 53 52 54 5f 45 78 69 73 74 73 20 20 20 20 20 20  SRT_Exists      
2aea0 53 74 6f 72 65 20 61 20 31 20 69 6e 20 6d 65 6d  Store a 1 in mem
2aea1 6f 72 79 20 63 65 6c 6c 20 70 44 65 73 74 2d 3e  ory cell pDest->
2aea2 69 50 61 72 6d 20 69 66 20 74 68 65 20 72 65 73  iParm if the res
2aea3 75 6c 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ult.**          
2aea4 20 20 20 20 20 20 20 20 20 20 20 73 65 74 20 69             set i
2aea5 73 20 6e 6f 74 20 65 6d 70 74 79 2e 0a 2a 2a 0a  s not empty..**.
2aea6 2a 2a 20 20 20 20 20 53 52 54 5f 44 69 73 63 61  **     SRT_Disca
2aea7 72 64 20 20 20 20 20 54 68 72 6f 77 20 74 68 65  rd     Throw the
2aea8 20 72 65 73 75 6c 74 73 20 61 77 61 79 2e 20 20   results away.  
2aea9 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20  This is used by 
2aeaa 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20  SELECT.**       
2aeab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74                st
2aeac 61 74 65 6d 65 6e 74 73 20 77 69 74 68 69 6e 20  atements within 
2aead 74 72 69 67 67 65 72 73 20 77 68 6f 73 65 20 6f  triggers whose o
2aeae 6e 6c 79 20 70 75 72 70 6f 73 65 20 69 73 0a 2a  nly purpose is.*
2aeaf 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
2aeb0 20 20 20 20 20 20 74 68 65 20 73 69 64 65 2d 65        the side-e
2aeb1 66 66 65 63 74 73 20 6f 66 20 66 75 6e 63 74 69  ffects of functi
2aeb2 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ons..**.** This 
2aeb3 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
2aeb4 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72  the number of er
2aeb5 72 6f 72 73 2e 20 20 49 66 20 61 6e 79 20 65 72  rors.  If any er
2aeb6 72 6f 72 73 20 61 72 65 0a 2a 2a 20 65 6e 63 6f  rors are.** enco
2aeb7 75 6e 74 65 72 65 64 2c 20 74 68 65 6e 20 61 6e  untered, then an
2aeb8 20 61 70 70 72 6f 70 72 69 61 74 65 20 65 72 72   appropriate err
2aeb9 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20 6c 65  or message is le
2aeba 66 74 20 69 6e 0a 2a 2a 20 70 50 61 72 73 65 2d  ft in.** pParse-
2aebb 3e 7a 45 72 72 4d 73 67 2e 0a 2a 2a 0a 2a 2a 20  >zErrMsg..**.** 
2aebc 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65  This routine doe
2aebd 73 20 4e 4f 54 20 66 72 65 65 20 74 68 65 20 53  s NOT free the S
2aebe 65 6c 65 63 74 20 73 74 72 75 63 74 75 72 65 20  elect structure 
2aebf 70 61 73 73 65 64 20 69 6e 2e 20 20 54 68 65 0a  passed in.  The.
2aec0 2a 2a 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74  ** calling funct
2aec1 69 6f 6e 20 6e 65 65 64 73 20 74 6f 20 64 6f 20  ion needs to do 
2aec2 74 68 61 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  that..*/.SQLITE_
2aec3 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
2aec4 74 65 33 53 65 6c 65 63 74 28 0a 20 20 50 61 72  te3Select(.  Par
2aec5 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
2aec6 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65      /* The parse
2aec7 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53  r context */.  S
2aec8 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20  elect *p,       
2aec9 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c        /* The SEL
2aeca 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 62 65  ECT statement be
2aecb 69 6e 67 20 63 6f 64 65 64 2e 20 2a 2f 0a 20 20  ing coded. */.  
2aecc 53 65 6c 65 63 74 44 65 73 74 20 2a 70 44 65 73  SelectDest *pDes
2aecd 74 20 20 20 20 20 20 2f 2a 20 57 68 61 74 20 74  t      /* What t
2aece 6f 20 64 6f 20 77 69 74 68 20 74 68 65 20 71 75  o do with the qu
2aecf 65 72 79 20 72 65 73 75 6c 74 73 20 2a 2f 0a 29  ery results */.)
2aed0 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20  {.  int i, j;   
2aed1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
2aed2 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20  op counters */. 
2aed3 20 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e   WhereInfo *pWIn
2aed4 66 6f 3b 20 20 20 20 20 2f 2a 20 52 65 74 75 72  fo;     /* Retur
2aed5 6e 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 57 68  n from sqlite3Wh
2aed6 65 72 65 42 65 67 69 6e 28 29 20 2a 2f 0a 20 20  ereBegin() */.  
2aed7 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20  Vdbe *v;        
2aed8 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69         /* The vi
2aed9 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 75 6e  rtual machine un
2aeda 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e  der construction
2aedb 20 2a 2f 0a 20 20 69 6e 74 20 69 73 41 67 67 3b   */.  int isAgg;
2aedc 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2aedd 54 72 75 65 20 66 6f 72 20 73 65 6c 65 63 74 20  True for select 
2aede 6c 69 73 74 73 20 6c 69 6b 65 20 22 63 6f 75 6e  lists like "coun
2aedf 74 28 2a 29 22 20 2a 2f 0a 20 20 45 78 70 72 4c  t(*)" */.  ExprL
2aee0 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 20  ist *pEList;    
2aee1 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 63 6f 6c    /* List of col
2aee2 75 6d 6e 73 20 74 6f 20 65 78 74 72 61 63 74 2e  umns to extract.
2aee3 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70   */.  SrcList *p
2aee4 54 61 62 4c 69 73 74 3b 20 20 20 20 20 2f 2a 20  TabList;     /* 
2aee5 4c 69 73 74 20 6f 66 20 74 61 62 6c 65 73 20 74  List of tables t
2aee6 6f 20 73 65 6c 65 63 74 20 66 72 6f 6d 20 2a 2f  o select from */
2aee7 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 3b  .  Expr *pWhere;
2aee8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2aee9 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20   WHERE clause.  
2aeea 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20  May be NULL */. 
2aeeb 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65   ExprList *pOrde
2aeec 72 42 79 3b 20 20 20 20 2f 2a 20 54 68 65 20 4f  rBy;    /* The O
2aeed 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 2e 20  RDER BY clause. 
2aeee 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a   May be NULL */.
2aeef 20 20 45 78 70 72 4c 69 73 74 20 2a 70 47 72 6f    ExprList *pGro
2aef0 75 70 42 79 3b 20 20 20 20 2f 2a 20 54 68 65 20  upBy;    /* The 
2aef1 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2e  GROUP BY clause.
2aef2 20 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f    May be NULL */
2aef3 0a 20 20 45 78 70 72 20 2a 70 48 61 76 69 6e 67  .  Expr *pHaving
2aef4 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ;         /* The
2aef5 20 48 41 56 49 4e 47 20 63 6c 61 75 73 65 2e 20   HAVING clause. 
2aef6 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a   May be NULL */.
2aef7 20 20 69 6e 74 20 69 73 44 69 73 74 69 6e 63 74    int isDistinct
2aef8 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65  ;        /* True
2aef9 20 69 66 20 74 68 65 20 44 49 53 54 49 4e 43 54   if the DISTINCT
2aefa 20 6b 65 79 77 6f 72 64 20 69 73 20 70 72 65 73   keyword is pres
2aefb 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 64 69 73  ent */.  int dis
2aefc 74 69 6e 63 74 3b 20 20 20 20 20 20 20 20 20 20  tinct;          
2aefd 2f 2a 20 54 61 62 6c 65 20 74 6f 20 75 73 65 20  /* Table to use 
2aefe 66 6f 72 20 74 68 65 20 64 69 73 74 69 6e 63 74  for the distinct
2aeff 20 73 65 74 20 2a 2f 0a 20 20 69 6e 74 20 72 63   set */.  int rc
2af00 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 20   = 1;           
2af01 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74   /* Value to ret
2af02 75 72 6e 20 66 72 6f 6d 20 74 68 69 73 20 66 75  urn from this fu
2af03 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  nction */.  int 
2af04 61 64 64 72 53 6f 72 74 49 6e 64 65 78 3b 20 20  addrSortIndex;  
2af05 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66     /* Address of
2af06 20 61 6e 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d   an OP_OpenEphem
2af07 65 72 61 6c 20 69 6e 73 74 72 75 63 74 69 6f 6e  eral instruction
2af08 20 2a 2f 0a 20 20 41 67 67 49 6e 66 6f 20 73 41   */.  AggInfo sA
2af09 67 67 49 6e 66 6f 3b 20 20 20 20 20 20 2f 2a 20  ggInfo;      /* 
2af0a 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64  Information used
2af0b 20 62 79 20 61 67 67 72 65 67 61 74 65 20 71 75   by aggregate qu
2af0c 65 72 69 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69  eries */.  int i
2af0d 45 6e 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  End;            
2af0e 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20    /* Address of 
2af0f 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 71  the end of the q
2af10 75 65 72 79 20 2a 2f 0a 20 20 73 71 6c 69 74 65  uery */.  sqlite
2af11 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20  3 *db;          
2af12 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
2af13 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a   connection */..
2af14 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64    db = pParse->d
2af15 62 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c  b;.  if( p==0 ||
2af16 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2af17 64 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72  d || pParse->nEr
2af18 72 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  r ){.    return 
2af19 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c  1;.  }.  if( sql
2af1a 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
2af1b 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 53 45 4c  arse, SQLITE_SEL
2af1c 45 43 54 2c 20 30 2c 20 30 2c 20 30 29 20 29 20  ECT, 0, 0, 0) ) 
2af1d 72 65 74 75 72 6e 20 31 3b 0a 20 20 6d 65 6d 73  return 1;.  mems
2af1e 65 74 28 26 73 41 67 67 49 6e 66 6f 2c 20 30 2c  et(&sAggInfo, 0,
2af1f 20 73 69 7a 65 6f 66 28 73 41 67 67 49 6e 66 6f   sizeof(sAggInfo
2af20 29 29 3b 0a 0a 20 20 69 66 28 20 49 67 6e 6f 72  ));..  if( Ignor
2af21 61 62 6c 65 4f 72 64 65 72 62 79 28 70 44 65 73  ableOrderby(pDes
2af22 74 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  t) ){.    assert
2af23 28 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53  (pDest->eDest==S
2af24 52 54 5f 45 78 69 73 74 73 20 7c 7c 20 70 44 65  RT_Exists || pDe
2af25 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 55  st->eDest==SRT_U
2af26 6e 69 6f 6e 20 7c 7c 20 0a 20 20 20 20 20 20 20  nion || .       
2af27 20 20 20 20 70 44 65 73 74 2d 3e 65 44 65 73 74      pDest->eDest
2af28 3d 3d 53 52 54 5f 45 78 63 65 70 74 20 7c 7c 20  ==SRT_Except || 
2af29 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52  pDest->eDest==SR
2af2a 54 5f 44 69 73 63 61 72 64 29 3b 0a 20 20 20 20  T_Discard);.    
2af2b 2f 2a 20 49 66 20 4f 52 44 45 52 20 42 59 20 6d  /* If ORDER BY m
2af2c 61 6b 65 73 20 6e 6f 20 64 69 66 66 65 72 65 6e  akes no differen
2af2d 63 65 20 69 6e 20 74 68 65 20 6f 75 74 70 75 74  ce in the output
2af2e 20 74 68 65 6e 20 6e 65 69 74 68 65 72 20 64 6f   then neither do
2af2f 65 73 0a 20 20 20 20 2a 2a 20 44 49 53 54 49 4e  es.    ** DISTIN
2af30 43 54 20 73 6f 20 69 74 20 63 61 6e 20 62 65 20  CT so it can be 
2af31 72 65 6d 6f 76 65 64 20 74 6f 6f 2e 20 2a 2f 0a  removed too. */.
2af32 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c      sqlite3ExprL
2af33 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d  istDelete(db, p-
2af34 3e 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 20 20  >pOrderBy);.    
2af35 70 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 30 3b  p->pOrderBy = 0;
2af36 0a 20 20 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73  .    p->selFlags
2af37 20 26 3d 20 7e 53 46 5f 44 69 73 74 69 6e 63 74   &= ~SF_Distinct
2af38 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 53  ;.  }.  sqlite3S
2af39 65 6c 65 63 74 50 72 65 70 28 70 50 61 72 73 65  electPrep(pParse
2af3a 2c 20 70 2c 20 30 29 3b 0a 20 20 70 4f 72 64 65  , p, 0);.  pOrde
2af3b 72 42 79 20 3d 20 70 2d 3e 70 4f 72 64 65 72 42  rBy = p->pOrderB
2af3c 79 3b 0a 20 20 70 54 61 62 4c 69 73 74 20 3d 20  y;.  pTabList = 
2af3d 70 2d 3e 70 53 72 63 3b 0a 20 20 70 45 4c 69 73  p->pSrc;.  pELis
2af3e 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20  t = p->pEList;. 
2af3f 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72   if( pParse->nEr
2af40 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  r || db->mallocF
2af41 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74  ailed ){.    got
2af42 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20  o select_end;.  
2af43 7d 0a 20 20 69 73 41 67 67 20 3d 20 28 70 2d 3e  }.  isAgg = (p->
2af44 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 41 67  selFlags & SF_Ag
2af45 67 72 65 67 61 74 65 29 21 3d 30 3b 0a 20 20 61  gregate)!=0;.  a
2af46 73 73 65 72 74 28 20 70 45 4c 69 73 74 21 3d 30  ssert( pEList!=0
2af47 20 29 3b 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20   );..  /* Begin 
2af48 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 2e  generating code.
2af49 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69  .  */.  v = sqli
2af4a 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
2af4b 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29  e);.  if( v==0 )
2af4c 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64   goto select_end
2af4d 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65  ;..  /* Generate
2af4e 20 63 6f 64 65 20 66 6f 72 20 61 6c 6c 20 73 75   code for all su
2af4f 62 2d 71 75 65 72 69 65 73 20 69 6e 20 74 68 65  b-queries in the
2af50 20 46 52 4f 4d 20 63 6c 61 75 73 65 0a 20 20 2a   FROM clause.  *
2af51 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  /.#if !defined(S
2af52 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
2af53 45 52 59 29 20 7c 7c 20 21 64 65 66 69 6e 65 64  ERY) || !defined
2af54 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45  (SQLITE_OMIT_VIE
2af55 57 29 0a 20 20 66 6f 72 28 69 3d 30 3b 20 21 70  W).  for(i=0; !p
2af56 2d 3e 70 50 72 69 6f 72 20 26 26 20 69 3c 70 54  ->pPrior && i<pT
2af57 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b  abList->nSrc; i+
2af58 2b 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 53  +){.    struct S
2af59 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  rcList_item *pIt
2af5a 65 6d 20 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e  em = &pTabList->
2af5b 61 5b 69 5d 3b 0a 20 20 20 20 53 65 6c 65 63 74  a[i];.    Select
2af5c 44 65 73 74 20 64 65 73 74 3b 0a 20 20 20 20 53  Dest dest;.    S
2af5d 65 6c 65 63 74 20 2a 70 53 75 62 20 3d 20 70 49  elect *pSub = pI
2af5e 74 65 6d 2d 3e 70 53 65 6c 65 63 74 3b 0a 20 20  tem->pSelect;.  
2af5f 20 20 69 6e 74 20 69 73 41 67 67 53 75 62 3b 0a    int isAggSub;.
2af60 0a 20 20 20 20 69 66 28 20 70 53 75 62 3d 3d 30  .    if( pSub==0
2af61 20 7c 7c 20 70 49 74 65 6d 2d 3e 69 73 50 6f 70   || pItem->isPop
2af62 75 6c 61 74 65 64 20 29 20 63 6f 6e 74 69 6e 75  ulated ) continu
2af63 65 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 63 72 65  e;..    /* Incre
2af64 6d 65 6e 74 20 50 61 72 73 65 2e 6e 48 65 69 67  ment Parse.nHeig
2af65 68 74 20 62 79 20 74 68 65 20 68 65 69 67 68 74  ht by the height
2af66 20 6f 66 20 74 68 65 20 6c 61 72 67 65 73 74 20   of the largest 
2af67 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 20 20 2a  expression.    *
2af68 2a 20 74 72 65 65 20 72 65 66 65 72 65 64 20 74  * tree refered t
2af69 6f 20 62 79 20 74 68 69 73 2c 20 74 68 65 20 70  o by this, the p
2af6a 61 72 65 6e 74 20 73 65 6c 65 63 74 2e 20 54 68  arent select. Th
2af6b 65 20 63 68 69 6c 64 20 73 65 6c 65 63 74 0a 20  e child select. 
2af6c 20 20 20 2a 2a 20 6d 61 79 20 63 6f 6e 74 61 69     ** may contai
2af6d 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65  n expression tre
2af6e 65 73 20 6f 66 20 61 74 20 6d 6f 73 74 0a 20 20  es of at most.  
2af6f 20 20 2a 2a 20 28 53 51 4c 49 54 45 5f 4d 41 58    ** (SQLITE_MAX
2af70 5f 45 58 50 52 5f 44 45 50 54 48 2d 50 61 72 73  _EXPR_DEPTH-Pars
2af71 65 2e 6e 48 65 69 67 68 74 29 20 68 65 69 67 68  e.nHeight) heigh
2af72 74 2e 20 54 68 69 73 20 69 73 20 61 20 62 69 74  t. This is a bit
2af73 0a 20 20 20 20 2a 2a 20 6d 6f 72 65 20 63 6f 6e  .    ** more con
2af74 73 65 72 76 61 74 69 76 65 20 74 68 61 6e 20 6e  servative than n
2af75 65 63 65 73 73 61 72 79 2c 20 62 75 74 20 6d 75  ecessary, but mu
2af76 63 68 20 65 61 73 69 65 72 20 74 68 61 6e 20 65  ch easier than e
2af77 6e 66 6f 72 63 69 6e 67 0a 20 20 20 20 2a 2a 20  nforcing.    ** 
2af78 61 6e 20 65 78 61 63 74 20 6c 69 6d 69 74 2e 0a  an exact limit..
2af79 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 61 72 73      */.    pPars
2af7a 65 2d 3e 6e 48 65 69 67 68 74 20 2b 3d 20 73 71  e->nHeight += sq
2af7b 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 72 48  lite3SelectExprH
2af7c 65 69 67 68 74 28 70 29 3b 0a 0a 20 20 20 20 2f  eight(p);..    /
2af7d 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69  * Check to see i
2af7e 66 20 74 68 65 20 73 75 62 71 75 65 72 79 20 63  f the subquery c
2af7f 61 6e 20 62 65 20 61 62 73 6f 72 62 65 64 20 69  an be absorbed i
2af80 6e 74 6f 20 74 68 65 20 70 61 72 65 6e 74 2e 20  nto the parent. 
2af81 2a 2f 0a 20 20 20 20 69 73 41 67 67 53 75 62 20  */.    isAggSub 
2af82 3d 20 28 70 53 75 62 2d 3e 73 65 6c 46 6c 61 67  = (pSub->selFlag
2af83 73 20 26 20 53 46 5f 41 67 67 72 65 67 61 74 65  s & SF_Aggregate
2af84 29 21 3d 30 3b 0a 20 20 20 20 69 66 28 20 66 6c  )!=0;.    if( fl
2af85 61 74 74 65 6e 53 75 62 71 75 65 72 79 28 70 50  attenSubquery(pP
2af86 61 72 73 65 2c 20 70 2c 20 69 2c 20 69 73 41 67  arse, p, i, isAg
2af87 67 2c 20 69 73 41 67 67 53 75 62 29 20 29 7b 0a  g, isAggSub) ){.
2af88 20 20 20 20 20 20 69 66 28 20 69 73 41 67 67 53        if( isAggS
2af89 75 62 20 29 7b 0a 20 20 20 20 20 20 20 20 69 73  ub ){.        is
2af8a 41 67 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  Agg = 1;.       
2af8b 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20   p->selFlags |= 
2af8c 53 46 5f 41 67 67 72 65 67 61 74 65 3b 0a 20 20  SF_Aggregate;.  
2af8d 20 20 20 20 7d 0a 20 20 20 20 20 20 69 20 3d 20      }.      i = 
2af8e 2d 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  -1;.    }else{. 
2af8f 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65       sqlite3Sele
2af90 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74  ctDestInit(&dest
2af91 2c 20 53 52 54 5f 45 70 68 65 6d 54 61 62 2c 20  , SRT_EphemTab, 
2af92 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 29 3b  pItem->iCursor);
2af93 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
2af94 49 74 65 6d 2d 3e 69 73 50 6f 70 75 6c 61 74 65  Item->isPopulate
2af95 64 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71  d==0 );.      sq
2af96 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72  lite3Select(pPar
2af97 73 65 2c 20 70 53 75 62 2c 20 26 64 65 73 74 29  se, pSub, &dest)
2af98 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69  ;.      pItem->i
2af99 73 50 6f 70 75 6c 61 74 65 64 20 3d 20 31 3b 0a  sPopulated = 1;.
2af9a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 2f 2a      }.    if( /*
2af9b 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 2a  pParse->nErr ||*
2af9c 2f 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  / db->mallocFail
2af9d 65 64 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  ed ){.      goto
2af9e 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20   select_end;.   
2af9f 20 7d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e   }.    pParse->n
2afa0 48 65 69 67 68 74 20 2d 3d 20 73 71 6c 69 74 65  Height -= sqlite
2afa1 33 53 65 6c 65 63 74 45 78 70 72 48 65 69 67 68  3SelectExprHeigh
2afa2 74 28 70 29 3b 0a 20 20 20 20 70 54 61 62 4c 69  t(p);.    pTabLi
2afa3 73 74 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20  st = p->pSrc;.  
2afa4 20 20 69 66 28 20 21 49 67 6e 6f 72 61 62 6c 65    if( !Ignorable
2afa5 4f 72 64 65 72 62 79 28 70 44 65 73 74 29 20 29  Orderby(pDest) )
2afa6 7b 0a 20 20 20 20 20 20 70 4f 72 64 65 72 42 79  {.      pOrderBy
2afa7 20 3d 20 70 2d 3e 70 4f 72 64 65 72 42 79 3b 0a   = p->pOrderBy;.
2afa8 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 45 4c 69      }.  }.  pELi
2afa9 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a  st = p->pEList;.
2afaa 23 65 6e 64 69 66 0a 20 20 70 57 68 65 72 65 20  #endif.  pWhere 
2afab 3d 20 70 2d 3e 70 57 68 65 72 65 3b 0a 20 20 70  = p->pWhere;.  p
2afac 47 72 6f 75 70 42 79 20 3d 20 70 2d 3e 70 47 72  GroupBy = p->pGr
2afad 6f 75 70 42 79 3b 0a 20 20 70 48 61 76 69 6e 67  oupBy;.  pHaving
2afae 20 3d 20 70 2d 3e 70 48 61 76 69 6e 67 3b 0a 20   = p->pHaving;. 
2afaf 20 69 73 44 69 73 74 69 6e 63 74 20 3d 20 28 70   isDistinct = (p
2afb0 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f  ->selFlags & SF_
2afb1 44 69 73 74 69 6e 63 74 29 21 3d 30 3b 0a 0a 23  Distinct)!=0;..#
2afb2 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2afb3 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45  IT_COMPOUND_SELE
2afb4 43 54 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65  CT.  /* If there
2afb5 20 69 73 20 61 72 65 20 61 20 73 65 71 75 65 6e   is are a sequen
2afb6 63 65 20 6f 66 20 71 75 65 72 69 65 73 2c 20 64  ce of queries, d
2afb7 6f 20 74 68 65 20 65 61 72 6c 69 65 72 20 6f 6e  o the earlier on
2afb8 65 73 20 66 69 72 73 74 2e 0a 20 20 2a 2f 0a 20  es first..  */. 
2afb9 20 69 66 28 20 70 2d 3e 70 50 72 69 6f 72 20 29   if( p->pPrior )
2afba 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 52 69  {.    if( p->pRi
2afbb 67 68 74 6d 6f 73 74 3d 3d 30 20 29 7b 0a 20 20  ghtmost==0 ){.  
2afbc 20 20 20 20 53 65 6c 65 63 74 20 2a 70 4c 6f 6f      Select *pLoo
2afbd 70 2c 20 2a 70 52 69 67 68 74 20 3d 20 30 3b 0a  p, *pRight = 0;.
2afbe 20 20 20 20 20 20 69 6e 74 20 63 6e 74 20 3d 20        int cnt = 
2afbf 30 3b 0a 20 20 20 20 20 20 69 6e 74 20 6d 78 53  0;.      int mxS
2afc0 65 6c 65 63 74 3b 0a 20 20 20 20 20 20 66 6f 72  elect;.      for
2afc1 28 70 4c 6f 6f 70 3d 70 3b 20 70 4c 6f 6f 70 3b  (pLoop=p; pLoop;
2afc2 20 70 4c 6f 6f 70 3d 70 4c 6f 6f 70 2d 3e 70 50   pLoop=pLoop->pP
2afc3 72 69 6f 72 2c 20 63 6e 74 2b 2b 29 7b 0a 20 20  rior, cnt++){.  
2afc4 20 20 20 20 20 20 70 4c 6f 6f 70 2d 3e 70 52 69        pLoop->pRi
2afc5 67 68 74 6d 6f 73 74 20 3d 20 70 3b 0a 20 20 20  ghtmost = p;.   
2afc6 20 20 20 20 20 70 4c 6f 6f 70 2d 3e 70 4e 65 78       pLoop->pNex
2afc7 74 20 3d 20 70 52 69 67 68 74 3b 0a 20 20 20 20  t = pRight;.    
2afc8 20 20 20 20 70 52 69 67 68 74 20 3d 20 70 4c 6f      pRight = pLo
2afc9 6f 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  op;.      }.    
2afca 20 20 6d 78 53 65 6c 65 63 74 20 3d 20 64 62 2d    mxSelect = db-
2afcb 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
2afcc 49 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45  IMIT_COMPOUND_SE
2afcd 4c 45 43 54 5d 3b 0a 20 20 20 20 20 20 69 66 28  LECT];.      if(
2afce 20 6d 78 53 65 6c 65 63 74 20 26 26 20 63 6e 74   mxSelect && cnt
2afcf 3e 6d 78 53 65 6c 65 63 74 20 29 7b 0a 20 20 20  >mxSelect ){.   
2afd0 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
2afd1 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f  rMsg(pParse, "to
2afd2 6f 20 6d 61 6e 79 20 74 65 72 6d 73 20 69 6e 20  o many terms in 
2afd3 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 22  compound SELECT"
2afd4 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  );.        retur
2afd5 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 1;.      }.   
2afd6 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 6d 75   }.    return mu
2afd7 6c 74 69 53 65 6c 65 63 74 28 70 50 61 72 73 65  ltiSelect(pParse
2afd8 2c 20 70 2c 20 70 44 65 73 74 29 3b 0a 20 20 7d  , p, pDest);.  }
2afd9 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66  .#endif..  /* If
2afda 20 77 72 69 74 69 6e 67 20 74 6f 20 6d 65 6d 6f   writing to memo
2afdb 72 79 20 6f 72 20 67 65 6e 65 72 61 74 69 6e 67  ry or generating
2afdc 20 61 20 73 65 74 0a 20 20 2a 2a 20 6f 6e 6c 79   a set.  ** only
2afdd 20 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e   a single column
2afde 20 6d 61 79 20 62 65 20 6f 75 74 70 75 74 2e 0a   may be output..
2afdf 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c    */.#ifndef SQL
2afe0 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
2afe1 59 0a 20 20 69 66 28 20 63 68 65 63 6b 46 6f 72  Y.  if( checkFor
2afe2 4d 75 6c 74 69 43 6f 6c 75 6d 6e 53 65 6c 65 63  MultiColumnSelec
2afe3 74 45 72 72 6f 72 28 70 50 61 72 73 65 2c 20 70  tError(pParse, p
2afe4 44 65 73 74 2c 20 70 45 4c 69 73 74 2d 3e 6e 45  Dest, pEList->nE
2afe5 78 70 72 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  xpr) ){.    goto
2afe6 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 7d   select_end;.  }
2afe7 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66  .#endif..  /* If
2afe8 20 70 6f 73 73 69 62 6c 65 2c 20 72 65 77 72 69   possible, rewri
2afe9 74 65 20 74 68 65 20 71 75 65 72 79 20 74 6f 20  te the query to 
2afea 75 73 65 20 47 52 4f 55 50 20 42 59 20 69 6e 73  use GROUP BY ins
2afeb 74 65 61 64 20 6f 66 20 44 49 53 54 49 4e 43 54  tead of DISTINCT
2afec 2e 0a 20 20 2a 2a 20 47 52 4f 55 50 20 42 59 20  ..  ** GROUP BY 
2afed 6d 69 67 68 74 20 75 73 65 20 61 6e 20 69 6e 64  might use an ind
2afee 65 78 2c 20 44 49 53 54 49 4e 43 54 20 6e 65 76  ex, DISTINCT nev
2afef 65 72 20 64 6f 65 73 2e 0a 20 20 2a 2f 0a 20 20  er does..  */.  
2aff0 61 73 73 65 72 74 28 20 70 2d 3e 70 47 72 6f 75  assert( p->pGrou
2aff1 70 42 79 3d 3d 30 20 7c 7c 20 28 70 2d 3e 73 65  pBy==0 || (p->se
2aff2 6c 46 6c 61 67 73 20 26 20 53 46 5f 41 67 67 72  lFlags & SF_Aggr
2aff3 65 67 61 74 65 29 21 3d 30 20 29 3b 0a 20 20 69  egate)!=0 );.  i
2aff4 66 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20  f( (p->selFlags 
2aff5 26 20 28 53 46 5f 44 69 73 74 69 6e 63 74 7c 53  & (SF_Distinct|S
2aff6 46 5f 41 67 67 72 65 67 61 74 65 29 29 3d 3d 53  F_Aggregate))==S
2aff7 46 5f 44 69 73 74 69 6e 63 74 20 29 7b 0a 20 20  F_Distinct ){.  
2aff8 20 20 70 2d 3e 70 47 72 6f 75 70 42 79 20 3d 20    p->pGroupBy = 
2aff9 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
2affa 75 70 28 64 62 2c 20 70 2d 3e 70 45 4c 69 73 74  up(db, p->pEList
2affb 2c 20 30 29 3b 0a 20 20 20 20 70 47 72 6f 75 70  , 0);.    pGroup
2affc 42 79 20 3d 20 70 2d 3e 70 47 72 6f 75 70 42 79  By = p->pGroupBy
2affd 3b 0a 20 20 20 20 70 2d 3e 73 65 6c 46 6c 61 67  ;.    p->selFlag
2affe 73 20 26 3d 20 7e 53 46 5f 44 69 73 74 69 6e 63  s &= ~SF_Distinc
2afff 74 3b 0a 20 20 20 20 69 73 44 69 73 74 69 6e 63  t;.    isDistinc
2b000 74 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  t = 0;.  }..  /*
2b001 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6e 20   If there is an 
2b002 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 2c  ORDER BY clause,
2b003 20 74 68 65 6e 20 74 68 69 73 20 73 6f 72 74 69   then this sorti
2b004 6e 67 0a 20 20 2a 2a 20 69 6e 64 65 78 20 6d 69  ng.  ** index mi
2b005 67 68 74 20 65 6e 64 20 75 70 20 62 65 69 6e 67  ght end up being
2b006 20 75 6e 75 73 65 64 20 69 66 20 74 68 65 20 64   unused if the d
2b007 61 74 61 20 63 61 6e 20 62 65 20 0a 20 20 2a 2a  ata can be .  **
2b008 20 65 78 74 72 61 63 74 65 64 20 69 6e 20 70 72   extracted in pr
2b009 65 2d 73 6f 72 74 65 64 20 6f 72 64 65 72 2e 20  e-sorted order. 
2b00a 20 49 66 20 74 68 61 74 20 69 73 20 74 68 65 20   If that is the 
2b00b 63 61 73 65 2c 20 74 68 65 6e 20 74 68 65 0a 20  case, then the. 
2b00c 20 2a 2a 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d   ** OP_OpenEphem
2b00d 65 72 61 6c 20 69 6e 73 74 72 75 63 74 69 6f 6e  eral instruction
2b00e 20 77 69 6c 6c 20 62 65 20 63 68 61 6e 67 65 64   will be changed
2b00f 20 74 6f 20 61 6e 20 4f 50 5f 4e 6f 6f 70 20 6f   to an OP_Noop o
2b010 6e 63 65 0a 20 20 2a 2a 20 77 65 20 66 69 67 75  nce.  ** we figu
2b011 72 65 20 6f 75 74 20 74 68 61 74 20 74 68 65 20  re out that the 
2b012 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 69 73  sorting index is
2b013 20 6e 6f 74 20 6e 65 65 64 65 64 2e 20 20 54 68   not needed.  Th
2b014 65 20 61 64 64 72 53 6f 72 74 49 6e 64 65 78 0a  e addrSortIndex.
2b015 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 20 69 73    ** variable is
2b016 20 75 73 65 64 20 74 6f 20 66 61 63 69 6c 69 74   used to facilit
2b017 61 74 65 20 74 68 61 74 20 63 68 61 6e 67 65 2e  ate that change.
2b018 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4f 72 64  .  */.  if( pOrd
2b019 65 72 42 79 20 29 7b 0a 20 20 20 20 4b 65 79 49  erBy ){.    KeyI
2b01a 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20  nfo *pKeyInfo;. 
2b01b 20 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20 6b 65     pKeyInfo = ke
2b01c 79 49 6e 66 6f 46 72 6f 6d 45 78 70 72 4c 69 73  yInfoFromExprLis
2b01d 74 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65 72  t(pParse, pOrder
2b01e 42 79 29 3b 0a 20 20 20 20 70 4f 72 64 65 72 42  By);.    pOrderB
2b01f 79 2d 3e 69 45 43 75 72 73 6f 72 20 3d 20 70 50  y->iECursor = pP
2b020 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20  arse->nTab++;.  
2b021 20 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68    p->addrOpenEph
2b022 6d 5b 32 5d 20 3d 20 61 64 64 72 53 6f 72 74 49  m[2] = addrSortI
2b023 6e 64 65 78 20 3d 0a 20 20 20 20 20 20 73 71 6c  ndex =.      sql
2b024 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
2b025 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72  , OP_OpenEphemer
2b026 61 6c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  al,.            
2b027 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
2b028 4f 72 64 65 72 42 79 2d 3e 69 45 43 75 72 73 6f  OrderBy->iECurso
2b029 72 2c 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78  r, pOrderBy->nEx
2b02a 70 72 2b 32 2c 20 30 2c 0a 20 20 20 20 20 20 20  pr+2, 0,.       
2b02b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b02c 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79 49      (char*)pKeyI
2b02d 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f  nfo, P4_KEYINFO_
2b02e 48 41 4e 44 4f 46 46 29 3b 0a 20 20 7d 65 6c 73  HANDOFF);.  }els
2b02f 65 7b 0a 20 20 20 20 61 64 64 72 53 6f 72 74 49  e{.    addrSortI
2b030 6e 64 65 78 20 3d 20 2d 31 3b 0a 20 20 7d 0a 0a  ndex = -1;.  }..
2b031 20 20 2f 2a 20 49 66 20 74 68 65 20 6f 75 74 70    /* If the outp
2b032 75 74 20 69 73 20 64 65 73 74 69 6e 65 64 20 66  ut is destined f
2b033 6f 72 20 61 20 74 65 6d 70 6f 72 61 72 79 20 74  or a temporary t
2b034 61 62 6c 65 2c 20 6f 70 65 6e 20 74 68 61 74 20  able, open that 
2b035 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  table..  */.  if
2b036 28 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d  ( pDest->eDest==
2b037 53 52 54 5f 45 70 68 65 6d 54 61 62 20 29 7b 0a  SRT_EphemTab ){.
2b038 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2b039 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp2(v, OP_Open
2b03a 45 70 68 65 6d 65 72 61 6c 2c 20 70 44 65 73 74  Ephemeral, pDest
2b03b 2d 3e 69 50 61 72 6d 2c 20 70 45 4c 69 73 74 2d  ->iParm, pEList-
2b03c 3e 6e 45 78 70 72 29 3b 0a 20 20 7d 0a 0a 20 20  >nExpr);.  }..  
2b03d 2f 2a 20 53 65 74 20 74 68 65 20 6c 69 6d 69 74  /* Set the limit
2b03e 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 45 6e 64 20  er..  */.  iEnd 
2b03f 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
2b040 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 63 6f 6d  eLabel(v);.  com
2b041 70 75 74 65 4c 69 6d 69 74 52 65 67 69 73 74 65  puteLimitRegiste
2b042 72 73 28 70 50 61 72 73 65 2c 20 70 2c 20 69 45  rs(pParse, p, iE
2b043 6e 64 29 3b 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20  nd);..  /* Open 
2b044 61 20 76 69 72 74 75 61 6c 20 69 6e 64 65 78 20  a virtual index 
2b045 74 6f 20 75 73 65 20 66 6f 72 20 74 68 65 20 64  to use for the d
2b046 69 73 74 69 6e 63 74 20 73 65 74 2e 0a 20 20 2a  istinct set..  *
2b047 2f 0a 20 20 69 66 28 20 69 73 44 69 73 74 69 6e  /.  if( isDistin
2b048 63 74 20 29 7b 0a 20 20 20 20 4b 65 79 49 6e 66  ct ){.    KeyInf
2b049 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20  o *pKeyInfo;.   
2b04a 20 61 73 73 65 72 74 28 20 69 73 41 67 67 20 7c   assert( isAgg |
2b04b 7c 20 70 47 72 6f 75 70 42 79 20 29 3b 0a 20 20  | pGroupBy );.  
2b04c 20 20 64 69 73 74 69 6e 63 74 20 3d 20 70 50 61    distinct = pPa
2b04d 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20  rse->nTab++;.   
2b04e 20 70 4b 65 79 49 6e 66 6f 20 3d 20 6b 65 79 49   pKeyInfo = keyI
2b04f 6e 66 6f 46 72 6f 6d 45 78 70 72 4c 69 73 74 28  nfoFromExprList(
2b050 70 50 61 72 73 65 2c 20 70 2d 3e 70 45 4c 69 73  pParse, p->pELis
2b051 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  t);.    sqlite3V
2b052 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
2b053 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 64  OpenEphemeral, d
2b054 69 73 74 69 6e 63 74 2c 20 30 2c 20 30 2c 0a 20  istinct, 0, 0,. 
2b055 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b056 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b         (char*)pK
2b057 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e  eyInfo, P4_KEYIN
2b058 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 7d  FO_HANDOFF);.  }
2b059 65 6c 73 65 7b 0a 20 20 20 20 64 69 73 74 69 6e  else{.    distin
2b05a 63 74 20 3d 20 2d 31 3b 0a 20 20 7d 0a 0a 20 20  ct = -1;.  }..  
2b05b 2f 2a 20 41 67 67 72 65 67 61 74 65 20 61 6e 64  /* Aggregate and
2b05c 20 6e 6f 6e 2d 61 67 67 72 65 67 61 74 65 20 71   non-aggregate q
2b05d 75 65 72 69 65 73 20 61 72 65 20 68 61 6e 64 6c  ueries are handl
2b05e 65 64 20 64 69 66 66 65 72 65 6e 74 6c 79 20 2a  ed differently *
2b05f 2f 0a 20 20 69 66 28 20 21 69 73 41 67 67 20 26  /.  if( !isAgg &
2b060 26 20 70 47 72 6f 75 70 42 79 3d 3d 30 20 29 7b  & pGroupBy==0 ){
2b061 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61 73  .    /* This cas
2b062 65 20 69 73 20 66 6f 72 20 6e 6f 6e 2d 61 67 67  e is for non-agg
2b063 72 65 67 61 74 65 20 71 75 65 72 69 65 73 0a 20  regate queries. 
2b064 20 20 20 2a 2a 20 42 65 67 69 6e 20 74 68 65 20     ** Begin the 
2b065 64 61 74 61 62 61 73 65 20 73 63 61 6e 0a 20 20  database scan.  
2b066 20 20 2a 2f 0a 20 20 20 20 70 57 49 6e 66 6f 20    */.    pWInfo 
2b067 3d 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65  = sqlite3WhereBe
2b068 67 69 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62  gin(pParse, pTab
2b069 4c 69 73 74 2c 20 70 57 68 65 72 65 2c 20 26 70  List, pWhere, &p
2b06a 4f 72 64 65 72 42 79 2c 20 30 29 3b 0a 20 20 20  OrderBy, 0);.   
2b06b 20 69 66 28 20 70 57 49 6e 66 6f 3d 3d 30 20 29   if( pWInfo==0 )
2b06c 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64   goto select_end
2b06d 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 73 6f 72  ;..    /* If sor
2b06e 74 69 6e 67 20 69 6e 64 65 78 20 74 68 61 74 20  ting index that 
2b06f 77 61 73 20 63 72 65 61 74 65 64 20 62 79 20 61  was created by a
2b070 20 70 72 69 6f 72 20 4f 50 5f 4f 70 65 6e 45 70   prior OP_OpenEp
2b071 68 65 6d 65 72 61 6c 20 0a 20 20 20 20 2a 2a 20  hemeral .    ** 
2b072 69 6e 73 74 72 75 63 74 69 6f 6e 20 65 6e 64 65  instruction ende
2b073 64 20 75 70 20 6e 6f 74 20 62 65 69 6e 67 20 6e  d up not being n
2b074 65 65 64 65 64 2c 20 74 68 65 6e 20 63 68 61 6e  eeded, then chan
2b075 67 65 20 74 68 65 20 4f 50 5f 4f 70 65 6e 45 70  ge the OP_OpenEp
2b076 68 65 6d 65 72 61 6c 0a 20 20 20 20 2a 2a 20 69  hemeral.    ** i
2b077 6e 74 6f 20 61 6e 20 4f 50 5f 4e 6f 6f 70 2e 0a  nto an OP_Noop..
2b078 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 61      */.    if( a
2b079 64 64 72 53 6f 72 74 49 6e 64 65 78 3e 3d 30 20  ddrSortIndex>=0 
2b07a 26 26 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 29  && pOrderBy==0 )
2b07b 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
2b07c 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28  dbeChangeToNoop(
2b07d 76 2c 20 61 64 64 72 53 6f 72 74 49 6e 64 65 78  v, addrSortIndex
2b07e 2c 20 31 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61  , 1);.      p->a
2b07f 64 64 72 4f 70 65 6e 45 70 68 6d 5b 32 5d 20 3d  ddrOpenEphm[2] =
2b080 20 2d 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20   -1;.    }..    
2b081 2f 2a 20 55 73 65 20 74 68 65 20 73 74 61 6e 64  /* Use the stand
2b082 61 72 64 20 69 6e 6e 65 72 20 6c 6f 6f 70 0a 20  ard inner loop. 
2b083 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74     */.    assert
2b084 28 21 69 73 44 69 73 74 69 6e 63 74 29 3b 0a 20  (!isDistinct);. 
2b085 20 20 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f     selectInnerLo
2b086 6f 70 28 70 50 61 72 73 65 2c 20 70 2c 20 70 45  op(pParse, p, pE
2b087 4c 69 73 74 2c 20 30 2c 20 30 2c 20 70 4f 72 64  List, 0, 0, pOrd
2b088 65 72 42 79 2c 20 2d 31 2c 20 70 44 65 73 74 2c  erBy, -1, pDest,
2b089 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2b08a 20 20 20 20 20 70 57 49 6e 66 6f 2d 3e 69 43 6f       pWInfo->iCo
2b08b 6e 74 69 6e 75 65 2c 20 70 57 49 6e 66 6f 2d 3e  ntinue, pWInfo->
2b08c 69 42 72 65 61 6b 29 3b 0a 0a 20 20 20 20 2f 2a  iBreak);..    /*
2b08d 20 45 6e 64 20 74 68 65 20 64 61 74 61 62 61 73   End the databas
2b08e 65 20 73 63 61 6e 20 6c 6f 6f 70 2e 0a 20 20 20  e scan loop..   
2b08f 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 57   */.    sqlite3W
2b090 68 65 72 65 45 6e 64 28 70 57 49 6e 66 6f 29 3b  hereEnd(pWInfo);
2b091 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
2b092 20 54 68 69 73 20 69 73 20 74 68 65 20 70 72 6f   This is the pro
2b093 63 65 73 73 69 6e 67 20 66 6f 72 20 61 67 67 72  cessing for aggr
2b094 65 67 61 74 65 20 71 75 65 72 69 65 73 20 2a 2f  egate queries */
2b095 0a 20 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74  .    NameContext
2b096 20 73 4e 43 3b 20 20 20 20 2f 2a 20 4e 61 6d 65   sNC;    /* Name
2b097 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 70 72 6f   context for pro
2b098 63 65 73 73 69 6e 67 20 61 67 67 72 65 67 61 74  cessing aggregat
2b099 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f  e information */
2b09a 0a 20 20 20 20 69 6e 74 20 69 41 4d 65 6d 3b 20  .    int iAMem; 
2b09b 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73           /* Firs
2b09c 74 20 4d 65 6d 20 61 64 64 72 65 73 73 20 66 6f  t Mem address fo
2b09d 72 20 73 74 6f 72 69 6e 67 20 63 75 72 72 65 6e  r storing curren
2b09e 74 20 47 52 4f 55 50 20 42 59 20 2a 2f 0a 20 20  t GROUP BY */.  
2b09f 20 20 69 6e 74 20 69 42 4d 65 6d 3b 20 20 20 20    int iBMem;    
2b0a0 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 4d        /* First M
2b0a1 65 6d 20 61 64 64 72 65 73 73 20 66 6f 72 20 70  em address for p
2b0a2 72 65 76 69 6f 75 73 20 47 52 4f 55 50 20 42 59  revious GROUP BY
2b0a3 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 55 73 65   */.    int iUse
2b0a4 46 6c 61 67 3b 20 20 20 20 20 20 20 2f 2a 20 4d  Flag;       /* M
2b0a5 65 6d 20 61 64 64 72 65 73 73 20 68 6f 6c 64 69  em address holdi
2b0a6 6e 67 20 66 6c 61 67 20 69 6e 64 69 63 61 74 69  ng flag indicati
2b0a7 6e 67 20 74 68 61 74 20 61 74 20 6c 65 61 73 74  ng that at least
2b0a8 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2b0a9 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e 65 20           ** one 
2b0aa 72 6f 77 20 6f 66 20 74 68 65 20 69 6e 70 75 74  row of the input
2b0ab 20 74 6f 20 74 68 65 20 61 67 67 72 65 67 61 74   to the aggregat
2b0ac 6f 72 20 68 61 73 20 62 65 65 6e 0a 20 20 20 20  or has been.    
2b0ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b0ae 20 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 65 64      ** processed
2b0af 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 41 62 6f   */.    int iAbo
2b0b0 72 74 46 6c 61 67 3b 20 20 20 20 20 2f 2a 20 4d  rtFlag;     /* M
2b0b1 65 6d 20 61 64 64 72 65 73 73 20 77 68 69 63 68  em address which
2b0b2 20 63 61 75 73 65 73 20 71 75 65 72 79 20 61 62   causes query ab
2b0b3 6f 72 74 20 69 66 20 70 6f 73 69 74 69 76 65 20  ort if positive 
2b0b4 2a 2f 0a 20 20 20 20 69 6e 74 20 67 72 6f 75 70  */.    int group
2b0b5 42 79 53 6f 72 74 3b 20 20 20 20 2f 2a 20 52 6f  BySort;    /* Ro
2b0b6 77 73 20 63 6f 6d 65 20 66 72 6f 6d 20 73 6f 75  ws come from sou
2b0b7 72 63 65 20 69 6e 20 47 52 4f 55 50 20 42 59 20  rce in GROUP BY 
2b0b8 6f 72 64 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74  order */.    int
2b0b9 20 61 64 64 72 45 6e 64 3b 20 20 20 20 20 20 20   addrEnd;       
2b0ba 20 2f 2a 20 45 6e 64 20 6f 66 20 70 72 6f 63 65   /* End of proce
2b0bb 73 73 69 6e 67 20 66 6f 72 20 74 68 69 73 20 53  ssing for this S
2b0bc 45 4c 45 43 54 20 2a 2f 0a 0a 20 20 20 20 2f 2a  ELECT */..    /*
2b0bd 20 52 65 6d 6f 76 65 20 61 6e 79 20 61 6e 64 20   Remove any and 
2b0be 61 6c 6c 20 61 6c 69 61 73 65 73 20 62 65 74 77  all aliases betw
2b0bf 65 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73  een the result s
2b0c0 65 74 20 61 6e 64 20 74 68 65 0a 20 20 20 20 2a  et and the.    *
2b0c1 2a 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73  * GROUP BY claus
2b0c2 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  e..    */.    if
2b0c3 28 20 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 20  ( pGroupBy ){.  
2b0c4 20 20 20 20 69 6e 74 20 6b 3b 20 20 20 20 20 20      int k;      
2b0c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b0c6 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
2b0c7 72 20 2a 2f 0a 20 20 20 20 20 20 73 74 72 75 63  r */.      struc
2b0c8 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
2b0c9 2a 70 49 74 65 6d 3b 20 20 2f 2a 20 46 6f 72 20  *pItem;  /* For 
2b0ca 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 65 78 70  looping over exp
2b0cb 72 65 73 73 69 6f 6e 20 69 6e 20 61 20 6c 69 73  ression in a lis
2b0cc 74 20 2a 2f 0a 0a 20 20 20 20 20 20 66 6f 72 28  t */..      for(
2b0cd 6b 3d 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78  k=p->pEList->nEx
2b0ce 70 72 2c 20 70 49 74 65 6d 3d 70 2d 3e 70 45 4c  pr, pItem=p->pEL
2b0cf 69 73 74 2d 3e 61 3b 20 6b 3e 30 3b 20 6b 2d 2d  ist->a; k>0; k--
2b0d0 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20  , pItem++){.    
2b0d1 20 20 20 20 70 49 74 65 6d 2d 3e 69 41 6c 69 61      pItem->iAlia
2b0d2 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  s = 0;.      }. 
2b0d3 20 20 20 20 20 66 6f 72 28 6b 3d 70 47 72 6f 75       for(k=pGrou
2b0d4 70 42 79 2d 3e 6e 45 78 70 72 2c 20 70 49 74 65  pBy->nExpr, pIte
2b0d5 6d 3d 70 47 72 6f 75 70 42 79 2d 3e 61 3b 20 6b  m=pGroupBy->a; k
2b0d6 3e 30 3b 20 6b 2d 2d 2c 20 70 49 74 65 6d 2b 2b  >0; k--, pItem++
2b0d7 29 7b 0a 20 20 20 20 20 20 20 20 70 49 74 65 6d  ){.        pItem
2b0d8 2d 3e 69 41 6c 69 61 73 20 3d 20 30 3b 0a 20 20  ->iAlias = 0;.  
2b0d9 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 0a 20      }.    }.. . 
2b0da 20 20 20 2f 2a 20 43 72 65 61 74 65 20 61 20 6c     /* Create a l
2b0db 61 62 65 6c 20 74 6f 20 6a 75 6d 70 20 74 6f 20  abel to jump to 
2b0dc 77 68 65 6e 20 77 65 20 77 61 6e 74 20 74 6f 20  when we want to 
2b0dd 61 62 6f 72 74 20 74 68 65 20 71 75 65 72 79 20  abort the query 
2b0de 2a 2f 0a 20 20 20 20 61 64 64 72 45 6e 64 20 3d  */.    addrEnd =
2b0df 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
2b0e0 4c 61 62 65 6c 28 76 29 3b 0a 0a 20 20 20 20 2f  Label(v);..    /
2b0e1 2a 20 43 6f 6e 76 65 72 74 20 54 4b 5f 43 4f 4c  * Convert TK_COL
2b0e2 55 4d 4e 20 6e 6f 64 65 73 20 69 6e 74 6f 20 54  UMN nodes into T
2b0e3 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 61 6e 64  K_AGG_COLUMN and
2b0e4 20 6d 61 6b 65 20 65 6e 74 72 69 65 73 20 69 6e   make entries in
2b0e5 0a 20 20 20 20 2a 2a 20 73 41 67 67 49 6e 66 6f  .    ** sAggInfo
2b0e6 20 66 6f 72 20 61 6c 6c 20 54 4b 5f 41 47 47 5f   for all TK_AGG_
2b0e7 46 55 4e 43 54 49 4f 4e 20 6e 6f 64 65 73 20 69  FUNCTION nodes i
2b0e8 6e 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 66  n expressions of
2b0e9 20 74 68 65 0a 20 20 20 20 2a 2a 20 53 45 4c 45   the.    ** SELE
2b0ea 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20  CT statement..  
2b0eb 20 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28    */.    memset(
2b0ec 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28  &sNC, 0, sizeof(
2b0ed 73 4e 43 29 29 3b 0a 20 20 20 20 73 4e 43 2e 70  sNC));.    sNC.p
2b0ee 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a  Parse = pParse;.
2b0ef 20 20 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74      sNC.pSrcList
2b0f0 20 3d 20 70 54 61 62 4c 69 73 74 3b 0a 20 20 20   = pTabList;.   
2b0f1 20 73 4e 43 2e 70 41 67 67 49 6e 66 6f 20 3d 20   sNC.pAggInfo = 
2b0f2 26 73 41 67 67 49 6e 66 6f 3b 0a 20 20 20 20 73  &sAggInfo;.    s
2b0f3 41 67 67 49 6e 66 6f 2e 6e 53 6f 72 74 69 6e 67  AggInfo.nSorting
2b0f4 43 6f 6c 75 6d 6e 20 3d 20 70 47 72 6f 75 70 42  Column = pGroupB
2b0f5 79 20 3f 20 70 47 72 6f 75 70 42 79 2d 3e 6e 45  y ? pGroupBy->nE
2b0f6 78 70 72 2b 31 20 3a 20 30 3b 0a 20 20 20 20 73  xpr+1 : 0;.    s
2b0f7 41 67 67 49 6e 66 6f 2e 70 47 72 6f 75 70 42 79  AggInfo.pGroupBy
2b0f8 20 3d 20 70 47 72 6f 75 70 42 79 3b 0a 20 20 20   = pGroupBy;.   
2b0f9 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c   sqlite3ExprAnal
2b0fa 79 7a 65 41 67 67 4c 69 73 74 28 26 73 4e 43 2c  yzeAggList(&sNC,
2b0fb 20 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 73 71   pEList);.    sq
2b0fc 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65  lite3ExprAnalyze
2b0fd 41 67 67 4c 69 73 74 28 26 73 4e 43 2c 20 70 4f  AggList(&sNC, pO
2b0fe 72 64 65 72 42 79 29 3b 0a 20 20 20 20 69 66 28  rderBy);.    if(
2b0ff 20 70 48 61 76 69 6e 67 20 29 7b 0a 20 20 20 20   pHaving ){.    
2b100 20 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61    sqlite3ExprAna
2b101 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73 28 26  lyzeAggregates(&
2b102 73 4e 43 2c 20 70 48 61 76 69 6e 67 29 3b 0a 20  sNC, pHaving);. 
2b103 20 20 20 7d 0a 20 20 20 20 73 41 67 67 49 6e 66     }.    sAggInf
2b104 6f 2e 6e 41 63 63 75 6d 75 6c 61 74 6f 72 20 3d  o.nAccumulator =
2b105 20 73 41 67 67 49 6e 66 6f 2e 6e 43 6f 6c 75 6d   sAggInfo.nColum
2b106 6e 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  n;.    for(i=0; 
2b107 69 3c 73 41 67 67 49 6e 66 6f 2e 6e 46 75 6e 63  i<sAggInfo.nFunc
2b108 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73  ; i++){.      as
2b109 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72  sert( !ExprHasPr
2b10a 6f 70 65 72 74 79 28 73 41 67 67 49 6e 66 6f 2e  operty(sAggInfo.
2b10b 61 46 75 6e 63 5b 69 5d 2e 70 45 78 70 72 2c 20  aFunc[i].pExpr, 
2b10c 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b  EP_xIsSelect) );
2b10d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2b10e 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c 69 73 74  prAnalyzeAggList
2b10f 28 26 73 4e 43 2c 20 73 41 67 67 49 6e 66 6f 2e  (&sNC, sAggInfo.
2b110 61 46 75 6e 63 5b 69 5d 2e 70 45 78 70 72 2d 3e  aFunc[i].pExpr->
2b111 78 2e 70 4c 69 73 74 29 3b 0a 20 20 20 20 7d 0a  x.pList);.    }.
2b112 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c      if( db->mall
2b113 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20  ocFailed ) goto 
2b114 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 0a 20 20 20  select_end;..   
2b115 20 2f 2a 20 50 72 6f 63 65 73 73 69 6e 67 20 66   /* Processing f
2b116 6f 72 20 61 67 67 72 65 67 61 74 65 73 20 77 69  or aggregates wi
2b117 74 68 20 47 52 4f 55 50 20 42 59 20 69 73 20 76  th GROUP BY is v
2b118 65 72 79 20 64 69 66 66 65 72 65 6e 74 20 61 6e  ery different an
2b119 64 0a 20 20 20 20 2a 2a 20 6d 75 63 68 20 6d 6f  d.    ** much mo
2b11a 72 65 20 63 6f 6d 70 6c 65 78 20 74 68 61 6e 20  re complex than 
2b11b 61 67 67 72 65 67 61 74 65 73 20 77 69 74 68 6f  aggregates witho
2b11c 75 74 20 61 20 47 52 4f 55 50 20 42 59 2e 0a 20  ut a GROUP BY.. 
2b11d 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 47     */.    if( pG
2b11e 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 20 20  roupBy ){.      
2b11f 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66  KeyInfo *pKeyInf
2b120 6f 3b 20 20 2f 2a 20 4b 65 79 69 6e 67 20 69 6e  o;  /* Keying in
2b121 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68  formation for th
2b122 65 20 67 72 6f 75 70 20 62 79 20 63 6c 61 75 73  e group by claus
2b123 65 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6a  e */.      int j
2b124 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  1;             /
2b125 2a 20 41 2d 76 73 2d 42 20 63 6f 6d 70 61 72 69  * A-vs-B compari
2b126 73 69 6f 6e 20 6a 75 6d 70 20 2a 2f 0a 20 20 20  sion jump */.   
2b127 20 20 20 69 6e 74 20 61 64 64 72 4f 75 74 70 75     int addrOutpu
2b128 74 52 6f 77 3b 20 20 2f 2a 20 53 74 61 72 74 20  tRow;  /* Start 
2b129 6f 66 20 73 75 62 72 6f 75 74 69 6e 65 20 74 68  of subroutine th
2b12a 61 74 20 6f 75 74 70 75 74 73 20 61 20 72 65 73  at outputs a res
2b12b 75 6c 74 20 72 6f 77 20 2a 2f 0a 20 20 20 20 20  ult row */.     
2b12c 20 69 6e 74 20 72 65 67 4f 75 74 70 75 74 52 6f   int regOutputRo
2b12d 77 3b 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 61  w;   /* Return a
2b12e 64 64 72 65 73 73 20 72 65 67 69 73 74 65 72 20  ddress register 
2b12f 66 6f 72 20 6f 75 74 70 75 74 20 73 75 62 72 6f  for output subro
2b130 75 74 69 6e 65 20 2a 2f 0a 20 20 20 20 20 20 69  utine */.      i
2b131 6e 74 20 61 64 64 72 53 65 74 41 62 6f 72 74 3b  nt addrSetAbort;
2b132 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 61 62     /* Set the ab
2b133 6f 72 74 20 66 6c 61 67 20 61 6e 64 20 72 65 74  ort flag and ret
2b134 75 72 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  urn */.      int
2b135 20 61 64 64 72 54 6f 70 4f 66 4c 6f 6f 70 3b 20   addrTopOfLoop; 
2b136 20 2f 2a 20 54 6f 70 20 6f 66 20 74 68 65 20 69   /* Top of the i
2b137 6e 70 75 74 20 6c 6f 6f 70 20 2a 2f 0a 20 20 20  nput loop */.   
2b138 20 20 20 69 6e 74 20 61 64 64 72 53 6f 72 74 69     int addrSorti
2b139 6e 67 49 64 78 3b 20 2f 2a 20 54 68 65 20 4f 50  ngIdx; /* The OP
2b13a 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 66  _OpenEphemeral f
2b13b 6f 72 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69  or the sorting i
2b13c 6e 64 65 78 20 2a 2f 0a 20 20 20 20 20 20 69 6e  ndex */.      in
2b13d 74 20 61 64 64 72 52 65 73 65 74 3b 20 20 20 20  t addrReset;    
2b13e 20 20 2f 2a 20 53 75 62 72 6f 75 74 69 6e 65 20    /* Subroutine 
2b13f 66 6f 72 20 72 65 73 65 74 74 69 6e 67 20 74 68  for resetting th
2b140 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 2a 2f  e accumulator */
2b141 0a 20 20 20 20 20 20 69 6e 74 20 72 65 67 52 65  .      int regRe
2b142 73 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 52 65  set;       /* Re
2b143 74 75 72 6e 20 61 64 64 72 65 73 73 20 72 65 67  turn address reg
2b144 69 73 74 65 72 20 66 6f 72 20 72 65 73 65 74 20  ister for reset 
2b145 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 0a 20  subroutine */.. 
2b146 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65       /* If there
2b147 20 69 73 20 61 20 47 52 4f 55 50 20 42 59 20 63   is a GROUP BY c
2b148 6c 61 75 73 65 20 77 65 20 6d 69 67 68 74 20 6e  lause we might n
2b149 65 65 64 20 61 20 73 6f 72 74 69 6e 67 20 69 6e  eed a sorting in
2b14a 64 65 78 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20  dex to.      ** 
2b14b 69 6d 70 6c 65 6d 65 6e 74 20 69 74 2e 20 20 41  implement it.  A
2b14c 6c 6c 6f 63 61 74 65 20 74 68 61 74 20 73 6f 72  llocate that sor
2b14d 74 69 6e 67 20 69 6e 64 65 78 20 6e 6f 77 2e 20  ting index now. 
2b14e 20 49 66 20 69 74 20 74 75 72 6e 73 20 6f 75 74   If it turns out
2b14f 0a 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 77  .      ** that w
2b150 65 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 69 74  e do not need it
2b151 20 61 66 74 65 72 20 61 6c 6c 2c 20 74 68 65 20   after all, the 
2b152 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 69 6e  OpenEphemeral in
2b153 73 74 72 75 63 74 69 6f 6e 0a 20 20 20 20 20 20  struction.      
2b154 2a 2a 20 77 69 6c 6c 20 62 65 20 63 6f 6e 76 65  ** will be conve
2b155 72 74 65 64 20 69 6e 74 6f 20 61 20 4e 6f 6f 70  rted into a Noop
2b156 2e 20 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  .  .      */.   
2b157 20 20 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74     sAggInfo.sort
2b158 69 6e 67 49 64 78 20 3d 20 70 50 61 72 73 65 2d  ingIdx = pParse-
2b159 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 70  >nTab++;.      p
2b15a 4b 65 79 49 6e 66 6f 20 3d 20 6b 65 79 49 6e 66  KeyInfo = keyInf
2b15b 6f 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 70 50  oFromExprList(pP
2b15c 61 72 73 65 2c 20 70 47 72 6f 75 70 42 79 29 3b  arse, pGroupBy);
2b15d 0a 20 20 20 20 20 20 61 64 64 72 53 6f 72 74 69  .      addrSorti
2b15e 6e 67 49 64 78 20 3d 20 73 71 6c 69 74 65 33 56  ngIdx = sqlite3V
2b15f 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
2b160 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 0a  OpenEphemeral, .
2b161 20 20 20 20 20 20 20 20 20 20 73 41 67 67 49 6e            sAggIn
2b162 66 6f 2e 73 6f 72 74 69 6e 67 49 64 78 2c 20 73  fo.sortingIdx, s
2b163 41 67 67 49 6e 66 6f 2e 6e 53 6f 72 74 69 6e 67  AggInfo.nSorting
2b164 43 6f 6c 75 6d 6e 2c 20 0a 20 20 20 20 20 20 20  Column, .       
2b165 20 20 20 30 2c 20 28 63 68 61 72 2a 29 70 4b 65     0, (char*)pKe
2b166 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46  yInfo, P4_KEYINF
2b167 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 0a 20 20 20  O_HANDOFF);..   
2b168 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65     /* Initialize
2b169 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e   memory location
2b16a 73 20 75 73 65 64 20 62 79 20 47 52 4f 55 50 20  s used by GROUP 
2b16b 42 59 20 61 67 67 72 65 67 61 74 65 20 70 72 6f  BY aggregate pro
2b16c 63 65 73 73 69 6e 67 0a 20 20 20 20 20 20 2a 2f  cessing.      */
2b16d 0a 20 20 20 20 20 20 69 55 73 65 46 6c 61 67 20  .      iUseFlag 
2b16e 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
2b16f 3b 0a 20 20 20 20 20 20 69 41 62 6f 72 74 46 6c  ;.      iAbortFl
2b170 61 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  ag = ++pParse->n
2b171 4d 65 6d 3b 0a 20 20 20 20 20 20 72 65 67 4f 75  Mem;.      regOu
2b172 74 70 75 74 52 6f 77 20 3d 20 2b 2b 70 50 61 72  tputRow = ++pPar
2b173 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20  se->nMem;.      
2b174 61 64 64 72 4f 75 74 70 75 74 52 6f 77 20 3d 20  addrOutputRow = 
2b175 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
2b176 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 72  abel(v);.      r
2b177 65 67 52 65 73 65 74 20 3d 20 2b 2b 70 50 61 72  egReset = ++pPar
2b178 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20  se->nMem;.      
2b179 61 64 64 72 52 65 73 65 74 20 3d 20 73 71 6c 69  addrReset = sqli
2b17a 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
2b17b 28 76 29 3b 0a 20 20 20 20 20 20 69 41 4d 65 6d  (v);.      iAMem
2b17c 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20   = pParse->nMem 
2b17d 2b 20 31 3b 0a 20 20 20 20 20 20 70 50 61 72 73  + 1;.      pPars
2b17e 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 47 72 6f 75  e->nMem += pGrou
2b17f 70 42 79 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20  pBy->nExpr;.    
2b180 20 20 69 42 4d 65 6d 20 3d 20 70 50 61 72 73 65    iBMem = pParse
2b181 2d 3e 6e 4d 65 6d 20 2b 20 31 3b 0a 20 20 20 20  ->nMem + 1;.    
2b182 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b    pParse->nMem +
2b183 3d 20 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70  = pGroupBy->nExp
2b184 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  r;.      sqlite3
2b185 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2b186 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 69 41 62  _Integer, 0, iAb
2b187 6f 72 74 46 6c 61 67 29 3b 0a 20 20 20 20 20 20  ortFlag);.      
2b188 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
2b189 22 63 6c 65 61 72 20 61 62 6f 72 74 20 66 6c 61  "clear abort fla
2b18a 67 22 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  g"));.      sqli
2b18b 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2b18c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20   OP_Integer, 0, 
2b18d 69 55 73 65 46 6c 61 67 29 3b 0a 20 20 20 20 20  iUseFlag);.     
2b18e 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
2b18f 20 22 69 6e 64 69 63 61 74 65 20 61 63 63 75 6d   "indicate accum
2b190 75 6c 61 74 6f 72 20 65 6d 70 74 79 22 29 29 3b  ulator empty"));
2b191 0a 0a 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e  ..      /* Begin
2b192 20 61 20 6c 6f 6f 70 20 74 68 61 74 20 77 69 6c   a loop that wil
2b193 6c 20 65 78 74 72 61 63 74 20 61 6c 6c 20 73 6f  l extract all so
2b194 75 72 63 65 20 72 6f 77 73 20 69 6e 20 47 52 4f  urce rows in GRO
2b195 55 50 20 42 59 20 6f 72 64 65 72 2e 0a 20 20 20  UP BY order..   
2b196 20 20 20 2a 2a 20 54 68 69 73 20 6d 69 67 68 74     ** This might
2b197 20 69 6e 76 6f 6c 76 65 20 74 77 6f 20 73 65 70   involve two sep
2b198 61 72 61 74 65 20 6c 6f 6f 70 73 20 77 69 74 68  arate loops with
2b199 20 61 6e 20 4f 50 5f 53 6f 72 74 20 69 6e 20 62   an OP_Sort in b
2b19a 65 74 77 65 65 6e 2c 20 6f 72 0a 20 20 20 20 20  etween, or.     
2b19b 20 2a 2a 20 69 74 20 6d 69 67 68 74 20 62 65 20   ** it might be 
2b19c 61 20 73 69 6e 67 6c 65 20 6c 6f 6f 70 20 74 68  a single loop th
2b19d 61 74 20 75 73 65 73 20 61 6e 20 69 6e 64 65 78  at uses an index
2b19e 20 74 6f 20 65 78 74 72 61 63 74 20 69 6e 66 6f   to extract info
2b19f 72 6d 61 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a  rmation.      **
2b1a0 20 69 6e 20 74 68 65 20 72 69 67 68 74 20 6f 72   in the right or
2b1a1 64 65 72 20 74 6f 20 62 65 67 69 6e 20 77 69 74  der to begin wit
2b1a2 68 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  h..      */.    
2b1a3 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b1a4 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c  Op2(v, OP_Gosub,
2b1a5 20 72 65 67 52 65 73 65 74 2c 20 61 64 64 72 52   regReset, addrR
2b1a6 65 73 65 74 29 3b 0a 20 20 20 20 20 20 70 57 49  eset);.      pWI
2b1a7 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 57 68 65  nfo = sqlite3Whe
2b1a8 72 65 42 65 67 69 6e 28 70 50 61 72 73 65 2c 20  reBegin(pParse, 
2b1a9 70 54 61 62 4c 69 73 74 2c 20 70 57 68 65 72 65  pTabList, pWhere
2b1aa 2c 20 26 70 47 72 6f 75 70 42 79 2c 20 30 29 3b  , &pGroupBy, 0);
2b1ab 0a 20 20 20 20 20 20 69 66 28 20 70 57 49 6e 66  .      if( pWInf
2b1ac 6f 3d 3d 30 20 29 20 67 6f 74 6f 20 73 65 6c 65  o==0 ) goto sele
2b1ad 63 74 5f 65 6e 64 3b 0a 20 20 20 20 20 20 69 66  ct_end;.      if
2b1ae 28 20 70 47 72 6f 75 70 42 79 3d 3d 30 20 29 7b  ( pGroupBy==0 ){
2b1af 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  .        /* The 
2b1b0 6f 70 74 69 6d 69 7a 65 72 20 69 73 20 61 62 6c  optimizer is abl
2b1b1 65 20 74 6f 20 64 65 6c 69 76 65 72 20 72 6f 77  e to deliver row
2b1b2 73 20 69 6e 20 67 72 6f 75 70 20 62 79 20 6f 72  s in group by or
2b1b3 64 65 72 20 73 6f 0a 20 20 20 20 20 20 20 20 2a  der so.        *
2b1b4 2a 20 77 65 20 64 6f 20 6e 6f 74 20 68 61 76 65  * we do not have
2b1b5 20 74 6f 20 73 6f 72 74 2e 20 20 54 68 65 20 4f   to sort.  The O
2b1b6 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20  P_OpenEphemeral 
2b1b7 74 61 62 6c 65 20 77 69 6c 6c 20 62 65 0a 20 20  table will be.  
2b1b8 20 20 20 20 20 20 2a 2a 20 63 61 6e 63 65 6c 6c        ** cancell
2b1b9 65 64 20 6c 61 74 65 72 20 62 65 63 61 75 73 65  ed later because
2b1ba 20 77 65 20 73 74 69 6c 6c 20 6e 65 65 64 20 74   we still need t
2b1bb 6f 20 75 73 65 20 74 68 65 20 70 4b 65 79 49 6e  o use the pKeyIn
2b1bc 66 6f 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  fo.        */.  
2b1bd 20 20 20 20 20 20 70 47 72 6f 75 70 42 79 20 3d        pGroupBy =
2b1be 20 70 2d 3e 70 47 72 6f 75 70 42 79 3b 0a 20 20   p->pGroupBy;.  
2b1bf 20 20 20 20 20 20 67 72 6f 75 70 42 79 53 6f 72        groupBySor
2b1c0 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c  t = 0;.      }el
2b1c1 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 52  se{.        /* R
2b1c2 6f 77 73 20 61 72 65 20 63 6f 6d 69 6e 67 20 6f  ows are coming o
2b1c3 75 74 20 69 6e 20 75 6e 64 65 74 65 72 6d 69 6e  ut in undetermin
2b1c4 65 64 20 6f 72 64 65 72 2e 20 20 57 65 20 68 61  ed order.  We ha
2b1c5 76 65 20 74 6f 20 70 75 73 68 0a 20 20 20 20 20  ve to push.     
2b1c6 20 20 20 2a 2a 20 65 61 63 68 20 72 6f 77 20 69     ** each row i
2b1c7 6e 74 6f 20 61 20 73 6f 72 74 69 6e 67 20 69 6e  nto a sorting in
2b1c8 64 65 78 2c 20 74 65 72 6d 69 6e 61 74 65 20 74  dex, terminate t
2b1c9 68 65 20 66 69 72 73 74 20 6c 6f 6f 70 2c 0a 20  he first loop,. 
2b1ca 20 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 6c         ** then l
2b1cb 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 73 6f 72  oop over the sor
2b1cc 74 69 6e 67 20 69 6e 64 65 78 20 69 6e 20 6f 72  ting index in or
2b1cd 64 65 72 20 74 6f 20 67 65 74 20 74 68 65 20 6f  der to get the o
2b1ce 75 74 70 75 74 0a 20 20 20 20 20 20 20 20 2a 2a  utput.        **
2b1cf 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72   in sorted order
2b1d0 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
2b1d1 20 20 20 20 69 6e 74 20 72 65 67 42 61 73 65 3b      int regBase;
2b1d2 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 65 67  .        int reg
2b1d3 52 65 63 6f 72 64 3b 0a 20 20 20 20 20 20 20 20  Record;.        
2b1d4 69 6e 74 20 6e 43 6f 6c 3b 0a 20 20 20 20 20 20  int nCol;.      
2b1d5 20 20 69 6e 74 20 6e 47 72 6f 75 70 42 79 3b 0a    int nGroupBy;.
2b1d6 0a 20 20 20 20 20 20 20 20 67 72 6f 75 70 42 79  .        groupBy
2b1d7 53 6f 72 74 20 3d 20 31 3b 0a 20 20 20 20 20 20  Sort = 1;.      
2b1d8 20 20 6e 47 72 6f 75 70 42 79 20 3d 20 70 47 72    nGroupBy = pGr
2b1d9 6f 75 70 42 79 2d 3e 6e 45 78 70 72 3b 0a 20 20  oupBy->nExpr;.  
2b1da 20 20 20 20 20 20 6e 43 6f 6c 20 3d 20 6e 47 72        nCol = nGr
2b1db 6f 75 70 42 79 20 2b 20 31 3b 0a 20 20 20 20 20  oupBy + 1;.     
2b1dc 20 20 20 6a 20 3d 20 6e 47 72 6f 75 70 42 79 2b     j = nGroupBy+
2b1dd 31 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  1;.        for(i
2b1de 3d 30 3b 20 69 3c 73 41 67 67 49 6e 66 6f 2e 6e  =0; i<sAggInfo.n
2b1df 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Column; i++){.  
2b1e0 20 20 20 20 20 20 20 20 69 66 28 20 73 41 67 67          if( sAgg
2b1e1 49 6e 66 6f 2e 61 43 6f 6c 5b 69 5d 2e 69 53 6f  Info.aCol[i].iSo
2b1e2 72 74 65 72 43 6f 6c 75 6d 6e 3e 3d 6a 20 29 7b  rterColumn>=j ){
2b1e3 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 43 6f  .            nCo
2b1e4 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20  l++;.           
2b1e5 20 6a 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20   j++;.          
2b1e6 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
2b1e7 20 20 20 20 72 65 67 42 61 73 65 20 3d 20 73 71      regBase = sq
2b1e8 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67  lite3GetTempRang
2b1e9 65 28 70 50 61 72 73 65 2c 20 6e 43 6f 6c 29 3b  e(pParse, nCol);
2b1ea 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2b1eb 45 78 70 72 43 61 63 68 65 43 6c 65 61 72 28 70  ExprCacheClear(p
2b1ec 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20  Parse);.        
2b1ed 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 45  sqlite3ExprCodeE
2b1ee 78 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20  xprList(pParse, 
2b1ef 70 47 72 6f 75 70 42 79 2c 20 72 65 67 42 61 73  pGroupBy, regBas
2b1f0 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73  e, 0);.        s
2b1f1 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2b1f2 28 76 2c 20 4f 50 5f 53 65 71 75 65 6e 63 65 2c  (v, OP_Sequence,
2b1f3 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e   sAggInfo.sortin
2b1f4 67 49 64 78 2c 72 65 67 42 61 73 65 2b 6e 47 72  gIdx,regBase+nGr
2b1f5 6f 75 70 42 79 29 3b 0a 20 20 20 20 20 20 20 20  oupBy);.        
2b1f6 6a 20 3d 20 6e 47 72 6f 75 70 42 79 2b 31 3b 0a  j = nGroupBy+1;.
2b1f7 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b          for(i=0;
2b1f8 20 69 3c 73 41 67 67 49 6e 66 6f 2e 6e 43 6f 6c   i<sAggInfo.nCol
2b1f9 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  umn; i++){.     
2b1fa 20 20 20 20 20 73 74 72 75 63 74 20 41 67 67 49       struct AggI
2b1fb 6e 66 6f 5f 63 6f 6c 20 2a 70 43 6f 6c 20 3d 20  nfo_col *pCol = 
2b1fc 26 73 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 5b 69  &sAggInfo.aCol[i
2b1fd 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ];.          if(
2b1fe 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f   pCol->iSorterCo
2b1ff 6c 75 6d 6e 3e 3d 6a 20 29 7b 0a 20 20 20 20 20  lumn>=j ){.     
2b200 20 20 20 20 20 20 20 69 6e 74 20 72 31 20 3d 20         int r1 = 
2b201 6a 20 2b 20 72 65 67 42 61 73 65 3b 0a 20 20 20  j + regBase;.   
2b202 20 20 20 20 20 20 20 20 20 69 6e 74 20 72 32 3b           int r2;
2b203 0a 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 32  ..            r2
2b204 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
2b205 64 65 47 65 74 43 6f 6c 75 6d 6e 28 70 50 61 72  deGetColumn(pPar
2b206 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  se, .           
2b207 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b208 20 20 20 20 70 43 6f 6c 2d 3e 70 54 61 62 2c 20      pCol->pTab, 
2b209 70 43 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 70  pCol->iColumn, p
2b20a 43 6f 6c 2d 3e 69 54 61 62 6c 65 2c 20 72 31 2c  Col->iTable, r1,
2b20b 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20   0);.           
2b20c 20 69 66 28 20 72 31 21 3d 72 32 20 29 7b 0a 20   if( r1!=r2 ){. 
2b20d 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
2b20e 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2b20f 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 32 2c 20  , OP_SCopy, r2, 
2b210 72 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  r1);.           
2b211 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 6a   }.            j
2b212 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ++;.          }.
2b213 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2b214 20 20 72 65 67 52 65 63 6f 72 64 20 3d 20 73 71    regRecord = sq
2b215 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
2b216 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20  pParse);.       
2b217 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b218 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63  p3(v, OP_MakeRec
2b219 6f 72 64 2c 20 72 65 67 42 61 73 65 2c 20 6e 43  ord, regBase, nC
2b21a 6f 6c 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a  ol, regRecord);.
2b21b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2b21c 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2b21d 49 64 78 49 6e 73 65 72 74 2c 20 73 41 67 67 49  IdxInsert, sAggI
2b21e 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 64 78 2c 20  nfo.sortingIdx, 
2b21f 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 20 20  regRecord);.    
2b220 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61      sqlite3Relea
2b221 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
2b222 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20  , regRecord);.  
2b223 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c        sqlite3Rel
2b224 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50  easeTempRange(pP
2b225 61 72 73 65 2c 20 72 65 67 42 61 73 65 2c 20 6e  arse, regBase, n
2b226 43 6f 6c 29 3b 0a 20 20 20 20 20 20 20 20 73 71  Col);.        sq
2b227 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 70 57  lite3WhereEnd(pW
2b228 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 73  Info);.        s
2b229 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2b22a 28 76 2c 20 4f 50 5f 53 6f 72 74 2c 20 73 41 67  (v, OP_Sort, sAg
2b22b 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 64 78  gInfo.sortingIdx
2b22c 2c 20 61 64 64 72 45 6e 64 29 3b 0a 20 20 20 20  , addrEnd);.    
2b22d 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
2b22e 28 76 2c 20 22 47 52 4f 55 50 20 42 59 20 73 6f  (v, "GROUP BY so
2b22f 72 74 22 29 29 3b 0a 20 20 20 20 20 20 20 20 73  rt"));.        s
2b230 41 67 67 49 6e 66 6f 2e 75 73 65 53 6f 72 74 69  AggInfo.useSorti
2b231 6e 67 49 64 78 20 3d 20 31 3b 0a 20 20 20 20 20  ngIdx = 1;.     
2b232 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
2b233 63 68 65 43 6c 65 61 72 28 70 50 61 72 73 65 29  cheClear(pParse)
2b234 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
2b235 20 2f 2a 20 45 76 61 6c 75 61 74 65 20 74 68 65   /* Evaluate the
2b236 20 63 75 72 72 65 6e 74 20 47 52 4f 55 50 20 42   current GROUP B
2b237 59 20 74 65 72 6d 73 20 61 6e 64 20 73 74 6f 72  Y terms and stor
2b238 65 20 69 6e 20 62 30 2c 20 62 31 2c 20 62 32 2e  e in b0, b1, b2.
2b239 2e 2e 0a 20 20 20 20 20 20 2a 2a 20 28 62 30 20  ...      ** (b0 
2b23a 69 73 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69  is memory locati
2b23b 6f 6e 20 69 42 4d 65 6d 2b 30 2c 20 62 31 20 69  on iBMem+0, b1 i
2b23c 73 20 69 42 4d 65 6d 2b 31 2c 20 61 6e 64 20 73  s iBMem+1, and s
2b23d 6f 20 66 6f 72 74 68 29 0a 20 20 20 20 20 20 2a  o forth).      *
2b23e 2a 20 54 68 65 6e 20 63 6f 6d 70 61 72 65 20 74  * Then compare t
2b23f 68 65 20 63 75 72 72 65 6e 74 20 47 52 4f 55 50  he current GROUP
2b240 20 42 59 20 74 65 72 6d 73 20 61 67 61 69 6e 73   BY terms agains
2b241 74 20 74 68 65 20 47 52 4f 55 50 20 42 59 20 74  t the GROUP BY t
2b242 65 72 6d 73 0a 20 20 20 20 20 20 2a 2a 20 66 72  erms.      ** fr
2b243 6f 6d 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  om the previous 
2b244 72 6f 77 20 63 75 72 72 65 6e 74 6c 79 20 73 74  row currently st
2b245 6f 72 65 64 20 69 6e 20 61 30 2c 20 61 31 2c 20  ored in a0, a1, 
2b246 61 32 2e 2e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  a2....      */. 
2b247 20 20 20 20 20 61 64 64 72 54 6f 70 4f 66 4c 6f       addrTopOfLo
2b248 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  op = sqlite3Vdbe
2b249 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a  CurrentAddr(v);.
2b24a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2b24b 72 43 61 63 68 65 43 6c 65 61 72 28 70 50 61 72  rCacheClear(pPar
2b24c 73 65 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a  se);.      for(j
2b24d 3d 30 3b 20 6a 3c 70 47 72 6f 75 70 42 79 2d 3e  =0; j<pGroupBy->
2b24e 6e 45 78 70 72 3b 20 6a 2b 2b 29 7b 0a 20 20 20  nExpr; j++){.   
2b24f 20 20 20 20 20 69 66 28 20 67 72 6f 75 70 42 79       if( groupBy
2b250 53 6f 72 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Sort ){.        
2b251 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b252 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  Op3(v, OP_Column
2b253 2c 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69  , sAggInfo.sorti
2b254 6e 67 49 64 78 2c 20 6a 2c 20 69 42 4d 65 6d 2b  ngIdx, j, iBMem+
2b255 6a 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  j);.        }els
2b256 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73 41 67  e{.          sAg
2b257 67 49 6e 66 6f 2e 64 69 72 65 63 74 4d 6f 64 65  gInfo.directMode
2b258 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20   = 1;.          
2b259 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
2b25a 70 50 61 72 73 65 2c 20 70 47 72 6f 75 70 42 79  pParse, pGroupBy
2b25b 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72 2c 20 69 42  ->a[j].pExpr, iB
2b25c 4d 65 6d 2b 6a 29 3b 0a 20 20 20 20 20 20 20 20  Mem+j);.        
2b25d 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
2b25e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b25f 34 28 76 2c 20 4f 50 5f 43 6f 6d 70 61 72 65 2c  4(v, OP_Compare,
2b260 20 69 41 4d 65 6d 2c 20 69 42 4d 65 6d 2c 20 70   iAMem, iBMem, p
2b261 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72 2c 0a  GroupBy->nExpr,.
2b262 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b263 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a            (char*
2b264 29 70 4b 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45  )pKeyInfo, P4_KE
2b265 59 49 4e 46 4f 29 3b 0a 20 20 20 20 20 20 6a 31  YINFO);.      j1
2b266 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75   = sqlite3VdbeCu
2b267 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20  rrentAddr(v);.  
2b268 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2b269 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4a 75 6d 70  ddOp3(v, OP_Jump
2b26a 2c 20 6a 31 2b 31 2c 20 30 2c 20 6a 31 2b 31 29  , j1+1, 0, j1+1)
2b26b 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 47 65 6e 65  ;..      /* Gene
2b26c 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 72  rate code that r
2b26d 75 6e 73 20 77 68 65 6e 65 76 65 72 20 74 68 65  uns whenever the
2b26e 20 47 52 4f 55 50 20 42 59 20 63 68 61 6e 67 65   GROUP BY change
2b26f 73 2e 0a 20 20 20 20 20 20 2a 2a 20 43 68 61 6e  s..      ** Chan
2b270 67 65 73 20 69 6e 20 74 68 65 20 47 52 4f 55 50  ges in the GROUP
2b271 20 42 59 20 61 72 65 20 64 65 74 65 63 74 65 64   BY are detected
2b272 20 62 79 20 74 68 65 20 70 72 65 76 69 6f 75 73   by the previous
2b273 20 63 6f 64 65 0a 20 20 20 20 20 20 2a 2a 20 62   code.      ** b
2b274 6c 6f 63 6b 2e 20 20 49 66 20 74 68 65 72 65 20  lock.  If there 
2b275 77 65 72 65 20 6e 6f 20 63 68 61 6e 67 65 73 2c  were no changes,
2b276 20 74 68 69 73 20 62 6c 6f 63 6b 20 69 73 20 73   this block is s
2b277 6b 69 70 70 65 64 2e 0a 20 20 20 20 20 20 2a 2a  kipped..      **
2b278 0a 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 63  .      ** This c
2b279 6f 64 65 20 63 6f 70 69 65 73 20 63 75 72 72 65  ode copies curre
2b27a 6e 74 20 67 72 6f 75 70 20 62 79 20 74 65 72 6d  nt group by term
2b27b 73 20 69 6e 20 62 30 2c 62 31 2c 62 32 2c 2e 2e  s in b0,b1,b2,..
2b27c 2e 0a 20 20 20 20 20 20 2a 2a 20 6f 76 65 72 20  ..      ** over 
2b27d 74 6f 20 61 30 2c 61 31 2c 61 32 2e 20 20 49 74  to a0,a1,a2.  It
2b27e 20 74 68 65 6e 20 63 61 6c 6c 73 20 74 68 65 20   then calls the 
2b27f 6f 75 74 70 75 74 20 73 75 62 72 6f 75 74 69 6e  output subroutin
2b280 65 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 72  e.      ** and r
2b281 65 73 65 74 73 20 74 68 65 20 61 67 67 72 65 67  esets the aggreg
2b282 61 74 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 20  ate accumulator 
2b283 72 65 67 69 73 74 65 72 73 20 69 6e 20 70 72 65  registers in pre
2b284 70 61 72 61 74 69 6f 6e 0a 20 20 20 20 20 20 2a  paration.      *
2b285 2a 20 66 6f 72 20 74 68 65 20 6e 65 78 74 20 47  * for the next G
2b286 52 4f 55 50 20 42 59 20 62 61 74 63 68 2e 0a 20  ROUP BY batch.. 
2b287 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71       */.      sq
2b288 6c 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76  lite3ExprCodeMov
2b289 65 28 70 50 61 72 73 65 2c 20 69 42 4d 65 6d 2c  e(pParse, iBMem,
2b28a 20 69 41 4d 65 6d 2c 20 70 47 72 6f 75 70 42 79   iAMem, pGroupBy
2b28b 2d 3e 6e 45 78 70 72 29 3b 0a 20 20 20 20 20 20  ->nExpr);.      
2b28c 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b28d 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72  2(v, OP_Gosub, r
2b28e 65 67 4f 75 74 70 75 74 52 6f 77 2c 20 61 64 64  egOutputRow, add
2b28f 72 4f 75 74 70 75 74 52 6f 77 29 3b 0a 20 20 20  rOutputRow);.   
2b290 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
2b291 76 2c 20 22 6f 75 74 70 75 74 20 6f 6e 65 20 72  v, "output one r
2b292 6f 77 22 29 29 3b 0a 20 20 20 20 20 20 73 71 6c  ow"));.      sql
2b293 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2b294 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 69 41 62 6f  , OP_IfPos, iAbo
2b295 72 74 46 6c 61 67 2c 20 61 64 64 72 45 6e 64 29  rtFlag, addrEnd)
2b296 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d  ;.      VdbeComm
2b297 65 6e 74 28 28 76 2c 20 22 63 68 65 63 6b 20 61  ent((v, "check a
2b298 62 6f 72 74 20 66 6c 61 67 22 29 29 3b 0a 20 20  bort flag"));.  
2b299 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2b29a 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75  ddOp2(v, OP_Gosu
2b29b 62 2c 20 72 65 67 52 65 73 65 74 2c 20 61 64 64  b, regReset, add
2b29c 72 52 65 73 65 74 29 3b 0a 20 20 20 20 20 20 56  rReset);.      V
2b29d 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
2b29e 72 65 73 65 74 20 61 63 63 75 6d 75 6c 61 74 6f  reset accumulato
2b29f 72 22 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  r"));..      /* 
2b2a0 55 70 64 61 74 65 20 74 68 65 20 61 67 67 72 65  Update the aggre
2b2a1 67 61 74 65 20 61 63 63 75 6d 75 6c 61 74 6f 72  gate accumulator
2b2a2 73 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 63  s based on the c
2b2a3 6f 6e 74 65 6e 74 20 6f 66 0a 20 20 20 20 20 20  ontent of.      
2b2a4 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 72  ** the current r
2b2a5 6f 77 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  ow.      */.    
2b2a6 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
2b2a7 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20  pHere(v, j1);.  
2b2a8 20 20 20 20 75 70 64 61 74 65 41 63 63 75 6d 75      updateAccumu
2b2a9 6c 61 74 6f 72 28 70 50 61 72 73 65 2c 20 26 73  lator(pParse, &s
2b2aa 41 67 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20  AggInfo);.      
2b2ab 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b2ac 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
2b2ad 20 31 2c 20 69 55 73 65 46 6c 61 67 29 3b 0a 20   1, iUseFlag);. 
2b2ae 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74       VdbeComment
2b2af 28 28 76 2c 20 22 69 6e 64 69 63 61 74 65 20 64  ((v, "indicate d
2b2b0 61 74 61 20 69 6e 20 61 63 63 75 6d 75 6c 61 74  ata in accumulat
2b2b1 6f 72 22 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  or"));..      /*
2b2b2 20 45 6e 64 20 6f 66 20 74 68 65 20 6c 6f 6f 70   End of the loop
2b2b3 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
2b2b4 69 66 28 20 67 72 6f 75 70 42 79 53 6f 72 74 20  if( groupBySort 
2b2b5 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
2b2b6 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2b2b7 4f 50 5f 4e 65 78 74 2c 20 73 41 67 67 49 6e 66  OP_Next, sAggInf
2b2b8 6f 2e 73 6f 72 74 69 6e 67 49 64 78 2c 20 61 64  o.sortingIdx, ad
2b2b9 64 72 54 6f 70 4f 66 4c 6f 6f 70 29 3b 0a 20 20  drTopOfLoop);.  
2b2ba 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2b2bb 20 20 20 73 71 6c 69 74 65 33 57 68 65 72 65 45     sqlite3WhereE
2b2bc 6e 64 28 70 57 49 6e 66 6f 29 3b 0a 20 20 20 20  nd(pWInfo);.    
2b2bd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
2b2be 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c 20 61  hangeToNoop(v, a
2b2bf 64 64 72 53 6f 72 74 69 6e 67 49 64 78 2c 20 31  ddrSortingIdx, 1
2b2c0 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  );.      }..    
2b2c1 20 20 2f 2a 20 4f 75 74 70 75 74 20 74 68 65 20    /* Output the 
2b2c2 66 69 6e 61 6c 20 72 6f 77 20 6f 66 20 72 65 73  final row of res
2b2c3 75 6c 74 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ult.      */.   
2b2c4 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2b2c5 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62  dOp2(v, OP_Gosub
2b2c6 2c 20 72 65 67 4f 75 74 70 75 74 52 6f 77 2c 20  , regOutputRow, 
2b2c7 61 64 64 72 4f 75 74 70 75 74 52 6f 77 29 3b 0a  addrOutputRow);.
2b2c8 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e        VdbeCommen
2b2c9 74 28 28 76 2c 20 22 6f 75 74 70 75 74 20 66 69  t((v, "output fi
2b2ca 6e 61 6c 20 72 6f 77 22 29 29 3b 0a 0a 20 20 20  nal row"));..   
2b2cb 20 20 20 2f 2a 20 4a 75 6d 70 20 6f 76 65 72 20     /* Jump over 
2b2cc 74 68 65 20 73 75 62 72 6f 75 74 69 6e 65 73 0a  the subroutines.
2b2cd 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73        */.      s
2b2ce 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2b2cf 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
2b2d0 61 64 64 72 45 6e 64 29 3b 0a 0a 20 20 20 20 20  addrEnd);..     
2b2d1 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20 73   /* Generate a s
2b2d2 75 62 72 6f 75 74 69 6e 65 20 74 68 61 74 20 6f  ubroutine that o
2b2d3 75 74 70 75 74 73 20 61 20 73 69 6e 67 6c 65 20  utputs a single 
2b2d4 72 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c  row of the resul
2b2d5 74 0a 20 20 20 20 20 20 2a 2a 20 73 65 74 2e 20  t.      ** set. 
2b2d6 20 54 68 69 73 20 73 75 62 72 6f 75 74 69 6e 65   This subroutine
2b2d7 20 66 69 72 73 74 20 6c 6f 6f 6b 73 20 61 74 20   first looks at 
2b2d8 74 68 65 20 69 55 73 65 46 6c 61 67 2e 20 20 49  the iUseFlag.  I
2b2d9 66 20 69 55 73 65 46 6c 61 67 0a 20 20 20 20 20  f iUseFlag.     
2b2da 20 2a 2a 20 69 73 20 6c 65 73 73 20 74 68 61 6e   ** is less than
2b2db 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 7a 65 72   or equal to zer
2b2dc 6f 2c 20 74 68 65 20 73 75 62 72 6f 75 74 69 6e  o, the subroutin
2b2dd 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 49  e is a no-op.  I
2b2de 66 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 70  f.      ** the p
2b2df 72 6f 63 65 73 73 69 6e 67 20 63 61 6c 6c 73 20  rocessing calls 
2b2e0 66 6f 72 20 74 68 65 20 71 75 65 72 79 20 74 6f  for the query to
2b2e1 20 61 62 6f 72 74 2c 20 74 68 69 73 20 73 75 62   abort, this sub
2b2e2 72 6f 75 74 69 6e 65 0a 20 20 20 20 20 20 2a 2a  routine.      **
2b2e3 20 69 6e 63 72 65 6d 65 6e 74 73 20 74 68 65 20   increments the 
2b2e4 69 41 62 6f 72 74 46 6c 61 67 20 6d 65 6d 6f 72  iAbortFlag memor
2b2e5 79 20 6c 6f 63 61 74 69 6f 6e 20 62 65 66 6f 72  y location befor
2b2e6 65 20 72 65 74 75 72 6e 69 6e 67 20 69 6e 0a 20  e returning in. 
2b2e7 20 20 20 20 20 2a 2a 20 6f 72 64 65 72 20 74 6f       ** order to
2b2e8 20 73 69 67 6e 61 6c 20 74 68 65 20 63 61 6c 6c   signal the call
2b2e9 65 72 20 74 6f 20 61 62 6f 72 74 2e 0a 20 20 20  er to abort..   
2b2ea 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 64 64 72     */.      addr
2b2eb 53 65 74 41 62 6f 72 74 20 3d 20 73 71 6c 69 74  SetAbort = sqlit
2b2ec 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
2b2ed 72 28 76 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  r(v);.      sqli
2b2ee 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2b2ef 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20   OP_Integer, 1, 
2b2f0 69 41 62 6f 72 74 46 6c 61 67 29 3b 0a 20 20 20  iAbortFlag);.   
2b2f1 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
2b2f2 76 2c 20 22 73 65 74 20 61 62 6f 72 74 20 66 6c  v, "set abort fl
2b2f3 61 67 22 29 29 3b 0a 20 20 20 20 20 20 73 71 6c  ag"));.      sql
2b2f4 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2b2f5 2c 20 4f 50 5f 52 65 74 75 72 6e 2c 20 72 65 67  , OP_Return, reg
2b2f6 4f 75 74 70 75 74 52 6f 77 29 3b 0a 20 20 20 20  OutputRow);.    
2b2f7 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
2b2f8 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61 64 64  olveLabel(v, add
2b2f9 72 4f 75 74 70 75 74 52 6f 77 29 3b 0a 20 20 20  rOutputRow);.   
2b2fa 20 20 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77     addrOutputRow
2b2fb 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75   = sqlite3VdbeCu
2b2fc 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20  rrentAddr(v);.  
2b2fd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2b2fe 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 50 6f  ddOp2(v, OP_IfPo
2b2ff 73 2c 20 69 55 73 65 46 6c 61 67 2c 20 61 64 64  s, iUseFlag, add
2b300 72 4f 75 74 70 75 74 52 6f 77 2b 32 29 3b 0a 20  rOutputRow+2);. 
2b301 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74       VdbeComment
2b302 28 28 76 2c 20 22 47 72 6f 75 70 62 79 20 72 65  ((v, "Groupby re
2b303 73 75 6c 74 20 67 65 6e 65 72 61 74 6f 72 20 65  sult generator e
2b304 6e 74 72 79 20 70 6f 69 6e 74 22 29 29 3b 0a 20  ntry point"));. 
2b305 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2b306 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 74  AddOp1(v, OP_Ret
2b307 75 72 6e 2c 20 72 65 67 4f 75 74 70 75 74 52 6f  urn, regOutputRo
2b308 77 29 3b 0a 20 20 20 20 20 20 66 69 6e 61 6c 69  w);.      finali
2b309 7a 65 41 67 67 46 75 6e 63 74 69 6f 6e 73 28 70  zeAggFunctions(p
2b30a 50 61 72 73 65 2c 20 26 73 41 67 67 49 6e 66 6f  Parse, &sAggInfo
2b30b 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2b30c 45 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72  ExprIfFalse(pPar
2b30d 73 65 2c 20 70 48 61 76 69 6e 67 2c 20 61 64 64  se, pHaving, add
2b30e 72 4f 75 74 70 75 74 52 6f 77 2b 31 2c 20 53 51  rOutputRow+1, SQ
2b30f 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29  LITE_JUMPIFNULL)
2b310 3b 0a 20 20 20 20 20 20 73 65 6c 65 63 74 49 6e  ;.      selectIn
2b311 6e 65 72 4c 6f 6f 70 28 70 50 61 72 73 65 2c 20  nerLoop(pParse, 
2b312 70 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20 30 2c  p, p->pEList, 0,
2b313 20 30 2c 20 70 4f 72 64 65 72 42 79 2c 0a 20 20   0, pOrderBy,.  
2b314 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b315 20 20 20 20 64 69 73 74 69 6e 63 74 2c 20 70 44      distinct, pD
2b316 65 73 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20  est,.           
2b317 20 20 20 20 20 20 20 20 20 20 20 61 64 64 72 4f             addrO
2b318 75 74 70 75 74 52 6f 77 2b 31 2c 20 61 64 64 72  utputRow+1, addr
2b319 53 65 74 41 62 6f 72 74 29 3b 0a 20 20 20 20 20  SetAbort);.     
2b31a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b31b 70 31 28 76 2c 20 4f 50 5f 52 65 74 75 72 6e 2c  p1(v, OP_Return,
2b31c 20 72 65 67 4f 75 74 70 75 74 52 6f 77 29 3b 0a   regOutputRow);.
2b31d 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e        VdbeCommen
2b31e 74 28 28 76 2c 20 22 65 6e 64 20 67 72 6f 75 70  t((v, "end group
2b31f 62 79 20 72 65 73 75 6c 74 20 67 65 6e 65 72 61  by result genera
2b320 74 6f 72 22 29 29 3b 0a 0a 20 20 20 20 20 20 2f  tor"));..      /
2b321 2a 20 47 65 6e 65 72 61 74 65 20 61 20 73 75 62  * Generate a sub
2b322 72 6f 75 74 69 6e 65 20 74 68 61 74 20 77 69 6c  routine that wil
2b323 6c 20 72 65 73 65 74 20 74 68 65 20 67 72 6f 75  l reset the grou
2b324 70 2d 62 79 20 61 63 63 75 6d 75 6c 61 74 6f 72  p-by accumulator
2b325 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
2b326 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c  sqlite3VdbeResol
2b327 76 65 4c 61 62 65 6c 28 76 2c 20 61 64 64 72 52  veLabel(v, addrR
2b328 65 73 65 74 29 3b 0a 20 20 20 20 20 20 72 65 73  eset);.      res
2b329 65 74 41 63 63 75 6d 75 6c 61 74 6f 72 28 70 50  etAccumulator(pP
2b32a 61 72 73 65 2c 20 26 73 41 67 67 49 6e 66 6f 29  arse, &sAggInfo)
2b32b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2b32c 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2b32d 52 65 74 75 72 6e 2c 20 72 65 67 52 65 73 65 74  Return, regReset
2b32e 29 3b 0a 20 20 20 20 20 0a 20 20 20 20 7d 20 2f  );.     .    } /
2b32f 2a 20 65 6e 64 69 66 20 70 47 72 6f 75 70 42 79  * endif pGroupBy
2b330 2e 20 20 42 65 67 69 6e 20 61 67 67 72 65 67 61  .  Begin aggrega
2b331 74 65 20 71 75 65 72 69 65 73 20 77 69 74 68 6f  te queries witho
2b332 75 74 20 47 52 4f 55 50 20 42 59 3a 20 2a 2f 0a  ut GROUP BY: */.
2b333 20 20 20 20 65 6c 73 65 20 7b 0a 20 20 20 20 20      else {.     
2b334 20 45 78 70 72 4c 69 73 74 20 2a 70 44 65 6c 20   ExprList *pDel 
2b335 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  = 0;.#ifndef SQL
2b336 49 54 45 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f  ITE_OMIT_BTREECO
2b337 55 4e 54 0a 20 20 20 20 20 20 54 61 62 6c 65 20  UNT.      Table 
2b338 2a 70 54 61 62 3b 0a 20 20 20 20 20 20 69 66 28  *pTab;.      if(
2b339 20 28 70 54 61 62 20 3d 20 69 73 53 69 6d 70 6c   (pTab = isSimpl
2b33a 65 43 6f 75 6e 74 28 70 2c 20 26 73 41 67 67 49  eCount(p, &sAggI
2b33b 6e 66 6f 29 29 21 3d 30 20 29 7b 0a 20 20 20 20  nfo))!=0 ){.    
2b33c 20 20 20 20 2f 2a 20 49 66 20 69 73 53 69 6d 70      /* If isSimp
2b33d 6c 65 43 6f 75 6e 74 28 29 20 72 65 74 75 72 6e  leCount() return
2b33e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
2b33f 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65   Table structure
2b340 2c 20 74 68 65 6e 0a 20 20 20 20 20 20 20 20 2a  , then.        *
2b341 2a 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d  * the SQL statem
2b342 65 6e 74 20 69 73 20 6f 66 20 74 68 65 20 66 6f  ent is of the fo
2b343 72 6d 3a 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20  rm:.        **. 
2b344 20 20 20 20 20 20 20 2a 2a 20 20 20 53 45 4c 45         **   SELE
2b345 43 54 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d  CT count(*) FROM
2b346 20 3c 74 62 6c 3e 0a 20 20 20 20 20 20 20 20 2a   <tbl>.        *
2b347 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 68 65  *.        ** whe
2b348 72 65 20 74 68 65 20 54 61 62 6c 65 20 73 74 72  re the Table str
2b349 75 63 74 75 72 65 20 72 65 74 75 72 6e 65 64 20  ucture returned 
2b34a 72 65 70 72 65 73 65 6e 74 73 20 74 61 62 6c 65  represents table
2b34b 20 3c 74 62 6c 3e 2e 0a 20 20 20 20 20 20 20 20   <tbl>..        
2b34c 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54 68  **.        ** Th
2b34d 69 73 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  is statement is 
2b34e 73 6f 20 63 6f 6d 6d 6f 6e 20 74 68 61 74 20 69  so common that i
2b34f 74 20 69 73 20 6f 70 74 69 6d 69 7a 65 64 20 73  t is optimized s
2b350 70 65 63 69 61 6c 6c 79 2e 20 54 68 65 0a 20 20  pecially. The.  
2b351 20 20 20 20 20 20 2a 2a 20 4f 50 5f 43 6f 75 6e        ** OP_Coun
2b352 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73  t instruction is
2b353 20 65 78 65 63 75 74 65 64 20 65 69 74 68 65 72   executed either
2b354 20 6f 6e 20 74 68 65 20 69 6e 74 6b 65 79 20 74   on the intkey t
2b355 61 62 6c 65 20 74 68 61 74 0a 20 20 20 20 20 20  able that.      
2b356 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 74 68    ** contains th
2b357 65 20 64 61 74 61 20 66 6f 72 20 74 61 62 6c 65  e data for table
2b358 20 3c 74 62 6c 3e 20 6f 72 20 6f 6e 20 6f 6e 65   <tbl> or on one
2b359 20 6f 66 20 69 74 73 20 69 6e 64 65 78 65 73 2e   of its indexes.
2b35a 20 49 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 69   It.        ** i
2b35b 73 20 62 65 74 74 65 72 20 74 6f 20 65 78 65 63  s better to exec
2b35c 75 74 65 20 74 68 65 20 6f 70 20 6f 6e 20 61 6e  ute the op on an
2b35d 20 69 6e 64 65 78 2c 20 61 73 20 69 6e 64 65 78   index, as index
2b35e 65 73 20 61 72 65 20 61 6c 6d 6f 73 74 0a 20 20  es are almost.  
2b35f 20 20 20 20 20 20 2a 2a 20 61 6c 77 61 79 73 20        ** always 
2b360 73 70 72 65 61 64 20 61 63 72 6f 73 73 20 6c 65  spread across le
2b361 73 73 20 70 61 67 65 73 20 74 68 61 6e 20 74 68  ss pages than th
2b362 65 69 72 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  eir correspondin
2b363 67 20 74 61 62 6c 65 73 2e 0a 20 20 20 20 20 20  g tables..      
2b364 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 6f 6e    */.        con
2b365 73 74 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c  st int iDb = sql
2b366 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
2b367 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54  x(pParse->db, pT
2b368 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  ab->pSchema);.  
2b369 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20        const int 
2b36a 69 43 73 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e  iCsr = pParse->n
2b36b 54 61 62 2b 2b 3b 20 20 20 20 20 2f 2a 20 43 75  Tab++;     /* Cu
2b36c 72 73 6f 72 20 74 6f 20 73 63 61 6e 20 62 2d 74  rsor to scan b-t
2b36d 72 65 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 49  ree */.        I
2b36e 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20  ndex *pIdx;     
2b36f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b370 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20      /* Iterator 
2b371 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20  variable */.    
2b372 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65      KeyInfo *pKe
2b373 79 49 6e 66 6f 20 3d 20 30 3b 20 20 20 20 20 20  yInfo = 0;      
2b374 20 20 20 20 20 20 20 20 20 2f 2a 20 4b 65 79 69           /* Keyi
2b375 6e 66 6f 20 66 6f 72 20 73 63 61 6e 6e 65 64 20  nfo for scanned 
2b376 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 20 20 20  index */.       
2b377 20 49 6e 64 65 78 20 2a 70 42 65 73 74 20 3d 20   Index *pBest = 
2b378 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
2b379 20 20 20 20 20 20 2f 2a 20 42 65 73 74 20 69 6e        /* Best in
2b37a 64 65 78 20 66 6f 75 6e 64 20 73 6f 20 66 61 72  dex found so far
2b37b 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20   */.        int 
2b37c 69 52 6f 6f 74 20 3d 20 70 54 61 62 2d 3e 74 6e  iRoot = pTab->tn
2b37d 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  um;             
2b37e 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66   /* Root page of
2b37f 20 73 63 61 6e 6e 65 64 20 62 2d 74 72 65 65 20   scanned b-tree 
2b380 2a 2f 0a 0a 20 20 20 20 20 20 20 20 73 71 6c 69  */..        sqli
2b381 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 68  te3CodeVerifySch
2b382 65 6d 61 28 70 50 61 72 73 65 2c 20 69 44 62 29  ema(pParse, iDb)
2b383 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2b384 33 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73  3TableLock(pPars
2b385 65 2c 20 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e  e, iDb, pTab->tn
2b386 75 6d 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61  um, 0, pTab->zNa
2b387 6d 65 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a  me);..        /*
2b388 20 53 65 61 72 63 68 20 66 6f 72 20 74 68 65 20   Search for the 
2b389 69 6e 64 65 78 20 74 68 61 74 20 68 61 73 20 74  index that has t
2b38a 68 65 20 6c 65 61 73 74 20 61 6d 6f 75 6e 74 20  he least amount 
2b38b 6f 66 20 63 6f 6c 75 6d 6e 73 2e 20 49 66 0a 20  of columns. If. 
2b38c 20 20 20 20 20 20 20 2a 2a 20 74 68 65 72 65 20         ** there 
2b38d 69 73 20 73 75 63 68 20 61 6e 20 69 6e 64 65 78  is such an index
2b38e 2c 20 61 6e 64 20 69 74 20 68 61 73 20 6c 65 73  , and it has les
2b38f 73 20 63 6f 6c 75 6d 6e 73 20 74 68 61 6e 20 74  s columns than t
2b390 68 65 20 74 61 62 6c 65 0a 20 20 20 20 20 20 20  he table.       
2b391 20 2a 2a 20 64 6f 65 73 2c 20 74 68 65 6e 20 77   ** does, then w
2b392 65 20 63 61 6e 20 61 73 73 75 6d 65 20 74 68 61  e can assume tha
2b393 74 20 69 74 20 63 6f 6e 73 75 6d 65 73 20 6c 65  t it consumes le
2b394 73 73 20 73 70 61 63 65 20 6f 6e 20 64 69 73 6b  ss space on disk
2b395 20 61 6e 64 0a 20 20 20 20 20 20 20 20 2a 2a 20   and.        ** 
2b396 77 69 6c 6c 20 74 68 65 72 65 66 6f 72 65 20 62  will therefore b
2b397 65 20 63 68 65 61 70 65 72 20 74 6f 20 73 63 61  e cheaper to sca
2b398 6e 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74  n to determine t
2b399 68 65 20 71 75 65 72 79 20 72 65 73 75 6c 74 2e  he query result.
2b39a 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 6e 20 74  .        ** In t
2b39b 68 69 73 20 63 61 73 65 20 73 65 74 20 69 52 6f  his case set iRo
2b39c 6f 74 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 70  ot to the root p
2b39d 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  age number of th
2b39e 65 20 69 6e 64 65 78 20 62 2d 74 72 65 65 0a 20  e index b-tree. 
2b39f 20 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 70 4b         ** and pK
2b3a0 65 79 49 6e 66 6f 20 74 6f 20 74 68 65 20 4b 65  eyInfo to the Ke
2b3a1 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  yInfo structure 
2b3a2 72 65 71 75 69 72 65 64 20 74 6f 20 6e 61 76 69  required to navi
2b3a3 67 61 74 65 20 74 68 65 0a 20 20 20 20 20 20 20  gate the.       
2b3a4 20 2a 2a 20 69 6e 64 65 78 2e 0a 20 20 20 20 20   ** index..     
2b3a5 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
2b3a6 20 49 6e 20 70 72 61 63 74 69 63 65 20 74 68 65   In practice the
2b3a7 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75   KeyInfo structu
2b3a8 72 65 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 75  re will not be u
2b3a9 73 65 64 2e 20 49 74 20 69 73 20 6f 6e 6c 79 20  sed. It is only 
2b3aa 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 61 73 73  .        ** pass
2b3ab 65 64 20 74 6f 20 6b 65 65 70 20 4f 50 5f 4f 70  ed to keep OP_Op
2b3ac 65 6e 52 65 61 64 20 68 61 70 70 79 2e 0a 20 20  enRead happy..  
2b3ad 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
2b3ae 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e   for(pIdx=pTab->
2b3af 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49  pIndex; pIdx; pI
2b3b0 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b  dx=pIdx->pNext){
2b3b1 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21  .          if( !
2b3b2 70 42 65 73 74 20 7c 7c 20 70 49 64 78 2d 3e 6e  pBest || pIdx->n
2b3b3 43 6f 6c 75 6d 6e 3c 70 42 65 73 74 2d 3e 6e 43  Column<pBest->nC
2b3b4 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20  olumn ){.       
2b3b5 20 20 20 20 20 70 42 65 73 74 20 3d 20 70 49 64       pBest = pId
2b3b6 78 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  x;.          }. 
2b3b7 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2b3b8 20 69 66 28 20 70 42 65 73 74 20 26 26 20 70 42   if( pBest && pB
2b3b9 65 73 74 2d 3e 6e 43 6f 6c 75 6d 6e 3c 70 54 61  est->nColumn<pTa
2b3ba 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20  b->nCol ){.     
2b3bb 20 20 20 20 20 69 52 6f 6f 74 20 3d 20 70 42 65       iRoot = pBe
2b3bc 73 74 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20 20 20  st->tnum;.      
2b3bd 20 20 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20 73      pKeyInfo = s
2b3be 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e  qlite3IndexKeyin
2b3bf 66 6f 28 70 50 61 72 73 65 2c 20 70 42 65 73 74  fo(pParse, pBest
2b3c0 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20  );.        }..  
2b3c1 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 61 20        /* Open a 
2b3c2 72 65 61 64 2d 6f 6e 6c 79 20 63 75 72 73 6f 72  read-only cursor
2b3c3 2c 20 65 78 65 63 75 74 65 20 74 68 65 20 4f 50  , execute the OP
2b3c4 5f 43 6f 75 6e 74 2c 20 63 6c 6f 73 65 20 74 68  _Count, close th
2b3c5 65 20 63 75 72 73 6f 72 2e 20 2a 2f 0a 20 20 20  e cursor. */.   
2b3c6 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2b3c7 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65  AddOp3(v, OP_Ope
2b3c8 6e 52 65 61 64 2c 20 69 43 73 72 2c 20 69 52 6f  nRead, iCsr, iRo
2b3c9 6f 74 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20  ot, iDb);.      
2b3ca 20 20 69 66 28 20 70 4b 65 79 49 6e 66 6f 20 29    if( pKeyInfo )
2b3cb 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
2b3cc 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
2b3cd 76 2c 20 2d 31 2c 20 28 63 68 61 72 20 2a 29 70  v, -1, (char *)p
2b3ce 4b 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49  KeyInfo, P4_KEYI
2b3cf 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20  NFO_HANDOFF);.  
2b3d0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2b3d1 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b3d2 32 28 76 2c 20 4f 50 5f 43 6f 75 6e 74 2c 20 69  2(v, OP_Count, i
2b3d3 43 73 72 2c 20 73 41 67 67 49 6e 66 6f 2e 61 46  Csr, sAggInfo.aF
2b3d4 75 6e 63 5b 30 5d 2e 69 4d 65 6d 29 3b 0a 20 20  unc[0].iMem);.  
2b3d5 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2b3d6 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c  eAddOp1(v, OP_Cl
2b3d7 6f 73 65 2c 20 69 43 73 72 29 3b 0a 20 20 20 20  ose, iCsr);.    
2b3d8 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f    }else.#endif /
2b3d9 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 54  * SQLITE_OMIT_BT
2b3da 52 45 45 43 4f 55 4e 54 20 2a 2f 0a 20 20 20 20  REECOUNT */.    
2b3db 20 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43    {.        /* C
2b3dc 68 65 63 6b 20 69 66 20 74 68 65 20 71 75 65 72  heck if the quer
2b3dd 79 20 69 73 20 6f 66 20 6f 6e 65 20 6f 66 20 74  y is of one of t
2b3de 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72  he following for
2b3df 6d 73 3a 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20  ms:.        **. 
2b3e0 20 20 20 20 20 20 20 2a 2a 20 20 20 53 45 4c 45         **   SELE
2b3e1 43 54 20 6d 69 6e 28 78 29 20 46 52 4f 4d 20 2e  CT min(x) FROM .
2b3e2 2e 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20  ...        **   
2b3e3 53 45 4c 45 43 54 20 6d 61 78 28 78 29 20 46 52  SELECT max(x) FR
2b3e4 4f 4d 20 2e 2e 2e 0a 20 20 20 20 20 20 20 20 2a  OM ....        *
2b3e5 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 66 20  *.        ** If 
2b3e6 69 74 20 69 73 2c 20 74 68 65 6e 20 61 73 6b 20  it is, then ask 
2b3e7 74 68 65 20 63 6f 64 65 20 69 6e 20 77 68 65 72  the code in wher
2b3e8 65 2e 63 20 74 6f 20 61 74 74 65 6d 70 74 20 74  e.c to attempt t
2b3e9 6f 20 73 6f 72 74 20 72 65 73 75 6c 74 73 0a 20  o sort results. 
2b3ea 20 20 20 20 20 20 20 2a 2a 20 61 73 20 69 66 20         ** as if 
2b3eb 74 68 65 72 65 20 77 61 73 20 61 6e 20 22 4f 52  there was an "OR
2b3ec 44 45 52 20 4f 4e 20 78 22 20 6f 72 20 22 4f 52  DER ON x" or "OR
2b3ed 44 45 52 20 4f 4e 20 78 20 44 45 53 43 22 20 63  DER ON x DESC" c
2b3ee 6c 61 75 73 65 2e 20 0a 20 20 20 20 20 20 20 20  lause. .        
2b3ef 2a 2a 20 49 66 20 77 68 65 72 65 2e 63 20 69 73  ** If where.c is
2b3f0 20 61 62 6c 65 20 74 6f 20 70 72 6f 64 75 63 65   able to produce
2b3f1 20 72 65 73 75 6c 74 73 20 73 6f 72 74 65 64 20   results sorted 
2b3f2 69 6e 20 74 68 69 73 20 6f 72 64 65 72 2c 20 74  in this order, t
2b3f3 68 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 61  hen.        ** a
2b3f4 64 64 20 76 64 62 65 20 63 6f 64 65 20 74 6f 20  dd vdbe code to 
2b3f5 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 65  break out of the
2b3f6 20 70 72 6f 63 65 73 73 69 6e 67 20 6c 6f 6f 70   processing loop
2b3f7 20 61 66 74 65 72 20 74 68 65 20 0a 20 20 20 20   after the .    
2b3f8 20 20 20 20 2a 2a 20 66 69 72 73 74 20 69 74 65      ** first ite
2b3f9 72 61 74 69 6f 6e 20 28 73 69 6e 63 65 20 74 68  ration (since th
2b3fa 65 20 66 69 72 73 74 20 69 74 65 72 61 74 69 6f  e first iteratio
2b3fb 6e 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 69 73  n of the loop is
2b3fc 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 67 75 61   .        ** gua
2b3fd 72 61 6e 74 65 65 64 20 74 6f 20 6f 70 65 72 61  ranteed to opera
2b3fe 74 65 20 6f 6e 20 74 68 65 20 72 6f 77 20 77 69  te on the row wi
2b3ff 74 68 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 6f  th the minimum o
2b400 72 20 6d 61 78 69 6d 75 6d 20 0a 20 20 20 20 20  r maximum .     
2b401 20 20 20 2a 2a 20 76 61 6c 75 65 20 6f 66 20 78     ** value of x
2b402 2c 20 74 68 65 20 6f 6e 6c 79 20 72 6f 77 20 72  , the only row r
2b403 65 71 75 69 72 65 64 29 2e 0a 20 20 20 20 20 20  equired)..      
2b404 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20    **.        ** 
2b405 41 20 73 70 65 63 69 61 6c 20 66 6c 61 67 20 6d  A special flag m
2b406 75 73 74 20 62 65 20 70 61 73 73 65 64 20 74 6f  ust be passed to
2b407 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67   sqlite3WhereBeg
2b408 69 6e 28 29 20 74 6f 20 73 6c 69 67 68 74 6c 79  in() to slightly
2b409 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d 6f 64 69  .        ** modi
2b40a 66 79 20 62 65 68 61 76 69 6f 75 72 20 61 73 20  fy behaviour as 
2b40b 66 6f 6c 6c 6f 77 73 3a 0a 20 20 20 20 20 20 20  follows:.       
2b40c 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 20   **.        **  
2b40d 20 2b 20 49 66 20 74 68 65 20 71 75 65 72 79 20   + If the query 
2b40e 69 73 20 61 20 22 53 45 4c 45 43 54 20 6d 69 6e  is a "SELECT min
2b40f 28 78 29 22 2c 20 74 68 65 6e 20 74 68 65 20 6c  (x)", then the l
2b410 6f 6f 70 20 63 6f 64 65 64 20 62 79 0a 20 20 20  oop coded by.   
2b411 20 20 20 20 20 2a 2a 20 20 20 20 20 77 68 65 72       **     wher
2b412 65 2e 63 20 73 68 6f 75 6c 64 20 6e 6f 74 20 69  e.c should not i
2b413 74 65 72 61 74 65 20 6f 76 65 72 20 61 6e 79 20  terate over any 
2b414 76 61 6c 75 65 73 20 77 69 74 68 20 61 20 4e 55  values with a NU
2b415 4c 4c 20 76 61 6c 75 65 0a 20 20 20 20 20 20 20  LL value.       
2b416 20 2a 2a 20 20 20 20 20 66 6f 72 20 78 2e 0a 20   **     for x.. 
2b417 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20         **.      
2b418 20 20 2a 2a 20 20 20 2b 20 54 68 65 20 6f 70 74    **   + The opt
2b419 69 6d 69 7a 65 72 20 63 6f 64 65 20 69 6e 20 77  imizer code in w
2b41a 68 65 72 65 2e 63 20 28 74 68 65 20 74 68 69 6e  here.c (the thin
2b41b 67 20 74 68 61 74 20 64 65 63 69 64 65 73 20 77  g that decides w
2b41c 68 69 63 68 0a 20 20 20 20 20 20 20 20 2a 2a 20  hich.        ** 
2b41d 20 20 20 20 69 6e 64 65 78 20 6f 72 20 69 6e 64      index or ind
2b41e 69 63 65 73 20 74 6f 20 75 73 65 29 20 73 68 6f  ices to use) sho
2b41f 75 6c 64 20 70 6c 61 63 65 20 61 20 64 69 66 66  uld place a diff
2b420 65 72 65 6e 74 20 70 72 69 6f 72 69 74 79 20 6f  erent priority o
2b421 6e 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20  n .        **   
2b422 20 20 73 61 74 69 73 66 79 69 6e 67 20 74 68 65    satisfying the
2b423 20 27 4f 52 44 45 52 20 42 59 27 20 63 6c 61 75   'ORDER BY' clau
2b424 73 65 20 74 68 61 6e 20 69 74 20 64 6f 65 73 20  se than it does 
2b425 69 6e 20 6f 74 68 65 72 20 63 61 73 65 73 2e 0a  in other cases..
2b426 20 20 20 20 20 20 20 20 2a 2a 20 20 20 20 20 52          **     R
2b427 65 66 65 72 20 74 6f 20 63 6f 64 65 20 61 6e 64  efer to code and
2b428 20 63 6f 6d 6d 65 6e 74 73 20 69 6e 20 77 68 65   comments in whe
2b429 72 65 2e 63 20 66 6f 72 20 64 65 74 61 69 6c 73  re.c for details
2b42a 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
2b42b 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70       ExprList *p
2b42c 4d 69 6e 4d 61 78 20 3d 20 30 3b 0a 20 20 20 20  MinMax = 0;.    
2b42d 20 20 20 20 75 38 20 66 6c 61 67 20 3d 20 6d 69      u8 flag = mi
2b42e 6e 4d 61 78 51 75 65 72 79 28 70 29 3b 0a 20 20  nMaxQuery(p);.  
2b42f 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 20 29        if( flag )
2b430 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  {.          asse
2b431 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70  rt( !ExprHasProp
2b432 65 72 74 79 28 70 2d 3e 70 45 4c 69 73 74 2d 3e  erty(p->pEList->
2b433 61 5b 30 5d 2e 70 45 78 70 72 2c 20 45 50 5f 78  a[0].pExpr, EP_x
2b434 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20  IsSelect) );.   
2b435 20 20 20 20 20 20 20 70 4d 69 6e 4d 61 78 20 3d         pMinMax =
2b436 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
2b437 44 75 70 28 64 62 2c 20 70 2d 3e 70 45 4c 69 73  Dup(db, p->pELis
2b438 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 78  t->a[0].pExpr->x
2b439 2e 70 4c 69 73 74 2c 30 29 3b 0a 20 20 20 20 20  .pList,0);.     
2b43a 20 20 20 20 20 70 44 65 6c 20 3d 20 70 4d 69 6e       pDel = pMin
2b43b 4d 61 78 3b 0a 20 20 20 20 20 20 20 20 20 20 69  Max;.          i
2b43c 66 28 20 70 4d 69 6e 4d 61 78 20 26 26 20 21 64  f( pMinMax && !d
2b43d 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2b43e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  ){.            p
2b43f 4d 69 6e 4d 61 78 2d 3e 61 5b 30 5d 2e 73 6f 72  MinMax->a[0].sor
2b440 74 4f 72 64 65 72 20 3d 20 66 6c 61 67 21 3d 57  tOrder = flag!=W
2b441 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e  HERE_ORDERBY_MIN
2b442 20 3f 31 3a 30 3b 0a 20 20 20 20 20 20 20 20 20   ?1:0;.         
2b443 20 20 20 70 4d 69 6e 4d 61 78 2d 3e 61 5b 30 5d     pMinMax->a[0]
2b444 2e 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f  .pExpr->op = TK_
2b445 43 4f 4c 55 4d 4e 3b 0a 20 20 20 20 20 20 20 20  COLUMN;.        
2b446 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
2b447 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73  .        /* This
2b448 20 63 61 73 65 20 72 75 6e 73 20 69 66 20 74 68   case runs if th
2b449 65 20 61 67 67 72 65 67 61 74 65 20 68 61 73 20  e aggregate has 
2b44a 6e 6f 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75  no GROUP BY clau
2b44b 73 65 2e 20 20 54 68 65 0a 20 20 20 20 20 20 20  se.  The.       
2b44c 20 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67 20 69   ** processing i
2b44d 73 20 6d 75 63 68 20 73 69 6d 70 6c 65 72 20 73  s much simpler s
2b44e 69 6e 63 65 20 74 68 65 72 65 20 69 73 20 6f 6e  ince there is on
2b44f 6c 79 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 0a  ly a single row.
2b450 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 6f 75          ** of ou
2b451 74 70 75 74 2e 0a 20 20 20 20 20 20 20 20 2a 2f  tput..        */
2b452 0a 20 20 20 20 20 20 20 20 72 65 73 65 74 41 63  .        resetAc
2b453 63 75 6d 75 6c 61 74 6f 72 28 70 50 61 72 73 65  cumulator(pParse
2b454 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a 20 20  , &sAggInfo);.  
2b455 20 20 20 20 20 20 70 57 49 6e 66 6f 20 3d 20 73        pWInfo = s
2b456 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e  qlite3WhereBegin
2b457 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73  (pParse, pTabLis
2b458 74 2c 20 70 57 68 65 72 65 2c 20 26 70 4d 69 6e  t, pWhere, &pMin
2b459 4d 61 78 2c 20 66 6c 61 67 29 3b 0a 20 20 20 20  Max, flag);.    
2b45a 20 20 20 20 69 66 28 20 70 57 49 6e 66 6f 3d 3d      if( pWInfo==
2b45b 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  0 ){.          s
2b45c 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65  qlite3ExprListDe
2b45d 6c 65 74 65 28 64 62 2c 20 70 44 65 6c 29 3b 0a  lete(db, pDel);.
2b45e 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 73            goto s
2b45f 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20 20  elect_end;.     
2b460 20 20 20 7d 0a 20 20 20 20 20 20 20 20 75 70 64     }.        upd
2b461 61 74 65 41 63 63 75 6d 75 6c 61 74 6f 72 28 70  ateAccumulator(p
2b462 50 61 72 73 65 2c 20 26 73 41 67 67 49 6e 66 6f  Parse, &sAggInfo
2b463 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21  );.        if( !
2b464 70 4d 69 6e 4d 61 78 20 26 26 20 66 6c 61 67 20  pMinMax && flag 
2b465 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
2b466 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2b467 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 70 57  , OP_Goto, 0, pW
2b468 49 6e 66 6f 2d 3e 69 42 72 65 61 6b 29 3b 0a 20  Info->iBreak);. 
2b469 20 20 20 20 20 20 20 20 20 56 64 62 65 43 6f 6d           VdbeCom
2b46a 6d 65 6e 74 28 28 76 2c 20 22 25 73 28 29 20 62  ment((v, "%s() b
2b46b 79 20 69 6e 64 65 78 22 2c 0a 20 20 20 20 20 20  y index",.      
2b46c 20 20 20 20 20 20 20 20 20 20 28 66 6c 61 67 3d            (flag=
2b46d 3d 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d  =WHERE_ORDERBY_M
2b46e 49 4e 3f 22 6d 69 6e 22 3a 22 6d 61 78 22 29 29  IN?"min":"max"))
2b46f 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
2b470 20 20 20 20 20 73 71 6c 69 74 65 33 57 68 65 72       sqlite3Wher
2b471 65 45 6e 64 28 70 57 49 6e 66 6f 29 3b 0a 20 20  eEnd(pWInfo);.  
2b472 20 20 20 20 20 20 66 69 6e 61 6c 69 7a 65 41 67        finalizeAg
2b473 67 46 75 6e 63 74 69 6f 6e 73 28 70 50 61 72 73  gFunctions(pPars
2b474 65 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a 20  e, &sAggInfo);. 
2b475 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 70 4f       }..      pO
2b476 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20 20 20  rderBy = 0;.    
2b477 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46    sqlite3ExprIfF
2b478 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 48 61  alse(pParse, pHa
2b479 76 69 6e 67 2c 20 61 64 64 72 45 6e 64 2c 20 53  ving, addrEnd, S
2b47a 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c  QLITE_JUMPIFNULL
2b47b 29 3b 0a 20 20 20 20 20 20 73 65 6c 65 63 74 49  );.      selectI
2b47c 6e 6e 65 72 4c 6f 6f 70 28 70 50 61 72 73 65 2c  nnerLoop(pParse,
2b47d 20 70 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20 30   p, p->pEList, 0
2b47e 2c 20 30 2c 20 30 2c 20 2d 31 2c 20 0a 20 20 20  , 0, 0, -1, .   
2b47f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b480 20 20 20 70 44 65 73 74 2c 20 61 64 64 72 45 6e     pDest, addrEn
2b481 64 2c 20 61 64 64 72 45 6e 64 29 3b 0a 20 20 20  d, addrEnd);.   
2b482 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69     sqlite3ExprLi
2b483 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 44 65  stDelete(db, pDe
2b484 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  l);.    }.    sq
2b485 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
2b486 4c 61 62 65 6c 28 76 2c 20 61 64 64 72 45 6e 64  Label(v, addrEnd
2b487 29 3b 0a 20 20 20 20 0a 20 20 7d 20 2f 2a 20 65  );.    .  } /* e
2b488 6e 64 69 66 20 61 67 67 72 65 67 61 74 65 20 71  ndif aggregate q
2b489 75 65 72 79 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66  uery */..  /* If
2b48a 20 74 68 65 72 65 20 69 73 20 61 6e 20 4f 52 44   there is an ORD
2b48b 45 52 20 42 59 20 63 6c 61 75 73 65 2c 20 74 68  ER BY clause, th
2b48c 65 6e 20 77 65 20 6e 65 65 64 20 74 6f 20 73 6f  en we need to so
2b48d 72 74 20 74 68 65 20 72 65 73 75 6c 74 73 0a 20  rt the results. 
2b48e 20 2a 2a 20 61 6e 64 20 73 65 6e 64 20 74 68 65   ** and send the
2b48f 6d 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63  m to the callbac
2b490 6b 20 6f 6e 65 20 62 79 20 6f 6e 65 2e 0a 20 20  k one by one..  
2b491 2a 2f 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42  */.  if( pOrderB
2b492 79 20 29 7b 0a 20 20 20 20 67 65 6e 65 72 61 74  y ){.    generat
2b493 65 53 6f 72 74 54 61 69 6c 28 70 50 61 72 73 65  eSortTail(pParse
2b494 2c 20 70 2c 20 76 2c 20 70 45 4c 69 73 74 2d 3e  , p, v, pEList->
2b495 6e 45 78 70 72 2c 20 70 44 65 73 74 29 3b 0a 20  nExpr, pDest);. 
2b496 20 7d 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 68 65   }..  /* Jump he
2b497 72 65 20 74 6f 20 73 6b 69 70 20 74 68 69 73 20  re to skip this 
2b498 71 75 65 72 79 0a 20 20 2a 2f 0a 20 20 73 71 6c  query.  */.  sql
2b499 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
2b49a 61 62 65 6c 28 76 2c 20 69 45 6e 64 29 3b 0a 0a  abel(v, iEnd);..
2b49b 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20    /* The SELECT 
2b49c 77 61 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79  was successfully
2b49d 20 63 6f 64 65 64 2e 20 20 20 53 65 74 20 74 68   coded.   Set th
2b49e 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 74 6f  e return code to
2b49f 20 30 0a 20 20 2a 2a 20 74 6f 20 69 6e 64 69 63   0.  ** to indic
2b4a0 61 74 65 20 6e 6f 20 65 72 72 6f 72 73 2e 0a 20  ate no errors.. 
2b4a1 20 2a 2f 0a 20 20 72 63 20 3d 20 30 3b 0a 0a 20   */.  rc = 0;.. 
2b4a2 20 2f 2a 20 43 6f 6e 74 72 6f 6c 20 6a 75 6d 70   /* Control jump
2b4a3 73 20 74 6f 20 68 65 72 65 20 69 66 20 61 6e 20  s to here if an 
2b4a4 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74  error is encount
2b4a5 65 72 65 64 20 61 62 6f 76 65 2c 20 6f 72 20 75  ered above, or u
2b4a6 70 6f 6e 0a 20 20 2a 2a 20 73 75 63 63 65 73 73  pon.  ** success
2b4a7 66 75 6c 20 63 6f 64 69 6e 67 20 6f 66 20 74 68  ful coding of th
2b4a8 65 20 53 45 4c 45 43 54 2e 0a 20 20 2a 2f 0a 73  e SELECT..  */.s
2b4a9 65 6c 65 63 74 5f 65 6e 64 3a 0a 0a 20 20 2f 2a  elect_end:..  /*
2b4aa 20 49 64 65 6e 74 69 66 79 20 63 6f 6c 75 6d 6e   Identify column
2b4ab 20 6e 61 6d 65 73 20 69 66 20 72 65 73 75 6c 74   names if result
2b4ac 73 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20  s of the SELECT 
2b4ad 61 72 65 20 74 6f 20 62 65 20 6f 75 74 70 75 74  are to be output
2b4ae 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d  ..  */.  if( rc=
2b4af 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 44  =SQLITE_OK && pD
2b4b0 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f  est->eDest==SRT_
2b4b1 4f 75 74 70 75 74 20 29 7b 0a 20 20 20 20 67 65  Output ){.    ge
2b4b2 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 4e 61 6d 65  nerateColumnName
2b4b3 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69  s(pParse, pTabLi
2b4b4 73 74 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 7d  st, pEList);.  }
2b4b5 0a 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ..  sqlite3DbFre
2b4b6 65 28 64 62 2c 20 73 41 67 67 49 6e 66 6f 2e 61  e(db, sAggInfo.a
2b4b7 43 6f 6c 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  Col);.  sqlite3D
2b4b8 62 46 72 65 65 28 64 62 2c 20 73 41 67 67 49 6e  bFree(db, sAggIn
2b4b9 66 6f 2e 61 46 75 6e 63 29 3b 0a 20 20 72 65 74  fo.aFunc);.  ret
2b4ba 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 64  urn rc;.}..#if d
2b4bb 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
2b4bc 42 55 47 29 0a 2f 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  BUG)./*.********
2b4bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b4be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b4bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b4c1 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66  *******.** The f
2b4c2 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 69 73  ollowing code is
2b4c3 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e   used for testin
2b4c4 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20  g and debugging 
2b4c5 6f 6e 6c 79 2e 20 20 54 68 65 20 63 6f 64 65 0a  only.  The code.
2b4c6 2a 2a 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20  ** that follows 
2b4c7 64 6f 65 73 20 6e 6f 74 20 61 70 70 65 61 72 20  does not appear 
2b4c8 69 6e 20 6e 6f 72 6d 61 6c 20 62 75 69 6c 64 73  in normal builds
2b4c9 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f  ..**.** These ro
2b4ca 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 20  utines are used 
2b4cb 74 6f 20 70 72 69 6e 74 20 6f 75 74 20 74 68 65  to print out the
2b4cc 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 6c 6c 20   content of all 
2b4cd 6f 72 20 70 61 72 74 20 6f 66 20 61 20 0a 2a 2a  or part of a .**
2b4ce 20 70 61 72 73 65 20 73 74 72 75 63 74 75 72 65   parse structure
2b4cf 73 20 73 75 63 68 20 61 73 20 53 65 6c 65 63 74  s such as Select
2b4d0 20 6f 72 20 45 78 70 72 2e 20 20 53 75 63 68 20   or Expr.  Such 
2b4d1 70 72 69 6e 74 6f 75 74 73 20 61 72 65 20 75 73  printouts are us
2b4d2 65 66 75 6c 0a 2a 2a 20 66 6f 72 20 68 65 6c 70  eful.** for help
2b4d3 69 6e 67 20 74 6f 20 75 6e 64 65 72 73 74 61 6e  ing to understan
2b4d4 64 20 77 68 61 74 20 69 73 20 68 61 70 70 65 6e  d what is happen
2b4d5 69 6e 67 20 69 6e 73 69 64 65 20 74 68 65 20 63  ing inside the c
2b4d6 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 0a 2a 2a  ode generator.**
2b4d7 20 64 75 72 69 6e 67 20 74 68 65 20 65 78 65 63   during the exec
2b4d8 75 74 69 6f 6e 20 6f 66 20 63 6f 6d 70 6c 65 78  ution of complex
2b4d9 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2b4da 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  ts..**.** These 
2b4db 72 6f 75 74 69 6e 65 20 61 72 65 20 6e 6f 74 20  routine are not 
2b4dc 63 61 6c 6c 65 64 20 61 6e 79 77 68 65 72 65 20  called anywhere 
2b4dd 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20  from within the 
2b4de 6e 6f 72 6d 61 6c 0a 2a 2a 20 63 6f 64 65 20 62  normal.** code b
2b4df 61 73 65 2e 20 20 54 68 65 6e 20 61 72 65 20 69  ase.  Then are i
2b4e0 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 63 61  ntended to be ca
2b4e1 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e  lled from within
2b4e2 20 74 68 65 20 64 65 62 75 67 67 65 72 0a 2a 2a   the debugger.**
2b4e3 20 6f 72 20 66 72 6f 6d 20 74 65 6d 70 6f 72 61   or from tempora
2b4e4 72 79 20 22 70 72 69 6e 74 66 22 20 73 74 61 74  ry "printf" stat
2b4e5 65 6d 65 6e 74 73 20 69 6e 73 65 72 74 65 64 20  ements inserted 
2b4e6 66 6f 72 20 64 65 62 75 67 67 69 6e 67 2e 0a 2a  for debugging..*
2b4e7 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2b4e8 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 69   void sqlite3Pri
2b4e9 6e 74 45 78 70 72 28 45 78 70 72 20 2a 70 29 7b  ntExpr(Expr *p){
2b4ea 0a 20 20 69 66 28 20 21 45 78 70 72 48 61 73 50  .  if( !ExprHasP
2b4eb 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 49 6e  roperty(p, EP_In
2b4ec 74 56 61 6c 75 65 29 20 26 26 20 70 2d 3e 75 2e  tValue) && p->u.
2b4ed 7a 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 73 71  zToken ){.    sq
2b4ee 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
2b4ef 28 22 28 25 73 22 2c 20 70 2d 3e 75 2e 7a 54 6f  ("(%s", p->u.zTo
2b4f0 6b 65 6e 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ken);.  }else{. 
2b4f1 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
2b4f2 72 69 6e 74 66 28 22 28 25 64 22 2c 20 70 2d 3e  rintf("(%d", p->
2b4f3 6f 70 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  op);.  }.  if( p
2b4f4 2d 3e 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 73  ->pLeft ){.    s
2b4f5 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
2b4f6 66 28 22 20 22 29 3b 0a 20 20 20 20 73 71 6c 69  f(" ");.    sqli
2b4f7 74 65 33 50 72 69 6e 74 45 78 70 72 28 70 2d 3e  te3PrintExpr(p->
2b4f8 70 4c 65 66 74 29 3b 0a 20 20 7d 0a 20 20 69 66  pLeft);.  }.  if
2b4f9 28 20 70 2d 3e 70 52 69 67 68 74 20 29 7b 0a 20  ( p->pRight ){. 
2b4fa 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
2b4fb 72 69 6e 74 66 28 22 20 22 29 3b 0a 20 20 20 20  rintf(" ");.    
2b4fc 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78 70 72  sqlite3PrintExpr
2b4fd 28 70 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 7d  (p->pRight);.  }
2b4fe 0a 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50  .  sqlite3DebugP
2b4ff 72 69 6e 74 66 28 22 29 22 29 3b 0a 7d 0a 53 51  rintf(")");.}.SQ
2b500 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2b501 64 20 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78  d sqlite3PrintEx
2b502 70 72 4c 69 73 74 28 45 78 70 72 4c 69 73 74 20  prList(ExprList 
2b503 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69  *pList){.  int i
2b504 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
2b505 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b  List->nExpr; i++
2b506 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 72  ){.    sqlite3Pr
2b507 69 6e 74 45 78 70 72 28 70 4c 69 73 74 2d 3e 61  intExpr(pList->a
2b508 5b 69 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20  [i].pExpr);.    
2b509 69 66 28 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78  if( i<pList->nEx
2b50a 70 72 2d 31 20 29 7b 0a 20 20 20 20 20 20 73 71  pr-1 ){.      sq
2b50b 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
2b50c 28 22 2c 20 22 29 3b 0a 20 20 20 20 7d 0a 20 20  (", ");.    }.  
2b50d 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  }.}.SQLITE_PRIVA
2b50e 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
2b50f 72 69 6e 74 53 65 6c 65 63 74 28 53 65 6c 65 63  rintSelect(Selec
2b510 74 20 2a 70 2c 20 69 6e 74 20 69 6e 64 65 6e 74  t *p, int indent
2b511 29 7b 0a 20 20 73 71 6c 69 74 65 33 44 65 62 75  ){.  sqlite3Debu
2b512 67 50 72 69 6e 74 66 28 22 25 2a 73 53 45 4c 45  gPrintf("%*sSELE
2b513 43 54 28 25 70 29 20 22 2c 20 69 6e 64 65 6e 74  CT(%p) ", indent
2b514 2c 20 22 22 2c 20 70 29 3b 0a 20 20 73 71 6c 69  , "", p);.  sqli
2b515 74 65 33 50 72 69 6e 74 45 78 70 72 4c 69 73 74  te3PrintExprList
2b516 28 70 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 73  (p->pEList);.  s
2b517 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
2b518 66 28 22 5c 6e 22 29 3b 0a 20 20 69 66 28 20 70  f("\n");.  if( p
2b519 2d 3e 70 53 72 63 20 29 7b 0a 20 20 20 20 63 68  ->pSrc ){.    ch
2b51a 61 72 20 2a 7a 50 72 65 66 69 78 3b 0a 20 20 20  ar *zPrefix;.   
2b51b 20 69 6e 74 20 69 3b 0a 20 20 20 20 7a 50 72 65   int i;.    zPre
2b51c 66 69 78 20 3d 20 22 46 52 4f 4d 22 3b 0a 20 20  fix = "FROM";.  
2b51d 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e    for(i=0; i<p->
2b51e 70 53 72 63 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29  pSrc->nSrc; i++)
2b51f 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 53  {.      struct S
2b520 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  rcList_item *pIt
2b521 65 6d 20 3d 20 26 70 2d 3e 70 53 72 63 2d 3e 61  em = &p->pSrc->a
2b522 5b 69 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  [i];.      sqlit
2b523 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 25  e3DebugPrintf("%
2b524 2a 73 20 22 2c 20 69 6e 64 65 6e 74 2b 36 2c 20  *s ", indent+6, 
2b525 7a 50 72 65 66 69 78 29 3b 0a 20 20 20 20 20 20  zPrefix);.      
2b526 7a 50 72 65 66 69 78 20 3d 20 22 22 3b 0a 20 20  zPrefix = "";.  
2b527 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 70      if( pItem->p
2b528 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20  Select ){.      
2b529 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
2b52a 69 6e 74 66 28 22 28 5c 6e 22 29 3b 0a 20 20 20  intf("(\n");.   
2b52b 20 20 20 20 20 73 71 6c 69 74 65 33 50 72 69 6e       sqlite3Prin
2b52c 74 53 65 6c 65 63 74 28 70 49 74 65 6d 2d 3e 70  tSelect(pItem->p
2b52d 53 65 6c 65 63 74 2c 20 69 6e 64 65 6e 74 2b 31  Select, indent+1
2b52e 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  0);.        sqli
2b52f 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
2b530 25 2a 73 29 22 2c 20 69 6e 64 65 6e 74 2b 38 2c  %*s)", indent+8,
2b531 20 22 22 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73   "");.      }els
2b532 65 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 4e 61  e if( pItem->zNa
2b533 6d 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  me ){.        sq
2b534 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
2b535 28 22 25 73 22 2c 20 70 49 74 65 6d 2d 3e 7a 4e  ("%s", pItem->zN
2b536 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ame);.      }.  
2b537 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 70      if( pItem->p
2b538 54 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20 73  Tab ){.        s
2b539 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
2b53a 66 28 22 28 74 61 62 6c 65 3a 20 25 73 29 22 2c  f("(table: %s)",
2b53b 20 70 49 74 65 6d 2d 3e 70 54 61 62 2d 3e 7a 4e   pItem->pTab->zN
2b53c 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ame);.      }.  
2b53d 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a      if( pItem->z
2b53e 41 6c 69 61 73 20 29 7b 0a 20 20 20 20 20 20 20  Alias ){.       
2b53f 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
2b540 6e 74 66 28 22 20 41 53 20 25 73 22 2c 20 70 49  ntf(" AS %s", pI
2b541 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20 20  tem->zAlias);.  
2b542 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
2b543 69 3c 70 2d 3e 70 53 72 63 2d 3e 6e 53 72 63 2d  i<p->pSrc->nSrc-
2b544 31 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  1 ){.        sql
2b545 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
2b546 22 2c 22 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ",");.      }.  
2b547 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67      sqlite3Debug
2b548 50 72 69 6e 74 66 28 22 5c 6e 22 29 3b 0a 20 20  Printf("\n");.  
2b549 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d    }.  }.  if( p-
2b54a 3e 70 57 68 65 72 65 20 29 7b 0a 20 20 20 20 73  >pWhere ){.    s
2b54b 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
2b54c 66 28 22 25 2a 73 20 57 48 45 52 45 20 22 2c 20  f("%*s WHERE ", 
2b54d 69 6e 64 65 6e 74 2c 20 22 22 29 3b 0a 20 20 20  indent, "");.   
2b54e 20 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78 70   sqlite3PrintExp
2b54f 72 28 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20  r(p->pWhere);.  
2b550 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
2b551 69 6e 74 66 28 22 5c 6e 22 29 3b 0a 20 20 7d 0a  intf("\n");.  }.
2b552 20 20 69 66 28 20 70 2d 3e 70 47 72 6f 75 70 42    if( p->pGroupB
2b553 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  y ){.    sqlite3
2b554 44 65 62 75 67 50 72 69 6e 74 66 28 22 25 2a 73  DebugPrintf("%*s
2b555 20 47 52 4f 55 50 20 42 59 20 22 2c 20 69 6e 64   GROUP BY ", ind
2b556 65 6e 74 2c 20 22 22 29 3b 0a 20 20 20 20 73 71  ent, "");.    sq
2b557 6c 69 74 65 33 50 72 69 6e 74 45 78 70 72 4c 69  lite3PrintExprLi
2b558 73 74 28 70 2d 3e 70 47 72 6f 75 70 42 79 29 3b  st(p->pGroupBy);
2b559 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75  .    sqlite3Debu
2b55a 67 50 72 69 6e 74 66 28 22 5c 6e 22 29 3b 0a 20  gPrintf("\n");. 
2b55b 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 48 61 76   }.  if( p->pHav
2b55c 69 6e 67 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ing ){.    sqlit
2b55d 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 25  e3DebugPrintf("%
2b55e 2a 73 20 48 41 56 49 4e 47 20 22 2c 20 69 6e 64  *s HAVING ", ind
2b55f 65 6e 74 2c 20 22 22 29 3b 0a 20 20 20 20 73 71  ent, "");.    sq
2b560 6c 69 74 65 33 50 72 69 6e 74 45 78 70 72 28 70  lite3PrintExpr(p
2b561 2d 3e 70 48 61 76 69 6e 67 29 3b 0a 20 20 20 20  ->pHaving);.    
2b562 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
2b563 74 66 28 22 5c 6e 22 29 3b 0a 20 20 7d 0a 20 20  tf("\n");.  }.  
2b564 69 66 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 20  if( p->pOrderBy 
2b565 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  ){.    sqlite3De
2b566 62 75 67 50 72 69 6e 74 66 28 22 25 2a 73 20 4f  bugPrintf("%*s O
2b567 52 44 45 52 20 42 59 20 22 2c 20 69 6e 64 65 6e  RDER BY ", inden
2b568 74 2c 20 22 22 29 3b 0a 20 20 20 20 73 71 6c 69  t, "");.    sqli
2b569 74 65 33 50 72 69 6e 74 45 78 70 72 4c 69 73 74  te3PrintExprList
2b56a 28 70 2d 3e 70 4f 72 64 65 72 42 79 29 3b 0a 20  (p->pOrderBy);. 
2b56b 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
2b56c 72 69 6e 74 66 28 22 5c 6e 22 29 3b 0a 20 20 7d  rintf("\n");.  }
2b56d 0a 7d 0a 2f 2a 20 45 6e 64 20 6f 66 20 74 68 65  .}./* End of the
2b56e 20 73 74 72 75 63 74 75 72 65 20 64 65 62 75 67   structure debug
2b56f 20 70 72 69 6e 74 69 6e 67 20 63 6f 64 65 0a 2a   printing code.*
2b570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b571 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b572 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b573 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b574 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65  ************/.#e
2b575 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28  ndif /* defined(
2b576 53 51 4c 49 54 45 5f 54 45 53 54 29 20 7c 7c 20  SQLITE_TEST) || 
2b577 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
2b578 45 42 55 47 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  EBUG) */../*****
2b579 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
2b57a 20 73 65 6c 65 63 74 2e 63 20 2a 2a 2a 2a 2a 2a   select.c ******
2b57b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b57c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b57d 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
2b57e 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
2b57f 66 69 6c 65 20 74 61 62 6c 65 2e 63 20 2a 2a 2a  file table.c ***
2b580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b581 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b582 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
2b583 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
2b584 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
2b585 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
2b586 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
2b587 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
2b588 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
2b589 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
2b58a 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
2b58b 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
2b58c 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
2b58d 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
2b58e 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
2b58f 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
2b590 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
2b591 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
2b592 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
2b593 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
2b594 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
2b595 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
2b596 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b597 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b598 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b599 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b59a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
2b59b 6e 74 61 69 6e 73 20 74 68 65 20 73 71 6c 69 74  ntains the sqlit
2b59c 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 61  e3_get_table() a
2b59d 6e 64 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f  nd sqlite3_free_
2b59e 74 61 62 6c 65 28 29 0a 2a 2a 20 69 6e 74 65 72  table().** inter
2b59f 66 61 63 65 20 72 6f 75 74 69 6e 65 73 2e 20 20  face routines.  
2b5a0 54 68 65 73 65 20 61 72 65 20 6a 75 73 74 20 77  These are just w
2b5a1 72 61 70 70 65 72 73 20 61 72 6f 75 6e 64 20 74  rappers around t
2b5a2 68 65 20 6d 61 69 6e 0a 2a 2a 20 69 6e 74 65 72  he main.** inter
2b5a3 66 61 63 65 20 72 6f 75 74 69 6e 65 20 6f 66 20  face routine of 
2b5a4 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 2e 0a  sqlite3_exec()..
2b5a5 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74  **.** These rout
2b5a6 69 6e 65 73 20 61 72 65 20 69 6e 20 61 20 73 65  ines are in a se
2b5a7 70 61 72 61 74 65 20 66 69 6c 65 73 20 73 6f 20  parate files so 
2b5a8 74 68 61 74 20 74 68 65 79 20 77 69 6c 6c 20 6e  that they will n
2b5a9 6f 74 20 62 65 20 6c 69 6e 6b 65 64 0a 2a 2a 20  ot be linked.** 
2b5aa 69 66 20 74 68 65 79 20 61 72 65 20 6e 6f 74 20  if they are not 
2b5ab 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  used..**.** $Id:
2b5ac 20 74 61 62 6c 65 2e 63 2c 76 20 31 2e 34 30 20   table.c,v 1.40 
2b5ad 32 30 30 39 2f 30 34 2f 31 30 20 31 34 3a 32 38  2009/04/10 14:28
2b5ae 3a 30 30 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :00 drh Exp $.*/
2b5af 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2b5b0 5f 4f 4d 49 54 5f 47 45 54 5f 54 41 42 4c 45 0a  _OMIT_GET_TABLE.
2b5b1 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75  ./*.** This stru
2b5b2 63 74 75 72 65 20 69 73 20 75 73 65 64 20 74 6f  cture is used to
2b5b3 20 70 61 73 73 20 64 61 74 61 20 66 72 6f 6d 20   pass data from 
2b5b4 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c  sqlite3_get_tabl
2b5b5 65 28 29 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74  e() through.** t
2b5b6 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66  o the callback f
2b5b7 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 73 20  unction is uses 
2b5b8 74 6f 20 62 75 69 6c 64 20 74 68 65 20 72 65 73  to build the res
2b5b9 75 6c 74 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ult..*/.typedef 
2b5ba 73 74 72 75 63 74 20 54 61 62 52 65 73 75 6c 74  struct TabResult
2b5bb 20 7b 0a 20 20 63 68 61 72 20 2a 2a 61 7a 52 65   {.  char **azRe
2b5bc 73 75 6c 74 3b 20 20 20 2f 2a 20 41 63 63 75 6d  sult;   /* Accum
2b5bd 75 6c 61 74 65 64 20 6f 75 74 70 75 74 20 2a 2f  ulated output */
2b5be 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67  .  char *zErrMsg
2b5bf 3b 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d  ;     /* Error m
2b5c0 65 73 73 61 67 65 20 74 65 78 74 2c 20 69 66 20  essage text, if 
2b5c1 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  an error occurs 
2b5c2 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b  */.  int nAlloc;
2b5c3 20 20 20 20 20 20 20 20 2f 2a 20 53 6c 6f 74 73          /* Slots
2b5c4 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61   allocated for a
2b5c5 7a 52 65 73 75 6c 74 5b 5d 20 2a 2f 0a 20 20 69  zResult[] */.  i
2b5c6 6e 74 20 6e 52 6f 77 3b 20 20 20 20 20 20 20 20  nt nRow;        
2b5c7 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72    /* Number of r
2b5c8 6f 77 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c  ows in the resul
2b5c9 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75  t */.  int nColu
2b5ca 6d 6e 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  mn;       /* Num
2b5cb 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
2b5cc 6e 20 74 68 65 20 72 65 73 75 6c 74 20 2a 2f 0a  n the result */.
2b5cd 20 20 69 6e 74 20 6e 44 61 74 61 3b 20 20 20 20    int nData;    
2b5ce 20 20 20 20 20 2f 2a 20 53 6c 6f 74 73 20 75 73       /* Slots us
2b5cf 65 64 20 69 6e 20 61 7a 52 65 73 75 6c 74 5b 5d  ed in azResult[]
2b5d0 2e 20 20 28 6e 52 6f 77 2b 31 29 2a 6e 43 6f 6c  .  (nRow+1)*nCol
2b5d1 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b  umn */.  int rc;
2b5d2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
2b5d3 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20  eturn code from 
2b5d4 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 2a  sqlite3_exec() *
2b5d5 2f 0a 7d 20 54 61 62 52 65 73 75 6c 74 3b 0a 0a  /.} TabResult;..
2b5d6 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
2b5d7 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63  ne is called onc
2b5d8 65 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 69  e for each row i
2b5d9 6e 20 74 68 65 20 72 65 73 75 6c 74 20 74 61 62  n the result tab
2b5da 6c 65 2e 20 20 49 74 73 20 6a 6f 62 0a 2a 2a 20  le.  Its job.** 
2b5db 69 73 20 74 6f 20 66 69 6c 6c 20 69 6e 20 74 68  is to fill in th
2b5dc 65 20 54 61 62 52 65 73 75 6c 74 20 73 74 72 75  e TabResult stru
2b5dd 63 74 75 72 65 20 61 70 70 72 6f 70 72 69 61 74  cture appropriat
2b5de 65 6c 79 2c 20 61 6c 6c 6f 63 61 74 69 6e 67 20  ely, allocating 
2b5df 6e 65 77 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 73  new.** memory as
2b5e0 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 73   necessary..*/.s
2b5e1 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65  tatic int sqlite
2b5e2 33 5f 67 65 74 5f 74 61 62 6c 65 5f 63 62 28 76  3_get_table_cb(v
2b5e3 6f 69 64 20 2a 70 41 72 67 2c 20 69 6e 74 20 6e  oid *pArg, int n
2b5e4 43 6f 6c 2c 20 63 68 61 72 20 2a 2a 61 72 67 76  Col, char **argv
2b5e5 2c 20 63 68 61 72 20 2a 2a 63 6f 6c 76 29 7b 0a  , char **colv){.
2b5e6 20 20 54 61 62 52 65 73 75 6c 74 20 2a 70 20 3d    TabResult *p =
2b5e7 20 28 54 61 62 52 65 73 75 6c 74 2a 29 70 41 72   (TabResult*)pAr
2b5e8 67 3b 20 20 2f 2a 20 52 65 73 75 6c 74 20 61 63  g;  /* Result ac
2b5e9 63 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 69  cumulator */.  i
2b5ea 6e 74 20 6e 65 65 64 3b 20 20 20 20 20 20 20 20  nt need;        
2b5eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b5ec 20 2f 2a 20 53 6c 6f 74 73 20 6e 65 65 64 65 64   /* Slots needed
2b5ed 20 69 6e 20 70 2d 3e 61 7a 52 65 73 75 6c 74 5b   in p->azResult[
2b5ee 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20  ] */.  int i;   
2b5ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b5f0 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
2b5f1 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 63 68   counter */.  ch
2b5f2 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20  ar *z;          
2b5f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b5f4 2f 2a 20 41 20 73 69 6e 67 6c 65 20 63 6f 6c 75  /* A single colu
2b5f5 6d 6e 20 6f 66 20 72 65 73 75 6c 74 20 2a 2f 0a  mn of result */.
2b5f6 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20  .  /* Make sure 
2b5f7 74 68 65 72 65 20 69 73 20 65 6e 6f 75 67 68 20  there is enough 
2b5f8 73 70 61 63 65 20 69 6e 20 70 2d 3e 61 7a 52 65  space in p->azRe
2b5f9 73 75 6c 74 20 74 6f 20 68 6f 6c 64 20 65 76 65  sult to hold eve
2b5fa 72 79 74 68 69 6e 67 0a 20 20 2a 2a 20 77 65 20  rything.  ** we 
2b5fb 6e 65 65 64 20 74 6f 20 72 65 6d 65 6d 62 65 72  need to remember
2b5fc 20 66 72 6f 6d 20 74 68 69 73 20 69 6e 76 6f 63   from this invoc
2b5fd 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 61 6c  ation of the cal
2b5fe 6c 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66  lback..  */.  if
2b5ff 28 20 70 2d 3e 6e 52 6f 77 3d 3d 30 20 26 26 20  ( p->nRow==0 && 
2b600 61 72 67 76 21 3d 30 20 29 7b 0a 20 20 20 20 6e  argv!=0 ){.    n
2b601 65 65 64 20 3d 20 6e 43 6f 6c 2a 32 3b 0a 20 20  eed = nCol*2;.  
2b602 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 65 65 64 20  }else{.    need 
2b603 3d 20 6e 43 6f 6c 3b 0a 20 20 7d 0a 20 20 69 66  = nCol;.  }.  if
2b604 28 20 70 2d 3e 6e 44 61 74 61 20 2b 20 6e 65 65  ( p->nData + nee
2b605 64 20 3e 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29 7b  d > p->nAlloc ){
2b606 0a 20 20 20 20 63 68 61 72 20 2a 2a 61 7a 4e 65  .    char **azNe
2b607 77 3b 0a 20 20 20 20 70 2d 3e 6e 41 6c 6c 6f 63  w;.    p->nAlloc
2b608 20 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 2a 32 20 2b   = p->nAlloc*2 +
2b609 20 6e 65 65 64 3b 0a 20 20 20 20 61 7a 4e 65 77   need;.    azNew
2b60a 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c   = sqlite3_reall
2b60b 6f 63 28 20 70 2d 3e 61 7a 52 65 73 75 6c 74 2c  oc( p->azResult,
2b60c 20 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2a 70   sizeof(char*)*p
2b60d 2d 3e 6e 41 6c 6c 6f 63 20 29 3b 0a 20 20 20 20  ->nAlloc );.    
2b60e 69 66 28 20 61 7a 4e 65 77 3d 3d 30 20 29 20 67  if( azNew==0 ) g
2b60f 6f 74 6f 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65  oto malloc_faile
2b610 64 3b 0a 20 20 20 20 70 2d 3e 61 7a 52 65 73 75  d;.    p->azResu
2b611 6c 74 20 3d 20 61 7a 4e 65 77 3b 0a 20 20 7d 0a  lt = azNew;.  }.
2b612 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73  .  /* If this is
2b613 20 74 68 65 20 66 69 72 73 74 20 72 6f 77 2c 20   the first row, 
2b614 74 68 65 6e 20 67 65 6e 65 72 61 74 65 20 61 6e  then generate an
2b615 20 65 78 74 72 61 20 72 6f 77 20 63 6f 6e 74 61   extra row conta
2b616 69 6e 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6e  ining.  ** the n
2b617 61 6d 65 73 20 6f 66 20 61 6c 6c 20 63 6f 6c 75  ames of all colu
2b618 6d 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  mns..  */.  if( 
2b619 70 2d 3e 6e 52 6f 77 3d 3d 30 20 29 7b 0a 20 20  p->nRow==0 ){.  
2b61a 20 20 70 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 6e    p->nColumn = n
2b61b 43 6f 6c 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  Col;.    for(i=0
2b61c 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a  ; i<nCol; i++){.
2b61d 20 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65        z = sqlite
2b61e 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 22 2c 20  3_mprintf("%s", 
2b61f 63 6f 6c 76 5b 69 5d 29 3b 0a 20 20 20 20 20 20  colv[i]);.      
2b620 69 66 28 20 7a 3d 3d 30 20 29 20 67 6f 74 6f 20  if( z==0 ) goto 
2b621 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b 0a 20  malloc_failed;. 
2b622 20 20 20 20 20 70 2d 3e 61 7a 52 65 73 75 6c 74       p->azResult
2b623 5b 70 2d 3e 6e 44 61 74 61 2b 2b 5d 20 3d 20 7a  [p->nData++] = z
2b624 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20  ;.    }.  }else 
2b625 69 66 28 20 70 2d 3e 6e 43 6f 6c 75 6d 6e 21 3d  if( p->nColumn!=
2b626 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69  nCol ){.    sqli
2b627 74 65 33 5f 66 72 65 65 28 70 2d 3e 7a 45 72 72  te3_free(p->zErr
2b628 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72  Msg);.    p->zEr
2b629 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 5f 6d  rMsg = sqlite3_m
2b62a 70 72 69 6e 74 66 28 0a 20 20 20 20 20 20 20 22  printf(.       "
2b62b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c  sqlite3_get_tabl
2b62c 65 28 29 20 63 61 6c 6c 65 64 20 77 69 74 68 20  e() called with 
2b62d 74 77 6f 20 6f 72 20 6d 6f 72 65 20 69 6e 63 6f  two or more inco
2b62e 6d 70 61 74 69 62 6c 65 20 71 75 65 72 69 65 73  mpatible queries
2b62f 22 0a 20 20 20 20 29 3b 0a 20 20 20 20 70 2d 3e  ".    );.    p->
2b630 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
2b631 52 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  R;.    return 1;
2b632 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79 20  .  }..  /* Copy 
2b633 6f 76 65 72 20 74 68 65 20 72 6f 77 20 64 61 74  over the row dat
2b634 61 0a 20 20 2a 2f 0a 20 20 69 66 28 20 61 72 67  a.  */.  if( arg
2b635 76 21 3d 30 20 29 7b 0a 20 20 20 20 66 6f 72 28  v!=0 ){.    for(
2b636 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b  i=0; i<nCol; i++
2b637 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 72 67  ){.      if( arg
2b638 76 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20  v[i]==0 ){.     
2b639 20 20 20 7a 20 3d 20 30 3b 0a 20 20 20 20 20 20     z = 0;.      
2b63a 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69  }else{.        i
2b63b 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74  nt n = sqlite3St
2b63c 72 6c 65 6e 33 30 28 61 72 67 76 5b 69 5d 29 2b  rlen30(argv[i])+
2b63d 31 3b 0a 20 20 20 20 20 20 20 20 7a 20 3d 20 73  1;.        z = s
2b63e 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 6e  qlite3_malloc( n
2b63f 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   );.        if( 
2b640 7a 3d 3d 30 20 29 20 67 6f 74 6f 20 6d 61 6c 6c  z==0 ) goto mall
2b641 6f 63 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 20  oc_failed;.     
2b642 20 20 20 6d 65 6d 63 70 79 28 7a 2c 20 61 72 67     memcpy(z, arg
2b643 76 5b 69 5d 2c 20 6e 29 3b 0a 20 20 20 20 20 20  v[i], n);.      
2b644 7d 0a 20 20 20 20 20 20 70 2d 3e 61 7a 52 65 73  }.      p->azRes
2b645 75 6c 74 5b 70 2d 3e 6e 44 61 74 61 2b 2b 5d 20  ult[p->nData++] 
2b646 3d 20 7a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  = z;.    }.    p
2b647 2d 3e 6e 52 6f 77 2b 2b 3b 0a 20 20 7d 0a 20 20  ->nRow++;.  }.  
2b648 72 65 74 75 72 6e 20 30 3b 0a 0a 6d 61 6c 6c 6f  return 0;..mallo
2b649 63 5f 66 61 69 6c 65 64 3a 0a 20 20 70 2d 3e 72  c_failed:.  p->r
2b64a 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
2b64b 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a  ;.  return 1;.}.
2b64c 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 74 68 65  ./*.** Query the
2b64d 20 64 61 74 61 62 61 73 65 2e 20 20 42 75 74 20   database.  But 
2b64e 69 6e 73 74 65 61 64 20 6f 66 20 69 6e 76 6f 6b  instead of invok
2b64f 69 6e 67 20 61 20 63 61 6c 6c 62 61 63 6b 20 66  ing a callback f
2b650 6f 72 20 65 61 63 68 20 72 6f 77 2c 0a 2a 2a 20  or each row,.** 
2b651 6d 61 6c 6c 6f 63 28 29 20 66 6f 72 20 73 70 61  malloc() for spa
2b652 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72  ce to hold the r
2b653 65 73 75 6c 74 20 61 6e 64 20 72 65 74 75 72 6e  esult and return
2b654 20 74 68 65 20 65 6e 74 69 72 65 20 72 65 73 75   the entire resu
2b655 6c 74 73 0a 2a 2a 20 61 74 20 74 68 65 20 63 6f  lts.** at the co
2b656 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 74 68 65 20  nclusion of the 
2b657 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  call..**.** The 
2b658 72 65 73 75 6c 74 20 74 68 61 74 20 69 73 20 77  result that is w
2b659 72 69 74 74 65 6e 20 74 6f 20 2a 2a 2a 70 61 7a  ritten to ***paz
2b65a 52 65 73 75 6c 74 20 69 73 20 68 65 6c 64 20 69  Result is held i
2b65b 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65  n memory obtaine
2b65c 64 0a 2a 2a 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63  d.** from malloc
2b65d 28 29 2e 20 20 42 75 74 20 74 68 65 20 63 61 6c  ().  But the cal
2b65e 6c 65 72 20 63 61 6e 6e 6f 74 20 66 72 65 65 20  ler cannot free 
2b65f 74 68 69 73 20 6d 65 6d 6f 72 79 20 64 69 72 65  this memory dire
2b660 63 74 6c 79 2e 20 20 0a 2a 2a 20 49 6e 73 74 65  ctly.  .** Inste
2b661 61 64 2c 20 74 68 65 20 65 6e 74 69 72 65 20 74  ad, the entire t
2b662 61 62 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 70  able should be p
2b663 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33  assed to sqlite3
2b664 5f 66 72 65 65 5f 74 61 62 6c 65 28 29 20 77 68  _free_table() wh
2b665 65 6e 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 69 6e  en.** the callin
2b666 67 20 70 72 6f 63 65 64 75 72 65 20 69 73 20 66  g procedure is f
2b667 69 6e 69 73 68 65 64 20 75 73 69 6e 67 20 69 74  inished using it
2b668 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
2b669 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 65 74 5f  int sqlite3_get_
2b66a 74 61 62 6c 65 28 0a 20 20 73 71 6c 69 74 65 33  table(.  sqlite3
2b66b 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
2b66c 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
2b66d 62 61 73 65 20 6f 6e 20 77 68 69 63 68 20 74 68  base on which th
2b66e 65 20 53 51 4c 20 65 78 65 63 75 74 65 73 20 2a  e SQL executes *
2b66f 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
2b670 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 20 20 20  zSql,           
2b671 2f 2a 20 54 68 65 20 53 51 4c 20 74 6f 20 62 65  /* The SQL to be
2b672 20 65 78 65 63 75 74 65 64 20 2a 2f 0a 20 20 63   executed */.  c
2b673 68 61 72 20 2a 2a 2a 70 61 7a 52 65 73 75 6c 74  har ***pazResult
2b674 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72  ,          /* Wr
2b675 69 74 65 20 74 68 65 20 72 65 73 75 6c 74 20 74  ite the result t
2b676 61 62 6c 65 20 68 65 72 65 20 2a 2f 0a 20 20 69  able here */.  i
2b677 6e 74 20 2a 70 6e 52 6f 77 2c 20 20 20 20 20 20  nt *pnRow,      
2b678 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72             /* Wr
2b679 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ite the number o
2b67a 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20 72 65  f rows in the re
2b67b 73 75 6c 74 20 68 65 72 65 20 2a 2f 0a 20 20 69  sult here */.  i
2b67c 6e 74 20 2a 70 6e 43 6f 6c 75 6d 6e 2c 20 20 20  nt *pnColumn,   
2b67d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72             /* Wr
2b67e 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ite the number o
2b67f 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 72 65 73  f columns of res
2b680 75 6c 74 20 68 65 72 65 20 2a 2f 0a 20 20 63 68  ult here */.  ch
2b681 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20  ar **pzErrMsg   
2b682 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
2b683 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  te error message
2b684 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69  s here */.){.  i
2b685 6e 74 20 72 63 3b 0a 20 20 54 61 62 52 65 73 75  nt rc;.  TabResu
2b686 6c 74 20 72 65 73 3b 0a 0a 20 20 2a 70 61 7a 52  lt res;..  *pazR
2b687 65 73 75 6c 74 20 3d 20 30 3b 0a 20 20 69 66 28  esult = 0;.  if(
2b688 20 70 6e 43 6f 6c 75 6d 6e 20 29 20 2a 70 6e 43   pnColumn ) *pnC
2b689 6f 6c 75 6d 6e 20 3d 20 30 3b 0a 20 20 69 66 28  olumn = 0;.  if(
2b68a 20 70 6e 52 6f 77 20 29 20 2a 70 6e 52 6f 77 20   pnRow ) *pnRow 
2b68b 3d 20 30 3b 0a 20 20 69 66 28 20 70 7a 45 72 72  = 0;.  if( pzErr
2b68c 4d 73 67 20 29 20 2a 70 7a 45 72 72 4d 73 67 20  Msg ) *pzErrMsg 
2b68d 3d 20 30 3b 0a 20 20 72 65 73 2e 7a 45 72 72 4d  = 0;.  res.zErrM
2b68e 73 67 20 3d 20 30 3b 0a 20 20 72 65 73 2e 6e 52  sg = 0;.  res.nR
2b68f 6f 77 20 3d 20 30 3b 0a 20 20 72 65 73 2e 6e 43  ow = 0;.  res.nC
2b690 6f 6c 75 6d 6e 20 3d 20 30 3b 0a 20 20 72 65 73  olumn = 0;.  res
2b691 2e 6e 44 61 74 61 20 3d 20 31 3b 0a 20 20 72 65  .nData = 1;.  re
2b692 73 2e 6e 41 6c 6c 6f 63 20 3d 20 32 30 3b 0a 20  s.nAlloc = 20;. 
2b693 20 72 65 73 2e 72 63 20 3d 20 53 51 4c 49 54 45   res.rc = SQLITE
2b694 5f 4f 4b 3b 0a 20 20 72 65 73 2e 61 7a 52 65 73  _OK;.  res.azRes
2b695 75 6c 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  ult = sqlite3_ma
2b696 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 63 68 61 72  lloc(sizeof(char
2b697 2a 29 2a 72 65 73 2e 6e 41 6c 6c 6f 63 20 29 3b  *)*res.nAlloc );
2b698 0a 20 20 69 66 28 20 72 65 73 2e 61 7a 52 65 73  .  if( res.azRes
2b699 75 6c 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 64  ult==0 ){.     d
2b69a 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 53 51 4c  b->errCode = SQL
2b69b 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
2b69c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
2b69d 4d 45 4d 3b 0a 20 20 7d 0a 20 20 72 65 73 2e 61  MEM;.  }.  res.a
2b69e 7a 52 65 73 75 6c 74 5b 30 5d 20 3d 20 30 3b 0a  zResult[0] = 0;.
2b69f 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65    rc = sqlite3_e
2b6a0 78 65 63 28 64 62 2c 20 7a 53 71 6c 2c 20 73 71  xec(db, zSql, sq
2b6a1 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 5f  lite3_get_table_
2b6a2 63 62 2c 20 26 72 65 73 2c 20 70 7a 45 72 72 4d  cb, &res, pzErrM
2b6a3 73 67 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  sg);.  assert( s
2b6a4 69 7a 65 6f 66 28 72 65 73 2e 61 7a 52 65 73 75  izeof(res.azResu
2b6a5 6c 74 5b 30 5d 29 3e 3d 20 73 69 7a 65 6f 66 28  lt[0])>= sizeof(
2b6a6 72 65 73 2e 6e 44 61 74 61 29 20 29 3b 0a 20 20  res.nData) );.  
2b6a7 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b 30 5d 20  res.azResult[0] 
2b6a8 3d 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f  = SQLITE_INT_TO_
2b6a9 50 54 52 28 72 65 73 2e 6e 44 61 74 61 29 3b 0a  PTR(res.nData);.
2b6aa 20 20 69 66 28 20 28 72 63 26 30 78 66 66 29 3d    if( (rc&0xff)=
2b6ab 3d 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 29 7b  =SQLITE_ABORT ){
2b6ac 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
2b6ad 65 5f 74 61 62 6c 65 28 26 72 65 73 2e 61 7a 52  e_table(&res.azR
2b6ae 65 73 75 6c 74 5b 31 5d 29 3b 0a 20 20 20 20 69  esult[1]);.    i
2b6af 66 28 20 72 65 73 2e 7a 45 72 72 4d 73 67 20 29  f( res.zErrMsg )
2b6b0 7b 0a 20 20 20 20 20 20 69 66 28 20 70 7a 45 72  {.      if( pzEr
2b6b1 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 20 20  rMsg ){.        
2b6b2 73 71 6c 69 74 65 33 5f 66 72 65 65 28 2a 70 7a  sqlite3_free(*pz
2b6b3 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 20  ErrMsg);.       
2b6b4 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c   *pzErrMsg = sql
2b6b5 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73  ite3_mprintf("%s
2b6b6 22 2c 72 65 73 2e 7a 45 72 72 4d 73 67 29 3b 0a  ",res.zErrMsg);.
2b6b7 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
2b6b8 6c 69 74 65 33 5f 66 72 65 65 28 72 65 73 2e 7a  lite3_free(res.z
2b6b9 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 7d 0a 20  ErrMsg);.    }. 
2b6ba 20 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d     db->errCode =
2b6bb 20 72 65 73 2e 72 63 3b 20 20 2f 2a 20 41 73 73   res.rc;  /* Ass
2b6bc 75 6d 65 20 33 32 2d 62 69 74 20 61 73 73 69 67  ume 32-bit assig
2b6bd 6e 6d 65 6e 74 20 69 73 20 61 74 6f 6d 69 63 20  nment is atomic 
2b6be 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 72 65  */.    return re
2b6bf 73 2e 72 63 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  s.rc;.  }.  sqli
2b6c0 74 65 33 5f 66 72 65 65 28 72 65 73 2e 7a 45 72  te3_free(res.zEr
2b6c1 72 4d 73 67 29 3b 0a 20 20 69 66 28 20 72 63 21  rMsg);.  if( rc!
2b6c2 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
2b6c3 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74    sqlite3_free_t
2b6c4 61 62 6c 65 28 26 72 65 73 2e 61 7a 52 65 73 75  able(&res.azResu
2b6c5 6c 74 5b 31 5d 29 3b 0a 20 20 20 20 72 65 74 75  lt[1]);.    retu
2b6c6 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28  rn rc;.  }.  if(
2b6c7 20 72 65 73 2e 6e 41 6c 6c 6f 63 3e 72 65 73 2e   res.nAlloc>res.
2b6c8 6e 44 61 74 61 20 29 7b 0a 20 20 20 20 63 68 61  nData ){.    cha
2b6c9 72 20 2a 2a 61 7a 4e 65 77 3b 0a 20 20 20 20 61  r **azNew;.    a
2b6ca 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72  zNew = sqlite3_r
2b6cb 65 61 6c 6c 6f 63 28 20 72 65 73 2e 61 7a 52 65  ealloc( res.azRe
2b6cc 73 75 6c 74 2c 20 73 69 7a 65 6f 66 28 63 68 61  sult, sizeof(cha
2b6cd 72 2a 29 2a 72 65 73 2e 6e 44 61 74 61 20 29 3b  r*)*res.nData );
2b6ce 0a 20 20 20 20 69 66 28 20 61 7a 4e 65 77 3d 3d  .    if( azNew==
2b6cf 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
2b6d0 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 26 72  e3_free_table(&r
2b6d1 65 73 2e 61 7a 52 65 73 75 6c 74 5b 31 5d 29 3b  es.azResult[1]);
2b6d2 0a 20 20 20 20 20 20 64 62 2d 3e 65 72 72 43 6f  .      db->errCo
2b6d3 64 65 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  de = SQLITE_NOME
2b6d4 4d 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  M;.      return 
2b6d5 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
2b6d6 20 20 7d 0a 20 20 20 20 72 65 73 2e 61 7a 52 65    }.    res.azRe
2b6d7 73 75 6c 74 20 3d 20 61 7a 4e 65 77 3b 0a 20 20  sult = azNew;.  
2b6d8 7d 0a 20 20 2a 70 61 7a 52 65 73 75 6c 74 20 3d  }.  *pazResult =
2b6d9 20 26 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b 31   &res.azResult[1
2b6da 5d 3b 0a 20 20 69 66 28 20 70 6e 43 6f 6c 75 6d  ];.  if( pnColum
2b6db 6e 20 29 20 2a 70 6e 43 6f 6c 75 6d 6e 20 3d 20  n ) *pnColumn = 
2b6dc 72 65 73 2e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 69  res.nColumn;.  i
2b6dd 66 28 20 70 6e 52 6f 77 20 29 20 2a 70 6e 52 6f  f( pnRow ) *pnRo
2b6de 77 20 3d 20 72 65 73 2e 6e 52 6f 77 3b 0a 20 20  w = res.nRow;.  
2b6df 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
2b6e0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
2b6e1 20 66 72 65 65 73 20 74 68 65 20 73 70 61 63 65   frees the space
2b6e2 20 74 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74   the sqlite3_get
2b6e3 5f 74 61 62 6c 65 28 29 20 6d 61 6c 6c 6f 63 65  _table() malloce
2b6e4 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  d..*/.SQLITE_API
2b6e5 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 66 72   void sqlite3_fr
2b6e6 65 65 5f 74 61 62 6c 65 28 0a 20 20 63 68 61 72  ee_table(.  char
2b6e7 20 2a 2a 61 7a 52 65 73 75 6c 74 20 20 20 20 20   **azResult     
2b6e8 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74         /* Result
2b6e9 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 66   returned from f
2b6ea 72 6f 6d 20 73 71 6c 69 74 65 33 5f 67 65 74 5f  rom sqlite3_get_
2b6eb 74 61 62 6c 65 28 29 20 2a 2f 0a 29 7b 0a 20 20  table() */.){.  
2b6ec 69 66 28 20 61 7a 52 65 73 75 6c 74 20 29 7b 0a  if( azResult ){.
2b6ed 20 20 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20      int i, n;.  
2b6ee 20 20 61 7a 52 65 73 75 6c 74 2d 2d 3b 0a 20 20    azResult--;.  
2b6ef 20 20 61 73 73 65 72 74 28 20 61 7a 52 65 73 75    assert( azResu
2b6f0 6c 74 21 3d 30 20 29 3b 0a 20 20 20 20 6e 20 3d  lt!=0 );.    n =
2b6f1 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49   SQLITE_PTR_TO_I
2b6f2 4e 54 28 61 7a 52 65 73 75 6c 74 5b 30 5d 29 3b  NT(azResult[0]);
2b6f3 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c  .    for(i=1; i<
2b6f4 6e 3b 20 69 2b 2b 29 7b 20 69 66 28 20 61 7a 52  n; i++){ if( azR
2b6f5 65 73 75 6c 74 5b 69 5d 20 29 20 73 71 6c 69 74  esult[i] ) sqlit
2b6f6 65 33 5f 66 72 65 65 28 61 7a 52 65 73 75 6c 74  e3_free(azResult
2b6f7 5b 69 5d 29 3b 20 7d 0a 20 20 20 20 73 71 6c 69  [i]); }.    sqli
2b6f8 74 65 33 5f 66 72 65 65 28 61 7a 52 65 73 75 6c  te3_free(azResul
2b6f9 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69  t);.  }.}..#endi
2b6fa 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
2b6fb 5f 47 45 54 5f 54 41 42 4c 45 20 2a 2f 0a 0a 2f  _GET_TABLE */../
2b6fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
2b6fd 6e 64 20 6f 66 20 74 61 62 6c 65 2e 63 20 2a 2a  nd of table.c **
2b6fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b6ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
2b701 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
2b702 65 67 69 6e 20 66 69 6c 65 20 74 72 69 67 67 65  egin file trigge
2b703 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  r.c ************
2b704 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b705 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
2b706 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  *.**.** The auth
2b707 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
2b708 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
2b709 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
2b70a 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
2b70b 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
2b70c 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
2b70d 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
2b70e 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
2b70f 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
2b710 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
2b711 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
2b712 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
2b713 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
2b714 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
2b715 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
2b716 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
2b717 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
2b718 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b719 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b71a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b71b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b71c 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 74  .**.**.** $Id: t
2b71d 72 69 67 67 65 72 2e 63 2c 76 20 31 2e 31 34 33  rigger.c,v 1.143
2b71e 20 32 30 30 39 2f 30 38 2f 31 30 20 30 33 3a 35   2009/08/10 03:5
2b71f 37 3a 35 38 20 73 68 61 6e 65 20 45 78 70 20 24  7:58 shane Exp $
2b720 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  .*/..#ifndef SQL
2b721 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
2b722 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 20  ./*.** Delete a 
2b723 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 54  linked list of T
2b724 72 69 67 67 65 72 53 74 65 70 20 73 74 72 75 63  riggerStep struc
2b725 74 75 72 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  tures..*/.SQLITE
2b726 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2b727 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67  lite3DeleteTrigg
2b728 65 72 53 74 65 70 28 73 71 6c 69 74 65 33 20 2a  erStep(sqlite3 *
2b729 64 62 2c 20 54 72 69 67 67 65 72 53 74 65 70 20  db, TriggerStep 
2b72a 2a 70 54 72 69 67 67 65 72 53 74 65 70 29 7b 0a  *pTriggerStep){.
2b72b 20 20 77 68 69 6c 65 28 20 70 54 72 69 67 67 65    while( pTrigge
2b72c 72 53 74 65 70 20 29 7b 0a 20 20 20 20 54 72 69  rStep ){.    Tri
2b72d 67 67 65 72 53 74 65 70 20 2a 20 70 54 6d 70 20  ggerStep * pTmp 
2b72e 3d 20 70 54 72 69 67 67 65 72 53 74 65 70 3b 0a  = pTriggerStep;.
2b72f 20 20 20 20 70 54 72 69 67 67 65 72 53 74 65 70      pTriggerStep
2b730 20 3d 20 70 54 72 69 67 67 65 72 53 74 65 70 2d   = pTriggerStep-
2b731 3e 70 4e 65 78 74 3b 0a 0a 20 20 20 20 73 71 6c  >pNext;..    sql
2b732 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
2b733 62 2c 20 70 54 6d 70 2d 3e 70 57 68 65 72 65 29  b, pTmp->pWhere)
2b734 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ;.    sqlite3Exp
2b735 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  rListDelete(db, 
2b736 70 54 6d 70 2d 3e 70 45 78 70 72 4c 69 73 74 29  pTmp->pExprList)
2b737 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c  ;.    sqlite3Sel
2b738 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 54  ectDelete(db, pT
2b739 6d 70 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20  mp->pSelect);.  
2b73a 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44    sqlite3IdListD
2b73b 65 6c 65 74 65 28 64 62 2c 20 70 54 6d 70 2d 3e  elete(db, pTmp->
2b73c 70 49 64 4c 69 73 74 29 3b 0a 0a 20 20 20 20 73  pIdList);..    s
2b73d 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2b73e 20 70 54 6d 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f   pTmp);.  }.}../
2b73f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 61 62 6c 65  *.** Given table
2b740 20 70 54 61 62 2c 20 72 65 74 75 72 6e 20 61 20   pTab, return a 
2b741 6c 69 73 74 20 6f 66 20 61 6c 6c 20 74 68 65 20  list of all the 
2b742 74 72 69 67 67 65 72 73 20 61 74 74 61 63 68 65  triggers attache
2b743 64 20 74 6f 20 0a 2a 2a 20 74 68 65 20 74 61 62  d to .** the tab
2b744 6c 65 2e 20 54 68 65 20 6c 69 73 74 20 69 73 20  le. The list is 
2b745 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 54 72 69  connected by Tri
2b746 67 67 65 72 2e 70 4e 65 78 74 20 70 6f 69 6e 74  gger.pNext point
2b747 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f  ers..**.** All o
2b748 66 20 74 68 65 20 74 72 69 67 67 65 72 73 20 6f  f the triggers o
2b749 6e 20 70 54 61 62 20 74 68 61 74 20 61 72 65 20  n pTab that are 
2b74a 69 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 61  in the same data
2b74b 62 61 73 65 20 61 73 20 70 54 61 62 0a 2a 2a 20  base as pTab.** 
2b74c 61 72 65 20 61 6c 72 65 61 64 79 20 61 74 74 61  are already atta
2b74d 63 68 65 64 20 74 6f 20 70 54 61 62 2d 3e 70 54  ched to pTab->pT
2b74e 72 69 67 67 65 72 2e 20 20 42 75 74 20 74 68 65  rigger.  But the
2b74f 72 65 20 6d 69 67 68 74 20 62 65 20 61 64 64 69  re might be addi
2b750 74 69 6f 6e 61 6c 0a 2a 2a 20 74 72 69 67 67 65  tional.** trigge
2b751 72 73 20 6f 6e 20 70 54 61 62 20 69 6e 20 74 68  rs on pTab in th
2b752 65 20 54 45 4d 50 20 73 63 68 65 6d 61 2e 20 20  e TEMP schema.  
2b753 54 68 69 73 20 72 6f 75 74 69 6e 65 20 70 72 65  This routine pre
2b754 70 65 6e 64 73 20 61 6c 6c 0a 2a 2a 20 54 45 4d  pends all.** TEM
2b755 50 20 74 72 69 67 67 65 72 73 20 6f 6e 20 70 54  P triggers on pT
2b756 61 62 20 74 6f 20 74 68 65 20 62 65 67 69 6e 6e  ab to the beginn
2b757 69 6e 67 20 6f 66 20 74 68 65 20 70 54 61 62 2d  ing of the pTab-
2b758 3e 70 54 72 69 67 67 65 72 20 6c 69 73 74 0a 2a  >pTrigger list.*
2b759 2a 20 61 6e 64 20 72 65 74 75 72 6e 73 20 74 68  * and returns th
2b75a 65 20 63 6f 6d 62 69 6e 65 64 20 6c 69 73 74 2e  e combined list.
2b75b 0a 2a 2a 0a 2a 2a 20 54 6f 20 73 74 61 74 65 20  .**.** To state 
2b75c 69 74 20 61 6e 6f 74 68 65 72 20 77 61 79 3a 20  it another way: 
2b75d 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   This routine re
2b75e 74 75 72 6e 73 20 61 20 6c 69 73 74 20 6f 66 20  turns a list of 
2b75f 61 6c 6c 20 74 72 69 67 67 65 72 73 0a 2a 2a 20  all triggers.** 
2b760 74 68 61 74 20 66 69 72 65 20 6f 66 66 20 6f 66  that fire off of
2b761 20 70 54 61 62 2e 20 20 54 68 65 20 6c 69 73 74   pTab.  The list
2b762 20 77 69 6c 6c 20 69 6e 63 6c 75 64 65 20 61 6e   will include an
2b763 79 20 54 45 4d 50 20 74 72 69 67 67 65 72 73 20  y TEMP triggers 
2b764 6f 6e 0a 2a 2a 20 70 54 61 62 20 61 73 20 77 65  on.** pTab as we
2b765 6c 6c 20 61 73 20 74 68 65 20 74 72 69 67 67 65  ll as the trigge
2b766 72 73 20 6c 69 73 65 64 20 69 6e 20 70 54 61 62  rs lised in pTab
2b767 2d 3e 70 54 72 69 67 67 65 72 2e 0a 2a 2f 0a 53  ->pTrigger..*/.S
2b768 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 72  QLITE_PRIVATE Tr
2b769 69 67 67 65 72 20 2a 73 71 6c 69 74 65 33 54 72  igger *sqlite3Tr
2b76a 69 67 67 65 72 4c 69 73 74 28 50 61 72 73 65 20  iggerList(Parse 
2b76b 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a  *pParse, Table *
2b76c 70 54 61 62 29 7b 0a 20 20 53 63 68 65 6d 61 20  pTab){.  Schema 
2b76d 2a 20 63 6f 6e 73 74 20 70 54 6d 70 53 63 68 65  * const pTmpSche
2b76e 6d 61 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d  ma = pParse->db-
2b76f 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 3b  >aDb[1].pSchema;
2b770 0a 20 20 54 72 69 67 67 65 72 20 2a 70 4c 69 73  .  Trigger *pLis
2b771 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  t = 0;          
2b772 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20          /* List 
2b773 6f 66 20 74 72 69 67 67 65 72 73 20 74 6f 20 72  of triggers to r
2b774 65 74 75 72 6e 20 2a 2f 0a 0a 20 20 69 66 28 20  eturn */..  if( 
2b775 70 54 6d 70 53 63 68 65 6d 61 21 3d 70 54 61 62  pTmpSchema!=pTab
2b776 2d 3e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20  ->pSchema ){.   
2b777 20 48 61 73 68 45 6c 65 6d 20 2a 70 3b 0a 20 20   HashElem *p;.  
2b778 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 48 61    for(p=sqliteHa
2b779 73 68 46 69 72 73 74 28 26 70 54 6d 70 53 63 68  shFirst(&pTmpSch
2b77a 65 6d 61 2d 3e 74 72 69 67 48 61 73 68 29 3b 20  ema->trigHash); 
2b77b 70 3b 20 70 3d 73 71 6c 69 74 65 48 61 73 68 4e  p; p=sqliteHashN
2b77c 65 78 74 28 70 29 29 7b 0a 20 20 20 20 20 20 54  ext(p)){.      T
2b77d 72 69 67 67 65 72 20 2a 70 54 72 69 67 20 3d 20  rigger *pTrig = 
2b77e 28 54 72 69 67 67 65 72 20 2a 29 73 71 6c 69 74  (Trigger *)sqlit
2b77f 65 48 61 73 68 44 61 74 61 28 70 29 3b 0a 20 20  eHashData(p);.  
2b780 20 20 20 20 69 66 28 20 70 54 72 69 67 2d 3e 70      if( pTrig->p
2b781 54 61 62 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d  TabSchema==pTab-
2b782 3e 70 53 63 68 65 6d 61 0a 20 20 20 20 20 20 20  >pSchema.       
2b783 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72  && 0==sqlite3Str
2b784 49 43 6d 70 28 70 54 72 69 67 2d 3e 74 61 62 6c  ICmp(pTrig->tabl
2b785 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 20  e, pTab->zName) 
2b786 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20  .      ){.      
2b787 20 20 70 54 72 69 67 2d 3e 70 4e 65 78 74 20 3d    pTrig->pNext =
2b788 20 28 70 4c 69 73 74 20 3f 20 70 4c 69 73 74 20   (pList ? pList 
2b789 3a 20 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72  : pTab->pTrigger
2b78a 29 3b 0a 20 20 20 20 20 20 20 20 70 4c 69 73 74  );.        pList
2b78b 20 3d 20 70 54 72 69 67 3b 0a 20 20 20 20 20 20   = pTrig;.      
2b78c 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72  }.    }.  }..  r
2b78d 65 74 75 72 6e 20 28 70 4c 69 73 74 20 3f 20 70  eturn (pList ? p
2b78e 4c 69 73 74 20 3a 20 70 54 61 62 2d 3e 70 54 72  List : pTab->pTr
2b78f 69 67 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  igger);.}../*.**
2b790 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20   This is called 
2b791 62 79 20 74 68 65 20 70 61 72 73 65 72 20 77 68  by the parser wh
2b792 65 6e 20 69 74 20 73 65 65 73 20 61 20 43 52 45  en it sees a CRE
2b793 41 54 45 20 54 52 49 47 47 45 52 20 73 74 61 74  ATE TRIGGER stat
2b794 65 6d 65 6e 74 0a 2a 2a 20 75 70 20 74 6f 20 74  ement.** up to t
2b795 68 65 20 70 6f 69 6e 74 20 6f 66 20 74 68 65 20  he point of the 
2b796 42 45 47 49 4e 20 62 65 66 6f 72 65 20 74 68 65  BEGIN before the
2b797 20 74 72 69 67 67 65 72 20 61 63 74 69 6f 6e 73   trigger actions
2b798 2e 20 20 41 20 54 72 69 67 67 65 72 0a 2a 2a 20  .  A Trigger.** 
2b799 73 74 72 75 63 74 75 72 65 20 69 73 20 67 65 6e  structure is gen
2b79a 65 72 61 74 65 64 20 62 61 73 65 64 20 6f 6e 20  erated based on 
2b79b 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  the information 
2b79c 61 76 61 69 6c 61 62 6c 65 20 61 6e 64 20 73 74  available and st
2b79d 6f 72 65 64 0a 2a 2a 20 69 6e 20 70 50 61 72 73  ored.** in pPars
2b79e 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 2e 20  e->pNewTrigger. 
2b79f 20 41 66 74 65 72 20 74 68 65 20 74 72 69 67 67   After the trigg
2b7a0 65 72 20 61 63 74 69 6f 6e 73 20 68 61 76 65 20  er actions have 
2b7a1 62 65 65 6e 20 70 61 72 73 65 64 2c 20 74 68 65  been parsed, the
2b7a2 0a 2a 2a 20 73 71 6c 69 74 65 33 46 69 6e 69 73  .** sqlite3Finis
2b7a3 68 54 72 69 67 67 65 72 28 29 20 66 75 6e 63 74  hTrigger() funct
2b7a4 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f  ion is called to
2b7a5 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 74 72   complete the tr
2b7a6 69 67 67 65 72 0a 2a 2a 20 63 6f 6e 73 74 72 75  igger.** constru
2b7a7 63 74 69 6f 6e 20 70 72 6f 63 65 73 73 2e 0a 2a  ction process..*
2b7a8 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2b7a9 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67   void sqlite3Beg
2b7aa 69 6e 54 72 69 67 67 65 72 28 0a 20 20 50 61 72  inTrigger(.  Par
2b7ab 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
2b7ac 20 2f 2a 20 54 68 65 20 70 61 72 73 65 20 63 6f   /* The parse co
2b7ad 6e 74 65 78 74 20 6f 66 20 74 68 65 20 43 52 45  ntext of the CRE
2b7ae 41 54 45 20 54 52 49 47 47 45 52 20 73 74 61 74  ATE TRIGGER stat
2b7af 65 6d 65 6e 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e  ement */.  Token
2b7b0 20 2a 70 4e 61 6d 65 31 2c 20 20 20 20 20 20 2f   *pName1,      /
2b7b1 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  * The name of th
2b7b2 65 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 54  e trigger */.  T
2b7b3 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20  oken *pName2,   
2b7b4 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 6f     /* The name o
2b7b5 66 20 74 68 65 20 74 72 69 67 67 65 72 20 2a 2f  f the trigger */
2b7b6 0a 20 20 69 6e 74 20 74 72 5f 74 6d 2c 20 20 20  .  int tr_tm,   
2b7b7 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66         /* One of
2b7b8 20 54 4b 5f 42 45 46 4f 52 45 2c 20 54 4b 5f 41   TK_BEFORE, TK_A
2b7b9 46 54 45 52 2c 20 54 4b 5f 49 4e 53 54 45 41 44  FTER, TK_INSTEAD
2b7ba 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20   */.  int op,   
2b7bb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
2b7bc 20 6f 66 20 54 4b 5f 49 4e 53 45 52 54 2c 20 54   of TK_INSERT, T
2b7bd 4b 5f 55 50 44 41 54 45 2c 20 54 4b 5f 44 45 4c  K_UPDATE, TK_DEL
2b7be 45 54 45 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20  ETE */.  IdList 
2b7bf 2a 70 43 6f 6c 75 6d 6e 73 2c 20 20 20 2f 2a 20  *pColumns,   /* 
2b7c0 63 6f 6c 75 6d 6e 20 6c 69 73 74 20 69 66 20 74  column list if t
2b7c1 68 69 73 20 69 73 20 61 6e 20 55 50 44 41 54 45  his is an UPDATE
2b7c2 20 4f 46 20 74 72 69 67 67 65 72 20 2a 2f 0a 20   OF trigger */. 
2b7c3 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 6c 65   SrcList *pTable
2b7c4 4e 61 6d 65 2c 2f 2a 20 54 68 65 20 6e 61 6d 65  Name,/* The name
2b7c5 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2f 76 69   of the table/vi
2b7c6 65 77 20 74 68 65 20 74 72 69 67 67 65 72 20 61  ew the trigger a
2b7c7 70 70 6c 69 65 73 20 74 6f 20 2a 2f 0a 20 20 45  pplies to */.  E
2b7c8 78 70 72 20 2a 70 57 68 65 6e 2c 20 20 20 20 20  xpr *pWhen,     
2b7c9 20 20 20 2f 2a 20 57 48 45 4e 20 63 6c 61 75 73     /* WHEN claus
2b7ca 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 54 65 6d  e */.  int isTem
2b7cb 70 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  p,         /* Tr
2b7cc 75 65 20 69 66 20 74 68 65 20 54 45 4d 50 4f 52  ue if the TEMPOR
2b7cd 41 52 59 20 6b 65 79 77 6f 72 64 20 69 73 20 70  ARY keyword is p
2b7ce 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20  resent */.  int 
2b7cf 6e 6f 45 72 72 20 20 20 20 20 20 20 20 20 20 20  noErr           
2b7d0 2f 2a 20 53 75 70 70 72 65 73 73 20 65 72 72 6f  /* Suppress erro
2b7d1 72 73 20 69 66 20 74 68 65 20 74 72 69 67 67 65  rs if the trigge
2b7d2 72 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73  r already exists
2b7d3 20 2a 2f 0a 29 7b 0a 20 20 54 72 69 67 67 65 72   */.){.  Trigger
2b7d4 20 2a 70 54 72 69 67 67 65 72 20 3d 20 30 3b 20   *pTrigger = 0; 
2b7d5 20 2f 2a 20 54 68 65 20 6e 65 77 20 74 72 69 67   /* The new trig
2b7d6 67 65 72 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ger */.  Table *
2b7d7 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20  pTab;           
2b7d8 20 2f 2a 20 54 61 62 6c 65 20 74 68 61 74 20 74   /* Table that t
2b7d9 68 65 20 74 72 69 67 67 65 72 20 66 69 72 65 73  he trigger fires
2b7da 20 6f 66 66 20 6f 66 20 2a 2f 0a 20 20 63 68 61   off of */.  cha
2b7db 72 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 20 20 20  r *zName = 0;   
2b7dc 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
2b7dd 74 68 65 20 74 72 69 67 67 65 72 20 2a 2f 0a 20  the trigger */. 
2b7de 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
2b7df 50 61 72 73 65 2d 3e 64 62 3b 20 20 2f 2a 20 54  Parse->db;  /* T
2b7e0 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
2b7e1 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  ection */.  int 
2b7e2 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  iDb;            
2b7e3 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
2b7e4 61 73 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65  ase to store the
2b7e5 20 74 72 69 67 67 65 72 20 69 6e 20 2a 2f 0a 20   trigger in */. 
2b7e6 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 3b 20 20   Token *pName;  
2b7e7 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2b7e8 75 6e 71 75 61 6c 69 66 69 65 64 20 64 62 20 6e  unqualified db n
2b7e9 61 6d 65 20 2a 2f 0a 20 20 44 62 46 69 78 65 72  ame */.  DbFixer
2b7ea 20 73 46 69 78 3b 20 20 20 20 20 20 20 20 20 20   sFix;          
2b7eb 20 2f 2a 20 53 74 61 74 65 20 76 65 63 74 6f 72   /* State vector
2b7ec 20 66 6f 72 20 74 68 65 20 44 42 20 66 69 78 65   for the DB fixe
2b7ed 72 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 44  r */.  int iTabD
2b7ee 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  b;             /
2b7ef 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 64  * Index of the d
2b7f0 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20  atabase holding 
2b7f1 70 54 61 62 20 2a 2f 0a 0a 20 20 61 73 73 65 72  pTab */..  asser
2b7f2 74 28 20 70 4e 61 6d 65 31 21 3d 30 20 29 3b 20  t( pName1!=0 ); 
2b7f3 20 20 2f 2a 20 70 4e 61 6d 65 31 2d 3e 7a 20 6d    /* pName1->z m
2b7f4 69 67 68 74 20 62 65 20 4e 55 4c 4c 2c 20 62 75  ight be NULL, bu
2b7f5 74 20 6e 6f 74 20 70 4e 61 6d 65 31 20 69 74 73  t not pName1 its
2b7f6 65 6c 66 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  elf */.  assert(
2b7f7 20 70 4e 61 6d 65 32 21 3d 30 20 29 3b 0a 20 20   pName2!=0 );.  
2b7f8 61 73 73 65 72 74 28 20 6f 70 3d 3d 54 4b 5f 49  assert( op==TK_I
2b7f9 4e 53 45 52 54 20 7c 7c 20 6f 70 3d 3d 54 4b 5f  NSERT || op==TK_
2b7fa 55 50 44 41 54 45 20 7c 7c 20 6f 70 3d 3d 54 4b  UPDATE || op==TK
2b7fb 5f 44 45 4c 45 54 45 20 29 3b 0a 20 20 61 73 73  _DELETE );.  ass
2b7fc 65 72 74 28 20 6f 70 3e 30 20 26 26 20 6f 70 3c  ert( op>0 && op<
2b7fd 30 78 66 66 20 29 3b 0a 20 20 69 66 28 20 69 73  0xff );.  if( is
2b7fe 54 65 6d 70 20 29 7b 0a 20 20 20 20 2f 2a 20 49  Temp ){.    /* I
2b7ff 66 20 54 45 4d 50 20 77 61 73 20 73 70 65 63 69  f TEMP was speci
2b800 66 69 65 64 2c 20 74 68 65 6e 20 74 68 65 20 74  fied, then the t
2b801 72 69 67 67 65 72 20 6e 61 6d 65 20 6d 61 79 20  rigger name may 
2b802 6e 6f 74 20 62 65 20 71 75 61 6c 69 66 69 65 64  not be qualified
2b803 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4e 61  . */.    if( pNa
2b804 6d 65 32 2d 3e 6e 3e 30 20 29 7b 0a 20 20 20 20  me2->n>0 ){.    
2b805 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
2b806 67 28 70 50 61 72 73 65 2c 20 22 74 65 6d 70 6f  g(pParse, "tempo
2b807 72 61 72 79 20 74 72 69 67 67 65 72 20 6d 61 79  rary trigger may
2b808 20 6e 6f 74 20 68 61 76 65 20 71 75 61 6c 69 66   not have qualif
2b809 69 65 64 20 6e 61 6d 65 22 29 3b 0a 20 20 20 20  ied name");.    
2b80a 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63    goto trigger_c
2b80b 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20  leanup;.    }.  
2b80c 20 20 69 44 62 20 3d 20 31 3b 0a 20 20 20 20 70    iDb = 1;.    p
2b80d 4e 61 6d 65 20 3d 20 70 4e 61 6d 65 31 3b 0a 20  Name = pName1;. 
2b80e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 46   }else{.    /* F
2b80f 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 64 62  igure out the db
2b810 20 74 68 61 74 20 74 68 65 20 74 68 65 20 74 72   that the the tr
2b811 69 67 67 65 72 20 77 69 6c 6c 20 62 65 20 63 72  igger will be cr
2b812 65 61 74 65 64 20 69 6e 20 2a 2f 0a 20 20 20 20  eated in */.    
2b813 69 44 62 20 3d 20 73 71 6c 69 74 65 33 54 77 6f  iDb = sqlite3Two
2b814 50 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c  PartName(pParse,
2b815 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c   pName1, pName2,
2b816 20 26 70 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66   &pName);.    if
2b817 28 20 69 44 62 3c 30 20 29 7b 0a 20 20 20 20 20  ( iDb<0 ){.     
2b818 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c   goto trigger_cl
2b819 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 7d  eanup;.    }.  }
2b81a 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 74 72  ..  /* If the tr
2b81b 69 67 67 65 72 20 6e 61 6d 65 20 77 61 73 20 75  igger name was u
2b81c 6e 71 75 61 6c 69 66 69 65 64 2c 20 61 6e 64 20  nqualified, and 
2b81d 74 68 65 20 74 61 62 6c 65 20 69 73 20 61 20 74  the table is a t
2b81e 65 6d 70 20 74 61 62 6c 65 2c 0a 20 20 2a 2a 20  emp table,.  ** 
2b81f 74 68 65 6e 20 73 65 74 20 69 44 62 20 74 6f 20  then set iDb to 
2b820 31 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20  1 to create the 
2b821 74 72 69 67 67 65 72 20 69 6e 20 74 68 65 20 74  trigger in the t
2b822 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73  emporary databas
2b823 65 2e 0a 20 20 2a 2a 20 49 66 20 73 71 6c 69 74  e..  ** If sqlit
2b824 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28  e3SrcListLookup(
2b825 29 20 72 65 74 75 72 6e 73 20 30 2c 20 69 6e 64  ) returns 0, ind
2b826 69 63 61 74 69 6e 67 20 74 68 65 20 74 61 62 6c  icating the tabl
2b827 65 20 64 6f 65 73 20 6e 6f 74 0a 20 20 2a 2a 20  e does not.  ** 
2b828 65 78 69 73 74 2c 20 74 68 65 20 65 72 72 6f 72  exist, the error
2b829 20 69 73 20 63 61 75 67 68 74 20 62 79 20 74 68   is caught by th
2b82a 65 20 62 6c 6f 63 6b 20 62 65 6c 6f 77 2e 0a 20  e block below.. 
2b82b 20 2a 2f 0a 20 20 69 66 28 20 21 70 54 61 62 6c   */.  if( !pTabl
2b82c 65 4e 61 6d 65 20 7c 7c 20 64 62 2d 3e 6d 61 6c  eName || db->mal
2b82d 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
2b82e 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c   goto trigger_cl
2b82f 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 70 54 61  eanup;.  }.  pTa
2b830 62 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69  b = sqlite3SrcLi
2b831 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c  stLookup(pParse,
2b832 20 70 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20 20   pTableName);.  
2b833 69 66 28 20 70 4e 61 6d 65 32 2d 3e 6e 3d 3d 30  if( pName2->n==0
2b834 20 26 26 20 70 54 61 62 20 26 26 20 70 54 61 62   && pTab && pTab
2b835 2d 3e 70 53 63 68 65 6d 61 3d 3d 64 62 2d 3e 61  ->pSchema==db->a
2b836 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29 7b  Db[1].pSchema ){
2b837 0a 20 20 20 20 69 44 62 20 3d 20 31 3b 0a 20 20  .    iDb = 1;.  
2b838 7d 0a 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20 74  }..  /* Ensure t
2b839 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 6d 61  he table name ma
2b83a 74 63 68 65 73 20 64 61 74 61 62 61 73 65 20 6e  tches database n
2b83b 61 6d 65 20 61 6e 64 20 74 68 61 74 20 74 68 65  ame and that the
2b83c 20 74 61 62 6c 65 20 65 78 69 73 74 73 20 2a 2f   table exists */
2b83d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
2b83e 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 74  cFailed ) goto t
2b83f 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a  rigger_cleanup;.
2b840 20 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65    assert( pTable
2b841 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b  Name->nSrc==1 );
2b842 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69  .  if( sqlite3Fi
2b843 78 49 6e 69 74 28 26 73 46 69 78 2c 20 70 50 61  xInit(&sFix, pPa
2b844 72 73 65 2c 20 69 44 62 2c 20 22 74 72 69 67 67  rse, iDb, "trigg
2b845 65 72 22 2c 20 70 4e 61 6d 65 29 20 26 26 20 0a  er", pName) && .
2b846 20 20 20 20 20 20 73 71 6c 69 74 65 33 46 69 78        sqlite3Fix
2b847 53 72 63 4c 69 73 74 28 26 73 46 69 78 2c 20 70  SrcList(&sFix, p
2b848 54 61 62 6c 65 4e 61 6d 65 29 20 29 7b 0a 20 20  TableName) ){.  
2b849 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63    goto trigger_c
2b84a 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 70 54  leanup;.  }.  pT
2b84b 61 62 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c  ab = sqlite3SrcL
2b84c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65  istLookup(pParse
2b84d 2c 20 70 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20  , pTableName);. 
2b84e 20 69 66 28 20 21 70 54 61 62 20 29 7b 0a 20 20   if( !pTab ){.  
2b84f 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 64    /* The table d
2b850 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 20 2a  oes not exist. *
2b851 2f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 69 6e  /.    if( db->in
2b852 69 74 2e 69 44 62 3d 3d 31 20 29 7b 0a 20 20 20  it.iDb==1 ){.   
2b853 20 20 20 2f 2a 20 54 69 63 6b 65 74 20 23 33 38     /* Ticket #38
2b854 31 30 2e 0a 20 20 20 20 20 20 2a 2a 20 4e 6f 72  10..      ** Nor
2b855 6d 61 6c 6c 79 2c 20 77 68 65 6e 65 76 65 72 20  mally, whenever 
2b856 61 20 74 61 62 6c 65 20 69 73 20 64 72 6f 70 70  a table is dropp
2b857 65 64 2c 20 61 6c 6c 20 61 73 73 6f 63 69 61 74  ed, all associat
2b858 65 64 20 74 72 69 67 67 65 72 73 20 61 72 65 0a  ed triggers are.
2b859 20 20 20 20 20 20 2a 2a 20 64 72 6f 70 70 65 64        ** dropped
2b85a 20 74 6f 6f 2e 20 20 42 75 74 20 69 66 20 61 20   too.  But if a 
2b85b 54 45 4d 50 20 74 72 69 67 67 65 72 20 69 73 20  TEMP trigger is 
2b85c 63 72 65 61 74 65 64 20 6f 6e 20 61 20 6e 6f 6e  created on a non
2b85d 2d 54 45 4d 50 20 74 61 62 6c 65 0a 20 20 20 20  -TEMP table.    
2b85e 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 74 61 62    ** and the tab
2b85f 6c 65 20 69 73 20 64 72 6f 70 70 65 64 20 62 79  le is dropped by
2b860 20 61 20 64 69 66 66 65 72 65 6e 74 20 64 61 74   a different dat
2b861 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2b862 2c 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 74  , the.      ** t
2b863 72 69 67 67 65 72 20 69 73 20 6e 6f 74 20 76 69  rigger is not vi
2b864 73 69 62 6c 65 20 74 6f 20 74 68 65 20 64 61 74  sible to the dat
2b865 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2b866 20 74 68 61 74 20 64 6f 65 73 20 74 68 65 0a 20   that does the. 
2b867 20 20 20 20 20 2a 2a 20 64 72 6f 70 20 73 6f 20       ** drop so 
2b868 74 68 65 20 74 72 69 67 67 65 72 20 63 61 6e 6e  the trigger cann
2b869 6f 74 20 62 65 20 64 72 6f 70 70 65 64 2e 20 20  ot be dropped.  
2b86a 54 68 69 73 20 72 65 73 75 6c 74 73 20 69 6e 20  This results in 
2b86b 61 6e 0a 20 20 20 20 20 20 2a 2a 20 22 6f 72 70  an.      ** "orp
2b86c 68 61 6e 65 64 20 74 72 69 67 67 65 72 22 20 2d  haned trigger" -
2b86d 20 61 20 74 72 69 67 67 65 72 20 77 68 6f 73 65   a trigger whose
2b86e 20 61 73 73 6f 63 69 61 74 65 64 20 74 61 62 6c   associated tabl
2b86f 65 20 69 73 20 6d 69 73 73 69 6e 67 2e 0a 20 20  e is missing..  
2b870 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 62 2d      */.      db-
2b871 3e 69 6e 69 74 2e 6f 72 70 68 61 6e 54 72 69 67  >init.orphanTrig
2b872 67 65 72 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  ger = 1;.    }. 
2b873 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f     goto trigger_
2b874 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69  cleanup;.  }.  i
2b875 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  f( IsVirtual(pTa
2b876 62 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  b) ){.    sqlite
2b877 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2b878 2c 20 22 63 61 6e 6e 6f 74 20 63 72 65 61 74 65  , "cannot create
2b879 20 74 72 69 67 67 65 72 73 20 6f 6e 20 76 69 72   triggers on vir
2b87a 74 75 61 6c 20 74 61 62 6c 65 73 22 29 3b 0a 20  tual tables");. 
2b87b 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f     goto trigger_
2b87c 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20  cleanup;.  }..  
2b87d 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68  /* Check that th
2b87e 65 20 74 72 69 67 67 65 72 20 6e 61 6d 65 20 69  e trigger name i
2b87f 73 20 6e 6f 74 20 72 65 73 65 72 76 65 64 20 61  s not reserved a
2b880 6e 64 20 74 68 61 74 20 6e 6f 20 74 72 69 67 67  nd that no trigg
2b881 65 72 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 73  er of the.  ** s
2b882 70 65 63 69 66 69 65 64 20 6e 61 6d 65 20 65 78  pecified name ex
2b883 69 73 74 73 20 2a 2f 0a 20 20 7a 4e 61 6d 65 20  ists */.  zName 
2b884 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f  = sqlite3NameFro
2b885 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65  mToken(db, pName
2b886 29 3b 0a 20 20 69 66 28 20 21 7a 4e 61 6d 65 20  );.  if( !zName 
2b887 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71  || SQLITE_OK!=sq
2b888 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 74  lite3CheckObject
2b889 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 7a 4e 61  Name(pParse, zNa
2b88a 6d 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  me) ){.    goto 
2b88b 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b  trigger_cleanup;
2b88c 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74  .  }.  if( sqlit
2b88d 65 33 48 61 73 68 46 69 6e 64 28 26 28 64 62 2d  e3HashFind(&(db-
2b88e 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d  >aDb[iDb].pSchem
2b88f 61 2d 3e 74 72 69 67 48 61 73 68 29 2c 0a 20 20  a->trigHash),.  
2b890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b891 20 20 20 20 7a 4e 61 6d 65 2c 20 73 71 6c 69 74      zName, sqlit
2b892 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65  e3Strlen30(zName
2b893 29 29 20 29 7b 0a 20 20 20 20 69 66 28 20 21 6e  )) ){.    if( !n
2b894 6f 45 72 72 20 29 7b 0a 20 20 20 20 20 20 73 71  oErr ){.      sq
2b895 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2b896 61 72 73 65 2c 20 22 74 72 69 67 67 65 72 20 25  arse, "trigger %
2b897 54 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73  T already exists
2b898 22 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 20 20 7d  ", pName);.    }
2b899 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65  .    goto trigge
2b89a 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a  r_cleanup;.  }..
2b89b 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 63 72 65 61    /* Do not crea
2b89c 74 65 20 61 20 74 72 69 67 67 65 72 20 6f 6e 20  te a trigger on 
2b89d 61 20 73 79 73 74 65 6d 20 74 61 62 6c 65 20 2a  a system table *
2b89e 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  /.  if( sqlite3S
2b89f 74 72 4e 49 43 6d 70 28 70 54 61 62 2d 3e 7a 4e  trNICmp(pTab->zN
2b8a0 61 6d 65 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20  ame, "sqlite_", 
2b8a1 37 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  7)==0 ){.    sql
2b8a2 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2b8a3 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20 63 72 65  rse, "cannot cre
2b8a4 61 74 65 20 74 72 69 67 67 65 72 20 6f 6e 20 73  ate trigger on s
2b8a5 79 73 74 65 6d 20 74 61 62 6c 65 22 29 3b 0a 20  ystem table");. 
2b8a6 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b     pParse->nErr+
2b8a7 2b 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67  +;.    goto trig
2b8a8 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  ger_cleanup;.  }
2b8a9 0a 0a 20 20 2f 2a 20 49 4e 53 54 45 41 44 20 6f  ..  /* INSTEAD o
2b8aa 66 20 74 72 69 67 67 65 72 73 20 61 72 65 20 6f  f triggers are o
2b8ab 6e 6c 79 20 66 6f 72 20 76 69 65 77 73 20 61 6e  nly for views an
2b8ac 64 20 76 69 65 77 73 20 6f 6e 6c 79 20 73 75 70  d views only sup
2b8ad 70 6f 72 74 20 49 4e 53 54 45 41 44 0a 20 20 2a  port INSTEAD.  *
2b8ae 2a 20 6f 66 20 74 72 69 67 67 65 72 73 2e 0a 20  * of triggers.. 
2b8af 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 2d 3e   */.  if( pTab->
2b8b0 70 53 65 6c 65 63 74 20 26 26 20 74 72 5f 74 6d  pSelect && tr_tm
2b8b1 21 3d 54 4b 5f 49 4e 53 54 45 41 44 20 29 7b 0a  !=TK_INSTEAD ){.
2b8b2 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2b8b3 4d 73 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e  Msg(pParse, "can
2b8b4 6e 6f 74 20 63 72 65 61 74 65 20 25 73 20 74 72  not create %s tr
2b8b5 69 67 67 65 72 20 6f 6e 20 76 69 65 77 3a 20 25  igger on view: %
2b8b6 53 22 2c 20 0a 20 20 20 20 20 20 20 20 28 74 72  S", .        (tr
2b8b7 5f 74 6d 20 3d 3d 20 54 4b 5f 42 45 46 4f 52 45  _tm == TK_BEFORE
2b8b8 29 3f 22 42 45 46 4f 52 45 22 3a 22 41 46 54 45  )?"BEFORE":"AFTE
2b8b9 52 22 2c 20 70 54 61 62 6c 65 4e 61 6d 65 2c 20  R", pTableName, 
2b8ba 30 29 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72 69  0);.    goto tri
2b8bb 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  gger_cleanup;.  
2b8bc 7d 0a 20 20 69 66 28 20 21 70 54 61 62 2d 3e 70  }.  if( !pTab->p
2b8bd 53 65 6c 65 63 74 20 26 26 20 74 72 5f 74 6d 3d  Select && tr_tm=
2b8be 3d 54 4b 5f 49 4e 53 54 45 41 44 20 29 7b 0a 20  =TK_INSTEAD ){. 
2b8bf 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2b8c0 73 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e  sg(pParse, "cann
2b8c1 6f 74 20 63 72 65 61 74 65 20 49 4e 53 54 45 41  ot create INSTEA
2b8c2 44 20 4f 46 22 0a 20 20 20 20 20 20 20 20 22 20  D OF".        " 
2b8c3 74 72 69 67 67 65 72 20 6f 6e 20 74 61 62 6c 65  trigger on table
2b8c4 3a 20 25 53 22 2c 20 70 54 61 62 6c 65 4e 61 6d  : %S", pTableNam
2b8c5 65 2c 20 30 29 3b 0a 20 20 20 20 67 6f 74 6f 20  e, 0);.    goto 
2b8c6 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b  trigger_cleanup;
2b8c7 0a 20 20 7d 0a 20 20 69 54 61 62 44 62 20 3d 20  .  }.  iTabDb = 
2b8c8 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
2b8c9 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70  ndex(db, pTab->p
2b8ca 53 63 68 65 6d 61 29 3b 0a 0a 23 69 66 6e 64 65  Schema);..#ifnde
2b8cb 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
2b8cc 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a  THORIZATION.  {.
2b8cd 20 20 20 20 69 6e 74 20 63 6f 64 65 20 3d 20 53      int code = S
2b8ce 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 52 49  QLITE_CREATE_TRI
2b8cf 47 47 45 52 3b 0a 20 20 20 20 63 6f 6e 73 74 20  GGER;.    const 
2b8d0 63 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e  char *zDb = db->
2b8d1 61 44 62 5b 69 54 61 62 44 62 5d 2e 7a 4e 61 6d  aDb[iTabDb].zNam
2b8d2 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  e;.    const cha
2b8d3 72 20 2a 7a 44 62 54 72 69 67 20 3d 20 69 73 54  r *zDbTrig = isT
2b8d4 65 6d 70 20 3f 20 64 62 2d 3e 61 44 62 5b 31 5d  emp ? db->aDb[1]
2b8d5 2e 7a 4e 61 6d 65 20 3a 20 7a 44 62 3b 0a 20 20  .zName : zDb;.  
2b8d6 20 20 69 66 28 20 69 54 61 62 44 62 3d 3d 31 20    if( iTabDb==1 
2b8d7 7c 7c 20 69 73 54 65 6d 70 20 29 20 63 6f 64 65  || isTemp ) code
2b8d8 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45   = SQLITE_CREATE
2b8d9 5f 54 45 4d 50 5f 54 52 49 47 47 45 52 3b 0a 20  _TEMP_TRIGGER;. 
2b8da 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75     if( sqlite3Au
2b8db 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
2b8dc 63 6f 64 65 2c 20 7a 4e 61 6d 65 2c 20 70 54 61  code, zName, pTa
2b8dd 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 44 62 54 72 69  b->zName, zDbTri
2b8de 67 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  g) ){.      goto
2b8df 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70   trigger_cleanup
2b8e0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
2b8e1 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
2b8e2 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f  (pParse, SQLITE_
2b8e3 49 4e 53 45 52 54 2c 20 53 43 48 45 4d 41 5f 54  INSERT, SCHEMA_T
2b8e4 41 42 4c 45 28 69 54 61 62 44 62 29 2c 30 2c 7a  ABLE(iTabDb),0,z
2b8e5 44 62 29 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  Db)){.      goto
2b8e6 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70   trigger_cleanup
2b8e7 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
2b8e8 69 66 0a 0a 20 20 2f 2a 20 49 4e 53 54 45 41 44  if..  /* INSTEAD
2b8e9 20 4f 46 20 74 72 69 67 67 65 72 73 20 63 61 6e   OF triggers can
2b8ea 20 6f 6e 6c 79 20 61 70 70 65 61 72 20 6f 6e 20   only appear on 
2b8eb 76 69 65 77 73 20 61 6e 64 20 42 45 46 4f 52 45  views and BEFORE
2b8ec 20 74 72 69 67 67 65 72 73 0a 20 20 2a 2a 20 63   triggers.  ** c
2b8ed 61 6e 6e 6f 74 20 61 70 70 65 61 72 20 6f 6e 20  annot appear on 
2b8ee 76 69 65 77 73 2e 20 20 53 6f 20 77 65 20 6d 69  views.  So we mi
2b8ef 67 68 74 20 61 73 20 77 65 6c 6c 20 74 72 61 6e  ght as well tran
2b8f0 73 6c 61 74 65 20 65 76 65 72 79 0a 20 20 2a 2a  slate every.  **
2b8f1 20 49 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67   INSTEAD OF trig
2b8f2 67 65 72 20 69 6e 74 6f 20 61 20 42 45 46 4f 52  ger into a BEFOR
2b8f3 45 20 74 72 69 67 67 65 72 2e 20 20 49 74 20 73  E trigger.  It s
2b8f4 69 6d 70 6c 69 66 69 65 73 20 63 6f 64 65 0a 20  implifies code. 
2b8f5 20 2a 2a 20 65 6c 73 65 77 68 65 72 65 2e 0a 20   ** elsewhere.. 
2b8f6 20 2a 2f 0a 20 20 69 66 20 28 74 72 5f 74 6d 20   */.  if (tr_tm 
2b8f7 3d 3d 20 54 4b 5f 49 4e 53 54 45 41 44 29 7b 0a  == TK_INSTEAD){.
2b8f8 20 20 20 20 74 72 5f 74 6d 20 3d 20 54 4b 5f 42      tr_tm = TK_B
2b8f9 45 46 4f 52 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  EFORE;.  }..  /*
2b8fa 20 42 75 69 6c 64 20 74 68 65 20 54 72 69 67 67   Build the Trigg
2b8fb 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 70  er object */.  p
2b8fc 54 72 69 67 67 65 72 20 3d 20 28 54 72 69 67 67  Trigger = (Trigg
2b8fd 65 72 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c  er*)sqlite3DbMal
2b8fe 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65  locZero(db, size
2b8ff 6f 66 28 54 72 69 67 67 65 72 29 29 3b 0a 20 20  of(Trigger));.  
2b900 69 66 28 20 70 54 72 69 67 67 65 72 3d 3d 30 20  if( pTrigger==0 
2b901 29 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63  ) goto trigger_c
2b902 6c 65 61 6e 75 70 3b 0a 20 20 70 54 72 69 67 67  leanup;.  pTrigg
2b903 65 72 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e 61 6d  er->zName = zNam
2b904 65 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 30 3b 0a  e;.  zName = 0;.
2b905 20 20 70 54 72 69 67 67 65 72 2d 3e 74 61 62 6c    pTrigger->tabl
2b906 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  e = sqlite3DbStr
2b907 44 75 70 28 64 62 2c 20 70 54 61 62 6c 65 4e 61  Dup(db, pTableNa
2b908 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 29 3b  me->a[0].zName);
2b909 0a 20 20 70 54 72 69 67 67 65 72 2d 3e 70 53 63  .  pTrigger->pSc
2b90a 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69  hema = db->aDb[i
2b90b 44 62 5d 2e 70 53 63 68 65 6d 61 3b 0a 20 20 70  Db].pSchema;.  p
2b90c 54 72 69 67 67 65 72 2d 3e 70 54 61 62 53 63 68  Trigger->pTabSch
2b90d 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68  ema = pTab->pSch
2b90e 65 6d 61 3b 0a 20 20 70 54 72 69 67 67 65 72 2d  ema;.  pTrigger-
2b90f 3e 6f 70 20 3d 20 28 75 38 29 6f 70 3b 0a 20 20  >op = (u8)op;.  
2b910 70 54 72 69 67 67 65 72 2d 3e 74 72 5f 74 6d 20  pTrigger->tr_tm 
2b911 3d 20 74 72 5f 74 6d 3d 3d 54 4b 5f 42 45 46 4f  = tr_tm==TK_BEFO
2b912 52 45 20 3f 20 54 52 49 47 47 45 52 5f 42 45 46  RE ? TRIGGER_BEF
2b913 4f 52 45 20 3a 20 54 52 49 47 47 45 52 5f 41 46  ORE : TRIGGER_AF
2b914 54 45 52 3b 0a 20 20 70 54 72 69 67 67 65 72 2d  TER;.  pTrigger-
2b915 3e 70 57 68 65 6e 20 3d 20 73 71 6c 69 74 65 33  >pWhen = sqlite3
2b916 45 78 70 72 44 75 70 28 64 62 2c 20 70 57 68 65  ExprDup(db, pWhe
2b917 6e 2c 20 45 58 50 52 44 55 50 5f 52 45 44 55 43  n, EXPRDUP_REDUC
2b918 45 29 3b 0a 20 20 70 54 72 69 67 67 65 72 2d 3e  E);.  pTrigger->
2b919 70 43 6f 6c 75 6d 6e 73 20 3d 20 73 71 6c 69 74  pColumns = sqlit
2b91a 65 33 49 64 4c 69 73 74 44 75 70 28 64 62 2c 20  e3IdListDup(db, 
2b91b 70 43 6f 6c 75 6d 6e 73 29 3b 0a 20 20 61 73 73  pColumns);.  ass
2b91c 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70 4e 65  ert( pParse->pNe
2b91d 77 54 72 69 67 67 65 72 3d 3d 30 20 29 3b 0a 20  wTrigger==0 );. 
2b91e 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69   pParse->pNewTri
2b91f 67 67 65 72 20 3d 20 70 54 72 69 67 67 65 72 3b  gger = pTrigger;
2b920 0a 0a 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75  ..trigger_cleanu
2b921 70 3a 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  p:.  sqlite3DbFr
2b922 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20  ee(db, zName);. 
2b923 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44   sqlite3SrcListD
2b924 65 6c 65 74 65 28 64 62 2c 20 70 54 61 62 6c 65  elete(db, pTable
2b925 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  Name);.  sqlite3
2b926 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  IdListDelete(db,
2b927 20 70 43 6f 6c 75 6d 6e 73 29 3b 0a 20 20 73 71   pColumns);.  sq
2b928 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
2b929 64 62 2c 20 70 57 68 65 6e 29 3b 0a 20 20 69 66  db, pWhen);.  if
2b92a 28 20 21 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  ( !pParse->pNewT
2b92b 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20 73 71  rigger ){.    sq
2b92c 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67  lite3DeleteTrigg
2b92d 65 72 28 64 62 2c 20 70 54 72 69 67 67 65 72 29  er(db, pTrigger)
2b92e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
2b92f 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70  ssert( pParse->p
2b930 4e 65 77 54 72 69 67 67 65 72 3d 3d 70 54 72 69  NewTrigger==pTri
2b931 67 67 65 72 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  gger );.  }.}../
2b932 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
2b933 65 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65  e is called afte
2b934 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 74 72 69  r all of the tri
2b935 67 67 65 72 20 61 63 74 69 6f 6e 73 20 68 61 76  gger actions hav
2b936 65 20 62 65 65 6e 20 70 61 72 73 65 64 0a 2a 2a  e been parsed.**
2b937 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 6f 6d   in order to com
2b938 70 6c 65 74 65 20 74 68 65 20 70 72 6f 63 65 73  plete the proces
2b939 73 20 6f 66 20 62 75 69 6c 64 69 6e 67 20 74 68  s of building th
2b93a 65 20 74 72 69 67 67 65 72 2e 0a 2a 2f 0a 53 51  e trigger..*/.SQ
2b93b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2b93c 64 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68 54  d sqlite3FinishT
2b93d 72 69 67 67 65 72 28 0a 20 20 50 61 72 73 65 20  rigger(.  Parse 
2b93e 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
2b93f 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74    /* Parser cont
2b940 65 78 74 20 2a 2f 0a 20 20 54 72 69 67 67 65 72  ext */.  Trigger
2b941 53 74 65 70 20 2a 70 53 74 65 70 4c 69 73 74 2c  Step *pStepList,
2b942 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72 65   /* The triggere
2b943 64 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 54  d program */.  T
2b944 6f 6b 65 6e 20 2a 70 41 6c 6c 20 20 20 20 20 20  oken *pAll      
2b945 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20         /* Token 
2b946 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 74  that describes t
2b947 68 65 20 63 6f 6d 70 6c 65 74 65 20 43 52 45 41  he complete CREA
2b948 54 45 20 54 52 49 47 47 45 52 20 2a 2f 0a 29 7b  TE TRIGGER */.){
2b949 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69  .  Trigger *pTri
2b94a 67 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  g = pParse->pNew
2b94b 54 72 69 67 67 65 72 3b 20 20 20 20 2f 2a 20 54  Trigger;    /* T
2b94c 72 69 67 67 65 72 20 62 65 69 6e 67 20 66 69 6e  rigger being fin
2b94d 69 73 68 65 64 20 2a 2f 0a 20 20 63 68 61 72 20  ished */.  char 
2b94e 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20  *zName;         
2b94f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b950 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
2b951 72 69 67 67 65 72 20 2a 2f 0a 20 20 73 71 6c 69  rigger */.  sqli
2b952 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
2b953 2d 3e 64 62 3b 20 20 20 20 20 20 20 20 20 20 20  ->db;           
2b954 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
2b955 62 61 73 65 20 2a 2f 0a 20 20 44 62 46 69 78 65  base */.  DbFixe
2b956 72 20 73 46 69 78 3b 0a 20 20 69 6e 74 20 69 44  r sFix;.  int iD
2b957 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
2b958 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b959 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63     /* Database c
2b95a 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 72  ontaining the tr
2b95b 69 67 67 65 72 20 2a 2f 0a 20 20 54 6f 6b 65 6e  igger */.  Token
2b95c 20 6e 61 6d 65 54 6f 6b 65 6e 3b 20 20 20 20 20   nameToken;     
2b95d 20 20 20 20 20 20 2f 2a 20 54 72 69 67 67 65 72        /* Trigger
2b95e 20 6e 61 6d 65 20 66 6f 72 20 65 72 72 6f 72 20   name for error 
2b95f 72 65 70 6f 72 74 69 6e 67 20 2a 2f 0a 0a 20 20  reporting */..  
2b960 70 54 72 69 67 20 3d 20 70 50 61 72 73 65 2d 3e  pTrig = pParse->
2b961 70 4e 65 77 54 72 69 67 67 65 72 3b 0a 20 20 70  pNewTrigger;.  p
2b962 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67  Parse->pNewTrigg
2b963 65 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 4e 45  er = 0;.  if( NE
2b964 56 45 52 28 70 50 61 72 73 65 2d 3e 6e 45 72 72  VER(pParse->nErr
2b965 29 20 7c 7c 20 21 70 54 72 69 67 20 29 20 67 6f  ) || !pTrig ) go
2b966 74 6f 20 74 72 69 67 67 65 72 66 69 6e 69 73 68  to triggerfinish
2b967 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7a 4e 61 6d  _cleanup;.  zNam
2b968 65 20 3d 20 70 54 72 69 67 2d 3e 7a 4e 61 6d 65  e = pTrig->zName
2b969 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65  ;.  iDb = sqlite
2b96a 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70  3SchemaToIndex(p
2b96b 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 72 69 67  Parse->db, pTrig
2b96c 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 70 54  ->pSchema);.  pT
2b96d 72 69 67 2d 3e 73 74 65 70 5f 6c 69 73 74 20 3d  rig->step_list =
2b96e 20 70 53 74 65 70 4c 69 73 74 3b 0a 20 20 77 68   pStepList;.  wh
2b96f 69 6c 65 28 20 70 53 74 65 70 4c 69 73 74 20 29  ile( pStepList )
2b970 7b 0a 20 20 20 20 70 53 74 65 70 4c 69 73 74 2d  {.    pStepList-
2b971 3e 70 54 72 69 67 20 3d 20 70 54 72 69 67 3b 0a  >pTrig = pTrig;.
2b972 20 20 20 20 70 53 74 65 70 4c 69 73 74 20 3d 20      pStepList = 
2b973 70 53 74 65 70 4c 69 73 74 2d 3e 70 4e 65 78 74  pStepList->pNext
2b974 3b 0a 20 20 7d 0a 20 20 6e 61 6d 65 54 6f 6b 65  ;.  }.  nameToke
2b975 6e 2e 7a 20 3d 20 70 54 72 69 67 2d 3e 7a 4e 61  n.z = pTrig->zNa
2b976 6d 65 3b 0a 20 20 6e 61 6d 65 54 6f 6b 65 6e 2e  me;.  nameToken.
2b977 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  n = sqlite3Strle
2b978 6e 33 30 28 6e 61 6d 65 54 6f 6b 65 6e 2e 7a 29  n30(nameToken.z)
2b979 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 46  ;.  if( sqlite3F
2b97a 69 78 49 6e 69 74 28 26 73 46 69 78 2c 20 70 50  ixInit(&sFix, pP
2b97b 61 72 73 65 2c 20 69 44 62 2c 20 22 74 72 69 67  arse, iDb, "trig
2b97c 67 65 72 22 2c 20 26 6e 61 6d 65 54 6f 6b 65 6e  ger", &nameToken
2b97d 29 20 0a 20 20 20 20 20 20 20 20 20 20 26 26 20  ) .          && 
2b97e 73 71 6c 69 74 65 33 46 69 78 54 72 69 67 67 65  sqlite3FixTrigge
2b97f 72 53 74 65 70 28 26 73 46 69 78 2c 20 70 54 72  rStep(&sFix, pTr
2b980 69 67 2d 3e 73 74 65 70 5f 6c 69 73 74 29 20 29  ig->step_list) )
2b981 7b 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67  {.    goto trigg
2b982 65 72 66 69 6e 69 73 68 5f 63 6c 65 61 6e 75 70  erfinish_cleanup
2b983 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 69 66 20 77  ;.  }..  /* if w
2b984 65 20 61 72 65 20 6e 6f 74 20 69 6e 69 74 69 61  e are not initia
2b985 6c 69 7a 69 6e 67 2c 20 61 6e 64 20 74 68 69 73  lizing, and this
2b986 20 74 72 69 67 67 65 72 20 69 73 20 6e 6f 74 20   trigger is not 
2b987 6f 6e 20 61 20 54 45 4d 50 20 74 61 62 6c 65 2c  on a TEMP table,
2b988 20 0a 20 20 2a 2a 20 62 75 69 6c 64 20 74 68 65   .  ** build the
2b989 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 65   sqlite_master e
2b98a 6e 74 72 79 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ntry.  */.  if( 
2b98b 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29  !db->init.busy )
2b98c 7b 0a 20 20 20 20 56 64 62 65 20 2a 76 3b 0a 20  {.    Vdbe *v;. 
2b98d 20 20 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 20     char *z;..   
2b98e 20 2f 2a 20 4d 61 6b 65 20 61 6e 20 65 6e 74 72   /* Make an entr
2b98f 79 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 5f  y in the sqlite_
2b990 6d 61 73 74 65 72 20 74 61 62 6c 65 20 2a 2f 0a  master table */.
2b991 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47      v = sqlite3G
2b992 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
2b993 20 20 20 20 69 66 28 20 76 3d 3d 30 20 29 20 67      if( v==0 ) g
2b994 6f 74 6f 20 74 72 69 67 67 65 72 66 69 6e 69 73  oto triggerfinis
2b995 68 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 73  h_cleanup;.    s
2b996 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65  qlite3BeginWrite
2b997 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65  Operation(pParse
2b998 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 20 20 7a  , 0, iDb);.    z
2b999 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e   = sqlite3DbStrN
2b99a 44 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29 70  Dup(db, (char*)p
2b99b 41 6c 6c 2d 3e 7a 2c 20 70 41 6c 6c 2d 3e 6e 29  All->z, pAll->n)
2b99c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73  ;.    sqlite3Nes
2b99d 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c  tedParse(pParse,
2b99e 0a 20 20 20 20 20 20 20 22 49 4e 53 45 52 54 20  .       "INSERT 
2b99f 49 4e 54 4f 20 25 51 2e 25 73 20 56 41 4c 55 45  INTO %Q.%s VALUE
2b9a0 53 28 27 74 72 69 67 67 65 72 27 2c 25 51 2c 25  S('trigger',%Q,%
2b9a1 51 2c 30 2c 27 43 52 45 41 54 45 20 54 52 49 47  Q,0,'CREATE TRIG
2b9a2 47 45 52 20 25 71 27 29 22 2c 0a 20 20 20 20 20  GER %q')",.     
2b9a3 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a    db->aDb[iDb].z
2b9a4 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f 54 41 42  Name, SCHEMA_TAB
2b9a5 4c 45 28 69 44 62 29 2c 20 7a 4e 61 6d 65 2c 0a  LE(iDb), zName,.
2b9a6 20 20 20 20 20 20 20 70 54 72 69 67 2d 3e 74 61         pTrig->ta
2b9a7 62 6c 65 2c 20 7a 29 3b 0a 20 20 20 20 73 71 6c  ble, z);.    sql
2b9a8 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
2b9a9 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68  );.    sqlite3Ch
2b9aa 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73  angeCookie(pPars
2b9ab 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c  e, iDb);.    sql
2b9ac 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
2b9ad 2c 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61  , OP_ParseSchema
2b9ae 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20 73 71 6c  , iDb, 0, 0, sql
2b9af 69 74 65 33 4d 50 72 69 6e 74 66 28 0a 20 20 20  ite3MPrintf(.   
2b9b0 20 20 20 20 20 64 62 2c 20 22 74 79 70 65 3d 27       db, "type='
2b9b1 74 72 69 67 67 65 72 27 20 41 4e 44 20 6e 61 6d  trigger' AND nam
2b9b2 65 3d 27 25 71 27 22 2c 20 7a 4e 61 6d 65 29 2c  e='%q'", zName),
2b9b3 20 50 34 5f 44 59 4e 41 4d 49 43 0a 20 20 20 20   P4_DYNAMIC.    
2b9b4 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 64 62  );.  }..  if( db
2b9b5 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20  ->init.busy ){. 
2b9b6 20 20 20 54 72 69 67 67 65 72 20 2a 70 4c 69 6e     Trigger *pLin
2b9b7 6b 20 3d 20 70 54 72 69 67 3b 0a 20 20 20 20 48  k = pTrig;.    H
2b9b8 61 73 68 20 2a 70 48 61 73 68 20 3d 20 26 64 62  ash *pHash = &db
2b9b9 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65  ->aDb[iDb].pSche
2b9ba 6d 61 2d 3e 74 72 69 67 48 61 73 68 3b 0a 20 20  ma->trigHash;.  
2b9bb 20 20 70 54 72 69 67 20 3d 20 73 71 6c 69 74 65    pTrig = sqlite
2b9bc 33 48 61 73 68 49 6e 73 65 72 74 28 70 48 61 73  3HashInsert(pHas
2b9bd 68 2c 20 7a 4e 61 6d 65 2c 20 73 71 6c 69 74 65  h, zName, sqlite
2b9be 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29  3Strlen30(zName)
2b9bf 2c 20 70 54 72 69 67 29 3b 0a 20 20 20 20 69 66  , pTrig);.    if
2b9c0 28 20 70 54 72 69 67 20 29 7b 0a 20 20 20 20 20  ( pTrig ){.     
2b9c1 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2b9c2 64 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65  d = 1;.    }else
2b9c3 20 69 66 28 20 70 4c 69 6e 6b 2d 3e 70 53 63 68   if( pLink->pSch
2b9c4 65 6d 61 3d 3d 70 4c 69 6e 6b 2d 3e 70 54 61 62  ema==pLink->pTab
2b9c5 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 20 20  Schema ){.      
2b9c6 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20  Table *pTab;.   
2b9c7 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74     int n = sqlit
2b9c8 65 33 53 74 72 6c 65 6e 33 30 28 70 4c 69 6e 6b  e3Strlen30(pLink
2b9c9 2d 3e 74 61 62 6c 65 29 3b 0a 20 20 20 20 20 20  ->table);.      
2b9ca 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 48 61  pTab = sqlite3Ha
2b9cb 73 68 46 69 6e 64 28 26 70 4c 69 6e 6b 2d 3e 70  shFind(&pLink->p
2b9cc 54 61 62 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61  TabSchema->tblHa
2b9cd 73 68 2c 20 70 4c 69 6e 6b 2d 3e 74 61 62 6c 65  sh, pLink->table
2b9ce 2c 20 6e 29 3b 0a 20 20 20 20 20 20 61 73 73 65  , n);.      asse
2b9cf 72 74 28 20 70 54 61 62 21 3d 30 20 29 3b 0a 20  rt( pTab!=0 );. 
2b9d0 20 20 20 20 20 70 4c 69 6e 6b 2d 3e 70 4e 65 78       pLink->pNex
2b9d1 74 20 3d 20 70 54 61 62 2d 3e 70 54 72 69 67 67  t = pTab->pTrigg
2b9d2 65 72 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e  er;.      pTab->
2b9d3 70 54 72 69 67 67 65 72 20 3d 20 70 4c 69 6e 6b  pTrigger = pLink
2b9d4 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 74 72 69  ;.    }.  }..tri
2b9d5 67 67 65 72 66 69 6e 69 73 68 5f 63 6c 65 61 6e  ggerfinish_clean
2b9d6 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 44 65 6c  up:.  sqlite3Del
2b9d7 65 74 65 54 72 69 67 67 65 72 28 64 62 2c 20 70  eteTrigger(db, p
2b9d8 54 72 69 67 29 3b 0a 20 20 61 73 73 65 72 74 28  Trig);.  assert(
2b9d9 20 21 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72   !pParse->pNewTr
2b9da 69 67 67 65 72 20 29 3b 0a 20 20 73 71 6c 69 74  igger );.  sqlit
2b9db 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 53  e3DeleteTriggerS
2b9dc 74 65 70 28 64 62 2c 20 70 53 74 65 70 4c 69 73  tep(db, pStepLis
2b9dd 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 75 72  t);.}../*.** Tur
2b9de 6e 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65  n a SELECT state
2b9df 6d 65 6e 74 20 28 74 68 61 74 20 74 68 65 20 70  ment (that the p
2b9e0 53 65 6c 65 63 74 20 70 61 72 61 6d 65 74 65 72  Select parameter
2b9e1 20 70 6f 69 6e 74 73 20 74 6f 29 20 69 6e 74 6f   points to) into
2b9e2 0a 2a 2a 20 61 20 74 72 69 67 67 65 72 20 73 74  .** a trigger st
2b9e3 65 70 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f  ep.  Return a po
2b9e4 69 6e 74 65 72 20 74 6f 20 61 20 54 72 69 67 67  inter to a Trigg
2b9e5 65 72 53 74 65 70 20 73 74 72 75 63 74 75 72 65  erStep structure
2b9e6 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73  ..**.** The pars
2b9e7 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f  er calls this ro
2b9e8 75 74 69 6e 65 20 77 68 65 6e 20 69 74 20 66 69  utine when it fi
2b9e9 6e 64 73 20 61 20 53 45 4c 45 43 54 20 73 74 61  nds a SELECT sta
2b9ea 74 65 6d 65 6e 74 20 69 6e 0a 2a 2a 20 62 6f 64  tement in.** bod
2b9eb 79 20 6f 66 20 61 20 54 52 49 47 47 45 52 2e 20  y of a TRIGGER. 
2b9ec 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56   .*/.SQLITE_PRIV
2b9ed 41 54 45 20 54 72 69 67 67 65 72 53 74 65 70 20  ATE TriggerStep 
2b9ee 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 53  *sqlite3TriggerS
2b9ef 65 6c 65 63 74 53 74 65 70 28 73 71 6c 69 74 65  electStep(sqlite
2b9f0 33 20 2a 64 62 2c 20 53 65 6c 65 63 74 20 2a 70  3 *db, Select *p
2b9f1 53 65 6c 65 63 74 29 7b 0a 20 20 54 72 69 67 67  Select){.  Trigg
2b9f2 65 72 53 74 65 70 20 2a 70 54 72 69 67 67 65 72  erStep *pTrigger
2b9f3 53 74 65 70 20 3d 20 73 71 6c 69 74 65 33 44 62  Step = sqlite3Db
2b9f4 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
2b9f5 69 7a 65 6f 66 28 54 72 69 67 67 65 72 53 74 65  izeof(TriggerSte
2b9f6 70 29 29 3b 0a 20 20 69 66 28 20 70 54 72 69 67  p));.  if( pTrig
2b9f7 67 65 72 53 74 65 70 3d 3d 30 20 29 20 7b 0a 20  gerStep==0 ) {. 
2b9f8 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74     sqlite3Select
2b9f9 44 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65  Delete(db, pSele
2b9fa 63 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ct);.    return 
2b9fb 30 3b 0a 20 20 7d 0a 20 20 70 54 72 69 67 67 65  0;.  }.  pTrigge
2b9fc 72 53 74 65 70 2d 3e 6f 70 20 3d 20 54 4b 5f 53  rStep->op = TK_S
2b9fd 45 4c 45 43 54 3b 0a 20 20 70 54 72 69 67 67 65  ELECT;.  pTrigge
2b9fe 72 53 74 65 70 2d 3e 70 53 65 6c 65 63 74 20 3d  rStep->pSelect =
2b9ff 20 70 53 65 6c 65 63 74 3b 0a 20 20 70 54 72 69   pSelect;.  pTri
2ba00 67 67 65 72 53 74 65 70 2d 3e 6f 72 63 6f 6e 66  ggerStep->orconf
2ba01 20 3d 20 4f 45 5f 44 65 66 61 75 6c 74 3b 0a 20   = OE_Default;. 
2ba02 20 72 65 74 75 72 6e 20 70 54 72 69 67 67 65 72   return pTrigger
2ba03 53 74 65 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  Step;.}../*.** A
2ba04 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 74 6f  llocate space to
2ba05 20 68 6f 6c 64 20 61 20 6e 65 77 20 74 72 69 67   hold a new trig
2ba06 67 65 72 20 73 74 65 70 2e 20 20 54 68 65 20 61  ger step.  The a
2ba07 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 0a 2a  llocated space.*
2ba08 2a 20 68 6f 6c 64 73 20 62 6f 74 68 20 74 68 65  * holds both the
2ba09 20 54 72 69 67 67 65 72 53 74 65 70 20 6f 62 6a   TriggerStep obj
2ba0a 65 63 74 20 61 6e 64 20 74 68 65 20 54 72 69 67  ect and the Trig
2ba0b 67 65 72 53 74 65 70 2e 74 61 72 67 65 74 2e 7a  gerStep.target.z
2ba0c 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49   string..**.** I
2ba0d 66 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 20 6f  f an OOM error o
2ba0e 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69 73 20 72  ccurs, NULL is r
2ba0f 65 74 75 72 6e 65 64 20 61 6e 64 20 64 62 2d 3e  eturned and db->
2ba10 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 69 73 20  mallocFailed is 
2ba11 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 54  set..*/.static T
2ba12 72 69 67 67 65 72 53 74 65 70 20 2a 74 72 69 67  riggerStep *trig
2ba13 67 65 72 53 74 65 70 41 6c 6c 6f 63 61 74 65 28  gerStepAllocate(
2ba14 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
2ba15 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2ba16 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * Database conne
2ba17 63 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 20 6f 70  ction */.  u8 op
2ba18 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2ba19 20 20 20 20 20 20 20 2f 2a 20 54 72 69 67 67 65         /* Trigge
2ba1a 72 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 54 6f  r opcode */.  To
2ba1b 6b 65 6e 20 2a 70 4e 61 6d 65 20 20 20 20 20 20  ken *pName      
2ba1c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2ba1d 20 74 61 72 67 65 74 20 6e 61 6d 65 20 2a 2f 0a   target name */.
2ba1e 29 7b 0a 20 20 54 72 69 67 67 65 72 53 74 65 70  ){.  TriggerStep
2ba1f 20 2a 70 54 72 69 67 67 65 72 53 74 65 70 3b 0a   *pTriggerStep;.
2ba20 0a 20 20 70 54 72 69 67 67 65 72 53 74 65 70 20  .  pTriggerStep 
2ba21 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
2ba22 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
2ba23 28 54 72 69 67 67 65 72 53 74 65 70 29 20 2b 20  (TriggerStep) + 
2ba24 70 4e 61 6d 65 2d 3e 6e 29 3b 0a 20 20 69 66 28  pName->n);.  if(
2ba25 20 70 54 72 69 67 67 65 72 53 74 65 70 20 29 7b   pTriggerStep ){
2ba26 0a 20 20 20 20 63 68 61 72 20 2a 7a 20 3d 20 28  .    char *z = (
2ba27 63 68 61 72 2a 29 26 70 54 72 69 67 67 65 72 53  char*)&pTriggerS
2ba28 74 65 70 5b 31 5d 3b 0a 20 20 20 20 6d 65 6d 63  tep[1];.    memc
2ba29 70 79 28 7a 2c 20 70 4e 61 6d 65 2d 3e 7a 2c 20  py(z, pName->z, 
2ba2a 70 4e 61 6d 65 2d 3e 6e 29 3b 0a 20 20 20 20 70  pName->n);.    p
2ba2b 54 72 69 67 67 65 72 53 74 65 70 2d 3e 74 61 72  TriggerStep->tar
2ba2c 67 65 74 2e 7a 20 3d 20 7a 3b 0a 20 20 20 20 70  get.z = z;.    p
2ba2d 54 72 69 67 67 65 72 53 74 65 70 2d 3e 74 61 72  TriggerStep->tar
2ba2e 67 65 74 2e 6e 20 3d 20 70 4e 61 6d 65 2d 3e 6e  get.n = pName->n
2ba2f 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72 53 74  ;.    pTriggerSt
2ba30 65 70 2d 3e 6f 70 20 3d 20 6f 70 3b 0a 20 20 7d  ep->op = op;.  }
2ba31 0a 20 20 72 65 74 75 72 6e 20 70 54 72 69 67 67  .  return pTrigg
2ba32 65 72 53 74 65 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  erStep;.}../*.**
2ba33 20 42 75 69 6c 64 20 61 20 74 72 69 67 67 65 72   Build a trigger
2ba34 20 73 74 65 70 20 6f 75 74 20 6f 66 20 61 6e 20   step out of an 
2ba35 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74  INSERT statement
2ba36 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  .  Return a poin
2ba37 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 6e 65  ter.** to the ne
2ba38 77 20 74 72 69 67 67 65 72 20 73 74 65 70 2e 0a  w trigger step..
2ba39 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73 65 72  **.** The parser
2ba3a 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74   calls this rout
2ba3b 69 6e 65 20 77 68 65 6e 20 69 74 20 73 65 65 73  ine when it sees
2ba3c 20 61 6e 20 49 4e 53 45 52 54 20 69 6e 73 69 64   an INSERT insid
2ba3d 65 20 74 68 65 0a 2a 2a 20 62 6f 64 79 20 6f 66  e the.** body of
2ba3e 20 61 20 74 72 69 67 67 65 72 2e 0a 2a 2f 0a 53   a trigger..*/.S
2ba3f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 72  QLITE_PRIVATE Tr
2ba40 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74  iggerStep *sqlit
2ba41 65 33 54 72 69 67 67 65 72 49 6e 73 65 72 74 53  e3TriggerInsertS
2ba42 74 65 70 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  tep(.  sqlite3 *
2ba43 64 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68  db,        /* Th
2ba44 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
2ba45 63 74 69 6f 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e  ction */.  Token
2ba46 20 2a 70 54 61 62 6c 65 4e 61 6d 65 2c 20 20 2f   *pTableName,  /
2ba47 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  * Name of the ta
2ba48 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77  ble into which w
2ba49 65 20 69 6e 73 65 72 74 20 2a 2f 0a 20 20 49 64  e insert */.  Id
2ba4a 4c 69 73 74 20 2a 70 43 6f 6c 75 6d 6e 2c 20 20  List *pColumn,  
2ba4b 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 63 6f 6c    /* List of col
2ba4c 75 6d 6e 73 20 69 6e 20 70 54 61 62 6c 65 4e 61  umns in pTableNa
2ba4d 6d 65 20 74 6f 20 69 6e 73 65 72 74 20 69 6e 74  me to insert int
2ba4e 6f 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  o */.  ExprList 
2ba4f 2a 70 45 4c 69 73 74 2c 20 20 20 2f 2a 20 54 68  *pEList,   /* Th
2ba50 65 20 56 41 4c 55 45 20 63 6c 61 75 73 65 3a 20  e VALUE clause: 
2ba51 61 20 6c 69 73 74 20 6f 66 20 76 61 6c 75 65 73  a list of values
2ba52 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20   to be inserted 
2ba53 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65  */.  Select *pSe
2ba54 6c 65 63 74 2c 20 20 20 20 2f 2a 20 41 20 53 45  lect,    /* A SE
2ba55 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74  LECT statement t
2ba56 68 61 74 20 73 75 70 70 6c 69 65 73 20 76 61 6c  hat supplies val
2ba57 75 65 73 20 2a 2f 0a 20 20 75 38 20 6f 72 63 6f  ues */.  u8 orco
2ba58 6e 66 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  nf           /* 
2ba59 54 68 65 20 63 6f 6e 66 6c 69 63 74 20 61 6c 67  The conflict alg
2ba5a 6f 72 69 74 68 6d 20 28 4f 45 5f 41 62 6f 72 74  orithm (OE_Abort
2ba5b 2c 20 4f 45 5f 52 65 70 6c 61 63 65 2c 20 65 74  , OE_Replace, et
2ba5c 63 2e 29 20 2a 2f 0a 29 7b 0a 20 20 54 72 69 67  c.) */.){.  Trig
2ba5d 67 65 72 53 74 65 70 20 2a 70 54 72 69 67 67 65  gerStep *pTrigge
2ba5e 72 53 74 65 70 3b 0a 0a 20 20 61 73 73 65 72 74  rStep;..  assert
2ba5f 28 70 45 4c 69 73 74 20 3d 3d 20 30 20 7c 7c 20  (pEList == 0 || 
2ba60 70 53 65 6c 65 63 74 20 3d 3d 20 30 29 3b 0a 20  pSelect == 0);. 
2ba61 20 61 73 73 65 72 74 28 70 45 4c 69 73 74 20 21   assert(pEList !
2ba62 3d 20 30 20 7c 7c 20 70 53 65 6c 65 63 74 20 21  = 0 || pSelect !
2ba63 3d 20 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  = 0 || db->mallo
2ba64 63 46 61 69 6c 65 64 29 3b 0a 0a 20 20 70 54 72  cFailed);..  pTr
2ba65 69 67 67 65 72 53 74 65 70 20 3d 20 74 72 69 67  iggerStep = trig
2ba66 67 65 72 53 74 65 70 41 6c 6c 6f 63 61 74 65 28  gerStepAllocate(
2ba67 64 62 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20 70  db, TK_INSERT, p
2ba68 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 69 66  TableName);.  if
2ba69 28 20 70 54 72 69 67 67 65 72 53 74 65 70 20 29  ( pTriggerStep )
2ba6a 7b 0a 20 20 20 20 70 54 72 69 67 67 65 72 53 74  {.    pTriggerSt
2ba6b 65 70 2d 3e 70 53 65 6c 65 63 74 20 3d 20 73 71  ep->pSelect = sq
2ba6c 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 64  lite3SelectDup(d
2ba6d 62 2c 20 70 53 65 6c 65 63 74 2c 20 45 58 50 52  b, pSelect, EXPR
2ba6e 44 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 20 20  DUP_REDUCE);.   
2ba6f 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70   pTriggerStep->p
2ba70 49 64 4c 69 73 74 20 3d 20 70 43 6f 6c 75 6d 6e  IdList = pColumn
2ba71 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72 53 74  ;.    pTriggerSt
2ba72 65 70 2d 3e 70 45 78 70 72 4c 69 73 74 20 3d 20  ep->pExprList = 
2ba73 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
2ba74 75 70 28 64 62 2c 20 70 45 4c 69 73 74 2c 20 45  up(db, pEList, E
2ba75 58 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b 0a  XPRDUP_REDUCE);.
2ba76 20 20 20 20 70 54 72 69 67 67 65 72 53 74 65 70      pTriggerStep
2ba77 2d 3e 6f 72 63 6f 6e 66 20 3d 20 6f 72 63 6f 6e  ->orconf = orcon
2ba78 66 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  f;.  }else{.    
2ba79 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c  sqlite3IdListDel
2ba7a 65 74 65 28 64 62 2c 20 70 43 6f 6c 75 6d 6e 29  ete(db, pColumn)
2ba7b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45  ;.  }.  sqlite3E
2ba7c 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62  xprListDelete(db
2ba7d 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 73 71 6c  , pEList);.  sql
2ba7e 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
2ba7f 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a 0a  (db, pSelect);..
2ba80 20 20 72 65 74 75 72 6e 20 70 54 72 69 67 67 65    return pTrigge
2ba81 72 53 74 65 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rStep;.}../*.** 
2ba82 43 6f 6e 73 74 72 75 63 74 20 61 20 74 72 69 67  Construct a trig
2ba83 67 65 72 20 73 74 65 70 20 74 68 61 74 20 69 6d  ger step that im
2ba84 70 6c 65 6d 65 6e 74 73 20 61 6e 20 55 50 44 41  plements an UPDA
2ba85 54 45 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64  TE statement and
2ba86 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69   return.** a poi
2ba87 6e 74 65 72 20 74 6f 20 74 68 61 74 20 74 72 69  nter to that tri
2ba88 67 67 65 72 20 73 74 65 70 2e 20 20 54 68 65 20  gger step.  The 
2ba89 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69  parser calls thi
2ba8a 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 69  s routine when i
2ba8b 74 0a 2a 2a 20 73 65 65 73 20 61 6e 20 55 50 44  t.** sees an UPD
2ba8c 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69 6e  ATE statement in
2ba8d 73 69 64 65 20 74 68 65 20 62 6f 64 79 20 6f 66  side the body of
2ba8e 20 61 20 43 52 45 41 54 45 20 54 52 49 47 47 45   a CREATE TRIGGE
2ba8f 52 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  R..*/.SQLITE_PRI
2ba90 56 41 54 45 20 54 72 69 67 67 65 72 53 74 65 70  VATE TriggerStep
2ba91 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72   *sqlite3Trigger
2ba92 55 70 64 61 74 65 53 74 65 70 28 0a 20 20 73 71  UpdateStep(.  sq
2ba93 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
2ba94 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
2ba95 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
2ba96 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65  .  Token *pTable
2ba97 4e 61 6d 65 2c 20 20 20 2f 2a 20 4e 61 6d 65 20  Name,   /* Name 
2ba98 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20  of the table to 
2ba99 62 65 20 75 70 64 61 74 65 64 20 2a 2f 0a 20 20  be updated */.  
2ba9a 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
2ba9b 2c 20 20 20 20 2f 2a 20 54 68 65 20 53 45 54 20  ,    /* The SET 
2ba9c 63 6c 61 75 73 65 3a 20 6c 69 73 74 20 6f 66 20  clause: list of 
2ba9d 63 6f 6c 75 6d 6e 20 61 6e 64 20 6e 65 77 20 76  column and new v
2ba9e 61 6c 75 65 73 20 2a 2f 0a 20 20 45 78 70 72 20  alues */.  Expr 
2ba9f 2a 70 57 68 65 72 65 2c 20 20 20 20 20 20 20 20  *pWhere,        
2baa0 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61  /* The WHERE cla
2baa1 75 73 65 20 2a 2f 0a 20 20 75 38 20 6f 72 63 6f  use */.  u8 orco
2baa2 6e 66 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  nf            /*
2baa3 20 54 68 65 20 63 6f 6e 66 6c 69 63 74 20 61 6c   The conflict al
2baa4 67 6f 72 69 74 68 6d 2e 20 28 4f 45 5f 41 62 6f  gorithm. (OE_Abo
2baa5 72 74 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 65  rt, OE_Ignore, e
2baa6 74 63 29 20 2a 2f 0a 29 7b 0a 20 20 54 72 69 67  tc) */.){.  Trig
2baa7 67 65 72 53 74 65 70 20 2a 70 54 72 69 67 67 65  gerStep *pTrigge
2baa8 72 53 74 65 70 3b 0a 0a 20 20 70 54 72 69 67 67  rStep;..  pTrigg
2baa9 65 72 53 74 65 70 20 3d 20 74 72 69 67 67 65 72  erStep = trigger
2baaa 53 74 65 70 41 6c 6c 6f 63 61 74 65 28 64 62 2c  StepAllocate(db,
2baab 20 54 4b 5f 55 50 44 41 54 45 2c 20 70 54 61 62   TK_UPDATE, pTab
2baac 6c 65 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 70  leName);.  if( p
2baad 54 72 69 67 67 65 72 53 74 65 70 20 29 7b 0a 20  TriggerStep ){. 
2baae 20 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d     pTriggerStep-
2baaf 3e 70 45 78 70 72 4c 69 73 74 20 3d 20 73 71 6c  >pExprList = sql
2bab0 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28  ite3ExprListDup(
2bab1 64 62 2c 20 70 45 4c 69 73 74 2c 20 45 58 50 52  db, pEList, EXPR
2bab2 44 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 20 20  DUP_REDUCE);.   
2bab3 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70   pTriggerStep->p
2bab4 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45  Where = sqlite3E
2bab5 78 70 72 44 75 70 28 64 62 2c 20 70 57 68 65 72  xprDup(db, pWher
2bab6 65 2c 20 45 58 50 52 44 55 50 5f 52 45 44 55 43  e, EXPRDUP_REDUC
2bab7 45 29 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72  E);.    pTrigger
2bab8 53 74 65 70 2d 3e 6f 72 63 6f 6e 66 20 3d 20 6f  Step->orconf = o
2bab9 72 63 6f 6e 66 3b 0a 20 20 7d 0a 20 20 73 71 6c  rconf;.  }.  sql
2baba 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
2babb 74 65 28 64 62 2c 20 70 45 4c 69 73 74 29 3b 0a  te(db, pEList);.
2babc 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
2babd 65 74 65 28 64 62 2c 20 70 57 68 65 72 65 29 3b  ete(db, pWhere);
2babe 0a 20 20 72 65 74 75 72 6e 20 70 54 72 69 67 67  .  return pTrigg
2babf 65 72 53 74 65 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  erStep;.}../*.**
2bac0 20 43 6f 6e 73 74 72 75 63 74 20 61 20 74 72 69   Construct a tri
2bac1 67 67 65 72 20 73 74 65 70 20 74 68 61 74 20 69  gger step that i
2bac2 6d 70 6c 65 6d 65 6e 74 73 20 61 20 44 45 4c 45  mplements a DELE
2bac3 54 45 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64  TE statement and
2bac4 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69   return.** a poi
2bac5 6e 74 65 72 20 74 6f 20 74 68 61 74 20 74 72 69  nter to that tri
2bac6 67 67 65 72 20 73 74 65 70 2e 20 20 54 68 65 20  gger step.  The 
2bac7 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69  parser calls thi
2bac8 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 69  s routine when i
2bac9 74 0a 2a 2a 20 73 65 65 73 20 61 20 44 45 4c 45  t.** sees a DELE
2baca 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 73  TE statement ins
2bacb 69 64 65 20 74 68 65 20 62 6f 64 79 20 6f 66 20  ide the body of 
2bacc 61 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52  a CREATE TRIGGER
2bacd 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2bace 41 54 45 20 54 72 69 67 67 65 72 53 74 65 70 20  ATE TriggerStep 
2bacf 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 44  *sqlite3TriggerD
2bad0 65 6c 65 74 65 53 74 65 70 28 0a 20 20 73 71 6c  eleteStep(.  sql
2bad1 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
2bad2 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
2bad3 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
2bad4 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 4e 61   Token *pTableNa
2bad5 6d 65 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20  me,      /* The 
2bad6 74 61 62 6c 65 20 66 72 6f 6d 20 77 68 69 63 68  table from which
2bad7 20 72 6f 77 73 20 61 72 65 20 64 65 6c 65 74 65   rows are delete
2bad8 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68  d */.  Expr *pWh
2bad9 65 72 65 20 20 20 20 20 20 20 20 20 20 20 20 2f  ere            /
2bada 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75  * The WHERE clau
2badb 73 65 20 2a 2f 0a 29 7b 0a 20 20 54 72 69 67 67  se */.){.  Trigg
2badc 65 72 53 74 65 70 20 2a 70 54 72 69 67 67 65 72  erStep *pTrigger
2badd 53 74 65 70 3b 0a 0a 20 20 70 54 72 69 67 67 65  Step;..  pTrigge
2bade 72 53 74 65 70 20 3d 20 74 72 69 67 67 65 72 53  rStep = triggerS
2badf 74 65 70 41 6c 6c 6f 63 61 74 65 28 64 62 2c 20  tepAllocate(db, 
2bae0 54 4b 5f 44 45 4c 45 54 45 2c 20 70 54 61 62 6c  TK_DELETE, pTabl
2bae1 65 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 70 54  eName);.  if( pT
2bae2 72 69 67 67 65 72 53 74 65 70 20 29 7b 0a 20 20  riggerStep ){.  
2bae3 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e    pTriggerStep->
2bae4 70 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33  pWhere = sqlite3
2bae5 45 78 70 72 44 75 70 28 64 62 2c 20 70 57 68 65  ExprDup(db, pWhe
2bae6 72 65 2c 20 45 58 50 52 44 55 50 5f 52 45 44 55  re, EXPRDUP_REDU
2bae7 43 45 29 3b 0a 20 20 20 20 70 54 72 69 67 67 65  CE);.    pTrigge
2bae8 72 53 74 65 70 2d 3e 6f 72 63 6f 6e 66 20 3d 20  rStep->orconf = 
2bae9 4f 45 5f 44 65 66 61 75 6c 74 3b 0a 20 20 7d 0a  OE_Default;.  }.
2baea 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
2baeb 65 74 65 28 64 62 2c 20 70 57 68 65 72 65 29 3b  ete(db, pWhere);
2baec 0a 20 20 72 65 74 75 72 6e 20 70 54 72 69 67 67  .  return pTrigg
2baed 65 72 53 74 65 70 3b 0a 7d 0a 0a 2f 2a 20 0a 2a  erStep;.}../* .*
2baee 2a 20 52 65 63 75 72 73 69 76 65 6c 79 20 64 65  * Recursively de
2baef 6c 65 74 65 20 61 20 54 72 69 67 67 65 72 20 73  lete a Trigger s
2baf0 74 72 75 63 74 75 72 65 0a 2a 2f 0a 53 51 4c 49  tructure.*/.SQLI
2baf1 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2baf2 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69  sqlite3DeleteTri
2baf3 67 67 65 72 28 73 71 6c 69 74 65 33 20 2a 64 62  gger(sqlite3 *db
2baf4 2c 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67  , Trigger *pTrig
2baf5 67 65 72 29 7b 0a 20 20 69 66 28 20 70 54 72 69  ger){.  if( pTri
2baf6 67 67 65 72 3d 3d 30 20 29 20 72 65 74 75 72 6e  gger==0 ) return
2baf7 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74  ;.  sqlite3Delet
2baf8 65 54 72 69 67 67 65 72 53 74 65 70 28 64 62 2c  eTriggerStep(db,
2baf9 20 70 54 72 69 67 67 65 72 2d 3e 73 74 65 70 5f   pTrigger->step_
2bafa 6c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  list);.  sqlite3
2bafb 44 62 46 72 65 65 28 64 62 2c 20 70 54 72 69 67  DbFree(db, pTrig
2bafc 67 65 72 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 73  ger->zName);.  s
2bafd 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2bafe 20 70 54 72 69 67 67 65 72 2d 3e 74 61 62 6c 65   pTrigger->table
2baff 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
2bb00 44 65 6c 65 74 65 28 64 62 2c 20 70 54 72 69 67  Delete(db, pTrig
2bb01 67 65 72 2d 3e 70 57 68 65 6e 29 3b 0a 20 20 73  ger->pWhen);.  s
2bb02 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65  qlite3IdListDele
2bb03 74 65 28 64 62 2c 20 70 54 72 69 67 67 65 72 2d  te(db, pTrigger-
2bb04 3e 70 43 6f 6c 75 6d 6e 73 29 3b 0a 20 20 73 71  >pColumns);.  sq
2bb05 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2bb06 70 54 72 69 67 67 65 72 29 3b 0a 7d 0a 0a 2f 2a  pTrigger);.}../*
2bb07 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
2bb08 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 64  n is called to d
2bb09 72 6f 70 20 61 20 74 72 69 67 67 65 72 20 66 72  rop a trigger fr
2bb0a 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20  om the database 
2bb0b 73 63 68 65 6d 61 2e 20 0a 2a 2a 0a 2a 2a 20 54  schema. .**.** T
2bb0c 68 69 73 20 6d 61 79 20 62 65 20 63 61 6c 6c 65  his may be calle
2bb0d 64 20 64 69 72 65 63 74 6c 79 20 66 72 6f 6d 20  d directly from 
2bb0e 74 68 65 20 70 61 72 73 65 72 20 61 6e 64 20 74  the parser and t
2bb0f 68 65 72 65 66 6f 72 65 20 69 64 65 6e 74 69 66  herefore identif
2bb10 69 65 73 0a 2a 2a 20 74 68 65 20 74 72 69 67 67  ies.** the trigg
2bb11 65 72 20 62 79 20 6e 61 6d 65 2e 20 20 54 68 65  er by name.  The
2bb12 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67   sqlite3DropTrig
2bb13 67 65 72 50 74 72 28 29 20 72 6f 75 74 69 6e 65  gerPtr() routine
2bb14 20 64 6f 65 73 20 74 68 65 0a 2a 2a 20 73 61 6d   does the.** sam
2bb15 65 20 6a 6f 62 20 61 73 20 74 68 69 73 20 72 6f  e job as this ro
2bb16 75 74 69 6e 65 20 65 78 63 65 70 74 20 69 74 20  utine except it 
2bb17 74 61 6b 65 73 20 61 20 70 6f 69 6e 74 65 72 20  takes a pointer 
2bb18 74 6f 20 74 68 65 20 74 72 69 67 67 65 72 0a 2a  to the trigger.*
2bb19 2a 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65  * instead of the
2bb1a 20 74 72 69 67 67 65 72 20 6e 61 6d 65 2e 0a 2a   trigger name..*
2bb1b 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2bb1c 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72  E void sqlite3Dr
2bb1d 6f 70 54 72 69 67 67 65 72 28 50 61 72 73 65 20  opTrigger(Parse 
2bb1e 2a 70 50 61 72 73 65 2c 20 53 72 63 4c 69 73 74  *pParse, SrcList
2bb1f 20 2a 70 4e 61 6d 65 2c 20 69 6e 74 20 6e 6f 45   *pName, int noE
2bb20 72 72 29 7b 0a 20 20 54 72 69 67 67 65 72 20 2a  rr){.  Trigger *
2bb21 70 54 72 69 67 67 65 72 20 3d 20 30 3b 0a 20 20  pTrigger = 0;.  
2bb22 69 6e 74 20 69 3b 0a 20 20 63 6f 6e 73 74 20 63  int i;.  const c
2bb23 68 61 72 20 2a 7a 44 62 3b 0a 20 20 63 6f 6e 73  har *zDb;.  cons
2bb24 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20  t char *zName;. 
2bb25 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 73 71   int nName;.  sq
2bb26 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
2bb27 73 65 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20 64  se->db;..  if( d
2bb28 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2bb29 29 20 67 6f 74 6f 20 64 72 6f 70 5f 74 72 69 67  ) goto drop_trig
2bb2a 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 69  ger_cleanup;.  i
2bb2b 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71  f( SQLITE_OK!=sq
2bb2c 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28  lite3ReadSchema(
2bb2d 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20 67  pParse) ){.    g
2bb2e 6f 74 6f 20 64 72 6f 70 5f 74 72 69 67 67 65 72  oto drop_trigger
2bb2f 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20  _cleanup;.  }.. 
2bb30 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65 2d 3e   assert( pName->
2bb31 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 7a 44 62  nSrc==1 );.  zDb
2bb32 20 3d 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a   = pName->a[0].z
2bb33 44 61 74 61 62 61 73 65 3b 0a 20 20 7a 4e 61 6d  Database;.  zNam
2bb34 65 20 3d 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e  e = pName->a[0].
2bb35 7a 4e 61 6d 65 3b 0a 20 20 6e 4e 61 6d 65 20 3d  zName;.  nName =
2bb36 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
2bb37 28 7a 4e 61 6d 65 29 3b 0a 20 20 66 6f 72 28 69  (zName);.  for(i
2bb38 3d 4f 4d 49 54 5f 54 45 4d 50 44 42 3b 20 69 3c  =OMIT_TEMPDB; i<
2bb39 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20  db->nDb; i++){. 
2bb3a 20 20 20 69 6e 74 20 6a 20 3d 20 28 69 3c 32 29     int j = (i<2)
2bb3b 20 3f 20 69 5e 31 20 3a 20 69 3b 20 20 2f 2a 20   ? i^1 : i;  /* 
2bb3c 53 65 61 72 63 68 20 54 45 4d 50 20 62 65 66 6f  Search TEMP befo
2bb3d 72 65 20 4d 41 49 4e 20 2a 2f 0a 20 20 20 20 69  re MAIN */.    i
2bb3e 66 28 20 7a 44 62 20 26 26 20 73 71 6c 69 74 65  f( zDb && sqlite
2bb3f 33 53 74 72 49 43 6d 70 28 64 62 2d 3e 61 44 62  3StrICmp(db->aDb
2bb40 5b 6a 5d 2e 7a 4e 61 6d 65 2c 20 7a 44 62 29 20  [j].zName, zDb) 
2bb41 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
2bb42 70 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69 74  pTrigger = sqlit
2bb43 65 33 48 61 73 68 46 69 6e 64 28 26 28 64 62 2d  e3HashFind(&(db-
2bb44 3e 61 44 62 5b 6a 5d 2e 70 53 63 68 65 6d 61 2d  >aDb[j].pSchema-
2bb45 3e 74 72 69 67 48 61 73 68 29 2c 20 7a 4e 61 6d  >trigHash), zNam
2bb46 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 69  e, nName);.    i
2bb47 66 28 20 70 54 72 69 67 67 65 72 20 29 20 62 72  f( pTrigger ) br
2bb48 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21  eak;.  }.  if( !
2bb49 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20  pTrigger ){.    
2bb4a 69 66 28 20 21 6e 6f 45 72 72 20 29 7b 0a 20 20  if( !noErr ){.  
2bb4b 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2bb4c 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20  Msg(pParse, "no 
2bb4d 73 75 63 68 20 74 72 69 67 67 65 72 3a 20 25 53  such trigger: %S
2bb4e 22 2c 20 70 4e 61 6d 65 2c 20 30 29 3b 0a 20 20  ", pName, 0);.  
2bb4f 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20 64 72 6f    }.    goto dro
2bb50 70 5f 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75  p_trigger_cleanu
2bb51 70 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  p;.  }.  sqlite3
2bb52 44 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 70  DropTriggerPtr(p
2bb53 50 61 72 73 65 2c 20 70 54 72 69 67 67 65 72 29  Parse, pTrigger)
2bb54 3b 0a 0a 64 72 6f 70 5f 74 72 69 67 67 65 72 5f  ;..drop_trigger_
2bb55 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71 6c 69 74  cleanup:.  sqlit
2bb56 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28  e3SrcListDelete(
2bb57 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f  db, pName);.}../
2bb58 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f  *.** Return a po
2bb59 69 6e 74 65 72 20 74 6f 20 74 68 65 20 54 61 62  inter to the Tab
2bb5a 6c 65 20 73 74 72 75 63 74 75 72 65 20 66 6f 72  le structure for
2bb5b 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20   the table that 
2bb5c 61 20 74 72 69 67 67 65 72 0a 2a 2a 20 69 73 20  a trigger.** is 
2bb5d 73 65 74 20 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  set on..*/.stati
2bb5e 63 20 54 61 62 6c 65 20 2a 74 61 62 6c 65 4f 66  c Table *tableOf
2bb5f 54 72 69 67 67 65 72 28 54 72 69 67 67 65 72 20  Trigger(Trigger 
2bb60 2a 70 54 72 69 67 67 65 72 29 7b 0a 20 20 69 6e  *pTrigger){.  in
2bb61 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72  t n = sqlite3Str
2bb62 6c 65 6e 33 30 28 70 54 72 69 67 67 65 72 2d 3e  len30(pTrigger->
2bb63 74 61 62 6c 65 29 3b 0a 20 20 72 65 74 75 72 6e  table);.  return
2bb64 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64   sqlite3HashFind
2bb65 28 26 70 54 72 69 67 67 65 72 2d 3e 70 54 61 62  (&pTrigger->pTab
2bb66 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c  Schema->tblHash,
2bb67 20 70 54 72 69 67 67 65 72 2d 3e 74 61 62 6c 65   pTrigger->table
2bb68 2c 20 6e 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  , n);.}.../*.** 
2bb69 44 72 6f 70 20 61 20 74 72 69 67 67 65 72 20 67  Drop a trigger g
2bb6a 69 76 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  iven a pointer t
2bb6b 6f 20 74 68 61 74 20 74 72 69 67 67 65 72 2e 20  o that trigger. 
2bb6c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2bb6d 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
2bb6e 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 50 61  ropTriggerPtr(Pa
2bb6f 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 72 69  rse *pParse, Tri
2bb70 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 29 7b  gger *pTrigger){
2bb71 0a 20 20 54 61 62 6c 65 20 20 20 2a 70 54 61 62  .  Table   *pTab
2bb72 6c 65 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20  le;.  Vdbe *v;. 
2bb73 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
2bb74 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74  Parse->db;.  int
2bb75 20 69 44 62 3b 0a 0a 20 20 69 44 62 20 3d 20 73   iDb;..  iDb = s
2bb76 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
2bb77 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  dex(pParse->db, 
2bb78 70 54 72 69 67 67 65 72 2d 3e 70 53 63 68 65 6d  pTrigger->pSchem
2bb79 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44  a);.  assert( iD
2bb7a 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e  b>=0 && iDb<db->
2bb7b 6e 44 62 20 29 3b 0a 20 20 70 54 61 62 6c 65 20  nDb );.  pTable 
2bb7c 3d 20 74 61 62 6c 65 4f 66 54 72 69 67 67 65 72  = tableOfTrigger
2bb7d 28 70 54 72 69 67 67 65 72 29 3b 0a 20 20 61 73  (pTrigger);.  as
2bb7e 73 65 72 74 28 20 70 54 61 62 6c 65 20 29 3b 0a  sert( pTable );.
2bb7f 20 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65    assert( pTable
2bb80 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 72 69 67  ->pSchema==pTrig
2bb81 67 65 72 2d 3e 70 53 63 68 65 6d 61 20 7c 7c 20  ger->pSchema || 
2bb82 69 44 62 3d 3d 31 20 29 3b 0a 23 69 66 6e 64 65  iDb==1 );.#ifnde
2bb83 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
2bb84 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a  THORIZATION.  {.
2bb85 20 20 20 20 69 6e 74 20 63 6f 64 65 20 3d 20 53      int code = S
2bb86 51 4c 49 54 45 5f 44 52 4f 50 5f 54 52 49 47 47  QLITE_DROP_TRIGG
2bb87 45 52 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  ER;.    const ch
2bb88 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44  ar *zDb = db->aD
2bb89 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  b[iDb].zName;.  
2bb8a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
2bb8b 61 62 20 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c  ab = SCHEMA_TABL
2bb8c 45 28 69 44 62 29 3b 0a 20 20 20 20 69 66 28 20  E(iDb);.    if( 
2bb8d 69 44 62 3d 3d 31 20 29 20 63 6f 64 65 20 3d 20  iDb==1 ) code = 
2bb8e 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50  SQLITE_DROP_TEMP
2bb8f 5f 54 52 49 47 47 45 52 3b 0a 20 20 20 20 69 66  _TRIGGER;.    if
2bb90 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65  ( sqlite3AuthChe
2bb91 63 6b 28 70 50 61 72 73 65 2c 20 63 6f 64 65 2c  ck(pParse, code,
2bb92 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65   pTrigger->zName
2bb93 2c 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 2c  , pTable->zName,
2bb94 20 7a 44 62 29 20 7c 7c 0a 20 20 20 20 20 20 73   zDb) ||.      s
2bb95 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
2bb96 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 44  pParse, SQLITE_D
2bb97 45 4c 45 54 45 2c 20 7a 54 61 62 2c 20 30 2c 20  ELETE, zTab, 0, 
2bb98 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20 72 65  zDb) ){.      re
2bb99 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  turn;.    }.  }.
2bb9a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 47 65 6e  #endif..  /* Gen
2bb9b 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 64 65  erate code to de
2bb9c 73 74 72 6f 79 20 74 68 65 20 64 61 74 61 62 61  stroy the databa
2bb9d 73 65 20 72 65 63 6f 72 64 20 6f 66 20 74 68 65  se record of the
2bb9e 20 74 72 69 67 67 65 72 2e 0a 20 20 2a 2f 0a 20   trigger..  */. 
2bb9f 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 21   assert( pTable!
2bba0 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 76 20 3d  =0 );.  if( (v =
2bba1 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
2bba2 70 50 61 72 73 65 29 29 21 3d 30 20 29 7b 0a 20  pParse))!=0 ){. 
2bba3 20 20 20 69 6e 74 20 62 61 73 65 3b 0a 20 20 20     int base;.   
2bba4 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64   static const Vd
2bba5 62 65 4f 70 4c 69 73 74 20 64 72 6f 70 54 72 69  beOpList dropTri
2bba6 67 67 65 72 5b 5d 20 3d 20 7b 0a 20 20 20 20 20  gger[] = {.     
2bba7 20 7b 20 4f 50 5f 52 65 77 69 6e 64 2c 20 20 20   { OP_Rewind,   
2bba8 20 20 30 2c 20 41 44 44 52 28 39 29 2c 20 20 30    0, ADDR(9),  0
2bba9 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 53 74  },.      { OP_St
2bbaa 72 69 6e 67 38 2c 20 20 20 20 30 2c 20 31 2c 20  ring8,    0, 1, 
2bbab 20 20 20 20 20 20 20 30 7d 2c 20 2f 2a 20 31 20         0}, /* 1 
2bbac 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f 43 6f  */.      { OP_Co
2bbad 6c 75 6d 6e 2c 20 20 20 20 20 30 2c 20 31 2c 20  lumn,     0, 1, 
2bbae 20 20 20 20 20 20 20 32 7d 2c 0a 20 20 20 20 20         2},.     
2bbaf 20 7b 20 4f 50 5f 4e 65 2c 20 20 20 20 20 20 20   { OP_Ne,       
2bbb0 20 20 32 2c 20 41 44 44 52 28 38 29 2c 20 20 31    2, ADDR(8),  1
2bbb1 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 53 74  },.      { OP_St
2bbb2 72 69 6e 67 38 2c 20 20 20 20 30 2c 20 31 2c 20  ring8,    0, 1, 
2bbb3 20 20 20 20 20 20 20 30 7d 2c 20 2f 2a 20 34 3a         0}, /* 4:
2bbb4 20 22 74 72 69 67 67 65 72 22 20 2a 2f 0a 20 20   "trigger" */.  
2bbb5 20 20 20 20 7b 20 4f 50 5f 43 6f 6c 75 6d 6e 2c      { OP_Column,
2bbb6 20 20 20 20 20 30 2c 20 30 2c 20 20 20 20 20 20       0, 0,      
2bbb7 20 20 32 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50    2},.      { OP
2bbb8 5f 4e 65 2c 20 20 20 20 20 20 20 20 20 32 2c 20  _Ne,         2, 
2bbb9 41 44 44 52 28 38 29 2c 20 20 31 7d 2c 0a 20 20  ADDR(8),  1},.  
2bbba 20 20 20 20 7b 20 4f 50 5f 44 65 6c 65 74 65 2c      { OP_Delete,
2bbbb 20 20 20 20 20 30 2c 20 30 2c 20 20 20 20 20 20       0, 0,      
2bbbc 20 20 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50    0},.      { OP
2bbbd 5f 4e 65 78 74 2c 20 20 20 20 20 20 20 30 2c 20  _Next,       0, 
2bbbe 41 44 44 52 28 31 29 2c 20 20 30 7d 2c 20 2f 2a  ADDR(1),  0}, /*
2bbbf 20 38 20 2a 2f 0a 20 20 20 20 7d 3b 0a 0a 20 20   8 */.    };..  
2bbc0 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72    sqlite3BeginWr
2bbc1 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61  iteOperation(pPa
2bbc2 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20  rse, 0, iDb);.  
2bbc3 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73    sqlite3OpenMas
2bbc4 74 65 72 54 61 62 6c 65 28 70 50 61 72 73 65 2c  terTable(pParse,
2bbc5 20 69 44 62 29 3b 0a 20 20 20 20 62 61 73 65 20   iDb);.    base 
2bbc6 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
2bbc7 4f 70 4c 69 73 74 28 76 2c 20 20 41 72 72 61 79  OpList(v,  Array
2bbc8 53 69 7a 65 28 64 72 6f 70 54 72 69 67 67 65 72  Size(dropTrigger
2bbc9 29 2c 20 64 72 6f 70 54 72 69 67 67 65 72 29 3b  ), dropTrigger);
2bbca 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2bbcb 43 68 61 6e 67 65 50 34 28 76 2c 20 62 61 73 65  ChangeP4(v, base
2bbcc 2b 31 2c 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e  +1, pTrigger->zN
2bbcd 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  ame, 0);.    sql
2bbce 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34  ite3VdbeChangeP4
2bbcf 28 76 2c 20 62 61 73 65 2b 34 2c 20 22 74 72 69  (v, base+4, "tri
2bbd0 67 67 65 72 22 2c 20 50 34 5f 53 54 41 54 49 43  gger", P4_STATIC
2bbd1 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68  );.    sqlite3Ch
2bbd2 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73  angeCookie(pPars
2bbd3 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c  e, iDb);.    sql
2bbd4 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2bbd5 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 30 2c 20 30  , OP_Close, 0, 0
2bbd6 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2bbd7 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 44  beAddOp4(v, OP_D
2bbd8 72 6f 70 54 72 69 67 67 65 72 2c 20 69 44 62 2c  ropTrigger, iDb,
2bbd9 20 30 2c 20 30 2c 20 70 54 72 69 67 67 65 72 2d   0, 0, pTrigger-
2bbda 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20  >zName, 0);.    
2bbdb 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  if( pParse->nMem
2bbdc 3c 33 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72  <3 ){.      pPar
2bbdd 73 65 2d 3e 6e 4d 65 6d 20 3d 20 33 3b 0a 20 20  se->nMem = 3;.  
2bbde 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
2bbdf 20 52 65 6d 6f 76 65 20 61 20 74 72 69 67 67 65   Remove a trigge
2bbe0 72 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20  r from the hash 
2bbe1 74 61 62 6c 65 73 20 6f 66 20 74 68 65 20 73 71  tables of the sq
2bbe2 6c 69 74 65 2a 20 70 6f 69 6e 74 65 72 2e 0a 2a  lite* pointer..*
2bbe3 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2bbe4 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 6c   void sqlite3Unl
2bbe5 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 72 69 67  inkAndDeleteTrig
2bbe6 67 65 72 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ger(sqlite3 *db,
2bbe7 20 69 6e 74 20 69 44 62 2c 20 63 6f 6e 73 74 20   int iDb, const 
2bbe8 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20  char *zName){.  
2bbe9 48 61 73 68 20 2a 70 48 61 73 68 20 3d 20 26 28  Hash *pHash = &(
2bbea 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63  db->aDb[iDb].pSc
2bbeb 68 65 6d 61 2d 3e 74 72 69 67 48 61 73 68 29 3b  hema->trigHash);
2bbec 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69  .  Trigger *pTri
2bbed 67 67 65 72 3b 0a 20 20 70 54 72 69 67 67 65 72  gger;.  pTrigger
2bbee 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e   = sqlite3HashIn
2bbef 73 65 72 74 28 70 48 61 73 68 2c 20 7a 4e 61 6d  sert(pHash, zNam
2bbf0 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  e, sqlite3Strlen
2bbf1 33 30 28 7a 4e 61 6d 65 29 2c 20 30 29 3b 0a 20  30(zName), 0);. 
2bbf2 20 69 66 28 20 41 4c 57 41 59 53 28 70 54 72 69   if( ALWAYS(pTri
2bbf3 67 67 65 72 29 20 29 7b 0a 20 20 20 20 69 66 28  gger) ){.    if(
2bbf4 20 70 54 72 69 67 67 65 72 2d 3e 70 53 63 68 65   pTrigger->pSche
2bbf5 6d 61 3d 3d 70 54 72 69 67 67 65 72 2d 3e 70 54  ma==pTrigger->pT
2bbf6 61 62 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20  abSchema ){.    
2bbf7 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
2bbf8 74 61 62 6c 65 4f 66 54 72 69 67 67 65 72 28 70  tableOfTrigger(p
2bbf9 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20 20 20  Trigger);.      
2bbfa 54 72 69 67 67 65 72 20 2a 2a 70 70 3b 0a 20 20  Trigger **pp;.  
2bbfb 20 20 20 20 66 6f 72 28 70 70 3d 26 70 54 61 62      for(pp=&pTab
2bbfc 2d 3e 70 54 72 69 67 67 65 72 3b 20 2a 70 70 21  ->pTrigger; *pp!
2bbfd 3d 70 54 72 69 67 67 65 72 3b 20 70 70 3d 26 28  =pTrigger; pp=&(
2bbfe 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 29 3b 0a  (*pp)->pNext));.
2bbff 20 20 20 20 20 20 2a 70 70 20 3d 20 28 2a 70 70        *pp = (*pp
2bc00 29 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a  )->pNext;.    }.
2bc01 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74      sqlite3Delet
2bc02 65 54 72 69 67 67 65 72 28 64 62 2c 20 70 54 72  eTrigger(db, pTr
2bc03 69 67 67 65 72 29 3b 0a 20 20 20 20 64 62 2d 3e  igger);.    db->
2bc04 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f  flags |= SQLITE_
2bc05 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 20  InternChanges;. 
2bc06 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 4c 69   }.}../*.** pELi
2bc07 73 74 20 69 73 20 74 68 65 20 53 45 54 20 63 6c  st is the SET cl
2bc08 61 75 73 65 20 6f 66 20 61 6e 20 55 50 44 41 54  ause of an UPDAT
2bc09 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 45 61  E statement.  Ea
2bc0a 63 68 20 65 6e 74 72 79 0a 2a 2a 20 69 6e 20 70  ch entry.** in p
2bc0b 45 4c 69 73 74 20 69 73 20 6f 66 20 74 68 65 20  EList is of the 
2bc0c 66 6f 72 6d 61 74 20 3c 69 64 3e 3d 3c 65 78 70  format <id>=<exp
2bc0d 72 3e 2e 20 20 49 66 20 61 6e 79 20 6f 66 20 74  r>.  If any of t
2bc0e 68 65 20 65 6e 74 72 69 65 73 0a 2a 2a 20 69 6e  he entries.** in
2bc0f 20 70 45 4c 69 73 74 20 68 61 76 65 20 61 6e 20   pEList have an 
2bc10 3c 69 64 3e 20 77 68 69 63 68 20 6d 61 74 63 68  <id> which match
2bc11 65 73 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72  es an identifier
2bc12 20 69 6e 20 70 49 64 4c 69 73 74 2c 0a 2a 2a 20   in pIdList,.** 
2bc13 74 68 65 6e 20 72 65 74 75 72 6e 20 54 52 55 45  then return TRUE
2bc14 2e 20 20 49 66 20 70 49 64 4c 69 73 74 3d 3d 4e  .  If pIdList==N
2bc15 55 4c 4c 2c 20 74 68 65 6e 20 69 74 20 69 73 20  ULL, then it is 
2bc16 63 6f 6e 73 69 64 65 72 65 64 20 61 0a 2a 2a 20  considered a.** 
2bc17 77 69 6c 64 63 61 72 64 20 74 68 61 74 20 6d 61  wildcard that ma
2bc18 74 63 68 65 73 20 61 6e 79 74 68 69 6e 67 2e 20  tches anything. 
2bc19 20 4c 69 6b 65 77 69 73 65 20 69 66 20 70 45 4c   Likewise if pEL
2bc1a 69 73 74 3d 3d 4e 55 4c 4c 20 74 68 65 6e 0a 2a  ist==NULL then.*
2bc1b 2a 20 69 74 20 6d 61 74 63 68 65 73 20 61 6e 79  * it matches any
2bc1c 74 68 69 6e 67 20 73 6f 20 61 6c 77 61 79 73 20  thing so always 
2bc1d 72 65 74 75 72 6e 20 74 72 75 65 2e 20 20 52 65  return true.  Re
2bc1e 74 75 72 6e 20 66 61 6c 73 65 20 6f 6e 6c 79 0a  turn false only.
2bc1f 2a 2a 20 69 66 20 74 68 65 72 65 20 69 73 20 6e  ** if there is n
2bc20 6f 20 6d 61 74 63 68 2e 0a 2a 2f 0a 73 74 61 74  o match..*/.stat
2bc21 69 63 20 69 6e 74 20 63 68 65 63 6b 43 6f 6c 75  ic int checkColu
2bc22 6d 6e 4f 76 65 72 6c 61 70 28 49 64 4c 69 73 74  mnOverlap(IdList
2bc23 20 2a 70 49 64 4c 69 73 74 2c 20 45 78 70 72 4c   *pIdList, ExprL
2bc24 69 73 74 20 2a 70 45 4c 69 73 74 29 7b 0a 20 20  ist *pEList){.  
2bc25 69 6e 74 20 65 3b 0a 20 20 69 66 28 20 70 49 64  int e;.  if( pId
2bc26 4c 69 73 74 3d 3d 30 20 7c 7c 20 4e 45 56 45 52  List==0 || NEVER
2bc27 28 70 45 4c 69 73 74 3d 3d 30 29 20 29 20 72 65  (pEList==0) ) re
2bc28 74 75 72 6e 20 31 3b 0a 20 20 66 6f 72 28 65 3d  turn 1;.  for(e=
2bc29 30 3b 20 65 3c 70 45 4c 69 73 74 2d 3e 6e 45 78  0; e<pEList->nEx
2bc2a 70 72 3b 20 65 2b 2b 29 7b 0a 20 20 20 20 69 66  pr; e++){.    if
2bc2b 28 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 49  ( sqlite3IdListI
2bc2c 6e 64 65 78 28 70 49 64 4c 69 73 74 2c 20 70 45  ndex(pIdList, pE
2bc2d 4c 69 73 74 2d 3e 61 5b 65 5d 2e 7a 4e 61 6d 65  List->a[e].zName
2bc2e 29 3e 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b  )>=0 ) return 1;
2bc2f 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
2bc30 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72   .}../*.** Retur
2bc31 6e 20 61 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20  n a list of all 
2bc32 74 72 69 67 67 65 72 73 20 6f 6e 20 74 61 62 6c  triggers on tabl
2bc33 65 20 70 54 61 62 20 69 66 20 74 68 65 72 65 20  e pTab if there 
2bc34 65 78 69 73 74 73 20 61 74 20 6c 65 61 73 74 0a  exists at least.
2bc35 2a 2a 20 6f 6e 65 20 74 72 69 67 67 65 72 20 74  ** one trigger t
2bc36 68 61 74 20 6d 75 73 74 20 62 65 20 66 69 72 65  hat must be fire
2bc37 64 20 77 68 65 6e 20 61 6e 20 6f 70 65 72 61 74  d when an operat
2bc38 69 6f 6e 20 6f 66 20 74 79 70 65 20 27 6f 70 27  ion of type 'op'
2bc39 20 69 73 20 0a 2a 2a 20 70 65 72 66 6f 72 6d 65   is .** performe
2bc3a 64 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 2c 20  d on the table, 
2bc3b 61 6e 64 2c 20 69 66 20 74 68 61 74 20 6f 70 65  and, if that ope
2bc3c 72 61 74 69 6f 6e 20 69 73 20 61 6e 20 55 50 44  ration is an UPD
2bc3d 41 54 45 2c 20 69 66 20 61 74 0a 2a 2a 20 6c 65  ATE, if at.** le
2bc3e 61 73 74 20 6f 6e 65 20 6f 66 20 74 68 65 20 63  ast one of the c
2bc3f 6f 6c 75 6d 6e 73 20 69 6e 20 70 43 68 61 6e 67  olumns in pChang
2bc40 65 73 20 69 73 20 62 65 69 6e 67 20 6d 6f 64 69  es is being modi
2bc41 66 69 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  fied..*/.SQLITE_
2bc42 50 52 49 56 41 54 45 20 54 72 69 67 67 65 72 20  PRIVATE Trigger 
2bc43 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 73  *sqlite3Triggers
2bc44 45 78 69 73 74 28 0a 20 20 50 61 72 73 65 20 2a  Exist(.  Parse *
2bc45 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
2bc46 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78   /* Parse contex
2bc47 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  t */.  Table *pT
2bc48 61 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  ab,            /
2bc49 2a 20 54 68 65 20 74 61 62 6c 65 20 74 68 65 20  * The table the 
2bc4a 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 72 69  contains the tri
2bc4b 67 67 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6f  ggers */.  int o
2bc4c 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
2bc4d 20 20 20 2f 2a 20 6f 6e 65 20 6f 66 20 54 4b 5f     /* one of TK_
2bc4e 44 45 4c 45 54 45 2c 20 54 4b 5f 49 4e 53 45 52  DELETE, TK_INSER
2bc4f 54 2c 20 54 4b 5f 55 50 44 41 54 45 20 2a 2f 0a  T, TK_UPDATE */.
2bc50 20 20 45 78 70 72 4c 69 73 74 20 2a 70 43 68 61    ExprList *pCha
2bc51 6e 67 65 73 2c 20 20 20 20 20 2f 2a 20 43 6f 6c  nges,     /* Col
2bc52 75 6d 6e 73 20 74 68 61 74 20 63 68 61 6e 67 65  umns that change
2bc53 20 69 6e 20 61 6e 20 55 50 44 41 54 45 20 73 74   in an UPDATE st
2bc54 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74  atement */.  int
2bc55 20 2a 70 4d 61 73 6b 20 20 20 20 20 20 20 20 20   *pMask         
2bc56 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4d 61 73       /* OUT: Mas
2bc57 6b 20 6f 66 20 54 52 49 47 47 45 52 5f 42 45 46  k of TRIGGER_BEF
2bc58 4f 52 45 7c 54 52 49 47 47 45 52 5f 41 46 54 45  ORE|TRIGGER_AFTE
2bc59 52 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6d 61  R */.){.  int ma
2bc5a 73 6b 20 3d 20 30 3b 0a 20 20 54 72 69 67 67 65  sk = 0;.  Trigge
2bc5b 72 20 2a 70 4c 69 73 74 20 3d 20 73 71 6c 69 74  r *pList = sqlit
2bc5c 65 33 54 72 69 67 67 65 72 4c 69 73 74 28 70 50  e3TriggerList(pP
2bc5d 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 54  arse, pTab);.  T
2bc5e 72 69 67 67 65 72 20 2a 70 3b 0a 20 20 61 73 73  rigger *p;.  ass
2bc5f 65 72 74 28 20 70 4c 69 73 74 3d 3d 30 20 7c 7c  ert( pList==0 ||
2bc60 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
2bc61 3d 3d 30 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70  ==0 );.  for(p=p
2bc62 4c 69 73 74 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e  List; p; p=p->pN
2bc63 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 2d  ext){.    if( p-
2bc64 3e 6f 70 3d 3d 6f 70 20 26 26 20 63 68 65 63 6b  >op==op && check
2bc65 43 6f 6c 75 6d 6e 4f 76 65 72 6c 61 70 28 70 2d  ColumnOverlap(p-
2bc66 3e 70 43 6f 6c 75 6d 6e 73 2c 20 70 43 68 61 6e  >pColumns, pChan
2bc67 67 65 73 29 20 29 7b 0a 20 20 20 20 20 20 6d 61  ges) ){.      ma
2bc68 73 6b 20 7c 3d 20 70 2d 3e 74 72 5f 74 6d 3b 0a  sk |= p->tr_tm;.
2bc69 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
2bc6a 70 4d 61 73 6b 20 29 7b 0a 20 20 20 20 2a 70 4d  pMask ){.    *pM
2bc6b 61 73 6b 20 3d 20 6d 61 73 6b 3b 0a 20 20 7d 0a  ask = mask;.  }.
2bc6c 20 20 72 65 74 75 72 6e 20 28 6d 61 73 6b 20 3f    return (mask ?
2bc6d 20 70 4c 69 73 74 20 3a 20 30 29 3b 0a 7d 0a 0a   pList : 0);.}..
2bc6e 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68  /*.** Convert th
2bc6f 65 20 70 53 74 65 70 2d 3e 74 61 72 67 65 74 20  e pStep->target 
2bc70 74 6f 6b 65 6e 20 69 6e 74 6f 20 61 20 53 72 63  token into a Src
2bc71 4c 69 73 74 20 61 6e 64 20 72 65 74 75 72 6e 20  List and return 
2bc72 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20  a pointer.** to 
2bc73 74 68 61 74 20 53 72 63 4c 69 73 74 2e 0a 2a 2a  that SrcList..**
2bc74 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
2bc75 20 61 64 64 73 20 61 20 73 70 65 63 69 66 69 63   adds a specific
2bc76 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 2c 20   database name, 
2bc77 69 66 20 6e 65 65 64 65 64 2c 20 74 6f 20 74 68  if needed, to th
2bc78 65 20 74 61 72 67 65 74 20 77 68 65 6e 0a 2a 2a  e target when.**
2bc79 20 66 6f 72 6d 69 6e 67 20 74 68 65 20 53 72 63   forming the Src
2bc7a 4c 69 73 74 2e 20 20 54 68 69 73 20 70 72 65 76  List.  This prev
2bc7b 65 6e 74 73 20 61 20 74 72 69 67 67 65 72 20 69  ents a trigger i
2bc7c 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 66  n one database f
2bc7d 72 6f 6d 0a 2a 2a 20 72 65 66 65 72 72 69 6e 67  rom.** referring
2bc7e 20 74 6f 20 61 20 74 61 72 67 65 74 20 69 6e 20   to a target in 
2bc7f 61 6e 6f 74 68 65 72 20 64 61 74 61 62 61 73 65  another database
2bc80 2e 20 20 41 6e 20 65 78 63 65 70 74 69 6f 6e 20  .  An exception 
2bc81 69 73 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 74  is when the.** t
2bc82 72 69 67 67 65 72 20 69 73 20 69 6e 20 54 45 4d  rigger is in TEM
2bc83 50 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20  P in which case 
2bc84 69 74 20 63 61 6e 20 72 65 66 65 72 20 74 6f 20  it can refer to 
2bc85 61 6e 79 20 6f 74 68 65 72 20 64 61 74 61 62 61  any other databa
2bc86 73 65 20 69 74 0a 2a 2a 20 77 61 6e 74 73 2e 0a  se it.** wants..
2bc87 2a 2f 0a 73 74 61 74 69 63 20 53 72 63 4c 69 73  */.static SrcLis
2bc88 74 20 2a 74 61 72 67 65 74 53 72 63 4c 69 73 74  t *targetSrcList
2bc89 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2bc8a 65 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  e,       /* The 
2bc8b 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  parsing context 
2bc8c 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 65 70  */.  TriggerStep
2bc8d 20 2a 70 53 74 65 70 20 20 20 2f 2a 20 54 68 65   *pStep   /* The
2bc8e 20 74 72 69 67 67 65 72 20 63 6f 6e 74 61 69 6e   trigger contain
2bc8f 69 6e 67 20 74 68 65 20 74 61 72 67 65 74 20 74  ing the target t
2bc90 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  oken */.){.  int
2bc91 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20   iDb;           
2bc92 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68    /* Index of th
2bc93 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 75 73  e database to us
2bc94 65 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a  e */.  SrcList *
2bc95 70 53 72 63 3b 20 20 20 20 20 20 20 2f 2a 20 53  pSrc;       /* S
2bc96 72 63 4c 69 73 74 20 74 6f 20 62 65 20 72 65 74  rcList to be ret
2bc97 75 72 6e 65 64 20 2a 2f 0a 0a 20 20 70 53 72 63  urned */..  pSrc
2bc98 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73   = sqlite3SrcLis
2bc99 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2d 3e  tAppend(pParse->
2bc9a 64 62 2c 20 30 2c 20 26 70 53 74 65 70 2d 3e 74  db, 0, &pStep->t
2bc9b 61 72 67 65 74 2c 20 30 29 3b 0a 20 20 69 66 28  arget, 0);.  if(
2bc9c 20 70 53 72 63 20 29 7b 0a 20 20 20 20 61 73 73   pSrc ){.    ass
2bc9d 65 72 74 28 20 70 53 72 63 2d 3e 6e 53 72 63 3e  ert( pSrc->nSrc>
2bc9e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
2bc9f 20 70 53 72 63 2d 3e 61 21 3d 30 20 29 3b 0a 20   pSrc->a!=0 );. 
2bca0 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33     iDb = sqlite3
2bca1 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50  SchemaToIndex(pP
2bca2 61 72 73 65 2d 3e 64 62 2c 20 70 53 74 65 70 2d  arse->db, pStep-
2bca3 3e 70 54 72 69 67 2d 3e 70 53 63 68 65 6d 61 29  >pTrig->pSchema)
2bca4 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3d 3d 30  ;.    if( iDb==0
2bca5 20 7c 7c 20 69 44 62 3e 3d 32 20 29 7b 0a 20 20   || iDb>=2 ){.  
2bca6 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20      sqlite3 *db 
2bca7 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
2bca8 20 20 20 20 61 73 73 65 72 74 28 20 69 44 62 3c      assert( iDb<
2bca9 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6e 44 62 20  pParse->db->nDb 
2bcaa 29 3b 0a 20 20 20 20 20 20 70 53 72 63 2d 3e 61  );.      pSrc->a
2bcab 5b 70 53 72 63 2d 3e 6e 53 72 63 2d 31 5d 2e 7a  [pSrc->nSrc-1].z
2bcac 44 61 74 61 62 61 73 65 20 3d 20 73 71 6c 69 74  Database = sqlit
2bcad 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 64  e3DbStrDup(db, d
2bcae 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
2bcaf 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  e);.    }.  }.  
2bcb0 72 65 74 75 72 6e 20 70 53 72 63 3b 0a 7d 0a 0a  return pSrc;.}..
2bcb1 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 56  /*.** Generate V
2bcb2 44 42 45 20 63 6f 64 65 20 66 6f 72 20 74 68 65  DBE code for the
2bcb3 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 73 69   statements insi
2bcb4 64 65 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61  de the body of a
2bcb5 20 73 69 6e 67 6c 65 20 0a 2a 2a 20 74 72 69 67   single .** trig
2bcb6 67 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ger..*/.static i
2bcb7 6e 74 20 63 6f 64 65 54 72 69 67 67 65 72 50 72  nt codeTriggerPr
2bcb8 6f 67 72 61 6d 28 0a 20 20 50 61 72 73 65 20 2a  ogram(.  Parse *
2bcb9 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
2bcba 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72     /* The parser
2bcbb 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 72   context */.  Tr
2bcbc 69 67 67 65 72 53 74 65 70 20 2a 70 53 74 65 70  iggerStep *pStep
2bcbd 4c 69 73 74 2c 20 20 20 2f 2a 20 4c 69 73 74 20  List,   /* List 
2bcbe 6f 66 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e  of statements in
2bcbf 73 69 64 65 20 74 68 65 20 74 72 69 67 67 65 72  side the trigger
2bcc0 20 62 6f 64 79 20 2a 2f 0a 20 20 69 6e 74 20 6f   body */.  int o
2bcc1 72 63 6f 6e 66 20 20 20 20 20 20 20 20 20 20 20  rconf           
2bcc2 20 20 20 20 20 2f 2a 20 43 6f 6e 66 6c 69 63 74       /* Conflict
2bcc3 20 61 6c 67 6f 72 69 74 68 6d 2e 20 28 4f 45 5f   algorithm. (OE_
2bcc4 41 62 6f 72 74 2c 20 65 74 63 29 20 2a 2f 20 20  Abort, etc) */  
2bcc5 0a 29 7b 0a 20 20 54 72 69 67 67 65 72 53 74 65  .){.  TriggerSte
2bcc6 70 20 2a 70 53 74 65 70 3b 0a 20 20 56 64 62 65  p *pStep;.  Vdbe
2bcc7 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
2bcc8 64 62 65 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  dbe;.  sqlite3 *
2bcc9 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2bcca 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72  ..  assert( pPar
2bccb 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 20  se->pTriggerTab 
2bccc 26 26 20 70 50 61 72 73 65 2d 3e 70 54 6f 70 6c  && pParse->pTopl
2bccd 65 76 65 6c 20 29 3b 0a 20 20 61 73 73 65 72 74  evel );.  assert
2bcce 28 20 70 53 74 65 70 4c 69 73 74 20 29 3b 0a 20  ( pStepList );. 
2bccf 20 61 73 73 65 72 74 28 20 76 21 3d 30 20 29 3b   assert( v!=0 );
2bcd0 0a 20 20 66 6f 72 28 70 53 74 65 70 3d 70 53 74  .  for(pStep=pSt
2bcd1 65 70 4c 69 73 74 3b 20 70 53 74 65 70 3b 20 70  epList; pStep; p
2bcd2 53 74 65 70 3d 70 53 74 65 70 2d 3e 70 4e 65 78  Step=pStep->pNex
2bcd3 74 29 7b 0a 20 20 20 20 2f 2a 20 46 69 67 75 72  t){.    /* Figur
2bcd4 65 20 6f 75 74 20 74 68 65 20 4f 4e 20 43 4f 4e  e out the ON CON
2bcd5 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 74 68 61  FLICT policy tha
2bcd6 74 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 66  t will be used f
2bcd7 6f 72 20 74 68 69 73 20 73 74 65 70 0a 20 20 20  or this step.   
2bcd8 20 2a 2a 20 6f 66 20 74 68 65 20 74 72 69 67 67   ** of the trigg
2bcd9 65 72 20 70 72 6f 67 72 61 6d 2e 20 49 66 20 74  er program. If t
2bcda 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61  he statement tha
2bcdb 74 20 63 61 75 73 65 64 20 74 68 69 73 20 74 72  t caused this tr
2bcdc 69 67 67 65 72 0a 20 20 20 20 2a 2a 20 74 6f 20  igger.    ** to 
2bcdd 66 69 72 65 20 68 61 64 20 61 6e 20 65 78 70 6c  fire had an expl
2bcde 69 63 69 74 20 4f 4e 20 43 4f 4e 46 4c 49 43 54  icit ON CONFLICT
2bcdf 2c 20 74 68 65 6e 20 75 73 65 20 69 74 2e 20 4f  , then use it. O
2bce0 74 68 65 72 77 69 73 65 2c 20 75 73 65 0a 20 20  therwise, use.  
2bce1 20 20 2a 2a 20 74 68 65 20 4f 4e 20 43 4f 4e 46    ** the ON CONF
2bce2 4c 49 43 54 20 70 6f 6c 69 63 79 20 74 68 61 74  LICT policy that
2bce3 20 77 61 73 20 73 70 65 63 69 66 69 65 64 20 61   was specified a
2bce4 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 74 72  s part of the tr
2bce5 69 67 67 65 72 0a 20 20 20 20 2a 2a 20 73 74 65  igger.    ** ste
2bce6 70 20 73 74 61 74 65 6d 65 6e 74 2e 20 45 78 61  p statement. Exa
2bce7 6d 70 6c 65 3a 0a 20 20 20 20 2a 2a 0a 20 20 20  mple:.    **.   
2bce8 20 2a 2a 20 20 20 43 52 45 41 54 45 20 54 52 49   **   CREATE TRI
2bce9 47 47 45 52 20 41 46 54 45 52 20 49 4e 53 45 52  GGER AFTER INSER
2bcea 54 20 4f 4e 20 74 31 20 42 45 47 49 4e 3b 0a 20  T ON t1 BEGIN;. 
2bceb 20 20 20 2a 2a 20 20 20 20 20 49 4e 53 45 52 54     **     INSERT
2bcec 20 4f 52 20 52 45 50 4c 41 43 45 20 49 4e 54 4f   OR REPLACE INTO
2bced 20 74 32 20 56 41 4c 55 45 53 28 6e 65 77 2e 61   t2 VALUES(new.a
2bcee 2c 20 6e 65 77 2e 62 29 3b 0a 20 20 20 20 2a 2a  , new.b);.    **
2bcef 20 20 20 45 4e 44 3b 0a 20 20 20 20 2a 2a 0a 20     END;.    **. 
2bcf0 20 20 20 2a 2a 20 20 20 49 4e 53 45 52 54 20 49     **   INSERT I
2bcf1 4e 54 4f 20 74 31 20 2e 2e 2e 20 3b 20 20 20 20  NTO t1 ... ;    
2bcf2 20 20 20 20 20 20 20 20 2d 2d 20 69 6e 73 65 72          -- inser
2bcf3 74 20 69 6e 74 6f 20 74 32 20 75 73 65 73 20 52  t into t2 uses R
2bcf4 45 50 4c 41 43 45 20 70 6f 6c 69 63 79 0a 20 20  EPLACE policy.  
2bcf5 20 20 2a 2a 20 20 20 49 4e 53 45 52 54 20 4f 52    **   INSERT OR
2bcf6 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 74 31 20   IGNORE INTO t1 
2bcf7 2e 2e 2e 20 3b 20 20 2d 2d 20 69 6e 73 65 72 74  ... ;  -- insert
2bcf8 20 69 6e 74 6f 20 74 32 20 75 73 65 73 20 49 47   into t2 uses IG
2bcf9 4e 4f 52 45 20 70 6f 6c 69 63 79 0a 20 20 20 20  NORE policy.    
2bcfa 2a 2f 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 65  */.    pParse->e
2bcfb 4f 72 63 6f 6e 66 20 3d 20 28 6f 72 63 6f 6e 66  Orconf = (orconf
2bcfc 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 29 3f 70 53  ==OE_Default)?pS
2bcfd 74 65 70 2d 3e 6f 72 63 6f 6e 66 3a 28 75 38 29  tep->orconf:(u8)
2bcfe 6f 72 63 6f 6e 66 3b 0a 0a 20 20 20 20 73 77 69  orconf;..    swi
2bcff 74 63 68 28 20 70 53 74 65 70 2d 3e 6f 70 20 29  tch( pStep->op )
2bd00 7b 0a 20 20 20 20 20 20 63 61 73 65 20 54 4b 5f  {.      case TK_
2bd01 55 50 44 41 54 45 3a 20 7b 0a 20 20 20 20 20 20  UPDATE: {.      
2bd02 20 20 73 71 6c 69 74 65 33 55 70 64 61 74 65 28    sqlite3Update(
2bd03 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20  pParse, .       
2bd04 20 20 20 74 61 72 67 65 74 53 72 63 4c 69 73 74     targetSrcList
2bd05 28 70 50 61 72 73 65 2c 20 70 53 74 65 70 29 2c  (pParse, pStep),
2bd06 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2bd07 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62  e3ExprListDup(db
2bd08 2c 20 70 53 74 65 70 2d 3e 70 45 78 70 72 4c 69  , pStep->pExprLi
2bd09 73 74 2c 20 30 29 2c 20 0a 20 20 20 20 20 20 20  st, 0), .       
2bd0a 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 75     sqlite3ExprDu
2bd0b 70 28 64 62 2c 20 70 53 74 65 70 2d 3e 70 57 68  p(db, pStep->pWh
2bd0c 65 72 65 2c 20 30 29 2c 20 0a 20 20 20 20 20 20  ere, 0), .      
2bd0d 20 20 20 20 70 50 61 72 73 65 2d 3e 65 4f 72 63      pParse->eOrc
2bd0e 6f 6e 66 0a 20 20 20 20 20 20 20 20 29 3b 0a 20  onf.        );. 
2bd0f 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2bd10 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65      }.      case
2bd11 20 54 4b 5f 49 4e 53 45 52 54 3a 20 7b 0a 20 20   TK_INSERT: {.  
2bd12 20 20 20 20 20 20 73 71 6c 69 74 65 33 49 6e 73        sqlite3Ins
2bd13 65 72 74 28 70 50 61 72 73 65 2c 20 0a 20 20 20  ert(pParse, .   
2bd14 20 20 20 20 20 20 20 74 61 72 67 65 74 53 72 63         targetSrc
2bd15 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 53 74  List(pParse, pSt
2bd16 65 70 29 2c 0a 20 20 20 20 20 20 20 20 20 20 73  ep),.          s
2bd17 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75  qlite3ExprListDu
2bd18 70 28 64 62 2c 20 70 53 74 65 70 2d 3e 70 45 78  p(db, pStep->pEx
2bd19 70 72 4c 69 73 74 2c 20 30 29 2c 20 0a 20 20 20  prList, 0), .   
2bd1a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65         sqlite3Se
2bd1b 6c 65 63 74 44 75 70 28 64 62 2c 20 70 53 74 65  lectDup(db, pSte
2bd1c 70 2d 3e 70 53 65 6c 65 63 74 2c 20 30 29 2c 20  p->pSelect, 0), 
2bd1d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2bd1e 65 33 49 64 4c 69 73 74 44 75 70 28 64 62 2c 20  e3IdListDup(db, 
2bd1f 70 53 74 65 70 2d 3e 70 49 64 4c 69 73 74 29 2c  pStep->pIdList),
2bd20 20 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 72   .          pPar
2bd21 73 65 2d 3e 65 4f 72 63 6f 6e 66 0a 20 20 20 20  se->eOrconf.    
2bd22 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 62      );.        b
2bd23 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
2bd24 20 20 20 20 63 61 73 65 20 54 4b 5f 44 45 4c 45      case TK_DELE
2bd25 54 45 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71  TE: {.        sq
2bd26 6c 69 74 65 33 44 65 6c 65 74 65 46 72 6f 6d 28  lite3DeleteFrom(
2bd27 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20  pParse, .       
2bd28 20 20 20 74 61 72 67 65 74 53 72 63 4c 69 73 74     targetSrcList
2bd29 28 70 50 61 72 73 65 2c 20 70 53 74 65 70 29 2c  (pParse, pStep),
2bd2a 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2bd2b 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 53  e3ExprDup(db, pS
2bd2c 74 65 70 2d 3e 70 57 68 65 72 65 2c 20 30 29 0a  tep->pWhere, 0).
2bd2d 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20          );.     
2bd2e 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2bd2f 7d 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a  }.      default:
2bd30 20 61 73 73 65 72 74 28 20 70 53 74 65 70 2d 3e   assert( pStep->
2bd31 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 29 3b  op==TK_SELECT );
2bd32 20 7b 0a 20 20 20 20 20 20 20 20 53 65 6c 65 63   {.        Selec
2bd33 74 44 65 73 74 20 73 44 65 73 74 3b 0a 20 20 20  tDest sDest;.   
2bd34 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 65       Select *pSe
2bd35 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65  lect = sqlite3Se
2bd36 6c 65 63 74 44 75 70 28 64 62 2c 20 70 53 74 65  lectDup(db, pSte
2bd37 70 2d 3e 70 53 65 6c 65 63 74 2c 20 30 29 3b 0a  p->pSelect, 0);.
2bd38 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
2bd39 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26 73  electDestInit(&s
2bd3a 44 65 73 74 2c 20 53 52 54 5f 44 69 73 63 61 72  Dest, SRT_Discar
2bd3b 64 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73  d, 0);.        s
2bd3c 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61  qlite3Select(pPa
2bd3d 72 73 65 2c 20 70 53 65 6c 65 63 74 2c 20 26 73  rse, pSelect, &s
2bd3e 44 65 73 74 29 3b 0a 20 20 20 20 20 20 20 20 73  Dest);.        s
2bd3f 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
2bd40 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b  te(db, pSelect);
2bd41 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2bd42 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 0a 20        }.    } . 
2bd43 20 20 20 69 66 28 20 70 53 74 65 70 2d 3e 6f 70     if( pStep->op
2bd44 21 3d 54 4b 5f 53 45 4c 45 43 54 20 29 7b 0a 20  !=TK_SELECT ){. 
2bd45 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2bd46 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 52 65 73  AddOp0(v, OP_Res
2bd47 65 74 43 6f 75 6e 74 29 3b 0a 20 20 20 20 7d 0a  etCount);.    }.
2bd48 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 30 3b    }..  return 0;
2bd49 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .}..#ifdef SQLIT
2bd4a 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68  E_DEBUG./*.** Th
2bd4b 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75  is function is u
2bd4c 73 65 64 20 74 6f 20 61 64 64 20 56 64 62 65 43  sed to add VdbeC
2bd4d 6f 6d 6d 65 6e 74 28 29 20 61 6e 6e 6f 74 61 74  omment() annotat
2bd4e 69 6f 6e 73 20 74 6f 20 61 20 56 44 42 45 0a 2a  ions to a VDBE.*
2bd4f 2a 20 70 72 6f 67 72 61 6d 2e 20 49 74 20 69 73  * program. It is
2bd50 20 6e 6f 74 20 75 73 65 64 20 69 6e 20 70 72 6f   not used in pro
2bd51 64 75 63 74 69 6f 6e 20 63 6f 64 65 2c 20 6f 6e  duction code, on
2bd52 6c 79 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67  ly for debugging
2bd53 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  ..*/.static cons
2bd54 74 20 63 68 61 72 20 2a 6f 6e 45 72 72 6f 72 54  t char *onErrorT
2bd55 65 78 74 28 69 6e 74 20 6f 6e 45 72 72 6f 72 29  ext(int onError)
2bd56 7b 0a 20 20 73 77 69 74 63 68 28 20 6f 6e 45 72  {.  switch( onEr
2bd57 72 6f 72 20 29 7b 0a 20 20 20 20 63 61 73 65 20  ror ){.    case 
2bd58 4f 45 5f 41 62 6f 72 74 3a 20 20 20 20 72 65 74  OE_Abort:    ret
2bd59 75 72 6e 20 22 61 62 6f 72 74 22 3b 0a 20 20 20  urn "abort";.   
2bd5a 20 63 61 73 65 20 4f 45 5f 52 6f 6c 6c 62 61 63   case OE_Rollbac
2bd5b 6b 3a 20 72 65 74 75 72 6e 20 22 72 6f 6c 6c 62  k: return "rollb
2bd5c 61 63 6b 22 3b 0a 20 20 20 20 63 61 73 65 20 4f  ack";.    case O
2bd5d 45 5f 46 61 69 6c 3a 20 20 20 20 20 72 65 74 75  E_Fail:     retu
2bd5e 72 6e 20 22 66 61 69 6c 22 3b 0a 20 20 20 20 63  rn "fail";.    c
2bd5f 61 73 65 20 4f 45 5f 52 65 70 6c 61 63 65 3a 20  ase OE_Replace: 
2bd60 20 72 65 74 75 72 6e 20 22 72 65 70 6c 61 63 65   return "replace
2bd61 22 3b 0a 20 20 20 20 63 61 73 65 20 4f 45 5f 49  ";.    case OE_I
2bd62 67 6e 6f 72 65 3a 20 20 20 72 65 74 75 72 6e 20  gnore:   return 
2bd63 22 69 67 6e 6f 72 65 22 3b 0a 20 20 20 20 63 61  "ignore";.    ca
2bd64 73 65 20 4f 45 5f 44 65 66 61 75 6c 74 3a 20 20  se OE_Default:  
2bd65 72 65 74 75 72 6e 20 22 64 65 66 61 75 6c 74 22  return "default"
2bd66 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 22  ;.  }.  return "
2bd67 6e 2f 61 22 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  n/a";.}.#endif..
2bd68 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 63 6f 6e 74  /*.** Parse cont
2bd69 65 78 74 20 73 74 72 75 63 74 75 72 65 20 70 46  ext structure pF
2bd6a 72 6f 6d 20 68 61 73 20 6a 75 73 74 20 62 65 65  rom has just bee
2bd6b 6e 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65  n used to create
2bd6c 20 61 20 73 75 62 2d 76 64 62 65 0a 2a 2a 20 28   a sub-vdbe.** (
2bd6d 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 29  trigger program)
2bd6e 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 68 61  . If an error ha
2bd6f 73 20 6f 63 63 75 72 72 65 64 2c 20 74 72 61 6e  s occurred, tran
2bd70 73 66 65 72 20 65 72 72 6f 72 20 69 6e 66 6f 72  sfer error infor
2bd71 6d 61 74 69 6f 6e 0a 2a 2a 20 66 72 6f 6d 20 70  mation.** from p
2bd72 46 72 6f 6d 20 74 6f 20 70 54 6f 2e 0a 2a 2f 0a  From to pTo..*/.
2bd73 73 74 61 74 69 63 20 76 6f 69 64 20 74 72 61 6e  static void tran
2bd74 73 66 65 72 50 61 72 73 65 45 72 72 6f 72 28 50  sferParseError(P
2bd75 61 72 73 65 20 2a 70 54 6f 2c 20 50 61 72 73 65  arse *pTo, Parse
2bd76 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 61 73 73 65   *pFrom){.  asse
2bd77 72 74 28 20 70 46 72 6f 6d 2d 3e 7a 45 72 72 4d  rt( pFrom->zErrM
2bd78 73 67 3d 3d 30 20 7c 7c 20 70 46 72 6f 6d 2d 3e  sg==0 || pFrom->
2bd79 6e 45 72 72 20 29 3b 0a 20 20 61 73 73 65 72 74  nErr );.  assert
2bd7a 28 20 70 54 6f 2d 3e 7a 45 72 72 4d 73 67 3d 3d  ( pTo->zErrMsg==
2bd7b 30 20 7c 7c 20 70 54 6f 2d 3e 6e 45 72 72 20 29  0 || pTo->nErr )
2bd7c 3b 0a 20 20 69 66 28 20 70 54 6f 2d 3e 6e 45 72  ;.  if( pTo->nEr
2bd7d 72 3d 3d 30 20 29 7b 0a 20 20 20 20 70 54 6f 2d  r==0 ){.    pTo-
2bd7e 3e 7a 45 72 72 4d 73 67 20 3d 20 70 46 72 6f 6d  >zErrMsg = pFrom
2bd7f 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 70  ->zErrMsg;.    p
2bd80 54 6f 2d 3e 6e 45 72 72 20 3d 20 70 46 72 6f 6d  To->nErr = pFrom
2bd81 2d 3e 6e 45 72 72 3b 0a 20 20 7d 65 6c 73 65 7b  ->nErr;.  }else{
2bd82 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
2bd83 65 65 28 70 46 72 6f 6d 2d 3e 64 62 2c 20 70 46  ee(pFrom->db, pF
2bd84 72 6f 6d 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20  rom->zErrMsg);. 
2bd85 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61   }.}../*.** Crea
2bd86 74 65 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 20  te and populate 
2bd87 61 20 6e 65 77 20 54 72 69 67 67 65 72 50 72 67  a new TriggerPrg
2bd88 20 6f 62 6a 65 63 74 20 77 69 74 68 20 61 20 73   object with a s
2bd89 75 62 2d 70 72 6f 67 72 61 6d 20 0a 2a 2a 20 69  ub-program .** i
2bd8a 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 74 72 69 67  mplementing trig
2bd8b 67 65 72 20 70 54 72 69 67 67 65 72 20 77 69 74  ger pTrigger wit
2bd8c 68 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f  h ON CONFLICT po
2bd8d 6c 69 63 79 20 6f 72 63 6f 6e 66 2e 0a 2a 2f 0a  licy orconf..*/.
2bd8e 73 74 61 74 69 63 20 54 72 69 67 67 65 72 50 72  static TriggerPr
2bd8f 67 20 2a 63 6f 64 65 52 6f 77 54 72 69 67 67 65  g *codeRowTrigge
2bd90 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  r(.  Parse *pPar
2bd91 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 43 75 72  se,       /* Cur
2bd92 72 65 6e 74 20 70 61 72 73 65 20 63 6f 6e 74 65  rent parse conte
2bd93 78 74 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20  xt */.  Trigger 
2bd94 2a 70 54 72 69 67 67 65 72 2c 20 20 20 2f 2a 20  *pTrigger,   /* 
2bd95 54 72 69 67 67 65 72 20 74 6f 20 63 6f 64 65 20  Trigger to code 
2bd96 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  */.  Table *pTab
2bd97 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ,         /* The
2bd98 20 74 61 62 6c 65 20 70 54 72 69 67 67 65 72 20   table pTrigger 
2bd99 69 73 20 61 74 74 61 63 68 65 64 20 74 6f 20 2a  is attached to *
2bd9a 2f 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 20 20  /.  int orconf  
2bd9b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 4e 20 43           /* ON C
2bd9c 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 74  ONFLICT policy t
2bd9d 6f 20 63 6f 64 65 20 74 72 69 67 67 65 72 20 70  o code trigger p
2bd9e 72 6f 67 72 61 6d 20 77 69 74 68 20 2a 2f 0a 29  rogram with */.)
2bd9f 7b 0a 20 20 50 61 72 73 65 20 2a 70 54 6f 70 20  {.  Parse *pTop 
2bda0 3d 20 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f  = sqlite3ParseTo
2bda1 70 6c 65 76 65 6c 28 70 50 61 72 73 65 29 3b 0a  plevel(pParse);.
2bda2 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
2bda3 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 2f 2a  pParse->db;   /*
2bda4 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   Database handle
2bda5 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 50 72 67   */.  TriggerPrg
2bda6 20 2a 70 50 72 67 3b 20 20 20 20 20 20 20 20 20   *pPrg;         
2bda7 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65    /* Value to re
2bda8 74 75 72 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a  turn */.  Expr *
2bda9 70 57 68 65 6e 20 3d 20 30 3b 20 20 20 20 20 20  pWhen = 0;      
2bdaa 20 20 20 20 20 20 2f 2a 20 44 75 70 6c 69 63 61        /* Duplica
2bdab 74 65 20 6f 66 20 74 72 69 67 67 65 72 20 57 48  te of trigger WH
2bdac 45 4e 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f  EN expression */
2bdad 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20  .  Vdbe *v;     
2bdae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2bdaf 2a 20 54 65 6d 70 6f 72 61 72 79 20 56 4d 20 2a  * Temporary VM *
2bdb0 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  /.  NameContext 
2bdb1 73 4e 43 3b 20 20 20 20 20 20 20 20 20 20 20 20  sNC;            
2bdb2 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20  /* Name context 
2bdb3 66 6f 72 20 73 75 62 2d 76 64 62 65 20 2a 2f 0a  for sub-vdbe */.
2bdb4 20 20 53 75 62 50 72 6f 67 72 61 6d 20 2a 70 50    SubProgram *pP
2bdb5 72 6f 67 72 61 6d 20 3d 20 30 3b 20 20 20 2f 2a  rogram = 0;   /*
2bdb6 20 53 75 62 2d 76 64 62 65 20 66 6f 72 20 74 72   Sub-vdbe for tr
2bdb7 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 2a 2f  igger program */
2bdb8 0a 20 20 50 61 72 73 65 20 2a 70 53 75 62 50 61  .  Parse *pSubPa
2bdb9 72 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f  rse;           /
2bdba 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20  * Parse context 
2bdbb 66 6f 72 20 73 75 62 2d 76 64 62 65 20 2a 2f 0a  for sub-vdbe */.
2bdbc 20 20 69 6e 74 20 69 45 6e 64 54 72 69 67 67 65    int iEndTrigge
2bdbd 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a  r = 0;        /*
2bdbe 20 4c 61 62 65 6c 20 74 6f 20 6a 75 6d 70 20 74   Label to jump t
2bdbf 6f 20 69 66 20 57 48 45 4e 20 69 73 20 66 61 6c  o if WHEN is fal
2bdc0 73 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  se */..  assert(
2bdc1 20 70 54 61 62 3d 3d 74 61 62 6c 65 4f 66 54 72   pTab==tableOfTr
2bdc2 69 67 67 65 72 28 70 54 72 69 67 67 65 72 29 20  igger(pTrigger) 
2bdc3 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  );..  /* Allocat
2bdc4 65 20 74 68 65 20 54 72 69 67 67 65 72 50 72 67  e the TriggerPrg
2bdc5 20 61 6e 64 20 53 75 62 50 72 6f 67 72 61 6d 20   and SubProgram 
2bdc6 6f 62 6a 65 63 74 73 2e 20 54 6f 20 65 6e 73 75  objects. To ensu
2bdc7 72 65 20 74 68 61 74 20 74 68 65 79 0a 20 20 2a  re that they.  *
2bdc8 2a 20 61 72 65 20 66 72 65 65 64 20 69 66 20 61  * are freed if a
2bdc9 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
2bdca 6c 69 6e 6b 20 74 68 65 6d 20 69 6e 74 6f 20 74  link them into t
2bdcb 68 65 20 50 61 72 73 65 2e 70 54 72 69 67 67 65  he Parse.pTrigge
2bdcc 72 50 72 67 20 0a 20 20 2a 2a 20 6c 69 73 74 20  rPrg .  ** list 
2bdcd 6f 66 20 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c  of the top-level
2bdce 20 50 61 72 73 65 20 6f 62 6a 65 63 74 20 73 6f   Parse object so
2bdcf 6f 6e 65 72 20 72 61 74 68 65 72 20 74 68 61 6e  oner rather than
2bdd0 20 6c 61 74 65 72 2e 20 20 2a 2f 0a 20 20 70 50   later.  */.  pP
2bdd1 72 67 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  rg = sqlite3DbMa
2bdd2 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
2bdd3 65 6f 66 28 54 72 69 67 67 65 72 50 72 67 29 29  eof(TriggerPrg))
2bdd4 3b 0a 20 20 69 66 28 20 21 70 50 72 67 20 29 20  ;.  if( !pPrg ) 
2bdd5 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 50 72 67  return 0;.  pPrg
2bdd6 2d 3e 70 4e 65 78 74 20 3d 20 70 54 6f 70 2d 3e  ->pNext = pTop->
2bdd7 70 54 72 69 67 67 65 72 50 72 67 3b 0a 20 20 70  pTriggerPrg;.  p
2bdd8 54 6f 70 2d 3e 70 54 72 69 67 67 65 72 50 72 67  Top->pTriggerPrg
2bdd9 20 3d 20 70 50 72 67 3b 0a 20 20 70 50 72 67 2d   = pPrg;.  pPrg-
2bdda 3e 70 50 72 6f 67 72 61 6d 20 3d 20 70 50 72 6f  >pProgram = pPro
2bddb 67 72 61 6d 20 3d 20 73 71 6c 69 74 65 33 44 62  gram = sqlite3Db
2bddc 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
2bddd 69 7a 65 6f 66 28 53 75 62 50 72 6f 67 72 61 6d  izeof(SubProgram
2bdde 29 29 3b 0a 20 20 69 66 28 20 21 70 50 72 6f 67  ));.  if( !pProg
2bddf 72 61 6d 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ram ) return 0;.
2bde0 20 20 70 50 72 6f 67 72 61 6d 2d 3e 6e 52 65 66    pProgram->nRef
2bde1 20 3d 20 31 3b 0a 20 20 70 50 72 67 2d 3e 70 54   = 1;.  pPrg->pT
2bde2 72 69 67 67 65 72 20 3d 20 70 54 72 69 67 67 65  rigger = pTrigge
2bde3 72 3b 0a 20 20 70 50 72 67 2d 3e 6f 72 63 6f 6e  r;.  pPrg->orcon
2bde4 66 20 3d 20 6f 72 63 6f 6e 66 3b 0a 0a 20 20 2f  f = orconf;..  /
2bde5 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 70  * Allocate and p
2bde6 6f 70 75 6c 61 74 65 20 61 20 6e 65 77 20 50 61  opulate a new Pa
2bde7 72 73 65 20 63 6f 6e 74 65 78 74 20 74 6f 20 75  rse context to u
2bde8 73 65 20 66 6f 72 20 63 6f 64 69 6e 67 20 74 68  se for coding th
2bde9 65 20 0a 20 20 2a 2a 20 74 72 69 67 67 65 72 20  e .  ** trigger 
2bdea 73 75 62 2d 70 72 6f 67 72 61 6d 2e 20 20 2a 2f  sub-program.  */
2bdeb 0a 20 20 70 53 75 62 50 61 72 73 65 20 3d 20 73  .  pSubParse = s
2bdec 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63  qlite3StackAlloc
2bded 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28  Zero(db, sizeof(
2bdee 50 61 72 73 65 29 29 3b 0a 20 20 69 66 28 20 21  Parse));.  if( !
2bdef 70 53 75 62 50 61 72 73 65 20 29 20 72 65 74 75  pSubParse ) retu
2bdf0 72 6e 20 30 3b 0a 20 20 6d 65 6d 73 65 74 28 26  rn 0;.  memset(&
2bdf1 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73  sNC, 0, sizeof(s
2bdf2 4e 43 29 29 3b 0a 20 20 73 4e 43 2e 70 50 61 72  NC));.  sNC.pPar
2bdf3 73 65 20 3d 20 70 53 75 62 50 61 72 73 65 3b 0a  se = pSubParse;.
2bdf4 20 20 70 53 75 62 50 61 72 73 65 2d 3e 64 62 20    pSubParse->db 
2bdf5 3d 20 64 62 3b 0a 20 20 70 53 75 62 50 61 72 73  = db;.  pSubPars
2bdf6 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 20 3d  e->pTriggerTab =
2bdf7 20 70 54 61 62 3b 0a 20 20 70 53 75 62 50 61 72   pTab;.  pSubPar
2bdf8 73 65 2d 3e 70 54 6f 70 6c 65 76 65 6c 20 3d 20  se->pToplevel = 
2bdf9 70 54 6f 70 3b 0a 20 20 70 53 75 62 50 61 72 73  pTop;.  pSubPars
2bdfa 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20  e->zAuthContext 
2bdfb 3d 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d  = pTrigger->zNam
2bdfc 65 3b 0a 20 20 70 53 75 62 50 61 72 73 65 2d 3e  e;.  pSubParse->
2bdfd 65 54 72 69 67 67 65 72 4f 70 20 3d 20 70 54 72  eTriggerOp = pTr
2bdfe 69 67 67 65 72 2d 3e 6f 70 3b 0a 0a 20 20 76 20  igger->op;..  v 
2bdff 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
2be00 28 70 53 75 62 50 61 72 73 65 29 3b 0a 20 20 69  (pSubParse);.  i
2be01 66 28 20 76 20 29 7b 0a 20 20 20 20 56 64 62 65  f( v ){.    Vdbe
2be02 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 53 74 61  Comment((v, "Sta
2be03 72 74 3a 20 25 73 2e 25 73 20 28 25 73 20 25 73  rt: %s.%s (%s %s
2be04 25 73 25 73 20 4f 4e 20 25 73 29 22 2c 20 0a 20  %s%s ON %s)", . 
2be05 20 20 20 20 20 70 54 72 69 67 67 65 72 2d 3e 7a       pTrigger->z
2be06 4e 61 6d 65 2c 20 6f 6e 45 72 72 6f 72 54 65 78  Name, onErrorTex
2be07 74 28 6f 72 63 6f 6e 66 29 2c 0a 20 20 20 20 20  t(orconf),.     
2be08 20 28 70 54 72 69 67 67 65 72 2d 3e 74 72 5f 74   (pTrigger->tr_t
2be09 6d 3d 3d 54 52 49 47 47 45 52 5f 42 45 46 4f 52  m==TRIGGER_BEFOR
2be0a 45 20 3f 20 22 42 45 46 4f 52 45 22 20 3a 20 22  E ? "BEFORE" : "
2be0b 41 46 54 45 52 22 29 2c 0a 20 20 20 20 20 20 20  AFTER"),.       
2be0c 20 28 70 54 72 69 67 67 65 72 2d 3e 6f 70 3d 3d   (pTrigger->op==
2be0d 54 4b 5f 55 50 44 41 54 45 20 3f 20 22 55 50 44  TK_UPDATE ? "UPD
2be0e 41 54 45 22 20 3a 20 22 22 29 2c 0a 20 20 20 20  ATE" : ""),.    
2be0f 20 20 20 20 28 70 54 72 69 67 67 65 72 2d 3e 6f      (pTrigger->o
2be10 70 3d 3d 54 4b 5f 49 4e 53 45 52 54 20 3f 20 22  p==TK_INSERT ? "
2be11 49 4e 53 45 52 54 22 20 3a 20 22 22 29 2c 0a 20  INSERT" : ""),. 
2be12 20 20 20 20 20 20 20 28 70 54 72 69 67 67 65 72         (pTrigger
2be13 2d 3e 6f 70 3d 3d 54 4b 5f 44 45 4c 45 54 45 20  ->op==TK_DELETE 
2be14 3f 20 22 44 45 4c 45 54 45 22 20 3a 20 22 22 29  ? "DELETE" : "")
2be15 2c 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 7a 4e  ,.      pTab->zN
2be16 61 6d 65 0a 20 20 20 20 29 29 3b 0a 23 69 66 6e  ame.    ));.#ifn
2be17 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2be18 54 52 41 43 45 0a 20 20 20 20 73 71 6c 69 74 65  TRACE.    sqlite
2be19 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
2be1a 20 2d 31 2c 20 0a 20 20 20 20 20 20 73 71 6c 69   -1, .      sqli
2be1b 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
2be1c 2d 2d 20 54 52 49 47 47 45 52 20 25 73 22 2c 20  -- TRIGGER %s", 
2be1d 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65 29  pTrigger->zName)
2be1e 2c 20 50 34 5f 44 59 4e 41 4d 49 43 0a 20 20 20  , P4_DYNAMIC.   
2be1f 20 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20   );.#endif..    
2be20 2f 2a 20 49 66 20 6f 6e 65 20 77 61 73 20 73 70  /* If one was sp
2be21 65 63 69 66 69 65 64 2c 20 63 6f 64 65 20 74 68  ecified, code th
2be22 65 20 57 48 45 4e 20 63 6c 61 75 73 65 2e 20 49  e WHEN clause. I
2be23 66 20 69 74 20 65 76 61 6c 75 61 74 65 73 20 74  f it evaluates t
2be24 6f 20 66 61 6c 73 65 0a 20 20 20 20 2a 2a 20 28  o false.    ** (
2be25 6f 72 20 4e 55 4c 4c 29 20 74 68 65 20 73 75 62  or NULL) the sub
2be26 2d 76 64 62 65 20 69 73 20 69 6d 6d 65 64 69 61  -vdbe is immedia
2be27 74 65 6c 79 20 68 61 6c 74 65 64 20 62 79 20 6a  tely halted by j
2be28 75 6d 70 69 6e 67 20 74 6f 20 74 68 65 20 0a 20  umping to the . 
2be29 20 20 20 2a 2a 20 4f 50 5f 48 61 6c 74 20 69 6e     ** OP_Halt in
2be2a 73 65 72 74 65 64 20 61 74 20 74 68 65 20 65 6e  serted at the en
2be2b 64 20 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d  d of the program
2be2c 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 54  .  */.    if( pT
2be2d 72 69 67 67 65 72 2d 3e 70 57 68 65 6e 20 29 7b  rigger->pWhen ){
2be2e 0a 20 20 20 20 20 20 70 57 68 65 6e 20 3d 20 73  .      pWhen = s
2be2f 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
2be30 2c 20 70 54 72 69 67 67 65 72 2d 3e 70 57 68 65  , pTrigger->pWhe
2be31 6e 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28  n, 0);.      if(
2be32 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69   SQLITE_OK==sqli
2be33 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61  te3ResolveExprNa
2be34 6d 65 73 28 26 73 4e 43 2c 20 70 57 68 65 6e 29  mes(&sNC, pWhen)
2be35 20 0a 20 20 20 20 20 20 20 26 26 20 64 62 2d 3e   .       && db->
2be36 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20  mallocFailed==0 
2be37 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20  .      ){.      
2be38 20 20 69 45 6e 64 54 72 69 67 67 65 72 20 3d 20    iEndTrigger = 
2be39 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
2be3a 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 20  abel(v);.       
2be3b 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61   sqlite3ExprIfFa
2be3c 6c 73 65 28 70 53 75 62 50 61 72 73 65 2c 20 70  lse(pSubParse, p
2be3d 57 68 65 6e 2c 20 69 45 6e 64 54 72 69 67 67 65  When, iEndTrigge
2be3e 72 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46  r, SQLITE_JUMPIF
2be3f 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 7d 0a 20  NULL);.      }. 
2be40 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2be41 44 65 6c 65 74 65 28 64 62 2c 20 70 57 68 65 6e  Delete(db, pWhen
2be42 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  );.    }..    /*
2be43 20 43 6f 64 65 20 74 68 65 20 74 72 69 67 67 65   Code the trigge
2be44 72 20 70 72 6f 67 72 61 6d 20 69 6e 74 6f 20 74  r program into t
2be45 68 65 20 73 75 62 2d 76 64 62 65 2e 20 2a 2f 0a  he sub-vdbe. */.
2be46 20 20 20 20 63 6f 64 65 54 72 69 67 67 65 72 50      codeTriggerP
2be47 72 6f 67 72 61 6d 28 70 53 75 62 50 61 72 73 65  rogram(pSubParse
2be48 2c 20 70 54 72 69 67 67 65 72 2d 3e 73 74 65 70  , pTrigger->step
2be49 5f 6c 69 73 74 2c 20 6f 72 63 6f 6e 66 29 3b 0a  _list, orconf);.
2be4a 0a 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 20 61  .    /* Insert a
2be4b 6e 20 4f 50 5f 48 61 6c 74 20 61 74 20 74 68 65  n OP_Halt at the
2be4c 20 65 6e 64 20 6f 66 20 74 68 65 20 73 75 62 2d   end of the sub-
2be4d 70 72 6f 67 72 61 6d 2e 20 2a 2f 0a 20 20 20 20  program. */.    
2be4e 69 66 28 20 69 45 6e 64 54 72 69 67 67 65 72 20  if( iEndTrigger 
2be4f 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2be50 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
2be51 28 76 2c 20 69 45 6e 64 54 72 69 67 67 65 72 29  (v, iEndTrigger)
2be52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
2be53 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c  te3VdbeAddOp0(v,
2be54 20 4f 50 5f 48 61 6c 74 29 3b 0a 20 20 20 20 56   OP_Halt);.    V
2be55 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
2be56 45 6e 64 3a 20 25 73 2e 25 73 22 2c 20 70 54 72  End: %s.%s", pTr
2be57 69 67 67 65 72 2d 3e 7a 4e 61 6d 65 2c 20 6f 6e  igger->zName, on
2be58 45 72 72 6f 72 54 65 78 74 28 6f 72 63 6f 6e 66  ErrorText(orconf
2be59 29 29 29 3b 0a 0a 20 20 20 20 74 72 61 6e 73 66  )));..    transf
2be5a 65 72 50 61 72 73 65 45 72 72 6f 72 28 70 50 61  erParseError(pPa
2be5b 72 73 65 2c 20 70 53 75 62 50 61 72 73 65 29 3b  rse, pSubParse);
2be5c 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  .    if( db->mal
2be5d 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 29 7b 0a  locFailed==0 ){.
2be5e 20 20 20 20 20 20 70 50 72 6f 67 72 61 6d 2d 3e        pProgram->
2be5f 61 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62  aOp = sqlite3Vdb
2be60 65 54 61 6b 65 4f 70 41 72 72 61 79 28 76 2c 20  eTakeOpArray(v, 
2be61 26 70 50 72 6f 67 72 61 6d 2d 3e 6e 4f 70 2c 20  &pProgram->nOp, 
2be62 26 70 54 6f 70 2d 3e 6e 4d 61 78 41 72 67 29 3b  &pTop->nMaxArg);
2be63 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 72 6f 67  .    }.    pProg
2be64 72 61 6d 2d 3e 6e 4d 65 6d 20 3d 20 70 53 75 62  ram->nMem = pSub
2be65 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
2be66 20 70 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 20   pProgram->nCsr 
2be67 3d 20 70 53 75 62 50 61 72 73 65 2d 3e 6e 54 61  = pSubParse->nTa
2be68 62 3b 0a 20 20 20 20 70 50 72 6f 67 72 61 6d 2d  b;.    pProgram-
2be69 3e 74 6f 6b 65 6e 20 3d 20 28 76 6f 69 64 20 2a  >token = (void *
2be6a 29 70 54 72 69 67 67 65 72 3b 0a 20 20 20 20 70  )pTrigger;.    p
2be6b 50 72 67 2d 3e 6f 6c 64 6d 61 73 6b 20 3d 20 70  Prg->oldmask = p
2be6c 53 75 62 50 61 72 73 65 2d 3e 6f 6c 64 6d 61 73  SubParse->oldmas
2be6d 6b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  k;.    sqlite3Vd
2be6e 62 65 44 65 6c 65 74 65 28 76 29 3b 0a 20 20 7d  beDelete(v);.  }
2be6f 0a 0a 20 20 61 73 73 65 72 74 28 20 21 70 53 75  ..  assert( !pSu
2be70 62 50 61 72 73 65 2d 3e 70 41 69 6e 63 20 20 20  bParse->pAinc   
2be71 20 20 20 20 26 26 20 21 70 53 75 62 50 61 72 73      && !pSubPars
2be72 65 2d 3e 70 5a 6f 6d 62 69 65 54 61 62 20 29 3b  e->pZombieTab );
2be73 0a 20 20 61 73 73 65 72 74 28 20 21 70 53 75 62  .  assert( !pSub
2be74 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 50  Parse->pTriggerP
2be75 72 67 20 26 26 20 21 70 53 75 62 50 61 72 73 65  rg && !pSubParse
2be76 2d 3e 6e 4d 61 78 41 72 67 20 29 3b 0a 20 20 73  ->nMaxArg );.  s
2be77 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65 28  qlite3StackFree(
2be78 64 62 2c 20 70 53 75 62 50 61 72 73 65 29 3b 0a  db, pSubParse);.
2be79 0a 20 20 72 65 74 75 72 6e 20 70 50 72 67 3b 0a  .  return pPrg;.
2be7a 7d 0a 20 20 20 20 0a 2f 2a 0a 2a 2a 20 52 65 74  }.    ./*.** Ret
2be7b 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
2be7c 20 61 20 54 72 69 67 67 65 72 50 72 67 20 6f 62   a TriggerPrg ob
2be7d 6a 65 63 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ject containing 
2be7e 74 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 20  the sub-program 
2be7f 66 6f 72 0a 2a 2a 20 74 72 69 67 67 65 72 20 70  for.** trigger p
2be80 54 72 69 67 67 65 72 20 77 69 74 68 20 64 65 66  Trigger with def
2be81 61 75 6c 74 20 4f 4e 20 43 4f 4e 46 4c 49 43 54  ault ON CONFLICT
2be82 20 61 6c 67 6f 72 69 74 68 6d 20 6f 72 63 6f 6e   algorithm orcon
2be83 66 2e 20 49 66 20 6e 6f 20 73 75 63 68 0a 2a 2a  f. If no such.**
2be84 20 54 72 69 67 67 65 72 50 72 67 20 6f 62 6a 65   TriggerPrg obje
2be85 63 74 20 65 78 69 73 74 73 2c 20 61 20 6e 65 77  ct exists, a new
2be86 20 6f 62 6a 65 63 74 20 69 73 20 61 6c 6c 6f 63   object is alloc
2be87 61 74 65 64 20 61 6e 64 20 70 6f 70 75 6c 61 74  ated and populat
2be88 65 64 20 62 65 66 6f 72 65 0a 2a 2a 20 62 65 69  ed before.** bei
2be89 6e 67 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a  ng returned..*/.
2be8a 73 74 61 74 69 63 20 54 72 69 67 67 65 72 50 72  static TriggerPr
2be8b 67 20 2a 67 65 74 52 6f 77 54 72 69 67 67 65 72  g *getRowTrigger
2be8c 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2be8d 65 2c 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72  e,       /* Curr
2be8e 65 6e 74 20 70 61 72 73 65 20 63 6f 6e 74 65 78  ent parse contex
2be8f 74 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a  t */.  Trigger *
2be90 70 54 72 69 67 67 65 72 2c 20 20 20 2f 2a 20 54  pTrigger,   /* T
2be91 72 69 67 67 65 72 20 74 6f 20 63 6f 64 65 20 2a  rigger to code *
2be92 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c  /.  Table *pTab,
2be93 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2be94 74 61 62 6c 65 20 74 72 69 67 67 65 72 20 70 54  table trigger pT
2be95 72 69 67 67 65 72 20 69 73 20 61 74 74 61 63 68  rigger is attach
2be96 65 64 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 6f  ed to */.  int o
2be97 72 63 6f 6e 66 20 20 20 20 20 20 20 20 20 20 20  rconf           
2be98 2f 2a 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 61  /* ON CONFLICT a
2be99 6c 67 6f 72 69 74 68 6d 2e 20 2a 2f 0a 29 7b 0a  lgorithm. */.){.
2be9a 20 20 50 61 72 73 65 20 2a 70 52 6f 6f 74 20 3d    Parse *pRoot =
2be9b 20 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f 70   sqlite3ParseTop
2be9c 6c 65 76 65 6c 28 70 50 61 72 73 65 29 3b 0a 20  level(pParse);. 
2be9d 20 54 72 69 67 67 65 72 50 72 67 20 2a 70 50 72   TriggerPrg *pPr
2be9e 67 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 54  g;..  assert( pT
2be9f 61 62 3d 3d 74 61 62 6c 65 4f 66 54 72 69 67 67  ab==tableOfTrigg
2bea0 65 72 28 70 54 72 69 67 67 65 72 29 20 29 3b 0a  er(pTrigger) );.
2bea1 0a 20 20 2f 2a 20 49 74 20 6d 61 79 20 62 65 20  .  /* It may be 
2bea2 74 68 61 74 20 74 68 69 73 20 74 72 69 67 67 65  that this trigge
2bea3 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  r has already be
2bea4 65 6e 20 63 6f 64 65 64 20 28 6f 72 20 69 73 20  en coded (or is 
2bea5 69 6e 20 74 68 65 0a 20 20 2a 2a 20 70 72 6f 63  in the.  ** proc
2bea6 65 73 73 20 6f 66 20 62 65 69 6e 67 20 63 6f 64  ess of being cod
2bea7 65 64 29 2e 20 49 66 20 74 68 69 73 20 69 73 20  ed). If this is 
2bea8 74 68 65 20 63 61 73 65 2c 20 74 68 65 6e 20 61  the case, then a
2bea9 6e 20 65 6e 74 72 79 20 77 69 74 68 0a 20 20 2a  n entry with.  *
2beaa 2a 20 61 20 6d 61 74 63 68 69 6e 67 20 54 72 69  * a matching Tri
2beab 67 67 65 72 50 72 67 2e 70 54 72 69 67 67 65 72  ggerPrg.pTrigger
2beac 20 66 69 65 6c 64 20 77 69 6c 6c 20 62 65 20 70   field will be p
2bead 72 65 73 65 6e 74 20 73 6f 6d 65 77 68 65 72 65  resent somewhere
2beae 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 50 61 72  .  ** in the Par
2beaf 73 65 2e 70 54 72 69 67 67 65 72 50 72 67 20 6c  se.pTriggerPrg l
2beb0 69 73 74 2e 20 53 65 61 72 63 68 20 66 6f 72 20  ist. Search for 
2beb1 73 75 63 68 20 61 6e 20 65 6e 74 72 79 2e 20 20  such an entry.  
2beb2 2a 2f 0a 20 20 66 6f 72 28 70 50 72 67 3d 70 52  */.  for(pPrg=pR
2beb3 6f 6f 74 2d 3e 70 54 72 69 67 67 65 72 50 72 67  oot->pTriggerPrg
2beb4 3b 20 0a 20 20 20 20 20 20 70 50 72 67 20 26 26  ; .      pPrg &&
2beb5 20 28 70 50 72 67 2d 3e 70 54 72 69 67 67 65 72   (pPrg->pTrigger
2beb6 21 3d 70 54 72 69 67 67 65 72 20 7c 7c 20 70 50  !=pTrigger || pP
2beb7 72 67 2d 3e 6f 72 63 6f 6e 66 21 3d 6f 72 63 6f  rg->orconf!=orco
2beb8 6e 66 29 3b 20 0a 20 20 20 20 20 20 70 50 72 67  nf); .      pPrg
2beb9 3d 70 50 72 67 2d 3e 70 4e 65 78 74 0a 20 20 29  =pPrg->pNext.  )
2beba 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 20 65 78  ;..  /* If an ex
2bebb 69 73 74 69 6e 67 20 54 72 69 67 67 65 72 50 72  isting TriggerPr
2bebc 67 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 6c  g could not be l
2bebd 6f 63 61 74 65 64 2c 20 63 72 65 61 74 65 20 61  ocated, create a
2bebe 20 6e 65 77 20 6f 6e 65 2e 20 2a 2f 0a 20 20 69   new one. */.  i
2bebf 66 28 20 21 70 50 72 67 20 29 7b 0a 20 20 20 20  f( !pPrg ){.    
2bec0 70 50 72 67 20 3d 20 63 6f 64 65 52 6f 77 54 72  pPrg = codeRowTr
2bec1 69 67 67 65 72 28 70 50 61 72 73 65 2c 20 70 54  igger(pParse, pT
2bec2 72 69 67 67 65 72 2c 20 70 54 61 62 2c 20 6f 72  rigger, pTab, or
2bec3 63 6f 6e 66 29 3b 0a 20 20 7d 0a 0a 20 20 72 65  conf);.  }..  re
2bec4 74 75 72 6e 20 70 50 72 67 3b 0a 7d 0a 0a 2f 2a  turn pPrg;.}../*
2bec5 0a 2a 2a 20 54 68 69 73 20 69 73 20 63 61 6c 6c  .** This is call
2bec6 65 64 20 74 6f 20 63 6f 64 65 20 46 4f 52 20 45  ed to code FOR E
2bec7 41 43 48 20 52 4f 57 20 74 72 69 67 67 65 72 73  ACH ROW triggers
2bec8 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65  ..**.** When the
2bec9 20 63 6f 64 65 20 74 68 61 74 20 74 68 69 73 20   code that this 
2beca 66 75 6e 63 74 69 6f 6e 20 67 65 6e 65 72 61 74  function generat
2becb 65 73 20 69 73 20 65 78 65 63 75 74 65 64 2c 20  es is executed, 
2becc 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a  the following .*
2becd 2a 20 6d 75 73 74 20 62 65 20 74 72 75 65 3a 0a  * must be true:.
2bece 2a 2a 0a 2a 2a 20 31 2e 20 4e 6f 20 63 75 72 73  **.** 1. No curs
2becf 6f 72 73 20 6d 61 79 20 62 65 20 6f 70 65 6e 20  ors may be open 
2bed0 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  in the main data
2bed1 62 61 73 65 2e 20 20 28 42 75 74 20 6e 65 77 49  base.  (But newI
2bed2 64 78 20 61 6e 64 20 6f 6c 64 49 64 78 0a 2a 2a  dx and oldIdx.**
2bed3 20 20 20 20 63 61 6e 20 62 65 20 69 6e 64 69 63      can be indic
2bed4 65 73 20 6f 66 20 63 75 72 73 6f 72 73 20 69 6e  es of cursors in
2bed5 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
2bed6 73 2e 20 20 53 65 65 20 62 65 6c 6f 77 2e 29 0a  s.  See below.).
2bed7 2a 2a 0a 2a 2a 20 32 2e 20 49 66 20 74 68 65 20  **.** 2. If the 
2bed8 74 72 69 67 67 65 72 73 20 62 65 69 6e 67 20 63  triggers being c
2bed9 6f 64 65 64 20 61 72 65 20 4f 4e 20 49 4e 53 45  oded are ON INSE
2beda 52 54 20 6f 72 20 4f 4e 20 55 50 44 41 54 45 20  RT or ON UPDATE 
2bedb 74 72 69 67 67 65 72 73 2c 20 74 68 65 6e 0a 2a  triggers, then.*
2bedc 2a 20 20 20 20 61 20 74 65 6d 70 6f 72 61 72 79  *    a temporary
2bedd 20 76 64 62 65 20 63 75 72 73 6f 72 20 28 69 6e   vdbe cursor (in
2bede 64 65 78 20 6e 65 77 49 64 78 29 20 6d 75 73 74  dex newIdx) must
2bedf 20 62 65 20 6f 70 65 6e 20 61 6e 64 20 70 6f 69   be open and poi
2bee0 6e 74 69 6e 67 20 61 74 0a 2a 2a 20 20 20 20 61  nting at.**    a
2bee1 20 72 6f 77 20 63 6f 6e 74 61 69 6e 69 6e 67 20   row containing 
2bee2 76 61 6c 75 65 73 20 74 6f 20 62 65 20 73 75 62  values to be sub
2bee3 73 74 69 74 75 74 65 64 20 66 6f 72 20 6e 65 77  stituted for new
2bee4 2e 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69  .* expressions i
2bee5 6e 20 74 68 65 0a 2a 2a 20 20 20 20 74 72 69 67  n the.**    trig
2bee6 67 65 72 20 70 72 6f 67 72 61 6d 28 73 29 2e 0a  ger program(s)..
2bee7 2a 2a 0a 2a 2a 20 33 2e 20 49 66 20 74 68 65 20  **.** 3. If the 
2bee8 74 72 69 67 67 65 72 73 20 62 65 69 6e 67 20 63  triggers being c
2bee9 6f 64 65 64 20 61 72 65 20 4f 4e 20 44 45 4c 45  oded are ON DELE
2beea 54 45 20 6f 72 20 4f 4e 20 55 50 44 41 54 45 20  TE or ON UPDATE 
2beeb 74 72 69 67 67 65 72 73 2c 20 74 68 65 6e 0a 2a  triggers, then.*
2beec 2a 20 20 20 20 61 20 74 65 6d 70 6f 72 61 72 79  *    a temporary
2beed 20 76 64 62 65 20 63 75 72 73 6f 72 20 28 69 6e   vdbe cursor (in
2beee 64 65 78 20 6f 6c 64 49 64 78 29 20 6d 75 73 74  dex oldIdx) must
2beef 20 62 65 20 6f 70 65 6e 20 61 6e 64 20 70 6f 69   be open and poi
2bef0 6e 74 69 6e 67 20 61 74 0a 2a 2a 20 20 20 20 61  nting at.**    a
2bef1 20 72 6f 77 20 63 6f 6e 74 61 69 6e 69 6e 67 20   row containing 
2bef2 76 61 6c 75 65 73 20 74 6f 20 62 65 20 73 75 62  values to be sub
2bef3 73 74 69 74 75 74 65 64 20 66 6f 72 20 6f 6c 64  stituted for old
2bef4 2e 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69  .* expressions i
2bef5 6e 20 74 68 65 0a 2a 2a 20 20 20 20 74 72 69 67  n the.**    trig
2bef6 67 65 72 20 70 72 6f 67 72 61 6d 28 73 29 2e 0a  ger program(s)..
2bef7 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 79 20 61 72  **.** If they ar
2bef8 65 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 20  e not NULL, the 
2bef9 70 69 4f 6c 64 43 6f 6c 4d 61 73 6b 20 61 6e 64  piOldColMask and
2befa 20 70 69 4e 65 77 43 6f 6c 4d 61 73 6b 20 6f 75   piNewColMask ou
2befb 74 70 75 74 20 76 61 72 69 61 62 6c 65 73 0a 2a  tput variables.*
2befc 2a 20 61 72 65 20 73 65 74 20 74 6f 20 76 61 6c  * are set to val
2befd 75 65 73 20 74 68 61 74 20 64 65 73 63 72 69 62  ues that describ
2befe 65 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 75 73  e the columns us
2beff 65 64 20 62 79 20 74 68 65 20 74 72 69 67 67 65  ed by the trigge
2bf00 72 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 69 6e 20  r program.** in 
2bf01 74 68 65 20 4f 4c 44 2e 2a 20 61 6e 64 20 4e 45  the OLD.* and NE
2bf02 57 2e 2a 20 74 61 62 6c 65 73 20 72 65 73 70 65  W.* tables respe
2bf03 63 74 69 76 65 6c 79 2e 20 49 66 20 63 6f 6c 75  ctively. If colu
2bf04 6d 6e 20 4e 20 6f 66 20 74 68 65 20 0a 2a 2a 20  mn N of the .** 
2bf05 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 69 73 20  pseudo-table is 
2bf06 72 65 61 64 20 61 74 20 6c 65 61 73 74 20 6f 6e  read at least on
2bf07 63 65 2c 20 74 68 65 20 63 6f 72 72 65 73 70 6f  ce, the correspo
2bf08 6e 64 69 6e 67 20 62 69 74 20 6f 66 20 74 68 65  nding bit of the
2bf09 20 6f 75 74 70 75 74 0a 2a 2a 20 6d 61 73 6b 20   output.** mask 
2bf0a 69 73 20 73 65 74 2e 20 49 66 20 61 20 63 6f 6c  is set. If a col
2bf0b 75 6d 6e 20 77 69 74 68 20 61 6e 20 69 6e 64 65  umn with an inde
2bf0c 78 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 33  x greater than 3
2bf0d 32 20 69 73 20 72 65 61 64 2c 20 74 68 65 0a 2a  2 is read, the.*
2bf0e 2a 20 6f 75 74 70 75 74 20 6d 61 73 6b 20 69 73  * output mask is
2bf0f 20 73 65 74 20 74 6f 20 74 68 65 20 73 70 65 63   set to the spec
2bf10 69 61 6c 20 76 61 6c 75 65 20 30 78 66 66 66 66  ial value 0xffff
2bf11 66 66 66 66 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49  ffff..**.*/.SQLI
2bf12 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2bf13 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72  sqlite3CodeRowTr
2bf14 69 67 67 65 72 28 0a 20 20 50 61 72 73 65 20 2a  igger(.  Parse *
2bf15 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a  pParse,       /*
2bf16 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a   Parse context *
2bf17 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72  /.  Trigger *pTr
2bf18 69 67 67 65 72 2c 20 20 20 2f 2a 20 4c 69 73 74  igger,   /* List
2bf19 20 6f 66 20 74 72 69 67 67 65 72 73 20 6f 6e 20   of triggers on 
2bf1a 74 61 62 6c 65 20 70 54 61 62 20 2a 2f 0a 20 20  table pTab */.  
2bf1b 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20  int op,         
2bf1c 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54       /* One of T
2bf1d 4b 5f 55 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53  K_UPDATE, TK_INS
2bf1e 45 52 54 2c 20 54 4b 5f 44 45 4c 45 54 45 20 2a  ERT, TK_DELETE *
2bf1f 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 43  /.  ExprList *pC
2bf20 68 61 6e 67 65 73 2c 20 20 2f 2a 20 43 68 61 6e  hanges,  /* Chan
2bf21 67 65 73 20 6c 69 73 74 20 66 6f 72 20 61 6e 79  ges list for any
2bf22 20 55 50 44 41 54 45 20 4f 46 20 74 72 69 67 67   UPDATE OF trigg
2bf23 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 74 72 5f  ers */.  int tr_
2bf24 74 6d 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  tm,           /*
2bf25 20 4f 6e 65 20 6f 66 20 54 52 49 47 47 45 52 5f   One of TRIGGER_
2bf26 42 45 46 4f 52 45 2c 20 54 52 49 47 47 45 52 5f  BEFORE, TRIGGER_
2bf27 41 46 54 45 52 20 2a 2f 0a 20 20 54 61 62 6c 65  AFTER */.  Table
2bf28 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20 20   *pTab,         
2bf29 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20  /* The table to 
2bf2a 63 6f 64 65 20 74 72 69 67 67 65 72 73 20 66 72  code triggers fr
2bf2b 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 49  om */.  int newI
2bf2c 64 78 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  dx,          /* 
2bf2d 54 68 65 20 69 6e 64 69 63 65 20 6f 66 20 74 68  The indice of th
2bf2e 65 20 22 6e 65 77 22 20 72 6f 77 20 74 6f 20 61  e "new" row to a
2bf2f 63 63 65 73 73 20 2a 2f 0a 20 20 69 6e 74 20 6f  ccess */.  int o
2bf30 6c 64 49 64 78 2c 20 20 20 20 20 20 20 20 20 20  ldIdx,          
2bf31 2f 2a 20 54 68 65 20 69 6e 64 69 63 65 20 6f 66  /* The indice of
2bf32 20 74 68 65 20 22 6f 6c 64 22 20 72 6f 77 20 74   the "old" row t
2bf33 6f 20 61 63 63 65 73 73 20 2a 2f 0a 20 20 69 6e  o access */.  in
2bf34 74 20 6f 72 63 6f 6e 66 2c 20 20 20 20 20 20 20  t orconf,       
2bf35 20 20 20 2f 2a 20 4f 4e 20 43 4f 4e 46 4c 49 43     /* ON CONFLIC
2bf36 54 20 70 6f 6c 69 63 79 20 2a 2f 0a 20 20 69 6e  T policy */.  in
2bf37 74 20 69 67 6e 6f 72 65 4a 75 6d 70 20 20 20 20  t ignoreJump    
2bf38 20 20 20 2f 2a 20 49 6e 73 74 72 75 63 74 69 6f     /* Instructio
2bf39 6e 20 74 6f 20 6a 75 6d 70 20 74 6f 20 66 6f 72  n to jump to for
2bf3a 20 52 41 49 53 45 28 49 47 4e 4f 52 45 29 20 2a   RAISE(IGNORE) *
2bf3b 2f 0a 29 7b 0a 20 20 54 72 69 67 67 65 72 20 2a  /.){.  Trigger *
2bf3c 70 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  p;..  UNUSED_PAR
2bf3d 41 4d 45 54 45 52 28 6e 65 77 49 64 78 29 3b 0a  AMETER(newIdx);.
2bf3e 0a 20 20 61 73 73 65 72 74 28 6f 70 20 3d 3d 20  .  assert(op == 
2bf3f 54 4b 5f 55 50 44 41 54 45 20 7c 7c 20 6f 70 20  TK_UPDATE || op 
2bf40 3d 3d 20 54 4b 5f 49 4e 53 45 52 54 20 7c 7c 20  == TK_INSERT || 
2bf41 6f 70 20 3d 3d 20 54 4b 5f 44 45 4c 45 54 45 29  op == TK_DELETE)
2bf42 3b 0a 20 20 61 73 73 65 72 74 28 74 72 5f 74 6d  ;.  assert(tr_tm
2bf43 20 3d 3d 20 54 52 49 47 47 45 52 5f 42 45 46 4f   == TRIGGER_BEFO
2bf44 52 45 20 7c 7c 20 74 72 5f 74 6d 20 3d 3d 20 54  RE || tr_tm == T
2bf45 52 49 47 47 45 52 5f 41 46 54 45 52 20 29 3b 0a  RIGGER_AFTER );.
2bf46 0a 20 20 66 6f 72 28 70 3d 70 54 72 69 67 67 65  .  for(p=pTrigge
2bf47 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74  r; p; p=p->pNext
2bf48 29 7b 0a 0a 20 20 20 20 2f 2a 20 53 61 6e 69 74  ){..    /* Sanit
2bf49 79 20 63 68 65 63 6b 69 6e 67 3a 20 20 54 68 65  y checking:  The
2bf4a 20 73 63 68 65 6d 61 20 66 6f 72 20 74 68 65 20   schema for the 
2bf4b 74 72 69 67 67 65 72 20 61 6e 64 20 66 6f 72 20  trigger and for 
2bf4c 74 68 65 20 74 61 62 6c 65 20 61 72 65 0a 20 20  the table are.  
2bf4d 20 20 2a 2a 20 61 6c 77 61 79 73 20 64 65 66 69    ** always defi
2bf4e 6e 65 64 2e 20 20 54 68 65 20 74 72 69 67 67 65  ned.  The trigge
2bf4f 72 20 6d 75 73 74 20 62 65 20 69 6e 20 74 68 65  r must be in the
2bf50 20 73 61 6d 65 20 73 63 68 65 6d 61 20 61 73 20   same schema as 
2bf51 74 68 65 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a  the table.    **
2bf52 20 6f 72 20 65 6c 73 65 20 69 74 20 6d 75 73 74   or else it must
2bf53 20 62 65 20 61 20 54 45 4d 50 20 74 72 69 67 67   be a TEMP trigg
2bf54 65 72 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72  er. */.    asser
2bf55 74 28 20 70 2d 3e 70 53 63 68 65 6d 61 21 3d 30  t( p->pSchema!=0
2bf56 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
2bf57 70 2d 3e 70 54 61 62 53 63 68 65 6d 61 21 3d 30  p->pTabSchema!=0
2bf58 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
2bf59 70 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 2d 3e 70  p->pSchema==p->p
2bf5a 54 61 62 53 63 68 65 6d 61 20 0a 20 20 20 20 20  TabSchema .     
2bf5b 20 20 20 20 7c 7c 20 70 2d 3e 70 53 63 68 65 6d      || p->pSchem
2bf5c 61 3d 3d 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61  a==pParse->db->a
2bf5d 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29 3b  Db[1].pSchema );
2bf5e 0a 0a 20 20 20 20 2f 2a 20 44 65 74 65 72 6d 69  ..    /* Determi
2bf5f 6e 65 20 77 68 65 74 68 65 72 20 77 65 20 73 68  ne whether we sh
2bf60 6f 75 6c 64 20 63 6f 64 65 20 74 68 69 73 20 74  ould code this t
2bf61 72 69 67 67 65 72 20 2a 2f 0a 20 20 20 20 69 66  rigger */.    if
2bf62 28 20 70 2d 3e 6f 70 3d 3d 6f 70 20 0a 20 20 20  ( p->op==op .   
2bf63 20 20 26 26 20 70 2d 3e 74 72 5f 74 6d 3d 3d 74    && p->tr_tm==t
2bf64 72 5f 74 6d 20 0a 20 20 20 20 20 26 26 20 63 68  r_tm .     && ch
2bf65 65 63 6b 43 6f 6c 75 6d 6e 4f 76 65 72 6c 61 70  eckColumnOverlap
2bf66 28 70 2d 3e 70 43 6f 6c 75 6d 6e 73 2c 70 43 68  (p->pColumns,pCh
2bf67 61 6e 67 65 73 29 0a 20 20 20 20 29 7b 0a 20 20  anges).    ){.  
2bf68 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71      Vdbe *v = sq
2bf69 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
2bf6a 72 73 65 29 3b 20 2f 2a 20 4d 61 69 6e 20 56 4d  rse); /* Main VM
2bf6b 20 2a 2f 0a 20 20 20 20 20 20 54 72 69 67 67 65   */.      Trigge
2bf6c 72 50 72 67 20 2a 70 50 72 67 3b 0a 20 20 20 20  rPrg *pPrg;.    
2bf6d 20 20 70 50 72 67 20 3d 20 67 65 74 52 6f 77 54    pPrg = getRowT
2bf6e 72 69 67 67 65 72 28 70 50 61 72 73 65 2c 20 70  rigger(pParse, p
2bf6f 2c 20 70 54 61 62 2c 20 6f 72 63 6f 6e 66 29 3b  , pTab, orconf);
2bf70 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
2bf71 50 72 67 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e  Prg || pParse->n
2bf72 45 72 72 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64  Err || pParse->d
2bf73 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2bf74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 64  );..      /* Cod
2bf75 65 20 74 68 65 20 4f 50 5f 50 72 6f 67 72 61 6d  e the OP_Program
2bf76 20 6f 70 63 6f 64 65 20 69 6e 20 74 68 65 20 70   opcode in the p
2bf77 61 72 65 6e 74 20 56 44 42 45 2e 20 50 34 20 6f  arent VDBE. P4 o
2bf78 66 20 74 68 65 20 4f 50 5f 50 72 6f 67 72 61 6d  f the OP_Program
2bf79 20 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 61 20   .      ** is a 
2bf7a 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 73  pointer to the s
2bf7b 75 62 2d 76 64 62 65 20 63 6f 6e 74 61 69 6e 69  ub-vdbe containi
2bf7c 6e 67 20 74 68 65 20 74 72 69 67 67 65 72 20 70  ng the trigger p
2bf7d 72 6f 67 72 61 6d 2e 20 20 2a 2f 0a 20 20 20 20  rogram.  */.    
2bf7e 20 20 69 66 28 20 70 50 72 67 20 29 7b 0a 20 20    if( pPrg ){.  
2bf7f 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2bf80 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 50 72  eAddOp3(v, OP_Pr
2bf81 6f 67 72 61 6d 2c 20 6f 6c 64 49 64 78 2c 20 69  ogram, oldIdx, i
2bf82 67 6e 6f 72 65 4a 75 6d 70 2c 20 2b 2b 70 50 61  gnoreJump, ++pPa
2bf83 72 73 65 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20  rse->nMem);.    
2bf84 20 20 20 20 70 50 72 67 2d 3e 70 50 72 6f 67 72      pPrg->pProgr
2bf85 61 6d 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20  am->nRef++;.    
2bf86 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
2bf87 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 28  hangeP4(v, -1, (
2bf88 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 50 72  const char *)pPr
2bf89 67 2d 3e 70 50 72 6f 67 72 61 6d 2c 20 50 34 5f  g->pProgram, P4_
2bf8a 53 55 42 50 52 4f 47 52 41 4d 29 3b 0a 20 20 20  SUBPROGRAM);.   
2bf8b 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74       VdbeComment
2bf8c 28 28 76 2c 20 22 43 61 6c 6c 3a 20 25 73 2e 25  ((v, "Call: %s.%
2bf8d 73 22 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 20 6f 6e  s", p->zName, on
2bf8e 45 72 72 6f 72 54 65 78 74 28 6f 72 63 6f 6e 66  ErrorText(orconf
2bf8f 29 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  )));.      }.   
2bf90 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
2bf91 54 72 69 67 67 65 72 73 20 66 69 72 65 64 20 62  Triggers fired b
2bf92 79 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45  y UPDATE or DELE
2bf93 54 45 20 73 74 61 74 65 6d 65 6e 74 73 20 6d 61  TE statements ma
2bf94 79 20 61 63 63 65 73 73 20 76 61 6c 75 65 73 20  y access values 
2bf95 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 74 68 65  stored.** in the
2bf96 20 6f 6c 64 2e 2a 20 70 73 65 75 64 6f 2d 74 61   old.* pseudo-ta
2bf97 62 6c 65 2e 20 54 68 69 73 20 66 75 6e 63 74 69  ble. This functi
2bf98 6f 6e 20 72 65 74 75 72 6e 73 20 61 20 33 32 2d  on returns a 32-
2bf99 62 69 74 20 62 69 74 6d 61 73 6b 0a 2a 2a 20 69  bit bitmask.** i
2bf9a 6e 64 69 63 61 74 69 6e 67 20 77 68 69 63 68 20  ndicating which 
2bf9b 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 6f  columns of the o
2bf9c 6c 64 2e 2a 20 74 61 62 6c 65 20 61 63 74 75 61  ld.* table actua
2bf9d 6c 6c 79 20 61 72 65 20 75 73 65 64 20 62 79 0a  lly are used by.
2bf9e 2a 2a 20 74 72 69 67 67 65 72 73 2e 20 54 68 69  ** triggers. Thi
2bf9f 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6d 61  s information ma
2bfa0 79 20 62 65 20 75 73 65 64 20 62 79 20 74 68 65  y be used by the
2bfa1 20 63 61 6c 6c 65 72 20 74 6f 20 61 76 6f 69 64   caller to avoid
2bfa2 20 68 61 76 69 6e 67 0a 2a 2a 20 74 6f 20 6c 6f   having.** to lo
2bfa3 61 64 20 74 68 65 20 65 6e 74 69 72 65 20 6f 6c  ad the entire ol
2bfa4 64 2e 2a 20 72 65 63 6f 72 64 20 69 6e 74 6f 20  d.* record into 
2bfa5 6d 65 6d 6f 72 79 20 77 68 65 6e 20 65 78 65 63  memory when exec
2bfa6 75 74 69 6e 67 20 61 6e 20 55 50 44 41 54 45 0a  uting an UPDATE.
2bfa7 2a 2a 20 6f 72 20 44 45 4c 45 54 45 20 63 6f 6d  ** or DELETE com
2bfa8 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 42 69 74 20  mand..**.** Bit 
2bfa9 30 20 6f 66 20 74 68 65 20 72 65 74 75 72 6e 65  0 of the returne
2bfaa 64 20 6d 61 73 6b 20 69 73 20 73 65 74 20 69 66  d mask is set if
2bfab 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 63   the left-most c
2bfac 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 0a 2a 2a 20  olumn of the.** 
2bfad 74 61 62 6c 65 20 6d 61 79 20 62 65 20 61 63 63  table may be acc
2bfae 65 73 73 65 64 20 75 73 69 6e 67 20 61 6e 20 6f  essed using an o
2bfaf 6c 64 2e 3c 63 6f 6c 3e 20 72 65 66 65 72 65 6e  ld.<col> referen
2bfb0 63 65 2e 20 42 69 74 20 31 20 69 73 20 73 65 74  ce. Bit 1 is set
2bfb1 20 69 66 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e   if.** the secon
2bfb2 64 20 6c 65 66 74 6d 6f 73 74 20 63 6f 6c 75 6d  d leftmost colum
2bfb3 6e 20 76 61 6c 75 65 20 69 73 20 72 65 71 75 69  n value is requi
2bfb4 72 65 64 2c 20 61 6e 64 20 73 6f 20 6f 6e 2e 20  red, and so on. 
2bfb5 49 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20  If there.** are 
2bfb6 6d 6f 72 65 20 74 68 61 6e 20 33 32 20 63 6f 6c  more than 32 col
2bfb7 75 6d 6e 73 20 69 6e 20 74 68 65 20 74 61 62 6c  umns in the tabl
2bfb8 65 2c 20 61 6e 64 20 61 74 20 6c 65 61 73 74 20  e, and at least 
2bfb9 6f 6e 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d  one of the colum
2bfba 6e 73 0a 2a 2a 20 77 69 74 68 20 61 6e 20 69 6e  ns.** with an in
2bfbb 64 65 78 20 67 72 65 61 74 65 72 20 74 68 61 6e  dex greater than
2bfbc 20 33 32 20 6d 61 79 20 62 65 20 61 63 63 65 73   32 may be acces
2bfbd 73 65 64 2c 20 30 78 66 66 66 66 66 66 66 66 20  sed, 0xffffffff 
2bfbe 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
2bfbf 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73  ** It is not pos
2bfc0 73 69 62 6c 65 20 74 6f 20 64 65 74 65 72 6d 69  sible to determi
2bfc1 6e 65 20 69 66 20 74 68 65 20 6f 6c 64 2e 72 6f  ne if the old.ro
2bfc2 77 69 64 20 63 6f 6c 75 6d 6e 20 69 73 20 61 63  wid column is ac
2bfc3 63 65 73 73 65 64 0a 2a 2a 20 62 79 20 74 72 69  cessed.** by tri
2bfc4 67 67 65 72 73 2e 20 54 68 65 20 63 61 6c 6c 65  ggers. The calle
2bfc5 72 20 6d 75 73 74 20 61 6c 77 61 79 73 20 61 73  r must always as
2bfc6 73 75 6d 65 20 74 68 61 74 20 69 74 20 69 73 2e  sume that it is.
2bfc7 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20  .**.** There is 
2bfc8 6e 6f 20 65 71 75 69 76 61 6c 65 6e 74 20 66 75  no equivalent fu
2bfc9 6e 63 74 69 6f 6e 20 66 6f 72 20 6e 65 77 2e 2a  nction for new.*
2bfca 20 72 65 66 65 72 65 6e 63 65 73 2e 0a 2a 2f 0a   references..*/.
2bfcb 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75  SQLITE_PRIVATE u
2bfcc 33 32 20 73 71 6c 69 74 65 33 54 72 69 67 67 65  32 sqlite3Trigge
2bfcd 72 4f 6c 64 6d 61 73 6b 28 0a 20 20 50 61 72 73  rOldmask(.  Pars
2bfce 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
2bfcf 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78   /* Parse contex
2bfd0 74 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a  t */.  Trigger *
2bfd1 70 54 72 69 67 67 65 72 2c 20 20 20 2f 2a 20 4c  pTrigger,   /* L
2bfd2 69 73 74 20 6f 66 20 74 72 69 67 67 65 72 73 20  ist of triggers 
2bfd3 6f 6e 20 74 61 62 6c 65 20 70 54 61 62 20 2a 2f  on table pTab */
2bfd4 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20  .  int op,      
2bfd5 20 20 20 20 20 20 20 20 2f 2a 20 45 69 74 68 65          /* Eithe
2bfd6 72 20 54 4b 5f 55 50 44 41 54 45 20 6f 72 20 54  r TK_UPDATE or T
2bfd7 4b 5f 44 45 4c 45 54 45 20 2a 2f 0a 20 20 45 78  K_DELETE */.  Ex
2bfd8 70 72 4c 69 73 74 20 2a 70 43 68 61 6e 67 65 73  prList *pChanges
2bfd9 2c 20 20 2f 2a 20 43 68 61 6e 67 65 73 20 6c 69  ,  /* Changes li
2bfda 73 74 20 66 6f 72 20 61 6e 79 20 55 50 44 41 54  st for any UPDAT
2bfdb 45 20 4f 46 20 74 72 69 67 67 65 72 73 20 2a 2f  E OF triggers */
2bfdc 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20  .  Table *pTab, 
2bfdd 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
2bfde 61 62 6c 65 20 74 6f 20 63 6f 64 65 20 74 72 69  able to code tri
2bfdf 67 67 65 72 73 20 66 72 6f 6d 20 2a 2f 0a 20 20  ggers from */.  
2bfe0 69 6e 74 20 6f 72 63 6f 6e 66 20 20 20 20 20 20  int orconf      
2bfe1 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20       /* Default 
2bfe2 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c 69  ON CONFLICT poli
2bfe3 63 79 20 66 6f 72 20 74 72 69 67 67 65 72 20 73  cy for trigger s
2bfe4 74 65 70 73 20 2a 2f 0a 29 7b 0a 20 20 75 33 32  teps */.){.  u32
2bfe5 20 6d 61 73 6b 20 3d 20 30 3b 0a 20 20 54 72 69   mask = 0;.  Tri
2bfe6 67 67 65 72 20 2a 70 3b 0a 0a 20 20 61 73 73 65  gger *p;..  asse
2bfe7 72 74 28 6f 70 3d 3d 54 4b 5f 55 50 44 41 54 45  rt(op==TK_UPDATE
2bfe8 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 44 45 4c 45 54   || op==TK_DELET
2bfe9 45 29 3b 0a 20 20 66 6f 72 28 70 3d 70 54 72 69  E);.  for(p=pTri
2bfea 67 67 65 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e  gger; p; p=p->pN
2bfeb 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 2d  ext){.    if( p-
2bfec 3e 6f 70 3d 3d 6f 70 20 26 26 20 63 68 65 63 6b  >op==op && check
2bfed 43 6f 6c 75 6d 6e 4f 76 65 72 6c 61 70 28 70 2d  ColumnOverlap(p-
2bfee 3e 70 43 6f 6c 75 6d 6e 73 2c 70 43 68 61 6e 67  >pColumns,pChang
2bfef 65 73 29 20 29 7b 0a 20 20 20 20 20 20 54 72 69  es) ){.      Tri
2bff0 67 67 65 72 50 72 67 20 2a 70 50 72 67 3b 0a 20  ggerPrg *pPrg;. 
2bff1 20 20 20 20 20 70 50 72 67 20 3d 20 67 65 74 52       pPrg = getR
2bff2 6f 77 54 72 69 67 67 65 72 28 70 50 61 72 73 65  owTrigger(pParse
2bff3 2c 20 70 2c 20 70 54 61 62 2c 20 6f 72 63 6f 6e  , p, pTab, orcon
2bff4 66 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50  f);.      if( pP
2bff5 72 67 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 61  rg ){.        ma
2bff6 73 6b 20 7c 3d 20 70 50 72 67 2d 3e 6f 6c 64 6d  sk |= pPrg->oldm
2bff7 61 73 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ask;.      }.   
2bff8 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e   }.  }..  return
2bff9 20 6d 61 73 6b 3b 0a 7d 0a 0a 23 65 6e 64 69 66   mask;.}..#endif
2bffa 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c   /* !defined(SQL
2bffb 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
2bffc 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ) */../*********
2bffd 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 72 69  ***** End of tri
2bffe 67 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  gger.c *********
2bfff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c001 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
2c002 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
2c003 20 75 70 64 61 74 65 2e 63 20 2a 2a 2a 2a 2a 2a   update.c ******
2c004 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c005 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c006 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31  ****/./*.** 2001
2c007 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a   September 15.**
2c008 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
2c009 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
2c00a 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
2c00b 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
2c00c 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
2c00d 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
2c00e 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
2c00f 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
2c010 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
2c011 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
2c012 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
2c013 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
2c014 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
2c015 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
2c016 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
2c017 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
2c018 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
2c019 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
2c01a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c01b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c01c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c01d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
2c01e 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
2c01f 6e 73 20 43 20 63 6f 64 65 20 72 6f 75 74 69 6e  ns C code routin
2c020 65 73 20 74 68 61 74 20 61 72 65 20 63 61 6c 6c  es that are call
2c021 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72  ed by the parser
2c022 0a 2a 2a 20 74 6f 20 68 61 6e 64 6c 65 20 55 50  .** to handle UP
2c023 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 73 2e  DATE statements.
2c024 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 75 70 64 61  .**.** $Id: upda
2c025 74 65 2e 63 2c 76 20 31 2e 32 30 37 20 32 30 30  te.c,v 1.207 200
2c026 39 2f 30 38 2f 30 38 20 31 38 3a 30 31 3a 30 38  9/08/08 18:01:08
2c027 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 23   drh Exp $.*/..#
2c028 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2c029 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
2c02a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61  /* Forward decla
2c02b 72 61 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69 63  ration */.static
2c02c 20 76 6f 69 64 20 75 70 64 61 74 65 56 69 72 74   void updateVirt
2c02d 75 61 6c 54 61 62 6c 65 28 0a 20 20 50 61 72 73  ualTable(.  Pars
2c02e 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
2c02f 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20   /* The parsing 
2c030 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63  context */.  Src
2c031 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20  List *pSrc,     
2c032 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c    /* The virtual
2c033 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6d 6f 64   table to be mod
2c034 69 66 69 65 64 20 2a 2f 0a 20 20 54 61 62 6c 65  ified */.  Table
2c035 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20 20   *pTab,         
2c036 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 74  /* The virtual t
2c037 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69  able */.  ExprLi
2c038 73 74 20 2a 70 43 68 61 6e 67 65 73 2c 20 20 2f  st *pChanges,  /
2c039 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20 74 6f  * The columns to
2c03a 20 63 68 61 6e 67 65 20 69 6e 20 74 68 65 20 55   change in the U
2c03b 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20  PDATE statement 
2c03c 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 6f 77 69  */.  Expr *pRowi
2c03d 64 45 78 70 72 2c 20 20 20 20 2f 2a 20 45 78 70  dExpr,    /* Exp
2c03e 72 65 73 73 69 6f 6e 20 75 73 65 64 20 74 6f 20  ression used to 
2c03f 72 65 63 6f 6d 70 75 74 65 20 74 68 65 20 72 6f  recompute the ro
2c040 77 69 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 58  wid */.  int *aX
2c041 52 65 66 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  Ref,          /*
2c042 20 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 63 6f   Mapping from co
2c043 6c 75 6d 6e 73 20 6f 66 20 70 54 61 62 20 74 6f  lumns of pTab to
2c044 20 65 6e 74 72 69 65 73 20 69 6e 20 70 43 68 61   entries in pCha
2c045 6e 67 65 73 20 2a 2f 0a 20 20 45 78 70 72 20 2a  nges */.  Expr *
2c046 70 57 68 65 72 65 20 20 20 20 20 20 20 20 20 2f  pWhere         /
2c047 2a 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 6f  * WHERE clause o
2c048 66 20 74 68 65 20 55 50 44 41 54 45 20 73 74 61  f the UPDATE sta
2c049 74 65 6d 65 6e 74 20 2a 2f 0a 29 3b 0a 23 65 6e  tement */.);.#en
2c04a 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
2c04b 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20  IT_VIRTUALTABLE 
2c04c 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 6f  */../*.** The mo
2c04d 73 74 20 72 65 63 65 6e 74 6c 79 20 63 6f 64 65  st recently code
2c04e 64 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 61  d instruction wa
2c04f 73 20 61 6e 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74  s an OP_Column t
2c050 6f 20 72 65 74 72 69 65 76 65 20 74 68 65 0a 2a  o retrieve the.*
2c051 2a 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66  * i-th column of
2c052 20 74 61 62 6c 65 20 70 54 61 62 2e 20 54 68 69   table pTab. Thi
2c053 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74  s routine sets t
2c054 68 65 20 50 34 20 70 61 72 61 6d 65 74 65 72 20  he P4 parameter 
2c055 6f 66 20 74 68 65 20 0a 2a 2a 20 4f 50 5f 43 6f  of the .** OP_Co
2c056 6c 75 6d 6e 20 74 6f 20 74 68 65 20 64 65 66 61  lumn to the defa
2c057 75 6c 74 20 76 61 6c 75 65 2c 20 69 66 20 61 6e  ult value, if an
2c058 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66  y..**.** The def
2c059 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 61 20  ault value of a 
2c05a 63 6f 6c 75 6d 6e 20 69 73 20 73 70 65 63 69 66  column is specif
2c05b 69 65 64 20 62 79 20 61 20 44 45 46 41 55 4c 54  ied by a DEFAULT
2c05c 20 63 6c 61 75 73 65 20 69 6e 20 74 68 65 20 0a   clause in the .
2c05d 2a 2a 20 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69  ** column defini
2c05e 74 69 6f 6e 2e 20 54 68 69 73 20 77 61 73 20 65  tion. This was e
2c05f 69 74 68 65 72 20 73 75 70 70 6c 69 65 64 20 62  ither supplied b
2c060 79 20 74 68 65 20 75 73 65 72 20 77 68 65 6e 20  y the user when 
2c061 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20 77 61 73  the table.** was
2c062 20 63 72 65 61 74 65 64 2c 20 6f 72 20 61 64 64   created, or add
2c063 65 64 20 6c 61 74 65 72 20 74 6f 20 74 68 65 20  ed later to the 
2c064 74 61 62 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e  table definition
2c065 20 62 79 20 61 6e 20 41 4c 54 45 52 20 54 41 42   by an ALTER TAB
2c066 4c 45 0a 2a 2a 20 63 6f 6d 6d 61 6e 64 2e 20 49  LE.** command. I
2c067 66 20 74 68 65 20 6c 61 74 74 65 72 2c 20 74 68  f the latter, th
2c068 65 6e 20 74 68 65 20 72 6f 77 2d 72 65 63 6f 72  en the row-recor
2c069 64 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20  ds in the table 
2c06a 62 74 72 65 65 20 6f 6e 20 64 69 73 6b 0a 2a 2a  btree on disk.**
2c06b 20 6d 61 79 20 6e 6f 74 20 63 6f 6e 74 61 69 6e   may not contain
2c06c 20 61 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65   a value for the
2c06d 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 74 68 65 20   column and the 
2c06e 64 65 66 61 75 6c 74 20 76 61 6c 75 65 2c 20 74  default value, t
2c06f 61 6b 65 6e 0a 2a 2a 20 66 72 6f 6d 20 74 68 65  aken.** from the
2c070 20 50 34 20 70 61 72 61 6d 65 74 65 72 20 6f 66   P4 parameter of
2c071 20 74 68 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 69   the OP_Column i
2c072 6e 73 74 72 75 63 74 69 6f 6e 2c 20 69 73 20 72  nstruction, is r
2c073 65 74 75 72 6e 65 64 20 69 6e 73 74 65 61 64 2e  eturned instead.
2c074 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f 72 6d 65  .** If the forme
2c075 72 2c 20 74 68 65 6e 20 61 6c 6c 20 72 6f 77 2d  r, then all row-
2c076 72 65 63 6f 72 64 73 20 61 72 65 20 67 75 61 72  records are guar
2c077 61 6e 74 65 65 64 20 74 6f 20 69 6e 63 6c 75 64  anteed to includ
2c078 65 20 61 20 76 61 6c 75 65 0a 2a 2a 20 66 6f 72  e a value.** for
2c079 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61 6e 64 20   the column and 
2c07a 74 68 65 20 50 34 20 76 61 6c 75 65 20 69 73 20  the P4 value is 
2c07b 6e 6f 74 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a  not required..**
2c07c 0a 2a 2a 20 43 6f 6c 75 6d 6e 20 64 65 66 69 6e  .** Column defin
2c07d 69 74 69 6f 6e 73 20 63 72 65 61 74 65 64 20 62  itions created b
2c07e 79 20 61 6e 20 41 4c 54 45 52 20 54 41 42 4c 45  y an ALTER TABLE
2c07f 20 63 6f 6d 6d 61 6e 64 20 6d 61 79 20 6f 6e 6c   command may onl
2c080 79 20 68 61 76 65 20 0a 2a 2a 20 6c 69 74 65 72  y have .** liter
2c081 61 6c 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  al default value
2c082 73 20 73 70 65 63 69 66 69 65 64 3a 20 61 20 6e  s specified: a n
2c083 75 6d 62 65 72 2c 20 6e 75 6c 6c 20 6f 72 20 61  umber, null or a
2c084 20 73 74 72 69 6e 67 2e 20 28 49 66 20 61 20 6d   string. (If a m
2c085 6f 72 65 0a 2a 2a 20 63 6f 6d 70 6c 69 63 61 74  ore.** complicat
2c086 65 64 20 64 65 66 61 75 6c 74 20 65 78 70 72 65  ed default expre
2c087 73 73 69 6f 6e 20 76 61 6c 75 65 20 77 61 73 20  ssion value was 
2c088 70 72 6f 76 69 64 65 64 2c 20 69 74 20 69 73 20  provided, it is 
2c089 65 76 61 6c 75 61 74 65 64 20 0a 2a 2a 20 77 68  evaluated .** wh
2c08a 65 6e 20 74 68 65 20 41 4c 54 45 52 20 54 41 42  en the ALTER TAB
2c08b 4c 45 20 69 73 20 65 78 65 63 75 74 65 64 20 61  LE is executed a
2c08c 6e 64 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 69  nd one of the li
2c08d 74 65 72 61 6c 20 76 61 6c 75 65 73 20 77 72 69  teral values wri
2c08e 74 74 65 6e 0a 2a 2a 20 69 6e 74 6f 20 74 68 65  tten.** into the
2c08f 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74   sqlite_master t
2c090 61 62 6c 65 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65  able.).**.** The
2c091 72 65 66 6f 72 65 2c 20 74 68 65 20 50 34 20 70  refore, the P4 p
2c092 61 72 61 6d 65 74 65 72 20 69 73 20 6f 6e 6c 79  arameter is only
2c093 20 72 65 71 75 69 72 65 64 20 69 66 20 74 68 65   required if the
2c094 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66   default value f
2c095 6f 72 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e  or.** the column
2c096 20 69 73 20 61 20 6c 69 74 65 72 61 6c 20 6e 75   is a literal nu
2c097 6d 62 65 72 2c 20 73 74 72 69 6e 67 20 6f 72 20  mber, string or 
2c098 6e 75 6c 6c 2e 20 54 68 65 20 73 71 6c 69 74 65  null. The sqlite
2c099 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 29  3ValueFromExpr()
2c09a 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  .** function is 
2c09b 63 61 70 61 62 6c 65 20 6f 66 20 74 72 61 6e 73  capable of trans
2c09c 66 6f 72 6d 69 6e 67 20 74 68 65 73 65 20 74 79  forming these ty
2c09d 70 65 73 20 6f 66 20 65 78 70 72 65 73 73 69 6f  pes of expressio
2c09e 6e 73 20 69 6e 74 6f 0a 2a 2a 20 73 71 6c 69 74  ns into.** sqlit
2c09f 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73  e3_value objects
2c0a0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 72 61 6d  ..**.** If param
2c0a1 65 74 65 72 20 69 52 65 67 20 69 73 20 6e 6f 74  eter iReg is not
2c0a2 20 6e 65 67 61 74 69 76 65 2c 20 63 6f 64 65 20   negative, code 
2c0a3 61 6e 20 4f 50 5f 52 65 61 6c 41 66 66 69 6e 69  an OP_RealAffini
2c0a4 74 79 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a  ty instruction.*
2c0a5 2a 20 6f 6e 20 72 65 67 69 73 74 65 72 20 69 52  * on register iR
2c0a6 65 67 2e 20 54 68 69 73 20 69 73 20 75 73 65 64  eg. This is used
2c0a7 20 77 68 65 6e 20 61 6e 20 65 71 75 69 76 61 6c   when an equival
2c0a8 65 6e 74 20 69 6e 74 65 67 65 72 20 76 61 6c 75  ent integer valu
2c0a9 65 20 69 73 20 0a 2a 2a 20 73 74 6f 72 65 64 20  e is .** stored 
2c0aa 69 6e 20 70 6c 61 63 65 20 6f 66 20 61 6e 20 38  in place of an 8
2c0ab 2d 62 79 74 65 20 66 6c 6f 61 74 69 6e 67 20 70  -byte floating p
2c0ac 6f 69 6e 74 20 76 61 6c 75 65 20 69 6e 20 6f 72  oint value in or
2c0ad 64 65 72 20 74 6f 20 73 61 76 65 20 0a 2a 2a 20  der to save .** 
2c0ae 73 70 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  space..*/.SQLITE
2c0af 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2c0b0 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75  lite3ColumnDefau
2c0b1 6c 74 28 56 64 62 65 20 2a 76 2c 20 54 61 62 6c  lt(Vdbe *v, Tabl
2c0b2 65 20 2a 70 54 61 62 2c 20 69 6e 74 20 69 2c 20  e *pTab, int i, 
2c0b3 69 6e 74 20 69 52 65 67 29 7b 0a 20 20 61 73 73  int iReg){.  ass
2c0b4 65 72 74 28 20 70 54 61 62 21 3d 30 20 29 3b 0a  ert( pTab!=0 );.
2c0b5 20 20 69 66 28 20 21 70 54 61 62 2d 3e 70 53 65    if( !pTab->pSe
2c0b6 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  lect ){.    sqli
2c0b7 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 75  te3_value *pValu
2c0b8 65 3b 0a 20 20 20 20 75 38 20 65 6e 63 20 3d 20  e;.    u8 enc = 
2c0b9 45 4e 43 28 73 71 6c 69 74 65 33 56 64 62 65 44  ENC(sqlite3VdbeD
2c0ba 62 28 76 29 29 3b 0a 20 20 20 20 43 6f 6c 75 6d  b(v));.    Colum
2c0bb 6e 20 2a 70 43 6f 6c 20 3d 20 26 70 54 61 62 2d  n *pCol = &pTab-
2c0bc 3e 61 43 6f 6c 5b 69 5d 3b 0a 20 20 20 20 56 64  >aCol[i];.    Vd
2c0bd 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25  beComment((v, "%
2c0be 73 2e 25 73 22 2c 20 70 54 61 62 2d 3e 7a 4e 61  s.%s", pTab->zNa
2c0bf 6d 65 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29  me, pCol->zName)
2c0c0 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  );.    assert( i
2c0c1 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20  <pTab->nCol );. 
2c0c2 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46     sqlite3ValueF
2c0c3 72 6f 6d 45 78 70 72 28 73 71 6c 69 74 65 33 56  romExpr(sqlite3V
2c0c4 64 62 65 44 62 28 76 29 2c 20 70 43 6f 6c 2d 3e  dbeDb(v), pCol->
2c0c5 70 44 66 6c 74 2c 20 65 6e 63 2c 20 0a 20 20 20  pDflt, enc, .   
2c0c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c0c7 20 20 20 20 20 20 70 43 6f 6c 2d 3e 61 66 66 69        pCol->affi
2c0c8 6e 69 74 79 2c 20 26 70 56 61 6c 75 65 29 3b 0a  nity, &pValue);.
2c0c9 20 20 20 20 69 66 28 20 70 56 61 6c 75 65 20 29      if( pValue )
2c0ca 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
2c0cb 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d  dbeChangeP4(v, -
2c0cc 31 2c 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  1, (const char *
2c0cd 29 70 56 61 6c 75 65 2c 20 50 34 5f 4d 45 4d 29  )pValue, P4_MEM)
2c0ce 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20  ;.    }.#ifndef 
2c0cf 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41  SQLITE_OMIT_FLOA
2c0d0 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 69  TING_POINT.    i
2c0d1 66 28 20 69 52 65 67 3e 3d 30 20 26 26 20 70 54  f( iReg>=0 && pT
2c0d2 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 61 66 66 69  ab->aCol[i].affi
2c0d3 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46  nity==SQLITE_AFF
2c0d4 5f 52 45 41 4c 20 29 7b 0a 20 20 20 20 20 20 73  _REAL ){.      s
2c0d5 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
2c0d6 28 76 2c 20 4f 50 5f 52 65 61 6c 41 66 66 69 6e  (v, OP_RealAffin
2c0d7 69 74 79 2c 20 69 52 65 67 29 3b 0a 20 20 20 20  ity, iReg);.    
2c0d8 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a  }.#endif.  }.}..
2c0d9 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 20 61 6e  /*.** Process an
2c0da 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e   UPDATE statemen
2c0db 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 55 50 44 41 54  t..**.**   UPDAT
2c0dc 45 20 4f 52 20 49 47 4e 4f 52 45 20 74 61 62 6c  E OR IGNORE tabl
2c0dd 65 5f 77 78 79 7a 20 53 45 54 20 61 3d 62 2c 20  e_wxyz SET a=b, 
2c0de 63 3d 64 20 57 48 45 52 45 20 65 3c 35 20 41 4e  c=d WHERE e<5 AN
2c0df 44 20 66 20 4e 4f 54 20 4e 55 4c 4c 3b 0a 2a 2a  D f NOT NULL;.**
2c0e0 20 20 20 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f            \_____
2c0e1 5f 5f 2f 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20  __/ \________/  
2c0e2 20 20 20 5c 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20     \______/     
2c0e3 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f    \_____________
2c0e4 5f 5f 5f 2f 0a 2a 20 20 20 20 20 20 20 20 20 20  ___/.*          
2c0e5 20 20 6f 6e 45 72 72 6f 72 20 20 20 70 54 61 62    onError   pTab
2c0e6 4c 69 73 74 20 20 20 20 20 20 70 43 68 61 6e 67  List      pChang
2c0e7 65 73 20 20 20 20 20 20 20 20 20 20 20 20 20 70  es             p
2c0e8 57 68 65 72 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f  Where.*/.SQLITE_
2c0e9 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2c0ea 69 74 65 33 55 70 64 61 74 65 28 0a 20 20 50 61  ite3Update(.  Pa
2c0eb 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
2c0ec 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
2c0ed 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
2c0ee 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73  SrcList *pTabLis
2c0ef 74 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61  t,     /* The ta
2c0f0 62 6c 65 20 69 6e 20 77 68 69 63 68 20 77 65 20  ble in which we 
2c0f1 73 68 6f 75 6c 64 20 63 68 61 6e 67 65 20 74 68  should change th
2c0f2 69 6e 67 73 20 2a 2f 0a 20 20 45 78 70 72 4c 69  ings */.  ExprLi
2c0f3 73 74 20 2a 70 43 68 61 6e 67 65 73 2c 20 20 20  st *pChanges,   
2c0f4 20 2f 2a 20 54 68 69 6e 67 73 20 74 6f 20 62 65   /* Things to be
2c0f5 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20 45 78   changed */.  Ex
2c0f6 70 72 20 2a 70 57 68 65 72 65 2c 20 20 20 20 20  pr *pWhere,     
2c0f7 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52       /* The WHER
2c0f8 45 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20 62  E clause.  May b
2c0f9 65 20 6e 75 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20  e null */.  int 
2c0fa 6f 6e 45 72 72 6f 72 20 20 20 20 20 20 20 20 20  onError         
2c0fb 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 68 61 6e     /* How to han
2c0fc 64 6c 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 65  dle constraint e
2c0fd 72 72 6f 72 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e  rrors */.){.  in
2c0fe 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20  t i, j;         
2c0ff 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
2c100 6e 74 65 72 73 20 2a 2f 0a 20 20 54 61 62 6c 65  nters */.  Table
2c101 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20   *pTab;         
2c102 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74    /* The table t
2c103 6f 20 62 65 20 75 70 64 61 74 65 64 20 2a 2f 0a  o be updated */.
2c104 20 20 69 6e 74 20 61 64 64 72 20 3d 20 30 3b 20    int addr = 0; 
2c105 20 20 20 20 20 20 20 20 20 2f 2a 20 56 44 42 45           /* VDBE
2c106 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 64 64   instruction add
2c107 72 65 73 73 20 6f 66 20 74 68 65 20 73 74 61 72  ress of the star
2c108 74 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f  t of the loop */
2c109 0a 20 20 57 68 65 72 65 49 6e 66 6f 20 2a 70 57  .  WhereInfo *pW
2c10a 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20 49 6e 66  Info;     /* Inf
2c10b 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74  ormation about t
2c10c 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
2c10d 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20  */.  Vdbe *v;   
2c10e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2c10f 68 65 20 76 69 72 74 75 61 6c 20 64 61 74 61 62  he virtual datab
2c110 61 73 65 20 65 6e 67 69 6e 65 20 2a 2f 0a 20 20  ase engine */.  
2c111 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20  Index *pIdx;    
2c112 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f         /* For lo
2c113 6f 70 69 6e 67 20 6f 76 65 72 20 69 6e 64 69 63  oping over indic
2c114 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 64 78  es */.  int nIdx
2c115 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
2c116 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69 6e 64 69  * Number of indi
2c117 63 65 73 20 74 68 61 74 20 6e 65 65 64 20 75 70  ces that need up
2c118 64 61 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20  dating */.  int 
2c119 69 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 20  iCur;           
2c11a 20 20 20 2f 2a 20 56 44 42 45 20 43 75 72 73 6f     /* VDBE Curso
2c11b 72 20 6e 75 6d 62 65 72 20 6f 66 20 70 54 61 62  r number of pTab
2c11c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
2c11d 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  b;           /* 
2c11e 54 68 65 20 64 61 74 61 62 61 73 65 20 73 74 72  The database str
2c11f 75 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e 74 20  ucture */.  int 
2c120 2a 61 52 65 67 49 64 78 20 3d 20 30 3b 20 20 20  *aRegIdx = 0;   
2c121 20 20 20 2f 2a 20 4f 6e 65 20 72 65 67 69 73 74     /* One regist
2c122 65 72 20 61 73 73 69 67 6e 65 64 20 74 6f 20 65  er assigned to e
2c123 61 63 68 20 69 6e 64 65 78 20 74 6f 20 62 65 20  ach index to be 
2c124 75 70 64 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74  updated */.  int
2c125 20 2a 61 58 52 65 66 20 3d 20 30 3b 20 20 20 20   *aXRef = 0;    
2c126 20 20 20 20 2f 2a 20 61 58 52 65 66 5b 69 5d 20      /* aXRef[i] 
2c127 69 73 20 74 68 65 20 69 6e 64 65 78 20 69 6e 20  is the index in 
2c128 70 43 68 61 6e 67 65 73 2d 3e 61 5b 5d 20 6f 66  pChanges->a[] of
2c129 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20 20   the.           
2c12a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a                **
2c12b 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 66   an expression f
2c12c 6f 72 20 74 68 65 20 69 2d 74 68 20 63 6f 6c 75  or the i-th colu
2c12d 6d 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e  mn of the table.
2c12e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2c12f 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 58 52            ** aXR
2c130 65 66 5b 69 5d 3d 3d 2d 31 20 69 66 20 74 68 65  ef[i]==-1 if the
2c131 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 69 73 20   i-th column is 
2c132 6e 6f 74 20 63 68 61 6e 67 65 64 2e 20 2a 2f 0a  not changed. */.
2c133 20 20 69 6e 74 20 63 68 6e 67 52 6f 77 69 64 3b    int chngRowid;
2c134 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
2c135 20 69 66 20 74 68 65 20 72 65 63 6f 72 64 20 6e   if the record n
2c136 75 6d 62 65 72 20 69 73 20 62 65 69 6e 67 20 63  umber is being c
2c137 68 61 6e 67 65 64 20 2a 2f 0a 20 20 45 78 70 72  hanged */.  Expr
2c138 20 2a 70 52 6f 77 69 64 45 78 70 72 20 3d 20 30   *pRowidExpr = 0
2c139 3b 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e  ;  /* Expression
2c13a 20 64 65 66 69 6e 69 6e 67 20 74 68 65 20 6e 65   defining the ne
2c13b 77 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20  w record number 
2c13c 2a 2f 0a 20 20 69 6e 74 20 6f 70 65 6e 41 6c 6c  */.  int openAll
2c13d 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 54   = 0;       /* T
2c13e 72 75 65 20 69 66 20 61 6c 6c 20 69 6e 64 69 63  rue if all indic
2c13f 65 73 20 6e 65 65 64 20 74 6f 20 62 65 20 6f 70  es need to be op
2c140 65 6e 65 64 20 2a 2f 0a 20 20 41 75 74 68 43 6f  ened */.  AuthCo
2c141 6e 74 65 78 74 20 73 43 6f 6e 74 65 78 74 3b 20  ntext sContext; 
2c142 20 2f 2a 20 54 68 65 20 61 75 74 68 6f 72 69 7a   /* The authoriz
2c143 61 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f  ation context */
2c144 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73  .  NameContext s
2c145 4e 43 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65  NC;       /* The
2c146 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20 74 6f   name-context to
2c147 20 72 65 73 6f 6c 76 65 20 65 78 70 72 65 73 73   resolve express
2c148 69 6f 6e 73 20 69 6e 20 2a 2f 0a 20 20 69 6e 74  ions in */.  int
2c149 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20   iDb;           
2c14a 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
2c14b 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74  containing the t
2c14c 61 62 6c 65 20 62 65 69 6e 67 20 75 70 64 61 74  able being updat
2c14d 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6a 31 3b 20  ed */.  int j1; 
2c14e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2c14f 2a 20 41 64 64 72 65 73 73 65 73 20 6f 66 20 6a  * Addresses of j
2c150 75 6d 70 20 69 6e 73 74 72 75 63 74 69 6f 6e 73  ump instructions
2c151 20 2a 2f 0a 20 20 69 6e 74 20 6f 6b 4f 6e 65 50   */.  int okOneP
2c152 61 73 73 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ass;         /* 
2c153 54 72 75 65 20 66 6f 72 20 6f 6e 65 2d 70 61 73  True for one-pas
2c154 73 20 61 6c 67 6f 72 69 74 68 6d 20 77 69 74 68  s algorithm with
2c155 6f 75 74 20 74 68 65 20 46 49 46 4f 20 2a 2f 0a  out the FIFO */.
2c156 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2c157 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 69  OMIT_TRIGGER.  i
2c158 6e 74 20 69 73 56 69 65 77 3b 20 20 20 20 20 20  nt isView;      
2c159 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2c15a 72 79 69 6e 67 20 74 6f 20 75 70 64 61 74 65 20  rying to update 
2c15b 61 20 76 69 65 77 20 2a 2f 0a 20 20 54 72 69 67  a view */.  Trig
2c15c 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 20 20  ger *pTrigger;  
2c15d 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74           /* List
2c15e 20 6f 66 20 74 72 69 67 67 65 72 73 20 6f 6e 20   of triggers on 
2c15f 70 54 61 62 2c 20 69 66 20 72 65 71 75 69 72 65  pTab, if require
2c160 64 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 75 33  d */.#endif.  u3
2c161 32 20 6f 6c 64 6d 61 73 6b 20 3d 20 30 3b 20 20  2 oldmask = 0;  
2c162 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66        /* Mask of
2c163 20 4f 4c 44 2e 2a 20 63 6f 6c 75 6d 6e 73 20 69   OLD.* columns i
2c164 6e 20 75 73 65 20 2a 2f 0a 0a 20 20 2f 2a 20 52  n use */..  /* R
2c165 65 67 69 73 74 65 72 20 41 6c 6c 6f 63 61 74 69  egister Allocati
2c166 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  ons */.  int reg
2c167 52 6f 77 43 6f 75 6e 74 20 3d 20 30 3b 20 20 20  RowCount = 0;   
2c168 2f 2a 20 41 20 63 6f 75 6e 74 20 6f 66 20 72 6f  /* A count of ro
2c169 77 73 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20  ws changed */.  
2c16a 69 6e 74 20 72 65 67 4f 6c 64 52 6f 77 69 64 3b  int regOldRowid;
2c16b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 6c         /* The ol
2c16c 64 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69 6e 74  d rowid */.  int
2c16d 20 72 65 67 4e 65 77 52 6f 77 69 64 3b 20 20 20   regNewRowid;   
2c16e 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72      /* The new r
2c16f 6f 77 69 64 20 2a 2f 0a 20 20 69 6e 74 20 72 65  owid */.  int re
2c170 67 4e 65 77 3b 0a 20 20 69 6e 74 20 72 65 67 4f  gNew;.  int regO
2c171 6c 64 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 65  ld = 0;.  int re
2c172 67 52 6f 77 53 65 74 20 3d 20 30 3b 20 20 20 20  gRowSet = 0;    
2c173 20 2f 2a 20 52 6f 77 73 65 74 20 6f 66 20 72 6f   /* Rowset of ro
2c174 77 73 20 74 6f 20 62 65 20 75 70 64 61 74 65 64  ws to be updated
2c175 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 65 63   */.  int regRec
2c176 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
2c177 52 65 67 69 73 74 65 72 20 75 73 65 64 20 66 6f  Register used fo
2c178 72 20 6e 65 77 20 74 61 62 6c 65 20 72 65 63 6f  r new table reco
2c179 72 64 20 74 6f 20 69 6e 73 65 72 74 20 2a 2f 0a  rd to insert */.
2c17a 0a 20 20 6d 65 6d 73 65 74 28 26 73 43 6f 6e 74  .  memset(&sCont
2c17b 65 78 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73  ext, 0, sizeof(s
2c17c 43 6f 6e 74 65 78 74 29 29 3b 0a 20 20 64 62 20  Context));.  db 
2c17d 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
2c17e 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72  if( pParse->nErr
2c17f 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
2c180 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f  iled ){.    goto
2c181 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b   update_cleanup;
2c182 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
2c183 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31  TabList->nSrc==1
2c184 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65   );..  /* Locate
2c185 20 74 68 65 20 74 61 62 6c 65 20 77 68 69 63 68   the table which
2c186 20 77 65 20 77 61 6e 74 20 74 6f 20 75 70 64 61   we want to upda
2c187 74 65 2e 20 0a 20 20 2a 2f 0a 20 20 70 54 61 62  te. .  */.  pTab
2c188 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73   = sqlite3SrcLis
2c189 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c 20  tLookup(pParse, 
2c18a 70 54 61 62 4c 69 73 74 29 3b 0a 20 20 69 66 28  pTabList);.  if(
2c18b 20 70 54 61 62 3d 3d 30 20 29 20 67 6f 74 6f 20   pTab==0 ) goto 
2c18c 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a  update_cleanup;.
2c18d 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
2c18e 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61  chemaToIndex(pPa
2c18f 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70  rse->db, pTab->p
2c190 53 63 68 65 6d 61 29 3b 0a 0a 20 20 2f 2a 20 46  Schema);..  /* F
2c191 69 67 75 72 65 20 6f 75 74 20 69 66 20 77 65 20  igure out if we 
2c192 68 61 76 65 20 61 6e 79 20 74 72 69 67 67 65 72  have any trigger
2c193 73 20 61 6e 64 20 69 66 20 74 68 65 20 74 61 62  s and if the tab
2c194 6c 65 20 62 65 69 6e 67 0a 20 20 2a 2a 20 75 70  le being.  ** up
2c195 64 61 74 65 64 20 69 73 20 61 20 76 69 65 77 2e  dated is a view.
2c196 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  .  */.#ifndef SQ
2c197 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
2c198 52 0a 20 20 70 54 72 69 67 67 65 72 20 3d 20 73  R.  pTrigger = s
2c199 71 6c 69 74 65 33 54 72 69 67 67 65 72 73 45 78  qlite3TriggersEx
2c19a 69 73 74 28 70 50 61 72 73 65 2c 20 70 54 61 62  ist(pParse, pTab
2c19b 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 70 43 68  , TK_UPDATE, pCh
2c19c 61 6e 67 65 73 2c 20 30 29 3b 0a 20 20 69 73 56  anges, 0);.  isV
2c19d 69 65 77 20 3d 20 70 54 61 62 2d 3e 70 53 65 6c  iew = pTab->pSel
2c19e 65 63 74 21 3d 30 3b 0a 23 65 6c 73 65 0a 23 20  ect!=0;.#else.# 
2c19f 64 65 66 69 6e 65 20 70 54 72 69 67 67 65 72 20  define pTrigger 
2c1a0 30 0a 23 20 64 65 66 69 6e 65 20 69 73 56 69 65  0.# define isVie
2c1a1 77 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 64 65  w 0.#endif.#ifde
2c1a2 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
2c1a3 45 57 0a 23 20 75 6e 64 65 66 20 69 73 56 69 65  EW.# undef isVie
2c1a4 77 0a 23 20 64 65 66 69 6e 65 20 69 73 56 69 65  w.# define isVie
2c1a5 77 20 30 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66  w 0.#endif..  if
2c1a6 28 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74  ( sqlite3ViewGet
2c1a7 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72  ColumnNames(pPar
2c1a8 73 65 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20  se, pTab) ){.   
2c1a9 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65   goto update_cle
2c1aa 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20  anup;.  }.  if( 
2c1ab 73 71 6c 69 74 65 33 49 73 52 65 61 64 4f 6e 6c  sqlite3IsReadOnl
2c1ac 79 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  y(pParse, pTab, 
2c1ad 28 70 54 72 69 67 67 65 72 3f 31 3a 30 29 29 20  (pTrigger?1:0)) 
2c1ae 29 7b 0a 20 20 20 20 67 6f 74 6f 20 75 70 64 61  ){.    goto upda
2c1af 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a  te_cleanup;.  }.
2c1b0 20 20 61 58 52 65 66 20 3d 20 73 71 6c 69 74 65    aXRef = sqlite
2c1b1 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
2c1b2 20 73 69 7a 65 6f 66 28 69 6e 74 29 20 2a 20 70   sizeof(int) * p
2c1b3 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 69  Tab->nCol );.  i
2c1b4 66 28 20 61 58 52 65 66 3d 3d 30 20 29 20 67 6f  f( aXRef==0 ) go
2c1b5 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75  to update_cleanu
2c1b6 70 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  p;.  for(i=0; i<
2c1b7 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29  pTab->nCol; i++)
2c1b8 20 61 58 52 65 66 5b 69 5d 20 3d 20 2d 31 3b 0a   aXRef[i] = -1;.
2c1b9 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61  .  /* Allocate a
2c1ba 20 63 75 72 73 6f 72 73 20 66 6f 72 20 74 68 65   cursors for the
2c1bb 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 74   main database t
2c1bc 61 62 6c 65 20 61 6e 64 20 66 6f 72 20 61 6c 6c  able and for all
2c1bd 20 69 6e 64 69 63 65 73 2e 0a 20 20 2a 2a 20 54   indices..  ** T
2c1be 68 65 20 69 6e 64 65 78 20 63 75 72 73 6f 72 73  he index cursors
2c1bf 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 20 75 73   might not be us
2c1c0 65 64 2c 20 62 75 74 20 69 66 20 74 68 65 79 20  ed, but if they 
2c1c1 61 72 65 20 75 73 65 64 20 74 68 65 79 0a 20 20  are used they.  
2c1c2 2a 2a 20 6e 65 65 64 20 74 6f 20 6f 63 63 75 72  ** need to occur
2c1c3 20 72 69 67 68 74 20 61 66 74 65 72 20 74 68 65   right after the
2c1c4 20 64 61 74 61 62 61 73 65 20 63 75 72 73 6f 72   database cursor
2c1c5 2e 20 20 53 6f 20 67 6f 20 61 68 65 61 64 20 61  .  So go ahead a
2c1c6 6e 64 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65  nd.  ** allocate
2c1c7 20 65 6e 6f 75 67 68 20 73 70 61 63 65 2c 20 6a   enough space, j
2c1c8 75 73 74 20 69 6e 20 63 61 73 65 2e 0a 20 20 2a  ust in case..  *
2c1c9 2f 0a 20 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b  /.  pTabList->a[
2c1ca 30 5d 2e 69 43 75 72 73 6f 72 20 3d 20 69 43 75  0].iCursor = iCu
2c1cb 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  r = pParse->nTab
2c1cc 2b 2b 3b 0a 20 20 66 6f 72 28 70 49 64 78 3d 70  ++;.  for(pIdx=p
2c1cd 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64  Tab->pIndex; pId
2c1ce 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e  x; pIdx=pIdx->pN
2c1cf 65 78 74 29 7b 0a 20 20 20 20 70 50 61 72 73 65  ext){.    pParse
2c1d0 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 7d 0a 0a 20  ->nTab++;.  }.. 
2c1d1 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74   /* Initialize t
2c1d2 68 65 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20  he name-context 
2c1d3 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 73 4e 43  */.  memset(&sNC
2c1d4 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29  , 0, sizeof(sNC)
2c1d5 29 3b 0a 20 20 73 4e 43 2e 70 50 61 72 73 65 20  );.  sNC.pParse 
2c1d6 3d 20 70 50 61 72 73 65 3b 0a 20 20 73 4e 43 2e  = pParse;.  sNC.
2c1d7 70 53 72 63 4c 69 73 74 20 3d 20 70 54 61 62 4c  pSrcList = pTabL
2c1d8 69 73 74 3b 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c  ist;..  /* Resol
2c1d9 76 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61  ve the column na
2c1da 6d 65 73 20 69 6e 20 61 6c 6c 20 74 68 65 20 65  mes in all the e
2c1db 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 74 68  xpressions of th
2c1dc 65 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 55 50  e.  ** of the UP
2c1dd 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 20  DATE statement. 
2c1de 20 41 6c 73 6f 20 66 69 6e 64 20 74 68 65 20 63   Also find the c
2c1df 6f 6c 75 6d 6e 20 69 6e 64 65 78 0a 20 20 2a 2a  olumn index.  **
2c1e0 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e   for each column
2c1e1 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 20 69   to be updated i
2c1e2 6e 20 74 68 65 20 70 43 68 61 6e 67 65 73 20 61  n the pChanges a
2c1e3 72 72 61 79 2e 20 20 46 6f 72 20 65 61 63 68 0a  rray.  For each.
2c1e4 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 74 6f 20 62    ** column to b
2c1e5 65 20 75 70 64 61 74 65 64 2c 20 6d 61 6b 65 20  e updated, make 
2c1e6 73 75 72 65 20 77 65 20 68 61 76 65 20 61 75 74  sure we have aut
2c1e7 68 6f 72 69 7a 61 74 69 6f 6e 20 74 6f 20 63 68  horization to ch
2c1e8 61 6e 67 65 0a 20 20 2a 2a 20 74 68 61 74 20 63  ange.  ** that c
2c1e9 6f 6c 75 6d 6e 2e 0a 20 20 2a 2f 0a 20 20 63 68  olumn..  */.  ch
2c1ea 6e 67 52 6f 77 69 64 20 3d 20 30 3b 0a 20 20 66  ngRowid = 0;.  f
2c1eb 6f 72 28 69 3d 30 3b 20 69 3c 70 43 68 61 6e 67  or(i=0; i<pChang
2c1ec 65 73 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b  es->nExpr; i++){
2c1ed 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
2c1ee 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73  ResolveExprNames
2c1ef 28 26 73 4e 43 2c 20 70 43 68 61 6e 67 65 73 2d  (&sNC, pChanges-
2c1f0 3e 61 5b 69 5d 2e 70 45 78 70 72 29 20 29 7b 0a  >a[i].pExpr) ){.
2c1f1 20 20 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74        goto updat
2c1f2 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d  e_cleanup;.    }
2c1f3 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c  .    for(j=0; j<
2c1f4 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29  pTab->nCol; j++)
2c1f5 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  {.      if( sqli
2c1f6 74 65 33 53 74 72 49 43 6d 70 28 70 54 61 62 2d  te3StrICmp(pTab-
2c1f7 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c 20  >aCol[j].zName, 
2c1f8 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e 7a  pChanges->a[i].z
2c1f9 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20  Name)==0 ){.    
2c1fa 20 20 20 20 69 66 28 20 6a 3d 3d 70 54 61 62 2d      if( j==pTab-
2c1fb 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20  >iPKey ){.      
2c1fc 20 20 20 20 63 68 6e 67 52 6f 77 69 64 20 3d 20      chngRowid = 
2c1fd 31 3b 0a 20 20 20 20 20 20 20 20 20 20 70 52 6f  1;.          pRo
2c1fe 77 69 64 45 78 70 72 20 3d 20 70 43 68 61 6e 67  widExpr = pChang
2c1ff 65 73 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a  es->a[i].pExpr;.
2c200 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2c201 20 20 61 58 52 65 66 5b 6a 5d 20 3d 20 69 3b 0a    aXRef[j] = i;.
2c202 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2c203 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
2c204 20 69 66 28 20 6a 3e 3d 70 54 61 62 2d 3e 6e 43   if( j>=pTab->nC
2c205 6f 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ol ){.      if( 
2c206 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 70  sqlite3IsRowid(p
2c207 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e 7a 4e  Changes->a[i].zN
2c208 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20  ame) ){.        
2c209 63 68 6e 67 52 6f 77 69 64 20 3d 20 31 3b 0a 20  chngRowid = 1;. 
2c20a 20 20 20 20 20 20 20 70 52 6f 77 69 64 45 78 70         pRowidExp
2c20b 72 20 3d 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b  r = pChanges->a[
2c20c 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  i].pExpr;.      
2c20d 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73  }else{.        s
2c20e 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2c20f 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20  Parse, "no such 
2c210 63 6f 6c 75 6d 6e 3a 20 25 73 22 2c 20 70 43 68  column: %s", pCh
2c211 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d  anges->a[i].zNam
2c212 65 29 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f  e);.        goto
2c213 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b   update_cleanup;
2c214 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23  .      }.    }.#
2c215 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2c216 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e  IT_AUTHORIZATION
2c217 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 69 6e 74  .    {.      int
2c218 20 72 63 3b 0a 20 20 20 20 20 20 72 63 20 3d 20   rc;.      rc = 
2c219 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
2c21a 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f  (pParse, SQLITE_
2c21b 55 50 44 41 54 45 2c 20 70 54 61 62 2d 3e 7a 4e  UPDATE, pTab->zN
2c21c 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ame,.           
2c21d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c21e 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e  pTab->aCol[j].zN
2c21f 61 6d 65 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62  ame, db->aDb[iDb
2c220 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  ].zName);.      
2c221 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44  if( rc==SQLITE_D
2c222 45 4e 59 20 29 7b 0a 20 20 20 20 20 20 20 20 67  ENY ){.        g
2c223 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e  oto update_clean
2c224 75 70 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  up;.      }else 
2c225 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49  if( rc==SQLITE_I
2c226 47 4e 4f 52 45 20 29 7b 0a 20 20 20 20 20 20 20  GNORE ){.       
2c227 20 61 58 52 65 66 5b 6a 5d 20 3d 20 2d 31 3b 0a   aXRef[j] = -1;.
2c228 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65        }.    }.#e
2c229 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41  ndif.  }..  /* A
2c22a 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 66  llocate memory f
2c22b 6f 72 20 74 68 65 20 61 72 72 61 79 20 61 52 65  or the array aRe
2c22c 67 49 64 78 5b 5d 2e 20 20 54 68 65 72 65 20 69  gIdx[].  There i
2c22d 73 20 6f 6e 65 20 65 6e 74 72 79 20 69 6e 20 74  s one entry in t
2c22e 68 65 0a 20 20 2a 2a 20 61 72 72 61 79 20 66 6f  he.  ** array fo
2c22f 72 20 65 61 63 68 20 69 6e 64 65 78 20 61 73 73  r each index ass
2c230 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 61 62  ociated with tab
2c231 6c 65 20 62 65 69 6e 67 20 75 70 64 61 74 65 64  le being updated
2c232 2e 20 20 46 69 6c 6c 20 69 6e 0a 20 20 2a 2a 20  .  Fill in.  ** 
2c233 74 68 65 20 76 61 6c 75 65 20 77 69 74 68 20 61  the value with a
2c234 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72   register number
2c235 20 66 6f 72 20 69 6e 64 69 63 65 73 20 74 68 61   for indices tha
2c236 74 20 61 72 65 20 74 6f 20 62 65 20 75 73 65 64  t are to be used
2c237 0a 20 20 2a 2a 20 61 6e 64 20 77 69 74 68 20 7a  .  ** and with z
2c238 65 72 6f 20 66 6f 72 20 75 6e 75 73 65 64 20 69  ero for unused i
2c239 6e 64 69 63 65 73 2e 0a 20 20 2a 2f 0a 20 20 66  ndices..  */.  f
2c23a 6f 72 28 6e 49 64 78 3d 30 2c 20 70 49 64 78 3d  or(nIdx=0, pIdx=
2c23b 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49  pTab->pIndex; pI
2c23c 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70  dx; pIdx=pIdx->p
2c23d 4e 65 78 74 2c 20 6e 49 64 78 2b 2b 29 7b 7d 0a  Next, nIdx++){}.
2c23e 20 20 69 66 28 20 6e 49 64 78 3e 30 20 29 7b 0a    if( nIdx>0 ){.
2c23f 20 20 20 20 61 52 65 67 49 64 78 20 3d 20 73 71      aRegIdx = sq
2c240 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
2c241 28 64 62 2c 20 73 69 7a 65 6f 66 28 49 6e 64 65  (db, sizeof(Inde
2c242 78 2a 29 20 2a 20 6e 49 64 78 20 29 3b 0a 20 20  x*) * nIdx );.  
2c243 20 20 69 66 28 20 61 52 65 67 49 64 78 3d 3d 30    if( aRegIdx==0
2c244 20 29 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63   ) goto update_c
2c245 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 66 6f  leanup;.  }.  fo
2c246 72 28 6a 3d 30 2c 20 70 49 64 78 3d 70 54 61 62  r(j=0, pIdx=pTab
2c247 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
2c248 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
2c249 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20  , j++){.    int 
2c24a 72 65 67 3b 0a 20 20 20 20 69 66 28 20 63 68 6e  reg;.    if( chn
2c24b 67 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20  gRowid ){.      
2c24c 72 65 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  reg = ++pParse->
2c24d 6e 4d 65 6d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  nMem;.    }else{
2c24e 0a 20 20 20 20 20 20 72 65 67 20 3d 20 30 3b 0a  .      reg = 0;.
2c24f 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
2c250 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20  <pIdx->nColumn; 
2c251 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66  i++){.        if
2c252 28 20 61 58 52 65 66 5b 70 49 64 78 2d 3e 61 69  ( aXRef[pIdx->ai
2c253 43 6f 6c 75 6d 6e 5b 69 5d 5d 3e 3d 30 20 29 7b  Column[i]]>=0 ){
2c254 0a 20 20 20 20 20 20 20 20 20 20 72 65 67 20 3d  .          reg =
2c255 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
2c256 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
2c257 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2c258 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 52    }.    }.    aR
2c259 65 67 49 64 78 5b 6a 5d 20 3d 20 72 65 67 3b 0a  egIdx[j] = reg;.
2c25a 20 20 7d 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20    }..  /* Begin 
2c25b 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 2e  generating code.
2c25c 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65   */.  v = sqlite
2c25d 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
2c25e 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 67  ;.  if( v==0 ) g
2c25f 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e  oto update_clean
2c260 75 70 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65  up;.  if( pParse
2c261 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 20 73 71  ->nested==0 ) sq
2c262 6c 69 74 65 33 56 64 62 65 43 6f 75 6e 74 43 68  lite3VdbeCountCh
2c263 61 6e 67 65 73 28 76 29 3b 0a 20 20 73 71 6c 69  anges(v);.  sqli
2c264 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65  te3BeginWriteOpe
2c265 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 31  ration(pParse, 1
2c266 2c 20 69 44 62 29 3b 0a 0a 23 69 66 6e 64 65 66  , iDb);..#ifndef
2c267 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
2c268 54 55 41 4c 54 41 42 4c 45 0a 20 20 2f 2a 20 56  TUALTABLE.  /* V
2c269 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6d 75  irtual tables mu
2c26a 73 74 20 62 65 20 68 61 6e 64 6c 65 64 20 73 65  st be handled se
2c26b 70 61 72 61 74 65 6c 79 20 2a 2f 0a 20 20 69 66  parately */.  if
2c26c 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  ( IsVirtual(pTab
2c26d 29 20 29 7b 0a 20 20 20 20 75 70 64 61 74 65 56  ) ){.    updateV
2c26e 69 72 74 75 61 6c 54 61 62 6c 65 28 70 50 61 72  irtualTable(pPar
2c26f 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20 70 54  se, pTabList, pT
2c270 61 62 2c 20 70 43 68 61 6e 67 65 73 2c 20 70 52  ab, pChanges, pR
2c271 6f 77 69 64 45 78 70 72 2c 20 61 58 52 65 66 2c  owidExpr, aXRef,
2c272 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2c273 20 20 20 20 20 20 20 20 70 57 68 65 72 65 29 3b          pWhere);
2c274 0a 20 20 20 20 70 57 68 65 72 65 20 3d 20 30 3b  .    pWhere = 0;
2c275 0a 20 20 20 20 70 54 61 62 4c 69 73 74 20 3d 20  .    pTabList = 
2c276 30 3b 0a 20 20 20 20 67 6f 74 6f 20 75 70 64 61  0;.    goto upda
2c277 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a  te_cleanup;.  }.
2c278 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 6c 6c  #endif..  /* All
2c279 6f 63 61 74 65 20 72 65 71 75 69 72 65 64 20 72  ocate required r
2c27a 65 67 69 73 74 65 72 73 2e 20 2a 2f 0a 20 20 72  egisters. */.  r
2c27b 65 67 4f 6c 64 52 6f 77 69 64 20 3d 20 72 65 67  egOldRowid = reg
2c27c 4e 65 77 52 6f 77 69 64 20 3d 20 2b 2b 70 50 61  NewRowid = ++pPa
2c27d 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 69 66 28  rse->nMem;.  if(
2c27e 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20   pTrigger ){.   
2c27f 20 72 65 67 4f 6c 64 20 3d 20 70 50 61 72 73 65   regOld = pParse
2c280 2d 3e 6e 4d 65 6d 20 2b 20 31 3b 0a 20 20 20 20  ->nMem + 1;.    
2c281 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20  pParse->nMem += 
2c282 70 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 7d 0a  pTab->nCol;.  }.
2c283 20 20 69 66 28 20 63 68 6e 67 52 6f 77 69 64 20    if( chngRowid 
2c284 7c 7c 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20  || pTrigger ){. 
2c285 20 20 20 72 65 67 4e 65 77 52 6f 77 69 64 20 3d     regNewRowid =
2c286 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
2c287 0a 20 20 7d 0a 20 20 72 65 67 4e 65 77 20 3d 20  .  }.  regNew = 
2c288 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 20 31  pParse->nMem + 1
2c289 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  ;.  pParse->nMem
2c28a 20 2b 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c 3b 0a   += pTab->nCol;.
2c28b 20 20 72 65 67 52 65 63 20 3d 20 2b 2b 70 50 61    regRec = ++pPa
2c28c 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 0a 20 20 2f 2a  rse->nMem;..  /*
2c28d 20 53 74 61 72 74 20 74 68 65 20 76 69 65 77 20   Start the view 
2c28e 63 6f 6e 74 65 78 74 2e 20 2a 2f 0a 20 20 69 66  context. */.  if
2c28f 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20  ( isView ){.    
2c290 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65  sqlite3AuthConte
2c291 78 74 50 75 73 68 28 70 50 61 72 73 65 2c 20 26  xtPush(pParse, &
2c292 73 43 6f 6e 74 65 78 74 2c 20 70 54 61 62 2d 3e  sContext, pTab->
2c293 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f  zName);.  }..  /
2c294 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 61  * If there are a
2c295 6e 79 20 74 72 69 67 67 65 72 73 2c 20 73 65 74  ny triggers, set
2c296 20 6f 6c 64 6d 61 73 6b 20 61 6e 64 20 6e 65 77   oldmask and new
2c297 5f 63 6f 6c 5f 6d 61 73 6b 2e 20 2a 2f 0a 20 20  _col_mask. */.  
2c298 6f 6c 64 6d 61 73 6b 20 3d 20 73 71 6c 69 74 65  oldmask = sqlite
2c299 33 54 72 69 67 67 65 72 4f 6c 64 6d 61 73 6b 28  3TriggerOldmask(
2c29a 0a 20 20 20 20 20 20 70 50 61 72 73 65 2c 20 70  .      pParse, p
2c29b 54 72 69 67 67 65 72 2c 20 54 4b 5f 55 50 44 41  Trigger, TK_UPDA
2c29c 54 45 2c 20 70 43 68 61 6e 67 65 73 2c 20 70 54  TE, pChanges, pT
2c29d 61 62 2c 20 6f 6e 45 72 72 6f 72 29 3b 0a 0a 20  ab, onError);.. 
2c29e 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 74 72   /* If we are tr
2c29f 79 69 6e 67 20 74 6f 20 75 70 64 61 74 65 20 61  ying to update a
2c2a0 20 76 69 65 77 2c 20 72 65 61 6c 69 7a 65 20 74   view, realize t
2c2a1 68 61 74 20 76 69 65 77 20 69 6e 74 6f 0a 20 20  hat view into.  
2c2a2 2a 2a 20 61 20 65 70 68 65 6d 65 72 61 6c 20 74  ** a ephemeral t
2c2a3 61 62 6c 65 2e 0a 20 20 2a 2f 0a 23 69 66 20 21  able..  */.#if !
2c2a4 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
2c2a5 4d 49 54 5f 56 49 45 57 29 20 26 26 20 21 64 65  MIT_VIEW) && !de
2c2a6 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
2c2a7 54 5f 54 52 49 47 47 45 52 29 0a 20 20 69 66 28  T_TRIGGER).  if(
2c2a8 20 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 73   isView ){.    s
2c2a9 71 6c 69 74 65 33 4d 61 74 65 72 69 61 6c 69 7a  qlite3Materializ
2c2aa 65 56 69 65 77 28 70 50 61 72 73 65 2c 20 70 54  eView(pParse, pT
2c2ab 61 62 2c 20 70 57 68 65 72 65 2c 20 69 43 75 72  ab, pWhere, iCur
2c2ac 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  );.  }.#endif.. 
2c2ad 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20   /* Resolve the 
2c2ae 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 69 6e 20  column names in 
2c2af 61 6c 6c 20 74 68 65 20 65 78 70 72 65 73 73 69  all the expressi
2c2b0 6f 6e 73 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20  ons in the.  ** 
2c2b1 57 48 45 52 45 20 63 6c 61 75 73 65 2e 0a 20 20  WHERE clause..  
2c2b2 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
2c2b3 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73  ResolveExprNames
2c2b4 28 26 73 4e 43 2c 20 70 57 68 65 72 65 29 20 29  (&sNC, pWhere) )
2c2b5 7b 0a 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74  {.    goto updat
2c2b6 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a  e_cleanup;.  }..
2c2b7 20 20 2f 2a 20 42 65 67 69 6e 20 74 68 65 20 64    /* Begin the d
2c2b8 61 74 61 62 61 73 65 20 73 63 61 6e 0a 20 20 2a  atabase scan.  *
2c2b9 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  /.  sqlite3VdbeA
2c2ba 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp2(v, OP_Null
2c2bb 2c 20 30 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64  , 0, regOldRowid
2c2bc 29 3b 0a 20 20 70 57 49 6e 66 6f 20 3d 20 73 71  );.  pWInfo = sq
2c2bd 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28  lite3WhereBegin(
2c2be 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74  pParse, pTabList
2c2bf 2c 20 70 57 68 65 72 65 2c 30 2c 20 57 48 45 52  , pWhere,0, WHER
2c2c0 45 5f 4f 4e 45 50 41 53 53 5f 44 45 53 49 52 45  E_ONEPASS_DESIRE
2c2c1 44 29 3b 0a 20 20 69 66 28 20 70 57 49 6e 66 6f  D);.  if( pWInfo
2c2c2 3d 3d 30 20 29 20 67 6f 74 6f 20 75 70 64 61 74  ==0 ) goto updat
2c2c3 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 6f 6b 4f  e_cleanup;.  okO
2c2c4 6e 65 50 61 73 73 20 3d 20 70 57 49 6e 66 6f 2d  nePass = pWInfo-
2c2c5 3e 6f 6b 4f 6e 65 50 61 73 73 3b 0a 0a 20 20 2f  >okOnePass;..  /
2c2c6 2a 20 52 65 6d 65 6d 62 65 72 20 74 68 65 20 72  * Remember the r
2c2c7 6f 77 69 64 20 6f 66 20 65 76 65 72 79 20 69 74  owid of every it
2c2c8 65 6d 20 74 6f 20 62 65 20 75 70 64 61 74 65 64  em to be updated
2c2c9 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
2c2ca 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2c2cb 5f 52 6f 77 69 64 2c 20 69 43 75 72 2c 20 72 65  _Rowid, iCur, re
2c2cc 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 20 20 69 66  gOldRowid);.  if
2c2cd 28 20 21 6f 6b 4f 6e 65 50 61 73 73 20 29 7b 0a  ( !okOnePass ){.
2c2ce 20 20 20 20 72 65 67 52 6f 77 53 65 74 20 3d 20      regRowSet = 
2c2cf 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
2c2d0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2c2d1 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 53  ddOp2(v, OP_RowS
2c2d2 65 74 41 64 64 2c 20 72 65 67 52 6f 77 53 65 74  etAdd, regRowSet
2c2d3 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a  , regOldRowid);.
2c2d4 20 20 7d 0a 0a 20 20 2f 2a 20 45 6e 64 20 74 68    }..  /* End th
2c2d5 65 20 64 61 74 61 62 61 73 65 20 73 63 61 6e 20  e database scan 
2c2d6 6c 6f 6f 70 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  loop..  */.  sql
2c2d7 69 74 65 33 57 68 65 72 65 45 6e 64 28 70 57 49  ite3WhereEnd(pWI
2c2d8 6e 66 6f 29 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74  nfo);..  /* Init
2c2d9 69 61 6c 69 7a 65 20 74 68 65 20 63 6f 75 6e 74  ialize the count
2c2da 20 6f 66 20 75 70 64 61 74 65 64 20 72 6f 77 73   of updated rows
2c2db 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 64 62 2d  .  */.  if( (db-
2c2dc 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
2c2dd 43 6f 75 6e 74 52 6f 77 73 29 20 26 26 20 21 70  CountRows) && !p
2c2de 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54  Parse->pTriggerT
2c2df 61 62 20 29 7b 0a 20 20 20 20 72 65 67 52 6f 77  ab ){.    regRow
2c2e0 43 6f 75 6e 74 20 3d 20 2b 2b 70 50 61 72 73 65  Count = ++pParse
2c2e1 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69  ->nMem;.    sqli
2c2e2 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2c2e3 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20   OP_Integer, 0, 
2c2e4 72 65 67 52 6f 77 43 6f 75 6e 74 29 3b 0a 20 20  regRowCount);.  
2c2e5 7d 0a 0a 20 20 69 66 28 20 21 69 73 56 69 65 77  }..  if( !isView
2c2e6 20 29 7b 0a 20 20 20 20 2f 2a 20 0a 20 20 20 20   ){.    /* .    
2c2e7 2a 2a 20 4f 70 65 6e 20 65 76 65 72 79 20 69 6e  ** Open every in
2c2e8 64 65 78 20 74 68 61 74 20 6e 65 65 64 73 20 75  dex that needs u
2c2e9 70 64 61 74 69 6e 67 2e 20 20 4e 6f 74 65 20 74  pdating.  Note t
2c2ea 68 61 74 20 69 66 20 61 6e 79 0a 20 20 20 20 2a  hat if any.    *
2c2eb 2a 20 69 6e 64 65 78 20 63 6f 75 6c 64 20 70 6f  * index could po
2c2ec 74 65 6e 74 69 61 6c 6c 79 20 69 6e 76 6f 6b 65  tentially invoke
2c2ed 20 61 20 52 45 50 4c 41 43 45 20 63 6f 6e 66 6c   a REPLACE confl
2c2ee 69 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 0a  ict resolution .
2c2ef 20 20 20 20 2a 2a 20 61 63 74 69 6f 6e 2c 20 74      ** action, t
2c2f0 68 65 6e 20 77 65 20 6e 65 65 64 20 74 6f 20 6f  hen we need to o
2c2f1 70 65 6e 20 61 6c 6c 20 69 6e 64 69 63 65 73 20  pen all indices 
2c2f2 62 65 63 61 75 73 65 20 77 65 20 6d 69 67 68 74  because we might
2c2f3 20 6e 65 65 64 0a 20 20 20 20 2a 2a 20 74 6f 20   need.    ** to 
2c2f4 62 65 20 64 65 6c 65 74 69 6e 67 20 73 6f 6d 65  be deleting some
2c2f5 20 72 65 63 6f 72 64 73 2e 0a 20 20 20 20 2a 2f   records..    */
2c2f6 0a 20 20 20 20 69 66 28 20 21 6f 6b 4f 6e 65 50  .    if( !okOneP
2c2f7 61 73 73 20 29 20 73 71 6c 69 74 65 33 4f 70 65  ass ) sqlite3Ope
2c2f8 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69  nTable(pParse, i
2c2f9 43 75 72 2c 20 69 44 62 2c 20 70 54 61 62 2c 20  Cur, iDb, pTab, 
2c2fa 4f 50 5f 4f 70 65 6e 57 72 69 74 65 29 3b 20 0a  OP_OpenWrite); .
2c2fb 20 20 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d      if( onError=
2c2fc 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29 7b 0a 20  =OE_Replace ){. 
2c2fd 20 20 20 20 20 6f 70 65 6e 41 6c 6c 20 3d 20 31       openAll = 1
2c2fe 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
2c2ff 20 20 20 6f 70 65 6e 41 6c 6c 20 3d 20 30 3b 0a     openAll = 0;.
2c300 20 20 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70        for(pIdx=p
2c301 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64  Tab->pIndex; pId
2c302 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e  x; pIdx=pIdx->pN
2c303 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20 69 66  ext){.        if
2c304 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 3d  ( pIdx->onError=
2c305 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29 7b 0a 20  =OE_Replace ){. 
2c306 20 20 20 20 20 20 20 20 20 6f 70 65 6e 41 6c 6c           openAll
2c307 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20   = 1;.          
2c308 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
2c309 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2c30a 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 64 78     for(i=0, pIdx
2c30b 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
2c30c 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
2c30d 70 4e 65 78 74 2c 20 69 2b 2b 29 7b 0a 20 20 20  pNext, i++){.   
2c30e 20 20 20 69 66 28 20 6f 70 65 6e 41 6c 6c 20 7c     if( openAll |
2c30f 7c 20 61 52 65 67 49 64 78 5b 69 5d 3e 30 20 29  | aRegIdx[i]>0 )
2c310 7b 0a 20 20 20 20 20 20 20 20 4b 65 79 49 6e 66  {.        KeyInf
2c311 6f 20 2a 70 4b 65 79 20 3d 20 73 71 6c 69 74 65  o *pKey = sqlite
2c312 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50  3IndexKeyinfo(pP
2c313 61 72 73 65 2c 20 70 49 64 78 29 3b 0a 20 20 20  arse, pIdx);.   
2c314 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2c315 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65  AddOp4(v, OP_Ope
2c316 6e 57 72 69 74 65 2c 20 69 43 75 72 2b 69 2b 31  nWrite, iCur+i+1
2c317 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44  , pIdx->tnum, iD
2c318 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  b,.             
2c319 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a            (char*
2c31a 29 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46  )pKey, P4_KEYINF
2c31b 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20  O_HANDOFF);.    
2c31c 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72      assert( pPar
2c31d 73 65 2d 3e 6e 54 61 62 3e 69 43 75 72 2b 69 2b  se->nTab>iCur+i+
2c31e 31 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  1 );.      }.   
2c31f 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 6f 70   }.  }..  /* Top
2c320 20 6f 66 20 74 68 65 20 75 70 64 61 74 65 20 6c   of the update l
2c321 6f 6f 70 20 2a 2f 0a 20 20 69 66 28 20 6f 6b 4f  oop */.  if( okO
2c322 6e 65 50 61 73 73 20 29 7b 0a 20 20 20 20 69 6e  nePass ){.    in
2c323 74 20 61 31 20 3d 20 73 71 6c 69 74 65 33 56 64  t a1 = sqlite3Vd
2c324 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e  beAddOp1(v, OP_N
2c325 6f 74 4e 75 6c 6c 2c 20 72 65 67 4f 6c 64 52 6f  otNull, regOldRo
2c326 77 69 64 29 3b 0a 20 20 20 20 61 64 64 72 20 3d  wid);.    addr =
2c327 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2c328 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0a  p0(v, OP_Goto);.
2c329 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
2c32a 75 6d 70 48 65 72 65 28 76 2c 20 61 31 29 3b 0a  umpHere(v, a1);.
2c32b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 64 64    }else{.    add
2c32c 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
2c32d 64 64 4f 70 33 28 76 2c 20 4f 50 5f 52 6f 77 53  ddOp3(v, OP_RowS
2c32e 65 74 52 65 61 64 2c 20 72 65 67 52 6f 77 53 65  etRead, regRowSe
2c32f 74 2c 20 30 2c 20 72 65 67 4f 6c 64 52 6f 77 69  t, 0, regOldRowi
2c330 64 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61  d);.  }..  /* Ma
2c331 6b 65 20 63 75 72 73 6f 72 20 69 43 75 72 20 70  ke cursor iCur p
2c332 6f 69 6e 74 20 74 6f 20 74 68 65 20 72 65 63 6f  oint to the reco
2c333 72 64 20 74 68 61 74 20 69 73 20 62 65 69 6e 67  rd that is being
2c334 20 75 70 64 61 74 65 64 2e 20 49 66 0a 20 20 2a   updated. If.  *
2c335 2a 20 74 68 69 73 20 72 65 63 6f 72 64 20 64 6f  * this record do
2c336 65 73 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72  es not exist for
2c337 20 73 6f 6d 65 20 72 65 61 73 6f 6e 20 28 64 65   some reason (de
2c338 6c 65 74 65 64 20 62 79 20 61 20 74 72 69 67 67  leted by a trigg
2c339 65 72 2c 0a 20 20 2a 2a 20 66 6f 72 20 65 78 61  er,.  ** for exa
2c33a 6d 70 6c 65 2c 20 74 68 65 6e 20 6a 75 6d 70 20  mple, then jump 
2c33b 74 6f 20 74 68 65 20 6e 65 78 74 20 69 74 65 72  to the next iter
2c33c 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 52 6f 77  ation of the Row
2c33d 53 65 74 20 6c 6f 6f 70 2e 20 20 2a 2f 0a 20 20  Set loop.  */.  
2c33e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2c33f 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74  3(v, OP_NotExist
2c340 73 2c 20 69 43 75 72 2c 20 61 64 64 72 2c 20 72  s, iCur, addr, r
2c341 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 0a 20 20  egOldRowid);..  
2c342 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20  /* If there are 
2c343 74 72 69 67 67 65 72 73 20 6f 6e 20 74 68 69 73  triggers on this
2c344 20 74 61 62 6c 65 2c 20 70 6f 70 75 6c 61 74 65   table, populate
2c345 20 61 6e 20 61 72 72 61 79 20 6f 66 20 72 65 67   an array of reg
2c346 69 73 74 65 72 73 20 0a 20 20 2a 2a 20 77 69 74  isters .  ** wit
2c347 68 20 74 68 65 20 72 65 71 75 69 72 65 64 20 6f  h the required o
2c348 6c 64 2e 2a 20 63 6f 6c 75 6d 6e 20 64 61 74 61  ld.* column data
2c349 2e 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 72 69  .  */.  if( pTri
2c34a 67 67 65 72 20 29 7b 0a 20 20 20 20 66 6f 72 28  gger ){.    for(
2c34b 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f  i=0; i<pTab->nCo
2c34c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  l; i++){.      i
2c34d 66 28 20 61 58 52 65 66 5b 69 5d 3c 30 20 7c 7c  f( aXRef[i]<0 ||
2c34e 20 6f 6c 64 6d 61 73 6b 3d 3d 30 78 66 66 66 66   oldmask==0xffff
2c34f 66 66 66 66 20 7c 7c 20 28 6f 6c 64 6d 61 73 6b  ffff || (oldmask
2c350 20 26 20 28 31 3c 3c 69 29 29 20 29 7b 0a 20 20   & (1<<i)) ){.  
2c351 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2c352 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
2c353 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 69 2c 20 72  lumn, iCur, i, r
2c354 65 67 4f 6c 64 2b 69 29 3b 0a 20 20 20 20 20 20  egOld+i);.      
2c355 20 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44    sqlite3ColumnD
2c356 65 66 61 75 6c 74 28 76 2c 20 70 54 61 62 2c 20  efault(v, pTab, 
2c357 69 2c 20 72 65 67 4f 6c 64 2b 69 29 3b 0a 20 20  i, regOld+i);.  
2c358 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2c359 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2c35a 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp2(v, OP_Null,
2c35b 20 30 2c 20 72 65 67 4f 6c 64 2b 69 29 3b 0a 20   0, regOld+i);. 
2c35c 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
2c35d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 72 65  ..  /* If the re
2c35e 63 6f 72 64 20 6e 75 6d 62 65 72 20 77 69 6c 6c  cord number will
2c35f 20 63 68 61 6e 67 65 2c 20 73 65 74 20 72 65 67   change, set reg
2c360 69 73 74 65 72 20 72 65 67 4e 65 77 52 6f 77 69  ister regNewRowi
2c361 64 20 74 6f 0a 20 20 2a 2a 20 63 6f 6e 74 61 69  d to.  ** contai
2c362 6e 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 2e  n the new value.
2c363 20 49 66 20 74 68 65 20 72 65 63 6f 72 64 20 6e   If the record n
2c364 75 6d 62 65 72 20 69 73 20 6e 6f 74 20 62 65 69  umber is not bei
2c365 6e 67 20 6d 6f 64 69 66 69 65 64 2c 0a 20 20 2a  ng modified,.  *
2c366 2a 20 74 68 65 6e 20 72 65 67 4e 65 77 52 6f 77  * then regNewRow
2c367 69 64 20 69 73 20 74 68 65 20 73 61 6d 65 20 72  id is the same r
2c368 65 67 69 73 74 65 72 20 61 73 20 72 65 67 4f 6c  egister as regOl
2c369 64 52 6f 77 69 64 2c 20 77 68 69 63 68 20 69 73  dRowid, which is
2c36a 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 70 6f  .  ** already po
2c36b 70 75 6c 61 74 65 64 2e 20 20 2a 2f 0a 20 20 61  pulated.  */.  a
2c36c 73 73 65 72 74 28 20 63 68 6e 67 52 6f 77 69 64  ssert( chngRowid
2c36d 20 7c 7c 20 70 54 72 69 67 67 65 72 20 7c 7c 20   || pTrigger || 
2c36e 72 65 67 4f 6c 64 52 6f 77 69 64 3d 3d 72 65 67  regOldRowid==reg
2c36f 4e 65 77 52 6f 77 69 64 20 29 3b 0a 20 20 69 66  NewRowid );.  if
2c370 28 20 63 68 6e 67 52 6f 77 69 64 20 29 7b 0a 20  ( chngRowid ){. 
2c371 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
2c372 64 65 28 70 50 61 72 73 65 2c 20 70 52 6f 77 69  de(pParse, pRowi
2c373 64 45 78 70 72 2c 20 72 65 67 4e 65 77 52 6f 77  dExpr, regNewRow
2c374 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  id);.    sqlite3
2c375 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
2c376 5f 4d 75 73 74 42 65 49 6e 74 2c 20 72 65 67 4e  _MustBeInt, regN
2c377 65 77 52 6f 77 69 64 29 3b 0a 20 20 7d 65 6c 73  ewRowid);.  }els
2c378 65 20 69 66 28 20 70 54 72 69 67 67 65 72 20 29  e if( pTrigger )
2c379 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
2c37a 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f  eAddOp2(v, OP_Co
2c37b 70 79 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 2c  py, regOldRowid,
2c37c 20 72 65 67 4e 65 77 52 6f 77 69 64 29 3b 0a 20   regNewRowid);. 
2c37d 20 7d 0a 0a 20 20 2f 2a 20 50 6f 70 75 6c 61 74   }..  /* Populat
2c37e 65 20 74 68 65 20 61 72 72 61 79 20 6f 66 20 72  e the array of r
2c37f 65 67 69 73 74 65 72 73 20 62 65 67 69 6e 6e 69  egisters beginni
2c380 6e 67 20 61 74 20 72 65 67 4e 65 77 20 77 69 74  ng at regNew wit
2c381 68 20 74 68 65 20 6e 65 77 0a 20 20 2a 2a 20 72  h the new.  ** r
2c382 6f 77 20 64 61 74 61 2e 20 54 68 69 73 20 61 72  ow data. This ar
2c383 72 61 79 20 69 73 20 75 73 65 64 20 74 6f 20 63  ray is used to c
2c384 68 65 63 6b 20 63 6f 6e 73 74 61 69 6e 74 73 2c  heck constaints,
2c385 20 63 72 65 61 74 65 20 74 68 65 20 6e 65 77 0a   create the new.
2c386 20 20 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 69    ** table and i
2c387 6e 64 65 78 20 72 65 63 6f 72 64 73 2c 20 61 6e  ndex records, an
2c388 64 20 61 73 20 74 68 65 20 76 61 6c 75 65 73 20  d as the values 
2c389 66 6f 72 20 61 6e 79 20 6e 65 77 2e 2a 20 72 65  for any new.* re
2c38a 66 65 72 65 6e 63 65 73 0a 20 20 2a 2a 20 6d 61  ferences.  ** ma
2c38b 64 65 20 62 79 20 74 72 69 67 67 65 72 73 2e 20  de by triggers. 
2c38c 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   */.  for(i=0; i
2c38d 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b  <pTab->nCol; i++
2c38e 29 7b 0a 20 20 20 20 69 66 28 20 69 3d 3d 70 54  ){.    if( i==pT
2c38f 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20  ab->iPKey ){.   
2c390 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2c391 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp2(v, OP_Null,
2c392 20 30 2c 20 72 65 67 4e 65 77 2b 69 29 3b 0a 20   0, regNew+i);. 
2c393 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2c394 6a 20 3d 20 61 58 52 65 66 5b 69 5d 3b 0a 20 20  j = aXRef[i];.  
2c395 20 20 20 20 69 66 28 20 6a 3c 30 20 29 7b 0a 20      if( j<0 ){. 
2c396 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2c397 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43  beAddOp3(v, OP_C
2c398 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 69 2c 20  olumn, iCur, i, 
2c399 72 65 67 4e 65 77 2b 69 29 3b 0a 20 20 20 20 20  regNew+i);.     
2c39a 20 20 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e     sqlite3Column
2c39b 44 65 66 61 75 6c 74 28 76 2c 20 70 54 61 62 2c  Default(v, pTab,
2c39c 20 69 2c 20 72 65 67 4e 65 77 2b 69 29 3b 0a 20   i, regNew+i);. 
2c39d 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2c39e 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
2c39f 6f 64 65 28 70 50 61 72 73 65 2c 20 70 43 68 61  ode(pParse, pCha
2c3a0 6e 67 65 73 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72  nges->a[j].pExpr
2c3a1 2c 20 72 65 67 4e 65 77 2b 69 29 3b 0a 20 20 20  , regNew+i);.   
2c3a2 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
2c3a3 20 20 2f 2a 20 46 69 72 65 20 61 6e 79 20 42 45    /* Fire any BE
2c3a4 46 4f 52 45 20 55 50 44 41 54 45 20 74 72 69 67  FORE UPDATE trig
2c3a5 67 65 72 73 2e 20 54 68 69 73 20 68 61 70 70 65  gers. This happe
2c3a6 6e 73 20 62 65 66 6f 72 65 20 63 6f 6e 73 74 72  ns before constr
2c3a7 61 69 6e 74 73 20 61 72 65 0a 20 20 2a 2a 20 76  aints are.  ** v
2c3a8 65 72 69 66 69 65 64 2e 20 4f 6e 65 20 63 6f 75  erified. One cou
2c3a9 6c 64 20 61 72 67 75 65 20 74 68 61 74 20 74 68  ld argue that th
2c3aa 69 73 20 69 73 20 77 72 6f 6e 67 2e 20 20 2a 2f  is is wrong.  */
2c3ab 0a 20 20 69 66 28 20 70 54 72 69 67 67 65 72 20  .  if( pTrigger 
2c3ac 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
2c3ad 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41  beAddOp2(v, OP_A
2c3ae 66 66 69 6e 69 74 79 2c 20 72 65 67 4e 65 77 2c  ffinity, regNew,
2c3af 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20   pTab->nCol);.  
2c3b0 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 41 66    sqlite3TableAf
2c3b1 66 69 6e 69 74 79 53 74 72 28 76 2c 20 70 54 61  finityStr(v, pTa
2c3b2 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43  b);.    sqlite3C
2c3b3 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 70 50  odeRowTrigger(pP
2c3b4 61 72 73 65 2c 20 70 54 72 69 67 67 65 72 2c 20  arse, pTrigger, 
2c3b5 54 4b 5f 55 50 44 41 54 45 2c 20 70 43 68 61 6e  TK_UPDATE, pChan
2c3b6 67 65 73 2c 20 0a 20 20 20 20 20 20 20 20 54 52  ges, .        TR
2c3b7 49 47 47 45 52 5f 42 45 46 4f 52 45 2c 20 70 54  IGGER_BEFORE, pT
2c3b8 61 62 2c 20 2d 31 2c 20 72 65 67 4f 6c 64 52 6f  ab, -1, regOldRo
2c3b9 77 69 64 2c 20 6f 6e 45 72 72 6f 72 2c 20 61 64  wid, onError, ad
2c3ba 64 72 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65  dr);..    /* The
2c3bb 20 72 6f 77 2d 74 72 69 67 67 65 72 20 6d 61 79   row-trigger may
2c3bc 20 68 61 76 65 20 64 65 6c 65 74 65 64 20 74 68   have deleted th
2c3bd 65 20 72 6f 77 20 62 65 69 6e 67 20 75 70 64 61  e row being upda
2c3be 74 65 64 2e 20 49 6e 20 74 68 69 73 0a 20 20 20  ted. In this.   
2c3bf 20 2a 2a 20 63 61 73 65 2c 20 6a 75 6d 70 20 74   ** case, jump t
2c3c0 6f 20 74 68 65 20 6e 65 78 74 20 72 6f 77 2e 20  o the next row. 
2c3c1 4e 6f 20 75 70 64 61 74 65 73 20 6f 72 20 41 46  No updates or AF
2c3c2 54 45 52 20 74 72 69 67 67 65 72 73 20 61 72 65  TER triggers are
2c3c3 20 0a 20 20 20 20 2a 2a 20 72 65 71 75 69 72 65   .    ** require
2c3c4 64 2e 20 54 68 69 73 20 62 65 68 61 76 69 6f 75  d. This behaviou
2c3c5 72 20 2d 20 77 68 61 74 20 68 61 70 70 65 6e 73  r - what happens
2c3c6 20 77 68 65 6e 20 74 68 65 20 72 6f 77 20 62 65   when the row be
2c3c7 69 6e 67 20 75 70 64 61 74 65 64 0a 20 20 20 20  ing updated.    
2c3c8 2a 2a 20 69 73 20 64 65 6c 65 74 65 64 20 6f 72  ** is deleted or
2c3c9 20 72 65 6e 61 6d 65 64 20 62 79 20 61 20 42 45   renamed by a BE
2c3ca 46 4f 52 45 20 74 72 69 67 67 65 72 20 2d 20 69  FORE trigger - i
2c3cb 73 20 6c 65 66 74 20 75 6e 64 65 66 69 6e 65 64  s left undefined
2c3cc 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 64   in the.    ** d
2c3cd 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 2a  ocumentation.  *
2c3ce 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  /.    sqlite3Vdb
2c3cf 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f  eAddOp3(v, OP_No
2c3d0 74 45 78 69 73 74 73 2c 20 69 43 75 72 2c 20 61  tExists, iCur, a
2c3d1 64 64 72 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64  ddr, regOldRowid
2c3d2 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 69  );.  }..  if( !i
2c3d3 73 56 69 65 77 20 29 7b 0a 0a 20 20 20 20 2f 2a  sView ){..    /*
2c3d4 20 44 6f 20 63 6f 6e 73 74 72 61 69 6e 74 20 63   Do constraint c
2c3d5 68 65 63 6b 73 2e 20 2a 2f 0a 20 20 20 20 73 71  hecks. */.    sq
2c3d6 6c 69 74 65 33 47 65 6e 65 72 61 74 65 43 6f 6e  lite3GenerateCon
2c3d7 73 74 72 61 69 6e 74 43 68 65 63 6b 73 28 70 50  straintChecks(pP
2c3d8 61 72 73 65 2c 20 70 54 61 62 2c 20 69 43 75 72  arse, pTab, iCur
2c3d9 2c 20 72 65 67 4e 65 77 52 6f 77 69 64 2c 0a 20  , regNewRowid,. 
2c3da 20 20 20 20 20 20 20 61 52 65 67 49 64 78 2c 20         aRegIdx, 
2c3db 28 63 68 6e 67 52 6f 77 69 64 3f 72 65 67 4f 6c  (chngRowid?regOl
2c3dc 64 52 6f 77 69 64 3a 30 29 2c 20 31 2c 20 6f 6e  dRowid:0), 1, on
2c3dd 45 72 72 6f 72 2c 20 61 64 64 72 2c 20 30 29 3b  Error, addr, 0);
2c3de 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65 74 65 20  ..    /* Delete 
2c3df 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72 69 65  the index entrie
2c3e0 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
2c3e1 68 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 65  h the current re
2c3e2 63 6f 72 64 2e 20 20 2a 2f 0a 20 20 20 20 6a 31  cord.  */.    j1
2c3e3 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
2c3e4 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78  dOp3(v, OP_NotEx
2c3e5 69 73 74 73 2c 20 69 43 75 72 2c 20 30 2c 20 72  ists, iCur, 0, r
2c3e6 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 20 20 20  egOldRowid);.   
2c3e7 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65   sqlite3Generate
2c3e8 52 6f 77 49 6e 64 65 78 44 65 6c 65 74 65 28 70  RowIndexDelete(p
2c3e9 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69 43 75  Parse, pTab, iCu
2c3ea 72 2c 20 61 52 65 67 49 64 78 29 3b 0a 20 20 0a  r, aRegIdx);.  .
2c3eb 20 20 20 20 2f 2a 20 49 66 20 63 68 61 6e 67 69      /* If changi
2c3ec 6e 67 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75  ng the record nu
2c3ed 6d 62 65 72 2c 20 64 65 6c 65 74 65 20 74 68 65  mber, delete the
2c3ee 20 6f 6c 64 20 72 65 63 6f 72 64 2e 20 20 2a 2f   old record.  */
2c3ef 0a 20 20 20 20 69 66 28 20 63 68 6e 67 52 6f 77  .    if( chngRow
2c3f0 69 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  id ){.      sqli
2c3f1 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2c3f2 20 4f 50 5f 44 65 6c 65 74 65 2c 20 69 43 75 72   OP_Delete, iCur
2c3f3 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  , 0);.    }.    
2c3f4 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
2c3f5 65 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20 0a 20  ere(v, j1);.  . 
2c3f6 20 20 20 2f 2a 20 49 6e 73 65 72 74 20 74 68 65     /* Insert the
2c3f7 20 6e 65 77 20 69 6e 64 65 78 20 65 6e 74 72 69   new index entri
2c3f8 65 73 20 61 6e 64 20 74 68 65 20 6e 65 77 20 72  es and the new r
2c3f9 65 63 6f 72 64 2e 20 2a 2f 0a 20 20 20 20 73 71  ecord. */.    sq
2c3fa 6c 69 74 65 33 43 6f 6d 70 6c 65 74 65 49 6e 73  lite3CompleteIns
2c3fb 65 72 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 70  ertion(pParse, p
2c3fc 54 61 62 2c 20 69 43 75 72 2c 20 72 65 67 4e 65  Tab, iCur, regNe
2c3fd 77 52 6f 77 69 64 2c 20 61 52 65 67 49 64 78 2c  wRowid, aRegIdx,
2c3fe 20 31 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 0a   1, 0, 0);.  }..
2c3ff 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74    /* Increment t
2c400 68 65 20 72 6f 77 20 63 6f 75 6e 74 65 72 20 0a  he row counter .
2c401 20 20 2a 2f 0a 20 20 69 66 28 20 28 64 62 2d 3e    */.  if( (db->
2c402 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 43  flags & SQLITE_C
2c403 6f 75 6e 74 52 6f 77 73 29 20 26 26 20 21 70 50  ountRows) && !pP
2c404 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61  arse->pTriggerTa
2c405 62 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  b){.    sqlite3V
2c406 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2c407 41 64 64 49 6d 6d 2c 20 72 65 67 52 6f 77 43 6f  AddImm, regRowCo
2c408 75 6e 74 2c 20 31 29 3b 0a 20 20 7d 0a 0a 20 20  unt, 1);.  }..  
2c409 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72  sqlite3CodeRowTr
2c40a 69 67 67 65 72 28 70 50 61 72 73 65 2c 20 70 54  igger(pParse, pT
2c40b 72 69 67 67 65 72 2c 20 54 4b 5f 55 50 44 41 54  rigger, TK_UPDAT
2c40c 45 2c 20 70 43 68 61 6e 67 65 73 2c 20 0a 20 20  E, pChanges, .  
2c40d 20 20 20 20 54 52 49 47 47 45 52 5f 41 46 54 45      TRIGGER_AFTE
2c40e 52 2c 20 70 54 61 62 2c 20 2d 31 2c 20 72 65 67  R, pTab, -1, reg
2c40f 4f 6c 64 52 6f 77 69 64 2c 20 6f 6e 45 72 72 6f  OldRowid, onErro
2c410 72 2c 20 61 64 64 72 29 3b 0a 0a 20 20 2f 2a 20  r, addr);..  /* 
2c411 52 65 70 65 61 74 20 74 68 65 20 61 62 6f 76 65  Repeat the above
2c412 20 77 69 74 68 20 74 68 65 20 6e 65 78 74 20 72   with the next r
2c413 65 63 6f 72 64 20 74 6f 20 62 65 20 75 70 64 61  ecord to be upda
2c414 74 65 64 2c 20 75 6e 74 69 6c 0a 20 20 2a 2a 20  ted, until.  ** 
2c415 61 6c 6c 20 72 65 63 6f 72 64 20 73 65 6c 65 63  all record selec
2c416 74 65 64 20 62 79 20 74 68 65 20 57 48 45 52 45  ted by the WHERE
2c417 20 63 6c 61 75 73 65 20 68 61 76 65 20 62 65 65   clause have bee
2c418 6e 20 75 70 64 61 74 65 64 2e 0a 20 20 2a 2f 0a  n updated..  */.
2c419 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2c41a 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
2c41b 30 2c 20 61 64 64 72 29 3b 0a 20 20 73 71 6c 69  0, addr);.  sqli
2c41c 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
2c41d 76 2c 20 61 64 64 72 29 3b 0a 0a 20 20 2f 2a 20  v, addr);..  /* 
2c41e 43 6c 6f 73 65 20 61 6c 6c 20 74 61 62 6c 65 73  Close all tables
2c41f 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70   */.  for(i=0, p
2c420 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78  Idx=pTab->pIndex
2c421 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64  ; pIdx; pIdx=pId
2c422 78 2d 3e 70 4e 65 78 74 2c 20 69 2b 2b 29 7b 0a  x->pNext, i++){.
2c423 20 20 20 20 69 66 28 20 6f 70 65 6e 41 6c 6c 20      if( openAll 
2c424 7c 7c 20 61 52 65 67 49 64 78 5b 69 5d 3e 30 20  || aRegIdx[i]>0 
2c425 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2c426 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2c427 5f 43 6c 6f 73 65 2c 20 69 43 75 72 2b 69 2b 31  _Close, iCur+i+1
2c428 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  , 0);.    }.  }.
2c429 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2c42a 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  Op2(v, OP_Close,
2c42b 20 69 43 75 72 2c 20 30 29 3b 0a 0a 20 20 2f 2a   iCur, 0);..  /*
2c42c 20 55 70 64 61 74 65 20 74 68 65 20 73 71 6c 69   Update the sqli
2c42d 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c  te_sequence tabl
2c42e 65 20 62 79 20 73 74 6f 72 69 6e 67 20 74 68 65  e by storing the
2c42f 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 0a   content of the.
2c430 20 20 2a 2a 20 6d 61 78 69 6d 75 6d 20 72 6f 77    ** maximum row
2c431 69 64 20 63 6f 75 6e 74 65 72 20 76 61 6c 75 65  id counter value
2c432 73 20 72 65 63 6f 72 64 65 64 20 77 68 69 6c 65  s recorded while
2c433 20 69 6e 73 65 72 74 69 6e 67 20 69 6e 74 6f 0a   inserting into.
2c434 20 20 2a 2a 20 61 75 74 6f 69 6e 63 72 65 6d 65    ** autoincreme
2c435 6e 74 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a  nt tables..  */.
2c436 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65    if( pParse->ne
2c437 73 74 65 64 3d 3d 30 20 26 26 20 70 50 61 72 73  sted==0 && pPars
2c438 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 3d 3d  e->pTriggerTab==
2c439 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
2c43a 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 45 6e 64  AutoincrementEnd
2c43b 28 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a 0a 20  (pParse);.  }.. 
2c43c 20 2f 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20   /*.  ** Return 
2c43d 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f  the number of ro
2c43e 77 73 20 74 68 61 74 20 77 65 72 65 20 63 68 61  ws that were cha
2c43f 6e 67 65 64 2e 20 49 66 20 74 68 69 73 20 72 6f  nged. If this ro
2c440 75 74 69 6e 65 20 69 73 20 0a 20 20 2a 2a 20 67  utine is .  ** g
2c441 65 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 20 62  enerating code b
2c442 65 63 61 75 73 65 20 6f 66 20 61 20 63 61 6c 6c  ecause of a call
2c443 20 74 6f 20 73 71 6c 69 74 65 33 4e 65 73 74 65   to sqlite3Neste
2c444 64 50 61 72 73 65 28 29 2c 20 64 6f 20 6e 6f 74  dParse(), do not
2c445 0a 20 20 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65  .  ** invoke the
2c446 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69   callback functi
2c447 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  on..  */.  if( (
2c448 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45  db->flags&SQLITE
2c449 5f 43 6f 75 6e 74 52 6f 77 73 29 20 26 26 20 21  _CountRows) && !
2c44a 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72  pParse->pTrigger
2c44b 54 61 62 20 26 26 20 21 70 50 61 72 73 65 2d 3e  Tab && !pParse->
2c44c 6e 65 73 74 65 64 20 29 7b 0a 20 20 20 20 73 71  nested ){.    sq
2c44d 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2c44e 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c  v, OP_ResultRow,
2c44f 20 72 65 67 52 6f 77 43 6f 75 6e 74 2c 20 31 29   regRowCount, 1)
2c450 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2c451 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31  eSetNumCols(v, 1
2c452 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2c453 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
2c454 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  0, COLNAME_NAME,
2c455 20 22 72 6f 77 73 20 75 70 64 61 74 65 64 22 2c   "rows updated",
2c456 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
2c457 0a 20 20 7d 0a 0a 75 70 64 61 74 65 5f 63 6c 65  .  }..update_cle
2c458 61 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 41  anup:.  sqlite3A
2c459 75 74 68 43 6f 6e 74 65 78 74 50 6f 70 28 26 73  uthContextPop(&s
2c45a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c 69  Context);.  sqli
2c45b 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 61 52  te3DbFree(db, aR
2c45c 65 67 49 64 78 29 3b 0a 20 20 73 71 6c 69 74 65  egIdx);.  sqlite
2c45d 33 44 62 46 72 65 65 28 64 62 2c 20 61 58 52 65  3DbFree(db, aXRe
2c45e 66 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 72 63  f);.  sqlite3Src
2c45f 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
2c460 54 61 62 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69  TabList);.  sqli
2c461 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
2c462 65 28 64 62 2c 20 70 43 68 61 6e 67 65 73 29 3b  e(db, pChanges);
2c463 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  .  sqlite3ExprDe
2c464 6c 65 74 65 28 64 62 2c 20 70 57 68 65 72 65 29  lete(db, pWhere)
2c465 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 23  ;.  return;.}..#
2c466 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2c467 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
2c468 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
2c469 6f 64 65 20 66 6f 72 20 61 6e 20 55 50 44 41 54  ode for an UPDAT
2c46a 45 20 6f 66 20 61 20 76 69 72 74 75 61 6c 20 74  E of a virtual t
2c46b 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  able..**.** The 
2c46c 73 74 72 61 74 65 67 79 20 69 73 20 74 68 61 74  strategy is that
2c46d 20 77 65 20 63 72 65 61 74 65 20 61 6e 20 65 70   we create an ep
2c46e 68 65 6d 65 72 69 61 6c 20 74 61 62 6c 65 20 74  hemerial table t
2c46f 68 61 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20  hat contains.** 
2c470 66 6f 72 20 65 61 63 68 20 72 6f 77 20 74 6f 20  for each row to 
2c471 62 65 20 63 68 61 6e 67 65 64 3a 0a 2a 2a 0a 2a  be changed:.**.*
2c472 2a 20 20 20 28 41 29 20 20 54 68 65 20 6f 72 69  *   (A)  The ori
2c473 67 69 6e 61 6c 20 72 6f 77 69 64 20 6f 66 20 74  ginal rowid of t
2c474 68 61 74 20 72 6f 77 2e 0a 2a 2a 20 20 20 28 42  hat row..**   (B
2c475 29 20 20 54 68 65 20 72 65 76 69 73 65 64 20 72  )  The revised r
2c476 6f 77 69 64 20 66 6f 72 20 74 68 65 20 72 6f 77  owid for the row
2c477 2e 20 28 6e 6f 74 65 31 29 0a 2a 2a 20 20 20 28  . (note1).**   (
2c478 43 29 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 20  C)  The content 
2c479 6f 66 20 65 76 65 72 79 20 63 6f 6c 75 6d 6e 20  of every column 
2c47a 69 6e 20 74 68 65 20 72 6f 77 2e 0a 2a 2a 0a 2a  in the row..**.*
2c47b 2a 20 54 68 65 6e 20 77 65 20 6c 6f 6f 70 20 6f  * Then we loop o
2c47c 76 65 72 20 74 68 69 73 20 65 70 68 65 6d 65 72  ver this ephemer
2c47d 61 6c 20 74 61 62 6c 65 20 61 6e 64 20 66 6f 72  al table and for
2c47e 20 65 61 63 68 20 72 6f 77 20 69 6e 0a 2a 2a 20   each row in.** 
2c47f 74 68 65 20 65 70 68 65 72 6d 65 72 61 6c 20 74  the ephermeral t
2c480 61 62 6c 65 20 63 61 6c 6c 20 56 55 70 64 61 74  able call VUpdat
2c481 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 66 69  e..**.** When fi
2c482 6e 69 73 68 65 64 2c 20 64 72 6f 70 20 74 68 65  nished, drop the
2c483 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65   ephemeral table
2c484 2e 0a 2a 2a 0a 2a 2a 20 28 6e 6f 74 65 31 29 20  ..**.** (note1) 
2c485 41 63 74 75 61 6c 6c 79 2c 20 69 66 20 77 65 20  Actually, if we 
2c486 6b 6e 6f 77 20 69 6e 20 61 64 76 61 6e 63 65 20  know in advance 
2c487 74 68 61 74 20 28 41 29 20 69 73 20 61 6c 77 61  that (A) is alwa
2c488 79 73 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 61  ys the same.** a
2c489 73 20 28 42 29 20 77 65 20 6f 6e 6c 79 20 73 74  s (B) we only st
2c48a 6f 72 65 20 28 41 29 2c 20 74 68 65 6e 20 64 75  ore (A), then du
2c48b 70 6c 69 63 61 74 65 20 28 41 29 20 77 68 65 6e  plicate (A) when
2c48c 20 70 75 6c 6c 69 6e 67 0a 2a 2a 20 69 74 20 6f   pulling.** it o
2c48d 75 74 20 6f 66 20 74 68 65 20 65 70 68 65 6d 65  ut of the epheme
2c48e 72 61 6c 20 74 61 62 6c 65 20 62 65 66 6f 72 65  ral table before
2c48f 20 63 61 6c 6c 69 6e 67 20 56 55 70 64 61 74 65   calling VUpdate
2c490 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2c491 20 75 70 64 61 74 65 56 69 72 74 75 61 6c 54 61   updateVirtualTa
2c492 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ble(.  Parse *pP
2c493 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 54  arse,       /* T
2c494 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65  he parsing conte
2c495 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20  xt */.  SrcList 
2c496 2a 70 53 72 63 2c 20 20 20 20 20 20 20 2f 2a 20  *pSrc,       /* 
2c497 54 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  The virtual tabl
2c498 65 20 74 6f 20 62 65 20 6d 6f 64 69 66 69 65 64  e to be modified
2c499 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
2c49a 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  b,         /* Th
2c49b 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
2c49c 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
2c49d 43 68 61 6e 67 65 73 2c 20 20 2f 2a 20 54 68 65  Changes,  /* The
2c49e 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 63 68 61 6e   columns to chan
2c49f 67 65 20 69 6e 20 74 68 65 20 55 50 44 41 54 45  ge in the UPDATE
2c4a0 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
2c4a1 45 78 70 72 20 2a 70 52 6f 77 69 64 2c 20 20 20  Expr *pRowid,   
2c4a2 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69       /* Expressi
2c4a3 6f 6e 20 75 73 65 64 20 74 6f 20 72 65 63 6f 6d  on used to recom
2c4a4 70 75 74 65 20 74 68 65 20 72 6f 77 69 64 20 2a  pute the rowid *
2c4a5 2f 0a 20 20 69 6e 74 20 2a 61 58 52 65 66 2c 20  /.  int *aXRef, 
2c4a6 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 70 70           /* Mapp
2c4a7 69 6e 67 20 66 72 6f 6d 20 63 6f 6c 75 6d 6e 73  ing from columns
2c4a8 20 6f 66 20 70 54 61 62 20 74 6f 20 65 6e 74 72   of pTab to entr
2c4a9 69 65 73 20 69 6e 20 70 43 68 61 6e 67 65 73 20  ies in pChanges 
2c4aa 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72  */.  Expr *pWher
2c4ab 65 20 20 20 20 20 20 20 20 20 2f 2a 20 57 48 45  e         /* WHE
2c4ac 52 45 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65  RE clause of the
2c4ad 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e   UPDATE statemen
2c4ae 74 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a  t */.){.  Vdbe *
2c4af 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
2c4b0 65 3b 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 6d  e;  /* Virtual m
2c4b1 61 63 68 69 6e 65 20 75 6e 64 65 72 20 63 6f 6e  achine under con
2c4b2 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 45  struction */.  E
2c4b3 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20  xprList *pEList 
2c4b4 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54 68 65 20  = 0;     /* The 
2c4b5 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68  result set of th
2c4b6 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
2c4b7 6e 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  nt */.  Select *
2c4b8 70 53 65 6c 65 63 74 20 3d 20 30 3b 20 20 20 20  pSelect = 0;    
2c4b9 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20    /* The SELECT 
2c4ba 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 45  statement */.  E
2c4bb 78 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20  xpr *pExpr;     
2c4bc 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70           /* Temp
2c4bd 6f 72 61 72 79 20 65 78 70 72 65 73 73 69 6f 6e  orary expression
2c4be 20 2a 2f 0a 20 20 69 6e 74 20 65 70 68 65 6d 54   */.  int ephemT
2c4bf 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ab;             
2c4c0 2f 2a 20 54 61 62 6c 65 20 68 6f 6c 64 69 6e 67  /* Table holding
2c4c1 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74   the result of t
2c4c2 68 65 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69  he SELECT */.  i
2c4c3 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
2c4c4 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
2c4c5 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e   counter */.  in
2c4c6 74 20 61 64 64 72 3b 20 20 20 20 20 20 20 20 20  t addr;         
2c4c7 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
2c4c8 73 73 20 6f 66 20 74 6f 70 20 6f 66 20 6c 6f 6f  ss of top of loo
2c4c9 70 20 2a 2f 0a 20 20 69 6e 74 20 69 52 65 67 3b  p */.  int iReg;
2c4ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c4cb 20 2f 2a 20 46 69 72 73 74 20 72 65 67 69 73 74   /* First regist
2c4cc 65 72 20 69 6e 20 73 65 74 20 70 61 73 73 65 64  er in set passed
2c4cd 20 74 6f 20 4f 50 5f 56 55 70 64 61 74 65 20 2a   to OP_VUpdate *
2c4ce 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  /.  sqlite3 *db 
2c4cf 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 2f 2a  = pParse->db; /*
2c4d0 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   Database connec
2c4d1 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  tion */.  const 
2c4d2 63 68 61 72 20 2a 70 56 54 61 62 20 3d 20 28 63  char *pVTab = (c
2c4d3 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 6c 69 74  onst char*)sqlit
2c4d4 65 33 47 65 74 56 54 61 62 6c 65 28 64 62 2c 20  e3GetVTable(db, 
2c4d5 70 54 61 62 29 3b 0a 20 20 53 65 6c 65 63 74 44  pTab);.  SelectD
2c4d6 65 73 74 20 64 65 73 74 3b 0a 0a 20 20 2f 2a 20  est dest;..  /* 
2c4d7 43 6f 6e 73 74 72 75 63 74 20 74 68 65 20 53 45  Construct the SE
2c4d8 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74  LECT statement t
2c4d9 68 61 74 20 77 69 6c 6c 20 66 69 6e 64 20 74 68  hat will find th
2c4da 65 20 6e 65 77 20 76 61 6c 75 65 73 20 66 6f 72  e new values for
2c4db 0a 20 20 2a 2a 20 61 6c 6c 20 75 70 64 61 74 65  .  ** all update
2c4dc 64 20 72 6f 77 73 2e 20 0a 20 20 2a 2f 0a 20 20  d rows. .  */.  
2c4dd 70 45 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33  pEList = sqlite3
2c4de 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
2c4df 50 61 72 73 65 2c 20 30 2c 20 0a 20 20 20 20 20  Parse, 0, .     
2c4e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c4e1 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
2c4e2 74 65 33 43 72 65 61 74 65 49 64 45 78 70 72 28  te3CreateIdExpr(
2c4e3 70 50 61 72 73 65 2c 20 22 5f 72 6f 77 69 64 5f  pParse, "_rowid_
2c4e4 22 29 29 3b 0a 20 20 69 66 28 20 70 52 6f 77 69  "));.  if( pRowi
2c4e5 64 20 29 7b 0a 20 20 20 20 70 45 4c 69 73 74 20  d ){.    pEList 
2c4e6 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
2c4e7 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20  tAppend(pParse, 
2c4e8 70 45 4c 69 73 74 2c 0a 20 20 20 20 20 20 20 20  pEList,.        
2c4e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c4ea 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2c4eb 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 52  e3ExprDup(db, pR
2c4ec 6f 77 69 64 2c 20 30 29 29 3b 0a 20 20 7d 0a 20  owid, 0));.  }. 
2c4ed 20 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e 69   assert( pTab->i
2c4ee 50 4b 65 79 3c 30 20 29 3b 0a 20 20 66 6f 72 28  PKey<0 );.  for(
2c4ef 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f  i=0; i<pTab->nCo
2c4f0 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  l; i++){.    if(
2c4f1 20 61 58 52 65 66 5b 69 5d 3e 3d 30 20 29 7b 0a   aXRef[i]>=0 ){.
2c4f2 20 20 20 20 20 20 70 45 78 70 72 20 3d 20 73 71        pExpr = sq
2c4f3 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
2c4f4 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b 61 58 52   pChanges->a[aXR
2c4f5 65 66 5b 69 5d 5d 2e 70 45 78 70 72 2c 20 30 29  ef[i]].pExpr, 0)
2c4f6 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
2c4f7 20 20 20 70 45 78 70 72 20 3d 20 73 71 6c 69 74     pExpr = sqlit
2c4f8 65 33 43 72 65 61 74 65 49 64 45 78 70 72 28 70  e3CreateIdExpr(p
2c4f9 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 61 43 6f  Parse, pTab->aCo
2c4fa 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20  l[i].zName);.   
2c4fb 20 7d 0a 20 20 20 20 70 45 4c 69 73 74 20 3d 20   }.    pEList = 
2c4fc 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
2c4fd 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 70 45  ppend(pParse, pE
2c4fe 4c 69 73 74 2c 20 70 45 78 70 72 29 3b 0a 20 20  List, pExpr);.  
2c4ff 7d 0a 20 20 70 53 65 6c 65 63 74 20 3d 20 73 71  }.  pSelect = sq
2c500 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28 70  lite3SelectNew(p
2c501 50 61 72 73 65 2c 20 70 45 4c 69 73 74 2c 20 70  Parse, pEList, p
2c502 53 72 63 2c 20 70 57 68 65 72 65 2c 20 30 2c 20  Src, pWhere, 0, 
2c503 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a  0, 0, 0, 0, 0);.
2c504 20 20 0a 20 20 2f 2a 20 43 72 65 61 74 65 20 74    .  /* Create t
2c505 68 65 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62  he ephemeral tab
2c506 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 74 68  le into which th
2c507 65 20 75 70 64 61 74 65 20 72 65 73 75 6c 74 73  e update results
2c508 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 73 74   will.  ** be st
2c509 6f 72 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  ored..  */.  ass
2c50a 65 72 74 28 20 76 20 29 3b 0a 20 20 65 70 68 65  ert( v );.  ephe
2c50b 6d 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e  mTab = pParse->n
2c50c 54 61 62 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33  Tab++;.  sqlite3
2c50d 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2c50e 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20  _OpenEphemeral, 
2c50f 65 70 68 65 6d 54 61 62 2c 20 70 54 61 62 2d 3e  ephemTab, pTab->
2c510 6e 43 6f 6c 2b 31 2b 28 70 52 6f 77 69 64 21 3d  nCol+1+(pRowid!=
2c511 30 29 29 3b 0a 0a 20 20 2f 2a 20 66 69 6c 6c 20  0));..  /* fill 
2c512 74 68 65 20 65 70 68 65 6d 65 72 61 6c 20 74 61  the ephemeral ta
2c513 62 6c 65 20 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  ble .  */.  sqli
2c514 74 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69  te3SelectDestIni
2c515 74 28 26 64 65 73 74 2c 20 53 52 54 5f 54 61 62  t(&dest, SRT_Tab
2c516 6c 65 2c 20 65 70 68 65 6d 54 61 62 29 3b 0a 20  le, ephemTab);. 
2c517 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70   sqlite3Select(p
2c518 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2c 20  Parse, pSelect, 
2c519 26 64 65 73 74 29 3b 0a 0a 20 20 2f 2a 20 47 65  &dest);..  /* Ge
2c51a 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 73  nerate code to s
2c51b 63 61 6e 20 74 68 65 20 65 70 68 65 6d 65 72 61  can the ephemera
2c51c 6c 20 74 61 62 6c 65 20 61 6e 64 20 63 61 6c 6c  l table and call
2c51d 20 56 55 70 64 61 74 65 2e 20 2a 2f 0a 20 20 69   VUpdate. */.  i
2c51e 52 65 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  Reg = ++pParse->
2c51f 6e 4d 65 6d 3b 0a 20 20 70 50 61 72 73 65 2d 3e  nMem;.  pParse->
2c520 6e 4d 65 6d 20 2b 3d 20 70 54 61 62 2d 3e 6e 43  nMem += pTab->nC
2c521 6f 6c 2b 31 3b 0a 20 20 61 64 64 72 20 3d 20 73  ol+1;.  addr = s
2c522 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2c523 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 65  (v, OP_Rewind, e
2c524 70 68 65 6d 54 61 62 2c 20 30 29 3b 0a 20 20 73  phemTab, 0);.  s
2c525 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2c526 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 20  (v, OP_Column,  
2c527 65 70 68 65 6d 54 61 62 2c 20 30 2c 20 69 52 65  ephemTab, 0, iRe
2c528 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  g);.  sqlite3Vdb
2c529 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
2c52a 6c 75 6d 6e 2c 20 65 70 68 65 6d 54 61 62 2c 20  lumn, ephemTab, 
2c52b 28 70 52 6f 77 69 64 3f 31 3a 30 29 2c 20 69 52  (pRowid?1:0), iR
2c52c 65 67 2b 31 29 3b 0a 20 20 66 6f 72 28 69 3d 30  eg+1);.  for(i=0
2c52d 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20  ; i<pTab->nCol; 
2c52e 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  i++){.    sqlite
2c52f 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2c530 50 5f 43 6f 6c 75 6d 6e 2c 20 65 70 68 65 6d 54  P_Column, ephemT
2c531 61 62 2c 20 69 2b 31 2b 28 70 52 6f 77 69 64 21  ab, i+1+(pRowid!
2c532 3d 30 29 2c 20 69 52 65 67 2b 32 2b 69 29 3b 0a  =0), iReg+2+i);.
2c533 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 74 61    }.  sqlite3Vta
2c534 62 4d 61 6b 65 57 72 69 74 61 62 6c 65 28 70 50  bMakeWritable(pP
2c535 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 73  arse, pTab);.  s
2c536 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
2c537 28 76 2c 20 4f 50 5f 56 55 70 64 61 74 65 2c 20  (v, OP_VUpdate, 
2c538 30 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2b 32 2c  0, pTab->nCol+2,
2c539 20 69 52 65 67 2c 20 70 56 54 61 62 2c 20 50 34   iReg, pVTab, P4
2c53a 5f 56 54 41 42 29 3b 0a 20 20 73 71 6c 69 74 65  _VTAB);.  sqlite
2c53b 33 4d 61 79 41 62 6f 72 74 28 70 50 61 72 73 65  3MayAbort(pParse
2c53c 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
2c53d 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78  AddOp2(v, OP_Nex
2c53e 74 2c 20 65 70 68 65 6d 54 61 62 2c 20 61 64 64  t, ephemTab, add
2c53f 72 2b 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  r+1);.  sqlite3V
2c540 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
2c541 64 64 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  ddr);.  sqlite3V
2c542 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2c543 43 6c 6f 73 65 2c 20 65 70 68 65 6d 54 61 62 2c  Close, ephemTab,
2c544 20 30 29 3b 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e   0);..  /* Clean
2c545 75 70 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 53  up */.  sqlite3S
2c546 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20  electDelete(db, 
2c547 70 53 65 6c 65 63 74 29 3b 20 20 0a 7d 0a 23 65  pSelect);  .}.#e
2c548 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
2c549 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
2c54a 20 2a 2f 0a 0a 2f 2a 20 4d 61 6b 65 20 73 75 72   */../* Make sur
2c54b 65 20 22 69 73 56 69 65 77 22 20 67 65 74 73 20  e "isView" gets 
2c54c 75 6e 64 65 66 69 6e 65 64 20 69 6e 20 63 61 73  undefined in cas
2c54d 65 20 74 68 69 73 20 66 69 6c 65 20 62 65 63 6f  e this file beco
2c54e 6d 65 73 20 70 61 72 74 20 6f 66 0a 2a 2a 20 74  mes part of.** t
2c54f 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20  he amalgamation 
2c550 2d 20 73 6f 20 74 68 61 74 20 73 75 62 73 65 71  - so that subseq
2c551 75 65 6e 74 20 66 69 6c 65 73 20 64 6f 20 6e 6f  uent files do no
2c552 74 20 73 65 65 20 69 73 56 69 65 77 20 61 73 20  t see isView as 
2c553 61 0a 2a 2a 20 6d 61 63 72 6f 2e 20 2a 2f 0a 23  a.** macro. */.#
2c554 75 6e 64 65 66 20 69 73 56 69 65 77 0a 0a 2f 2a  undef isView../*
2c555 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
2c556 64 20 6f 66 20 75 70 64 61 74 65 2e 63 20 2a 2a  d of update.c **
2c557 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c558 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c559 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
2c55a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
2c55b 67 69 6e 20 66 69 6c 65 20 76 61 63 75 75 6d 2e  gin file vacuum.
2c55c 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
2c55d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c55e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
2c55f 0a 2a 2a 20 32 30 30 33 20 41 70 72 69 6c 20 36  .** 2003 April 6
2c560 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
2c561 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
2c562 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
2c563 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
2c564 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
2c565 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
2c566 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
2c567 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
2c568 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
2c569 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
2c56a 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
2c56b 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
2c56c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
2c56d 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
2c56e 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
2c56f 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
2c570 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
2c571 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
2c572 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c573 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c574 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c575 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
2c576 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
2c577 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20  tains code used 
2c578 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
2c579 20 56 41 43 55 55 4d 20 63 6f 6d 6d 61 6e 64 2e   VACUUM command.
2c57a 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 6f 66 20 74  .**.** Most of t
2c57b 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20  he code in this 
2c57c 66 69 6c 65 20 6d 61 79 20 62 65 20 6f 6d 69 74  file may be omit
2c57d 74 65 64 20 62 79 20 64 65 66 69 6e 69 6e 67 20  ted by defining 
2c57e 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4d  the.** SQLITE_OM
2c57f 49 54 5f 56 41 43 55 55 4d 20 6d 61 63 72 6f 2e  IT_VACUUM macro.
2c580 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 61 63 75  .**.** $Id: vacu
2c581 75 6d 2e 63 2c 76 20 31 2e 39 31 20 32 30 30 39  um.c,v 1.91 2009
2c582 2f 30 37 2f 30 32 20 30 37 3a 34 37 3a 33 33 20  /07/02 07:47:33 
2c583 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20  danielk1977 Exp 
2c584 24 0a 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e  $.*/..#if !defin
2c585 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
2c586 41 43 55 55 4d 29 20 26 26 20 21 64 65 66 69 6e  ACUUM) && !defin
2c587 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ed(SQLITE_OMIT_A
2c588 54 54 41 43 48 29 0a 2f 2a 0a 2a 2a 20 45 78 65  TTACH)./*.** Exe
2c589 63 75 74 65 20 7a 53 71 6c 20 6f 6e 20 64 61 74  cute zSql on dat
2c58a 61 62 61 73 65 20 64 62 2e 20 52 65 74 75 72 6e  abase db. Return
2c58b 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a   an error code..
2c58c 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 65 78  */.static int ex
2c58d 65 63 53 71 6c 28 73 71 6c 69 74 65 33 20 2a 64  ecSql(sqlite3 *d
2c58e 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
2c58f 53 71 6c 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  Sql){.  sqlite3_
2c590 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 20 20 56  stmt *pStmt;.  V
2c591 56 41 5f 4f 4e 4c 59 28 20 69 6e 74 20 72 63 3b  VA_ONLY( int rc;
2c592 20 29 0a 20 20 69 66 28 20 21 7a 53 71 6c 20 29   ).  if( !zSql )
2c593 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
2c594 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20  ITE_NOMEM;.  }. 
2c595 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
2c596 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28  sqlite3_prepare(
2c597 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70  db, zSql, -1, &p
2c598 53 74 6d 74 2c 20 30 29 20 29 7b 0a 20 20 20 20  Stmt, 0) ){.    
2c599 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 65  return sqlite3_e
2c59a 72 72 63 6f 64 65 28 64 62 29 3b 0a 20 20 7d 0a  rrcode(db);.  }.
2c59b 20 20 56 56 41 5f 4f 4e 4c 59 28 20 72 63 20 3d    VVA_ONLY( rc =
2c59c 20 29 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   ) sqlite3_step(
2c59d 70 53 74 6d 74 29 3b 0a 20 20 61 73 73 65 72 74  pStmt);.  assert
2c59e 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57  ( rc!=SQLITE_ROW
2c59f 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c   );.  return sql
2c5a0 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53  ite3_finalize(pS
2c5a1 74 6d 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45  tmt);.}../*.** E
2c5a2 78 65 63 75 74 65 20 7a 53 71 6c 20 6f 6e 20 64  xecute zSql on d
2c5a3 61 74 61 62 61 73 65 20 64 62 2e 20 54 68 65 20  atabase db. The 
2c5a4 73 74 61 74 65 6d 65 6e 74 20 72 65 74 75 72 6e  statement return
2c5a5 73 20 65 78 61 63 74 6c 79 0a 2a 2a 20 6f 6e 65  s exactly.** one
2c5a6 20 63 6f 6c 75 6d 6e 2e 20 45 78 65 63 75 74 65   column. Execute
2c5a7 20 74 68 69 73 20 61 73 20 53 51 4c 20 6f 6e 20   this as SQL on 
2c5a8 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
2c5a9 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
2c5aa 20 65 78 65 63 45 78 65 63 53 71 6c 28 73 71 6c   execExecSql(sql
2c5ab 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20  ite3 *db, const 
2c5ac 63 68 61 72 20 2a 7a 53 71 6c 29 7b 0a 20 20 73  char *zSql){.  s
2c5ad 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
2c5ae 6d 74 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20  mt;.  int rc;.. 
2c5af 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72   rc = sqlite3_pr
2c5b0 65 70 61 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20  epare(db, zSql, 
2c5b1 2d 31 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a  -1, &pStmt, 0);.
2c5b2 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2c5b3 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
2c5b4 0a 0a 20 20 77 68 69 6c 65 28 20 53 51 4c 49 54  ..  while( SQLIT
2c5b5 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73  E_ROW==sqlite3_s
2c5b6 74 65 70 28 70 53 74 6d 74 29 20 29 7b 0a 20 20  tep(pStmt) ){.  
2c5b7 20 20 72 63 20 3d 20 65 78 65 63 53 71 6c 28 64    rc = execSql(d
2c5b8 62 2c 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65  b, (char*)sqlite
2c5b9 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 70 53  3_column_text(pS
2c5ba 74 6d 74 2c 20 30 29 29 3b 0a 20 20 20 20 69 66  tmt, 0));.    if
2c5bb 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
2c5bc 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2c5bd 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29  _finalize(pStmt)
2c5be 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  ;.      return r
2c5bf 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  c;.    }.  }..  
2c5c0 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 66  return sqlite3_f
2c5c1 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a  inalize(pStmt);.
2c5c2 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 6f 6e  }../*.** The non
2c5c3 2d 73 74 61 6e 64 61 72 64 20 56 41 43 55 55 4d  -standard VACUUM
2c5c4 20 63 6f 6d 6d 61 6e 64 20 69 73 20 75 73 65 64   command is used
2c5c5 20 74 6f 20 63 6c 65 61 6e 20 75 70 20 74 68 65   to clean up the
2c5c6 20 64 61 74 61 62 61 73 65 2c 0a 2a 2a 20 63 6f   database,.** co
2c5c7 6c 6c 61 70 73 65 20 66 72 65 65 20 73 70 61 63  llapse free spac
2c5c8 65 2c 20 65 74 63 2e 20 20 49 74 20 69 73 20 6d  e, etc.  It is m
2c5c9 6f 64 65 6c 6c 65 64 20 61 66 74 65 72 20 74 68  odelled after th
2c5ca 65 20 56 41 43 55 55 4d 20 63 6f 6d 6d 61 6e 64  e VACUUM command
2c5cb 0a 2a 2a 20 69 6e 20 50 6f 73 74 67 72 65 53 51  .** in PostgreSQ
2c5cc 4c 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 76 65 72 73  L..**.** In vers
2c5cd 69 6f 6e 20 31 2e 30 2e 78 20 6f 66 20 53 51 4c  ion 1.0.x of SQL
2c5ce 69 74 65 2c 20 74 68 65 20 56 41 43 55 55 4d 20  ite, the VACUUM 
2c5cf 63 6f 6d 6d 61 6e 64 20 77 6f 75 6c 64 20 63 61  command would ca
2c5d0 6c 6c 0a 2a 2a 20 67 64 62 6d 5f 72 65 6f 72 67  ll.** gdbm_reorg
2c5d1 61 6e 69 7a 65 28 29 20 6f 6e 20 61 6c 6c 20 74  anize() on all t
2c5d2 68 65 20 64 61 74 61 62 61 73 65 20 74 61 62 6c  he database tabl
2c5d3 65 73 2e 20 20 42 75 74 20 62 65 67 69 6e 6e 69  es.  But beginni
2c5d4 6e 67 0a 2a 2a 20 77 69 74 68 20 32 2e 30 2e 30  ng.** with 2.0.0
2c5d5 2c 20 53 51 4c 69 74 65 20 6e 6f 20 6c 6f 6e 67  , SQLite no long
2c5d6 65 72 20 75 73 65 73 20 47 44 42 4d 20 73 6f 20  er uses GDBM so 
2c5d7 74 68 69 73 20 63 6f 6d 6d 61 6e 64 20 68 61 73  this command has
2c5d8 0a 2a 2a 20 62 65 63 6f 6d 65 20 61 20 6e 6f 2d  .** become a no-
2c5d9 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  op..*/.SQLITE_PR
2c5da 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2c5db 65 33 56 61 63 75 75 6d 28 50 61 72 73 65 20 2a  e3Vacuum(Parse *
2c5dc 70 50 61 72 73 65 29 7b 0a 20 20 56 64 62 65 20  pParse){.  Vdbe 
2c5dd 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56  *v = sqlite3GetV
2c5de 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69  dbe(pParse);.  i
2c5df 66 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69  f( v ){.    sqli
2c5e0 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2c5e1 20 4f 50 5f 56 61 63 75 75 6d 2c 20 30 2c 20 30   OP_Vacuum, 0, 0
2c5e2 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 3b  );.  }.  return;
2c5e3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
2c5e4 6f 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74  outine implement
2c5e5 73 20 74 68 65 20 4f 50 5f 56 61 63 75 75 6d 20  s the OP_Vacuum 
2c5e6 6f 70 63 6f 64 65 20 6f 66 20 74 68 65 20 56 44  opcode of the VD
2c5e7 42 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  BE..*/.SQLITE_PR
2c5e8 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
2c5e9 33 52 75 6e 56 61 63 75 75 6d 28 63 68 61 72 20  3RunVacuum(char 
2c5ea 2a 2a 70 7a 45 72 72 4d 73 67 2c 20 73 71 6c 69  **pzErrMsg, sqli
2c5eb 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20  te3 *db){.  int 
2c5ec 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  rc = SQLITE_OK; 
2c5ed 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
2c5ee 64 65 20 66 72 6f 6d 20 73 65 72 76 69 63 65 20  de from service 
2c5ef 72 6f 75 74 69 6e 65 73 20 2a 2f 0a 20 20 42 74  routines */.  Bt
2c5f0 72 65 65 20 2a 70 4d 61 69 6e 3b 20 20 20 20 20  ree *pMain;     
2c5f1 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
2c5f2 61 62 61 73 65 20 62 65 69 6e 67 20 76 61 63 75  abase being vacu
2c5f3 75 6d 65 64 20 2a 2f 0a 20 20 42 74 72 65 65 20  umed */.  Btree 
2c5f4 2a 70 54 65 6d 70 3b 20 20 20 20 20 20 20 20 20  *pTemp;         
2c5f5 20 20 2f 2a 20 54 68 65 20 74 65 6d 70 6f 72 61    /* The tempora
2c5f6 72 79 20 64 61 74 61 62 61 73 65 20 77 65 20 76  ry database we v
2c5f7 61 63 75 75 6d 20 69 6e 74 6f 20 2a 2f 0a 20 20  acuum into */.  
2c5f8 63 68 61 72 20 2a 7a 53 71 6c 20 3d 20 30 3b 20  char *zSql = 0; 
2c5f9 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 73          /* SQL s
2c5fa 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 69  tatements */.  i
2c5fb 6e 74 20 73 61 76 65 64 5f 66 6c 61 67 73 3b 20  nt saved_flags; 
2c5fc 20 20 20 20 20 20 20 2f 2a 20 53 61 76 65 64 20         /* Saved 
2c5fd 76 61 6c 75 65 20 6f 66 20 74 68 65 20 64 62 2d  value of the db-
2c5fe 3e 66 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20  >flags */.  int 
2c5ff 73 61 76 65 64 5f 6e 43 68 61 6e 67 65 3b 20 20  saved_nChange;  
2c600 20 20 20 20 2f 2a 20 53 61 76 65 64 20 76 61 6c      /* Saved val
2c601 75 65 20 6f 66 20 64 62 2d 3e 6e 43 68 61 6e 67  ue of db->nChang
2c602 65 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65 64  e */.  int saved
2c603 5f 6e 54 6f 74 61 6c 43 68 61 6e 67 65 3b 20 2f  _nTotalChange; /
2c604 2a 20 53 61 76 65 64 20 76 61 6c 75 65 20 6f 66  * Saved value of
2c605 20 64 62 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e 67   db->nTotalChang
2c606 65 20 2a 2f 0a 20 20 44 62 20 2a 70 44 62 20 3d  e */.  Db *pDb =
2c607 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f   0;            /
2c608 2a 20 44 61 74 61 62 61 73 65 20 74 6f 20 64 65  * Database to de
2c609 74 61 63 68 20 61 74 20 65 6e 64 20 6f 66 20 76  tach at end of v
2c60a 61 63 75 75 6d 20 2a 2f 0a 20 20 69 6e 74 20 69  acuum */.  int i
2c60b 73 4d 65 6d 44 62 3b 20 20 20 20 20 20 20 20 20  sMemDb;         
2c60c 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 76 61     /* True if va
2c60d 63 75 75 6d 69 6e 67 20 61 20 3a 6d 65 6d 6f 72  cuuming a :memor
2c60e 79 3a 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  y: database */. 
2c60f 20 69 6e 74 20 6e 52 65 73 3b 0a 0a 20 20 69 66   int nRes;..  if
2c610 28 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69  ( !db->autoCommi
2c611 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
2c612 53 65 74 53 74 72 69 6e 67 28 70 7a 45 72 72 4d  SetString(pzErrM
2c613 73 67 2c 20 64 62 2c 20 22 63 61 6e 6e 6f 74 20  sg, db, "cannot 
2c614 56 41 43 55 55 4d 20 66 72 6f 6d 20 77 69 74 68  VACUUM from with
2c615 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  in a transaction
2c616 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  ");.    return S
2c617 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
2c618 0a 0a 20 20 2f 2a 20 53 61 76 65 20 74 68 65 20  ..  /* Save the 
2c619 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66  current value of
2c61a 20 74 68 65 20 77 72 69 74 65 2d 73 63 68 65 6d   the write-schem
2c61b 61 20 66 6c 61 67 20 62 65 66 6f 72 65 20 73 65  a flag before se
2c61c 74 74 69 6e 67 20 69 74 2e 20 2a 2f 0a 20 20 73  tting it. */.  s
2c61d 61 76 65 64 5f 66 6c 61 67 73 20 3d 20 64 62 2d  aved_flags = db-
2c61e 3e 66 6c 61 67 73 3b 0a 20 20 73 61 76 65 64 5f  >flags;.  saved_
2c61f 6e 43 68 61 6e 67 65 20 3d 20 64 62 2d 3e 6e 43  nChange = db->nC
2c620 68 61 6e 67 65 3b 0a 20 20 73 61 76 65 64 5f 6e  hange;.  saved_n
2c621 54 6f 74 61 6c 43 68 61 6e 67 65 20 3d 20 64 62  TotalChange = db
2c622 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e 67 65 3b 0a  ->nTotalChange;.
2c623 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53    db->flags |= S
2c624 51 4c 49 54 45 5f 57 72 69 74 65 53 63 68 65 6d  QLITE_WriteSchem
2c625 61 20 7c 20 53 51 4c 49 54 45 5f 49 67 6e 6f 72  a | SQLITE_Ignor
2c626 65 43 68 65 63 6b 73 3b 0a 0a 20 20 70 4d 61 69  eChecks;..  pMai
2c627 6e 20 3d 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 70  n = db->aDb[0].p
2c628 42 74 3b 0a 20 20 69 73 4d 65 6d 44 62 20 3d 20  Bt;.  isMemDb = 
2c629 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 4d 65  sqlite3PagerIsMe
2c62a 6d 64 62 28 73 71 6c 69 74 65 33 42 74 72 65 65  mdb(sqlite3Btree
2c62b 50 61 67 65 72 28 70 4d 61 69 6e 29 29 3b 0a 0a  Pager(pMain));..
2c62c 20 20 2f 2a 20 41 74 74 61 63 68 20 74 68 65 20    /* Attach the 
2c62d 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61  temporary databa
2c62e 73 65 20 61 73 20 27 76 61 63 75 75 6d 5f 64 62  se as 'vacuum_db
2c62f 27 2e 20 54 68 65 20 73 79 6e 63 68 72 6f 6e 6f  '. The synchrono
2c630 75 73 20 70 72 61 67 6d 61 0a 20 20 2a 2a 20 63  us pragma.  ** c
2c631 61 6e 20 62 65 20 73 65 74 20 74 6f 20 27 6f 66  an be set to 'of
2c632 66 27 20 66 6f 72 20 74 68 69 73 20 66 69 6c 65  f' for this file
2c633 2c 20 61 73 20 69 74 20 69 73 20 6e 6f 74 20 72  , as it is not r
2c634 65 63 6f 76 65 72 65 64 20 69 66 20 61 20 63 72  ecovered if a cr
2c635 61 73 68 0a 20 20 2a 2a 20 6f 63 63 75 72 73 20  ash.  ** occurs 
2c636 61 6e 79 77 61 79 2e 20 54 68 65 20 69 6e 74 65  anyway. The inte
2c637 67 72 69 74 79 20 6f 66 20 74 68 65 20 64 61 74  grity of the dat
2c638 61 62 61 73 65 20 69 73 20 6d 61 69 6e 74 61 69  abase is maintai
2c639 6e 65 64 20 62 79 20 61 0a 20 20 2a 2a 20 28 70  ned by a.  ** (p
2c63a 6f 73 73 69 62 6c 79 20 73 79 6e 63 68 72 6f 6e  ossibly synchron
2c63b 6f 75 73 29 20 74 72 61 6e 73 61 63 74 69 6f 6e  ous) transaction
2c63c 20 6f 70 65 6e 65 64 20 6f 6e 20 74 68 65 20 6d   opened on the m
2c63d 61 69 6e 20 64 61 74 61 62 61 73 65 20 62 65 66  ain database bef
2c63e 6f 72 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33  ore.  ** sqlite3
2c63f 42 74 72 65 65 43 6f 70 79 46 69 6c 65 28 29 20  BtreeCopyFile() 
2c640 69 73 20 63 61 6c 6c 65 64 2e 0a 20 20 2a 2a 0a  is called..  **.
2c641 20 20 2a 2a 20 41 6e 20 6f 70 74 69 6d 69 73 61    ** An optimisa
2c642 74 69 6f 6e 20 77 6f 75 6c 64 20 62 65 20 74 6f  tion would be to
2c643 20 75 73 65 20 61 20 6e 6f 6e 2d 6a 6f 75 72 6e   use a non-journ
2c644 61 6c 65 64 20 70 61 67 65 72 2e 0a 20 20 2a 2a  aled pager..  **
2c645 20 28 4c 61 74 65 72 3a 29 20 49 20 74 72 69 65   (Later:) I trie
2c646 64 20 73 65 74 74 69 6e 67 20 22 50 52 41 47 4d  d setting "PRAGM
2c647 41 20 76 61 63 75 75 6d 5f 64 62 2e 6a 6f 75 72  A vacuum_db.jour
2c648 6e 61 6c 5f 6d 6f 64 65 3d 4f 46 46 22 20 62 75  nal_mode=OFF" bu
2c649 74 0a 20 20 2a 2a 20 74 68 61 74 20 61 63 74 75  t.  ** that actu
2c64a 61 6c 6c 79 20 6d 61 64 65 20 74 68 65 20 56 41  ally made the VA
2c64b 43 55 55 4d 20 72 75 6e 20 73 6c 6f 77 65 72 2e  CUUM run slower.
2c64c 20 20 56 65 72 79 20 6c 69 74 74 6c 65 20 6a 6f    Very little jo
2c64d 75 72 6e 61 6c 6c 69 6e 67 0a 20 20 2a 2a 20 61  urnalling.  ** a
2c64e 63 74 75 61 6c 6c 79 20 6f 63 63 75 72 73 20 77  ctually occurs w
2c64f 68 65 6e 20 64 6f 69 6e 67 20 61 20 76 61 63 75  hen doing a vacu
2c650 75 6d 20 73 69 6e 63 65 20 74 68 65 20 76 61 63  um since the vac
2c651 75 75 6d 5f 64 62 20 69 73 20 69 6e 69 74 69 61  uum_db is initia
2c652 6c 6c 79 0a 20 20 2a 2a 20 65 6d 70 74 79 2e 20  lly.  ** empty. 
2c653 20 4f 6e 6c 79 20 74 68 65 20 6a 6f 75 72 6e 61   Only the journa
2c654 6c 20 68 65 61 64 65 72 20 69 73 20 77 72 69 74  l header is writ
2c655 74 65 6e 2e 20 20 41 70 70 61 72 65 6e 74 6c 79  ten.  Apparently
2c656 20 69 74 20 74 61 6b 65 73 20 6d 6f 72 65 0a 20   it takes more. 
2c657 20 2a 2a 20 74 69 6d 65 20 74 6f 20 70 61 72 73   ** time to pars
2c658 65 20 61 6e 64 20 72 75 6e 20 74 68 65 20 50 52  e and run the PR
2c659 41 47 4d 41 20 74 6f 20 74 75 72 6e 20 6a 6f 75  AGMA to turn jou
2c65a 72 6e 61 6c 6c 69 6e 67 20 6f 66 66 20 74 68 61  rnalling off tha
2c65b 6e 20 69 74 20 64 6f 65 73 0a 20 20 2a 2a 20 74  n it does.  ** t
2c65c 6f 20 77 72 69 74 65 20 74 68 65 20 6a 6f 75 72  o write the jour
2c65d 6e 61 6c 20 68 65 61 64 65 72 20 66 69 6c 65 2e  nal header file.
2c65e 0a 20 20 2a 2f 0a 20 20 7a 53 71 6c 20 3d 20 22  .  */.  zSql = "
2c65f 41 54 54 41 43 48 20 27 27 20 41 53 20 76 61 63  ATTACH '' AS vac
2c660 75 75 6d 5f 64 62 3b 22 3b 0a 20 20 72 63 20 3d  uum_db;";.  rc =
2c661 20 65 78 65 63 53 71 6c 28 64 62 2c 20 7a 53 71   execSql(db, zSq
2c662 6c 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  l);.  if( rc!=SQ
2c663 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65  LITE_OK ) goto e
2c664 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20  nd_of_vacuum;.  
2c665 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 64  pDb = &db->aDb[d
2c666 62 2d 3e 6e 44 62 2d 31 5d 3b 0a 20 20 61 73 73  b->nDb-1];.  ass
2c667 65 72 74 28 20 73 74 72 63 6d 70 28 64 62 2d 3e  ert( strcmp(db->
2c668 61 44 62 5b 64 62 2d 3e 6e 44 62 2d 31 5d 2e 7a  aDb[db->nDb-1].z
2c669 4e 61 6d 65 2c 22 76 61 63 75 75 6d 5f 64 62 22  Name,"vacuum_db"
2c66a 29 3d 3d 30 20 29 3b 0a 20 20 70 54 65 6d 70 20  )==0 );.  pTemp 
2c66b 3d 20 64 62 2d 3e 61 44 62 5b 64 62 2d 3e 6e 44  = db->aDb[db->nD
2c66c 62 2d 31 5d 2e 70 42 74 3b 0a 0a 20 20 6e 52 65  b-1].pBt;..  nRe
2c66d 73 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  s = sqlite3Btree
2c66e 47 65 74 52 65 73 65 72 76 65 28 70 4d 61 69 6e  GetReserve(pMain
2c66f 29 3b 0a 0a 20 20 2f 2a 20 41 20 56 41 43 55 55  );..  /* A VACUU
2c670 4d 20 63 61 6e 6e 6f 74 20 63 68 61 6e 67 65 20  M cannot change 
2c671 74 68 65 20 70 61 67 65 73 69 7a 65 20 6f 66 20  the pagesize of 
2c672 61 6e 20 65 6e 63 72 79 70 74 65 64 20 64 61 74  an encrypted dat
2c673 61 62 61 73 65 2e 20 2a 2f 0a 23 69 66 64 65 66  abase. */.#ifdef
2c674 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45   SQLITE_HAS_CODE
2c675 43 0a 20 20 69 66 28 20 64 62 2d 3e 6e 65 78 74  C.  if( db->next
2c676 50 61 67 65 73 69 7a 65 20 29 7b 0a 20 20 20 20  Pagesize ){.    
2c677 65 78 74 65 72 6e 20 76 6f 69 64 20 73 71 6c 69  extern void sqli
2c678 74 65 33 43 6f 64 65 63 47 65 74 4b 65 79 28 73  te3CodecGetKey(s
2c679 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 76 6f  qlite3*, int, vo
2c67a 69 64 2a 2a 2c 20 69 6e 74 2a 29 3b 0a 20 20 20  id**, int*);.   
2c67b 20 69 6e 74 20 6e 4b 65 79 3b 0a 20 20 20 20 63   int nKey;.    c
2c67c 68 61 72 20 2a 7a 4b 65 79 3b 0a 20 20 20 20 73  har *zKey;.    s
2c67d 71 6c 69 74 65 33 43 6f 64 65 63 47 65 74 4b 65  qlite3CodecGetKe
2c67e 79 28 64 62 2c 20 30 2c 20 28 76 6f 69 64 2a 2a  y(db, 0, (void**
2c67f 29 26 7a 4b 65 79 2c 20 26 6e 4b 65 79 29 3b 0a  )&zKey, &nKey);.
2c680 20 20 20 20 69 66 28 20 6e 4b 65 79 20 29 20 64      if( nKey ) d
2c681 62 2d 3e 6e 65 78 74 50 61 67 65 73 69 7a 65 20  b->nextPagesize 
2c682 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  = 0;.  }.#endif.
2c683 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 74  .  if( sqlite3Bt
2c684 72 65 65 53 65 74 50 61 67 65 53 69 7a 65 28 70  reeSetPageSize(p
2c685 54 65 6d 70 2c 20 73 71 6c 69 74 65 33 42 74 72  Temp, sqlite3Btr
2c686 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 70 4d  eeGetPageSize(pM
2c687 61 69 6e 29 2c 20 6e 52 65 73 2c 20 30 29 0a 20  ain), nRes, 0). 
2c688 20 20 7c 7c 20 28 21 69 73 4d 65 6d 44 62 20 26    || (!isMemDb &
2c689 26 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65  & sqlite3BtreeSe
2c68a 74 50 61 67 65 53 69 7a 65 28 70 54 65 6d 70 2c  tPageSize(pTemp,
2c68b 20 64 62 2d 3e 6e 65 78 74 50 61 67 65 73 69 7a   db->nextPagesiz
2c68c 65 2c 20 6e 52 65 73 2c 20 30 29 29 0a 20 20 20  e, nRes, 0)).   
2c68d 7c 7c 20 4e 45 56 45 52 28 64 62 2d 3e 6d 61 6c  || NEVER(db->mal
2c68e 6c 6f 63 46 61 69 6c 65 64 29 0a 20 20 29 7b 0a  locFailed).  ){.
2c68f 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
2c690 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f 20  NOMEM;.    goto 
2c691 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20  end_of_vacuum;. 
2c692 20 7d 0a 20 20 72 63 20 3d 20 65 78 65 63 53 71   }.  rc = execSq
2c693 6c 28 64 62 2c 20 22 50 52 41 47 4d 41 20 76 61  l(db, "PRAGMA va
2c694 63 75 75 6d 5f 64 62 2e 73 79 6e 63 68 72 6f 6e  cuum_db.synchron
2c695 6f 75 73 3d 4f 46 46 22 29 3b 0a 20 20 69 66 28  ous=OFF");.  if(
2c696 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
2c697 7b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6f  {.    goto end_o
2c698 66 5f 76 61 63 75 75 6d 3b 0a 20 20 7d 0a 0a 23  f_vacuum;.  }..#
2c699 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2c69a 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
2c69b 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 41  sqlite3BtreeSetA
2c69c 75 74 6f 56 61 63 75 75 6d 28 70 54 65 6d 70 2c  utoVacuum(pTemp,
2c69d 20 64 62 2d 3e 6e 65 78 74 41 75 74 6f 76 61 63   db->nextAutovac
2c69e 3e 3d 30 20 3f 20 64 62 2d 3e 6e 65 78 74 41 75  >=0 ? db->nextAu
2c69f 74 6f 76 61 63 20 3a 0a 20 20 20 20 20 20 20 20  tovac :.        
2c6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c6a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c6a2 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 47     sqlite3BtreeG
2c6a3 65 74 41 75 74 6f 56 61 63 75 75 6d 28 70 4d 61  etAutoVacuum(pMa
2c6a4 69 6e 29 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20  in));.#endif..  
2c6a5 2f 2a 20 42 65 67 69 6e 20 61 20 74 72 61 6e 73  /* Begin a trans
2c6a6 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 72 63 20 3d  action */.  rc =
2c6a7 20 65 78 65 63 53 71 6c 28 64 62 2c 20 22 42 45   execSql(db, "BE
2c6a8 47 49 4e 20 45 58 43 4c 55 53 49 56 45 3b 22 29  GIN EXCLUSIVE;")
2c6a9 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
2c6aa 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64  TE_OK ) goto end
2c6ab 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 0a 20 20 2f  _of_vacuum;..  /
2c6ac 2a 20 51 75 65 72 79 20 74 68 65 20 73 63 68 65  * Query the sche
2c6ad 6d 61 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 64  ma of the main d
2c6ae 61 74 61 62 61 73 65 2e 20 43 72 65 61 74 65 20  atabase. Create 
2c6af 61 20 6d 69 72 72 6f 72 20 73 63 68 65 6d 61 0a  a mirror schema.
2c6b0 20 20 2a 2a 20 69 6e 20 74 68 65 20 74 65 6d 70    ** in the temp
2c6b1 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 2e 0a  orary database..
2c6b2 20 20 2a 2f 0a 20 20 72 63 20 3d 20 65 78 65 63    */.  rc = exec
2c6b3 45 78 65 63 53 71 6c 28 64 62 2c 20 0a 20 20 20  ExecSql(db, .   
2c6b4 20 20 20 22 53 45 4c 45 43 54 20 27 43 52 45 41     "SELECT 'CREA
2c6b5 54 45 20 54 41 42 4c 45 20 76 61 63 75 75 6d 5f  TE TABLE vacuum_
2c6b6 64 62 2e 27 20 7c 7c 20 73 75 62 73 74 72 28 73  db.' || substr(s
2c6b7 71 6c 2c 31 34 29 20 22 0a 20 20 20 20 20 20 22  ql,14) ".      "
2c6b8 20 20 46 52 4f 4d 20 73 71 6c 69 74 65 5f 6d 61    FROM sqlite_ma
2c6b9 73 74 65 72 20 57 48 45 52 45 20 74 79 70 65 3d  ster WHERE type=
2c6ba 27 74 61 62 6c 65 27 20 41 4e 44 20 6e 61 6d 65  'table' AND name
2c6bb 21 3d 27 73 71 6c 69 74 65 5f 73 65 71 75 65 6e  !='sqlite_sequen
2c6bc 63 65 27 22 0a 20 20 20 20 20 20 22 20 20 20 41  ce'".      "   A
2c6bd 4e 44 20 72 6f 6f 74 70 61 67 65 3e 30 22 0a 20  ND rootpage>0". 
2c6be 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51   );.  if( rc!=SQ
2c6bf 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65  LITE_OK ) goto e
2c6c0 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20  nd_of_vacuum;.  
2c6c1 72 63 20 3d 20 65 78 65 63 45 78 65 63 53 71 6c  rc = execExecSql
2c6c2 28 64 62 2c 20 0a 20 20 20 20 20 20 22 53 45 4c  (db, .      "SEL
2c6c3 45 43 54 20 27 43 52 45 41 54 45 20 49 4e 44 45  ECT 'CREATE INDE
2c6c4 58 20 76 61 63 75 75 6d 5f 64 62 2e 27 20 7c 7c  X vacuum_db.' ||
2c6c5 20 73 75 62 73 74 72 28 73 71 6c 2c 31 34 29 22   substr(sql,14)"
2c6c6 0a 20 20 20 20 20 20 22 20 20 46 52 4f 4d 20 73  .      "  FROM s
2c6c7 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 57 48 45  qlite_master WHE
2c6c8 52 45 20 73 71 6c 20 4c 49 4b 45 20 27 43 52 45  RE sql LIKE 'CRE
2c6c9 41 54 45 20 49 4e 44 45 58 20 25 27 20 22 29 3b  ATE INDEX %' ");
2c6ca 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
2c6cb 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f  E_OK ) goto end_
2c6cc 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20 72 63 20  of_vacuum;.  rc 
2c6cd 3d 20 65 78 65 63 45 78 65 63 53 71 6c 28 64 62  = execExecSql(db
2c6ce 2c 20 0a 20 20 20 20 20 20 22 53 45 4c 45 43 54  , .      "SELECT
2c6cf 20 27 43 52 45 41 54 45 20 55 4e 49 51 55 45 20   'CREATE UNIQUE 
2c6d0 49 4e 44 45 58 20 76 61 63 75 75 6d 5f 64 62 2e  INDEX vacuum_db.
2c6d1 27 20 7c 7c 20 73 75 62 73 74 72 28 73 71 6c 2c  ' || substr(sql,
2c6d2 32 31 29 20 22 0a 20 20 20 20 20 20 22 20 20 46  21) ".      "  F
2c6d3 52 4f 4d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65  ROM sqlite_maste
2c6d4 72 20 57 48 45 52 45 20 73 71 6c 20 4c 49 4b 45  r WHERE sql LIKE
2c6d5 20 27 43 52 45 41 54 45 20 55 4e 49 51 55 45 20   'CREATE UNIQUE 
2c6d6 49 4e 44 45 58 20 25 27 22 29 3b 0a 20 20 69 66  INDEX %'");.  if
2c6d7 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
2c6d8 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61  ) goto end_of_va
2c6d9 63 75 75 6d 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 70  cuum;..  /* Loop
2c6da 20 74 68 72 6f 75 67 68 20 74 68 65 20 74 61 62   through the tab
2c6db 6c 65 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20  les in the main 
2c6dc 64 61 74 61 62 61 73 65 2e 20 46 6f 72 20 65 61  database. For ea
2c6dd 63 68 2c 20 64 6f 0a 20 20 2a 2a 20 61 6e 20 22  ch, do.  ** an "
2c6de 49 4e 53 45 52 54 20 49 4e 54 4f 20 76 61 63 75  INSERT INTO vacu
2c6df 75 6d 5f 64 62 2e 78 78 78 20 53 45 4c 45 43 54  um_db.xxx SELECT
2c6e0 20 2a 20 46 52 4f 4d 20 78 78 78 3b 22 20 74 6f   * FROM xxx;" to
2c6e1 20 63 6f 70 79 0a 20 20 2a 2a 20 74 68 65 20 63   copy.  ** the c
2c6e2 6f 6e 74 65 6e 74 73 20 74 6f 20 74 68 65 20 74  ontents to the t
2c6e3 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73  emporary databas
2c6e4 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 65  e..  */.  rc = e
2c6e5 78 65 63 45 78 65 63 53 71 6c 28 64 62 2c 20 0a  xecExecSql(db, .
2c6e6 20 20 20 20 20 20 22 53 45 4c 45 43 54 20 27 49        "SELECT 'I
2c6e7 4e 53 45 52 54 20 49 4e 54 4f 20 76 61 63 75 75  NSERT INTO vacuu
2c6e8 6d 5f 64 62 2e 27 20 7c 7c 20 71 75 6f 74 65 28  m_db.' || quote(
2c6e9 6e 61 6d 65 29 20 22 0a 20 20 20 20 20 20 22 7c  name) ".      "|
2c6ea 7c 20 27 20 53 45 4c 45 43 54 20 2a 20 46 52 4f  | ' SELECT * FRO
2c6eb 4d 20 27 20 7c 7c 20 71 75 6f 74 65 28 6e 61 6d  M ' || quote(nam
2c6ec 65 29 20 7c 7c 20 27 3b 27 22 0a 20 20 20 20 20  e) || ';'".     
2c6ed 20 22 46 52 4f 4d 20 73 71 6c 69 74 65 5f 6d 61   "FROM sqlite_ma
2c6ee 73 74 65 72 20 22 0a 20 20 20 20 20 20 22 57 48  ster ".      "WH
2c6ef 45 52 45 20 74 79 70 65 20 3d 20 27 74 61 62 6c  ERE type = 'tabl
2c6f0 65 27 20 41 4e 44 20 6e 61 6d 65 21 3d 27 73 71  e' AND name!='sq
2c6f1 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 27 20 22  lite_sequence' "
2c6f2 0a 20 20 20 20 20 20 22 20 20 41 4e 44 20 72 6f  .      "  AND ro
2c6f3 6f 74 70 61 67 65 3e 30 22 0a 0a 20 20 29 3b 0a  otpage>0"..  );.
2c6f4 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2c6f5 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f  _OK ) goto end_o
2c6f6 66 5f 76 61 63 75 75 6d 3b 0a 0a 20 20 2f 2a 20  f_vacuum;..  /* 
2c6f7 43 6f 70 79 20 6f 76 65 72 20 74 68 65 20 73 65  Copy over the se
2c6f8 71 75 65 6e 63 65 20 74 61 62 6c 65 0a 20 20 2a  quence table.  *
2c6f9 2f 0a 20 20 72 63 20 3d 20 65 78 65 63 45 78 65  /.  rc = execExe
2c6fa 63 53 71 6c 28 64 62 2c 20 0a 20 20 20 20 20 20  cSql(db, .      
2c6fb 22 53 45 4c 45 43 54 20 27 44 45 4c 45 54 45 20  "SELECT 'DELETE 
2c6fc 46 52 4f 4d 20 76 61 63 75 75 6d 5f 64 62 2e 27  FROM vacuum_db.'
2c6fd 20 7c 7c 20 71 75 6f 74 65 28 6e 61 6d 65 29 20   || quote(name) 
2c6fe 7c 7c 20 27 3b 27 20 22 0a 20 20 20 20 20 20 22  || ';' ".      "
2c6ff 46 52 4f 4d 20 76 61 63 75 75 6d 5f 64 62 2e 73  FROM vacuum_db.s
2c700 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 57 48 45  qlite_master WHE
2c701 52 45 20 6e 61 6d 65 3d 27 73 71 6c 69 74 65 5f  RE name='sqlite_
2c702 73 65 71 75 65 6e 63 65 27 20 22 0a 20 20 29 3b  sequence' ".  );
2c703 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
2c704 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f  E_OK ) goto end_
2c705 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20 72 63 20  of_vacuum;.  rc 
2c706 3d 20 65 78 65 63 45 78 65 63 53 71 6c 28 64 62  = execExecSql(db
2c707 2c 20 0a 20 20 20 20 20 20 22 53 45 4c 45 43 54  , .      "SELECT
2c708 20 27 49 4e 53 45 52 54 20 49 4e 54 4f 20 76 61   'INSERT INTO va
2c709 63 75 75 6d 5f 64 62 2e 27 20 7c 7c 20 71 75 6f  cuum_db.' || quo
2c70a 74 65 28 6e 61 6d 65 29 20 22 0a 20 20 20 20 20  te(name) ".     
2c70b 20 22 7c 7c 20 27 20 53 45 4c 45 43 54 20 2a 20   "|| ' SELECT * 
2c70c 46 52 4f 4d 20 27 20 7c 7c 20 71 75 6f 74 65 28  FROM ' || quote(
2c70d 6e 61 6d 65 29 20 7c 7c 20 27 3b 27 20 22 0a 20  name) || ';' ". 
2c70e 20 20 20 20 20 22 46 52 4f 4d 20 76 61 63 75 75       "FROM vacuu
2c70f 6d 5f 64 62 2e 73 71 6c 69 74 65 5f 6d 61 73 74  m_db.sqlite_mast
2c710 65 72 20 57 48 45 52 45 20 6e 61 6d 65 3d 3d 27  er WHERE name=='
2c711 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 27  sqlite_sequence'
2c712 3b 22 0a 20 20 29 3b 0a 20 20 69 66 28 20 72 63  ;".  );.  if( rc
2c713 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
2c714 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d  to end_of_vacuum
2c715 3b 0a 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 74 68  ;...  /* Copy th
2c716 65 20 74 72 69 67 67 65 72 73 2c 20 76 69 65 77  e triggers, view
2c717 73 2c 20 61 6e 64 20 76 69 72 74 75 61 6c 20 74  s, and virtual t
2c718 61 62 6c 65 73 20 66 72 6f 6d 20 74 68 65 20 6d  ables from the m
2c719 61 69 6e 20 64 61 74 61 62 61 73 65 0a 20 20 2a  ain database.  *
2c71a 2a 20 6f 76 65 72 20 74 6f 20 74 68 65 20 74 65  * over to the te
2c71b 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65  mporary database
2c71c 2e 20 20 4e 6f 6e 65 20 6f 66 20 74 68 65 73 65  .  None of these
2c71d 20 6f 62 6a 65 63 74 73 20 68 61 73 20 61 6e 79   objects has any
2c71e 0a 20 20 2a 2a 20 61 73 73 6f 63 69 61 74 65 64  .  ** associated
2c71f 20 73 74 6f 72 61 67 65 2c 20 73 6f 20 61 6c 6c   storage, so all
2c720 20 77 65 20 68 61 76 65 20 74 6f 20 64 6f 20 69   we have to do i
2c721 73 20 63 6f 70 79 20 74 68 65 69 72 20 65 6e 74  s copy their ent
2c722 72 69 65 73 0a 20 20 2a 2a 20 66 72 6f 6d 20 74  ries.  ** from t
2c723 68 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52  he SQLITE_MASTER
2c724 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72   table..  */.  r
2c725 63 20 3d 20 65 78 65 63 53 71 6c 28 64 62 2c 0a  c = execSql(db,.
2c726 20 20 20 20 20 20 22 49 4e 53 45 52 54 20 49 4e        "INSERT IN
2c727 54 4f 20 76 61 63 75 75 6d 5f 64 62 2e 73 71 6c  TO vacuum_db.sql
2c728 69 74 65 5f 6d 61 73 74 65 72 20 22 0a 20 20 20  ite_master ".   
2c729 20 20 20 22 20 20 53 45 4c 45 43 54 20 74 79 70     "  SELECT typ
2c72a 65 2c 20 6e 61 6d 65 2c 20 74 62 6c 5f 6e 61 6d  e, name, tbl_nam
2c72b 65 2c 20 72 6f 6f 74 70 61 67 65 2c 20 73 71 6c  e, rootpage, sql
2c72c 22 0a 20 20 20 20 20 20 22 20 20 20 20 46 52 4f  ".      "    FRO
2c72d 4d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 22  M sqlite_master"
2c72e 0a 20 20 20 20 20 20 22 20 20 20 57 48 45 52 45  .      "   WHERE
2c72f 20 74 79 70 65 3d 27 76 69 65 77 27 20 4f 52 20   type='view' OR 
2c730 74 79 70 65 3d 27 74 72 69 67 67 65 72 27 22 0a  type='trigger'".
2c731 20 20 20 20 20 20 22 20 20 20 20 20 20 4f 52 20        "      OR 
2c732 28 74 79 70 65 3d 27 74 61 62 6c 65 27 20 41 4e  (type='table' AN
2c733 44 20 72 6f 6f 74 70 61 67 65 3d 30 29 22 0a 20  D rootpage=0)". 
2c734 20 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 67   );.  if( rc ) g
2c735 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75  oto end_of_vacuu
2c736 6d 3b 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73  m;..  /* At this
2c737 20 70 6f 69 6e 74 2c 20 75 6e 6c 65 73 73 20 74   point, unless t
2c738 68 65 20 6d 61 69 6e 20 64 62 20 77 61 73 20 63  he main db was c
2c739 6f 6d 70 6c 65 74 65 6c 79 20 65 6d 70 74 79 2c  ompletely empty,
2c73a 20 74 68 65 72 65 20 69 73 20 6e 6f 77 20 61 0a   there is now a.
2c73b 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e    ** transaction
2c73c 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 76 61 63   open on the vac
2c73d 75 75 6d 20 64 61 74 61 62 61 73 65 2c 20 62 75  uum database, bu
2c73e 74 20 6e 6f 74 20 6f 6e 20 74 68 65 20 6d 61 69  t not on the mai
2c73f 6e 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2a  n database..  **
2c740 20 4f 70 65 6e 20 61 20 62 74 72 65 65 20 6c 65   Open a btree le
2c741 76 65 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  vel transaction 
2c742 6f 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  on the main data
2c743 62 61 73 65 2e 20 54 68 69 73 20 61 6c 6c 6f 77  base. This allow
2c744 73 20 61 0a 20 20 2a 2a 20 63 61 6c 6c 20 74 6f  s a.  ** call to
2c745 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 70   sqlite3BtreeCop
2c746 79 46 69 6c 65 28 29 2e 20 54 68 65 20 6d 61 69  yFile(). The mai
2c747 6e 20 64 61 74 61 62 61 73 65 20 62 74 72 65 65  n database btree
2c748 20 6c 65 76 65 6c 0a 20 20 2a 2a 20 74 72 61 6e   level.  ** tran
2c749 73 61 63 74 69 6f 6e 20 69 73 20 74 68 65 6e 20  saction is then 
2c74a 63 6f 6d 6d 69 74 74 65 64 2c 20 73 6f 20 74 68  committed, so th
2c74b 65 20 53 51 4c 20 6c 65 76 65 6c 20 6e 65 76 65  e SQL level neve
2c74c 72 20 6b 6e 6f 77 73 20 69 74 20 77 61 73 0a 20  r knows it was. 
2c74d 20 2a 2a 20 6f 70 65 6e 65 64 20 66 6f 72 20 77   ** opened for w
2c74e 72 69 74 69 6e 67 2e 20 54 68 69 73 20 77 61 79  riting. This way
2c74f 2c 20 74 68 65 20 53 51 4c 20 74 72 61 6e 73 61  , the SQL transa
2c750 63 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 63 72  ction used to cr
2c751 65 61 74 65 20 74 68 65 0a 20 20 2a 2a 20 74 65  eate the.  ** te
2c752 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65  mporary database
2c753 20 6e 65 76 65 72 20 6e 65 65 64 73 20 74 6f 20   never needs to 
2c754 62 65 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 20 20  be committed..  
2c755 2a 2f 0a 20 20 7b 0a 20 20 20 20 75 33 32 20 6d  */.  {.    u32 m
2c756 65 74 61 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  eta;.    int i;.
2c757 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 61 72 72  .    /* This arr
2c758 61 79 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68  ay determines wh
2c759 69 63 68 20 6d 65 74 61 20 6d 65 74 61 20 76 61  ich meta meta va
2c75a 6c 75 65 73 20 61 72 65 20 70 72 65 73 65 72 76  lues are preserv
2c75b 65 64 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a  ed in the.    **
2c75c 20 76 61 63 75 75 6d 2e 20 20 45 76 65 6e 20 65   vacuum.  Even e
2c75d 6e 74 72 69 65 73 20 61 72 65 20 74 68 65 20 6d  ntries are the m
2c75e 65 74 61 20 76 61 6c 75 65 20 6e 75 6d 62 65 72  eta value number
2c75f 20 61 6e 64 20 6f 64 64 20 65 6e 74 72 69 65 73   and odd entries
2c760 0a 20 20 20 20 2a 2a 20 61 72 65 20 61 6e 20 69  .    ** are an i
2c761 6e 63 72 65 6d 65 6e 74 20 74 6f 20 61 70 70 6c  ncrement to appl
2c762 79 20 74 6f 20 74 68 65 20 6d 65 74 61 20 76 61  y to the meta va
2c763 6c 75 65 20 61 66 74 65 72 20 74 68 65 20 76 61  lue after the va
2c764 63 75 75 6d 2e 0a 20 20 20 20 2a 2a 20 54 68 65  cuum..    ** The
2c765 20 69 6e 63 72 65 6d 65 6e 74 20 69 73 20 75 73   increment is us
2c766 65 64 20 74 6f 20 69 6e 63 72 65 61 73 65 20 74  ed to increase t
2c767 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65  he schema cookie
2c768 20 73 6f 20 74 68 61 74 20 6f 74 68 65 72 0a 20   so that other. 
2c769 20 20 20 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e     ** connection
2c76a 73 20 74 6f 20 74 68 65 20 73 61 6d 65 20 64 61  s to the same da
2c76b 74 61 62 61 73 65 20 77 69 6c 6c 20 6b 6e 6f 77  tabase will know
2c76c 20 74 6f 20 72 65 72 65 61 64 20 74 68 65 20 73   to reread the s
2c76d 63 68 65 6d 61 2e 0a 20 20 20 20 2a 2f 0a 20 20  chema..    */.  
2c76e 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
2c76f 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61 43 6f  nsigned char aCo
2c770 70 79 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20  py[] = {.       
2c771 42 54 52 45 45 5f 53 43 48 45 4d 41 5f 56 45 52  BTREE_SCHEMA_VER
2c772 53 49 4f 4e 2c 20 20 20 20 20 31 2c 20 20 2f 2a  SION,     1,  /*
2c773 20 41 64 64 20 6f 6e 65 20 74 6f 20 74 68 65 20   Add one to the 
2c774 6f 6c 64 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69  old schema cooki
2c775 65 20 2a 2f 0a 20 20 20 20 20 20 20 42 54 52 45  e */.       BTRE
2c776 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f  E_DEFAULT_CACHE_
2c777 53 49 5a 45 2c 20 30 2c 20 20 2f 2a 20 50 72 65  SIZE, 0,  /* Pre
2c778 73 65 72 76 65 20 74 68 65 20 64 65 66 61 75 6c  serve the defaul
2c779 74 20 70 61 67 65 20 63 61 63 68 65 20 73 69 7a  t page cache siz
2c77a 65 20 2a 2f 0a 20 20 20 20 20 20 20 42 54 52 45  e */.       BTRE
2c77b 45 5f 54 45 58 54 5f 45 4e 43 4f 44 49 4e 47 2c  E_TEXT_ENCODING,
2c77c 20 20 20 20 20 20 30 2c 20 20 2f 2a 20 50 72 65        0,  /* Pre
2c77d 73 65 72 76 65 20 74 68 65 20 74 65 78 74 20 65  serve the text e
2c77e 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 20 20 20  ncoding */.     
2c77f 20 20 42 54 52 45 45 5f 55 53 45 52 5f 56 45 52    BTREE_USER_VER
2c780 53 49 4f 4e 2c 20 20 20 20 20 20 20 30 2c 20 20  SION,       0,  
2c781 2f 2a 20 50 72 65 73 65 72 76 65 20 74 68 65 20  /* Preserve the 
2c782 75 73 65 72 20 76 65 72 73 69 6f 6e 20 2a 2f 0a  user version */.
2c783 20 20 20 20 7d 3b 0a 0a 20 20 20 20 61 73 73 65      };..    asse
2c784 72 74 28 20 31 3d 3d 73 71 6c 69 74 65 33 42 74  rt( 1==sqlite3Bt
2c785 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70 54 65  reeIsInTrans(pTe
2c786 6d 70 29 20 29 3b 0a 20 20 20 20 61 73 73 65 72  mp) );.    asser
2c787 74 28 20 31 3d 3d 73 71 6c 69 74 65 33 42 74 72  t( 1==sqlite3Btr
2c788 65 65 49 73 49 6e 54 72 61 6e 73 28 70 4d 61 69  eeIsInTrans(pMai
2c789 6e 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f  n) );..    /* Co
2c78a 70 79 20 42 74 72 65 65 20 6d 65 74 61 20 76 61  py Btree meta va
2c78b 6c 75 65 73 20 2a 2f 0a 20 20 20 20 66 6f 72 28  lues */.    for(
2c78c 69 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65  i=0; i<ArraySize
2c78d 28 61 43 6f 70 79 29 3b 20 69 2b 3d 32 29 7b 0a  (aCopy); i+=2){.
2c78e 20 20 20 20 20 20 2f 2a 20 47 65 74 4d 65 74 61        /* GetMeta
2c78f 28 29 20 61 6e 64 20 55 70 64 61 74 65 4d 65 74  () and UpdateMet
2c790 61 28 29 20 63 61 6e 6e 6f 74 20 66 61 69 6c 20  a() cannot fail 
2c791 69 6e 20 74 68 69 73 20 63 6f 6e 74 65 78 74 20  in this context 
2c792 62 65 63 61 75 73 65 0a 20 20 20 20 20 20 2a 2a  because.      **
2c793 20 77 65 20 61 6c 72 65 61 64 79 20 68 61 76 65   we already have
2c794 20 70 61 67 65 20 31 20 6c 6f 61 64 65 64 20 69   page 1 loaded i
2c795 6e 74 6f 20 63 61 63 68 65 20 61 6e 64 20 6d 61  nto cache and ma
2c796 72 6b 65 64 20 64 69 72 74 79 2e 20 2a 2f 0a 20  rked dirty. */. 
2c797 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
2c798 65 47 65 74 4d 65 74 61 28 70 4d 61 69 6e 2c 20  eGetMeta(pMain, 
2c799 61 43 6f 70 79 5b 69 5d 2c 20 26 6d 65 74 61 29  aCopy[i], &meta)
2c79a 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
2c79b 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d  ite3BtreeUpdateM
2c79c 65 74 61 28 70 54 65 6d 70 2c 20 61 43 6f 70 79  eta(pTemp, aCopy
2c79d 5b 69 5d 2c 20 6d 65 74 61 2b 61 43 6f 70 79 5b  [i], meta+aCopy[
2c79e 69 2b 31 5d 29 3b 0a 20 20 20 20 20 20 69 66 28  i+1]);.      if(
2c79f 20 4e 45 56 45 52 28 72 63 21 3d 53 51 4c 49 54   NEVER(rc!=SQLIT
2c7a0 45 5f 4f 4b 29 20 29 20 67 6f 74 6f 20 65 6e 64  E_OK) ) goto end
2c7a1 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20 20 20  _of_vacuum;.    
2c7a2 7d 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  }..    rc = sqli
2c7a3 74 65 33 42 74 72 65 65 43 6f 70 79 46 69 6c 65  te3BtreeCopyFile
2c7a4 28 70 4d 61 69 6e 2c 20 70 54 65 6d 70 29 3b 0a  (pMain, pTemp);.
2c7a5 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
2c7a6 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64  TE_OK ) goto end
2c7a7 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20 20 20  _of_vacuum;.    
2c7a8 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
2c7a9 65 43 6f 6d 6d 69 74 28 70 54 65 6d 70 29 3b 0a  eCommit(pTemp);.
2c7aa 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
2c7ab 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64  TE_OK ) goto end
2c7ac 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 23 69 66 6e  _of_vacuum;.#ifn
2c7ad 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2c7ae 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 73  AUTOVACUUM.    s
2c7af 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 41 75  qlite3BtreeSetAu
2c7b0 74 6f 56 61 63 75 75 6d 28 70 4d 61 69 6e 2c 20  toVacuum(pMain, 
2c7b1 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 41  sqlite3BtreeGetA
2c7b2 75 74 6f 56 61 63 75 75 6d 28 70 54 65 6d 70 29  utoVacuum(pTemp)
2c7b3 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20  );.#endif.  }.. 
2c7b4 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
2c7b5 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 72 63 20 3d  ITE_OK );.  rc =
2c7b6 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74   sqlite3BtreeSet
2c7b7 50 61 67 65 53 69 7a 65 28 70 4d 61 69 6e 2c 20  PageSize(pMain, 
2c7b8 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50  sqlite3BtreeGetP
2c7b9 61 67 65 53 69 7a 65 28 70 54 65 6d 70 29 2c 20  ageSize(pTemp), 
2c7ba 6e 52 65 73 2c 31 29 3b 0a 0a 65 6e 64 5f 6f 66  nRes,1);..end_of
2c7bb 5f 76 61 63 75 75 6d 3a 0a 20 20 2f 2a 20 52 65  _vacuum:.  /* Re
2c7bc 73 74 6f 72 65 20 74 68 65 20 6f 72 69 67 69 6e  store the origin
2c7bd 61 6c 20 76 61 6c 75 65 20 6f 66 20 64 62 2d 3e  al value of db->
2c7be 66 6c 61 67 73 20 2a 2f 0a 20 20 64 62 2d 3e 66  flags */.  db->f
2c7bf 6c 61 67 73 20 3d 20 73 61 76 65 64 5f 66 6c 61  lags = saved_fla
2c7c0 67 73 3b 0a 20 20 64 62 2d 3e 6e 43 68 61 6e 67  gs;.  db->nChang
2c7c1 65 20 3d 20 73 61 76 65 64 5f 6e 43 68 61 6e 67  e = saved_nChang
2c7c2 65 3b 0a 20 20 64 62 2d 3e 6e 54 6f 74 61 6c 43  e;.  db->nTotalC
2c7c3 68 61 6e 67 65 20 3d 20 73 61 76 65 64 5f 6e 54  hange = saved_nT
2c7c4 6f 74 61 6c 43 68 61 6e 67 65 3b 0a 0a 20 20 2f  otalChange;..  /
2c7c5 2a 20 43 75 72 72 65 6e 74 6c 79 20 74 68 65 72  * Currently ther
2c7c6 65 20 69 73 20 61 6e 20 53 51 4c 20 6c 65 76 65  e is an SQL leve
2c7c7 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70  l transaction op
2c7c8 65 6e 20 6f 6e 20 74 68 65 20 76 61 63 75 75 6d  en on the vacuum
2c7c9 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20  .  ** database. 
2c7ca 4e 6f 20 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c  No locks are hel
2c7cb 64 20 6f 6e 20 61 6e 79 20 6f 74 68 65 72 20 66  d on any other f
2c7cc 69 6c 65 73 20 28 73 69 6e 63 65 20 74 68 65 20  iles (since the 
2c7cd 6d 61 69 6e 20 66 69 6c 65 0a 20 20 2a 2a 20 77  main file.  ** w
2c7ce 61 73 20 63 6f 6d 6d 69 74 74 65 64 20 61 74 20  as committed at 
2c7cf 74 68 65 20 62 74 72 65 65 20 6c 65 76 65 6c 29  the btree level)
2c7d0 2e 20 53 6f 20 69 74 20 73 61 66 65 20 74 6f 20  . So it safe to 
2c7d1 65 6e 64 20 74 68 65 20 74 72 61 6e 73 61 63 74  end the transact
2c7d2 69 6f 6e 0a 20 20 2a 2a 20 62 79 20 6d 61 6e 75  ion.  ** by manu
2c7d3 61 6c 6c 79 20 73 65 74 74 69 6e 67 20 74 68 65  ally setting the
2c7d4 20 61 75 74 6f 43 6f 6d 6d 69 74 20 66 6c 61 67   autoCommit flag
2c7d5 20 74 6f 20 74 72 75 65 20 61 6e 64 20 64 65 74   to true and det
2c7d6 61 63 68 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20  aching the.  ** 
2c7d7 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 2e  vacuum database.
2c7d8 20 54 68 65 20 76 61 63 75 75 6d 5f 64 62 20 6a   The vacuum_db j
2c7d9 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 64  ournal file is d
2c7da 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68 65 20  eleted when the 
2c7db 70 61 67 65 72 0a 20 20 2a 2a 20 69 73 20 63 6c  pager.  ** is cl
2c7dc 6f 73 65 64 20 62 79 20 74 68 65 20 44 45 54 41  osed by the DETA
2c7dd 43 48 2e 0a 20 20 2a 2f 0a 20 20 64 62 2d 3e 61  CH..  */.  db->a
2c7de 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 0a  utoCommit = 1;..
2c7df 20 20 69 66 28 20 70 44 62 20 29 7b 0a 20 20 20    if( pDb ){.   
2c7e0 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f   sqlite3BtreeClo
2c7e1 73 65 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20  se(pDb->pBt);.  
2c7e2 20 20 70 44 62 2d 3e 70 42 74 20 3d 20 30 3b 0a    pDb->pBt = 0;.
2c7e3 20 20 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61      pDb->pSchema
2c7e4 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c   = 0;.  }..  sql
2c7e5 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61  ite3ResetInterna
2c7e6 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a  lSchema(db, 0);.
2c7e7 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
2c7e8 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 54  #endif  /* SQLIT
2c7e9 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 20 26 26  E_OMIT_VACUUM &&
2c7ea 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54   SQLITE_OMIT_ATT
2c7eb 41 43 48 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  ACH */../*******
2c7ec 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76  ******* End of v
2c7ed 61 63 75 75 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  acuum.c ********
2c7ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c7ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c7f0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
2c7f1 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
2c7f2 6c 65 20 76 74 61 62 2e 63 20 2a 2a 2a 2a 2a 2a  le vtab.c ******
2c7f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c7f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c7f5 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
2c7f6 30 36 20 4a 75 6e 65 20 31 30 0a 2a 2a 0a 2a 2a  06 June 10.**.**
2c7f7 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
2c7f8 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
2c7f9 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
2c7fa 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
2c7fb 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
2c7fc 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
2c7fd 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
2c7fe 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
2c7ff 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
2c800 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
2c801 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
2c802 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
2c803 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
2c804 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
2c805 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
2c806 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
2c807 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
2c808 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c809 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c80a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c80b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c80c 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
2c80d 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
2c80e 63 6f 64 65 20 75 73 65 64 20 74 6f 20 68 65 6c  code used to hel
2c80f 70 20 69 6d 70 6c 65 6d 65 6e 74 20 76 69 72 74  p implement virt
2c810 75 61 6c 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a  ual tables..**.*
2c811 2a 20 24 49 64 3a 20 76 74 61 62 2e 63 2c 76 20  * $Id: vtab.c,v 
2c812 31 2e 39 34 20 32 30 30 39 2f 30 38 2f 30 38 20  1.94 2009/08/08 
2c813 31 38 3a 30 31 3a 30 38 20 64 72 68 20 45 78 70  18:01:08 drh Exp
2c814 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51   $.*/.#ifndef SQ
2c815 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
2c816 4c 54 41 42 4c 45 0a 0a 2f 2a 0a 2a 2a 20 54 68  LTABLE../*.** Th
2c817 65 20 61 63 74 75 61 6c 20 66 75 6e 63 74 69 6f  e actual functio
2c818 6e 20 74 68 61 74 20 64 6f 65 73 20 74 68 65 20  n that does the 
2c819 77 6f 72 6b 20 6f 66 20 63 72 65 61 74 69 6e 67  work of creating
2c81a 20 61 20 6e 65 77 20 6d 6f 64 75 6c 65 2e 0a 2a   a new module..*
2c81b 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
2c81c 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 73  implements the s
2c81d 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f  qlite3_create_mo
2c81e 64 75 6c 65 28 29 20 61 6e 64 0a 2a 2a 20 73 71  dule() and.** sq
2c81f 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64  lite3_create_mod
2c820 75 6c 65 5f 76 32 28 29 20 69 6e 74 65 72 66 61  ule_v2() interfa
2c821 63 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ces..*/.static i
2c822 6e 74 20 63 72 65 61 74 65 4d 6f 64 75 6c 65 28  nt createModule(
2c823 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
2c824 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c825 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69     /* Database i
2c826 6e 20 77 68 69 63 68 20 6d 6f 64 75 6c 65 20 69  n which module i
2c827 73 20 72 65 67 69 73 74 65 72 65 64 20 2a 2f 0a  s registered */.
2c828 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
2c829 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ame,            
2c82a 20 20 2f 2a 20 4e 61 6d 65 20 61 73 73 69 67 6e    /* Name assign
2c82b 65 64 20 74 6f 20 74 68 69 73 20 6d 6f 64 75 6c  ed to this modul
2c82c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c  e */.  const sql
2c82d 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f  ite3_module *pMo
2c82e 64 75 6c 65 2c 20 20 2f 2a 20 54 68 65 20 64 65  dule,  /* The de
2c82f 66 69 6e 69 74 69 6f 6e 20 6f 66 20 74 68 65 20  finition of the 
2c830 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 76 6f 69 64  module */.  void
2c831 20 2a 70 41 75 78 2c 20 20 20 20 20 20 20 20 20   *pAux,         
2c832 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
2c833 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 20 66  ontext pointer f
2c834 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e  or xCreate/xConn
2c835 65 63 74 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a  ect */.  void (*
2c836 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64 20 2a  xDestroy)(void *
2c837 29 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 64 75  )        /* Modu
2c838 6c 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 75  le destructor fu
2c839 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 69  nction */.){.  i
2c83a 6e 74 20 72 63 2c 20 6e 4e 61 6d 65 3b 0a 20 20  nt rc, nName;.  
2c83b 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 3b 0a 0a 20  Module *pMod;.. 
2c83c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
2c83d 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
2c83e 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74  .  nName = sqlit
2c83f 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65  e3Strlen30(zName
2c840 29 3b 0a 20 20 70 4d 6f 64 20 3d 20 28 4d 6f 64  );.  pMod = (Mod
2c841 75 6c 65 20 2a 29 73 71 6c 69 74 65 33 44 62 4d  ule *)sqlite3DbM
2c842 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a  allocRaw(db, siz
2c843 65 6f 66 28 4d 6f 64 75 6c 65 29 20 2b 20 6e 4e  eof(Module) + nN
2c844 61 6d 65 20 2b 20 31 29 3b 0a 20 20 69 66 28 20  ame + 1);.  if( 
2c845 70 4d 6f 64 20 29 7b 0a 20 20 20 20 4d 6f 64 75  pMod ){.    Modu
2c846 6c 65 20 2a 70 44 65 6c 3b 0a 20 20 20 20 63 68  le *pDel;.    ch
2c847 61 72 20 2a 7a 43 6f 70 79 20 3d 20 28 63 68 61  ar *zCopy = (cha
2c848 72 20 2a 29 28 26 70 4d 6f 64 5b 31 5d 29 3b 0a  r *)(&pMod[1]);.
2c849 20 20 20 20 6d 65 6d 63 70 79 28 7a 43 6f 70 79      memcpy(zCopy
2c84a 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2b 31  , zName, nName+1
2c84b 29 3b 0a 20 20 20 20 70 4d 6f 64 2d 3e 7a 4e 61  );.    pMod->zNa
2c84c 6d 65 20 3d 20 7a 43 6f 70 79 3b 0a 20 20 20 20  me = zCopy;.    
2c84d 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20  pMod->pModule = 
2c84e 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 70 4d 6f  pModule;.    pMo
2c84f 64 2d 3e 70 41 75 78 20 3d 20 70 41 75 78 3b 0a  d->pAux = pAux;.
2c850 20 20 20 20 70 4d 6f 64 2d 3e 78 44 65 73 74 72      pMod->xDestr
2c851 6f 79 20 3d 20 78 44 65 73 74 72 6f 79 3b 0a 20  oy = xDestroy;. 
2c852 20 20 20 70 44 65 6c 20 3d 20 28 4d 6f 64 75 6c     pDel = (Modul
2c853 65 20 2a 29 73 71 6c 69 74 65 33 48 61 73 68 49  e *)sqlite3HashI
2c854 6e 73 65 72 74 28 26 64 62 2d 3e 61 4d 6f 64 75  nsert(&db->aModu
2c855 6c 65 2c 20 7a 43 6f 70 79 2c 20 6e 4e 61 6d 65  le, zCopy, nName
2c856 2c 20 28 76 6f 69 64 2a 29 70 4d 6f 64 29 3b 0a  , (void*)pMod);.
2c857 20 20 20 20 69 66 28 20 70 44 65 6c 20 26 26 20      if( pDel && 
2c858 70 44 65 6c 2d 3e 78 44 65 73 74 72 6f 79 20 29  pDel->xDestroy )
2c859 7b 0a 20 20 20 20 20 20 70 44 65 6c 2d 3e 78 44  {.      pDel->xD
2c85a 65 73 74 72 6f 79 28 70 44 65 6c 2d 3e 70 41 75  estroy(pDel->pAu
2c85b 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  x);.    }.    sq
2c85c 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2c85d 70 44 65 6c 29 3b 0a 20 20 20 20 69 66 28 20 70  pDel);.    if( p
2c85e 44 65 6c 3d 3d 70 4d 6f 64 20 29 7b 0a 20 20 20  Del==pMod ){.   
2c85f 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
2c860 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  led = 1;.    }. 
2c861 20 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49     sqlite3ResetI
2c862 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62  nternalSchema(db
2c863 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  , 0);.  }else if
2c864 28 20 78 44 65 73 74 72 6f 79 20 29 7b 0a 20 20  ( xDestroy ){.  
2c865 20 20 78 44 65 73 74 72 6f 79 28 70 41 75 78 29    xDestroy(pAux)
2c866 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c  ;.  }.  rc = sql
2c867 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20  ite3ApiExit(db, 
2c868 53 51 4c 49 54 45 5f 4f 4b 29 3b 0a 20 20 73 71  SQLITE_OK);.  sq
2c869 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
2c86a 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
2c86b 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f  return rc;.}.../
2c86c 2a 0a 2a 2a 20 45 78 74 65 72 6e 61 6c 20 41 50  *.** External AP
2c86d 49 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 64 20  I function used 
2c86e 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 20  to create a new 
2c86f 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 6f  virtual-table mo
2c870 64 75 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  dule..*/.SQLITE_
2c871 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
2c872 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 0a 20  create_module(. 
2c873 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
2c874 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c875 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 6e 20   /* Database in 
2c876 77 68 69 63 68 20 6d 6f 64 75 6c 65 20 69 73 20  which module is 
2c877 72 65 67 69 73 74 65 72 65 64 20 2a 2f 0a 20 20  registered */.  
2c878 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
2c879 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
2c87a 2f 2a 20 4e 61 6d 65 20 61 73 73 69 67 6e 65 64  /* Name assigned
2c87b 20 74 6f 20 74 68 69 73 20 6d 6f 64 75 6c 65 20   to this module 
2c87c 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  */.  const sqlit
2c87d 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75  e3_module *pModu
2c87e 6c 65 2c 20 20 2f 2a 20 54 68 65 20 64 65 66 69  le,  /* The defi
2c87f 6e 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f  nition of the mo
2c880 64 75 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  dule */.  void *
2c881 70 41 75 78 20 20 20 20 20 20 20 20 20 20 20 20  pAux            
2c882 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e            /* Con
2c883 74 65 78 74 20 70 6f 69 6e 74 65 72 20 66 6f 72  text pointer for
2c884 20 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63   xCreate/xConnec
2c885 74 20 2a 2f 0a 29 7b 0a 20 20 72 65 74 75 72 6e  t */.){.  return
2c886 20 63 72 65 61 74 65 4d 6f 64 75 6c 65 28 64 62   createModule(db
2c887 2c 20 7a 4e 61 6d 65 2c 20 70 4d 6f 64 75 6c 65  , zName, pModule
2c888 2c 20 70 41 75 78 2c 20 30 29 3b 0a 7d 0a 0a 2f  , pAux, 0);.}../
2c889 2a 0a 2a 2a 20 45 78 74 65 72 6e 61 6c 20 41 50  *.** External AP
2c88a 49 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 64 20  I function used 
2c88b 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 20  to create a new 
2c88c 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 6f  virtual-table mo
2c88d 64 75 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  dule..*/.SQLITE_
2c88e 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
2c88f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32  create_module_v2
2c890 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
2c891 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c892 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
2c893 69 6e 20 77 68 69 63 68 20 6d 6f 64 75 6c 65 20  in which module 
2c894 69 73 20 72 65 67 69 73 74 65 72 65 64 20 2a 2f  is registered */
2c895 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2c896 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20  Name,           
2c897 20 20 20 2f 2a 20 4e 61 6d 65 20 61 73 73 69 67     /* Name assig
2c898 6e 65 64 20 74 6f 20 74 68 69 73 20 6d 6f 64 75  ned to this modu
2c899 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71  le */.  const sq
2c89a 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d  lite3_module *pM
2c89b 6f 64 75 6c 65 2c 20 20 2f 2a 20 54 68 65 20 64  odule,  /* The d
2c89c 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 74 68 65  efinition of the
2c89d 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 76 6f 69   module */.  voi
2c89e 64 20 2a 70 41 75 78 2c 20 20 20 20 20 20 20 20  d *pAux,        
2c89f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2c8a0 43 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 20  Context pointer 
2c8a1 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f 6e  for xCreate/xCon
2c8a2 6e 65 63 74 20 2a 2f 0a 20 20 76 6f 69 64 20 28  nect */.  void (
2c8a3 2a 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64 20  *xDestroy)(void 
2c8a4 2a 29 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 64  *)        /* Mod
2c8a5 75 6c 65 20 64 65 73 74 72 75 63 74 6f 72 20 66  ule destructor f
2c8a6 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20  unction */.){.  
2c8a7 72 65 74 75 72 6e 20 63 72 65 61 74 65 4d 6f 64  return createMod
2c8a8 75 6c 65 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 70  ule(db, zName, p
2c8a9 4d 6f 64 75 6c 65 2c 20 70 41 75 78 2c 20 78 44  Module, pAux, xD
2c8aa 65 73 74 72 6f 79 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  estroy);.}../*.*
2c8ab 2a 20 4c 6f 63 6b 20 74 68 65 20 76 69 72 74 75  * Lock the virtu
2c8ac 61 6c 20 74 61 62 6c 65 20 73 6f 20 74 68 61 74  al table so that
2c8ad 20 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 64 69   it cannot be di
2c8ae 73 63 6f 6e 6e 65 63 74 65 64 2e 0a 2a 2a 20 4c  sconnected..** L
2c8af 6f 63 6b 73 20 6e 65 73 74 2e 20 20 45 76 65 72  ocks nest.  Ever
2c8b0 79 20 6c 6f 63 6b 20 73 68 6f 75 6c 64 20 68 61  y lock should ha
2c8b1 76 65 20 61 20 63 6f 72 72 65 73 70 6f 6e 64 69  ve a correspondi
2c8b2 6e 67 20 75 6e 6c 6f 63 6b 2e 0a 2a 2a 20 49 66  ng unlock..** If
2c8b3 20 61 6e 20 75 6e 6c 6f 63 6b 20 69 73 20 6f 6d   an unlock is om
2c8b4 69 74 74 65 64 2c 20 72 65 73 6f 75 72 63 65 73  itted, resources
2c8b5 20 6c 65 61 6b 73 20 77 69 6c 6c 20 6f 63 63 75   leaks will occu
2c8b6 72 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20  r.  .**.** If a 
2c8b7 64 69 73 63 6f 6e 6e 65 63 74 20 69 73 20 61 74  disconnect is at
2c8b8 74 65 6d 70 74 65 64 20 77 68 69 6c 65 20 61 20  tempted while a 
2c8b9 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 73  virtual table is
2c8ba 20 6c 6f 63 6b 65 64 2c 0a 2a 2a 20 74 68 65 20   locked,.** the 
2c8bb 64 69 73 63 6f 6e 6e 65 63 74 20 69 73 20 64 65  disconnect is de
2c8bc 66 65 72 72 65 64 20 75 6e 74 69 6c 20 61 6c 6c  ferred until all
2c8bd 20 6c 6f 63 6b 73 20 68 61 76 65 20 62 65 65 6e   locks have been
2c8be 20 72 65 6d 6f 76 65 64 2e 0a 2a 2f 0a 53 51 4c   removed..*/.SQL
2c8bf 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2c8c0 20 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b   sqlite3VtabLock
2c8c1 28 56 54 61 62 6c 65 20 2a 70 56 54 61 62 29 7b  (VTable *pVTab){
2c8c2 0a 20 20 70 56 54 61 62 2d 3e 6e 52 65 66 2b 2b  .  pVTab->nRef++
2c8c3 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 70 54 61 62  ;.}.../*.** pTab
2c8c4 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
2c8c5 20 61 20 54 61 62 6c 65 20 73 74 72 75 63 74 75   a Table structu
2c8c6 72 65 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20  re representing 
2c8c7 61 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 2e  a virtual-table.
2c8c8 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69  .** Return a poi
2c8c9 6e 74 65 72 20 74 6f 20 74 68 65 20 56 54 61 62  nter to the VTab
2c8ca 6c 65 20 6f 62 6a 65 63 74 20 75 73 65 64 20 62  le object used b
2c8cb 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20  y connection db 
2c8cc 74 6f 20 61 63 63 65 73 73 20 0a 2a 2a 20 74 68  to access .** th
2c8cd 69 73 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65  is virtual-table
2c8ce 2c 20 69 66 20 6f 6e 65 20 68 61 73 20 62 65 65  , if one has bee
2c8cf 6e 20 63 72 65 61 74 65 64 2c 20 6f 72 20 4e 55  n created, or NU
2c8d0 4c 4c 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f  LL otherwise..*/
2c8d1 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2c8d2 56 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 47  VTable *sqlite3G
2c8d3 65 74 56 54 61 62 6c 65 28 73 71 6c 69 74 65 33  etVTable(sqlite3
2c8d4 20 2a 64 62 2c 20 54 61 62 6c 65 20 2a 70 54 61   *db, Table *pTa
2c8d5 62 29 7b 0a 20 20 56 54 61 62 6c 65 20 2a 70 56  b){.  VTable *pV
2c8d6 74 61 62 3b 0a 20 20 61 73 73 65 72 74 28 20 49  tab;.  assert( I
2c8d7 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
2c8d8 3b 0a 20 20 66 6f 72 28 70 56 74 61 62 3d 70 54  ;.  for(pVtab=pT
2c8d9 61 62 2d 3e 70 56 54 61 62 6c 65 3b 20 70 56 74  ab->pVTable; pVt
2c8da 61 62 20 26 26 20 70 56 74 61 62 2d 3e 64 62 21  ab && pVtab->db!
2c8db 3d 64 62 3b 20 70 56 74 61 62 3d 70 56 74 61 62  =db; pVtab=pVtab
2c8dc 2d 3e 70 4e 65 78 74 29 3b 0a 20 20 72 65 74 75  ->pNext);.  retu
2c8dd 72 6e 20 70 56 74 61 62 3b 0a 7d 0a 0a 2f 2a 0a  rn pVtab;.}../*.
2c8de 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65  ** Decrement the
2c8df 20 72 65 66 2d 63 6f 75 6e 74 20 6f 6e 20 61 20   ref-count on a 
2c8e0 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6f 62  virtual table ob
2c8e1 6a 65 63 74 2e 20 57 68 65 6e 20 74 68 65 20 72  ject. When the r
2c8e2 65 66 2d 63 6f 75 6e 74 0a 2a 2a 20 72 65 61 63  ef-count.** reac
2c8e3 68 65 73 20 7a 65 72 6f 2c 20 63 61 6c 6c 20 74  hes zero, call t
2c8e4 68 65 20 78 44 69 73 63 6f 6e 6e 65 63 74 28 29  he xDisconnect()
2c8e5 20 6d 65 74 68 6f 64 20 74 6f 20 64 65 6c 65 74   method to delet
2c8e6 65 20 74 68 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f  e the object..*/
2c8e7 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2c8e8 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62  void sqlite3Vtab
2c8e9 55 6e 6c 6f 63 6b 28 56 54 61 62 6c 65 20 2a 70  Unlock(VTable *p
2c8ea 56 54 61 62 29 7b 0a 20 20 73 71 6c 69 74 65 33  VTab){.  sqlite3
2c8eb 20 2a 64 62 20 3d 20 70 56 54 61 62 2d 3e 64 62   *db = pVTab->db
2c8ec 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62 20  ;..  assert( db 
2c8ed 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 56 54  );.  assert( pVT
2c8ee 61 62 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20  ab->nRef>0 );.  
2c8ef 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 53  assert( sqlite3S
2c8f0 61 66 65 74 79 43 68 65 63 6b 4f 6b 28 64 62 29  afetyCheckOk(db)
2c8f1 20 29 3b 0a 0a 20 20 70 56 54 61 62 2d 3e 6e 52   );..  pVTab->nR
2c8f2 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 56 54 61  ef--;.  if( pVTa
2c8f3 62 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20  b->nRef==0 ){.  
2c8f4 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
2c8f5 70 20 3d 20 70 56 54 61 62 2d 3e 70 56 74 61 62  p = pVTab->pVtab
2c8f6 3b 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a 23  ;.    if( p ){.#
2c8f7 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
2c8f8 55 47 0a 20 20 20 20 20 20 69 66 28 20 70 56 54  UG.      if( pVT
2c8f9 61 62 2d 3e 64 62 2d 3e 6d 61 67 69 63 3d 3d 53  ab->db->magic==S
2c8fa 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59  QLITE_MAGIC_BUSY
2c8fb 20 29 7b 0a 20 20 20 20 20 20 20 20 28 76 6f 69   ){.        (voi
2c8fc 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  d)sqlite3SafetyO
2c8fd 66 66 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20  ff(db);.        
2c8fe 70 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 44 69 73  p->pModule->xDis
2c8ff 63 6f 6e 6e 65 63 74 28 70 29 3b 0a 20 20 20 20  connect(p);.    
2c900 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65      (void)sqlite
2c901 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20  3SafetyOn(db);. 
2c902 20 20 20 20 20 7d 20 65 6c 73 65 0a 23 65 6e 64       } else.#end
2c903 69 66 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20  if.      {.     
2c904 20 20 20 70 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78     p->pModule->x
2c905 44 69 73 63 6f 6e 6e 65 63 74 28 70 29 3b 0a 20  Disconnect(p);. 
2c906 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
2c907 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2c908 62 2c 20 70 56 54 61 62 29 3b 0a 20 20 7d 0a 7d  b, pVTab);.  }.}
2c909 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 6c 65 20 70 20  ../*.** Table p 
2c90a 69 73 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  is a virtual tab
2c90b 6c 65 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f  le. This functio
2c90c 6e 20 6d 6f 76 65 73 20 61 6c 6c 20 65 6c 65 6d  n moves all elem
2c90d 65 6e 74 73 20 69 6e 20 74 68 65 0a 2a 2a 20 70  ents in the.** p
2c90e 2d 3e 70 56 54 61 62 6c 65 20 6c 69 73 74 20 74  ->pVTable list t
2c90f 6f 20 74 68 65 20 73 71 6c 69 74 65 33 2e 70 44  o the sqlite3.pD
2c910 69 73 63 6f 6e 6e 65 63 74 20 6c 69 73 74 73 20  isconnect lists 
2c911 6f 66 20 74 68 65 69 72 20 61 73 73 6f 63 69 61  of their associa
2c912 74 65 64 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  ted.** database 
2c913 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 6f 20 62  connections to b
2c914 65 20 64 69 73 63 6f 6e 6e 65 63 74 65 64 20 61  e disconnected a
2c915 74 20 74 68 65 20 6e 65 78 74 20 6f 70 70 6f 72  t the next oppor
2c916 74 75 6e 69 74 79 2e 20 0a 2a 2a 20 45 78 63 65  tunity. .** Exce
2c917 70 74 2c 20 69 66 20 61 72 67 75 6d 65 6e 74 20  pt, if argument 
2c918 64 62 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20  db is not NULL, 
2c919 74 68 65 6e 20 74 68 65 20 65 6e 74 72 79 20 61  then the entry a
2c91a 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a 2a  ssociated with.*
2c91b 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20  * connection db 
2c91c 69 73 20 6c 65 66 74 20 69 6e 20 74 68 65 20 70  is left in the p
2c91d 2d 3e 70 56 54 61 62 6c 65 20 6c 69 73 74 2e 0a  ->pVTable list..
2c91e 2a 2f 0a 73 74 61 74 69 63 20 56 54 61 62 6c 65  */.static VTable
2c91f 20 2a 76 74 61 62 44 69 73 63 6f 6e 6e 65 63 74   *vtabDisconnect
2c920 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  All(sqlite3 *db,
2c921 20 54 61 62 6c 65 20 2a 70 29 7b 0a 20 20 56 54   Table *p){.  VT
2c922 61 62 6c 65 20 2a 70 52 65 74 20 3d 20 30 3b 0a  able *pRet = 0;.
2c923 20 20 56 54 61 62 6c 65 20 2a 70 56 54 61 62 6c    VTable *pVTabl
2c924 65 20 3d 20 70 2d 3e 70 56 54 61 62 6c 65 3b 0a  e = p->pVTable;.
2c925 20 20 70 2d 3e 70 56 54 61 62 6c 65 20 3d 20 30    p->pVTable = 0
2c926 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74  ;..  /* Assert t
2c927 68 61 74 20 74 68 65 20 6d 75 74 65 78 20 28 69  hat the mutex (i
2c928 66 20 61 6e 79 29 20 61 73 73 6f 63 69 61 74 65  f any) associate
2c929 64 20 77 69 74 68 20 74 68 65 20 42 74 53 68 61  d with the BtSha
2c92a 72 65 64 20 64 61 74 61 62 61 73 65 20 0a 20 20  red database .  
2c92b 2a 2a 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  ** that contains
2c92c 20 74 61 62 6c 65 20 70 20 69 73 20 68 65 6c 64   table p is held
2c92d 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20   by the caller. 
2c92e 53 65 65 20 68 65 61 64 65 72 20 63 6f 6d 6d 65  See header comme
2c92f 6e 74 73 20 0a 20 20 2a 2a 20 61 62 6f 76 65 20  nts .  ** above 
2c930 66 75 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65 33  function sqlite3
2c931 56 74 61 62 55 6e 6c 6f 63 6b 4c 69 73 74 28 29  VtabUnlockList()
2c932 20 66 6f 72 20 61 6e 20 65 78 70 6c 61 6e 61 74   for an explanat
2c933 69 6f 6e 20 6f 66 20 77 68 79 0a 20 20 2a 2a 20  ion of why.  ** 
2c934 74 68 69 73 20 6d 61 6b 65 73 20 69 74 20 73 61  this makes it sa
2c935 66 65 20 74 6f 20 61 63 63 65 73 73 20 74 68 65  fe to access the
2c936 20 73 71 6c 69 74 65 33 2e 70 44 69 73 63 6f 6e   sqlite3.pDiscon
2c937 6e 65 63 74 20 6c 69 73 74 20 6f 66 20 61 6e 79  nect list of any
2c938 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 63  .  ** database c
2c939 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 6d  onnection that m
2c93a 61 79 20 68 61 76 65 20 61 6e 20 65 6e 74 72 79  ay have an entry
2c93b 20 69 6e 20 74 68 65 20 70 2d 3e 70 56 54 61 62   in the p->pVTab
2c93c 6c 65 20 6c 69 73 74 2e 20 20 2a 2f 0a 20 20 61  le list.  */.  a
2c93d 73 73 65 72 74 28 20 64 62 3d 3d 30 20 7c 7c 0a  ssert( db==0 ||.
2c93e 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
2c93f 48 6f 6c 64 73 4d 75 74 65 78 28 64 62 2d 3e 61  HoldsMutex(db->a
2c940 44 62 5b 73 71 6c 69 74 65 33 53 63 68 65 6d 61  Db[sqlite3Schema
2c941 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 2d 3e 70  ToIndex(db, p->p
2c942 53 63 68 65 6d 61 29 5d 2e 70 42 74 29 20 0a 20  Schema)].pBt) . 
2c943 20 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 70 56   );..  while( pV
2c944 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 73 71 6c  Table ){.    sql
2c945 69 74 65 33 20 2a 64 62 32 20 3d 20 70 56 54 61  ite3 *db2 = pVTa
2c946 62 6c 65 2d 3e 64 62 3b 0a 20 20 20 20 56 54 61  ble->db;.    VTa
2c947 62 6c 65 20 2a 70 4e 65 78 74 20 3d 20 70 56 54  ble *pNext = pVT
2c948 61 62 6c 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  able->pNext;.   
2c949 20 61 73 73 65 72 74 28 20 64 62 32 20 29 3b 0a   assert( db2 );.
2c94a 20 20 20 20 69 66 28 20 64 62 32 3d 3d 64 62 20      if( db2==db 
2c94b 29 7b 0a 20 20 20 20 20 20 70 52 65 74 20 3d 20  ){.      pRet = 
2c94c 70 56 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 70  pVTable;.      p
2c94d 2d 3e 70 56 54 61 62 6c 65 20 3d 20 70 52 65 74  ->pVTable = pRet
2c94e 3b 0a 20 20 20 20 20 20 70 52 65 74 2d 3e 70 4e  ;.      pRet->pN
2c94f 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c  ext = 0;.    }el
2c950 73 65 7b 0a 20 20 20 20 20 20 70 56 54 61 62 6c  se{.      pVTabl
2c951 65 2d 3e 70 4e 65 78 74 20 3d 20 64 62 32 2d 3e  e->pNext = db2->
2c952 70 44 69 73 63 6f 6e 6e 65 63 74 3b 0a 20 20 20  pDisconnect;.   
2c953 20 20 20 64 62 32 2d 3e 70 44 69 73 63 6f 6e 6e     db2->pDisconn
2c954 65 63 74 20 3d 20 70 56 54 61 62 6c 65 3b 0a 20  ect = pVTable;. 
2c955 20 20 20 7d 0a 20 20 20 20 70 56 54 61 62 6c 65     }.    pVTable
2c956 20 3d 20 70 4e 65 78 74 3b 0a 20 20 7d 0a 0a 20   = pNext;.  }.. 
2c957 20 61 73 73 65 72 74 28 20 21 64 62 20 7c 7c 20   assert( !db || 
2c958 70 52 65 74 20 29 3b 0a 20 20 72 65 74 75 72 6e  pRet );.  return
2c959 20 70 52 65 74 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a   pRet;.}.../*.**
2c95a 20 44 69 73 63 6f 6e 6e 65 63 74 20 61 6c 6c 20   Disconnect all 
2c95b 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  the virtual tabl
2c95c 65 20 6f 62 6a 65 63 74 73 20 69 6e 20 74 68 65  e objects in the
2c95d 20 73 71 6c 69 74 65 33 2e 70 44 69 73 63 6f 6e   sqlite3.pDiscon
2c95e 6e 65 63 74 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a  nect list..**.**
2c95f 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d   This function m
2c960 61 79 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65  ay only be calle
2c961 64 20 77 68 65 6e 20 74 68 65 20 6d 75 74 65 78  d when the mutex
2c962 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  es associated wi
2c963 74 68 20 61 6c 6c 0a 2a 2a 20 73 68 61 72 65 64  th all.** shared
2c964 20 62 2d 74 72 65 65 20 64 61 74 61 62 61 73 65   b-tree database
2c965 73 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20 63  s opened using c
2c966 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20 61 72 65  onnection db are
2c967 20 68 65 6c 64 20 62 79 20 74 68 65 20 0a 2a 2a   held by the .**
2c968 20 63 61 6c 6c 65 72 2e 20 54 68 69 73 20 69 73   caller. This is
2c969 20 64 6f 6e 65 20 74 6f 20 70 72 6f 74 65 63 74   done to protect
2c96a 20 74 68 65 20 73 71 6c 69 74 65 33 2e 70 44 69   the sqlite3.pDi
2c96b 73 63 6f 6e 6e 65 63 74 20 6c 69 73 74 2e 20 54  sconnect list. T
2c96c 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 2e 70 44  he.** sqlite3.pD
2c96d 69 73 63 6f 6e 6e 65 63 74 20 6c 69 73 74 20 69  isconnect list i
2c96e 73 20 61 63 63 65 73 73 65 64 20 6f 6e 6c 79 20  s accessed only 
2c96f 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
2c970 2a 20 20 20 31 29 20 42 79 20 74 68 69 73 20 66  *   1) By this f
2c971 75 6e 63 74 69 6f 6e 2e 20 49 6e 20 74 68 69 73  unction. In this
2c972 20 63 61 73 65 2c 20 61 6c 6c 20 42 74 53 68 61   case, all BtSha
2c973 72 65 64 20 6d 75 74 65 78 65 73 20 61 6e 64 20  red mutexes and 
2c974 74 68 65 20 6d 75 74 65 78 0a 2a 2a 20 20 20 20  the mutex.**    
2c975 20 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74    associated wit
2c976 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68  h the database h
2c977 61 6e 64 6c 65 20 69 74 73 65 6c 66 20 6d 75 73  andle itself mus
2c978 74 20 62 65 20 68 65 6c 64 2e 0a 2a 2a 0a 2a 2a  t be held..**.**
2c979 20 20 20 32 29 20 42 79 20 66 75 6e 63 74 69 6f     2) By functio
2c97a 6e 20 76 74 61 62 44 69 73 63 6f 6e 6e 65 63 74  n vtabDisconnect
2c97b 41 6c 6c 28 29 2c 20 77 68 65 6e 20 69 74 20 61  All(), when it a
2c97c 64 64 73 20 61 20 56 54 61 62 6c 65 20 65 6e 74  dds a VTable ent
2c97d 72 79 20 74 6f 0a 2a 2a 20 20 20 20 20 20 74 68  ry to.**      th
2c97e 65 20 73 71 6c 69 74 65 33 2e 70 44 69 73 63 6f  e sqlite3.pDisco
2c97f 6e 6e 65 63 74 20 6c 69 73 74 2e 20 49 6e 20 74  nnect list. In t
2c980 68 69 73 20 63 61 73 65 20 65 69 74 68 65 72 20  his case either 
2c981 74 68 65 20 42 74 53 68 61 72 65 64 20 6d 75 74  the BtShared mut
2c982 65 78 0a 2a 2a 20 20 20 20 20 20 61 73 73 6f 63  ex.**      assoc
2c983 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 64  iated with the d
2c984 61 74 61 62 61 73 65 20 74 68 65 20 76 69 72 74  atabase the virt
2c985 75 61 6c 20 74 61 62 6c 65 20 69 73 20 73 74 6f  ual table is sto
2c986 72 65 64 20 69 6e 20 69 73 20 68 65 6c 64 0a 2a  red in is held.*
2c987 2a 20 20 20 20 20 20 6f 72 2c 20 69 66 20 74 68  *      or, if th
2c988 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
2c989 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 20 6e  is stored in a n
2c98a 6f 6e 2d 73 68 61 72 61 62 6c 65 20 64 61 74 61  on-sharable data
2c98b 62 61 73 65 2c 20 74 68 65 6e 0a 2a 2a 20 20 20  base, then.**   
2c98c 20 20 20 74 68 65 20 64 61 74 61 62 61 73 65 20     the database 
2c98d 68 61 6e 64 6c 65 20 6d 75 74 65 78 20 69 73 20  handle mutex is 
2c98e 68 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 61  held..**.** As a
2c98f 20 72 65 73 75 6c 74 2c 20 61 20 73 71 6c 69 74   result, a sqlit
2c990 65 33 2e 70 44 69 73 63 6f 6e 6e 65 63 74 20 63  e3.pDisconnect c
2c991 61 6e 6e 6f 74 20 62 65 20 61 63 63 65 73 73 65  annot be accesse
2c992 64 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79  d simultaneously
2c993 20 0a 2a 2a 20 62 79 20 6d 75 6c 74 69 70 6c 65   .** by multiple
2c994 20 74 68 72 65 61 64 73 2e 20 49 74 20 69 73 20   threads. It is 
2c995 74 68 72 65 61 64 2d 73 61 66 65 2e 0a 2a 2f 0a  thread-safe..*/.
2c996 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2c997 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 55  oid sqlite3VtabU
2c998 6e 6c 6f 63 6b 4c 69 73 74 28 73 71 6c 69 74 65  nlockList(sqlite
2c999 33 20 2a 64 62 29 7b 0a 20 20 56 54 61 62 6c 65  3 *db){.  VTable
2c99a 20 2a 70 20 3d 20 64 62 2d 3e 70 44 69 73 63 6f   *p = db->pDisco
2c99b 6e 6e 65 63 74 3b 0a 20 20 64 62 2d 3e 70 44 69  nnect;.  db->pDi
2c99c 73 63 6f 6e 6e 65 63 74 20 3d 20 30 3b 0a 0a 20  sconnect = 0;.. 
2c99d 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
2c99e 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74  BtreeHoldsAllMut
2c99f 65 78 65 73 28 64 62 29 20 29 3b 0a 20 20 61 73  exes(db) );.  as
2c9a0 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
2c9a1 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74  tex_held(db->mut
2c9a2 65 78 29 20 29 3b 0a 0a 20 20 69 66 28 20 70 20  ex) );..  if( p 
2c9a3 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  ){.    sqlite3Ex
2c9a4 70 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74  pirePreparedStat
2c9a5 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 20 20  ements(db);.    
2c9a6 64 6f 20 7b 0a 20 20 20 20 20 20 56 54 61 62 6c  do {.      VTabl
2c9a7 65 20 2a 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4e  e *pNext = p->pN
2c9a8 65 78 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ext;.      sqlit
2c9a9 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 70 29 3b  e3VtabUnlock(p);
2c9aa 0a 20 20 20 20 20 20 70 20 3d 20 70 4e 65 78 74  .      p = pNext
2c9ab 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 70 20  ;.    }while( p 
2c9ac 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
2c9ad 43 6c 65 61 72 20 61 6e 79 20 61 6e 64 20 61 6c  Clear any and al
2c9ae 6c 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20  l virtual-table 
2c9af 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d  information from
2c9b0 20 74 68 65 20 54 61 62 6c 65 20 72 65 63 6f 72   the Table recor
2c9b1 64 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  d..** This routi
2c9b2 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 66 6f  ne is called, fo
2c9b3 72 20 65 78 61 6d 70 6c 65 2c 20 6a 75 73 74 20  r example, just 
2c9b4 62 65 66 6f 72 65 20 64 65 6c 65 74 69 6e 67 20  before deleting 
2c9b5 74 68 65 20 54 61 62 6c 65 0a 2a 2a 20 72 65 63  the Table.** rec
2c9b6 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65  ord..**.** Since
2c9b7 20 69 74 20 69 73 20 61 20 76 69 72 74 75 61 6c   it is a virtual
2c9b8 2d 74 61 62 6c 65 2c 20 74 68 65 20 54 61 62 6c  -table, the Tabl
2c9b9 65 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74  e structure cont
2c9ba 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a  ains a pointer.*
2c9bb 2a 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66  * to the head of
2c9bc 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f   a linked list o
2c9bd 66 20 56 54 61 62 6c 65 20 73 74 72 75 63 74 75  f VTable structu
2c9be 72 65 73 2e 20 45 61 63 68 20 56 54 61 62 6c 65  res. Each VTable
2c9bf 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 69   .** structure i
2c9c0 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
2c9c1 68 20 61 20 73 69 6e 67 6c 65 20 73 71 6c 69 74  h a single sqlit
2c9c2 65 33 2a 20 75 73 65 72 20 6f 66 20 74 68 65 20  e3* user of the 
2c9c3 73 63 68 65 6d 61 2e 0a 2a 2a 20 54 68 65 20 72  schema..** The r
2c9c4 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 6f  eference count o
2c9c5 66 20 74 68 65 20 56 54 61 62 6c 65 20 73 74 72  f the VTable str
2c9c6 75 63 74 75 72 65 20 61 73 73 6f 63 69 61 74 65  ucture associate
2c9c7 64 20 77 69 74 68 20 64 61 74 61 62 61 73 65 20  d with database 
2c9c8 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64  .** connection d
2c9c9 62 20 69 73 20 64 65 63 72 65 6d 65 6e 74 65 64  b is decremented
2c9ca 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 28 77 68   immediately (wh
2c9cb 69 63 68 20 6d 61 79 20 6c 65 61 64 20 74 6f 20  ich may lead to 
2c9cc 74 68 65 20 0a 2a 2a 20 73 74 72 75 63 74 75 72  the .** structur
2c9cd 65 20 62 65 69 6e 67 20 78 44 69 73 63 6f 6e 6e  e being xDisconn
2c9ce 65 63 74 65 64 20 61 6e 64 20 66 72 65 65 29 2e  ected and free).
2c9cf 20 41 6e 79 20 6f 74 68 65 72 20 56 54 61 62 6c   Any other VTabl
2c9d0 65 20 73 74 72 75 63 74 75 72 65 73 0a 2a 2a 20  e structures.** 
2c9d1 69 6e 20 74 68 65 20 6c 69 73 74 20 61 72 65 20  in the list are 
2c9d2 6d 6f 76 65 64 20 74 6f 20 74 68 65 20 73 71 6c  moved to the sql
2c9d3 69 74 65 33 2e 70 44 69 73 63 6f 6e 6e 65 63 74  ite3.pDisconnect
2c9d4 20 6c 69 73 74 20 6f 66 20 74 68 65 20 61 73 73   list of the ass
2c9d5 6f 63 69 61 74 65 64 20 0a 2a 2a 20 64 61 74 61  ociated .** data
2c9d6 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  base connection.
2c9d7 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2c9d8 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
2c9d9 74 61 62 43 6c 65 61 72 28 54 61 62 6c 65 20 2a  tabClear(Table *
2c9da 70 29 7b 0a 20 20 76 74 61 62 44 69 73 63 6f 6e  p){.  vtabDiscon
2c9db 6e 65 63 74 41 6c 6c 28 30 2c 20 70 29 3b 0a 20  nectAll(0, p);. 
2c9dc 20 69 66 28 20 70 2d 3e 61 7a 4d 6f 64 75 6c 65   if( p->azModule
2c9dd 41 72 67 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  Arg ){.    int i
2c9de 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
2c9df 3c 70 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 3b 20  <p->nModuleArg; 
2c9e0 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  i++){.      sqli
2c9e1 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62 4d  te3DbFree(p->dbM
2c9e2 65 6d 2c 20 70 2d 3e 61 7a 4d 6f 64 75 6c 65 41  em, p->azModuleA
2c9e3 72 67 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20  rg[i]);.    }.  
2c9e4 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2c9e5 70 2d 3e 64 62 4d 65 6d 2c 20 70 2d 3e 61 7a 4d  p->dbMem, p->azM
2c9e6 6f 64 75 6c 65 41 72 67 29 3b 0a 20 20 7d 0a 7d  oduleArg);.  }.}
2c9e7 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65  ../*.** Add a ne
2c9e8 77 20 6d 6f 64 75 6c 65 20 61 72 67 75 6d 65 6e  w module argumen
2c9e9 74 20 74 6f 20 70 54 61 62 6c 65 2d 3e 61 7a 4d  t to pTable->azM
2c9ea 6f 64 75 6c 65 41 72 67 5b 5d 2e 0a 2a 2a 20 54  oduleArg[]..** T
2c9eb 68 65 20 73 74 72 69 6e 67 20 69 73 20 6e 6f 74  he string is not
2c9ec 20 63 6f 70 69 65 64 20 2d 20 74 68 65 20 70 6f   copied - the po
2c9ed 69 6e 74 65 72 20 69 73 20 73 74 6f 72 65 64 2e  inter is stored.
2c9ee 20 20 54 68 65 0a 2a 2a 20 73 74 72 69 6e 67 20    The.** string 
2c9ef 77 69 6c 6c 20 62 65 20 66 72 65 65 64 20 61 75  will be freed au
2c9f0 74 6f 6d 61 74 69 63 61 6c 6c 79 20 77 68 65 6e  tomatically when
2c9f1 20 74 68 65 20 74 61 62 6c 65 20 69 73 0a 2a 2a   the table is.**
2c9f2 20 64 65 6c 65 74 65 64 2e 0a 2a 2f 0a 73 74 61   deleted..*/.sta
2c9f3 74 69 63 20 76 6f 69 64 20 61 64 64 4d 6f 64 75  tic void addModu
2c9f4 6c 65 41 72 67 75 6d 65 6e 74 28 73 71 6c 69 74  leArgument(sqlit
2c9f5 65 33 20 2a 64 62 2c 20 54 61 62 6c 65 20 2a 70  e3 *db, Table *p
2c9f6 54 61 62 6c 65 2c 20 63 68 61 72 20 2a 7a 41 72  Table, char *zAr
2c9f7 67 29 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 54  g){.  int i = pT
2c9f8 61 62 6c 65 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67  able->nModuleArg
2c9f9 2b 2b 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 73  ++;.  int nBytes
2c9fa 20 3d 20 73 69 7a 65 6f 66 28 63 68 61 72 20 2a   = sizeof(char *
2c9fb 29 2a 28 31 2b 70 54 61 62 6c 65 2d 3e 6e 4d 6f  )*(1+pTable->nMo
2c9fc 64 75 6c 65 41 72 67 29 3b 0a 20 20 63 68 61 72  duleArg);.  char
2c9fd 20 2a 2a 61 7a 4d 6f 64 75 6c 65 41 72 67 3b 0a   **azModuleArg;.
2c9fe 20 20 61 7a 4d 6f 64 75 6c 65 41 72 67 20 3d 20    azModuleArg = 
2c9ff 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63  sqlite3DbRealloc
2ca00 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e 61 7a 4d  (db, pTable->azM
2ca01 6f 64 75 6c 65 41 72 67 2c 20 6e 42 79 74 65 73  oduleArg, nBytes
2ca02 29 3b 0a 20 20 69 66 28 20 61 7a 4d 6f 64 75 6c  );.  if( azModul
2ca03 65 41 72 67 3d 3d 30 20 29 7b 0a 20 20 20 20 69  eArg==0 ){.    i
2ca04 6e 74 20 6a 3b 0a 20 20 20 20 66 6f 72 28 6a 3d  nt j;.    for(j=
2ca05 30 3b 20 6a 3c 69 3b 20 6a 2b 2b 29 7b 0a 20 20  0; j<i; j++){.  
2ca06 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2ca07 65 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e 61 7a  e(db, pTable->az
2ca08 4d 6f 64 75 6c 65 41 72 67 5b 6a 5d 29 3b 0a 20  ModuleArg[j]);. 
2ca09 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
2ca0a 44 62 46 72 65 65 28 64 62 2c 20 7a 41 72 67 29  DbFree(db, zArg)
2ca0b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  ;.    sqlite3DbF
2ca0c 72 65 65 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e  ree(db, pTable->
2ca0d 61 7a 4d 6f 64 75 6c 65 41 72 67 29 3b 0a 20 20  azModuleArg);.  
2ca0e 20 20 70 54 61 62 6c 65 2d 3e 6e 4d 6f 64 75 6c    pTable->nModul
2ca0f 65 41 72 67 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  eArg = 0;.  }els
2ca10 65 7b 0a 20 20 20 20 61 7a 4d 6f 64 75 6c 65 41  e{.    azModuleA
2ca11 72 67 5b 69 5d 20 3d 20 7a 41 72 67 3b 0a 20 20  rg[i] = zArg;.  
2ca12 20 20 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 69 2b    azModuleArg[i+
2ca13 31 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 54  1] = 0;.  }.  pT
2ca14 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72  able->azModuleAr
2ca15 67 20 3d 20 61 7a 4d 6f 64 75 6c 65 41 72 67 3b  g = azModuleArg;
2ca16 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61  .}../*.** The pa
2ca17 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20  rser calls this 
2ca18 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 69 74 20  routine when it 
2ca19 66 69 72 73 74 20 73 65 65 73 20 61 20 43 52 45  first sees a CRE
2ca1a 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c  ATE VIRTUAL TABL
2ca1b 45 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20  E.** statement. 
2ca1c 20 54 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65   The module name
2ca1d 20 68 61 73 20 62 65 65 6e 20 70 61 72 73 65 64   has been parsed
2ca1e 2c 20 62 75 74 20 74 68 65 20 6f 70 74 69 6f 6e  , but the option
2ca1f 61 6c 20 6c 69 73 74 0a 2a 2a 20 6f 66 20 70 61  al list.** of pa
2ca20 72 61 6d 65 74 65 72 73 20 74 68 61 74 20 66 6f  rameters that fo
2ca21 6c 6c 6f 77 20 74 68 65 20 6d 6f 64 75 6c 65 20  llow the module 
2ca22 6e 61 6d 65 20 61 72 65 20 73 74 69 6c 6c 20 70  name are still p
2ca23 65 6e 64 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54  ending..*/.SQLIT
2ca24 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2ca25 71 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e 50  qlite3VtabBeginP
2ca26 61 72 73 65 28 0a 20 20 50 61 72 73 65 20 2a 70  arse(.  Parse *p
2ca27 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a  Parse,        /*
2ca28 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
2ca29 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61   */.  Token *pNa
2ca2a 6d 65 31 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e  me1,        /* N
2ca2b 61 6d 65 20 6f 66 20 6e 65 77 20 74 61 62 6c 65  ame of new table
2ca2c 2c 20 6f 72 20 64 61 74 61 62 61 73 65 20 6e 61  , or database na
2ca2d 6d 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  me */.  Token *p
2ca2e 4e 61 6d 65 32 2c 20 20 20 20 20 20 20 20 2f 2a  Name2,        /*
2ca2f 20 4e 61 6d 65 20 6f 66 20 6e 65 77 20 74 61 62   Name of new tab
2ca30 6c 65 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20  le or NULL */.  
2ca31 54 6f 6b 65 6e 20 2a 70 4d 6f 64 75 6c 65 4e 61  Token *pModuleNa
2ca32 6d 65 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  me    /* Name of
2ca33 20 74 68 65 20 6d 6f 64 75 6c 65 20 66 6f 72 20   the module for 
2ca34 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  the virtual tabl
2ca35 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 44  e */.){.  int iD
2ca36 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
2ca37 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
2ca38 74 68 65 20 74 61 62 6c 65 20 69 73 20 62 65 69  the table is bei
2ca39 6e 67 20 63 72 65 61 74 65 64 20 69 6e 20 2a 2f  ng created in */
2ca3a 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65  .  Table *pTable
2ca3b 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ;        /* The 
2ca3c 6e 65 77 20 76 69 72 74 75 61 6c 20 74 61 62 6c  new virtual tabl
2ca3d 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  e */.  sqlite3 *
2ca3e 64 62 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  db;          /* 
2ca3f 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  Database connect
2ca40 69 6f 6e 20 2a 2f 0a 0a 20 20 73 71 6c 69 74 65  ion */..  sqlite
2ca41 33 53 74 61 72 74 54 61 62 6c 65 28 70 50 61 72  3StartTable(pPar
2ca42 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d  se, pName1, pNam
2ca43 65 32 2c 20 30 2c 20 30 2c 20 31 2c 20 30 29 3b  e2, 0, 0, 1, 0);
2ca44 0a 20 20 70 54 61 62 6c 65 20 3d 20 70 50 61 72  .  pTable = pPar
2ca45 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20  se->pNewTable;. 
2ca46 20 69 66 28 20 70 54 61 62 6c 65 3d 3d 30 20 29   if( pTable==0 )
2ca47 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72   return;.  asser
2ca48 74 28 20 30 3d 3d 70 54 61 62 6c 65 2d 3e 70 49  t( 0==pTable->pI
2ca49 6e 64 65 78 20 29 3b 0a 0a 20 20 64 62 20 3d 20  ndex );..  db = 
2ca4a 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 44  pParse->db;.  iD
2ca4b 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
2ca4c 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61  aToIndex(db, pTa
2ca4d 62 6c 65 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  ble->pSchema);. 
2ca4e 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20   assert( iDb>=0 
2ca4f 29 3b 0a 0a 20 20 70 54 61 62 6c 65 2d 3e 74 61  );..  pTable->ta
2ca50 62 46 6c 61 67 73 20 7c 3d 20 54 46 5f 56 69 72  bFlags |= TF_Vir
2ca51 74 75 61 6c 3b 0a 20 20 70 54 61 62 6c 65 2d 3e  tual;.  pTable->
2ca52 6e 4d 6f 64 75 6c 65 41 72 67 20 3d 20 30 3b 0a  nModuleArg = 0;.
2ca53 20 20 61 64 64 4d 6f 64 75 6c 65 41 72 67 75 6d    addModuleArgum
2ca54 65 6e 74 28 64 62 2c 20 70 54 61 62 6c 65 2c 20  ent(db, pTable, 
2ca55 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54  sqlite3NameFromT
2ca56 6f 6b 65 6e 28 64 62 2c 20 70 4d 6f 64 75 6c 65  oken(db, pModule
2ca57 4e 61 6d 65 29 29 3b 0a 20 20 61 64 64 4d 6f 64  Name));.  addMod
2ca58 75 6c 65 41 72 67 75 6d 65 6e 74 28 64 62 2c 20  uleArgument(db, 
2ca59 70 54 61 62 6c 65 2c 20 73 71 6c 69 74 65 33 44  pTable, sqlite3D
2ca5a 62 53 74 72 44 75 70 28 64 62 2c 20 64 62 2d 3e  bStrDup(db, db->
2ca5b 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 29  aDb[iDb].zName))
2ca5c 3b 0a 20 20 61 64 64 4d 6f 64 75 6c 65 41 72 67  ;.  addModuleArg
2ca5d 75 6d 65 6e 74 28 64 62 2c 20 70 54 61 62 6c 65  ument(db, pTable
2ca5e 2c 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  , sqlite3DbStrDu
2ca5f 70 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e 7a 4e  p(db, pTable->zN
2ca60 61 6d 65 29 29 3b 0a 20 20 70 50 61 72 73 65 2d  ame));.  pParse-
2ca61 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 6e 20 3d 20  >sNameToken.n = 
2ca62 28 69 6e 74 29 28 26 70 4d 6f 64 75 6c 65 4e 61  (int)(&pModuleNa
2ca63 6d 65 2d 3e 7a 5b 70 4d 6f 64 75 6c 65 4e 61 6d  me->z[pModuleNam
2ca64 65 2d 3e 6e 5d 20 2d 20 70 4e 61 6d 65 31 2d 3e  e->n] - pName1->
2ca65 7a 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  z);..#ifndef SQL
2ca66 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49  ITE_OMIT_AUTHORI
2ca67 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 43 72 65 61  ZATION.  /* Crea
2ca68 74 69 6e 67 20 61 20 76 69 72 74 75 61 6c 20 74  ting a virtual t
2ca69 61 62 6c 65 20 69 6e 76 6f 6b 65 73 20 74 68 65  able invokes the
2ca6a 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63   authorization c
2ca6b 61 6c 6c 62 61 63 6b 20 74 77 69 63 65 2e 0a 20  allback twice.. 
2ca6c 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20 69 6e   ** The first in
2ca6d 76 6f 63 61 74 69 6f 6e 2c 20 74 6f 20 6f 62 74  vocation, to obt
2ca6e 61 69 6e 20 70 65 72 6d 69 73 73 69 6f 6e 20 74  ain permission t
2ca6f 6f 20 49 4e 53 45 52 54 20 61 20 72 6f 77 20 69  o INSERT a row i
2ca70 6e 74 6f 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c  nto the.  ** sql
2ca71 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65  ite_master table
2ca72 2c 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  , has already be
2ca73 65 6e 20 6d 61 64 65 20 62 79 20 73 71 6c 69 74  en made by sqlit
2ca74 65 33 53 74 61 72 74 54 61 62 6c 65 28 29 2e 0a  e3StartTable()..
2ca75 20 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20    ** The second 
2ca76 63 61 6c 6c 2c 20 74 6f 20 6f 62 74 61 69 6e 20  call, to obtain 
2ca77 70 65 72 6d 69 73 73 69 6f 6e 20 74 6f 20 63 72  permission to cr
2ca78 65 61 74 65 20 74 68 65 20 74 61 62 6c 65 2c 20  eate the table, 
2ca79 69 73 20 6d 61 64 65 20 6e 6f 77 2e 0a 20 20 2a  is made now..  *
2ca7a 2f 0a 20 20 69 66 28 20 70 54 61 62 6c 65 2d 3e  /.  if( pTable->
2ca7b 61 7a 4d 6f 64 75 6c 65 41 72 67 20 29 7b 0a 20  azModuleArg ){. 
2ca7c 20 20 20 73 71 6c 69 74 65 33 41 75 74 68 43 68     sqlite3AuthCh
2ca7d 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49  eck(pParse, SQLI
2ca7e 54 45 5f 43 52 45 41 54 45 5f 56 54 41 42 4c 45  TE_CREATE_VTABLE
2ca7f 2c 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 2c  , pTable->zName,
2ca80 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 54   .            pT
2ca81 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72  able->azModuleAr
2ca82 67 5b 30 5d 2c 20 70 50 61 72 73 65 2d 3e 64 62  g[0], pParse->db
2ca83 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
2ca84 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a  );.  }.#endif.}.
2ca85 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
2ca86 69 6e 65 20 74 61 6b 65 73 20 74 68 65 20 6d 6f  ine takes the mo
2ca87 64 75 6c 65 20 61 72 67 75 6d 65 6e 74 20 74 68  dule argument th
2ca88 61 74 20 68 61 73 20 62 65 65 6e 20 61 63 63 75  at has been accu
2ca89 6d 75 6c 61 74 69 6e 67 0a 2a 2a 20 69 6e 20 70  mulating.** in p
2ca8a 50 61 72 73 65 2d 3e 7a 41 72 67 5b 5d 20 61 6e  Parse->zArg[] an
2ca8b 64 20 61 70 70 65 6e 64 73 20 69 74 20 74 6f 20  d appends it to 
2ca8c 74 68 65 20 6c 69 73 74 20 6f 66 20 61 72 67 75  the list of argu
2ca8d 6d 65 6e 74 73 20 6f 6e 20 74 68 65 0a 2a 2a 20  ments on the.** 
2ca8e 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63 75  virtual table cu
2ca8f 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f  rrently under co
2ca90 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 20 70 50  nstruction in pP
2ca91 61 72 73 65 2d 3e 70 54 61 62 6c 65 2e 0a 2a 2f  arse->pTable..*/
2ca92 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 64 64  .static void add
2ca93 41 72 67 75 6d 65 6e 74 54 6f 56 74 61 62 28 50  ArgumentToVtab(P
2ca94 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20  arse *pParse){. 
2ca95 20 69 66 28 20 70 50 61 72 73 65 2d 3e 73 41 72   if( pParse->sAr
2ca96 67 2e 7a 20 26 26 20 41 4c 57 41 59 53 28 70 50  g.z && ALWAYS(pP
2ca97 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29  arse->pNewTable)
2ca98 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68   ){.    const ch
2ca99 61 72 20 2a 7a 20 3d 20 28 63 6f 6e 73 74 20 63  ar *z = (const c
2ca9a 68 61 72 2a 29 70 50 61 72 73 65 2d 3e 73 41 72  har*)pParse->sAr
2ca9b 67 2e 7a 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d  g.z;.    int n =
2ca9c 20 70 50 61 72 73 65 2d 3e 73 41 72 67 2e 6e 3b   pParse->sArg.n;
2ca9d 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62  .    sqlite3 *db
2ca9e 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
2ca9f 20 20 20 61 64 64 4d 6f 64 75 6c 65 41 72 67 75     addModuleArgu
2caa0 6d 65 6e 74 28 64 62 2c 20 70 50 61 72 73 65 2d  ment(db, pParse-
2caa1 3e 70 4e 65 77 54 61 62 6c 65 2c 20 73 71 6c 69  >pNewTable, sqli
2caa2 74 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c  te3DbStrNDup(db,
2caa3 20 7a 2c 20 6e 29 29 3b 0a 20 20 7d 0a 7d 0a 0a   z, n));.  }.}..
2caa4 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73 65 72  /*.** The parser
2caa5 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74   calls this rout
2caa6 69 6e 65 20 61 66 74 65 72 20 74 68 65 20 43 52  ine after the CR
2caa7 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42  EATE VIRTUAL TAB
2caa8 4c 45 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20  LE statement.** 
2caa9 68 61 73 20 62 65 65 6e 20 63 6f 6d 70 6c 65 74  has been complet
2caaa 65 6c 79 20 70 61 72 73 65 64 2e 0a 2a 2f 0a 53  ely parsed..*/.S
2caab 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2caac 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 46 69  id sqlite3VtabFi
2caad 6e 69 73 68 50 61 72 73 65 28 50 61 72 73 65 20  nishParse(Parse 
2caae 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a  *pParse, Token *
2caaf 70 45 6e 64 29 7b 0a 20 20 54 61 62 6c 65 20 2a  pEnd){.  Table *
2cab0 70 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70  pTab = pParse->p
2cab1 4e 65 77 54 61 62 6c 65 3b 20 20 2f 2a 20 54 68  NewTable;  /* Th
2cab2 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 63 6f  e table being co
2cab3 6e 73 74 72 75 63 74 65 64 20 2a 2f 0a 20 20 73  nstructed */.  s
2cab4 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
2cab5 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 20  rse->db;        
2cab6 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
2cab7 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a   connection */..
2cab8 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29 20    if( pTab==0 ) 
2cab9 72 65 74 75 72 6e 3b 0a 20 20 61 64 64 41 72 67  return;.  addArg
2caba 75 6d 65 6e 74 54 6f 56 74 61 62 28 70 50 61 72  umentToVtab(pPar
2cabb 73 65 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 73  se);.  pParse->s
2cabc 41 72 67 2e 7a 20 3d 20 30 3b 0a 20 20 69 66 28  Arg.z = 0;.  if(
2cabd 20 70 54 61 62 2d 3e 6e 4d 6f 64 75 6c 65 41 72   pTab->nModuleAr
2cabe 67 3c 31 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  g<1 ) return;.  
2cabf 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 43 52 45  .  /* If the CRE
2cac0 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c  ATE VIRTUAL TABL
2cac1 45 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 62  E statement is b
2cac2 65 69 6e 67 20 65 6e 74 65 72 65 64 20 66 6f 72  eing entered for
2cac3 20 74 68 65 0a 20 20 2a 2a 20 66 69 72 73 74 20   the.  ** first 
2cac4 74 69 6d 65 20 28 69 6e 20 6f 74 68 65 72 20 77  time (in other w
2cac5 6f 72 64 73 20 69 66 20 74 68 65 20 76 69 72 74  ords if the virt
2cac6 75 61 6c 20 74 61 62 6c 65 20 69 73 20 61 63 74  ual table is act
2cac7 75 61 6c 6c 79 20 62 65 69 6e 67 0a 20 20 2a 2a  ually being.  **
2cac8 20 63 72 65 61 74 65 64 20 6e 6f 77 20 69 6e 73   created now ins
2cac9 74 65 61 64 20 6f 66 20 6a 75 73 74 20 62 65 69  tead of just bei
2caca 6e 67 20 72 65 61 64 20 6f 75 74 20 6f 66 20 73  ng read out of s
2cacb 71 6c 69 74 65 5f 6d 61 73 74 65 72 29 20 74 68  qlite_master) th
2cacc 65 6e 0a 20 20 2a 2a 20 64 6f 20 61 64 64 69 74  en.  ** do addit
2cacd 69 6f 6e 61 6c 20 69 6e 69 74 69 61 6c 69 7a 61  ional initializa
2cace 74 69 6f 6e 20 77 6f 72 6b 20 61 6e 64 20 73 74  tion work and st
2cacf 6f 72 65 20 74 68 65 20 73 74 61 74 65 6d 65 6e  ore the statemen
2cad0 74 20 74 65 78 74 0a 20 20 2a 2a 20 69 6e 20 74  t text.  ** in t
2cad1 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  he sqlite_master
2cad2 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69   table..  */.  i
2cad3 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73  f( !db->init.bus
2cad4 79 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a  y ){.    char *z
2cad5 53 74 6d 74 3b 0a 20 20 20 20 63 68 61 72 20 2a  Stmt;.    char *
2cad6 7a 57 68 65 72 65 3b 0a 20 20 20 20 69 6e 74 20  zWhere;.    int 
2cad7 69 44 62 3b 0a 20 20 20 20 56 64 62 65 20 2a 76  iDb;.    Vdbe *v
2cad8 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 75 74  ;..    /* Comput
2cad9 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74  e the complete t
2cada 65 78 74 20 6f 66 20 74 68 65 20 43 52 45 41 54  ext of the CREAT
2cadb 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20  E VIRTUAL TABLE 
2cadc 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 20  statement */.   
2cadd 20 69 66 28 20 70 45 6e 64 20 29 7b 0a 20 20 20   if( pEnd ){.   
2cade 20 20 20 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65     pParse->sName
2cadf 54 6f 6b 65 6e 2e 6e 20 3d 20 28 69 6e 74 29 28  Token.n = (int)(
2cae0 70 45 6e 64 2d 3e 7a 20 2d 20 70 50 61 72 73 65  pEnd->z - pParse
2cae1 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 7a 29 20  ->sNameToken.z) 
2cae2 2b 20 70 45 6e 64 2d 3e 6e 3b 0a 20 20 20 20 7d  + pEnd->n;.    }
2cae3 0a 20 20 20 20 7a 53 74 6d 74 20 3d 20 73 71 6c  .    zStmt = sql
2cae4 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
2cae5 22 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20  "CREATE VIRTUAL 
2cae6 54 41 42 4c 45 20 25 54 22 2c 20 26 70 50 61 72  TABLE %T", &pPar
2cae7 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 29 3b  se->sNameToken);
2cae8 0a 0a 20 20 20 20 2f 2a 20 41 20 73 6c 6f 74 20  ..    /* A slot 
2cae9 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 68  for the record h
2caea 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
2caeb 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 74 68 65  allocated in the
2caec 20 0a 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f   .    ** SQLITE_
2caed 4d 41 53 54 45 52 20 74 61 62 6c 65 2e 20 20 57  MASTER table.  W
2caee 65 20 6a 75 73 74 20 6e 65 65 64 20 74 6f 20 75  e just need to u
2caef 70 64 61 74 65 20 74 68 61 74 20 73 6c 6f 74 20  pdate that slot 
2caf0 77 69 74 68 20 61 6c 6c 0a 20 20 20 20 2a 2a 20  with all.    ** 
2caf1 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  the information 
2caf2 77 65 27 76 65 20 63 6f 6c 6c 65 63 74 65 64 2e  we've collected.
2caf3 20 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a    .    **.    **
2caf4 20 54 68 65 20 56 4d 20 72 65 67 69 73 74 65 72   The VM register
2caf5 20 6e 75 6d 62 65 72 20 70 50 61 72 73 65 2d 3e   number pParse->
2caf6 72 65 67 52 6f 77 69 64 20 68 6f 6c 64 73 20 74  regRowid holds t
2caf7 68 65 20 72 6f 77 69 64 20 6f 66 20 61 6e 0a 20  he rowid of an. 
2caf8 20 20 20 2a 2a 20 65 6e 74 72 79 20 69 6e 20 74     ** entry in t
2caf9 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  he sqlite_master
2cafa 20 74 61 62 6c 65 20 74 68 74 20 77 61 73 20 63   table tht was c
2cafb 72 65 61 74 65 64 20 66 6f 72 20 74 68 69 73 20  reated for this 
2cafc 76 74 61 62 0a 20 20 20 20 2a 2a 20 62 79 20 73  vtab.    ** by s
2cafd 71 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c 65  qlite3StartTable
2cafe 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ()..    */.    i
2caff 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
2cb00 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54  maToIndex(db, pT
2cb01 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  ab->pSchema);.  
2cb02 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50    sqlite3NestedP
2cb03 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20  arse(pParse,.   
2cb04 20 20 20 22 55 50 44 41 54 45 20 25 51 2e 25 73     "UPDATE %Q.%s
2cb05 20 22 0a 20 20 20 20 20 20 20 20 20 22 53 45 54   ".         "SET
2cb06 20 74 79 70 65 3d 27 74 61 62 6c 65 27 2c 20 6e   type='table', n
2cb07 61 6d 65 3d 25 51 2c 20 74 62 6c 5f 6e 61 6d 65  ame=%Q, tbl_name
2cb08 3d 25 51 2c 20 72 6f 6f 74 70 61 67 65 3d 30 2c  =%Q, rootpage=0,
2cb09 20 73 71 6c 3d 25 51 20 22 0a 20 20 20 20 20 20   sql=%Q ".      
2cb0a 20 22 57 48 45 52 45 20 72 6f 77 69 64 3d 23 25   "WHERE rowid=#%
2cb0b 64 22 2c 0a 20 20 20 20 20 20 64 62 2d 3e 61 44  d",.      db->aD
2cb0c 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43  b[iDb].zName, SC
2cb0d 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c  HEMA_TABLE(iDb),
2cb0e 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61  .      pTab->zNa
2cb0f 6d 65 2c 0a 20 20 20 20 20 20 70 54 61 62 2d 3e  me,.      pTab->
2cb10 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 7a 53 74  zName,.      zSt
2cb11 6d 74 2c 0a 20 20 20 20 20 20 70 50 61 72 73 65  mt,.      pParse
2cb12 2d 3e 72 65 67 52 6f 77 69 64 0a 20 20 20 20 29  ->regRowid.    )
2cb13 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  ;.    sqlite3DbF
2cb14 72 65 65 28 64 62 2c 20 7a 53 74 6d 74 29 3b 0a  ree(db, zStmt);.
2cb15 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47      v = sqlite3G
2cb16 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
2cb17 20 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67      sqlite3Chang
2cb18 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20  eCookie(pParse, 
2cb19 69 44 62 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74  iDb);..    sqlit
2cb1a 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2cb1b 4f 50 5f 45 78 70 69 72 65 2c 20 30 2c 20 30 29  OP_Expire, 0, 0)
2cb1c 3b 0a 20 20 20 20 7a 57 68 65 72 65 20 3d 20 73  ;.    zWhere = s
2cb1d 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
2cb1e 2c 20 22 6e 61 6d 65 3d 27 25 71 27 22 2c 20 70  , "name='%q'", p
2cb1f 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  Tab->zName);.   
2cb20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2cb21 70 34 28 76 2c 20 4f 50 5f 50 61 72 73 65 53 63  p4(v, OP_ParseSc
2cb22 68 65 6d 61 2c 20 69 44 62 2c 20 31 2c 20 30 2c  hema, iDb, 1, 0,
2cb23 20 7a 57 68 65 72 65 2c 20 50 34 5f 44 59 4e 41   zWhere, P4_DYNA
2cb24 4d 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  MIC);.    sqlite
2cb25 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
2cb26 50 5f 56 43 72 65 61 74 65 2c 20 69 44 62 2c 20  P_VCreate, iDb, 
2cb27 30 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20  0, 0, .         
2cb28 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cb29 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 73 71 6c  pTab->zName, sql
2cb2a 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 54 61  ite3Strlen30(pTa
2cb2b 62 2d 3e 7a 4e 61 6d 65 29 20 2b 20 31 29 3b 0a  b->zName) + 1);.
2cb2c 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20    }..  /* If we 
2cb2d 61 72 65 20 72 65 72 65 61 64 69 6e 67 20 74 68  are rereading th
2cb2e 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  e sqlite_master 
2cb2f 74 61 62 6c 65 20 63 72 65 61 74 65 20 74 68 65  table create the
2cb30 20 69 6e 2d 6d 65 6d 6f 72 79 0a 20 20 2a 2a 20   in-memory.  ** 
2cb31 72 65 63 6f 72 64 20 6f 66 20 74 68 65 20 74 61  record of the ta
2cb32 62 6c 65 2e 20 54 68 65 20 78 43 6f 6e 6e 65 63  ble. The xConnec
2cb33 74 28 29 20 6d 65 74 68 6f 64 20 69 73 20 6e 6f  t() method is no
2cb34 74 20 63 61 6c 6c 65 64 20 75 6e 74 69 6c 0a 20  t called until. 
2cb35 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20 74 69   ** the first ti
2cb36 6d 65 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  me the virtual t
2cb37 61 62 6c 65 20 69 73 20 75 73 65 64 20 69 6e 20  able is used in 
2cb38 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  an SQL statement
2cb39 2e 20 54 68 69 73 0a 20 20 2a 2a 20 61 6c 6c 6f  . This.  ** allo
2cb3a 77 73 20 61 20 73 63 68 65 6d 61 20 74 68 61 74  ws a schema that
2cb3b 20 63 6f 6e 74 61 69 6e 73 20 76 69 72 74 75 61   contains virtua
2cb3c 6c 20 74 61 62 6c 65 73 20 74 6f 20 62 65 20 6c  l tables to be l
2cb3d 6f 61 64 65 64 20 62 65 66 6f 72 65 0a 20 20 2a  oaded before.  *
2cb3e 2a 20 74 68 65 20 72 65 71 75 69 72 65 64 20 76  * the required v
2cb3f 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6d 70  irtual table imp
2cb40 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 61 72 65  lementations are
2cb41 20 72 65 67 69 73 74 65 72 65 64 2e 20 20 2a 2f   registered.  */
2cb42 0a 20 20 65 6c 73 65 20 7b 0a 20 20 20 20 54 61  .  else {.    Ta
2cb43 62 6c 65 20 2a 70 4f 6c 64 3b 0a 20 20 20 20 53  ble *pOld;.    S
2cb44 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d  chema *pSchema =
2cb45 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a   pTab->pSchema;.
2cb46 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2cb47 7a 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e 7a 4e  zName = pTab->zN
2cb48 61 6d 65 3b 0a 20 20 20 20 69 6e 74 20 6e 4e 61  ame;.    int nNa
2cb49 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c  me = sqlite3Strl
2cb4a 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 20 20  en30(zName);.   
2cb4b 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74 65 33 48   pOld = sqlite3H
2cb4c 61 73 68 49 6e 73 65 72 74 28 26 70 53 63 68 65  ashInsert(&pSche
2cb4d 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 7a 4e 61  ma->tblHash, zNa
2cb4e 6d 65 2c 20 6e 4e 61 6d 65 2c 20 70 54 61 62 29  me, nName, pTab)
2cb4f 3b 0a 20 20 20 20 69 66 28 20 70 4f 6c 64 20 29  ;.    if( pOld )
2cb50 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c  {.      db->mall
2cb51 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
2cb52 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62      assert( pTab
2cb53 3d 3d 70 4f 6c 64 20 29 3b 20 20 2f 2a 20 4d 61  ==pOld );  /* Ma
2cb54 6c 6c 6f 63 20 6d 75 73 74 20 68 61 76 65 20 66  lloc must have f
2cb55 61 69 6c 65 64 20 69 6e 73 69 64 65 20 48 61 73  ailed inside Has
2cb56 68 49 6e 73 65 72 74 28 29 20 2a 2f 0a 20 20 20  hInsert() */.   
2cb57 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
2cb58 0a 20 20 20 20 70 53 63 68 65 6d 61 2d 3e 64 62  .    pSchema->db
2cb59 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
2cb5a 20 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54     pParse->pNewT
2cb5b 61 62 6c 65 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a  able = 0;.  }.}.
2cb5c 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73 65  ./*.** The parse
2cb5d 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75  r calls this rou
2cb5e 74 69 6e 65 20 77 68 65 6e 20 69 74 20 73 65 65  tine when it see
2cb5f 73 20 74 68 65 20 66 69 72 73 74 20 74 6f 6b 65  s the first toke
2cb60 6e 0a 2a 2a 20 6f 66 20 61 6e 20 61 72 67 75 6d  n.** of an argum
2cb61 65 6e 74 20 74 6f 20 74 68 65 20 6d 6f 64 75 6c  ent to the modul
2cb62 65 20 6e 61 6d 65 20 69 6e 20 61 20 43 52 45 41  e name in a CREA
2cb63 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45  TE VIRTUAL TABLE
2cb64 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53   statement..*/.S
2cb65 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2cb66 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 41 72  id sqlite3VtabAr
2cb67 67 49 6e 69 74 28 50 61 72 73 65 20 2a 70 50 61  gInit(Parse *pPa
2cb68 72 73 65 29 7b 0a 20 20 61 64 64 41 72 67 75 6d  rse){.  addArgum
2cb69 65 6e 74 54 6f 56 74 61 62 28 70 50 61 72 73 65  entToVtab(pParse
2cb6a 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 73 41 72  );.  pParse->sAr
2cb6b 67 2e 7a 20 3d 20 30 3b 0a 20 20 70 50 61 72 73  g.z = 0;.  pPars
2cb6c 65 2d 3e 73 41 72 67 2e 6e 20 3d 20 30 3b 0a 7d  e->sArg.n = 0;.}
2cb6d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73  ../*.** The pars
2cb6e 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f  er calls this ro
2cb6f 75 74 69 6e 65 20 66 6f 72 20 65 61 63 68 20 74  utine for each t
2cb70 6f 6b 65 6e 20 61 66 74 65 72 20 74 68 65 20 66  oken after the f
2cb71 69 72 73 74 20 74 6f 6b 65 6e 0a 2a 2a 20 69 6e  irst token.** in
2cb72 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20   an argument to 
2cb73 74 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20  the module name 
2cb74 69 6e 20 61 20 43 52 45 41 54 45 20 56 49 52 54  in a CREATE VIRT
2cb75 55 41 4c 20 54 41 42 4c 45 20 73 74 61 74 65 6d  UAL TABLE statem
2cb76 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ent..*/.SQLITE_P
2cb77 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2cb78 74 65 33 56 74 61 62 41 72 67 45 78 74 65 6e 64  te3VtabArgExtend
2cb79 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
2cb7a 54 6f 6b 65 6e 20 2a 70 29 7b 0a 20 20 54 6f 6b  Token *p){.  Tok
2cb7b 65 6e 20 2a 70 41 72 67 20 3d 20 26 70 50 61 72  en *pArg = &pPar
2cb7c 73 65 2d 3e 73 41 72 67 3b 0a 20 20 69 66 28 20  se->sArg;.  if( 
2cb7d 70 41 72 67 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20  pArg->z==0 ){.  
2cb7e 20 20 70 41 72 67 2d 3e 7a 20 3d 20 70 2d 3e 7a    pArg->z = p->z
2cb7f 3b 0a 20 20 20 20 70 41 72 67 2d 3e 6e 20 3d 20  ;.    pArg->n = 
2cb80 70 2d 3e 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  p->n;.  }else{. 
2cb81 20 20 20 61 73 73 65 72 74 28 70 41 72 67 2d 3e     assert(pArg->
2cb82 7a 20 3c 20 70 2d 3e 7a 29 3b 0a 20 20 20 20 70  z < p->z);.    p
2cb83 41 72 67 2d 3e 6e 20 3d 20 28 69 6e 74 29 28 26  Arg->n = (int)(&
2cb84 70 2d 3e 7a 5b 70 2d 3e 6e 5d 20 2d 20 70 41 72  p->z[p->n] - pAr
2cb85 67 2d 3e 7a 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  g->z);.  }.}../*
2cb86 0a 2a 2a 20 49 6e 76 6f 6b 65 20 61 20 76 69 72  .** Invoke a vir
2cb87 74 75 61 6c 20 74 61 62 6c 65 20 63 6f 6e 73 74  tual table const
2cb88 72 75 63 74 6f 72 20 28 65 69 74 68 65 72 20 78  ructor (either x
2cb89 43 72 65 61 74 65 20 6f 72 20 78 43 6f 6e 6e 65  Create or xConne
2cb8a 63 74 29 2e 20 54 68 65 0a 2a 2a 20 70 6f 69 6e  ct). The.** poin
2cb8b 74 65 72 20 74 6f 20 74 68 65 20 66 75 6e 63 74  ter to the funct
2cb8c 69 6f 6e 20 74 6f 20 69 6e 76 6f 6b 65 20 69 73  ion to invoke is
2cb8d 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66   passed as the f
2cb8e 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 0a  ourth parameter.
2cb8f 2a 2a 20 74 6f 20 74 68 69 73 20 70 72 6f 63 65  ** to this proce
2cb90 64 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  dure..*/.static 
2cb91 69 6e 74 20 76 74 61 62 43 61 6c 6c 43 6f 6e 73  int vtabCallCons
2cb92 74 72 75 63 74 6f 72 28 0a 20 20 73 71 6c 69 74  tructor(.  sqlit
2cb93 65 33 20 2a 64 62 2c 20 0a 20 20 54 61 62 6c 65  e3 *db, .  Table
2cb94 20 2a 70 54 61 62 2c 0a 20 20 4d 6f 64 75 6c 65   *pTab,.  Module
2cb95 20 2a 70 4d 6f 64 2c 0a 20 20 69 6e 74 20 28 2a   *pMod,.  int (*
2cb96 78 43 6f 6e 73 74 72 75 63 74 29 28 73 71 6c 69  xConstruct)(sqli
2cb97 74 65 33 2a 2c 76 6f 69 64 2a 2c 69 6e 74 2c 63  te3*,void*,int,c
2cb98 6f 6e 73 74 20 63 68 61 72 2a 63 6f 6e 73 74 2a  onst char*const*
2cb99 2c 73 71 6c 69 74 65 33 5f 76 74 61 62 2a 2a 2c  ,sqlite3_vtab**,
2cb9a 63 68 61 72 2a 2a 29 2c 0a 20 20 63 68 61 72 20  char**),.  char 
2cb9b 2a 2a 70 7a 45 72 72 0a 29 7b 0a 20 20 56 54 61  **pzErr.){.  VTa
2cb9c 62 6c 65 20 2a 70 56 54 61 62 6c 65 3b 0a 20 20  ble *pVTable;.  
2cb9d 69 6e 74 20 72 63 3b 0a 20 20 63 6f 6e 73 74 20  int rc;.  const 
2cb9e 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 7a 41 72  char *const*azAr
2cb9f 67 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  g = (const char 
2cba0 2a 63 6f 6e 73 74 2a 29 70 54 61 62 2d 3e 61 7a  *const*)pTab->az
2cba1 4d 6f 64 75 6c 65 41 72 67 3b 0a 20 20 69 6e 74  ModuleArg;.  int
2cba2 20 6e 41 72 67 20 3d 20 70 54 61 62 2d 3e 6e 4d   nArg = pTab->nM
2cba3 6f 64 75 6c 65 41 72 67 3b 0a 20 20 63 68 61 72  oduleArg;.  char
2cba4 20 2a 7a 45 72 72 20 3d 20 30 3b 0a 20 20 63 68   *zErr = 0;.  ch
2cba5 61 72 20 2a 7a 4d 6f 64 75 6c 65 4e 61 6d 65 20  ar *zModuleName 
2cba6 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
2cba7 28 64 62 2c 20 22 25 73 22 2c 20 70 54 61 62 2d  (db, "%s", pTab-
2cba8 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 69 66 28 20  >zName);..  if( 
2cba9 21 7a 4d 6f 64 75 6c 65 4e 61 6d 65 20 29 7b 0a  !zModuleName ){.
2cbaa 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2cbab 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20  E_NOMEM;.  }..  
2cbac 70 56 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65  pVTable = sqlite
2cbad 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
2cbae 2c 20 73 69 7a 65 6f 66 28 56 54 61 62 6c 65 29  , sizeof(VTable)
2cbaf 29 3b 0a 20 20 69 66 28 20 21 70 56 54 61 62 6c  );.  if( !pVTabl
2cbb0 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  e ){.    sqlite3
2cbb1 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 6f 64 75  DbFree(db, zModu
2cbb2 6c 65 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74  leName);.    ret
2cbb3 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
2cbb4 3b 0a 20 20 7d 0a 20 20 70 56 54 61 62 6c 65 2d  ;.  }.  pVTable-
2cbb5 3e 64 62 20 3d 20 64 62 3b 0a 20 20 70 56 54 61  >db = db;.  pVTa
2cbb6 62 6c 65 2d 3e 70 4d 6f 64 20 3d 20 70 4d 6f 64  ble->pMod = pMod
2cbb7 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 21 64 62  ;..  assert( !db
2cbb8 2d 3e 70 56 54 61 62 20 29 3b 0a 20 20 61 73 73  ->pVTab );.  ass
2cbb9 65 72 74 28 20 78 43 6f 6e 73 74 72 75 63 74 20  ert( xConstruct 
2cbba 29 3b 0a 20 20 64 62 2d 3e 70 56 54 61 62 20 3d  );.  db->pVTab =
2cbbb 20 70 54 61 62 3b 0a 0a 20 20 2f 2a 20 49 6e 76   pTab;..  /* Inv
2cbbc 6f 6b 65 20 74 68 65 20 76 69 72 74 75 61 6c 20  oke the virtual 
2cbbd 74 61 62 6c 65 20 63 6f 6e 73 74 72 75 63 74 6f  table constructo
2cbbe 72 20 2a 2f 0a 20 20 28 76 6f 69 64 29 73 71 6c  r */.  (void)sql
2cbbf 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62  ite3SafetyOff(db
2cbc0 29 3b 0a 20 20 72 63 20 3d 20 78 43 6f 6e 73 74  );.  rc = xConst
2cbc1 72 75 63 74 28 64 62 2c 20 70 4d 6f 64 2d 3e 70  ruct(db, pMod->p
2cbc2 41 75 78 2c 20 6e 41 72 67 2c 20 61 7a 41 72 67  Aux, nArg, azArg
2cbc3 2c 20 26 70 56 54 61 62 6c 65 2d 3e 70 56 74 61  , &pVTable->pVta
2cbc4 62 2c 20 26 7a 45 72 72 29 3b 0a 20 20 28 76 6f  b, &zErr);.  (vo
2cbc5 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79  id)sqlite3Safety
2cbc6 4f 6e 28 64 62 29 3b 0a 20 20 69 66 28 20 72 63  On(db);.  if( rc
2cbc7 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29  ==SQLITE_NOMEM )
2cbc8 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2cbc9 64 20 3d 20 31 3b 0a 0a 20 20 69 66 28 20 53 51  d = 1;..  if( SQ
2cbca 4c 49 54 45 5f 4f 4b 21 3d 72 63 20 29 7b 0a 20  LITE_OK!=rc ){. 
2cbcb 20 20 20 69 66 28 20 7a 45 72 72 3d 3d 30 20 29     if( zErr==0 )
2cbcc 7b 0a 20 20 20 20 20 20 2a 70 7a 45 72 72 20 3d  {.      *pzErr =
2cbcd 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
2cbce 64 62 2c 20 22 76 74 61 62 6c 65 20 63 6f 6e 73  db, "vtable cons
2cbcf 74 72 75 63 74 6f 72 20 66 61 69 6c 65 64 3a 20  tructor failed: 
2cbd0 25 73 22 2c 20 7a 4d 6f 64 75 6c 65 4e 61 6d 65  %s", zModuleName
2cbd1 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 7b 0a 20  );.    }else {. 
2cbd2 20 20 20 20 20 2a 70 7a 45 72 72 20 3d 20 73 71       *pzErr = sq
2cbd3 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
2cbd4 20 22 25 73 22 2c 20 7a 45 72 72 29 3b 0a 20 20   "%s", zErr);.  
2cbd5 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2cbd6 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20  e(db, zErr);.   
2cbd7 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62   }.    sqlite3Db
2cbd8 46 72 65 65 28 64 62 2c 20 70 56 54 61 62 6c 65  Free(db, pVTable
2cbd9 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 41  );.  }else if( A
2cbda 4c 57 41 59 53 28 70 56 54 61 62 6c 65 2d 3e 70  LWAYS(pVTable->p
2cbdb 56 74 61 62 29 20 29 7b 0a 20 20 20 20 2f 2a 20  Vtab) ){.    /* 
2cbdc 4a 75 73 74 69 66 69 63 61 74 69 6f 6e 20 6f 66  Justification of
2cbdd 20 41 4c 57 41 59 53 28 29 3a 20 20 41 20 63 6f   ALWAYS():  A co
2cbde 72 72 65 63 74 20 76 74 61 62 20 63 6f 6e 73 74  rrect vtab const
2cbdf 72 75 63 74 6f 72 20 6d 75 73 74 20 61 6c 6c 6f  ructor must allo
2cbe0 63 61 74 65 0a 20 20 20 20 2a 2a 20 74 68 65 20  cate.    ** the 
2cbe1 73 71 6c 69 74 65 33 5f 76 74 61 62 20 6f 62 6a  sqlite3_vtab obj
2cbe2 65 63 74 20 69 66 20 73 75 63 63 65 73 73 66 75  ect if successfu
2cbe3 6c 2e 20 20 2a 2f 0a 20 20 20 20 70 56 54 61 62  l.  */.    pVTab
2cbe4 6c 65 2d 3e 70 56 74 61 62 2d 3e 70 4d 6f 64 75  le->pVtab->pModu
2cbe5 6c 65 20 3d 20 70 4d 6f 64 2d 3e 70 4d 6f 64 75  le = pMod->pModu
2cbe6 6c 65 3b 0a 20 20 20 20 70 56 54 61 62 6c 65 2d  le;.    pVTable-
2cbe7 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 69  >nRef = 1;.    i
2cbe8 66 28 20 64 62 2d 3e 70 56 54 61 62 20 29 7b 0a  f( db->pVTab ){.
2cbe9 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
2cbea 20 2a 7a 46 6f 72 6d 61 74 20 3d 20 22 76 74 61   *zFormat = "vta
2cbeb 62 6c 65 20 63 6f 6e 73 74 72 75 63 74 6f 72 20  ble constructor 
2cbec 64 69 64 20 6e 6f 74 20 64 65 63 6c 61 72 65 20  did not declare 
2cbed 73 63 68 65 6d 61 3a 20 25 73 22 3b 0a 20 20 20  schema: %s";.   
2cbee 20 20 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69     *pzErr = sqli
2cbef 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a  te3MPrintf(db, z
2cbf0 46 6f 72 6d 61 74 2c 20 70 54 61 62 2d 3e 7a 4e  Format, pTab->zN
2cbf1 61 6d 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ame);.      sqli
2cbf2 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 70 56  te3VtabUnlock(pV
2cbf3 54 61 62 6c 65 29 3b 0a 20 20 20 20 20 20 72 63  Table);.      rc
2cbf4 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
2cbf5 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2cbf6 20 20 69 6e 74 20 69 43 6f 6c 3b 0a 20 20 20 20    int iCol;.    
2cbf7 20 20 2f 2a 20 49 66 20 65 76 65 72 79 74 68 69    /* If everythi
2cbf8 6e 67 20 77 65 6e 74 20 61 63 63 6f 72 64 69 6e  ng went accordin
2cbf9 67 20 74 6f 20 70 6c 61 6e 2c 20 6c 69 6e 6b 20  g to plan, link 
2cbfa 74 68 65 20 6e 65 77 20 56 54 61 62 6c 65 20 73  the new VTable s
2cbfb 74 72 75 63 74 75 72 65 0a 20 20 20 20 20 20 2a  tructure.      *
2cbfc 2a 20 69 6e 74 6f 20 74 68 65 20 6c 69 6e 6b 65  * into the linke
2cbfd 64 20 6c 69 73 74 20 68 65 61 64 65 64 20 62 79  d list headed by
2cbfe 20 70 54 61 62 2d 3e 70 56 54 61 62 6c 65 2e 20   pTab->pVTable. 
2cbff 54 68 65 6e 20 6c 6f 6f 70 20 74 68 72 6f 75 67  Then loop throug
2cc00 68 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20  h the .      ** 
2cc01 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 74  columns of the t
2cc02 61 62 6c 65 20 74 6f 20 73 65 65 20 69 66 20 61  able to see if a
2cc03 6e 79 20 6f 66 20 74 68 65 6d 20 63 6f 6e 74 61  ny of them conta
2cc04 69 6e 20 74 68 65 20 74 6f 6b 65 6e 20 22 68 69  in the token "hi
2cc05 64 64 65 6e 22 2e 0a 20 20 20 20 20 20 2a 2a 20  dden"..      ** 
2cc06 49 66 20 73 6f 2c 20 73 65 74 20 74 68 65 20 43  If so, set the C
2cc07 6f 6c 75 6d 6e 2e 69 73 48 69 64 64 65 6e 20 66  olumn.isHidden f
2cc08 6c 61 67 20 61 6e 64 20 72 65 6d 6f 76 65 20 74  lag and remove t
2cc09 68 65 20 74 6f 6b 65 6e 20 66 72 6f 6d 0a 20 20  he token from.  
2cc0a 20 20 20 20 2a 2a 20 74 68 65 20 74 79 70 65 20      ** the type 
2cc0b 73 74 72 69 6e 67 2e 20 20 2a 2f 0a 20 20 20 20  string.  */.    
2cc0c 20 20 70 56 54 61 62 6c 65 2d 3e 70 4e 65 78 74    pVTable->pNext
2cc0d 20 3d 20 70 54 61 62 2d 3e 70 56 54 61 62 6c 65   = pTab->pVTable
2cc0e 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 70 56  ;.      pTab->pV
2cc0f 54 61 62 6c 65 20 3d 20 70 56 54 61 62 6c 65 3b  Table = pVTable;
2cc10 0a 0a 20 20 20 20 20 20 66 6f 72 28 69 43 6f 6c  ..      for(iCol
2cc11 3d 30 3b 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e  =0; iCol<pTab->n
2cc12 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20  Col; iCol++){.  
2cc13 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 79 70        char *zTyp
2cc14 65 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69  e = pTab->aCol[i
2cc15 43 6f 6c 5d 2e 7a 54 79 70 65 3b 0a 20 20 20 20  Col].zType;.    
2cc16 20 20 20 20 69 6e 74 20 6e 54 79 70 65 3b 0a 20      int nType;. 
2cc17 20 20 20 20 20 20 20 69 6e 74 20 69 20 3d 20 30         int i = 0
2cc18 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 7a  ;.        if( !z
2cc19 54 79 70 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b  Type ) continue;
2cc1a 0a 20 20 20 20 20 20 20 20 6e 54 79 70 65 20 3d  .        nType =
2cc1b 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
2cc1c 28 7a 54 79 70 65 29 3b 0a 20 20 20 20 20 20 20  (zType);.       
2cc1d 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e   if( sqlite3StrN
2cc1e 49 43 6d 70 28 22 68 69 64 64 65 6e 22 2c 20 7a  ICmp("hidden", z
2cc1f 54 79 70 65 2c 20 36 29 7c 7c 28 7a 54 79 70 65  Type, 6)||(zType
2cc20 5b 36 5d 20 26 26 20 7a 54 79 70 65 5b 36 5d 21  [6] && zType[6]!
2cc21 3d 27 20 27 29 20 29 7b 0a 20 20 20 20 20 20 20  =' ') ){.       
2cc22 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 54     for(i=0; i<nT
2cc23 79 70 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  ype; i++){.     
2cc24 20 20 20 20 20 20 20 69 66 28 20 28 30 3d 3d 73         if( (0==s
2cc25 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 22  qlite3StrNICmp("
2cc26 20 68 69 64 64 65 6e 22 2c 20 26 7a 54 79 70 65   hidden", &zType
2cc27 5b 69 5d 2c 20 37 29 29 0a 20 20 20 20 20 20 20  [i], 7)).       
2cc28 20 20 20 20 20 20 26 26 20 28 7a 54 79 70 65 5b        && (zType[
2cc29 69 2b 37 5d 3d 3d 27 5c 30 27 20 7c 7c 20 7a 54  i+7]=='\0' || zT
2cc2a 79 70 65 5b 69 2b 37 5d 3d 3d 27 20 27 29 0a 20  ype[i+7]==' '). 
2cc2b 20 20 20 20 20 20 20 20 20 20 20 29 7b 0a 20 20             ){.  
2cc2c 20 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b              i++;
2cc2d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62  .              b
2cc2e 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
2cc2f 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a    }.          }.
2cc30 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2cc31 20 20 69 66 28 20 69 3c 6e 54 79 70 65 20 29 7b    if( i<nType ){
2cc32 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6a  .          int j
2cc33 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20  ;.          int 
2cc34 6e 44 65 6c 20 3d 20 36 20 2b 20 28 7a 54 79 70  nDel = 6 + (zTyp
2cc35 65 5b 69 2b 36 5d 20 3f 20 31 20 3a 20 30 29 3b  e[i+6] ? 1 : 0);
2cc36 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6a  .          for(j
2cc37 3d 69 3b 20 28 6a 2b 6e 44 65 6c 29 3c 3d 6e 54  =i; (j+nDel)<=nT
2cc38 79 70 65 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  ype; j++){.     
2cc39 20 20 20 20 20 20 20 7a 54 79 70 65 5b 6a 5d 20         zType[j] 
2cc3a 3d 20 7a 54 79 70 65 5b 6a 2b 6e 44 65 6c 5d 3b  = zType[j+nDel];
2cc3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
2cc3c 20 20 20 20 20 20 20 69 66 28 20 7a 54 79 70 65         if( zType
2cc3d 5b 69 5d 3d 3d 27 5c 30 27 20 26 26 20 69 3e 30  [i]=='\0' && i>0
2cc3e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
2cc3f 61 73 73 65 72 74 28 7a 54 79 70 65 5b 69 2d 31  assert(zType[i-1
2cc40 5d 3d 3d 27 20 27 29 3b 0a 20 20 20 20 20 20 20  ]==' ');.       
2cc41 20 20 20 20 20 7a 54 79 70 65 5b 69 2d 31 5d 20       zType[i-1] 
2cc42 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 20  = '\0';.        
2cc43 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 54    }.          pT
2cc44 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 69  ab->aCol[iCol].i
2cc45 73 48 69 64 64 65 6e 20 3d 20 31 3b 0a 20 20 20  sHidden = 1;.   
2cc46 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
2cc47 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 73 71 6c 69     }.  }..  sqli
2cc48 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d  te3DbFree(db, zM
2cc49 6f 64 75 6c 65 4e 61 6d 65 29 3b 0a 20 20 64 62  oduleName);.  db
2cc4a 2d 3e 70 56 54 61 62 20 3d 20 30 3b 0a 20 20 72  ->pVTab = 0;.  r
2cc4b 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
2cc4c 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
2cc4d 20 69 73 20 69 6e 76 6f 6b 65 64 20 62 79 20 74   is invoked by t
2cc4e 68 65 20 70 61 72 73 65 72 20 74 6f 20 63 61 6c  he parser to cal
2cc4f 6c 20 74 68 65 20 78 43 6f 6e 6e 65 63 74 28 29  l the xConnect()
2cc50 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 68   method.** of th
2cc51 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
2cc52 70 54 61 62 2e 20 49 66 20 61 6e 20 65 72 72 6f  pTab. If an erro
2cc53 72 20 6f 63 63 75 72 73 2c 20 61 6e 20 65 72 72  r occurs, an err
2cc54 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
2cc55 6e 65 64 20 0a 2a 2a 20 61 6e 64 20 61 6e 20 65  ned .** and an e
2cc56 72 72 6f 72 20 6c 65 66 74 20 69 6e 20 70 50 61  rror left in pPa
2cc57 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  rse..**.** This 
2cc58 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 20  call is a no-op 
2cc59 69 66 20 74 61 62 6c 65 20 70 54 61 62 20 69 73  if table pTab is
2cc5a 20 6e 6f 74 20 61 20 76 69 72 74 75 61 6c 20 74   not a virtual t
2cc5b 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  able..*/.SQLITE_
2cc5c 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
2cc5d 74 65 33 56 74 61 62 43 61 6c 6c 43 6f 6e 6e 65  te3VtabCallConne
2cc5e 63 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ct(Parse *pParse
2cc5f 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a  , Table *pTab){.
2cc60 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
2cc61 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 63 6f  pParse->db;.  co
2cc62 6e 73 74 20 63 68 61 72 20 2a 7a 4d 6f 64 3b 0a  nst char *zMod;.
2cc63 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 3b 0a    Module *pMod;.
2cc64 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73    int rc;..  ass
2cc65 65 72 74 28 20 70 54 61 62 20 29 3b 0a 20 20 69  ert( pTab );.  i
2cc66 66 28 20 28 70 54 61 62 2d 3e 74 61 62 46 6c 61  f( (pTab->tabFla
2cc67 67 73 20 26 20 54 46 5f 56 69 72 74 75 61 6c 29  gs & TF_Virtual)
2cc68 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 47 65  ==0 || sqlite3Ge
2cc69 74 56 54 61 62 6c 65 28 64 62 2c 20 70 54 61 62  tVTable(db, pTab
2cc6a 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
2cc6b 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a  SQLITE_OK;.  }..
2cc6c 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20    /* Locate the 
2cc6d 72 65 71 75 69 72 65 64 20 76 69 72 74 75 61 6c  required virtual
2cc6e 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 2a 2f   table module */
2cc6f 0a 20 20 7a 4d 6f 64 20 3d 20 70 54 61 62 2d 3e  .  zMod = pTab->
2cc70 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 30 5d 3b 0a  azModuleArg[0];.
2cc71 20 20 70 4d 6f 64 20 3d 20 28 4d 6f 64 75 6c 65    pMod = (Module
2cc72 2a 29 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e  *)sqlite3HashFin
2cc73 64 28 26 64 62 2d 3e 61 4d 6f 64 75 6c 65 2c 20  d(&db->aModule, 
2cc74 7a 4d 6f 64 2c 20 73 71 6c 69 74 65 33 53 74 72  zMod, sqlite3Str
2cc75 6c 65 6e 33 30 28 7a 4d 6f 64 29 29 3b 0a 0a 20  len30(zMod));.. 
2cc76 20 69 66 28 20 21 70 4d 6f 64 20 29 7b 0a 20 20   if( !pMod ){.  
2cc77 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d    const char *zM
2cc78 6f 64 75 6c 65 20 3d 20 70 54 61 62 2d 3e 61 7a  odule = pTab->az
2cc79 4d 6f 64 75 6c 65 41 72 67 5b 30 5d 3b 0a 20 20  ModuleArg[0];.  
2cc7a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
2cc7b 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75  g(pParse, "no su
2cc7c 63 68 20 6d 6f 64 75 6c 65 3a 20 25 73 22 2c 20  ch module: %s", 
2cc7d 7a 4d 6f 64 75 6c 65 29 3b 0a 20 20 20 20 72 63  zModule);.    rc
2cc7e 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
2cc7f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68  .  }else{.    ch
2cc80 61 72 20 2a 7a 45 72 72 20 3d 20 30 3b 0a 20 20  ar *zErr = 0;.  
2cc81 20 20 72 63 20 3d 20 76 74 61 62 43 61 6c 6c 43    rc = vtabCallC
2cc82 6f 6e 73 74 72 75 63 74 6f 72 28 64 62 2c 20 70  onstructor(db, p
2cc83 54 61 62 2c 20 70 4d 6f 64 2c 20 70 4d 6f 64 2d  Tab, pMod, pMod-
2cc84 3e 70 4d 6f 64 75 6c 65 2d 3e 78 43 6f 6e 6e 65  >pModule->xConne
2cc85 63 74 2c 20 26 7a 45 72 72 29 3b 0a 20 20 20 20  ct, &zErr);.    
2cc86 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2cc87 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  K ){.      sqlit
2cc88 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2cc89 65 2c 20 22 25 73 22 2c 20 7a 45 72 72 29 3b 0a  e, "%s", zErr);.
2cc8a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
2cc8b 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72  3DbFree(db, zErr
2cc8c 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  );.  }..  return
2cc8d 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64   rc;.}../*.** Ad
2cc8e 64 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  d the virtual ta
2cc8f 62 6c 65 20 70 56 54 61 62 20 74 6f 20 74 68 65  ble pVTab to the
2cc90 20 61 72 72 61 79 20 73 71 6c 69 74 65 33 2e 61   array sqlite3.a
2cc91 56 54 72 61 6e 73 5b 5d 2e 0a 2a 2f 0a 73 74 61  VTrans[]..*/.sta
2cc92 74 69 63 20 69 6e 74 20 61 64 64 54 6f 56 54 72  tic int addToVTr
2cc93 61 6e 73 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ans(sqlite3 *db,
2cc94 20 56 54 61 62 6c 65 20 2a 70 56 54 61 62 29 7b   VTable *pVTab){
2cc95 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 41 52 52  .  const int ARR
2cc96 41 59 5f 49 4e 43 52 20 3d 20 35 3b 0a 0a 20 20  AY_INCR = 5;..  
2cc97 2f 2a 20 47 72 6f 77 20 74 68 65 20 73 71 6c 69  /* Grow the sqli
2cc98 74 65 33 2e 61 56 54 72 61 6e 73 20 61 72 72 61  te3.aVTrans arra
2cc99 79 20 69 66 20 72 65 71 75 69 72 65 64 20 2a 2f  y if required */
2cc9a 0a 20 20 69 66 28 20 28 64 62 2d 3e 6e 56 54 72  .  if( (db->nVTr
2cc9b 61 6e 73 25 41 52 52 41 59 5f 49 4e 43 52 29 3d  ans%ARRAY_INCR)=
2cc9c 3d 30 20 29 7b 0a 20 20 20 20 56 54 61 62 6c 65  =0 ){.    VTable
2cc9d 20 2a 2a 61 56 54 72 61 6e 73 3b 0a 20 20 20 20   **aVTrans;.    
2cc9e 69 6e 74 20 6e 42 79 74 65 73 20 3d 20 73 69 7a  int nBytes = siz
2cc9f 65 6f 66 28 73 71 6c 69 74 65 33 5f 76 74 61 62  eof(sqlite3_vtab
2cca0 20 2a 29 20 2a 20 28 64 62 2d 3e 6e 56 54 72 61   *) * (db->nVTra
2cca1 6e 73 20 2b 20 41 52 52 41 59 5f 49 4e 43 52 29  ns + ARRAY_INCR)
2cca2 3b 0a 20 20 20 20 61 56 54 72 61 6e 73 20 3d 20  ;.    aVTrans = 
2cca3 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63  sqlite3DbRealloc
2cca4 28 64 62 2c 20 28 76 6f 69 64 20 2a 29 64 62 2d  (db, (void *)db-
2cca5 3e 61 56 54 72 61 6e 73 2c 20 6e 42 79 74 65 73  >aVTrans, nBytes
2cca6 29 3b 0a 20 20 20 20 69 66 28 20 21 61 56 54 72  );.    if( !aVTr
2cca7 61 6e 73 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ans ){.      ret
2cca8 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
2cca9 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73  ;.    }.    mems
2ccaa 65 74 28 26 61 56 54 72 61 6e 73 5b 64 62 2d 3e  et(&aVTrans[db->
2ccab 6e 56 54 72 61 6e 73 5d 2c 20 30 2c 20 73 69 7a  nVTrans], 0, siz
2ccac 65 6f 66 28 73 71 6c 69 74 65 33 5f 76 74 61 62  eof(sqlite3_vtab
2ccad 20 2a 29 2a 41 52 52 41 59 5f 49 4e 43 52 29 3b   *)*ARRAY_INCR);
2ccae 0a 20 20 20 20 64 62 2d 3e 61 56 54 72 61 6e 73  .    db->aVTrans
2ccaf 20 3d 20 61 56 54 72 61 6e 73 3b 0a 20 20 7d 0a   = aVTrans;.  }.
2ccb0 0a 20 20 2f 2a 20 41 64 64 20 70 56 74 61 62 20  .  /* Add pVtab 
2ccb1 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 73 71  to the end of sq
2ccb2 6c 69 74 65 33 2e 61 56 54 72 61 6e 73 20 2a 2f  lite3.aVTrans */
2ccb3 0a 20 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b 64  .  db->aVTrans[d
2ccb4 62 2d 3e 6e 56 54 72 61 6e 73 2b 2b 5d 20 3d 20  b->nVTrans++] = 
2ccb5 70 56 54 61 62 3b 0a 20 20 73 71 6c 69 74 65 33  pVTab;.  sqlite3
2ccb6 56 74 61 62 4c 6f 63 6b 28 70 56 54 61 62 29 3b  VtabLock(pVTab);
2ccb7 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
2ccb8 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  _OK;.}../*.** Th
2ccb9 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69  is function is i
2ccba 6e 76 6f 6b 65 64 20 62 79 20 74 68 65 20 76 64  nvoked by the vd
2ccbb 62 65 20 74 6f 20 63 61 6c 6c 20 74 68 65 20 78  be to call the x
2ccbc 43 72 65 61 74 65 20 6d 65 74 68 6f 64 0a 2a 2a  Create method.**
2ccbd 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20   of the virtual 
2ccbe 74 61 62 6c 65 20 6e 61 6d 65 64 20 7a 54 61 62  table named zTab
2ccbf 20 69 6e 20 64 61 74 61 62 61 73 65 20 69 44 62   in database iDb
2ccc0 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65  . .**.** If an e
2ccc1 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 2a 70 7a  rror occurs, *pz
2ccc2 45 72 72 20 69 73 20 73 65 74 20 74 6f 20 70 6f  Err is set to po
2ccc3 69 6e 74 20 61 6e 20 61 6e 20 45 6e 67 6c 69 73  int an an Englis
2ccc4 68 20 6c 61 6e 67 75 61 67 65 0a 2a 2a 20 64 65  h language.** de
2ccc5 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 65  scription of the
2ccc6 20 65 72 72 6f 72 20 61 6e 64 20 61 6e 20 53 51   error and an SQ
2ccc7 4c 49 54 45 5f 58 58 58 20 65 72 72 6f 72 20 63  LITE_XXX error c
2ccc8 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ode is returned.
2ccc9 0a 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65  .** In this case
2ccca 20 74 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74   the caller must
2cccb 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 44 62 46   call sqlite3DbF
2cccc 72 65 65 28 64 62 2c 20 29 20 6f 6e 20 2a 70 7a  ree(db, ) on *pz
2cccd 45 72 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  Err..*/.SQLITE_P
2ccce 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2cccf 65 33 56 74 61 62 43 61 6c 6c 43 72 65 61 74 65  e3VtabCallCreate
2ccd0 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
2ccd1 74 20 69 44 62 2c 20 63 6f 6e 73 74 20 63 68 61  t iDb, const cha
2ccd2 72 20 2a 7a 54 61 62 2c 20 63 68 61 72 20 2a 2a  r *zTab, char **
2ccd3 70 7a 45 72 72 29 7b 0a 20 20 69 6e 74 20 72 63  pzErr){.  int rc
2ccd4 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
2ccd5 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 4d  Table *pTab;.  M
2ccd6 6f 64 75 6c 65 20 2a 70 4d 6f 64 3b 0a 20 20 63  odule *pMod;.  c
2ccd7 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 6f 64 3b  onst char *zMod;
2ccd8 0a 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74  ..  pTab = sqlit
2ccd9 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20  e3FindTable(db, 
2ccda 7a 54 61 62 2c 20 64 62 2d 3e 61 44 62 5b 69 44  zTab, db->aDb[iD
2ccdb 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 61 73 73  b].zName);.  ass
2ccdc 65 72 74 28 20 70 54 61 62 20 26 26 20 28 70 54  ert( pTab && (pT
2ccdd 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54  ab->tabFlags & T
2ccde 46 5f 56 69 72 74 75 61 6c 29 21 3d 30 20 26 26  F_Virtual)!=0 &&
2ccdf 20 21 70 54 61 62 2d 3e 70 56 54 61 62 6c 65 20   !pTab->pVTable 
2cce0 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20  );..  /* Locate 
2cce1 74 68 65 20 72 65 71 75 69 72 65 64 20 76 69 72  the required vir
2cce2 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c  tual table modul
2cce3 65 20 2a 2f 0a 20 20 7a 4d 6f 64 20 3d 20 70 54  e */.  zMod = pT
2cce4 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b  ab->azModuleArg[
2cce5 30 5d 3b 0a 20 20 70 4d 6f 64 20 3d 20 28 4d 6f  0];.  pMod = (Mo
2cce6 64 75 6c 65 2a 29 73 71 6c 69 74 65 33 48 61 73  dule*)sqlite3Has
2cce7 68 46 69 6e 64 28 26 64 62 2d 3e 61 4d 6f 64 75  hFind(&db->aModu
2cce8 6c 65 2c 20 7a 4d 6f 64 2c 20 73 71 6c 69 74 65  le, zMod, sqlite
2cce9 33 53 74 72 6c 65 6e 33 30 28 7a 4d 6f 64 29 29  3Strlen30(zMod))
2ccea 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6d  ;..  /* If the m
2cceb 6f 64 75 6c 65 20 68 61 73 20 62 65 65 6e 20 72  odule has been r
2ccec 65 67 69 73 74 65 72 65 64 20 61 6e 64 20 69 6e  egistered and in
2cced 63 6c 75 64 65 73 20 61 20 43 72 65 61 74 65 20  cludes a Create 
2ccee 6d 65 74 68 6f 64 2c 20 0a 20 20 2a 2a 20 69 6e  method, .  ** in
2ccef 76 6f 6b 65 20 69 74 20 6e 6f 77 2e 20 49 66 20  voke it now. If 
2ccf0 74 68 65 20 6d 6f 64 75 6c 65 20 68 61 73 20 6e  the module has n
2ccf1 6f 74 20 62 65 65 6e 20 72 65 67 69 73 74 65 72  ot been register
2ccf2 65 64 2c 20 72 65 74 75 72 6e 20 61 6e 20 0a 20  ed, return an . 
2ccf3 20 2a 2a 20 65 72 72 6f 72 2e 20 4f 74 68 65 72   ** error. Other
2ccf4 77 69 73 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67  wise, do nothing
2ccf5 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 4d  ..  */.  if( !pM
2ccf6 6f 64 20 29 7b 0a 20 20 20 20 2a 70 7a 45 72 72  od ){.    *pzErr
2ccf7 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
2ccf8 66 28 64 62 2c 20 22 6e 6f 20 73 75 63 68 20 6d  f(db, "no such m
2ccf9 6f 64 75 6c 65 3a 20 25 73 22 2c 20 7a 4d 6f 64  odule: %s", zMod
2ccfa 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  );.    rc = SQLI
2ccfb 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 65 6c 73  TE_ERROR;.  }els
2ccfc 65 7b 0a 20 20 20 20 72 63 20 3d 20 76 74 61 62  e{.    rc = vtab
2ccfd 43 61 6c 6c 43 6f 6e 73 74 72 75 63 74 6f 72 28  CallConstructor(
2ccfe 64 62 2c 20 70 54 61 62 2c 20 70 4d 6f 64 2c 20  db, pTab, pMod, 
2ccff 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78  pMod->pModule->x
2cd00 43 72 65 61 74 65 2c 20 70 7a 45 72 72 29 3b 0a  Create, pzErr);.
2cd01 20 20 7d 0a 0a 20 20 2f 2a 20 4a 75 73 74 69 66    }..  /* Justif
2cd02 69 63 61 74 69 6f 6e 20 6f 66 20 41 4c 57 41 59  ication of ALWAY
2cd03 53 28 29 3a 20 20 54 68 65 20 78 43 6f 6e 73 74  S():  The xConst
2cd04 72 75 63 74 6f 72 20 6d 65 74 68 6f 64 20 69 73  ructor method is
2cd05 20 72 65 71 75 69 72 65 64 20 74 6f 0a 20 20 2a   required to.  *
2cd06 2a 20 63 72 65 61 74 65 20 61 20 76 61 6c 69 64  * create a valid
2cd07 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 69 66   sqlite3_vtab if
2cd08 20 69 74 20 72 65 74 75 72 6e 73 20 53 51 4c 49   it returns SQLI
2cd09 54 45 5f 4f 4b 2e 20 2a 2f 0a 20 20 69 66 28 20  TE_OK. */.  if( 
2cd0a 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
2cd0b 20 41 4c 57 41 59 53 28 73 71 6c 69 74 65 33 47   ALWAYS(sqlite3G
2cd0c 65 74 56 54 61 62 6c 65 28 64 62 2c 20 70 54 61  etVTable(db, pTa
2cd0d 62 29 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20  b)) ){.      rc 
2cd0e 3d 20 61 64 64 54 6f 56 54 72 61 6e 73 28 64 62  = addToVTrans(db
2cd0f 2c 20 73 71 6c 69 74 65 33 47 65 74 56 54 61 62  , sqlite3GetVTab
2cd10 6c 65 28 64 62 2c 20 70 54 61 62 29 29 3b 0a 20  le(db, pTab));. 
2cd11 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
2cd12 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
2cd13 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  unction is used 
2cd14 74 6f 20 73 65 74 20 74 68 65 20 73 63 68 65 6d  to set the schem
2cd15 61 20 6f 66 20 61 20 76 69 72 74 75 61 6c 20 74  a of a virtual t
2cd16 61 62 6c 65 2e 20 20 49 74 20 69 73 20 6f 6e 6c  able.  It is onl
2cd17 79 0a 2a 2a 20 76 61 6c 69 64 20 74 6f 20 63 61  y.** valid to ca
2cd18 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ll this function
2cd19 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65   from within the
2cd1a 20 78 43 72 65 61 74 65 28 29 20 6f 72 20 78 43   xCreate() or xC
2cd1b 6f 6e 6e 65 63 74 28 29 20 6f 66 20 61 0a 2a 2a  onnect() of a.**
2cd1c 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d   virtual table m
2cd1d 6f 64 75 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  odule..*/.SQLITE
2cd1e 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
2cd1f 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 28 73 71  _declare_vtab(sq
2cd20 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74  lite3 *db, const
2cd21 20 63 68 61 72 20 2a 7a 43 72 65 61 74 65 54 61   char *zCreateTa
2cd22 62 6c 65 29 7b 0a 20 20 50 61 72 73 65 20 2a 70  ble){.  Parse *p
2cd23 50 61 72 73 65 3b 0a 0a 20 20 69 6e 74 20 72 63  Parse;..  int rc
2cd24 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
2cd25 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 63  Table *pTab;.  c
2cd26 68 61 72 20 2a 7a 45 72 72 20 3d 20 30 3b 0a 0a  har *zErr = 0;..
2cd27 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
2cd28 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
2cd29 3b 0a 20 20 70 54 61 62 20 3d 20 64 62 2d 3e 70  ;.  pTab = db->p
2cd2a 56 54 61 62 3b 0a 20 20 69 66 28 20 21 70 54 61  VTab;.  if( !pTa
2cd2b 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  b ){.    sqlite3
2cd2c 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45  Error(db, SQLITE
2cd2d 5f 4d 49 53 55 53 45 2c 20 30 29 3b 0a 20 20 20  _MISUSE, 0);.   
2cd2e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
2cd2f 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
2cd30 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
2cd31 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20  TE_MISUSE;.  }. 
2cd32 20 61 73 73 65 72 74 28 20 28 70 54 61 62 2d 3e   assert( (pTab->
2cd33 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 56 69  tabFlags & TF_Vi
2cd34 72 74 75 61 6c 29 21 3d 30 20 29 3b 0a 0a 20 20  rtual)!=0 );..  
2cd35 70 50 61 72 73 65 20 3d 20 73 71 6c 69 74 65 33  pParse = sqlite3
2cd36 53 74 61 63 6b 41 6c 6c 6f 63 5a 65 72 6f 28 64  StackAllocZero(d
2cd37 62 2c 20 73 69 7a 65 6f 66 28 2a 70 50 61 72 73  b, sizeof(*pPars
2cd38 65 29 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73  e));.  if( pPars
2cd39 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d  e==0 ){.    rc =
2cd3a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
2cd3b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 72   }else{.    pPar
2cd3c 73 65 2d 3e 64 65 63 6c 61 72 65 56 74 61 62 20  se->declareVtab 
2cd3d 3d 20 31 3b 0a 20 20 20 20 70 50 61 72 73 65 2d  = 1;.    pParse-
2cd3e 3e 64 62 20 3d 20 64 62 3b 0a 20 20 0a 20 20 20  >db = db;.  .   
2cd3f 20 69 66 28 20 0a 20 20 20 20 20 20 20 20 53 51   if( .        SQ
2cd40 4c 49 54 45 5f 4f 4b 20 3d 3d 20 73 71 6c 69 74  LITE_OK == sqlit
2cd41 65 33 52 75 6e 50 61 72 73 65 72 28 70 50 61 72  e3RunParser(pPar
2cd42 73 65 2c 20 7a 43 72 65 61 74 65 54 61 62 6c 65  se, zCreateTable
2cd43 2c 20 26 7a 45 72 72 29 20 26 26 20 0a 20 20 20  , &zErr) && .   
2cd44 20 20 20 20 20 70 50 61 72 73 65 2d 3e 70 4e 65       pParse->pNe
2cd45 77 54 61 62 6c 65 20 26 26 20 0a 20 20 20 20 20  wTable && .     
2cd46 20 20 20 21 70 50 61 72 73 65 2d 3e 70 4e 65 77     !pParse->pNew
2cd47 54 61 62 6c 65 2d 3e 70 53 65 6c 65 63 74 20 26  Table->pSelect &
2cd48 26 20 0a 20 20 20 20 20 20 20 20 28 70 50 61 72  & .        (pPar
2cd49 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2d 3e 74  se->pNewTable->t
2cd4a 61 62 46 6c 61 67 73 20 26 20 54 46 5f 56 69 72  abFlags & TF_Vir
2cd4b 74 75 61 6c 29 3d 3d 30 0a 20 20 20 20 29 7b 0a  tual)==0.    ){.
2cd4c 20 20 20 20 20 20 69 66 28 20 21 70 54 61 62 2d        if( !pTab-
2cd4d 3e 61 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20  >aCol ){.       
2cd4e 20 70 54 61 62 2d 3e 61 43 6f 6c 20 3d 20 70 50   pTab->aCol = pP
2cd4f 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2d  arse->pNewTable-
2cd50 3e 61 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70  >aCol;.        p
2cd51 54 61 62 2d 3e 6e 43 6f 6c 20 3d 20 70 50 61 72  Tab->nCol = pPar
2cd52 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2d 3e 6e  se->pNewTable->n
2cd53 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 50 61  Col;.        pPa
2cd54 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2d 3e  rse->pNewTable->
2cd55 6e 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20  nCol = 0;.      
2cd56 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61    pParse->pNewTa
2cd57 62 6c 65 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20  ble->aCol = 0;. 
2cd58 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 62 2d       }.      db-
2cd59 3e 70 56 54 61 62 20 3d 20 30 3b 0a 20 20 20 20  >pVTab = 0;.    
2cd5a 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 73  } else {.      s
2cd5b 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20  qlite3Error(db, 
2cd5c 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20 7a 45  SQLITE_ERROR, zE
2cd5d 72 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  rr);.      sqlit
2cd5e 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72  e3DbFree(db, zEr
2cd5f 72 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53  r);.      rc = S
2cd60 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
2cd61 20 7d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 64   }.    pParse->d
2cd62 65 63 6c 61 72 65 56 74 61 62 20 3d 20 30 3b 0a  eclareVtab = 0;.
2cd63 20 20 0a 20 20 20 20 69 66 28 20 70 50 61 72 73    .    if( pPars
2cd64 65 2d 3e 70 56 64 62 65 20 29 7b 0a 20 20 20 20  e->pVdbe ){.    
2cd65 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e    sqlite3VdbeFin
2cd66 61 6c 69 7a 65 28 70 50 61 72 73 65 2d 3e 70 56  alize(pParse->pV
2cd67 64 62 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  dbe);.    }.    
2cd68 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62  sqlite3DeleteTab
2cd69 6c 65 28 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  le(pParse->pNewT
2cd6a 61 62 6c 65 29 3b 0a 20 20 20 20 73 71 6c 69 74  able);.    sqlit
2cd6b 65 33 53 74 61 63 6b 46 72 65 65 28 64 62 2c 20  e3StackFree(db, 
2cd6c 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a 0a 20 20  pParse);.  }..  
2cd6d 61 73 73 65 72 74 28 20 28 72 63 26 30 78 66 66  assert( (rc&0xff
2cd6e 29 3d 3d 72 63 20 29 3b 0a 20 20 72 63 20 3d 20  )==rc );.  rc = 
2cd6f 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64  sqlite3ApiExit(d
2cd70 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69 74 65  b, rc);.  sqlite
2cd71 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
2cd72 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
2cd73 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
2cd74 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
2cd75 20 69 6e 76 6f 6b 65 64 20 62 79 20 74 68 65 20   invoked by the 
2cd76 76 64 62 65 20 74 6f 20 63 61 6c 6c 20 74 68 65  vdbe to call the
2cd77 20 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f 64   xDestroy method
2cd78 0a 2a 2a 20 6f 66 20 74 68 65 20 76 69 72 74 75  .** of the virtu
2cd79 61 6c 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 7a  al table named z
2cd7a 54 61 62 20 69 6e 20 64 61 74 61 62 61 73 65 20  Tab in database 
2cd7b 69 44 62 2e 20 54 68 69 73 20 6f 63 63 75 72 73  iDb. This occurs
2cd7c 0a 2a 2a 20 77 68 65 6e 20 61 20 44 52 4f 50 20  .** when a DROP 
2cd7d 54 41 42 4c 45 20 69 73 20 6d 65 6e 74 69 6f 6e  TABLE is mention
2cd7e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63  ed..**.** This c
2cd7f 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69  all is a no-op i
2cd80 66 20 7a 54 61 62 20 69 73 20 6e 6f 74 20 61 20  f zTab is not a 
2cd81 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a  virtual table..*
2cd82 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2cd83 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62   int sqlite3Vtab
2cd84 43 61 6c 6c 44 65 73 74 72 6f 79 28 73 71 6c 69  CallDestroy(sqli
2cd85 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62  te3 *db, int iDb
2cd86 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54  , const char *zT
2cd87 61 62 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ab){.  int rc = 
2cd88 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 54 61 62  SQLITE_OK;.  Tab
2cd89 6c 65 20 2a 70 54 61 62 3b 0a 0a 20 20 70 54 61  le *pTab;..  pTa
2cd8a 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54  b = sqlite3FindT
2cd8b 61 62 6c 65 28 64 62 2c 20 7a 54 61 62 2c 20 64  able(db, zTab, d
2cd8c 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
2cd8d 65 29 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53  e);.  if( ALWAYS
2cd8e 28 70 54 61 62 21 3d 30 20 26 26 20 70 54 61 62  (pTab!=0 && pTab
2cd8f 2d 3e 70 56 54 61 62 6c 65 21 3d 30 29 20 29 7b  ->pVTable!=0) ){
2cd90 0a 20 20 20 20 56 54 61 62 6c 65 20 2a 70 20 3d  .    VTable *p =
2cd91 20 76 74 61 62 44 69 73 63 6f 6e 6e 65 63 74 41   vtabDisconnectA
2cd92 6c 6c 28 64 62 2c 20 70 54 61 62 29 3b 0a 0a 20  ll(db, pTab);.. 
2cd93 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53     rc = sqlite3S
2cd94 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20  afetyOff(db);.  
2cd95 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
2cd96 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 72  LITE_OK );.    r
2cd97 63 20 3d 20 70 2d 3e 70 4d 6f 64 2d 3e 70 4d 6f  c = p->pMod->pMo
2cd98 64 75 6c 65 2d 3e 78 44 65 73 74 72 6f 79 28 70  dule->xDestroy(p
2cd99 2d 3e 70 56 74 61 62 29 3b 0a 20 20 20 20 28 76  ->pVtab);.    (v
2cd9a 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74  oid)sqlite3Safet
2cd9b 79 4f 6e 28 64 62 29 3b 0a 0a 20 20 20 20 2f 2a  yOn(db);..    /*
2cd9c 20 52 65 6d 6f 76 65 20 74 68 65 20 73 71 6c 69   Remove the sqli
2cd9d 74 65 33 5f 76 74 61 62 2a 20 66 72 6f 6d 20 74  te3_vtab* from t
2cd9e 68 65 20 61 56 54 72 61 6e 73 5b 5d 20 61 72 72  he aVTrans[] arr
2cd9f 61 79 2c 20 69 66 20 61 70 70 6c 69 63 61 62 6c  ay, if applicabl
2cda0 65 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d  e */.    if( rc=
2cda1 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
2cda2 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62      assert( pTab
2cda3 2d 3e 70 56 54 61 62 6c 65 3d 3d 70 20 26 26 20  ->pVTable==p && 
2cda4 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 29 3b 0a 20  p->pNext==0 );. 
2cda5 20 20 20 20 20 70 2d 3e 70 56 74 61 62 20 3d 20       p->pVtab = 
2cda6 30 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 70  0;.      pTab->p
2cda7 56 54 61 62 6c 65 20 3d 20 30 3b 0a 20 20 20 20  VTable = 0;.    
2cda8 20 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c    sqlite3VtabUnl
2cda9 6f 63 6b 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20  ock(p);.    }.  
2cdaa 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  }..  return rc;.
2cdab 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
2cdac 6e 63 74 69 6f 6e 20 69 6e 76 6f 6b 65 73 20 65  nction invokes e
2cdad 69 74 68 65 72 20 74 68 65 20 78 52 6f 6c 6c 62  ither the xRollb
2cdae 61 63 6b 20 6f 72 20 78 43 6f 6d 6d 69 74 20 6d  ack or xCommit m
2cdaf 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 65 61 63 68  ethod.** of each
2cdb0 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20   of the virtual 
2cdb1 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 73 71  tables in the sq
2cdb2 6c 69 74 65 33 2e 61 56 54 72 61 6e 73 20 61 72  lite3.aVTrans ar
2cdb3 72 61 79 2e 20 54 68 65 20 6d 65 74 68 6f 64 0a  ray. The method.
2cdb4 2a 2a 20 63 61 6c 6c 65 64 20 69 73 20 69 64 65  ** called is ide
2cdb5 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 20 73  ntified by the s
2cdb6 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20  econd argument, 
2cdb7 22 6f 66 66 73 65 74 22 2c 20 77 68 69 63 68 20  "offset", which 
2cdb8 69 73 0a 2a 2a 20 74 68 65 20 6f 66 66 73 65 74  is.** the offset
2cdb9 20 6f 66 20 74 68 65 20 6d 65 74 68 6f 64 20 74   of the method t
2cdba 6f 20 63 61 6c 6c 20 69 6e 20 74 68 65 20 73 71  o call in the sq
2cdbb 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 73 74 72  lite3_module str
2cdbc 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  ucture..**.** Th
2cdbd 65 20 61 72 72 61 79 20 69 73 20 63 6c 65 61 72  e array is clear
2cdbe 65 64 20 61 66 74 65 72 20 69 6e 76 6f 6b 69 6e  ed after invokin
2cdbf 67 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 73 2e  g the callbacks.
2cdc0 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64   .*/.static void
2cdc1 20 63 61 6c 6c 46 69 6e 61 6c 69 73 65 72 28 73   callFinaliser(s
2cdc2 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
2cdc3 6f 66 66 73 65 74 29 7b 0a 20 20 69 6e 74 20 69  offset){.  int i
2cdc4 3b 0a 20 20 69 66 28 20 64 62 2d 3e 61 56 54 72  ;.  if( db->aVTr
2cdc5 61 6e 73 20 29 7b 0a 20 20 20 20 66 6f 72 28 69  ans ){.    for(i
2cdc6 3d 30 3b 20 69 3c 64 62 2d 3e 6e 56 54 72 61 6e  =0; i<db->nVTran
2cdc7 73 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 56  s; i++){.      V
2cdc8 54 61 62 6c 65 20 2a 70 56 54 61 62 20 3d 20 64  Table *pVTab = d
2cdc9 62 2d 3e 61 56 54 72 61 6e 73 5b 69 5d 3b 0a 20  b->aVTrans[i];. 
2cdca 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61       sqlite3_vta
2cdcb 62 20 2a 70 20 3d 20 70 56 54 61 62 2d 3e 70 56  b *p = pVTab->pV
2cdcc 74 61 62 3b 0a 20 20 20 20 20 20 69 66 28 20 70  tab;.      if( p
2cdcd 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   ){.        int 
2cdce 28 2a 78 29 28 73 71 6c 69 74 65 33 5f 76 74 61  (*x)(sqlite3_vta
2cdcf 62 20 2a 29 3b 0a 20 20 20 20 20 20 20 20 78 20  b *);.        x 
2cdd0 3d 20 2a 28 69 6e 74 20 28 2a 2a 29 28 73 71 6c  = *(int (**)(sql
2cdd1 69 74 65 33 5f 76 74 61 62 20 2a 29 29 28 28 63  ite3_vtab *))((c
2cdd2 68 61 72 20 2a 29 70 2d 3e 70 4d 6f 64 75 6c 65  har *)p->pModule
2cdd3 20 2b 20 6f 66 66 73 65 74 29 3b 0a 20 20 20 20   + offset);.    
2cdd4 20 20 20 20 69 66 28 20 78 20 29 20 78 28 70 29      if( x ) x(p)
2cdd5 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2cdd6 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63  sqlite3VtabUnloc
2cdd7 6b 28 70 56 54 61 62 29 3b 0a 20 20 20 20 7d 0a  k(pVTab);.    }.
2cdd8 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2cdd9 65 28 64 62 2c 20 64 62 2d 3e 61 56 54 72 61 6e  e(db, db->aVTran
2cdda 73 29 3b 0a 20 20 20 20 64 62 2d 3e 6e 56 54 72  s);.    db->nVTr
2cddb 61 6e 73 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d  ans = 0;.    db-
2cddc 3e 61 56 54 72 61 6e 73 20 3d 20 30 3b 0a 20 20  >aVTrans = 0;.  
2cddd 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b  }.}../*.** Invok
2cdde 65 20 74 68 65 20 78 53 79 6e 63 20 6d 65 74 68  e the xSync meth
2cddf 6f 64 20 6f 66 20 61 6c 6c 20 76 69 72 74 75 61  od of all virtua
2cde0 6c 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20  l tables in the 
2cde1 73 71 6c 69 74 65 33 2e 61 56 54 72 61 6e 73 0a  sqlite3.aVTrans.
2cde2 2a 2a 20 61 72 72 61 79 2e 20 52 65 74 75 72 6e  ** array. Return
2cde3 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20   the error code 
2cde4 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 65 72  for the first er
2cde5 72 6f 72 20 74 68 61 74 20 6f 63 63 75 72 73 2c  ror that occurs,
2cde6 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b   or.** SQLITE_OK
2cde7 20 69 66 20 61 6c 6c 20 78 53 79 6e 63 20 6f 70   if all xSync op
2cde8 65 72 61 74 69 6f 6e 73 20 61 72 65 20 73 75 63  erations are suc
2cde9 63 65 73 73 66 75 6c 2e 0a 2a 2a 0a 2a 2a 20 53  cessful..**.** S
2cdea 65 74 20 2a 70 7a 45 72 72 6d 73 67 20 74 6f 20  et *pzErrmsg to 
2cdeb 70 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65  point to a buffe
2cdec 72 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65  r that should be
2cded 20 72 65 6c 65 61 73 65 64 20 75 73 69 6e 67 20   released using 
2cdee 0a 2a 2a 20 73 71 6c 69 74 65 33 44 62 46 72 65  .** sqlite3DbFre
2cdef 65 28 29 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61  e() containing a
2cdf0 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2c  n error message,
2cdf1 20 69 66 20 6f 6e 65 20 69 73 20 61 76 61 69 6c   if one is avail
2cdf2 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  able..*/.SQLITE_
2cdf3 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
2cdf4 74 65 33 56 74 61 62 53 79 6e 63 28 73 71 6c 69  te3VtabSync(sqli
2cdf5 74 65 33 20 2a 64 62 2c 20 63 68 61 72 20 2a 2a  te3 *db, char **
2cdf6 70 7a 45 72 72 6d 73 67 29 7b 0a 20 20 69 6e 74  pzErrmsg){.  int
2cdf7 20 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53   i;.  int rc = S
2cdf8 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20  QLITE_OK;.  int 
2cdf9 72 63 73 61 66 65 74 79 3b 0a 20 20 56 54 61 62  rcsafety;.  VTab
2cdfa 6c 65 20 2a 2a 61 56 54 72 61 6e 73 20 3d 20 64  le **aVTrans = d
2cdfb 62 2d 3e 61 56 54 72 61 6e 73 3b 0a 0a 20 20 72  b->aVTrans;..  r
2cdfc 63 20 3d 20 73 71 6c 69 74 65 33 53 61 66 65 74  c = sqlite3Safet
2cdfd 79 4f 66 66 28 64 62 29 3b 0a 20 20 64 62 2d 3e  yOff(db);.  db->
2cdfe 61 56 54 72 61 6e 73 20 3d 20 30 3b 0a 20 20 66  aVTrans = 0;.  f
2cdff 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49  or(i=0; rc==SQLI
2ce00 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e  TE_OK && i<db->n
2ce01 56 54 72 61 6e 73 3b 20 69 2b 2b 29 7b 0a 20 20  VTrans; i++){.  
2ce02 20 20 69 6e 74 20 28 2a 78 29 28 73 71 6c 69 74    int (*x)(sqlit
2ce03 65 33 5f 76 74 61 62 20 2a 29 3b 0a 20 20 20 20  e3_vtab *);.    
2ce04 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
2ce05 74 61 62 20 3d 20 61 56 54 72 61 6e 73 5b 69 5d  tab = aVTrans[i]
2ce06 2d 3e 70 56 74 61 62 3b 0a 20 20 20 20 69 66 28  ->pVtab;.    if(
2ce07 20 70 56 74 61 62 20 26 26 20 28 78 20 3d 20 70   pVtab && (x = p
2ce08 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78  Vtab->pModule->x
2ce09 53 79 6e 63 29 21 3d 30 20 29 7b 0a 20 20 20 20  Sync)!=0 ){.    
2ce0a 20 20 72 63 20 3d 20 78 28 70 56 74 61 62 29 3b    rc = x(pVtab);
2ce0b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
2ce0c 46 72 65 65 28 64 62 2c 20 2a 70 7a 45 72 72 6d  Free(db, *pzErrm
2ce0d 73 67 29 3b 0a 20 20 20 20 20 20 2a 70 7a 45 72  sg);.      *pzEr
2ce0e 72 6d 73 67 20 3d 20 70 56 74 61 62 2d 3e 7a 45  rmsg = pVtab->zE
2ce0f 72 72 4d 73 67 3b 0a 20 20 20 20 20 20 70 56 74  rrMsg;.      pVt
2ce10 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b  ab->zErrMsg = 0;
2ce11 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 64 62 2d  .    }.  }.  db-
2ce12 3e 61 56 54 72 61 6e 73 20 3d 20 61 56 54 72 61  >aVTrans = aVTra
2ce13 6e 73 3b 0a 20 20 72 63 73 61 66 65 74 79 20 3d  ns;.  rcsafety =
2ce14 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e   sqlite3SafetyOn
2ce15 28 64 62 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d  (db);..  if( rc=
2ce16 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
2ce17 20 20 72 63 20 3d 20 72 63 73 61 66 65 74 79 3b    rc = rcsafety;
2ce18 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
2ce19 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b  ;.}../*.** Invok
2ce1a 65 20 74 68 65 20 78 52 6f 6c 6c 62 61 63 6b 20  e the xRollback 
2ce1b 6d 65 74 68 6f 64 20 6f 66 20 61 6c 6c 20 76 69  method of all vi
2ce1c 72 74 75 61 6c 20 74 61 62 6c 65 73 20 69 6e 20  rtual tables in 
2ce1d 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 65 33 2e  the .** sqlite3.
2ce1e 61 56 54 72 61 6e 73 20 61 72 72 61 79 2e 20 54  aVTrans array. T
2ce1f 68 65 6e 20 63 6c 65 61 72 20 74 68 65 20 61 72  hen clear the ar
2ce20 72 61 79 20 69 74 73 65 6c 66 2e 0a 2a 2f 0a 53  ray itself..*/.S
2ce21 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
2ce22 74 20 73 71 6c 69 74 65 33 56 74 61 62 52 6f 6c  t sqlite3VtabRol
2ce23 6c 62 61 63 6b 28 73 71 6c 69 74 65 33 20 2a 64  lback(sqlite3 *d
2ce24 62 29 7b 0a 20 20 63 61 6c 6c 46 69 6e 61 6c 69  b){.  callFinali
2ce25 73 65 72 28 64 62 2c 20 6f 66 66 73 65 74 6f 66  ser(db, offsetof
2ce26 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 2c  (sqlite3_module,
2ce27 78 52 6f 6c 6c 62 61 63 6b 29 29 3b 0a 20 20 72  xRollback));.  r
2ce28 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2ce29 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65  .}../*.** Invoke
2ce2a 20 74 68 65 20 78 43 6f 6d 6d 69 74 20 6d 65 74   the xCommit met
2ce2b 68 6f 64 20 6f 66 20 61 6c 6c 20 76 69 72 74 75  hod of all virtu
2ce2c 61 6c 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65  al tables in the
2ce2d 20 0a 2a 2a 20 73 71 6c 69 74 65 33 2e 61 56 54   .** sqlite3.aVT
2ce2e 72 61 6e 73 20 61 72 72 61 79 2e 20 54 68 65 6e  rans array. Then
2ce2f 20 63 6c 65 61 72 20 74 68 65 20 61 72 72 61 79   clear the array
2ce30 20 69 74 73 65 6c 66 2e 0a 2a 2f 0a 53 51 4c 49   itself..*/.SQLI
2ce31 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
2ce32 71 6c 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74  qlite3VtabCommit
2ce33 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
2ce34 20 63 61 6c 6c 46 69 6e 61 6c 69 73 65 72 28 64   callFinaliser(d
2ce35 62 2c 20 6f 66 66 73 65 74 6f 66 28 73 71 6c 69  b, offsetof(sqli
2ce36 74 65 33 5f 6d 6f 64 75 6c 65 2c 78 43 6f 6d 6d  te3_module,xComm
2ce37 69 74 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  it));.  return S
2ce38 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
2ce39 2a 2a 20 49 66 20 74 68 65 20 76 69 72 74 75 61  ** If the virtua
2ce3a 6c 20 74 61 62 6c 65 20 70 56 74 61 62 20 73 75  l table pVtab su
2ce3b 70 70 6f 72 74 73 20 74 68 65 20 74 72 61 6e 73  pports the trans
2ce3c 61 63 74 69 6f 6e 20 69 6e 74 65 72 66 61 63 65  action interface
2ce3d 0a 2a 2a 20 28 78 42 65 67 69 6e 2f 78 52 6f 6c  .** (xBegin/xRol
2ce3e 6c 62 61 63 6b 2f 78 43 6f 6d 6d 69 74 20 61 6e  lback/xCommit an
2ce3f 64 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 78 53 79  d optionally xSy
2ce40 6e 63 29 20 61 6e 64 20 61 20 74 72 61 6e 73 61  nc) and a transa
2ce41 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 6e 6f 74 20  ction is.** not 
2ce42 63 75 72 72 65 6e 74 6c 79 20 6f 70 65 6e 2c 20  currently open, 
2ce43 69 6e 76 6f 6b 65 20 74 68 65 20 78 42 65 67 69  invoke the xBegi
2ce44 6e 20 6d 65 74 68 6f 64 20 6e 6f 77 2e 0a 2a 2a  n method now..**
2ce45 0a 2a 2a 20 49 66 20 74 68 65 20 78 42 65 67 69  .** If the xBegi
2ce46 6e 20 63 61 6c 6c 20 69 73 20 73 75 63 63 65 73  n call is succes
2ce47 73 66 75 6c 2c 20 70 6c 61 63 65 20 74 68 65 20  sful, place the 
2ce48 73 71 6c 69 74 65 33 5f 76 74 61 62 20 70 6f 69  sqlite3_vtab poi
2ce49 6e 74 65 72 0a 2a 2a 20 69 6e 20 74 68 65 20 73  nter.** in the s
2ce4a 71 6c 69 74 65 33 2e 61 56 54 72 61 6e 73 20 61  qlite3.aVTrans a
2ce4b 72 72 61 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rray..*/.SQLITE_
2ce4c 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
2ce4d 74 65 33 56 74 61 62 42 65 67 69 6e 28 73 71 6c  te3VtabBegin(sql
2ce4e 69 74 65 33 20 2a 64 62 2c 20 56 54 61 62 6c 65  ite3 *db, VTable
2ce4f 20 2a 70 56 54 61 62 29 7b 0a 20 20 69 6e 74 20   *pVTab){.  int 
2ce50 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
2ce51 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
2ce52 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b  module *pModule;
2ce53 0a 0a 20 20 2f 2a 20 53 70 65 63 69 61 6c 20 63  ..  /* Special c
2ce54 61 73 65 3a 20 49 66 20 64 62 2d 3e 61 56 54 72  ase: If db->aVTr
2ce55 61 6e 73 20 69 73 20 4e 55 4c 4c 20 61 6e 64 20  ans is NULL and 
2ce56 64 62 2d 3e 6e 56 54 72 61 6e 73 20 69 73 20 67  db->nVTrans is g
2ce57 72 65 61 74 65 72 0a 20 20 2a 2a 20 74 68 61 6e  reater.  ** than
2ce58 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 69 73   zero, then this
2ce59 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 62 65 69   function is bei
2ce5a 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77  ng called from w
2ce5b 69 74 68 69 6e 20 61 0a 20 20 2a 2a 20 76 69 72  ithin a.  ** vir
2ce5c 74 75 61 6c 20 6d 6f 64 75 6c 65 20 78 53 79 6e  tual module xSyn
2ce5d 63 28 29 20 63 61 6c 6c 62 61 63 6b 2e 20 49 74  c() callback. It
2ce5e 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 77   is illegal to w
2ce5f 72 69 74 65 20 74 6f 20 0a 20 20 2a 2a 20 76 69  rite to .  ** vi
2ce60 72 74 75 61 6c 20 6d 6f 64 75 6c 65 20 74 61 62  rtual module tab
2ce61 6c 65 73 20 69 6e 20 74 68 69 73 20 63 61 73 65  les in this case
2ce62 2c 20 73 6f 20 72 65 74 75 72 6e 20 53 51 4c 49  , so return SQLI
2ce63 54 45 5f 4c 4f 43 4b 45 44 2e 0a 20 20 2a 2f 0a  TE_LOCKED..  */.
2ce64 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 74 61    if( sqlite3Vta
2ce65 62 49 6e 53 79 6e 63 28 64 62 29 20 29 7b 0a 20  bInSync(db) ){. 
2ce66 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
2ce67 5f 4c 4f 43 4b 45 44 3b 0a 20 20 7d 0a 20 20 69  _LOCKED;.  }.  i
2ce68 66 28 20 21 70 56 54 61 62 20 29 7b 0a 20 20 20  f( !pVTab ){.   
2ce69 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
2ce6a 4b 3b 0a 20 20 7d 20 0a 20 20 70 4d 6f 64 75 6c  K;.  } .  pModul
2ce6b 65 20 3d 20 70 56 54 61 62 2d 3e 70 56 74 61 62  e = pVTab->pVtab
2ce6c 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20 69 66  ->pModule;..  if
2ce6d 28 20 70 4d 6f 64 75 6c 65 2d 3e 78 42 65 67 69  ( pModule->xBegi
2ce6e 6e 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  n ){.    int i;.
2ce6f 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70 56 74 61  ..    /* If pVta
2ce70 62 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20  b is already in 
2ce71 74 68 65 20 61 56 54 72 61 6e 73 20 61 72 72 61  the aVTrans arra
2ce72 79 2c 20 72 65 74 75 72 6e 20 65 61 72 6c 79 20  y, return early 
2ce73 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  */.    for(i=0; 
2ce74 69 3c 64 62 2d 3e 6e 56 54 72 61 6e 73 3b 20 69  i<db->nVTrans; i
2ce75 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 64  ++){.      if( d
2ce76 62 2d 3e 61 56 54 72 61 6e 73 5b 69 5d 3d 3d 70  b->aVTrans[i]==p
2ce77 56 54 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20  VTab ){.        
2ce78 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
2ce79 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2ce7a 0a 20 20 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74  .    /* Invoke t
2ce7b 68 65 20 78 42 65 67 69 6e 20 6d 65 74 68 6f 64  he xBegin method
2ce7c 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 4d 6f   */.    rc = pMo
2ce7d 64 75 6c 65 2d 3e 78 42 65 67 69 6e 28 70 56 54  dule->xBegin(pVT
2ce7e 61 62 2d 3e 70 56 74 61 62 29 3b 0a 20 20 20 20  ab->pVtab);.    
2ce7f 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
2ce80 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  K ){.      rc = 
2ce81 61 64 64 54 6f 56 54 72 61 6e 73 28 64 62 2c 20  addToVTrans(db, 
2ce82 70 56 54 61 62 29 3b 0a 20 20 20 20 7d 0a 20 20  pVTab);.    }.  
2ce83 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
2ce84 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73  ../*.** The firs
2ce85 74 20 70 61 72 61 6d 65 74 65 72 20 28 70 44 65  t parameter (pDe
2ce86 66 29 20 69 73 20 61 20 66 75 6e 63 74 69 6f 6e  f) is a function
2ce87 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e   implementation.
2ce88 20 20 54 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20    The.** second 
2ce89 70 61 72 61 6d 65 74 65 72 20 28 70 45 78 70 72  parameter (pExpr
2ce8a 29 20 69 73 20 74 68 65 20 66 69 72 73 74 20 61  ) is the first a
2ce8b 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20  rgument to this 
2ce8c 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 49 66 20  function..** If 
2ce8d 70 45 78 70 72 20 69 73 20 61 20 63 6f 6c 75 6d  pExpr is a colum
2ce8e 6e 20 69 6e 20 61 20 76 69 72 74 75 61 6c 20 74  n in a virtual t
2ce8f 61 62 6c 65 2c 20 74 68 65 6e 20 6c 65 74 20 74  able, then let t
2ce90 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61  he virtual.** ta
2ce91 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ble implementati
2ce92 6f 6e 20 68 61 76 65 20 61 6e 20 6f 70 70 6f 72  on have an oppor
2ce93 74 75 6e 69 74 79 20 74 6f 20 6f 76 65 72 6c 6f  tunity to overlo
2ce94 61 64 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e  ad the function.
2ce95 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
2ce96 69 6e 65 20 69 73 20 75 73 65 64 20 74 6f 20 61  ine is used to a
2ce97 6c 6c 6f 77 20 76 69 72 74 75 61 6c 20 74 61 62  llow virtual tab
2ce98 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  le implementatio
2ce99 6e 73 20 74 6f 0a 2a 2a 20 6f 76 65 72 6c 6f 61  ns to.** overloa
2ce9a 64 20 4d 41 54 43 48 2c 20 4c 49 4b 45 2c 20 47  d MATCH, LIKE, G
2ce9b 4c 4f 42 2c 20 61 6e 64 20 52 45 47 45 58 50 20  LOB, and REGEXP 
2ce9c 6f 70 65 72 61 74 6f 72 73 2e 0a 2a 2a 0a 2a 2a  operators..**.**
2ce9d 20 52 65 74 75 72 6e 20 65 69 74 68 65 72 20 74   Return either t
2ce9e 68 65 20 70 44 65 66 20 61 72 67 75 6d 65 6e 74  he pDef argument
2ce9f 20 28 69 6e 64 69 63 61 74 69 6e 67 20 6e 6f 20   (indicating no 
2cea0 63 68 61 6e 67 65 29 20 6f 72 20 61 20 0a 2a 2a  change) or a .**
2cea1 20 6e 65 77 20 46 75 6e 63 44 65 66 20 73 74 72   new FuncDef str
2cea2 75 63 74 75 72 65 20 74 68 61 74 20 69 73 20 6d  ucture that is m
2cea3 61 72 6b 65 64 20 61 73 20 65 70 68 65 6d 65 72  arked as ephemer
2cea4 61 6c 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20  al using the.** 
2cea5 53 51 4c 49 54 45 5f 46 55 4e 43 5f 45 50 48 45  SQLITE_FUNC_EPHE
2cea6 4d 20 66 6c 61 67 2e 0a 2a 2f 0a 53 51 4c 49 54  M flag..*/.SQLIT
2cea7 45 5f 50 52 49 56 41 54 45 20 46 75 6e 63 44 65  E_PRIVATE FuncDe
2cea8 66 20 2a 73 71 6c 69 74 65 33 56 74 61 62 4f 76  f *sqlite3VtabOv
2cea9 65 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e 28 0a  erloadFunction(.
2ceaa 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
2ceab 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f    /* Database co
2ceac 6e 6e 65 63 74 69 6f 6e 20 66 6f 72 20 72 65 70  nnection for rep
2cead 6f 72 74 69 6e 67 20 6d 61 6c 6c 6f 63 20 70 72  orting malloc pr
2ceae 6f 62 6c 65 6d 73 20 2a 2f 0a 20 20 46 75 6e 63  oblems */.  Func
2ceaf 44 65 66 20 2a 70 44 65 66 2c 20 20 2f 2a 20 46  Def *pDef,  /* F
2ceb0 75 6e 63 74 69 6f 6e 20 74 6f 20 70 6f 73 73 69  unction to possi
2ceb1 62 6c 79 20 6f 76 65 72 6c 6f 61 64 20 2a 2f 0a  bly overload */.
2ceb2 20 20 69 6e 74 20 6e 41 72 67 2c 20 20 20 20 20    int nArg,     
2ceb3 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61    /* Number of a
2ceb4 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20  rguments to the 
2ceb5 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 45 78  function */.  Ex
2ceb6 70 72 20 2a 70 45 78 70 72 20 20 20 20 20 2f 2a  pr *pExpr     /*
2ceb7 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   First argument 
2ceb8 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  to the function 
2ceb9 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70  */.){.  Table *p
2ceba 54 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76  Tab;.  sqlite3_v
2cebb 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 73 71  tab *pVtab;.  sq
2cebc 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d  lite3_module *pM
2cebd 6f 64 3b 0a 20 20 76 6f 69 64 20 28 2a 78 46 75  od;.  void (*xFu
2cebe 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  nc)(sqlite3_cont
2cebf 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
2cec0 5f 76 61 6c 75 65 2a 2a 29 20 3d 20 30 3b 0a 20  _value**) = 0;. 
2cec1 20 76 6f 69 64 20 2a 70 41 72 67 20 3d 20 30 3b   void *pArg = 0;
2cec2 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 4e 65 77  .  FuncDef *pNew
2cec3 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a  ;.  int rc = 0;.
2cec4 20 20 63 68 61 72 20 2a 7a 4c 6f 77 65 72 4e 61    char *zLowerNa
2cec5 6d 65 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  me;.  unsigned c
2cec6 68 61 72 20 2a 7a 3b 0a 0a 0a 20 20 2f 2a 20 43  har *z;...  /* C
2cec7 68 65 63 6b 20 74 6f 20 73 65 65 20 74 68 65 20  heck to see the 
2cec8 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 69 73 20  left operand is 
2cec9 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 76 69  a column in a vi
2ceca 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20  rtual table */. 
2cecb 20 69 66 28 20 4e 45 56 45 52 28 70 45 78 70 72   if( NEVER(pExpr
2cecc 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 20 70 44  ==0) ) return pD
2cecd 65 66 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d  ef;.  if( pExpr-
2cece 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29  >op!=TK_COLUMN )
2cecf 20 72 65 74 75 72 6e 20 70 44 65 66 3b 0a 20 20   return pDef;.  
2ced0 70 54 61 62 20 3d 20 70 45 78 70 72 2d 3e 70 54  pTab = pExpr->pT
2ced1 61 62 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28  ab;.  if( NEVER(
2ced2 70 54 61 62 3d 3d 30 29 20 29 20 72 65 74 75 72  pTab==0) ) retur
2ced3 6e 20 70 44 65 66 3b 0a 20 20 69 66 28 20 28 70  n pDef;.  if( (p
2ced4 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20  Tab->tabFlags & 
2ced5 54 46 5f 56 69 72 74 75 61 6c 29 3d 3d 30 20 29  TF_Virtual)==0 )
2ced6 20 72 65 74 75 72 6e 20 70 44 65 66 3b 0a 20 20   return pDef;.  
2ced7 70 56 74 61 62 20 3d 20 73 71 6c 69 74 65 33 47  pVtab = sqlite3G
2ced8 65 74 56 54 61 62 6c 65 28 64 62 2c 20 70 54 61  etVTable(db, pTa
2ced9 62 29 2d 3e 70 56 74 61 62 3b 0a 20 20 61 73 73  b)->pVtab;.  ass
2ceda 65 72 74 28 20 70 56 74 61 62 21 3d 30 20 29 3b  ert( pVtab!=0 );
2cedb 0a 20 20 61 73 73 65 72 74 28 20 70 56 74 61 62  .  assert( pVtab
2cedc 2d 3e 70 4d 6f 64 75 6c 65 21 3d 30 20 29 3b 0a  ->pModule!=0 );.
2cedd 20 20 70 4d 6f 64 20 3d 20 28 73 71 6c 69 74 65    pMod = (sqlite
2cede 33 5f 6d 6f 64 75 6c 65 20 2a 29 70 56 74 61 62  3_module *)pVtab
2cedf 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 69 66 28  ->pModule;.  if(
2cee0 20 70 4d 6f 64 2d 3e 78 46 69 6e 64 46 75 6e 63   pMod->xFindFunc
2cee1 74 69 6f 6e 3d 3d 30 20 29 20 72 65 74 75 72 6e  tion==0 ) return
2cee2 20 70 44 65 66 3b 0a 20 0a 20 20 2f 2a 20 43 61   pDef;. .  /* Ca
2cee3 6c 6c 20 74 68 65 20 78 46 69 6e 64 46 75 6e 63  ll the xFindFunc
2cee4 74 69 6f 6e 20 6d 65 74 68 6f 64 20 6f 6e 20 74  tion method on t
2cee5 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
2cee6 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a   implementation.
2cee7 20 20 2a 2a 20 74 6f 20 73 65 65 20 69 66 20 74    ** to see if t
2cee8 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
2cee9 6e 20 77 61 6e 74 73 20 74 6f 20 6f 76 65 72 6c  n wants to overl
2ceea 6f 61 64 20 74 68 69 73 20 66 75 6e 63 74 69 6f  oad this functio
2ceeb 6e 20 0a 20 20 2a 2f 0a 20 20 7a 4c 6f 77 65 72  n .  */.  zLower
2ceec 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62  Name = sqlite3Db
2ceed 53 74 72 44 75 70 28 64 62 2c 20 70 44 65 66 2d  StrDup(db, pDef-
2ceee 3e 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a  >zName);.  if( z
2ceef 4c 6f 77 65 72 4e 61 6d 65 20 29 7b 0a 20 20 20  LowerName ){.   
2cef0 20 66 6f 72 28 7a 3d 28 75 6e 73 69 67 6e 65 64   for(z=(unsigned
2cef1 20 63 68 61 72 2a 29 7a 4c 6f 77 65 72 4e 61 6d   char*)zLowerNam
2cef2 65 3b 20 2a 7a 3b 20 7a 2b 2b 29 7b 0a 20 20 20  e; *z; z++){.   
2cef3 20 20 20 2a 7a 20 3d 20 73 71 6c 69 74 65 33 55     *z = sqlite3U
2cef4 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 7a 5d 3b  pperToLower[*z];
2cef5 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20  .    }.    rc = 
2cef6 70 4d 6f 64 2d 3e 78 46 69 6e 64 46 75 6e 63 74  pMod->xFindFunct
2cef7 69 6f 6e 28 70 56 74 61 62 2c 20 6e 41 72 67 2c  ion(pVtab, nArg,
2cef8 20 7a 4c 6f 77 65 72 4e 61 6d 65 2c 20 26 78 46   zLowerName, &xF
2cef9 75 6e 63 2c 20 26 70 41 72 67 29 3b 0a 20 20 20  unc, &pArg);.   
2cefa 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2cefb 62 2c 20 7a 4c 6f 77 65 72 4e 61 6d 65 29 3b 0a  b, zLowerName);.
2cefc 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 30 20    }.  if( rc==0 
2cefd 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 44  ){.    return pD
2cefe 65 66 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 72  ef;.  }..  /* Cr
2ceff 65 61 74 65 20 61 20 6e 65 77 20 65 70 68 65 6d  eate a new ephem
2cf00 65 72 61 6c 20 66 75 6e 63 74 69 6f 6e 20 64 65  eral function de
2cf01 66 69 6e 69 74 69 6f 6e 20 66 6f 72 20 74 68 65  finition for the
2cf02 20 6f 76 65 72 6c 6f 61 64 65 64 0a 20 20 2a 2a   overloaded.  **
2cf03 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 70   function */.  p
2cf04 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  New = sqlite3DbM
2cf05 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  allocZero(db, si
2cf06 7a 65 6f 66 28 2a 70 4e 65 77 29 0a 20 20 20 20  zeof(*pNew).    
2cf07 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cf08 20 20 20 20 20 20 20 20 20 2b 20 73 71 6c 69 74           + sqlit
2cf09 65 33 53 74 72 6c 65 6e 33 30 28 70 44 65 66 2d  e3Strlen30(pDef-
2cf0a 3e 7a 4e 61 6d 65 29 20 2b 20 31 29 3b 0a 20 20  >zName) + 1);.  
2cf0b 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20  if( pNew==0 ){. 
2cf0c 20 20 20 72 65 74 75 72 6e 20 70 44 65 66 3b 0a     return pDef;.
2cf0d 20 20 7d 0a 20 20 2a 70 4e 65 77 20 3d 20 2a 70    }.  *pNew = *p
2cf0e 44 65 66 3b 0a 20 20 70 4e 65 77 2d 3e 7a 4e 61  Def;.  pNew->zNa
2cf0f 6d 65 20 3d 20 28 63 68 61 72 20 2a 29 26 70 4e  me = (char *)&pN
2cf10 65 77 5b 31 5d 3b 0a 20 20 6d 65 6d 63 70 79 28  ew[1];.  memcpy(
2cf11 70 4e 65 77 2d 3e 7a 4e 61 6d 65 2c 20 70 44 65  pNew->zName, pDe
2cf12 66 2d 3e 7a 4e 61 6d 65 2c 20 73 71 6c 69 74 65  f->zName, sqlite
2cf13 33 53 74 72 6c 65 6e 33 30 28 70 44 65 66 2d 3e  3Strlen30(pDef->
2cf14 7a 4e 61 6d 65 29 2b 31 29 3b 0a 20 20 70 4e 65  zName)+1);.  pNe
2cf15 77 2d 3e 78 46 75 6e 63 20 3d 20 78 46 75 6e 63  w->xFunc = xFunc
2cf16 3b 0a 20 20 70 4e 65 77 2d 3e 70 55 73 65 72 44  ;.  pNew->pUserD
2cf17 61 74 61 20 3d 20 70 41 72 67 3b 0a 20 20 70 4e  ata = pArg;.  pN
2cf18 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c  ew->flags |= SQL
2cf19 49 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d 3b 0a  ITE_FUNC_EPHEM;.
2cf1a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d    return pNew;.}
2cf1b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72  ../*.** Make sur
2cf1c 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
2cf1d 70 54 61 62 20 69 73 20 63 6f 6e 74 61 69 6e 65  pTab is containe
2cf1e 64 20 69 6e 20 74 68 65 20 70 50 61 72 73 65 2d  d in the pParse-
2cf1f 3e 61 70 56 69 72 74 75 61 6c 4c 6f 63 6b 5b 5d  >apVirtualLock[]
2cf20 0a 2a 2a 20 61 72 72 61 79 20 73 6f 20 74 68 61  .** array so tha
2cf21 74 20 61 6e 20 4f 50 5f 56 42 65 67 69 6e 20 77  t an OP_VBegin w
2cf22 69 6c 6c 20 67 65 74 20 67 65 6e 65 72 61 74 65  ill get generate
2cf23 64 20 66 6f 72 20 69 74 2e 20 20 41 64 64 20 70  d for it.  Add p
2cf24 54 61 62 20 74 6f 20 74 68 65 0a 2a 2a 20 61 72  Tab to the.** ar
2cf25 72 61 79 20 69 66 20 69 74 20 69 73 20 6d 69 73  ray if it is mis
2cf26 73 69 6e 67 2e 20 20 49 66 20 70 54 61 62 20 69  sing.  If pTab i
2cf27 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65  s already in the
2cf28 20 61 72 72 61 79 2c 20 74 68 69 73 20 72 6f 75   array, this rou
2cf29 74 69 6e 65 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d  tine.** is a no-
2cf2a 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  op..*/.SQLITE_PR
2cf2b 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2cf2c 65 33 56 74 61 62 4d 61 6b 65 57 72 69 74 61 62  e3VtabMakeWritab
2cf2d 6c 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  le(Parse *pParse
2cf2e 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a  , Table *pTab){.
2cf2f 20 20 50 61 72 73 65 20 2a 70 54 6f 70 6c 65 76    Parse *pToplev
2cf30 65 6c 20 3d 20 73 71 6c 69 74 65 33 50 61 72 73  el = sqlite3Pars
2cf31 65 54 6f 70 6c 65 76 65 6c 28 70 50 61 72 73 65  eToplevel(pParse
2cf32 29 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20  );.  int i, n;. 
2cf33 20 54 61 62 6c 65 20 2a 2a 61 70 56 74 61 62 4c   Table **apVtabL
2cf34 6f 63 6b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  ock;..  assert( 
2cf35 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
2cf36 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
2cf37 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e 56 74 61 62  pToplevel->nVtab
2cf38 4c 6f 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Lock; i++){.    
2cf39 69 66 28 20 70 54 61 62 3d 3d 70 54 6f 70 6c 65  if( pTab==pTople
2cf3a 76 65 6c 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 5b  vel->apVtabLock[
2cf3b 69 5d 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7d  i] ) return;.  }
2cf3c 0a 20 20 6e 20 3d 20 28 70 54 6f 70 6c 65 76 65  .  n = (pTopleve
2cf3d 6c 2d 3e 6e 56 74 61 62 4c 6f 63 6b 2b 31 29 2a  l->nVtabLock+1)*
2cf3e 73 69 7a 65 6f 66 28 70 54 6f 70 6c 65 76 65 6c  sizeof(pToplevel
2cf3f 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 5b 30 5d 29  ->apVtabLock[0])
2cf40 3b 0a 20 20 61 70 56 74 61 62 4c 6f 63 6b 20 3d  ;.  apVtabLock =
2cf41 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
2cf42 28 70 54 6f 70 6c 65 76 65 6c 2d 3e 61 70 56 74  (pToplevel->apVt
2cf43 61 62 4c 6f 63 6b 2c 20 6e 29 3b 0a 20 20 69 66  abLock, n);.  if
2cf44 28 20 61 70 56 74 61 62 4c 6f 63 6b 20 29 7b 0a  ( apVtabLock ){.
2cf45 20 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 61      pToplevel->a
2cf46 70 56 74 61 62 4c 6f 63 6b 20 3d 20 61 70 56 74  pVtabLock = apVt
2cf47 61 62 4c 6f 63 6b 3b 0a 20 20 20 20 70 54 6f 70  abLock;.    pTop
2cf48 6c 65 76 65 6c 2d 3e 61 70 56 74 61 62 4c 6f 63  level->apVtabLoc
2cf49 6b 5b 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e 56 74  k[pToplevel->nVt
2cf4a 61 62 4c 6f 63 6b 2b 2b 5d 20 3d 20 70 54 61 62  abLock++] = pTab
2cf4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
2cf4c 54 6f 70 6c 65 76 65 6c 2d 3e 64 62 2d 3e 6d 61  Toplevel->db->ma
2cf4d 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
2cf4e 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a    }.}..#endif /*
2cf4f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
2cf50 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a  TUALTABLE */../*
2cf51 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
2cf52 64 20 6f 66 20 76 74 61 62 2e 63 20 2a 2a 2a 2a  d of vtab.c ****
2cf53 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cf54 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cf55 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
2cf56 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
2cf57 67 69 6e 20 66 69 6c 65 20 77 68 65 72 65 2e 63  gin file where.c
2cf58 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2cf59 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cf5a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
2cf5b 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
2cf5c 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 15.**.** The 
2cf5d 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
2cf5e 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
2cf5f 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
2cf60 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
2cf61 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
2cf62 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
2cf63 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
2cf64 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
2cf65 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
2cf66 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
2cf67 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
2cf68 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
2cf69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
2cf6a 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
2cf6b 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
2cf6c 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
2cf6d 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
2cf6e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cf6f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cf70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cf71 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cf72 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 6f 64  ****.** This mod
2cf73 75 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63  ule contains C c
2cf74 6f 64 65 20 74 68 61 74 20 67 65 6e 65 72 61 74  ode that generat
2cf75 65 73 20 56 44 42 45 20 63 6f 64 65 20 75 73 65  es VDBE code use
2cf76 64 20 74 6f 20 70 72 6f 63 65 73 73 0a 2a 2a 20  d to process.** 
2cf77 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2cf78 20 6f 66 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   of SQL statemen
2cf79 74 73 2e 20 20 54 68 69 73 20 6d 6f 64 75 6c 65  ts.  This module
2cf7a 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20   is responsible 
2cf7b 66 6f 72 0a 2a 2a 20 67 65 6e 65 72 61 74 69 6e  for.** generatin
2cf7c 67 20 74 68 65 20 63 6f 64 65 20 74 68 61 74 20  g the code that 
2cf7d 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 61 20  loops through a 
2cf7e 74 61 62 6c 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f  table looking fo
2cf7f 72 20 61 70 70 6c 69 63 61 62 6c 65 0a 2a 2a 20  r applicable.** 
2cf80 72 6f 77 73 2e 20 20 49 6e 64 69 63 65 73 20 61  rows.  Indices a
2cf81 72 65 20 73 65 6c 65 63 74 65 64 20 61 6e 64 20  re selected and 
2cf82 75 73 65 64 20 74 6f 20 73 70 65 65 64 20 74 68  used to speed th
2cf83 65 20 73 65 61 72 63 68 20 77 68 65 6e 20 64 6f  e search when do
2cf84 69 6e 67 0a 2a 2a 20 73 6f 20 69 73 20 61 70 70  ing.** so is app
2cf85 6c 69 63 61 62 6c 65 2e 20 20 42 65 63 61 75 73  licable.  Becaus
2cf86 65 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 73  e this module is
2cf87 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72   responsible for
2cf88 20 73 65 6c 65 63 74 69 6e 67 0a 2a 2a 20 69 6e   selecting.** in
2cf89 64 69 63 65 73 2c 20 79 6f 75 20 6d 69 67 68 74  dices, you might
2cf8a 20 61 6c 73 6f 20 74 68 69 6e 6b 20 6f 66 20 74   also think of t
2cf8b 68 69 73 20 6d 6f 64 75 6c 65 20 61 73 20 74 68  his module as th
2cf8c 65 20 22 71 75 65 72 79 20 6f 70 74 69 6d 69 7a  e "query optimiz
2cf8d 65 72 22 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  er"..**.** $Id: 
2cf8e 77 68 65 72 65 2e 63 2c 76 20 31 2e 34 31 31 20  where.c,v 1.411 
2cf8f 32 30 30 39 2f 30 37 2f 33 31 20 30 36 3a 31 34  2009/07/31 06:14
2cf90 3a 35 32 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :52 danielk1977 
2cf91 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  Exp $.*/../*.** 
2cf92 54 72 61 63 65 20 6f 75 74 70 75 74 20 6d 61 63  Trace output mac
2cf93 72 6f 73 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e  ros.*/.#if defin
2cf94 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20  ed(SQLITE_TEST) 
2cf95 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  || defined(SQLIT
2cf96 45 5f 44 45 42 55 47 29 0a 53 51 4c 49 54 45 5f  E_DEBUG).SQLITE_
2cf97 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
2cf98 74 65 33 57 68 65 72 65 54 72 61 63 65 20 3d 20  te3WhereTrace = 
2cf99 30 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 64 65  0;.#endif.#if de
2cf9a 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53  fined(SQLITE_TES
2cf9b 54 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51  T) && defined(SQ
2cf9c 4c 49 54 45 5f 44 45 42 55 47 29 0a 23 20 64 65  LITE_DEBUG).# de
2cf9d 66 69 6e 65 20 57 48 45 52 45 54 52 41 43 45 28  fine WHERETRACE(
2cf9e 58 29 20 20 69 66 28 73 71 6c 69 74 65 33 57 68  X)  if(sqlite3Wh
2cf9f 65 72 65 54 72 61 63 65 29 20 73 71 6c 69 74 65  ereTrace) sqlite
2cfa0 33 44 65 62 75 67 50 72 69 6e 74 66 20 58 0a 23  3DebugPrintf X.#
2cfa1 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 57 48  else.# define WH
2cfa2 45 52 45 54 52 41 43 45 28 58 29 0a 23 65 6e 64  ERETRACE(X).#end
2cfa3 69 66 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72  if../* Forward r
2cfa4 65 66 65 72 65 6e 63 65 0a 2a 2f 0a 74 79 70 65  eference.*/.type
2cfa5 64 65 66 20 73 74 72 75 63 74 20 57 68 65 72 65  def struct Where
2cfa6 43 6c 61 75 73 65 20 57 68 65 72 65 43 6c 61 75  Clause WhereClau
2cfa7 73 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  se;.typedef stru
2cfa8 63 74 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20  ct WhereMaskSet 
2cfa9 57 68 65 72 65 4d 61 73 6b 53 65 74 3b 0a 74 79  WhereMaskSet;.ty
2cfaa 70 65 64 65 66 20 73 74 72 75 63 74 20 57 68 65  pedef struct Whe
2cfab 72 65 4f 72 49 6e 66 6f 20 57 68 65 72 65 4f 72  reOrInfo WhereOr
2cfac 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20 73 74  Info;.typedef st
2cfad 72 75 63 74 20 57 68 65 72 65 41 6e 64 49 6e 66  ruct WhereAndInf
2cfae 6f 20 57 68 65 72 65 41 6e 64 49 6e 66 6f 3b 0a  o WhereAndInfo;.
2cfaf 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 57  typedef struct W
2cfb0 68 65 72 65 43 6f 73 74 20 57 68 65 72 65 43 6f  hereCost WhereCo
2cfb1 73 74 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 71  st;../*.** The q
2cfb2 75 65 72 79 20 67 65 6e 65 72 61 74 6f 72 20 75  uery generator u
2cfb3 73 65 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20  ses an array of 
2cfb4 69 6e 73 74 61 6e 63 65 73 20 6f 66 20 74 68 69  instances of thi
2cfb5 73 20 73 74 72 75 63 74 75 72 65 20 74 6f 0a 2a  s structure to.*
2cfb6 2a 20 68 65 6c 70 20 69 74 20 61 6e 61 6c 79 7a  * help it analyz
2cfb7 65 20 74 68 65 20 73 75 62 65 78 70 72 65 73 73  e the subexpress
2cfb8 69 6f 6e 73 20 6f 66 20 74 68 65 20 57 48 45 52  ions of the WHER
2cfb9 45 20 63 6c 61 75 73 65 2e 20 20 45 61 63 68 20  E clause.  Each 
2cfba 57 48 45 52 45 0a 2a 2a 20 63 6c 61 75 73 65 20  WHERE.** clause 
2cfbb 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20 69 73  subexpression is
2cfbc 20 73 65 70 61 72 61 74 65 64 20 66 72 6f 6d 20   separated from 
2cfbd 74 68 65 20 6f 74 68 65 72 73 20 62 79 20 41 4e  the others by AN
2cfbe 44 20 6f 70 65 72 61 74 6f 72 73 2c 0a 2a 2a 20  D operators,.** 
2cfbf 75 73 75 61 6c 6c 79 2c 20 6f 72 20 73 6f 6d 65  usually, or some
2cfc0 74 69 6d 65 73 20 73 75 62 65 78 70 72 65 73 73  times subexpress
2cfc1 69 6f 6e 73 20 73 65 70 61 72 61 74 65 64 20 62  ions separated b
2cfc2 79 20 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20  y OR..**.** All 
2cfc3 57 68 65 72 65 54 65 72 6d 73 20 61 72 65 20 63  WhereTerms are c
2cfc4 6f 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20 61 20  ollected into a 
2cfc5 73 69 6e 67 6c 65 20 57 68 65 72 65 43 6c 61 75  single WhereClau
2cfc6 73 65 20 73 74 72 75 63 74 75 72 65 2e 20 20 0a  se structure.  .
2cfc7 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
2cfc8 20 69 64 65 6e 74 69 74 79 20 68 6f 6c 64 73 3a   identity holds:
2cfc9 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 57 68  .**.**        Wh
2cfca 65 72 65 54 65 72 6d 2e 70 57 43 2d 3e 61 5b 57  ereTerm.pWC->a[W
2cfcb 68 65 72 65 54 65 72 6d 2e 69 64 78 5d 20 3d 3d  hereTerm.idx] ==
2cfcc 20 57 68 65 72 65 54 65 72 6d 0a 2a 2a 0a 2a 2a   WhereTerm.**.**
2cfcd 20 57 68 65 6e 20 61 20 74 65 72 6d 20 69 73 20   When a term is 
2cfce 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a  of the form:.**.
2cfcf 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
2cfd0 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 0a 2a 2a  X <op> <expr>.**
2cfd1 0a 2a 2a 20 77 68 65 72 65 20 58 20 69 73 20 61  .** where X is a
2cfd2 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 61 6e 64   column name and
2cfd3 20 3c 6f 70 3e 20 69 73 20 6f 6e 65 20 6f 66 20   <op> is one of 
2cfd4 63 65 72 74 61 69 6e 20 6f 70 65 72 61 74 6f 72  certain operator
2cfd5 73 2c 0a 2a 2a 20 74 68 65 6e 20 57 68 65 72 65  s,.** then Where
2cfd6 54 65 72 6d 2e 6c 65 66 74 43 75 72 73 6f 72 20  Term.leftCursor 
2cfd7 61 6e 64 20 57 68 65 72 65 54 65 72 6d 2e 75 2e  and WhereTerm.u.
2cfd8 6c 65 66 74 43 6f 6c 75 6d 6e 20 72 65 63 6f 72  leftColumn recor
2cfd9 64 20 74 68 65 0a 2a 2a 20 63 75 72 73 6f 72 20  d the.** cursor 
2cfda 6e 75 6d 62 65 72 20 61 6e 64 20 63 6f 6c 75 6d  number and colum
2cfdb 6e 20 6e 75 6d 62 65 72 20 66 6f 72 20 58 2e 20  n number for X. 
2cfdc 20 57 68 65 72 65 54 65 72 6d 2e 65 4f 70 65 72   WhereTerm.eOper
2cfdd 61 74 6f 72 20 72 65 63 6f 72 64 73 0a 2a 2a 20  ator records.** 
2cfde 74 68 65 20 3c 6f 70 3e 20 75 73 69 6e 67 20 61  the <op> using a
2cfdf 20 62 69 74 6d 61 73 6b 20 65 6e 63 6f 64 69 6e   bitmask encodin
2cfe0 67 20 64 65 66 69 6e 65 64 20 62 79 20 57 4f 5f  g defined by WO_
2cfe1 78 78 78 20 62 65 6c 6f 77 2e 20 20 54 68 65 0a  xxx below.  The.
2cfe2 2a 2a 20 75 73 65 20 6f 66 20 61 20 62 69 74 6d  ** use of a bitm
2cfe3 61 73 6b 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72  ask encoding for
2cfe4 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20 61 6c   the operator al
2cfe5 6c 6f 77 73 20 75 73 20 74 6f 20 73 65 61 72 63  lows us to searc
2cfe6 68 0a 2a 2a 20 71 75 69 63 6b 6c 79 20 66 6f 72  h.** quickly for
2cfe7 20 74 65 72 6d 73 20 74 68 61 74 20 6d 61 74 63   terms that matc
2cfe8 68 20 61 6e 79 20 6f 66 20 73 65 76 65 72 61 6c  h any of several
2cfe9 20 64 69 66 66 65 72 65 6e 74 20 6f 70 65 72 61   different opera
2cfea 74 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 57 68  tors..**.** A Wh
2cfeb 65 72 65 54 65 72 6d 20 6d 69 67 68 74 20 61 6c  ereTerm might al
2cfec 73 6f 20 62 65 20 74 77 6f 20 6f 72 20 6d 6f 72  so be two or mor
2cfed 65 20 73 75 62 74 65 72 6d 73 20 63 6f 6e 6e 65  e subterms conne
2cfee 63 74 65 64 20 62 79 20 4f 52 3a 0a 2a 2a 0a 2a  cted by OR:.**.*
2cfef 2a 20 20 20 20 20 20 20 20 20 28 74 31 2e 58 20  *         (t1.X 
2cff0 3c 6f 70 3e 20 3c 65 78 70 72 3e 29 20 4f 52 20  <op> <expr>) OR 
2cff1 28 74 31 2e 59 20 3c 6f 70 3e 20 3c 65 78 70 72  (t1.Y <op> <expr
2cff2 3e 29 20 4f 52 20 2e 2e 2e 2e 0a 2a 2a 0a 2a 2a  >) OR .....**.**
2cff3 20 49 6e 20 74 68 69 73 20 73 65 63 6f 6e 64 20   In this second 
2cff4 63 61 73 65 2c 20 77 74 46 6c 61 67 20 61 73 20  case, wtFlag as 
2cff5 74 68 65 20 54 45 52 4d 5f 4f 52 49 4e 46 4f 20  the TERM_ORINFO 
2cff6 73 65 74 20 61 6e 64 20 65 4f 70 65 72 61 74 6f  set and eOperato
2cff7 72 3d 3d 57 4f 5f 4f 52 0a 2a 2a 20 61 6e 64 20  r==WO_OR.** and 
2cff8 74 68 65 20 57 68 65 72 65 54 65 72 6d 2e 75 2e  the WhereTerm.u.
2cff9 70 4f 72 49 6e 66 6f 20 66 69 65 6c 64 20 70 6f  pOrInfo field po
2cffa 69 6e 74 73 20 74 6f 20 61 75 78 69 6c 69 61 72  ints to auxiliar
2cffb 79 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68  y information th
2cffc 61 74 0a 2a 2a 20 69 73 20 63 6f 6c 6c 65 63 74  at.** is collect
2cffd 65 64 20 61 62 6f 75 74 20 74 68 65 0a 2a 2a 0a  ed about the.**.
2cffe 2a 2a 20 49 66 20 61 20 74 65 72 6d 20 69 6e 20  ** If a term in 
2cfff 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2d000 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20   does not match 
2d001 65 69 74 68 65 72 20 6f 66 20 74 68 65 20 74 77  either of the tw
2d002 6f 20 70 72 65 76 69 6f 75 73 0a 2a 2a 20 63 61  o previous.** ca
2d003 74 65 67 6f 72 69 65 73 2c 20 74 68 65 6e 20 65  tegories, then e
2d004 4f 70 65 72 61 74 6f 72 3d 3d 30 2e 20 20 54 68  Operator==0.  Th
2d005 65 20 57 68 65 72 65 54 65 72 6d 2e 70 45 78 70  e WhereTerm.pExp
2d006 72 20 66 69 65 6c 64 20 69 73 20 73 74 69 6c 6c  r field is still
2d007 20 73 65 74 0a 2a 2a 20 74 6f 20 74 68 65 20 6f   set.** to the o
2d008 72 69 67 69 6e 61 6c 20 73 75 62 65 78 70 72 65  riginal subexpre
2d009 73 73 69 6f 6e 20 63 6f 6e 74 65 6e 74 20 61 6e  ssion content an
2d00a 64 20 77 74 46 6c 61 67 73 20 69 73 20 73 65 74  d wtFlags is set
2d00b 20 75 70 20 61 70 70 72 6f 70 72 69 61 74 65 6c   up appropriatel
2d00c 79 0a 2a 2a 20 62 75 74 20 6e 6f 20 6f 74 68 65  y.** but no othe
2d00d 72 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20  r fields in the 
2d00e 57 68 65 72 65 54 65 72 6d 20 6f 62 6a 65 63 74  WhereTerm object
2d00f 20 61 72 65 20 6d 65 61 6e 69 6e 67 66 75 6c 2e   are meaningful.
2d010 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 65 4f 70 65  .**.** When eOpe
2d011 72 61 74 6f 72 21 3d 30 2c 20 70 72 65 72 65 71  rator!=0, prereq
2d012 52 69 67 68 74 20 61 6e 64 20 70 72 65 72 65 71  Right and prereq
2d013 41 6c 6c 20 72 65 63 6f 72 64 20 73 65 74 73 20  All record sets 
2d014 6f 66 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  of cursor number
2d015 73 2c 0a 2a 2a 20 62 75 74 20 74 68 65 79 20 64  s,.** but they d
2d016 6f 20 73 6f 20 69 6e 64 69 72 65 63 74 6c 79 2e  o so indirectly.
2d017 20 20 41 20 73 69 6e 67 6c 65 20 57 68 65 72 65    A single Where
2d018 4d 61 73 6b 53 65 74 20 73 74 72 75 63 74 75 72  MaskSet structur
2d019 65 20 74 72 61 6e 73 6c 61 74 65 73 0a 2a 2a 20  e translates.** 
2d01a 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 69 6e  cursor number in
2d01b 74 6f 20 62 69 74 73 20 61 6e 64 20 74 68 65 20  to bits and the 
2d01c 74 72 61 6e 73 6c 61 74 65 64 20 62 69 74 20 69  translated bit i
2d01d 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  s stored in the 
2d01e 70 72 65 72 65 71 0a 2a 2a 20 66 69 65 6c 64 73  prereq.** fields
2d01f 2e 20 20 54 68 65 20 74 72 61 6e 73 6c 61 74 69  .  The translati
2d020 6f 6e 20 69 73 20 75 73 65 64 20 69 6e 20 6f 72  on is used in or
2d021 64 65 72 20 74 6f 20 6d 61 78 69 6d 69 7a 65 20  der to maximize 
2d022 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a  the number of.**
2d023 20 62 69 74 73 20 74 68 61 74 20 77 69 6c 6c 20   bits that will 
2d024 66 69 74 20 69 6e 20 61 20 42 69 74 6d 61 73 6b  fit in a Bitmask
2d025 2e 20 20 54 68 65 20 56 44 42 45 20 63 75 72 73  .  The VDBE curs
2d026 6f 72 20 6e 75 6d 62 65 72 73 20 6d 69 67 68 74  or numbers might
2d027 20 62 65 0a 2a 2a 20 73 70 72 65 61 64 20 6f 75   be.** spread ou
2d028 74 20 6f 76 65 72 20 74 68 65 20 6e 6f 6e 2d 6e  t over the non-n
2d029 65 67 61 74 69 76 65 20 69 6e 74 65 67 65 72 73  egative integers
2d02a 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  .  For example, 
2d02b 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 6e 75  the cursor.** nu
2d02c 6d 62 65 72 73 20 6d 69 67 68 74 20 62 65 20 33  mbers might be 3
2d02d 2c 20 38 2c 20 39 2c 20 31 30 2c 20 32 30 2c 20  , 8, 9, 10, 20, 
2d02e 32 33 2c 20 34 31 2c 20 61 6e 64 20 34 35 2e 20  23, 41, and 45. 
2d02f 20 54 68 65 20 57 68 65 72 65 4d 61 73 6b 53 65   The WhereMaskSe
2d030 74 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 65 73 20  t.** translates 
2d031 74 68 65 73 65 20 73 70 61 72 73 65 20 63 75 72  these sparse cur
2d032 73 6f 72 20 6e 75 6d 62 65 72 73 20 69 6e 74 6f  sor numbers into
2d033 20 63 6f 6e 73 65 63 75 74 69 76 65 20 69 6e 74   consecutive int
2d034 65 67 65 72 73 0a 2a 2a 20 62 65 67 69 6e 6e 69  egers.** beginni
2d035 6e 67 20 77 69 74 68 20 30 20 69 6e 20 6f 72 64  ng with 0 in ord
2d036 65 72 20 74 6f 20 6d 61 6b 65 20 74 68 65 20 62  er to make the b
2d037 65 73 74 20 70 6f 73 73 69 62 6c 65 20 75 73 65  est possible use
2d038 20 6f 66 20 74 68 65 20 61 76 61 69 6c 61 62 6c   of the availabl
2d039 65 0a 2a 2a 20 62 69 74 73 20 69 6e 20 74 68 65  e.** bits in the
2d03a 20 42 69 74 6d 61 73 6b 2e 20 20 53 6f 2c 20 69   Bitmask.  So, i
2d03b 6e 20 74 68 65 20 65 78 61 6d 70 6c 65 20 61 62  n the example ab
2d03c 6f 76 65 2c 20 74 68 65 20 63 75 72 73 6f 72 20  ove, the cursor 
2d03d 6e 75 6d 62 65 72 73 0a 2a 2a 20 77 6f 75 6c 64  numbers.** would
2d03e 20 62 65 20 6d 61 70 70 65 64 20 69 6e 74 6f 20   be mapped into 
2d03f 69 6e 74 65 67 65 72 73 20 30 20 74 68 72 6f 75  integers 0 throu
2d040 67 68 20 37 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  gh 7..**.** The 
2d041 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20  number of terms 
2d042 69 6e 20 61 20 6a 6f 69 6e 20 69 73 20 6c 69 6d  in a join is lim
2d043 69 74 65 64 20 62 79 20 74 68 65 20 6e 75 6d 62  ited by the numb
2d044 65 72 20 6f 66 20 62 69 74 73 0a 2a 2a 20 69 6e  er of bits.** in
2d045 20 70 72 65 72 65 71 52 69 67 68 74 20 61 6e 64   prereqRight and
2d046 20 70 72 65 72 65 71 41 6c 6c 2e 20 20 54 68 65   prereqAll.  The
2d047 20 64 65 66 61 75 6c 74 20 69 73 20 36 34 20 62   default is 64 b
2d048 69 74 73 2c 20 68 65 6e 63 65 20 53 51 4c 69 74  its, hence SQLit
2d049 65 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 61 62 6c  e.** is only abl
2d04a 65 20 74 6f 20 70 72 6f 63 65 73 73 20 6a 6f 69  e to process joi
2d04b 6e 73 20 77 69 74 68 20 36 34 20 6f 72 20 66 65  ns with 64 or fe
2d04c 77 65 72 20 74 61 62 6c 65 73 2e 0a 2a 2f 0a 74  wer tables..*/.t
2d04d 79 70 65 64 65 66 20 73 74 72 75 63 74 20 57 68  ypedef struct Wh
2d04e 65 72 65 54 65 72 6d 20 57 68 65 72 65 54 65 72  ereTerm WhereTer
2d04f 6d 3b 0a 73 74 72 75 63 74 20 57 68 65 72 65 54  m;.struct WhereT
2d050 65 72 6d 20 7b 0a 20 20 45 78 70 72 20 2a 70 45  erm {.  Expr *pE
2d051 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  xpr;            
2d052 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68  /* Pointer to th
2d053 65 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20  e subexpression 
2d054 74 68 61 74 20 69 73 20 74 68 69 73 20 74 65 72  that is this ter
2d055 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 72 65  m */.  int iPare
2d056 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  nt;            /
2d057 2a 20 44 69 73 61 62 6c 65 20 70 57 43 2d 3e 61  * Disable pWC->a
2d058 5b 69 50 61 72 65 6e 74 5d 20 77 68 65 6e 20 74  [iParent] when t
2d059 68 69 73 20 74 65 72 6d 20 64 69 73 61 62 6c 65  his term disable
2d05a 64 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 66 74 43  d */.  int leftC
2d05b 75 72 73 6f 72 3b 20 20 20 20 20 20 20 20 20 2f  ursor;         /
2d05c 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  * Cursor number 
2d05d 6f 66 20 58 20 69 6e 20 22 58 20 3c 6f 70 3e 20  of X in "X <op> 
2d05e 3c 65 78 70 72 3e 22 20 2a 2f 0a 20 20 75 6e 69  <expr>" */.  uni
2d05f 6f 6e 20 7b 0a 20 20 20 20 69 6e 74 20 6c 65 66  on {.    int lef
2d060 74 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20  tColumn;        
2d061 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65   /* Column numbe
2d062 72 20 6f 66 20 58 20 69 6e 20 22 58 20 3c 6f 70  r of X in "X <op
2d063 3e 20 3c 65 78 70 72 3e 22 20 2a 2f 0a 20 20 20  > <expr>" */.   
2d064 20 57 68 65 72 65 4f 72 49 6e 66 6f 20 2a 70 4f   WhereOrInfo *pO
2d065 72 49 6e 66 6f 3b 20 20 20 2f 2a 20 45 78 74 72  rInfo;   /* Extr
2d066 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 66  a information if
2d067 20 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 4f   eOperator==WO_O
2d068 52 20 2a 2f 0a 20 20 20 20 57 68 65 72 65 41 6e  R */.    WhereAn
2d069 64 49 6e 66 6f 20 2a 70 41 6e 64 49 6e 66 6f 3b  dInfo *pAndInfo;
2d06a 20 2f 2a 20 45 78 74 72 61 20 69 6e 66 6f 72 6d   /* Extra inform
2d06b 61 74 69 6f 6e 20 69 66 20 65 4f 70 65 72 61 74  ation if eOperat
2d06c 6f 72 3d 3d 57 4f 5f 41 4e 44 20 2a 2f 0a 20 20  or==WO_AND */.  
2d06d 7d 20 75 3b 0a 20 20 75 31 36 20 65 4f 70 65 72  } u;.  u16 eOper
2d06e 61 74 6f 72 3b 20 20 20 20 20 20 20 20 20 20 2f  ator;          /
2d06f 2a 20 41 20 57 4f 5f 78 78 20 76 61 6c 75 65 20  * A WO_xx value 
2d070 64 65 73 63 72 69 62 69 6e 67 20 3c 6f 70 3e 20  describing <op> 
2d071 2a 2f 0a 20 20 75 38 20 77 74 46 6c 61 67 73 3b  */.  u8 wtFlags;
2d072 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2d073 54 45 52 4d 5f 78 78 78 20 62 69 74 20 66 6c 61  TERM_xxx bit fla
2d074 67 73 2e 20 20 53 65 65 20 62 65 6c 6f 77 20 2a  gs.  See below *
2d075 2f 0a 20 20 75 38 20 6e 43 68 69 6c 64 3b 20 20  /.  u8 nChild;  
2d076 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
2d077 75 6d 62 65 72 20 6f 66 20 63 68 69 6c 64 72 65  umber of childre
2d078 6e 20 74 68 61 74 20 6d 75 73 74 20 64 69 73 61  n that must disa
2d079 62 6c 65 20 75 73 20 2a 2f 0a 20 20 57 68 65 72  ble us */.  Wher
2d07a 65 43 6c 61 75 73 65 20 2a 70 57 43 3b 20 20 20  eClause *pWC;   
2d07b 20 20 20 20 2f 2a 20 54 68 65 20 63 6c 61 75 73      /* The claus
2d07c 65 20 74 68 69 73 20 74 65 72 6d 20 69 73 20 70  e this term is p
2d07d 61 72 74 20 6f 66 20 2a 2f 0a 20 20 42 69 74 6d  art of */.  Bitm
2d07e 61 73 6b 20 70 72 65 72 65 71 52 69 67 68 74 3b  ask prereqRight;
2d07f 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f      /* Bitmask o
2d080 66 20 74 61 62 6c 65 73 20 75 73 65 64 20 62 79  f tables used by
2d081 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 20 2a   pExpr->pRight *
2d082 2f 0a 20 20 42 69 74 6d 61 73 6b 20 70 72 65 72  /.  Bitmask prer
2d083 65 71 41 6c 6c 3b 20 20 20 20 20 20 2f 2a 20 42  eqAll;      /* B
2d084 69 74 6d 61 73 6b 20 6f 66 20 74 61 62 6c 65 73  itmask of tables
2d085 20 72 65 66 65 72 65 6e 63 65 64 20 62 79 20 70   referenced by p
2d086 45 78 70 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  Expr */.};../*.*
2d087 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73  * Allowed values
2d088 20 6f 66 20 57 68 65 72 65 54 65 72 6d 2e 77 74   of WhereTerm.wt
2d089 46 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 6e 65  Flags.*/.#define
2d08a 20 54 45 52 4d 5f 44 59 4e 41 4d 49 43 20 20 20   TERM_DYNAMIC   
2d08b 20 30 78 30 31 20 20 20 2f 2a 20 4e 65 65 64 20   0x01   /* Need 
2d08c 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 45  to call sqlite3E
2d08d 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 45  xprDelete(db, pE
2d08e 78 70 72 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20  xpr) */.#define 
2d08f 54 45 52 4d 5f 56 49 52 54 55 41 4c 20 20 20 20  TERM_VIRTUAL    
2d090 30 78 30 32 20 20 20 2f 2a 20 41 64 64 65 64 20  0x02   /* Added 
2d091 62 79 20 74 68 65 20 6f 70 74 69 6d 69 7a 65 72  by the optimizer
2d092 2e 20 20 44 6f 20 6e 6f 74 20 63 6f 64 65 20 2a  .  Do not code *
2d093 2f 0a 23 64 65 66 69 6e 65 20 54 45 52 4d 5f 43  /.#define TERM_C
2d094 4f 44 45 44 20 20 20 20 20 20 30 78 30 34 20 20  ODED      0x04  
2d095 20 2f 2a 20 54 68 69 73 20 74 65 72 6d 20 69 73   /* This term is
2d096 20 61 6c 72 65 61 64 79 20 63 6f 64 65 64 20 2a   already coded *
2d097 2f 0a 23 64 65 66 69 6e 65 20 54 45 52 4d 5f 43  /.#define TERM_C
2d098 4f 50 49 45 44 20 20 20 20 20 30 78 30 38 20 20  OPIED     0x08  
2d099 20 2f 2a 20 48 61 73 20 61 20 63 68 69 6c 64 20   /* Has a child 
2d09a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45 52 4d 5f  */.#define TERM_
2d09b 4f 52 49 4e 46 4f 20 20 20 20 20 30 78 31 30 20  ORINFO     0x10 
2d09c 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 66 72 65    /* Need to fre
2d09d 65 20 74 68 65 20 57 68 65 72 65 54 65 72 6d 2e  e the WhereTerm.
2d09e 75 2e 70 4f 72 49 6e 66 6f 20 6f 62 6a 65 63 74  u.pOrInfo object
2d09f 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45 52 4d   */.#define TERM
2d0a0 5f 41 4e 44 49 4e 46 4f 20 20 20 20 30 78 32 30  _ANDINFO    0x20
2d0a1 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 66 72     /* Need to fr
2d0a2 65 65 20 74 68 65 20 57 68 65 72 65 54 65 72 6d  ee the WhereTerm
2d0a3 2e 75 2e 70 41 6e 64 49 6e 66 6f 20 6f 62 6a 20  .u.pAndInfo obj 
2d0a4 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45 52 4d 5f  */.#define TERM_
2d0a5 4f 52 5f 4f 4b 20 20 20 20 20 20 30 78 34 30 20  OR_OK      0x40 
2d0a6 20 20 2f 2a 20 55 73 65 64 20 64 75 72 69 6e 67    /* Used during
2d0a7 20 4f 52 2d 63 6c 61 75 73 65 20 70 72 6f 63 65   OR-clause proce
2d0a8 73 73 69 6e 67 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  ssing */../*.** 
2d0a9 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  An instance of t
2d0aa 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
2d0ab 75 63 74 75 72 65 20 68 6f 6c 64 73 20 61 6c 6c  ucture holds all
2d0ac 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
2d0ad 75 74 20 61 0a 2a 2a 20 57 48 45 52 45 20 63 6c  ut a.** WHERE cl
2d0ae 61 75 73 65 2e 20 20 4d 6f 73 74 6c 79 20 74 68  ause.  Mostly th
2d0af 69 73 20 69 73 20 61 20 63 6f 6e 74 61 69 6e 65  is is a containe
2d0b0 72 20 66 6f 72 20 6f 6e 65 20 6f 72 20 6d 6f 72  r for one or mor
2d0b1 65 20 57 68 65 72 65 54 65 72 6d 73 2e 0a 2a 2f  e WhereTerms..*/
2d0b2 0a 73 74 72 75 63 74 20 57 68 65 72 65 43 6c 61  .struct WhereCla
2d0b3 75 73 65 20 7b 0a 20 20 50 61 72 73 65 20 2a 70  use {.  Parse *p
2d0b4 50 61 72 73 65 3b 20 20 20 20 20 20 20 20 20 20  Parse;          
2d0b5 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63   /* The parser c
2d0b6 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68 65 72  ontext */.  Wher
2d0b7 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53  eMaskSet *pMaskS
2d0b8 65 74 3b 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20  et;  /* Mapping 
2d0b9 6f 66 20 74 61 62 6c 65 20 63 75 72 73 6f 72 20  of table cursor 
2d0ba 6e 75 6d 62 65 72 73 20 74 6f 20 62 69 74 6d 61  numbers to bitma
2d0bb 73 6b 73 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b  sks */.  Bitmask
2d0bc 20 76 6d 61 73 6b 3b 20 20 20 20 20 20 20 20 20   vmask;         
2d0bd 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 69 64 65    /* Bitmask ide
2d0be 6e 74 69 66 79 69 6e 67 20 76 69 72 74 75 61 6c  ntifying virtual
2d0bf 20 74 61 62 6c 65 20 63 75 72 73 6f 72 73 20 2a   table cursors *
2d0c0 2f 0a 20 20 75 38 20 6f 70 3b 20 20 20 20 20 20  /.  u8 op;      
2d0c1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2d0c2 53 70 6c 69 74 20 6f 70 65 72 61 74 6f 72 2e 20  Split operator. 
2d0c3 20 54 4b 5f 41 4e 44 20 6f 72 20 54 4b 5f 4f 52   TK_AND or TK_OR
2d0c4 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b   */.  int nTerm;
2d0c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2d0c6 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d  * Number of term
2d0c7 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 6c 6f 74  s */.  int nSlot
2d0c8 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2d0c9 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74  /* Number of ent
2d0ca 72 69 65 73 20 69 6e 20 61 5b 5d 20 2a 2f 0a 20  ries in a[] */. 
2d0cb 20 57 68 65 72 65 54 65 72 6d 20 2a 61 3b 20 20   WhereTerm *a;  
2d0cc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 61 63            /* Eac
2d0cd 68 20 61 5b 5d 20 64 65 73 63 72 69 62 65 73 20  h a[] describes 
2d0ce 61 20 74 65 72 6d 20 6f 66 20 74 68 65 20 57 48  a term of the WH
2d0cf 45 52 45 20 63 6c 75 61 73 65 20 2a 2f 0a 23 69  ERE cluase */.#i
2d0d0 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
2d0d1 5f 53 4d 41 4c 4c 5f 53 54 41 43 4b 29 0a 20 20  _SMALL_STACK).  
2d0d2 57 68 65 72 65 54 65 72 6d 20 61 53 74 61 74 69  WhereTerm aStati
2d0d3 63 5b 31 5d 3b 20 20 20 20 2f 2a 20 49 6e 69 74  c[1];    /* Init
2d0d4 69 61 6c 20 73 74 61 74 69 63 20 73 70 61 63 65  ial static space
2d0d5 20 66 6f 72 20 61 5b 5d 20 2a 2f 0a 23 65 6c 73   for a[] */.#els
2d0d6 65 0a 20 20 57 68 65 72 65 54 65 72 6d 20 61 53  e.  WhereTerm aS
2d0d7 74 61 74 69 63 5b 38 5d 3b 20 20 20 20 2f 2a 20  tatic[8];    /* 
2d0d8 49 6e 69 74 69 61 6c 20 73 74 61 74 69 63 20 73  Initial static s
2d0d9 70 61 63 65 20 66 6f 72 20 61 5b 5d 20 2a 2f 0a  pace for a[] */.
2d0da 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  #endif.};../*.**
2d0db 20 41 20 57 68 65 72 65 54 65 72 6d 20 77 69 74   A WhereTerm wit
2d0dc 68 20 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f  h eOperator==WO_
2d0dd 4f 52 20 68 61 73 20 69 74 73 20 75 2e 70 4f 72  OR has its u.pOr
2d0de 49 6e 66 6f 20 70 6f 69 6e 74 65 72 20 73 65 74  Info pointer set
2d0df 20 74 6f 0a 2a 2a 20 61 20 64 79 6e 61 6d 69 63   to.** a dynamic
2d0e0 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 69  ally allocated i
2d0e1 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
2d0e2 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
2d0e3 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68  re..*/.struct Wh
2d0e4 65 72 65 4f 72 49 6e 66 6f 20 7b 0a 20 20 57 68  ereOrInfo {.  Wh
2d0e5 65 72 65 43 6c 61 75 73 65 20 77 63 3b 20 20 20  ereClause wc;   
2d0e6 20 20 20 20 20 20 20 2f 2a 20 44 65 63 6f 6d 70         /* Decomp
2d0e7 6f 73 69 74 69 6f 6e 20 69 6e 74 6f 20 73 75 62  osition into sub
2d0e8 74 65 72 6d 73 20 2a 2f 0a 20 20 42 69 74 6d 61  terms */.  Bitma
2d0e9 73 6b 20 69 6e 64 65 78 61 62 6c 65 3b 20 20 20  sk indexable;   
2d0ea 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f      /* Bitmask o
2d0eb 66 20 61 6c 6c 20 69 6e 64 65 78 61 62 6c 65 20  f all indexable 
2d0ec 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 63 6c  tables in the cl
2d0ed 61 75 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  ause */.};../*.*
2d0ee 2a 20 41 20 57 68 65 72 65 54 65 72 6d 20 77 69  * A WhereTerm wi
2d0ef 74 68 20 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f  th eOperator==WO
2d0f0 5f 41 4e 44 20 68 61 73 20 69 74 73 20 75 2e 70  _AND has its u.p
2d0f1 41 6e 64 49 6e 66 6f 20 70 6f 69 6e 74 65 72 20  AndInfo pointer 
2d0f2 73 65 74 20 74 6f 0a 2a 2a 20 61 20 64 79 6e 61  set to.** a dyna
2d0f3 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65  mically allocate
2d0f4 64 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  d instance of th
2d0f5 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
2d0f6 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74  cture..*/.struct
2d0f7 20 57 68 65 72 65 41 6e 64 49 6e 66 6f 20 7b 0a   WhereAndInfo {.
2d0f8 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 77 63    WhereClause wc
2d0f9 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ;          /* Th
2d0fa 65 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20  e subexpression 
2d0fb 62 72 6f 6b 65 6e 20 6f 75 74 20 2a 2f 0a 7d 3b  broken out */.};
2d0fc 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  ../*.** An insta
2d0fd 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
2d0fe 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 6b  wing structure k
2d0ff 65 65 70 73 20 74 72 61 63 6b 20 6f 66 20 61 20  eeps track of a 
2d100 6d 61 70 70 69 6e 67 0a 2a 2a 20 62 65 74 77 65  mapping.** betwe
2d101 65 6e 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e  en VDBE cursor n
2d102 75 6d 62 65 72 73 20 61 6e 64 20 62 69 74 73 20  umbers and bits 
2d103 6f 66 20 74 68 65 20 62 69 74 6d 61 73 6b 73 20  of the bitmasks 
2d104 69 6e 20 57 68 65 72 65 54 65 72 6d 2e 0a 2a 2a  in WhereTerm..**
2d105 0a 2a 2a 20 54 68 65 20 56 44 42 45 20 63 75 72  .** The VDBE cur
2d106 73 6f 72 20 6e 75 6d 62 65 72 73 20 61 72 65 20  sor numbers are 
2d107 73 6d 61 6c 6c 20 69 6e 74 65 67 65 72 73 20 63  small integers c
2d108 6f 6e 74 61 69 6e 65 64 20 69 6e 20 0a 2a 2a 20  ontained in .** 
2d109 53 72 63 4c 69 73 74 5f 69 74 65 6d 2e 69 43 75  SrcList_item.iCu
2d10a 72 73 6f 72 20 61 6e 64 20 45 78 70 72 2e 69 54  rsor and Expr.iT
2d10b 61 62 6c 65 20 66 69 65 6c 64 73 2e 20 20 46 6f  able fields.  Fo
2d10c 72 20 61 6e 79 20 67 69 76 65 6e 20 57 48 45 52  r any given WHER
2d10d 45 20 0a 2a 2a 20 63 6c 61 75 73 65 2c 20 74 68  E .** clause, th
2d10e 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73  e cursor numbers
2d10f 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 67 69 6e   might not begin
2d110 20 77 69 74 68 20 30 20 61 6e 64 20 74 68 65 79   with 0 and they
2d111 20 6d 69 67 68 74 0a 2a 2a 20 63 6f 6e 74 61 69   might.** contai
2d112 6e 20 67 61 70 73 20 69 6e 20 74 68 65 20 6e 75  n gaps in the nu
2d113 6d 62 65 72 69 6e 67 20 73 65 71 75 65 6e 63 65  mbering sequence
2d114 2e 20 20 42 75 74 20 77 65 20 77 61 6e 74 20 74  .  But we want t
2d115 6f 20 6d 61 6b 65 20 6d 61 78 69 6d 75 6d 0a 2a  o make maximum.*
2d116 2a 20 75 73 65 20 6f 66 20 74 68 65 20 62 69 74  * use of the bit
2d117 73 20 69 6e 20 6f 75 72 20 62 69 74 6d 61 73 6b  s in our bitmask
2d118 73 2e 20 20 54 68 69 73 20 73 74 72 75 63 74 75  s.  This structu
2d119 72 65 20 70 72 6f 76 69 64 65 73 20 61 20 6d 61  re provides a ma
2d11a 70 70 69 6e 67 0a 2a 2a 20 66 72 6f 6d 20 74 68  pping.** from th
2d11b 65 20 73 70 61 72 73 65 20 63 75 72 73 6f 72 20  e sparse cursor 
2d11c 6e 75 6d 62 65 72 73 20 69 6e 74 6f 20 63 6f 6e  numbers into con
2d11d 73 65 63 75 74 69 76 65 20 69 6e 74 65 67 65 72  secutive integer
2d11e 73 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 77  s beginning.** w
2d11f 69 74 68 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ith 0..**.** If 
2d120 57 68 65 72 65 4d 61 73 6b 53 65 74 2e 69 78 5b  WhereMaskSet.ix[
2d121 41 5d 3d 3d 42 20 69 74 20 6d 65 61 6e 73 20 74  A]==B it means t
2d122 68 61 74 20 54 68 65 20 41 2d 74 68 20 62 69 74  hat The A-th bit
2d123 20 6f 66 20 61 20 42 69 74 6d 61 73 6b 0a 2a 2a   of a Bitmask.**
2d124 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 56 44 42   corresponds VDB
2d125 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  E cursor number 
2d126 42 2e 20 20 54 68 65 20 41 2d 74 68 20 62 69 74  B.  The A-th bit
2d127 20 6f 66 20 61 20 62 69 74 6d 61 73 6b 20 69 73   of a bitmask is
2d128 20 31 3c 3c 41 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72   1<<A..**.** For
2d129 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65   example, if the
2d12a 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 65 78   WHERE clause ex
2d12b 70 72 65 73 73 69 6f 6e 20 75 73 65 64 20 74 68  pression used th
2d12c 65 73 65 20 56 44 42 45 0a 2a 2a 20 63 75 72 73  ese VDBE.** curs
2d12d 6f 72 73 3a 20 20 34 2c 20 35 2c 20 38 2c 20 32  ors:  4, 5, 8, 2
2d12e 39 2c 20 35 37 2c 20 37 33 2e 20 20 54 68 65 6e  9, 57, 73.  Then
2d12f 20 74 68 65 20 20 57 68 65 72 65 4d 61 73 6b 53   the  WhereMaskS
2d130 65 74 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  et structure.** 
2d131 77 6f 75 6c 64 20 6d 61 70 20 74 68 6f 73 65 20  would map those 
2d132 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 69  cursor numbers i
2d133 6e 74 6f 20 62 69 74 73 20 30 20 74 68 72 6f 75  nto bits 0 throu
2d134 67 68 20 35 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65  gh 5..**.** Note
2d135 20 74 68 61 74 20 74 68 65 20 6d 61 70 70 69 6e   that the mappin
2d136 67 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61  g is not necessa
2d137 72 69 6c 79 20 6f 72 64 65 72 65 64 2e 20 20 49  rily ordered.  I
2d138 6e 20 74 68 65 20 65 78 61 6d 70 6c 65 0a 2a 2a  n the example.**
2d139 20 61 62 6f 76 65 2c 20 74 68 65 20 6d 61 70 70   above, the mapp
2d13a 69 6e 67 20 6d 69 67 68 74 20 67 6f 20 6c 69 6b  ing might go lik
2d13b 65 20 74 68 69 73 3a 20 20 34 2d 3e 33 2c 20 35  e this:  4->3, 5
2d13c 2d 3e 31 2c 20 38 2d 3e 32 2c 20 32 39 2d 3e 30  ->1, 8->2, 29->0
2d13d 2c 0a 2a 2a 20 35 37 2d 3e 35 2c 20 37 33 2d 3e  ,.** 57->5, 73->
2d13e 34 2e 20 20 4f 72 20 6f 6e 65 20 6f 66 20 37 31  4.  Or one of 71
2d13f 39 20 6f 74 68 65 72 20 63 6f 6d 62 69 6e 61 74  9 other combinat
2d140 69 6f 6e 73 20 6d 69 67 68 74 20 62 65 20 75 73  ions might be us
2d141 65 64 2e 20 49 74 0a 2a 2a 20 64 6f 65 73 20 6e  ed. It.** does n
2d142 6f 74 20 72 65 61 6c 6c 79 20 6d 61 74 74 65 72  ot really matter
2d143 2e 20 20 57 68 61 74 20 69 73 20 69 6d 70 6f 72  .  What is impor
2d144 74 61 6e 74 20 69 73 20 74 68 61 74 20 73 70 61  tant is that spa
2d145 72 73 65 20 63 75 72 73 6f 72 0a 2a 2a 20 6e 75  rse cursor.** nu
2d146 6d 62 65 72 73 20 61 6c 6c 20 67 65 74 20 6d 61  mbers all get ma
2d147 70 70 65 64 20 69 6e 74 6f 20 62 69 74 20 6e 75  pped into bit nu
2d148 6d 62 65 72 73 20 74 68 61 74 20 62 65 67 69 6e  mbers that begin
2d149 20 77 69 74 68 20 30 20 61 6e 64 20 63 6f 6e 74   with 0 and cont
2d14a 61 69 6e 0a 2a 2a 20 6e 6f 20 67 61 70 73 2e 0a  ain.** no gaps..
2d14b 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 65 4d  */.struct WhereM
2d14c 61 73 6b 53 65 74 20 7b 0a 20 20 69 6e 74 20 6e  askSet {.  int n
2d14d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2d14e 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
2d14f 65 72 20 6f 66 20 61 73 73 69 67 6e 65 64 20 63  er of assigned c
2d150 75 72 73 6f 72 20 76 61 6c 75 65 73 20 2a 2f 0a  ursor values */.
2d151 20 20 69 6e 74 20 69 78 5b 42 4d 53 5d 3b 20 20    int ix[BMS];  
2d152 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d153 2f 2a 20 43 75 72 73 6f 72 20 61 73 73 69 67 6e  /* Cursor assign
2d154 65 64 20 74 6f 20 65 61 63 68 20 62 69 74 20 2a  ed to each bit *
2d155 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 57 68  /.};../*.** A Wh
2d156 65 72 65 43 6f 73 74 20 6f 62 6a 65 63 74 20 72  ereCost object r
2d157 65 63 6f 72 64 73 20 61 20 6c 6f 6f 6b 75 70 20  ecords a lookup 
2d158 73 74 72 61 74 65 67 79 20 61 6e 64 20 74 68 65  strategy and the
2d159 20 65 73 74 69 6d 61 74 65 64 0a 2a 2a 20 63 6f   estimated.** co
2d15a 73 74 20 6f 66 20 70 75 72 73 75 69 6e 67 20 74  st of pursuing t
2d15b 68 61 74 20 73 74 72 61 74 65 67 79 2e 0a 2a 2f  hat strategy..*/
2d15c 0a 73 74 72 75 63 74 20 57 68 65 72 65 43 6f 73  .struct WhereCos
2d15d 74 20 7b 0a 20 20 57 68 65 72 65 50 6c 61 6e 20  t {.  WherePlan 
2d15e 70 6c 61 6e 3b 20 20 20 20 2f 2a 20 54 68 65 20  plan;    /* The 
2d15f 6c 6f 6f 6b 75 70 20 73 74 72 61 74 65 67 79 20  lookup strategy 
2d160 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 43 6f 73  */.  double rCos
2d161 74 3b 20 20 20 20 20 20 2f 2a 20 4f 76 65 72 61  t;      /* Overa
2d162 6c 6c 20 63 6f 73 74 20 6f 66 20 70 75 72 73 75  ll cost of pursu
2d163 69 6e 67 20 74 68 69 73 20 73 65 61 72 63 68 20  ing this search 
2d164 73 74 72 61 74 65 67 79 20 2a 2f 0a 20 20 64 6f  strategy */.  do
2d165 75 62 6c 65 20 6e 52 6f 77 3b 20 20 20 20 20 20  uble nRow;      
2d166 20 2f 2a 20 45 73 74 69 6d 61 74 65 64 20 6e 75   /* Estimated nu
2d167 6d 62 65 72 20 6f 66 20 6f 75 74 70 75 74 20 72  mber of output r
2d168 6f 77 73 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b  ows */.  Bitmask
2d169 20 75 73 65 64 3b 20 20 20 20 20 20 2f 2a 20 42   used;      /* B
2d16a 69 74 6d 61 73 6b 20 6f 66 20 63 75 72 73 6f 72  itmask of cursor
2d16b 73 20 75 73 65 64 20 62 79 20 74 68 69 73 20 70  s used by this p
2d16c 6c 61 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  lan */.};../*.**
2d16d 20 42 69 74 6d 61 73 6b 73 20 66 6f 72 20 74 68   Bitmasks for th
2d16e 65 20 6f 70 65 72 61 74 6f 72 73 20 74 68 61 74  e operators that
2d16f 20 69 6e 64 69 63 65 73 20 61 72 65 20 61 62 6c   indices are abl
2d170 65 20 74 6f 20 65 78 70 6c 6f 69 74 2e 20 20 41  e to exploit.  A
2d171 6e 0a 2a 2a 20 4f 52 2d 65 64 20 63 6f 6d 62 69  n.** OR-ed combi
2d172 6e 61 74 69 6f 6e 20 6f 66 20 74 68 65 73 65 20  nation of these 
2d173 76 61 6c 75 65 73 20 63 61 6e 20 62 65 20 75 73  values can be us
2d174 65 64 20 77 68 65 6e 20 73 65 61 72 63 68 69 6e  ed when searchin
2d175 67 20 66 6f 72 0a 2a 2a 20 74 65 72 6d 73 20 69  g for.** terms i
2d176 6e 20 74 68 65 20 77 68 65 72 65 20 63 6c 61 75  n the where clau
2d177 73 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 57  se..*/.#define W
2d178 4f 5f 49 4e 20 20 20 20 20 30 78 30 30 31 0a 23  O_IN     0x001.#
2d179 64 65 66 69 6e 65 20 57 4f 5f 45 51 20 20 20 20  define WO_EQ    
2d17a 20 30 78 30 30 32 0a 23 64 65 66 69 6e 65 20 57   0x002.#define W
2d17b 4f 5f 4c 54 20 20 20 20 20 28 57 4f 5f 45 51 3c  O_LT     (WO_EQ<
2d17c 3c 28 54 4b 5f 4c 54 2d 54 4b 5f 45 51 29 29 0a  <(TK_LT-TK_EQ)).
2d17d 23 64 65 66 69 6e 65 20 57 4f 5f 4c 45 20 20 20  #define WO_LE   
2d17e 20 20 28 57 4f 5f 45 51 3c 3c 28 54 4b 5f 4c 45    (WO_EQ<<(TK_LE
2d17f 2d 54 4b 5f 45 51 29 29 0a 23 64 65 66 69 6e 65  -TK_EQ)).#define
2d180 20 57 4f 5f 47 54 20 20 20 20 20 28 57 4f 5f 45   WO_GT     (WO_E
2d181 51 3c 3c 28 54 4b 5f 47 54 2d 54 4b 5f 45 51 29  Q<<(TK_GT-TK_EQ)
2d182 29 0a 23 64 65 66 69 6e 65 20 57 4f 5f 47 45 20  ).#define WO_GE 
2d183 20 20 20 20 28 57 4f 5f 45 51 3c 3c 28 54 4b 5f      (WO_EQ<<(TK_
2d184 47 45 2d 54 4b 5f 45 51 29 29 0a 23 64 65 66 69  GE-TK_EQ)).#defi
2d185 6e 65 20 57 4f 5f 4d 41 54 43 48 20 20 30 78 30  ne WO_MATCH  0x0
2d186 34 30 0a 23 64 65 66 69 6e 65 20 57 4f 5f 49 53  40.#define WO_IS
2d187 4e 55 4c 4c 20 30 78 30 38 30 0a 23 64 65 66 69  NULL 0x080.#defi
2d188 6e 65 20 57 4f 5f 4f 52 20 20 20 20 20 30 78 31  ne WO_OR     0x1
2d189 30 30 20 20 20 20 20 20 20 2f 2a 20 54 77 6f 20  00       /* Two 
2d18a 6f 72 20 6d 6f 72 65 20 4f 52 2d 63 6f 6e 6e 65  or more OR-conne
2d18b 63 74 65 64 20 74 65 72 6d 73 20 2a 2f 0a 23 64  cted terms */.#d
2d18c 65 66 69 6e 65 20 57 4f 5f 41 4e 44 20 20 20 20  efine WO_AND    
2d18d 30 78 32 30 30 20 20 20 20 20 20 20 2f 2a 20 54  0x200       /* T
2d18e 77 6f 20 6f 72 20 6d 6f 72 65 20 41 4e 44 2d 63  wo or more AND-c
2d18f 6f 6e 6e 65 63 74 65 64 20 74 65 72 6d 73 20 2a  onnected terms *
2d190 2f 0a 0a 23 64 65 66 69 6e 65 20 57 4f 5f 41 4c  /..#define WO_AL
2d191 4c 20 20 20 20 30 78 66 66 66 20 20 20 20 20 20  L    0xfff      
2d192 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 61 6c 6c 20   /* Mask of all 
2d193 70 6f 73 73 69 62 6c 65 20 57 4f 5f 2a 20 76 61  possible WO_* va
2d194 6c 75 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20  lues */.#define 
2d195 57 4f 5f 53 49 4e 47 4c 45 20 30 78 30 66 66 20  WO_SINGLE 0x0ff 
2d196 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66        /* Mask of
2d197 20 61 6c 6c 20 6e 6f 6e 2d 63 6f 6d 70 6f 75 6e   all non-compoun
2d198 64 20 57 4f 5f 2a 20 76 61 6c 75 65 73 20 2a 2f  d WO_* values */
2d199 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 75 65 20 66 6f  ../*.** Value fo
2d19a 72 20 77 73 46 6c 61 67 73 20 72 65 74 75 72 6e  r wsFlags return
2d19b 65 64 20 62 79 20 62 65 73 74 49 6e 64 65 78 28  ed by bestIndex(
2d19c 29 20 61 6e 64 20 73 74 6f 72 65 64 20 69 6e 0a  ) and stored in.
2d19d 2a 2a 20 57 68 65 72 65 4c 65 76 65 6c 2e 77 73  ** WhereLevel.ws
2d19e 46 6c 61 67 73 2e 20 20 54 68 65 73 65 20 66 6c  Flags.  These fl
2d19f 61 67 73 20 64 65 74 65 72 6d 69 6e 65 20 77 68  ags determine wh
2d1a0 69 63 68 20 73 65 61 72 63 68 0a 2a 2a 20 73 74  ich search.** st
2d1a1 72 61 74 65 67 69 65 73 20 61 72 65 20 61 70 70  rategies are app
2d1a2 72 6f 70 72 69 61 74 65 2e 0a 2a 2a 0a 2a 2a 20  ropriate..**.** 
2d1a3 54 68 65 20 6c 65 61 73 74 20 73 69 67 6e 69 66  The least signif
2d1a4 69 63 61 6e 74 20 31 32 20 62 69 74 73 20 69 73  icant 12 bits is
2d1a5 20 72 65 73 65 72 76 65 64 20 61 73 20 61 20 6d   reserved as a m
2d1a6 61 73 6b 20 66 6f 72 20 57 4f 5f 20 76 61 6c 75  ask for WO_ valu
2d1a7 65 73 20 61 62 6f 76 65 2e 0a 2a 2a 20 54 68 65  es above..** The
2d1a8 20 57 68 65 72 65 4c 65 76 65 6c 2e 77 73 46 6c   WhereLevel.wsFl
2d1a9 61 67 73 20 66 69 65 6c 64 20 69 73 20 75 73 75  ags field is usu
2d1aa 61 6c 6c 79 20 73 65 74 20 74 6f 20 57 4f 5f 49  ally set to WO_I
2d1ab 4e 7c 57 4f 5f 45 51 7c 57 4f 5f 49 53 4e 55 4c  N|WO_EQ|WO_ISNUL
2d1ac 4c 2e 0a 2a 2a 20 42 75 74 20 69 66 20 74 68 65  L..** But if the
2d1ad 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 72 69   table is the ri
2d1ae 67 68 74 20 74 61 62 6c 65 20 6f 66 20 61 20 6c  ght table of a l
2d1af 65 66 74 20 6a 6f 69 6e 2c 20 57 68 65 72 65 4c  eft join, WhereL
2d1b0 65 76 65 6c 2e 77 73 46 6c 61 67 73 0a 2a 2a 20  evel.wsFlags.** 
2d1b1 69 73 20 73 65 74 20 74 6f 20 57 4f 5f 49 4e 7c  is set to WO_IN|
2d1b2 57 4f 5f 45 51 2e 20 20 54 68 65 20 57 68 65 72  WO_EQ.  The Wher
2d1b3 65 4c 65 76 65 6c 2e 77 73 46 6c 61 67 73 20 66  eLevel.wsFlags f
2d1b4 69 65 6c 64 20 63 61 6e 20 74 68 65 6e 20 62 65  ield can then be
2d1b5 20 75 73 65 64 20 61 73 0a 2a 2a 20 74 68 65 20   used as.** the 
2d1b6 22 6f 70 22 20 70 61 72 61 6d 65 74 65 72 20 74  "op" parameter t
2d1b7 6f 20 66 69 6e 64 54 65 72 6d 20 77 68 65 6e 20  o findTerm when 
2d1b8 77 65 20 61 72 65 20 72 65 73 6f 6c 76 69 6e 67  we are resolving
2d1b9 20 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72   equality constr
2d1ba 61 69 6e 74 73 2e 0a 2a 2a 20 49 53 4e 55 4c 4c  aints..** ISNULL
2d1bb 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 77 69 6c   constraints wil
2d1bc 6c 20 74 68 65 6e 20 6e 6f 74 20 62 65 20 75 73  l then not be us
2d1bd 65 64 20 6f 6e 20 74 68 65 20 72 69 67 68 74 20  ed on the right 
2d1be 74 61 62 6c 65 20 6f 66 20 61 20 6c 65 66 74 0a  table of a left.
2d1bf 2a 2a 20 6a 6f 69 6e 2e 20 20 54 69 63 6b 65 74  ** join.  Ticket
2d1c0 73 20 23 32 31 37 37 20 61 6e 64 20 23 32 31 38  s #2177 and #218
2d1c1 39 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48  9..*/.#define WH
2d1c2 45 52 45 5f 52 4f 57 49 44 5f 45 51 20 20 20 20  ERE_ROWID_EQ    
2d1c3 20 30 78 30 30 30 30 31 30 30 30 20 20 2f 2a 20   0x00001000  /* 
2d1c4 72 6f 77 69 64 3d 45 58 50 52 20 6f 72 20 72 6f  rowid=EXPR or ro
2d1c5 77 69 64 20 49 4e 20 28 2e 2e 2e 29 20 2a 2f 0a  wid IN (...) */.
2d1c6 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 52 4f  #define WHERE_RO
2d1c7 57 49 44 5f 52 41 4e 47 45 20 20 30 78 30 30 30  WID_RANGE  0x000
2d1c8 30 32 30 30 30 20 20 2f 2a 20 72 6f 77 69 64 3c  02000  /* rowid<
2d1c9 45 58 50 52 20 61 6e 64 2f 6f 72 20 72 6f 77 69  EXPR and/or rowi
2d1ca 64 3e 45 58 50 52 20 2a 2f 0a 23 64 65 66 69 6e  d>EXPR */.#defin
2d1cb 65 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 45  e WHERE_COLUMN_E
2d1cc 51 20 20 20 20 30 78 30 30 30 31 30 30 30 30 20  Q    0x00010000 
2d1cd 20 2f 2a 20 78 3d 45 58 50 52 20 6f 72 20 78 20   /* x=EXPR or x 
2d1ce 49 4e 20 28 2e 2e 2e 29 20 6f 72 20 78 20 49 53  IN (...) or x IS
2d1cf 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65   NULL */.#define
2d1d0 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41   WHERE_COLUMN_RA
2d1d1 4e 47 45 20 30 78 30 30 30 32 30 30 30 30 20 20  NGE 0x00020000  
2d1d2 2f 2a 20 78 3c 45 58 50 52 20 61 6e 64 2f 6f 72  /* x<EXPR and/or
2d1d3 20 78 3e 45 58 50 52 20 2a 2f 0a 23 64 65 66 69   x>EXPR */.#defi
2d1d4 6e 65 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f  ne WHERE_COLUMN_
2d1d5 49 4e 20 20 20 20 30 78 30 30 30 34 30 30 30 30  IN    0x00040000
2d1d6 20 20 2f 2a 20 78 20 49 4e 20 28 2e 2e 2e 29 20    /* x IN (...) 
2d1d7 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45  */.#define WHERE
2d1d8 5f 43 4f 4c 55 4d 4e 5f 4e 55 4c 4c 20 20 30 78  _COLUMN_NULL  0x
2d1d9 30 30 30 38 30 30 30 30 20 20 2f 2a 20 78 20 49  00080000  /* x I
2d1da 53 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e  S NULL */.#defin
2d1db 65 20 57 48 45 52 45 5f 49 4e 44 45 58 45 44 20  e WHERE_INDEXED 
2d1dc 20 20 20 20 20 30 78 30 30 30 66 30 30 30 30 20       0x000f0000 
2d1dd 20 2f 2a 20 41 6e 79 74 68 69 6e 67 20 74 68 61   /* Anything tha
2d1de 74 20 75 73 65 73 20 61 6e 20 69 6e 64 65 78 20  t uses an index 
2d1df 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45  */.#define WHERE
2d1e0 5f 49 4e 5f 41 42 4c 45 20 20 20 20 20 20 30 78  _IN_ABLE      0x
2d1e1 30 30 30 66 31 30 30 30 20 20 2f 2a 20 41 62 6c  000f1000  /* Abl
2d1e2 65 20 74 6f 20 73 75 70 70 6f 72 74 20 61 6e 20  e to support an 
2d1e3 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 23  IN operator */.#
2d1e4 64 65 66 69 6e 65 20 57 48 45 52 45 5f 54 4f 50  define WHERE_TOP
2d1e5 5f 4c 49 4d 49 54 20 20 20 20 30 78 30 30 31 30  _LIMIT    0x0010
2d1e6 30 30 30 30 20 20 2f 2a 20 78 3c 45 58 50 52 20  0000  /* x<EXPR 
2d1e7 6f 72 20 78 3c 3d 45 58 50 52 20 63 6f 6e 73 74  or x<=EXPR const
2d1e8 72 61 69 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65  raint */.#define
2d1e9 20 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49 54   WHERE_BTM_LIMIT
2d1ea 20 20 20 20 30 78 30 30 32 30 30 30 30 30 20 20      0x00200000  
2d1eb 2f 2a 20 78 3e 45 58 50 52 20 6f 72 20 78 3e 3d  /* x>EXPR or x>=
2d1ec 45 58 50 52 20 63 6f 6e 73 74 72 61 69 6e 74 20  EXPR constraint 
2d1ed 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45  */.#define WHERE
2d1ee 5f 49 44 58 5f 4f 4e 4c 59 20 20 20 20 20 30 78  _IDX_ONLY     0x
2d1ef 30 30 38 30 30 30 30 30 20 20 2f 2a 20 55 73 65  00800000  /* Use
2d1f0 20 69 6e 64 65 78 20 6f 6e 6c 79 20 2d 20 6f 6d   index only - om
2d1f1 69 74 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65 66  it table */.#def
2d1f2 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 52 42  ine WHERE_ORDERB
2d1f3 59 20 20 20 20 20 20 30 78 30 31 30 30 30 30 30  Y      0x0100000
2d1f4 30 20 20 2f 2a 20 4f 75 74 70 75 74 20 77 69 6c  0  /* Output wil
2d1f5 6c 20 61 70 70 65 61 72 20 69 6e 20 63 6f 72 72  l appear in corr
2d1f6 65 63 74 20 6f 72 64 65 72 20 2a 2f 0a 23 64 65  ect order */.#de
2d1f7 66 69 6e 65 20 57 48 45 52 45 5f 52 45 56 45 52  fine WHERE_REVER
2d1f8 53 45 20 20 20 20 20 20 30 78 30 32 30 30 30 30  SE      0x020000
2d1f9 30 30 20 20 2f 2a 20 53 63 61 6e 20 69 6e 20 72  00  /* Scan in r
2d1fa 65 76 65 72 73 65 20 6f 72 64 65 72 20 2a 2f 0a  everse order */.
2d1fb 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 55 4e  #define WHERE_UN
2d1fc 49 51 55 45 20 20 20 20 20 20 20 30 78 30 34 30  IQUE       0x040
2d1fd 30 30 30 30 30 20 20 2f 2a 20 53 65 6c 65 63 74  00000  /* Select
2d1fe 73 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e 20 6f  s no more than o
2d1ff 6e 65 20 72 6f 77 20 2a 2f 0a 23 64 65 66 69 6e  ne row */.#defin
2d200 65 20 57 48 45 52 45 5f 56 49 52 54 55 41 4c 54  e WHERE_VIRTUALT
2d201 41 42 4c 45 20 30 78 30 38 30 30 30 30 30 30 20  ABLE 0x08000000 
2d202 20 2f 2a 20 55 73 65 20 76 69 72 74 75 61 6c 2d   /* Use virtual-
2d203 74 61 62 6c 65 20 70 72 6f 63 65 73 73 69 6e 67  table processing
2d204 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52   */.#define WHER
2d205 45 5f 4d 55 4c 54 49 5f 4f 52 20 20 20 20 20 30  E_MULTI_OR     0
2d206 78 31 30 30 30 30 30 30 30 20 20 2f 2a 20 4f 52  x10000000  /* OR
2d207 20 75 73 69 6e 67 20 6d 75 6c 74 69 70 6c 65 20   using multiple 
2d208 69 6e 64 69 63 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a  indices */../*.*
2d209 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 20 70  * Initialize a p
2d20a 72 65 61 6c 6c 6f 63 61 74 65 64 20 57 68 65 72  reallocated Wher
2d20b 65 43 6c 61 75 73 65 20 73 74 72 75 63 74 75 72  eClause structur
2d20c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
2d20d 64 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e 69  d whereClauseIni
2d20e 74 28 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65  t(.  WhereClause
2d20f 20 2a 70 57 43 2c 20 20 20 20 20 20 20 20 2f 2a   *pWC,        /*
2d210 20 54 68 65 20 57 68 65 72 65 43 6c 61 75 73 65   The WhereClause
2d211 20 74 6f 20 62 65 20 69 6e 69 74 69 61 6c 69 7a   to be initializ
2d212 65 64 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70  ed */.  Parse *p
2d213 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
2d214 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20   /* The parsing 
2d215 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68 65  context */.  Whe
2d216 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b  reMaskSet *pMask
2d217 53 65 74 20 20 20 2f 2a 20 4d 61 70 70 69 6e 67  Set   /* Mapping
2d218 20 66 72 6f 6d 20 74 61 62 6c 65 20 63 75 72 73   from table curs
2d219 6f 72 20 6e 75 6d 62 65 72 73 20 74 6f 20 62 69  or numbers to bi
2d21a 74 6d 61 73 6b 73 20 2a 2f 0a 29 7b 0a 20 20 70  tmasks */.){.  p
2d21b 57 43 2d 3e 70 50 61 72 73 65 20 3d 20 70 50 61  WC->pParse = pPa
2d21c 72 73 65 3b 0a 20 20 70 57 43 2d 3e 70 4d 61 73  rse;.  pWC->pMas
2d21d 6b 53 65 74 20 3d 20 70 4d 61 73 6b 53 65 74 3b  kSet = pMaskSet;
2d21e 0a 20 20 70 57 43 2d 3e 6e 54 65 72 6d 20 3d 20  .  pWC->nTerm = 
2d21f 30 3b 0a 20 20 70 57 43 2d 3e 6e 53 6c 6f 74 20  0;.  pWC->nSlot 
2d220 3d 20 41 72 72 61 79 53 69 7a 65 28 70 57 43 2d  = ArraySize(pWC-
2d221 3e 61 53 74 61 74 69 63 29 3b 0a 20 20 70 57 43  >aStatic);.  pWC
2d222 2d 3e 61 20 3d 20 70 57 43 2d 3e 61 53 74 61 74  ->a = pWC->aStat
2d223 69 63 3b 0a 20 20 70 57 43 2d 3e 76 6d 61 73 6b  ic;.  pWC->vmask
2d224 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77   = 0;.}../* Forw
2d225 61 72 64 20 72 65 66 65 72 65 6e 63 65 20 2a 2f  ard reference */
2d226 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 68 65  .static void whe
2d227 72 65 43 6c 61 75 73 65 43 6c 65 61 72 28 57 68  reClauseClear(Wh
2d228 65 72 65 43 6c 61 75 73 65 2a 29 3b 0a 0a 2f 2a  ereClause*);../*
2d229 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20 61  .** Deallocate a
2d22a 6c 6c 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69  ll memory associ
2d22b 61 74 65 64 20 77 69 74 68 20 61 20 57 68 65 72  ated with a Wher
2d22c 65 4f 72 49 6e 66 6f 20 6f 62 6a 65 63 74 2e 0a  eOrInfo object..
2d22d 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77  */.static void w
2d22e 68 65 72 65 4f 72 49 6e 66 6f 44 65 6c 65 74 65  hereOrInfoDelete
2d22f 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 57 68  (sqlite3 *db, Wh
2d230 65 72 65 4f 72 49 6e 66 6f 20 2a 70 29 7b 0a 20  ereOrInfo *p){. 
2d231 20 77 68 65 72 65 43 6c 61 75 73 65 43 6c 65 61   whereClauseClea
2d232 72 28 26 70 2d 3e 77 63 29 3b 0a 20 20 73 71 6c  r(&p->wc);.  sql
2d233 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
2d234 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 61 6c  );.}../*.** Deal
2d235 6c 6f 63 61 74 65 20 61 6c 6c 20 6d 65 6d 6f 72  locate all memor
2d236 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  y associated wit
2d237 68 20 61 20 57 68 65 72 65 41 6e 64 49 6e 66 6f  h a WhereAndInfo
2d238 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 61 74   object..*/.stat
2d239 69 63 20 76 6f 69 64 20 77 68 65 72 65 41 6e 64  ic void whereAnd
2d23a 49 6e 66 6f 44 65 6c 65 74 65 28 73 71 6c 69 74  InfoDelete(sqlit
2d23b 65 33 20 2a 64 62 2c 20 57 68 65 72 65 41 6e 64  e3 *db, WhereAnd
2d23c 49 6e 66 6f 20 2a 70 29 7b 0a 20 20 77 68 65 72  Info *p){.  wher
2d23d 65 43 6c 61 75 73 65 43 6c 65 61 72 28 26 70 2d  eClauseClear(&p-
2d23e 3e 77 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  >wc);.  sqlite3D
2d23f 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 7d 0a  bFree(db, p);.}.
2d240 0a 2f 2a 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61 74  ./*.** Deallocat
2d241 65 20 61 20 57 68 65 72 65 43 6c 61 75 73 65 20  e a WhereClause 
2d242 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20  structure.  The 
2d243 57 68 65 72 65 43 6c 61 75 73 65 20 73 74 72 75  WhereClause stru
2d244 63 74 75 72 65 0a 2a 2a 20 69 74 73 65 6c 66 20  cture.** itself 
2d245 69 73 20 6e 6f 74 20 66 72 65 65 64 2e 20 20 54  is not freed.  T
2d246 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74  his routine is t
2d247 68 65 20 69 6e 76 65 72 73 65 20 6f 66 20 77 68  he inverse of wh
2d248 65 72 65 43 6c 61 75 73 65 49 6e 69 74 28 29 2e  ereClauseInit().
2d249 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2d24a 77 68 65 72 65 43 6c 61 75 73 65 43 6c 65 61 72  whereClauseClear
2d24b 28 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57  (WhereClause *pW
2d24c 43 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 57  C){.  int i;.  W
2d24d 68 65 72 65 54 65 72 6d 20 2a 61 3b 0a 20 20 73  hereTerm *a;.  s
2d24e 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 57 43  qlite3 *db = pWC
2d24f 2d 3e 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  ->pParse->db;.  
2d250 66 6f 72 28 69 3d 70 57 43 2d 3e 6e 54 65 72 6d  for(i=pWC->nTerm
2d251 2d 31 2c 20 61 3d 70 57 43 2d 3e 61 3b 20 69 3e  -1, a=pWC->a; i>
2d252 3d 30 3b 20 69 2d 2d 2c 20 61 2b 2b 29 7b 0a 20  =0; i--, a++){. 
2d253 20 20 20 69 66 28 20 61 2d 3e 77 74 46 6c 61 67     if( a->wtFlag
2d254 73 20 26 20 54 45 52 4d 5f 44 59 4e 41 4d 49 43  s & TERM_DYNAMIC
2d255 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2d256 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
2d257 61 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20 20 7d  a->pExpr);.    }
2d258 0a 20 20 20 20 69 66 28 20 61 2d 3e 77 74 46 6c  .    if( a->wtFl
2d259 61 67 73 20 26 20 54 45 52 4d 5f 4f 52 49 4e 46  ags & TERM_ORINF
2d25a 4f 20 29 7b 0a 20 20 20 20 20 20 77 68 65 72 65  O ){.      where
2d25b 4f 72 49 6e 66 6f 44 65 6c 65 74 65 28 64 62 2c  OrInfoDelete(db,
2d25c 20 61 2d 3e 75 2e 70 4f 72 49 6e 66 6f 29 3b 0a   a->u.pOrInfo);.
2d25d 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 61 2d      }else if( a-
2d25e 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f  >wtFlags & TERM_
2d25f 41 4e 44 49 4e 46 4f 20 29 7b 0a 20 20 20 20 20  ANDINFO ){.     
2d260 20 77 68 65 72 65 41 6e 64 49 6e 66 6f 44 65 6c   whereAndInfoDel
2d261 65 74 65 28 64 62 2c 20 61 2d 3e 75 2e 70 41 6e  ete(db, a->u.pAn
2d262 64 49 6e 66 6f 29 3b 0a 20 20 20 20 7d 0a 20 20  dInfo);.    }.  
2d263 7d 0a 20 20 69 66 28 20 70 57 43 2d 3e 61 21 3d  }.  if( pWC->a!=
2d264 70 57 43 2d 3e 61 53 74 61 74 69 63 20 29 7b 0a  pWC->aStatic ){.
2d265 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2d266 65 28 64 62 2c 20 70 57 43 2d 3e 61 29 3b 0a 20  e(db, pWC->a);. 
2d267 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20   }.}../*.** Add 
2d268 61 20 73 69 6e 67 6c 65 20 6e 65 77 20 57 68 65  a single new Whe
2d269 72 65 54 65 72 6d 20 65 6e 74 72 79 20 74 6f 20  reTerm entry to 
2d26a 74 68 65 20 57 68 65 72 65 43 6c 61 75 73 65 20  the WhereClause 
2d26b 6f 62 6a 65 63 74 20 70 57 43 2e 0a 2a 2a 20 54  object pWC..** T
2d26c 68 65 20 6e 65 77 20 57 68 65 72 65 54 65 72 6d  he new WhereTerm
2d26d 20 6f 62 6a 65 63 74 20 69 73 20 63 6f 6e 73 74   object is const
2d26e 72 75 63 74 65 64 20 66 72 6f 6d 20 45 78 70 72  ructed from Expr
2d26f 20 70 20 61 6e 64 20 77 69 74 68 20 77 74 46 6c   p and with wtFl
2d270 61 67 73 2e 0a 2a 2a 20 54 68 65 20 69 6e 64 65  ags..** The inde
2d271 78 20 69 6e 20 70 57 43 2d 3e 61 5b 5d 20 6f 66  x in pWC->a[] of
2d272 20 74 68 65 20 6e 65 77 20 57 68 65 72 65 54 65   the new WhereTe
2d273 72 6d 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f  rm is returned o
2d274 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2a 20 30 20  n success..** 0 
2d275 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74  is returned if t
2d276 68 65 20 6e 65 77 20 57 68 65 72 65 54 65 72 6d  he new WhereTerm
2d277 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 64   could not be ad
2d278 64 65 64 20 64 75 65 20 74 6f 20 61 20 6d 65 6d  ded due to a mem
2d279 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f  ory.** allocatio
2d27a 6e 20 65 72 72 6f 72 2e 20 20 54 68 65 20 6d 65  n error.  The me
2d27b 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
2d27c 66 61 69 6c 75 72 65 20 77 69 6c 6c 20 62 65 20  failure will be 
2d27d 72 65 63 6f 72 64 65 64 20 69 6e 0a 2a 2a 20 74  recorded in.** t
2d27e 68 65 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  he db->mallocFai
2d27f 6c 65 64 20 66 6c 61 67 20 73 6f 20 74 68 61 74  led flag so that
2d280 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 20 66 75   higher-level fu
2d281 6e 63 74 69 6f 6e 73 20 63 61 6e 20 64 65 74 65  nctions can dete
2d282 63 74 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ct it..**.** Thi
2d283 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 69  s routine will i
2d284 6e 63 72 65 61 73 65 20 74 68 65 20 73 69 7a 65  ncrease the size
2d285 20 6f 66 20 74 68 65 20 70 57 43 2d 3e 61 5b 5d   of the pWC->a[]
2d286 20 61 72 72 61 79 20 61 73 20 6e 65 63 65 73 73   array as necess
2d287 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ary..**.** If th
2d288 65 20 77 74 46 6c 61 67 73 20 61 72 67 75 6d 65  e wtFlags argume
2d289 6e 74 20 69 6e 63 6c 75 64 65 73 20 54 45 52 4d  nt includes TERM
2d28a 5f 44 59 4e 41 4d 49 43 2c 20 74 68 65 6e 20 72  _DYNAMIC, then r
2d28b 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 0a 2a 2a  esponsibility.**
2d28c 20 66 6f 72 20 66 72 65 65 69 6e 67 20 74 68 65   for freeing the
2d28d 20 65 78 70 72 65 73 73 69 6f 6e 20 70 20 69 73   expression p is
2d28e 20 61 73 73 75 6d 65 64 20 62 79 20 74 68 65 20   assumed by the 
2d28f 57 68 65 72 65 43 6c 61 75 73 65 20 6f 62 6a 65  WhereClause obje
2d290 63 74 20 70 57 43 2e 0a 2a 2a 20 54 68 69 73 20  ct pWC..** This 
2d291 69 73 20 74 72 75 65 20 65 76 65 6e 20 69 66 20  is true even if 
2d292 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66 61 69  this routine fai
2d293 6c 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61  ls to allocate a
2d294 20 6e 65 77 20 57 68 65 72 65 54 65 72 6d 2e 0a   new WhereTerm..
2d295 2a 2a 0a 2a 2a 20 57 41 52 4e 49 4e 47 3a 20 20  **.** WARNING:  
2d296 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 67  This routine mig
2d297 68 74 20 72 65 61 6c 6c 6f 63 61 74 65 20 74 68  ht reallocate th
2d298 65 20 73 70 61 63 65 20 75 73 65 64 20 74 6f 20  e space used to 
2d299 73 74 6f 72 65 0a 2a 2a 20 57 68 65 72 65 54 65  store.** WhereTe
2d29a 72 6d 73 2e 20 20 41 6c 6c 20 70 6f 69 6e 74 65  rms.  All pointe
2d29b 72 73 20 74 6f 20 57 68 65 72 65 54 65 72 6d 73  rs to WhereTerms
2d29c 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 76 61 6c   should be inval
2d29d 69 64 61 74 65 64 20 61 66 74 65 72 0a 2a 2a 20  idated after.** 
2d29e 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75  calling this rou
2d29f 74 69 6e 65 2e 20 20 53 75 63 68 20 70 6f 69 6e  tine.  Such poin
2d2a0 74 65 72 73 20 6d 61 79 20 62 65 20 72 65 69 6e  ters may be rein
2d2a1 69 74 69 61 6c 69 7a 65 64 20 62 79 20 72 65 66  itialized by ref
2d2a2 65 72 65 6e 63 69 6e 67 0a 2a 2a 20 74 68 65 20  erencing.** the 
2d2a3 70 57 43 2d 3e 61 5b 5d 20 61 72 72 61 79 2e 0a  pWC->a[] array..
2d2a4 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 68  */.static int wh
2d2a5 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72 74 28  ereClauseInsert(
2d2a6 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43  WhereClause *pWC
2d2a7 2c 20 45 78 70 72 20 2a 70 2c 20 75 38 20 77 74  , Expr *p, u8 wt
2d2a8 46 6c 61 67 73 29 7b 0a 20 20 57 68 65 72 65 54  Flags){.  WhereT
2d2a9 65 72 6d 20 2a 70 54 65 72 6d 3b 0a 20 20 69 6e  erm *pTerm;.  in
2d2aa 74 20 69 64 78 3b 0a 20 20 69 66 28 20 70 57 43  t idx;.  if( pWC
2d2ab 2d 3e 6e 54 65 72 6d 3e 3d 70 57 43 2d 3e 6e 53  ->nTerm>=pWC->nS
2d2ac 6c 6f 74 20 29 7b 0a 20 20 20 20 57 68 65 72 65  lot ){.    Where
2d2ad 54 65 72 6d 20 2a 70 4f 6c 64 20 3d 20 70 57 43  Term *pOld = pWC
2d2ae 2d 3e 61 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ->a;.    sqlite3
2d2af 20 2a 64 62 20 3d 20 70 57 43 2d 3e 70 50 61 72   *db = pWC->pPar
2d2b0 73 65 2d 3e 64 62 3b 0a 20 20 20 20 70 57 43 2d  se->db;.    pWC-
2d2b1 3e 61 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  >a = sqlite3DbMa
2d2b2 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65  llocRaw(db, size
2d2b3 6f 66 28 70 57 43 2d 3e 61 5b 30 5d 29 2a 70 57  of(pWC->a[0])*pW
2d2b4 43 2d 3e 6e 53 6c 6f 74 2a 32 20 29 3b 0a 20 20  C->nSlot*2 );.  
2d2b5 20 20 69 66 28 20 70 57 43 2d 3e 61 3d 3d 30 20    if( pWC->a==0 
2d2b6 29 7b 0a 20 20 20 20 20 20 69 66 28 20 77 74 46  ){.      if( wtF
2d2b7 6c 61 67 73 20 26 20 54 45 52 4d 5f 44 59 4e 41  lags & TERM_DYNA
2d2b8 4d 49 43 20 29 7b 0a 20 20 20 20 20 20 20 20 73  MIC ){.        s
2d2b9 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
2d2ba 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 20 20 7d  (db, p);.      }
2d2bb 0a 20 20 20 20 20 20 70 57 43 2d 3e 61 20 3d 20  .      pWC->a = 
2d2bc 70 4f 6c 64 3b 0a 20 20 20 20 20 20 72 65 74 75  pOld;.      retu
2d2bd 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 0;.    }.    
2d2be 6d 65 6d 63 70 79 28 70 57 43 2d 3e 61 2c 20 70  memcpy(pWC->a, p
2d2bf 4f 6c 64 2c 20 73 69 7a 65 6f 66 28 70 57 43 2d  Old, sizeof(pWC-
2d2c0 3e 61 5b 30 5d 29 2a 70 57 43 2d 3e 6e 54 65 72  >a[0])*pWC->nTer
2d2c1 6d 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 6c 64  m);.    if( pOld
2d2c2 21 3d 70 57 43 2d 3e 61 53 74 61 74 69 63 20 29  !=pWC->aStatic )
2d2c3 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  {.      sqlite3D
2d2c4 62 46 72 65 65 28 64 62 2c 20 70 4f 6c 64 29 3b  bFree(db, pOld);
2d2c5 0a 20 20 20 20 7d 0a 20 20 20 20 70 57 43 2d 3e  .    }.    pWC->
2d2c6 6e 53 6c 6f 74 20 3d 20 73 71 6c 69 74 65 33 44  nSlot = sqlite3D
2d2c7 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20  bMallocSize(db, 
2d2c8 70 57 43 2d 3e 61 29 2f 73 69 7a 65 6f 66 28 70  pWC->a)/sizeof(p
2d2c9 57 43 2d 3e 61 5b 30 5d 29 3b 0a 20 20 7d 0a 20  WC->a[0]);.  }. 
2d2ca 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61   pTerm = &pWC->a
2d2cb 5b 69 64 78 20 3d 20 70 57 43 2d 3e 6e 54 65 72  [idx = pWC->nTer
2d2cc 6d 2b 2b 5d 3b 0a 20 20 70 54 65 72 6d 2d 3e 70  m++];.  pTerm->p
2d2cd 45 78 70 72 20 3d 20 70 3b 0a 20 20 70 54 65 72  Expr = p;.  pTer
2d2ce 6d 2d 3e 77 74 46 6c 61 67 73 20 3d 20 77 74 46  m->wtFlags = wtF
2d2cf 6c 61 67 73 3b 0a 20 20 70 54 65 72 6d 2d 3e 70  lags;.  pTerm->p
2d2d0 57 43 20 3d 20 70 57 43 3b 0a 20 20 70 54 65 72  WC = pWC;.  pTer
2d2d1 6d 2d 3e 69 50 61 72 65 6e 74 20 3d 20 2d 31 3b  m->iParent = -1;
2d2d2 0a 20 20 72 65 74 75 72 6e 20 69 64 78 3b 0a 7d  .  return idx;.}
2d2d3 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
2d2d4 74 69 6e 65 20 69 64 65 6e 74 69 66 69 65 73 20  tine identifies 
2d2d5 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73 20 69  subexpressions i
2d2d6 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  n the WHERE clau
2d2d7 73 65 20 77 68 65 72 65 0a 2a 2a 20 65 61 63 68  se where.** each
2d2d8 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20 69   subexpression i
2d2d9 73 20 73 65 70 61 72 61 74 65 64 20 62 79 20 74  s separated by t
2d2da 68 65 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 20  he AND operator 
2d2db 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a  or some other.**
2d2dc 20 6f 70 65 72 61 74 6f 72 20 73 70 65 63 69 66   operator specif
2d2dd 69 65 64 20 69 6e 20 74 68 65 20 6f 70 20 70 61  ied in the op pa
2d2de 72 61 6d 65 74 65 72 2e 20 20 54 68 65 20 57 68  rameter.  The Wh
2d2df 65 72 65 43 6c 61 75 73 65 20 73 74 72 75 63 74  ereClause struct
2d2e0 75 72 65 0a 2a 2a 20 69 73 20 66 69 6c 6c 65 64  ure.** is filled
2d2e1 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74   with pointers t
2d2e2 6f 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73  o subexpressions
2d2e3 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a  .  For example:.
2d2e4 2a 2a 0a 2a 2a 20 20 20 20 57 48 45 52 45 20 20  **.**    WHERE  
2d2e5 61 3d 3d 27 68 65 6c 6c 6f 27 20 41 4e 44 20 63  a=='hello' AND c
2d2e6 6f 61 6c 65 73 63 65 28 62 2c 31 31 29 3c 31 30  oalesce(b,11)<10
2d2e7 20 41 4e 44 20 28 63 2b 31 32 21 3d 64 20 4f 52   AND (c+12!=d OR
2d2e8 20 63 3d 3d 32 32 29 0a 2a 2a 20 20 20 20 20 20   c==22).**      
2d2e9 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 2f 20       \________/ 
2d2ea 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f      \___________
2d2eb 5f 5f 5f 5f 2f 20 20 20 20 20 5c 5f 5f 5f 5f 5f  ____/     \_____
2d2ec 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a 2a 20  ___________/.** 
2d2ed 20 20 20 20 20 20 20 20 20 20 20 73 6c 6f 74 5b             slot[
2d2ee 30 5d 20 20 20 20 20 20 20 20 20 20 20 20 73 6c  0]            sl
2d2ef 6f 74 5b 31 5d 20 20 20 20 20 20 20 20 20 20 20  ot[1]           
2d2f0 20 20 20 20 73 6c 6f 74 5b 32 5d 0a 2a 2a 0a 2a      slot[2].**.*
2d2f1 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 57  * The original W
2d2f2 48 45 52 45 20 63 6c 61 75 73 65 20 69 6e 20 70  HERE clause in p
2d2f3 45 78 70 72 20 69 73 20 75 6e 61 6c 74 65 72 65  Expr is unaltere
2d2f4 64 2e 20 20 41 6c 6c 20 74 68 69 73 20 72 6f 75  d.  All this rou
2d2f5 74 69 6e 65 0a 2a 2a 20 64 6f 65 73 20 69 73 20  tine.** does is 
2d2f6 6d 61 6b 65 20 73 6c 6f 74 5b 5d 20 65 6e 74 72  make slot[] entr
2d2f7 69 65 73 20 70 6f 69 6e 74 20 74 6f 20 73 75 62  ies point to sub
2d2f8 73 74 72 75 63 74 75 72 65 20 77 69 74 68 69 6e  structure within
2d2f9 20 70 45 78 70 72 2e 0a 2a 2a 0a 2a 2a 20 49 6e   pExpr..**.** In
2d2fa 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 65   the previous se
2d2fb 6e 74 65 6e 63 65 20 61 6e 64 20 69 6e 20 74 68  ntence and in th
2d2fc 65 20 64 69 61 67 72 61 6d 2c 20 22 73 6c 6f 74  e diagram, "slot
2d2fd 5b 5d 22 20 72 65 66 65 72 73 20 74 6f 0a 2a 2a  []" refers to.**
2d2fe 20 74 68 65 20 57 68 65 72 65 43 6c 61 75 73 65   the WhereClause
2d2ff 2e 61 5b 5d 20 61 72 72 61 79 2e 20 20 54 68 65  .a[] array.  The
2d300 20 73 6c 6f 74 5b 5d 20 61 72 72 61 79 20 67 72   slot[] array gr
2d301 6f 77 73 20 61 73 20 6e 65 65 64 65 64 20 74 6f  ows as needed to
2d302 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20 61 6c 6c 20   contain.** all 
2d303 74 65 72 6d 73 20 6f 66 20 74 68 65 20 57 48 45  terms of the WHE
2d304 52 45 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a 73 74  RE clause..*/.st
2d305 61 74 69 63 20 76 6f 69 64 20 77 68 65 72 65 53  atic void whereS
2d306 70 6c 69 74 28 57 68 65 72 65 43 6c 61 75 73 65  plit(WhereClause
2d307 20 2a 70 57 43 2c 20 45 78 70 72 20 2a 70 45 78   *pWC, Expr *pEx
2d308 70 72 2c 20 69 6e 74 20 6f 70 29 7b 0a 20 20 70  pr, int op){.  p
2d309 57 43 2d 3e 6f 70 20 3d 20 28 75 38 29 6f 70 3b  WC->op = (u8)op;
2d30a 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20  .  if( pExpr==0 
2d30b 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
2d30c 70 45 78 70 72 2d 3e 6f 70 21 3d 6f 70 20 29 7b  pExpr->op!=op ){
2d30d 0a 20 20 20 20 77 68 65 72 65 43 6c 61 75 73 65  .    whereClause
2d30e 49 6e 73 65 72 74 28 70 57 43 2c 20 70 45 78 70  Insert(pWC, pExp
2d30f 72 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  r, 0);.  }else{.
2d310 20 20 20 20 77 68 65 72 65 53 70 6c 69 74 28 70      whereSplit(p
2d311 57 43 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  WC, pExpr->pLeft
2d312 2c 20 6f 70 29 3b 0a 20 20 20 20 77 68 65 72 65  , op);.    where
2d313 53 70 6c 69 74 28 70 57 43 2c 20 70 45 78 70 72  Split(pWC, pExpr
2d314 2d 3e 70 52 69 67 68 74 2c 20 6f 70 29 3b 0a 20  ->pRight, op);. 
2d315 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74   }.}../*.** Init
2d316 69 61 6c 69 7a 65 20 61 6e 20 65 78 70 72 65 73  ialize an expres
2d317 73 69 6f 6e 20 6d 61 73 6b 20 73 65 74 20 28 61  sion mask set (a
2d318 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20 6f 62   WhereMaskSet ob
2d319 6a 65 63 74 29 0a 2a 2f 0a 23 64 65 66 69 6e 65  ject).*/.#define
2d31a 20 69 6e 69 74 4d 61 73 6b 53 65 74 28 50 29 20   initMaskSet(P) 
2d31b 20 6d 65 6d 73 65 74 28 50 2c 20 30 2c 20 73 69   memset(P, 0, si
2d31c 7a 65 6f 66 28 2a 50 29 29 0a 0a 2f 2a 0a 2a 2a  zeof(*P))../*.**
2d31d 20 52 65 74 75 72 6e 20 74 68 65 20 62 69 74 6d   Return the bitm
2d31e 61 73 6b 20 66 6f 72 20 74 68 65 20 67 69 76 65  ask for the give
2d31f 6e 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 2e  n cursor number.
2d320 20 20 52 65 74 75 72 6e 20 30 20 69 66 0a 2a 2a    Return 0 if.**
2d321 20 69 43 75 72 73 6f 72 20 69 73 20 6e 6f 74 20   iCursor is not 
2d322 69 6e 20 74 68 65 20 73 65 74 2e 0a 2a 2f 0a 73  in the set..*/.s
2d323 74 61 74 69 63 20 42 69 74 6d 61 73 6b 20 67 65  tatic Bitmask ge
2d324 74 4d 61 73 6b 28 57 68 65 72 65 4d 61 73 6b 53  tMask(WhereMaskS
2d325 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 69 6e  et *pMaskSet, in
2d326 74 20 69 43 75 72 73 6f 72 29 7b 0a 20 20 69 6e  t iCursor){.  in
2d327 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 70  t i;.  assert( p
2d328 4d 61 73 6b 53 65 74 2d 3e 6e 3c 3d 73 69 7a 65  MaskSet->n<=size
2d329 6f 66 28 42 69 74 6d 61 73 6b 29 2a 38 20 29 3b  of(Bitmask)*8 );
2d32a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4d  .  for(i=0; i<pM
2d32b 61 73 6b 53 65 74 2d 3e 6e 3b 20 69 2b 2b 29 7b  askSet->n; i++){
2d32c 0a 20 20 20 20 69 66 28 20 70 4d 61 73 6b 53 65  .    if( pMaskSe
2d32d 74 2d 3e 69 78 5b 69 5d 3d 3d 69 43 75 72 73 6f  t->ix[i]==iCurso
2d32e 72 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  r ){.      retur
2d32f 6e 20 28 28 42 69 74 6d 61 73 6b 29 31 29 3c 3c  n ((Bitmask)1)<<
2d330 69 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  i;.    }.  }.  r
2d331 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
2d332 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 6d  * Create a new m
2d333 61 73 6b 20 66 6f 72 20 63 75 72 73 6f 72 20 69  ask for cursor i
2d334 43 75 72 73 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  Cursor..**.** Th
2d335 65 72 65 20 69 73 20 6f 6e 65 20 63 75 72 73 6f  ere is one curso
2d336 72 20 70 65 72 20 74 61 62 6c 65 20 69 6e 20 74  r per table in t
2d337 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20  he FROM clause. 
2d338 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a   The number of.*
2d339 2a 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20  * tables in the 
2d33a 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 6c  FROM clause is l
2d33b 69 6d 69 74 65 64 20 62 79 20 61 20 74 65 73 74  imited by a test
2d33c 20 65 61 72 6c 79 20 69 6e 20 74 68 65 0a 2a 2a   early in the.**
2d33d 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67   sqlite3WhereBeg
2d33e 69 6e 28 29 20 72 6f 75 74 69 6e 65 2e 20 20 53  in() routine.  S
2d33f 6f 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 74  o we know that t
2d340 68 65 20 70 4d 61 73 6b 53 65 74 2d 3e 69 78 5b  he pMaskSet->ix[
2d341 5d 0a 2a 2a 20 61 72 72 61 79 20 77 69 6c 6c 20  ].** array will 
2d342 6e 65 76 65 72 20 6f 76 65 72 66 6c 6f 77 2e 0a  never overflow..
2d343 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
2d344 72 65 61 74 65 4d 61 73 6b 28 57 68 65 72 65 4d  reateMask(WhereM
2d345 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74  askSet *pMaskSet
2d346 2c 20 69 6e 74 20 69 43 75 72 73 6f 72 29 7b 0a  , int iCursor){.
2d347 20 20 61 73 73 65 72 74 28 20 70 4d 61 73 6b 53    assert( pMaskS
2d348 65 74 2d 3e 6e 20 3c 20 41 72 72 61 79 53 69 7a  et->n < ArraySiz
2d349 65 28 70 4d 61 73 6b 53 65 74 2d 3e 69 78 29 20  e(pMaskSet->ix) 
2d34a 29 3b 0a 20 20 70 4d 61 73 6b 53 65 74 2d 3e 69  );.  pMaskSet->i
2d34b 78 5b 70 4d 61 73 6b 53 65 74 2d 3e 6e 2b 2b 5d  x[pMaskSet->n++]
2d34c 20 3d 20 69 43 75 72 73 6f 72 3b 0a 7d 0a 0a 2f   = iCursor;.}../
2d34d 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
2d34e 65 20 77 61 6c 6b 73 20 28 72 65 63 75 72 73 69  e walks (recursi
2d34f 76 65 6c 79 29 20 61 6e 20 65 78 70 72 65 73 73  vely) an express
2d350 69 6f 6e 20 74 72 65 65 20 61 6e 64 20 67 65 6e  ion tree and gen
2d351 65 72 61 74 65 73 0a 2a 2a 20 61 20 62 69 74 6d  erates.** a bitm
2d352 61 73 6b 20 69 6e 64 69 63 61 74 69 6e 67 20 77  ask indicating w
2d353 68 69 63 68 20 74 61 62 6c 65 73 20 61 72 65 20  hich tables are 
2d354 75 73 65 64 20 69 6e 20 74 68 61 74 20 65 78 70  used in that exp
2d355 72 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 65 2e  ression.** tree.
2d356 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 72 64 65 72 20  .**.** In order 
2d357 66 6f 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65  for this routine
2d358 20 74 6f 20 77 6f 72 6b 2c 20 74 68 65 20 63 61   to work, the ca
2d359 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6d  lling function m
2d35a 75 73 74 20 68 61 76 65 0a 2a 2a 20 70 72 65 76  ust have.** prev
2d35b 69 6f 75 73 6c 79 20 69 6e 76 6f 6b 65 64 20 73  iously invoked s
2d35c 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70  qlite3ResolveExp
2d35d 72 4e 61 6d 65 73 28 29 20 6f 6e 20 74 68 65 20  rNames() on the 
2d35e 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 53 65 65  expression.  See
2d35f 0a 2a 2a 20 74 68 65 20 68 65 61 64 65 72 20 63  .** the header c
2d360 6f 6d 6d 65 6e 74 20 6f 6e 20 74 68 61 74 20 72  omment on that r
2d361 6f 75 74 69 6e 65 20 66 6f 72 20 61 64 64 69 74  outine for addit
2d362 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
2d363 6e 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  n..** The sqlite
2d364 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65  3ResolveExprName
2d365 73 28 29 20 72 6f 75 74 69 6e 65 73 20 6c 6f 6f  s() routines loo
2d366 6b 73 20 66 6f 72 20 63 6f 6c 75 6d 6e 20 6e 61  ks for column na
2d367 6d 65 73 20 61 6e 64 0a 2a 2a 20 73 65 74 73 20  mes and.** sets 
2d368 74 68 65 69 72 20 6f 70 63 6f 64 65 73 20 74 6f  their opcodes to
2d369 20 54 4b 5f 43 4f 4c 55 4d 4e 20 61 6e 64 20 74   TK_COLUMN and t
2d36a 68 65 69 72 20 45 78 70 72 2e 69 54 61 62 6c 65  heir Expr.iTable
2d36b 20 66 69 65 6c 64 73 20 74 6f 0a 2a 2a 20 74 68   fields to.** th
2d36c 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75  e VDBE cursor nu
2d36d 6d 62 65 72 20 6f 66 20 74 68 65 20 74 61 62 6c  mber of the tabl
2d36e 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  e.  This routine
2d36f 20 6a 75 73 74 20 68 61 73 20 74 6f 0a 2a 2a 20   just has to.** 
2d370 74 72 61 6e 73 6c 61 74 65 20 74 68 65 20 63 75  translate the cu
2d371 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 69 6e 74  rsor numbers int
2d372 6f 20 62 69 74 6d 61 73 6b 20 76 61 6c 75 65 73  o bitmask values
2d373 20 61 6e 64 20 4f 52 20 61 6c 6c 0a 2a 2a 20 74   and OR all.** t
2d374 68 65 20 62 69 74 6d 61 73 6b 73 20 74 6f 67 65  he bitmasks toge
2d375 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ther..*/.static 
2d376 42 69 74 6d 61 73 6b 20 65 78 70 72 4c 69 73 74  Bitmask exprList
2d377 54 61 62 6c 65 55 73 61 67 65 28 57 68 65 72 65  TableUsage(Where
2d378 4d 61 73 6b 53 65 74 2a 2c 20 45 78 70 72 4c 69  MaskSet*, ExprLi
2d379 73 74 2a 29 3b 0a 73 74 61 74 69 63 20 42 69 74  st*);.static Bit
2d37a 6d 61 73 6b 20 65 78 70 72 53 65 6c 65 63 74 54  mask exprSelectT
2d37b 61 62 6c 65 55 73 61 67 65 28 57 68 65 72 65 4d  ableUsage(WhereM
2d37c 61 73 6b 53 65 74 2a 2c 20 53 65 6c 65 63 74 2a  askSet*, Select*
2d37d 29 3b 0a 73 74 61 74 69 63 20 42 69 74 6d 61 73  );.static Bitmas
2d37e 6b 20 65 78 70 72 54 61 62 6c 65 55 73 61 67 65  k exprTableUsage
2d37f 28 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70  (WhereMaskSet *p
2d380 4d 61 73 6b 53 65 74 2c 20 45 78 70 72 20 2a 70  MaskSet, Expr *p
2d381 29 7b 0a 20 20 42 69 74 6d 61 73 6b 20 6d 61 73  ){.  Bitmask mas
2d382 6b 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 3d 3d  k = 0;.  if( p==
2d383 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
2d384 69 66 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f  if( p->op==TK_CO
2d385 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 6d 61 73 6b  LUMN ){.    mask
2d386 20 3d 20 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b   = getMask(pMask
2d387 53 65 74 2c 20 70 2d 3e 69 54 61 62 6c 65 29 3b  Set, p->iTable);
2d388 0a 20 20 20 20 72 65 74 75 72 6e 20 6d 61 73 6b  .    return mask
2d389 3b 0a 20 20 7d 0a 20 20 6d 61 73 6b 20 3d 20 65  ;.  }.  mask = e
2d38a 78 70 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d  xprTableUsage(pM
2d38b 61 73 6b 53 65 74 2c 20 70 2d 3e 70 52 69 67 68  askSet, p->pRigh
2d38c 74 29 3b 0a 20 20 6d 61 73 6b 20 7c 3d 20 65 78  t);.  mask |= ex
2d38d 70 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61  prTableUsage(pMa
2d38e 73 6b 53 65 74 2c 20 70 2d 3e 70 4c 65 66 74 29  skSet, p->pLeft)
2d38f 3b 0a 20 20 69 66 28 20 45 78 70 72 48 61 73 50  ;.  if( ExprHasP
2d390 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 78 49  roperty(p, EP_xI
2d391 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20  sSelect) ){.    
2d392 6d 61 73 6b 20 7c 3d 20 65 78 70 72 53 65 6c 65  mask |= exprSele
2d393 63 74 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61  ctTableUsage(pMa
2d394 73 6b 53 65 74 2c 20 70 2d 3e 78 2e 70 53 65 6c  skSet, p->x.pSel
2d395 65 63 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ect);.  }else{. 
2d396 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 4c     mask |= exprL
2d397 69 73 74 54 61 62 6c 65 55 73 61 67 65 28 70 4d  istTableUsage(pM
2d398 61 73 6b 53 65 74 2c 20 70 2d 3e 78 2e 70 4c 69  askSet, p->x.pLi
2d399 73 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  st);.  }.  retur
2d39a 6e 20 6d 61 73 6b 3b 0a 7d 0a 73 74 61 74 69 63  n mask;.}.static
2d39b 20 42 69 74 6d 61 73 6b 20 65 78 70 72 4c 69 73   Bitmask exprLis
2d39c 74 54 61 62 6c 65 55 73 61 67 65 28 57 68 65 72  tTableUsage(Wher
2d39d 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53  eMaskSet *pMaskS
2d39e 65 74 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 4c  et, ExprList *pL
2d39f 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  ist){.  int i;. 
2d3a0 20 42 69 74 6d 61 73 6b 20 6d 61 73 6b 20 3d 20   Bitmask mask = 
2d3a1 30 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 20 29  0;.  if( pList )
2d3a2 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  {.    for(i=0; i
2d3a3 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69  <pList->nExpr; i
2d3a4 2b 2b 29 7b 0a 20 20 20 20 20 20 6d 61 73 6b 20  ++){.      mask 
2d3a5 7c 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61 67  |= exprTableUsag
2d3a6 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 4c 69 73  e(pMaskSet, pLis
2d3a7 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 3b 0a  t->a[i].pExpr);.
2d3a8 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
2d3a9 72 6e 20 6d 61 73 6b 3b 0a 7d 0a 73 74 61 74 69  rn mask;.}.stati
2d3aa 63 20 42 69 74 6d 61 73 6b 20 65 78 70 72 53 65  c Bitmask exprSe
2d3ab 6c 65 63 74 54 61 62 6c 65 55 73 61 67 65 28 57  lectTableUsage(W
2d3ac 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61  hereMaskSet *pMa
2d3ad 73 6b 53 65 74 2c 20 53 65 6c 65 63 74 20 2a 70  skSet, Select *p
2d3ae 53 29 7b 0a 20 20 42 69 74 6d 61 73 6b 20 6d 61  S){.  Bitmask ma
2d3af 73 6b 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28  sk = 0;.  while(
2d3b0 20 70 53 20 29 7b 0a 20 20 20 20 6d 61 73 6b 20   pS ){.    mask 
2d3b1 7c 3d 20 65 78 70 72 4c 69 73 74 54 61 62 6c 65  |= exprListTable
2d3b2 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20  Usage(pMaskSet, 
2d3b3 70 53 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20  pS->pEList);.   
2d3b4 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 4c 69 73   mask |= exprLis
2d3b5 74 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73  tTableUsage(pMas
2d3b6 6b 53 65 74 2c 20 70 53 2d 3e 70 47 72 6f 75 70  kSet, pS->pGroup
2d3b7 42 79 29 3b 0a 20 20 20 20 6d 61 73 6b 20 7c 3d  By);.    mask |=
2d3b8 20 65 78 70 72 4c 69 73 74 54 61 62 6c 65 55 73   exprListTableUs
2d3b9 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 53  age(pMaskSet, pS
2d3ba 2d 3e 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 20  ->pOrderBy);.   
2d3bb 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 54 61 62   mask |= exprTab
2d3bc 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74  leUsage(pMaskSet
2d3bd 2c 20 70 53 2d 3e 70 57 68 65 72 65 29 3b 0a 20  , pS->pWhere);. 
2d3be 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 54     mask |= exprT
2d3bf 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53  ableUsage(pMaskS
2d3c0 65 74 2c 20 70 53 2d 3e 70 48 61 76 69 6e 67 29  et, pS->pHaving)
2d3c1 3b 0a 20 20 20 20 70 53 20 3d 20 70 53 2d 3e 70  ;.    pS = pS->p
2d3c2 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 72 65 74  Prior;.  }.  ret
2d3c3 75 72 6e 20 6d 61 73 6b 3b 0a 7d 0a 0a 2f 2a 0a  urn mask;.}../*.
2d3c4 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69  ** Return TRUE i
2d3c5 66 20 74 68 65 20 67 69 76 65 6e 20 6f 70 65 72  f the given oper
2d3c6 61 74 6f 72 20 69 73 20 6f 6e 65 20 6f 66 20 74  ator is one of t
2d3c7 68 65 20 6f 70 65 72 61 74 6f 72 73 20 74 68 61  he operators tha
2d3c8 74 20 69 73 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20  t is.** allowed 
2d3c9 66 6f 72 20 61 6e 20 69 6e 64 65 78 61 62 6c 65  for an indexable
2d3ca 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65   WHERE clause te
2d3cb 72 6d 2e 20 20 54 68 65 20 61 6c 6c 6f 77 65 64  rm.  The allowed
2d3cc 20 6f 70 65 72 61 74 6f 72 73 20 61 72 65 0a 2a   operators are.*
2d3cd 2a 20 22 3d 22 2c 20 22 3c 22 2c 20 22 3e 22 2c  * "=", "<", ">",
2d3ce 20 22 3c 3d 22 2c 20 22 3e 3d 22 2c 20 61 6e 64   "<=", ">=", and
2d3cf 20 22 49 4e 22 2e 0a 2a 2f 0a 73 74 61 74 69 63   "IN"..*/.static
2d3d0 20 69 6e 74 20 61 6c 6c 6f 77 65 64 4f 70 28 69   int allowedOp(i
2d3d1 6e 74 20 6f 70 29 7b 0a 20 20 61 73 73 65 72 74  nt op){.  assert
2d3d2 28 20 54 4b 5f 47 54 3e 54 4b 5f 45 51 20 26 26  ( TK_GT>TK_EQ &&
2d3d3 20 54 4b 5f 47 54 3c 54 4b 5f 47 45 20 29 3b 0a   TK_GT<TK_GE );.
2d3d4 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 54 3e    assert( TK_LT>
2d3d5 54 4b 5f 45 51 20 26 26 20 54 4b 5f 4c 54 3c 54  TK_EQ && TK_LT<T
2d3d6 4b 5f 47 45 20 29 3b 0a 20 20 61 73 73 65 72 74  K_GE );.  assert
2d3d7 28 20 54 4b 5f 4c 45 3e 54 4b 5f 45 51 20 26 26  ( TK_LE>TK_EQ &&
2d3d8 20 54 4b 5f 4c 45 3c 54 4b 5f 47 45 20 29 3b 0a   TK_LE<TK_GE );.
2d3d9 20 20 61 73 73 65 72 74 28 20 54 4b 5f 47 45 3d    assert( TK_GE=
2d3da 3d 54 4b 5f 45 51 2b 34 20 29 3b 0a 20 20 72 65  =TK_EQ+4 );.  re
2d3db 74 75 72 6e 20 6f 70 3d 3d 54 4b 5f 49 4e 20 7c  turn op==TK_IN |
2d3dc 7c 20 28 6f 70 3e 3d 54 4b 5f 45 51 20 26 26 20  | (op>=TK_EQ && 
2d3dd 6f 70 3c 3d 54 4b 5f 47 45 29 20 7c 7c 20 6f 70  op<=TK_GE) || op
2d3de 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 3b 0a 7d 0a 0a  ==TK_ISNULL;.}..
2d3df 2f 2a 0a 2a 2a 20 53 77 61 70 20 74 77 6f 20 6f  /*.** Swap two o
2d3e0 62 6a 65 63 74 73 20 6f 66 20 74 79 70 65 20 54  bjects of type T
2d3e1 59 50 45 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  YPE..*/.#define 
2d3e2 53 57 41 50 28 54 59 50 45 2c 41 2c 42 29 20 7b  SWAP(TYPE,A,B) {
2d3e3 54 59 50 45 20 74 3d 41 3b 20 41 3d 42 3b 20 42  TYPE t=A; A=B; B
2d3e4 3d 74 3b 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d  =t;}../*.** Comm
2d3e5 75 74 65 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e  ute a comparison
2d3e6 20 6f 70 65 72 61 74 6f 72 2e 20 20 45 78 70 72   operator.  Expr
2d3e7 65 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 66  essions of the f
2d3e8 6f 72 6d 20 22 58 20 6f 70 20 59 22 0a 2a 2a 20  orm "X op Y".** 
2d3e9 61 72 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e  are converted in
2d3ea 74 6f 20 22 59 20 6f 70 20 58 22 2e 0a 2a 2a 0a  to "Y op X"..**.
2d3eb 2a 2a 20 49 66 20 61 20 63 6f 6c 6c 61 74 69 6f  ** If a collatio
2d3ec 6e 20 73 65 71 75 65 6e 63 65 20 69 73 20 61 73  n sequence is as
2d3ed 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 65 69  sociated with ei
2d3ee 74 68 65 72 20 74 68 65 20 6c 65 66 74 20 6f 72  ther the left or
2d3ef 20 72 69 67 68 74 0a 2a 2a 20 73 69 64 65 20 6f   right.** side o
2d3f0 66 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e  f the comparison
2d3f1 2c 20 69 74 20 72 65 6d 61 69 6e 73 20 61 73 73  , it remains ass
2d3f2 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
2d3f3 20 73 61 6d 65 20 73 69 64 65 20 61 66 74 65 72   same side after
2d3f4 0a 2a 2a 20 74 68 65 20 63 6f 6d 6d 75 74 61 74  .** the commutat
2d3f5 69 6f 6e 2e 20 53 6f 20 22 59 20 63 6f 6c 6c 61  ion. So "Y colla
2d3f6 74 65 20 4e 4f 43 41 53 45 20 6f 70 20 58 22 20  te NOCASE op X" 
2d3f7 62 65 63 6f 6d 65 73 20 0a 2a 2a 20 22 58 20 63  becomes .** "X c
2d3f8 6f 6c 6c 61 74 65 20 4e 4f 43 41 53 45 20 6f 70  ollate NOCASE op
2d3f9 20 59 22 2e 20 54 68 69 73 20 69 73 20 62 65 63   Y". This is bec
2d3fa 61 75 73 65 20 61 6e 79 20 63 6f 6c 6c 61 74 69  ause any collati
2d3fb 6f 6e 20 73 65 71 75 65 6e 63 65 20 6f 6e 0a 2a  on sequence on.*
2d3fc 2a 20 74 68 65 20 6c 65 66 74 20 68 61 6e 64 20  * the left hand 
2d3fd 73 69 64 65 20 6f 66 20 61 20 63 6f 6d 70 61 72  side of a compar
2d3fe 69 73 6f 6e 20 6f 76 65 72 72 69 64 65 73 20 61  ison overrides a
2d3ff 6e 79 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  ny collation seq
2d400 75 65 6e 63 65 20 0a 2a 2a 20 61 74 74 61 63 68  uence .** attach
2d401 65 64 20 74 6f 20 74 68 65 20 72 69 67 68 74 2e  ed to the right.
2d402 20 46 6f 72 20 74 68 65 20 73 61 6d 65 20 72 65   For the same re
2d403 61 73 6f 6e 20 74 68 65 20 45 50 5f 45 78 70 43  ason the EP_ExpC
2d404 6f 6c 6c 61 74 65 20 66 6c 61 67 0a 2a 2a 20 69  ollate flag.** i
2d405 73 20 6e 6f 74 20 63 6f 6d 6d 75 74 65 64 2e 0a  s not commuted..
2d406 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 65  */.static void e
2d407 78 70 72 43 6f 6d 6d 75 74 65 28 50 61 72 73 65  xprCommute(Parse
2d408 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a   *pParse, Expr *
2d409 70 45 78 70 72 29 7b 0a 20 20 75 31 36 20 65 78  pExpr){.  u16 ex
2d40a 70 52 69 67 68 74 20 3d 20 28 70 45 78 70 72 2d  pRight = (pExpr-
2d40b 3e 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 26  >pRight->flags &
2d40c 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 29 3b   EP_ExpCollate);
2d40d 0a 20 20 75 31 36 20 65 78 70 4c 65 66 74 20 3d  .  u16 expLeft =
2d40e 20 28 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e   (pExpr->pLeft->
2d40f 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f  flags & EP_ExpCo
2d410 6c 6c 61 74 65 29 3b 0a 20 20 61 73 73 65 72 74  llate);.  assert
2d411 28 20 61 6c 6c 6f 77 65 64 4f 70 28 70 45 78 70  ( allowedOp(pExp
2d412 72 2d 3e 6f 70 29 20 26 26 20 70 45 78 70 72 2d  r->op) && pExpr-
2d413 3e 6f 70 21 3d 54 4b 5f 49 4e 20 29 3b 0a 20 20  >op!=TK_IN );.  
2d414 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2d 3e 70  pExpr->pRight->p
2d415 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78  Coll = sqlite3Ex
2d416 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65  prCollSeq(pParse
2d417 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29  , pExpr->pRight)
2d418 3b 0a 20 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  ;.  pExpr->pLeft
2d419 2d 3e 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65  ->pColl = sqlite
2d41a 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61  3ExprCollSeq(pPa
2d41b 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
2d41c 74 29 3b 0a 20 20 53 57 41 50 28 43 6f 6c 6c 53  t);.  SWAP(CollS
2d41d 65 71 2a 2c 70 45 78 70 72 2d 3e 70 52 69 67 68  eq*,pExpr->pRigh
2d41e 74 2d 3e 70 43 6f 6c 6c 2c 70 45 78 70 72 2d 3e  t->pColl,pExpr->
2d41f 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 29 3b 0a 20  pLeft->pColl);. 
2d420 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2d 3e   pExpr->pRight->
2d421 66 6c 61 67 73 20 3d 20 28 70 45 78 70 72 2d 3e  flags = (pExpr->
2d422 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 26 20  pRight->flags & 
2d423 7e 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 29 20  ~EP_ExpCollate) 
2d424 7c 20 65 78 70 4c 65 66 74 3b 0a 20 20 70 45 78  | expLeft;.  pEx
2d425 70 72 2d 3e 70 4c 65 66 74 2d 3e 66 6c 61 67 73  pr->pLeft->flags
2d426 20 3d 20 28 70 45 78 70 72 2d 3e 70 4c 65 66 74   = (pExpr->pLeft
2d427 2d 3e 66 6c 61 67 73 20 26 20 7e 45 50 5f 45 78  ->flags & ~EP_Ex
2d428 70 43 6f 6c 6c 61 74 65 29 20 7c 20 65 78 70 52  pCollate) | expR
2d429 69 67 68 74 3b 0a 20 20 53 57 41 50 28 45 78 70  ight;.  SWAP(Exp
2d42a 72 2a 2c 70 45 78 70 72 2d 3e 70 52 69 67 68 74  r*,pExpr->pRight
2d42b 2c 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a  ,pExpr->pLeft);.
2d42c 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3e    if( pExpr->op>
2d42d 3d 54 4b 5f 47 54 20 29 7b 0a 20 20 20 20 61 73  =TK_GT ){.    as
2d42e 73 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 54 4b 5f  sert( TK_LT==TK_
2d42f 47 54 2b 32 20 29 3b 0a 20 20 20 20 61 73 73 65  GT+2 );.    asse
2d430 72 74 28 20 54 4b 5f 47 45 3d 3d 54 4b 5f 4c 45  rt( TK_GE==TK_LE
2d431 2b 32 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  +2 );.    assert
2d432 28 20 54 4b 5f 47 54 3e 54 4b 5f 45 51 20 29 3b  ( TK_GT>TK_EQ );
2d433 0a 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f  .    assert( TK_
2d434 47 54 3c 54 4b 5f 4c 45 20 29 3b 0a 20 20 20 20  GT<TK_LE );.    
2d435 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f  assert( pExpr->o
2d436 70 3e 3d 54 4b 5f 47 54 20 26 26 20 70 45 78 70  p>=TK_GT && pExp
2d437 72 2d 3e 6f 70 3c 3d 54 4b 5f 47 45 20 29 3b 0a  r->op<=TK_GE );.
2d438 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20      pExpr->op = 
2d439 28 28 70 45 78 70 72 2d 3e 6f 70 2d 54 4b 5f 47  ((pExpr->op-TK_G
2d43a 54 29 5e 32 29 2b 54 4b 5f 47 54 3b 0a 20 20 7d  T)^2)+TK_GT;.  }
2d43b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c  .}../*.** Transl
2d43c 61 74 65 20 66 72 6f 6d 20 54 4b 5f 78 78 20 6f  ate from TK_xx o
2d43d 70 65 72 61 74 6f 72 20 74 6f 20 57 4f 5f 78 78  perator to WO_xx
2d43e 20 62 69 74 6d 61 73 6b 2e 0a 2a 2f 0a 73 74 61   bitmask..*/.sta
2d43f 74 69 63 20 75 31 36 20 6f 70 65 72 61 74 6f 72  tic u16 operator
2d440 4d 61 73 6b 28 69 6e 74 20 6f 70 29 7b 0a 20 20  Mask(int op){.  
2d441 75 31 36 20 63 3b 0a 20 20 61 73 73 65 72 74 28  u16 c;.  assert(
2d442 20 61 6c 6c 6f 77 65 64 4f 70 28 6f 70 29 20 29   allowedOp(op) )
2d443 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 49  ;.  if( op==TK_I
2d444 4e 20 29 7b 0a 20 20 20 20 63 20 3d 20 57 4f 5f  N ){.    c = WO_
2d445 49 4e 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  IN;.  }else if( 
2d446 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29 7b  op==TK_ISNULL ){
2d447 0a 20 20 20 20 63 20 3d 20 57 4f 5f 49 53 4e 55  .    c = WO_ISNU
2d448 4c 4c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  LL;.  }else{.   
2d449 20 61 73 73 65 72 74 28 20 28 57 4f 5f 45 51 3c   assert( (WO_EQ<
2d44a 3c 28 6f 70 2d 54 4b 5f 45 51 29 29 20 3c 20 30  <(op-TK_EQ)) < 0
2d44b 78 37 66 66 66 20 29 3b 0a 20 20 20 20 63 20 3d  x7fff );.    c =
2d44c 20 28 75 31 36 29 28 57 4f 5f 45 51 3c 3c 28 6f   (u16)(WO_EQ<<(o
2d44d 70 2d 54 4b 5f 45 51 29 29 3b 0a 20 20 7d 0a 20  p-TK_EQ));.  }. 
2d44e 20 61 73 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f   assert( op!=TK_
2d44f 49 53 4e 55 4c 4c 20 7c 7c 20 63 3d 3d 57 4f 5f  ISNULL || c==WO_
2d450 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 61 73 73 65  ISNULL );.  asse
2d451 72 74 28 20 6f 70 21 3d 54 4b 5f 49 4e 20 7c 7c  rt( op!=TK_IN ||
2d452 20 63 3d 3d 57 4f 5f 49 4e 20 29 3b 0a 20 20 61   c==WO_IN );.  a
2d453 73 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f 45 51  ssert( op!=TK_EQ
2d454 20 7c 7c 20 63 3d 3d 57 4f 5f 45 51 20 29 3b 0a   || c==WO_EQ );.
2d455 20 20 61 73 73 65 72 74 28 20 6f 70 21 3d 54 4b    assert( op!=TK
2d456 5f 4c 54 20 7c 7c 20 63 3d 3d 57 4f 5f 4c 54 20  _LT || c==WO_LT 
2d457 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 21  );.  assert( op!
2d458 3d 54 4b 5f 4c 45 20 7c 7c 20 63 3d 3d 57 4f 5f  =TK_LE || c==WO_
2d459 4c 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  LE );.  assert( 
2d45a 6f 70 21 3d 54 4b 5f 47 54 20 7c 7c 20 63 3d 3d  op!=TK_GT || c==
2d45b 57 4f 5f 47 54 20 29 3b 0a 20 20 61 73 73 65 72  WO_GT );.  asser
2d45c 74 28 20 6f 70 21 3d 54 4b 5f 47 45 20 7c 7c 20  t( op!=TK_GE || 
2d45d 63 3d 3d 57 4f 5f 47 45 20 29 3b 0a 20 20 72 65  c==WO_GE );.  re
2d45e 74 75 72 6e 20 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn c;.}../*.**
2d45f 20 53 65 61 72 63 68 20 66 6f 72 20 61 20 74 65   Search for a te
2d460 72 6d 20 69 6e 20 74 68 65 20 57 48 45 52 45 20  rm in the WHERE 
2d461 63 6c 61 75 73 65 20 74 68 61 74 20 69 73 20 6f  clause that is o
2d462 66 20 74 68 65 20 66 6f 72 6d 20 22 58 20 3c 6f  f the form "X <o
2d463 70 3e 20 3c 65 78 70 72 3e 22 0a 2a 2a 20 77 68  p> <expr>".** wh
2d464 65 72 65 20 58 20 69 73 20 61 20 72 65 66 65 72  ere X is a refer
2d465 65 6e 63 65 20 74 6f 20 74 68 65 20 69 43 6f 6c  ence to the iCol
2d466 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 69 43 75  umn of table iCu
2d467 72 20 61 6e 64 20 3c 6f 70 3e 20 69 73 20 6f 6e  r and <op> is on
2d468 65 20 6f 66 0a 2a 2a 20 74 68 65 20 57 4f 5f 78  e of.** the WO_x
2d469 78 20 6f 70 65 72 61 74 6f 72 20 63 6f 64 65 73  x operator codes
2d46a 20 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68   specified by th
2d46b 65 20 6f 70 20 70 61 72 61 6d 65 74 65 72 2e 0a  e op parameter..
2d46c 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  ** Return a poin
2d46d 74 65 72 20 74 6f 20 74 68 65 20 74 65 72 6d 2e  ter to the term.
2d46e 20 20 52 65 74 75 72 6e 20 30 20 69 66 20 6e 6f    Return 0 if no
2d46f 74 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 74 61 74  t found..*/.stat
2d470 69 63 20 57 68 65 72 65 54 65 72 6d 20 2a 66 69  ic WhereTerm *fi
2d471 6e 64 54 65 72 6d 28 0a 20 20 57 68 65 72 65 43  ndTerm(.  WhereC
2d472 6c 61 75 73 65 20 2a 70 57 43 2c 20 20 20 20 20  lause *pWC,     
2d473 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61  /* The WHERE cla
2d474 75 73 65 20 74 6f 20 62 65 20 73 65 61 72 63 68  use to be search
2d475 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72  ed */.  int iCur
2d476 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
2d477 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f   Cursor number o
2d478 66 20 4c 48 53 20 2a 2f 0a 20 20 69 6e 74 20 69  f LHS */.  int i
2d479 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 20  Column,         
2d47a 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65   /* Column numbe
2d47b 72 20 6f 66 20 4c 48 53 20 2a 2f 0a 20 20 42 69  r of LHS */.  Bi
2d47c 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64 79 2c 20  tmask notReady, 
2d47d 20 20 20 20 2f 2a 20 52 48 53 20 6d 75 73 74 20      /* RHS must 
2d47e 6e 6f 74 20 6f 76 65 72 6c 61 70 20 77 69 74 68  not overlap with
2d47f 20 74 68 69 73 20 6d 61 73 6b 20 2a 2f 0a 20 20   this mask */.  
2d480 75 33 32 20 6f 70 2c 20 20 20 20 20 20 20 20 20  u32 op,         
2d481 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66        /* Mask of
2d482 20 57 4f 5f 78 78 20 76 61 6c 75 65 73 20 64 65   WO_xx values de
2d483 73 63 72 69 62 69 6e 67 20 6f 70 65 72 61 74 6f  scribing operato
2d484 72 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49  r */.  Index *pI
2d485 64 78 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  dx           /* 
2d486 4d 75 73 74 20 62 65 20 63 6f 6d 70 61 74 69 62  Must be compatib
2d487 6c 65 20 77 69 74 68 20 74 68 69 73 20 69 6e 64  le with this ind
2d488 65 78 2c 20 69 66 20 6e 6f 74 20 4e 55 4c 4c 20  ex, if not NULL 
2d489 2a 2f 0a 29 7b 0a 20 20 57 68 65 72 65 54 65 72  */.){.  WhereTer
2d48a 6d 20 2a 70 54 65 72 6d 3b 0a 20 20 69 6e 74 20  m *pTerm;.  int 
2d48b 6b 3b 0a 20 20 61 73 73 65 72 74 28 20 69 43 75  k;.  assert( iCu
2d48c 72 3e 3d 30 20 29 3b 0a 20 20 6f 70 20 26 3d 20  r>=0 );.  op &= 
2d48d 57 4f 5f 41 4c 4c 3b 0a 20 20 66 6f 72 28 70 54  WO_ALL;.  for(pT
2d48e 65 72 6d 3d 70 57 43 2d 3e 61 2c 20 6b 3d 70 57  erm=pWC->a, k=pW
2d48f 43 2d 3e 6e 54 65 72 6d 3b 20 6b 3b 20 6b 2d 2d  C->nTerm; k; k--
2d490 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20  , pTerm++){.    
2d491 69 66 28 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43  if( pTerm->leftC
2d492 75 72 73 6f 72 3d 3d 69 43 75 72 0a 20 20 20 20  ursor==iCur.    
2d493 20 20 20 26 26 20 28 70 54 65 72 6d 2d 3e 70 72     && (pTerm->pr
2d494 65 72 65 71 52 69 67 68 74 20 26 20 6e 6f 74 52  ereqRight & notR
2d495 65 61 64 79 29 3d 3d 30 0a 20 20 20 20 20 20 20  eady)==0.       
2d496 26 26 20 70 54 65 72 6d 2d 3e 75 2e 6c 65 66 74  && pTerm->u.left
2d497 43 6f 6c 75 6d 6e 3d 3d 69 43 6f 6c 75 6d 6e 0a  Column==iColumn.
2d498 20 20 20 20 20 20 20 26 26 20 28 70 54 65 72 6d         && (pTerm
2d499 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 6f 70  ->eOperator & op
2d49a 29 21 3d 30 0a 20 20 20 20 29 7b 0a 20 20 20 20  )!=0.    ){.    
2d49b 20 20 69 66 28 20 70 49 64 78 20 26 26 20 70 54    if( pIdx && pT
2d49c 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 21 3d  erm->eOperator!=
2d49d 57 4f 5f 49 53 4e 55 4c 4c 20 29 7b 0a 20 20 20  WO_ISNULL ){.   
2d49e 20 20 20 20 20 45 78 70 72 20 2a 70 58 20 3d 20       Expr *pX = 
2d49f 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20  pTerm->pExpr;.  
2d4a0 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70        CollSeq *p
2d4a1 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 63 68  Coll;.        ch
2d4a2 61 72 20 69 64 78 61 66 66 3b 0a 20 20 20 20 20  ar idxaff;.     
2d4a3 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20     int j;.      
2d4a4 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 20    Parse *pParse 
2d4a5 3d 20 70 57 43 2d 3e 70 50 61 72 73 65 3b 0a 0a  = pWC->pParse;..
2d4a6 20 20 20 20 20 20 20 20 69 64 78 61 66 66 20 3d          idxaff =
2d4a7 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 2d 3e 61   pIdx->pTable->a
2d4a8 43 6f 6c 5b 69 43 6f 6c 75 6d 6e 5d 2e 61 66 66  Col[iColumn].aff
2d4a9 69 6e 69 74 79 3b 0a 20 20 20 20 20 20 20 20 69  inity;.        i
2d4aa 66 28 20 21 73 71 6c 69 74 65 33 49 6e 64 65 78  f( !sqlite3Index
2d4ab 41 66 66 69 6e 69 74 79 4f 6b 28 70 58 2c 20 69  AffinityOk(pX, i
2d4ac 64 78 61 66 66 29 20 29 20 63 6f 6e 74 69 6e 75  dxaff) ) continu
2d4ad 65 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 46  e;..        /* F
2d4ae 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 63 6f  igure out the co
2d4af 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
2d4b0 20 72 65 71 75 69 72 65 64 20 66 72 6f 6d 20 61   required from a
2d4b1 6e 20 69 6e 64 65 78 20 66 6f 72 0a 20 20 20 20  n index for.    
2d4b2 20 20 20 20 2a 2a 20 69 74 20 74 6f 20 62 65 20      ** it to be 
2d4b3 75 73 65 66 75 6c 20 66 6f 72 20 6f 70 74 69 6d  useful for optim
2d4b4 69 73 69 6e 67 20 65 78 70 72 65 73 73 69 6f 6e  ising expression
2d4b5 20 70 58 2e 20 53 74 6f 72 65 20 74 68 69 73 0a   pX. Store this.
2d4b6 20 20 20 20 20 20 20 20 2a 2a 20 76 61 6c 75 65          ** value
2d4b7 20 69 6e 20 76 61 72 69 61 62 6c 65 20 70 43 6f   in variable pCo
2d4b8 6c 6c 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  ll..        */. 
2d4b9 20 20 20 20 20 20 20 61 73 73 65 72 74 28 70 58         assert(pX
2d4ba 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 20 20 20 20  ->pLeft);.      
2d4bb 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    pColl = sqlite
2d4bc 33 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f  3BinaryCompareCo
2d4bd 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 58  llSeq(pParse, pX
2d4be 2d 3e 70 4c 65 66 74 2c 20 70 58 2d 3e 70 52 69  ->pLeft, pX->pRi
2d4bf 67 68 74 29 3b 0a 20 20 20 20 20 20 20 20 61 73  ght);.        as
2d4c0 73 65 72 74 28 70 43 6f 6c 6c 20 7c 7c 20 70 50  sert(pColl || pP
2d4c1 61 72 73 65 2d 3e 6e 45 72 72 29 3b 0a 0a 20 20  arse->nErr);..  
2d4c2 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 70        for(j=0; p
2d4c3 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d  Idx->aiColumn[j]
2d4c4 21 3d 69 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b  !=iColumn; j++){
2d4c5 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 4e  .          if( N
2d4c6 45 56 45 52 28 6a 3e 3d 70 49 64 78 2d 3e 6e 43  EVER(j>=pIdx->nC
2d4c7 6f 6c 75 6d 6e 29 20 29 20 72 65 74 75 72 6e 20  olumn) ) return 
2d4c8 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  0;.        }.   
2d4c9 20 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 26       if( pColl &
2d4ca 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  & sqlite3StrICmp
2d4cb 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 2c 20 70  (pColl->zName, p
2d4cc 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 6a 5d 29 20  Idx->azColl[j]) 
2d4cd 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
2d4ce 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e    }.      return
2d4cf 20 70 54 65 72 6d 3b 0a 20 20 20 20 7d 0a 20 20   pTerm;.    }.  
2d4d0 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
2d4d1 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65  ./* Forward refe
2d4d2 72 65 6e 63 65 20 2a 2f 0a 73 74 61 74 69 63 20  rence */.static 
2d4d3 76 6f 69 64 20 65 78 70 72 41 6e 61 6c 79 7a 65  void exprAnalyze
2d4d4 28 53 72 63 4c 69 73 74 2a 2c 20 57 68 65 72 65  (SrcList*, Where
2d4d5 43 6c 61 75 73 65 2a 2c 20 69 6e 74 29 3b 0a 0a  Clause*, int);..
2d4d6 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 65 78 70 72 41  /*.** Call exprA
2d4d7 6e 61 6c 79 7a 65 20 6f 6e 20 61 6c 6c 20 74 65  nalyze on all te
2d4d8 72 6d 73 20 69 6e 20 61 20 57 48 45 52 45 20 63  rms in a WHERE c
2d4d9 6c 61 75 73 65 2e 20 20 0a 2a 2a 0a 2a 2a 0a 2a  lause.  .**.**.*
2d4da 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 65 78  /.static void ex
2d4db 70 72 41 6e 61 6c 79 7a 65 41 6c 6c 28 0a 20 20  prAnalyzeAll(.  
2d4dc 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73  SrcList *pTabLis
2d4dd 74 2c 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20  t,       /* the 
2d4de 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20  FROM clause */. 
2d4df 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57   WhereClause *pW
2d4e0 43 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65  C         /* the
2d4e1 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 6f   WHERE clause to
2d4e2 20 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a   be analyzed */.
2d4e3 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f  ){.  int i;.  fo
2d4e4 72 28 69 3d 70 57 43 2d 3e 6e 54 65 72 6d 2d 31  r(i=pWC->nTerm-1
2d4e5 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20  ; i>=0; i--){.  
2d4e6 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 28 70 54    exprAnalyze(pT
2d4e7 61 62 4c 69 73 74 2c 20 70 57 43 2c 20 69 29 3b  abList, pWC, i);
2d4e8 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  .  }.}..#ifndef 
2d4e9 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 49 4b 45  SQLITE_OMIT_LIKE
2d4ea 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 0a 2f 2a  _OPTIMIZATION./*
2d4eb 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65  .** Check to see
2d4ec 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 65 78   if the given ex
2d4ed 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 4c 49  pression is a LI
2d4ee 4b 45 20 6f 72 20 47 4c 4f 42 20 6f 70 65 72 61  KE or GLOB opera
2d4ef 74 6f 72 20 74 68 61 74 0a 2a 2a 20 63 61 6e 20  tor that.** can 
2d4f0 62 65 20 6f 70 74 69 6d 69 7a 65 64 20 75 73 69  be optimized usi
2d4f1 6e 67 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f  ng inequality co
2d4f2 6e 73 74 72 61 69 6e 74 73 2e 20 20 52 65 74 75  nstraints.  Retu
2d4f3 72 6e 20 54 52 55 45 20 69 66 20 69 74 20 69 73  rn TRUE if it is
2d4f4 0a 2a 2a 20 73 6f 20 61 6e 64 20 66 61 6c 73 65  .** so and false
2d4f5 20 69 66 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 49   if not..**.** I
2d4f6 6e 20 6f 72 64 65 72 20 66 6f 72 20 74 68 65 20  n order for the 
2d4f7 6f 70 65 72 61 74 6f 72 20 74 6f 20 62 65 20 6f  operator to be o
2d4f8 70 74 69 6d 69 7a 69 62 6c 65 2c 20 74 68 65 20  ptimizible, the 
2d4f9 52 48 53 20 6d 75 73 74 20 62 65 20 61 20 73 74  RHS must be a st
2d4fa 72 69 6e 67 0a 2a 2a 20 6c 69 74 65 72 61 6c 20  ring.** literal 
2d4fb 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 62 65  that does not be
2d4fc 67 69 6e 20 77 69 74 68 20 61 20 77 69 6c 64 63  gin with a wildc
2d4fd 61 72 64 2e 20 20 0a 2a 2f 0a 73 74 61 74 69 63  ard.  .*/.static
2d4fe 20 69 6e 74 20 69 73 4c 69 6b 65 4f 72 47 6c 6f   int isLikeOrGlo
2d4ff 62 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  b(.  Parse *pPar
2d500 73 65 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e  se,    /* Parsin
2d501 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72  g and code gener
2d502 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  ating context */
2d503 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20  .  Expr *pExpr, 
2d504 20 20 20 20 20 2f 2a 20 54 65 73 74 20 74 68 69       /* Test thi
2d505 73 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a  s expression */.
2d506 20 20 69 6e 74 20 2a 70 6e 50 61 74 74 65 72 6e    int *pnPattern
2d507 2c 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66  ,   /* Number of
2d508 20 6e 6f 6e 2d 77 69 6c 64 63 61 72 64 20 70 72   non-wildcard pr
2d509 65 66 69 78 20 63 68 61 72 61 63 74 65 72 73 20  efix characters 
2d50a 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 73 43 6f 6d  */.  int *pisCom
2d50b 70 6c 65 74 65 2c 20 2f 2a 20 54 72 75 65 20 69  plete, /* True i
2d50c 66 20 74 68 65 20 6f 6e 6c 79 20 77 69 6c 64 63  f the only wildc
2d50d 61 72 64 20 69 73 20 25 20 69 6e 20 74 68 65 20  ard is % in the 
2d50e 6c 61 73 74 20 63 68 61 72 61 63 74 65 72 20 2a  last character *
2d50f 2f 0a 20 20 69 6e 74 20 2a 70 6e 6f 43 61 73 65  /.  int *pnoCase
2d510 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
2d511 20 75 70 70 65 72 63 61 73 65 20 69 73 20 65 71   uppercase is eq
2d512 75 69 76 61 6c 65 6e 74 20 74 6f 20 6c 6f 77 65  uivalent to lowe
2d513 72 63 61 73 65 20 2a 2f 0a 29 7b 0a 20 20 63 6f  rcase */.){.  co
2d514 6e 73 74 20 63 68 61 72 20 2a 7a 3b 20 20 20 20  nst char *z;    
2d515 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69           /* Stri
2d516 6e 67 20 6f 6e 20 52 48 53 20 6f 66 20 4c 49 4b  ng on RHS of LIK
2d517 45 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20  E operator */.  
2d518 45 78 70 72 20 2a 70 52 69 67 68 74 2c 20 2a 70  Expr *pRight, *p
2d519 4c 65 66 74 3b 20 20 20 20 20 20 2f 2a 20 52 69  Left;      /* Ri
2d51a 67 68 74 20 61 6e 64 20 6c 65 66 74 20 73 69 7a  ght and left siz
2d51b 65 20 6f 66 20 4c 49 4b 45 20 6f 70 65 72 61 74  e of LIKE operat
2d51c 6f 72 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  or */.  ExprList
2d51d 20 2a 70 4c 69 73 74 3b 20 20 20 20 20 20 20 20   *pList;        
2d51e 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 6f 70     /* List of op
2d51f 65 72 61 6e 64 73 20 74 6f 20 74 68 65 20 4c 49  erands to the LI
2d520 4b 45 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20  KE operator */. 
2d521 20 69 6e 74 20 63 3b 20 20 20 20 20 20 20 20 20   int c;         
2d522 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
2d523 6e 65 20 63 68 61 72 61 63 74 65 72 20 69 6e 20  ne character in 
2d524 7a 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74  z[] */.  int cnt
2d525 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2d526 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
2d527 20 6e 6f 6e 2d 77 69 6c 64 63 61 72 64 20 70 72   non-wildcard pr
2d528 65 66 69 78 20 63 68 61 72 61 63 74 65 72 73 20  efix characters 
2d529 2a 2f 0a 20 20 63 68 61 72 20 77 63 5b 33 5d 3b  */.  char wc[3];
2d52a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d52b 2f 2a 20 57 69 6c 64 63 61 72 64 20 63 68 61 72  /* Wildcard char
2d52c 61 63 74 65 72 73 20 2a 2f 0a 20 20 43 6f 6c 6c  acters */.  Coll
2d52d 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20  Seq *pColl;     
2d52e 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74         /* Collat
2d52f 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72  ing sequence for
2d530 20 4c 48 53 20 2a 2f 0a 20 20 73 71 6c 69 74 65   LHS */.  sqlite
2d531 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
2d532 64 62 3b 20 20 2f 2a 20 44 61 74 61 62 61 73 65  db;  /* Database
2d533 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a   connection */..
2d534 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49 73    if( !sqlite3Is
2d535 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 28 64 62 2c  LikeFunction(db,
2d536 20 70 45 78 70 72 2c 20 70 6e 6f 43 61 73 65 2c   pExpr, pnoCase,
2d537 20 77 63 29 20 29 7b 0a 20 20 20 20 72 65 74 75   wc) ){.    retu
2d538 72 6e 20 30 3b 0a 20 20 7d 0a 23 69 66 64 65 66  rn 0;.  }.#ifdef
2d539 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43 0a 20   SQLITE_EBCDIC. 
2d53a 20 69 66 28 20 2a 70 6e 6f 43 61 73 65 20 29 20   if( *pnoCase ) 
2d53b 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66  return 0;.#endif
2d53c 0a 20 20 70 4c 69 73 74 20 3d 20 70 45 78 70 72  .  pList = pExpr
2d53d 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 70 52 69  ->x.pList;.  pRi
2d53e 67 68 74 20 3d 20 70 4c 69 73 74 2d 3e 61 5b 30  ght = pList->a[0
2d53f 5d 2e 70 45 78 70 72 3b 0a 20 20 69 66 28 20 70  ].pExpr;.  if( p
2d540 52 69 67 68 74 2d 3e 6f 70 21 3d 54 4b 5f 53 54  Right->op!=TK_ST
2d541 52 49 4e 47 20 29 7b 0a 20 20 20 20 72 65 74 75  RING ){.    retu
2d542 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4c 65 66  rn 0;.  }.  pLef
2d543 74 20 3d 20 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e  t = pList->a[1].
2d544 70 45 78 70 72 3b 0a 20 20 69 66 28 20 70 4c 65  pExpr;.  if( pLe
2d545 66 74 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d  ft->op!=TK_COLUM
2d546 4e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  N ){.    return 
2d547 30 3b 0a 20 20 7d 0a 20 20 70 43 6f 6c 6c 20 3d  0;.  }.  pColl =
2d548 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c   sqlite3ExprColl
2d549 53 65 71 28 70 50 61 72 73 65 2c 20 70 4c 65 66  Seq(pParse, pLef
2d54a 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43  t);.  assert( pC
2d54b 6f 6c 6c 21 3d 30 20 7c 7c 20 70 4c 65 66 74 2d  oll!=0 || pLeft-
2d54c 3e 69 43 6f 6c 75 6d 6e 3d 3d 2d 31 20 29 3b 0a  >iColumn==-1 );.
2d54d 20 20 69 66 28 20 70 43 6f 6c 6c 3d 3d 30 20 29    if( pColl==0 )
2d54e 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28   return 0;.  if(
2d54f 20 28 70 43 6f 6c 6c 2d 3e 74 79 70 65 21 3d 53   (pColl->type!=S
2d550 51 4c 49 54 45 5f 43 4f 4c 4c 5f 42 49 4e 41 52  QLITE_COLL_BINAR
2d551 59 20 7c 7c 20 2a 70 6e 6f 43 61 73 65 29 20 26  Y || *pnoCase) &
2d552 26 0a 20 20 20 20 20 20 28 70 43 6f 6c 6c 2d 3e  &.      (pColl->
2d553 74 79 70 65 21 3d 53 51 4c 49 54 45 5f 43 4f 4c  type!=SQLITE_COL
2d554 4c 5f 4e 4f 43 41 53 45 20 7c 7c 20 21 2a 70 6e  L_NOCASE || !*pn
2d555 6f 43 61 73 65 29 20 29 7b 0a 20 20 20 20 72 65  oCase) ){.    re
2d556 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66  turn 0;.  }.  if
2d557 28 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66  ( sqlite3ExprAff
2d558 69 6e 69 74 79 28 70 4c 65 66 74 29 21 3d 53 51  inity(pLeft)!=SQ
2d559 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20 29 20  LITE_AFF_TEXT ) 
2d55a 72 65 74 75 72 6e 20 30 3b 0a 20 20 7a 20 3d 20  return 0;.  z = 
2d55b 70 52 69 67 68 74 2d 3e 75 2e 7a 54 6f 6b 65 6e  pRight->u.zToken
2d55c 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 7a  ;.  if( ALWAYS(z
2d55d 29 20 29 7b 0a 20 20 20 20 63 6e 74 20 3d 20 30  ) ){.    cnt = 0
2d55e 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 63 3d  ;.    while( (c=
2d55f 7a 5b 63 6e 74 5d 29 21 3d 30 20 26 26 20 63 21  z[cnt])!=0 && c!
2d560 3d 77 63 5b 30 5d 20 26 26 20 63 21 3d 77 63 5b  =wc[0] && c!=wc[
2d561 31 5d 20 26 26 20 63 21 3d 77 63 5b 32 5d 20 29  1] && c!=wc[2] )
2d562 7b 0a 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20  {.      cnt++;. 
2d563 20 20 20 7d 0a 20 20 20 20 69 66 28 20 63 6e 74     }.    if( cnt
2d564 21 3d 30 20 26 26 20 63 21 3d 30 20 26 26 20 32  !=0 && c!=0 && 2
2d565 35 35 21 3d 28 75 38 29 7a 5b 63 6e 74 2d 31 5d  55!=(u8)z[cnt-1]
2d566 20 29 7b 0a 20 20 20 20 20 20 2a 70 69 73 43 6f   ){.      *pisCo
2d567 6d 70 6c 65 74 65 20 3d 20 7a 5b 63 6e 74 5d 3d  mplete = z[cnt]=
2d568 3d 77 63 5b 30 5d 20 26 26 20 7a 5b 63 6e 74 2b  =wc[0] && z[cnt+
2d569 31 5d 3d 3d 30 3b 0a 20 20 20 20 20 20 2a 70 6e  1]==0;.      *pn
2d56a 50 61 74 74 65 72 6e 20 3d 20 63 6e 74 3b 0a 20  Pattern = cnt;. 
2d56b 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
2d56c 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
2d56d 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  n 0;.}.#endif /*
2d56e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 49 4b   SQLITE_OMIT_LIK
2d56f 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a  E_OPTIMIZATION *
2d570 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  /...#ifndef SQLI
2d571 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
2d572 41 42 4c 45 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b  ABLE./*.** Check
2d573 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 67   to see if the g
2d574 69 76 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  iven expression 
2d575 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a  is of the form.*
2d576 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 63 6f 6c  *.**         col
2d577 75 6d 6e 20 4d 41 54 43 48 20 65 78 70 72 0a 2a  umn MATCH expr.*
2d578 2a 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 74 68  *.** If it is th
2d579 65 6e 20 72 65 74 75 72 6e 20 54 52 55 45 2e 20  en return TRUE. 
2d57a 20 49 66 20 6e 6f 74 2c 20 72 65 74 75 72 6e 20   If not, return 
2d57b 46 41 4c 53 45 2e 0a 2a 2f 0a 73 74 61 74 69 63  FALSE..*/.static
2d57c 20 69 6e 74 20 69 73 4d 61 74 63 68 4f 66 43 6f   int isMatchOfCo
2d57d 6c 75 6d 6e 28 0a 20 20 45 78 70 72 20 2a 70 45  lumn(.  Expr *pE
2d57e 78 70 72 20 20 20 20 20 20 2f 2a 20 54 65 73 74  xpr      /* Test
2d57f 20 74 68 69 73 20 65 78 70 72 65 73 73 69 6f 6e   this expression
2d580 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 4c 69 73   */.){.  ExprLis
2d581 74 20 2a 70 4c 69 73 74 3b 0a 0a 20 20 69 66 28  t *pList;..  if(
2d582 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 46   pExpr->op!=TK_F
2d583 55 4e 43 54 49 4f 4e 20 29 7b 0a 20 20 20 20 72  UNCTION ){.    r
2d584 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69  eturn 0;.  }.  i
2d585 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
2d586 70 28 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65  p(pExpr->u.zToke
2d587 6e 2c 22 6d 61 74 63 68 22 29 21 3d 30 20 29 7b  n,"match")!=0 ){
2d588 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
2d589 20 7d 0a 20 20 70 4c 69 73 74 20 3d 20 70 45 78   }.  pList = pEx
2d58a 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 69  pr->x.pList;.  i
2d58b 66 28 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 21  f( pList->nExpr!
2d58c 3d 32 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =2 ){.    return
2d58d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4c   0;.  }.  if( pL
2d58e 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 2d  ist->a[1].pExpr-
2d58f 3e 6f 70 20 21 3d 20 54 4b 5f 43 4f 4c 55 4d 4e  >op != TK_COLUMN
2d590 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
2d591 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31  ;.  }.  return 1
2d592 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
2d593 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
2d594 4c 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  LTABLE */../*.**
2d595 20 49 66 20 74 68 65 20 70 42 61 73 65 20 65 78   If the pBase ex
2d596 70 72 65 73 73 69 6f 6e 20 6f 72 69 67 69 6e 61  pression origina
2d597 74 65 64 20 69 6e 20 74 68 65 20 4f 4e 20 6f 72  ted in the ON or
2d598 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20 6f 66   USING clause of
2d599 0a 2a 2a 20 61 20 6a 6f 69 6e 2c 20 74 68 65 6e  .** a join, then
2d59a 20 74 72 61 6e 73 66 65 72 20 74 68 65 20 61 70   transfer the ap
2d59b 70 72 6f 70 72 69 61 74 65 20 6d 61 72 6b 69 6e  propriate markin
2d59c 67 73 20 6f 76 65 72 20 74 6f 20 64 65 72 69 76  gs over to deriv
2d59d 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ed..*/.static vo
2d59e 69 64 20 74 72 61 6e 73 66 65 72 4a 6f 69 6e 4d  id transferJoinM
2d59f 61 72 6b 69 6e 67 73 28 45 78 70 72 20 2a 70 44  arkings(Expr *pD
2d5a0 65 72 69 76 65 64 2c 20 45 78 70 72 20 2a 70 42  erived, Expr *pB
2d5a1 61 73 65 29 7b 0a 20 20 70 44 65 72 69 76 65 64  ase){.  pDerived
2d5a2 2d 3e 66 6c 61 67 73 20 7c 3d 20 70 42 61 73 65  ->flags |= pBase
2d5a3 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 46 72 6f  ->flags & EP_Fro
2d5a4 6d 4a 6f 69 6e 3b 0a 20 20 70 44 65 72 69 76 65  mJoin;.  pDerive
2d5a5 64 2d 3e 69 52 69 67 68 74 4a 6f 69 6e 54 61 62  d->iRightJoinTab
2d5a6 6c 65 20 3d 20 70 42 61 73 65 2d 3e 69 52 69 67  le = pBase->iRig
2d5a7 68 74 4a 6f 69 6e 54 61 62 6c 65 3b 0a 7d 0a 0a  htJoinTable;.}..
2d5a8 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
2d5a9 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49  ITE_OMIT_OR_OPTI
2d5aa 4d 49 5a 41 54 49 4f 4e 29 20 26 26 20 21 64 65  MIZATION) && !de
2d5ab 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
2d5ac 54 5f 53 55 42 51 55 45 52 59 29 0a 2f 2a 0a 2a  T_SUBQUERY)./*.*
2d5ad 2a 20 41 6e 61 6c 79 7a 65 20 61 20 74 65 72 6d  * Analyze a term
2d5ae 20 74 68 61 74 20 63 6f 6e 73 69 73 74 73 20 6f   that consists o
2d5af 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 4f 52  f two or more OR
2d5b0 2d 63 6f 6e 6e 65 63 74 65 64 0a 2a 2a 20 73 75  -connected.** su
2d5b1 62 74 65 72 6d 73 2e 20 20 53 6f 20 69 6e 3a 0a  bterms.  So in:.
2d5b2 2a 2a 0a 2a 2a 20 20 20 20 20 2e 2e 2e 20 57 48  **.**     ... WH
2d5b3 45 52 45 20 20 28 61 3d 35 29 20 41 4e 44 20 28  ERE  (a=5) AND (
2d5b4 62 3d 37 20 4f 52 20 63 3d 39 20 4f 52 20 64 3d  b=7 OR c=9 OR d=
2d5b5 31 33 29 20 41 4e 44 20 28 64 3d 31 33 29 0a 2a  13) AND (d=13).*
2d5b6 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
2d5b7 20 20 20 20 20 20 20 20 20 20 20 5e 5e 5e 5e 5e             ^^^^^
2d5b8 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 0a  ^^^^^^^^^^^^^^^.
2d5b9 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
2d5ba 6e 65 20 61 6e 61 6c 79 7a 65 73 20 74 65 72 6d  ne analyzes term
2d5bb 73 20 73 75 63 68 20 61 73 20 74 68 65 20 6d 69  s such as the mi
2d5bc 64 64 6c 65 20 74 65 72 6d 20 69 6e 20 74 68 65  ddle term in the
2d5bd 20 61 62 6f 76 65 20 65 78 61 6d 70 6c 65 2e 0a   above example..
2d5be 2a 2a 20 41 20 57 68 65 72 65 4f 72 54 65 72 6d  ** A WhereOrTerm
2d5bf 20 6f 62 6a 65 63 74 20 69 73 20 63 6f 6d 70 75   object is compu
2d5c0 74 65 64 20 61 6e 64 20 61 74 74 61 63 68 65 64  ted and attached
2d5c1 20 74 6f 20 74 68 65 20 74 65 72 6d 20 75 6e 64   to the term und
2d5c2 65 72 0a 2a 2a 20 61 6e 61 6c 79 73 69 73 2c 20  er.** analysis, 
2d5c3 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68  regardless of th
2d5c4 65 20 6f 75 74 63 6f 6d 65 20 6f 66 20 74 68 65  e outcome of the
2d5c5 20 61 6e 61 6c 79 73 69 73 2e 20 20 48 65 6e 63   analysis.  Henc
2d5c6 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 57 68 65  e:.**.**     Whe
2d5c7 72 65 54 65 72 6d 2e 77 74 46 6c 61 67 73 20 20  reTerm.wtFlags  
2d5c8 20 7c 3d 20 20 54 45 52 4d 5f 4f 52 49 4e 46 4f   |=  TERM_ORINFO
2d5c9 0a 2a 2a 20 20 20 20 20 57 68 65 72 65 54 65 72  .**     WhereTer
2d5ca 6d 2e 75 2e 70 4f 72 49 6e 66 6f 20 20 3d 20 20  m.u.pOrInfo  =  
2d5cb 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c  a dynamically al
2d5cc 6c 6f 63 61 74 65 64 20 57 68 65 72 65 4f 72 54  located WhereOrT
2d5cd 65 72 6d 20 6f 62 6a 65 63 74 0a 2a 2a 0a 2a 2a  erm object.**.**
2d5ce 20 54 68 65 20 74 65 72 6d 20 62 65 69 6e 67 20   The term being 
2d5cf 61 6e 61 6c 79 7a 65 64 20 6d 75 73 74 20 68 61  analyzed must ha
2d5d0 76 65 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 6f  ve two or more o
2d5d1 66 20 4f 52 2d 63 6f 6e 6e 65 63 74 65 64 20 73  f OR-connected s
2d5d2 75 62 74 65 72 6d 73 2e 0a 2a 2a 20 41 20 73 69  ubterms..** A si
2d5d3 6e 67 6c 65 20 73 75 62 74 65 72 6d 20 6d 69 67  ngle subterm mig
2d5d4 68 74 20 62 65 20 61 20 73 65 74 20 6f 66 20 41  ht be a set of A
2d5d5 4e 44 2d 63 6f 6e 6e 65 63 74 65 64 20 73 75 62  ND-connected sub
2d5d6 2d 73 75 62 74 65 72 6d 73 2e 0a 2a 2a 20 45 78  -subterms..** Ex
2d5d7 61 6d 70 6c 65 73 20 6f 66 20 74 65 72 6d 73 20  amples of terms 
2d5d8 75 6e 64 65 72 20 61 6e 61 6c 79 73 69 73 3a 0a  under analysis:.
2d5d9 2a 2a 0a 2a 2a 20 20 20 20 20 28 41 29 20 20 20  **.**     (A)   
2d5da 20 20 74 31 2e 78 3d 74 32 2e 79 20 4f 52 20 74    t1.x=t2.y OR t
2d5db 31 2e 78 3d 74 32 2e 7a 20 4f 52 20 74 31 2e 79  1.x=t2.z OR t1.y
2d5dc 3d 31 35 20 4f 52 20 74 31 2e 7a 3d 74 33 2e 61  =15 OR t1.z=t3.a
2d5dd 2b 35 0a 2a 2a 20 20 20 20 20 28 42 29 20 20 20  +5.**     (B)   
2d5de 20 20 78 3d 65 78 70 72 31 20 4f 52 20 65 78 70    x=expr1 OR exp
2d5df 72 32 3d 78 20 4f 52 20 78 3d 65 78 70 72 33 0a  r2=x OR x=expr3.
2d5e0 2a 2a 20 20 20 20 20 28 43 29 20 20 20 20 20 74  **     (C)     t
2d5e1 31 2e 78 3d 74 32 2e 79 20 4f 52 20 28 74 31 2e  1.x=t2.y OR (t1.
2d5e2 78 3d 74 32 2e 7a 20 41 4e 44 20 74 31 2e 79 3d  x=t2.z AND t1.y=
2d5e3 31 35 29 0a 2a 2a 20 20 20 20 20 28 44 29 20 20  15).**     (D)  
2d5e4 20 20 20 78 3d 65 78 70 72 31 20 4f 52 20 28 79     x=expr1 OR (y
2d5e5 3e 31 31 20 41 4e 44 20 79 3c 32 32 20 41 4e 44  >11 AND y<22 AND
2d5e6 20 7a 20 4c 49 4b 45 20 27 2a 68 65 6c 6c 6f 2a   z LIKE '*hello*
2d5e7 27 29 0a 2a 2a 20 20 20 20 20 28 45 29 20 20 20  ').**     (E)   
2d5e8 20 20 28 70 2e 61 3d 31 20 41 4e 44 20 71 2e 62    (p.a=1 AND q.b
2d5e9 3d 32 20 41 4e 44 20 72 2e 63 3d 33 29 20 4f 52  =2 AND r.c=3) OR
2d5ea 20 28 70 2e 78 3d 34 20 41 4e 44 20 71 2e 79 3d   (p.x=4 AND q.y=
2d5eb 35 20 41 4e 44 20 72 2e 7a 3d 36 29 0a 2a 2a 0a  5 AND r.z=6).**.
2d5ec 2a 2a 20 43 41 53 45 20 31 3a 0a 2a 2a 0a 2a 2a  ** CASE 1:.**.**
2d5ed 20 49 66 20 61 6c 6c 20 73 75 62 74 65 72 6d 73   If all subterms
2d5ee 20 61 72 65 20 6f 66 20 74 68 65 20 66 6f 72 6d   are of the form
2d5ef 20 54 2e 43 3d 65 78 70 72 20 66 6f 72 20 73 6f   T.C=expr for so
2d5f0 6d 65 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e  me single column
2d5f1 20 6f 66 20 43 0a 2a 2a 20 61 20 73 69 6e 67 6c   of C.** a singl
2d5f2 65 20 74 61 62 6c 65 20 54 20 28 61 73 20 73 68  e table T (as sh
2d5f3 6f 77 6e 20 69 6e 20 65 78 61 6d 70 6c 65 20 42  own in example B
2d5f4 20 61 62 6f 76 65 29 20 74 68 65 6e 20 63 72 65   above) then cre
2d5f5 61 74 65 20 61 20 6e 65 77 20 76 69 72 74 75 61  ate a new virtua
2d5f6 6c 0a 2a 2a 20 74 65 72 6d 20 74 68 61 74 20 69  l.** term that i
2d5f7 73 20 61 6e 20 65 71 75 69 76 61 6c 65 6e 74 20  s an equivalent 
2d5f8 49 4e 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20  IN expression.  
2d5f9 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
2d5fa 69 66 20 74 68 65 20 74 65 72 6d 0a 2a 2a 20 62  if the term.** b
2d5fb 65 69 6e 67 20 61 6e 61 6c 79 7a 65 64 20 69 73  eing analyzed is
2d5fc 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 78 20 3d  :.**.**      x =
2d5fd 20 65 78 70 72 31 20 20 4f 52 20 20 65 78 70 72   expr1  OR  expr
2d5fe 32 20 3d 20 78 20 20 4f 52 20 20 78 20 3d 20 65  2 = x  OR  x = e
2d5ff 78 70 72 33 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20  xpr3.**.** then 
2d600 63 72 65 61 74 65 20 61 20 6e 65 77 20 76 69 72  create a new vir
2d601 74 75 61 6c 20 74 65 72 6d 20 6c 69 6b 65 20 74  tual term like t
2d602 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  his:.**.**      
2d603 78 20 49 4e 20 28 65 78 70 72 31 2c 65 78 70 72  x IN (expr1,expr
2d604 32 2c 65 78 70 72 33 29 0a 2a 2a 0a 2a 2a 20 43  2,expr3).**.** C
2d605 41 53 45 20 32 3a 0a 2a 2a 0a 2a 2a 20 49 66 20  ASE 2:.**.** If 
2d606 61 6c 6c 20 73 75 62 74 65 72 6d 73 20 61 72 65  all subterms are
2d607 20 69 6e 64 65 78 61 62 6c 65 20 62 79 20 61 20   indexable by a 
2d608 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 54 2c 20  single table T, 
2d609 74 68 65 6e 20 73 65 74 0a 2a 2a 0a 2a 2a 20 20  then set.**.**  
2d60a 20 20 20 57 68 65 72 65 54 65 72 6d 2e 65 4f 70     WhereTerm.eOp
2d60b 65 72 61 74 6f 72 20 20 20 20 20 20 20 20 20 20  erator          
2d60c 20 20 20 20 3d 20 20 57 4f 5f 4f 52 0a 2a 2a 20      =  WO_OR.** 
2d60d 20 20 20 20 57 68 65 72 65 54 65 72 6d 2e 75 2e      WhereTerm.u.
2d60e 70 4f 72 49 6e 66 6f 2d 3e 69 6e 64 65 78 61 62  pOrInfo->indexab
2d60f 6c 65 20 20 7c 3d 20 20 74 68 65 20 63 75 72 73  le  |=  the curs
2d610 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 61  or number for ta
2d611 62 6c 65 20 54 0a 2a 2a 0a 2a 2a 20 41 20 73 75  ble T.**.** A su
2d612 62 74 65 72 6d 20 69 73 20 22 69 6e 64 65 78 61  bterm is "indexa
2d613 62 6c 65 22 20 69 66 20 69 74 20 69 73 20 6f 66  ble" if it is of
2d614 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 20 22 54 2e   the form.** "T.
2d615 43 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 22 20 77  C <op> <expr>" w
2d616 68 65 72 65 20 43 20 69 73 20 61 6e 79 20 63 6f  here C is any co
2d617 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 54 20  lumn of table T 
2d618 61 6e 64 20 0a 2a 2a 20 3c 6f 70 3e 20 69 73 20  and .** <op> is 
2d619 6f 6e 65 20 6f 66 20 22 3d 22 2c 20 22 3c 22 2c  one of "=", "<",
2d61a 20 22 3c 3d 22 2c 20 22 3e 22 2c 20 22 3e 3d 22   "<=", ">", ">="
2d61b 2c 20 22 49 53 20 4e 55 4c 4c 22 2c 20 6f 72 20  , "IS NULL", or 
2d61c 22 49 4e 22 2e 0a 2a 2a 20 41 20 73 75 62 74 65  "IN"..** A subte
2d61d 72 6d 20 69 73 20 61 6c 73 6f 20 69 6e 64 65 78  rm is also index
2d61e 61 62 6c 65 20 69 66 20 69 74 20 69 73 20 61 6e  able if it is an
2d61f 20 41 4e 44 20 6f 66 20 74 77 6f 20 6f 72 20 6d   AND of two or m
2d620 6f 72 65 0a 2a 2a 20 73 75 62 73 75 62 74 65 72  ore.** subsubter
2d621 6d 73 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20  ms at least one 
2d622 6f 66 20 77 68 69 63 68 20 69 73 20 69 6e 64 65  of which is inde
2d623 78 61 62 6c 65 2e 20 20 49 6e 64 65 78 61 62 6c  xable.  Indexabl
2d624 65 20 41 4e 44 20 0a 2a 2a 20 73 75 62 74 65 72  e AND .** subter
2d625 6d 73 20 68 61 76 65 20 74 68 65 69 72 20 65 4f  ms have their eO
2d626 70 65 72 61 74 6f 72 20 73 65 74 20 74 6f 20 57  perator set to W
2d627 4f 5f 41 4e 44 20 61 6e 64 20 74 68 65 79 20 68  O_AND and they h
2d628 61 76 65 0a 2a 2a 20 75 2e 70 41 6e 64 49 6e 66  ave.** u.pAndInf
2d629 6f 20 73 65 74 20 74 6f 20 61 20 64 79 6e 61 6d  o set to a dynam
2d62a 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64  ically allocated
2d62b 20 57 68 65 72 65 41 6e 64 54 65 72 6d 20 6f 62   WhereAndTerm ob
2d62c 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 46 72 6f 6d  ject..**.** From
2d62d 20 61 6e 6f 74 68 65 72 20 70 6f 69 6e 74 20 6f   another point o
2d62e 66 20 76 69 65 77 2c 20 22 69 6e 64 65 78 61 62  f view, "indexab
2d62f 6c 65 22 20 6d 65 61 6e 73 20 74 68 61 74 20 74  le" means that t
2d630 68 65 20 73 75 62 74 65 72 6d 20 63 6f 75 6c 64  he subterm could
2d631 0a 2a 2a 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20  .** potentially 
2d632 62 65 20 75 73 65 64 20 77 69 74 68 20 61 6e 20  be used with an 
2d633 69 6e 64 65 78 20 69 66 20 61 6e 20 61 70 70 72  index if an appr
2d634 6f 70 72 69 61 74 65 20 69 6e 64 65 78 20 65 78  opriate index ex
2d635 69 73 74 73 2e 0a 2a 2a 20 54 68 69 73 20 61 6e  ists..** This an
2d636 61 6c 79 73 69 73 20 64 6f 65 73 20 6e 6f 74 20  alysis does not 
2d637 63 6f 6e 73 69 64 65 72 20 77 68 65 74 68 65 72  consider whether
2d638 20 6f 72 20 6e 6f 74 20 74 68 65 20 69 6e 64 65   or not the inde
2d639 78 20 65 78 69 73 74 73 3b 20 74 68 61 74 0a 2a  x exists; that.*
2d63a 2a 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 74  * is something t
2d63b 68 65 20 62 65 73 74 49 6e 64 65 78 28 29 20 72  he bestIndex() r
2d63c 6f 75 74 69 6e 65 20 77 69 6c 6c 20 64 65 74 65  outine will dete
2d63d 72 6d 69 6e 65 2e 20 20 54 68 69 73 20 61 6e 61  rmine.  This ana
2d63e 6c 79 73 69 73 0a 2a 2a 20 6f 6e 6c 79 20 6c 6f  lysis.** only lo
2d63f 6f 6b 73 20 61 74 20 77 68 65 74 68 65 72 20 73  oks at whether s
2d640 75 62 74 65 72 6d 73 20 61 70 70 72 6f 70 72 69  ubterms appropri
2d641 61 74 65 20 66 6f 72 20 69 6e 64 65 78 69 6e 67  ate for indexing
2d642 20 65 78 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 41 6c   exist..**.** Al
2d643 6c 20 65 78 61 6d 70 6c 65 73 20 41 20 74 68 72  l examples A thr
2d644 6f 75 67 68 20 45 20 61 62 6f 76 65 20 61 6c 6c  ough E above all
2d645 20 73 61 74 69 73 66 79 20 63 61 73 65 20 32 2e   satisfy case 2.
2d646 20 20 42 75 74 20 69 66 20 61 20 74 65 72 6d 0a    But if a term.
2d647 2a 2a 20 61 6c 73 6f 20 73 74 61 74 69 73 66 69  ** also statisfi
2d648 65 73 20 63 61 73 65 20 31 20 28 73 75 63 68 20  es case 1 (such 
2d649 61 73 20 42 29 20 77 65 20 6b 6e 6f 77 20 74 68  as B) we know th
2d64a 61 74 20 74 68 65 20 6f 70 74 69 6d 69 7a 65 72  at the optimizer
2d64b 20 77 69 6c 6c 0a 2a 2a 20 61 6c 77 61 79 73 20   will.** always 
2d64c 70 72 65 66 65 72 20 63 61 73 65 20 31 2c 20 73  prefer case 1, s
2d64d 6f 20 69 6e 20 74 68 61 74 20 63 61 73 65 20 77  o in that case w
2d64e 65 20 70 72 65 74 65 6e 64 20 74 68 61 74 20 63  e pretend that c
2d64f 61 73 65 20 32 20 69 73 20 6e 6f 74 0a 2a 2a 20  ase 2 is not.** 
2d650 73 61 74 69 73 66 69 65 64 2e 0a 2a 2a 0a 2a 2a  satisfied..**.**
2d651 20 49 74 20 6d 69 67 68 74 20 62 65 20 74 68 65   It might be the
2d652 20 63 61 73 65 20 74 68 61 74 20 6d 75 6c 74 69   case that multi
2d653 70 6c 65 20 74 61 62 6c 65 73 20 61 72 65 20 69  ple tables are i
2d654 6e 64 65 78 61 62 6c 65 2e 20 20 46 6f 72 20 65  ndexable.  For e
2d655 78 61 6d 70 6c 65 2c 0a 2a 2a 20 28 45 29 20 61  xample,.** (E) a
2d656 62 6f 76 65 20 69 73 20 69 6e 64 65 78 61 62 6c  bove is indexabl
2d657 65 20 6f 6e 20 74 61 62 6c 65 73 20 50 2c 20 51  e on tables P, Q
2d658 2c 20 61 6e 64 20 52 2e 0a 2a 2a 0a 2a 2a 20 54  , and R..**.** T
2d659 65 72 6d 73 20 74 68 61 74 20 73 61 74 69 73 66  erms that satisf
2d65a 79 20 63 61 73 65 20 32 20 61 72 65 20 63 61 6e  y case 2 are can
2d65b 64 69 64 61 74 65 73 20 66 6f 72 20 6c 6f 6f 6b  didates for look
2d65c 75 70 20 62 79 20 75 73 69 6e 67 0a 2a 2a 20 73  up by using.** s
2d65d 65 70 61 72 61 74 65 20 69 6e 64 69 63 65 73 20  eparate indices 
2d65e 74 6f 20 66 69 6e 64 20 72 6f 77 69 64 73 20 66  to find rowids f
2d65f 6f 72 20 65 61 63 68 20 73 75 62 74 65 72 6d 20  or each subterm 
2d660 61 6e 64 20 63 6f 6d 70 6f 73 69 6e 67 0a 2a 2a  and composing.**
2d661 20 74 68 65 20 75 6e 69 6f 6e 20 6f 66 20 61 6c   the union of al
2d662 6c 20 72 6f 77 69 64 73 20 75 73 69 6e 67 20 61  l rowids using a
2d663 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 2e 20   RowSet object. 
2d664 20 54 68 69 73 20 69 73 20 73 69 6d 69 6c 61 72   This is similar
2d665 0a 2a 2a 20 74 6f 20 22 62 69 74 6d 61 70 20 69  .** to "bitmap i
2d666 6e 64 69 63 65 73 22 20 69 6e 20 6f 74 68 65 72  ndices" in other
2d667 20 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65   database engine
2d668 73 2e 0a 2a 2a 0a 2a 2a 20 4f 54 48 45 52 57 49  s..**.** OTHERWI
2d669 53 45 3a 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 65 69  SE:.**.** If nei
2d66a 74 68 65 72 20 63 61 73 65 20 31 20 6e 6f 72 20  ther case 1 nor 
2d66b 63 61 73 65 20 32 20 61 70 70 6c 79 2c 20 74 68  case 2 apply, th
2d66c 65 6e 20 6c 65 61 76 65 20 74 68 65 20 65 4f 70  en leave the eOp
2d66d 65 72 61 74 6f 72 20 73 65 74 20 74 6f 0a 2a 2a  erator set to.**
2d66e 20 7a 65 72 6f 2e 20 20 54 68 69 73 20 74 65 72   zero.  This ter
2d66f 6d 20 69 73 20 6e 6f 74 20 75 73 65 66 75 6c 20  m is not useful 
2d670 66 6f 72 20 73 65 61 72 63 68 2e 0a 2a 2f 0a 73  for search..*/.s
2d671 74 61 74 69 63 20 76 6f 69 64 20 65 78 70 72 41  tatic void exprA
2d672 6e 61 6c 79 7a 65 4f 72 54 65 72 6d 28 0a 20 20  nalyzeOrTerm(.  
2d673 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 20  SrcList *pSrc,  
2d674 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65            /* the
2d675 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a   FROM clause */.
2d676 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70    WhereClause *p
2d677 57 43 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 74  WC,         /* t
2d678 68 65 20 63 6f 6d 70 6c 65 74 65 20 57 48 45 52  he complete WHER
2d679 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e  E clause */.  in
2d67a 74 20 69 64 78 54 65 72 6d 20 20 20 20 20 20 20  t idxTerm       
2d67b 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
2d67c 20 6f 66 20 74 68 65 20 4f 52 2d 74 65 72 6d 20   of the OR-term 
2d67d 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a  to be analyzed *
2d67e 2f 0a 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 50  /.){.  Parse *pP
2d67f 61 72 73 65 20 3d 20 70 57 43 2d 3e 70 50 61 72  arse = pWC->pPar
2d680 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  se;            /
2d681 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74  * Parser context
2d682 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
2d683 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20  b = pParse->db; 
2d684 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2d685 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   Database connec
2d686 74 69 6f 6e 20 2a 2f 0a 20 20 57 68 65 72 65 54  tion */.  WhereT
2d687 65 72 6d 20 2a 70 54 65 72 6d 20 3d 20 26 70 57  erm *pTerm = &pW
2d688 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 20 20  C->a[idxTerm];  
2d689 20 20 2f 2a 20 54 68 65 20 74 65 72 6d 20 74 6f    /* The term to
2d68a 20 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a   be analyzed */.
2d68b 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20    Expr *pExpr = 
2d68c 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 20 20 20  pTerm->pExpr;   
2d68d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2d68e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 66 20 74   expression of t
2d68f 68 65 20 74 65 72 6d 20 2a 2f 0a 20 20 57 68 65  he term */.  Whe
2d690 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b  reMaskSet *pMask
2d691 53 65 74 20 3d 20 70 57 43 2d 3e 70 4d 61 73 6b  Set = pWC->pMask
2d692 53 65 74 3b 20 2f 2a 20 54 61 62 6c 65 20 75 73  Set; /* Table us
2d693 65 20 6d 61 73 6b 73 20 2a 2f 0a 20 20 69 6e 74  e masks */.  int
2d694 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
2d695 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d696 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
2d697 6e 74 65 72 73 20 2a 2f 0a 20 20 57 68 65 72 65  nters */.  Where
2d698 43 6c 61 75 73 65 20 2a 70 4f 72 57 63 3b 20 20  Clause *pOrWc;  
2d699 20 20 20 20 20 2f 2a 20 42 72 65 61 6b 75 70 20       /* Breakup 
2d69a 6f 66 20 70 54 65 72 6d 20 69 6e 74 6f 20 73 75  of pTerm into su
2d69b 62 74 65 72 6d 73 20 2a 2f 0a 20 20 57 68 65 72  bterms */.  Wher
2d69c 65 54 65 72 6d 20 2a 70 4f 72 54 65 72 6d 3b 20  eTerm *pOrTerm; 
2d69d 20 20 20 20 20 20 2f 2a 20 41 20 53 75 62 2d 74        /* A Sub-t
2d69e 65 72 6d 20 77 69 74 68 69 6e 20 74 68 65 20 70  erm within the p
2d69f 4f 72 57 63 20 2a 2f 0a 20 20 57 68 65 72 65 4f  OrWc */.  WhereO
2d6a0 72 49 6e 66 6f 20 2a 70 4f 72 49 6e 66 6f 3b 20  rInfo *pOrInfo; 
2d6a1 20 20 20 20 2f 2a 20 41 64 64 69 74 69 6f 6e 61      /* Additiona
2d6a2 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 73  l information as
2d6a3 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 54  sociated with pT
2d6a4 65 72 6d 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b  erm */.  Bitmask
2d6a5 20 63 68 6e 67 54 6f 49 4e 3b 20 20 20 20 20 20   chngToIN;      
2d6a6 20 20 20 2f 2a 20 54 61 62 6c 65 73 20 74 68 61     /* Tables tha
2d6a7 74 20 6d 69 67 68 74 20 73 61 74 69 73 66 79 20  t might satisfy 
2d6a8 63 61 73 65 20 31 20 2a 2f 0a 20 20 42 69 74 6d  case 1 */.  Bitm
2d6a9 61 73 6b 20 69 6e 64 65 78 61 62 6c 65 3b 20 20  ask indexable;  
2d6aa 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 73 20        /* Tables 
2d6ab 74 68 61 74 20 61 72 65 20 69 6e 64 65 78 61 62  that are indexab
2d6ac 6c 65 2c 20 73 61 74 69 73 66 79 69 6e 67 20 63  le, satisfying c
2d6ad 61 73 65 20 32 20 2a 2f 0a 0a 20 20 2f 2a 0a 20  ase 2 */..  /*. 
2d6ae 20 2a 2a 20 42 72 65 61 6b 20 74 68 65 20 4f 52   ** Break the OR
2d6af 20 63 6c 61 75 73 65 20 69 6e 74 6f 20 69 74 73   clause into its
2d6b0 20 73 65 70 61 72 61 74 65 20 73 75 62 74 65 72   separate subter
2d6b1 6d 73 2e 20 20 54 68 65 20 73 75 62 74 65 72 6d  ms.  The subterm
2d6b2 73 20 61 72 65 0a 20 20 2a 2a 20 73 74 6f 72 65  s are.  ** store
2d6b3 64 20 69 6e 20 61 20 57 68 65 72 65 43 6c 61 75  d in a WhereClau
2d6b4 73 65 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e  se structure con
2d6b5 74 61 69 6e 69 6e 67 20 77 69 74 68 69 6e 20 74  taining within t
2d6b6 68 65 20 57 68 65 72 65 4f 72 49 6e 66 6f 0a 20  he WhereOrInfo. 
2d6b7 20 2a 2a 20 6f 62 6a 65 63 74 20 74 68 61 74 20   ** object that 
2d6b8 69 73 20 61 74 74 61 63 68 65 64 20 74 6f 20 74  is attached to t
2d6b9 68 65 20 6f 72 69 67 69 6e 61 6c 20 4f 52 20 63  he original OR c
2d6ba 6c 61 75 73 65 20 74 65 72 6d 2e 0a 20 20 2a 2f  lause term..  */
2d6bb 0a 20 20 61 73 73 65 72 74 28 20 28 70 54 65 72  .  assert( (pTer
2d6bc 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 28 54 45  m->wtFlags & (TE
2d6bd 52 4d 5f 44 59 4e 41 4d 49 43 7c 54 45 52 4d 5f  RM_DYNAMIC|TERM_
2d6be 4f 52 49 4e 46 4f 7c 54 45 52 4d 5f 41 4e 44 49  ORINFO|TERM_ANDI
2d6bf 4e 46 4f 29 29 3d 3d 30 20 29 3b 0a 20 20 61 73  NFO))==0 );.  as
2d6c0 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 3d  sert( pExpr->op=
2d6c1 3d 54 4b 5f 4f 52 20 29 3b 0a 20 20 70 54 65 72  =TK_OR );.  pTer
2d6c2 6d 2d 3e 75 2e 70 4f 72 49 6e 66 6f 20 3d 20 70  m->u.pOrInfo = p
2d6c3 4f 72 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33  OrInfo = sqlite3
2d6c4 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
2d6c5 20 73 69 7a 65 6f 66 28 2a 70 4f 72 49 6e 66 6f   sizeof(*pOrInfo
2d6c6 29 29 3b 0a 20 20 69 66 28 20 70 4f 72 49 6e 66  ));.  if( pOrInf
2d6c7 6f 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  o==0 ) return;. 
2d6c8 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20   pTerm->wtFlags 
2d6c9 7c 3d 20 54 45 52 4d 5f 4f 52 49 4e 46 4f 3b 0a  |= TERM_ORINFO;.
2d6ca 20 20 70 4f 72 57 63 20 3d 20 26 70 4f 72 49 6e    pOrWc = &pOrIn
2d6cb 66 6f 2d 3e 77 63 3b 0a 20 20 77 68 65 72 65 43  fo->wc;.  whereC
2d6cc 6c 61 75 73 65 49 6e 69 74 28 70 4f 72 57 63 2c  lauseInit(pOrWc,
2d6cd 20 70 57 43 2d 3e 70 50 61 72 73 65 2c 20 70 4d   pWC->pParse, pM
2d6ce 61 73 6b 53 65 74 29 3b 0a 20 20 77 68 65 72 65  askSet);.  where
2d6cf 53 70 6c 69 74 28 70 4f 72 57 63 2c 20 70 45 78  Split(pOrWc, pEx
2d6d0 70 72 2c 20 54 4b 5f 4f 52 29 3b 0a 20 20 65 78  pr, TK_OR);.  ex
2d6d1 70 72 41 6e 61 6c 79 7a 65 41 6c 6c 28 70 53 72  prAnalyzeAll(pSr
2d6d2 63 2c 20 70 4f 72 57 63 29 3b 0a 20 20 69 66 28  c, pOrWc);.  if(
2d6d3 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2d6d4 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73  d ) return;.  as
2d6d5 73 65 72 74 28 20 70 4f 72 57 63 2d 3e 6e 54 65  sert( pOrWc->nTe
2d6d6 72 6d 3e 3d 32 20 29 3b 0a 0a 20 20 2f 2a 0a 20  rm>=2 );..  /*. 
2d6d7 20 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20   ** Compute the 
2d6d8 73 65 74 20 6f 66 20 74 61 62 6c 65 73 20 74 68  set of tables th
2d6d9 61 74 20 6d 69 67 68 74 20 73 61 74 69 73 66 79  at might satisfy
2d6da 20 63 61 73 65 73 20 31 20 6f 72 20 32 2e 0a 20   cases 1 or 2.. 
2d6db 20 2a 2f 0a 20 20 69 6e 64 65 78 61 62 6c 65 20   */.  indexable 
2d6dc 3d 20 7e 28 42 69 74 6d 61 73 6b 29 30 3b 0a 20  = ~(Bitmask)0;. 
2d6dd 20 63 68 6e 67 54 6f 49 4e 20 3d 20 7e 28 70 57   chngToIN = ~(pW
2d6de 43 2d 3e 76 6d 61 73 6b 29 3b 0a 20 20 66 6f 72  C->vmask);.  for
2d6df 28 69 3d 70 4f 72 57 63 2d 3e 6e 54 65 72 6d 2d  (i=pOrWc->nTerm-
2d6e0 31 2c 20 70 4f 72 54 65 72 6d 3d 70 4f 72 57 63  1, pOrTerm=pOrWc
2d6e1 2d 3e 61 3b 20 69 3e 3d 30 20 26 26 20 69 6e 64  ->a; i>=0 && ind
2d6e2 65 78 61 62 6c 65 3b 20 69 2d 2d 2c 20 70 4f 72  exable; i--, pOr
2d6e3 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28  Term++){.    if(
2d6e4 20 28 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72   (pOrTerm->eOper
2d6e5 61 74 6f 72 20 26 20 57 4f 5f 53 49 4e 47 4c 45  ator & WO_SINGLE
2d6e6 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 57 68  )==0 ){.      Wh
2d6e7 65 72 65 41 6e 64 49 6e 66 6f 20 2a 70 41 6e 64  ereAndInfo *pAnd
2d6e8 49 6e 66 6f 3b 0a 20 20 20 20 20 20 61 73 73 65  Info;.      asse
2d6e9 72 74 28 20 70 4f 72 54 65 72 6d 2d 3e 65 4f 70  rt( pOrTerm->eOp
2d6ea 65 72 61 74 6f 72 3d 3d 30 20 29 3b 0a 20 20 20  erator==0 );.   
2d6eb 20 20 20 61 73 73 65 72 74 28 20 28 70 4f 72 54     assert( (pOrT
2d6ec 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 28  erm->wtFlags & (
2d6ed 54 45 52 4d 5f 41 4e 44 49 4e 46 4f 7c 54 45 52  TERM_ANDINFO|TER
2d6ee 4d 5f 4f 52 49 4e 46 4f 29 29 3d 3d 30 20 29 3b  M_ORINFO))==0 );
2d6ef 0a 20 20 20 20 20 20 63 68 6e 67 54 6f 49 4e 20  .      chngToIN 
2d6f0 3d 20 30 3b 0a 20 20 20 20 20 20 70 41 6e 64 49  = 0;.      pAndI
2d6f1 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  nfo = sqlite3DbM
2d6f2 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a  allocRaw(db, siz
2d6f3 65 6f 66 28 2a 70 41 6e 64 49 6e 66 6f 29 29 3b  eof(*pAndInfo));
2d6f4 0a 20 20 20 20 20 20 69 66 28 20 70 41 6e 64 49  .      if( pAndI
2d6f5 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 57  nfo ){.        W
2d6f6 68 65 72 65 43 6c 61 75 73 65 20 2a 70 41 6e 64  hereClause *pAnd
2d6f7 57 43 3b 0a 20 20 20 20 20 20 20 20 57 68 65 72  WC;.        Wher
2d6f8 65 54 65 72 6d 20 2a 70 41 6e 64 54 65 72 6d 3b  eTerm *pAndTerm;
2d6f9 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a  .        int j;.
2d6fa 20 20 20 20 20 20 20 20 42 69 74 6d 61 73 6b 20          Bitmask 
2d6fb 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70  b = 0;.        p
2d6fc 4f 72 54 65 72 6d 2d 3e 75 2e 70 41 6e 64 49 6e  OrTerm->u.pAndIn
2d6fd 66 6f 20 3d 20 70 41 6e 64 49 6e 66 6f 3b 0a 20  fo = pAndInfo;. 
2d6fe 20 20 20 20 20 20 20 70 4f 72 54 65 72 6d 2d 3e         pOrTerm->
2d6ff 77 74 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f  wtFlags |= TERM_
2d700 41 4e 44 49 4e 46 4f 3b 0a 20 20 20 20 20 20 20  ANDINFO;.       
2d701 20 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61   pOrTerm->eOpera
2d702 74 6f 72 20 3d 20 57 4f 5f 41 4e 44 3b 0a 20 20  tor = WO_AND;.  
2d703 20 20 20 20 20 20 70 41 6e 64 57 43 20 3d 20 26        pAndWC = &
2d704 70 41 6e 64 49 6e 66 6f 2d 3e 77 63 3b 0a 20 20  pAndInfo->wc;.  
2d705 20 20 20 20 20 20 77 68 65 72 65 43 6c 61 75 73        whereClaus
2d706 65 49 6e 69 74 28 70 41 6e 64 57 43 2c 20 70 57  eInit(pAndWC, pW
2d707 43 2d 3e 70 50 61 72 73 65 2c 20 70 4d 61 73 6b  C->pParse, pMask
2d708 53 65 74 29 3b 0a 20 20 20 20 20 20 20 20 77 68  Set);.        wh
2d709 65 72 65 53 70 6c 69 74 28 70 41 6e 64 57 43 2c  ereSplit(pAndWC,
2d70a 20 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72 2c   pOrTerm->pExpr,
2d70b 20 54 4b 5f 41 4e 44 29 3b 0a 20 20 20 20 20 20   TK_AND);.      
2d70c 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 41 6c 6c    exprAnalyzeAll
2d70d 28 70 53 72 63 2c 20 70 41 6e 64 57 43 29 3b 0a  (pSrc, pAndWC);.
2d70e 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
2d70f 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
2d710 65 64 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66  ed );.        if
2d711 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  ( !db->mallocFai
2d712 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20  led ){.         
2d713 20 66 6f 72 28 6a 3d 30 2c 20 70 41 6e 64 54 65   for(j=0, pAndTe
2d714 72 6d 3d 70 41 6e 64 57 43 2d 3e 61 3b 20 6a 3c  rm=pAndWC->a; j<
2d715 70 41 6e 64 57 43 2d 3e 6e 54 65 72 6d 3b 20 6a  pAndWC->nTerm; j
2d716 2b 2b 2c 20 70 41 6e 64 54 65 72 6d 2b 2b 29 7b  ++, pAndTerm++){
2d717 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73  .            ass
2d718 65 72 74 28 20 70 41 6e 64 54 65 72 6d 2d 3e 70  ert( pAndTerm->p
2d719 45 78 70 72 20 29 3b 0a 20 20 20 20 20 20 20 20  Expr );.        
2d71a 20 20 20 20 69 66 28 20 61 6c 6c 6f 77 65 64 4f      if( allowedO
2d71b 70 28 70 41 6e 64 54 65 72 6d 2d 3e 70 45 78 70  p(pAndTerm->pExp
2d71c 72 2d 3e 6f 70 29 20 29 7b 0a 20 20 20 20 20 20  r->op) ){.      
2d71d 20 20 20 20 20 20 20 20 62 20 7c 3d 20 67 65 74          b |= get
2d71e 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70  Mask(pMaskSet, p
2d71f 41 6e 64 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72  AndTerm->leftCur
2d720 73 6f 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20  sor);.          
2d721 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a    }.          }.
2d722 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2d723 20 20 69 6e 64 65 78 61 62 6c 65 20 26 3d 20 62    indexable &= b
2d724 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
2d725 6c 73 65 20 69 66 28 20 70 4f 72 54 65 72 6d 2d  lse if( pOrTerm-
2d726 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f  >wtFlags & TERM_
2d727 43 4f 50 49 45 44 20 29 7b 0a 20 20 20 20 20 20  COPIED ){.      
2d728 2f 2a 20 53 6b 69 70 20 74 68 69 73 20 74 65 72  /* Skip this ter
2d729 6d 20 66 6f 72 20 6e 6f 77 2e 20 20 57 65 20 72  m for now.  We r
2d72a 65 76 69 73 69 74 20 69 74 20 77 68 65 6e 20 77  evisit it when w
2d72b 65 20 70 72 6f 63 65 73 73 20 74 68 65 0a 20 20  e process the.  
2d72c 20 20 20 20 2a 2a 20 63 6f 72 72 65 73 70 6f 6e      ** correspon
2d72d 64 69 6e 67 20 54 45 52 4d 5f 56 49 52 54 55 41  ding TERM_VIRTUA
2d72e 4c 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20 7d 65  L term */.    }e
2d72f 6c 73 65 7b 0a 20 20 20 20 20 20 42 69 74 6d 61  lse{.      Bitma
2d730 73 6b 20 62 3b 0a 20 20 20 20 20 20 62 20 3d 20  sk b;.      b = 
2d731 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74  getMask(pMaskSet
2d732 2c 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74 43  , pOrTerm->leftC
2d733 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 69 66  ursor);.      if
2d734 28 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61  ( pOrTerm->wtFla
2d735 67 73 20 26 20 54 45 52 4d 5f 56 49 52 54 55 41  gs & TERM_VIRTUA
2d736 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 57 68 65  L ){.        Whe
2d737 72 65 54 65 72 6d 20 2a 70 4f 74 68 65 72 20 3d  reTerm *pOther =
2d738 20 26 70 4f 72 57 63 2d 3e 61 5b 70 4f 72 54 65   &pOrWc->a[pOrTe
2d739 72 6d 2d 3e 69 50 61 72 65 6e 74 5d 3b 0a 20 20  rm->iParent];.  
2d73a 20 20 20 20 20 20 62 20 7c 3d 20 67 65 74 4d 61        b |= getMa
2d73b 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70 4f 74  sk(pMaskSet, pOt
2d73c 68 65 72 2d 3e 6c 65 66 74 43 75 72 73 6f 72 29  her->leftCursor)
2d73d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2d73e 69 6e 64 65 78 61 62 6c 65 20 26 3d 20 62 3b 0a  indexable &= b;.
2d73f 20 20 20 20 20 20 69 66 28 20 70 4f 72 54 65 72        if( pOrTer
2d740 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 21 3d 57 4f  m->eOperator!=WO
2d741 5f 45 51 20 29 7b 0a 20 20 20 20 20 20 20 20 63  _EQ ){.        c
2d742 68 6e 67 54 6f 49 4e 20 3d 20 30 3b 0a 20 20 20  hngToIN = 0;.   
2d743 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2d744 20 20 63 68 6e 67 54 6f 49 4e 20 26 3d 20 62 3b    chngToIN &= b;
2d745 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2d746 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52 65   }..  /*.  ** Re
2d747 63 6f 72 64 20 74 68 65 20 73 65 74 20 6f 66 20  cord the set of 
2d748 74 61 62 6c 65 73 20 74 68 61 74 20 73 61 74 69  tables that sati
2d749 73 66 79 20 63 61 73 65 20 32 2e 20 20 54 68 65  sfy case 2.  The
2d74a 20 73 65 74 20 6d 69 67 68 74 20 62 65 0a 20 20   set might be.  
2d74b 2a 2a 20 65 6d 70 74 79 2e 0a 20 20 2a 2f 0a 20  ** empty..  */. 
2d74c 20 70 4f 72 49 6e 66 6f 2d 3e 69 6e 64 65 78 61   pOrInfo->indexa
2d74d 62 6c 65 20 3d 20 69 6e 64 65 78 61 62 6c 65 3b  ble = indexable;
2d74e 0a 20 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61  .  pTerm->eOpera
2d74f 74 6f 72 20 3d 20 69 6e 64 65 78 61 62 6c 65 3d  tor = indexable=
2d750 3d 30 20 3f 20 30 20 3a 20 57 4f 5f 4f 52 3b 0a  =0 ? 0 : WO_OR;.
2d751 0a 20 20 2f 2a 0a 20 20 2a 2a 20 63 68 6e 67 54  .  /*.  ** chngT
2d752 6f 49 4e 20 68 6f 6c 64 73 20 61 20 73 65 74 20  oIN holds a set 
2d753 6f 66 20 74 61 62 6c 65 73 20 74 68 61 74 20 2a  of tables that *
2d754 6d 69 67 68 74 2a 20 73 61 74 69 73 66 79 20 63  might* satisfy c
2d755 61 73 65 20 31 2e 20 20 42 75 74 0a 20 20 2a 2a  ase 1.  But.  **
2d756 20 77 65 20 68 61 76 65 20 74 6f 20 64 6f 20 73   we have to do s
2d757 6f 6d 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 63  ome additional c
2d758 68 65 63 6b 69 6e 67 20 74 6f 20 73 65 65 20 69  hecking to see i
2d759 66 20 63 61 73 65 20 31 20 72 65 61 6c 6c 79 0a  f case 1 really.
2d75a 20 20 2a 2a 20 69 73 20 73 61 74 69 73 66 69 65    ** is satisfie
2d75b 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 63 68 6e  d..  **.  ** chn
2d75c 67 54 6f 49 4e 20 77 69 6c 6c 20 68 6f 6c 64 20  gToIN will hold 
2d75d 65 69 74 68 65 72 20 30 2c 20 31 2c 20 6f 72 20  either 0, 1, or 
2d75e 32 20 62 69 74 73 2e 20 20 54 68 65 20 30 2d 62  2 bits.  The 0-b
2d75f 69 74 20 63 61 73 65 20 6d 65 61 6e 73 0a 20 20  it case means.  
2d760 2a 2a 20 74 68 61 74 20 74 68 65 72 65 20 69 73  ** that there is
2d761 20 6e 6f 20 70 6f 73 73 69 62 69 6c 69 74 79 20   no possibility 
2d762 6f 66 20 74 72 61 6e 73 66 6f 72 6d 69 6e 67 20  of transforming 
2d763 74 68 65 20 4f 52 20 63 6c 61 75 73 65 20 69 6e  the OR clause in
2d764 74 6f 20 61 6e 0a 20 20 2a 2a 20 49 4e 20 6f 70  to an.  ** IN op
2d765 65 72 61 74 6f 72 20 62 65 63 61 75 73 65 20 6f  erator because o
2d766 6e 65 20 6f 72 20 6d 6f 72 65 20 74 65 72 6d 73  ne or more terms
2d767 20 69 6e 20 74 68 65 20 4f 52 20 63 6c 61 75 73   in the OR claus
2d768 65 20 63 6f 6e 74 61 69 6e 0a 20 20 2a 2a 20 73  e contain.  ** s
2d769 6f 6d 65 74 68 69 6e 67 20 6f 74 68 65 72 20 74  omething other t
2d76a 68 61 6e 20 3d 3d 20 6f 6e 20 61 20 63 6f 6c 75  han == on a colu
2d76b 6d 6e 20 69 6e 20 74 68 65 20 73 69 6e 67 6c 65  mn in the single
2d76c 20 74 61 62 6c 65 2e 20 20 54 68 65 20 31 2d 62   table.  The 1-b
2d76d 69 74 0a 20 20 2a 2a 20 63 61 73 65 20 6d 65 61  it.  ** case mea
2d76e 6e 73 20 74 68 61 74 20 65 76 65 72 79 20 74 65  ns that every te
2d76f 72 6d 20 6f 66 20 74 68 65 20 4f 52 20 63 6c 61  rm of the OR cla
2d770 75 73 65 20 69 73 20 6f 66 20 74 68 65 20 66 6f  use is of the fo
2d771 72 6d 0a 20 20 2a 2a 20 22 74 61 62 6c 65 2e 63  rm.  ** "table.c
2d772 6f 6c 75 6d 6e 3d 65 78 70 72 22 20 66 6f 72 20  olumn=expr" for 
2d773 73 6f 6d 65 20 73 69 6e 67 6c 65 20 74 61 62 6c  some single tabl
2d774 65 2e 20 20 54 68 65 20 6f 6e 65 20 62 69 74 20  e.  The one bit 
2d775 74 68 61 74 20 69 73 20 73 65 74 0a 20 20 2a 2a  that is set.  **
2d776 20 77 69 6c 6c 20 63 6f 72 72 65 73 70 6f 6e 64   will correspond
2d777 20 74 6f 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 74   to the common t
2d778 61 62 6c 65 2e 20 20 57 65 20 73 74 69 6c 6c 20  able.  We still 
2d779 6e 65 65 64 20 74 6f 20 63 68 65 63 6b 20 74 6f  need to check to
2d77a 20 6d 61 6b 65 0a 20 20 2a 2a 20 73 75 72 65 20   make.  ** sure 
2d77b 74 68 65 20 73 61 6d 65 20 63 6f 6c 75 6d 6e 20  the same column 
2d77c 69 73 20 75 73 65 64 20 6f 6e 20 61 6c 6c 20 74  is used on all t
2d77d 65 72 6d 73 2e 20 20 54 68 65 20 32 2d 62 69 74  erms.  The 2-bit
2d77e 20 63 61 73 65 20 69 73 20 77 68 65 6e 0a 20 20   case is when.  
2d77f 2a 2a 20 74 68 65 20 61 6c 6c 20 74 65 72 6d 73  ** the all terms
2d780 20 61 72 65 20 6f 66 20 74 68 65 20 66 6f 72 6d   are of the form
2d781 20 22 74 61 62 6c 65 31 2e 63 6f 6c 75 6d 6e 3d   "table1.column=
2d782 74 61 62 6c 65 32 2e 63 6f 6c 75 6d 6e 22 2e 20  table2.column". 
2d783 20 49 74 0a 20 20 2a 2a 20 6d 69 67 68 74 20 62   It.  ** might b
2d784 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 66 6f  e possible to fo
2d785 72 6d 20 61 6e 20 49 4e 20 6f 70 65 72 61 74 6f  rm an IN operato
2d786 72 20 77 69 74 68 20 65 69 74 68 65 72 20 74 61  r with either ta
2d787 62 6c 65 31 2e 63 6f 6c 75 6d 6e 0a 20 20 2a 2a  ble1.column.  **
2d788 20 6f 72 20 74 61 62 6c 65 32 2e 63 6f 6c 75 6d   or table2.colum
2d789 6e 20 61 73 20 74 68 65 20 4c 48 53 20 69 66 20  n as the LHS if 
2d78a 65 69 74 68 65 72 20 69 73 20 63 6f 6d 6d 6f 6e  either is common
2d78b 20 74 6f 20 65 76 65 72 79 20 74 65 72 6d 20 6f   to every term o
2d78c 66 0a 20 20 2a 2a 20 74 68 65 20 4f 52 20 63 6c  f.  ** the OR cl
2d78d 61 75 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ause..  **.  ** 
2d78e 4e 6f 74 65 20 74 68 61 74 20 74 65 72 6d 73 20  Note that terms 
2d78f 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 74 61 62  of the form "tab
2d790 6c 65 2e 63 6f 6c 75 6d 6e 31 3d 74 61 62 6c 65  le.column1=table
2d791 2e 63 6f 6c 75 6d 6e 32 22 20 28 74 68 65 0a 20  .column2" (the. 
2d792 20 2a 2a 20 73 61 6d 65 20 74 61 62 6c 65 20 6f   ** same table o
2d793 6e 20 62 6f 74 68 20 73 69 7a 65 73 20 6f 66 20  n both sizes of 
2d794 74 68 65 20 3d 3d 29 20 63 61 6e 6e 6f 74 20 62  the ==) cannot b
2d795 65 20 6f 70 74 69 6d 69 7a 65 64 2e 0a 20 20 2a  e optimized..  *
2d796 2f 0a 20 20 69 66 28 20 63 68 6e 67 54 6f 49 4e  /.  if( chngToIN
2d797 20 29 7b 0a 20 20 20 20 69 6e 74 20 6f 6b 54 6f   ){.    int okTo
2d798 43 68 6e 67 54 6f 49 4e 20 3d 20 30 3b 20 20 20  ChngToIN = 0;   
2d799 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65    /* True if the
2d79a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 74 6f 20 49   conversion to I
2d79b 4e 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20  N is valid */.  
2d79c 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 20 3d 20    int iColumn = 
2d79d 2d 31 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43  -1;         /* C
2d79e 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 6f 6e 20 6c  olumn index on l
2d79f 68 73 20 6f 66 20 49 4e 20 6f 70 65 72 61 74 6f  hs of IN operato
2d7a0 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 75  r */.    int iCu
2d7a1 72 73 6f 72 20 3d 20 2d 31 3b 20 20 20 20 20 20  rsor = -1;      
2d7a2 20 20 20 2f 2a 20 54 61 62 6c 65 20 63 75 72 73     /* Table curs
2d7a3 6f 72 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c  or common to all
2d7a4 20 74 65 72 6d 73 20 2a 2f 0a 20 20 20 20 69 6e   terms */.    in
2d7a5 74 20 6a 20 3d 20 30 3b 20 20 20 20 20 20 20 20  t j = 0;        
2d7a6 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
2d7a7 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20 20 20  counter */..    
2d7a8 2f 2a 20 53 65 61 72 63 68 20 66 6f 72 20 61 20  /* Search for a 
2d7a9 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e  table and column
2d7aa 20 74 68 61 74 20 61 70 70 65 61 72 73 20 6f 6e   that appears on
2d7ab 20 6f 6e 65 20 73 69 64 65 20 6f 72 20 74 68 65   one side or the
2d7ac 0a 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 6f 66  .    ** other of
2d7ad 20 74 68 65 20 3d 3d 20 6f 70 65 72 61 74 6f 72   the == operator
2d7ae 20 69 6e 20 65 76 65 72 79 20 73 75 62 74 65 72   in every subter
2d7af 6d 2e 20 20 54 68 61 74 20 74 61 62 6c 65 20 61  m.  That table a
2d7b0 6e 64 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2a  nd column.    **
2d7b1 20 77 69 6c 6c 20 62 65 20 72 65 63 6f 72 64 65   will be recorde
2d7b2 64 20 69 6e 20 69 43 75 72 73 6f 72 20 61 6e 64  d in iCursor and
2d7b3 20 69 43 6f 6c 75 6d 6e 2e 20 20 54 68 65 72 65   iColumn.  There
2d7b4 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 20 61 6e   might not be an
2d7b5 79 0a 20 20 20 20 2a 2a 20 73 75 63 68 20 74 61  y.    ** such ta
2d7b6 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 2e 20  ble and column. 
2d7b7 20 53 65 74 20 6f 6b 54 6f 43 68 6e 67 54 6f 49   Set okToChngToI
2d7b8 4e 20 69 66 20 61 6e 20 61 70 70 72 6f 70 72 69  N if an appropri
2d7b9 61 74 65 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a  ate table.    **
2d7ba 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 69 73 20 66   and column is f
2d7bb 6f 75 6e 64 20 62 75 74 20 6c 65 61 76 65 20 6f  ound but leave o
2d7bc 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 66 61 6c 73  kToChngToIN fals
2d7bd 65 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a  e if not found..
2d7be 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 6a      */.    for(j
2d7bf 3d 30 3b 20 6a 3c 32 20 26 26 20 21 6f 6b 54 6f  =0; j<2 && !okTo
2d7c0 43 68 6e 67 54 6f 49 4e 3b 20 6a 2b 2b 29 7b 0a  ChngToIN; j++){.
2d7c1 20 20 20 20 20 20 70 4f 72 54 65 72 6d 20 3d 20        pOrTerm = 
2d7c2 70 4f 72 57 63 2d 3e 61 3b 0a 20 20 20 20 20 20  pOrWc->a;.      
2d7c3 66 6f 72 28 69 3d 70 4f 72 57 63 2d 3e 6e 54 65  for(i=pOrWc->nTe
2d7c4 72 6d 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c  rm-1; i>=0; i--,
2d7c5 20 70 4f 72 54 65 72 6d 2b 2b 29 7b 0a 20 20 20   pOrTerm++){.   
2d7c6 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 72       assert( pOr
2d7c7 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d  Term->eOperator=
2d7c8 3d 57 4f 5f 45 51 20 29 3b 0a 20 20 20 20 20 20  =WO_EQ );.      
2d7c9 20 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61    pOrTerm->wtFla
2d7ca 67 73 20 26 3d 20 7e 54 45 52 4d 5f 4f 52 5f 4f  gs &= ~TERM_OR_O
2d7cb 4b 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  K;.        if( p
2d7cc 4f 72 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73  OrTerm->leftCurs
2d7cd 6f 72 3d 3d 69 43 75 72 73 6f 72 20 29 7b 0a 20  or==iCursor ){. 
2d7ce 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73           /* This
2d7cf 20 69 73 20 74 68 65 20 32 2d 62 69 74 20 63 61   is the 2-bit ca
2d7d0 73 65 20 61 6e 64 20 77 65 20 61 72 65 20 6f 6e  se and we are on
2d7d1 20 74 68 65 20 73 65 63 6f 6e 64 20 69 74 65 72   the second iter
2d7d2 61 74 69 6f 6e 20 61 6e 64 0a 20 20 20 20 20 20  ation and.      
2d7d3 20 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 74      ** current t
2d7d4 65 72 6d 20 69 73 20 66 72 6f 6d 20 74 68 65 20  erm is from the 
2d7d5 66 69 72 73 74 20 69 74 65 72 61 74 69 6f 6e 2e  first iteration.
2d7d6 20 20 53 6f 20 73 6b 69 70 20 74 68 69 73 20 74    So skip this t
2d7d7 65 72 6d 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  erm. */.        
2d7d8 20 20 61 73 73 65 72 74 28 20 6a 3d 3d 31 20 29    assert( j==1 )
2d7d9 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74  ;.          cont
2d7da 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a  inue;.        }.
2d7db 20 20 20 20 20 20 20 20 69 66 28 20 28 63 68 6e          if( (chn
2d7dc 67 54 6f 49 4e 20 26 20 67 65 74 4d 61 73 6b 28  gToIN & getMask(
2d7dd 70 4d 61 73 6b 53 65 74 2c 20 70 4f 72 54 65 72  pMaskSet, pOrTer
2d7de 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 29 29 3d  m->leftCursor))=
2d7df 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
2d7e0 2f 2a 20 54 68 69 73 20 74 65 72 6d 20 6d 75 73  /* This term mus
2d7e1 74 20 62 65 20 6f 66 20 74 68 65 20 66 6f 72 6d  t be of the form
2d7e2 20 74 31 2e 61 3d 3d 74 32 2e 62 20 77 68 65 72   t1.a==t2.b wher
2d7e3 65 20 74 32 20 69 73 20 69 6e 20 74 68 65 0a 20  e t2 is in the. 
2d7e4 20 20 20 20 20 20 20 20 20 2a 2a 20 63 68 6e 67           ** chng
2d7e5 54 6f 49 4e 20 73 65 74 20 62 75 74 20 74 31 20  ToIN set but t1 
2d7e6 69 73 20 6e 6f 74 2e 20 20 54 68 69 73 20 74 65  is not.  This te
2d7e7 72 6d 20 77 69 6c 6c 20 62 65 20 65 69 74 68 65  rm will be eithe
2d7e8 72 20 70 72 65 63 65 65 64 65 64 0a 20 20 20 20  r preceeded.    
2d7e9 20 20 20 20 20 20 2a 2a 20 6f 72 20 66 6f 6c 6c        ** or foll
2d7ea 77 65 64 20 62 79 20 61 6e 20 69 6e 76 65 72 74  wed by an invert
2d7eb 65 64 20 63 6f 70 79 20 28 74 32 2e 62 3d 3d 74  ed copy (t2.b==t
2d7ec 31 2e 61 29 2e 20 20 53 6b 69 70 20 74 68 69 73  1.a).  Skip this
2d7ed 20 74 65 72 6d 20 0a 20 20 20 20 20 20 20 20 20   term .         
2d7ee 20 2a 2a 20 61 6e 64 20 75 73 65 20 69 74 73 20   ** and use its 
2d7ef 69 6e 76 65 72 73 69 6f 6e 2e 20 2a 2f 0a 20 20  inversion. */.  
2d7f0 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
2d7f1 28 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61  ( pOrTerm->wtFla
2d7f2 67 73 20 26 20 54 45 52 4d 5f 43 4f 50 49 45 44  gs & TERM_COPIED
2d7f3 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 74 65   );.          te
2d7f4 73 74 63 61 73 65 28 20 70 4f 72 54 65 72 6d 2d  stcase( pOrTerm-
2d7f5 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f  >wtFlags & TERM_
2d7f6 56 49 52 54 55 41 4c 20 29 3b 0a 20 20 20 20 20  VIRTUAL );.     
2d7f7 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 72       assert( pOr
2d7f8 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20  Term->wtFlags & 
2d7f9 28 54 45 52 4d 5f 43 4f 50 49 45 44 7c 54 45 52  (TERM_COPIED|TER
2d7fa 4d 5f 56 49 52 54 55 41 4c 29 20 29 3b 0a 20 20  M_VIRTUAL) );.  
2d7fb 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65          continue
2d7fc 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2d7fd 20 20 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 70 4f      iColumn = pO
2d7fe 72 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c  rTerm->u.leftCol
2d7ff 75 6d 6e 3b 0a 20 20 20 20 20 20 20 20 69 43 75  umn;.        iCu
2d800 72 73 6f 72 20 3d 20 70 4f 72 54 65 72 6d 2d 3e  rsor = pOrTerm->
2d801 6c 65 66 74 43 75 72 73 6f 72 3b 0a 20 20 20 20  leftCursor;.    
2d802 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2d803 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 3c 30   }.      if( i<0
2d804 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e   ){.        /* N
2d805 6f 20 63 61 6e 64 69 64 61 74 65 20 74 61 62 6c  o candidate tabl
2d806 65 2b 63 6f 6c 75 6d 6e 20 77 61 73 20 66 6f 75  e+column was fou
2d807 6e 64 2e 20 20 54 68 69 73 20 63 61 6e 20 6f 6e  nd.  This can on
2d808 6c 79 20 6f 63 63 75 72 0a 20 20 20 20 20 20 20  ly occur.       
2d809 20 2a 2a 20 6f 6e 20 74 68 65 20 73 65 63 6f 6e   ** on the secon
2d80a 64 20 69 74 65 72 61 74 69 6f 6e 20 2a 2f 0a 20  d iteration */. 
2d80b 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6a         assert( j
2d80c 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 61  ==1 );.        a
2d80d 73 73 65 72 74 28 20 28 63 68 6e 67 54 6f 49 4e  ssert( (chngToIN
2d80e 26 28 63 68 6e 67 54 6f 49 4e 2d 31 29 29 3d 3d  &(chngToIN-1))==
2d80f 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73  0 );.        ass
2d810 65 72 74 28 20 63 68 6e 67 54 6f 49 4e 3d 3d 67  ert( chngToIN==g
2d811 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c  etMask(pMaskSet,
2d812 20 69 43 75 72 73 6f 72 29 20 29 3b 0a 20 20 20   iCursor) );.   
2d813 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2d814 20 20 7d 0a 20 20 20 20 20 20 74 65 73 74 63 61    }.      testca
2d815 73 65 28 20 6a 3d 3d 31 20 29 3b 0a 0a 20 20 20  se( j==1 );..   
2d816 20 20 20 2f 2a 20 57 65 20 68 61 76 65 20 66 6f     /* We have fo
2d817 75 6e 64 20 61 20 63 61 6e 64 69 64 61 74 65 20  und a candidate 
2d818 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e  table and column
2d819 2e 20 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20  .  Check to see 
2d81a 69 66 20 74 68 61 74 0a 20 20 20 20 20 20 2a 2a  if that.      **
2d81b 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d   table and colum
2d81c 6e 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 65  n is common to e
2d81d 76 65 72 79 20 74 65 72 6d 20 69 6e 20 74 68 65  very term in the
2d81e 20 4f 52 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20   OR clause */.  
2d81f 20 20 20 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e      okToChngToIN
2d820 20 3d 20 31 3b 0a 20 20 20 20 20 20 66 6f 72 28   = 1;.      for(
2d821 3b 20 69 3e 3d 30 20 26 26 20 6f 6b 54 6f 43 68  ; i>=0 && okToCh
2d822 6e 67 54 6f 49 4e 3b 20 69 2d 2d 2c 20 70 4f 72  ngToIN; i--, pOr
2d823 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20  Term++){.       
2d824 20 61 73 73 65 72 74 28 20 70 4f 72 54 65 72 6d   assert( pOrTerm
2d825 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f  ->eOperator==WO_
2d826 45 51 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66  EQ );.        if
2d827 28 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74 43  ( pOrTerm->leftC
2d828 75 72 73 6f 72 21 3d 69 43 75 72 73 6f 72 20 29  ursor!=iCursor )
2d829 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 72 54  {.          pOrT
2d82a 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 3d 20  erm->wtFlags &= 
2d82b 7e 54 45 52 4d 5f 4f 52 5f 4f 4b 3b 0a 20 20 20  ~TERM_OR_OK;.   
2d82c 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70       }else if( p
2d82d 4f 72 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f  OrTerm->u.leftCo
2d82e 6c 75 6d 6e 21 3d 69 43 6f 6c 75 6d 6e 20 29 7b  lumn!=iColumn ){
2d82f 0a 20 20 20 20 20 20 20 20 20 20 6f 6b 54 6f 43  .          okToC
2d830 68 6e 67 54 6f 49 4e 20 3d 20 30 3b 0a 20 20 20  hngToIN = 0;.   
2d831 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2d832 20 20 20 20 20 20 69 6e 74 20 61 66 66 4c 65 66        int affLef
2d833 74 2c 20 61 66 66 52 69 67 68 74 3b 0a 20 20 20  t, affRight;.   
2d834 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65         /* If the
2d835 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65   right-hand side
2d836 20 69 73 20 61 6c 73 6f 20 61 20 63 6f 6c 75 6d   is also a colum
2d837 6e 2c 20 74 68 65 6e 20 74 68 65 20 61 66 66 69  n, then the affi
2d838 6e 69 74 69 65 73 0a 20 20 20 20 20 20 20 20 20  nities.         
2d839 20 2a 2a 20 6f 66 20 62 6f 74 68 20 72 69 67 68   ** of both righ
2d83a 74 20 61 6e 64 20 6c 65 66 74 20 73 69 64 65 73  t and left sides
2d83b 20 6d 75 73 74 20 62 65 20 73 75 63 68 20 74 68   must be such th
2d83c 61 74 20 6e 6f 20 74 79 70 65 0a 20 20 20 20 20  at no type.     
2d83d 20 20 20 20 20 2a 2a 20 63 6f 6e 76 65 72 73 69       ** conversi
2d83e 6f 6e 73 20 61 72 65 20 72 65 71 75 69 72 65 64  ons are required
2d83f 20 6f 6e 20 74 68 65 20 72 69 67 68 74 2e 20 20   on the right.  
2d840 28 54 69 63 6b 65 74 20 23 32 32 34 39 29 0a 20  (Ticket #2249). 
2d841 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20           */.    
2d842 20 20 20 20 20 20 61 66 66 52 69 67 68 74 20 3d        affRight =
2d843 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69   sqlite3ExprAffi
2d844 6e 69 74 79 28 70 4f 72 54 65 72 6d 2d 3e 70 45  nity(pOrTerm->pE
2d845 78 70 72 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20  xpr->pRight);.  
2d846 20 20 20 20 20 20 20 20 61 66 66 4c 65 66 74 20          affLeft 
2d847 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66  = sqlite3ExprAff
2d848 69 6e 69 74 79 28 70 4f 72 54 65 72 6d 2d 3e 70  inity(pOrTerm->p
2d849 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20  Expr->pLeft);.  
2d84a 20 20 20 20 20 20 20 20 69 66 28 20 61 66 66 52          if( affR
2d84b 69 67 68 74 21 3d 30 20 26 26 20 61 66 66 52 69  ight!=0 && affRi
2d84c 67 68 74 21 3d 61 66 66 4c 65 66 74 20 29 7b 0a  ght!=affLeft ){.
2d84d 20 20 20 20 20 20 20 20 20 20 20 20 6f 6b 54 6f              okTo
2d84e 43 68 6e 67 54 6f 49 4e 20 3d 20 30 3b 0a 20 20  ChngToIN = 0;.  
2d84f 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
2d850 20 20 20 20 20 20 20 20 20 20 20 70 4f 72 54 65             pOrTe
2d851 72 6d 2d 3e 77 74 46 6c 61 67 73 20 7c 3d 20 54  rm->wtFlags |= T
2d852 45 52 4d 5f 4f 52 5f 4f 4b 3b 0a 20 20 20 20 20  ERM_OR_OK;.     
2d853 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
2d854 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
2d855 20 20 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70      /* At this p
2d856 6f 69 6e 74 2c 20 6f 6b 54 6f 43 68 6e 67 54 6f  oint, okToChngTo
2d857 49 4e 20 69 73 20 74 72 75 65 20 69 66 20 6f 72  IN is true if or
2d858 69 67 69 6e 61 6c 20 70 54 65 72 6d 20 73 61 74  iginal pTerm sat
2d859 69 73 66 69 65 73 0a 20 20 20 20 2a 2a 20 63 61  isfies.    ** ca
2d85a 73 65 20 31 2e 20 20 49 6e 20 74 68 61 74 20 63  se 1.  In that c
2d85b 61 73 65 2c 20 63 6f 6e 73 74 72 75 63 74 20 61  ase, construct a
2d85c 20 6e 65 77 20 76 69 72 74 75 61 6c 20 74 65 72   new virtual ter
2d85d 6d 20 74 68 61 74 20 69 73 20 0a 20 20 20 20 2a  m that is .    *
2d85e 2a 20 70 54 65 72 6d 20 63 6f 6e 76 65 72 74 65  * pTerm converte
2d85f 64 20 69 6e 74 6f 20 61 6e 20 49 4e 20 6f 70 65  d into an IN ope
2d860 72 61 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20  rator..    */.  
2d861 20 20 69 66 28 20 6f 6b 54 6f 43 68 6e 67 54 6f    if( okToChngTo
2d862 49 4e 20 29 7b 0a 20 20 20 20 20 20 45 78 70 72  IN ){.      Expr
2d863 20 2a 70 44 75 70 3b 20 20 20 20 20 20 20 20 20   *pDup;         
2d864 20 20 20 2f 2a 20 41 20 74 72 61 6e 73 69 65 6e     /* A transien
2d865 74 20 64 75 70 6c 69 63 61 74 65 20 65 78 70 72  t duplicate expr
2d866 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20  ession */.      
2d867 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20  ExprList *pList 
2d868 3d 20 30 3b 20 20 20 2f 2a 20 54 68 65 20 52 48  = 0;   /* The RH
2d869 53 20 6f 66 20 74 68 65 20 49 4e 20 6f 70 65 72  S of the IN oper
2d86a 61 74 6f 72 20 2a 2f 0a 20 20 20 20 20 20 45 78  ator */.      Ex
2d86b 70 72 20 2a 70 4c 65 66 74 20 3d 20 30 3b 20 20  pr *pLeft = 0;  
2d86c 20 20 20 20 20 2f 2a 20 54 68 65 20 4c 48 53 20       /* The LHS 
2d86d 6f 66 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74  of the IN operat
2d86e 6f 72 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72  or */.      Expr
2d86f 20 2a 70 4e 65 77 3b 20 20 20 20 20 20 20 20 20   *pNew;         
2d870 20 20 20 2f 2a 20 54 68 65 20 63 6f 6d 70 6c 65     /* The comple
2d871 74 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a  te IN operator *
2d872 2f 0a 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 70  /..      for(i=p
2d873 4f 72 57 63 2d 3e 6e 54 65 72 6d 2d 31 2c 20 70  OrWc->nTerm-1, p
2d874 4f 72 54 65 72 6d 3d 70 4f 72 57 63 2d 3e 61 3b  OrTerm=pOrWc->a;
2d875 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70 4f 72 54   i>=0; i--, pOrT
2d876 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  erm++){.        
2d877 69 66 28 20 28 70 4f 72 54 65 72 6d 2d 3e 77 74  if( (pOrTerm->wt
2d878 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 4f 52 5f  Flags & TERM_OR_
2d879 4f 4b 29 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75  OK)==0 ) continu
2d87a 65 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  e;.        asser
2d87b 74 28 20 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65  t( pOrTerm->eOpe
2d87c 72 61 74 6f 72 3d 3d 57 4f 5f 45 51 20 29 3b 0a  rator==WO_EQ );.
2d87d 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
2d87e 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72  pOrTerm->leftCur
2d87f 73 6f 72 3d 3d 69 43 75 72 73 6f 72 20 29 3b 0a  sor==iCursor );.
2d880 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
2d881 70 4f 72 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43  pOrTerm->u.leftC
2d882 6f 6c 75 6d 6e 3d 3d 69 43 6f 6c 75 6d 6e 20 29  olumn==iColumn )
2d883 3b 0a 20 20 20 20 20 20 20 20 70 44 75 70 20 3d  ;.        pDup =
2d884 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
2d885 64 62 2c 20 70 4f 72 54 65 72 6d 2d 3e 70 45 78  db, pOrTerm->pEx
2d886 70 72 2d 3e 70 52 69 67 68 74 2c 20 30 29 3b 0a  pr->pRight, 0);.
2d887 20 20 20 20 20 20 20 20 70 4c 69 73 74 20 3d 20          pList = 
2d888 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
2d889 70 70 65 6e 64 28 70 57 43 2d 3e 70 50 61 72 73  ppend(pWC->pPars
2d88a 65 2c 20 70 4c 69 73 74 2c 20 70 44 75 70 29 3b  e, pList, pDup);
2d88b 0a 20 20 20 20 20 20 20 20 70 4c 65 66 74 20 3d  .        pLeft =
2d88c 20 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72 2d   pOrTerm->pExpr-
2d88d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 7d 0a  >pLeft;.      }.
2d88e 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4c        assert( pL
2d88f 65 66 74 21 3d 30 20 29 3b 0a 20 20 20 20 20 20  eft!=0 );.      
2d890 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45 78  pDup = sqlite3Ex
2d891 70 72 44 75 70 28 64 62 2c 20 70 4c 65 66 74 2c  prDup(db, pLeft,
2d892 20 30 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 20   0);.      pNew 
2d893 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
2d894 50 61 72 73 65 2c 20 54 4b 5f 49 4e 2c 20 70 44  Parse, TK_IN, pD
2d895 75 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20  up, 0, 0);.     
2d896 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20   if( pNew ){.   
2d897 20 20 20 20 20 69 6e 74 20 69 64 78 4e 65 77 3b       int idxNew;
2d898 0a 20 20 20 20 20 20 20 20 74 72 61 6e 73 66 65  .        transfe
2d899 72 4a 6f 69 6e 4d 61 72 6b 69 6e 67 73 28 70 4e  rJoinMarkings(pN
2d89a 65 77 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20  ew, pExpr);.    
2d89b 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70      assert( !Exp
2d89c 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 4e 65  rHasProperty(pNe
2d89d 77 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  w, EP_xIsSelect)
2d89e 20 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77   );.        pNew
2d89f 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 70 4c 69 73  ->x.pList = pLis
2d8a0 74 3b 0a 20 20 20 20 20 20 20 20 69 64 78 4e 65  t;.        idxNe
2d8a1 77 20 3d 20 77 68 65 72 65 43 6c 61 75 73 65 49  w = whereClauseI
2d8a2 6e 73 65 72 74 28 70 57 43 2c 20 70 4e 65 77 2c  nsert(pWC, pNew,
2d8a3 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45   TERM_VIRTUAL|TE
2d8a4 52 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20  RM_DYNAMIC);.   
2d8a5 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
2d8a6 64 78 4e 65 77 3d 3d 30 20 29 3b 0a 20 20 20 20  dxNew==0 );.    
2d8a7 20 20 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 28      exprAnalyze(
2d8a8 70 53 72 63 2c 20 70 57 43 2c 20 69 64 78 4e 65  pSrc, pWC, idxNe
2d8a9 77 29 3b 0a 20 20 20 20 20 20 20 20 70 54 65 72  w);.        pTer
2d8aa 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 54  m = &pWC->a[idxT
2d8ab 65 72 6d 5d 3b 0a 20 20 20 20 20 20 20 20 70 57  erm];.        pW
2d8ac 43 2d 3e 61 5b 69 64 78 4e 65 77 5d 2e 69 50 61  C->a[idxNew].iPa
2d8ad 72 65 6e 74 20 3d 20 69 64 78 54 65 72 6d 3b 0a  rent = idxTerm;.
2d8ae 20 20 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 6e          pTerm->n
2d8af 43 68 69 6c 64 20 3d 20 31 3b 0a 20 20 20 20 20  Child = 1;.     
2d8b0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2d8b1 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
2d8b2 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73 74 29  elete(db, pList)
2d8b3 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2d8b4 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
2d8b5 20 3d 20 30 3b 20 20 2f 2a 20 63 61 73 65 20 31   = 0;  /* case 1
2d8b6 20 74 72 75 6d 70 73 20 63 61 73 65 20 32 20 2a   trumps case 2 *
2d8b7 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65  /.    }.  }.}.#e
2d8b8 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f  ndif /* !SQLITE_
2d8b9 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41  OMIT_OR_OPTIMIZA
2d8ba 54 49 4f 4e 20 26 26 20 21 53 51 4c 49 54 45 5f  TION && !SQLITE_
2d8bb 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f  OMIT_SUBQUERY */
2d8bc 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 70  .../*.** The inp
2d8bd 75 74 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69  ut to this routi
2d8be 6e 65 20 69 73 20 61 6e 20 57 68 65 72 65 54 65  ne is an WhereTe
2d8bf 72 6d 20 73 74 72 75 63 74 75 72 65 20 77 69 74  rm structure wit
2d8c0 68 20 6f 6e 6c 79 20 74 68 65 0a 2a 2a 20 22 70  h only the.** "p
2d8c1 45 78 70 72 22 20 66 69 65 6c 64 20 66 69 6c 6c  Expr" field fill
2d8c2 65 64 20 69 6e 2e 20 20 54 68 65 20 6a 6f 62 20  ed in.  The job 
2d8c3 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  of this routine 
2d8c4 69 73 20 74 6f 20 61 6e 61 6c 79 7a 65 20 74 68  is to analyze th
2d8c5 65 0a 2a 2a 20 73 75 62 65 78 70 72 65 73 73 69  e.** subexpressi
2d8c6 6f 6e 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 20  on and populate 
2d8c7 61 6c 6c 20 74 68 65 20 6f 74 68 65 72 20 66 69  all the other fi
2d8c8 65 6c 64 73 20 6f 66 20 74 68 65 20 57 68 65 72  elds of the Wher
2d8c9 65 54 65 72 6d 0a 2a 2a 20 73 74 72 75 63 74 75  eTerm.** structu
2d8ca 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  re..**.** If the
2d8cb 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6f   expression is o
2d8cc 66 20 74 68 65 20 66 6f 72 6d 20 22 3c 65 78 70  f the form "<exp
2d8cd 72 3e 20 3c 6f 70 3e 20 58 22 20 69 74 20 67 65  r> <op> X" it ge
2d8ce 74 73 20 63 6f 6d 6d 75 74 65 64 0a 2a 2a 20 74  ts commuted.** t
2d8cf 6f 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 66  o the standard f
2d8d0 6f 72 6d 20 6f 66 20 22 58 20 3c 6f 70 3e 20 3c  orm of "X <op> <
2d8d1 65 78 70 72 3e 22 2e 0a 2a 2a 0a 2a 2a 20 49 66  expr>"..**.** If
2d8d2 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
2d8d3 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22  is of the form "
2d8d4 58 20 3c 6f 70 3e 20 59 22 20 77 68 65 72 65 20  X <op> Y" where 
2d8d5 62 6f 74 68 20 58 20 61 6e 64 20 59 20 61 72 65  both X and Y are
2d8d6 0a 2a 2a 20 63 6f 6c 75 6d 6e 73 2c 20 74 68 65  .** columns, the
2d8d7 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 65  n the original e
2d8d8 78 70 72 65 73 73 69 6f 6e 20 69 73 20 75 6e 63  xpression is unc
2d8d9 68 61 6e 67 65 64 20 61 6e 64 20 61 20 6e 65 77  hanged and a new
2d8da 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 65 72 6d   virtual.** term
2d8db 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 59 20   of the form "Y 
2d8dc 3c 6f 70 3e 20 58 22 20 69 73 20 61 64 64 65 64  <op> X" is added
2d8dd 20 74 6f 20 74 68 65 20 57 48 45 52 45 20 63 6c   to the WHERE cl
2d8de 61 75 73 65 20 61 6e 64 0a 2a 2a 20 61 6e 61 6c  ause and.** anal
2d8df 79 7a 65 64 20 73 65 70 61 72 61 74 65 6c 79 2e  yzed separately.
2d8e0 20 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 74    The original t
2d8e1 65 72 6d 20 69 73 20 6d 61 72 6b 65 64 20 77 69  erm is marked wi
2d8e2 74 68 20 54 45 52 4d 5f 43 4f 50 49 45 44 0a 2a  th TERM_COPIED.*
2d8e3 2a 20 61 6e 64 20 74 68 65 20 6e 65 77 20 74 65  * and the new te
2d8e4 72 6d 20 69 73 20 6d 61 72 6b 65 64 20 77 69 74  rm is marked wit
2d8e5 68 20 54 45 52 4d 5f 44 59 4e 41 4d 49 43 20 28  h TERM_DYNAMIC (
2d8e6 62 65 63 61 75 73 65 20 69 74 27 73 20 70 45 78  because it's pEx
2d8e7 70 72 0a 2a 2a 20 6e 65 65 64 73 20 74 6f 20 62  pr.** needs to b
2d8e8 65 20 66 72 65 65 64 20 77 69 74 68 20 74 68 65  e freed with the
2d8e9 20 57 68 65 72 65 43 6c 61 75 73 65 29 20 61 6e   WhereClause) an
2d8ea 64 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 20 28  d TERM_VIRTUAL (
2d8eb 62 65 63 61 75 73 65 20 69 74 0a 2a 2a 20 69 73  because it.** is
2d8ec 20 61 20 63 6f 6d 6d 75 74 65 64 20 63 6f 70 79   a commuted copy
2d8ed 20 6f 66 20 61 20 70 72 69 6f 72 20 74 65 72 6d   of a prior term
2d8ee 2e 29 20 20 54 68 65 20 6f 72 69 67 69 6e 61 6c  .)  The original
2d8ef 20 74 65 72 6d 20 68 61 73 20 6e 43 68 69 6c 64   term has nChild
2d8f0 3d 31 0a 2a 2a 20 61 6e 64 20 74 68 65 20 63 6f  =1.** and the co
2d8f1 70 79 20 68 61 73 20 69 64 78 50 61 72 65 6e 74  py has idxParent
2d8f2 20 73 65 74 20 74 6f 20 74 68 65 20 69 6e 64 65   set to the inde
2d8f3 78 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61  x of the origina
2d8f4 6c 20 74 65 72 6d 2e 0a 2a 2f 0a 73 74 61 74 69  l term..*/.stati
2d8f5 63 20 76 6f 69 64 20 65 78 70 72 41 6e 61 6c 79  c void exprAnaly
2d8f6 7a 65 28 0a 20 20 53 72 63 4c 69 73 74 20 2a 70  ze(.  SrcList *p
2d8f7 53 72 63 2c 20 20 20 20 20 20 20 20 20 20 20 20  Src,            
2d8f8 2f 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  /* the FROM clau
2d8f9 73 65 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61  se */.  WhereCla
2d8fa 75 73 65 20 2a 70 57 43 2c 20 20 20 20 20 20 20  use *pWC,       
2d8fb 20 20 2f 2a 20 74 68 65 20 57 48 45 52 45 20 63    /* the WHERE c
2d8fc 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69  lause */.  int i
2d8fd 64 78 54 65 72 6d 20 20 20 20 20 20 20 20 20 20  dxTerm          
2d8fe 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
2d8ff 20 74 68 65 20 74 65 72 6d 20 74 6f 20 62 65 20   the term to be 
2d900 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 29 7b 0a 20  analyzed */.){. 
2d901 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72   WhereTerm *pTer
2d902 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m;              
2d903 20 20 2f 2a 20 54 68 65 20 74 65 72 6d 20 74 6f    /* The term to
2d904 20 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a   be analyzed */.
2d905 20 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a    WhereMaskSet *
2d906 70 4d 61 73 6b 53 65 74 3b 20 20 20 20 20 20 20  pMaskSet;       
2d907 20 20 20 2f 2a 20 53 65 74 20 6f 66 20 74 61 62     /* Set of tab
2d908 6c 65 20 69 6e 64 65 78 20 6d 61 73 6b 73 20 2a  le index masks *
2d909 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b  /.  Expr *pExpr;
2d90a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d90b 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72       /* The expr
2d90c 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 61 6e 61  ession to be ana
2d90d 6c 79 7a 65 64 20 2a 2f 0a 20 20 42 69 74 6d 61  lyzed */.  Bitma
2d90e 73 6b 20 70 72 65 72 65 71 4c 65 66 74 3b 20 20  sk prereqLeft;  
2d90f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
2d910 72 65 72 65 71 75 65 73 69 74 65 73 20 6f 66 20  rerequesites of 
2d911 74 68 65 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  the pExpr->pLeft
2d912 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 70 72   */.  Bitmask pr
2d913 65 72 65 71 41 6c 6c 3b 20 20 20 20 20 20 20 20  ereqAll;        
2d914 20 20 20 20 20 20 20 2f 2a 20 50 72 65 72 65 71         /* Prereq
2d915 75 65 73 69 74 65 73 20 6f 66 20 70 45 78 70 72  uesites of pExpr
2d916 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 65 78   */.  Bitmask ex
2d917 74 72 61 52 69 67 68 74 20 3d 20 30 3b 0a 20 20  traRight = 0;.  
2d918 69 6e 74 20 6e 50 61 74 74 65 72 6e 3b 0a 20 20  int nPattern;.  
2d919 69 6e 74 20 69 73 43 6f 6d 70 6c 65 74 65 3b 0a  int isComplete;.
2d91a 20 20 69 6e 74 20 6e 6f 43 61 73 65 3b 0a 20 20    int noCase;.  
2d91b 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20  int op;         
2d91c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d91d 20 2f 2a 20 54 6f 70 2d 6c 65 76 65 6c 20 6f 70   /* Top-level op
2d91e 65 72 61 74 6f 72 2e 20 20 70 45 78 70 72 2d 3e  erator.  pExpr->
2d91f 6f 70 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70  op */.  Parse *p
2d920 50 61 72 73 65 20 3d 20 70 57 43 2d 3e 70 50 61  Parse = pWC->pPa
2d921 72 73 65 3b 20 20 20 20 20 2f 2a 20 50 61 72 73  rse;     /* Pars
2d922 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
2d923 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
2d924 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 20  Parse->db;      
2d925 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f    /* Database co
2d926 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69  nnection */..  i
2d927 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
2d928 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72  led ){.    retur
2d929 6e 3b 0a 20 20 7d 0a 20 20 70 54 65 72 6d 20 3d  n;.  }.  pTerm =
2d92a 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d   &pWC->a[idxTerm
2d92b 5d 3b 0a 20 20 70 4d 61 73 6b 53 65 74 20 3d 20  ];.  pMaskSet = 
2d92c 70 57 43 2d 3e 70 4d 61 73 6b 53 65 74 3b 0a 20  pWC->pMaskSet;. 
2d92d 20 70 45 78 70 72 20 3d 20 70 54 65 72 6d 2d 3e   pExpr = pTerm->
2d92e 70 45 78 70 72 3b 0a 20 20 70 72 65 72 65 71 4c  pExpr;.  prereqL
2d92f 65 66 74 20 3d 20 65 78 70 72 54 61 62 6c 65 55  eft = exprTableU
2d930 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70  sage(pMaskSet, p
2d931 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20  Expr->pLeft);.  
2d932 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a  op = pExpr->op;.
2d933 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 4e 20    if( op==TK_IN 
2d934 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
2d935 45 78 70 72 2d 3e 70 52 69 67 68 74 3d 3d 30 20  Expr->pRight==0 
2d936 29 3b 0a 20 20 20 20 69 66 28 20 45 78 70 72 48  );.    if( ExprH
2d937 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
2d938 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20  , EP_xIsSelect) 
2d939 29 7b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e  ){.      pTerm->
2d93a 70 72 65 72 65 71 52 69 67 68 74 20 3d 20 65 78  prereqRight = ex
2d93b 70 72 53 65 6c 65 63 74 54 61 62 6c 65 55 73 61  prSelectTableUsa
2d93c 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 45 78  ge(pMaskSet, pEx
2d93d 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 29 3b 0a  pr->x.pSelect);.
2d93e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2d93f 20 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69   pTerm->prereqRi
2d940 67 68 74 20 3d 20 65 78 70 72 4c 69 73 74 54 61  ght = exprListTa
2d941 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65  bleUsage(pMaskSe
2d942 74 2c 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73  t, pExpr->x.pLis
2d943 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  t);.    }.  }els
2d944 65 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e  e if( op==TK_ISN
2d945 55 4c 4c 20 29 7b 0a 20 20 20 20 70 54 65 72 6d  ULL ){.    pTerm
2d946 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20 3d 20  ->prereqRight = 
2d947 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  0;.  }else{.    
2d948 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67  pTerm->prereqRig
2d949 68 74 20 3d 20 65 78 70 72 54 61 62 6c 65 55 73  ht = exprTableUs
2d94a 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 45  age(pMaskSet, pE
2d94b 78 70 72 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20  xpr->pRight);.  
2d94c 7d 0a 20 20 70 72 65 72 65 71 41 6c 6c 20 3d 20  }.  prereqAll = 
2d94d 65 78 70 72 54 61 62 6c 65 55 73 61 67 65 28 70  exprTableUsage(p
2d94e 4d 61 73 6b 53 65 74 2c 20 70 45 78 70 72 29 3b  MaskSet, pExpr);
2d94f 0a 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72  .  if( ExprHasPr
2d950 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
2d951 5f 46 72 6f 6d 4a 6f 69 6e 29 20 29 7b 0a 20 20  _FromJoin) ){.  
2d952 20 20 42 69 74 6d 61 73 6b 20 78 20 3d 20 67 65    Bitmask x = ge
2d953 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20  tMask(pMaskSet, 
2d954 70 45 78 70 72 2d 3e 69 52 69 67 68 74 4a 6f 69  pExpr->iRightJoi
2d955 6e 54 61 62 6c 65 29 3b 0a 20 20 20 20 70 72 65  nTable);.    pre
2d956 72 65 71 41 6c 6c 20 7c 3d 20 78 3b 0a 20 20 20  reqAll |= x;.   
2d957 20 65 78 74 72 61 52 69 67 68 74 20 3d 20 78 2d   extraRight = x-
2d958 31 3b 20 20 2f 2a 20 4f 4e 20 63 6c 61 75 73 65  1;  /* ON clause
2d959 20 74 65 72 6d 73 20 6d 61 79 20 6e 6f 74 20 62   terms may not b
2d95a 65 20 75 73 65 64 20 77 69 74 68 20 61 6e 20 69  e used with an i
2d95b 6e 64 65 78 0a 20 20 20 20 20 20 20 20 20 20 20  ndex.           
2d95c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f              ** o
2d95d 6e 20 6c 65 66 74 20 74 61 62 6c 65 20 6f 66 20  n left table of 
2d95e 61 20 4c 45 46 54 20 4a 4f 49 4e 2e 20 20 54 69  a LEFT JOIN.  Ti
2d95f 63 6b 65 74 20 23 33 30 31 35 20 2a 2f 0a 20 20  cket #3015 */.  
2d960 7d 0a 20 20 70 54 65 72 6d 2d 3e 70 72 65 72 65  }.  pTerm->prere
2d961 71 41 6c 6c 20 3d 20 70 72 65 72 65 71 41 6c 6c  qAll = prereqAll
2d962 3b 0a 20 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43  ;.  pTerm->leftC
2d963 75 72 73 6f 72 20 3d 20 2d 31 3b 0a 20 20 70 54  ursor = -1;.  pT
2d964 65 72 6d 2d 3e 69 50 61 72 65 6e 74 20 3d 20 2d  erm->iParent = -
2d965 31 3b 0a 20 20 70 54 65 72 6d 2d 3e 65 4f 70 65  1;.  pTerm->eOpe
2d966 72 61 74 6f 72 20 3d 20 30 3b 0a 20 20 69 66 28  rator = 0;.  if(
2d967 20 61 6c 6c 6f 77 65 64 4f 70 28 6f 70 29 20 26   allowedOp(op) &
2d968 26 20 28 70 54 65 72 6d 2d 3e 70 72 65 72 65 71  & (pTerm->prereq
2d969 52 69 67 68 74 20 26 20 70 72 65 72 65 71 4c 65  Right & prereqLe
2d96a 66 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 45 78  ft)==0 ){.    Ex
2d96b 70 72 20 2a 70 4c 65 66 74 20 3d 20 70 45 78 70  pr *pLeft = pExp
2d96c 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 45 78  r->pLeft;.    Ex
2d96d 70 72 20 2a 70 52 69 67 68 74 20 3d 20 70 45 78  pr *pRight = pEx
2d96e 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20  pr->pRight;.    
2d96f 69 66 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54  if( pLeft->op==T
2d970 4b 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20  K_COLUMN ){.    
2d971 20 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72    pTerm->leftCur
2d972 73 6f 72 20 3d 20 70 4c 65 66 74 2d 3e 69 54 61  sor = pLeft->iTa
2d973 62 6c 65 3b 0a 20 20 20 20 20 20 70 54 65 72 6d  ble;.      pTerm
2d974 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 20 3d  ->u.leftColumn =
2d975 20 70 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3b   pLeft->iColumn;
2d976 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 65 4f  .      pTerm->eO
2d977 70 65 72 61 74 6f 72 20 3d 20 6f 70 65 72 61 74  perator = operat
2d978 6f 72 4d 61 73 6b 28 6f 70 29 3b 0a 20 20 20 20  orMask(op);.    
2d979 7d 0a 20 20 20 20 69 66 28 20 70 52 69 67 68 74  }.    if( pRight
2d97a 20 26 26 20 70 52 69 67 68 74 2d 3e 6f 70 3d 3d   && pRight->op==
2d97b 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20  TK_COLUMN ){.   
2d97c 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 4e     WhereTerm *pN
2d97d 65 77 3b 0a 20 20 20 20 20 20 45 78 70 72 20 2a  ew;.      Expr *
2d97e 70 44 75 70 3b 0a 20 20 20 20 20 20 69 66 28 20  pDup;.      if( 
2d97f 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f  pTerm->leftCurso
2d980 72 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  r>=0 ){.        
2d981 69 6e 74 20 69 64 78 4e 65 77 3b 0a 20 20 20 20  int idxNew;.    
2d982 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74      pDup = sqlit
2d983 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 45  e3ExprDup(db, pE
2d984 78 70 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  xpr, 0);.       
2d985 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
2d986 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20  ailed ){.       
2d987 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65     sqlite3ExprDe
2d988 6c 65 74 65 28 64 62 2c 20 70 44 75 70 29 3b 0a  lete(db, pDup);.
2d989 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
2d98a 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2d98b 20 20 20 20 69 64 78 4e 65 77 20 3d 20 77 68 65      idxNew = whe
2d98c 72 65 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70  reClauseInsert(p
2d98d 57 43 2c 20 70 44 75 70 2c 20 54 45 52 4d 5f 56  WC, pDup, TERM_V
2d98e 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44 59 4e 41  IRTUAL|TERM_DYNA
2d98f 4d 49 43 29 3b 0a 20 20 20 20 20 20 20 20 69 66  MIC);.        if
2d990 28 20 69 64 78 4e 65 77 3d 3d 30 20 29 20 72 65  ( idxNew==0 ) re
2d991 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 70 4e  turn;.        pN
2d992 65 77 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78  ew = &pWC->a[idx
2d993 4e 65 77 5d 3b 0a 20 20 20 20 20 20 20 20 70 4e  New];.        pN
2d994 65 77 2d 3e 69 50 61 72 65 6e 74 20 3d 20 69 64  ew->iParent = id
2d995 78 54 65 72 6d 3b 0a 20 20 20 20 20 20 20 20 70  xTerm;.        p
2d996 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69  Term = &pWC->a[i
2d997 64 78 54 65 72 6d 5d 3b 0a 20 20 20 20 20 20 20  dxTerm];.       
2d998 20 70 54 65 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d   pTerm->nChild =
2d999 20 31 3b 0a 20 20 20 20 20 20 20 20 70 54 65 72   1;.        pTer
2d99a 6d 2d 3e 77 74 46 6c 61 67 73 20 7c 3d 20 54 45  m->wtFlags |= TE
2d99b 52 4d 5f 43 4f 50 49 45 44 3b 0a 20 20 20 20 20  RM_COPIED;.     
2d99c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2d99d 70 44 75 70 20 3d 20 70 45 78 70 72 3b 0a 20 20  pDup = pExpr;.  
2d99e 20 20 20 20 20 20 70 4e 65 77 20 3d 20 70 54 65        pNew = pTe
2d99f 72 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rm;.      }.    
2d9a0 20 20 65 78 70 72 43 6f 6d 6d 75 74 65 28 70 50    exprCommute(pP
2d9a1 61 72 73 65 2c 20 70 44 75 70 29 3b 0a 20 20 20  arse, pDup);.   
2d9a2 20 20 20 70 4c 65 66 74 20 3d 20 70 44 75 70 2d     pLeft = pDup-
2d9a3 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 70 4e  >pLeft;.      pN
2d9a4 65 77 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20 3d  ew->leftCursor =
2d9a5 20 70 4c 65 66 74 2d 3e 69 54 61 62 6c 65 3b 0a   pLeft->iTable;.
2d9a6 20 20 20 20 20 20 70 4e 65 77 2d 3e 75 2e 6c 65        pNew->u.le
2d9a7 66 74 43 6f 6c 75 6d 6e 20 3d 20 70 4c 65 66 74  ftColumn = pLeft
2d9a8 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20  ->iColumn;.     
2d9a9 20 70 4e 65 77 2d 3e 70 72 65 72 65 71 52 69 67   pNew->prereqRig
2d9aa 68 74 20 3d 20 70 72 65 72 65 71 4c 65 66 74 3b  ht = prereqLeft;
2d9ab 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 72 65  .      pNew->pre
2d9ac 72 65 71 41 6c 6c 20 3d 20 70 72 65 72 65 71 41  reqAll = prereqA
2d9ad 6c 6c 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e  ll;.      pNew->
2d9ae 65 4f 70 65 72 61 74 6f 72 20 3d 20 6f 70 65 72  eOperator = oper
2d9af 61 74 6f 72 4d 61 73 6b 28 70 44 75 70 2d 3e 6f  atorMask(pDup->o
2d9b0 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 23  p);.    }.  }..#
2d9b1 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2d9b2 49 54 5f 42 45 54 57 45 45 4e 5f 4f 50 54 49 4d  IT_BETWEEN_OPTIM
2d9b3 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 49 66 20  IZATION.  /* If 
2d9b4 61 20 74 65 72 6d 20 69 73 20 74 68 65 20 42 45  a term is the BE
2d9b5 54 57 45 45 4e 20 6f 70 65 72 61 74 6f 72 2c 20  TWEEN operator, 
2d9b6 63 72 65 61 74 65 20 74 77 6f 20 6e 65 77 20 76  create two new v
2d9b7 69 72 74 75 61 6c 20 74 65 72 6d 73 0a 20 20 2a  irtual terms.  *
2d9b8 2a 20 74 68 61 74 20 64 65 66 69 6e 65 20 74 68  * that define th
2d9b9 65 20 72 61 6e 67 65 20 74 68 61 74 20 74 68 65  e range that the
2d9ba 20 42 45 54 57 45 45 4e 20 69 6d 70 6c 65 6d 65   BETWEEN impleme
2d9bb 6e 74 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c  nts.  For exampl
2d9bc 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  e:.  **.  **    
2d9bd 20 20 61 20 42 45 54 57 45 45 4e 20 62 20 41 4e    a BETWEEN b AN
2d9be 44 20 63 0a 20 20 2a 2a 0a 20 20 2a 2a 20 69 73  D c.  **.  ** is
2d9bf 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 3a   converted into:
2d9c0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20  .  **.  **      
2d9c1 28 61 20 42 45 54 57 45 45 4e 20 62 20 41 4e 44  (a BETWEEN b AND
2d9c2 20 63 29 20 41 4e 44 20 28 61 3e 3d 62 29 20 41   c) AND (a>=b) A
2d9c3 4e 44 20 28 61 3c 3d 63 29 0a 20 20 2a 2a 0a 20  ND (a<=c).  **. 
2d9c4 20 2a 2a 20 54 68 65 20 74 77 6f 20 6e 65 77 20   ** The two new 
2d9c5 74 65 72 6d 73 20 61 72 65 20 61 64 64 65 64 20  terms are added 
2d9c6 6f 6e 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20  onto the end of 
2d9c7 74 68 65 20 57 68 65 72 65 43 6c 61 75 73 65 20  the WhereClause 
2d9c8 6f 62 6a 65 63 74 2e 0a 20 20 2a 2a 20 54 68 65  object..  ** The
2d9c9 20 6e 65 77 20 74 65 72 6d 73 20 61 72 65 20 22   new terms are "
2d9ca 64 79 6e 61 6d 69 63 22 20 61 6e 64 20 61 72 65  dynamic" and are
2d9cb 20 63 68 69 6c 64 72 65 6e 20 6f 66 20 74 68 65   children of the
2d9cc 20 6f 72 69 67 69 6e 61 6c 20 42 45 54 57 45 45   original BETWEE
2d9cd 4e 0a 20 20 2a 2a 20 74 65 72 6d 2e 20 20 54 68  N.  ** term.  Th
2d9ce 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 69 66  at means that if
2d9cf 20 74 68 65 20 42 45 54 57 45 45 4e 20 74 65 72   the BETWEEN ter
2d9d0 6d 20 69 73 20 63 6f 64 65 64 2c 20 74 68 65 20  m is coded, the 
2d9d1 63 68 69 6c 64 72 65 6e 20 61 72 65 0a 20 20 2a  children are.  *
2d9d2 2a 20 73 6b 69 70 70 65 64 2e 20 20 4f 72 2c 20  * skipped.  Or, 
2d9d3 69 66 20 74 68 65 20 63 68 69 6c 64 72 65 6e 20  if the children 
2d9d4 61 72 65 20 73 61 74 69 73 66 69 65 64 20 62 79  are satisfied by
2d9d5 20 61 6e 20 69 6e 64 65 78 2c 20 74 68 65 20 6f   an index, the o
2d9d6 72 69 67 69 6e 61 6c 0a 20 20 2a 2a 20 42 45 54  riginal.  ** BET
2d9d7 57 45 45 4e 20 74 65 72 6d 20 69 73 20 73 6b 69  WEEN term is ski
2d9d8 70 70 65 64 2e 0a 20 20 2a 2f 0a 20 20 65 6c 73  pped..  */.  els
2d9d9 65 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d  e if( pExpr->op=
2d9da 3d 54 4b 5f 42 45 54 57 45 45 4e 20 26 26 20 70  =TK_BETWEEN && p
2d9db 57 43 2d 3e 6f 70 3d 3d 54 4b 5f 41 4e 44 20 29  WC->op==TK_AND )
2d9dc 7b 0a 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a  {.    ExprList *
2d9dd 70 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 78  pList = pExpr->x
2d9de 2e 70 4c 69 73 74 3b 0a 20 20 20 20 69 6e 74 20  .pList;.    int 
2d9df 69 3b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f  i;.    static co
2d9e0 6e 73 74 20 75 38 20 6f 70 73 5b 5d 20 3d 20 7b  nst u8 ops[] = {
2d9e1 54 4b 5f 47 45 2c 20 54 4b 5f 4c 45 7d 3b 0a 20  TK_GE, TK_LE};. 
2d9e2 20 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74     assert( pList
2d9e3 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  !=0 );.    asser
2d9e4 74 28 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3d  t( pList->nExpr=
2d9e5 3d 32 20 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d  =2 );.    for(i=
2d9e6 30 3b 20 69 3c 32 3b 20 69 2b 2b 29 7b 0a 20 20  0; i<2; i++){.  
2d9e7 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77 45 78      Expr *pNewEx
2d9e8 70 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 64  pr;.      int id
2d9e9 78 4e 65 77 3b 0a 20 20 20 20 20 20 70 4e 65 77  xNew;.      pNew
2d9ea 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45  Expr = sqlite3PE
2d9eb 78 70 72 28 70 50 61 72 73 65 2c 20 6f 70 73 5b  xpr(pParse, ops[
2d9ec 69 5d 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  i], .           
2d9ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d9ee 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70    sqlite3ExprDup
2d9ef 28 64 62 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  (db, pExpr->pLef
2d9f0 74 2c 20 30 29 2c 0a 20 20 20 20 20 20 20 20 20  t, 0),.         
2d9f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d9f2 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
2d9f3 75 70 28 64 62 2c 20 70 4c 69 73 74 2d 3e 61 5b  up(db, pList->a[
2d9f4 69 5d 2e 70 45 78 70 72 2c 20 30 29 2c 20 30 29  i].pExpr, 0), 0)
2d9f5 3b 0a 20 20 20 20 20 20 69 64 78 4e 65 77 20 3d  ;.      idxNew =
2d9f6 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e 73 65   whereClauseInse
2d9f7 72 74 28 70 57 43 2c 20 70 4e 65 77 45 78 70 72  rt(pWC, pNewExpr
2d9f8 2c 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54  , TERM_VIRTUAL|T
2d9f9 45 52 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20  ERM_DYNAMIC);.  
2d9fa 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 64      testcase( id
2d9fb 78 4e 65 77 3d 3d 30 20 29 3b 0a 20 20 20 20 20  xNew==0 );.     
2d9fc 20 65 78 70 72 41 6e 61 6c 79 7a 65 28 70 53 72   exprAnalyze(pSr
2d9fd 63 2c 20 70 57 43 2c 20 69 64 78 4e 65 77 29 3b  c, pWC, idxNew);
2d9fe 0a 20 20 20 20 20 20 70 54 65 72 6d 20 3d 20 26  .      pTerm = &
2d9ff 70 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b  pWC->a[idxTerm];
2da00 0a 20 20 20 20 20 20 70 57 43 2d 3e 61 5b 69 64  .      pWC->a[id
2da01 78 4e 65 77 5d 2e 69 50 61 72 65 6e 74 20 3d 20  xNew].iParent = 
2da02 69 64 78 54 65 72 6d 3b 0a 20 20 20 20 7d 0a 20  idxTerm;.    }. 
2da03 20 20 20 70 54 65 72 6d 2d 3e 6e 43 68 69 6c 64     pTerm->nChild
2da04 20 3d 20 32 3b 0a 20 20 7d 0a 23 65 6e 64 69 66   = 2;.  }.#endif
2da05 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
2da06 42 45 54 57 45 45 4e 5f 4f 50 54 49 4d 49 5a 41  BETWEEN_OPTIMIZA
2da07 54 49 4f 4e 20 2a 2f 0a 0a 23 69 66 20 21 64 65  TION */..#if !de
2da08 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
2da09 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f  T_OR_OPTIMIZATIO
2da0a 4e 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  N) && !defined(S
2da0b 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
2da0c 45 52 59 29 0a 20 20 2f 2a 20 41 6e 61 6c 79 7a  ERY).  /* Analyz
2da0d 65 20 61 20 74 65 72 6d 20 74 68 61 74 20 69 73  e a term that is
2da0e 20 63 6f 6d 70 6f 73 65 64 20 6f 66 20 74 77 6f   composed of two
2da0f 20 6f 72 20 6d 6f 72 65 20 73 75 62 74 65 72 6d   or more subterm
2da10 73 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 0a 20  s connected by. 
2da11 20 2a 2a 20 61 6e 20 4f 52 20 6f 70 65 72 61 74   ** an OR operat
2da12 6f 72 2e 0a 20 20 2a 2f 0a 20 20 65 6c 73 65 20  or..  */.  else 
2da13 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54  if( pExpr->op==T
2da14 4b 5f 4f 52 20 29 7b 0a 20 20 20 20 61 73 73 65  K_OR ){.    asse
2da15 72 74 28 20 70 57 43 2d 3e 6f 70 3d 3d 54 4b 5f  rt( pWC->op==TK_
2da16 41 4e 44 20 29 3b 0a 20 20 20 20 65 78 70 72 41  AND );.    exprA
2da17 6e 61 6c 79 7a 65 4f 72 54 65 72 6d 28 70 53 72  nalyzeOrTerm(pSr
2da18 63 2c 20 70 57 43 2c 20 69 64 78 54 65 72 6d 29  c, pWC, idxTerm)
2da19 3b 0a 20 20 20 20 70 54 65 72 6d 20 3d 20 26 70  ;.    pTerm = &p
2da1a 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a  WC->a[idxTerm];.
2da1b 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51    }.#endif /* SQ
2da1c 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54  LITE_OMIT_OR_OPT
2da1d 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 23 69  IMIZATION */..#i
2da1e 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2da1f 54 5f 4c 49 4b 45 5f 4f 50 54 49 4d 49 5a 41 54  T_LIKE_OPTIMIZAT
2da20 49 4f 4e 0a 20 20 2f 2a 20 41 64 64 20 63 6f 6e  ION.  /* Add con
2da21 73 74 72 61 69 6e 74 73 20 74 6f 20 72 65 64 75  straints to redu
2da22 63 65 20 74 68 65 20 73 65 61 72 63 68 20 73 70  ce the search sp
2da23 61 63 65 20 6f 6e 20 61 20 4c 49 4b 45 20 6f 72  ace on a LIKE or
2da24 20 47 4c 4f 42 0a 20 20 2a 2a 20 6f 70 65 72 61   GLOB.  ** opera
2da25 74 6f 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41  tor..  **.  ** A
2da26 20 6c 69 6b 65 20 70 61 74 74 65 72 6e 20 6f 66   like pattern of
2da27 20 74 68 65 20 66 6f 72 6d 20 22 78 20 4c 49 4b   the form "x LIK
2da28 45 20 27 61 62 63 25 27 22 20 69 73 20 63 68 61  E 'abc%'" is cha
2da29 6e 67 65 64 20 69 6e 74 6f 20 63 6f 6e 73 74 72  nged into constr
2da2a 61 69 6e 74 73 0a 20 20 2a 2a 0a 20 20 2a 2a 20  aints.  **.  ** 
2da2b 20 20 20 20 20 20 20 20 20 78 3e 3d 27 61 62 63           x>='abc
2da2c 27 20 41 4e 44 20 78 3c 27 61 62 64 27 20 41 4e  ' AND x<'abd' AN
2da2d 44 20 78 20 4c 49 4b 45 20 27 61 62 63 25 27 0a  D x LIKE 'abc%'.
2da2e 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 6c 61    **.  ** The la
2da2f 73 74 20 63 68 61 72 61 63 74 65 72 20 6f 66 20  st character of 
2da30 74 68 65 20 70 72 65 66 69 78 20 22 61 62 63 22  the prefix "abc"
2da31 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20   is incremented 
2da32 74 6f 20 66 6f 72 6d 20 74 68 65 0a 20 20 2a 2a  to form the.  **
2da33 20 74 65 72 6d 69 6e 61 74 69 6f 6e 20 63 6f 6e   termination con
2da34 64 69 74 69 6f 6e 20 22 61 62 64 22 2e 0a 20 20  dition "abd"..  
2da35 2a 2f 0a 20 20 69 66 28 20 69 73 4c 69 6b 65 4f  */.  if( isLikeO
2da36 72 47 6c 6f 62 28 70 50 61 72 73 65 2c 20 70 45  rGlob(pParse, pE
2da37 78 70 72 2c 20 26 6e 50 61 74 74 65 72 6e 2c 20  xpr, &nPattern, 
2da38 26 69 73 43 6f 6d 70 6c 65 74 65 2c 20 26 6e 6f  &isComplete, &no
2da39 43 61 73 65 29 0a 20 20 20 20 20 20 20 20 20 26  Case).         &
2da3a 26 20 70 57 43 2d 3e 6f 70 3d 3d 54 4b 5f 41 4e  & pWC->op==TK_AN
2da3b 44 20 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70  D ){.    Expr *p
2da3c 4c 65 66 74 2c 20 2a 70 52 69 67 68 74 3b 0a 20  Left, *pRight;. 
2da3d 20 20 20 45 78 70 72 20 2a 70 53 74 72 31 2c 20     Expr *pStr1, 
2da3e 2a 70 53 74 72 32 3b 0a 20 20 20 20 45 78 70 72  *pStr2;.    Expr
2da3f 20 2a 70 4e 65 77 45 78 70 72 31 2c 20 2a 70 4e   *pNewExpr1, *pN
2da40 65 77 45 78 70 72 32 3b 0a 20 20 20 20 69 6e 74  ewExpr2;.    int
2da41 20 69 64 78 4e 65 77 31 2c 20 69 64 78 4e 65 77   idxNew1, idxNew
2da42 32 3b 0a 0a 20 20 20 20 70 4c 65 66 74 20 3d 20  2;..    pLeft = 
2da43 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e  pExpr->x.pList->
2da44 61 5b 31 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20  a[1].pExpr;.    
2da45 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e  pRight = pExpr->
2da46 78 2e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45  x.pList->a[0].pE
2da47 78 70 72 3b 0a 20 20 20 20 70 53 74 72 31 20 3d  xpr;.    pStr1 =
2da48 20 73 71 6c 69 74 65 33 45 78 70 72 28 64 62 2c   sqlite3Expr(db,
2da49 20 54 4b 5f 53 54 52 49 4e 47 2c 20 70 52 69 67   TK_STRING, pRig
2da4a 68 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20  ht->u.zToken);. 
2da4b 20 20 20 69 66 28 20 70 53 74 72 31 20 29 20 70     if( pStr1 ) p
2da4c 53 74 72 31 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 6e  Str1->u.zToken[n
2da4d 50 61 74 74 65 72 6e 5d 20 3d 20 30 3b 0a 20 20  Pattern] = 0;.  
2da4e 20 20 70 53 74 72 32 20 3d 20 73 71 6c 69 74 65    pStr2 = sqlite
2da4f 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 53 74  3ExprDup(db, pSt
2da50 72 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20  r1, 0);.    if( 
2da51 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  !db->mallocFaile
2da52 64 20 29 7b 0a 20 20 20 20 20 20 75 38 20 63 2c  d ){.      u8 c,
2da53 20 2a 70 43 3b 20 20 20 20 20 20 20 2f 2a 20 4c   *pC;       /* L
2da54 61 73 74 20 63 68 61 72 61 63 74 65 72 20 62 65  ast character be
2da55 66 6f 72 65 20 74 68 65 20 66 69 72 73 74 20 77  fore the first w
2da56 69 6c 64 63 61 72 64 20 2a 2f 0a 20 20 20 20 20  ildcard */.     
2da57 20 70 43 20 3d 20 28 75 38 2a 29 26 70 53 74 72   pC = (u8*)&pStr
2da58 32 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 6e 50 61 74  2->u.zToken[nPat
2da59 74 65 72 6e 2d 31 5d 3b 0a 20 20 20 20 20 20 63  tern-1];.      c
2da5a 20 3d 20 2a 70 43 3b 0a 20 20 20 20 20 20 69 66   = *pC;.      if
2da5b 28 20 6e 6f 43 61 73 65 20 29 7b 0a 20 20 20 20  ( noCase ){.    
2da5c 20 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74      /* The point
2da5d 20 69 73 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74   is to increment
2da5e 20 74 68 65 20 6c 61 73 74 20 63 68 61 72 61 63   the last charac
2da5f 74 65 72 20 62 65 66 6f 72 65 20 74 68 65 20 66  ter before the f
2da60 69 72 73 74 0a 20 20 20 20 20 20 20 20 2a 2a 20  irst.        ** 
2da61 77 69 6c 64 63 61 72 64 2e 20 20 42 75 74 20 69  wildcard.  But i
2da62 66 20 77 65 20 69 6e 63 72 65 6d 65 6e 74 20 27  f we increment '
2da63 40 27 2c 20 74 68 61 74 20 77 69 6c 6c 20 70 75  @', that will pu
2da64 73 68 20 69 74 20 69 6e 74 6f 20 74 68 65 0a 20  sh it into the. 
2da65 20 20 20 20 20 20 20 2a 2a 20 61 6c 70 68 61 62         ** alphab
2da66 65 74 69 63 20 72 61 6e 67 65 20 77 68 65 72 65  etic range where
2da67 20 63 61 73 65 20 63 6f 6e 76 65 72 73 69 6f 6e   case conversion
2da68 73 20 77 69 6c 6c 20 6d 65 73 73 20 75 70 20 74  s will mess up t
2da69 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 69  he .        ** i
2da6a 6e 65 71 75 61 6c 69 74 79 2e 20 20 54 6f 20 61  nequality.  To a
2da6b 76 6f 69 64 20 74 68 69 73 2c 20 6d 61 6b 65 20  void this, make 
2da6c 73 75 72 65 20 74 6f 20 61 6c 73 6f 20 72 75 6e  sure to also run
2da6d 20 74 68 65 20 66 75 6c 6c 0a 20 20 20 20 20 20   the full.      
2da6e 20 20 2a 2a 20 4c 49 4b 45 20 6f 6e 20 61 6c 6c    ** LIKE on all
2da6f 20 63 61 6e 64 69 64 61 74 65 20 65 78 70 72 65   candidate expre
2da70 73 73 69 6f 6e 73 20 62 79 20 63 6c 65 61 72 69  ssions by cleari
2da71 6e 67 20 74 68 65 20 69 73 43 6f 6d 70 6c 65 74  ng the isComplet
2da72 65 20 66 6c 61 67 0a 20 20 20 20 20 20 20 20 2a  e flag.        *
2da73 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 3d  /.        if( c=
2da74 3d 27 41 27 2d 31 20 29 20 69 73 43 6f 6d 70 6c  ='A'-1 ) isCompl
2da75 65 74 65 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20  ete = 0;..      
2da76 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 70 70    c = sqlite3Upp
2da77 65 72 54 6f 4c 6f 77 65 72 5b 63 5d 3b 0a 20 20  erToLower[c];.  
2da78 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 70 43 20      }.      *pC 
2da79 3d 20 63 20 2b 20 31 3b 0a 20 20 20 20 7d 0a 20  = c + 1;.    }. 
2da7a 20 20 20 70 4e 65 77 45 78 70 72 31 20 3d 20 73     pNewExpr1 = s
2da7b 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
2da7c 73 65 2c 20 54 4b 5f 47 45 2c 20 73 71 6c 69 74  se, TK_GE, sqlit
2da7d 65 33 45 78 70 72 44 75 70 28 64 62 2c 70 4c 65  e3ExprDup(db,pLe
2da7e 66 74 2c 30 29 2c 70 53 74 72 31 2c 30 29 3b 0a  ft,0),pStr1,0);.
2da7f 20 20 20 20 69 64 78 4e 65 77 31 20 3d 20 77 68      idxNew1 = wh
2da80 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72 74 28  ereClauseInsert(
2da81 70 57 43 2c 20 70 4e 65 77 45 78 70 72 31 2c 20  pWC, pNewExpr1, 
2da82 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52  TERM_VIRTUAL|TER
2da83 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20  M_DYNAMIC);.    
2da84 74 65 73 74 63 61 73 65 28 20 69 64 78 4e 65 77  testcase( idxNew
2da85 31 3d 3d 30 20 29 3b 0a 20 20 20 20 65 78 70 72  1==0 );.    expr
2da86 41 6e 61 6c 79 7a 65 28 70 53 72 63 2c 20 70 57  Analyze(pSrc, pW
2da87 43 2c 20 69 64 78 4e 65 77 31 29 3b 0a 20 20 20  C, idxNew1);.   
2da88 20 70 4e 65 77 45 78 70 72 32 20 3d 20 73 71 6c   pNewExpr2 = sql
2da89 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
2da8a 2c 20 54 4b 5f 4c 54 2c 20 73 71 6c 69 74 65 33  , TK_LT, sqlite3
2da8b 45 78 70 72 44 75 70 28 64 62 2c 70 4c 65 66 74  ExprDup(db,pLeft
2da8c 2c 30 29 2c 70 53 74 72 32 2c 30 29 3b 0a 20 20  ,0),pStr2,0);.  
2da8d 20 20 69 64 78 4e 65 77 32 20 3d 20 77 68 65 72    idxNew2 = wher
2da8e 65 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57  eClauseInsert(pW
2da8f 43 2c 20 70 4e 65 77 45 78 70 72 32 2c 20 54 45  C, pNewExpr2, TE
2da90 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f  RM_VIRTUAL|TERM_
2da91 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 74 65  DYNAMIC);.    te
2da92 73 74 63 61 73 65 28 20 69 64 78 4e 65 77 32 3d  stcase( idxNew2=
2da93 3d 30 20 29 3b 0a 20 20 20 20 65 78 70 72 41 6e  =0 );.    exprAn
2da94 61 6c 79 7a 65 28 70 53 72 63 2c 20 70 57 43 2c  alyze(pSrc, pWC,
2da95 20 69 64 78 4e 65 77 32 29 3b 0a 20 20 20 20 70   idxNew2);.    p
2da96 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69  Term = &pWC->a[i
2da97 64 78 54 65 72 6d 5d 3b 0a 20 20 20 20 69 66 28  dxTerm];.    if(
2da98 20 69 73 43 6f 6d 70 6c 65 74 65 20 29 7b 0a 20   isComplete ){. 
2da99 20 20 20 20 20 70 57 43 2d 3e 61 5b 69 64 78 4e       pWC->a[idxN
2da9a 65 77 31 5d 2e 69 50 61 72 65 6e 74 20 3d 20 69  ew1].iParent = i
2da9b 64 78 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 57  dxTerm;.      pW
2da9c 43 2d 3e 61 5b 69 64 78 4e 65 77 32 5d 2e 69 50  C->a[idxNew2].iP
2da9d 61 72 65 6e 74 20 3d 20 69 64 78 54 65 72 6d 3b  arent = idxTerm;
2da9e 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 6e 43  .      pTerm->nC
2da9f 68 69 6c 64 20 3d 20 32 3b 0a 20 20 20 20 7d 0a  hild = 2;.    }.
2daa0 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51    }.#endif /* SQ
2daa1 4c 49 54 45 5f 4f 4d 49 54 5f 4c 49 4b 45 5f 4f  LITE_OMIT_LIKE_O
2daa2 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a  PTIMIZATION */..
2daa3 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2daa4 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
2daa5 0a 20 20 2f 2a 20 41 64 64 20 61 20 57 4f 5f 4d  .  /* Add a WO_M
2daa6 41 54 43 48 20 61 75 78 69 6c 69 61 72 79 20 74  ATCH auxiliary t
2daa7 65 72 6d 20 74 6f 20 74 68 65 20 63 6f 6e 73 74  erm to the const
2daa8 72 61 69 6e 74 20 73 65 74 20 69 66 20 74 68 65  raint set if the
2daa9 0a 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 65 78  .  ** current ex
2daaa 70 72 65 73 73 69 6f 6e 20 69 73 20 6f 66 20 74  pression is of t
2daab 68 65 20 66 6f 72 6d 3a 20 20 63 6f 6c 75 6d 6e  he form:  column
2daac 20 4d 41 54 43 48 20 65 78 70 72 2e 0a 20 20 2a   MATCH expr..  *
2daad 2a 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69  * This informati
2daae 6f 6e 20 69 73 20 75 73 65 64 20 62 79 20 74 68  on is used by th
2daaf 65 20 78 42 65 73 74 49 6e 64 65 78 20 6d 65 74  e xBestIndex met
2dab0 68 6f 64 73 20 6f 66 0a 20 20 2a 2a 20 76 69 72  hods of.  ** vir
2dab1 74 75 61 6c 20 74 61 62 6c 65 73 2e 20 20 54 68  tual tables.  Th
2dab2 65 20 6e 61 74 69 76 65 20 71 75 65 72 79 20 6f  e native query o
2dab3 70 74 69 6d 69 7a 65 72 20 64 6f 65 73 20 6e 6f  ptimizer does no
2dab4 74 20 61 74 74 65 6d 70 74 0a 20 20 2a 2a 20 74  t attempt.  ** t
2dab5 6f 20 64 6f 20 61 6e 79 74 68 69 6e 67 20 77 69  o do anything wi
2dab6 74 68 20 4d 41 54 43 48 20 66 75 6e 63 74 69 6f  th MATCH functio
2dab7 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69  ns..  */.  if( i
2dab8 73 4d 61 74 63 68 4f 66 43 6f 6c 75 6d 6e 28 70  sMatchOfColumn(p
2dab9 45 78 70 72 29 20 29 7b 0a 20 20 20 20 69 6e 74  Expr) ){.    int
2daba 20 69 64 78 4e 65 77 3b 0a 20 20 20 20 45 78 70   idxNew;.    Exp
2dabb 72 20 2a 70 52 69 67 68 74 2c 20 2a 70 4c 65 66  r *pRight, *pLef
2dabc 74 3b 0a 20 20 20 20 57 68 65 72 65 54 65 72 6d  t;.    WhereTerm
2dabd 20 2a 70 4e 65 77 54 65 72 6d 3b 0a 20 20 20 20   *pNewTerm;.    
2dabe 42 69 74 6d 61 73 6b 20 70 72 65 72 65 71 43 6f  Bitmask prereqCo
2dabf 6c 75 6d 6e 2c 20 70 72 65 72 65 71 45 78 70 72  lumn, prereqExpr
2dac0 3b 0a 0a 20 20 20 20 70 52 69 67 68 74 20 3d 20  ;..    pRight = 
2dac1 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e  pExpr->x.pList->
2dac2 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20  a[0].pExpr;.    
2dac3 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 78  pLeft = pExpr->x
2dac4 2e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78  .pList->a[1].pEx
2dac5 70 72 3b 0a 20 20 20 20 70 72 65 72 65 71 45 78  pr;.    prereqEx
2dac6 70 72 20 3d 20 65 78 70 72 54 61 62 6c 65 55 73  pr = exprTableUs
2dac7 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 52  age(pMaskSet, pR
2dac8 69 67 68 74 29 3b 0a 20 20 20 20 70 72 65 72 65  ight);.    prere
2dac9 71 43 6f 6c 75 6d 6e 20 3d 20 65 78 70 72 54 61  qColumn = exprTa
2daca 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65  bleUsage(pMaskSe
2dacb 74 2c 20 70 4c 65 66 74 29 3b 0a 20 20 20 20 69  t, pLeft);.    i
2dacc 66 28 20 28 70 72 65 72 65 71 45 78 70 72 20 26  f( (prereqExpr &
2dacd 20 70 72 65 72 65 71 43 6f 6c 75 6d 6e 29 3d 3d   prereqColumn)==
2dace 30 20 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20  0 ){.      Expr 
2dacf 2a 70 4e 65 77 45 78 70 72 3b 0a 20 20 20 20 20  *pNewExpr;.     
2dad0 20 70 4e 65 77 45 78 70 72 20 3d 20 73 71 6c 69   pNewExpr = sqli
2dad1 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
2dad2 20 54 4b 5f 4d 41 54 43 48 2c 20 0a 20 20 20 20   TK_MATCH, .    
2dad3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2dad4 20 20 20 20 20 20 20 20 20 20 30 2c 20 73 71 6c            0, sql
2dad5 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20  ite3ExprDup(db, 
2dad6 70 52 69 67 68 74 2c 20 30 29 2c 20 30 29 3b 0a  pRight, 0), 0);.
2dad7 20 20 20 20 20 20 69 64 78 4e 65 77 20 3d 20 77        idxNew = w
2dad8 68 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72 74  hereClauseInsert
2dad9 28 70 57 43 2c 20 70 4e 65 77 45 78 70 72 2c 20  (pWC, pNewExpr, 
2dada 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52  TERM_VIRTUAL|TER
2dadb 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20  M_DYNAMIC);.    
2dadc 20 20 74 65 73 74 63 61 73 65 28 20 69 64 78 4e    testcase( idxN
2dadd 65 77 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70  ew==0 );.      p
2dade 4e 65 77 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e  NewTerm = &pWC->
2dadf 61 5b 69 64 78 4e 65 77 5d 3b 0a 20 20 20 20 20  a[idxNew];.     
2dae0 20 70 4e 65 77 54 65 72 6d 2d 3e 70 72 65 72 65   pNewTerm->prere
2dae1 71 52 69 67 68 74 20 3d 20 70 72 65 72 65 71 45  qRight = prereqE
2dae2 78 70 72 3b 0a 20 20 20 20 20 20 70 4e 65 77 54  xpr;.      pNewT
2dae3 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20  erm->leftCursor 
2dae4 3d 20 70 4c 65 66 74 2d 3e 69 54 61 62 6c 65 3b  = pLeft->iTable;
2dae5 0a 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d  .      pNewTerm-
2dae6 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 20 3d 20  >u.leftColumn = 
2dae7 70 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a  pLeft->iColumn;.
2dae8 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e        pNewTerm->
2dae9 65 4f 70 65 72 61 74 6f 72 20 3d 20 57 4f 5f 4d  eOperator = WO_M
2daea 41 54 43 48 3b 0a 20 20 20 20 20 20 70 4e 65 77  ATCH;.      pNew
2daeb 54 65 72 6d 2d 3e 69 50 61 72 65 6e 74 20 3d 20  Term->iParent = 
2daec 69 64 78 54 65 72 6d 3b 0a 20 20 20 20 20 20 70  idxTerm;.      p
2daed 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69  Term = &pWC->a[i
2daee 64 78 54 65 72 6d 5d 3b 0a 20 20 20 20 20 20 70  dxTerm];.      p
2daef 54 65 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20 31  Term->nChild = 1
2daf0 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 77  ;.      pTerm->w
2daf1 74 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f 43  tFlags |= TERM_C
2daf2 4f 50 49 45 44 3b 0a 20 20 20 20 20 20 70 4e 65  OPIED;.      pNe
2daf3 77 54 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c 6c  wTerm->prereqAll
2daf4 20 3d 20 70 54 65 72 6d 2d 3e 70 72 65 72 65 71   = pTerm->prereq
2daf5 41 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23  All;.    }.  }.#
2daf6 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
2daf7 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
2daf8 45 20 2a 2f 0a 0a 20 20 2f 2a 20 50 72 65 76 65  E */..  /* Preve
2daf9 6e 74 20 4f 4e 20 63 6c 61 75 73 65 20 74 65 72  nt ON clause ter
2dafa 6d 73 20 6f 66 20 61 20 4c 45 46 54 20 4a 4f 49  ms of a LEFT JOI
2dafb 4e 20 66 72 6f 6d 20 62 65 69 6e 67 20 75 73 65  N from being use
2dafc 64 20 74 6f 20 64 72 69 76 65 0a 20 20 2a 2a 20  d to drive.  ** 
2dafd 61 6e 20 69 6e 64 65 78 20 66 6f 72 20 74 61 62  an index for tab
2dafe 6c 65 73 20 74 6f 20 74 68 65 20 6c 65 66 74 20  les to the left 
2daff 6f 66 20 74 68 65 20 6a 6f 69 6e 2e 0a 20 20 2a  of the join..  *
2db00 2f 0a 20 20 70 54 65 72 6d 2d 3e 70 72 65 72 65  /.  pTerm->prere
2db01 71 52 69 67 68 74 20 7c 3d 20 65 78 74 72 61 52  qRight |= extraR
2db02 69 67 68 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ight;.}../*.** R
2db03 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 61 6e  eturn TRUE if an
2db04 79 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73  y of the express
2db05 69 6f 6e 73 20 69 6e 20 70 4c 69 73 74 2d 3e 61  ions in pList->a
2db06 5b 69 46 69 72 73 74 2e 2e 2e 5d 20 63 6f 6e 74  [iFirst...] cont
2db07 61 69 6e 0a 2a 2a 20 61 20 72 65 66 65 72 65 6e  ain.** a referen
2db08 63 65 20 74 6f 20 61 6e 79 20 74 61 62 6c 65 20  ce to any table 
2db09 6f 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 69  other than the i
2db0a 42 61 73 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73  Base table..*/.s
2db0b 74 61 74 69 63 20 69 6e 74 20 72 65 66 65 72 65  tatic int refere
2db0c 6e 63 65 73 4f 74 68 65 72 54 61 62 6c 65 73 28  ncesOtherTables(
2db0d 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69  .  ExprList *pLi
2db0e 73 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  st,          /* 
2db0f 53 65 61 72 63 68 20 65 78 70 72 65 73 73 69 6f  Search expressio
2db10 6e 73 20 69 6e 20 74 68 73 20 6c 69 73 74 20 2a  ns in ths list *
2db11 2f 0a 20 20 57 68 65 72 65 4d 61 73 6b 53 65 74  /.  WhereMaskSet
2db12 20 2a 70 4d 61 73 6b 53 65 74 2c 20 20 20 2f 2a   *pMaskSet,   /*
2db13 20 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 74 61   Mapping from ta
2db14 62 6c 65 73 20 74 6f 20 62 69 74 6d 61 70 73 20  bles to bitmaps 
2db15 2a 2f 0a 20 20 69 6e 74 20 69 46 69 72 73 74 2c  */.  int iFirst,
2db16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2db17 2a 20 42 65 20 73 65 61 72 63 68 69 6e 67 20 77  * Be searching w
2db18 69 74 68 20 74 68 65 20 69 46 69 72 73 74 2d 74  ith the iFirst-t
2db19 68 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a  h expression */.
2db1a 20 20 69 6e 74 20 69 42 61 73 65 20 20 20 20 20    int iBase     
2db1b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
2db1c 67 6e 6f 72 65 20 72 65 66 65 72 65 6e 63 65 73  gnore references
2db1d 20 74 6f 20 74 68 69 73 20 74 61 62 6c 65 20 2a   to this table *
2db1e 2f 0a 29 7b 0a 20 20 42 69 74 6d 61 73 6b 20 61  /.){.  Bitmask a
2db1f 6c 6c 6f 77 65 64 20 3d 20 7e 67 65 74 4d 61 73  llowed = ~getMas
2db20 6b 28 70 4d 61 73 6b 53 65 74 2c 20 69 42 61 73  k(pMaskSet, iBas
2db21 65 29 3b 0a 20 20 77 68 69 6c 65 28 20 69 46 69  e);.  while( iFi
2db22 72 73 74 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72  rst<pList->nExpr
2db23 20 29 7b 0a 20 20 20 20 69 66 28 20 28 65 78 70   ){.    if( (exp
2db24 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73  rTableUsage(pMas
2db25 6b 53 65 74 2c 20 70 4c 69 73 74 2d 3e 61 5b 69  kSet, pList->a[i
2db26 46 69 72 73 74 2b 2b 5d 2e 70 45 78 70 72 29 26  First++].pExpr)&
2db27 61 6c 6c 6f 77 65 64 29 21 3d 30 20 29 7b 0a 20  allowed)!=0 ){. 
2db28 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
2db29 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
2db2a 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54  n 0;.}.../*.** T
2db2b 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65 63 69  his routine deci
2db2c 64 65 73 20 69 66 20 70 49 64 78 20 63 61 6e 20  des if pIdx can 
2db2d 62 65 20 75 73 65 64 20 74 6f 20 73 61 74 69 73  be used to satis
2db2e 66 79 20 74 68 65 20 4f 52 44 45 52 20 42 59 0a  fy the ORDER BY.
2db2f 2a 2a 20 63 6c 61 75 73 65 2e 20 20 49 66 20 69  ** clause.  If i
2db30 74 20 63 61 6e 2c 20 69 74 20 72 65 74 75 72 6e  t can, it return
2db31 73 20 31 2e 20 20 49 66 20 70 49 64 78 20 63 61  s 1.  If pIdx ca
2db32 6e 6e 6f 74 20 73 61 74 69 73 66 79 20 74 68 65  nnot satisfy the
2db33 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 63 6c 61  .** ORDER BY cla
2db34 75 73 65 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  use, this routin
2db35 65 20 72 65 74 75 72 6e 73 20 30 2e 0a 2a 2a 0a  e returns 0..**.
2db36 2a 2a 20 70 4f 72 64 65 72 42 79 20 69 73 20 61  ** pOrderBy is a
2db37 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  n ORDER BY claus
2db38 65 20 66 72 6f 6d 20 61 20 53 45 4c 45 43 54 20  e from a SELECT 
2db39 73 74 61 74 65 6d 65 6e 74 2e 20 20 70 54 61 62  statement.  pTab
2db3a 20 69 73 20 74 68 65 0a 2a 2a 20 6c 65 66 74 2d   is the.** left-
2db3b 6d 6f 73 74 20 74 61 62 6c 65 20 69 6e 20 74 68  most table in th
2db3c 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66  e FROM clause of
2db3d 20 74 68 61 74 20 73 61 6d 65 20 53 45 4c 45 43   that same SELEC
2db3e 54 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 0a  T statement and.
2db3f 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 68 61 73  ** the table has
2db40 20 61 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72   a cursor number
2db41 20 6f 66 20 22 62 61 73 65 22 2e 20 20 70 49 64   of "base".  pId
2db42 78 20 69 73 20 61 6e 20 69 6e 64 65 78 20 6f 6e  x is an index on
2db43 20 70 54 61 62 2e 0a 2a 2a 0a 2a 2a 20 6e 45 71   pTab..**.** nEq
2db44 43 6f 6c 20 69 73 20 74 68 65 20 6e 75 6d 62 65  Col is the numbe
2db45 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20  r of columns of 
2db46 70 49 64 78 20 74 68 61 74 20 61 72 65 20 75 73  pIdx that are us
2db47 65 64 20 61 73 20 65 71 75 61 6c 69 74 79 0a 2a  ed as equality.*
2db48 2a 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 20  * constraints.  
2db49 41 6e 79 20 6f 66 20 74 68 65 73 65 20 63 6f 6c  Any of these col
2db4a 75 6d 6e 73 20 6d 61 79 20 62 65 20 6d 69 73 73  umns may be miss
2db4b 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 4f 52 44  ing from the ORD
2db4c 45 52 20 42 59 0a 2a 2a 20 63 6c 61 75 73 65 20  ER BY.** clause 
2db4d 61 6e 64 20 74 68 65 20 6d 61 74 63 68 20 63 61  and the match ca
2db4e 6e 20 73 74 69 6c 6c 20 62 65 20 61 20 73 75 63  n still be a suc
2db4f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20  cess..**.** All 
2db50 74 65 72 6d 73 20 6f 66 20 74 68 65 20 4f 52 44  terms of the ORD
2db51 45 52 20 42 59 20 74 68 61 74 20 6d 61 74 63 68  ER BY that match
2db52 20 61 67 61 69 6e 73 74 20 74 68 65 20 69 6e 64   against the ind
2db53 65 78 20 6d 75 73 74 20 62 65 20 65 69 74 68 65  ex must be eithe
2db54 72 0a 2a 2a 20 41 53 43 20 6f 72 20 44 45 53 43  r.** ASC or DESC
2db55 2e 20 20 28 54 65 72 6d 73 20 6f 66 20 74 68 65  .  (Terms of the
2db56 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
2db57 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66   past the end of
2db58 20 61 20 55 4e 49 51 55 45 0a 2a 2a 20 69 6e 64   a UNIQUE.** ind
2db59 65 78 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74  ex do not need t
2db5a 6f 20 73 61 74 69 73 66 79 20 74 68 69 73 20 63  o satisfy this c
2db5b 6f 6e 73 74 72 61 69 6e 74 2e 29 20 20 54 68 65  onstraint.)  The
2db5c 20 2a 70 62 52 65 76 20 76 61 6c 75 65 20 69 73   *pbRev value is
2db5d 0a 2a 2a 20 73 65 74 20 74 6f 20 31 20 69 66 20  .** set to 1 if 
2db5e 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  the ORDER BY cla
2db5f 75 73 65 20 69 73 20 61 6c 6c 20 44 45 53 43 20  use is all DESC 
2db60 61 6e 64 20 69 74 20 69 73 20 73 65 74 20 74 6f  and it is set to
2db61 20 30 20 69 66 0a 2a 2a 20 74 68 65 20 4f 52 44   0 if.** the ORD
2db62 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 73 20  ER BY clause is 
2db63 61 6c 6c 20 41 53 43 2e 0a 2a 2f 0a 73 74 61 74  all ASC..*/.stat
2db64 69 63 20 69 6e 74 20 69 73 53 6f 72 74 69 6e 67  ic int isSorting
2db65 49 6e 64 65 78 28 0a 20 20 50 61 72 73 65 20 2a  Index(.  Parse *
2db66 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
2db67 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
2db68 65 78 74 20 2a 2f 0a 20 20 57 68 65 72 65 4d 61  ext */.  WhereMa
2db69 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c  skSet *pMaskSet,
2db6a 20 2f 2a 20 4d 61 70 70 69 6e 67 20 66 72 6f 6d   /* Mapping from
2db6b 20 74 61 62 6c 65 20 63 75 72 73 6f 72 20 6e 75   table cursor nu
2db6c 6d 62 65 72 73 20 74 6f 20 62 69 74 6d 61 70 73  mbers to bitmaps
2db6d 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64   */.  Index *pId
2db6e 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  x,            /*
2db6f 20 54 68 65 20 69 6e 64 65 78 20 77 65 20 61 72   The index we ar
2db70 65 20 74 65 73 74 69 6e 67 20 2a 2f 0a 20 20 69  e testing */.  i
2db71 6e 74 20 62 61 73 65 2c 20 20 20 20 20 20 20 20  nt base,        
2db72 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72         /* Cursor
2db73 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20   number for the 
2db74 74 61 62 6c 65 20 74 6f 20 62 65 20 73 6f 72 74  table to be sort
2db75 65 64 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  ed */.  ExprList
2db76 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 20 20   *pOrderBy,     
2db77 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20  /* The ORDER BY 
2db78 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20  clause */.  int 
2db79 6e 45 71 43 6f 6c 2c 20 20 20 20 20 20 20 20 20  nEqCol,         
2db7a 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
2db7b 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e 73 20 77   index columns w
2db7c 69 74 68 20 3d 3d 20 63 6f 6e 73 74 72 61 69 6e  ith == constrain
2db7d 74 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 62 52  ts */.  int *pbR
2db7e 65 76 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ev              
2db7f 2f 2a 20 53 65 74 20 74 6f 20 31 20 69 66 20 4f  /* Set to 1 if O
2db80 52 44 45 52 20 42 59 20 69 73 20 44 45 53 43 20  RDER BY is DESC 
2db81 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a  */.){.  int i, j
2db82 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2db83 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
2db84 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69 6e  counters */.  in
2db85 74 20 73 6f 72 74 4f 72 64 65 72 20 3d 20 30 3b  t sortOrder = 0;
2db86 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2db87 20 58 4f 52 20 6f 66 20 69 6e 64 65 78 20 61 6e   XOR of index an
2db88 64 20 4f 52 44 45 52 20 42 59 20 73 6f 72 74 20  d ORDER BY sort 
2db89 64 69 72 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69  direction */.  i
2db8a 6e 74 20 6e 54 65 72 6d 3b 20 20 20 20 20 20 20  nt nTerm;       
2db8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2db8c 2a 20 4e 75 6d 62 65 72 20 6f 66 20 4f 52 44 45  * Number of ORDE
2db8d 52 20 42 59 20 74 65 72 6d 73 20 2a 2f 0a 20 20  R BY terms */.  
2db8e 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
2db8f 69 74 65 6d 20 2a 70 54 65 72 6d 3b 20 20 20 20  item *pTerm;    
2db90 2f 2a 20 41 20 74 65 72 6d 20 6f 66 20 74 68 65  /* A term of the
2db91 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
2db92 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
2db93 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
2db94 0a 20 20 61 73 73 65 72 74 28 20 70 4f 72 64 65  .  assert( pOrde
2db95 72 42 79 21 3d 30 20 29 3b 0a 20 20 6e 54 65 72  rBy!=0 );.  nTer
2db96 6d 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45  m = pOrderBy->nE
2db97 78 70 72 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  xpr;.  assert( n
2db98 54 65 72 6d 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20  Term>0 );..  /* 
2db99 41 72 67 75 6d 65 6e 74 20 70 49 64 78 20 6d 75  Argument pIdx mu
2db9a 73 74 20 65 69 74 68 65 72 20 70 6f 69 6e 74 20  st either point 
2db9b 74 6f 20 61 20 27 72 65 61 6c 27 20 6e 61 6d 65  to a 'real' name
2db9c 64 20 69 6e 64 65 78 20 73 74 72 75 63 74 75 72  d index structur
2db9d 65 2c 20 0a 20 20 2a 2a 20 6f 72 20 61 6e 20 69  e, .  ** or an i
2db9e 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 20 61  ndex structure a
2db9f 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 74 68 65 20  llocated on the 
2dba0 73 74 61 63 6b 20 62 79 20 62 65 73 74 42 74 72  stack by bestBtr
2dba1 65 65 49 6e 64 65 78 28 29 20 74 6f 0a 20 20 2a  eeIndex() to.  *
2dba2 2a 20 72 65 70 72 65 73 65 6e 74 20 74 68 65 20  * represent the 
2dba3 72 6f 77 69 64 20 69 6e 64 65 78 20 74 68 61 74  rowid index that
2dba4 20 69 73 20 70 61 72 74 20 6f 66 20 65 76 65 72   is part of ever
2dba5 79 20 74 61 62 6c 65 2e 20 20 2a 2f 0a 20 20 61  y table.  */.  a
2dba6 73 73 65 72 74 28 20 70 49 64 78 2d 3e 7a 4e 61  ssert( pIdx->zNa
2dba7 6d 65 20 7c 7c 20 28 70 49 64 78 2d 3e 6e 43 6f  me || (pIdx->nCo
2dba8 6c 75 6d 6e 3d 3d 31 20 26 26 20 70 49 64 78 2d  lumn==1 && pIdx-
2dba9 3e 61 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d 2d 31  >aiColumn[0]==-1
2dbaa 29 20 29 3b 0a 0a 20 20 2f 2a 20 4d 61 74 63 68  ) );..  /* Match
2dbab 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 4f 52   terms of the OR
2dbac 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 61 67  DER BY clause ag
2dbad 61 69 6e 73 74 20 63 6f 6c 75 6d 6e 73 20 6f 66  ainst columns of
2dbae 0a 20 20 2a 2a 20 74 68 65 20 69 6e 64 65 78 2e  .  ** the index.
2dbaf 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20  .  **.  ** Note 
2dbb0 74 68 61 74 20 69 6e 64 69 63 65 73 20 68 61 76  that indices hav
2dbb1 65 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20  e pIdx->nColumn 
2dbb2 72 65 67 75 6c 61 72 20 63 6f 6c 75 6d 6e 73 20  regular columns 
2dbb3 70 6c 75 73 0a 20 20 2a 2a 20 6f 6e 65 20 61 64  plus.  ** one ad
2dbb4 64 69 74 69 6f 6e 61 6c 20 63 6f 6c 75 6d 6e 20  ditional column 
2dbb5 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 72  containing the r
2dbb6 6f 77 69 64 2e 20 20 54 68 65 20 72 6f 77 69 64  owid.  The rowid
2dbb7 20 63 6f 6c 75 6d 6e 0a 20 20 2a 2a 20 6f 66 20   column.  ** of 
2dbb8 74 68 65 20 69 6e 64 65 78 20 69 73 20 61 6c 73  the index is als
2dbb9 6f 20 61 6c 6c 6f 77 65 64 20 74 6f 20 6d 61 74  o allowed to mat
2dbba 63 68 20 61 67 61 69 6e 73 74 20 74 68 65 20 4f  ch against the O
2dbbb 52 44 45 52 20 42 59 0a 20 20 2a 2a 20 63 6c 61  RDER BY.  ** cla
2dbbc 75 73 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  use..  */.  for(
2dbbd 69 3d 6a 3d 30 2c 20 70 54 65 72 6d 3d 70 4f 72  i=j=0, pTerm=pOr
2dbbe 64 65 72 42 79 2d 3e 61 3b 20 6a 3c 6e 54 65 72  derBy->a; j<nTer
2dbbf 6d 20 26 26 20 69 3c 3d 70 49 64 78 2d 3e 6e 43  m && i<=pIdx->nC
2dbc0 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20  olumn; i++){.   
2dbc1 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20 20   Expr *pExpr;   
2dbc2 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65      /* The expre
2dbc3 73 73 69 6f 6e 20 6f 66 20 74 68 65 20 4f 52 44  ssion of the ORD
2dbc4 45 52 20 42 59 20 70 54 65 72 6d 20 2a 2f 0a 20  ER BY pTerm */. 
2dbc5 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c     CollSeq *pCol
2dbc6 6c 3b 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c  l;    /* The col
2dbc7 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
2dbc8 6f 66 20 70 45 78 70 72 20 2a 2f 0a 20 20 20 20  of pExpr */.    
2dbc9 69 6e 74 20 74 65 72 6d 53 6f 72 74 4f 72 64 65  int termSortOrde
2dbca 72 3b 20 2f 2a 20 53 6f 72 74 20 6f 72 64 65 72  r; /* Sort order
2dbcb 20 66 6f 72 20 74 68 69 73 20 74 65 72 6d 20 2a   for this term *
2dbcc 2f 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d  /.    int iColum
2dbcd 6e 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  n;       /* The 
2dbce 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74  i-th column of t
2dbcf 68 65 20 69 6e 64 65 78 2e 20 20 2d 31 20 66 6f  he index.  -1 fo
2dbd0 72 20 72 6f 77 69 64 20 2a 2f 0a 20 20 20 20 69  r rowid */.    i
2dbd1 6e 74 20 69 53 6f 72 74 4f 72 64 65 72 3b 20 20  nt iSortOrder;  
2dbd2 20 20 2f 2a 20 31 20 66 6f 72 20 44 45 53 43 2c    /* 1 for DESC,
2dbd3 20 30 20 66 6f 72 20 41 53 43 20 6f 6e 20 74 68   0 for ASC on th
2dbd4 65 20 69 2d 74 68 20 69 6e 64 65 78 20 74 65 72  e i-th index ter
2dbd5 6d 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 63  m */.    const c
2dbd6 68 61 72 20 2a 7a 43 6f 6c 6c 3b 20 2f 2a 20 4e  har *zColl; /* N
2dbd7 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61  ame of the colla
2dbd8 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f  ting sequence fo
2dbd9 72 20 69 2d 74 68 20 69 6e 64 65 78 20 74 65 72  r i-th index ter
2dbda 6d 20 2a 2f 0a 0a 20 20 20 20 70 45 78 70 72 20  m */..    pExpr 
2dbdb 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a  = pTerm->pExpr;.
2dbdc 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f      if( pExpr->o
2dbdd 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20  p!=TK_COLUMN || 
2dbde 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 21 3d 62  pExpr->iTable!=b
2dbdf 61 73 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ase ){.      /* 
2dbe0 43 61 6e 20 6e 6f 74 20 75 73 65 20 61 6e 20 69  Can not use an i
2dbe1 6e 64 65 78 20 73 6f 72 74 20 6f 6e 20 61 6e 79  ndex sort on any
2dbe2 74 68 69 6e 67 20 74 68 61 74 20 69 73 20 6e 6f  thing that is no
2dbe3 74 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68  t a column in th
2dbe4 65 0a 20 20 20 20 20 20 2a 2a 20 6c 65 66 74 2d  e.      ** left-
2dbe5 6d 6f 73 74 20 74 61 62 6c 65 20 6f 66 20 74 68  most table of th
2dbe6 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f  e FROM clause */
2dbe7 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2dbe8 20 20 7d 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20    }.    pColl = 
2dbe9 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53  sqlite3ExprCollS
2dbea 65 71 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  eq(pParse, pExpr
2dbeb 29 3b 0a 20 20 20 20 69 66 28 20 21 70 43 6f 6c  );.    if( !pCol
2dbec 6c 20 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c 6c  l ){.      pColl
2dbed 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c   = db->pDfltColl
2dbee 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
2dbef 70 49 64 78 2d 3e 7a 4e 61 6d 65 20 26 26 20 69  pIdx->zName && i
2dbf0 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29  <pIdx->nColumn )
2dbf1 7b 0a 20 20 20 20 20 20 69 43 6f 6c 75 6d 6e 20  {.      iColumn 
2dbf2 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e  = pIdx->aiColumn
2dbf3 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 69  [i];.      if( i
2dbf4 43 6f 6c 75 6d 6e 3d 3d 70 49 64 78 2d 3e 70 54  Column==pIdx->pT
2dbf5 61 62 6c 65 2d 3e 69 50 4b 65 79 20 29 7b 0a 20  able->iPKey ){. 
2dbf6 20 20 20 20 20 20 20 69 43 6f 6c 75 6d 6e 20 3d         iColumn =
2dbf7 20 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   -1;.      }.   
2dbf8 20 20 20 69 53 6f 72 74 4f 72 64 65 72 20 3d 20     iSortOrder = 
2dbf9 70 49 64 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72  pIdx->aSortOrder
2dbfa 5b 69 5d 3b 0a 20 20 20 20 20 20 7a 43 6f 6c 6c  [i];.      zColl
2dbfb 20 3d 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b   = pIdx->azColl[
2dbfc 69 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  i];.    }else{. 
2dbfd 20 20 20 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 2d       iColumn = -
2dbfe 31 3b 0a 20 20 20 20 20 20 69 53 6f 72 74 4f 72  1;.      iSortOr
2dbff 64 65 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 7a  der = 0;.      z
2dc00 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 2d 3e 7a 4e  Coll = pColl->zN
2dc01 61 6d 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ame;.    }.    i
2dc02 66 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d  f( pExpr->iColum
2dc03 6e 21 3d 69 43 6f 6c 75 6d 6e 20 7c 7c 20 73 71  n!=iColumn || sq
2dc04 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f  lite3StrICmp(pCo
2dc05 6c 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 6c  ll->zName, zColl
2dc06 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 65  ) ){.      /* Te
2dc07 72 6d 20 6a 20 6f 66 20 74 68 65 20 4f 52 44 45  rm j of the ORDE
2dc08 52 20 42 59 20 63 6c 61 75 73 65 20 64 6f 65 73  R BY clause does
2dc09 20 6e 6f 74 20 6d 61 74 63 68 20 63 6f 6c 75 6d   not match colum
2dc0a 6e 20 69 20 6f 66 20 74 68 65 20 69 6e 64 65 78  n i of the index
2dc0b 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 3c   */.      if( i<
2dc0c 6e 45 71 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20  nEqCol ){.      
2dc0d 20 20 2f 2a 20 49 66 20 61 6e 20 69 6e 64 65 78    /* If an index
2dc0e 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 20   column that is 
2dc0f 63 6f 6e 73 74 72 61 69 6e 65 64 20 62 79 20 3d  constrained by =
2dc10 3d 20 66 61 69 6c 73 20 74 6f 20 6d 61 74 63 68  = fails to match
2dc11 20 61 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f   an.        ** O
2dc12 52 44 45 52 20 42 59 20 74 65 72 6d 2c 20 74 68  RDER BY term, th
2dc13 61 74 20 69 73 20 4f 4b 2e 20 20 4a 75 73 74 20  at is OK.  Just 
2dc14 69 67 6e 6f 72 65 20 74 68 61 74 20 63 6f 6c 75  ignore that colu
2dc15 6d 6e 20 6f 66 20 74 68 65 20 69 6e 64 65 78 0a  mn of the index.
2dc16 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
2dc17 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
2dc18 20 20 20 7d 65 6c 73 65 20 69 66 28 20 69 3d 3d     }else if( i==
2dc19 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b  pIdx->nColumn ){
2dc1a 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65  .        /* Inde
2dc1b 78 20 63 6f 6c 75 6d 6e 20 69 20 69 73 20 74 68  x column i is th
2dc1c 65 20 72 6f 77 69 64 2e 20 20 41 6c 6c 20 6f 74  e rowid.  All ot
2dc1d 68 65 72 20 74 65 72 6d 73 20 6d 61 74 63 68 2e  her terms match.
2dc1e 20 2a 2f 0a 20 20 20 20 20 20 20 20 62 72 65 61   */.        brea
2dc1f 6b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  k;.      }else{.
2dc20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61 6e          /* If an
2dc21 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e 20 66 61   index column fa
2dc22 69 6c 73 20 74 6f 20 6d 61 74 63 68 20 61 6e 64  ils to match and
2dc23 20 69 73 20 6e 6f 74 20 63 6f 6e 73 74 72 61 69   is not constrai
2dc24 6e 65 64 20 62 79 20 3d 3d 0a 20 20 20 20 20 20  ned by ==.      
2dc25 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 69 6e    ** then the in
2dc26 64 65 78 20 63 61 6e 6e 6f 74 20 73 61 74 69 73  dex cannot satis
2dc27 66 79 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  fy the ORDER BY 
2dc28 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 20 20 20 20  constraint..    
2dc29 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72      */.        r
2dc2a 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d  eturn 0;.      }
2dc2b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
2dc2c 74 28 20 70 49 64 78 2d 3e 61 53 6f 72 74 4f 72  t( pIdx->aSortOr
2dc2d 64 65 72 21 3d 30 20 7c 7c 20 69 43 6f 6c 75 6d  der!=0 || iColum
2dc2e 6e 3d 3d 2d 31 20 29 3b 0a 20 20 20 20 61 73 73  n==-1 );.    ass
2dc2f 65 72 74 28 20 70 54 65 72 6d 2d 3e 73 6f 72 74  ert( pTerm->sort
2dc30 4f 72 64 65 72 3d 3d 30 20 7c 7c 20 70 54 65 72  Order==0 || pTer
2dc31 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 3d 3d 31 20  m->sortOrder==1 
2dc32 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  );.    assert( i
2dc33 53 6f 72 74 4f 72 64 65 72 3d 3d 30 20 7c 7c 20  SortOrder==0 || 
2dc34 69 53 6f 72 74 4f 72 64 65 72 3d 3d 31 20 29 3b  iSortOrder==1 );
2dc35 0a 20 20 20 20 74 65 72 6d 53 6f 72 74 4f 72 64  .    termSortOrd
2dc36 65 72 20 3d 20 69 53 6f 72 74 4f 72 64 65 72 20  er = iSortOrder 
2dc37 5e 20 70 54 65 72 6d 2d 3e 73 6f 72 74 4f 72 64  ^ pTerm->sortOrd
2dc38 65 72 3b 0a 20 20 20 20 69 66 28 20 69 3e 6e 45  er;.    if( i>nE
2dc39 71 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 69 66  qCol ){.      if
2dc3a 28 20 74 65 72 6d 53 6f 72 74 4f 72 64 65 72 21  ( termSortOrder!
2dc3b 3d 73 6f 72 74 4f 72 64 65 72 20 29 7b 0a 20 20  =sortOrder ){.  
2dc3c 20 20 20 20 20 20 2f 2a 20 49 6e 64 69 63 65 73        /* Indices
2dc3d 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 75 73 65   can only be use
2dc3e 64 20 69 66 20 61 6c 6c 20 4f 52 44 45 52 20 42  d if all ORDER B
2dc3f 59 20 74 65 72 6d 73 20 70 61 73 74 20 74 68 65  Y terms past the
2dc40 0a 20 20 20 20 20 20 20 20 2a 2a 20 65 71 75 61  .        ** equa
2dc41 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73  lity constraints
2dc42 20 61 72 65 20 61 6c 6c 20 65 69 74 68 65 72 20   are all either 
2dc43 44 45 53 43 20 6f 72 20 41 53 43 2e 20 2a 2f 0a  DESC or ASC. */.
2dc44 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30          return 0
2dc45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
2dc46 6c 73 65 7b 0a 20 20 20 20 20 20 73 6f 72 74 4f  lse{.      sortO
2dc47 72 64 65 72 20 3d 20 74 65 72 6d 53 6f 72 74 4f  rder = termSortO
2dc48 72 64 65 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rder;.    }.    
2dc49 6a 2b 2b 3b 0a 20 20 20 20 70 54 65 72 6d 2b 2b  j++;.    pTerm++
2dc4a 3b 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 75 6d  ;.    if( iColum
2dc4b 6e 3c 30 20 26 26 20 21 72 65 66 65 72 65 6e 63  n<0 && !referenc
2dc4c 65 73 4f 74 68 65 72 54 61 62 6c 65 73 28 70 4f  esOtherTables(pO
2dc4d 72 64 65 72 42 79 2c 20 70 4d 61 73 6b 53 65 74  rderBy, pMaskSet
2dc4e 2c 20 6a 2c 20 62 61 73 65 29 20 29 7b 0a 20 20  , j, base) ){.  
2dc4f 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 69 6e      /* If the in
2dc50 64 65 78 65 64 20 63 6f 6c 75 6d 6e 20 69 73 20  dexed column is 
2dc51 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20  the primary key 
2dc52 61 6e 64 20 65 76 65 72 79 74 68 69 6e 67 20 6d  and everything m
2dc53 61 74 63 68 65 73 0a 20 20 20 20 20 20 2a 2a 20  atches.      ** 
2dc54 73 6f 20 66 61 72 20 61 6e 64 20 6e 6f 6e 65 20  so far and none 
2dc55 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  of the ORDER BY 
2dc56 74 65 72 6d 73 20 74 6f 20 74 68 65 20 72 69 67  terms to the rig
2dc57 68 74 20 72 65 66 65 72 65 6e 63 65 20 6f 74 68  ht reference oth
2dc58 65 72 0a 20 20 20 20 20 20 2a 2a 20 74 61 62 6c  er.      ** tabl
2dc59 65 73 20 69 6e 20 74 68 65 20 6a 6f 69 6e 2c 20  es in the join, 
2dc5a 74 68 65 6e 20 77 65 20 61 72 65 20 61 73 73 75  then we are assu
2dc5b 72 65 64 20 74 68 61 74 20 74 68 65 20 69 6e 64  red that the ind
2dc5c 65 78 20 63 61 6e 20 62 65 20 75 73 65 64 20 0a  ex can be used .
2dc5d 20 20 20 20 20 20 2a 2a 20 74 6f 20 73 6f 72 74        ** to sort
2dc5e 20 62 65 63 61 75 73 65 20 74 68 65 20 70 72 69   because the pri
2dc5f 6d 61 72 79 20 6b 65 79 20 69 73 20 75 6e 69 71  mary key is uniq
2dc60 75 65 20 61 6e 64 20 73 6f 20 6e 6f 6e 65 20 6f  ue and so none o
2dc61 66 20 74 68 65 20 6f 74 68 65 72 0a 20 20 20 20  f the other.    
2dc62 20 20 2a 2a 20 63 6f 6c 75 6d 6e 73 20 77 69 6c    ** columns wil
2dc63 6c 20 6d 61 6b 65 20 61 6e 79 20 64 69 66 66 65  l make any diffe
2dc64 72 65 6e 63 65 0a 20 20 20 20 20 20 2a 2f 0a 20  rence.      */. 
2dc65 20 20 20 20 20 6a 20 3d 20 6e 54 65 72 6d 3b 0a       j = nTerm;.
2dc66 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 62      }.  }..  *pb
2dc67 52 65 76 20 3d 20 73 6f 72 74 4f 72 64 65 72 21  Rev = sortOrder!
2dc68 3d 30 3b 0a 20 20 69 66 28 20 6a 3e 3d 6e 54 65  =0;.  if( j>=nTe
2dc69 72 6d 20 29 7b 0a 20 20 20 20 2f 2a 20 41 6c 6c  rm ){.    /* All
2dc6a 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 4f 52   terms of the OR
2dc6b 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 61 72  DER BY clause ar
2dc6c 65 20 63 6f 76 65 72 65 64 20 62 79 20 74 68 69  e covered by thi
2dc6d 73 20 69 6e 64 65 78 20 73 6f 0a 20 20 20 20 2a  s index so.    *
2dc6e 2a 20 74 68 69 73 20 69 6e 64 65 78 20 63 61 6e  * this index can
2dc6f 20 62 65 20 75 73 65 64 20 66 6f 72 20 73 6f 72   be used for sor
2dc70 74 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 72 65 74  ting. */.    ret
2dc71 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28  urn 1;.  }.  if(
2dc72 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d   pIdx->onError!=
2dc73 4f 45 5f 4e 6f 6e 65 20 26 26 20 69 3d 3d 70 49  OE_None && i==pI
2dc74 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 0a 20 20 20 20  dx->nColumn.    
2dc75 20 20 26 26 20 21 72 65 66 65 72 65 6e 63 65 73    && !references
2dc76 4f 74 68 65 72 54 61 62 6c 65 73 28 70 4f 72 64  OtherTables(pOrd
2dc77 65 72 42 79 2c 20 70 4d 61 73 6b 53 65 74 2c 20  erBy, pMaskSet, 
2dc78 6a 2c 20 62 61 73 65 29 20 29 7b 0a 20 20 20 20  j, base) ){.    
2dc79 2f 2a 20 41 6c 6c 20 74 65 72 6d 73 20 6f 66 20  /* All terms of 
2dc7a 74 68 69 73 20 69 6e 64 65 78 20 6d 61 74 63 68  this index match
2dc7b 20 73 6f 6d 65 20 70 72 65 66 69 78 20 6f 66 20   some prefix of 
2dc7c 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  the ORDER BY cla
2dc7d 75 73 65 0a 20 20 20 20 2a 2a 20 61 6e 64 20 74  use.    ** and t
2dc7e 68 65 20 69 6e 64 65 78 20 69 73 20 55 4e 49 51  he index is UNIQ
2dc7f 55 45 20 61 6e 64 20 6e 6f 20 74 65 72 6d 73 20  UE and no terms 
2dc80 6f 6e 20 74 68 65 20 74 61 69 6c 20 6f 66 20 74  on the tail of t
2dc81 68 65 20 4f 52 44 45 52 20 42 59 0a 20 20 20 20  he ORDER BY.    
2dc82 2a 2a 20 63 6c 61 75 73 65 20 72 65 66 65 72 65  ** clause refere
2dc83 6e 63 65 20 6f 74 68 65 72 20 74 61 62 6c 65 73  nce other tables
2dc84 20 69 6e 20 61 20 6a 6f 69 6e 2e 20 20 49 66 20   in a join.  If 
2dc85 74 68 69 73 20 69 73 20 61 6c 6c 20 74 72 75 65  this is all true
2dc86 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 74 68 65   then.    ** the
2dc87 20 6f 72 64 65 72 20 62 79 20 63 6c 61 75 73 65   order by clause
2dc88 20 69 73 20 73 75 70 65 72 66 6c 75 6f 75 73 2e   is superfluous.
2dc89 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31   */.    return 1
2dc8a 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  ;.  }.  return 0
2dc8b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61  ;.}../*.** Prepa
2dc8c 72 65 20 61 20 63 72 75 64 65 20 65 73 74 69 6d  re a crude estim
2dc8d 61 74 65 20 6f 66 20 74 68 65 20 6c 6f 67 61 72  ate of the logar
2dc8e 69 74 68 6d 20 6f 66 20 74 68 65 20 69 6e 70 75  ithm of the inpu
2dc8f 74 20 76 61 6c 75 65 2e 0a 2a 2a 20 54 68 65 20  t value..** The 
2dc90 72 65 73 75 6c 74 73 20 6e 65 65 64 20 6e 6f 74  results need not
2dc91 20 62 65 20 65 78 61 63 74 2e 20 20 54 68 69 73   be exact.  This
2dc92 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 66 6f   is only used fo
2dc93 72 20 65 73 74 69 6d 61 74 69 6e 67 0a 2a 2a 20  r estimating.** 
2dc94 74 68 65 20 74 6f 74 61 6c 20 63 6f 73 74 20 6f  the total cost o
2dc95 66 20 70 65 72 66 6f 72 6d 69 6e 67 20 6f 70 65  f performing ope
2dc96 72 61 74 69 6f 6e 73 20 77 69 74 68 20 4f 28 6c  rations with O(l
2dc97 6f 67 4e 29 20 6f 72 20 4f 28 4e 6c 6f 67 4e 29  ogN) or O(NlogN)
2dc98 0a 2a 2a 20 63 6f 6d 70 6c 65 78 69 74 79 2e 20  .** complexity. 
2dc99 20 42 65 63 61 75 73 65 20 4e 20 69 73 20 6a 75   Because N is ju
2dc9a 73 74 20 61 20 67 75 65 73 73 2c 20 69 74 20 69  st a guess, it i
2dc9b 73 20 6e 6f 20 67 72 65 61 74 20 74 72 61 67 65  s no great trage
2dc9c 64 79 20 69 66 0a 2a 2a 20 6c 6f 67 4e 20 69 73  dy if.** logN is
2dc9d 20 61 20 6c 69 74 74 6c 65 20 6f 66 66 2e 0a 2a   a little off..*
2dc9e 2f 0a 73 74 61 74 69 63 20 64 6f 75 62 6c 65 20  /.static double 
2dc9f 65 73 74 4c 6f 67 28 64 6f 75 62 6c 65 20 4e 29  estLog(double N)
2dca0 7b 0a 20 20 64 6f 75 62 6c 65 20 6c 6f 67 4e 20  {.  double logN 
2dca1 3d 20 31 3b 0a 20 20 64 6f 75 62 6c 65 20 78 20  = 1;.  double x 
2dca2 3d 20 31 30 3b 0a 20 20 77 68 69 6c 65 28 20 4e  = 10;.  while( N
2dca3 3e 78 20 29 7b 0a 20 20 20 20 6c 6f 67 4e 20 2b  >x ){.    logN +
2dca4 3d 20 31 3b 0a 20 20 20 20 78 20 2a 3d 20 31 30  = 1;.    x *= 10
2dca5 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6c  ;.  }.  return l
2dca6 6f 67 4e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 77  ogN;.}../*.** Tw
2dca7 6f 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 70  o routines for p
2dca8 72 69 6e 74 69 6e 67 20 74 68 65 20 63 6f 6e 74  rinting the cont
2dca9 65 6e 74 20 6f 66 20 61 6e 20 73 71 6c 69 74 65  ent of an sqlite
2dcaa 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 0a 2a 2a 20  3_index_info.** 
2dcab 73 74 72 75 63 74 75 72 65 2e 20 20 55 73 65 64  structure.  Used
2dcac 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64   for testing and
2dcad 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e   debugging only.
2dcae 20 20 49 66 20 6e 65 69 74 68 65 72 0a 2a 2a 20    If neither.** 
2dcaf 53 51 4c 49 54 45 5f 54 45 53 54 20 6f 72 20 53  SQLITE_TEST or S
2dcb0 51 4c 49 54 45 5f 44 45 42 55 47 20 61 72 65 20  QLITE_DEBUG are 
2dcb1 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 74 68  defined, then th
2dcb2 65 73 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20  ese routines.** 
2dcb3 61 72 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 23  are no-ops..*/.#
2dcb4 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
2dcb5 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
2dcb6 41 42 4c 45 29 20 26 26 20 64 65 66 69 6e 65 64  ABLE) && defined
2dcb7 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 73  (SQLITE_DEBUG).s
2dcb8 74 61 74 69 63 20 76 6f 69 64 20 54 52 41 43 45  tatic void TRACE
2dcb9 5f 49 44 58 5f 49 4e 50 55 54 53 28 73 71 6c 69  _IDX_INPUTS(sqli
2dcba 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a  te3_index_info *
2dcbb 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  p){.  int i;.  i
2dcbc 66 28 20 21 73 71 6c 69 74 65 33 57 68 65 72 65  f( !sqlite3Where
2dcbd 54 72 61 63 65 20 29 20 72 65 74 75 72 6e 3b 0a  Trace ) return;.
2dcbe 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e    for(i=0; i<p->
2dcbf 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 2b 2b  nConstraint; i++
2dcc0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  ){.    sqlite3De
2dcc1 62 75 67 50 72 69 6e 74 66 28 22 20 20 63 6f 6e  bugPrintf("  con
2dcc2 73 74 72 61 69 6e 74 5b 25 64 5d 3a 20 63 6f 6c  straint[%d]: col
2dcc3 3d 25 64 20 74 65 72 6d 69 64 3d 25 64 20 6f 70  =%d termid=%d op
2dcc4 3d 25 64 20 75 73 61 62 6c 65 64 3d 25 64 5c 6e  =%d usabled=%d\n
2dcc5 22 2c 0a 20 20 20 20 20 20 20 69 2c 0a 20 20 20  ",.       i,.   
2dcc6 20 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72 61 69      p->aConstrai
2dcc7 6e 74 5b 69 5d 2e 69 43 6f 6c 75 6d 6e 2c 0a 20  nt[i].iColumn,. 
2dcc8 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72        p->aConstr
2dcc9 61 69 6e 74 5b 69 5d 2e 69 54 65 72 6d 4f 66 66  aint[i].iTermOff
2dcca 73 65 74 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61  set,.       p->a
2dccb 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 2e 6f 70  Constraint[i].op
2dccc 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e  ,.       p->aCon
2dccd 73 74 72 61 69 6e 74 5b 69 5d 2e 75 73 61 62 6c  straint[i].usabl
2dcce 65 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d  e);.  }.  for(i=
2dccf 30 3b 20 69 3c 70 2d 3e 6e 4f 72 64 65 72 42 79  0; i<p->nOrderBy
2dcd0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69  ; i++){.    sqli
2dcd1 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
2dcd2 20 20 6f 72 64 65 72 62 79 5b 25 64 5d 3a 20 63    orderby[%d]: c
2dcd3 6f 6c 3d 25 64 20 64 65 73 63 3d 25 64 5c 6e 22  ol=%d desc=%d\n"
2dcd4 2c 0a 20 20 20 20 20 20 20 69 2c 0a 20 20 20 20  ,.       i,.    
2dcd5 20 20 20 70 2d 3e 61 4f 72 64 65 72 42 79 5b 69     p->aOrderBy[i
2dcd6 5d 2e 69 43 6f 6c 75 6d 6e 2c 0a 20 20 20 20 20  ].iColumn,.     
2dcd7 20 20 70 2d 3e 61 4f 72 64 65 72 42 79 5b 69 5d    p->aOrderBy[i]
2dcd8 2e 64 65 73 63 29 3b 0a 20 20 7d 0a 7d 0a 73 74  .desc);.  }.}.st
2dcd9 61 74 69 63 20 76 6f 69 64 20 54 52 41 43 45 5f  atic void TRACE_
2dcda 49 44 58 5f 4f 55 54 50 55 54 53 28 73 71 6c 69  IDX_OUTPUTS(sqli
2dcdb 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a  te3_index_info *
2dcdc 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  p){.  int i;.  i
2dcdd 66 28 20 21 73 71 6c 69 74 65 33 57 68 65 72 65  f( !sqlite3Where
2dcde 54 72 61 63 65 20 29 20 72 65 74 75 72 6e 3b 0a  Trace ) return;.
2dcdf 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e    for(i=0; i<p->
2dce0 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 2b 2b  nConstraint; i++
2dce1 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  ){.    sqlite3De
2dce2 62 75 67 50 72 69 6e 74 66 28 22 20 20 75 73 61  bugPrintf("  usa
2dce3 67 65 5b 25 64 5d 3a 20 61 72 67 76 49 64 78 3d  ge[%d]: argvIdx=
2dce4 25 64 20 6f 6d 69 74 3d 25 64 5c 6e 22 2c 0a 20  %d omit=%d\n",. 
2dce5 20 20 20 20 20 20 69 2c 0a 20 20 20 20 20 20 20        i,.       
2dce6 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73  p->aConstraintUs
2dce7 61 67 65 5b 69 5d 2e 61 72 67 76 49 6e 64 65 78  age[i].argvIndex
2dce8 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e  ,.       p->aCon
2dce9 73 74 72 61 69 6e 74 55 73 61 67 65 5b 69 5d 2e  straintUsage[i].
2dcea 6f 6d 69 74 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  omit);.  }.  sql
2dceb 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
2dcec 22 20 20 69 64 78 4e 75 6d 3d 25 64 5c 6e 22 2c  "  idxNum=%d\n",
2dced 20 70 2d 3e 69 64 78 4e 75 6d 29 3b 0a 20 20 73   p->idxNum);.  s
2dcee 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
2dcef 66 28 22 20 20 69 64 78 53 74 72 3d 25 73 5c 6e  f("  idxStr=%s\n
2dcf0 22 2c 20 70 2d 3e 69 64 78 53 74 72 29 3b 0a 20  ", p->idxStr);. 
2dcf1 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
2dcf2 6e 74 66 28 22 20 20 6f 72 64 65 72 42 79 43 6f  ntf("  orderByCo
2dcf3 6e 73 75 6d 65 64 3d 25 64 5c 6e 22 2c 20 70 2d  nsumed=%d\n", p-
2dcf4 3e 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d 65 64  >orderByConsumed
2dcf5 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 62 75  );.  sqlite3Debu
2dcf6 67 50 72 69 6e 74 66 28 22 20 20 65 73 74 69 6d  gPrintf("  estim
2dcf7 61 74 65 64 43 6f 73 74 3d 25 67 5c 6e 22 2c 20  atedCost=%g\n", 
2dcf8 70 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f 73 74  p->estimatedCost
2dcf9 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69  );.}.#else.#defi
2dcfa 6e 65 20 54 52 41 43 45 5f 49 44 58 5f 49 4e 50  ne TRACE_IDX_INP
2dcfb 55 54 53 28 41 29 0a 23 64 65 66 69 6e 65 20 54  UTS(A).#define T
2dcfc 52 41 43 45 5f 49 44 58 5f 4f 55 54 50 55 54 53  RACE_IDX_OUTPUTS
2dcfd 28 41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 0a  (A).#endif../* .
2dcfe 2a 2a 20 52 65 71 75 69 72 65 64 20 62 65 63 61  ** Required beca
2dcff 75 73 65 20 62 65 73 74 49 6e 64 65 78 28 29 20  use bestIndex() 
2dd00 69 73 20 63 61 6c 6c 65 64 20 62 79 20 62 65 73  is called by bes
2dd01 74 4f 72 43 6c 61 75 73 65 49 6e 64 65 78 28 29  tOrClauseIndex()
2dd02 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64   .*/.static void
2dd03 20 62 65 73 74 49 6e 64 65 78 28 0a 20 20 20 20   bestIndex(.    
2dd04 50 61 72 73 65 2a 2c 20 57 68 65 72 65 43 6c 61  Parse*, WhereCla
2dd05 75 73 65 2a 2c 20 73 74 72 75 63 74 20 53 72 63  use*, struct Src
2dd06 4c 69 73 74 5f 69 74 65 6d 2a 2c 20 42 69 74 6d  List_item*, Bitm
2dd07 61 73 6b 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20  ask, ExprList*, 
2dd08 57 68 65 72 65 43 6f 73 74 2a 29 3b 0a 0a 2f 2a  WhereCost*);../*
2dd09 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
2dd0a 20 61 74 74 65 6d 70 74 73 20 74 6f 20 66 69 6e   attempts to fin
2dd0b 64 20 61 6e 20 73 63 61 6e 6e 69 6e 67 20 73 74  d an scanning st
2dd0c 72 61 74 65 67 79 20 74 68 61 74 20 63 61 6e 20  rategy that can 
2dd0d 62 65 20 75 73 65 64 20 0a 2a 2a 20 74 6f 20 6f  be used .** to o
2dd0e 70 74 69 6d 69 7a 65 20 61 6e 20 27 4f 52 27 20  ptimize an 'OR' 
2dd0f 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20  expression that 
2dd10 69 73 20 70 61 72 74 20 6f 66 20 61 20 57 48 45  is part of a WHE
2dd11 52 45 20 63 6c 61 75 73 65 2e 20 0a 2a 2a 0a 2a  RE clause. .**.*
2dd12 2a 20 54 68 65 20 74 61 62 6c 65 20 61 73 73 6f  * The table asso
2dd13 63 69 61 74 65 64 20 77 69 74 68 20 46 52 4f 4d  ciated with FROM
2dd14 20 63 6c 61 75 73 65 20 74 65 72 6d 20 70 53 72   clause term pSr
2dd15 63 20 6d 61 79 20 62 65 20 65 69 74 68 65 72 20  c may be either 
2dd16 61 0a 2a 2a 20 72 65 67 75 6c 61 72 20 42 2d 54  a.** regular B-T
2dd17 72 65 65 20 74 61 62 6c 65 20 6f 72 20 61 20 76  ree table or a v
2dd18 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f  irtual table..*/
2dd19 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62 65 73  .static void bes
2dd1a 74 4f 72 43 6c 61 75 73 65 49 6e 64 65 78 28 0a  tOrClauseIndex(.
2dd1b 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2dd1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2dd1d 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e   The parsing con
2dd1e 74 65 78 74 20 2a 2f 0a 20 20 57 68 65 72 65 43  text */.  WhereC
2dd1f 6c 61 75 73 65 20 2a 70 57 43 2c 20 20 20 20 20  lause *pWC,     
2dd20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45        /* The WHE
2dd21 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73  RE clause */.  s
2dd22 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
2dd23 65 6d 20 2a 70 53 72 63 2c 20 20 2f 2a 20 54 68  em *pSrc,  /* Th
2dd24 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65  e FROM clause te
2dd25 72 6d 20 74 6f 20 73 65 61 72 63 68 20 2a 2f 0a  rm to search */.
2dd26 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61    Bitmask notRea
2dd27 64 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  dy,           /*
2dd28 20 4d 61 73 6b 20 6f 66 20 63 75 72 73 6f 72 73   Mask of cursors
2dd29 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20 61 76   that are not av
2dd2a 61 69 6c 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70  ailable */.  Exp
2dd2b 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c  rList *pOrderBy,
2dd2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2dd2d 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
2dd2e 2a 2f 0a 20 20 57 68 65 72 65 43 6f 73 74 20 2a  */.  WhereCost *
2dd2f 70 43 6f 73 74 20 20 20 20 20 20 20 20 20 20 20  pCost           
2dd30 20 2f 2a 20 4c 6f 77 65 73 74 20 63 6f 73 74 20   /* Lowest cost 
2dd31 71 75 65 72 79 20 70 6c 61 6e 20 2a 2f 0a 29 7b  query plan */.){
2dd32 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2dd33 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41  OMIT_OR_OPTIMIZA
2dd34 54 49 4f 4e 0a 20 20 63 6f 6e 73 74 20 69 6e 74  TION.  const int
2dd35 20 69 43 75 72 20 3d 20 70 53 72 63 2d 3e 69 43   iCur = pSrc->iC
2dd36 75 72 73 6f 72 3b 20 20 20 2f 2a 20 54 68 65 20  ursor;   /* The 
2dd37 63 75 72 73 6f 72 20 6f 66 20 74 68 65 20 74 61  cursor of the ta
2dd38 62 6c 65 20 74 6f 20 62 65 20 61 63 63 65 73 73  ble to be access
2dd39 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 42 69  ed */.  const Bi
2dd3a 74 6d 61 73 6b 20 6d 61 73 6b 53 72 63 20 3d 20  tmask maskSrc = 
2dd3b 67 65 74 4d 61 73 6b 28 70 57 43 2d 3e 70 4d 61  getMask(pWC->pMa
2dd3c 73 6b 53 65 74 2c 20 69 43 75 72 29 3b 20 20 2f  skSet, iCur);  /
2dd3d 2a 20 42 69 74 6d 61 73 6b 20 66 6f 72 20 70 53  * Bitmask for pS
2dd3e 72 63 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72  rc */.  WhereTer
2dd3f 6d 20 2a 20 63 6f 6e 73 74 20 70 57 43 45 6e 64  m * const pWCEnd
2dd40 20 3d 20 26 70 57 43 2d 3e 61 5b 70 57 43 2d 3e   = &pWC->a[pWC->
2dd41 6e 54 65 72 6d 5d 3b 20 20 20 20 20 20 20 20 2f  nTerm];        /
2dd42 2a 20 45 6e 64 20 6f 66 20 70 57 43 2d 3e 61 5b  * End of pWC->a[
2dd43 5d 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72 6d  ] */.  WhereTerm
2dd44 20 2a 70 54 65 72 6d 3b 20 20 20 20 20 20 20 20   *pTerm;        
2dd45 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73 69           /* A si
2dd46 6e 67 6c 65 20 74 65 72 6d 20 6f 66 20 74 68 65  ngle term of the
2dd47 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f   WHERE clause */
2dd48 0a 0a 20 20 2f 2a 20 53 65 61 72 63 68 20 74 68  ..  /* Search th
2dd49 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74  e WHERE clause t
2dd4a 65 72 6d 73 20 66 6f 72 20 61 20 75 73 61 62 6c  erms for a usabl
2dd4b 65 20 57 4f 5f 4f 52 20 74 65 72 6d 2e 20 2a 2f  e WO_OR term. */
2dd4c 0a 20 20 66 6f 72 28 70 54 65 72 6d 3d 70 57 43  .  for(pTerm=pWC
2dd4d 2d 3e 61 3b 20 70 54 65 72 6d 3c 70 57 43 45 6e  ->a; pTerm<pWCEn
2dd4e 64 3b 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20  d; pTerm++){.   
2dd4f 20 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65   if( pTerm->eOpe
2dd50 72 61 74 6f 72 3d 3d 57 4f 5f 4f 52 20 0a 20 20  rator==WO_OR .  
2dd51 20 20 20 26 26 20 28 28 70 54 65 72 6d 2d 3e 70     && ((pTerm->p
2dd52 72 65 72 65 71 41 6c 6c 20 26 20 7e 6d 61 73 6b  rereqAll & ~mask
2dd53 53 72 63 29 20 26 20 6e 6f 74 52 65 61 64 79 29  Src) & notReady)
2dd54 3d 3d 30 0a 20 20 20 20 20 26 26 20 28 70 54 65  ==0.     && (pTe
2dd55 72 6d 2d 3e 75 2e 70 4f 72 49 6e 66 6f 2d 3e 69  rm->u.pOrInfo->i
2dd56 6e 64 65 78 61 62 6c 65 20 26 20 6d 61 73 6b 53  ndexable & maskS
2dd57 72 63 29 21 3d 30 20 0a 20 20 20 20 29 7b 0a 20  rc)!=0 .    ){. 
2dd58 20 20 20 20 20 57 68 65 72 65 43 6c 61 75 73 65       WhereClause
2dd59 20 2a 20 63 6f 6e 73 74 20 70 4f 72 57 43 20 3d   * const pOrWC =
2dd5a 20 26 70 54 65 72 6d 2d 3e 75 2e 70 4f 72 49 6e   &pTerm->u.pOrIn
2dd5b 66 6f 2d 3e 77 63 3b 0a 20 20 20 20 20 20 57 68  fo->wc;.      Wh
2dd5c 65 72 65 54 65 72 6d 20 2a 20 63 6f 6e 73 74 20  ereTerm * const 
2dd5d 70 4f 72 57 43 45 6e 64 20 3d 20 26 70 4f 72 57  pOrWCEnd = &pOrW
2dd5e 43 2d 3e 61 5b 70 4f 72 57 43 2d 3e 6e 54 65 72  C->a[pOrWC->nTer
2dd5f 6d 5d 3b 0a 20 20 20 20 20 20 57 68 65 72 65 54  m];.      WhereT
2dd60 65 72 6d 20 2a 70 4f 72 54 65 72 6d 3b 0a 20 20  erm *pOrTerm;.  
2dd61 20 20 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20      int flags = 
2dd62 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 3b 0a  WHERE_MULTI_OR;.
2dd63 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 54 6f        double rTo
2dd64 74 61 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 64  tal = 0;.      d
2dd65 6f 75 62 6c 65 20 6e 52 6f 77 20 3d 20 30 3b 0a  ouble nRow = 0;.
2dd66 20 20 20 20 20 20 42 69 74 6d 61 73 6b 20 75 73        Bitmask us
2dd67 65 64 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20 66  ed = 0;..      f
2dd68 6f 72 28 70 4f 72 54 65 72 6d 3d 70 4f 72 57 43  or(pOrTerm=pOrWC
2dd69 2d 3e 61 3b 20 70 4f 72 54 65 72 6d 3c 70 4f 72  ->a; pOrTerm<pOr
2dd6a 57 43 45 6e 64 3b 20 70 4f 72 54 65 72 6d 2b 2b  WCEnd; pOrTerm++
2dd6b 29 7b 0a 20 20 20 20 20 20 20 20 57 68 65 72 65  ){.        Where
2dd6c 43 6f 73 74 20 73 54 65 72 6d 43 6f 73 74 3b 0a  Cost sTermCost;.
2dd6d 20 20 20 20 20 20 20 20 57 48 45 52 45 54 52 41          WHERETRA
2dd6e 43 45 28 28 22 2e 2e 2e 20 4d 75 6c 74 69 2d 69  CE(("... Multi-i
2dd6f 6e 64 65 78 20 4f 52 20 74 65 73 74 69 6e 67 20  ndex OR testing 
2dd70 66 6f 72 20 74 65 72 6d 20 25 64 20 6f 66 20 25  for term %d of %
2dd71 64 2e 2e 2e 2e 5c 6e 22 2c 20 0a 20 20 20 20 20  d....\n", .     
2dd72 20 20 20 20 20 28 70 4f 72 54 65 72 6d 20 2d 20       (pOrTerm - 
2dd73 70 4f 72 57 43 2d 3e 61 29 2c 20 28 70 54 65 72  pOrWC->a), (pTer
2dd74 6d 20 2d 20 70 57 43 2d 3e 61 29 0a 20 20 20 20  m - pWC->a).    
2dd75 20 20 20 20 29 29 3b 0a 20 20 20 20 20 20 20 20      ));.        
2dd76 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 65 4f 70  if( pOrTerm->eOp
2dd77 65 72 61 74 6f 72 3d 3d 57 4f 5f 41 4e 44 20 29  erator==WO_AND )
2dd78 7b 0a 20 20 20 20 20 20 20 20 20 20 57 68 65 72  {.          Wher
2dd79 65 43 6c 61 75 73 65 20 2a 70 41 6e 64 57 43 20  eClause *pAndWC 
2dd7a 3d 20 26 70 4f 72 54 65 72 6d 2d 3e 75 2e 70 41  = &pOrTerm->u.pA
2dd7b 6e 64 49 6e 66 6f 2d 3e 77 63 3b 0a 20 20 20 20  ndInfo->wc;.    
2dd7c 20 20 20 20 20 20 62 65 73 74 49 6e 64 65 78 28        bestIndex(
2dd7d 70 50 61 72 73 65 2c 20 70 41 6e 64 57 43 2c 20  pParse, pAndWC, 
2dd7e 70 53 72 63 2c 20 6e 6f 74 52 65 61 64 79 2c 20  pSrc, notReady, 
2dd7f 30 2c 20 26 73 54 65 72 6d 43 6f 73 74 29 3b 0a  0, &sTermCost);.
2dd80 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66          }else if
2dd81 28 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74 43  ( pOrTerm->leftC
2dd82 75 72 73 6f 72 3d 3d 69 43 75 72 20 29 7b 0a 20  ursor==iCur ){. 
2dd83 20 20 20 20 20 20 20 20 20 57 68 65 72 65 43 6c           WhereCl
2dd84 61 75 73 65 20 74 65 6d 70 57 43 3b 0a 20 20 20  ause tempWC;.   
2dd85 20 20 20 20 20 20 20 74 65 6d 70 57 43 2e 70 50         tempWC.pP
2dd86 61 72 73 65 20 3d 20 70 57 43 2d 3e 70 50 61 72  arse = pWC->pPar
2dd87 73 65 3b 0a 20 20 20 20 20 20 20 20 20 20 74 65  se;.          te
2dd88 6d 70 57 43 2e 70 4d 61 73 6b 53 65 74 20 3d 20  mpWC.pMaskSet = 
2dd89 70 57 43 2d 3e 70 4d 61 73 6b 53 65 74 3b 0a 20  pWC->pMaskSet;. 
2dd8a 20 20 20 20 20 20 20 20 20 74 65 6d 70 57 43 2e           tempWC.
2dd8b 6f 70 20 3d 20 54 4b 5f 41 4e 44 3b 0a 20 20 20  op = TK_AND;.   
2dd8c 20 20 20 20 20 20 20 74 65 6d 70 57 43 2e 61 20         tempWC.a 
2dd8d 3d 20 70 4f 72 54 65 72 6d 3b 0a 20 20 20 20 20  = pOrTerm;.     
2dd8e 20 20 20 20 20 74 65 6d 70 57 43 2e 6e 54 65 72       tempWC.nTer
2dd8f 6d 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  m = 1;.         
2dd90 20 62 65 73 74 49 6e 64 65 78 28 70 50 61 72 73   bestIndex(pPars
2dd91 65 2c 20 26 74 65 6d 70 57 43 2c 20 70 53 72 63  e, &tempWC, pSrc
2dd92 2c 20 6e 6f 74 52 65 61 64 79 2c 20 30 2c 20 26  , notReady, 0, &
2dd93 73 54 65 72 6d 43 6f 73 74 29 3b 0a 20 20 20 20  sTermCost);.    
2dd94 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2dd95 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
2dd96 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2dd97 20 72 54 6f 74 61 6c 20 2b 3d 20 73 54 65 72 6d   rTotal += sTerm
2dd98 43 6f 73 74 2e 72 43 6f 73 74 3b 0a 20 20 20 20  Cost.rCost;.    
2dd99 20 20 20 20 6e 52 6f 77 20 2b 3d 20 73 54 65 72      nRow += sTer
2dd9a 6d 43 6f 73 74 2e 6e 52 6f 77 3b 0a 20 20 20 20  mCost.nRow;.    
2dd9b 20 20 20 20 75 73 65 64 20 7c 3d 20 73 54 65 72      used |= sTer
2dd9c 6d 43 6f 73 74 2e 75 73 65 64 3b 0a 20 20 20 20  mCost.used;.    
2dd9d 20 20 20 20 69 66 28 20 72 54 6f 74 61 6c 3e 3d      if( rTotal>=
2dd9e 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20 29 20 62  pCost->rCost ) b
2dd9f 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  reak;.      }.. 
2dda0 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65       /* If there
2dda1 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 20   is an ORDER BY 
2dda2 63 6c 61 75 73 65 2c 20 69 6e 63 72 65 61 73 65  clause, increase
2dda3 20 74 68 65 20 73 63 61 6e 20 63 6f 73 74 20 74   the scan cost t
2dda4 6f 20 61 63 63 6f 75 6e 74 20 0a 20 20 20 20 20  o account .     
2dda5 20 2a 2a 20 66 6f 72 20 74 68 65 20 63 6f 73 74   ** for the cost
2dda6 20 6f 66 20 74 68 65 20 73 6f 72 74 2e 20 2a 2f   of the sort. */
2dda7 0a 20 20 20 20 20 20 69 66 28 20 70 4f 72 64 65  .      if( pOrde
2dda8 72 42 79 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  rBy!=0 ){.      
2dda9 20 20 72 54 6f 74 61 6c 20 2b 3d 20 6e 52 6f 77    rTotal += nRow
2ddaa 2a 65 73 74 4c 6f 67 28 6e 52 6f 77 29 3b 0a 20  *estLog(nRow);. 
2ddab 20 20 20 20 20 20 20 57 48 45 52 45 54 52 41 43         WHERETRAC
2ddac 45 28 28 22 2e 2e 2e 20 73 6f 72 74 69 6e 67 20  E(("... sorting 
2ddad 69 6e 63 72 65 61 73 65 73 20 4f 52 20 63 6f 73  increases OR cos
2ddae 74 20 74 6f 20 25 2e 39 67 5c 6e 22 2c 20 72 54  t to %.9g\n", rT
2ddaf 6f 74 61 6c 29 29 3b 0a 20 20 20 20 20 20 7d 0a  otal));.      }.
2ddb0 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65  .      /* If the
2ddb1 20 63 6f 73 74 20 6f 66 20 73 63 61 6e 6e 69 6e   cost of scannin
2ddb2 67 20 75 73 69 6e 67 20 74 68 69 73 20 4f 52 20  g using this OR 
2ddb3 74 65 72 6d 20 66 6f 72 20 6f 70 74 69 6d 69 7a  term for optimiz
2ddb4 61 74 69 6f 6e 20 69 73 0a 20 20 20 20 20 20 2a  ation is.      *
2ddb5 2a 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20  * less than the 
2ddb6 63 75 72 72 65 6e 74 20 63 6f 73 74 20 73 74 6f  current cost sto
2ddb7 72 65 64 20 69 6e 20 70 43 6f 73 74 2c 20 72 65  red in pCost, re
2ddb8 70 6c 61 63 65 20 74 68 65 20 63 6f 6e 74 65 6e  place the conten
2ddb9 74 73 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 70  ts.      ** of p
2ddba 43 6f 73 74 2e 20 2a 2f 0a 20 20 20 20 20 20 57  Cost. */.      W
2ddbb 48 45 52 45 54 52 41 43 45 28 28 22 2e 2e 2e 20  HERETRACE(("... 
2ddbc 6d 75 6c 74 69 2d 69 6e 64 65 78 20 4f 52 20 63  multi-index OR c
2ddbd 6f 73 74 3d 25 2e 39 67 20 6e 72 6f 77 3d 25 2e  ost=%.9g nrow=%.
2ddbe 39 67 5c 6e 22 2c 20 72 54 6f 74 61 6c 2c 20 6e  9g\n", rTotal, n
2ddbf 52 6f 77 29 29 3b 0a 20 20 20 20 20 20 69 66 28  Row));.      if(
2ddc0 20 72 54 6f 74 61 6c 3c 70 43 6f 73 74 2d 3e 72   rTotal<pCost->r
2ddc1 43 6f 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Cost ){.        
2ddc2 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20 3d 20 72  pCost->rCost = r
2ddc3 54 6f 74 61 6c 3b 0a 20 20 20 20 20 20 20 20 70  Total;.        p
2ddc4 43 6f 73 74 2d 3e 6e 52 6f 77 20 3d 20 6e 52 6f  Cost->nRow = nRo
2ddc5 77 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 73 74  w;.        pCost
2ddc6 2d 3e 75 73 65 64 20 3d 20 75 73 65 64 3b 0a 20  ->used = used;. 
2ddc7 20 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 70 6c         pCost->pl
2ddc8 61 6e 2e 77 73 46 6c 61 67 73 20 3d 20 66 6c 61  an.wsFlags = fla
2ddc9 67 73 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 73  gs;.        pCos
2ddca 74 2d 3e 70 6c 61 6e 2e 75 2e 70 54 65 72 6d 20  t->plan.u.pTerm 
2ddcb 3d 20 70 54 65 72 6d 3b 0a 20 20 20 20 20 20 7d  = pTerm;.      }
2ddcc 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
2ddcd 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
2ddce 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e  _OR_OPTIMIZATION
2ddcf 20 2a 2f 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53   */.}..#ifndef S
2ddd0 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
2ddd1 41 4c 54 41 42 4c 45 0a 2f 2a 0a 2a 2a 20 41 6c  ALTABLE./*.** Al
2ddd2 6c 6f 63 61 74 65 20 61 6e 64 20 70 6f 70 75 6c  locate and popul
2ddd3 61 74 65 20 61 6e 20 73 71 6c 69 74 65 33 5f 69  ate an sqlite3_i
2ddd4 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74  ndex_info struct
2ddd5 75 72 65 2e 20 49 74 20 69 73 20 74 68 65 20 0a  ure. It is the .
2ddd6 2a 2a 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74  ** responsibilit
2ddd7 79 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 20  y of the caller 
2ddd8 74 6f 20 65 76 65 6e 74 75 61 6c 6c 79 20 72 65  to eventually re
2ddd9 6c 65 61 73 65 20 74 68 65 20 73 74 72 75 63 74  lease the struct
2ddda 75 72 65 0a 2a 2a 20 62 79 20 70 61 73 73 69 6e  ure.** by passin
2dddb 67 20 74 68 65 20 70 6f 69 6e 74 65 72 20 72 65  g the pointer re
2dddc 74 75 72 6e 65 64 20 62 79 20 74 68 69 73 20 66  turned by this f
2dddd 75 6e 63 74 69 6f 6e 20 74 6f 20 73 71 6c 69 74  unction to sqlit
2ddde 65 33 5f 66 72 65 65 28 29 2e 0a 2a 2f 0a 73 74  e3_free()..*/.st
2dddf 61 74 69 63 20 73 71 6c 69 74 65 33 5f 69 6e 64  atic sqlite3_ind
2dde0 65 78 5f 69 6e 66 6f 20 2a 61 6c 6c 6f 63 61 74  ex_info *allocat
2dde1 65 49 6e 64 65 78 49 6e 66 6f 28 0a 20 20 50 61  eIndexInfo(.  Pa
2dde2 72 73 65 20 2a 70 50 61 72 73 65 2c 20 0a 20 20  rse *pParse, .  
2dde3 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43  WhereClause *pWC
2dde4 2c 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69  ,.  struct SrcLi
2dde5 73 74 5f 69 74 65 6d 20 2a 70 53 72 63 2c 0a 20  st_item *pSrc,. 
2dde6 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65   ExprList *pOrde
2dde7 72 42 79 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20  rBy.){.  int i, 
2dde8 6a 3b 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b 0a  j;.  int nTerm;.
2dde9 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33    struct sqlite3
2ddea 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e  _index_constrain
2ddeb 74 20 2a 70 49 64 78 43 6f 6e 73 3b 0a 20 20 73  t *pIdxCons;.  s
2ddec 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e  truct sqlite3_in
2dded 64 65 78 5f 6f 72 64 65 72 62 79 20 2a 70 49 64  dex_orderby *pId
2ddee 78 4f 72 64 65 72 42 79 3b 0a 20 20 73 74 72 75  xOrderBy;.  stru
2ddef 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  ct sqlite3_index
2ddf0 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61 67  _constraint_usag
2ddf1 65 20 2a 70 55 73 61 67 65 3b 0a 20 20 57 68 65  e *pUsage;.  Whe
2ddf2 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 0a 20  reTerm *pTerm;. 
2ddf3 20 69 6e 74 20 6e 4f 72 64 65 72 42 79 3b 0a 20   int nOrderBy;. 
2ddf4 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
2ddf5 6e 66 6f 20 2a 70 49 64 78 49 6e 66 6f 3b 0a 0a  nfo *pIdxInfo;..
2ddf6 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22 52    WHERETRACE(("R
2ddf7 65 63 6f 6d 70 75 74 69 6e 67 20 69 6e 64 65 78  ecomputing index
2ddf8 20 69 6e 66 6f 20 66 6f 72 20 25 73 2e 2e 2e 5c   info for %s...\
2ddf9 6e 22 2c 20 70 53 72 63 2d 3e 70 54 61 62 2d 3e  n", pSrc->pTab->
2ddfa 7a 4e 61 6d 65 29 29 3b 0a 0a 20 20 2f 2a 20 43  zName));..  /* C
2ddfb 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20  ount the number 
2ddfc 6f 66 20 70 6f 73 73 69 62 6c 65 20 57 48 45 52  of possible WHER
2ddfd 45 20 63 6c 61 75 73 65 20 63 6f 6e 73 74 72 61  E clause constra
2ddfe 69 6e 74 73 20 72 65 66 65 72 72 69 6e 67 0a 20  ints referring. 
2ddff 20 2a 2a 20 74 6f 20 74 68 69 73 20 76 69 72 74   ** to this virt
2de00 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 66  ual table */.  f
2de01 6f 72 28 69 3d 6e 54 65 72 6d 3d 30 2c 20 70 54  or(i=nTerm=0, pT
2de02 65 72 6d 3d 70 57 43 2d 3e 61 3b 20 69 3c 70 57  erm=pWC->a; i<pW
2de03 43 2d 3e 6e 54 65 72 6d 3b 20 69 2b 2b 2c 20 70  C->nTerm; i++, p
2de04 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28  Term++){.    if(
2de05 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73   pTerm->leftCurs
2de06 6f 72 20 21 3d 20 70 53 72 63 2d 3e 69 43 75 72  or != pSrc->iCur
2de07 73 6f 72 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  sor ) continue;.
2de08 20 20 20 20 61 73 73 65 72 74 28 20 28 70 54 65      assert( (pTe
2de09 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 26 28 70  rm->eOperator&(p
2de0a 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 2d  Term->eOperator-
2de0b 31 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 74 65  1))==0 );.    te
2de0c 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 65  stcase( pTerm->e
2de0d 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 49 4e 20  Operator==WO_IN 
2de0e 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
2de0f 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f   pTerm->eOperato
2de10 72 3d 3d 57 4f 5f 49 53 4e 55 4c 4c 20 29 3b 0a  r==WO_ISNULL );.
2de11 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 65      if( pTerm->e
2de12 4f 70 65 72 61 74 6f 72 20 26 20 28 57 4f 5f 49  Operator & (WO_I
2de13 4e 7c 57 4f 5f 49 53 4e 55 4c 4c 29 20 29 20 63  N|WO_ISNULL) ) c
2de14 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 6e 54 65  ontinue;.    nTe
2de15 72 6d 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  rm++;.  }..  /* 
2de16 49 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  If the ORDER BY 
2de17 63 6c 61 75 73 65 20 63 6f 6e 74 61 69 6e 73 20  clause contains 
2de18 6f 6e 6c 79 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  only columns in 
2de19 74 68 65 20 63 75 72 72 65 6e 74 20 0a 20 20 2a  the current .  *
2de1a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  * virtual table 
2de1b 74 68 65 6e 20 61 6c 6c 6f 63 61 74 65 20 73 70  then allocate sp
2de1c 61 63 65 20 66 6f 72 20 74 68 65 20 61 4f 72 64  ace for the aOrd
2de1d 65 72 42 79 20 70 61 72 74 20 6f 66 0a 20 20 2a  erBy part of.  *
2de1e 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e  * the sqlite3_in
2de1f 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75  dex_info structu
2de20 72 65 2e 0a 20 20 2a 2f 0a 20 20 6e 4f 72 64 65  re..  */.  nOrde
2de21 72 42 79 20 3d 20 30 3b 0a 20 20 69 66 28 20 70  rBy = 0;.  if( p
2de22 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 66  OrderBy ){.    f
2de23 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 72 64 65 72  or(i=0; i<pOrder
2de24 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b  By->nExpr; i++){
2de25 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 78  .      Expr *pEx
2de26 70 72 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61  pr = pOrderBy->a
2de27 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20  [i].pExpr;.     
2de28 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d   if( pExpr->op!=
2de29 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 70 45 78  TK_COLUMN || pEx
2de2a 70 72 2d 3e 69 54 61 62 6c 65 21 3d 70 53 72 63  pr->iTable!=pSrc
2de2b 2d 3e 69 43 75 72 73 6f 72 20 29 20 62 72 65 61  ->iCursor ) brea
2de2c 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  k;.    }.    if(
2de2d 20 69 3d 3d 70 4f 72 64 65 72 42 79 2d 3e 6e 45   i==pOrderBy->nE
2de2e 78 70 72 20 29 7b 0a 20 20 20 20 20 20 6e 4f 72  xpr ){.      nOr
2de2f 64 65 72 42 79 20 3d 20 70 4f 72 64 65 72 42 79  derBy = pOrderBy
2de30 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 7d 0a 20  ->nExpr;.    }. 
2de31 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74   }..  /* Allocat
2de32 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e  e the sqlite3_in
2de33 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75  dex_info structu
2de34 72 65 0a 20 20 2a 2f 0a 20 20 70 49 64 78 49 6e  re.  */.  pIdxIn
2de35 66 6f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  fo = sqlite3DbMa
2de36 6c 6c 6f 63 5a 65 72 6f 28 70 50 61 72 73 65 2d  llocZero(pParse-
2de37 3e 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 49 64  >db, sizeof(*pId
2de38 78 49 6e 66 6f 29 0a 20 20 20 20 20 20 20 20 20  xInfo).         
2de39 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2de3a 20 20 2b 20 28 73 69 7a 65 6f 66 28 2a 70 49 64    + (sizeof(*pId
2de3b 78 43 6f 6e 73 29 20 2b 20 73 69 7a 65 6f 66 28  xCons) + sizeof(
2de3c 2a 70 55 73 61 67 65 29 29 2a 6e 54 65 72 6d 0a  *pUsage))*nTerm.
2de3d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2de3e 20 20 20 20 20 20 20 20 20 20 20 2b 20 73 69 7a             + siz
2de3f 65 6f 66 28 2a 70 49 64 78 4f 72 64 65 72 42 79  eof(*pIdxOrderBy
2de40 29 2a 6e 4f 72 64 65 72 42 79 20 29 3b 0a 20 20  )*nOrderBy );.  
2de41 69 66 28 20 70 49 64 78 49 6e 66 6f 3d 3d 30 20  if( pIdxInfo==0 
2de42 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
2de43 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
2de44 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b  out of memory");
2de45 0a 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29  .    /* (double)
2de46 30 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51 4c  0 In case of SQL
2de47 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e  ITE_OMIT_FLOATIN
2de48 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20  G_POINT... */.  
2de49 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
2de4a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65  .  /* Initialize
2de4b 20 74 68 65 20 73 74 72 75 63 74 75 72 65 2e 20   the structure. 
2de4c 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64   The sqlite3_ind
2de4d 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72  ex_info structur
2de4e 65 20 63 6f 6e 74 61 69 6e 73 0a 20 20 2a 2a 20  e contains.  ** 
2de4f 6d 61 6e 79 20 66 69 65 6c 64 73 20 74 68 61 74  many fields that
2de50 20 61 72 65 20 64 65 63 6c 61 72 65 64 20 22 63   are declared "c
2de51 6f 6e 73 74 22 20 74 6f 20 70 72 65 76 65 6e 74  onst" to prevent
2de52 20 78 42 65 73 74 49 6e 64 65 78 20 66 72 6f 6d   xBestIndex from
2de53 0a 20 20 2a 2a 20 63 68 61 6e 67 69 6e 67 20 74  .  ** changing t
2de54 68 65 6d 2e 20 20 57 65 20 68 61 76 65 20 74 6f  hem.  We have to
2de55 20 64 6f 20 73 6f 6d 65 20 66 75 6e 6b 79 20 63   do some funky c
2de56 61 73 74 69 6e 67 20 69 6e 20 6f 72 64 65 72 20  asting in order 
2de57 74 6f 0a 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69  to.  ** initiali
2de58 7a 65 20 74 68 6f 73 65 20 66 69 65 6c 64 73 2e  ze those fields.
2de59 0a 20 20 2a 2f 0a 20 20 70 49 64 78 43 6f 6e 73  .  */.  pIdxCons
2de5a 20 3d 20 28 73 74 72 75 63 74 20 73 71 6c 69 74   = (struct sqlit
2de5b 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61  e3_index_constra
2de5c 69 6e 74 2a 29 26 70 49 64 78 49 6e 66 6f 5b 31  int*)&pIdxInfo[1
2de5d 5d 3b 0a 20 20 70 49 64 78 4f 72 64 65 72 42 79  ];.  pIdxOrderBy
2de5e 20 3d 20 28 73 74 72 75 63 74 20 73 71 6c 69 74   = (struct sqlit
2de5f 65 33 5f 69 6e 64 65 78 5f 6f 72 64 65 72 62 79  e3_index_orderby
2de60 2a 29 26 70 49 64 78 43 6f 6e 73 5b 6e 54 65 72  *)&pIdxCons[nTer
2de61 6d 5d 3b 0a 20 20 70 55 73 61 67 65 20 3d 20 28  m];.  pUsage = (
2de62 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69  struct sqlite3_i
2de63 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f  ndex_constraint_
2de64 75 73 61 67 65 2a 29 26 70 49 64 78 4f 72 64 65  usage*)&pIdxOrde
2de65 72 42 79 5b 6e 4f 72 64 65 72 42 79 5d 3b 0a 20  rBy[nOrderBy];. 
2de66 20 2a 28 69 6e 74 2a 29 26 70 49 64 78 49 6e 66   *(int*)&pIdxInf
2de67 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 20 3d  o->nConstraint =
2de68 20 6e 54 65 72 6d 3b 0a 20 20 2a 28 69 6e 74 2a   nTerm;.  *(int*
2de69 29 26 70 49 64 78 49 6e 66 6f 2d 3e 6e 4f 72 64  )&pIdxInfo->nOrd
2de6a 65 72 42 79 20 3d 20 6e 4f 72 64 65 72 42 79 3b  erBy = nOrderBy;
2de6b 0a 20 20 2a 28 73 74 72 75 63 74 20 73 71 6c 69  .  *(struct sqli
2de6c 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72  te3_index_constr
2de6d 61 69 6e 74 2a 2a 29 26 70 49 64 78 49 6e 66 6f  aint**)&pIdxInfo
2de6e 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20  ->aConstraint = 
2de6f 70 49 64 78 43 6f 6e 73 3b 0a 20 20 2a 28 73 74  pIdxCons;.  *(st
2de70 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64  ruct sqlite3_ind
2de71 65 78 5f 6f 72 64 65 72 62 79 2a 2a 29 26 70 49  ex_orderby**)&pI
2de72 64 78 49 6e 66 6f 2d 3e 61 4f 72 64 65 72 42 79  dxInfo->aOrderBy
2de73 20 3d 20 70 49 64 78 4f 72 64 65 72 42 79 3b 0a   = pIdxOrderBy;.
2de74 20 20 2a 28 73 74 72 75 63 74 20 73 71 6c 69 74    *(struct sqlit
2de75 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61  e3_index_constra
2de76 69 6e 74 5f 75 73 61 67 65 2a 2a 29 26 70 49 64  int_usage**)&pId
2de77 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69  xInfo->aConstrai
2de78 6e 74 55 73 61 67 65 20 3d 0a 20 20 20 20 20 20  ntUsage =.      
2de79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2de7a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2de7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2de7c 20 20 20 20 20 20 20 20 20 20 20 20 20 70 55 73               pUs
2de7d 61 67 65 3b 0a 0a 20 20 66 6f 72 28 69 3d 6a 3d  age;..  for(i=j=
2de7e 30 2c 20 70 54 65 72 6d 3d 70 57 43 2d 3e 61 3b  0, pTerm=pWC->a;
2de7f 20 69 3c 70 57 43 2d 3e 6e 54 65 72 6d 3b 20 69   i<pWC->nTerm; i
2de80 2b 2b 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20  ++, pTerm++){.  
2de81 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 6c 65 66    if( pTerm->lef
2de82 74 43 75 72 73 6f 72 20 21 3d 20 70 53 72 63 2d  tCursor != pSrc-
2de83 3e 69 43 75 72 73 6f 72 20 29 20 63 6f 6e 74 69  >iCursor ) conti
2de84 6e 75 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28  nue;.    assert(
2de85 20 28 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74   (pTerm->eOperat
2de86 6f 72 26 28 70 54 65 72 6d 2d 3e 65 4f 70 65 72  or&(pTerm->eOper
2de87 61 74 6f 72 2d 31 29 29 3d 3d 30 20 29 3b 0a 20  ator-1))==0 );. 
2de88 20 20 20 74 65 73 74 63 61 73 65 28 20 70 54 65     testcase( pTe
2de89 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57  rm->eOperator==W
2de8a 4f 5f 49 4e 20 29 3b 0a 20 20 20 20 74 65 73 74  O_IN );.    test
2de8b 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 65 4f 70  case( pTerm->eOp
2de8c 65 72 61 74 6f 72 3d 3d 57 4f 5f 49 53 4e 55 4c  erator==WO_ISNUL
2de8d 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 70 54 65  L );.    if( pTe
2de8e 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20  rm->eOperator & 
2de8f 28 57 4f 5f 49 4e 7c 57 4f 5f 49 53 4e 55 4c 4c  (WO_IN|WO_ISNULL
2de90 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  ) ) continue;.  
2de91 20 20 70 49 64 78 43 6f 6e 73 5b 6a 5d 2e 69 43    pIdxCons[j].iC
2de92 6f 6c 75 6d 6e 20 3d 20 70 54 65 72 6d 2d 3e 75  olumn = pTerm->u
2de93 2e 6c 65 66 74 43 6f 6c 75 6d 6e 3b 0a 20 20 20  .leftColumn;.   
2de94 20 70 49 64 78 43 6f 6e 73 5b 6a 5d 2e 69 54 65   pIdxCons[j].iTe
2de95 72 6d 4f 66 66 73 65 74 20 3d 20 69 3b 0a 20 20  rmOffset = i;.  
2de96 20 20 70 49 64 78 43 6f 6e 73 5b 6a 5d 2e 6f 70    pIdxCons[j].op
2de97 20 3d 20 28 75 38 29 70 54 65 72 6d 2d 3e 65 4f   = (u8)pTerm->eO
2de98 70 65 72 61 74 6f 72 3b 0a 20 20 20 20 2f 2a 20  perator;.    /* 
2de99 54 68 65 20 64 69 72 65 63 74 20 61 73 73 69 67  The direct assig
2de9a 6e 6d 65 6e 74 20 69 6e 20 74 68 65 20 70 72 65  nment in the pre
2de9b 76 69 6f 75 73 20 6c 69 6e 65 20 69 73 20 70 6f  vious line is po
2de9c 73 73 69 62 6c 65 20 6f 6e 6c 79 20 62 65 63 61  ssible only beca
2de9d 75 73 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 57  use.    ** the W
2de9e 4f 5f 20 61 6e 64 20 53 51 4c 49 54 45 5f 49 4e  O_ and SQLITE_IN
2de9f 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 20  DEX_CONSTRAINT_ 
2dea0 63 6f 64 65 73 20 61 72 65 20 69 64 65 6e 74 69  codes are identi
2dea1 63 61 6c 2e 20 20 54 68 65 0a 20 20 20 20 2a 2a  cal.  The.    **
2dea2 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72   following asser
2dea3 74 73 20 76 65 72 69 66 79 20 74 68 69 73 20 66  ts verify this f
2dea4 61 63 74 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65  act. */.    asse
2dea5 72 74 28 20 57 4f 5f 45 51 3d 3d 53 51 4c 49 54  rt( WO_EQ==SQLIT
2dea6 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49  E_INDEX_CONSTRAI
2dea7 4e 54 5f 45 51 20 29 3b 0a 20 20 20 20 61 73 73  NT_EQ );.    ass
2dea8 65 72 74 28 20 57 4f 5f 4c 54 3d 3d 53 51 4c 49  ert( WO_LT==SQLI
2dea9 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41  TE_INDEX_CONSTRA
2deaa 49 4e 54 5f 4c 54 20 29 3b 0a 20 20 20 20 61 73  INT_LT );.    as
2deab 73 65 72 74 28 20 57 4f 5f 4c 45 3d 3d 53 51 4c  sert( WO_LE==SQL
2deac 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52  ITE_INDEX_CONSTR
2dead 41 49 4e 54 5f 4c 45 20 29 3b 0a 20 20 20 20 61  AINT_LE );.    a
2deae 73 73 65 72 74 28 20 57 4f 5f 47 54 3d 3d 53 51  ssert( WO_GT==SQ
2deaf 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54  LITE_INDEX_CONST
2deb0 52 41 49 4e 54 5f 47 54 20 29 3b 0a 20 20 20 20  RAINT_GT );.    
2deb1 61 73 73 65 72 74 28 20 57 4f 5f 47 45 3d 3d 53  assert( WO_GE==S
2deb2 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53  QLITE_INDEX_CONS
2deb3 54 52 41 49 4e 54 5f 47 45 20 29 3b 0a 20 20 20  TRAINT_GE );.   
2deb4 20 61 73 73 65 72 74 28 20 57 4f 5f 4d 41 54 43   assert( WO_MATC
2deb5 48 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f  H==SQLITE_INDEX_
2deb6 43 4f 4e 53 54 52 41 49 4e 54 5f 4d 41 54 43 48  CONSTRAINT_MATCH
2deb7 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
2deb8 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
2deb9 20 26 20 28 57 4f 5f 45 51 7c 57 4f 5f 4c 54 7c   & (WO_EQ|WO_LT|
2deba 57 4f 5f 4c 45 7c 57 4f 5f 47 54 7c 57 4f 5f 47  WO_LE|WO_GT|WO_G
2debb 45 7c 57 4f 5f 4d 41 54 43 48 29 20 29 3b 0a 20  E|WO_MATCH) );. 
2debc 20 20 20 6a 2b 2b 3b 0a 20 20 7d 0a 20 20 66 6f     j++;.  }.  fo
2debd 72 28 69 3d 30 3b 20 69 3c 6e 4f 72 64 65 72 42  r(i=0; i<nOrderB
2debe 79 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 45 78 70  y; i++){.    Exp
2debf 72 20 2a 70 45 78 70 72 20 3d 20 70 4f 72 64 65  r *pExpr = pOrde
2dec0 72 42 79 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b  rBy->a[i].pExpr;
2dec1 0a 20 20 20 20 70 49 64 78 4f 72 64 65 72 42 79  .    pIdxOrderBy
2dec2 5b 69 5d 2e 69 43 6f 6c 75 6d 6e 20 3d 20 70 45  [i].iColumn = pE
2dec3 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20  xpr->iColumn;.  
2dec4 20 20 70 49 64 78 4f 72 64 65 72 42 79 5b 69 5d    pIdxOrderBy[i]
2dec5 2e 64 65 73 63 20 3d 20 70 4f 72 64 65 72 42 79  .desc = pOrderBy
2dec6 2d 3e 61 5b 69 5d 2e 73 6f 72 74 4f 72 64 65 72  ->a[i].sortOrder
2dec7 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
2dec8 70 49 64 78 49 6e 66 6f 3b 0a 7d 0a 0a 2f 2a 0a  pIdxInfo;.}../*.
2dec9 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 6f 62 6a  ** The table obj
2deca 65 63 74 20 72 65 66 65 72 65 6e 63 65 20 70 61  ect reference pa
2decb 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f  ssed as the seco
2decc 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  nd argument to t
2decd 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  his function.** 
2dece 6d 75 73 74 20 72 65 70 72 65 73 65 6e 74 20 61  must represent a
2decf 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20   virtual table. 
2ded0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e  This function in
2ded1 76 6f 6b 65 73 20 74 68 65 20 78 42 65 73 74 49  vokes the xBestI
2ded2 6e 64 65 78 28 29 0a 2a 2a 20 6d 65 74 68 6f 64  ndex().** method
2ded3 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20   of the virtual 
2ded4 74 61 62 6c 65 20 77 69 74 68 20 74 68 65 20 73  table with the s
2ded5 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
2ded6 6f 20 70 6f 69 6e 74 65 72 20 70 61 73 73 65 64  o pointer passed
2ded7 0a 2a 2a 20 61 73 20 74 68 65 20 61 72 67 75 6d  .** as the argum
2ded8 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  ent..**.** If an
2ded9 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 70   error occurs, p
2deda 50 61 72 73 65 20 69 73 20 70 6f 70 75 6c 61 74  Parse is populat
2dedb 65 64 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72  ed with an error
2dedc 20 6d 65 73 73 61 67 65 20 61 6e 64 20 61 0a 2a   message and a.*
2dedd 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65  * non-zero value
2dede 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74   is returned. Ot
2dedf 68 65 72 77 69 73 65 2c 20 30 20 69 73 20 72 65  herwise, 0 is re
2dee0 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20 6f  turned and the o
2dee1 75 74 70 75 74 0a 2a 2a 20 70 61 72 74 20 6f 66  utput.** part of
2dee2 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64   the sqlite3_ind
2dee3 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72  ex_info structur
2dee4 65 20 69 73 20 6c 65 66 74 20 70 6f 70 75 6c 61  e is left popula
2dee5 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68  ted..**.** Wheth
2dee6 65 72 20 6f 72 20 6e 6f 74 20 61 6e 20 65 72 72  er or not an err
2dee7 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20  or is returned, 
2dee8 69 74 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e  it is the respon
2dee9 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65 0a  sibility of the.
2deea 2a 2a 20 63 61 6c 6c 65 72 20 74 6f 20 65 76 65  ** caller to eve
2deeb 6e 74 75 61 6c 6c 79 20 66 72 65 65 20 70 2d 3e  ntually free p->
2deec 69 64 78 53 74 72 20 69 66 20 70 2d 3e 6e 65 65  idxStr if p->nee
2deed 64 54 6f 46 72 65 65 49 64 78 53 74 72 20 69 6e  dToFreeIdxStr in
2deee 64 69 63 61 74 65 73 0a 2a 2a 20 74 68 61 74 20  dicates.** that 
2deef 74 68 69 73 20 69 73 20 72 65 71 75 69 72 65 64  this is required
2def0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2def1 76 74 61 62 42 65 73 74 49 6e 64 65 78 28 50 61  vtabBestIndex(Pa
2def2 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62  rse *pParse, Tab
2def3 6c 65 20 2a 70 54 61 62 2c 20 73 71 6c 69 74 65  le *pTab, sqlite
2def4 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 29  3_index_info *p)
2def5 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62  {.  sqlite3_vtab
2def6 20 2a 70 56 74 61 62 20 3d 20 73 71 6c 69 74 65   *pVtab = sqlite
2def7 33 47 65 74 56 54 61 62 6c 65 28 70 50 61 72 73  3GetVTable(pPars
2def8 65 2d 3e 64 62 2c 20 70 54 61 62 29 2d 3e 70 56  e->db, pTab)->pV
2def9 74 61 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  tab;.  int i;.  
2defa 69 6e 74 20 72 63 3b 0a 0a 20 20 28 76 6f 69 64  int rc;..  (void
2defb 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66  )sqlite3SafetyOf
2defc 66 28 70 50 61 72 73 65 2d 3e 64 62 29 3b 0a 20  f(pParse->db);. 
2defd 20 57 48 45 52 45 54 52 41 43 45 28 28 22 78 42   WHERETRACE(("xB
2defe 65 73 74 49 6e 64 65 78 20 66 6f 72 20 25 73 5c  estIndex for %s\
2deff 6e 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  n", pTab->zName)
2df00 29 3b 0a 20 20 54 52 41 43 45 5f 49 44 58 5f 49  );.  TRACE_IDX_I
2df01 4e 50 55 54 53 28 70 29 3b 0a 20 20 72 63 20 3d  NPUTS(p);.  rc =
2df02 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d   pVtab->pModule-
2df03 3e 78 42 65 73 74 49 6e 64 65 78 28 70 56 74 61  >xBestIndex(pVta
2df04 62 2c 20 70 29 3b 0a 20 20 54 52 41 43 45 5f 49  b, p);.  TRACE_I
2df05 44 58 5f 4f 55 54 50 55 54 53 28 70 29 3b 0a 20  DX_OUTPUTS(p);. 
2df06 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61   (void)sqlite3Sa
2df07 66 65 74 79 4f 6e 28 70 50 61 72 73 65 2d 3e 64  fetyOn(pParse->d
2df08 62 29 3b 0a 0a 20 20 69 66 28 20 72 63 21 3d 53  b);..  if( rc!=S
2df09 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
2df0a 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e  if( rc==SQLITE_N
2df0b 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 70 50  OMEM ){.      pP
2df0c 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  arse->db->malloc
2df0d 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20  Failed = 1;.    
2df0e 7d 65 6c 73 65 20 69 66 28 20 21 70 56 74 61 62  }else if( !pVtab
2df0f 2d 3e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20  ->zErrMsg ){.   
2df10 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2df11 73 67 28 70 50 61 72 73 65 2c 20 22 25 73 22 2c  sg(pParse, "%s",
2df12 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 72   sqlite3ErrStr(r
2df13 63 29 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  c));.    }else{.
2df14 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
2df15 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25  orMsg(pParse, "%
2df16 73 22 2c 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d  s", pVtab->zErrM
2df17 73 67 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  sg);.    }.  }. 
2df18 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70   sqlite3DbFree(p
2df19 50 61 72 73 65 2d 3e 64 62 2c 20 70 56 74 61 62  Parse->db, pVtab
2df1a 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 56  ->zErrMsg);.  pV
2df1b 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30  tab->zErrMsg = 0
2df1c 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  ;..  for(i=0; i<
2df1d 70 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20  p->nConstraint; 
2df1e 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 21 70  i++){.    if( !p
2df1f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d  ->aConstraint[i]
2df20 2e 75 73 61 62 6c 65 20 26 26 20 70 2d 3e 61 43  .usable && p->aC
2df21 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b 69  onstraintUsage[i
2df22 5d 2e 61 72 67 76 49 6e 64 65 78 3e 30 20 29 7b  ].argvIndex>0 ){
2df23 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
2df24 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a  rorMsg(pParse, .
2df25 20 20 20 20 20 20 20 20 20 20 22 74 61 62 6c 65            "table
2df26 20 25 73 3a 20 78 42 65 73 74 49 6e 64 65 78 20   %s: xBestIndex 
2df27 72 65 74 75 72 6e 65 64 20 61 6e 20 69 6e 76 61  returned an inva
2df28 6c 69 64 20 70 6c 61 6e 22 2c 20 70 54 61 62 2d  lid plan", pTab-
2df29 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20  >zName);.    }. 
2df2a 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 50 61   }..  return pPa
2df2b 72 73 65 2d 3e 6e 45 72 72 3b 0a 7d 0a 0a 0a 2f  rse->nErr;.}.../
2df2c 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65  *.** Compute the
2df2d 20 62 65 73 74 20 69 6e 64 65 78 20 66 6f 72 20   best index for 
2df2e 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  a virtual table.
2df2f 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 65 73 74 20  .**.** The best 
2df30 69 6e 64 65 78 20 69 73 20 63 6f 6d 70 75 74 65  index is compute
2df31 64 20 62 79 20 74 68 65 20 78 42 65 73 74 49 6e  d by the xBestIn
2df32 64 65 78 20 6d 65 74 68 6f 64 20 6f 66 20 74 68  dex method of th
2df33 65 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62  e virtual.** tab
2df34 6c 65 20 6d 6f 64 75 6c 65 2e 20 20 54 68 69 73  le module.  This
2df35 20 72 6f 75 74 69 6e 65 20 69 73 20 72 65 61 6c   routine is real
2df36 6c 79 20 6a 75 73 74 20 61 20 77 72 61 70 70 65  ly just a wrappe
2df37 72 20 74 68 61 74 20 73 65 74 73 20 75 70 0a 2a  r that sets up.*
2df38 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e  * the sqlite3_in
2df39 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75  dex_info structu
2df3a 72 65 20 74 68 61 74 20 69 73 20 75 73 65 64 20  re that is used 
2df3b 74 6f 20 63 6f 6d 6d 75 6e 69 63 61 74 65 20 77  to communicate w
2df3c 69 74 68 0a 2a 2a 20 78 42 65 73 74 49 6e 64 65  ith.** xBestInde
2df3d 78 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 20 6a 6f  x..**.** In a jo
2df3e 69 6e 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  in, this routine
2df3f 20 6d 69 67 68 74 20 62 65 20 63 61 6c 6c 65 64   might be called
2df40 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20   multiple times 
2df41 66 6f 72 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20  for the.** same 
2df42 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20  virtual table.  
2df43 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  The sqlite3_inde
2df44 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65  x_info structure
2df45 20 69 73 20 63 72 65 61 74 65 64 0a 2a 2a 20 61   is created.** a
2df46 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 6f  nd initialized o
2df47 6e 20 74 68 65 20 66 69 72 73 74 20 69 6e 76 6f  n the first invo
2df48 63 61 74 69 6f 6e 20 61 6e 64 20 72 65 75 73 65  cation and reuse
2df49 64 20 6f 6e 20 61 6c 6c 20 73 75 62 73 65 71 75  d on all subsequ
2df4a 65 6e 74 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f  ent.** invocatio
2df4b 6e 73 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33  ns.  The sqlite3
2df4c 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75  _index_info stru
2df4d 63 74 75 72 65 20 69 73 20 61 6c 73 6f 20 75 73  cture is also us
2df4e 65 64 20 77 68 65 6e 0a 2a 2a 20 63 6f 64 65 20  ed when.** code 
2df4f 69 73 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20  is generated to 
2df50 61 63 63 65 73 73 20 74 68 65 20 76 69 72 74 75  access the virtu
2df51 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 65 20 77  al table.  The w
2df52 68 65 72 65 49 6e 66 6f 44 65 6c 65 74 65 28 29  hereInfoDelete()
2df53 20 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 61 6b   .** routine tak
2df54 65 73 20 63 61 72 65 20 6f 66 20 66 72 65 65 69  es care of freei
2df55 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69  ng the sqlite3_i
2df56 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74  ndex_info struct
2df57 75 72 65 20 61 66 74 65 72 0a 2a 2a 20 65 76 65  ure after.** eve
2df58 72 79 62 6f 64 79 20 68 61 73 20 66 69 6e 69 73  rybody has finis
2df59 68 65 64 20 77 69 74 68 20 69 74 2e 0a 2a 2f 0a  hed with it..*/.
2df5a 73 74 61 74 69 63 20 76 6f 69 64 20 62 65 73 74  static void best
2df5b 56 69 72 74 75 61 6c 49 6e 64 65 78 28 0a 20 20  VirtualIndex(.  
2df5c 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2df5d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2df5e 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63  /* The parsing c
2df5f 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68 65 72  ontext */.  Wher
2df60 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 20 20  eClause *pWC,   
2df61 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2df62 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
2df63 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c  */.  struct SrcL
2df64 69 73 74 5f 69 74 65 6d 20 2a 70 53 72 63 2c 20  ist_item *pSrc, 
2df65 20 20 20 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d       /* The FROM
2df66 20 63 6c 61 75 73 65 20 74 65 72 6d 20 74 6f 20   clause term to 
2df67 73 65 61 72 63 68 20 2a 2f 0a 20 20 42 69 74 6d  search */.  Bitm
2df68 61 73 6b 20 6e 6f 74 52 65 61 64 79 2c 20 20 20  ask notReady,   
2df69 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
2df6a 61 73 6b 20 6f 66 20 63 75 72 73 6f 72 73 20 74  ask of cursors t
2df6b 68 61 74 20 61 72 65 20 6e 6f 74 20 61 76 61 69  hat are not avai
2df6c 6c 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72 4c  lable */.  ExprL
2df6d 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20  ist *pOrderBy,  
2df6e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2df6f 65 20 6f 72 64 65 72 20 62 79 20 63 6c 61 75 73  e order by claus
2df70 65 20 2a 2f 0a 20 20 57 68 65 72 65 43 6f 73 74  e */.  WhereCost
2df71 20 2a 70 43 6f 73 74 2c 20 20 20 20 20 20 20 20   *pCost,        
2df72 20 20 20 20 20 20 20 2f 2a 20 4c 6f 77 65 73 74         /* Lowest
2df73 20 63 6f 73 74 20 71 75 65 72 79 20 70 6c 61 6e   cost query plan
2df74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e   */.  sqlite3_in
2df75 64 65 78 5f 69 6e 66 6f 20 2a 2a 70 70 49 64 78  dex_info **ppIdx
2df76 49 6e 66 6f 20 20 2f 2a 20 49 6e 64 65 78 20 69  Info  /* Index i
2df77 6e 66 6f 72 6d 61 74 69 6f 6e 20 70 61 73 73 65  nformation passe
2df78 64 20 74 6f 20 78 42 65 73 74 49 6e 64 65 78 20  d to xBestIndex 
2df79 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70  */.){.  Table *p
2df7a 54 61 62 20 3d 20 70 53 72 63 2d 3e 70 54 61 62  Tab = pSrc->pTab
2df7b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  ;.  sqlite3_inde
2df7c 78 5f 69 6e 66 6f 20 2a 70 49 64 78 49 6e 66 6f  x_info *pIdxInfo
2df7d 3b 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69 74  ;.  struct sqlit
2df7e 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61  e3_index_constra
2df7f 69 6e 74 20 2a 70 49 64 78 43 6f 6e 73 3b 0a 20  int *pIdxCons;. 
2df80 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
2df81 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74  index_constraint
2df82 5f 75 73 61 67 65 20 2a 70 55 73 61 67 65 3b 0a  _usage *pUsage;.
2df83 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65    WhereTerm *pTe
2df84 72 6d 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a  rm;.  int i, j;.
2df85 20 20 69 6e 74 20 6e 4f 72 64 65 72 42 79 3b 0a    int nOrderBy;.
2df86 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20  .  /* Make sure 
2df87 77 73 46 6c 61 67 73 20 69 73 20 69 6e 69 74 69  wsFlags is initi
2df88 61 6c 69 7a 65 64 20 74 6f 20 73 6f 6d 65 20 73  alized to some s
2df89 61 6e 65 20 76 61 6c 75 65 2e 20 4f 74 68 65 72  ane value. Other
2df8a 77 69 73 65 2c 20 69 66 20 74 68 65 20 0a 20 20  wise, if the .  
2df8b 2a 2a 20 6d 61 6c 6c 6f 63 20 69 6e 20 61 6c 6c  ** malloc in all
2df8c 6f 63 61 74 65 49 6e 64 65 78 49 6e 66 6f 28 29  ocateIndexInfo()
2df8d 20 66 61 69 6c 73 20 61 6e 64 20 74 68 69 73 20   fails and this 
2df8e 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
2df8f 20 6c 65 61 76 69 6e 67 0a 20 20 2a 2a 20 77 73   leaving.  ** ws
2df90 46 6c 61 67 73 20 69 6e 20 61 6e 20 75 6e 69 6e  Flags in an unin
2df91 69 74 69 61 6c 69 7a 65 64 20 73 74 61 74 65 2c  itialized state,
2df92 20 74 68 65 20 63 61 6c 6c 65 72 20 6d 61 79 20   the caller may 
2df93 62 65 68 61 76 65 20 75 6e 70 72 65 64 69 63 74  behave unpredict
2df94 61 62 6c 79 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d  ably..  */.  mem
2df95 73 65 74 28 70 43 6f 73 74 2c 20 30 2c 20 73 69  set(pCost, 0, si
2df96 7a 65 6f 66 28 2a 70 43 6f 73 74 29 29 3b 0a 20  zeof(*pCost));. 
2df97 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73 46   pCost->plan.wsF
2df98 6c 61 67 73 20 3d 20 57 48 45 52 45 5f 56 49 52  lags = WHERE_VIR
2df99 54 55 41 4c 54 41 42 4c 45 3b 0a 0a 20 20 2f 2a  TUALTABLE;..  /*
2df9a 20 49 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   If the sqlite3_
2df9b 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63  index_info struc
2df9c 74 75 72 65 20 68 61 73 20 6e 6f 74 20 62 65 65  ture has not bee
2df9d 6e 20 70 72 65 76 69 6f 75 73 6c 79 0a 20 20 2a  n previously.  *
2df9e 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20  * allocated and 
2df9f 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20 74 68 65  initialized, the
2dfa0 6e 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 69  n allocate and i
2dfa1 6e 69 74 69 61 6c 69 7a 65 20 69 74 20 6e 6f 77  nitialize it now
2dfa2 2e 0a 20 20 2a 2f 0a 20 20 70 49 64 78 49 6e 66  ..  */.  pIdxInf
2dfa3 6f 20 3d 20 2a 70 70 49 64 78 49 6e 66 6f 3b 0a  o = *ppIdxInfo;.
2dfa4 20 20 69 66 28 20 70 49 64 78 49 6e 66 6f 3d 3d    if( pIdxInfo==
2dfa5 30 20 29 7b 0a 20 20 20 20 2a 70 70 49 64 78 49  0 ){.    *ppIdxI
2dfa6 6e 66 6f 20 3d 20 70 49 64 78 49 6e 66 6f 20 3d  nfo = pIdxInfo =
2dfa7 20 61 6c 6c 6f 63 61 74 65 49 6e 64 65 78 49 6e   allocateIndexIn
2dfa8 66 6f 28 70 50 61 72 73 65 2c 20 70 57 43 2c 20  fo(pParse, pWC, 
2dfa9 70 53 72 63 2c 20 70 4f 72 64 65 72 42 79 29 3b  pSrc, pOrderBy);
2dfaa 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 64 78 49  .  }.  if( pIdxI
2dfab 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  nfo==0 ){.    re
2dfac 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  turn;.  }..  /* 
2dfad 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 74  At this point, t
2dfae 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  he sqlite3_index
2dfaf 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  _info structure 
2dfb0 74 68 61 74 20 70 49 64 78 49 6e 66 6f 20 70 6f  that pIdxInfo po
2dfb1 69 6e 74 73 0a 20 20 2a 2a 20 74 6f 20 77 69 6c  ints.  ** to wil
2dfb2 6c 20 68 61 76 65 20 62 65 65 6e 20 69 6e 69 74  l have been init
2dfb3 69 61 6c 69 7a 65 64 2c 20 65 69 74 68 65 72 20  ialized, either 
2dfb4 64 75 72 69 6e 67 20 74 68 65 20 63 75 72 72 65  during the curre
2dfb5 6e 74 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 72  nt invocation or
2dfb6 0a 20 20 2a 2a 20 64 75 72 69 6e 67 20 73 6f 6d  .  ** during som
2dfb7 65 20 70 72 69 6f 72 20 69 6e 76 6f 63 61 74 69  e prior invocati
2dfb8 6f 6e 2e 20 20 4e 6f 77 20 77 65 20 6a 75 73 74  on.  Now we just
2dfb9 20 68 61 76 65 20 74 6f 20 63 75 73 74 6f 6d 69   have to customi
2dfba 7a 65 20 74 68 65 0a 20 20 2a 2a 20 64 65 74 61  ze the.  ** deta
2dfbb 69 6c 73 20 6f 66 20 70 49 64 78 49 6e 66 6f 20  ils of pIdxInfo 
2dfbc 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20  for the current 
2dfbd 69 6e 76 6f 63 61 74 69 6f 6e 20 61 6e 64 20 70  invocation and p
2dfbe 61 73 73 20 69 74 20 74 6f 0a 20 20 2a 2a 20 78  ass it to.  ** x
2dfbf 42 65 73 74 49 6e 64 65 78 2e 0a 20 20 2a 2f 0a  BestIndex..  */.
2dfc0 0a 20 20 2f 2a 20 54 68 65 20 6d 6f 64 75 6c 65  .  /* The module
2dfc1 20 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 64 65   name must be de
2dfc2 66 69 6e 65 64 2e 20 41 6c 73 6f 2c 20 62 79 20  fined. Also, by 
2dfc3 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 72 65  this point there
2dfc4 20 6d 75 73 74 0a 20 20 2a 2a 20 62 65 20 61 20   must.  ** be a 
2dfc5 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 71  pointer to an sq
2dfc6 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 63  lite3_vtab struc
2dfc7 74 75 72 65 2e 20 4f 74 68 65 72 77 69 73 65 0a  ture. Otherwise.
2dfc8 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 69 65 77    ** sqlite3View
2dfc9 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 29  GetColumnNames()
2dfca 20 77 6f 75 6c 64 20 68 61 76 65 20 70 69 63 6b   would have pick
2dfcb 65 64 20 75 70 20 74 68 65 20 65 72 72 6f 72 2e  ed up the error.
2dfcc 20 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28   .  */.  assert(
2dfcd 20 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41   pTab->azModuleA
2dfce 72 67 20 26 26 20 70 54 61 62 2d 3e 61 7a 4d 6f  rg && pTab->azMo
2dfcf 64 75 6c 65 41 72 67 5b 30 5d 20 29 3b 0a 20 20  duleArg[0] );.  
2dfd0 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 47  assert( sqlite3G
2dfd1 65 74 56 54 61 62 6c 65 28 70 50 61 72 73 65 2d  etVTable(pParse-
2dfd2 3e 64 62 2c 20 70 54 61 62 29 20 29 3b 0a 0a 20  >db, pTab) );.. 
2dfd3 20 2f 2a 20 53 65 74 20 74 68 65 20 61 43 6f 6e   /* Set the aCon
2dfd4 73 74 72 61 69 6e 74 5b 5d 2e 75 73 61 62 6c 65  straint[].usable
2dfd5 20 66 69 65 6c 64 73 20 61 6e 64 20 69 6e 69 74   fields and init
2dfd6 69 61 6c 69 7a 65 20 61 6c 6c 20 0a 20 20 2a 2a  ialize all .  **
2dfd7 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c 65   output variable
2dfd8 73 20 74 6f 20 7a 65 72 6f 2e 0a 20 20 2a 2a 0a  s to zero..  **.
2dfd9 20 20 2a 2a 20 61 43 6f 6e 73 74 72 61 69 6e 74    ** aConstraint
2dfda 5b 5d 2e 75 73 61 62 6c 65 20 69 73 20 74 72 75  [].usable is tru
2dfdb 65 20 66 6f 72 20 63 6f 6e 73 74 72 61 69 6e 74  e for constraint
2dfdc 73 20 77 68 65 72 65 20 74 68 65 20 72 69 67 68  s where the righ
2dfdd 74 2d 68 61 6e 64 0a 20 20 2a 2a 20 73 69 64 65  t-hand.  ** side
2dfde 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 6c 79 20 72   contains only r
2dfdf 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 61 62  eferences to tab
2dfe0 6c 65 73 20 74 6f 20 74 68 65 20 6c 65 66 74 20  les to the left 
2dfe1 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 0a 20  of the current. 
2dfe2 20 2a 2a 20 74 61 62 6c 65 2e 20 20 49 6e 20 6f   ** table.  In o
2dfe3 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 74  ther words, if t
2dfe4 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73  he constraint is
2dfe5 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 20 20   of the form:.  
2dfe6 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20  **.  **         
2dfe7 20 20 63 6f 6c 75 6d 6e 20 3d 20 65 78 70 72 0a    column = expr.
2dfe8 20 20 2a 2a 0a 20 20 2a 2a 20 61 6e 64 20 77 65    **.  ** and we
2dfe9 20 61 72 65 20 65 76 61 6c 75 61 74 69 6e 67 20   are evaluating 
2dfea 61 20 6a 6f 69 6e 2c 20 74 68 65 6e 20 74 68 65  a join, then the
2dfeb 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f 6e 20 63   constraint on c
2dfec 6f 6c 75 6d 6e 20 69 73 20 0a 20 20 2a 2a 20 6f  olumn is .  ** o
2dfed 6e 6c 79 20 76 61 6c 69 64 20 69 66 20 61 6c 6c  nly valid if all
2dfee 20 74 61 62 6c 65 73 20 72 65 66 65 72 65 6e 63   tables referenc
2dfef 65 64 20 69 6e 20 65 78 70 72 20 6f 63 63 75 72  ed in expr occur
2dff0 20 74 6f 20 74 68 65 20 6c 65 66 74 0a 20 20 2a   to the left.  *
2dff1 2a 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63  * of the table c
2dff2 6f 6e 74 61 69 6e 69 6e 67 20 63 6f 6c 75 6d 6e  ontaining column
2dff3 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  ..  **.  ** The 
2dff4 61 43 6f 6e 73 74 72 61 69 6e 74 73 5b 5d 20 61  aConstraints[] a
2dff5 72 72 61 79 20 63 6f 6e 74 61 69 6e 73 20 65 6e  rray contains en
2dff6 74 72 69 65 73 20 66 6f 72 20 61 6c 6c 20 63 6f  tries for all co
2dff7 6e 73 74 72 61 69 6e 74 73 0a 20 20 2a 2a 20 6f  nstraints.  ** o
2dff8 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 61  n the current ta
2dff9 62 6c 65 2e 20 20 54 68 61 74 20 77 61 79 20 77  ble.  That way w
2dffa 65 20 6f 6e 6c 79 20 68 61 76 65 20 74 6f 20 63  e only have to c
2dffb 6f 6d 70 75 74 65 20 69 74 20 6f 6e 63 65 0a 20  ompute it once. 
2dffc 20 2a 2a 20 65 76 65 6e 20 74 68 6f 75 67 68 20   ** even though 
2dffd 77 65 20 6d 69 67 68 74 20 74 72 79 20 74 6f 20  we might try to 
2dffe 70 69 63 6b 20 74 68 65 20 62 65 73 74 20 69 6e  pick the best in
2dfff 64 65 78 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d  dex multiple tim
2e000 65 73 2e 0a 20 20 2a 2a 20 46 6f 72 20 65 61 63  es..  ** For eac
2e001 68 20 61 74 74 65 6d 70 74 20 61 74 20 70 69 63  h attempt at pic
2e002 6b 69 6e 67 20 61 6e 20 69 6e 64 65 78 2c 20 74  king an index, t
2e003 68 65 20 6f 72 64 65 72 20 6f 66 20 74 61 62 6c  he order of tabl
2e004 65 73 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 6a  es in the.  ** j
2e005 6f 69 6e 20 6d 69 67 68 74 20 62 65 20 64 69 66  oin might be dif
2e006 66 65 72 65 6e 74 20 73 6f 20 77 65 20 68 61 76  ferent so we hav
2e007 65 20 74 6f 20 72 65 63 6f 6d 70 75 74 65 20 74  e to recompute t
2e008 68 65 20 75 73 61 62 6c 65 20 66 6c 61 67 0a 20  he usable flag. 
2e009 20 2a 2a 20 65 61 63 68 20 74 69 6d 65 2e 0a 20   ** each time.. 
2e00a 20 2a 2f 0a 20 20 70 49 64 78 43 6f 6e 73 20 3d   */.  pIdxCons =
2e00b 20 2a 28 73 74 72 75 63 74 20 73 71 6c 69 74 65   *(struct sqlite
2e00c 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69  3_index_constrai
2e00d 6e 74 2a 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e  nt**)&pIdxInfo->
2e00e 61 43 6f 6e 73 74 72 61 69 6e 74 3b 0a 20 20 70  aConstraint;.  p
2e00f 55 73 61 67 65 20 3d 20 70 49 64 78 49 6e 66 6f  Usage = pIdxInfo
2e010 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61  ->aConstraintUsa
2e011 67 65 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  ge;.  for(i=0; i
2e012 3c 70 49 64 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73  <pIdxInfo->nCons
2e013 74 72 61 69 6e 74 3b 20 69 2b 2b 2c 20 70 49 64  traint; i++, pId
2e014 78 43 6f 6e 73 2b 2b 29 7b 0a 20 20 20 20 6a 20  xCons++){.    j 
2e015 3d 20 70 49 64 78 43 6f 6e 73 2d 3e 69 54 65 72  = pIdxCons->iTer
2e016 6d 4f 66 66 73 65 74 3b 0a 20 20 20 20 70 54 65  mOffset;.    pTe
2e017 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 6a 5d 3b  rm = &pWC->a[j];
2e018 0a 20 20 20 20 70 49 64 78 43 6f 6e 73 2d 3e 75  .    pIdxCons->u
2e019 73 61 62 6c 65 20 3d 20 28 70 54 65 72 6d 2d 3e  sable = (pTerm->
2e01a 70 72 65 72 65 71 52 69 67 68 74 26 6e 6f 74 52  prereqRight&notR
2e01b 65 61 64 79 29 20 3f 20 30 20 3a 20 31 3b 0a 20  eady) ? 0 : 1;. 
2e01c 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 55 73 61   }.  memset(pUsa
2e01d 67 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 55  ge, 0, sizeof(pU
2e01e 73 61 67 65 5b 30 5d 29 2a 70 49 64 78 49 6e 66  sage[0])*pIdxInf
2e01f 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 29 3b  o->nConstraint);
2e020 0a 20 20 69 66 28 20 70 49 64 78 49 6e 66 6f 2d  .  if( pIdxInfo-
2e021 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 74  >needToFreeIdxSt
2e022 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  r ){.    sqlite3
2e023 5f 66 72 65 65 28 70 49 64 78 49 6e 66 6f 2d 3e  _free(pIdxInfo->
2e024 69 64 78 53 74 72 29 3b 0a 20 20 7d 0a 20 20 70  idxStr);.  }.  p
2e025 49 64 78 49 6e 66 6f 2d 3e 69 64 78 53 74 72 20  IdxInfo->idxStr 
2e026 3d 20 30 3b 0a 20 20 70 49 64 78 49 6e 66 6f 2d  = 0;.  pIdxInfo-
2e027 3e 69 64 78 4e 75 6d 20 3d 20 30 3b 0a 20 20 70  >idxNum = 0;.  p
2e028 49 64 78 49 6e 66 6f 2d 3e 6e 65 65 64 54 6f 46  IdxInfo->needToF
2e029 72 65 65 49 64 78 53 74 72 20 3d 20 30 3b 0a 20  reeIdxStr = 0;. 
2e02a 20 70 49 64 78 49 6e 66 6f 2d 3e 6f 72 64 65 72   pIdxInfo->order
2e02b 42 79 43 6f 6e 73 75 6d 65 64 20 3d 20 30 3b 0a  ByConsumed = 0;.
2e02c 20 20 2f 2a 20 28 28 64 6f 75 62 6c 65 29 32 29    /* ((double)2)
2e02d 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49   In case of SQLI
2e02e 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
2e02f 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 70  _POINT... */.  p
2e030 49 64 78 49 6e 66 6f 2d 3e 65 73 74 69 6d 61 74  IdxInfo->estimat
2e031 65 64 43 6f 73 74 20 3d 20 53 51 4c 49 54 45 5f  edCost = SQLITE_
2e032 42 49 47 5f 44 42 4c 20 2f 20 28 28 64 6f 75 62  BIG_DBL / ((doub
2e033 6c 65 29 32 29 3b 0a 20 20 6e 4f 72 64 65 72 42  le)2);.  nOrderB
2e034 79 20 3d 20 70 49 64 78 49 6e 66 6f 2d 3e 6e 4f  y = pIdxInfo->nO
2e035 72 64 65 72 42 79 3b 0a 20 20 69 66 28 20 21 70  rderBy;.  if( !p
2e036 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 70  OrderBy ){.    p
2e037 49 64 78 49 6e 66 6f 2d 3e 6e 4f 72 64 65 72 42  IdxInfo->nOrderB
2e038 79 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 69 66  y = 0;.  }..  if
2e039 28 20 76 74 61 62 42 65 73 74 49 6e 64 65 78 28  ( vtabBestIndex(
2e03a 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 70 49  pParse, pTab, pI
2e03b 64 78 49 6e 66 6f 29 20 29 7b 0a 20 20 20 20 72  dxInfo) ){.    r
2e03c 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 70 49  eturn;.  }..  pI
2e03d 64 78 43 6f 6e 73 20 3d 20 2a 28 73 74 72 75 63  dxCons = *(struc
2e03e 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  t sqlite3_index_
2e03f 63 6f 6e 73 74 72 61 69 6e 74 2a 2a 29 26 70 49  constraint**)&pI
2e040 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61  dxInfo->aConstra
2e041 69 6e 74 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  int;.  for(i=0; 
2e042 69 3c 70 49 64 78 49 6e 66 6f 2d 3e 6e 43 6f 6e  i<pIdxInfo->nCon
2e043 73 74 72 61 69 6e 74 3b 20 69 2b 2b 29 7b 0a 20  straint; i++){. 
2e044 20 20 20 69 66 28 20 70 55 73 61 67 65 5b 69 5d     if( pUsage[i]
2e045 2e 61 72 67 76 49 6e 64 65 78 3e 30 20 29 7b 0a  .argvIndex>0 ){.
2e046 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 75 73 65        pCost->use
2e047 64 20 7c 3d 20 70 57 43 2d 3e 61 5b 70 49 64 78  d |= pWC->a[pIdx
2e048 43 6f 6e 73 5b 69 5d 2e 69 54 65 72 6d 4f 66 66  Cons[i].iTermOff
2e049 73 65 74 5d 2e 70 72 65 72 65 71 52 69 67 68 74  set].prereqRight
2e04a 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
2e04b 2a 20 54 68 65 20 63 6f 73 74 20 69 73 20 6e 6f  * The cost is no
2e04c 74 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 65 20  t allowed to be 
2e04d 6c 61 72 67 65 72 20 74 68 61 6e 20 53 51 4c 49  larger than SQLI
2e04e 54 45 5f 42 49 47 5f 44 42 4c 20 28 74 68 65 0a  TE_BIG_DBL (the.
2e04f 20 20 2a 2a 20 69 6e 69 74 61 6c 20 76 61 6c 75    ** inital valu
2e050 65 20 6f 66 20 6c 6f 77 65 73 74 43 6f 73 74 20  e of lowestCost 
2e051 69 6e 20 74 68 69 73 20 6c 6f 6f 70 2e 20 49 66  in this loop. If
2e052 20 69 74 20 69 73 2c 20 74 68 65 6e 20 74 68 65   it is, then the
2e053 0a 20 20 2a 2a 20 28 63 6f 73 74 3c 6c 6f 77 65  .  ** (cost<lowe
2e054 73 74 43 6f 73 74 29 20 74 65 73 74 20 62 65 6c  stCost) test bel
2e055 6f 77 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65  ow will never be
2e056 20 74 72 75 65 2e 0a 20 20 2a 2a 20 0a 20 20 2a   true..  ** .  *
2e057 2a 20 55 73 65 20 22 28 64 6f 75 62 6c 65 29 32  * Use "(double)2
2e058 22 20 69 6e 73 74 65 61 64 20 6f 66 20 22 32 2e  " instead of "2.
2e059 30 22 20 69 6e 20 63 61 73 65 20 4f 4d 49 54 5f  0" in case OMIT_
2e05a 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 20 0a  FLOATING_POINT .
2e05b 20 20 2a 2a 20 69 73 20 64 65 66 69 6e 65 64 2e    ** is defined.
2e05c 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 53 51 4c  .  */.  if( (SQL
2e05d 49 54 45 5f 42 49 47 5f 44 42 4c 2f 28 28 64 6f  ITE_BIG_DBL/((do
2e05e 75 62 6c 65 29 32 29 29 3c 70 49 64 78 49 6e 66  uble)2))<pIdxInf
2e05f 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f 73 74  o->estimatedCost
2e060 20 29 7b 0a 20 20 20 20 70 43 6f 73 74 2d 3e 72   ){.    pCost->r
2e061 43 6f 73 74 20 3d 20 28 53 51 4c 49 54 45 5f 42  Cost = (SQLITE_B
2e062 49 47 5f 44 42 4c 2f 28 28 64 6f 75 62 6c 65 29  IG_DBL/((double)
2e063 32 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  2));.  }else{.  
2e064 20 20 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20 3d    pCost->rCost =
2e065 20 70 49 64 78 49 6e 66 6f 2d 3e 65 73 74 69 6d   pIdxInfo->estim
2e066 61 74 65 64 43 6f 73 74 3b 0a 20 20 7d 0a 20 20  atedCost;.  }.  
2e067 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 75 2e 70 56  pCost->plan.u.pV
2e068 74 61 62 49 64 78 20 3d 20 70 49 64 78 49 6e 66  tabIdx = pIdxInf
2e069 6f 3b 0a 20 20 69 66 28 20 70 49 64 78 49 6e 66  o;.  if( pIdxInf
2e06a 6f 2d 3e 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d  o->orderByConsum
2e06b 65 64 20 29 7b 0a 20 20 20 20 70 43 6f 73 74 2d  ed ){.    pCost-
2e06c 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 7c 3d  >plan.wsFlags |=
2e06d 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59 3b 0a   WHERE_ORDERBY;.
2e06e 20 20 7d 0a 20 20 70 43 6f 73 74 2d 3e 70 6c 61    }.  pCost->pla
2e06f 6e 2e 6e 45 71 20 3d 20 30 3b 0a 20 20 70 49 64  n.nEq = 0;.  pId
2e070 78 49 6e 66 6f 2d 3e 6e 4f 72 64 65 72 42 79 20  xInfo->nOrderBy 
2e071 3d 20 6e 4f 72 64 65 72 42 79 3b 0a 0a 20 20 2f  = nOrderBy;..  /
2e072 2a 20 54 72 79 20 74 6f 20 66 69 6e 64 20 61 20  * Try to find a 
2e073 6d 6f 72 65 20 65 66 66 69 63 69 65 6e 74 20 61  more efficient a
2e074 63 63 65 73 73 20 70 61 74 74 65 72 6e 20 62 79  ccess pattern by
2e075 20 75 73 69 6e 67 20 6d 75 6c 74 69 70 6c 65 20   using multiple 
2e076 69 6e 64 65 78 65 73 0a 20 20 2a 2a 20 74 6f 20  indexes.  ** to 
2e077 6f 70 74 69 6d 69 7a 65 20 61 6e 20 4f 52 20 65  optimize an OR e
2e078 78 70 72 65 73 73 69 6f 6e 20 77 69 74 68 69 6e  xpression within
2e079 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2e07a 65 2e 20 0a 20 20 2a 2f 0a 20 20 62 65 73 74 4f  e. .  */.  bestO
2e07b 72 43 6c 61 75 73 65 49 6e 64 65 78 28 70 50 61  rClauseIndex(pPa
2e07c 72 73 65 2c 20 70 57 43 2c 20 70 53 72 63 2c 20  rse, pWC, pSrc, 
2e07d 6e 6f 74 52 65 61 64 79 2c 20 70 4f 72 64 65 72  notReady, pOrder
2e07e 42 79 2c 20 70 43 6f 73 74 29 3b 0a 7d 0a 23 65  By, pCost);.}.#e
2e07f 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
2e080 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
2e081 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d   */../*.** Argum
2e082 65 6e 74 20 70 49 64 78 20 69 73 20 61 20 70 6f  ent pIdx is a po
2e083 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 64 65  inter to an inde
2e084 78 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  x structure that
2e085 20 68 61 73 20 61 6e 20 61 72 72 61 79 20 6f 66   has an array of
2e086 0a 2a 2a 20 53 51 4c 49 54 45 5f 49 4e 44 45 58  .** SQLITE_INDEX
2e087 5f 53 41 4d 50 4c 45 53 20 65 76 65 6e 6c 79 20  _SAMPLES evenly 
2e088 73 70 61 63 65 64 20 73 61 6d 70 6c 65 73 20 6f  spaced samples o
2e089 66 20 74 68 65 20 66 69 72 73 74 20 69 6e 64 65  f the first inde
2e08a 78 65 64 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 73 74  xed column.** st
2e08b 6f 72 65 64 20 69 6e 20 49 6e 64 65 78 2e 61 53  ored in Index.aS
2e08c 61 6d 70 6c 65 2e 20 54 68 65 20 64 6f 6d 61 69  ample. The domai
2e08d 6e 20 6f 66 20 76 61 6c 75 65 73 20 73 74 6f 72  n of values stor
2e08e 65 64 20 69 6e 20 73 61 69 64 20 63 6f 6c 75 6d  ed in said colum
2e08f 6e 0a 2a 2a 20 6d 61 79 20 62 65 20 74 68 6f 75  n.** may be thou
2e090 67 68 74 20 6f 66 20 61 73 20 64 69 76 69 64 65  ght of as divide
2e091 64 20 69 6e 74 6f 20 28 53 51 4c 49 54 45 5f 49  d into (SQLITE_I
2e092 4e 44 45 58 5f 53 41 4d 50 4c 45 53 2b 31 29 20  NDEX_SAMPLES+1) 
2e093 72 65 67 69 6f 6e 73 2e 0a 2a 2a 20 52 65 67 69  regions..** Regi
2e094 6f 6e 20 30 20 63 6f 6e 74 61 69 6e 73 20 61 6c  on 0 contains al
2e095 6c 20 76 61 6c 75 65 73 20 73 6d 61 6c 6c 65 72  l values smaller
2e096 20 74 68 61 6e 20 74 68 65 20 66 69 72 73 74 20   than the first 
2e097 73 61 6d 70 6c 65 20 76 61 6c 75 65 2e 20 52 65  sample value. Re
2e098 67 69 6f 6e 0a 2a 2a 20 31 20 63 6f 6e 74 61 69  gion.** 1 contai
2e099 6e 73 20 76 61 6c 75 65 73 20 6c 61 72 67 65 72  ns values larger
2e09a 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
2e09b 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  o the value of t
2e09c 68 65 20 66 69 72 73 74 20 73 61 6d 70 6c 65 2c  he first sample,
2e09d 0a 2a 2a 20 62 75 74 20 73 6d 61 6c 6c 65 72 20  .** but smaller 
2e09e 74 68 61 6e 20 74 68 65 20 76 61 6c 75 65 20 6f  than the value o
2e09f 66 20 74 68 65 20 73 65 63 6f 6e 64 2e 20 41 6e  f the second. An
2e0a0 64 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49  d so on..**.** I
2e0a1 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74 68  f successful, th
2e0a2 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 65 74 65  is function dete
2e0a3 72 6d 69 6e 65 73 20 77 68 69 63 68 20 6f 66 20  rmines which of 
2e0a4 74 68 65 20 72 65 67 69 6f 6e 73 20 76 61 6c 75  the regions valu
2e0a5 65 20 0a 2a 2a 20 70 56 61 6c 20 6c 69 65 73 20  e .** pVal lies 
2e0a6 69 6e 2c 20 73 65 74 73 20 2a 70 69 52 65 67 69  in, sets *piRegi
2e0a7 6f 6e 20 74 6f 20 74 68 65 20 72 65 67 69 6f 6e  on to the region
2e0a8 20 69 6e 64 65 78 20 28 61 20 76 61 6c 75 65 20   index (a value 
2e0a9 62 65 74 77 65 65 6e 20 30 0a 2a 2a 20 61 6e 64  between 0.** and
2e0aa 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41   SQLITE_INDEX_SA
2e0ab 4d 50 4c 45 53 2b 31 2c 20 69 6e 63 6c 75 73 69  MPLES+1, inclusi
2e0ac 76 65 29 20 61 6e 64 20 72 65 74 75 72 6e 73 20  ve) and returns 
2e0ad 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 20 4f 72  SQLITE_OK..** Or
2e0ae 2c 20 69 66 20 61 6e 20 4f 4f 4d 20 6f 63 63 75  , if an OOM occu
2e0af 72 73 20 77 68 69 6c 65 20 63 6f 6e 76 65 72 74  rs while convert
2e0b0 69 6e 67 20 74 65 78 74 20 76 61 6c 75 65 73 20  ing text values 
2e0b1 62 65 74 77 65 65 6e 20 65 6e 63 6f 64 69 6e 67  between encoding
2e0b2 73 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d  s,.** SQLITE_NOM
2e0b3 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  EM is returned a
2e0b4 6e 64 20 2a 70 69 52 65 67 69 6f 6e 20 69 73 20  nd *piRegion is 
2e0b5 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69  undefined..*/.#i
2e0b6 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
2e0b7 4c 45 5f 53 54 41 54 32 0a 73 74 61 74 69 63 20  LE_STAT2.static 
2e0b8 69 6e 74 20 77 68 65 72 65 52 61 6e 67 65 52 65  int whereRangeRe
2e0b9 67 69 6f 6e 28 0a 20 20 50 61 72 73 65 20 2a 70  gion(.  Parse *p
2e0ba 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
2e0bb 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
2e0bc 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
2e0bd 49 6e 64 65 78 20 2a 70 49 64 78 2c 20 20 20 20  Index *pIdx,    
2e0be 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
2e0bf 6e 64 65 78 20 74 6f 20 63 6f 6e 73 69 64 65 72  ndex to consider
2e0c0 20 64 6f 6d 61 69 6e 20 6f 66 20 2a 2f 0a 20 20   domain of */.  
2e0c1 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
2e0c2 56 61 6c 2c 20 20 20 20 20 20 20 20 2f 2a 20 56  Val,        /* V
2e0c3 61 6c 75 65 20 74 6f 20 63 6f 6e 73 69 64 65 72  alue to consider
2e0c4 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 52 65 67   */.  int *piReg
2e0c5 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20  ion             
2e0c6 20 20 2f 2a 20 4f 55 54 3a 20 52 65 67 69 6f 6e    /* OUT: Region
2e0c7 20 6f 66 20 64 6f 6d 61 69 6e 20 69 6e 20 77 68   of domain in wh
2e0c8 69 63 68 20 76 61 6c 75 65 20 6c 69 65 73 20 2a  ich value lies *
2e0c9 2f 0a 29 7b 0a 20 20 69 66 28 20 41 4c 57 41 59  /.){.  if( ALWAY
2e0ca 53 28 70 56 61 6c 29 20 29 7b 0a 20 20 20 20 49  S(pVal) ){.    I
2e0cb 6e 64 65 78 53 61 6d 70 6c 65 20 2a 61 53 61 6d  ndexSample *aSam
2e0cc 70 6c 65 20 3d 20 70 49 64 78 2d 3e 61 53 61 6d  ple = pIdx->aSam
2e0cd 70 6c 65 3b 0a 20 20 20 20 69 6e 74 20 69 20 3d  ple;.    int i =
2e0ce 20 30 3b 0a 20 20 20 20 69 6e 74 20 65 54 79 70   0;.    int eTyp
2e0cf 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  e = sqlite3_valu
2e0d0 65 5f 74 79 70 65 28 70 56 61 6c 29 3b 0a 0a 20  e_type(pVal);.. 
2e0d1 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 53 51     if( eType==SQ
2e0d2 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 7c 7c 20  LITE_INTEGER || 
2e0d3 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 46 4c  eType==SQLITE_FL
2e0d4 4f 41 54 20 29 7b 0a 20 20 20 20 20 20 64 6f 75  OAT ){.      dou
2e0d5 62 6c 65 20 72 20 3d 20 73 71 6c 69 74 65 33 5f  ble r = sqlite3_
2e0d6 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 70 56 61  value_double(pVa
2e0d7 6c 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d  l);.      for(i=
2e0d8 30 3b 20 69 3c 53 51 4c 49 54 45 5f 49 4e 44 45  0; i<SQLITE_INDE
2e0d9 58 5f 53 41 4d 50 4c 45 53 3b 20 69 2b 2b 29 7b  X_SAMPLES; i++){
2e0da 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 53 61  .        if( aSa
2e0db 6d 70 6c 65 5b 69 5d 2e 65 54 79 70 65 3d 3d 53  mple[i].eType==S
2e0dc 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 63 6f 6e  QLITE_NULL ) con
2e0dd 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 69  tinue;.        i
2e0de 66 28 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 65 54  f( aSample[i].eT
2e0df 79 70 65 3e 3d 53 51 4c 49 54 45 5f 54 45 58 54  ype>=SQLITE_TEXT
2e0e0 20 7c 7c 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 75   || aSample[i].u
2e0e1 2e 72 3e 72 20 29 20 62 72 65 61 6b 3b 0a 20 20  .r>r ) break;.  
2e0e2 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b      }.    }else{
2e0e3 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 20   .      sqlite3 
2e0e4 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
2e0e5 3b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20  ;.      CollSeq 
2e0e6 2a 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 63 6f  *pColl;.      co
2e0e7 6e 73 74 20 75 38 20 2a 7a 3b 0a 20 20 20 20 20  nst u8 *z;.     
2e0e8 20 69 6e 74 20 6e 3b 0a 0a 20 20 20 20 20 20 2f   int n;..      /
2e0e9 2a 20 70 56 61 6c 20 63 6f 6d 65 73 20 66 72 6f  * pVal comes fro
2e0ea 6d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72  m sqlite3ValueFr
2e0eb 6f 6d 45 78 70 72 28 29 20 73 6f 20 74 68 65 20  omExpr() so the 
2e0ec 74 79 70 65 20 63 61 6e 6e 6f 74 20 62 65 20 4e  type cannot be N
2e0ed 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 61 73 73  ULL */.      ass
2e0ee 65 72 74 28 20 65 54 79 70 65 3d 3d 53 51 4c 49  ert( eType==SQLI
2e0ef 54 45 5f 54 45 58 54 20 7c 7c 20 65 54 79 70 65  TE_TEXT || eType
2e0f0 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29 3b  ==SQLITE_BLOB );
2e0f1 0a 0a 20 20 20 20 20 20 69 66 28 20 65 54 79 70  ..      if( eTyp
2e0f2 65 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29  e==SQLITE_BLOB )
2e0f3 7b 0a 20 20 20 20 20 20 20 20 7a 20 3d 20 28 63  {.        z = (c
2e0f4 6f 6e 73 74 20 75 38 20 2a 29 73 71 6c 69 74 65  onst u8 *)sqlite
2e0f5 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 70 56 61  3_value_blob(pVa
2e0f6 6c 29 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c  l);.        pCol
2e0f7 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c  l = db->pDfltCol
2e0f8 6c 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  l;.        asser
2e0f9 74 28 20 70 43 6f 6c 6c 2d 3e 65 6e 63 3d 3d 53  t( pColl->enc==S
2e0fa 51 4c 49 54 45 5f 55 54 46 38 20 29 3b 0a 20 20  QLITE_UTF8 );.  
2e0fb 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2e0fc 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74     pColl = sqlit
2e0fd 65 33 47 65 74 43 6f 6c 6c 53 65 71 28 64 62 2c  e3GetCollSeq(db,
2e0fe 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c   SQLITE_UTF8, 0,
2e0ff 20 2a 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 29 3b   *pIdx->azColl);
2e100 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 43 6f  .        if( pCo
2e101 6c 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ll==0 ){.       
2e102 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2e103 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73  sg(pParse, "no s
2e104 75 63 68 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  uch collation se
2e105 71 75 65 6e 63 65 3a 20 25 73 22 2c 0a 20 20 20  quence: %s",.   
2e106 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e107 20 20 20 20 20 20 20 2a 70 49 64 78 2d 3e 61 7a         *pIdx->az
2e108 43 6f 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20 20  Coll);.         
2e109 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
2e10a 52 52 4f 52 3b 0a 20 20 20 20 20 20 20 20 7d 0a  RROR;.        }.
2e10b 20 20 20 20 20 20 20 20 7a 20 3d 20 28 63 6f 6e          z = (con
2e10c 73 74 20 75 38 20 2a 29 73 71 6c 69 74 65 33 56  st u8 *)sqlite3V
2e10d 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20 70  alueText(pVal, p
2e10e 43 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20 20  Coll->enc);.    
2e10f 20 20 20 20 69 66 28 20 21 7a 20 29 7b 0a 20 20      if( !z ){.  
2e110 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
2e111 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
2e112 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61       }.        a
2e113 73 73 65 72 74 28 20 7a 20 26 26 20 70 43 6f 6c  ssert( z && pCol
2e114 6c 20 26 26 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70  l && pColl->xCmp
2e115 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   );.      }.    
2e116 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 56 61 6c    n = sqlite3Val
2e117 75 65 42 79 74 65 73 28 70 56 61 6c 2c 20 70 43  ueBytes(pVal, pC
2e118 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 0a 20 20 20 20  oll->enc);..    
2e119 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 53 51 4c    for(i=0; i<SQL
2e11a 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45  ITE_INDEX_SAMPLE
2e11b 53 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  S; i++){.       
2e11c 20 69 6e 74 20 72 3b 0a 20 20 20 20 20 20 20 20   int r;.        
2e11d 69 6e 74 20 65 53 61 6d 70 6c 65 74 79 70 65 20  int eSampletype 
2e11e 3d 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 65 54 79  = aSample[i].eTy
2e11f 70 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  pe;.        if( 
2e120 65 53 61 6d 70 6c 65 74 79 70 65 3d 3d 53 51 4c  eSampletype==SQL
2e121 49 54 45 5f 4e 55 4c 4c 20 7c 7c 20 65 53 61 6d  ITE_NULL || eSam
2e122 70 6c 65 74 79 70 65 3c 65 54 79 70 65 20 29 20  pletype<eType ) 
2e123 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
2e124 20 20 69 66 28 20 28 65 53 61 6d 70 6c 65 74 79    if( (eSamplety
2e125 70 65 21 3d 65 54 79 70 65 29 20 29 20 62 72 65  pe!=eType) ) bre
2e126 61 6b 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ak;.        if( 
2e127 70 43 6f 6c 6c 2d 3e 65 6e 63 3d 3d 53 51 4c 49  pColl->enc==SQLI
2e128 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 20  TE_UTF8 ){.     
2e129 20 20 20 20 20 72 20 3d 20 70 43 6f 6c 6c 2d 3e       r = pColl->
2e12a 78 43 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55 73 65  xCmp(pColl->pUse
2e12b 72 2c 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 6e 42  r, aSample[i].nB
2e12c 79 74 65 2c 20 61 53 61 6d 70 6c 65 5b 69 5d 2e  yte, aSample[i].
2e12d 75 2e 7a 2c 20 6e 2c 20 7a 29 3b 0a 20 20 20 20  u.z, n, z);.    
2e12e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2e12f 20 20 20 20 20 69 6e 74 20 6e 53 61 6d 70 6c 65       int nSample
2e130 3b 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 72  ;.          char
2e131 20 2a 7a 53 61 6d 70 6c 65 20 3d 20 73 71 6c 69   *zSample = sqli
2e132 74 65 33 55 74 66 38 74 6f 31 36 28 0a 20 20 20  te3Utf8to16(.   
2e133 20 20 20 20 20 20 20 20 20 20 20 64 62 2c 20 70             db, p
2e134 43 6f 6c 6c 2d 3e 65 6e 63 2c 20 61 53 61 6d 70  Coll->enc, aSamp
2e135 6c 65 5b 69 5d 2e 75 2e 7a 2c 20 61 53 61 6d 70  le[i].u.z, aSamp
2e136 6c 65 5b 69 5d 2e 6e 42 79 74 65 2c 20 26 6e 53  le[i].nByte, &nS
2e137 61 6d 70 6c 65 0a 20 20 20 20 20 20 20 20 20 20  ample.          
2e138 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
2e139 20 21 7a 53 61 6d 70 6c 65 20 29 7b 0a 20 20 20   !zSample ){.   
2e13a 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
2e13b 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2e13c 64 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  d );.           
2e13d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
2e13e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20 20  OMEM;.          
2e13f 7d 0a 20 20 20 20 20 20 20 20 20 20 72 20 3d 20  }.          r = 
2e140 70 43 6f 6c 6c 2d 3e 78 43 6d 70 28 70 43 6f 6c  pColl->xCmp(pCol
2e141 6c 2d 3e 70 55 73 65 72 2c 20 6e 53 61 6d 70 6c  l->pUser, nSampl
2e142 65 2c 20 7a 53 61 6d 70 6c 65 2c 20 6e 2c 20 7a  e, zSample, n, z
2e143 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
2e144 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
2e145 53 61 6d 70 6c 65 29 3b 0a 20 20 20 20 20 20 20  Sample);.       
2e146 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72   }.        if( r
2e147 3e 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  >0 ) break;.    
2e148 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61    }.    }..    a
2e149 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69  ssert( i>=0 && i
2e14a 3c 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53  <=SQLITE_INDEX_S
2e14b 41 4d 50 4c 45 53 20 29 3b 0a 20 20 20 20 2a 70  AMPLES );.    *p
2e14c 69 52 65 67 69 6f 6e 20 3d 20 69 3b 0a 20 20 7d  iRegion = i;.  }
2e14d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
2e14e 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 20 20  _OK;.}.#endif   
2e14f 2f 2a 20 23 69 66 64 65 66 20 53 51 4c 49 54 45  /* #ifdef SQLITE
2e150 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 20 2a 2f  _ENABLE_STAT2 */
2e151 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
2e152 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f  ction is used to
2e153 20 65 73 74 69 6d 61 74 65 20 74 68 65 20 6e 75   estimate the nu
2e154 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 74 68 61  mber of rows tha
2e155 74 20 77 69 6c 6c 20 62 65 20 76 69 73 69 74 65  t will be visite
2e156 64 0a 2a 2a 20 62 79 20 73 63 61 6e 6e 69 6e 67  d.** by scanning
2e157 20 61 6e 20 69 6e 64 65 78 20 66 6f 72 20 61 20   an index for a 
2e158 72 61 6e 67 65 20 6f 66 20 76 61 6c 75 65 73 2e  range of values.
2e159 20 54 68 65 20 72 61 6e 67 65 20 6d 61 79 20 68   The range may h
2e15a 61 76 65 20 61 6e 20 75 70 70 65 72 0a 2a 2a 20  ave an upper.** 
2e15b 62 6f 75 6e 64 2c 20 61 20 6c 6f 77 65 72 20 62  bound, a lower b
2e15c 6f 75 6e 64 2c 20 6f 72 20 62 6f 74 68 2e 20 54  ound, or both. T
2e15d 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
2e15e 74 65 72 6d 73 20 74 68 61 74 20 73 65 74 20 74  terms that set t
2e15f 68 65 20 75 70 70 65 72 0a 2a 2a 20 61 6e 64 20  he upper.** and 
2e160 6c 6f 77 65 72 20 62 6f 75 6e 64 73 20 61 72 65  lower bounds are
2e161 20 72 65 70 72 65 73 65 6e 74 65 64 20 62 79 20   represented by 
2e162 70 4c 6f 77 65 72 20 61 6e 64 20 70 55 70 70 65  pLower and pUppe
2e163 72 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20  r respectively. 
2e164 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c 20  For.** example, 
2e165 61 73 73 75 6d 69 6e 67 20 74 68 61 74 20 69 6e  assuming that in
2e166 64 65 78 20 70 20 69 73 20 6f 6e 20 74 31 28 61  dex p is on t1(a
2e167 29 3a 0a 2a 2a 0a 2a 2a 20 20 20 2e 2e 2e 20 46  ):.**.**   ... F
2e168 52 4f 4d 20 74 31 20 57 48 45 52 45 20 61 20 3e  ROM t1 WHERE a >
2e169 20 3f 20 41 4e 44 20 61 20 3c 20 3f 20 2e 2e 2e   ? AND a < ? ...
2e16a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
2e16b 20 20 20 20 20 20 20 7c 5f 5f 5f 5f 5f 7c 20 20         |_____|  
2e16c 20 7c 5f 5f 5f 5f 5f 7c 0a 2a 2a 20 20 20 20 20   |_____|.**     
2e16d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e16e 20 20 7c 20 20 20 20 20 20 20 20 20 7c 0a 2a 2a    |         |.**
2e16f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e170 20 20 20 20 20 70 4c 6f 77 65 72 20 20 20 20 70       pLower    p
2e171 55 70 70 65 72 0a 2a 2a 0a 2a 2a 20 49 66 20 65  Upper.**.** If e
2e172 69 74 68 65 72 20 6f 66 20 74 68 65 20 75 70 70  ither of the upp
2e173 65 72 20 6f 72 20 6c 6f 77 65 72 20 62 6f 75 6e  er or lower boun
2e174 64 20 69 73 20 6e 6f 74 20 70 72 65 73 65 6e 74  d is not present
2e175 2c 20 74 68 65 6e 20 4e 55 4c 4c 20 69 73 20 70  , then NULL is p
2e176 61 73 73 65 64 20 69 6e 0a 2a 2a 20 70 6c 61 63  assed in.** plac
2e177 65 20 6f 66 20 74 68 65 20 63 6f 72 72 65 73 70  e of the corresp
2e178 6f 6e 64 69 6e 67 20 57 68 65 72 65 54 65 72 6d  onding WhereTerm
2e179 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 45 71 20  ..**.** The nEq 
2e17a 70 61 72 61 6d 65 74 65 72 20 69 73 20 70 61 73  parameter is pas
2e17b 73 65 64 20 74 68 65 20 69 6e 64 65 78 20 6f 66  sed the index of
2e17c 20 74 68 65 20 69 6e 64 65 78 20 63 6f 6c 75 6d   the index colum
2e17d 6e 20 73 75 62 6a 65 63 74 20 74 6f 20 74 68 65  n subject to the
2e17e 0a 2a 2a 20 72 61 6e 67 65 20 63 6f 6e 73 74 72  .** range constr
2e17f 61 69 6e 74 2e 20 4f 72 2c 20 65 71 75 69 76 61  aint. Or, equiva
2e180 6c 65 6e 74 6c 79 2c 20 74 68 65 20 6e 75 6d 62  lently, the numb
2e181 65 72 20 6f 66 20 65 71 75 61 6c 69 74 79 20 63  er of equality c
2e182 6f 6e 73 74 72 61 69 6e 74 73 0a 2a 2a 20 6f 70  onstraints.** op
2e183 74 69 6d 69 7a 65 64 20 62 79 20 74 68 65 20 70  timized by the p
2e184 72 6f 70 6f 73 65 64 20 69 6e 64 65 78 20 73 63  roposed index sc
2e185 61 6e 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  an. For example,
2e186 20 61 73 73 75 6d 69 6e 67 20 69 6e 64 65 78 20   assuming index 
2e187 70 20 69 73 0a 2a 2a 20 6f 6e 20 74 31 28 61 2c  p is.** on t1(a,
2e188 20 62 29 2c 20 61 6e 64 20 74 68 65 20 53 51 4c   b), and the SQL
2e189 20 71 75 65 72 79 20 69 73 3a 0a 2a 2a 0a 2a 2a   query is:.**.**
2e18a 20 20 20 2e 2e 2e 20 46 52 4f 4d 20 74 31 20 57     ... FROM t1 W
2e18b 48 45 52 45 20 61 20 3d 20 3f 20 41 4e 44 20 62  HERE a = ? AND b
2e18c 20 3e 20 3f 20 41 4e 44 20 62 20 3c 20 3f 20 2e   > ? AND b < ? .
2e18d 2e 2e 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 6e 45  ...**.** then nE
2e18e 71 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73  q should be pass
2e18f 65 64 20 74 68 65 20 76 61 6c 75 65 20 31 20 28  ed the value 1 (
2e190 61 73 20 74 68 65 20 72 61 6e 67 65 20 72 65 73  as the range res
2e191 74 72 69 63 74 65 64 20 63 6f 6c 75 6d 6e 2c 0a  tricted column,.
2e192 2a 2a 20 62 2c 20 69 73 20 74 68 65 20 73 65 63  ** b, is the sec
2e193 6f 6e 64 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f  ond left-most co
2e194 6c 75 6d 6e 20 6f 66 20 74 68 65 20 69 6e 64 65  lumn of the inde
2e195 78 29 2e 20 4f 72 2c 20 69 66 20 74 68 65 20 71  x). Or, if the q
2e196 75 65 72 79 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20  uery is:.**.**  
2e197 20 2e 2e 2e 20 46 52 4f 4d 20 74 31 20 57 48 45   ... FROM t1 WHE
2e198 52 45 20 61 20 3e 20 3f 20 41 4e 44 20 61 20 3c  RE a > ? AND a <
2e199 20 3f 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 74 68 65   ? ....**.** the
2e19a 6e 20 6e 45 71 20 73 68 6f 75 6c 64 20 62 65 20  n nEq should be 
2e19b 70 61 73 73 65 64 20 30 2e 0a 2a 2a 0a 2a 2a 20  passed 0..**.** 
2e19c 54 68 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c  The returned val
2e19d 75 65 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72  ue is an integer
2e19e 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 31   between 1 and 1
2e19f 30 30 2c 20 69 6e 63 6c 75 73 69 76 65 2e 20 41  00, inclusive. A
2e1a0 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65   return.** value
2e1a1 20 6f 66 20 31 20 69 6e 64 69 63 61 74 65 73 20   of 1 indicates 
2e1a2 74 68 61 74 20 74 68 65 20 70 72 6f 70 6f 73 65  that the propose
2e1a3 64 20 72 61 6e 67 65 20 73 63 61 6e 20 69 73 20  d range scan is 
2e1a4 65 78 70 65 63 74 65 64 20 74 6f 20 76 69 73 69  expected to visi
2e1a5 74 0a 2a 2a 20 61 70 70 72 6f 78 69 6d 61 74 65  t.** approximate
2e1a6 6c 79 20 31 2f 31 30 30 74 68 20 28 31 25 29 20  ly 1/100th (1%) 
2e1a7 6f 66 20 74 68 65 20 72 6f 77 73 20 73 65 6c 65  of the rows sele
2e1a8 63 74 65 64 20 62 79 20 74 68 65 20 6e 45 71 20  cted by the nEq 
2e1a9 65 71 75 61 6c 69 74 79 0a 2a 2a 20 63 6f 6e 73  equality.** cons
2e1aa 74 72 61 69 6e 74 73 20 28 69 66 20 61 6e 79 29  traints (if any)
2e1ab 2e 20 41 20 72 65 74 75 72 6e 20 76 61 6c 75 65  . A return value
2e1ac 20 6f 66 20 31 30 30 20 69 6e 64 69 63 61 74 65   of 100 indicate
2e1ad 73 20 74 68 61 74 20 69 74 20 69 73 20 65 78 70  s that it is exp
2e1ae 65 63 74 65 64 0a 2a 2a 20 74 68 61 74 20 74 68  ected.** that th
2e1af 65 20 72 61 6e 67 65 20 73 63 61 6e 20 77 69 6c  e range scan wil
2e1b0 6c 20 76 69 73 69 74 20 65 76 65 72 79 20 72 6f  l visit every ro
2e1b1 77 20 28 31 30 30 25 29 20 73 65 6c 65 63 74 65  w (100%) selecte
2e1b2 64 20 62 79 20 74 68 65 20 65 71 75 61 6c 69 74  d by the equalit
2e1b3 79 0a 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 73  y.** constraints
2e1b4 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 61  ..**.** In the a
2e1b5 62 73 65 6e 63 65 20 6f 66 20 73 71 6c 69 74 65  bsence of sqlite
2e1b6 5f 73 74 61 74 32 20 41 4e 41 4c 59 5a 45 20 64  _stat2 ANALYZE d
2e1b7 61 74 61 2c 20 65 61 63 68 20 72 61 6e 67 65 20  ata, each range 
2e1b8 69 6e 65 71 75 61 6c 69 74 79 0a 2a 2a 20 72 65  inequality.** re
2e1b9 64 75 63 65 73 20 74 68 65 20 73 65 61 72 63 68  duces the search
2e1ba 20 73 70 61 63 65 20 62 79 20 32 2f 33 72 64 73   space by 2/3rds
2e1bb 2e 20 20 48 65 6e 63 65 20 61 20 73 69 6e 67 6c  .  Hence a singl
2e1bc 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 28 78 3e  e constraint (x>
2e1bd 3f 29 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69 6e  ?).** results in
2e1be 20 61 20 72 65 74 75 72 6e 20 6f 66 20 33 33 20   a return of 33 
2e1bf 61 6e 64 20 61 20 72 61 6e 67 65 20 63 6f 6e 73  and a range cons
2e1c0 74 72 61 69 6e 74 20 28 78 3e 3f 20 41 4e 44 20  traint (x>? AND 
2e1c1 78 3c 3f 29 20 72 65 73 75 6c 74 73 0a 2a 2a 20  x<?) results.** 
2e1c2 69 6e 20 61 20 72 65 74 75 72 6e 20 6f 66 20 31  in a return of 1
2e1c3 31 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  1..*/.static int
2e1c4 20 77 68 65 72 65 52 61 6e 67 65 53 63 61 6e 45   whereRangeScanE
2e1c5 73 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  st(.  Parse *pPa
2e1c6 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 50 61  rse,       /* Pa
2e1c7 72 73 69 6e 67 20 26 20 63 6f 64 65 20 67 65 6e  rsing & code gen
2e1c8 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20  erating context 
2e1c9 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 2c 20 20  */.  Index *p,  
2e1ca 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2e1cb 20 69 6e 64 65 78 20 63 6f 6e 74 61 69 6e 69 6e   index containin
2e1cc 67 20 74 68 65 20 72 61 6e 67 65 2d 63 6f 6d 70  g the range-comp
2e1cd 61 72 65 64 20 63 6f 6c 75 6d 6e 3b 20 22 78 22  ared column; "x"
2e1ce 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 71 2c 20 20   */.  int nEq,  
2e1cf 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e             /* in
2e1d0 64 65 78 20 69 6e 74 6f 20 70 2d 3e 61 43 6f 6c  dex into p->aCol
2e1d1 5b 5d 20 6f 66 20 74 68 65 20 72 61 6e 67 65 2d  [] of the range-
2e1d2 63 6f 6d 70 61 72 65 64 20 63 6f 6c 75 6d 6e 20  compared column 
2e1d3 2a 2f 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a  */.  WhereTerm *
2e1d4 70 4c 6f 77 65 72 2c 20 20 20 2f 2a 20 4c 6f 77  pLower,   /* Low
2e1d5 65 72 20 62 6f 75 6e 64 20 6f 6e 20 74 68 65 20  er bound on the 
2e1d6 72 61 6e 67 65 2e 20 65 78 3a 20 22 78 3e 31 32  range. ex: "x>12
2e1d7 33 22 20 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c  3" Might be NULL
2e1d8 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72 6d 20   */.  WhereTerm 
2e1d9 2a 70 55 70 70 65 72 2c 20 20 20 2f 2a 20 55 70  *pUpper,   /* Up
2e1da 70 65 72 20 62 6f 75 6e 64 20 6f 6e 20 74 68 65  per bound on the
2e1db 20 72 61 6e 67 65 2e 20 65 78 3a 20 22 78 3c 34   range. ex: "x<4
2e1dc 35 35 22 20 4d 69 67 68 74 20 62 65 20 4e 55 4c  55" Might be NUL
2e1dd 4c 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 45 73  L */.  int *piEs
2e1de 74 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  t           /* O
2e1df 55 54 3a 20 52 65 74 75 72 6e 20 76 61 6c 75 65  UT: Return value
2e1e0 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20   */.){.  int rc 
2e1e1 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 23 69  = SQLITE_OK;..#i
2e1e2 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
2e1e3 4c 45 5f 53 54 41 54 32 0a 20 20 73 71 6c 69 74  LE_STAT2.  sqlit
2e1e4 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
2e1e5 3e 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76  >db;.  sqlite3_v
2e1e6 61 6c 75 65 20 2a 70 4c 6f 77 65 72 56 61 6c 20  alue *pLowerVal 
2e1e7 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76  = 0;.  sqlite3_v
2e1e8 61 6c 75 65 20 2a 70 55 70 70 65 72 56 61 6c 20  alue *pUpperVal 
2e1e9 3d 20 30 3b 0a 0a 20 20 69 66 28 20 6e 45 71 3d  = 0;..  if( nEq=
2e1ea 3d 30 20 26 26 20 70 2d 3e 61 53 61 6d 70 6c 65  =0 && p->aSample
2e1eb 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 45 73 74   ){.    int iEst
2e1ec 3b 0a 20 20 20 20 69 6e 74 20 69 4c 6f 77 65 72  ;.    int iLower
2e1ed 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 69 55   = 0;.    int iU
2e1ee 70 70 65 72 20 3d 20 53 51 4c 49 54 45 5f 49 4e  pper = SQLITE_IN
2e1ef 44 45 58 5f 53 41 4d 50 4c 45 53 3b 0a 20 20 20  DEX_SAMPLES;.   
2e1f0 20 75 38 20 61 66 66 20 3d 20 70 2d 3e 70 54 61   u8 aff = p->pTa
2e1f1 62 6c 65 2d 3e 61 43 6f 6c 5b 30 5d 2e 61 66 66  ble->aCol[0].aff
2e1f2 69 6e 69 74 79 3b 0a 0a 20 20 20 20 69 66 28 20  inity;..    if( 
2e1f3 70 4c 6f 77 65 72 20 29 7b 0a 20 20 20 20 20 20  pLower ){.      
2e1f4 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20 70 4c  Expr *pExpr = pL
2e1f5 6f 77 65 72 2d 3e 70 45 78 70 72 2d 3e 70 52 69  ower->pExpr->pRi
2e1f6 67 68 74 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  ght;.      rc = 
2e1f7 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d  sqlite3ValueFrom
2e1f8 45 78 70 72 28 64 62 2c 20 70 45 78 70 72 2c 20  Expr(db, pExpr, 
2e1f9 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 61 66 66  SQLITE_UTF8, aff
2e1fa 2c 20 26 70 4c 6f 77 65 72 56 61 6c 29 3b 0a 20  , &pLowerVal);. 
2e1fb 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d     }.    if( rc=
2e1fc 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 55  =SQLITE_OK && pU
2e1fd 70 70 65 72 20 29 7b 0a 20 20 20 20 20 20 45 78  pper ){.      Ex
2e1fe 70 72 20 2a 70 45 78 70 72 20 3d 20 70 55 70 70  pr *pExpr = pUpp
2e1ff 65 72 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67 68  er->pExpr->pRigh
2e200 74 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  t;.      rc = sq
2e201 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78  lite3ValueFromEx
2e202 70 72 28 64 62 2c 20 70 45 78 70 72 2c 20 53 51  pr(db, pExpr, SQ
2e203 4c 49 54 45 5f 55 54 46 38 2c 20 61 66 66 2c 20  LITE_UTF8, aff, 
2e204 26 70 55 70 70 65 72 56 61 6c 29 3b 0a 20 20 20  &pUpperVal);.   
2e205 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63 21 3d   }..    if( rc!=
2e206 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 28 70 4c  SQLITE_OK || (pL
2e207 6f 77 65 72 56 61 6c 3d 3d 30 20 26 26 20 70 55  owerVal==0 && pU
2e208 70 70 65 72 56 61 6c 3d 3d 30 29 20 29 7b 0a 20  pperVal==0) ){. 
2e209 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75       sqlite3Valu
2e20a 65 46 72 65 65 28 70 4c 6f 77 65 72 56 61 6c 29  eFree(pLowerVal)
2e20b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2e20c 61 6c 75 65 46 72 65 65 28 70 55 70 70 65 72 56  alueFree(pUpperV
2e20d 61 6c 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  al);.      goto 
2e20e 72 61 6e 67 65 5f 65 73 74 5f 66 61 6c 6c 62 61  range_est_fallba
2e20f 63 6b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  ck;.    }else if
2e210 28 20 70 4c 6f 77 65 72 56 61 6c 3d 3d 30 20 29  ( pLowerVal==0 )
2e211 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 77 68 65  {.      rc = whe
2e212 72 65 52 61 6e 67 65 52 65 67 69 6f 6e 28 70 50  reRangeRegion(pP
2e213 61 72 73 65 2c 20 70 2c 20 70 55 70 70 65 72 56  arse, p, pUpperV
2e214 61 6c 2c 20 26 69 55 70 70 65 72 29 3b 0a 20 20  al, &iUpper);.  
2e215 20 20 20 20 69 66 28 20 70 4c 6f 77 65 72 20 29      if( pLower )
2e216 20 69 4c 6f 77 65 72 20 3d 20 69 55 70 70 65 72   iLower = iUpper
2e217 2f 32 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  /2;.    }else if
2e218 28 20 70 55 70 70 65 72 56 61 6c 3d 3d 30 20 29  ( pUpperVal==0 )
2e219 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 77 68 65  {.      rc = whe
2e21a 72 65 52 61 6e 67 65 52 65 67 69 6f 6e 28 70 50  reRangeRegion(pP
2e21b 61 72 73 65 2c 20 70 2c 20 70 4c 6f 77 65 72 56  arse, p, pLowerV
2e21c 61 6c 2c 20 26 69 4c 6f 77 65 72 29 3b 0a 20 20  al, &iLower);.  
2e21d 20 20 20 20 69 66 28 20 70 55 70 70 65 72 20 29      if( pUpper )
2e21e 20 69 55 70 70 65 72 20 3d 20 28 69 4c 6f 77 65   iUpper = (iLowe
2e21f 72 20 2b 20 53 51 4c 49 54 45 5f 49 4e 44 45 58  r + SQLITE_INDEX
2e220 5f 53 41 4d 50 4c 45 53 20 2b 20 31 29 2f 32 3b  _SAMPLES + 1)/2;
2e221 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2e222 20 20 72 63 20 3d 20 77 68 65 72 65 52 61 6e 67    rc = whereRang
2e223 65 52 65 67 69 6f 6e 28 70 50 61 72 73 65 2c 20  eRegion(pParse, 
2e224 70 2c 20 70 55 70 70 65 72 56 61 6c 2c 20 26 69  p, pUpperVal, &i
2e225 55 70 70 65 72 29 3b 0a 20 20 20 20 20 20 69 66  Upper);.      if
2e226 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
2e227 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
2e228 77 68 65 72 65 52 61 6e 67 65 52 65 67 69 6f 6e  whereRangeRegion
2e229 28 70 50 61 72 73 65 2c 20 70 2c 20 70 4c 6f 77  (pParse, p, pLow
2e22a 65 72 56 61 6c 2c 20 26 69 4c 6f 77 65 72 29 3b  erVal, &iLower);
2e22b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
2e22c 20 20 20 20 69 45 73 74 20 3d 20 69 55 70 70 65      iEst = iUppe
2e22d 72 20 2d 20 69 4c 6f 77 65 72 3b 0a 20 20 20 20  r - iLower;.    
2e22e 74 65 73 74 63 61 73 65 28 20 69 45 73 74 3d 3d  testcase( iEst==
2e22f 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d  SQLITE_INDEX_SAM
2e230 50 4c 45 53 20 29 3b 0a 20 20 20 20 61 73 73 65  PLES );.    asse
2e231 72 74 28 20 69 45 73 74 3c 3d 53 51 4c 49 54 45  rt( iEst<=SQLITE
2e232 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 20 29  _INDEX_SAMPLES )
2e233 3b 0a 20 20 20 20 69 66 28 20 69 45 73 74 3c 31  ;.    if( iEst<1
2e234 20 29 7b 0a 20 20 20 20 20 20 69 45 73 74 20 3d   ){.      iEst =
2e235 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73   1;.    }..    s
2e236 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28  qlite3ValueFree(
2e237 70 4c 6f 77 65 72 56 61 6c 29 3b 0a 20 20 20 20  pLowerVal);.    
2e238 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65  sqlite3ValueFree
2e239 28 70 55 70 70 65 72 56 61 6c 29 3b 0a 20 20 20  (pUpperVal);.   
2e23a 20 2a 70 69 45 73 74 20 3d 20 28 69 45 73 74 20   *piEst = (iEst 
2e23b 2a 20 31 30 30 29 2f 53 51 4c 49 54 45 5f 49 4e  * 100)/SQLITE_IN
2e23c 44 45 58 5f 53 41 4d 50 4c 45 53 3b 0a 20 20 20  DEX_SAMPLES;.   
2e23d 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
2e23e 72 61 6e 67 65 5f 65 73 74 5f 66 61 6c 6c 62 61  range_est_fallba
2e23f 63 6b 3a 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53  ck:.#else.  UNUS
2e240 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 50 61  ED_PARAMETER(pPa
2e241 72 73 65 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  rse);.  UNUSED_P
2e242 41 52 41 4d 45 54 45 52 28 70 29 3b 0a 20 20 55  ARAMETER(p);.  U
2e243 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
2e244 6e 45 71 29 3b 0a 23 65 6e 64 69 66 0a 20 20 61  nEq);.#endif.  a
2e245 73 73 65 72 74 28 20 70 4c 6f 77 65 72 20 7c 7c  ssert( pLower ||
2e246 20 70 55 70 70 65 72 20 29 3b 0a 20 20 69 66 28   pUpper );.  if(
2e247 20 70 4c 6f 77 65 72 20 26 26 20 70 55 70 70 65   pLower && pUppe
2e248 72 20 29 7b 0a 20 20 20 20 2a 70 69 45 73 74 20  r ){.    *piEst 
2e249 3d 20 31 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  = 11;.  }else{. 
2e24a 20 20 20 2a 70 69 45 73 74 20 3d 20 33 33 3b 0a     *piEst = 33;.
2e24b 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
2e24c 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20  .}.../*.** Find 
2e24d 74 68 65 20 71 75 65 72 79 20 70 6c 61 6e 20 66  the query plan f
2e24e 6f 72 20 61 63 63 65 73 73 69 6e 67 20 61 20 70  or accessing a p
2e24f 61 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65 2e  articular table.
2e250 20 20 57 72 69 74 65 20 74 68 65 0a 2a 2a 20 62    Write the.** b
2e251 65 73 74 20 71 75 65 72 79 20 70 6c 61 6e 20 61  est query plan a
2e252 6e 64 20 69 74 73 20 63 6f 73 74 20 69 6e 74 6f  nd its cost into
2e253 20 74 68 65 20 57 68 65 72 65 43 6f 73 74 20 6f   the WhereCost o
2e254 62 6a 65 63 74 20 73 75 70 70 6c 69 65 64 20 61  bject supplied a
2e255 73 20 74 68 65 0a 2a 2a 20 6c 61 73 74 20 70 61  s the.** last pa
2e256 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54  rameter..**.** T
2e257 68 65 20 6c 6f 77 65 73 74 20 63 6f 73 74 20 70  he lowest cost p
2e258 6c 61 6e 20 77 69 6e 73 2e 20 20 54 68 65 20 63  lan wins.  The c
2e259 6f 73 74 20 69 73 20 61 6e 20 65 73 74 69 6d 61  ost is an estima
2e25a 74 65 20 6f 66 20 74 68 65 20 61 6d 6f 75 6e 74  te of the amount
2e25b 20 6f 66 0a 2a 2a 20 43 50 55 20 61 6e 64 20 64   of.** CPU and d
2e25c 69 73 6b 20 49 2f 4f 20 6e 65 65 64 20 74 6f 20  isk I/O need to 
2e25d 70 72 6f 63 65 73 73 20 74 68 65 20 72 65 71 75  process the requ
2e25e 65 73 74 20 75 73 69 6e 67 20 74 68 65 20 73 65  est using the se
2e25f 6c 65 63 74 65 64 20 70 6c 61 6e 2e 0a 2a 2a 20  lected plan..** 
2e260 46 61 63 74 6f 72 73 20 74 68 61 74 20 69 6e 66  Factors that inf
2e261 6c 75 65 6e 63 65 20 63 6f 73 74 20 69 6e 63 6c  luence cost incl
2e262 75 64 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20  ude:.**.**    * 
2e263 20 54 68 65 20 65 73 74 69 6d 61 74 65 64 20 6e   The estimated n
2e264 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 74 68  umber of rows th
2e265 61 74 20 77 69 6c 6c 20 62 65 20 72 65 74 72 69  at will be retri
2e266 65 76 65 64 2e 20 20 28 54 68 65 0a 2a 2a 20 20  eved.  (The.**  
2e267 20 20 20 20 20 66 65 77 65 72 20 74 68 65 20 62       fewer the b
2e268 65 74 74 65 72 2e 29 0a 2a 2a 0a 2a 2a 20 20 20  etter.).**.**   
2e269 20 2a 20 20 57 68 65 74 68 65 72 20 6f 72 20 6e   *  Whether or n
2e26a 6f 74 20 73 6f 72 74 69 6e 67 20 6d 75 73 74 20  ot sorting must 
2e26b 6f 63 63 75 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  occur..**.**    
2e26c 2a 20 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f  *  Whether or no
2e26d 74 20 74 68 65 72 65 20 6d 75 73 74 20 62 65 20  t there must be 
2e26e 73 65 70 61 72 61 74 65 20 6c 6f 6f 6b 75 70 73  separate lookups
2e26f 20 69 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20   in the.**      
2e270 20 69 6e 64 65 78 20 61 6e 64 20 69 6e 20 74 68   index and in th
2e271 65 20 6d 61 69 6e 20 74 61 62 6c 65 2e 0a 2a 2a  e main table..**
2e272 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 77 61 73  .** If there was
2e273 20 61 6e 20 49 4e 44 45 58 45 44 20 42 59 20 63   an INDEXED BY c
2e274 6c 61 75 73 65 20 28 70 53 72 63 2d 3e 70 49 6e  lause (pSrc->pIn
2e275 64 65 78 29 20 61 74 74 61 63 68 65 64 20 74 6f  dex) attached to
2e276 20 74 68 65 20 74 61 62 6c 65 20 69 6e 0a 2a 2a   the table in.**
2e277 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65   the SQL stateme
2e278 6e 74 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75  nt, then this fu
2e279 6e 63 74 69 6f 6e 20 6f 6e 6c 79 20 63 6f 6e 73  nction only cons
2e27a 69 64 65 72 73 20 70 6c 61 6e 73 20 75 73 69 6e  iders plans usin
2e27b 67 20 74 68 65 20 0a 2a 2a 20 6e 61 6d 65 64 20  g the .** named 
2e27c 69 6e 64 65 78 2e 20 49 66 20 6e 6f 20 73 75 63  index. If no suc
2e27d 68 20 70 6c 61 6e 20 69 73 20 66 6f 75 6e 64 2c  h plan is found,
2e27e 20 74 68 65 6e 20 74 68 65 20 72 65 74 75 72 6e   then the return
2e27f 65 64 20 63 6f 73 74 20 69 73 0a 2a 2a 20 53 51  ed cost is.** SQ
2e280 4c 49 54 45 5f 42 49 47 5f 44 42 4c 2e 20 49 66  LITE_BIG_DBL. If
2e281 20 61 20 70 6c 61 6e 20 69 73 20 66 6f 75 6e 64   a plan is found
2e282 20 74 68 61 74 20 75 73 65 73 20 74 68 65 20 6e   that uses the n
2e283 61 6d 65 64 20 69 6e 64 65 78 2c 20 0a 2a 2a 20  amed index, .** 
2e284 74 68 65 6e 20 74 68 65 20 63 6f 73 74 20 69 73  then the cost is
2e285 20 63 61 6c 63 75 6c 61 74 65 64 20 69 6e 20 74   calculated in t
2e286 68 65 20 75 73 75 61 6c 20 77 61 79 2e 0a 2a 2a  he usual way..**
2e287 0a 2a 2a 20 49 66 20 61 20 4e 4f 54 20 49 4e 44  .** If a NOT IND
2e288 45 58 45 44 20 63 6c 61 75 73 65 20 28 70 53 72  EXED clause (pSr
2e289 63 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 21 3d 30  c->notIndexed!=0
2e28a 29 20 77 61 73 20 61 74 74 61 63 68 65 64 20 74  ) was attached t
2e28b 6f 20 74 68 65 20 74 61 62 6c 65 20 0a 2a 2a 20  o the table .** 
2e28c 69 6e 20 74 68 65 20 53 45 4c 45 43 54 20 73 74  in the SELECT st
2e28d 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 6e 6f  atement, then no
2e28e 20 69 6e 64 65 78 65 73 20 61 72 65 20 63 6f 6e   indexes are con
2e28f 73 69 64 65 72 65 64 2e 20 48 6f 77 65 76 65 72  sidered. However
2e290 2c 20 74 68 65 20 0a 2a 2a 20 73 65 6c 65 63 74  , the .** select
2e291 65 64 20 70 6c 61 6e 20 6d 61 79 20 73 74 69 6c  ed plan may stil
2e292 6c 20 74 61 6b 65 20 61 64 76 61 6e 74 61 67 65  l take advantage
2e293 20 6f 66 20 74 68 65 20 74 61 62 6c 65 73 20 62   of the tables b
2e294 75 69 6c 74 2d 69 6e 20 72 6f 77 69 64 0a 2a 2a  uilt-in rowid.**
2e295 20 69 6e 64 65 78 2e 0a 2a 2f 0a 73 74 61 74 69   index..*/.stati
2e296 63 20 76 6f 69 64 20 62 65 73 74 42 74 72 65 65  c void bestBtree
2e297 49 6e 64 65 78 28 0a 20 20 50 61 72 73 65 20 2a  Index(.  Parse *
2e298 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
2e299 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
2e29a 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
2e29b 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57   WhereClause *pW
2e29c 43 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  C,           /* 
2e29d 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  The WHERE clause
2e29e 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63   */.  struct Src
2e29f 4c 69 73 74 5f 69 74 65 6d 20 2a 70 53 72 63 2c  List_item *pSrc,
2e2a0 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c    /* The FROM cl
2e2a1 61 75 73 65 20 74 65 72 6d 20 74 6f 20 73 65 61  ause term to sea
2e2a2 72 63 68 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b  rch */.  Bitmask
2e2a3 20 6e 6f 74 52 65 61 64 79 2c 20 20 20 20 20 20   notReady,      
2e2a4 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20       /* Mask of 
2e2a5 63 75 72 73 6f 72 73 20 74 68 61 74 20 61 72 65  cursors that are
2e2a6 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 2a   not available *
2e2a7 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f  /.  ExprList *pO
2e2a8 72 64 65 72 42 79 2c 20 20 20 20 20 20 20 20 20  rderBy,         
2e2a9 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20  /* The ORDER BY 
2e2aa 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68 65 72  clause */.  Wher
2e2ab 65 43 6f 73 74 20 2a 70 43 6f 73 74 20 20 20 20  eCost *pCost    
2e2ac 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 77 65 73          /* Lowes
2e2ad 74 20 63 6f 73 74 20 71 75 65 72 79 20 70 6c 61  t cost query pla
2e2ae 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 43  n */.){.  int iC
2e2af 75 72 20 3d 20 70 53 72 63 2d 3e 69 43 75 72 73  ur = pSrc->iCurs
2e2b0 6f 72 3b 20 20 20 2f 2a 20 54 68 65 20 63 75 72  or;   /* The cur
2e2b1 73 6f 72 20 6f 66 20 74 68 65 20 74 61 62 6c 65  sor of the table
2e2b2 20 74 6f 20 62 65 20 61 63 63 65 73 73 65 64 20   to be accessed 
2e2b3 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 50 72 6f  */.  Index *pPro
2e2b4 62 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  be;             
2e2b5 20 2f 2a 20 41 6e 20 69 6e 64 65 78 20 77 65 20   /* An index we 
2e2b6 61 72 65 20 65 76 61 6c 75 61 74 69 6e 67 20 2a  are evaluating *
2e2b7 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b  /.  Index *pIdx;
2e2b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e2b9 2f 2a 20 43 6f 70 79 20 6f 66 20 70 50 72 6f 62  /* Copy of pProb
2e2ba 65 2c 20 6f 72 20 7a 65 72 6f 20 66 6f 72 20 49  e, or zero for I
2e2bb 50 4b 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e  PK index */.  in
2e2bc 74 20 65 71 54 65 72 6d 4d 61 73 6b 3b 20 20 20  t eqTermMask;   
2e2bd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
2e2be 72 65 6e 74 20 6d 61 73 6b 20 6f 66 20 76 61 6c  rent mask of val
2e2bf 69 64 20 65 71 75 61 6c 69 74 79 20 6f 70 65 72  id equality oper
2e2c0 61 74 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69  ators */.  int i
2e2c1 64 78 45 71 54 65 72 6d 4d 61 73 6b 3b 20 20 20  dxEqTermMask;   
2e2c2 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
2e2c3 6d 61 73 6b 20 6f 66 20 76 61 6c 69 64 20 65 71  mask of valid eq
2e2c4 75 61 6c 69 74 79 20 6f 70 65 72 61 74 6f 72 73  uality operators
2e2c5 20 2a 2f 0a 20 20 49 6e 64 65 78 20 73 50 6b 3b   */.  Index sPk;
2e2c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e2c7 20 20 2f 2a 20 41 20 66 61 6b 65 20 69 6e 64 65    /* A fake inde
2e2c8 78 20 6f 62 6a 65 63 74 20 66 6f 72 20 74 68 65  x object for the
2e2c9 20 70 72 69 6d 61 72 79 20 6b 65 79 20 2a 2f 0a   primary key */.
2e2ca 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 61    unsigned int a
2e2cb 69 52 6f 77 45 73 74 50 6b 5b 32 5d 3b 20 2f 2a  iRowEstPk[2]; /*
2e2cc 20 54 68 65 20 61 69 52 6f 77 45 73 74 5b 5d 20   The aiRowEst[] 
2e2cd 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 73 50  value for the sP
2e2ce 6b 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74  k index */.  int
2e2cf 20 61 69 43 6f 6c 75 6d 6e 50 6b 20 3d 20 2d 31   aiColumnPk = -1
2e2d0 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ;        /* The 
2e2d1 61 43 6f 6c 75 6d 6e 5b 5d 20 76 61 6c 75 65 20  aColumn[] value 
2e2d2 66 6f 72 20 74 68 65 20 73 50 6b 20 69 6e 64 65  for the sPk inde
2e2d3 78 20 2a 2f 0a 20 20 69 6e 74 20 77 73 46 6c 61  x */.  int wsFla
2e2d4 67 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20  gMask;          
2e2d5 20 20 20 2f 2a 20 41 6c 6c 6f 77 65 64 20 66 6c     /* Allowed fl
2e2d6 61 67 73 20 69 6e 20 70 43 6f 73 74 2d 3e 70 6c  ags in pCost->pl
2e2d7 61 6e 2e 77 73 46 6c 61 67 20 2a 2f 0a 0a 20 20  an.wsFlag */..  
2e2d8 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68  /* Initialize th
2e2d9 65 20 63 6f 73 74 20 74 6f 20 61 20 77 6f 72 73  e cost to a wors
2e2da 74 2d 63 61 73 65 20 76 61 6c 75 65 20 2a 2f 0a  t-case value */.
2e2db 20 20 6d 65 6d 73 65 74 28 70 43 6f 73 74 2c 20    memset(pCost, 
2e2dc 30 2c 20 73 69 7a 65 6f 66 28 2a 70 43 6f 73 74  0, sizeof(*pCost
2e2dd 29 29 3b 0a 20 20 70 43 6f 73 74 2d 3e 72 43 6f  ));.  pCost->rCo
2e2de 73 74 20 3d 20 53 51 4c 49 54 45 5f 42 49 47 5f  st = SQLITE_BIG_
2e2df 44 42 4c 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  DBL;..  /* If th
2e2e0 65 20 70 53 72 63 20 74 61 62 6c 65 20 69 73 20  e pSrc table is 
2e2e1 74 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20  the right table 
2e2e2 6f 66 20 61 20 4c 45 46 54 20 4a 4f 49 4e 20 74  of a LEFT JOIN t
2e2e3 68 65 6e 20 77 65 20 6d 61 79 20 6e 6f 74 0a 20  hen we may not. 
2e2e4 20 2a 2a 20 75 73 65 20 61 6e 20 69 6e 64 65 78   ** use an index
2e2e5 20 74 6f 20 73 61 74 69 73 66 79 20 49 53 20 4e   to satisfy IS N
2e2e6 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  ULL constraints 
2e2e7 6f 6e 20 74 68 61 74 20 74 61 62 6c 65 2e 20 20  on that table.  
2e2e8 54 68 69 73 20 69 73 0a 20 20 2a 2a 20 62 65 63  This is.  ** bec
2e2e9 61 75 73 65 20 63 6f 6c 75 6d 6e 73 20 6d 69 67  ause columns mig
2e2ea 68 74 20 65 6e 64 20 75 70 20 62 65 69 6e 67 20  ht end up being 
2e2eb 4e 55 4c 4c 20 69 66 20 74 68 65 20 74 61 62 6c  NULL if the tabl
2e2ec 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68  e does not match
2e2ed 20 2d 0a 20 20 2a 2a 20 61 20 63 69 72 63 75 6d   -.  ** a circum
2e2ee 73 74 61 6e 63 65 20 77 68 69 63 68 20 74 68 65  stance which the
2e2ef 20 69 6e 64 65 78 20 63 61 6e 6e 6f 74 20 68 65   index cannot he
2e2f0 6c 70 20 75 73 20 64 69 73 63 6f 76 65 72 2e 20  lp us discover. 
2e2f1 20 54 69 63 6b 65 74 20 23 32 31 37 37 2e 0a 20   Ticket #2177.. 
2e2f2 20 2a 2f 0a 20 20 69 66 28 20 70 53 72 63 2d 3e   */.  if( pSrc->
2e2f3 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 4c 45  jointype & JT_LE
2e2f4 46 54 20 29 7b 0a 20 20 20 20 69 64 78 45 71 54  FT ){.    idxEqT
2e2f5 65 72 6d 4d 61 73 6b 20 3d 20 57 4f 5f 45 51 7c  ermMask = WO_EQ|
2e2f6 57 4f 5f 49 4e 3b 0a 20 20 7d 65 6c 73 65 7b 0a  WO_IN;.  }else{.
2e2f7 20 20 20 20 69 64 78 45 71 54 65 72 6d 4d 61 73      idxEqTermMas
2e2f8 6b 20 3d 20 57 4f 5f 45 51 7c 57 4f 5f 49 4e 7c  k = WO_EQ|WO_IN|
2e2f9 57 4f 5f 49 53 4e 55 4c 4c 3b 0a 20 20 7d 0a 0a  WO_ISNULL;.  }..
2e2fa 20 20 69 66 28 20 70 53 72 63 2d 3e 70 49 6e 64    if( pSrc->pInd
2e2fb 65 78 20 29 7b 0a 20 20 20 20 2f 2a 20 41 6e 20  ex ){.    /* An 
2e2fc 49 4e 44 45 58 45 44 20 42 59 20 63 6c 61 75 73  INDEXED BY claus
2e2fd 65 20 73 70 65 63 69 66 69 65 73 20 61 20 70 61  e specifies a pa
2e2fe 72 74 69 63 75 6c 61 72 20 69 6e 64 65 78 20 74  rticular index t
2e2ff 6f 20 75 73 65 20 2a 2f 0a 20 20 20 20 70 49 64  o use */.    pId
2e300 78 20 3d 20 70 50 72 6f 62 65 20 3d 20 70 53 72  x = pProbe = pSr
2e301 63 2d 3e 70 49 6e 64 65 78 3b 0a 20 20 20 20 77  c->pIndex;.    w
2e302 73 46 6c 61 67 4d 61 73 6b 20 3d 20 7e 28 57 48  sFlagMask = ~(WH
2e303 45 52 45 5f 52 4f 57 49 44 5f 45 51 7c 57 48 45  ERE_ROWID_EQ|WHE
2e304 52 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45 29 3b  RE_ROWID_RANGE);
2e305 0a 20 20 20 20 65 71 54 65 72 6d 4d 61 73 6b 20  .    eqTermMask 
2e306 3d 20 69 64 78 45 71 54 65 72 6d 4d 61 73 6b 3b  = idxEqTermMask;
2e307 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
2e308 20 54 68 65 72 65 20 69 73 20 6e 6f 20 49 4e 44   There is no IND
2e309 45 58 45 44 20 42 59 20 63 6c 61 75 73 65 2e 20  EXED BY clause. 
2e30a 20 43 72 65 61 74 65 20 61 20 66 61 6b 65 20 49   Create a fake I
2e30b 6e 64 65 78 20 6f 62 6a 65 63 74 20 74 6f 0a 20  ndex object to. 
2e30c 20 20 20 2a 2a 20 72 65 70 72 65 73 65 6e 74 20     ** represent 
2e30d 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20  the primary key 
2e30e 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 46  */.    Index *pF
2e30f 69 72 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  irst;           
2e310 20 20 20 20 20 2f 2a 20 41 6e 79 20 6f 74 68 65       /* Any othe
2e311 72 20 69 6e 64 65 78 20 6f 6e 20 74 68 65 20 74  r index on the t
2e312 61 62 6c 65 20 2a 2f 0a 20 20 20 20 6d 65 6d 73  able */.    mems
2e313 65 74 28 26 73 50 6b 2c 20 30 2c 20 73 69 7a 65  et(&sPk, 0, size
2e314 6f 66 28 49 6e 64 65 78 29 29 3b 0a 20 20 20 20  of(Index));.    
2e315 73 50 6b 2e 6e 43 6f 6c 75 6d 6e 20 3d 20 31 3b  sPk.nColumn = 1;
2e316 0a 20 20 20 20 73 50 6b 2e 61 69 43 6f 6c 75 6d  .    sPk.aiColum
2e317 6e 20 3d 20 26 61 69 43 6f 6c 75 6d 6e 50 6b 3b  n = &aiColumnPk;
2e318 0a 20 20 20 20 73 50 6b 2e 61 69 52 6f 77 45 73  .    sPk.aiRowEs
2e319 74 20 3d 20 61 69 52 6f 77 45 73 74 50 6b 3b 0a  t = aiRowEstPk;.
2e31a 20 20 20 20 61 69 52 6f 77 45 73 74 50 6b 5b 31      aiRowEstPk[1
2e31b 5d 20 3d 20 31 3b 0a 20 20 20 20 73 50 6b 2e 6f  ] = 1;.    sPk.o
2e31c 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 52 65 70 6c  nError = OE_Repl
2e31d 61 63 65 3b 0a 20 20 20 20 73 50 6b 2e 70 54 61  ace;.    sPk.pTa
2e31e 62 6c 65 20 3d 20 70 53 72 63 2d 3e 70 54 61 62  ble = pSrc->pTab
2e31f 3b 0a 20 20 20 20 70 46 69 72 73 74 20 3d 20 70  ;.    pFirst = p
2e320 53 72 63 2d 3e 70 54 61 62 2d 3e 70 49 6e 64 65  Src->pTab->pInde
2e321 78 3b 0a 20 20 20 20 69 66 28 20 70 53 72 63 2d  x;.    if( pSrc-
2e322 3e 6e 6f 74 49 6e 64 65 78 65 64 3d 3d 30 20 29  >notIndexed==0 )
2e323 7b 0a 20 20 20 20 20 20 73 50 6b 2e 70 4e 65 78  {.      sPk.pNex
2e324 74 20 3d 20 70 46 69 72 73 74 3b 0a 20 20 20 20  t = pFirst;.    
2e325 7d 0a 20 20 20 20 2f 2a 20 54 68 65 20 61 69 52  }.    /* The aiR
2e326 6f 77 45 73 74 50 6b 5b 30 5d 20 69 73 20 61 6e  owEstPk[0] is an
2e327 20 65 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65   estimate of the
2e328 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   total number of
2e329 20 72 6f 77 73 20 69 6e 20 74 68 65 0a 20 20 20   rows in the.   
2e32a 20 2a 2a 20 74 61 62 6c 65 2e 20 20 47 65 74 20   ** table.  Get 
2e32b 74 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  this information
2e32c 20 66 72 6f 6d 20 74 68 65 20 41 4e 41 4c 59 5a   from the ANALYZ
2e32d 45 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 66  E information if
2e32e 20 69 74 20 69 73 0a 20 20 20 20 2a 2a 20 61 76   it is.    ** av
2e32f 61 69 6c 61 62 6c 65 2e 20 20 49 66 20 6e 6f 74  ailable.  If not
2e330 20 61 76 61 69 6c 61 62 6c 65 2c 20 61 73 73 75   available, assu
2e331 6d 65 20 74 68 65 20 74 61 62 6c 65 20 31 20 6d  me the table 1 m
2e332 69 6c 6c 69 6f 6e 20 72 6f 77 73 20 69 6e 20 73  illion rows in s
2e333 69 7a 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ize..    */.    
2e334 69 66 28 20 70 46 69 72 73 74 20 29 7b 0a 20 20  if( pFirst ){.  
2e335 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 72      assert( pFir
2e336 73 74 2d 3e 61 69 52 6f 77 45 73 74 21 3d 30 20  st->aiRowEst!=0 
2e337 29 3b 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20  ); /* Allocated 
2e338 74 6f 67 65 74 68 65 72 20 77 69 74 68 20 70 46  together with pF
2e339 69 72 73 74 20 2a 2f 0a 20 20 20 20 20 20 61 69  irst */.      ai
2e33a 52 6f 77 45 73 74 50 6b 5b 30 5d 20 3d 20 70 46  RowEstPk[0] = pF
2e33b 69 72 73 74 2d 3e 61 69 52 6f 77 45 73 74 5b 30  irst->aiRowEst[0
2e33c 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  ];.    }else{.  
2e33d 20 20 20 20 61 69 52 6f 77 45 73 74 50 6b 5b 30      aiRowEstPk[0
2e33e 5d 20 3d 20 31 30 30 30 30 30 30 3b 0a 20 20 20  ] = 1000000;.   
2e33f 20 7d 0a 20 20 20 20 70 50 72 6f 62 65 20 3d 20   }.    pProbe = 
2e340 26 73 50 6b 3b 0a 20 20 20 20 77 73 46 6c 61 67  &sPk;.    wsFlag
2e341 4d 61 73 6b 20 3d 20 7e 28 0a 20 20 20 20 20 20  Mask = ~(.      
2e342 20 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49    WHERE_COLUMN_I
2e343 4e 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 45  N|WHERE_COLUMN_E
2e344 51 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 4e  Q|WHERE_COLUMN_N
2e345 55 4c 4c 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e  ULL|WHERE_COLUMN
2e346 5f 52 41 4e 47 45 0a 20 20 20 20 29 3b 0a 20 20  _RANGE.    );.  
2e347 20 20 65 71 54 65 72 6d 4d 61 73 6b 20 3d 20 57    eqTermMask = W
2e348 4f 5f 45 51 7c 57 4f 5f 49 4e 3b 0a 20 20 20 20  O_EQ|WO_IN;.    
2e349 70 49 64 78 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20  pIdx = 0;.  }.. 
2e34a 20 2f 2a 20 4c 6f 6f 70 20 6f 76 65 72 20 61 6c   /* Loop over al
2e34b 6c 20 69 6e 64 69 63 65 73 20 6c 6f 6f 6b 69 6e  l indices lookin
2e34c 67 20 66 6f 72 20 74 68 65 20 62 65 73 74 20 6f  g for the best o
2e34d 6e 65 20 74 6f 20 75 73 65 0a 20 20 2a 2f 0a 20  ne to use.  */. 
2e34e 20 66 6f 72 28 3b 20 70 50 72 6f 62 65 3b 20 70   for(; pProbe; p
2e34f 49 64 78 3d 70 50 72 6f 62 65 3d 70 50 72 6f 62  Idx=pProbe=pProb
2e350 65 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 63  e->pNext){.    c
2e351 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 69 6e  onst unsigned in
2e352 74 20 2a 20 63 6f 6e 73 74 20 61 69 52 6f 77 45  t * const aiRowE
2e353 73 74 20 3d 20 70 50 72 6f 62 65 2d 3e 61 69 52  st = pProbe->aiR
2e354 6f 77 45 73 74 3b 0a 20 20 20 20 64 6f 75 62 6c  owEst;.    doubl
2e355 65 20 63 6f 73 74 3b 20 20 20 20 20 20 20 20 20  e cost;         
2e356 20 20 20 20 20 20 20 2f 2a 20 43 6f 73 74 20 6f         /* Cost o
2e357 66 20 75 73 69 6e 67 20 70 50 72 6f 62 65 20 2a  f using pProbe *
2e358 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20 6e 52 6f  /.    double nRo
2e359 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  w;              
2e35a 20 20 2f 2a 20 45 73 74 69 6d 61 74 65 64 20 6e    /* Estimated n
2e35b 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e  umber of rows in
2e35c 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20   result set */. 
2e35d 20 20 20 69 6e 74 20 72 65 76 3b 20 20 20 20 20     int rev;     
2e35e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2e35f 2a 20 54 72 75 65 20 74 6f 20 73 63 61 6e 20 69  * True to scan i
2e360 6e 20 72 65 76 65 72 73 65 20 6f 72 64 65 72 20  n reverse order 
2e361 2a 2f 0a 20 20 20 20 69 6e 74 20 77 73 46 6c 61  */.    int wsFla
2e362 67 73 20 3d 20 30 3b 0a 20 20 20 20 42 69 74 6d  gs = 0;.    Bitm
2e363 61 73 6b 20 75 73 65 64 20 3d 20 30 3b 0a 0a 20  ask used = 0;.. 
2e364 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77     /* The follow
2e365 69 6e 67 20 76 61 72 69 61 62 6c 65 73 20 61 72  ing variables ar
2e366 65 20 70 6f 70 75 6c 61 74 65 64 20 62 61 73 65  e populated base
2e367 64 20 6f 6e 20 74 68 65 20 70 72 6f 70 65 72 74  d on the propert
2e368 69 65 73 20 6f 66 0a 20 20 20 20 2a 2a 20 73 63  ies of.    ** sc
2e369 61 6e 20 62 65 69 6e 67 20 65 76 61 6c 75 61 74  an being evaluat
2e36a 65 64 2e 20 54 68 65 79 20 61 72 65 20 74 68 65  ed. They are the
2e36b 6e 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d  n used to determ
2e36c 69 6e 65 20 74 68 65 20 65 78 70 65 63 74 65 64  ine the expected
2e36d 0a 20 20 20 20 2a 2a 20 63 6f 73 74 20 61 6e 64  .    ** cost and
2e36e 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20   number of rows 
2e36f 72 65 74 75 72 6e 65 64 2e 0a 20 20 20 20 2a 2a  returned..    **
2e370 0a 20 20 20 20 2a 2a 20 20 6e 45 71 3a 20 0a 20  .    **  nEq: . 
2e371 20 20 20 2a 2a 20 20 20 20 4e 75 6d 62 65 72 20     **    Number 
2e372 6f 66 20 65 71 75 61 6c 69 74 79 20 74 65 72 6d  of equality term
2e373 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 69 6d  s that can be im
2e374 70 6c 65 6d 65 6e 74 65 64 20 75 73 69 6e 67 20  plemented using 
2e375 74 68 65 20 69 6e 64 65 78 2e 0a 20 20 20 20 2a  the index..    *
2e376 2a 0a 20 20 20 20 2a 2a 20 20 6e 49 6e 4d 75 6c  *.    **  nInMul
2e377 3a 20 20 0a 20 20 20 20 2a 2a 20 20 20 20 54 68  :  .    **    Th
2e378 65 20 22 69 6e 2d 6d 75 6c 74 69 70 6c 69 65 72  e "in-multiplier
2e379 22 2e 20 54 68 69 73 20 69 73 20 61 6e 20 65 73  ". This is an es
2e37a 74 69 6d 61 74 65 20 6f 66 20 68 6f 77 20 6d 61  timate of how ma
2e37b 6e 79 20 73 65 65 6b 20 6f 70 65 72 61 74 69 6f  ny seek operatio
2e37c 6e 73 20 0a 20 20 20 20 2a 2a 20 20 20 20 53 51  ns .    **    SQ
2e37d 4c 69 74 65 20 6d 75 73 74 20 70 65 72 66 6f 72  Lite must perfor
2e37e 6d 20 6f 6e 20 74 68 65 20 69 6e 64 65 78 20 69  m on the index i
2e37f 6e 20 71 75 65 73 74 69 6f 6e 2e 20 46 6f 72 20  n question. For 
2e380 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 20  example, if the 
2e381 0a 20 20 20 20 2a 2a 20 20 20 20 57 48 45 52 45  .    **    WHERE
2e382 20 63 6c 61 75 73 65 20 69 73 3a 0a 20 20 20 20   clause is:.    
2e383 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 57  **.    **      W
2e384 48 45 52 45 20 61 20 49 4e 20 28 31 2c 20 32 2c  HERE a IN (1, 2,
2e385 20 33 29 20 41 4e 44 20 62 20 49 4e 20 28 34 2c   3) AND b IN (4,
2e386 20 35 2c 20 36 29 0a 20 20 20 20 2a 2a 0a 20 20   5, 6).    **.  
2e387 20 20 2a 2a 20 20 20 20 53 51 4c 69 74 65 20 6d    **    SQLite m
2e388 75 73 74 20 70 65 72 66 6f 72 6d 20 39 20 6c 6f  ust perform 9 lo
2e389 6f 6b 75 70 73 20 6f 6e 20 61 6e 20 69 6e 64 65  okups on an inde
2e38a 78 20 6f 6e 20 28 61 2c 20 62 29 2c 20 73 6f 20  x on (a, b), so 
2e38b 6e 49 6e 4d 75 6c 20 69 73 20 0a 20 20 20 20 2a  nInMul is .    *
2e38c 2a 20 20 20 20 73 65 74 20 74 6f 20 39 2e 20 47  *    set to 9. G
2e38d 69 76 65 6e 20 74 68 65 20 73 61 6d 65 20 73 63  iven the same sc
2e38e 68 65 6d 61 20 61 6e 64 20 65 69 74 68 65 72 20  hema and either 
2e38f 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
2e390 20 57 48 45 52 45 20 0a 20 20 20 20 2a 2a 20 20   WHERE .    **  
2e391 20 20 63 6c 61 75 73 65 73 3a 0a 20 20 20 20 2a    clauses:.    *
2e392 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 57 48  *.    **      WH
2e393 45 52 45 20 61 20 3d 20 20 31 0a 20 20 20 20 2a  ERE a =  1.    *
2e394 2a 20 20 20 20 20 20 57 48 45 52 45 20 61 20 3e  *      WHERE a >
2e395 3d 20 32 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  = 2.    **.    *
2e396 2a 20 20 20 20 6e 49 6e 4d 75 6c 20 69 73 20 73  *    nInMul is s
2e397 65 74 20 74 6f 20 31 2e 0a 20 20 20 20 2a 2a 0a  et to 1..    **.
2e398 20 20 20 20 2a 2a 20 20 20 20 49 66 20 74 68 65      **    If the
2e399 72 65 20 65 78 69 73 74 73 20 61 20 57 48 45 52  re exists a WHER
2e39a 45 20 74 65 72 6d 20 6f 66 20 74 68 65 20 66 6f  E term of the fo
2e39b 72 6d 20 22 78 20 49 4e 20 28 53 45 4c 45 43 54  rm "x IN (SELECT
2e39c 20 2e 2e 2e 29 22 2c 20 74 68 65 6e 20 0a 20 20   ...)", then .  
2e39d 20 20 2a 2a 20 20 20 20 74 68 65 20 73 75 62 2d    **    the sub-
2e39e 73 65 6c 65 63 74 20 69 73 20 61 73 73 75 6d 65  select is assume
2e39f 64 20 74 6f 20 72 65 74 75 72 6e 20 32 35 20 72  d to return 25 r
2e3a0 6f 77 73 20 66 6f 72 20 74 68 65 20 70 75 72 70  ows for the purp
2e3a1 6f 73 65 73 20 6f 66 20 0a 20 20 20 20 2a 2a 20  oses of .    ** 
2e3a2 20 20 20 64 65 74 65 72 6d 69 6e 69 6e 67 20 6e     determining n
2e3a3 49 6e 4d 75 6c 2e 0a 20 20 20 20 2a 2a 0a 20 20  InMul..    **.  
2e3a4 20 20 2a 2a 20 20 62 49 6e 45 73 74 3a 20 20 0a    **  bInEst:  .
2e3a5 20 20 20 20 2a 2a 20 20 20 20 53 65 74 20 74 6f      **    Set to
2e3a6 20 74 72 75 65 20 69 66 20 74 68 65 72 65 20 77   true if there w
2e3a7 61 73 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20  as at least one 
2e3a8 22 78 20 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e  "x IN (SELECT ..
2e3a9 2e 29 22 20 74 65 72 6d 20 75 73 65 64 20 0a 20  .)" term used . 
2e3aa 20 20 20 2a 2a 20 20 20 20 69 6e 20 64 65 74 65     **    in dete
2e3ab 72 6d 69 6e 69 6e 67 20 74 68 65 20 76 61 6c 75  rmining the valu
2e3ac 65 20 6f 66 20 6e 49 6e 4d 75 6c 2e 0a 20 20 20  e of nInMul..   
2e3ad 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 6e 42 6f 75   **.    **  nBou
2e3ae 6e 64 3a 0a 20 20 20 20 2a 2a 20 20 20 20 41 6e  nd:.    **    An
2e3af 20 65 73 74 69 6d 61 74 65 20 6f 6e 20 74 68 65   estimate on the
2e3b0 20 61 6d 6f 75 6e 74 20 6f 66 20 74 68 65 20 74   amount of the t
2e3b1 61 62 6c 65 20 74 68 61 74 20 6d 75 73 74 20 62  able that must b
2e3b2 65 20 73 65 61 72 63 68 65 64 2e 20 20 41 0a 20  e searched.  A. 
2e3b3 20 20 20 2a 2a 20 20 20 20 76 61 6c 75 65 20 6f     **    value o
2e3b4 66 20 31 30 30 20 6d 65 61 6e 73 20 74 68 65 20  f 100 means the 
2e3b5 65 6e 74 69 72 65 20 74 61 62 6c 65 20 69 73 20  entire table is 
2e3b6 73 65 61 72 63 68 65 64 2e 20 20 52 61 6e 67 65  searched.  Range
2e3b7 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20 20   constraints.   
2e3b8 20 2a 2a 20 20 20 20 6d 69 67 68 74 20 72 65 64   **    might red
2e3b9 75 63 65 20 74 68 69 73 20 74 6f 20 61 20 76 61  uce this to a va
2e3ba 6c 75 65 20 6c 65 73 73 20 74 68 61 6e 20 31 30  lue less than 10
2e3bb 30 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68  0 to indicate th
2e3bc 61 74 20 6f 6e 6c 79 0a 20 20 20 20 2a 2a 20 20  at only.    **  
2e3bd 20 20 61 20 66 72 61 63 74 69 6f 6e 20 6f 66 20    a fraction of 
2e3be 74 68 65 20 74 61 62 6c 65 20 6e 65 65 64 73 20  the table needs 
2e3bf 73 65 61 72 63 68 69 6e 67 2e 20 20 49 6e 20 74  searching.  In t
2e3c0 68 65 20 61 62 73 65 6e 63 65 20 6f 66 0a 20 20  he absence of.  
2e3c1 20 20 2a 2a 20 20 20 20 73 71 6c 69 74 65 5f 73    **    sqlite_s
2e3c2 74 61 74 32 20 41 4e 41 4c 59 5a 45 20 64 61 74  tat2 ANALYZE dat
2e3c3 61 2c 20 61 20 73 69 6e 67 6c 65 20 69 6e 65 71  a, a single ineq
2e3c4 75 61 6c 69 74 79 20 72 65 64 75 63 65 73 20 74  uality reduces t
2e3c5 68 65 20 73 65 61 72 63 68 0a 20 20 20 20 2a 2a  he search.    **
2e3c6 20 20 20 20 73 70 61 63 65 20 74 6f 20 31 2f 33      space to 1/3
2e3c7 72 64 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20  rd its original 
2e3c8 73 69 7a 65 2e 20 20 53 6f 20 61 6e 20 78 3e 3f  size.  So an x>?
2e3c9 20 63 6f 6e 73 74 72 61 69 6e 74 20 72 65 64 75   constraint redu
2e3ca 63 65 73 0a 20 20 20 20 2a 2a 20 20 20 20 6e 42  ces.    **    nB
2e3cb 6f 75 6e 64 20 74 6f 20 33 33 2e 20 20 54 77 6f  ound to 33.  Two
2e3cc 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 28 78 3e   constraints (x>
2e3cd 3f 20 41 4e 44 20 78 3c 3f 29 20 72 65 64 75 63  ? AND x<?) reduc
2e3ce 65 20 6e 42 6f 75 6e 64 20 74 6f 20 31 31 2e 0a  e nBound to 11..
2e3cf 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 62      **.    **  b
2e3d0 53 6f 72 74 3a 20 20 20 0a 20 20 20 20 2a 2a 20  Sort:   .    ** 
2e3d1 20 20 20 42 6f 6f 6c 65 61 6e 2e 20 54 72 75 65     Boolean. True
2e3d2 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20   if there is an 
2e3d3 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
2e3d4 74 68 61 74 20 77 69 6c 6c 20 72 65 71 75 69 72  that will requir
2e3d5 65 20 61 6e 20 0a 20 20 20 20 2a 2a 20 20 20 20  e an .    **    
2e3d6 65 78 74 65 72 6e 61 6c 20 73 6f 72 74 20 28 69  external sort (i
2e3d7 2e 65 2e 20 73 63 61 6e 6e 69 6e 67 20 74 68 65  .e. scanning the
2e3d8 20 69 6e 64 65 78 20 62 65 69 6e 67 20 65 76 61   index being eva
2e3d9 6c 75 61 74 65 64 20 77 69 6c 6c 20 6e 6f 74 20  luated will not 
2e3da 0a 20 20 20 20 2a 2a 20 20 20 20 63 6f 72 72 65  .    **    corre
2e3db 63 74 6c 79 20 6f 72 64 65 72 20 72 65 63 6f 72  ctly order recor
2e3dc 64 73 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ds)..    **.    
2e3dd 2a 2a 20 20 62 4c 6f 6f 6b 75 70 3a 20 0a 20 20  **  bLookup: .  
2e3de 20 20 2a 2a 20 20 20 20 42 6f 6f 6c 65 61 6e 2e    **    Boolean.
2e3df 20 54 72 75 65 20 69 66 20 66 6f 72 20 65 61 63   True if for eac
2e3e0 68 20 69 6e 64 65 78 20 65 6e 74 72 79 20 76 69  h index entry vi
2e3e1 73 69 74 65 64 20 61 20 6c 6f 6f 6b 75 70 20 6f  sited a lookup o
2e3e2 6e 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 20 20  n the .    **   
2e3e3 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74   corresponding t
2e3e4 61 62 6c 65 20 62 2d 74 72 65 65 20 69 73 20 72  able b-tree is r
2e3e5 65 71 75 69 72 65 64 2e 20 54 68 69 73 20 69 73  equired. This is
2e3e6 20 61 6c 77 61 79 73 20 66 61 6c 73 65 20 0a 20   always false . 
2e3e7 20 20 20 2a 2a 20 20 20 20 66 6f 72 20 74 68 65     **    for the
2e3e8 20 72 6f 77 69 64 20 69 6e 64 65 78 2e 20 46 6f   rowid index. Fo
2e3e9 72 20 6f 74 68 65 72 20 69 6e 64 65 78 65 73 2c  r other indexes,
2e3ea 20 69 74 20 69 73 20 74 72 75 65 20 75 6e 6c 65   it is true unle
2e3eb 73 73 20 61 6c 6c 20 74 68 65 20 0a 20 20 20 20  ss all the .    
2e3ec 2a 2a 20 20 20 20 63 6f 6c 75 6d 6e 73 20 6f 66  **    columns of
2e3ed 20 74 68 65 20 74 61 62 6c 65 20 75 73 65 64 20   the table used 
2e3ee 62 79 20 74 68 65 20 53 45 4c 45 43 54 20 73 74  by the SELECT st
2e3ef 61 74 65 6d 65 6e 74 20 61 72 65 20 70 72 65 73  atement are pres
2e3f0 65 6e 74 20 69 6e 20 0a 20 20 20 20 2a 2a 20 20  ent in .    **  
2e3f1 20 20 74 68 65 20 69 6e 64 65 78 20 28 73 75 63    the index (suc
2e3f2 68 20 61 6e 20 69 6e 64 65 78 20 69 73 20 73 6f  h an index is so
2e3f3 6d 65 74 69 6d 65 73 20 64 65 73 63 72 69 62 65  metimes describe
2e3f4 64 20 61 73 20 61 20 63 6f 76 65 72 69 6e 67 20  d as a covering 
2e3f5 69 6e 64 65 78 29 2e 0a 20 20 20 20 2a 2a 20 20  index)..    **  
2e3f6 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 67    For example, g
2e3f7 69 76 65 6e 20 74 68 65 20 69 6e 64 65 78 20 6f  iven the index o
2e3f8 6e 20 28 61 2c 20 62 29 2c 20 74 68 65 20 73 65  n (a, b), the se
2e3f9 63 6f 6e 64 20 6f 66 20 74 68 65 20 66 6f 6c 6c  cond of the foll
2e3fa 6f 77 69 6e 67 20 0a 20 20 20 20 2a 2a 20 20 20  owing .    **   
2e3fb 20 74 77 6f 20 71 75 65 72 69 65 73 20 72 65 71   two queries req
2e3fc 75 69 72 65 73 20 74 61 62 6c 65 20 62 2d 74 72  uires table b-tr
2e3fd 65 65 20 6c 6f 6f 6b 75 70 73 2c 20 62 75 74 20  ee lookups, but 
2e3fe 74 68 65 20 66 69 72 73 74 20 64 6f 65 73 20 6e  the first does n
2e3ff 6f 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ot..    **.    *
2e400 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 53 45  *             SE
2e401 4c 45 43 54 20 61 2c 20 62 20 20 20 20 46 52 4f  LECT a, b    FRO
2e402 4d 20 74 62 6c 20 57 48 45 52 45 20 61 20 3d 20  M tbl WHERE a = 
2e403 31 3b 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  1;.    **       
2e404 20 20 20 20 20 20 53 45 4c 45 43 54 20 61 2c 20        SELECT a, 
2e405 62 2c 20 63 20 46 52 4f 4d 20 74 62 6c 20 57 48  b, c FROM tbl WH
2e406 45 52 45 20 61 20 3d 20 31 3b 0a 20 20 20 20 2a  ERE a = 1;.    *
2e407 2f 0a 20 20 20 20 69 6e 74 20 6e 45 71 3b 0a 20  /.    int nEq;. 
2e408 20 20 20 69 6e 74 20 62 49 6e 45 73 74 20 3d 20     int bInEst = 
2e409 30 3b 0a 20 20 20 20 69 6e 74 20 6e 49 6e 4d 75  0;.    int nInMu
2e40a 6c 20 3d 20 31 3b 0a 20 20 20 20 69 6e 74 20 6e  l = 1;.    int n
2e40b 42 6f 75 6e 64 20 3d 20 31 30 30 3b 0a 20 20 20  Bound = 100;.   
2e40c 20 69 6e 74 20 62 53 6f 72 74 20 3d 20 30 3b 0a   int bSort = 0;.
2e40d 20 20 20 20 69 6e 74 20 62 4c 6f 6f 6b 75 70 20      int bLookup 
2e40e 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 44 65 74  = 0;..    /* Det
2e40f 65 72 6d 69 6e 65 20 74 68 65 20 76 61 6c 75 65  ermine the value
2e410 73 20 6f 66 20 6e 45 71 20 61 6e 64 20 6e 49 6e  s of nEq and nIn
2e411 4d 75 6c 20 2a 2f 0a 20 20 20 20 66 6f 72 28 6e  Mul */.    for(n
2e412 45 71 3d 30 3b 20 6e 45 71 3c 70 50 72 6f 62 65  Eq=0; nEq<pProbe
2e413 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6e 45 71 2b 2b  ->nColumn; nEq++
2e414 29 7b 0a 20 20 20 20 20 20 57 68 65 72 65 54 65  ){.      WhereTe
2e415 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20 20 20 20  rm *pTerm;      
2e416 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65       /* A single
2e417 20 74 65 72 6d 20 6f 66 20 74 68 65 20 57 48 45   term of the WHE
2e418 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20  RE clause */.   
2e419 20 20 20 69 6e 74 20 6a 20 3d 20 70 50 72 6f 62     int j = pProb
2e41a 65 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 45 71 5d  e->aiColumn[nEq]
2e41b 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 20 3d 20  ;.      pTerm = 
2e41c 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43  findTerm(pWC, iC
2e41d 75 72 2c 20 6a 2c 20 6e 6f 74 52 65 61 64 79 2c  ur, j, notReady,
2e41e 20 65 71 54 65 72 6d 4d 61 73 6b 2c 20 70 49 64   eqTermMask, pId
2e41f 78 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54  x);.      if( pT
2e420 65 72 6d 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a  erm==0 ) break;.
2e421 20 20 20 20 20 20 77 73 46 6c 61 67 73 20 7c 3d        wsFlags |=
2e422 20 28 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 45   (WHERE_COLUMN_E
2e423 51 7c 57 48 45 52 45 5f 52 4f 57 49 44 5f 45 51  Q|WHERE_ROWID_EQ
2e424 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 65  );.      if( pTe
2e425 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20  rm->eOperator & 
2e426 57 4f 5f 49 4e 20 29 7b 0a 20 20 20 20 20 20 20  WO_IN ){.       
2e427 20 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20 70   Expr *pExpr = p
2e428 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20  Term->pExpr;.   
2e429 20 20 20 20 20 77 73 46 6c 61 67 73 20 7c 3d 20       wsFlags |= 
2e42a 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 3b  WHERE_COLUMN_IN;
2e42b 0a 20 20 20 20 20 20 20 20 69 66 28 20 45 78 70  .        if( Exp
2e42c 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78  rHasProperty(pEx
2e42d 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  pr, EP_xIsSelect
2e42e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e  ) ){.          n
2e42f 49 6e 4d 75 6c 20 2a 3d 20 32 35 3b 0a 20 20 20  InMul *= 25;.   
2e430 20 20 20 20 20 20 20 62 49 6e 45 73 74 20 3d 20         bInEst = 
2e431 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  1;.        }else
2e432 20 69 66 28 20 70 45 78 70 72 2d 3e 78 2e 70 4c   if( pExpr->x.pL
2e433 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ist ){.         
2e434 20 6e 49 6e 4d 75 6c 20 2a 3d 20 70 45 78 70 72   nInMul *= pExpr
2e435 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72  ->x.pList->nExpr
2e436 20 2b 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a   + 1;.        }.
2e437 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
2e438 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
2e439 20 26 20 57 4f 5f 49 53 4e 55 4c 4c 20 29 7b 0a   & WO_ISNULL ){.
2e43a 20 20 20 20 20 20 20 20 77 73 46 6c 61 67 73 20          wsFlags 
2e43b 7c 3d 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f  |= WHERE_COLUMN_
2e43c 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 7d 0a 20 20  NULL;.      }.  
2e43d 20 20 20 20 75 73 65 64 20 7c 3d 20 70 54 65 72      used |= pTer
2e43e 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74 3b 0a  m->prereqRight;.
2e43f 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65      }..    /* De
2e440 74 65 72 6d 69 6e 65 20 74 68 65 20 76 61 6c 75  termine the valu
2e441 65 20 6f 66 20 6e 42 6f 75 6e 64 2e 20 2a 2f 0a  e of nBound. */.
2e442 20 20 20 20 69 66 28 20 6e 45 71 3c 70 50 72 6f      if( nEq<pPro
2e443 62 65 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20  be->nColumn ){. 
2e444 20 20 20 20 20 69 6e 74 20 6a 20 3d 20 70 50 72       int j = pPr
2e445 6f 62 65 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 45  obe->aiColumn[nE
2e446 71 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 66 69  q];.      if( fi
2e447 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72  ndTerm(pWC, iCur
2e448 2c 20 6a 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57  , j, notReady, W
2e449 4f 5f 4c 54 7c 57 4f 5f 4c 45 7c 57 4f 5f 47 54  O_LT|WO_LE|WO_GT
2e44a 7c 57 4f 5f 47 45 2c 20 70 49 64 78 29 20 29 7b  |WO_GE, pIdx) ){
2e44b 0a 20 20 20 20 20 20 20 20 57 68 65 72 65 54 65  .        WhereTe
2e44c 72 6d 20 2a 70 54 6f 70 20 3d 20 66 69 6e 64 54  rm *pTop = findT
2e44d 65 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 6a  erm(pWC, iCur, j
2e44e 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f 4c  , notReady, WO_L
2e44f 54 7c 57 4f 5f 4c 45 2c 20 70 49 64 78 29 3b 0a  T|WO_LE, pIdx);.
2e450 20 20 20 20 20 20 20 20 57 68 65 72 65 54 65 72          WhereTer
2e451 6d 20 2a 70 42 74 6d 20 3d 20 66 69 6e 64 54 65  m *pBtm = findTe
2e452 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 6a 2c  rm(pWC, iCur, j,
2e453 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f 47 54   notReady, WO_GT
2e454 7c 57 4f 5f 47 45 2c 20 70 49 64 78 29 3b 0a 20  |WO_GE, pIdx);. 
2e455 20 20 20 20 20 20 20 77 68 65 72 65 52 61 6e 67         whereRang
2e456 65 53 63 61 6e 45 73 74 28 70 50 61 72 73 65 2c  eScanEst(pParse,
2e457 20 70 50 72 6f 62 65 2c 20 6e 45 71 2c 20 70 42   pProbe, nEq, pB
2e458 74 6d 2c 20 70 54 6f 70 2c 20 26 6e 42 6f 75 6e  tm, pTop, &nBoun
2e459 64 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  d);.        if( 
2e45a 70 54 6f 70 20 29 7b 0a 20 20 20 20 20 20 20 20  pTop ){.        
2e45b 20 20 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45    wsFlags |= WHE
2e45c 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 3b 0a 20 20  RE_TOP_LIMIT;.  
2e45d 20 20 20 20 20 20 20 20 75 73 65 64 20 7c 3d 20          used |= 
2e45e 70 54 6f 70 2d 3e 70 72 65 72 65 71 52 69 67 68  pTop->prereqRigh
2e45f 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  t;.        }.   
2e460 20 20 20 20 20 69 66 28 20 70 42 74 6d 20 29 7b       if( pBtm ){
2e461 0a 20 20 20 20 20 20 20 20 20 20 77 73 46 6c 61  .          wsFla
2e462 67 73 20 7c 3d 20 57 48 45 52 45 5f 42 54 4d 5f  gs |= WHERE_BTM_
2e463 4c 49 4d 49 54 3b 0a 20 20 20 20 20 20 20 20 20  LIMIT;.         
2e464 20 75 73 65 64 20 7c 3d 20 70 42 74 6d 2d 3e 70   used |= pBtm->p
2e465 72 65 72 65 71 52 69 67 68 74 3b 0a 20 20 20 20  rereqRight;.    
2e466 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 77 73      }.        ws
2e467 46 6c 61 67 73 20 7c 3d 20 28 57 48 45 52 45 5f  Flags |= (WHERE_
2e468 43 4f 4c 55 4d 4e 5f 52 41 4e 47 45 7c 57 48 45  COLUMN_RANGE|WHE
2e469 52 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45 29 3b  RE_ROWID_RANGE);
2e46a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
2e46b 73 65 20 69 66 28 20 70 50 72 6f 62 65 2d 3e 6f  se if( pProbe->o
2e46c 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20  nError!=OE_None 
2e46d 29 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  ){.      testcas
2e46e 65 28 20 77 73 46 6c 61 67 73 20 26 20 57 48 45  e( wsFlags & WHE
2e46f 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 20 29 3b 0a  RE_COLUMN_IN );.
2e470 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2e471 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  wsFlags & WHERE_
2e472 43 4f 4c 55 4d 4e 5f 4e 55 4c 4c 20 29 3b 0a 20  COLUMN_NULL );. 
2e473 20 20 20 20 20 69 66 28 20 28 77 73 46 6c 61 67       if( (wsFlag
2e474 73 20 26 20 28 57 48 45 52 45 5f 43 4f 4c 55 4d  s & (WHERE_COLUM
2e475 4e 5f 49 4e 7c 57 48 45 52 45 5f 43 4f 4c 55 4d  N_IN|WHERE_COLUM
2e476 4e 5f 4e 55 4c 4c 29 29 3d 3d 30 20 29 7b 0a 20  N_NULL))==0 ){. 
2e477 20 20 20 20 20 20 20 77 73 46 6c 61 67 73 20 7c         wsFlags |
2e478 3d 20 57 48 45 52 45 5f 55 4e 49 51 55 45 3b 0a  = WHERE_UNIQUE;.
2e479 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
2e47a 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69     /* If there i
2e47b 73 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c  s an ORDER BY cl
2e47c 61 75 73 65 20 61 6e 64 20 74 68 65 20 69 6e 64  ause and the ind
2e47d 65 78 20 62 65 69 6e 67 20 63 6f 6e 73 69 64 65  ex being conside
2e47e 72 65 64 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20  red will.    ** 
2e47f 6e 61 74 75 72 61 6c 6c 79 20 73 63 61 6e 20 72  naturally scan r
2e480 6f 77 73 20 69 6e 20 74 68 65 20 72 65 71 75 69  ows in the requi
2e481 72 65 64 20 6f 72 64 65 72 2c 20 73 65 74 20 74  red order, set t
2e482 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 66  he appropriate f
2e483 6c 61 67 73 0a 20 20 20 20 2a 2a 20 69 6e 20 77  lags.    ** in w
2e484 73 46 6c 61 67 73 2e 20 4f 74 68 65 72 77 69 73  sFlags. Otherwis
2e485 65 2c 20 69 66 20 74 68 65 72 65 20 69 73 20 61  e, if there is a
2e486 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  n ORDER BY claus
2e487 65 20 62 75 74 20 74 68 65 20 69 6e 64 65 78 0a  e but the index.
2e488 20 20 20 20 2a 2a 20 77 69 6c 6c 20 73 63 61 6e      ** will scan
2e489 20 72 6f 77 73 20 69 6e 20 61 20 64 69 66 66 65   rows in a diffe
2e48a 72 65 6e 74 20 6f 72 64 65 72 2c 20 73 65 74 20  rent order, set 
2e48b 74 68 65 20 62 53 6f 72 74 20 76 61 72 69 61 62  the bSort variab
2e48c 6c 65 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  le.  */.    if( 
2e48d 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20  pOrderBy ){.    
2e48e 20 20 69 66 28 20 28 77 73 46 6c 61 67 73 20 26    if( (wsFlags &
2e48f 20 28 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49   (WHERE_COLUMN_I
2e490 4e 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 4e  N|WHERE_COLUMN_N
2e491 55 4c 4c 29 29 3d 3d 30 0a 20 20 20 20 20 20 20  ULL))==0.       
2e492 20 26 26 20 69 73 53 6f 72 74 69 6e 67 49 6e 64   && isSortingInd
2e493 65 78 28 70 50 61 72 73 65 2c 70 57 43 2d 3e 70  ex(pParse,pWC->p
2e494 4d 61 73 6b 53 65 74 2c 70 50 72 6f 62 65 2c 69  MaskSet,pProbe,i
2e495 43 75 72 2c 70 4f 72 64 65 72 42 79 2c 6e 45 71  Cur,pOrderBy,nEq
2e496 2c 26 72 65 76 29 0a 20 20 20 20 20 20 29 7b 0a  ,&rev).      ){.
2e497 20 20 20 20 20 20 20 20 77 73 46 6c 61 67 73 20          wsFlags 
2e498 7c 3d 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 52  |= WHERE_ROWID_R
2e499 41 4e 47 45 7c 57 48 45 52 45 5f 43 4f 4c 55 4d  ANGE|WHERE_COLUM
2e49a 4e 5f 52 41 4e 47 45 7c 57 48 45 52 45 5f 4f 52  N_RANGE|WHERE_OR
2e49b 44 45 52 42 59 3b 0a 20 20 20 20 20 20 20 20 77  DERBY;.        w
2e49c 73 46 6c 61 67 73 20 7c 3d 20 28 72 65 76 20 3f  sFlags |= (rev ?
2e49d 20 57 48 45 52 45 5f 52 45 56 45 52 53 45 20 3a   WHERE_REVERSE :
2e49e 20 30 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65   0);.      }else
2e49f 7b 0a 20 20 20 20 20 20 20 20 62 53 6f 72 74 20  {.        bSort 
2e4a0 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 1;.      }.   
2e4a1 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 63 75   }..    /* If cu
2e4a2 72 72 65 6e 74 6c 79 20 63 61 6c 63 75 6c 61 74  rrently calculat
2e4a3 69 6e 67 20 74 68 65 20 63 6f 73 74 20 6f 66 20  ing the cost of 
2e4a4 75 73 69 6e 67 20 61 6e 20 69 6e 64 65 78 20 28  using an index (
2e4a5 6e 6f 74 20 74 68 65 20 49 50 4b 0a 20 20 20 20  not the IPK.    
2e4a6 2a 2a 20 69 6e 64 65 78 29 2c 20 64 65 74 65 72  ** index), deter
2e4a7 6d 69 6e 65 20 69 66 20 61 6c 6c 20 72 65 71 75  mine if all requ
2e4a8 69 72 65 64 20 63 6f 6c 75 6d 6e 20 64 61 74 61  ired column data
2e4a9 20 6d 61 79 20 62 65 20 6f 62 74 61 69 6e 65 64   may be obtained
2e4aa 20 77 69 74 68 6f 75 74 20 0a 20 20 20 20 2a 2a   without .    **
2e4ab 20 73 65 65 6b 69 6e 67 20 74 6f 20 65 6e 74 72   seeking to entr
2e4ac 69 65 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20  ies in the main 
2e4ad 74 61 62 6c 65 20 28 69 2e 65 2e 20 69 66 20 74  table (i.e. if t
2e4ae 68 65 20 69 6e 64 65 78 20 69 73 20 61 20 63 6f  he index is a co
2e4af 76 65 72 69 6e 67 0a 20 20 20 20 2a 2a 20 69 6e  vering.    ** in
2e4b0 64 65 78 20 66 6f 72 20 74 68 69 73 20 71 75 65  dex for this que
2e4b1 72 79 29 2e 20 49 66 20 69 74 20 69 73 2c 20 73  ry). If it is, s
2e4b2 65 74 20 74 68 65 20 57 48 45 52 45 5f 49 44 58  et the WHERE_IDX
2e4b3 5f 4f 4e 4c 59 20 66 6c 61 67 20 69 6e 0a 20 20  _ONLY flag in.  
2e4b4 20 20 2a 2a 20 77 73 46 6c 61 67 73 2e 20 4f 74    ** wsFlags. Ot
2e4b5 68 65 72 77 69 73 65 2c 20 73 65 74 20 74 68 65  herwise, set the
2e4b6 20 62 4c 6f 6f 6b 75 70 20 76 61 72 69 61 62 6c   bLookup variabl
2e4b7 65 20 74 6f 20 74 72 75 65 2e 20 20 2a 2f 0a 20  e to true.  */. 
2e4b8 20 20 20 69 66 28 20 70 49 64 78 20 26 26 20 77     if( pIdx && w
2e4b9 73 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 20 20  sFlags ){.      
2e4ba 42 69 74 6d 61 73 6b 20 6d 20 3d 20 70 53 72 63  Bitmask m = pSrc
2e4bb 2d 3e 63 6f 6c 55 73 65 64 3b 0a 20 20 20 20 20  ->colUsed;.     
2e4bc 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 66 6f   int j;.      fo
2e4bd 72 28 6a 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e 6e  r(j=0; j<pIdx->n
2e4be 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20 20  Column; j++){.  
2e4bf 20 20 20 20 20 20 69 6e 74 20 78 20 3d 20 70 49        int x = pI
2e4c0 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3b  dx->aiColumn[j];
2e4c1 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 3c 42  .        if( x<B
2e4c2 4d 53 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20  MS-1 ){.        
2e4c3 20 20 6d 20 26 3d 20 7e 28 28 28 42 69 74 6d 61    m &= ~(((Bitma
2e4c4 73 6b 29 31 29 3c 3c 78 29 3b 0a 20 20 20 20 20  sk)1)<<x);.     
2e4c5 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
2e4c6 20 20 20 69 66 28 20 6d 3d 3d 30 20 29 7b 0a 20     if( m==0 ){. 
2e4c7 20 20 20 20 20 20 20 77 73 46 6c 61 67 73 20 7c         wsFlags |
2e4c8 3d 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59  = WHERE_IDX_ONLY
2e4c9 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
2e4ca 20 20 20 20 20 20 20 62 4c 6f 6f 6b 75 70 20 3d         bLookup =
2e4cb 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   1;.      }.    
2e4cc 7d 0a 0a 20 20 20 20 2f 2a 2a 2a 2a 20 42 65 67  }..    /**** Beg
2e4cd 69 6e 20 61 64 64 69 6e 67 20 75 70 20 74 68 65  in adding up the
2e4ce 20 63 6f 73 74 20 6f 66 20 75 73 69 6e 67 20 74   cost of using t
2e4cf 68 69 73 20 69 6e 64 65 78 20 28 4e 65 65 64 73  his index (Needs
2e4d0 20 69 6d 70 72 6f 76 65 6d 65 6e 74 73 29 0a 20   improvements). 
2e4d1 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 45 73 74     **.    ** Est
2e4d2 69 6d 61 74 65 20 74 68 65 20 6e 75 6d 62 65 72  imate the number
2e4d3 20 6f 66 20 72 6f 77 73 20 6f 66 20 6f 75 74 70   of rows of outp
2e4d4 75 74 2e 20 20 46 6f 72 20 61 6e 20 49 4e 20 6f  ut.  For an IN o
2e4d5 70 65 72 61 74 6f 72 2c 0a 20 20 20 20 2a 2a 20  perator,.    ** 
2e4d6 64 6f 20 6e 6f 74 20 6c 65 74 20 74 68 65 20 65  do not let the e
2e4d7 73 74 69 6d 61 74 65 20 65 78 63 65 65 64 20 68  stimate exceed h
2e4d8 61 6c 66 20 74 68 65 20 72 6f 77 73 20 69 6e 20  alf the rows in 
2e4d9 74 68 65 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a  the table..    *
2e4da 2f 0a 20 20 20 20 6e 52 6f 77 20 3d 20 28 64 6f  /.    nRow = (do
2e4db 75 62 6c 65 29 28 61 69 52 6f 77 45 73 74 5b 6e  uble)(aiRowEst[n
2e4dc 45 71 5d 20 2a 20 6e 49 6e 4d 75 6c 29 3b 0a 20  Eq] * nInMul);. 
2e4dd 20 20 20 69 66 28 20 62 49 6e 45 73 74 20 26 26     if( bInEst &&
2e4de 20 6e 52 6f 77 2a 32 3e 61 69 52 6f 77 45 73 74   nRow*2>aiRowEst
2e4df 5b 30 5d 20 29 7b 0a 20 20 20 20 20 20 6e 52 6f  [0] ){.      nRo
2e4e0 77 20 3d 20 61 69 52 6f 77 45 73 74 5b 30 5d 2f  w = aiRowEst[0]/
2e4e1 32 3b 0a 20 20 20 20 20 20 6e 49 6e 4d 75 6c 20  2;.      nInMul 
2e4e2 3d 20 28 69 6e 74 29 28 6e 52 6f 77 20 2f 20 61  = (int)(nRow / a
2e4e3 69 52 6f 77 45 73 74 5b 6e 45 71 5d 29 3b 0a 20  iRowEst[nEq]);. 
2e4e4 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 73 73     }..    /* Ass
2e4e5 75 6d 65 20 63 6f 6e 73 74 61 6e 74 20 63 6f 73  ume constant cos
2e4e6 74 20 74 6f 20 61 63 63 65 73 73 20 61 20 72 6f  t to access a ro
2e4e7 77 20 61 6e 64 20 6c 6f 67 61 72 69 74 68 6d 69  w and logarithmi
2e4e8 63 20 63 6f 73 74 20 74 6f 0a 20 20 20 20 2a 2a  c cost to.    **
2e4e9 20 64 6f 20 61 20 62 69 6e 61 72 79 20 73 65 61   do a binary sea
2e4ea 72 63 68 2e 20 20 48 65 6e 63 65 2c 20 74 68 65  rch.  Hence, the
2e4eb 20 69 6e 69 74 69 61 6c 20 63 6f 73 74 20 69 73   initial cost is
2e4ec 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f   the number of o
2e4ed 75 74 70 75 74 0a 20 20 20 20 2a 2a 20 72 6f 77  utput.    ** row
2e4ee 73 20 70 6c 75 73 20 6c 6f 67 32 28 74 61 62 6c  s plus log2(tabl
2e4ef 65 2d 73 69 7a 65 29 20 74 69 6d 65 73 20 74 68  e-size) times th
2e4f0 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 6e 61  e number of bina
2e4f1 72 79 20 73 65 61 72 63 68 65 73 2e 0a 20 20 20  ry searches..   
2e4f2 20 2a 2f 0a 20 20 20 20 63 6f 73 74 20 3d 20 6e   */.    cost = n
2e4f3 52 6f 77 20 2b 20 6e 49 6e 4d 75 6c 2a 65 73 74  Row + nInMul*est
2e4f4 4c 6f 67 28 61 69 52 6f 77 45 73 74 5b 30 5d 29  Log(aiRowEst[0])
2e4f5 3b 0a 0a 20 20 20 20 2f 2a 20 41 64 6a 75 73 74  ;..    /* Adjust
2e4f6 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72   the number of r
2e4f7 6f 77 73 20 61 6e 64 20 74 68 65 20 63 6f 73 74  ows and the cost
2e4f8 20 64 6f 77 6e 77 61 72 64 20 74 6f 20 72 65 66   downward to ref
2e4f9 6c 65 63 74 20 72 6f 77 73 0a 20 20 20 20 2a 2a  lect rows.    **
2e4fa 20 74 68 61 74 20 61 72 65 20 65 78 63 6c 75 64   that are exclud
2e4fb 65 64 20 62 79 20 72 61 6e 67 65 20 63 6f 6e 73  ed by range cons
2e4fc 74 72 61 69 6e 74 73 2e 0a 20 20 20 20 2a 2f 0a  traints..    */.
2e4fd 20 20 20 20 6e 52 6f 77 20 3d 20 28 6e 52 6f 77      nRow = (nRow
2e4fe 20 2a 20 28 64 6f 75 62 6c 65 29 6e 42 6f 75 6e   * (double)nBoun
2e4ff 64 29 20 2f 20 28 64 6f 75 62 6c 65 29 31 30 30  d) / (double)100
2e500 3b 0a 20 20 20 20 63 6f 73 74 20 3d 20 28 63 6f  ;.    cost = (co
2e501 73 74 20 2a 20 28 64 6f 75 62 6c 65 29 6e 42 6f  st * (double)nBo
2e502 75 6e 64 29 20 2f 20 28 64 6f 75 62 6c 65 29 31  und) / (double)1
2e503 30 30 3b 0a 0a 20 20 20 20 2f 2a 20 41 64 64 20  00;..    /* Add 
2e504 69 6e 20 74 68 65 20 65 73 74 69 6d 61 74 65 64  in the estimated
2e505 20 63 6f 73 74 20 6f 66 20 73 6f 72 74 69 6e 67   cost of sorting
2e506 20 74 68 65 20 72 65 73 75 6c 74 0a 20 20 20 20   the result.    
2e507 2a 2f 0a 20 20 20 20 69 66 28 20 62 53 6f 72 74  */.    if( bSort
2e508 20 29 7b 0a 20 20 20 20 20 20 63 6f 73 74 20 2b   ){.      cost +
2e509 3d 20 63 6f 73 74 2a 65 73 74 4c 6f 67 28 63 6f  = cost*estLog(co
2e50a 73 74 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  st);.    }..    
2e50b 2f 2a 20 49 66 20 61 6c 6c 20 69 6e 66 6f 72 6d  /* If all inform
2e50c 61 74 69 6f 6e 20 63 61 6e 20 62 65 20 74 61 6b  ation can be tak
2e50d 65 6e 20 64 69 72 65 63 74 6c 79 20 66 72 6f 6d  en directly from
2e50e 20 74 68 65 20 69 6e 64 65 78 2c 20 77 65 20 61   the index, we a
2e50f 76 6f 69 64 0a 20 20 20 20 2a 2a 20 64 6f 69 6e  void.    ** doin
2e510 67 20 74 61 62 6c 65 20 6c 6f 6f 6b 75 70 73 2e  g table lookups.
2e511 20 20 54 68 69 73 20 72 65 64 75 63 65 73 20 74    This reduces t
2e512 68 65 20 63 6f 73 74 20 62 79 20 68 61 6c 66 2e  he cost by half.
2e513 20 20 28 4e 6f 74 20 72 65 61 6c 6c 79 20 2d 0a    (Not really -.
2e514 20 20 20 20 2a 2a 20 74 68 69 73 20 6e 65 65 64      ** this need
2e515 73 20 74 6f 20 62 65 20 66 69 78 65 64 2e 29 0a  s to be fixed.).
2e516 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
2e517 49 64 78 20 26 26 20 62 4c 6f 6f 6b 75 70 3d 3d  Idx && bLookup==
2e518 30 20 29 7b 0a 20 20 20 20 20 20 63 6f 73 74 20  0 ){.      cost 
2e519 2f 3d 20 28 64 6f 75 62 6c 65 29 32 3b 0a 20 20  /= (double)2;.  
2e51a 20 20 7d 0a 20 20 20 20 2f 2a 2a 2a 2a 20 43 6f    }.    /**** Co
2e51b 73 74 20 6f 66 20 75 73 69 6e 67 20 74 68 69 73  st of using this
2e51c 20 69 6e 64 65 78 20 68 61 73 20 6e 6f 77 20 62   index has now b
2e51d 65 65 6e 20 63 6f 6d 70 75 74 65 64 20 2a 2a 2a  een computed ***
2e51e 2a 2f 0a 0a 20 20 20 20 57 48 45 52 45 54 52 41  */..    WHERETRA
2e51f 43 45 28 28 0a 20 20 20 20 20 20 22 74 62 6c 3d  CE((.      "tbl=
2e520 25 73 20 69 64 78 3d 25 73 20 6e 45 71 3d 25 64  %s idx=%s nEq=%d
2e521 20 6e 49 6e 4d 75 6c 3d 25 64 20 6e 42 6f 75 6e   nInMul=%d nBoun
2e522 64 3d 25 64 20 62 53 6f 72 74 3d 25 64 20 62 4c  d=%d bSort=%d bL
2e523 6f 6f 6b 75 70 3d 25 64 22 0a 20 20 20 20 20 20  ookup=%d".      
2e524 22 20 77 73 46 6c 61 67 73 3d 25 64 20 20 20 28  " wsFlags=%d   (
2e525 6e 52 6f 77 3d 25 2e 32 66 20 63 6f 73 74 3d 25  nRow=%.2f cost=%
2e526 2e 32 66 29 5c 6e 22 2c 0a 20 20 20 20 20 20 70  .2f)\n",.      p
2e527 53 72 63 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65  Src->pTab->zName
2e528 2c 20 28 70 49 64 78 20 3f 20 70 49 64 78 2d 3e  , (pIdx ? pIdx->
2e529 7a 4e 61 6d 65 20 3a 20 22 69 70 6b 22 29 2c 20  zName : "ipk"), 
2e52a 0a 20 20 20 20 20 20 6e 45 71 2c 20 6e 49 6e 4d  .      nEq, nInM
2e52b 75 6c 2c 20 6e 42 6f 75 6e 64 2c 20 62 53 6f 72  ul, nBound, bSor
2e52c 74 2c 20 62 4c 6f 6f 6b 75 70 2c 20 77 73 46 6c  t, bLookup, wsFl
2e52d 61 67 73 2c 20 6e 52 6f 77 2c 20 63 6f 73 74 0a  ags, nRow, cost.
2e52e 20 20 20 20 29 29 3b 0a 0a 20 20 20 20 2f 2a 20      ));..    /* 
2e52f 49 66 20 74 68 69 73 20 69 6e 64 65 78 20 69 73  If this index is
2e530 20 74 68 65 20 62 65 73 74 20 77 65 20 68 61 76   the best we hav
2e531 65 20 73 65 65 6e 20 73 6f 20 66 61 72 2c 20 74  e seen so far, t
2e532 68 65 6e 20 72 65 63 6f 72 64 20 74 68 69 73 0a  hen record this.
2e533 20 20 20 20 2a 2a 20 69 6e 64 65 78 20 61 6e 64      ** index and
2e534 20 69 74 73 20 63 6f 73 74 20 69 6e 20 74 68 65   its cost in the
2e535 20 70 43 6f 73 74 20 73 74 72 75 63 74 75 72 65   pCost structure
2e536 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
2e537 20 28 21 70 49 64 78 20 7c 7c 20 77 73 46 6c 61   (!pIdx || wsFla
2e538 67 73 29 20 26 26 20 63 6f 73 74 3c 70 43 6f 73  gs) && cost<pCos
2e539 74 2d 3e 72 43 6f 73 74 20 29 7b 0a 20 20 20 20  t->rCost ){.    
2e53a 20 20 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20 3d    pCost->rCost =
2e53b 20 63 6f 73 74 3b 0a 20 20 20 20 20 20 70 43 6f   cost;.      pCo
2e53c 73 74 2d 3e 6e 52 6f 77 20 3d 20 6e 52 6f 77 3b  st->nRow = nRow;
2e53d 0a 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 75 73  .      pCost->us
2e53e 65 64 20 3d 20 75 73 65 64 3b 0a 20 20 20 20 20  ed = used;.     
2e53f 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73 46   pCost->plan.wsF
2e540 6c 61 67 73 20 3d 20 28 77 73 46 6c 61 67 73 26  lags = (wsFlags&
2e541 77 73 46 6c 61 67 4d 61 73 6b 29 3b 0a 20 20 20  wsFlagMask);.   
2e542 20 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 6e     pCost->plan.n
2e543 45 71 20 3d 20 6e 45 71 3b 0a 20 20 20 20 20 20  Eq = nEq;.      
2e544 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 75 2e 70 49  pCost->plan.u.pI
2e545 64 78 20 3d 20 70 49 64 78 3b 0a 20 20 20 20 7d  dx = pIdx;.    }
2e546 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72  ..    /* If ther
2e547 65 20 77 61 73 20 61 6e 20 49 4e 44 45 58 45 44  e was an INDEXED
2e548 20 42 59 20 63 6c 61 75 73 65 2c 20 74 68 65 6e   BY clause, then
2e549 20 6f 6e 6c 79 20 74 68 61 74 20 6f 6e 65 20 69   only that one i
2e54a 6e 64 65 78 20 69 73 0a 20 20 20 20 2a 2a 20 63  ndex is.    ** c
2e54b 6f 6e 73 69 64 65 72 65 64 2e 20 2a 2f 0a 20 20  onsidered. */.  
2e54c 20 20 69 66 28 20 70 53 72 63 2d 3e 70 49 6e 64    if( pSrc->pInd
2e54d 65 78 20 29 20 62 72 65 61 6b 3b 0a 0a 20 20 20  ex ) break;..   
2e54e 20 2f 2a 20 52 65 73 65 74 20 6d 61 73 6b 73 20   /* Reset masks 
2e54f 66 6f 72 20 74 68 65 20 6e 65 78 74 20 69 6e 64  for the next ind
2e550 65 78 20 69 6e 20 74 68 65 20 6c 6f 6f 70 20 2a  ex in the loop *
2e551 2f 0a 20 20 20 20 77 73 46 6c 61 67 4d 61 73 6b  /.    wsFlagMask
2e552 20 3d 20 7e 28 57 48 45 52 45 5f 52 4f 57 49 44   = ~(WHERE_ROWID
2e553 5f 45 51 7c 57 48 45 52 45 5f 52 4f 57 49 44 5f  _EQ|WHERE_ROWID_
2e554 52 41 4e 47 45 29 3b 0a 20 20 20 20 65 71 54 65  RANGE);.    eqTe
2e555 72 6d 4d 61 73 6b 20 3d 20 69 64 78 45 71 54 65  rmMask = idxEqTe
2e556 72 6d 4d 61 73 6b 3b 0a 20 20 7d 0a 0a 20 20 2f  rmMask;.  }..  /
2e557 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f  * If there is no
2e558 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
2e559 20 61 6e 64 20 74 68 65 20 53 51 4c 49 54 45 5f   and the SQLITE_
2e55a 52 65 76 65 72 73 65 4f 72 64 65 72 20 66 6c 61  ReverseOrder fla
2e55b 67 0a 20 20 2a 2a 20 69 73 20 73 65 74 2c 20 74  g.  ** is set, t
2e55c 68 65 6e 20 72 65 76 65 72 73 65 20 74 68 65 20  hen reverse the 
2e55d 6f 72 64 65 72 20 74 68 61 74 20 74 68 65 20 69  order that the i
2e55e 6e 64 65 78 20 77 69 6c 6c 20 62 65 20 73 63 61  ndex will be sca
2e55f 6e 6e 65 64 0a 20 20 2a 2a 20 69 6e 2e 20 54 68  nned.  ** in. Th
2e560 69 73 20 69 73 20 75 73 65 64 20 66 6f 72 20 61  is is used for a
2e561 70 70 6c 69 63 61 74 69 6f 6e 20 74 65 73 74 69  pplication testi
2e562 6e 67 2c 20 74 6f 20 68 65 6c 70 20 66 69 6e 64  ng, to help find
2e563 20 63 61 73 65 73 0a 20 20 2a 2a 20 77 68 65 72   cases.  ** wher
2e564 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 62 65  e application be
2e565 68 61 76 69 6f 75 72 20 64 65 70 65 6e 64 73 20  haviour depends 
2e566 6f 6e 20 74 68 65 20 28 75 6e 64 65 66 69 6e 65  on the (undefine
2e567 64 29 20 6f 72 64 65 72 20 74 68 61 74 0a 20 20  d) order that.  
2e568 2a 2a 20 53 51 4c 69 74 65 20 6f 75 74 70 75 74  ** SQLite output
2e569 73 20 72 6f 77 73 20 69 6e 20 69 6e 20 74 68 65  s rows in in the
2e56a 20 61 62 73 65 6e 63 65 20 6f 66 20 61 6e 20 4f   absence of an O
2e56b 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 2e 20  RDER BY clause. 
2e56c 20 2a 2f 0a 20 20 69 66 28 20 21 70 4f 72 64 65   */.  if( !pOrde
2e56d 72 42 79 20 26 26 20 70 50 61 72 73 65 2d 3e 64  rBy && pParse->d
2e56e 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54  b->flags & SQLIT
2e56f 45 5f 52 65 76 65 72 73 65 4f 72 64 65 72 20 29  E_ReverseOrder )
2e570 7b 0a 20 20 20 20 70 43 6f 73 74 2d 3e 70 6c 61  {.    pCost->pla
2e571 6e 2e 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45  n.wsFlags |= WHE
2e572 52 45 5f 52 45 56 45 52 53 45 3b 0a 20 20 7d 0a  RE_REVERSE;.  }.
2e573 0a 20 20 61 73 73 65 72 74 28 20 70 4f 72 64 65  .  assert( pOrde
2e574 72 42 79 20 7c 7c 20 28 70 43 6f 73 74 2d 3e 70  rBy || (pCost->p
2e575 6c 61 6e 2e 77 73 46 6c 61 67 73 26 57 48 45 52  lan.wsFlags&WHER
2e576 45 5f 4f 52 44 45 52 42 59 29 3d 3d 30 20 29 3b  E_ORDERBY)==0 );
2e577 0a 20 20 61 73 73 65 72 74 28 20 70 43 6f 73 74  .  assert( pCost
2e578 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 3d 3d 30  ->plan.u.pIdx==0
2e579 20 7c 7c 20 28 70 43 6f 73 74 2d 3e 70 6c 61 6e   || (pCost->plan
2e57a 2e 77 73 46 6c 61 67 73 26 57 48 45 52 45 5f 52  .wsFlags&WHERE_R
2e57b 4f 57 49 44 5f 45 51 29 3d 3d 30 20 29 3b 0a 20  OWID_EQ)==0 );. 
2e57c 20 61 73 73 65 72 74 28 20 70 53 72 63 2d 3e 70   assert( pSrc->p
2e57d 49 6e 64 65 78 3d 3d 30 20 0a 20 20 20 20 20 20  Index==0 .      
2e57e 20 7c 7c 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e   || pCost->plan.
2e57f 75 2e 70 49 64 78 3d 3d 30 20 0a 20 20 20 20 20  u.pIdx==0 .     
2e580 20 20 7c 7c 20 70 43 6f 73 74 2d 3e 70 6c 61 6e    || pCost->plan
2e581 2e 75 2e 70 49 64 78 3d 3d 70 53 72 63 2d 3e 70  .u.pIdx==pSrc->p
2e582 49 6e 64 65 78 20 0a 20 20 29 3b 0a 0a 20 20 57  Index .  );..  W
2e583 48 45 52 45 54 52 41 43 45 28 28 22 62 65 73 74  HERETRACE(("best
2e584 20 69 6e 64 65 78 20 69 73 3a 20 25 73 5c 6e 22   index is: %s\n"
2e585 2c 20 0a 20 20 20 20 28 70 43 6f 73 74 2d 3e 70  , .    (pCost->p
2e586 6c 61 6e 2e 75 2e 70 49 64 78 20 3f 20 70 43 6f  lan.u.pIdx ? pCo
2e587 73 74 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 2d  st->plan.u.pIdx-
2e588 3e 7a 4e 61 6d 65 20 3a 20 22 69 70 6b 22 29 0a  >zName : "ipk").
2e589 20 20 29 29 3b 0a 20 20 0a 20 20 62 65 73 74 4f    ));.  .  bestO
2e58a 72 43 6c 61 75 73 65 49 6e 64 65 78 28 70 50 61  rClauseIndex(pPa
2e58b 72 73 65 2c 20 70 57 43 2c 20 70 53 72 63 2c 20  rse, pWC, pSrc, 
2e58c 6e 6f 74 52 65 61 64 79 2c 20 70 4f 72 64 65 72  notReady, pOrder
2e58d 42 79 2c 20 70 43 6f 73 74 29 3b 0a 20 20 70 43  By, pCost);.  pC
2e58e 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  ost->plan.wsFlag
2e58f 73 20 7c 3d 20 65 71 54 65 72 6d 4d 61 73 6b 3b  s |= eqTermMask;
2e590 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74  .}../*.** Find t
2e591 68 65 20 71 75 65 72 79 20 70 6c 61 6e 20 66 6f  he query plan fo
2e592 72 20 61 63 63 65 73 73 69 6e 67 20 74 61 62 6c  r accessing tabl
2e593 65 20 70 53 72 63 2d 3e 70 54 61 62 2e 20 57 72  e pSrc->pTab. Wr
2e594 69 74 65 20 74 68 65 0a 2a 2a 20 62 65 73 74 20  ite the.** best 
2e595 71 75 65 72 79 20 70 6c 61 6e 20 61 6e 64 20 69  query plan and i
2e596 74 73 20 63 6f 73 74 20 69 6e 74 6f 20 74 68 65  ts cost into the
2e597 20 57 68 65 72 65 43 6f 73 74 20 6f 62 6a 65 63   WhereCost objec
2e598 74 20 73 75 70 70 6c 69 65 64 20 0a 2a 2a 20 61  t supplied .** a
2e599 73 20 74 68 65 20 6c 61 73 74 20 70 61 72 61 6d  s the last param
2e59a 65 74 65 72 2e 20 54 68 69 73 20 66 75 6e 63 74  eter. This funct
2e59b 69 6f 6e 20 6d 61 79 20 63 61 6c 63 75 6c 61 74  ion may calculat
2e59c 65 20 74 68 65 20 63 6f 73 74 20 6f 66 0a 2a 2a  e the cost of.**
2e59d 20 62 6f 74 68 20 72 65 61 6c 20 61 6e 64 20 76   both real and v
2e59e 69 72 74 75 61 6c 20 74 61 62 6c 65 20 73 63 61  irtual table sca
2e59f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ns..*/.static vo
2e5a0 69 64 20 62 65 73 74 49 6e 64 65 78 28 0a 20 20  id bestIndex(.  
2e5a1 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2e5a2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2e5a3 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65  he parsing conte
2e5a4 78 74 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61  xt */.  WhereCla
2e5a5 75 73 65 20 2a 70 57 43 2c 20 20 20 20 20 20 20  use *pWC,       
2e5a6 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45      /* The WHERE
2e5a7 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74 72   clause */.  str
2e5a8 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
2e5a9 20 2a 70 53 72 63 2c 20 20 2f 2a 20 54 68 65 20   *pSrc,  /* The 
2e5aa 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72 6d  FROM clause term
2e5ab 20 74 6f 20 73 65 61 72 63 68 20 2a 2f 0a 20 20   to search */.  
2e5ac 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64 79  Bitmask notReady
2e5ad 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  ,           /* M
2e5ae 61 73 6b 20 6f 66 20 63 75 72 73 6f 72 73 20 74  ask of cursors t
2e5af 68 61 74 20 61 72 65 20 6e 6f 74 20 61 76 61 69  hat are not avai
2e5b0 6c 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72 4c  lable */.  ExprL
2e5b1 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20  ist *pOrderBy,  
2e5b2 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 52         /* The OR
2e5b3 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f  DER BY clause */
2e5b4 0a 20 20 57 68 65 72 65 43 6f 73 74 20 2a 70 43  .  WhereCost *pC
2e5b5 6f 73 74 20 20 20 20 20 20 20 20 20 20 20 20 2f  ost            /
2e5b6 2a 20 4c 6f 77 65 73 74 20 63 6f 73 74 20 71 75  * Lowest cost qu
2e5b7 65 72 79 20 70 6c 61 6e 20 2a 2f 0a 29 7b 0a 23  ery plan */.){.#
2e5b8 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2e5b9 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
2e5ba 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28    if( IsVirtual(
2e5bb 70 53 72 63 2d 3e 70 54 61 62 29 20 29 7b 0a 20  pSrc->pTab) ){. 
2e5bc 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78     sqlite3_index
2e5bd 5f 69 6e 66 6f 20 2a 70 20 3d 20 30 3b 0a 20 20  _info *p = 0;.  
2e5be 20 20 62 65 73 74 56 69 72 74 75 61 6c 49 6e 64    bestVirtualInd
2e5bf 65 78 28 70 50 61 72 73 65 2c 20 70 57 43 2c 20  ex(pParse, pWC, 
2e5c0 70 53 72 63 2c 20 6e 6f 74 52 65 61 64 79 2c 20  pSrc, notReady, 
2e5c1 70 4f 72 64 65 72 42 79 2c 20 70 43 6f 73 74 2c  pOrderBy, pCost,
2e5c2 20 26 70 29 3b 0a 20 20 20 20 69 66 28 20 70 2d   &p);.    if( p-
2e5c3 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 74  >needToFreeIdxSt
2e5c4 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  r ){.      sqlit
2e5c5 65 33 5f 66 72 65 65 28 70 2d 3e 69 64 78 53 74  e3_free(p->idxSt
2e5c6 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  r);.    }.    sq
2e5c7 6c 69 74 65 33 44 62 46 72 65 65 28 70 50 61 72  lite3DbFree(pPar
2e5c8 73 65 2d 3e 64 62 2c 20 70 29 3b 0a 20 20 7d 65  se->db, p);.  }e
2e5c9 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 7b 0a 20  lse.#endif.  {. 
2e5ca 20 20 20 62 65 73 74 42 74 72 65 65 49 6e 64 65     bestBtreeInde
2e5cb 78 28 70 50 61 72 73 65 2c 20 70 57 43 2c 20 70  x(pParse, pWC, p
2e5cc 53 72 63 2c 20 6e 6f 74 52 65 61 64 79 2c 20 70  Src, notReady, p
2e5cd 4f 72 64 65 72 42 79 2c 20 70 43 6f 73 74 29 3b  OrderBy, pCost);
2e5ce 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 69  .  }.}../*.** Di
2e5cf 73 61 62 6c 65 20 61 20 74 65 72 6d 20 69 6e 20  sable a term in 
2e5d0 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2e5d1 2e 20 20 45 78 63 65 70 74 2c 20 64 6f 20 6e 6f  .  Except, do no
2e5d2 74 20 64 69 73 61 62 6c 65 20 74 68 65 20 74 65  t disable the te
2e5d3 72 6d 0a 2a 2a 20 69 66 20 69 74 20 63 6f 6e 74  rm.** if it cont
2e5d4 72 6f 6c 73 20 61 20 4c 45 46 54 20 4f 55 54 45  rols a LEFT OUTE
2e5d5 52 20 4a 4f 49 4e 20 61 6e 64 20 69 74 20 64 69  R JOIN and it di
2e5d6 64 20 6e 6f 74 20 6f 72 69 67 69 6e 61 74 65 20  d not originate 
2e5d7 69 6e 20 74 68 65 20 4f 4e 0a 2a 2a 20 6f 72 20  in the ON.** or 
2e5d8 55 53 49 4e 47 20 63 6c 61 75 73 65 20 6f 66 20  USING clause of 
2e5d9 74 68 61 74 20 6a 6f 69 6e 2e 0a 2a 2a 0a 2a 2a  that join..**.**
2e5da 20 43 6f 6e 73 69 64 65 72 20 74 68 65 20 74 65   Consider the te
2e5db 72 6d 20 74 32 2e 7a 3d 27 6f 6b 27 20 69 6e 20  rm t2.z='ok' in 
2e5dc 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 71 75  the following qu
2e5dd 65 72 69 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 28  eries:.**.**   (
2e5de 31 29 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f  1)  SELECT * FRO
2e5df 4d 20 74 31 20 4c 45 46 54 20 4a 4f 49 4e 20 74  M t1 LEFT JOIN t
2e5e0 32 20 4f 4e 20 74 31 2e 61 3d 74 32 2e 78 20 57  2 ON t1.a=t2.x W
2e5e1 48 45 52 45 20 74 32 2e 7a 3d 27 6f 6b 27 0a 2a  HERE t2.z='ok'.*
2e5e2 2a 20 20 20 28 32 29 20 20 53 45 4c 45 43 54 20  *   (2)  SELECT 
2e5e3 2a 20 46 52 4f 4d 20 74 31 20 4c 45 46 54 20 4a  * FROM t1 LEFT J
2e5e4 4f 49 4e 20 74 32 20 4f 4e 20 74 31 2e 61 3d 74  OIN t2 ON t1.a=t
2e5e5 32 2e 78 20 41 4e 44 20 74 32 2e 7a 3d 27 6f 6b  2.x AND t2.z='ok
2e5e6 27 0a 2a 2a 20 20 20 28 33 29 20 20 53 45 4c 45  '.**   (3)  SELE
2e5e7 43 54 20 2a 20 46 52 4f 4d 20 74 31 2c 20 74 32  CT * FROM t1, t2
2e5e8 20 57 48 45 52 45 20 74 31 2e 61 3d 74 32 2e 78   WHERE t1.a=t2.x
2e5e9 20 41 4e 44 20 74 32 2e 7a 3d 27 6f 6b 27 0a 2a   AND t2.z='ok'.*
2e5ea 2a 0a 2a 2a 20 54 68 65 20 74 32 2e 7a 3d 27 6f  *.** The t2.z='o
2e5eb 6b 27 20 69 73 20 64 69 73 61 62 6c 65 64 20 69  k' is disabled i
2e5ec 6e 20 74 68 65 20 69 6e 20 28 32 29 20 62 65 63  n the in (2) bec
2e5ed 61 75 73 65 20 69 74 20 6f 72 69 67 69 6e 61 74  ause it originat
2e5ee 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 4f 4e 20  es.** in the ON 
2e5ef 63 6c 61 75 73 65 2e 20 20 54 68 65 20 74 65 72  clause.  The ter
2e5f0 6d 20 69 73 20 64 69 73 61 62 6c 65 64 20 69 6e  m is disabled in
2e5f1 20 28 33 29 20 62 65 63 61 75 73 65 20 69 74 20   (3) because it 
2e5f2 69 73 20 6e 6f 74 20 70 61 72 74 0a 2a 2a 20 6f  is not part.** o
2e5f3 66 20 61 20 4c 45 46 54 20 4f 55 54 45 52 20 4a  f a LEFT OUTER J
2e5f4 4f 49 4e 2e 20 20 49 6e 20 28 31 29 2c 20 74 68  OIN.  In (1), th
2e5f5 65 20 74 65 72 6d 20 69 73 20 6e 6f 74 20 64 69  e term is not di
2e5f6 73 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 44 69  sabled..**.** Di
2e5f7 73 61 62 6c 69 6e 67 20 61 20 74 65 72 6d 20 63  sabling a term c
2e5f8 61 75 73 65 73 20 74 68 61 74 20 74 65 72 6d 20  auses that term 
2e5f9 74 6f 20 6e 6f 74 20 62 65 20 74 65 73 74 65 64  to not be tested
2e5fa 20 69 6e 20 74 68 65 20 69 6e 6e 65 72 20 6c 6f   in the inner lo
2e5fb 6f 70 0a 2a 2a 20 6f 66 20 74 68 65 20 6a 6f 69  op.** of the joi
2e5fc 6e 2e 20 20 44 69 73 61 62 6c 69 6e 67 20 69 73  n.  Disabling is
2e5fd 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e   an optimization
2e5fe 2e 20 20 57 68 65 6e 20 74 65 72 6d 73 20 61 72  .  When terms ar
2e5ff 65 20 73 61 74 69 73 66 69 65 64 0a 2a 2a 20 62  e satisfied.** b
2e600 79 20 69 6e 64 69 63 65 73 2c 20 77 65 20 64 69  y indices, we di
2e601 73 61 62 6c 65 20 74 68 65 6d 20 74 6f 20 70 72  sable them to pr
2e602 65 76 65 6e 74 20 72 65 64 75 6e 64 61 6e 74 20  event redundant 
2e603 74 65 73 74 73 20 69 6e 20 74 68 65 20 69 6e 6e  tests in the inn
2e604 65 72 0a 2a 2a 20 6c 6f 6f 70 2e 20 20 57 65 20  er.** loop.  We 
2e605 77 6f 75 6c 64 20 67 65 74 20 74 68 65 20 63 6f  would get the co
2e606 72 72 65 63 74 20 72 65 73 75 6c 74 73 20 69 66  rrect results if
2e607 20 6e 6f 74 68 69 6e 67 20 77 65 72 65 20 65 76   nothing were ev
2e608 65 72 20 64 69 73 61 62 6c 65 64 2c 0a 2a 2a 20  er disabled,.** 
2e609 62 75 74 20 6a 6f 69 6e 73 20 6d 69 67 68 74 20  but joins might 
2e60a 72 75 6e 20 61 20 6c 69 74 74 6c 65 20 73 6c 6f  run a little slo
2e60b 77 65 72 2e 20 20 54 68 65 20 74 72 69 63 6b 20  wer.  The trick 
2e60c 69 73 20 74 6f 20 64 69 73 61 62 6c 65 20 61 73  is to disable as
2e60d 20 6d 75 63 68 0a 2a 2a 20 61 73 20 77 65 20 63   much.** as we c
2e60e 61 6e 20 77 69 74 68 6f 75 74 20 64 69 73 61 62  an without disab
2e60f 6c 69 6e 67 20 74 6f 6f 20 6d 75 63 68 2e 20 20  ling too much.  
2e610 49 66 20 77 65 20 64 69 73 61 62 6c 65 64 20 69  If we disabled i
2e611 6e 20 28 31 29 2c 20 77 65 27 64 20 67 65 74 0a  n (1), we'd get.
2e612 2a 2a 20 74 68 65 20 77 72 6f 6e 67 20 61 6e 73  ** the wrong ans
2e613 77 65 72 2e 20 20 53 65 65 20 74 69 63 6b 65 74  wer.  See ticket
2e614 20 23 38 31 33 2e 0a 2a 2f 0a 73 74 61 74 69 63   #813..*/.static
2e615 20 76 6f 69 64 20 64 69 73 61 62 6c 65 54 65 72   void disableTer
2e616 6d 28 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c  m(WhereLevel *pL
2e617 65 76 65 6c 2c 20 57 68 65 72 65 54 65 72 6d 20  evel, WhereTerm 
2e618 2a 70 54 65 72 6d 29 7b 0a 20 20 69 66 28 20 70  *pTerm){.  if( p
2e619 54 65 72 6d 0a 20 20 20 20 20 20 26 26 20 41 4c  Term.      && AL
2e61a 57 41 59 53 28 28 70 54 65 72 6d 2d 3e 77 74 46  WAYS((pTerm->wtF
2e61b 6c 61 67 73 20 26 20 54 45 52 4d 5f 43 4f 44 45  lags & TERM_CODE
2e61c 44 29 3d 3d 30 29 0a 20 20 20 20 20 20 26 26 20  D)==0).      && 
2e61d 28 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f  (pLevel->iLeftJo
2e61e 69 6e 3d 3d 30 20 7c 7c 20 45 78 70 72 48 61 73  in==0 || ExprHas
2e61f 50 72 6f 70 65 72 74 79 28 70 54 65 72 6d 2d 3e  Property(pTerm->
2e620 70 45 78 70 72 2c 20 45 50 5f 46 72 6f 6d 4a 6f  pExpr, EP_FromJo
2e621 69 6e 29 29 0a 20 20 29 7b 0a 20 20 20 20 70 54  in)).  ){.    pT
2e622 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 7c 3d 20  erm->wtFlags |= 
2e623 54 45 52 4d 5f 43 4f 44 45 44 3b 0a 20 20 20 20  TERM_CODED;.    
2e624 69 66 28 20 70 54 65 72 6d 2d 3e 69 50 61 72 65  if( pTerm->iPare
2e625 6e 74 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 57  nt>=0 ){.      W
2e626 68 65 72 65 54 65 72 6d 20 2a 70 4f 74 68 65 72  hereTerm *pOther
2e627 20 3d 20 26 70 54 65 72 6d 2d 3e 70 57 43 2d 3e   = &pTerm->pWC->
2e628 61 5b 70 54 65 72 6d 2d 3e 69 50 61 72 65 6e 74  a[pTerm->iParent
2e629 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 28 2d 2d  ];.      if( (--
2e62a 70 4f 74 68 65 72 2d 3e 6e 43 68 69 6c 64 29 3d  pOther->nChild)=
2e62b 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 64 69  =0 ){.        di
2e62c 73 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c  sableTerm(pLevel
2e62d 2c 20 70 4f 74 68 65 72 29 3b 0a 20 20 20 20 20  , pOther);.     
2e62e 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a   }.    }.  }.}..
2e62f 2f 2a 0a 2a 2a 20 43 6f 64 65 20 61 6e 20 4f 50  /*.** Code an OP
2e630 5f 41 66 66 69 6e 69 74 79 20 6f 70 63 6f 64 65  _Affinity opcode
2e631 20 74 6f 20 61 70 70 6c 79 20 74 68 65 20 63 6f   to apply the co
2e632 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74  lumn affinity st
2e633 72 69 6e 67 20 7a 41 66 66 0a 2a 2a 20 74 6f 20  ring zAff.** to 
2e634 74 68 65 20 6e 20 72 65 67 69 73 74 65 72 73 20  the n registers 
2e635 73 74 61 72 74 69 6e 67 20 61 74 20 62 61 73 65  starting at base
2e636 2e 20 0a 2a 2a 0a 2a 2a 20 42 75 66 66 65 72 20  . .**.** Buffer 
2e637 7a 41 66 66 20 77 61 73 20 61 6c 6c 6f 63 61 74  zAff was allocat
2e638 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33  ed using sqlite3
2e639 44 62 4d 61 6c 6c 6f 63 28 29 2e 20 49 74 20 69  DbMalloc(). It i
2e63a 73 20 74 68 65 20 0a 2a 2a 20 72 65 73 70 6f 6e  s the .** respon
2e63b 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68 69 73  sibility of this
2e63c 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 61 72 72   function to arr
2e63d 61 6e 67 65 20 66 6f 72 20 69 74 20 74 6f 20 62  ange for it to b
2e63e 65 20 65 76 65 6e 74 75 61 6c 6c 79 0a 2a 2a 20  e eventually.** 
2e63f 66 72 65 65 64 20 75 73 69 6e 67 20 73 71 6c 69  freed using sqli
2e640 74 65 33 44 62 46 72 65 65 28 29 2e 0a 2a 2f 0a  te3DbFree()..*/.
2e641 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65  static void code
2e642 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 50 61  ApplyAffinity(Pa
2e643 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74  rse *pParse, int
2e644 20 62 61 73 65 2c 20 69 6e 74 20 6e 2c 20 63 68   base, int n, ch
2e645 61 72 20 2a 7a 41 66 66 29 7b 0a 20 20 56 64 62  ar *zAff){.  Vdb
2e646 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70  e *v = pParse->p
2e647 56 64 62 65 3b 0a 20 20 61 73 73 65 72 74 28 20  Vdbe;.  assert( 
2e648 76 21 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65  v!=0 );.  sqlite
2e649 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2e64a 50 5f 41 66 66 69 6e 69 74 79 2c 20 62 61 73 65  P_Affinity, base
2e64b 2c 20 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  , n);.  sqlite3V
2e64c 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d  dbeChangeP4(v, -
2e64d 31 2c 20 7a 41 66 66 2c 20 50 34 5f 44 59 4e 41  1, zAff, P4_DYNA
2e64e 4d 49 43 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  MIC);.  sqlite3E
2e64f 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79  xprCacheAffinity
2e650 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 62  Change(pParse, b
2e651 61 73 65 2c 20 6e 29 3b 0a 7d 0a 0a 0a 2f 2a 0a  ase, n);.}.../*.
2e652 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
2e653 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 65 71   for a single eq
2e654 75 61 6c 69 74 79 20 74 65 72 6d 20 6f 66 20 74  uality term of t
2e655 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e  he WHERE clause.
2e656 20 20 41 6e 20 65 71 75 61 6c 69 74 79 0a 2a 2a    An equality.**
2e657 20 74 65 72 6d 20 63 61 6e 20 62 65 20 65 69 74   term can be eit
2e658 68 65 72 20 58 3d 65 78 70 72 20 6f 72 20 58 20  her X=expr or X 
2e659 49 4e 20 28 2e 2e 2e 29 2e 20 20 20 70 54 65 72  IN (...).   pTer
2e65a 6d 20 69 73 20 74 68 65 20 74 65 72 6d 20 74 6f  m is the term to
2e65b 20 62 65 20 0a 2a 2a 20 63 6f 64 65 64 2e 0a 2a   be .** coded..*
2e65c 2a 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74  *.** The current
2e65d 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 63   value for the c
2e65e 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 6c 65 66  onstraint is lef
2e65f 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 69 52  t in register iR
2e660 65 67 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 20  eg..**.** For a 
2e661 63 6f 6e 73 74 72 61 69 6e 74 20 6f 66 20 74 68  constraint of th
2e662 65 20 66 6f 72 6d 20 58 3d 65 78 70 72 2c 20 74  e form X=expr, t
2e663 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
2e664 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 69   evaluated and i
2e665 74 73 0a 2a 2a 20 72 65 73 75 6c 74 20 69 73 20  ts.** result is 
2e666 6c 65 66 74 20 6f 6e 20 74 68 65 20 73 74 61 63  left on the stac
2e667 6b 2e 20 20 46 6f 72 20 63 6f 6e 73 74 72 61 69  k.  For constrai
2e668 6e 74 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  nts of the form 
2e669 58 20 49 4e 20 28 2e 2e 2e 29 0a 2a 2a 20 74 68  X IN (...).** th
2e66a 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20  is routine sets 
2e66b 75 70 20 61 20 6c 6f 6f 70 20 74 68 61 74 20 77  up a loop that w
2e66c 69 6c 6c 20 69 74 65 72 61 74 65 20 6f 76 65 72  ill iterate over
2e66d 20 61 6c 6c 20 76 61 6c 75 65 73 20 6f 66 20 58   all values of X
2e66e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2e66f 63 6f 64 65 45 71 75 61 6c 69 74 79 54 65 72 6d  codeEqualityTerm
2e670 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2e671 65 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70  e,      /* The p
2e672 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
2e673 2f 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70  /.  WhereTerm *p
2e674 54 65 72 6d 2c 20 20 20 2f 2a 20 54 68 65 20 74  Term,   /* The t
2e675 65 72 6d 20 6f 66 20 74 68 65 20 57 48 45 52 45  erm of the WHERE
2e676 20 63 6c 61 75 73 65 20 74 6f 20 62 65 20 63 6f   clause to be co
2e677 64 65 64 20 2a 2f 0a 20 20 57 68 65 72 65 4c 65  ded */.  WhereLe
2e678 76 65 6c 20 2a 70 4c 65 76 65 6c 2c 20 2f 2a 20  vel *pLevel, /* 
2e679 57 68 65 6e 20 6c 65 76 65 6c 20 6f 66 20 74 68  When level of th
2e67a 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 77 65  e FROM clause we
2e67b 20 61 72 65 20 77 6f 72 6b 69 6e 67 20 6f 6e 20   are working on 
2e67c 2a 2f 0a 20 20 69 6e 74 20 69 54 61 72 67 65 74  */.  int iTarget
2e67d 20 20 20 20 20 20 20 20 20 2f 2a 20 41 74 74 65           /* Atte
2e67e 6d 70 74 20 74 6f 20 6c 65 61 76 65 20 72 65 73  mpt to leave res
2e67f 75 6c 74 73 20 69 6e 20 74 68 69 73 20 72 65 67  ults in this reg
2e680 69 73 74 65 72 20 2a 2f 0a 29 7b 0a 20 20 45 78  ister */.){.  Ex
2e681 70 72 20 2a 70 58 20 3d 20 70 54 65 72 6d 2d 3e  pr *pX = pTerm->
2e682 70 45 78 70 72 3b 0a 20 20 56 64 62 65 20 2a 76  pExpr;.  Vdbe *v
2e683 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
2e684 3b 0a 20 20 69 6e 74 20 69 52 65 67 3b 20 20 20  ;.  int iReg;   
2e685 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2e686 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69  * Register holdi
2e687 6e 67 20 72 65 73 75 6c 74 73 20 2a 2f 0a 0a 20  ng results */.. 
2e688 20 61 73 73 65 72 74 28 20 69 54 61 72 67 65 74   assert( iTarget
2e689 3e 30 20 29 3b 0a 20 20 69 66 28 20 70 58 2d 3e  >0 );.  if( pX->
2e68a 6f 70 3d 3d 54 4b 5f 45 51 20 29 7b 0a 20 20 20  op==TK_EQ ){.   
2e68b 20 69 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45   iReg = sqlite3E
2e68c 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50  xprCodeTarget(pP
2e68d 61 72 73 65 2c 20 70 58 2d 3e 70 52 69 67 68 74  arse, pX->pRight
2e68e 2c 20 69 54 61 72 67 65 74 29 3b 0a 20 20 7d 65  , iTarget);.  }e
2e68f 6c 73 65 20 69 66 28 20 70 58 2d 3e 6f 70 3d 3d  lse if( pX->op==
2e690 54 4b 5f 49 53 4e 55 4c 4c 20 29 7b 0a 20 20 20  TK_ISNULL ){.   
2e691 20 69 52 65 67 20 3d 20 69 54 61 72 67 65 74 3b   iReg = iTarget;
2e692 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2e693 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c  AddOp2(v, OP_Nul
2e694 6c 2c 20 30 2c 20 69 52 65 67 29 3b 0a 23 69 66  l, 0, iReg);.#if
2e695 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2e696 5f 53 55 42 51 55 45 52 59 0a 20 20 7d 65 6c 73  _SUBQUERY.  }els
2e697 65 7b 0a 20 20 20 20 69 6e 74 20 65 54 79 70 65  e{.    int eType
2e698 3b 0a 20 20 20 20 69 6e 74 20 69 54 61 62 3b 0a  ;.    int iTab;.
2e699 20 20 20 20 73 74 72 75 63 74 20 49 6e 4c 6f 6f      struct InLoo
2e69a 70 20 2a 70 49 6e 3b 0a 0a 20 20 20 20 61 73 73  p *pIn;..    ass
2e69b 65 72 74 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f  ert( pX->op==TK_
2e69c 49 4e 20 29 3b 0a 20 20 20 20 69 52 65 67 20 3d  IN );.    iReg =
2e69d 20 69 54 61 72 67 65 74 3b 0a 20 20 20 20 65 54   iTarget;.    eT
2e69e 79 70 65 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  ype = sqlite3Fin
2e69f 64 49 6e 49 6e 64 65 78 28 70 50 61 72 73 65 2c  dInIndex(pParse,
2e6a0 20 70 58 2c 20 30 29 3b 0a 20 20 20 20 69 54 61   pX, 0);.    iTa
2e6a1 62 20 3d 20 70 58 2d 3e 69 54 61 62 6c 65 3b 0a  b = pX->iTable;.
2e6a2 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2e6a3 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69  ddOp2(v, OP_Rewi
2e6a4 6e 64 2c 20 69 54 61 62 2c 20 30 29 3b 0a 20 20  nd, iTab, 0);.  
2e6a5 20 20 61 73 73 65 72 74 28 20 70 4c 65 76 65 6c    assert( pLevel
2e6a6 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26  ->plan.wsFlags &
2e6a7 20 57 48 45 52 45 5f 49 4e 5f 41 42 4c 45 20 29   WHERE_IN_ABLE )
2e6a8 3b 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c  ;.    if( pLevel
2e6a9 2d 3e 75 2e 69 6e 2e 6e 49 6e 3d 3d 30 20 29 7b  ->u.in.nIn==0 ){
2e6aa 0a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 61  .      pLevel->a
2e6ab 64 64 72 4e 78 74 20 3d 20 73 71 6c 69 74 65 33  ddrNxt = sqlite3
2e6ac 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29  VdbeMakeLabel(v)
2e6ad 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 65 76  ;.    }.    pLev
2e6ae 65 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e 2b 2b 3b 0a  el->u.in.nIn++;.
2e6af 20 20 20 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e      pLevel->u.in
2e6b0 2e 61 49 6e 4c 6f 6f 70 20 3d 0a 20 20 20 20 20  .aInLoop =.     
2e6b1 20 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c    sqlite3DbReall
2e6b2 6f 63 4f 72 46 72 65 65 28 70 50 61 72 73 65 2d  ocOrFree(pParse-
2e6b3 3e 64 62 2c 20 70 4c 65 76 65 6c 2d 3e 75 2e 69  >db, pLevel->u.i
2e6b4 6e 2e 61 49 6e 4c 6f 6f 70 2c 0a 20 20 20 20 20  n.aInLoop,.     
2e6b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e6b6 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28           sizeof(
2e6b7 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 61 49 6e  pLevel->u.in.aIn
2e6b8 4c 6f 6f 70 5b 30 5d 29 2a 70 4c 65 76 65 6c 2d  Loop[0])*pLevel-
2e6b9 3e 75 2e 69 6e 2e 6e 49 6e 29 3b 0a 20 20 20 20  >u.in.nIn);.    
2e6ba 70 49 6e 20 3d 20 70 4c 65 76 65 6c 2d 3e 75 2e  pIn = pLevel->u.
2e6bb 69 6e 2e 61 49 6e 4c 6f 6f 70 3b 0a 20 20 20 20  in.aInLoop;.    
2e6bc 69 66 28 20 70 49 6e 20 29 7b 0a 20 20 20 20 20  if( pIn ){.     
2e6bd 20 70 49 6e 20 2b 3d 20 70 4c 65 76 65 6c 2d 3e   pIn += pLevel->
2e6be 75 2e 69 6e 2e 6e 49 6e 20 2d 20 31 3b 0a 20 20  u.in.nIn - 1;.  
2e6bf 20 20 20 20 70 49 6e 2d 3e 69 43 75 72 20 3d 20      pIn->iCur = 
2e6c0 69 54 61 62 3b 0a 20 20 20 20 20 20 69 66 28 20  iTab;.      if( 
2e6c1 65 54 79 70 65 3d 3d 49 4e 5f 49 4e 44 45 58 5f  eType==IN_INDEX_
2e6c2 52 4f 57 49 44 20 29 7b 0a 20 20 20 20 20 20 20  ROWID ){.       
2e6c3 20 70 49 6e 2d 3e 61 64 64 72 49 6e 54 6f 70 20   pIn->addrInTop 
2e6c4 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
2e6c5 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c  Op2(v, OP_Rowid,
2e6c6 20 69 54 61 62 2c 20 69 52 65 67 29 3b 0a 20 20   iTab, iReg);.  
2e6c7 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2e6c8 20 20 20 70 49 6e 2d 3e 61 64 64 72 49 6e 54 6f     pIn->addrInTo
2e6c9 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  p = sqlite3VdbeA
2e6ca 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75  ddOp3(v, OP_Colu
2e6cb 6d 6e 2c 20 69 54 61 62 2c 20 30 2c 20 69 52 65  mn, iTab, 0, iRe
2e6cc 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  g);.      }.    
2e6cd 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2e6ce 4f 70 31 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c  Op1(v, OP_IsNull
2e6cf 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 7d 65 6c  , iReg);.    }el
2e6d0 73 65 7b 0a 20 20 20 20 20 20 70 4c 65 76 65 6c  se{.      pLevel
2e6d1 2d 3e 75 2e 69 6e 2e 6e 49 6e 20 3d 20 30 3b 0a  ->u.in.nIn = 0;.
2e6d2 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d      }.#endif.  }
2e6d3 0a 20 20 64 69 73 61 62 6c 65 54 65 72 6d 28 70  .  disableTerm(p
2e6d4 4c 65 76 65 6c 2c 20 70 54 65 72 6d 29 3b 0a 20  Level, pTerm);. 
2e6d5 20 72 65 74 75 72 6e 20 69 52 65 67 3b 0a 7d 0a   return iReg;.}.
2e6d6 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
2e6d7 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 65  code that will e
2e6d8 76 61 6c 75 61 74 65 20 61 6c 6c 20 3d 3d 20 61  valuate all == a
2e6d9 6e 64 20 49 4e 20 63 6f 6e 73 74 72 61 69 6e 74  nd IN constraint
2e6da 73 20 66 6f 72 20 61 6e 0a 2a 2a 20 69 6e 64 65  s for an.** inde
2e6db 78 2e 20 20 54 68 65 20 76 61 6c 75 65 73 20 66  x.  The values f
2e6dc 6f 72 20 61 6c 6c 20 63 6f 6e 73 74 72 61 69 6e  or all constrain
2e6dd 74 73 20 61 72 65 20 6c 65 66 74 20 6f 6e 20 74  ts are left on t
2e6de 68 65 20 73 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20  he stack..**.** 
2e6df 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 63 6f 6e  For example, con
2e6e0 73 69 64 65 72 20 74 61 62 6c 65 20 74 31 28 61  sider table t1(a
2e6e1 2c 62 2c 63 2c 64 2c 65 2c 66 29 20 77 69 74 68  ,b,c,d,e,f) with
2e6e2 20 69 6e 64 65 78 20 69 31 28 61 2c 62 2c 63 29   index i1(a,b,c)
2e6e3 2e 0a 2a 2a 20 53 75 70 70 6f 73 65 20 74 68 65  ..** Suppose the
2e6e4 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 69 73   WHERE clause is
2e6e5 20 74 68 69 73 3a 20 20 61 3d 3d 35 20 41 4e 44   this:  a==5 AND
2e6e6 20 62 20 49 4e 20 28 31 2c 32 2c 33 29 20 41 4e   b IN (1,2,3) AN
2e6e7 44 20 63 3e 35 20 41 4e 44 20 63 3c 31 30 0a 2a  D c>5 AND c<10.*
2e6e8 2a 20 54 68 65 20 69 6e 64 65 78 20 68 61 73 20  * The index has 
2e6e9 61 73 20 6d 61 6e 79 20 61 73 20 74 68 72 65 65  as many as three
2e6ea 20 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72   equality constr
2e6eb 61 69 6e 74 73 2c 20 62 75 74 20 69 6e 20 74 68  aints, but in th
2e6ec 69 73 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 74  is.** example, t
2e6ed 68 65 20 74 68 69 72 64 20 22 63 22 20 76 61 6c  he third "c" val
2e6ee 75 65 20 69 73 20 61 6e 20 69 6e 65 71 75 61 6c  ue is an inequal
2e6ef 69 74 79 2e 20 20 53 6f 20 6f 6e 6c 79 20 74 77  ity.  So only tw
2e6f0 6f 20 0a 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74  o .** constraint
2e6f1 73 20 61 72 65 20 63 6f 64 65 64 2e 20 20 54 68  s are coded.  Th
2e6f2 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  is routine will 
2e6f3 67 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  generate code to
2e6f4 20 65 76 61 6c 75 61 74 65 0a 2a 2a 20 61 3d 3d   evaluate.** a==
2e6f5 35 20 61 6e 64 20 62 20 49 4e 20 28 31 2c 32 2c  5 and b IN (1,2,
2e6f6 33 29 2e 20 20 54 68 65 20 63 75 72 72 65 6e 74  3).  The current
2e6f7 20 76 61 6c 75 65 73 20 66 6f 72 20 61 20 61 6e   values for a an
2e6f8 64 20 62 20 77 69 6c 6c 20 62 65 20 73 74 6f 72  d b will be stor
2e6f9 65 64 0a 2a 2a 20 69 6e 20 63 6f 6e 73 65 63 75  ed.** in consecu
2e6fa 74 69 76 65 20 72 65 67 69 73 74 65 72 73 20 61  tive registers a
2e6fb 6e 64 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  nd the index of 
2e6fc 74 68 65 20 66 69 72 73 74 20 72 65 67 69 73 74  the first regist
2e6fd 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  er is returned..
2e6fe 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 65 78 61  **.** In the exa
2e6ff 6d 70 6c 65 20 61 62 6f 76 65 20 6e 45 71 3d 3d  mple above nEq==
2e700 32 2e 20 20 42 75 74 20 74 68 69 73 20 73 75 62  2.  But this sub
2e701 72 6f 75 74 69 6e 65 20 77 6f 72 6b 73 20 66 6f  routine works fo
2e702 72 20 61 6e 79 20 76 61 6c 75 65 0a 2a 2a 20 6f  r any value.** o
2e703 66 20 6e 45 71 20 69 6e 63 6c 75 64 69 6e 67 20  f nEq including 
2e704 30 2e 20 20 49 66 20 6e 45 71 3d 3d 30 2c 20 74  0.  If nEq==0, t
2e705 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e  his routine is n
2e706 65 61 72 6c 79 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  early a no-op..*
2e707 2a 20 54 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67  * The only thing
2e708 20 69 74 20 64 6f 65 73 20 69 73 20 61 6c 6c 6f   it does is allo
2e709 63 61 74 65 20 74 68 65 20 70 4c 65 76 65 6c 2d  cate the pLevel-
2e70a 3e 69 4d 65 6d 20 6d 65 6d 6f 72 79 20 63 65 6c  >iMem memory cel
2e70b 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  l..**.** This ro
2e70c 75 74 69 6e 65 20 61 6c 77 61 79 73 20 61 6c 6c  utine always all
2e70d 6f 63 61 74 65 73 20 61 74 20 6c 65 61 73 74 20  ocates at least 
2e70e 6f 6e 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20  one memory cell 
2e70f 61 6e 64 20 72 65 74 75 72 6e 73 0a 2a 2a 20 74  and returns.** t
2e710 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 61 74  he index of that
2e711 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 54 68   memory cell. Th
2e712 65 20 63 6f 64 65 20 74 68 61 74 0a 2a 2a 20 63  e code that.** c
2e713 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e  alls this routin
2e714 65 20 77 69 6c 6c 20 75 73 65 20 74 68 61 74 20  e will use that 
2e715 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20 73  memory cell to s
2e716 74 6f 72 65 20 74 68 65 20 74 65 72 6d 69 6e 61  tore the termina
2e717 74 69 6f 6e 0a 2a 2a 20 6b 65 79 20 76 61 6c 75  tion.** key valu
2e718 65 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 20 20  e of the loop.  
2e719 49 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 49  If one or more I
2e71a 4e 20 6f 70 65 72 61 74 6f 72 73 20 61 70 70 65  N operators appe
2e71b 61 72 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 69 73  ar, then.** this
2e71c 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74   routine allocat
2e71d 65 73 20 61 6e 20 61 64 64 69 74 69 6f 6e 61 6c  es an additional
2e71e 20 6e 45 71 20 6d 65 6d 6f 72 79 20 63 65 6c 6c   nEq memory cell
2e71f 73 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a  s for internal.*
2e720 2a 20 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 42 65 66  * use..**.** Bef
2e721 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 2a  ore returning, *
2e722 70 7a 41 66 66 20 69 73 20 73 65 74 20 74 6f 20  pzAff is set to 
2e723 70 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65  point to a buffe
2e724 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 0a 2a  r containing a.*
2e725 2a 20 63 6f 70 79 20 6f 66 20 74 68 65 20 63 6f  * copy of the co
2e726 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74  lumn affinity st
2e727 72 69 6e 67 20 6f 66 20 74 68 65 20 69 6e 64 65  ring of the inde
2e728 78 20 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e  x allocated usin
2e729 67 0a 2a 2a 20 73 71 6c 69 74 65 33 44 62 4d 61  g.** sqlite3DbMa
2e72a 6c 6c 6f 63 28 29 2e 20 45 78 63 65 70 74 2c 20  lloc(). Except, 
2e72b 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 63  entries in the c
2e72c 6f 70 79 20 6f 66 20 74 68 65 20 73 74 72 69 6e  opy of the strin
2e72d 67 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20  g associated.** 
2e72e 77 69 74 68 20 65 71 75 61 6c 69 74 79 20 63 6f  with equality co
2e72f 6e 73 74 72 61 69 6e 74 73 20 74 68 61 74 20 75  nstraints that u
2e730 73 65 20 4e 4f 4e 45 20 61 66 66 69 6e 69 74 79  se NONE affinity
2e731 20 61 72 65 20 73 65 74 20 74 6f 0a 2a 2a 20 53   are set to.** S
2e732 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 2e 20  QLITE_AFF_NONE. 
2e733 54 68 69 73 20 69 73 20 74 6f 20 64 65 61 6c 20  This is to deal 
2e734 77 69 74 68 20 53 51 4c 20 73 75 63 68 20 61 73  with SQL such as
2e735 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
2e736 2a 2a 0a 2a 2a 20 20 20 43 52 45 41 54 45 20 54  **.**   CREATE T
2e737 41 42 4c 45 20 74 31 28 61 20 54 45 58 54 20 50  ABLE t1(a TEXT P
2e738 52 49 4d 41 52 59 20 4b 45 59 2c 20 62 29 3b 0a  RIMARY KEY, b);.
2e739 2a 2a 20 20 20 53 45 4c 45 43 54 20 2e 2e 2e 20  **   SELECT ... 
2e73a 46 52 4f 4d 20 74 31 20 41 53 20 74 32 2c 20 74  FROM t1 AS t2, t
2e73b 31 20 57 48 45 52 45 20 74 31 2e 61 20 3d 20 74  1 WHERE t1.a = t
2e73c 32 2e 62 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68  2.b;.**.** In th
2e73d 65 20 65 78 61 6d 70 6c 65 20 61 62 6f 76 65 2c  e example above,
2e73e 20 74 68 65 20 69 6e 64 65 78 20 6f 6e 20 74 31   the index on t1
2e73f 28 61 29 20 68 61 73 20 54 45 58 54 20 61 66 66  (a) has TEXT aff
2e740 69 6e 69 74 79 2e 20 42 75 74 20 73 69 6e 63 65  inity. But since
2e741 0a 2a 2a 20 74 68 65 20 72 69 67 68 74 20 68 61  .** the right ha
2e742 6e 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 65  nd side of the e
2e743 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69  quality constrai
2e744 6e 74 20 28 74 32 2e 62 29 20 68 61 73 20 4e 4f  nt (t2.b) has NO
2e745 4e 45 20 61 66 66 69 6e 69 74 79 2c 0a 2a 2a 20  NE affinity,.** 
2e746 6e 6f 20 63 6f 6e 76 65 72 73 69 6f 6e 20 73 68  no conversion sh
2e747 6f 75 6c 64 20 62 65 20 61 74 74 65 6d 70 74 65  ould be attempte
2e748 64 20 62 65 66 6f 72 65 20 75 73 69 6e 67 20 61  d before using a
2e749 20 74 32 2e 62 20 76 61 6c 75 65 20 61 73 20 70   t2.b value as p
2e74a 61 72 74 20 6f 66 0a 2a 2a 20 61 20 6b 65 79 20  art of.** a key 
2e74b 74 6f 20 73 65 61 72 63 68 20 74 68 65 20 69 6e  to search the in
2e74c 64 65 78 2e 20 48 65 6e 63 65 20 74 68 65 20 66  dex. Hence the f
2e74d 69 72 73 74 20 62 79 74 65 20 69 6e 20 74 68 65  irst byte in the
2e74e 20 72 65 74 75 72 6e 65 64 20 61 66 66 69 6e 69   returned affini
2e74f 74 79 0a 2a 2a 20 73 74 72 69 6e 67 20 69 6e 20  ty.** string in 
2e750 74 68 69 73 20 65 78 61 6d 70 6c 65 20 77 6f 75  this example wou
2e751 6c 64 20 62 65 20 73 65 74 20 74 6f 20 53 51 4c  ld be set to SQL
2e752 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 2e 0a 2a 2f  ITE_AFF_NONE..*/
2e753 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 64 65  .static int code
2e754 41 6c 6c 45 71 75 61 6c 69 74 79 54 65 72 6d 73  AllEqualityTerms
2e755 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2e756 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72  e,        /* Par
2e757 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
2e758 20 20 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c    WhereLevel *pL
2e759 65 76 65 6c 2c 20 20 20 2f 2a 20 57 68 69 63 68  evel,   /* Which
2e75a 20 6e 65 73 74 65 64 20 6c 6f 6f 70 20 6f 66 20   nested loop of 
2e75b 74 68 65 20 46 52 4f 4d 20 77 65 20 61 72 65 20  the FROM we are 
2e75c 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 57 68 65 72  coding */.  Wher
2e75d 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 20 20  eClause *pWC,   
2e75e 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63    /* The WHERE c
2e75f 6c 61 75 73 65 20 2a 2f 0a 20 20 42 69 74 6d 61  lause */.  Bitma
2e760 73 6b 20 6e 6f 74 52 65 61 64 79 2c 20 20 20 20  sk notReady,    
2e761 20 2f 2a 20 57 68 69 63 68 20 70 61 72 74 73 20   /* Which parts 
2e762 6f 66 20 46 52 4f 4d 20 68 61 76 65 20 6e 6f 74  of FROM have not
2e763 20 79 65 74 20 62 65 65 6e 20 63 6f 64 65 64 20   yet been coded 
2e764 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 52  */.  int nExtraR
2e765 65 67 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  eg,        /* Nu
2e766 6d 62 65 72 20 6f 66 20 65 78 74 72 61 20 72 65  mber of extra re
2e767 67 69 73 74 65 72 73 20 74 6f 20 61 6c 6c 6f 63  gisters to alloc
2e768 61 74 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a  ate */.  char **
2e769 70 7a 41 66 66 20 20 20 20 20 20 20 20 20 20 2f  pzAff          /
2e76a 2a 20 4f 55 54 3a 20 53 65 74 20 74 6f 20 70 6f  * OUT: Set to po
2e76b 69 6e 74 20 74 6f 20 61 66 66 69 6e 69 74 79 20  int to affinity 
2e76c 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69  string */.){.  i
2e76d 6e 74 20 6e 45 71 20 3d 20 70 4c 65 76 65 6c 2d  nt nEq = pLevel-
2e76e 3e 70 6c 61 6e 2e 6e 45 71 3b 20 20 20 2f 2a 20  >plan.nEq;   /* 
2e76f 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 3d 3d  The number of ==
2e770 20 6f 72 20 49 4e 20 63 6f 6e 73 74 72 61 69 6e   or IN constrain
2e771 74 73 20 74 6f 20 63 6f 64 65 20 2a 2f 0a 20 20  ts to code */.  
2e772 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65  Vdbe *v = pParse
2e773 2d 3e 70 56 64 62 65 3b 20 20 20 20 20 20 2f 2a  ->pVdbe;      /*
2e774 20 54 68 65 20 76 6d 20 75 6e 64 65 72 20 63 6f   The vm under co
2e775 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20  nstruction */.  
2e776 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20  Index *pIdx;    
2e777 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2e778 20 54 68 65 20 69 6e 64 65 78 20 62 65 69 6e 67   The index being
2e779 20 75 73 65 64 20 66 6f 72 20 74 68 69 73 20 6c   used for this l
2e77a 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75  oop */.  int iCu
2e77b 72 20 3d 20 70 4c 65 76 65 6c 2d 3e 69 54 61 62  r = pLevel->iTab
2e77c 43 75 72 3b 20 20 20 2f 2a 20 54 68 65 20 63 75  Cur;   /* The cu
2e77d 72 73 6f 72 20 6f 66 20 74 68 65 20 74 61 62 6c  rsor of the tabl
2e77e 65 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72 6d  e */.  WhereTerm
2e77f 20 2a 70 54 65 72 6d 3b 20 20 20 20 20 20 20 20   *pTerm;        
2e780 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65       /* A single
2e781 20 63 6f 6e 73 74 72 61 69 6e 74 20 74 65 72 6d   constraint term
2e782 20 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20 20 20 20   */.  int j;    
2e783 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e784 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
2e785 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  ter */.  int reg
2e786 42 61 73 65 3b 20 20 20 20 20 20 20 20 20 20 20  Base;           
2e787 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20 72         /* Base r
2e788 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74  egister */.  int
2e789 20 6e 52 65 67 3b 20 20 20 20 20 20 20 20 20 20   nReg;          
2e78a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
2e78b 6d 62 65 72 20 6f 66 20 72 65 67 69 73 74 65 72  mber of register
2e78c 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f  s to allocate */
2e78d 0a 20 20 63 68 61 72 20 2a 7a 41 66 66 3b 20 20  .  char *zAff;  
2e78e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e78f 20 2f 2a 20 41 66 66 69 6e 69 74 79 20 73 74 72   /* Affinity str
2e790 69 6e 67 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f  ing to return */
2e791 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6d 6f 64 75  ..  /* This modu
2e792 6c 65 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65  le is only calle
2e793 64 20 6f 6e 20 71 75 65 72 79 20 70 6c 61 6e 73  d on query plans
2e794 20 74 68 61 74 20 75 73 65 20 61 6e 20 69 6e 64   that use an ind
2e795 65 78 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ex. */.  assert(
2e796 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73   pLevel->plan.ws
2e797 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e  Flags & WHERE_IN
2e798 44 45 58 45 44 20 29 3b 0a 20 20 70 49 64 78 20  DEXED );.  pIdx 
2e799 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75  = pLevel->plan.u
2e79a 2e 70 49 64 78 3b 0a 0a 20 20 2f 2a 20 46 69 67  .pIdx;..  /* Fig
2e79b 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79  ure out how many
2e79c 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 77 65   memory cells we
2e79d 20 77 69 6c 6c 20 6e 65 65 64 20 74 68 65 6e 20   will need then 
2e79e 61 6c 6c 6f 63 61 74 65 20 74 68 65 6d 2e 0a 20  allocate them.. 
2e79f 20 2a 2f 0a 20 20 72 65 67 42 61 73 65 20 3d 20   */.  regBase = 
2e7a0 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 20 31  pParse->nMem + 1
2e7a1 3b 0a 20 20 6e 52 65 67 20 3d 20 70 4c 65 76 65  ;.  nReg = pLeve
2e7a2 6c 2d 3e 70 6c 61 6e 2e 6e 45 71 20 2b 20 6e 45  l->plan.nEq + nE
2e7a3 78 74 72 61 52 65 67 3b 0a 20 20 70 50 61 72 73  xtraReg;.  pPars
2e7a4 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 6e 52 65 67 3b  e->nMem += nReg;
2e7a5 0a 0a 20 20 7a 41 66 66 20 3d 20 73 71 6c 69 74  ..  zAff = sqlit
2e7a6 65 33 44 62 53 74 72 44 75 70 28 70 50 61 72 73  e3DbStrDup(pPars
2e7a7 65 2d 3e 64 62 2c 20 73 71 6c 69 74 65 33 49 6e  e->db, sqlite3In
2e7a8 64 65 78 41 66 66 69 6e 69 74 79 53 74 72 28 76  dexAffinityStr(v
2e7a9 2c 20 70 49 64 78 29 29 3b 0a 20 20 69 66 28 20  , pIdx));.  if( 
2e7aa 21 7a 41 66 66 20 29 7b 0a 20 20 20 20 70 50 61  !zAff ){.    pPa
2e7ab 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46  rse->db->mallocF
2e7ac 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 0a  ailed = 1;.  }..
2e7ad 20 20 2f 2a 20 45 76 61 6c 75 61 74 65 20 74 68    /* Evaluate th
2e7ae 65 20 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74  e equality const
2e7af 72 61 69 6e 74 73 0a 20 20 2a 2f 0a 20 20 61 73  raints.  */.  as
2e7b0 73 65 72 74 28 20 70 49 64 78 2d 3e 6e 43 6f 6c  sert( pIdx->nCol
2e7b1 75 6d 6e 3e 3d 6e 45 71 20 29 3b 0a 20 20 66 6f  umn>=nEq );.  fo
2e7b2 72 28 6a 3d 30 3b 20 6a 3c 6e 45 71 3b 20 6a 2b  r(j=0; j<nEq; j+
2e7b3 2b 29 7b 0a 20 20 20 20 69 6e 74 20 72 31 3b 0a  +){.    int r1;.
2e7b4 20 20 20 20 69 6e 74 20 6b 20 3d 20 70 49 64 78      int k = pIdx
2e7b5 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3b 0a 20  ->aiColumn[j];. 
2e7b6 20 20 20 70 54 65 72 6d 20 3d 20 66 69 6e 64 54     pTerm = findT
2e7b7 65 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 6b  erm(pWC, iCur, k
2e7b8 2c 20 6e 6f 74 52 65 61 64 79 2c 20 70 4c 65 76  , notReady, pLev
2e7b9 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
2e7ba 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 69 66 28  , pIdx);.    if(
2e7bb 20 4e 45 56 45 52 28 70 54 65 72 6d 3d 3d 30 29   NEVER(pTerm==0)
2e7bc 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 61 73   ) break;.    as
2e7bd 73 65 72 74 28 20 28 70 54 65 72 6d 2d 3e 77 74  sert( (pTerm->wt
2e7be 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 43 4f 44  Flags & TERM_COD
2e7bf 45 44 29 3d 3d 30 20 29 3b 0a 20 20 20 20 72 31  ED)==0 );.    r1
2e7c0 20 3d 20 63 6f 64 65 45 71 75 61 6c 69 74 79 54   = codeEqualityT
2e7c1 65 72 6d 28 70 50 61 72 73 65 2c 20 70 54 65 72  erm(pParse, pTer
2e7c2 6d 2c 20 70 4c 65 76 65 6c 2c 20 72 65 67 42 61  m, pLevel, regBa
2e7c3 73 65 2b 6a 29 3b 0a 20 20 20 20 69 66 28 20 72  se+j);.    if( r
2e7c4 31 21 3d 72 65 67 42 61 73 65 2b 6a 20 29 7b 0a  1!=regBase+j ){.
2e7c5 20 20 20 20 20 20 69 66 28 20 6e 52 65 67 3d 3d        if( nReg==
2e7c6 31 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  1 ){.        sql
2e7c7 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
2e7c8 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 42 61  eg(pParse, regBa
2e7c9 73 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65 67  se);.        reg
2e7ca 42 61 73 65 20 3d 20 72 31 3b 0a 20 20 20 20 20  Base = r1;.     
2e7cb 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2e7cc 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2e7cd 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72  2(v, OP_SCopy, r
2e7ce 31 2c 20 72 65 67 42 61 73 65 2b 6a 29 3b 0a 20  1, regBase+j);. 
2e7cf 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
2e7d0 20 74 65 73 74 63 61 73 65 28 20 70 54 65 72 6d   testcase( pTerm
2e7d1 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 57 4f  ->eOperator & WO
2e7d2 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 74  _ISNULL );.    t
2e7d3 65 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e  estcase( pTerm->
2e7d4 65 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 49  eOperator & WO_I
2e7d5 4e 20 29 3b 0a 20 20 20 20 69 66 28 20 28 70 54  N );.    if( (pT
2e7d6 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26  erm->eOperator &
2e7d7 20 28 57 4f 5f 49 53 4e 55 4c 4c 7c 57 4f 5f 49   (WO_ISNULL|WO_I
2e7d8 4e 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  N))==0 ){.      
2e7d9 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2e7da 32 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20  2(v, OP_IsNull, 
2e7db 72 65 67 42 61 73 65 2b 6a 2c 20 70 4c 65 76 65  regBase+j, pLeve
2e7dc 6c 2d 3e 61 64 64 72 42 72 6b 29 3b 0a 20 20 20  l->addrBrk);.   
2e7dd 20 20 20 69 66 28 20 7a 41 66 66 20 0a 20 20 20     if( zAff .   
2e7de 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 43 6f      && sqlite3Co
2e7df 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 70 54  mpareAffinity(pT
2e7e0 65 72 6d 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67  erm->pExpr->pRig
2e7e1 68 74 2c 20 7a 41 66 66 5b 6a 5d 29 3d 3d 53 51  ht, zAff[j])==SQ
2e7e2 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 0a 20 20  LITE_AFF_NONE.  
2e7e3 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 7a      ){.        z
2e7e4 41 66 66 5b 6a 5d 20 3d 20 53 51 4c 49 54 45 5f  Aff[j] = SQLITE_
2e7e5 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20 20  AFF_NONE;.      
2e7e6 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70  }.    }.  }.  *p
2e7e7 7a 41 66 66 20 3d 20 7a 41 66 66 3b 0a 20 20 72  zAff = zAff;.  r
2e7e8 65 74 75 72 6e 20 72 65 67 42 61 73 65 3b 0a 7d  eturn regBase;.}
2e7e9 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
2e7ea 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 73 74   code for the st
2e7eb 61 72 74 20 6f 66 20 74 68 65 20 69 4c 65 76 65  art of the iLeve
2e7ec 6c 2d 74 68 20 6c 6f 6f 70 20 69 6e 20 74 68 65  l-th loop in the
2e7ed 20 57 48 45 52 45 20 63 6c 61 75 73 65 0a 2a 2a   WHERE clause.**
2e7ee 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
2e7ef 64 65 73 63 72 69 62 65 64 20 62 79 20 70 57 49  described by pWI
2e7f0 6e 66 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 42  nfo..*/.static B
2e7f1 69 74 6d 61 73 6b 20 63 6f 64 65 4f 6e 65 4c 6f  itmask codeOneLo
2e7f2 6f 70 53 74 61 72 74 28 0a 20 20 57 68 65 72 65  opStart(.  Where
2e7f3 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 2c 20 20 20  Info *pWInfo,   
2e7f4 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 69 6e 66 6f  /* Complete info
2e7f5 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68  rmation about th
2e7f6 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a  e WHERE clause *
2e7f7 2f 0a 20 20 69 6e 74 20 69 4c 65 76 65 6c 2c 20  /.  int iLevel, 
2e7f8 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 69 63           /* Whic
2e7f9 68 20 6c 65 76 65 6c 20 6f 66 20 70 57 49 6e 66  h level of pWInf
2e7fa 6f 2d 3e 61 5b 5d 20 73 68 6f 75 6c 64 20 62 65  o->a[] should be
2e7fb 20 63 6f 64 65 64 20 2a 2f 0a 20 20 75 31 36 20   coded */.  u16 
2e7fc 77 63 74 72 6c 46 6c 61 67 73 2c 20 20 20 20 20  wctrlFlags,     
2e7fd 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 57   /* One of the W
2e7fe 48 45 52 45 5f 2a 20 66 6c 61 67 73 20 64 65 66  HERE_* flags def
2e7ff 69 6e 65 64 20 69 6e 20 73 71 6c 69 74 65 49 6e  ined in sqliteIn
2e800 74 2e 68 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b  t.h */.  Bitmask
2e801 20 6e 6f 74 52 65 61 64 79 20 20 20 20 20 2f 2a   notReady     /*
2e802 20 57 68 69 63 68 20 74 61 62 6c 65 73 20 61 72   Which tables ar
2e803 65 20 63 75 72 72 65 6e 74 6c 79 20 61 76 61 69  e currently avai
2e804 6c 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e  lable */.){.  in
2e805 74 20 6a 2c 20 6b 3b 20 20 20 20 20 20 20 20 20  t j, k;         
2e806 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
2e807 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75  ers */.  int iCu
2e808 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  r;            /*
2e809 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72   The VDBE cursor
2e80a 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 2a   for the table *
2e80b 2f 0a 20 20 69 6e 74 20 61 64 64 72 4e 78 74 3b  /.  int addrNxt;
2e80c 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72           /* Wher
2e80d 65 20 74 6f 20 6a 75 6d 70 20 74 6f 20 63 6f 6e  e to jump to con
2e80e 74 69 6e 75 65 20 77 69 74 68 20 74 68 65 20 6e  tinue with the n
2e80f 65 78 74 20 49 4e 20 63 61 73 65 20 2a 2f 0a 20  ext IN case */. 
2e810 20 69 6e 74 20 6f 6d 69 74 54 61 62 6c 65 3b 20   int omitTable; 
2e811 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
2e812 20 77 65 20 75 73 65 20 74 68 65 20 69 6e 64 65   we use the inde
2e813 78 20 6f 6e 6c 79 20 2a 2f 0a 20 20 69 6e 74 20  x only */.  int 
2e814 62 52 65 76 3b 20 20 20 20 20 20 20 20 20 20 20  bRev;           
2e815 20 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20 6e   /* True if we n
2e816 65 65 64 20 74 6f 20 73 63 61 6e 20 69 6e 20 72  eed to scan in r
2e817 65 76 65 72 73 65 20 6f 72 64 65 72 20 2a 2f 0a  everse order */.
2e818 20 20 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c    WhereLevel *pL
2e819 65 76 65 6c 3b 20 20 2f 2a 20 54 68 65 20 77 68  evel;  /* The wh
2e81a 65 72 65 20 6c 65 76 65 6c 20 74 6f 20 62 65 20  ere level to be 
2e81b 63 6f 64 65 64 20 2a 2f 0a 20 20 57 68 65 72 65  coded */.  Where
2e81c 43 6c 61 75 73 65 20 2a 70 57 43 3b 20 20 20 20  Clause *pWC;    
2e81d 2f 2a 20 44 65 63 6f 6d 70 6f 73 69 74 69 6f 6e  /* Decomposition
2e81e 20 6f 66 20 74 68 65 20 65 6e 74 69 72 65 20 57   of the entire W
2e81f 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20  HERE clause */. 
2e820 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72   WhereTerm *pTer
2e821 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m;              
2e822 20 2f 2a 20 41 20 57 48 45 52 45 20 63 6c 61 75   /* A WHERE clau
2e823 73 65 20 74 65 72 6d 20 2a 2f 0a 20 20 50 61 72  se term */.  Par
2e824 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20  se *pParse;     
2e825 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2e826 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
2e827 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20  */.  Vdbe *v;   
2e828 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e829 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72 65 70       /* The prep
2e82a 61 72 65 64 20 73 74 6d 74 20 75 6e 64 65 72 20  ared stmt under 
2e82b 63 6f 6e 73 74 72 75 63 74 69 6f 6e 73 20 2a 2f  constructions */
2e82c 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
2e82d 74 5f 69 74 65 6d 20 2a 70 54 61 62 49 74 65 6d  t_item *pTabItem
2e82e 3b 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73  ;  /* FROM claus
2e82f 65 20 74 65 72 6d 20 62 65 69 6e 67 20 63 6f 64  e term being cod
2e830 65 64 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  ed */.  int addr
2e831 42 72 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20  Brk;            
2e832 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20          /* Jump 
2e833 68 65 72 65 20 74 6f 20 62 72 65 61 6b 20 6f 75  here to break ou
2e834 74 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f  t of the loop */
2e835 0a 20 20 69 6e 74 20 61 64 64 72 43 6f 6e 74 3b  .  int addrCont;
2e836 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e837 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20     /* Jump here 
2e838 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68  to continue with
2e839 20 6e 65 78 74 20 63 79 63 6c 65 20 2a 2f 0a 20   next cycle */. 
2e83a 20 69 6e 74 20 69 52 6f 77 69 64 52 65 67 20 3d   int iRowidReg =
2e83b 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 52 6f   0;        /* Ro
2e83c 77 69 64 20 69 73 20 73 74 6f 72 65 64 20 69 6e  wid is stored in
2e83d 20 74 68 69 73 20 72 65 67 69 73 74 65 72 2c 20   this register, 
2e83e 69 66 20 6e 6f 74 20 7a 65 72 6f 20 2a 2f 0a 20  if not zero */. 
2e83f 20 69 6e 74 20 69 52 65 6c 65 61 73 65 52 65 67   int iReleaseReg
2e840 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 54 65   = 0;      /* Te
2e841 6d 70 20 72 65 67 69 73 74 65 72 20 74 6f 20 66  mp register to f
2e842 72 65 65 20 62 65 66 6f 72 65 20 72 65 74 75 72  ree before retur
2e843 6e 69 6e 67 20 2a 2f 0a 0a 20 20 70 50 61 72 73  ning */..  pPars
2e844 65 20 3d 20 70 57 49 6e 66 6f 2d 3e 70 50 61 72  e = pWInfo->pPar
2e845 73 65 3b 0a 20 20 76 20 3d 20 70 50 61 72 73 65  se;.  v = pParse
2e846 2d 3e 70 56 64 62 65 3b 0a 20 20 70 57 43 20 3d  ->pVdbe;.  pWC =
2e847 20 70 57 49 6e 66 6f 2d 3e 70 57 43 3b 0a 20 20   pWInfo->pWC;.  
2e848 70 4c 65 76 65 6c 20 3d 20 26 70 57 49 6e 66 6f  pLevel = &pWInfo
2e849 2d 3e 61 5b 69 4c 65 76 65 6c 5d 3b 0a 20 20 70  ->a[iLevel];.  p
2e84a 54 61 62 49 74 65 6d 20 3d 20 26 70 57 49 6e 66  TabItem = &pWInf
2e84b 6f 2d 3e 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70  o->pTabList->a[p
2e84c 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20  Level->iFrom];. 
2e84d 20 69 43 75 72 20 3d 20 70 54 61 62 49 74 65 6d   iCur = pTabItem
2e84e 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 62 52 65  ->iCursor;.  bRe
2e84f 76 20 3d 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61  v = (pLevel->pla
2e850 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52  n.wsFlags & WHER
2e851 45 5f 52 45 56 45 52 53 45 29 21 3d 30 3b 0a 20  E_REVERSE)!=0;. 
2e852 20 6f 6d 69 74 54 61 62 6c 65 20 3d 20 28 70 4c   omitTable = (pL
2e853 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  evel->plan.wsFla
2e854 67 73 20 26 20 57 48 45 52 45 5f 49 44 58 5f 4f  gs & WHERE_IDX_O
2e855 4e 4c 59 29 21 3d 30 20 0a 20 20 20 20 20 20 20  NLY)!=0 .       
2e856 20 20 20 20 26 26 20 28 77 63 74 72 6c 46 6c 61      && (wctrlFla
2e857 67 73 20 26 20 57 48 45 52 45 5f 46 4f 52 43 45  gs & WHERE_FORCE
2e858 5f 54 41 42 4c 45 29 3d 3d 30 3b 0a 0a 20 20 2f  _TABLE)==0;..  /
2e859 2a 20 43 72 65 61 74 65 20 6c 61 62 65 6c 73 20  * Create labels 
2e85a 66 6f 72 20 74 68 65 20 22 62 72 65 61 6b 22 20  for the "break" 
2e85b 61 6e 64 20 22 63 6f 6e 74 69 6e 75 65 22 20 69  and "continue" i
2e85c 6e 73 74 72 75 63 74 69 6f 6e 73 0a 20 20 2a 2a  nstructions.  **
2e85d 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74   for the current
2e85e 20 6c 6f 6f 70 2e 20 20 4a 75 6d 70 20 74 6f 20   loop.  Jump to 
2e85f 61 64 64 72 42 72 6b 20 74 6f 20 62 72 65 61 6b  addrBrk to break
2e860 20 6f 75 74 20 6f 66 20 61 20 6c 6f 6f 70 2e 0a   out of a loop..
2e861 20 20 2a 2a 20 4a 75 6d 70 20 74 6f 20 63 6f 6e    ** Jump to con
2e862 74 20 74 6f 20 67 6f 20 69 6d 6d 65 64 69 61 74  t to go immediat
2e863 65 6c 79 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ely to the next 
2e864 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65  iteration of the
2e865 0a 20 20 2a 2a 20 6c 6f 6f 70 2e 0a 20 20 2a 2a  .  ** loop..  **
2e866 0a 20 20 2a 2a 20 57 68 65 6e 20 74 68 65 72 65  .  ** When there
2e867 20 69 73 20 61 6e 20 49 4e 20 6f 70 65 72 61 74   is an IN operat
2e868 6f 72 2c 20 77 65 20 61 6c 73 6f 20 68 61 76 65  or, we also have
2e869 20 61 20 22 61 64 64 72 4e 78 74 22 20 6c 61 62   a "addrNxt" lab
2e86a 65 6c 20 74 68 61 74 0a 20 20 2a 2a 20 6d 65 61  el that.  ** mea
2e86b 6e 73 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77  ns to continue w
2e86c 69 74 68 20 74 68 65 20 6e 65 78 74 20 49 4e 20  ith the next IN 
2e86d 76 61 6c 75 65 20 63 6f 6d 62 69 6e 61 74 69 6f  value combinatio
2e86e 6e 2e 20 20 57 68 65 6e 0a 20 20 2a 2a 20 74 68  n.  When.  ** th
2e86f 65 72 65 20 61 72 65 20 6e 6f 20 49 4e 20 6f 70  ere are no IN op
2e870 65 72 61 74 6f 72 73 20 69 6e 20 74 68 65 20 63  erators in the c
2e871 6f 6e 73 74 72 61 69 6e 74 73 2c 20 74 68 65 20  onstraints, the 
2e872 22 61 64 64 72 4e 78 74 22 20 6c 61 62 65 6c 0a  "addrNxt" label.
2e873 20 20 2a 2a 20 69 73 20 74 68 65 20 73 61 6d 65    ** is the same
2e874 20 61 73 20 22 61 64 64 72 42 72 6b 22 2e 0a 20   as "addrBrk".. 
2e875 20 2a 2f 0a 20 20 61 64 64 72 42 72 6b 20 3d 20   */.  addrBrk = 
2e876 70 4c 65 76 65 6c 2d 3e 61 64 64 72 42 72 6b 20  pLevel->addrBrk 
2e877 3d 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 4e 78  = pLevel->addrNx
2e878 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  t = sqlite3VdbeM
2e879 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 61  akeLabel(v);.  a
2e87a 64 64 72 43 6f 6e 74 20 3d 20 70 4c 65 76 65 6c  ddrCont = pLevel
2e87b 2d 3e 61 64 64 72 43 6f 6e 74 20 3d 20 73 71 6c  ->addrCont = sql
2e87c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
2e87d 6c 28 76 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  l(v);..  /* If t
2e87e 68 69 73 20 69 73 20 74 68 65 20 72 69 67 68 74  his is the right
2e87f 20 74 61 62 6c 65 20 6f 66 20 61 20 4c 45 46 54   table of a LEFT
2e880 20 4f 55 54 45 52 20 4a 4f 49 4e 2c 20 61 6c 6c   OUTER JOIN, all
2e881 6f 63 61 74 65 20 61 6e 64 0a 20 20 2a 2a 20 69  ocate and.  ** i
2e882 6e 69 74 69 61 6c 69 7a 65 20 61 20 6d 65 6d 6f  nitialize a memo
2e883 72 79 20 63 65 6c 6c 20 74 68 61 74 20 72 65 63  ry cell that rec
2e884 6f 72 64 73 20 69 66 20 74 68 69 73 20 74 61 62  ords if this tab
2e885 6c 65 20 6d 61 74 63 68 65 73 20 61 6e 79 0a 20  le matches any. 
2e886 20 2a 2a 20 72 6f 77 20 6f 66 20 74 68 65 20 6c   ** row of the l
2e887 65 66 74 20 74 61 62 6c 65 20 6f 66 20 74 68 65  eft table of the
2e888 20 6a 6f 69 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66   join..  */.  if
2e889 28 20 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 3e  ( pLevel->iFrom>
2e88a 30 20 26 26 20 28 70 54 61 62 49 74 65 6d 5b 30  0 && (pTabItem[0
2e88b 5d 2e 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f  ].jointype & JT_
2e88c 4c 45 46 54 29 21 3d 30 20 29 7b 0a 20 20 20 20  LEFT)!=0 ){.    
2e88d 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69  pLevel->iLeftJoi
2e88e 6e 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  n = ++pParse->nM
2e88f 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  em;.    sqlite3V
2e890 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2e891 49 6e 74 65 67 65 72 2c 20 30 2c 20 70 4c 65 76  Integer, 0, pLev
2e892 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 29 3b 0a  el->iLeftJoin);.
2e893 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
2e894 28 76 2c 20 22 69 6e 69 74 20 4c 45 46 54 20 4a  (v, "init LEFT J
2e895 4f 49 4e 20 6e 6f 2d 6d 61 74 63 68 20 66 6c 61  OIN no-match fla
2e896 67 22 29 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64  g"));.  }..#ifnd
2e897 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
2e898 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66  IRTUALTABLE.  if
2e899 28 20 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  (  (pLevel->plan
2e89a 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
2e89b 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29 21 3d  _VIRTUALTABLE)!=
2e89c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 43 61 73 65  0 ){.    /* Case
2e89d 20 30 3a 20 20 54 68 65 20 74 61 62 6c 65 20 69   0:  The table i
2e89e 73 20 61 20 76 69 72 74 75 61 6c 2d 74 61 62 6c  s a virtual-tabl
2e89f 65 2e 20 20 55 73 65 20 74 68 65 20 56 46 69 6c  e.  Use the VFil
2e8a0 74 65 72 20 61 6e 64 20 56 4e 65 78 74 0a 20 20  ter and VNext.  
2e8a1 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f    **          to
2e8a2 20 61 63 63 65 73 73 20 74 68 65 20 64 61 74 61   access the data
2e8a3 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74  ..    */.    int
2e8a4 20 69 52 65 67 3b 20 20 20 2f 2a 20 50 33 20 56   iReg;   /* P3 V
2e8a5 61 6c 75 65 20 66 6f 72 20 4f 50 5f 56 46 69 6c  alue for OP_VFil
2e8a6 74 65 72 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  ter */.    sqlit
2e8a7 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70  e3_index_info *p
2e8a8 56 74 61 62 49 64 78 20 3d 20 70 4c 65 76 65 6c  VtabIdx = pLevel
2e8a9 2d 3e 70 6c 61 6e 2e 75 2e 70 56 74 61 62 49 64  ->plan.u.pVtabId
2e8aa 78 3b 0a 20 20 20 20 69 6e 74 20 6e 43 6f 6e 73  x;.    int nCons
2e8ab 74 72 61 69 6e 74 20 3d 20 70 56 74 61 62 49 64  traint = pVtabId
2e8ac 78 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 0a  x->nConstraint;.
2e8ad 20 20 20 20 73 74 72 75 63 74 20 73 71 6c 69 74      struct sqlit
2e8ae 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61  e3_index_constra
2e8af 69 6e 74 5f 75 73 61 67 65 20 2a 61 55 73 61 67  int_usage *aUsag
2e8b0 65 20 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20  e =.            
2e8b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e8b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e8b3 20 20 20 20 70 56 74 61 62 49 64 78 2d 3e 61 43      pVtabIdx->aC
2e8b4 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 3b 0a  onstraintUsage;.
2e8b5 20 20 20 20 63 6f 6e 73 74 20 73 74 72 75 63 74      const struct
2e8b6 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63   sqlite3_index_c
2e8b7 6f 6e 73 74 72 61 69 6e 74 20 2a 61 43 6f 6e 73  onstraint *aCons
2e8b8 74 72 61 69 6e 74 20 3d 0a 20 20 20 20 20 20 20  traint =.       
2e8b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e8ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e8bb 20 20 20 20 20 20 20 20 20 70 56 74 61 62 49 64           pVtabId
2e8bc 78 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 3b 0a  x->aConstraint;.
2e8bd 0a 20 20 20 20 69 52 65 67 20 3d 20 73 71 6c 69  .    iReg = sqli
2e8be 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28  te3GetTempRange(
2e8bf 70 50 61 72 73 65 2c 20 6e 43 6f 6e 73 74 72 61  pParse, nConstra
2e8c0 69 6e 74 2b 32 29 3b 0a 20 20 20 20 66 6f 72 28  int+2);.    for(
2e8c1 6a 3d 31 3b 20 6a 3c 3d 6e 43 6f 6e 73 74 72 61  j=1; j<=nConstra
2e8c2 69 6e 74 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  int; j++){.     
2e8c3 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 6e 43 6f 6e   for(k=0; k<nCon
2e8c4 73 74 72 61 69 6e 74 3b 20 6b 2b 2b 29 7b 0a 20  straint; k++){. 
2e8c5 20 20 20 20 20 20 20 69 66 28 20 61 55 73 61 67         if( aUsag
2e8c6 65 5b 6b 5d 2e 61 72 67 76 49 6e 64 65 78 3d 3d  e[k].argvIndex==
2e8c7 6a 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  j ){.          i
2e8c8 6e 74 20 69 54 65 72 6d 20 3d 20 61 43 6f 6e 73  nt iTerm = aCons
2e8c9 74 72 61 69 6e 74 5b 6b 5d 2e 69 54 65 72 6d 4f  traint[k].iTermO
2e8ca 66 66 73 65 74 3b 0a 20 20 20 20 20 20 20 20 20  ffset;.         
2e8cb 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
2e8cc 28 70 50 61 72 73 65 2c 20 70 57 43 2d 3e 61 5b  (pParse, pWC->a[
2e8cd 69 54 65 72 6d 5d 2e 70 45 78 70 72 2d 3e 70 52  iTerm].pExpr->pR
2e8ce 69 67 68 74 2c 20 69 52 65 67 2b 6a 2b 31 29 3b  ight, iReg+j+1);
2e8cf 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
2e8d0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2e8d1 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6b 3d    }.      if( k=
2e8d2 3d 6e 43 6f 6e 73 74 72 61 69 6e 74 20 29 20 62  =nConstraint ) b
2e8d3 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
2e8d4 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2e8d5 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
2e8d6 20 70 56 74 61 62 49 64 78 2d 3e 69 64 78 4e 75   pVtabIdx->idxNu
2e8d7 6d 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 73 71  m, iReg);.    sq
2e8d8 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2e8d9 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 6a  v, OP_Integer, j
2e8da 2d 31 2c 20 69 52 65 67 2b 31 29 3b 0a 20 20 20  -1, iReg+1);.   
2e8db 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2e8dc 70 34 28 76 2c 20 4f 50 5f 56 46 69 6c 74 65 72  p4(v, OP_VFilter
2e8dd 2c 20 69 43 75 72 2c 20 61 64 64 72 42 72 6b 2c  , iCur, addrBrk,
2e8de 20 69 52 65 67 2c 20 70 56 74 61 62 49 64 78 2d   iReg, pVtabIdx-
2e8df 3e 69 64 78 53 74 72 2c 0a 20 20 20 20 20 20 20  >idxStr,.       
2e8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
2e8e1 56 74 61 62 49 64 78 2d 3e 6e 65 65 64 54 6f 46  VtabIdx->needToF
2e8e2 72 65 65 49 64 78 53 74 72 20 3f 20 50 34 5f 4d  reeIdxStr ? P4_M
2e8e3 50 52 49 4e 54 46 20 3a 20 50 34 5f 53 54 41 54  PRINTF : P4_STAT
2e8e4 49 43 29 3b 0a 20 20 20 20 70 56 74 61 62 49 64  IC);.    pVtabId
2e8e5 78 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78  x->needToFreeIdx
2e8e6 53 74 72 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72  Str = 0;.    for
2e8e7 28 6a 3d 30 3b 20 6a 3c 6e 43 6f 6e 73 74 72 61  (j=0; j<nConstra
2e8e8 69 6e 74 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  int; j++){.     
2e8e9 20 69 66 28 20 61 55 73 61 67 65 5b 6a 5d 2e 6f   if( aUsage[j].o
2e8ea 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69  mit ){.        i
2e8eb 6e 74 20 69 54 65 72 6d 20 3d 20 61 43 6f 6e 73  nt iTerm = aCons
2e8ec 74 72 61 69 6e 74 5b 6a 5d 2e 69 54 65 72 6d 4f  traint[j].iTermO
2e8ed 66 66 73 65 74 3b 0a 20 20 20 20 20 20 20 20 64  ffset;.        d
2e8ee 69 73 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65  isableTerm(pLeve
2e8ef 6c 2c 20 26 70 57 43 2d 3e 61 5b 69 54 65 72 6d  l, &pWC->a[iTerm
2e8f0 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ]);.      }.    
2e8f1 7d 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70  }.    pLevel->op
2e8f2 20 3d 20 4f 50 5f 56 4e 65 78 74 3b 0a 20 20 20   = OP_VNext;.   
2e8f3 20 70 4c 65 76 65 6c 2d 3e 70 31 20 3d 20 69 43   pLevel->p1 = iC
2e8f4 75 72 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e  ur;.    pLevel->
2e8f5 70 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  p2 = sqlite3Vdbe
2e8f6 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a  CurrentAddr(v);.
2e8f7 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61      sqlite3Relea
2e8f8 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72  seTempRange(pPar
2e8f9 73 65 2c 20 69 52 65 67 2c 20 6e 43 6f 6e 73 74  se, iReg, nConst
2e8fa 72 61 69 6e 74 2b 32 29 3b 0a 20 20 7d 65 6c 73  raint+2);.  }els
2e8fb 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  e.#endif /* SQLI
2e8fc 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
2e8fd 41 42 4c 45 20 2a 2f 0a 0a 20 20 69 66 28 20 70  ABLE */..  if( p
2e8fe 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c  Level->plan.wsFl
2e8ff 61 67 73 20 26 20 57 48 45 52 45 5f 52 4f 57 49  ags & WHERE_ROWI
2e900 44 5f 45 51 20 29 7b 0a 20 20 20 20 2f 2a 20 43  D_EQ ){.    /* C
2e901 61 73 65 20 31 3a 20 20 57 65 20 63 61 6e 20 64  ase 1:  We can d
2e902 69 72 65 63 74 6c 79 20 72 65 66 65 72 65 6e 63  irectly referenc
2e903 65 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 75  e a single row u
2e904 73 69 6e 67 20 61 6e 0a 20 20 20 20 2a 2a 20 20  sing an.    **  
2e905 20 20 20 20 20 20 20 20 65 71 75 61 6c 69 74 79          equality
2e906 20 63 6f 6d 70 61 72 69 73 6f 6e 20 61 67 61 69   comparison agai
2e907 6e 73 74 20 74 68 65 20 52 4f 57 49 44 20 66 69  nst the ROWID fi
2e908 65 6c 64 2e 20 20 4f 72 0a 20 20 20 20 2a 2a 20  eld.  Or.    ** 
2e909 20 20 20 20 20 20 20 20 20 77 65 20 72 65 66 65           we refe
2e90a 72 65 6e 63 65 20 6d 75 6c 74 69 70 6c 65 20 72  rence multiple r
2e90b 6f 77 73 20 75 73 69 6e 67 20 61 20 22 72 6f 77  ows using a "row
2e90c 69 64 20 49 4e 20 28 2e 2e 2e 29 22 0a 20 20 20  id IN (...)".   
2e90d 20 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6e   **          con
2e90e 73 74 72 75 63 74 2e 0a 20 20 20 20 2a 2f 0a 20  struct..    */. 
2e90f 20 20 20 69 52 65 6c 65 61 73 65 52 65 67 20 3d     iReleaseReg =
2e910 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
2e911 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
2e912 70 54 65 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d  pTerm = findTerm
2e913 28 70 57 43 2c 20 69 43 75 72 2c 20 2d 31 2c 20  (pWC, iCur, -1, 
2e914 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f 45 51 7c  notReady, WO_EQ|
2e915 57 4f 5f 49 4e 2c 20 30 29 3b 0a 20 20 20 20 61  WO_IN, 0);.    a
2e916 73 73 65 72 74 28 20 70 54 65 72 6d 21 3d 30 20  ssert( pTerm!=0 
2e917 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
2e918 54 65 72 6d 2d 3e 70 45 78 70 72 21 3d 30 20 29  Term->pExpr!=0 )
2e919 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54  ;.    assert( pT
2e91a 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3d  erm->leftCursor=
2e91b 3d 69 43 75 72 20 29 3b 0a 20 20 20 20 61 73 73  =iCur );.    ass
2e91c 65 72 74 28 20 6f 6d 69 74 54 61 62 6c 65 3d 3d  ert( omitTable==
2e91d 30 20 29 3b 0a 20 20 20 20 69 52 6f 77 69 64 52  0 );.    iRowidR
2e91e 65 67 20 3d 20 63 6f 64 65 45 71 75 61 6c 69 74  eg = codeEqualit
2e91f 79 54 65 72 6d 28 70 50 61 72 73 65 2c 20 70 54  yTerm(pParse, pT
2e920 65 72 6d 2c 20 70 4c 65 76 65 6c 2c 20 69 52 65  erm, pLevel, iRe
2e921 6c 65 61 73 65 52 65 67 29 3b 0a 20 20 20 20 61  leaseReg);.    a
2e922 64 64 72 4e 78 74 20 3d 20 70 4c 65 76 65 6c 2d  ddrNxt = pLevel-
2e923 3e 61 64 64 72 4e 78 74 3b 0a 20 20 20 20 73 71  >addrNxt;.    sq
2e924 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2e925 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c  v, OP_MustBeInt,
2e926 20 69 52 6f 77 69 64 52 65 67 2c 20 61 64 64 72   iRowidReg, addr
2e927 4e 78 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Nxt);.    sqlite
2e928 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2e929 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20 69 43 75  P_NotExists, iCu
2e92a 72 2c 20 61 64 64 72 4e 78 74 2c 20 69 52 6f 77  r, addrNxt, iRow
2e92b 69 64 52 65 67 29 3b 0a 20 20 20 20 73 71 6c 69  idReg);.    sqli
2e92c 74 65 33 45 78 70 72 43 61 63 68 65 53 74 6f 72  te3ExprCacheStor
2e92d 65 28 70 50 61 72 73 65 2c 20 69 43 75 72 2c 20  e(pParse, iCur, 
2e92e 2d 31 2c 20 69 52 6f 77 69 64 52 65 67 29 3b 0a  -1, iRowidReg);.
2e92f 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
2e930 28 76 2c 20 22 70 6b 22 29 29 3b 0a 20 20 20 20  (v, "pk"));.    
2e931 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 4f 50 5f  pLevel->op = OP_
2e932 4e 6f 6f 70 3b 0a 20 20 7d 65 6c 73 65 20 69 66  Noop;.  }else if
2e933 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77  ( pLevel->plan.w
2e934 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 52  sFlags & WHERE_R
2e935 4f 57 49 44 5f 52 41 4e 47 45 20 29 7b 0a 20 20  OWID_RANGE ){.  
2e936 20 20 2f 2a 20 43 61 73 65 20 32 3a 20 20 57 65    /* Case 2:  We
2e937 20 68 61 76 65 20 61 6e 20 69 6e 65 71 75 61 6c   have an inequal
2e938 69 74 79 20 63 6f 6d 70 61 72 69 73 6f 6e 20 61  ity comparison a
2e939 67 61 69 6e 73 74 20 74 68 65 20 52 4f 57 49 44  gainst the ROWID
2e93a 20 66 69 65 6c 64 2e 0a 20 20 20 20 2a 2f 0a 20   field..    */. 
2e93b 20 20 20 69 6e 74 20 74 65 73 74 4f 70 20 3d 20     int testOp = 
2e93c 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20 69 6e 74  OP_Noop;.    int
2e93d 20 73 74 61 72 74 3b 0a 20 20 20 20 69 6e 74 20   start;.    int 
2e93e 6d 65 6d 45 6e 64 56 61 6c 75 65 20 3d 20 30 3b  memEndValue = 0;
2e93f 0a 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a  .    WhereTerm *
2e940 70 53 74 61 72 74 2c 20 2a 70 45 6e 64 3b 0a 0a  pStart, *pEnd;..
2e941 20 20 20 20 61 73 73 65 72 74 28 20 6f 6d 69 74      assert( omit
2e942 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20  Table==0 );.    
2e943 70 53 74 61 72 74 20 3d 20 66 69 6e 64 54 65 72  pStart = findTer
2e944 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 2d 31 2c  m(pWC, iCur, -1,
2e945 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f 47 54   notReady, WO_GT
2e946 7c 57 4f 5f 47 45 2c 20 30 29 3b 0a 20 20 20 20  |WO_GE, 0);.    
2e947 70 45 6e 64 20 3d 20 66 69 6e 64 54 65 72 6d 28  pEnd = findTerm(
2e948 70 57 43 2c 20 69 43 75 72 2c 20 2d 31 2c 20 6e  pWC, iCur, -1, n
2e949 6f 74 52 65 61 64 79 2c 20 57 4f 5f 4c 54 7c 57  otReady, WO_LT|W
2e94a 4f 5f 4c 45 2c 20 30 29 3b 0a 20 20 20 20 69 66  O_LE, 0);.    if
2e94b 28 20 62 52 65 76 20 29 7b 0a 20 20 20 20 20 20  ( bRev ){.      
2e94c 70 54 65 72 6d 20 3d 20 70 53 74 61 72 74 3b 0a  pTerm = pStart;.
2e94d 20 20 20 20 20 20 70 53 74 61 72 74 20 3d 20 70        pStart = p
2e94e 45 6e 64 3b 0a 20 20 20 20 20 20 70 45 6e 64 20  End;.      pEnd 
2e94f 3d 20 70 54 65 72 6d 3b 0a 20 20 20 20 7d 0a 20  = pTerm;.    }. 
2e950 20 20 20 69 66 28 20 70 53 74 61 72 74 20 29 7b     if( pStart ){
2e951 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 58 3b  .      Expr *pX;
2e952 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2e953 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74  The expression t
2e954 68 61 74 20 64 65 66 69 6e 65 73 20 74 68 65 20  hat defines the 
2e955 73 74 61 72 74 20 62 6f 75 6e 64 20 2a 2f 0a 20  start bound */. 
2e956 20 20 20 20 20 69 6e 74 20 72 31 2c 20 72 54 65       int r1, rTe
2e957 6d 70 3b 20 20 20 20 20 20 20 20 2f 2a 20 52 65  mp;        /* Re
2e958 67 69 73 74 65 72 73 20 66 6f 72 20 68 6f 6c 64  gisters for hold
2e959 69 6e 67 20 74 68 65 20 73 74 61 72 74 20 62 6f  ing the start bo
2e95a 75 6e 64 61 72 79 20 2a 2f 0a 0a 20 20 20 20 20  undary */..     
2e95b 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e   /* The followin
2e95c 67 20 63 6f 6e 73 74 61 6e 74 20 6d 61 70 73 20  g constant maps 
2e95d 54 4b 5f 78 78 20 63 6f 64 65 73 20 69 6e 74 6f  TK_xx codes into
2e95e 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 0a   corresponding .
2e95f 20 20 20 20 20 20 2a 2a 20 73 65 65 6b 20 6f 70        ** seek op
2e960 63 6f 64 65 73 2e 20 20 49 74 20 64 65 70 65 6e  codes.  It depen
2e961 64 73 20 6f 6e 20 61 20 70 61 72 74 69 63 75 6c  ds on a particul
2e962 61 72 20 6f 72 64 65 72 69 6e 67 20 6f 66 20 54  ar ordering of T
2e963 4b 5f 78 78 0a 20 20 20 20 20 20 2a 2f 0a 20 20  K_xx.      */.  
2e964 20 20 20 20 63 6f 6e 73 74 20 75 38 20 61 4d 6f      const u8 aMo
2e965 76 65 4f 70 5b 5d 20 3d 20 7b 0a 20 20 20 20 20  veOp[] = {.     
2e966 20 20 20 20 20 20 2f 2a 20 54 4b 5f 47 54 20 2a        /* TK_GT *
2e967 2f 20 20 4f 50 5f 53 65 65 6b 47 74 2c 0a 20 20  /  OP_SeekGt,.  
2e968 20 20 20 20 20 20 20 20 20 2f 2a 20 54 4b 5f 4c           /* TK_L
2e969 45 20 2a 2f 20 20 4f 50 5f 53 65 65 6b 4c 65 2c  E */  OP_SeekLe,
2e96a 0a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  .           /* T
2e96b 4b 5f 4c 54 20 2a 2f 20 20 4f 50 5f 53 65 65 6b  K_LT */  OP_Seek
2e96c 4c 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 2f  Lt,.           /
2e96d 2a 20 54 4b 5f 47 45 20 2a 2f 20 20 4f 50 5f 53  * TK_GE */  OP_S
2e96e 65 65 6b 47 65 0a 20 20 20 20 20 20 7d 3b 0a 20  eekGe.      };. 
2e96f 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
2e970 4c 45 3d 3d 54 4b 5f 47 54 2b 31 20 29 3b 20 20  LE==TK_GT+1 );  
2e971 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65      /* Make sure
2e972 20 74 68 65 20 6f 72 64 65 72 69 6e 67 2e 2e 20   the ordering.. 
2e973 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  */.      assert(
2e974 20 54 4b 5f 4c 54 3d 3d 54 4b 5f 47 54 2b 32 20   TK_LT==TK_GT+2 
2e975 29 3b 20 20 20 20 20 20 2f 2a 20 20 2e 2e 2e 20  );      /*  ... 
2e976 6f 66 20 74 68 65 20 54 4b 5f 78 78 20 76 61 6c  of the TK_xx val
2e977 75 65 73 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 20  ues... */.      
2e978 61 73 73 65 72 74 28 20 54 4b 5f 47 45 3d 3d 54  assert( TK_GE==T
2e979 4b 5f 47 54 2b 33 20 29 3b 20 20 20 20 20 20 2f  K_GT+3 );      /
2e97a 2a 20 20 2e 2e 2e 20 69 73 20 63 6f 72 72 65 63  *  ... is correc
2e97b 63 74 2e 20 2a 2f 0a 0a 20 20 20 20 20 20 70 58  ct. */..      pX
2e97c 20 3d 20 70 53 74 61 72 74 2d 3e 70 45 78 70 72   = pStart->pExpr
2e97d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2e97e 70 58 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 61  pX!=0 );.      a
2e97f 73 73 65 72 74 28 20 70 53 74 61 72 74 2d 3e 6c  ssert( pStart->l
2e980 65 66 74 43 75 72 73 6f 72 3d 3d 69 43 75 72 20  eftCursor==iCur 
2e981 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71  );.      r1 = sq
2e982 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d  lite3ExprCodeTem
2e983 70 28 70 50 61 72 73 65 2c 20 70 58 2d 3e 70 52  p(pParse, pX->pR
2e984 69 67 68 74 2c 20 26 72 54 65 6d 70 29 3b 0a 20  ight, &rTemp);. 
2e985 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2e986 41 64 64 4f 70 33 28 76 2c 20 61 4d 6f 76 65 4f  AddOp3(v, aMoveO
2e987 70 5b 70 58 2d 3e 6f 70 2d 54 4b 5f 47 54 5d 2c  p[pX->op-TK_GT],
2e988 20 69 43 75 72 2c 20 61 64 64 72 42 72 6b 2c 20   iCur, addrBrk, 
2e989 72 31 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43  r1);.      VdbeC
2e98a 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 70 6b 22 29  omment((v, "pk")
2e98b 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2e98c 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74  ExprCacheAffinit
2e98d 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20  yChange(pParse, 
2e98e 72 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71  r1, 1);.      sq
2e98f 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
2e990 52 65 67 28 70 50 61 72 73 65 2c 20 72 54 65 6d  Reg(pParse, rTem
2e991 70 29 3b 0a 20 20 20 20 20 20 64 69 73 61 62 6c  p);.      disabl
2e992 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70 53  eTerm(pLevel, pS
2e993 74 61 72 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  tart);.    }else
2e994 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
2e995 64 62 65 41 64 64 4f 70 32 28 76 2c 20 62 52 65  dbeAddOp2(v, bRe
2e996 76 20 3f 20 4f 50 5f 4c 61 73 74 20 3a 20 4f 50  v ? OP_Last : OP
2e997 5f 52 65 77 69 6e 64 2c 20 69 43 75 72 2c 20 61  _Rewind, iCur, a
2e998 64 64 72 42 72 6b 29 3b 0a 20 20 20 20 7d 0a 20  ddrBrk);.    }. 
2e999 20 20 20 69 66 28 20 70 45 6e 64 20 29 7b 0a 20     if( pEnd ){. 
2e99a 20 20 20 20 20 45 78 70 72 20 2a 70 58 3b 0a 20       Expr *pX;. 
2e99b 20 20 20 20 20 70 58 20 3d 20 70 45 6e 64 2d 3e       pX = pEnd->
2e99c 70 45 78 70 72 3b 0a 20 20 20 20 20 20 61 73 73  pExpr;.      ass
2e99d 65 72 74 28 20 70 58 21 3d 30 20 29 3b 0a 20 20  ert( pX!=0 );.  
2e99e 20 20 20 20 61 73 73 65 72 74 28 20 70 45 6e 64      assert( pEnd
2e99f 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69 43  ->leftCursor==iC
2e9a0 75 72 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d 45  ur );.      memE
2e9a1 6e 64 56 61 6c 75 65 20 3d 20 2b 2b 70 50 61 72  ndValue = ++pPar
2e9a2 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20  se->nMem;.      
2e9a3 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
2e9a4 70 50 61 72 73 65 2c 20 70 58 2d 3e 70 52 69 67  pParse, pX->pRig
2e9a5 68 74 2c 20 6d 65 6d 45 6e 64 56 61 6c 75 65 29  ht, memEndValue)
2e9a6 3b 0a 20 20 20 20 20 20 69 66 28 20 70 58 2d 3e  ;.      if( pX->
2e9a7 6f 70 3d 3d 54 4b 5f 4c 54 20 7c 7c 20 70 58 2d  op==TK_LT || pX-
2e9a8 3e 6f 70 3d 3d 54 4b 5f 47 54 20 29 7b 0a 20 20  >op==TK_GT ){.  
2e9a9 20 20 20 20 20 20 74 65 73 74 4f 70 20 3d 20 62        testOp = b
2e9aa 52 65 76 20 3f 20 4f 50 5f 4c 65 20 3a 20 4f 50  Rev ? OP_Le : OP
2e9ab 5f 47 65 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  _Ge;.      }else
2e9ac 7b 0a 20 20 20 20 20 20 20 20 74 65 73 74 4f 70  {.        testOp
2e9ad 20 3d 20 62 52 65 76 20 3f 20 4f 50 5f 4c 74 20   = bRev ? OP_Lt 
2e9ae 3a 20 4f 50 5f 47 74 3b 0a 20 20 20 20 20 20 7d  : OP_Gt;.      }
2e9af 0a 20 20 20 20 20 20 64 69 73 61 62 6c 65 54 65  .      disableTe
2e9b0 72 6d 28 70 4c 65 76 65 6c 2c 20 70 45 6e 64 29  rm(pLevel, pEnd)
2e9b1 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 74 61 72  ;.    }.    star
2e9b2 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  t = sqlite3VdbeC
2e9b3 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20  urrentAddr(v);. 
2e9b4 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20     pLevel->op = 
2e9b5 62 52 65 76 20 3f 20 4f 50 5f 50 72 65 76 20 3a  bRev ? OP_Prev :
2e9b6 20 4f 50 5f 4e 65 78 74 3b 0a 20 20 20 20 70 4c   OP_Next;.    pL
2e9b7 65 76 65 6c 2d 3e 70 31 20 3d 20 69 43 75 72 3b  evel->p1 = iCur;
2e9b8 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 32 20  .    pLevel->p2 
2e9b9 3d 20 73 74 61 72 74 3b 0a 20 20 20 20 70 4c 65  = start;.    pLe
2e9ba 76 65 6c 2d 3e 70 35 20 3d 20 28 70 53 74 61 72  vel->p5 = (pStar
2e9bb 74 3d 3d 30 20 26 26 20 70 45 6e 64 3d 3d 30 29  t==0 && pEnd==0)
2e9bc 20 3f 31 3a 30 3b 0a 20 20 20 20 69 66 28 20 74   ?1:0;.    if( t
2e9bd 65 73 74 4f 70 21 3d 4f 50 5f 4e 6f 6f 70 20 29  estOp!=OP_Noop )
2e9be 7b 0a 20 20 20 20 20 20 69 52 6f 77 69 64 52 65  {.      iRowidRe
2e9bf 67 20 3d 20 69 52 65 6c 65 61 73 65 52 65 67 20  g = iReleaseReg 
2e9c0 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
2e9c1 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20  Reg(pParse);.   
2e9c2 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2e9c3 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64  dOp2(v, OP_Rowid
2e9c4 2c 20 69 43 75 72 2c 20 69 52 6f 77 69 64 52 65  , iCur, iRowidRe
2e9c5 67 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  g);.      sqlite
2e9c6 33 45 78 70 72 43 61 63 68 65 53 74 6f 72 65 28  3ExprCacheStore(
2e9c7 70 50 61 72 73 65 2c 20 69 43 75 72 2c 20 2d 31  pParse, iCur, -1
2e9c8 2c 20 69 52 6f 77 69 64 52 65 67 29 3b 0a 20 20  , iRowidReg);.  
2e9c9 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2e9ca 64 64 4f 70 33 28 76 2c 20 74 65 73 74 4f 70 2c  ddOp3(v, testOp,
2e9cb 20 6d 65 6d 45 6e 64 56 61 6c 75 65 2c 20 61 64   memEndValue, ad
2e9cc 64 72 42 72 6b 2c 20 69 52 6f 77 69 64 52 65 67  drBrk, iRowidReg
2e9cd 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2e9ce 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20  VdbeChangeP5(v, 
2e9cf 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52  SQLITE_AFF_NUMER
2e9d0 49 43 20 7c 20 53 51 4c 49 54 45 5f 4a 55 4d 50  IC | SQLITE_JUMP
2e9d1 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 7d 0a 20  IFNULL);.    }. 
2e9d2 20 7d 65 6c 73 65 20 69 66 28 20 70 4c 65 76 65   }else if( pLeve
2e9d3 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
2e9d4 26 20 28 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f  & (WHERE_COLUMN_
2e9d5 52 41 4e 47 45 7c 57 48 45 52 45 5f 43 4f 4c 55  RANGE|WHERE_COLU
2e9d6 4d 4e 5f 45 51 29 20 29 7b 0a 20 20 20 20 2f 2a  MN_EQ) ){.    /*
2e9d7 20 43 61 73 65 20 33 3a 20 41 20 73 63 61 6e 20   Case 3: A scan 
2e9d8 75 73 69 6e 67 20 61 6e 20 69 6e 64 65 78 2e 0a  using an index..
2e9d9 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
2e9da 20 20 20 20 20 20 54 68 65 20 57 48 45 52 45 20        The WHERE 
2e9db 63 6c 61 75 73 65 20 6d 61 79 20 63 6f 6e 74 61  clause may conta
2e9dc 69 6e 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20  in zero or more 
2e9dd 65 71 75 61 6c 69 74 79 20 0a 20 20 20 20 2a 2a  equality .    **
2e9de 20 20 20 20 20 20 20 20 20 74 65 72 6d 73 20 28           terms (
2e9df 22 3d 3d 22 20 6f 72 20 22 49 4e 22 20 6f 70 65  "==" or "IN" ope
2e9e0 72 61 74 6f 72 73 29 20 74 68 61 74 20 72 65 66  rators) that ref
2e9e1 65 72 20 74 6f 20 74 68 65 20 4e 0a 20 20 20 20  er to the N.    
2e9e2 2a 2a 20 20 20 20 20 20 20 20 20 6c 65 66 74 2d  **         left-
2e9e3 6d 6f 73 74 20 63 6f 6c 75 6d 6e 73 20 6f 66 20  most columns of 
2e9e4 74 68 65 20 69 6e 64 65 78 2e 20 49 74 20 6d 61  the index. It ma
2e9e5 79 20 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 0a 20  y also contain. 
2e9e6 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 69 6e     **         in
2e9e7 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61  equality constra
2e9e8 69 6e 74 73 20 28 3e 2c 20 3c 2c 20 3e 3d 20 6f  ints (>, <, >= o
2e9e9 72 20 3c 3d 29 20 6f 6e 20 74 68 65 20 69 6e 64  r <=) on the ind
2e9ea 65 78 65 64 0a 20 20 20 20 2a 2a 20 20 20 20 20  exed.    **     
2e9eb 20 20 20 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20      column that 
2e9ec 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c  immediately foll
2e9ed 6f 77 73 20 74 68 65 20 4e 20 65 71 75 61 6c 69  ows the N equali
2e9ee 74 69 65 73 2e 20 4f 6e 6c 79 20 0a 20 20 20 20  ties. Only .    
2e9ef 2a 2a 20 20 20 20 20 20 20 20 20 74 68 65 20 72  **         the r
2e9f0 69 67 68 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e  ight-most column
2e9f1 20 63 61 6e 20 62 65 20 61 6e 20 69 6e 65 71 75   can be an inequ
2e9f2 61 6c 69 74 79 20 2d 20 74 68 65 20 72 65 73 74  ality - the rest
2e9f3 20 6d 75 73 74 0a 20 20 20 20 2a 2a 20 20 20 20   must.    **    
2e9f4 20 20 20 20 20 75 73 65 20 74 68 65 20 22 3d 3d       use the "==
2e9f5 22 20 61 6e 64 20 22 49 4e 22 20 6f 70 65 72 61  " and "IN" opera
2e9f6 74 6f 72 73 2e 20 46 6f 72 20 65 78 61 6d 70 6c  tors. For exampl
2e9f7 65 2c 20 69 66 20 74 68 65 20 0a 20 20 20 20 2a  e, if the .    *
2e9f8 2a 20 20 20 20 20 20 20 20 20 69 6e 64 65 78 20  *         index 
2e9f9 69 73 20 6f 6e 20 28 78 2c 79 2c 7a 29 2c 20 74  is on (x,y,z), t
2e9fa 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  hen the followin
2e9fb 67 20 63 6c 61 75 73 65 73 20 61 72 65 20 61 6c  g clauses are al
2e9fc 6c 20 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  l .    **       
2e9fd 20 20 6f 70 74 69 6d 69 7a 65 64 3a 0a 20 20 20    optimized:.   
2e9fe 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20   **.    **      
2e9ff 20 20 20 20 20 20 78 3d 35 0a 20 20 20 20 2a 2a        x=5.    **
2ea00 20 20 20 20 20 20 20 20 20 20 20 20 78 3d 35 20              x=5 
2ea01 41 4e 44 20 79 3d 31 30 0a 20 20 20 20 2a 2a 20  AND y=10.    ** 
2ea02 20 20 20 20 20 20 20 20 20 20 20 78 3d 35 20 41             x=5 A
2ea03 4e 44 20 79 3c 31 30 0a 20 20 20 20 2a 2a 20 20  ND y<10.    **  
2ea04 20 20 20 20 20 20 20 20 20 20 78 3d 35 20 41 4e            x=5 AN
2ea05 44 20 79 3e 35 20 41 4e 44 20 79 3c 31 30 0a 20  D y>5 AND y<10. 
2ea06 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20     **           
2ea07 20 78 3d 35 20 41 4e 44 20 79 3d 35 20 41 4e 44   x=5 AND y=5 AND
2ea08 20 7a 3c 3d 31 30 0a 20 20 20 20 2a 2a 0a 20 20   z<=10.    **.  
2ea09 20 20 2a 2a 20 20 20 20 20 20 20 20 20 54 68 65    **         The
2ea0a 20 7a 3c 31 30 20 74 65 72 6d 20 6f 66 20 74 68   z<10 term of th
2ea0b 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6e 6e  e following cann
2ea0c 6f 74 20 62 65 20 75 73 65 64 2c 20 6f 6e 6c 79  ot be used, only
2ea0d 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
2ea0e 74 68 65 20 78 3d 35 20 74 65 72 6d 3a 0a 20 20  the x=5 term:.  
2ea0f 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20    **.    **     
2ea10 20 20 20 20 20 20 20 78 3d 35 20 41 4e 44 20 7a         x=5 AND z
2ea11 3c 31 30 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  <10.    **.    *
2ea12 2a 20 20 20 20 20 20 20 20 20 4e 20 6d 61 79 20  *         N may 
2ea13 62 65 20 7a 65 72 6f 20 69 66 20 74 68 65 72 65  be zero if there
2ea14 20 61 72 65 20 69 6e 65 71 75 61 6c 69 74 79 20   are inequality 
2ea15 63 6f 6e 73 74 72 61 69 6e 74 73 2e 0a 20 20 20  constraints..   
2ea16 20 2a 2a 20 20 20 20 20 20 20 20 20 49 66 20 74   **         If t
2ea17 68 65 72 65 20 61 72 65 20 6e 6f 20 69 6e 65 71  here are no ineq
2ea18 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e  uality constrain
2ea19 74 73 2c 20 74 68 65 6e 20 4e 20 69 73 20 61 74  ts, then N is at
2ea1a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
2ea1b 6c 65 61 73 74 20 6f 6e 65 2e 0a 20 20 20 20 2a  least one..    *
2ea1c 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  *.    **        
2ea1d 20 54 68 69 73 20 63 61 73 65 20 69 73 20 61 6c   This case is al
2ea1e 73 6f 20 75 73 65 64 20 77 68 65 6e 20 74 68 65  so used when the
2ea1f 72 65 20 61 72 65 20 6e 6f 20 57 48 45 52 45 20  re are no WHERE 
2ea20 63 6c 61 75 73 65 0a 20 20 20 20 2a 2a 20 20 20  clause.    **   
2ea21 20 20 20 20 20 20 63 6f 6e 73 74 72 61 69 6e 74        constraint
2ea22 73 20 62 75 74 20 61 6e 20 69 6e 64 65 78 20 69  s but an index i
2ea23 73 20 73 65 6c 65 63 74 65 64 20 61 6e 79 77 61  s selected anywa
2ea24 79 2c 20 69 6e 20 6f 72 64 65 72 0a 20 20 20 20  y, in order.    
2ea25 2a 2a 20 20 20 20 20 20 20 20 20 74 6f 20 66 6f  **         to fo
2ea26 72 63 65 20 74 68 65 20 6f 75 74 70 75 74 20 6f  rce the output o
2ea27 72 64 65 72 20 74 6f 20 63 6f 6e 66 6f 72 6d 20  rder to conform 
2ea28 74 6f 20 61 6e 20 4f 52 44 45 52 20 42 59 2e 0a  to an ORDER BY..
2ea29 20 20 20 20 2a 2f 20 20 0a 20 20 20 20 69 6e 74      */  .    int
2ea2a 20 61 53 74 61 72 74 4f 70 5b 5d 20 3d 20 7b 0a   aStartOp[] = {.
2ea2b 20 20 20 20 20 20 30 2c 0a 20 20 20 20 20 20 30        0,.      0
2ea2c 2c 0a 20 20 20 20 20 20 4f 50 5f 52 65 77 69 6e  ,.      OP_Rewin
2ea2d 64 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  d,           /* 
2ea2e 32 3a 20 28 21 73 74 61 72 74 5f 63 6f 6e 73 74  2: (!start_const
2ea2f 72 61 69 6e 74 73 20 26 26 20 73 74 61 72 74 45  raints && startE
2ea30 71 20 26 26 20 20 21 62 52 65 76 29 20 2a 2f 0a  q &&  !bRev) */.
2ea31 20 20 20 20 20 20 4f 50 5f 4c 61 73 74 2c 20 20        OP_Last,  
2ea32 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 33 3a             /* 3:
2ea33 20 28 21 73 74 61 72 74 5f 63 6f 6e 73 74 72 61   (!start_constra
2ea34 69 6e 74 73 20 26 26 20 73 74 61 72 74 45 71 20  ints && startEq 
2ea35 26 26 20 20 20 62 52 65 76 29 20 2a 2f 0a 20 20  &&   bRev) */.  
2ea36 20 20 20 20 4f 50 5f 53 65 65 6b 47 74 2c 20 20      OP_SeekGt,  
2ea37 20 20 20 20 20 20 20 20 20 2f 2a 20 34 3a 20 28           /* 4: (
2ea38 73 74 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74  start_constraint
2ea39 73 20 20 26 26 20 21 73 74 61 72 74 45 71 20 26  s  && !startEq &
2ea3a 26 20 21 62 52 65 76 29 20 2a 2f 0a 20 20 20 20  & !bRev) */.    
2ea3b 20 20 4f 50 5f 53 65 65 6b 4c 74 2c 20 20 20 20    OP_SeekLt,    
2ea3c 20 20 20 20 20 20 20 2f 2a 20 35 3a 20 28 73 74         /* 5: (st
2ea3d 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20  art_constraints 
2ea3e 20 26 26 20 21 73 74 61 72 74 45 71 20 26 26 20   && !startEq && 
2ea3f 20 62 52 65 76 29 20 2a 2f 0a 20 20 20 20 20 20   bRev) */.      
2ea40 4f 50 5f 53 65 65 6b 47 65 2c 20 20 20 20 20 20  OP_SeekGe,      
2ea41 20 20 20 20 20 2f 2a 20 36 3a 20 28 73 74 61 72       /* 6: (star
2ea42 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 20 26  t_constraints  &
2ea43 26 20 20 73 74 61 72 74 45 71 20 26 26 20 21 62  &  startEq && !b
2ea44 52 65 76 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50  Rev) */.      OP
2ea45 5f 53 65 65 6b 4c 65 20 20 20 20 20 20 20 20 20  _SeekLe         
2ea46 20 20 20 2f 2a 20 37 3a 20 28 73 74 61 72 74 5f     /* 7: (start_
2ea47 63 6f 6e 73 74 72 61 69 6e 74 73 20 20 26 26 20  constraints  && 
2ea48 20 73 74 61 72 74 45 71 20 26 26 20 20 62 52 65   startEq &&  bRe
2ea49 76 29 20 2a 2f 0a 20 20 20 20 7d 3b 0a 20 20 20  v) */.    };.   
2ea4a 20 69 6e 74 20 61 45 6e 64 4f 70 5b 5d 20 3d 20   int aEndOp[] = 
2ea4b 7b 0a 20 20 20 20 20 20 4f 50 5f 4e 6f 6f 70 2c  {.      OP_Noop,
2ea4c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2ea4d 30 3a 20 28 21 65 6e 64 5f 63 6f 6e 73 74 72 61  0: (!end_constra
2ea4e 69 6e 74 73 29 20 2a 2f 0a 20 20 20 20 20 20 4f  ints) */.      O
2ea4f 50 5f 49 64 78 47 45 2c 20 20 20 20 20 20 20 20  P_IdxGE,        
2ea50 20 20 20 20 2f 2a 20 31 3a 20 28 65 6e 64 5f 63      /* 1: (end_c
2ea51 6f 6e 73 74 72 61 69 6e 74 73 20 26 26 20 21 62  onstraints && !b
2ea52 52 65 76 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50  Rev) */.      OP
2ea53 5f 49 64 78 4c 54 20 20 20 20 20 20 20 20 20 20  _IdxLT          
2ea54 20 20 20 2f 2a 20 32 3a 20 28 65 6e 64 5f 63 6f     /* 2: (end_co
2ea55 6e 73 74 72 61 69 6e 74 73 20 26 26 20 62 52 65  nstraints && bRe
2ea56 76 29 20 2a 2f 0a 20 20 20 20 7d 3b 0a 20 20 20  v) */.    };.   
2ea57 20 69 6e 74 20 6e 45 71 20 3d 20 70 4c 65 76 65   int nEq = pLeve
2ea58 6c 2d 3e 70 6c 61 6e 2e 6e 45 71 3b 0a 20 20 20  l->plan.nEq;.   
2ea59 20 69 6e 74 20 69 73 4d 69 6e 51 75 65 72 79 20   int isMinQuery 
2ea5a 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  = 0;          /*
2ea5b 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 6f   If this is an o
2ea5c 70 74 69 6d 69 7a 65 64 20 53 45 4c 45 43 54 20  ptimized SELECT 
2ea5d 6d 69 6e 28 78 29 2e 2e 20 2a 2f 0a 20 20 20 20  min(x).. */.    
2ea5e 69 6e 74 20 72 65 67 42 61 73 65 3b 20 20 20 20  int regBase;    
2ea5f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2ea60 42 61 73 65 20 72 65 67 69 73 74 65 72 20 68 6f  Base register ho
2ea61 6c 64 69 6e 67 20 63 6f 6e 73 74 72 61 69 6e 74  lding constraint
2ea62 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 20 20 69   values */.    i
2ea63 6e 74 20 72 31 3b 20 20 20 20 20 20 20 20 20 20  nt r1;          
2ea64 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2ea65 65 6d 70 20 72 65 67 69 73 74 65 72 20 2a 2f 0a  emp register */.
2ea66 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70      WhereTerm *p
2ea67 52 61 6e 67 65 53 74 61 72 74 20 3d 20 30 3b 20  RangeStart = 0; 
2ea68 20 2f 2a 20 49 6e 65 71 75 61 6c 69 74 79 20 63   /* Inequality c
2ea69 6f 6e 73 74 72 61 69 6e 74 20 61 74 20 72 61 6e  onstraint at ran
2ea6a 67 65 20 73 74 61 72 74 20 2a 2f 0a 20 20 20 20  ge start */.    
2ea6b 57 68 65 72 65 54 65 72 6d 20 2a 70 52 61 6e 67  WhereTerm *pRang
2ea6c 65 45 6e 64 20 3d 20 30 3b 20 20 20 20 2f 2a 20  eEnd = 0;    /* 
2ea6d 49 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74  Inequality const
2ea6e 72 61 69 6e 74 20 61 74 20 72 61 6e 67 65 20 65  raint at range e
2ea6f 6e 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 74  nd */.    int st
2ea70 61 72 74 45 71 3b 20 20 20 20 20 20 20 20 20 20  artEq;          
2ea71 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
2ea72 66 20 72 61 6e 67 65 20 73 74 61 72 74 20 75 73  f range start us
2ea73 65 73 20 3d 3d 2c 20 3e 3d 20 6f 72 20 3c 3d 20  es ==, >= or <= 
2ea74 2a 2f 0a 20 20 20 20 69 6e 74 20 65 6e 64 45 71  */.    int endEq
2ea75 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2ea76 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 72      /* True if r
2ea77 61 6e 67 65 20 65 6e 64 20 75 73 65 73 20 3d 3d  ange end uses ==
2ea78 2c 20 3e 3d 20 6f 72 20 3c 3d 20 2a 2f 0a 20 20  , >= or <= */.  
2ea79 20 20 69 6e 74 20 73 74 61 72 74 5f 63 6f 6e 73    int start_cons
2ea7a 74 72 61 69 6e 74 73 3b 20 20 20 20 20 20 20 2f  traints;       /
2ea7b 2a 20 53 74 61 72 74 20 6f 66 20 72 61 6e 67 65  * Start of range
2ea7c 20 69 73 20 63 6f 6e 73 74 72 61 69 6e 65 64 20   is constrained 
2ea7d 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 43 6f 6e 73  */.    int nCons
2ea7e 74 72 61 69 6e 74 3b 20 20 20 20 20 20 20 20 20  traint;         
2ea7f 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
2ea80 20 63 6f 6e 73 74 72 61 69 6e 74 20 74 65 72 6d   constraint term
2ea81 73 20 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a  s */.    Index *
2ea82 70 49 64 78 3b 20 20 20 20 20 20 20 20 20 2f 2a  pIdx;         /*
2ea83 20 54 68 65 20 69 6e 64 65 78 20 77 65 20 77 69   The index we wi
2ea84 6c 6c 20 62 65 20 75 73 69 6e 67 20 2a 2f 0a 20  ll be using */. 
2ea85 20 20 20 69 6e 74 20 69 49 64 78 43 75 72 3b 20     int iIdxCur; 
2ea86 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56          /* The V
2ea87 44 42 45 20 63 75 72 73 6f 72 20 66 6f 72 20 74  DBE cursor for t
2ea88 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20  he index */.    
2ea89 69 6e 74 20 6e 45 78 74 72 61 52 65 67 20 3d 20  int nExtraReg = 
2ea8a 30 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f  0;   /* Number o
2ea8b 66 20 65 78 74 72 61 20 72 65 67 69 73 74 65 72  f extra register
2ea8c 73 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20 20 20  s needed */.    
2ea8d 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20  int op;         
2ea8e 20 20 20 20 20 2f 2a 20 49 6e 73 74 72 75 63 74       /* Instruct
2ea8f 69 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20  ion opcode */.  
2ea90 20 20 63 68 61 72 20 2a 7a 41 66 66 3b 0a 0a 20    char *zAff;.. 
2ea91 20 20 20 70 49 64 78 20 3d 20 70 4c 65 76 65 6c     pIdx = pLevel
2ea92 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 3b 0a 20  ->plan.u.pIdx;. 
2ea93 20 20 20 69 49 64 78 43 75 72 20 3d 20 70 4c 65     iIdxCur = pLe
2ea94 76 65 6c 2d 3e 69 49 64 78 43 75 72 3b 0a 20 20  vel->iIdxCur;.  
2ea95 20 20 6b 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f    k = pIdx->aiCo
2ea96 6c 75 6d 6e 5b 6e 45 71 5d 3b 20 20 20 20 20 2f  lumn[nEq];     /
2ea97 2a 20 43 6f 6c 75 6d 6e 20 66 6f 72 20 69 6e 65  * Column for ine
2ea98 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69  quality constrai
2ea99 6e 74 73 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 49  nts */..    /* I
2ea9a 66 20 74 68 69 73 20 6c 6f 6f 70 20 73 61 74 69  f this loop sati
2ea9b 73 66 69 65 73 20 61 20 73 6f 72 74 20 6f 72 64  sfies a sort ord
2ea9c 65 72 20 28 70 4f 72 64 65 72 42 79 29 20 72 65  er (pOrderBy) re
2ea9d 71 75 65 73 74 20 74 68 61 74 20 0a 20 20 20 20  quest that .    
2ea9e 2a 2a 20 77 61 73 20 70 61 73 73 65 64 20 74 6f  ** was passed to
2ea9f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74   this function t
2eaa0 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 22 53  o implement a "S
2eaa1 45 4c 45 43 54 20 6d 69 6e 28 78 29 20 2e 2e 2e  ELECT min(x) ...
2eaa2 22 20 0a 20 20 20 20 2a 2a 20 71 75 65 72 79 2c  " .    ** query,
2eaa3 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c 65 72   then the caller
2eaa4 20 77 69 6c 6c 20 6f 6e 6c 79 20 61 6c 6c 6f 77   will only allow
2eaa5 20 74 68 65 20 6c 6f 6f 70 20 74 6f 20 72 75 6e   the loop to run
2eaa6 20 66 6f 72 0a 20 20 20 20 2a 2a 20 61 20 73 69   for.    ** a si
2eaa7 6e 67 6c 65 20 69 74 65 72 61 74 69 6f 6e 2e 20  ngle iteration. 
2eaa8 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20  This means that 
2eaa9 74 68 65 20 66 69 72 73 74 20 72 6f 77 20 72 65  the first row re
2eaaa 74 75 72 6e 65 64 0a 20 20 20 20 2a 2a 20 73 68  turned.    ** sh
2eaab 6f 75 6c 64 20 6e 6f 74 20 68 61 76 65 20 61 20  ould not have a 
2eaac 4e 55 4c 4c 20 76 61 6c 75 65 20 73 74 6f 72 65  NULL value store
2eaad 64 20 69 6e 20 27 78 27 2e 20 49 66 20 63 6f 6c  d in 'x'. If col
2eaae 75 6d 6e 20 27 78 27 20 69 73 0a 20 20 20 20 2a  umn 'x' is.    *
2eaaf 2a 20 74 68 65 20 66 69 72 73 74 20 6f 6e 65 20  * the first one 
2eab0 61 66 74 65 72 20 74 68 65 20 6e 45 71 20 65 71  after the nEq eq
2eab1 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e  uality constrain
2eab2 74 73 20 69 6e 20 74 68 65 20 69 6e 64 65 78 2c  ts in the index,
2eab3 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 72 65 71  .    ** this req
2eab4 75 69 72 65 73 20 73 6f 6d 65 20 73 70 65 63 69  uires some speci
2eab5 61 6c 20 68 61 6e 64 6c 69 6e 67 2e 0a 20 20 20  al handling..   
2eab6 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 77 63 74   */.    if( (wct
2eab7 72 6c 46 6c 61 67 73 26 57 48 45 52 45 5f 4f 52  rlFlags&WHERE_OR
2eab8 44 45 52 42 59 5f 4d 49 4e 29 21 3d 30 0a 20 20  DERBY_MIN)!=0.  
2eab9 20 20 20 26 26 20 28 70 4c 65 76 65 6c 2d 3e 70     && (pLevel->p
2eaba 6c 61 6e 2e 77 73 46 6c 61 67 73 26 57 48 45 52  lan.wsFlags&WHER
2eabb 45 5f 4f 52 44 45 52 42 59 29 0a 20 20 20 20 20  E_ORDERBY).     
2eabc 26 26 20 28 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  && (pIdx->nColum
2eabd 6e 3e 6e 45 71 29 0a 20 20 20 20 29 7b 0a 20 20  n>nEq).    ){.  
2eabe 20 20 20 20 2f 2a 20 61 73 73 65 72 74 28 20 70      /* assert( p
2eabf 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3d 3d  OrderBy->nExpr==
2eac0 31 20 29 3b 20 2a 2f 0a 20 20 20 20 20 20 2f 2a  1 ); */.      /*
2eac1 20 61 73 73 65 72 74 28 20 70 4f 72 64 65 72 42   assert( pOrderB
2eac2 79 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 69  y->a[0].pExpr->i
2eac3 43 6f 6c 75 6d 6e 3d 3d 70 49 64 78 2d 3e 61 69  Column==pIdx->ai
2eac4 43 6f 6c 75 6d 6e 5b 6e 45 71 5d 20 29 3b 20 2a  Column[nEq] ); *
2eac5 2f 0a 20 20 20 20 20 20 69 73 4d 69 6e 51 75 65  /.      isMinQue
2eac6 72 79 20 3d 20 31 3b 0a 20 20 20 20 20 20 6e 45  ry = 1;.      nE
2eac7 78 74 72 61 52 65 67 20 3d 20 31 3b 0a 20 20 20  xtraReg = 1;.   
2eac8 20 7d 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20   }..    /* Find 
2eac9 61 6e 79 20 69 6e 65 71 75 61 6c 69 74 79 20 63  any inequality c
2eaca 6f 6e 73 74 72 61 69 6e 74 20 74 65 72 6d 73 20  onstraint terms 
2eacb 66 6f 72 20 74 68 65 20 73 74 61 72 74 20 61 6e  for the start an
2eacc 64 20 65 6e 64 20 0a 20 20 20 20 2a 2a 20 6f 66  d end .    ** of
2eacd 20 74 68 65 20 72 61 6e 67 65 2e 20 0a 20 20 20   the range. .   
2eace 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4c 65 76   */.    if( pLev
2eacf 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
2ead0 20 26 20 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d   & WHERE_TOP_LIM
2ead1 49 54 20 29 7b 0a 20 20 20 20 20 20 70 52 61 6e  IT ){.      pRan
2ead2 67 65 45 6e 64 20 3d 20 66 69 6e 64 54 65 72 6d  geEnd = findTerm
2ead3 28 70 57 43 2c 20 69 43 75 72 2c 20 6b 2c 20 6e  (pWC, iCur, k, n
2ead4 6f 74 52 65 61 64 79 2c 20 28 57 4f 5f 4c 54 7c  otReady, (WO_LT|
2ead5 57 4f 5f 4c 45 29 2c 20 70 49 64 78 29 3b 0a 20  WO_LE), pIdx);. 
2ead6 20 20 20 20 20 6e 45 78 74 72 61 52 65 67 20 3d       nExtraReg =
2ead7 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   1;.    }.    if
2ead8 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77  ( pLevel->plan.w
2ead9 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 42  sFlags & WHERE_B
2eada 54 4d 5f 4c 49 4d 49 54 20 29 7b 0a 20 20 20 20  TM_LIMIT ){.    
2eadb 20 20 70 52 61 6e 67 65 53 74 61 72 74 20 3d 20    pRangeStart = 
2eadc 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43  findTerm(pWC, iC
2eadd 75 72 2c 20 6b 2c 20 6e 6f 74 52 65 61 64 79 2c  ur, k, notReady,
2eade 20 28 57 4f 5f 47 54 7c 57 4f 5f 47 45 29 2c 20   (WO_GT|WO_GE), 
2eadf 70 49 64 78 29 3b 0a 20 20 20 20 20 20 6e 45 78  pIdx);.      nEx
2eae0 74 72 61 52 65 67 20 3d 20 31 3b 0a 20 20 20 20  traReg = 1;.    
2eae1 7d 0a 0a 20 20 20 20 2f 2a 20 47 65 6e 65 72 61  }..    /* Genera
2eae2 74 65 20 63 6f 64 65 20 74 6f 20 65 76 61 6c 75  te code to evalu
2eae3 61 74 65 20 61 6c 6c 20 63 6f 6e 73 74 72 61 69  ate all constrai
2eae4 6e 74 20 74 65 72 6d 73 20 75 73 69 6e 67 20 3d  nt terms using =
2eae5 3d 20 6f 72 20 49 4e 0a 20 20 20 20 2a 2a 20 61  = or IN.    ** a
2eae6 6e 64 20 73 74 6f 72 65 20 74 68 65 20 76 61 6c  nd store the val
2eae7 75 65 73 20 6f 66 20 74 68 6f 73 65 20 74 65 72  ues of those ter
2eae8 6d 73 20 69 6e 20 61 6e 20 61 72 72 61 79 20 6f  ms in an array o
2eae9 66 20 72 65 67 69 73 74 65 72 73 0a 20 20 20 20  f registers.    
2eaea 2a 2a 20 73 74 61 72 74 69 6e 67 20 61 74 20 72  ** starting at r
2eaeb 65 67 42 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20  egBase..    */. 
2eaec 20 20 20 72 65 67 42 61 73 65 20 3d 20 63 6f 64     regBase = cod
2eaed 65 41 6c 6c 45 71 75 61 6c 69 74 79 54 65 72 6d  eAllEqualityTerm
2eaee 73 28 0a 20 20 20 20 20 20 20 20 70 50 61 72 73  s(.        pPars
2eaef 65 2c 20 70 4c 65 76 65 6c 2c 20 70 57 43 2c 20  e, pLevel, pWC, 
2eaf0 6e 6f 74 52 65 61 64 79 2c 20 6e 45 78 74 72 61  notReady, nExtra
2eaf1 52 65 67 2c 20 26 7a 41 66 66 0a 20 20 20 20 29  Reg, &zAff.    )
2eaf2 3b 0a 20 20 20 20 61 64 64 72 4e 78 74 20 3d 20  ;.    addrNxt = 
2eaf3 70 4c 65 76 65 6c 2d 3e 61 64 64 72 4e 78 74 3b  pLevel->addrNxt;
2eaf4 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 61  ..    /* If we a
2eaf5 72 65 20 64 6f 69 6e 67 20 61 20 72 65 76 65 72  re doing a rever
2eaf6 73 65 20 6f 72 64 65 72 20 73 63 61 6e 20 6f 6e  se order scan on
2eaf7 20 61 6e 20 61 73 63 65 6e 64 69 6e 67 20 69 6e   an ascending in
2eaf8 64 65 78 2c 20 6f 72 0a 20 20 20 20 2a 2a 20 61  dex, or.    ** a
2eaf9 20 66 6f 72 77 61 72 64 20 6f 72 64 65 72 20 73   forward order s
2eafa 63 61 6e 20 6f 6e 20 61 20 64 65 73 63 65 6e 64  can on a descend
2eafb 69 6e 67 20 69 6e 64 65 78 2c 20 69 6e 74 65 72  ing index, inter
2eafc 63 68 61 6e 67 65 20 74 68 65 20 0a 20 20 20 20  change the .    
2eafd 2a 2a 20 73 74 61 72 74 20 61 6e 64 20 65 6e 64  ** start and end
2eafe 20 74 65 72 6d 73 20 28 70 52 61 6e 67 65 53 74   terms (pRangeSt
2eaff 61 72 74 20 61 6e 64 20 70 52 61 6e 67 65 45 6e  art and pRangeEn
2eb00 64 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  d)..    */.    i
2eb01 66 28 20 62 52 65 76 3d 3d 28 70 49 64 78 2d 3e  f( bRev==(pIdx->
2eb02 61 53 6f 72 74 4f 72 64 65 72 5b 6e 45 71 5d 3d  aSortOrder[nEq]=
2eb03 3d 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43 29 20  =SQLITE_SO_ASC) 
2eb04 29 7b 0a 20 20 20 20 20 20 53 57 41 50 28 57 68  ){.      SWAP(Wh
2eb05 65 72 65 54 65 72 6d 20 2a 2c 20 70 52 61 6e 67  ereTerm *, pRang
2eb06 65 45 6e 64 2c 20 70 52 61 6e 67 65 53 74 61 72  eEnd, pRangeStar
2eb07 74 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 74  t);.    }..    t
2eb08 65 73 74 63 61 73 65 28 20 70 52 61 6e 67 65 53  estcase( pRangeS
2eb09 74 61 72 74 20 26 26 20 70 52 61 6e 67 65 53 74  tart && pRangeSt
2eb0a 61 72 74 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26  art->eOperator &
2eb0b 20 57 4f 5f 4c 45 20 29 3b 0a 20 20 20 20 74 65   WO_LE );.    te
2eb0c 73 74 63 61 73 65 28 20 70 52 61 6e 67 65 53 74  stcase( pRangeSt
2eb0d 61 72 74 20 26 26 20 70 52 61 6e 67 65 53 74 61  art && pRangeSta
2eb0e 72 74 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20  rt->eOperator & 
2eb0f 57 4f 5f 47 45 20 29 3b 0a 20 20 20 20 74 65 73  WO_GE );.    tes
2eb10 74 63 61 73 65 28 20 70 52 61 6e 67 65 45 6e 64  tcase( pRangeEnd
2eb11 20 26 26 20 70 52 61 6e 67 65 45 6e 64 2d 3e 65   && pRangeEnd->e
2eb12 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 4c 45  Operator & WO_LE
2eb13 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65   );.    testcase
2eb14 28 20 70 52 61 6e 67 65 45 6e 64 20 26 26 20 70  ( pRangeEnd && p
2eb15 52 61 6e 67 65 45 6e 64 2d 3e 65 4f 70 65 72 61  RangeEnd->eOpera
2eb16 74 6f 72 20 26 20 57 4f 5f 47 45 20 29 3b 0a 20  tor & WO_GE );. 
2eb17 20 20 20 73 74 61 72 74 45 71 20 3d 20 21 70 52     startEq = !pR
2eb18 61 6e 67 65 53 74 61 72 74 20 7c 7c 20 70 52 61  angeStart || pRa
2eb19 6e 67 65 53 74 61 72 74 2d 3e 65 4f 70 65 72 61  ngeStart->eOpera
2eb1a 74 6f 72 20 26 20 28 57 4f 5f 4c 45 7c 57 4f 5f  tor & (WO_LE|WO_
2eb1b 47 45 29 3b 0a 20 20 20 20 65 6e 64 45 71 20 3d  GE);.    endEq =
2eb1c 20 20 20 21 70 52 61 6e 67 65 45 6e 64 20 7c 7c     !pRangeEnd ||
2eb1d 20 70 52 61 6e 67 65 45 6e 64 2d 3e 65 4f 70 65   pRangeEnd->eOpe
2eb1e 72 61 74 6f 72 20 26 20 28 57 4f 5f 4c 45 7c 57  rator & (WO_LE|W
2eb1f 4f 5f 47 45 29 3b 0a 20 20 20 20 73 74 61 72 74  O_GE);.    start
2eb20 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 3d 20 70  _constraints = p
2eb21 52 61 6e 67 65 53 74 61 72 74 20 7c 7c 20 6e 45  RangeStart || nE
2eb22 71 3e 30 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 65  q>0;..    /* See
2eb23 6b 20 74 68 65 20 69 6e 64 65 78 20 63 75 72 73  k the index curs
2eb24 6f 72 20 74 6f 20 74 68 65 20 73 74 61 72 74 20  or to the start 
2eb25 6f 66 20 74 68 65 20 72 61 6e 67 65 2e 20 2a 2f  of the range. */
2eb26 0a 20 20 20 20 6e 43 6f 6e 73 74 72 61 69 6e 74  .    nConstraint
2eb27 20 3d 20 6e 45 71 3b 0a 20 20 20 20 69 66 28 20   = nEq;.    if( 
2eb28 70 52 61 6e 67 65 53 74 61 72 74 20 29 7b 0a 20  pRangeStart ){. 
2eb29 20 20 20 20 20 45 78 70 72 20 2a 70 52 69 67 68       Expr *pRigh
2eb2a 74 20 3d 20 70 52 61 6e 67 65 53 74 61 72 74 2d  t = pRangeStart-
2eb2b 3e 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a  >pExpr->pRight;.
2eb2c 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2eb2d 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 52  rCode(pParse, pR
2eb2e 69 67 68 74 2c 20 72 65 67 42 61 73 65 2b 6e 45  ight, regBase+nE
2eb2f 71 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  q);.      sqlite
2eb30 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2eb31 50 5f 49 73 4e 75 6c 6c 2c 20 72 65 67 42 61 73  P_IsNull, regBas
2eb32 65 2b 6e 45 71 2c 20 61 64 64 72 4e 78 74 29 3b  e+nEq, addrNxt);
2eb33 0a 20 20 20 20 20 20 69 66 28 20 7a 41 66 66 20  .      if( zAff 
2eb34 0a 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74  .       && sqlit
2eb35 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74  e3CompareAffinit
2eb36 79 28 70 52 69 67 68 74 2c 20 7a 41 66 66 5b 6e  y(pRight, zAff[n
2eb37 43 6f 6e 73 74 72 61 69 6e 74 5d 29 3d 3d 53 51  Constraint])==SQ
2eb38 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 0a 20 20  LITE_AFF_NONE.  
2eb39 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 2f      ){.        /
2eb3a 2a 20 53 69 6e 63 65 20 74 68 65 20 63 6f 6d 70  * Since the comp
2eb3b 61 72 69 73 6f 6e 20 69 73 20 74 6f 20 62 65 20  arison is to be 
2eb3c 70 65 72 66 6f 72 6d 65 64 20 77 69 74 68 20 6e  performed with n
2eb3d 6f 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 61 70  o conversions ap
2eb3e 70 6c 69 65 64 0a 20 20 20 20 20 20 20 20 2a 2a  plied.        **
2eb3f 20 74 6f 20 74 68 65 20 6f 70 65 72 61 6e 64 73   to the operands
2eb40 2c 20 73 65 74 20 74 68 65 20 61 66 66 69 6e 69  , set the affini
2eb41 74 79 20 74 6f 20 61 70 70 6c 79 20 74 6f 20 70  ty to apply to p
2eb42 52 69 67 68 74 20 74 6f 20 0a 20 20 20 20 20 20  Right to .      
2eb43 20 20 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f    ** SQLITE_AFF_
2eb44 4e 4f 4e 45 2e 20 20 2a 2f 0a 20 20 20 20 20 20  NONE.  */.      
2eb45 20 20 7a 41 66 66 5b 6e 43 6f 6e 73 74 72 61 69    zAff[nConstrai
2eb46 6e 74 5d 20 3d 20 53 51 4c 49 54 45 5f 41 46 46  nt] = SQLITE_AFF
2eb47 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 0a 20  _NONE;.      }. 
2eb48 20 20 20 20 20 6e 43 6f 6e 73 74 72 61 69 6e 74       nConstraint
2eb49 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  ++;.    }else if
2eb4a 28 20 69 73 4d 69 6e 51 75 65 72 79 20 29 7b 0a  ( isMinQuery ){.
2eb4b 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2eb4c 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75  eAddOp2(v, OP_Nu
2eb4d 6c 6c 2c 20 30 2c 20 72 65 67 42 61 73 65 2b 6e  ll, 0, regBase+n
2eb4e 45 71 29 3b 0a 20 20 20 20 20 20 6e 43 6f 6e 73  Eq);.      nCons
2eb4f 74 72 61 69 6e 74 2b 2b 3b 0a 20 20 20 20 20 20  traint++;.      
2eb50 73 74 61 72 74 45 71 20 3d 20 30 3b 0a 20 20 20  startEq = 0;.   
2eb51 20 20 20 73 74 61 72 74 5f 63 6f 6e 73 74 72 61     start_constra
2eb52 69 6e 74 73 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  ints = 1;.    }.
2eb53 20 20 20 20 63 6f 64 65 41 70 70 6c 79 41 66 66      codeApplyAff
2eb54 69 6e 69 74 79 28 70 50 61 72 73 65 2c 20 72 65  inity(pParse, re
2eb55 67 42 61 73 65 2c 20 6e 43 6f 6e 73 74 72 61 69  gBase, nConstrai
2eb56 6e 74 2c 20 7a 41 66 66 29 3b 0a 20 20 20 20 6f  nt, zAff);.    o
2eb57 70 20 3d 20 61 53 74 61 72 74 4f 70 5b 28 73 74  p = aStartOp[(st
2eb58 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 3c  art_constraints<
2eb59 3c 32 29 20 2b 20 28 73 74 61 72 74 45 71 3c 3c  <2) + (startEq<<
2eb5a 31 29 20 2b 20 62 52 65 76 5d 3b 0a 20 20 20 20  1) + bRev];.    
2eb5b 61 73 73 65 72 74 28 20 6f 70 21 3d 30 20 29 3b  assert( op!=0 );
2eb5c 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f  .    testcase( o
2eb5d 70 3d 3d 4f 50 5f 52 65 77 69 6e 64 20 29 3b 0a  p==OP_Rewind );.
2eb5e 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
2eb5f 3d 3d 4f 50 5f 4c 61 73 74 20 29 3b 0a 20 20 20  ==OP_Last );.   
2eb60 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f   testcase( op==O
2eb61 50 5f 53 65 65 6b 47 74 20 29 3b 0a 20 20 20 20  P_SeekGt );.    
2eb62 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50  testcase( op==OP
2eb63 5f 53 65 65 6b 47 65 20 29 3b 0a 20 20 20 20 74  _SeekGe );.    t
2eb64 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f  estcase( op==OP_
2eb65 53 65 65 6b 4c 65 20 29 3b 0a 20 20 20 20 74 65  SeekLe );.    te
2eb66 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 53  stcase( op==OP_S
2eb67 65 65 6b 4c 74 20 29 3b 0a 20 20 20 20 73 71 6c  eekLt );.    sql
2eb68 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
2eb69 2c 20 6f 70 2c 20 69 49 64 78 43 75 72 2c 20 61  , op, iIdxCur, a
2eb6a 64 64 72 4e 78 74 2c 20 72 65 67 42 61 73 65 2c  ddrNxt, regBase,
2eb6b 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
2eb6c 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49          SQLITE_I
2eb6d 4e 54 5f 54 4f 5f 50 54 52 28 6e 43 6f 6e 73 74  NT_TO_PTR(nConst
2eb6e 72 61 69 6e 74 29 2c 20 50 34 5f 49 4e 54 33 32  raint), P4_INT32
2eb6f 29 3b 0a 0a 20 20 20 20 2f 2a 20 4c 6f 61 64 20  );..    /* Load 
2eb70 74 68 65 20 76 61 6c 75 65 20 66 6f 72 20 74 68  the value for th
2eb71 65 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e  e inequality con
2eb72 73 74 72 61 69 6e 74 20 61 74 20 74 68 65 20 65  straint at the e
2eb73 6e 64 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a  nd of the.    **
2eb74 20 72 61 6e 67 65 20 28 69 66 20 61 6e 79 29 2e   range (if any).
2eb75 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6e 43 6f 6e  .    */.    nCon
2eb76 73 74 72 61 69 6e 74 20 3d 20 6e 45 71 3b 0a 20  straint = nEq;. 
2eb77 20 20 20 69 66 28 20 70 52 61 6e 67 65 45 6e 64     if( pRangeEnd
2eb78 20 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a   ){.      Expr *
2eb79 70 52 69 67 68 74 20 3d 20 70 52 61 6e 67 65 45  pRight = pRangeE
2eb7a 6e 64 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67 68  nd->pExpr->pRigh
2eb7b 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  t;.      sqlite3
2eb7c 45 78 70 72 43 61 63 68 65 52 65 6d 6f 76 65 28  ExprCacheRemove(
2eb7d 70 50 61 72 73 65 2c 20 72 65 67 42 61 73 65 2b  pParse, regBase+
2eb7e 6e 45 71 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  nEq);.      sqli
2eb7f 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72  te3ExprCode(pPar
2eb80 73 65 2c 20 70 52 69 67 68 74 2c 20 72 65 67 42  se, pRight, regB
2eb81 61 73 65 2b 6e 45 71 29 3b 0a 20 20 20 20 20 20  ase+nEq);.      
2eb82 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2eb83 32 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20  2(v, OP_IsNull, 
2eb84 72 65 67 42 61 73 65 2b 6e 45 71 2c 20 61 64 64  regBase+nEq, add
2eb85 72 4e 78 74 29 3b 0a 20 20 20 20 20 20 7a 41 66  rNxt);.      zAf
2eb86 66 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  f = sqlite3DbStr
2eb87 44 75 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  Dup(pParse->db, 
2eb88 7a 41 66 66 29 3b 0a 20 20 20 20 20 20 69 66 28  zAff);.      if(
2eb89 20 7a 41 66 66 20 0a 20 20 20 20 20 20 20 26 26   zAff .       &&
2eb8a 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41   sqlite3CompareA
2eb8b 66 66 69 6e 69 74 79 28 70 52 69 67 68 74 2c 20  ffinity(pRight, 
2eb8c 7a 41 66 66 5b 6e 43 6f 6e 73 74 72 61 69 6e 74  zAff[nConstraint
2eb8d 5d 29 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e  ])==SQLITE_AFF_N
2eb8e 4f 4e 45 0a 20 20 20 20 20 20 29 7b 0a 20 20 20  ONE.      ){.   
2eb8f 20 20 20 20 20 2f 2a 20 53 69 6e 63 65 20 74 68       /* Since th
2eb90 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20  e comparison is 
2eb91 74 6f 20 62 65 20 70 65 72 66 6f 72 6d 65 64 20  to be performed 
2eb92 77 69 74 68 20 6e 6f 20 63 6f 6e 76 65 72 73 69  with no conversi
2eb93 6f 6e 73 20 61 70 70 6c 69 65 64 0a 20 20 20 20  ons applied.    
2eb94 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 6f 70      ** to the op
2eb95 65 72 61 6e 64 73 2c 20 73 65 74 20 74 68 65 20  erands, set the 
2eb96 61 66 66 69 6e 69 74 79 20 74 6f 20 61 70 70 6c  affinity to appl
2eb97 79 20 74 6f 20 70 52 69 67 68 74 20 74 6f 20 0a  y to pRight to .
2eb98 20 20 20 20 20 20 20 20 2a 2a 20 53 51 4c 49 54          ** SQLIT
2eb99 45 5f 41 46 46 5f 4e 4f 4e 45 2e 20 20 2a 2f 0a  E_AFF_NONE.  */.
2eb9a 20 20 20 20 20 20 20 20 7a 41 66 66 5b 6e 43 6f          zAff[nCo
2eb9b 6e 73 74 72 61 69 6e 74 5d 20 3d 20 53 51 4c 49  nstraint] = SQLI
2eb9c 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20  TE_AFF_NONE;.   
2eb9d 20 20 20 7d 0a 20 20 20 20 20 20 63 6f 64 65 41     }.      codeA
2eb9e 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 50 61  pplyAffinity(pPa
2eb9f 72 73 65 2c 20 72 65 67 42 61 73 65 2c 20 6e 45  rse, regBase, nE
2eba0 71 2b 31 2c 20 7a 41 66 66 29 3b 0a 20 20 20 20  q+1, zAff);.    
2eba1 20 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b 2b 3b    nConstraint++;
2eba2 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54  .    }..    /* T
2eba3 6f 70 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 62  op of the loop b
2eba4 6f 64 79 20 2a 2f 0a 20 20 20 20 70 4c 65 76 65  ody */.    pLeve
2eba5 6c 2d 3e 70 32 20 3d 20 73 71 6c 69 74 65 33 56  l->p2 = sqlite3V
2eba6 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
2eba7 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b  );..    /* Check
2eba8 20 69 66 20 74 68 65 20 69 6e 64 65 78 20 63 75   if the index cu
2eba9 72 73 6f 72 20 69 73 20 70 61 73 74 20 74 68 65  rsor is past the
2ebaa 20 65 6e 64 20 6f 66 20 74 68 65 20 72 61 6e 67   end of the rang
2ebab 65 2e 20 2a 2f 0a 20 20 20 20 6f 70 20 3d 20 61  e. */.    op = a
2ebac 45 6e 64 4f 70 5b 28 70 52 61 6e 67 65 45 6e 64  EndOp[(pRangeEnd
2ebad 20 7c 7c 20 6e 45 71 29 20 2a 20 28 31 20 2b 20   || nEq) * (1 + 
2ebae 62 52 65 76 29 5d 3b 0a 20 20 20 20 74 65 73 74  bRev)];.    test
2ebaf 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 4e 6f 6f  case( op==OP_Noo
2ebb0 70 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73  p );.    testcas
2ebb1 65 28 20 6f 70 3d 3d 4f 50 5f 49 64 78 47 45 20  e( op==OP_IdxGE 
2ebb2 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
2ebb3 20 6f 70 3d 3d 4f 50 5f 49 64 78 4c 54 20 29 3b   op==OP_IdxLT );
2ebb4 0a 20 20 20 20 69 66 28 20 6f 70 21 3d 4f 50 5f  .    if( op!=OP_
2ebb5 4e 6f 6f 70 20 29 7b 0a 20 20 20 20 20 20 73 71  Noop ){.      sq
2ebb6 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
2ebb7 76 2c 20 6f 70 2c 20 69 49 64 78 43 75 72 2c 20  v, op, iIdxCur, 
2ebb8 61 64 64 72 4e 78 74 2c 20 72 65 67 42 61 73 65  addrNxt, regBase
2ebb9 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2ebba 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
2ebbb 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 6e 43 6f 6e  _INT_TO_PTR(nCon
2ebbc 73 74 72 61 69 6e 74 29 2c 20 50 34 5f 49 4e 54  straint), P4_INT
2ebbd 33 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  32);.      sqlit
2ebbe 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76  e3VdbeChangeP5(v
2ebbf 2c 20 65 6e 64 45 71 21 3d 62 52 65 76 20 3f 31  , endEq!=bRev ?1
2ebc0 3a 30 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  :0);.    }..    
2ebc1 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20  /* If there are 
2ebc2 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74  inequality const
2ebc3 72 61 69 6e 74 73 2c 20 63 68 65 63 6b 20 74 68  raints, check th
2ebc4 61 74 20 74 68 65 20 76 61 6c 75 65 0a 20 20 20  at the value.   
2ebc5 20 2a 2a 20 6f 66 20 74 68 65 20 74 61 62 6c 65   ** of the table
2ebc6 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 74 68 65   column that the
2ebc7 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 74   inequality cont
2ebc8 72 61 69 6e 73 20 69 73 20 6e 6f 74 20 4e 55 4c  rains is not NUL
2ebc9 4c 2e 0a 20 20 20 20 2a 2a 20 49 66 20 69 74 20  L..    ** If it 
2ebca 69 73 2c 20 6a 75 6d 70 20 74 6f 20 74 68 65 20  is, jump to the 
2ebcb 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f  next iteration o
2ebcc 66 20 74 68 65 20 6c 6f 6f 70 2e 0a 20 20 20 20  f the loop..    
2ebcd 2a 2f 0a 20 20 20 20 72 31 20 3d 20 73 71 6c 69  */.    r1 = sqli
2ebce 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
2ebcf 61 72 73 65 29 3b 0a 20 20 20 20 74 65 73 74 63  arse);.    testc
2ebd0 61 73 65 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61  ase( pLevel->pla
2ebd1 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52  n.wsFlags & WHER
2ebd2 45 5f 42 54 4d 5f 4c 49 4d 49 54 20 29 3b 0a 20  E_BTM_LIMIT );. 
2ebd3 20 20 20 74 65 73 74 63 61 73 65 28 20 70 4c 65     testcase( pLe
2ebd4 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  vel->plan.wsFlag
2ebd5 73 20 26 20 57 48 45 52 45 5f 54 4f 50 5f 4c 49  s & WHERE_TOP_LI
2ebd6 4d 49 54 20 29 3b 0a 20 20 20 20 69 66 28 20 70  MIT );.    if( p
2ebd7 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c  Level->plan.wsFl
2ebd8 61 67 73 20 26 20 28 57 48 45 52 45 5f 42 54 4d  ags & (WHERE_BTM
2ebd9 5f 4c 49 4d 49 54 7c 57 48 45 52 45 5f 54 4f 50  _LIMIT|WHERE_TOP
2ebda 5f 4c 49 4d 49 54 29 20 29 7b 0a 20 20 20 20 20  _LIMIT) ){.     
2ebdb 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2ebdc 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c  p3(v, OP_Column,
2ebdd 20 69 49 64 78 43 75 72 2c 20 6e 45 71 2c 20 72   iIdxCur, nEq, r
2ebde 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
2ebdf 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2ebe0 50 5f 49 73 4e 75 6c 6c 2c 20 72 31 2c 20 61 64  P_IsNull, r1, ad
2ebe1 64 72 43 6f 6e 74 29 3b 0a 20 20 20 20 7d 0a 20  drCont);.    }. 
2ebe2 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
2ebe3 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
2ebe4 20 72 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65   r1);..    /* Se
2ebe5 65 6b 20 74 68 65 20 74 61 62 6c 65 20 63 75 72  ek the table cur
2ebe6 73 6f 72 2c 20 69 66 20 72 65 71 75 69 72 65 64  sor, if required
2ebe7 20 2a 2f 0a 20 20 20 20 64 69 73 61 62 6c 65 54   */.    disableT
2ebe8 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70 52 61 6e  erm(pLevel, pRan
2ebe9 67 65 53 74 61 72 74 29 3b 0a 20 20 20 20 64 69  geStart);.    di
2ebea 73 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c  sableTerm(pLevel
2ebeb 2c 20 70 52 61 6e 67 65 45 6e 64 29 3b 0a 20 20  , pRangeEnd);.  
2ebec 20 20 69 66 28 20 21 6f 6d 69 74 54 61 62 6c 65    if( !omitTable
2ebed 20 29 7b 0a 20 20 20 20 20 20 69 52 6f 77 69 64   ){.      iRowid
2ebee 52 65 67 20 3d 20 69 52 65 6c 65 61 73 65 52 65  Reg = iReleaseRe
2ebef 67 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  g = sqlite3GetTe
2ebf0 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
2ebf1 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2ebf2 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78  AddOp2(v, OP_Idx
2ebf3 52 6f 77 69 64 2c 20 69 49 64 78 43 75 72 2c 20  Rowid, iIdxCur, 
2ebf4 69 52 6f 77 69 64 52 65 67 29 3b 0a 20 20 20 20  iRowidReg);.    
2ebf5 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
2ebf6 68 65 53 74 6f 72 65 28 70 50 61 72 73 65 2c 20  heStore(pParse, 
2ebf7 69 43 75 72 2c 20 2d 31 2c 20 69 52 6f 77 69 64  iCur, -1, iRowid
2ebf8 52 65 67 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  Reg);.      sqli
2ebf9 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2ebfa 20 4f 50 5f 53 65 65 6b 2c 20 69 43 75 72 2c 20   OP_Seek, iCur, 
2ebfb 69 52 6f 77 69 64 52 65 67 29 3b 20 20 2f 2a 20  iRowidReg);  /* 
2ebfc 44 65 66 65 72 72 65 64 20 73 65 65 6b 20 2a 2f  Deferred seek */
2ebfd 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52  .    }..    /* R
2ebfe 65 63 6f 72 64 20 74 68 65 20 69 6e 73 74 72 75  ecord the instru
2ebff 63 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 74 65  ction used to te
2ec00 72 6d 69 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70  rminate the loop
2ec01 2e 20 44 69 73 61 62 6c 65 20 0a 20 20 20 20 2a  . Disable .    *
2ec02 2a 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74  * WHERE clause t
2ec03 65 72 6d 73 20 6d 61 64 65 20 72 65 64 75 6e 64  erms made redund
2ec04 61 6e 74 20 62 79 20 74 68 65 20 69 6e 64 65 78  ant by the index
2ec05 20 72 61 6e 67 65 20 73 63 61 6e 2e 0a 20 20 20   range scan..   
2ec06 20 2a 2f 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e   */.    pLevel->
2ec07 6f 70 20 3d 20 62 52 65 76 20 3f 20 4f 50 5f 50  op = bRev ? OP_P
2ec08 72 65 76 20 3a 20 4f 50 5f 4e 65 78 74 3b 0a 20  rev : OP_Next;. 
2ec09 20 20 20 70 4c 65 76 65 6c 2d 3e 70 31 20 3d 20     pLevel->p1 = 
2ec0a 69 49 64 78 43 75 72 3b 0a 20 20 7d 65 6c 73 65  iIdxCur;.  }else
2ec0b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2ec0c 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a  _OMIT_OR_OPTIMIZ
2ec0d 41 54 49 4f 4e 0a 20 20 69 66 28 20 70 4c 65 76  ATION.  if( pLev
2ec0e 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
2ec0f 20 26 20 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f   & WHERE_MULTI_O
2ec10 52 20 29 7b 0a 20 20 20 20 2f 2a 20 43 61 73 65  R ){.    /* Case
2ec11 20 34 3a 20 20 54 77 6f 20 6f 72 20 6d 6f 72 65   4:  Two or more
2ec12 20 73 65 70 61 72 61 74 65 6c 79 20 69 6e 64 65   separately inde
2ec13 78 65 64 20 74 65 72 6d 73 20 63 6f 6e 6e 65 63  xed terms connec
2ec14 74 65 64 20 62 79 20 4f 52 0a 20 20 20 20 2a 2a  ted by OR.    **
2ec15 0a 20 20 20 20 2a 2a 20 45 78 61 6d 70 6c 65 3a  .    ** Example:
2ec16 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
2ec17 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31   CREATE TABLE t1
2ec18 28 61 2c 62 2c 63 2c 64 29 3b 0a 20 20 20 20 2a  (a,b,c,d);.    *
2ec19 2a 20 20 20 43 52 45 41 54 45 20 49 4e 44 45 58  *   CREATE INDEX
2ec1a 20 69 31 20 4f 4e 20 74 31 28 61 29 3b 0a 20 20   i1 ON t1(a);.  
2ec1b 20 20 2a 2a 20 20 20 43 52 45 41 54 45 20 49 4e    **   CREATE IN
2ec1c 44 45 58 20 69 32 20 4f 4e 20 74 31 28 62 29 3b  DEX i2 ON t1(b);
2ec1d 0a 20 20 20 20 2a 2a 20 20 20 43 52 45 41 54 45  .    **   CREATE
2ec1e 20 49 4e 44 45 58 20 69 33 20 4f 4e 20 74 31 28   INDEX i3 ON t1(
2ec1f 63 29 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  c);.    **.    *
2ec20 2a 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f  *   SELECT * FRO
2ec21 4d 20 74 31 20 57 48 45 52 45 20 61 3d 35 20 4f  M t1 WHERE a=5 O
2ec22 52 20 62 3d 37 20 4f 52 20 28 63 3d 31 31 20 41  R b=7 OR (c=11 A
2ec23 4e 44 20 64 3d 31 33 29 0a 20 20 20 20 2a 2a 0a  ND d=13).    **.
2ec24 20 20 20 20 2a 2a 20 49 6e 20 74 68 65 20 65 78      ** In the ex
2ec25 61 6d 70 6c 65 2c 20 74 68 65 72 65 20 61 72 65  ample, there are
2ec26 20 74 68 72 65 65 20 69 6e 64 65 78 65 64 20 74   three indexed t
2ec27 65 72 6d 73 20 63 6f 6e 6e 65 63 74 65 64 20 62  erms connected b
2ec28 79 20 4f 52 2e 0a 20 20 20 20 2a 2a 20 54 68 65  y OR..    ** The
2ec29 20 74 6f 70 20 6f 66 20 74 68 65 20 6c 6f 6f 70   top of the loop
2ec2a 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73   looks like this
2ec2b 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  :.    **.    ** 
2ec2c 20 20 20 20 20 20 20 20 20 4e 75 6c 6c 20 20 20           Null   
2ec2d 20 20 20 20 31 20 20 20 20 20 20 20 20 20 20 20      1           
2ec2e 20 20 20 20 20 23 20 5a 65 72 6f 20 74 68 65 20       # Zero the 
2ec2f 72 6f 77 73 65 74 20 69 6e 20 72 65 67 20 31 0a  rowset in reg 1.
2ec30 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68      **.    ** Th
2ec31 65 6e 2c 20 66 6f 72 20 65 61 63 68 20 69 6e 64  en, for each ind
2ec32 65 78 65 64 20 74 65 72 6d 2c 20 74 68 65 20 66  exed term, the f
2ec33 6f 6c 6c 6f 77 69 6e 67 2e 20 54 68 65 20 61 72  ollowing. The ar
2ec34 67 75 6d 65 6e 74 73 20 74 6f 0a 20 20 20 20 2a  guments to.    *
2ec35 2a 20 52 6f 77 53 65 74 54 65 73 74 20 61 72 65  * RowSetTest are
2ec36 20 73 75 63 68 20 74 68 61 74 20 74 68 65 20 72   such that the r
2ec37 6f 77 69 64 20 6f 66 20 74 68 65 20 63 75 72 72  owid of the curr
2ec38 65 6e 74 20 72 6f 77 20 69 73 20 69 6e 73 65 72  ent row is inser
2ec39 74 65 64 0a 20 20 20 20 2a 2a 20 69 6e 74 6f 20  ted.    ** into 
2ec3a 74 68 65 20 52 6f 77 53 65 74 2e 20 49 66 20 69  the RowSet. If i
2ec3b 74 20 69 73 20 61 6c 72 65 61 64 79 20 70 72 65  t is already pre
2ec3c 73 65 6e 74 2c 20 63 6f 6e 74 72 6f 6c 20 73 6b  sent, control sk
2ec3d 69 70 73 20 74 68 65 0a 20 20 20 20 2a 2a 20 47  ips the.    ** G
2ec3e 6f 73 75 62 20 6f 70 63 6f 64 65 20 61 6e 64 20  osub opcode and 
2ec3f 6a 75 6d 70 73 20 73 74 72 61 69 67 68 74 20 74  jumps straight t
2ec40 6f 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72  o the code gener
2ec41 61 74 65 64 20 62 79 20 57 68 65 72 65 45 6e 64  ated by WhereEnd
2ec42 28 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ()..    **.    *
2ec43 2a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  *        sqlite3
2ec44 57 68 65 72 65 42 65 67 69 6e 28 3c 74 65 72 6d  WhereBegin(<term
2ec45 3e 29 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  >).    **       
2ec46 20 20 20 52 6f 77 53 65 74 54 65 73 74 20 20 20     RowSetTest   
2ec47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 23                 #
2ec48 20 49 6e 73 65 72 74 20 72 6f 77 69 64 20 69 6e   Insert rowid in
2ec49 74 6f 20 72 6f 77 73 65 74 0a 20 20 20 20 2a 2a  to rowset.    **
2ec4a 20 20 20 20 20 20 20 20 20 20 47 6f 73 75 62 20            Gosub 
2ec4b 20 20 20 20 20 32 20 41 0a 20 20 20 20 2a 2a 20       2 A.    ** 
2ec4c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 68         sqlite3Wh
2ec4d 65 72 65 45 6e 64 28 29 0a 20 20 20 20 2a 2a 0a  ereEnd().    **.
2ec4e 20 20 20 20 2a 2a 20 46 6f 6c 6c 6f 77 69 6e 67      ** Following
2ec4f 20 74 68 65 20 61 62 6f 76 65 2c 20 63 6f 64 65   the above, code
2ec50 20 74 6f 20 74 65 72 6d 69 6e 61 74 65 20 74 68   to terminate th
2ec51 65 20 6c 6f 6f 70 2e 20 4c 61 62 65 6c 20 41 2c  e loop. Label A,
2ec52 20 74 68 65 20 74 61 72 67 65 74 0a 20 20 20 20   the target.    
2ec53 2a 2a 20 6f 66 20 74 68 65 20 47 6f 73 75 62 20  ** of the Gosub 
2ec54 61 62 6f 76 65 2c 20 6a 75 6d 70 73 20 74 6f 20  above, jumps to 
2ec55 74 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  the instruction 
2ec56 72 69 67 68 74 20 61 66 74 65 72 20 74 68 65 20  right after the 
2ec57 47 6f 74 6f 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  Goto..    **.   
2ec58 20 2a 2a 20 20 20 20 20 20 20 20 20 20 4e 75 6c   **          Nul
2ec59 6c 20 20 20 20 20 20 20 31 20 20 20 20 20 20 20  l       1       
2ec5a 20 20 20 20 20 20 20 20 20 23 20 5a 65 72 6f 20           # Zero 
2ec5b 74 68 65 20 72 6f 77 73 65 74 20 69 6e 20 72 65  the rowset in re
2ec5c 67 20 31 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  g 1.    **      
2ec5d 20 20 20 20 47 6f 74 6f 20 20 20 20 20 20 20 42      Goto       B
2ec5e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ec5f 23 20 54 68 65 20 6c 6f 6f 70 20 69 73 20 66 69  # The loop is fi
2ec60 6e 69 73 68 65 64 2e 0a 20 20 20 20 2a 2a 0a 20  nished..    **. 
2ec61 20 20 20 2a 2a 20 20 20 20 20 20 20 41 3a 20 3c     **       A: <
2ec62 6c 6f 6f 70 20 62 6f 64 79 3e 20 20 20 20 20 20  loop body>      
2ec63 20 20 20 20 20 20 20 20 20 20 20 23 20 52 65 74             # Ret
2ec64 75 72 6e 20 64 61 74 61 2c 20 77 68 61 74 65 76  urn data, whatev
2ec65 65 72 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  er..    **.    *
2ec66 2a 20 20 20 20 20 20 20 20 20 20 52 65 74 75 72  *          Retur
2ec67 6e 20 20 20 20 20 32 20 20 20 20 20 20 20 20 20  n     2         
2ec68 20 20 20 20 20 20 20 23 20 4a 75 6d 70 20 62 61         # Jump ba
2ec69 63 6b 20 74 6f 20 74 68 65 20 47 6f 73 75 62 0a  ck to the Gosub.
2ec6a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
2ec6b 20 20 20 20 42 3a 20 3c 61 66 74 65 72 20 74 68      B: <after th
2ec6c 65 20 6c 6f 6f 70 3e 0a 20 20 20 20 2a 2a 0a 20  e loop>.    **. 
2ec6d 20 20 20 2a 2f 0a 20 20 20 20 57 68 65 72 65 43     */.    WhereC
2ec6e 6c 61 75 73 65 20 2a 70 4f 72 57 63 3b 20 20 20  lause *pOrWc;   
2ec6f 20 2f 2a 20 54 68 65 20 4f 52 2d 63 6c 61 75 73   /* The OR-claus
2ec70 65 20 62 72 6f 6b 65 6e 20 6f 75 74 20 69 6e 74  e broken out int
2ec71 6f 20 73 75 62 74 65 72 6d 73 20 2a 2f 0a 20 20  o subterms */.  
2ec72 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 46 69    WhereTerm *pFi
2ec73 6e 61 6c 3b 20 20 20 20 20 2f 2a 20 46 69 6e 61  nal;     /* Fina
2ec74 6c 20 73 75 62 74 65 72 6d 20 77 69 74 68 69 6e  l subterm within
2ec75 20 74 68 65 20 4f 52 2d 63 6c 61 75 73 65 2e 20   the OR-clause. 
2ec76 2a 2f 0a 20 20 20 20 53 72 63 4c 69 73 74 20 6f  */.    SrcList o
2ec77 6e 65 54 61 62 3b 20 20 20 20 20 20 20 20 2f 2a  neTab;        /*
2ec78 20 53 68 6f 72 74 65 6e 65 64 20 74 61 62 6c 65   Shortened table
2ec79 20 6c 69 73 74 20 2a 2f 0a 0a 20 20 20 20 69 6e   list */..    in
2ec7a 74 20 72 65 67 52 65 74 75 72 6e 20 3d 20 2b 2b  t regReturn = ++
2ec7b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 20 20 20  pParse->nMem;   
2ec7c 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73          /* Regis
2ec7d 74 65 72 20 75 73 65 64 20 77 69 74 68 20 4f 50  ter used with OP
2ec7e 5f 47 6f 73 75 62 20 2a 2f 0a 20 20 20 20 69 6e  _Gosub */.    in
2ec7f 74 20 72 65 67 52 6f 77 73 65 74 20 3d 20 30 3b  t regRowset = 0;
2ec80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ec81 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73          /* Regis
2ec82 74 65 72 20 66 6f 72 20 52 6f 77 53 65 74 20 6f  ter for RowSet o
2ec83 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 69 6e 74  bject */.    int
2ec84 20 72 65 67 52 6f 77 69 64 20 3d 20 30 3b 20 20   regRowid = 0;  
2ec85 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ec86 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
2ec87 65 72 20 68 6f 6c 64 69 6e 67 20 72 6f 77 69 64  er holding rowid
2ec88 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 4c 6f 6f   */.    int iLoo
2ec89 70 42 6f 64 79 20 3d 20 73 71 6c 69 74 65 33 56  pBody = sqlite3V
2ec8a 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
2ec8b 20 20 2f 2a 20 53 74 61 72 74 20 6f 66 20 6c 6f    /* Start of lo
2ec8c 6f 70 20 62 6f 64 79 20 2a 2f 0a 20 20 20 20 69  op body */.    i
2ec8d 6e 74 20 69 52 65 74 49 6e 69 74 3b 20 20 20 20  nt iRetInit;    
2ec8e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ec8f 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72           /* Addr
2ec90 65 73 73 20 6f 66 20 72 65 67 52 65 74 75 72 6e  ess of regReturn
2ec91 20 69 6e 69 74 20 2a 2f 0a 20 20 20 20 69 6e 74   init */.    int
2ec92 20 69 69 3b 0a 20 20 20 0a 20 20 20 20 70 54 65   ii;.   .    pTe
2ec93 72 6d 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61  rm = pLevel->pla
2ec94 6e 2e 75 2e 70 54 65 72 6d 3b 0a 20 20 20 20 61  n.u.pTerm;.    a
2ec95 73 73 65 72 74 28 20 70 54 65 72 6d 21 3d 30 20  ssert( pTerm!=0 
2ec96 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
2ec97 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d  Term->eOperator=
2ec98 3d 57 4f 5f 4f 52 20 29 3b 0a 20 20 20 20 61 73  =WO_OR );.    as
2ec99 73 65 72 74 28 20 28 70 54 65 72 6d 2d 3e 77 74  sert( (pTerm->wt
2ec9a 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 4f 52 49  Flags & TERM_ORI
2ec9b 4e 46 4f 29 21 3d 30 20 29 3b 0a 20 20 20 20 70  NFO)!=0 );.    p
2ec9c 4f 72 57 63 20 3d 20 26 70 54 65 72 6d 2d 3e 75  OrWc = &pTerm->u
2ec9d 2e 70 4f 72 49 6e 66 6f 2d 3e 77 63 3b 0a 20 20  .pOrInfo->wc;.  
2ec9e 20 20 70 46 69 6e 61 6c 20 3d 20 26 70 4f 72 57    pFinal = &pOrW
2ec9f 63 2d 3e 61 5b 70 4f 72 57 63 2d 3e 6e 54 65 72  c->a[pOrWc->nTer
2eca0 6d 2d 31 5d 3b 0a 0a 20 20 20 20 2f 2a 20 53 65  m-1];..    /* Se
2eca1 74 20 75 70 20 61 20 53 72 63 4c 69 73 74 20 63  t up a SrcList c
2eca2 6f 6e 74 61 69 6e 69 6e 67 20 6a 75 73 74 20 74  ontaining just t
2eca3 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 73  he table being s
2eca4 63 61 6e 6e 65 64 20 62 79 20 74 68 69 73 20 6c  canned by this l
2eca5 6f 6f 70 2e 20 2a 2f 0a 20 20 20 20 6f 6e 65 54  oop. */.    oneT
2eca6 61 62 2e 6e 53 72 63 20 3d 20 31 3b 0a 20 20 20  ab.nSrc = 1;.   
2eca7 20 6f 6e 65 54 61 62 2e 6e 41 6c 6c 6f 63 20 3d   oneTab.nAlloc =
2eca8 20 31 3b 0a 20 20 20 20 6f 6e 65 54 61 62 2e 61   1;.    oneTab.a
2eca9 5b 30 5d 20 3d 20 2a 70 54 61 62 49 74 65 6d 3b  [0] = *pTabItem;
2ecaa 0a 0a 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c  ..    /* Initial
2ecab 69 7a 65 20 74 68 65 20 72 6f 77 73 65 74 20 72  ize the rowset r
2ecac 65 67 69 73 74 65 72 20 74 6f 20 63 6f 6e 74 61  egister to conta
2ecad 69 6e 20 4e 55 4c 4c 2e 20 41 6e 20 53 51 4c 20  in NULL. An SQL 
2ecae 4e 55 4c 4c 20 69 73 20 0a 20 20 20 20 2a 2a 20  NULL is .    ** 
2ecaf 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 61 6e  equivalent to an
2ecb0 20 65 6d 70 74 79 20 72 6f 77 73 65 74 2e 0a 20   empty rowset.. 
2ecb1 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 41 6c 73     **.    ** Als
2ecb2 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 72 65 67  o initialize reg
2ecb3 52 65 74 75 72 6e 20 74 6f 20 63 6f 6e 74 61 69  Return to contai
2ecb4 6e 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66  n the address of
2ecb5 20 74 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e   the instruction
2ecb6 20 0a 20 20 20 20 2a 2a 20 69 6d 6d 65 64 69 61   .    ** immedia
2ecb7 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 74  tely following t
2ecb8 68 65 20 4f 50 5f 52 65 74 75 72 6e 20 61 74 20  he OP_Return at 
2ecb9 74 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20 74 68  the bottom of th
2ecba 65 20 6c 6f 6f 70 2e 20 54 68 69 73 0a 20 20 20  e loop. This.   
2ecbb 20 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64 20   ** is required 
2ecbc 69 6e 20 61 20 66 65 77 20 6f 62 73 63 75 72 65  in a few obscure
2ecbd 20 4c 45 46 54 20 4a 4f 49 4e 20 63 61 73 65 73   LEFT JOIN cases
2ecbe 20 77 68 65 72 65 20 63 6f 6e 74 72 6f 6c 20 6a   where control j
2ecbf 75 6d 70 73 0a 20 20 20 20 2a 2a 20 6f 76 65 72  umps.    ** over
2ecc0 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20   the top of the 
2ecc1 6c 6f 6f 70 20 69 6e 74 6f 20 74 68 65 20 62 6f  loop into the bo
2ecc2 64 79 20 6f 66 20 69 74 2e 20 49 6e 20 74 68 69  dy of it. In thi
2ecc3 73 20 63 61 73 65 20 74 68 65 20 0a 20 20 20 20  s case the .    
2ecc4 2a 2a 20 63 6f 72 72 65 63 74 20 72 65 73 70 6f  ** correct respo
2ecc5 6e 73 65 20 66 6f 72 20 74 68 65 20 65 6e 64 2d  nse for the end-
2ecc6 6f 66 2d 6c 6f 6f 70 20 63 6f 64 65 20 28 74 68  of-loop code (th
2ecc7 65 20 4f 50 5f 52 65 74 75 72 6e 29 20 69 73 20  e OP_Return) is 
2ecc8 74 6f 20 0a 20 20 20 20 2a 2a 20 66 61 6c 6c 20  to .    ** fall 
2ecc9 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e  through to the n
2ecca 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2c  ext instruction,
2eccb 20 6a 75 73 74 20 61 73 20 61 6e 20 4f 50 5f 4e   just as an OP_N
2eccc 65 78 74 20 64 6f 65 73 20 69 66 0a 20 20 20 20  ext does if.    
2eccd 2a 2a 20 63 61 6c 6c 65 64 20 6f 6e 20 61 6e 20  ** called on an 
2ecce 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 63 75  uninitialized cu
2eccf 72 73 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  rsor..    */.   
2ecd0 20 69 66 28 20 28 77 63 74 72 6c 46 6c 61 67 73   if( (wctrlFlags
2ecd1 20 26 20 57 48 45 52 45 5f 44 55 50 4c 49 43 41   & WHERE_DUPLICA
2ecd2 54 45 53 5f 4f 4b 29 3d 3d 30 20 29 7b 0a 20 20  TES_OK)==0 ){.  
2ecd3 20 20 20 20 72 65 67 52 6f 77 73 65 74 20 3d 20      regRowset = 
2ecd4 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
2ecd5 20 20 20 20 20 20 72 65 67 52 6f 77 69 64 20 3d        regRowid =
2ecd6 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
2ecd7 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2ecd8 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
2ecd9 75 6c 6c 2c 20 30 2c 20 72 65 67 52 6f 77 73 65  ull, 0, regRowse
2ecda 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 52  t);.    }.    iR
2ecdb 65 74 49 6e 69 74 20 3d 20 73 71 6c 69 74 65 33  etInit = sqlite3
2ecdc 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2ecdd 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67  _Integer, 0, reg
2ecde 52 65 74 75 72 6e 29 3b 0a 0a 20 20 20 20 66 6f  Return);..    fo
2ecdf 72 28 69 69 3d 30 3b 20 69 69 3c 70 4f 72 57 63  r(ii=0; ii<pOrWc
2ece0 2d 3e 6e 54 65 72 6d 3b 20 69 69 2b 2b 29 7b 0a  ->nTerm; ii++){.
2ece1 20 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 20        WhereTerm 
2ece2 2a 70 4f 72 54 65 72 6d 20 3d 20 26 70 4f 72 57  *pOrTerm = &pOrW
2ece3 63 2d 3e 61 5b 69 69 5d 3b 0a 20 20 20 20 20 20  c->a[ii];.      
2ece4 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66  if( pOrTerm->lef
2ece5 74 43 75 72 73 6f 72 3d 3d 69 43 75 72 20 7c 7c  tCursor==iCur ||
2ece6 20 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61   pOrTerm->eOpera
2ece7 74 6f 72 3d 3d 57 4f 5f 41 4e 44 20 29 7b 0a 20  tor==WO_AND ){. 
2ece8 20 20 20 20 20 20 20 57 68 65 72 65 49 6e 66 6f         WhereInfo
2ece9 20 2a 70 53 75 62 57 49 6e 66 6f 3b 20 20 20 20   *pSubWInfo;    
2ecea 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 20 66 6f        /* Info fo
2eceb 72 20 73 69 6e 67 6c 65 20 4f 52 2d 74 65 72 6d  r single OR-term
2ecec 20 73 63 61 6e 20 2a 2f 0a 20 20 20 20 20 20 20   scan */.       
2eced 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68   /* Loop through
2ecee 20 74 61 62 6c 65 20 65 6e 74 72 69 65 73 20 74   table entries t
2ecef 68 61 74 20 6d 61 74 63 68 20 74 65 72 6d 20 70  hat match term p
2ecf0 4f 72 54 65 72 6d 2e 20 2a 2f 0a 20 20 20 20 20  OrTerm. */.     
2ecf1 20 20 20 70 53 75 62 57 49 6e 66 6f 20 3d 20 73     pSubWInfo = s
2ecf2 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e  qlite3WhereBegin
2ecf3 28 70 50 61 72 73 65 2c 20 26 6f 6e 65 54 61 62  (pParse, &oneTab
2ecf4 2c 20 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72  , pOrTerm->pExpr
2ecf5 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20  , 0,.           
2ecf6 20 20 20 20 20 20 20 20 20 20 20 20 20 57 48 45               WHE
2ecf7 52 45 5f 4f 4d 49 54 5f 4f 50 45 4e 20 7c 20 57  RE_OMIT_OPEN | W
2ecf8 48 45 52 45 5f 4f 4d 49 54 5f 43 4c 4f 53 45 20  HERE_OMIT_CLOSE 
2ecf9 7c 20 57 48 45 52 45 5f 46 4f 52 43 45 5f 54 41  | WHERE_FORCE_TA
2ecfa 42 4c 45 29 3b 0a 20 20 20 20 20 20 20 20 69 66  BLE);.        if
2ecfb 28 20 70 53 75 62 57 49 6e 66 6f 20 29 7b 0a 20  ( pSubWInfo ){. 
2ecfc 20 20 20 20 20 20 20 20 20 69 66 28 20 28 77 63           if( (wc
2ecfd 74 72 6c 46 6c 61 67 73 20 26 20 57 48 45 52 45  trlFlags & WHERE
2ecfe 5f 44 55 50 4c 49 43 41 54 45 53 5f 4f 4b 29 3d  _DUPLICATES_OK)=
2ecff 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
2ed00 20 20 69 6e 74 20 69 53 65 74 20 3d 20 28 28 69    int iSet = ((i
2ed01 69 3d 3d 70 4f 72 57 63 2d 3e 6e 54 65 72 6d 2d  i==pOrWc->nTerm-
2ed02 31 29 3f 2d 31 3a 69 69 29 3b 0a 20 20 20 20 20  1)?-1:ii);.     
2ed03 20 20 20 20 20 20 20 69 6e 74 20 72 3b 0a 20 20         int r;.  
2ed04 20 20 20 20 20 20 20 20 20 20 72 20 3d 20 73 71            r = sq
2ed05 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47 65 74  lite3ExprCodeGet
2ed06 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20 70  Column(pParse, p
2ed07 54 61 62 49 74 65 6d 2d 3e 70 54 61 62 2c 20 2d  TabItem->pTab, -
2ed08 31 2c 20 69 43 75 72 2c 20 0a 20 20 20 20 20 20  1, iCur, .      
2ed09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ed0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ed0b 20 20 20 72 65 67 52 6f 77 69 64 2c 20 30 29 3b     regRowid, 0);
2ed0c 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
2ed0d 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
2ed0e 2c 20 4f 50 5f 52 6f 77 53 65 74 54 65 73 74 2c  , OP_RowSetTest,
2ed0f 20 72 65 67 52 6f 77 73 65 74 2c 0a 20 20 20 20   regRowset,.    
2ed10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ed11 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2ed12 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
2ed13 28 76 29 2b 32 2c 0a 20 20 20 20 20 20 20 20 20  (v)+2,.         
2ed14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ed15 20 20 20 20 20 72 2c 20 53 51 4c 49 54 45 5f 49       r, SQLITE_I
2ed16 4e 54 5f 54 4f 5f 50 54 52 28 69 53 65 74 29 2c  NT_TO_PTR(iSet),
2ed17 20 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20 20 20   P4_INT32);.    
2ed18 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2ed19 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2ed1a 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c  Op2(v, OP_Gosub,
2ed1b 20 72 65 67 52 65 74 75 72 6e 2c 20 69 4c 6f 6f   regReturn, iLoo
2ed1c 70 42 6f 64 79 29 3b 0a 0a 20 20 20 20 20 20 20  pBody);..       
2ed1d 20 20 20 2f 2a 20 46 69 6e 69 73 68 20 74 68 65     /* Finish the
2ed1e 20 6c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 61   loop through ta
2ed1f 62 6c 65 20 65 6e 74 72 69 65 73 20 74 68 61 74  ble entries that
2ed20 20 6d 61 74 63 68 20 74 65 72 6d 20 70 4f 72 54   match term pOrT
2ed21 65 72 6d 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  erm. */.        
2ed22 20 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e    sqlite3WhereEn
2ed23 64 28 70 53 75 62 57 49 6e 66 6f 29 3b 0a 20 20  d(pSubWInfo);.  
2ed24 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
2ed25 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
2ed26 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c  3VdbeChangeP1(v,
2ed27 20 69 52 65 74 49 6e 69 74 2c 20 73 71 6c 69 74   iRetInit, sqlit
2ed28 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
2ed29 72 28 76 29 29 3b 0a 20 20 20 20 2f 2a 20 73 71  r(v));.    /* sq
2ed2a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2ed2b 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72  v, OP_Null, 0, r
2ed2c 65 67 52 6f 77 73 65 74 29 3b 20 2a 2f 0a 20 20  egRowset); */.  
2ed2d 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2ed2e 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
2ed2f 30 2c 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 42  0, pLevel->addrB
2ed30 72 6b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  rk);.    sqlite3
2ed31 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
2ed32 28 76 2c 20 69 4c 6f 6f 70 42 6f 64 79 29 3b 0a  (v, iLoopBody);.
2ed33 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20  .    pLevel->op 
2ed34 3d 20 4f 50 5f 52 65 74 75 72 6e 3b 0a 20 20 20  = OP_Return;.   
2ed35 20 70 4c 65 76 65 6c 2d 3e 70 31 20 3d 20 72 65   pLevel->p1 = re
2ed36 67 52 65 74 75 72 6e 3b 0a 20 20 20 20 64 69 73  gReturn;.    dis
2ed37 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c  ableTerm(pLevel,
2ed38 20 70 54 65 72 6d 29 3b 0a 20 20 7d 65 6c 73 65   pTerm);.  }else
2ed39 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
2ed3a 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49  E_OMIT_OR_OPTIMI
2ed3b 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 20 20 7b 0a 20  ZATION */..  {. 
2ed3c 20 20 20 2f 2a 20 43 61 73 65 20 35 3a 20 20 54     /* Case 5:  T
2ed3d 68 65 72 65 20 69 73 20 6e 6f 20 75 73 61 62 6c  here is no usabl
2ed3e 65 20 69 6e 64 65 78 2e 20 20 57 65 20 6d 75 73  e index.  We mus
2ed3f 74 20 64 6f 20 61 20 63 6f 6d 70 6c 65 74 65 0a  t do a complete.
2ed40 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
2ed41 73 63 61 6e 20 6f 66 20 74 68 65 20 65 6e 74 69  scan of the enti
2ed42 72 65 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2f  re table..    */
2ed43 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73  .    static cons
2ed44 74 20 75 38 20 61 53 74 65 70 5b 5d 20 3d 20 7b  t u8 aStep[] = {
2ed45 20 4f 50 5f 4e 65 78 74 2c 20 4f 50 5f 50 72 65   OP_Next, OP_Pre
2ed46 76 20 7d 3b 0a 20 20 20 20 73 74 61 74 69 63 20  v };.    static 
2ed47 63 6f 6e 73 74 20 75 38 20 61 53 74 61 72 74 5b  const u8 aStart[
2ed48 5d 20 3d 20 7b 20 4f 50 5f 52 65 77 69 6e 64 2c  ] = { OP_Rewind,
2ed49 20 4f 50 5f 4c 61 73 74 20 7d 3b 0a 20 20 20 20   OP_Last };.    
2ed4a 61 73 73 65 72 74 28 20 62 52 65 76 3d 3d 30 20  assert( bRev==0 
2ed4b 7c 7c 20 62 52 65 76 3d 3d 31 20 29 3b 0a 20 20  || bRev==1 );.  
2ed4c 20 20 61 73 73 65 72 74 28 20 6f 6d 69 74 54 61    assert( omitTa
2ed4d 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 70 4c  ble==0 );.    pL
2ed4e 65 76 65 6c 2d 3e 6f 70 20 3d 20 61 53 74 65 70  evel->op = aStep
2ed4f 5b 62 52 65 76 5d 3b 0a 20 20 20 20 70 4c 65 76  [bRev];.    pLev
2ed50 65 6c 2d 3e 70 31 20 3d 20 69 43 75 72 3b 0a 20  el->p1 = iCur;. 
2ed51 20 20 20 70 4c 65 76 65 6c 2d 3e 70 32 20 3d 20     pLevel->p2 = 
2ed52 31 20 2b 20 73 71 6c 69 74 65 33 56 64 62 65 41  1 + sqlite3VdbeA
2ed53 64 64 4f 70 32 28 76 2c 20 61 53 74 61 72 74 5b  ddOp2(v, aStart[
2ed54 62 52 65 76 5d 2c 20 69 43 75 72 2c 20 61 64 64  bRev], iCur, add
2ed55 72 42 72 6b 29 3b 0a 20 20 20 20 70 4c 65 76 65  rBrk);.    pLeve
2ed56 6c 2d 3e 70 35 20 3d 20 53 51 4c 49 54 45 5f 53  l->p5 = SQLITE_S
2ed57 54 4d 54 53 54 41 54 55 53 5f 46 55 4c 4c 53 43  TMTSTATUS_FULLSC
2ed58 41 4e 5f 53 54 45 50 3b 0a 20 20 7d 0a 20 20 6e  AN_STEP;.  }.  n
2ed59 6f 74 52 65 61 64 79 20 26 3d 20 7e 67 65 74 4d  otReady &= ~getM
2ed5a 61 73 6b 28 70 57 43 2d 3e 70 4d 61 73 6b 53 65  ask(pWC->pMaskSe
2ed5b 74 2c 20 69 43 75 72 29 3b 0a 0a 20 20 2f 2a 20  t, iCur);..  /* 
2ed5c 49 6e 73 65 72 74 20 63 6f 64 65 20 74 6f 20 74  Insert code to t
2ed5d 65 73 74 20 65 76 65 72 79 20 73 75 62 65 78 70  est every subexp
2ed5e 72 65 73 73 69 6f 6e 20 74 68 61 74 20 63 61 6e  ression that can
2ed5f 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 20   be completely. 
2ed60 20 2a 2a 20 63 6f 6d 70 75 74 65 64 20 75 73 69   ** computed usi
2ed61 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 73  ng the current s
2ed62 65 74 20 6f 66 20 74 61 62 6c 65 73 2e 0a 20 20  et of tables..  
2ed63 2a 2f 0a 20 20 6b 20 3d 20 30 3b 0a 20 20 66 6f  */.  k = 0;.  fo
2ed64 72 28 70 54 65 72 6d 3d 70 57 43 2d 3e 61 2c 20  r(pTerm=pWC->a, 
2ed65 6a 3d 70 57 43 2d 3e 6e 54 65 72 6d 3b 20 6a 3e  j=pWC->nTerm; j>
2ed66 30 3b 20 6a 2d 2d 2c 20 70 54 65 72 6d 2b 2b 29  0; j--, pTerm++)
2ed67 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45 3b 0a  {.    Expr *pE;.
2ed68 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 54      testcase( pT
2ed69 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54  erm->wtFlags & T
2ed6a 45 52 4d 5f 56 49 52 54 55 41 4c 20 29 3b 0a 20  ERM_VIRTUAL );. 
2ed6b 20 20 20 74 65 73 74 63 61 73 65 28 20 70 54 65     testcase( pTe
2ed6c 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45  rm->wtFlags & TE
2ed6d 52 4d 5f 43 4f 44 45 44 20 29 3b 0a 20 20 20 20  RM_CODED );.    
2ed6e 69 66 28 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61  if( pTerm->wtFla
2ed6f 67 73 20 26 20 28 54 45 52 4d 5f 56 49 52 54 55  gs & (TERM_VIRTU
2ed70 41 4c 7c 54 45 52 4d 5f 43 4f 44 45 44 29 20 29  AL|TERM_CODED) )
2ed71 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69   continue;.    i
2ed72 66 28 20 28 70 54 65 72 6d 2d 3e 70 72 65 72 65  f( (pTerm->prere
2ed73 71 41 6c 6c 20 26 20 6e 6f 74 52 65 61 64 79 29  qAll & notReady)
2ed74 21 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  !=0 ) continue;.
2ed75 20 20 20 20 70 45 20 3d 20 70 54 65 72 6d 2d 3e      pE = pTerm->
2ed76 70 45 78 70 72 3b 0a 20 20 20 20 61 73 73 65 72  pExpr;.    asser
2ed77 74 28 20 70 45 21 3d 30 20 29 3b 0a 20 20 20 20  t( pE!=0 );.    
2ed78 69 66 28 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66  if( pLevel->iLef
2ed79 74 4a 6f 69 6e 20 26 26 20 21 45 78 70 72 48 61  tJoin && !ExprHa
2ed7a 73 50 72 6f 70 65 72 74 79 28 70 45 2c 20 45 50  sProperty(pE, EP
2ed7b 5f 46 72 6f 6d 4a 6f 69 6e 29 20 29 7b 0a 20 20  _FromJoin) ){.  
2ed7c 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20      continue;.  
2ed7d 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 45    }.    sqlite3E
2ed7e 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73  xprIfFalse(pPars
2ed7f 65 2c 20 70 45 2c 20 61 64 64 72 43 6f 6e 74 2c  e, pE, addrCont,
2ed80 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55   SQLITE_JUMPIFNU
2ed81 4c 4c 29 3b 0a 20 20 20 20 6b 20 3d 20 31 3b 0a  LL);.    k = 1;.
2ed82 20 20 20 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61      pTerm->wtFla
2ed83 67 73 20 7c 3d 20 54 45 52 4d 5f 43 4f 44 45 44  gs |= TERM_CODED
2ed84 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 6f 72 20  ;.  }..  /* For 
2ed85 61 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49  a LEFT OUTER JOI
2ed86 4e 2c 20 67 65 6e 65 72 61 74 65 20 63 6f 64 65  N, generate code
2ed87 20 74 68 61 74 20 77 69 6c 6c 20 72 65 63 6f 72   that will recor
2ed88 64 20 74 68 65 20 66 61 63 74 20 74 68 61 74 0a  d the fact that.
2ed89 20 20 2a 2a 20 61 74 20 6c 65 61 73 74 20 6f 6e    ** at least on
2ed8a 65 20 72 6f 77 20 6f 66 20 74 68 65 20 72 69 67  e row of the rig
2ed8b 68 74 20 74 61 62 6c 65 20 68 61 73 20 6d 61 74  ht table has mat
2ed8c 63 68 65 64 20 74 68 65 20 6c 65 66 74 20 74 61  ched the left ta
2ed8d 62 6c 65 2e 20 20 0a 20 20 2a 2f 0a 20 20 69 66  ble.  .  */.  if
2ed8e 28 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a  ( pLevel->iLeftJ
2ed8f 6f 69 6e 20 29 7b 0a 20 20 20 20 70 4c 65 76 65  oin ){.    pLeve
2ed90 6c 2d 3e 61 64 64 72 46 69 72 73 74 20 3d 20 73  l->addrFirst = s
2ed91 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
2ed92 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 73 71  tAddr(v);.    sq
2ed93 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2ed94 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31  v, OP_Integer, 1
2ed95 2c 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a  , pLevel->iLeftJ
2ed96 6f 69 6e 29 3b 0a 20 20 20 20 56 64 62 65 43 6f  oin);.    VdbeCo
2ed97 6d 6d 65 6e 74 28 28 76 2c 20 22 72 65 63 6f 72  mment((v, "recor
2ed98 64 20 4c 45 46 54 20 4a 4f 49 4e 20 68 69 74 22  d LEFT JOIN hit"
2ed99 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  ));.    sqlite3E
2ed9a 78 70 72 43 61 63 68 65 43 6c 65 61 72 28 70 50  xprCacheClear(pP
2ed9b 61 72 73 65 29 3b 0a 20 20 20 20 66 6f 72 28 70  arse);.    for(p
2ed9c 54 65 72 6d 3d 70 57 43 2d 3e 61 2c 20 6a 3d 30  Term=pWC->a, j=0
2ed9d 3b 20 6a 3c 70 57 43 2d 3e 6e 54 65 72 6d 3b 20  ; j<pWC->nTerm; 
2ed9e 6a 2b 2b 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a 20  j++, pTerm++){. 
2ed9f 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
2eda0 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20  Term->wtFlags & 
2eda1 54 45 52 4d 5f 56 49 52 54 55 41 4c 20 29 3b 0a  TERM_VIRTUAL );.
2eda2 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2eda3 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26  pTerm->wtFlags &
2eda4 20 54 45 52 4d 5f 43 4f 44 45 44 20 29 3b 0a 20   TERM_CODED );. 
2eda5 20 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e       if( pTerm->
2eda6 77 74 46 6c 61 67 73 20 26 20 28 54 45 52 4d 5f  wtFlags & (TERM_
2eda7 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 43 4f 44  VIRTUAL|TERM_COD
2eda8 45 44 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ED) ) continue;.
2eda9 20 20 20 20 20 20 69 66 28 20 28 70 54 65 72 6d        if( (pTerm
2edaa 2d 3e 70 72 65 72 65 71 41 6c 6c 20 26 20 6e 6f  ->prereqAll & no
2edab 74 52 65 61 64 79 29 21 3d 30 20 29 20 63 6f 6e  tReady)!=0 ) con
2edac 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 61 73 73  tinue;.      ass
2edad 65 72 74 28 20 70 54 65 72 6d 2d 3e 70 45 78 70  ert( pTerm->pExp
2edae 72 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  r );.      sqlit
2edaf 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50  e3ExprIfFalse(pP
2edb0 61 72 73 65 2c 20 70 54 65 72 6d 2d 3e 70 45 78  arse, pTerm->pEx
2edb1 70 72 2c 20 61 64 64 72 43 6f 6e 74 2c 20 53 51  pr, addrCont, SQ
2edb2 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29  LITE_JUMPIFNULL)
2edb3 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 77  ;.      pTerm->w
2edb4 74 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f 43  tFlags |= TERM_C
2edb5 4f 44 45 44 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ODED;.    }.  }.
2edb6 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
2edb7 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
2edb8 69 52 65 6c 65 61 73 65 52 65 67 29 3b 0a 0a 20  iReleaseReg);.. 
2edb9 20 72 65 74 75 72 6e 20 6e 6f 74 52 65 61 64 79   return notReady
2edba 3b 0a 7d 0a 0a 23 69 66 20 64 65 66 69 6e 65 64  ;.}..#if defined
2edbb 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 2f 2a  (SQLITE_TEST)./*
2edbc 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
2edbd 67 20 76 61 72 69 61 62 6c 65 20 68 6f 6c 64 73  g variable holds
2edbe 20 61 20 74 65 78 74 20 64 65 73 63 72 69 70 74   a text descript
2edbf 69 6f 6e 20 6f 66 20 71 75 65 72 79 20 70 6c 61  ion of query pla
2edc0 6e 20 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 62  n generated.** b
2edc1 79 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  y the most recen
2edc2 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  t call to sqlite
2edc3 33 57 68 65 72 65 42 65 67 69 6e 28 29 2e 20 20  3WhereBegin().  
2edc4 45 61 63 68 20 63 61 6c 6c 20 74 6f 20 57 68 65  Each call to Whe
2edc5 72 65 42 65 67 69 6e 0a 2a 2a 20 6f 76 65 72 77  reBegin.** overw
2edc6 72 69 74 65 73 20 74 68 65 20 70 72 65 76 69 6f  rites the previo
2edc7 75 73 2e 20 20 54 68 69 73 20 69 6e 66 6f 72 6d  us.  This inform
2edc8 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 66 6f  ation is used fo
2edc9 72 20 74 65 73 74 69 6e 67 20 61 6e 64 0a 2a 2a  r testing and.**
2edca 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 2e 0a   analysis only..
2edcb 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68  */.SQLITE_API ch
2edcc 61 72 20 73 71 6c 69 74 65 33 5f 71 75 65 72 79  ar sqlite3_query
2edcd 5f 70 6c 61 6e 5b 42 4d 53 2a 32 2a 34 30 5d 3b  _plan[BMS*2*40];
2edce 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65    /* Text of the
2edcf 20 6a 6f 69 6e 20 2a 2f 0a 73 74 61 74 69 63 20   join */.static 
2edd0 69 6e 74 20 6e 51 50 6c 61 6e 20 3d 20 30 3b 20  int nQPlan = 0; 
2edd1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2edd2 4e 65 78 74 20 66 72 65 65 20 73 6c 6f 77 20 69  Next free slow i
2edd3 6e 20 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 5d 20  n _query_plan[] 
2edd4 2a 2f 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  */..#endif /* SQ
2edd5 4c 49 54 45 5f 54 45 53 54 20 2a 2f 0a 0a 0a 2f  LITE_TEST */.../
2edd6 2a 0a 2a 2a 20 46 72 65 65 20 61 20 57 68 65 72  *.** Free a Wher
2edd7 65 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 0a  eInfo structure.
2edd8 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77  */.static void w
2edd9 68 65 72 65 49 6e 66 6f 46 72 65 65 28 73 71 6c  hereInfoFree(sql
2edda 69 74 65 33 20 2a 64 62 2c 20 57 68 65 72 65 49  ite3 *db, WhereI
2eddb 6e 66 6f 20 2a 70 57 49 6e 66 6f 29 7b 0a 20 20  nfo *pWInfo){.  
2eddc 69 66 28 20 70 57 49 6e 66 6f 20 29 7b 0a 20 20  if( pWInfo ){.  
2eddd 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72    int i;.    for
2edde 28 69 3d 30 3b 20 69 3c 70 57 49 6e 66 6f 2d 3e  (i=0; i<pWInfo->
2eddf 6e 4c 65 76 65 6c 3b 20 69 2b 2b 29 7b 0a 20 20  nLevel; i++){.  
2ede0 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65      sqlite3_inde
2ede1 78 5f 69 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20  x_info *pInfo = 
2ede2 70 57 49 6e 66 6f 2d 3e 61 5b 69 5d 2e 70 49 64  pWInfo->a[i].pId
2ede3 78 49 6e 66 6f 3b 0a 20 20 20 20 20 20 69 66 28  xInfo;.      if(
2ede4 20 70 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20   pInfo ){.      
2ede5 20 20 2f 2a 20 61 73 73 65 72 74 28 20 70 49 6e    /* assert( pIn
2ede6 66 6f 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49 64  fo->needToFreeId
2ede7 78 53 74 72 3d 3d 30 20 7c 7c 20 64 62 2d 3e 6d  xStr==0 || db->m
2ede8 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 20 2a  allocFailed ); *
2ede9 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 49  /.        if( pI
2edea 6e 66 6f 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49  nfo->needToFreeI
2edeb 64 78 53 74 72 20 29 7b 0a 20 20 20 20 20 20 20  dxStr ){.       
2edec 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
2eded 70 49 6e 66 6f 2d 3e 69 64 78 53 74 72 29 3b 0a  pInfo->idxStr);.
2edee 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2edef 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2edf0 64 62 2c 20 70 49 6e 66 6f 29 3b 0a 20 20 20 20  db, pInfo);.    
2edf1 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 77 68    }.    }.    wh
2edf2 65 72 65 43 6c 61 75 73 65 43 6c 65 61 72 28 70  ereClauseClear(p
2edf3 57 49 6e 66 6f 2d 3e 70 57 43 29 3b 0a 20 20 20  WInfo->pWC);.   
2edf4 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2edf5 62 2c 20 70 57 49 6e 66 6f 29 3b 0a 20 20 7d 0a  b, pWInfo);.  }.
2edf6 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  }.../*.** Genera
2edf7 74 65 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  te the beginning
2edf8 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 75 73 65   of the loop use
2edf9 64 20 66 6f 72 20 57 48 45 52 45 20 63 6c 61 75  d for WHERE clau
2edfa 73 65 20 70 72 6f 63 65 73 73 69 6e 67 2e 0a 2a  se processing..*
2edfb 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c  * The return val
2edfc 75 65 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  ue is a pointer 
2edfd 74 6f 20 61 6e 20 6f 70 61 71 75 65 20 73 74 72  to an opaque str
2edfe 75 63 74 75 72 65 20 74 68 61 74 20 63 6f 6e 74  ucture that cont
2edff 61 69 6e 73 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74  ains.** informat
2ee00 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 74 65  ion needed to te
2ee01 72 6d 69 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70  rminate the loop
2ee02 2e 20 20 4c 61 74 65 72 2c 20 74 68 65 20 63 61  .  Later, the ca
2ee03 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 0a 2a 2a  lling routine.**
2ee04 20 73 68 6f 75 6c 64 20 69 6e 76 6f 6b 65 20 73   should invoke s
2ee05 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 29  qlite3WhereEnd()
2ee06 20 77 69 74 68 20 74 68 65 20 72 65 74 75 72 6e   with the return
2ee07 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 66   value of this f
2ee08 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 6e 20 6f 72  unction.** in or
2ee09 64 65 72 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20  der to complete 
2ee0a 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2ee0b 20 70 72 6f 63 65 73 73 69 6e 67 2e 0a 2a 2a 0a   processing..**.
2ee0c 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  ** If an error o
2ee0d 63 63 75 72 73 2c 20 74 68 69 73 20 72 6f 75 74  ccurs, this rout
2ee0e 69 6e 65 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c  ine returns NULL
2ee0f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 61 73 69  ..**.** The basi
2ee10 63 20 69 64 65 61 20 69 73 20 74 6f 20 64 6f 20  c idea is to do 
2ee11 61 20 6e 65 73 74 65 64 20 6c 6f 6f 70 2c 20 6f  a nested loop, o
2ee12 6e 65 20 6c 6f 6f 70 20 66 6f 72 20 65 61 63 68  ne loop for each
2ee13 20 74 61 62 6c 65 20 69 6e 0a 2a 2a 20 74 68 65   table in.** the
2ee14 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20   FROM clause of 
2ee15 61 20 73 65 6c 65 63 74 2e 20 20 28 49 4e 53 45  a select.  (INSE
2ee16 52 54 20 61 6e 64 20 55 50 44 41 54 45 20 73 74  RT and UPDATE st
2ee17 61 74 65 6d 65 6e 74 73 20 61 72 65 20 74 68 65  atements are the
2ee18 0a 2a 2a 20 73 61 6d 65 20 61 73 20 61 20 53 45  .** same as a SE
2ee19 4c 45 43 54 20 77 69 74 68 20 6f 6e 6c 79 20 61  LECT with only a
2ee1a 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 69 6e   single table in
2ee1b 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
2ee1c 2e 29 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70  .)  For.** examp
2ee1d 6c 65 2c 20 69 66 20 74 68 65 20 53 51 4c 20 69  le, if the SQL i
2ee1e 73 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  s this:.**.**   
2ee1f 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f      SELECT * FRO
2ee20 4d 20 74 31 2c 20 74 32 2c 20 74 33 20 57 48 45  M t1, t2, t3 WHE
2ee21 52 45 20 2e 2e 2e 3b 0a 2a 2a 0a 2a 2a 20 54 68  RE ...;.**.** Th
2ee22 65 6e 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65  en the code gene
2ee23 72 61 74 65 64 20 69 73 20 63 6f 6e 63 65 70 74  rated is concept
2ee24 75 61 6c 6c 79 20 6c 69 6b 65 20 74 68 65 20 66  ually like the f
2ee25 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  ollowing:.**.** 
2ee26 20 20 20 20 20 66 6f 72 65 61 63 68 20 72 6f 77       foreach row
2ee27 31 20 69 6e 20 74 31 20 64 6f 20 20 20 20 20 20  1 in t1 do      
2ee28 20 5c 20 20 20 20 43 6f 64 65 20 67 65 6e 65 72   \    Code gener
2ee29 61 74 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 66  ated.**        f
2ee2a 6f 72 65 61 63 68 20 72 6f 77 32 20 69 6e 20 74  oreach row2 in t
2ee2b 32 20 64 6f 20 20 20 20 20 20 7c 2d 2d 20 62 79  2 do      |-- by
2ee2c 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67   sqlite3WhereBeg
2ee2d 69 6e 28 29 0a 2a 2a 20 20 20 20 20 20 20 20 20  in().**         
2ee2e 20 66 6f 72 65 61 63 68 20 72 6f 77 33 20 69 6e   foreach row3 in
2ee2f 20 74 33 20 64 6f 20 20 20 2f 0a 2a 2a 20 20 20   t3 do   /.**   
2ee30 20 20 20 20 20 20 20 20 20 2e 2e 2e 0a 2a 2a 20           ....** 
2ee31 20 20 20 20 20 20 20 20 20 65 6e 64 20 20 20 20           end    
2ee32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ee33 20 5c 20 20 20 20 43 6f 64 65 20 67 65 6e 65 72   \    Code gener
2ee34 61 74 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 65  ated.**        e
2ee35 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20  nd              
2ee36 20 20 20 20 20 20 20 20 20 20 7c 2d 2d 20 62 79            |-- by
2ee37 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64   sqlite3WhereEnd
2ee38 28 29 0a 2a 2a 20 20 20 20 20 20 65 6e 64 20 20  ().**      end  
2ee39 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ee3a 20 20 20 20 20 20 20 2f 0a 2a 2a 0a 2a 2a 20 4e         /.**.** N
2ee3b 6f 74 65 20 74 68 61 74 20 74 68 65 20 6c 6f 6f  ote that the loo
2ee3c 70 73 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 20  ps might not be 
2ee3d 6e 65 73 74 65 64 20 69 6e 20 74 68 65 20 6f 72  nested in the or
2ee3e 64 65 72 20 69 6e 20 77 68 69 63 68 20 74 68 65  der in which the
2ee3f 79 0a 2a 2a 20 61 70 70 65 61 72 20 69 6e 20 74  y.** appear in t
2ee40 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69  he FROM clause i
2ee41 66 20 61 20 64 69 66 66 65 72 65 6e 74 20 6f 72  f a different or
2ee42 64 65 72 20 69 73 20 62 65 74 74 65 72 20 61 62  der is better ab
2ee43 6c 65 20 74 6f 20 6d 61 6b 65 0a 2a 2a 20 75 73  le to make.** us
2ee44 65 20 6f 66 20 69 6e 64 69 63 65 73 2e 20 20 4e  e of indices.  N
2ee45 6f 74 65 20 61 6c 73 6f 20 74 68 61 74 20 77 68  ote also that wh
2ee46 65 6e 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74  en the IN operat
2ee47 6f 72 20 61 70 70 65 61 72 73 20 69 6e 0a 2a 2a  or appears in.**
2ee48 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2ee49 65 2c 20 69 74 20 6d 69 67 68 74 20 72 65 73 75  e, it might resu
2ee4a 6c 74 20 69 6e 20 61 64 64 69 74 69 6f 6e 61 6c  lt in additional
2ee4b 20 6e 65 73 74 65 64 20 6c 6f 6f 70 73 20 66 6f   nested loops fo
2ee4c 72 0a 2a 2a 20 73 63 61 6e 6e 69 6e 67 20 74 68  r.** scanning th
2ee4d 72 6f 75 67 68 20 61 6c 6c 20 76 61 6c 75 65 73  rough all values
2ee4e 20 6f 6e 20 74 68 65 20 72 69 67 68 74 2d 68 61   on the right-ha
2ee4f 6e 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 49  nd side of the I
2ee50 4e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61  N..**.** There a
2ee51 72 65 20 42 74 72 65 65 20 63 75 72 73 6f 72 73  re Btree cursors
2ee52 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
2ee53 20 65 61 63 68 20 74 61 62 6c 65 2e 20 20 74 31   each table.  t1
2ee54 20 75 73 65 73 20 63 75 72 73 6f 72 0a 2a 2a 20   uses cursor.** 
2ee55 6e 75 6d 62 65 72 20 70 54 61 62 4c 69 73 74 2d  number pTabList-
2ee56 3e 61 5b 30 5d 2e 69 43 75 72 73 6f 72 2e 20 20  >a[0].iCursor.  
2ee57 74 32 20 75 73 65 73 20 74 68 65 20 63 75 72 73  t2 uses the curs
2ee58 6f 72 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 31  or pTabList->a[1
2ee59 5d 2e 69 43 75 72 73 6f 72 2e 0a 2a 2a 20 41 6e  ].iCursor..** An
2ee5a 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 54 68 69  d so forth.  Thi
2ee5b 73 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61  s routine genera
2ee5c 74 65 73 20 63 6f 64 65 20 74 6f 20 6f 70 65 6e  tes code to open
2ee5d 20 74 68 6f 73 65 20 56 44 42 45 20 63 75 72 73   those VDBE curs
2ee5e 6f 72 73 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74  ors.** and sqlit
2ee5f 65 33 57 68 65 72 65 45 6e 64 28 29 20 67 65 6e  e3WhereEnd() gen
2ee60 65 72 61 74 65 73 20 74 68 65 20 63 6f 64 65 20  erates the code 
2ee61 74 6f 20 63 6c 6f 73 65 20 74 68 65 6d 2e 0a 2a  to close them..*
2ee62 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 74 68  *.** The code th
2ee63 61 74 20 73 71 6c 69 74 65 33 57 68 65 72 65 42  at sqlite3WhereB
2ee64 65 67 69 6e 28 29 20 67 65 6e 65 72 61 74 65 73  egin() generates
2ee65 20 6c 65 61 76 65 73 20 74 68 65 20 63 75 72 73   leaves the curs
2ee66 6f 72 73 20 6e 61 6d 65 64 0a 2a 2a 20 69 6e 20  ors named.** in 
2ee67 70 54 61 62 4c 69 73 74 20 70 6f 69 6e 74 69 6e  pTabList pointin
2ee68 67 20 61 74 20 74 68 65 69 72 20 61 70 70 72 6f  g at their appro
2ee69 70 72 69 61 74 65 20 65 6e 74 72 69 65 73 2e 20  priate entries. 
2ee6a 20 54 68 65 20 5b 2e 2e 2e 5d 20 63 6f 64 65 0a   The [...] code.
2ee6b 2a 2a 20 63 61 6e 20 75 73 65 20 4f 50 5f 43 6f  ** can use OP_Co
2ee6c 6c 75 6d 6e 20 61 6e 64 20 4f 50 5f 52 6f 77 69  lumn and OP_Rowi
2ee6d 64 20 6f 70 63 6f 64 65 73 20 6f 6e 20 74 68 65  d opcodes on the
2ee6e 73 65 20 63 75 72 73 6f 72 73 20 74 6f 20 65 78  se cursors to ex
2ee6f 74 72 61 63 74 0a 2a 2a 20 64 61 74 61 20 66 72  tract.** data fr
2ee70 6f 6d 20 74 68 65 20 76 61 72 69 6f 75 73 20 74  om the various t
2ee71 61 62 6c 65 73 20 6f 66 20 74 68 65 20 6c 6f 6f  ables of the loo
2ee72 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  p..**.** If the 
2ee73 57 48 45 52 45 20 63 6c 61 75 73 65 20 69 73 20  WHERE clause is 
2ee74 65 6d 70 74 79 2c 20 74 68 65 20 66 6f 72 65 61  empty, the forea
2ee75 63 68 20 6c 6f 6f 70 73 20 6d 75 73 74 20 65 61  ch loops must ea
2ee76 63 68 20 73 63 61 6e 20 74 68 65 69 72 0a 2a 2a  ch scan their.**
2ee77 20 65 6e 74 69 72 65 20 74 61 62 6c 65 73 2e 20   entire tables. 
2ee78 20 54 68 75 73 20 61 20 74 68 72 65 65 2d 77 61   Thus a three-wa
2ee79 79 20 6a 6f 69 6e 20 69 73 20 61 6e 20 4f 28 4e  y join is an O(N
2ee7a 5e 33 29 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20  ^3) operation.  
2ee7b 42 75 74 20 69 66 0a 2a 2a 20 74 68 65 20 74 61  But if.** the ta
2ee7c 62 6c 65 73 20 68 61 76 65 20 69 6e 64 69 63 65  bles have indice
2ee7d 73 20 61 6e 64 20 74 68 65 72 65 20 61 72 65 20  s and there are 
2ee7e 74 65 72 6d 73 20 69 6e 20 74 68 65 20 57 48 45  terms in the WHE
2ee7f 52 45 20 63 6c 61 75 73 65 20 74 68 61 74 0a 2a  RE clause that.*
2ee80 2a 20 72 65 66 65 72 20 74 6f 20 74 68 6f 73 65  * refer to those
2ee81 20 69 6e 64 69 63 65 73 2c 20 61 20 63 6f 6d 70   indices, a comp
2ee82 6c 65 74 65 20 74 61 62 6c 65 20 73 63 61 6e 20  lete table scan 
2ee83 63 61 6e 20 62 65 20 61 76 6f 69 64 65 64 20 61  can be avoided a
2ee84 6e 64 20 74 68 65 0a 2a 2a 20 63 6f 64 65 20 77  nd the.** code w
2ee85 69 6c 6c 20 72 75 6e 20 6d 75 63 68 20 66 61 73  ill run much fas
2ee86 74 65 72 2e 20 20 4d 6f 73 74 20 6f 66 20 74 68  ter.  Most of th
2ee87 65 20 77 6f 72 6b 20 6f 66 20 74 68 69 73 20 72  e work of this r
2ee88 6f 75 74 69 6e 65 20 69 73 20 63 68 65 63 6b 69  outine is checki
2ee89 6e 67 0a 2a 2a 20 74 6f 20 73 65 65 20 69 66 20  ng.** to see if 
2ee8a 74 68 65 72 65 20 61 72 65 20 69 6e 64 69 63 65  there are indice
2ee8b 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 73  s that can be us
2ee8c 65 64 20 74 6f 20 73 70 65 65 64 20 75 70 20 74  ed to speed up t
2ee8d 68 65 20 6c 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54  he loop..**.** T
2ee8e 65 72 6d 73 20 6f 66 20 74 68 65 20 57 48 45 52  erms of the WHER
2ee8f 45 20 63 6c 61 75 73 65 20 61 72 65 20 61 6c 73  E clause are als
2ee90 6f 20 75 73 65 64 20 74 6f 20 6c 69 6d 69 74 20  o used to limit 
2ee91 77 68 69 63 68 20 72 6f 77 73 20 61 63 74 75 61  which rows actua
2ee92 6c 6c 79 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 74  lly.** make it t
2ee93 6f 20 74 68 65 20 22 2e 2e 2e 22 20 69 6e 20 74  o the "..." in t
2ee94 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 74 68 65  he middle of the
2ee95 20 6c 6f 6f 70 2e 20 20 41 66 74 65 72 20 65 61   loop.  After ea
2ee96 63 68 20 22 66 6f 72 65 61 63 68 22 2c 0a 2a 2a  ch "foreach",.**
2ee97 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 57 48   terms of the WH
2ee98 45 52 45 20 63 6c 61 75 73 65 20 74 68 61 74 20  ERE clause that 
2ee99 75 73 65 20 6f 6e 6c 79 20 74 65 72 6d 73 20 69  use only terms i
2ee9a 6e 20 74 68 61 74 20 6c 6f 6f 70 20 61 6e 64 20  n that loop and 
2ee9b 6f 75 74 65 72 0a 2a 2a 20 6c 6f 6f 70 73 20 61  outer.** loops a
2ee9c 72 65 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64  re evaluated and
2ee9d 20 69 66 20 66 61 6c 73 65 20 61 20 6a 75 6d 70   if false a jump
2ee9e 20 69 73 20 6d 61 64 65 20 61 72 6f 75 6e 64 20   is made around 
2ee9f 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 0a 2a  all subsequent.*
2eea0 2a 20 69 6e 6e 65 72 20 6c 6f 6f 70 73 20 28 6f  * inner loops (o
2eea1 72 20 61 72 6f 75 6e 64 20 74 68 65 20 22 2e 2e  r around the "..
2eea2 2e 22 20 69 66 20 74 68 65 20 74 65 73 74 20 6f  ." if the test o
2eea3 63 63 75 72 73 20 77 69 74 68 69 6e 20 74 68 65  ccurs within the
2eea4 20 69 6e 6e 65 72 2d 0a 2a 2a 20 6d 6f 73 74 20   inner-.** most 
2eea5 6c 6f 6f 70 29 0a 2a 2a 0a 2a 2a 20 4f 55 54 45  loop).**.** OUTE
2eea6 52 20 4a 4f 49 4e 53 0a 2a 2a 0a 2a 2a 20 41 6e  R JOINS.**.** An
2eea7 20 6f 75 74 65 72 20 6a 6f 69 6e 20 6f 66 20 74   outer join of t
2eea8 61 62 6c 65 73 20 74 31 20 61 6e 64 20 74 32 20  ables t1 and t2 
2eea9 69 73 20 63 6f 6e 63 65 70 74 61 6c 6c 79 20 63  is conceptally c
2eeaa 6f 64 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  oded as follows:
2eeab 0a 2a 2a 0a 2a 2a 20 20 20 20 66 6f 72 65 61 63  .**.**    foreac
2eeac 68 20 72 6f 77 31 20 69 6e 20 74 31 20 64 6f 0a  h row1 in t1 do.
2eead 2a 2a 20 20 20 20 20 20 66 6c 61 67 20 3d 20 30  **      flag = 0
2eeae 0a 2a 2a 20 20 20 20 20 20 66 6f 72 65 61 63 68  .**      foreach
2eeaf 20 72 6f 77 32 20 69 6e 20 74 32 20 64 6f 0a 2a   row2 in t2 do.*
2eeb0 2a 20 20 20 20 20 20 20 20 73 74 61 72 74 3a 0a  *        start:.
2eeb1 2a 2a 20 20 20 20 20 20 20 20 20 20 2e 2e 2e 0a  **          ....
2eeb2 2a 2a 20 20 20 20 20 20 20 20 20 20 66 6c 61 67  **          flag
2eeb3 20 3d 20 31 0a 2a 2a 20 20 20 20 20 20 65 6e 64   = 1.**      end
2eeb4 0a 2a 2a 20 20 20 20 20 20 69 66 20 66 6c 61 67  .**      if flag
2eeb5 3d 3d 30 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20  ==0 then.**     
2eeb6 20 20 20 6d 6f 76 65 20 74 68 65 20 72 6f 77 32     move the row2
2eeb7 20 63 75 72 73 6f 72 20 74 6f 20 61 20 6e 75 6c   cursor to a nul
2eeb8 6c 20 72 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20  l row.**        
2eeb9 67 6f 74 6f 20 73 74 61 72 74 0a 2a 2a 20 20 20  goto start.**   
2eeba 20 20 20 66 69 0a 2a 2a 20 20 20 20 65 6e 64 0a     fi.**    end.
2eebb 2a 2a 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 43  **.** ORDER BY C
2eebc 4c 41 55 53 45 20 50 52 4f 43 45 53 53 49 4e 47  LAUSE PROCESSING
2eebd 0a 2a 2a 0a 2a 2a 20 2a 70 70 4f 72 64 65 72 42  .**.** *ppOrderB
2eebe 79 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  y is a pointer t
2eebf 6f 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  o the ORDER BY c
2eec0 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43  lause of a SELEC
2eec1 54 20 73 74 61 74 65 6d 65 6e 74 2c 0a 2a 2a 20  T statement,.** 
2eec2 69 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65 2e  if there is one.
2eec3 20 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f    If there is no
2eec4 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
2eec5 20 6f 72 20 69 66 20 74 68 69 73 20 72 6f 75 74   or if this rout
2eec6 69 6e 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64  ine.** is called
2eec7 20 66 72 6f 6d 20 61 6e 20 55 50 44 41 54 45 20   from an UPDATE 
2eec8 6f 72 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d  or DELETE statem
2eec9 65 6e 74 2c 20 74 68 65 6e 20 70 70 4f 72 64 65  ent, then ppOrde
2eeca 72 42 79 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a  rBy is NULL..**.
2eecb 2a 2a 20 49 66 20 61 6e 20 69 6e 64 65 78 20 63  ** If an index c
2eecc 61 6e 20 62 65 20 75 73 65 64 20 73 6f 20 74 68  an be used so th
2eecd 61 74 20 74 68 65 20 6e 61 74 75 72 61 6c 20 6f  at the natural o
2eece 75 74 70 75 74 20 6f 72 64 65 72 20 6f 66 20 74  utput order of t
2eecf 68 65 20 74 61 62 6c 65 0a 2a 2a 20 73 63 61 6e  he table.** scan
2eed0 20 69 73 20 63 6f 72 72 65 63 74 20 66 6f 72 20   is correct for 
2eed1 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  the ORDER BY cla
2eed2 75 73 65 2c 20 74 68 65 6e 20 74 68 61 74 20 69  use, then that i
2eed3 6e 64 65 78 20 69 73 20 75 73 65 64 20 61 6e 64  ndex is used and
2eed4 0a 2a 2a 20 2a 70 70 4f 72 64 65 72 42 79 20 69  .** *ppOrderBy i
2eed5 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 20 20  s set to NULL.  
2eed6 54 68 69 73 20 69 73 20 61 6e 20 6f 70 74 69 6d  This is an optim
2eed7 69 7a 61 74 69 6f 6e 20 74 68 61 74 20 70 72 65  ization that pre
2eed8 76 65 6e 74 73 20 61 6e 0a 2a 2a 20 75 6e 6e 65  vents an.** unne
2eed9 63 65 73 73 61 72 79 20 73 6f 72 74 20 6f 66 20  cessary sort of 
2eeda 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 69  the result set i
2eedb 66 20 61 6e 20 69 6e 64 65 78 20 61 70 70 72 6f  f an index appro
2eedc 70 72 69 61 74 65 20 66 6f 72 20 74 68 65 0a 2a  priate for the.*
2eedd 2a 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  * ORDER BY claus
2eede 65 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73  e already exists
2eedf 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 77  ..**.** If the w
2eee0 68 65 72 65 20 63 6c 61 75 73 65 20 6c 6f 6f 70  here clause loop
2eee1 73 20 63 61 6e 6e 6f 74 20 62 65 20 61 72 72 61  s cannot be arra
2eee2 6e 67 65 64 20 74 6f 20 70 72 6f 76 69 64 65 20  nged to provide 
2eee3 74 68 65 20 63 6f 72 72 65 63 74 0a 2a 2a 20 6f  the correct.** o
2eee4 75 74 70 75 74 20 6f 72 64 65 72 2c 20 74 68 65  utput order, the
2eee5 6e 20 74 68 65 20 2a 70 70 4f 72 64 65 72 42 79  n the *ppOrderBy
2eee6 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a   is unchanged..*
2eee7 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2eee8 20 57 68 65 72 65 49 6e 66 6f 20 2a 73 71 6c 69   WhereInfo *sqli
2eee9 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 0a 20  te3WhereBegin(. 
2eeea 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2eeeb 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
2eeec 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  rser context */.
2eeed 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c    SrcList *pTabL
2eeee 69 73 74 2c 20 20 20 20 2f 2a 20 41 20 6c 69 73  ist,    /* A lis
2eeef 74 20 6f 66 20 61 6c 6c 20 74 61 62 6c 65 73 20  t of all tables 
2eef0 74 6f 20 62 65 20 73 63 61 6e 6e 65 64 20 2a 2f  to be scanned */
2eef1 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 2c  .  Expr *pWhere,
2eef2 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2eef3 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a  WHERE clause */.
2eef4 20 20 45 78 70 72 4c 69 73 74 20 2a 2a 70 70 4f    ExprList **ppO
2eef5 72 64 65 72 42 79 2c 20 2f 2a 20 41 6e 20 4f 52  rderBy, /* An OR
2eef6 44 45 52 20 42 59 20 63 6c 61 75 73 65 2c 20 6f  DER BY clause, o
2eef7 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 75 31 36 20  r NULL */.  u16 
2eef8 77 63 74 72 6c 46 6c 61 67 73 20 20 20 20 20 20  wctrlFlags      
2eef9 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20    /* One of the 
2eefa 57 48 45 52 45 5f 2a 20 66 6c 61 67 73 20 64 65  WHERE_* flags de
2eefb 66 69 6e 65 64 20 69 6e 20 73 71 6c 69 74 65 49  fined in sqliteI
2eefc 6e 74 2e 68 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  nt.h */.){.  int
2eefd 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
2eefe 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
2eeff 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74  counter */.  int
2ef00 20 6e 42 79 74 65 57 49 6e 66 6f 3b 20 20 20 20   nByteWInfo;    
2ef01 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 2e 20          /* Num. 
2ef02 62 79 74 65 73 20 61 6c 6c 6f 63 61 74 65 64 20  bytes allocated 
2ef03 66 6f 72 20 57 68 65 72 65 49 6e 66 6f 20 73 74  for WhereInfo st
2ef04 72 75 63 74 20 2a 2f 0a 20 20 57 68 65 72 65 49  ruct */.  WhereI
2ef05 6e 66 6f 20 2a 70 57 49 6e 66 6f 3b 20 20 20 20  nfo *pWInfo;    
2ef06 20 20 20 20 20 2f 2a 20 57 69 6c 6c 20 62 65 63       /* Will bec
2ef07 6f 6d 65 20 74 68 65 20 72 65 74 75 72 6e 20 76  ome the return v
2ef08 61 6c 75 65 20 6f 66 20 74 68 69 73 20 66 75 6e  alue of this fun
2ef09 63 74 69 6f 6e 20 2a 2f 0a 20 20 56 64 62 65 20  ction */.  Vdbe 
2ef0a 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
2ef0b 62 65 3b 20 20 20 2f 2a 20 54 68 65 20 76 69 72  be;   /* The vir
2ef0c 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 65 6e  tual database en
2ef0d 67 69 6e 65 20 2a 2f 0a 20 20 42 69 74 6d 61 73  gine */.  Bitmas
2ef0e 6b 20 6e 6f 74 52 65 61 64 79 3b 20 20 20 20 20  k notReady;     
2ef0f 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 73 20       /* Cursors 
2ef10 74 68 61 74 20 61 72 65 20 6e 6f 74 20 79 65 74  that are not yet
2ef11 20 70 6f 73 69 74 69 6f 6e 65 64 20 2a 2f 0a 20   positioned */. 
2ef12 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70   WhereMaskSet *p
2ef13 4d 61 73 6b 53 65 74 3b 20 20 20 20 2f 2a 20 54  MaskSet;    /* T
2ef14 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 61  he expression ma
2ef15 73 6b 20 73 65 74 20 2a 2f 0a 20 20 57 68 65 72  sk set */.  Wher
2ef16 65 43 6c 61 75 73 65 20 2a 70 57 43 3b 20 20 20  eClause *pWC;   
2ef17 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
2ef18 65 63 6f 6d 70 6f 73 69 74 69 6f 6e 20 6f 66 20  ecomposition of 
2ef19 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2ef1a 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63   */.  struct Src
2ef1b 4c 69 73 74 5f 69 74 65 6d 20 2a 70 54 61 62 49  List_item *pTabI
2ef1c 74 65 6d 3b 20 20 2f 2a 20 41 20 73 69 6e 67 6c  tem;  /* A singl
2ef1d 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 70 54 61  e entry from pTa
2ef1e 62 4c 69 73 74 20 2a 2f 0a 20 20 57 68 65 72 65  bList */.  Where
2ef1f 4c 65 76 65 6c 20 2a 70 4c 65 76 65 6c 3b 20 20  Level *pLevel;  
2ef20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20             /* A 
2ef21 73 69 6e 67 6c 65 20 6c 65 76 65 6c 20 69 6e 20  single level in 
2ef22 74 68 65 20 70 57 49 6e 66 6f 20 6c 69 73 74 20  the pWInfo list 
2ef23 2a 2f 0a 20 20 69 6e 74 20 69 46 72 6f 6d 3b 20  */.  int iFrom; 
2ef24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ef25 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 75 6e       /* First un
2ef26 75 73 65 64 20 46 52 4f 4d 20 63 6c 61 75 73 65  used FROM clause
2ef27 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e   element */.  in
2ef28 74 20 61 6e 64 46 6c 61 67 73 3b 20 20 20 20 20  t andFlags;     
2ef29 20 20 20 20 20 20 20 20 20 2f 2a 20 41 4e 44 2d           /* AND-
2ef2a 65 64 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f  ed combination o
2ef2b 66 20 61 6c 6c 20 70 57 43 2d 3e 61 5b 5d 2e 77  f all pWC->a[].w
2ef2c 74 46 6c 61 67 73 20 2a 2f 0a 20 20 73 71 6c 69  tFlags */.  sqli
2ef2d 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20  te3 *db;        
2ef2e 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
2ef2f 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
2ef30 0a 0a 20 20 2f 2a 20 54 68 65 20 6e 75 6d 62 65  ..  /* The numbe
2ef31 72 20 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 74  r of tables in t
2ef32 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69  he FROM clause i
2ef33 73 20 6c 69 6d 69 74 65 64 20 62 79 20 74 68 65  s limited by the
2ef34 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 2a 2a 20   number of.  ** 
2ef35 62 69 74 73 20 69 6e 20 61 20 42 69 74 6d 61 73  bits in a Bitmas
2ef36 6b 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 54  k .  */.  if( pT
2ef37 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3e 42 4d 53  abList->nSrc>BMS
2ef38 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
2ef39 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2ef3a 22 61 74 20 6d 6f 73 74 20 25 64 20 74 61 62 6c  "at most %d tabl
2ef3b 65 73 20 69 6e 20 61 20 6a 6f 69 6e 22 2c 20 42  es in a join", B
2ef3c 4d 53 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  MS);.    return 
2ef3d 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c  0;.  }..  /* All
2ef3e 6f 63 61 74 65 20 61 6e 64 20 69 6e 69 74 69 61  ocate and initia
2ef3f 6c 69 7a 65 20 74 68 65 20 57 68 65 72 65 49 6e  lize the WhereIn
2ef40 66 6f 20 73 74 72 75 63 74 75 72 65 20 74 68 61  fo structure tha
2ef41 74 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68  t will become th
2ef42 65 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 76 61  e.  ** return va
2ef43 6c 75 65 2e 20 41 20 73 69 6e 67 6c 65 20 61 6c  lue. A single al
2ef44 6c 6f 63 61 74 69 6f 6e 20 69 73 20 75 73 65 64  location is used
2ef45 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 57 68   to store the Wh
2ef46 65 72 65 49 6e 66 6f 0a 20 20 2a 2a 20 73 74 72  ereInfo.  ** str
2ef47 75 63 74 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74  uct, the content
2ef48 73 20 6f 66 20 57 68 65 72 65 49 6e 66 6f 2e 61  s of WhereInfo.a
2ef49 5b 5d 2c 20 74 68 65 20 57 68 65 72 65 43 6c 61  [], the WhereCla
2ef4a 75 73 65 20 73 74 72 75 63 74 75 72 65 0a 20 20  use structure.  
2ef4b 2a 2a 20 61 6e 64 20 74 68 65 20 57 68 65 72 65  ** and the Where
2ef4c 4d 61 73 6b 53 65 74 20 73 74 72 75 63 74 75 72  MaskSet structur
2ef4d 65 2e 20 53 69 6e 63 65 20 57 68 65 72 65 43 6c  e. Since WhereCl
2ef4e 61 75 73 65 20 63 6f 6e 74 61 69 6e 73 20 61 6e  ause contains an
2ef4f 20 38 2d 62 79 74 65 0a 20 20 2a 2a 20 66 69 65   8-byte.  ** fie
2ef50 6c 64 20 28 74 79 70 65 20 42 69 74 6d 61 73 6b  ld (type Bitmask
2ef51 29 20 69 74 20 6d 75 73 74 20 62 65 20 61 6c 69  ) it must be ali
2ef52 67 6e 65 64 20 6f 6e 20 61 6e 20 38 2d 62 79 74  gned on an 8-byt
2ef53 65 20 62 6f 75 6e 64 61 72 79 20 6f 6e 0a 20 20  e boundary on.  
2ef54 2a 2a 20 73 6f 6d 65 20 61 72 63 68 69 74 65 63  ** some architec
2ef55 74 75 72 65 73 2e 20 48 65 6e 63 65 20 74 68 65  tures. Hence the
2ef56 20 52 4f 55 4e 44 38 28 29 20 62 65 6c 6f 77 2e   ROUND8() below.
2ef57 0a 20 20 2a 2f 0a 20 20 64 62 20 3d 20 70 50 61  .  */.  db = pPa
2ef58 72 73 65 2d 3e 64 62 3b 0a 20 20 6e 42 79 74 65  rse->db;.  nByte
2ef59 57 49 6e 66 6f 20 3d 20 52 4f 55 4e 44 38 28 73  WInfo = ROUND8(s
2ef5a 69 7a 65 6f 66 28 57 68 65 72 65 49 6e 66 6f 29  izeof(WhereInfo)
2ef5b 2b 28 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  +(pTabList->nSrc
2ef5c 2d 31 29 2a 73 69 7a 65 6f 66 28 57 68 65 72 65  -1)*sizeof(Where
2ef5d 4c 65 76 65 6c 29 29 3b 0a 20 20 70 57 49 6e 66  Level));.  pWInf
2ef5e 6f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  o = sqlite3DbMal
2ef5f 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 0a 20 20 20  locZero(db, .   
2ef60 20 20 20 6e 42 79 74 65 57 49 6e 66 6f 20 2b 20     nByteWInfo + 
2ef61 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 57 68  .      sizeof(Wh
2ef62 65 72 65 43 6c 61 75 73 65 29 20 2b 0a 20 20 20  ereClause) +.   
2ef63 20 20 20 73 69 7a 65 6f 66 28 57 68 65 72 65 4d     sizeof(WhereM
2ef64 61 73 6b 53 65 74 29 0a 20 20 29 3b 0a 20 20 69  askSet).  );.  i
2ef65 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
2ef66 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  led ){.    goto 
2ef67 77 68 65 72 65 42 65 67 69 6e 45 72 72 6f 72 3b  whereBeginError;
2ef68 0a 20 20 7d 0a 20 20 70 57 49 6e 66 6f 2d 3e 6e  .  }.  pWInfo->n
2ef69 4c 65 76 65 6c 20 3d 20 70 54 61 62 4c 69 73 74  Level = pTabList
2ef6a 2d 3e 6e 53 72 63 3b 0a 20 20 70 57 49 6e 66 6f  ->nSrc;.  pWInfo
2ef6b 2d 3e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73  ->pParse = pPars
2ef6c 65 3b 0a 20 20 70 57 49 6e 66 6f 2d 3e 70 54 61  e;.  pWInfo->pTa
2ef6d 62 4c 69 73 74 20 3d 20 70 54 61 62 4c 69 73 74  bList = pTabList
2ef6e 3b 0a 20 20 70 57 49 6e 66 6f 2d 3e 69 42 72 65  ;.  pWInfo->iBre
2ef6f 61 6b 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  ak = sqlite3Vdbe
2ef70 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20  MakeLabel(v);.  
2ef71 70 57 49 6e 66 6f 2d 3e 70 57 43 20 3d 20 70 57  pWInfo->pWC = pW
2ef72 43 20 3d 20 28 57 68 65 72 65 43 6c 61 75 73 65  C = (WhereClause
2ef73 20 2a 29 26 28 28 75 38 20 2a 29 70 57 49 6e 66   *)&((u8 *)pWInf
2ef74 6f 29 5b 6e 42 79 74 65 57 49 6e 66 6f 5d 3b 0a  o)[nByteWInfo];.
2ef75 20 20 70 57 49 6e 66 6f 2d 3e 77 63 74 72 6c 46    pWInfo->wctrlF
2ef76 6c 61 67 73 20 3d 20 77 63 74 72 6c 46 6c 61 67  lags = wctrlFlag
2ef77 73 3b 0a 20 20 70 4d 61 73 6b 53 65 74 20 3d 20  s;.  pMaskSet = 
2ef78 28 57 68 65 72 65 4d 61 73 6b 53 65 74 2a 29 26  (WhereMaskSet*)&
2ef79 70 57 43 5b 31 5d 3b 0a 0a 20 20 2f 2a 20 53 70  pWC[1];..  /* Sp
2ef7a 6c 69 74 20 74 68 65 20 57 48 45 52 45 20 63 6c  lit the WHERE cl
2ef7b 61 75 73 65 20 69 6e 74 6f 20 73 65 70 61 72 61  ause into separa
2ef7c 74 65 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e  te subexpression
2ef7d 73 20 77 68 65 72 65 20 65 61 63 68 0a 20 20 2a  s where each.  *
2ef7e 2a 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20  * subexpression 
2ef7f 69 73 20 73 65 70 61 72 61 74 65 64 20 62 79 20  is separated by 
2ef80 61 6e 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e  an AND operator.
2ef81 0a 20 20 2a 2f 0a 20 20 69 6e 69 74 4d 61 73 6b  .  */.  initMask
2ef82 53 65 74 28 70 4d 61 73 6b 53 65 74 29 3b 0a 20  Set(pMaskSet);. 
2ef83 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e 69 74   whereClauseInit
2ef84 28 70 57 43 2c 20 70 50 61 72 73 65 2c 20 70 4d  (pWC, pParse, pM
2ef85 61 73 6b 53 65 74 29 3b 0a 20 20 73 71 6c 69 74  askSet);.  sqlit
2ef86 65 33 45 78 70 72 43 6f 64 65 43 6f 6e 73 74 61  e3ExprCodeConsta
2ef87 6e 74 73 28 70 50 61 72 73 65 2c 20 70 57 68 65  nts(pParse, pWhe
2ef88 72 65 29 3b 0a 20 20 77 68 65 72 65 53 70 6c 69  re);.  whereSpli
2ef89 74 28 70 57 43 2c 20 70 57 68 65 72 65 2c 20 54  t(pWC, pWhere, T
2ef8a 4b 5f 41 4e 44 29 3b 0a 20 20 20 20 0a 20 20 2f  K_AND);.    .  /
2ef8b 2a 20 53 70 65 63 69 61 6c 20 63 61 73 65 3a 20  * Special case: 
2ef8c 61 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74  a WHERE clause t
2ef8d 68 61 74 20 69 73 20 63 6f 6e 73 74 61 6e 74 2e  hat is constant.
2ef8e 20 20 45 76 61 6c 75 61 74 65 20 74 68 65 0a 20    Evaluate the. 
2ef8f 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 61   ** expression a
2ef90 6e 64 20 65 69 74 68 65 72 20 6a 75 6d 70 20 6f  nd either jump o
2ef91 76 65 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 63  ver all of the c
2ef92 6f 64 65 20 6f 72 20 66 61 6c 6c 20 74 68 72 75  ode or fall thru
2ef93 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 57 68  ..  */.  if( pWh
2ef94 65 72 65 20 26 26 20 28 70 54 61 62 4c 69 73 74  ere && (pTabList
2ef95 2d 3e 6e 53 72 63 3d 3d 30 20 7c 7c 20 73 71 6c  ->nSrc==0 || sql
2ef96 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61  ite3ExprIsConsta
2ef97 6e 74 4e 6f 74 4a 6f 69 6e 28 70 57 68 65 72 65  ntNotJoin(pWhere
2ef98 29 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  )) ){.    sqlite
2ef99 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50 61  3ExprIfFalse(pPa
2ef9a 72 73 65 2c 20 70 57 68 65 72 65 2c 20 70 57 49  rse, pWhere, pWI
2ef9b 6e 66 6f 2d 3e 69 42 72 65 61 6b 2c 20 53 51 4c  nfo->iBreak, SQL
2ef9c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b  ITE_JUMPIFNULL);
2ef9d 0a 20 20 20 20 70 57 68 65 72 65 20 3d 20 30 3b  .    pWhere = 0;
2ef9e 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 73 73 69 67  .  }..  /* Assig
2ef9f 6e 20 61 20 62 69 74 20 66 72 6f 6d 20 74 68 65  n a bit from the
2efa0 20 62 69 74 6d 61 73 6b 20 74 6f 20 65 76 65 72   bitmask to ever
2efa1 79 20 74 65 72 6d 20 69 6e 20 74 68 65 20 46 52  y term in the FR
2efa2 4f 4d 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2a 0a  OM clause..  **.
2efa3 20 20 2a 2a 20 57 68 65 6e 20 61 73 73 69 67 6e    ** When assign
2efa4 69 6e 67 20 62 69 74 6d 61 73 6b 20 76 61 6c 75  ing bitmask valu
2efa5 65 73 20 74 6f 20 46 52 4f 4d 20 63 6c 61 75 73  es to FROM claus
2efa6 65 20 63 75 72 73 6f 72 73 2c 20 69 74 20 6d 75  e cursors, it mu
2efa7 73 74 20 62 65 0a 20 20 2a 2a 20 74 68 65 20 63  st be.  ** the c
2efa8 61 73 65 20 74 68 61 74 20 69 66 20 58 20 69 73  ase that if X is
2efa9 20 74 68 65 20 62 69 74 6d 61 73 6b 20 66 6f 72   the bitmask for
2efaa 20 74 68 65 20 4e 2d 74 68 20 46 52 4f 4d 20 63   the N-th FROM c
2efab 6c 61 75 73 65 20 74 65 72 6d 20 74 68 65 6e 0a  lause term then.
2efac 20 20 2a 2a 20 74 68 65 20 62 69 74 6d 61 73 6b    ** the bitmask
2efad 20 66 6f 72 20 61 6c 6c 20 46 52 4f 4d 20 63 6c   for all FROM cl
2efae 61 75 73 65 20 74 65 72 6d 73 20 74 6f 20 74 68  ause terms to th
2efaf 65 20 6c 65 66 74 20 6f 66 20 74 68 65 20 4e 2d  e left of the N-
2efb0 74 68 20 74 65 72 6d 0a 20 20 2a 2a 20 69 73 20  th term.  ** is 
2efb1 28 58 2d 31 29 2e 20 20 20 41 6e 20 65 78 70 72  (X-1).   An expr
2efb2 65 73 73 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20  ession from the 
2efb3 4f 4e 20 63 6c 61 75 73 65 20 6f 66 20 61 20 4c  ON clause of a L
2efb4 45 46 54 20 4a 4f 49 4e 20 63 61 6e 20 75 73 65  EFT JOIN can use
2efb5 0a 20 20 2a 2a 20 69 74 73 20 45 78 70 72 2e 69  .  ** its Expr.i
2efb6 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 76  RightJoinTable v
2efb7 61 6c 75 65 20 74 6f 20 66 69 6e 64 20 74 68 65  alue to find the
2efb8 20 62 69 74 6d 61 73 6b 20 6f 66 20 74 68 65 20   bitmask of the 
2efb9 72 69 67 68 74 20 74 61 62 6c 65 0a 20 20 2a 2a  right table.  **
2efba 20 6f 66 20 74 68 65 20 6a 6f 69 6e 2e 20 20 53   of the join.  S
2efbb 75 62 74 72 61 63 74 69 6e 67 20 6f 6e 65 20 66  ubtracting one f
2efbc 72 6f 6d 20 74 68 65 20 72 69 67 68 74 20 74 61  rom the right ta
2efbd 62 6c 65 20 62 69 74 6d 61 73 6b 20 67 69 76 65  ble bitmask give
2efbe 73 20 61 0a 20 20 2a 2a 20 62 69 74 6d 61 73 6b  s a.  ** bitmask
2efbf 20 66 6f 72 20 61 6c 6c 20 74 61 62 6c 65 73 20   for all tables 
2efc0 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 20 74  to the left of t
2efc1 68 65 20 6a 6f 69 6e 2e 20 20 4b 6e 6f 77 69 6e  he join.  Knowin
2efc2 67 20 74 68 65 20 62 69 74 6d 61 73 6b 0a 20 20  g the bitmask.  
2efc3 2a 2a 20 66 6f 72 20 61 6c 6c 20 74 61 62 6c 65  ** for all table
2efc4 73 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66  s to the left of
2efc5 20 61 20 6c 65 66 74 20 6a 6f 69 6e 20 69 73 20   a left join is 
2efc6 69 6d 70 6f 72 74 61 6e 74 2e 20 20 54 69 63 6b  important.  Tick
2efc7 65 74 20 23 33 30 31 35 2e 0a 20 20 2a 2a 0a 20  et #3015..  **. 
2efc8 20 2a 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68   ** Configure th
2efc9 65 20 57 68 65 72 65 43 6c 61 75 73 65 2e 76 6d  e WhereClause.vm
2efca 61 73 6b 20 76 61 72 69 61 62 6c 65 20 73 6f 20  ask variable so 
2efcb 74 68 61 74 20 62 69 74 73 20 74 68 61 74 20 63  that bits that c
2efcc 6f 72 72 65 73 70 6f 6e 64 0a 20 20 2a 2a 20 74  orrespond.  ** t
2efcd 6f 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  o virtual table 
2efce 63 75 72 73 6f 72 73 20 61 72 65 20 73 65 74 2e  cursors are set.
2efcf 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74 6f   This is used to
2efd0 20 73 65 6c 65 63 74 69 76 65 6c 79 20 64 69 73   selectively dis
2efd1 61 62 6c 65 20 0a 20 20 2a 2a 20 74 68 65 20 4f  able .  ** the O
2efd2 52 2d 74 6f 2d 49 4e 20 74 72 61 6e 73 66 6f 72  R-to-IN transfor
2efd3 6d 61 74 69 6f 6e 20 69 6e 20 65 78 70 72 41 6e  mation in exprAn
2efd4 61 6c 79 7a 65 4f 72 54 65 72 6d 28 29 2e 20 49  alyzeOrTerm(). I
2efd5 74 20 69 73 20 6e 6f 74 20 68 65 6c 70 66 75 6c  t is not helpful
2efd6 20 0a 20 20 2a 2a 20 77 69 74 68 20 76 69 72 74   .  ** with virt
2efd7 75 61 6c 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f  ual tables..  */
2efd8 0a 20 20 61 73 73 65 72 74 28 20 70 57 43 2d 3e  .  assert( pWC->
2efd9 76 6d 61 73 6b 3d 3d 30 20 26 26 20 70 4d 61 73  vmask==0 && pMas
2efda 6b 53 65 74 2d 3e 6e 3d 3d 30 20 29 3b 0a 20 20  kSet->n==0 );.  
2efdb 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 4c  for(i=0; i<pTabL
2efdc 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b  ist->nSrc; i++){
2efdd 0a 20 20 20 20 63 72 65 61 74 65 4d 61 73 6b 28  .    createMask(
2efde 70 4d 61 73 6b 53 65 74 2c 20 70 54 61 62 4c 69  pMaskSet, pTabLi
2efdf 73 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72  st->a[i].iCursor
2efe0 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
2efe1 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
2efe2 42 4c 45 0a 20 20 20 20 69 66 28 20 41 4c 57 41  BLE.    if( ALWA
2efe3 59 53 28 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69  YS(pTabList->a[i
2efe4 5d 2e 70 54 61 62 29 20 26 26 20 49 73 56 69 72  ].pTab) && IsVir
2efe5 74 75 61 6c 28 70 54 61 62 4c 69 73 74 2d 3e 61  tual(pTabList->a
2efe6 5b 69 5d 2e 70 54 61 62 29 20 29 7b 0a 20 20 20  [i].pTab) ){.   
2efe7 20 20 20 70 57 43 2d 3e 76 6d 61 73 6b 20 7c 3d     pWC->vmask |=
2efe8 20 28 28 42 69 74 6d 61 73 6b 29 31 20 3c 3c 20   ((Bitmask)1 << 
2efe9 69 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  i);.    }.#endif
2efea 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 4e 44 45  .  }.#ifndef NDE
2efeb 42 55 47 0a 20 20 7b 0a 20 20 20 20 42 69 74 6d  BUG.  {.    Bitm
2efec 61 73 6b 20 74 6f 54 68 65 4c 65 66 74 20 3d 20  ask toTheLeft = 
2efed 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  0;.    for(i=0; 
2efee 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  i<pTabList->nSrc
2efef 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 42 69  ; i++){.      Bi
2eff0 74 6d 61 73 6b 20 6d 20 3d 20 67 65 74 4d 61 73  tmask m = getMas
2eff1 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70 54 61 62  k(pMaskSet, pTab
2eff2 4c 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73  List->a[i].iCurs
2eff3 6f 72 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  or);.      asser
2eff4 74 28 20 28 6d 2d 31 29 3d 3d 74 6f 54 68 65 4c  t( (m-1)==toTheL
2eff5 65 66 74 20 29 3b 0a 20 20 20 20 20 20 74 6f 54  eft );.      toT
2eff6 68 65 4c 65 66 74 20 7c 3d 20 6d 3b 0a 20 20 20  heLeft |= m;.   
2eff7 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20   }.  }.#endif.. 
2eff8 20 2f 2a 20 41 6e 61 6c 79 7a 65 20 61 6c 6c 20   /* Analyze all 
2eff9 6f 66 20 74 68 65 20 73 75 62 65 78 70 72 65 73  of the subexpres
2effa 73 69 6f 6e 73 2e 20 20 4e 6f 74 65 20 74 68 61  sions.  Note tha
2effb 74 20 65 78 70 72 41 6e 61 6c 79 7a 65 28 29 20  t exprAnalyze() 
2effc 6d 69 67 68 74 0a 20 20 2a 2a 20 61 64 64 20 6e  might.  ** add n
2effd 65 77 20 76 69 72 74 75 61 6c 20 74 65 72 6d 73  ew virtual terms
2effe 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64 20 6f 66   onto the end of
2efff 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2f000 65 2e 20 20 57 65 20 64 6f 20 6e 6f 74 0a 20 20  e.  We do not.  
2f001 2a 2a 20 77 61 6e 74 20 74 6f 20 61 6e 61 6c 79  ** want to analy
2f002 7a 65 20 74 68 65 73 65 20 76 69 72 74 75 61 6c  ze these virtual
2f003 20 74 65 72 6d 73 2c 20 73 6f 20 73 74 61 72 74   terms, so start
2f004 20 61 6e 61 6c 79 7a 69 6e 67 20 61 74 20 74 68   analyzing at th
2f005 65 20 65 6e 64 0a 20 20 2a 2a 20 61 6e 64 20 77  e end.  ** and w
2f006 6f 72 6b 20 66 6f 72 77 61 72 64 20 73 6f 20 74  ork forward so t
2f007 68 61 74 20 74 68 65 20 61 64 64 65 64 20 76 69  hat the added vi
2f008 72 74 75 61 6c 20 74 65 72 6d 73 20 61 72 65 20  rtual terms are 
2f009 6e 65 76 65 72 20 70 72 6f 63 65 73 73 65 64 2e  never processed.
2f00a 0a 20 20 2a 2f 0a 20 20 65 78 70 72 41 6e 61 6c  .  */.  exprAnal
2f00b 79 7a 65 41 6c 6c 28 70 54 61 62 4c 69 73 74 2c  yzeAll(pTabList,
2f00c 20 70 57 43 29 3b 0a 20 20 69 66 28 20 64 62 2d   pWC);.  if( db-
2f00d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
2f00e 0a 20 20 20 20 67 6f 74 6f 20 77 68 65 72 65 42  .    goto whereB
2f00f 65 67 69 6e 45 72 72 6f 72 3b 0a 20 20 7d 0a 0a  eginError;.  }..
2f010 20 20 2f 2a 20 43 68 6f 73 65 20 74 68 65 20 62    /* Chose the b
2f011 65 73 74 20 69 6e 64 65 78 20 74 6f 20 75 73 65  est index to use
2f012 20 66 6f 72 20 65 61 63 68 20 74 61 62 6c 65 20   for each table 
2f013 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  in the FROM clau
2f014 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  se..  **.  ** Th
2f015 69 73 20 6c 6f 6f 70 20 66 69 6c 6c 73 20 69 6e  is loop fills in
2f016 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66   the following f
2f017 69 65 6c 64 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  ields:.  **.  **
2f018 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d 2e 70     pWInfo->a[].p
2f019 49 64 78 20 20 20 20 20 20 54 68 65 20 69 6e 64  Idx      The ind
2f01a 65 78 20 74 6f 20 75 73 65 20 66 6f 72 20 74 68  ex to use for th
2f01b 69 73 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20  is level of the 
2f01c 6c 6f 6f 70 2e 0a 20 20 2a 2a 20 20 20 70 57 49  loop..  **   pWI
2f01d 6e 66 6f 2d 3e 61 5b 5d 2e 77 73 46 6c 61 67 73  nfo->a[].wsFlags
2f01e 20 20 20 57 48 45 52 45 5f 78 78 78 20 66 6c 61     WHERE_xxx fla
2f01f 67 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  gs associated wi
2f020 74 68 20 70 49 64 78 0a 20 20 2a 2a 20 20 20 70  th pIdx.  **   p
2f021 57 49 6e 66 6f 2d 3e 61 5b 5d 2e 6e 45 71 20 20  WInfo->a[].nEq  
2f022 20 20 20 20 20 54 68 65 20 6e 75 6d 62 65 72 20       The number 
2f023 6f 66 20 3d 3d 20 61 6e 64 20 49 4e 20 63 6f 6e  of == and IN con
2f024 73 74 72 61 69 6e 74 73 0a 20 20 2a 2a 20 20 20  straints.  **   
2f025 70 57 49 6e 66 6f 2d 3e 61 5b 5d 2e 69 46 72 6f  pWInfo->a[].iFro
2f026 6d 20 20 20 20 20 57 68 69 63 68 20 74 65 72 6d  m     Which term
2f027 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   of the FROM cla
2f028 75 73 65 20 69 73 20 62 65 69 6e 67 20 63 6f 64  use is being cod
2f029 65 64 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f  ed.  **   pWInfo
2f02a 2d 3e 61 5b 5d 2e 69 54 61 62 43 75 72 20 20 20  ->a[].iTabCur   
2f02b 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20  The VDBE cursor 
2f02c 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65  for the database
2f02d 20 74 61 62 6c 65 0a 20 20 2a 2a 20 20 20 70 57   table.  **   pW
2f02e 49 6e 66 6f 2d 3e 61 5b 5d 2e 69 49 64 78 43 75  Info->a[].iIdxCu
2f02f 72 20 20 20 54 68 65 20 56 44 42 45 20 63 75 72  r   The VDBE cur
2f030 73 6f 72 20 66 6f 72 20 74 68 65 20 69 6e 64 65  sor for the inde
2f031 78 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d  x.  **   pWInfo-
2f032 3e 61 5b 5d 2e 70 54 65 72 6d 20 20 20 20 20 57  >a[].pTerm     W
2f033 68 65 6e 20 77 73 46 6c 61 67 73 3d 3d 57 4f 5f  hen wsFlags==WO_
2f034 4f 52 2c 20 74 68 65 20 4f 52 2d 63 6c 61 75 73  OR, the OR-claus
2f035 65 20 74 65 72 6d 0a 20 20 2a 2a 0a 20 20 2a 2a  e term.  **.  **
2f036 20 54 68 69 73 20 6c 6f 6f 70 20 61 6c 73 6f 20   This loop also 
2f037 66 69 67 75 72 65 73 20 6f 75 74 20 74 68 65 20  figures out the 
2f038 6e 65 73 74 69 6e 67 20 6f 72 64 65 72 20 6f 66  nesting order of
2f039 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 46   tables in the F
2f03a 52 4f 4d 0a 20 20 2a 2a 20 63 6c 61 75 73 65 2e  ROM.  ** clause.
2f03b 0a 20 20 2a 2f 0a 20 20 6e 6f 74 52 65 61 64 79  .  */.  notReady
2f03c 20 3d 20 7e 28 42 69 74 6d 61 73 6b 29 30 3b 0a   = ~(Bitmask)0;.
2f03d 20 20 70 54 61 62 49 74 65 6d 20 3d 20 70 54 61    pTabItem = pTa
2f03e 62 4c 69 73 74 2d 3e 61 3b 0a 20 20 70 4c 65 76  bList->a;.  pLev
2f03f 65 6c 20 3d 20 70 57 49 6e 66 6f 2d 3e 61 3b 0a  el = pWInfo->a;.
2f040 20 20 61 6e 64 46 6c 61 67 73 20 3d 20 7e 30 3b    andFlags = ~0;
2f041 0a 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22  .  WHERETRACE(("
2f042 2a 2a 2a 20 4f 70 74 69 6d 69 7a 65 72 20 53 74  *** Optimizer St
2f043 61 72 74 20 2a 2a 2a 5c 6e 22 29 29 3b 0a 20 20  art ***\n"));.  
2f044 66 6f 72 28 69 3d 69 46 72 6f 6d 3d 30 2c 20 70  for(i=iFrom=0, p
2f045 4c 65 76 65 6c 3d 70 57 49 6e 66 6f 2d 3e 61 3b  Level=pWInfo->a;
2f046 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72   i<pTabList->nSr
2f047 63 3b 20 69 2b 2b 2c 20 70 4c 65 76 65 6c 2b 2b  c; i++, pLevel++
2f048 29 7b 0a 20 20 20 20 57 68 65 72 65 43 6f 73 74  ){.    WhereCost
2f049 20 62 65 73 74 50 6c 61 6e 3b 20 20 20 20 20 20   bestPlan;      
2f04a 20 20 20 2f 2a 20 4d 6f 73 74 20 65 66 66 69 63     /* Most effic
2f04b 69 65 6e 74 20 70 6c 61 6e 20 73 65 65 6e 20 73  ient plan seen s
2f04c 6f 20 66 61 72 20 2a 2f 0a 20 20 20 20 49 6e 64  o far */.    Ind
2f04d 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20  ex *pIdx;       
2f04e 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
2f04f 78 20 66 6f 72 20 46 52 4f 4d 20 74 61 62 6c 65  x for FROM table
2f050 20 61 74 20 70 54 61 62 49 74 65 6d 20 2a 2f 0a   at pTabItem */.
2f051 20 20 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 20      int j;      
2f052 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f053 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f  /* For looping o
2f054 76 65 72 20 46 52 4f 4d 20 74 61 62 6c 65 73 20  ver FROM tables 
2f055 2a 2f 0a 20 20 20 20 69 6e 74 20 62 65 73 74 4a  */.    int bestJ
2f056 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 20 20   = -1;          
2f057 20 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20     /* The value 
2f058 6f 66 20 6a 20 2a 2f 0a 20 20 20 20 42 69 74 6d  of j */.    Bitm
2f059 61 73 6b 20 6d 3b 20 20 20 20 20 20 20 20 20 20  ask m;          
2f05a 20 20 20 20 20 20 20 20 2f 2a 20 42 69 74 6d 61          /* Bitma
2f05b 73 6b 20 76 61 6c 75 65 20 66 6f 72 20 6a 20 6f  sk value for j o
2f05c 72 20 62 65 73 74 4a 20 2a 2f 0a 20 20 20 20 69  r bestJ */.    i
2f05d 6e 74 20 69 73 4f 70 74 69 6d 61 6c 3b 20 20 20  nt isOptimal;   
2f05e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74             /* It
2f05f 65 72 61 74 6f 72 20 66 6f 72 20 6f 70 74 69 6d  erator for optim
2f060 61 6c 2f 6e 6f 6e 2d 6f 70 74 69 6d 61 6c 20 73  al/non-optimal s
2f061 65 61 72 63 68 20 2a 2f 0a 0a 20 20 20 20 6d 65  earch */..    me
2f062 6d 73 65 74 28 26 62 65 73 74 50 6c 61 6e 2c 20  mset(&bestPlan, 
2f063 30 2c 20 73 69 7a 65 6f 66 28 62 65 73 74 50 6c  0, sizeof(bestPl
2f064 61 6e 29 29 3b 0a 20 20 20 20 62 65 73 74 50 6c  an));.    bestPl
2f065 61 6e 2e 72 43 6f 73 74 20 3d 20 53 51 4c 49 54  an.rCost = SQLIT
2f066 45 5f 42 49 47 5f 44 42 4c 3b 0a 0a 20 20 20 20  E_BIG_DBL;..    
2f067 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20  /* Loop through 
2f068 74 68 65 20 72 65 6d 61 69 6e 69 6e 67 20 65 6e  the remaining en
2f069 74 72 69 65 73 20 69 6e 20 74 68 65 20 46 52 4f  tries in the FRO
2f06a 4d 20 63 6c 61 75 73 65 20 74 6f 20 66 69 6e 64  M clause to find
2f06b 20 74 68 65 0a 20 20 20 20 2a 2a 20 6e 65 78 74   the.    ** next
2f06c 20 6e 65 73 74 65 64 20 6c 6f 6f 70 2e 20 54 68   nested loop. Th
2f06d 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 65 6e  e FROM clause en
2f06e 74 72 69 65 73 20 6d 61 79 20 62 65 20 69 74 65  tries may be ite
2f06f 72 61 74 65 64 20 74 68 72 6f 75 67 68 0a 20 20  rated through.  
2f070 20 20 2a 2a 20 65 69 74 68 65 72 20 6f 6e 63 65    ** either once
2f071 20 6f 72 20 74 77 69 63 65 2e 20 0a 20 20 20 20   or twice. .    
2f072 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 66 69  **.    ** The fi
2f073 72 73 74 20 69 74 65 72 61 74 69 6f 6e 2c 20 77  rst iteration, w
2f074 68 69 63 68 20 69 73 20 61 6c 77 61 79 73 20 70  hich is always p
2f075 65 72 66 6f 72 6d 65 64 2c 20 73 65 61 72 63 68  erformed, search
2f076 65 73 20 66 6f 72 20 74 68 65 0a 20 20 20 20 2a  es for the.    *
2f077 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 65 6e  * FROM clause en
2f078 74 72 79 20 74 68 61 74 20 70 65 72 6d 69 74 73  try that permits
2f079 20 74 68 65 20 6c 6f 77 65 73 74 2d 63 6f 73 74   the lowest-cost
2f07a 2c 20 22 6f 70 74 69 6d 61 6c 22 20 73 63 61 6e  , "optimal" scan
2f07b 2e 20 49 6e 0a 20 20 20 20 2a 2a 20 74 68 69 73  . In.    ** this
2f07c 20 63 6f 6e 74 65 78 74 20 61 6e 20 6f 70 74 69   context an opti
2f07d 6d 61 6c 20 73 63 61 6e 20 69 73 20 6f 6e 65 20  mal scan is one 
2f07e 74 68 61 74 20 75 73 65 73 20 74 68 65 20 73 61  that uses the sa
2f07f 6d 65 20 73 74 72 61 74 65 67 79 0a 20 20 20 20  me strategy.    
2f080 2a 2a 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e  ** for the given
2f081 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 65 6e 74   FROM clause ent
2f082 72 79 20 61 73 20 77 6f 75 6c 64 20 62 65 20 73  ry as would be s
2f083 65 6c 65 63 74 65 64 20 69 66 20 74 68 65 20 65  elected if the e
2f084 6e 74 72 79 0a 20 20 20 20 2a 2a 20 77 65 72 65  ntry.    ** were
2f085 20 75 73 65 64 20 61 73 20 74 68 65 20 69 6e 6e   used as the inn
2f086 65 72 6d 6f 73 74 20 6e 65 73 74 65 64 20 6c 6f  ermost nested lo
2f087 6f 70 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f  op.  In other wo
2f088 72 64 73 2c 20 61 20 74 61 62 6c 65 0a 20 20 20  rds, a table.   
2f089 20 2a 2a 20 69 73 20 63 68 6f 73 65 6e 20 73 75   ** is chosen su
2f08a 63 68 20 74 68 61 74 20 74 68 65 20 63 6f 73 74  ch that the cost
2f08b 20 6f 66 20 72 75 6e 6e 69 6e 67 20 74 68 61 74   of running that
2f08c 20 74 61 62 6c 65 20 63 61 6e 6e 6f 74 20 62 65   table cannot be
2f08d 20 72 65 64 75 63 65 64 0a 20 20 20 20 2a 2a 20   reduced.    ** 
2f08e 62 79 20 77 61 69 74 69 6e 67 20 66 6f 72 20 6f  by waiting for o
2f08f 74 68 65 72 20 74 61 62 6c 65 73 20 74 6f 20 72  ther tables to r
2f090 75 6e 20 66 69 72 73 74 2e 0a 20 20 20 20 2a 2a  un first..    **
2f091 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 65 63 6f  .    ** The seco
2f092 6e 64 20 69 74 65 72 61 74 69 6f 6e 20 69 73 20  nd iteration is 
2f093 6f 6e 6c 79 20 70 65 72 66 6f 72 6d 65 64 20 69  only performed i
2f094 66 20 6e 6f 20 6f 70 74 69 6d 61 6c 20 73 63 61  f no optimal sca
2f095 6e 20 73 74 72 61 74 65 67 69 65 73 0a 20 20 20  n strategies.   
2f096 20 2a 2a 20 77 65 72 65 20 66 6f 75 6e 64 20 62   ** were found b
2f097 79 20 74 68 65 20 66 69 72 73 74 2e 20 54 68 69  y the first. Thi
2f098 73 20 69 74 65 72 61 74 69 6f 6e 20 69 73 20 75  s iteration is u
2f099 73 65 64 20 74 6f 20 73 65 61 72 63 68 20 66 6f  sed to search fo
2f09a 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 6c 6f 77  r the.    ** low
2f09b 65 73 74 20 63 6f 73 74 20 73 63 61 6e 20 6f 76  est cost scan ov
2f09c 65 72 61 6c 6c 2e 0a 20 20 20 20 2a 2a 0a 20 20  erall..    **.  
2f09d 20 20 2a 2a 20 50 72 65 76 69 6f 75 73 20 76 65    ** Previous ve
2f09e 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65  rsions of SQLite
2f09f 20 70 65 72 66 6f 72 6d 65 64 20 6f 6e 6c 79 20   performed only 
2f0a0 74 68 65 20 73 65 63 6f 6e 64 20 69 74 65 72 61  the second itera
2f0a1 74 69 6f 6e 20 2d 0a 20 20 20 20 2a 2a 20 74 68  tion -.    ** th
2f0a2 65 20 6e 65 78 74 20 6f 75 74 65 72 6d 6f 73 74  e next outermost
2f0a3 20 6c 6f 6f 70 20 77 61 73 20 61 6c 77 61 79 73   loop was always
2f0a4 20 74 68 61 74 20 77 69 74 68 20 74 68 65 20 6c   that with the l
2f0a5 6f 77 65 73 74 20 6f 76 65 72 61 6c 6c 0a 20 20  owest overall.  
2f0a6 20 20 2a 2a 20 63 6f 73 74 2e 20 48 6f 77 65 76    ** cost. Howev
2f0a7 65 72 2c 20 74 68 69 73 20 6d 65 61 6e 74 20 74  er, this meant t
2f0a8 68 61 74 20 53 51 4c 69 74 65 20 63 6f 75 6c 64  hat SQLite could
2f0a9 20 73 65 6c 65 63 74 20 74 68 65 20 77 72 6f 6e   select the wron
2f0aa 67 20 70 6c 61 6e 0a 20 20 20 20 2a 2a 20 66 6f  g plan.    ** fo
2f0ab 72 20 73 63 72 69 70 74 73 20 73 75 63 68 20 61  r scripts such a
2f0ac 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  s the following:
2f0ad 0a 20 20 20 20 2a 2a 20 20 20 0a 20 20 20 20 2a  .    **   .    *
2f0ae 2a 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45  *   CREATE TABLE
2f0af 20 74 31 28 61 2c 20 62 29 3b 20 0a 20 20 20 20   t1(a, b); .    
2f0b0 2a 2a 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  **   CREATE TABL
2f0b1 45 20 74 32 28 63 2c 20 64 29 3b 0a 20 20 20 20  E t2(c, d);.    
2f0b2 2a 2a 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52  **   SELECT * FR
2f0b3 4f 4d 20 74 32 2c 20 74 31 20 57 48 45 52 45 20  OM t2, t1 WHERE 
2f0b4 74 32 2e 72 6f 77 69 64 20 3d 20 74 31 2e 61 3b  t2.rowid = t1.a;
2f0b5 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54  .    **.    ** T
2f0b6 68 65 20 62 65 73 74 20 73 74 72 61 74 65 67 79  he best strategy
2f0b7 20 69 73 20 74 6f 20 69 74 65 72 61 74 65 20 74   is to iterate t
2f0b8 68 72 6f 75 67 68 20 74 61 62 6c 65 20 74 31 20  hrough table t1 
2f0b9 66 69 72 73 74 2e 20 48 6f 77 65 76 65 72 20 69  first. However i
2f0ba 74 0a 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20  t.    ** is not 
2f0bb 70 6f 73 73 69 62 6c 65 20 74 6f 20 64 65 74 65  possible to dete
2f0bc 72 6d 69 6e 65 20 74 68 69 73 20 77 69 74 68 20  rmine this with 
2f0bd 61 20 73 69 6d 70 6c 65 20 67 72 65 65 64 79 20  a simple greedy 
2f0be 61 6c 67 6f 72 69 74 68 6d 2e 0a 20 20 20 20 2a  algorithm..    *
2f0bf 2a 20 48 6f 77 65 76 65 72 2c 20 73 69 6e 63 65  * However, since
2f0c0 20 74 68 65 20 63 6f 73 74 20 6f 66 20 61 20 6c   the cost of a l
2f0c1 69 6e 65 61 72 20 73 63 61 6e 20 74 68 72 6f 75  inear scan throu
2f0c2 67 68 20 74 61 62 6c 65 20 74 32 20 69 73 20 74  gh table t2 is t
2f0c3 68 65 20 73 61 6d 65 20 0a 20 20 20 20 2a 2a 20  he same .    ** 
2f0c4 61 73 20 74 68 65 20 63 6f 73 74 20 6f 66 20 61  as the cost of a
2f0c5 20 6c 69 6e 65 61 72 20 73 63 61 6e 20 74 68 72   linear scan thr
2f0c6 6f 75 67 68 20 74 61 62 6c 65 20 74 31 2c 20 61  ough table t1, a
2f0c7 20 73 69 6d 70 6c 65 20 67 72 65 65 64 79 20 0a   simple greedy .
2f0c8 20 20 20 20 2a 2a 20 61 6c 67 6f 72 69 74 68 6d      ** algorithm
2f0c9 20 6d 61 79 20 63 68 6f 6f 73 65 20 74 6f 20 75   may choose to u
2f0ca 73 65 20 74 32 20 66 6f 72 20 74 68 65 20 6f 75  se t2 for the ou
2f0cb 74 65 72 20 6c 6f 6f 70 2c 20 77 68 69 63 68 20  ter loop, which 
2f0cc 69 73 20 61 20 6d 75 63 68 0a 20 20 20 20 2a 2a  is a much.    **
2f0cd 20 63 6f 73 74 6c 69 65 72 20 61 70 70 72 6f 61   costlier approa
2f0ce 63 68 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66  ch..    */.    f
2f0cf 6f 72 28 69 73 4f 70 74 69 6d 61 6c 3d 31 3b 20  or(isOptimal=1; 
2f0d0 69 73 4f 70 74 69 6d 61 6c 3e 3d 30 20 26 26 20  isOptimal>=0 && 
2f0d1 62 65 73 74 4a 3c 30 3b 20 69 73 4f 70 74 69 6d  bestJ<0; isOptim
2f0d2 61 6c 2d 2d 29 7b 0a 20 20 20 20 20 20 42 69 74  al--){.      Bit
2f0d3 6d 61 73 6b 20 6d 61 73 6b 20 3d 20 28 69 73 4f  mask mask = (isO
2f0d4 70 74 69 6d 61 6c 20 3f 20 30 20 3a 20 6e 6f 74  ptimal ? 0 : not
2f0d5 52 65 61 64 79 29 3b 0a 20 20 20 20 20 20 61 73  Ready);.      as
2f0d6 73 65 72 74 28 20 28 70 54 61 62 4c 69 73 74 2d  sert( (pTabList-
2f0d7 3e 6e 53 72 63 2d 69 46 72 6f 6d 29 3e 31 20 7c  >nSrc-iFrom)>1 |
2f0d8 7c 20 69 73 4f 70 74 69 6d 61 6c 20 29 3b 0a 20  | isOptimal );. 
2f0d9 20 20 20 20 20 66 6f 72 28 6a 3d 69 46 72 6f 6d       for(j=iFrom
2f0da 2c 20 70 54 61 62 49 74 65 6d 3d 26 70 54 61 62  , pTabItem=&pTab
2f0db 4c 69 73 74 2d 3e 61 5b 6a 5d 3b 20 6a 3c 70 54  List->a[j]; j<pT
2f0dc 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 6a 2b  abList->nSrc; j+
2f0dd 2b 2c 20 70 54 61 62 49 74 65 6d 2b 2b 29 7b 0a  +, pTabItem++){.
2f0de 20 20 20 20 20 20 20 20 69 6e 74 20 64 6f 4e 6f          int doNo
2f0df 74 52 65 6f 72 64 65 72 3b 20 20 20 20 2f 2a 20  tReorder;    /* 
2f0e0 54 72 75 65 20 69 66 20 74 68 69 73 20 74 61 62  True if this tab
2f0e1 6c 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65  le should not be
2f0e2 20 72 65 6f 72 64 65 72 65 64 20 2a 2f 0a 20 20   reordered */.  
2f0e3 20 20 20 20 20 20 57 68 65 72 65 43 6f 73 74 20        WhereCost 
2f0e4 73 43 6f 73 74 3b 20 20 20 20 20 2f 2a 20 43 6f  sCost;     /* Co
2f0e5 73 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  st information f
2f0e6 72 6f 6d 20 62 65 73 74 5b 56 69 72 74 75 61 6c  rom best[Virtual
2f0e7 5d 49 6e 64 65 78 28 29 20 2a 2f 0a 20 20 20 20  ]Index() */.    
2f0e8 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f      ExprList *pO
2f0e9 72 64 65 72 42 79 3b 20 20 2f 2a 20 4f 52 44 45  rderBy;  /* ORDE
2f0ea 52 20 42 59 20 63 6c 61 75 73 65 20 66 6f 72 20  R BY clause for 
2f0eb 69 6e 64 65 78 20 74 6f 20 6f 70 74 69 6d 69 7a  index to optimiz
2f0ec 65 20 2a 2f 0a 20 20 0a 20 20 20 20 20 20 20 20  e */.  .        
2f0ed 64 6f 4e 6f 74 52 65 6f 72 64 65 72 20 3d 20 20  doNotReorder =  
2f0ee 28 70 54 61 62 49 74 65 6d 2d 3e 6a 6f 69 6e 74  (pTabItem->joint
2f0ef 79 70 65 20 26 20 28 4a 54 5f 4c 45 46 54 7c 4a  ype & (JT_LEFT|J
2f0f0 54 5f 43 52 4f 53 53 29 29 21 3d 30 3b 0a 20 20  T_CROSS))!=0;.  
2f0f1 20 20 20 20 20 20 69 66 28 20 6a 21 3d 69 46 72        if( j!=iFr
2f0f2 6f 6d 20 26 26 20 64 6f 4e 6f 74 52 65 6f 72 64  om && doNotReord
2f0f3 65 72 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  er ) break;.    
2f0f4 20 20 20 20 6d 20 3d 20 67 65 74 4d 61 73 6b 28      m = getMask(
2f0f5 70 4d 61 73 6b 53 65 74 2c 20 70 54 61 62 49 74  pMaskSet, pTabIt
2f0f6 65 6d 2d 3e 69 43 75 72 73 6f 72 29 3b 0a 20 20  em->iCursor);.  
2f0f7 20 20 20 20 20 20 69 66 28 20 28 6d 20 26 20 6e        if( (m & n
2f0f8 6f 74 52 65 61 64 79 29 3d 3d 30 20 29 7b 0a 20  otReady)==0 ){. 
2f0f9 20 20 20 20 20 20 20 20 20 69 66 28 20 6a 3d 3d           if( j==
2f0fa 69 46 72 6f 6d 20 29 20 69 46 72 6f 6d 2b 2b 3b  iFrom ) iFrom++;
2f0fb 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69  .          conti
2f0fc 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  nue;.        }. 
2f0fd 20 20 20 20 20 20 20 70 4f 72 64 65 72 42 79 20         pOrderBy 
2f0fe 3d 20 28 28 69 3d 3d 30 20 26 26 20 70 70 4f 72  = ((i==0 && ppOr
2f0ff 64 65 72 42 79 20 29 3f 2a 70 70 4f 72 64 65 72  derBy )?*ppOrder
2f100 42 79 3a 30 29 3b 0a 20 20 0a 20 20 20 20 20 20  By:0);.  .      
2f101 20 20 61 73 73 65 72 74 28 20 70 54 61 62 49 74    assert( pTabIt
2f102 65 6d 2d 3e 70 54 61 62 20 29 3b 0a 23 69 66 6e  em->pTab );.#ifn
2f103 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2f104 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20  VIRTUALTABLE.   
2f105 20 20 20 20 20 69 66 28 20 49 73 56 69 72 74 75       if( IsVirtu
2f106 61 6c 28 70 54 61 62 49 74 65 6d 2d 3e 70 54 61  al(pTabItem->pTa
2f107 62 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  b) ){.          
2f108 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
2f109 66 6f 20 2a 2a 70 70 20 3d 20 26 70 57 49 6e 66  fo **pp = &pWInf
2f10a 6f 2d 3e 61 5b 6a 5d 2e 70 49 64 78 49 6e 66 6f  o->a[j].pIdxInfo
2f10b 3b 0a 20 20 20 20 20 20 20 20 20 20 62 65 73 74  ;.          best
2f10c 56 69 72 74 75 61 6c 49 6e 64 65 78 28 70 50 61  VirtualIndex(pPa
2f10d 72 73 65 2c 20 70 57 43 2c 20 70 54 61 62 49 74  rse, pWC, pTabIt
2f10e 65 6d 2c 20 6d 61 73 6b 2c 20 70 4f 72 64 65 72  em, mask, pOrder
2f10f 42 79 2c 20 26 73 43 6f 73 74 2c 20 70 70 29 3b  By, &sCost, pp);
2f110 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 0a  .        }else .
2f111 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 7b  #endif.        {
2f112 0a 20 20 20 20 20 20 20 20 20 20 62 65 73 74 42  .          bestB
2f113 74 72 65 65 49 6e 64 65 78 28 70 50 61 72 73 65  treeIndex(pParse
2f114 2c 20 70 57 43 2c 20 70 54 61 62 49 74 65 6d 2c  , pWC, pTabItem,
2f115 20 6d 61 73 6b 2c 20 70 4f 72 64 65 72 42 79 2c   mask, pOrderBy,
2f116 20 26 73 43 6f 73 74 29 3b 0a 20 20 20 20 20 20   &sCost);.      
2f117 20 20 7d 0a 20 20 20 20 20 20 20 20 61 73 73 65    }.        asse
2f118 72 74 28 20 69 73 4f 70 74 69 6d 61 6c 20 7c 7c  rt( isOptimal ||
2f119 20 28 73 43 6f 73 74 2e 75 73 65 64 26 6e 6f 74   (sCost.used&not
2f11a 52 65 61 64 79 29 3d 3d 30 20 29 3b 0a 0a 20 20  Ready)==0 );..  
2f11b 20 20 20 20 20 20 69 66 28 20 28 73 43 6f 73 74        if( (sCost
2f11c 2e 75 73 65 64 26 6e 6f 74 52 65 61 64 79 29 3d  .used&notReady)=
2f11d 3d 30 0a 20 20 20 20 20 20 20 20 20 26 26 20 28  =0.         && (
2f11e 6a 3d 3d 69 46 72 6f 6d 20 7c 7c 20 73 43 6f 73  j==iFrom || sCos
2f11f 74 2e 72 43 6f 73 74 3c 62 65 73 74 50 6c 61 6e  t.rCost<bestPlan
2f120 2e 72 43 6f 73 74 29 20 0a 20 20 20 20 20 20 20  .rCost) .       
2f121 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62 65   ){.          be
2f122 73 74 50 6c 61 6e 20 3d 20 73 43 6f 73 74 3b 0a  stPlan = sCost;.
2f123 20 20 20 20 20 20 20 20 20 20 62 65 73 74 4a 20            bestJ 
2f124 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = j;.        }. 
2f125 20 20 20 20 20 20 20 69 66 28 20 64 6f 4e 6f 74         if( doNot
2f126 52 65 6f 72 64 65 72 20 29 20 62 72 65 61 6b 3b  Reorder ) break;
2f127 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2f128 20 20 20 61 73 73 65 72 74 28 20 62 65 73 74 4a     assert( bestJ
2f129 3e 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  >=0 );.    asser
2f12a 74 28 20 6e 6f 74 52 65 61 64 79 20 26 20 67 65  t( notReady & ge
2f12b 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20  tMask(pMaskSet, 
2f12c 70 54 61 62 4c 69 73 74 2d 3e 61 5b 62 65 73 74  pTabList->a[best
2f12d 4a 5d 2e 69 43 75 72 73 6f 72 29 20 29 3b 0a 20  J].iCursor) );. 
2f12e 20 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22     WHERETRACE(("
2f12f 2a 2a 2a 20 4f 70 74 69 6d 69 7a 65 72 20 73 65  *** Optimizer se
2f130 6c 65 63 74 73 20 74 61 62 6c 65 20 25 64 20 66  lects table %d f
2f131 6f 72 20 6c 6f 6f 70 20 25 64 5c 6e 22 2c 20 62  or loop %d\n", b
2f132 65 73 74 4a 2c 0a 20 20 20 20 20 20 20 20 20 20  estJ,.          
2f133 20 70 4c 65 76 65 6c 2d 70 57 49 6e 66 6f 2d 3e   pLevel-pWInfo->
2f134 61 29 29 3b 0a 20 20 20 20 69 66 28 20 28 62 65  a));.    if( (be
2f135 73 74 50 6c 61 6e 2e 70 6c 61 6e 2e 77 73 46 6c  stPlan.plan.wsFl
2f136 61 67 73 20 26 20 57 48 45 52 45 5f 4f 52 44 45  ags & WHERE_ORDE
2f137 52 42 59 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  RBY)!=0 ){.     
2f138 20 2a 70 70 4f 72 64 65 72 42 79 20 3d 20 30 3b   *ppOrderBy = 0;
2f139 0a 20 20 20 20 7d 0a 20 20 20 20 61 6e 64 46 6c  .    }.    andFl
2f13a 61 67 73 20 26 3d 20 62 65 73 74 50 6c 61 6e 2e  ags &= bestPlan.
2f13b 70 6c 61 6e 2e 77 73 46 6c 61 67 73 3b 0a 20 20  plan.wsFlags;.  
2f13c 20 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 20 3d    pLevel->plan =
2f13d 20 62 65 73 74 50 6c 61 6e 2e 70 6c 61 6e 3b 0a   bestPlan.plan;.
2f13e 20 20 20 20 69 66 28 20 62 65 73 74 50 6c 61 6e      if( bestPlan
2f13f 2e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  .plan.wsFlags & 
2f140 57 48 45 52 45 5f 49 4e 44 45 58 45 44 20 29 7b  WHERE_INDEXED ){
2f141 0a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 69  .      pLevel->i
2f142 49 64 78 43 75 72 20 3d 20 70 50 61 72 73 65 2d  IdxCur = pParse-
2f143 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 7d 65 6c  >nTab++;.    }el
2f144 73 65 7b 0a 20 20 20 20 20 20 70 4c 65 76 65 6c  se{.      pLevel
2f145 2d 3e 69 49 64 78 43 75 72 20 3d 20 2d 31 3b 0a  ->iIdxCur = -1;.
2f146 20 20 20 20 7d 0a 20 20 20 20 6e 6f 74 52 65 61      }.    notRea
2f147 64 79 20 26 3d 20 7e 67 65 74 4d 61 73 6b 28 70  dy &= ~getMask(p
2f148 4d 61 73 6b 53 65 74 2c 20 70 54 61 62 4c 69 73  MaskSet, pTabLis
2f149 74 2d 3e 61 5b 62 65 73 74 4a 5d 2e 69 43 75 72  t->a[bestJ].iCur
2f14a 73 6f 72 29 3b 0a 20 20 20 20 70 4c 65 76 65 6c  sor);.    pLevel
2f14b 2d 3e 69 46 72 6f 6d 20 3d 20 28 75 38 29 62 65  ->iFrom = (u8)be
2f14c 73 74 4a 3b 0a 0a 20 20 20 20 2f 2a 20 43 68 65  stJ;..    /* Che
2f14d 63 6b 20 74 68 61 74 20 69 66 20 74 68 65 20 74  ck that if the t
2f14e 61 62 6c 65 20 73 63 61 6e 6e 65 64 20 62 79 20  able scanned by 
2f14f 74 68 69 73 20 6c 6f 6f 70 20 69 74 65 72 61 74  this loop iterat
2f150 69 6f 6e 20 68 61 64 20 61 6e 0a 20 20 20 20 2a  ion had an.    *
2f151 2a 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c 61  * INDEXED BY cla
2f152 75 73 65 20 61 74 74 61 63 68 65 64 20 74 6f 20  use attached to 
2f153 69 74 2c 20 74 68 61 74 20 74 68 65 20 6e 61 6d  it, that the nam
2f154 65 64 20 69 6e 64 65 78 20 69 73 20 62 65 69 6e  ed index is bein
2f155 67 0a 20 20 20 20 2a 2a 20 75 73 65 64 20 66 6f  g.    ** used fo
2f156 72 20 74 68 65 20 73 63 61 6e 2e 20 49 66 20 6e  r the scan. If n
2f157 6f 74 2c 20 74 68 65 6e 20 71 75 65 72 79 20 63  ot, then query c
2f158 6f 6d 70 69 6c 61 74 69 6f 6e 20 68 61 73 20 66  ompilation has f
2f159 61 69 6c 65 64 2e 0a 20 20 20 20 2a 2a 20 52 65  ailed..    ** Re
2f15a 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 20  turn an error.. 
2f15b 20 20 20 2a 2f 0a 20 20 20 20 70 49 64 78 20 3d     */.    pIdx =
2f15c 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 62 65 73   pTabList->a[bes
2f15d 74 4a 5d 2e 70 49 6e 64 65 78 3b 0a 20 20 20 20  tJ].pIndex;.    
2f15e 69 66 28 20 70 49 64 78 20 29 7b 0a 20 20 20 20  if( pIdx ){.    
2f15f 20 20 69 66 28 20 28 62 65 73 74 50 6c 61 6e 2e    if( (bestPlan.
2f160 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
2f161 48 45 52 45 5f 49 4e 44 45 58 45 44 29 3d 3d 30  HERE_INDEXED)==0
2f162 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
2f163 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
2f164 73 65 2c 20 22 63 61 6e 6e 6f 74 20 75 73 65 20  se, "cannot use 
2f165 69 6e 64 65 78 3a 20 25 73 22 2c 20 70 49 64 78  index: %s", pIdx
2f166 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  ->zName);.      
2f167 20 20 67 6f 74 6f 20 77 68 65 72 65 42 65 67 69    goto whereBegi
2f168 6e 45 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 65  nError;.      }e
2f169 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  lse{.        /* 
2f16a 49 66 20 61 6e 20 49 4e 44 45 58 45 44 20 42 59  If an INDEXED BY
2f16b 20 63 6c 61 75 73 65 20 69 73 20 75 73 65 64 2c   clause is used,
2f16c 20 74 68 65 20 62 65 73 74 49 6e 64 65 78 28 29   the bestIndex()
2f16d 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a 20 20 20   function is.   
2f16e 20 20 20 20 20 2a 2a 20 67 75 61 72 61 6e 74 65       ** guarante
2f16f 65 64 20 74 6f 20 66 69 6e 64 20 74 68 65 20 69  ed to find the i
2f170 6e 64 65 78 20 73 70 65 63 69 66 69 65 64 20 69  ndex specified i
2f171 6e 20 74 68 65 20 49 4e 44 45 58 45 44 20 42 59  n the INDEXED BY
2f172 20 63 6c 61 75 73 65 0a 20 20 20 20 20 20 20 20   clause.        
2f173 2a 2a 20 69 66 20 69 74 20 66 69 6e 64 20 61 6e  ** if it find an
2f174 20 69 6e 64 65 78 20 61 74 20 61 6c 6c 2e 20 2a   index at all. *
2f175 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  /.        assert
2f176 28 20 62 65 73 74 50 6c 61 6e 2e 70 6c 61 6e 2e  ( bestPlan.plan.
2f177 75 2e 70 49 64 78 3d 3d 70 49 64 78 20 29 3b 0a  u.pIdx==pIdx );.
2f178 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
2f179 7d 0a 20 20 57 48 45 52 45 54 52 41 43 45 28 28  }.  WHERETRACE((
2f17a 22 2a 2a 2a 20 4f 70 74 69 6d 69 7a 65 72 20 46  "*** Optimizer F
2f17b 69 6e 69 73 68 65 64 20 2a 2a 2a 5c 6e 22 29 29  inished ***\n"))
2f17c 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ;.  if( pParse->
2f17d 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  nErr || db->mall
2f17e 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
2f17f 67 6f 74 6f 20 77 68 65 72 65 42 65 67 69 6e 45  goto whereBeginE
2f180 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  rror;.  }..  /* 
2f181 49 66 20 74 68 65 20 74 6f 74 61 6c 20 71 75 65  If the total que
2f182 72 79 20 6f 6e 6c 79 20 73 65 6c 65 63 74 73 20  ry only selects 
2f183 61 20 73 69 6e 67 6c 65 20 72 6f 77 2c 20 74 68  a single row, th
2f184 65 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59 0a  en the ORDER BY.
2f185 20 20 2a 2a 20 63 6c 61 75 73 65 20 69 73 20 69    ** clause is i
2f186 72 72 65 6c 65 76 61 6e 74 2e 0a 20 20 2a 2f 0a  rrelevant..  */.
2f187 20 20 69 66 28 20 28 61 6e 64 46 6c 61 67 73 20    if( (andFlags 
2f188 26 20 57 48 45 52 45 5f 55 4e 49 51 55 45 29 21  & WHERE_UNIQUE)!
2f189 3d 30 20 26 26 20 70 70 4f 72 64 65 72 42 79 20  =0 && ppOrderBy 
2f18a 29 7b 0a 20 20 20 20 2a 70 70 4f 72 64 65 72 42  ){.    *ppOrderB
2f18b 79 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  y = 0;.  }..  /*
2f18c 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20 69   If the caller i
2f18d 73 20 61 6e 20 55 50 44 41 54 45 20 6f 72 20 44  s an UPDATE or D
2f18e 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 20  ELETE statement 
2f18f 74 68 61 74 20 69 73 20 72 65 71 75 65 73 74 69  that is requesti
2f190 6e 67 0a 20 20 2a 2a 20 74 6f 20 75 73 65 20 61  ng.  ** to use a
2f191 20 6f 6e 65 2d 70 61 73 73 20 61 6c 67 6f 72 69   one-pass algori
2f192 74 68 6d 2c 20 64 65 74 65 72 6d 69 6e 65 20 69  thm, determine i
2f193 66 20 74 68 69 73 20 69 73 20 61 70 70 72 6f 70  f this is approp
2f194 72 69 61 74 65 2e 0a 20 20 2a 2a 20 54 68 65 20  riate..  ** The 
2f195 6f 6e 65 2d 70 61 73 73 20 61 6c 67 6f 72 69 74  one-pass algorit
2f196 68 6d 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 69 66  hm only works if
2f197 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2f198 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20  e constraints.  
2f199 2a 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  ** the statement
2f19a 20 74 6f 20 75 70 64 61 74 65 20 61 20 73 69 6e   to update a sin
2f19b 67 6c 65 20 72 6f 77 2e 0a 20 20 2a 2f 0a 20 20  gle row..  */.  
2f19c 61 73 73 65 72 74 28 20 28 77 63 74 72 6c 46 6c  assert( (wctrlFl
2f19d 61 67 73 20 26 20 57 48 45 52 45 5f 4f 4e 45 50  ags & WHERE_ONEP
2f19e 41 53 53 5f 44 45 53 49 52 45 44 29 3d 3d 30 20  ASS_DESIRED)==0 
2f19f 7c 7c 20 70 57 49 6e 66 6f 2d 3e 6e 4c 65 76 65  || pWInfo->nLeve
2f1a0 6c 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 28 77  l==1 );.  if( (w
2f1a1 63 74 72 6c 46 6c 61 67 73 20 26 20 57 48 45 52  ctrlFlags & WHER
2f1a2 45 5f 4f 4e 45 50 41 53 53 5f 44 45 53 49 52 45  E_ONEPASS_DESIRE
2f1a3 44 29 21 3d 30 20 26 26 20 28 61 6e 64 46 6c 61  D)!=0 && (andFla
2f1a4 67 73 20 26 20 57 48 45 52 45 5f 55 4e 49 51 55  gs & WHERE_UNIQU
2f1a5 45 29 21 3d 30 20 29 7b 0a 20 20 20 20 70 57 49  E)!=0 ){.    pWI
2f1a6 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50 61 73 73 20 3d  nfo->okOnePass =
2f1a7 20 31 3b 0a 20 20 20 20 70 57 49 6e 66 6f 2d 3e   1;.    pWInfo->
2f1a8 61 5b 30 5d 2e 70 6c 61 6e 2e 77 73 46 6c 61 67  a[0].plan.wsFlag
2f1a9 73 20 26 3d 20 7e 57 48 45 52 45 5f 49 44 58 5f  s &= ~WHERE_IDX_
2f1aa 4f 4e 4c 59 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ONLY;.  }..  /* 
2f1ab 4f 70 65 6e 20 61 6c 6c 20 74 61 62 6c 65 73 20  Open all tables 
2f1ac 69 6e 20 74 68 65 20 70 54 61 62 4c 69 73 74 20  in the pTabList 
2f1ad 61 6e 64 20 61 6e 79 20 69 6e 64 69 63 65 73 20  and any indices 
2f1ae 73 65 6c 65 63 74 65 64 20 66 6f 72 0a 20 20 2a  selected for.  *
2f1af 2a 20 73 65 61 72 63 68 69 6e 67 20 74 68 6f 73  * searching thos
2f1b0 65 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20  e tables..  */. 
2f1b1 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69   sqlite3CodeVeri
2f1b2 66 79 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c  fySchema(pParse,
2f1b3 20 2d 31 29 3b 20 2f 2a 20 49 6e 73 65 72 74 20   -1); /* Insert 
2f1b4 74 68 65 20 63 6f 6f 6b 69 65 20 76 65 72 69 66  the cookie verif
2f1b5 69 65 72 20 47 6f 74 6f 20 2a 2f 0a 20 20 66 6f  ier Goto */.  fo
2f1b6 72 28 69 3d 30 2c 20 70 4c 65 76 65 6c 3d 70 57  r(i=0, pLevel=pW
2f1b7 49 6e 66 6f 2d 3e 61 3b 20 69 3c 70 54 61 62 4c  Info->a; i<pTabL
2f1b8 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20  ist->nSrc; i++, 
2f1b9 70 4c 65 76 65 6c 2b 2b 29 7b 0a 20 20 20 20 54  pLevel++){.    T
2f1ba 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20  able *pTab;     
2f1bb 2f 2a 20 54 61 62 6c 65 20 74 6f 20 6f 70 65 6e  /* Table to open
2f1bc 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 44 62 3b   */.    int iDb;
2f1bd 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
2f1be 78 20 6f 66 20 64 61 74 61 62 61 73 65 20 63 6f  x of database co
2f1bf 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65 2f 69  ntaining table/i
2f1c0 6e 64 65 78 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  ndex */..#ifndef
2f1c1 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50   SQLITE_OMIT_EXP
2f1c2 4c 41 49 4e 0a 20 20 20 20 69 66 28 20 70 50 61  LAIN.    if( pPa
2f1c3 72 73 65 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20  rse->explain==2 
2f1c4 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  ){.      char *z
2f1c5 4d 73 67 3b 0a 20 20 20 20 20 20 73 74 72 75 63  Msg;.      struc
2f1c6 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
2f1c7 70 49 74 65 6d 20 3d 20 26 70 54 61 62 4c 69 73  pItem = &pTabLis
2f1c8 74 2d 3e 61 5b 70 4c 65 76 65 6c 2d 3e 69 46 72  t->a[pLevel->iFr
2f1c9 6f 6d 5d 3b 0a 20 20 20 20 20 20 7a 4d 73 67 20  om];.      zMsg 
2f1ca 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
2f1cb 28 64 62 2c 20 22 54 41 42 4c 45 20 25 73 22 2c  (db, "TABLE %s",
2f1cc 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a   pItem->zName);.
2f1cd 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d        if( pItem-
2f1ce 3e 7a 41 6c 69 61 73 20 29 7b 0a 20 20 20 20 20  >zAlias ){.     
2f1cf 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65     zMsg = sqlite
2f1d0 33 4d 41 70 70 65 6e 64 66 28 64 62 2c 20 7a 4d  3MAppendf(db, zM
2f1d1 73 67 2c 20 22 25 73 20 41 53 20 25 73 22 2c 20  sg, "%s AS %s", 
2f1d2 7a 4d 73 67 2c 20 70 49 74 65 6d 2d 3e 7a 41 6c  zMsg, pItem->zAl
2f1d3 69 61 73 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ias);.      }.  
2f1d4 20 20 20 20 69 66 28 20 28 70 4c 65 76 65 6c 2d      if( (pLevel-
2f1d5 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  >plan.wsFlags & 
2f1d6 57 48 45 52 45 5f 49 4e 44 45 58 45 44 29 21 3d  WHERE_INDEXED)!=
2f1d7 30 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 4d 73  0 ){.        zMs
2f1d8 67 20 3d 20 73 71 6c 69 74 65 33 4d 41 70 70 65  g = sqlite3MAppe
2f1d9 6e 64 66 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25  ndf(db, zMsg, "%
2f1da 73 20 57 49 54 48 20 49 4e 44 45 58 20 25 73 22  s WITH INDEX %s"
2f1db 2c 0a 20 20 20 20 20 20 20 20 20 20 20 7a 4d 73  ,.           zMs
2f1dc 67 2c 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e  g, pLevel->plan.
2f1dd 75 2e 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 3b 0a  u.pIdx->zName);.
2f1de 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
2f1df 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
2f1e0 6c 61 67 73 20 26 20 57 48 45 52 45 5f 4d 55 4c  lags & WHERE_MUL
2f1e1 54 49 5f 4f 52 20 29 7b 0a 20 20 20 20 20 20 20  TI_OR ){.       
2f1e2 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d   zMsg = sqlite3M
2f1e3 41 70 70 65 6e 64 66 28 64 62 2c 20 7a 4d 73 67  Appendf(db, zMsg
2f1e4 2c 20 22 25 73 20 56 49 41 20 4d 55 4c 54 49 2d  , "%s VIA MULTI-
2f1e5 49 4e 44 45 58 20 55 4e 49 4f 4e 22 2c 20 7a 4d  INDEX UNION", zM
2f1e6 73 67 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  sg);.      }else
2f1e7 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61   if( pLevel->pla
2f1e8 6e 2e 77 73 46 6c 61 67 73 20 26 20 28 57 48 45  n.wsFlags & (WHE
2f1e9 52 45 5f 52 4f 57 49 44 5f 45 51 7c 57 48 45 52  RE_ROWID_EQ|WHER
2f1ea 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45 29 20 29  E_ROWID_RANGE) )
2f1eb 7b 0a 20 20 20 20 20 20 20 20 7a 4d 73 67 20 3d  {.        zMsg =
2f1ec 20 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66   sqlite3MAppendf
2f1ed 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20 55  (db, zMsg, "%s U
2f1ee 53 49 4e 47 20 50 52 49 4d 41 52 59 20 4b 45 59  SING PRIMARY KEY
2f1ef 22 2c 20 7a 4d 73 67 29 3b 0a 20 20 20 20 20 20  ", zMsg);.      
2f1f0 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
2f1f1 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
2f1f2 4c 45 0a 20 20 20 20 20 20 65 6c 73 65 20 69 66  LE.      else if
2f1f3 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e  ( (pLevel->plan.
2f1f4 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  wsFlags & WHERE_
2f1f5 56 49 52 54 55 41 4c 54 41 42 4c 45 29 21 3d 30  VIRTUALTABLE)!=0
2f1f6 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
2f1f7 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a  te3_index_info *
2f1f8 70 56 74 61 62 49 64 78 20 3d 20 70 4c 65 76 65  pVtabIdx = pLeve
2f1f9 6c 2d 3e 70 6c 61 6e 2e 75 2e 70 56 74 61 62 49  l->plan.u.pVtabI
2f1fa 64 78 3b 0a 20 20 20 20 20 20 20 20 7a 4d 73 67  dx;.        zMsg
2f1fb 20 3d 20 73 71 6c 69 74 65 33 4d 41 70 70 65 6e   = sqlite3MAppen
2f1fc 64 66 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73  df(db, zMsg, "%s
2f1fd 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 49   VIRTUAL TABLE I
2f1fe 4e 44 45 58 20 25 64 3a 25 73 22 2c 20 7a 4d 73  NDEX %d:%s", zMs
2f1ff 67 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  g,.             
2f200 20 20 20 20 20 20 20 70 56 74 61 62 49 64 78 2d         pVtabIdx-
2f201 3e 69 64 78 4e 75 6d 2c 20 70 56 74 61 62 49 64  >idxNum, pVtabId
2f202 78 2d 3e 69 64 78 53 74 72 29 3b 0a 20 20 20 20  x->idxStr);.    
2f203 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20    }.#endif.     
2f204 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61   if( pLevel->pla
2f205 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52  n.wsFlags & WHER
2f206 45 5f 4f 52 44 45 52 42 59 20 29 7b 0a 20 20 20  E_ORDERBY ){.   
2f207 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69       zMsg = sqli
2f208 74 65 33 4d 41 70 70 65 6e 64 66 28 64 62 2c 20  te3MAppendf(db, 
2f209 7a 4d 73 67 2c 20 22 25 73 20 4f 52 44 45 52 20  zMsg, "%s ORDER 
2f20a 42 59 22 2c 20 7a 4d 73 67 29 3b 0a 20 20 20 20  BY", zMsg);.    
2f20b 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
2f20c 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
2f20d 50 5f 45 78 70 6c 61 69 6e 2c 20 69 2c 20 70 4c  P_Explain, i, pL
2f20e 65 76 65 6c 2d 3e 69 46 72 6f 6d 2c 20 30 2c 20  evel->iFrom, 0, 
2f20f 7a 4d 73 67 2c 20 50 34 5f 44 59 4e 41 4d 49 43  zMsg, P4_DYNAMIC
2f210 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20  );.    }.#endif 
2f211 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45  /* SQLITE_OMIT_E
2f212 58 50 4c 41 49 4e 20 2a 2f 0a 20 20 20 20 70 54  XPLAIN */.    pT
2f213 61 62 49 74 65 6d 20 3d 20 26 70 54 61 62 4c 69  abItem = &pTabLi
2f214 73 74 2d 3e 61 5b 70 4c 65 76 65 6c 2d 3e 69 46  st->a[pLevel->iF
2f215 72 6f 6d 5d 3b 0a 20 20 20 20 70 54 61 62 20 3d  rom];.    pTab =
2f216 20 70 54 61 62 49 74 65 6d 2d 3e 70 54 61 62 3b   pTabItem->pTab;
2f217 0a 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74  .    iDb = sqlit
2f218 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
2f219 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
2f21a 61 29 3b 0a 20 20 20 20 69 66 28 20 28 70 54 61  a);.    if( (pTa
2f21b 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46  b->tabFlags & TF
2f21c 5f 45 70 68 65 6d 65 72 61 6c 29 21 3d 30 20 7c  _Ephemeral)!=0 |
2f21d 7c 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20  | pTab->pSelect 
2f21e 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 23 69 66 6e  ) continue;.#ifn
2f21f 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2f220 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20  VIRTUALTABLE.   
2f221 20 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c   if( (pLevel->pl
2f222 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45  an.wsFlags & WHE
2f223 52 45 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29  RE_VIRTUALTABLE)
2f224 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e  !=0 ){.      con
2f225 73 74 20 63 68 61 72 20 2a 70 56 54 61 62 20 3d  st char *pVTab =
2f226 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73   (const char *)s
2f227 71 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65 28  qlite3GetVTable(
2f228 64 62 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 20  db, pTab);.     
2f229 20 69 6e 74 20 69 43 75 72 20 3d 20 70 54 61 62   int iCur = pTab
2f22a 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20  Item->iCursor;. 
2f22b 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2f22c 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 4f 70  AddOp4(v, OP_VOp
2f22d 65 6e 2c 20 69 43 75 72 2c 20 30 2c 20 30 2c 20  en, iCur, 0, 0, 
2f22e 70 56 54 61 62 2c 20 50 34 5f 56 54 41 42 29 3b  pVTab, P4_VTAB);
2f22f 0a 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69  .    }else.#endi
2f230 66 0a 20 20 20 20 69 66 28 20 28 70 4c 65 76 65  f.    if( (pLeve
2f231 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
2f232 26 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59  & WHERE_IDX_ONLY
2f233 29 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 26 26  )==0.         &&
2f234 20 28 77 63 74 72 6c 46 6c 61 67 73 20 26 20 57   (wctrlFlags & W
2f235 48 45 52 45 5f 4f 4d 49 54 5f 4f 50 45 4e 29 3d  HERE_OMIT_OPEN)=
2f236 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  =0 ){.      int 
2f237 6f 70 20 3d 20 70 57 49 6e 66 6f 2d 3e 6f 6b 4f  op = pWInfo->okO
2f238 6e 65 50 61 73 73 20 3f 20 4f 50 5f 4f 70 65 6e  nePass ? OP_Open
2f239 57 72 69 74 65 20 3a 20 4f 50 5f 4f 70 65 6e 52  Write : OP_OpenR
2f23a 65 61 64 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ead;.      sqlit
2f23b 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72  e3OpenTable(pPar
2f23c 73 65 2c 20 70 54 61 62 49 74 65 6d 2d 3e 69 43  se, pTabItem->iC
2f23d 75 72 73 6f 72 2c 20 69 44 62 2c 20 70 54 61 62  ursor, iDb, pTab
2f23e 2c 20 6f 70 29 3b 0a 20 20 20 20 20 20 69 66 28  , op);.      if(
2f23f 20 21 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50   !pWInfo->okOneP
2f240 61 73 73 20 26 26 20 70 54 61 62 2d 3e 6e 43 6f  ass && pTab->nCo
2f241 6c 3c 42 4d 53 20 29 7b 0a 20 20 20 20 20 20 20  l<BMS ){.       
2f242 20 42 69 74 6d 61 73 6b 20 62 20 3d 20 70 54 61   Bitmask b = pTa
2f243 62 49 74 65 6d 2d 3e 63 6f 6c 55 73 65 64 3b 0a  bItem->colUsed;.
2f244 20 20 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20          int n = 
2f245 30 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 3b  0;.        for(;
2f246 20 62 3b 20 62 3d 62 3e 3e 31 2c 20 6e 2b 2b 29   b; b=b>>1, n++)
2f247 7b 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  {}.        sqlit
2f248 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
2f249 2c 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  , sqlite3VdbeCur
2f24a 72 65 6e 74 41 64 64 72 28 76 29 2d 31 2c 20 53  rentAddr(v)-1, S
2f24b 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52  QLITE_INT_TO_PTR
2f24c 28 6e 29 2c 20 50 34 5f 49 4e 54 33 32 29 3b 0a  (n), P4_INT32);.
2f24d 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
2f24e 6e 3c 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b  n<=pTab->nCol );
2f24f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
2f250 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
2f251 33 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73  3TableLock(pPars
2f252 65 2c 20 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e  e, iDb, pTab->tn
2f253 75 6d 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61  um, 0, pTab->zNa
2f254 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  me);.    }.    p
2f255 4c 65 76 65 6c 2d 3e 69 54 61 62 43 75 72 20 3d  Level->iTabCur =
2f256 20 70 54 61 62 49 74 65 6d 2d 3e 69 43 75 72 73   pTabItem->iCurs
2f257 6f 72 3b 0a 20 20 20 20 69 66 28 20 28 70 4c 65  or;.    if( (pLe
2f258 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  vel->plan.wsFlag
2f259 73 20 26 20 57 48 45 52 45 5f 49 4e 44 45 58 45  s & WHERE_INDEXE
2f25a 44 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 49  D)!=0 ){.      I
2f25b 6e 64 65 78 20 2a 70 49 78 20 3d 20 70 4c 65 76  ndex *pIx = pLev
2f25c 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 3b  el->plan.u.pIdx;
2f25d 0a 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a  .      KeyInfo *
2f25e 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e  pKey = sqlite3In
2f25f 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73  dexKeyinfo(pPars
2f260 65 2c 20 70 49 78 29 3b 0a 20 20 20 20 20 20 69  e, pIx);.      i
2f261 6e 74 20 69 49 64 78 43 75 72 20 3d 20 70 4c 65  nt iIdxCur = pLe
2f262 76 65 6c 2d 3e 69 49 64 78 43 75 72 3b 0a 20 20  vel->iIdxCur;.  
2f263 20 20 20 20 61 73 73 65 72 74 28 20 70 49 78 2d      assert( pIx-
2f264 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e  >pSchema==pTab->
2f265 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 20  pSchema );.     
2f266 20 61 73 73 65 72 74 28 20 69 49 64 78 43 75 72   assert( iIdxCur
2f267 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  >=0 );.      sql
2f268 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
2f269 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20 69  , OP_OpenRead, i
2f26a 49 64 78 43 75 72 2c 20 70 49 78 2d 3e 74 6e 75  IdxCur, pIx->tnu
2f26b 6d 2c 20 69 44 62 2c 0a 20 20 20 20 20 20 20 20  m, iDb,.        
2f26c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f26d 28 63 68 61 72 2a 29 70 4b 65 79 2c 20 50 34 5f  (char*)pKey, P4_
2f26e 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29  KEYINFO_HANDOFF)
2f26f 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d  ;.      VdbeComm
2f270 65 6e 74 28 28 76 2c 20 22 25 73 22 2c 20 70 49  ent((v, "%s", pI
2f271 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20  x->zName));.    
2f272 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64  }.    sqlite3Cod
2f273 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 70 50  eVerifySchema(pP
2f274 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 7d 0a  arse, iDb);.  }.
2f275 20 20 70 57 49 6e 66 6f 2d 3e 69 54 6f 70 20 3d    pWInfo->iTop =
2f276 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
2f277 65 6e 74 41 64 64 72 28 76 29 3b 0a 0a 20 20 2f  entAddr(v);..  /
2f278 2a 20 47 65 6e 65 72 61 74 65 20 74 68 65 20 63  * Generate the c
2f279 6f 64 65 20 74 6f 20 64 6f 20 74 68 65 20 73 65  ode to do the se
2f27a 61 72 63 68 2e 20 20 45 61 63 68 20 69 74 65 72  arch.  Each iter
2f27b 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 72  ation of the for
2f27c 0a 20 20 2a 2a 20 6c 6f 6f 70 20 62 65 6c 6f 77  .  ** loop below
2f27d 20 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20   generates code 
2f27e 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 6e 65 73  for a single nes
2f27f 74 65 64 20 6c 6f 6f 70 20 6f 66 20 74 68 65 20  ted loop of the 
2f280 56 4d 0a 20 20 2a 2a 20 70 72 6f 67 72 61 6d 2e  VM.  ** program.
2f281 0a 20 20 2a 2f 0a 20 20 6e 6f 74 52 65 61 64 79  .  */.  notReady
2f282 20 3d 20 7e 28 42 69 74 6d 61 73 6b 29 30 3b 0a   = ~(Bitmask)0;.
2f283 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61    for(i=0; i<pTa
2f284 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b  bList->nSrc; i++
2f285 29 7b 0a 20 20 20 20 6e 6f 74 52 65 61 64 79 20  ){.    notReady 
2f286 3d 20 63 6f 64 65 4f 6e 65 4c 6f 6f 70 53 74 61  = codeOneLoopSta
2f287 72 74 28 70 57 49 6e 66 6f 2c 20 69 2c 20 77 63  rt(pWInfo, i, wc
2f288 74 72 6c 46 6c 61 67 73 2c 20 6e 6f 74 52 65 61  trlFlags, notRea
2f289 64 79 29 3b 0a 20 20 20 20 70 57 49 6e 66 6f 2d  dy);.    pWInfo-
2f28a 3e 69 43 6f 6e 74 69 6e 75 65 20 3d 20 70 57 49  >iContinue = pWI
2f28b 6e 66 6f 2d 3e 61 5b 69 5d 2e 61 64 64 72 43 6f  nfo->a[i].addrCo
2f28c 6e 74 3b 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20  nt;.  }..#ifdef 
2f28d 53 51 4c 49 54 45 5f 54 45 53 54 20 20 2f 2a 20  SQLITE_TEST  /* 
2f28e 46 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64 20  For testing and 
2f28f 64 65 62 75 67 67 69 6e 67 20 75 73 65 20 6f 6e  debugging use on
2f290 6c 79 20 2a 2f 0a 20 20 2f 2a 20 52 65 63 6f 72  ly */.  /* Recor
2f291 64 20 69 6e 20 74 68 65 20 71 75 65 72 79 20 70  d in the query p
2f292 6c 61 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  lan information 
2f293 61 62 6f 75 74 20 74 68 65 20 63 75 72 72 65 6e  about the curren
2f294 74 20 74 61 62 6c 65 0a 20 20 2a 2a 20 61 6e 64  t table.  ** and
2f295 20 74 68 65 20 69 6e 64 65 78 20 75 73 65 64 20   the index used 
2f296 74 6f 20 61 63 63 65 73 73 20 69 74 20 28 69 66  to access it (if
2f297 20 61 6e 79 29 2e 20 20 49 66 20 74 68 65 20 74   any).  If the t
2f298 61 62 6c 65 20 69 74 73 65 6c 66 0a 20 20 2a 2a  able itself.  **
2f299 20 69 73 20 6e 6f 74 20 75 73 65 64 2c 20 69 74   is not used, it
2f29a 73 20 6e 61 6d 65 20 69 73 20 6a 75 73 74 20 27  s name is just '
2f29b 7b 7d 27 2e 20 20 49 66 20 6e 6f 20 69 6e 64 65  {}'.  If no inde
2f29c 78 20 69 73 20 75 73 65 64 0a 20 20 2a 2a 20 74  x is used.  ** t
2f29d 68 65 20 69 6e 64 65 78 20 69 73 20 6c 69 73 74  he index is list
2f29e 65 64 20 61 73 20 22 7b 7d 22 2e 20 20 49 66 20  ed as "{}".  If 
2f29f 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20  the primary key 
2f2a0 69 73 20 75 73 65 64 20 74 68 65 0a 20 20 2a 2a  is used the.  **
2f2a1 20 69 6e 64 65 78 20 6e 61 6d 65 20 69 73 20 27   index name is '
2f2a2 2a 27 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69  *'..  */.  for(i
2f2a3 3d 30 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e  =0; i<pTabList->
2f2a4 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nSrc; i++){.    
2f2a5 63 68 61 72 20 2a 7a 3b 0a 20 20 20 20 69 6e 74  char *z;.    int
2f2a6 20 6e 3b 0a 20 20 20 20 70 4c 65 76 65 6c 20 3d   n;.    pLevel =
2f2a7 20 26 70 57 49 6e 66 6f 2d 3e 61 5b 69 5d 3b 0a   &pWInfo->a[i];.
2f2a8 20 20 20 20 70 54 61 62 49 74 65 6d 20 3d 20 26      pTabItem = &
2f2a9 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70 4c 65 76  pTabList->a[pLev
2f2aa 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20 20 20  el->iFrom];.    
2f2ab 7a 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 7a 41  z = pTabItem->zA
2f2ac 6c 69 61 73 3b 0a 20 20 20 20 69 66 28 20 7a 3d  lias;.    if( z=
2f2ad 3d 30 20 29 20 7a 20 3d 20 70 54 61 62 49 74 65  =0 ) z = pTabIte
2f2ae 6d 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a  m->pTab->zName;.
2f2af 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 53      n = sqlite3S
2f2b0 74 72 6c 65 6e 33 30 28 7a 29 3b 0a 20 20 20 20  trlen30(z);.    
2f2b1 69 66 28 20 6e 2b 6e 51 50 6c 61 6e 20 3c 20 73  if( n+nQPlan < s
2f2b2 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 71 75  izeof(sqlite3_qu
2f2b3 65 72 79 5f 70 6c 61 6e 29 2d 31 30 20 29 7b 0a  ery_plan)-10 ){.
2f2b4 20 20 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c        if( pLevel
2f2b5 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26  ->plan.wsFlags &
2f2b6 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 20   WHERE_IDX_ONLY 
2f2b7 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70  ){.        memcp
2f2b8 79 28 26 73 71 6c 69 74 65 33 5f 71 75 65 72 79  y(&sqlite3_query
2f2b9 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 2c 20 22  _plan[nQPlan], "
2f2ba 7b 7d 22 2c 20 32 29 3b 0a 20 20 20 20 20 20 20  {}", 2);.       
2f2bb 20 6e 51 50 6c 61 6e 20 2b 3d 20 32 3b 0a 20 20   nQPlan += 2;.  
2f2bc 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2f2bd 20 20 20 6d 65 6d 63 70 79 28 26 73 71 6c 69 74     memcpy(&sqlit
2f2be 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51  e3_query_plan[nQ
2f2bf 50 6c 61 6e 5d 2c 20 7a 2c 20 6e 29 3b 0a 20 20  Plan], z, n);.  
2f2c0 20 20 20 20 20 20 6e 51 50 6c 61 6e 20 2b 3d 20        nQPlan += 
2f2c1 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  n;.      }.     
2f2c2 20 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70   sqlite3_query_p
2f2c3 6c 61 6e 5b 6e 51 50 6c 61 6e 2b 2b 5d 20 3d 20  lan[nQPlan++] = 
2f2c4 27 20 27 3b 0a 20 20 20 20 7d 0a 20 20 20 20 74  ' ';.    }.    t
2f2c5 65 73 74 63 61 73 65 28 20 70 4c 65 76 65 6c 2d  estcase( pLevel-
2f2c6 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  >plan.wsFlags & 
2f2c7 57 48 45 52 45 5f 52 4f 57 49 44 5f 45 51 20 29  WHERE_ROWID_EQ )
2f2c8 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
2f2c9 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
2f2ca 6c 61 67 73 20 26 20 57 48 45 52 45 5f 52 4f 57  lags & WHERE_ROW
2f2cb 49 44 5f 52 41 4e 47 45 20 29 3b 0a 20 20 20 20  ID_RANGE );.    
2f2cc 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  if( pLevel->plan
2f2cd 2e 77 73 46 6c 61 67 73 20 26 20 28 57 48 45 52  .wsFlags & (WHER
2f2ce 45 5f 52 4f 57 49 44 5f 45 51 7c 57 48 45 52 45  E_ROWID_EQ|WHERE
2f2cf 5f 52 4f 57 49 44 5f 52 41 4e 47 45 29 20 29 7b  _ROWID_RANGE) ){
2f2d0 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 73  .      memcpy(&s
2f2d1 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61  qlite3_query_pla
2f2d2 6e 5b 6e 51 50 6c 61 6e 5d 2c 20 22 2a 20 22 2c  n[nQPlan], "* ",
2f2d3 20 32 29 3b 0a 20 20 20 20 20 20 6e 51 50 6c 61   2);.      nQPla
2f2d4 6e 20 2b 3d 20 32 3b 0a 20 20 20 20 7d 65 6c 73  n += 2;.    }els
2f2d5 65 20 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70  e if( (pLevel->p
2f2d6 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48  lan.wsFlags & WH
2f2d7 45 52 45 5f 49 4e 44 45 58 45 44 29 21 3d 30 20  ERE_INDEXED)!=0 
2f2d8 29 7b 0a 20 20 20 20 20 20 6e 20 3d 20 73 71 6c  ){.      n = sql
2f2d9 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4c 65  ite3Strlen30(pLe
2f2da 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78  vel->plan.u.pIdx
2f2db 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  ->zName);.      
2f2dc 69 66 28 20 6e 2b 6e 51 50 6c 61 6e 20 3c 20 73  if( n+nQPlan < s
2f2dd 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 71 75  izeof(sqlite3_qu
2f2de 65 72 79 5f 70 6c 61 6e 29 2d 32 20 29 7b 0a 20  ery_plan)-2 ){. 
2f2df 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 73         memcpy(&s
2f2e0 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61  qlite3_query_pla
2f2e1 6e 5b 6e 51 50 6c 61 6e 5d 2c 20 70 4c 65 76 65  n[nQPlan], pLeve
2f2e2 6c 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 2d 3e  l->plan.u.pIdx->
2f2e3 7a 4e 61 6d 65 2c 20 6e 29 3b 0a 20 20 20 20 20  zName, n);.     
2f2e4 20 20 20 6e 51 50 6c 61 6e 20 2b 3d 20 6e 3b 0a     nQPlan += n;.
2f2e5 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
2f2e6 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61  query_plan[nQPla
2f2e7 6e 2b 2b 5d 20 3d 20 27 20 27 3b 0a 20 20 20 20  n++] = ' ';.    
2f2e8 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
2f2e9 20 20 20 20 20 6d 65 6d 63 70 79 28 26 73 71 6c       memcpy(&sql
2f2ea 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b  ite3_query_plan[
2f2eb 6e 51 50 6c 61 6e 5d 2c 20 22 7b 7d 20 22 2c 20  nQPlan], "{} ", 
2f2ec 33 29 3b 0a 20 20 20 20 20 20 6e 51 50 6c 61 6e  3);.      nQPlan
2f2ed 20 2b 3d 20 33 3b 0a 20 20 20 20 7d 0a 20 20 7d   += 3;.    }.  }
2f2ee 0a 20 20 77 68 69 6c 65 28 20 6e 51 50 6c 61 6e  .  while( nQPlan
2f2ef 3e 30 20 26 26 20 73 71 6c 69 74 65 33 5f 71 75  >0 && sqlite3_qu
2f2f0 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 2d  ery_plan[nQPlan-
2f2f1 31 5d 3d 3d 27 20 27 20 29 7b 0a 20 20 20 20 73  1]==' ' ){.    s
2f2f2 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61  qlite3_query_pla
2f2f3 6e 5b 2d 2d 6e 51 50 6c 61 6e 5d 20 3d 20 30 3b  n[--nQPlan] = 0;
2f2f4 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 71  .  }.  sqlite3_q
2f2f5 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e  uery_plan[nQPlan
2f2f6 5d 20 3d 20 30 3b 0a 20 20 6e 51 50 6c 61 6e 20  ] = 0;.  nQPlan 
2f2f7 3d 20 30 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53  = 0;.#endif /* S
2f2f8 51 4c 49 54 45 5f 54 45 53 54 20 2f 2f 20 54 65  QLITE_TEST // Te
2f2f9 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67  sting and debugg
2f2fa 69 6e 67 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a  ing use only */.
2f2fb 0a 20 20 2f 2a 20 52 65 63 6f 72 64 20 74 68 65  .  /* Record the
2f2fc 20 63 6f 6e 74 69 6e 75 61 74 69 6f 6e 20 61 64   continuation ad
2f2fd 64 72 65 73 73 20 69 6e 20 74 68 65 20 57 68 65  dress in the Whe
2f2fe 72 65 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  reInfo structure
2f2ff 2e 20 20 54 68 65 6e 0a 20 20 2a 2a 20 63 6c 65  .  Then.  ** cle
2f300 61 6e 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e  an up and return
2f301 2e 0a 20 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20  ..  */.  return 
2f302 70 57 49 6e 66 6f 3b 0a 0a 20 20 2f 2a 20 4a 75  pWInfo;..  /* Ju
2f303 6d 70 20 68 65 72 65 20 69 66 20 6d 61 6c 6c 6f  mp here if mallo
2f304 63 20 66 61 69 6c 73 20 2a 2f 0a 77 68 65 72 65  c fails */.where
2f305 42 65 67 69 6e 45 72 72 6f 72 3a 0a 20 20 77 68  BeginError:.  wh
2f306 65 72 65 49 6e 66 6f 46 72 65 65 28 64 62 2c 20  ereInfoFree(db, 
2f307 70 57 49 6e 66 6f 29 3b 0a 20 20 72 65 74 75 72  pWInfo);.  retur
2f308 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  n 0;.}../*.** Ge
2f309 6e 65 72 61 74 65 20 74 68 65 20 65 6e 64 20 6f  nerate the end o
2f30a 66 20 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70  f the WHERE loop
2f30b 2e 20 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20  .  See comments 
2f30c 6f 6e 20 0a 2a 2a 20 73 71 6c 69 74 65 33 57 68  on .** sqlite3Wh
2f30d 65 72 65 42 65 67 69 6e 28 29 20 66 6f 72 20 61  ereBegin() for a
2f30e 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
2f30f 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ation..*/.SQLITE
2f310 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2f311 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 57 68  lite3WhereEnd(Wh
2f312 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 29  ereInfo *pWInfo)
2f313 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  {.  Parse *pPars
2f314 65 20 3d 20 70 57 49 6e 66 6f 2d 3e 70 50 61 72  e = pWInfo->pPar
2f315 73 65 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  se;.  Vdbe *v = 
2f316 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20  pParse->pVdbe;. 
2f317 20 69 6e 74 20 69 3b 0a 20 20 57 68 65 72 65 4c   int i;.  WhereL
2f318 65 76 65 6c 20 2a 70 4c 65 76 65 6c 3b 0a 20 20  evel *pLevel;.  
2f319 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73  SrcList *pTabLis
2f31a 74 20 3d 20 70 57 49 6e 66 6f 2d 3e 70 54 61 62  t = pWInfo->pTab
2f31b 4c 69 73 74 3b 0a 20 20 73 71 6c 69 74 65 33 20  List;.  sqlite3 
2f31c 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
2f31d 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65  ;..  /* Generate
2f31e 20 6c 6f 6f 70 20 74 65 72 6d 69 6e 61 74 69 6f   loop terminatio
2f31f 6e 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 73  n code..  */.  s
2f320 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 43  qlite3ExprCacheC
2f321 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20  lear(pParse);.  
2f322 66 6f 72 28 69 3d 70 54 61 62 4c 69 73 74 2d 3e  for(i=pTabList->
2f323 6e 53 72 63 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d  nSrc-1; i>=0; i-
2f324 2d 29 7b 0a 20 20 20 20 70 4c 65 76 65 6c 20 3d  -){.    pLevel =
2f325 20 26 70 57 49 6e 66 6f 2d 3e 61 5b 69 5d 3b 0a   &pWInfo->a[i];.
2f326 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52      sqlite3VdbeR
2f327 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 70  esolveLabel(v, p
2f328 4c 65 76 65 6c 2d 3e 61 64 64 72 43 6f 6e 74 29  Level->addrCont)
2f329 3b 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c  ;.    if( pLevel
2f32a 2d 3e 6f 70 21 3d 4f 50 5f 4e 6f 6f 70 20 29 7b  ->op!=OP_Noop ){
2f32b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2f32c 62 65 41 64 64 4f 70 32 28 76 2c 20 70 4c 65 76  beAddOp2(v, pLev
2f32d 65 6c 2d 3e 6f 70 2c 20 70 4c 65 76 65 6c 2d 3e  el->op, pLevel->
2f32e 70 31 2c 20 70 4c 65 76 65 6c 2d 3e 70 32 29 3b  p1, pLevel->p2);
2f32f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2f330 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 70 4c  beChangeP5(v, pL
2f331 65 76 65 6c 2d 3e 70 35 29 3b 0a 20 20 20 20 7d  evel->p5);.    }
2f332 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d  .    if( pLevel-
2f333 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  >plan.wsFlags & 
2f334 57 48 45 52 45 5f 49 4e 5f 41 42 4c 45 20 26 26  WHERE_IN_ABLE &&
2f335 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 6e 49   pLevel->u.in.nI
2f336 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 74 72  n>0 ){.      str
2f337 75 63 74 20 49 6e 4c 6f 6f 70 20 2a 70 49 6e 3b  uct InLoop *pIn;
2f338 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20  .      int j;.  
2f339 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52      sqlite3VdbeR
2f33a 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 70  esolveLabel(v, p
2f33b 4c 65 76 65 6c 2d 3e 61 64 64 72 4e 78 74 29 3b  Level->addrNxt);
2f33c 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 70 4c 65  .      for(j=pLe
2f33d 76 65 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e 2c 20 70  vel->u.in.nIn, p
2f33e 49 6e 3d 26 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e  In=&pLevel->u.in
2f33f 2e 61 49 6e 4c 6f 6f 70 5b 6a 2d 31 5d 3b 20 6a  .aInLoop[j-1]; j
2f340 3e 30 3b 20 6a 2d 2d 2c 20 70 49 6e 2d 2d 29 7b  >0; j--, pIn--){
2f341 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2f342 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
2f343 70 49 6e 2d 3e 61 64 64 72 49 6e 54 6f 70 2b 31  pIn->addrInTop+1
2f344 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2f345 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2f346 4f 50 5f 4e 65 78 74 2c 20 70 49 6e 2d 3e 69 43  OP_Next, pIn->iC
2f347 75 72 2c 20 70 49 6e 2d 3e 61 64 64 72 49 6e 54  ur, pIn->addrInT
2f348 6f 70 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  op);.        sql
2f349 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
2f34a 28 76 2c 20 70 49 6e 2d 3e 61 64 64 72 49 6e 54  (v, pIn->addrInT
2f34b 6f 70 2d 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20  op-1);.      }. 
2f34c 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
2f34d 65 65 28 64 62 2c 20 70 4c 65 76 65 6c 2d 3e 75  ee(db, pLevel->u
2f34e 2e 69 6e 2e 61 49 6e 4c 6f 6f 70 29 3b 0a 20 20  .in.aInLoop);.  
2f34f 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
2f350 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
2f351 76 2c 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 42  v, pLevel->addrB
2f352 72 6b 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 65  rk);.    if( pLe
2f353 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 20 29  vel->iLeftJoin )
2f354 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72  {.      int addr
2f355 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73  ;.      addr = s
2f356 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
2f357 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 70 4c  (v, OP_IfPos, pL
2f358 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 29  evel->iLeftJoin)
2f359 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2f35a 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2f35b 4e 75 6c 6c 52 6f 77 2c 20 70 54 61 62 4c 69 73  NullRow, pTabLis
2f35c 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 29  t->a[i].iCursor)
2f35d 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4c 65 76  ;.      if( pLev
2f35e 65 6c 2d 3e 69 49 64 78 43 75 72 3e 3d 30 20 29  el->iIdxCur>=0 )
2f35f 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2f360 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
2f361 50 5f 4e 75 6c 6c 52 6f 77 2c 20 70 4c 65 76 65  P_NullRow, pLeve
2f362 6c 2d 3e 69 49 64 78 43 75 72 29 3b 0a 20 20 20  l->iIdxCur);.   
2f363 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70     }.      if( p
2f364 4c 65 76 65 6c 2d 3e 6f 70 3d 3d 4f 50 5f 52 65  Level->op==OP_Re
2f365 74 75 72 6e 20 29 7b 0a 20 20 20 20 20 20 20 20  turn ){.        
2f366 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2f367 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 70  2(v, OP_Gosub, p
2f368 4c 65 76 65 6c 2d 3e 70 31 2c 20 70 4c 65 76 65  Level->p1, pLeve
2f369 6c 2d 3e 61 64 64 72 46 69 72 73 74 29 3b 0a 20  l->addrFirst);. 
2f36a 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2f36b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2f36c 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp2(v, OP_Goto
2f36d 2c 20 30 2c 20 70 4c 65 76 65 6c 2d 3e 61 64 64  , 0, pLevel->add
2f36e 72 46 69 72 73 74 29 3b 0a 20 20 20 20 20 20 7d  rFirst);.      }
2f36f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2f370 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
2f371 64 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  dr);.    }.  }..
2f372 20 20 2f 2a 20 54 68 65 20 22 62 72 65 61 6b 22    /* The "break"
2f373 20 70 6f 69 6e 74 20 69 73 20 68 65 72 65 2c 20   point is here, 
2f374 6a 75 73 74 20 70 61 73 74 20 74 68 65 20 65 6e  just past the en
2f375 64 20 6f 66 20 74 68 65 20 6f 75 74 65 72 20 6c  d of the outer l
2f376 6f 6f 70 2e 0a 20 20 2a 2a 20 53 65 74 20 69 74  oop..  ** Set it
2f377 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
2f378 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
2f379 28 76 2c 20 70 57 49 6e 66 6f 2d 3e 69 42 72 65  (v, pWInfo->iBre
2f37a 61 6b 29 3b 0a 0a 20 20 2f 2a 20 43 6c 6f 73 65  ak);..  /* Close
2f37b 20 61 6c 6c 20 6f 66 20 74 68 65 20 63 75 72 73   all of the curs
2f37c 6f 72 73 20 74 68 61 74 20 77 65 72 65 20 6f 70  ors that were op
2f37d 65 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 57  ened by sqlite3W
2f37e 68 65 72 65 42 65 67 69 6e 2e 0a 20 20 2a 2f 0a  hereBegin..  */.
2f37f 20 20 66 6f 72 28 69 3d 30 2c 20 70 4c 65 76 65    for(i=0, pLeve
2f380 6c 3d 70 57 49 6e 66 6f 2d 3e 61 3b 20 69 3c 70  l=pWInfo->a; i<p
2f381 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69  TabList->nSrc; i
2f382 2b 2b 2c 20 70 4c 65 76 65 6c 2b 2b 29 7b 0a 20  ++, pLevel++){. 
2f383 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73     struct SrcLis
2f384 74 5f 69 74 65 6d 20 2a 70 54 61 62 49 74 65 6d  t_item *pTabItem
2f385 20 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b   = &pTabList->a[
2f386 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a  pLevel->iFrom];.
2f387 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20      Table *pTab 
2f388 3d 20 70 54 61 62 49 74 65 6d 2d 3e 70 54 61 62  = pTabItem->pTab
2f389 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54  ;.    assert( pT
2f38a 61 62 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28  ab!=0 );.    if(
2f38b 20 28 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73   (pTab->tabFlags
2f38c 20 26 20 54 46 5f 45 70 68 65 6d 65 72 61 6c 29   & TF_Ephemeral)
2f38d 21 3d 30 20 7c 7c 20 70 54 61 62 2d 3e 70 53 65  !=0 || pTab->pSe
2f38e 6c 65 63 74 20 29 20 63 6f 6e 74 69 6e 75 65 3b  lect ) continue;
2f38f 0a 20 20 20 20 69 66 28 20 28 70 57 49 6e 66 6f  .    if( (pWInfo
2f390 2d 3e 77 63 74 72 6c 46 6c 61 67 73 20 26 20 57  ->wctrlFlags & W
2f391 48 45 52 45 5f 4f 4d 49 54 5f 43 4c 4f 53 45 29  HERE_OMIT_CLOSE)
2f392 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ==0 ){.      if(
2f393 20 21 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50   !pWInfo->okOneP
2f394 61 73 73 20 26 26 20 28 70 4c 65 76 65 6c 2d 3e  ass && (pLevel->
2f395 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
2f396 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 29 3d 3d  HERE_IDX_ONLY)==
2f397 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  0 ){.        sql
2f398 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2f399 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 70 54 61 62  , OP_Close, pTab
2f39a 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 29 3b 0a  Item->iCursor);.
2f39b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
2f39c 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e  ( (pLevel->plan.
2f39d 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  wsFlags & WHERE_
2f39e 49 4e 44 45 58 45 44 29 21 3d 30 20 29 7b 0a 20  INDEXED)!=0 ){. 
2f39f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2f3a0 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43  beAddOp1(v, OP_C
2f3a1 6c 6f 73 65 2c 20 70 4c 65 76 65 6c 2d 3e 69 49  lose, pLevel->iI
2f3a2 64 78 43 75 72 29 3b 0a 20 20 20 20 20 20 7d 0a  dxCur);.      }.
2f3a3 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66      }..    /* If
2f3a4 20 74 68 69 73 20 73 63 61 6e 20 75 73 65 73 20   this scan uses 
2f3a5 61 6e 20 69 6e 64 65 78 2c 20 6d 61 6b 65 20 63  an index, make c
2f3a6 6f 64 65 20 73 75 62 73 74 69 74 75 74 69 6f 6e  ode substitution
2f3a7 73 20 74 6f 20 72 65 61 64 20 64 61 74 61 0a 20  s to read data. 
2f3a8 20 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 69     ** from the i
2f3a9 6e 64 65 78 20 69 6e 20 70 72 65 66 65 72 65 6e  ndex in preferen
2f3aa 63 65 20 74 6f 20 74 68 65 20 74 61 62 6c 65 2e  ce to the table.
2f3ab 20 53 6f 6d 65 74 69 6d 65 73 2c 20 74 68 69 73   Sometimes, this
2f3ac 20 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 74 68   means.    ** th
2f3ad 65 20 74 61 62 6c 65 20 6e 65 65 64 20 6e 65 76  e table need nev
2f3ae 65 72 20 62 65 20 72 65 61 64 20 66 72 6f 6d 2e  er be read from.
2f3af 20 54 68 69 73 20 69 73 20 61 20 70 65 72 66 6f   This is a perfo
2f3b0 72 6d 61 6e 63 65 20 62 6f 6f 73 74 2c 0a 20 20  rmance boost,.  
2f3b1 20 20 2a 2a 20 61 73 20 74 68 65 20 76 64 62 65    ** as the vdbe
2f3b2 20 6c 65 76 65 6c 20 77 61 69 74 73 20 75 6e 74   level waits unt
2f3b3 69 6c 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  il the table is 
2f3b4 72 65 61 64 20 62 65 66 6f 72 65 20 61 63 74 75  read before actu
2f3b5 61 6c 6c 79 0a 20 20 20 20 2a 2a 20 73 65 65 6b  ally.    ** seek
2f3b6 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20 63 75  ing the table cu
2f3b7 72 73 6f 72 20 74 6f 20 74 68 65 20 72 65 63 6f  rsor to the reco
2f3b8 72 64 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  rd corresponding
2f3b9 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 0a   to the current.
2f3ba 20 20 20 20 2a 2a 20 70 6f 73 69 74 69 6f 6e 20      ** position 
2f3bb 69 6e 20 74 68 65 20 69 6e 64 65 78 2e 0a 20 20  in the index..  
2f3bc 20 20 2a 2a 20 0a 20 20 20 20 2a 2a 20 43 61 6c    ** .    ** Cal
2f3bd 6c 73 20 74 6f 20 74 68 65 20 63 6f 64 65 20 67  ls to the code g
2f3be 65 6e 65 72 61 74 6f 72 20 69 6e 20 62 65 74 77  enerator in betw
2f3bf 65 65 6e 20 73 71 6c 69 74 65 33 57 68 65 72 65  een sqlite3Where
2f3c0 42 65 67 69 6e 20 61 6e 64 0a 20 20 20 20 2a 2a  Begin and.    **
2f3c1 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64   sqlite3WhereEnd
2f3c2 20 77 69 6c 6c 20 68 61 76 65 20 63 72 65 61 74   will have creat
2f3c3 65 64 20 63 6f 64 65 20 74 68 61 74 20 72 65 66  ed code that ref
2f3c4 65 72 65 6e 63 65 73 20 74 68 65 20 74 61 62 6c  erences the tabl
2f3c5 65 0a 20 20 20 20 2a 2a 20 64 69 72 65 63 74 6c  e.    ** directl
2f3c6 79 2e 20 20 54 68 69 73 20 6c 6f 6f 70 20 73 63  y.  This loop sc
2f3c7 61 6e 73 20 61 6c 6c 20 74 68 61 74 20 63 6f 64  ans all that cod
2f3c8 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 6f 70  e looking for op
2f3c9 63 6f 64 65 73 0a 20 20 20 20 2a 2a 20 74 68 61  codes.    ** tha
2f3ca 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 20  t reference the 
2f3cb 74 61 62 6c 65 20 61 6e 64 20 63 6f 6e 76 65 72  table and conver
2f3cc 74 73 20 74 68 65 6d 20 69 6e 74 6f 20 6f 70 63  ts them into opc
2f3cd 6f 64 65 73 20 74 68 61 74 0a 20 20 20 20 2a 2a  odes that.    **
2f3ce 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 20 69   reference the i
2f3cf 6e 64 65 78 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ndex..    */.   
2f3d0 20 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c   if( (pLevel->pl
2f3d1 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45  an.wsFlags & WHE
2f3d2 52 45 5f 49 4e 44 45 58 45 44 29 21 3d 30 20 26  RE_INDEXED)!=0 &
2f3d3 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  & !db->mallocFai
2f3d4 6c 65 64 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  led){.      int 
2f3d5 6b 2c 20 6a 2c 20 6c 61 73 74 3b 0a 20 20 20 20  k, j, last;.    
2f3d6 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20    VdbeOp *pOp;. 
2f3d7 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78       Index *pIdx
2f3d8 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e   = pLevel->plan.
2f3d9 75 2e 70 49 64 78 3b 0a 20 20 20 20 20 20 69 6e  u.pIdx;.      in
2f3da 74 20 75 73 65 49 6e 64 65 78 4f 6e 6c 79 20 3d  t useIndexOnly =
2f3db 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73   pLevel->plan.ws
2f3dc 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 44  Flags & WHERE_ID
2f3dd 58 5f 4f 4e 4c 59 3b 0a 0a 20 20 20 20 20 20 61  X_ONLY;..      a
2f3de 73 73 65 72 74 28 20 70 49 64 78 21 3d 30 20 29  ssert( pIdx!=0 )
2f3df 3b 0a 20 20 20 20 20 20 70 4f 70 20 3d 20 73 71  ;.      pOp = sq
2f3e0 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 76  lite3VdbeGetOp(v
2f3e1 2c 20 70 57 49 6e 66 6f 2d 3e 69 54 6f 70 29 3b  , pWInfo->iTop);
2f3e2 0a 20 20 20 20 20 20 6c 61 73 74 20 3d 20 73 71  .      last = sq
2f3e3 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
2f3e4 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20 66  Addr(v);.      f
2f3e5 6f 72 28 6b 3d 70 57 49 6e 66 6f 2d 3e 69 54 6f  or(k=pWInfo->iTo
2f3e6 70 3b 20 6b 3c 6c 61 73 74 3b 20 6b 2b 2b 2c 20  p; k<last; k++, 
2f3e7 70 4f 70 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  pOp++){.        
2f3e8 69 66 28 20 70 4f 70 2d 3e 70 31 21 3d 70 4c 65  if( pOp->p1!=pLe
2f3e9 76 65 6c 2d 3e 69 54 61 62 43 75 72 20 29 20 63  vel->iTabCur ) c
2f3ea 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20  ontinue;.       
2f3eb 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65   if( pOp->opcode
2f3ec 3d 3d 4f 50 5f 43 6f 6c 75 6d 6e 20 29 7b 0a 20  ==OP_Column ){. 
2f3ed 20 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30           for(j=0
2f3ee 3b 20 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  ; j<pIdx->nColum
2f3ef 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  n; j++){.       
2f3f0 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32       if( pOp->p2
2f3f1 3d 3d 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e  ==pIdx->aiColumn
2f3f2 5b 6a 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20  [j] ){.         
2f3f3 20 20 20 20 20 70 4f 70 2d 3e 70 32 20 3d 20 6a       pOp->p2 = j
2f3f4 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
2f3f5 70 4f 70 2d 3e 70 31 20 3d 20 70 4c 65 76 65 6c  pOp->p1 = pLevel
2f3f6 2d 3e 69 49 64 78 43 75 72 3b 0a 20 20 20 20 20  ->iIdxCur;.     
2f3f7 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2f3f8 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
2f3f9 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2f3fa 20 20 20 20 61 73 73 65 72 74 28 21 75 73 65 49      assert(!useI
2f3fb 6e 64 65 78 4f 6e 6c 79 20 7c 7c 20 6a 3c 70 49  ndexOnly || j<pI
2f3fc 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20  dx->nColumn);.  
2f3fd 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
2f3fe 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f  pOp->opcode==OP_
2f3ff 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20  Rowid ){.       
2f400 20 20 20 70 4f 70 2d 3e 70 31 20 3d 20 70 4c 65     pOp->p1 = pLe
2f401 76 65 6c 2d 3e 69 49 64 78 43 75 72 3b 0a 20 20  vel->iIdxCur;.  
2f402 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63          pOp->opc
2f403 6f 64 65 20 3d 20 4f 50 5f 49 64 78 52 6f 77 69  ode = OP_IdxRowi
2f404 64 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  d;.        }else
2f405 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65   if( pOp->opcode
2f406 3d 3d 4f 50 5f 4e 75 6c 6c 52 6f 77 20 26 26 20  ==OP_NullRow && 
2f407 75 73 65 49 6e 64 65 78 4f 6e 6c 79 20 29 7b 0a  useIndexOnly ){.
2f408 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 6f            pOp->o
2f409 70 63 6f 64 65 20 3d 20 4f 50 5f 4e 6f 6f 70 3b  pcode = OP_Noop;
2f40a 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2f40b 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
2f40c 2f 2a 20 46 69 6e 61 6c 20 63 6c 65 61 6e 75 70  /* Final cleanup
2f40d 0a 20 20 2a 2f 0a 20 20 77 68 65 72 65 49 6e 66  .  */.  whereInf
2f40e 6f 46 72 65 65 28 64 62 2c 20 70 57 49 6e 66 6f  oFree(db, pWInfo
2f40f 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a  );.  return;.}..
2f410 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2f411 45 6e 64 20 6f 66 20 77 68 65 72 65 2e 63 20 2a  End of where.c *
2f412 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f413 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f414 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2f415 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2f416 42 65 67 69 6e 20 66 69 6c 65 20 70 61 72 73 65  Begin file parse
2f417 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
2f418 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f419 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2f41a 2f 2a 20 44 72 69 76 65 72 20 74 65 6d 70 6c 61  /* Driver templa
2f41b 74 65 20 66 6f 72 20 74 68 65 20 4c 45 4d 4f 4e  te for the LEMON
2f41c 20 70 61 72 73 65 72 20 67 65 6e 65 72 61 74 6f   parser generato
2f41d 72 2e 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  r..** The author
2f41e 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
2f41f 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
2f420 72 63 65 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20  rce code..**.** 
2f421 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20  This version of 
2f422 22 6c 65 6d 70 61 72 2e 63 22 20 69 73 20 6d 6f  "lempar.c" is mo
2f423 64 69 66 69 65 64 2c 20 73 6c 69 67 68 74 6c 79  dified, slightly
2f424 2c 20 66 6f 72 20 75 73 65 20 62 79 20 53 51 4c  , for use by SQL
2f425 69 74 65 2e 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79  ite..** The only
2f426 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 73 20 61   modifications a
2f427 72 65 20 74 68 65 20 61 64 64 69 74 69 6f 6e 20  re the addition 
2f428 6f 66 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 4e  of a couple of N
2f429 45 56 45 52 28 29 0a 2a 2a 20 6d 61 63 72 6f 73  EVER().** macros
2f42a 20 74 6f 20 64 69 73 61 62 6c 65 20 74 65 73 74   to disable test
2f42b 73 20 74 68 61 74 20 61 72 65 20 6e 65 65 64 65  s that are neede
2f42c 64 20 69 6e 20 74 68 65 20 63 61 73 65 20 6f 66  d in the case of
2f42d 20 61 20 67 65 6e 65 72 61 6c 0a 2a 2a 20 4c 41   a general.** LA
2f42e 4c 52 28 31 29 20 67 72 61 6d 6d 61 72 20 62 75  LR(1) grammar bu
2f42f 74 20 77 68 69 63 68 20 61 72 65 20 61 6c 77 61  t which are alwa
2f430 79 73 20 66 61 6c 73 65 20 69 6e 20 74 68 65 0a  ys false in the.
2f431 2a 2a 20 73 70 65 63 69 66 69 63 20 67 72 61 6d  ** specific gram
2f432 6d 61 72 20 75 73 65 64 20 62 79 20 53 51 4c 69  mar used by SQLi
2f433 74 65 2e 0a 2a 2f 0a 2f 2a 20 46 69 72 73 74 20  te..*/./* First 
2f434 6f 66 66 2c 20 63 6f 64 65 20 69 73 20 69 6e 63  off, code is inc
2f435 6c 75 64 65 64 20 74 68 61 74 20 66 6f 6c 6c 6f  luded that follo
2f436 77 73 20 74 68 65 20 22 69 6e 63 6c 75 64 65 22  ws the "include"
2f437 20 64 65 63 6c 61 72 61 74 69 6f 6e 0a 2a 2a 20   declaration.** 
2f438 69 6e 20 74 68 65 20 69 6e 70 75 74 20 67 72 61  in the input gra
2f439 6d 6d 61 72 20 66 69 6c 65 2e 20 2a 2f 0a 0a 0a  mmar file. */...
2f43a 2f 2a 0a 2a 2a 20 44 69 73 61 62 6c 65 20 61 6c  /*.** Disable al
2f43b 6c 20 65 72 72 6f 72 20 72 65 63 6f 76 65 72 79  l error recovery
2f43c 20 70 72 6f 63 65 73 73 69 6e 67 20 69 6e 20 74   processing in t
2f43d 68 65 20 70 61 72 73 65 72 20 70 75 73 68 2d 64  he parser push-d
2f43e 6f 77 6e 0a 2a 2a 20 61 75 74 6f 6d 61 74 6f 6e  own.** automaton
2f43f 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 59 59 4e  ..*/.#define YYN
2f440 4f 45 52 52 4f 52 52 45 43 4f 56 45 52 59 20 31  OERRORRECOVERY 1
2f441 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 79 79 74  ../*.** Make yyt
2f442 65 73 74 63 61 73 65 28 29 20 74 68 65 20 73 61  estcase() the sa
2f443 6d 65 20 61 73 20 74 65 73 74 63 61 73 65 28 29  me as testcase()
2f444 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 79 79 74 65  .*/.#define yyte
2f445 73 74 63 61 73 65 28 58 29 20 74 65 73 74 63 61  stcase(X) testca
2f446 73 65 28 58 29 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  se(X)../*.** An 
2f447 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73  instance of this
2f448 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73   structure holds
2f449 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
2f44a 75 74 20 74 68 65 0a 2a 2a 20 4c 49 4d 49 54 20  ut the.** LIMIT 
2f44b 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45  clause of a SELE
2f44c 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f  CT statement..*/
2f44d 0a 73 74 72 75 63 74 20 4c 69 6d 69 74 56 61 6c  .struct LimitVal
2f44e 20 7b 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d 69   {.  Expr *pLimi
2f44f 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 4c 49 4d  t;    /* The LIM
2f450 49 54 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20  IT expression.  
2f451 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20 69 73  NULL if there is
2f452 20 6e 6f 20 6c 69 6d 69 74 20 2a 2f 0a 20 20 45   no limit */.  E
2f453 78 70 72 20 2a 70 4f 66 66 73 65 74 3b 20 20 20  xpr *pOffset;   
2f454 2f 2a 20 54 68 65 20 4f 46 46 53 45 54 20 65 78  /* The OFFSET ex
2f455 70 72 65 73 73 69 6f 6e 2e 20 20 4e 55 4c 4c 20  pression.  NULL 
2f456 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 6e 65  if there is none
2f457 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e   */.};../*.** An
2f458 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
2f459 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 75  s structure is u
2f45a 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65  sed to store the
2f45b 20 4c 49 4b 45 2c 0a 2a 2a 20 47 4c 4f 42 2c 20   LIKE,.** GLOB, 
2f45c 4e 4f 54 20 4c 49 4b 45 2c 20 61 6e 64 20 4e 4f  NOT LIKE, and NO
2f45d 54 20 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72 73  T GLOB operators
2f45e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4c 69 6b 65  ..*/.struct Like
2f45f 4f 70 20 7b 0a 20 20 54 6f 6b 65 6e 20 65 4f 70  Op {.  Token eOp
2f460 65 72 61 74 6f 72 3b 20 20 2f 2a 20 22 6c 69 6b  erator;  /* "lik
2f461 65 22 20 6f 72 20 22 67 6c 6f 62 22 20 6f 72 20  e" or "glob" or 
2f462 22 72 65 67 65 78 70 22 20 2a 2f 0a 20 20 69 6e  "regexp" */.  in
2f463 74 20 6e 6f 74 3b 20 20 20 20 20 20 20 20 20 2f  t not;         /
2f464 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 4e 4f  * True if the NO
2f465 54 20 6b 65 79 77 6f 72 64 20 69 73 20 70 72 65  T keyword is pre
2f466 73 65 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  sent */.};../*.*
2f467 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
2f468 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
2f469 74 72 75 63 74 75 72 65 20 64 65 73 63 72 69 62  tructure describ
2f46a 65 73 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20  es the event of 
2f46b 61 0a 2a 2a 20 54 52 49 47 47 45 52 2e 20 20 22  a.** TRIGGER.  "
2f46c 61 22 20 69 73 20 74 68 65 20 65 76 65 6e 74 20  a" is the event 
2f46d 74 79 70 65 2c 20 6f 6e 65 20 6f 66 20 54 4b 5f  type, one of TK_
2f46e 55 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52  UPDATE, TK_INSER
2f46f 54 2c 0a 2a 2a 20 54 4b 5f 44 45 4c 45 54 45 2c  T,.** TK_DELETE,
2f470 20 6f 72 20 54 4b 5f 49 4e 53 54 45 41 44 2e 20   or TK_INSTEAD. 
2f471 20 49 66 20 74 68 65 20 65 76 65 6e 74 20 69 73   If the event is
2f472 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a   of the form.**.
2f473 2a 2a 20 20 20 20 20 20 55 50 44 41 54 45 20 4f  **      UPDATE O
2f474 4e 20 28 61 2c 62 2c 63 29 0a 2a 2a 0a 2a 2a 20  N (a,b,c).**.** 
2f475 54 68 65 6e 20 74 68 65 20 22 62 22 20 49 64 4c  Then the "b" IdL
2f476 69 73 74 20 72 65 63 6f 72 64 73 20 74 68 65 20  ist records the 
2f477 6c 69 73 74 20 22 61 2c 62 2c 63 22 2e 0a 2a 2f  list "a,b,c"..*/
2f478 0a 73 74 72 75 63 74 20 54 72 69 67 45 76 65 6e  .struct TrigEven
2f479 74 20 7b 20 69 6e 74 20 61 3b 20 49 64 4c 69 73  t { int a; IdLis
2f47a 74 20 2a 20 62 3b 20 7d 3b 0a 0a 2f 2a 0a 2a 2a  t * b; };../*.**
2f47b 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
2f47c 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 68  this structure h
2f47d 6f 6c 64 73 20 74 68 65 20 41 54 54 41 43 48 20  olds the ATTACH 
2f47e 6b 65 79 20 61 6e 64 20 74 68 65 20 6b 65 79 20  key and the key 
2f47f 74 79 70 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  type..*/.struct 
2f480 41 74 74 61 63 68 4b 65 79 20 7b 20 69 6e 74 20  AttachKey { int 
2f481 74 79 70 65 3b 20 20 54 6f 6b 65 6e 20 6b 65 79  type;  Token key
2f482 3b 20 7d 3b 0a 0a 0a 20 20 2f 2a 20 54 68 69 73  ; };...  /* This
2f483 20 69 73 20 61 20 75 74 69 6c 69 74 79 20 72 6f   is a utility ro
2f484 75 74 69 6e 65 20 75 73 65 64 20 74 6f 20 73 65  utine used to se
2f485 74 20 74 68 65 20 45 78 70 72 53 70 61 6e 2e 7a  t the ExprSpan.z
2f486 53 74 61 72 74 20 61 6e 64 0a 20 20 2a 2a 20 45  Start and.  ** E
2f487 78 70 72 53 70 61 6e 2e 7a 45 6e 64 20 76 61 6c  xprSpan.zEnd val
2f488 75 65 73 20 6f 66 20 70 4f 75 74 20 73 6f 20 74  ues of pOut so t
2f489 68 61 74 20 74 68 65 20 73 70 61 6e 20 63 6f 76  hat the span cov
2f48a 65 72 73 20 74 68 65 20 63 6f 6d 70 6c 65 74 65  ers the complete
2f48b 0a 20 20 2a 2a 20 72 61 6e 67 65 20 6f 66 20 74  .  ** range of t
2f48c 65 78 74 20 62 65 67 69 6e 6e 69 6e 67 20 77 69  ext beginning wi
2f48d 74 68 20 70 53 74 61 72 74 20 61 6e 64 20 67 6f  th pStart and go
2f48e 69 6e 67 20 74 6f 20 74 68 65 20 65 6e 64 20 6f  ing to the end o
2f48f 66 20 70 45 6e 64 2e 0a 20 20 2a 2f 0a 20 20 73  f pEnd..  */.  s
2f490 74 61 74 69 63 20 76 6f 69 64 20 73 70 61 6e 53  tatic void spanS
2f491 65 74 28 45 78 70 72 53 70 61 6e 20 2a 70 4f 75  et(ExprSpan *pOu
2f492 74 2c 20 54 6f 6b 65 6e 20 2a 70 53 74 61 72 74  t, Token *pStart
2f493 2c 20 54 6f 6b 65 6e 20 2a 70 45 6e 64 29 7b 0a  , Token *pEnd){.
2f494 20 20 20 20 70 4f 75 74 2d 3e 7a 53 74 61 72 74      pOut->zStart
2f495 20 3d 20 70 53 74 61 72 74 2d 3e 7a 3b 0a 20 20   = pStart->z;.  
2f496 20 20 70 4f 75 74 2d 3e 7a 45 6e 64 20 3d 20 26    pOut->zEnd = &
2f497 70 45 6e 64 2d 3e 7a 5b 70 45 6e 64 2d 3e 6e 5d  pEnd->z[pEnd->n]
2f498 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 73  ;.  }..  /* Cons
2f499 74 72 75 63 74 20 61 20 6e 65 77 20 45 78 70 72  truct a new Expr
2f49a 20 6f 62 6a 65 63 74 20 66 72 6f 6d 20 61 20 73   object from a s
2f49b 69 6e 67 6c 65 20 69 64 65 6e 74 69 66 69 65 72  ingle identifier
2f49c 2e 20 20 55 73 65 20 74 68 65 0a 20 20 2a 2a 20  .  Use the.  ** 
2f49d 6e 65 77 20 45 78 70 72 20 74 6f 20 70 6f 70 75  new Expr to popu
2f49e 6c 61 74 65 20 70 4f 75 74 2e 20 20 53 65 74 20  late pOut.  Set 
2f49f 74 68 65 20 73 70 61 6e 20 6f 66 20 70 4f 75 74  the span of pOut
2f4a0 20 74 6f 20 62 65 20 74 68 65 20 69 64 65 6e 74   to be the ident
2f4a1 69 66 69 65 72 0a 20 20 2a 2a 20 74 68 61 74 20  ifier.  ** that 
2f4a2 63 72 65 61 74 65 64 20 74 68 65 20 65 78 70 72  created the expr
2f4a3 65 73 73 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 73  ession..  */.  s
2f4a4 74 61 74 69 63 20 76 6f 69 64 20 73 70 61 6e 45  tatic void spanE
2f4a5 78 70 72 28 45 78 70 72 53 70 61 6e 20 2a 70 4f  xpr(ExprSpan *pO
2f4a6 75 74 2c 20 50 61 72 73 65 20 2a 70 50 61 72 73  ut, Parse *pPars
2f4a7 65 2c 20 69 6e 74 20 6f 70 2c 20 54 6f 6b 65 6e  e, int op, Token
2f4a8 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20 20 20 70   *pValue){.    p
2f4a9 4f 75 74 2d 3e 70 45 78 70 72 20 3d 20 73 71 6c  Out->pExpr = sql
2f4aa 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
2f4ab 2c 20 6f 70 2c 20 30 2c 20 30 2c 20 70 56 61 6c  , op, 0, 0, pVal
2f4ac 75 65 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a  ue);.    pOut->z
2f4ad 53 74 61 72 74 20 3d 20 70 56 61 6c 75 65 2d 3e  Start = pValue->
2f4ae 7a 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 45 6e  z;.    pOut->zEn
2f4af 64 20 3d 20 26 70 56 61 6c 75 65 2d 3e 7a 5b 70  d = &pValue->z[p
2f4b0 56 61 6c 75 65 2d 3e 6e 5d 3b 0a 20 20 7d 0a 0a  Value->n];.  }..
2f4b1 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e    /* This routin
2f4b2 65 20 63 6f 6e 73 74 72 75 63 74 73 20 61 20 62  e constructs a b
2f4b3 69 6e 61 72 79 20 65 78 70 72 65 73 73 69 6f 6e  inary expression
2f4b4 20 6e 6f 64 65 20 6f 75 74 20 6f 66 20 74 77 6f   node out of two
2f4b5 20 45 78 70 72 53 70 61 6e 0a 20 20 2a 2a 20 6f   ExprSpan.  ** o
2f4b6 62 6a 65 63 74 73 20 61 6e 64 20 75 73 65 73 20  bjects and uses 
2f4b7 74 68 65 20 72 65 73 75 6c 74 20 74 6f 20 70 6f  the result to po
2f4b8 70 75 6c 61 74 65 20 61 20 6e 65 77 20 45 78 70  pulate a new Exp
2f4b9 72 53 70 61 6e 20 6f 62 6a 65 63 74 2e 0a 20 20  rSpan object..  
2f4ba 2a 2f 0a 20 20 73 74 61 74 69 63 20 76 6f 69 64  */.  static void
2f4bb 20 73 70 61 6e 42 69 6e 61 72 79 45 78 70 72 28   spanBinaryExpr(
2f4bc 0a 20 20 20 20 45 78 70 72 53 70 61 6e 20 2a 70  .    ExprSpan *p
2f4bd 4f 75 74 2c 20 20 20 20 20 2f 2a 20 57 72 69 74  Out,     /* Writ
2f4be 65 20 74 68 65 20 72 65 73 75 6c 74 20 68 65 72  e the result her
2f4bf 65 20 2a 2f 0a 20 20 20 20 50 61 72 73 65 20 2a  e */.    Parse *
2f4c0 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20  pParse,      /* 
2f4c1 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74  The parsing cont
2f4c2 65 78 74 2e 20 20 45 72 72 6f 72 73 20 61 63 63  ext.  Errors acc
2f4c3 75 6d 75 6c 61 74 65 20 68 65 72 65 20 2a 2f 0a  umulate here */.
2f4c4 20 20 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20      int op,     
2f4c5 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62          /* The b
2f4c6 69 6e 61 72 79 20 6f 70 65 72 61 74 69 6f 6e 20  inary operation 
2f4c7 2a 2f 0a 20 20 20 20 45 78 70 72 53 70 61 6e 20  */.    ExprSpan 
2f4c8 2a 70 4c 65 66 74 2c 20 20 20 20 2f 2a 20 54 68  *pLeft,    /* Th
2f4c9 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 2a  e left operand *
2f4ca 2f 0a 20 20 20 20 45 78 70 72 53 70 61 6e 20 2a  /.    ExprSpan *
2f4cb 70 52 69 67 68 74 20 20 20 20 2f 2a 20 54 68 65  pRight    /* The
2f4cc 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a   right operand *
2f4cd 2f 0a 20 20 29 7b 0a 20 20 20 20 70 4f 75 74 2d  /.  ){.    pOut-
2f4ce 3e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33  >pExpr = sqlite3
2f4cf 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 6f 70  PExpr(pParse, op
2f4d0 2c 20 70 4c 65 66 74 2d 3e 70 45 78 70 72 2c 20  , pLeft->pExpr, 
2f4d1 70 52 69 67 68 74 2d 3e 70 45 78 70 72 2c 20 30  pRight->pExpr, 0
2f4d2 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 53 74  );.    pOut->zSt
2f4d3 61 72 74 20 3d 20 70 4c 65 66 74 2d 3e 7a 53 74  art = pLeft->zSt
2f4d4 61 72 74 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a  art;.    pOut->z
2f4d5 45 6e 64 20 3d 20 70 52 69 67 68 74 2d 3e 7a 45  End = pRight->zE
2f4d6 6e 64 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f  nd;.  }..  /* Co
2f4d7 6e 73 74 72 75 63 74 20 61 6e 20 65 78 70 72 65  nstruct an expre
2f4d8 73 73 69 6f 6e 20 6e 6f 64 65 20 66 6f 72 20 61  ssion node for a
2f4d9 20 75 6e 61 72 79 20 70 6f 73 74 66 69 78 20 6f   unary postfix o
2f4da 70 65 72 61 74 6f 72 0a 20 20 2a 2f 0a 20 20 73  perator.  */.  s
2f4db 74 61 74 69 63 20 76 6f 69 64 20 73 70 61 6e 55  tatic void spanU
2f4dc 6e 61 72 79 50 6f 73 74 66 69 78 28 0a 20 20 20  naryPostfix(.   
2f4dd 20 45 78 70 72 53 70 61 6e 20 2a 70 4f 75 74 2c   ExprSpan *pOut,
2f4de 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65          /* Write
2f4df 20 74 68 65 20 6e 65 77 20 65 78 70 72 65 73 73   the new express
2f4e0 69 6f 6e 20 6e 6f 64 65 20 68 65 72 65 20 2a 2f  ion node here */
2f4e1 0a 20 20 20 20 50 61 72 73 65 20 2a 70 50 61 72  .    Parse *pPar
2f4e2 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50  se,         /* P
2f4e3 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 74  arsing context t
2f4e4 6f 20 72 65 63 6f 72 64 20 65 72 72 6f 72 73 20  o record errors 
2f4e5 2a 2f 0a 20 20 20 20 69 6e 74 20 6f 70 2c 20 20  */.    int op,  
2f4e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2f4e7 20 54 68 65 20 6f 70 65 72 61 74 6f 72 20 2a 2f   The operator */
2f4e8 0a 20 20 20 20 45 78 70 72 53 70 61 6e 20 2a 70  .    ExprSpan *p
2f4e9 4f 70 65 72 61 6e 64 2c 20 20 20 20 2f 2a 20 54  Operand,    /* T
2f4ea 68 65 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20  he operand */.  
2f4eb 20 20 54 6f 6b 65 6e 20 2a 70 50 6f 73 74 4f 70    Token *pPostOp
2f4ec 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2f4ed 6f 70 65 72 61 6e 64 20 74 6f 6b 65 6e 20 66 6f  operand token fo
2f4ee 72 20 73 65 74 74 69 6e 67 20 74 68 65 20 73 70  r setting the sp
2f4ef 61 6e 20 2a 2f 0a 20 20 29 7b 0a 20 20 20 20 70  an */.  ){.    p
2f4f0 4f 75 74 2d 3e 70 45 78 70 72 20 3d 20 73 71 6c  Out->pExpr = sql
2f4f1 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
2f4f2 2c 20 6f 70 2c 20 70 4f 70 65 72 61 6e 64 2d 3e  , op, pOperand->
2f4f3 70 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20  pExpr, 0, 0);.  
2f4f4 20 20 70 4f 75 74 2d 3e 7a 53 74 61 72 74 20 3d    pOut->zStart =
2f4f5 20 70 4f 70 65 72 61 6e 64 2d 3e 7a 53 74 61 72   pOperand->zStar
2f4f6 74 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 45 6e  t;.    pOut->zEn
2f4f7 64 20 3d 20 26 70 50 6f 73 74 4f 70 2d 3e 7a 5b  d = &pPostOp->z[
2f4f8 70 50 6f 73 74 4f 70 2d 3e 6e 5d 3b 0a 20 20 7d  pPostOp->n];.  }
2f4f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f4fa 20 20 20 20 20 20 20 20 20 20 20 0a 0a 20 20 2f             ..  /
2f4fb 2a 20 43 6f 6e 73 74 72 75 63 74 20 61 6e 20 65  * Construct an e
2f4fc 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20 66  xpression node f
2f4fd 6f 72 20 61 20 75 6e 61 72 79 20 70 72 65 66 69  or a unary prefi
2f4fe 78 20 6f 70 65 72 61 74 6f 72 0a 20 20 2a 2f 0a  x operator.  */.
2f4ff 20 20 73 74 61 74 69 63 20 76 6f 69 64 20 73 70    static void sp
2f500 61 6e 55 6e 61 72 79 50 72 65 66 69 78 28 0a 20  anUnaryPrefix(. 
2f501 20 20 20 45 78 70 72 53 70 61 6e 20 2a 70 4f 75     ExprSpan *pOu
2f502 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69  t,        /* Wri
2f503 74 65 20 74 68 65 20 6e 65 77 20 65 78 70 72 65  te the new expre
2f504 73 73 69 6f 6e 20 6e 6f 64 65 20 68 65 72 65 20  ssion node here 
2f505 2a 2f 0a 20 20 20 20 50 61 72 73 65 20 2a 70 50  */.    Parse *pP
2f506 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a  arse,         /*
2f507 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
2f508 20 74 6f 20 72 65 63 6f 72 64 20 65 72 72 6f 72   to record error
2f509 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6f 70 2c  s */.    int op,
2f50a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f50b 2f 2a 20 54 68 65 20 6f 70 65 72 61 74 6f 72 20  /* The operator 
2f50c 2a 2f 0a 20 20 20 20 45 78 70 72 53 70 61 6e 20  */.    ExprSpan 
2f50d 2a 70 4f 70 65 72 61 6e 64 2c 20 20 20 20 2f 2a  *pOperand,    /*
2f50e 20 54 68 65 20 6f 70 65 72 61 6e 64 20 2a 2f 0a   The operand */.
2f50f 20 20 20 20 54 6f 6b 65 6e 20 2a 70 50 72 65 4f      Token *pPreO
2f510 70 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  p         /* The
2f511 20 6f 70 65 72 61 6e 64 20 74 6f 6b 65 6e 20 66   operand token f
2f512 6f 72 20 73 65 74 74 69 6e 67 20 74 68 65 20 73  or setting the s
2f513 70 61 6e 20 2a 2f 0a 20 20 29 7b 0a 20 20 20 20  pan */.  ){.    
2f514 70 4f 75 74 2d 3e 70 45 78 70 72 20 3d 20 73 71  pOut->pExpr = sq
2f515 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2f516 65 2c 20 6f 70 2c 20 70 4f 70 65 72 61 6e 64 2d  e, op, pOperand-
2f517 3e 70 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20  >pExpr, 0, 0);. 
2f518 20 20 20 70 4f 75 74 2d 3e 7a 53 74 61 72 74 20     pOut->zStart 
2f519 3d 20 70 50 72 65 4f 70 2d 3e 7a 3b 0a 20 20 20  = pPreOp->z;.   
2f51a 20 70 4f 75 74 2d 3e 7a 45 6e 64 20 3d 20 70 4f   pOut->zEnd = pO
2f51b 70 65 72 61 6e 64 2d 3e 7a 45 6e 64 3b 0a 20 20  perand->zEnd;.  
2f51c 7d 0a 2f 2a 20 4e 65 78 74 20 69 73 20 61 6c 6c  }./* Next is all
2f51d 20 74 6f 6b 65 6e 20 76 61 6c 75 65 73 2c 20 69   token values, i
2f51e 6e 20 61 20 66 6f 72 6d 20 73 75 69 74 61 62 6c  n a form suitabl
2f51f 65 20 66 6f 72 20 75 73 65 20 62 79 20 6d 61 6b  e for use by mak
2f520 65 68 65 61 64 65 72 73 2e 0a 2a 2a 20 54 68 69  eheaders..** Thi
2f521 73 20 73 65 63 74 69 6f 6e 20 77 69 6c 6c 20 62  s section will b
2f522 65 20 6e 75 6c 6c 20 75 6e 6c 65 73 73 20 6c 65  e null unless le
2f523 6d 6f 6e 20 69 73 20 72 75 6e 20 77 69 74 68 20  mon is run with 
2f524 74 68 65 20 2d 6d 20 73 77 69 74 63 68 2e 0a 2a  the -m switch..*
2f525 2f 0a 2f 2a 20 0a 2a 2a 20 54 68 65 73 65 20 63  /./* .** These c
2f526 6f 6e 73 74 61 6e 74 73 20 28 61 6c 6c 20 67 65  onstants (all ge
2f527 6e 65 72 61 74 65 64 20 61 75 74 6f 6d 61 74 69  nerated automati
2f528 63 61 6c 6c 79 20 62 79 20 74 68 65 20 70 61 72  cally by the par
2f529 73 65 72 20 67 65 6e 65 72 61 74 6f 72 29 0a 2a  ser generator).*
2f52a 2a 20 73 70 65 63 69 66 79 20 74 68 65 20 76 61  * specify the va
2f52b 72 69 6f 75 73 20 6b 69 6e 64 73 20 6f 66 20 74  rious kinds of t
2f52c 6f 6b 65 6e 73 20 28 74 65 72 6d 69 6e 61 6c 73  okens (terminals
2f52d 29 20 74 68 61 74 20 74 68 65 20 70 61 72 73 65  ) that the parse
2f52e 72 0a 2a 2a 20 75 6e 64 65 72 73 74 61 6e 64 73  r.** understands
2f52f 2e 20 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 73 79  . .**.** Each sy
2f530 6d 62 6f 6c 20 68 65 72 65 20 69 73 20 61 20 74  mbol here is a t
2f531 65 72 6d 69 6e 61 6c 20 73 79 6d 62 6f 6c 20 69  erminal symbol i
2f532 6e 20 74 68 65 20 67 72 61 6d 6d 61 72 2e 0a 2a  n the grammar..*
2f533 2f 0a 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74  /./* Make sure t
2f534 68 65 20 49 4e 54 45 52 46 41 43 45 20 6d 61 63  he INTERFACE mac
2f535 72 6f 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a  ro is defined..*
2f536 2f 0a 23 69 66 6e 64 65 66 20 49 4e 54 45 52 46  /.#ifndef INTERF
2f537 41 43 45 0a 23 20 64 65 66 69 6e 65 20 49 4e 54  ACE.# define INT
2f538 45 52 46 41 43 45 20 31 0a 23 65 6e 64 69 66 0a  ERFACE 1.#endif.
2f539 2f 2a 20 54 68 65 20 6e 65 78 74 20 74 68 69 6e  /* The next thin
2f53a 67 20 69 6e 63 6c 75 64 65 64 20 69 73 20 73 65  g included is se
2f53b 72 69 65 73 20 6f 66 20 64 65 66 69 6e 65 73 20  ries of defines 
2f53c 77 68 69 63 68 20 63 6f 6e 74 72 6f 6c 0a 2a 2a  which control.**
2f53d 20 76 61 72 69 6f 75 73 20 61 73 70 65 63 74 73   various aspects
2f53e 20 6f 66 20 74 68 65 20 67 65 6e 65 72 61 74 65   of the generate
2f53f 64 20 70 61 72 73 65 72 2e 0a 2a 2a 20 20 20 20  d parser..**    
2f540 59 59 43 4f 44 45 54 59 50 45 20 20 20 20 20 20  YYCODETYPE      
2f541 20 20 20 69 73 20 74 68 65 20 64 61 74 61 20 74     is the data t
2f542 79 70 65 20 75 73 65 64 20 66 6f 72 20 73 74 6f  ype used for sto
2f543 72 69 6e 67 20 74 65 72 6d 69 6e 61 6c 0a 2a 2a  ring terminal.**
2f544 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f545 20 20 20 20 20 20 20 61 6e 64 20 6e 6f 6e 74 65         and nonte
2f546 72 6d 69 6e 61 6c 20 6e 75 6d 62 65 72 73 2e 20  rminal numbers. 
2f547 20 22 75 6e 73 69 67 6e 65 64 20 63 68 61 72 22   "unsigned char"
2f548 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   is.**          
2f549 20 20 20 20 20 20 20 20 20 20 20 20 20 75 73 65               use
2f54a 64 20 69 66 20 74 68 65 72 65 20 61 72 65 20 66  d if there are f
2f54b 65 77 65 72 20 74 68 61 6e 20 32 35 30 20 74 65  ewer than 250 te
2f54c 72 6d 69 6e 61 6c 73 0a 2a 2a 20 20 20 20 20 20  rminals.**      
2f54d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f54e 20 61 6e 64 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c   and nonterminal
2f54f 73 2e 20 20 22 69 6e 74 22 20 69 73 20 75 73 65  s.  "int" is use
2f550 64 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 20  d otherwise..** 
2f551 20 20 20 59 59 4e 4f 43 4f 44 45 20 20 20 20 20     YYNOCODE     
2f552 20 20 20 20 20 20 69 73 20 61 20 6e 75 6d 62 65        is a numbe
2f553 72 20 6f 66 20 74 79 70 65 20 59 59 43 4f 44 45  r of type YYCODE
2f554 54 59 50 45 20 77 68 69 63 68 20 63 6f 72 72 65  TYPE which corre
2f555 73 70 6f 6e 64 73 0a 2a 2a 20 20 20 20 20 20 20  sponds.**       
2f556 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f557 74 6f 20 6e 6f 20 6c 65 67 61 6c 20 74 65 72 6d  to no legal term
2f558 69 6e 61 6c 20 6f 72 20 6e 6f 6e 74 65 72 6d 69  inal or nontermi
2f559 6e 61 6c 20 6e 75 6d 62 65 72 2e 20 20 54 68 69  nal number.  Thi
2f55a 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  s.**            
2f55b 20 20 20 20 20 20 20 20 20 20 20 6e 75 6d 62 65             numbe
2f55c 72 20 69 73 20 75 73 65 64 20 74 6f 20 66 69 6c  r is used to fil
2f55d 6c 20 69 6e 20 65 6d 70 74 79 20 73 6c 6f 74 73  l in empty slots
2f55e 20 6f 66 20 74 68 65 20 68 61 73 68 20 0a 2a 2a   of the hash .**
2f55f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f560 20 20 20 20 20 20 20 74 61 62 6c 65 2e 0a 2a 2a         table..**
2f561 20 20 20 20 59 59 46 41 4c 4c 42 41 43 4b 20 20      YYFALLBACK  
2f562 20 20 20 20 20 20 20 49 66 20 64 65 66 69 6e 65         If define
2f563 64 2c 20 74 68 69 73 20 69 6e 64 69 63 61 74 65  d, this indicate
2f564 73 20 74 68 61 74 20 6f 6e 65 20 6f 72 20 6d 6f  s that one or mo
2f565 72 65 20 74 6f 6b 65 6e 73 0a 2a 2a 20 20 20 20  re tokens.**    
2f566 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f567 20 20 20 68 61 76 65 20 66 61 6c 6c 2d 62 61 63     have fall-bac
2f568 6b 20 76 61 6c 75 65 73 20 77 68 69 63 68 20 73  k values which s
2f569 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 69 66  hould be used if
2f56a 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
2f56b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 72                or
2f56c 69 67 69 6e 61 6c 20 76 61 6c 75 65 20 6f 66 20  iginal value of 
2f56d 74 68 65 20 74 6f 6b 65 6e 20 77 69 6c 6c 20 6e  the token will n
2f56e 6f 74 20 70 61 72 73 65 2e 0a 2a 2a 20 20 20 20  ot parse..**    
2f56f 59 59 41 43 54 49 4f 4e 54 59 50 45 20 20 20 20  YYACTIONTYPE    
2f570 20 20 20 69 73 20 74 68 65 20 64 61 74 61 20 74     is the data t
2f571 79 70 65 20 75 73 65 64 20 66 6f 72 20 73 74 6f  ype used for sto
2f572 72 69 6e 67 20 74 65 72 6d 69 6e 61 6c 0a 2a 2a  ring terminal.**
2f573 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f574 20 20 20 20 20 20 20 61 6e 64 20 6e 6f 6e 74 65         and nonte
2f575 72 6d 69 6e 61 6c 20 6e 75 6d 62 65 72 73 2e 20  rminal numbers. 
2f576 20 22 75 6e 73 69 67 6e 65 64 20 63 68 61 72 22   "unsigned char"
2f577 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   is.**          
2f578 20 20 20 20 20 20 20 20 20 20 20 20 20 75 73 65               use
2f579 64 20 69 66 20 74 68 65 72 65 20 61 72 65 20 66  d if there are f
2f57a 65 77 65 72 20 74 68 61 6e 20 32 35 30 20 72 75  ewer than 250 ru
2f57b 6c 65 73 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20  les and.**      
2f57c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f57d 20 73 74 61 74 65 73 20 63 6f 6d 62 69 6e 65 64   states combined
2f57e 2e 20 20 22 69 6e 74 22 20 69 73 20 75 73 65 64  .  "int" is used
2f57f 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 20 20   otherwise..**  
2f580 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 54    sqlite3ParserT
2f581 4f 4b 45 4e 54 59 50 45 20 20 20 20 20 69 73 20  OKENTYPE     is 
2f582 74 68 65 20 64 61 74 61 20 74 79 70 65 20 75 73  the data type us
2f583 65 64 20 66 6f 72 20 6d 69 6e 6f 72 20 74 6f 6b  ed for minor tok
2f584 65 6e 73 20 67 69 76 65 6e 20 0a 2a 2a 20 20 20  ens given .**   
2f585 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f586 20 20 20 20 64 69 72 65 63 74 6c 79 20 74 6f 20      directly to 
2f587 74 68 65 20 70 61 72 73 65 72 20 66 72 6f 6d 20  the parser from 
2f588 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 2e 0a 2a  the tokenizer..*
2f589 2a 20 20 20 20 59 59 4d 49 4e 4f 52 54 59 50 45  *    YYMINORTYPE
2f58a 20 20 20 20 20 20 20 20 69 73 20 74 68 65 20 64          is the d
2f58b 61 74 61 20 74 79 70 65 20 75 73 65 64 20 66 6f  ata type used fo
2f58c 72 20 61 6c 6c 20 6d 69 6e 6f 72 20 74 6f 6b 65  r all minor toke
2f58d 6e 73 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ns..**          
2f58e 20 20 20 20 20 20 20 20 20 20 20 20 20 54 68 69               Thi
2f58f 73 20 69 73 20 74 79 70 69 63 61 6c 6c 79 20 61  s is typically a
2f590 20 75 6e 69 6f 6e 20 6f 66 20 6d 61 6e 79 20 74   union of many t
2f591 79 70 65 73 2c 20 6f 6e 65 20 6f 66 0a 2a 2a 20  ypes, one of.** 
2f592 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f593 20 20 20 20 20 20 77 68 69 63 68 20 69 73 20 73        which is s
2f594 71 6c 69 74 65 33 50 61 72 73 65 72 54 4f 4b 45  qlite3ParserTOKE
2f595 4e 54 59 50 45 2e 20 20 54 68 65 20 65 6e 74 72  NTYPE.  The entr
2f596 79 20 69 6e 20 74 68 65 20 75 6e 69 6f 6e 0a 2a  y in the union.*
2f597 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
2f598 20 20 20 20 20 20 20 20 66 6f 72 20 62 61 73 65          for base
2f599 20 74 6f 6b 65 6e 73 20 69 73 20 63 61 6c 6c 65   tokens is calle
2f59a 64 20 22 79 79 30 22 2e 0a 2a 2a 20 20 20 20 59  d "yy0"..**    Y
2f59b 59 53 54 41 43 4b 44 45 50 54 48 20 20 20 20 20  YSTACKDEPTH     
2f59c 20 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d    is the maximum
2f59d 20 64 65 70 74 68 20 6f 66 20 74 68 65 20 70 61   depth of the pa
2f59e 72 73 65 72 27 73 20 73 74 61 63 6b 2e 20 20 49  rser's stack.  I
2f59f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  f.**            
2f5a0 20 20 20 20 20 20 20 20 20 20 20 7a 65 72 6f 20             zero 
2f5a1 74 68 65 20 73 74 61 63 6b 20 69 73 20 64 79 6e  the stack is dyn
2f5a2 61 6d 69 63 61 6c 6c 79 20 73 69 7a 65 64 20 75  amically sized u
2f5a3 73 69 6e 67 20 72 65 61 6c 6c 6f 63 28 29 0a 2a  sing realloc().*
2f5a4 2a 20 20 20 20 73 71 6c 69 74 65 33 50 61 72 73  *    sqlite3Pars
2f5a5 65 72 41 52 47 5f 53 44 45 43 4c 20 20 20 20 20  erARG_SDECL     
2f5a6 41 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c  A static variabl
2f5a7 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 66 6f  e declaration fo
2f5a8 72 20 74 68 65 20 25 65 78 74 72 61 5f 61 72 67  r the %extra_arg
2f5a9 75 6d 65 6e 74 0a 2a 2a 20 20 20 20 73 71 6c 69  ument.**    sqli
2f5aa 74 65 33 50 61 72 73 65 72 41 52 47 5f 50 44 45  te3ParserARG_PDE
2f5ab 43 4c 20 20 20 20 20 41 20 70 61 72 61 6d 65 74  CL     A paramet
2f5ac 65 72 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 66  er declaration f
2f5ad 6f 72 20 74 68 65 20 25 65 78 74 72 61 5f 61 72  or the %extra_ar
2f5ae 67 75 6d 65 6e 74 0a 2a 2a 20 20 20 20 73 71 6c  gument.**    sql
2f5af 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 53 54  ite3ParserARG_ST
2f5b0 4f 52 45 20 20 20 20 20 43 6f 64 65 20 74 6f 20  ORE     Code to 
2f5b1 73 74 6f 72 65 20 25 65 78 74 72 61 5f 61 72 67  store %extra_arg
2f5b2 75 6d 65 6e 74 20 69 6e 74 6f 20 79 79 70 50 61  ument into yypPa
2f5b3 72 73 65 72 0a 2a 2a 20 20 20 20 73 71 6c 69 74  rser.**    sqlit
2f5b4 65 33 50 61 72 73 65 72 41 52 47 5f 46 45 54 43  e3ParserARG_FETC
2f5b5 48 20 20 20 20 20 43 6f 64 65 20 74 6f 20 65 78  H     Code to ex
2f5b6 74 72 61 63 74 20 25 65 78 74 72 61 5f 61 72 67  tract %extra_arg
2f5b7 75 6d 65 6e 74 20 66 72 6f 6d 20 79 79 70 50 61  ument from yypPa
2f5b8 72 73 65 72 0a 2a 2a 20 20 20 20 59 59 4e 53 54  rser.**    YYNST
2f5b9 41 54 45 20 20 20 20 20 20 20 20 20 20 20 74 68  ATE           th
2f5ba 65 20 63 6f 6d 62 69 6e 65 64 20 6e 75 6d 62 65  e combined numbe
2f5bb 72 20 6f 66 20 73 74 61 74 65 73 2e 0a 2a 2a 20  r of states..** 
2f5bc 20 20 20 59 59 4e 52 55 4c 45 20 20 20 20 20 20     YYNRULE      
2f5bd 20 20 20 20 20 20 74 68 65 20 6e 75 6d 62 65 72        the number
2f5be 20 6f 66 20 72 75 6c 65 73 20 69 6e 20 74 68 65   of rules in the
2f5bf 20 67 72 61 6d 6d 61 72 0a 2a 2a 20 20 20 20 59   grammar.**    Y
2f5c0 59 45 52 52 4f 52 53 59 4d 42 4f 4c 20 20 20 20  YERRORSYMBOL    
2f5c1 20 20 69 73 20 74 68 65 20 63 6f 64 65 20 6e 75    is the code nu
2f5c2 6d 62 65 72 20 6f 66 20 74 68 65 20 65 72 72 6f  mber of the erro
2f5c3 72 20 73 79 6d 62 6f 6c 2e 20 20 49 66 20 6e 6f  r symbol.  If no
2f5c4 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
2f5c5 20 20 20 20 20 20 20 20 20 20 20 64 65 66 69 6e             defin
2f5c6 65 64 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 20 65  ed, then do no e
2f5c7 72 72 6f 72 20 70 72 6f 63 65 73 73 69 6e 67 2e  rror processing.
2f5c8 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 59 59 43 4f  .*/.#define YYCO
2f5c9 44 45 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20  DETYPE unsigned 
2f5ca 63 68 61 72 0a 23 64 65 66 69 6e 65 20 59 59 4e  char.#define YYN
2f5cb 4f 43 4f 44 45 20 32 35 34 0a 23 64 65 66 69 6e  OCODE 254.#defin
2f5cc 65 20 59 59 41 43 54 49 4f 4e 54 59 50 45 20 75  e YYACTIONTYPE u
2f5cd 6e 73 69 67 6e 65 64 20 73 68 6f 72 74 20 69 6e  nsigned short in
2f5ce 74 0a 23 64 65 66 69 6e 65 20 59 59 57 49 4c 44  t.#define YYWILD
2f5cf 43 41 52 44 20 36 35 0a 23 64 65 66 69 6e 65 20  CARD 65.#define 
2f5d0 73 71 6c 69 74 65 33 50 61 72 73 65 72 54 4f 4b  sqlite3ParserTOK
2f5d1 45 4e 54 59 50 45 20 54 6f 6b 65 6e 0a 74 79 70  ENTYPE Token.typ
2f5d2 65 64 65 66 20 75 6e 69 6f 6e 20 7b 0a 20 20 69  edef union {.  i
2f5d3 6e 74 20 79 79 69 6e 69 74 3b 0a 20 20 73 71 6c  nt yyinit;.  sql
2f5d4 69 74 65 33 50 61 72 73 65 72 54 4f 4b 45 4e 54  ite3ParserTOKENT
2f5d5 59 50 45 20 79 79 30 3b 0a 20 20 53 65 6c 65 63  YPE yy0;.  Selec
2f5d6 74 2a 20 79 79 33 3b 0a 20 20 45 78 70 72 4c 69  t* yy3;.  ExprLi
2f5d7 73 74 2a 20 79 79 31 34 3b 0a 20 20 53 72 63 4c  st* yy14;.  SrcL
2f5d8 69 73 74 2a 20 79 79 36 35 3b 0a 20 20 73 74 72  ist* yy65;.  str
2f5d9 75 63 74 20 4c 69 6b 65 4f 70 20 79 79 39 36 3b  uct LikeOp yy96;
2f5da 0a 20 20 45 78 70 72 2a 20 79 79 31 33 32 3b 0a  .  Expr* yy132;.
2f5db 20 20 75 38 20 79 79 31 38 36 3b 0a 20 20 69 6e    u8 yy186;.  in
2f5dc 74 20 79 79 33 32 38 3b 0a 20 20 45 78 70 72 53  t yy328;.  ExprS
2f5dd 70 61 6e 20 79 79 33 34 36 3b 0a 20 20 73 74 72  pan yy346;.  str
2f5de 75 63 74 20 54 72 69 67 45 76 65 6e 74 20 79 79  uct TrigEvent yy
2f5df 33 37 38 3b 0a 20 20 49 64 4c 69 73 74 2a 20 79  378;.  IdList* y
2f5e0 79 34 30 38 3b 0a 20 20 73 74 72 75 63 74 20 7b  y408;.  struct {
2f5e1 69 6e 74 20 76 61 6c 75 65 3b 20 69 6e 74 20 6d  int value; int m
2f5e2 61 73 6b 3b 7d 20 79 79 34 32 39 3b 0a 20 20 54  ask;} yy429;.  T
2f5e3 72 69 67 67 65 72 53 74 65 70 2a 20 79 79 34 37  riggerStep* yy47
2f5e4 33 3b 0a 20 20 73 74 72 75 63 74 20 4c 69 6d 69  3;.  struct Limi
2f5e5 74 56 61 6c 20 79 79 34 37 36 3b 0a 7d 20 59 59  tVal yy476;.} YY
2f5e6 4d 49 4e 4f 52 54 59 50 45 3b 0a 23 69 66 6e 64  MINORTYPE;.#ifnd
2f5e7 65 66 20 59 59 53 54 41 43 4b 44 45 50 54 48 0a  ef YYSTACKDEPTH.
2f5e8 23 64 65 66 69 6e 65 20 59 59 53 54 41 43 4b 44  #define YYSTACKD
2f5e9 45 50 54 48 20 31 30 30 0a 23 65 6e 64 69 66 0a  EPTH 100.#endif.
2f5ea 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 50  #define sqlite3P
2f5eb 61 72 73 65 72 41 52 47 5f 53 44 45 43 4c 20 50  arserARG_SDECL P
2f5ec 61 72 73 65 20 2a 70 50 61 72 73 65 3b 0a 23 64  arse *pParse;.#d
2f5ed 65 66 69 6e 65 20 73 71 6c 69 74 65 33 50 61 72  efine sqlite3Par
2f5ee 73 65 72 41 52 47 5f 50 44 45 43 4c 20 2c 50 61  serARG_PDECL ,Pa
2f5ef 72 73 65 20 2a 70 50 61 72 73 65 0a 23 64 65 66  rse *pParse.#def
2f5f0 69 6e 65 20 73 71 6c 69 74 65 33 50 61 72 73 65  ine sqlite3Parse
2f5f1 72 41 52 47 5f 46 45 54 43 48 20 50 61 72 73 65  rARG_FETCH Parse
2f5f2 20 2a 70 50 61 72 73 65 20 3d 20 79 79 70 50 61   *pParse = yypPa
2f5f3 72 73 65 72 2d 3e 70 50 61 72 73 65 0a 23 64 65  rser->pParse.#de
2f5f4 66 69 6e 65 20 73 71 6c 69 74 65 33 50 61 72 73  fine sqlite3Pars
2f5f5 65 72 41 52 47 5f 53 54 4f 52 45 20 79 79 70 50  erARG_STORE yypP
2f5f6 61 72 73 65 72 2d 3e 70 50 61 72 73 65 20 3d 20  arser->pParse = 
2f5f7 70 50 61 72 73 65 0a 23 64 65 66 69 6e 65 20 59  pParse.#define Y
2f5f8 59 4e 53 54 41 54 45 20 36 32 39 0a 23 64 65 66  YNSTATE 629.#def
2f5f9 69 6e 65 20 59 59 4e 52 55 4c 45 20 33 32 39 0a  ine YYNRULE 329.
2f5fa 23 64 65 66 69 6e 65 20 59 59 46 41 4c 4c 42 41  #define YYFALLBA
2f5fb 43 4b 20 31 0a 23 64 65 66 69 6e 65 20 59 59 5f  CK 1.#define YY_
2f5fc 4e 4f 5f 41 43 54 49 4f 4e 20 20 20 20 20 20 28  NO_ACTION      (
2f5fd 59 59 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c 45  YYNSTATE+YYNRULE
2f5fe 2b 32 29 0a 23 64 65 66 69 6e 65 20 59 59 5f 41  +2).#define YY_A
2f5ff 43 43 45 50 54 5f 41 43 54 49 4f 4e 20 20 28 59  CCEPT_ACTION  (Y
2f600 59 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c 45 2b  YNSTATE+YYNRULE+
2f601 31 29 0a 23 64 65 66 69 6e 65 20 59 59 5f 45 52  1).#define YY_ER
2f602 52 4f 52 5f 41 43 54 49 4f 4e 20 20 20 28 59 59  ROR_ACTION   (YY
2f603 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c 45 29 0a  NSTATE+YYNRULE).
2f604 0a 2f 2a 20 54 68 65 20 79 79 7a 65 72 6f 6d 69  ./* The yyzeromi
2f605 6e 6f 72 20 63 6f 6e 73 74 61 6e 74 20 69 73 20  nor constant is 
2f606 75 73 65 64 20 74 6f 20 69 6e 69 74 69 61 6c 69  used to initiali
2f607 7a 65 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 0a  ze instances of.
2f608 2a 2a 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 6f  ** YYMINORTYPE o
2f609 62 6a 65 63 74 73 20 74 6f 20 7a 65 72 6f 2e 20  bjects to zero. 
2f60a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
2f60b 59 59 4d 49 4e 4f 52 54 59 50 45 20 79 79 7a 65  YYMINORTYPE yyze
2f60c 72 6f 6d 69 6e 6f 72 20 3d 20 7b 20 30 20 7d 3b  rominor = { 0 };
2f60d 0a 0a 2f 2a 20 44 65 66 69 6e 65 20 74 68 65 20  ../* Define the 
2f60e 79 79 74 65 73 74 63 61 73 65 28 29 20 6d 61 63  yytestcase() mac
2f60f 72 6f 20 74 6f 20 62 65 20 61 20 6e 6f 2d 6f 70  ro to be a no-op
2f610 20 69 66 20 69 73 20 6e 6f 74 20 61 6c 72 65 61   if is not alrea
2f611 64 79 20 64 65 66 69 6e 65 64 0a 2a 2a 20 6f 74  dy defined.** ot
2f612 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 41  herwise..**.** A
2f613 70 70 6c 69 63 61 74 69 6f 6e 73 20 63 61 6e 20  pplications can 
2f614 63 68 6f 6f 73 65 20 74 6f 20 64 65 66 69 6e 65  choose to define
2f615 20 79 79 74 65 73 74 63 61 73 65 28 29 20 69 6e   yytestcase() in
2f616 20 74 68 65 20 25 69 6e 63 6c 75 64 65 20 73 65   the %include se
2f617 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 61 20 6d 61  ction.** to a ma
2f618 63 72 6f 20 74 68 61 74 20 63 61 6e 20 61 73 73  cro that can ass
2f619 69 73 74 20 69 6e 20 76 65 72 69 66 79 69 6e 67  ist in verifying
2f61a 20 63 6f 64 65 20 63 6f 76 65 72 61 67 65 2e 20   code coverage. 
2f61b 20 46 6f 72 20 70 72 6f 64 75 63 74 69 6f 6e 0a   For production.
2f61c 2a 2a 20 63 6f 64 65 20 74 68 65 20 79 79 74 65  ** code the yyte
2f61d 73 74 63 61 73 65 28 29 20 6d 61 63 72 6f 20 73  stcase() macro s
2f61e 68 6f 75 6c 64 20 62 65 20 74 75 72 6e 65 64 20  hould be turned 
2f61f 6f 66 66 2e 20 20 42 75 74 20 69 74 20 69 73 20  off.  But it is 
2f620 75 73 65 66 75 6c 0a 2a 2a 20 66 6f 72 20 74 65  useful.** for te
2f621 73 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 6e 64 65  sting..*/.#ifnde
2f622 66 20 79 79 74 65 73 74 63 61 73 65 0a 23 20 64  f yytestcase.# d
2f623 65 66 69 6e 65 20 79 79 74 65 73 74 63 61 73 65  efine yytestcase
2f624 28 58 29 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 20  (X).#endif.../* 
2f625 4e 65 78 74 20 61 72 65 20 74 68 65 20 74 61 62  Next are the tab
2f626 6c 65 73 20 75 73 65 64 20 74 6f 20 64 65 74 65  les used to dete
2f627 72 6d 69 6e 65 20 77 68 61 74 20 61 63 74 69 6f  rmine what actio
2f628 6e 20 74 6f 20 74 61 6b 65 20 62 61 73 65 64 20  n to take based 
2f629 6f 6e 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e  on the.** curren
2f62a 74 20 73 74 61 74 65 20 61 6e 64 20 6c 6f 6f 6b  t state and look
2f62b 61 68 65 61 64 20 74 6f 6b 65 6e 2e 20 20 54 68  ahead token.  Th
2f62c 65 73 65 20 74 61 62 6c 65 73 20 61 72 65 20 75  ese tables are u
2f62d 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  sed to implement
2f62e 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 74 68  .** functions th
2f62f 61 74 20 74 61 6b 65 20 61 20 73 74 61 74 65 20  at take a state 
2f630 6e 75 6d 62 65 72 20 61 6e 64 20 6c 6f 6f 6b 61  number and looka
2f631 68 65 61 64 20 76 61 6c 75 65 20 61 6e 64 20 72  head value and r
2f632 65 74 75 72 6e 20 61 6e 0a 2a 2a 20 61 63 74 69  eturn an.** acti
2f633 6f 6e 20 69 6e 74 65 67 65 72 2e 20 20 0a 2a 2a  on integer.  .**
2f634 0a 2a 2a 20 53 75 70 70 6f 73 65 20 74 68 65 20  .** Suppose the 
2f635 61 63 74 69 6f 6e 20 69 6e 74 65 67 65 72 20 69  action integer i
2f636 73 20 4e 2e 20 20 54 68 65 6e 20 74 68 65 20 61  s N.  Then the a
2f637 63 74 69 6f 6e 20 69 73 20 64 65 74 65 72 6d 69  ction is determi
2f638 6e 65 64 20 61 73 0a 2a 2a 20 66 6f 6c 6c 6f 77  ned as.** follow
2f639 73 0a 2a 2a 0a 2a 2a 20 20 20 30 20 3c 3d 20 4e  s.**.**   0 <= N
2f63a 20 3c 20 59 59 4e 53 54 41 54 45 20 20 20 20 20   < YYNSTATE     
2f63b 20 20 20 20 20 20 20 20 20 20 20 20 20 53 68 69               Shi
2f63c 66 74 20 4e 2e 20 20 54 68 61 74 20 69 73 2c 20  ft N.  That is, 
2f63d 70 75 73 68 20 74 68 65 20 6c 6f 6f 6b 61 68 65  push the lookahe
2f63e 61 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ad.**           
2f63f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f640 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e             token
2f641 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 20   onto the stack 
2f642 61 6e 64 20 67 6f 74 6f 20 73 74 61 74 65 20 4e  and goto state N
2f643 2e 0a 2a 2a 0a 2a 2a 20 20 20 59 59 4e 53 54 41  ..**.**   YYNSTA
2f644 54 45 20 3c 3d 20 4e 20 3c 20 59 59 4e 53 54 41  TE <= N < YYNSTA
2f645 54 45 2b 59 59 4e 52 55 4c 45 20 20 20 52 65 64  TE+YYNRULE   Red
2f646 75 63 65 20 62 79 20 72 75 6c 65 20 4e 2d 59 59  uce by rule N-YY
2f647 4e 53 54 41 54 45 2e 0a 2a 2a 0a 2a 2a 20 20 20  NSTATE..**.**   
2f648 4e 20 3d 3d 20 59 59 4e 53 54 41 54 45 2b 59 59  N == YYNSTATE+YY
2f649 4e 52 55 4c 45 20 20 20 20 20 20 20 20 20 20 20  NRULE           
2f64a 20 20 20 41 20 73 79 6e 74 61 78 20 65 72 72 6f     A syntax erro
2f64b 72 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 0a  r has occurred..
2f64c 2a 2a 0a 2a 2a 20 20 20 4e 20 3d 3d 20 59 59 4e  **.**   N == YYN
2f64d 53 54 41 54 45 2b 59 59 4e 52 55 4c 45 2b 31 20  STATE+YYNRULE+1 
2f64e 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20 70             The p
2f64f 61 72 73 65 72 20 61 63 63 65 70 74 73 20 69 74  arser accepts it
2f650 73 20 69 6e 70 75 74 2e 0a 2a 2a 0a 2a 2a 20 20  s input..**.**  
2f651 20 4e 20 3d 3d 20 59 59 4e 53 54 41 54 45 2b 59   N == YYNSTATE+Y
2f652 59 4e 52 55 4c 45 2b 32 20 20 20 20 20 20 20 20  YNRULE+2        
2f653 20 20 20 20 4e 6f 20 73 75 63 68 20 61 63 74 69      No such acti
2f654 6f 6e 2e 20 20 44 65 6e 6f 74 65 73 20 75 6e 75  on.  Denotes unu
2f655 73 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  sed.**          
2f656 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f657 20 20 20 20 20 20 20 20 20 20 20 20 73 6c 6f 74              slot
2f658 73 20 69 6e 20 74 68 65 20 79 79 5f 61 63 74 69  s in the yy_acti
2f659 6f 6e 5b 5d 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a  on[] table..**.*
2f65a 2a 20 54 68 65 20 61 63 74 69 6f 6e 20 74 61 62  * The action tab
2f65b 6c 65 20 69 73 20 63 6f 6e 73 74 72 75 63 74 65  le is constructe
2f65c 64 20 61 73 20 61 20 73 69 6e 67 6c 65 20 6c 61  d as a single la
2f65d 72 67 65 20 74 61 62 6c 65 20 6e 61 6d 65 64 20  rge table named 
2f65e 79 79 5f 61 63 74 69 6f 6e 5b 5d 2e 0a 2a 2a 20  yy_action[]..** 
2f65f 47 69 76 65 6e 20 73 74 61 74 65 20 53 20 61 6e  Given state S an
2f660 64 20 6c 6f 6f 6b 61 68 65 61 64 20 58 2c 20 74  d lookahead X, t
2f661 68 65 20 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d  he action is com
2f662 70 75 74 65 64 20 61 73 0a 2a 2a 0a 2a 2a 20 20  puted as.**.**  
2f663 20 20 20 20 79 79 5f 61 63 74 69 6f 6e 5b 20 79      yy_action[ y
2f664 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b 53 5d 20  y_shift_ofst[S] 
2f665 2b 20 58 20 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 74  + X ].**.** If t
2f666 68 65 20 69 6e 64 65 78 20 76 61 6c 75 65 20 79  he index value y
2f667 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b 53 5d 2b  y_shift_ofst[S]+
2f668 58 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67  X is out of rang
2f669 65 20 6f 72 20 69 66 20 74 68 65 20 76 61 6c 75  e or if the valu
2f66a 65 0a 2a 2a 20 79 79 5f 6c 6f 6f 6b 61 68 65 61  e.** yy_lookahea
2f66b 64 5b 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b  d[yy_shift_ofst[
2f66c 53 5d 2b 58 5d 20 69 73 20 6e 6f 74 20 65 71 75  S]+X] is not equ
2f66d 61 6c 20 74 6f 20 58 20 6f 72 20 69 66 20 79 79  al to X or if yy
2f66e 5f 73 68 69 66 74 5f 6f 66 73 74 5b 53 5d 0a 2a  _shift_ofst[S].*
2f66f 2a 20 69 73 20 65 71 75 61 6c 20 74 6f 20 59 59  * is equal to YY
2f670 5f 53 48 49 46 54 5f 55 53 45 5f 44 46 4c 54 2c  _SHIFT_USE_DFLT,
2f671 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74   it means that t
2f672 68 65 20 61 63 74 69 6f 6e 20 69 73 20 6e 6f 74  he action is not
2f673 20 69 6e 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a   in the table.**
2f674 20 61 6e 64 20 74 68 61 74 20 79 79 5f 64 65 66   and that yy_def
2f675 61 75 6c 74 5b 53 5d 20 73 68 6f 75 6c 64 20 62  ault[S] should b
2f676 65 20 75 73 65 64 20 69 6e 73 74 65 61 64 2e 20  e used instead. 
2f677 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 72 6d   .**.** The form
2f678 75 6c 61 20 61 62 6f 76 65 20 69 73 20 66 6f 72  ula above is for
2f679 20 63 6f 6d 70 75 74 69 6e 67 20 74 68 65 20 61   computing the a
2f67a 63 74 69 6f 6e 20 77 68 65 6e 20 74 68 65 20 6c  ction when the l
2f67b 6f 6f 6b 61 68 65 61 64 20 69 73 0a 2a 2a 20 61  ookahead is.** a
2f67c 20 74 65 72 6d 69 6e 61 6c 20 73 79 6d 62 6f 6c   terminal symbol
2f67d 2e 20 20 49 66 20 74 68 65 20 6c 6f 6f 6b 61 68  .  If the lookah
2f67e 65 61 64 20 69 73 20 61 20 6e 6f 6e 2d 74 65 72  ead is a non-ter
2f67f 6d 69 6e 61 6c 20 28 61 73 20 6f 63 63 75 72 73  minal (as occurs
2f680 20 61 66 74 65 72 0a 2a 2a 20 61 20 72 65 64 75   after.** a redu
2f681 63 65 20 61 63 74 69 6f 6e 29 20 74 68 65 6e 20  ce action) then 
2f682 74 68 65 20 79 79 5f 72 65 64 75 63 65 5f 6f 66  the yy_reduce_of
2f683 73 74 5b 5d 20 61 72 72 61 79 20 69 73 20 75 73  st[] array is us
2f684 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  ed in place of.*
2f685 2a 20 74 68 65 20 79 79 5f 73 68 69 66 74 5f 6f  * the yy_shift_o
2f686 66 73 74 5b 5d 20 61 72 72 61 79 20 61 6e 64 20  fst[] array and 
2f687 59 59 5f 52 45 44 55 43 45 5f 55 53 45 5f 44 46  YY_REDUCE_USE_DF
2f688 4c 54 20 69 73 20 75 73 65 64 20 69 6e 20 70 6c  LT is used in pl
2f689 61 63 65 20 6f 66 0a 2a 2a 20 59 59 5f 53 48 49  ace of.** YY_SHI
2f68a 46 54 5f 55 53 45 5f 44 46 4c 54 2e 0a 2a 2a 0a  FT_USE_DFLT..**.
2f68b 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
2f68c 20 61 72 65 20 74 68 65 20 74 61 62 6c 65 73 20   are the tables 
2f68d 67 65 6e 65 72 61 74 65 64 20 69 6e 20 74 68 69  generated in thi
2f68e 73 20 73 65 63 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a  s section:.**.**
2f68f 20 20 79 79 5f 61 63 74 69 6f 6e 5b 5d 20 20 20    yy_action[]   
2f690 20 20 20 20 20 41 20 73 69 6e 67 6c 65 20 74 61       A single ta
2f691 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61  ble containing a
2f692 6c 6c 20 61 63 74 69 6f 6e 73 2e 0a 2a 2a 20 20  ll actions..**  
2f693 79 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b 5d 20 20  yy_lookahead[]  
2f694 20 20 20 41 20 74 61 62 6c 65 20 63 6f 6e 74 61     A table conta
2f695 69 6e 69 6e 67 20 74 68 65 20 6c 6f 6f 6b 61 68  ining the lookah
2f696 65 61 64 20 66 6f 72 20 65 61 63 68 20 65 6e 74  ead for each ent
2f697 72 79 20 69 6e 0a 2a 2a 20 20 20 20 20 20 20 20  ry in.**        
2f698 20 20 20 20 20 20 20 20 20 20 20 20 20 79 79 5f               yy_
2f699 61 63 74 69 6f 6e 2e 20 20 55 73 65 64 20 74 6f  action.  Used to
2f69a 20 64 65 74 65 63 74 20 68 61 73 68 20 63 6f 6c   detect hash col
2f69b 6c 69 73 69 6f 6e 73 2e 0a 2a 2a 20 20 79 79 5f  lisions..**  yy_
2f69c 73 68 69 66 74 5f 6f 66 73 74 5b 5d 20 20 20 20  shift_ofst[]    
2f69d 46 6f 72 20 65 61 63 68 20 73 74 61 74 65 2c 20  For each state, 
2f69e 74 68 65 20 6f 66 66 73 65 74 20 69 6e 74 6f 20  the offset into 
2f69f 79 79 5f 61 63 74 69 6f 6e 20 66 6f 72 0a 2a 2a  yy_action for.**
2f6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f6a1 20 20 20 20 20 73 68 69 66 74 69 6e 67 20 74 65       shifting te
2f6a2 72 6d 69 6e 61 6c 73 2e 0a 2a 2a 20 20 79 79 5f  rminals..**  yy_
2f6a3 72 65 64 75 63 65 5f 6f 66 73 74 5b 5d 20 20 20  reduce_ofst[]   
2f6a4 46 6f 72 20 65 61 63 68 20 73 74 61 74 65 2c 20  For each state, 
2f6a5 74 68 65 20 6f 66 66 73 65 74 20 69 6e 74 6f 20  the offset into 
2f6a6 79 79 5f 61 63 74 69 6f 6e 20 66 6f 72 0a 2a 2a  yy_action for.**
2f6a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f6a8 20 20 20 20 20 73 68 69 66 74 69 6e 67 20 6e 6f       shifting no
2f6a9 6e 2d 74 65 72 6d 69 6e 61 6c 73 20 61 66 74 65  n-terminals afte
2f6aa 72 20 61 20 72 65 64 75 63 65 2e 0a 2a 2a 20 20  r a reduce..**  
2f6ab 79 79 5f 64 65 66 61 75 6c 74 5b 5d 20 20 20 20  yy_default[]    
2f6ac 20 20 20 44 65 66 61 75 6c 74 20 61 63 74 69 6f     Default actio
2f6ad 6e 20 66 6f 72 20 65 61 63 68 20 73 74 61 74 65  n for each state
2f6ae 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  ..*/.static cons
2f6af 74 20 59 59 41 43 54 49 4f 4e 54 59 50 45 20 79  t YYACTIONTYPE y
2f6b0 79 5f 61 63 74 69 6f 6e 5b 5d 20 3d 20 7b 0a 20  y_action[] = {. 
2f6b1 2f 2a 20 20 20 20 20 30 20 2a 2f 20 20 20 33 30  /*     0 */   30
2f6b2 39 2c 20 20 39 35 39 2c 20 20 31 37 38 2c 20 20  9,  959,  178,  
2f6b3 36 32 38 2c 20 20 20 20 32 2c 20 20 31 35 33 2c  628,    2,  153,
2f6b4 20 20 32 31 36 2c 20 20 34 34 38 2c 20 20 20 32    216,  448,   2
2f6b5 34 2c 20 20 20 32 34 2c 0a 20 2f 2a 20 20 20 20  4,   24,. /*    
2f6b6 31 30 20 2a 2f 20 20 20 20 32 34 2c 20 20 20 32  10 */    24,   2
2f6b7 34 2c 20 20 34 39 37 2c 20 20 20 32 36 2c 20 20  4,  497,   26,  
2f6b8 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c   26,   26,   26,
2f6b9 20 20 20 32 37 2c 20 20 20 32 37 2c 20 20 20 32     27,   27,   2
2f6ba 38 2c 0a 20 2f 2a 20 20 20 20 32 30 20 2a 2f 20  8,. /*    20 */ 
2f6bb 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32     28,   28,   2
2f6bc 39 2c 20 20 32 31 38 2c 20 20 34 32 32 2c 20 20  9,  218,  422,  
2f6bd 34 32 33 2c 20 20 32 31 34 2c 20 20 34 32 32 2c  423,  214,  422,
2f6be 20 20 34 32 33 2c 20 20 34 35 35 2c 0a 20 2f 2a    423,  455,. /*
2f6bf 20 20 20 20 33 30 20 2a 2f 20 20 20 34 36 31 2c      30 */   461,
2f6c0 20 20 20 33 31 2c 20 20 20 32 36 2c 20 20 20 32     31,   26,   2
2f6c1 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20  6,   26,   26,  
2f6c2 20 32 37 2c 20 20 20 32 37 2c 20 20 20 32 38 2c   27,   27,   28,
2f6c3 20 20 20 32 38 2c 0a 20 2f 2a 20 20 20 20 34 30     28,. /*    40
2f6c4 20 2a 2f 20 20 20 20 32 38 2c 20 20 20 32 39 2c   */    28,   29,
2f6c5 20 20 32 31 38 2c 20 20 20 33 30 2c 20 20 34 39    218,   30,  49
2f6c6 32 2c 20 20 20 33 32 2c 20 20 31 33 37 2c 20 20  2,   32,  137,  
2f6c7 20 32 33 2c 20 20 20 32 32 2c 20 20 33 31 35 2c   23,   22,  315,
2f6c8 0a 20 2f 2a 20 20 20 20 35 30 20 2a 2f 20 20 20  . /*    50 */   
2f6c9 34 36 35 2c 20 20 34 36 36 2c 20 20 34 36 32 2c  465,  466,  462,
2f6ca 20 20 34 36 32 2c 20 20 20 32 35 2c 20 20 20 32    462,   25,   2
2f6cb 35 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20  5,   24,   24,  
2f6cc 20 32 34 2c 20 20 20 32 34 2c 0a 20 2f 2a 20 20   24,   24,. /*  
2f6cd 20 20 36 30 20 2a 2f 20 20 20 34 34 35 2c 20 20    60 */   445,  
2f6ce 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c   26,   26,   26,
2f6cf 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20 20 32     26,   27,   2
2f6d0 37 2c 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20  7,   28,   28,  
2f6d1 20 32 38 2c 0a 20 2f 2a 20 20 20 20 37 30 20 2a   28,. /*    70 *
2f6d2 2f 20 20 20 20 32 39 2c 20 20 32 31 38 2c 20 20  /    29,  218,  
2f6d3 33 30 39 2c 20 20 32 31 38 2c 20 20 33 31 38 2c  309,  218,  318,
2f6d4 20 20 34 34 38 2c 20 20 35 32 31 2c 20 20 34 39    448,  521,  49
2f6d5 39 2c 20 20 20 34 35 2c 20 20 20 32 36 2c 0a 20  9,   45,   26,. 
2f6d6 2f 2a 20 20 20 20 38 30 20 2a 2f 20 20 20 20 32  /*    80 */    2
2f6d7 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20  6,   26,   26,  
2f6d8 20 32 37 2c 20 20 20 32 37 2c 20 20 20 32 38 2c   27,   27,   28,
2f6d9 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32     28,   28,   2
2f6da 39 2c 20 20 32 31 38 2c 0a 20 2f 2a 20 20 20 20  9,  218,. /*    
2f6db 39 30 20 2a 2f 20 20 20 34 32 32 2c 20 20 34 32  90 */   422,  42
2f6dc 33 2c 20 20 34 32 35 2c 20 20 34 32 36 2c 20 20  3,  425,  426,  
2f6dd 31 35 39 2c 20 20 34 32 35 2c 20 20 34 32 36 2c  159,  425,  426,
2f6de 20 20 33 36 36 2c 20 20 33 36 39 2c 20 20 33 37    366,  369,  37
2f6df 30 2c 0a 20 2f 2a 20 20 20 31 30 30 20 2a 2f 20  0,. /*   100 */ 
2f6e0 20 20 33 31 38 2c 20 20 34 35 35 2c 20 20 34 36    318,  455,  46
2f6e1 31 2c 20 20 33 39 34 2c 20 20 35 32 33 2c 20 20  1,  394,  523,  
2f6e2 20 32 31 2c 20 20 31 38 38 2c 20 20 35 30 34 2c   21,  188,  504,
2f6e3 20 20 33 37 31 2c 20 20 20 32 37 2c 0a 20 2f 2a    371,   27,. /*
2f6e4 20 20 20 31 31 30 20 2a 2f 20 20 20 20 32 37 2c     110 */    27,
2f6e5 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32     28,   28,   2
2f6e6 38 2c 20 20 20 32 39 2c 20 20 32 31 38 2c 20 20  8,   29,  218,  
2f6e7 34 32 32 2c 20 20 34 32 33 2c 20 20 34 32 34 2c  422,  423,  424,
2f6e8 20 20 20 32 33 2c 0a 20 2f 2a 20 20 20 31 32 30     23,. /*   120
2f6e9 20 2a 2f 20 20 20 20 32 32 2c 20 20 33 31 35 2c   */    22,  315,
2f6ea 20 20 34 36 35 2c 20 20 34 36 36 2c 20 20 34 36    465,  466,  46
2f6eb 32 2c 20 20 34 36 32 2c 20 20 20 32 35 2c 20 20  2,  462,   25,  
2f6ec 20 32 35 2c 20 20 20 32 34 2c 20 20 20 32 34 2c   25,   24,   24,
2f6ed 0a 20 2f 2a 20 20 20 31 33 30 20 2a 2f 20 20 20  . /*   130 */   
2f6ee 20 32 34 2c 20 20 20 32 34 2c 20 20 35 36 34 2c   24,   24,  564,
2f6ef 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32     26,   26,   2
2f6f0 36 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20  6,   26,   27,  
2f6f1 20 32 37 2c 20 20 20 32 38 2c 0a 20 2f 2a 20 20   27,   28,. /*  
2f6f2 20 31 34 30 20 2a 2f 20 20 20 20 32 38 2c 20 20   140 */    28,  
2f6f3 20 32 38 2c 20 20 20 32 39 2c 20 20 32 31 38 2c   28,   29,  218,
2f6f4 20 20 33 30 39 2c 20 20 32 33 30 2c 20 20 35 31    309,  230,  51
2f6f5 33 2c 20 20 31 33 38 2c 20 20 34 37 37 2c 20 20  3,  138,  477,  
2f6f6 32 32 30 2c 0a 20 2f 2a 20 20 20 31 35 30 20 2a  220,. /*   150 *
2f6f7 2f 20 20 20 35 35 37 2c 20 20 31 34 38 2c 20 20  /   557,  148,  
2f6f8 31 33 35 2c 20 20 32 36 30 2c 20 20 33 36 34 2c  135,  260,  364,
2f6f9 20 20 32 36 35 2c 20 20 33 36 35 2c 20 20 31 35    265,  365,  15
2f6fa 36 2c 20 20 34 32 35 2c 20 20 34 32 36 2c 0a 20  6,  425,  426,. 
2f6fb 2f 2a 20 20 20 31 36 30 20 2a 2f 20 20 20 32 34  /*   160 */   24
2f6fc 35 2c 20 20 36 31 30 2c 20 20 33 33 37 2c 20 20  5,  610,  337,  
2f6fd 20 33 30 2c 20 20 32 36 39 2c 20 20 20 33 32 2c   30,  269,   32,
2f6fe 20 20 31 33 37 2c 20 20 34 34 38 2c 20 20 36 30    137,  448,  60
2f6ff 38 2c 20 20 36 30 39 2c 0a 20 2f 2a 20 20 20 31  8,  609,. /*   1
2f700 37 30 20 2a 2f 20 20 20 32 33 33 2c 20 20 32 33  70 */   233,  23
2f701 30 2c 20 20 34 39 39 2c 20 20 34 35 35 2c 20 20  0,  499,  455,  
2f702 34 36 31 2c 20 20 20 35 37 2c 20 20 35 31 35 2c  461,   57,  515,
2f703 20 20 33 33 34 2c 20 20 31 33 35 2c 20 20 32 36    334,  135,  26
2f704 30 2c 0a 20 2f 2a 20 20 20 31 38 30 20 2a 2f 20  0,. /*   180 */ 
2f705 20 20 33 36 34 2c 20 20 32 36 35 2c 20 20 33 36    364,  265,  36
2f706 35 2c 20 20 31 35 36 2c 20 20 34 32 35 2c 20 20  5,  156,  425,  
2f707 34 32 36 2c 20 20 34 34 34 2c 20 20 20 37 38 2c  426,  444,   78,
2f708 20 20 34 31 37 2c 20 20 34 31 34 2c 0a 20 2f 2a    417,  414,. /*
2f709 20 20 20 31 39 30 20 2a 2f 20 20 20 32 36 39 2c     190 */   269,
2f70a 20 20 20 32 33 2c 20 20 20 32 32 2c 20 20 33 31     23,   22,  31
2f70b 35 2c 20 20 34 36 35 2c 20 20 34 36 36 2c 20 20  5,  465,  466,  
2f70c 34 36 32 2c 20 20 34 36 32 2c 20 20 20 32 35 2c  462,  462,   25,
2f70d 20 20 20 32 35 2c 0a 20 2f 2a 20 20 20 32 30 30     25,. /*   200
2f70e 20 2a 2f 20 20 20 20 32 34 2c 20 20 20 32 34 2c   */    24,   24,
2f70f 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20 33 34     24,   24,  34
2f710 38 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20  8,   26,   26,  
2f711 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32 37 2c   26,   26,   27,
2f712 0a 20 2f 2a 20 20 20 32 31 30 20 2a 2f 20 20 20  . /*   210 */   
2f713 20 32 37 2c 20 20 20 32 38 2c 20 20 20 32 38 2c   27,   28,   28,
2f714 20 20 20 32 38 2c 20 20 20 32 39 2c 20 20 32 31     28,   29,  21
2f715 38 2c 20 20 33 30 39 2c 20 20 32 31 36 2c 20 20  8,  309,  216,  
2f716 35 34 33 2c 20 20 35 35 36 2c 0a 20 2f 2a 20 20  543,  556,. /*  
2f717 20 32 32 30 20 2a 2f 20 20 20 34 38 36 2c 20 20   220 */   486,  
2f718 31 33 30 2c 20 20 34 39 38 2c 20 20 36 30 37 2c  130,  498,  607,
2f719 20 20 20 33 30 2c 20 20 33 33 37 2c 20 20 20 33     30,  337,   3
2f71a 32 2c 20 20 31 33 37 2c 20 20 33 35 31 2c 20 20  2,  137,  351,  
2f71b 33 39 36 2c 0a 20 2f 2a 20 20 20 32 33 30 20 2a  396,. /*   230 *
2f71c 2f 20 20 20 34 33 38 2c 20 20 20 36 33 2c 20 20  /   438,   63,  
2f71d 33 33 37 2c 20 20 33 36 31 2c 20 20 34 32 34 2c  337,  361,  424,
2f71e 20 20 34 34 38 2c 20 20 34 38 37 2c 20 20 33 33    448,  487,  33
2f71f 37 2c 20 20 34 32 34 2c 20 20 35 34 34 2c 0a 20  7,  424,  544,. 
2f720 2f 2a 20 20 20 32 34 30 20 2a 2f 20 20 20 33 33  /*   240 */   33
2f721 34 2c 20 20 32 31 37 2c 20 20 31 39 35 2c 20 20  4,  217,  195,  
2f722 36 30 36 2c 20 20 36 30 35 2c 20 20 34 35 35 2c  606,  605,  455,
2f723 20 20 34 36 31 2c 20 20 33 33 34 2c 20 20 20 31    461,  334,   1
2f724 38 2c 20 20 34 34 34 2c 0a 20 2f 2a 20 20 20 32  8,  444,. /*   2
2f725 35 30 20 2a 2f 20 20 20 20 38 35 2c 20 20 34 38  50 */    85,  48
2f726 38 2c 20 20 33 33 34 2c 20 20 33 34 37 2c 20 20  8,  334,  347,  
2f727 31 39 32 2c 20 20 35 36 35 2c 20 20 34 34 34 2c  192,  565,  444,
2f728 20 20 20 37 38 2c 20 20 33 31 36 2c 20 20 34 37     78,  316,  47
2f729 32 2c 0a 20 2f 2a 20 20 20 32 36 30 20 2a 2f 20  2,. /*   260 */ 
2f72a 20 20 34 37 33 2c 20 20 34 34 34 2c 20 20 20 38    473,  444,   8
2f72b 35 2c 20 20 20 32 33 2c 20 20 20 32 32 2c 20 20  5,   23,   22,  
2f72c 33 31 35 2c 20 20 34 36 35 2c 20 20 34 36 36 2c  315,  465,  466,
2f72d 20 20 34 36 32 2c 20 20 34 36 32 2c 0a 20 2f 2a    462,  462,. /*
2f72e 20 20 20 32 37 30 20 2a 2f 20 20 20 20 32 35 2c     270 */    25,
2f72f 20 20 20 32 35 2c 20 20 20 32 34 2c 20 20 20 32     25,   24,   2
2f730 34 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20  4,   24,   24,  
2f731 34 34 35 2c 20 20 20 32 36 2c 20 20 20 32 36 2c  445,   26,   26,
2f732 20 20 20 32 36 2c 0a 20 2f 2a 20 20 20 32 38 30     26,. /*   280
2f733 20 2a 2f 20 20 20 20 32 36 2c 20 20 20 32 37 2c   */    26,   27,
2f734 20 20 20 32 37 2c 20 20 20 32 38 2c 20 20 20 32     27,   28,   2
2f735 38 2c 20 20 20 32 38 2c 20 20 20 32 39 2c 20 20  8,   28,   29,  
2f736 32 31 38 2c 20 20 33 30 39 2c 20 20 33 35 33 2c  218,  309,  353,
2f737 0a 20 2f 2a 20 20 20 32 39 30 20 2a 2f 20 20 20  . /*   290 */   
2f738 32 32 33 2c 20 20 33 32 30 2c 20 20 36 30 37 2c  223,  320,  607,
2f739 20 20 31 39 33 2c 20 20 32 33 38 2c 20 20 33 33    193,  238,  33
2f73a 37 2c 20 20 34 38 31 2c 20 20 20 31 36 2c 20 20  7,  481,   16,  
2f73b 33 35 31 2c 20 20 31 38 35 2c 0a 20 2f 2a 20 20  351,  185,. /*  
2f73c 20 33 30 30 20 2a 2f 20 20 20 33 33 30 2c 20 20   300 */   330,  
2f73d 34 31 39 2c 20 20 32 32 32 2c 20 20 33 35 30 2c  419,  222,  350,
2f73e 20 20 36 30 34 2c 20 20 32 31 39 2c 20 20 32 31    604,  219,  21
2f73f 35 2c 20 20 34 32 34 2c 20 20 31 31 32 2c 20 20  5,  424,  112,  
2f740 33 33 37 2c 0a 20 2f 2a 20 20 20 33 31 30 20 2a  337,. /*   310 *
2f741 2f 20 20 20 33 33 34 2c 20 20 31 35 37 2c 20 20  /   334,  157,  
2f742 36 30 36 2c 20 20 34 30 38 2c 20 20 32 31 33 2c  606,  408,  213,
2f743 20 20 35 36 33 2c 20 20 35 33 38 2c 20 20 34 35    563,  538,  45
2f744 35 2c 20 20 34 36 31 2c 20 20 34 34 34 2c 0a 20  5,  461,  444,. 
2f745 2f 2a 20 20 20 33 32 30 20 2a 2f 20 20 20 20 37  /*   320 */    7
2f746 39 2c 20 20 32 31 39 2c 20 20 35 36 32 2c 20 20  9,  219,  562,  
2f747 35 32 34 2c 20 20 33 33 34 2c 20 20 35 37 36 2c  524,  334,  576,
2f748 20 20 35 32 32 2c 20 20 36 32 39 2c 20 20 34 31    522,  629,  41
2f749 37 2c 20 20 34 31 34 2c 0a 20 2f 2a 20 20 20 33  7,  414,. /*   3
2f74a 33 30 20 2a 2f 20 20 20 34 35 30 2c 20 20 35 38  30 */   450,  58
2f74b 31 2c 20 20 34 34 31 2c 20 20 34 34 34 2c 20 20  1,  441,  444,  
2f74c 20 37 38 2c 20 20 20 32 33 2c 20 20 20 32 32 2c   78,   23,   22,
2f74d 20 20 33 31 35 2c 20 20 34 36 35 2c 20 20 34 36    315,  465,  46
2f74e 36 2c 0a 20 2f 2a 20 20 20 33 34 30 20 2a 2f 20  6,. /*   340 */ 
2f74f 20 20 34 36 32 2c 20 20 34 36 32 2c 20 20 20 32    462,  462,   2
2f750 35 2c 20 20 20 32 35 2c 20 20 20 32 34 2c 20 20  5,   25,   24,  
2f751 20 32 34 2c 20 20 20 32 34 2c 20 20 20 32 34 2c   24,   24,   24,
2f752 20 20 34 34 35 2c 20 20 20 32 36 2c 0a 20 2f 2a    445,   26,. /*
2f753 20 20 20 33 35 30 20 2a 2f 20 20 20 20 32 36 2c     350 */    26,
2f754 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32     26,   26,   2
2f755 37 2c 20 20 20 32 37 2c 20 20 20 32 38 2c 20 20  7,   27,   28,  
2f756 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32 39 2c   28,   28,   29,
2f757 20 20 32 31 38 2c 0a 20 2f 2a 20 20 20 33 36 30    218,. /*   360
2f758 20 2a 2f 20 20 20 33 30 39 2c 20 20 34 35 32 2c   */   309,  452,
2f759 20 20 34 35 32 2c 20 20 34 35 32 2c 20 20 31 35    452,  452,  15
2f75a 39 2c 20 20 33 39 39 2c 20 20 33 31 31 2c 20 20  9,  399,  311,  
2f75b 33 36 36 2c 20 20 33 36 39 2c 20 20 33 37 30 2c  366,  369,  370,
2f75c 0a 20 2f 2a 20 20 20 33 37 30 20 2a 2f 20 20 20  . /*   370 */   
2f75d 33 33 37 2c 20 20 32 35 31 2c 20 20 34 30 34 2c  337,  251,  404,
2f75e 20 20 34 30 37 2c 20 20 32 31 39 2c 20 20 33 35    407,  219,  35
2f75f 35 2c 20 20 35 35 36 2c 20 20 20 20 34 2c 20 20  5,  556,    4,  
2f760 33 37 31 2c 20 20 34 32 32 2c 0a 20 2f 2a 20 20  371,  422,. /*  
2f761 20 33 38 30 20 2a 2f 20 20 20 34 32 33 2c 20 20   380 */   423,  
2f762 33 39 37 2c 20 20 32 38 36 2c 20 20 32 38 35 2c  397,  286,  285,
2f763 20 20 32 34 34 2c 20 20 33 33 34 2c 20 20 35 34    244,  334,  54
2f764 30 2c 20 20 35 36 36 2c 20 20 20 36 33 2c 20 20  0,  566,   63,  
2f765 34 35 35 2c 0a 20 2f 2a 20 20 20 33 39 30 20 2a  455,. /*   390 *
2f766 2f 20 20 20 34 36 31 2c 20 20 34 32 34 2c 20 20  /   461,  424,  
2f767 32 31 36 2c 20 20 34 37 38 2c 20 20 34 34 34 2c  216,  478,  444,
2f768 20 20 20 39 33 2c 20 20 20 32 38 2c 20 20 20 32     93,   28,   2
2f769 38 2c 20 20 20 32 38 2c 20 20 20 32 39 2c 0a 20  8,   28,   29,. 
2f76a 2f 2a 20 20 20 34 30 30 20 2a 2f 20 20 20 32 31  /*   400 */   21
2f76b 38 2c 20 20 34 31 33 2c 20 20 34 37 37 2c 20 20  8,  413,  477,  
2f76c 32 32 30 2c 20 20 35 37 38 2c 20 20 20 34 30 2c  220,  578,   40,
2f76d 20 20 35 34 35 2c 20 20 20 32 33 2c 20 20 20 32    545,   23,   2
2f76e 32 2c 20 20 33 31 35 2c 0a 20 2f 2a 20 20 20 34  2,  315,. /*   4
2f76f 31 30 20 2a 2f 20 20 20 34 36 35 2c 20 20 34 36  10 */   465,  46
2f770 36 2c 20 20 34 36 32 2c 20 20 34 36 32 2c 20 20  6,  462,  462,  
2f771 20 32 35 2c 20 20 20 32 35 2c 20 20 20 32 34 2c   25,   25,   24,
2f772 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20 20 32     24,   24,   2
2f773 34 2c 0a 20 2f 2a 20 20 20 34 32 30 20 2a 2f 20  4,. /*   420 */ 
2f774 20 20 35 38 32 2c 20 20 20 32 36 2c 20 20 20 32    582,   26,   2
2f775 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20  6,   26,   26,  
2f776 20 32 37 2c 20 20 20 32 37 2c 20 20 20 32 38 2c   27,   27,   28,
2f777 20 20 20 32 38 2c 20 20 20 32 38 2c 0a 20 2f 2a     28,   28,. /*
2f778 20 20 20 34 33 30 20 2a 2f 20 20 20 20 32 39 2c     430 */    29,
2f779 20 20 32 31 38 2c 20 20 33 30 39 2c 20 20 35 34    218,  309,  54
2f77a 36 2c 20 20 33 33 37 2c 20 20 20 33 30 2c 20 20  6,  337,   30,  
2f77b 35 31 37 2c 20 20 20 33 32 2c 20 20 31 33 37 2c  517,   32,  137,
2f77c 20 20 33 37 38 2c 0a 20 2f 2a 20 20 20 34 34 30    378,. /*   440
2f77d 20 2a 2f 20 20 20 33 32 36 2c 20 20 33 33 37 2c   */   326,  337,
2f77e 20 20 38 37 34 2c 20 20 31 35 33 2c 20 20 31 39    874,  153,  19
2f77f 34 2c 20 20 34 34 38 2c 20 20 20 20 31 2c 20 20  4,  448,    1,  
2f780 34 32 35 2c 20 20 34 32 36 2c 20 20 33 33 34 2c  425,  426,  334,
2f781 0a 20 2f 2a 20 20 20 34 35 30 20 2a 2f 20 20 20  . /*   450 */   
2f782 34 32 32 2c 20 20 34 32 33 2c 20 20 34 32 32 2c  422,  423,  422,
2f783 20 20 34 32 33 2c 20 20 20 32 39 2c 20 20 32 31    423,   29,  21
2f784 38 2c 20 20 33 33 34 2c 20 20 36 31 33 2c 20 20  8,  334,  613,  
2f785 34 34 34 2c 20 20 20 37 31 2c 0a 20 2f 2a 20 20  444,   71,. /*  
2f786 20 34 36 30 20 2a 2f 20 20 20 32 31 30 2c 20 20   460 */   210,  
2f787 34 35 35 2c 20 20 34 36 31 2c 20 20 20 36 36 2c  455,  461,   66,
2f788 20 20 35 38 31 2c 20 20 34 34 34 2c 20 20 20 39    581,  444,   9
2f789 33 2c 20 20 34 32 32 2c 20 20 34 32 33 2c 20 20  3,  422,  423,  
2f78a 36 32 36 2c 0a 20 2f 2a 20 20 20 34 37 30 20 2a  626,. /*   470 *
2f78b 2f 20 20 20 39 34 39 2c 20 20 33 30 33 2c 20 20  /   949,  303,  
2f78c 39 34 39 2c 20 20 35 30 30 2c 20 20 34 37 39 2c  949,  500,  479,
2f78d 20 20 35 35 35 2c 20 20 32 30 32 2c 20 20 20 34    555,  202,   4
2f78e 33 2c 20 20 34 34 35 2c 20 20 20 32 33 2c 0a 20  3,  445,   23,. 
2f78f 2f 2a 20 20 20 34 38 30 20 2a 2f 20 20 20 20 32  /*   480 */    2
2f790 32 2c 20 20 33 31 35 2c 20 20 34 36 35 2c 20 20  2,  315,  465,  
2f791 34 36 36 2c 20 20 34 36 32 2c 20 20 34 36 32 2c  466,  462,  462,
2f792 20 20 20 32 35 2c 20 20 20 32 35 2c 20 20 20 32     25,   25,   2
2f793 34 2c 20 20 20 32 34 2c 0a 20 2f 2a 20 20 20 34  4,   24,. /*   4
2f794 39 30 20 2a 2f 20 20 20 20 32 34 2c 20 20 20 32  90 */    24,   2
2f795 34 2c 20 20 34 33 36 2c 20 20 20 32 36 2c 20 20  4,  436,   26,  
2f796 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c   26,   26,   26,
2f797 20 20 20 32 37 2c 20 20 20 32 37 2c 20 20 20 32     27,   27,   2
2f798 38 2c 0a 20 2f 2a 20 20 20 35 30 30 20 2a 2f 20  8,. /*   500 */ 
2f799 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32     28,   28,   2
2f79a 39 2c 20 20 32 31 38 2c 20 20 33 30 39 2c 20 20  9,  218,  309,  
2f79b 31 38 37 2c 20 20 32 31 31 2c 20 20 33 36 30 2c  187,  211,  360,
2f79c 20 20 35 32 30 2c 20 20 34 34 30 2c 0a 20 2f 2a    520,  440,. /*
2f79d 20 20 20 35 31 30 20 2a 2f 20 20 20 32 34 36 2c     510 */   246,
2f79e 20 20 33 32 37 2c 20 20 36 32 32 2c 20 20 34 34    327,  622,  44
2f79f 38 2c 20 20 33 39 37 2c 20 20 32 38 36 2c 20 20  8,  397,  286,  
2f7a0 32 38 35 2c 20 20 35 35 31 2c 20 20 34 32 35 2c  285,  551,  425,
2f7a1 20 20 34 32 36 2c 0a 20 2f 2a 20 20 20 35 32 30    426,. /*   520
2f7a2 20 2a 2f 20 20 20 34 32 35 2c 20 20 34 32 36 2c   */   425,  426,
2f7a3 20 20 33 33 34 2c 20 20 31 35 39 2c 20 20 33 33    334,  159,  33
2f7a4 37 2c 20 20 32 31 36 2c 20 20 33 36 36 2c 20 20  7,  216,  366,  
2f7a5 33 36 39 2c 20 20 33 37 30 2c 20 20 34 39 34 2c  369,  370,  494,
2f7a6 0a 20 2f 2a 20 20 20 35 33 30 20 2a 2f 20 20 20  . /*   530 */   
2f7a7 35 35 36 2c 20 20 34 34 34 2c 20 20 20 20 39 2c  556,  444,    9,
2f7a8 20 20 34 35 35 2c 20 20 34 36 31 2c 20 20 34 32    455,  461,  42
2f7a9 35 2c 20 20 34 32 36 2c 20 20 33 37 31 2c 20 20  5,  426,  371,  
2f7aa 34 39 35 2c 20 20 33 33 34 2c 0a 20 2f 2a 20 20  495,  334,. /*  
2f7ab 20 35 34 30 20 2a 2f 20 20 20 34 34 35 2c 20 20   540 */   445,  
2f7ac 36 31 38 2c 20 20 20 36 33 2c 20 20 35 30 34 2c  618,   63,  504,
2f7ad 20 20 31 39 38 2c 20 20 34 32 34 2c 20 20 35 30    198,  424,  50
2f7ae 31 2c 20 20 34 34 39 2c 20 20 34 34 34 2c 20 20  1,  449,  444,  
2f7af 20 37 32 2c 0a 20 2f 2a 20 20 20 35 35 30 20 2a   72,. /*   550 *
2f7b0 2f 20 20 20 34 37 34 2c 20 20 20 32 33 2c 20 20  /   474,   23,  
2f7b1 20 32 32 2c 20 20 33 31 35 2c 20 20 34 36 35 2c   22,  315,  465,
2f7b2 20 20 34 36 36 2c 20 20 34 36 32 2c 20 20 34 36    466,  462,  46
2f7b3 32 2c 20 20 20 32 35 2c 20 20 20 32 35 2c 0a 20  2,   25,   25,. 
2f7b4 2f 2a 20 20 20 35 36 30 20 2a 2f 20 20 20 20 32  /*   560 */    2
2f7b5 34 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20  4,   24,   24,  
2f7b6 20 32 34 2c 20 20 33 39 35 2c 20 20 20 32 36 2c   24,  395,   26,
2f7b7 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32     26,   26,   2
2f7b8 36 2c 20 20 20 32 37 2c 0a 20 2f 2a 20 20 20 35  6,   27,. /*   5
2f7b9 37 30 20 2a 2f 20 20 20 20 32 37 2c 20 20 20 32  70 */    27,   2
2f7ba 38 2c 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20  8,   28,   28,  
2f7bb 20 32 39 2c 20 20 32 31 38 2c 20 20 33 30 39 2c   29,  218,  309,
2f7bc 20 20 34 38 36 2c 20 20 34 34 35 2c 20 20 33 33    486,  445,  33
2f7bd 37 2c 0a 20 2f 2a 20 20 20 35 38 30 20 2a 2f 20  7,. /*   580 */ 
2f7be 20 20 35 33 37 2c 20 20 20 36 30 2c 20 20 32 32    537,   60,  22
2f7bf 34 2c 20 20 34 37 39 2c 20 20 33 34 33 2c 20 20  4,  479,  343,  
2f7c0 32 30 32 2c 20 20 33 39 38 2c 20 20 33 33 37 2c  202,  398,  337,
2f7c1 20 20 34 33 39 2c 20 20 35 35 34 2c 0a 20 2f 2a    439,  554,. /*
2f7c2 20 20 20 35 39 30 20 2a 2f 20 20 20 31 39 39 2c     590 */   199,
2f7c3 20 20 31 34 30 2c 20 20 33 33 37 2c 20 20 34 38    140,  337,  48
2f7c4 37 2c 20 20 33 33 34 2c 20 20 35 32 36 2c 20 20  7,  334,  526,  
2f7c5 35 32 37 2c 20 20 35 35 31 2c 20 20 35 31 36 2c  527,  551,  516,
2f7c6 20 20 35 30 38 2c 0a 20 2f 2a 20 20 20 36 30 30    508,. /*   600
2f7c7 20 2a 2f 20 20 20 34 35 36 2c 20 20 34 35 37 2c   */   456,  457,
2f7c8 20 20 33 33 34 2c 20 20 34 34 34 2c 20 20 20 36    334,  444,   6
2f7c9 37 2c 20 20 34 35 35 2c 20 20 34 36 31 2c 20 20  7,  455,  461,  
2f7ca 33 33 34 2c 20 20 34 38 38 2c 20 20 34 37 36 2c  334,  488,  476,
2f7cb 0a 20 2f 2a 20 20 20 36 31 30 20 2a 2f 20 20 20  . /*   610 */   
2f7cc 35 32 38 2c 20 20 34 34 34 2c 20 20 20 37 36 2c  528,  444,   76,
2f7cd 20 20 20 33 39 2c 20 20 34 32 34 2c 20 20 20 34     39,  424,   4
2f7ce 31 2c 20 20 34 34 34 2c 20 20 20 39 37 2c 20 20  1,  444,   97,  
2f7cf 35 37 39 2c 20 20 35 32 37 2c 0a 20 2f 2a 20 20  579,  527,. /*  
2f7d0 20 36 32 30 20 2a 2f 20 20 20 35 32 39 2c 20 20   620 */   529,  
2f7d1 34 35 39 2c 20 20 34 36 30 2c 20 20 20 32 33 2c  459,  460,   23,
2f7d2 20 20 20 32 32 2c 20 20 33 31 35 2c 20 20 34 36     22,  315,  46
2f7d3 35 2c 20 20 34 36 36 2c 20 20 34 36 32 2c 20 20  5,  466,  462,  
2f7d4 34 36 32 2c 0a 20 2f 2a 20 20 20 36 33 30 20 2a  462,. /*   630 *
2f7d5 2f 20 20 20 20 32 35 2c 20 20 20 32 35 2c 20 20  /    25,   25,  
2f7d6 20 32 34 2c 20 20 20 32 34 2c 20 20 20 32 34 2c   24,   24,   24,
2f7d7 20 20 20 32 34 2c 20 20 33 33 37 2c 20 20 20 32     24,  337,   2
2f7d8 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 0a 20  6,   26,   26,. 
2f7d9 2f 2a 20 20 20 36 34 30 20 2a 2f 20 20 20 20 32  /*   640 */    2
2f7da 36 2c 20 20 20 32 37 2c 20 20 20 32 37 2c 20 20  6,   27,   27,  
2f7db 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32 38 2c   28,   28,   28,
2f7dc 20 20 20 32 39 2c 20 20 32 31 38 2c 20 20 33 30     29,  218,  30
2f7dd 39 2c 20 20 33 33 37 2c 0a 20 2f 2a 20 20 20 36  9,  337,. /*   6
2f7de 35 30 20 2a 2f 20 20 20 34 35 38 2c 20 20 33 33  50 */   458,  33
2f7df 34 2c 20 20 32 37 32 2c 20 20 36 32 31 2c 20 20  4,  272,  621,  
2f7e0 33 30 37 2c 20 20 33 33 37 2c 20 20 33 31 32 2c  307,  337,  312,
2f7e1 20 20 33 33 37 2c 20 20 33 37 34 2c 20 20 20 36    337,  374,   6
2f7e2 34 2c 0a 20 2f 2a 20 20 20 36 36 30 20 2a 2f 20  4,. /*   660 */ 
2f7e3 20 20 34 34 34 2c 20 20 20 39 36 2c 20 20 33 31    444,   96,  31
2f7e4 37 2c 20 20 34 34 38 2c 20 20 33 33 34 2c 20 20  7,  448,  334,  
2f7e5 33 34 32 2c 20 20 34 37 32 2c 20 20 34 37 33 2c  342,  472,  473,
2f7e6 20 20 34 36 39 2c 20 20 33 33 37 2c 0a 20 2f 2a    469,  337,. /*
2f7e7 20 20 20 36 37 30 20 2a 2f 20 20 20 33 33 34 2c     670 */   334,
2f7e8 20 20 35 30 38 2c 20 20 33 33 34 2c 20 20 34 34    508,  334,  44
2f7e9 34 2c 20 20 31 30 31 2c 20 20 33 35 39 2c 20 20  4,  101,  359,  
2f7ea 32 35 32 2c 20 20 34 35 35 2c 20 20 34 36 31 2c  252,  455,  461,
2f7eb 20 20 34 34 34 2c 0a 20 2f 2a 20 20 20 36 38 30    444,. /*   680
2f7ec 20 2a 2f 20 20 20 20 39 39 2c 20 20 34 34 34 2c   */    99,  444,
2f7ed 20 20 31 30 34 2c 20 20 33 35 38 2c 20 20 33 33    104,  358,  33
2f7ee 34 2c 20 20 33 34 35 2c 20 20 34 32 34 2c 20 20  4,  345,  424,  
2f7ef 33 34 30 2c 20 20 31 35 37 2c 20 20 34 36 38 2c  340,  157,  468,
2f7f0 0a 20 2f 2a 20 20 20 36 39 30 20 2a 2f 20 20 20  . /*   690 */   
2f7f1 34 36 38 2c 20 20 34 32 34 2c 20 20 34 39 33 2c  468,  424,  493,
2f7f2 20 20 34 34 34 2c 20 20 31 30 35 2c 20 20 20 32    444,  105,   2
2f7f3 33 2c 20 20 20 32 32 2c 20 20 33 31 35 2c 20 20  3,   22,  315,  
2f7f4 34 36 35 2c 20 20 34 36 36 2c 0a 20 2f 2a 20 20  465,  466,. /*  
2f7f5 20 37 30 30 20 2a 2f 20 20 20 34 36 32 2c 20 20   700 */   462,  
2f7f6 34 36 32 2c 20 20 20 32 35 2c 20 20 20 32 35 2c  462,   25,   25,
2f7f7 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20 20 32     24,   24,   2
2f7f8 34 2c 20 20 20 32 34 2c 20 20 33 33 37 2c 20 20  4,   24,  337,  
2f7f9 20 32 36 2c 0a 20 2f 2a 20 20 20 37 31 30 20 2a   26,. /*   710 *
2f7fa 2f 20 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20  /    26,   26,  
2f7fb 20 32 36 2c 20 20 20 32 37 2c 20 20 20 32 37 2c   26,   27,   27,
2f7fc 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32     28,   28,   2
2f7fd 38 2c 20 20 20 32 39 2c 20 20 32 31 38 2c 0a 20  8,   29,  218,. 
2f7fe 2f 2a 20 20 20 37 32 30 20 2a 2f 20 20 20 33 30  /*   720 */   30
2f7ff 39 2c 20 20 33 33 37 2c 20 20 31 38 31 2c 20 20  9,  337,  181,  
2f800 33 33 34 2c 20 20 34 39 39 2c 20 20 20 35 36 2c  334,  499,   56,
2f801 20 20 31 33 39 2c 20 20 33 33 37 2c 20 20 32 31    139,  337,  21
2f802 39 2c 20 20 32 36 38 2c 0a 20 2f 2a 20 20 20 37  9,  268,. /*   7
2f803 33 30 20 2a 2f 20 20 20 33 38 34 2c 20 20 34 34  30 */   384,  44
2f804 38 2c 20 20 34 34 34 2c 20 20 31 32 39 2c 20 20  8,  444,  129,  
2f805 33 38 32 2c 20 20 33 38 37 2c 20 20 33 33 34 2c  382,  387,  334,
2f806 20 20 31 36 38 2c 20 20 33 33 37 2c 20 20 33 38    168,  337,  38
2f807 39 2c 0a 20 2f 2a 20 20 20 37 34 30 20 2a 2f 20  9,. /*   740 */ 
2f808 20 20 35 30 38 2c 20 20 34 32 34 2c 20 20 33 33    508,  424,  33
2f809 34 2c 20 20 33 31 31 2c 20 20 34 32 34 2c 20 20  4,  311,  424,  
2f80a 34 34 34 2c 20 20 31 33 31 2c 20 20 34 39 36 2c  444,  131,  496,
2f80b 20 20 32 36 39 2c 20 20 34 35 35 2c 0a 20 2f 2a    269,  455,. /*
2f80c 20 20 20 37 35 30 20 2a 2f 20 20 20 34 36 31 2c     750 */   461,
2f80d 20 20 34 34 34 2c 20 20 20 35 39 2c 20 20 33 33    444,   59,  33
2f80e 34 2c 20 20 34 32 34 2c 20 20 34 32 34 2c 20 20  4,  424,  424,  
2f80f 33 39 31 2c 20 20 33 34 30 2c 20 20 20 20 38 2c  391,  340,    8,
2f810 20 20 34 36 38 2c 0a 20 2f 2a 20 20 20 37 36 30    468,. /*   760
2f811 20 2a 2f 20 20 20 34 36 38 2c 20 20 32 36 33 2c   */   468,  263,
2f812 20 20 34 34 34 2c 20 20 31 30 32 2c 20 20 33 39    444,  102,  39
2f813 30 2c 20 20 32 39 30 2c 20 20 33 32 31 2c 20 20  0,  290,  321,  
2f814 20 32 33 2c 20 20 20 32 32 2c 20 20 33 31 35 2c   23,   22,  315,
2f815 0a 20 2f 2a 20 20 20 37 37 30 20 2a 2f 20 20 20  . /*   770 */   
2f816 34 36 35 2c 20 20 34 36 36 2c 20 20 34 36 32 2c  465,  466,  462,
2f817 20 20 34 36 32 2c 20 20 20 32 35 2c 20 20 20 32    462,   25,   2
2f818 35 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20  5,   24,   24,  
2f819 20 32 34 2c 20 20 20 32 34 2c 0a 20 2f 2a 20 20   24,   24,. /*  
2f81a 20 37 38 30 20 2a 2f 20 20 20 33 33 37 2c 20 20   780 */   337,  
2f81b 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c   26,   26,   26,
2f81c 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20 20 32     26,   27,   2
2f81d 37 2c 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20  7,   28,   28,  
2f81e 20 32 38 2c 0a 20 2f 2a 20 20 20 37 39 30 20 2a   28,. /*   790 *
2f81f 2f 20 20 20 20 32 39 2c 20 20 32 31 38 2c 20 20  /    29,  218,  
2f820 33 30 39 2c 20 20 33 33 37 2c 20 20 31 33 38 2c  309,  337,  138,
2f821 20 20 33 33 34 2c 20 20 34 31 36 2c 20 20 20 20    334,  416,    
2f822 32 2c 20 20 32 36 38 2c 20 20 33 33 37 2c 0a 20  2,  268,  337,. 
2f823 2f 2a 20 20 20 38 30 30 20 2a 2f 20 20 20 33 38  /*   800 */   38
2f824 39 2c 20 20 33 33 37 2c 20 20 34 34 33 2c 20 20  9,  337,  443,  
2f825 33 32 35 2c 20 20 34 34 34 2c 20 20 20 37 37 2c  325,  444,   77,
2f826 20 20 34 34 32 2c 20 20 32 39 33 2c 20 20 33 33    442,  293,  33
2f827 34 2c 20 20 32 39 31 2c 0a 20 2f 2a 20 20 20 38  4,  291,. /*   8
2f828 31 30 20 2a 2f 20 20 20 20 20 37 2c 20 20 34 38  10 */     7,  48
2f829 32 2c 20 20 33 33 37 2c 20 20 34 32 34 2c 20 20  2,  337,  424,  
2f82a 33 33 34 2c 20 20 34 32 34 2c 20 20 33 33 34 2c  334,  424,  334,
2f82b 20 20 34 34 34 2c 20 20 31 30 30 2c 20 20 34 39    444,  100,  49
2f82c 39 2c 0a 20 2f 2a 20 20 20 38 32 30 20 2a 2f 20  9,. /*   820 */ 
2f82d 20 20 33 33 39 2c 20 20 34 35 35 2c 20 20 34 36    339,  455,  46
2f82e 31 2c 20 20 34 34 34 2c 20 20 20 36 38 2c 20 20  1,  444,   68,  
2f82f 34 34 34 2c 20 20 20 39 38 2c 20 20 33 33 34 2c  444,   98,  334,
2f830 20 20 32 35 34 2c 20 20 35 30 34 2c 0a 20 2f 2a    254,  504,. /*
2f831 20 20 20 38 33 30 20 2a 2f 20 20 20 32 33 32 2c     830 */   232,
2f832 20 20 36 32 36 2c 20 20 39 34 38 2c 20 20 35 30    626,  948,  50
2f833 34 2c 20 20 39 34 38 2c 20 20 32 33 31 2c 20 20  4,  948,  231,  
2f834 34 34 34 2c 20 20 31 33 32 2c 20 20 20 34 37 2c  444,  132,   47,
2f835 20 20 20 32 33 2c 0a 20 2f 2a 20 20 20 38 34 30     23,. /*   840
2f836 20 2a 2f 20 20 20 20 32 32 2c 20 20 33 31 35 2c   */    22,  315,
2f837 20 20 34 36 35 2c 20 20 34 36 36 2c 20 20 34 36    465,  466,  46
2f838 32 2c 20 20 34 36 32 2c 20 20 20 32 35 2c 20 20  2,  462,   25,  
2f839 20 32 35 2c 20 20 20 32 34 2c 20 20 20 32 34 2c   25,   24,   24,
2f83a 0a 20 2f 2a 20 20 20 38 35 30 20 2a 2f 20 20 20  . /*   850 */   
2f83b 20 32 34 2c 20 20 20 32 34 2c 20 20 33 33 37 2c   24,   24,  337,
2f83c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32     26,   26,   2
2f83d 36 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20  6,   26,   27,  
2f83e 20 32 37 2c 20 20 20 32 38 2c 0a 20 2f 2a 20 20   27,   28,. /*  
2f83f 20 38 36 30 20 2a 2f 20 20 20 20 32 38 2c 20 20   860 */    28,  
2f840 20 32 38 2c 20 20 20 32 39 2c 20 20 32 31 38 2c   28,   29,  218,
2f841 20 20 33 30 39 2c 20 20 33 33 37 2c 20 20 32 38    309,  337,  28
2f842 30 2c 20 20 33 33 34 2c 20 20 32 35 36 2c 20 20  0,  334,  256,  
2f843 35 33 38 2c 0a 20 2f 2a 20 20 20 38 37 30 20 2a  538,. /*   870 *
2f844 2f 20 20 20 33 36 32 2c 20 20 33 33 37 2c 20 20  /   362,  337,  
2f845 32 35 38 2c 20 20 32 36 38 2c 20 20 36 32 32 2c  258,  268,  622,
2f846 20 20 35 34 39 2c 20 20 34 34 34 2c 20 20 31 33    549,  444,  13
2f847 33 2c 20 20 32 30 33 2c 20 20 31 34 30 2c 0a 20  3,  203,  140,. 
2f848 2f 2a 20 20 20 38 38 30 20 2a 2f 20 20 20 33 33  /*   880 */   33
2f849 34 2c 20 20 34 32 34 2c 20 20 35 34 38 2c 20 20  4,  424,  548,  
2f84a 33 33 37 2c 20 20 31 38 30 2c 20 20 31 35 38 2c  337,  180,  158,
2f84b 20 20 33 33 34 2c 20 20 32 39 32 2c 20 20 34 32    334,  292,  42
2f84c 34 2c 20 20 34 34 34 2c 0a 20 2f 2a 20 20 20 38  4,  444,. /*   8
2f84d 39 30 20 2a 2f 20 20 20 31 33 34 2c 20 20 32 38  90 */   134,  28
2f84e 37 2c 20 20 35 35 32 2c 20 20 34 35 35 2c 20 20  7,  552,  455,  
2f84f 34 36 31 2c 20 20 34 34 34 2c 20 20 20 36 39 2c  461,  444,   69,
2f850 20 20 34 34 33 2c 20 20 33 33 34 2c 20 20 34 36    443,  334,  46
2f851 33 2c 0a 20 2f 2a 20 20 20 39 30 30 20 2a 2f 20  3,. /*   900 */ 
2f852 20 20 33 34 30 2c 20 20 34 34 32 2c 20 20 34 36    340,  442,  46
2f853 38 2c 20 20 34 36 38 2c 20 20 34 32 37 2c 20 20  8,  468,  427,  
2f854 34 32 38 2c 20 20 34 32 39 2c 20 20 34 34 34 2c  428,  429,  444,
2f855 20 20 20 38 30 2c 20 20 32 38 31 2c 0a 20 2f 2a     80,  281,. /*
2f856 20 20 20 39 31 30 20 2a 2f 20 20 20 33 32 32 2c     910 */   322,
2f857 20 20 20 32 33 2c 20 20 20 33 33 2c 20 20 33 31     23,   33,  31
2f858 35 2c 20 20 34 36 35 2c 20 20 34 36 36 2c 20 20  5,  465,  466,  
2f859 34 36 32 2c 20 20 34 36 32 2c 20 20 20 32 35 2c  462,  462,   25,
2f85a 20 20 20 32 35 2c 0a 20 2f 2a 20 20 20 39 32 30     25,. /*   920
2f85b 20 2a 2f 20 20 20 20 32 34 2c 20 20 20 32 34 2c   */    24,   24,
2f85c 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20 33 33     24,   24,  33
2f85d 37 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20  7,   26,   26,  
2f85e 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32 37 2c   26,   26,   27,
2f85f 0a 20 2f 2a 20 20 20 39 33 30 20 2a 2f 20 20 20  . /*   930 */   
2f860 20 32 37 2c 20 20 20 32 38 2c 20 20 20 32 38 2c   27,   28,   28,
2f861 20 20 20 32 38 2c 20 20 20 32 39 2c 20 20 32 31     28,   29,  21
2f862 38 2c 20 20 33 30 39 2c 20 20 33 33 37 2c 20 20  8,  309,  337,  
2f863 34 30 36 2c 20 20 33 33 34 2c 0a 20 2f 2a 20 20  406,  334,. /*  
2f864 20 39 34 30 20 2a 2f 20 20 20 32 31 32 2c 20 20   940 */   212,  
2f865 32 36 38 2c 20 20 35 35 30 2c 20 20 33 33 37 2c  268,  550,  337,
2f866 20 20 32 36 38 2c 20 20 33 38 39 2c 20 20 33 32    268,  389,  32
2f867 39 2c 20 20 31 37 37 2c 20 20 34 34 34 2c 20 20  9,  177,  444,  
2f868 20 38 31 2c 0a 20 2f 2a 20 20 20 39 35 30 20 2a   81,. /*   950 *
2f869 2f 20 20 20 35 34 32 2c 20 20 35 34 31 2c 20 20  /   542,  541,  
2f86a 33 33 34 2c 20 20 34 37 35 2c 20 20 34 37 35 2c  334,  475,  475,
2f86b 20 20 33 33 37 2c 20 20 34 32 34 2c 20 20 32 31    337,  424,  21
2f86c 36 2c 20 20 33 33 34 2c 20 20 34 32 34 2c 0a 20  6,  334,  424,. 
2f86d 2f 2a 20 20 20 39 36 30 20 2a 2f 20 20 20 34 32  /*   960 */   42
2f86e 34 2c 20 20 34 34 34 2c 20 20 20 37 30 2c 20 20  4,  444,   70,  
2f86f 35 33 35 2c 20 20 33 36 38 2c 20 20 34 35 35 2c  535,  368,  455,
2f870 20 20 34 36 31 2c 20 20 34 34 34 2c 20 20 20 38    461,  444,   8
2f871 32 2c 20 20 34 30 35 2c 0a 20 2f 2a 20 20 20 39  2,  405,. /*   9
2f872 37 30 20 2a 2f 20 20 20 33 33 34 2c 20 20 32 36  70 */   334,  26
2f873 31 2c 20 20 33 39 32 2c 20 20 33 34 30 2c 20 20  1,  392,  340,  
2f874 34 34 35 2c 20 20 34 36 38 2c 20 20 34 36 38 2c  445,  468,  468,
2f875 20 20 35 38 37 2c 20 20 33 32 33 2c 20 20 34 34    587,  323,  44
2f876 34 2c 0a 20 2f 2a 20 20 20 39 38 30 20 2a 2f 20  4,. /*   980 */ 
2f877 20 20 20 38 33 2c 20 20 33 32 34 2c 20 20 32 36     83,  324,  26
2f878 32 2c 20 20 32 38 38 2c 20 20 20 32 32 2c 20 20  2,  288,   22,  
2f879 33 31 35 2c 20 20 34 36 35 2c 20 20 34 36 36 2c  315,  465,  466,
2f87a 20 20 34 36 32 2c 20 20 34 36 32 2c 0a 20 2f 2a    462,  462,. /*
2f87b 20 20 20 39 39 30 20 2a 2f 20 20 20 20 32 35 2c     990 */    25,
2f87c 20 20 20 32 35 2c 20 20 20 32 34 2c 20 20 20 32     25,   24,   2
2f87d 34 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20  4,   24,   24,  
2f87e 33 33 37 2c 20 20 20 32 36 2c 20 20 20 32 36 2c  337,   26,   26,
2f87f 20 20 20 32 36 2c 0a 20 2f 2a 20 20 31 30 30 30     26,. /*  1000
2f880 20 2a 2f 20 20 20 20 32 36 2c 20 20 20 32 37 2c   */    26,   27,
2f881 20 20 20 32 37 2c 20 20 20 32 38 2c 20 20 20 32     27,   28,   2
2f882 38 2c 20 20 20 32 38 2c 20 20 20 32 39 2c 20 20  8,   28,   29,  
2f883 32 31 38 2c 20 20 33 30 39 2c 20 20 33 33 37 2c  218,  309,  337,
2f884 0a 20 2f 2a 20 20 31 30 31 30 20 2a 2f 20 20 20  . /*  1010 */   
2f885 32 31 31 2c 20 20 33 33 34 2c 20 20 32 39 34 2c  211,  334,  294,
2f886 20 20 33 35 36 2c 20 20 33 34 30 2c 20 20 33 33    356,  340,  33
2f887 37 2c 20 20 34 36 38 2c 20 20 34 36 38 2c 20 20  7,  468,  468,  
2f888 35 33 32 2c 20 20 35 33 33 2c 0a 20 2f 2a 20 20  532,  533,. /*  
2f889 31 30 32 30 20 2a 2f 20 20 20 34 34 34 2c 20 20  1020 */   444,  
2f88a 20 38 34 2c 20 20 34 30 33 2c 20 20 31 34 34 2c   84,  403,  144,
2f88b 20 20 33 33 34 2c 20 20 35 37 34 2c 20 20 36 30    334,  574,  60
2f88c 30 2c 20 20 33 33 37 2c 20 20 34 32 34 2c 20 20  0,  337,  424,  
2f88d 35 37 33 2c 0a 20 2f 2a 20 20 31 30 33 30 20 2a  573,. /*  1030 *
2f88e 2f 20 20 20 33 33 34 2c 20 20 33 33 37 2c 20 20  /   334,  337,  
2f88f 34 32 30 2c 20 20 34 34 34 2c 20 20 20 38 36 2c  420,  444,   86,
2f890 20 20 32 35 33 2c 20 20 32 33 34 2c 20 20 34 35    253,  234,  45
2f891 35 2c 20 20 34 36 31 2c 20 20 34 34 34 2c 0a 20  5,  461,  444,. 
2f892 2f 2a 20 20 31 30 34 30 20 2a 2f 20 20 20 20 38  /*  1040 */    8
2f893 37 2c 20 20 34 33 30 2c 20 20 33 33 34 2c 20 20  7,  430,  334,  
2f894 33 38 33 2c 20 20 34 34 35 2c 20 20 34 33 31 2c  383,  445,  431,
2f895 20 20 33 33 34 2c 20 20 32 37 34 2c 20 20 31 39    334,  274,  19
2f896 36 2c 20 20 33 33 31 2c 0a 20 2f 2a 20 20 31 30  6,  331,. /*  10
2f897 35 30 20 2a 2f 20 20 20 34 32 34 2c 20 20 34 34  50 */   424,  44
2f898 34 2c 20 20 20 38 38 2c 20 20 34 33 32 2c 20 20  4,   88,  432,  
2f899 31 34 35 2c 20 20 34 34 34 2c 20 20 20 37 33 2c  145,  444,   73,
2f89a 20 20 33 31 35 2c 20 20 34 36 35 2c 20 20 34 36    315,  465,  46
2f89b 36 2c 0a 20 2f 2a 20 20 31 30 36 30 20 2a 2f 20  6,. /*  1060 */ 
2f89c 20 20 34 36 32 2c 20 20 34 36 32 2c 20 20 20 32    462,  462,   2
2f89d 35 2c 20 20 20 32 35 2c 20 20 20 32 34 2c 20 20  5,   25,   24,  
2f89e 20 32 34 2c 20 20 20 32 34 2c 20 20 20 32 34 2c   24,   24,   24,
2f89f 20 20 33 39 35 2c 20 20 20 32 36 2c 0a 20 2f 2a    395,   26,. /*
2f8a0 20 20 31 30 37 30 20 2a 2f 20 20 20 20 32 36 2c    1070 */    26,
2f8a1 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32     26,   26,   2
2f8a2 37 2c 20 20 20 32 37 2c 20 20 20 32 38 2c 20 20  7,   27,   28,  
2f8a3 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32 39 2c   28,   28,   29,
2f8a4 20 20 32 31 38 2c 0a 20 2f 2a 20 20 31 30 38 30    218,. /*  1080
2f8a5 20 2a 2f 20 20 20 20 33 35 2c 20 20 33 34 34 2c   */    35,  344,
2f8a6 20 20 34 34 35 2c 20 20 20 20 33 2c 20 20 33 33    445,    3,  33
2f8a7 37 2c 20 20 33 39 34 2c 20 20 33 33 37 2c 20 20  7,  394,  337,  
2f8a8 33 33 33 2c 20 20 34 32 33 2c 20 20 32 37 38 2c  333,  423,  278,
2f8a9 0a 20 2f 2a 20 20 31 30 39 30 20 2a 2f 20 20 20  . /*  1090 */   
2f8aa 33 38 38 2c 20 20 32 37 36 2c 20 20 32 38 30 2c  388,  276,  280,
2f8ab 20 20 32 30 37 2c 20 20 31 34 37 2c 20 20 20 33    207,  147,   3
2f8ac 35 2c 20 20 33 34 34 2c 20 20 33 34 31 2c 20 20  5,  344,  341,  
2f8ad 20 20 33 2c 20 20 33 33 34 2c 0a 20 2f 2a 20 20    3,  334,. /*  
2f8ae 31 31 30 30 20 2a 2f 20 20 20 34 32 34 2c 20 20  1100 */   424,  
2f8af 33 33 34 2c 20 20 33 33 33 2c 20 20 34 32 33 2c  334,  333,  423,
2f8b0 20 20 33 30 38 2c 20 20 36 32 33 2c 20 20 32 38    308,  623,  28
2f8b1 30 2c 20 20 34 32 34 2c 20 20 34 34 34 2c 20 20  0,  424,  444,  
2f8b2 20 37 34 2c 0a 20 2f 2a 20 20 31 31 31 30 20 2a   74,. /*  1110 *
2f8b3 2f 20 20 20 34 34 34 2c 20 20 20 38 39 2c 20 20  /   444,   89,  
2f8b4 33 34 31 2c 20 20 33 33 37 2c 20 20 20 20 36 2c  341,  337,    6,
2f8b5 20 20 33 34 36 2c 20 20 33 33 38 2c 20 20 33 33    346,  338,  33
2f8b6 37 2c 20 20 34 32 31 2c 20 20 33 33 37 2c 0a 20  7,  421,  337,. 
2f8b7 2f 2a 20 20 31 31 32 30 20 2a 2f 20 20 20 34 37  /*  1120 */   47
2f8b8 30 2c 20 20 34 32 34 2c 20 20 20 36 35 2c 20 20  0,  424,   65,  
2f8b9 33 33 32 2c 20 20 32 38 30 2c 20 20 34 38 31 2c  332,  280,  481,
2f8ba 20 20 34 34 36 2c 20 20 34 34 35 2c 20 20 33 33    446,  445,  33
2f8bb 34 2c 20 20 32 34 37 2c 0a 20 2f 2a 20 20 31 31  4,  247,. /*  11
2f8bc 33 30 20 2a 2f 20 20 20 33 34 36 2c 20 20 34 32  30 */   346,  42
2f8bd 34 2c 20 20 33 33 34 2c 20 20 34 32 34 2c 20 20  4,  334,  424,  
2f8be 33 33 34 2c 20 20 35 39 34 2c 20 20 32 38 30 2c  334,  594,  280,
2f8bf 20 20 34 34 34 2c 20 20 20 39 30 2c 20 20 34 32    444,   90,  42
2f8c0 34 2c 0a 20 2f 2a 20 20 31 31 34 30 20 2a 2f 20  4,. /*  1140 */ 
2f8c1 20 20 34 38 31 2c 20 20 34 34 34 2c 20 20 20 39    481,  444,   9
2f8c2 31 2c 20 20 34 34 34 2c 20 20 20 39 32 2c 20 20  1,  444,   92,  
2f8c3 20 33 38 2c 20 20 20 33 37 2c 20 20 36 32 35 2c   38,   37,  625,
2f8c4 20 20 33 33 37 2c 20 20 34 31 30 2c 0a 20 2f 2a    337,  410,. /*
2f8c5 20 20 31 31 35 30 20 2a 2f 20 20 20 20 34 37 2c    1150 */    47,
2f8c6 20 20 34 32 34 2c 20 20 32 33 37 2c 20 20 32 38    424,  237,  28
2f8c7 30 2c 20 20 20 33 36 2c 20 20 33 33 35 2c 20 20  0,   36,  335,  
2f8c8 33 33 36 2c 20 20 33 35 34 2c 20 20 32 34 38 2c  336,  354,  248,
2f8c9 20 20 34 35 30 2c 0a 20 2f 2a 20 20 31 31 36 30    450,. /*  1160
2f8ca 20 2a 2f 20 20 20 20 33 38 2c 20 20 20 33 37 2c   */    38,   37,
2f8cb 20 20 35 31 34 2c 20 20 33 33 34 2c 20 20 35 37    514,  334,  57
2f8cc 32 2c 20 20 33 38 31 2c 20 20 35 37 32 2c 20 20  2,  381,  572,  
2f8cd 35 39 36 2c 20 20 34 32 34 2c 20 20 20 33 36 2c  596,  424,   36,
2f8ce 0a 20 2f 2a 20 20 31 31 37 30 20 2a 2f 20 20 20  . /*  1170 */   
2f8cf 33 33 35 2c 20 20 33 33 36 2c 20 20 34 34 34 2c  335,  336,  444,
2f8d0 20 20 20 37 35 2c 20 20 34 35 30 2c 20 20 32 30     75,  450,  20
2f8d1 30 2c 20 20 35 30 36 2c 20 20 32 31 36 2c 20 20  0,  506,  216,  
2f8d2 31 35 34 2c 20 20 35 39 37 2c 0a 20 2f 2a 20 20  154,  597,. /*  
2f8d3 31 31 38 30 20 2a 2f 20 20 20 32 33 39 2c 20 20  1180 */   239,  
2f8d4 32 34 30 2c 20 20 32 34 31 2c 20 20 31 34 36 2c  240,  241,  146,
2f8d5 20 20 32 34 33 2c 20 20 32 34 39 2c 20 20 35 34    243,  249,  54
2f8d6 37 2c 20 20 35 39 33 2c 20 20 31 35 38 2c 20 20  7,  593,  158,  
2f8d7 34 33 33 2c 0a 20 2f 2a 20 20 31 31 39 30 20 2a  433,. /*  1190 *
2f8d8 2f 20 20 20 34 35 32 2c 20 20 34 35 32 2c 20 20  /   452,  452,  
2f8d9 34 35 32 2c 20 20 34 35 33 2c 20 20 34 35 34 2c  452,  453,  454,
2f8da 20 20 20 31 30 2c 20 20 35 39 38 2c 20 20 32 38     10,  598,  28
2f8db 30 2c 20 20 20 32 30 2c 20 20 20 34 36 2c 0a 20  0,   20,   46,. 
2f8dc 2f 2a 20 20 31 32 30 30 20 2a 2f 20 20 20 31 37  /*  1200 */   17
2f8dd 34 2c 20 20 34 31 32 2c 20 20 32 39 38 2c 20 20  4,  412,  298,  
2f8de 33 33 37 2c 20 20 34 32 34 2c 20 20 34 35 32 2c  337,  424,  452,
2f8df 20 20 34 35 32 2c 20 20 34 35 32 2c 20 20 34 35    452,  452,  45
2f8e0 33 2c 20 20 34 35 34 2c 0a 20 2f 2a 20 20 31 32  3,  454,. /*  12
2f8e1 31 30 20 2a 2f 20 20 20 20 31 30 2c 20 20 32 39  10 */    10,  29
2f8e2 39 2c 20 20 34 32 34 2c 20 20 20 33 35 2c 20 20  9,  424,   35,  
2f8e3 33 34 34 2c 20 20 33 35 32 2c 20 20 20 20 33 2c  344,  352,    3,
2f8e4 20 20 32 35 30 2c 20 20 33 33 34 2c 20 20 34 33    250,  334,  43
2f8e5 34 2c 0a 20 2f 2a 20 20 31 32 32 30 20 2a 2f 20  4,. /*  1220 */ 
2f8e6 20 20 33 33 33 2c 20 20 34 32 33 2c 20 20 33 33    333,  423,  33
2f8e7 37 2c 20 20 31 37 32 2c 20 20 32 38 30 2c 20 20  7,  172,  280,  
2f8e8 35 38 31 2c 20 20 32 30 38 2c 20 20 34 34 34 2c  581,  208,  444,
2f8e9 20 20 20 31 37 2c 20 20 31 37 31 2c 0a 20 2f 2a     17,  171,. /*
2f8ea 20 20 31 32 33 30 20 2a 2f 20 20 20 33 34 31 2c    1230 */   341,
2f8eb 20 20 20 31 39 2c 20 20 31 37 33 2c 20 20 34 34     19,  173,  44
2f8ec 37 2c 20 20 34 32 34 2c 20 20 34 32 32 2c 20 20  7,  424,  422,  
2f8ed 34 32 33 2c 20 20 33 33 34 2c 20 20 33 33 37 2c  423,  334,  337,
2f8ee 20 20 34 32 34 2c 0a 20 2f 2a 20 20 31 32 34 30    424,. /*  1240
2f8ef 20 2a 2f 20 20 20 32 33 35 2c 20 20 32 38 30 2c   */   235,  280,
2f8f0 20 20 32 30 34 2c 20 20 32 30 35 2c 20 20 32 30    204,  205,  20
2f8f1 36 2c 20 20 20 34 32 2c 20 20 34 34 34 2c 20 20  6,   42,  444,  
2f8f2 20 39 34 2c 20 20 33 34 36 2c 20 20 34 33 35 2c   94,  346,  435,
2f8f3 0a 20 2f 2a 20 20 31 32 35 30 20 2a 2f 20 20 20  . /*  1250 */   
2f8f4 31 33 36 2c 20 20 34 35 31 2c 20 20 32 32 31 2c  136,  451,  221,
2f8f5 20 20 33 33 34 2c 20 20 33 30 38 2c 20 20 36 32    334,  308,  62
2f8f6 34 2c 20 20 34 32 34 2c 20 20 33 34 39 2c 20 20  4,  424,  349,  
2f8f7 34 38 31 2c 20 20 34 39 30 2c 0a 20 2f 2a 20 20  481,  490,. /*  
2f8f8 31 32 36 30 20 2a 2f 20 20 20 34 34 35 2c 20 20  1260 */   445,  
2f8f9 31 35 32 2c 20 20 34 34 34 2c 20 20 20 39 35 2c  152,  444,   95,
2f8fa 20 20 34 32 34 2c 20 20 34 32 34 2c 20 20 34 32    424,  424,  42
2f8fb 34 2c 20 20 32 33 36 2c 20 20 35 30 33 2c 20 20  4,  236,  503,  
2f8fc 34 39 31 2c 0a 20 2f 2a 20 20 31 32 37 30 20 2a  491,. /*  1270 *
2f8fd 2f 20 20 20 35 30 37 2c 20 20 31 37 39 2c 20 20  /   507,  179,  
2f8fe 34 32 34 2c 20 20 34 38 31 2c 20 20 34 32 34 2c  424,  481,  424,
2f8ff 20 20 34 30 32 2c 20 20 32 39 35 2c 20 20 32 38    402,  295,  28
2f900 35 2c 20 20 20 33 38 2c 20 20 20 33 37 2c 0a 20  5,   38,   37,. 
2f901 2f 2a 20 20 31 32 38 30 20 2a 2f 20 20 20 32 37  /*  1280 */   27
2f902 31 2c 20 20 33 31 30 2c 20 20 31 35 38 2c 20 20  1,  310,  158,  
2f903 34 32 34 2c 20 20 32 39 36 2c 20 20 34 32 34 2c  424,  296,  424,
2f904 20 20 32 31 36 2c 20 20 20 33 36 2c 20 20 33 33    216,   36,  33
2f905 35 2c 20 20 33 33 36 2c 0a 20 2f 2a 20 20 31 32  5,  336,. /*  12
2f906 39 30 20 2a 2f 20 20 20 35 30 39 2c 20 20 32 36  90 */   509,  26
2f907 36 2c 20 20 34 35 30 2c 20 20 31 39 30 2c 20 20  6,  450,  190,  
2f908 31 39 31 2c 20 20 35 33 39 2c 20 20 32 36 37 2c  191,  539,  267,
2f909 20 20 36 32 35 2c 20 20 35 35 38 2c 20 20 32 37    625,  558,  27
2f90a 33 2c 0a 20 2f 2a 20 20 31 33 30 30 20 2a 2f 20  3,. /*  1300 */ 
2f90b 20 20 32 37 35 2c 20 20 20 34 38 2c 20 20 32 37    275,   48,  27
2f90c 37 2c 20 20 35 32 32 2c 20 20 32 37 39 2c 20 20  7,  522,  279,  
2f90d 34 32 34 2c 20 20 34 32 34 2c 20 20 34 35 30 2c  424,  424,  450,
2f90e 20 20 32 35 35 2c 20 20 34 30 39 2c 0a 20 2f 2a    255,  409,. /*
2f90f 20 20 31 33 31 30 20 2a 2f 20 20 20 34 32 34 2c    1310 */   424,
2f910 20 20 34 32 34 2c 20 20 32 35 37 2c 20 20 34 32    424,  257,  42
2f911 34 2c 20 20 34 32 34 2c 20 20 34 32 34 2c 20 20  4,  424,  424,  
2f912 32 38 34 2c 20 20 34 32 34 2c 20 20 33 38 36 2c  284,  424,  386,
2f913 20 20 34 32 34 2c 0a 20 2f 2a 20 20 31 33 32 30    424,. /*  1320
2f914 20 2a 2f 20 20 20 33 35 37 2c 20 20 35 38 34 2c   */   357,  584,
2f915 20 20 35 38 35 2c 20 20 34 35 32 2c 20 20 34 35    585,  452,  45
2f916 32 2c 20 20 34 35 32 2c 20 20 34 35 33 2c 20 20  2,  452,  453,  
2f917 34 35 34 2c 20 20 20 31 30 2c 20 20 32 35 39 2c  454,   10,  259,
2f918 0a 20 2f 2a 20 20 31 33 33 30 20 2a 2f 20 20 20  . /*  1330 */   
2f919 33 39 33 2c 20 20 34 32 34 2c 20 20 32 38 39 2c  393,  424,  289,
2f91a 20 20 34 32 34 2c 20 20 35 39 32 2c 20 20 36 30    424,  592,  60
2f91b 33 2c 20 20 34 32 34 2c 20 20 34 32 34 2c 20 20  3,  424,  424,  
2f91c 34 35 32 2c 20 20 34 35 32 2c 0a 20 2f 2a 20 20  452,  452,. /*  
2f91d 31 33 34 30 20 2a 2f 20 20 20 34 35 32 2c 20 20  1340 */   452,  
2f91e 32 39 37 2c 20 20 33 30 30 2c 20 20 33 30 31 2c  297,  300,  301,
2f91f 20 20 35 30 35 2c 20 20 34 32 34 2c 20 20 36 31    505,  424,  61
2f920 37 2c 20 20 34 32 34 2c 20 20 33 36 33 2c 20 20  7,  424,  363,  
2f921 34 32 34 2c 0a 20 2f 2a 20 20 31 33 35 30 20 2a  424,. /*  1350 *
2f922 2f 20 20 20 34 32 34 2c 20 20 33 37 33 2c 20 20  /   424,  373,  
2f923 35 37 37 2c 20 20 31 35 38 2c 20 20 31 35 38 2c  577,  158,  158,
2f924 20 20 35 31 31 2c 20 20 34 32 34 2c 20 20 34 32    511,  424,  42
2f925 34 2c 20 20 34 32 34 2c 20 20 35 32 35 2c 0a 20  4,  424,  525,. 
2f926 2f 2a 20 20 31 33 36 30 20 2a 2f 20 20 20 35 38  /*  1360 */   58
2f927 38 2c 20 20 34 32 34 2c 20 20 31 35 34 2c 20 20  8,  424,  154,  
2f928 35 38 39 2c 20 20 36 30 31 2c 20 20 20 35 34 2c  589,  601,   54,
2f929 20 20 20 35 34 2c 20 20 36 32 30 2c 20 20 35 31     54,  620,  51
2f92a 32 2c 20 20 33 30 36 2c 0a 20 2f 2a 20 20 31 33  2,  306,. /*  13
2f92b 37 30 20 2a 2f 20 20 20 33 31 39 2c 20 20 35 33  70 */   319,  53
2f92c 30 2c 20 20 35 33 31 2c 20 20 35 33 35 2c 20 20  0,  531,  535,  
2f92d 32 36 34 2c 20 20 31 30 37 2c 20 20 32 32 38 2c  264,  107,  228,
2f92e 20 20 35 33 36 2c 20 20 35 33 34 2c 20 20 33 37    536,  534,  37
2f92f 35 2c 0a 20 2f 2a 20 20 31 33 38 30 20 2a 2f 20  5,. /*  1380 */ 
2f930 20 20 35 35 39 2c 20 20 33 30 34 2c 20 20 35 36    559,  304,  56
2f931 30 2c 20 20 35 36 31 2c 20 20 33 30 35 2c 20 20  0,  561,  305,  
2f932 32 32 37 2c 20 20 32 32 39 2c 20 20 35 35 33 2c  227,  229,  553,
2f933 20 20 35 36 37 2c 20 20 31 36 31 2c 0a 20 2f 2a    567,  161,. /*
2f934 20 20 31 33 39 30 20 2a 2f 20 20 20 31 36 32 2c    1390 */   162,
2f935 20 20 33 37 39 2c 20 20 33 37 37 2c 20 20 31 36    379,  377,  16
2f936 33 2c 20 20 20 35 31 2c 20 20 32 30 39 2c 20 20  3,   51,  209,  
2f937 35 36 39 2c 20 20 32 38 32 2c 20 20 31 36 34 2c  569,  282,  164,
2f938 20 20 35 37 30 2c 0a 20 2f 2a 20 20 31 34 30 30    570,. /*  1400
2f939 20 2a 2f 20 20 20 33 38 35 2c 20 20 31 34 33 2c   */   385,  143,
2f93a 20 20 35 38 30 2c 20 20 31 31 36 2c 20 20 31 31    580,  116,  11
2f93b 39 2c 20 20 31 38 33 2c 20 20 34 30 30 2c 20 20  9,  183,  400,  
2f93c 35 39 30 2c 20 20 34 30 31 2c 20 20 31 32 31 2c  590,  401,  121,
2f93d 0a 20 2f 2a 20 20 31 34 31 30 20 2a 2f 20 20 20  . /*  1410 */   
2f93e 31 32 32 2c 20 20 31 32 33 2c 20 20 31 32 34 2c  122,  123,  124,
2f93f 20 20 31 32 36 2c 20 20 35 39 39 2c 20 20 33 32    126,  599,  32
2f940 38 2c 20 20 36 31 34 2c 20 20 20 35 35 2c 20 20  8,  614,   55,  
2f941 20 35 38 2c 20 20 36 31 35 2c 0a 20 2f 2a 20 20   58,  615,. /*  
2f942 31 34 32 30 20 2a 2f 20 20 20 36 31 36 2c 20 20  1420 */   616,  
2f943 36 31 39 2c 20 20 20 36 32 2c 20 20 34 31 38 2c  619,   62,  418,
2f944 20 20 31 30 33 2c 20 20 32 32 36 2c 20 20 31 31    103,  226,  11
2f945 31 2c 20 20 31 37 36 2c 20 20 32 34 32 2c 20 20  1,  176,  242,  
2f946 31 38 32 2c 0a 20 2f 2a 20 20 31 34 33 30 20 2a  182,. /*  1430 *
2f947 2f 20 20 20 34 33 37 2c 20 20 33 31 33 2c 20 20  /   437,  313,  
2f948 32 30 31 2c 20 20 33 31 34 2c 20 20 36 37 30 2c  201,  314,  670,
2f949 20 20 36 37 31 2c 20 20 36 37 32 2c 20 20 31 34    671,  672,  14
2f94a 39 2c 20 20 31 35 30 2c 20 20 34 36 37 2c 0a 20  9,  150,  467,. 
2f94b 2f 2a 20 20 31 34 34 30 20 2a 2f 20 20 20 34 36  /*  1440 */   46
2f94c 34 2c 20 20 20 33 34 2c 20 20 34 38 33 2c 20 20  4,   34,  483,  
2f94d 34 37 31 2c 20 20 34 38 30 2c 20 20 31 38 34 2c  471,  480,  184,
2f94e 20 20 31 39 37 2c 20 20 35 30 32 2c 20 20 34 38    197,  502,  48
2f94f 34 2c 20 20 20 20 35 2c 0a 20 2f 2a 20 20 31 34  4,    5,. /*  14
2f950 35 30 20 2a 2f 20 20 20 34 38 35 2c 20 20 31 35  50 */   485,  15
2f951 31 2c 20 20 34 38 39 2c 20 20 20 34 34 2c 20 20  1,  489,   44,  
2f952 31 34 31 2c 20 20 20 31 31 2c 20 20 31 30 36 2c  141,   11,  106,
2f953 20 20 31 36 30 2c 20 20 32 32 35 2c 20 20 35 31    160,  225,  51
2f954 38 2c 0a 20 2f 2a 20 20 31 34 36 30 20 2a 2f 20  8,. /*  1460 */ 
2f955 20 20 35 31 39 2c 20 20 20 34 39 2c 20 20 35 31    519,   49,  51
2f956 30 2c 20 20 31 30 38 2c 20 20 33 36 37 2c 20 20  0,  108,  367,  
2f957 32 37 30 2c 20 20 20 31 32 2c 20 20 31 35 35 2c  270,   12,  155,
2f958 20 20 31 30 39 2c 20 20 20 35 30 2c 0a 20 2f 2a    109,   50,. /*
2f959 20 20 31 34 37 30 20 2a 2f 20 20 20 31 31 30 2c    1470 */   110,
2f95a 20 20 32 36 32 2c 20 20 33 37 36 2c 20 20 31 38    262,  376,  18
2f95b 36 2c 20 20 35 36 38 2c 20 20 31 31 33 2c 20 20  6,  568,  113,  
2f95c 31 34 32 2c 20 20 31 35 34 2c 20 20 31 36 35 2c  142,  154,  165,
2f95d 20 20 31 31 35 2c 0a 20 2f 2a 20 20 31 34 38 30    115,. /*  1480
2f95e 20 2a 2f 20 20 20 20 31 35 2c 20 20 32 38 33 2c   */    15,  283,
2f95f 20 20 35 38 33 2c 20 20 31 36 36 2c 20 20 31 36    583,  166,  16
2f960 37 2c 20 20 33 38 30 2c 20 20 35 38 36 2c 20 20  7,  380,  586,  
2f961 31 31 37 2c 20 20 20 31 33 2c 20 20 31 32 30 2c  117,   13,  120,
2f962 0a 20 2f 2a 20 20 31 34 39 30 20 2a 2f 20 20 20  . /*  1490 */   
2f963 33 37 32 2c 20 20 20 35 32 2c 20 20 20 35 33 2c  372,   52,   53,
2f964 20 20 31 31 38 2c 20 20 35 39 31 2c 20 20 31 36    118,  591,  16
2f965 39 2c 20 20 31 31 34 2c 20 20 31 37 30 2c 20 20  9,  114,  170,  
2f966 35 39 35 2c 20 20 31 32 35 2c 0a 20 2f 2a 20 20  595,  125,. /*  
2f967 31 35 30 30 20 2a 2f 20 20 20 31 32 37 2c 20 20  1500 */   127,  
2f968 35 37 31 2c 20 20 35 37 35 2c 20 20 36 30 32 2c  571,  575,  602,
2f969 20 20 20 31 34 2c 20 20 31 32 38 2c 20 20 36 31     14,  128,  61
2f96a 31 2c 20 20 36 31 32 2c 20 20 20 36 31 2c 20 20  1,  612,   61,  
2f96b 31 37 35 2c 0a 20 2f 2a 20 20 31 35 31 30 20 2a  175,. /*  1510 *
2f96c 2f 20 20 20 31 38 39 2c 20 20 34 31 35 2c 20 20  /   189,  415,  
2f96d 33 30 32 2c 20 20 36 32 37 2c 20 20 39 36 30 2c  302,  627,  960,
2f96e 20 20 39 36 30 2c 20 20 39 36 30 2c 20 20 39 36    960,  960,  96
2f96f 30 2c 20 20 34 31 31 2c 0a 7d 3b 0a 73 74 61 74  0,  411,.};.stat
2f970 69 63 20 63 6f 6e 73 74 20 59 59 43 4f 44 45 54  ic const YYCODET
2f971 59 50 45 20 79 79 5f 6c 6f 6f 6b 61 68 65 61 64  YPE yy_lookahead
2f972 5b 5d 20 3d 20 7b 0a 20 2f 2a 20 20 20 20 20 30  [] = {. /*     0
2f973 20 2a 2f 20 20 20 20 31 39 2c 20 20 31 34 32 2c   */    19,  142,
2f974 20 20 31 34 33 2c 20 20 31 34 34 2c 20 20 31 34    143,  144,  14
2f975 35 2c 20 20 20 32 34 2c 20 20 31 31 36 2c 20 20  5,   24,  116,  
2f976 20 32 36 2c 20 20 20 37 35 2c 20 20 20 37 36 2c   26,   75,   76,
2f977 0a 20 2f 2a 20 20 20 20 31 30 20 2a 2f 20 20 20  . /*    10 */   
2f978 20 37 37 2c 20 20 20 37 38 2c 20 20 20 32 35 2c   77,   78,   25,
2f979 20 20 20 38 30 2c 20 20 20 38 31 2c 20 20 20 38     80,   81,   8
2f97a 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20  2,   83,   84,  
2f97b 20 38 35 2c 20 20 20 38 36 2c 0a 20 2f 2a 20 20   85,   86,. /*  
2f97c 20 20 32 30 20 2a 2f 20 20 20 20 38 37 2c 20 20    20 */    87,  
2f97d 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c   88,   89,   90,
2f97e 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20 31 36     26,   27,  16
2f97f 30 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20  0,   26,   27,  
2f980 20 34 38 2c 0a 20 2f 2a 20 20 20 20 33 30 20 2a   48,. /*    30 *
2f981 2f 20 20 20 20 34 39 2c 20 20 20 37 39 2c 20 20  /    49,   79,  
2f982 20 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c   80,   81,   82,
2f983 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38     83,   84,   8
2f984 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 0a 20  5,   86,   87,. 
2f985 2f 2a 20 20 20 20 34 30 20 2a 2f 20 20 20 20 38  /*    40 */    8
2f986 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20  8,   89,   90,  
2f987 32 32 32 2c 20 20 32 32 33 2c 20 20 32 32 34 2c  222,  223,  224,
2f988 20 20 32 32 35 2c 20 20 20 36 36 2c 20 20 20 36    225,   66,   6
2f989 37 2c 20 20 20 36 38 2c 0a 20 2f 2a 20 20 20 20  7,   68,. /*    
2f98a 35 30 20 2a 2f 20 20 20 20 36 39 2c 20 20 20 37  50 */    69,   7
2f98b 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20  0,   71,   72,  
2f98c 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37 35 2c   73,   74,   75,
2f98d 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37     76,   77,   7
2f98e 38 2c 0a 20 2f 2a 20 20 20 20 36 30 20 2a 2f 20  8,. /*    60 */ 
2f98f 20 20 31 39 34 2c 20 20 20 38 30 2c 20 20 20 38    194,   80,   8
2f990 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20  1,   82,   83,  
2f991 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c   84,   85,   86,
2f992 20 20 20 38 37 2c 20 20 20 38 38 2c 0a 20 2f 2a     87,   88,. /*
2f993 20 20 20 20 37 30 20 2a 2f 20 20 20 20 38 39 2c      70 */    89,
2f994 20 20 20 39 30 2c 20 20 20 31 39 2c 20 20 20 39     90,   19,   9
2f995 30 2c 20 20 20 31 39 2c 20 20 20 39 34 2c 20 20  0,   19,   94,  
2f996 31 37 34 2c 20 20 20 32 35 2c 20 20 20 32 35 2c  174,   25,   25,
2f997 20 20 20 38 30 2c 0a 20 2f 2a 20 20 20 20 38 30     80,. /*    80
2f998 20 2a 2f 20 20 20 20 38 31 2c 20 20 20 38 32 2c   */    81,   82,
2f999 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38     83,   84,   8
2f99a 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20  5,   86,   87,  
2f99b 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c   88,   89,   90,
2f99c 0a 20 2f 2a 20 20 20 20 39 30 20 2a 2f 20 20 20  . /*    90 */   
2f99d 20 32 36 2c 20 20 20 32 37 2c 20 20 20 39 34 2c   26,   27,   94,
2f99e 20 20 20 39 35 2c 20 20 20 39 36 2c 20 20 20 39     95,   96,   9
2f99f 34 2c 20 20 20 39 35 2c 20 20 20 39 39 2c 20 20  4,   95,   99,  
2f9a0 31 30 30 2c 20 20 31 30 31 2c 0a 20 2f 2a 20 20  100,  101,. /*  
2f9a1 20 31 30 30 20 2a 2f 20 20 20 20 31 39 2c 20 20   100 */    19,  
2f9a2 20 34 38 2c 20 20 20 34 39 2c 20 20 31 35 30 2c   48,   49,  150,
2f9a3 20 20 31 37 34 2c 20 20 20 35 32 2c 20 20 31 31    174,   52,  11
2f9a4 39 2c 20 20 31 36 36 2c 20 20 31 31 30 2c 20 20  9,  166,  110,  
2f9a5 20 38 34 2c 0a 20 2f 2a 20 20 20 31 31 30 20 2a   84,. /*   110 *
2f9a6 2f 20 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20  /    85,   86,  
2f9a7 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c   87,   88,   89,
2f9a8 20 20 20 39 30 2c 20 20 20 32 36 2c 20 20 20 32     90,   26,   2
2f9a9 37 2c 20 20 31 36 35 2c 20 20 20 36 36 2c 0a 20  7,  165,   66,. 
2f9aa 2f 2a 20 20 20 31 32 30 20 2a 2f 20 20 20 20 36  /*   120 */    6
2f9ab 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20  7,   68,   69,  
2f9ac 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c   70,   71,   72,
2f9ad 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37     73,   74,   7
2f9ae 35 2c 20 20 20 37 36 2c 0a 20 2f 2a 20 20 20 31  5,   76,. /*   1
2f9af 33 30 20 2a 2f 20 20 20 20 37 37 2c 20 20 20 37  30 */    77,   7
2f9b0 38 2c 20 20 31 38 36 2c 20 20 20 38 30 2c 20 20  8,  186,   80,  
2f9b1 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c   81,   82,   83,
2f9b2 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38     84,   85,   8
2f9b3 36 2c 0a 20 2f 2a 20 20 20 31 34 30 20 2a 2f 20  6,. /*   140 */ 
2f9b4 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38     87,   88,   8
2f9b5 39 2c 20 20 20 39 30 2c 20 20 20 31 39 2c 20 20  9,   90,   19,  
2f9b6 20 39 30 2c 20 20 32 30 35 2c 20 20 20 39 35 2c   90,  205,   95,
2f9b7 20 20 20 38 34 2c 20 20 20 38 35 2c 0a 20 2f 2a     84,   85,. /*
2f9b8 20 20 20 31 35 30 20 2a 2f 20 20 20 31 38 36 2c     150 */   186,
2f9b9 20 20 20 39 36 2c 20 20 20 39 37 2c 20 20 20 39     96,   97,   9
2f9ba 38 2c 20 20 20 39 39 2c 20 20 31 30 30 2c 20 20  8,   99,  100,  
2f9bb 31 30 31 2c 20 20 31 30 32 2c 20 20 20 39 34 2c  101,  102,   94,
2f9bc 20 20 20 39 35 2c 0a 20 2f 2a 20 20 20 31 36 30     95,. /*   160
2f9bd 20 2a 2f 20 20 20 31 39 35 2c 20 20 20 39 37 2c   */   195,   97,
2f9be 20 20 31 35 30 2c 20 20 32 32 32 2c 20 20 31 30    150,  222,  10
2f9bf 39 2c 20 20 32 32 34 2c 20 20 32 32 35 2c 20 20  9,  224,  225,  
2f9c0 20 32 36 2c 20 20 31 30 34 2c 20 20 31 30 35 2c   26,  104,  105,
2f9c1 0a 20 2f 2a 20 20 20 31 37 30 20 2a 2f 20 20 20  . /*   170 */   
2f9c2 32 31 37 2c 20 20 20 39 30 2c 20 20 31 32 30 2c  217,   90,  120,
2f9c3 20 20 20 34 38 2c 20 20 20 34 39 2c 20 20 20 35     48,   49,   5
2f9c4 30 2c 20 20 20 38 36 2c 20 20 31 36 35 2c 20 20  0,   86,  165,  
2f9c5 20 39 37 2c 20 20 20 39 38 2c 0a 20 2f 2a 20 20   97,   98,. /*  
2f9c6 20 31 38 30 20 2a 2f 20 20 20 20 39 39 2c 20 20   180 */    99,  
2f9c7 31 30 30 2c 20 20 31 30 31 2c 20 20 31 30 32 2c  100,  101,  102,
2f9c8 20 20 20 39 34 2c 20 20 20 39 35 2c 20 20 31 37     94,   95,  17
2f9c9 34 2c 20 20 31 37 35 2c 20 20 20 20 31 2c 20 20  4,  175,    1,  
2f9ca 20 20 32 2c 0a 20 2f 2a 20 20 20 31 39 30 20 2a    2,. /*   190 *
2f9cb 2f 20 20 20 31 30 39 2c 20 20 20 36 36 2c 20 20  /   109,   66,  
2f9cc 20 36 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c   67,   68,   69,
2f9cd 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37     70,   71,   7
2f9ce 32 2c 20 20 20 37 33 2c 20 20 20 37 34 2c 0a 20  2,   73,   74,. 
2f9cf 2f 2a 20 20 20 32 30 30 20 2a 2f 20 20 20 20 37  /*   200 */    7
2f9d0 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20  5,   76,   77,  
2f9d1 20 37 38 2c 20 20 31 39 31 2c 20 20 20 38 30 2c   78,  191,   80,
2f9d2 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38     81,   82,   8
2f9d3 33 2c 20 20 20 38 34 2c 0a 20 2f 2a 20 20 20 32  3,   84,. /*   2
2f9d4 31 30 20 2a 2f 20 20 20 20 38 35 2c 20 20 20 38  10 */    85,   8
2f9d5 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20  6,   87,   88,  
2f9d6 20 38 39 2c 20 20 20 39 30 2c 20 20 20 31 39 2c   89,   90,   19,
2f9d7 20 20 31 31 36 2c 20 20 20 33 35 2c 20 20 31 35    116,   35,  15
2f9d8 30 2c 0a 20 2f 2a 20 20 20 32 32 30 20 2a 2f 20  0,. /*   220 */ 
2f9d9 20 20 20 31 32 2c 20 20 20 32 34 2c 20 20 32 30     12,   24,  20
2f9da 38 2c 20 20 31 35 30 2c 20 20 32 32 32 2c 20 20  8,  150,  222,  
2f9db 31 35 30 2c 20 20 32 32 34 2c 20 20 32 32 35 2c  150,  224,  225,
2f9dc 20 20 32 31 36 2c 20 20 31 32 38 2c 0a 20 2f 2a    216,  128,. /*
2f9dd 20 20 20 32 33 30 20 2a 2f 20 20 20 31 36 31 2c     230 */   161,
2f9de 20 20 31 36 32 2c 20 20 31 35 30 2c 20 20 32 32    162,  150,  22
2f9df 31 2c 20 20 31 36 35 2c 20 20 20 39 34 2c 20 20  1,  165,   94,  
2f9e0 20 32 38 2c 20 20 31 35 30 2c 20 20 31 36 35 2c   28,  150,  165,
2f9e1 20 20 20 35 36 2c 0a 20 2f 2a 20 20 20 32 34 30     56,. /*   240
2f9e2 20 2a 2f 20 20 20 31 36 35 2c 20 20 31 39 37 2c   */   165,  197,
2f9e3 20 20 31 36 30 2c 20 20 31 37 30 2c 20 20 31 37    160,  170,  17
2f9e4 31 2c 20 20 20 34 38 2c 20 20 20 34 39 2c 20 20  1,   48,   49,  
2f9e5 31 36 35 2c 20 20 32 30 34 2c 20 20 31 37 34 2c  165,  204,  174,
2f9e6 0a 20 2f 2a 20 20 20 32 35 30 20 2a 2f 20 20 20  . /*   250 */   
2f9e7 31 37 35 2c 20 20 20 34 33 2c 20 20 31 36 35 2c  175,   43,  165,
2f9e8 20 20 20 34 35 2c 20 20 31 38 35 2c 20 20 31 38     45,  185,  18
2f9e9 36 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20  6,  174,  175,  
2f9ea 31 36 39 2c 20 20 31 37 30 2c 0a 20 2f 2a 20 20  169,  170,. /*  
2f9eb 20 32 36 30 20 2a 2f 20 20 20 31 37 31 2c 20 20   260 */   171,  
2f9ec 31 37 34 2c 20 20 31 37 35 2c 20 20 20 36 36 2c  174,  175,   66,
2f9ed 20 20 20 36 37 2c 20 20 20 36 38 2c 20 20 20 36     67,   68,   6
2f9ee 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20  9,   70,   71,  
2f9ef 20 37 32 2c 0a 20 2f 2a 20 20 20 32 37 30 20 2a   72,. /*   270 *
2f9f0 2f 20 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20  /    73,   74,  
2f9f1 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c   75,   76,   77,
2f9f2 20 20 20 37 38 2c 20 20 31 39 34 2c 20 20 20 38     78,  194,   8
2f9f3 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c 0a 20  0,   81,   82,. 
2f9f4 2f 2a 20 20 20 32 38 30 20 2a 2f 20 20 20 20 38  /*   280 */    8
2f9f5 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20  3,   84,   85,  
2f9f6 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c   86,   87,   88,
2f9f7 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20 20 31     89,   90,   1
2f9f8 39 2c 20 20 32 31 34 2c 0a 20 2f 2a 20 20 20 32  9,  214,. /*   2
2f9f9 39 30 20 2a 2f 20 20 20 32 31 35 2c 20 20 31 30  90 */   215,  10
2f9fa 38 2c 20 20 31 35 30 2c 20 20 20 32 35 2c 20 20  8,  150,   25,  
2f9fb 31 34 38 2c 20 20 31 35 30 2c 20 20 20 36 34 2c  148,  150,   64,
2f9fc 20 20 20 32 32 2c 20 20 32 31 36 2c 20 20 20 32     22,  216,   2
2f9fd 34 2c 0a 20 2f 2a 20 20 20 33 30 30 20 2a 2f 20  4,. /*   300 */ 
2f9fe 20 20 31 34 36 2c 20 20 31 34 37 2c 20 20 32 31    146,  147,  21
2f9ff 35 2c 20 20 32 32 31 2c 20 20 32 33 31 2c 20 20  5,  221,  231,  
2fa00 32 33 32 2c 20 20 31 35 32 2c 20 20 31 36 35 2c  232,  152,  165,
2fa01 20 20 31 35 34 2c 20 20 31 35 30 2c 0a 20 2f 2a    154,  150,. /*
2fa02 20 20 20 33 31 30 20 2a 2f 20 20 20 31 36 35 2c     310 */   165,
2fa03 20 20 20 34 39 2c 20 20 31 37 30 2c 20 20 31 37     49,  170,  17
2fa04 31 2c 20 20 31 36 30 2c 20 20 31 38 31 2c 20 20  1,  160,  181,  
2fa05 31 38 32 2c 20 20 20 34 38 2c 20 20 20 34 39 2c  182,   48,   49,
2fa06 20 20 31 37 34 2c 0a 20 2f 2a 20 20 20 33 32 30    174,. /*   320
2fa07 20 2a 2f 20 20 20 31 37 35 2c 20 20 32 33 32 2c   */   175,  232,
2fa08 20 20 31 38 38 2c 20 20 31 36 35 2c 20 20 31 36    188,  165,  16
2fa09 35 2c 20 20 20 32 31 2c 20 20 20 39 34 2c 20 20  5,   21,   94,  
2fa0a 20 20 30 2c 20 20 20 20 31 2c 20 20 20 20 32 2c    0,    1,    2,
2fa0b 0a 20 2f 2a 20 20 20 33 33 30 20 2a 2f 20 20 20  . /*   330 */   
2fa0c 20 39 38 2c 20 20 20 35 35 2c 20 20 31 37 34 2c   98,   55,  174,
2fa0d 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20 36    174,  175,   6
2fa0e 36 2c 20 20 20 36 37 2c 20 20 20 36 38 2c 20 20  6,   67,   68,  
2fa0f 20 36 39 2c 20 20 20 37 30 2c 0a 20 2f 2a 20 20   69,   70,. /*  
2fa10 20 33 34 30 20 2a 2f 20 20 20 20 37 31 2c 20 20   340 */    71,  
2fa11 20 37 32 2c 20 20 20 37 33 2c 20 20 20 37 34 2c   72,   73,   74,
2fa12 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37     75,   76,   7
2fa13 37 2c 20 20 20 37 38 2c 20 20 31 39 34 2c 20 20  7,   78,  194,  
2fa14 20 38 30 2c 0a 20 2f 2a 20 20 20 33 35 30 20 2a   80,. /*   350 *
2fa15 2f 20 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20  /    81,   82,  
2fa16 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c   83,   84,   85,
2fa17 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38     86,   87,   8
2fa18 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 0a 20  8,   89,   90,. 
2fa19 2f 2a 20 20 20 33 36 30 20 2a 2f 20 20 20 20 31  /*   360 */    1
2fa1a 39 2c 20 20 31 32 39 2c 20 20 31 33 30 2c 20 20  9,  129,  130,  
2fa1b 31 33 31 2c 20 20 20 39 36 2c 20 20 20 36 31 2c  131,   96,   61,
2fa1c 20 20 31 30 34 2c 20 20 20 39 39 2c 20 20 31 30    104,   99,  10
2fa1d 30 2c 20 20 31 30 31 2c 0a 20 2f 2a 20 20 20 33  0,  101,. /*   3
2fa1e 37 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 32 32  70 */   150,  22
2fa1f 36 2c 20 20 32 31 38 2c 20 20 32 33 31 2c 20 20  6,  218,  231,  
2fa20 32 33 32 2c 20 20 32 31 36 2c 20 20 31 35 30 2c  232,  216,  150,
2fa21 20 20 31 39 36 2c 20 20 31 31 30 2c 20 20 20 32    196,  110,   2
2fa22 36 2c 0a 20 2f 2a 20 20 20 33 38 30 20 2a 2f 20  6,. /*   380 */ 
2fa23 20 20 20 32 37 2c 20 20 31 30 35 2c 20 20 31 30     27,  105,  10
2fa24 36 2c 20 20 31 30 37 2c 20 20 31 35 38 2c 20 20  6,  107,  158,  
2fa25 31 36 35 2c 20 20 31 38 33 2c 20 20 31 36 31 2c  165,  183,  161,
2fa26 20 20 31 36 32 2c 20 20 20 34 38 2c 0a 20 2f 2a    162,   48,. /*
2fa27 20 20 20 33 39 30 20 2a 2f 20 20 20 20 34 39 2c     390 */    49,
2fa28 20 20 31 36 35 2c 20 20 31 31 36 2c 20 20 31 36    165,  116,  16
2fa29 36 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20  6,  174,  175,  
2fa2a 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c   86,   87,   88,
2fa2b 20 20 20 38 39 2c 0a 20 2f 2a 20 20 20 34 30 30     89,. /*   400
2fa2c 20 2a 2f 20 20 20 20 39 30 2c 20 20 32 34 37 2c   */    90,  247,
2fa2d 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 31 30     84,   85,  10
2fa2e 30 2c 20 20 31 33 36 2c 20 20 31 38 33 2c 20 20  0,  136,  183,  
2fa2f 20 36 36 2c 20 20 20 36 37 2c 20 20 20 36 38 2c   66,   67,   68,
2fa30 0a 20 2f 2a 20 20 20 34 31 30 20 2a 2f 20 20 20  . /*   410 */   
2fa31 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c   69,   70,   71,
2fa32 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20 20 37     72,   73,   7
2fa33 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20  4,   75,   76,  
2fa34 20 37 37 2c 20 20 20 37 38 2c 0a 20 2f 2a 20 20   77,   78,. /*  
2fa35 20 34 32 30 20 2a 2f 20 20 20 20 31 31 2c 20 20   420 */    11,  
2fa36 20 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c   80,   81,   82,
2fa37 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38     83,   84,   8
2fa38 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20  5,   86,   87,  
2fa39 20 38 38 2c 0a 20 2f 2a 20 20 20 34 33 30 20 2a   88,. /*   430 *
2fa3a 2f 20 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20  /    89,   90,  
2fa3b 20 31 39 2c 20 20 31 38 33 2c 20 20 31 35 30 2c   19,  183,  150,
2fa3c 20 20 32 32 32 2c 20 20 20 32 33 2c 20 20 32 32    222,   23,  22
2fa3d 34 2c 20 20 32 32 35 2c 20 20 32 33 37 2c 0a 20  4,  225,  237,. 
2fa3e 2f 2a 20 20 20 34 34 30 20 2a 2f 20 20 20 32 32  /*   440 */   22
2fa3f 30 2c 20 20 31 35 30 2c 20 20 31 33 38 2c 20 20  0,  150,  138,  
2fa40 20 32 34 2c 20 20 31 36 30 2c 20 20 20 32 36 2c   24,  160,   26,
2fa41 20 20 20 32 32 2c 20 20 20 39 34 2c 20 20 20 39     22,   94,   9
2fa42 35 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20 20 34  5,  165,. /*   4
2fa43 35 30 20 2a 2f 20 20 20 20 32 36 2c 20 20 20 32  50 */    26,   2
2fa44 37 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20  7,   26,   27,  
2fa45 20 38 39 2c 20 20 20 39 30 2c 20 20 31 36 35 2c   89,   90,  165,
2fa46 20 20 32 34 34 2c 20 20 31 37 34 2c 20 20 31 37    244,  174,  17
2fa47 35 2c 0a 20 2f 2a 20 20 20 34 36 30 20 2a 2f 20  5,. /*   460 */ 
2fa48 20 20 32 33 36 2c 20 20 20 34 38 2c 20 20 20 34    236,   48,   4
2fa49 39 2c 20 20 20 32 32 2c 20 20 20 35 35 2c 20 20  9,   22,   55,  
2fa4a 31 37 34 2c 20 20 31 37 35 2c 20 20 20 32 36 2c  174,  175,   26,
2fa4b 20 20 20 32 37 2c 20 20 20 32 32 2c 0a 20 2f 2a     27,   22,. /*
2fa4c 20 20 20 34 37 30 20 2a 2f 20 20 20 20 32 33 2c     470 */    23,
2fa4d 20 20 31 36 33 2c 20 20 20 32 35 2c 20 20 31 32    163,   25,  12
2fa4e 30 2c 20 20 31 36 36 2c 20 20 31 36 37 2c 20 20  0,  166,  167,  
2fa4f 31 36 38 2c 20 20 31 33 36 2c 20 20 31 39 34 2c  168,  136,  194,
2fa50 20 20 20 36 36 2c 0a 20 2f 2a 20 20 20 34 38 30     66,. /*   480
2fa51 20 2a 2f 20 20 20 20 36 37 2c 20 20 20 36 38 2c   */    67,   68,
2fa52 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37     69,   70,   7
2fa53 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20  1,   72,   73,  
2fa54 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c   74,   75,   76,
2fa55 0a 20 2f 2a 20 20 20 34 39 30 20 2a 2f 20 20 20  . /*   490 */   
2fa56 20 37 37 2c 20 20 20 37 38 2c 20 20 31 35 33 2c   77,   78,  153,
2fa57 20 20 20 38 30 2c 20 20 20 38 31 2c 20 20 20 38     80,   81,   8
2fa58 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20  2,   83,   84,  
2fa59 20 38 35 2c 20 20 20 38 36 2c 0a 20 2f 2a 20 20   85,   86,. /*  
2fa5a 20 35 30 30 20 2a 2f 20 20 20 20 38 37 2c 20 20   500 */    87,  
2fa5b 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c   88,   89,   90,
2fa5c 20 20 20 31 39 2c 20 20 31 39 36 2c 20 20 31 36     19,  196,  16
2fa5d 30 2c 20 20 31 35 30 2c 20 20 20 32 33 2c 20 20  0,  150,   23,  
2fa5e 31 37 33 2c 0a 20 2f 2a 20 20 20 35 31 30 20 2a  173,. /*   510 *
2fa5f 2f 20 20 20 31 39 38 2c 20 20 32 32 30 2c 20 20  /   198,  220,  
2fa60 20 36 35 2c 20 20 20 39 34 2c 20 20 31 30 35 2c   65,   94,  105,
2fa61 20 20 31 30 36 2c 20 20 31 30 37 2c 20 20 31 38    106,  107,  18
2fa62 31 2c 20 20 20 39 34 2c 20 20 20 39 35 2c 0a 20  1,   94,   95,. 
2fa63 2f 2a 20 20 20 35 32 30 20 2a 2f 20 20 20 20 39  /*   520 */    9
2fa64 34 2c 20 20 20 39 35 2c 20 20 31 36 35 2c 20 20  4,   95,  165,  
2fa65 20 39 36 2c 20 20 31 35 30 2c 20 20 31 31 36 2c   96,  150,  116,
2fa66 20 20 20 39 39 2c 20 20 31 30 30 2c 20 20 31 30     99,  100,  10
2fa67 31 2c 20 20 20 33 31 2c 0a 20 2f 2a 20 20 20 35  1,   31,. /*   5
2fa68 33 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 31 37  30 */   150,  17
2fa69 34 2c 20 20 31 37 35 2c 20 20 20 34 38 2c 20 20  4,  175,   48,  
2fa6a 20 34 39 2c 20 20 20 39 34 2c 20 20 20 39 35 2c   49,   94,   95,
2fa6b 20 20 31 31 30 2c 20 20 20 34 30 2c 20 20 31 36    110,   40,  16
2fa6c 35 2c 0a 20 2f 2a 20 20 20 35 34 30 20 2a 2f 20  5,. /*   540 */ 
2fa6d 20 20 31 39 34 2c 20 20 31 36 31 2c 20 20 31 36    194,  161,  16
2fa6e 32 2c 20 20 31 36 36 2c 20 20 31 36 30 2c 20 20  2,  166,  160,  
2fa6f 31 36 35 2c 20 20 31 32 30 2c 20 20 31 36 36 2c  165,  120,  166,
2fa70 20 20 31 37 34 2c 20 20 31 37 35 2c 0a 20 2f 2a    174,  175,. /*
2fa71 20 20 20 35 35 30 20 2a 2f 20 20 20 32 33 33 2c     550 */   233,
2fa72 20 20 20 36 36 2c 20 20 20 36 37 2c 20 20 20 36     66,   67,   6
2fa73 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20  8,   69,   70,  
2fa74 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c   71,   72,   73,
2fa75 20 20 20 37 34 2c 0a 20 2f 2a 20 20 20 35 36 30     74,. /*   560
2fa76 20 2a 2f 20 20 20 20 37 35 2c 20 20 20 37 36 2c   */    75,   76,
2fa77 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20 32 31     77,   78,  21
2fa78 38 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 20 20  8,   80,   81,  
2fa79 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c   82,   83,   84,
2fa7a 0a 20 2f 2a 20 20 20 35 37 30 20 2a 2f 20 20 20  . /*   570 */   
2fa7b 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c   85,   86,   87,
2fa7c 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39     88,   89,   9
2fa7d 30 2c 20 20 20 31 39 2c 20 20 20 31 32 2c 20 20  0,   19,   12,  
2fa7e 31 39 34 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20  194,  150,. /*  
2fa7f 20 35 38 30 20 2a 2f 20 20 20 20 32 33 2c 20 20   580 */    23,  
2fa80 32 33 35 2c 20 20 32 30 35 2c 20 20 31 36 36 2c  235,  205,  166,
2fa81 20 20 31 36 37 2c 20 20 31 36 38 2c 20 20 32 34    167,  168,  24
2fa82 30 2c 20 20 31 35 30 2c 20 20 31 37 32 2c 20 20  0,  150,  172,  
2fa83 31 37 33 2c 0a 20 2f 2a 20 20 20 35 39 30 20 2a  173,. /*   590 *
2fa84 2f 20 20 20 32 30 36 2c 20 20 32 30 37 2c 20 20  /   206,  207,  
2fa85 31 35 30 2c 20 20 20 32 38 2c 20 20 31 36 35 2c  150,   28,  165,
2fa86 20 20 31 39 30 2c 20 20 31 39 31 2c 20 20 31 38    190,  191,  18
2fa87 31 2c 20 20 20 32 33 2c 20 20 31 35 30 2c 0a 20  1,   23,  150,. 
2fa88 2f 2a 20 20 20 36 30 30 20 2a 2f 20 20 20 20 34  /*   600 */    4
2fa89 38 2c 20 20 20 34 39 2c 20 20 31 36 35 2c 20 20  8,   49,  165,  
2fa8a 31 37 34 2c 20 20 31 37 35 2c 20 20 20 34 38 2c  174,  175,   48,
2fa8b 20 20 20 34 39 2c 20 20 31 36 35 2c 20 20 20 34     49,  165,   4
2fa8c 33 2c 20 20 32 33 33 2c 0a 20 2f 2a 20 20 20 36  3,  233,. /*   6
2fa8d 31 30 20 2a 2f 20 20 20 20 34 35 2c 20 20 31 37  10 */    45,  17
2fa8e 34 2c 20 20 31 37 35 2c 20 20 31 33 35 2c 20 20  4,  175,  135,  
2fa8f 31 36 35 2c 20 20 31 33 37 2c 20 20 31 37 34 2c  165,  137,  174,
2fa90 20 20 31 37 35 2c 20 20 31 39 30 2c 20 20 31 39    175,  190,  19
2fa91 31 2c 0a 20 2f 2a 20 20 20 36 32 30 20 2a 2f 20  1,. /*   620 */ 
2fa92 20 20 20 35 35 2c 20 20 20 36 39 2c 20 20 20 37     55,   69,   7
2fa93 30 2c 20 20 20 36 36 2c 20 20 20 36 37 2c 20 20  0,   66,   67,  
2fa94 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c   68,   69,   70,
2fa95 20 20 20 37 31 2c 20 20 20 37 32 2c 0a 20 2f 2a     71,   72,. /*
2fa96 20 20 20 36 33 30 20 2a 2f 20 20 20 20 37 33 2c     630 */    73,
2fa97 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37     74,   75,   7
2fa98 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20  6,   77,   78,  
2fa99 31 35 30 2c 20 20 20 38 30 2c 20 20 20 38 31 2c  150,   80,   81,
2fa9a 20 20 20 38 32 2c 0a 20 2f 2a 20 20 20 36 34 30     82,. /*   640
2fa9b 20 2a 2f 20 20 20 20 38 33 2c 20 20 20 38 34 2c   */    83,   84,
2fa9c 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38     85,   86,   8
2fa9d 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20  7,   88,   89,  
2fa9e 20 39 30 2c 20 20 20 31 39 2c 20 20 31 35 30 2c   90,   19,  150,
2fa9f 0a 20 2f 2a 20 20 20 36 35 30 20 2a 2f 20 20 20  . /*   650 */   
2faa0 20 39 38 2c 20 20 31 36 35 2c 20 20 20 32 33 2c   98,  165,   23,
2faa1 20 20 32 35 30 2c 20 20 32 35 31 2c 20 20 31 35    250,  251,  15
2faa2 30 2c 20 20 31 35 35 2c 20 20 31 35 30 2c 20 20  0,  155,  150,  
2faa3 20 31 39 2c 20 20 20 32 32 2c 0a 20 2f 2a 20 20   19,   22,. /*  
2faa4 20 36 36 30 20 2a 2f 20 20 20 31 37 34 2c 20 20   660 */   174,  
2faa5 31 37 35 2c 20 20 32 31 33 2c 20 20 20 32 36 2c  175,  213,   26,
2faa6 20 20 31 36 35 2c 20 20 31 36 39 2c 20 20 31 37    165,  169,  17
2faa7 30 2c 20 20 31 37 31 2c 20 20 20 32 33 2c 20 20  0,  171,   23,  
2faa8 31 35 30 2c 0a 20 2f 2a 20 20 20 36 37 30 20 2a  150,. /*   670 *
2faa9 2f 20 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20  /   165,  150,  
2faaa 31 36 35 2c 20 20 31 37 34 2c 20 20 31 37 35 2c  165,  174,  175,
2faab 20 20 20 31 39 2c 20 20 31 35 30 2c 20 20 20 34     19,  150,   4
2faac 38 2c 20 20 20 34 39 2c 20 20 31 37 34 2c 0a 20  8,   49,  174,. 
2faad 2f 2a 20 20 20 36 38 30 20 2a 2f 20 20 20 31 37  /*   680 */   17
2faae 35 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20  5,  174,  175,  
2faaf 20 32 37 2c 20 20 31 36 35 2c 20 20 32 32 38 2c   27,  165,  228,
2fab0 20 20 31 36 35 2c 20 20 31 31 32 2c 20 20 20 34    165,  112,   4
2fab1 39 2c 20 20 31 31 34 2c 0a 20 2f 2a 20 20 20 36  9,  114,. /*   6
2fab2 39 30 20 2a 2f 20 20 20 31 31 35 2c 20 20 31 36  90 */   115,  16
2fab3 35 2c 20 20 31 37 37 2c 20 20 31 37 34 2c 20 20  5,  177,  174,  
2fab4 31 37 35 2c 20 20 20 36 36 2c 20 20 20 36 37 2c  175,   66,   67,
2fab5 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37     68,   69,   7
2fab6 30 2c 0a 20 2f 2a 20 20 20 37 30 30 20 2a 2f 20  0,. /*   700 */ 
2fab7 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37     71,   72,   7
2fab8 33 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20  3,   74,   75,  
2fab9 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c   76,   77,   78,
2faba 20 20 31 35 30 2c 20 20 20 38 30 2c 0a 20 2f 2a    150,   80,. /*
2fabb 20 20 20 37 31 30 20 2a 2f 20 20 20 20 38 31 2c     710 */    81,
2fabc 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38     82,   83,   8
2fabd 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20  4,   85,   86,  
2fabe 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c   87,   88,   89,
2fabf 20 20 20 39 30 2c 0a 20 2f 2a 20 20 20 37 32 30     90,. /*   720
2fac0 20 2a 2f 20 20 20 20 31 39 2c 20 20 31 35 30 2c   */    19,  150,
2fac1 20 20 20 32 33 2c 20 20 31 36 35 2c 20 20 20 32     23,  165,   2
2fac2 35 2c 20 20 20 32 34 2c 20 20 31 35 30 2c 20 20  5,   24,  150,  
2fac3 31 35 30 2c 20 20 32 33 32 2c 20 20 31 35 30 2c  150,  232,  150,
2fac4 0a 20 2f 2a 20 20 20 37 33 30 20 2a 2f 20 20 20  . /*   730 */   
2fac5 32 32 39 2c 20 20 20 39 34 2c 20 20 31 37 34 2c  229,   94,  174,
2fac6 20 20 31 37 35 2c 20 20 32 31 33 2c 20 20 32 33    175,  213,  23
2fac7 34 2c 20 20 31 36 35 2c 20 20 20 32 35 2c 20 20  4,  165,   25,  
2fac8 31 35 30 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20  150,  150,. /*  
2fac9 20 37 34 30 20 2a 2f 20 20 20 31 35 30 2c 20 20   740 */   150,  
2faca 31 36 35 2c 20 20 31 36 35 2c 20 20 31 30 34 2c  165,  165,  104,
2facb 20 20 31 36 35 2c 20 20 31 37 34 2c 20 20 31 37    165,  174,  17
2facc 35 2c 20 20 31 37 37 2c 20 20 31 30 39 2c 20 20  5,  177,  109,  
2facd 20 34 38 2c 0a 20 2f 2a 20 20 20 37 35 30 20 2a   48,. /*   750 *
2face 2f 20 20 20 20 34 39 2c 20 20 31 37 34 2c 20 20  /    49,  174,  
2facf 31 37 35 2c 20 20 31 36 35 2c 20 20 31 36 35 2c  175,  165,  165,
2fad0 20 20 31 36 35 2c 20 20 20 31 39 2c 20 20 31 31    165,   19,  11
2fad1 32 2c 20 20 20 32 32 2c 20 20 31 31 34 2c 0a 20  2,   22,  114,. 
2fad2 2f 2a 20 20 20 37 36 30 20 2a 2f 20 20 20 31 31  /*   760 */   11
2fad3 35 2c 20 20 31 37 37 2c 20 20 31 37 34 2c 20 20  5,  177,  174,  
2fad4 31 37 35 2c 20 20 20 32 37 2c 20 20 20 31 36 2c  175,   27,   16,
2fad5 20 20 31 38 37 2c 20 20 20 36 36 2c 20 20 20 36    187,   66,   6
2fad6 37 2c 20 20 20 36 38 2c 0a 20 2f 2a 20 20 20 37  7,   68,. /*   7
2fad7 37 30 20 2a 2f 20 20 20 20 36 39 2c 20 20 20 37  70 */    69,   7
2fad8 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20  0,   71,   72,  
2fad9 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37 35 2c   73,   74,   75,
2fada 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37     76,   77,   7
2fadb 38 2c 0a 20 2f 2a 20 20 20 37 38 30 20 2a 2f 20  8,. /*   780 */ 
2fadc 20 20 31 35 30 2c 20 20 20 38 30 2c 20 20 20 38    150,   80,   8
2fadd 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20  1,   82,   83,  
2fade 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c   84,   85,   86,
2fadf 20 20 20 38 37 2c 20 20 20 38 38 2c 0a 20 2f 2a     87,   88,. /*
2fae0 20 20 20 37 39 30 20 2a 2f 20 20 20 20 38 39 2c     790 */    89,
2fae1 20 20 20 39 30 2c 20 20 20 31 39 2c 20 20 31 35     90,   19,  15
2fae2 30 2c 20 20 20 39 35 2c 20 20 31 36 35 2c 20 20  0,   95,  165,  
2fae3 31 34 34 2c 20 20 31 34 35 2c 20 20 31 35 30 2c  144,  145,  150,
2fae4 20 20 31 35 30 2c 0a 20 2f 2a 20 20 20 38 30 30    150,. /*   800
2fae5 20 2a 2f 20 20 20 31 35 30 2c 20 20 31 35 30 2c   */   150,  150,
2fae6 20 20 31 31 33 2c 20 20 32 31 33 2c 20 20 31 37    113,  213,  17
2fae7 34 2c 20 20 31 37 35 2c 20 20 31 31 37 2c 20 20  4,  175,  117,  
2fae8 20 35 38 2c 20 20 31 36 35 2c 20 20 20 36 30 2c   58,  165,   60,
2fae9 0a 20 2f 2a 20 20 20 38 31 30 20 2a 2f 20 20 20  . /*   810 */   
2faea 20 37 34 2c 20 20 20 32 33 2c 20 20 31 35 30 2c   74,   23,  150,
2faeb 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 36    165,  165,  16
2faec 35 2c 20 20 31 36 35 2c 20 20 31 37 34 2c 20 20  5,  165,  174,  
2faed 31 37 35 2c 20 20 31 32 30 2c 0a 20 2f 2a 20 20  175,  120,. /*  
2faee 20 38 32 30 20 2a 2f 20 20 20 20 31 39 2c 20 20   820 */    19,  
2faef 20 34 38 2c 20 20 20 34 39 2c 20 20 31 37 34 2c   48,   49,  174,
2faf0 20 20 31 37 35 2c 20 20 31 37 34 2c 20 20 31 37    175,  174,  17
2faf1 35 2c 20 20 31 36 35 2c 20 20 32 30 39 2c 20 20  5,  165,  209,  
2faf2 31 36 36 2c 0a 20 2f 2a 20 20 20 38 33 30 20 2a  166,. /*   830 *
2faf3 2f 20 20 20 32 34 31 2c 20 20 20 32 32 2c 20 20  /   241,   22,  
2faf4 20 32 33 2c 20 20 31 36 36 2c 20 20 20 32 35 2c   23,  166,   25,
2faf5 20 20 31 38 37 2c 20 20 31 37 34 2c 20 20 31 37    187,  174,  17
2faf6 35 2c 20 20 31 32 36 2c 20 20 20 36 36 2c 0a 20  5,  126,   66,. 
2faf7 2f 2a 20 20 20 38 34 30 20 2a 2f 20 20 20 20 36  /*   840 */    6
2faf8 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20  7,   68,   69,  
2faf9 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c   70,   71,   72,
2fafa 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37     73,   74,   7
2fafb 35 2c 20 20 20 37 36 2c 0a 20 2f 2a 20 20 20 38  5,   76,. /*   8
2fafc 35 30 20 2a 2f 20 20 20 20 37 37 2c 20 20 20 37  50 */    77,   7
2fafd 38 2c 20 20 31 35 30 2c 20 20 20 38 30 2c 20 20  8,  150,   80,  
2fafe 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c   81,   82,   83,
2faff 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38     84,   85,   8
2fb00 36 2c 0a 20 2f 2a 20 20 20 38 36 30 20 2a 2f 20  6,. /*   860 */ 
2fb01 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38     87,   88,   8
2fb02 39 2c 20 20 20 39 30 2c 20 20 20 31 39 2c 20 20  9,   90,   19,  
2fb03 31 35 30 2c 20 20 31 35 30 2c 20 20 31 36 35 2c  150,  150,  165,
2fb04 20 20 32 30 35 2c 20 20 31 38 32 2c 0a 20 2f 2a    205,  182,. /*
2fb05 20 20 20 38 37 30 20 2a 2f 20 20 20 20 38 36 2c     870 */    86,
2fb06 20 20 31 35 30 2c 20 20 32 30 35 2c 20 20 31 35    150,  205,  15
2fb07 30 2c 20 20 20 36 35 2c 20 20 31 36 36 2c 20 20  0,   65,  166,  
2fb08 31 37 34 2c 20 20 31 37 35 2c 20 20 32 30 36 2c  174,  175,  206,
2fb09 20 20 32 30 37 2c 0a 20 2f 2a 20 20 20 38 38 30    207,. /*   880
2fb0a 20 2a 2f 20 20 20 31 36 35 2c 20 20 31 36 35 2c   */   165,  165,
2fb0b 20 20 31 37 37 2c 20 20 31 35 30 2c 20 20 20 32    177,  150,   2
2fb0c 33 2c 20 20 20 32 35 2c 20 20 31 36 35 2c 20 20  3,   25,  165,  
2fb0d 31 33 38 2c 20 20 31 36 35 2c 20 20 31 37 34 2c  138,  165,  174,
2fb0e 0a 20 2f 2a 20 20 20 38 39 30 20 2a 2f 20 20 20  . /*   890 */   
2fb0f 31 37 35 2c 20 20 32 34 31 2c 20 20 31 36 36 2c  175,  241,  166,
2fb10 20 20 20 34 38 2c 20 20 20 34 39 2c 20 20 31 37     48,   49,  17
2fb11 34 2c 20 20 31 37 35 2c 20 20 31 31 33 2c 20 20  4,  175,  113,  
2fb12 31 36 35 2c 20 20 20 39 38 2c 0a 20 2f 2a 20 20  165,   98,. /*  
2fb13 20 39 30 30 20 2a 2f 20 20 20 31 31 32 2c 20 20   900 */   112,  
2fb14 31 31 37 2c 20 20 31 31 34 2c 20 20 31 31 35 2c  117,  114,  115,
2fb15 20 20 20 20 37 2c 20 20 20 20 38 2c 20 20 20 20      7,    8,    
2fb16 39 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20  9,  174,  175,  
2fb17 31 39 33 2c 0a 20 2f 2a 20 20 20 39 31 30 20 2a  193,. /*   910 *
2fb18 2f 20 20 20 31 38 37 2c 20 20 20 36 36 2c 20 20  /   187,   66,  
2fb19 20 36 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c   67,   68,   69,
2fb1a 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37     70,   71,   7
2fb1b 32 2c 20 20 20 37 33 2c 20 20 20 37 34 2c 0a 20  2,   73,   74,. 
2fb1c 2f 2a 20 20 20 39 32 30 20 2a 2f 20 20 20 20 37  /*   920 */    7
2fb1d 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20  5,   76,   77,  
2fb1e 20 37 38 2c 20 20 31 35 30 2c 20 20 20 38 30 2c   78,  150,   80,
2fb1f 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38     81,   82,   8
2fb20 33 2c 20 20 20 38 34 2c 0a 20 2f 2a 20 20 20 39  3,   84,. /*   9
2fb21 33 30 20 2a 2f 20 20 20 20 38 35 2c 20 20 20 38  30 */    85,   8
2fb22 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20  6,   87,   88,  
2fb23 20 38 39 2c 20 20 20 39 30 2c 20 20 20 31 39 2c   89,   90,   19,
2fb24 20 20 31 35 30 2c 20 20 20 39 37 2c 20 20 31 36    150,   97,  16
2fb25 35 2c 0a 20 2f 2a 20 20 20 39 34 30 20 2a 2f 20  5,. /*   940 */ 
2fb26 20 20 31 36 30 2c 20 20 31 35 30 2c 20 20 31 37    160,  150,  17
2fb27 37 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20  7,  150,  150,  
2fb28 31 35 30 2c 20 20 32 34 38 2c 20 20 32 34 39 2c  150,  248,  249,
2fb29 20 20 31 37 34 2c 20 20 31 37 35 2c 0a 20 2f 2a    174,  175,. /*
2fb2a 20 20 20 39 35 30 20 2a 2f 20 20 20 20 39 37 2c     950 */    97,
2fb2b 20 20 20 39 38 2c 20 20 31 36 35 2c 20 20 31 32     98,  165,  12
2fb2c 39 2c 20 20 31 33 30 2c 20 20 31 35 30 2c 20 20  9,  130,  150,  
2fb2d 31 36 35 2c 20 20 31 31 36 2c 20 20 31 36 35 2c  165,  116,  165,
2fb2e 20 20 31 36 35 2c 0a 20 2f 2a 20 20 20 39 36 30    165,. /*   960
2fb2f 20 2a 2f 20 20 20 31 36 35 2c 20 20 31 37 34 2c   */   165,  174,
2fb30 20 20 31 37 35 2c 20 20 31 30 33 2c 20 20 31 37    175,  103,  17
2fb31 38 2c 20 20 20 34 38 2c 20 20 20 34 39 2c 20 20  8,   48,   49,  
2fb32 31 37 34 2c 20 20 31 37 35 2c 20 20 31 32 38 2c  174,  175,  128,
2fb33 0a 20 2f 2a 20 20 20 39 37 30 20 2a 2f 20 20 20  . /*   970 */   
2fb34 31 36 35 2c 20 20 20 39 38 2c 20 20 32 34 32 2c  165,   98,  242,
2fb35 20 20 31 31 32 2c 20 20 31 39 34 2c 20 20 31 31    112,  194,  11
2fb36 34 2c 20 20 31 31 35 2c 20 20 31 39 39 2c 20 20  4,  115,  199,  
2fb37 31 38 37 2c 20 20 31 37 34 2c 0a 20 2f 2a 20 20  187,  174,. /*  
2fb38 20 39 38 30 20 2a 2f 20 20 20 31 37 35 2c 20 20   980 */   175,  
2fb39 31 38 37 2c 20 20 31 30 39 2c 20 20 32 34 32 2c  187,  109,  242,
2fb3a 20 20 20 36 37 2c 20 20 20 36 38 2c 20 20 20 36     67,   68,   6
2fb3b 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20  9,   70,   71,  
2fb3c 20 37 32 2c 0a 20 2f 2a 20 20 20 39 39 30 20 2a   72,. /*   990 *
2fb3d 2f 20 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20  /    73,   74,  
2fb3e 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c   75,   76,   77,
2fb3f 20 20 20 37 38 2c 20 20 31 35 30 2c 20 20 20 38     78,  150,   8
2fb40 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c 0a 20  0,   81,   82,. 
2fb41 2f 2a 20 20 31 30 30 30 20 2a 2f 20 20 20 20 38  /*  1000 */    8
2fb42 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20  3,   84,   85,  
2fb43 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c   86,   87,   88,
2fb44 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20 20 31     89,   90,   1
2fb45 39 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20 31 30  9,  150,. /*  10
2fb46 31 30 20 2a 2f 20 20 20 31 36 30 2c 20 20 31 36  10 */   160,  16
2fb47 35 2c 20 20 32 30 39 2c 20 20 31 35 30 2c 20 20  5,  209,  150,  
2fb48 31 31 32 2c 20 20 31 35 30 2c 20 20 31 31 34 2c  112,  150,  114,
2fb49 20 20 31 31 35 2c 20 20 20 20 37 2c 20 20 20 20    115,    7,    
2fb4a 38 2c 0a 20 2f 2a 20 20 31 30 32 30 20 2a 2f 20  8,. /*  1020 */ 
2fb4b 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 32 30    174,  175,  20
2fb4c 39 2c 20 20 20 20 36 2c 20 20 31 36 35 2c 20 20  9,    6,  165,  
2fb4d 20 32 39 2c 20 20 31 39 39 2c 20 20 31 35 30 2c   29,  199,  150,
2fb4e 20 20 31 36 35 2c 20 20 20 33 33 2c 0a 20 2f 2a    165,   33,. /*
2fb4f 20 20 31 30 33 30 20 2a 2f 20 20 20 31 36 35 2c    1030 */   165,
2fb50 20 20 31 35 30 2c 20 20 31 34 39 2c 20 20 31 37    150,  149,  17
2fb51 34 2c 20 20 31 37 35 2c 20 20 31 35 30 2c 20 20  4,  175,  150,  
2fb52 32 34 31 2c 20 20 20 34 38 2c 20 20 20 34 39 2c  241,   48,   49,
2fb53 20 20 31 37 34 2c 0a 20 2f 2a 20 20 31 30 34 30    174,. /*  1040
2fb54 20 2a 2f 20 20 20 31 37 35 2c 20 20 31 34 39 2c   */   175,  149,
2fb55 20 20 31 36 35 2c 20 20 20 34 37 2c 20 20 31 39    165,   47,  19
2fb56 34 2c 20 20 31 34 39 2c 20 20 31 36 35 2c 20 20  4,  149,  165,  
2fb57 20 31 36 2c 20 20 31 36 30 2c 20 20 31 34 39 2c   16,  160,  149,
2fb58 0a 20 2f 2a 20 20 31 30 35 30 20 2a 2f 20 20 20  . /*  1050 */   
2fb59 31 36 35 2c 20 20 31 37 34 2c 20 20 31 37 35 2c  165,  174,  175,
2fb5a 20 20 20 31 33 2c 20 20 31 35 31 2c 20 20 31 37     13,  151,  17
2fb5b 34 2c 20 20 31 37 35 2c 20 20 20 36 38 2c 20 20  4,  175,   68,  
2fb5c 20 36 39 2c 20 20 20 37 30 2c 0a 20 2f 2a 20 20   69,   70,. /*  
2fb5d 31 30 36 30 20 2a 2f 20 20 20 20 37 31 2c 20 20  1060 */    71,  
2fb5e 20 37 32 2c 20 20 20 37 33 2c 20 20 20 37 34 2c   72,   73,   74,
2fb5f 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37     75,   76,   7
2fb60 37 2c 20 20 20 37 38 2c 20 20 32 31 38 2c 20 20  7,   78,  218,  
2fb61 20 38 30 2c 0a 20 2f 2a 20 20 31 30 37 30 20 2a   80,. /*  1070 *
2fb62 2f 20 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20  /    81,   82,  
2fb63 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c   83,   84,   85,
2fb64 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38     86,   87,   8
2fb65 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 0a 20  8,   89,   90,. 
2fb66 2f 2a 20 20 31 30 38 30 20 2a 2f 20 20 20 20 31  /*  1080 */    1
2fb67 39 2c 20 20 20 32 30 2c 20 20 31 39 34 2c 20 20  9,   20,  194,  
2fb68 20 32 32 2c 20 20 31 35 30 2c 20 20 31 35 30 2c   22,  150,  150,
2fb69 20 20 31 35 30 2c 20 20 20 32 36 2c 20 20 20 32    150,   26,   2
2fb6a 37 2c 20 20 20 35 38 2c 0a 20 2f 2a 20 20 31 30  7,   58,. /*  10
2fb6b 39 30 20 2a 2f 20 20 20 32 34 30 2c 20 20 20 36  90 */   240,   6
2fb6c 30 2c 20 20 31 35 30 2c 20 20 31 36 30 2c 20 20  0,  150,  160,  
2fb6d 31 35 31 2c 20 20 20 31 39 2c 20 20 20 32 30 2c  151,   19,   20,
2fb6e 20 20 20 33 36 2c 20 20 20 32 32 2c 20 20 31 36     36,   22,  16
2fb6f 35 2c 0a 20 2f 2a 20 20 31 31 30 30 20 2a 2f 20  5,. /*  1100 */ 
2fb70 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20 20 32    165,  165,   2
2fb71 36 2c 20 20 20 32 37 2c 20 20 20 32 32 2c 20 20  6,   27,   22,  
2fb72 20 32 33 2c 20 20 31 35 30 2c 20 20 31 36 35 2c   23,  150,  165,
2fb73 20 20 31 37 34 2c 20 20 31 37 35 2c 0a 20 2f 2a    174,  175,. /*
2fb74 20 20 31 31 31 30 20 2a 2f 20 20 20 31 37 34 2c    1110 */   174,
2fb75 20 20 31 37 35 2c 20 20 20 33 36 2c 20 20 31 35    175,   36,  15
2fb76 30 2c 20 20 20 32 35 2c 20 20 20 35 34 2c 20 20  0,   25,   54,  
2fb77 31 35 30 2c 20 20 31 35 30 2c 20 20 31 35 30 2c  150,  150,  150,
2fb78 20 20 31 35 30 2c 0a 20 2f 2a 20 20 31 31 32 30    150,. /*  1120
2fb79 20 2a 2f 20 20 20 20 32 33 2c 20 20 31 36 35 2c   */    23,  165,
2fb7a 20 20 20 32 35 2c 20 20 31 35 39 2c 20 20 31 35     25,  159,  15
2fb7b 30 2c 20 20 20 36 34 2c 20 20 31 39 34 2c 20 20  0,   64,  194,  
2fb7c 31 39 34 2c 20 20 31 36 35 2c 20 20 31 39 39 2c  194,  165,  199,
2fb7d 0a 20 2f 2a 20 20 31 31 33 30 20 2a 2f 20 20 20  . /*  1130 */   
2fb7e 20 35 34 2c 20 20 31 36 35 2c 20 20 31 36 35 2c   54,  165,  165,
2fb7f 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 39    165,  165,  19
2fb80 33 2c 20 20 31 35 30 2c 20 20 31 37 34 2c 20 20  3,  150,  174,  
2fb81 31 37 35 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20  175,  165,. /*  
2fb82 31 31 34 30 20 2a 2f 20 20 20 20 36 34 2c 20 20  1140 */    64,  
2fb83 31 37 34 2c 20 20 31 37 35 2c 20 20 31 37 34 2c  174,  175,  174,
2fb84 20 20 31 37 35 2c 20 20 20 38 34 2c 20 20 20 38    175,   84,   8
2fb85 35 2c 20 20 20 36 35 2c 20 20 31 35 30 2c 20 20  5,   65,  150,  
2fb86 31 39 33 2c 0a 20 2f 2a 20 20 31 31 35 30 20 2a  193,. /*  1150 *
2fb87 2f 20 20 20 31 32 36 2c 20 20 31 36 35 2c 20 20  /   126,  165,  
2fb88 32 31 37 2c 20 20 31 35 30 2c 20 20 20 39 33 2c  217,  150,   93,
2fb89 20 20 20 39 34 2c 20 20 20 39 35 2c 20 20 31 32     94,   95,  12
2fb8a 33 2c 20 20 32 30 30 2c 20 20 20 39 38 2c 0a 20  3,  200,   98,. 
2fb8b 2f 2a 20 20 31 31 36 30 20 2a 2f 20 20 20 20 38  /*  1160 */    8
2fb8c 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20  4,   85,   86,  
2fb8d 31 36 35 2c 20 20 31 30 35 2c 20 20 31 30 36 2c  165,  105,  106,
2fb8e 20 20 31 30 37 2c 20 20 31 39 33 2c 20 20 31 36    107,  193,  16
2fb8f 35 2c 20 20 20 39 33 2c 0a 20 2f 2a 20 20 31 31  5,   93,. /*  11
2fb90 37 30 20 2a 2f 20 20 20 20 39 34 2c 20 20 20 39  70 */    94,   9
2fb91 35 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20  5,  174,  175,  
2fb92 20 39 38 2c 20 20 20 20 35 2c 20 20 20 32 33 2c   98,    5,   23,
2fb93 20 20 31 31 36 2c 20 20 20 32 35 2c 20 20 31 39    116,   25,  19
2fb94 33 2c 0a 20 2f 2a 20 20 31 31 38 30 20 2a 2f 20  3,. /*  1180 */ 
2fb95 20 20 20 31 30 2c 20 20 20 31 31 2c 20 20 20 31     10,   11,   1
2fb96 32 2c 20 20 20 31 33 2c 20 20 20 31 34 2c 20 20  2,   13,   14,  
2fb97 32 30 31 2c 20 20 20 32 33 2c 20 20 20 31 37 2c  201,   23,   17,
2fb98 20 20 20 32 35 2c 20 20 31 35 30 2c 0a 20 2f 2a     25,  150,. /*
2fb99 20 20 31 31 39 30 20 2a 2f 20 20 20 31 32 39 2c    1190 */   129,
2fb9a 20 20 31 33 30 2c 20 20 31 33 31 2c 20 20 31 33    130,  131,  13
2fb9b 32 2c 20 20 31 33 33 2c 20 20 31 33 34 2c 20 20  2,  133,  134,  
2fb9c 31 39 33 2c 20 20 31 35 30 2c 20 20 31 32 35 2c  193,  150,  125,
2fb9d 20 20 31 32 34 2c 0a 20 2f 2a 20 20 31 32 30 30    124,. /*  1200
2fb9e 20 2a 2f 20 20 20 20 33 30 2c 20 20 32 34 35 2c   */    30,  245,
2fb9f 20 20 20 33 32 2c 20 20 31 35 30 2c 20 20 31 36     32,  150,  16
2fba0 35 2c 20 20 31 32 39 2c 20 20 31 33 30 2c 20 20  5,  129,  130,  
2fba1 31 33 31 2c 20 20 31 33 32 2c 20 20 31 33 33 2c  131,  132,  133,
2fba2 0a 20 2f 2a 20 20 31 32 31 30 20 2a 2f 20 20 20  . /*  1210 */   
2fba3 31 33 34 2c 20 20 20 34 31 2c 20 20 31 36 35 2c  134,   41,  165,
2fba4 20 20 20 31 39 2c 20 20 20 32 30 2c 20 20 31 32     19,   20,  12
2fba5 32 2c 20 20 20 32 32 2c 20 20 32 30 32 2c 20 20  2,   22,  202,  
2fba6 31 36 35 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20  165,  150,. /*  
2fba7 31 32 32 30 20 2a 2f 20 20 20 20 32 36 2c 20 20  1220 */    26,  
2fba8 20 32 37 2c 20 20 31 35 30 2c 20 20 20 35 33 2c   27,  150,   53,
2fba9 20 20 31 35 30 2c 20 20 20 35 35 2c 20 20 31 36    150,   55,  16
2fbaa 30 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20  0,  174,  175,  
2fbab 20 35 39 2c 0a 20 2f 2a 20 20 31 32 33 30 20 2a   59,. /*  1230 *
2fbac 2f 20 20 20 20 33 36 2c 20 20 20 32 32 2c 20 20  /    36,   22,  
2fbad 20 36 32 2c 20 20 32 30 33 2c 20 20 31 36 35 2c   62,  203,  165,
2fbae 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20 31 36     26,   27,  16
2fbaf 35 2c 20 20 31 35 30 2c 20 20 31 36 35 2c 0a 20  5,  150,  165,. 
2fbb0 2f 2a 20 20 31 32 34 30 20 2a 2f 20 20 20 31 39  /*  1240 */   19
2fbb1 33 2c 20 20 31 35 30 2c 20 20 31 30 35 2c 20 20  3,  150,  105,  
2fbb2 31 30 36 2c 20 20 31 30 37 2c 20 20 31 33 35 2c  106,  107,  135,
2fbb3 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20 35    174,  175,   5
2fbb4 34 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20 31 32  4,  150,. /*  12
2fbb5 35 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 31 35  50 */   150,  15
2fbb6 30 2c 20 20 32 32 37 2c 20 20 31 36 35 2c 20 20  0,  227,  165,  
2fbb7 20 32 32 2c 20 20 20 32 33 2c 20 20 31 36 35 2c   22,   23,  165,
2fbb8 20 20 31 35 30 2c 20 20 20 36 34 2c 20 20 31 35    150,   64,  15
2fbb9 30 2c 0a 20 2f 2a 20 20 31 32 36 30 20 2a 2f 20  0,. /*  1260 */ 
2fbba 20 20 31 39 34 2c 20 20 31 31 38 2c 20 20 31 37    194,  118,  17
2fbbb 34 2c 20 20 31 37 35 2c 20 20 31 36 35 2c 20 20  4,  175,  165,  
2fbbc 31 36 35 2c 20 20 31 36 35 2c 20 20 31 39 33 2c  165,  165,  193,
2fbbd 20 20 31 35 30 2c 20 20 31 35 37 2c 0a 20 2f 2a    150,  157,. /*
2fbbe 20 20 31 32 37 30 20 2a 2f 20 20 20 31 35 30 2c    1270 */   150,
2fbbf 20 20 31 35 37 2c 20 20 31 36 35 2c 20 20 20 36    157,  165,   6
2fbc0 34 2c 20 20 31 36 35 2c 20 20 31 30 35 2c 20 20  4,  165,  105,  
2fbc1 31 30 36 2c 20 20 31 30 37 2c 20 20 20 38 34 2c  106,  107,   84,
2fbc2 20 20 20 38 35 2c 0a 20 2f 2a 20 20 31 32 38 30     85,. /*  1280
2fbc3 20 2a 2f 20 20 20 20 32 33 2c 20 20 31 31 31 2c   */    23,  111,
2fbc4 20 20 20 32 35 2c 20 20 31 36 35 2c 20 20 31 39     25,  165,  19
2fbc5 33 2c 20 20 31 36 35 2c 20 20 31 31 36 2c 20 20  3,  165,  116,  
2fbc6 20 39 33 2c 20 20 20 39 34 2c 20 20 20 39 35 2c   93,   94,   95,
2fbc7 0a 20 2f 2a 20 20 31 32 39 30 20 2a 2f 20 20 20  . /*  1290 */   
2fbc8 31 35 30 2c 20 20 31 35 30 2c 20 20 20 39 38 2c  150,  150,   98,
2fbc9 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 31 35     84,   85,  15
2fbca 30 2c 20 20 31 35 30 2c 20 20 20 36 35 2c 20 20  0,  150,   65,  
2fbcb 31 35 30 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20  150,  150,. /*  
2fbcc 31 33 30 30 20 2a 2f 20 20 20 31 35 30 2c 20 20  1300 */   150,  
2fbcd 31 30 34 2c 20 20 31 35 30 2c 20 20 20 39 34 2c  104,  150,   94,
2fbce 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20 31 36    150,  165,  16
2fbcf 35 2c 20 20 20 39 38 2c 20 20 32 31 30 2c 20 20  5,   98,  210,  
2fbd0 31 33 39 2c 0a 20 2f 2a 20 20 31 33 31 30 20 2a  139,. /*  1310 *
2fbd1 2f 20 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20  /   165,  165,  
2fbd2 32 31 30 2c 20 20 31 36 35 2c 20 20 31 36 35 2c  210,  165,  165,
2fbd3 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 31 36    165,  150,  16
2fbd4 35 2c 20 20 31 35 30 2c 20 20 31 36 35 2c 0a 20  5,  150,  165,. 
2fbd5 2f 2a 20 20 31 33 32 30 20 2a 2f 20 20 20 31 32  /*  1320 */   12
2fbd6 31 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20  1,  150,  150,  
2fbd7 31 32 39 2c 20 20 31 33 30 2c 20 20 31 33 31 2c  129,  130,  131,
2fbd8 20 20 31 33 32 2c 20 20 31 33 33 2c 20 20 31 33    132,  133,  13
2fbd9 34 2c 20 20 32 31 30 2c 0a 20 2f 2a 20 20 31 33  4,  210,. /*  13
2fbda 33 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 31 36  30 */   150,  16
2fbdb 35 2c 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20  5,  150,  165,  
2fbdc 31 35 30 2c 20 20 31 35 30 2c 20 20 31 36 35 2c  150,  150,  165,
2fbdd 20 20 31 36 35 2c 20 20 31 32 39 2c 20 20 31 33    165,  129,  13
2fbde 30 2c 0a 20 2f 2a 20 20 31 33 34 30 20 2a 2f 20  0,. /*  1340 */ 
2fbdf 20 20 31 33 31 2c 20 20 31 35 30 2c 20 20 31 35    131,  150,  15
2fbe0 30 2c 20 20 31 35 30 2c 20 20 32 31 31 2c 20 20  0,  150,  211,  
2fbe1 31 36 35 2c 20 20 31 35 30 2c 20 20 31 36 35 2c  165,  150,  165,
2fbe2 20 20 31 30 34 2c 20 20 31 36 35 2c 0a 20 2f 2a    104,  165,. /*
2fbe3 20 20 31 33 35 30 20 2a 2f 20 20 20 31 36 35 2c    1350 */   165,
2fbe4 20 20 20 32 33 2c 20 20 20 32 33 2c 20 20 20 32     23,   23,   2
2fbe5 35 2c 20 20 20 32 35 2c 20 20 32 31 31 2c 20 20  5,   25,  211,  
2fbe6 31 36 35 2c 20 20 31 36 35 2c 20 20 31 36 35 2c  165,  165,  165,
2fbe7 20 20 31 37 36 2c 0a 20 2f 2a 20 20 31 33 36 30    176,. /*  1360
2fbe8 20 2a 2f 20 20 20 20 32 33 2c 20 20 31 36 35 2c   */    23,  165,
2fbe9 20 20 20 32 35 2c 20 20 20 32 33 2c 20 20 20 32     25,   23,   2
2fbea 33 2c 20 20 20 32 35 2c 20 20 20 32 35 2c 20 20  3,   25,   25,  
2fbeb 20 32 33 2c 20 20 32 31 31 2c 20 20 20 32 35 2c   23,  211,   25,
2fbec 0a 20 2f 2a 20 20 31 33 37 30 20 2a 2f 20 20 20  . /*  1370 */   
2fbed 20 34 36 2c 20 20 31 37 36 2c 20 20 31 38 34 2c   46,  176,  184,
2fbee 20 20 31 30 33 2c 20 20 31 37 36 2c 20 20 20 32    103,  176,   2
2fbef 32 2c 20 20 20 39 30 2c 20 20 31 37 36 2c 20 20  2,   90,  176,  
2fbf0 31 37 38 2c 20 20 20 31 38 2c 0a 20 2f 2a 20 20  178,   18,. /*  
2fbf1 31 33 38 30 20 2a 2f 20 20 20 31 37 36 2c 20 20  1380 */   176,  
2fbf2 31 37 39 2c 20 20 31 37 36 2c 20 20 31 37 36 2c  179,  176,  176,
2fbf3 20 20 31 37 39 2c 20 20 32 33 30 2c 20 20 32 33    179,  230,  23
2fbf4 30 2c 20 20 31 38 34 2c 20 20 31 35 37 2c 20 20  0,  184,  157,  
2fbf5 31 35 36 2c 0a 20 2f 2a 20 20 31 33 39 30 20 2a  156,. /*  1390 *
2fbf6 2f 20 20 20 31 35 36 2c 20 20 20 34 34 2c 20 20  /   156,   44,  
2fbf7 31 35 37 2c 20 20 31 35 36 2c 20 20 31 33 35 2c  157,  156,  135,
2fbf8 20 20 31 35 37 2c 20 20 31 35 37 2c 20 20 32 33    157,  157,  23
2fbf9 38 2c 20 20 31 35 36 2c 20 20 32 33 39 2c 0a 20  8,  156,  239,. 
2fbfa 2f 2a 20 20 31 34 30 30 20 2a 2f 20 20 20 31 35  /*  1400 */   15
2fbfb 37 2c 20 20 20 36 36 2c 20 20 31 38 39 2c 20 20  7,   66,  189,  
2fbfc 31 38 39 2c 20 20 20 32 32 2c 20 20 32 31 39 2c  189,   22,  219,
2fbfd 20 20 31 35 37 2c 20 20 31 39 39 2c 20 20 20 31    157,  199,   1
2fbfe 38 2c 20 20 31 39 32 2c 0a 20 2f 2a 20 20 31 34  8,  192,. /*  14
2fbff 31 30 20 2a 2f 20 20 20 31 39 32 2c 20 20 31 39  10 */   192,  19
2fc00 32 2c 20 20 31 39 32 2c 20 20 31 38 39 2c 20 20  2,  192,  189,  
2fc01 31 39 39 2c 20 20 31 35 37 2c 20 20 20 33 39 2c  199,  157,   39,
2fc02 20 20 32 34 33 2c 20 20 32 34 33 2c 20 20 31 35    243,  243,  15
2fc03 37 2c 0a 20 2f 2a 20 20 31 34 32 30 20 2a 2f 20  7,. /*  1420 */ 
2fc04 20 20 31 35 37 2c 20 20 20 33 37 2c 20 20 32 34    157,   37,  24
2fc05 36 2c 20 20 20 20 31 2c 20 20 31 36 34 2c 20 20  6,    1,  164,  
2fc06 31 38 30 2c 20 20 31 38 30 2c 20 20 32 34 39 2c  180,  180,  249,
2fc07 20 20 20 31 35 2c 20 20 32 31 39 2c 0a 20 2f 2a     15,  219,. /*
2fc08 20 20 31 34 33 30 20 2a 2f 20 20 20 20 32 33 2c    1430 */    23,
2fc09 20 20 32 35 32 2c 20 20 20 32 32 2c 20 20 32 35    252,   22,  25
2fc0a 32 2c 20 20 31 31 38 2c 20 20 31 31 38 2c 20 20  2,  118,  118,  
2fc0b 31 31 38 2c 20 20 31 31 38 2c 20 20 31 31 38 2c  118,  118,  118,
2fc0c 20 20 31 31 33 2c 0a 20 2f 2a 20 20 31 34 34 30    113,. /*  1440
2fc0d 20 2a 2f 20 20 20 20 39 38 2c 20 20 20 32 32 2c   */    98,   22,
2fc0e 20 20 20 31 31 2c 20 20 20 32 33 2c 20 20 20 32     11,   23,   2
2fc0f 33 2c 20 20 20 32 32 2c 20 20 20 32 32 2c 20 20  3,   22,   22,  
2fc10 31 32 30 2c 20 20 20 32 33 2c 20 20 20 33 34 2c  120,   23,   34,
2fc11 0a 20 2f 2a 20 20 31 34 35 30 20 2a 2f 20 20 20  . /*  1450 */   
2fc12 20 32 33 2c 20 20 20 32 35 2c 20 20 20 32 33 2c   23,   25,   23,
2fc13 20 20 20 32 35 2c 20 20 31 31 38 2c 20 20 20 32     25,  118,   2
2fc14 35 2c 20 20 20 32 32 2c 20 20 31 30 32 2c 20 20  5,   22,  102,  
2fc15 20 35 30 2c 20 20 20 32 33 2c 0a 20 2f 2a 20 20   50,   23,. /*  
2fc16 31 34 36 30 20 2a 2f 20 20 20 20 32 33 2c 20 20  1460 */    23,  
2fc17 20 32 32 2c 20 20 20 32 37 2c 20 20 20 32 32 2c   22,   27,   22,
2fc18 20 20 20 35 30 2c 20 20 20 32 33 2c 20 20 20 33     50,   23,   3
2fc19 34 2c 20 20 20 33 34 2c 20 20 20 32 32 2c 20 20  4,   34,   22,  
2fc1a 20 32 32 2c 0a 20 2f 2a 20 20 31 34 37 30 20 2a   22,. /*  1470 *
2fc1b 2f 20 20 20 20 32 32 2c 20 20 31 30 39 2c 20 20  /    22,  109,  
2fc1c 20 31 39 2c 20 20 20 32 34 2c 20 20 20 32 30 2c   19,   24,   20,
2fc1d 20 20 31 30 34 2c 20 20 20 33 38 2c 20 20 20 32    104,   38,   2
2fc1e 35 2c 20 20 31 30 34 2c 20 20 20 32 32 2c 0a 20  5,  104,   22,. 
2fc1f 2f 2a 20 20 31 34 38 30 20 2a 2f 20 20 20 20 20  /*  1480 */     
2fc20 35 2c 20 20 31 33 38 2c 20 20 20 20 31 2c 20 20  5,  138,    1,  
2fc21 31 31 38 2c 20 20 20 33 34 2c 20 20 20 34 32 2c  118,   34,   42,
2fc22 20 20 20 32 37 2c 20 20 31 30 38 2c 20 20 20 32     27,  108,   2
2fc23 32 2c 20 20 31 31 39 2c 0a 20 2f 2a 20 20 31 34  2,  119,. /*  14
2fc24 39 30 20 2a 2f 20 20 20 20 35 30 2c 20 20 20 37  90 */    50,   7
2fc25 34 2c 20 20 20 37 34 2c 20 20 31 32 37 2c 20 20  4,   74,  127,  
2fc26 20 20 31 2c 20 20 20 31 36 2c 20 20 20 35 31 2c    1,   16,   51,
2fc27 20 20 31 32 31 2c 20 20 20 32 30 2c 20 20 31 31    121,   20,  11
2fc28 39 2c 0a 20 2f 2a 20 20 31 35 30 30 20 2a 2f 20  9,. /*  1500 */ 
2fc29 20 20 31 30 38 2c 20 20 20 35 37 2c 20 20 20 35    108,   57,   5
2fc2a 31 2c 20 20 31 32 38 2c 20 20 20 32 32 2c 20 20  1,  128,   22,  
2fc2b 31 32 37 2c 20 20 20 32 33 2c 20 20 20 32 33 2c  127,   23,   23,
2fc2c 20 20 20 31 36 2c 20 20 20 31 35 2c 0a 20 2f 2a     16,   15,. /*
2fc2d 20 20 31 35 31 30 20 2a 2f 20 20 20 20 32 32 2c    1510 */    22,
2fc2e 20 20 20 20 33 2c 20 20 31 34 30 2c 20 20 20 20      3,  140,    
2fc2f 34 2c 20 20 32 35 33 2c 20 20 32 35 33 2c 20 20  4,  253,  253,  
2fc30 32 35 33 2c 20 20 32 35 33 2c 20 20 20 36 33 2c  253,  253,   63,
2fc31 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 59 59 5f 53  .};.#define YY_S
2fc32 48 49 46 54 5f 55 53 45 5f 44 46 4c 54 20 28 2d  HIFT_USE_DFLT (-
2fc33 31 31 31 29 0a 23 64 65 66 69 6e 65 20 59 59 5f  111).#define YY_
2fc34 53 48 49 46 54 5f 4d 41 58 20 34 31 35 0a 73 74  SHIFT_MAX 415.st
2fc35 61 74 69 63 20 63 6f 6e 73 74 20 73 68 6f 72 74  atic const short
2fc36 20 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b 5d   yy_shift_ofst[]
2fc37 20 3d 20 7b 0a 20 2f 2a 20 20 20 20 20 30 20 2a   = {. /*     0 *
2fc38 2f 20 20 20 31 38 37 2c 20 31 30 36 31 2c 20 31  /   187, 1061, 1
2fc39 31 37 30 2c 20 31 30 36 31 2c 20 31 31 39 34 2c  170, 1061, 1194,
2fc3a 20 31 31 39 34 2c 20 20 20 2d 32 2c 20 20 20 36   1194,   -2,   6
2fc3b 34 2c 20 20 20 36 34 2c 20 20 2d 31 39 2c 0a 20  4,   64,  -19,. 
2fc3c 2f 2a 20 20 20 20 31 30 20 2a 2f 20 20 31 31 39  /*    10 */  119
2fc3d 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31  4, 1194, 1194, 1
2fc3e 31 39 34 2c 20 31 31 39 34 2c 20 20 32 37 36 2c  194, 1194,  276,
2fc3f 20 20 20 20 31 2c 20 20 31 32 35 2c 20 31 30 37      1,  125, 107
2fc40 36 2c 20 31 31 39 34 2c 0a 20 2f 2a 20 20 20 20  6, 1194,. /*    
2fc41 32 30 20 2a 2f 20 20 31 31 39 34 2c 20 31 31 39  20 */  1194, 119
2fc42 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31  4, 1194, 1194, 1
2fc43 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c  194, 1194, 1194,
2fc44 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39   1194, 1194, 119
2fc45 34 2c 0a 20 2f 2a 20 20 20 20 33 30 20 2a 2f 20  4,. /*    30 */ 
2fc46 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39   1194, 1194, 119
2fc47 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31  4, 1194, 1194, 1
2fc48 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c  194, 1194, 1194,
2fc49 20 31 31 39 34 2c 20 31 31 39 34 2c 0a 20 2f 2a   1194, 1194,. /*
2fc4a 20 20 20 20 34 30 20 2a 2f 20 20 31 31 39 34 2c      40 */  1194,
2fc4b 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39   1194, 1194, 119
2fc4c 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31  4, 1194, 1194, 1
2fc4d 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c  194, 1194, 1194,
2fc4e 20 31 31 39 34 2c 0a 20 2f 2a 20 20 20 20 35 30   1194,. /*    50
2fc4f 20 2a 2f 20 20 31 31 39 34 2c 20 31 31 39 34 2c   */  1194, 1194,
2fc50 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31 31 39   1194, 1194, 119
2fc51 34 2c 20 31 31 39 34 2c 20 31 31 39 34 2c 20 31  4, 1194, 1194, 1
2fc52 31 39 34 2c 20 31 31 39 34 2c 20 20 2d 34 38 2c  194, 1194,  -48,
2fc53 0a 20 2f 2a 20 20 20 20 36 30 20 2a 2f 20 20 20  . /*    60 */   
2fc54 34 30 39 2c 20 20 20 20 31 2c 20 20 20 20 31 2c  409,    1,    1,
2fc55 20 20 31 34 31 2c 20 20 33 31 38 2c 20 20 33 31    141,  318,  31
2fc56 38 2c 20 2d 31 31 30 2c 20 20 20 35 33 2c 20 20  8, -110,   53,  
2fc57 31 39 37 2c 20 20 32 36 39 2c 0a 20 2f 2a 20 20  197,  269,. /*  
2fc58 20 20 37 30 20 2a 2f 20 20 20 33 34 31 2c 20 20    70 */   341,  
2fc59 34 31 33 2c 20 20 34 38 35 2c 20 20 35 35 37 2c  413,  485,  557,
2fc5a 20 20 36 32 39 2c 20 20 37 30 31 2c 20 20 37 37    629,  701,  77
2fc5b 33 2c 20 20 38 34 35 2c 20 20 37 37 33 2c 20 20  3,  845,  773,  
2fc5c 37 37 33 2c 0a 20 2f 2a 20 20 20 20 38 30 20 2a  773,. /*    80 *
2fc5d 2f 20 20 20 37 37 33 2c 20 20 37 37 33 2c 20 20  /   773,  773,  
2fc5e 37 37 33 2c 20 20 37 37 33 2c 20 20 37 37 33 2c  773,  773,  773,
2fc5f 20 20 37 37 33 2c 20 20 37 37 33 2c 20 20 37 37    773,  773,  77
2fc60 33 2c 20 20 37 37 33 2c 20 20 37 37 33 2c 0a 20  3,  773,  773,. 
2fc61 2f 2a 20 20 20 20 39 30 20 2a 2f 20 20 20 37 37  /*    90 */   77
2fc62 33 2c 20 20 37 37 33 2c 20 20 37 37 33 2c 20 20  3,  773,  773,  
2fc63 37 37 33 2c 20 20 37 37 33 2c 20 20 37 37 33 2c  773,  773,  773,
2fc64 20 20 39 31 37 2c 20 20 39 38 39 2c 20 20 39 38    917,  989,  98
2fc65 39 2c 20 20 2d 36 37 2c 0a 20 2f 2a 20 20 20 31  9,  -67,. /*   1
2fc66 30 30 20 2a 2f 20 20 20 2d 36 37 2c 20 20 20 2d  00 */   -67,   -
2fc67 31 2c 20 20 20 2d 31 2c 20 20 20 35 35 2c 20 20  1,   -1,   55,  
2fc68 20 32 35 2c 20 20 33 31 30 2c 20 20 20 20 31 2c   25,  310,    1,
2fc69 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20      1,    1,    
2fc6a 31 2c 0a 20 2f 2a 20 20 20 31 31 30 20 2a 2f 20  1,. /*   110 */ 
2fc6b 20 20 20 20 31 2c 20 20 36 33 39 2c 20 20 33 30      1,  639,  30
2fc6c 34 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20  4,    1,    1,  
2fc6d 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c    1,    1,    1,
2fc6e 20 20 20 20 31 2c 20 20 20 20 31 2c 0a 20 2f 2a      1,    1,. /*
2fc6f 20 20 20 31 32 30 20 2a 2f 20 20 20 20 20 31 2c     120 */     1,
2fc70 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20      1,    1,    
2fc71 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20  1,    1,    1,  
2fc72 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c    1,    1,    1,
2fc73 20 20 33 36 35 2c 0a 20 2f 2a 20 20 20 31 33 30    365,. /*   130
2fc74 20 2a 2f 20 20 20 31 34 31 2c 20 20 2d 31 37 2c   */   141,  -17,
2fc75 20 2d 31 31 31 2c 20 2d 31 31 31 2c 20 2d 31 31   -111, -111, -11
2fc76 31 2c 20 31 32 30 39 2c 20 20 20 38 31 2c 20 20  1, 1209,   81,  
2fc77 34 32 34 2c 20 20 33 35 33 2c 20 20 34 32 36 2c  424,  353,  426,
2fc78 0a 20 2f 2a 20 20 20 31 34 30 20 2a 2f 20 20 20  . /*   140 */   
2fc79 34 34 31 2c 20 20 20 39 30 2c 20 20 35 36 35 2c  441,   90,  565,
2fc7a 20 20 35 36 35 2c 20 20 20 20 31 2c 20 20 20 20    565,    1,    
2fc7b 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20  1,    1,    1,  
2fc7c 20 20 31 2c 20 20 20 20 31 2c 0a 20 2f 2a 20 20    1,    1,. /*  
2fc7d 20 31 35 30 20 2a 2f 20 20 20 20 20 31 2c 20 20   150 */     1,  
2fc7e 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c    1,    1,    1,
2fc7f 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20      1,    1,    
2fc80 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20  1,    1,    1,  
2fc81 20 20 31 2c 0a 20 2f 2a 20 20 20 31 36 30 20 2a    1,. /*   160 *
2fc82 2f 20 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20  /     1,    1,  
2fc83 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c    1,    1,    1,
2fc84 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20      1,    1,    
2fc85 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 0a 20  1,    1,    1,. 
2fc86 2f 2a 20 20 20 31 37 30 20 2a 2f 20 20 20 20 20  /*   170 */     
2fc87 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20  1,    1,    1,  
2fc88 20 20 31 2c 20 20 20 20 31 2c 20 20 20 20 31 2c    1,    1,    1,
2fc89 20 20 34 34 37 2c 20 20 38 30 39 2c 20 20 33 32    447,  809,  32
2fc8a 37 2c 20 20 34 31 39 2c 0a 20 2f 2a 20 20 20 31  7,  419,. /*   1
2fc8b 38 30 20 2a 2f 20 20 20 34 31 39 2c 20 20 34 31  80 */   419,  41
2fc8c 39 2c 20 20 38 34 31 2c 20 20 31 30 31 2c 20 2d  9,  841,  101, -
2fc8d 31 31 30 2c 20 2d 31 31 30 2c 20 2d 31 31 30 2c  110, -110, -110,
2fc8e 20 2d 31 31 31 2c 20 2d 31 31 31 2c 20 2d 31 31   -111, -111, -11
2fc8f 31 2c 0a 20 2f 2a 20 20 20 31 39 30 20 2a 2f 20  1,. /*   190 */ 
2fc90 20 20 32 33 32 2c 20 20 32 33 32 2c 20 20 32 36    232,  232,  26
2fc91 38 2c 20 20 34 32 37 2c 20 20 35 37 35 2c 20 20  8,  427,  575,  
2fc92 36 34 35 2c 20 20 37 38 38 2c 20 20 32 30 38 2c  645,  788,  208,
2fc93 20 20 38 36 31 2c 20 20 36 39 39 2c 0a 20 2f 2a    861,  699,. /*
2fc94 20 20 20 32 30 30 20 2a 2f 20 20 20 38 39 37 2c     200 */   897,
2fc95 20 20 37 38 34 2c 20 20 36 33 37 2c 20 20 20 35    784,  637,   5
2fc96 32 2c 20 20 31 38 33 2c 20 20 31 38 33 2c 20 20  2,  183,  183,  
2fc97 31 38 33 2c 20 20 39 30 32 2c 20 20 39 30 32 2c  183,  902,  902,
2fc98 20 20 39 39 36 2c 0a 20 2f 2a 20 20 20 32 31 30    996,. /*   210
2fc99 20 2a 2f 20 20 31 30 35 39 2c 20 20 39 30 32 2c   */  1059,  902,
2fc9a 20 20 39 30 32 2c 20 20 39 30 32 2c 20 20 39 30    902,  902,  90
2fc9b 32 2c 20 20 32 37 35 2c 20 20 36 38 39 2c 20 20  2,  275,  689,  
2fc9c 2d 31 33 2c 20 20 31 34 31 2c 20 20 38 32 34 2c  -13,  141,  824,
2fc9d 0a 20 2f 2a 20 20 20 32 32 30 20 2a 2f 20 20 20  . /*   220 */   
2fc9e 38 32 34 2c 20 20 34 37 38 2c 20 20 34 39 38 2c  824,  478,  498,
2fc9f 20 20 34 39 38 2c 20 20 36 35 36 2c 20 20 34 39    498,  656,  49
2fca0 38 2c 20 20 32 36 32 2c 20 20 34 39 38 2c 20 20  8,  262,  498,  
2fca1 31 34 31 2c 20 20 34 39 38 2c 0a 20 2f 2a 20 20  141,  498,. /*  
2fca2 20 32 33 30 20 2a 2f 20 20 20 31 34 31 2c 20 20   230 */   141,  
2fca3 38 36 30 2c 20 20 37 33 37 2c 20 20 37 31 32 2c  860,  737,  712,
2fca4 20 20 37 33 37 2c 20 20 36 35 36 2c 20 20 36 35    737,  656,  65
2fca5 36 2c 20 20 37 31 32 2c 20 31 30 31 37 2c 20 31  6,  712, 1017, 1
2fca6 30 31 37 2c 0a 20 2f 2a 20 20 20 32 34 30 20 2a  017,. /*   240 *
2fca7 2f 20 20 31 30 31 37 2c 20 31 30 31 37 2c 20 31  /  1017, 1017, 1
2fca8 30 34 30 2c 20 31 30 34 30 2c 20 31 30 38 39 2c  040, 1040, 1089,
2fca9 20 2d 31 31 30 2c 20 31 30 32 34 2c 20 31 30 33   -110, 1024, 103
2fcaa 34 2c 20 31 30 37 35 2c 20 31 30 39 33 2c 0a 20  4, 1075, 1093,. 
2fcab 2f 2a 20 20 20 32 35 30 20 2a 2f 20 20 31 30 37  /*   250 */  107
2fcac 33 2c 20 31 31 31 30 2c 20 31 31 34 33 2c 20 31  3, 1110, 1143, 1
2fcad 31 34 33 2c 20 31 31 39 37 2c 20 31 31 39 39 2c  143, 1197, 1199,
2fcae 20 31 31 39 37 2c 20 31 31 39 39 2c 20 31 31 39   1197, 1199, 119
2fcaf 37 2c 20 31 31 39 39 2c 0a 20 2f 2a 20 20 20 32  7, 1199,. /*   2
2fcb0 36 30 20 2a 2f 20 20 31 32 34 34 2c 20 31 32 34  60 */  1244, 124
2fcb1 34 2c 20 31 33 32 34 2c 20 31 32 34 34 2c 20 31  4, 1324, 1244, 1
2fcb2 32 37 30 2c 20 31 32 34 34 2c 20 31 33 35 33 2c  270, 1244, 1353,
2fcb3 20 31 32 38 36 2c 20 31 32 38 36 2c 20 31 33 32   1286, 1286, 132
2fcb4 34 2c 0a 20 2f 2a 20 20 20 32 37 30 20 2a 2f 20  4,. /*   270 */ 
2fcb5 20 31 32 34 34 2c 20 31 32 34 34 2c 20 31 32 34   1244, 1244, 124
2fcb6 34 2c 20 31 33 35 33 2c 20 31 33 36 31 2c 20 31  4, 1353, 1361, 1
2fcb7 31 34 33 2c 20 31 33 36 31 2c 20 31 31 34 33 2c  143, 1361, 1143,
2fcb8 20 31 33 36 31 2c 20 31 31 34 33 2c 0a 20 2f 2a   1361, 1143,. /*
2fcb9 20 20 20 32 38 30 20 2a 2f 20 20 31 31 34 33 2c     280 */  1143,
2fcba 20 31 33 34 37 2c 20 31 32 35 39 2c 20 31 33 36   1347, 1259, 136
2fcbb 31 2c 20 31 31 34 33 2c 20 31 33 33 35 2c 20 31  1, 1143, 1335, 1
2fcbc 33 33 35 2c 20 31 33 38 32 2c 20 31 30 32 34 2c  335, 1382, 1024,
2fcbd 20 31 31 34 33 2c 0a 20 2f 2a 20 20 20 32 39 30   1143,. /*   290
2fcbe 20 2a 2f 20 20 31 33 39 30 2c 20 31 33 39 30 2c   */  1390, 1390,
2fcbf 20 31 33 39 30 2c 20 31 33 39 30 2c 20 31 30 32   1390, 1390, 102
2fcc0 34 2c 20 31 33 33 35 2c 20 31 33 38 32 2c 20 31  4, 1335, 1382, 1
2fcc1 31 34 33 2c 20 31 33 37 37 2c 20 31 33 37 37 2c  143, 1377, 1377,
2fcc2 0a 20 2f 2a 20 20 20 33 30 30 20 2a 2f 20 20 31  . /*   300 */  1
2fcc3 31 34 33 2c 20 31 31 34 33 2c 20 31 33 38 34 2c  143, 1143, 1384,
2fcc4 20 2d 31 31 31 2c 20 2d 31 31 31 2c 20 2d 31 31   -111, -111, -11
2fcc5 31 2c 20 2d 31 31 31 2c 20 2d 31 31 31 2c 20 2d  1, -111, -111, -
2fcc6 31 31 31 2c 20 20 35 35 32 2c 0a 20 2f 2a 20 20  111,  552,. /*  
2fcc7 20 33 31 30 20 2a 2f 20 20 20 37 34 39 2c 20 31   310 */   749, 1
2fcc8 31 33 37 2c 20 31 30 33 31 2c 20 31 30 38 32 2c  137, 1031, 1082,
2fcc9 20 31 32 33 32 2c 20 20 38 30 31 2c 20 31 30 39   1232,  801, 109
2fcca 37 2c 20 31 31 35 33 2c 20 20 38 37 33 2c 20 31  7, 1153,  873, 1
2fccb 30 31 31 2c 0a 20 2f 2a 20 20 20 33 32 30 20 2a  011,. /*   320 *
2fccc 2f 20 20 20 38 35 33 2c 20 31 31 36 33 2c 20 31  /   853, 1163, 1
2fccd 32 35 37 2c 20 31 33 32 38 2c 20 31 33 32 39 2c  257, 1328, 1329,
2fcce 20 31 33 33 37 2c 20 31 33 34 30 2c 20 31 33 34   1337, 1340, 134
2fccf 31 2c 20 20 37 33 36 2c 20 31 33 34 34 2c 0a 20  1,  736, 1344,. 
2fcd0 2f 2a 20 20 20 33 33 30 20 2a 2f 20 20 31 34 32  /*   330 */  142
2fcd1 32 2c 20 31 34 31 33 2c 20 31 34 30 37 2c 20 31  2, 1413, 1407, 1
2fcd2 34 31 30 2c 20 31 33 31 36 2c 20 31 33 31 37 2c  410, 1316, 1317,
2fcd3 20 31 33 31 38 2c 20 31 33 31 39 2c 20 31 33 32   1318, 1319, 132
2fcd4 30 2c 20 31 33 34 32 2c 0a 20 2f 2a 20 20 20 33  0, 1342,. /*   3
2fcd5 34 30 20 2a 2f 20 20 31 33 32 36 2c 20 31 34 31  40 */  1326, 141
2fcd6 39 2c 20 31 34 32 30 2c 20 31 34 32 31 2c 20 31  9, 1420, 1421, 1
2fcd7 34 32 33 2c 20 31 34 33 31 2c 20 31 34 32 34 2c  423, 1431, 1424,
2fcd8 20 31 34 32 35 2c 20 31 34 32 36 2c 20 31 34 32   1425, 1426, 142
2fcd9 37 2c 0a 20 2f 2a 20 20 20 33 35 30 20 2a 2f 20  7,. /*   350 */ 
2fcda 20 31 34 32 39 2c 20 31 34 32 38 2c 20 31 34 31   1429, 1428, 141
2fcdb 35 2c 20 31 34 33 30 2c 20 31 34 33 32 2c 20 31  5, 1430, 1432, 1
2fcdc 34 32 38 2c 20 31 33 32 37 2c 20 31 34 33 34 2c  428, 1327, 1434,
2fcdd 20 31 34 33 33 2c 20 31 34 33 35 2c 0a 20 2f 2a   1433, 1435,. /*
2fcde 20 20 20 33 36 30 20 2a 2f 20 20 31 33 33 36 2c     360 */  1336,
2fcdf 20 31 34 33 36 2c 20 31 34 33 37 2c 20 31 34 33   1436, 1437, 143
2fce0 38 2c 20 31 34 30 38 2c 20 31 34 33 39 2c 20 31  8, 1408, 1439, 1
2fce1 34 31 34 2c 20 31 34 34 31 2c 20 31 34 34 32 2c  414, 1441, 1442,
2fce2 20 31 34 34 36 2c 0a 20 2f 2a 20 20 20 33 37 30   1446,. /*   370
2fce3 20 2a 2f 20 20 31 34 34 37 2c 20 31 34 34 30 2c   */  1447, 1440,
2fce4 20 31 34 34 38 2c 20 31 33 35 35 2c 20 31 33 36   1448, 1355, 136
2fce5 32 2c 20 31 34 35 33 2c 20 31 34 35 34 2c 20 31  2, 1453, 1454, 1
2fce6 34 34 39 2c 20 31 33 37 31 2c 20 31 34 34 33 2c  449, 1371, 1443,
2fce7 0a 20 2f 2a 20 20 20 33 38 30 20 2a 2f 20 20 31  . /*   380 */  1
2fce8 34 34 34 2c 20 31 34 34 35 2c 20 31 34 35 32 2c  444, 1445, 1452,
2fce9 20 31 34 35 31 2c 20 31 33 34 33 2c 20 31 33 37   1451, 1343, 137
2fcea 34 2c 20 31 34 35 37 2c 20 31 34 37 35 2c 20 31  4, 1457, 1475, 1
2fceb 34 38 31 2c 20 31 33 36 35 2c 0a 20 2f 2a 20 20  481, 1365,. /*  
2fcec 20 33 39 30 20 2a 2f 20 20 31 34 35 30 2c 20 31   390 */  1450, 1
2fced 34 35 39 2c 20 31 33 37 39 2c 20 31 34 31 37 2c  459, 1379, 1417,
2fcee 20 31 34 31 38 2c 20 31 33 36 36 2c 20 31 34 36   1418, 1366, 146
2fcef 36 2c 20 31 33 37 30 2c 20 31 34 39 33 2c 20 31  6, 1370, 1493, 1
2fcf0 34 37 39 2c 0a 20 2f 2a 20 20 20 34 30 30 20 2a  479,. /*   400 *
2fcf1 2f 20 20 31 33 37 36 2c 20 31 34 37 38 2c 20 31  /  1376, 1478, 1
2fcf2 33 38 30 2c 20 31 33 39 32 2c 20 31 33 37 38 2c  380, 1392, 1378,
2fcf3 20 31 34 38 32 2c 20 31 33 37 35 2c 20 31 34 38   1482, 1375, 148
2fcf4 33 2c 20 31 34 38 34 2c 20 31 34 39 32 2c 0a 20  3, 1484, 1492,. 
2fcf5 2f 2a 20 20 20 34 31 30 20 2a 2f 20 20 31 34 35  /*   410 */  145
2fcf6 35 2c 20 31 34 39 34 2c 20 31 33 37 32 2c 20 31  5, 1494, 1372, 1
2fcf7 34 38 38 2c 20 31 35 30 38 2c 20 31 35 30 39 2c  488, 1508, 1509,
2fcf8 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 59 59 5f 52  .};.#define YY_R
2fcf9 45 44 55 43 45 5f 55 53 45 5f 44 46 4c 54 20 28  EDUCE_USE_DFLT (
2fcfa 2d 31 38 30 29 0a 23 64 65 66 69 6e 65 20 59 59  -180).#define YY
2fcfb 5f 52 45 44 55 43 45 5f 4d 41 58 20 33 30 38 0a  _REDUCE_MAX 308.
2fcfc 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 68 6f  static const sho
2fcfd 72 74 20 79 79 5f 72 65 64 75 63 65 5f 6f 66 73  rt yy_reduce_ofs
2fcfe 74 5b 5d 20 3d 20 7b 0a 20 2f 2a 20 20 20 20 20  t[] = {. /*     
2fcff 30 20 2a 2f 20 20 2d 31 34 31 2c 20 20 20 38 32  0 */  -141,   82
2fd00 2c 20 20 31 35 34 2c 20 20 32 38 34 2c 20 20 20  ,  154,  284,   
2fd01 31 32 2c 20 20 20 37 35 2c 20 20 20 36 39 2c 20  12,   75,   69, 
2fd02 20 20 37 33 2c 20 20 31 34 32 2c 20 20 2d 35 39    73,  142,  -59
2fd03 2c 0a 20 2f 2a 20 20 20 20 31 30 20 2a 2f 20 20  ,. /*    10 */  
2fd04 20 31 34 35 2c 20 20 20 38 37 2c 20 20 31 35 39   145,   87,  159
2fd05 2c 20 20 32 32 30 2c 20 20 32 39 31 2c 20 20 33  ,  220,  291,  3
2fd06 34 36 2c 20 20 32 32 36 2c 20 20 32 31 33 2c 20  46,  226,  213, 
2fd07 20 33 35 37 2c 20 20 33 37 34 2c 0a 20 2f 2a 20   357,  374,. /* 
2fd08 20 20 20 32 30 20 2a 2f 20 20 20 34 32 39 2c 20     20 */   429, 
2fd09 20 34 33 37 2c 20 20 34 34 32 2c 20 20 34 38 36   437,  442,  486
2fd0a 2c 20 20 34 39 39 2c 20 20 35 30 35 2c 20 20 35  ,  499,  505,  5
2fd0b 30 37 2c 20 20 35 31 39 2c 20 20 35 35 38 2c 20  07,  519,  558, 
2fd0c 20 35 37 31 2c 0a 20 2f 2a 20 20 20 20 33 30 20   571,. /*    30 
2fd0d 2a 2f 20 20 20 35 37 37 2c 20 20 35 38 38 2c 20  */   577,  588, 
2fd0e 20 36 33 30 2c 20 20 36 34 33 2c 20 20 36 34 39   630,  643,  649
2fd0f 2c 20 20 36 35 31 2c 20 20 36 36 32 2c 20 20 37  ,  651,  662,  7
2fd10 30 32 2c 20 20 37 31 35 2c 20 20 37 32 31 2c 0a  02,  715,  721,.
2fd11 20 2f 2a 20 20 20 20 34 30 20 2a 2f 20 20 20 37   /*    40 */   7
2fd12 33 33 2c 20 20 37 37 34 2c 20 20 37 38 37 2c 20  33,  774,  787, 
2fd13 20 37 39 33 2c 20 20 38 30 35 2c 20 20 38 34 36   793,  805,  846
2fd14 2c 20 20 38 35 39 2c 20 20 38 36 35 2c 20 20 38  ,  859,  865,  8
2fd15 37 37 2c 20 20 38 38 31 2c 0a 20 2f 2a 20 20 20  77,  881,. /*   
2fd16 20 35 30 20 2a 2f 20 20 20 39 33 34 2c 20 20 39   50 */   934,  9
2fd17 33 36 2c 20 20 39 36 33 2c 20 20 39 36 37 2c 20  36,  963,  967, 
2fd18 20 39 36 39 2c 20 20 39 39 38 2c 20 31 30 35 33   969,  998, 1053
2fd19 2c 20 31 30 37 32 2c 20 31 30 38 38 2c 20 2d 31  , 1072, 1088, -1
2fd1a 37 39 2c 0a 20 2f 2a 20 20 20 20 36 30 20 2a 2f  79,. /*    60 */
2fd1b 20 20 20 38 35 30 2c 20 20 39 35 36 2c 20 20 33     850,  956,  3
2fd1c 38 30 2c 20 20 33 30 38 2c 20 20 20 38 39 2c 20  80,  308,   89, 
2fd1d 20 34 39 36 2c 20 20 33 38 34 2c 20 20 20 20 32   496,  384,    2
2fd1e 2c 20 20 20 20 32 2c 20 20 20 20 32 2c 0a 20 2f  ,    2,    2,. /
2fd1f 2a 20 20 20 20 37 30 20 2a 2f 20 20 20 20 20 32  *    70 */     2
2fd20 2c 20 20 20 20 32 2c 20 20 20 20 32 2c 20 20 20  ,    2,    2,   
2fd21 20 32 2c 20 20 20 20 32 2c 20 20 20 20 32 2c 20   2,    2,    2, 
2fd22 20 20 20 32 2c 20 20 20 20 32 2c 20 20 20 20 32     2,    2,    2
2fd23 2c 20 20 20 20 32 2c 0a 20 2f 2a 20 20 20 20 38  ,    2,. /*    8
2fd24 30 20 2a 2f 20 20 20 20 20 32 2c 20 20 20 20 32  0 */     2,    2
2fd25 2c 20 20 20 20 32 2c 20 20 20 20 32 2c 20 20 20  ,    2,    2,   
2fd26 20 32 2c 20 20 20 20 32 2c 20 20 20 20 32 2c 20   2,    2,    2, 
2fd27 20 20 20 32 2c 20 20 20 20 32 2c 20 20 20 20 32     2,    2,    2
2fd28 2c 0a 20 2f 2a 20 20 20 20 39 30 20 2a 2f 20 20  ,. /*    90 */  
2fd29 20 20 20 32 2c 20 20 20 20 32 2c 20 20 20 20 32     2,    2,    2
2fd2a 2c 20 20 20 20 32 2c 20 20 20 20 32 2c 20 20 20  ,    2,    2,   
2fd2b 20 32 2c 20 20 20 20 32 2c 20 20 20 20 32 2c 20   2,    2,    2, 
2fd2c 20 20 20 32 2c 20 20 20 20 32 2c 0a 20 2f 2a 20     2,    2,. /* 
2fd2d 20 20 31 30 30 20 2a 2f 20 20 20 20 20 32 2c 20    100 */     2, 
2fd2e 20 20 20 32 2c 20 20 20 20 32 2c 20 20 34 31 36     2,    2,  416
2fd2f 2c 20 20 20 20 32 2c 20 20 20 20 32 2c 20 20 34  ,    2,    2,  4
2fd30 34 39 2c 20 20 35 37 39 2c 20 20 36 34 38 2c 20  49,  579,  648, 
2fd31 20 37 32 33 2c 0a 20 2f 2a 20 20 20 31 31 30 20   723,. /*   110 
2fd32 2a 2f 20 20 20 37 39 31 2c 20 20 31 33 34 2c 20  */   791,  134, 
2fd33 20 35 30 31 2c 20 20 37 31 36 2c 20 20 35 32 31   501,  716,  521
2fd34 2c 20 20 37 39 34 2c 20 20 35 38 39 2c 20 20 2d  ,  794,  589,  -
2fd35 34 37 2c 20 20 36 35 30 2c 20 20 35 39 30 2c 0a  47,  650,  590,.
2fd36 20 2f 2a 20 20 20 31 32 30 20 2a 2f 20 20 20 37   /*   120 */   7
2fd37 39 35 2c 20 20 39 34 32 2c 20 20 39 37 34 2c 20  95,  942,  974, 
2fd38 20 39 38 36 2c 20 31 30 30 33 2c 20 31 30 34 37   986, 1003, 1047
2fd39 2c 20 31 30 37 34 2c 20 20 39 33 35 2c 20 31 30  , 1074,  935, 10
2fd3a 39 31 2c 20 20 20 20 32 2c 0a 20 2f 2a 20 20 20  91,    2,. /*   
2fd3b 31 33 30 20 2a 2f 20 20 20 34 31 37 2c 20 20 20  130 */   417,   
2fd3c 20 32 2c 20 20 20 20 32 2c 20 20 20 20 32 2c 20   2,    2,    2, 
2fd3d 20 20 20 32 2c 20 20 31 35 38 2c 20 20 33 33 36     2,  158,  336
2fd3e 2c 20 20 35 32 36 2c 20 20 35 37 36 2c 20 20 38  ,  526,  576,  8
2fd3f 36 33 2c 0a 20 2f 2a 20 20 20 31 34 30 20 2a 2f  63,. /*   140 */
2fd40 20 20 20 38 38 35 2c 20 20 39 36 36 2c 20 20 34     885,  966,  4
2fd41 30 35 2c 20 20 34 32 38 2c 20 20 39 36 38 2c 20  05,  428,  968, 
2fd42 31 30 33 39 2c 20 31 30 36 39 2c 20 31 30 39 39  1039, 1069, 1099
2fd43 2c 20 31 31 30 30 2c 20 20 39 36 36 2c 0a 20 2f  , 1100,  966,. /
2fd44 2a 20 20 20 31 35 30 20 2a 2f 20 20 31 31 30 31  *   150 */  1101
2fd45 2c 20 31 31 30 37 2c 20 31 31 30 39 2c 20 31 31  , 1107, 1109, 11
2fd46 31 38 2c 20 31 31 32 30 2c 20 31 31 34 30 2c 20  18, 1120, 1140, 
2fd47 31 31 34 31 2c 20 31 31 34 35 2c 20 31 31 34 36  1141, 1145, 1146
2fd48 2c 20 31 31 34 38 2c 0a 20 2f 2a 20 20 20 31 36  , 1148,. /*   16
2fd49 30 20 2a 2f 20 20 31 31 34 39 2c 20 31 31 35 30  0 */  1149, 1150
2fd4a 2c 20 31 31 35 32 2c 20 31 31 35 34 2c 20 31 31  , 1152, 1154, 11
2fd4b 36 36 2c 20 31 31 36 38 2c 20 31 31 37 31 2c 20  66, 1168, 1171, 
2fd4c 31 31 37 32 2c 20 31 31 38 30 2c 20 31 31 38 32  1172, 1180, 1182
2fd4d 2c 0a 20 2f 2a 20 20 20 31 37 30 20 2a 2f 20 20  ,. /*   170 */  
2fd4e 31 31 38 34 2c 20 31 31 38 35 2c 20 31 31 39 31  1184, 1185, 1191
2fd4f 2c 20 31 31 39 32 2c 20 31 31 39 33 2c 20 31 31  , 1192, 1193, 11
2fd50 39 36 2c 20 20 34 30 33 2c 20 20 34 30 33 2c 20  96,  403,  403, 
2fd51 20 36 35 32 2c 20 20 33 37 37 2c 0a 20 2f 2a 20   652,  377,. /* 
2fd52 20 20 31 38 30 20 2a 2f 20 20 20 36 36 33 2c 20    180 */   663, 
2fd53 20 36 36 37 2c 20 2d 31 33 34 2c 20 20 37 38 30   667, -134,  780
2fd54 2c 20 20 38 38 38 2c 20 20 39 33 33 2c 20 31 30  ,  888,  933, 10
2fd55 36 36 2c 20 20 20 34 34 2c 20 20 36 37 32 2c 20  66,   44,  672, 
2fd56 20 36 39 38 2c 0a 20 2f 2a 20 20 20 31 39 30 20   698,. /*   190 
2fd57 2a 2f 20 20 20 2d 39 38 2c 20 20 2d 37 30 2c 20  */   -98,  -70, 
2fd58 20 2d 35 34 2c 20 20 2d 33 36 2c 20 20 2d 33 35   -54,  -36,  -35
2fd59 2c 20 20 2d 33 35 2c 20 20 2d 33 35 2c 20 20 20  ,  -35,  -35,   
2fd5a 31 33 2c 20 20 2d 33 35 2c 20 20 20 31 34 2c 0a  13,  -35,   14,.
2fd5b 20 2f 2a 20 20 20 32 30 30 20 2a 2f 20 20 20 31   /*   200 */   1
2fd5c 34 36 2c 20 20 31 38 31 2c 20 20 32 32 37 2c 20  46,  181,  227, 
2fd5d 20 20 31 34 2c 20 20 32 30 33 2c 20 20 32 32 33    14,  203,  223
2fd5e 2c 20 20 32 35 30 2c 20 20 2d 33 35 2c 20 20 2d  ,  250,  -35,  -
2fd5f 33 35 2c 20 20 32 32 34 2c 0a 20 2f 2a 20 20 20  35,  224,. /*   
2fd60 32 31 30 20 2a 2f 20 20 20 32 30 32 2c 20 20 2d  210 */   202,  -
2fd61 33 35 2c 20 20 2d 33 35 2c 20 20 2d 33 35 2c 20  35,  -35,  -35, 
2fd62 20 2d 33 35 2c 20 20 33 33 39 2c 20 20 33 30 39   -35,  339,  309
2fd63 2c 20 20 33 31 32 2c 20 20 33 38 31 2c 20 20 33  ,  312,  381,  3
2fd64 31 37 2c 0a 20 2f 2a 20 20 20 32 32 30 20 2a 2f  17,. /*   220 */
2fd65 20 20 20 33 37 36 2c 20 20 34 35 37 2c 20 20 35     376,  457,  5
2fd66 31 35 2c 20 20 35 37 30 2c 20 20 36 31 39 2c 20  15,  570,  619, 
2fd67 20 35 38 34 2c 20 20 36 38 37 2c 20 20 37 30 35   584,  687,  705
2fd68 2c 20 20 37 30 39 2c 20 20 37 36 35 2c 0a 20 2f  ,  709,  765,. /
2fd69 2a 20 20 20 32 33 30 20 2a 2f 20 20 20 37 32 36  *   230 */   726
2fd6a 2c 20 20 37 38 36 2c 20 20 37 33 30 2c 20 20 37  ,  786,  730,  7
2fd6b 37 38 2c 20 20 37 34 31 2c 20 20 38 30 33 2c 20  78,  741,  803, 
2fd6c 20 38 31 33 2c 20 20 38 32 37 2c 20 20 38 38 33   813,  827,  883
2fd6d 2c 20 20 38 39 32 2c 0a 20 2f 2a 20 20 20 32 34  ,  892,. /*   24
2fd6e 30 20 2a 2f 20 20 20 38 39 36 2c 20 20 39 30 30  0 */   896,  900
2fd6f 2c 20 20 39 30 33 2c 20 20 39 34 33 2c 20 20 39  ,  903,  943,  9
2fd70 36 34 2c 20 20 39 33 32 2c 20 20 39 33 30 2c 20  64,  932,  930, 
2fd71 20 39 35 38 2c 20 20 39 38 34 2c 20 31 30 31 35   958,  984, 1015
2fd72 2c 0a 20 2f 2a 20 20 20 32 35 30 20 2a 2f 20 20  ,. /*   250 */  
2fd73 31 30 33 30 2c 20 31 30 32 35 2c 20 31 31 31 32  1030, 1025, 1112
2fd74 2c 20 31 31 31 34 2c 20 31 30 39 38 2c 20 31 31  , 1114, 1098, 11
2fd75 33 33 2c 20 31 31 30 32 2c 20 31 31 34 34 2c 20  33, 1102, 1144, 
2fd76 31 31 31 39 2c 20 31 31 35 37 2c 0a 20 2f 2a 20  1119, 1157,. /* 
2fd77 20 20 32 36 30 20 2a 2f 20 20 31 31 38 33 2c 20    260 */  1183, 
2fd78 31 31 39 35 2c 20 31 31 38 38 2c 20 31 31 39 38  1195, 1188, 1198
2fd79 2c 20 31 32 30 30 2c 20 31 32 30 31 2c 20 31 32  , 1200, 1201, 12
2fd7a 30 32 2c 20 31 31 35 35 2c 20 31 31 35 36 2c 20  02, 1155, 1156, 
2fd7b 31 32 30 33 2c 0a 20 2f 2a 20 20 20 32 37 30 20  1203,. /*   270 
2fd7c 2a 2f 20 20 31 32 30 34 2c 20 31 32 30 36 2c 20  */  1204, 1206, 
2fd7d 31 32 30 37 2c 20 31 32 30 35 2c 20 31 32 33 33  1207, 1205, 1233
2fd7e 2c 20 31 32 33 31 2c 20 31 32 33 34 2c 20 31 32  , 1231, 1234, 12
2fd7f 33 35 2c 20 31 32 33 37 2c 20 31 32 33 38 2c 0a  35, 1237, 1238,.
2fd80 20 2f 2a 20 20 20 32 38 30 20 2a 2f 20 20 31 32   /*   280 */  12
2fd81 33 39 2c 20 31 31 35 39 2c 20 31 31 36 30 2c 20  39, 1159, 1160, 
2fd82 31 32 34 32 2c 20 31 32 34 33 2c 20 31 32 31 33  1242, 1243, 1213
2fd83 2c 20 31 32 31 34 2c 20 31 31 38 36 2c 20 31 32  , 1214, 1186, 12
2fd84 30 38 2c 20 31 32 34 39 2c 0a 20 2f 2a 20 20 20  08, 1249,. /*   
2fd85 32 39 30 20 2a 2f 20 20 31 32 31 37 2c 20 31 32  290 */  1217, 12
2fd86 31 38 2c 20 31 32 31 39 2c 20 31 32 32 30 2c 20  18, 1219, 1220, 
2fd87 31 32 31 35 2c 20 31 32 32 34 2c 20 31 32 31 30  1215, 1224, 1210
2fd88 2c 20 31 32 35 38 2c 20 31 31 37 34 2c 20 31 31  , 1258, 1174, 11
2fd89 37 35 2c 0a 20 2f 2a 20 20 20 33 30 30 20 2a 2f  75,. /*   300 */
2fd8a 20 20 31 32 36 32 2c 20 31 32 36 33 2c 20 31 31    1262, 1263, 11
2fd8b 37 36 2c 20 31 32 36 30 2c 20 31 32 34 35 2c 20  76, 1260, 1245, 
2fd8c 31 32 34 36 2c 20 31 31 37 38 2c 20 31 31 37 39  1246, 1178, 1179
2fd8d 2c 20 31 31 38 31 2c 0a 7d 3b 0a 73 74 61 74 69  , 1181,.};.stati
2fd8e 63 20 63 6f 6e 73 74 20 59 59 41 43 54 49 4f 4e  c const YYACTION
2fd8f 54 59 50 45 20 79 79 5f 64 65 66 61 75 6c 74 5b  TYPE yy_default[
2fd90 5d 20 3d 20 7b 0a 20 2f 2a 20 20 20 20 20 30 20  ] = {. /*     0 
2fd91 2a 2f 20 20 20 36 33 34 2c 20 20 38 36 39 2c 20  */   634,  869, 
2fd92 20 39 35 38 2c 20 20 39 35 38 2c 20 20 38 36 39   958,  958,  869
2fd93 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 38  ,  958,  958,  8
2fd94 39 38 2c 20 20 38 39 38 2c 20 20 37 35 37 2c 0a  98,  898,  757,.
2fd95 20 2f 2a 20 20 20 20 31 30 20 2a 2f 20 20 20 38   /*    10 */   8
2fd96 36 37 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  67,  958,  958, 
2fd97 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fd98 2c 20 20 39 35 38 2c 20 20 39 33 32 2c 20 20 39  ,  958,  932,  9
2fd99 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20  58,  958,. /*   
2fd9a 20 32 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39   20 */   958,  9
2fd9b 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
2fd9c 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fd9d 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fd9e 35 38 2c 0a 20 2f 2a 20 20 20 20 33 30 20 2a 2f  58,. /*    30 */
2fd9f 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39     958,  958,  9
2fda0 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
2fda1 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fda2 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f  ,  958,  958,. /
2fda3 2a 20 20 20 20 34 30 20 2a 2f 20 20 20 39 35 38  *    40 */   958
2fda4 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fda5 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
2fda6 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fda7 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20 20 35  ,  958,. /*    5
2fda8 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38  0 */   958,  958
2fda9 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fdaa 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
2fdab 20 39 35 38 2c 20 20 39 35 38 2c 20 20 38 34 31   958,  958,  841
2fdac 2c 0a 20 2f 2a 20 20 20 20 36 30 20 2a 2f 20 20  ,. /*    60 */  
2fdad 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fdae 2c 20 20 36 37 33 2c 20 20 38 39 38 2c 20 20 38  ,  673,  898,  8
2fdaf 39 38 2c 20 20 37 36 31 2c 20 20 37 39 32 2c 20  98,  761,  792, 
2fdb0 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20   958,  958,. /* 
2fdb1 20 20 20 37 30 20 2a 2f 20 20 20 39 35 38 2c 20     70 */   958, 
2fdb2 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fdb3 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 37  ,  958,  958,  7
2fdb4 39 33 2c 20 20 39 35 38 2c 20 20 38 37 31 2c 20  93,  958,  871, 
2fdb5 20 38 36 36 2c 0a 20 2f 2a 20 20 20 20 38 30 20   866,. /*    80 
2fdb6 2a 2f 20 20 20 38 36 32 2c 20 20 38 36 34 2c 20  */   862,  864, 
2fdb7 20 38 36 33 2c 20 20 38 37 30 2c 20 20 37 39 34   863,  870,  794
2fdb8 2c 20 20 37 38 33 2c 20 20 37 39 30 2c 20 20 37  ,  783,  790,  7
2fdb9 39 37 2c 20 20 37 37 32 2c 20 20 39 31 31 2c 0a  97,  772,  911,.
2fdba 20 2f 2a 20 20 20 20 39 30 20 2a 2f 20 20 20 37   /*    90 */   7
2fdbb 39 39 2c 20 20 38 30 30 2c 20 20 38 30 36 2c 20  99,  800,  806, 
2fdbc 20 38 30 37 2c 20 20 39 33 33 2c 20 20 39 33 31   807,  933,  931
2fdbd 2c 20 20 38 32 39 2c 20 20 38 32 38 2c 20 20 38  ,  829,  828,  8
2fdbe 34 37 2c 20 20 38 33 31 2c 0a 20 2f 2a 20 20 20  47,  831,. /*   
2fdbf 31 30 30 20 2a 2f 20 20 20 38 35 33 2c 20 20 38  100 */   853,  8
2fdc0 33 30 2c 20 20 38 34 30 2c 20 20 36 36 35 2c 20  30,  840,  665, 
2fdc1 20 38 33 32 2c 20 20 38 33 33 2c 20 20 39 35 38   832,  833,  958
2fdc2 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fdc3 35 38 2c 0a 20 2f 2a 20 20 20 31 31 30 20 2a 2f  58,. /*   110 */
2fdc4 20 20 20 39 35 38 2c 20 20 37 32 36 2c 20 20 36     958,  726,  6
2fdc5 36 30 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  60,  958,  958, 
2fdc6 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fdc7 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f  ,  958,  958,. /
2fdc8 2a 20 20 20 31 32 30 20 2a 2f 20 20 20 39 35 38  *   120 */   958
2fdc9 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fdca 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
2fdcb 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fdcc 2c 20 20 38 33 34 2c 0a 20 2f 2a 20 20 20 31 33  ,  834,. /*   13
2fdcd 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 38 33 35  0 */   958,  835
2fdce 2c 20 20 38 34 38 2c 20 20 38 34 39 2c 20 20 38  ,  848,  849,  8
2fdcf 35 30 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  50,  958,  958, 
2fdd0 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fdd1 2c 0a 20 2f 2a 20 20 20 31 34 30 20 2a 2f 20 20  ,. /*   140 */  
2fdd2 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fdd3 2c 20 20 39 35 38 2c 20 20 36 34 30 2c 20 20 39  ,  958,  640,  9
2fdd4 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
2fdd5 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20   958,  958,. /* 
2fdd6 20 20 31 35 30 20 2a 2f 20 20 20 39 35 38 2c 20    150 */   958, 
2fdd7 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fdd8 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fdd9 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
2fdda 20 39 35 38 2c 0a 20 2f 2a 20 20 20 31 36 30 20   958,. /*   160 
2fddb 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20  */   958,  958, 
2fddc 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fddd 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fdde 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a  58,  958,  958,.
2fddf 20 2f 2a 20 20 20 31 37 30 20 2a 2f 20 20 20 39   /*   170 */   9
2fde0 35 38 2c 20 20 38 38 32 2c 20 20 39 35 38 2c 20  58,  882,  958, 
2fde1 20 39 33 36 2c 20 20 39 33 38 2c 20 20 39 35 38   936,  938,  958
2fde2 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 36  ,  958,  958,  6
2fde3 33 34 2c 20 20 37 35 37 2c 0a 20 2f 2a 20 20 20  34,  757,. /*   
2fde4 31 38 30 20 2a 2f 20 20 20 37 35 37 2c 20 20 37  180 */   757,  7
2fde5 35 37 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  57,  958,  958, 
2fde6 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fde7 2c 20 20 37 35 31 2c 20 20 37 36 31 2c 20 20 39  ,  751,  761,  9
2fde8 35 30 2c 0a 20 2f 2a 20 20 20 31 39 30 20 2a 2f  50,. /*   190 */
2fde9 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 37     958,  958,  7
2fdea 31 37 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  17,  958,  958, 
2fdeb 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fdec 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f  ,  958,  958,. /
2fded 2a 20 20 20 32 30 30 20 2a 2f 20 20 20 36 34 32  *   200 */   642
2fdee 2c 20 20 37 34 39 2c 20 20 36 37 35 2c 20 20 37  ,  749,  675,  7
2fdef 35 39 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  59,  958,  958, 
2fdf0 20 39 35 38 2c 20 20 36 36 32 2c 20 20 37 33 38   958,  662,  738
2fdf1 2c 20 20 39 30 34 2c 0a 20 2f 2a 20 20 20 32 31  ,  904,. /*   21
2fdf2 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 32 33  0 */   958,  923
2fdf3 2c 20 20 39 32 31 2c 20 20 37 34 30 2c 20 20 38  ,  921,  740,  8
2fdf4 30 32 2c 20 20 39 35 38 2c 20 20 37 34 39 2c 20  02,  958,  749, 
2fdf5 20 37 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   758,  958,  958
2fdf6 2c 0a 20 2f 2a 20 20 20 32 32 30 20 2a 2f 20 20  ,. /*   220 */  
2fdf7 20 39 35 38 2c 20 20 38 36 35 2c 20 20 37 38 36   958,  865,  786
2fdf8 2c 20 20 37 38 36 2c 20 20 37 37 34 2c 20 20 37  ,  786,  774,  7
2fdf9 38 36 2c 20 20 36 39 36 2c 20 20 37 38 36 2c 20  86,  696,  786, 
2fdfa 20 39 35 38 2c 20 20 37 38 36 2c 0a 20 2f 2a 20   958,  786,. /* 
2fdfb 20 20 32 33 30 20 2a 2f 20 20 20 39 35 38 2c 20    230 */   958, 
2fdfc 20 36 39 39 2c 20 20 39 31 36 2c 20 20 37 39 36   699,  916,  796
2fdfd 2c 20 20 39 31 36 2c 20 20 37 37 34 2c 20 20 37  ,  916,  774,  7
2fdfe 37 34 2c 20 20 37 39 36 2c 20 20 36 33 39 2c 20  74,  796,  639, 
2fdff 20 36 33 39 2c 0a 20 2f 2a 20 20 20 32 34 30 20   639,. /*   240 
2fe00 2a 2f 20 20 20 36 33 39 2c 20 20 36 33 39 2c 20  */   639,  639, 
2fe01 20 36 35 30 2c 20 20 36 35 30 2c 20 20 37 31 36   650,  650,  716
2fe02 2c 20 20 39 35 38 2c 20 20 37 39 36 2c 20 20 37  ,  958,  796,  7
2fe03 38 37 2c 20 20 37 38 39 2c 20 20 37 37 39 2c 0a  87,  789,  779,.
2fe04 20 2f 2a 20 20 20 32 35 30 20 2a 2f 20 20 20 37   /*   250 */   7
2fe05 39 31 2c 20 20 39 35 38 2c 20 20 37 36 35 2c 20  91,  958,  765, 
2fe06 20 37 36 35 2c 20 20 37 37 33 2c 20 20 37 37 38   765,  773,  778
2fe07 2c 20 20 37 37 33 2c 20 20 37 37 38 2c 20 20 37  ,  773,  778,  7
2fe08 37 33 2c 20 20 37 37 38 2c 0a 20 2f 2a 20 20 20  73,  778,. /*   
2fe09 32 36 30 20 2a 2f 20 20 20 37 32 38 2c 20 20 37  260 */   728,  7
2fe0a 32 38 2c 20 20 37 31 33 2c 20 20 37 32 38 2c 20  28,  713,  728, 
2fe0b 20 36 39 39 2c 20 20 37 32 38 2c 20 20 38 37 35   699,  728,  875
2fe0c 2c 20 20 38 37 39 2c 20 20 38 37 39 2c 20 20 37  ,  879,  879,  7
2fe0d 31 33 2c 0a 20 2f 2a 20 20 20 32 37 30 20 2a 2f  13,. /*   270 */
2fe0e 20 20 20 37 32 38 2c 20 20 37 32 38 2c 20 20 37     728,  728,  7
2fe0f 32 38 2c 20 20 38 37 35 2c 20 20 36 35 37 2c 20  28,  875,  657, 
2fe10 20 37 36 35 2c 20 20 36 35 37 2c 20 20 37 36 35   765,  657,  765
2fe11 2c 20 20 36 35 37 2c 20 20 37 36 35 2c 0a 20 2f  ,  657,  765,. /
2fe12 2a 20 20 20 32 38 30 20 2a 2f 20 20 20 37 36 35  *   280 */   765
2fe13 2c 20 20 39 30 38 2c 20 20 39 31 30 2c 20 20 36  ,  908,  910,  6
2fe14 35 37 2c 20 20 37 36 35 2c 20 20 37 33 30 2c 20  57,  765,  730, 
2fe15 20 37 33 30 2c 20 20 38 30 38 2c 20 20 37 39 36   730,  808,  796
2fe16 2c 20 20 37 36 35 2c 0a 20 2f 2a 20 20 20 32 39  ,  765,. /*   29
2fe17 30 20 2a 2f 20 20 20 37 33 37 2c 20 20 37 33 37  0 */   737,  737
2fe18 2c 20 20 37 33 37 2c 20 20 37 33 37 2c 20 20 37  ,  737,  737,  7
2fe19 39 36 2c 20 20 37 33 30 2c 20 20 38 30 38 2c 20  96,  730,  808, 
2fe1a 20 37 36 35 2c 20 20 39 33 35 2c 20 20 39 33 35   765,  935,  935
2fe1b 2c 0a 20 2f 2a 20 20 20 33 30 30 20 2a 2f 20 20  ,. /*   300 */  
2fe1c 20 37 36 35 2c 20 20 37 36 35 2c 20 20 39 34 33   765,  765,  943
2fe1d 2c 20 20 36 38 33 2c 20 20 37 30 31 2c 20 20 37  ,  683,  701,  7
2fe1e 30 31 2c 20 20 39 35 30 2c 20 20 39 35 35 2c 20  01,  950,  955, 
2fe1f 20 39 35 35 2c 20 20 39 35 38 2c 0a 20 2f 2a 20   955,  958,. /* 
2fe20 20 20 33 31 30 20 2a 2f 20 20 20 39 35 38 2c 20    310 */   958, 
2fe21 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fe22 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fe23 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
2fe24 20 39 35 38 2c 0a 20 2f 2a 20 20 20 33 32 30 20   958,. /*   320 
2fe25 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20  */   958,  958, 
2fe26 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fe27 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fe28 35 38 2c 20 20 38 38 34 2c 20 20 39 35 38 2c 0a  58,  884,  958,.
2fe29 20 2f 2a 20 20 20 33 33 30 20 2a 2f 20 20 20 39   /*   330 */   9
2fe2a 35 38 2c 20 20 36 34 38 2c 20 20 39 35 38 2c 20  58,  648,  958, 
2fe2b 20 36 36 37 2c 20 20 38 31 35 2c 20 20 38 32 30   667,  815,  820
2fe2c 2c 20 20 38 31 36 2c 20 20 39 35 38 2c 20 20 38  ,  816,  958,  8
2fe2d 31 37 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20  17,  958,. /*   
2fe2e 33 34 30 20 2a 2f 20 20 20 37 34 33 2c 20 20 39  340 */   743,  9
2fe2f 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
2fe30 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fe31 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fe32 35 38 2c 0a 20 2f 2a 20 20 20 33 35 30 20 2a 2f  58,. /*   350 */
2fe33 20 20 20 39 35 38 2c 20 20 38 36 38 2c 20 20 39     958,  868,  9
2fe34 35 38 2c 20 20 37 38 30 2c 20 20 39 35 38 2c 20  58,  780,  958, 
2fe35 20 37 38 38 2c 20 20 39 35 38 2c 20 20 39 35 38   788,  958,  958
2fe36 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f  ,  958,  958,. /
2fe37 2a 20 20 20 33 36 30 20 2a 2f 20 20 20 39 35 38  *   360 */   958
2fe38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fe39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
2fe3a 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fe3b 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20 33 37  ,  958,. /*   37
2fe3c 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38  0 */   958,  958
2fe3d 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fe3e 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
2fe3f 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fe40 2c 0a 20 2f 2a 20 20 20 33 38 30 20 2a 2f 20 20  ,. /*   380 */  
2fe41 20 39 35 38 2c 20 20 39 30 36 2c 20 20 39 30 37   958,  906,  907
2fe42 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fe43 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
2fe44 20 39 35 38 2c 20 20 39 31 34 2c 0a 20 2f 2a 20   958,  914,. /* 
2fe45 20 20 33 39 30 20 2a 2f 20 20 20 39 35 38 2c 20    390 */   958, 
2fe46 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fe47 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fe48 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
2fe49 20 39 35 38 2c 0a 20 2f 2a 20 20 20 34 30 30 20   958,. /*   400 
2fe4a 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20  */   958,  958, 
2fe4b 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
2fe4c 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
2fe4d 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a  58,  958,  958,.
2fe4e 20 2f 2a 20 20 20 34 31 30 20 2a 2f 20 20 20 39   /*   410 */   9
2fe4f 34 32 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  42,  958,  958, 
2fe50 20 39 34 35 2c 20 20 36 33 35 2c 20 20 39 35 38   945,  635,  958
2fe51 2c 20 20 36 33 30 2c 20 20 36 33 32 2c 20 20 36  ,  630,  632,  6
2fe52 33 33 2c 20 20 36 33 37 2c 0a 20 2f 2a 20 20 20  33,  637,. /*   
2fe53 34 32 30 20 2a 2f 20 20 20 36 33 38 2c 20 20 36  420 */   638,  6
2fe54 34 31 2c 20 20 36 36 37 2c 20 20 36 36 38 2c 20  41,  667,  668, 
2fe55 20 36 37 30 2c 20 20 36 37 31 2c 20 20 36 37 32   670,  671,  672
2fe56 2c 20 20 36 34 33 2c 20 20 36 34 34 2c 20 20 36  ,  643,  644,  6
2fe57 34 35 2c 0a 20 2f 2a 20 20 20 34 33 30 20 2a 2f  45,. /*   430 */
2fe58 20 20 20 36 34 36 2c 20 20 36 34 37 2c 20 20 36     646,  647,  6
2fe59 34 39 2c 20 20 36 35 33 2c 20 20 36 35 31 2c 20  49,  653,  651, 
2fe5a 20 36 35 32 2c 20 20 36 35 34 2c 20 20 36 36 31   652,  654,  661
2fe5b 2c 20 20 36 36 33 2c 20 20 36 38 32 2c 0a 20 2f  ,  663,  682,. /
2fe5c 2a 20 20 20 34 34 30 20 2a 2f 20 20 20 36 38 34  *   440 */   684
2fe5d 2c 20 20 36 38 36 2c 20 20 37 34 37 2c 20 20 37  ,  686,  747,  7
2fe5e 34 38 2c 20 20 38 31 32 2c 20 20 37 34 31 2c 20  48,  812,  741, 
2fe5f 20 37 34 32 2c 20 20 37 34 36 2c 20 20 36 36 39   742,  746,  669
2fe60 2c 20 20 38 32 33 2c 0a 20 2f 2a 20 20 20 34 35  ,  823,. /*   45
2fe61 30 20 2a 2f 20 20 20 38 31 34 2c 20 20 38 31 38  0 */   814,  818
2fe62 2c 20 20 38 31 39 2c 20 20 38 32 31 2c 20 20 38  ,  819,  821,  8
2fe63 32 32 2c 20 20 38 33 36 2c 20 20 38 33 37 2c 20  22,  836,  837, 
2fe64 20 38 33 39 2c 20 20 38 34 35 2c 20 20 38 35 32   839,  845,  852
2fe65 2c 0a 20 2f 2a 20 20 20 34 36 30 20 2a 2f 20 20  ,. /*   460 */  
2fe66 20 38 35 35 2c 20 20 38 33 38 2c 20 20 38 34 33   855,  838,  843
2fe67 2c 20 20 38 34 34 2c 20 20 38 34 36 2c 20 20 38  ,  844,  846,  8
2fe68 35 31 2c 20 20 38 35 34 2c 20 20 37 34 34 2c 20  51,  854,  744, 
2fe69 20 37 34 35 2c 20 20 38 35 38 2c 0a 20 2f 2a 20   745,  858,. /* 
2fe6a 20 20 34 37 30 20 2a 2f 20 20 20 36 37 36 2c 20    470 */   676, 
2fe6b 20 36 37 37 2c 20 20 36 38 30 2c 20 20 36 38 31   677,  680,  681
2fe6c 2c 20 20 38 39 34 2c 20 20 38 39 36 2c 20 20 38  ,  894,  896,  8
2fe6d 39 35 2c 20 20 38 39 37 2c 20 20 36 37 39 2c 20  95,  897,  679, 
2fe6e 20 36 37 38 2c 0a 20 2f 2a 20 20 20 34 38 30 20   678,. /*   480 
2fe6f 2a 2f 20 20 20 38 32 34 2c 20 20 38 32 37 2c 20  */   824,  827, 
2fe70 20 38 36 30 2c 20 20 38 36 31 2c 20 20 39 32 34   860,  861,  924
2fe71 2c 20 20 39 32 35 2c 20 20 39 32 36 2c 20 20 39  ,  925,  926,  9
2fe72 32 37 2c 20 20 39 32 38 2c 20 20 38 35 36 2c 0a  27,  928,  856,.
2fe73 20 2f 2a 20 20 20 34 39 30 20 2a 2f 20 20 20 37   /*   490 */   7
2fe74 36 36 2c 20 20 38 35 39 2c 20 20 38 34 32 2c 20  66,  859,  842, 
2fe75 20 37 38 31 2c 20 20 37 38 34 2c 20 20 37 38 35   781,  784,  785
2fe76 2c 20 20 37 38 32 2c 20 20 37 35 30 2c 20 20 37  ,  782,  750,  7
2fe77 36 30 2c 20 20 37 36 38 2c 0a 20 2f 2a 20 20 20  60,  768,. /*   
2fe78 35 30 30 20 2a 2f 20 20 20 37 36 39 2c 20 20 37  500 */   769,  7
2fe79 37 30 2c 20 20 37 37 31 2c 20 20 37 35 35 2c 20  70,  771,  755, 
2fe7a 20 37 35 36 2c 20 20 37 36 32 2c 20 20 37 37 37   756,  762,  777
2fe7b 2c 20 20 38 31 30 2c 20 20 38 31 31 2c 20 20 37  ,  810,  811,  7
2fe7c 37 35 2c 0a 20 2f 2a 20 20 20 35 31 30 20 2a 2f  75,. /*   510 */
2fe7d 20 20 20 37 37 36 2c 20 20 37 36 33 2c 20 20 37     776,  763,  7
2fe7e 36 34 2c 20 20 37 35 32 2c 20 20 37 35 33 2c 20  64,  752,  753, 
2fe7f 20 37 35 34 2c 20 20 38 35 37 2c 20 20 38 31 33   754,  857,  813
2fe80 2c 20 20 38 32 35 2c 20 20 38 32 36 2c 0a 20 2f  ,  825,  826,. /
2fe81 2a 20 20 20 35 32 30 20 2a 2f 20 20 20 36 38 37  *   520 */   687
2fe82 2c 20 20 36 38 38 2c 20 20 38 32 30 2c 20 20 36  ,  688,  820,  6
2fe83 38 39 2c 20 20 36 39 30 2c 20 20 36 39 31 2c 20  89,  690,  691, 
2fe84 20 37 32 39 2c 20 20 37 33 32 2c 20 20 37 33 33   729,  732,  733
2fe85 2c 20 20 37 33 34 2c 0a 20 2f 2a 20 20 20 35 33  ,  734,. /*   53
2fe86 30 20 2a 2f 20 20 20 36 39 32 2c 20 20 37 31 31  0 */   692,  711
2fe87 2c 20 20 37 31 34 2c 20 20 37 31 35 2c 20 20 36  ,  714,  715,  6
2fe88 39 33 2c 20 20 37 30 30 2c 20 20 36 39 34 2c 20  93,  700,  694, 
2fe89 20 36 39 35 2c 20 20 37 30 32 2c 20 20 37 30 33   695,  702,  703
2fe8a 2c 0a 20 2f 2a 20 20 20 35 34 30 20 2a 2f 20 20  ,. /*   540 */  
2fe8b 20 37 30 34 2c 20 20 37 30 37 2c 20 20 37 30 38   704,  707,  708
2fe8c 2c 20 20 37 30 39 2c 20 20 37 31 30 2c 20 20 37  ,  709,  710,  7
2fe8d 30 35 2c 20 20 37 30 36 2c 20 20 38 37 36 2c 20  05,  706,  876, 
2fe8e 20 38 37 37 2c 20 20 38 38 30 2c 0a 20 2f 2a 20   877,  880,. /* 
2fe8f 20 20 35 35 30 20 2a 2f 20 20 20 38 37 38 2c 20    550 */   878, 
2fe90 20 36 39 37 2c 20 20 36 39 38 2c 20 20 37 31 32   697,  698,  712
2fe91 2c 20 20 36 38 35 2c 20 20 36 37 34 2c 20 20 36  ,  685,  674,  6
2fe92 36 36 2c 20 20 37 31 38 2c 20 20 37 32 31 2c 20  66,  718,  721, 
2fe93 20 37 32 32 2c 0a 20 2f 2a 20 20 20 35 36 30 20   722,. /*   560 
2fe94 2a 2f 20 20 20 37 32 33 2c 20 20 37 32 34 2c 20  */   723,  724, 
2fe95 20 37 32 35 2c 20 20 37 32 37 2c 20 20 37 31 39   725,  727,  719
2fe96 2c 20 20 37 32 30 2c 20 20 36 36 34 2c 20 20 36  ,  720,  664,  6
2fe97 35 35 2c 20 20 36 35 38 2c 20 20 37 36 37 2c 0a  55,  658,  767,.
2fe98 20 2f 2a 20 20 20 35 37 30 20 2a 2f 20 20 20 39   /*   570 */   9
2fe99 30 30 2c 20 20 39 30 39 2c 20 20 39 30 35 2c 20  00,  909,  905, 
2fe9a 20 39 30 31 2c 20 20 39 30 32 2c 20 20 39 30 33   901,  902,  903
2fe9b 2c 20 20 36 35 39 2c 20 20 38 37 32 2c 20 20 38  ,  659,  872,  8
2fe9c 37 33 2c 20 20 37 33 31 2c 0a 20 2f 2a 20 20 20  73,  731,. /*   
2fe9d 35 38 30 20 2a 2f 20 20 20 38 30 34 2c 20 20 38  580 */   804,  8
2fe9e 30 35 2c 20 20 38 39 39 2c 20 20 39 31 32 2c 20  05,  899,  912, 
2fe9f 20 39 31 35 2c 20 20 39 31 37 2c 20 20 39 31 38   915,  917,  918
2fea0 2c 20 20 39 31 39 2c 20 20 38 30 39 2c 20 20 39  ,  919,  809,  9
2fea1 32 30 2c 0a 20 2f 2a 20 20 20 35 39 30 20 2a 2f  20,. /*   590 */
2fea2 20 20 20 39 32 32 2c 20 20 39 31 33 2c 20 20 39     922,  913,  9
2fea3 34 37 2c 20 20 36 35 36 2c 20 20 37 33 35 2c 20  47,  656,  735, 
2fea4 20 37 33 36 2c 20 20 37 33 39 2c 20 20 38 38 31   736,  739,  881
2fea5 2c 20 20 39 32 39 2c 20 20 37 39 35 2c 0a 20 2f  ,  929,  795,. /
2fea6 2a 20 20 20 36 30 30 20 2a 2f 20 20 20 37 39 38  *   600 */   798
2fea7 2c 20 20 38 30 31 2c 20 20 38 30 33 2c 20 20 38  ,  801,  803,  8
2fea8 38 33 2c 20 20 38 38 35 2c 20 20 38 38 37 2c 20  83,  885,  887, 
2fea9 20 38 38 39 2c 20 20 38 39 30 2c 20 20 38 39 31   889,  890,  891
2feaa 2c 20 20 38 39 32 2c 0a 20 2f 2a 20 20 20 36 31  ,  892,. /*   61
2feab 30 20 2a 2f 20 20 20 38 39 33 2c 20 20 38 38 36  0 */   893,  886
2feac 2c 20 20 38 38 38 2c 20 20 39 33 30 2c 20 20 39  ,  888,  930,  9
2fead 33 34 2c 20 20 39 33 37 2c 20 20 39 33 39 2c 20  34,  937,  939, 
2feae 20 39 34 30 2c 20 20 39 34 31 2c 20 20 39 34 34   940,  941,  944
2feaf 2c 0a 20 2f 2a 20 20 20 36 32 30 20 2a 2f 20 20  ,. /*   620 */  
2feb0 20 39 34 36 2c 20 20 39 35 31 2c 20 20 39 35 32   946,  951,  952
2feb1 2c 20 20 39 35 33 2c 20 20 39 35 36 2c 20 20 39  ,  953,  956,  9
2feb2 35 37 2c 20 20 39 35 34 2c 20 20 36 33 36 2c 20  57,  954,  636, 
2feb3 20 36 33 31 2c 0a 7d 3b 0a 23 64 65 66 69 6e 65   631,.};.#define
2feb4 20 59 59 5f 53 5a 5f 41 43 54 54 41 42 20 28 69   YY_SZ_ACTTAB (i
2feb5 6e 74 29 28 73 69 7a 65 6f 66 28 79 79 5f 61 63  nt)(sizeof(yy_ac
2feb6 74 69 6f 6e 29 2f 73 69 7a 65 6f 66 28 79 79 5f  tion)/sizeof(yy_
2feb7 61 63 74 69 6f 6e 5b 30 5d 29 29 0a 0a 2f 2a 20  action[0]))../* 
2feb8 54 68 65 20 6e 65 78 74 20 74 61 62 6c 65 20 6d  The next table m
2feb9 61 70 73 20 74 6f 6b 65 6e 73 20 69 6e 74 6f 20  aps tokens into 
2feba 66 61 6c 6c 62 61 63 6b 20 74 6f 6b 65 6e 73 2e  fallback tokens.
2febb 20 20 49 66 20 61 20 63 6f 6e 73 74 72 75 63 74    If a construct
2febc 0a 2a 2a 20 6c 69 6b 65 20 74 68 65 20 66 6f 6c  .** like the fol
2febd 6c 6f 77 69 6e 67 3a 0a 2a 2a 20 0a 2a 2a 20 20  lowing:.** .**  
2febe 20 20 20 20 25 66 61 6c 6c 62 61 63 6b 20 49 44      %fallback ID
2febf 20 58 20 59 20 5a 2e 0a 2a 2a 0a 2a 2a 20 61 70   X Y Z..**.** ap
2fec0 70 65 61 72 73 20 69 6e 20 74 68 65 20 67 72 61  pears in the gra
2fec1 6d 6d 61 72 2c 20 74 68 65 6e 20 49 44 20 62 65  mmar, then ID be
2fec2 63 6f 6d 65 73 20 61 20 66 61 6c 6c 62 61 63 6b  comes a fallback
2fec3 20 74 6f 6b 65 6e 20 66 6f 72 20 58 2c 20 59 2c   token for X, Y,
2fec4 0a 2a 2a 20 61 6e 64 20 5a 2e 20 20 57 68 65 6e  .** and Z.  When
2fec5 65 76 65 72 20 6f 6e 65 20 6f 66 20 74 68 65 20  ever one of the 
2fec6 74 6f 6b 65 6e 73 20 58 2c 20 59 2c 20 6f 72 20  tokens X, Y, or 
2fec7 5a 20 69 73 20 69 6e 70 75 74 20 74 6f 20 74 68  Z is input to th
2fec8 65 20 70 61 72 73 65 72 0a 2a 2a 20 62 75 74 20  e parser.** but 
2fec9 69 74 20 64 6f 65 73 20 6e 6f 74 20 70 61 72 73  it does not pars
2feca 65 2c 20 74 68 65 20 74 79 70 65 20 6f 66 20 74  e, the type of t
2fecb 68 65 20 74 6f 6b 65 6e 20 69 73 20 63 68 61 6e  he token is chan
2fecc 67 65 64 20 74 6f 20 49 44 20 61 6e 64 0a 2a 2a  ged to ID and.**
2fecd 20 74 68 65 20 70 61 72 73 65 20 69 73 20 72 65   the parse is re
2fece 74 72 69 65 64 20 62 65 66 6f 72 65 20 61 6e 20  tried before an 
2fecf 65 72 72 6f 72 20 69 73 20 74 68 72 6f 77 6e 2e  error is thrown.
2fed0 0a 2a 2f 0a 23 69 66 64 65 66 20 59 59 46 41 4c  .*/.#ifdef YYFAL
2fed1 4c 42 41 43 4b 0a 73 74 61 74 69 63 20 63 6f 6e  LBACK.static con
2fed2 73 74 20 59 59 43 4f 44 45 54 59 50 45 20 79 79  st YYCODETYPE yy
2fed3 46 61 6c 6c 62 61 63 6b 5b 5d 20 3d 20 7b 0a 20  Fallback[] = {. 
2fed4 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2fed5 20 20 20 24 20 3d 3e 20 6e 6f 74 68 69 6e 67 20     $ => nothing 
2fed6 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2fed7 20 20 20 20 53 45 4d 49 20 3d 3e 20 6e 6f 74 68      SEMI => noth
2fed8 69 6e 67 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f  ing */.   26,  /
2fed9 2a 20 20 20 20 45 58 50 4c 41 49 4e 20 3d 3e 20  *    EXPLAIN => 
2feda 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
2fedb 20 20 20 20 20 20 51 55 45 52 59 20 3d 3e 20 49        QUERY => I
2fedc 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
2fedd 20 20 20 20 20 20 50 4c 41 4e 20 3d 3e 20 49 44        PLAN => ID
2fede 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
2fedf 20 20 20 20 42 45 47 49 4e 20 3d 3e 20 49 44 20      BEGIN => ID 
2fee0 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 54 52  */.    0,  /* TR
2fee1 41 4e 53 41 43 54 49 4f 4e 20 3d 3e 20 6e 6f 74  ANSACTION => not
2fee2 68 69 6e 67 20 2a 2f 0a 20 20 20 32 36 2c 20 20  hing */.   26,  
2fee3 2f 2a 20 20 20 44 45 46 45 52 52 45 44 20 3d 3e  /*   DEFERRED =>
2fee4 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
2fee5 2a 20 20 49 4d 4d 45 44 49 41 54 45 20 3d 3e 20  *  IMMEDIATE => 
2fee6 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
2fee7 20 20 45 58 43 4c 55 53 49 56 45 20 3d 3e 20 49    EXCLUSIVE => I
2fee8 44 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20  D */.    0,  /* 
2fee9 20 20 20 20 43 4f 4d 4d 49 54 20 3d 3e 20 6e 6f      COMMIT => no
2feea 74 68 69 6e 67 20 2a 2f 0a 20 20 20 32 36 2c 20  thing */.   26, 
2feeb 20 2f 2a 20 20 20 20 20 20 20 20 45 4e 44 20 3d   /*        END =
2feec 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
2feed 2f 2a 20 20 20 52 4f 4c 4c 42 41 43 4b 20 3d 3e  /*   ROLLBACK =>
2feee 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
2feef 2a 20 20 53 41 56 45 50 4f 49 4e 54 20 3d 3e 20  *  SAVEPOINT => 
2fef0 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
2fef1 20 20 20 20 52 45 4c 45 41 53 45 20 3d 3e 20 49      RELEASE => I
2fef2 44 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20  D */.    0,  /* 
2fef3 20 20 20 20 20 20 20 20 54 4f 20 3d 3e 20 6e 6f          TO => no
2fef4 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20  thing */.    0, 
2fef5 20 2f 2a 20 20 20 20 20 20 54 41 42 4c 45 20 3d   /*      TABLE =
2fef6 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20  > nothing */.   
2fef7 20 30 2c 20 20 2f 2a 20 20 20 20 20 43 52 45 41   0,  /*     CREA
2fef8 54 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f  TE => nothing */
2fef9 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20  .   26,  /*     
2fefa 20 20 20 20 49 46 20 3d 3e 20 49 44 20 2a 2f 0a      IF => ID */.
2fefb 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20      0,  /*      
2fefc 20 20 4e 4f 54 20 3d 3e 20 6e 6f 74 68 69 6e 67    NOT => nothing
2fefd 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
2fefe 20 20 20 45 58 49 53 54 53 20 3d 3e 20 6e 6f 74     EXISTS => not
2feff 68 69 6e 67 20 2a 2f 0a 20 20 20 32 36 2c 20 20  hing */.   26,  
2ff00 2f 2a 20 20 20 20 20 20 20 54 45 4d 50 20 3d 3e  /*       TEMP =>
2ff01 20 49 44 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f   ID */.    0,  /
2ff02 2a 20 20 20 20 20 20 20 20 20 4c 50 20 3d 3e 20  *         LP => 
2ff03 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2ff04 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 52 50  ,  /*         RP
2ff05 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2ff06 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2ff07 20 20 41 53 20 3d 3e 20 6e 6f 74 68 69 6e 67 20    AS => nothing 
2ff08 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2ff09 20 20 20 43 4f 4d 4d 41 20 3d 3e 20 6e 6f 74 68     COMMA => noth
2ff0a 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2ff0b 2a 20 20 20 20 20 20 20 20 20 49 44 20 3d 3e 20  *         ID => 
2ff0c 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2ff0d 2c 20 20 2f 2a 20 20 20 20 49 4e 44 45 58 45 44  ,  /*    INDEXED
2ff0e 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2ff0f 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 41    26,  /*      A
2ff10 42 4f 52 54 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  BORT => ID */.  
2ff11 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 41 46   26,  /*      AF
2ff12 54 45 52 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  TER => ID */.   
2ff13 32 36 2c 20 20 2f 2a 20 20 20 20 41 4e 41 4c 59  26,  /*    ANALY
2ff14 5a 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  ZE => ID */.   2
2ff15 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 41 53  6,  /*        AS
2ff16 43 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  C => ID */.   26
2ff17 2c 20 20 2f 2a 20 20 20 20 20 41 54 54 41 43 48  ,  /*     ATTACH
2ff18 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
2ff19 20 20 2f 2a 20 20 20 20 20 42 45 46 4f 52 45 20    /*     BEFORE 
2ff1a 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
2ff1b 20 2f 2a 20 20 20 20 20 20 20 20 20 42 59 20 3d   /*         BY =
2ff1c 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
2ff1d 2f 2a 20 20 20 20 43 41 53 43 41 44 45 20 3d 3e  /*    CASCADE =>
2ff1e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
2ff1f 2a 20 20 20 20 20 20 20 43 41 53 54 20 3d 3e 20  *       CAST => 
2ff20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
2ff21 20 20 20 43 4f 4c 55 4d 4e 4b 57 20 3d 3e 20 49     COLUMNKW => I
2ff22 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
2ff23 20 20 43 4f 4e 46 4c 49 43 54 20 3d 3e 20 49 44    CONFLICT => ID
2ff24 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
2ff25 20 44 41 54 41 42 41 53 45 20 3d 3e 20 49 44 20   DATABASE => ID 
2ff26 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
2ff27 20 20 20 20 44 45 53 43 20 3d 3e 20 49 44 20 2a      DESC => ID *
2ff28 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
2ff29 20 44 45 54 41 43 48 20 3d 3e 20 49 44 20 2a 2f   DETACH => ID */
2ff2a 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20  .   26,  /*     
2ff2b 20 20 45 41 43 48 20 3d 3e 20 49 44 20 2a 2f 0a    EACH => ID */.
2ff2c 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20     26,  /*      
2ff2d 20 46 41 49 4c 20 3d 3e 20 49 44 20 2a 2f 0a 20   FAIL => ID */. 
2ff2e 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20    26,  /*       
2ff2f 20 46 4f 52 20 3d 3e 20 49 44 20 2a 2f 0a 20 20   FOR => ID */.  
2ff30 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 49 47 4e   26,  /*     IGN
2ff31 4f 52 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  ORE => ID */.   
2ff32 32 36 2c 20 20 2f 2a 20 20 49 4e 49 54 49 41 4c  26,  /*  INITIAL
2ff33 4c 59 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  LY => ID */.   2
2ff34 36 2c 20 20 2f 2a 20 20 20 20 49 4e 53 54 45 41  6,  /*    INSTEA
2ff35 44 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  D => ID */.   26
2ff36 2c 20 20 2f 2a 20 20 20 20 4c 49 4b 45 5f 4b 57  ,  /*    LIKE_KW
2ff37 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
2ff38 20 20 2f 2a 20 20 20 20 20 20 4d 41 54 43 48 20    /*      MATCH 
2ff39 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
2ff3a 20 2f 2a 20 20 20 20 20 20 20 20 4b 45 59 20 3d   /*        KEY =
2ff3b 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
2ff3c 2f 2a 20 20 20 20 20 20 20 20 20 4f 46 20 3d 3e  /*         OF =>
2ff3d 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
2ff3e 2a 20 20 20 20 20 4f 46 46 53 45 54 20 3d 3e 20  *     OFFSET => 
2ff3f 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
2ff40 20 20 20 20 20 50 52 41 47 4d 41 20 3d 3e 20 49       PRAGMA => I
2ff41 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
2ff42 20 20 20 20 20 52 41 49 53 45 20 3d 3e 20 49 44       RAISE => ID
2ff43 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
2ff44 20 20 52 45 50 4c 41 43 45 20 3d 3e 20 49 44 20    REPLACE => ID 
2ff45 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
2ff46 52 45 53 54 52 49 43 54 20 3d 3e 20 49 44 20 2a  RESTRICT => ID *
2ff47 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
2ff48 20 20 20 20 52 4f 57 20 3d 3e 20 49 44 20 2a 2f      ROW => ID */
2ff49 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 54  .   26,  /*    T
2ff4a 52 49 47 47 45 52 20 3d 3e 20 49 44 20 2a 2f 0a  RIGGER => ID */.
2ff4b 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 56     26,  /*     V
2ff4c 41 43 55 55 4d 20 3d 3e 20 49 44 20 2a 2f 0a 20  ACUUM => ID */. 
2ff4d 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20    26,  /*       
2ff4e 56 49 45 57 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  VIEW => ID */.  
2ff4f 20 32 36 2c 20 20 2f 2a 20 20 20 20 56 49 52 54   26,  /*    VIRT
2ff50 55 41 4c 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  UAL => ID */.   
2ff51 32 36 2c 20 20 2f 2a 20 20 20 20 52 45 49 4e 44  26,  /*    REIND
2ff52 45 58 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  EX => ID */.   2
2ff53 36 2c 20 20 2f 2a 20 20 20 20 20 52 45 4e 41 4d  6,  /*     RENAM
2ff54 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  E => ID */.   26
2ff55 2c 20 20 2f 2a 20 20 20 43 54 49 4d 45 5f 4b 57  ,  /*   CTIME_KW
2ff56 20 3d 3e 20 49 44 20 2a 2f 0a 7d 3b 0a 23 65 6e   => ID */.};.#en
2ff57 64 69 66 20 2f 2a 20 59 59 46 41 4c 4c 42 41 43  dif /* YYFALLBAC
2ff58 4b 20 2a 2f 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c  K */../* The fol
2ff59 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
2ff5a 20 72 65 70 72 65 73 65 6e 74 73 20 61 20 73 69   represents a si
2ff5b 6e 67 6c 65 20 65 6c 65 6d 65 6e 74 20 6f 66 20  ngle element of 
2ff5c 74 68 65 0a 2a 2a 20 70 61 72 73 65 72 27 73 20  the.** parser's 
2ff5d 73 74 61 63 6b 2e 20 20 49 6e 66 6f 72 6d 61 74  stack.  Informat
2ff5e 69 6f 6e 20 73 74 6f 72 65 64 20 69 6e 63 6c 75  ion stored inclu
2ff5f 64 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2b 20 20  des:.**.**   +  
2ff60 54 68 65 20 73 74 61 74 65 20 6e 75 6d 62 65 72  The state number
2ff61 20 66 6f 72 20 74 68 65 20 70 61 72 73 65 72 20   for the parser 
2ff62 61 74 20 74 68 69 73 20 6c 65 76 65 6c 20 6f 66  at this level of
2ff63 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2a 0a 2a   the stack..**.*
2ff64 2a 20 20 20 2b 20 20 54 68 65 20 76 61 6c 75 65  *   +  The value
2ff65 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 73 74   of the token st
2ff66 6f 72 65 64 20 61 74 20 74 68 69 73 20 6c 65 76  ored at this lev
2ff67 65 6c 20 6f 66 20 74 68 65 20 73 74 61 63 6b 2e  el of the stack.
2ff68 0a 2a 2a 20 20 20 20 20 20 28 49 6e 20 6f 74 68  .**      (In oth
2ff69 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 22 6d  er words, the "m
2ff6a 61 6a 6f 72 22 20 74 6f 6b 65 6e 2e 29 0a 2a 2a  ajor" token.).**
2ff6b 0a 2a 2a 20 20 20 2b 20 20 54 68 65 20 73 65 6d  .**   +  The sem
2ff6c 61 6e 74 69 63 20 76 61 6c 75 65 20 73 74 6f 72  antic value stor
2ff6d 65 64 20 61 74 20 74 68 69 73 20 6c 65 76 65 6c  ed at this level
2ff6e 20 6f 66 20 74 68 65 20 73 74 61 63 6b 2e 20 20   of the stack.  
2ff6f 54 68 69 73 20 69 73 0a 2a 2a 20 20 20 20 20 20  This is.**      
2ff70 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  the information 
2ff71 75 73 65 64 20 62 79 20 74 68 65 20 61 63 74 69  used by the acti
2ff72 6f 6e 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74  on routines in t
2ff73 68 65 20 67 72 61 6d 6d 61 72 2e 0a 2a 2a 20 20  he grammar..**  
2ff74 20 20 20 20 49 74 20 69 73 20 73 6f 6d 65 74 69      It is someti
2ff75 6d 65 73 20 63 61 6c 6c 65 64 20 74 68 65 20 22  mes called the "
2ff76 6d 69 6e 6f 72 22 20 74 6f 6b 65 6e 2e 0a 2a 2f  minor" token..*/
2ff77 0a 73 74 72 75 63 74 20 79 79 53 74 61 63 6b 45  .struct yyStackE
2ff78 6e 74 72 79 20 7b 0a 20 20 59 59 41 43 54 49 4f  ntry {.  YYACTIO
2ff79 4e 54 59 50 45 20 73 74 61 74 65 6e 6f 3b 20 20  NTYPE stateno;  
2ff7a 2f 2a 20 54 68 65 20 73 74 61 74 65 2d 6e 75 6d  /* The state-num
2ff7b 62 65 72 20 2a 2f 0a 20 20 59 59 43 4f 44 45 54  ber */.  YYCODET
2ff7c 59 50 45 20 6d 61 6a 6f 72 3b 20 20 20 20 20 20  YPE major;      
2ff7d 2f 2a 20 54 68 65 20 6d 61 6a 6f 72 20 74 6f 6b  /* The major tok
2ff7e 65 6e 20 76 61 6c 75 65 2e 20 20 54 68 69 73 20  en value.  This 
2ff7f 69 73 20 74 68 65 20 63 6f 64 65 0a 20 20 20 20  is the code.    
2ff80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ff81 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 66       ** number f
2ff82 6f 72 20 74 68 65 20 74 6f 6b 65 6e 20 61 74 20  or the token at 
2ff83 74 68 69 73 20 73 74 61 63 6b 20 6c 65 76 65 6c  this stack level
2ff84 20 2a 2f 0a 20 20 59 59 4d 49 4e 4f 52 54 59 50   */.  YYMINORTYP
2ff85 45 20 6d 69 6e 6f 72 3b 20 20 20 20 20 2f 2a 20  E minor;     /* 
2ff86 54 68 65 20 75 73 65 72 2d 73 75 70 70 6c 69 65  The user-supplie
2ff87 64 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e 20 76 61  d minor token va
2ff88 6c 75 65 2e 20 20 54 68 69 73 0a 20 20 20 20 20  lue.  This.     
2ff89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ff8a 20 20 20 20 2a 2a 20 69 73 20 74 68 65 20 76 61      ** is the va
2ff8b 6c 75 65 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e  lue of the token
2ff8c 20 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20    */.};.typedef 
2ff8d 73 74 72 75 63 74 20 79 79 53 74 61 63 6b 45 6e  struct yyStackEn
2ff8e 74 72 79 20 79 79 53 74 61 63 6b 45 6e 74 72 79  try yyStackEntry
2ff8f 3b 0a 0a 2f 2a 20 54 68 65 20 73 74 61 74 65 20  ;../* The state 
2ff90 6f 66 20 74 68 65 20 70 61 72 73 65 72 20 69 73  of the parser is
2ff91 20 63 6f 6d 70 6c 65 74 65 6c 79 20 63 6f 6e 74   completely cont
2ff92 61 69 6e 65 64 20 69 6e 20 61 6e 20 69 6e 73 74  ained in an inst
2ff93 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 65 20 66  ance of.** the f
2ff94 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
2ff95 72 65 20 2a 2f 0a 73 74 72 75 63 74 20 79 79 50  re */.struct yyP
2ff96 61 72 73 65 72 20 7b 0a 20 20 69 6e 74 20 79 79  arser {.  int yy
2ff97 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  idx;            
2ff98 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
2ff99 20 6f 66 20 74 6f 70 20 65 6c 65 6d 65 6e 74 20   of top element 
2ff9a 69 6e 20 73 74 61 63 6b 20 2a 2f 0a 23 69 66 64  in stack */.#ifd
2ff9b 65 66 20 59 59 54 52 41 43 4b 4d 41 58 53 54 41  ef YYTRACKMAXSTA
2ff9c 43 4b 44 45 50 54 48 0a 20 20 69 6e 74 20 79 79  CKDEPTH.  int yy
2ff9d 69 64 78 4d 61 78 3b 20 20 20 20 20 20 20 20 20  idxMax;         
2ff9e 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d          /* Maxim
2ff9f 75 6d 20 76 61 6c 75 65 20 6f 66 20 79 79 69 64  um value of yyid
2ffa0 78 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e  x */.#endif.  in
2ffa1 74 20 79 79 65 72 72 63 6e 74 3b 20 20 20 20 20  t yyerrcnt;     
2ffa2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
2ffa3 68 69 66 74 73 20 6c 65 66 74 20 62 65 66 6f 72  hifts left befor
2ffa4 65 20 6f 75 74 20 6f 66 20 74 68 65 20 65 72 72  e out of the err
2ffa5 6f 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50  or */.  sqlite3P
2ffa6 61 72 73 65 72 41 52 47 5f 53 44 45 43 4c 20 20  arserARG_SDECL  
2ffa7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2ffa8 20 41 20 70 6c 61 63 65 20 74 6f 20 68 6f 6c 64   A place to hold
2ffa9 20 25 65 78 74 72 61 5f 61 72 67 75 6d 65 6e 74   %extra_argument
2ffaa 20 2a 2f 0a 23 69 66 20 59 59 53 54 41 43 4b 44   */.#if YYSTACKD
2ffab 45 50 54 48 3c 3d 30 0a 20 20 69 6e 74 20 79 79  EPTH<=0.  int yy
2ffac 73 74 6b 73 7a 3b 20 20 20 20 20 20 20 20 20 20  stksz;          
2ffad 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65          /* Curre
2ffae 6e 74 20 73 69 64 65 20 6f 66 20 74 68 65 20 73  nt side of the s
2ffaf 74 61 63 6b 20 2a 2f 0a 20 20 79 79 53 74 61 63  tack */.  yyStac
2ffb0 6b 45 6e 74 72 79 20 2a 79 79 73 74 61 63 6b 3b  kEntry *yystack;
2ffb1 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
2ffb2 61 72 73 65 72 27 73 20 73 74 61 63 6b 20 2a 2f  arser's stack */
2ffb3 0a 23 65 6c 73 65 0a 20 20 79 79 53 74 61 63 6b  .#else.  yyStack
2ffb4 45 6e 74 72 79 20 79 79 73 74 61 63 6b 5b 59 59  Entry yystack[YY
2ffb5 53 54 41 43 4b 44 45 50 54 48 5d 3b 20 20 2f 2a  STACKDEPTH];  /*
2ffb6 20 54 68 65 20 70 61 72 73 65 72 27 73 20 73 74   The parser's st
2ffb7 61 63 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b  ack */.#endif.};
2ffb8 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
2ffb9 79 79 50 61 72 73 65 72 20 79 79 50 61 72 73 65  yyParser yyParse
2ffba 72 3b 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  r;..#ifndef NDEB
2ffbb 55 47 0a 73 74 61 74 69 63 20 46 49 4c 45 20 2a  UG.static FILE *
2ffbc 79 79 54 72 61 63 65 46 49 4c 45 20 3d 20 30 3b  yyTraceFILE = 0;
2ffbd 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 79 79  .static char *yy
2ffbe 54 72 61 63 65 50 72 6f 6d 70 74 20 3d 20 30 3b  TracePrompt = 0;
2ffbf 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55  .#endif /* NDEBU
2ffc0 47 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44  G */..#ifndef ND
2ffc1 45 42 55 47 0a 2f 2a 20 0a 2a 2a 20 54 75 72 6e  EBUG./* .** Turn
2ffc2 20 70 61 72 73 65 72 20 74 72 61 63 69 6e 67 20   parser tracing 
2ffc3 6f 6e 20 62 79 20 67 69 76 69 6e 67 20 61 20 73  on by giving a s
2ffc4 74 72 65 61 6d 20 74 6f 20 77 68 69 63 68 20 74  tream to which t
2ffc5 6f 20 77 72 69 74 65 20 74 68 65 20 74 72 61 63  o write the trac
2ffc6 65 0a 2a 2a 20 61 6e 64 20 61 20 70 72 6f 6d 70  e.** and a promp
2ffc7 74 20 74 6f 20 70 72 65 66 61 63 65 20 65 61 63  t to preface eac
2ffc8 68 20 74 72 61 63 65 20 6d 65 73 73 61 67 65 2e  h trace message.
2ffc9 20 20 54 72 61 63 69 6e 67 20 69 73 20 74 75 72    Tracing is tur
2ffca 6e 65 64 20 6f 66 66 0a 2a 2a 20 62 79 20 6d 61  ned off.** by ma
2ffcb 6b 69 6e 67 20 65 69 74 68 65 72 20 61 72 67 75  king either argu
2ffcc 6d 65 6e 74 20 4e 55 4c 4c 20 0a 2a 2a 0a 2a 2a  ment NULL .**.**
2ffcd 20 49 6e 70 75 74 73 3a 0a 2a 2a 20 3c 75 6c 3e   Inputs:.** <ul>
2ffce 0a 2a 2a 20 3c 6c 69 3e 20 41 20 46 49 4c 45 2a  .** <li> A FILE*
2ffcf 20 74 6f 20 77 68 69 63 68 20 74 72 61 63 65 20   to which trace 
2ffd0 6f 75 74 70 75 74 20 73 68 6f 75 6c 64 20 62 65  output should be
2ffd1 20 77 72 69 74 74 65 6e 2e 0a 2a 2a 20 20 20 20   written..**    
2ffd2 20 20 49 66 20 4e 55 4c 4c 2c 20 74 68 65 6e 20    If NULL, then 
2ffd3 74 72 61 63 69 6e 67 20 69 73 20 74 75 72 6e 65  tracing is turne
2ffd4 64 20 6f 66 66 2e 0a 2a 2a 20 3c 6c 69 3e 20 41  d off..** <li> A
2ffd5 20 70 72 65 66 69 78 20 73 74 72 69 6e 67 20 77   prefix string w
2ffd6 72 69 74 74 65 6e 20 61 74 20 74 68 65 20 62 65  ritten at the be
2ffd7 67 69 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72 79  ginning of every
2ffd8 0a 2a 2a 20 20 20 20 20 20 6c 69 6e 65 20 6f 66  .**      line of
2ffd9 20 74 72 61 63 65 20 6f 75 74 70 75 74 2e 20 20   trace output.  
2ffda 49 66 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 72  If NULL, then tr
2ffdb 61 63 69 6e 67 20 69 73 0a 2a 2a 20 20 20 20 20  acing is.**     
2ffdc 20 74 75 72 6e 65 64 20 6f 66 66 2e 0a 2a 2a 20   turned off..** 
2ffdd 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 4f 75 74 70  </ul>.**.** Outp
2ffde 75 74 73 3a 0a 2a 2a 20 4e 6f 6e 65 2e 0a 2a 2f  uts:.** None..*/
2ffdf 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2ffe0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72 73  void sqlite3Pars
2ffe1 65 72 54 72 61 63 65 28 46 49 4c 45 20 2a 54 72  erTrace(FILE *Tr
2ffe2 61 63 65 46 49 4c 45 2c 20 63 68 61 72 20 2a 7a  aceFILE, char *z
2ffe3 54 72 61 63 65 50 72 6f 6d 70 74 29 7b 0a 20 20  TracePrompt){.  
2ffe4 79 79 54 72 61 63 65 46 49 4c 45 20 3d 20 54 72  yyTraceFILE = Tr
2ffe5 61 63 65 46 49 4c 45 3b 0a 20 20 79 79 54 72 61  aceFILE;.  yyTra
2ffe6 63 65 50 72 6f 6d 70 74 20 3d 20 7a 54 72 61 63  cePrompt = zTrac
2ffe7 65 50 72 6f 6d 70 74 3b 0a 20 20 69 66 28 20 79  ePrompt;.  if( y
2ffe8 79 54 72 61 63 65 46 49 4c 45 3d 3d 30 20 29 20  yTraceFILE==0 ) 
2ffe9 79 79 54 72 61 63 65 50 72 6f 6d 70 74 20 3d 20  yyTracePrompt = 
2ffea 30 3b 0a 20 20 65 6c 73 65 20 69 66 28 20 79 79  0;.  else if( yy
2ffeb 54 72 61 63 65 50 72 6f 6d 70 74 3d 3d 30 20 29  TracePrompt==0 )
2ffec 20 79 79 54 72 61 63 65 46 49 4c 45 20 3d 20 30   yyTraceFILE = 0
2ffed 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44  ;.}.#endif /* ND
2ffee 45 42 55 47 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  EBUG */..#ifndef
2ffef 20 4e 44 45 42 55 47 0a 2f 2a 20 46 6f 72 20 74   NDEBUG./* For t
2fff0 72 61 63 69 6e 67 20 73 68 69 66 74 73 2c 20 74  racing shifts, t
2fff1 68 65 20 6e 61 6d 65 73 20 6f 66 20 61 6c 6c 20  he names of all 
2fff2 74 65 72 6d 69 6e 61 6c 73 20 61 6e 64 20 6e 6f  terminals and no
2fff3 6e 74 65 72 6d 69 6e 61 6c 73 0a 2a 2a 20 61 72  nterminals.** ar
2fff4 65 20 72 65 71 75 69 72 65 64 2e 20 20 54 68 65  e required.  The
2fff5 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65   following table
2fff6 20 73 75 70 70 6c 69 65 73 20 74 68 65 73 65 20   supplies these 
2fff7 6e 61 6d 65 73 20 2a 2f 0a 73 74 61 74 69 63 20  names */.static 
2fff8 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73  const char *cons
2fff9 74 20 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 5d 20  t yyTokenName[] 
2fffa 3d 20 7b 20 0a 20 20 22 24 22 2c 20 20 20 20 20  = { .  "$",     
2fffb 20 20 20 20 20 20 20 20 22 53 45 4d 49 22 2c 20          "SEMI", 
2fffc 20 20 20 20 20 20 20 20 20 22 45 58 50 4c 41 49           "EXPLAI
2fffd 4e 22 2c 20 20 20 20 20 20 20 22 51 55 45 52 59  N",       "QUERY
2fffe 22 2c 20 20 20 20 20 20 20 0a 20 20 22 50 4c 41  ",       .  "PLA
2ffff 4e 22 2c 20 20 20 20 20 20 20 20 20 20 22 42 45  N",          "BE
30000 47 49 4e 22 2c 20 20 20 20 20 20 20 20 20 22 54  GIN",         "T
30001 52 41 4e 53 41 43 54 49 4f 4e 22 2c 20 20 20 22  RANSACTION",   "
30002 44 45 46 45 52 52 45 44 22 2c 20 20 20 20 0a 20  DEFERRED",    . 
30003 20 22 49 4d 4d 45 44 49 41 54 45 22 2c 20 20 20   "IMMEDIATE",   
30004 20 20 22 45 58 43 4c 55 53 49 56 45 22 2c 20 20    "EXCLUSIVE",  
30005 20 20 20 22 43 4f 4d 4d 49 54 22 2c 20 20 20 20     "COMMIT",    
30006 20 20 20 20 22 45 4e 44 22 2c 20 20 20 20 20 20      "END",      
30007 20 20 20 0a 20 20 22 52 4f 4c 4c 42 41 43 4b 22     .  "ROLLBACK"
30008 2c 20 20 20 20 20 20 22 53 41 56 45 50 4f 49 4e  ,      "SAVEPOIN
30009 54 22 2c 20 20 20 20 20 22 52 45 4c 45 41 53 45  T",     "RELEASE
3000a 22 2c 20 20 20 20 20 20 20 22 54 4f 22 2c 20 20  ",       "TO",  
3000b 20 20 20 20 20 20 20 20 0a 20 20 22 54 41 42 4c          .  "TABL
3000c 45 22 2c 20 20 20 20 20 20 20 20 20 22 43 52 45  E",         "CRE
3000d 41 54 45 22 2c 20 20 20 20 20 20 20 20 22 49 46  ATE",        "IF
3000e 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 4e  ",            "N
3000f 4f 54 22 2c 20 20 20 20 20 20 20 20 20 0a 20 20  OT",         .  
30010 22 45 58 49 53 54 53 22 2c 20 20 20 20 20 20 20  "EXISTS",       
30011 20 22 54 45 4d 50 22 2c 20 20 20 20 20 20 20 20   "TEMP",        
30012 20 20 22 4c 50 22 2c 20 20 20 20 20 20 20 20 20    "LP",         
30013 20 20 20 22 52 50 22 2c 20 20 20 20 20 20 20 20     "RP",        
30014 20 20 0a 20 20 22 41 53 22 2c 20 20 20 20 20 20    .  "AS",      
30015 20 20 20 20 20 20 22 43 4f 4d 4d 41 22 2c 20 20        "COMMA",  
30016 20 20 20 20 20 20 20 22 49 44 22 2c 20 20 20 20         "ID",    
30017 20 20 20 20 20 20 20 20 22 49 4e 44 45 58 45 44          "INDEXED
30018 22 2c 20 20 20 20 20 0a 20 20 22 41 42 4f 52 54  ",     .  "ABORT
30019 22 2c 20 20 20 20 20 20 20 20 20 22 41 46 54 45  ",         "AFTE
3001a 52 22 2c 20 20 20 20 20 20 20 20 20 22 41 4e 41  R",         "ANA
3001b 4c 59 5a 45 22 2c 20 20 20 20 20 20 20 22 41 53  LYZE",       "AS
3001c 43 22 2c 20 20 20 20 20 20 20 20 20 0a 20 20 22  C",         .  "
3001d 41 54 54 41 43 48 22 2c 20 20 20 20 20 20 20 20  ATTACH",        
3001e 22 42 45 46 4f 52 45 22 2c 20 20 20 20 20 20 20  "BEFORE",       
3001f 20 22 42 59 22 2c 20 20 20 20 20 20 20 20 20 20   "BY",          
30020 20 20 22 43 41 53 43 41 44 45 22 2c 20 20 20 20    "CASCADE",    
30021 20 0a 20 20 22 43 41 53 54 22 2c 20 20 20 20 20   .  "CAST",     
30022 20 20 20 20 20 22 43 4f 4c 55 4d 4e 4b 57 22 2c       "COLUMNKW",
30023 20 20 20 20 20 20 22 43 4f 4e 46 4c 49 43 54 22        "CONFLICT"
30024 2c 20 20 20 20 20 20 22 44 41 54 41 42 41 53 45  ,      "DATABASE
30025 22 2c 20 20 20 20 0a 20 20 22 44 45 53 43 22 2c  ",    .  "DESC",
30026 20 20 20 20 20 20 20 20 20 20 22 44 45 54 41 43            "DETAC
30027 48 22 2c 20 20 20 20 20 20 20 20 22 45 41 43 48  H",        "EACH
30028 22 2c 20 20 20 20 20 20 20 20 20 20 22 46 41 49  ",          "FAI
30029 4c 22 2c 20 20 20 20 20 20 20 20 0a 20 20 22 46  L",        .  "F
3002a 4f 52 22 2c 20 20 20 20 20 20 20 20 20 20 20 22  OR",           "
3002b 49 47 4e 4f 52 45 22 2c 20 20 20 20 20 20 20 20  IGNORE",        
3002c 22 49 4e 49 54 49 41 4c 4c 59 22 2c 20 20 20 20  "INITIALLY",    
3002d 20 22 49 4e 53 54 45 41 44 22 2c 20 20 20 20 20   "INSTEAD",     
3002e 0a 20 20 22 4c 49 4b 45 5f 4b 57 22 2c 20 20 20  .  "LIKE_KW",   
3002f 20 20 20 20 22 4d 41 54 43 48 22 2c 20 20 20 20      "MATCH",    
30030 20 20 20 20 20 22 4b 45 59 22 2c 20 20 20 20 20       "KEY",     
30031 20 20 20 20 20 20 22 4f 46 22 2c 20 20 20 20 20        "OF",     
30032 20 20 20 20 20 0a 20 20 22 4f 46 46 53 45 54 22       .  "OFFSET"
30033 2c 20 20 20 20 20 20 20 20 22 50 52 41 47 4d 41  ,        "PRAGMA
30034 22 2c 20 20 20 20 20 20 20 20 22 52 41 49 53 45  ",        "RAISE
30035 22 2c 20 20 20 20 20 20 20 20 20 22 52 45 50 4c  ",         "REPL
30036 41 43 45 22 2c 20 20 20 20 20 0a 20 20 22 52 45  ACE",     .  "RE
30037 53 54 52 49 43 54 22 2c 20 20 20 20 20 20 22 52  STRICT",      "R
30038 4f 57 22 2c 20 20 20 20 20 20 20 20 20 20 20 22  OW",           "
30039 54 52 49 47 47 45 52 22 2c 20 20 20 20 20 20 20  TRIGGER",       
3003a 22 56 41 43 55 55 4d 22 2c 20 20 20 20 20 20 0a  "VACUUM",      .
3003b 20 20 22 56 49 45 57 22 2c 20 20 20 20 20 20 20    "VIEW",       
3003c 20 20 20 22 56 49 52 54 55 41 4c 22 2c 20 20 20     "VIRTUAL",   
3003d 20 20 20 20 22 52 45 49 4e 44 45 58 22 2c 20 20      "REINDEX",  
3003e 20 20 20 20 20 22 52 45 4e 41 4d 45 22 2c 20 20       "RENAME",  
3003f 20 20 20 20 0a 20 20 22 43 54 49 4d 45 5f 4b 57      .  "CTIME_KW
30040 22 2c 20 20 20 20 20 20 22 41 4e 59 22 2c 20 20  ",      "ANY",  
30041 20 20 20 20 20 20 20 20 20 22 4f 52 22 2c 20 20           "OR",  
30042 20 20 20 20 20 20 20 20 20 20 22 41 4e 44 22 2c            "AND",
30043 20 20 20 20 20 20 20 20 20 0a 20 20 22 49 53 22           .  "IS"
30044 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 42 45  ,            "BE
30045 54 57 45 45 4e 22 2c 20 20 20 20 20 20 20 22 49  TWEEN",       "I
30046 4e 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22  N",            "
30047 49 53 4e 55 4c 4c 22 2c 20 20 20 20 20 20 0a 20  ISNULL",      . 
30048 20 22 4e 4f 54 4e 55 4c 4c 22 2c 20 20 20 20 20   "NOTNULL",     
30049 20 20 22 4e 45 22 2c 20 20 20 20 20 20 20 20 20    "NE",         
3004a 20 20 20 22 45 51 22 2c 20 20 20 20 20 20 20 20     "EQ",        
3004b 20 20 20 20 22 47 54 22 2c 20 20 20 20 20 20 20      "GT",       
3004c 20 20 20 0a 20 20 22 4c 45 22 2c 20 20 20 20 20     .  "LE",     
3004d 20 20 20 20 20 20 20 22 4c 54 22 2c 20 20 20 20         "LT",    
3004e 20 20 20 20 20 20 20 20 22 47 45 22 2c 20 20 20          "GE",   
3004f 20 20 20 20 20 20 20 20 20 22 45 53 43 41 50 45           "ESCAPE
30050 22 2c 20 20 20 20 20 20 0a 20 20 22 42 49 54 41  ",      .  "BITA
30051 4e 44 22 2c 20 20 20 20 20 20 20 20 22 42 49 54  ND",        "BIT
30052 4f 52 22 2c 20 20 20 20 20 20 20 20 20 22 4c 53  OR",         "LS
30053 48 49 46 54 22 2c 20 20 20 20 20 20 20 20 22 52  HIFT",        "R
30054 53 48 49 46 54 22 2c 20 20 20 20 20 20 0a 20 20  SHIFT",      .  
30055 22 50 4c 55 53 22 2c 20 20 20 20 20 20 20 20 20  "PLUS",         
30056 20 22 4d 49 4e 55 53 22 2c 20 20 20 20 20 20 20   "MINUS",       
30057 20 20 22 53 54 41 52 22 2c 20 20 20 20 20 20 20    "STAR",       
30058 20 20 20 22 53 4c 41 53 48 22 2c 20 20 20 20 20     "SLASH",     
30059 20 20 0a 20 20 22 52 45 4d 22 2c 20 20 20 20 20    .  "REM",     
3005a 20 20 20 20 20 20 22 43 4f 4e 43 41 54 22 2c 20        "CONCAT", 
3005b 20 20 20 20 20 20 20 22 43 4f 4c 4c 41 54 45 22         "COLLATE"
3005c 2c 20 20 20 20 20 20 20 22 55 4d 49 4e 55 53 22  ,       "UMINUS"
3005d 2c 20 20 20 20 20 20 0a 20 20 22 55 50 4c 55 53  ,      .  "UPLUS
3005e 22 2c 20 20 20 20 20 20 20 20 20 22 42 49 54 4e  ",         "BITN
3005f 4f 54 22 2c 20 20 20 20 20 20 20 20 22 53 54 52  OT",        "STR
30060 49 4e 47 22 2c 20 20 20 20 20 20 20 20 22 4a 4f  ING",        "JO
30061 49 4e 5f 4b 57 22 2c 20 20 20 20 20 0a 20 20 22  IN_KW",     .  "
30062 43 4f 4e 53 54 52 41 49 4e 54 22 2c 20 20 20 20  CONSTRAINT",    
30063 22 44 45 46 41 55 4c 54 22 2c 20 20 20 20 20 20  "DEFAULT",      
30064 20 22 4e 55 4c 4c 22 2c 20 20 20 20 20 20 20 20   "NULL",        
30065 20 20 22 50 52 49 4d 41 52 59 22 2c 20 20 20 20    "PRIMARY",    
30066 20 0a 20 20 22 55 4e 49 51 55 45 22 2c 20 20 20   .  "UNIQUE",   
30067 20 20 20 20 20 22 43 48 45 43 4b 22 2c 20 20 20       "CHECK",   
30068 20 20 20 20 20 20 22 52 45 46 45 52 45 4e 43 45        "REFERENCE
30069 53 22 2c 20 20 20 20 22 41 55 54 4f 49 4e 43 52  S",    "AUTOINCR
3006a 22 2c 20 20 20 20 0a 20 20 22 4f 4e 22 2c 20 20  ",    .  "ON",  
3006b 20 20 20 20 20 20 20 20 20 20 22 44 45 4c 45 54            "DELET
3006c 45 22 2c 20 20 20 20 20 20 20 20 22 55 50 44 41  E",        "UPDA
3006d 54 45 22 2c 20 20 20 20 20 20 20 20 22 49 4e 53  TE",        "INS
3006e 45 52 54 22 2c 20 20 20 20 20 20 0a 20 20 22 53  ERT",      .  "S
3006f 45 54 22 2c 20 20 20 20 20 20 20 20 20 20 20 22  ET",           "
30070 44 45 46 45 52 52 41 42 4c 45 22 2c 20 20 20 20  DEFERRABLE",    
30071 22 46 4f 52 45 49 47 4e 22 2c 20 20 20 20 20 20  "FOREIGN",      
30072 20 22 44 52 4f 50 22 2c 20 20 20 20 20 20 20 20   "DROP",        
30073 0a 20 20 22 55 4e 49 4f 4e 22 2c 20 20 20 20 20  .  "UNION",     
30074 20 20 20 20 22 41 4c 4c 22 2c 20 20 20 20 20 20      "ALL",      
30075 20 20 20 20 20 22 45 58 43 45 50 54 22 2c 20 20       "EXCEPT",  
30076 20 20 20 20 20 20 22 49 4e 54 45 52 53 45 43 54        "INTERSECT
30077 22 2c 20 20 20 0a 20 20 22 53 45 4c 45 43 54 22  ",   .  "SELECT"
30078 2c 20 20 20 20 20 20 20 20 22 44 49 53 54 49 4e  ,        "DISTIN
30079 43 54 22 2c 20 20 20 20 20 20 22 44 4f 54 22 2c  CT",      "DOT",
3007a 20 20 20 20 20 20 20 20 20 20 20 22 46 52 4f 4d             "FROM
3007b 22 2c 20 20 20 20 20 20 20 20 0a 20 20 22 4a 4f  ",        .  "JO
3007c 49 4e 22 2c 20 20 20 20 20 20 20 20 20 20 22 55  IN",          "U
3007d 53 49 4e 47 22 2c 20 20 20 20 20 20 20 20 20 22  SING",         "
3007e 4f 52 44 45 52 22 2c 20 20 20 20 20 20 20 20 20  ORDER",         
3007f 22 47 52 4f 55 50 22 2c 20 20 20 20 20 20 20 0a  "GROUP",       .
30080 20 20 22 48 41 56 49 4e 47 22 2c 20 20 20 20 20    "HAVING",     
30081 20 20 20 22 4c 49 4d 49 54 22 2c 20 20 20 20 20     "LIMIT",     
30082 20 20 20 20 22 57 48 45 52 45 22 2c 20 20 20 20      "WHERE",    
30083 20 20 20 20 20 22 49 4e 54 4f 22 2c 20 20 20 20       "INTO",    
30084 20 20 20 20 0a 20 20 22 56 41 4c 55 45 53 22 2c      .  "VALUES",
30085 20 20 20 20 20 20 20 20 22 49 4e 54 45 47 45 52          "INTEGER
30086 22 2c 20 20 20 20 20 20 20 22 46 4c 4f 41 54 22  ",       "FLOAT"
30087 2c 20 20 20 20 20 20 20 20 20 22 42 4c 4f 42 22  ,         "BLOB"
30088 2c 20 20 20 20 20 20 20 20 0a 20 20 22 52 45 47  ,        .  "REG
30089 49 53 54 45 52 22 2c 20 20 20 20 20 20 22 56 41  ISTER",      "VA
3008a 52 49 41 42 4c 45 22 2c 20 20 20 20 20 20 22 43  RIABLE",      "C
3008b 41 53 45 22 2c 20 20 20 20 20 20 20 20 20 20 22  ASE",          "
3008c 57 48 45 4e 22 2c 20 20 20 20 20 20 20 20 0a 20  WHEN",        . 
3008d 20 22 54 48 45 4e 22 2c 20 20 20 20 20 20 20 20   "THEN",        
3008e 20 20 22 45 4c 53 45 22 2c 20 20 20 20 20 20 20    "ELSE",       
3008f 20 20 20 22 49 4e 44 45 58 22 2c 20 20 20 20 20     "INDEX",     
30090 20 20 20 20 22 41 4c 54 45 52 22 2c 20 20 20 20      "ALTER",    
30091 20 20 20 0a 20 20 22 41 44 44 22 2c 20 20 20 20     .  "ADD",    
30092 20 20 20 20 20 20 20 22 65 72 72 6f 72 22 2c 20         "error", 
30093 20 20 20 20 20 20 20 20 22 69 6e 70 75 74 22 2c          "input",
30094 20 20 20 20 20 20 20 20 20 22 63 6d 64 6c 69 73           "cmdlis
30095 74 22 2c 20 20 20 20 20 0a 20 20 22 65 63 6d 64  t",     .  "ecmd
30096 22 2c 20 20 20 20 20 20 20 20 20 20 22 65 78 70  ",          "exp
30097 6c 61 69 6e 22 2c 20 20 20 20 20 20 20 22 63 6d  lain",       "cm
30098 64 78 22 2c 20 20 20 20 20 20 20 20 20 20 22 63  dx",          "c
30099 6d 64 22 2c 20 20 20 20 20 20 20 20 20 0a 20 20  md",         .  
3009a 22 74 72 61 6e 73 74 79 70 65 22 2c 20 20 20 20  "transtype",    
3009b 20 22 74 72 61 6e 73 5f 6f 70 74 22 2c 20 20 20   "trans_opt",   
3009c 20 20 22 6e 6d 22 2c 20 20 20 20 20 20 20 20 20    "nm",         
3009d 20 20 20 22 73 61 76 65 70 6f 69 6e 74 5f 6f 70     "savepoint_op
3009e 74 22 2c 0a 20 20 22 63 72 65 61 74 65 5f 74 61  t",.  "create_ta
3009f 62 6c 65 22 2c 20 20 22 63 72 65 61 74 65 5f 74  ble",  "create_t
300a0 61 62 6c 65 5f 61 72 67 73 22 2c 20 20 22 63 72  able_args",  "cr
300a1 65 61 74 65 6b 77 22 2c 20 20 20 20 20 20 22 74  eatekw",      "t
300a2 65 6d 70 22 2c 20 20 20 20 20 20 20 20 0a 20 20  emp",        .  
300a3 22 69 66 6e 6f 74 65 78 69 73 74 73 22 2c 20 20  "ifnotexists",  
300a4 20 22 64 62 6e 6d 22 2c 20 20 20 20 20 20 20 20   "dbnm",        
300a5 20 20 22 63 6f 6c 75 6d 6e 6c 69 73 74 22 2c 20    "columnlist", 
300a6 20 20 20 22 63 6f 6e 73 6c 69 73 74 5f 6f 70 74     "conslist_opt
300a7 22 2c 0a 20 20 22 73 65 6c 65 63 74 22 2c 20 20  ",.  "select",  
300a8 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e 22 2c 20        "column", 
300a9 20 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e 69 64         "columnid
300aa 22 2c 20 20 20 20 20 20 22 74 79 70 65 22 2c 20  ",      "type", 
300ab 20 20 20 20 20 20 20 0a 20 20 22 63 61 72 67 6c         .  "cargl
300ac 69 73 74 22 2c 20 20 20 20 20 20 22 69 64 22 2c  ist",      "id",
300ad 20 20 20 20 20 20 20 20 20 20 20 20 22 69 64 73              "ids
300ae 22 2c 20 20 20 20 20 20 20 20 20 20 20 22 74 79  ",           "ty
300af 70 65 74 6f 6b 65 6e 22 2c 20 20 20 0a 20 20 22  petoken",   .  "
300b0 74 79 70 65 6e 61 6d 65 22 2c 20 20 20 20 20 20  typename",      
300b1 22 73 69 67 6e 65 64 22 2c 20 20 20 20 20 20 20  "signed",       
300b2 20 22 70 6c 75 73 5f 6e 75 6d 22 2c 20 20 20 20   "plus_num",    
300b3 20 20 22 6d 69 6e 75 73 5f 6e 75 6d 22 2c 20 20    "minus_num",  
300b4 20 0a 20 20 22 63 61 72 67 22 2c 20 20 20 20 20   .  "carg",     
300b5 20 20 20 20 20 22 63 63 6f 6e 73 22 2c 20 20 20       "ccons",   
300b6 20 20 20 20 20 20 22 74 65 72 6d 22 2c 20 20 20        "term",   
300b7 20 20 20 20 20 20 20 22 65 78 70 72 22 2c 20 20         "expr",  
300b8 20 20 20 20 20 20 0a 20 20 22 6f 6e 63 6f 6e 66        .  "onconf
300b9 22 2c 20 20 20 20 20 20 20 20 22 73 6f 72 74 6f  ",        "sorto
300ba 72 64 65 72 22 2c 20 20 20 20 20 22 61 75 74 6f  rder",     "auto
300bb 69 6e 63 22 2c 20 20 20 20 20 20 20 22 69 64 78  inc",       "idx
300bc 6c 69 73 74 5f 6f 70 74 22 2c 20 0a 20 20 22 72  list_opt", .  "r
300bd 65 66 61 72 67 73 22 2c 20 20 20 20 20 20 20 22  efargs",       "
300be 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 22  defer_subclause"
300bf 2c 20 20 22 72 65 66 61 72 67 22 2c 20 20 20 20  ,  "refarg",    
300c0 20 20 20 20 22 72 65 66 61 63 74 22 2c 20 20 20      "refact",   
300c1 20 20 20 0a 20 20 22 69 6e 69 74 5f 64 65 66 65     .  "init_defe
300c2 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 22 2c 20  rred_pred_opt", 
300c3 20 22 63 6f 6e 73 6c 69 73 74 22 2c 20 20 20 20   "conslist",    
300c4 20 20 22 74 63 6f 6e 73 22 2c 20 20 20 20 20 20    "tcons",      
300c5 20 20 20 22 69 64 78 6c 69 73 74 22 2c 20 20 20     "idxlist",   
300c6 20 20 0a 20 20 22 64 65 66 65 72 5f 73 75 62 63    .  "defer_subc
300c7 6c 61 75 73 65 5f 6f 70 74 22 2c 20 20 22 6f 72  lause_opt",  "or
300c8 63 6f 6e 66 22 2c 20 20 20 20 20 20 20 20 22 72  conf",        "r
300c9 65 73 6f 6c 76 65 74 79 70 65 22 2c 20 20 20 22  esolvetype",   "
300ca 72 61 69 73 65 74 79 70 65 22 2c 20 20 20 0a 20  raisetype",   . 
300cb 20 22 69 66 65 78 69 73 74 73 22 2c 20 20 20 20   "ifexists",    
300cc 20 20 22 66 75 6c 6c 6e 61 6d 65 22 2c 20 20 20    "fullname",   
300cd 20 20 20 22 6f 6e 65 73 65 6c 65 63 74 22 2c 20     "oneselect", 
300ce 20 20 20 20 22 6d 75 6c 74 69 73 65 6c 65 63 74      "multiselect
300cf 5f 6f 70 22 2c 0a 20 20 22 64 69 73 74 69 6e 63  _op",.  "distinc
300d0 74 22 2c 20 20 20 20 20 20 22 73 65 6c 63 6f 6c  t",      "selcol
300d1 6c 69 73 74 22 2c 20 20 20 20 22 66 72 6f 6d 22  list",    "from"
300d2 2c 20 20 20 20 20 20 20 20 20 20 22 77 68 65 72  ,          "wher
300d3 65 5f 6f 70 74 22 2c 20 20 20 0a 20 20 22 67 72  e_opt",   .  "gr
300d4 6f 75 70 62 79 5f 6f 70 74 22 2c 20 20 20 22 68  oupby_opt",   "h
300d5 61 76 69 6e 67 5f 6f 70 74 22 2c 20 20 20 20 22  aving_opt",    "
300d6 6f 72 64 65 72 62 79 5f 6f 70 74 22 2c 20 20 20  orderby_opt",   
300d7 22 6c 69 6d 69 74 5f 6f 70 74 22 2c 20 20 20 0a  "limit_opt",   .
300d8 20 20 22 73 63 6c 70 22 2c 20 20 20 20 20 20 20    "sclp",       
300d9 20 20 20 22 61 73 22 2c 20 20 20 20 20 20 20 20     "as",        
300da 20 20 20 20 22 73 65 6c 74 61 62 6c 69 73 74 22      "seltablist"
300db 2c 20 20 20 20 22 73 74 6c 5f 70 72 65 66 69 78  ,    "stl_prefix
300dc 22 2c 20 20 0a 20 20 22 6a 6f 69 6e 6f 70 22 2c  ",  .  "joinop",
300dd 20 20 20 20 20 20 20 20 22 69 6e 64 65 78 65 64          "indexed
300de 5f 6f 70 74 22 2c 20 20 20 22 6f 6e 5f 6f 70 74  _opt",   "on_opt
300df 22 2c 20 20 20 20 20 20 20 20 22 75 73 69 6e 67  ",        "using
300e0 5f 6f 70 74 22 2c 20 20 20 0a 20 20 22 6a 6f 69  _opt",   .  "joi
300e1 6e 6f 70 32 22 2c 20 20 20 20 20 20 20 22 69 6e  nop2",       "in
300e2 73 63 6f 6c 6c 69 73 74 22 2c 20 20 20 20 22 73  scollist",    "s
300e3 6f 72 74 6c 69 73 74 22 2c 20 20 20 20 20 20 22  ortlist",      "
300e4 73 6f 72 74 69 74 65 6d 22 2c 20 20 20 20 0a 20  sortitem",    . 
300e5 20 22 6e 65 78 70 72 6c 69 73 74 22 2c 20 20 20   "nexprlist",   
300e6 20 20 22 73 65 74 6c 69 73 74 22 2c 20 20 20 20    "setlist",    
300e7 20 20 20 22 69 6e 73 65 72 74 5f 63 6d 64 22 2c     "insert_cmd",
300e8 20 20 20 20 22 69 6e 73 63 6f 6c 6c 69 73 74 5f      "inscollist_
300e9 6f 70 74 22 2c 0a 20 20 22 69 74 65 6d 6c 69 73  opt",.  "itemlis
300ea 74 22 2c 20 20 20 20 20 20 22 65 78 70 72 6c 69  t",      "exprli
300eb 73 74 22 2c 20 20 20 20 20 20 22 6c 69 6b 65 6f  st",      "likeo
300ec 70 22 2c 20 20 20 20 20 20 20 20 22 65 73 63 61  p",        "esca
300ed 70 65 22 2c 20 20 20 20 20 20 0a 20 20 22 62 65  pe",      .  "be
300ee 74 77 65 65 6e 5f 6f 70 22 2c 20 20 20 20 22 69  tween_op",    "i
300ef 6e 5f 6f 70 22 2c 20 20 20 20 20 20 20 20 20 22  n_op",         "
300f0 63 61 73 65 5f 6f 70 65 72 61 6e 64 22 2c 20 20  case_operand",  
300f1 22 63 61 73 65 5f 65 78 70 72 6c 69 73 74 22 2c  "case_exprlist",
300f2 0a 20 20 22 63 61 73 65 5f 65 6c 73 65 22 2c 20  .  "case_else", 
300f3 20 20 20 20 22 75 6e 69 71 75 65 66 6c 61 67 22      "uniqueflag"
300f4 2c 20 20 20 20 22 63 6f 6c 6c 61 74 65 22 2c 20  ,    "collate", 
300f5 20 20 20 20 20 20 22 6e 6d 6e 75 6d 22 2c 20 20        "nmnum",  
300f6 20 20 20 20 20 0a 20 20 22 70 6c 75 73 5f 6f 70       .  "plus_op
300f7 74 22 2c 20 20 20 20 20 20 22 6e 75 6d 62 65 72  t",      "number
300f8 22 2c 20 20 20 20 20 20 20 20 22 74 72 69 67 67  ",        "trigg
300f9 65 72 5f 64 65 63 6c 22 2c 20 20 22 74 72 69 67  er_decl",  "trig
300fa 67 65 72 5f 63 6d 64 5f 6c 69 73 74 22 2c 0a 20  ger_cmd_list",. 
300fb 20 22 74 72 69 67 67 65 72 5f 74 69 6d 65 22 2c   "trigger_time",
300fc 20 20 22 74 72 69 67 67 65 72 5f 65 76 65 6e 74    "trigger_event
300fd 22 2c 20 20 22 66 6f 72 65 61 63 68 5f 63 6c 61  ",  "foreach_cla
300fe 75 73 65 22 2c 20 20 22 77 68 65 6e 5f 63 6c 61  use",  "when_cla
300ff 75 73 65 22 2c 20 0a 20 20 22 74 72 69 67 67 65  use", .  "trigge
30100 72 5f 63 6d 64 22 2c 20 20 20 22 74 72 6e 6d 22  r_cmd",   "trnm"
30101 2c 20 20 20 20 20 20 20 20 20 20 22 74 72 69 64  ,          "trid
30102 78 62 79 22 2c 20 20 20 20 20 20 20 22 64 61 74  xby",       "dat
30103 61 62 61 73 65 5f 6b 77 5f 6f 70 74 22 2c 0a 20  abase_kw_opt",. 
30104 20 22 6b 65 79 5f 6f 70 74 22 2c 20 20 20 20 20   "key_opt",     
30105 20 20 22 61 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75    "add_column_fu
30106 6c 6c 6e 61 6d 65 22 2c 20 20 22 6b 77 63 6f 6c  llname",  "kwcol
30107 75 6d 6e 5f 6f 70 74 22 2c 20 20 22 63 72 65 61  umn_opt",  "crea
30108 74 65 5f 76 74 61 62 22 2c 20 0a 20 20 22 76 74  te_vtab", .  "vt
30109 61 62 61 72 67 6c 69 73 74 22 2c 20 20 20 22 76  abarglist",   "v
3010a 74 61 62 61 72 67 22 2c 20 20 20 20 20 20 20 22  tabarg",       "
3010b 76 74 61 62 61 72 67 74 6f 6b 65 6e 22 2c 20 20  vtabargtoken",  
3010c 22 6c 70 22 2c 20 20 20 20 20 20 20 20 20 20 0a  "lp",          .
3010d 20 20 22 61 6e 79 6c 69 73 74 22 2c 20 20 20 20    "anylist",    
3010e 20 0a 7d 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 4e   .};.#endif /* N
3010f 44 45 42 55 47 20 2a 2f 0a 0a 23 69 66 6e 64 65  DEBUG */..#ifnde
30110 66 20 4e 44 45 42 55 47 0a 2f 2a 20 46 6f 72 20  f NDEBUG./* For 
30111 74 72 61 63 69 6e 67 20 72 65 64 75 63 65 20 61  tracing reduce a
30112 63 74 69 6f 6e 73 2c 20 74 68 65 20 6e 61 6d 65  ctions, the name
30113 73 20 6f 66 20 61 6c 6c 20 72 75 6c 65 73 20 61  s of all rules a
30114 72 65 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a  re required..*/.
30115 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
30116 72 20 2a 63 6f 6e 73 74 20 79 79 52 75 6c 65 4e  r *const yyRuleN
30117 61 6d 65 5b 5d 20 3d 20 7b 0a 20 2f 2a 20 20 20  ame[] = {. /*   
30118 30 20 2a 2f 20 22 69 6e 70 75 74 20 3a 3a 3d 20  0 */ "input ::= 
30119 63 6d 64 6c 69 73 74 22 2c 0a 20 2f 2a 20 20 20  cmdlist",. /*   
3011a 31 20 2a 2f 20 22 63 6d 64 6c 69 73 74 20 3a 3a  1 */ "cmdlist ::
3011b 3d 20 63 6d 64 6c 69 73 74 20 65 63 6d 64 22 2c  = cmdlist ecmd",
3011c 0a 20 2f 2a 20 20 20 32 20 2a 2f 20 22 63 6d 64  . /*   2 */ "cmd
3011d 6c 69 73 74 20 3a 3a 3d 20 65 63 6d 64 22 2c 0a  list ::= ecmd",.
3011e 20 2f 2a 20 20 20 33 20 2a 2f 20 22 65 63 6d 64   /*   3 */ "ecmd
3011f 20 3a 3a 3d 20 53 45 4d 49 22 2c 0a 20 2f 2a 20   ::= SEMI",. /* 
30120 20 20 34 20 2a 2f 20 22 65 63 6d 64 20 3a 3a 3d    4 */ "ecmd ::=
30121 20 65 78 70 6c 61 69 6e 20 63 6d 64 78 20 53 45   explain cmdx SE
30122 4d 49 22 2c 0a 20 2f 2a 20 20 20 35 20 2a 2f 20  MI",. /*   5 */ 
30123 22 65 78 70 6c 61 69 6e 20 3a 3a 3d 22 2c 0a 20  "explain ::=",. 
30124 2f 2a 20 20 20 36 20 2a 2f 20 22 65 78 70 6c 61  /*   6 */ "expla
30125 69 6e 20 3a 3a 3d 20 45 58 50 4c 41 49 4e 22 2c  in ::= EXPLAIN",
30126 0a 20 2f 2a 20 20 20 37 20 2a 2f 20 22 65 78 70  . /*   7 */ "exp
30127 6c 61 69 6e 20 3a 3a 3d 20 45 58 50 4c 41 49 4e  lain ::= EXPLAIN
30128 20 51 55 45 52 59 20 50 4c 41 4e 22 2c 0a 20 2f   QUERY PLAN",. /
30129 2a 20 20 20 38 20 2a 2f 20 22 63 6d 64 78 20 3a  *   8 */ "cmdx :
3012a 3a 3d 20 63 6d 64 22 2c 0a 20 2f 2a 20 20 20 39  := cmd",. /*   9
3012b 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 42 45 47   */ "cmd ::= BEG
3012c 49 4e 20 74 72 61 6e 73 74 79 70 65 20 74 72 61  IN transtype tra
3012d 6e 73 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 31 30  ns_opt",. /*  10
3012e 20 2a 2f 20 22 74 72 61 6e 73 5f 6f 70 74 20 3a   */ "trans_opt :
3012f 3a 3d 22 2c 0a 20 2f 2a 20 20 31 31 20 2a 2f 20  :=",. /*  11 */ 
30130 22 74 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d 20 54  "trans_opt ::= T
30131 52 41 4e 53 41 43 54 49 4f 4e 22 2c 0a 20 2f 2a  RANSACTION",. /*
30132 20 20 31 32 20 2a 2f 20 22 74 72 61 6e 73 5f 6f    12 */ "trans_o
30133 70 74 20 3a 3a 3d 20 54 52 41 4e 53 41 43 54 49  pt ::= TRANSACTI
30134 4f 4e 20 6e 6d 22 2c 0a 20 2f 2a 20 20 31 33 20  ON nm",. /*  13 
30135 2a 2f 20 22 74 72 61 6e 73 74 79 70 65 20 3a 3a  */ "transtype ::
30136 3d 22 2c 0a 20 2f 2a 20 20 31 34 20 2a 2f 20 22  =",. /*  14 */ "
30137 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d 20 44 45  transtype ::= DE
30138 46 45 52 52 45 44 22 2c 0a 20 2f 2a 20 20 31 35  FERRED",. /*  15
30139 20 2a 2f 20 22 74 72 61 6e 73 74 79 70 65 20 3a   */ "transtype :
3013a 3a 3d 20 49 4d 4d 45 44 49 41 54 45 22 2c 0a 20  := IMMEDIATE",. 
3013b 2f 2a 20 20 31 36 20 2a 2f 20 22 74 72 61 6e 73  /*  16 */ "trans
3013c 74 79 70 65 20 3a 3a 3d 20 45 58 43 4c 55 53 49  type ::= EXCLUSI
3013d 56 45 22 2c 0a 20 2f 2a 20 20 31 37 20 2a 2f 20  VE",. /*  17 */ 
3013e 22 63 6d 64 20 3a 3a 3d 20 43 4f 4d 4d 49 54 20  "cmd ::= COMMIT 
3013f 74 72 61 6e 73 5f 6f 70 74 22 2c 0a 20 2f 2a 20  trans_opt",. /* 
30140 20 31 38 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20   18 */ "cmd ::= 
30141 45 4e 44 20 74 72 61 6e 73 5f 6f 70 74 22 2c 0a  END trans_opt",.
30142 20 2f 2a 20 20 31 39 20 2a 2f 20 22 63 6d 64 20   /*  19 */ "cmd 
30143 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b 20 74 72 61  ::= ROLLBACK tra
30144 6e 73 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 32 30  ns_opt",. /*  20
30145 20 2a 2f 20 22 73 61 76 65 70 6f 69 6e 74 5f 6f   */ "savepoint_o
30146 70 74 20 3a 3a 3d 20 53 41 56 45 50 4f 49 4e 54  pt ::= SAVEPOINT
30147 22 2c 0a 20 2f 2a 20 20 32 31 20 2a 2f 20 22 73  ",. /*  21 */ "s
30148 61 76 65 70 6f 69 6e 74 5f 6f 70 74 20 3a 3a 3d  avepoint_opt ::=
30149 22 2c 0a 20 2f 2a 20 20 32 32 20 2a 2f 20 22 63  ",. /*  22 */ "c
3014a 6d 64 20 3a 3a 3d 20 53 41 56 45 50 4f 49 4e 54  md ::= SAVEPOINT
3014b 20 6e 6d 22 2c 0a 20 2f 2a 20 20 32 33 20 2a 2f   nm",. /*  23 */
3014c 20 22 63 6d 64 20 3a 3a 3d 20 52 45 4c 45 41 53   "cmd ::= RELEAS
3014d 45 20 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74 20  E savepoint_opt 
3014e 6e 6d 22 2c 0a 20 2f 2a 20 20 32 34 20 2a 2f 20  nm",. /*  24 */ 
3014f 22 63 6d 64 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43  "cmd ::= ROLLBAC
30150 4b 20 74 72 61 6e 73 5f 6f 70 74 20 54 4f 20 73  K trans_opt TO s
30151 61 76 65 70 6f 69 6e 74 5f 6f 70 74 20 6e 6d 22  avepoint_opt nm"
30152 2c 0a 20 2f 2a 20 20 32 35 20 2a 2f 20 22 63 6d  ,. /*  25 */ "cm
30153 64 20 3a 3a 3d 20 63 72 65 61 74 65 5f 74 61 62  d ::= create_tab
30154 6c 65 20 63 72 65 61 74 65 5f 74 61 62 6c 65 5f  le create_table_
30155 61 72 67 73 22 2c 0a 20 2f 2a 20 20 32 36 20 2a  args",. /*  26 *
30156 2f 20 22 63 72 65 61 74 65 5f 74 61 62 6c 65 20  / "create_table 
30157 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20 74 65 6d  ::= createkw tem
30158 70 20 54 41 42 4c 45 20 69 66 6e 6f 74 65 78 69  p TABLE ifnotexi
30159 73 74 73 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f  sts nm dbnm",. /
3015a 2a 20 20 32 37 20 2a 2f 20 22 63 72 65 61 74 65  *  27 */ "create
3015b 6b 77 20 3a 3a 3d 20 43 52 45 41 54 45 22 2c 0a  kw ::= CREATE",.
3015c 20 2f 2a 20 20 32 38 20 2a 2f 20 22 69 66 6e 6f   /*  28 */ "ifno
3015d 74 65 78 69 73 74 73 20 3a 3a 3d 22 2c 0a 20 2f  texists ::=",. /
3015e 2a 20 20 32 39 20 2a 2f 20 22 69 66 6e 6f 74 65  *  29 */ "ifnote
3015f 78 69 73 74 73 20 3a 3a 3d 20 49 46 20 4e 4f 54  xists ::= IF NOT
30160 20 45 58 49 53 54 53 22 2c 0a 20 2f 2a 20 20 33   EXISTS",. /*  3
30161 30 20 2a 2f 20 22 74 65 6d 70 20 3a 3a 3d 20 54  0 */ "temp ::= T
30162 45 4d 50 22 2c 0a 20 2f 2a 20 20 33 31 20 2a 2f  EMP",. /*  31 */
30163 20 22 74 65 6d 70 20 3a 3a 3d 22 2c 0a 20 2f 2a   "temp ::=",. /*
30164 20 20 33 32 20 2a 2f 20 22 63 72 65 61 74 65 5f    32 */ "create_
30165 74 61 62 6c 65 5f 61 72 67 73 20 3a 3a 3d 20 4c  table_args ::= L
30166 50 20 63 6f 6c 75 6d 6e 6c 69 73 74 20 63 6f 6e  P columnlist con
30167 73 6c 69 73 74 5f 6f 70 74 20 52 50 22 2c 0a 20  slist_opt RP",. 
30168 2f 2a 20 20 33 33 20 2a 2f 20 22 63 72 65 61 74  /*  33 */ "creat
30169 65 5f 74 61 62 6c 65 5f 61 72 67 73 20 3a 3a 3d  e_table_args ::=
3016a 20 41 53 20 73 65 6c 65 63 74 22 2c 0a 20 2f 2a   AS select",. /*
3016b 20 20 33 34 20 2a 2f 20 22 63 6f 6c 75 6d 6e 6c    34 */ "columnl
3016c 69 73 74 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 6c 69  ist ::= columnli
3016d 73 74 20 43 4f 4d 4d 41 20 63 6f 6c 75 6d 6e 22  st COMMA column"
3016e 2c 0a 20 2f 2a 20 20 33 35 20 2a 2f 20 22 63 6f  ,. /*  35 */ "co
3016f 6c 75 6d 6e 6c 69 73 74 20 3a 3a 3d 20 63 6f 6c  lumnlist ::= col
30170 75 6d 6e 22 2c 0a 20 2f 2a 20 20 33 36 20 2a 2f  umn",. /*  36 */
30171 20 22 63 6f 6c 75 6d 6e 20 3a 3a 3d 20 63 6f 6c   "column ::= col
30172 75 6d 6e 69 64 20 74 79 70 65 20 63 61 72 67 6c  umnid type cargl
30173 69 73 74 22 2c 0a 20 2f 2a 20 20 33 37 20 2a 2f  ist",. /*  37 */
30174 20 22 63 6f 6c 75 6d 6e 69 64 20 3a 3a 3d 20 6e   "columnid ::= n
30175 6d 22 2c 0a 20 2f 2a 20 20 33 38 20 2a 2f 20 22  m",. /*  38 */ "
30176 69 64 20 3a 3a 3d 20 49 44 22 2c 0a 20 2f 2a 20  id ::= ID",. /* 
30177 20 33 39 20 2a 2f 20 22 69 64 20 3a 3a 3d 20 49   39 */ "id ::= I
30178 4e 44 45 58 45 44 22 2c 0a 20 2f 2a 20 20 34 30  NDEXED",. /*  40
30179 20 2a 2f 20 22 69 64 73 20 3a 3a 3d 20 49 44 7c   */ "ids ::= ID|
3017a 53 54 52 49 4e 47 22 2c 0a 20 2f 2a 20 20 34 31  STRING",. /*  41
3017b 20 2a 2f 20 22 6e 6d 20 3a 3a 3d 20 69 64 22 2c   */ "nm ::= id",
3017c 0a 20 2f 2a 20 20 34 32 20 2a 2f 20 22 6e 6d 20  . /*  42 */ "nm 
3017d 3a 3a 3d 20 53 54 52 49 4e 47 22 2c 0a 20 2f 2a  ::= STRING",. /*
3017e 20 20 34 33 20 2a 2f 20 22 6e 6d 20 3a 3a 3d 20    43 */ "nm ::= 
3017f 4a 4f 49 4e 5f 4b 57 22 2c 0a 20 2f 2a 20 20 34  JOIN_KW",. /*  4
30180 34 20 2a 2f 20 22 74 79 70 65 20 3a 3a 3d 22 2c  4 */ "type ::=",
30181 0a 20 2f 2a 20 20 34 35 20 2a 2f 20 22 74 79 70  . /*  45 */ "typ
30182 65 20 3a 3a 3d 20 74 79 70 65 74 6f 6b 65 6e 22  e ::= typetoken"
30183 2c 0a 20 2f 2a 20 20 34 36 20 2a 2f 20 22 74 79  ,. /*  46 */ "ty
30184 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70 65  petoken ::= type
30185 6e 61 6d 65 22 2c 0a 20 2f 2a 20 20 34 37 20 2a  name",. /*  47 *
30186 2f 20 22 74 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d  / "typetoken ::=
30187 20 74 79 70 65 6e 61 6d 65 20 4c 50 20 73 69 67   typename LP sig
30188 6e 65 64 20 52 50 22 2c 0a 20 2f 2a 20 20 34 38  ned RP",. /*  48
30189 20 2a 2f 20 22 74 79 70 65 74 6f 6b 65 6e 20 3a   */ "typetoken :
3018a 3a 3d 20 74 79 70 65 6e 61 6d 65 20 4c 50 20 73  := typename LP s
3018b 69 67 6e 65 64 20 43 4f 4d 4d 41 20 73 69 67 6e  igned COMMA sign
3018c 65 64 20 52 50 22 2c 0a 20 2f 2a 20 20 34 39 20  ed RP",. /*  49 
3018d 2a 2f 20 22 74 79 70 65 6e 61 6d 65 20 3a 3a 3d  */ "typename ::=
3018e 20 69 64 73 22 2c 0a 20 2f 2a 20 20 35 30 20 2a   ids",. /*  50 *
3018f 2f 20 22 74 79 70 65 6e 61 6d 65 20 3a 3a 3d 20  / "typename ::= 
30190 74 79 70 65 6e 61 6d 65 20 69 64 73 22 2c 0a 20  typename ids",. 
30191 2f 2a 20 20 35 31 20 2a 2f 20 22 73 69 67 6e 65  /*  51 */ "signe
30192 64 20 3a 3a 3d 20 70 6c 75 73 5f 6e 75 6d 22 2c  d ::= plus_num",
30193 0a 20 2f 2a 20 20 35 32 20 2a 2f 20 22 73 69 67  . /*  52 */ "sig
30194 6e 65 64 20 3a 3a 3d 20 6d 69 6e 75 73 5f 6e 75  ned ::= minus_nu
30195 6d 22 2c 0a 20 2f 2a 20 20 35 33 20 2a 2f 20 22  m",. /*  53 */ "
30196 63 61 72 67 6c 69 73 74 20 3a 3a 3d 20 63 61 72  carglist ::= car
30197 67 6c 69 73 74 20 63 61 72 67 22 2c 0a 20 2f 2a  glist carg",. /*
30198 20 20 35 34 20 2a 2f 20 22 63 61 72 67 6c 69 73    54 */ "carglis
30199 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 35 35 20  t ::=",. /*  55 
3019a 2a 2f 20 22 63 61 72 67 20 3a 3a 3d 20 43 4f 4e  */ "carg ::= CON
3019b 53 54 52 41 49 4e 54 20 6e 6d 20 63 63 6f 6e 73  STRAINT nm ccons
3019c 22 2c 0a 20 2f 2a 20 20 35 36 20 2a 2f 20 22 63  ",. /*  56 */ "c
3019d 61 72 67 20 3a 3a 3d 20 63 63 6f 6e 73 22 2c 0a  arg ::= ccons",.
3019e 20 2f 2a 20 20 35 37 20 2a 2f 20 22 63 63 6f 6e   /*  57 */ "ccon
3019f 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 74 65  s ::= DEFAULT te
301a0 72 6d 22 2c 0a 20 2f 2a 20 20 35 38 20 2a 2f 20  rm",. /*  58 */ 
301a1 22 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55  "ccons ::= DEFAU
301a2 4c 54 20 4c 50 20 65 78 70 72 20 52 50 22 2c 0a  LT LP expr RP",.
301a3 20 2f 2a 20 20 35 39 20 2a 2f 20 22 63 63 6f 6e   /*  59 */ "ccon
301a4 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 50 4c  s ::= DEFAULT PL
301a5 55 53 20 74 65 72 6d 22 2c 0a 20 2f 2a 20 20 36  US term",. /*  6
301a6 30 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20  0 */ "ccons ::= 
301a7 44 45 46 41 55 4c 54 20 4d 49 4e 55 53 20 74 65  DEFAULT MINUS te
301a8 72 6d 22 2c 0a 20 2f 2a 20 20 36 31 20 2a 2f 20  rm",. /*  61 */ 
301a9 22 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55  "ccons ::= DEFAU
301aa 4c 54 20 69 64 22 2c 0a 20 2f 2a 20 20 36 32 20  LT id",. /*  62 
301ab 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 4e 55  */ "ccons ::= NU
301ac 4c 4c 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20  LL onconf",. /* 
301ad 20 36 33 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a   63 */ "ccons ::
301ae 3d 20 4e 4f 54 20 4e 55 4c 4c 20 6f 6e 63 6f 6e  = NOT NULL oncon
301af 66 22 2c 0a 20 2f 2a 20 20 36 34 20 2a 2f 20 22  f",. /*  64 */ "
301b0 63 63 6f 6e 73 20 3a 3a 3d 20 50 52 49 4d 41 52  ccons ::= PRIMAR
301b1 59 20 4b 45 59 20 73 6f 72 74 6f 72 64 65 72 20  Y KEY sortorder 
301b2 6f 6e 63 6f 6e 66 20 61 75 74 6f 69 6e 63 22 2c  onconf autoinc",
301b3 0a 20 2f 2a 20 20 36 35 20 2a 2f 20 22 63 63 6f  . /*  65 */ "cco
301b4 6e 73 20 3a 3a 3d 20 55 4e 49 51 55 45 20 6f 6e  ns ::= UNIQUE on
301b5 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20 36 36 20 2a  conf",. /*  66 *
301b6 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 43 48 45  / "ccons ::= CHE
301b7 43 4b 20 4c 50 20 65 78 70 72 20 52 50 22 2c 0a  CK LP expr RP",.
301b8 20 2f 2a 20 20 36 37 20 2a 2f 20 22 63 63 6f 6e   /*  67 */ "ccon
301b9 73 20 3a 3a 3d 20 52 45 46 45 52 45 4e 43 45 53  s ::= REFERENCES
301ba 20 6e 6d 20 69 64 78 6c 69 73 74 5f 6f 70 74 20   nm idxlist_opt 
301bb 72 65 66 61 72 67 73 22 2c 0a 20 2f 2a 20 20 36  refargs",. /*  6
301bc 38 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20  8 */ "ccons ::= 
301bd 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 22  defer_subclause"
301be 2c 0a 20 2f 2a 20 20 36 39 20 2a 2f 20 22 63 63  ,. /*  69 */ "cc
301bf 6f 6e 73 20 3a 3a 3d 20 43 4f 4c 4c 41 54 45 20  ons ::= COLLATE 
301c0 69 64 73 22 2c 0a 20 2f 2a 20 20 37 30 20 2a 2f  ids",. /*  70 */
301c1 20 22 61 75 74 6f 69 6e 63 20 3a 3a 3d 22 2c 0a   "autoinc ::=",.
301c2 20 2f 2a 20 20 37 31 20 2a 2f 20 22 61 75 74 6f   /*  71 */ "auto
301c3 69 6e 63 20 3a 3a 3d 20 41 55 54 4f 49 4e 43 52  inc ::= AUTOINCR
301c4 22 2c 0a 20 2f 2a 20 20 37 32 20 2a 2f 20 22 72  ",. /*  72 */ "r
301c5 65 66 61 72 67 73 20 3a 3a 3d 22 2c 0a 20 2f 2a  efargs ::=",. /*
301c6 20 20 37 33 20 2a 2f 20 22 72 65 66 61 72 67 73    73 */ "refargs
301c7 20 3a 3a 3d 20 72 65 66 61 72 67 73 20 72 65 66   ::= refargs ref
301c8 61 72 67 22 2c 0a 20 2f 2a 20 20 37 34 20 2a 2f  arg",. /*  74 */
301c9 20 22 72 65 66 61 72 67 20 3a 3a 3d 20 4d 41 54   "refarg ::= MAT
301ca 43 48 20 6e 6d 22 2c 0a 20 2f 2a 20 20 37 35 20  CH nm",. /*  75 
301cb 2a 2f 20 22 72 65 66 61 72 67 20 3a 3a 3d 20 4f  */ "refarg ::= O
301cc 4e 20 44 45 4c 45 54 45 20 72 65 66 61 63 74 22  N DELETE refact"
301cd 2c 0a 20 2f 2a 20 20 37 36 20 2a 2f 20 22 72 65  ,. /*  76 */ "re
301ce 66 61 72 67 20 3a 3a 3d 20 4f 4e 20 55 50 44 41  farg ::= ON UPDA
301cf 54 45 20 72 65 66 61 63 74 22 2c 0a 20 2f 2a 20  TE refact",. /* 
301d0 20 37 37 20 2a 2f 20 22 72 65 66 61 72 67 20 3a   77 */ "refarg :
301d1 3a 3d 20 4f 4e 20 49 4e 53 45 52 54 20 72 65 66  := ON INSERT ref
301d2 61 63 74 22 2c 0a 20 2f 2a 20 20 37 38 20 2a 2f  act",. /*  78 */
301d3 20 22 72 65 66 61 63 74 20 3a 3a 3d 20 53 45 54   "refact ::= SET
301d4 20 4e 55 4c 4c 22 2c 0a 20 2f 2a 20 20 37 39 20   NULL",. /*  79 
301d5 2a 2f 20 22 72 65 66 61 63 74 20 3a 3a 3d 20 53  */ "refact ::= S
301d6 45 54 20 44 45 46 41 55 4c 54 22 2c 0a 20 2f 2a  ET DEFAULT",. /*
301d7 20 20 38 30 20 2a 2f 20 22 72 65 66 61 63 74 20    80 */ "refact 
301d8 3a 3a 3d 20 43 41 53 43 41 44 45 22 2c 0a 20 2f  ::= CASCADE",. /
301d9 2a 20 20 38 31 20 2a 2f 20 22 72 65 66 61 63 74  *  81 */ "refact
301da 20 3a 3a 3d 20 52 45 53 54 52 49 43 54 22 2c 0a   ::= RESTRICT",.
301db 20 2f 2a 20 20 38 32 20 2a 2f 20 22 64 65 66 65   /*  82 */ "defe
301dc 72 5f 73 75 62 63 6c 61 75 73 65 20 3a 3a 3d 20  r_subclause ::= 
301dd 4e 4f 54 20 44 45 46 45 52 52 41 42 4c 45 20 69  NOT DEFERRABLE i
301de 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65  nit_deferred_pre
301df 64 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 38 33 20  d_opt",. /*  83 
301e0 2a 2f 20 22 64 65 66 65 72 5f 73 75 62 63 6c 61  */ "defer_subcla
301e1 75 73 65 20 3a 3a 3d 20 44 45 46 45 52 52 41 42  use ::= DEFERRAB
301e2 4c 45 20 69 6e 69 74 5f 64 65 66 65 72 72 65 64  LE init_deferred
301e3 5f 70 72 65 64 5f 6f 70 74 22 2c 0a 20 2f 2a 20  _pred_opt",. /* 
301e4 20 38 34 20 2a 2f 20 22 69 6e 69 74 5f 64 65 66   84 */ "init_def
301e5 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 20 3a  erred_pred_opt :
301e6 3a 3d 22 2c 0a 20 2f 2a 20 20 38 35 20 2a 2f 20  :=",. /*  85 */ 
301e7 22 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70  "init_deferred_p
301e8 72 65 64 5f 6f 70 74 20 3a 3a 3d 20 49 4e 49 54  red_opt ::= INIT
301e9 49 41 4c 4c 59 20 44 45 46 45 52 52 45 44 22 2c  IALLY DEFERRED",
301ea 0a 20 2f 2a 20 20 38 36 20 2a 2f 20 22 69 6e 69  . /*  86 */ "ini
301eb 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f  t_deferred_pred_
301ec 6f 70 74 20 3a 3a 3d 20 49 4e 49 54 49 41 4c 4c  opt ::= INITIALL
301ed 59 20 49 4d 4d 45 44 49 41 54 45 22 2c 0a 20 2f  Y IMMEDIATE",. /
301ee 2a 20 20 38 37 20 2a 2f 20 22 63 6f 6e 73 6c 69  *  87 */ "consli
301ef 73 74 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a  st_opt ::=",. /*
301f0 20 20 38 38 20 2a 2f 20 22 63 6f 6e 73 6c 69 73    88 */ "conslis
301f1 74 5f 6f 70 74 20 3a 3a 3d 20 43 4f 4d 4d 41 20  t_opt ::= COMMA 
301f2 63 6f 6e 73 6c 69 73 74 22 2c 0a 20 2f 2a 20 20  conslist",. /*  
301f3 38 39 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 20  89 */ "conslist 
301f4 3a 3a 3d 20 63 6f 6e 73 6c 69 73 74 20 43 4f 4d  ::= conslist COM
301f5 4d 41 20 74 63 6f 6e 73 22 2c 0a 20 2f 2a 20 20  MA tcons",. /*  
301f6 39 30 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 20  90 */ "conslist 
301f7 3a 3a 3d 20 63 6f 6e 73 6c 69 73 74 20 74 63 6f  ::= conslist tco
301f8 6e 73 22 2c 0a 20 2f 2a 20 20 39 31 20 2a 2f 20  ns",. /*  91 */ 
301f9 22 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20 74 63  "conslist ::= tc
301fa 6f 6e 73 22 2c 0a 20 2f 2a 20 20 39 32 20 2a 2f  ons",. /*  92 */
301fb 20 22 74 63 6f 6e 73 20 3a 3a 3d 20 43 4f 4e 53   "tcons ::= CONS
301fc 54 52 41 49 4e 54 20 6e 6d 22 2c 0a 20 2f 2a 20  TRAINT nm",. /* 
301fd 20 39 33 20 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a   93 */ "tcons ::
301fe 3d 20 50 52 49 4d 41 52 59 20 4b 45 59 20 4c 50  = PRIMARY KEY LP
301ff 20 69 64 78 6c 69 73 74 20 61 75 74 6f 69 6e 63   idxlist autoinc
30200 20 52 50 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a   RP onconf",. /*
30201 20 20 39 34 20 2a 2f 20 22 74 63 6f 6e 73 20 3a    94 */ "tcons :
30202 3a 3d 20 55 4e 49 51 55 45 20 4c 50 20 69 64 78  := UNIQUE LP idx
30203 6c 69 73 74 20 52 50 20 6f 6e 63 6f 6e 66 22 2c  list RP onconf",
30204 0a 20 2f 2a 20 20 39 35 20 2a 2f 20 22 74 63 6f  . /*  95 */ "tco
30205 6e 73 20 3a 3a 3d 20 43 48 45 43 4b 20 4c 50 20  ns ::= CHECK LP 
30206 65 78 70 72 20 52 50 20 6f 6e 63 6f 6e 66 22 2c  expr RP onconf",
30207 0a 20 2f 2a 20 20 39 36 20 2a 2f 20 22 74 63 6f  . /*  96 */ "tco
30208 6e 73 20 3a 3a 3d 20 46 4f 52 45 49 47 4e 20 4b  ns ::= FOREIGN K
30209 45 59 20 4c 50 20 69 64 78 6c 69 73 74 20 52 50  EY LP idxlist RP
3020a 20 52 45 46 45 52 45 4e 43 45 53 20 6e 6d 20 69   REFERENCES nm i
3020b 64 78 6c 69 73 74 5f 6f 70 74 20 72 65 66 61 72  dxlist_opt refar
3020c 67 73 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75  gs defer_subclau
3020d 73 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 39 37  se_opt",. /*  97
3020e 20 2a 2f 20 22 64 65 66 65 72 5f 73 75 62 63 6c   */ "defer_subcl
3020f 61 75 73 65 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20  ause_opt ::=",. 
30210 2f 2a 20 20 39 38 20 2a 2f 20 22 64 65 66 65 72  /*  98 */ "defer
30211 5f 73 75 62 63 6c 61 75 73 65 5f 6f 70 74 20 3a  _subclause_opt :
30212 3a 3d 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75  := defer_subclau
30213 73 65 22 2c 0a 20 2f 2a 20 20 39 39 20 2a 2f 20  se",. /*  99 */ 
30214 22 6f 6e 63 6f 6e 66 20 3a 3a 3d 22 2c 0a 20 2f  "onconf ::=",. /
30215 2a 20 31 30 30 20 2a 2f 20 22 6f 6e 63 6f 6e 66  * 100 */ "onconf
30216 20 3a 3a 3d 20 4f 4e 20 43 4f 4e 46 4c 49 43 54   ::= ON CONFLICT
30217 20 72 65 73 6f 6c 76 65 74 79 70 65 22 2c 0a 20   resolvetype",. 
30218 2f 2a 20 31 30 31 20 2a 2f 20 22 6f 72 63 6f 6e  /* 101 */ "orcon
30219 66 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 30 32 20  f ::=",. /* 102 
3021a 2a 2f 20 22 6f 72 63 6f 6e 66 20 3a 3a 3d 20 4f  */ "orconf ::= O
3021b 52 20 72 65 73 6f 6c 76 65 74 79 70 65 22 2c 0a  R resolvetype",.
3021c 20 2f 2a 20 31 30 33 20 2a 2f 20 22 72 65 73 6f   /* 103 */ "reso
3021d 6c 76 65 74 79 70 65 20 3a 3a 3d 20 72 61 69 73  lvetype ::= rais
3021e 65 74 79 70 65 22 2c 0a 20 2f 2a 20 31 30 34 20  etype",. /* 104 
3021f 2a 2f 20 22 72 65 73 6f 6c 76 65 74 79 70 65 20  */ "resolvetype 
30220 3a 3a 3d 20 49 47 4e 4f 52 45 22 2c 0a 20 2f 2a  ::= IGNORE",. /*
30221 20 31 30 35 20 2a 2f 20 22 72 65 73 6f 6c 76 65   105 */ "resolve
30222 74 79 70 65 20 3a 3a 3d 20 52 45 50 4c 41 43 45  type ::= REPLACE
30223 22 2c 0a 20 2f 2a 20 31 30 36 20 2a 2f 20 22 63  ",. /* 106 */ "c
30224 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 54 41 42 4c  md ::= DROP TABL
30225 45 20 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e  E ifexists fulln
30226 61 6d 65 22 2c 0a 20 2f 2a 20 31 30 37 20 2a 2f  ame",. /* 107 */
30227 20 22 69 66 65 78 69 73 74 73 20 3a 3a 3d 20 49   "ifexists ::= I
30228 46 20 45 58 49 53 54 53 22 2c 0a 20 2f 2a 20 31  F EXISTS",. /* 1
30229 30 38 20 2a 2f 20 22 69 66 65 78 69 73 74 73 20  08 */ "ifexists 
3022a 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 30 39 20 2a 2f  ::=",. /* 109 */
3022b 20 22 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65   "cmd ::= create
3022c 6b 77 20 74 65 6d 70 20 56 49 45 57 20 69 66 6e  kw temp VIEW ifn
3022d 6f 74 65 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d  otexists nm dbnm
3022e 20 41 53 20 73 65 6c 65 63 74 22 2c 0a 20 2f 2a   AS select",. /*
3022f 20 31 31 30 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   110 */ "cmd ::=
30230 20 44 52 4f 50 20 56 49 45 57 20 69 66 65 78 69   DROP VIEW ifexi
30231 73 74 73 20 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20  sts fullname",. 
30232 2f 2a 20 31 31 31 20 2a 2f 20 22 63 6d 64 20 3a  /* 111 */ "cmd :
30233 3a 3d 20 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20  := select",. /* 
30234 31 31 32 20 2a 2f 20 22 73 65 6c 65 63 74 20 3a  112 */ "select :
30235 3a 3d 20 6f 6e 65 73 65 6c 65 63 74 22 2c 0a 20  := oneselect",. 
30236 2f 2a 20 31 31 33 20 2a 2f 20 22 73 65 6c 65 63  /* 113 */ "selec
30237 74 20 3a 3a 3d 20 73 65 6c 65 63 74 20 6d 75 6c  t ::= select mul
30238 74 69 73 65 6c 65 63 74 5f 6f 70 20 6f 6e 65 73  tiselect_op ones
30239 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31 31 34 20  elect",. /* 114 
3023a 2a 2f 20 22 6d 75 6c 74 69 73 65 6c 65 63 74 5f  */ "multiselect_
3023b 6f 70 20 3a 3a 3d 20 55 4e 49 4f 4e 22 2c 0a 20  op ::= UNION",. 
3023c 2f 2a 20 31 31 35 20 2a 2f 20 22 6d 75 6c 74 69  /* 115 */ "multi
3023d 73 65 6c 65 63 74 5f 6f 70 20 3a 3a 3d 20 55 4e  select_op ::= UN
3023e 49 4f 4e 20 41 4c 4c 22 2c 0a 20 2f 2a 20 31 31  ION ALL",. /* 11
3023f 36 20 2a 2f 20 22 6d 75 6c 74 69 73 65 6c 65 63  6 */ "multiselec
30240 74 5f 6f 70 20 3a 3a 3d 20 45 58 43 45 50 54 7c  t_op ::= EXCEPT|
30241 49 4e 54 45 52 53 45 43 54 22 2c 0a 20 2f 2a 20  INTERSECT",. /* 
30242 31 31 37 20 2a 2f 20 22 6f 6e 65 73 65 6c 65 63  117 */ "oneselec
30243 74 20 3a 3a 3d 20 53 45 4c 45 43 54 20 64 69 73  t ::= SELECT dis
30244 74 69 6e 63 74 20 73 65 6c 63 6f 6c 6c 69 73 74  tinct selcollist
30245 20 66 72 6f 6d 20 77 68 65 72 65 5f 6f 70 74 20   from where_opt 
30246 67 72 6f 75 70 62 79 5f 6f 70 74 20 68 61 76 69  groupby_opt havi
30247 6e 67 5f 6f 70 74 20 6f 72 64 65 72 62 79 5f 6f  ng_opt orderby_o
30248 70 74 20 6c 69 6d 69 74 5f 6f 70 74 22 2c 0a 20  pt limit_opt",. 
30249 2f 2a 20 31 31 38 20 2a 2f 20 22 64 69 73 74 69  /* 118 */ "disti
3024a 6e 63 74 20 3a 3a 3d 20 44 49 53 54 49 4e 43 54  nct ::= DISTINCT
3024b 22 2c 0a 20 2f 2a 20 31 31 39 20 2a 2f 20 22 64  ",. /* 119 */ "d
3024c 69 73 74 69 6e 63 74 20 3a 3a 3d 20 41 4c 4c 22  istinct ::= ALL"
3024d 2c 0a 20 2f 2a 20 31 32 30 20 2a 2f 20 22 64 69  ,. /* 120 */ "di
3024e 73 74 69 6e 63 74 20 3a 3a 3d 22 2c 0a 20 2f 2a  stinct ::=",. /*
3024f 20 31 32 31 20 2a 2f 20 22 73 63 6c 70 20 3a 3a   121 */ "sclp ::
30250 3d 20 73 65 6c 63 6f 6c 6c 69 73 74 20 43 4f 4d  = selcollist COM
30251 4d 41 22 2c 0a 20 2f 2a 20 31 32 32 20 2a 2f 20  MA",. /* 122 */ 
30252 22 73 63 6c 70 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  "sclp ::=",. /* 
30253 31 32 33 20 2a 2f 20 22 73 65 6c 63 6f 6c 6c 69  123 */ "selcolli
30254 73 74 20 3a 3a 3d 20 73 63 6c 70 20 65 78 70 72  st ::= sclp expr
30255 20 61 73 22 2c 0a 20 2f 2a 20 31 32 34 20 2a 2f   as",. /* 124 */
30256 20 22 73 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d   "selcollist ::=
30257 20 73 63 6c 70 20 53 54 41 52 22 2c 0a 20 2f 2a   sclp STAR",. /*
30258 20 31 32 35 20 2a 2f 20 22 73 65 6c 63 6f 6c 6c   125 */ "selcoll
30259 69 73 74 20 3a 3a 3d 20 73 63 6c 70 20 6e 6d 20  ist ::= sclp nm 
3025a 44 4f 54 20 53 54 41 52 22 2c 0a 20 2f 2a 20 31  DOT STAR",. /* 1
3025b 32 36 20 2a 2f 20 22 61 73 20 3a 3a 3d 20 41 53  26 */ "as ::= AS
3025c 20 6e 6d 22 2c 0a 20 2f 2a 20 31 32 37 20 2a 2f   nm",. /* 127 */
3025d 20 22 61 73 20 3a 3a 3d 20 69 64 73 22 2c 0a 20   "as ::= ids",. 
3025e 2f 2a 20 31 32 38 20 2a 2f 20 22 61 73 20 3a 3a  /* 128 */ "as ::
3025f 3d 22 2c 0a 20 2f 2a 20 31 32 39 20 2a 2f 20 22  =",. /* 129 */ "
30260 66 72 6f 6d 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31  from ::=",. /* 1
30261 33 30 20 2a 2f 20 22 66 72 6f 6d 20 3a 3a 3d 20  30 */ "from ::= 
30262 46 52 4f 4d 20 73 65 6c 74 61 62 6c 69 73 74 22  FROM seltablist"
30263 2c 0a 20 2f 2a 20 31 33 31 20 2a 2f 20 22 73 74  ,. /* 131 */ "st
30264 6c 5f 70 72 65 66 69 78 20 3a 3a 3d 20 73 65 6c  l_prefix ::= sel
30265 74 61 62 6c 69 73 74 20 6a 6f 69 6e 6f 70 22 2c  tablist joinop",
30266 0a 20 2f 2a 20 31 33 32 20 2a 2f 20 22 73 74 6c  . /* 132 */ "stl
30267 5f 70 72 65 66 69 78 20 3a 3a 3d 22 2c 0a 20 2f  _prefix ::=",. /
30268 2a 20 31 33 33 20 2a 2f 20 22 73 65 6c 74 61 62  * 133 */ "seltab
30269 6c 69 73 74 20 3a 3a 3d 20 73 74 6c 5f 70 72 65  list ::= stl_pre
3026a 66 69 78 20 6e 6d 20 64 62 6e 6d 20 61 73 20 69  fix nm dbnm as i
3026b 6e 64 65 78 65 64 5f 6f 70 74 20 6f 6e 5f 6f 70  ndexed_opt on_op
3026c 74 20 75 73 69 6e 67 5f 6f 70 74 22 2c 0a 20 2f  t using_opt",. /
3026d 2a 20 31 33 34 20 2a 2f 20 22 73 65 6c 74 61 62  * 134 */ "seltab
3026e 6c 69 73 74 20 3a 3a 3d 20 73 74 6c 5f 70 72 65  list ::= stl_pre
3026f 66 69 78 20 4c 50 20 73 65 6c 65 63 74 20 52 50  fix LP select RP
30270 20 61 73 20 6f 6e 5f 6f 70 74 20 75 73 69 6e 67   as on_opt using
30271 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31 33 35 20 2a  _opt",. /* 135 *
30272 2f 20 22 73 65 6c 74 61 62 6c 69 73 74 20 3a 3a  / "seltablist ::
30273 3d 20 73 74 6c 5f 70 72 65 66 69 78 20 4c 50 20  = stl_prefix LP 
30274 73 65 6c 74 61 62 6c 69 73 74 20 52 50 20 61 73  seltablist RP as
30275 20 6f 6e 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70   on_opt using_op
30276 74 22 2c 0a 20 2f 2a 20 31 33 36 20 2a 2f 20 22  t",. /* 136 */ "
30277 64 62 6e 6d 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31  dbnm ::=",. /* 1
30278 33 37 20 2a 2f 20 22 64 62 6e 6d 20 3a 3a 3d 20  37 */ "dbnm ::= 
30279 44 4f 54 20 6e 6d 22 2c 0a 20 2f 2a 20 31 33 38  DOT nm",. /* 138
3027a 20 2a 2f 20 22 66 75 6c 6c 6e 61 6d 65 20 3a 3a   */ "fullname ::
3027b 3d 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a 20  = nm dbnm",. /* 
3027c 31 33 39 20 2a 2f 20 22 6a 6f 69 6e 6f 70 20 3a  139 */ "joinop :
3027d 3a 3d 20 43 4f 4d 4d 41 7c 4a 4f 49 4e 22 2c 0a  := COMMA|JOIN",.
3027e 20 2f 2a 20 31 34 30 20 2a 2f 20 22 6a 6f 69 6e   /* 140 */ "join
3027f 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 4a  op ::= JOIN_KW J
30280 4f 49 4e 22 2c 0a 20 2f 2a 20 31 34 31 20 2a 2f  OIN",. /* 141 */
30281 20 22 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49   "joinop ::= JOI
30282 4e 5f 4b 57 20 6e 6d 20 4a 4f 49 4e 22 2c 0a 20  N_KW nm JOIN",. 
30283 2f 2a 20 31 34 32 20 2a 2f 20 22 6a 6f 69 6e 6f  /* 142 */ "joino
30284 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 6e 6d  p ::= JOIN_KW nm
30285 20 6e 6d 20 4a 4f 49 4e 22 2c 0a 20 2f 2a 20 31   nm JOIN",. /* 1
30286 34 33 20 2a 2f 20 22 6f 6e 5f 6f 70 74 20 3a 3a  43 */ "on_opt ::
30287 3d 20 4f 4e 20 65 78 70 72 22 2c 0a 20 2f 2a 20  = ON expr",. /* 
30288 31 34 34 20 2a 2f 20 22 6f 6e 5f 6f 70 74 20 3a  144 */ "on_opt :
30289 3a 3d 22 2c 0a 20 2f 2a 20 31 34 35 20 2a 2f 20  :=",. /* 145 */ 
3028a 22 69 6e 64 65 78 65 64 5f 6f 70 74 20 3a 3a 3d  "indexed_opt ::=
3028b 22 2c 0a 20 2f 2a 20 31 34 36 20 2a 2f 20 22 69  ",. /* 146 */ "i
3028c 6e 64 65 78 65 64 5f 6f 70 74 20 3a 3a 3d 20 49  ndexed_opt ::= I
3028d 4e 44 45 58 45 44 20 42 59 20 6e 6d 22 2c 0a 20  NDEXED BY nm",. 
3028e 2f 2a 20 31 34 37 20 2a 2f 20 22 69 6e 64 65 78  /* 147 */ "index
3028f 65 64 5f 6f 70 74 20 3a 3a 3d 20 4e 4f 54 20 49  ed_opt ::= NOT I
30290 4e 44 45 58 45 44 22 2c 0a 20 2f 2a 20 31 34 38  NDEXED",. /* 148
30291 20 2a 2f 20 22 75 73 69 6e 67 5f 6f 70 74 20 3a   */ "using_opt :
30292 3a 3d 20 55 53 49 4e 47 20 4c 50 20 69 6e 73 63  := USING LP insc
30293 6f 6c 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20  ollist RP",. /* 
30294 31 34 39 20 2a 2f 20 22 75 73 69 6e 67 5f 6f 70  149 */ "using_op
30295 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 35 30 20  t ::=",. /* 150 
30296 2a 2f 20 22 6f 72 64 65 72 62 79 5f 6f 70 74 20  */ "orderby_opt 
30297 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 35 31 20 2a 2f  ::=",. /* 151 */
30298 20 22 6f 72 64 65 72 62 79 5f 6f 70 74 20 3a 3a   "orderby_opt ::
30299 3d 20 4f 52 44 45 52 20 42 59 20 73 6f 72 74 6c  = ORDER BY sortl
3029a 69 73 74 22 2c 0a 20 2f 2a 20 31 35 32 20 2a 2f  ist",. /* 152 */
3029b 20 22 73 6f 72 74 6c 69 73 74 20 3a 3a 3d 20 73   "sortlist ::= s
3029c 6f 72 74 6c 69 73 74 20 43 4f 4d 4d 41 20 73 6f  ortlist COMMA so
3029d 72 74 69 74 65 6d 20 73 6f 72 74 6f 72 64 65 72  rtitem sortorder
3029e 22 2c 0a 20 2f 2a 20 31 35 33 20 2a 2f 20 22 73  ",. /* 153 */ "s
3029f 6f 72 74 6c 69 73 74 20 3a 3a 3d 20 73 6f 72 74  ortlist ::= sort
302a0 69 74 65 6d 20 73 6f 72 74 6f 72 64 65 72 22 2c  item sortorder",
302a1 0a 20 2f 2a 20 31 35 34 20 2a 2f 20 22 73 6f 72  . /* 154 */ "sor
302a2 74 69 74 65 6d 20 3a 3a 3d 20 65 78 70 72 22 2c  titem ::= expr",
302a3 0a 20 2f 2a 20 31 35 35 20 2a 2f 20 22 73 6f 72  . /* 155 */ "sor
302a4 74 6f 72 64 65 72 20 3a 3a 3d 20 41 53 43 22 2c  torder ::= ASC",
302a5 0a 20 2f 2a 20 31 35 36 20 2a 2f 20 22 73 6f 72  . /* 156 */ "sor
302a6 74 6f 72 64 65 72 20 3a 3a 3d 20 44 45 53 43 22  torder ::= DESC"
302a7 2c 0a 20 2f 2a 20 31 35 37 20 2a 2f 20 22 73 6f  ,. /* 157 */ "so
302a8 72 74 6f 72 64 65 72 20 3a 3a 3d 22 2c 0a 20 2f  rtorder ::=",. /
302a9 2a 20 31 35 38 20 2a 2f 20 22 67 72 6f 75 70 62  * 158 */ "groupb
302aa 79 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  y_opt ::=",. /* 
302ab 31 35 39 20 2a 2f 20 22 67 72 6f 75 70 62 79 5f  159 */ "groupby_
302ac 6f 70 74 20 3a 3a 3d 20 47 52 4f 55 50 20 42 59  opt ::= GROUP BY
302ad 20 6e 65 78 70 72 6c 69 73 74 22 2c 0a 20 2f 2a   nexprlist",. /*
302ae 20 31 36 30 20 2a 2f 20 22 68 61 76 69 6e 67 5f   160 */ "having_
302af 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 36  opt ::=",. /* 16
302b0 31 20 2a 2f 20 22 68 61 76 69 6e 67 5f 6f 70 74  1 */ "having_opt
302b1 20 3a 3a 3d 20 48 41 56 49 4e 47 20 65 78 70 72   ::= HAVING expr
302b2 22 2c 0a 20 2f 2a 20 31 36 32 20 2a 2f 20 22 6c  ",. /* 162 */ "l
302b3 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20  imit_opt ::=",. 
302b4 2f 2a 20 31 36 33 20 2a 2f 20 22 6c 69 6d 69 74  /* 163 */ "limit
302b5 5f 6f 70 74 20 3a 3a 3d 20 4c 49 4d 49 54 20 65  _opt ::= LIMIT e
302b6 78 70 72 22 2c 0a 20 2f 2a 20 31 36 34 20 2a 2f  xpr",. /* 164 */
302b7 20 22 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20   "limit_opt ::= 
302b8 4c 49 4d 49 54 20 65 78 70 72 20 4f 46 46 53 45  LIMIT expr OFFSE
302b9 54 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 36 35  T expr",. /* 165
302ba 20 2a 2f 20 22 6c 69 6d 69 74 5f 6f 70 74 20 3a   */ "limit_opt :
302bb 3a 3d 20 4c 49 4d 49 54 20 65 78 70 72 20 43 4f  := LIMIT expr CO
302bc 4d 4d 41 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31  MMA expr",. /* 1
302bd 36 36 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44  66 */ "cmd ::= D
302be 45 4c 45 54 45 20 46 52 4f 4d 20 66 75 6c 6c 6e  ELETE FROM fulln
302bf 61 6d 65 20 69 6e 64 65 78 65 64 5f 6f 70 74 20  ame indexed_opt 
302c0 77 68 65 72 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20  where_opt",. /* 
302c1 31 36 37 20 2a 2f 20 22 77 68 65 72 65 5f 6f 70  167 */ "where_op
302c2 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 36 38 20  t ::=",. /* 168 
302c3 2a 2f 20 22 77 68 65 72 65 5f 6f 70 74 20 3a 3a  */ "where_opt ::
302c4 3d 20 57 48 45 52 45 20 65 78 70 72 22 2c 0a 20  = WHERE expr",. 
302c5 2f 2a 20 31 36 39 20 2a 2f 20 22 63 6d 64 20 3a  /* 169 */ "cmd :
302c6 3a 3d 20 55 50 44 41 54 45 20 6f 72 63 6f 6e 66  := UPDATE orconf
302c7 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 64 65 78 65   fullname indexe
302c8 64 5f 6f 70 74 20 53 45 54 20 73 65 74 6c 69 73  d_opt SET setlis
302c9 74 20 77 68 65 72 65 5f 6f 70 74 22 2c 0a 20 2f  t where_opt",. /
302ca 2a 20 31 37 30 20 2a 2f 20 22 73 65 74 6c 69 73  * 170 */ "setlis
302cb 74 20 3a 3a 3d 20 73 65 74 6c 69 73 74 20 43 4f  t ::= setlist CO
302cc 4d 4d 41 20 6e 6d 20 45 51 20 65 78 70 72 22 2c  MMA nm EQ expr",
302cd 0a 20 2f 2a 20 31 37 31 20 2a 2f 20 22 73 65 74  . /* 171 */ "set
302ce 6c 69 73 74 20 3a 3a 3d 20 6e 6d 20 45 51 20 65  list ::= nm EQ e
302cf 78 70 72 22 2c 0a 20 2f 2a 20 31 37 32 20 2a 2f  xpr",. /* 172 */
302d0 20 22 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74   "cmd ::= insert
302d1 5f 63 6d 64 20 49 4e 54 4f 20 66 75 6c 6c 6e 61  _cmd INTO fullna
302d2 6d 65 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70  me inscollist_op
302d3 74 20 56 41 4c 55 45 53 20 4c 50 20 69 74 65 6d  t VALUES LP item
302d4 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 31 37  list RP",. /* 17
302d5 33 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 69 6e  3 */ "cmd ::= in
302d6 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20 66 75  sert_cmd INTO fu
302d7 6c 6c 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c 69 73  llname inscollis
302d8 74 5f 6f 70 74 20 73 65 6c 65 63 74 22 2c 0a 20  t_opt select",. 
302d9 2f 2a 20 31 37 34 20 2a 2f 20 22 63 6d 64 20 3a  /* 174 */ "cmd :
302da 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e  := insert_cmd IN
302db 54 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 73 63  TO fullname insc
302dc 6f 6c 6c 69 73 74 5f 6f 70 74 20 44 45 46 41 55  ollist_opt DEFAU
302dd 4c 54 20 56 41 4c 55 45 53 22 2c 0a 20 2f 2a 20  LT VALUES",. /* 
302de 31 37 35 20 2a 2f 20 22 69 6e 73 65 72 74 5f 63  175 */ "insert_c
302df 6d 64 20 3a 3a 3d 20 49 4e 53 45 52 54 20 6f 72  md ::= INSERT or
302e0 63 6f 6e 66 22 2c 0a 20 2f 2a 20 31 37 36 20 2a  conf",. /* 176 *
302e1 2f 20 22 69 6e 73 65 72 74 5f 63 6d 64 20 3a 3a  / "insert_cmd ::
302e2 3d 20 52 45 50 4c 41 43 45 22 2c 0a 20 2f 2a 20  = REPLACE",. /* 
302e3 31 37 37 20 2a 2f 20 22 69 74 65 6d 6c 69 73 74  177 */ "itemlist
302e4 20 3a 3a 3d 20 69 74 65 6d 6c 69 73 74 20 43 4f   ::= itemlist CO
302e5 4d 4d 41 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31  MMA expr",. /* 1
302e6 37 38 20 2a 2f 20 22 69 74 65 6d 6c 69 73 74 20  78 */ "itemlist 
302e7 3a 3a 3d 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31  ::= expr",. /* 1
302e8 37 39 20 2a 2f 20 22 69 6e 73 63 6f 6c 6c 69 73  79 */ "inscollis
302e9 74 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  t_opt ::=",. /* 
302ea 31 38 30 20 2a 2f 20 22 69 6e 73 63 6f 6c 6c 69  180 */ "inscolli
302eb 73 74 5f 6f 70 74 20 3a 3a 3d 20 4c 50 20 69 6e  st_opt ::= LP in
302ec 73 63 6f 6c 6c 69 73 74 20 52 50 22 2c 0a 20 2f  scollist RP",. /
302ed 2a 20 31 38 31 20 2a 2f 20 22 69 6e 73 63 6f 6c  * 181 */ "inscol
302ee 6c 69 73 74 20 3a 3a 3d 20 69 6e 73 63 6f 6c 6c  list ::= inscoll
302ef 69 73 74 20 43 4f 4d 4d 41 20 6e 6d 22 2c 0a 20  ist COMMA nm",. 
302f0 2f 2a 20 31 38 32 20 2a 2f 20 22 69 6e 73 63 6f  /* 182 */ "insco
302f1 6c 6c 69 73 74 20 3a 3a 3d 20 6e 6d 22 2c 0a 20  llist ::= nm",. 
302f2 2f 2a 20 31 38 33 20 2a 2f 20 22 65 78 70 72 20  /* 183 */ "expr 
302f3 3a 3a 3d 20 74 65 72 6d 22 2c 0a 20 2f 2a 20 31  ::= term",. /* 1
302f4 38 34 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  84 */ "expr ::= 
302f5 4c 50 20 65 78 70 72 20 52 50 22 2c 0a 20 2f 2a  LP expr RP",. /*
302f6 20 31 38 35 20 2a 2f 20 22 74 65 72 6d 20 3a 3a   185 */ "term ::
302f7 3d 20 4e 55 4c 4c 22 2c 0a 20 2f 2a 20 31 38 36  = NULL",. /* 186
302f8 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 69 64   */ "expr ::= id
302f9 22 2c 0a 20 2f 2a 20 31 38 37 20 2a 2f 20 22 65  ",. /* 187 */ "e
302fa 78 70 72 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 22  xpr ::= JOIN_KW"
302fb 2c 0a 20 2f 2a 20 31 38 38 20 2a 2f 20 22 65 78  ,. /* 188 */ "ex
302fc 70 72 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d  pr ::= nm DOT nm
302fd 22 2c 0a 20 2f 2a 20 31 38 39 20 2a 2f 20 22 65  ",. /* 189 */ "e
302fe 78 70 72 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e  xpr ::= nm DOT n
302ff 6d 20 44 4f 54 20 6e 6d 22 2c 0a 20 2f 2a 20 31  m DOT nm",. /* 1
30300 39 30 20 2a 2f 20 22 74 65 72 6d 20 3a 3a 3d 20  90 */ "term ::= 
30301 49 4e 54 45 47 45 52 7c 46 4c 4f 41 54 7c 42 4c  INTEGER|FLOAT|BL
30302 4f 42 22 2c 0a 20 2f 2a 20 31 39 31 20 2a 2f 20  OB",. /* 191 */ 
30303 22 74 65 72 6d 20 3a 3a 3d 20 53 54 52 49 4e 47  "term ::= STRING
30304 22 2c 0a 20 2f 2a 20 31 39 32 20 2a 2f 20 22 65  ",. /* 192 */ "e
30305 78 70 72 20 3a 3a 3d 20 52 45 47 49 53 54 45 52  xpr ::= REGISTER
30306 22 2c 0a 20 2f 2a 20 31 39 33 20 2a 2f 20 22 65  ",. /* 193 */ "e
30307 78 70 72 20 3a 3a 3d 20 56 41 52 49 41 42 4c 45  xpr ::= VARIABLE
30308 22 2c 0a 20 2f 2a 20 31 39 34 20 2a 2f 20 22 65  ",. /* 194 */ "e
30309 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 43 4f 4c  xpr ::= expr COL
3030a 4c 41 54 45 20 69 64 73 22 2c 0a 20 2f 2a 20 31  LATE ids",. /* 1
3030b 39 35 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  95 */ "expr ::= 
3030c 43 41 53 54 20 4c 50 20 65 78 70 72 20 41 53 20  CAST LP expr AS 
3030d 74 79 70 65 74 6f 6b 65 6e 20 52 50 22 2c 0a 20  typetoken RP",. 
3030e 2f 2a 20 31 39 36 20 2a 2f 20 22 65 78 70 72 20  /* 196 */ "expr 
3030f 3a 3a 3d 20 49 44 20 4c 50 20 64 69 73 74 69 6e  ::= ID LP distin
30310 63 74 20 65 78 70 72 6c 69 73 74 20 52 50 22 2c  ct exprlist RP",
30311 0a 20 2f 2a 20 31 39 37 20 2a 2f 20 22 65 78 70  . /* 197 */ "exp
30312 72 20 3a 3a 3d 20 49 44 20 4c 50 20 53 54 41 52  r ::= ID LP STAR
30313 20 52 50 22 2c 0a 20 2f 2a 20 31 39 38 20 2a 2f   RP",. /* 198 */
30314 20 22 74 65 72 6d 20 3a 3a 3d 20 43 54 49 4d 45   "term ::= CTIME
30315 5f 4b 57 22 2c 0a 20 2f 2a 20 31 39 39 20 2a 2f  _KW",. /* 199 */
30316 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20   "expr ::= expr 
30317 41 4e 44 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32  AND expr",. /* 2
30318 30 30 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  00 */ "expr ::= 
30319 65 78 70 72 20 4f 52 20 65 78 70 72 22 2c 0a 20  expr OR expr",. 
3031a 2f 2a 20 32 30 31 20 2a 2f 20 22 65 78 70 72 20  /* 201 */ "expr 
3031b 3a 3a 3d 20 65 78 70 72 20 4c 54 7c 47 54 7c 47  ::= expr LT|GT|G
3031c 45 7c 4c 45 20 65 78 70 72 22 2c 0a 20 2f 2a 20  E|LE expr",. /* 
3031d 32 30 32 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  202 */ "expr ::=
3031e 20 65 78 70 72 20 45 51 7c 4e 45 20 65 78 70 72   expr EQ|NE expr
3031f 22 2c 0a 20 2f 2a 20 32 30 33 20 2a 2f 20 22 65  ",. /* 203 */ "e
30320 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 42 49 54  xpr ::= expr BIT
30321 41 4e 44 7c 42 49 54 4f 52 7c 4c 53 48 49 46 54  AND|BITOR|LSHIFT
30322 7c 52 53 48 49 46 54 20 65 78 70 72 22 2c 0a 20  |RSHIFT expr",. 
30323 2f 2a 20 32 30 34 20 2a 2f 20 22 65 78 70 72 20  /* 204 */ "expr 
30324 3a 3a 3d 20 65 78 70 72 20 50 4c 55 53 7c 4d 49  ::= expr PLUS|MI
30325 4e 55 53 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32  NUS expr",. /* 2
30326 30 35 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  05 */ "expr ::= 
30327 65 78 70 72 20 53 54 41 52 7c 53 4c 41 53 48 7c  expr STAR|SLASH|
30328 52 45 4d 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32  REM expr",. /* 2
30329 30 36 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  06 */ "expr ::= 
3032a 65 78 70 72 20 43 4f 4e 43 41 54 20 65 78 70 72  expr CONCAT expr
3032b 22 2c 0a 20 2f 2a 20 32 30 37 20 2a 2f 20 22 6c  ",. /* 207 */ "l
3032c 69 6b 65 6f 70 20 3a 3a 3d 20 4c 49 4b 45 5f 4b  ikeop ::= LIKE_K
3032d 57 22 2c 0a 20 2f 2a 20 32 30 38 20 2a 2f 20 22  W",. /* 208 */ "
3032e 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4e 4f 54 20 4c  likeop ::= NOT L
3032f 49 4b 45 5f 4b 57 22 2c 0a 20 2f 2a 20 32 30 39  IKE_KW",. /* 209
30330 20 2a 2f 20 22 6c 69 6b 65 6f 70 20 3a 3a 3d 20   */ "likeop ::= 
30331 4d 41 54 43 48 22 2c 0a 20 2f 2a 20 32 31 30 20  MATCH",. /* 210 
30332 2a 2f 20 22 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4e  */ "likeop ::= N
30333 4f 54 20 4d 41 54 43 48 22 2c 0a 20 2f 2a 20 32  OT MATCH",. /* 2
30334 31 31 20 2a 2f 20 22 65 73 63 61 70 65 20 3a 3a  11 */ "escape ::
30335 3d 20 45 53 43 41 50 45 20 65 78 70 72 22 2c 0a  = ESCAPE expr",.
30336 20 2f 2a 20 32 31 32 20 2a 2f 20 22 65 73 63 61   /* 212 */ "esca
30337 70 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 31 33  pe ::=",. /* 213
30338 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78   */ "expr ::= ex
30339 70 72 20 6c 69 6b 65 6f 70 20 65 78 70 72 20 65  pr likeop expr e
3033a 73 63 61 70 65 22 2c 0a 20 2f 2a 20 32 31 34 20  scape",. /* 214 
3033b 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70  */ "expr ::= exp
3033c 72 20 49 53 4e 55 4c 4c 7c 4e 4f 54 4e 55 4c 4c  r ISNULL|NOTNULL
3033d 22 2c 0a 20 2f 2a 20 32 31 35 20 2a 2f 20 22 65  ",. /* 215 */ "e
3033e 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 49 53 20  xpr ::= expr IS 
3033f 4e 55 4c 4c 22 2c 0a 20 2f 2a 20 32 31 36 20 2a  NULL",. /* 216 *
30340 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  / "expr ::= expr
30341 20 4e 4f 54 20 4e 55 4c 4c 22 2c 0a 20 2f 2a 20   NOT NULL",. /* 
30342 32 31 37 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  217 */ "expr ::=
30343 20 65 78 70 72 20 49 53 20 4e 4f 54 20 4e 55 4c   expr IS NOT NUL
30344 4c 22 2c 0a 20 2f 2a 20 32 31 38 20 2a 2f 20 22  L",. /* 218 */ "
30345 65 78 70 72 20 3a 3a 3d 20 4e 4f 54 20 65 78 70  expr ::= NOT exp
30346 72 22 2c 0a 20 2f 2a 20 32 31 39 20 2a 2f 20 22  r",. /* 219 */ "
30347 65 78 70 72 20 3a 3a 3d 20 42 49 54 4e 4f 54 20  expr ::= BITNOT 
30348 65 78 70 72 22 2c 0a 20 2f 2a 20 32 32 30 20 2a  expr",. /* 220 *
30349 2f 20 22 65 78 70 72 20 3a 3a 3d 20 4d 49 4e 55  / "expr ::= MINU
3034a 53 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 32 31  S expr",. /* 221
3034b 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 50 4c   */ "expr ::= PL
3034c 55 53 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 32  US expr",. /* 22
3034d 32 20 2a 2f 20 22 62 65 74 77 65 65 6e 5f 6f 70  2 */ "between_op
3034e 20 3a 3a 3d 20 42 45 54 57 45 45 4e 22 2c 0a 20   ::= BETWEEN",. 
3034f 2f 2a 20 32 32 33 20 2a 2f 20 22 62 65 74 77 65  /* 223 */ "betwe
30350 65 6e 5f 6f 70 20 3a 3a 3d 20 4e 4f 54 20 42 45  en_op ::= NOT BE
30351 54 57 45 45 4e 22 2c 0a 20 2f 2a 20 32 32 34 20  TWEEN",. /* 224 
30352 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70  */ "expr ::= exp
30353 72 20 62 65 74 77 65 65 6e 5f 6f 70 20 65 78 70  r between_op exp
30354 72 20 41 4e 44 20 65 78 70 72 22 2c 0a 20 2f 2a  r AND expr",. /*
30355 20 32 32 35 20 2a 2f 20 22 69 6e 5f 6f 70 20 3a   225 */ "in_op :
30356 3a 3d 20 49 4e 22 2c 0a 20 2f 2a 20 32 32 36 20  := IN",. /* 226 
30357 2a 2f 20 22 69 6e 5f 6f 70 20 3a 3a 3d 20 4e 4f  */ "in_op ::= NO
30358 54 20 49 4e 22 2c 0a 20 2f 2a 20 32 32 37 20 2a  T IN",. /* 227 *
30359 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  / "expr ::= expr
3035a 20 69 6e 5f 6f 70 20 4c 50 20 65 78 70 72 6c 69   in_op LP exprli
3035b 73 74 20 52 50 22 2c 0a 20 2f 2a 20 32 32 38 20  st RP",. /* 228 
3035c 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 4c 50 20  */ "expr ::= LP 
3035d 73 65 6c 65 63 74 20 52 50 22 2c 0a 20 2f 2a 20  select RP",. /* 
3035e 32 32 39 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  229 */ "expr ::=
3035f 20 65 78 70 72 20 69 6e 5f 6f 70 20 4c 50 20 73   expr in_op LP s
30360 65 6c 65 63 74 20 52 50 22 2c 0a 20 2f 2a 20 32  elect RP",. /* 2
30361 33 30 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  30 */ "expr ::= 
30362 65 78 70 72 20 69 6e 5f 6f 70 20 6e 6d 20 64 62  expr in_op nm db
30363 6e 6d 22 2c 0a 20 2f 2a 20 32 33 31 20 2a 2f 20  nm",. /* 231 */ 
30364 22 65 78 70 72 20 3a 3a 3d 20 45 58 49 53 54 53  "expr ::= EXISTS
30365 20 4c 50 20 73 65 6c 65 63 74 20 52 50 22 2c 0a   LP select RP",.
30366 20 2f 2a 20 32 33 32 20 2a 2f 20 22 65 78 70 72   /* 232 */ "expr
30367 20 3a 3a 3d 20 43 41 53 45 20 63 61 73 65 5f 6f   ::= CASE case_o
30368 70 65 72 61 6e 64 20 63 61 73 65 5f 65 78 70 72  perand case_expr
30369 6c 69 73 74 20 63 61 73 65 5f 65 6c 73 65 20 45  list case_else E
3036a 4e 44 22 2c 0a 20 2f 2a 20 32 33 33 20 2a 2f 20  ND",. /* 233 */ 
3036b 22 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20 3a  "case_exprlist :
3036c 3a 3d 20 63 61 73 65 5f 65 78 70 72 6c 69 73 74  := case_exprlist
3036d 20 57 48 45 4e 20 65 78 70 72 20 54 48 45 4e 20   WHEN expr THEN 
3036e 65 78 70 72 22 2c 0a 20 2f 2a 20 32 33 34 20 2a  expr",. /* 234 *
3036f 2f 20 22 63 61 73 65 5f 65 78 70 72 6c 69 73 74  / "case_exprlist
30370 20 3a 3a 3d 20 57 48 45 4e 20 65 78 70 72 20 54   ::= WHEN expr T
30371 48 45 4e 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32  HEN expr",. /* 2
30372 33 35 20 2a 2f 20 22 63 61 73 65 5f 65 6c 73 65  35 */ "case_else
30373 20 3a 3a 3d 20 45 4c 53 45 20 65 78 70 72 22 2c   ::= ELSE expr",
30374 0a 20 2f 2a 20 32 33 36 20 2a 2f 20 22 63 61 73  . /* 236 */ "cas
30375 65 5f 65 6c 73 65 20 3a 3a 3d 22 2c 0a 20 2f 2a  e_else ::=",. /*
30376 20 32 33 37 20 2a 2f 20 22 63 61 73 65 5f 6f 70   237 */ "case_op
30377 65 72 61 6e 64 20 3a 3a 3d 20 65 78 70 72 22 2c  erand ::= expr",
30378 0a 20 2f 2a 20 32 33 38 20 2a 2f 20 22 63 61 73  . /* 238 */ "cas
30379 65 5f 6f 70 65 72 61 6e 64 20 3a 3a 3d 22 2c 0a  e_operand ::=",.
3037a 20 2f 2a 20 32 33 39 20 2a 2f 20 22 65 78 70 72   /* 239 */ "expr
3037b 6c 69 73 74 20 3a 3a 3d 20 6e 65 78 70 72 6c 69  list ::= nexprli
3037c 73 74 22 2c 0a 20 2f 2a 20 32 34 30 20 2a 2f 20  st",. /* 240 */ 
3037d 22 65 78 70 72 6c 69 73 74 20 3a 3a 3d 22 2c 0a  "exprlist ::=",.
3037e 20 2f 2a 20 32 34 31 20 2a 2f 20 22 6e 65 78 70   /* 241 */ "nexp
3037f 72 6c 69 73 74 20 3a 3a 3d 20 6e 65 78 70 72 6c  rlist ::= nexprl
30380 69 73 74 20 43 4f 4d 4d 41 20 65 78 70 72 22 2c  ist COMMA expr",
30381 0a 20 2f 2a 20 32 34 32 20 2a 2f 20 22 6e 65 78  . /* 242 */ "nex
30382 70 72 6c 69 73 74 20 3a 3a 3d 20 65 78 70 72 22  prlist ::= expr"
30383 2c 0a 20 2f 2a 20 32 34 33 20 2a 2f 20 22 63 6d  ,. /* 243 */ "cm
30384 64 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20 75  d ::= createkw u
30385 6e 69 71 75 65 66 6c 61 67 20 49 4e 44 45 58 20  niqueflag INDEX 
30386 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20 64  ifnotexists nm d
30387 62 6e 6d 20 4f 4e 20 6e 6d 20 4c 50 20 69 64 78  bnm ON nm LP idx
30388 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 32 34  list RP",. /* 24
30389 34 20 2a 2f 20 22 75 6e 69 71 75 65 66 6c 61 67  4 */ "uniqueflag
3038a 20 3a 3a 3d 20 55 4e 49 51 55 45 22 2c 0a 20 2f   ::= UNIQUE",. /
3038b 2a 20 32 34 35 20 2a 2f 20 22 75 6e 69 71 75 65  * 245 */ "unique
3038c 66 6c 61 67 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32  flag ::=",. /* 2
3038d 34 36 20 2a 2f 20 22 69 64 78 6c 69 73 74 5f 6f  46 */ "idxlist_o
3038e 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 34 37  pt ::=",. /* 247
3038f 20 2a 2f 20 22 69 64 78 6c 69 73 74 5f 6f 70 74   */ "idxlist_opt
30390 20 3a 3a 3d 20 4c 50 20 69 64 78 6c 69 73 74 20   ::= LP idxlist 
30391 52 50 22 2c 0a 20 2f 2a 20 32 34 38 20 2a 2f 20  RP",. /* 248 */ 
30392 22 69 64 78 6c 69 73 74 20 3a 3a 3d 20 69 64 78  "idxlist ::= idx
30393 6c 69 73 74 20 43 4f 4d 4d 41 20 6e 6d 20 63 6f  list COMMA nm co
30394 6c 6c 61 74 65 20 73 6f 72 74 6f 72 64 65 72 22  llate sortorder"
30395 2c 0a 20 2f 2a 20 32 34 39 20 2a 2f 20 22 69 64  ,. /* 249 */ "id
30396 78 6c 69 73 74 20 3a 3a 3d 20 6e 6d 20 63 6f 6c  xlist ::= nm col
30397 6c 61 74 65 20 73 6f 72 74 6f 72 64 65 72 22 2c  late sortorder",
30398 0a 20 2f 2a 20 32 35 30 20 2a 2f 20 22 63 6f 6c  . /* 250 */ "col
30399 6c 61 74 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32  late ::=",. /* 2
3039a 35 31 20 2a 2f 20 22 63 6f 6c 6c 61 74 65 20 3a  51 */ "collate :
3039b 3a 3d 20 43 4f 4c 4c 41 54 45 20 69 64 73 22 2c  := COLLATE ids",
3039c 0a 20 2f 2a 20 32 35 32 20 2a 2f 20 22 63 6d 64  . /* 252 */ "cmd
3039d 20 3a 3a 3d 20 44 52 4f 50 20 49 4e 44 45 58 20   ::= DROP INDEX 
3039e 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d  ifexists fullnam
3039f 65 22 2c 0a 20 2f 2a 20 32 35 33 20 2a 2f 20 22  e",. /* 253 */ "
303a0 63 6d 64 20 3a 3a 3d 20 56 41 43 55 55 4d 22 2c  cmd ::= VACUUM",
303a1 0a 20 2f 2a 20 32 35 34 20 2a 2f 20 22 63 6d 64  . /* 254 */ "cmd
303a2 20 3a 3a 3d 20 56 41 43 55 55 4d 20 6e 6d 22 2c   ::= VACUUM nm",
303a3 0a 20 2f 2a 20 32 35 35 20 2a 2f 20 22 63 6d 64  . /* 255 */ "cmd
303a4 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64   ::= PRAGMA nm d
303a5 62 6e 6d 22 2c 0a 20 2f 2a 20 32 35 36 20 2a 2f  bnm",. /* 256 */
303a6 20 22 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41   "cmd ::= PRAGMA
303a7 20 6e 6d 20 64 62 6e 6d 20 45 51 20 6e 6d 6e 75   nm dbnm EQ nmnu
303a8 6d 22 2c 0a 20 2f 2a 20 32 35 37 20 2a 2f 20 22  m",. /* 257 */ "
303a9 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e  cmd ::= PRAGMA n
303aa 6d 20 64 62 6e 6d 20 4c 50 20 6e 6d 6e 75 6d 20  m dbnm LP nmnum 
303ab 52 50 22 2c 0a 20 2f 2a 20 32 35 38 20 2a 2f 20  RP",. /* 258 */ 
303ac 22 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20  "cmd ::= PRAGMA 
303ad 6e 6d 20 64 62 6e 6d 20 45 51 20 6d 69 6e 75 73  nm dbnm EQ minus
303ae 5f 6e 75 6d 22 2c 0a 20 2f 2a 20 32 35 39 20 2a  _num",. /* 259 *
303af 2f 20 22 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d  / "cmd ::= PRAGM
303b0 41 20 6e 6d 20 64 62 6e 6d 20 4c 50 20 6d 69 6e  A nm dbnm LP min
303b1 75 73 5f 6e 75 6d 20 52 50 22 2c 0a 20 2f 2a 20  us_num RP",. /* 
303b2 32 36 30 20 2a 2f 20 22 6e 6d 6e 75 6d 20 3a 3a  260 */ "nmnum ::
303b3 3d 20 70 6c 75 73 5f 6e 75 6d 22 2c 0a 20 2f 2a  = plus_num",. /*
303b4 20 32 36 31 20 2a 2f 20 22 6e 6d 6e 75 6d 20 3a   261 */ "nmnum :
303b5 3a 3d 20 6e 6d 22 2c 0a 20 2f 2a 20 32 36 32 20  := nm",. /* 262 
303b6 2a 2f 20 22 6e 6d 6e 75 6d 20 3a 3a 3d 20 4f 4e  */ "nmnum ::= ON
303b7 22 2c 0a 20 2f 2a 20 32 36 33 20 2a 2f 20 22 6e  ",. /* 263 */ "n
303b8 6d 6e 75 6d 20 3a 3a 3d 20 44 45 4c 45 54 45 22  mnum ::= DELETE"
303b9 2c 0a 20 2f 2a 20 32 36 34 20 2a 2f 20 22 6e 6d  ,. /* 264 */ "nm
303ba 6e 75 6d 20 3a 3a 3d 20 44 45 46 41 55 4c 54 22  num ::= DEFAULT"
303bb 2c 0a 20 2f 2a 20 32 36 35 20 2a 2f 20 22 70 6c  ,. /* 265 */ "pl
303bc 75 73 5f 6e 75 6d 20 3a 3a 3d 20 70 6c 75 73 5f  us_num ::= plus_
303bd 6f 70 74 20 6e 75 6d 62 65 72 22 2c 0a 20 2f 2a  opt number",. /*
303be 20 32 36 36 20 2a 2f 20 22 6d 69 6e 75 73 5f 6e   266 */ "minus_n
303bf 75 6d 20 3a 3a 3d 20 4d 49 4e 55 53 20 6e 75 6d  um ::= MINUS num
303c0 62 65 72 22 2c 0a 20 2f 2a 20 32 36 37 20 2a 2f  ber",. /* 267 */
303c1 20 22 6e 75 6d 62 65 72 20 3a 3a 3d 20 49 4e 54   "number ::= INT
303c2 45 47 45 52 7c 46 4c 4f 41 54 22 2c 0a 20 2f 2a  EGER|FLOAT",. /*
303c3 20 32 36 38 20 2a 2f 20 22 70 6c 75 73 5f 6f 70   268 */ "plus_op
303c4 74 20 3a 3a 3d 20 50 4c 55 53 22 2c 0a 20 2f 2a  t ::= PLUS",. /*
303c5 20 32 36 39 20 2a 2f 20 22 70 6c 75 73 5f 6f 70   269 */ "plus_op
303c6 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 37 30 20  t ::=",. /* 270 
303c7 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 63 72 65 61  */ "cmd ::= crea
303c8 74 65 6b 77 20 74 72 69 67 67 65 72 5f 64 65 63  tekw trigger_dec
303c9 6c 20 42 45 47 49 4e 20 74 72 69 67 67 65 72 5f  l BEGIN trigger_
303ca 63 6d 64 5f 6c 69 73 74 20 45 4e 44 22 2c 0a 20  cmd_list END",. 
303cb 2f 2a 20 32 37 31 20 2a 2f 20 22 74 72 69 67 67  /* 271 */ "trigg
303cc 65 72 5f 64 65 63 6c 20 3a 3a 3d 20 74 65 6d 70  er_decl ::= temp
303cd 20 54 52 49 47 47 45 52 20 69 66 6e 6f 74 65 78   TRIGGER ifnotex
303ce 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20 74 72 69  ists nm dbnm tri
303cf 67 67 65 72 5f 74 69 6d 65 20 74 72 69 67 67 65  gger_time trigge
303d0 72 5f 65 76 65 6e 74 20 4f 4e 20 66 75 6c 6c 6e  r_event ON fulln
303d1 61 6d 65 20 66 6f 72 65 61 63 68 5f 63 6c 61 75  ame foreach_clau
303d2 73 65 20 77 68 65 6e 5f 63 6c 61 75 73 65 22 2c  se when_clause",
303d3 0a 20 2f 2a 20 32 37 32 20 2a 2f 20 22 74 72 69  . /* 272 */ "tri
303d4 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20 42 45  gger_time ::= BE
303d5 46 4f 52 45 22 2c 0a 20 2f 2a 20 32 37 33 20 2a  FORE",. /* 273 *
303d6 2f 20 22 74 72 69 67 67 65 72 5f 74 69 6d 65 20  / "trigger_time 
303d7 3a 3a 3d 20 41 46 54 45 52 22 2c 0a 20 2f 2a 20  ::= AFTER",. /* 
303d8 32 37 34 20 2a 2f 20 22 74 72 69 67 67 65 72 5f  274 */ "trigger_
303d9 74 69 6d 65 20 3a 3a 3d 20 49 4e 53 54 45 41 44  time ::= INSTEAD
303da 20 4f 46 22 2c 0a 20 2f 2a 20 32 37 35 20 2a 2f   OF",. /* 275 */
303db 20 22 74 72 69 67 67 65 72 5f 74 69 6d 65 20 3a   "trigger_time :
303dc 3a 3d 22 2c 0a 20 2f 2a 20 32 37 36 20 2a 2f 20  :=",. /* 276 */ 
303dd 22 74 72 69 67 67 65 72 5f 65 76 65 6e 74 20 3a  "trigger_event :
303de 3a 3d 20 44 45 4c 45 54 45 7c 49 4e 53 45 52 54  := DELETE|INSERT
303df 22 2c 0a 20 2f 2a 20 32 37 37 20 2a 2f 20 22 74  ",. /* 277 */ "t
303e0 72 69 67 67 65 72 5f 65 76 65 6e 74 20 3a 3a 3d  rigger_event ::=
303e1 20 55 50 44 41 54 45 22 2c 0a 20 2f 2a 20 32 37   UPDATE",. /* 27
303e2 38 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 65 76  8 */ "trigger_ev
303e3 65 6e 74 20 3a 3a 3d 20 55 50 44 41 54 45 20 4f  ent ::= UPDATE O
303e4 46 20 69 6e 73 63 6f 6c 6c 69 73 74 22 2c 0a 20  F inscollist",. 
303e5 2f 2a 20 32 37 39 20 2a 2f 20 22 66 6f 72 65 61  /* 279 */ "forea
303e6 63 68 5f 63 6c 61 75 73 65 20 3a 3a 3d 22 2c 0a  ch_clause ::=",.
303e7 20 2f 2a 20 32 38 30 20 2a 2f 20 22 66 6f 72 65   /* 280 */ "fore
303e8 61 63 68 5f 63 6c 61 75 73 65 20 3a 3a 3d 20 46  ach_clause ::= F
303e9 4f 52 20 45 41 43 48 20 52 4f 57 22 2c 0a 20 2f  OR EACH ROW",. /
303ea 2a 20 32 38 31 20 2a 2f 20 22 77 68 65 6e 5f 63  * 281 */ "when_c
303eb 6c 61 75 73 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  lause ::=",. /* 
303ec 32 38 32 20 2a 2f 20 22 77 68 65 6e 5f 63 6c 61  282 */ "when_cla
303ed 75 73 65 20 3a 3a 3d 20 57 48 45 4e 20 65 78 70  use ::= WHEN exp
303ee 72 22 2c 0a 20 2f 2a 20 32 38 33 20 2a 2f 20 22  r",. /* 283 */ "
303ef 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74  trigger_cmd_list
303f0 20 3a 3a 3d 20 74 72 69 67 67 65 72 5f 63 6d 64   ::= trigger_cmd
303f1 5f 6c 69 73 74 20 74 72 69 67 67 65 72 5f 63 6d  _list trigger_cm
303f2 64 20 53 45 4d 49 22 2c 0a 20 2f 2a 20 32 38 34  d SEMI",. /* 284
303f3 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 63 6d 64   */ "trigger_cmd
303f4 5f 6c 69 73 74 20 3a 3a 3d 20 74 72 69 67 67 65  _list ::= trigge
303f5 72 5f 63 6d 64 20 53 45 4d 49 22 2c 0a 20 2f 2a  r_cmd SEMI",. /*
303f6 20 32 38 35 20 2a 2f 20 22 74 72 6e 6d 20 3a 3a   285 */ "trnm ::
303f7 3d 20 6e 6d 22 2c 0a 20 2f 2a 20 32 38 36 20 2a  = nm",. /* 286 *
303f8 2f 20 22 74 72 6e 6d 20 3a 3a 3d 20 6e 6d 20 44  / "trnm ::= nm D
303f9 4f 54 20 6e 6d 22 2c 0a 20 2f 2a 20 32 38 37 20  OT nm",. /* 287 
303fa 2a 2f 20 22 74 72 69 64 78 62 79 20 3a 3a 3d 22  */ "tridxby ::="
303fb 2c 0a 20 2f 2a 20 32 38 38 20 2a 2f 20 22 74 72  ,. /* 288 */ "tr
303fc 69 64 78 62 79 20 3a 3a 3d 20 49 4e 44 45 58 45  idxby ::= INDEXE
303fd 44 20 42 59 20 6e 6d 22 2c 0a 20 2f 2a 20 32 38  D BY nm",. /* 28
303fe 39 20 2a 2f 20 22 74 72 69 64 78 62 79 20 3a 3a  9 */ "tridxby ::
303ff 3d 20 4e 4f 54 20 49 4e 44 45 58 45 44 22 2c 0a  = NOT INDEXED",.
30400 20 2f 2a 20 32 39 30 20 2a 2f 20 22 74 72 69 67   /* 290 */ "trig
30401 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 55 50 44 41  ger_cmd ::= UPDA
30402 54 45 20 6f 72 63 6f 6e 66 20 74 72 6e 6d 20 74  TE orconf trnm t
30403 72 69 64 78 62 79 20 53 45 54 20 73 65 74 6c 69  ridxby SET setli
30404 73 74 20 77 68 65 72 65 5f 6f 70 74 22 2c 0a 20  st where_opt",. 
30405 2f 2a 20 32 39 31 20 2a 2f 20 22 74 72 69 67 67  /* 291 */ "trigg
30406 65 72 5f 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72  er_cmd ::= inser
30407 74 5f 63 6d 64 20 49 4e 54 4f 20 74 72 6e 6d 20  t_cmd INTO trnm 
30408 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 56  inscollist_opt V
30409 41 4c 55 45 53 20 4c 50 20 69 74 65 6d 6c 69 73  ALUES LP itemlis
3040a 74 20 52 50 22 2c 0a 20 2f 2a 20 32 39 32 20 2a  t RP",. /* 292 *
3040b 2f 20 22 74 72 69 67 67 65 72 5f 63 6d 64 20 3a  / "trigger_cmd :
3040c 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e  := insert_cmd IN
3040d 54 4f 20 74 72 6e 6d 20 69 6e 73 63 6f 6c 6c 69  TO trnm inscolli
3040e 73 74 5f 6f 70 74 20 73 65 6c 65 63 74 22 2c 0a  st_opt select",.
3040f 20 2f 2a 20 32 39 33 20 2a 2f 20 22 74 72 69 67   /* 293 */ "trig
30410 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 44 45 4c 45  ger_cmd ::= DELE
30411 54 45 20 46 52 4f 4d 20 74 72 6e 6d 20 74 72 69  TE FROM trnm tri
30412 64 78 62 79 20 77 68 65 72 65 5f 6f 70 74 22 2c  dxby where_opt",
30413 0a 20 2f 2a 20 32 39 34 20 2a 2f 20 22 74 72 69  . /* 294 */ "tri
30414 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 73 65 6c  gger_cmd ::= sel
30415 65 63 74 22 2c 0a 20 2f 2a 20 32 39 35 20 2a 2f  ect",. /* 295 */
30416 20 22 65 78 70 72 20 3a 3a 3d 20 52 41 49 53 45   "expr ::= RAISE
30417 20 4c 50 20 49 47 4e 4f 52 45 20 52 50 22 2c 0a   LP IGNORE RP",.
30418 20 2f 2a 20 32 39 36 20 2a 2f 20 22 65 78 70 72   /* 296 */ "expr
30419 20 3a 3a 3d 20 52 41 49 53 45 20 4c 50 20 72 61   ::= RAISE LP ra
3041a 69 73 65 74 79 70 65 20 43 4f 4d 4d 41 20 6e 6d  isetype COMMA nm
3041b 20 52 50 22 2c 0a 20 2f 2a 20 32 39 37 20 2a 2f   RP",. /* 297 */
3041c 20 22 72 61 69 73 65 74 79 70 65 20 3a 3a 3d 20   "raisetype ::= 
3041d 52 4f 4c 4c 42 41 43 4b 22 2c 0a 20 2f 2a 20 32  ROLLBACK",. /* 2
3041e 39 38 20 2a 2f 20 22 72 61 69 73 65 74 79 70 65  98 */ "raisetype
3041f 20 3a 3a 3d 20 41 42 4f 52 54 22 2c 0a 20 2f 2a   ::= ABORT",. /*
30420 20 32 39 39 20 2a 2f 20 22 72 61 69 73 65 74 79   299 */ "raisety
30421 70 65 20 3a 3a 3d 20 46 41 49 4c 22 2c 0a 20 2f  pe ::= FAIL",. /
30422 2a 20 33 30 30 20 2a 2f 20 22 63 6d 64 20 3a 3a  * 300 */ "cmd ::
30423 3d 20 44 52 4f 50 20 54 52 49 47 47 45 52 20 69  = DROP TRIGGER i
30424 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65  fexists fullname
30425 22 2c 0a 20 2f 2a 20 33 30 31 20 2a 2f 20 22 63  ",. /* 301 */ "c
30426 6d 64 20 3a 3a 3d 20 41 54 54 41 43 48 20 64 61  md ::= ATTACH da
30427 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20 65 78  tabase_kw_opt ex
30428 70 72 20 41 53 20 65 78 70 72 20 6b 65 79 5f 6f  pr AS expr key_o
30429 70 74 22 2c 0a 20 2f 2a 20 33 30 32 20 2a 2f 20  pt",. /* 302 */ 
3042a 22 63 6d 64 20 3a 3a 3d 20 44 45 54 41 43 48 20  "cmd ::= DETACH 
3042b 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20  database_kw_opt 
3042c 65 78 70 72 22 2c 0a 20 2f 2a 20 33 30 33 20 2a  expr",. /* 303 *
3042d 2f 20 22 6b 65 79 5f 6f 70 74 20 3a 3a 3d 22 2c  / "key_opt ::=",
3042e 0a 20 2f 2a 20 33 30 34 20 2a 2f 20 22 6b 65 79  . /* 304 */ "key
3042f 5f 6f 70 74 20 3a 3a 3d 20 4b 45 59 20 65 78 70  _opt ::= KEY exp
30430 72 22 2c 0a 20 2f 2a 20 33 30 35 20 2a 2f 20 22  r",. /* 305 */ "
30431 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20  database_kw_opt 
30432 3a 3a 3d 20 44 41 54 41 42 41 53 45 22 2c 0a 20  ::= DATABASE",. 
30433 2f 2a 20 33 30 36 20 2a 2f 20 22 64 61 74 61 62  /* 306 */ "datab
30434 61 73 65 5f 6b 77 5f 6f 70 74 20 3a 3a 3d 22 2c  ase_kw_opt ::=",
30435 0a 20 2f 2a 20 33 30 37 20 2a 2f 20 22 63 6d 64  . /* 307 */ "cmd
30436 20 3a 3a 3d 20 52 45 49 4e 44 45 58 22 2c 0a 20   ::= REINDEX",. 
30437 2f 2a 20 33 30 38 20 2a 2f 20 22 63 6d 64 20 3a  /* 308 */ "cmd :
30438 3a 3d 20 52 45 49 4e 44 45 58 20 6e 6d 20 64 62  := REINDEX nm db
30439 6e 6d 22 2c 0a 20 2f 2a 20 33 30 39 20 2a 2f 20  nm",. /* 309 */ 
3043a 22 63 6d 64 20 3a 3a 3d 20 41 4e 41 4c 59 5a 45  "cmd ::= ANALYZE
3043b 22 2c 0a 20 2f 2a 20 33 31 30 20 2a 2f 20 22 63  ",. /* 310 */ "c
3043c 6d 64 20 3a 3a 3d 20 41 4e 41 4c 59 5a 45 20 6e  md ::= ANALYZE n
3043d 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a 20 33 31 31  m dbnm",. /* 311
3043e 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 41 4c 54   */ "cmd ::= ALT
3043f 45 52 20 54 41 42 4c 45 20 66 75 6c 6c 6e 61 6d  ER TABLE fullnam
30440 65 20 52 45 4e 41 4d 45 20 54 4f 20 6e 6d 22 2c  e RENAME TO nm",
30441 0a 20 2f 2a 20 33 31 32 20 2a 2f 20 22 63 6d 64  . /* 312 */ "cmd
30442 20 3a 3a 3d 20 41 4c 54 45 52 20 54 41 42 4c 45   ::= ALTER TABLE
30443 20 61 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c   add_column_full
30444 6e 61 6d 65 20 41 44 44 20 6b 77 63 6f 6c 75 6d  name ADD kwcolum
30445 6e 5f 6f 70 74 20 63 6f 6c 75 6d 6e 22 2c 0a 20  n_opt column",. 
30446 2f 2a 20 33 31 33 20 2a 2f 20 22 61 64 64 5f 63  /* 313 */ "add_c
30447 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61 6d 65 20 3a  olumn_fullname :
30448 3a 3d 20 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20 2f  := fullname",. /
30449 2a 20 33 31 34 20 2a 2f 20 22 6b 77 63 6f 6c 75  * 314 */ "kwcolu
3044a 6d 6e 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a  mn_opt ::=",. /*
3044b 20 33 31 35 20 2a 2f 20 22 6b 77 63 6f 6c 75 6d   315 */ "kwcolum
3044c 6e 5f 6f 70 74 20 3a 3a 3d 20 43 4f 4c 55 4d 4e  n_opt ::= COLUMN
3044d 4b 57 22 2c 0a 20 2f 2a 20 33 31 36 20 2a 2f 20  KW",. /* 316 */ 
3044e 22 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 5f  "cmd ::= create_
3044f 76 74 61 62 22 2c 0a 20 2f 2a 20 33 31 37 20 2a  vtab",. /* 317 *
30450 2f 20 22 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74  / "cmd ::= creat
30451 65 5f 76 74 61 62 20 4c 50 20 76 74 61 62 61 72  e_vtab LP vtabar
30452 67 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 33  glist RP",. /* 3
30453 31 38 20 2a 2f 20 22 63 72 65 61 74 65 5f 76 74  18 */ "create_vt
30454 61 62 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20  ab ::= createkw 
30455 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 6e 6d  VIRTUAL TABLE nm
30456 20 64 62 6e 6d 20 55 53 49 4e 47 20 6e 6d 22 2c   dbnm USING nm",
30457 0a 20 2f 2a 20 33 31 39 20 2a 2f 20 22 76 74 61  . /* 319 */ "vta
30458 62 61 72 67 6c 69 73 74 20 3a 3a 3d 20 76 74 61  barglist ::= vta
30459 62 61 72 67 22 2c 0a 20 2f 2a 20 33 32 30 20 2a  barg",. /* 320 *
3045a 2f 20 22 76 74 61 62 61 72 67 6c 69 73 74 20 3a  / "vtabarglist :
3045b 3a 3d 20 76 74 61 62 61 72 67 6c 69 73 74 20 43  := vtabarglist C
3045c 4f 4d 4d 41 20 76 74 61 62 61 72 67 22 2c 0a 20  OMMA vtabarg",. 
3045d 2f 2a 20 33 32 31 20 2a 2f 20 22 76 74 61 62 61  /* 321 */ "vtaba
3045e 72 67 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 33 32 32  rg ::=",. /* 322
3045f 20 2a 2f 20 22 76 74 61 62 61 72 67 20 3a 3a 3d   */ "vtabarg ::=
30460 20 76 74 61 62 61 72 67 20 76 74 61 62 61 72 67   vtabarg vtabarg
30461 74 6f 6b 65 6e 22 2c 0a 20 2f 2a 20 33 32 33 20  token",. /* 323 
30462 2a 2f 20 22 76 74 61 62 61 72 67 74 6f 6b 65 6e  */ "vtabargtoken
30463 20 3a 3a 3d 20 41 4e 59 22 2c 0a 20 2f 2a 20 33   ::= ANY",. /* 3
30464 32 34 20 2a 2f 20 22 76 74 61 62 61 72 67 74 6f  24 */ "vtabargto
30465 6b 65 6e 20 3a 3a 3d 20 6c 70 20 61 6e 79 6c 69  ken ::= lp anyli
30466 73 74 20 52 50 22 2c 0a 20 2f 2a 20 33 32 35 20  st RP",. /* 325 
30467 2a 2f 20 22 6c 70 20 3a 3a 3d 20 4c 50 22 2c 0a  */ "lp ::= LP",.
30468 20 2f 2a 20 33 32 36 20 2a 2f 20 22 61 6e 79 6c   /* 326 */ "anyl
30469 69 73 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 33 32  ist ::=",. /* 32
3046a 37 20 2a 2f 20 22 61 6e 79 6c 69 73 74 20 3a 3a  7 */ "anylist ::
3046b 3d 20 61 6e 79 6c 69 73 74 20 4c 50 20 61 6e 79  = anylist LP any
3046c 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 33 32  list RP",. /* 32
3046d 38 20 2a 2f 20 22 61 6e 79 6c 69 73 74 20 3a 3a  8 */ "anylist ::
3046e 3d 20 61 6e 79 6c 69 73 74 20 41 4e 59 22 2c 0a  = anylist ANY",.
3046f 7d 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45  };.#endif /* NDE
30470 42 55 47 20 2a 2f 0a 0a 0a 23 69 66 20 59 59 53  BUG */...#if YYS
30471 54 41 43 4b 44 45 50 54 48 3c 3d 30 0a 2f 2a 0a  TACKDEPTH<=0./*.
30472 2a 2a 20 54 72 79 20 74 6f 20 69 6e 63 72 65 61  ** Try to increa
30473 73 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  se the size of t
30474 68 65 20 70 61 72 73 65 72 20 73 74 61 63 6b 2e  he parser stack.
30475 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
30476 79 79 47 72 6f 77 53 74 61 63 6b 28 79 79 50 61  yyGrowStack(yyPa
30477 72 73 65 72 20 2a 70 29 7b 0a 20 20 69 6e 74 20  rser *p){.  int 
30478 6e 65 77 53 69 7a 65 3b 0a 20 20 79 79 53 74 61  newSize;.  yySta
30479 63 6b 45 6e 74 72 79 20 2a 70 4e 65 77 3b 0a 0a  ckEntry *pNew;..
3047a 20 20 6e 65 77 53 69 7a 65 20 3d 20 70 2d 3e 79    newSize = p->y
3047b 79 73 74 6b 73 7a 2a 32 20 2b 20 31 30 30 3b 0a  ystksz*2 + 100;.
3047c 20 20 70 4e 65 77 20 3d 20 72 65 61 6c 6c 6f 63    pNew = realloc
3047d 28 70 2d 3e 79 79 73 74 61 63 6b 2c 20 6e 65 77  (p->yystack, new
3047e 53 69 7a 65 2a 73 69 7a 65 6f 66 28 70 4e 65 77  Size*sizeof(pNew
3047f 5b 30 5d 29 29 3b 0a 20 20 69 66 28 20 70 4e 65  [0]));.  if( pNe
30480 77 20 29 7b 0a 20 20 20 20 70 2d 3e 79 79 73 74  w ){.    p->yyst
30481 61 63 6b 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20  ack = pNew;.    
30482 70 2d 3e 79 79 73 74 6b 73 7a 20 3d 20 6e 65 77  p->yystksz = new
30483 53 69 7a 65 3b 0a 23 69 66 6e 64 65 66 20 4e 44  Size;.#ifndef ND
30484 45 42 55 47 0a 20 20 20 20 69 66 28 20 79 79 54  EBUG.    if( yyT
30485 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20  raceFILE ){.    
30486 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63    fprintf(yyTrac
30487 65 46 49 4c 45 2c 22 25 73 53 74 61 63 6b 20 67  eFILE,"%sStack g
30488 72 6f 77 73 20 74 6f 20 25 64 20 65 6e 74 72 69  rows to %d entri
30489 65 73 21 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20  es!\n",.        
3048a 20 20 20 20 20 20 79 79 54 72 61 63 65 50 72 6f        yyTracePro
3048b 6d 70 74 2c 20 70 2d 3e 79 79 73 74 6b 73 7a 29  mpt, p->yystksz)
3048c 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
3048d 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20   }.}.#endif../* 
3048e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
3048f 6e 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65  n allocates a ne
30490 77 20 70 61 72 73 65 72 2e 0a 2a 2a 20 54 68 65  w parser..** The
30491 20 6f 6e 6c 79 20 61 72 67 75 6d 65 6e 74 20 69   only argument i
30492 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
30493 20 66 75 6e 63 74 69 6f 6e 20 77 68 69 63 68 20   function which 
30494 77 6f 72 6b 73 20 6c 69 6b 65 0a 2a 2a 20 6d 61  works like.** ma
30495 6c 6c 6f 63 2e 0a 2a 2a 0a 2a 2a 20 49 6e 70 75  lloc..**.** Inpu
30496 74 73 3a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72  ts:.** A pointer
30497 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   to the function
30498 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74   used to allocat
30499 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20  e memory..**.** 
3049a 4f 75 74 70 75 74 73 3a 0a 2a 2a 20 41 20 70 6f  Outputs:.** A po
3049b 69 6e 74 65 72 20 74 6f 20 61 20 70 61 72 73 65  inter to a parse
3049c 72 2e 20 20 54 68 69 73 20 70 6f 69 6e 74 65 72  r.  This pointer
3049d 20 69 73 20 75 73 65 64 20 69 6e 20 73 75 62 73   is used in subs
3049e 65 71 75 65 6e 74 20 63 61 6c 6c 73 0a 2a 2a 20  equent calls.** 
3049f 74 6f 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  to sqlite3Parser
304a0 20 61 6e 64 20 73 71 6c 69 74 65 33 50 61 72 73   and sqlite3Pars
304a1 65 72 46 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54  erFree..*/.SQLIT
304a2 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
304a3 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 6c 6c  sqlite3ParserAll
304a4 6f 63 28 76 6f 69 64 20 2a 28 2a 6d 61 6c 6c 6f  oc(void *(*mallo
304a5 63 50 72 6f 63 29 28 73 69 7a 65 5f 74 29 29 7b  cProc)(size_t)){
304a6 0a 20 20 79 79 50 61 72 73 65 72 20 2a 70 50 61  .  yyParser *pPa
304a7 72 73 65 72 3b 0a 20 20 70 50 61 72 73 65 72 20  rser;.  pParser 
304a8 3d 20 28 79 79 50 61 72 73 65 72 2a 29 28 2a 6d  = (yyParser*)(*m
304a9 61 6c 6c 6f 63 50 72 6f 63 29 28 20 28 73 69 7a  allocProc)( (siz
304aa 65 5f 74 29 73 69 7a 65 6f 66 28 79 79 50 61 72  e_t)sizeof(yyPar
304ab 73 65 72 29 20 29 3b 0a 20 20 69 66 28 20 70 50  ser) );.  if( pP
304ac 61 72 73 65 72 20 29 7b 0a 20 20 20 20 70 50 61  arser ){.    pPa
304ad 72 73 65 72 2d 3e 79 79 69 64 78 20 3d 20 2d 31  rser->yyidx = -1
304ae 3b 0a 23 69 66 64 65 66 20 59 59 54 52 41 43 4b  ;.#ifdef YYTRACK
304af 4d 41 58 53 54 41 43 4b 44 45 50 54 48 0a 20 20  MAXSTACKDEPTH.  
304b0 20 20 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78    pParser->yyidx
304b1 4d 61 78 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a  Max = 0;.#endif.
304b2 23 69 66 20 59 59 53 54 41 43 4b 44 45 50 54 48  #if YYSTACKDEPTH
304b3 3c 3d 30 0a 20 20 20 20 70 50 61 72 73 65 72 2d  <=0.    pParser-
304b4 3e 79 79 73 74 61 63 6b 20 3d 20 4e 55 4c 4c 3b  >yystack = NULL;
304b5 0a 20 20 20 20 70 50 61 72 73 65 72 2d 3e 79 79  .    pParser->yy
304b6 73 74 6b 73 7a 20 3d 20 30 3b 0a 20 20 20 20 79  stksz = 0;.    y
304b7 79 47 72 6f 77 53 74 61 63 6b 28 70 50 61 72 73  yGrowStack(pPars
304b8 65 72 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a  er);.#endif.  }.
304b9 20 20 72 65 74 75 72 6e 20 70 50 61 72 73 65 72    return pParser
304ba 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c  ;.}../* The foll
304bb 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 64  owing function d
304bc 65 6c 65 74 65 73 20 74 68 65 20 76 61 6c 75 65  eletes the value
304bd 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
304be 20 61 0a 2a 2a 20 73 79 6d 62 6f 6c 2e 20 20 54   a.** symbol.  T
304bf 68 65 20 73 79 6d 62 6f 6c 20 63 61 6e 20 62 65  he symbol can be
304c0 20 65 69 74 68 65 72 20 61 20 74 65 72 6d 69 6e   either a termin
304c1 61 6c 20 6f 72 20 6e 6f 6e 74 65 72 6d 69 6e 61  al or nontermina
304c2 6c 2e 0a 2a 2a 20 22 79 79 6d 61 6a 6f 72 22 20  l..** "yymajor" 
304c3 69 73 20 74 68 65 20 73 79 6d 62 6f 6c 20 63 6f  is the symbol co
304c4 64 65 2c 20 61 6e 64 20 22 79 79 70 6d 69 6e 6f  de, and "yypmino
304c5 72 22 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  r" is a pointer 
304c6 74 6f 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 2e  to.** the value.
304c7 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
304c8 79 79 5f 64 65 73 74 72 75 63 74 6f 72 28 0a 20  yy_destructor(. 
304c9 20 79 79 50 61 72 73 65 72 20 2a 79 79 70 50 61   yyParser *yypPa
304ca 72 73 65 72 2c 20 20 20 20 2f 2a 20 54 68 65 20  rser,    /* The 
304cb 70 61 72 73 65 72 20 2a 2f 0a 20 20 59 59 43 4f  parser */.  YYCO
304cc 44 45 54 59 50 45 20 79 79 6d 61 6a 6f 72 2c 20  DETYPE yymajor, 
304cd 20 20 20 20 2f 2a 20 54 79 70 65 20 63 6f 64 65      /* Type code
304ce 20 66 6f 72 20 6f 62 6a 65 63 74 20 74 6f 20 64   for object to d
304cf 65 73 74 72 6f 79 20 2a 2f 0a 20 20 59 59 4d 49  estroy */.  YYMI
304d0 4e 4f 52 54 59 50 45 20 2a 79 79 70 6d 69 6e 6f  NORTYPE *yypmino
304d1 72 20 20 20 2f 2a 20 54 68 65 20 6f 62 6a 65 63  r   /* The objec
304d2 74 20 74 6f 20 62 65 20 64 65 73 74 72 6f 79 65  t to be destroye
304d3 64 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65  d */.){.  sqlite
304d4 33 50 61 72 73 65 72 41 52 47 5f 46 45 54 43 48  3ParserARG_FETCH
304d5 3b 0a 20 20 73 77 69 74 63 68 28 20 79 79 6d 61  ;.  switch( yyma
304d6 6a 6f 72 20 29 7b 0a 20 20 20 20 2f 2a 20 48 65  jor ){.    /* He
304d7 72 65 20 69 73 20 69 6e 73 65 72 74 65 64 20 74  re is inserted t
304d8 68 65 20 61 63 74 69 6f 6e 73 20 77 68 69 63 68  he actions which
304d9 20 74 61 6b 65 20 70 6c 61 63 65 20 77 68 65 6e   take place when
304da 20 61 0a 20 20 20 20 2a 2a 20 74 65 72 6d 69 6e   a.    ** termin
304db 61 6c 20 6f 72 20 6e 6f 6e 2d 74 65 72 6d 69 6e  al or non-termin
304dc 61 6c 20 69 73 20 64 65 73 74 72 6f 79 65 64 2e  al is destroyed.
304dd 20 20 54 68 69 73 20 63 61 6e 20 68 61 70 70 65    This can happe
304de 6e 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20 74 68  n.    ** when th
304df 65 20 73 79 6d 62 6f 6c 20 69 73 20 70 6f 70 70  e symbol is popp
304e0 65 64 20 66 72 6f 6d 20 74 68 65 20 73 74 61 63  ed from the stac
304e1 6b 20 64 75 72 69 6e 67 20 61 0a 20 20 20 20 2a  k during a.    *
304e2 2a 20 72 65 64 75 63 65 20 6f 72 20 64 75 72 69  * reduce or duri
304e3 6e 67 20 65 72 72 6f 72 20 70 72 6f 63 65 73 73  ng error process
304e4 69 6e 67 20 6f 72 20 77 68 65 6e 20 61 20 70 61  ing or when a pa
304e5 72 73 65 72 20 69 73 20 0a 20 20 20 20 2a 2a 20  rser is .    ** 
304e6 62 65 69 6e 67 20 64 65 73 74 72 6f 79 65 64 20  being destroyed 
304e7 62 65 66 6f 72 65 20 69 74 20 69 73 20 66 69 6e  before it is fin
304e8 69 73 68 65 64 20 70 61 72 73 69 6e 67 2e 0a 20  ished parsing.. 
304e9 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f 74     **.    ** Not
304ea 65 3a 20 64 75 72 69 6e 67 20 61 20 72 65 64 75  e: during a redu
304eb 63 65 2c 20 74 68 65 20 6f 6e 6c 79 20 73 79 6d  ce, the only sym
304ec 62 6f 6c 73 20 64 65 73 74 72 6f 79 65 64 20 61  bols destroyed a
304ed 72 65 20 74 68 6f 73 65 0a 20 20 20 20 2a 2a 20  re those.    ** 
304ee 77 68 69 63 68 20 61 70 70 65 61 72 20 6f 6e 20  which appear on 
304ef 74 68 65 20 52 48 53 20 6f 66 20 74 68 65 20 72  the RHS of the r
304f0 75 6c 65 2c 20 62 75 74 20 77 68 69 63 68 20 61  ule, but which a
304f1 72 65 20 6e 6f 74 20 75 73 65 64 0a 20 20 20 20  re not used.    
304f2 2a 2a 20 69 6e 73 69 64 65 20 74 68 65 20 43 20  ** inside the C 
304f3 63 6f 64 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  code..    */.   
304f4 20 63 61 73 65 20 31 36 30 3a 20 2f 2a 20 73 65   case 160: /* se
304f5 6c 65 63 74 20 2a 2f 0a 20 20 20 20 63 61 73 65  lect */.    case
304f6 20 31 39 34 3a 20 2f 2a 20 6f 6e 65 73 65 6c 65   194: /* onesele
304f7 63 74 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 53  ct */.{.sqlite3S
304f8 65 6c 65 63 74 44 65 6c 65 74 65 28 70 50 61 72  electDelete(pPar
304f9 73 65 2d 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f  se->db, (yypmino
304fa 72 2d 3e 79 79 33 29 29 3b 0a 7d 0a 20 20 20 20  r->yy3));.}.    
304fb 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
304fc 65 20 31 37 34 3a 20 2f 2a 20 74 65 72 6d 20 2a  e 174: /* term *
304fd 2f 0a 20 20 20 20 63 61 73 65 20 31 37 35 3a 20  /.    case 175: 
304fe 2f 2a 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 63  /* expr */.    c
304ff 61 73 65 20 32 32 33 3a 20 2f 2a 20 65 73 63 61  ase 223: /* esca
30500 70 65 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 45  pe */.{.sqlite3E
30501 78 70 72 44 65 6c 65 74 65 28 70 50 61 72 73 65  xprDelete(pParse
30502 2d 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d  ->db, (yypminor-
30503 3e 79 79 33 34 36 29 2e 70 45 78 70 72 29 3b 0a  >yy346).pExpr);.
30504 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
30505 20 20 20 63 61 73 65 20 31 37 39 3a 20 2f 2a 20     case 179: /* 
30506 69 64 78 6c 69 73 74 5f 6f 70 74 20 2a 2f 0a 20  idxlist_opt */. 
30507 20 20 20 63 61 73 65 20 31 38 37 3a 20 2f 2a 20     case 187: /* 
30508 69 64 78 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63  idxlist */.    c
30509 61 73 65 20 31 39 37 3a 20 2f 2a 20 73 65 6c 63  ase 197: /* selc
3050a 6f 6c 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63 61  ollist */.    ca
3050b 73 65 20 32 30 30 3a 20 2f 2a 20 67 72 6f 75 70  se 200: /* group
3050c 62 79 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 63 61  by_opt */.    ca
3050d 73 65 20 32 30 32 3a 20 2f 2a 20 6f 72 64 65 72  se 202: /* order
3050e 62 79 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 63 61  by_opt */.    ca
3050f 73 65 20 32 30 34 3a 20 2f 2a 20 73 63 6c 70 20  se 204: /* sclp 
30510 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 31 34 3a  */.    case 214:
30511 20 2f 2a 20 73 6f 72 74 6c 69 73 74 20 2a 2f 0a   /* sortlist */.
30512 20 20 20 20 63 61 73 65 20 32 31 36 3a 20 2f 2a      case 216: /*
30513 20 6e 65 78 70 72 6c 69 73 74 20 2a 2f 0a 20 20   nexprlist */.  
30514 20 20 63 61 73 65 20 32 31 37 3a 20 2f 2a 20 73    case 217: /* s
30515 65 74 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63 61  etlist */.    ca
30516 73 65 20 32 32 30 3a 20 2f 2a 20 69 74 65 6d 6c  se 220: /* iteml
30517 69 73 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  ist */.    case 
30518 32 32 31 3a 20 2f 2a 20 65 78 70 72 6c 69 73 74  221: /* exprlist
30519 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 32 37   */.    case 227
3051a 3a 20 2f 2a 20 63 61 73 65 5f 65 78 70 72 6c 69  : /* case_exprli
3051b 73 74 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 45  st */.{.sqlite3E
3051c 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50  xprListDelete(pP
3051d 61 72 73 65 2d 3e 64 62 2c 20 28 79 79 70 6d 69  arse->db, (yypmi
3051e 6e 6f 72 2d 3e 79 79 31 34 29 29 3b 0a 7d 0a 20  nor->yy14));.}. 
3051f 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
30520 63 61 73 65 20 31 39 33 3a 20 2f 2a 20 66 75 6c  case 193: /* ful
30521 6c 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 63 61 73  lname */.    cas
30522 65 20 31 39 38 3a 20 2f 2a 20 66 72 6f 6d 20 2a  e 198: /* from *
30523 2f 0a 20 20 20 20 63 61 73 65 20 32 30 36 3a 20  /.    case 206: 
30524 2f 2a 20 73 65 6c 74 61 62 6c 69 73 74 20 2a 2f  /* seltablist */
30525 0a 20 20 20 20 63 61 73 65 20 32 30 37 3a 20 2f  .    case 207: /
30526 2a 20 73 74 6c 5f 70 72 65 66 69 78 20 2a 2f 0a  * stl_prefix */.
30527 7b 0a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74  {.sqlite3SrcList
30528 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64  Delete(pParse->d
30529 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79  b, (yypminor->yy
3052a 36 35 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72  65));.}.      br
3052b 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 31 39  eak;.    case 19
3052c 39 3a 20 2f 2a 20 77 68 65 72 65 5f 6f 70 74 20  9: /* where_opt 
3052d 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 30 31 3a  */.    case 201:
3052e 20 2f 2a 20 68 61 76 69 6e 67 5f 6f 70 74 20 2a   /* having_opt *
3052f 2f 0a 20 20 20 20 63 61 73 65 20 32 31 30 3a 20  /.    case 210: 
30530 2f 2a 20 6f 6e 5f 6f 70 74 20 2a 2f 0a 20 20 20  /* on_opt */.   
30531 20 63 61 73 65 20 32 31 35 3a 20 2f 2a 20 73 6f   case 215: /* so
30532 72 74 69 74 65 6d 20 2a 2f 0a 20 20 20 20 63 61  rtitem */.    ca
30533 73 65 20 32 32 36 3a 20 2f 2a 20 63 61 73 65 5f  se 226: /* case_
30534 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20 63  operand */.    c
30535 61 73 65 20 32 32 38 3a 20 2f 2a 20 63 61 73 65  ase 228: /* case
30536 5f 65 6c 73 65 20 2a 2f 0a 20 20 20 20 63 61 73  _else */.    cas
30537 65 20 32 33 39 3a 20 2f 2a 20 77 68 65 6e 5f 63  e 239: /* when_c
30538 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 63 61 73  lause */.    cas
30539 65 20 32 34 34 3a 20 2f 2a 20 6b 65 79 5f 6f 70  e 244: /* key_op
3053a 74 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 45 78  t */.{.sqlite3Ex
3053b 70 72 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d  prDelete(pParse-
3053c 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e  >db, (yypminor->
3053d 79 79 31 33 32 29 29 3b 0a 7d 0a 20 20 20 20 20  yy132));.}.     
3053e 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
3053f 20 32 31 31 3a 20 2f 2a 20 75 73 69 6e 67 5f 6f   211: /* using_o
30540 70 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32  pt */.    case 2
30541 31 33 3a 20 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73  13: /* inscollis
30542 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 31  t */.    case 21
30543 39 3a 20 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73 74  9: /* inscollist
30544 5f 6f 70 74 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65  _opt */.{.sqlite
30545 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 70 50  3IdListDelete(pP
30546 61 72 73 65 2d 3e 64 62 2c 20 28 79 79 70 6d 69  arse->db, (yypmi
30547 6e 6f 72 2d 3e 79 79 34 30 38 29 29 3b 0a 7d 0a  nor->yy408));.}.
30548 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
30549 20 63 61 73 65 20 32 33 35 3a 20 2f 2a 20 74 72   case 235: /* tr
3054a 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20 2a  igger_cmd_list *
3054b 2f 0a 20 20 20 20 63 61 73 65 20 32 34 30 3a 20  /.    case 240: 
3054c 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d 64 20 2a  /* trigger_cmd *
3054d 2f 0a 7b 0a 73 71 6c 69 74 65 33 44 65 6c 65 74  /.{.sqlite3Delet
3054e 65 54 72 69 67 67 65 72 53 74 65 70 28 70 50 61  eTriggerStep(pPa
3054f 72 73 65 2d 3e 64 62 2c 20 28 79 79 70 6d 69 6e  rse->db, (yypmin
30550 6f 72 2d 3e 79 79 34 37 33 29 29 3b 0a 7d 0a 20  or->yy473));.}. 
30551 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
30552 63 61 73 65 20 32 33 37 3a 20 2f 2a 20 74 72 69  case 237: /* tri
30553 67 67 65 72 5f 65 76 65 6e 74 20 2a 2f 0a 7b 0a  gger_event */.{.
30554 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c  sqlite3IdListDel
30555 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ete(pParse->db, 
30556 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 33 37 38  (yypminor->yy378
30557 29 2e 62 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72  ).b);.}.      br
30558 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74  eak;.    default
30559 3a 20 20 62 72 65 61 6b 3b 20 20 20 2f 2a 20 49  :  break;   /* I
3055a 66 20 6e 6f 20 64 65 73 74 72 75 63 74 6f 72 20  f no destructor 
3055b 61 63 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64  action specified
3055c 3a 20 64 6f 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  : do nothing */.
3055d 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70    }.}../*.** Pop
3055e 20 74 68 65 20 70 61 72 73 65 72 27 73 20 73 74   the parser's st
3055f 61 63 6b 20 6f 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20  ack once..**.** 
30560 49 66 20 74 68 65 72 65 20 69 73 20 61 20 64 65  If there is a de
30561 73 74 72 75 63 74 6f 72 20 72 6f 75 74 69 6e 65  structor routine
30562 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
30563 20 74 68 65 20 74 6f 6b 65 6e 20 77 68 69 63 68   the token which
30564 0a 2a 2a 20 69 73 20 70 6f 70 70 65 64 20 66 72  .** is popped fr
30565 6f 6d 20 74 68 65 20 73 74 61 63 6b 2c 20 74 68  om the stack, th
30566 65 6e 20 63 61 6c 6c 20 69 74 2e 0a 2a 2a 0a 2a  en call it..**.*
30567 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6d 61 6a  * Return the maj
30568 6f 72 20 74 6f 6b 65 6e 20 6e 75 6d 62 65 72 20  or token number 
30569 66 6f 72 20 74 68 65 20 73 79 6d 62 6f 6c 20 70  for the symbol p
3056a 6f 70 70 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  opped..*/.static
3056b 20 69 6e 74 20 79 79 5f 70 6f 70 5f 70 61 72 73   int yy_pop_pars
3056c 65 72 5f 73 74 61 63 6b 28 79 79 50 61 72 73 65  er_stack(yyParse
3056d 72 20 2a 70 50 61 72 73 65 72 29 7b 0a 20 20 59  r *pParser){.  Y
3056e 59 43 4f 44 45 54 59 50 45 20 79 79 6d 61 6a 6f  YCODETYPE yymajo
3056f 72 3b 0a 20 20 79 79 53 74 61 63 6b 45 6e 74 72  r;.  yyStackEntr
30570 79 20 2a 79 79 74 6f 73 20 3d 20 26 70 50 61 72  y *yytos = &pPar
30571 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 70 50 61  ser->yystack[pPa
30572 72 73 65 72 2d 3e 79 79 69 64 78 5d 3b 0a 0a 20  rser->yyidx];.. 
30573 20 2f 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20   /* There is no 
30574 6d 65 63 68 61 6e 69 73 6d 20 62 79 20 77 68 69  mechanism by whi
30575 63 68 20 74 68 65 20 70 61 72 73 65 72 20 73 74  ch the parser st
30576 61 63 6b 20 63 61 6e 20 62 65 20 70 6f 70 70 65  ack can be poppe
30577 64 20 62 65 6c 6f 77 0a 20 20 2a 2a 20 65 6d 70  d below.  ** emp
30578 74 79 20 69 6e 20 53 51 4c 69 74 65 2e 20 20 2a  ty in SQLite.  *
30579 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 50  /.  if( NEVER(pP
3057a 61 72 73 65 72 2d 3e 79 79 69 64 78 3c 30 29 20  arser->yyidx<0) 
3057b 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 69 66 6e  ) return 0;.#ifn
3057c 64 65 66 20 4e 44 45 42 55 47 0a 20 20 69 66 28  def NDEBUG.  if(
3057d 20 79 79 54 72 61 63 65 46 49 4c 45 20 26 26 20   yyTraceFILE && 
3057e 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d  pParser->yyidx>=
3057f 30 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66  0 ){.    fprintf
30580 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22 25 73  (yyTraceFILE,"%s
30581 50 6f 70 70 69 6e 67 20 25 73 5c 6e 22 2c 0a 20  Popping %s\n",. 
30582 20 20 20 20 20 79 79 54 72 61 63 65 50 72 6f 6d       yyTraceProm
30583 70 74 2c 0a 20 20 20 20 20 20 79 79 54 6f 6b 65  pt,.      yyToke
30584 6e 4e 61 6d 65 5b 79 79 74 6f 73 2d 3e 6d 61 6a  nName[yytos->maj
30585 6f 72 5d 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  or]);.  }.#endif
30586 0a 20 20 79 79 6d 61 6a 6f 72 20 3d 20 79 79 74  .  yymajor = yyt
30587 6f 73 2d 3e 6d 61 6a 6f 72 3b 0a 20 20 79 79 5f  os->major;.  yy_
30588 64 65 73 74 72 75 63 74 6f 72 28 70 50 61 72 73  destructor(pPars
30589 65 72 2c 20 79 79 6d 61 6a 6f 72 2c 20 26 79 79  er, yymajor, &yy
3058a 74 6f 73 2d 3e 6d 69 6e 6f 72 29 3b 0a 20 20 70  tos->minor);.  p
3058b 50 61 72 73 65 72 2d 3e 79 79 69 64 78 2d 2d 3b  Parser->yyidx--;
3058c 0a 20 20 72 65 74 75 72 6e 20 79 79 6d 61 6a 6f  .  return yymajo
3058d 72 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 44 65 61  r;.}../* .** Dea
3058e 6c 6c 6f 63 61 74 65 20 61 6e 64 20 64 65 73 74  llocate and dest
3058f 72 6f 79 20 61 20 70 61 72 73 65 72 2e 20 20 44  roy a parser.  D
30590 65 73 74 72 75 63 74 6f 72 73 20 61 72 65 20 61  estructors are a
30591 6c 6c 20 63 61 6c 6c 65 64 20 66 6f 72 0a 2a 2a  ll called for.**
30592 20 61 6c 6c 20 73 74 61 63 6b 20 65 6c 65 6d 65   all stack eleme
30593 6e 74 73 20 62 65 66 6f 72 65 20 73 68 75 74 74  nts before shutt
30594 69 6e 67 20 74 68 65 20 70 61 72 73 65 72 20 64  ing the parser d
30595 6f 77 6e 2e 0a 2a 2a 0a 2a 2a 20 49 6e 70 75 74  own..**.** Input
30596 73 3a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c  s:.** <ul>.** <l
30597 69 3e 20 20 41 20 70 6f 69 6e 74 65 72 20 74 6f  i>  A pointer to
30598 20 74 68 65 20 70 61 72 73 65 72 2e 20 20 54 68   the parser.  Th
30599 69 73 20 73 68 6f 75 6c 64 20 62 65 20 61 20 70  is should be a p
3059a 6f 69 6e 74 65 72 0a 2a 2a 20 20 20 20 20 20 20  ointer.**       
3059b 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71  obtained from sq
3059c 6c 69 74 65 33 50 61 72 73 65 72 41 6c 6c 6f 63  lite3ParserAlloc
3059d 2e 0a 2a 2a 20 3c 6c 69 3e 20 20 41 20 70 6f 69  ..** <li>  A poi
3059e 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69  nter to a functi
3059f 6f 6e 20 75 73 65 64 20 74 6f 20 72 65 63 6c 61  on used to recla
305a0 69 6d 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e  im memory obtain
305a1 65 64 0a 2a 2a 20 20 20 20 20 20 20 66 72 6f 6d  ed.**       from
305a2 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a 20 3c 2f 75 6c   malloc..** </ul
305a3 3e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  >.*/.SQLITE_PRIV
305a4 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
305a5 50 61 72 73 65 72 46 72 65 65 28 0a 20 20 76 6f  ParserFree(.  vo
305a6 69 64 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  id *p,          
305a7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
305a8 20 70 61 72 73 65 72 20 74 6f 20 62 65 20 64 65   parser to be de
305a9 6c 65 74 65 64 20 2a 2f 0a 20 20 76 6f 69 64 20  leted */.  void 
305aa 28 2a 66 72 65 65 50 72 6f 63 29 28 76 6f 69 64  (*freeProc)(void
305ab 2a 29 20 20 20 20 20 2f 2a 20 46 75 6e 63 74 69  *)     /* Functi
305ac 6f 6e 20 75 73 65 64 20 74 6f 20 72 65 63 6c 61  on used to recla
305ad 69 6d 20 6d 65 6d 6f 72 79 20 2a 2f 0a 29 7b 0a  im memory */.){.
305ae 20 20 79 79 50 61 72 73 65 72 20 2a 70 50 61 72    yyParser *pPar
305af 73 65 72 20 3d 20 28 79 79 50 61 72 73 65 72 2a  ser = (yyParser*
305b0 29 70 3b 0a 20 20 2f 2a 20 49 6e 20 53 51 4c 69  )p;.  /* In SQLi
305b1 74 65 2c 20 77 65 20 6e 65 76 65 72 20 74 72 79  te, we never try
305b2 20 74 6f 20 64 65 73 74 72 6f 79 20 61 20 70 61   to destroy a pa
305b3 72 73 65 72 20 74 68 61 74 20 77 61 73 20 6e 6f  rser that was no
305b4 74 20 73 75 63 63 65 73 73 66 75 6c 6c 79 0a 20  t successfully. 
305b5 20 2a 2a 20 63 72 65 61 74 65 64 20 69 6e 20 74   ** created in t
305b6 68 65 20 66 69 72 73 74 20 70 6c 61 63 65 2e 20  he first place. 
305b7 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70  */.  if( NEVER(p
305b8 50 61 72 73 65 72 3d 3d 30 29 20 29 20 72 65 74  Parser==0) ) ret
305b9 75 72 6e 3b 0a 20 20 77 68 69 6c 65 28 20 70 50  urn;.  while( pP
305ba 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d 30 20  arser->yyidx>=0 
305bb 29 20 79 79 5f 70 6f 70 5f 70 61 72 73 65 72 5f  ) yy_pop_parser_
305bc 73 74 61 63 6b 28 70 50 61 72 73 65 72 29 3b 0a  stack(pParser);.
305bd 23 69 66 20 59 59 53 54 41 43 4b 44 45 50 54 48  #if YYSTACKDEPTH
305be 3c 3d 30 0a 20 20 66 72 65 65 28 70 50 61 72 73  <=0.  free(pPars
305bf 65 72 2d 3e 79 79 73 74 61 63 6b 29 3b 0a 23 65  er->yystack);.#e
305c0 6e 64 69 66 0a 20 20 28 2a 66 72 65 65 50 72 6f  ndif.  (*freePro
305c1 63 29 28 28 76 6f 69 64 2a 29 70 50 61 72 73 65  c)((void*)pParse
305c2 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  r);.}../*.** Ret
305c3 75 72 6e 20 74 68 65 20 70 65 61 6b 20 64 65 70  urn the peak dep
305c4 74 68 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20  th of the stack 
305c5 66 6f 72 20 61 20 70 61 72 73 65 72 2e 0a 2a 2f  for a parser..*/
305c6 0a 23 69 66 64 65 66 20 59 59 54 52 41 43 4b 4d  .#ifdef YYTRACKM
305c7 41 58 53 54 41 43 4b 44 45 50 54 48 0a 53 51 4c  AXSTACKDEPTH.SQL
305c8 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
305c9 73 71 6c 69 74 65 33 50 61 72 73 65 72 53 74 61  sqlite3ParserSta
305ca 63 6b 50 65 61 6b 28 76 6f 69 64 20 2a 70 29 7b  ckPeak(void *p){
305cb 0a 20 20 79 79 50 61 72 73 65 72 20 2a 70 50 61  .  yyParser *pPa
305cc 72 73 65 72 20 3d 20 28 79 79 50 61 72 73 65 72  rser = (yyParser
305cd 2a 29 70 3b 0a 20 20 72 65 74 75 72 6e 20 70 50  *)p;.  return pP
305ce 61 72 73 65 72 2d 3e 79 79 69 64 78 4d 61 78 3b  arser->yyidxMax;
305cf 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
305d0 20 46 69 6e 64 20 74 68 65 20 61 70 70 72 6f 70   Find the approp
305d1 72 69 61 74 65 20 61 63 74 69 6f 6e 20 66 6f 72  riate action for
305d2 20 61 20 70 61 72 73 65 72 20 67 69 76 65 6e 20   a parser given 
305d3 74 68 65 20 74 65 72 6d 69 6e 61 6c 0a 2a 2a 20  the terminal.** 
305d4 6c 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65 6e  look-ahead token
305d5 20 69 4c 6f 6f 6b 41 68 65 61 64 2e 0a 2a 2a 0a   iLookAhead..**.
305d6 2a 2a 20 49 66 20 74 68 65 20 6c 6f 6f 6b 2d 61  ** If the look-a
305d7 68 65 61 64 20 74 6f 6b 65 6e 20 69 73 20 59 59  head token is YY
305d8 4e 4f 43 4f 44 45 2c 20 74 68 65 6e 20 63 68 65  NOCODE, then che
305d9 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65  ck to see if the
305da 20 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 69 6e   action is.** in
305db 64 65 70 65 6e 64 65 6e 74 20 6f 66 20 74 68 65  dependent of the
305dc 20 6c 6f 6f 6b 2d 61 68 65 61 64 2e 20 20 49 66   look-ahead.  If
305dd 20 69 74 20 69 73 2c 20 72 65 74 75 72 6e 20 74   it is, return t
305de 68 65 20 61 63 74 69 6f 6e 2c 20 6f 74 68 65 72  he action, other
305df 77 69 73 65 0a 2a 2a 20 72 65 74 75 72 6e 20 59  wise.** return Y
305e0 59 5f 4e 4f 5f 41 43 54 49 4f 4e 2e 0a 2a 2f 0a  Y_NO_ACTION..*/.
305e1 73 74 61 74 69 63 20 69 6e 74 20 79 79 5f 66 69  static int yy_fi
305e2 6e 64 5f 73 68 69 66 74 5f 61 63 74 69 6f 6e 28  nd_shift_action(
305e3 0a 20 20 79 79 50 61 72 73 65 72 20 2a 70 50 61  .  yyParser *pPa
305e4 72 73 65 72 2c 20 20 20 20 20 20 20 20 2f 2a 20  rser,        /* 
305e5 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 20 20  The parser */.  
305e6 59 59 43 4f 44 45 54 59 50 45 20 69 4c 6f 6f 6b  YYCODETYPE iLook
305e7 41 68 65 61 64 20 20 20 20 20 2f 2a 20 54 68 65  Ahead     /* The
305e8 20 6c 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65   look-ahead toke
305e9 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b  n */.){.  int i;
305ea 0a 20 20 69 6e 74 20 73 74 61 74 65 6e 6f 20 3d  .  int stateno =
305eb 20 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63   pParser->yystac
305ec 6b 5b 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  k[pParser->yyidx
305ed 5d 2e 73 74 61 74 65 6e 6f 3b 0a 20 0a 20 20 69  ].stateno;. .  i
305ee 66 28 20 73 74 61 74 65 6e 6f 3e 59 59 5f 53 48  f( stateno>YY_SH
305ef 49 46 54 5f 4d 41 58 20 7c 7c 20 28 69 20 3d 20  IFT_MAX || (i = 
305f0 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b 73 74  yy_shift_ofst[st
305f1 61 74 65 6e 6f 5d 29 3d 3d 59 59 5f 53 48 49 46  ateno])==YY_SHIF
305f2 54 5f 55 53 45 5f 44 46 4c 54 20 29 7b 0a 20 20  T_USE_DFLT ){.  
305f3 20 20 72 65 74 75 72 6e 20 79 79 5f 64 65 66 61    return yy_defa
305f4 75 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20 20  ult[stateno];.  
305f5 7d 0a 20 20 61 73 73 65 72 74 28 20 69 4c 6f 6f  }.  assert( iLoo
305f6 6b 41 68 65 61 64 21 3d 59 59 4e 4f 43 4f 44 45  kAhead!=YYNOCODE
305f7 20 29 3b 0a 20 20 69 20 2b 3d 20 69 4c 6f 6f 6b   );.  i += iLook
305f8 41 68 65 61 64 3b 0a 20 20 69 66 28 20 69 3c 30  Ahead;.  if( i<0
305f9 20 7c 7c 20 69 3e 3d 59 59 5f 53 5a 5f 41 43 54   || i>=YY_SZ_ACT
305fa 54 41 42 20 7c 7c 20 79 79 5f 6c 6f 6f 6b 61 68  TAB || yy_lookah
305fb 65 61 64 5b 69 5d 21 3d 69 4c 6f 6f 6b 41 68 65  ead[i]!=iLookAhe
305fc 61 64 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65  ad ){.    /* The
305fd 20 75 73 65 72 20 6f 66 20 22 3b 22 20 69 6e 73   user of ";" ins
305fe 74 65 61 64 20 6f 66 20 22 5c 30 30 30 22 20 61  tead of "\000" a
305ff 73 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 65  s a statement te
30600 72 6d 69 6e 61 74 6f 72 20 69 6e 20 53 51 4c 69  rminator in SQLi
30601 74 65 0a 20 20 20 20 2a 2a 20 6d 65 61 6e 73 20  te.    ** means 
30602 74 68 61 74 20 77 65 20 61 6c 77 61 79 73 20 68  that we always h
30603 61 76 65 20 61 20 6c 6f 6f 6b 2d 61 68 65 61 64  ave a look-ahead
30604 20 74 6f 6b 65 6e 2e 20 2a 2f 0a 20 20 20 20 69   token. */.    i
30605 66 28 20 69 4c 6f 6f 6b 41 68 65 61 64 3e 30 20  f( iLookAhead>0 
30606 29 7b 0a 23 69 66 64 65 66 20 59 59 46 41 4c 4c  ){.#ifdef YYFALL
30607 42 41 43 4b 0a 20 20 20 20 20 20 59 59 43 4f 44  BACK.      YYCOD
30608 45 54 59 50 45 20 69 46 61 6c 6c 62 61 63 6b 3b  ETYPE iFallback;
30609 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
3060a 61 6c 6c 62 61 63 6b 20 74 6f 6b 65 6e 20 2a 2f  allback token */
3060b 0a 20 20 20 20 20 20 69 66 28 20 69 4c 6f 6f 6b  .      if( iLook
3060c 41 68 65 61 64 3c 73 69 7a 65 6f 66 28 79 79 46  Ahead<sizeof(yyF
3060d 61 6c 6c 62 61 63 6b 29 2f 73 69 7a 65 6f 66 28  allback)/sizeof(
3060e 79 79 46 61 6c 6c 62 61 63 6b 5b 30 5d 29 0a 20  yyFallback[0]). 
3060f 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28              && (
30610 69 46 61 6c 6c 62 61 63 6b 20 3d 20 79 79 46 61  iFallback = yyFa
30611 6c 6c 62 61 63 6b 5b 69 4c 6f 6f 6b 41 68 65 61  llback[iLookAhea
30612 64 5d 29 21 3d 30 20 29 7b 0a 23 69 66 6e 64 65  d])!=0 ){.#ifnde
30613 66 20 4e 44 45 42 55 47 0a 20 20 20 20 20 20 20  f NDEBUG.       
30614 20 69 66 28 20 79 79 54 72 61 63 65 46 49 4c 45   if( yyTraceFILE
30615 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 70   ){.          fp
30616 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c  rintf(yyTraceFIL
30617 45 2c 20 22 25 73 46 41 4c 4c 42 41 43 4b 20 25  E, "%sFALLBACK %
30618 73 20 3d 3e 20 25 73 5c 6e 22 2c 0a 20 20 20 20  s => %s\n",.    
30619 20 20 20 20 20 20 20 20 20 79 79 54 72 61 63 65           yyTrace
3061a 50 72 6f 6d 70 74 2c 20 79 79 54 6f 6b 65 6e 4e  Prompt, yyTokenN
3061b 61 6d 65 5b 69 4c 6f 6f 6b 41 68 65 61 64 5d 2c  ame[iLookAhead],
3061c 20 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 69 46 61   yyTokenName[iFa
3061d 6c 6c 62 61 63 6b 5d 29 3b 0a 20 20 20 20 20 20  llback]);.      
3061e 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20    }.#endif.     
3061f 20 20 20 72 65 74 75 72 6e 20 79 79 5f 66 69 6e     return yy_fin
30620 64 5f 73 68 69 66 74 5f 61 63 74 69 6f 6e 28 70  d_shift_action(p
30621 50 61 72 73 65 72 2c 20 69 46 61 6c 6c 62 61 63  Parser, iFallbac
30622 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64  k);.      }.#end
30623 69 66 0a 23 69 66 64 65 66 20 59 59 57 49 4c 44  if.#ifdef YYWILD
30624 43 41 52 44 0a 20 20 20 20 20 20 7b 0a 20 20 20  CARD.      {.   
30625 20 20 20 20 20 69 6e 74 20 6a 20 3d 20 69 20 2d       int j = i -
30626 20 69 4c 6f 6f 6b 41 68 65 61 64 20 2b 20 59 59   iLookAhead + YY
30627 57 49 4c 44 43 41 52 44 3b 0a 20 20 20 20 20 20  WILDCARD;.      
30628 20 20 69 66 28 20 6a 3e 3d 30 20 26 26 20 6a 3c    if( j>=0 && j<
30629 59 59 5f 53 5a 5f 41 43 54 54 41 42 20 26 26 20  YY_SZ_ACTTAB && 
3062a 79 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b 6a 5d 3d  yy_lookahead[j]=
3062b 3d 59 59 57 49 4c 44 43 41 52 44 20 29 7b 0a 23  =YYWILDCARD ){.#
3062c 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20  ifndef NDEBUG.  
3062d 20 20 20 20 20 20 20 20 69 66 28 20 79 79 54 72          if( yyTr
3062e 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 20  aceFILE ){.     
3062f 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 79         fprintf(y
30630 79 54 72 61 63 65 46 49 4c 45 2c 20 22 25 73 57  yTraceFILE, "%sW
30631 49 4c 44 43 41 52 44 20 25 73 20 3d 3e 20 25 73  ILDCARD %s => %s
30632 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \n",.           
30633 20 20 20 20 79 79 54 72 61 63 65 50 72 6f 6d 70      yyTracePromp
30634 74 2c 20 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 69  t, yyTokenName[i
30635 4c 6f 6f 6b 41 68 65 61 64 5d 2c 20 79 79 54 6f  LookAhead], yyTo
30636 6b 65 6e 4e 61 6d 65 5b 59 59 57 49 4c 44 43 41  kenName[YYWILDCA
30637 52 44 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20  RD]);.          
30638 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42  }.#endif /* NDEB
30639 55 47 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  UG */.          
3063a 72 65 74 75 72 6e 20 79 79 5f 61 63 74 69 6f 6e  return yy_action
3063b 5b 6a 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  [j];.        }. 
3063c 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a       }.#endif /*
3063d 20 59 59 57 49 4c 44 43 41 52 44 20 2a 2f 0a 20   YYWILDCARD */. 
3063e 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20     }.    return 
3063f 79 79 5f 64 65 66 61 75 6c 74 5b 73 74 61 74 65  yy_default[state
30640 6e 6f 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  no];.  }else{.  
30641 20 20 72 65 74 75 72 6e 20 79 79 5f 61 63 74 69    return yy_acti
30642 6f 6e 5b 69 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  on[i];.  }.}../*
30643 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 61 70 70  .** Find the app
30644 72 6f 70 72 69 61 74 65 20 61 63 74 69 6f 6e 20  ropriate action 
30645 66 6f 72 20 61 20 70 61 72 73 65 72 20 67 69 76  for a parser giv
30646 65 6e 20 74 68 65 20 6e 6f 6e 2d 74 65 72 6d 69  en the non-termi
30647 6e 61 6c 0a 2a 2a 20 6c 6f 6f 6b 2d 61 68 65 61  nal.** look-ahea
30648 64 20 74 6f 6b 65 6e 20 69 4c 6f 6f 6b 41 68 65  d token iLookAhe
30649 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ad..**.** If the
3064a 20 6c 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65   look-ahead toke
3064b 6e 20 69 73 20 59 59 4e 4f 43 4f 44 45 2c 20 74  n is YYNOCODE, t
3064c 68 65 6e 20 63 68 65 63 6b 20 74 6f 20 73 65 65  hen check to see
3064d 20 69 66 20 74 68 65 20 61 63 74 69 6f 6e 20 69   if the action i
3064e 73 0a 2a 2a 20 69 6e 64 65 70 65 6e 64 65 6e 74  s.** independent
3064f 20 6f 66 20 74 68 65 20 6c 6f 6f 6b 2d 61 68 65   of the look-ahe
30650 61 64 2e 20 20 49 66 20 69 74 20 69 73 2c 20 72  ad.  If it is, r
30651 65 74 75 72 6e 20 74 68 65 20 61 63 74 69 6f 6e  eturn the action
30652 2c 20 6f 74 68 65 72 77 69 73 65 0a 2a 2a 20 72  , otherwise.** r
30653 65 74 75 72 6e 20 59 59 5f 4e 4f 5f 41 43 54 49  eturn YY_NO_ACTI
30654 4f 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ON..*/.static in
30655 74 20 79 79 5f 66 69 6e 64 5f 72 65 64 75 63 65  t yy_find_reduce
30656 5f 61 63 74 69 6f 6e 28 0a 20 20 69 6e 74 20 73  _action(.  int s
30657 74 61 74 65 6e 6f 2c 20 20 20 20 20 20 20 20 20  tateno,         
30658 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
30659 73 74 61 74 65 20 6e 75 6d 62 65 72 20 2a 2f 0a  state number */.
3065a 20 20 59 59 43 4f 44 45 54 59 50 45 20 69 4c 6f    YYCODETYPE iLo
3065b 6f 6b 41 68 65 61 64 20 20 20 20 20 2f 2a 20 54  okAhead     /* T
3065c 68 65 20 6c 6f 6f 6b 2d 61 68 65 61 64 20 74 6f  he look-ahead to
3065d 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ken */.){.  int 
3065e 69 3b 0a 23 69 66 64 65 66 20 59 59 45 52 52 4f  i;.#ifdef YYERRO
3065f 52 53 59 4d 42 4f 4c 0a 20 20 69 66 28 20 73 74  RSYMBOL.  if( st
30660 61 74 65 6e 6f 3e 59 59 5f 52 45 44 55 43 45 5f  ateno>YY_REDUCE_
30661 4d 41 58 20 29 7b 0a 20 20 20 20 72 65 74 75 72  MAX ){.    retur
30662 6e 20 79 79 5f 64 65 66 61 75 6c 74 5b 73 74 61  n yy_default[sta
30663 74 65 6e 6f 5d 3b 0a 20 20 7d 0a 23 65 6c 73 65  teno];.  }.#else
30664 0a 20 20 61 73 73 65 72 74 28 20 73 74 61 74 65  .  assert( state
30665 6e 6f 3c 3d 59 59 5f 52 45 44 55 43 45 5f 4d 41  no<=YY_REDUCE_MA
30666 58 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 69 20  X );.#endif.  i 
30667 3d 20 79 79 5f 72 65 64 75 63 65 5f 6f 66 73 74  = yy_reduce_ofst
30668 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20 20 61 73 73  [stateno];.  ass
30669 65 72 74 28 20 69 21 3d 59 59 5f 52 45 44 55 43  ert( i!=YY_REDUC
3066a 45 5f 55 53 45 5f 44 46 4c 54 20 29 3b 0a 20 20  E_USE_DFLT );.  
3066b 61 73 73 65 72 74 28 20 69 4c 6f 6f 6b 41 68 65  assert( iLookAhe
3066c 61 64 21 3d 59 59 4e 4f 43 4f 44 45 20 29 3b 0a  ad!=YYNOCODE );.
3066d 20 20 69 20 2b 3d 20 69 4c 6f 6f 6b 41 68 65 61    i += iLookAhea
3066e 64 3b 0a 23 69 66 64 65 66 20 59 59 45 52 52 4f  d;.#ifdef YYERRO
3066f 52 53 59 4d 42 4f 4c 0a 20 20 69 66 28 20 69 3c  RSYMBOL.  if( i<
30670 30 20 7c 7c 20 69 3e 3d 59 59 5f 53 5a 5f 41 43  0 || i>=YY_SZ_AC
30671 54 54 41 42 20 7c 7c 20 79 79 5f 6c 6f 6f 6b 61  TTAB || yy_looka
30672 68 65 61 64 5b 69 5d 21 3d 69 4c 6f 6f 6b 41 68  head[i]!=iLookAh
30673 65 61 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ead ){.    retur
30674 6e 20 79 79 5f 64 65 66 61 75 6c 74 5b 73 74 61  n yy_default[sta
30675 74 65 6e 6f 5d 3b 0a 20 20 7d 0a 23 65 6c 73 65  teno];.  }.#else
30676 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20  .  assert( i>=0 
30677 26 26 20 69 3c 59 59 5f 53 5a 5f 41 43 54 54 41  && i<YY_SZ_ACTTA
30678 42 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 79  B );.  assert( y
30679 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b 69 5d 3d 3d  y_lookahead[i]==
3067a 69 4c 6f 6f 6b 41 68 65 61 64 20 29 3b 0a 23 65  iLookAhead );.#e
3067b 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 79 79  ndif.  return yy
3067c 5f 61 63 74 69 6f 6e 5b 69 5d 3b 0a 7d 0a 0a 2f  _action[i];.}../
3067d 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
3067e 6e 67 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  ng routine is ca
3067f 6c 6c 65 64 20 69 66 20 74 68 65 20 73 74 61 63  lled if the stac
30680 6b 20 6f 76 65 72 66 6c 6f 77 73 2e 0a 2a 2f 0a  k overflows..*/.
30681 73 74 61 74 69 63 20 76 6f 69 64 20 79 79 53 74  static void yySt
30682 61 63 6b 4f 76 65 72 66 6c 6f 77 28 79 79 50 61  ackOverflow(yyPa
30683 72 73 65 72 20 2a 79 79 70 50 61 72 73 65 72 2c  rser *yypParser,
30684 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 2a 79 79   YYMINORTYPE *yy
30685 70 4d 69 6e 6f 72 29 7b 0a 20 20 20 73 71 6c 69  pMinor){.   sqli
30686 74 65 33 50 61 72 73 65 72 41 52 47 5f 46 45 54  te3ParserARG_FET
30687 43 48 3b 0a 20 20 20 79 79 70 50 61 72 73 65 72  CH;.   yypParser
30688 2d 3e 79 79 69 64 78 2d 2d 3b 0a 23 69 66 6e 64  ->yyidx--;.#ifnd
30689 65 66 20 4e 44 45 42 55 47 0a 20 20 20 69 66 28  ef NDEBUG.   if(
3068a 20 79 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0a   yyTraceFILE ){.
3068b 20 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54       fprintf(yyT
3068c 72 61 63 65 46 49 4c 45 2c 22 25 73 53 74 61 63  raceFILE,"%sStac
3068d 6b 20 4f 76 65 72 66 6c 6f 77 21 5c 6e 22 2c 79  k Overflow!\n",y
3068e 79 54 72 61 63 65 50 72 6f 6d 70 74 29 3b 0a 20  yTracePrompt);. 
3068f 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 77 68    }.#endif.   wh
30690 69 6c 65 28 20 79 79 70 50 61 72 73 65 72 2d 3e  ile( yypParser->
30691 79 79 69 64 78 3e 3d 30 20 29 20 79 79 5f 70 6f  yyidx>=0 ) yy_po
30692 70 5f 70 61 72 73 65 72 5f 73 74 61 63 6b 28 79  p_parser_stack(y
30693 79 70 50 61 72 73 65 72 29 3b 0a 20 20 20 2f 2a  ypParser);.   /*
30694 20 48 65 72 65 20 63 6f 64 65 20 69 73 20 69 6e   Here code is in
30695 73 65 72 74 65 64 20 77 68 69 63 68 20 77 69 6c  serted which wil
30696 6c 20 65 78 65 63 75 74 65 20 69 66 20 74 68 65  l execute if the
30697 20 70 61 72 73 65 72 0a 20 20 20 2a 2a 20 73 74   parser.   ** st
30698 61 63 6b 20 65 76 65 72 79 20 6f 76 65 72 66 6c  ack every overfl
30699 6f 77 73 20 2a 2f 0a 0a 20 20 55 4e 55 53 45 44  ows */..  UNUSED
3069a 5f 50 41 52 41 4d 45 54 45 52 28 79 79 70 4d 69  _PARAMETER(yypMi
3069b 6e 6f 72 29 3b 20 2f 2a 20 53 69 6c 65 6e 63 65  nor); /* Silence
3069c 20 73 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 20 77   some compiler w
3069d 61 72 6e 69 6e 67 73 20 2a 2f 0a 20 20 73 71 6c  arnings */.  sql
3069e 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
3069f 72 73 65 2c 20 22 70 61 72 73 65 72 20 73 74 61  rse, "parser sta
306a0 63 6b 20 6f 76 65 72 66 6c 6f 77 22 29 3b 0a 20  ck overflow");. 
306a1 20 70 50 61 72 73 65 2d 3e 70 61 72 73 65 45 72   pParse->parseEr
306a2 72 6f 72 20 3d 20 31 3b 0a 20 20 20 73 71 6c 69  ror = 1;.   sqli
306a3 74 65 33 50 61 72 73 65 72 41 52 47 5f 53 54 4f  te3ParserARG_STO
306a4 52 45 3b 20 2f 2a 20 53 75 70 70 72 65 73 73 20  RE; /* Suppress 
306a5 77 61 72 6e 69 6e 67 20 61 62 6f 75 74 20 75 6e  warning about un
306a6 75 73 65 64 20 25 65 78 74 72 61 5f 61 72 67 75  used %extra_argu
306a7 6d 65 6e 74 20 76 61 72 20 2a 2f 0a 7d 0a 0a 2f  ment var */.}../
306a8 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20 73  *.** Perform a s
306a9 68 69 66 74 20 61 63 74 69 6f 6e 2e 0a 2a 2f 0a  hift action..*/.
306aa 73 74 61 74 69 63 20 76 6f 69 64 20 79 79 5f 73  static void yy_s
306ab 68 69 66 74 28 0a 20 20 79 79 50 61 72 73 65 72  hift(.  yyParser
306ac 20 2a 79 79 70 50 61 72 73 65 72 2c 20 20 20 20   *yypParser,    
306ad 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
306ae 73 65 72 20 74 6f 20 62 65 20 73 68 69 66 74 65  ser to be shifte
306af 64 20 2a 2f 0a 20 20 69 6e 74 20 79 79 4e 65 77  d */.  int yyNew
306b0 53 74 61 74 65 2c 20 20 20 20 20 20 20 20 20 20  State,          
306b1 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20       /* The new 
306b2 73 74 61 74 65 20 74 6f 20 73 68 69 66 74 20 69  state to shift i
306b3 6e 20 2a 2f 0a 20 20 69 6e 74 20 79 79 4d 61 6a  n */.  int yyMaj
306b4 6f 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  or,             
306b5 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 6a 6f       /* The majo
306b6 72 20 74 6f 6b 65 6e 20 74 6f 20 73 68 69 66 74  r token to shift
306b7 20 69 6e 20 2a 2f 0a 20 20 59 59 4d 49 4e 4f 52   in */.  YYMINOR
306b8 54 59 50 45 20 2a 79 79 70 4d 69 6e 6f 72 20 20  TYPE *yypMinor  
306b9 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
306ba 72 20 74 6f 20 74 68 65 20 6d 69 6e 6f 72 20 74  r to the minor t
306bb 6f 6b 65 6e 20 74 6f 20 73 68 69 66 74 20 69 6e  oken to shift in
306bc 20 2a 2f 0a 29 7b 0a 20 20 79 79 53 74 61 63 6b   */.){.  yyStack
306bd 45 6e 74 72 79 20 2a 79 79 74 6f 73 3b 0a 20 20  Entry *yytos;.  
306be 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  yypParser->yyidx
306bf 2b 2b 3b 0a 23 69 66 64 65 66 20 59 59 54 52 41  ++;.#ifdef YYTRA
306c0 43 4b 4d 41 58 53 54 41 43 4b 44 45 50 54 48 0a  CKMAXSTACKDEPTH.
306c1 20 20 69 66 28 20 79 79 70 50 61 72 73 65 72 2d    if( yypParser-
306c2 3e 79 79 69 64 78 3e 79 79 70 50 61 72 73 65 72  >yyidx>yypParser
306c3 2d 3e 79 79 69 64 78 4d 61 78 20 29 7b 0a 20 20  ->yyidxMax ){.  
306c4 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69    yypParser->yyi
306c5 64 78 4d 61 78 20 3d 20 79 79 70 50 61 72 73 65  dxMax = yypParse
306c6 72 2d 3e 79 79 69 64 78 3b 0a 20 20 7d 0a 23 65  r->yyidx;.  }.#e
306c7 6e 64 69 66 0a 23 69 66 20 59 59 53 54 41 43 4b  ndif.#if YYSTACK
306c8 44 45 50 54 48 3e 30 20 0a 20 20 69 66 28 20 79  DEPTH>0 .  if( y
306c9 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e  ypParser->yyidx>
306ca 3d 59 59 53 54 41 43 4b 44 45 50 54 48 20 29 7b  =YYSTACKDEPTH ){
306cb 0a 20 20 20 20 79 79 53 74 61 63 6b 4f 76 65 72  .    yyStackOver
306cc 66 6c 6f 77 28 79 79 70 50 61 72 73 65 72 2c 20  flow(yypParser, 
306cd 79 79 70 4d 69 6e 6f 72 29 3b 0a 20 20 20 20 72  yypMinor);.    r
306ce 65 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6c 73 65  eturn;.  }.#else
306cf 0a 20 20 69 66 28 20 79 79 70 50 61 72 73 65 72  .  if( yypParser
306d0 2d 3e 79 79 69 64 78 3e 3d 79 79 70 50 61 72 73  ->yyidx>=yypPars
306d1 65 72 2d 3e 79 79 73 74 6b 73 7a 20 29 7b 0a 20  er->yystksz ){. 
306d2 20 20 20 79 79 47 72 6f 77 53 74 61 63 6b 28 79     yyGrowStack(y
306d3 79 70 50 61 72 73 65 72 29 3b 0a 20 20 20 20 69  ypParser);.    i
306d4 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79  f( yypParser->yy
306d5 69 64 78 3e 3d 79 79 70 50 61 72 73 65 72 2d 3e  idx>=yypParser->
306d6 79 79 73 74 6b 73 7a 20 29 7b 0a 20 20 20 20 20  yystksz ){.     
306d7 20 79 79 53 74 61 63 6b 4f 76 65 72 66 6c 6f 77   yyStackOverflow
306d8 28 79 79 70 50 61 72 73 65 72 2c 20 79 79 70 4d  (yypParser, yypM
306d9 69 6e 6f 72 29 3b 0a 20 20 20 20 20 20 72 65 74  inor);.      ret
306da 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23  urn;.    }.  }.#
306db 65 6e 64 69 66 0a 20 20 79 79 74 6f 73 20 3d 20  endif.  yytos = 
306dc 26 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74  &yypParser->yyst
306dd 61 63 6b 5b 79 79 70 50 61 72 73 65 72 2d 3e 79  ack[yypParser->y
306de 79 69 64 78 5d 3b 0a 20 20 79 79 74 6f 73 2d 3e  yidx];.  yytos->
306df 73 74 61 74 65 6e 6f 20 3d 20 28 59 59 41 43 54  stateno = (YYACT
306e0 49 4f 4e 54 59 50 45 29 79 79 4e 65 77 53 74 61  IONTYPE)yyNewSta
306e1 74 65 3b 0a 20 20 79 79 74 6f 73 2d 3e 6d 61 6a  te;.  yytos->maj
306e2 6f 72 20 3d 20 28 59 59 43 4f 44 45 54 59 50 45  or = (YYCODETYPE
306e3 29 79 79 4d 61 6a 6f 72 3b 0a 20 20 79 79 74 6f  )yyMajor;.  yyto
306e4 73 2d 3e 6d 69 6e 6f 72 20 3d 20 2a 79 79 70 4d  s->minor = *yypM
306e5 69 6e 6f 72 3b 0a 23 69 66 6e 64 65 66 20 4e 44  inor;.#ifndef ND
306e6 45 42 55 47 0a 20 20 69 66 28 20 79 79 54 72 61  EBUG.  if( yyTra
306e7 63 65 46 49 4c 45 20 26 26 20 79 79 70 50 61 72  ceFILE && yypPar
306e8 73 65 72 2d 3e 79 79 69 64 78 3e 30 20 29 7b 0a  ser->yyidx>0 ){.
306e9 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66      int i;.    f
306ea 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49  printf(yyTraceFI
306eb 4c 45 2c 22 25 73 53 68 69 66 74 20 25 64 5c 6e  LE,"%sShift %d\n
306ec 22 2c 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c  ",yyTracePrompt,
306ed 79 79 4e 65 77 53 74 61 74 65 29 3b 0a 20 20 20  yyNewState);.   
306ee 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65   fprintf(yyTrace
306ef 46 49 4c 45 2c 22 25 73 53 74 61 63 6b 3a 22 2c  FILE,"%sStack:",
306f0 79 79 54 72 61 63 65 50 72 6f 6d 70 74 29 3b 0a  yyTracePrompt);.
306f1 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d      for(i=1; i<=
306f2 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  yypParser->yyidx
306f3 3b 20 69 2b 2b 29 0a 20 20 20 20 20 20 66 70 72  ; i++).      fpr
306f4 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45  intf(yyTraceFILE
306f5 2c 22 20 25 73 22 2c 79 79 54 6f 6b 65 6e 4e 61  ," %s",yyTokenNa
306f6 6d 65 5b 79 79 70 50 61 72 73 65 72 2d 3e 79 79  me[yypParser->yy
306f7 73 74 61 63 6b 5b 69 5d 2e 6d 61 6a 6f 72 5d 29  stack[i].major])
306f8 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 79 79  ;.    fprintf(yy
306f9 54 72 61 63 65 46 49 4c 45 2c 22 5c 6e 22 29 3b  TraceFILE,"\n");
306fa 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f  .  }.#endif.}../
306fb 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
306fc 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69  table contains i
306fd 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
306fe 20 65 76 65 72 79 20 72 75 6c 65 20 74 68 61 74   every rule that
306ff 0a 2a 2a 20 69 73 20 75 73 65 64 20 64 75 72 69  .** is used duri
30700 6e 67 20 74 68 65 20 72 65 64 75 63 65 2e 0a 2a  ng the reduce..*
30701 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73  /.static const s
30702 74 72 75 63 74 20 7b 0a 20 20 59 59 43 4f 44 45  truct {.  YYCODE
30703 54 59 50 45 20 6c 68 73 3b 20 20 20 20 20 20 20  TYPE lhs;       
30704 20 20 2f 2a 20 53 79 6d 62 6f 6c 20 6f 6e 20 74    /* Symbol on t
30705 68 65 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64  he left-hand sid
30706 65 20 6f 66 20 74 68 65 20 72 75 6c 65 20 2a 2f  e of the rule */
30707 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
30708 20 6e 72 68 73 3b 20 20 20 20 20 2f 2a 20 4e 75   nrhs;     /* Nu
30709 6d 62 65 72 20 6f 66 20 72 69 67 68 74 2d 68 61  mber of right-ha
3070a 6e 64 20 73 69 64 65 20 73 79 6d 62 6f 6c 73 20  nd side symbols 
3070b 69 6e 20 74 68 65 20 72 75 6c 65 20 2a 2f 0a 7d  in the rule */.}
3070c 20 79 79 52 75 6c 65 49 6e 66 6f 5b 5d 20 3d 20   yyRuleInfo[] = 
3070d 7b 0a 20 20 7b 20 31 34 32 2c 20 31 20 7d 2c 0a  {.  { 142, 1 },.
3070e 20 20 7b 20 31 34 33 2c 20 32 20 7d 2c 0a 20 20    { 143, 2 },.  
3070f 7b 20 31 34 33 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 143, 1 },.  { 
30710 31 34 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34  144, 1 },.  { 14
30711 34 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34 35 2c  4, 3 },.  { 145,
30712 20 30 20 7d 2c 0a 20 20 7b 20 31 34 35 2c 20 31   0 },.  { 145, 1
30713 20 7d 2c 0a 20 20 7b 20 31 34 35 2c 20 33 20 7d   },.  { 145, 3 }
30714 2c 0a 20 20 7b 20 31 34 36 2c 20 31 20 7d 2c 0a  ,.  { 146, 1 },.
30715 20 20 7b 20 31 34 37 2c 20 33 20 7d 2c 0a 20 20    { 147, 3 },.  
30716 7b 20 31 34 39 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 149, 0 },.  { 
30717 31 34 39 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34  149, 1 },.  { 14
30718 39 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 38 2c  9, 2 },.  { 148,
30719 20 30 20 7d 2c 0a 20 20 7b 20 31 34 38 2c 20 31   0 },.  { 148, 1
3071a 20 7d 2c 0a 20 20 7b 20 31 34 38 2c 20 31 20 7d   },.  { 148, 1 }
3071b 2c 0a 20 20 7b 20 31 34 38 2c 20 31 20 7d 2c 0a  ,.  { 148, 1 },.
3071c 20 20 7b 20 31 34 37 2c 20 32 20 7d 2c 0a 20 20    { 147, 2 },.  
3071d 7b 20 31 34 37 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 147, 2 },.  { 
3071e 31 34 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 35  147, 2 },.  { 15
3071f 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 35 31 2c  1, 1 },.  { 151,
30720 20 30 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 32   0 },.  { 147, 2
30721 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 33 20 7d   },.  { 147, 3 }
30722 2c 0a 20 20 7b 20 31 34 37 2c 20 35 20 7d 2c 0a  ,.  { 147, 5 },.
30723 20 20 7b 20 31 34 37 2c 20 32 20 7d 2c 0a 20 20    { 147, 2 },.  
30724 7b 20 31 35 32 2c 20 36 20 7d 2c 0a 20 20 7b 20  { 152, 6 },.  { 
30725 31 35 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 35  154, 1 },.  { 15
30726 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 35 36 2c  6, 0 },.  { 156,
30727 20 33 20 7d 2c 0a 20 20 7b 20 31 35 35 2c 20 31   3 },.  { 155, 1
30728 20 7d 2c 0a 20 20 7b 20 31 35 35 2c 20 30 20 7d   },.  { 155, 0 }
30729 2c 0a 20 20 7b 20 31 35 33 2c 20 34 20 7d 2c 0a  ,.  { 153, 4 },.
3072a 20 20 7b 20 31 35 33 2c 20 32 20 7d 2c 0a 20 20    { 153, 2 },.  
3072b 7b 20 31 35 38 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 158, 3 },.  { 
3072c 31 35 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36  158, 1 },.  { 16
3072d 31 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 36 32 2c  1, 3 },.  { 162,
3072e 20 31 20 7d 2c 0a 20 20 7b 20 31 36 35 2c 20 31   1 },.  { 165, 1
3072f 20 7d 2c 0a 20 20 7b 20 31 36 35 2c 20 31 20 7d   },.  { 165, 1 }
30730 2c 0a 20 20 7b 20 31 36 36 2c 20 31 20 7d 2c 0a  ,.  { 166, 1 },.
30731 20 20 7b 20 31 35 30 2c 20 31 20 7d 2c 0a 20 20    { 150, 1 },.  
30732 7b 20 31 35 30 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 150, 1 },.  { 
30733 31 35 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36  150, 1 },.  { 16
30734 33 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 36 33 2c  3, 0 },.  { 163,
30735 20 31 20 7d 2c 0a 20 20 7b 20 31 36 37 2c 20 31   1 },.  { 167, 1
30736 20 7d 2c 0a 20 20 7b 20 31 36 37 2c 20 34 20 7d   },.  { 167, 4 }
30737 2c 0a 20 20 7b 20 31 36 37 2c 20 36 20 7d 2c 0a  ,.  { 167, 6 },.
30738 20 20 7b 20 31 36 38 2c 20 31 20 7d 2c 0a 20 20    { 168, 1 },.  
30739 7b 20 31 36 38 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 168, 2 },.  { 
3073a 31 36 39 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36  169, 1 },.  { 16
3073b 39 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36 34 2c  9, 1 },.  { 164,
3073c 20 32 20 7d 2c 0a 20 20 7b 20 31 36 34 2c 20 30   2 },.  { 164, 0
3073d 20 7d 2c 0a 20 20 7b 20 31 37 32 2c 20 33 20 7d   },.  { 172, 3 }
3073e 2c 0a 20 20 7b 20 31 37 32 2c 20 31 20 7d 2c 0a  ,.  { 172, 1 },.
3073f 20 20 7b 20 31 37 33 2c 20 32 20 7d 2c 0a 20 20    { 173, 2 },.  
30740 7b 20 31 37 33 2c 20 34 20 7d 2c 0a 20 20 7b 20  { 173, 4 },.  { 
30741 31 37 33 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37  173, 3 },.  { 17
30742 33 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 33 2c  3, 3 },.  { 173,
30743 20 32 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 32   2 },.  { 173, 2
30744 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 33 20 7d   },.  { 173, 3 }
30745 2c 0a 20 20 7b 20 31 37 33 2c 20 35 20 7d 2c 0a  ,.  { 173, 5 },.
30746 20 20 7b 20 31 37 33 2c 20 32 20 7d 2c 0a 20 20    { 173, 2 },.  
30747 7b 20 31 37 33 2c 20 34 20 7d 2c 0a 20 20 7b 20  { 173, 4 },.  { 
30748 31 37 33 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 37  173, 4 },.  { 17
30749 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 33 2c  3, 1 },.  { 173,
3074a 20 32 20 7d 2c 0a 20 20 7b 20 31 37 38 2c 20 30   2 },.  { 178, 0
3074b 20 7d 2c 0a 20 20 7b 20 31 37 38 2c 20 31 20 7d   },.  { 178, 1 }
3074c 2c 0a 20 20 7b 20 31 38 30 2c 20 30 20 7d 2c 0a  ,.  { 180, 0 },.
3074d 20 20 7b 20 31 38 30 2c 20 32 20 7d 2c 0a 20 20    { 180, 2 },.  
3074e 7b 20 31 38 32 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 182, 2 },.  { 
3074f 31 38 32 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 38  182, 3 },.  { 18
30750 32 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 38 32 2c  2, 3 },.  { 182,
30751 20 33 20 7d 2c 0a 20 20 7b 20 31 38 33 2c 20 32   3 },.  { 183, 2
30752 20 7d 2c 0a 20 20 7b 20 31 38 33 2c 20 32 20 7d   },.  { 183, 2 }
30753 2c 0a 20 20 7b 20 31 38 33 2c 20 31 20 7d 2c 0a  ,.  { 183, 1 },.
30754 20 20 7b 20 31 38 33 2c 20 31 20 7d 2c 0a 20 20    { 183, 1 },.  
30755 7b 20 31 38 31 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 181, 3 },.  { 
30756 31 38 31 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38  181, 2 },.  { 18
30757 34 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 38 34 2c  4, 0 },.  { 184,
30758 20 32 20 7d 2c 0a 20 20 7b 20 31 38 34 2c 20 32   2 },.  { 184, 2
30759 20 7d 2c 0a 20 20 7b 20 31 35 39 2c 20 30 20 7d   },.  { 159, 0 }
3075a 2c 0a 20 20 7b 20 31 35 39 2c 20 32 20 7d 2c 0a  ,.  { 159, 2 },.
3075b 20 20 7b 20 31 38 35 2c 20 33 20 7d 2c 0a 20 20    { 185, 3 },.  
3075c 7b 20 31 38 35 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 185, 2 },.  { 
3075d 31 38 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 38  185, 1 },.  { 18
3075e 36 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38 36 2c  6, 2 },.  { 186,
3075f 20 37 20 7d 2c 0a 20 20 7b 20 31 38 36 2c 20 35   7 },.  { 186, 5
30760 20 7d 2c 0a 20 20 7b 20 31 38 36 2c 20 35 20 7d   },.  { 186, 5 }
30761 2c 0a 20 20 7b 20 31 38 36 2c 20 31 30 20 7d 2c  ,.  { 186, 10 },
30762 0a 20 20 7b 20 31 38 38 2c 20 30 20 7d 2c 0a 20  .  { 188, 0 },. 
30763 20 7b 20 31 38 38 2c 20 31 20 7d 2c 0a 20 20 7b   { 188, 1 },.  {
30764 20 31 37 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 31   176, 0 },.  { 1
30765 37 36 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 38 39  76, 3 },.  { 189
30766 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 38 39 2c 20  , 0 },.  { 189, 
30767 32 20 7d 2c 0a 20 20 7b 20 31 39 30 2c 20 31 20  2 },.  { 190, 1 
30768 7d 2c 0a 20 20 7b 20 31 39 30 2c 20 31 20 7d 2c  },.  { 190, 1 },
30769 0a 20 20 7b 20 31 39 30 2c 20 31 20 7d 2c 0a 20  .  { 190, 1 },. 
3076a 20 7b 20 31 34 37 2c 20 34 20 7d 2c 0a 20 20 7b   { 147, 4 },.  {
3076b 20 31 39 32 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   192, 2 },.  { 1
3076c 39 32 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 34 37  92, 0 },.  { 147
3076d 2c 20 38 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 8 },.  { 147, 
3076e 34 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 31 20  4 },.  { 147, 1 
3076f 7d 2c 0a 20 20 7b 20 31 36 30 2c 20 31 20 7d 2c  },.  { 160, 1 },
30770 0a 20 20 7b 20 31 36 30 2c 20 33 20 7d 2c 0a 20  .  { 160, 3 },. 
30771 20 7b 20 31 39 35 2c 20 31 20 7d 2c 0a 20 20 7b   { 195, 1 },.  {
30772 20 31 39 35 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   195, 2 },.  { 1
30773 39 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 39 34  95, 1 },.  { 194
30774 2c 20 39 20 7d 2c 0a 20 20 7b 20 31 39 36 2c 20  , 9 },.  { 196, 
30775 31 20 7d 2c 0a 20 20 7b 20 31 39 36 2c 20 31 20  1 },.  { 196, 1 
30776 7d 2c 0a 20 20 7b 20 31 39 36 2c 20 30 20 7d 2c  },.  { 196, 0 },
30777 0a 20 20 7b 20 32 30 34 2c 20 32 20 7d 2c 0a 20  .  { 204, 2 },. 
30778 20 7b 20 32 30 34 2c 20 30 20 7d 2c 0a 20 20 7b   { 204, 0 },.  {
30779 20 31 39 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   197, 3 },.  { 1
3077a 39 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 39 37  97, 2 },.  { 197
3077b 2c 20 34 20 7d 2c 0a 20 20 7b 20 32 30 35 2c 20  , 4 },.  { 205, 
3077c 32 20 7d 2c 0a 20 20 7b 20 32 30 35 2c 20 31 20  2 },.  { 205, 1 
3077d 7d 2c 0a 20 20 7b 20 32 30 35 2c 20 30 20 7d 2c  },.  { 205, 0 },
3077e 0a 20 20 7b 20 31 39 38 2c 20 30 20 7d 2c 0a 20  .  { 198, 0 },. 
3077f 20 7b 20 31 39 38 2c 20 32 20 7d 2c 0a 20 20 7b   { 198, 2 },.  {
30780 20 32 30 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 32   207, 2 },.  { 2
30781 30 37 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 36  07, 0 },.  { 206
30782 2c 20 37 20 7d 2c 0a 20 20 7b 20 32 30 36 2c 20  , 7 },.  { 206, 
30783 37 20 7d 2c 0a 20 20 7b 20 32 30 36 2c 20 37 20  7 },.  { 206, 7 
30784 7d 2c 0a 20 20 7b 20 31 35 37 2c 20 30 20 7d 2c  },.  { 157, 0 },
30785 0a 20 20 7b 20 31 35 37 2c 20 32 20 7d 2c 0a 20  .  { 157, 2 },. 
30786 20 7b 20 31 39 33 2c 20 32 20 7d 2c 0a 20 20 7b   { 193, 2 },.  {
30787 20 32 30 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   208, 1 },.  { 2
30788 30 38 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30 38  08, 2 },.  { 208
30789 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 30 38 2c 20  , 3 },.  { 208, 
3078a 34 20 7d 2c 0a 20 20 7b 20 32 31 30 2c 20 32 20  4 },.  { 210, 2 
3078b 7d 2c 0a 20 20 7b 20 32 31 30 2c 20 30 20 7d 2c  },.  { 210, 0 },
3078c 0a 20 20 7b 20 32 30 39 2c 20 30 20 7d 2c 0a 20  .  { 209, 0 },. 
3078d 20 7b 20 32 30 39 2c 20 33 20 7d 2c 0a 20 20 7b   { 209, 3 },.  {
3078e 20 32 30 39 2c 20 32 20 7d 2c 0a 20 20 7b 20 32   209, 2 },.  { 2
3078f 31 31 2c 20 34 20 7d 2c 0a 20 20 7b 20 32 31 31  11, 4 },.  { 211
30790 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 32 2c 20  , 0 },.  { 202, 
30791 30 20 7d 2c 0a 20 20 7b 20 32 30 32 2c 20 33 20  0 },.  { 202, 3 
30792 7d 2c 0a 20 20 7b 20 32 31 34 2c 20 34 20 7d 2c  },.  { 214, 4 },
30793 0a 20 20 7b 20 32 31 34 2c 20 32 20 7d 2c 0a 20  .  { 214, 2 },. 
30794 20 7b 20 32 31 35 2c 20 31 20 7d 2c 0a 20 20 7b   { 215, 1 },.  {
30795 20 31 37 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   177, 1 },.  { 1
30796 37 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 37  77, 1 },.  { 177
30797 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 30 2c 20  , 0 },.  { 200, 
30798 30 20 7d 2c 0a 20 20 7b 20 32 30 30 2c 20 33 20  0 },.  { 200, 3 
30799 7d 2c 0a 20 20 7b 20 32 30 31 2c 20 30 20 7d 2c  },.  { 201, 0 },
3079a 0a 20 20 7b 20 32 30 31 2c 20 32 20 7d 2c 0a 20  .  { 201, 2 },. 
3079b 20 7b 20 32 30 33 2c 20 30 20 7d 2c 0a 20 20 7b   { 203, 0 },.  {
3079c 20 32 30 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 32   203, 2 },.  { 2
3079d 30 33 2c 20 34 20 7d 2c 0a 20 20 7b 20 32 30 33  03, 4 },.  { 203
3079e 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 4 },.  { 147, 
3079f 35 20 7d 2c 0a 20 20 7b 20 31 39 39 2c 20 30 20  5 },.  { 199, 0 
307a0 7d 2c 0a 20 20 7b 20 31 39 39 2c 20 32 20 7d 2c  },.  { 199, 2 },
307a1 0a 20 20 7b 20 31 34 37 2c 20 37 20 7d 2c 0a 20  .  { 147, 7 },. 
307a2 20 7b 20 32 31 37 2c 20 35 20 7d 2c 0a 20 20 7b   { 217, 5 },.  {
307a3 20 32 31 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   217, 3 },.  { 1
307a4 34 37 2c 20 38 20 7d 2c 0a 20 20 7b 20 31 34 37  47, 8 },.  { 147
307a5 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 5 },.  { 147, 
307a6 36 20 7d 2c 0a 20 20 7b 20 32 31 38 2c 20 32 20  6 },.  { 218, 2 
307a7 7d 2c 0a 20 20 7b 20 32 31 38 2c 20 31 20 7d 2c  },.  { 218, 1 },
307a8 0a 20 20 7b 20 32 32 30 2c 20 33 20 7d 2c 0a 20  .  { 220, 3 },. 
307a9 20 7b 20 32 32 30 2c 20 31 20 7d 2c 0a 20 20 7b   { 220, 1 },.  {
307aa 20 32 31 39 2c 20 30 20 7d 2c 0a 20 20 7b 20 32   219, 0 },.  { 2
307ab 31 39 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 31 33  19, 3 },.  { 213
307ac 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 31 33 2c 20  , 3 },.  { 213, 
307ad 31 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 31 20  1 },.  { 175, 1 
307ae 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c  },.  { 175, 3 },
307af 0a 20 20 7b 20 31 37 34 2c 20 31 20 7d 2c 0a 20  .  { 174, 1 },. 
307b0 20 7b 20 31 37 35 2c 20 31 20 7d 2c 0a 20 20 7b   { 175, 1 },.  {
307b1 20 31 37 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   175, 1 },.  { 1
307b2 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35  75, 3 },.  { 175
307b3 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20  , 5 },.  { 174, 
307b4 31 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20 31 20  1 },.  { 174, 1 
307b5 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 31 20 7d 2c  },.  { 175, 1 },
307b6 0a 20 20 7b 20 31 37 35 2c 20 31 20 7d 2c 0a 20  .  { 175, 1 },. 
307b7 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b   { 175, 3 },.  {
307b8 20 31 37 35 2c 20 36 20 7d 2c 0a 20 20 7b 20 31   175, 6 },.  { 1
307b9 37 35 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 37 35  75, 5 },.  { 175
307ba 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20  , 4 },.  { 174, 
307bb 31 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20  1 },.  { 175, 3 
307bc 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c  },.  { 175, 3 },
307bd 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20  .  { 175, 3 },. 
307be 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b   { 175, 3 },.  {
307bf 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   175, 3 },.  { 1
307c0 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35  75, 3 },.  { 175
307c1 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20  , 3 },.  { 175, 
307c2 33 20 7d 2c 0a 20 20 7b 20 32 32 32 2c 20 31 20  3 },.  { 222, 1 
307c3 7d 2c 0a 20 20 7b 20 32 32 32 2c 20 32 20 7d 2c  },.  { 222, 2 },
307c4 0a 20 20 7b 20 32 32 32 2c 20 31 20 7d 2c 0a 20  .  { 222, 1 },. 
307c5 20 7b 20 32 32 32 2c 20 32 20 7d 2c 0a 20 20 7b   { 222, 2 },.  {
307c6 20 32 32 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 32   223, 2 },.  { 2
307c7 32 33 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 37 35  23, 0 },.  { 175
307c8 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20  , 4 },.  { 175, 
307c9 32 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20  2 },.  { 175, 3 
307ca 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c  },.  { 175, 3 },
307cb 0a 20 20 7b 20 31 37 35 2c 20 34 20 7d 2c 0a 20  .  { 175, 4 },. 
307cc 20 7b 20 31 37 35 2c 20 32 20 7d 2c 0a 20 20 7b   { 175, 2 },.  {
307cd 20 31 37 35 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   175, 2 },.  { 1
307ce 37 35 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 35  75, 2 },.  { 175
307cf 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 32 34 2c 20  , 2 },.  { 224, 
307d0 31 20 7d 2c 0a 20 20 7b 20 32 32 34 2c 20 32 20  1 },.  { 224, 2 
307d1 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 35 20 7d 2c  },.  { 175, 5 },
307d2 0a 20 20 7b 20 32 32 35 2c 20 31 20 7d 2c 0a 20  .  { 225, 1 },. 
307d3 20 7b 20 32 32 35 2c 20 32 20 7d 2c 0a 20 20 7b   { 225, 2 },.  {
307d4 20 31 37 35 2c 20 35 20 7d 2c 0a 20 20 7b 20 31   175, 5 },.  { 1
307d5 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35  75, 3 },.  { 175
307d6 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20  , 5 },.  { 175, 
307d7 34 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 34 20  4 },.  { 175, 4 
307d8 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 35 20 7d 2c  },.  { 175, 5 },
307d9 0a 20 20 7b 20 32 32 37 2c 20 35 20 7d 2c 0a 20  .  { 227, 5 },. 
307da 20 7b 20 32 32 37 2c 20 34 20 7d 2c 0a 20 20 7b   { 227, 4 },.  {
307db 20 32 32 38 2c 20 32 20 7d 2c 0a 20 20 7b 20 32   228, 2 },.  { 2
307dc 32 38 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 32 36  28, 0 },.  { 226
307dd 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32 36 2c 20  , 1 },.  { 226, 
307de 30 20 7d 2c 0a 20 20 7b 20 32 32 31 2c 20 31 20  0 },.  { 221, 1 
307df 7d 2c 0a 20 20 7b 20 32 32 31 2c 20 30 20 7d 2c  },.  { 221, 0 },
307e0 0a 20 20 7b 20 32 31 36 2c 20 33 20 7d 2c 0a 20  .  { 216, 3 },. 
307e1 20 7b 20 32 31 36 2c 20 31 20 7d 2c 0a 20 20 7b   { 216, 1 },.  {
307e2 20 31 34 37 2c 20 31 31 20 7d 2c 0a 20 20 7b 20   147, 11 },.  { 
307e3 32 32 39 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32  229, 1 },.  { 22
307e4 39 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 37 39 2c  9, 0 },.  { 179,
307e5 20 30 20 7d 2c 0a 20 20 7b 20 31 37 39 2c 20 33   0 },.  { 179, 3
307e6 20 7d 2c 0a 20 20 7b 20 31 38 37 2c 20 35 20 7d   },.  { 187, 5 }
307e7 2c 0a 20 20 7b 20 31 38 37 2c 20 33 20 7d 2c 0a  ,.  { 187, 3 },.
307e8 20 20 7b 20 32 33 30 2c 20 30 20 7d 2c 0a 20 20    { 230, 0 },.  
307e9 7b 20 32 33 30 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 230, 2 },.  { 
307ea 31 34 37 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 34  147, 4 },.  { 14
307eb 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  7, 1 },.  { 147,
307ec 20 32 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 33   2 },.  { 147, 3
307ed 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 35 20 7d   },.  { 147, 5 }
307ee 2c 0a 20 20 7b 20 31 34 37 2c 20 36 20 7d 2c 0a  ,.  { 147, 6 },.
307ef 20 20 7b 20 31 34 37 2c 20 35 20 7d 2c 0a 20 20    { 147, 5 },.  
307f0 7b 20 31 34 37 2c 20 36 20 7d 2c 0a 20 20 7b 20  { 147, 6 },.  { 
307f1 32 33 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 33  231, 1 },.  { 23
307f2 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 33 31 2c  1, 1 },.  { 231,
307f3 20 31 20 7d 2c 0a 20 20 7b 20 32 33 31 2c 20 31   1 },.  { 231, 1
307f4 20 7d 2c 0a 20 20 7b 20 32 33 31 2c 20 31 20 7d   },.  { 231, 1 }
307f5 2c 0a 20 20 7b 20 31 37 30 2c 20 32 20 7d 2c 0a  ,.  { 170, 2 },.
307f6 20 20 7b 20 31 37 31 2c 20 32 20 7d 2c 0a 20 20    { 171, 2 },.  
307f7 7b 20 32 33 33 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 233, 1 },.  { 
307f8 32 33 32 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 33  232, 1 },.  { 23
307f9 32 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  2, 0 },.  { 147,
307fa 20 35 20 7d 2c 0a 20 20 7b 20 32 33 34 2c 20 31   5 },.  { 234, 1
307fb 31 20 7d 2c 0a 20 20 7b 20 32 33 36 2c 20 31 20  1 },.  { 236, 1 
307fc 7d 2c 0a 20 20 7b 20 32 33 36 2c 20 31 20 7d 2c  },.  { 236, 1 },
307fd 0a 20 20 7b 20 32 33 36 2c 20 32 20 7d 2c 0a 20  .  { 236, 2 },. 
307fe 20 7b 20 32 33 36 2c 20 30 20 7d 2c 0a 20 20 7b   { 236, 0 },.  {
307ff 20 32 33 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   237, 1 },.  { 2
30800 33 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 33 37  37, 1 },.  { 237
30801 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 33 38 2c 20  , 3 },.  { 238, 
30802 30 20 7d 2c 0a 20 20 7b 20 32 33 38 2c 20 33 20  0 },.  { 238, 3 
30803 7d 2c 0a 20 20 7b 20 32 33 39 2c 20 30 20 7d 2c  },.  { 239, 0 },
30804 0a 20 20 7b 20 32 33 39 2c 20 32 20 7d 2c 0a 20  .  { 239, 2 },. 
30805 20 7b 20 32 33 35 2c 20 33 20 7d 2c 0a 20 20 7b   { 235, 3 },.  {
30806 20 32 33 35 2c 20 32 20 7d 2c 0a 20 20 7b 20 32   235, 2 },.  { 2
30807 34 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 34 31  41, 1 },.  { 241
30808 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 34 32 2c 20  , 3 },.  { 242, 
30809 30 20 7d 2c 0a 20 20 7b 20 32 34 32 2c 20 33 20  0 },.  { 242, 3 
3080a 7d 2c 0a 20 20 7b 20 32 34 32 2c 20 32 20 7d 2c  },.  { 242, 2 },
3080b 0a 20 20 7b 20 32 34 30 2c 20 37 20 7d 2c 0a 20  .  { 240, 7 },. 
3080c 20 7b 20 32 34 30 2c 20 38 20 7d 2c 0a 20 20 7b   { 240, 8 },.  {
3080d 20 32 34 30 2c 20 35 20 7d 2c 0a 20 20 7b 20 32   240, 5 },.  { 2
3080e 34 30 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 34 30  40, 5 },.  { 240
3080f 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20  , 1 },.  { 175, 
30810 34 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 36 20  4 },.  { 175, 6 
30811 7d 2c 0a 20 20 7b 20 31 39 31 2c 20 31 20 7d 2c  },.  { 191, 1 },
30812 0a 20 20 7b 20 31 39 31 2c 20 31 20 7d 2c 0a 20  .  { 191, 1 },. 
30813 20 7b 20 31 39 31 2c 20 31 20 7d 2c 0a 20 20 7b   { 191, 1 },.  {
30814 20 31 34 37 2c 20 34 20 7d 2c 0a 20 20 7b 20 31   147, 4 },.  { 1
30815 34 37 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 34 37  47, 6 },.  { 147
30816 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 34 34 2c 20  , 3 },.  { 244, 
30817 30 20 7d 2c 0a 20 20 7b 20 32 34 34 2c 20 32 20  0 },.  { 244, 2 
30818 7d 2c 0a 20 20 7b 20 32 34 33 2c 20 31 20 7d 2c  },.  { 243, 1 },
30819 0a 20 20 7b 20 32 34 33 2c 20 30 20 7d 2c 0a 20  .  { 243, 0 },. 
3081a 20 7b 20 31 34 37 2c 20 31 20 7d 2c 0a 20 20 7b   { 147, 1 },.  {
3081b 20 31 34 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   147, 3 },.  { 1
3081c 34 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37  47, 1 },.  { 147
3081d 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 3 },.  { 147, 
3081e 36 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 36 20  6 },.  { 147, 6 
3081f 7d 2c 0a 20 20 7b 20 32 34 35 2c 20 31 20 7d 2c  },.  { 245, 1 },
30820 0a 20 20 7b 20 32 34 36 2c 20 30 20 7d 2c 0a 20  .  { 246, 0 },. 
30821 20 7b 20 32 34 36 2c 20 31 20 7d 2c 0a 20 20 7b   { 246, 1 },.  {
30822 20 31 34 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   147, 1 },.  { 1
30823 34 37 2c 20 34 20 7d 2c 0a 20 20 7b 20 32 34 37  47, 4 },.  { 247
30824 2c 20 37 20 7d 2c 0a 20 20 7b 20 32 34 38 2c 20  , 7 },.  { 248, 
30825 31 20 7d 2c 0a 20 20 7b 20 32 34 38 2c 20 33 20  1 },.  { 248, 3 
30826 7d 2c 0a 20 20 7b 20 32 34 39 2c 20 30 20 7d 2c  },.  { 249, 0 },
30827 0a 20 20 7b 20 32 34 39 2c 20 32 20 7d 2c 0a 20  .  { 249, 2 },. 
30828 20 7b 20 32 35 30 2c 20 31 20 7d 2c 0a 20 20 7b   { 250, 1 },.  {
30829 20 32 35 30 2c 20 33 20 7d 2c 0a 20 20 7b 20 32   250, 3 },.  { 2
3082a 35 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 35 32  51, 1 },.  { 252
3082b 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 35 32 2c 20  , 0 },.  { 252, 
3082c 34 20 7d 2c 0a 20 20 7b 20 32 35 32 2c 20 32 20  4 },.  { 252, 2 
3082d 7d 2c 0a 7d 3b 0a 0a 73 74 61 74 69 63 20 76 6f  },.};..static vo
3082e 69 64 20 79 79 5f 61 63 63 65 70 74 28 79 79 50  id yy_accept(yyP
3082f 61 72 73 65 72 2a 29 3b 20 20 2f 2a 20 46 6f 72  arser*);  /* For
30830 77 61 72 64 20 44 65 63 6c 61 72 61 74 69 6f 6e  ward Declaration
30831 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f   */../*.** Perfo
30832 72 6d 20 61 20 72 65 64 75 63 65 20 61 63 74 69  rm a reduce acti
30833 6f 6e 20 61 6e 64 20 74 68 65 20 73 68 69 66 74  on and the shift
30834 20 74 68 61 74 20 6d 75 73 74 20 69 6d 6d 65 64   that must immed
30835 69 61 74 65 6c 79 0a 2a 2a 20 66 6f 6c 6c 6f 77  iately.** follow
30836 20 74 68 65 20 72 65 64 75 63 65 2e 0a 2a 2f 0a   the reduce..*/.
30837 73 74 61 74 69 63 20 76 6f 69 64 20 79 79 5f 72  static void yy_r
30838 65 64 75 63 65 28 0a 20 20 79 79 50 61 72 73 65  educe(.  yyParse
30839 72 20 2a 79 79 70 50 61 72 73 65 72 2c 20 20 20  r *yypParser,   
3083a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
3083b 73 65 72 20 2a 2f 0a 20 20 69 6e 74 20 79 79 72  ser */.  int yyr
3083c 75 6c 65 6e 6f 20 20 20 20 20 20 20 20 20 20 20  uleno           
3083d 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
3083e 6f 66 20 74 68 65 20 72 75 6c 65 20 62 79 20 77  of the rule by w
3083f 68 69 63 68 20 74 6f 20 72 65 64 75 63 65 20 2a  hich to reduce *
30840 2f 0a 29 7b 0a 20 20 69 6e 74 20 79 79 67 6f 74  /.){.  int yygot
30841 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  o;              
30842 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65         /* The ne
30843 78 74 20 73 74 61 74 65 20 2a 2f 0a 20 20 69 6e  xt state */.  in
30844 74 20 79 79 61 63 74 3b 20 20 20 20 20 20 20 20  t yyact;        
30845 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
30846 20 54 68 65 20 6e 65 78 74 20 61 63 74 69 6f 6e   The next action
30847 20 2a 2f 0a 20 20 59 59 4d 49 4e 4f 52 54 59 50   */.  YYMINORTYP
30848 45 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 3b 20 20  E yygotominor;  
30849 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4c 48 53        /* The LHS
3084a 20 6f 66 20 74 68 65 20 72 75 6c 65 20 72 65 64   of the rule red
3084b 75 63 65 64 20 2a 2f 0a 20 20 79 79 53 74 61 63  uced */.  yyStac
3084c 6b 45 6e 74 72 79 20 2a 79 79 6d 73 70 3b 20 20  kEntry *yymsp;  
3084d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
3084e 20 74 6f 70 20 6f 66 20 74 68 65 20 70 61 72 73   top of the pars
3084f 65 72 27 73 20 73 74 61 63 6b 20 2a 2f 0a 20 20  er's stack */.  
30850 69 6e 74 20 79 79 73 69 7a 65 3b 20 20 20 20 20  int yysize;     
30851 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30852 2f 2a 20 41 6d 6f 75 6e 74 20 74 6f 20 70 6f 70  /* Amount to pop
30853 20 74 68 65 20 73 74 61 63 6b 20 2a 2f 0a 20 20   the stack */.  
30854 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47  sqlite3ParserARG
30855 5f 46 45 54 43 48 3b 0a 20 20 79 79 6d 73 70 20  _FETCH;.  yymsp 
30856 3d 20 26 79 79 70 50 61 72 73 65 72 2d 3e 79 79  = &yypParser->yy
30857 73 74 61 63 6b 5b 79 79 70 50 61 72 73 65 72 2d  stack[yypParser-
30858 3e 79 79 69 64 78 5d 3b 0a 23 69 66 6e 64 65 66  >yyidx];.#ifndef
30859 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20 79 79   NDEBUG.  if( yy
3085a 54 72 61 63 65 46 49 4c 45 20 26 26 20 79 79 72  TraceFILE && yyr
3085b 75 6c 65 6e 6f 3e 3d 30 20 0a 20 20 20 20 20 20  uleno>=0 .      
3085c 20 20 26 26 20 79 79 72 75 6c 65 6e 6f 3c 28 69    && yyruleno<(i
3085d 6e 74 29 28 73 69 7a 65 6f 66 28 79 79 52 75 6c  nt)(sizeof(yyRul
3085e 65 4e 61 6d 65 29 2f 73 69 7a 65 6f 66 28 79 79  eName)/sizeof(yy
3085f 52 75 6c 65 4e 61 6d 65 5b 30 5d 29 29 20 29 7b  RuleName[0])) ){
30860 0a 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54  .    fprintf(yyT
30861 72 61 63 65 46 49 4c 45 2c 20 22 25 73 52 65 64  raceFILE, "%sRed
30862 75 63 65 20 5b 25 73 5d 2e 5c 6e 22 2c 20 79 79  uce [%s].\n", yy
30863 54 72 61 63 65 50 72 6f 6d 70 74 2c 0a 20 20 20  TracePrompt,.   
30864 20 20 20 79 79 52 75 6c 65 4e 61 6d 65 5b 79 79     yyRuleName[yy
30865 72 75 6c 65 6e 6f 5d 29 3b 0a 20 20 7d 0a 23 65  ruleno]);.  }.#e
30866 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a  ndif /* NDEBUG *
30867 2f 0a 0a 20 20 2f 2a 20 53 69 6c 65 6e 63 65 20  /..  /* Silence 
30868 63 6f 6d 70 6c 61 69 6e 74 73 20 66 72 6f 6d 20  complaints from 
30869 70 75 72 69 66 79 20 61 62 6f 75 74 20 79 79 67  purify about yyg
3086a 6f 74 6f 6d 69 6e 6f 72 20 62 65 69 6e 67 20 75  otominor being u
3086b 6e 69 6e 69 74 69 61 6c 69 7a 65 64 0a 20 20 2a  ninitialized.  *
3086c 2a 20 69 6e 20 73 6f 6d 65 20 63 61 73 65 73 20  * in some cases 
3086d 77 68 65 6e 20 69 74 20 69 73 20 63 6f 70 69 65  when it is copie
3086e 64 20 69 6e 74 6f 20 74 68 65 20 73 74 61 63 6b  d into the stack
3086f 20 61 66 74 65 72 20 74 68 65 20 66 6f 6c 6c 6f   after the follo
30870 77 69 6e 67 0a 20 20 2a 2a 20 73 77 69 74 63 68  wing.  ** switch
30871 2e 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 20 69  .  yygotominor i
30872 73 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20  s uninitialized 
30873 77 68 65 6e 20 61 20 72 75 6c 65 20 72 65 64 75  when a rule redu
30874 63 65 73 20 74 68 61 74 20 64 6f 65 73 0a 20 20  ces that does.  
30875 2a 2a 20 6e 6f 74 20 73 65 74 20 74 68 65 20 76  ** not set the v
30876 61 6c 75 65 20 6f 66 20 69 74 73 20 6c 65 66 74  alue of its left
30877 2d 68 61 6e 64 20 73 69 64 65 20 6e 6f 6e 74 65  -hand side nonte
30878 72 6d 69 6e 61 6c 2e 20 20 4c 65 61 76 69 6e 67  rminal.  Leaving
30879 20 74 68 65 0a 20 20 2a 2a 20 76 61 6c 75 65 20   the.  ** value 
3087a 6f 66 20 74 68 65 20 6e 6f 6e 74 65 72 6d 69 6e  of the nontermin
3087b 61 6c 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64  al uninitialized
3087c 20 69 73 20 75 74 74 65 72 6c 79 20 68 61 72 6d   is utterly harm
3087d 6c 65 73 73 20 61 73 20 6c 6f 6e 67 0a 20 20 2a  less as long.  *
3087e 2a 20 61 73 20 74 68 65 20 76 61 6c 75 65 20 69  * as the value i
3087f 73 20 6e 65 76 65 72 20 75 73 65 64 2e 20 20 53  s never used.  S
30880 6f 20 72 65 61 6c 6c 79 20 74 68 65 20 6f 6e 6c  o really the onl
30881 79 20 74 68 69 6e 67 20 74 68 69 73 20 63 6f 64  y thing this cod
30882 65 0a 20 20 2a 2a 20 61 63 63 6f 6d 70 6c 69 73  e.  ** accomplis
30883 68 65 73 20 69 73 20 74 6f 20 71 75 69 65 74 65  hes is to quiete
30884 6e 20 70 75 72 69 66 79 2e 20 20 0a 20 20 2a 2a  n purify.  .  **
30885 0a 20 20 2a 2a 20 32 30 30 37 2d 30 31 2d 31 36  .  ** 2007-01-16
30886 3a 20 20 54 68 65 20 77 69 72 65 73 68 61 72 6b  :  The wireshark
30887 20 70 72 6f 6a 65 63 74 20 28 77 77 77 2e 77 69   project (www.wi
30888 72 65 73 68 61 72 6b 2e 6f 72 67 29 20 72 65 70  reshark.org) rep
30889 6f 72 74 73 20 74 68 61 74 0a 20 20 2a 2a 20 77  orts that.  ** w
3088a 69 74 68 6f 75 74 20 74 68 69 73 20 63 6f 64 65  ithout this code
3088b 2c 20 74 68 65 69 72 20 70 61 72 73 65 72 20 73  , their parser s
3088c 65 67 66 61 75 6c 74 73 2e 20 20 49 27 6d 20 6e  egfaults.  I'm n
3088d 6f 74 20 73 75 72 65 20 77 68 61 74 20 74 68 65  ot sure what the
3088e 72 65 0a 20 20 2a 2a 20 70 61 72 73 65 72 20 69  re.  ** parser i
3088f 73 20 64 6f 69 6e 67 20 74 6f 20 6d 61 6b 65 20  s doing to make 
30890 74 68 69 73 20 68 61 70 70 65 6e 2e 20 20 54 68  this happen.  Th
30891 69 73 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64  is is the second
30892 20 62 75 67 20 72 65 70 6f 72 74 0a 20 20 2a 2a   bug report.  **
30893 20 66 72 6f 6d 20 77 69 72 65 73 68 61 72 6b 20   from wireshark 
30894 74 68 69 73 20 77 65 65 6b 2e 20 20 43 6c 65 61  this week.  Clea
30895 72 6c 79 20 74 68 65 79 20 61 72 65 20 73 74 72  rly they are str
30896 65 73 73 69 6e 67 20 4c 65 6d 6f 6e 20 69 6e 20  essing Lemon in 
30897 77 61 79 73 0a 20 20 2a 2a 20 74 68 61 74 20 69  ways.  ** that i
30898 74 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 70  t has not been p
30899 72 65 76 69 6f 75 73 6c 79 20 73 74 72 65 73 73  reviously stress
3089a 65 64 2e 2e 2e 20 20 28 53 51 4c 69 74 65 20 74  ed...  (SQLite t
3089b 69 63 6b 65 74 20 23 32 31 37 32 29 0a 20 20 2a  icket #2172).  *
3089c 2f 0a 20 20 2f 2a 6d 65 6d 73 65 74 28 26 79 79  /.  /*memset(&yy
3089d 67 6f 74 6f 6d 69 6e 6f 72 2c 20 30 2c 20 73 69  gotominor, 0, si
3089e 7a 65 6f 66 28 79 79 67 6f 74 6f 6d 69 6e 6f 72  zeof(yygotominor
3089f 29 29 3b 2a 2f 0a 20 20 79 79 67 6f 74 6f 6d 69  ));*/.  yygotomi
308a0 6e 6f 72 20 3d 20 79 79 7a 65 72 6f 6d 69 6e 6f  nor = yyzeromino
308a1 72 3b 0a 0a 0a 20 20 73 77 69 74 63 68 28 20 79  r;...  switch( y
308a2 79 72 75 6c 65 6e 6f 20 29 7b 0a 20 20 2f 2a 20  yruleno ){.  /* 
308a3 42 65 67 69 6e 6e 69 6e 67 20 68 65 72 65 20 61  Beginning here a
308a4 72 65 20 74 68 65 20 72 65 64 75 63 74 69 6f 6e  re the reduction
308a5 20 63 61 73 65 73 2e 20 20 41 20 74 79 70 69 63   cases.  A typic
308a6 61 6c 20 65 78 61 6d 70 6c 65 0a 20 20 2a 2a 20  al example.  ** 
308a7 66 6f 6c 6c 6f 77 73 3a 0a 20 20 2a 2a 20 20 20  follows:.  **   
308a8 63 61 73 65 20 30 3a 0a 20 20 2a 2a 20 20 23 6c  case 0:.  **  #l
308a9 69 6e 65 20 3c 6c 69 6e 65 6e 6f 3e 20 3c 67 72  ine <lineno> <gr
308aa 61 6d 6d 61 72 66 69 6c 65 3e 0a 20 20 2a 2a 20  ammarfile>.  ** 
308ab 20 20 20 20 7b 20 2e 2e 2e 20 7d 20 20 20 20 20      { ... }     
308ac 20 20 20 20 20 20 2f 2f 20 55 73 65 72 20 73 75        // User su
308ad 70 70 6c 69 65 64 20 63 6f 64 65 0a 20 20 2a 2a  pplied code.  **
308ae 20 20 23 6c 69 6e 65 20 3c 6c 69 6e 65 6e 6f 3e    #line <lineno>
308af 20 3c 74 68 69 73 66 69 6c 65 3e 0a 20 20 2a 2a   <thisfile>.  **
308b0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 2a 2f       break;.  */
308b1 0a 20 20 20 20 20 20 63 61 73 65 20 35 3a 20 2f  .      case 5: /
308b2 2a 20 65 78 70 6c 61 69 6e 20 3a 3a 3d 20 2a 2f  * explain ::= */
308b3 0a 7b 20 73 71 6c 69 74 65 33 42 65 67 69 6e 50  .{ sqlite3BeginP
308b4 61 72 73 65 28 70 50 61 72 73 65 2c 20 30 29 3b  arse(pParse, 0);
308b5 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
308b6 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36 3a 20  ;.      case 6: 
308b7 2f 2a 20 65 78 70 6c 61 69 6e 20 3a 3a 3d 20 45  /* explain ::= E
308b8 58 50 4c 41 49 4e 20 2a 2f 0a 7b 20 73 71 6c 69  XPLAIN */.{ sqli
308b9 74 65 33 42 65 67 69 6e 50 61 72 73 65 28 70 50  te3BeginParse(pP
308ba 61 72 73 65 2c 20 31 29 3b 20 7d 0a 20 20 20 20  arse, 1); }.    
308bb 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
308bc 20 63 61 73 65 20 37 3a 20 2f 2a 20 65 78 70 6c   case 7: /* expl
308bd 61 69 6e 20 3a 3a 3d 20 45 58 50 4c 41 49 4e 20  ain ::= EXPLAIN 
308be 51 55 45 52 59 20 50 4c 41 4e 20 2a 2f 0a 7b 20  QUERY PLAN */.{ 
308bf 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61 72 73  sqlite3BeginPars
308c0 65 28 70 50 61 72 73 65 2c 20 32 29 3b 20 7d 0a  e(pParse, 2); }.
308c1 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
308c2 20 20 20 20 20 63 61 73 65 20 38 3a 20 2f 2a 20       case 8: /* 
308c3 63 6d 64 78 20 3a 3a 3d 20 63 6d 64 20 2a 2f 0a  cmdx ::= cmd */.
308c4 7b 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68 43  { sqlite3FinishC
308c5 6f 64 69 6e 67 28 70 50 61 72 73 65 29 3b 20 7d  oding(pParse); }
308c6 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
308c7 20 20 20 20 20 20 63 61 73 65 20 39 3a 20 2f 2a        case 9: /*
308c8 20 63 6d 64 20 3a 3a 3d 20 42 45 47 49 4e 20 74   cmd ::= BEGIN t
308c9 72 61 6e 73 74 79 70 65 20 74 72 61 6e 73 5f 6f  ranstype trans_o
308ca 70 74 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 42 65  pt */.{sqlite3Be
308cb 67 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 28 70  ginTransaction(p
308cc 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 31 5d  Parse, yymsp[-1]
308cd 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 29 3b 7d 0a  .minor.yy328);}.
308ce 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
308cf 20 20 20 20 20 63 61 73 65 20 31 33 3a 20 2f 2a       case 13: /*
308d0 20 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d 20 2a   transtype ::= *
308d1 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
308d2 79 33 32 38 20 3d 20 54 4b 5f 44 45 46 45 52 52  y328 = TK_DEFERR
308d3 45 44 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  ED;}.        bre
308d4 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
308d5 34 3a 20 2f 2a 20 74 72 61 6e 73 74 79 70 65 20  4: /* transtype 
308d6 3a 3a 3d 20 44 45 46 45 52 52 45 44 20 2a 2f 0a  ::= DEFERRED */.
308d7 20 20 20 20 20 20 63 61 73 65 20 31 35 3a 20 2f        case 15: /
308d8 2a 20 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d 20  * transtype ::= 
308d9 49 4d 4d 45 44 49 41 54 45 20 2a 2f 20 79 79 74  IMMEDIATE */ yyt
308da 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
308db 3d 3d 31 35 29 3b 0a 20 20 20 20 20 20 63 61 73  ==15);.      cas
308dc 65 20 31 36 3a 20 2f 2a 20 74 72 61 6e 73 74 79  e 16: /* transty
308dd 70 65 20 3a 3a 3d 20 45 58 43 4c 55 53 49 56 45  pe ::= EXCLUSIVE
308de 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
308df 79 72 75 6c 65 6e 6f 3d 3d 31 36 29 3b 0a 20 20  yruleno==16);.  
308e0 20 20 20 20 63 61 73 65 20 31 31 34 3a 20 2f 2a      case 114: /*
308e1 20 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20   multiselect_op 
308e2 3a 3a 3d 20 55 4e 49 4f 4e 20 2a 2f 20 79 79 74  ::= UNION */ yyt
308e3 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
308e4 3d 3d 31 31 34 29 3b 0a 20 20 20 20 20 20 63 61  ==114);.      ca
308e5 73 65 20 31 31 36 3a 20 2f 2a 20 6d 75 6c 74 69  se 116: /* multi
308e6 73 65 6c 65 63 74 5f 6f 70 20 3a 3a 3d 20 45 58  select_op ::= EX
308e7 43 45 50 54 7c 49 4e 54 45 52 53 45 43 54 20 2a  CEPT|INTERSECT *
308e8 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
308e9 75 6c 65 6e 6f 3d 3d 31 31 36 29 3b 0a 7b 79 79  uleno==116);.{yy
308ea 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20  gotominor.yy328 
308eb 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 61 6a 6f 72  = yymsp[0].major
308ec 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
308ed 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 37 3a  ;.      case 17:
308ee 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 43 4f 4d 4d   /* cmd ::= COMM
308ef 49 54 20 74 72 61 6e 73 5f 6f 70 74 20 2a 2f 0a  IT trans_opt */.
308f0 20 20 20 20 20 20 63 61 73 65 20 31 38 3a 20 2f        case 18: /
308f1 2a 20 63 6d 64 20 3a 3a 3d 20 45 4e 44 20 74 72  * cmd ::= END tr
308f2 61 6e 73 5f 6f 70 74 20 2a 2f 20 79 79 74 65 73  ans_opt */ yytes
308f3 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
308f4 31 38 29 3b 0a 7b 73 71 6c 69 74 65 33 43 6f 6d  18);.{sqlite3Com
308f5 6d 69 74 54 72 61 6e 73 61 63 74 69 6f 6e 28 70  mitTransaction(p
308f6 50 61 72 73 65 29 3b 7d 0a 20 20 20 20 20 20 20  Parse);}.       
308f7 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
308f8 73 65 20 31 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a  se 19: /* cmd ::
308f9 3d 20 52 4f 4c 4c 42 41 43 4b 20 74 72 61 6e 73  = ROLLBACK trans
308fa 5f 6f 70 74 20 2a 2f 0a 7b 73 71 6c 69 74 65 33  _opt */.{sqlite3
308fb 52 6f 6c 6c 62 61 63 6b 54 72 61 6e 73 61 63 74  RollbackTransact
308fc 69 6f 6e 28 70 50 61 72 73 65 29 3b 7d 0a 20 20  ion(pParse);}.  
308fd 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
308fe 20 20 20 63 61 73 65 20 32 32 3a 20 2f 2a 20 63     case 22: /* c
308ff 6d 64 20 3a 3a 3d 20 53 41 56 45 50 4f 49 4e 54  md ::= SAVEPOINT
30900 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74   nm */.{.  sqlit
30901 65 33 53 61 76 65 70 6f 69 6e 74 28 70 50 61 72  e3Savepoint(pPar
30902 73 65 2c 20 53 41 56 45 50 4f 49 4e 54 5f 42 45  se, SAVEPOINT_BE
30903 47 49 4e 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  GIN, &yymsp[0].m
30904 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20  inor.yy0);.}.   
30905 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
30906 20 20 63 61 73 65 20 32 33 3a 20 2f 2a 20 63 6d    case 23: /* cm
30907 64 20 3a 3a 3d 20 52 45 4c 45 41 53 45 20 73 61  d ::= RELEASE sa
30908 76 65 70 6f 69 6e 74 5f 6f 70 74 20 6e 6d 20 2a  vepoint_opt nm *
30909 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 53 61 76  /.{.  sqlite3Sav
3090a 65 70 6f 69 6e 74 28 70 50 61 72 73 65 2c 20 53  epoint(pParse, S
3090b 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45  AVEPOINT_RELEASE
3090c 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  , &yymsp[0].mino
3090d 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20  r.yy0);.}.      
3090e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3090f 61 73 65 20 32 34 3a 20 2f 2a 20 63 6d 64 20 3a  ase 24: /* cmd :
30910 3a 3d 20 52 4f 4c 4c 42 41 43 4b 20 74 72 61 6e  := ROLLBACK tran
30911 73 5f 6f 70 74 20 54 4f 20 73 61 76 65 70 6f 69  s_opt TO savepoi
30912 6e 74 5f 6f 70 74 20 6e 6d 20 2a 2f 0a 7b 0a 20  nt_opt nm */.{. 
30913 20 73 71 6c 69 74 65 33 53 61 76 65 70 6f 69 6e   sqlite3Savepoin
30914 74 28 70 50 61 72 73 65 2c 20 53 41 56 45 50 4f  t(pParse, SAVEPO
30915 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 26 79  INT_ROLLBACK, &y
30916 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
30917 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  0);.}.        br
30918 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
30919 32 36 3a 20 2f 2a 20 63 72 65 61 74 65 5f 74 61  26: /* create_ta
3091a 62 6c 65 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77  ble ::= createkw
3091b 20 74 65 6d 70 20 54 41 42 4c 45 20 69 66 6e 6f   temp TABLE ifno
3091c 74 65 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20  texists nm dbnm 
3091d 2a 2f 0a 7b 0a 20 20 20 73 71 6c 69 74 65 33 53  */.{.   sqlite3S
3091e 74 61 72 74 54 61 62 6c 65 28 70 50 61 72 73 65  tartTable(pParse
3091f 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  ,&yymsp[-1].mino
30920 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e  r.yy0,&yymsp[0].
30921 6d 69 6e 6f 72 2e 79 79 30 2c 79 79 6d 73 70 5b  minor.yy0,yymsp[
30922 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 2c  -4].minor.yy328,
30923 30 2c 30 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  0,0,yymsp[-2].mi
30924 6e 6f 72 2e 79 79 33 32 38 29 3b 0a 7d 0a 20 20  nor.yy328);.}.  
30925 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
30926 20 20 20 63 61 73 65 20 32 37 3a 20 2f 2a 20 63     case 27: /* c
30927 72 65 61 74 65 6b 77 20 3a 3a 3d 20 43 52 45 41  reatekw ::= CREA
30928 54 45 20 2a 2f 0a 7b 0a 20 20 70 50 61 72 73 65  TE */.{.  pParse
30929 2d 3e 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  ->db->lookaside.
3092a 62 45 6e 61 62 6c 65 64 20 3d 20 30 3b 0a 20 20  bEnabled = 0;.  
3092b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 20  yygotominor.yy0 
3092c 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  = yymsp[0].minor
3092d 2e 79 79 30 3b 0a 7d 0a 20 20 20 20 20 20 20 20  .yy0;.}.        
3092e 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3092f 65 20 32 38 3a 20 2f 2a 20 69 66 6e 6f 74 65 78  e 28: /* ifnotex
30930 69 73 74 73 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20  ists ::= */.    
30931 20 20 63 61 73 65 20 33 31 3a 20 2f 2a 20 74 65    case 31: /* te
30932 6d 70 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74  mp ::= */ yytest
30933 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33  case(yyruleno==3
30934 31 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 37  1);.      case 7
30935 30 3a 20 2f 2a 20 61 75 74 6f 69 6e 63 20 3a 3a  0: /* autoinc ::
30936 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  = */ yytestcase(
30937 79 79 72 75 6c 65 6e 6f 3d 3d 37 30 29 3b 0a 20  yyruleno==70);. 
30938 20 20 20 20 20 63 61 73 65 20 38 34 3a 20 2f 2a       case 84: /*
30939 20 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70   init_deferred_p
3093a 72 65 64 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79  red_opt ::= */ y
3093b 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
3093c 6e 6f 3d 3d 38 34 29 3b 0a 20 20 20 20 20 20 63  no==84);.      c
3093d 61 73 65 20 38 36 3a 20 2f 2a 20 69 6e 69 74 5f  ase 86: /* init_
3093e 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70  deferred_pred_op
3093f 74 20 3a 3a 3d 20 49 4e 49 54 49 41 4c 4c 59 20  t ::= INITIALLY 
30940 49 4d 4d 45 44 49 41 54 45 20 2a 2f 20 79 79 74  IMMEDIATE */ yyt
30941 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
30942 3d 3d 38 36 29 3b 0a 20 20 20 20 20 20 63 61 73  ==86);.      cas
30943 65 20 39 37 3a 20 2f 2a 20 64 65 66 65 72 5f 73  e 97: /* defer_s
30944 75 62 63 6c 61 75 73 65 5f 6f 70 74 20 3a 3a 3d  ubclause_opt ::=
30945 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
30946 79 72 75 6c 65 6e 6f 3d 3d 39 37 29 3b 0a 20 20  yruleno==97);.  
30947 20 20 20 20 63 61 73 65 20 31 30 38 3a 20 2f 2a      case 108: /*
30948 20 69 66 65 78 69 73 74 73 20 3a 3a 3d 20 2a 2f   ifexists ::= */
30949 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3094a 6c 65 6e 6f 3d 3d 31 30 38 29 3b 0a 20 20 20 20  leno==108);.    
3094b 20 20 63 61 73 65 20 31 31 39 3a 20 2f 2a 20 64    case 119: /* d
3094c 69 73 74 69 6e 63 74 20 3a 3a 3d 20 41 4c 4c 20  istinct ::= ALL 
3094d 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
3094e 72 75 6c 65 6e 6f 3d 3d 31 31 39 29 3b 0a 20 20  ruleno==119);.  
3094f 20 20 20 20 63 61 73 65 20 31 32 30 3a 20 2f 2a      case 120: /*
30950 20 64 69 73 74 69 6e 63 74 20 3a 3a 3d 20 2a 2f   distinct ::= */
30951 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
30952 6c 65 6e 6f 3d 3d 31 32 30 29 3b 0a 20 20 20 20  leno==120);.    
30953 20 20 63 61 73 65 20 32 32 32 3a 20 2f 2a 20 62    case 222: /* b
30954 65 74 77 65 65 6e 5f 6f 70 20 3a 3a 3d 20 42 45  etween_op ::= BE
30955 54 57 45 45 4e 20 2a 2f 20 79 79 74 65 73 74 63  TWEEN */ yytestc
30956 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 32  ase(yyruleno==22
30957 32 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  2);.      case 2
30958 32 35 3a 20 2f 2a 20 69 6e 5f 6f 70 20 3a 3a 3d  25: /* in_op ::=
30959 20 49 4e 20 2a 2f 20 79 79 74 65 73 74 63 61 73   IN */ yytestcas
3095a 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 32 35 29  e(yyruleno==225)
3095b 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  ;.{yygotominor.y
3095c 79 33 32 38 20 3d 20 30 3b 7d 0a 20 20 20 20 20  y328 = 0;}.     
3095d 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3095e 63 61 73 65 20 32 39 3a 20 2f 2a 20 69 66 6e 6f  case 29: /* ifno
3095f 74 65 78 69 73 74 73 20 3a 3a 3d 20 49 46 20 4e  texists ::= IF N
30960 4f 54 20 45 58 49 53 54 53 20 2a 2f 0a 20 20 20  OT EXISTS */.   
30961 20 20 20 63 61 73 65 20 33 30 3a 20 2f 2a 20 74     case 30: /* t
30962 65 6d 70 20 3a 3a 3d 20 54 45 4d 50 20 2a 2f 20  emp ::= TEMP */ 
30963 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
30964 65 6e 6f 3d 3d 33 30 29 3b 0a 20 20 20 20 20 20  eno==30);.      
30965 63 61 73 65 20 37 31 3a 20 2f 2a 20 61 75 74 6f  case 71: /* auto
30966 69 6e 63 20 3a 3a 3d 20 41 55 54 4f 49 4e 43 52  inc ::= AUTOINCR
30967 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
30968 79 72 75 6c 65 6e 6f 3d 3d 37 31 29 3b 0a 20 20  yruleno==71);.  
30969 20 20 20 20 63 61 73 65 20 38 35 3a 20 2f 2a 20      case 85: /* 
3096a 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72  init_deferred_pr
3096b 65 64 5f 6f 70 74 20 3a 3a 3d 20 49 4e 49 54 49  ed_opt ::= INITI
3096c 41 4c 4c 59 20 44 45 46 45 52 52 45 44 20 2a 2f  ALLY DEFERRED */
3096d 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3096e 6c 65 6e 6f 3d 3d 38 35 29 3b 0a 20 20 20 20 20  leno==85);.     
3096f 20 63 61 73 65 20 31 30 37 3a 20 2f 2a 20 69 66   case 107: /* if
30970 65 78 69 73 74 73 20 3a 3a 3d 20 49 46 20 45 58  exists ::= IF EX
30971 49 53 54 53 20 2a 2f 20 79 79 74 65 73 74 63 61  ISTS */ yytestca
30972 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 30 37  se(yyruleno==107
30973 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 31  );.      case 11
30974 38 3a 20 2f 2a 20 64 69 73 74 69 6e 63 74 20 3a  8: /* distinct :
30975 3a 3d 20 44 49 53 54 49 4e 43 54 20 2a 2f 20 79  := DISTINCT */ y
30976 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
30977 6e 6f 3d 3d 31 31 38 29 3b 0a 20 20 20 20 20 20  no==118);.      
30978 63 61 73 65 20 32 32 33 3a 20 2f 2a 20 62 65 74  case 223: /* bet
30979 77 65 65 6e 5f 6f 70 20 3a 3a 3d 20 4e 4f 54 20  ween_op ::= NOT 
3097a 42 45 54 57 45 45 4e 20 2a 2f 20 79 79 74 65 73  BETWEEN */ yytes
3097b 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
3097c 32 32 33 29 3b 0a 20 20 20 20 20 20 63 61 73 65  223);.      case
3097d 20 32 32 36 3a 20 2f 2a 20 69 6e 5f 6f 70 20 3a   226: /* in_op :
3097e 3a 3d 20 4e 4f 54 20 49 4e 20 2a 2f 20 79 79 74  := NOT IN */ yyt
3097f 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
30980 3d 3d 32 32 36 29 3b 0a 7b 79 79 67 6f 74 6f 6d  ==226);.{yygotom
30981 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 31 3b 7d  inor.yy328 = 1;}
30982 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
30983 20 20 20 20 20 20 63 61 73 65 20 33 32 3a 20 2f        case 32: /
30984 2a 20 63 72 65 61 74 65 5f 74 61 62 6c 65 5f 61  * create_table_a
30985 72 67 73 20 3a 3a 3d 20 4c 50 20 63 6f 6c 75 6d  rgs ::= LP colum
30986 6e 6c 69 73 74 20 63 6f 6e 73 6c 69 73 74 5f 6f  nlist conslist_o
30987 70 74 20 52 50 20 2a 2f 0a 7b 0a 20 20 73 71 6c  pt RP */.{.  sql
30988 69 74 65 33 45 6e 64 54 61 62 6c 65 28 70 50 61  ite3EndTable(pPa
30989 72 73 65 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d  rse,&yymsp[-1].m
3098a 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b  inor.yy0,&yymsp[
3098b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 29 3b  0].minor.yy0,0);
3098c 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
3098d 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 33 3a  ;.      case 33:
3098e 20 2f 2a 20 63 72 65 61 74 65 5f 74 61 62 6c 65   /* create_table
3098f 5f 61 72 67 73 20 3a 3a 3d 20 41 53 20 73 65 6c  _args ::= AS sel
30990 65 63 74 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74  ect */.{.  sqlit
30991 65 33 45 6e 64 54 61 62 6c 65 28 70 50 61 72 73  e3EndTable(pPars
30992 65 2c 30 2c 30 2c 79 79 6d 73 70 5b 30 5d 2e 6d  e,0,0,yymsp[0].m
30993 69 6e 6f 72 2e 79 79 33 29 3b 0a 20 20 73 71 6c  inor.yy3);.  sql
30994 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
30995 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d  (pParse->db, yym
30996 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 29  sp[0].minor.yy3)
30997 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
30998 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 36  k;.      case 36
30999 3a 20 2f 2a 20 63 6f 6c 75 6d 6e 20 3a 3a 3d 20  : /* column ::= 
3099a 63 6f 6c 75 6d 6e 69 64 20 74 79 70 65 20 63 61  columnid type ca
3099b 72 67 6c 69 73 74 20 2a 2f 0a 7b 0a 20 20 79 79  rglist */.{.  yy
3099c 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20  gotominor.yy0.z 
3099d 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  = yymsp[-2].mino
3099e 72 2e 79 79 30 2e 7a 3b 0a 20 20 79 79 67 6f 74  r.yy0.z;.  yygot
3099f 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20 28  ominor.yy0.n = (
309a0 69 6e 74 29 28 70 50 61 72 73 65 2d 3e 73 4c 61  int)(pParse->sLa
309a1 73 74 54 6f 6b 65 6e 2e 7a 2d 79 79 6d 73 70 5b  stToken.z-yymsp[
309a2 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 29  -2].minor.yy0.z)
309a3 20 2b 20 70 50 61 72 73 65 2d 3e 73 4c 61 73 74   + pParse->sLast
309a4 54 6f 6b 65 6e 2e 6e 3b 0a 7d 0a 20 20 20 20 20  Token.n;.}.     
309a5 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
309a6 63 61 73 65 20 33 37 3a 20 2f 2a 20 63 6f 6c 75  case 37: /* colu
309a7 6d 6e 69 64 20 3a 3a 3d 20 6e 6d 20 2a 2f 0a 7b  mnid ::= nm */.{
309a8 0a 20 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c  .  sqlite3AddCol
309a9 75 6d 6e 28 70 50 61 72 73 65 2c 26 79 79 6d 73  umn(pParse,&yyms
309aa 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
309ab 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
309ac 79 30 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  y0 = yymsp[0].mi
309ad 6e 6f 72 2e 79 79 30 3b 0a 7d 0a 20 20 20 20 20  nor.yy0;.}.     
309ae 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
309af 63 61 73 65 20 33 38 3a 20 2f 2a 20 69 64 20 3a  case 38: /* id :
309b0 3a 3d 20 49 44 20 2a 2f 0a 20 20 20 20 20 20 63  := ID */.      c
309b1 61 73 65 20 33 39 3a 20 2f 2a 20 69 64 20 3a 3a  ase 39: /* id ::
309b2 3d 20 49 4e 44 45 58 45 44 20 2a 2f 20 79 79 74  = INDEXED */ yyt
309b3 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
309b4 3d 3d 33 39 29 3b 0a 20 20 20 20 20 20 63 61 73  ==39);.      cas
309b5 65 20 34 30 3a 20 2f 2a 20 69 64 73 20 3a 3a 3d  e 40: /* ids ::=
309b6 20 49 44 7c 53 54 52 49 4e 47 20 2a 2f 20 79 79   ID|STRING */ yy
309b7 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
309b8 6f 3d 3d 34 30 29 3b 0a 20 20 20 20 20 20 63 61  o==40);.      ca
309b9 73 65 20 34 31 3a 20 2f 2a 20 6e 6d 20 3a 3a 3d  se 41: /* nm ::=
309ba 20 69 64 20 2a 2f 20 79 79 74 65 73 74 63 61 73   id */ yytestcas
309bb 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 34 31 29 3b  e(yyruleno==41);
309bc 0a 20 20 20 20 20 20 63 61 73 65 20 34 32 3a 20  .      case 42: 
309bd 2f 2a 20 6e 6d 20 3a 3a 3d 20 53 54 52 49 4e 47  /* nm ::= STRING
309be 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
309bf 79 72 75 6c 65 6e 6f 3d 3d 34 32 29 3b 0a 20 20  yruleno==42);.  
309c0 20 20 20 20 63 61 73 65 20 34 33 3a 20 2f 2a 20      case 43: /* 
309c1 6e 6d 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 2a  nm ::= JOIN_KW *
309c2 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
309c3 75 6c 65 6e 6f 3d 3d 34 33 29 3b 0a 20 20 20 20  uleno==43);.    
309c4 20 20 63 61 73 65 20 34 36 3a 20 2f 2a 20 74 79    case 46: /* ty
309c5 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70 65  petoken ::= type
309c6 6e 61 6d 65 20 2a 2f 20 79 79 74 65 73 74 63 61  name */ yytestca
309c7 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 34 36 29  se(yyruleno==46)
309c8 3b 0a 20 20 20 20 20 20 63 61 73 65 20 34 39 3a  ;.      case 49:
309c9 20 2f 2a 20 74 79 70 65 6e 61 6d 65 20 3a 3a 3d   /* typename ::=
309ca 20 69 64 73 20 2a 2f 20 79 79 74 65 73 74 63 61   ids */ yytestca
309cb 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 34 39 29  se(yyruleno==49)
309cc 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 36  ;.      case 126
309cd 3a 20 2f 2a 20 61 73 20 3a 3a 3d 20 41 53 20 6e  : /* as ::= AS n
309ce 6d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  m */ yytestcase(
309cf 79 79 72 75 6c 65 6e 6f 3d 3d 31 32 36 29 3b 0a  yyruleno==126);.
309d0 20 20 20 20 20 20 63 61 73 65 20 31 32 37 3a 20        case 127: 
309d1 2f 2a 20 61 73 20 3a 3a 3d 20 69 64 73 20 2a 2f  /* as ::= ids */
309d2 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
309d3 6c 65 6e 6f 3d 3d 31 32 37 29 3b 0a 20 20 20 20  leno==127);.    
309d4 20 20 63 61 73 65 20 31 33 37 3a 20 2f 2a 20 64    case 137: /* d
309d5 62 6e 6d 20 3a 3a 3d 20 44 4f 54 20 6e 6d 20 2a  bnm ::= DOT nm *
309d6 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
309d7 75 6c 65 6e 6f 3d 3d 31 33 37 29 3b 0a 20 20 20  uleno==137);.   
309d8 20 20 20 63 61 73 65 20 31 34 36 3a 20 2f 2a 20     case 146: /* 
309d9 69 6e 64 65 78 65 64 5f 6f 70 74 20 3a 3a 3d 20  indexed_opt ::= 
309da 49 4e 44 45 58 45 44 20 42 59 20 6e 6d 20 2a 2f  INDEXED BY nm */
309db 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
309dc 6c 65 6e 6f 3d 3d 31 34 36 29 3b 0a 20 20 20 20  leno==146);.    
309dd 20 20 63 61 73 65 20 32 35 31 3a 20 2f 2a 20 63    case 251: /* c
309de 6f 6c 6c 61 74 65 20 3a 3a 3d 20 43 4f 4c 4c 41  ollate ::= COLLA
309df 54 45 20 69 64 73 20 2a 2f 20 79 79 74 65 73 74  TE ids */ yytest
309e0 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
309e1 35 31 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  51);.      case 
309e2 32 36 30 3a 20 2f 2a 20 6e 6d 6e 75 6d 20 3a 3a  260: /* nmnum ::
309e3 3d 20 70 6c 75 73 5f 6e 75 6d 20 2a 2f 20 79 79  = plus_num */ yy
309e4 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
309e5 6f 3d 3d 32 36 30 29 3b 0a 20 20 20 20 20 20 63  o==260);.      c
309e6 61 73 65 20 32 36 31 3a 20 2f 2a 20 6e 6d 6e 75  ase 261: /* nmnu
309e7 6d 20 3a 3a 3d 20 6e 6d 20 2a 2f 20 79 79 74 65  m ::= nm */ yyte
309e8 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
309e9 3d 32 36 31 29 3b 0a 20 20 20 20 20 20 63 61 73  =261);.      cas
309ea 65 20 32 36 32 3a 20 2f 2a 20 6e 6d 6e 75 6d 20  e 262: /* nmnum 
309eb 3a 3a 3d 20 4f 4e 20 2a 2f 20 79 79 74 65 73 74  ::= ON */ yytest
309ec 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
309ed 36 32 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  62);.      case 
309ee 32 36 33 3a 20 2f 2a 20 6e 6d 6e 75 6d 20 3a 3a  263: /* nmnum ::
309ef 3d 20 44 45 4c 45 54 45 20 2a 2f 20 79 79 74 65  = DELETE */ yyte
309f0 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
309f1 3d 32 36 33 29 3b 0a 20 20 20 20 20 20 63 61 73  =263);.      cas
309f2 65 20 32 36 34 3a 20 2f 2a 20 6e 6d 6e 75 6d 20  e 264: /* nmnum 
309f3 3a 3a 3d 20 44 45 46 41 55 4c 54 20 2a 2f 20 79  ::= DEFAULT */ y
309f4 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
309f5 6e 6f 3d 3d 32 36 34 29 3b 0a 20 20 20 20 20 20  no==264);.      
309f6 63 61 73 65 20 32 36 35 3a 20 2f 2a 20 70 6c 75  case 265: /* plu
309f7 73 5f 6e 75 6d 20 3a 3a 3d 20 70 6c 75 73 5f 6f  s_num ::= plus_o
309f8 70 74 20 6e 75 6d 62 65 72 20 2a 2f 20 79 79 74  pt number */ yyt
309f9 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
309fa 3d 3d 32 36 35 29 3b 0a 20 20 20 20 20 20 63 61  ==265);.      ca
309fb 73 65 20 32 36 36 3a 20 2f 2a 20 6d 69 6e 75 73  se 266: /* minus
309fc 5f 6e 75 6d 20 3a 3a 3d 20 4d 49 4e 55 53 20 6e  _num ::= MINUS n
309fd 75 6d 62 65 72 20 2a 2f 20 79 79 74 65 73 74 63  umber */ yytestc
309fe 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 36  ase(yyruleno==26
309ff 36 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  6);.      case 2
30a00 36 37 3a 20 2f 2a 20 6e 75 6d 62 65 72 20 3a 3a  67: /* number ::
30a01 3d 20 49 4e 54 45 47 45 52 7c 46 4c 4f 41 54 20  = INTEGER|FLOAT 
30a02 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
30a03 72 75 6c 65 6e 6f 3d 3d 32 36 37 29 3b 0a 20 20  ruleno==267);.  
30a04 20 20 20 20 63 61 73 65 20 32 38 35 3a 20 2f 2a      case 285: /*
30a05 20 74 72 6e 6d 20 3a 3a 3d 20 6e 6d 20 2a 2f 20   trnm ::= nm */ 
30a06 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
30a07 65 6e 6f 3d 3d 32 38 35 29 3b 0a 7b 79 79 67 6f  eno==285);.{yygo
30a08 74 6f 6d 69 6e 6f 72 2e 79 79 30 20 3d 20 79 79  tominor.yy0 = yy
30a09 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
30a0a 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
30a0b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 34 35 3a  ;.      case 45:
30a0c 20 2f 2a 20 74 79 70 65 20 3a 3a 3d 20 74 79 70   /* type ::= typ
30a0d 65 74 6f 6b 65 6e 20 2a 2f 0a 7b 73 71 6c 69 74  etoken */.{sqlit
30a0e 65 33 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28  e3AddColumnType(
30a0f 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 30 5d  pParse,&yymsp[0]
30a10 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20  .minor.yy0);}.  
30a11 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
30a12 20 20 20 63 61 73 65 20 34 37 3a 20 2f 2a 20 74     case 47: /* t
30a13 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70  ypetoken ::= typ
30a14 65 6e 61 6d 65 20 4c 50 20 73 69 67 6e 65 64 20  ename LP signed 
30a15 52 50 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f  RP */.{.  yygoto
30a16 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20 3d 20 79 79  minor.yy0.z = yy
30a17 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
30a18 30 2e 7a 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  0.z;.  yygotomin
30a19 6f 72 2e 79 79 30 2e 6e 20 3d 20 28 69 6e 74 29  or.yy0.n = (int)
30a1a 28 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  (&yymsp[0].minor
30a1b 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e  .yy0.z[yymsp[0].
30a1c 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 20 2d 20 79  minor.yy0.n] - y
30a1d 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
30a1e 79 30 2e 7a 29 3b 0a 7d 0a 20 20 20 20 20 20 20  y0.z);.}.       
30a1f 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
30a20 73 65 20 34 38 3a 20 2f 2a 20 74 79 70 65 74 6f  se 48: /* typeto
30a21 6b 65 6e 20 3a 3a 3d 20 74 79 70 65 6e 61 6d 65  ken ::= typename
30a22 20 4c 50 20 73 69 67 6e 65 64 20 43 4f 4d 4d 41   LP signed COMMA
30a23 20 73 69 67 6e 65 64 20 52 50 20 2a 2f 0a 7b 0a   signed RP */.{.
30a24 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
30a25 30 2e 7a 20 3d 20 79 79 6d 73 70 5b 2d 35 5d 2e  0.z = yymsp[-5].
30a26 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20 79  minor.yy0.z;.  y
30a27 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e  ygotominor.yy0.n
30a28 20 3d 20 28 69 6e 74 29 28 26 79 79 6d 73 70 5b   = (int)(&yymsp[
30a29 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79  0].minor.yy0.z[y
30a2a 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
30a2b 30 2e 6e 5d 20 2d 20 79 79 6d 73 70 5b 2d 35 5d  0.n] - yymsp[-5]
30a2c 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 29 3b 0a 7d  .minor.yy0.z);.}
30a2d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
30a2e 20 20 20 20 20 20 63 61 73 65 20 35 30 3a 20 2f        case 50: /
30a2f 2a 20 74 79 70 65 6e 61 6d 65 20 3a 3a 3d 20 74  * typename ::= t
30a30 79 70 65 6e 61 6d 65 20 69 64 73 20 2a 2f 0a 7b  ypename ids */.{
30a31 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e  yygotominor.yy0.
30a32 7a 3d 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  z=yymsp[-1].mino
30a33 72 2e 79 79 30 2e 7a 3b 20 79 79 67 6f 74 6f 6d  r.yy0.z; yygotom
30a34 69 6e 6f 72 2e 79 79 30 2e 6e 3d 79 79 6d 73 70  inor.yy0.n=yymsp
30a35 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 2b  [0].minor.yy0.n+
30a36 28 69 6e 74 29 28 79 79 6d 73 70 5b 30 5d 2e 6d  (int)(yymsp[0].m
30a37 69 6e 6f 72 2e 79 79 30 2e 7a 2d 79 79 6d 73 70  inor.yy0.z-yymsp
30a38 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  [-1].minor.yy0.z
30a39 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
30a3a 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 35 37  k;.      case 57
30a3b 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 44  : /* ccons ::= D
30a3c 45 46 41 55 4c 54 20 74 65 72 6d 20 2a 2f 0a 20  EFAULT term */. 
30a3d 20 20 20 20 20 63 61 73 65 20 35 39 3a 20 2f 2a       case 59: /*
30a3e 20 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55   ccons ::= DEFAU
30a3f 4c 54 20 50 4c 55 53 20 74 65 72 6d 20 2a 2f 20  LT PLUS term */ 
30a40 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
30a41 65 6e 6f 3d 3d 35 39 29 3b 0a 7b 73 71 6c 69 74  eno==59);.{sqlit
30a42 65 33 41 64 64 44 65 66 61 75 6c 74 56 61 6c 75  e3AddDefaultValu
30a43 65 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b  e(pParse,&yymsp[
30a44 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 29 3b  0].minor.yy346);
30a45 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
30a46 0a 20 20 20 20 20 20 63 61 73 65 20 35 38 3a 20  .      case 58: 
30a47 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46  /* ccons ::= DEF
30a48 41 55 4c 54 20 4c 50 20 65 78 70 72 20 52 50 20  AULT LP expr RP 
30a49 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64 64 44 65  */.{sqlite3AddDe
30a4a 66 61 75 6c 74 56 61 6c 75 65 28 70 50 61 72 73  faultValue(pPars
30a4b 65 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  e,&yymsp[-1].min
30a4c 6f 72 2e 79 79 33 34 36 29 3b 7d 0a 20 20 20 20  or.yy346);}.    
30a4d 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
30a4e 20 63 61 73 65 20 36 30 3a 20 2f 2a 20 63 63 6f   case 60: /* cco
30a4f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 4d  ns ::= DEFAULT M
30a50 49 4e 55 53 20 74 65 72 6d 20 2a 2f 0a 7b 0a 20  INUS term */.{. 
30a51 20 45 78 70 72 53 70 61 6e 20 76 3b 0a 20 20 76   ExprSpan v;.  v
30a52 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33  .pExpr = sqlite3
30a53 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
30a54 5f 55 4d 49 4e 55 53 2c 20 79 79 6d 73 70 5b 30  _UMINUS, yymsp[0
30a55 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  ].minor.yy346.pE
30a56 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 76 2e  xpr, 0, 0);.  v.
30a57 7a 53 74 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d  zStart = yymsp[-
30a58 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a  1].minor.yy0.z;.
30a59 20 20 76 2e 7a 45 6e 64 20 3d 20 79 79 6d 73 70    v.zEnd = yymsp
30a5a 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  [0].minor.yy346.
30a5b 7a 45 6e 64 3b 0a 20 20 73 71 6c 69 74 65 33 41  zEnd;.  sqlite3A
30a5c 64 64 44 65 66 61 75 6c 74 56 61 6c 75 65 28 70  ddDefaultValue(p
30a5d 50 61 72 73 65 2c 26 76 29 3b 0a 7d 0a 20 20 20  Parse,&v);.}.   
30a5e 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
30a5f 20 20 63 61 73 65 20 36 31 3a 20 2f 2a 20 63 63    case 61: /* cc
30a60 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20  ons ::= DEFAULT 
30a61 69 64 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 53 70  id */.{.  ExprSp
30a62 61 6e 20 76 3b 0a 20 20 73 70 61 6e 45 78 70 72  an v;.  spanExpr
30a63 28 26 76 2c 20 70 50 61 72 73 65 2c 20 54 4b 5f  (&v, pParse, TK_
30a64 53 54 52 49 4e 47 2c 20 26 79 79 6d 73 70 5b 30  STRING, &yymsp[0
30a65 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20  ].minor.yy0);.  
30a66 73 71 6c 69 74 65 33 41 64 64 44 65 66 61 75 6c  sqlite3AddDefaul
30a67 74 56 61 6c 75 65 28 70 50 61 72 73 65 2c 26 76  tValue(pParse,&v
30a68 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
30a69 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36  ak;.      case 6
30a6a 33 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20  3: /* ccons ::= 
30a6b 4e 4f 54 20 4e 55 4c 4c 20 6f 6e 63 6f 6e 66 20  NOT NULL onconf 
30a6c 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64 64 4e 6f  */.{sqlite3AddNo
30a6d 74 4e 75 6c 6c 28 70 50 61 72 73 65 2c 20 79 79  tNull(pParse, yy
30a6e 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
30a6f 32 38 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  28);}.        br
30a70 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
30a71 36 34 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d  64: /* ccons ::=
30a72 20 50 52 49 4d 41 52 59 20 4b 45 59 20 73 6f 72   PRIMARY KEY sor
30a73 74 6f 72 64 65 72 20 6f 6e 63 6f 6e 66 20 61 75  torder onconf au
30a74 74 6f 69 6e 63 20 2a 2f 0a 7b 73 71 6c 69 74 65  toinc */.{sqlite
30a75 33 41 64 64 50 72 69 6d 61 72 79 4b 65 79 28 70  3AddPrimaryKey(p
30a76 50 61 72 73 65 2c 30 2c 79 79 6d 73 70 5b 2d 31  Parse,0,yymsp[-1
30a77 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 2c 79 79  ].minor.yy328,yy
30a78 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
30a79 32 38 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  28,yymsp[-2].min
30a7a 6f 72 2e 79 79 33 32 38 29 3b 7d 0a 20 20 20 20  or.yy328);}.    
30a7b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
30a7c 20 63 61 73 65 20 36 35 3a 20 2f 2a 20 63 63 6f   case 65: /* cco
30a7d 6e 73 20 3a 3a 3d 20 55 4e 49 51 55 45 20 6f 6e  ns ::= UNIQUE on
30a7e 63 6f 6e 66 20 2a 2f 0a 7b 73 71 6c 69 74 65 33  conf */.{sqlite3
30a7f 43 72 65 61 74 65 49 6e 64 65 78 28 70 50 61 72  CreateIndex(pPar
30a80 73 65 2c 30 2c 30 2c 30 2c 30 2c 79 79 6d 73 70  se,0,0,0,0,yymsp
30a81 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 2c  [0].minor.yy328,
30a82 30 2c 30 2c 30 2c 30 29 3b 7d 0a 20 20 20 20 20  0,0,0,0);}.     
30a83 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
30a84 63 61 73 65 20 36 36 3a 20 2f 2a 20 63 63 6f 6e  case 66: /* ccon
30a85 73 20 3a 3a 3d 20 43 48 45 43 4b 20 4c 50 20 65  s ::= CHECK LP e
30a86 78 70 72 20 52 50 20 2a 2f 0a 7b 73 71 6c 69 74  xpr RP */.{sqlit
30a87 65 33 41 64 64 43 68 65 63 6b 43 6f 6e 73 74 72  e3AddCheckConstr
30a88 61 69 6e 74 28 70 50 61 72 73 65 2c 79 79 6d 73  aint(pParse,yyms
30a89 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  p[-1].minor.yy34
30a8a 36 2e 70 45 78 70 72 29 3b 7d 0a 20 20 20 20 20  6.pExpr);}.     
30a8b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
30a8c 63 61 73 65 20 36 37 3a 20 2f 2a 20 63 63 6f 6e  case 67: /* ccon
30a8d 73 20 3a 3a 3d 20 52 45 46 45 52 45 4e 43 45 53  s ::= REFERENCES
30a8e 20 6e 6d 20 69 64 78 6c 69 73 74 5f 6f 70 74 20   nm idxlist_opt 
30a8f 72 65 66 61 72 67 73 20 2a 2f 0a 7b 73 71 6c 69  refargs */.{sqli
30a90 74 65 33 43 72 65 61 74 65 46 6f 72 65 69 67 6e  te3CreateForeign
30a91 4b 65 79 28 70 50 61 72 73 65 2c 30 2c 26 79 79  Key(pParse,0,&yy
30a92 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
30a93 30 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  0,yymsp[-1].mino
30a94 72 2e 79 79 31 34 2c 79 79 6d 73 70 5b 30 5d 2e  r.yy14,yymsp[0].
30a95 6d 69 6e 6f 72 2e 79 79 33 32 38 29 3b 7d 0a 20  minor.yy328);}. 
30a96 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
30a97 20 20 20 20 63 61 73 65 20 36 38 3a 20 2f 2a 20      case 68: /* 
30a98 63 63 6f 6e 73 20 3a 3a 3d 20 64 65 66 65 72 5f  ccons ::= defer_
30a99 73 75 62 63 6c 61 75 73 65 20 2a 2f 0a 7b 73 71  subclause */.{sq
30a9a 6c 69 74 65 33 44 65 66 65 72 46 6f 72 65 69 67  lite3DeferForeig
30a9b 6e 4b 65 79 28 70 50 61 72 73 65 2c 79 79 6d 73  nKey(pParse,yyms
30a9c 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38  p[0].minor.yy328
30a9d 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
30a9e 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36 39  k;.      case 69
30a9f 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 43  : /* ccons ::= C
30aa0 4f 4c 4c 41 54 45 20 69 64 73 20 2a 2f 0a 7b 73  OLLATE ids */.{s
30aa1 71 6c 69 74 65 33 41 64 64 43 6f 6c 6c 61 74 65  qlite3AddCollate
30aa2 54 79 70 65 28 70 50 61 72 73 65 2c 20 26 79 79  Type(pParse, &yy
30aa3 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
30aa4 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
30aa5 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 37 32  k;.      case 72
30aa6 3a 20 2f 2a 20 72 65 66 61 72 67 73 20 3a 3a 3d  : /* refargs ::=
30aa7 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f   */.{ yygotomino
30aa8 72 2e 79 79 33 32 38 20 3d 20 4f 45 5f 52 65 73  r.yy328 = OE_Res
30aa9 74 72 69 63 74 20 2a 20 30 78 30 31 30 31 30 31  trict * 0x010101
30aaa 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ; }.        brea
30aab 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 37 33  k;.      case 73
30aac 3a 20 2f 2a 20 72 65 66 61 72 67 73 20 3a 3a 3d  : /* refargs ::=
30aad 20 72 65 66 61 72 67 73 20 72 65 66 61 72 67 20   refargs refarg 
30aae 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  */.{ yygotominor
30aaf 2e 79 79 33 32 38 20 3d 20 28 79 79 6d 73 70 5b  .yy328 = (yymsp[
30ab0 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 20  -1].minor.yy328 
30ab1 26 20 7e 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  & ~yymsp[0].mino
30ab2 72 2e 79 79 34 32 39 2e 6d 61 73 6b 29 20 7c 20  r.yy429.mask) | 
30ab3 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
30ab4 79 34 32 39 2e 76 61 6c 75 65 3b 20 7d 0a 20 20  y429.value; }.  
30ab5 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
30ab6 20 20 20 63 61 73 65 20 37 34 3a 20 2f 2a 20 72     case 74: /* r
30ab7 65 66 61 72 67 20 3a 3a 3d 20 4d 41 54 43 48 20  efarg ::= MATCH 
30ab8 6e 6d 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69  nm */.{ yygotomi
30ab9 6e 6f 72 2e 79 79 34 32 39 2e 76 61 6c 75 65 20  nor.yy429.value 
30aba 3d 20 30 3b 20 20 20 20 20 79 79 67 6f 74 6f 6d  = 0;     yygotom
30abb 69 6e 6f 72 2e 79 79 34 32 39 2e 6d 61 73 6b 20  inor.yy429.mask 
30abc 3d 20 30 78 30 30 30 30 30 30 3b 20 7d 0a 20 20  = 0x000000; }.  
30abd 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
30abe 20 20 20 63 61 73 65 20 37 35 3a 20 2f 2a 20 72     case 75: /* r
30abf 65 66 61 72 67 20 3a 3a 3d 20 4f 4e 20 44 45 4c  efarg ::= ON DEL
30ac0 45 54 45 20 72 65 66 61 63 74 20 2a 2f 0a 7b 20  ETE refact */.{ 
30ac1 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 32  yygotominor.yy42
30ac2 39 2e 76 61 6c 75 65 20 3d 20 79 79 6d 73 70 5b  9.value = yymsp[
30ac3 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 3b 20  0].minor.yy328; 
30ac4 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e      yygotominor.
30ac5 79 79 34 32 39 2e 6d 61 73 6b 20 3d 20 30 78 30  yy429.mask = 0x0
30ac6 30 30 30 66 66 3b 20 7d 0a 20 20 20 20 20 20 20  000ff; }.       
30ac7 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
30ac8 73 65 20 37 36 3a 20 2f 2a 20 72 65 66 61 72 67  se 76: /* refarg
30ac9 20 3a 3a 3d 20 4f 4e 20 55 50 44 41 54 45 20 72   ::= ON UPDATE r
30aca 65 66 61 63 74 20 2a 2f 0a 7b 20 79 79 67 6f 74  efact */.{ yygot
30acb 6f 6d 69 6e 6f 72 2e 79 79 34 32 39 2e 76 61 6c  ominor.yy429.val
30acc 75 65 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  ue = yymsp[0].mi
30acd 6e 6f 72 2e 79 79 33 32 38 3c 3c 38 3b 20 20 79  nor.yy328<<8;  y
30ace 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 32 39  ygotominor.yy429
30acf 2e 6d 61 73 6b 20 3d 20 30 78 30 30 66 66 30 30  .mask = 0x00ff00
30ad0 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ; }.        brea
30ad1 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 37 37  k;.      case 77
30ad2 3a 20 2f 2a 20 72 65 66 61 72 67 20 3a 3a 3d 20  : /* refarg ::= 
30ad3 4f 4e 20 49 4e 53 45 52 54 20 72 65 66 61 63 74  ON INSERT refact
30ad4 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f   */.{ yygotomino
30ad5 72 2e 79 79 34 32 39 2e 76 61 6c 75 65 20 3d 20  r.yy429.value = 
30ad6 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
30ad7 79 33 32 38 3c 3c 31 36 3b 20 79 79 67 6f 74 6f  y328<<16; yygoto
30ad8 6d 69 6e 6f 72 2e 79 79 34 32 39 2e 6d 61 73 6b  minor.yy429.mask
30ad9 20 3d 20 30 78 66 66 30 30 30 30 3b 20 7d 0a 20   = 0xff0000; }. 
30ada 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
30adb 20 20 20 20 63 61 73 65 20 37 38 3a 20 2f 2a 20      case 78: /* 
30adc 72 65 66 61 63 74 20 3a 3a 3d 20 53 45 54 20 4e  refact ::= SET N
30add 55 4c 4c 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d  ULL */.{ yygotom
30ade 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 4f 45 5f  inor.yy328 = OE_
30adf 53 65 74 4e 75 6c 6c 3b 20 7d 0a 20 20 20 20 20  SetNull; }.     
30ae0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
30ae1 63 61 73 65 20 37 39 3a 20 2f 2a 20 72 65 66 61  case 79: /* refa
30ae2 63 74 20 3a 3a 3d 20 53 45 54 20 44 45 46 41 55  ct ::= SET DEFAU
30ae3 4c 54 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69  LT */.{ yygotomi
30ae4 6e 6f 72 2e 79 79 33 32 38 20 3d 20 4f 45 5f 53  nor.yy328 = OE_S
30ae5 65 74 44 66 6c 74 3b 20 7d 0a 20 20 20 20 20 20  etDflt; }.      
30ae6 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
30ae7 61 73 65 20 38 30 3a 20 2f 2a 20 72 65 66 61 63  ase 80: /* refac
30ae8 74 20 3a 3a 3d 20 43 41 53 43 41 44 45 20 2a 2f  t ::= CASCADE */
30ae9 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .{ yygotominor.y
30aea 79 33 32 38 20 3d 20 4f 45 5f 43 61 73 63 61 64  y328 = OE_Cascad
30aeb 65 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  e; }.        bre
30aec 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 38  ak;.      case 8
30aed 31 3a 20 2f 2a 20 72 65 66 61 63 74 20 3a 3a 3d  1: /* refact ::=
30aee 20 52 45 53 54 52 49 43 54 20 2a 2f 0a 7b 20 79   RESTRICT */.{ y
30aef 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38  ygotominor.yy328
30af0 20 3d 20 4f 45 5f 52 65 73 74 72 69 63 74 3b 20   = OE_Restrict; 
30af1 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
30af2 0a 20 20 20 20 20 20 63 61 73 65 20 38 32 3a 20  .      case 82: 
30af3 2f 2a 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75  /* defer_subclau
30af4 73 65 20 3a 3a 3d 20 4e 4f 54 20 44 45 46 45 52  se ::= NOT DEFER
30af5 52 41 42 4c 45 20 69 6e 69 74 5f 64 65 66 65 72  RABLE init_defer
30af6 72 65 64 5f 70 72 65 64 5f 6f 70 74 20 2a 2f 0a  red_pred_opt */.
30af7 20 20 20 20 20 20 63 61 73 65 20 38 33 3a 20 2f        case 83: /
30af8 2a 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73  * defer_subclaus
30af9 65 20 3a 3a 3d 20 44 45 46 45 52 52 41 42 4c 45  e ::= DEFERRABLE
30afa 20 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70   init_deferred_p
30afb 72 65 64 5f 6f 70 74 20 2a 2f 20 79 79 74 65 73  red_opt */ yytes
30afc 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
30afd 38 33 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  83);.      case 
30afe 39 38 3a 20 2f 2a 20 64 65 66 65 72 5f 73 75 62  98: /* defer_sub
30aff 63 6c 61 75 73 65 5f 6f 70 74 20 3a 3a 3d 20 64  clause_opt ::= d
30b00 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 20 2a  efer_subclause *
30b01 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
30b02 75 6c 65 6e 6f 3d 3d 39 38 29 3b 0a 20 20 20 20  uleno==98);.    
30b03 20 20 63 61 73 65 20 31 30 30 3a 20 2f 2a 20 6f    case 100: /* o
30b04 6e 63 6f 6e 66 20 3a 3a 3d 20 4f 4e 20 43 4f 4e  nconf ::= ON CON
30b05 46 4c 49 43 54 20 72 65 73 6f 6c 76 65 74 79 70  FLICT resolvetyp
30b06 65 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  e */ yytestcase(
30b07 79 79 72 75 6c 65 6e 6f 3d 3d 31 30 30 29 3b 0a  yyruleno==100);.
30b08 20 20 20 20 20 20 63 61 73 65 20 31 30 33 3a 20        case 103: 
30b09 2f 2a 20 72 65 73 6f 6c 76 65 74 79 70 65 20 3a  /* resolvetype :
30b0a 3a 3d 20 72 61 69 73 65 74 79 70 65 20 2a 2f 20  := raisetype */ 
30b0b 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
30b0c 65 6e 6f 3d 3d 31 30 33 29 3b 0a 7b 79 79 67 6f  eno==103);.{yygo
30b0d 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20  tominor.yy328 = 
30b0e 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
30b0f 79 33 32 38 3b 7d 0a 20 20 20 20 20 20 20 20 62  y328;}.        b
30b10 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
30b11 20 38 37 3a 20 2f 2a 20 63 6f 6e 73 6c 69 73 74   87: /* conslist
30b12 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67  _opt ::= */.{yyg
30b13 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d  otominor.yy0.n =
30b14 20 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e   0; yygotominor.
30b15 79 79 30 2e 7a 20 3d 20 30 3b 7d 0a 20 20 20 20  yy0.z = 0;}.    
30b16 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
30b17 20 63 61 73 65 20 38 38 3a 20 2f 2a 20 63 6f 6e   case 88: /* con
30b18 73 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 43 4f  slist_opt ::= CO
30b19 4d 4d 41 20 63 6f 6e 73 6c 69 73 74 20 2a 2f 0a  MMA conslist */.
30b1a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30  {yygotominor.yy0
30b1b 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e   = yymsp[-1].min
30b1c 6f 72 2e 79 79 30 3b 7d 0a 20 20 20 20 20 20 20  or.yy0;}.       
30b1d 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
30b1e 73 65 20 39 33 3a 20 2f 2a 20 74 63 6f 6e 73 20  se 93: /* tcons 
30b1f 3a 3a 3d 20 50 52 49 4d 41 52 59 20 4b 45 59 20  ::= PRIMARY KEY 
30b20 4c 50 20 69 64 78 6c 69 73 74 20 61 75 74 6f 69  LP idxlist autoi
30b21 6e 63 20 52 50 20 6f 6e 63 6f 6e 66 20 2a 2f 0a  nc RP onconf */.
30b22 7b 73 71 6c 69 74 65 33 41 64 64 50 72 69 6d 61  {sqlite3AddPrima
30b23 72 79 4b 65 79 28 70 50 61 72 73 65 2c 79 79 6d  ryKey(pParse,yym
30b24 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-3].minor.yy1
30b25 34 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  4,yymsp[0].minor
30b26 2e 79 79 33 32 38 2c 79 79 6d 73 70 5b 2d 32 5d  .yy328,yymsp[-2]
30b27 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 2c 30 29 3b  .minor.yy328,0);
30b28 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
30b29 0a 20 20 20 20 20 20 63 61 73 65 20 39 34 3a 20  .      case 94: 
30b2a 2f 2a 20 74 63 6f 6e 73 20 3a 3a 3d 20 55 4e 49  /* tcons ::= UNI
30b2b 51 55 45 20 4c 50 20 69 64 78 6c 69 73 74 20 52  QUE LP idxlist R
30b2c 50 20 6f 6e 63 6f 6e 66 20 2a 2f 0a 7b 73 71 6c  P onconf */.{sql
30b2d 69 74 65 33 43 72 65 61 74 65 49 6e 64 65 78 28  ite3CreateIndex(
30b2e 70 50 61 72 73 65 2c 30 2c 30 2c 30 2c 79 79 6d  pParse,0,0,0,yym
30b2f 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-2].minor.yy1
30b30 34 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  4,yymsp[0].minor
30b31 2e 79 79 33 32 38 2c 30 2c 30 2c 30 2c 30 29 3b  .yy328,0,0,0,0);
30b32 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
30b33 0a 20 20 20 20 20 20 63 61 73 65 20 39 35 3a 20  .      case 95: 
30b34 2f 2a 20 74 63 6f 6e 73 20 3a 3a 3d 20 43 48 45  /* tcons ::= CHE
30b35 43 4b 20 4c 50 20 65 78 70 72 20 52 50 20 6f 6e  CK LP expr RP on
30b36 63 6f 6e 66 20 2a 2f 0a 7b 73 71 6c 69 74 65 33  conf */.{sqlite3
30b37 41 64 64 43 68 65 63 6b 43 6f 6e 73 74 72 61 69  AddCheckConstrai
30b38 6e 74 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b  nt(pParse,yymsp[
30b39 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  -2].minor.yy346.
30b3a 70 45 78 70 72 29 3b 7d 0a 20 20 20 20 20 20 20  pExpr);}.       
30b3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
30b3c 73 65 20 39 36 3a 20 2f 2a 20 74 63 6f 6e 73 20  se 96: /* tcons 
30b3d 3a 3a 3d 20 46 4f 52 45 49 47 4e 20 4b 45 59 20  ::= FOREIGN KEY 
30b3e 4c 50 20 69 64 78 6c 69 73 74 20 52 50 20 52 45  LP idxlist RP RE
30b3f 46 45 52 45 4e 43 45 53 20 6e 6d 20 69 64 78 6c  FERENCES nm idxl
30b40 69 73 74 5f 6f 70 74 20 72 65 66 61 72 67 73 20  ist_opt refargs 
30b41 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 5f  defer_subclause_
30b42 6f 70 74 20 2a 2f 0a 7b 0a 20 20 20 20 73 71 6c  opt */.{.    sql
30b43 69 74 65 33 43 72 65 61 74 65 46 6f 72 65 69 67  ite3CreateForeig
30b44 6e 4b 65 79 28 70 50 61 72 73 65 2c 20 79 79 6d  nKey(pParse, yym
30b45 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-6].minor.yy1
30b46 34 2c 20 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  4, &yymsp[-3].mi
30b47 6e 6f 72 2e 79 79 30 2c 20 79 79 6d 73 70 5b 2d  nor.yy0, yymsp[-
30b48 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 79  2].minor.yy14, y
30b49 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
30b4a 79 33 32 38 29 3b 0a 20 20 20 20 73 71 6c 69 74  y328);.    sqlit
30b4b 65 33 44 65 66 65 72 46 6f 72 65 69 67 6e 4b 65  e3DeferForeignKe
30b4c 79 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b  y(pParse, yymsp[
30b4d 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 29 3b  0].minor.yy328);
30b4e 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
30b4f 3b 0a 20 20 20 20 20 20 63 61 73 65 20 39 39 3a  ;.      case 99:
30b50 20 2f 2a 20 6f 6e 63 6f 6e 66 20 3a 3a 3d 20 2a   /* onconf ::= *
30b51 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
30b52 79 33 32 38 20 3d 20 4f 45 5f 44 65 66 61 75 6c  y328 = OE_Defaul
30b53 74 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  t;}.        brea
30b54 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 30  k;.      case 10
30b55 31 3a 20 2f 2a 20 6f 72 63 6f 6e 66 20 3a 3a 3d  1: /* orconf ::=
30b56 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
30b57 2e 79 79 31 38 36 20 3d 20 4f 45 5f 44 65 66 61  .yy186 = OE_Defa
30b58 75 6c 74 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  ult;}.        br
30b59 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
30b5a 31 30 32 3a 20 2f 2a 20 6f 72 63 6f 6e 66 20 3a  102: /* orconf :
30b5b 3a 3d 20 4f 52 20 72 65 73 6f 6c 76 65 74 79 70  := OR resolvetyp
30b5c 65 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  e */.{yygotomino
30b5d 72 2e 79 79 31 38 36 20 3d 20 28 75 38 29 79 79  r.yy186 = (u8)yy
30b5e 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
30b5f 32 38 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  28;}.        bre
30b60 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
30b61 30 34 3a 20 2f 2a 20 72 65 73 6f 6c 76 65 74 79  04: /* resolvety
30b62 70 65 20 3a 3a 3d 20 49 47 4e 4f 52 45 20 2a 2f  pe ::= IGNORE */
30b63 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
30b64 33 32 38 20 3d 20 4f 45 5f 49 67 6e 6f 72 65 3b  328 = OE_Ignore;
30b65 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
30b66 0a 20 20 20 20 20 20 63 61 73 65 20 31 30 35 3a  .      case 105:
30b67 20 2f 2a 20 72 65 73 6f 6c 76 65 74 79 70 65 20   /* resolvetype 
30b68 3a 3a 3d 20 52 45 50 4c 41 43 45 20 2a 2f 0a 7b  ::= REPLACE */.{
30b69 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32  yygotominor.yy32
30b6a 38 20 3d 20 4f 45 5f 52 65 70 6c 61 63 65 3b 7d  8 = OE_Replace;}
30b6b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
30b6c 20 20 20 20 20 20 63 61 73 65 20 31 30 36 3a 20        case 106: 
30b6d 2f 2a 20 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20  /* cmd ::= DROP 
30b6e 54 41 42 4c 45 20 69 66 65 78 69 73 74 73 20 66  TABLE ifexists f
30b6f 75 6c 6c 6e 61 6d 65 20 2a 2f 0a 7b 0a 20 20 73  ullname */.{.  s
30b70 71 6c 69 74 65 33 44 72 6f 70 54 61 62 6c 65 28  qlite3DropTable(
30b71 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d  pParse, yymsp[0]
30b72 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 20 30 2c 20  .minor.yy65, 0, 
30b73 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
30b74 79 79 33 32 38 29 3b 0a 7d 0a 20 20 20 20 20 20  yy328);.}.      
30b75 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
30b76 61 73 65 20 31 30 39 3a 20 2f 2a 20 63 6d 64 20  ase 109: /* cmd 
30b77 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20 74 65 6d  ::= createkw tem
30b78 70 20 56 49 45 57 20 69 66 6e 6f 74 65 78 69 73  p VIEW ifnotexis
30b79 74 73 20 6e 6d 20 64 62 6e 6d 20 41 53 20 73 65  ts nm dbnm AS se
30b7a 6c 65 63 74 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69  lect */.{.  sqli
30b7b 74 65 33 43 72 65 61 74 65 56 69 65 77 28 70 50  te3CreateView(pP
30b7c 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d 37 5d  arse, &yymsp[-7]
30b7d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d  .minor.yy0, &yym
30b7e 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-3].minor.yy0
30b7f 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  , &yymsp[-2].min
30b80 6f 72 2e 79 79 30 2c 20 79 79 6d 73 70 5b 30 5d  or.yy0, yymsp[0]
30b81 2e 6d 69 6e 6f 72 2e 79 79 33 2c 20 79 79 6d 73  .minor.yy3, yyms
30b82 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32  p[-6].minor.yy32
30b83 38 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e  8, yymsp[-4].min
30b84 6f 72 2e 79 79 33 32 38 29 3b 0a 7d 0a 20 20 20  or.yy328);.}.   
30b85 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
30b86 20 20 63 61 73 65 20 31 31 30 3a 20 2f 2a 20 63    case 110: /* c
30b87 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 56 49 45 57  md ::= DROP VIEW
30b88 20 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61   ifexists fullna
30b89 6d 65 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65  me */.{.  sqlite
30b8a 33 44 72 6f 70 54 61 62 6c 65 28 70 50 61 72 73  3DropTable(pPars
30b8b 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  e, yymsp[0].mino
30b8c 72 2e 79 79 36 35 2c 20 31 2c 20 79 79 6d 73 70  r.yy65, 1, yymsp
30b8d 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38  [-1].minor.yy328
30b8e 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
30b8f 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
30b90 31 31 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 73  11: /* cmd ::= s
30b91 65 6c 65 63 74 20 2a 2f 0a 7b 0a 20 20 53 65 6c  elect */.{.  Sel
30b92 65 63 74 44 65 73 74 20 64 65 73 74 20 3d 20 7b  ectDest dest = {
30b93 53 52 54 5f 4f 75 74 70 75 74 2c 20 30 2c 20 30  SRT_Output, 0, 0
30b94 2c 20 30 2c 20 30 7d 3b 0a 20 20 73 71 6c 69 74  , 0, 0};.  sqlit
30b95 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c  e3Select(pParse,
30b96 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
30b97 79 79 33 2c 20 26 64 65 73 74 29 3b 0a 20 20 73  yy3, &dest);.  s
30b98 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
30b99 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79  te(pParse->db, y
30b9a 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
30b9b 33 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  3);.}.        br
30b9c 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
30b9d 31 31 32 3a 20 2f 2a 20 73 65 6c 65 63 74 20 3a  112: /* select :
30b9e 3a 3d 20 6f 6e 65 73 65 6c 65 63 74 20 2a 2f 0a  := oneselect */.
30b9f 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  {yygotominor.yy3
30ba0 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
30ba1 72 2e 79 79 33 3b 7d 0a 20 20 20 20 20 20 20 20  r.yy3;}.        
30ba2 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
30ba3 65 20 31 31 33 3a 20 2f 2a 20 73 65 6c 65 63 74  e 113: /* select
30ba4 20 3a 3a 3d 20 73 65 6c 65 63 74 20 6d 75 6c 74   ::= select mult
30ba5 69 73 65 6c 65 63 74 5f 6f 70 20 6f 6e 65 73 65  iselect_op onese
30ba6 6c 65 63 74 20 2a 2f 0a 7b 0a 20 20 69 66 28 20  lect */.{.  if( 
30ba7 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
30ba8 79 33 20 29 7b 0a 20 20 20 20 79 79 6d 73 70 5b  y3 ){.    yymsp[
30ba9 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 2d 3e 6f 70  0].minor.yy3->op
30baa 20 3d 20 28 75 38 29 79 79 6d 73 70 5b 2d 31 5d   = (u8)yymsp[-1]
30bab 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 3b 0a 20 20  .minor.yy328;.  
30bac 20 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72    yymsp[0].minor
30bad 2e 79 79 33 2d 3e 70 50 72 69 6f 72 20 3d 20 79  .yy3->pPrior = y
30bae 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
30baf 79 33 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  y3;.  }else{.   
30bb0 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
30bb1 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  lete(pParse->db,
30bb2 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72   yymsp[-2].minor
30bb3 2e 79 79 33 29 3b 0a 20 20 7d 0a 20 20 79 79 67  .yy3);.  }.  yyg
30bb4 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 20 3d 20 79  otominor.yy3 = y
30bb5 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
30bb6 33 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  3;.}.        bre
30bb7 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
30bb8 31 35 3a 20 2f 2a 20 6d 75 6c 74 69 73 65 6c 65  15: /* multisele
30bb9 63 74 5f 6f 70 20 3a 3a 3d 20 55 4e 49 4f 4e 20  ct_op ::= UNION 
30bba 41 4c 4c 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ALL */.{yygotomi
30bbb 6e 6f 72 2e 79 79 33 32 38 20 3d 20 54 4b 5f 41  nor.yy328 = TK_A
30bbc 4c 4c 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  LL;}.        bre
30bbd 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
30bbe 31 37 3a 20 2f 2a 20 6f 6e 65 73 65 6c 65 63 74  17: /* oneselect
30bbf 20 3a 3a 3d 20 53 45 4c 45 43 54 20 64 69 73 74   ::= SELECT dist
30bc0 69 6e 63 74 20 73 65 6c 63 6f 6c 6c 69 73 74 20  inct selcollist 
30bc1 66 72 6f 6d 20 77 68 65 72 65 5f 6f 70 74 20 67  from where_opt g
30bc2 72 6f 75 70 62 79 5f 6f 70 74 20 68 61 76 69 6e  roupby_opt havin
30bc3 67 5f 6f 70 74 20 6f 72 64 65 72 62 79 5f 6f 70  g_opt orderby_op
30bc4 74 20 6c 69 6d 69 74 5f 6f 70 74 20 2a 2f 0a 7b  t limit_opt */.{
30bc5 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
30bc6 79 33 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  y3 = sqlite3Sele
30bc7 63 74 4e 65 77 28 70 50 61 72 73 65 2c 79 79 6d  ctNew(pParse,yym
30bc8 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-6].minor.yy1
30bc9 34 2c 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f  4,yymsp[-5].mino
30bca 72 2e 79 79 36 35 2c 79 79 6d 73 70 5b 2d 34 5d  r.yy65,yymsp[-4]
30bcb 2e 6d 69 6e 6f 72 2e 79 79 31 33 32 2c 79 79 6d  .minor.yy132,yym
30bcc 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-3].minor.yy1
30bcd 34 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  4,yymsp[-2].mino
30bce 72 2e 79 79 31 33 32 2c 79 79 6d 73 70 5b 2d 31  r.yy132,yymsp[-1
30bcf 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 79 79 6d  ].minor.yy14,yym
30bd0 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-7].minor.yy3
30bd1 32 38 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  28,yymsp[0].mino
30bd2 72 2e 79 79 34 37 36 2e 70 4c 69 6d 69 74 2c 79  r.yy476.pLimit,y
30bd3 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
30bd4 34 37 36 2e 70 4f 66 66 73 65 74 29 3b 0a 7d 0a  476.pOffset);.}.
30bd5 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
30bd6 20 20 20 20 20 63 61 73 65 20 31 32 31 3a 20 2f       case 121: /
30bd7 2a 20 73 63 6c 70 20 3a 3a 3d 20 73 65 6c 63 6f  * sclp ::= selco
30bd8 6c 6c 69 73 74 20 43 4f 4d 4d 41 20 2a 2f 0a 20  llist COMMA */. 
30bd9 20 20 20 20 20 63 61 73 65 20 32 34 37 3a 20 2f       case 247: /
30bda 2a 20 69 64 78 6c 69 73 74 5f 6f 70 74 20 3a 3a  * idxlist_opt ::
30bdb 3d 20 4c 50 20 69 64 78 6c 69 73 74 20 52 50 20  = LP idxlist RP 
30bdc 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
30bdd 72 75 6c 65 6e 6f 3d 3d 32 34 37 29 3b 0a 7b 79  ruleno==247);.{y
30bde 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20  ygotominor.yy14 
30bdf 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  = yymsp[-1].mino
30be0 72 2e 79 79 31 34 3b 7d 0a 20 20 20 20 20 20 20  r.yy14;}.       
30be1 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
30be2 73 65 20 31 32 32 3a 20 2f 2a 20 73 63 6c 70 20  se 122: /* sclp 
30be3 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ::= */.      cas
30be4 65 20 31 35 30 3a 20 2f 2a 20 6f 72 64 65 72 62  e 150: /* orderb
30be5 79 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74  y_opt ::= */ yyt
30be6 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
30be7 3d 3d 31 35 30 29 3b 0a 20 20 20 20 20 20 63 61  ==150);.      ca
30be8 73 65 20 31 35 38 3a 20 2f 2a 20 67 72 6f 75 70  se 158: /* group
30be9 62 79 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79  by_opt ::= */ yy
30bea 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
30beb 6f 3d 3d 31 35 38 29 3b 0a 20 20 20 20 20 20 63  o==158);.      c
30bec 61 73 65 20 32 34 30 3a 20 2f 2a 20 65 78 70 72  ase 240: /* expr
30bed 6c 69 73 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65  list ::= */ yyte
30bee 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
30bef 3d 32 34 30 29 3b 0a 20 20 20 20 20 20 63 61 73  =240);.      cas
30bf0 65 20 32 34 36 3a 20 2f 2a 20 69 64 78 6c 69 73  e 246: /* idxlis
30bf1 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74  t_opt ::= */ yyt
30bf2 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
30bf3 3d 3d 32 34 36 29 3b 0a 7b 79 79 67 6f 74 6f 6d  ==246);.{yygotom
30bf4 69 6e 6f 72 2e 79 79 31 34 20 3d 20 30 3b 7d 0a  inor.yy14 = 0;}.
30bf5 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
30bf6 20 20 20 20 20 63 61 73 65 20 31 32 33 3a 20 2f       case 123: /
30bf7 2a 20 73 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d  * selcollist ::=
30bf8 20 73 63 6c 70 20 65 78 70 72 20 61 73 20 2a 2f   sclp expr as */
30bf9 0a 7b 0a 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  .{.   yygotomino
30bfa 72 2e 79 79 31 34 20 3d 20 73 71 6c 69 74 65 33  r.yy14 = sqlite3
30bfb 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
30bfc 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 32 5d  Parse, yymsp[-2]
30bfd 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 79 79 6d  .minor.yy14, yym
30bfe 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-1].minor.yy3
30bff 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 20 69 66  46.pExpr);.   if
30c00 28 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ( yymsp[0].minor
30c01 2e 79 79 30 2e 6e 3e 30 20 29 20 73 71 6c 69 74  .yy0.n>0 ) sqlit
30c02 65 33 45 78 70 72 4c 69 73 74 53 65 74 4e 61 6d  e3ExprListSetNam
30c03 65 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f  e(pParse, yygoto
30c04 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 26 79 79 6d  minor.yy14, &yym
30c05 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  sp[0].minor.yy0,
30c06 20 31 29 3b 0a 20 20 20 73 71 6c 69 74 65 33 45   1);.   sqlite3E
30c07 78 70 72 4c 69 73 74 53 65 74 53 70 61 6e 28 70  xprListSetSpan(p
30c08 50 61 72 73 65 2c 79 79 67 6f 74 6f 6d 69 6e 6f  Parse,yygotomino
30c09 72 2e 79 79 31 34 2c 26 79 79 6d 73 70 5b 2d 31  r.yy14,&yymsp[-1
30c0a 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 29 3b 0a  ].minor.yy346);.
30c0b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
30c0c 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 34 3a  .      case 124:
30c0d 20 2f 2a 20 73 65 6c 63 6f 6c 6c 69 73 74 20 3a   /* selcollist :
30c0e 3a 3d 20 73 63 6c 70 20 53 54 41 52 20 2a 2f 0a  := sclp STAR */.
30c0f 7b 0a 20 20 45 78 70 72 20 2a 70 20 3d 20 73 71  {.  Expr *p = sq
30c10 6c 69 74 65 33 45 78 70 72 28 70 50 61 72 73 65  lite3Expr(pParse
30c11 2d 3e 64 62 2c 20 54 4b 5f 41 4c 4c 2c 20 30 29  ->db, TK_ALL, 0)
30c12 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ;.  yygotominor.
30c13 79 79 31 34 20 3d 20 73 71 6c 69 74 65 33 45 78  yy14 = sqlite3Ex
30c14 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
30c15 72 73 65 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  rse, yymsp[-1].m
30c16 69 6e 6f 72 2e 79 79 31 34 2c 20 70 29 3b 0a 7d  inor.yy14, p);.}
30c17 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
30c18 20 20 20 20 20 20 63 61 73 65 20 31 32 35 3a 20        case 125: 
30c19 2f 2a 20 73 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a  /* selcollist ::
30c1a 3d 20 73 63 6c 70 20 6e 6d 20 44 4f 54 20 53 54  = sclp nm DOT ST
30c1b 41 52 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 20 2a  AR */.{.  Expr *
30c1c 70 52 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33  pRight = sqlite3
30c1d 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
30c1e 5f 41 4c 4c 2c 20 30 2c 20 30 2c 20 26 79 79 6d  _ALL, 0, 0, &yym
30c1f 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
30c20 3b 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20  ;.  Expr *pLeft 
30c21 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
30c22 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c  Parse, TK_ID, 0,
30c23 20 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d   0, &yymsp[-2].m
30c24 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 45 78 70  inor.yy0);.  Exp
30c25 72 20 2a 70 44 6f 74 20 3d 20 73 71 6c 69 74 65  r *pDot = sqlite
30c26 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
30c27 4b 5f 44 4f 54 2c 20 70 4c 65 66 74 2c 20 70 52  K_DOT, pLeft, pR
30c28 69 67 68 74 2c 20 30 29 3b 0a 20 20 79 79 67 6f  ight, 0);.  yygo
30c29 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73  tominor.yy14 = s
30c2a 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
30c2b 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79 6d 73  pend(pParse,yyms
30c2c 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34  p[-3].minor.yy14
30c2d 2c 20 70 44 6f 74 29 3b 0a 7d 0a 20 20 20 20 20  , pDot);.}.     
30c2e 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
30c2f 63 61 73 65 20 31 32 38 3a 20 2f 2a 20 61 73 20  case 128: /* as 
30c30 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ::= */.{yygotomi
30c31 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20 30 3b 7d 0a  nor.yy0.n = 0;}.
30c32 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
30c33 20 20 20 20 20 63 61 73 65 20 31 32 39 3a 20 2f       case 129: /
30c34 2a 20 66 72 6f 6d 20 3a 3a 3d 20 2a 2f 0a 7b 79  * from ::= */.{y
30c35 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 20  ygotominor.yy65 
30c36 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
30c37 63 5a 65 72 6f 28 70 50 61 72 73 65 2d 3e 64 62  cZero(pParse->db
30c38 2c 20 73 69 7a 65 6f 66 28 2a 79 79 67 6f 74 6f  , sizeof(*yygoto
30c39 6d 69 6e 6f 72 2e 79 79 36 35 29 29 3b 7d 0a 20  minor.yy65));}. 
30c3a 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
30c3b 20 20 20 20 63 61 73 65 20 31 33 30 3a 20 2f 2a      case 130: /*
30c3c 20 66 72 6f 6d 20 3a 3a 3d 20 46 52 4f 4d 20 73   from ::= FROM s
30c3d 65 6c 74 61 62 6c 69 73 74 20 2a 2f 0a 7b 0a 20  eltablist */.{. 
30c3e 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36   yygotominor.yy6
30c3f 35 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  5 = yymsp[0].min
30c40 6f 72 2e 79 79 36 35 3b 0a 20 20 73 71 6c 69 74  or.yy65;.  sqlit
30c41 65 33 53 72 63 4c 69 73 74 53 68 69 66 74 4a 6f  e3SrcListShiftJo
30c42 69 6e 54 79 70 65 28 79 79 67 6f 74 6f 6d 69 6e  inType(yygotomin
30c43 6f 72 2e 79 79 36 35 29 3b 0a 7d 0a 20 20 20 20  or.yy65);.}.    
30c44 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
30c45 20 63 61 73 65 20 31 33 31 3a 20 2f 2a 20 73 74   case 131: /* st
30c46 6c 5f 70 72 65 66 69 78 20 3a 3a 3d 20 73 65 6c  l_prefix ::= sel
30c47 74 61 62 6c 69 73 74 20 6a 6f 69 6e 6f 70 20 2a  tablist joinop *
30c48 2f 0a 7b 0a 20 20 20 79 79 67 6f 74 6f 6d 69 6e  /.{.   yygotomin
30c49 6f 72 2e 79 79 36 35 20 3d 20 79 79 6d 73 70 5b  or.yy65 = yymsp[
30c4a 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 3b 0a  -1].minor.yy65;.
30c4b 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 79 79     if( ALWAYS(yy
30c4c 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 20 26  gotominor.yy65 &
30c4d 26 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  & yygotominor.yy
30c4e 36 35 2d 3e 6e 53 72 63 3e 30 29 20 29 20 79 79  65->nSrc>0) ) yy
30c4f 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 2d 3e  gotominor.yy65->
30c50 61 5b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  a[yygotominor.yy
30c51 36 35 2d 3e 6e 53 72 63 2d 31 5d 2e 6a 6f 69 6e  65->nSrc-1].join
30c52 74 79 70 65 20 3d 20 28 75 38 29 79 79 6d 73 70  type = (u8)yymsp
30c53 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 3b  [0].minor.yy328;
30c54 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
30c55 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 33 32  ;.      case 132
30c56 3a 20 2f 2a 20 73 74 6c 5f 70 72 65 66 69 78 20  : /* stl_prefix 
30c57 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ::= */.{yygotomi
30c58 6e 6f 72 2e 79 79 36 35 20 3d 20 30 3b 7d 0a 20  nor.yy65 = 0;}. 
30c59 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
30c5a 20 20 20 20 63 61 73 65 20 31 33 33 3a 20 2f 2a      case 133: /*
30c5b 20 73 65 6c 74 61 62 6c 69 73 74 20 3a 3a 3d 20   seltablist ::= 
30c5c 73 74 6c 5f 70 72 65 66 69 78 20 6e 6d 20 64 62  stl_prefix nm db
30c5d 6e 6d 20 61 73 20 69 6e 64 65 78 65 64 5f 6f 70  nm as indexed_op
30c5e 74 20 6f 6e 5f 6f 70 74 20 75 73 69 6e 67 5f 6f  t on_opt using_o
30c5f 70 74 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f  pt */.{.  yygoto
30c60 6d 69 6e 6f 72 2e 79 79 36 35 20 3d 20 73 71 6c  minor.yy65 = sql
30c61 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e  ite3SrcListAppen
30c62 64 46 72 6f 6d 54 65 72 6d 28 70 50 61 72 73 65  dFromTerm(pParse
30c63 2c 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72  ,yymsp[-6].minor
30c64 2e 79 79 36 35 2c 26 79 79 6d 73 70 5b 2d 35 5d  .yy65,&yymsp[-5]
30c65 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73  .minor.yy0,&yyms
30c66 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-4].minor.yy0,
30c67 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72  &yymsp[-3].minor
30c68 2e 79 79 30 2c 30 2c 79 79 6d 73 70 5b 2d 31 5d  .yy0,0,yymsp[-1]
30c69 2e 6d 69 6e 6f 72 2e 79 79 31 33 32 2c 79 79 6d  .minor.yy132,yym
30c6a 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30  sp[0].minor.yy40
30c6b 38 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 72 63  8);.  sqlite3Src
30c6c 4c 69 73 74 49 6e 64 65 78 65 64 42 79 28 70 50  ListIndexedBy(pP
30c6d 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f  arse, yygotomino
30c6e 72 2e 79 79 36 35 2c 20 26 79 79 6d 73 70 5b 2d  r.yy65, &yymsp[-
30c6f 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d  2].minor.yy0);.}
30c70 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
30c71 20 20 20 20 20 20 63 61 73 65 20 31 33 34 3a 20        case 134: 
30c72 2f 2a 20 73 65 6c 74 61 62 6c 69 73 74 20 3a 3a  /* seltablist ::
30c73 3d 20 73 74 6c 5f 70 72 65 66 69 78 20 4c 50 20  = stl_prefix LP 
30c74 73 65 6c 65 63 74 20 52 50 20 61 73 20 6f 6e 5f  select RP as on_
30c75 6f 70 74 20 75 73 69 6e 67 5f 6f 70 74 20 2a 2f  opt using_opt */
30c76 0a 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e  .{.    yygotomin
30c77 6f 72 2e 79 79 36 35 20 3d 20 73 71 6c 69 74 65  or.yy65 = sqlite
30c78 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 46 72  3SrcListAppendFr
30c79 6f 6d 54 65 72 6d 28 70 50 61 72 73 65 2c 79 79  omTerm(pParse,yy
30c7a 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-6].minor.yy
30c7b 36 35 2c 30 2c 30 2c 26 79 79 6d 73 70 5b 2d 32  65,0,0,&yymsp[-2
30c7c 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 79 79 6d 73  ].minor.yy0,yyms
30c7d 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 2c  p[-4].minor.yy3,
30c7e 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
30c7f 79 79 31 33 32 2c 79 79 6d 73 70 5b 30 5d 2e 6d  yy132,yymsp[0].m
30c80 69 6e 6f 72 2e 79 79 34 30 38 29 3b 0a 20 20 7d  inor.yy408);.  }
30c81 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
30c82 20 20 20 20 20 20 63 61 73 65 20 31 33 35 3a 20        case 135: 
30c83 2f 2a 20 73 65 6c 74 61 62 6c 69 73 74 20 3a 3a  /* seltablist ::
30c84 3d 20 73 74 6c 5f 70 72 65 66 69 78 20 4c 50 20  = stl_prefix LP 
30c85 73 65 6c 74 61 62 6c 69 73 74 20 52 50 20 61 73  seltablist RP as
30c86 20 6f 6e 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70   on_opt using_op
30c87 74 20 2a 2f 0a 7b 0a 20 20 20 20 69 66 28 20 79  t */.{.    if( y
30c88 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-6].minor.y
30c89 79 36 35 3d 3d 30 20 26 26 20 79 79 6d 73 70 5b  y65==0 && yymsp[
30c8a 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3d  -2].minor.yy0.n=
30c8b 3d 30 20 26 26 20 79 79 6d 73 70 5b 2d 31 5d 2e  =0 && yymsp[-1].
30c8c 6d 69 6e 6f 72 2e 79 79 31 33 32 3d 3d 30 20 26  minor.yy132==0 &
30c8d 26 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  & yymsp[0].minor
30c8e 2e 79 79 34 30 38 3d 3d 30 20 29 7b 0a 20 20 20  .yy408==0 ){.   
30c8f 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
30c90 79 36 35 20 3d 20 79 79 6d 73 70 5b 2d 34 5d 2e  y65 = yymsp[-4].
30c91 6d 69 6e 6f 72 2e 79 79 36 35 3b 0a 20 20 20 20  minor.yy65;.    
30c92 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 53 65 6c  }else{.      Sel
30c93 65 63 74 20 2a 70 53 75 62 71 75 65 72 79 3b 0a  ect *pSubquery;.
30c94 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 72 63        sqlite3Src
30c95 4c 69 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70  ListShiftJoinTyp
30c96 65 28 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  e(yymsp[-4].mino
30c97 72 2e 79 79 36 35 29 3b 0a 20 20 20 20 20 20 70  r.yy65);.      p
30c98 53 75 62 71 75 65 72 79 20 3d 20 73 71 6c 69 74  Subquery = sqlit
30c99 65 33 53 65 6c 65 63 74 4e 65 77 28 70 50 61 72  e3SelectNew(pPar
30c9a 73 65 2c 30 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d  se,0,yymsp[-4].m
30c9b 69 6e 6f 72 2e 79 79 36 35 2c 30 2c 30 2c 30 2c  inor.yy65,0,0,0,
30c9c 30 2c 30 2c 30 2c 30 29 3b 0a 20 20 20 20 20 20  0,0,0,0);.      
30c9d 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35  yygotominor.yy65
30c9e 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73   = sqlite3SrcLis
30c9f 74 41 70 70 65 6e 64 46 72 6f 6d 54 65 72 6d 28  tAppendFromTerm(
30ca0 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 36 5d  pParse,yymsp[-6]
30ca1 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 30 2c 30 2c  .minor.yy65,0,0,
30ca2 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  &yymsp[-2].minor
30ca3 2e 79 79 30 2c 70 53 75 62 71 75 65 72 79 2c 79  .yy0,pSubquery,y
30ca4 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
30ca5 79 31 33 32 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69  y132,yymsp[0].mi
30ca6 6e 6f 72 2e 79 79 34 30 38 29 3b 0a 20 20 20 20  nor.yy408);.    
30ca7 7d 0a 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72  }.  }.        br
30ca8 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
30ca9 31 33 36 3a 20 2f 2a 20 64 62 6e 6d 20 3a 3a 3d  136: /* dbnm ::=
30caa 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
30cab 34 35 3a 20 2f 2a 20 69 6e 64 65 78 65 64 5f 6f  45: /* indexed_o
30cac 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74  pt ::= */ yytest
30cad 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31  case(yyruleno==1
30cae 34 35 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  45);.{yygotomino
30caf 72 2e 79 79 30 2e 7a 3d 30 3b 20 79 79 67 6f 74  r.yy0.z=0; yygot
30cb0 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3d 30 3b 7d  ominor.yy0.n=0;}
30cb1 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
30cb2 20 20 20 20 20 20 63 61 73 65 20 31 33 38 3a 20        case 138: 
30cb3 2f 2a 20 66 75 6c 6c 6e 61 6d 65 20 3a 3a 3d 20  /* fullname ::= 
30cb4 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 79 79 67 6f  nm dbnm */.{yygo
30cb5 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 20 3d 20 73  tominor.yy65 = s
30cb6 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70  qlite3SrcListApp
30cb7 65 6e 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 30  end(pParse->db,0
30cb8 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  ,&yymsp[-1].mino
30cb9 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e  r.yy0,&yymsp[0].
30cba 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20  minor.yy0);}.   
30cbb 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
30cbc 20 20 63 61 73 65 20 31 33 39 3a 20 2f 2a 20 6a    case 139: /* j
30cbd 6f 69 6e 6f 70 20 3a 3a 3d 20 43 4f 4d 4d 41 7c  oinop ::= COMMA|
30cbe 4a 4f 49 4e 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f  JOIN */.{ yygoto
30cbf 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 4a 54  minor.yy328 = JT
30cc0 5f 49 4e 4e 45 52 3b 20 7d 0a 20 20 20 20 20 20  _INNER; }.      
30cc1 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
30cc2 61 73 65 20 31 34 30 3a 20 2f 2a 20 6a 6f 69 6e  ase 140: /* join
30cc3 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 4a  op ::= JOIN_KW J
30cc4 4f 49 4e 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d  OIN */.{ yygotom
30cc5 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 73 71 6c  inor.yy328 = sql
30cc6 69 74 65 33 4a 6f 69 6e 54 79 70 65 28 70 50 61  ite3JoinType(pPa
30cc7 72 73 65 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d  rse,&yymsp[-1].m
30cc8 69 6e 6f 72 2e 79 79 30 2c 30 2c 30 29 3b 20 7d  inor.yy0,0,0); }
30cc9 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
30cca 20 20 20 20 20 20 63 61 73 65 20 31 34 31 3a 20        case 141: 
30ccb 2f 2a 20 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f  /* joinop ::= JO
30ccc 49 4e 5f 4b 57 20 6e 6d 20 4a 4f 49 4e 20 2a 2f  IN_KW nm JOIN */
30ccd 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .{ yygotominor.y
30cce 79 33 32 38 20 3d 20 73 71 6c 69 74 65 33 4a 6f  y328 = sqlite3Jo
30ccf 69 6e 54 79 70 65 28 70 50 61 72 73 65 2c 26 79  inType(pParse,&y
30cd0 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
30cd1 79 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  y0,&yymsp[-1].mi
30cd2 6e 6f 72 2e 79 79 30 2c 30 29 3b 20 7d 0a 20 20  nor.yy0,0); }.  
30cd3 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
30cd4 20 20 20 63 61 73 65 20 31 34 32 3a 20 2f 2a 20     case 142: /* 
30cd5 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f  joinop ::= JOIN_
30cd6 4b 57 20 6e 6d 20 6e 6d 20 4a 4f 49 4e 20 2a 2f  KW nm nm JOIN */
30cd7 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .{ yygotominor.y
30cd8 79 33 32 38 20 3d 20 73 71 6c 69 74 65 33 4a 6f  y328 = sqlite3Jo
30cd9 69 6e 54 79 70 65 28 70 50 61 72 73 65 2c 26 79  inType(pParse,&y
30cda 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
30cdb 79 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  y0,&yymsp[-2].mi
30cdc 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d  nor.yy0,&yymsp[-
30cdd 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 20 7d  1].minor.yy0); }
30cde 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
30cdf 20 20 20 20 20 20 63 61 73 65 20 31 34 33 3a 20        case 143: 
30ce0 2f 2a 20 6f 6e 5f 6f 70 74 20 3a 3a 3d 20 4f 4e  /* on_opt ::= ON
30ce1 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63   expr */.      c
30ce2 61 73 65 20 31 35 34 3a 20 2f 2a 20 73 6f 72 74  ase 154: /* sort
30ce3 69 74 65 6d 20 3a 3a 3d 20 65 78 70 72 20 2a 2f  item ::= expr */
30ce4 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
30ce5 6c 65 6e 6f 3d 3d 31 35 34 29 3b 0a 20 20 20 20  leno==154);.    
30ce6 20 20 63 61 73 65 20 31 36 31 3a 20 2f 2a 20 68    case 161: /* h
30ce7 61 76 69 6e 67 5f 6f 70 74 20 3a 3a 3d 20 48 41  aving_opt ::= HA
30ce8 56 49 4e 47 20 65 78 70 72 20 2a 2f 20 79 79 74  VING expr */ yyt
30ce9 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
30cea 3d 3d 31 36 31 29 3b 0a 20 20 20 20 20 20 63 61  ==161);.      ca
30ceb 73 65 20 31 36 38 3a 20 2f 2a 20 77 68 65 72 65  se 168: /* where
30cec 5f 6f 70 74 20 3a 3a 3d 20 57 48 45 52 45 20 65  _opt ::= WHERE e
30ced 78 70 72 20 2a 2f 20 79 79 74 65 73 74 63 61 73  xpr */ yytestcas
30cee 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 36 38 29  e(yyruleno==168)
30cef 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 33 35  ;.      case 235
30cf0 3a 20 2f 2a 20 63 61 73 65 5f 65 6c 73 65 20 3a  : /* case_else :
30cf1 3a 3d 20 45 4c 53 45 20 65 78 70 72 20 2a 2f 20  := ELSE expr */ 
30cf2 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
30cf3 65 6e 6f 3d 3d 32 33 35 29 3b 0a 20 20 20 20 20  eno==235);.     
30cf4 20 63 61 73 65 20 32 33 37 3a 20 2f 2a 20 63 61   case 237: /* ca
30cf5 73 65 5f 6f 70 65 72 61 6e 64 20 3a 3a 3d 20 65  se_operand ::= e
30cf6 78 70 72 20 2a 2f 20 79 79 74 65 73 74 63 61 73  xpr */ yytestcas
30cf7 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 33 37 29  e(yyruleno==237)
30cf8 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  ;.{yygotominor.y
30cf9 79 31 33 32 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  y132 = yymsp[0].
30cfa 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
30cfb 72 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  r;}.        brea
30cfc 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 34  k;.      case 14
30cfd 34 3a 20 2f 2a 20 6f 6e 5f 6f 70 74 20 3a 3a 3d  4: /* on_opt ::=
30cfe 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
30cff 36 30 3a 20 2f 2a 20 68 61 76 69 6e 67 5f 6f 70  60: /* having_op
30d00 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63  t ::= */ yytestc
30d01 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 36  ase(yyruleno==16
30d02 30 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  0);.      case 1
30d03 36 37 3a 20 2f 2a 20 77 68 65 72 65 5f 6f 70 74  67: /* where_opt
30d04 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61   ::= */ yytestca
30d05 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 36 37  se(yyruleno==167
30d06 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 33  );.      case 23
30d07 36 3a 20 2f 2a 20 63 61 73 65 5f 65 6c 73 65 20  6: /* case_else 
30d08 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ::= */ yytestcas
30d09 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 33 36 29  e(yyruleno==236)
30d0a 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 33 38  ;.      case 238
30d0b 3a 20 2f 2a 20 63 61 73 65 5f 6f 70 65 72 61 6e  : /* case_operan
30d0c 64 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63  d ::= */ yytestc
30d0d 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 33  ase(yyruleno==23
30d0e 38 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72  8);.{yygotominor
30d0f 2e 79 79 31 33 32 20 3d 20 30 3b 7d 0a 20 20 20  .yy132 = 0;}.   
30d10 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
30d11 20 20 63 61 73 65 20 31 34 37 3a 20 2f 2a 20 69    case 147: /* i
30d12 6e 64 65 78 65 64 5f 6f 70 74 20 3a 3a 3d 20 4e  ndexed_opt ::= N
30d13 4f 54 20 49 4e 44 45 58 45 44 20 2a 2f 0a 7b 79  OT INDEXED */.{y
30d14 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a  ygotominor.yy0.z
30d15 3d 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  =0; yygotominor.
30d16 79 79 30 2e 6e 3d 31 3b 7d 0a 20 20 20 20 20 20  yy0.n=1;}.      
30d17 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
30d18 61 73 65 20 31 34 38 3a 20 2f 2a 20 75 73 69 6e  ase 148: /* usin
30d19 67 5f 6f 70 74 20 3a 3a 3d 20 55 53 49 4e 47 20  g_opt ::= USING 
30d1a 4c 50 20 69 6e 73 63 6f 6c 6c 69 73 74 20 52 50  LP inscollist RP
30d1b 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
30d1c 38 30 3a 20 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73  80: /* inscollis
30d1d 74 5f 6f 70 74 20 3a 3a 3d 20 4c 50 20 69 6e 73  t_opt ::= LP ins
30d1e 63 6f 6c 6c 69 73 74 20 52 50 20 2a 2f 20 79 79  collist RP */ yy
30d1f 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
30d20 6f 3d 3d 31 38 30 29 3b 0a 7b 79 79 67 6f 74 6f  o==180);.{yygoto
30d21 6d 69 6e 6f 72 2e 79 79 34 30 38 20 3d 20 79 79  minor.yy408 = yy
30d22 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
30d23 34 30 38 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  408;}.        br
30d24 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
30d25 31 34 39 3a 20 2f 2a 20 75 73 69 6e 67 5f 6f 70  149: /* using_op
30d26 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63  t ::= */.      c
30d27 61 73 65 20 31 37 39 3a 20 2f 2a 20 69 6e 73 63  ase 179: /* insc
30d28 6f 6c 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 2a  ollist_opt ::= *
30d29 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
30d2a 75 6c 65 6e 6f 3d 3d 31 37 39 29 3b 0a 7b 79 79  uleno==179);.{yy
30d2b 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 30 38 20  gotominor.yy408 
30d2c 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  = 0;}.        br
30d2d 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
30d2e 31 35 31 3a 20 2f 2a 20 6f 72 64 65 72 62 79 5f  151: /* orderby_
30d2f 6f 70 74 20 3a 3a 3d 20 4f 52 44 45 52 20 42 59  opt ::= ORDER BY
30d30 20 73 6f 72 74 6c 69 73 74 20 2a 2f 0a 20 20 20   sortlist */.   
30d31 20 20 20 63 61 73 65 20 31 35 39 3a 20 2f 2a 20     case 159: /* 
30d32 67 72 6f 75 70 62 79 5f 6f 70 74 20 3a 3a 3d 20  groupby_opt ::= 
30d33 47 52 4f 55 50 20 42 59 20 6e 65 78 70 72 6c 69  GROUP BY nexprli
30d34 73 74 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  st */ yytestcase
30d35 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 35 39 29 3b  (yyruleno==159);
30d36 0a 20 20 20 20 20 20 63 61 73 65 20 32 33 39 3a  .      case 239:
30d37 20 2f 2a 20 65 78 70 72 6c 69 73 74 20 3a 3a 3d   /* exprlist ::=
30d38 20 6e 65 78 70 72 6c 69 73 74 20 2a 2f 20 79 79   nexprlist */ yy
30d39 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
30d3a 6f 3d 3d 32 33 39 29 3b 0a 7b 79 79 67 6f 74 6f  o==239);.{yygoto
30d3b 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20 79 79 6d  minor.yy14 = yym
30d3c 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34  sp[0].minor.yy14
30d3d 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
30d3e 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 35 32  ;.      case 152
30d3f 3a 20 2f 2a 20 73 6f 72 74 6c 69 73 74 20 3a 3a  : /* sortlist ::
30d40 3d 20 73 6f 72 74 6c 69 73 74 20 43 4f 4d 4d 41  = sortlist COMMA
30d41 20 73 6f 72 74 69 74 65 6d 20 73 6f 72 74 6f 72   sortitem sortor
30d42 64 65 72 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74  der */.{.  yygot
30d43 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71  ominor.yy14 = sq
30d44 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
30d45 65 6e 64 28 70 50 61 72 73 65 2c 79 79 6d 73 70  end(pParse,yymsp
30d46 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c  [-3].minor.yy14,
30d47 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
30d48 79 79 31 33 32 29 3b 0a 20 20 69 66 28 20 79 79  yy132);.  if( yy
30d49 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 29  gotominor.yy14 )
30d4a 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
30d4b 34 2d 3e 61 5b 79 79 67 6f 74 6f 6d 69 6e 6f 72  4->a[yygotominor
30d4c 2e 79 79 31 34 2d 3e 6e 45 78 70 72 2d 31 5d 2e  .yy14->nExpr-1].
30d4d 73 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38 29  sortOrder = (u8)
30d4e 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
30d4f 79 33 32 38 3b 0a 7d 0a 20 20 20 20 20 20 20 20  y328;.}.        
30d50 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
30d51 65 20 31 35 33 3a 20 2f 2a 20 73 6f 72 74 6c 69  e 153: /* sortli
30d52 73 74 20 3a 3a 3d 20 73 6f 72 74 69 74 65 6d 20  st ::= sortitem 
30d53 73 6f 72 74 6f 72 64 65 72 20 2a 2f 0a 7b 0a 20  sortorder */.{. 
30d54 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
30d55 34 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  4 = sqlite3ExprL
30d56 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
30d57 2c 30 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  ,0,yymsp[-1].min
30d58 6f 72 2e 79 79 31 33 32 29 3b 0a 20 20 69 66 28  or.yy132);.  if(
30d59 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
30d5a 34 20 26 26 20 41 4c 57 41 59 53 28 79 79 67 6f  4 && ALWAYS(yygo
30d5b 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2d 3e 61 29  tominor.yy14->a)
30d5c 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79   ) yygotominor.y
30d5d 79 31 34 2d 3e 61 5b 30 5d 2e 73 6f 72 74 4f 72  y14->a[0].sortOr
30d5e 64 65 72 20 3d 20 28 75 38 29 79 79 6d 73 70 5b  der = (u8)yymsp[
30d5f 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 3b 0a  0].minor.yy328;.
30d60 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
30d61 0a 20 20 20 20 20 20 63 61 73 65 20 31 35 35 3a  .      case 155:
30d62 20 2f 2a 20 73 6f 72 74 6f 72 64 65 72 20 3a 3a   /* sortorder ::
30d63 3d 20 41 53 43 20 2a 2f 0a 20 20 20 20 20 20 63  = ASC */.      c
30d64 61 73 65 20 31 35 37 3a 20 2f 2a 20 73 6f 72 74  ase 157: /* sort
30d65 6f 72 64 65 72 20 3a 3a 3d 20 2a 2f 20 79 79 74  order ::= */ yyt
30d66 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
30d67 3d 3d 31 35 37 29 3b 0a 7b 79 79 67 6f 74 6f 6d  ==157);.{yygotom
30d68 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 53 51 4c  inor.yy328 = SQL
30d69 49 54 45 5f 53 4f 5f 41 53 43 3b 7d 0a 20 20 20  ITE_SO_ASC;}.   
30d6a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
30d6b 20 20 63 61 73 65 20 31 35 36 3a 20 2f 2a 20 73    case 156: /* s
30d6c 6f 72 74 6f 72 64 65 72 20 3a 3a 3d 20 44 45 53  ortorder ::= DES
30d6d 43 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  C */.{yygotomino
30d6e 72 2e 79 79 33 32 38 20 3d 20 53 51 4c 49 54 45  r.yy328 = SQLITE
30d6f 5f 53 4f 5f 44 45 53 43 3b 7d 0a 20 20 20 20 20  _SO_DESC;}.     
30d70 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
30d71 63 61 73 65 20 31 36 32 3a 20 2f 2a 20 6c 69 6d  case 162: /* lim
30d72 69 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 7b 79  it_opt ::= */.{y
30d73 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 37 36  ygotominor.yy476
30d74 2e 70 4c 69 6d 69 74 20 3d 20 30 3b 20 79 79 67  .pLimit = 0; yyg
30d75 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 37 36 2e 70  otominor.yy476.p
30d76 4f 66 66 73 65 74 20 3d 20 30 3b 7d 0a 20 20 20  Offset = 0;}.   
30d77 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
30d78 20 20 63 61 73 65 20 31 36 33 3a 20 2f 2a 20 6c    case 163: /* l
30d79 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c 49 4d  imit_opt ::= LIM
30d7a 49 54 20 65 78 70 72 20 2a 2f 0a 7b 79 79 67 6f  IT expr */.{yygo
30d7b 74 6f 6d 69 6e 6f 72 2e 79 79 34 37 36 2e 70 4c  tominor.yy476.pL
30d7c 69 6d 69 74 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  imit = yymsp[0].
30d7d 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
30d7e 72 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  r; yygotominor.y
30d7f 79 34 37 36 2e 70 4f 66 66 73 65 74 20 3d 20 30  y476.pOffset = 0
30d80 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
30d81 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 36 34  ;.      case 164
30d82 3a 20 2f 2a 20 6c 69 6d 69 74 5f 6f 70 74 20 3a  : /* limit_opt :
30d83 3a 3d 20 4c 49 4d 49 54 20 65 78 70 72 20 4f 46  := LIMIT expr OF
30d84 46 53 45 54 20 65 78 70 72 20 2a 2f 0a 7b 79 79  FSET expr */.{yy
30d85 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 37 36 2e  gotominor.yy476.
30d86 70 4c 69 6d 69 74 20 3d 20 79 79 6d 73 70 5b 2d  pLimit = yymsp[-
30d87 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  2].minor.yy346.p
30d88 45 78 70 72 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f  Expr; yygotomino
30d89 72 2e 79 79 34 37 36 2e 70 4f 66 66 73 65 74 20  r.yy476.pOffset 
30d8a 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  = yymsp[0].minor
30d8b 2e 79 79 33 34 36 2e 70 45 78 70 72 3b 7d 0a 20  .yy346.pExpr;}. 
30d8c 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
30d8d 20 20 20 20 63 61 73 65 20 31 36 35 3a 20 2f 2a      case 165: /*
30d8e 20 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c   limit_opt ::= L
30d8f 49 4d 49 54 20 65 78 70 72 20 43 4f 4d 4d 41 20  IMIT expr COMMA 
30d90 65 78 70 72 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  expr */.{yygotom
30d91 69 6e 6f 72 2e 79 79 34 37 36 2e 70 4f 66 66 73  inor.yy476.pOffs
30d92 65 74 20 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d  et = yymsp[-2].m
30d93 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
30d94 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ; yygotominor.yy
30d95 34 37 36 2e 70 4c 69 6d 69 74 20 3d 20 79 79 6d  476.pLimit = yym
30d96 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  sp[0].minor.yy34
30d97 36 2e 70 45 78 70 72 3b 7d 0a 20 20 20 20 20 20  6.pExpr;}.      
30d98 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
30d99 61 73 65 20 31 36 36 3a 20 2f 2a 20 63 6d 64 20  ase 166: /* cmd 
30d9a 3a 3a 3d 20 44 45 4c 45 54 45 20 46 52 4f 4d 20  ::= DELETE FROM 
30d9b 66 75 6c 6c 6e 61 6d 65 20 69 6e 64 65 78 65 64  fullname indexed
30d9c 5f 6f 70 74 20 77 68 65 72 65 5f 6f 70 74 20 2a  _opt where_opt *
30d9d 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 53 72 63  /.{.  sqlite3Src
30d9e 4c 69 73 74 49 6e 64 65 78 65 64 42 79 28 70 50  ListIndexedBy(pP
30d9f 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e  arse, yymsp[-2].
30da0 6d 69 6e 6f 72 2e 79 79 36 35 2c 20 26 79 79 6d  minor.yy65, &yym
30da1 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-1].minor.yy0
30da2 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 6c 65  );.  sqlite3Dele
30da3 74 65 46 72 6f 6d 28 70 50 61 72 73 65 2c 79 79  teFrom(pParse,yy
30da4 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
30da5 36 35 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  65,yymsp[0].mino
30da6 72 2e 79 79 31 33 32 29 3b 0a 7d 0a 20 20 20 20  r.yy132);.}.    
30da7 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
30da8 20 63 61 73 65 20 31 36 39 3a 20 2f 2a 20 63 6d   case 169: /* cm
30da9 64 20 3a 3a 3d 20 55 50 44 41 54 45 20 6f 72 63  d ::= UPDATE orc
30daa 6f 6e 66 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 64  onf fullname ind
30dab 65 78 65 64 5f 6f 70 74 20 53 45 54 20 73 65 74  exed_opt SET set
30dac 6c 69 73 74 20 77 68 65 72 65 5f 6f 70 74 20 2a  list where_opt *
30dad 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 53 72 63  /.{.  sqlite3Src
30dae 4c 69 73 74 49 6e 64 65 78 65 64 42 79 28 70 50  ListIndexedBy(pP
30daf 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e  arse, yymsp[-4].
30db0 6d 69 6e 6f 72 2e 79 79 36 35 2c 20 26 79 79 6d  minor.yy65, &yym
30db1 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-3].minor.yy0
30db2 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
30db3 4c 69 73 74 43 68 65 63 6b 4c 65 6e 67 74 68 28  ListCheckLength(
30db4 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 31 5d  pParse,yymsp[-1]
30db5 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 22 73 65 74  .minor.yy14,"set
30db6 20 6c 69 73 74 22 29 3b 20 0a 20 20 73 71 6c 69   list"); .  sqli
30db7 74 65 33 55 70 64 61 74 65 28 70 50 61 72 73 65  te3Update(pParse
30db8 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72  ,yymsp[-4].minor
30db9 2e 79 79 36 35 2c 79 79 6d 73 70 5b 2d 31 5d 2e  .yy65,yymsp[-1].
30dba 6d 69 6e 6f 72 2e 79 79 31 34 2c 79 79 6d 73 70  minor.yy14,yymsp
30dbb 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33 32 2c  [0].minor.yy132,
30dbc 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e  yymsp[-5].minor.
30dbd 79 79 31 38 36 29 3b 0a 7d 0a 20 20 20 20 20 20  yy186);.}.      
30dbe 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
30dbf 61 73 65 20 31 37 30 3a 20 2f 2a 20 73 65 74 6c  ase 170: /* setl
30dc0 69 73 74 20 3a 3a 3d 20 73 65 74 6c 69 73 74 20  ist ::= setlist 
30dc1 43 4f 4d 4d 41 20 6e 6d 20 45 51 20 65 78 70 72  COMMA nm EQ expr
30dc2 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69   */.{.  yygotomi
30dc3 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c 69 74  nor.yy14 = sqlit
30dc4 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
30dc5 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d  (pParse, yymsp[-
30dc6 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 79  4].minor.yy14, y
30dc7 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
30dc8 33 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 73 71  346.pExpr);.  sq
30dc9 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53 65 74  lite3ExprListSet
30dca 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 79 79 67  Name(pParse, yyg
30dcb 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 26  otominor.yy14, &
30dcc 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
30dcd 79 79 30 2c 20 31 29 3b 0a 7d 0a 20 20 20 20 20  yy0, 1);.}.     
30dce 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
30dcf 63 61 73 65 20 31 37 31 3a 20 2f 2a 20 73 65 74  case 171: /* set
30dd0 6c 69 73 74 20 3a 3a 3d 20 6e 6d 20 45 51 20 65  list ::= nm EQ e
30dd1 78 70 72 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74  xpr */.{.  yygot
30dd2 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71  ominor.yy14 = sq
30dd3 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
30dd4 65 6e 64 28 70 50 61 72 73 65 2c 20 30 2c 20 79  end(pParse, 0, y
30dd5 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
30dd6 33 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 73 71  346.pExpr);.  sq
30dd7 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53 65 74  lite3ExprListSet
30dd8 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 79 79 67  Name(pParse, yyg
30dd9 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 26  otominor.yy14, &
30dda 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
30ddb 79 79 30 2c 20 31 29 3b 0a 7d 0a 20 20 20 20 20  yy0, 1);.}.     
30ddc 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
30ddd 63 61 73 65 20 31 37 32 3a 20 2f 2a 20 63 6d 64  case 172: /* cmd
30dde 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20   ::= insert_cmd 
30ddf 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e  INTO fullname in
30de0 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 56 41 4c  scollist_opt VAL
30de1 55 45 53 20 4c 50 20 69 74 65 6d 6c 69 73 74 20  UES LP itemlist 
30de2 52 50 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 49 6e  RP */.{sqlite3In
30de3 73 65 72 74 28 70 50 61 72 73 65 2c 20 79 79 6d  sert(pParse, yym
30de4 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 36  sp[-5].minor.yy6
30de5 35 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  5, yymsp[-1].min
30de6 6f 72 2e 79 79 31 34 2c 20 30 2c 20 79 79 6d 73  or.yy14, 0, yyms
30de7 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30  p[-4].minor.yy40
30de8 38 2c 20 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e  8, yymsp[-7].min
30de9 6f 72 2e 79 79 31 38 36 29 3b 7d 0a 20 20 20 20  or.yy186);}.    
30dea 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
30deb 20 63 61 73 65 20 31 37 33 3a 20 2f 2a 20 63 6d   case 173: /* cm
30dec 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64  d ::= insert_cmd
30ded 20 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65 20 69   INTO fullname i
30dee 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 73 65  nscollist_opt se
30def 6c 65 63 74 20 2a 2f 0a 7b 73 71 6c 69 74 65 33  lect */.{sqlite3
30df0 49 6e 73 65 72 74 28 70 50 61 72 73 65 2c 20 79  Insert(pParse, y
30df1 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
30df2 79 36 35 2c 20 30 2c 20 79 79 6d 73 70 5b 30 5d  y65, 0, yymsp[0]
30df3 2e 6d 69 6e 6f 72 2e 79 79 33 2c 20 79 79 6d 73  .minor.yy3, yyms
30df4 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30  p[-1].minor.yy40
30df5 38 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e  8, yymsp[-4].min
30df6 6f 72 2e 79 79 31 38 36 29 3b 7d 0a 20 20 20 20  or.yy186);}.    
30df7 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
30df8 20 63 61 73 65 20 31 37 34 3a 20 2f 2a 20 63 6d   case 174: /* cm
30df9 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64  d ::= insert_cmd
30dfa 20 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65 20 69   INTO fullname i
30dfb 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 44 45  nscollist_opt DE
30dfc 46 41 55 4c 54 20 56 41 4c 55 45 53 20 2a 2f 0a  FAULT VALUES */.
30dfd 7b 73 71 6c 69 74 65 33 49 6e 73 65 72 74 28 70  {sqlite3Insert(p
30dfe 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 33 5d  Parse, yymsp[-3]
30dff 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 20 30 2c 20  .minor.yy65, 0, 
30e00 30 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  0, yymsp[-2].min
30e01 6f 72 2e 79 79 34 30 38 2c 20 79 79 6d 73 70 5b  or.yy408, yymsp[
30e02 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 36 29  -5].minor.yy186)
30e03 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
30e04 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 37 35  ;.      case 175
30e05 3a 20 2f 2a 20 69 6e 73 65 72 74 5f 63 6d 64 20  : /* insert_cmd 
30e06 3a 3a 3d 20 49 4e 53 45 52 54 20 6f 72 63 6f 6e  ::= INSERT orcon
30e07 66 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  f */.{yygotomino
30e08 72 2e 79 79 31 38 36 20 3d 20 79 79 6d 73 70 5b  r.yy186 = yymsp[
30e09 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 36 3b 7d  0].minor.yy186;}
30e0a 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
30e0b 20 20 20 20 20 20 63 61 73 65 20 31 37 36 3a 20        case 176: 
30e0c 2f 2a 20 69 6e 73 65 72 74 5f 63 6d 64 20 3a 3a  /* insert_cmd ::
30e0d 3d 20 52 45 50 4c 41 43 45 20 2a 2f 0a 7b 79 79  = REPLACE */.{yy
30e0e 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 38 36 20  gotominor.yy186 
30e0f 3d 20 4f 45 5f 52 65 70 6c 61 63 65 3b 7d 0a 20  = OE_Replace;}. 
30e10 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
30e11 20 20 20 20 63 61 73 65 20 31 37 37 3a 20 2f 2a      case 177: /*
30e12 20 69 74 65 6d 6c 69 73 74 20 3a 3a 3d 20 69 74   itemlist ::= it
30e13 65 6d 6c 69 73 74 20 43 4f 4d 4d 41 20 65 78 70  emlist COMMA exp
30e14 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  r */.      case 
30e15 32 34 31 3a 20 2f 2a 20 6e 65 78 70 72 6c 69 73  241: /* nexprlis
30e16 74 20 3a 3a 3d 20 6e 65 78 70 72 6c 69 73 74 20  t ::= nexprlist 
30e17 43 4f 4d 4d 41 20 65 78 70 72 20 2a 2f 20 79 79  COMMA expr */ yy
30e18 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
30e19 6f 3d 3d 32 34 31 29 3b 0a 7b 79 79 67 6f 74 6f  o==241);.{yygoto
30e1a 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c  minor.yy14 = sql
30e1b 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
30e1c 6e 64 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b  nd(pParse,yymsp[
30e1d 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 79  -2].minor.yy14,y
30e1e 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
30e1f 33 34 36 2e 70 45 78 70 72 29 3b 7d 0a 20 20 20  346.pExpr);}.   
30e20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
30e21 20 20 63 61 73 65 20 31 37 38 3a 20 2f 2a 20 69    case 178: /* i
30e22 74 65 6d 6c 69 73 74 20 3a 3a 3d 20 65 78 70 72  temlist ::= expr
30e23 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32   */.      case 2
30e24 34 32 3a 20 2f 2a 20 6e 65 78 70 72 6c 69 73 74  42: /* nexprlist
30e25 20 3a 3a 3d 20 65 78 70 72 20 2a 2f 20 79 79 74   ::= expr */ yyt
30e26 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
30e27 3d 3d 32 34 32 29 3b 0a 7b 79 79 67 6f 74 6f 6d  ==242);.{yygotom
30e28 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c 69  inor.yy14 = sqli
30e29 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
30e2a 64 28 70 50 61 72 73 65 2c 30 2c 79 79 6d 73 70  d(pParse,0,yymsp
30e2b 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  [0].minor.yy346.
30e2c 70 45 78 70 72 29 3b 7d 0a 20 20 20 20 20 20 20  pExpr);}.       
30e2d 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
30e2e 73 65 20 31 38 31 3a 20 2f 2a 20 69 6e 73 63 6f  se 181: /* insco
30e2f 6c 6c 69 73 74 20 3a 3a 3d 20 69 6e 73 63 6f 6c  llist ::= inscol
30e30 6c 69 73 74 20 43 4f 4d 4d 41 20 6e 6d 20 2a 2f  list COMMA nm */
30e31 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
30e32 34 30 38 20 3d 20 73 71 6c 69 74 65 33 49 64 4c  408 = sqlite3IdL
30e33 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
30e34 2d 3e 64 62 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d  ->db,yymsp[-2].m
30e35 69 6e 6f 72 2e 79 79 34 30 38 2c 26 79 79 6d 73  inor.yy408,&yyms
30e36 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
30e37 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
30e38 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 32 3a  .      case 182:
30e39 20 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73 74 20 3a   /* inscollist :
30e3a 3a 3d 20 6e 6d 20 2a 2f 0a 7b 79 79 67 6f 74 6f  := nm */.{yygoto
30e3b 6d 69 6e 6f 72 2e 79 79 34 30 38 20 3d 20 73 71  minor.yy408 = sq
30e3c 6c 69 74 65 33 49 64 4c 69 73 74 41 70 70 65 6e  lite3IdListAppen
30e3d 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 30 2c 26  d(pParse->db,0,&
30e3e 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
30e3f 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  y0);}.        br
30e40 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
30e41 31 38 33 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  183: /* expr ::=
30e42 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20 20 20 63   term */.      c
30e43 61 73 65 20 32 31 31 3a 20 2f 2a 20 65 73 63 61  ase 211: /* esca
30e44 70 65 20 3a 3a 3d 20 45 53 43 41 50 45 20 65 78  pe ::= ESCAPE ex
30e45 70 72 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  pr */ yytestcase
30e46 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 31 31 29 3b  (yyruleno==211);
30e47 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
30e48 33 34 36 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d  346 = yymsp[0].m
30e49 69 6e 6f 72 2e 79 79 33 34 36 3b 7d 0a 20 20 20  inor.yy346;}.   
30e4a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
30e4b 20 20 63 61 73 65 20 31 38 34 3a 20 2f 2a 20 65    case 184: /* e
30e4c 78 70 72 20 3a 3a 3d 20 4c 50 20 65 78 70 72 20  xpr ::= LP expr 
30e4d 52 50 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  RP */.{yygotomin
30e4e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d  or.yy346.pExpr =
30e4f 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
30e50 2e 79 79 33 34 36 2e 70 45 78 70 72 3b 20 73 70  .yy346.pExpr; sp
30e51 61 6e 53 65 74 28 26 79 79 67 6f 74 6f 6d 69 6e  anSet(&yygotomin
30e52 6f 72 2e 79 79 33 34 36 2c 26 79 79 6d 73 70 5b  or.yy346,&yymsp[
30e53 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -2].minor.yy0,&y
30e54 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
30e55 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
30e56 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
30e57 38 35 3a 20 2f 2a 20 74 65 72 6d 20 3a 3a 3d 20  85: /* term ::= 
30e58 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 63 61  NULL */.      ca
30e59 73 65 20 31 39 30 3a 20 2f 2a 20 74 65 72 6d 20  se 190: /* term 
30e5a 3a 3a 3d 20 49 4e 54 45 47 45 52 7c 46 4c 4f 41  ::= INTEGER|FLOA
30e5b 54 7c 42 4c 4f 42 20 2a 2f 20 79 79 74 65 73 74  T|BLOB */ yytest
30e5c 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31  case(yyruleno==1
30e5d 39 30 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  90);.      case 
30e5e 31 39 31 3a 20 2f 2a 20 74 65 72 6d 20 3a 3a 3d  191: /* term ::=
30e5f 20 53 54 52 49 4e 47 20 2a 2f 20 79 79 74 65 73   STRING */ yytes
30e60 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
30e61 31 39 31 29 3b 0a 7b 73 70 61 6e 45 78 70 72 28  191);.{spanExpr(
30e62 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  &yygotominor.yy3
30e63 34 36 2c 20 70 50 61 72 73 65 2c 20 79 79 6d 73  46, pParse, yyms
30e64 70 5b 30 5d 2e 6d 61 6a 6f 72 2c 20 26 79 79 6d  p[0].major, &yym
30e65 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
30e66 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
30e67 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 36  ;.      case 186
30e68 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 69 64  : /* expr ::= id
30e69 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
30e6a 38 37 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  87: /* expr ::= 
30e6b 4a 4f 49 4e 5f 4b 57 20 2a 2f 20 79 79 74 65 73  JOIN_KW */ yytes
30e6c 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
30e6d 31 38 37 29 3b 0a 7b 73 70 61 6e 45 78 70 72 28  187);.{spanExpr(
30e6e 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  &yygotominor.yy3
30e6f 34 36 2c 20 70 50 61 72 73 65 2c 20 54 4b 5f 49  46, pParse, TK_I
30e70 44 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  D, &yymsp[0].min
30e71 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20  or.yy0);}.      
30e72 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
30e73 61 73 65 20 31 38 38 3a 20 2f 2a 20 65 78 70 72  ase 188: /* expr
30e74 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d 20 2a   ::= nm DOT nm *
30e75 2f 0a 7b 0a 20 20 45 78 70 72 20 2a 74 65 6d 70  /.{.  Expr *temp
30e76 31 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  1 = sqlite3PExpr
30e77 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20  (pParse, TK_ID, 
30e78 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d  0, 0, &yymsp[-2]
30e79 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 45  .minor.yy0);.  E
30e7a 78 70 72 20 2a 74 65 6d 70 32 20 3d 20 73 71 6c  xpr *temp2 = sql
30e7b 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
30e7c 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26  , TK_ID, 0, 0, &
30e7d 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
30e7e 79 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  y0);.  yygotomin
30e7f 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d  or.yy346.pExpr =
30e80 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
30e81 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 74 65  arse, TK_DOT, te
30e82 6d 70 31 2c 20 74 65 6d 70 32 2c 20 30 29 3b 0a  mp1, temp2, 0);.
30e83 20 20 73 70 61 6e 53 65 74 28 26 79 79 67 6f 74    spanSet(&yygot
30e84 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 26 79 79  ominor.yy346,&yy
30e85 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
30e86 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  0,&yymsp[0].mino
30e87 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20  r.yy0);.}.      
30e88 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
30e89 61 73 65 20 31 38 39 3a 20 2f 2a 20 65 78 70 72  ase 189: /* expr
30e8a 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d 20 44   ::= nm DOT nm D
30e8b 4f 54 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 45 78 70  OT nm */.{.  Exp
30e8c 72 20 2a 74 65 6d 70 31 20 3d 20 73 71 6c 69 74  r *temp1 = sqlit
30e8d 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
30e8e 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79 79  TK_ID, 0, 0, &yy
30e8f 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
30e90 30 29 3b 0a 20 20 45 78 70 72 20 2a 74 65 6d 70  0);.  Expr *temp
30e91 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  2 = sqlite3PExpr
30e92 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20  (pParse, TK_ID, 
30e93 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d  0, 0, &yymsp[-2]
30e94 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 45  .minor.yy0);.  E
30e95 78 70 72 20 2a 74 65 6d 70 33 20 3d 20 73 71 6c  xpr *temp3 = sql
30e96 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
30e97 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26  , TK_ID, 0, 0, &
30e98 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
30e99 79 30 29 3b 0a 20 20 45 78 70 72 20 2a 74 65 6d  y0);.  Expr *tem
30e9a 70 34 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  p4 = sqlite3PExp
30e9b 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54  r(pParse, TK_DOT
30e9c 2c 20 74 65 6d 70 32 2c 20 74 65 6d 70 33 2c 20  , temp2, temp3, 
30e9d 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  0);.  yygotomino
30e9e 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20  r.yy346.pExpr = 
30e9f 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
30ea0 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 74 65 6d  rse, TK_DOT, tem
30ea1 70 31 2c 20 74 65 6d 70 34 2c 20 30 29 3b 0a 20  p1, temp4, 0);. 
30ea2 20 73 70 61 6e 53 65 74 28 26 79 79 67 6f 74 6f   spanSet(&yygoto
30ea3 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 26 79 79 6d  minor.yy346,&yym
30ea4 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-4].minor.yy0
30ea5 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
30ea6 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20  .yy0);.}.       
30ea7 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
30ea8 73 65 20 31 39 32 3a 20 2f 2a 20 65 78 70 72 20  se 192: /* expr 
30ea9 3a 3a 3d 20 52 45 47 49 53 54 45 52 20 2a 2f 0a  ::= REGISTER */.
30eaa 7b 0a 20 20 2f 2a 20 57 68 65 6e 20 64 6f 69 6e  {.  /* When doin
30eab 67 20 61 20 6e 65 73 74 65 64 20 70 61 72 73 65  g a nested parse
30eac 2c 20 6f 6e 65 20 63 61 6e 20 69 6e 63 6c 75 64  , one can includ
30ead 65 20 74 65 72 6d 73 20 69 6e 20 61 6e 20 65 78  e terms in an ex
30eae 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2a 20 74 68  pression.  ** th
30eaf 61 74 20 6c 6f 6f 6b 20 6c 69 6b 65 20 74 68 69  at look like thi
30eb0 73 3a 20 20 20 23 31 20 23 32 20 2e 2e 2e 20 20  s:   #1 #2 ...  
30eb1 54 68 65 73 65 20 74 65 72 6d 73 20 72 65 66 65  These terms refe
30eb2 72 20 74 6f 20 72 65 67 69 73 74 65 72 73 0a 20  r to registers. 
30eb3 20 2a 2a 20 69 6e 20 74 68 65 20 76 69 72 74 75   ** in the virtu
30eb4 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 23 4e 20  al machine.  #N 
30eb5 69 73 20 74 68 65 20 4e 2d 74 68 20 72 65 67 69  is the N-th regi
30eb6 73 74 65 72 2e 20 2a 2f 0a 20 20 69 66 28 20 70  ster. */.  if( p
30eb7 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30  Parse->nested==0
30eb8 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
30eb9 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
30eba 22 6e 65 61 72 20 5c 22 25 54 5c 22 3a 20 73 79  "near \"%T\": sy
30ebb 6e 74 61 78 20 65 72 72 6f 72 22 2c 20 26 79 79  ntax error", &yy
30ebc 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
30ebd 29 3b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e  );.    yygotomin
30ebe 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d  or.yy346.pExpr =
30ebf 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
30ec0 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
30ec1 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74  46.pExpr = sqlit
30ec2 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
30ec3 54 4b 5f 52 45 47 49 53 54 45 52 2c 20 30 2c 20  TK_REGISTER, 0, 
30ec4 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  0, &yymsp[0].min
30ec5 6f 72 2e 79 79 30 29 3b 0a 20 20 20 20 69 66 28  or.yy0);.    if(
30ec6 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
30ec7 34 36 2e 70 45 78 70 72 20 29 20 73 71 6c 69 74  46.pExpr ) sqlit
30ec8 65 33 47 65 74 49 6e 74 33 32 28 26 79 79 6d 73  e3GetInt32(&yyms
30ec9 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  p[0].minor.yy0.z
30eca 5b 31 5d 2c 20 26 79 79 67 6f 74 6f 6d 69 6e 6f  [1], &yygotomino
30ecb 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2d 3e 69  r.yy346.pExpr->i
30ecc 54 61 62 6c 65 29 3b 0a 20 20 7d 0a 20 20 73 70  Table);.  }.  sp
30ecd 61 6e 53 65 74 28 26 79 79 67 6f 74 6f 6d 69 6e  anSet(&yygotomin
30ece 6f 72 2e 79 79 33 34 36 2c 20 26 79 79 6d 73 70  or.yy346, &yymsp
30ecf 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26  [0].minor.yy0, &
30ed0 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
30ed1 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  y0);.}.        b
30ed2 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
30ed3 20 31 39 33 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   193: /* expr ::
30ed4 3d 20 56 41 52 49 41 42 4c 45 20 2a 2f 0a 7b 0a  = VARIABLE */.{.
30ed5 20 20 73 70 61 6e 45 78 70 72 28 26 79 79 67 6f    spanExpr(&yygo
30ed6 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 20 70  tominor.yy346, p
30ed7 50 61 72 73 65 2c 20 54 4b 5f 56 41 52 49 41 42  Parse, TK_VARIAB
30ed8 4c 45 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  LE, &yymsp[0].mi
30ed9 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 73 71 6c 69  nor.yy0);.  sqli
30eda 74 65 33 45 78 70 72 41 73 73 69 67 6e 56 61 72  te3ExprAssignVar
30edb 4e 75 6d 62 65 72 28 70 50 61 72 73 65 2c 20 79  Number(pParse, y
30edc 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
30edd 2e 70 45 78 70 72 29 3b 0a 20 20 73 70 61 6e 53  .pExpr);.  spanS
30ede 65 74 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  et(&yygotominor.
30edf 79 79 33 34 36 2c 20 26 79 79 6d 73 70 5b 30 5d  yy346, &yymsp[0]
30ee0 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d  .minor.yy0, &yym
30ee1 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
30ee2 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
30ee3 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 39  k;.      case 19
30ee4 34 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65  4: /* expr ::= e
30ee5 78 70 72 20 43 4f 4c 4c 41 54 45 20 69 64 73 20  xpr COLLATE ids 
30ee6 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  */.{.  yygotomin
30ee7 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d  or.yy346.pExpr =
30ee8 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 43   sqlite3ExprSetC
30ee9 6f 6c 6c 28 70 50 61 72 73 65 2c 20 79 79 6d 73  oll(pParse, yyms
30eea 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  p[-2].minor.yy34
30eeb 36 2e 70 45 78 70 72 2c 20 26 79 79 6d 73 70 5b  6.pExpr, &yymsp[
30eec 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20  0].minor.yy0);. 
30eed 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
30eee 34 36 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d 73  46.zStart = yyms
30eef 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  p[-2].minor.yy34
30ef0 36 2e 7a 53 74 61 72 74 3b 0a 20 20 79 79 67 6f  6.zStart;.  yygo
30ef1 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 45  tominor.yy346.zE
30ef2 6e 64 20 3d 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  nd = &yymsp[0].m
30ef3 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70  inor.yy0.z[yymsp
30ef4 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d  [0].minor.yy0.n]
30ef5 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
30ef6 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 39  k;.      case 19
30ef7 35 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 43  5: /* expr ::= C
30ef8 41 53 54 20 4c 50 20 65 78 70 72 20 41 53 20 74  AST LP expr AS t
30ef9 79 70 65 74 6f 6b 65 6e 20 52 50 20 2a 2f 0a 7b  ypetoken RP */.{
30efa 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
30efb 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c  y346.pExpr = sql
30efc 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
30efd 2c 20 54 4b 5f 43 41 53 54 2c 20 79 79 6d 73 70  , TK_CAST, yymsp
30efe 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36  [-3].minor.yy346
30eff 2e 70 45 78 70 72 2c 20 30 2c 20 26 79 79 6d 73  .pExpr, 0, &yyms
30f00 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  p[-1].minor.yy0)
30f01 3b 0a 20 20 73 70 61 6e 53 65 74 28 26 79 79 67  ;.  spanSet(&yyg
30f02 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 26  otominor.yy346,&
30f03 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e  yymsp[-5].minor.
30f04 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  yy0,&yymsp[0].mi
30f05 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20  nor.yy0);.}.    
30f06 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
30f07 20 63 61 73 65 20 31 39 36 3a 20 2f 2a 20 65 78   case 196: /* ex
30f08 70 72 20 3a 3a 3d 20 49 44 20 4c 50 20 64 69 73  pr ::= ID LP dis
30f09 74 69 6e 63 74 20 65 78 70 72 6c 69 73 74 20 52  tinct exprlist R
30f0a 50 20 2a 2f 0a 7b 0a 20 20 69 66 28 20 79 79 6d  P */.{.  if( yym
30f0b 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-1].minor.yy1
30f0c 34 20 26 26 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  4 && yymsp[-1].m
30f0d 69 6e 6f 72 2e 79 79 31 34 2d 3e 6e 45 78 70 72  inor.yy14->nExpr
30f0e 3e 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 4c 69  >pParse->db->aLi
30f0f 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
30f10 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 5d 20 29  _FUNCTION_ARG] )
30f11 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
30f12 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74  orMsg(pParse, "t
30f13 6f 6f 20 6d 61 6e 79 20 61 72 67 75 6d 65 6e 74  oo many argument
30f14 73 20 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 25 54  s on function %T
30f15 22 2c 20 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  ", &yymsp[-4].mi
30f16 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 7d 0a 20 20  nor.yy0);.  }.  
30f17 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
30f18 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65  6.pExpr = sqlite
30f19 33 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 70 50  3ExprFunction(pP
30f1a 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e  arse, yymsp[-1].
30f1b 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 26 79 79 6d  minor.yy14, &yym
30f1c 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-4].minor.yy0
30f1d 29 3b 0a 20 20 73 70 61 6e 53 65 74 28 26 79 79  );.  spanSet(&yy
30f1e 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c  gotominor.yy346,
30f1f 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72  &yymsp[-4].minor
30f20 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  .yy0,&yymsp[0].m
30f21 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 69 66 28  inor.yy0);.  if(
30f22 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72   yymsp[-2].minor
30f23 2e 79 79 33 32 38 20 26 26 20 79 79 67 6f 74 6f  .yy328 && yygoto
30f24 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
30f25 72 20 29 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d  r ){.    yygotom
30f26 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
30f27 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 44 69  ->flags |= EP_Di
30f28 73 74 69 6e 63 74 3b 0a 20 20 7d 0a 7d 0a 20 20  stinct;.  }.}.  
30f29 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
30f2a 20 20 20 63 61 73 65 20 31 39 37 3a 20 2f 2a 20     case 197: /* 
30f2b 65 78 70 72 20 3a 3a 3d 20 49 44 20 4c 50 20 53  expr ::= ID LP S
30f2c 54 41 52 20 52 50 20 2a 2f 0a 7b 0a 20 20 79 79  TAR RP */.{.  yy
30f2d 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
30f2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 45  pExpr = sqlite3E
30f2f 78 70 72 46 75 6e 63 74 69 6f 6e 28 70 50 61 72  xprFunction(pPar
30f30 73 65 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d 33  se, 0, &yymsp[-3
30f31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20  ].minor.yy0);.  
30f32 73 70 61 6e 53 65 74 28 26 79 79 67 6f 74 6f 6d  spanSet(&yygotom
30f33 69 6e 6f 72 2e 79 79 33 34 36 2c 26 79 79 6d 73  inor.yy346,&yyms
30f34 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-3].minor.yy0,
30f35 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
30f36 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  yy0);.}.        
30f37 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
30f38 65 20 31 39 38 3a 20 2f 2a 20 74 65 72 6d 20 3a  e 198: /* term :
30f39 3a 3d 20 43 54 49 4d 45 5f 4b 57 20 2a 2f 0a 7b  := CTIME_KW */.{
30f3a 0a 20 20 2f 2a 20 54 68 65 20 43 55 52 52 45 4e  .  /* The CURREN
30f3b 54 5f 54 49 4d 45 2c 20 43 55 52 52 45 4e 54 5f  T_TIME, CURRENT_
30f3c 44 41 54 45 2c 20 61 6e 64 20 43 55 52 52 45 4e  DATE, and CURREN
30f3d 54 5f 54 49 4d 45 53 54 41 4d 50 20 76 61 6c 75  T_TIMESTAMP valu
30f3e 65 73 20 61 72 65 0a 20 20 2a 2a 20 74 72 65 61  es are.  ** trea
30f3f 74 65 64 20 61 73 20 66 75 6e 63 74 69 6f 6e 73  ted as functions
30f40 20 74 68 61 74 20 72 65 74 75 72 6e 20 63 6f 6e   that return con
30f41 73 74 61 6e 74 73 20 2a 2f 0a 20 20 79 79 67 6f  stants */.  yygo
30f42 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  tominor.yy346.pE
30f43 78 70 72 20 3d 20 73 71 6c 69 74 65 33 45 78 70  xpr = sqlite3Exp
30f44 72 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73 65  rFunction(pParse
30f45 2c 20 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  , 0,&yymsp[0].mi
30f46 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 69 66 28 20  nor.yy0);.  if( 
30f47 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
30f48 36 2e 70 45 78 70 72 20 29 7b 0a 20 20 20 20 79  6.pExpr ){.    y
30f49 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
30f4a 2e 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f  .pExpr->op = TK_
30f4b 43 4f 4e 53 54 5f 46 55 4e 43 3b 20 20 0a 20 20  CONST_FUNC;  .  
30f4c 7d 0a 20 20 73 70 61 6e 53 65 74 28 26 79 79 67  }.  spanSet(&yyg
30f4d 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 20  otominor.yy346, 
30f4e 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
30f4f 79 79 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  yy0, &yymsp[0].m
30f50 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20  inor.yy0);.}.   
30f51 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
30f52 20 20 63 61 73 65 20 31 39 39 3a 20 2f 2a 20 65    case 199: /* e
30f53 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 41 4e 44  xpr ::= expr AND
30f54 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63   expr */.      c
30f55 61 73 65 20 32 30 30 3a 20 2f 2a 20 65 78 70 72  ase 200: /* expr
30f56 20 3a 3a 3d 20 65 78 70 72 20 4f 52 20 65 78 70   ::= expr OR exp
30f57 72 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  r */ yytestcase(
30f58 79 79 72 75 6c 65 6e 6f 3d 3d 32 30 30 29 3b 0a  yyruleno==200);.
30f59 20 20 20 20 20 20 63 61 73 65 20 32 30 31 3a 20        case 201: 
30f5a 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  /* expr ::= expr
30f5b 20 4c 54 7c 47 54 7c 47 45 7c 4c 45 20 65 78 70   LT|GT|GE|LE exp
30f5c 72 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  r */ yytestcase(
30f5d 79 79 72 75 6c 65 6e 6f 3d 3d 32 30 31 29 3b 0a  yyruleno==201);.
30f5e 20 20 20 20 20 20 63 61 73 65 20 32 30 32 3a 20        case 202: 
30f5f 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  /* expr ::= expr
30f60 20 45 51 7c 4e 45 20 65 78 70 72 20 2a 2f 20 79   EQ|NE expr */ y
30f61 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
30f62 6e 6f 3d 3d 32 30 32 29 3b 0a 20 20 20 20 20 20  no==202);.      
30f63 63 61 73 65 20 32 30 33 3a 20 2f 2a 20 65 78 70  case 203: /* exp
30f64 72 20 3a 3a 3d 20 65 78 70 72 20 42 49 54 41 4e  r ::= expr BITAN
30f65 44 7c 42 49 54 4f 52 7c 4c 53 48 49 46 54 7c 52  D|BITOR|LSHIFT|R
30f66 53 48 49 46 54 20 65 78 70 72 20 2a 2f 20 79 79  SHIFT expr */ yy
30f67 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
30f68 6f 3d 3d 32 30 33 29 3b 0a 20 20 20 20 20 20 63  o==203);.      c
30f69 61 73 65 20 32 30 34 3a 20 2f 2a 20 65 78 70 72  ase 204: /* expr
30f6a 20 3a 3a 3d 20 65 78 70 72 20 50 4c 55 53 7c 4d   ::= expr PLUS|M
30f6b 49 4e 55 53 20 65 78 70 72 20 2a 2f 20 79 79 74  INUS expr */ yyt
30f6c 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
30f6d 3d 3d 32 30 34 29 3b 0a 20 20 20 20 20 20 63 61  ==204);.      ca
30f6e 73 65 20 32 30 35 3a 20 2f 2a 20 65 78 70 72 20  se 205: /* expr 
30f6f 3a 3a 3d 20 65 78 70 72 20 53 54 41 52 7c 53 4c  ::= expr STAR|SL
30f70 41 53 48 7c 52 45 4d 20 65 78 70 72 20 2a 2f 20  ASH|REM expr */ 
30f71 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
30f72 65 6e 6f 3d 3d 32 30 35 29 3b 0a 20 20 20 20 20  eno==205);.     
30f73 20 63 61 73 65 20 32 30 36 3a 20 2f 2a 20 65 78   case 206: /* ex
30f74 70 72 20 3a 3a 3d 20 65 78 70 72 20 43 4f 4e 43  pr ::= expr CONC
30f75 41 54 20 65 78 70 72 20 2a 2f 20 79 79 74 65 73  AT expr */ yytes
30f76 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
30f77 32 30 36 29 3b 0a 7b 73 70 61 6e 42 69 6e 61 72  206);.{spanBinar
30f78 79 45 78 70 72 28 26 79 79 67 6f 74 6f 6d 69 6e  yExpr(&yygotomin
30f79 6f 72 2e 79 79 33 34 36 2c 70 50 61 72 73 65 2c  or.yy346,pParse,
30f7a 79 79 6d 73 70 5b 2d 31 5d 2e 6d 61 6a 6f 72 2c  yymsp[-1].major,
30f7b 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  &yymsp[-2].minor
30f7c 2e 79 79 33 34 36 2c 26 79 79 6d 73 70 5b 30 5d  .yy346,&yymsp[0]
30f7d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 29 3b 7d 0a  .minor.yy346);}.
30f7e 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
30f7f 20 20 20 20 20 63 61 73 65 20 32 30 37 3a 20 2f       case 207: /
30f80 2a 20 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4c 49 4b  * likeop ::= LIK
30f81 45 5f 4b 57 20 2a 2f 0a 20 20 20 20 20 20 63 61  E_KW */.      ca
30f82 73 65 20 32 30 39 3a 20 2f 2a 20 6c 69 6b 65 6f  se 209: /* likeo
30f83 70 20 3a 3a 3d 20 4d 41 54 43 48 20 2a 2f 20 79  p ::= MATCH */ y
30f84 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
30f85 6e 6f 3d 3d 32 30 39 29 3b 0a 7b 79 79 67 6f 74  no==209);.{yygot
30f86 6f 6d 69 6e 6f 72 2e 79 79 39 36 2e 65 4f 70 65  ominor.yy96.eOpe
30f87 72 61 74 6f 72 20 3d 20 79 79 6d 73 70 5b 30 5d  rator = yymsp[0]
30f88 2e 6d 69 6e 6f 72 2e 79 79 30 3b 20 79 79 67 6f  .minor.yy0; yygo
30f89 74 6f 6d 69 6e 6f 72 2e 79 79 39 36 2e 6e 6f 74  tominor.yy96.not
30f8a 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62   = 0;}.        b
30f8b 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
30f8c 20 32 30 38 3a 20 2f 2a 20 6c 69 6b 65 6f 70 20   208: /* likeop 
30f8d 3a 3a 3d 20 4e 4f 54 20 4c 49 4b 45 5f 4b 57 20  ::= NOT LIKE_KW 
30f8e 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 31  */.      case 21
30f8f 30 3a 20 2f 2a 20 6c 69 6b 65 6f 70 20 3a 3a 3d  0: /* likeop ::=
30f90 20 4e 4f 54 20 4d 41 54 43 48 20 2a 2f 20 79 79   NOT MATCH */ yy
30f91 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
30f92 6f 3d 3d 32 31 30 29 3b 0a 7b 79 79 67 6f 74 6f  o==210);.{yygoto
30f93 6d 69 6e 6f 72 2e 79 79 39 36 2e 65 4f 70 65 72  minor.yy96.eOper
30f94 61 74 6f 72 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  ator = yymsp[0].
30f95 6d 69 6e 6f 72 2e 79 79 30 3b 20 79 79 67 6f 74  minor.yy0; yygot
30f96 6f 6d 69 6e 6f 72 2e 79 79 39 36 2e 6e 6f 74 20  ominor.yy96.not 
30f97 3d 20 31 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  = 1;}.        br
30f98 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
30f99 32 31 32 3a 20 2f 2a 20 65 73 63 61 70 65 20 3a  212: /* escape :
30f9a 3a 3d 20 2a 2f 0a 7b 6d 65 6d 73 65 74 28 26 79  := */.{memset(&y
30f9b 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
30f9c 2c 30 2c 73 69 7a 65 6f 66 28 79 79 67 6f 74 6f  ,0,sizeof(yygoto
30f9d 6d 69 6e 6f 72 2e 79 79 33 34 36 29 29 3b 7d 0a  minor.yy346));}.
30f9e 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
30f9f 20 20 20 20 20 63 61 73 65 20 32 31 33 3a 20 2f       case 213: /
30fa0 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20  * expr ::= expr 
30fa1 6c 69 6b 65 6f 70 20 65 78 70 72 20 65 73 63 61  likeop expr esca
30fa2 70 65 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 4c 69  pe */.{.  ExprLi
30fa3 73 74 20 2a 70 4c 69 73 74 3b 0a 20 20 70 4c 69  st *pList;.  pLi
30fa4 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  st = sqlite3Expr
30fa5 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
30fa6 65 2c 30 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  e,0, yymsp[-1].m
30fa7 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
30fa8 29 3b 0a 20 20 70 4c 69 73 74 20 3d 20 73 71 6c  );.  pList = sql
30fa9 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
30faa 6e 64 28 70 50 61 72 73 65 2c 70 4c 69 73 74 2c  nd(pParse,pList,
30fab 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72   yymsp[-3].minor
30fac 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b 0a 20  .yy346.pExpr);. 
30fad 20 69 66 28 20 79 79 6d 73 70 5b 30 5d 2e 6d 69   if( yymsp[0].mi
30fae 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20  nor.yy346.pExpr 
30faf 29 7b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 73  ){.    pList = s
30fb0 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
30fb1 70 65 6e 64 28 70 50 61 72 73 65 2c 70 4c 69 73  pend(pParse,pLis
30fb2 74 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  t, yymsp[0].mino
30fb3 72 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b 0a  r.yy346.pExpr);.
30fb4 20 20 7d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f    }.  yygotomino
30fb5 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20  r.yy346.pExpr = 
30fb6 73 71 6c 69 74 65 33 45 78 70 72 46 75 6e 63 74  sqlite3ExprFunct
30fb7 69 6f 6e 28 70 50 61 72 73 65 2c 20 70 4c 69 73  ion(pParse, pLis
30fb8 74 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  t, &yymsp[-2].mi
30fb9 6e 6f 72 2e 79 79 39 36 2e 65 4f 70 65 72 61 74  nor.yy96.eOperat
30fba 6f 72 29 3b 0a 20 20 69 66 28 20 79 79 6d 73 70  or);.  if( yymsp
30fbb 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 39 36 2e  [-2].minor.yy96.
30fbc 6e 6f 74 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f  not ) yygotomino
30fbd 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20  r.yy346.pExpr = 
30fbe 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
30fbf 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79 67  rse, TK_NOT, yyg
30fc0 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
30fc1 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 79  Expr, 0, 0);.  y
30fc2 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
30fc3 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d 73 70 5b  .zStart = yymsp[
30fc4 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  -3].minor.yy346.
30fc5 7a 53 74 61 72 74 3b 0a 20 20 79 79 67 6f 74 6f  zStart;.  yygoto
30fc6 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 45 6e 64  minor.yy346.zEnd
30fc7 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e   = yymsp[-1].min
30fc8 6f 72 2e 79 79 33 34 36 2e 7a 45 6e 64 3b 0a 20  or.yy346.zEnd;. 
30fc9 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72   if( yygotominor
30fca 2e 79 79 33 34 36 2e 70 45 78 70 72 20 29 20 79  .yy346.pExpr ) y
30fcb 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
30fcc 2e 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 7c 3d  .pExpr->flags |=
30fcd 20 45 50 5f 49 6e 66 69 78 46 75 6e 63 3b 0a 7d   EP_InfixFunc;.}
30fce 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
30fcf 20 20 20 20 20 20 63 61 73 65 20 32 31 34 3a 20        case 214: 
30fd0 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  /* expr ::= expr
30fd1 20 49 53 4e 55 4c 4c 7c 4e 4f 54 4e 55 4c 4c 20   ISNULL|NOTNULL 
30fd2 2a 2f 0a 7b 73 70 61 6e 55 6e 61 72 79 50 6f 73  */.{spanUnaryPos
30fd3 74 66 69 78 28 26 79 79 67 6f 74 6f 6d 69 6e 6f  tfix(&yygotomino
30fd4 72 2e 79 79 33 34 36 2c 70 50 61 72 73 65 2c 79  r.yy346,pParse,y
30fd5 79 6d 73 70 5b 30 5d 2e 6d 61 6a 6f 72 2c 26 79  ymsp[0].major,&y
30fd6 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
30fd7 79 33 34 36 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  y346,&yymsp[0].m
30fd8 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20  inor.yy0);}.    
30fd9 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
30fda 20 63 61 73 65 20 32 31 35 3a 20 2f 2a 20 65 78   case 215: /* ex
30fdb 70 72 20 3a 3a 3d 20 65 78 70 72 20 49 53 20 4e  pr ::= expr IS N
30fdc 55 4c 4c 20 2a 2f 0a 7b 73 70 61 6e 55 6e 61 72  ULL */.{spanUnar
30fdd 79 50 6f 73 74 66 69 78 28 26 79 79 67 6f 74 6f  yPostfix(&yygoto
30fde 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 70 50 61 72  minor.yy346,pPar
30fdf 73 65 2c 54 4b 5f 49 53 4e 55 4c 4c 2c 26 79 79  se,TK_ISNULL,&yy
30fe0 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
30fe1 33 34 36 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  346,&yymsp[0].mi
30fe2 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20  nor.yy0);}.     
30fe3 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
30fe4 63 61 73 65 20 32 31 36 3a 20 2f 2a 20 65 78 70  case 216: /* exp
30fe5 72 20 3a 3a 3d 20 65 78 70 72 20 4e 4f 54 20 4e  r ::= expr NOT N
30fe6 55 4c 4c 20 2a 2f 0a 7b 73 70 61 6e 55 6e 61 72  ULL */.{spanUnar
30fe7 79 50 6f 73 74 66 69 78 28 26 79 79 67 6f 74 6f  yPostfix(&yygoto
30fe8 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 70 50 61 72  minor.yy346,pPar
30fe9 73 65 2c 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c 26 79  se,TK_NOTNULL,&y
30fea 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
30feb 79 33 34 36 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  y346,&yymsp[0].m
30fec 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20  inor.yy0);}.    
30fed 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
30fee 20 63 61 73 65 20 32 31 37 3a 20 2f 2a 20 65 78   case 217: /* ex
30fef 70 72 20 3a 3a 3d 20 65 78 70 72 20 49 53 20 4e  pr ::= expr IS N
30ff0 4f 54 20 4e 55 4c 4c 20 2a 2f 0a 7b 73 70 61 6e  OT NULL */.{span
30ff1 55 6e 61 72 79 50 6f 73 74 66 69 78 28 26 79 79  UnaryPostfix(&yy
30ff2 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c  gotominor.yy346,
30ff3 70 50 61 72 73 65 2c 54 4b 5f 4e 4f 54 4e 55 4c  pParse,TK_NOTNUL
30ff4 4c 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  L,&yymsp[-3].min
30ff5 6f 72 2e 79 79 33 34 36 2c 26 79 79 6d 73 70 5b  or.yy346,&yymsp[
30ff6 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a  0].minor.yy0);}.
30ff7 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
30ff8 20 20 20 20 20 63 61 73 65 20 32 31 38 3a 20 2f       case 218: /
30ff9 2a 20 65 78 70 72 20 3a 3a 3d 20 4e 4f 54 20 65  * expr ::= NOT e
30ffa 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  xpr */.      cas
30ffb 65 20 32 31 39 3a 20 2f 2a 20 65 78 70 72 20 3a  e 219: /* expr :
30ffc 3a 3d 20 42 49 54 4e 4f 54 20 65 78 70 72 20 2a  := BITNOT expr *
30ffd 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
30ffe 75 6c 65 6e 6f 3d 3d 32 31 39 29 3b 0a 7b 73 70  uleno==219);.{sp
30fff 61 6e 55 6e 61 72 79 50 72 65 66 69 78 28 26 79  anUnaryPrefix(&y
31000 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
31001 2c 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 31  ,pParse,yymsp[-1
31002 5d 2e 6d 61 6a 6f 72 2c 26 79 79 6d 73 70 5b 30  ].major,&yymsp[0
31003 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 26 79  ].minor.yy346,&y
31004 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
31005 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  y0);}.        br
31006 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
31007 32 32 30 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  220: /* expr ::=
31008 20 4d 49 4e 55 53 20 65 78 70 72 20 2a 2f 0a 7b   MINUS expr */.{
31009 73 70 61 6e 55 6e 61 72 79 50 72 65 66 69 78 28  spanUnaryPrefix(
3100a 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  &yygotominor.yy3
3100b 34 36 2c 70 50 61 72 73 65 2c 54 4b 5f 55 4d 49  46,pParse,TK_UMI
3100c 4e 55 53 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  NUS,&yymsp[0].mi
3100d 6e 6f 72 2e 79 79 33 34 36 2c 26 79 79 6d 73 70  nor.yy346,&yymsp
3100e 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  [-1].minor.yy0);
3100f 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
31010 0a 20 20 20 20 20 20 63 61 73 65 20 32 32 31 3a  .      case 221:
31011 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 50 4c 55   /* expr ::= PLU
31012 53 20 65 78 70 72 20 2a 2f 0a 7b 73 70 61 6e 55  S expr */.{spanU
31013 6e 61 72 79 50 72 65 66 69 78 28 26 79 79 67 6f  naryPrefix(&yygo
31014 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 70 50  tominor.yy346,pP
31015 61 72 73 65 2c 54 4b 5f 55 50 4c 55 53 2c 26 79  arse,TK_UPLUS,&y
31016 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
31017 33 34 36 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d  346,&yymsp[-1].m
31018 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20  inor.yy0);}.    
31019 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3101a 20 63 61 73 65 20 32 32 34 3a 20 2f 2a 20 65 78   case 224: /* ex
3101b 70 72 20 3a 3a 3d 20 65 78 70 72 20 62 65 74 77  pr ::= expr betw
3101c 65 65 6e 5f 6f 70 20 65 78 70 72 20 41 4e 44 20  een_op expr AND 
3101d 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 45 78 70 72  expr */.{.  Expr
3101e 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 73 71  List *pList = sq
3101f 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
31020 65 6e 64 28 70 50 61 72 73 65 2c 30 2c 20 79 79  end(pParse,0, yy
31021 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
31022 33 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 70 4c  346.pExpr);.  pL
31023 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ist = sqlite3Exp
31024 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  rListAppend(pPar
31025 73 65 2c 70 4c 69 73 74 2c 20 79 79 6d 73 70 5b  se,pList, yymsp[
31026 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  0].minor.yy346.p
31027 45 78 70 72 29 3b 0a 20 20 79 79 67 6f 74 6f 6d  Expr);.  yygotom
31028 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
31029 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
3102a 70 50 61 72 73 65 2c 20 54 4b 5f 42 45 54 57 45  pParse, TK_BETWE
3102b 45 4e 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  EN, yymsp[-4].mi
3102c 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2c  nor.yy346.pExpr,
3102d 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 79 79   0, 0);.  if( yy
3102e 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
3102f 70 45 78 70 72 20 29 7b 0a 20 20 20 20 79 79 67  pExpr ){.    yyg
31030 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
31031 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 20 3d 20  Expr->x.pList = 
31032 70 4c 69 73 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a  pList;.  }else{.
31033 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c      sqlite3ExprL
31034 69 73 74 44 65 6c 65 74 65 28 70 50 61 72 73 65  istDelete(pParse
31035 2d 3e 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20  ->db, pList);.  
31036 7d 20 0a 20 20 69 66 28 20 79 79 6d 73 70 5b 2d  } .  if( yymsp[-
31037 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 20 29  3].minor.yy328 )
31038 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
31039 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74  46.pExpr = sqlit
3103a 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
3103b 54 4b 5f 4e 4f 54 2c 20 79 79 67 6f 74 6f 6d 69  TK_NOT, yygotomi
3103c 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2c  nor.yy346.pExpr,
3103d 20 30 2c 20 30 29 3b 0a 20 20 79 79 67 6f 74 6f   0, 0);.  yygoto
3103e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61  minor.yy346.zSta
3103f 72 74 20 3d 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d  rt = yymsp[-4].m
31040 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61 72  inor.yy346.zStar
31041 74 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  t;.  yygotominor
31042 2e 79 79 33 34 36 2e 7a 45 6e 64 20 3d 20 79 79  .yy346.zEnd = yy
31043 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
31044 34 36 2e 7a 45 6e 64 3b 0a 7d 0a 20 20 20 20 20  46.zEnd;.}.     
31045 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
31046 63 61 73 65 20 32 32 37 3a 20 2f 2a 20 65 78 70  case 227: /* exp
31047 72 20 3a 3a 3d 20 65 78 70 72 20 69 6e 5f 6f 70  r ::= expr in_op
31048 20 4c 50 20 65 78 70 72 6c 69 73 74 20 52 50 20   LP exprlist RP 
31049 2a 2f 0a 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d  */.{.    yygotom
3104a 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
3104b 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
3104c 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e 2c 20 79  pParse, TK_IN, y
3104d 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
3104e 79 33 34 36 2e 70 45 78 70 72 2c 20 30 2c 20 30  y346.pExpr, 0, 0
3104f 29 3b 0a 20 20 20 20 69 66 28 20 79 79 67 6f 74  );.    if( yygot
31050 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  ominor.yy346.pEx
31051 70 72 20 29 7b 0a 20 20 20 20 20 20 79 79 67 6f  pr ){.      yygo
31052 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  tominor.yy346.pE
31053 78 70 72 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 79  xpr->x.pList = y
31054 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
31055 79 31 34 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  y14;.      sqlit
31056 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28  e3ExprSetHeight(
31057 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69  pParse, yygotomi
31058 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 29  nor.yy346.pExpr)
31059 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
3105a 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69     sqlite3ExprLi
3105b 73 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d  stDelete(pParse-
3105c 3e 64 62 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  >db, yymsp[-1].m
3105d 69 6e 6f 72 2e 79 79 31 34 29 3b 0a 20 20 20 20  inor.yy14);.    
3105e 7d 0a 20 20 20 20 69 66 28 20 79 79 6d 73 70 5b  }.    if( yymsp[
3105f 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 20  -3].minor.yy328 
31060 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ) yygotominor.yy
31061 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69  346.pExpr = sqli
31062 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
31063 20 54 4b 5f 4e 4f 54 2c 20 79 79 67 6f 74 6f 6d   TK_NOT, yygotom
31064 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
31065 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 79 79 67  , 0, 0);.    yyg
31066 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a  otominor.yy346.z
31067 53 74 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d 34  Start = yymsp[-4
31068 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53  ].minor.yy346.zS
31069 74 61 72 74 3b 0a 20 20 20 20 79 79 67 6f 74 6f  tart;.    yygoto
3106a 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 45 6e 64  minor.yy346.zEnd
3106b 20 3d 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e   = &yymsp[0].min
3106c 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b 30  or.yy0.z[yymsp[0
3106d 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 3b 0a  ].minor.yy0.n];.
3106e 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61    }.        brea
3106f 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 32  k;.      case 22
31070 38 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 4c  8: /* expr ::= L
31071 50 20 73 65 6c 65 63 74 20 52 50 20 2a 2f 0a 7b  P select RP */.{
31072 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .    yygotominor
31073 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73  .yy346.pExpr = s
31074 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
31075 73 65 2c 20 54 4b 5f 53 45 4c 45 43 54 2c 20 30  se, TK_SELECT, 0
31076 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  , 0, 0);.    if(
31077 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
31078 34 36 2e 70 45 78 70 72 20 29 7b 0a 20 20 20 20  46.pExpr ){.    
31079 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
3107a 33 34 36 2e 70 45 78 70 72 2d 3e 78 2e 70 53 65  346.pExpr->x.pSe
3107b 6c 65 63 74 20 3d 20 79 79 6d 73 70 5b 2d 31 5d  lect = yymsp[-1]
3107c 2e 6d 69 6e 6f 72 2e 79 79 33 3b 0a 20 20 20 20  .minor.yy3;.    
3107d 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74    ExprSetPropert
3107e 79 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  y(yygotominor.yy
3107f 33 34 36 2e 70 45 78 70 72 2c 20 45 50 5f 78 49  346.pExpr, EP_xI
31080 73 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20  sSelect);.      
31081 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65  sqlite3ExprSetHe
31082 69 67 68 74 28 70 50 61 72 73 65 2c 20 79 79 67  ight(pParse, yyg
31083 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
31084 45 78 70 72 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Expr);.    }else
31085 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53  {.      sqlite3S
31086 65 6c 65 63 74 44 65 6c 65 74 65 28 70 50 61 72  electDelete(pPar
31087 73 65 2d 3e 64 62 2c 20 79 79 6d 73 70 5b 2d 31  se->db, yymsp[-1
31088 5d 2e 6d 69 6e 6f 72 2e 79 79 33 29 3b 0a 20 20  ].minor.yy3);.  
31089 20 20 7d 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69    }.    yygotomi
3108a 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61 72 74  nor.yy346.zStart
3108b 20 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e   = yymsp[-2].min
3108c 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20 20 20 79 79  or.yy0.z;.    yy
3108d 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
3108e 7a 45 6e 64 20 3d 20 26 79 79 6d 73 70 5b 30 5d  zEnd = &yymsp[0]
3108f 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d  .minor.yy0.z[yym
31090 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  sp[0].minor.yy0.
31091 6e 5d 3b 0a 20 20 7d 0a 20 20 20 20 20 20 20 20  n];.  }.        
31092 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
31093 65 20 32 32 39 3a 20 2f 2a 20 65 78 70 72 20 3a  e 229: /* expr :
31094 3a 3d 20 65 78 70 72 20 69 6e 5f 6f 70 20 4c 50  := expr in_op LP
31095 20 73 65 6c 65 63 74 20 52 50 20 2a 2f 0a 7b 0a   select RP */.{.
31096 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e      yygotominor.
31097 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71  yy346.pExpr = sq
31098 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
31099 65 2c 20 54 4b 5f 49 4e 2c 20 79 79 6d 73 70 5b  e, TK_IN, yymsp[
3109a 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  -4].minor.yy346.
3109b 70 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20  pExpr, 0, 0);.  
3109c 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f    if( yygotomino
3109d 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 29 7b  r.yy346.pExpr ){
3109e 0a 20 20 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e  .      yygotomin
3109f 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2d 3e  or.yy346.pExpr->
310a0 78 2e 70 53 65 6c 65 63 74 20 3d 20 79 79 6d 73  x.pSelect = yyms
310a1 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 3b  p[-1].minor.yy3;
310a2 0a 20 20 20 20 20 20 45 78 70 72 53 65 74 50 72  .      ExprSetPr
310a3 6f 70 65 72 74 79 28 79 79 67 6f 74 6f 6d 69 6e  operty(yygotomin
310a4 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2c 20  or.yy346.pExpr, 
310a5 45 50 5f 78 49 73 53 65 6c 65 63 74 29 3b 0a 20  EP_xIsSelect);. 
310a6 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
310a7 53 65 74 48 65 69 67 68 74 28 70 50 61 72 73 65  SetHeight(pParse
310a8 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  , yygotominor.yy
310a9 33 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 20 20  346.pExpr);.    
310aa 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
310ab 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
310ac 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d  (pParse->db, yym
310ad 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-1].minor.yy3
310ae 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
310af 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72   yymsp[-3].minor
310b0 2e 79 79 33 32 38 20 29 20 79 79 67 6f 74 6f 6d  .yy328 ) yygotom
310b1 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
310b2 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
310b3 70 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20  pParse, TK_NOT, 
310b4 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
310b5 36 2e 70 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a  6.pExpr, 0, 0);.
310b6 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e      yygotominor.
310b7 79 79 33 34 36 2e 7a 53 74 61 72 74 20 3d 20 79  yy346.zStart = y
310b8 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
310b9 79 33 34 36 2e 7a 53 74 61 72 74 3b 0a 20 20 20  y346.zStart;.   
310ba 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
310bb 34 36 2e 7a 45 6e 64 20 3d 20 26 79 79 6d 73 70  46.zEnd = &yymsp
310bc 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b  [0].minor.yy0.z[
310bd 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
310be 79 30 2e 6e 5d 3b 0a 20 20 7d 0a 20 20 20 20 20  y0.n];.  }.     
310bf 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
310c0 63 61 73 65 20 32 33 30 3a 20 2f 2a 20 65 78 70  case 230: /* exp
310c1 72 20 3a 3a 3d 20 65 78 70 72 20 69 6e 5f 6f 70  r ::= expr in_op
310c2 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 0a 20 20   nm dbnm */.{.  
310c3 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 20    SrcList *pSrc 
310c4 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74  = sqlite3SrcList
310c5 41 70 70 65 6e 64 28 70 50 61 72 73 65 2d 3e 64  Append(pParse->d
310c6 62 2c 20 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e  b, 0,&yymsp[-1].
310c7 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70  minor.yy0,&yymsp
310c8 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  [0].minor.yy0);.
310c9 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e      yygotominor.
310ca 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71  yy346.pExpr = sq
310cb 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
310cc 65 2c 20 54 4b 5f 49 4e 2c 20 79 79 6d 73 70 5b  e, TK_IN, yymsp[
310cd 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  -3].minor.yy346.
310ce 70 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20  pExpr, 0, 0);.  
310cf 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f    if( yygotomino
310d0 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 29 7b  r.yy346.pExpr ){
310d1 0a 20 20 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e  .      yygotomin
310d2 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2d 3e  or.yy346.pExpr->
310d3 78 2e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69  x.pSelect = sqli
310d4 74 65 33 53 65 6c 65 63 74 4e 65 77 28 70 50 61  te3SelectNew(pPa
310d5 72 73 65 2c 20 30 2c 70 53 72 63 2c 30 2c 30 2c  rse, 0,pSrc,0,0,
310d6 30 2c 30 2c 30 2c 30 2c 30 29 3b 0a 20 20 20 20  0,0,0,0,0);.    
310d7 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74    ExprSetPropert
310d8 79 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  y(yygotominor.yy
310d9 33 34 36 2e 70 45 78 70 72 2c 20 45 50 5f 78 49  346.pExpr, EP_xI
310da 73 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20  sSelect);.      
310db 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65  sqlite3ExprSetHe
310dc 69 67 68 74 28 70 50 61 72 73 65 2c 20 79 79 67  ight(pParse, yyg
310dd 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
310de 45 78 70 72 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Expr);.    }else
310df 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53  {.      sqlite3S
310e0 72 63 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61  rcListDelete(pPa
310e1 72 73 65 2d 3e 64 62 2c 20 70 53 72 63 29 3b 0a  rse->db, pSrc);.
310e2 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 79 79      }.    if( yy
310e3 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
310e4 33 32 38 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f  328 ) yygotomino
310e5 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20  r.yy346.pExpr = 
310e6 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
310e7 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79 67  rse, TK_NOT, yyg
310e8 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
310e9 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20  Expr, 0, 0);.   
310ea 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
310eb 34 36 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d 73  46.zStart = yyms
310ec 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  p[-3].minor.yy34
310ed 36 2e 7a 53 74 61 72 74 3b 0a 20 20 20 20 79 79  6.zStart;.    yy
310ee 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
310ef 7a 45 6e 64 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  zEnd = yymsp[0].
310f0 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20 3f 20 26 79  minor.yy0.z ? &y
310f1 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
310f2 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  0.z[yymsp[0].min
310f3 6f 72 2e 79 79 30 2e 6e 5d 20 3a 20 26 79 79 6d  or.yy0.n] : &yym
310f4 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-1].minor.yy0
310f5 2e 7a 5b 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  .z[yymsp[-1].min
310f6 6f 72 2e 79 79 30 2e 6e 5d 3b 0a 20 20 7d 0a 20  or.yy0.n];.  }. 
310f7 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
310f8 20 20 20 20 63 61 73 65 20 32 33 31 3a 20 2f 2a      case 231: /*
310f9 20 65 78 70 72 20 3a 3a 3d 20 45 58 49 53 54 53   expr ::= EXISTS
310fa 20 4c 50 20 73 65 6c 65 63 74 20 52 50 20 2a 2f   LP select RP */
310fb 0a 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 20 3d  .{.    Expr *p =
310fc 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
310fd 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74  46.pExpr = sqlit
310fe 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
310ff 54 4b 5f 45 58 49 53 54 53 2c 20 30 2c 20 30 2c  TK_EXISTS, 0, 0,
31100 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 20 29   0);.    if( p )
31101 7b 0a 20 20 20 20 20 20 70 2d 3e 78 2e 70 53 65  {.      p->x.pSe
31102 6c 65 63 74 20 3d 20 79 79 6d 73 70 5b 2d 31 5d  lect = yymsp[-1]
31103 2e 6d 69 6e 6f 72 2e 79 79 33 3b 0a 20 20 20 20  .minor.yy3;.    
31104 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74    ExprSetPropert
31105 79 28 70 2c 20 45 50 5f 78 49 73 53 65 6c 65 63  y(p, EP_xIsSelec
31106 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  t);.      sqlite
31107 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28 70  3ExprSetHeight(p
31108 50 61 72 73 65 2c 20 70 29 3b 0a 20 20 20 20 7d  Parse, p);.    }
31109 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
3110a 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
3110b 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73  pParse->db, yyms
3110c 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 29  p[-1].minor.yy3)
3110d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 79 79 67 6f  ;.    }.    yygo
3110e 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53  tominor.yy346.zS
3110f 74 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d 33 5d  tart = yymsp[-3]
31110 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20  .minor.yy0.z;.  
31111 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
31112 33 34 36 2e 7a 45 6e 64 20 3d 20 26 79 79 6d 73  346.zEnd = &yyms
31113 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  p[0].minor.yy0.z
31114 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  [yymsp[0].minor.
31115 79 79 30 2e 6e 5d 3b 0a 20 20 7d 0a 20 20 20 20  yy0.n];.  }.    
31116 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
31117 20 63 61 73 65 20 32 33 32 3a 20 2f 2a 20 65 78   case 232: /* ex
31118 70 72 20 3a 3a 3d 20 43 41 53 45 20 63 61 73 65  pr ::= CASE case
31119 5f 6f 70 65 72 61 6e 64 20 63 61 73 65 5f 65 78  _operand case_ex
3111a 70 72 6c 69 73 74 20 63 61 73 65 5f 65 6c 73 65  prlist case_else
3111b 20 45 4e 44 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f   END */.{.  yygo
3111c 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  tominor.yy346.pE
3111d 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78  xpr = sqlite3PEx
3111e 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 43 41  pr(pParse, TK_CA
3111f 53 45 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  SE, yymsp[-3].mi
31120 6e 6f 72 2e 79 79 31 33 32 2c 20 79 79 6d 73 70  nor.yy132, yymsp
31121 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33 32  [-1].minor.yy132
31122 2c 20 30 29 3b 0a 20 20 69 66 28 20 79 79 67 6f  , 0);.  if( yygo
31123 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  tominor.yy346.pE
31124 78 70 72 20 29 7b 0a 20 20 20 20 79 79 67 6f 74  xpr ){.    yygot
31125 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  ominor.yy346.pEx
31126 70 72 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 79 79  pr->x.pList = yy
31127 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
31128 31 34 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  14;.    sqlite3E
31129 78 70 72 53 65 74 48 65 69 67 68 74 28 70 50 61  xprSetHeight(pPa
3112a 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  rse, yygotominor
3112b 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b 0a 20  .yy346.pExpr);. 
3112c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
3112d 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
3112e 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79  e(pParse->db, yy
3112f 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
31130 31 34 29 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74  14);.  }.  yygot
31131 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74  ominor.yy346.zSt
31132 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d 34 5d 2e  art = yymsp[-4].
31133 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20 79  minor.yy0.z;.  y
31134 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
31135 2e 7a 45 6e 64 20 3d 20 26 79 79 6d 73 70 5b 30  .zEnd = &yymsp[0
31136 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79  ].minor.yy0.z[yy
31137 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
31138 2e 6e 5d 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  .n];.}.        b
31139 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
3113a 20 32 33 33 3a 20 2f 2a 20 63 61 73 65 5f 65 78   233: /* case_ex
3113b 70 72 6c 69 73 74 20 3a 3a 3d 20 63 61 73 65 5f  prlist ::= case_
3113c 65 78 70 72 6c 69 73 74 20 57 48 45 4e 20 65 78  exprlist WHEN ex
3113d 70 72 20 54 48 45 4e 20 65 78 70 72 20 2a 2f 0a  pr THEN expr */.
3113e 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  {.  yygotominor.
3113f 79 79 31 34 20 3d 20 73 71 6c 69 74 65 33 45 78  yy14 = sqlite3Ex
31140 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
31141 72 73 65 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  rse,yymsp[-4].mi
31142 6e 6f 72 2e 79 79 31 34 2c 20 79 79 6d 73 70 5b  nor.yy14, yymsp[
31143 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  -2].minor.yy346.
31144 70 45 78 70 72 29 3b 0a 20 20 79 79 67 6f 74 6f  pExpr);.  yygoto
31145 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c  minor.yy14 = sql
31146 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
31147 6e 64 28 70 50 61 72 73 65 2c 79 79 67 6f 74 6f  nd(pParse,yygoto
31148 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 79 79 6d 73  minor.yy14, yyms
31149 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36  p[0].minor.yy346
3114a 2e 70 45 78 70 72 29 3b 0a 7d 0a 20 20 20 20 20  .pExpr);.}.     
3114b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3114c 63 61 73 65 20 32 33 34 3a 20 2f 2a 20 63 61 73  case 234: /* cas
3114d 65 5f 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 57  e_exprlist ::= W
3114e 48 45 4e 20 65 78 70 72 20 54 48 45 4e 20 65 78  HEN expr THEN ex
3114f 70 72 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f  pr */.{.  yygoto
31150 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c  minor.yy14 = sql
31151 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
31152 6e 64 28 70 50 61 72 73 65 2c 30 2c 20 79 79 6d  nd(pParse,0, yym
31153 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-2].minor.yy3
31154 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 79 79 67  46.pExpr);.  yyg
31155 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20  otominor.yy14 = 
31156 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
31157 70 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79 67  ppend(pParse,yyg
31158 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 79  otominor.yy14, y
31159 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
3115a 33 34 36 2e 70 45 78 70 72 29 3b 0a 7d 0a 20 20  346.pExpr);.}.  
3115b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3115c 20 20 20 63 61 73 65 20 32 34 33 3a 20 2f 2a 20     case 243: /* 
3115d 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77  cmd ::= createkw
3115e 20 75 6e 69 71 75 65 66 6c 61 67 20 49 4e 44 45   uniqueflag INDE
3115f 58 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d  X ifnotexists nm
31160 20 64 62 6e 6d 20 4f 4e 20 6e 6d 20 4c 50 20 69   dbnm ON nm LP i
31161 64 78 6c 69 73 74 20 52 50 20 2a 2f 0a 7b 0a 20  dxlist RP */.{. 
31162 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 6e   sqlite3CreateIn
31163 64 65 78 28 70 50 61 72 73 65 2c 20 26 79 79 6d  dex(pParse, &yym
31164 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-6].minor.yy0
31165 2c 20 26 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e  , &yymsp[-5].min
31166 6f 72 2e 79 79 30 2c 20 0a 20 20 20 20 20 20 20  or.yy0, .       
31167 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
31168 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65  lite3SrcListAppe
31169 6e 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 30 2c  nd(pParse->db,0,
3116a 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72  &yymsp[-3].minor
3116b 2e 79 79 30 2c 30 29 2c 20 79 79 6d 73 70 5b 2d  .yy0,0), yymsp[-
3116c 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 79  1].minor.yy14, y
3116d 79 6d 73 70 5b 2d 39 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-9].minor.y
3116e 79 33 32 38 2c 0a 20 20 20 20 20 20 20 20 20 20  y328,.          
3116f 20 20 20 20 20 20 20 20 20 20 20 20 26 79 79 6d              &yym
31170 73 70 5b 2d 31 30 5d 2e 6d 69 6e 6f 72 2e 79 79  sp[-10].minor.yy
31171 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  0, &yymsp[0].min
31172 6f 72 2e 79 79 30 2c 20 53 51 4c 49 54 45 5f 53  or.yy0, SQLITE_S
31173 4f 5f 41 53 43 2c 20 79 79 6d 73 70 5b 2d 37 5d  O_ASC, yymsp[-7]
31174 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 29 3b 0a 7d  .minor.yy328);.}
31175 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
31176 20 20 20 20 20 20 63 61 73 65 20 32 34 34 3a 20        case 244: 
31177 2f 2a 20 75 6e 69 71 75 65 66 6c 61 67 20 3a 3a  /* uniqueflag ::
31178 3d 20 55 4e 49 51 55 45 20 2a 2f 0a 20 20 20 20  = UNIQUE */.    
31179 20 20 63 61 73 65 20 32 39 38 3a 20 2f 2a 20 72    case 298: /* r
3117a 61 69 73 65 74 79 70 65 20 3a 3a 3d 20 41 42 4f  aisetype ::= ABO
3117b 52 54 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  RT */ yytestcase
3117c 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 39 38 29 3b  (yyruleno==298);
3117d 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
3117e 33 32 38 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 7d  328 = OE_Abort;}
3117f 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
31180 20 20 20 20 20 20 63 61 73 65 20 32 34 35 3a 20        case 245: 
31181 2f 2a 20 75 6e 69 71 75 65 66 6c 61 67 20 3a 3a  /* uniqueflag ::
31182 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  = */.{yygotomino
31183 72 2e 79 79 33 32 38 20 3d 20 4f 45 5f 4e 6f 6e  r.yy328 = OE_Non
31184 65 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  e;}.        brea
31185 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 34  k;.      case 24
31186 38 3a 20 2f 2a 20 69 64 78 6c 69 73 74 20 3a 3a  8: /* idxlist ::
31187 3d 20 69 64 78 6c 69 73 74 20 43 4f 4d 4d 41 20  = idxlist COMMA 
31188 6e 6d 20 63 6f 6c 6c 61 74 65 20 73 6f 72 74 6f  nm collate sorto
31189 72 64 65 72 20 2a 2f 0a 7b 0a 20 20 45 78 70 72  rder */.{.  Expr
3118a 20 2a 70 20 3d 20 30 3b 0a 20 20 69 66 28 20 79   *p = 0;.  if( y
3118b 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
3118c 79 30 2e 6e 3e 30 20 29 7b 0a 20 20 20 20 70 20  y0.n>0 ){.    p 
3118d 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 70 50  = sqlite3Expr(pP
3118e 61 72 73 65 2d 3e 64 62 2c 20 54 4b 5f 43 4f 4c  arse->db, TK_COL
3118f 55 4d 4e 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  UMN, 0);.    sql
31190 69 74 65 33 45 78 70 72 53 65 74 43 6f 6c 6c 28  ite3ExprSetColl(
31191 70 50 61 72 73 65 2c 20 70 2c 20 26 79 79 6d 73  pParse, p, &yyms
31192 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  p[-1].minor.yy0)
31193 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f 6d 69  ;.  }.  yygotomi
31194 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c 69 74  nor.yy14 = sqlit
31195 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
31196 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 34  (pParse,yymsp[-4
31197 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 70 29  ].minor.yy14, p)
31198 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c  ;.  sqlite3ExprL
31199 69 73 74 53 65 74 4e 61 6d 65 28 70 50 61 72 73  istSetName(pPars
3119a 65 2c 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  e,yygotominor.yy
3119b 31 34 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  14,&yymsp[-2].mi
3119c 6e 6f 72 2e 79 79 30 2c 31 29 3b 0a 20 20 73 71  nor.yy0,1);.  sq
3119d 6c 69 74 65 33 45 78 70 72 4c 69 73 74 43 68 65  lite3ExprListChe
3119e 63 6b 4c 65 6e 67 74 68 28 70 50 61 72 73 65 2c  ckLength(pParse,
3119f 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
311a0 34 2c 20 22 69 6e 64 65 78 22 29 3b 0a 20 20 69  4, "index");.  i
311a1 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  f( yygotominor.y
311a2 79 31 34 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f  y14 ) yygotomino
311a3 72 2e 79 79 31 34 2d 3e 61 5b 79 79 67 6f 74 6f  r.yy14->a[yygoto
311a4 6d 69 6e 6f 72 2e 79 79 31 34 2d 3e 6e 45 78 70  minor.yy14->nExp
311a5 72 2d 31 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d  r-1].sortOrder =
311a6 20 28 75 38 29 79 79 6d 73 70 5b 30 5d 2e 6d 69   (u8)yymsp[0].mi
311a7 6e 6f 72 2e 79 79 33 32 38 3b 0a 7d 0a 20 20 20  nor.yy328;.}.   
311a8 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
311a9 20 20 63 61 73 65 20 32 34 39 3a 20 2f 2a 20 69    case 249: /* i
311aa 64 78 6c 69 73 74 20 3a 3a 3d 20 6e 6d 20 63 6f  dxlist ::= nm co
311ab 6c 6c 61 74 65 20 73 6f 72 74 6f 72 64 65 72 20  llate sortorder 
311ac 2a 2f 0a 7b 0a 20 20 45 78 70 72 20 2a 70 20 3d  */.{.  Expr *p =
311ad 20 30 3b 0a 20 20 69 66 28 20 79 79 6d 73 70 5b   0;.  if( yymsp[
311ae 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3e  -1].minor.yy0.n>
311af 30 20 29 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c  0 ){.    p = sql
311b0 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
311b1 2c 20 54 4b 5f 43 4f 4c 55 4d 4e 2c 20 30 2c 20  , TK_COLUMN, 0, 
311b2 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
311b3 65 33 45 78 70 72 53 65 74 43 6f 6c 6c 28 70 50  e3ExprSetColl(pP
311b4 61 72 73 65 2c 20 70 2c 20 26 79 79 6d 73 70 5b  arse, p, &yymsp[
311b5 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  -1].minor.yy0);.
311b6 20 20 7d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f    }.  yygotomino
311b7 72 2e 79 79 31 34 20 3d 20 73 71 6c 69 74 65 33  r.yy14 = sqlite3
311b8 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
311b9 50 61 72 73 65 2c 30 2c 20 70 29 3b 0a 20 20 73  Parse,0, p);.  s
311ba 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53 65  qlite3ExprListSe
311bb 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 79 79  tName(pParse, yy
311bc 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2c 20  gotominor.yy14, 
311bd 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  &yymsp[-2].minor
311be 2e 79 79 30 2c 20 31 29 3b 0a 20 20 73 71 6c 69  .yy0, 1);.  sqli
311bf 74 65 33 45 78 70 72 4c 69 73 74 43 68 65 63 6b  te3ExprListCheck
311c0 4c 65 6e 67 74 68 28 70 50 61 72 73 65 2c 20 79  Length(pParse, y
311c1 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2c  ygotominor.yy14,
311c2 20 22 69 6e 64 65 78 22 29 3b 0a 20 20 69 66 28   "index");.  if(
311c3 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
311c4 34 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  4 ) yygotominor.
311c5 79 79 31 34 2d 3e 61 5b 79 79 67 6f 74 6f 6d 69  yy14->a[yygotomi
311c6 6e 6f 72 2e 79 79 31 34 2d 3e 6e 45 78 70 72 2d  nor.yy14->nExpr-
311c7 31 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20 28  1].sortOrder = (
311c8 75 38 29 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  u8)yymsp[0].mino
311c9 72 2e 79 79 33 32 38 3b 0a 7d 0a 20 20 20 20 20  r.yy328;.}.     
311ca 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
311cb 63 61 73 65 20 32 35 30 3a 20 2f 2a 20 63 6f 6c  case 250: /* col
311cc 6c 61 74 65 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67  late ::= */.{yyg
311cd 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20 3d  otominor.yy0.z =
311ce 20 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e   0; yygotominor.
311cf 79 79 30 2e 6e 20 3d 20 30 3b 7d 0a 20 20 20 20  yy0.n = 0;}.    
311d0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
311d1 20 63 61 73 65 20 32 35 32 3a 20 2f 2a 20 63 6d   case 252: /* cm
311d2 64 20 3a 3a 3d 20 44 52 4f 50 20 49 4e 44 45 58  d ::= DROP INDEX
311d3 20 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61   ifexists fullna
311d4 6d 65 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 44 72  me */.{sqlite3Dr
311d5 6f 70 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20  opIndex(pParse, 
311d6 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
311d7 79 36 35 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  y65, yymsp[-1].m
311d8 69 6e 6f 72 2e 79 79 33 32 38 29 3b 7d 0a 20 20  inor.yy328);}.  
311d9 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
311da 20 20 20 63 61 73 65 20 32 35 33 3a 20 2f 2a 20     case 253: /* 
311db 63 6d 64 20 3a 3a 3d 20 56 41 43 55 55 4d 20 2a  cmd ::= VACUUM *
311dc 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 35 34  /.      case 254
311dd 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 56 41 43  : /* cmd ::= VAC
311de 55 55 4d 20 6e 6d 20 2a 2f 20 79 79 74 65 73 74  UUM nm */ yytest
311df 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
311e0 35 34 29 3b 0a 7b 73 71 6c 69 74 65 33 56 61 63  54);.{sqlite3Vac
311e1 75 75 6d 28 70 50 61 72 73 65 29 3b 7d 0a 20 20  uum(pParse);}.  
311e2 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
311e3 20 20 20 63 61 73 65 20 32 35 35 3a 20 2f 2a 20     case 255: /* 
311e4 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e  cmd ::= PRAGMA n
311e5 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 73 71 6c 69 74  m dbnm */.{sqlit
311e6 65 33 50 72 61 67 6d 61 28 70 50 61 72 73 65 2c  e3Pragma(pParse,
311e7 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  &yymsp[-1].minor
311e8 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  .yy0,&yymsp[0].m
311e9 69 6e 6f 72 2e 79 79 30 2c 30 2c 30 29 3b 7d 0a  inor.yy0,0,0);}.
311ea 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
311eb 20 20 20 20 20 63 61 73 65 20 32 35 36 3a 20 2f       case 256: /
311ec 2a 20 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41  * cmd ::= PRAGMA
311ed 20 6e 6d 20 64 62 6e 6d 20 45 51 20 6e 6d 6e 75   nm dbnm EQ nmnu
311ee 6d 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 50 72 61  m */.{sqlite3Pra
311ef 67 6d 61 28 70 50 61 72 73 65 2c 26 79 79 6d 73  gma(pParse,&yyms
311f0 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-3].minor.yy0,
311f1 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  &yymsp[-2].minor
311f2 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  .yy0,&yymsp[0].m
311f3 69 6e 6f 72 2e 79 79 30 2c 30 29 3b 7d 0a 20 20  inor.yy0,0);}.  
311f4 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
311f5 20 20 20 63 61 73 65 20 32 35 37 3a 20 2f 2a 20     case 257: /* 
311f6 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e  cmd ::= PRAGMA n
311f7 6d 20 64 62 6e 6d 20 4c 50 20 6e 6d 6e 75 6d 20  m dbnm LP nmnum 
311f8 52 50 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 50 72  RP */.{sqlite3Pr
311f9 61 67 6d 61 28 70 50 61 72 73 65 2c 26 79 79 6d  agma(pParse,&yym
311fa 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-4].minor.yy0
311fb 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  ,&yymsp[-3].mino
311fc 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 31 5d  r.yy0,&yymsp[-1]
311fd 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 29 3b 7d 0a  .minor.yy0,0);}.
311fe 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
311ff 20 20 20 20 20 63 61 73 65 20 32 35 38 3a 20 2f       case 258: /
31200 2a 20 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41  * cmd ::= PRAGMA
31201 20 6e 6d 20 64 62 6e 6d 20 45 51 20 6d 69 6e 75   nm dbnm EQ minu
31202 73 5f 6e 75 6d 20 2a 2f 0a 7b 73 71 6c 69 74 65  s_num */.{sqlite
31203 33 50 72 61 67 6d 61 28 70 50 61 72 73 65 2c 26  3Pragma(pParse,&
31204 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
31205 79 79 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d  yy0,&yymsp[-2].m
31206 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b  inor.yy0,&yymsp[
31207 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 31 29 3b  0].minor.yy0,1);
31208 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
31209 0a 20 20 20 20 20 20 63 61 73 65 20 32 35 39 3a  .      case 259:
3120a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50 52 41 47   /* cmd ::= PRAG
3120b 4d 41 20 6e 6d 20 64 62 6e 6d 20 4c 50 20 6d 69  MA nm dbnm LP mi
3120c 6e 75 73 5f 6e 75 6d 20 52 50 20 2a 2f 0a 7b 73  nus_num RP */.{s
3120d 71 6c 69 74 65 33 50 72 61 67 6d 61 28 70 50 61  qlite3Pragma(pPa
3120e 72 73 65 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d  rse,&yymsp[-4].m
3120f 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b  inor.yy0,&yymsp[
31210 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -3].minor.yy0,&y
31211 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
31212 79 30 2c 31 29 3b 7d 0a 20 20 20 20 20 20 20 20  y0,1);}.        
31213 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
31214 65 20 32 37 30 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 270: /* cmd ::
31215 3d 20 63 72 65 61 74 65 6b 77 20 74 72 69 67 67  = createkw trigg
31216 65 72 5f 64 65 63 6c 20 42 45 47 49 4e 20 74 72  er_decl BEGIN tr
31217 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20 45  igger_cmd_list E
31218 4e 44 20 2a 2f 0a 7b 0a 20 20 54 6f 6b 65 6e 20  ND */.{.  Token 
31219 61 6c 6c 3b 0a 20 20 61 6c 6c 2e 7a 20 3d 20 79  all;.  all.z = y
3121a 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
3121b 79 30 2e 7a 3b 0a 20 20 61 6c 6c 2e 6e 20 3d 20  y0.z;.  all.n = 
3121c 28 69 6e 74 29 28 79 79 6d 73 70 5b 30 5d 2e 6d  (int)(yymsp[0].m
3121d 69 6e 6f 72 2e 79 79 30 2e 7a 20 2d 20 79 79 6d  inor.yy0.z - yym
3121e 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-3].minor.yy0
3121f 2e 7a 29 20 2b 20 79 79 6d 73 70 5b 30 5d 2e 6d  .z) + yymsp[0].m
31220 69 6e 6f 72 2e 79 79 30 2e 6e 3b 0a 20 20 73 71  inor.yy0.n;.  sq
31221 6c 69 74 65 33 46 69 6e 69 73 68 54 72 69 67 67  lite3FinishTrigg
31222 65 72 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70  er(pParse, yymsp
31223 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37 33  [-1].minor.yy473
31224 2c 20 26 61 6c 6c 29 3b 0a 7d 0a 20 20 20 20 20  , &all);.}.     
31225 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
31226 63 61 73 65 20 32 37 31 3a 20 2f 2a 20 74 72 69  case 271: /* tri
31227 67 67 65 72 5f 64 65 63 6c 20 3a 3a 3d 20 74 65  gger_decl ::= te
31228 6d 70 20 54 52 49 47 47 45 52 20 69 66 6e 6f 74  mp TRIGGER ifnot
31229 65 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20 74  exists nm dbnm t
3122a 72 69 67 67 65 72 5f 74 69 6d 65 20 74 72 69 67  rigger_time trig
3122b 67 65 72 5f 65 76 65 6e 74 20 4f 4e 20 66 75 6c  ger_event ON ful
3122c 6c 6e 61 6d 65 20 66 6f 72 65 61 63 68 5f 63 6c  lname foreach_cl
3122d 61 75 73 65 20 77 68 65 6e 5f 63 6c 61 75 73 65  ause when_clause
3122e 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 42   */.{.  sqlite3B
3122f 65 67 69 6e 54 72 69 67 67 65 72 28 70 50 61 72  eginTrigger(pPar
31230 73 65 2c 20 26 79 79 6d 73 70 5b 2d 37 5d 2e 6d  se, &yymsp[-7].m
31231 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70  inor.yy0, &yymsp
31232 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20  [-6].minor.yy0, 
31233 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e  yymsp[-5].minor.
31234 79 79 33 32 38 2c 20 79 79 6d 73 70 5b 2d 34 5d  yy328, yymsp[-4]
31235 2e 6d 69 6e 6f 72 2e 79 79 33 37 38 2e 61 2c 20  .minor.yy378.a, 
31236 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
31237 79 79 33 37 38 2e 62 2c 20 79 79 6d 73 70 5b 2d  yy378.b, yymsp[-
31238 32 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 20 79  2].minor.yy65, y
31239 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
3123a 31 33 32 2c 20 79 79 6d 73 70 5b 2d 31 30 5d 2e  132, yymsp[-10].
3123b 6d 69 6e 6f 72 2e 79 79 33 32 38 2c 20 79 79 6d  minor.yy328, yym
3123c 73 70 5b 2d 38 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-8].minor.yy3
3123d 32 38 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  28);.  yygotomin
3123e 6f 72 2e 79 79 30 20 3d 20 28 79 79 6d 73 70 5b  or.yy0 = (yymsp[
3123f 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3d  -6].minor.yy0.n=
31240 3d 30 3f 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e  =0?yymsp[-7].min
31241 6f 72 2e 79 79 30 3a 79 79 6d 73 70 5b 2d 36 5d  or.yy0:yymsp[-6]
31242 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20  .minor.yy0);.}. 
31243 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
31244 20 20 20 20 63 61 73 65 20 32 37 32 3a 20 2f 2a      case 272: /*
31245 20 74 72 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a   trigger_time ::
31246 3d 20 42 45 46 4f 52 45 20 2a 2f 0a 20 20 20 20  = BEFORE */.    
31247 20 20 63 61 73 65 20 32 37 35 3a 20 2f 2a 20 74    case 275: /* t
31248 72 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20  rigger_time ::= 
31249 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
3124a 72 75 6c 65 6e 6f 3d 3d 32 37 35 29 3b 0a 7b 20  ruleno==275);.{ 
3124b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32  yygotominor.yy32
3124c 38 20 3d 20 54 4b 5f 42 45 46 4f 52 45 3b 20 7d  8 = TK_BEFORE; }
3124d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
3124e 20 20 20 20 20 20 63 61 73 65 20 32 37 33 3a 20        case 273: 
3124f 2f 2a 20 74 72 69 67 67 65 72 5f 74 69 6d 65 20  /* trigger_time 
31250 3a 3a 3d 20 41 46 54 45 52 20 2a 2f 0a 7b 20 79  ::= AFTER */.{ y
31251 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38  ygotominor.yy328
31252 20 3d 20 54 4b 5f 41 46 54 45 52 3b 20 20 7d 0a   = TK_AFTER;  }.
31253 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
31254 20 20 20 20 20 63 61 73 65 20 32 37 34 3a 20 2f       case 274: /
31255 2a 20 74 72 69 67 67 65 72 5f 74 69 6d 65 20 3a  * trigger_time :
31256 3a 3d 20 49 4e 53 54 45 41 44 20 4f 46 20 2a 2f  := INSTEAD OF */
31257 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .{ yygotominor.y
31258 79 33 32 38 20 3d 20 54 4b 5f 49 4e 53 54 45 41  y328 = TK_INSTEA
31259 44 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  D;}.        brea
3125a 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 37  k;.      case 27
3125b 36 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 65 76  6: /* trigger_ev
3125c 65 6e 74 20 3a 3a 3d 20 44 45 4c 45 54 45 7c 49  ent ::= DELETE|I
3125d 4e 53 45 52 54 20 2a 2f 0a 20 20 20 20 20 20 63  NSERT */.      c
3125e 61 73 65 20 32 37 37 3a 20 2f 2a 20 74 72 69 67  ase 277: /* trig
3125f 67 65 72 5f 65 76 65 6e 74 20 3a 3a 3d 20 55 50  ger_event ::= UP
31260 44 41 54 45 20 2a 2f 20 79 79 74 65 73 74 63 61  DATE */ yytestca
31261 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 37 37  se(yyruleno==277
31262 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  );.{yygotominor.
31263 79 79 33 37 38 2e 61 20 3d 20 79 79 6d 73 70 5b  yy378.a = yymsp[
31264 30 5d 2e 6d 61 6a 6f 72 3b 20 79 79 67 6f 74 6f  0].major; yygoto
31265 6d 69 6e 6f 72 2e 79 79 33 37 38 2e 62 20 3d 20  minor.yy378.b = 
31266 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  0;}.        brea
31267 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 37  k;.      case 27
31268 38 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 65 76  8: /* trigger_ev
31269 65 6e 74 20 3a 3a 3d 20 55 50 44 41 54 45 20 4f  ent ::= UPDATE O
3126a 46 20 69 6e 73 63 6f 6c 6c 69 73 74 20 2a 2f 0a  F inscollist */.
3126b 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  {yygotominor.yy3
3126c 37 38 2e 61 20 3d 20 54 4b 5f 55 50 44 41 54 45  78.a = TK_UPDATE
3126d 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ; yygotominor.yy
3126e 33 37 38 2e 62 20 3d 20 79 79 6d 73 70 5b 30 5d  378.b = yymsp[0]
3126f 2e 6d 69 6e 6f 72 2e 79 79 34 30 38 3b 7d 0a 20  .minor.yy408;}. 
31270 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
31271 20 20 20 20 63 61 73 65 20 32 38 31 3a 20 2f 2a      case 281: /*
31272 20 77 68 65 6e 5f 63 6c 61 75 73 65 20 3a 3a 3d   when_clause ::=
31273 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 33   */.      case 3
31274 30 33 3a 20 2f 2a 20 6b 65 79 5f 6f 70 74 20 3a  03: /* key_opt :
31275 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  := */ yytestcase
31276 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 30 33 29 3b  (yyruleno==303);
31277 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .{ yygotominor.y
31278 79 31 33 32 20 3d 20 30 3b 20 7d 0a 20 20 20 20  y132 = 0; }.    
31279 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3127a 20 63 61 73 65 20 32 38 32 3a 20 2f 2a 20 77 68   case 282: /* wh
3127b 65 6e 5f 63 6c 61 75 73 65 20 3a 3a 3d 20 57 48  en_clause ::= WH
3127c 45 4e 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20  EN expr */.     
3127d 20 63 61 73 65 20 33 30 34 3a 20 2f 2a 20 6b 65   case 304: /* ke
3127e 79 5f 6f 70 74 20 3a 3a 3d 20 4b 45 59 20 65 78  y_opt ::= KEY ex
3127f 70 72 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  pr */ yytestcase
31280 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 30 34 29 3b  (yyruleno==304);
31281 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .{ yygotominor.y
31282 79 31 33 32 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  y132 = yymsp[0].
31283 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
31284 72 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  r; }.        bre
31285 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
31286 38 33 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 63  83: /* trigger_c
31287 6d 64 5f 6c 69 73 74 20 3a 3a 3d 20 74 72 69 67  md_list ::= trig
31288 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20 74 72 69  ger_cmd_list tri
31289 67 67 65 72 5f 63 6d 64 20 53 45 4d 49 20 2a 2f  gger_cmd SEMI */
3128a 0a 7b 0a 20 20 61 73 73 65 72 74 28 20 79 79 6d  .{.  assert( yym
3128b 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-2].minor.yy4
3128c 37 33 21 3d 30 20 29 3b 0a 20 20 79 79 6d 73 70  73!=0 );.  yymsp
3128d 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37 33  [-2].minor.yy473
3128e 2d 3e 70 4c 61 73 74 2d 3e 70 4e 65 78 74 20 3d  ->pLast->pNext =
3128f 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
31290 2e 79 79 34 37 33 3b 0a 20 20 79 79 6d 73 70 5b  .yy473;.  yymsp[
31291 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37 33 2d  -2].minor.yy473-
31292 3e 70 4c 61 73 74 20 3d 20 79 79 6d 73 70 5b 2d  >pLast = yymsp[-
31293 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37 33 3b 0a  1].minor.yy473;.
31294 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
31295 34 37 33 20 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e  473 = yymsp[-2].
31296 6d 69 6e 6f 72 2e 79 79 34 37 33 3b 0a 7d 0a 20  minor.yy473;.}. 
31297 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
31298 20 20 20 20 63 61 73 65 20 32 38 34 3a 20 2f 2a      case 284: /*
31299 20 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73   trigger_cmd_lis
3129a 74 20 3a 3a 3d 20 74 72 69 67 67 65 72 5f 63 6d  t ::= trigger_cm
3129b 64 20 53 45 4d 49 20 2a 2f 0a 7b 20 0a 20 20 61  d SEMI */.{ .  a
3129c 73 73 65 72 74 28 20 79 79 6d 73 70 5b 2d 31 5d  ssert( yymsp[-1]
3129d 2e 6d 69 6e 6f 72 2e 79 79 34 37 33 21 3d 30 20  .minor.yy473!=0 
3129e 29 3b 0a 20 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  );.  yymsp[-1].m
3129f 69 6e 6f 72 2e 79 79 34 37 33 2d 3e 70 4c 61 73  inor.yy473->pLas
312a0 74 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  t = yymsp[-1].mi
312a1 6e 6f 72 2e 79 79 34 37 33 3b 0a 20 20 79 79 67  nor.yy473;.  yyg
312a2 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 37 33 20 3d  otominor.yy473 =
312a3 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
312a4 2e 79 79 34 37 33 3b 0a 7d 0a 20 20 20 20 20 20  .yy473;.}.      
312a5 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
312a6 61 73 65 20 32 38 36 3a 20 2f 2a 20 74 72 6e 6d  ase 286: /* trnm
312a7 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d 20 2a   ::= nm DOT nm *
312a8 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  /.{.  yygotomino
312a9 72 2e 79 79 30 20 3d 20 79 79 6d 73 70 5b 30 5d  r.yy0 = yymsp[0]
312aa 2e 6d 69 6e 6f 72 2e 79 79 30 3b 0a 20 20 73 71  .minor.yy0;.  sq
312ab 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
312ac 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22  arse, .        "
312ad 71 75 61 6c 69 66 69 65 64 20 74 61 62 6c 65 20  qualified table 
312ae 6e 61 6d 65 73 20 61 72 65 20 6e 6f 74 20 61 6c  names are not al
312af 6c 6f 77 65 64 20 6f 6e 20 49 4e 53 45 52 54 2c  lowed on INSERT,
312b0 20 55 50 44 41 54 45 2c 20 61 6e 64 20 44 45 4c   UPDATE, and DEL
312b1 45 54 45 20 22 0a 20 20 20 20 20 20 20 20 22 73  ETE ".        "s
312b2 74 61 74 65 6d 65 6e 74 73 20 77 69 74 68 69 6e  tatements within
312b3 20 74 72 69 67 67 65 72 73 22 29 3b 0a 7d 0a 20   triggers");.}. 
312b4 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
312b5 20 20 20 20 63 61 73 65 20 32 38 38 3a 20 2f 2a      case 288: /*
312b6 20 74 72 69 64 78 62 79 20 3a 3a 3d 20 49 4e 44   tridxby ::= IND
312b7 45 58 45 44 20 42 59 20 6e 6d 20 2a 2f 0a 7b 0a  EXED BY nm */.{.
312b8 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
312b9 67 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20  g(pParse,.      
312ba 20 20 22 74 68 65 20 49 4e 44 45 58 45 44 20 42    "the INDEXED B
312bb 59 20 63 6c 61 75 73 65 20 69 73 20 6e 6f 74 20  Y clause is not 
312bc 61 6c 6c 6f 77 65 64 20 6f 6e 20 55 50 44 41 54  allowed on UPDAT
312bd 45 20 6f 72 20 44 45 4c 45 54 45 20 73 74 61 74  E or DELETE stat
312be 65 6d 65 6e 74 73 20 22 0a 20 20 20 20 20 20 20  ements ".       
312bf 20 22 77 69 74 68 69 6e 20 74 72 69 67 67 65 72   "within trigger
312c0 73 22 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  s");.}.        b
312c1 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
312c2 20 32 38 39 3a 20 2f 2a 20 74 72 69 64 78 62 79   289: /* tridxby
312c3 20 3a 3a 3d 20 4e 4f 54 20 49 4e 44 45 58 45 44   ::= NOT INDEXED
312c4 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 45   */.{.  sqlite3E
312c5 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 0a  rrorMsg(pParse,.
312c6 20 20 20 20 20 20 20 20 22 74 68 65 20 4e 4f 54          "the NOT
312c7 20 49 4e 44 45 58 45 44 20 63 6c 61 75 73 65 20   INDEXED clause 
312c8 69 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 6f  is not allowed o
312c9 6e 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45  n UPDATE or DELE
312ca 54 45 20 73 74 61 74 65 6d 65 6e 74 73 20 22 0a  TE statements ".
312cb 20 20 20 20 20 20 20 20 22 77 69 74 68 69 6e 20          "within 
312cc 74 72 69 67 67 65 72 73 22 29 3b 0a 7d 0a 20 20  triggers");.}.  
312cd 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
312ce 20 20 20 63 61 73 65 20 32 39 30 3a 20 2f 2a 20     case 290: /* 
312cf 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20  trigger_cmd ::= 
312d0 55 50 44 41 54 45 20 6f 72 63 6f 6e 66 20 74 72  UPDATE orconf tr
312d1 6e 6d 20 74 72 69 64 78 62 79 20 53 45 54 20 73  nm tridxby SET s
312d2 65 74 6c 69 73 74 20 77 68 65 72 65 5f 6f 70 74  etlist where_opt
312d3 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f   */.{ yygotomino
312d4 72 2e 79 79 34 37 33 20 3d 20 73 71 6c 69 74 65  r.yy473 = sqlite
312d5 33 54 72 69 67 67 65 72 55 70 64 61 74 65 53 74  3TriggerUpdateSt
312d6 65 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 26  ep(pParse->db, &
312d7 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
312d8 79 79 30 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  yy0, yymsp[-1].m
312d9 69 6e 6f 72 2e 79 79 31 34 2c 20 79 79 6d 73 70  inor.yy14, yymsp
312da 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33 32 2c  [0].minor.yy132,
312db 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72   yymsp[-5].minor
312dc 2e 79 79 31 38 36 29 3b 20 7d 0a 20 20 20 20 20  .yy186); }.     
312dd 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
312de 63 61 73 65 20 32 39 31 3a 20 2f 2a 20 74 72 69  case 291: /* tri
312df 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 69 6e 73  gger_cmd ::= ins
312e0 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20 74 72 6e  ert_cmd INTO trn
312e1 6d 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74  m inscollist_opt
312e2 20 56 41 4c 55 45 53 20 4c 50 20 69 74 65 6d 6c   VALUES LP iteml
312e3 69 73 74 20 52 50 20 2a 2f 0a 7b 79 79 67 6f 74  ist RP */.{yygot
312e4 6f 6d 69 6e 6f 72 2e 79 79 34 37 33 20 3d 20 73  ominor.yy473 = s
312e5 71 6c 69 74 65 33 54 72 69 67 67 65 72 49 6e 73  qlite3TriggerIns
312e6 65 72 74 53 74 65 70 28 70 50 61 72 73 65 2d 3e  ertStep(pParse->
312e7 64 62 2c 20 26 79 79 6d 73 70 5b 2d 35 5d 2e 6d  db, &yymsp[-5].m
312e8 69 6e 6f 72 2e 79 79 30 2c 20 79 79 6d 73 70 5b  inor.yy0, yymsp[
312e9 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30 38 2c  -4].minor.yy408,
312ea 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
312eb 2e 79 79 31 34 2c 20 30 2c 20 79 79 6d 73 70 5b  .yy14, 0, yymsp[
312ec 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 36 29  -7].minor.yy186)
312ed 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
312ee 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 32  ;.      case 292
312ef 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d 64  : /* trigger_cmd
312f0 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20   ::= insert_cmd 
312f1 49 4e 54 4f 20 74 72 6e 6d 20 69 6e 73 63 6f 6c  INTO trnm inscol
312f2 6c 69 73 74 5f 6f 70 74 20 73 65 6c 65 63 74 20  list_opt select 
312f3 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
312f4 79 79 34 37 33 20 3d 20 73 71 6c 69 74 65 33 54  yy473 = sqlite3T
312f5 72 69 67 67 65 72 49 6e 73 65 72 74 53 74 65 70  riggerInsertStep
312f6 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 26 79 79  (pParse->db, &yy
312f7 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
312f8 30 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  0, yymsp[-1].min
312f9 6f 72 2e 79 79 34 30 38 2c 20 30 2c 20 79 79 6d  or.yy408, 0, yym
312fa 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 2c  sp[0].minor.yy3,
312fb 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72   yymsp[-4].minor
312fc 2e 79 79 31 38 36 29 3b 7d 0a 20 20 20 20 20 20  .yy186);}.      
312fd 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
312fe 61 73 65 20 32 39 33 3a 20 2f 2a 20 74 72 69 67  ase 293: /* trig
312ff 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 44 45 4c 45  ger_cmd ::= DELE
31300 54 45 20 46 52 4f 4d 20 74 72 6e 6d 20 74 72 69  TE FROM trnm tri
31301 64 78 62 79 20 77 68 65 72 65 5f 6f 70 74 20 2a  dxby where_opt *
31302 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
31303 79 34 37 33 20 3d 20 73 71 6c 69 74 65 33 54 72  y473 = sqlite3Tr
31304 69 67 67 65 72 44 65 6c 65 74 65 53 74 65 70 28  iggerDeleteStep(
31305 70 50 61 72 73 65 2d 3e 64 62 2c 20 26 79 79 6d  pParse->db, &yym
31306 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-2].minor.yy0
31307 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  , yymsp[0].minor
31308 2e 79 79 31 33 32 29 3b 7d 0a 20 20 20 20 20 20  .yy132);}.      
31309 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3130a 61 73 65 20 32 39 34 3a 20 2f 2a 20 74 72 69 67  ase 294: /* trig
3130b 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 73 65 6c 65  ger_cmd ::= sele
3130c 63 74 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  ct */.{yygotomin
3130d 6f 72 2e 79 79 34 37 33 20 3d 20 73 71 6c 69 74  or.yy473 = sqlit
3130e 65 33 54 72 69 67 67 65 72 53 65 6c 65 63 74 53  e3TriggerSelectS
3130f 74 65 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  tep(pParse->db, 
31310 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
31311 79 33 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 62  y3); }.        b
31312 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
31313 20 32 39 35 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   295: /* expr ::
31314 3d 20 52 41 49 53 45 20 4c 50 20 49 47 4e 4f 52  = RAISE LP IGNOR
31315 45 20 52 50 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f  E RP */.{.  yygo
31316 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  tominor.yy346.pE
31317 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78  xpr = sqlite3PEx
31318 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 52 41  pr(pParse, TK_RA
31319 49 53 45 2c 20 30 2c 20 30 2c 20 30 29 3b 20 0a  ISE, 0, 0, 0); .
3131a 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f    if( yygotomino
3131b 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 29 7b  r.yy346.pExpr ){
3131c 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .    yygotominor
3131d 2e 79 79 33 34 36 2e 70 45 78 70 72 2d 3e 61 66  .yy346.pExpr->af
3131e 66 69 6e 69 74 79 20 3d 20 4f 45 5f 49 67 6e 6f  finity = OE_Igno
3131f 72 65 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f  re;.  }.  yygoto
31320 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61  minor.yy346.zSta
31321 72 74 20 3d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d  rt = yymsp[-3].m
31322 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20 79 79  inor.yy0.z;.  yy
31323 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
31324 7a 45 6e 64 20 3d 20 26 79 79 6d 73 70 5b 30 5d  zEnd = &yymsp[0]
31325 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d  .minor.yy0.z[yym
31326 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  sp[0].minor.yy0.
31327 6e 5d 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  n];.}.        br
31328 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
31329 32 39 36 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  296: /* expr ::=
3132a 20 52 41 49 53 45 20 4c 50 20 72 61 69 73 65 74   RAISE LP raiset
3132b 79 70 65 20 43 4f 4d 4d 41 20 6e 6d 20 52 50 20  ype COMMA nm RP 
3132c 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  */.{.  yygotomin
3132d 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d  or.yy346.pExpr =
3132e 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
3132f 61 72 73 65 2c 20 54 4b 5f 52 41 49 53 45 2c 20  arse, TK_RAISE, 
31330 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d 31 5d  0, 0, &yymsp[-1]
31331 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 20 0a 20 20  .minor.yy0); .  
31332 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  if( yygotominor.
31333 79 79 33 34 36 2e 70 45 78 70 72 20 29 20 7b 0a  yy346.pExpr ) {.
31334 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e      yygotominor.
31335 79 79 33 34 36 2e 70 45 78 70 72 2d 3e 61 66 66  yy346.pExpr->aff
31336 69 6e 69 74 79 20 3d 20 28 63 68 61 72 29 79 79  inity = (char)yy
31337 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
31338 33 32 38 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74  328;.  }.  yygot
31339 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74  ominor.yy346.zSt
3133a 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d 35 5d 2e  art = yymsp[-5].
3133b 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20 79  minor.yy0.z;.  y
3133c 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
3133d 2e 7a 45 6e 64 20 3d 20 26 79 79 6d 73 70 5b 30  .zEnd = &yymsp[0
3133e 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79  ].minor.yy0.z[yy
3133f 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
31340 2e 6e 5d 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  .n];.}.        b
31341 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
31342 20 32 39 37 3a 20 2f 2a 20 72 61 69 73 65 74 79   297: /* raisety
31343 70 65 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b 20  pe ::= ROLLBACK 
31344 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
31345 79 79 33 32 38 20 3d 20 4f 45 5f 52 6f 6c 6c 62  yy328 = OE_Rollb
31346 61 63 6b 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  ack;}.        br
31347 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
31348 32 39 39 3a 20 2f 2a 20 72 61 69 73 65 74 79 70  299: /* raisetyp
31349 65 20 3a 3a 3d 20 46 41 49 4c 20 2a 2f 0a 7b 79  e ::= FAIL */.{y
3134a 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38  ygotominor.yy328
3134b 20 3d 20 4f 45 5f 46 61 69 6c 3b 7d 0a 20 20 20   = OE_Fail;}.   
3134c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3134d 20 20 63 61 73 65 20 33 30 30 3a 20 2f 2a 20 63    case 300: /* c
3134e 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 54 52 49 47  md ::= DROP TRIG
3134f 47 45 52 20 69 66 65 78 69 73 74 73 20 66 75 6c  GER ifexists ful
31350 6c 6e 61 6d 65 20 2a 2f 0a 7b 0a 20 20 73 71 6c  lname */.{.  sql
31351 69 74 65 33 44 72 6f 70 54 72 69 67 67 65 72 28  ite3DropTrigger(
31352 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 30 5d 2e  pParse,yymsp[0].
31353 6d 69 6e 6f 72 2e 79 79 36 35 2c 79 79 6d 73 70  minor.yy65,yymsp
31354 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38  [-1].minor.yy328
31355 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
31356 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33  ak;.      case 3
31357 30 31 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 41  01: /* cmd ::= A
31358 54 54 41 43 48 20 64 61 74 61 62 61 73 65 5f 6b  TTACH database_k
31359 77 5f 6f 70 74 20 65 78 70 72 20 41 53 20 65 78  w_opt expr AS ex
3135a 70 72 20 6b 65 79 5f 6f 70 74 20 2a 2f 0a 7b 0a  pr key_opt */.{.
3135b 20 20 73 71 6c 69 74 65 33 41 74 74 61 63 68 28    sqlite3Attach(
3135c 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 33  pParse, yymsp[-3
3135d 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  ].minor.yy346.pE
3135e 78 70 72 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  xpr, yymsp[-1].m
3135f 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
31360 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  , yymsp[0].minor
31361 2e 79 79 31 33 32 29 3b 0a 7d 0a 20 20 20 20 20  .yy132);.}.     
31362 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
31363 63 61 73 65 20 33 30 32 3a 20 2f 2a 20 63 6d 64  case 302: /* cmd
31364 20 3a 3a 3d 20 44 45 54 41 43 48 20 64 61 74 61   ::= DETACH data
31365 62 61 73 65 5f 6b 77 5f 6f 70 74 20 65 78 70 72  base_kw_opt expr
31366 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 44   */.{.  sqlite3D
31367 65 74 61 63 68 28 70 50 61 72 73 65 2c 20 79 79  etach(pParse, yy
31368 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
31369 34 36 2e 70 45 78 70 72 29 3b 0a 7d 0a 20 20 20  46.pExpr);.}.   
3136a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3136b 20 20 63 61 73 65 20 33 30 37 3a 20 2f 2a 20 63    case 307: /* c
3136c 6d 64 20 3a 3a 3d 20 52 45 49 4e 44 45 58 20 2a  md ::= REINDEX *
3136d 2f 0a 7b 73 71 6c 69 74 65 33 52 65 69 6e 64 65  /.{sqlite3Reinde
3136e 78 28 70 50 61 72 73 65 2c 20 30 2c 20 30 29 3b  x(pParse, 0, 0);
3136f 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
31370 0a 20 20 20 20 20 20 63 61 73 65 20 33 30 38 3a  .      case 308:
31371 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 52 45 49 4e   /* cmd ::= REIN
31372 44 45 58 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b  DEX nm dbnm */.{
31373 73 71 6c 69 74 65 33 52 65 69 6e 64 65 78 28 70  sqlite3Reindex(p
31374 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d 31  Parse, &yymsp[-1
31375 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79  ].minor.yy0, &yy
31376 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
31377 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
31378 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 30  k;.      case 30
31379 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 41 4e  9: /* cmd ::= AN
3137a 41 4c 59 5a 45 20 2a 2f 0a 7b 73 71 6c 69 74 65  ALYZE */.{sqlite
3137b 33 41 6e 61 6c 79 7a 65 28 70 50 61 72 73 65 2c  3Analyze(pParse,
3137c 20 30 2c 20 30 29 3b 7d 0a 20 20 20 20 20 20 20   0, 0);}.       
3137d 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3137e 73 65 20 33 31 30 3a 20 2f 2a 20 63 6d 64 20 3a  se 310: /* cmd :
3137f 3a 3d 20 41 4e 41 4c 59 5a 45 20 6e 6d 20 64 62  := ANALYZE nm db
31380 6e 6d 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 6e  nm */.{sqlite3An
31381 61 6c 79 7a 65 28 70 50 61 72 73 65 2c 20 26 79  alyze(pParse, &y
31382 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
31383 79 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  y0, &yymsp[0].mi
31384 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20  nor.yy0);}.     
31385 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
31386 63 61 73 65 20 33 31 31 3a 20 2f 2a 20 63 6d 64  case 311: /* cmd
31387 20 3a 3a 3d 20 41 4c 54 45 52 20 54 41 42 4c 45   ::= ALTER TABLE
31388 20 66 75 6c 6c 6e 61 6d 65 20 52 45 4e 41 4d 45   fullname RENAME
31389 20 54 4f 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 73 71   TO nm */.{.  sq
3138a 6c 69 74 65 33 41 6c 74 65 72 52 65 6e 61 6d 65  lite3AlterRename
3138b 54 61 62 6c 65 28 70 50 61 72 73 65 2c 79 79 6d  Table(pParse,yym
3138c 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 36  sp[-3].minor.yy6
3138d 35 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  5,&yymsp[0].mino
3138e 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20  r.yy0);.}.      
3138f 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
31390 61 73 65 20 33 31 32 3a 20 2f 2a 20 63 6d 64 20  ase 312: /* cmd 
31391 3a 3a 3d 20 41 4c 54 45 52 20 54 41 42 4c 45 20  ::= ALTER TABLE 
31392 61 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e  add_column_fulln
31393 61 6d 65 20 41 44 44 20 6b 77 63 6f 6c 75 6d 6e  ame ADD kwcolumn
31394 5f 6f 70 74 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 7b  _opt column */.{
31395 0a 20 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46  .  sqlite3AlterF
31396 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28 70  inishAddColumn(p
31397 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 30 5d  Parse, &yymsp[0]
31398 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20  .minor.yy0);.}. 
31399 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3139a 20 20 20 20 63 61 73 65 20 33 31 33 3a 20 2f 2a      case 313: /*
3139b 20 61 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c   add_column_full
3139c 6e 61 6d 65 20 3a 3a 3d 20 66 75 6c 6c 6e 61 6d  name ::= fullnam
3139d 65 20 2a 2f 0a 7b 0a 20 20 70 50 61 72 73 65 2d  e */.{.  pParse-
3139e 3e 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62  >db->lookaside.b
3139f 45 6e 61 62 6c 65 64 20 3d 20 30 3b 0a 20 20 73  Enabled = 0;.  s
313a0 71 6c 69 74 65 33 41 6c 74 65 72 42 65 67 69 6e  qlite3AlterBegin
313a1 41 64 64 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65  AddColumn(pParse
313a2 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  , yymsp[0].minor
313a3 2e 79 79 36 35 29 3b 0a 7d 0a 20 20 20 20 20 20  .yy65);.}.      
313a4 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
313a5 61 73 65 20 33 31 36 3a 20 2f 2a 20 63 6d 64 20  ase 316: /* cmd 
313a6 3a 3a 3d 20 63 72 65 61 74 65 5f 76 74 61 62 20  ::= create_vtab 
313a7 2a 2f 0a 7b 73 71 6c 69 74 65 33 56 74 61 62 46  */.{sqlite3VtabF
313a8 69 6e 69 73 68 50 61 72 73 65 28 70 50 61 72 73  inishParse(pPars
313a9 65 2c 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  e,0);}.        b
313aa 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
313ab 20 33 31 37 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d   317: /* cmd ::=
313ac 20 63 72 65 61 74 65 5f 76 74 61 62 20 4c 50 20   create_vtab LP 
313ad 76 74 61 62 61 72 67 6c 69 73 74 20 52 50 20 2a  vtabarglist RP *
313ae 2f 0a 7b 73 71 6c 69 74 65 33 56 74 61 62 46 69  /.{sqlite3VtabFi
313af 6e 69 73 68 50 61 72 73 65 28 70 50 61 72 73 65  nishParse(pParse
313b0 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
313b1 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  .yy0);}.        
313b2 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
313b3 65 20 33 31 38 3a 20 2f 2a 20 63 72 65 61 74 65  e 318: /* create
313b4 5f 76 74 61 62 20 3a 3a 3d 20 63 72 65 61 74 65  _vtab ::= create
313b5 6b 77 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45  kw VIRTUAL TABLE
313b6 20 6e 6d 20 64 62 6e 6d 20 55 53 49 4e 47 20 6e   nm dbnm USING n
313b7 6d 20 2a 2f 0a 7b 0a 20 20 20 20 73 71 6c 69 74  m */.{.    sqlit
313b8 65 33 56 74 61 62 42 65 67 69 6e 50 61 72 73 65  e3VtabBeginParse
313b9 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b  (pParse, &yymsp[
313ba 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26  -3].minor.yy0, &
313bb 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
313bc 79 79 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  yy0, &yymsp[0].m
313bd 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20  inor.yy0);.}.   
313be 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
313bf 20 20 63 61 73 65 20 33 32 31 3a 20 2f 2a 20 76    case 321: /* v
313c0 74 61 62 61 72 67 20 3a 3a 3d 20 2a 2f 0a 7b 73  tabarg ::= */.{s
313c1 71 6c 69 74 65 33 56 74 61 62 41 72 67 49 6e 69  qlite3VtabArgIni
313c2 74 28 70 50 61 72 73 65 29 3b 7d 0a 20 20 20 20  t(pParse);}.    
313c3 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
313c4 20 63 61 73 65 20 33 32 33 3a 20 2f 2a 20 76 74   case 323: /* vt
313c5 61 62 61 72 67 74 6f 6b 65 6e 20 3a 3a 3d 20 41  abargtoken ::= A
313c6 4e 59 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  NY */.      case
313c7 20 33 32 34 3a 20 2f 2a 20 76 74 61 62 61 72 67   324: /* vtabarg
313c8 74 6f 6b 65 6e 20 3a 3a 3d 20 6c 70 20 61 6e 79  token ::= lp any
313c9 6c 69 73 74 20 52 50 20 2a 2f 20 79 79 74 65 73  list RP */ yytes
313ca 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
313cb 33 32 34 29 3b 0a 20 20 20 20 20 20 63 61 73 65  324);.      case
313cc 20 33 32 35 3a 20 2f 2a 20 6c 70 20 3a 3a 3d 20   325: /* lp ::= 
313cd 4c 50 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  LP */ yytestcase
313ce 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 32 35 29 3b  (yyruleno==325);
313cf 0a 7b 73 71 6c 69 74 65 33 56 74 61 62 41 72 67  .{sqlite3VtabArg
313d0 45 78 74 65 6e 64 28 70 50 61 72 73 65 2c 26 79  Extend(pParse,&y
313d1 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
313d2 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
313d3 61 6b 3b 0a 20 20 20 20 20 20 64 65 66 61 75 6c  ak;.      defaul
313d4 74 3a 0a 20 20 20 20 20 20 2f 2a 20 28 30 29 20  t:.      /* (0) 
313d5 69 6e 70 75 74 20 3a 3a 3d 20 63 6d 64 6c 69 73  input ::= cmdlis
313d6 74 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  t */ yytestcase(
313d7 79 79 72 75 6c 65 6e 6f 3d 3d 30 29 3b 0a 20 20  yyruleno==0);.  
313d8 20 20 20 20 2f 2a 20 28 31 29 20 63 6d 64 6c 69      /* (1) cmdli
313d9 73 74 20 3a 3a 3d 20 63 6d 64 6c 69 73 74 20 65  st ::= cmdlist e
313da 63 6d 64 20 2a 2f 20 79 79 74 65 73 74 63 61 73  cmd */ yytestcas
313db 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 29 3b 0a  e(yyruleno==1);.
313dc 20 20 20 20 20 20 2f 2a 20 28 32 29 20 63 6d 64        /* (2) cmd
313dd 6c 69 73 74 20 3a 3a 3d 20 65 63 6d 64 20 2a 2f  list ::= ecmd */
313de 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
313df 6c 65 6e 6f 3d 3d 32 29 3b 0a 20 20 20 20 20 20  leno==2);.      
313e0 2f 2a 20 28 33 29 20 65 63 6d 64 20 3a 3a 3d 20  /* (3) ecmd ::= 
313e1 53 45 4d 49 20 2a 2f 20 79 79 74 65 73 74 63 61  SEMI */ yytestca
313e2 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 29 3b  se(yyruleno==3);
313e3 0a 20 20 20 20 20 20 2f 2a 20 28 34 29 20 65 63  .      /* (4) ec
313e4 6d 64 20 3a 3a 3d 20 65 78 70 6c 61 69 6e 20 63  md ::= explain c
313e5 6d 64 78 20 53 45 4d 49 20 2a 2f 20 79 79 74 65  mdx SEMI */ yyte
313e6 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
313e7 3d 34 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 31  =4);.      /* (1
313e8 30 29 20 74 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d  0) trans_opt ::=
313e9 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
313ea 79 72 75 6c 65 6e 6f 3d 3d 31 30 29 3b 0a 20 20  yruleno==10);.  
313eb 20 20 20 20 2f 2a 20 28 31 31 29 20 74 72 61 6e      /* (11) tran
313ec 73 5f 6f 70 74 20 3a 3a 3d 20 54 52 41 4e 53 41  s_opt ::= TRANSA
313ed 43 54 49 4f 4e 20 2a 2f 20 79 79 74 65 73 74 63  CTION */ yytestc
313ee 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 31  ase(yyruleno==11
313ef 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 31 32 29  );.      /* (12)
313f0 20 74 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d 20 54   trans_opt ::= T
313f1 52 41 4e 53 41 43 54 49 4f 4e 20 6e 6d 20 2a 2f  RANSACTION nm */
313f2 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
313f3 6c 65 6e 6f 3d 3d 31 32 29 3b 0a 20 20 20 20 20  leno==12);.     
313f4 20 2f 2a 20 28 32 30 29 20 73 61 76 65 70 6f 69   /* (20) savepoi
313f5 6e 74 5f 6f 70 74 20 3a 3a 3d 20 53 41 56 45 50  nt_opt ::= SAVEP
313f6 4f 49 4e 54 20 2a 2f 20 79 79 74 65 73 74 63 61  OINT */ yytestca
313f7 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 30 29  se(yyruleno==20)
313f8 3b 0a 20 20 20 20 20 20 2f 2a 20 28 32 31 29 20  ;.      /* (21) 
313f9 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74 20 3a 3a  savepoint_opt ::
313fa 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  = */ yytestcase(
313fb 79 79 72 75 6c 65 6e 6f 3d 3d 32 31 29 3b 0a 20  yyruleno==21);. 
313fc 20 20 20 20 20 2f 2a 20 28 32 35 29 20 63 6d 64       /* (25) cmd
313fd 20 3a 3a 3d 20 63 72 65 61 74 65 5f 74 61 62 6c   ::= create_tabl
313fe 65 20 63 72 65 61 74 65 5f 74 61 62 6c 65 5f 61  e create_table_a
313ff 72 67 73 20 2a 2f 20 79 79 74 65 73 74 63 61 73  rgs */ yytestcas
31400 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 35 29 3b  e(yyruleno==25);
31401 0a 20 20 20 20 20 20 2f 2a 20 28 33 34 29 20 63  .      /* (34) c
31402 6f 6c 75 6d 6e 6c 69 73 74 20 3a 3a 3d 20 63 6f  olumnlist ::= co
31403 6c 75 6d 6e 6c 69 73 74 20 43 4f 4d 4d 41 20 63  lumnlist COMMA c
31404 6f 6c 75 6d 6e 20 2a 2f 20 79 79 74 65 73 74 63  olumn */ yytestc
31405 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 34  ase(yyruleno==34
31406 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 33 35 29  );.      /* (35)
31407 20 63 6f 6c 75 6d 6e 6c 69 73 74 20 3a 3a 3d 20   columnlist ::= 
31408 63 6f 6c 75 6d 6e 20 2a 2f 20 79 79 74 65 73 74  column */ yytest
31409 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33  case(yyruleno==3
3140a 35 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 34 34  5);.      /* (44
3140b 29 20 74 79 70 65 20 3a 3a 3d 20 2a 2f 20 79 79  ) type ::= */ yy
3140c 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
3140d 6f 3d 3d 34 34 29 3b 0a 20 20 20 20 20 20 2f 2a  o==44);.      /*
3140e 20 28 35 31 29 20 73 69 67 6e 65 64 20 3a 3a 3d   (51) signed ::=
3140f 20 70 6c 75 73 5f 6e 75 6d 20 2a 2f 20 79 79 74   plus_num */ yyt
31410 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
31411 3d 3d 35 31 29 3b 0a 20 20 20 20 20 20 2f 2a 20  ==51);.      /* 
31412 28 35 32 29 20 73 69 67 6e 65 64 20 3a 3a 3d 20  (52) signed ::= 
31413 6d 69 6e 75 73 5f 6e 75 6d 20 2a 2f 20 79 79 74  minus_num */ yyt
31414 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
31415 3d 3d 35 32 29 3b 0a 20 20 20 20 20 20 2f 2a 20  ==52);.      /* 
31416 28 35 33 29 20 63 61 72 67 6c 69 73 74 20 3a 3a  (53) carglist ::
31417 3d 20 63 61 72 67 6c 69 73 74 20 63 61 72 67 20  = carglist carg 
31418 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
31419 72 75 6c 65 6e 6f 3d 3d 35 33 29 3b 0a 20 20 20  ruleno==53);.   
3141a 20 20 20 2f 2a 20 28 35 34 29 20 63 61 72 67 6c     /* (54) cargl
3141b 69 73 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73  ist ::= */ yytes
3141c 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
3141d 35 34 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 35  54);.      /* (5
3141e 35 29 20 63 61 72 67 20 3a 3a 3d 20 43 4f 4e 53  5) carg ::= CONS
3141f 54 52 41 49 4e 54 20 6e 6d 20 63 63 6f 6e 73 20  TRAINT nm ccons 
31420 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
31421 72 75 6c 65 6e 6f 3d 3d 35 35 29 3b 0a 20 20 20  ruleno==55);.   
31422 20 20 20 2f 2a 20 28 35 36 29 20 63 61 72 67 20     /* (56) carg 
31423 3a 3a 3d 20 63 63 6f 6e 73 20 2a 2f 20 79 79 74  ::= ccons */ yyt
31424 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
31425 3d 3d 35 36 29 3b 0a 20 20 20 20 20 20 2f 2a 20  ==56);.      /* 
31426 28 36 32 29 20 63 63 6f 6e 73 20 3a 3a 3d 20 4e  (62) ccons ::= N
31427 55 4c 4c 20 6f 6e 63 6f 6e 66 20 2a 2f 20 79 79  ULL onconf */ yy
31428 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
31429 6f 3d 3d 36 32 29 3b 0a 20 20 20 20 20 20 2f 2a  o==62);.      /*
3142a 20 28 38 39 29 20 63 6f 6e 73 6c 69 73 74 20 3a   (89) conslist :
3142b 3a 3d 20 63 6f 6e 73 6c 69 73 74 20 43 4f 4d 4d  := conslist COMM
3142c 41 20 74 63 6f 6e 73 20 2a 2f 20 79 79 74 65 73  A tcons */ yytes
3142d 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
3142e 38 39 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 39  89);.      /* (9
3142f 30 29 20 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20  0) conslist ::= 
31430 63 6f 6e 73 6c 69 73 74 20 74 63 6f 6e 73 20 2a  conslist tcons *
31431 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
31432 75 6c 65 6e 6f 3d 3d 39 30 29 3b 0a 20 20 20 20  uleno==90);.    
31433 20 20 2f 2a 20 28 39 31 29 20 63 6f 6e 73 6c 69    /* (91) consli
31434 73 74 20 3a 3a 3d 20 74 63 6f 6e 73 20 2a 2f 20  st ::= tcons */ 
31435 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
31436 65 6e 6f 3d 3d 39 31 29 3b 0a 20 20 20 20 20 20  eno==91);.      
31437 2f 2a 20 28 39 32 29 20 74 63 6f 6e 73 20 3a 3a  /* (92) tcons ::
31438 3d 20 43 4f 4e 53 54 52 41 49 4e 54 20 6e 6d 20  = CONSTRAINT nm 
31439 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
3143a 72 75 6c 65 6e 6f 3d 3d 39 32 29 3b 0a 20 20 20  ruleno==92);.   
3143b 20 20 20 2f 2a 20 28 32 36 38 29 20 70 6c 75 73     /* (268) plus
3143c 5f 6f 70 74 20 3a 3a 3d 20 50 4c 55 53 20 2a 2f  _opt ::= PLUS */
3143d 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3143e 6c 65 6e 6f 3d 3d 32 36 38 29 3b 0a 20 20 20 20  leno==268);.    
3143f 20 20 2f 2a 20 28 32 36 39 29 20 70 6c 75 73 5f    /* (269) plus_
31440 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73  opt ::= */ yytes
31441 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
31442 32 36 39 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28  269);.      /* (
31443 32 37 39 29 20 66 6f 72 65 61 63 68 5f 63 6c 61  279) foreach_cla
31444 75 73 65 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73  use ::= */ yytes
31445 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
31446 32 37 39 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28  279);.      /* (
31447 32 38 30 29 20 66 6f 72 65 61 63 68 5f 63 6c 61  280) foreach_cla
31448 75 73 65 20 3a 3a 3d 20 46 4f 52 20 45 41 43 48  use ::= FOR EACH
31449 20 52 4f 57 20 2a 2f 20 79 79 74 65 73 74 63 61   ROW */ yytestca
3144a 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 38 30  se(yyruleno==280
3144b 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 32 38 37  );.      /* (287
3144c 29 20 74 72 69 64 78 62 79 20 3a 3a 3d 20 2a 2f  ) tridxby ::= */
3144d 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3144e 6c 65 6e 6f 3d 3d 32 38 37 29 3b 0a 20 20 20 20  leno==287);.    
3144f 20 20 2f 2a 20 28 33 30 35 29 20 64 61 74 61 62    /* (305) datab
31450 61 73 65 5f 6b 77 5f 6f 70 74 20 3a 3a 3d 20 44  ase_kw_opt ::= D
31451 41 54 41 42 41 53 45 20 2a 2f 20 79 79 74 65 73  ATABASE */ yytes
31452 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
31453 33 30 35 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28  305);.      /* (
31454 33 30 36 29 20 64 61 74 61 62 61 73 65 5f 6b 77  306) database_kw
31455 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65  _opt ::= */ yyte
31456 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
31457 3d 33 30 36 29 3b 0a 20 20 20 20 20 20 2f 2a 20  =306);.      /* 
31458 28 33 31 34 29 20 6b 77 63 6f 6c 75 6d 6e 5f 6f  (314) kwcolumn_o
31459 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74  pt ::= */ yytest
3145a 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33  case(yyruleno==3
3145b 31 34 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 33  14);.      /* (3
3145c 31 35 29 20 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74  15) kwcolumn_opt
3145d 20 3a 3a 3d 20 43 4f 4c 55 4d 4e 4b 57 20 2a 2f   ::= COLUMNKW */
3145e 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3145f 6c 65 6e 6f 3d 3d 33 31 35 29 3b 0a 20 20 20 20  leno==315);.    
31460 20 20 2f 2a 20 28 33 31 39 29 20 76 74 61 62 61    /* (319) vtaba
31461 72 67 6c 69 73 74 20 3a 3a 3d 20 76 74 61 62 61  rglist ::= vtaba
31462 72 67 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  rg */ yytestcase
31463 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 31 39 29 3b  (yyruleno==319);
31464 0a 20 20 20 20 20 20 2f 2a 20 28 33 32 30 29 20  .      /* (320) 
31465 76 74 61 62 61 72 67 6c 69 73 74 20 3a 3a 3d 20  vtabarglist ::= 
31466 76 74 61 62 61 72 67 6c 69 73 74 20 43 4f 4d 4d  vtabarglist COMM
31467 41 20 76 74 61 62 61 72 67 20 2a 2f 20 79 79 74  A vtabarg */ yyt
31468 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
31469 3d 3d 33 32 30 29 3b 0a 20 20 20 20 20 20 2f 2a  ==320);.      /*
3146a 20 28 33 32 32 29 20 76 74 61 62 61 72 67 20 3a   (322) vtabarg :
3146b 3a 3d 20 76 74 61 62 61 72 67 20 76 74 61 62 61  := vtabarg vtaba
3146c 72 67 74 6f 6b 65 6e 20 2a 2f 20 79 79 74 65 73  rgtoken */ yytes
3146d 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
3146e 33 32 32 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28  322);.      /* (
3146f 33 32 36 29 20 61 6e 79 6c 69 73 74 20 3a 3a 3d  326) anylist ::=
31470 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
31471 79 72 75 6c 65 6e 6f 3d 3d 33 32 36 29 3b 0a 20  yruleno==326);. 
31472 20 20 20 20 20 2f 2a 20 28 33 32 37 29 20 61 6e       /* (327) an
31473 79 6c 69 73 74 20 3a 3a 3d 20 61 6e 79 6c 69 73  ylist ::= anylis
31474 74 20 4c 50 20 61 6e 79 6c 69 73 74 20 52 50 20  t LP anylist RP 
31475 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
31476 72 75 6c 65 6e 6f 3d 3d 33 32 37 29 3b 0a 20 20  ruleno==327);.  
31477 20 20 20 20 2f 2a 20 28 33 32 38 29 20 61 6e 79      /* (328) any
31478 6c 69 73 74 20 3a 3a 3d 20 61 6e 79 6c 69 73 74  list ::= anylist
31479 20 41 4e 59 20 2a 2f 20 79 79 74 65 73 74 63 61   ANY */ yytestca
3147a 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 32 38  se(yyruleno==328
3147b 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  );.        break
3147c 3b 0a 20 20 7d 3b 0a 20 20 79 79 67 6f 74 6f 20  ;.  };.  yygoto 
3147d 3d 20 79 79 52 75 6c 65 49 6e 66 6f 5b 79 79 72  = yyRuleInfo[yyr
3147e 75 6c 65 6e 6f 5d 2e 6c 68 73 3b 0a 20 20 79 79  uleno].lhs;.  yy
3147f 73 69 7a 65 20 3d 20 79 79 52 75 6c 65 49 6e 66  size = yyRuleInf
31480 6f 5b 79 79 72 75 6c 65 6e 6f 5d 2e 6e 72 68 73  o[yyruleno].nrhs
31481 3b 0a 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79  ;.  yypParser->y
31482 79 69 64 78 20 2d 3d 20 79 79 73 69 7a 65 3b 0a  yidx -= yysize;.
31483 20 20 79 79 61 63 74 20 3d 20 79 79 5f 66 69 6e    yyact = yy_fin
31484 64 5f 72 65 64 75 63 65 5f 61 63 74 69 6f 6e 28  d_reduce_action(
31485 79 79 6d 73 70 5b 2d 79 79 73 69 7a 65 5d 2e 73  yymsp[-yysize].s
31486 74 61 74 65 6e 6f 2c 28 59 59 43 4f 44 45 54 59  tateno,(YYCODETY
31487 50 45 29 79 79 67 6f 74 6f 29 3b 0a 20 20 69 66  PE)yygoto);.  if
31488 28 20 79 79 61 63 74 20 3c 20 59 59 4e 53 54 41  ( yyact < YYNSTA
31489 54 45 20 29 7b 0a 23 69 66 64 65 66 20 4e 44 45  TE ){.#ifdef NDE
3148a 42 55 47 0a 20 20 20 20 2f 2a 20 49 66 20 77 65  BUG.    /* If we
3148b 20 61 72 65 20 6e 6f 74 20 64 65 62 75 67 67 69   are not debuggi
3148c 6e 67 20 61 6e 64 20 74 68 65 20 72 65 64 75 63  ng and the reduc
3148d 65 20 61 63 74 69 6f 6e 20 70 6f 70 70 65 64 20  e action popped 
3148e 61 74 20 6c 65 61 73 74 0a 20 20 20 20 2a 2a 20  at least.    ** 
3148f 6f 6e 65 20 65 6c 65 6d 65 6e 74 20 6f 66 66 20  one element off 
31490 74 68 65 20 73 74 61 63 6b 2c 20 74 68 65 6e 20  the stack, then 
31491 77 65 20 63 61 6e 20 70 75 73 68 20 74 68 65 20  we can push the 
31492 6e 65 77 20 65 6c 65 6d 65 6e 74 20 62 61 63 6b  new element back
31493 0a 20 20 20 20 2a 2a 20 6f 6e 74 6f 20 74 68 65  .    ** onto the
31494 20 73 74 61 63 6b 20 68 65 72 65 2c 20 61 6e 64   stack here, and
31495 20 73 6b 69 70 20 74 68 65 20 73 74 61 63 6b 20   skip the stack 
31496 6f 76 65 72 66 6c 6f 77 20 74 65 73 74 20 69 6e  overflow test in
31497 20 79 79 5f 73 68 69 66 74 28 29 2e 0a 20 20 20   yy_shift()..   
31498 20 2a 2a 20 54 68 61 74 20 67 69 76 65 73 20 61   ** That gives a
31499 20 73 69 67 6e 69 66 69 63 61 6e 74 20 73 70 65   significant spe
3149a 65 64 20 69 6d 70 72 6f 76 65 6d 65 6e 74 2e 20  ed improvement. 
3149b 2a 2f 0a 20 20 20 20 69 66 28 20 79 79 73 69 7a  */.    if( yysiz
3149c 65 20 29 7b 0a 20 20 20 20 20 20 79 79 70 50 61  e ){.      yypPa
3149d 72 73 65 72 2d 3e 79 79 69 64 78 2b 2b 3b 0a 20  rser->yyidx++;. 
3149e 20 20 20 20 20 79 79 6d 73 70 20 2d 3d 20 79 79       yymsp -= yy
3149f 73 69 7a 65 2d 31 3b 0a 20 20 20 20 20 20 79 79  size-1;.      yy
314a0 6d 73 70 2d 3e 73 74 61 74 65 6e 6f 20 3d 20 28  msp->stateno = (
314a1 59 59 41 43 54 49 4f 4e 54 59 50 45 29 79 79 61  YYACTIONTYPE)yya
314a2 63 74 3b 0a 20 20 20 20 20 20 79 79 6d 73 70 2d  ct;.      yymsp-
314a3 3e 6d 61 6a 6f 72 20 3d 20 28 59 59 43 4f 44 45  >major = (YYCODE
314a4 54 59 50 45 29 79 79 67 6f 74 6f 3b 0a 20 20 20  TYPE)yygoto;.   
314a5 20 20 20 79 79 6d 73 70 2d 3e 6d 69 6e 6f 72 20     yymsp->minor 
314a6 3d 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 3b 0a 20  = yygotominor;. 
314a7 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a     }else.#endif.
314a8 20 20 20 20 7b 0a 20 20 20 20 20 20 79 79 5f 73      {.      yy_s
314a9 68 69 66 74 28 79 79 70 50 61 72 73 65 72 2c 79  hift(yypParser,y
314aa 79 61 63 74 2c 79 79 67 6f 74 6f 2c 26 79 79 67  yact,yygoto,&yyg
314ab 6f 74 6f 6d 69 6e 6f 72 29 3b 0a 20 20 20 20 7d  otominor);.    }
314ac 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
314ad 73 65 72 74 28 20 79 79 61 63 74 20 3d 3d 20 59  sert( yyact == Y
314ae 59 4e 53 54 41 54 45 20 2b 20 59 59 4e 52 55 4c  YNSTATE + YYNRUL
314af 45 20 2b 20 31 20 29 3b 0a 20 20 20 20 79 79 5f  E + 1 );.    yy_
314b0 61 63 63 65 70 74 28 79 79 70 50 61 72 73 65 72  accept(yypParser
314b1 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
314b2 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f  The following co
314b3 64 65 20 65 78 65 63 75 74 65 73 20 77 68 65 6e  de executes when
314b4 20 74 68 65 20 70 61 72 73 65 20 66 61 69 6c 73   the parse fails
314b5 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 59 59 4e 4f  .*/.#ifndef YYNO
314b6 45 52 52 4f 52 52 45 43 4f 56 45 52 59 0a 73 74  ERRORRECOVERY.st
314b7 61 74 69 63 20 76 6f 69 64 20 79 79 5f 70 61 72  atic void yy_par
314b8 73 65 5f 66 61 69 6c 65 64 28 0a 20 20 79 79 50  se_failed(.  yyP
314b9 61 72 73 65 72 20 2a 79 79 70 50 61 72 73 65 72  arser *yypParser
314ba 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
314bb 65 20 70 61 72 73 65 72 20 2a 2f 0a 29 7b 0a 20  e parser */.){. 
314bc 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52   sqlite3ParserAR
314bd 47 5f 46 45 54 43 48 3b 0a 23 69 66 6e 64 65 66  G_FETCH;.#ifndef
314be 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20 79 79   NDEBUG.  if( yy
314bf 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20  TraceFILE ){.   
314c0 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65   fprintf(yyTrace
314c1 46 49 4c 45 2c 22 25 73 46 61 69 6c 21 5c 6e 22  FILE,"%sFail!\n"
314c2 2c 79 79 54 72 61 63 65 50 72 6f 6d 70 74 29 3b  ,yyTracePrompt);
314c3 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 77 68  .  }.#endif.  wh
314c4 69 6c 65 28 20 79 79 70 50 61 72 73 65 72 2d 3e  ile( yypParser->
314c5 79 79 69 64 78 3e 3d 30 20 29 20 79 79 5f 70 6f  yyidx>=0 ) yy_po
314c6 70 5f 70 61 72 73 65 72 5f 73 74 61 63 6b 28 79  p_parser_stack(y
314c7 79 70 50 61 72 73 65 72 29 3b 0a 20 20 2f 2a 20  ypParser);.  /* 
314c8 48 65 72 65 20 63 6f 64 65 20 69 73 20 69 6e 73  Here code is ins
314c9 65 72 74 65 64 20 77 68 69 63 68 20 77 69 6c 6c  erted which will
314ca 20 62 65 20 65 78 65 63 75 74 65 64 20 77 68 65   be executed whe
314cb 6e 65 76 65 72 20 74 68 65 0a 20 20 2a 2a 20 70  never the.  ** p
314cc 61 72 73 65 72 20 66 61 69 6c 73 20 2a 2f 0a 20  arser fails */. 
314cd 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52   sqlite3ParserAR
314ce 47 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75 70 70  G_STORE; /* Supp
314cf 72 65 73 73 20 77 61 72 6e 69 6e 67 20 61 62 6f  ress warning abo
314d0 75 74 20 75 6e 75 73 65 64 20 25 65 78 74 72 61  ut unused %extra
314d1 5f 61 72 67 75 6d 65 6e 74 20 76 61 72 69 61 62  _argument variab
314d2 6c 65 20 2a 2f 0a 7d 0a 23 65 6e 64 69 66 20 2f  le */.}.#endif /
314d3 2a 20 59 59 4e 4f 45 52 52 4f 52 52 45 43 4f 56  * YYNOERRORRECOV
314d4 45 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  ERY */../*.** Th
314d5 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65  e following code
314d6 20 65 78 65 63 75 74 65 73 20 77 68 65 6e 20 61   executes when a
314d7 20 73 79 6e 74 61 78 20 65 72 72 6f 72 20 66 69   syntax error fi
314d8 72 73 74 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73  rst occurs..*/.s
314d9 74 61 74 69 63 20 76 6f 69 64 20 79 79 5f 73 79  tatic void yy_sy
314da 6e 74 61 78 5f 65 72 72 6f 72 28 0a 20 20 79 79  ntax_error(.  yy
314db 50 61 72 73 65 72 20 2a 79 79 70 50 61 72 73 65  Parser *yypParse
314dc 72 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  r,           /* 
314dd 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 20 20  The parser */.  
314de 69 6e 74 20 79 79 6d 61 6a 6f 72 2c 20 20 20 20  int yymajor,    
314df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
314e0 2a 20 54 68 65 20 6d 61 6a 6f 72 20 74 79 70 65  * The major type
314e1 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20 74 6f   of the error to
314e2 6b 65 6e 20 2a 2f 0a 20 20 59 59 4d 49 4e 4f 52  ken */.  YYMINOR
314e3 54 59 50 45 20 79 79 6d 69 6e 6f 72 20 20 20 20  TYPE yyminor    
314e4 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d          /* The m
314e5 69 6e 6f 72 20 74 79 70 65 20 6f 66 20 74 68 65  inor type of the
314e6 20 65 72 72 6f 72 20 74 6f 6b 65 6e 20 2a 2f 0a   error token */.
314e7 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73  ){.  sqlite3Pars
314e8 65 72 41 52 47 5f 46 45 54 43 48 3b 0a 23 64 65  erARG_FETCH;.#de
314e9 66 69 6e 65 20 54 4f 4b 45 4e 20 28 79 79 6d 69  fine TOKEN (yymi
314ea 6e 6f 72 2e 79 79 30 29 0a 0a 20 20 55 4e 55 53  nor.yy0)..  UNUS
314eb 45 44 5f 50 41 52 41 4d 45 54 45 52 28 79 79 6d  ED_PARAMETER(yym
314ec 61 6a 6f 72 29 3b 20 20 2f 2a 20 53 69 6c 65 6e  ajor);  /* Silen
314ed 63 65 20 73 6f 6d 65 20 63 6f 6d 70 69 6c 65 72  ce some compiler
314ee 20 77 61 72 6e 69 6e 67 73 20 2a 2f 0a 20 20 61   warnings */.  a
314ef 73 73 65 72 74 28 20 54 4f 4b 45 4e 2e 7a 5b 30  ssert( TOKEN.z[0
314f0 5d 20 29 3b 20 20 2f 2a 20 54 68 65 20 74 6f 6b  ] );  /* The tok
314f1 65 6e 69 7a 65 72 20 61 6c 77 61 79 73 20 67 69  enizer always gi
314f2 76 65 73 20 75 73 20 61 20 74 6f 6b 65 6e 20 2a  ves us a token *
314f3 2f 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72  /.  sqlite3Error
314f4 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 65 61  Msg(pParse, "nea
314f5 72 20 5c 22 25 54 5c 22 3a 20 73 79 6e 74 61 78  r \"%T\": syntax
314f6 20 65 72 72 6f 72 22 2c 20 26 54 4f 4b 45 4e 29   error", &TOKEN)
314f7 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70 61 72 73  ;.  pParse->pars
314f8 65 45 72 72 6f 72 20 3d 20 31 3b 0a 20 20 73 71  eError = 1;.  sq
314f9 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 53  lite3ParserARG_S
314fa 54 4f 52 45 3b 20 2f 2a 20 53 75 70 70 72 65 73  TORE; /* Suppres
314fb 73 20 77 61 72 6e 69 6e 67 20 61 62 6f 75 74 20  s warning about 
314fc 75 6e 75 73 65 64 20 25 65 78 74 72 61 5f 61 72  unused %extra_ar
314fd 67 75 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 20  gument variable 
314fe 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  */.}../*.** The 
314ff 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 65 78 65  following is exe
31500 63 75 74 65 64 20 77 68 65 6e 20 74 68 65 20 70  cuted when the p
31501 61 72 73 65 72 20 61 63 63 65 70 74 73 0a 2a 2f  arser accepts.*/
31502 0a 73 74 61 74 69 63 20 76 6f 69 64 20 79 79 5f  .static void yy_
31503 61 63 63 65 70 74 28 0a 20 20 79 79 50 61 72 73  accept(.  yyPars
31504 65 72 20 2a 79 79 70 50 61 72 73 65 72 20 20 20  er *yypParser   
31505 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
31506 61 72 73 65 72 20 2a 2f 0a 29 7b 0a 20 20 73 71  arser */.){.  sq
31507 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 46  lite3ParserARG_F
31508 45 54 43 48 3b 0a 23 69 66 6e 64 65 66 20 4e 44  ETCH;.#ifndef ND
31509 45 42 55 47 0a 20 20 69 66 28 20 79 79 54 72 61  EBUG.  if( yyTra
3150a 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 66 70  ceFILE ){.    fp
3150b 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c  rintf(yyTraceFIL
3150c 45 2c 22 25 73 41 63 63 65 70 74 21 5c 6e 22 2c  E,"%sAccept!\n",
3150d 79 79 54 72 61 63 65 50 72 6f 6d 70 74 29 3b 0a  yyTracePrompt);.
3150e 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 77 68 69    }.#endif.  whi
3150f 6c 65 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79  le( yypParser->y
31510 79 69 64 78 3e 3d 30 20 29 20 79 79 5f 70 6f 70  yidx>=0 ) yy_pop
31511 5f 70 61 72 73 65 72 5f 73 74 61 63 6b 28 79 79  _parser_stack(yy
31512 70 50 61 72 73 65 72 29 3b 0a 20 20 2f 2a 20 48  pParser);.  /* H
31513 65 72 65 20 63 6f 64 65 20 69 73 20 69 6e 73 65  ere code is inse
31514 72 74 65 64 20 77 68 69 63 68 20 77 69 6c 6c 20  rted which will 
31515 62 65 20 65 78 65 63 75 74 65 64 20 77 68 65 6e  be executed when
31516 65 76 65 72 20 74 68 65 0a 20 20 2a 2a 20 70 61  ever the.  ** pa
31517 72 73 65 72 20 61 63 63 65 70 74 73 20 2a 2f 0a  rser accepts */.
31518 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41    sqlite3ParserA
31519 52 47 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75 70  RG_STORE; /* Sup
3151a 70 72 65 73 73 20 77 61 72 6e 69 6e 67 20 61 62  press warning ab
3151b 6f 75 74 20 75 6e 75 73 65 64 20 25 65 78 74 72  out unused %extr
3151c 61 5f 61 72 67 75 6d 65 6e 74 20 76 61 72 69 61  a_argument varia
3151d 62 6c 65 20 2a 2f 0a 7d 0a 0a 2f 2a 20 54 68 65  ble */.}../* The
3151e 20 6d 61 69 6e 20 70 61 72 73 65 72 20 70 72 6f   main parser pro
3151f 67 72 61 6d 2e 0a 2a 2a 20 54 68 65 20 66 69 72  gram..** The fir
31520 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61  st argument is a
31521 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74   pointer to a st
31522 72 75 63 74 75 72 65 20 6f 62 74 61 69 6e 65 64  ructure obtained
31523 20 66 72 6f 6d 0a 2a 2a 20 22 73 71 6c 69 74 65   from.** "sqlite
31524 33 50 61 72 73 65 72 41 6c 6c 6f 63 22 20 77 68  3ParserAlloc" wh
31525 69 63 68 20 64 65 73 63 72 69 62 65 73 20 74 68  ich describes th
31526 65 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 20  e current state 
31527 6f 66 20 74 68 65 20 70 61 72 73 65 72 2e 0a 2a  of the parser..*
31528 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67  * The second arg
31529 75 6d 65 6e 74 20 69 73 20 74 68 65 20 6d 61 6a  ument is the maj
3152a 6f 72 20 74 6f 6b 65 6e 20 6e 75 6d 62 65 72 2e  or token number.
3152b 20 20 54 68 65 20 74 68 69 72 64 20 69 73 0a 2a    The third is.*
3152c 2a 20 74 68 65 20 6d 69 6e 6f 72 20 74 6f 6b 65  * the minor toke
3152d 6e 2e 20 20 54 68 65 20 66 6f 75 72 74 68 20 6f  n.  The fourth o
3152e 70 74 69 6f 6e 61 6c 20 61 72 67 75 6d 65 6e 74  ptional argument
3152f 20 69 73 20 77 68 61 74 65 76 65 72 20 74 68 65   is whatever the
31530 0a 2a 2a 20 75 73 65 72 20 77 61 6e 74 73 20 28  .** user wants (
31531 61 6e 64 20 73 70 65 63 69 66 69 65 64 20 69 6e  and specified in
31532 20 74 68 65 20 67 72 61 6d 6d 61 72 29 20 61 6e   the grammar) an
31533 64 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 66  d is available f
31534 6f 72 0a 2a 2a 20 75 73 65 20 62 79 20 74 68 65  or.** use by the
31535 20 61 63 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73   action routines
31536 2e 0a 2a 2a 0a 2a 2a 20 49 6e 70 75 74 73 3a 0a  ..**.** Inputs:.
31537 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20  ** <ul>.** <li> 
31538 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  A pointer to the
31539 20 70 61 72 73 65 72 20 28 61 6e 20 6f 70 61 71   parser (an opaq
3153a 75 65 20 73 74 72 75 63 74 75 72 65 2e 29 0a 2a  ue structure.).*
3153b 2a 20 3c 6c 69 3e 20 54 68 65 20 6d 61 6a 6f 72  * <li> The major
3153c 20 74 6f 6b 65 6e 20 6e 75 6d 62 65 72 2e 0a 2a   token number..*
3153d 2a 20 3c 6c 69 3e 20 54 68 65 20 6d 69 6e 6f 72  * <li> The minor
3153e 20 74 6f 6b 65 6e 20 6e 75 6d 62 65 72 2e 0a 2a   token number..*
3153f 2a 20 3c 6c 69 3e 20 41 6e 20 6f 70 74 69 6f 6e  * <li> An option
31540 20 61 72 67 75 6d 65 6e 74 20 6f 66 20 61 20 67   argument of a g
31541 72 61 6d 6d 61 72 2d 73 70 65 63 69 66 69 65 64  rammar-specified
31542 20 74 79 70 65 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a   type..** </ul>.
31543 2a 2a 0a 2a 2a 20 4f 75 74 70 75 74 73 3a 0a 2a  **.** Outputs:.*
31544 2a 20 4e 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54  * None..*/.SQLIT
31545 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
31546 71 6c 69 74 65 33 50 61 72 73 65 72 28 0a 20 20  qlite3Parser(.  
31547 76 6f 69 64 20 2a 79 79 70 2c 20 20 20 20 20 20  void *yyp,      
31548 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
31549 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 20 20  The parser */.  
3154a 69 6e 74 20 79 79 6d 61 6a 6f 72 2c 20 20 20 20  int yymajor,    
3154b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3154c 54 68 65 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20  The major token 
3154d 63 6f 64 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20  code number */. 
3154e 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 54 4f   sqlite3ParserTO
3154f 4b 45 4e 54 59 50 45 20 79 79 6d 69 6e 6f 72 20  KENTYPE yyminor 
31550 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 61 6c        /* The val
31551 75 65 20 66 6f 72 20 74 68 65 20 74 6f 6b 65 6e  ue for the token
31552 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72   */.  sqlite3Par
31553 73 65 72 41 52 47 5f 50 44 45 43 4c 20 20 20 20  serARG_PDECL    
31554 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70             /* Op
31555 74 69 6f 6e 61 6c 20 25 65 78 74 72 61 5f 61 72  tional %extra_ar
31556 67 75 6d 65 6e 74 20 70 61 72 61 6d 65 74 65 72  gument parameter
31557 20 2a 2f 0a 29 7b 0a 20 20 59 59 4d 49 4e 4f 52   */.){.  YYMINOR
31558 54 59 50 45 20 79 79 6d 69 6e 6f 72 75 6e 69 6f  TYPE yyminorunio
31559 6e 3b 0a 20 20 69 6e 74 20 79 79 61 63 74 3b 20  n;.  int yyact; 
3155a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
3155b 65 20 70 61 72 73 65 72 20 61 63 74 69 6f 6e 2e  e parser action.
3155c 20 2a 2f 0a 20 20 69 6e 74 20 79 79 65 6e 64 6f   */.  int yyendo
3155d 66 69 6e 70 75 74 3b 20 20 20 20 20 2f 2a 20 54  finput;     /* T
3155e 72 75 65 20 69 66 20 77 65 20 61 72 65 20 61 74  rue if we are at
3155f 20 74 68 65 20 65 6e 64 20 6f 66 20 69 6e 70 75   the end of inpu
31560 74 20 2a 2f 0a 23 69 66 64 65 66 20 59 59 45 52  t */.#ifdef YYER
31561 52 4f 52 53 59 4d 42 4f 4c 0a 20 20 69 6e 74 20  RORSYMBOL.  int 
31562 79 79 65 72 72 6f 72 68 69 74 20 3d 20 30 3b 20  yyerrorhit = 0; 
31563 20 20 2f 2a 20 54 72 75 65 20 69 66 20 79 79 6d    /* True if yym
31564 61 6a 6f 72 20 68 61 73 20 69 6e 76 6f 6b 65 64  ajor has invoked
31565 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 23 65 6e   an error */.#en
31566 64 69 66 0a 20 20 79 79 50 61 72 73 65 72 20 2a  dif.  yyParser *
31567 79 79 70 50 61 72 73 65 72 3b 20 20 2f 2a 20 54  yypParser;  /* T
31568 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 0a 20 20  he parser */..  
31569 2f 2a 20 28 72 65 29 69 6e 69 74 69 61 6c 69 7a  /* (re)initializ
3156a 65 20 74 68 65 20 70 61 72 73 65 72 2c 20 69 66  e the parser, if
3156b 20 6e 65 63 65 73 73 61 72 79 20 2a 2f 0a 20 20   necessary */.  
3156c 79 79 70 50 61 72 73 65 72 20 3d 20 28 79 79 50  yypParser = (yyP
3156d 61 72 73 65 72 2a 29 79 79 70 3b 0a 20 20 69 66  arser*)yyp;.  if
3156e 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69  ( yypParser->yyi
3156f 64 78 3c 30 20 29 7b 0a 23 69 66 20 59 59 53 54  dx<0 ){.#if YYST
31570 41 43 4b 44 45 50 54 48 3c 3d 30 0a 20 20 20 20  ACKDEPTH<=0.    
31571 69 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79  if( yypParser->y
31572 79 73 74 6b 73 7a 20 3c 3d 30 20 29 7b 0a 20 20  ystksz <=0 ){.  
31573 20 20 20 20 2f 2a 6d 65 6d 73 65 74 28 26 79 79      /*memset(&yy
31574 6d 69 6e 6f 72 75 6e 69 6f 6e 2c 20 30 2c 20 73  minorunion, 0, s
31575 69 7a 65 6f 66 28 79 79 6d 69 6e 6f 72 75 6e 69  izeof(yyminoruni
31576 6f 6e 29 29 3b 2a 2f 0a 20 20 20 20 20 20 79 79  on));*/.      yy
31577 6d 69 6e 6f 72 75 6e 69 6f 6e 20 3d 20 79 79 7a  minorunion = yyz
31578 65 72 6f 6d 69 6e 6f 72 3b 0a 20 20 20 20 20 20  erominor;.      
31579 79 79 53 74 61 63 6b 4f 76 65 72 66 6c 6f 77 28  yyStackOverflow(
3157a 79 79 70 50 61 72 73 65 72 2c 20 26 79 79 6d 69  yypParser, &yymi
3157b 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20  norunion);.     
3157c 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 23   return;.    }.#
3157d 65 6e 64 69 66 0a 20 20 20 20 79 79 70 50 61 72  endif.    yypPar
3157e 73 65 72 2d 3e 79 79 69 64 78 20 3d 20 30 3b 0a  ser->yyidx = 0;.
3157f 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79      yypParser->y
31580 79 65 72 72 63 6e 74 20 3d 20 2d 31 3b 0a 20 20  yerrcnt = -1;.  
31581 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73    yypParser->yys
31582 74 61 63 6b 5b 30 5d 2e 73 74 61 74 65 6e 6f 20  tack[0].stateno 
31583 3d 20 30 3b 0a 20 20 20 20 79 79 70 50 61 72 73  = 0;.    yypPars
31584 65 72 2d 3e 79 79 73 74 61 63 6b 5b 30 5d 2e 6d  er->yystack[0].m
31585 61 6a 6f 72 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  ajor = 0;.  }.  
31586 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 2e 79 79 30  yyminorunion.yy0
31587 20 3d 20 79 79 6d 69 6e 6f 72 3b 0a 20 20 79 79   = yyminor;.  yy
31588 65 6e 64 6f 66 69 6e 70 75 74 20 3d 20 28 79 79  endofinput = (yy
31589 6d 61 6a 6f 72 3d 3d 30 29 3b 0a 20 20 73 71 6c  major==0);.  sql
3158a 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 53 54  ite3ParserARG_ST
3158b 4f 52 45 3b 0a 0a 23 69 66 6e 64 65 66 20 4e 44  ORE;..#ifndef ND
3158c 45 42 55 47 0a 20 20 69 66 28 20 79 79 54 72 61  EBUG.  if( yyTra
3158d 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 66 70  ceFILE ){.    fp
3158e 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c  rintf(yyTraceFIL
3158f 45 2c 22 25 73 49 6e 70 75 74 20 25 73 5c 6e 22  E,"%sInput %s\n"
31590 2c 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 79  ,yyTracePrompt,y
31591 79 54 6f 6b 65 6e 4e 61 6d 65 5b 79 79 6d 61 6a  yTokenName[yymaj
31592 6f 72 5d 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  or]);.  }.#endif
31593 0a 0a 20 20 64 6f 7b 0a 20 20 20 20 79 79 61 63  ..  do{.    yyac
31594 74 20 3d 20 79 79 5f 66 69 6e 64 5f 73 68 69 66  t = yy_find_shif
31595 74 5f 61 63 74 69 6f 6e 28 79 79 70 50 61 72 73  t_action(yypPars
31596 65 72 2c 28 59 59 43 4f 44 45 54 59 50 45 29 79  er,(YYCODETYPE)y
31597 79 6d 61 6a 6f 72 29 3b 0a 20 20 20 20 69 66 28  ymajor);.    if(
31598 20 79 79 61 63 74 3c 59 59 4e 53 54 41 54 45 20   yyact<YYNSTATE 
31599 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
3159a 20 21 79 79 65 6e 64 6f 66 69 6e 70 75 74 20 29   !yyendofinput )
3159b 3b 20 20 2f 2a 20 49 6d 70 6f 73 73 69 62 6c 65  ;  /* Impossible
3159c 20 74 6f 20 73 68 69 66 74 20 74 68 65 20 24 20   to shift the $ 
3159d 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20 20 20 79  token */.      y
3159e 79 5f 73 68 69 66 74 28 79 79 70 50 61 72 73 65  y_shift(yypParse
3159f 72 2c 79 79 61 63 74 2c 79 79 6d 61 6a 6f 72 2c  r,yyact,yymajor,
315a0 26 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a  &yyminorunion);.
315a1 20 20 20 20 20 20 79 79 70 50 61 72 73 65 72 2d        yypParser-
315a2 3e 79 79 65 72 72 63 6e 74 2d 2d 3b 0a 20 20 20  >yyerrcnt--;.   
315a3 20 20 20 79 79 6d 61 6a 6f 72 20 3d 20 59 59 4e     yymajor = YYN
315a4 4f 43 4f 44 45 3b 0a 20 20 20 20 7d 65 6c 73 65  OCODE;.    }else
315a5 20 69 66 28 20 79 79 61 63 74 20 3c 20 59 59 4e   if( yyact < YYN
315a6 53 54 41 54 45 20 2b 20 59 59 4e 52 55 4c 45 20  STATE + YYNRULE 
315a7 29 7b 0a 20 20 20 20 20 20 79 79 5f 72 65 64 75  ){.      yy_redu
315a8 63 65 28 79 79 70 50 61 72 73 65 72 2c 79 79 61  ce(yypParser,yya
315a9 63 74 2d 59 59 4e 53 54 41 54 45 29 3b 0a 20 20  ct-YYNSTATE);.  
315aa 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61    }else{.      a
315ab 73 73 65 72 74 28 20 79 79 61 63 74 20 3d 3d 20  ssert( yyact == 
315ac 59 59 5f 45 52 52 4f 52 5f 41 43 54 49 4f 4e 20  YY_ERROR_ACTION 
315ad 29 3b 0a 23 69 66 64 65 66 20 59 59 45 52 52 4f  );.#ifdef YYERRO
315ae 52 53 59 4d 42 4f 4c 0a 20 20 20 20 20 20 69 6e  RSYMBOL.      in
315af 74 20 79 79 6d 78 3b 0a 23 65 6e 64 69 66 0a 23  t yymx;.#endif.#
315b0 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20  ifndef NDEBUG.  
315b1 20 20 20 20 69 66 28 20 79 79 54 72 61 63 65 46      if( yyTraceF
315b2 49 4c 45 20 29 7b 0a 20 20 20 20 20 20 20 20 66  ILE ){.        f
315b3 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49  printf(yyTraceFI
315b4 4c 45 2c 22 25 73 53 79 6e 74 61 78 20 45 72 72  LE,"%sSyntax Err
315b5 6f 72 21 5c 6e 22 2c 79 79 54 72 61 63 65 50 72  or!\n",yyTracePr
315b6 6f 6d 70 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23  ompt);.      }.#
315b7 65 6e 64 69 66 0a 23 69 66 64 65 66 20 59 59 45  endif.#ifdef YYE
315b8 52 52 4f 52 53 59 4d 42 4f 4c 0a 20 20 20 20 20  RRORSYMBOL.     
315b9 20 2f 2a 20 41 20 73 79 6e 74 61 78 20 65 72 72   /* A syntax err
315ba 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e  or has occurred.
315bb 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 20 72 65  .      ** The re
315bc 73 70 6f 6e 73 65 20 74 6f 20 61 6e 20 65 72 72  sponse to an err
315bd 6f 72 20 64 65 70 65 6e 64 73 20 75 70 6f 6e 20  or depends upon 
315be 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  whether or not t
315bf 68 65 0a 20 20 20 20 20 20 2a 2a 20 67 72 61 6d  he.      ** gram
315c0 6d 61 72 20 64 65 66 69 6e 65 73 20 61 6e 20 65  mar defines an e
315c1 72 72 6f 72 20 74 6f 6b 65 6e 20 22 45 52 52 4f  rror token "ERRO
315c2 52 22 2e 20 20 0a 20 20 20 20 20 20 2a 2a 0a 20  R".  .      **. 
315c3 20 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20       ** This is 
315c4 77 68 61 74 20 77 65 20 64 6f 20 69 66 20 74 68  what we do if th
315c5 65 20 67 72 61 6d 6d 61 72 20 64 6f 65 73 20 64  e grammar does d
315c6 65 66 69 6e 65 20 45 52 52 4f 52 3a 0a 20 20 20  efine ERROR:.   
315c7 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20     **.      **  
315c8 2a 20 43 61 6c 6c 20 74 68 65 20 25 73 79 6e 74  * Call the %synt
315c9 61 78 5f 65 72 72 6f 72 20 66 75 6e 63 74 69 6f  ax_error functio
315ca 6e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  n..      **.    
315cb 20 20 2a 2a 20 20 2a 20 42 65 67 69 6e 20 70 6f    **  * Begin po
315cc 70 70 69 6e 67 20 74 68 65 20 73 74 61 63 6b 20  pping the stack 
315cd 75 6e 74 69 6c 20 77 65 20 65 6e 74 65 72 20 61  until we enter a
315ce 20 73 74 61 74 65 20 77 68 65 72 65 0a 20 20 20   state where.   
315cf 20 20 20 2a 2a 20 20 20 20 69 74 20 69 73 20 6c     **    it is l
315d0 65 67 61 6c 20 74 6f 20 73 68 69 66 74 20 74 68  egal to shift th
315d1 65 20 65 72 72 6f 72 20 73 79 6d 62 6f 6c 2c 20  e error symbol, 
315d2 74 68 65 6e 20 73 68 69 66 74 0a 20 20 20 20 20  then shift.     
315d3 20 2a 2a 20 20 20 20 74 68 65 20 65 72 72 6f 72   **    the error
315d4 20 73 79 6d 62 6f 6c 2e 0a 20 20 20 20 20 20 2a   symbol..      *
315d5 2a 0a 20 20 20 20 20 20 2a 2a 20 20 2a 20 53 65  *.      **  * Se
315d6 74 20 74 68 65 20 65 72 72 6f 72 20 63 6f 75 6e  t the error coun
315d7 74 20 74 6f 20 74 68 72 65 65 2e 0a 20 20 20 20  t to three..    
315d8 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 2a    **.      **  *
315d9 20 42 65 67 69 6e 20 61 63 63 65 70 74 69 6e 67   Begin accepting
315da 20 61 6e 64 20 73 68 69 66 74 69 6e 67 20 6e 65   and shifting ne
315db 77 20 74 6f 6b 65 6e 73 2e 20 20 4e 6f 20 6e 65  w tokens.  No ne
315dc 77 20 65 72 72 6f 72 0a 20 20 20 20 20 20 2a 2a  w error.      **
315dd 20 20 20 20 70 72 6f 63 65 73 73 69 6e 67 20 77      processing w
315de 69 6c 6c 20 6f 63 63 75 72 20 75 6e 74 69 6c 20  ill occur until 
315df 74 68 72 65 65 20 74 6f 6b 65 6e 73 20 68 61 76  three tokens hav
315e0 65 20 62 65 65 6e 0a 20 20 20 20 20 20 2a 2a 20  e been.      ** 
315e1 20 20 20 73 68 69 66 74 65 64 20 73 75 63 63 65     shifted succe
315e2 73 73 66 75 6c 6c 79 2e 0a 20 20 20 20 20 20 2a  ssfully..      *
315e3 2a 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  *.      */.     
315e4 20 69 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e   if( yypParser->
315e5 79 79 65 72 72 63 6e 74 3c 30 20 29 7b 0a 20 20  yyerrcnt<0 ){.  
315e6 20 20 20 20 20 20 79 79 5f 73 79 6e 74 61 78 5f        yy_syntax_
315e7 65 72 72 6f 72 28 79 79 70 50 61 72 73 65 72 2c  error(yypParser,
315e8 79 79 6d 61 6a 6f 72 2c 79 79 6d 69 6e 6f 72 75  yymajor,yyminoru
315e9 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20  nion);.      }. 
315ea 20 20 20 20 20 79 79 6d 78 20 3d 20 79 79 70 50       yymx = yypP
315eb 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 79  arser->yystack[y
315ec 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 5d  ypParser->yyidx]
315ed 2e 6d 61 6a 6f 72 3b 0a 20 20 20 20 20 20 69 66  .major;.      if
315ee 28 20 79 79 6d 78 3d 3d 59 59 45 52 52 4f 52 53  ( yymx==YYERRORS
315ef 59 4d 42 4f 4c 20 7c 7c 20 79 79 65 72 72 6f 72  YMBOL || yyerror
315f0 68 69 74 20 29 7b 0a 23 69 66 6e 64 65 66 20 4e  hit ){.#ifndef N
315f1 44 45 42 55 47 0a 20 20 20 20 20 20 20 20 69 66  DEBUG.        if
315f2 28 20 79 79 54 72 61 63 65 46 49 4c 45 20 29 7b  ( yyTraceFILE ){
315f3 0a 20 20 20 20 20 20 20 20 20 20 66 70 72 69 6e  .          fprin
315f4 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22  tf(yyTraceFILE,"
315f5 25 73 44 69 73 63 61 72 64 20 69 6e 70 75 74 20  %sDiscard input 
315f6 74 6f 6b 65 6e 20 25 73 5c 6e 22 2c 0a 20 20 20  token %s\n",.   
315f7 20 20 20 20 20 20 20 20 20 20 79 79 54 72 61 63            yyTrac
315f8 65 50 72 6f 6d 70 74 2c 79 79 54 6f 6b 65 6e 4e  ePrompt,yyTokenN
315f9 61 6d 65 5b 79 79 6d 61 6a 6f 72 5d 29 3b 0a 20  ame[yymajor]);. 
315fa 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a         }.#endif.
315fb 20 20 20 20 20 20 20 20 79 79 5f 64 65 73 74 72          yy_destr
315fc 75 63 74 6f 72 28 79 79 70 50 61 72 73 65 72 2c  uctor(yypParser,
315fd 20 28 59 59 43 4f 44 45 54 59 50 45 29 79 79 6d   (YYCODETYPE)yym
315fe 61 6a 6f 72 2c 26 79 79 6d 69 6e 6f 72 75 6e 69  ajor,&yyminoruni
315ff 6f 6e 29 3b 0a 20 20 20 20 20 20 20 20 79 79 6d  on);.        yym
31600 61 6a 6f 72 20 3d 20 59 59 4e 4f 43 4f 44 45 3b  ajor = YYNOCODE;
31601 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
31602 20 20 20 20 20 20 20 77 68 69 6c 65 28 0a 20 20         while(.  
31603 20 20 20 20 20 20 20 20 79 79 70 50 61 72 73 65          yypParse
31604 72 2d 3e 79 79 69 64 78 20 3e 3d 20 30 20 26 26  r->yyidx >= 0 &&
31605 0a 20 20 20 20 20 20 20 20 20 20 79 79 6d 78 20  .          yymx 
31606 21 3d 20 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c  != YYERRORSYMBOL
31607 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 28 79   &&.          (y
31608 79 61 63 74 20 3d 20 79 79 5f 66 69 6e 64 5f 72  yact = yy_find_r
31609 65 64 75 63 65 5f 61 63 74 69 6f 6e 28 0a 20 20  educe_action(.  
3160a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3160b 20 20 20 20 20 20 79 79 70 50 61 72 73 65 72 2d        yypParser-
3160c 3e 79 79 73 74 61 63 6b 5b 79 79 70 50 61 72 73  >yystack[yypPars
3160d 65 72 2d 3e 79 79 69 64 78 5d 2e 73 74 61 74 65  er->yyidx].state
3160e 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  no,.            
3160f 20 20 20 20 20 20 20 20 20 20 20 20 59 59 45 52              YYER
31610 52 4f 52 53 59 4d 42 4f 4c 29 29 20 3e 3d 20 59  RORSYMBOL)) >= Y
31611 59 4e 53 54 41 54 45 0a 20 20 20 20 20 20 20 20  YNSTATE.        
31612 29 7b 0a 20 20 20 20 20 20 20 20 20 20 79 79 5f  ){.          yy_
31613 70 6f 70 5f 70 61 72 73 65 72 5f 73 74 61 63 6b  pop_parser_stack
31614 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20 20 20  (yypParser);.   
31615 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
31616 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79  f( yypParser->yy
31617 69 64 78 20 3c 20 30 20 7c 7c 20 79 79 6d 61 6a  idx < 0 || yymaj
31618 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  or==0 ){.       
31619 20 20 20 79 79 5f 64 65 73 74 72 75 63 74 6f 72     yy_destructor
3161a 28 79 79 70 50 61 72 73 65 72 2c 28 59 59 43 4f  (yypParser,(YYCO
3161b 44 45 54 59 50 45 29 79 79 6d 61 6a 6f 72 2c 26  DETYPE)yymajor,&
3161c 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20  yyminorunion);. 
3161d 20 20 20 20 20 20 20 20 20 79 79 5f 70 61 72 73           yy_pars
3161e 65 5f 66 61 69 6c 65 64 28 79 79 70 50 61 72 73  e_failed(yypPars
3161f 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 79  er);.          y
31620 79 6d 61 6a 6f 72 20 3d 20 59 59 4e 4f 43 4f 44  ymajor = YYNOCOD
31621 45 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  E;.        }else
31622 20 69 66 28 20 79 79 6d 78 21 3d 59 59 45 52 52   if( yymx!=YYERR
31623 4f 52 53 59 4d 42 4f 4c 20 29 7b 0a 20 20 20 20  ORSYMBOL ){.    
31624 20 20 20 20 20 20 59 59 4d 49 4e 4f 52 54 59 50        YYMINORTYP
31625 45 20 75 32 3b 0a 20 20 20 20 20 20 20 20 20 20  E u2;.          
31626 75 32 2e 59 59 45 52 52 53 59 4d 44 54 20 3d 20  u2.YYERRSYMDT = 
31627 30 3b 0a 20 20 20 20 20 20 20 20 20 20 79 79 5f  0;.          yy_
31628 73 68 69 66 74 28 79 79 70 50 61 72 73 65 72 2c  shift(yypParser,
31629 79 79 61 63 74 2c 59 59 45 52 52 4f 52 53 59 4d  yyact,YYERRORSYM
3162a 42 4f 4c 2c 26 75 32 29 3b 0a 20 20 20 20 20 20  BOL,&u2);.      
3162b 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
3162c 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 65    yypParser->yye
3162d 72 72 63 6e 74 20 3d 20 33 3b 0a 20 20 20 20 20  rrcnt = 3;.     
3162e 20 79 79 65 72 72 6f 72 68 69 74 20 3d 20 31 3b   yyerrorhit = 1;
3162f 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 59  .#elif defined(Y
31630 59 4e 4f 45 52 52 4f 52 52 45 43 4f 56 45 52 59  YNOERRORRECOVERY
31631 29 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68  ).      /* If th
31632 65 20 59 59 4e 4f 45 52 52 4f 52 52 45 43 4f 56  e YYNOERRORRECOV
31633 45 52 59 20 6d 61 63 72 6f 20 69 73 20 64 65 66  ERY macro is def
31634 69 6e 65 64 2c 20 74 68 65 6e 20 64 6f 20 6e 6f  ined, then do no
31635 74 20 61 74 74 65 6d 70 74 20 74 6f 0a 20 20 20  t attempt to.   
31636 20 20 20 2a 2a 20 64 6f 20 61 6e 79 20 6b 69 6e     ** do any kin
31637 64 20 6f 66 20 65 72 72 6f 72 20 72 65 63 6f 76  d of error recov
31638 65 72 79 2e 20 20 49 6e 73 74 65 61 64 2c 20 73  ery.  Instead, s
31639 69 6d 70 6c 79 20 69 6e 76 6f 6b 65 20 74 68 65  imply invoke the
3163a 20 73 79 6e 74 61 78 0a 20 20 20 20 20 20 2a 2a   syntax.      **
3163b 20 65 72 72 6f 72 20 72 6f 75 74 69 6e 65 20 61   error routine a
3163c 6e 64 20 63 6f 6e 74 69 6e 75 65 20 67 6f 69 6e  nd continue goin
3163d 67 20 61 73 20 69 66 20 6e 6f 74 68 69 6e 67 20  g as if nothing 
3163e 68 61 64 20 68 61 70 70 65 6e 65 64 2e 0a 20 20  had happened..  
3163f 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
31640 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 63 61 6e  Applications can
31641 20 73 65 74 20 74 68 69 73 20 6d 61 63 72 6f 20   set this macro 
31642 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 69 6e 73  (for example ins
31643 69 64 65 20 25 69 6e 63 6c 75 64 65 29 20 69 66  ide %include) if
31644 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 79 20 69  .      ** they i
31645 6e 74 65 6e 64 20 74 6f 20 61 62 61 6e 64 6f 6e  ntend to abandon
31646 20 74 68 65 20 70 61 72 73 65 20 75 70 6f 6e 20   the parse upon 
31647 74 68 65 20 66 69 72 73 74 20 73 79 6e 74 61 78  the first syntax
31648 20 65 72 72 6f 72 20 73 65 65 6e 2e 0a 20 20 20   error seen..   
31649 20 20 20 2a 2f 0a 20 20 20 20 20 20 79 79 5f 73     */.      yy_s
3164a 79 6e 74 61 78 5f 65 72 72 6f 72 28 79 79 70 50  yntax_error(yypP
3164b 61 72 73 65 72 2c 79 79 6d 61 6a 6f 72 2c 79 79  arser,yymajor,yy
3164c 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20  minorunion);.   
3164d 20 20 20 79 79 5f 64 65 73 74 72 75 63 74 6f 72     yy_destructor
3164e 28 79 79 70 50 61 72 73 65 72 2c 28 59 59 43 4f  (yypParser,(YYCO
3164f 44 45 54 59 50 45 29 79 79 6d 61 6a 6f 72 2c 26  DETYPE)yymajor,&
31650 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20  yyminorunion);. 
31651 20 20 20 20 20 79 79 6d 61 6a 6f 72 20 3d 20 59       yymajor = Y
31652 59 4e 4f 43 4f 44 45 3b 0a 20 20 20 20 20 20 0a  YNOCODE;.      .
31653 23 65 6c 73 65 20 20 2f 2a 20 59 59 45 52 52 4f  #else  /* YYERRO
31654 52 53 59 4d 42 4f 4c 20 69 73 20 6e 6f 74 20 64  RSYMBOL is not d
31655 65 66 69 6e 65 64 20 2a 2f 0a 20 20 20 20 20 20  efined */.      
31656 2f 2a 20 54 68 69 73 20 69 73 20 77 68 61 74 20  /* This is what 
31657 77 65 20 64 6f 20 69 66 20 74 68 65 20 67 72 61  we do if the gra
31658 6d 6d 61 72 20 64 6f 65 73 20 6e 6f 74 20 64 65  mmar does not de
31659 66 69 6e 65 20 45 52 52 4f 52 3a 0a 20 20 20 20  fine ERROR:.    
3165a 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 2a    **.      **  *
3165b 20 52 65 70 6f 72 74 20 61 6e 20 65 72 72 6f 72   Report an error
3165c 20 6d 65 73 73 61 67 65 2c 20 61 6e 64 20 74 68   message, and th
3165d 72 6f 77 20 61 77 61 79 20 74 68 65 20 69 6e 70  row away the inp
3165e 75 74 20 74 6f 6b 65 6e 2e 0a 20 20 20 20 20 20  ut token..      
3165f 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 2a 20 49  **.      **  * I
31660 66 20 74 68 65 20 69 6e 70 75 74 20 74 6f 6b 65  f the input toke
31661 6e 20 69 73 20 24 2c 20 74 68 65 6e 20 66 61 69  n is $, then fai
31662 6c 20 74 68 65 20 70 61 72 73 65 2e 0a 20 20 20  l the parse..   
31663 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 41     **.      ** A
31664 73 20 62 65 66 6f 72 65 2c 20 73 75 62 73 65 71  s before, subseq
31665 75 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 61  uent error messa
31666 67 65 73 20 61 72 65 20 73 75 70 70 72 65 73 73  ges are suppress
31667 65 64 20 75 6e 74 69 6c 0a 20 20 20 20 20 20 2a  ed until.      *
31668 2a 20 74 68 72 65 65 20 69 6e 70 75 74 20 74 6f  * three input to
31669 6b 65 6e 73 20 68 61 76 65 20 62 65 65 6e 20 73  kens have been s
3166a 75 63 63 65 73 73 66 75 6c 6c 79 20 73 68 69 66  uccessfully shif
3166b 74 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ted..      */.  
3166c 20 20 20 20 69 66 28 20 79 79 70 50 61 72 73 65      if( yypParse
3166d 72 2d 3e 79 79 65 72 72 63 6e 74 3c 3d 30 20 29  r->yyerrcnt<=0 )
3166e 7b 0a 20 20 20 20 20 20 20 20 79 79 5f 73 79 6e  {.        yy_syn
3166f 74 61 78 5f 65 72 72 6f 72 28 79 79 70 50 61 72  tax_error(yypPar
31670 73 65 72 2c 79 79 6d 61 6a 6f 72 2c 79 79 6d 69  ser,yymajor,yymi
31671 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20  norunion);.     
31672 20 7d 0a 20 20 20 20 20 20 79 79 70 50 61 72 73   }.      yypPars
31673 65 72 2d 3e 79 79 65 72 72 63 6e 74 20 3d 20 33  er->yyerrcnt = 3
31674 3b 0a 20 20 20 20 20 20 79 79 5f 64 65 73 74 72  ;.      yy_destr
31675 75 63 74 6f 72 28 79 79 70 50 61 72 73 65 72 2c  uctor(yypParser,
31676 28 59 59 43 4f 44 45 54 59 50 45 29 79 79 6d 61  (YYCODETYPE)yyma
31677 6a 6f 72 2c 26 79 79 6d 69 6e 6f 72 75 6e 69 6f  jor,&yyminorunio
31678 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 79 79  n);.      if( yy
31679 65 6e 64 6f 66 69 6e 70 75 74 20 29 7b 0a 20 20  endofinput ){.  
3167a 20 20 20 20 20 20 79 79 5f 70 61 72 73 65 5f 66        yy_parse_f
3167b 61 69 6c 65 64 28 79 79 70 50 61 72 73 65 72 29  ailed(yypParser)
3167c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
3167d 79 79 6d 61 6a 6f 72 20 3d 20 59 59 4e 4f 43 4f  yymajor = YYNOCO
3167e 44 45 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d  DE;.#endif.    }
3167f 0a 20 20 7d 77 68 69 6c 65 28 20 79 79 6d 61 6a  .  }while( yymaj
31680 6f 72 21 3d 59 59 4e 4f 43 4f 44 45 20 26 26 20  or!=YYNOCODE && 
31681 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  yypParser->yyidx
31682 3e 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 3b  >=0 );.  return;
31683 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
31684 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 72 73 65  *** End of parse
31685 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
31686 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31687 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31688 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
31689 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 74  *** Begin file t
3168a 6f 6b 65 6e 69 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a  okenize.c ******
3168b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3168c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3168d 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53  **/./*.** 2001 S
3168e 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a  eptember 15.**.*
3168f 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
31690 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
31691 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
31692 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
31693 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
31694 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
31695 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
31696 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
31697 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
31698 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
31699 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
3169a 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
3169b 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
3169c 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
3169d 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
3169e 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
3169f 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
316a0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
316a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
316a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
316a3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
316a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 41 6e  **********.** An
316a5 20 74 6f 6b 65 6e 69 7a 65 72 20 66 6f 72 20 53   tokenizer for S
316a6 51 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  QL.**.** This fi
316a7 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f  le contains C co
316a8 64 65 20 74 68 61 74 20 73 70 6c 69 74 73 20 61  de that splits a
316a9 6e 20 53 51 4c 20 69 6e 70 75 74 20 73 74 72 69  n SQL input stri
316aa 6e 67 20 75 70 20 69 6e 74 6f 0a 2a 2a 20 69 6e  ng up into.** in
316ab 64 69 76 69 64 75 61 6c 20 74 6f 6b 65 6e 73 20  dividual tokens 
316ac 61 6e 64 20 73 65 6e 64 73 20 74 68 6f 73 65 20  and sends those 
316ad 74 6f 6b 65 6e 73 20 6f 6e 65 2d 62 79 2d 6f 6e  tokens one-by-on
316ae 65 20 6f 76 65 72 20 74 6f 20 74 68 65 0a 2a 2a  e over to the.**
316af 20 70 61 72 73 65 72 20 66 6f 72 20 61 6e 61 6c   parser for anal
316b0 79 73 69 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  ysis..**.** $Id:
316b1 20 74 6f 6b 65 6e 69 7a 65 2e 63 2c 76 20 31 2e   tokenize.c,v 1.
316b2 31 36 33 20 32 30 30 39 2f 30 37 2f 30 33 20 32  163 2009/07/03 2
316b3 32 3a 35 34 3a 33 37 20 64 72 68 20 45 78 70 20  2:54:37 drh Exp 
316b4 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  $.*/../*.** The 
316b5 63 68 61 72 4d 61 70 28 29 20 6d 61 63 72 6f 20  charMap() macro 
316b6 6d 61 70 73 20 61 6c 70 68 61 62 65 74 69 63 20  maps alphabetic 
316b7 63 68 61 72 61 63 74 65 72 73 20 69 6e 74 6f 20  characters into 
316b8 74 68 65 69 72 0a 2a 2a 20 6c 6f 77 65 72 2d 63  their.** lower-c
316b9 61 73 65 20 41 53 43 49 49 20 65 71 75 69 76 61  ase ASCII equiva
316ba 6c 65 6e 74 2e 20 20 4f 6e 20 41 53 43 49 49 20  lent.  On ASCII 
316bb 6d 61 63 68 69 6e 65 73 2c 20 74 68 69 73 20 69  machines, this i
316bc 73 20 6a 75 73 74 0a 2a 2a 20 61 6e 20 75 70 70  s just.** an upp
316bd 65 72 2d 74 6f 2d 6c 6f 77 65 72 20 63 61 73 65  er-to-lower case
316be 20 6d 61 70 2e 20 20 4f 6e 20 45 42 43 44 49 43   map.  On EBCDIC
316bf 20 6d 61 63 68 69 6e 65 73 20 77 65 20 61 6c 73   machines we als
316c0 6f 20 6e 65 65 64 0a 2a 2a 20 74 6f 20 61 64 6a  o need.** to adj
316c1 75 73 74 20 74 68 65 20 65 6e 63 6f 64 69 6e 67  ust the encoding
316c2 2e 20 20 4f 6e 6c 79 20 61 6c 70 68 61 62 65 74  .  Only alphabet
316c3 69 63 20 63 68 61 72 61 63 74 65 72 73 20 61 6e  ic characters an
316c4 64 20 75 6e 64 65 72 73 63 6f 72 65 73 0a 2a 2a  d underscores.**
316c5 20 6e 65 65 64 20 74 6f 20 62 65 20 74 72 61 6e   need to be tran
316c6 73 6c 61 74 65 64 2e 0a 2a 2f 0a 23 69 66 64 65  slated..*/.#ifde
316c7 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 23  f SQLITE_ASCII.#
316c8 20 64 65 66 69 6e 65 20 63 68 61 72 4d 61 70 28   define charMap(
316c9 58 29 20 73 71 6c 69 74 65 33 55 70 70 65 72 54  X) sqlite3UpperT
316ca 6f 4c 6f 77 65 72 5b 28 75 6e 73 69 67 6e 65 64  oLower[(unsigned
316cb 20 63 68 61 72 29 58 5d 0a 23 65 6e 64 69 66 0a   char)X].#endif.
316cc 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42  #ifdef SQLITE_EB
316cd 43 44 49 43 0a 23 20 64 65 66 69 6e 65 20 63 68  CDIC.# define ch
316ce 61 72 4d 61 70 28 58 29 20 65 62 63 64 69 63 54  arMap(X) ebcdicT
316cf 6f 41 73 63 69 69 5b 28 75 6e 73 69 67 6e 65 64  oAscii[(unsigned
316d0 20 63 68 61 72 29 58 5d 0a 63 6f 6e 73 74 20 75   char)X].const u
316d1 6e 73 69 67 6e 65 64 20 63 68 61 72 20 65 62 63  nsigned char ebc
316d2 64 69 63 54 6f 41 73 63 69 69 5b 5d 20 3d 20 7b  dicToAscii[] = {
316d3 0a 2f 2a 20 30 20 20 20 31 20 20 20 32 20 20 20  ./* 0   1   2   
316d4 33 20 20 20 34 20 20 20 35 20 20 20 36 20 20 20  3   4   5   6   
316d5 37 20 20 20 38 20 20 20 39 20 20 20 41 20 20 20  7   8   9   A   
316d6 42 20 20 20 43 20 20 20 44 20 20 20 45 20 20 20  B   C   D   E   
316d7 46 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20  F */.   0,  0,  
316d8 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
316d9 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
316da 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
316db 30 2c 20 20 30 2c 20 20 2f 2a 20 30 78 20 2a 2f  0,  0,  /* 0x */
316dc 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  .   0,  0,  0,  
316dd 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
316de 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
316df 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
316e0 30 2c 20 20 2f 2a 20 31 78 20 2a 2f 0a 20 20 20  0,  /* 1x */.   
316e1 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
316e2 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
316e3 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
316e4 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
316e5 2f 2a 20 32 78 20 2a 2f 0a 20 20 20 30 2c 20 20  /* 2x */.   0,  
316e6 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
316e7 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
316e8 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
316e9 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 33  0,  0,  0,  /* 3
316ea 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20  x */.   0,  0,  
316eb 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
316ec 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
316ed 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
316ee 30 2c 20 20 30 2c 20 20 2f 2a 20 34 78 20 2a 2f  0,  0,  /* 4x */
316ef 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  .   0,  0,  0,  
316f0 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
316f1 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
316f2 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
316f3 30 2c 20 20 2f 2a 20 35 78 20 2a 2f 0a 20 20 20  0,  /* 5x */.   
316f4 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
316f5 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
316f6 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
316f7 30 2c 20 39 35 2c 20 20 30 2c 20 20 30 2c 20 20  0, 95,  0,  0,  
316f8 2f 2a 20 36 78 20 2a 2f 0a 20 20 20 30 2c 20 20  /* 6x */.   0,  
316f9 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
316fa 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
316fb 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
316fc 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 37  0,  0,  0,  /* 7
316fd 78 20 2a 2f 0a 20 20 20 30 2c 20 39 37 2c 20 39  x */.   0, 97, 9
316fe 38 2c 20 39 39 2c 31 30 30 2c 31 30 31 2c 31 30  8, 99,100,101,10
316ff 32 2c 31 30 33 2c 31 30 34 2c 31 30 35 2c 20 20  2,103,104,105,  
31700 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31701 30 2c 20 20 30 2c 20 20 2f 2a 20 38 78 20 2a 2f  0,  0,  /* 8x */
31702 0a 20 20 20 30 2c 31 30 36 2c 31 30 37 2c 31 30  .   0,106,107,10
31703 38 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 31 31  8,109,110,111,11
31704 32 2c 31 31 33 2c 31 31 34 2c 20 20 30 2c 20 20  2,113,114,  0,  
31705 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31706 30 2c 20 20 2f 2a 20 39 78 20 2a 2f 0a 20 20 20  0,  /* 9x */.   
31707 30 2c 20 20 30 2c 31 31 35 2c 31 31 36 2c 31 31  0,  0,115,116,11
31708 37 2c 31 31 38 2c 31 31 39 2c 31 32 30 2c 31 32  7,118,119,120,12
31709 31 2c 31 32 32 2c 20 20 30 2c 20 20 30 2c 20 20  1,122,  0,  0,  
3170a 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3170b 2f 2a 20 41 78 20 2a 2f 0a 20 20 20 30 2c 20 20  /* Ax */.   0,  
3170c 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3170d 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3170e 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3170f 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 42  0,  0,  0,  /* B
31710 78 20 2a 2f 0a 20 20 20 30 2c 20 39 37 2c 20 39  x */.   0, 97, 9
31711 38 2c 20 39 39 2c 31 30 30 2c 31 30 31 2c 31 30  8, 99,100,101,10
31712 32 2c 31 30 33 2c 31 30 34 2c 31 30 35 2c 20 20  2,103,104,105,  
31713 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31714 30 2c 20 20 30 2c 20 20 2f 2a 20 43 78 20 2a 2f  0,  0,  /* Cx */
31715 0a 20 20 20 30 2c 31 30 36 2c 31 30 37 2c 31 30  .   0,106,107,10
31716 38 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 31 31  8,109,110,111,11
31717 32 2c 31 31 33 2c 31 31 34 2c 20 20 30 2c 20 20  2,113,114,  0,  
31718 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31719 30 2c 20 20 2f 2a 20 44 78 20 2a 2f 0a 20 20 20  0,  /* Dx */.   
3171a 30 2c 20 20 30 2c 31 31 35 2c 31 31 36 2c 31 31  0,  0,115,116,11
3171b 37 2c 31 31 38 2c 31 31 39 2c 31 32 30 2c 31 32  7,118,119,120,12
3171c 31 2c 31 32 32 2c 20 20 30 2c 20 20 30 2c 20 20  1,122,  0,  0,  
3171d 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
3171e 2f 2a 20 45 78 20 2a 2f 0a 20 20 20 30 2c 20 20  /* Ex */.   0,  
3171f 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31720 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31721 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
31722 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 46  0,  0,  0,  /* F
31723 78 20 2a 2f 0a 7d 3b 0a 23 65 6e 64 69 66 0a 0a  x */.};.#endif..
31724 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  /*.** The sqlite
31725 33 4b 65 79 77 6f 72 64 43 6f 64 65 20 66 75 6e  3KeywordCode fun
31726 63 74 69 6f 6e 20 6c 6f 6f 6b 73 20 75 70 20 61  ction looks up a
31727 6e 20 69 64 65 6e 74 69 66 69 65 72 20 74 6f 20  n identifier to 
31728 64 65 74 65 72 6d 69 6e 65 20 69 66 0a 2a 2a 20  determine if.** 
31729 69 74 20 69 73 20 61 20 6b 65 79 77 6f 72 64 2e  it is a keyword.
3172a 20 20 49 66 20 69 74 20 69 73 20 61 20 6b 65 79    If it is a key
3172b 77 6f 72 64 2c 20 74 68 65 20 74 6f 6b 65 6e 20  word, the token 
3172c 63 6f 64 65 20 6f 66 20 74 68 61 74 20 6b 65 79  code of that key
3172d 77 6f 72 64 20 69 73 20 0a 2a 2a 20 72 65 74 75  word is .** retu
3172e 72 6e 65 64 2e 20 20 49 66 20 74 68 65 20 69 6e  rned.  If the in
3172f 70 75 74 20 69 73 20 6e 6f 74 20 61 20 6b 65 79  put is not a key
31730 77 6f 72 64 2c 20 54 4b 5f 49 44 20 69 73 20 72  word, TK_ID is r
31731 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  eturned..**.** T
31732 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
31733 6e 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e  n of this routin
31734 65 20 77 61 73 20 67 65 6e 65 72 61 74 65 64 20  e was generated 
31735 62 79 20 61 20 70 72 6f 67 72 61 6d 2c 0a 2a 2a  by a program,.**
31736 20 6d 6b 6b 65 79 77 6f 72 64 68 61 73 68 2e 68   mkkeywordhash.h
31737 2c 20 6c 6f 63 61 74 65 64 20 69 6e 20 74 68 65  , located in the
31738 20 74 6f 6f 6c 20 73 75 62 64 69 72 65 63 74 6f   tool subdirecto
31739 72 79 20 6f 66 20 74 68 65 20 64 69 73 74 72 69  ry of the distri
3173a 62 75 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 6f  bution..** The o
3173b 75 74 70 75 74 20 6f 66 20 74 68 65 20 6d 6b 6b  utput of the mkk
3173c 65 79 77 6f 72 64 68 61 73 68 2e 63 20 70 72 6f  eywordhash.c pro
3173d 67 72 61 6d 20 69 73 20 77 72 69 74 74 65 6e 20  gram is written 
3173e 69 6e 74 6f 20 61 20 66 69 6c 65 0a 2a 2a 20 6e  into a file.** n
3173f 61 6d 65 64 20 6b 65 79 77 6f 72 64 68 61 73 68  amed keywordhash
31740 2e 68 20 61 6e 64 20 74 68 65 6e 20 69 6e 63 6c  .h and then incl
31741 75 64 65 64 20 69 6e 74 6f 20 74 68 69 73 20 73  uded into this s
31742 6f 75 72 63 65 20 66 69 6c 65 20 62 79 0a 2a 2a  ource file by.**
31743 20 74 68 65 20 23 69 6e 63 6c 75 64 65 20 62 65   the #include be
31744 6c 6f 77 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  low..*/./*******
31745 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20  ******* Include 
31746 6b 65 79 77 6f 72 64 68 61 73 68 2e 68 20 69 6e  keywordhash.h in
31747 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 74   the middle of t
31748 6f 6b 65 6e 69 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a  okenize.c ******
31749 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
3174a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
3174b 6c 65 20 6b 65 79 77 6f 72 64 68 61 73 68 2e 68  le keywordhash.h
3174c 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
3174d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3174e 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 20 54  ******/./***** T
3174f 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
31750 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  s automatically 
31751 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65 20 2a  generated code *
31752 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20  *****.**.** The 
31753 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c  code in this fil
31754 65 20 68 61 73 20 62 65 65 6e 20 61 75 74 6f 6d  e has been autom
31755 61 74 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74  atically generat
31756 65 64 20 62 79 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ed by.**.**     
31757 24 48 65 61 64 65 72 3a 20 2f 68 6f 6d 65 2f 64  $Header: /home/d
31758 72 68 2f 73 71 6c 69 74 65 2f 74 72 61 6e 73 2f  rh/sqlite/trans/
31759 63 76 73 2f 73 71 6c 69 74 65 2f 73 71 6c 69 74  cvs/sqlite/sqlit
3175a 65 2f 74 6f 6f 6c 2f 6d 6b 6b 65 79 77 6f 72 64  e/tool/mkkeyword
3175b 68 61 73 68 2e 63 2c 76 20 31 2e 33 38 20 32 30  hash.c,v 1.38 20
3175c 30 39 2f 30 36 2f 30 39 20 31 34 3a 32 37 3a 34  09/06/09 14:27:4
3175d 31 20 64 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a  1 drh Exp $.**.*
3175e 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68  * The code in th
3175f 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e  is file implemen
31760 74 73 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 68  ts a function th
31761 61 74 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68  at determines wh
31762 65 74 68 65 72 0a 2a 2a 20 6f 72 20 6e 6f 74 20  ether.** or not 
31763 61 20 67 69 76 65 6e 20 69 64 65 6e 74 69 66 69  a given identifi
31764 65 72 20 69 73 20 72 65 61 6c 6c 79 20 61 6e 20  er is really an 
31765 53 51 4c 20 6b 65 79 77 6f 72 64 2e 20 20 54 68  SQL keyword.  Th
31766 65 20 73 61 6d 65 20 74 68 69 6e 67 0a 2a 2a 20  e same thing.** 
31767 6d 69 67 68 74 20 62 65 20 69 6d 70 6c 65 6d 65  might be impleme
31768 6e 74 65 64 20 6d 6f 72 65 20 64 69 72 65 63 74  nted more direct
31769 6c 79 20 75 73 69 6e 67 20 61 20 68 61 6e 64 2d  ly using a hand-
3176a 77 72 69 74 74 65 6e 20 68 61 73 68 20 74 61 62  written hash tab
3176b 6c 65 2e 0a 2a 2a 20 42 75 74 20 62 79 20 75 73  le..** But by us
3176c 69 6e 67 20 74 68 69 73 20 61 75 74 6f 6d 61 74  ing this automat
3176d 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64  ically generated
3176e 20 63 6f 64 65 2c 20 74 68 65 20 73 69 7a 65 20   code, the size 
3176f 6f 66 20 74 68 65 20 63 6f 64 65 0a 2a 2a 20 69  of the code.** i
31770 73 20 73 75 62 73 74 61 6e 74 69 61 6c 6c 79 20  s substantially 
31771 72 65 64 75 63 65 64 2e 20 20 54 68 69 73 20 69  reduced.  This i
31772 73 20 69 6d 70 6f 72 74 61 6e 74 20 66 6f 72 20  s important for 
31773 65 6d 62 65 64 64 65 64 20 61 70 70 6c 69 63 61  embedded applica
31774 74 69 6f 6e 73 0a 2a 2a 20 6f 6e 20 70 6c 61 74  tions.** on plat
31775 66 6f 72 6d 73 20 77 69 74 68 20 6c 69 6d 69 74  forms with limit
31776 65 64 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 2f 2a  ed memory..*/./*
31777 20 48 61 73 68 20 73 63 6f 72 65 3a 20 31 37 31   Hash score: 171
31778 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6b   */.static int k
31779 65 79 77 6f 72 64 43 6f 64 65 28 63 6f 6e 73 74  eywordCode(const
3177a 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29   char *z, int n)
3177b 7b 0a 20 20 2f 2a 20 7a 54 65 78 74 5b 5d 20 65  {.  /* zText[] e
3177c 6e 63 6f 64 65 73 20 38 30 31 20 62 79 74 65 73  ncodes 801 bytes
3177d 20 6f 66 20 6b 65 79 77 6f 72 64 73 20 69 6e 20   of keywords in 
3177e 35 34 31 20 62 79 74 65 73 20 2a 2f 0a 20 20 2f  541 bytes */.  /
3177f 2a 20 20 20 52 45 49 4e 44 45 58 45 44 45 53 43  *   REINDEXEDESC
31780 41 50 45 41 43 48 45 43 4b 45 59 42 45 46 4f 52  APEACHECKEYBEFOR
31781 45 49 47 4e 4f 52 45 47 45 58 50 4c 41 49 4e 53  EIGNOREGEXPLAINS
31782 54 45 41 44 44 41 54 41 42 41 53 45 4c 45 43 54  TEADDATABASELECT
31783 20 20 20 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20         */.  /*  
31784 20 41 42 4c 45 46 54 48 45 4e 44 45 46 45 52 52   ABLEFTHENDEFERR
31785 41 42 4c 45 4c 53 45 58 43 45 50 54 52 41 4e 53  ABLELSEXCEPTRANS
31786 41 43 54 49 4f 4e 41 54 55 52 41 4c 54 45 52 41  ACTIONATURALTERA
31787 49 53 45 58 43 4c 55 53 49 56 45 20 20 20 20 20  ISEXCLUSIVE     
31788 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20 20 58 49      */.  /*   XI
31789 53 54 53 41 56 45 50 4f 49 4e 54 45 52 53 45 43  STSAVEPOINTERSEC
3178a 54 52 49 47 47 45 52 45 46 45 52 45 4e 43 45 53  TRIGGEREFERENCES
3178b 43 4f 4e 53 54 52 41 49 4e 54 4f 46 46 53 45 54  CONSTRAINTOFFSET
3178c 45 4d 50 4f 52 41 52 59 20 20 20 20 20 20 20 20  EMPORARY        
3178d 20 2a 2f 0a 20 20 2f 2a 20 20 20 55 4e 49 51 55   */.  /*   UNIQU
3178e 45 52 59 41 54 54 41 43 48 41 56 49 4e 47 52 4f  ERYATTACHAVINGRO
3178f 55 50 44 41 54 45 42 45 47 49 4e 4e 45 52 45 4c  UPDATEBEGINNEREL
31790 45 41 53 45 42 45 54 57 45 45 4e 4f 54 4e 55 4c  EASEBETWEENOTNUL
31791 4c 49 4b 45 20 20 20 20 20 20 20 20 20 20 2a 2f  LIKE          */
31792 0a 20 20 2f 2a 20 20 20 43 41 53 43 41 44 45 4c  .  /*   CASCADEL
31793 45 54 45 43 41 53 45 43 4f 4c 4c 41 54 45 43 52  ETECASECOLLATECR
31794 45 41 54 45 43 55 52 52 45 4e 54 5f 44 41 54 45  EATECURRENT_DATE
31795 44 45 54 41 43 48 49 4d 4d 45 44 49 41 54 45 4a  DETACHIMMEDIATEJ
31796 4f 49 4e 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  OIN        */.  
31797 2f 2a 20 20 20 53 45 52 54 4d 41 54 43 48 50 4c  /*   SERTMATCHPL
31798 41 4e 41 4c 59 5a 45 50 52 41 47 4d 41 42 4f 52  ANALYZEPRAGMABOR
31799 54 56 41 4c 55 45 53 56 49 52 54 55 41 4c 49 4d  TVALUESVIRTUALIM
3179a 49 54 57 48 45 4e 57 48 45 52 45 4e 41 4d 45 20  ITWHENWHERENAME 
3179b 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 2f 2a 20          */.  /* 
3179c 20 20 41 46 54 45 52 45 50 4c 41 43 45 41 4e 44    AFTEREPLACEAND
3179d 45 46 41 55 4c 54 41 55 54 4f 49 4e 43 52 45 4d  EFAULTAUTOINCREM
3179e 45 4e 54 43 41 53 54 43 4f 4c 55 4d 4e 43 4f 4d  ENTCASTCOLUMNCOM
3179f 4d 49 54 43 4f 4e 46 4c 49 43 54 43 52 4f 53 53  MITCONFLICTCROSS
317a0 20 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20 20 43       */.  /*   C
317a1 55 52 52 45 4e 54 5f 54 49 4d 45 53 54 41 4d 50  URRENT_TIMESTAMP
317a2 52 49 4d 41 52 59 44 45 46 45 52 52 45 44 49 53  RIMARYDEFERREDIS
317a3 54 49 4e 43 54 44 52 4f 50 46 41 49 4c 46 52 4f  TINCTDROPFAILFRO
317a4 4d 46 55 4c 4c 47 4c 4f 42 59 49 46 20 20 20 20  MFULLGLOBYIF    
317a5 20 20 2a 2f 0a 20 20 2f 2a 20 20 20 49 53 4e 55    */.  /*   ISNU
317a6 4c 4c 4f 52 44 45 52 45 53 54 52 49 43 54 4f 55  LLORDERESTRICTOU
317a7 54 45 52 49 47 48 54 52 4f 4c 4c 42 41 43 4b 52  TERIGHTROLLBACKR
317a8 4f 57 55 4e 49 4f 4e 55 53 49 4e 47 56 41 43 55  OWUNIONUSINGVACU
317a9 55 4d 56 49 45 57 20 20 20 20 20 20 20 20 20 2a  UMVIEW         *
317aa 2f 0a 20 20 2f 2a 20 20 20 49 4e 49 54 49 41 4c  /.  /*   INITIAL
317ab 4c 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20  LY              
317ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
317ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
317ae 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20              */. 
317af 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68   static const ch
317b0 61 72 20 7a 54 65 78 74 5b 35 34 30 5d 20 3d 20  ar zText[540] = 
317b1 7b 0a 20 20 20 20 27 52 27 2c 27 45 27 2c 27 49  {.    'R','E','I
317b2 27 2c 27 4e 27 2c 27 44 27 2c 27 45 27 2c 27 58  ','N','D','E','X
317b3 27 2c 27 45 27 2c 27 44 27 2c 27 45 27 2c 27 53  ','E','D','E','S
317b4 27 2c 27 43 27 2c 27 41 27 2c 27 50 27 2c 27 45  ','C','A','P','E
317b5 27 2c 27 41 27 2c 27 43 27 2c 27 48 27 2c 0a 20  ','A','C','H',. 
317b6 20 20 20 27 45 27 2c 27 43 27 2c 27 4b 27 2c 27     'E','C','K','
317b7 45 27 2c 27 59 27 2c 27 42 27 2c 27 45 27 2c 27  E','Y','B','E','
317b8 46 27 2c 27 4f 27 2c 27 52 27 2c 27 45 27 2c 27  F','O','R','E','
317b9 49 27 2c 27 47 27 2c 27 4e 27 2c 27 4f 27 2c 27  I','G','N','O','
317ba 52 27 2c 27 45 27 2c 27 47 27 2c 0a 20 20 20 20  R','E','G',.    
317bb 27 45 27 2c 27 58 27 2c 27 50 27 2c 27 4c 27 2c  'E','X','P','L',
317bc 27 41 27 2c 27 49 27 2c 27 4e 27 2c 27 53 27 2c  'A','I','N','S',
317bd 27 54 27 2c 27 45 27 2c 27 41 27 2c 27 44 27 2c  'T','E','A','D',
317be 27 44 27 2c 27 41 27 2c 27 54 27 2c 27 41 27 2c  'D','A','T','A',
317bf 27 42 27 2c 27 41 27 2c 0a 20 20 20 20 27 53 27  'B','A',.    'S'
317c0 2c 27 45 27 2c 27 4c 27 2c 27 45 27 2c 27 43 27  ,'E','L','E','C'
317c1 2c 27 54 27 2c 27 41 27 2c 27 42 27 2c 27 4c 27  ,'T','A','B','L'
317c2 2c 27 45 27 2c 27 46 27 2c 27 54 27 2c 27 48 27  ,'E','F','T','H'
317c3 2c 27 45 27 2c 27 4e 27 2c 27 44 27 2c 27 45 27  ,'E','N','D','E'
317c4 2c 27 46 27 2c 0a 20 20 20 20 27 45 27 2c 27 52  ,'F',.    'E','R
317c5 27 2c 27 52 27 2c 27 41 27 2c 27 42 27 2c 27 4c  ','R','A','B','L
317c6 27 2c 27 45 27 2c 27 4c 27 2c 27 53 27 2c 27 45  ','E','L','S','E
317c7 27 2c 27 58 27 2c 27 43 27 2c 27 45 27 2c 27 50  ','X','C','E','P
317c8 27 2c 27 54 27 2c 27 52 27 2c 27 41 27 2c 27 4e  ','T','R','A','N
317c9 27 2c 0a 20 20 20 20 27 53 27 2c 27 41 27 2c 27  ',.    'S','A','
317ca 43 27 2c 27 54 27 2c 27 49 27 2c 27 4f 27 2c 27  C','T','I','O','
317cb 4e 27 2c 27 41 27 2c 27 54 27 2c 27 55 27 2c 27  N','A','T','U','
317cc 52 27 2c 27 41 27 2c 27 4c 27 2c 27 54 27 2c 27  R','A','L','T','
317cd 45 27 2c 27 52 27 2c 27 41 27 2c 27 49 27 2c 0a  E','R','A','I',.
317ce 20 20 20 20 27 53 27 2c 27 45 27 2c 27 58 27 2c      'S','E','X',
317cf 27 43 27 2c 27 4c 27 2c 27 55 27 2c 27 53 27 2c  'C','L','U','S',
317d0 27 49 27 2c 27 56 27 2c 27 45 27 2c 27 58 27 2c  'I','V','E','X',
317d1 27 49 27 2c 27 53 27 2c 27 54 27 2c 27 53 27 2c  'I','S','T','S',
317d2 27 41 27 2c 27 56 27 2c 27 45 27 2c 0a 20 20 20  'A','V','E',.   
317d3 20 27 50 27 2c 27 4f 27 2c 27 49 27 2c 27 4e 27   'P','O','I','N'
317d4 2c 27 54 27 2c 27 45 27 2c 27 52 27 2c 27 53 27  ,'T','E','R','S'
317d5 2c 27 45 27 2c 27 43 27 2c 27 54 27 2c 27 52 27  ,'E','C','T','R'
317d6 2c 27 49 27 2c 27 47 27 2c 27 47 27 2c 27 45 27  ,'I','G','G','E'
317d7 2c 27 52 27 2c 27 45 27 2c 0a 20 20 20 20 27 46  ,'R','E',.    'F
317d8 27 2c 27 45 27 2c 27 52 27 2c 27 45 27 2c 27 4e  ','E','R','E','N
317d9 27 2c 27 43 27 2c 27 45 27 2c 27 53 27 2c 27 43  ','C','E','S','C
317da 27 2c 27 4f 27 2c 27 4e 27 2c 27 53 27 2c 27 54  ','O','N','S','T
317db 27 2c 27 52 27 2c 27 41 27 2c 27 49 27 2c 27 4e  ','R','A','I','N
317dc 27 2c 27 54 27 2c 0a 20 20 20 20 27 4f 27 2c 27  ','T',.    'O','
317dd 46 27 2c 27 46 27 2c 27 53 27 2c 27 45 27 2c 27  F','F','S','E','
317de 54 27 2c 27 45 27 2c 27 4d 27 2c 27 50 27 2c 27  T','E','M','P','
317df 4f 27 2c 27 52 27 2c 27 41 27 2c 27 52 27 2c 27  O','R','A','R','
317e0 59 27 2c 27 55 27 2c 27 4e 27 2c 27 49 27 2c 27  Y','U','N','I','
317e1 51 27 2c 0a 20 20 20 20 27 55 27 2c 27 45 27 2c  Q',.    'U','E',
317e2 27 52 27 2c 27 59 27 2c 27 41 27 2c 27 54 27 2c  'R','Y','A','T',
317e3 27 54 27 2c 27 41 27 2c 27 43 27 2c 27 48 27 2c  'T','A','C','H',
317e4 27 41 27 2c 27 56 27 2c 27 49 27 2c 27 4e 27 2c  'A','V','I','N',
317e5 27 47 27 2c 27 52 27 2c 27 4f 27 2c 27 55 27 2c  'G','R','O','U',
317e6 0a 20 20 20 20 27 50 27 2c 27 44 27 2c 27 41 27  .    'P','D','A'
317e7 2c 27 54 27 2c 27 45 27 2c 27 42 27 2c 27 45 27  ,'T','E','B','E'
317e8 2c 27 47 27 2c 27 49 27 2c 27 4e 27 2c 27 4e 27  ,'G','I','N','N'
317e9 2c 27 45 27 2c 27 52 27 2c 27 45 27 2c 27 4c 27  ,'E','R','E','L'
317ea 2c 27 45 27 2c 27 41 27 2c 27 53 27 2c 0a 20 20  ,'E','A','S',.  
317eb 20 20 27 45 27 2c 27 42 27 2c 27 45 27 2c 27 54    'E','B','E','T
317ec 27 2c 27 57 27 2c 27 45 27 2c 27 45 27 2c 27 4e  ','W','E','E','N
317ed 27 2c 27 4f 27 2c 27 54 27 2c 27 4e 27 2c 27 55  ','O','T','N','U
317ee 27 2c 27 4c 27 2c 27 4c 27 2c 27 49 27 2c 27 4b  ','L','L','I','K
317ef 27 2c 27 45 27 2c 27 43 27 2c 0a 20 20 20 20 27  ','E','C',.    '
317f0 41 27 2c 27 53 27 2c 27 43 27 2c 27 41 27 2c 27  A','S','C','A','
317f1 44 27 2c 27 45 27 2c 27 4c 27 2c 27 45 27 2c 27  D','E','L','E','
317f2 54 27 2c 27 45 27 2c 27 43 27 2c 27 41 27 2c 27  T','E','C','A','
317f3 53 27 2c 27 45 27 2c 27 43 27 2c 27 4f 27 2c 27  S','E','C','O','
317f4 4c 27 2c 27 4c 27 2c 0a 20 20 20 20 27 41 27 2c  L','L',.    'A',
317f5 27 54 27 2c 27 45 27 2c 27 43 27 2c 27 52 27 2c  'T','E','C','R',
317f6 27 45 27 2c 27 41 27 2c 27 54 27 2c 27 45 27 2c  'E','A','T','E',
317f7 27 43 27 2c 27 55 27 2c 27 52 27 2c 27 52 27 2c  'C','U','R','R',
317f8 27 45 27 2c 27 4e 27 2c 27 54 27 2c 27 5f 27 2c  'E','N','T','_',
317f9 27 44 27 2c 0a 20 20 20 20 27 41 27 2c 27 54 27  'D',.    'A','T'
317fa 2c 27 45 27 2c 27 44 27 2c 27 45 27 2c 27 54 27  ,'E','D','E','T'
317fb 2c 27 41 27 2c 27 43 27 2c 27 48 27 2c 27 49 27  ,'A','C','H','I'
317fc 2c 27 4d 27 2c 27 4d 27 2c 27 45 27 2c 27 44 27  ,'M','M','E','D'
317fd 2c 27 49 27 2c 27 41 27 2c 27 54 27 2c 27 45 27  ,'I','A','T','E'
317fe 2c 0a 20 20 20 20 27 4a 27 2c 27 4f 27 2c 27 49  ,.    'J','O','I
317ff 27 2c 27 4e 27 2c 27 53 27 2c 27 45 27 2c 27 52  ','N','S','E','R
31800 27 2c 27 54 27 2c 27 4d 27 2c 27 41 27 2c 27 54  ','T','M','A','T
31801 27 2c 27 43 27 2c 27 48 27 2c 27 50 27 2c 27 4c  ','C','H','P','L
31802 27 2c 27 41 27 2c 27 4e 27 2c 27 41 27 2c 0a 20  ','A','N','A',. 
31803 20 20 20 27 4c 27 2c 27 59 27 2c 27 5a 27 2c 27     'L','Y','Z','
31804 45 27 2c 27 50 27 2c 27 52 27 2c 27 41 27 2c 27  E','P','R','A','
31805 47 27 2c 27 4d 27 2c 27 41 27 2c 27 42 27 2c 27  G','M','A','B','
31806 4f 27 2c 27 52 27 2c 27 54 27 2c 27 56 27 2c 27  O','R','T','V','
31807 41 27 2c 27 4c 27 2c 27 55 27 2c 0a 20 20 20 20  A','L','U',.    
31808 27 45 27 2c 27 53 27 2c 27 56 27 2c 27 49 27 2c  'E','S','V','I',
31809 27 52 27 2c 27 54 27 2c 27 55 27 2c 27 41 27 2c  'R','T','U','A',
3180a 27 4c 27 2c 27 49 27 2c 27 4d 27 2c 27 49 27 2c  'L','I','M','I',
3180b 27 54 27 2c 27 57 27 2c 27 48 27 2c 27 45 27 2c  'T','W','H','E',
3180c 27 4e 27 2c 27 57 27 2c 0a 20 20 20 20 27 48 27  'N','W',.    'H'
3180d 2c 27 45 27 2c 27 52 27 2c 27 45 27 2c 27 4e 27  ,'E','R','E','N'
3180e 2c 27 41 27 2c 27 4d 27 2c 27 45 27 2c 27 41 27  ,'A','M','E','A'
3180f 2c 27 46 27 2c 27 54 27 2c 27 45 27 2c 27 52 27  ,'F','T','E','R'
31810 2c 27 45 27 2c 27 50 27 2c 27 4c 27 2c 27 41 27  ,'E','P','L','A'
31811 2c 27 43 27 2c 0a 20 20 20 20 27 45 27 2c 27 41  ,'C',.    'E','A
31812 27 2c 27 4e 27 2c 27 44 27 2c 27 45 27 2c 27 46  ','N','D','E','F
31813 27 2c 27 41 27 2c 27 55 27 2c 27 4c 27 2c 27 54  ','A','U','L','T
31814 27 2c 27 41 27 2c 27 55 27 2c 27 54 27 2c 27 4f  ','A','U','T','O
31815 27 2c 27 49 27 2c 27 4e 27 2c 27 43 27 2c 27 52  ','I','N','C','R
31816 27 2c 0a 20 20 20 20 27 45 27 2c 27 4d 27 2c 27  ',.    'E','M','
31817 45 27 2c 27 4e 27 2c 27 54 27 2c 27 43 27 2c 27  E','N','T','C','
31818 41 27 2c 27 53 27 2c 27 54 27 2c 27 43 27 2c 27  A','S','T','C','
31819 4f 27 2c 27 4c 27 2c 27 55 27 2c 27 4d 27 2c 27  O','L','U','M','
3181a 4e 27 2c 27 43 27 2c 27 4f 27 2c 27 4d 27 2c 0a  N','C','O','M',.
3181b 20 20 20 20 27 4d 27 2c 27 49 27 2c 27 54 27 2c      'M','I','T',
3181c 27 43 27 2c 27 4f 27 2c 27 4e 27 2c 27 46 27 2c  'C','O','N','F',
3181d 27 4c 27 2c 27 49 27 2c 27 43 27 2c 27 54 27 2c  'L','I','C','T',
3181e 27 43 27 2c 27 52 27 2c 27 4f 27 2c 27 53 27 2c  'C','R','O','S',
3181f 27 53 27 2c 27 43 27 2c 27 55 27 2c 0a 20 20 20  'S','C','U',.   
31820 20 27 52 27 2c 27 52 27 2c 27 45 27 2c 27 4e 27   'R','R','E','N'
31821 2c 27 54 27 2c 27 5f 27 2c 27 54 27 2c 27 49 27  ,'T','_','T','I'
31822 2c 27 4d 27 2c 27 45 27 2c 27 53 27 2c 27 54 27  ,'M','E','S','T'
31823 2c 27 41 27 2c 27 4d 27 2c 27 50 27 2c 27 52 27  ,'A','M','P','R'
31824 2c 27 49 27 2c 27 4d 27 2c 0a 20 20 20 20 27 41  ,'I','M',.    'A
31825 27 2c 27 52 27 2c 27 59 27 2c 27 44 27 2c 27 45  ','R','Y','D','E
31826 27 2c 27 46 27 2c 27 45 27 2c 27 52 27 2c 27 52  ','F','E','R','R
31827 27 2c 27 45 27 2c 27 44 27 2c 27 49 27 2c 27 53  ','E','D','I','S
31828 27 2c 27 54 27 2c 27 49 27 2c 27 4e 27 2c 27 43  ','T','I','N','C
31829 27 2c 27 54 27 2c 0a 20 20 20 20 27 44 27 2c 27  ','T',.    'D','
3182a 52 27 2c 27 4f 27 2c 27 50 27 2c 27 46 27 2c 27  R','O','P','F','
3182b 41 27 2c 27 49 27 2c 27 4c 27 2c 27 46 27 2c 27  A','I','L','F','
3182c 52 27 2c 27 4f 27 2c 27 4d 27 2c 27 46 27 2c 27  R','O','M','F','
3182d 55 27 2c 27 4c 27 2c 27 4c 27 2c 27 47 27 2c 27  U','L','L','G','
3182e 4c 27 2c 0a 20 20 20 20 27 4f 27 2c 27 42 27 2c  L',.    'O','B',
3182f 27 59 27 2c 27 49 27 2c 27 46 27 2c 27 49 27 2c  'Y','I','F','I',
31830 27 53 27 2c 27 4e 27 2c 27 55 27 2c 27 4c 27 2c  'S','N','U','L',
31831 27 4c 27 2c 27 4f 27 2c 27 52 27 2c 27 44 27 2c  'L','O','R','D',
31832 27 45 27 2c 27 52 27 2c 27 45 27 2c 27 53 27 2c  'E','R','E','S',
31833 0a 20 20 20 20 27 54 27 2c 27 52 27 2c 27 49 27  .    'T','R','I'
31834 2c 27 43 27 2c 27 54 27 2c 27 4f 27 2c 27 55 27  ,'C','T','O','U'
31835 2c 27 54 27 2c 27 45 27 2c 27 52 27 2c 27 49 27  ,'T','E','R','I'
31836 2c 27 47 27 2c 27 48 27 2c 27 54 27 2c 27 52 27  ,'G','H','T','R'
31837 2c 27 4f 27 2c 27 4c 27 2c 27 4c 27 2c 0a 20 20  ,'O','L','L',.  
31838 20 20 27 42 27 2c 27 41 27 2c 27 43 27 2c 27 4b    'B','A','C','K
31839 27 2c 27 52 27 2c 27 4f 27 2c 27 57 27 2c 27 55  ','R','O','W','U
3183a 27 2c 27 4e 27 2c 27 49 27 2c 27 4f 27 2c 27 4e  ','N','I','O','N
3183b 27 2c 27 55 27 2c 27 53 27 2c 27 49 27 2c 27 4e  ','U','S','I','N
3183c 27 2c 27 47 27 2c 27 56 27 2c 0a 20 20 20 20 27  ','G','V',.    '
3183d 41 27 2c 27 43 27 2c 27 55 27 2c 27 55 27 2c 27  A','C','U','U','
3183e 4d 27 2c 27 56 27 2c 27 49 27 2c 27 45 27 2c 27  M','V','I','E','
3183f 57 27 2c 27 49 27 2c 27 4e 27 2c 27 49 27 2c 27  W','I','N','I','
31840 54 27 2c 27 49 27 2c 27 41 27 2c 27 4c 27 2c 27  T','I','A','L','
31841 4c 27 2c 27 59 27 2c 0a 20 20 7d 3b 0a 20 20 73  L','Y',.  };.  s
31842 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69  tatic const unsi
31843 67 6e 65 64 20 63 68 61 72 20 61 48 61 73 68 5b  gned char aHash[
31844 31 32 37 5d 20 3d 20 7b 0a 20 20 20 20 20 20 37  127] = {.      7
31845 30 2c 20 20 39 39 2c 20 31 31 32 2c 20 20 36 38  0,  99, 112,  68
31846 2c 20 20 20 30 2c 20 20 34 33 2c 20 20 20 30 2c  ,   0,  43,   0,
31847 20 20 20 30 2c 20 20 37 36 2c 20 20 20 30 2c 20     0,  76,   0, 
31848 20 37 31 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20   71,   0,   0,. 
31849 20 20 20 20 20 34 31 2c 20 20 31 32 2c 20 20 37       41,  12,  7
3184a 32 2c 20 20 31 35 2c 20 20 20 30 2c 20 31 31 31  2,  15,   0, 111
3184b 2c 20 20 37 39 2c 20 20 34 39 2c 20 31 30 36 2c  ,  79,  49, 106,
3184c 20 20 20 30 2c 20 20 31 39 2c 20 20 20 30 2c 20     0,  19,   0, 
3184d 20 20 30 2c 0a 20 20 20 20 20 31 31 36 2c 20 20    0,.     116,  
3184e 20 30 2c 20 31 31 34 2c 20 31 30 39 2c 20 20 20   0, 114, 109,   
3184f 30 2c 20 20 32 32 2c 20 20 38 37 2c 20 20 20 30  0,  22,  87,   0
31850 2c 20 20 20 39 2c 20 20 20 30 2c 20 20 20 30 2c  ,   9,   0,   0,
31851 20 20 36 34 2c 20 20 36 35 2c 0a 20 20 20 20 20    64,  65,.     
31852 20 20 30 2c 20 20 36 33 2c 20 20 20 36 2c 20 20    0,  63,   6,  
31853 20 30 2c 20 20 34 37 2c 20 20 38 34 2c 20 20 39   0,  47,  84,  9
31854 36 2c 20 20 20 30 2c 20 31 31 33 2c 20 20 39 35  6,   0, 113,  95
31855 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 34 34 2c  ,   0,   0,  44,
31856 0a 20 20 20 20 20 20 20 30 2c 20 20 39 37 2c 20  .       0,  97, 
31857 20 32 34 2c 20 20 20 30 2c 20 20 31 37 2c 20 20   24,   0,  17,  
31858 20 30 2c 20 31 31 37 2c 20 20 34 38 2c 20 20 32   0, 117,  48,  2
31859 33 2c 20 20 20 30 2c 20 20 20 35 2c 20 31 30 34  3,   0,   5, 104
3185a 2c 20 20 32 35 2c 0a 20 20 20 20 20 20 39 30 2c  ,  25,.      90,
3185b 20 20 20 30 2c 20 20 20 30 2c 20 31 31 39 2c 20     0,   0, 119, 
3185c 31 30 30 2c 20 20 35 35 2c 20 31 31 38 2c 20 20  100,  55, 118,  
3185d 35 32 2c 20 20 20 37 2c 20 20 35 30 2c 20 20 20  52,   7,  50,   
3185e 30 2c 20 20 38 35 2c 20 20 20 30 2c 0a 20 20 20  0,  85,   0,.   
3185f 20 20 20 39 34 2c 20 20 32 36 2c 20 20 20 30 2c     94,  26,   0,
31860 20 20 39 33 2c 20 20 20 30 2c 20 20 20 30 2c 20    93,   0,   0, 
31861 20 20 30 2c 20 20 38 39 2c 20 20 38 36 2c 20 20    0,  89,  86,  
31862 39 31 2c 20 20 38 32 2c 20 31 30 33 2c 20 20 31  91,  82, 103,  1
31863 34 2c 0a 20 20 20 20 20 20 33 38 2c 20 31 30 32  4,.      38, 102
31864 2c 20 20 20 30 2c 20 20 37 35 2c 20 20 20 30 2c  ,   0,  75,   0,
31865 20 20 31 38 2c 20 20 38 33 2c 20 31 30 35 2c 20    18,  83, 105, 
31866 20 33 31 2c 20 20 20 30 2c 20 31 31 35 2c 20 20   31,   0, 115,  
31867 37 34 2c 20 31 30 37 2c 0a 20 20 20 20 20 20 35  74, 107,.      5
31868 37 2c 20 20 34 35 2c 20 20 37 38 2c 20 20 20 30  7,  45,  78,   0
31869 2c 20 20 20 30 2c 20 20 38 38 2c 20 20 33 39 2c  ,   0,  88,  39,
3186a 20 20 20 30 2c 20 31 31 30 2c 20 20 20 30 2c 20     0, 110,   0, 
3186b 20 33 35 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20   35,   0,   0,. 
3186c 20 20 20 20 20 32 38 2c 20 20 20 30 2c 20 20 38       28,   0,  8
3186d 30 2c 20 20 35 33 2c 20 20 35 38 2c 20 20 20 30  0,  53,  58,   0
3186e 2c 20 20 32 30 2c 20 20 35 36 2c 20 20 20 30 2c  ,  20,  56,   0,
3186f 20 20 35 31 2c 0a 20 20 7d 3b 0a 20 20 73 74 61    51,.  };.  sta
31870 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  tic const unsign
31871 65 64 20 63 68 61 72 20 61 4e 65 78 74 5b 31 31  ed char aNext[11
31872 39 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 30 2c  9] = {.       0,
31873 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
31874 20 20 34 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    4,   0,   0,  
31875 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   0,   0,   0,   
31876 30 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20 20 20  0,   0,   0,.   
31877 20 20 20 20 30 2c 20 20 20 32 2c 20 20 20 30 2c      0,   2,   0,
31878 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
31879 20 20 30 2c 20 20 20 30 2c 20 20 31 33 2c 20 20    0,   0,  13,  
3187a 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   0,   0,   0,   
3187b 30 2c 0a 20 20 20 20 20 20 20 30 2c 20 20 20 30  0,.       0,   0
3187c 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
3187d 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
3187e 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
3187f 20 30 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 20   0,   0,.       
31880 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
31881 2c 20 20 33 32 2c 20 20 32 31 2c 20 20 20 30 2c  ,  32,  21,   0,
31882 20 20 20 30 2c 20 20 20 30 2c 20 20 34 32 2c 20     0,   0,  42, 
31883 20 20 33 2c 20 20 34 36 2c 20 20 20 30 2c 0a 20    3,  46,   0,. 
31884 20 20 20 20 20 20 30 2c 20 20 20 30 2c 20 20 20        0,   0,   
31885 30 2c 20 20 32 39 2c 20 20 20 30 2c 20 20 20 30  0,  29,   0,   0
31886 2c 20 20 33 37 2c 20 20 20 30 2c 20 20 20 30 2c  ,  37,   0,   0,
31887 20 20 20 30 2c 20 20 20 31 2c 20 20 36 30 2c 20     0,   1,  60, 
31888 20 20 30 2c 0a 20 20 20 20 20 20 20 30 2c 20 20    0,.       0,  
31889 36 31 2c 20 20 20 30 2c 20 20 34 30 2c 20 20 20  61,   0,  40,   
3188a 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
3188b 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
3188c 20 20 35 39 2c 20 20 20 30 2c 0a 20 20 20 20 20    59,   0,.     
3188d 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
3188e 33 30 2c 20 20 35 34 2c 20 20 31 36 2c 20 20 33  30,  54,  16,  3
3188f 33 2c 20 20 31 30 2c 20 20 20 30 2c 20 20 20 30  3,  10,   0,   0
31890 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
31891 0a 20 20 20 20 20 20 20 30 2c 20 20 20 30 2c 20  .       0,   0, 
31892 20 31 31 2c 20 20 36 36 2c 20 20 37 33 2c 20 20   11,  66,  73,  
31893 20 30 2c 20 20 20 38 2c 20 20 20 30 2c 20 20 39   0,   8,   0,  9
31894 38 2c 20 20 39 32 2c 20 20 20 30 2c 20 31 30 31  8,  92,   0, 101
31895 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 38 31 2c  ,   0,.      81,
31896 20 20 20 30 2c 20 20 36 39 2c 20 20 20 30 2c 20     0,  69,   0, 
31897 20 20 30 2c 20 31 30 38 2c 20 20 32 37 2c 20 20    0, 108,  27,  
31898 33 36 2c 20 20 36 37 2c 20 20 37 37 2c 20 20 20  36,  67,  77,   
31899 30 2c 20 20 33 34 2c 20 20 36 32 2c 0a 20 20 20  0,  34,  62,.   
3189a 20 20 20 20 30 2c 20 20 20 30 2c 0a 20 20 7d 3b      0,   0,.  };
3189b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
3189c 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61 4c  unsigned char aL
3189d 65 6e 5b 31 31 39 5d 20 3d 20 7b 0a 20 20 20 20  en[119] = {.    
3189e 20 20 20 37 2c 20 20 20 37 2c 20 20 20 35 2c 20     7,   7,   5, 
3189f 20 20 34 2c 20 20 20 36 2c 20 20 20 34 2c 20 20    4,   6,   4,  
318a0 20 35 2c 20 20 20 33 2c 20 20 20 36 2c 20 20 20   5,   3,   6,   
318a1 37 2c 20 20 20 33 2c 20 20 20 36 2c 20 20 20 36  7,   3,   6,   6
318a2 2c 0a 20 20 20 20 20 20 20 37 2c 20 20 20 37 2c  ,.       7,   7,
318a3 20 20 20 33 2c 20 20 20 38 2c 20 20 20 32 2c 20     3,   8,   2, 
318a4 20 20 36 2c 20 20 20 35 2c 20 20 20 34 2c 20 20    6,   5,   4,  
318a5 20 34 2c 20 20 20 33 2c 20 20 31 30 2c 20 20 20   4,   3,  10,   
318a6 34 2c 20 20 20 36 2c 0a 20 20 20 20 20 20 31 31  4,   6,.      11
318a7 2c 20 20 20 32 2c 20 20 20 37 2c 20 20 20 35 2c  ,   2,   7,   5,
318a8 20 20 20 35 2c 20 20 20 39 2c 20 20 20 36 2c 20     5,   9,   6, 
318a9 20 20 39 2c 20 20 20 39 2c 20 20 20 37 2c 20 20    9,   9,   7,  
318aa 31 30 2c 20 20 31 30 2c 20 20 20 34 2c 0a 20 20  10,  10,   4,.  
318ab 20 20 20 20 20 36 2c 20 20 20 32 2c 20 20 20 33       6,   2,   3
318ac 2c 20 20 20 34 2c 20 20 20 39 2c 20 20 20 32 2c  ,   4,   9,   2,
318ad 20 20 20 36 2c 20 20 20 35 2c 20 20 20 36 2c 20     6,   5,   6, 
318ae 20 20 36 2c 20 20 20 35 2c 20 20 20 36 2c 20 20    6,   5,   6,  
318af 20 35 2c 0a 20 20 20 20 20 20 20 35 2c 20 20 20   5,.       5,   
318b0 37 2c 20 20 20 37 2c 20 20 20 37 2c 20 20 20 33  7,   7,   7,   3
318b1 2c 20 20 20 34 2c 20 20 20 34 2c 20 20 20 37 2c  ,   4,   4,   7,
318b2 20 20 20 33 2c 20 20 20 36 2c 20 20 20 34 2c 20     3,   6,   4, 
318b3 20 20 37 2c 20 20 20 36 2c 0a 20 20 20 20 20 20    7,   6,.      
318b4 31 32 2c 20 20 20 36 2c 20 20 20 39 2c 20 20 20  12,   6,   9,   
318b5 34 2c 20 20 20 36 2c 20 20 20 35 2c 20 20 20 34  4,   6,   5,   4
318b6 2c 20 20 20 37 2c 20 20 20 36 2c 20 20 20 35 2c  ,   7,   6,   5,
318b7 20 20 20 36 2c 20 20 20 37 2c 20 20 20 35 2c 0a     6,   7,   5,.
318b8 20 20 20 20 20 20 20 34 2c 20 20 20 35 2c 20 20         4,   5,  
318b9 20 36 2c 20 20 20 35 2c 20 20 20 37 2c 20 20 20   6,   5,   7,   
318ba 33 2c 20 20 20 37 2c 20 20 31 33 2c 20 20 20 32  3,   7,  13,   2
318bb 2c 20 20 20 32 2c 20 20 20 34 2c 20 20 20 36 2c  ,   2,   4,   6,
318bc 20 20 20 36 2c 0a 20 20 20 20 20 20 20 38 2c 20     6,.       8, 
318bd 20 20 35 2c 20 20 31 37 2c 20 20 31 32 2c 20 20    5,  17,  12,  
318be 20 37 2c 20 20 20 38 2c 20 20 20 38 2c 20 20 20   7,   8,   8,   
318bf 32 2c 20 20 20 34 2c 20 20 20 34 2c 20 20 20 34  2,   4,   4,   4
318c0 2c 20 20 20 34 2c 20 20 20 34 2c 0a 20 20 20 20  ,   4,   4,.    
318c1 20 20 20 32 2c 20 20 20 32 2c 20 20 20 36 2c 20     2,   2,   6, 
318c2 20 20 35 2c 20 20 20 38 2c 20 20 20 35 2c 20 20    5,   8,   5,  
318c3 20 35 2c 20 20 20 38 2c 20 20 20 33 2c 20 20 20   5,   8,   3,   
318c4 35 2c 20 20 20 35 2c 20 20 20 36 2c 20 20 20 34  5,   5,   6,   4
318c5 2c 0a 20 20 20 20 20 20 20 39 2c 20 20 20 33 2c  ,.       9,   3,
318c6 0a 20 20 7d 3b 0a 20 20 73 74 61 74 69 63 20 63  .  };.  static c
318c7 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 73 68  onst unsigned sh
318c8 6f 72 74 20 69 6e 74 20 61 4f 66 66 73 65 74 5b  ort int aOffset[
318c9 31 31 39 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20  119] = {.       
318ca 30 2c 20 20 20 32 2c 20 20 20 32 2c 20 20 20 38  0,   2,   2,   8
318cb 2c 20 20 20 39 2c 20 20 31 34 2c 20 20 31 36 2c  ,   9,  14,  16,
318cc 20 20 32 30 2c 20 20 32 33 2c 20 20 32 35 2c 20    20,  23,  25, 
318cd 20 32 35 2c 20 20 32 39 2c 20 20 33 33 2c 0a 20   25,  29,  33,. 
318ce 20 20 20 20 20 33 36 2c 20 20 34 31 2c 20 20 34       36,  41,  4
318cf 36 2c 20 20 34 38 2c 20 20 35 33 2c 20 20 35 34  6,  48,  53,  54
318d0 2c 20 20 35 39 2c 20 20 36 32 2c 20 20 36 35 2c  ,  59,  62,  65,
318d1 20 20 36 37 2c 20 20 36 39 2c 20 20 37 38 2c 20    67,  69,  78, 
318d2 20 38 31 2c 0a 20 20 20 20 20 20 38 36 2c 20 20   81,.      86,  
318d3 39 35 2c 20 20 39 36 2c 20 31 30 31 2c 20 31 30  95,  96, 101, 10
318d4 35 2c 20 31 30 39 2c 20 31 31 37 2c 20 31 32 32  5, 109, 117, 122
318d5 2c 20 31 32 38 2c 20 31 33 36 2c 20 31 34 32 2c  , 128, 136, 142,
318d6 20 31 35 32 2c 20 31 35 39 2c 0a 20 20 20 20 20   152, 159,.     
318d7 31 36 32 2c 20 31 36 32 2c 20 31 36 35 2c 20 31  162, 162, 165, 1
318d8 36 37 2c 20 31 36 37 2c 20 31 37 31 2c 20 31 37  67, 167, 171, 17
318d9 36 2c 20 31 37 39 2c 20 31 38 34 2c 20 31 38 39  6, 179, 184, 189
318da 2c 20 31 39 34 2c 20 31 39 37 2c 20 32 30 33 2c  , 194, 197, 203,
318db 0a 20 20 20 20 20 32 30 36 2c 20 32 31 30 2c 20  .     206, 210, 
318dc 32 31 37 2c 20 32 32 33 2c 20 32 32 33 2c 20 32  217, 223, 223, 2
318dd 32 36 2c 20 32 32 39 2c 20 32 33 33 2c 20 32 33  26, 229, 233, 23
318de 34 2c 20 32 33 38 2c 20 32 34 34 2c 20 32 34 38  4, 238, 244, 248
318df 2c 20 32 35 35 2c 0a 20 20 20 20 20 32 36 31 2c  , 255,.     261,
318e0 20 32 37 33 2c 20 32 37 39 2c 20 32 38 38 2c 20   273, 279, 288, 
318e1 32 39 30 2c 20 32 39 36 2c 20 33 30 31 2c 20 33  290, 296, 301, 3
318e2 30 33 2c 20 33 31 30 2c 20 33 31 35 2c 20 33 32  03, 310, 315, 32
318e3 30 2c 20 33 32 36 2c 20 33 33 32 2c 0a 20 20 20  0, 326, 332,.   
318e4 20 20 33 33 37 2c 20 33 34 31 2c 20 33 34 34 2c    337, 341, 344,
318e5 20 33 35 30 2c 20 33 35 34 2c 20 33 36 31 2c 20   350, 354, 361, 
318e6 33 36 33 2c 20 33 37 30 2c 20 33 37 32 2c 20 33  363, 370, 372, 3
318e7 37 34 2c 20 33 38 33 2c 20 33 38 37 2c 20 33 39  74, 383, 387, 39
318e8 33 2c 0a 20 20 20 20 20 33 39 39 2c 20 34 30 37  3,.     399, 407
318e9 2c 20 34 31 32 2c 20 34 31 32 2c 20 34 32 38 2c  , 412, 412, 428,
318ea 20 34 33 35 2c 20 34 34 32 2c 20 34 34 33 2c 20   435, 442, 443, 
318eb 34 35 30 2c 20 34 35 34 2c 20 34 35 38 2c 20 34  450, 454, 458, 4
318ec 36 32 2c 20 34 36 36 2c 0a 20 20 20 20 20 34 36  62, 466,.     46
318ed 39 2c 20 34 37 31 2c 20 34 37 33 2c 20 34 37 39  9, 471, 473, 479
318ee 2c 20 34 38 33 2c 20 34 39 31 2c 20 34 39 35 2c  , 483, 491, 495,
318ef 20 35 30 30 2c 20 35 30 38 2c 20 35 31 31 2c 20   500, 508, 511, 
318f0 35 31 36 2c 20 35 32 31 2c 20 35 32 37 2c 0a 20  516, 521, 527,. 
318f1 20 20 20 20 35 33 31 2c 20 35 33 36 2c 0a 20 20      531, 536,.  
318f2 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  };.  static cons
318f3 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
318f4 61 43 6f 64 65 5b 31 31 39 5d 20 3d 20 7b 0a 20  aCode[119] = {. 
318f5 20 20 20 54 4b 5f 52 45 49 4e 44 45 58 2c 20 20     TK_REINDEX,  
318f6 20 20 54 4b 5f 49 4e 44 45 58 45 44 2c 20 20 20    TK_INDEXED,   
318f7 20 54 4b 5f 49 4e 44 45 58 2c 20 20 20 20 20 20   TK_INDEX,      
318f8 54 4b 5f 44 45 53 43 2c 20 20 20 20 20 20 20 54  TK_DESC,       T
318f9 4b 5f 45 53 43 41 50 45 2c 20 20 20 20 20 0a 20  K_ESCAPE,     . 
318fa 20 20 20 54 4b 5f 45 41 43 48 2c 20 20 20 20 20     TK_EACH,     
318fb 20 20 54 4b 5f 43 48 45 43 4b 2c 20 20 20 20 20    TK_CHECK,     
318fc 20 54 4b 5f 4b 45 59 2c 20 20 20 20 20 20 20 20   TK_KEY,        
318fd 54 4b 5f 42 45 46 4f 52 45 2c 20 20 20 20 20 54  TK_BEFORE,     T
318fe 4b 5f 46 4f 52 45 49 47 4e 2c 20 20 20 20 0a 20  K_FOREIGN,    . 
318ff 20 20 20 54 4b 5f 46 4f 52 2c 20 20 20 20 20 20     TK_FOR,      
31900 20 20 54 4b 5f 49 47 4e 4f 52 45 2c 20 20 20 20    TK_IGNORE,    
31901 20 54 4b 5f 4c 49 4b 45 5f 4b 57 2c 20 20 20 20   TK_LIKE_KW,    
31902 54 4b 5f 45 58 50 4c 41 49 4e 2c 20 20 20 20 54  TK_EXPLAIN,    T
31903 4b 5f 49 4e 53 54 45 41 44 2c 20 20 20 20 0a 20  K_INSTEAD,    . 
31904 20 20 20 54 4b 5f 41 44 44 2c 20 20 20 20 20 20     TK_ADD,      
31905 20 20 54 4b 5f 44 41 54 41 42 41 53 45 2c 20 20    TK_DATABASE,  
31906 20 54 4b 5f 41 53 2c 20 20 20 20 20 20 20 20 20   TK_AS,         
31907 54 4b 5f 53 45 4c 45 43 54 2c 20 20 20 20 20 54  TK_SELECT,     T
31908 4b 5f 54 41 42 4c 45 2c 20 20 20 20 20 20 0a 20  K_TABLE,      . 
31909 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20     TK_JOIN_KW,  
3190a 20 20 54 4b 5f 54 48 45 4e 2c 20 20 20 20 20 20    TK_THEN,      
3190b 20 54 4b 5f 45 4e 44 2c 20 20 20 20 20 20 20 20   TK_END,        
3190c 54 4b 5f 44 45 46 45 52 52 41 42 4c 45 2c 20 54  TK_DEFERRABLE, T
3190d 4b 5f 45 4c 53 45 2c 20 20 20 20 20 20 20 0a 20  K_ELSE,       . 
3190e 20 20 20 54 4b 5f 45 58 43 45 50 54 2c 20 20 20     TK_EXCEPT,   
3190f 20 20 54 4b 5f 54 52 41 4e 53 41 43 54 49 4f 4e    TK_TRANSACTION
31910 2c 54 4b 5f 4f 4e 2c 20 20 20 20 20 20 20 20 20  ,TK_ON,         
31911 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 54  TK_JOIN_KW,    T
31912 4b 5f 41 4c 54 45 52 2c 20 20 20 20 20 20 0a 20  K_ALTER,      . 
31913 20 20 20 54 4b 5f 52 41 49 53 45 2c 20 20 20 20     TK_RAISE,    
31914 20 20 54 4b 5f 45 58 43 4c 55 53 49 56 45 2c 20    TK_EXCLUSIVE, 
31915 20 54 4b 5f 45 58 49 53 54 53 2c 20 20 20 20 20   TK_EXISTS,     
31916 54 4b 5f 53 41 56 45 50 4f 49 4e 54 2c 20 20 54  TK_SAVEPOINT,  T
31917 4b 5f 49 4e 54 45 52 53 45 43 54 2c 20 20 0a 20  K_INTERSECT,  . 
31918 20 20 20 54 4b 5f 54 52 49 47 47 45 52 2c 20 20     TK_TRIGGER,  
31919 20 20 54 4b 5f 52 45 46 45 52 45 4e 43 45 53 2c    TK_REFERENCES,
3191a 20 54 4b 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20   TK_CONSTRAINT, 
3191b 54 4b 5f 49 4e 54 4f 2c 20 20 20 20 20 20 20 54  TK_INTO,       T
3191c 4b 5f 4f 46 46 53 45 54 2c 20 20 20 20 20 0a 20  K_OFFSET,     . 
3191d 20 20 20 54 4b 5f 4f 46 2c 20 20 20 20 20 20 20     TK_OF,       
3191e 20 20 54 4b 5f 53 45 54 2c 20 20 20 20 20 20 20    TK_SET,       
3191f 20 54 4b 5f 54 45 4d 50 2c 20 20 20 20 20 20 20   TK_TEMP,       
31920 54 4b 5f 54 45 4d 50 2c 20 20 20 20 20 20 20 54  TK_TEMP,       T
31921 4b 5f 4f 52 2c 20 20 20 20 20 20 20 20 20 0a 20  K_OR,         . 
31922 20 20 20 54 4b 5f 55 4e 49 51 55 45 2c 20 20 20     TK_UNIQUE,   
31923 20 20 54 4b 5f 51 55 45 52 59 2c 20 20 20 20 20    TK_QUERY,     
31924 20 54 4b 5f 41 54 54 41 43 48 2c 20 20 20 20 20   TK_ATTACH,     
31925 54 4b 5f 48 41 56 49 4e 47 2c 20 20 20 20 20 54  TK_HAVING,     T
31926 4b 5f 47 52 4f 55 50 2c 20 20 20 20 20 20 0a 20  K_GROUP,      . 
31927 20 20 20 54 4b 5f 55 50 44 41 54 45 2c 20 20 20     TK_UPDATE,   
31928 20 20 54 4b 5f 42 45 47 49 4e 2c 20 20 20 20 20    TK_BEGIN,     
31929 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20   TK_JOIN_KW,    
3192a 54 4b 5f 52 45 4c 45 41 53 45 2c 20 20 20 20 54  TK_RELEASE,    T
3192b 4b 5f 42 45 54 57 45 45 4e 2c 20 20 20 20 0a 20  K_BETWEEN,    . 
3192c 20 20 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c 20 20     TK_NOTNULL,  
3192d 20 20 54 4b 5f 4e 4f 54 2c 20 20 20 20 20 20 20    TK_NOT,       
3192e 20 54 4b 5f 4e 55 4c 4c 2c 20 20 20 20 20 20 20   TK_NULL,       
3192f 54 4b 5f 4c 49 4b 45 5f 4b 57 2c 20 20 20 20 54  TK_LIKE_KW,    T
31930 4b 5f 43 41 53 43 41 44 45 2c 20 20 20 20 0a 20  K_CASCADE,    . 
31931 20 20 20 54 4b 5f 41 53 43 2c 20 20 20 20 20 20     TK_ASC,      
31932 20 20 54 4b 5f 44 45 4c 45 54 45 2c 20 20 20 20    TK_DELETE,    
31933 20 54 4b 5f 43 41 53 45 2c 20 20 20 20 20 20 20   TK_CASE,       
31934 54 4b 5f 43 4f 4c 4c 41 54 45 2c 20 20 20 20 54  TK_COLLATE,    T
31935 4b 5f 43 52 45 41 54 45 2c 20 20 20 20 20 0a 20  K_CREATE,     . 
31936 20 20 20 54 4b 5f 43 54 49 4d 45 5f 4b 57 2c 20     TK_CTIME_KW, 
31937 20 20 54 4b 5f 44 45 54 41 43 48 2c 20 20 20 20    TK_DETACH,    
31938 20 54 4b 5f 49 4d 4d 45 44 49 41 54 45 2c 20 20   TK_IMMEDIATE,  
31939 54 4b 5f 4a 4f 49 4e 2c 20 20 20 20 20 20 20 54  TK_JOIN,       T
3193a 4b 5f 49 4e 53 45 52 54 2c 20 20 20 20 20 0a 20  K_INSERT,     . 
3193b 20 20 20 54 4b 5f 4d 41 54 43 48 2c 20 20 20 20     TK_MATCH,    
3193c 20 20 54 4b 5f 50 4c 41 4e 2c 20 20 20 20 20 20    TK_PLAN,      
3193d 20 54 4b 5f 41 4e 41 4c 59 5a 45 2c 20 20 20 20   TK_ANALYZE,    
3193e 54 4b 5f 50 52 41 47 4d 41 2c 20 20 20 20 20 54  TK_PRAGMA,     T
3193f 4b 5f 41 42 4f 52 54 2c 20 20 20 20 20 20 0a 20  K_ABORT,      . 
31940 20 20 20 54 4b 5f 56 41 4c 55 45 53 2c 20 20 20     TK_VALUES,   
31941 20 20 54 4b 5f 56 49 52 54 55 41 4c 2c 20 20 20    TK_VIRTUAL,   
31942 20 54 4b 5f 4c 49 4d 49 54 2c 20 20 20 20 20 20   TK_LIMIT,      
31943 54 4b 5f 57 48 45 4e 2c 20 20 20 20 20 20 20 54  TK_WHEN,       T
31944 4b 5f 57 48 45 52 45 2c 20 20 20 20 20 20 0a 20  K_WHERE,      . 
31945 20 20 20 54 4b 5f 52 45 4e 41 4d 45 2c 20 20 20     TK_RENAME,   
31946 20 20 54 4b 5f 41 46 54 45 52 2c 20 20 20 20 20    TK_AFTER,     
31947 20 54 4b 5f 52 45 50 4c 41 43 45 2c 20 20 20 20   TK_REPLACE,    
31948 54 4b 5f 41 4e 44 2c 20 20 20 20 20 20 20 20 54  TK_AND,        T
31949 4b 5f 44 45 46 41 55 4c 54 2c 20 20 20 20 0a 20  K_DEFAULT,    . 
3194a 20 20 20 54 4b 5f 41 55 54 4f 49 4e 43 52 2c 20     TK_AUTOINCR, 
3194b 20 20 54 4b 5f 54 4f 2c 20 20 20 20 20 20 20 20    TK_TO,        
3194c 20 54 4b 5f 49 4e 2c 20 20 20 20 20 20 20 20 20   TK_IN,         
3194d 54 4b 5f 43 41 53 54 2c 20 20 20 20 20 20 20 54  TK_CAST,       T
3194e 4b 5f 43 4f 4c 55 4d 4e 4b 57 2c 20 20 20 0a 20  K_COLUMNKW,   . 
3194f 20 20 20 54 4b 5f 43 4f 4d 4d 49 54 2c 20 20 20     TK_COMMIT,   
31950 20 20 54 4b 5f 43 4f 4e 46 4c 49 43 54 2c 20 20    TK_CONFLICT,  
31951 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20   TK_JOIN_KW,    
31952 54 4b 5f 43 54 49 4d 45 5f 4b 57 2c 20 20 20 54  TK_CTIME_KW,   T
31953 4b 5f 43 54 49 4d 45 5f 4b 57 2c 20 20 20 0a 20  K_CTIME_KW,   . 
31954 20 20 20 54 4b 5f 50 52 49 4d 41 52 59 2c 20 20     TK_PRIMARY,  
31955 20 20 54 4b 5f 44 45 46 45 52 52 45 44 2c 20 20    TK_DEFERRED,  
31956 20 54 4b 5f 44 49 53 54 49 4e 43 54 2c 20 20 20   TK_DISTINCT,   
31957 54 4b 5f 49 53 2c 20 20 20 20 20 20 20 20 20 54  TK_IS,         T
31958 4b 5f 44 52 4f 50 2c 20 20 20 20 20 20 20 0a 20  K_DROP,       . 
31959 20 20 20 54 4b 5f 46 41 49 4c 2c 20 20 20 20 20     TK_FAIL,     
3195a 20 20 54 4b 5f 46 52 4f 4d 2c 20 20 20 20 20 20    TK_FROM,      
3195b 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20   TK_JOIN_KW,    
3195c 54 4b 5f 4c 49 4b 45 5f 4b 57 2c 20 20 20 20 54  TK_LIKE_KW,    T
3195d 4b 5f 42 59 2c 20 20 20 20 20 20 20 20 20 0a 20  K_BY,         . 
3195e 20 20 20 54 4b 5f 49 46 2c 20 20 20 20 20 20 20     TK_IF,       
3195f 20 20 54 4b 5f 49 53 4e 55 4c 4c 2c 20 20 20 20    TK_ISNULL,    
31960 20 54 4b 5f 4f 52 44 45 52 2c 20 20 20 20 20 20   TK_ORDER,      
31961 54 4b 5f 52 45 53 54 52 49 43 54 2c 20 20 20 54  TK_RESTRICT,   T
31962 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 0a 20  K_JOIN_KW,    . 
31963 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20     TK_JOIN_KW,  
31964 20 20 54 4b 5f 52 4f 4c 4c 42 41 43 4b 2c 20 20    TK_ROLLBACK,  
31965 20 54 4b 5f 52 4f 57 2c 20 20 20 20 20 20 20 20   TK_ROW,        
31966 54 4b 5f 55 4e 49 4f 4e 2c 20 20 20 20 20 20 54  TK_UNION,      T
31967 4b 5f 55 53 49 4e 47 2c 20 20 20 20 20 20 0a 20  K_USING,      . 
31968 20 20 20 54 4b 5f 56 41 43 55 55 4d 2c 20 20 20     TK_VACUUM,   
31969 20 20 54 4b 5f 56 49 45 57 2c 20 20 20 20 20 20    TK_VIEW,      
3196a 20 54 4b 5f 49 4e 49 54 49 41 4c 4c 59 2c 20 20   TK_INITIALLY,  
3196b 54 4b 5f 41 4c 4c 2c 20 20 20 20 20 20 20 20 0a  TK_ALL,        .
3196c 20 20 7d 3b 0a 20 20 69 6e 74 20 68 2c 20 69 3b    };.  int h, i;
3196d 0a 20 20 69 66 28 20 6e 3c 32 20 29 20 72 65 74  .  if( n<2 ) ret
3196e 75 72 6e 20 54 4b 5f 49 44 3b 0a 20 20 68 20 3d  urn TK_ID;.  h =
3196f 20 28 28 63 68 61 72 4d 61 70 28 7a 5b 30 5d 29   ((charMap(z[0])
31970 2a 34 29 20 5e 0a 20 20 20 20 20 20 28 63 68 61  *4) ^.      (cha
31971 72 4d 61 70 28 7a 5b 6e 2d 31 5d 29 2a 33 29 20  rMap(z[n-1])*3) 
31972 5e 0a 20 20 20 20 20 20 6e 29 20 25 20 31 32 37  ^.      n) % 127
31973 3b 0a 20 20 66 6f 72 28 69 3d 28 28 69 6e 74 29  ;.  for(i=((int)
31974 61 48 61 73 68 5b 68 5d 29 2d 31 3b 20 69 3e 3d  aHash[h])-1; i>=
31975 30 3b 20 69 3d 28 28 69 6e 74 29 61 4e 65 78 74  0; i=((int)aNext
31976 5b 69 5d 29 2d 31 29 7b 0a 20 20 20 20 69 66 28  [i])-1){.    if(
31977 20 61 4c 65 6e 5b 69 5d 3d 3d 6e 20 26 26 20 73   aLen[i]==n && s
31978 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 26  qlite3StrNICmp(&
31979 7a 54 65 78 74 5b 61 4f 66 66 73 65 74 5b 69 5d  zText[aOffset[i]
3197a 5d 2c 7a 2c 6e 29 3d 3d 30 20 29 7b 0a 20 20 20  ],z,n)==0 ){.   
3197b 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
3197c 30 20 29 3b 20 2f 2a 20 52 45 49 4e 44 45 58 20  0 ); /* REINDEX 
3197d 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
3197e 65 28 20 69 3d 3d 31 20 29 3b 20 2f 2a 20 49 4e  e( i==1 ); /* IN
3197f 44 45 58 45 44 20 2a 2f 0a 20 20 20 20 20 20 74  DEXED */.      t
31980 65 73 74 63 61 73 65 28 20 69 3d 3d 32 20 29 3b  estcase( i==2 );
31981 20 2f 2a 20 49 4e 44 45 58 20 2a 2f 0a 20 20 20   /* INDEX */.   
31982 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
31983 33 20 29 3b 20 2f 2a 20 44 45 53 43 20 2a 2f 0a  3 ); /* DESC */.
31984 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
31985 69 3d 3d 34 20 29 3b 20 2f 2a 20 45 53 43 41 50  i==4 ); /* ESCAP
31986 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  E */.      testc
31987 61 73 65 28 20 69 3d 3d 35 20 29 3b 20 2f 2a 20  ase( i==5 ); /* 
31988 45 41 43 48 20 2a 2f 0a 20 20 20 20 20 20 74 65  EACH */.      te
31989 73 74 63 61 73 65 28 20 69 3d 3d 36 20 29 3b 20  stcase( i==6 ); 
3198a 2f 2a 20 43 48 45 43 4b 20 2a 2f 0a 20 20 20 20  /* CHECK */.    
3198b 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37    testcase( i==7
3198c 20 29 3b 20 2f 2a 20 4b 45 59 20 2a 2f 0a 20 20   ); /* KEY */.  
3198d 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
3198e 3d 38 20 29 3b 20 2f 2a 20 42 45 46 4f 52 45 20  =8 ); /* BEFORE 
3198f 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
31990 65 28 20 69 3d 3d 39 20 29 3b 20 2f 2a 20 46 4f  e( i==9 ); /* FO
31991 52 45 49 47 4e 20 2a 2f 0a 20 20 20 20 20 20 74  REIGN */.      t
31992 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30 20 29  estcase( i==10 )
31993 3b 20 2f 2a 20 46 4f 52 20 2a 2f 0a 20 20 20 20  ; /* FOR */.    
31994 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31    testcase( i==1
31995 31 20 29 3b 20 2f 2a 20 49 47 4e 4f 52 45 20 2a  1 ); /* IGNORE *
31996 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
31997 28 20 69 3d 3d 31 32 20 29 3b 20 2f 2a 20 52 45  ( i==12 ); /* RE
31998 47 45 58 50 20 2a 2f 0a 20 20 20 20 20 20 74 65  GEXP */.      te
31999 73 74 63 61 73 65 28 20 69 3d 3d 31 33 20 29 3b  stcase( i==13 );
3199a 20 2f 2a 20 45 58 50 4c 41 49 4e 20 2a 2f 0a 20   /* EXPLAIN */. 
3199b 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3199c 3d 3d 31 34 20 29 3b 20 2f 2a 20 49 4e 53 54 45  ==14 ); /* INSTE
3199d 41 44 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  AD */.      test
3199e 63 61 73 65 28 20 69 3d 3d 31 35 20 29 3b 20 2f  case( i==15 ); /
3199f 2a 20 41 44 44 20 2a 2f 0a 20 20 20 20 20 20 74  * ADD */.      t
319a0 65 73 74 63 61 73 65 28 20 69 3d 3d 31 36 20 29  estcase( i==16 )
319a1 3b 20 2f 2a 20 44 41 54 41 42 41 53 45 20 2a 2f  ; /* DATABASE */
319a2 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
319a3 20 69 3d 3d 31 37 20 29 3b 20 2f 2a 20 41 53 20   i==17 ); /* AS 
319a4 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
319a5 65 28 20 69 3d 3d 31 38 20 29 3b 20 2f 2a 20 53  e( i==18 ); /* S
319a6 45 4c 45 43 54 20 2a 2f 0a 20 20 20 20 20 20 74  ELECT */.      t
319a7 65 73 74 63 61 73 65 28 20 69 3d 3d 31 39 20 29  estcase( i==19 )
319a8 3b 20 2f 2a 20 54 41 42 4c 45 20 2a 2f 0a 20 20  ; /* TABLE */.  
319a9 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
319aa 3d 32 30 20 29 3b 20 2f 2a 20 4c 45 46 54 20 2a  =20 ); /* LEFT *
319ab 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
319ac 28 20 69 3d 3d 32 31 20 29 3b 20 2f 2a 20 54 48  ( i==21 ); /* TH
319ad 45 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  EN */.      test
319ae 63 61 73 65 28 20 69 3d 3d 32 32 20 29 3b 20 2f  case( i==22 ); /
319af 2a 20 45 4e 44 20 2a 2f 0a 20 20 20 20 20 20 74  * END */.      t
319b0 65 73 74 63 61 73 65 28 20 69 3d 3d 32 33 20 29  estcase( i==23 )
319b1 3b 20 2f 2a 20 44 45 46 45 52 52 41 42 4c 45 20  ; /* DEFERRABLE 
319b2 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
319b3 65 28 20 69 3d 3d 32 34 20 29 3b 20 2f 2a 20 45  e( i==24 ); /* E
319b4 4c 53 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  LSE */.      tes
319b5 74 63 61 73 65 28 20 69 3d 3d 32 35 20 29 3b 20  tcase( i==25 ); 
319b6 2f 2a 20 45 58 43 45 50 54 20 2a 2f 0a 20 20 20  /* EXCEPT */.   
319b7 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
319b8 32 36 20 29 3b 20 2f 2a 20 54 52 41 4e 53 41 43  26 ); /* TRANSAC
319b9 54 49 4f 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65  TION */.      te
319ba 73 74 63 61 73 65 28 20 69 3d 3d 32 37 20 29 3b  stcase( i==27 );
319bb 20 2f 2a 20 4f 4e 20 2a 2f 0a 20 20 20 20 20 20   /* ON */.      
319bc 74 65 73 74 63 61 73 65 28 20 69 3d 3d 32 38 20  testcase( i==28 
319bd 29 3b 20 2f 2a 20 4e 41 54 55 52 41 4c 20 2a 2f  ); /* NATURAL */
319be 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
319bf 20 69 3d 3d 32 39 20 29 3b 20 2f 2a 20 41 4c 54   i==29 ); /* ALT
319c0 45 52 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  ER */.      test
319c1 63 61 73 65 28 20 69 3d 3d 33 30 20 29 3b 20 2f  case( i==30 ); /
319c2 2a 20 52 41 49 53 45 20 2a 2f 0a 20 20 20 20 20  * RAISE */.     
319c3 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 33 31   testcase( i==31
319c4 20 29 3b 20 2f 2a 20 45 58 43 4c 55 53 49 56 45   ); /* EXCLUSIVE
319c5 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
319c6 73 65 28 20 69 3d 3d 33 32 20 29 3b 20 2f 2a 20  se( i==32 ); /* 
319c7 45 58 49 53 54 53 20 2a 2f 0a 20 20 20 20 20 20  EXISTS */.      
319c8 74 65 73 74 63 61 73 65 28 20 69 3d 3d 33 33 20  testcase( i==33 
319c9 29 3b 20 2f 2a 20 53 41 56 45 50 4f 49 4e 54 20  ); /* SAVEPOINT 
319ca 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
319cb 65 28 20 69 3d 3d 33 34 20 29 3b 20 2f 2a 20 49  e( i==34 ); /* I
319cc 4e 54 45 52 53 45 43 54 20 2a 2f 0a 20 20 20 20  NTERSECT */.    
319cd 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 33    testcase( i==3
319ce 35 20 29 3b 20 2f 2a 20 54 52 49 47 47 45 52 20  5 ); /* TRIGGER 
319cf 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
319d0 65 28 20 69 3d 3d 33 36 20 29 3b 20 2f 2a 20 52  e( i==36 ); /* R
319d1 45 46 45 52 45 4e 43 45 53 20 2a 2f 0a 20 20 20  EFERENCES */.   
319d2 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
319d3 33 37 20 29 3b 20 2f 2a 20 43 4f 4e 53 54 52 41  37 ); /* CONSTRA
319d4 49 4e 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  INT */.      tes
319d5 74 63 61 73 65 28 20 69 3d 3d 33 38 20 29 3b 20  tcase( i==38 ); 
319d6 2f 2a 20 49 4e 54 4f 20 2a 2f 0a 20 20 20 20 20  /* INTO */.     
319d7 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 33 39   testcase( i==39
319d8 20 29 3b 20 2f 2a 20 4f 46 46 53 45 54 20 2a 2f   ); /* OFFSET */
319d9 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
319da 20 69 3d 3d 34 30 20 29 3b 20 2f 2a 20 4f 46 20   i==40 ); /* OF 
319db 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
319dc 65 28 20 69 3d 3d 34 31 20 29 3b 20 2f 2a 20 53  e( i==41 ); /* S
319dd 45 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  ET */.      test
319de 63 61 73 65 28 20 69 3d 3d 34 32 20 29 3b 20 2f  case( i==42 ); /
319df 2a 20 54 45 4d 50 20 2a 2f 0a 20 20 20 20 20 20  * TEMP */.      
319e0 74 65 73 74 63 61 73 65 28 20 69 3d 3d 34 33 20  testcase( i==43 
319e1 29 3b 20 2f 2a 20 54 45 4d 50 4f 52 41 52 59 20  ); /* TEMPORARY 
319e2 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
319e3 65 28 20 69 3d 3d 34 34 20 29 3b 20 2f 2a 20 4f  e( i==44 ); /* O
319e4 52 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  R */.      testc
319e5 61 73 65 28 20 69 3d 3d 34 35 20 29 3b 20 2f 2a  ase( i==45 ); /*
319e6 20 55 4e 49 51 55 45 20 2a 2f 0a 20 20 20 20 20   UNIQUE */.     
319e7 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 34 36   testcase( i==46
319e8 20 29 3b 20 2f 2a 20 51 55 45 52 59 20 2a 2f 0a   ); /* QUERY */.
319e9 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
319ea 69 3d 3d 34 37 20 29 3b 20 2f 2a 20 41 54 54 41  i==47 ); /* ATTA
319eb 43 48 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  CH */.      test
319ec 63 61 73 65 28 20 69 3d 3d 34 38 20 29 3b 20 2f  case( i==48 ); /
319ed 2a 20 48 41 56 49 4e 47 20 2a 2f 0a 20 20 20 20  * HAVING */.    
319ee 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 34    testcase( i==4
319ef 39 20 29 3b 20 2f 2a 20 47 52 4f 55 50 20 2a 2f  9 ); /* GROUP */
319f0 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
319f1 20 69 3d 3d 35 30 20 29 3b 20 2f 2a 20 55 50 44   i==50 ); /* UPD
319f2 41 54 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ATE */.      tes
319f3 74 63 61 73 65 28 20 69 3d 3d 35 31 20 29 3b 20  tcase( i==51 ); 
319f4 2f 2a 20 42 45 47 49 4e 20 2a 2f 0a 20 20 20 20  /* BEGIN */.    
319f5 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 35    testcase( i==5
319f6 32 20 29 3b 20 2f 2a 20 49 4e 4e 45 52 20 2a 2f  2 ); /* INNER */
319f7 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
319f8 20 69 3d 3d 35 33 20 29 3b 20 2f 2a 20 52 45 4c   i==53 ); /* REL
319f9 45 41 53 45 20 2a 2f 0a 20 20 20 20 20 20 74 65  EASE */.      te
319fa 73 74 63 61 73 65 28 20 69 3d 3d 35 34 20 29 3b  stcase( i==54 );
319fb 20 2f 2a 20 42 45 54 57 45 45 4e 20 2a 2f 0a 20   /* BETWEEN */. 
319fc 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
319fd 3d 3d 35 35 20 29 3b 20 2f 2a 20 4e 4f 54 4e 55  ==55 ); /* NOTNU
319fe 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  LL */.      test
319ff 63 61 73 65 28 20 69 3d 3d 35 36 20 29 3b 20 2f  case( i==56 ); /
31a00 2a 20 4e 4f 54 20 2a 2f 0a 20 20 20 20 20 20 74  * NOT */.      t
31a01 65 73 74 63 61 73 65 28 20 69 3d 3d 35 37 20 29  estcase( i==57 )
31a02 3b 20 2f 2a 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20  ; /* NULL */.   
31a03 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
31a04 35 38 20 29 3b 20 2f 2a 20 4c 49 4b 45 20 2a 2f  58 ); /* LIKE */
31a05 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
31a06 20 69 3d 3d 35 39 20 29 3b 20 2f 2a 20 43 41 53   i==59 ); /* CAS
31a07 43 41 44 45 20 2a 2f 0a 20 20 20 20 20 20 74 65  CADE */.      te
31a08 73 74 63 61 73 65 28 20 69 3d 3d 36 30 20 29 3b  stcase( i==60 );
31a09 20 2f 2a 20 41 53 43 20 2a 2f 0a 20 20 20 20 20   /* ASC */.     
31a0a 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36 31   testcase( i==61
31a0b 20 29 3b 20 2f 2a 20 44 45 4c 45 54 45 20 2a 2f   ); /* DELETE */
31a0c 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
31a0d 20 69 3d 3d 36 32 20 29 3b 20 2f 2a 20 43 41 53   i==62 ); /* CAS
31a0e 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  E */.      testc
31a0f 61 73 65 28 20 69 3d 3d 36 33 20 29 3b 20 2f 2a  ase( i==63 ); /*
31a10 20 43 4f 4c 4c 41 54 45 20 2a 2f 0a 20 20 20 20   COLLATE */.    
31a11 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36    testcase( i==6
31a12 34 20 29 3b 20 2f 2a 20 43 52 45 41 54 45 20 2a  4 ); /* CREATE *
31a13 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
31a14 28 20 69 3d 3d 36 35 20 29 3b 20 2f 2a 20 43 55  ( i==65 ); /* CU
31a15 52 52 45 4e 54 5f 44 41 54 45 20 2a 2f 0a 20 20  RRENT_DATE */.  
31a16 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
31a17 3d 36 36 20 29 3b 20 2f 2a 20 44 45 54 41 43 48  =66 ); /* DETACH
31a18 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
31a19 73 65 28 20 69 3d 3d 36 37 20 29 3b 20 2f 2a 20  se( i==67 ); /* 
31a1a 49 4d 4d 45 44 49 41 54 45 20 2a 2f 0a 20 20 20  IMMEDIATE */.   
31a1b 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
31a1c 36 38 20 29 3b 20 2f 2a 20 4a 4f 49 4e 20 2a 2f  68 ); /* JOIN */
31a1d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
31a1e 20 69 3d 3d 36 39 20 29 3b 20 2f 2a 20 49 4e 53   i==69 ); /* INS
31a1f 45 52 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ERT */.      tes
31a20 74 63 61 73 65 28 20 69 3d 3d 37 30 20 29 3b 20  tcase( i==70 ); 
31a21 2f 2a 20 4d 41 54 43 48 20 2a 2f 0a 20 20 20 20  /* MATCH */.    
31a22 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37    testcase( i==7
31a23 31 20 29 3b 20 2f 2a 20 50 4c 41 4e 20 2a 2f 0a  1 ); /* PLAN */.
31a24 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
31a25 69 3d 3d 37 32 20 29 3b 20 2f 2a 20 41 4e 41 4c  i==72 ); /* ANAL
31a26 59 5a 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  YZE */.      tes
31a27 74 63 61 73 65 28 20 69 3d 3d 37 33 20 29 3b 20  tcase( i==73 ); 
31a28 2f 2a 20 50 52 41 47 4d 41 20 2a 2f 0a 20 20 20  /* PRAGMA */.   
31a29 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
31a2a 37 34 20 29 3b 20 2f 2a 20 41 42 4f 52 54 20 2a  74 ); /* ABORT *
31a2b 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
31a2c 28 20 69 3d 3d 37 35 20 29 3b 20 2f 2a 20 56 41  ( i==75 ); /* VA
31a2d 4c 55 45 53 20 2a 2f 0a 20 20 20 20 20 20 74 65  LUES */.      te
31a2e 73 74 63 61 73 65 28 20 69 3d 3d 37 36 20 29 3b  stcase( i==76 );
31a2f 20 2f 2a 20 56 49 52 54 55 41 4c 20 2a 2f 0a 20   /* VIRTUAL */. 
31a30 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
31a31 3d 3d 37 37 20 29 3b 20 2f 2a 20 4c 49 4d 49 54  ==77 ); /* LIMIT
31a32 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
31a33 73 65 28 20 69 3d 3d 37 38 20 29 3b 20 2f 2a 20  se( i==78 ); /* 
31a34 57 48 45 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65  WHEN */.      te
31a35 73 74 63 61 73 65 28 20 69 3d 3d 37 39 20 29 3b  stcase( i==79 );
31a36 20 2f 2a 20 57 48 45 52 45 20 2a 2f 0a 20 20 20   /* WHERE */.   
31a37 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
31a38 38 30 20 29 3b 20 2f 2a 20 52 45 4e 41 4d 45 20  80 ); /* RENAME 
31a39 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
31a3a 65 28 20 69 3d 3d 38 31 20 29 3b 20 2f 2a 20 41  e( i==81 ); /* A
31a3b 46 54 45 52 20 2a 2f 0a 20 20 20 20 20 20 74 65  FTER */.      te
31a3c 73 74 63 61 73 65 28 20 69 3d 3d 38 32 20 29 3b  stcase( i==82 );
31a3d 20 2f 2a 20 52 45 50 4c 41 43 45 20 2a 2f 0a 20   /* REPLACE */. 
31a3e 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
31a3f 3d 3d 38 33 20 29 3b 20 2f 2a 20 41 4e 44 20 2a  ==83 ); /* AND *
31a40 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
31a41 28 20 69 3d 3d 38 34 20 29 3b 20 2f 2a 20 44 45  ( i==84 ); /* DE
31a42 46 41 55 4c 54 20 2a 2f 0a 20 20 20 20 20 20 74  FAULT */.      t
31a43 65 73 74 63 61 73 65 28 20 69 3d 3d 38 35 20 29  estcase( i==85 )
31a44 3b 20 2f 2a 20 41 55 54 4f 49 4e 43 52 45 4d 45  ; /* AUTOINCREME
31a45 4e 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  NT */.      test
31a46 63 61 73 65 28 20 69 3d 3d 38 36 20 29 3b 20 2f  case( i==86 ); /
31a47 2a 20 54 4f 20 2a 2f 0a 20 20 20 20 20 20 74 65  * TO */.      te
31a48 73 74 63 61 73 65 28 20 69 3d 3d 38 37 20 29 3b  stcase( i==87 );
31a49 20 2f 2a 20 49 4e 20 2a 2f 0a 20 20 20 20 20 20   /* IN */.      
31a4a 74 65 73 74 63 61 73 65 28 20 69 3d 3d 38 38 20  testcase( i==88 
31a4b 29 3b 20 2f 2a 20 43 41 53 54 20 2a 2f 0a 20 20  ); /* CAST */.  
31a4c 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
31a4d 3d 38 39 20 29 3b 20 2f 2a 20 43 4f 4c 55 4d 4e  =89 ); /* COLUMN
31a4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
31a4f 73 65 28 20 69 3d 3d 39 30 20 29 3b 20 2f 2a 20  se( i==90 ); /* 
31a50 43 4f 4d 4d 49 54 20 2a 2f 0a 20 20 20 20 20 20  COMMIT */.      
31a51 74 65 73 74 63 61 73 65 28 20 69 3d 3d 39 31 20  testcase( i==91 
31a52 29 3b 20 2f 2a 20 43 4f 4e 46 4c 49 43 54 20 2a  ); /* CONFLICT *
31a53 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
31a54 28 20 69 3d 3d 39 32 20 29 3b 20 2f 2a 20 43 52  ( i==92 ); /* CR
31a55 4f 53 53 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  OSS */.      tes
31a56 74 63 61 73 65 28 20 69 3d 3d 39 33 20 29 3b 20  tcase( i==93 ); 
31a57 2f 2a 20 43 55 52 52 45 4e 54 5f 54 49 4d 45 53  /* CURRENT_TIMES
31a58 54 41 4d 50 20 2a 2f 0a 20 20 20 20 20 20 74 65  TAMP */.      te
31a59 73 74 63 61 73 65 28 20 69 3d 3d 39 34 20 29 3b  stcase( i==94 );
31a5a 20 2f 2a 20 43 55 52 52 45 4e 54 5f 54 49 4d 45   /* CURRENT_TIME
31a5b 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
31a5c 73 65 28 20 69 3d 3d 39 35 20 29 3b 20 2f 2a 20  se( i==95 ); /* 
31a5d 50 52 49 4d 41 52 59 20 2a 2f 0a 20 20 20 20 20  PRIMARY */.     
31a5e 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 39 36   testcase( i==96
31a5f 20 29 3b 20 2f 2a 20 44 45 46 45 52 52 45 44 20   ); /* DEFERRED 
31a60 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
31a61 65 28 20 69 3d 3d 39 37 20 29 3b 20 2f 2a 20 44  e( i==97 ); /* D
31a62 49 53 54 49 4e 43 54 20 2a 2f 0a 20 20 20 20 20  ISTINCT */.     
31a63 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 39 38   testcase( i==98
31a64 20 29 3b 20 2f 2a 20 49 53 20 2a 2f 0a 20 20 20   ); /* IS */.   
31a65 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
31a66 39 39 20 29 3b 20 2f 2a 20 44 52 4f 50 20 2a 2f  99 ); /* DROP */
31a67 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
31a68 20 69 3d 3d 31 30 30 20 29 3b 20 2f 2a 20 46 41   i==100 ); /* FA
31a69 49 4c 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  IL */.      test
31a6a 63 61 73 65 28 20 69 3d 3d 31 30 31 20 29 3b 20  case( i==101 ); 
31a6b 2f 2a 20 46 52 4f 4d 20 2a 2f 0a 20 20 20 20 20  /* FROM */.     
31a6c 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30   testcase( i==10
31a6d 32 20 29 3b 20 2f 2a 20 46 55 4c 4c 20 2a 2f 0a  2 ); /* FULL */.
31a6e 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
31a6f 69 3d 3d 31 30 33 20 29 3b 20 2f 2a 20 47 4c 4f  i==103 ); /* GLO
31a70 42 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  B */.      testc
31a71 61 73 65 28 20 69 3d 3d 31 30 34 20 29 3b 20 2f  ase( i==104 ); /
31a72 2a 20 42 59 20 2a 2f 0a 20 20 20 20 20 20 74 65  * BY */.      te
31a73 73 74 63 61 73 65 28 20 69 3d 3d 31 30 35 20 29  stcase( i==105 )
31a74 3b 20 2f 2a 20 49 46 20 2a 2f 0a 20 20 20 20 20  ; /* IF */.     
31a75 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30   testcase( i==10
31a76 36 20 29 3b 20 2f 2a 20 49 53 4e 55 4c 4c 20 2a  6 ); /* ISNULL *
31a77 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
31a78 28 20 69 3d 3d 31 30 37 20 29 3b 20 2f 2a 20 4f  ( i==107 ); /* O
31a79 52 44 45 52 20 2a 2f 0a 20 20 20 20 20 20 74 65  RDER */.      te
31a7a 73 74 63 61 73 65 28 20 69 3d 3d 31 30 38 20 29  stcase( i==108 )
31a7b 3b 20 2f 2a 20 52 45 53 54 52 49 43 54 20 2a 2f  ; /* RESTRICT */
31a7c 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
31a7d 20 69 3d 3d 31 30 39 20 29 3b 20 2f 2a 20 4f 55   i==109 ); /* OU
31a7e 54 45 52 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  TER */.      tes
31a7f 74 63 61 73 65 28 20 69 3d 3d 31 31 30 20 29 3b  tcase( i==110 );
31a80 20 2f 2a 20 52 49 47 48 54 20 2a 2f 0a 20 20 20   /* RIGHT */.   
31a81 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
31a82 31 31 31 20 29 3b 20 2f 2a 20 52 4f 4c 4c 42 41  111 ); /* ROLLBA
31a83 43 4b 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  CK */.      test
31a84 63 61 73 65 28 20 69 3d 3d 31 31 32 20 29 3b 20  case( i==112 ); 
31a85 2f 2a 20 52 4f 57 20 2a 2f 0a 20 20 20 20 20 20  /* ROW */.      
31a86 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 31 33  testcase( i==113
31a87 20 29 3b 20 2f 2a 20 55 4e 49 4f 4e 20 2a 2f 0a   ); /* UNION */.
31a88 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
31a89 69 3d 3d 31 31 34 20 29 3b 20 2f 2a 20 55 53 49  i==114 ); /* USI
31a8a 4e 47 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  NG */.      test
31a8b 63 61 73 65 28 20 69 3d 3d 31 31 35 20 29 3b 20  case( i==115 ); 
31a8c 2f 2a 20 56 41 43 55 55 4d 20 2a 2f 0a 20 20 20  /* VACUUM */.   
31a8d 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
31a8e 31 31 36 20 29 3b 20 2f 2a 20 56 49 45 57 20 2a  116 ); /* VIEW *
31a8f 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
31a90 28 20 69 3d 3d 31 31 37 20 29 3b 20 2f 2a 20 49  ( i==117 ); /* I
31a91 4e 49 54 49 41 4c 4c 59 20 2a 2f 0a 20 20 20 20  NITIALLY */.    
31a92 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31    testcase( i==1
31a93 31 38 20 29 3b 20 2f 2a 20 41 4c 4c 20 2a 2f 0a  18 ); /* ALL */.
31a94 20 20 20 20 20 20 72 65 74 75 72 6e 20 61 43 6f        return aCo
31a95 64 65 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d  de[i];.    }.  }
31a96 0a 20 20 72 65 74 75 72 6e 20 54 4b 5f 49 44 3b  .  return TK_ID;
31a97 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
31a98 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4b 65 79  E int sqlite3Key
31a99 77 6f 72 64 43 6f 64 65 28 63 6f 6e 73 74 20 75  wordCode(const u
31a9a 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 2c  nsigned char *z,
31a9b 20 69 6e 74 20 6e 29 7b 0a 20 20 72 65 74 75 72   int n){.  retur
31a9c 6e 20 6b 65 79 77 6f 72 64 43 6f 64 65 28 28 63  n keywordCode((c
31a9d 68 61 72 2a 29 7a 2c 20 6e 29 3b 0a 7d 0a 0a 2f  har*)z, n);.}../
31a9e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
31a9f 6e 64 20 6f 66 20 6b 65 79 77 6f 72 64 68 61 73  nd of keywordhas
31aa0 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h.h ************
31aa1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31aa2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
31aa3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43  ************** C
31aa4 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20  ontinuing where 
31aa5 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 74  we left off in t
31aa6 6f 6b 65 6e 69 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a  okenize.c ******
31aa7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
31aa8 0a 2f 2a 0a 2a 2a 20 49 66 20 58 20 69 73 20 61  ./*.** If X is a
31aa9 20 63 68 61 72 61 63 74 65 72 20 74 68 61 74 20   character that 
31aaa 63 61 6e 20 62 65 20 75 73 65 64 20 69 6e 20 61  can be used in a
31aab 6e 20 69 64 65 6e 74 69 66 69 65 72 20 74 68 65  n identifier the
31aac 6e 0a 2a 2a 20 49 64 43 68 61 72 28 58 29 20 77  n.** IdChar(X) w
31aad 69 6c 6c 20 62 65 20 74 72 75 65 2e 20 20 4f 74  ill be true.  Ot
31aae 68 65 72 77 69 73 65 20 69 74 20 69 73 20 66 61  herwise it is fa
31aaf 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 41  lse..**.** For A
31ab0 53 43 49 49 2c 20 61 6e 79 20 63 68 61 72 61 63  SCII, any charac
31ab1 74 65 72 20 77 69 74 68 20 74 68 65 20 68 69 67  ter with the hig
31ab2 68 2d 6f 72 64 65 72 20 62 69 74 20 73 65 74 20  h-order bit set 
31ab3 69 73 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 69 6e  is.** allowed in
31ab4 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72 2e 20   an identifier. 
31ab5 20 46 6f 72 20 37 2d 62 69 74 20 63 68 61 72 61   For 7-bit chara
31ab6 63 74 65 72 73 2c 20 0a 2a 2a 20 73 71 6c 69 74  cters, .** sqlit
31ab7 65 33 49 73 49 64 43 68 61 72 5b 58 5d 20 6d 75  e3IsIdChar[X] mu
31ab8 73 74 20 62 65 20 31 2e 0a 2a 2a 0a 2a 2a 20 46  st be 1..**.** F
31ab9 6f 72 20 45 42 43 44 49 43 2c 20 74 68 65 20 72  or EBCDIC, the r
31aba 75 6c 65 73 20 61 72 65 20 6d 6f 72 65 20 63 6f  ules are more co
31abb 6d 70 6c 65 78 20 62 75 74 20 68 61 76 65 20 74  mplex but have t
31abc 68 65 20 73 61 6d 65 0a 2a 2a 20 65 6e 64 20 72  he same.** end r
31abd 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 54 69 63  esult..**.** Tic
31abe 6b 65 74 20 23 31 30 36 36 2e 20 20 74 68 65 20  ket #1066.  the 
31abf 53 51 4c 20 73 74 61 6e 64 61 72 64 20 64 6f 65  SQL standard doe
31ac0 73 20 6e 6f 74 20 61 6c 6c 6f 77 20 27 24 27 20  s not allow '$' 
31ac1 69 6e 20 74 68 65 0a 2a 2a 20 6d 69 64 64 6c 65  in the.** middle
31ac2 20 6f 66 20 69 64 65 6e 74 66 69 65 72 73 2e 20   of identfiers. 
31ac3 20 42 75 74 20 6d 61 6e 79 20 53 51 4c 20 69 6d   But many SQL im
31ac4 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 64 6f  plementations do
31ac5 2e 20 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c  . .** SQLite wil
31ac6 6c 20 61 6c 6c 6f 77 20 27 24 27 20 69 6e 20 69  l allow '$' in i
31ac7 64 65 6e 74 69 66 69 65 72 73 20 66 6f 72 20 63  dentifiers for c
31ac8 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a 2a 2a  ompatibility..**
31ac9 20 42 75 74 20 74 68 65 20 66 65 61 74 75 72 65   But the feature
31aca 20 69 73 20 75 6e 64 6f 63 75 6d 65 6e 74 65 64   is undocumented
31acb 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
31acc 54 45 5f 41 53 43 49 49 0a 53 51 4c 49 54 45 5f  TE_ASCII.SQLITE_
31acd 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68  PRIVATE const ch
31ace 61 72 20 73 71 6c 69 74 65 33 49 73 41 73 63 69  ar sqlite3IsAsci
31acf 69 49 64 43 68 61 72 5b 5d 20 3d 20 7b 0a 2f 2a  iIdChar[] = {./*
31ad0 20 78 30 20 78 31 20 78 32 20 78 33 20 78 34 20   x0 x1 x2 x3 x4 
31ad1 78 35 20 78 36 20 78 37 20 78 38 20 78 39 20 78  x5 x6 x7 x8 x9 x
31ad2 41 20 78 42 20 78 43 20 78 44 20 78 45 20 78 46  A xB xC xD xE xF
31ad3 20 2a 2f 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c   */.    0, 0, 0,
31ad4 20 30 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 1, 0, 0, 0, 
31ad5 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
31ad6 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 32 78 20 2a  , 0, 0,  /* 2x *
31ad7 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 31  /.    1, 1, 1, 1
31ad8 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
31ad9 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   1, 0, 0, 0, 0, 
31ada 30 2c 20 30 2c 20 20 2f 2a 20 33 78 20 2a 2f 0a  0, 0,  /* 3x */.
31adb 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20      0, 1, 1, 1, 
31adc 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
31add 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
31ade 20 31 2c 20 20 2f 2a 20 34 78 20 2a 2f 0a 20 20   1,  /* 4x */.  
31adf 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    1, 1, 1, 1, 1,
31ae0 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
31ae1 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31  1, 0, 0, 0, 0, 1
31ae2 2c 20 20 2f 2a 20 35 78 20 2a 2f 0a 20 20 20 20  ,  /* 5x */.    
31ae3 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 1, 1, 1, 1, 1
31ae4 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
31ae5 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
31ae6 20 2f 2a 20 36 78 20 2a 2f 0a 20 20 20 20 31 2c   /* 6x */.    1,
31ae7 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
31ae8 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30  1, 1, 1, 1, 1, 0
31ae9 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f  , 0, 0, 0, 0,  /
31aea 2a 20 37 78 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69  * 7x */.};.#defi
31aeb 6e 65 20 49 64 43 68 61 72 28 43 29 20 20 28 28  ne IdChar(C)  ((
31aec 28 63 3d 43 29 26 30 78 38 30 29 21 3d 30 20 7c  (c=C)&0x80)!=0 |
31aed 7c 20 28 63 3e 30 78 31 66 20 26 26 20 73 71 6c  | (c>0x1f && sql
31aee 69 74 65 33 49 73 41 73 63 69 69 49 64 43 68 61  ite3IsAsciiIdCha
31aef 72 5b 63 2d 30 78 32 30 5d 29 29 0a 23 65 6e 64  r[c-0x20])).#end
31af0 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  if.#ifdef SQLITE
31af1 5f 45 42 43 44 49 43 0a 53 51 4c 49 54 45 5f 50  _EBCDIC.SQLITE_P
31af2 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61  RIVATE const cha
31af3 72 20 73 71 6c 69 74 65 33 49 73 45 62 63 64 69  r sqlite3IsEbcdi
31af4 63 49 64 43 68 61 72 5b 5d 20 3d 20 7b 0a 2f 2a  cIdChar[] = {./*
31af5 20 78 30 20 78 31 20 78 32 20 78 33 20 78 34 20   x0 x1 x2 x3 x4 
31af6 78 35 20 78 36 20 78 37 20 78 38 20 78 39 20 78  x5 x6 x7 x8 x9 x
31af7 41 20 78 42 20 78 43 20 78 44 20 78 45 20 78 46  A xB xC xD xE xF
31af8 20 2a 2f 0a 20 20 20 20 30 2c 20 30 2c 20 31 2c   */.    0, 0, 1,
31af9 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
31afa 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30  1, 1, 0, 0, 0, 0
31afb 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 34 78 20 2a  , 0, 0,  /* 4x *
31afc 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31  /.    0, 1, 1, 1
31afd 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
31afe 20 31 2c 20 30 2c 20 31 2c 20 30 2c 20 30 2c 20   1, 0, 1, 0, 0, 
31aff 30 2c 20 30 2c 20 20 2f 2a 20 35 78 20 2a 2f 0a  0, 0,  /* 5x */.
31b00 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 31 2c 20      0, 0, 1, 1, 
31b01 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
31b02 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20 30 2c  , 0, 0, 0, 1, 0,
31b03 20 30 2c 20 20 2f 2a 20 36 78 20 2a 2f 0a 20 20   0,  /* 6x */.  
31b04 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    0, 1, 1, 1, 1,
31b05 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20   1, 1, 1, 1, 0, 
31b06 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
31b07 2c 20 20 2f 2a 20 37 78 20 2a 2f 0a 20 20 20 20  ,  /* 7x */.    
31b08 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 1, 1, 1, 1, 1
31b09 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
31b0a 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20   0, 1, 1, 1, 0, 
31b0b 20 2f 2a 20 38 78 20 2a 2f 0a 20 20 20 20 30 2c   /* 8x */.    0,
31b0c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
31b0d 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30  1, 1, 1, 1, 0, 0
31b0e 2c 20 31 2c 20 30 2c 20 31 2c 20 30 2c 20 20 2f  , 1, 0, 1, 0,  /
31b0f 2a 20 39 78 20 2a 2f 0a 20 20 20 20 31 2c 20 30  * 9x */.    1, 0
31b10 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
31b11 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20   1, 1, 1, 1, 0, 
31b12 31 2c 20 31 2c 20 31 2c 20 30 2c 20 20 2f 2a 20  1, 1, 1, 0,  /* 
31b13 41 78 20 2a 2f 0a 20 20 20 20 30 2c 20 30 2c 20  Ax */.    0, 0, 
31b14 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
31b15 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
31b16 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 42 78   0, 0, 0,  /* Bx
31b17 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c   */.    0, 1, 1,
31b18 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
31b19 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31  1, 1, 0, 1, 1, 1
31b1a 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 43 78 20 2a  , 1, 1,  /* Cx *
31b1b 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31  /.    0, 1, 1, 1
31b1c 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
31b1d 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 0, 1, 1, 1, 
31b1e 31 2c 20 31 2c 20 20 2f 2a 20 44 78 20 2a 2f 0a  1, 1,  /* Dx */.
31b1f 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 31 2c 20      0, 0, 1, 1, 
31b20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
31b21 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 0, 1, 1, 1, 1,
31b22 20 31 2c 20 20 2f 2a 20 45 78 20 2a 2f 0a 20 20   1,  /* Ex */.  
31b23 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    1, 1, 1, 1, 1,
31b24 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
31b25 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30  0, 1, 1, 1, 1, 0
31b26 2c 20 20 2f 2a 20 46 78 20 2a 2f 0a 7d 3b 0a 23  ,  /* Fx */.};.#
31b27 64 65 66 69 6e 65 20 49 64 43 68 61 72 28 43 29  define IdChar(C)
31b28 20 20 28 28 28 63 3d 43 29 3e 3d 30 78 34 32 20    (((c=C)>=0x42 
31b29 26 26 20 73 71 6c 69 74 65 33 49 73 45 62 63 64  && sqlite3IsEbcd
31b2a 69 63 49 64 43 68 61 72 5b 63 2d 30 78 34 30 5d  icIdChar[c-0x40]
31b2b 29 29 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a  )).#endif.../*.*
31b2c 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6c 65 6e  * Return the len
31b2d 67 74 68 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e  gth of the token
31b2e 20 74 68 61 74 20 62 65 67 69 6e 73 20 61 74 20   that begins at 
31b2f 7a 5b 30 5d 2e 20 0a 2a 2a 20 53 74 6f 72 65 20  z[0]. .** Store 
31b30 74 68 65 20 74 6f 6b 65 6e 20 74 79 70 65 20 69  the token type i
31b31 6e 20 2a 74 6f 6b 65 6e 54 79 70 65 20 62 65 66  n *tokenType bef
31b32 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a  ore returning..*
31b33 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
31b34 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54   int sqlite3GetT
31b35 6f 6b 65 6e 28 63 6f 6e 73 74 20 75 6e 73 69 67  oken(const unsig
31b36 6e 65 64 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  ned char *z, int
31b37 20 2a 74 6f 6b 65 6e 54 79 70 65 29 7b 0a 20 20   *tokenType){.  
31b38 69 6e 74 20 69 2c 20 63 3b 0a 20 20 73 77 69 74  int i, c;.  swit
31b39 63 68 28 20 2a 7a 20 29 7b 0a 20 20 20 20 63 61  ch( *z ){.    ca
31b3a 73 65 20 27 20 27 3a 20 63 61 73 65 20 27 5c 74  se ' ': case '\t
31b3b 27 3a 20 63 61 73 65 20 27 5c 6e 27 3a 20 63 61  ': case '\n': ca
31b3c 73 65 20 27 5c 66 27 3a 20 63 61 73 65 20 27 5c  se '\f': case '\
31b3d 72 27 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74  r': {.      test
31b3e 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 20 27 20  case( z[0]==' ' 
31b3f 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
31b40 65 28 20 7a 5b 30 5d 3d 3d 27 5c 74 27 20 29 3b  e( z[0]=='\t' );
31b41 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
31b42 20 7a 5b 30 5d 3d 3d 27 5c 6e 27 20 29 3b 0a 20   z[0]=='\n' );. 
31b43 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 7a       testcase( z
31b44 5b 30 5d 3d 3d 27 5c 66 27 20 29 3b 0a 20 20 20  [0]=='\f' );.   
31b45 20 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30     testcase( z[0
31b46 5d 3d 3d 27 5c 72 27 20 29 3b 0a 20 20 20 20 20  ]=='\r' );.     
31b47 20 66 6f 72 28 69 3d 31 3b 20 73 71 6c 69 74 65   for(i=1; sqlite
31b48 33 49 73 73 70 61 63 65 28 7a 5b 69 5d 29 3b 20  3Isspace(z[i]); 
31b49 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 2a 74 6f  i++){}.      *to
31b4a 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 53 50 41  kenType = TK_SPA
31b4b 43 45 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  CE;.      return
31b4c 20 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61   i;.    }.    ca
31b4d 73 65 20 27 2d 27 3a 20 7b 0a 20 20 20 20 20 20  se '-': {.      
31b4e 69 66 28 20 7a 5b 31 5d 3d 3d 27 2d 27 20 29 7b  if( z[1]=='-' ){
31b4f 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 32  .        for(i=2
31b50 3b 20 28 63 3d 7a 5b 69 5d 29 21 3d 30 20 26 26  ; (c=z[i])!=0 &&
31b51 20 63 21 3d 27 5c 6e 27 3b 20 69 2b 2b 29 7b 7d   c!='\n'; i++){}
31b52 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
31b53 79 70 65 20 3d 20 54 4b 5f 53 50 41 43 45 3b 0a  ype = TK_SPACE;.
31b54 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 69          return i
31b55 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
31b56 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
31b57 4d 49 4e 55 53 3b 0a 20 20 20 20 20 20 72 65 74  MINUS;.      ret
31b58 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
31b59 20 63 61 73 65 20 27 28 27 3a 20 7b 0a 20 20 20   case '(': {.   
31b5a 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
31b5b 54 4b 5f 4c 50 3b 0a 20 20 20 20 20 20 72 65 74  TK_LP;.      ret
31b5c 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
31b5d 20 63 61 73 65 20 27 29 27 3a 20 7b 0a 20 20 20   case ')': {.   
31b5e 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
31b5f 54 4b 5f 52 50 3b 0a 20 20 20 20 20 20 72 65 74  TK_RP;.      ret
31b60 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
31b61 20 63 61 73 65 20 27 3b 27 3a 20 7b 0a 20 20 20   case ';': {.   
31b62 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
31b63 54 4b 5f 53 45 4d 49 3b 0a 20 20 20 20 20 20 72  TK_SEMI;.      r
31b64 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
31b65 20 20 20 63 61 73 65 20 27 2b 27 3a 20 7b 0a 20     case '+': {. 
31b66 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
31b67 3d 20 54 4b 5f 50 4c 55 53 3b 0a 20 20 20 20 20  = TK_PLUS;.     
31b68 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
31b69 0a 20 20 20 20 63 61 73 65 20 27 2a 27 3a 20 7b  .    case '*': {
31b6a 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70  .      *tokenTyp
31b6b 65 20 3d 20 54 4b 5f 53 54 41 52 3b 0a 20 20 20  e = TK_STAR;.   
31b6c 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
31b6d 20 7d 0a 20 20 20 20 63 61 73 65 20 27 2f 27 3a   }.    case '/':
31b6e 20 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 31   {.      if( z[1
31b6f 5d 21 3d 27 2a 27 20 7c 7c 20 7a 5b 32 5d 3d 3d  ]!='*' || z[2]==
31b70 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f  0 ){.        *to
31b71 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 53 4c 41  kenType = TK_SLA
31b72 53 48 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  SH;.        retu
31b73 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 1;.      }.  
31b74 20 20 20 20 66 6f 72 28 69 3d 33 2c 20 63 3d 7a      for(i=3, c=z
31b75 5b 32 5d 3b 20 28 63 21 3d 27 2a 27 20 7c 7c 20  [2]; (c!='*' || 
31b76 7a 5b 69 5d 21 3d 27 2f 27 29 20 26 26 20 28 63  z[i]!='/') && (c
31b77 3d 7a 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b  =z[i])!=0; i++){
31b78 7d 0a 20 20 20 20 20 20 69 66 28 20 63 20 29 20  }.      if( c ) 
31b79 69 2b 2b 3b 0a 20 20 20 20 20 20 2a 74 6f 6b 65  i++;.      *toke
31b7a 6e 54 79 70 65 20 3d 20 54 4b 5f 53 50 41 43 45  nType = TK_SPACE
31b7b 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69  ;.      return i
31b7c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
31b7d 20 27 25 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74   '%': {.      *t
31b7e 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 52 45  okenType = TK_RE
31b7f 4d 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  M;.      return 
31b80 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  1;.    }.    cas
31b81 65 20 27 3d 27 3a 20 7b 0a 20 20 20 20 20 20 2a  e '=': {.      *
31b82 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 45  tokenType = TK_E
31b83 51 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  Q;.      return 
31b84 31 20 2b 20 28 7a 5b 31 5d 3d 3d 27 3d 27 29 3b  1 + (z[1]=='=');
31b85 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
31b86 27 3c 27 3a 20 7b 0a 20 20 20 20 20 20 69 66 28  '<': {.      if(
31b87 20 28 63 3d 7a 5b 31 5d 29 3d 3d 27 3d 27 20 29   (c=z[1])=='=' )
31b88 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  {.        *token
31b89 54 79 70 65 20 3d 20 54 4b 5f 4c 45 3b 0a 20 20  Type = TK_LE;.  
31b8a 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a        return 2;.
31b8b 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
31b8c 63 3d 3d 27 3e 27 20 29 7b 0a 20 20 20 20 20 20  c=='>' ){.      
31b8d 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
31b8e 4b 5f 4e 45 3b 0a 20 20 20 20 20 20 20 20 72 65  K_NE;.        re
31b8f 74 75 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d 65  turn 2;.      }e
31b90 6c 73 65 20 69 66 28 20 63 3d 3d 27 3c 27 20 29  lse if( c=='<' )
31b91 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  {.        *token
31b92 54 79 70 65 20 3d 20 54 4b 5f 4c 53 48 49 46 54  Type = TK_LSHIFT
31b93 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
31b94 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b   2;.      }else{
31b95 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
31b96 79 70 65 20 3d 20 54 4b 5f 4c 54 3b 0a 20 20 20  ype = TK_LT;.   
31b97 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
31b98 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
31b99 20 63 61 73 65 20 27 3e 27 3a 20 7b 0a 20 20 20   case '>': {.   
31b9a 20 20 20 69 66 28 20 28 63 3d 7a 5b 31 5d 29 3d     if( (c=z[1])=
31b9b 3d 27 3d 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='=' ){.        
31b9c 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
31b9d 47 45 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  GE;.        retu
31b9e 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73  rn 2;.      }els
31b9f 65 20 69 66 28 20 63 3d 3d 27 3e 27 20 29 7b 0a  e if( c=='>' ){.
31ba0 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79          *tokenTy
31ba1 70 65 20 3d 20 54 4b 5f 52 53 48 49 46 54 3b 0a  pe = TK_RSHIFT;.
31ba2 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 32          return 2
31ba3 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
31ba4 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70         *tokenTyp
31ba5 65 20 3d 20 54 4b 5f 47 54 3b 0a 20 20 20 20 20  e = TK_GT;.     
31ba6 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
31ba7 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 63     }.    }.    c
31ba8 61 73 65 20 27 21 27 3a 20 7b 0a 20 20 20 20 20  ase '!': {.     
31ba9 20 69 66 28 20 7a 5b 31 5d 21 3d 27 3d 27 20 29   if( z[1]!='=' )
31baa 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  {.        *token
31bab 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41  Type = TK_ILLEGA
31bac 4c 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  L;.        retur
31bad 6e 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  n 2;.      }else
31bae 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  {.        *token
31baf 54 79 70 65 20 3d 20 54 4b 5f 4e 45 3b 0a 20 20  Type = TK_NE;.  
31bb0 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a        return 2;.
31bb1 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
31bb2 20 20 63 61 73 65 20 27 7c 27 3a 20 7b 0a 20 20    case '|': {.  
31bb3 20 20 20 20 69 66 28 20 7a 5b 31 5d 21 3d 27 7c      if( z[1]!='|
31bb4 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f  ' ){.        *to
31bb5 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 42 49 54  kenType = TK_BIT
31bb6 4f 52 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  OR;.        retu
31bb7 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73  rn 1;.      }els
31bb8 65 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65  e{.        *toke
31bb9 6e 54 79 70 65 20 3d 20 54 4b 5f 43 4f 4e 43 41  nType = TK_CONCA
31bba 54 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  T;.        retur
31bbb 6e 20 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 2;.      }.   
31bbc 20 7d 0a 20 20 20 20 63 61 73 65 20 27 2c 27 3a   }.    case ',':
31bbd 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54   {.      *tokenT
31bbe 79 70 65 20 3d 20 54 4b 5f 43 4f 4d 4d 41 3b 0a  ype = TK_COMMA;.
31bbf 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
31bc0 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27      }.    case '
31bc1 26 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b  &': {.      *tok
31bc2 65 6e 54 79 70 65 20 3d 20 54 4b 5f 42 49 54 41  enType = TK_BITA
31bc3 4e 44 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  ND;.      return
31bc4 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61   1;.    }.    ca
31bc5 73 65 20 27 7e 27 3a 20 7b 0a 20 20 20 20 20 20  se '~': {.      
31bc6 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
31bc7 42 49 54 4e 4f 54 3b 0a 20 20 20 20 20 20 72 65  BITNOT;.      re
31bc8 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
31bc9 20 20 63 61 73 65 20 27 60 27 3a 0a 20 20 20 20    case '`':.    
31bca 63 61 73 65 20 27 5c 27 27 3a 0a 20 20 20 20 63  case '\'':.    c
31bcb 61 73 65 20 27 22 27 3a 20 7b 0a 20 20 20 20 20  ase '"': {.     
31bcc 20 69 6e 74 20 64 65 6c 69 6d 20 3d 20 7a 5b 30   int delim = z[0
31bcd 5d 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  ];.      testcas
31bce 65 28 20 64 65 6c 69 6d 3d 3d 27 60 27 20 29 3b  e( delim=='`' );
31bcf 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
31bd0 20 64 65 6c 69 6d 3d 3d 27 5c 27 27 20 29 3b 0a   delim=='\'' );.
31bd1 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
31bd2 64 65 6c 69 6d 3d 3d 27 22 27 20 29 3b 0a 20 20  delim=='"' );.  
31bd3 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 28 63 3d      for(i=1; (c=
31bd4 7a 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a  z[i])!=0; i++){.
31bd5 20 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d 64          if( c==d
31bd6 65 6c 69 6d 20 29 7b 0a 20 20 20 20 20 20 20 20  elim ){.        
31bd7 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 64 65    if( z[i+1]==de
31bd8 6c 69 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 20  lim ){.         
31bd9 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20     i++;.        
31bda 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
31bdb 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
31bdc 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
31bdd 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
31bde 69 66 28 20 63 3d 3d 27 5c 27 27 20 29 7b 0a 20  if( c=='\'' ){. 
31bdf 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70         *tokenTyp
31be0 65 20 3d 20 54 4b 5f 53 54 52 49 4e 47 3b 0a 20  e = TK_STRING;. 
31be1 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 2b         return i+
31be2 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  1;.      }else i
31be3 66 28 20 63 21 3d 30 20 29 7b 0a 20 20 20 20 20  f( c!=0 ){.     
31be4 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
31be5 54 4b 5f 49 44 3b 0a 20 20 20 20 20 20 20 20 72  TK_ID;.        r
31be6 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20 20 20 20  eturn i+1;.     
31be7 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
31be8 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
31be9 49 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20 20  ILLEGAL;.       
31bea 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 20   return i;.     
31beb 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73   }.    }.    cas
31bec 65 20 27 2e 27 3a 20 7b 0a 23 69 66 6e 64 65 66  e '.': {.#ifndef
31bed 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
31bee 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20  ATING_POINT.    
31bef 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49 73    if( !sqlite3Is
31bf0 64 69 67 69 74 28 7a 5b 31 5d 29 20 29 0a 23 65  digit(z[1]) ).#e
31bf1 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 20 20  ndif.      {.   
31bf2 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
31bf3 3d 20 54 4b 5f 44 4f 54 3b 0a 20 20 20 20 20 20  = TK_DOT;.      
31bf4 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
31bf5 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20    }.      /* If 
31bf6 74 68 65 20 6e 65 78 74 20 63 68 61 72 61 63 74  the next charact
31bf7 65 72 20 69 73 20 61 20 64 69 67 69 74 2c 20 74  er is a digit, t
31bf8 68 69 73 20 69 73 20 61 20 66 6c 6f 61 74 69 6e  his is a floatin
31bf9 67 20 70 6f 69 6e 74 0a 20 20 20 20 20 20 2a 2a  g point.      **
31bfa 20 6e 75 6d 62 65 72 20 74 68 61 74 20 62 65 67   number that beg
31bfb 69 6e 73 20 77 69 74 68 20 22 2e 22 2e 20 20 46  ins with ".".  F
31bfc 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20 74 68  all thru into th
31bfd 65 20 6e 65 78 74 20 63 61 73 65 20 2a 2f 0a 20  e next case */. 
31bfe 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 30     }.    case '0
31bff 27 3a 20 63 61 73 65 20 27 31 27 3a 20 63 61 73  ': case '1': cas
31c00 65 20 27 32 27 3a 20 63 61 73 65 20 27 33 27 3a  e '2': case '3':
31c01 20 63 61 73 65 20 27 34 27 3a 0a 20 20 20 20 63   case '4':.    c
31c02 61 73 65 20 27 35 27 3a 20 63 61 73 65 20 27 36  ase '5': case '6
31c03 27 3a 20 63 61 73 65 20 27 37 27 3a 20 63 61 73  ': case '7': cas
31c04 65 20 27 38 27 3a 20 63 61 73 65 20 27 39 27 3a  e '8': case '9':
31c05 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73   {.      testcas
31c06 65 28 20 7a 5b 30 5d 3d 3d 27 30 27 20 29 3b 20  e( z[0]=='0' ); 
31c07 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d   testcase( z[0]=
31c08 3d 27 31 27 20 29 3b 20 20 74 65 73 74 63 61 73  ='1' );  testcas
31c09 65 28 20 7a 5b 30 5d 3d 3d 27 32 27 20 29 3b 0a  e( z[0]=='2' );.
31c0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
31c0b 7a 5b 30 5d 3d 3d 27 33 27 20 29 3b 20 20 74 65  z[0]=='3' );  te
31c0c 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 34  stcase( z[0]=='4
31c0d 27 20 29 3b 20 20 74 65 73 74 63 61 73 65 28 20  ' );  testcase( 
31c0e 7a 5b 30 5d 3d 3d 27 35 27 20 29 3b 0a 20 20 20  z[0]=='5' );.   
31c0f 20 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30     testcase( z[0
31c10 5d 3d 3d 27 36 27 20 29 3b 20 20 74 65 73 74 63  ]=='6' );  testc
31c11 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 37 27 20 29  ase( z[0]=='7' )
31c12 3b 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30  ;  testcase( z[0
31c13 5d 3d 3d 27 38 27 20 29 3b 0a 20 20 20 20 20 20  ]=='8' );.      
31c14 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d  testcase( z[0]==
31c15 27 39 27 20 29 3b 0a 20 20 20 20 20 20 2a 74 6f  '9' );.      *to
31c16 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4e 54  kenType = TK_INT
31c17 45 47 45 52 3b 0a 20 20 20 20 20 20 66 6f 72 28  EGER;.      for(
31c18 69 3d 30 3b 20 73 71 6c 69 74 65 33 49 73 64 69  i=0; sqlite3Isdi
31c19 67 69 74 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b  git(z[i]); i++){
31c1a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
31c1b 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
31c1c 4f 49 4e 54 0a 20 20 20 20 20 20 69 66 28 20 7a  OINT.      if( z
31c1d 5b 69 5d 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20  [i]=='.' ){.    
31c1e 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20      i++;.       
31c1f 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49   while( sqlite3I
31c20 73 64 69 67 69 74 28 7a 5b 69 5d 29 20 29 7b 20  sdigit(z[i]) ){ 
31c21 69 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20 20 2a  i++; }.        *
31c22 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 46  tokenType = TK_F
31c23 4c 4f 41 54 3b 0a 20 20 20 20 20 20 7d 0a 20 20  LOAT;.      }.  
31c24 20 20 20 20 69 66 28 20 28 7a 5b 69 5d 3d 3d 27      if( (z[i]=='
31c25 65 27 20 7c 7c 20 7a 5b 69 5d 3d 3d 27 45 27 29  e' || z[i]=='E')
31c26 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 28   &&.           (
31c27 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28   sqlite3Isdigit(
31c28 7a 5b 69 2b 31 5d 29 20 0a 20 20 20 20 20 20 20  z[i+1]) .       
31c29 20 20 20 20 20 7c 7c 20 28 28 7a 5b 69 2b 31 5d       || ((z[i+1]
31c2a 3d 3d 27 2b 27 20 7c 7c 20 7a 5b 69 2b 31 5d 3d  =='+' || z[i+1]=
31c2b 3d 27 2d 27 29 20 26 26 20 73 71 6c 69 74 65 33  ='-') && sqlite3
31c2c 49 73 64 69 67 69 74 28 7a 5b 69 2b 32 5d 29 29  Isdigit(z[i+2]))
31c2d 0a 20 20 20 20 20 20 20 20 20 20 20 29 0a 20 20  .           ).  
31c2e 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 69      ){.        i
31c2f 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 77   += 2;.        w
31c30 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64  hile( sqlite3Isd
31c31 69 67 69 74 28 7a 5b 69 5d 29 20 29 7b 20 69 2b  igit(z[i]) ){ i+
31c32 2b 3b 20 7d 0a 20 20 20 20 20 20 20 20 2a 74 6f  +; }.        *to
31c33 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 46 4c 4f  kenType = TK_FLO
31c34 41 54 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64  AT;.      }.#end
31c35 69 66 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  if.      while( 
31c36 49 64 43 68 61 72 28 7a 5b 69 5d 29 20 29 7b 0a  IdChar(z[i]) ){.
31c37 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79          *tokenTy
31c38 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b  pe = TK_ILLEGAL;
31c39 0a 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20  .        i++;.  
31c3a 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75      }.      retu
31c3b 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn i;.    }.    
31c3c 63 61 73 65 20 27 5b 27 3a 20 7b 0a 20 20 20 20  case '[': {.    
31c3d 20 20 66 6f 72 28 69 3d 31 2c 20 63 3d 7a 5b 30    for(i=1, c=z[0
31c3e 5d 3b 20 63 21 3d 27 5d 27 20 26 26 20 28 63 3d  ]; c!=']' && (c=
31c3f 7a 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 7d  z[i])!=0; i++){}
31c40 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70  .      *tokenTyp
31c41 65 20 3d 20 63 3d 3d 27 5d 27 20 3f 20 54 4b 5f  e = c==']' ? TK_
31c42 49 44 20 3a 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b  ID : TK_ILLEGAL;
31c43 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b  .      return i;
31c44 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
31c45 27 3f 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f  '?': {.      *to
31c46 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 56 41 52  kenType = TK_VAR
31c47 49 41 42 4c 45 3b 0a 20 20 20 20 20 20 66 6f 72  IABLE;.      for
31c48 28 69 3d 31 3b 20 73 71 6c 69 74 65 33 49 73 64  (i=1; sqlite3Isd
31c49 69 67 69 74 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29  igit(z[i]); i++)
31c4a 7b 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  {}.      return 
31c4b 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  i;.    }.    cas
31c4c 65 20 27 23 27 3a 20 7b 0a 20 20 20 20 20 20 66  e '#': {.      f
31c4d 6f 72 28 69 3d 31 3b 20 73 71 6c 69 74 65 33 49  or(i=1; sqlite3I
31c4e 73 64 69 67 69 74 28 7a 5b 69 5d 29 3b 20 69 2b  sdigit(z[i]); i+
31c4f 2b 29 7b 7d 0a 20 20 20 20 20 20 69 66 28 20 69  +){}.      if( i
31c50 3e 31 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  >1 ){.        /*
31c51 20 50 61 72 61 6d 65 74 65 72 73 20 6f 66 20 74   Parameters of t
31c52 68 65 20 66 6f 72 6d 20 23 4e 4e 4e 20 28 77 68  he form #NNN (wh
31c53 65 72 65 20 4e 4e 4e 20 69 73 20 61 20 6e 75 6d  ere NNN is a num
31c54 62 65 72 29 20 61 72 65 20 75 73 65 64 0a 20 20  ber) are used.  
31c55 20 20 20 20 20 20 2a 2a 20 69 6e 74 65 72 6e 61        ** interna
31c56 6c 6c 79 20 62 79 20 73 71 6c 69 74 65 33 4e 65  lly by sqlite3Ne
31c57 73 74 65 64 50 61 72 73 65 2e 20 20 2a 2f 0a 20  stedParse.  */. 
31c58 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70         *tokenTyp
31c59 65 20 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b  e = TK_REGISTER;
31c5a 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
31c5b 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  i;.      }.     
31c5c 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 68   /* Fall through
31c5d 20 69 6e 74 6f 20 74 68 65 20 6e 65 78 74 20 63   into the next c
31c5e 61 73 65 20 69 66 20 74 68 65 20 27 23 27 20 69  ase if the '#' i
31c5f 73 20 6e 6f 74 20 66 6f 6c 6c 6f 77 65 64 20 62  s not followed b
31c60 79 0a 20 20 20 20 20 20 2a 2a 20 61 20 64 69 67  y.      ** a dig
31c61 69 74 2e 20 54 72 79 20 74 6f 20 6d 61 74 63 68  it. Try to match
31c62 20 23 41 41 41 41 20 77 68 65 72 65 20 41 41 41   #AAAA where AAA
31c63 41 20 69 73 20 61 20 70 61 72 61 6d 65 74 65 72  A is a parameter
31c64 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 7d 0a   name. */.    }.
31c65 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
31c66 4d 49 54 5f 54 43 4c 5f 56 41 52 49 41 42 4c 45  MIT_TCL_VARIABLE
31c67 0a 20 20 20 20 63 61 73 65 20 27 24 27 3a 0a 23  .    case '$':.#
31c68 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 27  endif.    case '
31c69 40 27 3a 20 20 2f 2a 20 46 6f 72 20 63 6f 6d 70  @':  /* For comp
31c6a 61 74 69 62 69 6c 69 74 79 20 77 69 74 68 20 4d  atibility with M
31c6b 53 20 53 51 4c 20 53 65 72 76 65 72 20 2a 2f 0a  S SQL Server */.
31c6c 20 20 20 20 63 61 73 65 20 27 3a 27 3a 20 7b 0a      case ':': {.
31c6d 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 30 3b        int n = 0;
31c6e 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
31c6f 20 7a 5b 30 5d 3d 3d 27 24 27 20 29 3b 20 20 74   z[0]=='$' );  t
31c70 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27  estcase( z[0]=='
31c71 40 27 20 29 3b 20 20 74 65 73 74 63 61 73 65 28  @' );  testcase(
31c72 20 7a 5b 30 5d 3d 3d 27 3a 27 20 29 3b 0a 20 20   z[0]==':' );.  
31c73 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
31c74 20 54 4b 5f 56 41 52 49 41 42 4c 45 3b 0a 20 20   TK_VARIABLE;.  
31c75 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 28 63 3d      for(i=1; (c=
31c76 7a 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a  z[i])!=0; i++){.
31c77 20 20 20 20 20 20 20 20 69 66 28 20 49 64 43 68          if( IdCh
31c78 61 72 28 63 29 20 29 7b 0a 20 20 20 20 20 20 20  ar(c) ){.       
31c79 20 20 20 6e 2b 2b 3b 0a 23 69 66 6e 64 65 66 20     n++;.#ifndef 
31c7a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 43 4c 5f  SQLITE_OMIT_TCL_
31c7b 56 41 52 49 41 42 4c 45 0a 20 20 20 20 20 20 20  VARIABLE.       
31c7c 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 27 28   }else if( c=='(
31c7d 27 20 26 26 20 6e 3e 30 20 29 7b 0a 20 20 20 20  ' && n>0 ){.    
31c7e 20 20 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20        do{.      
31c7f 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20        i++;.     
31c80 20 20 20 20 20 7d 77 68 69 6c 65 28 20 28 63 3d       }while( (c=
31c81 7a 5b 69 5d 29 21 3d 30 20 26 26 20 21 73 71 6c  z[i])!=0 && !sql
31c82 69 74 65 33 49 73 73 70 61 63 65 28 63 29 20 26  ite3Isspace(c) &
31c83 26 20 63 21 3d 27 29 27 20 29 3b 0a 20 20 20 20  & c!=')' );.    
31c84 20 20 20 20 20 20 69 66 28 20 63 3d 3d 27 29 27        if( c==')'
31c85 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
31c86 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  i++;.          }
31c87 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
31c88 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
31c89 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20  K_ILLEGAL;.     
31c8a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
31c8b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
31c8c 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 27 3a 27  }else if( c==':'
31c8d 20 26 26 20 7a 5b 69 2b 31 5d 3d 3d 27 3a 27 20   && z[i+1]==':' 
31c8e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 2b 2b  ){.          i++
31c8f 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20  ;.#endif.       
31c90 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
31c91 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
31c92 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
31c93 20 69 66 28 20 6e 3d 3d 30 20 29 20 2a 74 6f 6b   if( n==0 ) *tok
31c94 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45  enType = TK_ILLE
31c95 47 41 4c 3b 0a 20 20 20 20 20 20 72 65 74 75 72  GAL;.      retur
31c96 6e 20 69 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64  n i;.    }.#ifnd
31c97 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42  ef SQLITE_OMIT_B
31c98 4c 4f 42 5f 4c 49 54 45 52 41 4c 0a 20 20 20 20  LOB_LITERAL.    
31c99 63 61 73 65 20 27 78 27 3a 20 63 61 73 65 20 27  case 'x': case '
31c9a 58 27 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74  X': {.      test
31c9b 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 78 27 20  case( z[0]=='x' 
31c9c 29 3b 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30  ); testcase( z[0
31c9d 5d 3d 3d 27 58 27 20 29 3b 0a 20 20 20 20 20 20  ]=='X' );.      
31c9e 69 66 28 20 7a 5b 31 5d 3d 3d 27 5c 27 27 20 29  if( z[1]=='\'' )
31c9f 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  {.        *token
31ca0 54 79 70 65 20 3d 20 54 4b 5f 42 4c 4f 42 3b 0a  Type = TK_BLOB;.
31ca1 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 32 3b          for(i=2;
31ca2 20 28 63 3d 7a 5b 69 5d 29 21 3d 30 20 26 26 20   (c=z[i])!=0 && 
31ca3 63 21 3d 27 5c 27 27 3b 20 69 2b 2b 29 7b 0a 20  c!='\''; i++){. 
31ca4 20 20 20 20 20 20 20 20 20 69 66 28 20 21 73 71           if( !sq
31ca5 6c 69 74 65 33 49 73 78 64 69 67 69 74 28 63 29  lite3Isxdigit(c)
31ca6 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
31ca7 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
31ca8 49 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20 20  ILLEGAL;.       
31ca9 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
31caa 20 20 20 20 20 20 20 69 66 28 20 69 25 32 20 7c         if( i%2 |
31cab 7c 20 21 63 20 29 20 2a 74 6f 6b 65 6e 54 79 70  | !c ) *tokenTyp
31cac 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a  e = TK_ILLEGAL;.
31cad 20 20 20 20 20 20 20 20 69 66 28 20 63 20 29 20          if( c ) 
31cae 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 72 65 74  i++;.        ret
31caf 75 72 6e 20 69 3b 0a 20 20 20 20 20 20 7d 0a 20  urn i;.      }. 
31cb0 20 20 20 20 20 2f 2a 20 4f 74 68 65 72 77 69 73       /* Otherwis
31cb1 65 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74  e fall through t
31cb2 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73 65 20  o the next case 
31cb3 2a 2f 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  */.    }.#endif.
31cb4 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
31cb5 20 20 20 20 20 69 66 28 20 21 49 64 43 68 61 72       if( !IdChar
31cb6 28 2a 7a 29 20 29 7b 0a 20 20 20 20 20 20 20 20  (*z) ){.        
31cb7 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
31cb8 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 49 64       for(i=1; Id
31cb9 43 68 61 72 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29  Char(z[i]); i++)
31cba 7b 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  {}.      *tokenT
31cbb 79 70 65 20 3d 20 6b 65 79 77 6f 72 64 43 6f 64  ype = keywordCod
31cbc 65 28 28 63 68 61 72 2a 29 7a 2c 20 69 29 3b 0a  e((char*)z, i);.
31cbd 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a        return i;.
31cbe 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 74 6f 6b      }.  }.  *tok
31cbf 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45  enType = TK_ILLE
31cc0 47 41 4c 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b  GAL;.  return 1;
31cc1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 75 6e 20 74 68  .}../*.** Run th
31cc2 65 20 70 61 72 73 65 72 20 6f 6e 20 74 68 65 20  e parser on the 
31cc3 67 69 76 65 6e 20 53 51 4c 20 73 74 72 69 6e 67  given SQL string
31cc4 2e 20 20 54 68 65 20 70 61 72 73 65 72 20 73 74  .  The parser st
31cc5 72 75 63 74 75 72 65 20 69 73 0a 2a 2a 20 70 61  ructure is.** pa
31cc6 73 73 65 64 20 69 6e 2e 20 20 41 6e 20 53 51 4c  ssed in.  An SQL
31cc7 49 54 45 5f 20 73 74 61 74 75 73 20 63 6f 64 65  ITE_ status code
31cc8 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 49   is returned.  I
31cc9 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
31cca 73 0a 2a 2a 20 74 68 65 6e 20 61 6e 20 61 6e 64  s.** then an and
31ccb 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65   attempt is made
31ccc 20 74 6f 20 77 72 69 74 65 20 61 6e 20 65 72 72   to write an err
31ccd 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 74 6f 20  or message into 
31cce 0a 2a 2a 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69  .** memory obtai
31ccf 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
31cd0 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 74 6f  _malloc() and to
31cd1 20 6d 61 6b 65 20 2a 70 7a 45 72 72 4d 73 67 20   make *pzErrMsg 
31cd2 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 0a 2a 2a  point to that.**
31cd3 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a   error message..
31cd4 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
31cd5 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 75 6e  E int sqlite3Run
31cd6 50 61 72 73 65 72 28 50 61 72 73 65 20 2a 70 50  Parser(Parse *pP
31cd7 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72  arse, const char
31cd8 20 2a 7a 53 71 6c 2c 20 63 68 61 72 20 2a 2a 70   *zSql, char **p
31cd9 7a 45 72 72 4d 73 67 29 7b 0a 20 20 69 6e 74 20  zErrMsg){.  int 
31cda 6e 45 72 72 20 3d 20 30 3b 20 20 20 20 20 20 20  nErr = 0;       
31cdb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
31cdc 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20  umber of errors 
31cdd 65 6e 63 6f 75 6e 74 65 72 65 64 20 2a 2f 0a 20  encountered */. 
31cde 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
31cdf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31ce0 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
31ce1 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 45 6e 67   */.  void *pEng
31ce2 69 6e 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ine;            
31ce3 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4c 45 4d        /* The LEM
31ce4 4f 4e 2d 67 65 6e 65 72 61 74 65 64 20 4c 41 4c  ON-generated LAL
31ce5 52 28 31 29 20 70 61 72 73 65 72 20 2a 2f 0a 20  R(1) parser */. 
31ce6 20 69 6e 74 20 74 6f 6b 65 6e 54 79 70 65 3b 20   int tokenType; 
31ce7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31ce8 20 2f 2a 20 74 79 70 65 20 6f 66 20 74 68 65 20   /* type of the 
31ce9 6e 65 78 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20  next token */.  
31cea 69 6e 74 20 6c 61 73 74 54 6f 6b 65 6e 50 61 72  int lastTokenPar
31ceb 73 65 64 20 3d 20 2d 31 3b 20 20 20 20 20 20 20  sed = -1;       
31cec 2f 2a 20 74 79 70 65 20 6f 66 20 74 68 65 20 70  /* type of the p
31ced 72 65 76 69 6f 75 73 20 74 6f 6b 65 6e 20 2a 2f  revious token */
31cee 0a 20 20 75 38 20 65 6e 61 62 6c 65 4c 6f 6f 6b  .  u8 enableLook
31cef 61 73 69 64 65 3b 20 20 20 20 20 20 20 20 20 20  aside;          
31cf0 20 20 20 2f 2a 20 53 61 76 65 64 20 76 61 6c 75     /* Saved valu
31cf1 65 20 6f 66 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69  e of db->lookasi
31cf2 64 65 2e 62 45 6e 61 62 6c 65 64 20 2a 2f 0a 20  de.bEnabled */. 
31cf3 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
31cf4 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 20  Parse->db;      
31cf5 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
31cf6 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
31cf7 20 69 6e 74 20 6d 78 53 71 6c 4c 65 6e 3b 20 20   int mxSqlLen;  
31cf8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31cf9 20 2f 2a 20 4d 61 78 20 6c 65 6e 67 74 68 20 6f   /* Max length o
31cfa 66 20 61 6e 20 53 51 4c 20 73 74 72 69 6e 67 20  f an SQL string 
31cfb 2a 2f 0a 0a 0a 20 20 6d 78 53 71 6c 4c 65 6e 20  */...  mxSqlLen 
31cfc 3d 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  = db->aLimit[SQL
31cfd 49 54 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45  ITE_LIMIT_SQL_LE
31cfe 4e 47 54 48 5d 3b 0a 20 20 69 66 28 20 64 62 2d  NGTH];.  if( db-
31cff 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3d 3d  >activeVdbeCnt==
31d00 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e 75 31 2e  0 ){.    db->u1.
31d01 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 3d 20  isInterrupted = 
31d02 30 3b 0a 20 20 7d 0a 20 20 70 50 61 72 73 65 2d  0;.  }.  pParse-
31d03 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b  >rc = SQLITE_OK;
31d04 0a 20 20 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c  .  pParse->zTail
31d05 20 3d 20 7a 53 71 6c 3b 0a 20 20 69 20 3d 20 30   = zSql;.  i = 0
31d06 3b 0a 20 20 61 73 73 65 72 74 28 20 70 7a 45 72  ;.  assert( pzEr
31d07 72 4d 73 67 21 3d 30 20 29 3b 0a 20 20 70 45 6e  rMsg!=0 );.  pEn
31d08 67 69 6e 65 20 3d 20 73 71 6c 69 74 65 33 50 61  gine = sqlite3Pa
31d09 72 73 65 72 41 6c 6c 6f 63 28 28 76 6f 69 64 2a  rserAlloc((void*
31d0a 28 2a 29 28 73 69 7a 65 5f 74 29 29 73 71 6c 69  (*)(size_t))sqli
31d0b 74 65 33 4d 61 6c 6c 6f 63 29 3b 0a 20 20 69 66  te3Malloc);.  if
31d0c 28 20 70 45 6e 67 69 6e 65 3d 3d 30 20 29 7b 0a  ( pEngine==0 ){.
31d0d 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
31d0e 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65  iled = 1;.    re
31d0f 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
31d10 4d 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  M;.  }.  assert(
31d11 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
31d12 6c 65 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  le==0 );.  asser
31d13 74 28 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  t( pParse->pNewT
31d14 72 69 67 67 65 72 3d 3d 30 20 29 3b 0a 20 20 61  rigger==0 );.  a
31d15 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 6e  ssert( pParse->n
31d16 56 61 72 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  Var==0 );.  asse
31d17 72 74 28 20 70 50 61 72 73 65 2d 3e 6e 56 61 72  rt( pParse->nVar
31d18 45 78 70 72 3d 3d 30 20 29 3b 0a 20 20 61 73 73  Expr==0 );.  ass
31d19 65 72 74 28 20 70 50 61 72 73 65 2d 3e 6e 56 61  ert( pParse->nVa
31d1a 72 45 78 70 72 41 6c 6c 6f 63 3d 3d 30 20 29 3b  rExprAlloc==0 );
31d1b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73  .  assert( pPars
31d1c 65 2d 3e 61 70 56 61 72 45 78 70 72 3d 3d 30 20  e->apVarExpr==0 
31d1d 29 3b 0a 20 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61  );.  enableLooka
31d1e 73 69 64 65 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61  side = db->looka
31d1f 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 3b 0a 20  side.bEnabled;. 
31d20 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69   if( db->lookasi
31d21 64 65 2e 70 53 74 61 72 74 20 29 20 64 62 2d 3e  de.pStart ) db->
31d22 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c  lookaside.bEnabl
31d23 65 64 20 3d 20 31 3b 0a 20 20 77 68 69 6c 65 28  ed = 1;.  while(
31d24 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
31d25 65 64 20 26 26 20 7a 53 71 6c 5b 69 5d 21 3d 30  ed && zSql[i]!=0
31d26 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
31d27 69 3e 3d 30 20 29 3b 0a 20 20 20 20 70 50 61 72  i>=0 );.    pPar
31d28 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e 7a  se->sLastToken.z
31d29 20 3d 20 26 7a 53 71 6c 5b 69 5d 3b 0a 20 20 20   = &zSql[i];.   
31d2a 20 70 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f   pParse->sLastTo
31d2b 6b 65 6e 2e 6e 20 3d 20 73 71 6c 69 74 65 33 47  ken.n = sqlite3G
31d2c 65 74 54 6f 6b 65 6e 28 28 75 6e 73 69 67 6e 65  etToken((unsigne
31d2d 64 20 63 68 61 72 2a 29 26 7a 53 71 6c 5b 69 5d  d char*)&zSql[i]
31d2e 2c 26 74 6f 6b 65 6e 54 79 70 65 29 3b 0a 20 20  ,&tokenType);.  
31d2f 20 20 69 20 2b 3d 20 70 50 61 72 73 65 2d 3e 73    i += pParse->s
31d30 4c 61 73 74 54 6f 6b 65 6e 2e 6e 3b 0a 20 20 20  LastToken.n;.   
31d31 20 69 66 28 20 69 3e 6d 78 53 71 6c 4c 65 6e 20   if( i>mxSqlLen 
31d32 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  ){.      pParse-
31d33 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 54 4f 4f  >rc = SQLITE_TOO
31d34 42 49 47 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  BIG;.      break
31d35 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 77 69 74  ;.    }.    swit
31d36 63 68 28 20 74 6f 6b 65 6e 54 79 70 65 20 29 7b  ch( tokenType ){
31d37 0a 20 20 20 20 20 20 63 61 73 65 20 54 4b 5f 53  .      case TK_S
31d38 50 41 43 45 3a 20 7b 0a 20 20 20 20 20 20 20 20  PACE: {.        
31d39 69 66 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74  if( db->u1.isInt
31d3a 65 72 72 75 70 74 65 64 20 29 7b 0a 20 20 20 20  errupted ){.    
31d3b 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
31d3c 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 69  orMsg(pParse, "i
31d3d 6e 74 65 72 72 75 70 74 22 29 3b 0a 20 20 20 20  nterrupt");.    
31d3e 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63        pParse->rc
31d3f 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52   = SQLITE_INTERR
31d40 55 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 67  UPT;.          g
31d41 6f 74 6f 20 61 62 6f 72 74 5f 70 61 72 73 65 3b  oto abort_parse;
31d42 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
31d43 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
31d44 7d 0a 20 20 20 20 20 20 63 61 73 65 20 54 4b 5f  }.      case TK_
31d45 49 4c 4c 45 47 41 4c 3a 20 7b 0a 20 20 20 20 20  ILLEGAL: {.     
31d46 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
31d47 28 64 62 2c 20 2a 70 7a 45 72 72 4d 73 67 29 3b  (db, *pzErrMsg);
31d48 0a 20 20 20 20 20 20 20 20 2a 70 7a 45 72 72 4d  .        *pzErrM
31d49 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  sg = sqlite3MPri
31d4a 6e 74 66 28 64 62 2c 20 22 75 6e 72 65 63 6f 67  ntf(db, "unrecog
31d4b 6e 69 7a 65 64 20 74 6f 6b 65 6e 3a 20 5c 22 25  nized token: \"%
31d4c 54 5c 22 22 2c 0a 20 20 20 20 20 20 20 20 20 20  T\"",.          
31d4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 70                &p
31d4e 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65  Parse->sLastToke
31d4f 6e 29 3b 0a 20 20 20 20 20 20 20 20 6e 45 72 72  n);.        nErr
31d50 2b 2b 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f  ++;.        goto
31d51 20 61 62 6f 72 74 5f 70 61 72 73 65 3b 0a 20 20   abort_parse;.  
31d52 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65      }.      case
31d53 20 54 4b 5f 53 45 4d 49 3a 20 7b 0a 20 20 20 20   TK_SEMI: {.    
31d54 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 54 61 69      pParse->zTai
31d55 6c 20 3d 20 26 7a 53 71 6c 5b 69 5d 3b 0a 20 20  l = &zSql[i];.  
31d56 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c 20 74 68        /* Fall th
31d57 72 75 20 69 6e 74 6f 20 74 68 65 20 64 65 66 61  ru into the defa
31d58 75 6c 74 20 63 61 73 65 20 2a 2f 0a 20 20 20 20  ult case */.    
31d59 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61 75 6c    }.      defaul
31d5a 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  t: {.        sql
31d5b 69 74 65 33 50 61 72 73 65 72 28 70 45 6e 67 69  ite3Parser(pEngi
31d5c 6e 65 2c 20 74 6f 6b 65 6e 54 79 70 65 2c 20 70  ne, tokenType, p
31d5d 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65  Parse->sLastToke
31d5e 6e 2c 20 70 50 61 72 73 65 29 3b 0a 20 20 20 20  n, pParse);.    
31d5f 20 20 20 20 6c 61 73 74 54 6f 6b 65 6e 50 61 72      lastTokenPar
31d60 73 65 64 20 3d 20 74 6f 6b 65 6e 54 79 70 65 3b  sed = tokenType;
31d61 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61  .        if( pPa
31d62 72 73 65 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f  rse->rc!=SQLITE_
31d63 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
31d64 67 6f 74 6f 20 61 62 6f 72 74 5f 70 61 72 73 65  goto abort_parse
31d65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
31d66 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
31d67 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 61 62 6f   }.    }.  }.abo
31d68 72 74 5f 70 61 72 73 65 3a 0a 20 20 69 66 28 20  rt_parse:.  if( 
31d69 7a 53 71 6c 5b 69 5d 3d 3d 30 20 26 26 20 6e 45  zSql[i]==0 && nE
31d6a 72 72 3d 3d 30 20 26 26 20 70 50 61 72 73 65 2d  rr==0 && pParse-
31d6b 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  >rc==SQLITE_OK )
31d6c 7b 0a 20 20 20 20 69 66 28 20 6c 61 73 74 54 6f  {.    if( lastTo
31d6d 6b 65 6e 50 61 72 73 65 64 21 3d 54 4b 5f 53 45  kenParsed!=TK_SE
31d6e 4d 49 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  MI ){.      sqli
31d6f 74 65 33 50 61 72 73 65 72 28 70 45 6e 67 69 6e  te3Parser(pEngin
31d70 65 2c 20 54 4b 5f 53 45 4d 49 2c 20 70 50 61 72  e, TK_SEMI, pPar
31d71 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2c 20  se->sLastToken, 
31d72 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 70  pParse);.      p
31d73 50 61 72 73 65 2d 3e 7a 54 61 69 6c 20 3d 20 26  Parse->zTail = &
31d74 7a 53 71 6c 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20  zSql[i];.    }. 
31d75 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72     sqlite3Parser
31d76 28 70 45 6e 67 69 6e 65 2c 20 30 2c 20 70 50 61  (pEngine, 0, pPa
31d77 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2c  rse->sLastToken,
31d78 20 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a 23 69   pParse);.  }.#i
31d79 66 64 65 66 20 59 59 54 52 41 43 4b 4d 41 58 53  fdef YYTRACKMAXS
31d7a 54 41 43 4b 44 45 50 54 48 0a 20 20 73 71 6c 69  TACKDEPTH.  sqli
31d7b 74 65 33 53 74 61 74 75 73 53 65 74 28 53 51 4c  te3StatusSet(SQL
31d7c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 52 53 45  ITE_STATUS_PARSE
31d7d 52 5f 53 54 41 43 4b 2c 0a 20 20 20 20 20 20 73  R_STACK,.      s
31d7e 71 6c 69 74 65 33 50 61 72 73 65 72 53 74 61 63  qlite3ParserStac
31d7f 6b 50 65 61 6b 28 70 45 6e 67 69 6e 65 29 0a 20  kPeak(pEngine). 
31d80 20 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 59 59   );.#endif /* YY
31d81 44 45 42 55 47 20 2a 2f 0a 20 20 73 71 6c 69 74  DEBUG */.  sqlit
31d82 65 33 50 61 72 73 65 72 46 72 65 65 28 70 45 6e  e3ParserFree(pEn
31d83 67 69 6e 65 2c 20 73 71 6c 69 74 65 33 5f 66 72  gine, sqlite3_fr
31d84 65 65 29 3b 0a 20 20 64 62 2d 3e 6c 6f 6f 6b 61  ee);.  db->looka
31d85 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 3d 20  side.bEnabled = 
31d86 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69 64 65 3b  enableLookaside;
31d87 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
31d88 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 70  cFailed ){.    p
31d89 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49  Parse->rc = SQLI
31d8a 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
31d8b 69 66 28 20 70 50 61 72 73 65 2d 3e 72 63 21 3d  if( pParse->rc!=
31d8c 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61  SQLITE_OK && pPa
31d8d 72 73 65 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f  rse->rc!=SQLITE_
31d8e 44 4f 4e 45 20 26 26 20 70 50 61 72 73 65 2d 3e  DONE && pParse->
31d8f 7a 45 72 72 4d 73 67 3d 3d 30 20 29 7b 0a 20 20  zErrMsg==0 ){.  
31d90 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
31d91 6e 67 28 26 70 50 61 72 73 65 2d 3e 7a 45 72 72  ng(&pParse->zErr
31d92 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73  Msg, db, "%s", s
31d93 71 6c 69 74 65 33 45 72 72 53 74 72 28 70 50 61  qlite3ErrStr(pPa
31d94 72 73 65 2d 3e 72 63 29 29 3b 0a 20 20 7d 0a 20  rse->rc));.  }. 
31d95 20 61 73 73 65 72 74 28 20 70 7a 45 72 72 4d 73   assert( pzErrMs
31d96 67 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 50  g!=0 );.  if( pP
31d97 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 29 7b  arse->zErrMsg ){
31d98 0a 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d  .    *pzErrMsg =
31d99 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67   pParse->zErrMsg
31d9a 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 45  ;.    pParse->zE
31d9b 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 6e  rrMsg = 0;.    n
31d9c 45 72 72 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 28  Err++;.  }.  if(
31d9d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20 26   pParse->pVdbe &
31d9e 26 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3e 30  & pParse->nErr>0
31d9f 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 65 73 74   && pParse->nest
31da0 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  ed==0 ){.    sql
31da1 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 28 70  ite3VdbeDelete(p
31da2 50 61 72 73 65 2d 3e 70 56 64 62 65 29 3b 0a 20  Parse->pVdbe);. 
31da3 20 20 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65     pParse->pVdbe
31da4 20 3d 20 30 3b 0a 20 20 7d 0a 23 69 66 6e 64 65   = 0;.  }.#ifnde
31da5 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
31da6 41 52 45 44 5f 43 41 43 48 45 0a 20 20 69 66 28  ARED_CACHE.  if(
31da7 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d   pParse->nested=
31da8 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
31da9 33 44 62 46 72 65 65 28 64 62 2c 20 70 50 61 72  3DbFree(db, pPar
31daa 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 29 3b  se->aTableLock);
31dab 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 61 54 61  .    pParse->aTa
31dac 62 6c 65 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20  bleLock = 0;.   
31dad 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c   pParse->nTableL
31dae 6f 63 6b 20 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e  ock = 0;.  }.#en
31daf 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  dif.#ifndef SQLI
31db0 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
31db1 41 42 4c 45 0a 20 20 73 71 6c 69 74 65 33 44 62  ABLE.  sqlite3Db
31db2 46 72 65 65 28 64 62 2c 20 70 50 61 72 73 65 2d  Free(db, pParse-
31db3 3e 61 70 56 74 61 62 4c 6f 63 6b 29 3b 0a 23 65  >apVtabLock);.#e
31db4 6e 64 69 66 0a 0a 20 20 69 66 28 20 21 49 4e 5f  ndif..  if( !IN_
31db5 44 45 43 4c 41 52 45 5f 56 54 41 42 20 29 7b 0a  DECLARE_VTAB ){.
31db6 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 70 50      /* If the pP
31db7 61 72 73 65 2d 3e 64 65 63 6c 61 72 65 56 74 61  arse->declareVta
31db8 62 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 64  b flag is set, d
31db9 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 61 6e 79  o not delete any
31dba 20 74 61 62 6c 65 20 0a 20 20 20 20 2a 2a 20 73   table .    ** s
31dbb 74 72 75 63 74 75 72 65 20 62 75 69 6c 74 20 75  tructure built u
31dbc 70 20 69 6e 20 70 50 61 72 73 65 2d 3e 70 4e 65  p in pParse->pNe
31dbd 77 54 61 62 6c 65 2e 20 54 68 65 20 63 61 6c 6c  wTable. The call
31dbe 69 6e 67 20 63 6f 64 65 20 28 73 65 65 20 76 74  ing code (see vt
31dbf 61 62 2e 63 29 0a 20 20 20 20 2a 2a 20 77 69 6c  ab.c).    ** wil
31dc0 6c 20 74 61 6b 65 20 72 65 73 70 6f 6e 73 69 62  l take responsib
31dc1 69 6c 69 74 79 20 66 6f 72 20 66 72 65 65 69 6e  ility for freein
31dc2 67 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75  g the Table stru
31dc3 63 74 75 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  cture..    */.  
31dc4 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54    sqlite3DeleteT
31dc5 61 62 6c 65 28 70 50 61 72 73 65 2d 3e 70 4e 65  able(pParse->pNe
31dc6 77 54 61 62 6c 65 29 3b 0a 20 20 7d 0a 0a 20 20  wTable);.  }..  
31dc7 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69  sqlite3DeleteTri
31dc8 67 67 65 72 28 64 62 2c 20 70 50 61 72 73 65 2d  gger(db, pParse-
31dc9 3e 70 4e 65 77 54 72 69 67 67 65 72 29 3b 0a 20  >pNewTrigger);. 
31dca 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
31dcb 62 2c 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72  b, pParse->apVar
31dcc 45 78 70 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  Expr);.  sqlite3
31dcd 44 62 46 72 65 65 28 64 62 2c 20 70 50 61 72 73  DbFree(db, pPars
31dce 65 2d 3e 61 41 6c 69 61 73 29 3b 0a 20 20 77 68  e->aAlias);.  wh
31dcf 69 6c 65 28 20 70 50 61 72 73 65 2d 3e 70 41 69  ile( pParse->pAi
31dd0 6e 63 20 29 7b 0a 20 20 20 20 41 75 74 6f 69 6e  nc ){.    Autoin
31dd1 63 49 6e 66 6f 20 2a 70 20 3d 20 70 50 61 72 73  cInfo *p = pPars
31dd2 65 2d 3e 70 41 69 6e 63 3b 0a 20 20 20 20 70 50  e->pAinc;.    pP
31dd3 61 72 73 65 2d 3e 70 41 69 6e 63 20 3d 20 70 2d  arse->pAinc = p-
31dd4 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c 69  >pNext;.    sqli
31dd5 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 29  te3DbFree(db, p)
31dd6 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 70  ;.  }.  while( p
31dd7 50 61 72 73 65 2d 3e 70 5a 6f 6d 62 69 65 54 61  Parse->pZombieTa
31dd8 62 20 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a  b ){.    Table *
31dd9 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 5a 6f 6d  p = pParse->pZom
31dda 62 69 65 54 61 62 3b 0a 20 20 20 20 70 50 61 72  bieTab;.    pPar
31ddb 73 65 2d 3e 70 5a 6f 6d 62 69 65 54 61 62 20 3d  se->pZombieTab =
31ddc 20 70 2d 3e 70 4e 65 78 74 5a 6f 6d 62 69 65 3b   p->pNextZombie;
31ddd 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65  .    sqlite3Dele
31dde 74 65 54 61 62 6c 65 28 70 29 3b 0a 20 20 7d 0a  teTable(p);.  }.
31ddf 20 20 69 66 28 20 6e 45 72 72 3e 30 20 26 26 20    if( nErr>0 && 
31de0 70 50 61 72 73 65 2d 3e 72 63 3d 3d 53 51 4c 49  pParse->rc==SQLI
31de1 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61  TE_OK ){.    pPa
31de2 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45  rse->rc = SQLITE
31de3 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 65  _ERROR;.  }.  re
31de4 74 75 72 6e 20 6e 45 72 72 3b 0a 7d 0a 0a 2f 2a  turn nErr;.}../*
31de5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
31de6 64 20 6f 66 20 74 6f 6b 65 6e 69 7a 65 2e 63 20  d of tokenize.c 
31de7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31de8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31de9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
31dea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
31deb 67 69 6e 20 66 69 6c 65 20 63 6f 6d 70 6c 65 74  gin file complet
31dec 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
31ded 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31dee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
31def 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
31df0 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 15.**.** The 
31df1 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
31df2 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
31df3 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
31df4 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
31df5 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
31df6 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
31df7 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
31df8 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
31df9 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
31dfa 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
31dfb 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
31dfc 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
31dfd 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
31dfe 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
31dff 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
31e00 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
31e01 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
31e02 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31e03 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31e04 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31e05 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31e06 2a 2a 2a 2a 0a 2a 2a 20 41 6e 20 74 6f 6b 65 6e  ****.** An token
31e07 69 7a 65 72 20 66 6f 72 20 53 51 4c 0a 2a 2a 0a  izer for SQL.**.
31e08 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
31e09 74 61 69 6e 73 20 43 20 63 6f 64 65 20 74 68 61  tains C code tha
31e0a 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  t implements the
31e0b 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74   sqlite3_complet
31e0c 65 28 29 20 41 50 49 2e 0a 2a 2a 20 54 68 69 73  e() API..** This
31e0d 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20 62 65   code used to be
31e0e 20 70 61 72 74 20 6f 66 20 74 68 65 20 74 6f 6b   part of the tok
31e0f 65 6e 69 7a 65 72 2e 63 20 73 6f 75 72 63 65 20  enizer.c source 
31e10 66 69 6c 65 2e 20 20 42 75 74 20 62 79 0a 2a 2a  file.  But by.**
31e11 20 73 65 70 61 72 61 74 69 6e 67 20 69 74 20 6f   separating it o
31e12 75 74 2c 20 74 68 65 20 63 6f 64 65 20 77 69 6c  ut, the code wil
31e13 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c  l be automatical
31e14 6c 79 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 0a  ly omitted from.
31e15 2a 2a 20 73 74 61 74 69 63 20 6c 69 6e 6b 73 20  ** static links 
31e16 74 68 61 74 20 64 6f 20 6e 6f 74 20 75 73 65 20  that do not use 
31e17 69 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 63  it..**.** $Id: c
31e18 6f 6d 70 6c 65 74 65 2e 63 2c 76 20 31 2e 38 20  omplete.c,v 1.8 
31e19 32 30 30 39 2f 30 34 2f 32 38 20 30 34 3a 34 36  2009/04/28 04:46
31e1a 3a 34 32 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :42 drh Exp $.*/
31e1b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
31e1c 4f 4d 49 54 5f 43 4f 4d 50 4c 45 54 45 0a 0a 2f  OMIT_COMPLETE../
31e1d 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 64 65 66  *.** This is def
31e1e 69 6e 65 64 20 69 6e 20 74 6f 6b 65 6e 69 7a 65  ined in tokenize
31e1f 2e 63 2e 20 20 57 65 20 6a 75 73 74 20 68 61 76  .c.  We just hav
31e20 65 20 74 6f 20 69 6d 70 6f 72 74 20 74 68 65 20  e to import the 
31e21 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 23  definition..*/.#
31e22 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 4d  ifndef SQLITE_AM
31e23 41 4c 47 41 4d 41 54 49 4f 4e 0a 23 69 66 64 65  ALGAMATION.#ifde
31e24 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 53  f SQLITE_ASCII.S
31e25 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
31e26 6e 73 74 20 63 68 61 72 20 73 71 6c 69 74 65 33  nst char sqlite3
31e27 49 73 41 73 63 69 69 49 64 43 68 61 72 5b 5d 3b  IsAsciiIdChar[];
31e28 0a 23 64 65 66 69 6e 65 20 49 64 43 68 61 72 28  .#define IdChar(
31e29 43 29 20 20 28 28 28 63 3d 43 29 26 30 78 38 30  C)  (((c=C)&0x80
31e2a 29 21 3d 30 20 7c 7c 20 28 63 3e 30 78 31 66 20  )!=0 || (c>0x1f 
31e2b 26 26 20 73 71 6c 69 74 65 33 49 73 41 73 63 69  && sqlite3IsAsci
31e2c 69 49 64 43 68 61 72 5b 63 2d 30 78 32 30 5d 29  iIdChar[c-0x20])
31e2d 29 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  ).#endif.#ifdef 
31e2e 53 51 4c 49 54 45 5f 45 42 43 44 49 43 0a 53 51  SQLITE_EBCDIC.SQ
31e2f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
31e30 73 74 20 63 68 61 72 20 73 71 6c 69 74 65 33 49  st char sqlite3I
31e31 73 45 62 63 64 69 63 49 64 43 68 61 72 5b 5d 3b  sEbcdicIdChar[];
31e32 0a 23 64 65 66 69 6e 65 20 49 64 43 68 61 72 28  .#define IdChar(
31e33 43 29 20 20 28 28 28 63 3d 43 29 3e 3d 30 78 34  C)  (((c=C)>=0x4
31e34 32 20 26 26 20 73 71 6c 69 74 65 33 49 73 45 62  2 && sqlite3IsEb
31e35 63 64 69 63 49 64 43 68 61 72 5b 63 2d 30 78 34  cdicIdChar[c-0x4
31e36 30 5d 29 29 0a 23 65 6e 64 69 66 0a 23 65 6e 64  0])).#endif.#end
31e37 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 41 4d 41  if /* SQLITE_AMA
31e38 4c 47 41 4d 41 54 49 4f 4e 20 2a 2f 0a 0a 0a 2f  LGAMATION */.../
31e39 2a 0a 2a 2a 20 54 6f 6b 65 6e 20 74 79 70 65 73  *.** Token types
31e3a 20 75 73 65 64 20 62 79 20 74 68 65 20 73 71 6c   used by the sql
31e3b 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 29 20  ite3_complete() 
31e3c 72 6f 75 74 69 6e 65 2e 20 20 53 65 65 20 74 68  routine.  See th
31e3d 65 20 68 65 61 64 65 72 0a 2a 2a 20 63 6f 6d 6d  e header.** comm
31e3e 65 6e 74 73 20 6f 6e 20 74 68 61 74 20 70 72 6f  ents on that pro
31e3f 63 65 64 75 72 65 20 66 6f 72 20 61 64 64 69 74  cedure for addit
31e40 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
31e41 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 74 6b  n..*/.#define tk
31e42 53 45 4d 49 20 20 20 20 30 0a 23 64 65 66 69 6e  SEMI    0.#defin
31e43 65 20 74 6b 57 53 20 20 20 20 20 20 31 0a 23 64  e tkWS      1.#d
31e44 65 66 69 6e 65 20 74 6b 4f 54 48 45 52 20 20 20  efine tkOTHER   
31e45 32 0a 23 64 65 66 69 6e 65 20 74 6b 45 58 50 4c  2.#define tkEXPL
31e46 41 49 4e 20 33 0a 23 64 65 66 69 6e 65 20 74 6b  AIN 3.#define tk
31e47 43 52 45 41 54 45 20 20 34 0a 23 64 65 66 69 6e  CREATE  4.#defin
31e48 65 20 74 6b 54 45 4d 50 20 20 20 20 35 0a 23 64  e tkTEMP    5.#d
31e49 65 66 69 6e 65 20 74 6b 54 52 49 47 47 45 52 20  efine tkTRIGGER 
31e4a 36 0a 23 64 65 66 69 6e 65 20 74 6b 45 4e 44 20  6.#define tkEND 
31e4b 20 20 20 20 37 0a 0a 2f 2a 0a 2a 2a 20 52 65 74      7../*.** Ret
31e4c 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20  urn TRUE if the 
31e4d 67 69 76 65 6e 20 53 51 4c 20 73 74 72 69 6e 67  given SQL string
31e4e 20 65 6e 64 73 20 69 6e 20 61 20 73 65 6d 69 63   ends in a semic
31e4f 6f 6c 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 70 65 63  olon..**.** Spec
31e50 69 61 6c 20 68 61 6e 64 6c 69 6e 67 20 69 73 20  ial handling is 
31e51 72 65 71 75 69 72 65 20 66 6f 72 20 43 52 45 41  require for CREA
31e52 54 45 20 54 52 49 47 47 45 52 20 73 74 61 74 65  TE TRIGGER state
31e53 6d 65 6e 74 73 2e 0a 2a 2a 20 57 68 65 6e 65 76  ments..** Whenev
31e54 65 72 20 74 68 65 20 43 52 45 41 54 45 20 54 52  er the CREATE TR
31e55 49 47 47 45 52 20 6b 65 79 77 6f 72 64 73 20 61  IGGER keywords a
31e56 72 65 20 73 65 65 6e 2c 20 74 68 65 20 73 74 61  re seen, the sta
31e57 74 65 6d 65 6e 74 0a 2a 2a 20 6d 75 73 74 20 65  tement.** must e
31e58 6e 64 20 77 69 74 68 20 22 3b 45 4e 44 3b 22 2e  nd with ";END;".
31e59 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c  .**.** This impl
31e5a 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20  ementation uses 
31e5b 61 20 73 74 61 74 65 20 6d 61 63 68 69 6e 65 20  a state machine 
31e5c 77 69 74 68 20 37 20 73 74 61 74 65 73 3a 0a 2a  with 7 states:.*
31e5d 2a 0a 2a 2a 20 20 20 28 30 29 20 53 54 41 52 54  *.**   (0) START
31e5e 20 20 20 20 20 41 74 20 74 68 65 20 62 65 67 69       At the begi
31e5f 6e 6e 69 6e 67 20 6f 72 20 65 6e 64 20 6f 66 20  nning or end of 
31e60 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  an SQL statement
31e61 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a  .  This routine.
31e62 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
31e63 20 20 20 72 65 74 75 72 6e 73 20 31 20 69 66 20     returns 1 if 
31e64 69 74 20 65 6e 64 73 20 69 6e 20 74 68 65 20 53  it ends in the S
31e65 54 41 52 54 20 73 74 61 74 65 20 61 6e 64 20 30  TART state and 0
31e66 20 69 66 20 69 74 20 65 6e 64 73 0a 2a 2a 20 20   if it ends.**  
31e67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
31e68 6e 20 61 6e 79 20 6f 74 68 65 72 20 73 74 61 74  n any other stat
31e69 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 31 29 20 4e  e..**.**   (1) N
31e6a 4f 52 4d 41 4c 20 20 20 20 57 65 20 61 72 65 20  ORMAL    We are 
31e6b 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
31e6c 20 73 74 61 74 65 6d 65 6e 74 20 77 68 69 63 68   statement which
31e6d 20 65 6e 64 73 20 77 69 74 68 20 61 20 73 69 6e   ends with a sin
31e6e 67 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  gle.**          
31e6f 20 20 20 20 20 20 20 73 65 6d 69 63 6f 6c 6f 6e         semicolon
31e70 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 32 29 20 45 58  ..**.**   (2) EX
31e71 50 4c 41 49 4e 20 20 20 54 68 65 20 6b 65 79 77  PLAIN   The keyw
31e72 6f 72 64 20 45 58 50 4c 41 49 4e 20 68 61 73 20  ord EXPLAIN has 
31e73 62 65 65 6e 20 73 65 65 6e 20 61 74 20 74 68 65  been seen at the
31e74 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 0a 2a   beginning of .*
31e75 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
31e76 20 20 61 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a    a statement..*
31e77 2a 0a 2a 2a 20 20 20 28 33 29 20 43 52 45 41 54  *.**   (3) CREAT
31e78 45 20 20 20 20 54 68 65 20 6b 65 79 77 6f 72 64  E    The keyword
31e79 20 43 52 45 41 54 45 20 68 61 73 20 62 65 65 6e   CREATE has been
31e7a 20 73 65 65 6e 20 61 74 20 74 68 65 20 62 65 67   seen at the beg
31e7b 69 6e 6e 69 6e 67 20 6f 66 20 61 0a 2a 2a 20 20  inning of a.**  
31e7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
31e7d 74 61 74 65 6d 65 6e 74 2c 20 70 6f 73 73 69 62  tatement, possib
31e7e 6c 79 20 70 72 65 63 65 65 64 65 64 20 62 79 20  ly preceeded by 
31e7f 45 58 50 4c 41 49 4e 20 61 6e 64 2f 6f 72 20 66  EXPLAIN and/or f
31e80 6f 6c 6c 6f 77 65 64 20 62 79 0a 2a 2a 20 20 20  ollowed by.**   
31e81 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 45                TE
31e82 4d 50 20 6f 72 20 54 45 4d 50 4f 52 41 52 59 0a  MP or TEMPORARY.
31e83 2a 2a 0a 2a 2a 20 20 20 28 34 29 20 54 52 49 47  **.**   (4) TRIG
31e84 47 45 52 20 20 20 57 65 20 61 72 65 20 69 6e 20  GER   We are in 
31e85 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 61 20  the middle of a 
31e86 74 72 69 67 67 65 72 20 64 65 66 69 6e 69 74 69  trigger definiti
31e87 6f 6e 20 74 68 61 74 20 6d 75 73 74 20 62 65 0a  on that must be.
31e88 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
31e89 20 20 20 65 6e 64 65 64 20 62 79 20 61 20 73 65     ended by a se
31e8a 6d 69 63 6f 6c 6f 6e 2c 20 74 68 65 20 6b 65 79  micolon, the key
31e8b 77 6f 72 64 20 45 4e 44 2c 20 61 6e 64 20 61 6e  word END, and an
31e8c 6f 74 68 65 72 20 73 65 6d 69 63 6f 6c 6f 6e 2e  other semicolon.
31e8d 0a 2a 2a 0a 2a 2a 20 20 20 28 35 29 20 53 45 4d  .**.**   (5) SEM
31e8e 49 20 20 20 20 20 20 57 65 27 76 65 20 73 65 65  I      We've see
31e8f 6e 20 74 68 65 20 66 69 72 73 74 20 73 65 6d 69  n the first semi
31e90 63 6f 6c 6f 6e 20 69 6e 20 74 68 65 20 22 3b 45  colon in the ";E
31e91 4e 44 3b 22 20 74 68 61 74 20 6f 63 63 75 72 73  ND;" that occurs
31e92 20 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   at.**          
31e93 20 20 20 20 20 20 20 74 68 65 20 65 6e 64 20 6f         the end o
31e94 66 20 61 20 74 72 69 67 67 65 72 20 64 65 66 69  f a trigger defi
31e95 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20  nition..**.**   
31e96 28 36 29 20 45 4e 44 20 20 20 20 20 20 20 57 65  (6) END       We
31e97 27 76 65 20 73 65 65 6e 20 74 68 65 20 22 3b 45  've seen the ";E
31e98 4e 44 22 20 6f 66 20 74 68 65 20 22 3b 45 4e 44  ND" of the ";END
31e99 3b 22 20 74 68 61 74 20 6f 63 63 75 72 73 20 61  ;" that occurs a
31e9a 74 20 74 68 65 20 65 6e 64 0a 2a 2a 20 20 20 20  t the end.**    
31e9b 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 66 20               of 
31e9c 61 20 74 72 69 67 67 65 72 20 64 69 66 69 6e 69  a trigger difini
31e9d 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 72 61 6e  tion..**.** Tran
31e9e 73 69 74 69 6f 6e 73 20 62 65 74 77 65 65 6e 20  sitions between 
31e9f 73 74 61 74 65 73 20 61 62 6f 76 65 20 61 72 65  states above are
31ea0 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74   determined by t
31ea1 6f 6b 65 6e 73 20 65 78 74 72 61 63 74 65 64 0a  okens extracted.
31ea2 2a 2a 20 66 72 6f 6d 20 74 68 65 20 69 6e 70 75  ** from the inpu
31ea3 74 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  t.  The followin
31ea4 67 20 74 6f 6b 65 6e 73 20 61 72 65 20 73 69 67  g tokens are sig
31ea5 6e 69 66 69 63 61 6e 74 3a 0a 2a 2a 0a 2a 2a 20  nificant:.**.** 
31ea6 20 20 28 30 29 20 74 6b 53 45 4d 49 20 20 20 20    (0) tkSEMI    
31ea7 20 20 41 20 73 65 6d 69 63 6f 6c 6f 6e 2e 0a 2a    A semicolon..*
31ea8 2a 20 20 20 28 31 29 20 74 6b 57 53 20 20 20 20  *   (1) tkWS    
31ea9 20 20 20 20 57 68 69 74 65 73 70 61 63 65 0a 2a      Whitespace.*
31eaa 2a 20 20 20 28 32 29 20 74 6b 4f 54 48 45 52 20  *   (2) tkOTHER 
31eab 20 20 20 20 41 6e 79 20 6f 74 68 65 72 20 53 51      Any other SQ
31eac 4c 20 74 6f 6b 65 6e 2e 0a 2a 2a 20 20 20 28 33  L token..**   (3
31ead 29 20 74 6b 45 58 50 4c 41 49 4e 20 20 20 54 68  ) tkEXPLAIN   Th
31eae 65 20 22 65 78 70 6c 61 69 6e 22 20 6b 65 79 77  e "explain" keyw
31eaf 6f 72 64 2e 0a 2a 2a 20 20 20 28 34 29 20 74 6b  ord..**   (4) tk
31eb0 43 52 45 41 54 45 20 20 20 20 54 68 65 20 22 63  CREATE    The "c
31eb1 72 65 61 74 65 22 20 6b 65 79 77 6f 72 64 2e 0a  reate" keyword..
31eb2 2a 2a 20 20 20 28 35 29 20 74 6b 54 45 4d 50 20  **   (5) tkTEMP 
31eb3 20 20 20 20 20 54 68 65 20 22 74 65 6d 70 22 20       The "temp" 
31eb4 6f 72 20 22 74 65 6d 70 6f 72 61 72 79 22 20 6b  or "temporary" k
31eb5 65 79 77 6f 72 64 2e 0a 2a 2a 20 20 20 28 36 29  eyword..**   (6)
31eb6 20 74 6b 54 52 49 47 47 45 52 20 20 20 54 68 65   tkTRIGGER   The
31eb7 20 22 74 72 69 67 67 65 72 22 20 6b 65 79 77 6f   "trigger" keywo
31eb8 72 64 2e 0a 2a 2a 20 20 20 28 37 29 20 74 6b 45  rd..**   (7) tkE
31eb9 4e 44 20 20 20 20 20 20 20 54 68 65 20 22 65 6e  ND       The "en
31eba 64 22 20 6b 65 79 77 6f 72 64 2e 0a 2a 2a 0a 2a  d" keyword..**.*
31ebb 2a 20 57 68 69 74 65 73 70 61 63 65 20 6e 65 76  * Whitespace nev
31ebc 65 72 20 63 61 75 73 65 73 20 61 20 73 74 61 74  er causes a stat
31ebd 65 20 74 72 61 6e 73 69 74 69 6f 6e 20 61 6e 64  e transition and
31ebe 20 69 73 20 61 6c 77 61 79 73 20 69 67 6e 6f 72   is always ignor
31ebf 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77 65 20  ed..**.** If we 
31ec0 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 53 51 4c  compile with SQL
31ec1 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
31ec2 2c 20 61 6c 6c 20 6f 66 20 74 68 65 20 63 6f 6d  , all of the com
31ec3 70 75 74 61 74 69 6f 6e 20 6e 65 65 64 65 64 0a  putation needed.
31ec4 2a 2a 20 74 6f 20 72 65 63 6f 67 6e 69 7a 65 20  ** to recognize 
31ec5 74 68 65 20 65 6e 64 20 6f 66 20 61 20 74 72 69  the end of a tri
31ec6 67 67 65 72 20 63 61 6e 20 62 65 20 6f 6d 69 74  gger can be omit
31ec7 74 65 64 2e 20 20 41 6c 6c 20 77 65 20 68 61 76  ted.  All we hav
31ec8 65 20 74 6f 20 64 6f 0a 2a 2a 20 69 73 20 6c 6f  e to do.** is lo
31ec9 6f 6b 20 66 6f 72 20 61 20 73 65 6d 69 63 6f 6c  ok for a semicol
31eca 6f 6e 20 74 68 61 74 20 69 73 20 6e 6f 74 20 70  on that is not p
31ecb 61 72 74 20 6f 66 20 61 6e 20 73 74 72 69 6e 67  art of an string
31ecc 20 6f 72 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2f 0a   or comment..*/.
31ecd 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
31ece 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28  qlite3_complete(
31ecf 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c  const char *zSql
31ed0 29 7b 0a 20 20 75 38 20 73 74 61 74 65 20 3d 20  ){.  u8 state = 
31ed1 30 3b 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20  0;   /* Current 
31ed2 73 74 61 74 65 2c 20 75 73 69 6e 67 20 6e 75 6d  state, using num
31ed3 62 65 72 73 20 64 65 66 69 6e 65 64 20 69 6e 20  bers defined in 
31ed4 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 2a  header comment *
31ed5 2f 0a 20 20 75 38 20 74 6f 6b 65 6e 3b 20 20 20  /.  u8 token;   
31ed6 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20      /* Value of 
31ed7 74 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e 20 2a  the next token *
31ed8 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
31ed9 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20  E_OMIT_TRIGGER. 
31eda 20 2f 2a 20 41 20 63 6f 6d 70 6c 65 78 20 73 74   /* A complex st
31edb 61 74 65 6d 65 6e 74 20 6d 61 63 68 69 6e 65 20  atement machine 
31edc 75 73 65 64 20 74 6f 20 64 65 74 65 63 74 20 74  used to detect t
31edd 68 65 20 65 6e 64 20 6f 66 20 61 20 43 52 45 41  he end of a CREA
31ede 54 45 20 54 52 49 47 47 45 52 0a 20 20 2a 2a 20  TE TRIGGER.  ** 
31edf 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 69 73  statement.  This
31ee0 20 69 73 20 74 68 65 20 6e 6f 72 6d 61 6c 20 63   is the normal c
31ee1 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74  ase..  */.  stat
31ee2 69 63 20 63 6f 6e 73 74 20 75 38 20 74 72 61 6e  ic const u8 tran
31ee3 73 5b 37 5d 5b 38 5d 20 3d 20 7b 0a 20 20 20 20  s[7][8] = {.    
31ee4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31ee5 20 2f 2a 20 54 6f 6b 65 6e 3a 20 20 20 20 20 20   /* Token:      
31ee6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31ee7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31ee8 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20            */.   
31ee9 20 20 2f 2a 20 53 74 61 74 65 3a 20 20 20 20 20    /* State:     
31eea 20 20 2a 2a 20 20 53 45 4d 49 20 20 57 53 20 20    **  SEMI  WS  
31eeb 4f 54 48 45 52 20 45 58 50 4c 41 49 4e 20 20 43  OTHER EXPLAIN  C
31eec 52 45 41 54 45 20 20 54 45 4d 50 20 20 54 52 49  REATE  TEMP  TRI
31eed 47 47 45 52 20 20 45 4e 44 20 20 2a 2f 0a 20 20  GGER  END  */.  
31eee 20 20 20 2f 2a 20 30 20 20 20 53 54 41 52 54 3a     /* 0   START:
31eef 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20 30 2c 20   */ {    0,  0, 
31ef0 20 20 20 20 31 2c 20 20 20 20 20 20 32 2c 20 20      1,      2,  
31ef1 20 20 20 20 33 2c 20 20 20 20 31 2c 20 20 20 20      3,    1,    
31ef2 20 20 20 31 2c 20 20 20 31 2c 20 20 7d 2c 0a 20     1,   1,  },. 
31ef3 20 20 20 20 2f 2a 20 31 20 20 4e 4f 52 4d 41 4c      /* 1  NORMAL
31ef4 3a 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20 31 2c  : */ {    0,  1,
31ef5 20 20 20 20 20 31 2c 20 20 20 20 20 20 31 2c 20       1,      1, 
31ef6 20 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20       1,    1,   
31ef7 20 20 20 20 31 2c 20 20 20 31 2c 20 20 7d 2c 0a      1,   1,  },.
31ef8 20 20 20 20 20 2f 2a 20 32 20 45 58 50 4c 41 49       /* 2 EXPLAI
31ef9 4e 3a 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20 32  N: */ {    0,  2
31efa 2c 20 20 20 20 20 32 2c 20 20 20 20 20 20 31 2c  ,     2,      1,
31efb 20 20 20 20 20 20 33 2c 20 20 20 20 31 2c 20 20        3,    1,  
31efc 20 20 20 20 20 31 2c 20 20 20 31 2c 20 20 7d 2c       1,   1,  },
31efd 0a 20 20 20 20 20 2f 2a 20 33 20 20 43 52 45 41  .     /* 3  CREA
31efe 54 45 3a 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20  TE: */ {    0,  
31eff 33 2c 20 20 20 20 20 31 2c 20 20 20 20 20 20 31  3,     1,      1
31f00 2c 20 20 20 20 20 20 31 2c 20 20 20 20 33 2c 20  ,      1,    3, 
31f01 20 20 20 20 20 20 34 2c 20 20 20 31 2c 20 20 7d        4,   1,  }
31f02 2c 0a 20 20 20 20 20 2f 2a 20 34 20 54 52 49 47  ,.     /* 4 TRIG
31f03 47 45 52 3a 20 2a 2f 20 7b 20 20 20 20 35 2c 20  GER: */ {    5, 
31f04 20 34 2c 20 20 20 20 20 34 2c 20 20 20 20 20 20   4,     4,      
31f05 34 2c 20 20 20 20 20 20 34 2c 20 20 20 20 34 2c  4,      4,    4,
31f06 20 20 20 20 20 20 20 34 2c 20 20 20 34 2c 20 20         4,   4,  
31f07 7d 2c 0a 20 20 20 20 20 2f 2a 20 35 20 20 20 20  },.     /* 5    
31f08 53 45 4d 49 3a 20 2a 2f 20 7b 20 20 20 20 35 2c  SEMI: */ {    5,
31f09 20 20 35 2c 20 20 20 20 20 34 2c 20 20 20 20 20    5,     4,     
31f0a 20 34 2c 20 20 20 20 20 20 34 2c 20 20 20 20 34   4,      4,    4
31f0b 2c 20 20 20 20 20 20 20 34 2c 20 20 20 36 2c 20  ,       4,   6, 
31f0c 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 36 20 20 20   },.     /* 6   
31f0d 20 20 45 4e 44 3a 20 2a 2f 20 7b 20 20 20 20 30    END: */ {    0
31f0e 2c 20 20 36 2c 20 20 20 20 20 34 2c 20 20 20 20  ,  6,     4,    
31f0f 20 20 34 2c 20 20 20 20 20 20 34 2c 20 20 20 20    4,      4,    
31f10 34 2c 20 20 20 20 20 20 20 34 2c 20 20 20 34 2c  4,       4,   4,
31f11 20 20 7d 2c 0a 20 20 7d 3b 0a 23 65 6c 73 65 0a    },.  };.#else.
31f12 20 20 2f 2a 20 49 66 20 74 72 69 67 67 65 72 73    /* If triggers
31f13 20 61 72 65 20 6e 6f 74 20 73 75 70 70 6f 72 65   are not suppore
31f14 64 20 62 79 20 74 68 69 73 20 63 6f 6d 70 69 6c  d by this compil
31f15 65 20 74 68 65 6e 20 74 68 65 20 73 74 61 74 65  e then the state
31f16 6d 65 6e 74 20 6d 61 63 68 69 6e 65 0a 20 20 2a  ment machine.  *
31f17 2a 20 75 73 65 64 20 74 6f 20 64 65 74 65 63 74  * used to detect
31f18 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20 73 74   the end of a st
31f19 61 74 65 6d 65 6e 74 20 69 73 20 6d 75 63 68 20  atement is much 
31f1a 73 69 6d 70 6c 69 65 72 0a 20 20 2a 2f 0a 20 20  simplier.  */.  
31f1b 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20  static const u8 
31f1c 74 72 61 6e 73 5b 32 5d 5b 33 5d 20 3d 20 7b 0a  trans[2][3] = {.
31f1d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31f1e 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 3a 20 20       /* Token:  
31f1f 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20           */.    
31f20 20 2f 2a 20 53 74 61 74 65 3a 20 20 20 20 20 20   /* State:      
31f21 20 2a 2a 20 20 53 45 4d 49 20 20 57 53 20 20 4f   **  SEMI  WS  O
31f22 54 48 45 52 20 2a 2f 0a 20 20 20 20 20 2f 2a 20  THER */.     /* 
31f23 30 20 20 20 53 54 41 52 54 3a 20 2a 2f 20 7b 20  0   START: */ { 
31f24 20 20 20 30 2c 20 20 30 2c 20 20 20 20 20 31 2c     0,  0,     1,
31f25 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 31 20 20 4e   },.     /* 1  N
31f26 4f 52 4d 41 4c 3a 20 2a 2f 20 7b 20 20 20 20 30  ORMAL: */ {    0
31f27 2c 20 20 31 2c 20 20 20 20 20 31 2c 20 7d 2c 0a  ,  1,     1, },.
31f28 20 20 7d 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53    };.#endif /* S
31f29 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
31f2a 45 52 20 2a 2f 0a 0a 20 20 77 68 69 6c 65 28 20  ER */..  while( 
31f2b 2a 7a 53 71 6c 20 29 7b 0a 20 20 20 20 73 77 69  *zSql ){.    swi
31f2c 74 63 68 28 20 2a 7a 53 71 6c 20 29 7b 0a 20 20  tch( *zSql ){.  
31f2d 20 20 20 20 63 61 73 65 20 27 3b 27 3a 20 7b 20      case ';': { 
31f2e 20 2f 2a 20 41 20 73 65 6d 69 63 6f 6c 6f 6e 20   /* A semicolon 
31f2f 2a 2f 0a 20 20 20 20 20 20 20 20 74 6f 6b 65 6e  */.        token
31f30 20 3d 20 74 6b 53 45 4d 49 3b 0a 20 20 20 20 20   = tkSEMI;.     
31f31 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
31f32 7d 0a 20 20 20 20 20 20 63 61 73 65 20 27 20 27  }.      case ' '
31f33 3a 0a 20 20 20 20 20 20 63 61 73 65 20 27 5c 72  :.      case '\r
31f34 27 3a 0a 20 20 20 20 20 20 63 61 73 65 20 27 5c  ':.      case '\
31f35 74 27 3a 0a 20 20 20 20 20 20 63 61 73 65 20 27  t':.      case '
31f36 5c 6e 27 3a 0a 20 20 20 20 20 20 63 61 73 65 20  \n':.      case 
31f37 27 5c 66 27 3a 20 7b 20 20 2f 2a 20 57 68 69 74  '\f': {  /* Whit
31f38 65 20 73 70 61 63 65 20 69 73 20 69 67 6e 6f 72  e space is ignor
31f39 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 74 6f  ed */.        to
31f3a 6b 65 6e 20 3d 20 74 6b 57 53 3b 0a 20 20 20 20  ken = tkWS;.    
31f3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
31f3c 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 27 2f   }.      case '/
31f3d 27 3a 20 7b 20 20 20 2f 2a 20 43 2d 73 74 79 6c  ': {   /* C-styl
31f3e 65 20 63 6f 6d 6d 65 6e 74 73 20 2a 2f 0a 20 20  e comments */.  
31f3f 20 20 20 20 20 20 69 66 28 20 7a 53 71 6c 5b 31        if( zSql[1
31f40 5d 21 3d 27 2a 27 20 29 7b 0a 20 20 20 20 20 20  ]!='*' ){.      
31f41 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54      token = tkOT
31f42 48 45 52 3b 0a 20 20 20 20 20 20 20 20 20 20 62  HER;.          b
31f43 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
31f44 20 20 20 20 20 20 20 20 7a 53 71 6c 20 2b 3d 20          zSql += 
31f45 32 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65  2;.        while
31f46 28 20 7a 53 71 6c 5b 30 5d 20 26 26 20 28 7a 53  ( zSql[0] && (zS
31f47 71 6c 5b 30 5d 21 3d 27 2a 27 20 7c 7c 20 7a 53  ql[0]!='*' || zS
31f48 71 6c 5b 31 5d 21 3d 27 2f 27 29 20 29 7b 20 7a  ql[1]!='/') ){ z
31f49 53 71 6c 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20  Sql++; }.       
31f4a 20 69 66 28 20 7a 53 71 6c 5b 30 5d 3d 3d 30 20   if( zSql[0]==0 
31f4b 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20  ) return 0;.    
31f4c 20 20 20 20 7a 53 71 6c 2b 2b 3b 0a 20 20 20 20      zSql++;.    
31f4d 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 57 53      token = tkWS
31f4e 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
31f4f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63  .      }.      c
31f50 61 73 65 20 27 2d 27 3a 20 7b 20 20 20 2f 2a 20  ase '-': {   /* 
31f51 53 51 4c 2d 73 74 79 6c 65 20 63 6f 6d 6d 65 6e  SQL-style commen
31f52 74 73 20 66 72 6f 6d 20 22 2d 2d 22 20 74 6f 20  ts from "--" to 
31f53 65 6e 64 20 6f 66 20 6c 69 6e 65 20 2a 2f 0a 20  end of line */. 
31f54 20 20 20 20 20 20 20 69 66 28 20 7a 53 71 6c 5b         if( zSql[
31f55 31 5d 21 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20  1]!='-' ){.     
31f56 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f       token = tkO
31f57 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20 20 20  THER;.          
31f58 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
31f59 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
31f5a 2a 7a 53 71 6c 20 26 26 20 2a 7a 53 71 6c 21 3d  *zSql && *zSql!=
31f5b 27 5c 6e 27 20 29 7b 20 7a 53 71 6c 2b 2b 3b 20  '\n' ){ zSql++; 
31f5c 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 2a 7a  }.        if( *z
31f5d 53 71 6c 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  Sql==0 ) return 
31f5e 73 74 61 74 65 3d 3d 30 3b 0a 20 20 20 20 20 20  state==0;.      
31f5f 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 57 53 3b 0a    token = tkWS;.
31f60 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
31f61 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73       }.      cas
31f62 65 20 27 5b 27 3a 20 7b 20 20 20 2f 2a 20 4d 69  e '[': {   /* Mi
31f63 63 72 6f 73 6f 66 74 2d 73 74 79 6c 65 20 69 64  crosoft-style id
31f64 65 6e 74 69 66 69 65 72 73 20 69 6e 20 5b 2e 2e  entifiers in [..
31f65 2e 5d 20 2a 2f 0a 20 20 20 20 20 20 20 20 7a 53  .] */.        zS
31f66 71 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20 77 68  ql++;.        wh
31f67 69 6c 65 28 20 2a 7a 53 71 6c 20 26 26 20 2a 7a  ile( *zSql && *z
31f68 53 71 6c 21 3d 27 5d 27 20 29 7b 20 7a 53 71 6c  Sql!=']' ){ zSql
31f69 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66  ++; }.        if
31f6a 28 20 2a 7a 53 71 6c 3d 3d 30 20 29 20 72 65 74  ( *zSql==0 ) ret
31f6b 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 74  urn 0;.        t
31f6c 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a  oken = tkOTHER;.
31f6d 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
31f6e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73       }.      cas
31f6f 65 20 27 60 27 3a 20 20 20 20 20 2f 2a 20 47 72  e '`':     /* Gr
31f70 61 76 65 2d 61 63 63 65 6e 74 20 71 75 6f 74 65  ave-accent quote
31f71 64 20 73 79 6d 62 6f 6c 73 20 75 73 65 64 20 62  d symbols used b
31f72 79 20 4d 79 53 51 4c 20 2a 2f 0a 20 20 20 20 20  y MySQL */.     
31f73 20 63 61 73 65 20 27 22 27 3a 20 20 20 20 20 2f   case '"':     /
31f74 2a 20 73 69 6e 67 6c 65 2d 20 61 6e 64 20 64 6f  * single- and do
31f75 75 62 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69  uble-quoted stri
31f76 6e 67 73 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ngs */.      cas
31f77 65 20 27 5c 27 27 3a 20 7b 0a 20 20 20 20 20 20  e '\'': {.      
31f78 20 20 69 6e 74 20 63 20 3d 20 2a 7a 53 71 6c 3b    int c = *zSql;
31f79 0a 20 20 20 20 20 20 20 20 7a 53 71 6c 2b 2b 3b  .        zSql++;
31f7a 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
31f7b 2a 7a 53 71 6c 20 26 26 20 2a 7a 53 71 6c 21 3d  *zSql && *zSql!=
31f7c 63 20 29 7b 20 7a 53 71 6c 2b 2b 3b 20 7d 0a 20  c ){ zSql++; }. 
31f7d 20 20 20 20 20 20 20 69 66 28 20 2a 7a 53 71 6c         if( *zSql
31f7e 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
31f7f 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20          token = 
31f80 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20  tkOTHER;.       
31f81 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
31f82 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b        default: {
31f83 0a 20 20 20 20 20 20 20 20 69 6e 74 20 63 3b 0a  .        int c;.
31f84 20 20 20 20 20 20 20 20 69 66 28 20 49 64 43 68          if( IdCh
31f85 61 72 28 28 75 38 29 2a 7a 53 71 6c 29 20 29 7b  ar((u8)*zSql) ){
31f86 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4b 65  .          /* Ke
31f87 79 77 6f 72 64 73 20 61 6e 64 20 75 6e 71 75 6f  ywords and unquo
31f88 74 65 64 20 69 64 65 6e 74 69 66 69 65 72 73 20  ted identifiers 
31f89 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74  */.          int
31f8a 20 6e 49 64 3b 0a 20 20 20 20 20 20 20 20 20 20   nId;.          
31f8b 66 6f 72 28 6e 49 64 3d 31 3b 20 49 64 43 68 61  for(nId=1; IdCha
31f8c 72 28 7a 53 71 6c 5b 6e 49 64 5d 29 3b 20 6e 49  r(zSql[nId]); nI
31f8d 64 2b 2b 29 7b 7d 0a 23 69 66 64 65 66 20 53 51  d++){}.#ifdef SQ
31f8e 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
31f8f 52 0a 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65  R.          toke
31f90 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 23 65 6c  n = tkOTHER;.#el
31f91 73 65 0a 20 20 20 20 20 20 20 20 20 20 73 77 69  se.          swi
31f92 74 63 68 28 20 2a 7a 53 71 6c 20 29 7b 0a 20 20  tch( *zSql ){.  
31f93 20 20 20 20 20 20 20 20 20 20 63 61 73 65 20 27            case '
31f94 63 27 3a 20 63 61 73 65 20 27 43 27 3a 20 7b 0a  c': case 'C': {.
31f95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66                if
31f96 28 20 6e 49 64 3d 3d 36 20 26 26 20 73 71 6c 69  ( nId==6 && sqli
31f97 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 53 71 6c  te3StrNICmp(zSql
31f98 2c 20 22 63 72 65 61 74 65 22 2c 20 36 29 3d 3d  , "create", 6)==
31f99 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
31f9a 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 43       token = tkC
31f9b 52 45 41 54 45 3b 0a 20 20 20 20 20 20 20 20 20  REATE;.         
31f9c 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
31f9d 20 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65              toke
31f9e 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20  n = tkOTHER;.   
31f9f 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
31fa0 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
31fa1 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
31fa2 20 20 20 20 20 20 20 20 20 20 20 20 63 61 73 65              case
31fa3 20 27 74 27 3a 20 63 61 73 65 20 27 54 27 3a 20   't': case 'T': 
31fa4 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
31fa5 69 66 28 20 6e 49 64 3d 3d 37 20 26 26 20 73 71  if( nId==7 && sq
31fa6 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 53  lite3StrNICmp(zS
31fa7 71 6c 2c 20 22 74 72 69 67 67 65 72 22 2c 20 37  ql, "trigger", 7
31fa8 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
31fa9 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20          token = 
31faa 74 6b 54 52 49 47 47 45 52 3b 0a 20 20 20 20 20  tkTRIGGER;.     
31fab 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69           }else i
31fac 66 28 20 6e 49 64 3d 3d 34 20 26 26 20 73 71 6c  f( nId==4 && sql
31fad 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 53 71  ite3StrNICmp(zSq
31fae 6c 2c 20 22 74 65 6d 70 22 2c 20 34 29 3d 3d 30  l, "temp", 4)==0
31faf 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
31fb0 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 54 45      token = tkTE
31fb1 4d 50 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  MP;.            
31fb2 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 49 64 3d    }else if( nId=
31fb3 3d 39 20 26 26 20 73 71 6c 69 74 65 33 53 74 72  =9 && sqlite3Str
31fb4 4e 49 43 6d 70 28 7a 53 71 6c 2c 20 22 74 65 6d  NICmp(zSql, "tem
31fb5 70 6f 72 61 72 79 22 2c 20 39 29 3d 3d 30 20 29  porary", 9)==0 )
31fb6 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
31fb7 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 54 45 4d 50    token = tkTEMP
31fb8 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
31fb9 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
31fba 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74         token = t
31fbb 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20  kOTHER;.        
31fbc 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
31fbd 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
31fbe 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
31fbf 20 20 20 20 20 20 20 63 61 73 65 20 27 65 27 3a         case 'e':
31fc0 20 20 63 61 73 65 20 27 45 27 3a 20 7b 0a 20 20    case 'E': {.  
31fc1 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
31fc2 6e 49 64 3d 3d 33 20 26 26 20 73 71 6c 69 74 65  nId==3 && sqlite
31fc3 33 53 74 72 4e 49 43 6d 70 28 7a 53 71 6c 2c 20  3StrNICmp(zSql, 
31fc4 22 65 6e 64 22 2c 20 33 29 3d 3d 30 20 29 7b 0a  "end", 3)==0 ){.
31fc5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31fc6 74 6f 6b 65 6e 20 3d 20 74 6b 45 4e 44 3b 0a 20  token = tkEND;. 
31fc7 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c               }el
31fc8 73 65 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  se.#ifndef SQLIT
31fc9 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 20  E_OMIT_EXPLAIN. 
31fca 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28               if(
31fcb 20 6e 49 64 3d 3d 37 20 26 26 20 73 71 6c 69 74   nId==7 && sqlit
31fcc 65 33 53 74 72 4e 49 43 6d 70 28 7a 53 71 6c 2c  e3StrNICmp(zSql,
31fcd 20 22 65 78 70 6c 61 69 6e 22 2c 20 37 29 3d 3d   "explain", 7)==
31fce 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
31fcf 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 45       token = tkE
31fd0 58 50 4c 41 49 4e 3b 0a 20 20 20 20 20 20 20 20  XPLAIN;.        
31fd1 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64        }else.#end
31fd2 69 66 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  if.             
31fd3 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   {.             
31fd4 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48     token = tkOTH
31fd5 45 52 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ER;.            
31fd6 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
31fd7 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
31fd8 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
31fd9 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
31fda 20 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65              toke
31fdb 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20  n = tkOTHER;.   
31fdc 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
31fdd 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
31fde 20 20 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64            }.#end
31fdf 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
31fe0 54 5f 54 52 49 47 47 45 52 20 2a 2f 0a 20 20 20  T_TRIGGER */.   
31fe1 20 20 20 20 20 20 20 7a 53 71 6c 20 2b 3d 20 6e         zSql += n
31fe2 49 64 2d 31 3b 0a 20 20 20 20 20 20 20 20 7d 65  Id-1;.        }e
31fe3 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f  lse{.          /
31fe4 2a 20 4f 70 65 72 61 74 6f 72 73 20 61 6e 64 20  * Operators and 
31fe5 73 70 65 63 69 61 6c 20 73 79 6d 62 6f 6c 73 20  special symbols 
31fe6 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 74 6f 6b  */.          tok
31fe7 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20  en = tkOTHER;.  
31fe8 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
31fe9 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
31fea 20 20 20 7d 0a 20 20 20 20 73 74 61 74 65 20 3d     }.    state =
31feb 20 74 72 61 6e 73 5b 73 74 61 74 65 5d 5b 74 6f   trans[state][to
31fec 6b 65 6e 5d 3b 0a 20 20 20 20 7a 53 71 6c 2b 2b  ken];.    zSql++
31fed 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73  ;.  }.  return s
31fee 74 61 74 65 3d 3d 30 3b 0a 7d 0a 0a 23 69 66 6e  tate==0;.}..#ifn
31fef 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
31ff0 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 54 68 69 73  UTF16./*.** This
31ff1 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20   routine is the 
31ff2 73 61 6d 65 20 61 73 20 74 68 65 20 73 71 6c 69  same as the sqli
31ff3 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 29 20 72  te3_complete() r
31ff4 6f 75 74 69 6e 65 20 64 65 73 63 72 69 62 65 64  outine described
31ff5 0a 2a 2a 20 61 62 6f 76 65 2c 20 65 78 63 65 70  .** above, excep
31ff6 74 20 74 68 61 74 20 74 68 65 20 70 61 72 61 6d  t that the param
31ff7 65 74 65 72 20 69 73 20 72 65 71 75 69 72 65 64  eter is required
31ff8 20 74 6f 20 62 65 20 55 54 46 2d 31 36 20 65 6e   to be UTF-16 en
31ff9 63 6f 64 65 64 2c 20 6e 6f 74 0a 2a 2a 20 55 54  coded, not.** UT
31ffa 46 2d 38 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  F-8..*/.SQLITE_A
31ffb 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
31ffc 6f 6d 70 6c 65 74 65 31 36 28 63 6f 6e 73 74 20  omplete16(const 
31ffd 76 6f 69 64 20 2a 7a 53 71 6c 29 7b 0a 20 20 73  void *zSql){.  s
31ffe 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56  qlite3_value *pV
31fff 61 6c 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74  al;.  char const
32000 20 2a 7a 53 71 6c 38 3b 0a 20 20 69 6e 74 20 72   *zSql8;.  int r
32001 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
32002 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
32003 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a  E_OMIT_AUTOINIT.
32004 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 69    rc = sqlite3_i
32005 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20 69  nitialize();.  i
32006 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
32007 63 3b 0a 23 65 6e 64 69 66 0a 20 20 70 56 61 6c  c;.#endif.  pVal
32008 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e   = sqlite3ValueN
32009 65 77 28 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  ew(0);.  sqlite3
3200a 56 61 6c 75 65 53 65 74 53 74 72 28 70 56 61 6c  ValueSetStr(pVal
3200b 2c 20 2d 31 2c 20 7a 53 71 6c 2c 20 53 51 4c 49  , -1, zSql, SQLI
3200c 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20  TE_UTF16NATIVE, 
3200d 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
3200e 20 20 7a 53 71 6c 38 20 3d 20 73 71 6c 69 74 65    zSql8 = sqlite
3200f 33 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c  3ValueText(pVal,
32010 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20   SQLITE_UTF8);. 
32011 20 69 66 28 20 7a 53 71 6c 38 20 29 7b 0a 20 20   if( zSql8 ){.  
32012 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63    rc = sqlite3_c
32013 6f 6d 70 6c 65 74 65 28 7a 53 71 6c 38 29 3b 0a  omplete(zSql8);.
32014 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
32015 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
32016 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 61 6c    }.  sqlite3Val
32017 75 65 46 72 65 65 28 70 56 61 6c 29 3b 0a 20 20  ueFree(pVal);.  
32018 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41 70  return sqlite3Ap
32019 69 45 78 69 74 28 30 2c 20 72 63 29 3b 0a 7d 0a  iExit(0, rc);.}.
3201a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
3201b 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 23  _OMIT_UTF16 */.#
3201c 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
3201d 4f 4d 49 54 5f 43 4f 4d 50 4c 45 54 45 20 2a 2f  OMIT_COMPLETE */
3201e 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
3201f 2a 20 45 6e 64 20 6f 66 20 63 6f 6d 70 6c 65 74  * End of complet
32020 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
32021 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32022 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32023 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
32024 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 61 69  * Begin file mai
32025 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.c ************
32026 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32027 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32028 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
32029 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20  tember 15.**.** 
3202a 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
3202b 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
3202c 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
3202d 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
3202e 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
3202f 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
32030 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
32031 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
32032 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
32033 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
32034 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
32035 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
32036 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
32037 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
32038 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
32039 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
3203a 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
3203b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3203c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3203d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3203e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3203f 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 4d 61 69 6e  ********.** Main
32040 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 53 51   file for the SQ
32041 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 20 20 54  Lite library.  T
32042 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74  he routines in t
32043 68 69 73 20 66 69 6c 65 0a 2a 2a 20 69 6d 70 6c  his file.** impl
32044 65 6d 65 6e 74 20 74 68 65 20 70 72 6f 67 72 61  ement the progra
32045 6d 6d 65 72 20 69 6e 74 65 72 66 61 63 65 20 74  mmer interface t
32046 6f 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20 20  o the library.  
32047 52 6f 75 74 69 6e 65 73 20 69 6e 0a 2a 2a 20 6f  Routines in.** o
32048 74 68 65 72 20 66 69 6c 65 73 20 61 72 65 20 66  ther files are f
32049 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 20  or internal use 
3204a 62 79 20 53 51 4c 69 74 65 20 61 6e 64 20 73 68  by SQLite and sh
3204b 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a 20 61  ould not be.** a
3204c 63 63 65 73 73 65 64 20 62 79 20 75 73 65 72 73  ccessed by users
3204d 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 2e   of the library.
3204e 0a 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  .*/..#ifdef SQLI
3204f 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 0a 2f  TE_ENABLE_FTS3./
32050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49  ************** I
32051 6e 63 6c 75 64 65 20 66 74 73 33 2e 68 20 69 6e  nclude fts3.h in
32052 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6d   the middle of m
32053 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ain.c **********
32054 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
32055 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
32056 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33 2e 68  egin file fts3.h
32057 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
32058 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32059 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
3205a 2a 0a 2a 2a 20 32 30 30 36 20 4f 63 74 20 31 30  *.** 2006 Oct 10
3205b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
3205c 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
3205d 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
3205e 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
3205f 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
32060 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
32061 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
32062 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
32063 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
32064 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
32065 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
32066 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
32067 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
32068 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
32069 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
3206a 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
3206b 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
3206c 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
3206d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3206e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3206f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32071 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ****.**.** This 
32072 68 65 61 64 65 72 20 66 69 6c 65 20 69 73 20 75  header file is u
32073 73 65 64 20 62 79 20 70 72 6f 67 72 61 6d 73 20  sed by programs 
32074 74 68 61 74 20 77 61 6e 74 20 74 6f 20 6c 69 6e  that want to lin
32075 6b 20 61 67 61 69 6e 73 74 20 74 68 65 0a 2a 2a  k against the.**
32076 20 46 54 53 33 20 6c 69 62 72 61 72 79 2e 20 20   FTS3 library.  
32077 41 6c 6c 20 69 74 20 64 6f 65 73 20 69 73 20 64  All it does is d
32078 65 63 6c 61 72 65 20 74 68 65 20 73 71 6c 69 74  eclare the sqlit
32079 65 33 46 74 73 33 49 6e 69 74 28 29 20 69 6e 74  e3Fts3Init() int
3207a 65 72 66 61 63 65 2e 0a 2a 2f 0a 0a 23 69 66 20  erface..*/..#if 
3207b 30 0a 65 78 74 65 72 6e 20 22 43 22 20 7b 0a 23  0.extern "C" {.#
3207c 65 6e 64 69 66 20 20 2f 2a 20 5f 5f 63 70 6c 75  endif  /* __cplu
3207d 73 70 6c 75 73 20 2a 2f 0a 0a 53 51 4c 49 54 45  splus */..SQLITE
3207e 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
3207f 69 74 65 33 46 74 73 33 49 6e 69 74 28 73 71 6c  ite3Fts3Init(sql
32080 69 74 65 33 20 2a 64 62 29 3b 0a 0a 23 69 66 20  ite3 *db);..#if 
32081 30 0a 7d 20 20 2f 2a 20 65 78 74 65 72 6e 20 22  0.}  /* extern "
32082 43 22 20 2a 2f 0a 23 65 6e 64 69 66 20 20 2f 2a  C" */.#endif  /*
32083 20 5f 5f 63 70 6c 75 73 70 6c 75 73 20 2a 2f 0a   __cplusplus */.
32084 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
32085 20 45 6e 64 20 6f 66 20 66 74 73 33 2e 68 20 2a   End of fts3.h *
32086 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32087 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32088 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
32089 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
3208a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72   Continuing wher
3208b 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e  e we left off in
3208c 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   main.c ********
3208d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
3208e 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
3208f 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 52 54 52  QLITE_ENABLE_RTR
32090 45 45 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  EE./************
32091 2a 2a 20 49 6e 63 6c 75 64 65 20 72 74 72 65 65  ** Include rtree
32092 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  .h in the middle
32093 20 6f 66 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a   of main.c *****
32094 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32095 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
32096 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 72 74  ** Begin file rt
32097 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ree.h **********
32098 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32099 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3209a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61  */./*.** 2008 Ma
3209b 79 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  y 26.**.** The a
3209c 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
3209d 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
3209e 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
3209f 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
320a0 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
320a1 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
320a2 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
320a3 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
320a4 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
320a5 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
320a6 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
320a7 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
320a8 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
320a9 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
320aa 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
320ab 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
320ac 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
320ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
320ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
320af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
320b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
320b1 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
320b2 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20  his header file 
320b3 69 73 20 75 73 65 64 20 62 79 20 70 72 6f 67 72  is used by progr
320b4 61 6d 73 20 74 68 61 74 20 77 61 6e 74 20 74 6f  ams that want to
320b5 20 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20 74 68   link against th
320b6 65 0a 2a 2a 20 52 54 52 45 45 20 6c 69 62 72 61  e.** RTREE libra
320b7 72 79 2e 20 20 41 6c 6c 20 69 74 20 64 6f 65 73  ry.  All it does
320b8 20 69 73 20 64 65 63 6c 61 72 65 20 74 68 65 20   is declare the 
320b9 73 71 6c 69 74 65 33 52 74 72 65 65 49 6e 69 74  sqlite3RtreeInit
320ba 28 29 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f  () interface..*/
320bb 0a 0a 23 69 66 20 30 0a 65 78 74 65 72 6e 20 22  ..#if 0.extern "
320bc 43 22 20 7b 0a 23 65 6e 64 69 66 20 20 2f 2a 20  C" {.#endif  /* 
320bd 5f 5f 63 70 6c 75 73 70 6c 75 73 20 2a 2f 0a 0a  __cplusplus */..
320be 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
320bf 6e 74 20 73 71 6c 69 74 65 33 52 74 72 65 65 49  nt sqlite3RtreeI
320c0 6e 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29  nit(sqlite3 *db)
320c1 3b 0a 0a 23 69 66 20 30 0a 7d 20 20 2f 2a 20 65  ;..#if 0.}  /* e
320c2 78 74 65 72 6e 20 22 43 22 20 2a 2f 0a 23 65 6e  xtern "C" */.#en
320c3 64 69 66 20 20 2f 2a 20 5f 5f 63 70 6c 75 73 70  dif  /* __cplusp
320c4 6c 75 73 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  lus */../*******
320c5 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 72  ******* End of r
320c6 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  tree.h *********
320c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
320c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
320c9 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
320ca 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69  ******* Continui
320cb 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74  ng where we left
320cc 20 6f 66 66 20 69 6e 20 6d 61 69 6e 2e 63 20 2a   off in main.c *
320cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
320ce 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64 69 66 0a 23  ******/.#endif.#
320cf 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
320d0 42 4c 45 5f 49 43 55 0a 2f 2a 2a 2a 2a 2a 2a 2a  BLE_ICU./*******
320d1 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20  ******* Include 
320d2 73 71 6c 69 74 65 69 63 75 2e 68 20 69 6e 20 74  sqliteicu.h in t
320d3 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6d 61 69  he middle of mai
320d4 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.c ************
320d5 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
320d6 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
320d7 6c 65 20 73 71 6c 69 74 65 69 63 75 2e 68 20 2a  le sqliteicu.h *
320d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
320d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
320da 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
320db 30 38 20 4d 61 79 20 32 36 0a 2a 2a 0a 2a 2a 20  08 May 26.**.** 
320dc 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
320dd 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
320de 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
320df 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
320e0 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
320e1 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
320e2 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
320e3 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
320e4 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
320e5 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
320e6 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
320e7 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
320e8 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
320e9 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
320ea 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
320eb 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
320ec 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
320ed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
320ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
320ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
320f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
320f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
320f2 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20  .** This header 
320f3 66 69 6c 65 20 69 73 20 75 73 65 64 20 62 79 20  file is used by 
320f4 70 72 6f 67 72 61 6d 73 20 74 68 61 74 20 77 61  programs that wa
320f5 6e 74 20 74 6f 20 6c 69 6e 6b 20 61 67 61 69 6e  nt to link again
320f6 73 74 20 74 68 65 0a 2a 2a 20 49 43 55 20 65 78  st the.** ICU ex
320f7 74 65 6e 73 69 6f 6e 2e 20 20 41 6c 6c 20 69 74  tension.  All it
320f8 20 64 6f 65 73 20 69 73 20 64 65 63 6c 61 72 65   does is declare
320f9 20 74 68 65 20 73 71 6c 69 74 65 33 49 63 75 49   the sqlite3IcuI
320fa 6e 69 74 28 29 20 69 6e 74 65 72 66 61 63 65 2e  nit() interface.
320fb 0a 2a 2f 0a 0a 23 69 66 20 30 0a 65 78 74 65 72  .*/..#if 0.exter
320fc 6e 20 22 43 22 20 7b 0a 23 65 6e 64 69 66 20 20  n "C" {.#endif  
320fd 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c 75 73 20 2a  /* __cplusplus *
320fe 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
320ff 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 63 75  E int sqlite3Icu
32100 49 6e 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62  Init(sqlite3 *db
32101 29 3b 0a 0a 23 69 66 20 30 0a 7d 20 20 2f 2a 20  );..#if 0.}  /* 
32102 65 78 74 65 72 6e 20 22 43 22 20 2a 2f 0a 23 65  extern "C" */.#e
32103 6e 64 69 66 20 20 2f 2a 20 5f 5f 63 70 6c 75 73  ndif  /* __cplus
32104 70 6c 75 73 20 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a  plus */.../*****
32105 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
32106 20 73 71 6c 69 74 65 69 63 75 2e 68 20 2a 2a 2a   sqliteicu.h ***
32107 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32108 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32109 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
3210a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
3210b 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
3210c 66 74 20 6f 66 66 20 69 6e 20 6d 61 69 6e 2e 63  ft off in main.c
3210d 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
3210e 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64 69 66  ********/.#endif
3210f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 76 65 72 73  ../*.** The vers
32110 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 62 72 61  ion of the libra
32111 72 79 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  ry.*/.#ifndef SQ
32112 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f  LITE_AMALGAMATIO
32113 4e 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  N.SQLITE_API con
32114 73 74 20 63 68 61 72 20 73 71 6c 69 74 65 33 5f  st char sqlite3_
32115 76 65 72 73 69 6f 6e 5b 5d 20 3d 20 53 51 4c 49  version[] = SQLI
32116 54 45 5f 56 45 52 53 49 4f 4e 3b 0a 23 65 6e 64  TE_VERSION;.#end
32117 69 66 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  if.SQLITE_API co
32118 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
32119 33 5f 6c 69 62 76 65 72 73 69 6f 6e 28 76 6f 69  3_libversion(voi
3211a 64 29 7b 20 72 65 74 75 72 6e 20 73 71 6c 69 74  d){ return sqlit
3211b 65 33 5f 76 65 72 73 69 6f 6e 3b 20 7d 0a 53 51  e3_version; }.SQ
3211c 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63  LITE_API const c
3211d 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 6f 75  har *sqlite3_sou
3211e 72 63 65 69 64 28 76 6f 69 64 29 7b 20 72 65 74  rceid(void){ ret
3211f 75 72 6e 20 53 51 4c 49 54 45 5f 53 4f 55 52 43  urn SQLITE_SOURC
32120 45 5f 49 44 3b 20 7d 0a 53 51 4c 49 54 45 5f 41  E_ID; }.SQLITE_A
32121 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c  PI int sqlite3_l
32122 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72  ibversion_number
32123 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53  (void){ return S
32124 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55  QLITE_VERSION_NU
32125 4d 42 45 52 3b 20 7d 0a 53 51 4c 49 54 45 5f 41  MBER; }.SQLITE_A
32126 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74  PI int sqlite3_t
32127 68 72 65 61 64 73 61 66 65 28 76 6f 69 64 29 7b  hreadsafe(void){
32128 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 54   return SQLITE_T
32129 48 52 45 41 44 53 41 46 45 3b 20 7d 0a 0a 23 69  HREADSAFE; }..#i
3212a 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
3212b 45 5f 4f 4d 49 54 5f 54 52 41 43 45 29 20 26 26  E_OMIT_TRACE) &&
3212c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
3212d 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 29 0a  ENABLE_IOTRACE).
3212e 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f 6c  /*.** If the fol
3212f 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  lowing function 
32130 70 6f 69 6e 74 65 72 20 69 73 20 6e 6f 74 20 4e  pointer is not N
32131 55 4c 4c 20 61 6e 64 20 69 66 0a 2a 2a 20 53 51  ULL and if.** SQ
32132 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52  LITE_ENABLE_IOTR
32133 41 43 45 20 69 73 20 65 6e 61 62 6c 65 64 2c 20  ACE is enabled, 
32134 74 68 65 6e 20 6d 65 73 73 61 67 65 73 20 64 65  then messages de
32135 73 63 72 69 62 69 6e 67 0a 2a 2a 20 49 2f 4f 20  scribing.** I/O 
32136 61 63 74 69 76 65 20 61 72 65 20 77 72 69 74 74  active are writt
32137 65 6e 20 75 73 69 6e 67 20 74 68 69 73 20 66 75  en using this fu
32138 6e 63 74 69 6f 6e 2e 20 20 54 68 65 73 65 20 6d  nction.  These m
32139 65 73 73 61 67 65 73 0a 2a 2a 20 61 72 65 20 69  essages.** are i
3213a 6e 74 65 6e 64 65 64 20 66 6f 72 20 64 65 62 75  ntended for debu
3213b 67 67 69 6e 67 20 61 63 74 69 76 69 74 79 20 6f  gging activity o
3213c 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  nly..*/.SQLITE_P
3213d 52 49 56 41 54 45 20 76 6f 69 64 20 28 2a 73 71  RIVATE void (*sq
3213e 6c 69 74 65 33 49 6f 54 72 61 63 65 29 28 63 6f  lite3IoTrace)(co
3213f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 20  nst char*, ...) 
32140 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  = 0;.#endif../*.
32141 2a 2a 20 49 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ** If the follow
32142 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 69 61  ing global varia
32143 62 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 61 20  ble points to a 
32144 73 74 72 69 6e 67 20 77 68 69 63 68 20 69 73 20  string which is 
32145 74 68 65 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 61  the.** name of a
32146 20 64 69 72 65 63 74 6f 72 79 2c 20 74 68 65 6e   directory, then
32147 20 74 68 61 74 20 64 69 72 65 63 74 6f 72 79 20   that directory 
32148 77 69 6c 6c 20 62 65 20 75 73 65 64 20 74 6f 20  will be used to 
32149 73 74 6f 72 65 0a 2a 2a 20 74 65 6d 70 6f 72 61  store.** tempora
3214a 72 79 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20  ry files..**.** 
3214b 53 65 65 20 61 6c 73 6f 20 74 68 65 20 22 50 52  See also the "PR
3214c 41 47 4d 41 20 74 65 6d 70 5f 73 74 6f 72 65 5f  AGMA temp_store_
3214d 64 69 72 65 63 74 6f 72 79 22 20 53 51 4c 20 63  directory" SQL c
3214e 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54  ommand..*/.SQLIT
3214f 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69  E_API char *sqli
32150 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f  te3_temp_directo
32151 72 79 20 3d 20 30 3b 0a 0a 2f 2a 0a 2a 2a 20 49  ry = 0;../*.** I
32152 6e 69 74 69 61 6c 69 7a 65 20 53 51 4c 69 74 65  nitialize SQLite
32153 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  .  .**.** This r
32154 6f 75 74 69 6e 65 20 6d 75 73 74 20 62 65 20 63  outine must be c
32155 61 6c 6c 65 64 20 74 6f 20 69 6e 69 74 69 61 6c  alled to initial
32156 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61  ize the memory a
32157 6c 6c 6f 63 61 74 69 6f 6e 2c 0a 2a 2a 20 56 46  llocation,.** VF
32158 53 2c 20 61 6e 64 20 6d 75 74 65 78 20 73 75 62  S, and mutex sub
32159 73 79 73 74 65 6d 73 20 70 72 69 6f 72 20 74 6f  systems prior to
3215a 20 64 6f 69 6e 67 20 61 6e 79 20 73 65 72 69 6f   doing any serio
3215b 75 73 20 77 6f 72 6b 20 77 69 74 68 0a 2a 2a 20  us work with.** 
3215c 53 51 4c 69 74 65 2e 20 20 42 75 74 20 61 73 20  SQLite.  But as 
3215d 6c 6f 6e 67 20 61 73 20 79 6f 75 20 64 6f 20 6e  long as you do n
3215e 6f 74 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20  ot compile with 
3215f 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
32160 49 4e 49 54 0a 2a 2a 20 74 68 69 73 20 72 6f 75  INIT.** this rou
32161 74 69 6e 65 20 77 69 6c 6c 20 62 65 20 63 61 6c  tine will be cal
32162 6c 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  led automaticall
32163 79 20 62 79 20 6b 65 79 20 72 6f 75 74 69 6e 65  y by key routine
32164 73 20 73 75 63 68 20 61 73 0a 2a 2a 20 73 71 6c  s such as.** sql
32165 69 74 65 33 5f 6f 70 65 6e 28 29 2e 20 20 0a 2a  ite3_open().  .*
32166 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
32167 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20 65 78 63  e is a no-op exc
32168 65 70 74 20 6f 6e 20 69 74 73 20 76 65 72 79 20  ept on its very 
32169 66 69 72 73 74 20 63 61 6c 6c 20 66 6f 72 20 74  first call for t
3216a 68 65 20 70 72 6f 63 65 73 73 2c 0a 2a 2a 20 6f  he process,.** o
3216b 72 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20  r for the first 
3216c 63 61 6c 6c 20 61 66 74 65 72 20 61 20 63 61 6c  call after a cal
3216d 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 68 75  l to sqlite3_shu
3216e 74 64 6f 77 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tdown..**.** The
3216f 20 66 69 72 73 74 20 74 68 72 65 61 64 20 74 6f   first thread to
32170 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69   call this routi
32171 6e 65 20 72 75 6e 73 20 74 68 65 20 69 6e 69 74  ne runs the init
32172 69 61 6c 69 7a 61 74 69 6f 6e 20 74 6f 0a 2a 2a  ialization to.**
32173 20 63 6f 6d 70 6c 65 74 69 6f 6e 2e 20 20 49 66   completion.  If
32174 20 73 75 62 73 65 71 75 65 6e 74 20 74 68 72 65   subsequent thre
32175 61 64 73 20 63 61 6c 6c 20 74 68 69 73 20 72 6f  ads call this ro
32176 75 74 69 6e 65 20 62 65 66 6f 72 65 20 74 68 65  utine before the
32177 20 66 69 72 73 74 0a 2a 2a 20 74 68 72 65 61 64   first.** thread
32178 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 74 68   has finished th
32179 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  e initialization
3217a 20 70 72 6f 63 65 73 73 2c 20 74 68 65 6e 20 74   process, then t
3217b 68 65 20 73 75 62 73 65 71 75 65 6e 74 0a 2a 2a  he subsequent.**
3217c 20 74 68 72 65 61 64 73 20 6d 75 73 74 20 62 6c   threads must bl
3217d 6f 63 6b 20 75 6e 74 69 6c 20 74 68 65 20 66 69  ock until the fi
3217e 72 73 74 20 74 68 72 65 61 64 20 66 69 6e 69 73  rst thread finis
3217f 68 65 73 20 77 69 74 68 20 74 68 65 20 69 6e 69  hes with the ini
32180 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 0a 2a 2a 0a  tialization..**.
32181 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 68 72  ** The first thr
32182 65 61 64 20 6d 69 67 68 74 20 63 61 6c 6c 20 74  ead might call t
32183 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 63 75  his routine recu
32184 72 73 69 76 65 6c 79 2e 20 20 52 65 63 75 72 73  rsively.  Recurs
32185 69 76 65 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20  ive.** calls to 
32186 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f  this routine sho
32187 75 6c 64 20 6e 6f 74 20 62 6c 6f 63 6b 2c 20 6f  uld not block, o
32188 66 20 63 6f 75 72 73 65 2e 20 20 4f 74 68 65 72  f course.  Other
32189 77 69 73 65 20 74 68 65 0a 2a 2a 20 69 6e 69 74  wise the.** init
3218a 69 61 6c 69 7a 61 74 69 6f 6e 20 70 72 6f 63 65  ialization proce
3218b 73 73 20 77 6f 75 6c 64 20 6e 65 76 65 72 20 63  ss would never c
3218c 6f 6d 70 6c 65 74 65 2e 0a 2a 2a 0a 2a 2a 20 4c  omplete..**.** L
3218d 65 74 20 58 20 62 65 20 74 68 65 20 66 69 72 73  et X be the firs
3218e 74 20 74 68 72 65 61 64 20 74 6f 20 65 6e 74 65  t thread to ente
3218f 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20  r this routine. 
32190 20 4c 65 74 20 59 20 62 65 20 73 6f 6d 65 20 6f   Let Y be some o
32191 74 68 65 72 0a 2a 2a 20 74 68 72 65 61 64 2e 20  ther.** thread. 
32192 20 54 68 65 6e 20 77 68 69 6c 65 20 74 68 65 20   Then while the 
32193 69 6e 69 74 69 61 6c 20 69 6e 76 6f 63 61 74 69  initial invocati
32194 6f 6e 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69  on of this routi
32195 6e 65 20 62 79 20 58 20 69 73 0a 2a 2a 20 69 6e  ne by X is.** in
32196 63 6f 6d 70 6c 65 74 65 2c 20 69 74 20 69 73 20  complete, it is 
32197 72 65 71 75 69 72 65 64 20 74 68 61 74 3a 0a 2a  required that:.*
32198 2a 0a 2a 2a 20 20 20 20 2a 20 20 43 61 6c 6c 73  *.**    *  Calls
32199 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65   to this routine
3219a 20 66 72 6f 6d 20 59 20 6d 75 73 74 20 62 6c 6f   from Y must blo
3219b 63 6b 20 75 6e 74 69 6c 20 74 68 65 20 6f 75 74  ck until the out
3219c 65 72 2d 6d 6f 73 74 0a 2a 2a 20 20 20 20 20 20  er-most.**      
3219d 20 63 61 6c 6c 20 62 79 20 58 20 63 6f 6d 70 6c   call by X compl
3219e 65 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 2a  etes..**.**    *
3219f 20 20 52 65 63 75 72 73 69 76 65 20 63 61 6c 6c    Recursive call
321a0 73 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e  s to this routin
321a1 65 20 66 72 6f 6d 20 74 68 72 65 61 64 20 58 20  e from thread X 
321a2 72 65 74 75 72 6e 20 69 6d 6d 65 64 69 61 74 65  return immediate
321a3 6c 79 0a 2a 2a 20 20 20 20 20 20 20 77 69 74 68  ly.**       with
321a4 6f 75 74 20 62 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f  out blocking..*/
321a5 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
321a6 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
321a7 7a 65 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69  ze(void){.  sqli
321a8 74 65 33 5f 6d 75 74 65 78 20 2a 70 4d 61 73 74  te3_mutex *pMast
321a9 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  er;             
321aa 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
321ab 6d 61 69 6e 20 73 74 61 74 69 63 20 6d 75 74 65  main static mute
321ac 78 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20  x */.  int rc;  
321ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
321ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
321af 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f      /* Result co
321b0 64 65 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51  de */..#ifdef SQ
321b1 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 20 20  LITE_OMIT_WSD.  
321b2 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 77 73 64  rc = sqlite3_wsd
321b3 5f 69 6e 69 74 28 34 30 39 36 2c 20 32 34 29 3b  _init(4096, 24);
321b4 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
321b5 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75  E_OK ){.    retu
321b6 72 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 6e 64 69  rn rc;.  }.#endi
321b7 66 0a 0a 20 20 2f 2a 20 49 66 20 53 51 4c 69 74  f..  /* If SQLit
321b8 65 20 69 73 20 61 6c 72 65 61 64 79 20 63 6f 6d  e is already com
321b9 70 6c 65 74 65 6c 79 20 69 6e 69 74 69 61 6c 69  pletely initiali
321ba 7a 65 64 2c 20 74 68 65 6e 20 74 68 69 73 20 63  zed, then this c
321bb 61 6c 6c 0a 20 20 2a 2a 20 74 6f 20 73 71 6c 69  all.  ** to sqli
321bc 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
321bd 20 73 68 6f 75 6c 64 20 62 65 20 61 20 6e 6f 2d   should be a no-
321be 6f 70 2e 20 20 42 75 74 20 74 68 65 20 69 6e 69  op.  But the ini
321bf 74 69 61 6c 69 7a 61 74 69 6f 6e 0a 20 20 2a 2a  tialization.  **
321c0 20 6d 75 73 74 20 62 65 20 63 6f 6d 70 6c 65 74   must be complet
321c1 65 2e 20 20 53 6f 20 69 73 49 6e 69 74 20 6d 75  e.  So isInit mu
321c2 73 74 20 6e 6f 74 20 62 65 20 73 65 74 20 75 6e  st not be set un
321c3 74 69 6c 20 74 68 65 20 76 65 72 79 20 65 6e 64  til the very end
321c4 0a 20 20 2a 2a 20 6f 66 20 74 68 69 73 20 72 6f  .  ** of this ro
321c5 75 74 69 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  utine..  */.  if
321c6 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
321c7 6f 6e 66 69 67 2e 69 73 49 6e 69 74 20 29 20 72  onfig.isInit ) r
321c8 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
321c9 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
321ca 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79   the mutex subsy
321cb 73 74 65 6d 20 69 73 20 69 6e 69 74 69 61 6c 69  stem is initiali
321cc 7a 65 64 2e 20 20 49 66 20 75 6e 61 62 6c 65 20  zed.  If unable 
321cd 74 6f 20 0a 20 20 2a 2a 20 69 6e 69 74 69 61 6c  to .  ** initial
321ce 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20 73 75  ize the mutex su
321cf 62 73 79 73 74 65 6d 2c 20 72 65 74 75 72 6e 20  bsystem, return 
321d0 65 61 72 6c 79 20 77 69 74 68 20 74 68 65 20 65  early with the e
321d1 72 72 6f 72 2e 0a 20 20 2a 2a 20 49 66 20 74 68  rror..  ** If th
321d2 65 20 73 79 73 74 65 6d 20 69 73 20 73 6f 20 73  e system is so s
321d3 69 63 6b 20 74 68 61 74 20 77 65 20 61 72 65 20  ick that we are 
321d4 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61  unable to alloca
321d5 74 65 20 61 20 6d 75 74 65 78 2c 0a 20 20 2a 2a  te a mutex,.  **
321d6 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20 6d 75   there is not mu
321d7 63 68 20 53 51 4c 69 74 65 20 69 73 20 67 6f 69  ch SQLite is goi
321d8 6e 67 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f  ng to be able to
321d9 20 64 6f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54   do..  **.  ** T
321da 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74  he mutex subsyst
321db 65 6d 20 6d 75 73 74 20 74 61 6b 65 20 63 61 72  em must take car
321dc 65 20 6f 66 20 73 65 72 69 61 6c 69 7a 69 6e 67  e of serializing
321dd 20 69 74 73 20 6f 77 6e 0a 20 20 2a 2a 20 69 6e   its own.  ** in
321de 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 0a 20 20  itialization..  
321df 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  */.  rc = sqlite
321e0 33 4d 75 74 65 78 49 6e 69 74 28 29 3b 0a 20 20  3MutexInit();.  
321e1 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
321e2 72 63 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61  rc;..  /* Initia
321e3 6c 69 7a 65 20 74 68 65 20 6d 61 6c 6c 6f 63 28  lize the malloc(
321e4 29 20 73 79 73 74 65 6d 20 61 6e 64 20 74 68 65  ) system and the
321e5 20 72 65 63 75 72 73 69 76 65 20 70 49 6e 69 74   recursive pInit
321e6 4d 75 74 65 78 20 6d 75 74 65 78 2e 0a 20 20 2a  Mutex mutex..  *
321e7 2a 20 54 68 69 73 20 6f 70 65 72 61 74 69 6f 6e  * This operation
321e8 20 69 73 20 70 72 6f 74 65 63 74 65 64 20 62 79   is protected by
321e9 20 74 68 65 20 53 54 41 54 49 43 5f 4d 41 53 54   the STATIC_MAST
321ea 45 52 20 6d 75 74 65 78 2e 20 20 4e 6f 74 65 20  ER mutex.  Note 
321eb 74 68 61 74 0a 20 20 2a 2a 20 4d 75 74 65 78 41  that.  ** MutexA
321ec 6c 6c 6f 63 28 29 20 69 73 20 63 61 6c 6c 65 64  lloc() is called
321ed 20 66 6f 72 20 61 20 73 74 61 74 69 63 20 6d 75   for a static mu
321ee 74 65 78 20 70 72 69 6f 72 20 74 6f 20 69 6e 69  tex prior to ini
321ef 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 0a 20 20  tializing the.  
321f0 2a 2a 20 6d 61 6c 6c 6f 63 20 73 75 62 73 79 73  ** malloc subsys
321f1 74 65 6d 20 2d 20 74 68 69 73 20 69 6d 70 6c 69  tem - this impli
321f2 65 73 20 74 68 61 74 20 74 68 65 20 61 6c 6c 6f  es that the allo
321f3 63 61 74 69 6f 6e 20 6f 66 20 61 20 73 74 61 74  cation of a stat
321f4 69 63 0a 20 20 2a 2a 20 6d 75 74 65 78 20 6d 75  ic.  ** mutex mu
321f5 73 74 20 6e 6f 74 20 72 65 71 75 69 72 65 20 73  st not require s
321f6 75 70 70 6f 72 74 20 66 72 6f 6d 20 74 68 65 20  upport from the 
321f7 6d 61 6c 6c 6f 63 20 73 75 62 73 79 73 74 65 6d  malloc subsystem
321f8 2e 0a 20 20 2a 2f 0a 20 20 70 4d 61 73 74 65 72  ..  */.  pMaster
321f9 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41   = sqlite3MutexA
321fa 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
321fb 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29  X_STATIC_MASTER)
321fc 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
321fd 78 5f 65 6e 74 65 72 28 70 4d 61 73 74 65 72 29  x_enter(pMaster)
321fe 3b 0a 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  ;.  sqlite3Globa
321ff 6c 43 6f 6e 66 69 67 2e 69 73 4d 75 74 65 78 49  lConfig.isMutexI
32200 6e 69 74 20 3d 20 31 3b 0a 20 20 69 66 28 20 21  nit = 1;.  if( !
32201 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
32202 66 69 67 2e 69 73 4d 61 6c 6c 6f 63 49 6e 69 74  fig.isMallocInit
32203 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   ){.    rc = sql
32204 69 74 65 33 4d 61 6c 6c 6f 63 49 6e 69 74 28 29  ite3MallocInit()
32205 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d  ;.  }.  if( rc==
32206 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
32207 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
32208 6e 66 69 67 2e 69 73 4d 61 6c 6c 6f 63 49 6e 69  nfig.isMallocIni
32209 74 20 3d 20 31 3b 0a 20 20 20 20 69 66 28 20 21  t = 1;.    if( !
3220a 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
3220b 66 69 67 2e 70 49 6e 69 74 4d 75 74 65 78 20 29  fig.pInitMutex )
3220c 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47  {.      sqlite3G
3220d 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 49 6e 69  lobalConfig.pIni
3220e 74 4d 75 74 65 78 20 3d 0a 20 20 20 20 20 20 20  tMutex =.       
3220f 20 20 20 20 73 71 6c 69 74 65 33 4d 75 74 65 78      sqlite3Mutex
32210 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
32211 45 58 5f 52 45 43 55 52 53 49 56 45 29 3b 0a 20  EX_RECURSIVE);. 
32212 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
32213 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f  GlobalConfig.bCo
32214 72 65 4d 75 74 65 78 20 26 26 20 21 73 71 6c 69  reMutex && !sqli
32215 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
32216 70 49 6e 69 74 4d 75 74 65 78 20 29 7b 0a 20 20  pInitMutex ){.  
32217 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
32218 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d  E_NOMEM;.      }
32219 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
3221a 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
3221b 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f  {.    sqlite3Glo
3221c 62 61 6c 43 6f 6e 66 69 67 2e 6e 52 65 66 49 6e  balConfig.nRefIn
3221d 69 74 4d 75 74 65 78 2b 2b 3b 0a 20 20 7d 0a 20  itMutex++;.  }. 
3221e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
3221f 65 61 76 65 28 70 4d 61 73 74 65 72 29 3b 0a 0a  eave(pMaster);..
32220 20 20 2f 2a 20 49 66 20 72 63 20 69 73 20 6e 6f    /* If rc is no
32221 74 20 53 51 4c 49 54 45 5f 4f 4b 20 61 74 20 74  t SQLITE_OK at t
32222 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e 20  his point, then 
32223 65 69 74 68 65 72 20 74 68 65 20 6d 61 6c 6c 6f  either the mallo
32224 63 0a 20 20 2a 2a 20 73 75 62 73 79 73 74 65 6d  c.  ** subsystem
32225 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 69 6e   could not be in
32226 69 74 69 61 6c 69 7a 65 64 20 6f 72 20 74 68 65  itialized or the
32227 20 73 79 73 74 65 6d 20 66 61 69 6c 65 64 20 74   system failed t
32228 6f 20 61 6c 6c 6f 63 61 74 65 0a 20 20 2a 2a 20  o allocate.  ** 
32229 74 68 65 20 70 49 6e 69 74 4d 75 74 65 78 20 6d  the pInitMutex m
3222a 75 74 65 78 2e 20 52 65 74 75 72 6e 20 61 6e 20  utex. Return an 
3222b 65 72 72 6f 72 20 69 6e 20 65 69 74 68 65 72 20  error in either 
3222c 63 61 73 65 2e 20 20 2a 2f 0a 20 20 69 66 28 20  case.  */.  if( 
3222d 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
3222e 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
3222f 20 20 7d 0a 0a 20 20 2f 2a 20 44 6f 20 74 68 65    }..  /* Do the
32230 20 72 65 73 74 20 6f 66 20 74 68 65 20 69 6e 69   rest of the ini
32231 74 69 61 6c 69 7a 61 74 69 6f 6e 20 75 6e 64 65  tialization unde
32232 72 20 74 68 65 20 72 65 63 75 72 73 69 76 65 20  r the recursive 
32233 6d 75 74 65 78 20 73 6f 0a 20 20 2a 2a 20 74 68  mutex so.  ** th
32234 61 74 20 77 65 20 77 69 6c 6c 20 62 65 20 61 62  at we will be ab
32235 6c 65 20 74 6f 20 68 61 6e 64 6c 65 20 72 65 63  le to handle rec
32236 75 72 73 69 76 65 20 63 61 6c 6c 73 20 69 6e 74  ursive calls int
32237 6f 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 69  o.  ** sqlite3_i
32238 6e 69 74 69 61 6c 69 7a 65 28 29 2e 20 20 54 68  nitialize().  Th
32239 65 20 72 65 63 75 72 73 69 76 65 20 63 61 6c 6c  e recursive call
3223a 73 20 6e 6f 72 6d 61 6c 6c 79 20 63 6f 6d 65 20  s normally come 
3223b 74 68 72 6f 75 67 68 0a 20 20 2a 2a 20 73 71 6c  through.  ** sql
3223c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 77  ite3_os_init() w
3223d 68 65 6e 20 69 74 20 69 6e 76 6f 6b 65 73 20 73  hen it invokes s
3223e 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73  qlite3_vfs_regis
3223f 74 65 72 28 29 2c 20 62 75 74 20 6f 74 68 65 72  ter(), but other
32240 0a 20 20 2a 2a 20 72 65 63 75 72 73 69 76 65 20  .  ** recursive 
32241 63 61 6c 6c 73 20 6d 69 67 68 74 20 61 6c 73 6f  calls might also
32242 20 62 65 20 70 6f 73 73 69 62 6c 65 2e 0a 20 20   be possible..  
32243 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  */.  sqlite3_mut
32244 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33  ex_enter(sqlite3
32245 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 49 6e  GlobalConfig.pIn
32246 69 74 4d 75 74 65 78 29 3b 0a 20 20 69 66 28 20  itMutex);.  if( 
32247 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
32248 66 69 67 2e 69 73 49 6e 69 74 3d 3d 30 20 26 26  fig.isInit==0 &&
32249 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
3224a 6e 66 69 67 2e 69 6e 50 72 6f 67 72 65 73 73 3d  nfig.inProgress=
3224b 3d 30 20 29 7b 0a 20 20 20 20 46 75 6e 63 44 65  =0 ){.    FuncDe
3224c 66 48 61 73 68 20 2a 70 48 61 73 68 20 3d 20 26  fHash *pHash = &
3224d 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66 48 61  GLOBAL(FuncDefHa
3224e 73 68 2c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  sh, sqlite3Globa
3224f 6c 46 75 6e 63 74 69 6f 6e 73 29 3b 0a 20 20 20  lFunctions);.   
32250 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
32251 6e 66 69 67 2e 69 6e 50 72 6f 67 72 65 73 73 20  nfig.inProgress 
32252 3d 20 31 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28  = 1;.    memset(
32253 70 48 61 73 68 2c 20 30 2c 20 73 69 7a 65 6f 66  pHash, 0, sizeof
32254 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75  (sqlite3GlobalFu
32255 6e 63 74 69 6f 6e 73 29 29 3b 0a 20 20 20 20 73  nctions));.    s
32256 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 47 6c  qlite3RegisterGl
32257 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 28 29 3b  obalFunctions();
32258 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
32259 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 50  GlobalConfig.isP
3225a 43 61 63 68 65 49 6e 69 74 3d 3d 30 20 29 7b 0a  CacheInit==0 ){.
3225b 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
3225c 65 33 50 63 61 63 68 65 49 6e 69 74 69 61 6c 69  e3PcacheInitiali
3225d 7a 65 28 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ze();.    }.    
3225e 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
3225f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  K ){.      sqlit
32260 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69  e3GlobalConfig.i
32261 73 50 43 61 63 68 65 49 6e 69 74 20 3d 20 31 3b  sPCacheInit = 1;
32262 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
32263 74 65 33 4f 73 49 6e 69 74 28 29 3b 0a 20 20 20  te3OsInit();.   
32264 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53   }.    if( rc==S
32265 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
32266 20 20 73 71 6c 69 74 65 33 50 43 61 63 68 65 42    sqlite3PCacheB
32267 75 66 66 65 72 53 65 74 75 70 28 20 73 71 6c 69  ufferSetup( sqli
32268 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
32269 70 50 61 67 65 2c 20 0a 20 20 20 20 20 20 20 20  pPage, .        
3226a 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
3226b 6f 6e 66 69 67 2e 73 7a 50 61 67 65 2c 20 73 71  onfig.szPage, sq
3226c 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
3226d 67 2e 6e 50 61 67 65 29 3b 0a 20 20 20 20 20 20  g.nPage);.      
3226e 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
3226f 66 69 67 2e 69 73 49 6e 69 74 20 3d 20 31 3b 0a  fig.isInit = 1;.
32270 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
32271 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 6e  3GlobalConfig.in
32272 50 72 6f 67 72 65 73 73 20 3d 20 30 3b 0a 20 20  Progress = 0;.  
32273 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
32274 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 33 47  x_leave(sqlite3G
32275 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 49 6e 69  lobalConfig.pIni
32276 74 4d 75 74 65 78 29 3b 0a 0a 20 20 2f 2a 20 47  tMutex);..  /* G
32277 6f 20 62 61 63 6b 20 75 6e 64 65 72 20 74 68 65  o back under the
32278 20 73 74 61 74 69 63 20 6d 75 74 65 78 20 61 6e   static mutex an
32279 64 20 63 6c 65 61 6e 20 75 70 20 74 68 65 20 72  d clean up the r
3227a 65 63 75 72 73 69 76 65 0a 20 20 2a 2a 20 6d 75  ecursive.  ** mu
3227b 74 65 78 20 74 6f 20 70 72 65 76 65 6e 74 20 61  tex to prevent a
3227c 20 72 65 73 6f 75 72 63 65 20 6c 65 61 6b 2e 0a   resource leak..
3227d 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d    */.  sqlite3_m
3227e 75 74 65 78 5f 65 6e 74 65 72 28 70 4d 61 73 74  utex_enter(pMast
3227f 65 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 47 6c  er);.  sqlite3Gl
32280 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 52 65 66 49  obalConfig.nRefI
32281 6e 69 74 4d 75 74 65 78 2d 2d 3b 0a 20 20 69 66  nitMutex--;.  if
32282 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
32283 6f 6e 66 69 67 2e 6e 52 65 66 49 6e 69 74 4d 75  onfig.nRefInitMu
32284 74 65 78 3c 3d 30 20 29 7b 0a 20 20 20 20 61 73  tex<=0 ){.    as
32285 73 65 72 74 28 20 73 71 6c 69 74 65 33 47 6c 6f  sert( sqlite3Glo
32286 62 61 6c 43 6f 6e 66 69 67 2e 6e 52 65 66 49 6e  balConfig.nRefIn
32287 69 74 4d 75 74 65 78 3d 3d 30 20 29 3b 0a 20 20  itMutex==0 );.  
32288 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
32289 66 72 65 65 28 73 71 6c 69 74 65 33 47 6c 6f 62  free(sqlite3Glob
3228a 61 6c 43 6f 6e 66 69 67 2e 70 49 6e 69 74 4d 75  alConfig.pInitMu
3228b 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  tex);.    sqlite
3228c 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 49  3GlobalConfig.pI
3228d 6e 69 74 4d 75 74 65 78 20 3d 20 30 3b 0a 20 20  nitMutex = 0;.  
3228e 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
3228f 78 5f 6c 65 61 76 65 28 70 4d 61 73 74 65 72 29  x_leave(pMaster)
32290 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c  ;..  /* The foll
32291 6f 77 69 6e 67 20 69 73 20 6a 75 73 74 20 61 20  owing is just a 
32292 73 61 6e 69 74 79 20 63 68 65 63 6b 20 74 6f 20  sanity check to 
32293 6d 61 6b 65 20 73 75 72 65 20 53 51 4c 69 74 65  make sure SQLite
32294 20 68 61 73 0a 20 20 2a 2a 20 62 65 65 6e 20 63   has.  ** been c
32295 6f 6d 70 69 6c 65 64 20 63 6f 72 72 65 63 74 6c  ompiled correctl
32296 79 2e 20 20 49 74 20 69 73 20 69 6d 70 6f 72 74  y.  It is import
32297 61 6e 74 20 74 6f 20 72 75 6e 20 74 68 69 73 20  ant to run this 
32298 63 6f 64 65 2c 20 62 75 74 0a 20 20 2a 2a 20 77  code, but.  ** w
32299 65 20 64 6f 6e 27 74 20 77 61 6e 74 20 74 6f 20  e don't want to 
3229a 72 75 6e 20 69 74 20 74 6f 6f 20 6f 66 74 65 6e  run it too often
3229b 20 61 6e 64 20 73 6f 61 6b 20 75 70 20 43 50 55   and soak up CPU
3229c 20 63 79 63 6c 65 73 20 66 6f 72 20 6e 6f 0a 20   cycles for no. 
3229d 20 2a 2a 20 72 65 61 73 6f 6e 2e 20 20 53 6f 20   ** reason.  So 
3229e 77 65 20 72 75 6e 20 69 74 20 6f 6e 63 65 20 64  we run it once d
3229f 75 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61  uring initializa
322a0 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64  tion..  */.#ifnd
322a1 65 66 20 4e 44 45 42 55 47 0a 23 69 66 6e 64 65  ef NDEBUG.#ifnde
322a2 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  f SQLITE_OMIT_FL
322a3 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 2f  OATING_POINT.  /
322a4 2a 20 54 68 69 73 20 73 65 63 74 69 6f 6e 20 6f  * This section o
322a5 66 20 63 6f 64 65 27 73 20 6f 6e 6c 79 20 22 6f  f code's only "o
322a6 75 74 70 75 74 22 20 69 73 20 76 69 61 20 61 73  utput" is via as
322a7 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74  sert() statement
322a8 73 2e 20 2a 2f 0a 20 20 69 66 20 28 20 72 63 3d  s. */.  if ( rc=
322a9 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
322aa 20 20 75 36 34 20 78 20 3d 20 28 28 28 75 36 34    u64 x = (((u64
322ab 29 31 29 3c 3c 36 33 29 2d 31 3b 0a 20 20 20 20  )1)<<63)-1;.    
322ac 64 6f 75 62 6c 65 20 79 3b 0a 20 20 20 20 61 73  double y;.    as
322ad 73 65 72 74 28 73 69 7a 65 6f 66 28 78 29 3d 3d  sert(sizeof(x)==
322ae 38 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 73  8);.    assert(s
322af 69 7a 65 6f 66 28 78 29 3d 3d 73 69 7a 65 6f 66  izeof(x)==sizeof
322b0 28 79 29 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79  (y));.    memcpy
322b1 28 26 79 2c 20 26 78 2c 20 38 29 3b 0a 20 20 20  (&y, &x, 8);.   
322b2 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
322b3 49 73 4e 61 4e 28 79 29 20 29 3b 0a 20 20 7d 0a  IsNaN(y) );.  }.
322b4 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 20  #endif.#endif.. 
322b5 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
322b6 2a 0a 2a 2a 20 55 6e 64 6f 20 74 68 65 20 65 66  *.** Undo the ef
322b7 66 65 63 74 73 20 6f 66 20 73 71 6c 69 74 65 33  fects of sqlite3
322b8 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 2e 20 20  _initialize().  
322b9 4d 75 73 74 20 6e 6f 74 20 62 65 20 63 61 6c 6c  Must not be call
322ba 65 64 20 77 68 69 6c 65 0a 2a 2a 20 74 68 65 72  ed while.** ther
322bb 65 20 61 72 65 20 6f 75 74 73 74 61 6e 64 69 6e  e are outstandin
322bc 67 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  g database conne
322bd 63 74 69 6f 6e 73 20 6f 72 20 6d 65 6d 6f 72 79  ctions or memory
322be 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 6f 72 0a   allocations or.
322bf 2a 2a 20 77 68 69 6c 65 20 61 6e 79 20 70 61 72  ** while any par
322c0 74 20 6f 66 20 53 51 4c 69 74 65 20 69 73 20 6f  t of SQLite is o
322c1 74 68 65 72 77 69 73 65 20 69 6e 20 75 73 65 20  therwise in use 
322c2 69 6e 20 61 6e 79 20 74 68 72 65 61 64 2e 20 20  in any thread.  
322c3 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  This.** routine 
322c4 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66  is not threadsaf
322c5 65 2e 20 20 42 75 74 20 69 74 20 69 73 20 73 61  e.  But it is sa
322c6 66 65 20 74 6f 20 69 6e 76 6f 6b 65 20 74 68 69  fe to invoke thi
322c7 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6f 6e 20  s routine.** on 
322c8 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 61  when SQLite is a
322c9 6c 72 65 61 64 79 20 73 68 75 74 20 64 6f 77 6e  lready shut down
322ca 2e 20 20 49 66 20 53 51 4c 69 74 65 20 69 73 20  .  If SQLite is 
322cb 61 6c 72 65 61 64 79 20 73 68 75 74 20 64 6f 77  already shut dow
322cc 6e 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 72  n.** when this r
322cd 6f 75 74 69 6e 65 20 69 73 20 69 6e 76 6f 6b 65  outine is invoke
322ce 64 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75  d, then this rou
322cf 74 69 6e 65 20 69 73 20 61 20 68 61 72 6d 6c 65  tine is a harmle
322d0 73 73 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c  ss no-op..*/.SQL
322d1 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
322d2 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 76 6f 69  te3_shutdown(voi
322d3 64 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  d){.  if( sqlite
322d4 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73  3GlobalConfig.is
322d5 49 6e 69 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  Init ){.    sqli
322d6 74 65 33 5f 6f 73 5f 65 6e 64 28 29 3b 0a 20 20  te3_os_end();.  
322d7 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 5f    sqlite3_reset_
322d8 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 29  auto_extension()
322d9 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f  ;.    sqlite3Glo
322da 62 61 6c 43 6f 6e 66 69 67 2e 69 73 49 6e 69 74  balConfig.isInit
322db 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20   = 0;.  }.  if( 
322dc 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
322dd 66 69 67 2e 69 73 50 43 61 63 68 65 49 6e 69 74  fig.isPCacheInit
322de 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 50   ){.    sqlite3P
322df 63 61 63 68 65 53 68 75 74 64 6f 77 6e 28 29 3b  cacheShutdown();
322e0 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
322e1 61 6c 43 6f 6e 66 69 67 2e 69 73 50 43 61 63 68  alConfig.isPCach
322e2 65 49 6e 69 74 20 3d 20 30 3b 0a 20 20 7d 0a 20  eInit = 0;.  }. 
322e3 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
322e4 61 6c 43 6f 6e 66 69 67 2e 69 73 4d 61 6c 6c 6f  alConfig.isMallo
322e5 63 49 6e 69 74 20 29 7b 0a 20 20 20 20 73 71 6c  cInit ){.    sql
322e6 69 74 65 33 4d 61 6c 6c 6f 63 45 6e 64 28 29 3b  ite3MallocEnd();
322e7 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
322e8 61 6c 43 6f 6e 66 69 67 2e 69 73 4d 61 6c 6c 6f  alConfig.isMallo
322e9 63 49 6e 69 74 20 3d 20 30 3b 0a 20 20 7d 0a 20  cInit = 0;.  }. 
322ea 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
322eb 61 6c 43 6f 6e 66 69 67 2e 69 73 4d 75 74 65 78  alConfig.isMutex
322ec 49 6e 69 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  Init ){.    sqli
322ed 74 65 33 4d 75 74 65 78 45 6e 64 28 29 3b 0a 20  te3MutexEnd();. 
322ee 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
322ef 43 6f 6e 66 69 67 2e 69 73 4d 75 74 65 78 49 6e  Config.isMutexIn
322f0 69 74 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 72  it = 0;.  }..  r
322f1 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
322f2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 41  .}../*.** This A
322f3 50 49 20 61 6c 6c 6f 77 73 20 61 70 70 6c 69 63  PI allows applic
322f4 61 74 69 6f 6e 73 20 74 6f 20 6d 6f 64 69 66 79  ations to modify
322f5 20 74 68 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66   the global conf
322f6 69 67 75 72 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20  iguration of.** 
322f7 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61  the SQLite libra
322f8 72 79 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 0a  ry at run-time..
322f9 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
322fa 6e 65 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 62  ne should only b
322fb 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68  e called when th
322fc 65 72 65 20 61 72 65 20 6e 6f 20 6f 75 74 73 74  ere are no outst
322fd 61 6e 64 69 6e 67 0a 2a 2a 20 64 61 74 61 62 61  anding.** databa
322fe 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6f  se connections o
322ff 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  r memory allocat
32300 69 6f 6e 73 2e 20 20 54 68 69 73 20 72 6f 75 74  ions.  This rout
32301 69 6e 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 74 68  ine is not.** th
32302 72 65 61 64 73 61 66 65 2e 20 20 46 61 69 6c 75  readsafe.  Failu
32303 72 65 20 74 6f 20 68 65 65 64 20 74 68 65 73 65  re to heed these
32304 20 77 61 72 6e 69 6e 67 73 20 63 61 6e 20 6c 65   warnings can le
32305 61 64 20 74 6f 20 75 6e 70 72 65 64 69 63 74 61  ad to unpredicta
32306 62 6c 65 0a 2a 2a 20 62 65 68 61 76 69 6f 72 2e  ble.** behavior.
32307 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
32308 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  nt sqlite3_confi
32309 67 28 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 7b 0a  g(int op, ...){.
3230a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20    va_list ap;.  
3230b 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
3230c 4f 4b 3b 0a 0a 20 20 2f 2a 20 73 71 6c 69 74 65  OK;..  /* sqlite
3230d 33 5f 63 6f 6e 66 69 67 28 29 20 73 68 61 6c 6c  3_config() shall
3230e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d   return SQLITE_M
3230f 49 53 55 53 45 20 69 66 20 69 74 20 69 73 20 69  ISUSE if it is i
32310 6e 76 6f 6b 65 64 20 77 68 69 6c 65 0a 20 20 2a  nvoked while.  *
32311 2a 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62  * the SQLite lib
32312 72 61 72 79 20 69 73 20 69 6e 20 75 73 65 2e 20  rary is in use. 
32313 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
32314 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 49  GlobalConfig.isI
32315 6e 69 74 20 29 20 72 65 74 75 72 6e 20 53 51 4c  nit ) return SQL
32316 49 54 45 5f 4d 49 53 55 53 45 3b 0a 0a 20 20 76  ITE_MISUSE;..  v
32317 61 5f 73 74 61 72 74 28 61 70 2c 20 6f 70 29 3b  a_start(ap, op);
32318 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b  .  switch( op ){
32319 0a 0a 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63  ..    /* Mutex c
3231a 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74  onfiguration opt
3231b 69 6f 6e 73 20 61 72 65 20 6f 6e 6c 79 20 61 76  ions are only av
3231c 61 69 6c 61 62 6c 65 20 69 6e 20 61 20 74 68 72  ailable in a thr
3231d 65 61 64 73 61 66 65 0a 20 20 20 20 2a 2a 20 63  eadsafe.    ** c
3231e 6f 6d 70 69 6c 65 2e 20 0a 20 20 20 20 2a 2f 0a  ompile. .    */.
3231f 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
32320 44 53 41 46 45 0a 20 20 20 20 63 61 73 65 20 53  DSAFE.    case S
32321 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e  QLITE_CONFIG_SIN
32322 47 4c 45 54 48 52 45 41 44 3a 20 7b 0a 20 20 20  GLETHREAD: {.   
32323 20 20 20 2f 2a 20 44 69 73 61 62 6c 65 20 61 6c     /* Disable al
32324 6c 20 6d 75 74 65 78 69 6e 67 20 2a 2f 0a 20 20  l mutexing */.  
32325 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
32326 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74  lConfig.bCoreMut
32327 65 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71  ex = 0;.      sq
32328 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
32329 67 2e 62 46 75 6c 6c 4d 75 74 65 78 20 3d 20 30  g.bFullMutex = 0
3232a 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
3232b 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51     }.    case SQ
3232c 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54  LITE_CONFIG_MULT
3232d 49 54 48 52 45 41 44 3a 20 7b 0a 20 20 20 20 20  ITHREAD: {.     
3232e 20 2f 2a 20 44 69 73 61 62 6c 65 20 6d 75 74 65   /* Disable mute
3232f 78 69 6e 67 20 6f 66 20 64 61 74 61 62 61 73 65  xing of database
32330 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 2a 2f 0a   connections */.
32331 20 20 20 20 20 20 2f 2a 20 45 6e 61 62 6c 65 20        /* Enable 
32332 6d 75 74 65 78 69 6e 67 20 6f 66 20 63 6f 72 65  mutexing of core
32333 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73   data structures
32334 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
32335 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43  3GlobalConfig.bC
32336 6f 72 65 4d 75 74 65 78 20 3d 20 31 3b 0a 20 20  oreMutex = 1;.  
32337 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
32338 6c 43 6f 6e 66 69 67 2e 62 46 75 6c 6c 4d 75 74  lConfig.bFullMut
32339 65 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 62 72  ex = 0;.      br
3233a 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
3233b 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ase SQLITE_CONFI
3233c 47 5f 53 45 52 49 41 4c 49 5a 45 44 3a 20 7b 0a  G_SERIALIZED: {.
3233d 20 20 20 20 20 20 2f 2a 20 45 6e 61 62 6c 65 20        /* Enable 
3233e 61 6c 6c 20 6d 75 74 65 78 69 6e 67 20 2a 2f 0a  all mutexing */.
3233f 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f        sqlite3Glo
32340 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d  balConfig.bCoreM
32341 75 74 65 78 20 3d 20 31 3b 0a 20 20 20 20 20 20  utex = 1;.      
32342 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
32343 66 69 67 2e 62 46 75 6c 6c 4d 75 74 65 78 20 3d  fig.bFullMutex =
32344 20 31 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   1;.      break;
32345 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
32346 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55  SQLITE_CONFIG_MU
32347 54 45 58 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20  TEX: {.      /* 
32348 53 70 65 63 69 66 79 20 61 6e 20 61 6c 74 65 72  Specify an alter
32349 6e 61 74 69 76 65 20 6d 75 74 65 78 20 69 6d 70  native mutex imp
3234a 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20  lementation */. 
3234b 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
3234c 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 20 3d  alConfig.mutex =
3234d 20 2a 76 61 5f 61 72 67 28 61 70 2c 20 73 71 6c   *va_arg(ap, sql
3234e 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
3234f 64 73 2a 29 3b 0a 20 20 20 20 20 20 62 72 65 61  ds*);.      brea
32350 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
32351 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  e SQLITE_CONFIG_
32352 47 45 54 4d 55 54 45 58 3a 20 7b 0a 20 20 20 20  GETMUTEX: {.    
32353 20 20 2f 2a 20 52 65 74 72 69 65 76 65 20 74 68    /* Retrieve th
32354 65 20 63 75 72 72 65 6e 74 20 6d 75 74 65 78 20  e current mutex 
32355 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a  implementation *
32356 2f 0a 20 20 20 20 20 20 2a 76 61 5f 61 72 67 28  /.      *va_arg(
32357 61 70 2c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ap, sqlite3_mute
32358 78 5f 6d 65 74 68 6f 64 73 2a 29 20 3d 20 73 71  x_methods*) = sq
32359 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
3235a 67 2e 6d 75 74 65 78 3b 0a 20 20 20 20 20 20 62  g.mutex;.      b
3235b 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  reak;.    }.#end
3235c 69 66 0a 0a 0a 20 20 20 20 63 61 73 65 20 53 51  if...    case SQ
3235d 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c  LITE_CONFIG_MALL
3235e 4f 43 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 53  OC: {.      /* S
3235f 70 65 63 69 66 79 20 61 6e 20 61 6c 74 65 72 6e  pecify an altern
32360 61 74 69 76 65 20 6d 61 6c 6c 6f 63 20 69 6d 70  ative malloc imp
32361 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20  lementation */. 
32362 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
32363 61 6c 43 6f 6e 66 69 67 2e 6d 20 3d 20 2a 76 61  alConfig.m = *va
32364 5f 61 72 67 28 61 70 2c 20 73 71 6c 69 74 65 33  _arg(ap, sqlite3
32365 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a 29 3b 0a  _mem_methods*);.
32366 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
32367 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49   }.    case SQLI
32368 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c  TE_CONFIG_GETMAL
32369 4c 4f 43 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20  LOC: {.      /* 
3236a 52 65 74 72 69 65 76 65 20 74 68 65 20 63 75 72  Retrieve the cur
3236b 72 65 6e 74 20 6d 61 6c 6c 6f 63 28 29 20 69 6d  rent malloc() im
3236c 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a  plementation */.
3236d 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
3236e 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e  3GlobalConfig.m.
3236f 78 4d 61 6c 6c 6f 63 3d 3d 30 20 29 20 73 71 6c  xMalloc==0 ) sql
32370 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c  ite3MemSetDefaul
32371 74 28 29 3b 0a 20 20 20 20 20 20 2a 76 61 5f 61  t();.      *va_a
32372 72 67 28 61 70 2c 20 73 71 6c 69 74 65 33 5f 6d  rg(ap, sqlite3_m
32373 65 6d 5f 6d 65 74 68 6f 64 73 2a 29 20 3d 20 73  em_methods*) = s
32374 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
32375 69 67 2e 6d 3b 0a 20 20 20 20 20 20 62 72 65 61  ig.m;.      brea
32376 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
32377 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  e SQLITE_CONFIG_
32378 4d 45 4d 53 54 41 54 55 53 3a 20 7b 0a 20 20 20  MEMSTATUS: {.   
32379 20 20 20 2f 2a 20 45 6e 61 62 6c 65 20 6f 72 20     /* Enable or 
3237a 64 69 73 61 62 6c 65 20 74 68 65 20 6d 61 6c 6c  disable the mall
3237b 6f 63 20 73 74 61 74 75 73 20 63 6f 6c 6c 65 63  oc status collec
3237c 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 73 71  tion */.      sq
3237d 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
3237e 67 2e 62 4d 65 6d 73 74 61 74 20 3d 20 76 61 5f  g.bMemstat = va_
3237f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20  arg(ap, int);.  
32380 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
32381 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
32382 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 3a  _CONFIG_SCRATCH:
32383 20 7b 0a 20 20 20 20 20 20 2f 2a 20 44 65 73 69   {.      /* Desi
32384 67 6e 61 74 65 20 61 20 62 75 66 66 65 72 20 66  gnate a buffer f
32385 6f 72 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72  or scratch memor
32386 79 20 73 70 61 63 65 20 2a 2f 0a 20 20 20 20 20  y space */.     
32387 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
32388 6e 66 69 67 2e 70 53 63 72 61 74 63 68 20 3d 20  nfig.pScratch = 
32389 76 61 5f 61 72 67 28 61 70 2c 20 76 6f 69 64 2a  va_arg(ap, void*
3238a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
3238b 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53  GlobalConfig.szS
3238c 63 72 61 74 63 68 20 3d 20 76 61 5f 61 72 67 28  cratch = va_arg(
3238d 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20  ap, int);.      
3238e 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
3238f 66 69 67 2e 6e 53 63 72 61 74 63 68 20 3d 20 76  fig.nScratch = v
32390 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a  a_arg(ap, int);.
32391 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
32392 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49   }.    case SQLI
32393 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41  TE_CONFIG_PAGECA
32394 43 48 45 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20  CHE: {.      /* 
32395 44 65 73 69 67 6e 61 74 65 20 61 20 62 75 66 66  Designate a buff
32396 65 72 20 66 6f 72 20 70 61 67 65 20 63 61 63 68  er for page cach
32397 65 20 6d 65 6d 6f 72 79 20 73 70 61 63 65 20 2a  e memory space *
32398 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47  /.      sqlite3G
32399 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67  lobalConfig.pPag
3239a 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 76  e = va_arg(ap, v
3239b 6f 69 64 2a 29 3b 0a 20 20 20 20 20 20 73 71 6c  oid*);.      sql
3239c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
3239d 2e 73 7a 50 61 67 65 20 3d 20 76 61 5f 61 72 67  .szPage = va_arg
3239e 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20  (ap, int);.     
3239f 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
323a0 6e 66 69 67 2e 6e 50 61 67 65 20 3d 20 76 61 5f  nfig.nPage = va_
323a1 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20  arg(ap, int);.  
323a2 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
323a3 0a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ..    case SQLIT
323a4 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 3a  E_CONFIG_PCACHE:
323a5 20 7b 0a 20 20 20 20 20 20 2f 2a 20 53 70 65 63   {.      /* Spec
323a6 69 66 79 20 61 6e 20 61 6c 74 65 72 6e 61 74 69  ify an alternati
323a7 76 65 20 70 61 67 65 20 63 61 63 68 65 20 69 6d  ve page cache im
323a8 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a  plementation */.
323a9 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f        sqlite3Glo
323aa 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65  balConfig.pcache
323ab 20 3d 20 2a 76 61 5f 61 72 67 28 61 70 2c 20 73   = *va_arg(ap, s
323ac 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65  qlite3_pcache_me
323ad 74 68 6f 64 73 2a 29 3b 0a 20 20 20 20 20 20 62  thods*);.      b
323ae 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  reak;.    }..   
323af 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e   case SQLITE_CON
323b0 46 49 47 5f 47 45 54 50 43 41 43 48 45 3a 20 7b  FIG_GETPCACHE: {
323b1 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
323b2 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
323b3 63 61 63 68 65 2e 78 49 6e 69 74 3d 3d 30 20 29  cache.xInit==0 )
323b4 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
323b5 33 50 43 61 63 68 65 53 65 74 44 65 66 61 75 6c  3PCacheSetDefaul
323b6 74 28 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  t();.      }.   
323b7 20 20 20 2a 76 61 5f 61 72 67 28 61 70 2c 20 73     *va_arg(ap, s
323b8 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65  qlite3_pcache_me
323b9 74 68 6f 64 73 2a 29 20 3d 20 73 71 6c 69 74 65  thods*) = sqlite
323ba 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63  3GlobalConfig.pc
323bb 61 63 68 65 3b 0a 20 20 20 20 20 20 62 72 65 61  ache;.      brea
323bc 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 20 64 65  k;.    }..#if de
323bd 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
323be 42 4c 45 5f 4d 45 4d 53 59 53 33 29 20 7c 7c 20  BLE_MEMSYS3) || 
323bf 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
323c0 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 29 0a 20  NABLE_MEMSYS5). 
323c1 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43     case SQLITE_C
323c2 4f 4e 46 49 47 5f 48 45 41 50 3a 20 7b 0a 20 20  ONFIG_HEAP: {.  
323c3 20 20 20 20 2f 2a 20 44 65 73 69 67 6e 61 74 65      /* Designate
323c4 20 61 20 62 75 66 66 65 72 20 66 6f 72 20 68 65   a buffer for he
323c5 61 70 20 6d 65 6d 6f 72 79 20 73 70 61 63 65 20  ap memory space 
323c6 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
323c7 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65  GlobalConfig.pHe
323c8 61 70 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20  ap = va_arg(ap, 
323c9 76 6f 69 64 2a 29 3b 0a 20 20 20 20 20 20 73 71  void*);.      sq
323ca 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
323cb 67 2e 6e 48 65 61 70 20 3d 20 76 61 5f 61 72 67  g.nHeap = va_arg
323cc 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20  (ap, int);.     
323cd 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
323ce 6e 66 69 67 2e 6d 6e 52 65 71 20 3d 20 76 61 5f  nfig.mnReq = va_
323cf 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 0a 20  arg(ap, int);.. 
323d0 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
323d1 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65  GlobalConfig.pHe
323d2 61 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ap==0 ){.       
323d3 20 2f 2a 20 49 66 20 74 68 65 20 68 65 61 70 20   /* If the heap 
323d4 70 6f 69 6e 74 65 72 20 69 73 20 4e 55 4c 4c 2c  pointer is NULL,
323d5 20 74 68 65 6e 20 72 65 73 74 6f 72 65 20 74 68   then restore th
323d6 65 20 6d 61 6c 6c 6f 63 20 69 6d 70 6c 65 6d 65  e malloc impleme
323d7 6e 74 61 74 69 6f 6e 0a 20 20 20 20 20 20 20 20  ntation.        
323d8 2a 2a 20 62 61 63 6b 20 74 6f 20 4e 55 4c 4c 20  ** back to NULL 
323d9 70 6f 69 6e 74 65 72 73 20 74 6f 6f 2e 20 20 54  pointers too.  T
323da 68 69 73 20 77 69 6c 6c 20 63 61 75 73 65 20 74  his will cause t
323db 68 65 20 6d 61 6c 6c 6f 63 20 74 6f 20 67 6f 0a  he malloc to go.
323dc 20 20 20 20 20 20 20 20 2a 2a 20 62 61 63 6b 20          ** back 
323dd 74 6f 20 69 74 73 20 64 65 66 61 75 6c 74 20 69  to its default i
323de 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 68  mplementation wh
323df 65 6e 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  en sqlite3_initi
323e0 61 6c 69 7a 65 28 29 20 69 73 0a 20 20 20 20 20  alize() is.     
323e1 20 20 20 2a 2a 20 72 75 6e 2e 0a 20 20 20 20 20     ** run..     
323e2 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 6d 65     */.        me
323e3 6d 73 65 74 28 26 73 71 6c 69 74 65 33 47 6c 6f  mset(&sqlite3Glo
323e4 62 61 6c 43 6f 6e 66 69 67 2e 6d 2c 20 30 2c 20  balConfig.m, 0, 
323e5 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 47 6c  sizeof(sqlite3Gl
323e6 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 29 29 3b 0a  obalConfig.m));.
323e7 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
323e8 20 20 20 20 20 2f 2a 20 54 68 65 20 68 65 61 70       /* The heap
323e9 20 70 6f 69 6e 74 65 72 20 69 73 20 6e 6f 74 20   pointer is not 
323ea 4e 55 4c 4c 2c 20 74 68 65 6e 20 69 6e 73 74 61  NULL, then insta
323eb 6c 6c 20 6f 6e 65 20 6f 66 20 74 68 65 0a 20 20  ll one of the.  
323ec 20 20 20 20 20 20 2a 2a 20 6d 65 6d 35 2e 63 2f        ** mem5.c/
323ed 6d 65 6d 33 2e 63 20 6d 65 74 68 6f 64 73 2e 20  mem3.c methods. 
323ee 49 66 20 6e 65 69 74 68 65 72 20 45 4e 41 42 4c  If neither ENABL
323ef 45 5f 4d 45 4d 53 59 53 33 20 6e 6f 72 0a 20 20  E_MEMSYS3 nor.  
323f0 20 20 20 20 20 20 2a 2a 20 45 4e 41 42 4c 45 5f        ** ENABLE_
323f1 4d 45 4d 53 59 53 35 20 69 73 20 64 65 66 69 6e  MEMSYS5 is defin
323f2 65 64 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72  ed, return an er
323f3 72 6f 72 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  ror..        */.
323f4 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
323f5 41 42 4c 45 5f 4d 45 4d 53 59 53 33 0a 20 20 20  ABLE_MEMSYS3.   
323f6 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
323f7 61 6c 43 6f 6e 66 69 67 2e 6d 20 3d 20 2a 73 71  alConfig.m = *sq
323f8 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73 79  lite3MemGetMemsy
323f9 73 33 28 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66  s3();.#endif.#if
323fa 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
323fb 45 5f 4d 45 4d 53 59 53 35 0a 20 20 20 20 20 20  E_MEMSYS5.      
323fc 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
323fd 6f 6e 66 69 67 2e 6d 20 3d 20 2a 73 71 6c 69 74  onfig.m = *sqlit
323fe 65 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73 35 28  e3MemGetMemsys5(
323ff 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20  );.#endif.      
32400 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
32401 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20     }.#endif..   
32402 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e   case SQLITE_CON
32403 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 3a 20 7b  FIG_LOOKASIDE: {
32404 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c  .      sqlite3Gl
32405 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 4c 6f 6f  obalConfig.szLoo
32406 6b 61 73 69 64 65 20 3d 20 76 61 5f 61 72 67 28  kaside = va_arg(
32407 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20  ap, int);.      
32408 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
32409 66 69 67 2e 6e 4c 6f 6f 6b 61 73 69 64 65 20 3d  fig.nLookaside =
3240a 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29   va_arg(ap, int)
3240b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
3240c 20 20 20 7d 0a 0a 20 20 20 20 64 65 66 61 75 6c     }..    defaul
3240d 74 3a 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  t: {.      rc = 
3240e 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
3240f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
32410 0a 20 20 7d 0a 20 20 76 61 5f 65 6e 64 28 61 70  .  }.  va_end(ap
32411 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
32412 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 75 70 20  }../*.** Set up 
32413 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75  the lookaside bu
32414 66 66 65 72 73 20 66 6f 72 20 61 20 64 61 74 61  ffers for a data
32415 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  base connection.
32416 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54  .** Return SQLIT
32417 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e  E_OK on success.
32418 20 20 0a 2a 2a 20 49 66 20 6c 6f 6f 6b 61 73 69    .** If lookasi
32419 64 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 63  de is already ac
3241a 74 69 76 65 2c 20 72 65 74 75 72 6e 20 53 51 4c  tive, return SQL
3241b 49 54 45 5f 42 55 53 59 2e 0a 2a 2a 0a 2a 2a 20  ITE_BUSY..**.** 
3241c 54 68 65 20 73 7a 20 70 61 72 61 6d 65 74 65 72  The sz parameter
3241d 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
3241e 66 20 62 79 74 65 73 20 69 6e 20 65 61 63 68 20  f bytes in each 
3241f 6c 6f 6f 6b 61 73 69 64 65 20 73 6c 6f 74 2e 0a  lookaside slot..
32420 2a 2a 20 54 68 65 20 63 6e 74 20 70 61 72 61 6d  ** The cnt param
32421 65 74 65 72 20 69 73 20 74 68 65 20 6e 75 6d 62  eter is the numb
32422 65 72 20 6f 66 20 73 6c 6f 74 73 2e 20 20 49 66  er of slots.  If
32423 20 70 53 74 61 72 74 20 69 73 20 4e 55 4c 4c 20   pStart is NULL 
32424 74 68 65 0a 2a 2a 20 73 70 61 63 65 20 66 6f 72  the.** space for
32425 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20 6d   the lookaside m
32426 65 6d 6f 72 79 20 69 73 20 6f 62 74 61 69 6e 65  emory is obtaine
32427 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d  d from sqlite3_m
32428 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 49 66 20 70  alloc()..** If p
32429 53 74 61 72 74 20 69 73 20 6e 6f 74 20 4e 55 4c  Start is not NUL
3242a 4c 20 74 68 65 6e 20 69 74 20 69 73 20 73 7a 2a  L then it is sz*
3242b 63 6e 74 20 62 79 74 65 73 20 6f 66 20 6d 65 6d  cnt bytes of mem
3242c 6f 72 79 20 74 6f 20 75 73 65 20 66 6f 72 0a 2a  ory to use for.*
3242d 2a 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20  * the lookaside 
3242e 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69  memory..*/.stati
3242f 63 20 69 6e 74 20 73 65 74 75 70 4c 6f 6f 6b 61  c int setupLooka
32430 73 69 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62  side(sqlite3 *db
32431 2c 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e  , void *pBuf, in
32432 74 20 73 7a 2c 20 69 6e 74 20 63 6e 74 29 7b 0a  t sz, int cnt){.
32433 20 20 76 6f 69 64 20 2a 70 53 74 61 72 74 3b 0a    void *pStart;.
32434 20 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73    if( db->lookas
32435 69 64 65 2e 6e 4f 75 74 20 29 7b 0a 20 20 20 20  ide.nOut ){.    
32436 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55  return SQLITE_BU
32437 53 59 3b 0a 20 20 7d 0a 20 20 2f 2a 20 46 72 65  SY;.  }.  /* Fre
32438 65 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20 6c  e any existing l
32439 6f 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 20  ookaside buffer 
3243a 66 6f 72 20 74 68 69 73 20 68 61 6e 64 6c 65 20  for this handle 
3243b 62 65 66 6f 72 65 0a 20 20 2a 2a 20 61 6c 6c 6f  before.  ** allo
3243c 63 61 74 69 6e 67 20 61 20 6e 65 77 20 6f 6e 65  cating a new one
3243d 20 73 6f 20 77 65 20 64 6f 6e 27 74 20 68 61 76   so we don't hav
3243e 65 20 74 6f 20 68 61 76 65 20 73 70 61 63 65 20  e to have space 
3243f 66 6f 72 20 0a 20 20 2a 2a 20 62 6f 74 68 20 61  for .  ** both a
32440 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e  t the same time.
32441 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e  .  */.  if( db->
32442 6c 6f 6f 6b 61 73 69 64 65 2e 62 4d 61 6c 6c 6f  lookaside.bMallo
32443 63 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ced ){.    sqlit
32444 65 33 5f 66 72 65 65 28 64 62 2d 3e 6c 6f 6f 6b  e3_free(db->look
32445 61 73 69 64 65 2e 70 53 74 61 72 74 29 3b 0a 20  aside.pStart);. 
32446 20 7d 0a 20 20 2f 2a 20 54 68 65 20 73 69 7a 65   }.  /* The size
32447 20 6f 66 20 61 20 6c 6f 6f 6b 61 73 69 64 65 20   of a lookaside 
32448 73 6c 6f 74 20 6e 65 65 64 73 20 74 6f 20 62 65  slot needs to be
32449 20 6c 61 72 67 65 72 20 74 68 61 6e 20 61 20 70   larger than a p
3244a 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 74 6f 20 62  ointer.  ** to b
3244b 65 20 75 73 65 66 75 6c 2e 0a 20 20 2a 2f 0a 20  e useful..  */. 
3244c 20 69 66 28 20 73 7a 3c 3d 28 69 6e 74 29 73 69   if( sz<=(int)si
3244d 7a 65 6f 66 28 4c 6f 6f 6b 61 73 69 64 65 53 6c  zeof(LookasideSl
3244e 6f 74 2a 29 20 29 20 73 7a 20 3d 20 30 3b 0a 20  ot*) ) sz = 0;. 
3244f 20 69 66 28 20 63 6e 74 3c 30 20 29 20 63 6e 74   if( cnt<0 ) cnt
32450 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 7a 3d 3d   = 0;.  if( sz==
32451 30 20 7c 7c 20 63 6e 74 3d 3d 30 20 29 7b 0a 20  0 || cnt==0 ){. 
32452 20 20 20 73 7a 20 3d 20 30 3b 0a 20 20 20 20 70     sz = 0;.    p
32453 53 74 61 72 74 20 3d 20 30 3b 0a 20 20 7d 65 6c  Start = 0;.  }el
32454 73 65 20 69 66 28 20 70 42 75 66 3d 3d 30 20 29  se if( pBuf==0 )
32455 7b 0a 20 20 20 20 73 7a 20 3d 20 52 4f 55 4e 44  {.    sz = ROUND
32456 38 28 73 7a 29 3b 0a 20 20 20 20 73 71 6c 69 74  8(sz);.    sqlit
32457 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c  e3BeginBenignMal
32458 6c 6f 63 28 29 3b 0a 20 20 20 20 70 53 74 61 72  loc();.    pStar
32459 74 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  t = sqlite3Mallo
3245a 63 28 20 73 7a 2a 63 6e 74 20 29 3b 0a 20 20 20  c( sz*cnt );.   
3245b 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67   sqlite3EndBenig
3245c 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 7d 65 6c  nMalloc();.  }el
3245d 73 65 7b 0a 20 20 20 20 73 7a 20 3d 20 52 4f 55  se{.    sz = ROU
3245e 4e 44 44 4f 57 4e 38 28 73 7a 29 3b 0a 20 20 20  NDDOWN8(sz);.   
3245f 20 70 53 74 61 72 74 20 3d 20 70 42 75 66 3b 0a   pStart = pBuf;.
32460 20 20 7d 0a 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73    }.  db->lookas
32461 69 64 65 2e 70 53 74 61 72 74 20 3d 20 70 53 74  ide.pStart = pSt
32462 61 72 74 3b 0a 20 20 64 62 2d 3e 6c 6f 6f 6b 61  art;.  db->looka
32463 73 69 64 65 2e 70 46 72 65 65 20 3d 20 30 3b 0a  side.pFree = 0;.
32464 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e    db->lookaside.
32465 73 7a 20 3d 20 28 75 31 36 29 73 7a 3b 0a 20 20  sz = (u16)sz;.  
32466 69 66 28 20 70 53 74 61 72 74 20 29 7b 0a 20 20  if( pStart ){.  
32467 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 4c 6f 6f    int i;.    Loo
32468 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 3b 0a 20  kasideSlot *p;. 
32469 20 20 20 61 73 73 65 72 74 28 20 73 7a 20 3e 20     assert( sz > 
3246a 28 69 6e 74 29 73 69 7a 65 6f 66 28 4c 6f 6f 6b  (int)sizeof(Look
3246b 61 73 69 64 65 53 6c 6f 74 2a 29 20 29 3b 0a 20  asideSlot*) );. 
3246c 20 20 20 70 20 3d 20 28 4c 6f 6f 6b 61 73 69 64     p = (Lookasid
3246d 65 53 6c 6f 74 2a 29 70 53 74 61 72 74 3b 0a 20  eSlot*)pStart;. 
3246e 20 20 20 66 6f 72 28 69 3d 63 6e 74 2d 31 3b 20     for(i=cnt-1; 
3246f 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20  i>=0; i--){.    
32470 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 64 62 2d    p->pNext = db-
32471 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65  >lookaside.pFree
32472 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b  ;.      db->look
32473 61 73 69 64 65 2e 70 46 72 65 65 20 3d 20 70 3b  aside.pFree = p;
32474 0a 20 20 20 20 20 20 70 20 3d 20 28 4c 6f 6f 6b  .      p = (Look
32475 61 73 69 64 65 53 6c 6f 74 2a 29 26 28 28 75 38  asideSlot*)&((u8
32476 2a 29 70 29 5b 73 7a 5d 3b 0a 20 20 20 20 7d 0a  *)p)[sz];.    }.
32477 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64      db->lookasid
32478 65 2e 70 45 6e 64 20 3d 20 70 3b 0a 20 20 20 20  e.pEnd = p;.    
32479 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45  db->lookaside.bE
3247a 6e 61 62 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20  nabled = 1;.    
3247b 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 4d  db->lookaside.bM
3247c 61 6c 6c 6f 63 65 64 20 3d 20 70 42 75 66 3d 3d  alloced = pBuf==
3247d 30 20 3f 31 3a 30 3b 0a 20 20 7d 65 6c 73 65 7b  0 ?1:0;.  }else{
3247e 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69  .    db->lookasi
3247f 64 65 2e 70 45 6e 64 20 3d 20 30 3b 0a 20 20 20  de.pEnd = 0;.   
32480 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62   db->lookaside.b
32481 45 6e 61 62 6c 65 64 20 3d 20 30 3b 0a 20 20 20  Enabled = 0;.   
32482 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62   db->lookaside.b
32483 4d 61 6c 6c 6f 63 65 64 20 3d 20 30 3b 0a 20 20  Malloced = 0;.  
32484 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
32485 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  E_OK;.}../*.** R
32486 65 74 75 72 6e 20 74 68 65 20 6d 75 74 65 78 20  eturn the mutex 
32487 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
32488 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  a database conne
32489 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ction..*/.SQLITE
3248a 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 6d 75 74  _API sqlite3_mut
3248b 65 78 20 2a 73 71 6c 69 74 65 33 5f 64 62 5f 6d  ex *sqlite3_db_m
3248c 75 74 65 78 28 73 71 6c 69 74 65 33 20 2a 64 62  utex(sqlite3 *db
3248d 29 7b 0a 20 20 72 65 74 75 72 6e 20 64 62 2d 3e  ){.  return db->
3248e 6d 75 74 65 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  mutex;.}../*.** 
3248f 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 73 65  Configuration se
32490 74 74 69 6e 67 73 20 66 6f 72 20 61 6e 20 69 6e  ttings for an in
32491 64 69 76 69 64 75 61 6c 20 64 61 74 61 62 61 73  dividual databas
32492 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2f 0a  e connection.*/.
32493 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
32494 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67  qlite3_db_config
32495 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
32496 74 20 6f 70 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61  t op, ...){.  va
32497 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69 6e 74 20  _list ap;.  int 
32498 72 63 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61  rc;.  va_start(a
32499 70 2c 20 6f 70 29 3b 0a 20 20 73 77 69 74 63 68  p, op);.  switch
3249a 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65  ( op ){.    case
3249b 20 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47   SQLITE_DBCONFIG
3249c 5f 4c 4f 4f 4b 41 53 49 44 45 3a 20 7b 0a 20 20  _LOOKASIDE: {.  
3249d 20 20 20 20 76 6f 69 64 20 2a 70 42 75 66 20 3d      void *pBuf =
3249e 20 76 61 5f 61 72 67 28 61 70 2c 20 76 6f 69 64   va_arg(ap, void
3249f 2a 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 73 7a  *);.      int sz
324a0 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e   = va_arg(ap, in
324a1 74 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 63 6e  t);.      int cn
324a2 74 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69  t = va_arg(ap, i
324a3 6e 74 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  nt);.      rc = 
324a4 73 65 74 75 70 4c 6f 6f 6b 61 73 69 64 65 28 64  setupLookaside(d
324a5 62 2c 20 70 42 75 66 2c 20 73 7a 2c 20 63 6e 74  b, pBuf, sz, cnt
324a6 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
324a7 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c      }.    defaul
324a8 74 3a 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  t: {.      rc = 
324a9 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
324aa 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
324ab 0a 20 20 7d 0a 20 20 76 61 5f 65 6e 64 28 61 70  .  }.  va_end(ap
324ac 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
324ad 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  }.../*.** Return
324ae 20 74 72 75 65 20 69 66 20 74 68 65 20 62 75 66   true if the buf
324af 66 65 72 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 63 6f  fer z[0..n-1] co
324b0 6e 74 61 69 6e 73 20 61 6c 6c 20 73 70 61 63 65  ntains all space
324b1 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
324b2 20 61 6c 6c 53 70 61 63 65 73 28 63 6f 6e 73 74   allSpaces(const
324b3 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29   char *z, int n)
324b4 7b 0a 20 20 77 68 69 6c 65 28 20 6e 3e 30 20 26  {.  while( n>0 &
324b5 26 20 7a 5b 6e 2d 31 5d 3d 3d 27 20 27 20 29 7b  & z[n-1]==' ' ){
324b6 20 6e 2d 2d 3b 20 7d 0a 20 20 72 65 74 75 72 6e   n--; }.  return
324b7 20 6e 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   n==0;.}../*.** 
324b8 54 68 69 73 20 69 73 20 74 68 65 20 64 65 66 61  This is the defa
324b9 75 6c 74 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75  ult collating fu
324ba 6e 63 74 69 6f 6e 20 6e 61 6d 65 64 20 22 42 49  nction named "BI
324bb 4e 41 52 59 22 20 77 68 69 63 68 20 69 73 20 61  NARY" which is a
324bc 6c 77 61 79 73 0a 2a 2a 20 61 76 61 69 6c 61 62  lways.** availab
324bd 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  le..**.** If the
324be 20 70 61 64 46 6c 61 67 20 61 72 67 75 6d 65 6e   padFlag argumen
324bf 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 74 68  t is not NULL th
324c0 65 6e 20 73 70 61 63 65 20 70 61 64 64 69 6e 67  en space padding
324c1 20 61 74 20 74 68 65 20 65 6e 64 0a 2a 2a 20 6f   at the end.** o
324c2 66 20 73 74 72 69 6e 67 73 20 69 73 20 69 67 6e  f strings is ign
324c3 6f 72 65 64 2e 20 20 54 68 69 73 20 69 6d 70 6c  ored.  This impl
324c4 65 6d 65 6e 74 73 20 74 68 65 20 52 54 52 49 4d  ements the RTRIM
324c5 20 63 6f 6c 6c 61 74 69 6f 6e 2e 0a 2a 2f 0a 73   collation..*/.s
324c6 74 61 74 69 63 20 69 6e 74 20 62 69 6e 43 6f 6c  tatic int binCol
324c7 6c 46 75 6e 63 28 0a 20 20 76 6f 69 64 20 2a 70  lFunc(.  void *p
324c8 61 64 46 6c 61 67 2c 0a 20 20 69 6e 74 20 6e 4b  adFlag,.  int nK
324c9 65 79 31 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20  ey1, const void 
324ca 2a 70 4b 65 79 31 2c 0a 20 20 69 6e 74 20 6e 4b  *pKey1,.  int nK
324cb 65 79 32 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20  ey2, const void 
324cc 2a 70 4b 65 79 32 0a 29 7b 0a 20 20 69 6e 74 20  *pKey2.){.  int 
324cd 72 63 2c 20 6e 3b 0a 20 20 6e 20 3d 20 6e 4b 65  rc, n;.  n = nKe
324ce 79 31 3c 6e 4b 65 79 32 20 3f 20 6e 4b 65 79 31  y1<nKey2 ? nKey1
324cf 20 3a 20 6e 4b 65 79 32 3b 0a 20 20 72 63 20 3d   : nKey2;.  rc =
324d0 20 6d 65 6d 63 6d 70 28 70 4b 65 79 31 2c 20 70   memcmp(pKey1, p
324d1 4b 65 79 32 2c 20 6e 29 3b 0a 20 20 69 66 28 20  Key2, n);.  if( 
324d2 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28  rc==0 ){.    if(
324d3 20 70 61 64 46 6c 61 67 0a 20 20 20 20 20 26 26   padFlag.     &&
324d4 20 61 6c 6c 53 70 61 63 65 73 28 28 28 63 68 61   allSpaces(((cha
324d5 72 2a 29 70 4b 65 79 31 29 2b 6e 2c 20 6e 4b 65  r*)pKey1)+n, nKe
324d6 79 31 2d 6e 29 0a 20 20 20 20 20 26 26 20 61 6c  y1-n).     && al
324d7 6c 53 70 61 63 65 73 28 28 28 63 68 61 72 2a 29  lSpaces(((char*)
324d8 70 4b 65 79 32 29 2b 6e 2c 20 6e 4b 65 79 32 2d  pKey2)+n, nKey2-
324d9 6e 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  n).    ){.      
324da 2f 2a 20 4c 65 61 76 65 20 72 63 20 75 6e 63 68  /* Leave rc unch
324db 61 6e 67 65 64 20 61 74 20 30 20 2a 2f 0a 20 20  anged at 0 */.  
324dc 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
324dd 63 20 3d 20 6e 4b 65 79 31 20 2d 20 6e 4b 65 79  c = nKey1 - nKey
324de 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  2;.    }.  }.  r
324df 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
324e0 2a 2a 20 41 6e 6f 74 68 65 72 20 62 75 69 6c 74  ** Another built
324e1 2d 69 6e 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  -in collating se
324e2 71 75 65 6e 63 65 3a 20 4e 4f 43 41 53 45 2e 20  quence: NOCASE. 
324e3 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 6f 6c 6c  .**.** This coll
324e4 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69  ating sequence i
324e5 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65  s intended to be
324e6 20 75 73 65 64 20 66 6f 72 20 22 63 61 73 65 20   used for "case 
324e7 69 6e 64 65 70 65 6e 64 61 6e 74 0a 2a 2a 20 63  independant.** c
324e8 6f 6d 70 61 72 69 73 6f 6e 22 2e 20 53 51 4c 69  omparison". SQLi
324e9 74 65 27 73 20 6b 6e 6f 77 6c 65 64 67 65 20 6f  te's knowledge o
324ea 66 20 75 70 70 65 72 20 61 6e 64 20 6c 6f 77 65  f upper and lowe
324eb 72 20 63 61 73 65 20 65 71 75 69 76 61 6c 65 6e  r case equivalen
324ec 74 73 0a 2a 2a 20 65 78 74 65 6e 64 73 20 6f 6e  ts.** extends on
324ed 6c 79 20 74 6f 20 74 68 65 20 32 36 20 63 68 61  ly to the 26 cha
324ee 72 61 63 74 65 72 73 20 75 73 65 64 20 69 6e 20  racters used in 
324ef 74 68 65 20 45 6e 67 6c 69 73 68 20 6c 61 6e 67  the English lang
324f0 75 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 74  uage..**.** At t
324f1 68 65 20 6d 6f 6d 65 6e 74 20 74 68 65 72 65 20  he moment there 
324f2 69 73 20 6f 6e 6c 79 20 61 20 55 54 46 2d 38 20  is only a UTF-8 
324f3 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 0a  implementation..
324f4 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f  */.static int no
324f5 63 61 73 65 43 6f 6c 6c 61 74 69 6e 67 46 75 6e  caseCollatingFun
324f6 63 28 0a 20 20 76 6f 69 64 20 2a 4e 6f 74 55 73  c(.  void *NotUs
324f7 65 64 2c 0a 20 20 69 6e 74 20 6e 4b 65 79 31 2c  ed,.  int nKey1,
324f8 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65   const void *pKe
324f9 79 31 2c 0a 20 20 69 6e 74 20 6e 4b 65 79 32 2c  y1,.  int nKey2,
324fa 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65   const void *pKe
324fb 79 32 0a 29 7b 0a 20 20 69 6e 74 20 72 20 3d 20  y2.){.  int r = 
324fc 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
324fd 0a 20 20 20 20 20 20 28 63 6f 6e 73 74 20 63 68  .      (const ch
324fe 61 72 20 2a 29 70 4b 65 79 31 2c 20 28 63 6f 6e  ar *)pKey1, (con
324ff 73 74 20 63 68 61 72 20 2a 29 70 4b 65 79 32 2c  st char *)pKey2,
32500 20 28 6e 4b 65 79 31 3c 6e 4b 65 79 32 29 3f 6e   (nKey1<nKey2)?n
32501 4b 65 79 31 3a 6e 4b 65 79 32 29 3b 0a 20 20 55  Key1:nKey2);.  U
32502 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
32503 4e 6f 74 55 73 65 64 29 3b 0a 20 20 69 66 28 20  NotUsed);.  if( 
32504 30 3d 3d 72 20 29 7b 0a 20 20 20 20 72 20 3d 20  0==r ){.    r = 
32505 6e 4b 65 79 31 2d 6e 4b 65 79 32 3b 0a 20 20 7d  nKey1-nKey2;.  }
32506 0a 20 20 72 65 74 75 72 6e 20 72 3b 0a 7d 0a 0a  .  return r;.}..
32507 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
32508 20 52 4f 57 49 44 20 6f 66 20 74 68 65 20 6d 6f   ROWID of the mo
32509 73 74 20 72 65 63 65 6e 74 20 69 6e 73 65 72 74  st recent insert
3250a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73  .*/.SQLITE_API s
3250b 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 71 6c 69  qlite_int64 sqli
3250c 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f  te3_last_insert_
3250d 72 6f 77 69 64 28 73 71 6c 69 74 65 33 20 2a 64  rowid(sqlite3 *d
3250e 62 29 7b 0a 20 20 72 65 74 75 72 6e 20 64 62 2d  b){.  return db-
3250f 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 7d 0a 0a 2f  >lastRowid;.}../
32510 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
32511 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 6e 67 65  number of change
32512 73 20 69 6e 20 74 68 65 20 6d 6f 73 74 20 72 65  s in the most re
32513 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c  cent call to sql
32514 69 74 65 33 5f 65 78 65 63 28 29 2e 0a 2a 2f 0a  ite3_exec()..*/.
32515 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
32516 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 73  qlite3_changes(s
32517 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 72  qlite3 *db){.  r
32518 65 74 75 72 6e 20 64 62 2d 3e 6e 43 68 61 6e 67  eturn db->nChang
32519 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  e;.}../*.** Retu
3251a 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
3251b 20 63 68 61 6e 67 65 73 20 73 69 6e 63 65 20 74   changes since t
3251c 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  he database hand
3251d 6c 65 20 77 61 73 20 6f 70 65 6e 65 64 2e 0a 2a  le was opened..*
3251e 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
3251f 20 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63   sqlite3_total_c
32520 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 20 2a  hanges(sqlite3 *
32521 64 62 29 7b 0a 20 20 72 65 74 75 72 6e 20 64 62  db){.  return db
32522 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e 67 65 3b 0a  ->nTotalChange;.
32523 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61  }../*.** Close a
32524 6c 6c 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e  ll open savepoin
32525 74 73 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f  ts. This functio
32526 6e 20 6f 6e 6c 79 20 6d 61 6e 69 70 75 6c 61 74  n only manipulat
32527 65 73 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65  es fields of the
32528 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 68 61 6e  .** database han
32529 64 6c 65 20 6f 62 6a 65 63 74 2c 20 69 74 20 64  dle object, it d
3252a 6f 65 73 20 6e 6f 74 20 63 6c 6f 73 65 20 61 6e  oes not close an
3252b 79 20 73 61 76 65 70 6f 69 6e 74 73 20 74 68 61  y savepoints tha
3252c 74 20 6d 61 79 20 62 65 20 6f 70 65 6e 0a 2a 2a  t may be open.**
3252d 20 61 74 20 74 68 65 20 62 2d 74 72 65 65 2f 70   at the b-tree/p
3252e 61 67 65 72 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 53  ager level..*/.S
3252f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
32530 69 64 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53  id sqlite3CloseS
32531 61 76 65 70 6f 69 6e 74 73 28 73 71 6c 69 74 65  avepoints(sqlite
32532 33 20 2a 64 62 29 7b 0a 20 20 77 68 69 6c 65 28  3 *db){.  while(
32533 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20   db->pSavepoint 
32534 29 7b 0a 20 20 20 20 53 61 76 65 70 6f 69 6e 74  ){.    Savepoint
32535 20 2a 70 54 6d 70 20 3d 20 64 62 2d 3e 70 53 61   *pTmp = db->pSa
32536 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20 64 62 2d  vepoint;.    db-
32537 3e 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 70 54  >pSavepoint = pT
32538 6d 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73  mp->pNext;.    s
32539 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
3253a 20 70 54 6d 70 29 3b 0a 20 20 7d 0a 20 20 64 62   pTmp);.  }.  db
3253b 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 30  ->nSavepoint = 0
3253c 3b 0a 20 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65  ;.  db->nStateme
3253d 6e 74 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 69 73  nt = 0;.  db->is
3253e 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70  TransactionSavep
3253f 6f 69 6e 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a  oint = 0;.}../*.
32540 2a 2a 20 43 6c 6f 73 65 20 61 6e 20 65 78 69 73  ** Close an exis
32541 74 69 6e 67 20 53 51 4c 69 74 65 20 64 61 74 61  ting SQLite data
32542 62 61 73 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  base.*/.SQLITE_A
32543 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
32544 6c 6f 73 65 28 73 71 6c 69 74 65 33 20 2a 64 62  lose(sqlite3 *db
32545 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 69  ){.  HashElem *i
32546 3b 0a 20 20 69 6e 74 20 6a 3b 0a 0a 20 20 69 66  ;.  int j;..  if
32547 28 20 21 64 62 20 29 7b 0a 20 20 20 20 72 65 74  ( !db ){.    ret
32548 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
32549 20 7d 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65   }.  if( !sqlite
3254a 33 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b  3SafetyCheckSick
3254b 4f 72 4f 6b 28 64 62 29 20 29 7b 0a 20 20 20 20  OrOk(db) ){.    
3254c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
3254d 53 55 53 45 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  SUSE;.  }.  sqli
3254e 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
3254f 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 0a 20 20 73  db->mutex);..  s
32550 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72  qlite3ResetInter
32551 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29  nalSchema(db, 0)
32552 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 20 74 72 61  ;..  /* If a tra
32553 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e  nsaction is open
32554 2c 20 74 68 65 20 52 65 73 65 74 49 6e 74 65 72  , the ResetInter
32555 6e 61 6c 53 63 68 65 6d 61 28 29 20 63 61 6c 6c  nalSchema() call
32556 20 61 62 6f 76 65 0a 20 20 2a 2a 20 77 69 6c 6c   above.  ** will
32557 20 6e 6f 74 20 68 61 76 65 20 63 61 6c 6c 65 64   not have called
32558 20 74 68 65 20 78 44 69 73 63 6f 6e 6e 65 63 74   the xDisconnect
32559 28 29 20 6d 65 74 68 6f 64 20 6f 6e 20 61 6e 79  () method on any
3255a 20 76 69 72 74 75 61 6c 0a 20 20 2a 2a 20 74 61   virtual.  ** ta
3255b 62 6c 65 73 20 69 6e 20 74 68 65 20 64 62 2d 3e  bles in the db->
3255c 61 56 54 72 61 6e 73 5b 5d 20 61 72 72 61 79 2e  aVTrans[] array.
3255d 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   The following s
3255e 71 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 61  qlite3VtabRollba
3255f 63 6b 28 29 0a 20 20 2a 2a 20 63 61 6c 6c 20 77  ck().  ** call w
32560 69 6c 6c 20 64 6f 20 73 6f 2e 20 57 65 20 6e 65  ill do so. We ne
32561 65 64 20 74 6f 20 64 6f 20 74 68 69 73 20 62 65  ed to do this be
32562 66 6f 72 65 20 74 68 65 20 63 68 65 63 6b 20 66  fore the check f
32563 6f 72 20 61 63 74 69 76 65 0a 20 20 2a 2a 20 53  or active.  ** S
32564 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 62 65  QL statements be
32565 6c 6f 77 2c 20 61 73 20 74 68 65 20 76 2d 74 61  low, as the v-ta
32566 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ble implementati
32567 6f 6e 20 6d 61 79 20 62 65 20 73 74 6f 72 69 6e  on may be storin
32568 67 0a 20 20 2a 2a 20 73 6f 6d 65 20 70 72 65 70  g.  ** some prep
32569 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20  ared statements 
3256a 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 20 20 2a 2f  internally..  */
3256b 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62 52 6f  .  sqlite3VtabRo
3256c 6c 6c 62 61 63 6b 28 64 62 29 3b 0a 0a 20 20 2f  llback(db);..  /
3256d 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 61  * If there are a
3256e 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 56  ny outstanding V
3256f 4d 73 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54  Ms, return SQLIT
32570 45 5f 42 55 53 59 2e 20 2a 2f 0a 20 20 69 66 28  E_BUSY. */.  if(
32571 20 64 62 2d 3e 70 56 64 62 65 20 29 7b 0a 20 20   db->pVdbe ){.  
32572 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
32573 62 2c 20 53 51 4c 49 54 45 5f 42 55 53 59 2c 20  b, SQLITE_BUSY, 
32574 0a 20 20 20 20 20 20 20 20 22 75 6e 61 62 6c 65  .        "unable
32575 20 74 6f 20 63 6c 6f 73 65 20 64 75 65 20 74 6f   to close due to
32576 20 75 6e 66 69 6e 61 6c 69 73 65 64 20 73 74 61   unfinalised sta
32577 74 65 6d 65 6e 74 73 22 29 3b 0a 20 20 20 20 73  tements");.    s
32578 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
32579 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
3257a 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
3257b 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20 61 73 73  _BUSY;.  }.  ass
3257c 65 72 74 28 20 73 71 6c 69 74 65 33 53 61 66 65  ert( sqlite3Safe
3257d 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28  tyCheckSickOrOk(
3257e 64 62 29 20 29 3b 0a 0a 20 20 66 6f 72 28 6a 3d  db) );..  for(j=
3257f 30 3b 20 6a 3c 64 62 2d 3e 6e 44 62 3b 20 6a 2b  0; j<db->nDb; j+
32580 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70  +){.    Btree *p
32581 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 6a 5d 2e  Bt = db->aDb[j].
32582 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70 42 74  pBt;.    if( pBt
32583 20 26 26 20 73 71 6c 69 74 65 33 42 74 72 65 65   && sqlite3Btree
32584 49 73 49 6e 42 61 63 6b 75 70 28 70 42 74 29 20  IsInBackup(pBt) 
32585 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
32586 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45  Error(db, SQLITE
32587 5f 42 55 53 59 2c 20 0a 20 20 20 20 20 20 20 20  _BUSY, .        
32588 20 20 22 75 6e 61 62 6c 65 20 74 6f 20 63 6c 6f    "unable to clo
32589 73 65 20 64 75 65 20 74 6f 20 75 6e 66 69 6e 69  se due to unfini
3258a 73 68 65 64 20 62 61 63 6b 75 70 20 6f 70 65 72  shed backup oper
3258b 61 74 69 6f 6e 22 29 3b 0a 20 20 20 20 20 20 73  ation");.      s
3258c 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
3258d 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
3258e 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
3258f 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 0a 20  TE_BUSY;.    }. 
32590 20 7d 0a 0a 20 20 2f 2a 20 46 72 65 65 20 61 6e   }..  /* Free an
32591 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 53 61  y outstanding Sa
32592 76 65 70 6f 69 6e 74 20 73 74 72 75 63 74 75 72  vepoint structur
32593 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  es. */.  sqlite3
32594 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e 74 73 28  CloseSavepoints(
32595 64 62 29 3b 0a 0a 20 20 66 6f 72 28 6a 3d 30 3b  db);..  for(j=0;
32596 20 6a 3c 64 62 2d 3e 6e 44 62 3b 20 6a 2b 2b 29   j<db->nDb; j++)
32597 7b 0a 20 20 20 20 73 74 72 75 63 74 20 44 62 20  {.    struct Db 
32598 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b  *pDb = &db->aDb[
32599 6a 5d 3b 0a 20 20 20 20 69 66 28 20 70 44 62 2d  j];.    if( pDb-
3259a 3e 70 42 74 20 29 7b 0a 20 20 20 20 20 20 73 71  >pBt ){.      sq
3259b 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28  lite3BtreeClose(
3259c 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20 20  pDb->pBt);.     
3259d 20 70 44 62 2d 3e 70 42 74 20 3d 20 30 3b 0a 20   pDb->pBt = 0;. 
3259e 20 20 20 20 20 69 66 28 20 6a 21 3d 31 20 29 7b       if( j!=1 ){
3259f 0a 20 20 20 20 20 20 20 20 70 44 62 2d 3e 70 53  .        pDb->pS
325a0 63 68 65 6d 61 20 3d 20 30 3b 0a 20 20 20 20 20  chema = 0;.     
325a1 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73   }.    }.  }.  s
325a2 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72  qlite3ResetInter
325a3 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29  nalSchema(db, 0)
325a4 3b 0a 0a 20 20 2f 2a 20 54 65 6c 6c 20 74 68 65  ;..  /* Tell the
325a5 20 63 6f 64 65 20 69 6e 20 6e 6f 74 69 66 79 2e   code in notify.
325a6 63 20 74 68 61 74 20 74 68 65 20 63 6f 6e 6e 65  c that the conne
325a7 63 74 69 6f 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20  ction no longer 
325a8 68 6f 6c 64 73 20 61 6e 79 0a 20 20 2a 2a 20 6c  holds any.  ** l
325a9 6f 63 6b 73 20 61 6e 64 20 64 6f 65 73 20 6e 6f  ocks and does no
325aa 74 20 72 65 71 75 69 72 65 20 61 6e 79 20 66 75  t require any fu
325ab 72 74 68 65 72 20 75 6e 6c 6f 63 6b 2d 6e 6f 74  rther unlock-not
325ac 69 66 79 20 63 61 6c 6c 62 61 63 6b 73 2e 0a 20  ify callbacks.. 
325ad 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 43 6f 6e   */.  sqlite3Con
325ae 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64 28 64 62  nectionClosed(db
325af 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62  );..  assert( db
325b0 2d 3e 6e 44 62 3c 3d 32 20 29 3b 0a 20 20 61 73  ->nDb<=2 );.  as
325b1 73 65 72 74 28 20 64 62 2d 3e 61 44 62 3d 3d 64  sert( db->aDb==d
325b2 62 2d 3e 61 44 62 53 74 61 74 69 63 20 29 3b 0a  b->aDbStatic );.
325b3 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 41 72 72    for(j=0; j<Arr
325b4 61 79 53 69 7a 65 28 64 62 2d 3e 61 46 75 6e 63  aySize(db->aFunc
325b5 2e 61 29 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 46  .a); j++){.    F
325b6 75 6e 63 44 65 66 20 2a 70 4e 65 78 74 2c 20 2a  uncDef *pNext, *
325b7 70 48 61 73 68 2c 20 2a 70 3b 0a 20 20 20 20 66  pHash, *p;.    f
325b8 6f 72 28 70 3d 64 62 2d 3e 61 46 75 6e 63 2e 61  or(p=db->aFunc.a
325b9 5b 6a 5d 3b 20 70 3b 20 70 3d 70 48 61 73 68 29  [j]; p; p=pHash)
325ba 7b 0a 20 20 20 20 20 20 70 48 61 73 68 20 3d 20  {.      pHash = 
325bb 70 2d 3e 70 48 61 73 68 3b 0a 20 20 20 20 20 20  p->pHash;.      
325bc 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 20  while( p ){.    
325bd 20 20 20 20 70 4e 65 78 74 20 3d 20 70 2d 3e 70      pNext = p->p
325be 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 73 71  Next;.        sq
325bf 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
325c0 70 29 3b 0a 20 20 20 20 20 20 20 20 70 20 3d 20  p);.        p = 
325c1 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20  pNext;.      }. 
325c2 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69     }.  }.  for(i
325c3 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74  =sqliteHashFirst
325c4 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 29 3b  (&db->aCollSeq);
325c5 20 69 3b 20 69 3d 73 71 6c 69 74 65 48 61 73 68   i; i=sqliteHash
325c6 4e 65 78 74 28 69 29 29 7b 0a 20 20 20 20 43 6f  Next(i)){.    Co
325c7 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 28  llSeq *pColl = (
325c8 43 6f 6c 6c 53 65 71 20 2a 29 73 71 6c 69 74 65  CollSeq *)sqlite
325c9 48 61 73 68 44 61 74 61 28 69 29 3b 0a 20 20 20  HashData(i);.   
325ca 20 2f 2a 20 49 6e 76 6f 6b 65 20 61 6e 79 20 64   /* Invoke any d
325cb 65 73 74 72 75 63 74 6f 72 73 20 72 65 67 69 73  estructors regis
325cc 74 65 72 65 64 20 66 6f 72 20 63 6f 6c 6c 61 74  tered for collat
325cd 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 75 73 65  ion sequence use
325ce 72 20 64 61 74 61 2e 20 2a 2f 0a 20 20 20 20 66  r data. */.    f
325cf 6f 72 28 6a 3d 30 3b 20 6a 3c 33 3b 20 6a 2b 2b  or(j=0; j<3; j++
325d0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43 6f  ){.      if( pCo
325d1 6c 6c 5b 6a 5d 2e 78 44 65 6c 20 29 7b 0a 20 20  ll[j].xDel ){.  
325d2 20 20 20 20 20 20 70 43 6f 6c 6c 5b 6a 5d 2e 78        pColl[j].x
325d3 44 65 6c 28 70 43 6f 6c 6c 5b 6a 5d 2e 70 55 73  Del(pColl[j].pUs
325d4 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  er);.      }.   
325d5 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62   }.    sqlite3Db
325d6 46 72 65 65 28 64 62 2c 20 70 43 6f 6c 6c 29 3b  Free(db, pColl);
325d7 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 48 61  .  }.  sqlite3Ha
325d8 73 68 43 6c 65 61 72 28 26 64 62 2d 3e 61 43 6f  shClear(&db->aCo
325d9 6c 6c 53 65 71 29 3b 0a 23 69 66 6e 64 65 66 20  llSeq);.#ifndef 
325da 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
325db 55 41 4c 54 41 42 4c 45 0a 20 20 66 6f 72 28 69  UALTABLE.  for(i
325dc 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74  =sqliteHashFirst
325dd 28 26 64 62 2d 3e 61 4d 6f 64 75 6c 65 29 3b 20  (&db->aModule); 
325de 69 3b 20 69 3d 73 71 6c 69 74 65 48 61 73 68 4e  i; i=sqliteHashN
325df 65 78 74 28 69 29 29 7b 0a 20 20 20 20 4d 6f 64  ext(i)){.    Mod
325e0 75 6c 65 20 2a 70 4d 6f 64 20 3d 20 28 4d 6f 64  ule *pMod = (Mod
325e1 75 6c 65 20 2a 29 73 71 6c 69 74 65 48 61 73 68  ule *)sqliteHash
325e2 44 61 74 61 28 69 29 3b 0a 20 20 20 20 69 66 28  Data(i);.    if(
325e3 20 70 4d 6f 64 2d 3e 78 44 65 73 74 72 6f 79 20   pMod->xDestroy 
325e4 29 7b 0a 20 20 20 20 20 20 70 4d 6f 64 2d 3e 78  ){.      pMod->x
325e5 44 65 73 74 72 6f 79 28 70 4d 6f 64 2d 3e 70 41  Destroy(pMod->pA
325e6 75 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  ux);.    }.    s
325e7 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
325e8 20 70 4d 6f 64 29 3b 0a 20 20 7d 0a 20 20 73 71   pMod);.  }.  sq
325e9 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 26  lite3HashClear(&
325ea 64 62 2d 3e 61 4d 6f 64 75 6c 65 29 3b 0a 23 65  db->aModule);.#e
325eb 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33 45  ndif..  sqlite3E
325ec 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f  rror(db, SQLITE_
325ed 4f 4b 2c 20 30 29 3b 20 2f 2a 20 44 65 61 6c 6c  OK, 0); /* Deall
325ee 6f 63 61 74 65 73 20 61 6e 79 20 63 61 63 68 65  ocates any cache
325ef 64 20 65 72 72 6f 72 20 73 74 72 69 6e 67 73 2e  d error strings.
325f0 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 70 45   */.  if( db->pE
325f1 72 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  rr ){.    sqlite
325f2 33 56 61 6c 75 65 46 72 65 65 28 64 62 2d 3e 70  3ValueFree(db->p
325f3 45 72 72 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  Err);.  }.  sqli
325f4 74 65 33 43 6c 6f 73 65 45 78 74 65 6e 73 69 6f  te3CloseExtensio
325f5 6e 73 28 64 62 29 3b 0a 0a 20 20 64 62 2d 3e 6d  ns(db);..  db->m
325f6 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41  agic = SQLITE_MA
325f7 47 49 43 5f 45 52 52 4f 52 3b 0a 0a 20 20 2f 2a  GIC_ERROR;..  /*
325f8 20 54 68 65 20 74 65 6d 70 2d 64 61 74 61 62 61   The temp-databa
325f9 73 65 20 73 63 68 65 6d 61 20 69 73 20 61 6c 6c  se schema is all
325fa 6f 63 61 74 65 64 20 64 69 66 66 65 72 65 6e 74  ocated different
325fb 6c 79 20 66 72 6f 6d 20 74 68 65 20 6f 74 68 65  ly from the othe
325fc 72 20 73 63 68 65 6d 61 0a 20 20 2a 2a 20 6f 62  r schema.  ** ob
325fd 6a 65 63 74 73 20 28 75 73 69 6e 67 20 73 71 6c  jects (using sql
325fe 69 74 65 4d 61 6c 6c 6f 63 28 29 20 64 69 72 65  iteMalloc() dire
325ff 63 74 6c 79 2c 20 69 6e 73 74 65 61 64 20 6f 66  ctly, instead of
32600 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 63 68   sqlite3BtreeSch
32601 65 6d 61 28 29 29 2e 0a 20 20 2a 2a 20 53 6f 20  ema())..  ** So 
32602 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 66  it needs to be f
32603 72 65 65 64 20 68 65 72 65 2e 20 54 6f 64 6f 3a  reed here. Todo:
32604 20 57 68 79 20 6e 6f 74 20 72 6f 6c 6c 20 74 68   Why not roll th
32605 65 20 74 65 6d 70 20 73 63 68 65 6d 61 20 69 6e  e temp schema in
32606 74 6f 0a 20 20 2a 2a 20 74 68 65 20 73 61 6d 65  to.  ** the same
32607 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20   sqliteMalloc() 
32608 61 73 20 74 68 65 20 6f 6e 65 20 74 68 61 74 20  as the one that 
32609 61 6c 6c 6f 63 61 74 65 73 20 74 68 65 20 64 61  allocates the da
3260a 74 61 62 61 73 65 20 0a 20 20 2a 2a 20 73 74 72  tabase .  ** str
3260b 75 63 74 75 72 65 3f 0a 20 20 2a 2f 0a 20 20 73  ucture?.  */.  s
3260c 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
3260d 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68   db->aDb[1].pSch
3260e 65 6d 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  ema);.  sqlite3_
3260f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
32610 6d 75 74 65 78 29 3b 0a 20 20 64 62 2d 3e 6d 61  mutex);.  db->ma
32611 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47  gic = SQLITE_MAG
32612 49 43 5f 43 4c 4f 53 45 44 3b 0a 20 20 73 71 6c  IC_CLOSED;.  sql
32613 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28  ite3_mutex_free(
32614 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 61 73  db->mutex);.  as
32615 73 65 72 74 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73  sert( db->lookas
32616 69 64 65 2e 6e 4f 75 74 3d 3d 30 20 29 3b 20 20  ide.nOut==0 );  
32617 2f 2a 20 46 61 69 6c 73 20 6f 6e 20 61 20 6c 6f  /* Fails on a lo
32618 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 6c  okaside memory l
32619 65 61 6b 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d  eak */.  if( db-
3261a 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 4d 61 6c 6c  >lookaside.bMall
3261b 6f 63 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69  oced ){.    sqli
3261c 74 65 33 5f 66 72 65 65 28 64 62 2d 3e 6c 6f 6f  te3_free(db->loo
3261d 6b 61 73 69 64 65 2e 70 53 74 61 72 74 29 3b 0a  kaside.pStart);.
3261e 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72    }.  sqlite3_fr
3261f 65 65 28 64 62 29 3b 0a 20 20 72 65 74 75 72 6e  ee(db);.  return
32620 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
32621 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 6c  *.** Rollback al
32622 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73  l database files
32623 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
32624 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
32625 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 73 71 6c 69  RollbackAll(sqli
32626 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20  te3 *db){.  int 
32627 69 3b 0a 20 20 69 6e 74 20 69 6e 54 72 61 6e 73  i;.  int inTrans
32628 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20   = 0;.  assert( 
32629 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
3262a 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  ld(db->mutex) );
3262b 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42  .  sqlite3BeginB
3262c 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20  enignMalloc();. 
3262d 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
3262e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  nDb; i++){.    i
3262f 66 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42  f( db->aDb[i].pB
32630 74 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73  t ){.      if( s
32631 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54  qlite3BtreeIsInT
32632 72 61 6e 73 28 64 62 2d 3e 61 44 62 5b 69 5d 2e  rans(db->aDb[i].
32633 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20  pBt) ){.        
32634 69 6e 54 72 61 6e 73 20 3d 20 31 3b 0a 20 20 20  inTrans = 1;.   
32635 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
32636 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28  e3BtreeRollback(
32637 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 3b  db->aDb[i].pBt);
32638 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69  .      db->aDb[i
32639 5d 2e 69 6e 54 72 61 6e 73 20 3d 20 30 3b 0a 20  ].inTrans = 0;. 
3263a 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74     }.  }.  sqlit
3263b 65 33 56 74 61 62 52 6f 6c 6c 62 61 63 6b 28 64  e3VtabRollback(d
3263c 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 6e 64  b);.  sqlite3End
3263d 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a  BenignMalloc();.
3263e 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73  .  if( db->flags
3263f 26 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68  &SQLITE_InternCh
32640 61 6e 67 65 73 20 29 7b 0a 20 20 20 20 73 71 6c  anges ){.    sql
32641 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61 72  ite3ExpirePrepar
32642 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29  edStatements(db)
32643 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 73  ;.    sqlite3Res
32644 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61  etInternalSchema
32645 28 64 62 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20  (db, 0);.  }..  
32646 2f 2a 20 49 66 20 6f 6e 65 20 68 61 73 20 62 65  /* If one has be
32647 65 6e 20 63 6f 6e 66 69 67 75 72 65 64 2c 20 69  en configured, i
32648 6e 76 6f 6b 65 20 74 68 65 20 72 6f 6c 6c 62 61  nvoke the rollba
32649 63 6b 2d 68 6f 6f 6b 20 63 61 6c 6c 62 61 63 6b  ck-hook callback
3264a 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 78 52   */.  if( db->xR
3264b 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b 20  ollbackCallback 
3264c 26 26 20 28 69 6e 54 72 61 6e 73 20 7c 7c 20 21  && (inTrans || !
3264d 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 29 20  db->autoCommit) 
3264e 29 7b 0a 20 20 20 20 64 62 2d 3e 78 52 6f 6c 6c  ){.    db->xRoll
3264f 62 61 63 6b 43 61 6c 6c 62 61 63 6b 28 64 62 2d  backCallback(db-
32650 3e 70 52 6f 6c 6c 62 61 63 6b 41 72 67 29 3b 0a  >pRollbackArg);.
32651 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74    }.}../*.** Ret
32652 75 72 6e 20 61 20 73 74 61 74 69 63 20 73 74 72  urn a static str
32653 69 6e 67 20 74 68 61 74 20 64 65 73 63 72 69 62  ing that describ
32654 65 73 20 74 68 65 20 6b 69 6e 64 20 6f 66 20 65  es the kind of e
32655 72 72 6f 72 20 73 70 65 63 69 66 69 65 64 20 69  rror specified i
32656 6e 20 74 68 65 0a 2a 2a 20 61 72 67 75 6d 65 6e  n the.** argumen
32657 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
32658 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20  VATE const char 
32659 2a 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 69  *sqlite3ErrStr(i
3265a 6e 74 20 72 63 29 7b 0a 20 20 73 74 61 74 69 63  nt rc){.  static
3265b 20 63 6f 6e 73 74 20 63 68 61 72 2a 20 63 6f 6e   const char* con
3265c 73 74 20 61 4d 73 67 5b 5d 20 3d 20 7b 0a 20 20  st aMsg[] = {.  
3265d 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 20    /* SQLITE_OK  
3265e 20 20 20 20 20 20 20 20 2a 2f 20 22 6e 6f 74 20          */ "not 
3265f 61 6e 20 65 72 72 6f 72 22 2c 0a 20 20 20 20 2f  an error",.    /
32660 2a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 20  * SQLITE_ERROR  
32661 20 20 20 20 20 2a 2f 20 22 53 51 4c 20 6c 6f 67       */ "SQL log
32662 69 63 20 65 72 72 6f 72 20 6f 72 20 6d 69 73 73  ic error or miss
32663 69 6e 67 20 64 61 74 61 62 61 73 65 22 2c 0a 20  ing database",. 
32664 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 49 4e 54     /* SQLITE_INT
32665 45 52 4e 41 4c 20 20 20 20 2a 2f 20 30 2c 0a 20  ERNAL    */ 0,. 
32666 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 50 45 52     /* SQLITE_PER
32667 4d 20 20 20 20 20 20 20 20 2a 2f 20 22 61 63 63  M        */ "acc
32668 65 73 73 20 70 65 72 6d 69 73 73 69 6f 6e 20 64  ess permission d
32669 65 6e 69 65 64 22 2c 0a 20 20 20 20 2f 2a 20 53  enied",.    /* S
3266a 51 4c 49 54 45 5f 41 42 4f 52 54 20 20 20 20 20  QLITE_ABORT     
3266b 20 20 2a 2f 20 22 63 61 6c 6c 62 61 63 6b 20 72    */ "callback r
3266c 65 71 75 65 73 74 65 64 20 71 75 65 72 79 20 61  equested query a
3266d 62 6f 72 74 22 2c 0a 20 20 20 20 2f 2a 20 53 51  bort",.    /* SQ
3266e 4c 49 54 45 5f 42 55 53 59 20 20 20 20 20 20 20  LITE_BUSY       
3266f 20 2a 2f 20 22 64 61 74 61 62 61 73 65 20 69 73   */ "database is
32670 20 6c 6f 63 6b 65 64 22 2c 0a 20 20 20 20 2f 2a   locked",.    /*
32671 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 20   SQLITE_LOCKED  
32672 20 20 20 20 2a 2f 20 22 64 61 74 61 62 61 73 65      */ "database
32673 20 74 61 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64   table is locked
32674 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45  ",.    /* SQLITE
32675 5f 4e 4f 4d 45 4d 20 20 20 20 20 20 20 2a 2f 20  _NOMEM       */ 
32676 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 2c  "out of memory",
32677 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 52  .    /* SQLITE_R
32678 45 41 44 4f 4e 4c 59 20 20 20 20 2a 2f 20 22 61  EADONLY    */ "a
32679 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 20  ttempt to write 
3267a 61 20 72 65 61 64 6f 6e 6c 79 20 64 61 74 61 62  a readonly datab
3267b 61 73 65 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c  ase",.    /* SQL
3267c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 20 20 20  ITE_INTERRUPT   
3267d 2a 2f 20 22 69 6e 74 65 72 72 75 70 74 65 64 22  */ "interrupted"
3267e 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ,.    /* SQLITE_
3267f 49 4f 45 52 52 20 20 20 20 20 20 20 2a 2f 20 22  IOERR       */ "
32680 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 22 2c  disk I/O error",
32681 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 43  .    /* SQLITE_C
32682 4f 52 52 55 50 54 20 20 20 20 20 2a 2f 20 22 64  ORRUPT     */ "d
32683 61 74 61 62 61 73 65 20 64 69 73 6b 20 69 6d 61  atabase disk ima
32684 67 65 20 69 73 20 6d 61 6c 66 6f 72 6d 65 64 22  ge is malformed"
32685 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ,.    /* SQLITE_
32686 4e 4f 54 46 4f 55 4e 44 20 20 20 20 2a 2f 20 30  NOTFOUND    */ 0
32687 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ,.    /* SQLITE_
32688 46 55 4c 4c 20 20 20 20 20 20 20 20 2a 2f 20 22  FULL        */ "
32689 64 61 74 61 62 61 73 65 20 6f 72 20 64 69 73 6b  database or disk
3268a 20 69 73 20 66 75 6c 6c 22 2c 0a 20 20 20 20 2f   is full",.    /
3268b 2a 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45  * SQLITE_CANTOPE
3268c 4e 20 20 20 20 2a 2f 20 22 75 6e 61 62 6c 65 20  N    */ "unable 
3268d 74 6f 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65  to open database
3268e 20 66 69 6c 65 22 2c 0a 20 20 20 20 2f 2a 20 53   file",.    /* S
3268f 51 4c 49 54 45 5f 50 52 4f 54 4f 43 4f 4c 20 20  QLITE_PROTOCOL  
32690 20 20 2a 2f 20 30 2c 0a 20 20 20 20 2f 2a 20 53    */ 0,.    /* S
32691 51 4c 49 54 45 5f 45 4d 50 54 59 20 20 20 20 20  QLITE_EMPTY     
32692 20 20 2a 2f 20 22 74 61 62 6c 65 20 63 6f 6e 74    */ "table cont
32693 61 69 6e 73 20 6e 6f 20 64 61 74 61 22 2c 0a 20  ains no data",. 
32694 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 53 43 48     /* SQLITE_SCH
32695 45 4d 41 20 20 20 20 20 20 2a 2f 20 22 64 61 74  EMA      */ "dat
32696 61 62 61 73 65 20 73 63 68 65 6d 61 20 68 61 73  abase schema has
32697 20 63 68 61 6e 67 65 64 22 2c 0a 20 20 20 20 2f   changed",.    /
32698 2a 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20  * SQLITE_TOOBIG 
32699 20 20 20 20 20 2a 2f 20 22 73 74 72 69 6e 67 20       */ "string 
3269a 6f 72 20 62 6c 6f 62 20 74 6f 6f 20 62 69 67 22  or blob too big"
3269b 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ,.    /* SQLITE_
3269c 43 4f 4e 53 54 52 41 49 4e 54 20 20 2a 2f 20 22  CONSTRAINT  */ "
3269d 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65  constraint faile
3269e 64 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54  d",.    /* SQLIT
3269f 45 5f 4d 49 53 4d 41 54 43 48 20 20 20 20 2a 2f  E_MISMATCH    */
326a0 20 22 64 61 74 61 74 79 70 65 20 6d 69 73 6d 61   "datatype misma
326a1 74 63 68 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c  tch",.    /* SQL
326a2 49 54 45 5f 4d 49 53 55 53 45 20 20 20 20 20 20  ITE_MISUSE      
326a3 2a 2f 20 22 6c 69 62 72 61 72 79 20 72 6f 75 74  */ "library rout
326a4 69 6e 65 20 63 61 6c 6c 65 64 20 6f 75 74 20 6f  ine called out o
326a5 66 20 73 65 71 75 65 6e 63 65 22 2c 0a 20 20 20  f sequence",.   
326a6 20 2f 2a 20 53 51 4c 49 54 45 5f 4e 4f 4c 46 53   /* SQLITE_NOLFS
326a7 20 20 20 20 20 20 20 2a 2f 20 22 6c 61 72 67 65         */ "large
326a8 20 66 69 6c 65 20 73 75 70 70 6f 72 74 20 69 73   file support is
326a9 20 64 69 73 61 62 6c 65 64 22 2c 0a 20 20 20 20   disabled",.    
326aa 2f 2a 20 53 51 4c 49 54 45 5f 41 55 54 48 20 20  /* SQLITE_AUTH  
326ab 20 20 20 20 20 20 2a 2f 20 22 61 75 74 68 6f 72        */ "author
326ac 69 7a 61 74 69 6f 6e 20 64 65 6e 69 65 64 22 2c  ization denied",
326ad 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 46  .    /* SQLITE_F
326ae 4f 52 4d 41 54 20 20 20 20 20 20 2a 2f 20 22 61  ORMAT      */ "a
326af 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73  uxiliary databas
326b0 65 20 66 6f 72 6d 61 74 20 65 72 72 6f 72 22 2c  e format error",
326b1 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 52  .    /* SQLITE_R
326b2 41 4e 47 45 20 20 20 20 20 20 20 2a 2f 20 22 62  ANGE       */ "b
326b3 69 6e 64 20 6f 72 20 63 6f 6c 75 6d 6e 20 69 6e  ind or column in
326b4 64 65 78 20 6f 75 74 20 6f 66 20 72 61 6e 67 65  dex out of range
326b5 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45  ",.    /* SQLITE
326b6 5f 4e 4f 54 41 44 42 20 20 20 20 20 20 2a 2f 20  _NOTADB      */ 
326b7 22 66 69 6c 65 20 69 73 20 65 6e 63 72 79 70 74  "file is encrypt
326b8 65 64 20 6f 72 20 69 73 20 6e 6f 74 20 61 20 64  ed or is not a d
326b9 61 74 61 62 61 73 65 22 2c 0a 20 20 7d 3b 0a 20  atabase",.  };. 
326ba 20 72 63 20 26 3d 20 30 78 66 66 3b 0a 20 20 69   rc &= 0xff;.  i
326bb 66 28 20 41 4c 57 41 59 53 28 72 63 3e 3d 30 29  f( ALWAYS(rc>=0)
326bc 20 26 26 20 72 63 3c 28 69 6e 74 29 28 73 69 7a   && rc<(int)(siz
326bd 65 6f 66 28 61 4d 73 67 29 2f 73 69 7a 65 6f 66  eof(aMsg)/sizeof
326be 28 61 4d 73 67 5b 30 5d 29 29 20 26 26 20 61 4d  (aMsg[0])) && aM
326bf 73 67 5b 72 63 5d 21 3d 30 20 29 7b 0a 20 20 20  sg[rc]!=0 ){.   
326c0 20 72 65 74 75 72 6e 20 61 4d 73 67 5b 72 63 5d   return aMsg[rc]
326c1 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
326c2 65 74 75 72 6e 20 22 75 6e 6b 6e 6f 77 6e 20 65  eturn "unknown e
326c3 72 72 6f 72 22 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  rror";.  }.}../*
326c4 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
326c5 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 62 75   implements a bu
326c6 73 79 20 63 61 6c 6c 62 61 63 6b 20 74 68 61 74  sy callback that
326c7 20 73 6c 65 65 70 73 20 61 6e 64 20 74 72 69 65   sleeps and trie
326c8 73 0a 2a 2a 20 61 67 61 69 6e 20 75 6e 74 69 6c  s.** again until
326c9 20 61 20 74 69 6d 65 6f 75 74 20 76 61 6c 75 65   a timeout value
326ca 20 69 73 20 72 65 61 63 68 65 64 2e 20 20 54 68   is reached.  Th
326cb 65 20 74 69 6d 65 6f 75 74 20 76 61 6c 75 65 20  e timeout value 
326cc 69 73 0a 2a 2a 20 61 6e 20 69 6e 74 65 67 65 72  is.** an integer
326cd 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6c 6c 69   number of milli
326ce 73 65 63 6f 6e 64 73 20 70 61 73 73 65 64 20 69  seconds passed i
326cf 6e 20 61 73 20 74 68 65 20 66 69 72 73 74 0a 2a  n as the first.*
326d0 2a 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73  * argument..*/.s
326d1 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65  tatic int sqlite
326d2 44 65 66 61 75 6c 74 42 75 73 79 43 61 6c 6c 62  DefaultBusyCallb
326d3 61 63 6b 28 0a 20 76 6f 69 64 20 2a 70 74 72 2c  ack(. void *ptr,
326d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
326d5 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * Database conne
326d6 63 74 69 6f 6e 20 2a 2f 0a 20 69 6e 74 20 63 6f  ction */. int co
326d7 75 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20  unt             
326d8 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
326d9 74 69 6d 65 73 20 74 61 62 6c 65 20 68 61 73 20  times table has 
326da 62 65 65 6e 20 62 75 73 79 20 2a 2f 0a 29 7b 0a  been busy */.){.
326db 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
326dc 4e 20 7c 7c 20 28 64 65 66 69 6e 65 64 28 48 41  N || (defined(HA
326dd 56 45 5f 55 53 4c 45 45 50 29 20 26 26 20 48 41  VE_USLEEP) && HA
326de 56 45 5f 55 53 4c 45 45 50 29 0a 20 20 73 74 61  VE_USLEEP).  sta
326df 74 69 63 20 63 6f 6e 73 74 20 75 38 20 64 65 6c  tic const u8 del
326e0 61 79 73 5b 5d 20 3d 0a 20 20 20 20 20 7b 20 31  ays[] =.     { 1
326e1 2c 20 32 2c 20 35 2c 20 31 30 2c 20 31 35 2c 20  , 2, 5, 10, 15, 
326e2 32 30 2c 20 32 35 2c 20 32 35 2c 20 20 32 35 2c  20, 25, 25,  25,
326e3 20 20 35 30 2c 20 20 35 30 2c 20 31 30 30 20 7d    50,  50, 100 }
326e4 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  ;.  static const
326e5 20 75 38 20 74 6f 74 61 6c 73 5b 5d 20 3d 0a 20   u8 totals[] =. 
326e6 20 20 20 20 7b 20 30 2c 20 31 2c 20 33 2c 20 20      { 0, 1, 3,  
326e7 38 2c 20 31 38 2c 20 33 33 2c 20 35 33 2c 20 37  8, 18, 33, 53, 7
326e8 38 2c 20 31 30 33 2c 20 31 32 38 2c 20 31 37 38  8, 103, 128, 178
326e9 2c 20 32 32 38 20 7d 3b 0a 23 20 64 65 66 69 6e  , 228 };.# defin
326ea 65 20 4e 44 45 4c 41 59 20 28 73 69 7a 65 6f 66  e NDELAY (sizeof
326eb 28 64 65 6c 61 79 73 29 2f 73 69 7a 65 6f 66 28  (delays)/sizeof(
326ec 64 65 6c 61 79 73 5b 30 5d 29 29 0a 20 20 73 71  delays[0])).  sq
326ed 6c 69 74 65 33 20 2a 64 62 20 3d 20 28 73 71 6c  lite3 *db = (sql
326ee 69 74 65 33 20 2a 29 70 74 72 3b 0a 20 20 69 6e  ite3 *)ptr;.  in
326ef 74 20 74 69 6d 65 6f 75 74 20 3d 20 64 62 2d 3e  t timeout = db->
326f0 62 75 73 79 54 69 6d 65 6f 75 74 3b 0a 20 20 69  busyTimeout;.  i
326f1 6e 74 20 64 65 6c 61 79 2c 20 70 72 69 6f 72 3b  nt delay, prior;
326f2 0a 0a 20 20 61 73 73 65 72 74 28 20 63 6f 75 6e  ..  assert( coun
326f3 74 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 63 6f  t>=0 );.  if( co
326f4 75 6e 74 20 3c 20 4e 44 45 4c 41 59 20 29 7b 0a  unt < NDELAY ){.
326f5 20 20 20 20 64 65 6c 61 79 20 3d 20 64 65 6c 61      delay = dela
326f6 79 73 5b 63 6f 75 6e 74 5d 3b 0a 20 20 20 20 70  ys[count];.    p
326f7 72 69 6f 72 20 3d 20 74 6f 74 61 6c 73 5b 63 6f  rior = totals[co
326f8 75 6e 74 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  unt];.  }else{. 
326f9 20 20 20 64 65 6c 61 79 20 3d 20 64 65 6c 61 79     delay = delay
326fa 73 5b 4e 44 45 4c 41 59 2d 31 5d 3b 0a 20 20 20  s[NDELAY-1];.   
326fb 20 70 72 69 6f 72 20 3d 20 74 6f 74 61 6c 73 5b   prior = totals[
326fc 4e 44 45 4c 41 59 2d 31 5d 20 2b 20 64 65 6c 61  NDELAY-1] + dela
326fd 79 2a 28 63 6f 75 6e 74 2d 28 4e 44 45 4c 41 59  y*(count-(NDELAY
326fe 2d 31 29 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  -1));.  }.  if( 
326ff 70 72 69 6f 72 20 2b 20 64 65 6c 61 79 20 3e 20  prior + delay > 
32700 74 69 6d 65 6f 75 74 20 29 7b 0a 20 20 20 20 64  timeout ){.    d
32701 65 6c 61 79 20 3d 20 74 69 6d 65 6f 75 74 20 2d  elay = timeout -
32702 20 70 72 69 6f 72 3b 0a 20 20 20 20 69 66 28 20   prior;.    if( 
32703 64 65 6c 61 79 3c 3d 30 20 29 20 72 65 74 75 72  delay<=0 ) retur
32704 6e 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  n 0;.  }.  sqlit
32705 65 33 4f 73 53 6c 65 65 70 28 64 62 2d 3e 70 56  e3OsSleep(db->pV
32706 66 73 2c 20 64 65 6c 61 79 2a 31 30 30 30 29 3b  fs, delay*1000);
32707 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 23 65 6c  .  return 1;.#el
32708 73 65 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  se.  sqlite3 *db
32709 20 3d 20 28 73 71 6c 69 74 65 33 20 2a 29 70 74   = (sqlite3 *)pt
3270a 72 3b 0a 20 20 69 6e 74 20 74 69 6d 65 6f 75 74  r;.  int timeout
3270b 20 3d 20 28 28 73 71 6c 69 74 65 33 20 2a 29 70   = ((sqlite3 *)p
3270c 74 72 29 2d 3e 62 75 73 79 54 69 6d 65 6f 75 74  tr)->busyTimeout
3270d 3b 0a 20 20 69 66 28 20 28 63 6f 75 6e 74 2b 31  ;.  if( (count+1
3270e 29 2a 31 30 30 30 20 3e 20 74 69 6d 65 6f 75 74  )*1000 > timeout
3270f 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
32710 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 4f  ;.  }.  sqlite3O
32711 73 53 6c 65 65 70 28 64 62 2d 3e 70 56 66 73 2c  sSleep(db->pVfs,
32712 20 31 30 30 30 30 30 30 29 3b 0a 20 20 72 65 74   1000000);.  ret
32713 75 72 6e 20 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a  urn 1;.#endif.}.
32714 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68  ./*.** Invoke th
32715 65 20 67 69 76 65 6e 20 62 75 73 79 20 68 61 6e  e given busy han
32716 64 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  dler..**.** This
32717 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
32718 65 64 20 77 68 65 6e 20 61 6e 20 6f 70 65 72 61  ed when an opera
32719 74 69 6f 6e 20 66 61 69 6c 65 64 20 77 69 74 68  tion failed with
3271a 20 61 20 6c 6f 63 6b 2e 0a 2a 2a 20 49 66 20 74   a lock..** If t
3271b 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
3271c 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68  rns non-zero, th
3271d 65 20 6c 6f 63 6b 20 69 73 20 72 65 74 72 69 65  e lock is retrie
3271e 64 2e 20 20 49 66 20 69 74 0a 2a 2a 20 72 65 74  d.  If it.** ret
3271f 75 72 6e 73 20 30 2c 20 74 68 65 20 6f 70 65 72  urns 0, the oper
32720 61 74 69 6f 6e 20 61 62 6f 72 74 73 20 77 69 74  ation aborts wit
32721 68 20 61 6e 20 53 51 4c 49 54 45 5f 42 55 53 59  h an SQLITE_BUSY
32722 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54   error..*/.SQLIT
32723 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
32724 6c 69 74 65 33 49 6e 76 6f 6b 65 42 75 73 79 48  lite3InvokeBusyH
32725 61 6e 64 6c 65 72 28 42 75 73 79 48 61 6e 64 6c  andler(BusyHandl
32726 65 72 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63  er *p){.  int rc
32727 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 3d  ;.  if( NEVER(p=
32728 3d 30 29 20 7c 7c 20 70 2d 3e 78 46 75 6e 63 3d  =0) || p->xFunc=
32729 3d 30 20 7c 7c 20 70 2d 3e 6e 42 75 73 79 3c 30  =0 || p->nBusy<0
3272a 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 72   ) return 0;.  r
3272b 63 20 3d 20 70 2d 3e 78 46 75 6e 63 28 70 2d 3e  c = p->xFunc(p->
3272c 70 41 72 67 2c 20 70 2d 3e 6e 42 75 73 79 29 3b  pArg, p->nBusy);
3272d 0a 20 20 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a  .  if( rc==0 ){.
3272e 20 20 20 20 70 2d 3e 6e 42 75 73 79 20 3d 20 2d      p->nBusy = -
3272f 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  1;.  }else{.    
32730 70 2d 3e 6e 42 75 73 79 2b 2b 3b 0a 20 20 7d 0a  p->nBusy++;.  }.
32731 20 20 72 65 74 75 72 6e 20 72 63 3b 20 0a 7d 0a    return rc; .}.
32732 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
32733 69 6e 65 20 73 65 74 73 20 74 68 65 20 62 75 73  ine sets the bus
32734 79 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61  y callback for a
32735 6e 20 53 71 6c 69 74 65 20 64 61 74 61 62 61 73  n Sqlite databas
32736 65 20 74 6f 20 74 68 65 0a 2a 2a 20 67 69 76 65  e to the.** give
32737 6e 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  n callback funct
32738 69 6f 6e 20 77 69 74 68 20 74 68 65 20 67 69 76  ion with the giv
32739 65 6e 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a  en argument..*/.
3273a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
3273b 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64  qlite3_busy_hand
3273c 6c 65 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ler(.  sqlite3 *
3273d 64 62 2c 0a 20 20 69 6e 74 20 28 2a 78 42 75 73  db,.  int (*xBus
3273e 79 29 28 76 6f 69 64 2a 2c 69 6e 74 29 2c 0a 20  y)(void*,int),. 
3273f 20 76 6f 69 64 20 2a 70 41 72 67 0a 29 7b 0a 20   void *pArg.){. 
32740 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
32741 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
32742 0a 20 20 64 62 2d 3e 62 75 73 79 48 61 6e 64 6c  .  db->busyHandl
32743 65 72 2e 78 46 75 6e 63 20 3d 20 78 42 75 73 79  er.xFunc = xBusy
32744 3b 0a 20 20 64 62 2d 3e 62 75 73 79 48 61 6e 64  ;.  db->busyHand
32745 6c 65 72 2e 70 41 72 67 20 3d 20 70 41 72 67 3b  ler.pArg = pArg;
32746 0a 20 20 64 62 2d 3e 62 75 73 79 48 61 6e 64 6c  .  db->busyHandl
32747 65 72 2e 6e 42 75 73 79 20 3d 20 30 3b 0a 20 20  er.nBusy = 0;.  
32748 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
32749 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
3274a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3274b 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53  OK;.}..#ifndef S
3274c 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52  QLITE_OMIT_PROGR
3274d 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 2f 2a 0a  ESS_CALLBACK./*.
3274e 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
3274f 73 65 74 73 20 74 68 65 20 70 72 6f 67 72 65 73  sets the progres
32750 73 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61  s callback for a
32751 6e 20 53 71 6c 69 74 65 20 64 61 74 61 62 61 73  n Sqlite databas
32752 65 20 74 6f 20 74 68 65 0a 2a 2a 20 67 69 76 65  e to the.** give
32753 6e 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  n callback funct
32754 69 6f 6e 20 77 69 74 68 20 74 68 65 20 67 69 76  ion with the giv
32755 65 6e 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 65  en argument. The
32756 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61   progress callba
32757 63 6b 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 69 6e  ck will.** be in
32758 76 6f 6b 65 64 20 65 76 65 72 79 20 6e 4f 70 73  voked every nOps
32759 20 6f 70 63 6f 64 65 73 2e 0a 2a 2f 0a 53 51 4c   opcodes..*/.SQL
3275a 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
3275b 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68 61  ite3_progress_ha
3275c 6e 64 6c 65 72 28 0a 20 20 73 71 6c 69 74 65 33  ndler(.  sqlite3
3275d 20 2a 64 62 2c 20 0a 20 20 69 6e 74 20 6e 4f 70   *db, .  int nOp
3275e 73 2c 0a 20 20 69 6e 74 20 28 2a 78 50 72 6f 67  s,.  int (*xProg
3275f 72 65 73 73 29 28 76 6f 69 64 2a 29 2c 20 0a 20  ress)(void*), . 
32760 20 76 6f 69 64 20 2a 70 41 72 67 0a 29 7b 0a 20   void *pArg.){. 
32761 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
32762 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
32763 0a 20 20 69 66 28 20 6e 4f 70 73 3e 30 20 29 7b  .  if( nOps>0 ){
32764 0a 20 20 20 20 64 62 2d 3e 78 50 72 6f 67 72 65  .    db->xProgre
32765 73 73 20 3d 20 78 50 72 6f 67 72 65 73 73 3b 0a  ss = xProgress;.
32766 20 20 20 20 64 62 2d 3e 6e 50 72 6f 67 72 65 73      db->nProgres
32767 73 4f 70 73 20 3d 20 6e 4f 70 73 3b 0a 20 20 20  sOps = nOps;.   
32768 20 64 62 2d 3e 70 50 72 6f 67 72 65 73 73 41 72   db->pProgressAr
32769 67 20 3d 20 70 41 72 67 3b 0a 20 20 7d 65 6c 73  g = pArg;.  }els
3276a 65 7b 0a 20 20 20 20 64 62 2d 3e 78 50 72 6f 67  e{.    db->xProg
3276b 72 65 73 73 20 3d 20 30 3b 0a 20 20 20 20 64 62  ress = 0;.    db
3276c 2d 3e 6e 50 72 6f 67 72 65 73 73 4f 70 73 20 3d  ->nProgressOps =
3276d 20 30 3b 0a 20 20 20 20 64 62 2d 3e 70 50 72 6f   0;.    db->pPro
3276e 67 72 65 73 73 41 72 67 20 3d 20 30 3b 0a 20 20  gressArg = 0;.  
3276f 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
32770 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
32771 78 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f  x);.}.#endif.../
32772 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
32773 65 20 69 6e 73 74 61 6c 6c 73 20 61 20 64 65 66  e installs a def
32774 61 75 6c 74 20 62 75 73 79 20 68 61 6e 64 6c 65  ault busy handle
32775 72 20 74 68 61 74 20 77 61 69 74 73 20 66 6f 72  r that waits for
32776 20 74 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 65   the.** specifie
32777 64 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6c 6c  d number of mill
32778 69 73 65 63 6f 6e 64 73 20 62 65 66 6f 72 65 20  iseconds before 
32779 72 65 74 75 72 6e 69 6e 67 20 30 2e 0a 2a 2f 0a  returning 0..*/.
3277a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
3277b 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65  qlite3_busy_time
3277c 6f 75 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  out(sqlite3 *db,
3277d 20 69 6e 74 20 6d 73 29 7b 0a 20 20 69 66 28 20   int ms){.  if( 
3277e 6d 73 3e 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e  ms>0 ){.    db->
3277f 62 75 73 79 54 69 6d 65 6f 75 74 20 3d 20 6d 73  busyTimeout = ms
32780 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62 75  ;.    sqlite3_bu
32781 73 79 5f 68 61 6e 64 6c 65 72 28 64 62 2c 20 73  sy_handler(db, s
32782 71 6c 69 74 65 44 65 66 61 75 6c 74 42 75 73 79  qliteDefaultBusy
32783 43 61 6c 6c 62 61 63 6b 2c 20 28 76 6f 69 64 2a  Callback, (void*
32784 29 64 62 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  )db);.  }else{. 
32785 20 20 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f     sqlite3_busy_
32786 68 61 6e 64 6c 65 72 28 64 62 2c 20 30 2c 20 30  handler(db, 0, 0
32787 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
32788 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
32789 0a 2a 2a 20 43 61 75 73 65 20 61 6e 79 20 70 65  .** Cause any pe
3278a 6e 64 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20  nding operation 
3278b 74 6f 20 73 74 6f 70 20 61 74 20 69 74 73 20 65  to stop at its e
3278c 61 72 6c 69 65 73 74 20 6f 70 70 6f 72 74 75 6e  arliest opportun
3278d 69 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ity..*/.SQLITE_A
3278e 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
3278f 69 6e 74 65 72 72 75 70 74 28 73 71 6c 69 74 65  interrupt(sqlite
32790 33 20 2a 64 62 29 7b 0a 20 20 64 62 2d 3e 75 31  3 *db){.  db->u1
32791 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 3d  .isInterrupted =
32792 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68   1;.}.../*.** Th
32793 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 65  is function is e
32794 78 61 63 74 6c 79 20 74 68 65 20 73 61 6d 65 20  xactly the same 
32795 61 73 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  as sqlite3_creat
32796 65 5f 66 75 6e 63 74 69 6f 6e 28 29 2c 20 65 78  e_function(), ex
32797 63 65 70 74 0a 2a 2a 20 74 68 61 74 20 69 74 20  cept.** that it 
32798 69 73 20 64 65 73 69 67 6e 65 64 20 74 6f 20 62  is designed to b
32799 65 20 63 61 6c 6c 65 64 20 62 79 20 69 6e 74 65  e called by inte
3279a 72 6e 61 6c 20 63 6f 64 65 2e 20 54 68 65 20 64  rnal code. The d
3279b 69 66 66 65 72 65 6e 63 65 20 69 73 0a 2a 2a 20  ifference is.** 
3279c 74 68 61 74 20 69 66 20 61 20 6d 61 6c 6c 6f 63  that if a malloc
3279d 28 29 20 66 61 69 6c 73 20 69 6e 20 73 71 6c 69  () fails in sqli
3279e 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
3279f 69 6f 6e 28 29 2c 20 61 6e 20 65 72 72 6f 72 20  ion(), an error 
327a0 63 6f 64 65 0a 2a 2a 20 69 73 20 72 65 74 75 72  code.** is retur
327a1 6e 65 64 20 61 6e 64 20 74 68 65 20 6d 61 6c 6c  ned and the mall
327a2 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 63 6c  ocFailed flag cl
327a3 65 61 72 65 64 2e 20 0a 2a 2f 0a 53 51 4c 49 54  eared. .*/.SQLIT
327a4 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
327a5 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 28  lite3CreateFunc(
327a6 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a  .  sqlite3 *db,.
327a7 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46    const char *zF
327a8 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69  unctionName,.  i
327a9 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65  nt nArg,.  int e
327aa 6e 63 2c 0a 20 20 76 6f 69 64 20 2a 70 55 73 65  nc,.  void *pUse
327ab 72 44 61 74 61 2c 0a 20 20 76 6f 69 64 20 28 2a  rData,.  void (*
327ac 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63  xFunc)(sqlite3_c
327ad 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
327ae 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a 20  te3_value **),. 
327af 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73   void (*xStep)(s
327b0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
327b1 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
327b2 65 20 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a  e **),.  void (*
327b3 78 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f  xFinal)(sqlite3_
327b4 63 6f 6e 74 65 78 74 2a 29 0a 29 7b 0a 20 20 46  context*).){.  F
327b5 75 6e 63 44 65 66 20 2a 70 3b 0a 20 20 69 6e 74  uncDef *p;.  int
327b6 20 6e 4e 61 6d 65 3b 0a 0a 20 20 61 73 73 65 72   nName;..  asser
327b7 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
327b8 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29  _held(db->mutex)
327b9 20 29 3b 0a 20 20 69 66 28 20 7a 46 75 6e 63 74   );.  if( zFunct
327ba 69 6f 6e 4e 61 6d 65 3d 3d 30 20 7c 7c 0a 20 20  ionName==0 ||.  
327bb 20 20 20 20 28 78 46 75 6e 63 20 26 26 20 28 78      (xFunc && (x
327bc 46 69 6e 61 6c 20 7c 7c 20 78 53 74 65 70 29 29  Final || xStep))
327bd 20 7c 7c 20 0a 20 20 20 20 20 20 28 21 78 46 75   || .      (!xFu
327be 6e 63 20 26 26 20 28 78 46 69 6e 61 6c 20 26 26  nc && (xFinal &&
327bf 20 21 78 53 74 65 70 29 29 20 7c 7c 0a 20 20 20   !xStep)) ||.   
327c0 20 20 20 28 21 78 46 75 6e 63 20 26 26 20 28 21     (!xFunc && (!
327c1 78 46 69 6e 61 6c 20 26 26 20 78 53 74 65 70 29  xFinal && xStep)
327c2 29 20 7c 7c 0a 20 20 20 20 20 20 28 6e 41 72 67  ) ||.      (nArg
327c3 3c 2d 31 20 7c 7c 20 6e 41 72 67 3e 53 51 4c 49  <-1 || nArg>SQLI
327c4 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f  TE_MAX_FUNCTION_
327c5 41 52 47 29 20 7c 7c 0a 20 20 20 20 20 20 28 32  ARG) ||.      (2
327c6 35 35 3c 28 6e 4e 61 6d 65 20 3d 20 73 71 6c 69  55<(nName = sqli
327c7 74 65 33 53 74 72 6c 65 6e 33 30 28 20 7a 46 75  te3Strlen30( zFu
327c8 6e 63 74 69 6f 6e 4e 61 6d 65 29 29 29 20 29 7b  nctionName))) ){
327c9 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
327ca 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20  TE_MISUSE;.  }. 
327cb 20 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45   .#ifndef SQLITE
327cc 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20 2f 2a  _OMIT_UTF16.  /*
327cd 20 49 66 20 53 51 4c 49 54 45 5f 55 54 46 31 36   If SQLITE_UTF16
327ce 20 69 73 20 73 70 65 63 69 66 69 65 64 20 61 73   is specified as
327cf 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 74 79   the encoding ty
327d0 70 65 2c 20 74 72 61 6e 73 66 6f 72 6d 20 74 68  pe, transform th
327d1 69 73 0a 20 20 2a 2a 20 74 6f 20 6f 6e 65 20 6f  is.  ** to one o
327d2 66 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45  f SQLITE_UTF16LE
327d3 20 6f 72 20 53 51 4c 49 54 45 5f 55 54 46 31 36   or SQLITE_UTF16
327d4 42 45 20 75 73 69 6e 67 20 74 68 65 0a 20 20 2a  BE using the.  *
327d5 2a 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41  * SQLITE_UTF16NA
327d6 54 49 56 45 20 6d 61 63 72 6f 2e 20 53 51 4c 49  TIVE macro. SQLI
327d7 54 45 5f 55 54 46 31 36 20 69 73 20 6e 6f 74 20  TE_UTF16 is not 
327d8 75 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 2e  used internally.
327d9 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 53 51  .  **.  ** If SQ
327da 4c 49 54 45 5f 41 4e 59 20 69 73 20 73 70 65 63  LITE_ANY is spec
327db 69 66 69 65 64 2c 20 61 64 64 20 74 68 72 65 65  ified, add three
327dc 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65   versions of the
327dd 20 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 74   function.  ** t
327de 6f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  o the hash table
327df 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 65 6e 63  ..  */.  if( enc
327e0 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 20 29  ==SQLITE_UTF16 )
327e1 7b 0a 20 20 20 20 65 6e 63 20 3d 20 53 51 4c 49  {.    enc = SQLI
327e2 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 3b 0a  TE_UTF16NATIVE;.
327e3 20 20 7d 65 6c 73 65 20 69 66 28 20 65 6e 63 3d    }else if( enc=
327e4 3d 53 51 4c 49 54 45 5f 41 4e 59 20 29 7b 0a 20  =SQLITE_ANY ){. 
327e5 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 72     int rc;.    r
327e6 63 20 3d 20 73 71 6c 69 74 65 33 43 72 65 61 74  c = sqlite3Creat
327e7 65 46 75 6e 63 28 64 62 2c 20 7a 46 75 6e 63 74  eFunc(db, zFunct
327e8 69 6f 6e 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 53  ionName, nArg, S
327e9 51 4c 49 54 45 5f 55 54 46 38 2c 0a 20 20 20 20  QLITE_UTF8,.    
327ea 20 20 20 20 20 70 55 73 65 72 44 61 74 61 2c 20       pUserData, 
327eb 78 46 75 6e 63 2c 20 78 53 74 65 70 2c 20 78 46  xFunc, xStep, xF
327ec 69 6e 61 6c 29 3b 0a 20 20 20 20 69 66 28 20 72  inal);.    if( r
327ed 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
327ee 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
327ef 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c  e3CreateFunc(db,
327f0 20 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20   zFunctionName, 
327f1 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46  nArg, SQLITE_UTF
327f2 31 36 4c 45 2c 0a 20 20 20 20 20 20 20 20 20 20  16LE,.          
327f3 70 55 73 65 72 44 61 74 61 2c 20 78 46 75 6e 63  pUserData, xFunc
327f4 2c 20 78 53 74 65 70 2c 20 78 46 69 6e 61 6c 29  , xStep, xFinal)
327f5 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
327f6 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
327f7 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
327f8 3b 0a 20 20 20 20 7d 0a 20 20 20 20 65 6e 63 20  ;.    }.    enc 
327f9 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45  = SQLITE_UTF16BE
327fa 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 65 6e  ;.  }.#else.  en
327fb 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b  c = SQLITE_UTF8;
327fc 0a 23 65 6e 64 69 66 0a 20 20 0a 20 20 2f 2a 20  .#endif.  .  /* 
327fd 43 68 65 63 6b 20 69 66 20 61 6e 20 65 78 69 73  Check if an exis
327fe 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 69 73  ting function is
327ff 20 62 65 69 6e 67 20 6f 76 65 72 72 69 64 64 65   being overridde
32800 6e 20 6f 72 20 64 65 6c 65 74 65 64 2e 20 49 66  n or deleted. If
32801 20 73 6f 2c 0a 20 20 2a 2a 20 61 6e 64 20 74 68   so,.  ** and th
32802 65 72 65 20 61 72 65 20 61 63 74 69 76 65 20 56  ere are active V
32803 4d 73 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20  Ms, then return 
32804 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 49 66 20  SQLITE_BUSY. If 
32805 61 20 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20  a function.  ** 
32806 69 73 20 62 65 69 6e 67 20 6f 76 65 72 72 69 64  is being overrid
32807 64 65 6e 2f 64 65 6c 65 74 65 64 20 62 75 74 20  den/deleted but 
32808 74 68 65 72 65 20 61 72 65 20 6e 6f 20 61 63 74  there are no act
32809 69 76 65 20 56 4d 73 2c 20 61 6c 6c 6f 77 20 74  ive VMs, allow t
3280a 68 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74 69 6f  he.  ** operatio
3280b 6e 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 62 75  n to continue bu
3280c 74 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c  t invalidate all
3280d 20 70 72 65 63 6f 6d 70 69 6c 65 64 20 73 74 61   precompiled sta
3280e 74 65 6d 65 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20  tements..  */.  
3280f 70 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46  p = sqlite3FindF
32810 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 46 75 6e  unction(db, zFun
32811 63 74 69 6f 6e 4e 61 6d 65 2c 20 6e 4e 61 6d 65  ctionName, nName
32812 2c 20 6e 41 72 67 2c 20 28 75 38 29 65 6e 63 2c  , nArg, (u8)enc,
32813 20 30 29 3b 0a 20 20 69 66 28 20 70 20 26 26 20   0);.  if( p && 
32814 70 2d 3e 69 50 72 65 66 45 6e 63 3d 3d 65 6e 63  p->iPrefEnc==enc
32815 20 26 26 20 70 2d 3e 6e 41 72 67 3d 3d 6e 41 72   && p->nArg==nAr
32816 67 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d  g ){.    if( db-
32817 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 20 29  >activeVdbeCnt )
32818 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
32819 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f  rror(db, SQLITE_
3281a 42 55 53 59 2c 20 0a 20 20 20 20 20 20 20 20 22  BUSY, .        "
3281b 75 6e 61 62 6c 65 20 74 6f 20 64 65 6c 65 74 65  unable to delete
3281c 2f 6d 6f 64 69 66 79 20 75 73 65 72 2d 66 75 6e  /modify user-fun
3281d 63 74 69 6f 6e 20 64 75 65 20 74 6f 20 61 63 74  ction due to act
3281e 69 76 65 20 73 74 61 74 65 6d 65 6e 74 73 22 29  ive statements")
3281f 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
32820 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  !db->mallocFaile
32821 64 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  d );.      retur
32822 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20  n SQLITE_BUSY;. 
32823 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
32824 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65  sqlite3ExpirePre
32825 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28  paredStatements(
32826 64 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  db);.    }.  }..
32827 20 20 70 20 3d 20 73 71 6c 69 74 65 33 46 69 6e    p = sqlite3Fin
32828 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 46  dFunction(db, zF
32829 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20 6e 4e 61  unctionName, nNa
3282a 6d 65 2c 20 6e 41 72 67 2c 20 28 75 38 29 65 6e  me, nArg, (u8)en
3282b 63 2c 20 31 29 3b 0a 20 20 61 73 73 65 72 74 28  c, 1);.  assert(
3282c 70 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  p || db->mallocF
3282d 61 69 6c 65 64 29 3b 0a 20 20 69 66 28 20 21 70  ailed);.  if( !p
3282e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
3282f 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
32830 0a 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 30 3b  .  p->flags = 0;
32831 0a 20 20 70 2d 3e 78 46 75 6e 63 20 3d 20 78 46  .  p->xFunc = xF
32832 75 6e 63 3b 0a 20 20 70 2d 3e 78 53 74 65 70 20  unc;.  p->xStep 
32833 3d 20 78 53 74 65 70 3b 0a 20 20 70 2d 3e 78 46  = xStep;.  p->xF
32834 69 6e 61 6c 69 7a 65 20 3d 20 78 46 69 6e 61 6c  inalize = xFinal
32835 3b 0a 20 20 70 2d 3e 70 55 73 65 72 44 61 74 61  ;.  p->pUserData
32836 20 3d 20 70 55 73 65 72 44 61 74 61 3b 0a 20 20   = pUserData;.  
32837 70 2d 3e 6e 41 72 67 20 3d 20 28 75 31 36 29 6e  p->nArg = (u16)n
32838 41 72 67 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  Arg;.  return SQ
32839 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
3283a 2a 20 43 72 65 61 74 65 20 6e 65 77 20 75 73 65  * Create new use
3283b 72 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a  r functions..*/.
3283c 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
3283d 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
3283e 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65  nction(.  sqlite
3283f 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 63  3 *db,.  const c
32840 68 61 72 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61  har *zFunctionNa
32841 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a  me,.  int nArg,.
32842 20 20 69 6e 74 20 65 6e 63 2c 0a 20 20 76 6f 69    int enc,.  voi
32843 64 20 2a 70 2c 0a 20 20 76 6f 69 64 20 28 2a 78  d *p,.  void (*x
32844 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f  Func)(sqlite3_co
32845 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74  ntext*,int,sqlit
32846 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a 20 20  e3_value **),.  
32847 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 71  void (*xStep)(sq
32848 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
32849 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt,sqlite3_value
3284a 20 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78   **),.  void (*x
3284b 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63  Final)(sqlite3_c
3284c 6f 6e 74 65 78 74 2a 29 0a 29 7b 0a 20 20 69 6e  ontext*).){.  in
3284d 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f  t rc;.  sqlite3_
3284e 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
3284f 6d 75 74 65 78 29 3b 0a 20 20 72 63 20 3d 20 73  mutex);.  rc = s
32850 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63  qlite3CreateFunc
32851 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e 61  (db, zFunctionNa
32852 6d 65 2c 20 6e 41 72 67 2c 20 65 6e 63 2c 20 70  me, nArg, enc, p
32853 2c 20 78 46 75 6e 63 2c 20 78 53 74 65 70 2c 20  , xFunc, xStep, 
32854 78 46 69 6e 61 6c 29 3b 0a 20 20 72 63 20 3d 20  xFinal);.  rc = 
32855 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64  sqlite3ApiExit(d
32856 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69 74 65  b, rc);.  sqlite
32857 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
32858 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
32859 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65  rn rc;.}..#ifnde
3285a 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  f SQLITE_OMIT_UT
3285b 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  F16.SQLITE_API i
3285c 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  nt sqlite3_creat
3285d 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 0a 20 20  e_function16(.  
3285e 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63  sqlite3 *db,.  c
3285f 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 46 75 6e 63  onst void *zFunc
32860 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20  tionName,.  int 
32861 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65 54 65 78  nArg,.  int eTex
32862 74 52 65 70 2c 0a 20 20 76 6f 69 64 20 2a 70 2c  tRep,.  void *p,
32863 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29  .  void (*xFunc)
32864 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
32865 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61  *,int,sqlite3_va
32866 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28  lue**),.  void (
32867 2a 78 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f  *xStep)(sqlite3_
32868 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c  context*,int,sql
32869 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20  ite3_value**),. 
3286a 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28   void (*xFinal)(
3286b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
3286c 29 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  ).){.  int rc;. 
3286d 20 63 68 61 72 20 2a 7a 46 75 6e 63 38 3b 0a 20   char *zFunc8;. 
3286e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
3286f 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
32870 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e  .  assert( !db->
32871 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a  mallocFailed );.
32872 20 20 7a 46 75 6e 63 38 20 3d 20 73 71 6c 69 74    zFunc8 = sqlit
32873 65 33 55 74 66 31 36 74 6f 38 28 64 62 2c 20 7a  e3Utf16to8(db, z
32874 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20 2d 31  FunctionName, -1
32875 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
32876 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20  3CreateFunc(db, 
32877 7a 46 75 6e 63 38 2c 20 6e 41 72 67 2c 20 65 54  zFunc8, nArg, eT
32878 65 78 74 52 65 70 2c 20 70 2c 20 78 46 75 6e 63  extRep, p, xFunc
32879 2c 20 78 53 74 65 70 2c 20 78 46 69 6e 61 6c 29  , xStep, xFinal)
3287a 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
3287b 65 28 64 62 2c 20 7a 46 75 6e 63 38 29 3b 0a 20  e(db, zFunc8);. 
3287c 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69   rc = sqlite3Api
3287d 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20  Exit(db, rc);.  
3287e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
3287f 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
32880 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23    return rc;.}.#
32881 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65  endif.../*.** De
32882 63 6c 61 72 65 20 74 68 61 74 20 61 20 66 75 6e  clare that a fun
32883 63 74 69 6f 6e 20 68 61 73 20 62 65 65 6e 20 6f  ction has been o
32884 76 65 72 6c 6f 61 64 65 64 20 62 79 20 61 20 76  verloaded by a v
32885 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a  irtual table..**
32886 0a 2a 2a 20 49 66 20 74 68 65 20 66 75 6e 63 74  .** If the funct
32887 69 6f 6e 20 61 6c 72 65 61 64 79 20 65 78 69 73  ion already exis
32888 74 73 20 61 73 20 61 20 72 65 67 75 6c 61 72 20  ts as a regular 
32889 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69 6f 6e 2c  global function,
3288a 20 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20 72 6f   then.** this ro
3288b 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70  utine is a no-op
3288c 2e 20 20 49 66 20 74 68 65 20 66 75 6e 63 74 69  .  If the functi
3288d 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73  on does not exis
3288e 74 2c 20 74 68 65 6e 20 63 72 65 61 74 65 0a 2a  t, then create.*
3288f 2a 20 61 20 6e 65 77 20 6f 6e 65 20 74 68 61 74  * a new one that
32890 20 61 6c 77 61 79 73 20 74 68 72 6f 77 73 20 61   always throws a
32891 20 72 75 6e 2d 74 69 6d 65 20 65 72 72 6f 72 2e   run-time error.
32892 20 20 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 76 69    .**.** When vi
32893 72 74 75 61 6c 20 74 61 62 6c 65 73 20 69 6e 74  rtual tables int
32894 65 6e 64 20 74 6f 20 70 72 6f 76 69 64 65 20 61  end to provide a
32895 6e 20 6f 76 65 72 6c 6f 61 64 65 64 20 66 75 6e  n overloaded fun
32896 63 74 69 6f 6e 2c 20 74 68 65 79 0a 2a 2a 20 73  ction, they.** s
32897 68 6f 75 6c 64 20 63 61 6c 6c 20 74 68 69 73 20  hould call this 
32898 72 6f 75 74 69 6e 65 20 74 6f 20 6d 61 6b 65 20  routine to make 
32899 73 75 72 65 20 74 68 65 20 67 6c 6f 62 61 6c 20  sure the global 
3289a 66 75 6e 63 74 69 6f 6e 20 65 78 69 73 74 73 2e  function exists.
3289b 0a 2a 2a 20 41 20 67 6c 6f 62 61 6c 20 66 75 6e  .** A global fun
3289c 63 74 69 6f 6e 20 6d 75 73 74 20 65 78 69 73 74  ction must exist
3289d 20 69 6e 20 6f 72 64 65 72 20 66 6f 72 20 6e 61   in order for na
3289e 6d 65 20 72 65 73 6f 6c 75 74 69 6f 6e 20 74 6f  me resolution to
3289f 20 77 6f 72 6b 0a 2a 2a 20 70 72 6f 70 65 72 6c   work.** properl
328a0 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  y..*/.SQLITE_API
328a1 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 76 65   int sqlite3_ove
328a2 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 0a  rload_function(.
328a3 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20    sqlite3 *db,. 
328a4 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
328a5 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 0a 29  me,.  int nArg.)
328a6 7b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20  {.  int nName = 
328a7 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
328a8 7a 4e 61 6d 65 29 3b 0a 20 20 69 6e 74 20 72 63  zName);.  int rc
328a9 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
328aa 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
328ab 78 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  x);.  if( sqlite
328ac 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62  3FindFunction(db
328ad 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20  , zName, nName, 
328ae 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46  nArg, SQLITE_UTF
328af 38 2c 20 30 29 3d 3d 30 20 29 7b 0a 20 20 20 20  8, 0)==0 ){.    
328b0 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e  sqlite3CreateFun
328b1 63 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 6e 41 72  c(db, zName, nAr
328b2 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 0a  g, SQLITE_UTF8,.
328b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
328b4 20 20 20 20 20 20 30 2c 20 73 71 6c 69 74 65 33        0, sqlite3
328b5 49 6e 76 61 6c 69 64 46 75 6e 63 74 69 6f 6e 2c  InvalidFunction,
328b6 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 63   0, 0);.  }.  rc
328b7 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69   = sqlite3ApiExi
328b8 74 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 29  t(db, SQLITE_OK)
328b9 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
328ba 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
328bb 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  x);.  return rc;
328bc 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
328bd 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 2f 2a  TE_OMIT_TRACE./*
328be 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 74  .** Register a t
328bf 72 61 63 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20  race function.  
328c0 54 68 65 20 70 41 72 67 20 66 72 6f 6d 20 74 68  The pArg from th
328c1 65 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 67  e previously reg
328c2 69 73 74 65 72 65 64 20 74 72 61 63 65 0a 2a 2a  istered trace.**
328c3 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 0a   is returned.  .
328c4 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 74 72 61  **.** A NULL tra
328c5 63 65 20 66 75 6e 63 74 69 6f 6e 20 6d 65 61 6e  ce function mean
328c6 73 20 74 68 61 74 20 6e 6f 20 74 72 61 63 69 6e  s that no tracin
328c7 67 20 69 73 20 65 78 65 63 75 74 65 73 2e 20 20  g is executes.  
328c8 41 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20 74 72  A non-NULL.** tr
328c9 61 63 65 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ace is a pointer
328ca 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 74   to a function t
328cb 68 61 74 20 69 73 20 69 6e 76 6f 6b 65 64 20 61  hat is invoked a
328cc 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 65  t the start of e
328cd 61 63 68 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65  ach.** SQL state
328ce 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ment..*/.SQLITE_
328cf 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  API void *sqlite
328d0 33 5f 74 72 61 63 65 28 73 71 6c 69 74 65 33 20  3_trace(sqlite3 
328d1 2a 64 62 2c 20 76 6f 69 64 20 28 2a 78 54 72 61  *db, void (*xTra
328d2 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20  ce)(void*,const 
328d3 63 68 61 72 2a 29 2c 20 76 6f 69 64 20 2a 70 41  char*), void *pA
328d4 72 67 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4f 6c  rg){.  void *pOl
328d5 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  d;.  sqlite3_mut
328d6 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
328d7 65 78 29 3b 0a 20 20 70 4f 6c 64 20 3d 20 64 62  ex);.  pOld = db
328d8 2d 3e 70 54 72 61 63 65 41 72 67 3b 0a 20 20 64  ->pTraceArg;.  d
328d9 62 2d 3e 78 54 72 61 63 65 20 3d 20 78 54 72 61  b->xTrace = xTra
328da 63 65 3b 0a 20 20 64 62 2d 3e 70 54 72 61 63 65  ce;.  db->pTrace
328db 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 73 71  Arg = pArg;.  sq
328dc 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
328dd 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
328de 72 65 74 75 72 6e 20 70 4f 6c 64 3b 0a 7d 0a 2f  return pOld;.}./
328df 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20  *.** Register a 
328e0 70 72 6f 66 69 6c 65 20 66 75 6e 63 74 69 6f 6e  profile function
328e1 2e 20 20 54 68 65 20 70 41 72 67 20 66 72 6f 6d  .  The pArg from
328e2 20 74 68 65 20 70 72 65 76 69 6f 75 73 6c 79 20   the previously 
328e3 72 65 67 69 73 74 65 72 65 64 20 0a 2a 2a 20 70  registered .** p
328e4 72 6f 66 69 6c 65 20 66 75 6e 63 74 69 6f 6e 20  rofile function 
328e5 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 0a 2a  is returned.  .*
328e6 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 70 72 6f 66  *.** A NULL prof
328e7 69 6c 65 20 66 75 6e 63 74 69 6f 6e 20 6d 65 61  ile function mea
328e8 6e 73 20 74 68 61 74 20 6e 6f 20 70 72 6f 66 69  ns that no profi
328e9 6c 69 6e 67 20 69 73 20 65 78 65 63 75 74 65 73  ling is executes
328ea 2e 20 20 41 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a  .  A non-NULL.**
328eb 20 70 72 6f 66 69 6c 65 20 69 73 20 61 20 70 6f   profile is a po
328ec 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74  inter to a funct
328ed 69 6f 6e 20 74 68 61 74 20 69 73 20 69 6e 76 6f  ion that is invo
328ee 6b 65 64 20 61 74 20 74 68 65 20 63 6f 6e 63 6c  ked at the concl
328ef 75 73 69 6f 6e 20 6f 66 0a 2a 2a 20 65 61 63 68  usion of.** each
328f0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74   SQL statement t
328f1 68 61 74 20 69 73 20 72 75 6e 2e 0a 2a 2f 0a 53  hat is run..*/.S
328f2 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
328f3 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65 28  sqlite3_profile(
328f4 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a  .  sqlite3 *db,.
328f5 20 20 76 6f 69 64 20 28 2a 78 50 72 6f 66 69 6c    void (*xProfil
328f6 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63  e)(void*,const c
328f7 68 61 72 2a 2c 73 71 6c 69 74 65 5f 75 69 6e 74  har*,sqlite_uint
328f8 36 34 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72  64),.  void *pAr
328f9 67 0a 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4f 6c  g.){.  void *pOl
328fa 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  d;.  sqlite3_mut
328fb 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
328fc 65 78 29 3b 0a 20 20 70 4f 6c 64 20 3d 20 64 62  ex);.  pOld = db
328fd 2d 3e 70 50 72 6f 66 69 6c 65 41 72 67 3b 0a 20  ->pProfileArg;. 
328fe 20 64 62 2d 3e 78 50 72 6f 66 69 6c 65 20 3d 20   db->xProfile = 
328ff 78 50 72 6f 66 69 6c 65 3b 0a 20 20 64 62 2d 3e  xProfile;.  db->
32900 70 50 72 6f 66 69 6c 65 41 72 67 20 3d 20 70 41  pProfileArg = pA
32901 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  rg;.  sqlite3_mu
32902 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
32903 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  tex);.  return p
32904 4f 6c 64 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  Old;.}.#endif /*
32905 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41   SQLITE_OMIT_TRA
32906 43 45 20 2a 2f 0a 0a 2f 2a 2a 2a 20 45 58 50 45  CE */../*** EXPE
32907 52 49 4d 45 4e 54 41 4c 20 2a 2a 2a 0a 2a 2a 0a  RIMENTAL ***.**.
32908 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 66 75  ** Register a fu
32909 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 69 6e 76  nction to be inv
3290a 6f 6b 65 64 20 77 68 65 6e 20 61 20 74 72 61 6e  oked when a tran
3290b 73 61 63 74 69 6f 6e 20 63 6f 6d 6d 65 6e 74 73  saction comments
3290c 2e 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 76 6f  ..** If the invo
3290d 6b 65 64 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  ked function ret
3290e 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74  urns non-zero, t
3290f 68 65 6e 20 74 68 65 20 63 6f 6d 6d 69 74 20 62  hen the commit b
32910 65 63 6f 6d 65 73 20 61 0a 2a 2a 20 72 6f 6c 6c  ecomes a.** roll
32911 62 61 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  back..*/.SQLITE_
32912 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  API void *sqlite
32913 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 0a 20  3_commit_hook(. 
32914 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
32915 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 74             /* At
32916 74 61 63 68 20 74 68 65 20 68 6f 6f 6b 20 74 6f  tach the hook to
32917 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 2a   this database *
32918 2f 0a 20 20 69 6e 74 20 28 2a 78 43 61 6c 6c 62  /.  int (*xCallb
32919 61 63 6b 29 28 76 6f 69 64 2a 29 2c 20 20 2f 2a  ack)(void*),  /*
3291a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e 76   Function to inv
3291b 6f 6b 65 20 6f 6e 20 65 61 63 68 20 63 6f 6d 6d  oke on each comm
3291c 69 74 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41  it */.  void *pA
3291d 72 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20  rg              
3291e 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f    /* Argument to
3291f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f   the function */
32920 0a 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4f 6c 64  .){.  void *pOld
32921 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
32922 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
32923 78 29 3b 0a 20 20 70 4f 6c 64 20 3d 20 64 62 2d  x);.  pOld = db-
32924 3e 70 43 6f 6d 6d 69 74 41 72 67 3b 0a 20 20 64  >pCommitArg;.  d
32925 62 2d 3e 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61  b->xCommitCallba
32926 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0a  ck = xCallback;.
32927 20 20 64 62 2d 3e 70 43 6f 6d 6d 69 74 41 72 67    db->pCommitArg
32928 20 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c 69 74   = pArg;.  sqlit
32929 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
3292a 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
3292b 75 72 6e 20 70 4f 6c 64 3b 0a 7d 0a 0a 2f 2a 0a  urn pOld;.}../*.
3292c 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 63 61  ** Register a ca
3292d 6c 6c 62 61 63 6b 20 74 6f 20 62 65 20 69 6e 76  llback to be inv
3292e 6f 6b 65 64 20 65 61 63 68 20 74 69 6d 65 20 61  oked each time a
3292f 20 72 6f 77 20 69 73 20 75 70 64 61 74 65 64 2c   row is updated,
32930 0a 2a 2a 20 69 6e 73 65 72 74 65 64 20 6f 72 20  .** inserted or 
32931 64 65 6c 65 74 65 64 20 75 73 69 6e 67 20 74 68  deleted using th
32932 69 73 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  is database conn
32933 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  ection..*/.SQLIT
32934 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69  E_API void *sqli
32935 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28  te3_update_hook(
32936 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
32937 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
32938 41 74 74 61 63 68 20 74 68 65 20 68 6f 6f 6b 20  Attach the hook 
32939 74 6f 20 74 68 69 73 20 64 61 74 61 62 61 73 65  to this database
3293a 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 43 61   */.  void (*xCa
3293b 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e  llback)(void*,in
3293c 74 2c 63 68 61 72 20 63 6f 6e 73 74 20 2a 2c 63  t,char const *,c
3293d 68 61 72 20 63 6f 6e 73 74 20 2a 2c 73 71 6c 69  har const *,sqli
3293e 74 65 5f 69 6e 74 36 34 29 2c 0a 20 20 76 6f 69  te_int64),.  voi
3293f 64 20 2a 70 41 72 67 20 20 20 20 20 20 20 20 20  d *pArg         
32940 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65         /* Argume
32941 6e 74 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69  nt to the functi
32942 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 76 6f 69 64 20  on */.){.  void 
32943 2a 70 52 65 74 3b 0a 20 20 73 71 6c 69 74 65 33  *pRet;.  sqlite3
32944 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d  _mutex_enter(db-
32945 3e 6d 75 74 65 78 29 3b 0a 20 20 70 52 65 74 20  >mutex);.  pRet 
32946 3d 20 64 62 2d 3e 70 55 70 64 61 74 65 41 72 67  = db->pUpdateArg
32947 3b 0a 20 20 64 62 2d 3e 78 55 70 64 61 74 65 43  ;.  db->xUpdateC
32948 61 6c 6c 62 61 63 6b 20 3d 20 78 43 61 6c 6c 62  allback = xCallb
32949 61 63 6b 3b 0a 20 20 64 62 2d 3e 70 55 70 64 61  ack;.  db->pUpda
3294a 74 65 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20  teArg = pArg;.  
3294b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
3294c 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
3294d 20 20 72 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d    return pRet;.}
3294e 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72  ../*.** Register
3294f 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 62   a callback to b
32950 65 20 69 6e 76 6f 6b 65 64 20 65 61 63 68 20 74  e invoked each t
32951 69 6d 65 20 61 20 74 72 61 6e 73 61 63 74 69 6f  ime a transactio
32952 6e 20 69 73 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62  n is rolled.** b
32953 61 63 6b 20 62 79 20 74 68 69 73 20 64 61 74 61  ack by this data
32954 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  base connection.
32955 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
32956 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 6f 6c  oid *sqlite3_rol
32957 6c 62 61 63 6b 5f 68 6f 6f 6b 28 0a 20 20 73 71  lback_hook(.  sq
32958 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
32959 20 20 20 20 20 20 20 20 2f 2a 20 41 74 74 61 63          /* Attac
3295a 68 20 74 68 65 20 68 6f 6f 6b 20 74 6f 20 74 68  h the hook to th
3295b 69 73 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  is database */. 
3295c 20 76 6f 69 64 20 28 2a 78 43 61 6c 6c 62 61 63   void (*xCallbac
3295d 6b 29 28 76 6f 69 64 2a 29 2c 20 2f 2a 20 43 61  k)(void*), /* Ca
3295e 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20  llback function 
3295f 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 20  */.  void *pArg 
32960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
32961 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  * Argument to th
32962 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b  e function */.){
32963 0a 20 20 76 6f 69 64 20 2a 70 52 65 74 3b 0a 20  .  void *pRet;. 
32964 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
32965 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
32966 0a 20 20 70 52 65 74 20 3d 20 64 62 2d 3e 70 52  .  pRet = db->pR
32967 6f 6c 6c 62 61 63 6b 41 72 67 3b 0a 20 20 64 62  ollbackArg;.  db
32968 2d 3e 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62  ->xRollbackCallb
32969 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b  ack = xCallback;
3296a 0a 20 20 64 62 2d 3e 70 52 6f 6c 6c 62 61 63 6b  .  db->pRollback
3296b 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 73 71  Arg = pArg;.  sq
3296c 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
3296d 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
3296e 72 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a  return pRet;.}..
3296f 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
32970 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 72 75 65  ion returns true
32971 20 69 66 20 6d 61 69 6e 2d 6d 65 6d 6f 72 79 20   if main-memory 
32972 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 69  should be used i
32973 6e 73 74 65 61 64 20 6f 66 0a 2a 2a 20 61 20 74  nstead of.** a t
32974 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 66 6f  emporary file fo
32975 72 20 74 72 61 6e 73 69 65 6e 74 20 70 61 67 65  r transient page
32976 72 20 66 69 6c 65 73 20 61 6e 64 20 73 74 61 74  r files and stat
32977 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 73 2e 0a  ement journals..
32978 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 72 65 74  ** The value ret
32979 75 72 6e 65 64 20 64 65 70 65 6e 64 73 20 6f 6e  urned depends on
3297a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 64 62   the value of db
3297b 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 20 28 72 75  ->temp_store (ru
3297c 6e 74 69 6d 65 0a 2a 2a 20 70 61 72 61 6d 65 74  ntime.** paramet
3297d 65 72 29 20 61 6e 64 20 74 68 65 20 63 6f 6d 70  er) and the comp
3297e 69 6c 65 20 74 69 6d 65 20 76 61 6c 75 65 20 6f  ile time value o
3297f 66 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54  f SQLITE_TEMP_ST
32980 4f 52 45 2e 20 54 68 65 0a 2a 2a 20 66 6f 6c 6c  ORE. The.** foll
32981 6f 77 69 6e 67 20 74 61 62 6c 65 20 64 65 73 63  owing table desc
32982 72 69 62 65 73 20 74 68 65 20 72 65 6c 61 74 69  ribes the relati
32983 6f 6e 73 68 69 70 20 62 65 74 77 65 65 6e 20 74  onship between t
32984 68 65 73 65 20 74 77 6f 20 76 61 6c 75 65 73 0a  hese two values.
32985 2a 2a 20 61 6e 64 20 74 68 69 73 20 66 75 6e 63  ** and this func
32986 74 69 6f 6e 73 20 72 65 74 75 72 6e 20 76 61 6c  tions return val
32987 75 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49  ue..**.**   SQLI
32988 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 20 20 20  TE_TEMP_STORE   
32989 20 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65    db->temp_store
3298a 20 20 20 20 20 4c 6f 63 61 74 69 6f 6e 20 6f 66       Location of
3298b 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62   temporary datab
3298c 61 73 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d  ase.**   -------
3298d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d  ----------     -
3298e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20  -------------   
3298f 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    --------------
32990 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
32991 0a 2a 2a 20 20 20 30 20 20 20 20 20 20 20 20 20  .**   0         
32992 20 20 20 20 20 20 20 20 20 20 20 20 61 6e 79 20              any 
32993 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66                 f
32994 69 6c 65 20 20 20 20 20 20 28 72 65 74 75 72 6e  ile      (return
32995 20 30 29 0a 2a 2a 20 20 20 31 20 20 20 20 20 20   0).**   1      
32996 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
32997 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32998 20 20 66 69 6c 65 20 20 20 20 20 20 28 72 65 74    file      (ret
32999 75 72 6e 20 30 29 0a 2a 2a 20 20 20 31 20 20 20  urn 0).**   1   
3299a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3299b 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20 20    2             
3299c 20 20 20 20 20 6d 65 6d 6f 72 79 20 20 20 20 28       memory    (
3299d 72 65 74 75 72 6e 20 31 29 0a 2a 2a 20 20 20 31  return 1).**   1
3299e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3299f 20 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20       0          
329a0 20 20 20 20 20 20 20 20 66 69 6c 65 20 20 20 20          file    
329a1 20 20 28 72 65 74 75 72 6e 20 30 29 0a 2a 2a 20    (return 0).** 
329a2 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20 20    2             
329a3 20 20 20 20 20 20 20 20 31 20 20 20 20 20 20 20          1       
329a4 20 20 20 20 20 20 20 20 20 20 20 66 69 6c 65 20             file 
329a5 20 20 20 20 20 28 72 65 74 75 72 6e 20 30 29 0a       (return 0).
329a6 2a 2a 20 20 20 32 20 20 20 20 20 20 20 20 20 20  **   2          
329a7 20 20 20 20 20 20 20 20 20 20 20 32 20 20 20 20             2    
329a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65                me
329a9 6d 6f 72 79 20 20 20 20 28 72 65 74 75 72 6e 20  mory    (return 
329aa 31 29 0a 2a 2a 20 20 20 32 20 20 20 20 20 20 20  1).**   2       
329ab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20                0 
329ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
329ad 20 6d 65 6d 6f 72 79 20 20 20 20 28 72 65 74 75   memory    (retu
329ae 72 6e 20 31 29 0a 2a 2a 20 20 20 33 20 20 20 20  rn 1).**   3    
329af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
329b0 20 61 6e 79 20 20 20 20 20 20 20 20 20 20 20 20   any            
329b1 20 20 20 20 6d 65 6d 6f 72 79 20 20 20 20 28 72      memory    (r
329b2 65 74 75 72 6e 20 31 29 0a 2a 2f 0a 53 51 4c 49  eturn 1).*/.SQLI
329b3 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
329b4 71 6c 69 74 65 33 54 65 6d 70 49 6e 4d 65 6d 6f  qlite3TempInMemo
329b5 72 79 28 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ry(const sqlite3
329b6 20 2a 64 62 29 7b 0a 23 69 66 20 53 51 4c 49 54   *db){.#if SQLIT
329b7 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 31 0a  E_TEMP_STORE==1.
329b8 20 20 72 65 74 75 72 6e 20 28 20 64 62 2d 3e 74    return ( db->t
329b9 65 6d 70 5f 73 74 6f 72 65 3d 3d 32 20 29 3b 0a  emp_store==2 );.
329ba 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54  #endif.#if SQLIT
329bb 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 32 0a  E_TEMP_STORE==2.
329bc 20 20 72 65 74 75 72 6e 20 28 20 64 62 2d 3e 74    return ( db->t
329bd 65 6d 70 5f 73 74 6f 72 65 21 3d 31 20 29 3b 0a  emp_store!=1 );.
329be 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54  #endif.#if SQLIT
329bf 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 33 0a  E_TEMP_STORE==3.
329c0 20 20 72 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64    return 1;.#end
329c1 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 45  if.#if SQLITE_TE
329c2 4d 50 5f 53 54 4f 52 45 3c 31 20 7c 7c 20 53 51  MP_STORE<1 || SQ
329c3 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3e  LITE_TEMP_STORE>
329c4 33 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 23 65  3.  return 0;.#e
329c5 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  ndif.}../*.** Th
329c6 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
329c7 6c 6c 65 64 20 74 6f 20 63 72 65 61 74 65 20 61  lled to create a
329c8 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 61   connection to a
329c9 20 64 61 74 61 62 61 73 65 20 42 54 72 65 65 0a   database BTree.
329ca 2a 2a 20 64 72 69 76 65 72 2e 20 20 49 66 20 7a  ** driver.  If z
329cb 46 69 6c 65 6e 61 6d 65 20 69 73 20 74 68 65 20  Filename is the 
329cc 6e 61 6d 65 20 6f 66 20 61 20 66 69 6c 65 2c 20  name of a file, 
329cd 74 68 65 6e 20 74 68 61 74 20 66 69 6c 65 20 69  then that file i
329ce 73 0a 2a 2a 20 6f 70 65 6e 65 64 20 61 6e 64 20  s.** opened and 
329cf 75 73 65 64 2e 20 20 49 66 20 7a 46 69 6c 65 6e  used.  If zFilen
329d0 61 6d 65 20 69 73 20 74 68 65 20 6d 61 67 69 63  ame is the magic
329d1 20 6e 61 6d 65 20 22 3a 6d 65 6d 6f 72 79 3a 22   name ":memory:"
329d2 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 64 61 74   then.** the dat
329d3 61 62 61 73 65 20 69 73 20 73 74 6f 72 65 64 20  abase is stored 
329d4 69 6e 20 6d 65 6d 6f 72 79 20 28 61 6e 64 20 69  in memory (and i
329d5 73 20 74 68 75 73 20 66 6f 72 67 6f 74 74 65 6e  s thus forgotten
329d6 20 61 73 20 73 6f 6f 6e 20 61 73 0a 2a 2a 20 74   as soon as.** t
329d7 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73  he connection is
329d8 20 63 6c 6f 73 65 64 2e 29 20 20 49 66 20 7a 46   closed.)  If zF
329d9 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c 20  ilename is NULL 
329da 74 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73  then the databas
329db 65 0a 2a 2a 20 69 73 20 61 20 22 76 69 72 74 75  e.** is a "virtu
329dc 61 6c 22 20 64 61 74 61 62 61 73 65 20 66 6f 72  al" database for
329dd 20 74 72 61 6e 73 69 65 6e 74 20 75 73 65 20 6f   transient use o
329de 6e 6c 79 20 61 6e 64 20 69 73 20 64 65 6c 65 74  nly and is delet
329df 65 64 20 61 73 0a 2a 2a 20 73 6f 6f 6e 20 61 73  ed as.** soon as
329e0 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20   the connection 
329e1 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a  is closed..**.**
329e2 20 41 20 76 69 72 74 75 61 6c 20 64 61 74 61 62   A virtual datab
329e3 61 73 65 20 63 61 6e 20 62 65 20 65 69 74 68 65  ase can be eithe
329e4 72 20 61 20 64 69 73 6b 20 66 69 6c 65 20 28 74  r a disk file (t
329e5 68 61 74 20 69 73 20 61 75 74 6f 6d 61 74 69 63  hat is automatic
329e6 61 6c 6c 79 0a 2a 2a 20 64 65 6c 65 74 65 64 20  ally.** deleted 
329e7 77 68 65 6e 20 74 68 65 20 66 69 6c 65 20 69 73  when the file is
329e8 20 63 6c 6f 73 65 64 29 20 6f 72 20 69 74 20 61   closed) or it a
329e9 6e 20 62 65 20 68 65 6c 64 20 65 6e 74 69 72 65  n be held entire
329ea 6c 79 20 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a 2a  ly in memory..**
329eb 20 54 68 65 20 73 71 6c 69 74 65 33 54 65 6d 70   The sqlite3Temp
329ec 49 6e 4d 65 6d 6f 72 79 28 29 20 66 75 6e 63 74  InMemory() funct
329ed 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 64  ion is used to d
329ee 65 74 65 72 6d 69 6e 65 20 77 68 69 63 68 2e 0a  etermine which..
329ef 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
329f0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
329f1 65 65 46 61 63 74 6f 72 79 28 0a 20 20 63 6f 6e  eeFactory(.  con
329f2 73 74 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  st sqlite3 *db, 
329f3 20 20 20 20 20 20 20 2f 2a 20 4d 61 69 6e 20 64         /* Main d
329f4 61 74 61 62 61 73 65 20 77 68 65 6e 20 6f 70 65  atabase when ope
329f5 6e 69 6e 67 20 61 75 78 20 6f 74 68 65 72 77 69  ning aux otherwi
329f6 73 65 20 30 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  se 0 */.  const 
329f7 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c  char *zFilename,
329f8 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
329f9 68 65 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 69  he file containi
329fa 6e 67 20 74 68 65 20 42 54 72 65 65 20 64 61 74  ng the BTree dat
329fb 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6f  abase */.  int o
329fc 6d 69 74 4a 6f 75 72 6e 61 6c 2c 20 20 20 20 20  mitJournal,     
329fd 20 20 20 20 20 2f 2a 20 69 66 20 54 52 55 45 20       /* if TRUE 
329fe 74 68 65 6e 20 64 6f 20 6e 6f 74 20 6a 6f 75 72  then do not jour
329ff 6e 61 6c 20 74 68 69 73 20 66 69 6c 65 20 2a 2f  nal this file */
32a00 0a 20 20 69 6e 74 20 6e 43 61 63 68 65 2c 20 20  .  int nCache,  
32a01 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
32a02 48 6f 77 20 6d 61 6e 79 20 70 61 67 65 73 20 69  How many pages i
32a03 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65  n the page cache
32a04 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61   */.  int vfsFla
32a05 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  gs,             
32a06 2f 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64 20  /* Flags passed 
32a07 74 68 72 6f 75 67 68 20 74 6f 20 76 66 73 4f 70  through to vfsOp
32a08 65 6e 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 2a  en */.  Btree **
32a09 70 70 42 74 72 65 65 20 20 20 20 20 20 20 20 20  ppBtree         
32a0a 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
32a0b 6e 65 77 20 42 74 72 65 65 20 6f 62 6a 65 63 74  new Btree object
32a0c 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f   written here */
32a0d 0a 29 7b 0a 20 20 69 6e 74 20 62 74 46 6c 61 67  .){.  int btFlag
32a0e 73 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b  s = 0;.  int rc;
32a0f 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 73 71  .  .  assert( sq
32a10 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
32a11 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  (db->mutex) );. 
32a12 20 61 73 73 65 72 74 28 20 70 70 42 74 72 65 65   assert( ppBtree
32a13 20 21 3d 20 30 29 3b 0a 20 20 69 66 28 20 6f 6d   != 0);.  if( om
32a14 69 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20  itJournal ){.   
32a15 20 62 74 46 6c 61 67 73 20 7c 3d 20 42 54 52 45   btFlags |= BTRE
32a16 45 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 3b 0a  E_OMIT_JOURNAL;.
32a17 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c    }.  if( db->fl
32a18 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4e 6f 52  ags & SQLITE_NoR
32a19 65 61 64 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 62  eadlock ){.    b
32a1a 74 46 6c 61 67 73 20 7c 3d 20 42 54 52 45 45 5f  tFlags |= BTREE_
32a1b 4e 4f 5f 52 45 41 44 4c 4f 43 4b 3b 0a 20 20 7d  NO_READLOCK;.  }
32a1c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
32a1d 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 20 20  OMIT_MEMORYDB.  
32a1e 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30  if( zFilename==0
32a1f 20 26 26 20 73 71 6c 69 74 65 33 54 65 6d 70 49   && sqlite3TempI
32a20 6e 4d 65 6d 6f 72 79 28 64 62 29 20 29 7b 0a 20  nMemory(db) ){. 
32a21 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 22     zFilename = "
32a22 3a 6d 65 6d 6f 72 79 3a 22 3b 0a 20 20 7d 0a 23  :memory:";.  }.#
32a23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 28 76 66  endif..  if( (vf
32a24 73 46 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  sFlags & SQLITE_
32a25 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 21 3d 30  OPEN_MAIN_DB)!=0
32a26 20 26 26 20 28 7a 46 69 6c 65 6e 61 6d 65 3d 3d   && (zFilename==
32a27 30 20 7c 7c 20 2a 7a 46 69 6c 65 6e 61 6d 65 3d  0 || *zFilename=
32a28 3d 30 29 20 29 7b 0a 20 20 20 20 76 66 73 46 6c  =0) ){.    vfsFl
32a29 61 67 73 20 3d 20 28 76 66 73 46 6c 61 67 73 20  ags = (vfsFlags 
32a2a 26 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  & ~SQLITE_OPEN_M
32a2b 41 49 4e 5f 44 42 29 20 7c 20 53 51 4c 49 54 45  AIN_DB) | SQLITE
32a2c 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 3b 0a 20  _OPEN_TEMP_DB;. 
32a2d 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65   }.  rc = sqlite
32a2e 33 42 74 72 65 65 4f 70 65 6e 28 7a 46 69 6c 65  3BtreeOpen(zFile
32a2f 6e 61 6d 65 2c 20 28 73 71 6c 69 74 65 33 20 2a  name, (sqlite3 *
32a30 29 64 62 2c 20 70 70 42 74 72 65 65 2c 20 62 74  )db, ppBtree, bt
32a31 46 6c 61 67 73 2c 20 76 66 73 46 6c 61 67 73 29  Flags, vfsFlags)
32a32 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 42  ;..  /* If the B
32a33 2d 54 72 65 65 20 77 61 73 20 73 75 63 63 65 73  -Tree was succes
32a34 73 66 75 6c 6c 79 20 6f 70 65 6e 65 64 2c 20 73  sfully opened, s
32a35 65 74 20 74 68 65 20 70 61 67 65 72 2d 63 61 63  et the pager-cac
32a36 68 65 20 73 69 7a 65 20 74 6f 20 74 68 65 0a 20  he size to the. 
32a37 20 2a 2a 20 64 65 66 61 75 6c 74 20 76 61 6c 75   ** default valu
32a38 65 2e 20 45 78 63 65 70 74 2c 20 69 66 20 74 68  e. Except, if th
32a39 65 20 63 61 6c 6c 20 74 6f 20 42 74 72 65 65 4f  e call to BtreeO
32a3a 70 65 6e 28 29 20 72 65 74 75 72 6e 65 64 20 61  pen() returned a
32a3b 20 68 61 6e 64 6c 65 0a 20 20 2a 2a 20 6f 70 65   handle.  ** ope
32a3c 6e 20 6f 6e 20 61 6e 20 65 78 69 73 74 69 6e 67  n on an existing
32a3d 20 73 68 61 72 65 64 20 70 61 67 65 72 2d 63 61   shared pager-ca
32a3e 63 68 65 2c 20 64 6f 20 6e 6f 74 20 63 68 61 6e  che, do not chan
32a3f 67 65 20 74 68 65 20 70 61 67 65 72 2d 63 61 63  ge the pager-cac
32a40 68 65 20 0a 20 20 2a 2a 20 73 69 7a 65 2e 0a 20  he .  ** size.. 
32a41 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   */.  if( rc==SQ
32a42 4c 49 54 45 5f 4f 4b 20 26 26 20 30 3d 3d 73 71  LITE_OK && 0==sq
32a43 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61  lite3BtreeSchema
32a44 28 2a 70 70 42 74 72 65 65 2c 20 30 2c 20 30 29  (*ppBtree, 0, 0)
32a45 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42   ){.    sqlite3B
32a46 74 72 65 65 53 65 74 43 61 63 68 65 53 69 7a 65  treeSetCacheSize
32a47 28 2a 70 70 42 74 72 65 65 2c 20 6e 43 61 63 68  (*ppBtree, nCach
32a48 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  e);.  }.  return
32a49 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
32a4a 74 75 72 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64  turn UTF-8 encod
32a4b 65 64 20 45 6e 67 6c 69 73 68 20 6c 61 6e 67 75  ed English langu
32a4c 61 67 65 20 65 78 70 6c 61 6e 61 74 69 6f 6e 20  age explanation 
32a4d 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  of the most rece
32a4e 6e 74 0a 2a 2a 20 65 72 72 6f 72 2e 0a 2a 2f 0a  nt.** error..*/.
32a4f 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
32a50 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 65   char *sqlite3_e
32a51 72 72 6d 73 67 28 73 71 6c 69 74 65 33 20 2a 64  rrmsg(sqlite3 *d
32a52 62 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  b){.  const char
32a53 20 2a 7a 3b 0a 20 20 69 66 28 20 21 64 62 20 29   *z;.  if( !db )
32a54 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c  {.    return sql
32a55 69 74 65 33 45 72 72 53 74 72 28 53 51 4c 49 54  ite3ErrStr(SQLIT
32a56 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20  E_NOMEM);.  }.  
32a57 69 66 28 20 21 73 71 6c 69 74 65 33 53 61 66 65  if( !sqlite3Safe
32a58 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28  tyCheckSickOrOk(
32a59 64 62 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  db) ){.    retur
32a5a 6e 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 28  n sqlite3ErrStr(
32a5b 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 29 3b 0a  SQLITE_MISUSE);.
32a5c 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75    }.  sqlite3_mu
32a5d 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
32a5e 74 65 78 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e  tex);.  if( db->
32a5f 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
32a60 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 45      z = sqlite3E
32a61 72 72 53 74 72 28 53 51 4c 49 54 45 5f 4e 4f 4d  rrStr(SQLITE_NOM
32a62 45 4d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  EM);.  }else{.  
32a63 20 20 7a 20 3d 20 28 63 68 61 72 2a 29 73 71 6c    z = (char*)sql
32a64 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
32a65 64 62 2d 3e 70 45 72 72 29 3b 0a 20 20 20 20 61  db->pErr);.    a
32a66 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c  ssert( !db->mall
32a67 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20  ocFailed );.    
32a68 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20  if( z==0 ){.    
32a69 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 45 72 72    z = sqlite3Err
32a6a 53 74 72 28 64 62 2d 3e 65 72 72 43 6f 64 65 29  Str(db->errCode)
32a6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71  ;.    }.  }.  sq
32a6c 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
32a6d 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
32a6e 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 23 69 66  return z;.}..#if
32a6f 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
32a70 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 52 65 74  _UTF16./*.** Ret
32a71 75 72 6e 20 55 54 46 2d 31 36 20 65 6e 63 6f 64  urn UTF-16 encod
32a72 65 64 20 45 6e 67 6c 69 73 68 20 6c 61 6e 67 75  ed English langu
32a73 61 67 65 20 65 78 70 6c 61 6e 61 74 69 6f 6e 20  age explanation 
32a74 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  of the most rece
32a75 6e 74 0a 2a 2a 20 65 72 72 6f 72 2e 0a 2a 2f 0a  nt.** error..*/.
32a76 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
32a77 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 65   void *sqlite3_e
32a78 72 72 6d 73 67 31 36 28 73 71 6c 69 74 65 33 20  rrmsg16(sqlite3 
32a79 2a 64 62 29 7b 0a 20 20 73 74 61 74 69 63 20 63  *db){.  static c
32a7a 6f 6e 73 74 20 75 31 36 20 6f 75 74 4f 66 4d 65  onst u16 outOfMe
32a7b 6d 5b 5d 20 3d 20 7b 0a 20 20 20 20 27 6f 27 2c  m[] = {.    'o',
32a7c 20 27 75 27 2c 20 27 74 27 2c 20 27 20 27 2c 20   'u', 't', ' ', 
32a7d 27 6f 27 2c 20 27 66 27 2c 20 27 20 27 2c 20 27  'o', 'f', ' ', '
32a7e 6d 27 2c 20 27 65 27 2c 20 27 6d 27 2c 20 27 6f  m', 'e', 'm', 'o
32a7f 27 2c 20 27 72 27 2c 20 27 79 27 2c 20 30 0a 20  ', 'r', 'y', 0. 
32a80 20 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e   };.  static con
32a81 73 74 20 75 31 36 20 6d 69 73 75 73 65 5b 5d 20  st u16 misuse[] 
32a82 3d 20 7b 0a 20 20 20 20 27 6c 27 2c 20 27 69 27  = {.    'l', 'i'
32a83 2c 20 27 62 27 2c 20 27 72 27 2c 20 27 61 27 2c  , 'b', 'r', 'a',
32a84 20 27 72 27 2c 20 27 79 27 2c 20 27 20 27 2c 20   'r', 'y', ' ', 
32a85 0a 20 20 20 20 27 72 27 2c 20 27 6f 27 2c 20 27  .    'r', 'o', '
32a86 75 27 2c 20 27 74 27 2c 20 27 69 27 2c 20 27 6e  u', 't', 'i', 'n
32a87 27 2c 20 27 65 27 2c 20 27 20 27 2c 20 0a 20 20  ', 'e', ' ', .  
32a88 20 20 27 63 27 2c 20 27 61 27 2c 20 27 6c 27 2c    'c', 'a', 'l',
32a89 20 27 6c 27 2c 20 27 65 27 2c 20 27 64 27 2c 20   'l', 'e', 'd', 
32a8a 27 20 27 2c 20 0a 20 20 20 20 27 6f 27 2c 20 27  ' ', .    'o', '
32a8b 75 27 2c 20 27 74 27 2c 20 27 20 27 2c 20 0a 20  u', 't', ' ', . 
32a8c 20 20 20 27 6f 27 2c 20 27 66 27 2c 20 27 20 27     'o', 'f', ' '
32a8d 2c 20 0a 20 20 20 20 27 73 27 2c 20 27 65 27 2c  , .    's', 'e',
32a8e 20 27 71 27 2c 20 27 75 27 2c 20 27 65 27 2c 20   'q', 'u', 'e', 
32a8f 27 6e 27 2c 20 27 63 27 2c 20 27 65 27 2c 20 30  'n', 'c', 'e', 0
32a90 0a 20 20 7d 3b 0a 0a 20 20 63 6f 6e 73 74 20 76  .  };..  const v
32a91 6f 69 64 20 2a 7a 3b 0a 20 20 69 66 28 20 21 64  oid *z;.  if( !d
32a92 62 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  b ){.    return 
32a93 28 76 6f 69 64 20 2a 29 6f 75 74 4f 66 4d 65 6d  (void *)outOfMem
32a94 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 73 71 6c  ;.  }.  if( !sql
32a95 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 53  ite3SafetyCheckS
32a96 69 63 6b 4f 72 4f 6b 28 64 62 29 20 29 7b 0a 20  ickOrOk(db) ){. 
32a97 20 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 20     return (void 
32a98 2a 29 6d 69 73 75 73 65 3b 0a 20 20 7d 0a 20 20  *)misuse;.  }.  
32a99 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
32a9a 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
32a9b 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
32a9c 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 7a 20  Failed ){.    z 
32a9d 3d 20 28 76 6f 69 64 20 2a 29 6f 75 74 4f 66 4d  = (void *)outOfM
32a9e 65 6d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  em;.  }else{.   
32a9f 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c   z = sqlite3_val
32aa0 75 65 5f 74 65 78 74 31 36 28 64 62 2d 3e 70 45  ue_text16(db->pE
32aa1 72 72 29 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d  rr);.    if( z==
32aa2 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
32aa3 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 64 62  e3ValueSetStr(db
32aa4 2d 3e 70 45 72 72 2c 20 2d 31 2c 20 73 71 6c 69  ->pErr, -1, sqli
32aa5 74 65 33 45 72 72 53 74 72 28 64 62 2d 3e 65 72  te3ErrStr(db->er
32aa6 72 43 6f 64 65 29 2c 0a 20 20 20 20 20 20 20 20  rCode),.        
32aa7 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20     SQLITE_UTF8, 
32aa8 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
32aa9 20 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65        z = sqlite
32aaa 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 64  3_value_text16(d
32aab 62 2d 3e 70 45 72 72 29 3b 0a 20 20 20 20 7d 0a  b->pErr);.    }.
32aac 20 20 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63 28      /* A malloc(
32aad 29 20 6d 61 79 20 68 61 76 65 20 66 61 69 6c 65  ) may have faile
32aae 64 20 77 69 74 68 69 6e 20 74 68 65 20 63 61 6c  d within the cal
32aaf 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 61 6c  l to sqlite3_val
32ab0 75 65 5f 74 65 78 74 31 36 28 29 0a 20 20 20 20  ue_text16().    
32ab1 2a 2a 20 61 62 6f 76 65 2e 20 49 66 20 74 68 69  ** above. If thi
32ab2 73 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74  s is the case, t
32ab3 68 65 6e 20 74 68 65 20 64 62 2d 3e 6d 61 6c 6c  hen the db->mall
32ab4 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 6e 65  ocFailed flag ne
32ab5 65 64 73 20 74 6f 0a 20 20 20 20 2a 2a 20 62 65  eds to.    ** be
32ab6 20 63 6c 65 61 72 65 64 20 62 65 66 6f 72 65 20   cleared before 
32ab7 72 65 74 75 72 6e 69 6e 67 2e 20 44 6f 20 74 68  returning. Do th
32ab8 69 73 20 64 69 72 65 63 74 6c 79 2c 20 69 6e 73  is directly, ins
32ab9 74 65 61 64 20 6f 66 20 76 69 61 0a 20 20 20 20  tead of via.    
32aba 2a 2a 20 73 71 6c 69 74 65 33 41 70 69 45 78 69  ** sqlite3ApiExi
32abb 74 28 29 2c 20 74 6f 20 61 76 6f 69 64 20 73 65  t(), to avoid se
32abc 74 74 69 6e 67 20 74 68 65 20 64 61 74 61 62 61  tting the databa
32abd 73 65 20 68 61 6e 64 6c 65 20 65 72 72 6f 72 20  se handle error 
32abe 6d 65 73 73 61 67 65 2e 0a 20 20 20 20 2a 2f 0a  message..    */.
32abf 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
32ac0 69 6c 65 64 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  iled = 0;.  }.  
32ac1 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
32ac2 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
32ac3 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 23 65    return z;.}.#e
32ac4 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
32ac5 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a  MIT_UTF16 */../*
32ac6 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6d  .** Return the m
32ac7 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72  ost recent error
32ac8 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20   code generated 
32ac9 62 79 20 61 6e 20 53 51 4c 69 74 65 20 72 6f 75  by an SQLite rou
32aca 74 69 6e 65 2e 20 49 66 20 4e 55 4c 4c 20 69 73  tine. If NULL is
32acb 0a 2a 2a 20 70 61 73 73 65 64 20 74 6f 20 74 68  .** passed to th
32acc 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 77 65 20  is function, we 
32acd 61 73 73 75 6d 65 20 61 20 6d 61 6c 6c 6f 63 28  assume a malloc(
32ace 29 20 66 61 69 6c 65 64 20 64 75 72 69 6e 67 20  ) failed during 
32acf 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 2e 0a  sqlite3_open()..
32ad0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
32ad1 74 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64  t sqlite3_errcod
32ad2 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  e(sqlite3 *db){.
32ad3 20 20 69 66 28 20 64 62 20 26 26 20 21 73 71 6c    if( db && !sql
32ad4 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 53  ite3SafetyCheckS
32ad5 69 63 6b 4f 72 4f 6b 28 64 62 29 20 29 7b 0a 20  ickOrOk(db) ){. 
32ad6 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
32ad7 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 69  _MISUSE;.  }.  i
32ad8 66 28 20 21 64 62 20 7c 7c 20 64 62 2d 3e 6d 61  f( !db || db->ma
32ad9 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
32ada 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
32adb 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 72 65 74  NOMEM;.  }.  ret
32adc 75 72 6e 20 64 62 2d 3e 65 72 72 43 6f 64 65 20  urn db->errCode 
32add 26 20 64 62 2d 3e 65 72 72 4d 61 73 6b 3b 0a 7d  & db->errMask;.}
32ade 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
32adf 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64  sqlite3_extended
32ae0 5f 65 72 72 63 6f 64 65 28 73 71 6c 69 74 65 33  _errcode(sqlite3
32ae1 20 2a 64 62 29 7b 0a 20 20 69 66 28 20 64 62 20   *db){.  if( db 
32ae2 26 26 20 21 73 71 6c 69 74 65 33 53 61 66 65 74  && !sqlite3Safet
32ae3 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 64  yCheckSickOrOk(d
32ae4 62 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  b) ){.    return
32ae5 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a   SQLITE_MISUSE;.
32ae6 20 20 7d 0a 20 20 69 66 28 20 21 64 62 20 7c 7c    }.  if( !db ||
32ae7 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
32ae8 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  d ){.    return 
32ae9 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
32aea 7d 0a 20 20 72 65 74 75 72 6e 20 64 62 2d 3e 65  }.  return db->e
32aeb 72 72 43 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  rrCode;.}../*.**
32aec 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 63 6f   Create a new co
32aed 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e  llating function
32aee 20 66 6f 72 20 64 61 74 61 62 61 73 65 20 22 64   for database "d
32aef 62 22 2e 20 20 54 68 65 20 6e 61 6d 65 20 69 73  b".  The name is
32af0 20 7a 4e 61 6d 65 0a 2a 2a 20 61 6e 64 20 74 68   zName.** and th
32af1 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 65 6e  e encoding is en
32af2 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  c..*/.static int
32af3 20 63 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e   createCollation
32af4 28 0a 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c  (.  sqlite3* db,
32af5 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
32af6 4e 61 6d 65 2c 20 0a 20 20 75 38 20 65 6e 63 2c  Name, .  u8 enc,
32af7 0a 20 20 75 38 20 63 6f 6c 6c 54 79 70 65 2c 0a  .  u8 collType,.
32af8 20 20 76 6f 69 64 2a 20 70 43 74 78 2c 0a 20 20    void* pCtx,.  
32af9 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76  int(*xCompare)(v
32afa 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  oid*,int,const v
32afb 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  oid*,int,const v
32afc 6f 69 64 2a 29 2c 0a 20 20 76 6f 69 64 28 2a 78  oid*),.  void(*x
32afd 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20  Del)(void*).){. 
32afe 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b   CollSeq *pColl;
32aff 0a 20 20 69 6e 74 20 65 6e 63 32 3b 0a 20 20 69  .  int enc2;.  i
32b00 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74  nt nName = sqlit
32b01 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65  e3Strlen30(zName
32b02 29 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20  );.  .  assert( 
32b03 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
32b04 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  ld(db->mutex) );
32b05 0a 0a 20 20 2f 2a 20 49 66 20 53 51 4c 49 54 45  ..  /* If SQLITE
32b06 5f 55 54 46 31 36 20 69 73 20 73 70 65 63 69 66  _UTF16 is specif
32b07 69 65 64 20 61 73 20 74 68 65 20 65 6e 63 6f 64  ied as the encod
32b08 69 6e 67 20 74 79 70 65 2c 20 74 72 61 6e 73 66  ing type, transf
32b09 6f 72 6d 20 74 68 69 73 0a 20 20 2a 2a 20 74 6f  orm this.  ** to
32b0a 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 55   one of SQLITE_U
32b0b 54 46 31 36 4c 45 20 6f 72 20 53 51 4c 49 54 45  TF16LE or SQLITE
32b0c 5f 55 54 46 31 36 42 45 20 75 73 69 6e 67 20 74  _UTF16BE using t
32b0d 68 65 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 55  he.  ** SQLITE_U
32b0e 54 46 31 36 4e 41 54 49 56 45 20 6d 61 63 72 6f  TF16NATIVE macro
32b0f 2e 20 53 51 4c 49 54 45 5f 55 54 46 31 36 20 69  . SQLITE_UTF16 i
32b10 73 20 6e 6f 74 20 75 73 65 64 20 69 6e 74 65 72  s not used inter
32b11 6e 61 6c 6c 79 2e 0a 20 20 2a 2f 0a 20 20 65 6e  nally..  */.  en
32b12 63 32 20 3d 20 65 6e 63 3b 0a 20 20 74 65 73 74  c2 = enc;.  test
32b13 63 61 73 65 28 20 65 6e 63 32 3d 3d 53 51 4c 49  case( enc2==SQLI
32b14 54 45 5f 55 54 46 31 36 20 29 3b 0a 20 20 74 65  TE_UTF16 );.  te
32b15 73 74 63 61 73 65 28 20 65 6e 63 32 3d 3d 53 51  stcase( enc2==SQ
32b16 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e  LITE_UTF16_ALIGN
32b17 45 44 20 29 3b 0a 20 20 69 66 28 20 65 6e 63 32  ED );.  if( enc2
32b18 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 20 7c  ==SQLITE_UTF16 |
32b19 7c 20 65 6e 63 32 3d 3d 53 51 4c 49 54 45 5f 55  | enc2==SQLITE_U
32b1a 54 46 31 36 5f 41 4c 49 47 4e 45 44 20 29 7b 0a  TF16_ALIGNED ){.
32b1b 20 20 20 20 65 6e 63 32 20 3d 20 53 51 4c 49 54      enc2 = SQLIT
32b1c 45 5f 55 54 46 31 36 4e 41 54 49 56 45 3b 0a 20  E_UTF16NATIVE;. 
32b1d 20 7d 0a 20 20 69 66 28 20 65 6e 63 32 3c 53 51   }.  if( enc2<SQ
32b1e 4c 49 54 45 5f 55 54 46 38 20 7c 7c 20 65 6e 63  LITE_UTF8 || enc
32b1f 32 3e 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45  2>SQLITE_UTF16BE
32b20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
32b21 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20  QLITE_MISUSE;.  
32b22 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66  }..  /* Check if
32b23 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 72 65   this call is re
32b24 6d 6f 76 69 6e 67 20 6f 72 20 72 65 70 6c 61 63  moving or replac
32b25 69 6e 67 20 61 6e 20 65 78 69 73 74 69 6e 67 20  ing an existing 
32b26 63 6f 6c 6c 61 74 69 6f 6e 20 0a 20 20 2a 2a 20  collation .  ** 
32b27 73 65 71 75 65 6e 63 65 2e 20 49 66 20 73 6f 2c  sequence. If so,
32b28 20 61 6e 64 20 74 68 65 72 65 20 61 72 65 20 61   and there are a
32b29 63 74 69 76 65 20 56 4d 73 2c 20 72 65 74 75 72  ctive VMs, retur
32b2a 6e 20 62 75 73 79 2e 20 49 66 20 74 68 65 72 65  n busy. If there
32b2b 0a 20 20 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74  .  ** are no act
32b2c 69 76 65 20 56 4d 73 2c 20 69 6e 76 61 6c 69 64  ive VMs, invalid
32b2d 61 74 65 20 61 6e 79 20 70 72 65 2d 63 6f 6d 70  ate any pre-comp
32b2e 69 6c 65 64 20 73 74 61 74 65 6d 65 6e 74 73 2e  iled statements.
32b2f 0a 20 20 2a 2f 0a 20 20 70 43 6f 6c 6c 20 3d 20  .  */.  pColl = 
32b30 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53  sqlite3FindCollS
32b31 65 71 28 64 62 2c 20 28 75 38 29 65 6e 63 32 2c  eq(db, (u8)enc2,
32b32 20 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 69 66   zName, 0);.  if
32b33 28 20 70 43 6f 6c 6c 20 26 26 20 70 43 6f 6c 6c  ( pColl && pColl
32b34 2d 3e 78 43 6d 70 20 29 7b 0a 20 20 20 20 69 66  ->xCmp ){.    if
32b35 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65  ( db->activeVdbe
32b36 43 6e 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Cnt ){.      sql
32b37 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51  ite3Error(db, SQ
32b38 4c 49 54 45 5f 42 55 53 59 2c 20 0a 20 20 20 20  LITE_BUSY, .    
32b39 20 20 20 20 22 75 6e 61 62 6c 65 20 74 6f 20 64      "unable to d
32b3a 65 6c 65 74 65 2f 6d 6f 64 69 66 79 20 63 6f 6c  elete/modify col
32b3b 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
32b3c 64 75 65 20 74 6f 20 61 63 74 69 76 65 20 73 74  due to active st
32b3d 61 74 65 6d 65 6e 74 73 22 29 3b 0a 20 20 20 20  atements");.    
32b3e 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
32b3f 42 55 53 59 3b 0a 20 20 20 20 7d 0a 20 20 20 20  BUSY;.    }.    
32b40 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65  sqlite3ExpirePre
32b41 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28  paredStatements(
32b42 64 62 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20  db);..    /* If 
32b43 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
32b44 63 65 20 70 43 6f 6c 6c 20 77 61 73 20 63 72 65  ce pColl was cre
32b45 61 74 65 64 20 64 69 72 65 63 74 6c 79 20 62 79  ated directly by
32b46 20 61 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20 2a   a call to.    *
32b47 2a 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  * sqlite3_create
32b48 5f 63 6f 6c 6c 61 74 69 6f 6e 2c 20 61 6e 64 20  _collation, and 
32b49 6e 6f 74 20 67 65 6e 65 72 61 74 65 64 20 62 79  not generated by
32b4a 20 73 79 6e 74 68 43 6f 6c 6c 53 65 71 28 29 2c   synthCollSeq(),
32b4b 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 61 6e 79  .    ** then any
32b4c 20 63 6f 70 69 65 73 20 6d 61 64 65 20 62 79 20   copies made by 
32b4d 73 79 6e 74 68 43 6f 6c 6c 53 65 71 28 29 20 6e  synthCollSeq() n
32b4e 65 65 64 20 74 6f 20 62 65 20 69 6e 76 61 6c 69  eed to be invali
32b4f 64 61 74 65 64 2e 0a 20 20 20 20 2a 2a 20 41 6c  dated..    ** Al
32b50 73 6f 2c 20 63 6f 6c 6c 61 74 69 6f 6e 20 64 65  so, collation de
32b51 73 74 72 75 63 74 6f 72 20 2d 20 43 6f 6c 6c 53  structor - CollS
32b52 65 71 2e 78 44 65 6c 28 29 20 2d 20 66 75 6e 63  eq.xDel() - func
32b53 74 69 6f 6e 20 6d 61 79 20 6e 65 65 64 0a 20 20  tion may need.  
32b54 20 20 2a 2a 20 74 6f 20 62 65 20 63 61 6c 6c 65    ** to be calle
32b55 64 2e 0a 20 20 20 20 2a 2f 20 0a 20 20 20 20 69  d..    */ .    i
32b56 66 28 20 28 70 43 6f 6c 6c 2d 3e 65 6e 63 20 26  f( (pColl->enc &
32b57 20 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41   ~SQLITE_UTF16_A
32b58 4c 49 47 4e 45 44 29 3d 3d 65 6e 63 32 20 29 7b  LIGNED)==enc2 ){
32b59 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a  .      CollSeq *
32b5a 61 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 48  aColl = sqlite3H
32b5b 61 73 68 46 69 6e 64 28 26 64 62 2d 3e 61 43 6f  ashFind(&db->aCo
32b5c 6c 6c 53 65 71 2c 20 7a 4e 61 6d 65 2c 20 6e 4e  llSeq, zName, nN
32b5d 61 6d 65 29 3b 0a 20 20 20 20 20 20 69 6e 74 20  ame);.      int 
32b5e 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30  j;.      for(j=0
32b5f 3b 20 6a 3c 33 3b 20 6a 2b 2b 29 7b 0a 20 20 20  ; j<3; j++){.   
32b60 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 20       CollSeq *p 
32b61 3d 20 26 61 43 6f 6c 6c 5b 6a 5d 3b 0a 20 20 20  = &aColl[j];.   
32b62 20 20 20 20 20 69 66 28 20 70 2d 3e 65 6e 63 3d       if( p->enc=
32b63 3d 70 43 6f 6c 6c 2d 3e 65 6e 63 20 29 7b 0a 20  =pColl->enc ){. 
32b64 20 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e           if( p->
32b65 78 44 65 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  xDel ){.        
32b66 20 20 20 20 70 2d 3e 78 44 65 6c 28 70 2d 3e 70      p->xDel(p->p
32b67 55 73 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20  User);.         
32b68 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e   }.          p->
32b69 78 43 6d 70 20 3d 20 30 3b 0a 20 20 20 20 20 20  xCmp = 0;.      
32b6a 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
32b6b 7d 0a 20 20 7d 0a 0a 20 20 70 43 6f 6c 6c 20 3d  }.  }..  pColl =
32b6c 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c   sqlite3FindColl
32b6d 53 65 71 28 64 62 2c 20 28 75 38 29 65 6e 63 32  Seq(db, (u8)enc2
32b6e 2c 20 7a 4e 61 6d 65 2c 20 31 29 3b 0a 20 20 69  , zName, 1);.  i
32b6f 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  f( pColl ){.    
32b70 70 43 6f 6c 6c 2d 3e 78 43 6d 70 20 3d 20 78 43  pColl->xCmp = xC
32b71 6f 6d 70 61 72 65 3b 0a 20 20 20 20 70 43 6f 6c  ompare;.    pCol
32b72 6c 2d 3e 70 55 73 65 72 20 3d 20 70 43 74 78 3b  l->pUser = pCtx;
32b73 0a 20 20 20 20 70 43 6f 6c 6c 2d 3e 78 44 65 6c  .    pColl->xDel
32b74 20 3d 20 78 44 65 6c 3b 0a 20 20 20 20 70 43 6f   = xDel;.    pCo
32b75 6c 6c 2d 3e 65 6e 63 20 3d 20 28 75 38 29 28 65  ll->enc = (u8)(e
32b76 6e 63 32 20 7c 20 28 65 6e 63 20 26 20 53 51 4c  nc2 | (enc & SQL
32b77 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45  ITE_UTF16_ALIGNE
32b78 44 29 29 3b 0a 20 20 20 20 70 43 6f 6c 6c 2d 3e  D));.    pColl->
32b79 74 79 70 65 20 3d 20 63 6f 6c 6c 54 79 70 65 3b  type = collType;
32b7a 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 72  .  }.  sqlite3Er
32b7b 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f  ror(db, SQLITE_O
32b7c 4b 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 20  K, 0);.  return 
32b7d 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f  SQLITE_OK;.}.../
32b7e 2a 0a 2a 2a 20 54 68 69 73 20 61 72 72 61 79 20  *.** This array 
32b7f 64 65 66 69 6e 65 73 20 68 61 72 64 20 75 70 70  defines hard upp
32b80 65 72 20 62 6f 75 6e 64 73 20 6f 6e 20 6c 69 6d  er bounds on lim
32b81 69 74 20 76 61 6c 75 65 73 2e 20 20 54 68 65 0a  it values.  The.
32b82 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 72 20 6d  ** initializer m
32b83 75 73 74 20 62 65 20 6b 65 70 74 20 69 6e 20 73  ust be kept in s
32b84 79 6e 63 20 77 69 74 68 20 74 68 65 20 53 51 4c  ync with the SQL
32b85 49 54 45 5f 4c 49 4d 49 54 5f 2a 0a 2a 2a 20 23  ITE_LIMIT_*.** #
32b86 64 65 66 69 6e 65 73 20 69 6e 20 73 71 6c 69 74  defines in sqlit
32b87 65 33 2e 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  e3.h..*/.static 
32b88 63 6f 6e 73 74 20 69 6e 74 20 61 48 61 72 64 4c  const int aHardL
32b89 69 6d 69 74 5b 5d 20 3d 20 7b 0a 20 20 53 51 4c  imit[] = {.  SQL
32b8a 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 2c 0a  ITE_MAX_LENGTH,.
32b8b 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c    SQLITE_MAX_SQL
32b8c 5f 4c 45 4e 47 54 48 2c 0a 20 20 53 51 4c 49 54  _LENGTH,.  SQLIT
32b8d 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 2c 0a 20 20  E_MAX_COLUMN,.  
32b8e 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f  SQLITE_MAX_EXPR_
32b8f 44 45 50 54 48 2c 0a 20 20 53 51 4c 49 54 45 5f  DEPTH,.  SQLITE_
32b90 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c  MAX_COMPOUND_SEL
32b91 45 43 54 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41  ECT,.  SQLITE_MA
32b92 58 5f 56 44 42 45 5f 4f 50 2c 0a 20 20 53 51 4c  X_VDBE_OP,.  SQL
32b93 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e  ITE_MAX_FUNCTION
32b94 5f 41 52 47 2c 0a 20 20 53 51 4c 49 54 45 5f 4d  _ARG,.  SQLITE_M
32b95 41 58 5f 41 54 54 41 43 48 45 44 2c 0a 20 20 53  AX_ATTACHED,.  S
32b96 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50  QLITE_MAX_LIKE_P
32b97 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 2c 0a 20  ATTERN_LENGTH,. 
32b98 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49   SQLITE_MAX_VARI
32b99 41 42 4c 45 5f 4e 55 4d 42 45 52 2c 0a 20 20 53  ABLE_NUMBER,.  S
32b9a 51 4c 49 54 45 5f 4d 41 58 5f 54 52 49 47 47 45  QLITE_MAX_TRIGGE
32b9b 52 5f 44 45 50 54 48 2c 0a 7d 3b 0a 0a 2f 2a 0a  R_DEPTH,.};../*.
32b9c 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65  ** Make sure the
32b9d 20 68 61 72 64 20 6c 69 6d 69 74 73 20 61 72 65   hard limits are
32b9e 20 73 65 74 20 74 6f 20 72 65 61 73 6f 6e 61 62   set to reasonab
32b9f 6c 65 20 76 61 6c 75 65 73 0a 2a 2f 0a 23 69 66  le values.*/.#if
32ba0 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47   SQLITE_MAX_LENG
32ba1 54 48 3c 31 30 30 0a 23 20 65 72 72 6f 72 20 53  TH<100.# error S
32ba2 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48  QLITE_MAX_LENGTH
32ba3 20 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61 73   must be at leas
32ba4 74 20 31 30 30 0a 23 65 6e 64 69 66 0a 23 69 66  t 100.#endif.#if
32ba5 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f   SQLITE_MAX_SQL_
32ba6 4c 45 4e 47 54 48 3c 31 30 30 0a 23 20 65 72 72  LENGTH<100.# err
32ba7 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51  or SQLITE_MAX_SQ
32ba8 4c 5f 4c 45 4e 47 54 48 20 6d 75 73 74 20 62 65  L_LENGTH must be
32ba9 20 61 74 20 6c 65 61 73 74 20 31 30 30 0a 23 65   at least 100.#e
32baa 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f  ndif.#if SQLITE_
32bab 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 3e 53  MAX_SQL_LENGTH>S
32bac 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48  QLITE_MAX_LENGTH
32bad 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f  .# error SQLITE_
32bae 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 20 6d  MAX_SQL_LENGTH m
32baf 75 73 74 20 6e 6f 74 20 62 65 20 67 72 65 61 74  ust not be great
32bb0 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 4d  er than SQLITE_M
32bb1 41 58 5f 4c 45 4e 47 54 48 0a 23 65 6e 64 69 66  AX_LENGTH.#endif
32bb2 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  .#if SQLITE_MAX_
32bb3 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 3c  COMPOUND_SELECT<
32bb4 32 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45  2.# error SQLITE
32bb5 5f 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45  _MAX_COMPOUND_SE
32bb6 4c 45 43 54 20 6d 75 73 74 20 62 65 20 61 74 20  LECT must be at 
32bb7 6c 65 61 73 74 20 32 0a 23 65 6e 64 69 66 0a 23  least 2.#endif.#
32bb8 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 44  if SQLITE_MAX_VD
32bb9 42 45 5f 4f 50 3c 34 30 0a 23 20 65 72 72 6f 72  BE_OP<40.# error
32bba 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 44 42 45   SQLITE_MAX_VDBE
32bbb 5f 4f 50 20 6d 75 73 74 20 62 65 20 61 74 20 6c  _OP must be at l
32bbc 65 61 73 74 20 34 30 0a 23 65 6e 64 69 66 0a 23  east 40.#endif.#
32bbd 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55  if SQLITE_MAX_FU
32bbe 4e 43 54 49 4f 4e 5f 41 52 47 3c 30 20 7c 7c 20  NCTION_ARG<0 || 
32bbf 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54  SQLITE_MAX_FUNCT
32bc0 49 4f 4e 5f 41 52 47 3e 31 30 30 30 0a 23 20 65  ION_ARG>1000.# e
32bc1 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f  rror SQLITE_MAX_
32bc2 46 55 4e 43 54 49 4f 4e 5f 41 52 47 20 6d 75 73  FUNCTION_ARG mus
32bc3 74 20 62 65 20 62 65 74 77 65 65 6e 20 30 20 61  t be between 0 a
32bc4 6e 64 20 31 30 30 30 0a 23 65 6e 64 69 66 0a 23  nd 1000.#endif.#
32bc5 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54  if SQLITE_MAX_AT
32bc6 54 41 43 48 45 44 3c 30 20 7c 7c 20 53 51 4c 49  TACHED<0 || SQLI
32bc7 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 3e  TE_MAX_ATTACHED>
32bc8 33 30 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54  30.# error SQLIT
32bc9 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 20 6d  E_MAX_ATTACHED m
32bca 75 73 74 20 62 65 20 62 65 74 77 65 65 6e 20 30  ust be between 0
32bcb 20 61 6e 64 20 33 30 0a 23 65 6e 64 69 66 0a 23   and 30.#endif.#
32bcc 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49  if SQLITE_MAX_LI
32bcd 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54  KE_PATTERN_LENGT
32bce 48 3c 31 0a 23 20 65 72 72 6f 72 20 53 51 4c 49  H<1.# error SQLI
32bcf 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54  TE_MAX_LIKE_PATT
32bd0 45 52 4e 5f 4c 45 4e 47 54 48 20 6d 75 73 74 20  ERN_LENGTH must 
32bd1 62 65 20 61 74 20 6c 65 61 73 74 20 31 0a 23 65  be at least 1.#e
32bd2 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f  ndif.#if SQLITE_
32bd3 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d  MAX_VARIABLE_NUM
32bd4 42 45 52 3c 31 0a 23 20 65 72 72 6f 72 20 53 51  BER<1.# error SQ
32bd5 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49 41 42 4c  LITE_MAX_VARIABL
32bd6 45 5f 4e 55 4d 42 45 52 20 6d 75 73 74 20 62 65  E_NUMBER must be
32bd7 20 61 74 20 6c 65 61 73 74 20 31 0a 23 65 6e 64   at least 1.#end
32bd8 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41  if.#if SQLITE_MA
32bd9 58 5f 43 4f 4c 55 4d 4e 3e 33 32 37 36 37 0a 23  X_COLUMN>32767.#
32bda 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41   error SQLITE_MA
32bdb 58 5f 43 4f 4c 55 4d 4e 20 6d 75 73 74 20 6e 6f  X_COLUMN must no
32bdc 74 20 65 78 63 65 65 64 20 33 32 37 36 37 0a 23  t exceed 32767.#
32bdd 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45  endif.#if SQLITE
32bde 5f 4d 41 58 5f 54 52 49 47 47 45 52 5f 44 45 50  _MAX_TRIGGER_DEP
32bdf 54 48 3c 31 0a 23 20 65 72 72 6f 72 20 53 51 4c  TH<1.# error SQL
32be0 49 54 45 5f 4d 41 58 5f 54 52 49 47 47 45 52 5f  ITE_MAX_TRIGGER_
32be1 44 45 50 54 48 20 6d 75 73 74 20 62 65 20 61 74  DEPTH must be at
32be2 20 6c 65 61 73 74 20 31 0a 23 65 6e 64 69 66 0a   least 1.#endif.
32be3 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74  ../*.** Change t
32be4 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20 6c 69  he value of a li
32be5 6d 69 74 2e 20 20 52 65 70 6f 72 74 20 74 68 65  mit.  Report the
32be6 20 6f 6c 64 20 76 61 6c 75 65 2e 0a 2a 2a 20 49   old value..** I
32be7 66 20 61 6e 20 69 6e 76 61 6c 69 64 20 6c 69 6d  f an invalid lim
32be8 69 74 20 69 6e 64 65 78 20 69 73 20 73 75 70 70  it index is supp
32be9 6c 69 65 64 2c 20 72 65 70 6f 72 74 20 2d 31 2e  lied, report -1.
32bea 0a 2a 2a 20 4d 61 6b 65 20 6e 6f 20 63 68 61 6e  .** Make no chan
32beb 67 65 73 20 62 75 74 20 73 74 69 6c 6c 20 72 65  ges but still re
32bec 70 6f 72 74 20 74 68 65 20 6f 6c 64 20 76 61 6c  port the old val
32bed 75 65 20 69 66 20 74 68 65 0a 2a 2a 20 6e 65 77  ue if the.** new
32bee 20 6c 69 6d 69 74 20 69 73 20 6e 65 67 61 74 69   limit is negati
32bef 76 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 77 20  ve..**.** A new 
32bf0 6c 6f 77 65 72 20 6c 69 6d 69 74 20 64 6f 65 73  lower limit does
32bf1 20 6e 6f 74 20 73 68 72 69 6e 6b 20 65 78 69 73   not shrink exis
32bf2 74 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 73 2e  ting constructs.
32bf3 0a 2a 2a 20 49 74 20 6d 65 72 65 6c 79 20 70 72  .** It merely pr
32bf4 65 76 65 6e 74 73 20 6e 65 77 20 63 6f 6e 73 74  events new const
32bf5 72 75 63 74 73 20 74 68 61 74 20 65 78 63 65 65  ructs that excee
32bf6 64 20 74 68 65 20 6c 69 6d 69 74 0a 2a 2a 20 66  d the limit.** f
32bf7 72 6f 6d 20 66 6f 72 6d 69 6e 67 2e 0a 2a 2f 0a  rom forming..*/.
32bf8 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
32bf9 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 73 71 6c  qlite3_limit(sql
32bfa 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6c 69  ite3 *db, int li
32bfb 6d 69 74 49 64 2c 20 69 6e 74 20 6e 65 77 4c 69  mitId, int newLi
32bfc 6d 69 74 29 7b 0a 20 20 69 6e 74 20 6f 6c 64 4c  mit){.  int oldL
32bfd 69 6d 69 74 3b 0a 20 20 69 66 28 20 6c 69 6d 69  imit;.  if( limi
32bfe 74 49 64 3c 30 20 7c 7c 20 6c 69 6d 69 74 49 64  tId<0 || limitId
32bff 3e 3d 53 51 4c 49 54 45 5f 4e 5f 4c 49 4d 49 54  >=SQLITE_N_LIMIT
32c00 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 2d   ){.    return -
32c01 31 3b 0a 20 20 7d 0a 20 20 6f 6c 64 4c 69 6d 69  1;.  }.  oldLimi
32c02 74 20 3d 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 6c  t = db->aLimit[l
32c03 69 6d 69 74 49 64 5d 3b 0a 20 20 69 66 28 20 6e  imitId];.  if( n
32c04 65 77 4c 69 6d 69 74 3e 3d 30 20 29 7b 0a 20 20  ewLimit>=0 ){.  
32c05 20 20 69 66 28 20 6e 65 77 4c 69 6d 69 74 3e 61    if( newLimit>a
32c06 48 61 72 64 4c 69 6d 69 74 5b 6c 69 6d 69 74 49  HardLimit[limitI
32c07 64 5d 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c  d] ){.      newL
32c08 69 6d 69 74 20 3d 20 61 48 61 72 64 4c 69 6d 69  imit = aHardLimi
32c09 74 5b 6c 69 6d 69 74 49 64 5d 3b 0a 20 20 20 20  t[limitId];.    
32c0a 7d 0a 20 20 20 20 64 62 2d 3e 61 4c 69 6d 69 74  }.    db->aLimit
32c0b 5b 6c 69 6d 69 74 49 64 5d 20 3d 20 6e 65 77 4c  [limitId] = newL
32c0c 69 6d 69 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75  imit;.  }.  retu
32c0d 72 6e 20 6f 6c 64 4c 69 6d 69 74 3b 0a 7d 0a 0a  rn oldLimit;.}..
32c0e 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
32c0f 6e 65 20 64 6f 65 73 20 74 68 65 20 77 6f 72 6b  ne does the work
32c10 20 6f 66 20 6f 70 65 6e 69 6e 67 20 61 20 64 61   of opening a da
32c11 74 61 62 61 73 65 20 6f 6e 20 62 65 68 61 6c 66  tabase on behalf
32c12 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f   of.** sqlite3_o
32c13 70 65 6e 28 29 20 61 6e 64 20 73 71 6c 69 74 65  pen() and sqlite
32c14 33 5f 6f 70 65 6e 31 36 28 29 2e 20 54 68 65 20  3_open16(). The 
32c15 64 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d  database filenam
32c16 65 20 22 7a 46 69 6c 65 6e 61 6d 65 22 20 20 0a  e "zFilename"  .
32c17 2a 2a 20 69 73 20 55 54 46 2d 38 20 65 6e 63 6f  ** is UTF-8 enco
32c18 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ded..*/.static i
32c19 6e 74 20 6f 70 65 6e 44 61 74 61 62 61 73 65 28  nt openDatabase(
32c1a 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
32c1b 46 69 6c 65 6e 61 6d 65 2c 20 2f 2a 20 44 61 74  Filename, /* Dat
32c1c 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 55  abase filename U
32c1d 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a  TF-8 encoded */.
32c1e 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62    sqlite3 **ppDb
32c1f 2c 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a  ,        /* OUT:
32c20 20 52 65 74 75 72 6e 65 64 20 64 61 74 61 62 61   Returned databa
32c21 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 75  se handle */.  u
32c22 6e 73 69 67 6e 65 64 20 66 6c 61 67 73 2c 20 20  nsigned flags,  
32c23 20 20 20 20 20 20 2f 2a 20 4f 70 65 72 61 74 69        /* Operati
32c24 6f 6e 61 6c 20 66 6c 61 67 73 20 2a 2f 0a 20 20  onal flags */.  
32c25 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 66 73  const char *zVfs
32c26 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
32c27 66 20 74 68 65 20 56 46 53 20 74 6f 20 75 73 65  f the VFS to use
32c28 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   */.){.  sqlite3
32c29 20 2a 64 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a   *db;.  int rc;.
32c2a 20 20 69 6e 74 20 69 73 54 68 72 65 61 64 73 61    int isThreadsa
32c2b 66 65 3b 0a 0a 20 20 2a 70 70 44 62 20 3d 20 30  fe;..  *ppDb = 0
32c2c 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
32c2d 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20  _OMIT_AUTOINIT. 
32c2e 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 69 6e   rc = sqlite3_in
32c2f 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20 69 66  itialize();.  if
32c30 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
32c31 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20  ;.#endif..  if( 
32c32 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
32c33 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 3d 3d  fig.bCoreMutex==
32c34 30 20 29 7b 0a 20 20 20 20 69 73 54 68 72 65 61  0 ){.    isThrea
32c35 64 73 61 66 65 20 3d 20 30 3b 0a 20 20 7d 65 6c  dsafe = 0;.  }el
32c36 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 53  se if( flags & S
32c37 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54  QLITE_OPEN_NOMUT
32c38 45 58 20 29 7b 0a 20 20 20 20 69 73 54 68 72 65  EX ){.    isThre
32c39 61 64 73 61 66 65 20 3d 20 30 3b 0a 20 20 7d 65  adsafe = 0;.  }e
32c3a 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20  lse if( flags & 
32c3b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c  SQLITE_OPEN_FULL
32c3c 4d 55 54 45 58 20 29 7b 0a 20 20 20 20 69 73 54  MUTEX ){.    isT
32c3d 68 72 65 61 64 73 61 66 65 20 3d 20 31 3b 0a 20  hreadsafe = 1;. 
32c3e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 73 54 68   }else{.    isTh
32c3f 72 65 61 64 73 61 66 65 20 3d 20 73 71 6c 69 74  readsafe = sqlit
32c40 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62  e3GlobalConfig.b
32c41 46 75 6c 6c 4d 75 74 65 78 3b 0a 20 20 7d 0a 20  FullMutex;.  }. 
32c42 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c   if( flags & SQL
32c43 49 54 45 5f 4f 50 45 4e 5f 50 52 49 56 41 54 45  ITE_OPEN_PRIVATE
32c44 43 41 43 48 45 20 29 7b 0a 20 20 20 20 66 6c 61  CACHE ){.    fla
32c45 67 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f 4f 50  gs &= ~SQLITE_OP
32c46 45 4e 5f 53 48 41 52 45 44 43 41 43 48 45 3b 0a  EN_SHAREDCACHE;.
32c47 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69    }else if( sqli
32c48 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
32c49 73 68 61 72 65 64 43 61 63 68 65 45 6e 61 62 6c  sharedCacheEnabl
32c4a 65 64 20 29 7b 0a 20 20 20 20 66 6c 61 67 73 20  ed ){.    flags 
32c4b 7c 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53  |= SQLITE_OPEN_S
32c4c 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20 7d 0a  HAREDCACHE;.  }.
32c4d 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20 68 61 72  .  /* Remove har
32c4e 6d 66 75 6c 20 62 69 74 73 20 66 72 6f 6d 20 74  mful bits from t
32c4f 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74  he flags paramet
32c50 65 72 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  er.  **.  ** The
32c51 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d   SQLITE_OPEN_NOM
32c52 55 54 45 58 20 61 6e 64 20 53 51 4c 49 54 45 5f  UTEX and SQLITE_
32c53 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 20 66  OPEN_FULLMUTEX f
32c54 6c 61 67 73 20 77 65 72 65 0a 20 20 2a 2a 20 64  lags were.  ** d
32c55 65 61 6c 74 20 77 69 74 68 20 69 6e 20 74 68 65  ealt with in the
32c56 20 70 72 65 76 69 6f 75 73 20 63 6f 64 65 20 62   previous code b
32c57 6c 6f 63 6b 2e 20 20 42 65 73 69 64 65 73 20 74  lock.  Besides t
32c58 68 65 73 65 2c 20 74 68 65 20 6f 6e 6c 79 0a 20  hese, the only. 
32c59 20 2a 2a 20 76 61 6c 69 64 20 69 6e 70 75 74 20   ** valid input 
32c5a 66 6c 61 67 73 20 66 6f 72 20 73 71 6c 69 74 65  flags for sqlite
32c5b 33 5f 6f 70 65 6e 5f 76 32 28 29 20 61 72 65 20  3_open_v2() are 
32c5c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
32c5d 4f 4e 4c 59 2c 0a 20 20 2a 2a 20 53 51 4c 49 54  ONLY,.  ** SQLIT
32c5e 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
32c5f 2c 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 50 45  , and SQLITE_OPE
32c60 4e 5f 43 52 45 41 54 45 2e 20 20 53 69 6c 65 6e  N_CREATE.  Silen
32c61 74 6c 79 20 6d 61 73 6b 0a 20 20 2a 2a 20 6f 66  tly mask.  ** of
32c62 66 20 61 6c 6c 20 6f 74 68 65 72 20 66 6c 61 67  f all other flag
32c63 73 2e 0a 20 20 2a 2f 0a 20 20 66 6c 61 67 73 20  s..  */.  flags 
32c64 26 3d 20 20 7e 28 20 53 51 4c 49 54 45 5f 4f 50  &=  ~( SQLITE_OP
32c65 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45  EN_DELETEONCLOSE
32c66 20 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   |.             
32c67 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58    SQLITE_OPEN_EX
32c68 43 4c 55 53 49 56 45 20 7c 0a 20 20 20 20 20 20  CLUSIVE |.      
32c69 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
32c6a 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 7c 0a 20  OPEN_MAIN_DB |. 
32c6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51                SQ
32c6c 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44  LITE_OPEN_TEMP_D
32c6d 42 20 7c 20 0a 20 20 20 20 20 20 20 20 20 20 20  B | .           
32c6e 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f      SQLITE_OPEN_
32c6f 54 52 41 4e 53 49 45 4e 54 5f 44 42 20 7c 20 0a  TRANSIENT_DB | .
32c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
32c71 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
32c72 4a 4f 55 52 4e 41 4c 20 7c 20 0a 20 20 20 20 20  JOURNAL | .     
32c73 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
32c74 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e  _OPEN_TEMP_JOURN
32c75 41 4c 20 7c 20 0a 20 20 20 20 20 20 20 20 20 20  AL | .          
32c76 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
32c77 5f 53 55 42 4a 4f 55 52 4e 41 4c 20 7c 20 0a 20  _SUBJOURNAL | . 
32c78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51                SQ
32c79 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52  LITE_OPEN_MASTER
32c7a 5f 4a 4f 55 52 4e 41 4c 20 7c 0a 20 20 20 20 20  _JOURNAL |.     
32c7b 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
32c7c 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58 20 7c 0a  _OPEN_NOMUTEX |.
32c7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
32c7e 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d  QLITE_OPEN_FULLM
32c7f 55 54 45 58 0a 20 20 20 20 20 20 20 20 20 20 20  UTEX.           
32c80 20 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63    );..  /* Alloc
32c81 61 74 65 20 74 68 65 20 73 71 6c 69 74 65 20 64  ate the sqlite d
32c82 61 74 61 20 73 74 72 75 63 74 75 72 65 20 2a 2f  ata structure */
32c83 0a 20 20 64 62 20 3d 20 73 71 6c 69 74 65 33 4d  .  db = sqlite3M
32c84 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f  allocZero( sizeo
32c85 66 28 73 71 6c 69 74 65 33 29 20 29 3b 0a 20 20  f(sqlite3) );.  
32c86 69 66 28 20 64 62 3d 3d 30 20 29 20 67 6f 74 6f  if( db==0 ) goto
32c87 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a 20 20 69   opendb_out;.  i
32c88 66 28 20 69 73 54 68 72 65 61 64 73 61 66 65 20  f( isThreadsafe 
32c89 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 75 74 65 78  ){.    db->mutex
32c8a 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41   = sqlite3MutexA
32c8b 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
32c8c 58 5f 52 45 43 55 52 53 49 56 45 29 3b 0a 20 20  X_RECURSIVE);.  
32c8d 20 20 69 66 28 20 64 62 2d 3e 6d 75 74 65 78 3d    if( db->mutex=
32c8e 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
32c8f 74 65 33 5f 66 72 65 65 28 64 62 29 3b 0a 20 20  te3_free(db);.  
32c90 20 20 20 20 64 62 20 3d 20 30 3b 0a 20 20 20 20      db = 0;.    
32c91 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75    goto opendb_ou
32c92 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73  t;.    }.  }.  s
32c93 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
32c94 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  er(db->mutex);. 
32c95 20 64 62 2d 3e 65 72 72 4d 61 73 6b 20 3d 20 30   db->errMask = 0
32c96 78 66 66 3b 0a 20 20 64 62 2d 3e 6e 44 62 20 3d  xff;.  db->nDb =
32c97 20 32 3b 0a 20 20 64 62 2d 3e 6d 61 67 69 63 20   2;.  db->magic 
32c98 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42  = SQLITE_MAGIC_B
32c99 55 53 59 3b 0a 20 20 64 62 2d 3e 61 44 62 20 3d  USY;.  db->aDb =
32c9a 20 64 62 2d 3e 61 44 62 53 74 61 74 69 63 3b 0a   db->aDbStatic;.
32c9b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f  .  assert( sizeo
32c9c 66 28 64 62 2d 3e 61 4c 69 6d 69 74 29 3d 3d 73  f(db->aLimit)==s
32c9d 69 7a 65 6f 66 28 61 48 61 72 64 4c 69 6d 69 74  izeof(aHardLimit
32c9e 29 20 29 3b 0a 20 20 6d 65 6d 63 70 79 28 64 62  ) );.  memcpy(db
32c9f 2d 3e 61 4c 69 6d 69 74 2c 20 61 48 61 72 64 4c  ->aLimit, aHardL
32ca0 69 6d 69 74 2c 20 73 69 7a 65 6f 66 28 64 62 2d  imit, sizeof(db-
32ca1 3e 61 4c 69 6d 69 74 29 29 3b 0a 20 20 64 62 2d  >aLimit));.  db-
32ca2 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b  >autoCommit = 1;
32ca3 0a 20 20 64 62 2d 3e 6e 65 78 74 41 75 74 6f 76  .  db->nextAutov
32ca4 61 63 20 3d 20 2d 31 3b 0a 20 20 64 62 2d 3e 6e  ac = -1;.  db->n
32ca5 65 78 74 50 61 67 65 73 69 7a 65 20 3d 20 30 3b  extPagesize = 0;
32ca6 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20  .  db->flags |= 
32ca7 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c 4e  SQLITE_ShortColN
32ca8 61 6d 65 73 0a 23 69 66 20 53 51 4c 49 54 45 5f  ames.#if SQLITE_
32ca9 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 46 4f 52  DEFAULT_FILE_FOR
32caa 4d 41 54 3c 34 0a 20 20 20 20 20 20 20 20 20 20  MAT<4.          
32cab 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f         | SQLITE_
32cac 4c 65 67 61 63 79 46 69 6c 65 46 6d 74 0a 23 65  LegacyFileFmt.#e
32cad 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49  ndif.#ifdef SQLI
32cae 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 41 44 5f 45  TE_ENABLE_LOAD_E
32caf 58 54 45 4e 53 49 4f 4e 0a 20 20 20 20 20 20 20  XTENSION.       
32cb0 20 20 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49            | SQLI
32cb1 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e  TE_LoadExtension
32cb2 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49  .#endif.#if SQLI
32cb3 54 45 5f 44 45 46 41 55 4c 54 5f 52 45 43 55 52  TE_DEFAULT_RECUR
32cb4 53 49 56 45 5f 54 52 49 47 47 45 52 53 0a 20 20  SIVE_TRIGGERS.  
32cb5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c                 |
32cb6 20 53 51 4c 49 54 45 5f 52 65 63 54 72 69 67 67   SQLITE_RecTrigg
32cb7 65 72 73 0a 23 65 6e 64 69 66 0a 20 20 20 20 20  ers.#endif.     
32cb8 20 3b 0a 20 20 73 71 6c 69 74 65 33 48 61 73 68   ;.  sqlite3Hash
32cb9 49 6e 69 74 28 26 64 62 2d 3e 61 43 6f 6c 6c 53  Init(&db->aCollS
32cba 65 71 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  eq);.#ifndef SQL
32cbb 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
32cbc 54 41 42 4c 45 0a 20 20 73 71 6c 69 74 65 33 48  TABLE.  sqlite3H
32cbd 61 73 68 49 6e 69 74 28 26 64 62 2d 3e 61 4d 6f  ashInit(&db->aMo
32cbe 64 75 6c 65 29 3b 0a 23 65 6e 64 69 66 0a 0a 20  dule);.#endif.. 
32cbf 20 64 62 2d 3e 70 56 66 73 20 3d 20 73 71 6c 69   db->pVfs = sqli
32cc0 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 7a 56 66  te3_vfs_find(zVf
32cc1 73 29 3b 0a 20 20 69 66 28 20 21 64 62 2d 3e 70  s);.  if( !db->p
32cc2 56 66 73 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  Vfs ){.    rc = 
32cc3 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
32cc4 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
32cc5 62 2c 20 72 63 2c 20 22 6e 6f 20 73 75 63 68 20  b, rc, "no such 
32cc6 76 66 73 3a 20 25 73 22 2c 20 7a 56 66 73 29 3b  vfs: %s", zVfs);
32cc7 0a 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62  .    goto opendb
32cc8 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  _out;.  }..  /* 
32cc9 41 64 64 20 74 68 65 20 64 65 66 61 75 6c 74 20  Add the default 
32cca 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
32ccb 63 65 20 42 49 4e 41 52 59 2e 20 42 49 4e 41 52  ce BINARY. BINAR
32ccc 59 20 77 6f 72 6b 73 20 66 6f 72 20 62 6f 74 68  Y works for both
32ccd 20 55 54 46 2d 38 0a 20 20 2a 2a 20 61 6e 64 20   UTF-8.  ** and 
32cce 55 54 46 2d 31 36 2c 20 73 6f 20 61 64 64 20 61  UTF-16, so add a
32ccf 20 76 65 72 73 69 6f 6e 20 66 6f 72 20 65 61 63   version for eac
32cd0 68 20 74 6f 20 61 76 6f 69 64 20 61 6e 79 20 75  h to avoid any u
32cd1 6e 6e 65 63 65 73 73 61 72 79 0a 20 20 2a 2a 20  nnecessary.  ** 
32cd2 63 6f 6e 76 65 72 73 69 6f 6e 73 2e 20 54 68 65  conversions. The
32cd3 20 6f 6e 6c 79 20 65 72 72 6f 72 20 74 68 61 74   only error that
32cd4 20 63 61 6e 20 6f 63 63 75 72 20 68 65 72 65 20   can occur here 
32cd5 69 73 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61  is a malloc() fa
32cd6 69 6c 75 72 65 2e 0a 20 20 2a 2f 0a 20 20 63 72  ilure..  */.  cr
32cd7 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62  eateCollation(db
32cd8 2c 20 22 42 49 4e 41 52 59 22 2c 20 53 51 4c 49  , "BINARY", SQLI
32cd9 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f  TE_UTF8, SQLITE_
32cda 43 4f 4c 4c 5f 42 49 4e 41 52 59 2c 20 30 2c 0a  COLL_BINARY, 0,.
32cdb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32cdc 20 20 62 69 6e 43 6f 6c 6c 46 75 6e 63 2c 20 30    binCollFunc, 0
32cdd 29 3b 0a 20 20 63 72 65 61 74 65 43 6f 6c 6c 61  );.  createColla
32cde 74 69 6f 6e 28 64 62 2c 20 22 42 49 4e 41 52 59  tion(db, "BINARY
32cdf 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42  ", SQLITE_UTF16B
32ce0 45 2c 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 42  E, SQLITE_COLL_B
32ce1 49 4e 41 52 59 2c 20 30 2c 0a 20 20 20 20 20 20  INARY, 0,.      
32ce2 20 20 20 20 20 20 20 20 20 20 20 20 62 69 6e 43              binC
32ce3 6f 6c 6c 46 75 6e 63 2c 20 30 29 3b 0a 20 20 63  ollFunc, 0);.  c
32ce4 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64  reateCollation(d
32ce5 62 2c 20 22 42 49 4e 41 52 59 22 2c 20 53 51 4c  b, "BINARY", SQL
32ce6 49 54 45 5f 55 54 46 31 36 4c 45 2c 20 53 51 4c  ITE_UTF16LE, SQL
32ce7 49 54 45 5f 43 4f 4c 4c 5f 42 49 4e 41 52 59 2c  ITE_COLL_BINARY,
32ce8 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20   0,.            
32ce9 20 20 20 20 20 20 62 69 6e 43 6f 6c 6c 46 75 6e        binCollFun
32cea 63 2c 20 30 29 3b 0a 20 20 63 72 65 61 74 65 43  c, 0);.  createC
32ceb 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 22 52 54  ollation(db, "RT
32cec 52 49 4d 22 2c 20 53 51 4c 49 54 45 5f 55 54 46  RIM", SQLITE_UTF
32ced 38 2c 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 55  8, SQLITE_COLL_U
32cee 53 45 52 2c 20 28 76 6f 69 64 2a 29 31 2c 0a 20  SER, (void*)1,. 
32cef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32cf0 20 62 69 6e 43 6f 6c 6c 46 75 6e 63 2c 20 30 29   binCollFunc, 0)
32cf1 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c  ;.  if( db->mall
32cf2 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
32cf3 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b  goto opendb_out;
32cf4 0a 20 20 7d 0a 20 20 64 62 2d 3e 70 44 66 6c 74  .  }.  db->pDflt
32cf5 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69  Coll = sqlite3Fi
32cf6 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 53 51  ndCollSeq(db, SQ
32cf7 4c 49 54 45 5f 55 54 46 38 2c 20 22 42 49 4e 41  LITE_UTF8, "BINA
32cf8 52 59 22 2c 20 30 29 3b 0a 20 20 61 73 73 65 72  RY", 0);.  asser
32cf9 74 28 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c  t( db->pDfltColl
32cfa 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c 73  !=0 );..  /* Als
32cfb 6f 20 61 64 64 20 61 20 55 54 46 2d 38 20 63 61  o add a UTF-8 ca
32cfc 73 65 2d 69 6e 73 65 6e 73 69 74 69 76 65 20 63  se-insensitive c
32cfd 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
32cfe 65 2e 20 2a 2f 0a 20 20 63 72 65 61 74 65 43 6f  e. */.  createCo
32cff 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 22 4e 4f 43  llation(db, "NOC
32d00 41 53 45 22 2c 20 53 51 4c 49 54 45 5f 55 54 46  ASE", SQLITE_UTF
32d01 38 2c 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 4e  8, SQLITE_COLL_N
32d02 4f 43 41 53 45 2c 20 30 2c 0a 20 20 20 20 20 20  OCASE, 0,.      
32d03 20 20 20 20 20 20 20 20 20 20 20 20 6e 6f 63 61              noca
32d04 73 65 43 6f 6c 6c 61 74 69 6e 67 46 75 6e 63 2c  seCollatingFunc,
32d05 20 30 29 3b 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20   0);..  /* Open 
32d06 74 68 65 20 62 61 63 6b 65 6e 64 20 64 61 74 61  the backend data
32d07 62 61 73 65 20 64 72 69 76 65 72 20 2a 2f 0a 20  base driver */. 
32d08 20 64 62 2d 3e 6f 70 65 6e 46 6c 61 67 73 20 3d   db->openFlags =
32d09 20 66 6c 61 67 73 3b 0a 20 20 72 63 20 3d 20 73   flags;.  rc = s
32d0a 71 6c 69 74 65 33 42 74 72 65 65 46 61 63 74 6f  qlite3BtreeFacto
32d0b 72 79 28 64 62 2c 20 7a 46 69 6c 65 6e 61 6d 65  ry(db, zFilename
32d0c 2c 20 30 2c 20 53 51 4c 49 54 45 5f 44 45 46 41  , 0, SQLITE_DEFA
32d0d 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 2c 20  ULT_CACHE_SIZE, 
32d0e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
32d0f 20 20 20 20 20 20 20 20 20 20 20 20 66 6c 61 67              flag
32d10 73 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  s | SQLITE_OPEN_
32d11 4d 41 49 4e 5f 44 42 2c 0a 20 20 20 20 20 20 20  MAIN_DB,.       
32d12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32d13 20 20 20 20 26 64 62 2d 3e 61 44 62 5b 30 5d 2e      &db->aDb[0].
32d14 70 42 74 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  pBt);.  if( rc!=
32d15 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
32d16 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
32d17 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20  IOERR_NOMEM ){. 
32d18 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
32d19 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20  _NOMEM;.    }.  
32d1a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
32d1b 62 2c 20 72 63 2c 20 30 29 3b 0a 20 20 20 20 67  b, rc, 0);.    g
32d1c 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a  oto opendb_out;.
32d1d 20 20 7d 0a 20 20 64 62 2d 3e 61 44 62 5b 30 5d    }.  db->aDb[0]
32d1e 2e 70 53 63 68 65 6d 61 20 3d 20 73 71 6c 69 74  .pSchema = sqlit
32d1f 65 33 53 63 68 65 6d 61 47 65 74 28 64 62 2c 20  e3SchemaGet(db, 
32d20 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 29 3b  db->aDb[0].pBt);
32d21 0a 20 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53  .  db->aDb[1].pS
32d22 63 68 65 6d 61 20 3d 20 73 71 6c 69 74 65 33 53  chema = sqlite3S
32d23 63 68 65 6d 61 47 65 74 28 64 62 2c 20 30 29 3b  chemaGet(db, 0);
32d24 0a 0a 0a 20 20 2f 2a 20 54 68 65 20 64 65 66 61  ...  /* The defa
32d25 75 6c 74 20 73 61 66 65 74 79 5f 6c 65 76 65 6c  ult safety_level
32d26 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61   for the main da
32d27 74 61 62 61 73 65 20 69 73 20 27 66 75 6c 6c 27  tabase is 'full'
32d28 3b 20 66 6f 72 20 74 68 65 20 74 65 6d 70 0a 20  ; for the temp. 
32d29 20 2a 2a 20 64 61 74 61 62 61 73 65 20 69 74 20   ** database it 
32d2a 69 73 20 27 4e 4f 4e 45 27 2e 20 54 68 69 73 20  is 'NONE'. This 
32d2b 6d 61 74 63 68 65 73 20 74 68 65 20 70 61 67 65  matches the page
32d2c 72 20 6c 61 79 65 72 20 64 65 66 61 75 6c 74 73  r layer defaults
32d2d 2e 20 20 0a 20 20 2a 2f 0a 20 20 64 62 2d 3e 61  .  .  */.  db->a
32d2e 44 62 5b 30 5d 2e 7a 4e 61 6d 65 20 3d 20 22 6d  Db[0].zName = "m
32d2f 61 69 6e 22 3b 0a 20 20 64 62 2d 3e 61 44 62 5b  ain";.  db->aDb[
32d30 30 5d 2e 73 61 66 65 74 79 5f 6c 65 76 65 6c 20  0].safety_level 
32d31 3d 20 33 3b 0a 20 20 64 62 2d 3e 61 44 62 5b 31  = 3;.  db->aDb[1
32d32 5d 2e 7a 4e 61 6d 65 20 3d 20 22 74 65 6d 70 22  ].zName = "temp"
32d33 3b 0a 20 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 73  ;.  db->aDb[1].s
32d34 61 66 65 74 79 5f 6c 65 76 65 6c 20 3d 20 31 3b  afety_level = 1;
32d35 0a 0a 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20  ..  db->magic = 
32d36 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45  SQLITE_MAGIC_OPE
32d37 4e 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  N;.  if( db->mal
32d38 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
32d39 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75 74   goto opendb_out
32d3a 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 67 69  ;.  }..  /* Regi
32d3b 73 74 65 72 20 61 6c 6c 20 62 75 69 6c 74 2d 69  ster all built-i
32d3c 6e 20 66 75 6e 63 74 69 6f 6e 73 2c 20 62 75 74  n functions, but
32d3d 20 64 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74 20   do not attempt 
32d3e 74 6f 20 72 65 61 64 20 74 68 65 0a 20 20 2a 2a  to read the.  **
32d3f 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
32d40 20 79 65 74 2e 20 54 68 69 73 20 69 73 20 64 65   yet. This is de
32d41 6c 61 79 65 64 20 75 6e 74 69 6c 20 74 68 65 20  layed until the 
32d42 66 69 72 73 74 20 74 69 6d 65 20 74 68 65 20 64  first time the d
32d43 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 69 73 20  atabase.  ** is 
32d44 61 63 63 65 73 73 65 64 2e 0a 20 20 2a 2f 0a 20  accessed..  */. 
32d45 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62   sqlite3Error(db
32d46 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b  , SQLITE_OK, 0);
32d47 0a 20 20 73 71 6c 69 74 65 33 52 65 67 69 73 74  .  sqlite3Regist
32d48 65 72 42 75 69 6c 74 69 6e 46 75 6e 63 74 69 6f  erBuiltinFunctio
32d49 6e 73 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 4c 6f  ns(db);..  /* Lo
32d4a 61 64 20 61 75 74 6f 6d 61 74 69 63 20 65 78 74  ad automatic ext
32d4b 65 6e 73 69 6f 6e 73 20 2d 20 65 78 74 65 6e 73  ensions - extens
32d4c 69 6f 6e 73 20 74 68 61 74 20 68 61 76 65 20 62  ions that have b
32d4d 65 65 6e 20 72 65 67 69 73 74 65 72 65 64 0a 20  een registered. 
32d4e 20 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 73 71   ** using the sq
32d4f 6c 69 74 65 33 5f 61 75 74 6f 6d 61 74 69 63 5f  lite3_automatic_
32d50 65 78 74 65 6e 73 69 6f 6e 28 29 20 41 50 49 2e  extension() API.
32d51 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 41  .  */.  sqlite3A
32d52 75 74 6f 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e  utoLoadExtension
32d53 73 28 64 62 29 3b 0a 20 20 72 63 20 3d 20 73 71  s(db);.  rc = sq
32d54 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 64 62  lite3_errcode(db
32d55 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
32d56 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f  ITE_OK ){.    go
32d57 74 6f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a 20  to opendb_out;. 
32d58 20 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54   }..#ifdef SQLIT
32d59 45 5f 45 4e 41 42 4c 45 5f 46 54 53 31 0a 20 20  E_ENABLE_FTS1.  
32d5a 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46  if( !db->mallocF
32d5b 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 65 78 74  ailed ){.    ext
32d5c 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ern int sqlite3F
32d5d 74 73 31 49 6e 69 74 28 73 71 6c 69 74 65 33 2a  ts1Init(sqlite3*
32d5e 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  );.    rc = sqli
32d5f 74 65 33 46 74 73 31 49 6e 69 74 28 64 62 29 3b  te3Fts1Init(db);
32d60 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66  .  }.#endif..#if
32d61 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
32d62 45 5f 46 54 53 32 0a 20 20 69 66 28 20 21 64 62  E_FTS2.  if( !db
32d63 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 26  ->mallocFailed &
32d64 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  & rc==SQLITE_OK 
32d65 29 7b 0a 20 20 20 20 65 78 74 65 72 6e 20 69 6e  ){.    extern in
32d66 74 20 73 71 6c 69 74 65 33 46 74 73 32 49 6e 69  t sqlite3Fts2Ini
32d67 74 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 20  t(sqlite3*);.   
32d68 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73   rc = sqlite3Fts
32d69 32 49 6e 69 74 28 64 62 29 3b 0a 20 20 7d 0a 23  2Init(db);.  }.#
32d6a 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
32d6b 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
32d6c 0a 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c  .  if( !db->mall
32d6d 6f 63 46 61 69 6c 65 64 20 26 26 20 72 63 3d 3d  ocFailed && rc==
32d6e 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
32d6f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73   rc = sqlite3Fts
32d70 33 49 6e 69 74 28 64 62 29 3b 0a 20 20 7d 0a 23  3Init(db);.  }.#
32d71 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
32d72 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 43 55 0a  LITE_ENABLE_ICU.
32d73 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f    if( !db->mallo
32d74 63 46 61 69 6c 65 64 20 26 26 20 72 63 3d 3d 53  cFailed && rc==S
32d75 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
32d76 72 63 20 3d 20 73 71 6c 69 74 65 33 49 63 75 49  rc = sqlite3IcuI
32d77 6e 69 74 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e  nit(db);.  }.#en
32d78 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
32d79 54 45 5f 45 4e 41 42 4c 45 5f 52 54 52 45 45 0a  TE_ENABLE_RTREE.
32d7a 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f    if( !db->mallo
32d7b 63 46 61 69 6c 65 64 20 26 26 20 72 63 3d 3d 53  cFailed && rc==S
32d7c 51 4c 49 54 45 5f 4f 4b 29 7b 0a 20 20 20 20 72  QLITE_OK){.    r
32d7d 63 20 3d 20 73 71 6c 69 74 65 33 52 74 72 65 65  c = sqlite3Rtree
32d7e 49 6e 69 74 28 64 62 29 3b 0a 20 20 7d 0a 23 65  Init(db);.  }.#e
32d7f 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33 45  ndif..  sqlite3E
32d80 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 30 29 3b  rror(db, rc, 0);
32d81 0a 0a 20 20 2f 2a 20 2d 44 53 51 4c 49 54 45 5f  ..  /* -DSQLITE_
32d82 44 45 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f  DEFAULT_LOCKING_
32d83 4d 4f 44 45 3d 31 20 6d 61 6b 65 73 20 45 58 43  MODE=1 makes EXC
32d84 4c 55 53 49 56 45 20 74 68 65 20 64 65 66 61 75  LUSIVE the defau
32d85 6c 74 20 6c 6f 63 6b 69 6e 67 0a 20 20 2a 2a 20  lt locking.  ** 
32d86 6d 6f 64 65 2e 20 20 2d 44 53 51 4c 49 54 45 5f  mode.  -DSQLITE_
32d87 44 45 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f  DEFAULT_LOCKING_
32d88 4d 4f 44 45 3d 30 20 6d 61 6b 65 20 4e 4f 52 4d  MODE=0 make NORM
32d89 41 4c 20 74 68 65 20 64 65 66 61 75 6c 74 20 6c  AL the default l
32d8a 6f 63 6b 69 6e 67 0a 20 20 2a 2a 20 6d 6f 64 65  ocking.  ** mode
32d8b 2e 20 20 44 6f 69 6e 67 20 6e 6f 74 68 69 6e 67  .  Doing nothing
32d8c 20 61 74 20 61 6c 6c 20 61 6c 73 6f 20 6d 61 6b   at all also mak
32d8d 65 73 20 4e 4f 52 4d 41 4c 20 74 68 65 20 64 65  es NORMAL the de
32d8e 66 61 75 6c 74 2e 0a 20 20 2a 2f 0a 23 69 66 64  fault..  */.#ifd
32d8f 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  ef SQLITE_DEFAUL
32d90 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45 0a 20  T_LOCKING_MODE. 
32d91 20 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b 4d 6f 64   db->dfltLockMod
32d92 65 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 41 55  e = SQLITE_DEFAU
32d93 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45 3b  LT_LOCKING_MODE;
32d94 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c  .  sqlite3PagerL
32d95 6f 63 6b 69 6e 67 4d 6f 64 65 28 73 71 6c 69 74  ockingMode(sqlit
32d96 65 33 42 74 72 65 65 50 61 67 65 72 28 64 62 2d  e3BtreePager(db-
32d97 3e 61 44 62 5b 30 5d 2e 70 42 74 29 2c 0a 20 20  >aDb[0].pBt),.  
32d98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32d99 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44          SQLITE_D
32d9a 45 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d  EFAULT_LOCKING_M
32d9b 4f 44 45 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20  ODE);.#endif..  
32d9c 2f 2a 20 45 6e 61 62 6c 65 20 74 68 65 20 6c 6f  /* Enable the lo
32d9d 6f 6b 61 73 69 64 65 2d 6d 61 6c 6c 6f 63 20 73  okaside-malloc s
32d9e 75 62 73 79 73 74 65 6d 20 2a 2f 0a 20 20 73 65  ubsystem */.  se
32d9f 74 75 70 4c 6f 6f 6b 61 73 69 64 65 28 64 62 2c  tupLookaside(db,
32da0 20 30 2c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61   0, sqlite3Globa
32da1 6c 43 6f 6e 66 69 67 2e 73 7a 4c 6f 6f 6b 61 73  lConfig.szLookas
32da2 69 64 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ide,.           
32da3 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
32da4 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
32da5 2e 6e 4c 6f 6f 6b 61 73 69 64 65 29 3b 0a 0a 6f  .nLookaside);..o
32da6 70 65 6e 64 62 5f 6f 75 74 3a 0a 20 20 69 66 28  pendb_out:.  if(
32da7 20 64 62 20 29 7b 0a 20 20 20 20 61 73 73 65 72   db ){.    asser
32da8 74 28 20 64 62 2d 3e 6d 75 74 65 78 21 3d 30 20  t( db->mutex!=0 
32da9 7c 7c 20 69 73 54 68 72 65 61 64 73 61 66 65 3d  || isThreadsafe=
32daa 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f  =0 || sqlite3Glo
32dab 62 61 6c 43 6f 6e 66 69 67 2e 62 46 75 6c 6c 4d  balConfig.bFullM
32dac 75 74 65 78 3d 3d 30 20 29 3b 0a 20 20 20 20 73  utex==0 );.    s
32dad 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
32dae 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
32daf 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65   }.  rc = sqlite
32db0 33 5f 65 72 72 63 6f 64 65 28 64 62 29 3b 0a 20  3_errcode(db);. 
32db1 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
32db2 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 73 71 6c  NOMEM ){.    sql
32db3 69 74 65 33 5f 63 6c 6f 73 65 28 64 62 29 3b 0a  ite3_close(db);.
32db4 20 20 20 20 64 62 20 3d 20 30 3b 0a 20 20 7d 65      db = 0;.  }e
32db5 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c 49  lse if( rc!=SQLI
32db6 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 64 62 2d  TE_OK ){.    db-
32db7 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f  >magic = SQLITE_
32db8 4d 41 47 49 43 5f 53 49 43 4b 3b 0a 20 20 7d 0a  MAGIC_SICK;.  }.
32db9 20 20 2a 70 70 44 62 20 3d 20 64 62 3b 0a 20 20    *ppDb = db;.  
32dba 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41 70  return sqlite3Ap
32dbb 69 45 78 69 74 28 30 2c 20 72 63 29 3b 0a 7d 0a  iExit(0, rc);.}.
32dbc 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e 65  ./*.** Open a ne
32dbd 77 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  w database handl
32dbe 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  e..*/.SQLITE_API
32dbf 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65   int sqlite3_ope
32dc0 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  n(.  const char 
32dc1 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 0a 20 20 73  *zFilename, .  s
32dc2 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 20 0a 29  qlite3 **ppDb .)
32dc3 7b 0a 20 20 72 65 74 75 72 6e 20 6f 70 65 6e 44  {.  return openD
32dc4 61 74 61 62 61 73 65 28 7a 46 69 6c 65 6e 61 6d  atabase(zFilenam
32dc5 65 2c 20 70 70 44 62 2c 0a 20 20 20 20 20 20 20  e, ppDb,.       
32dc6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
32dc7 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
32dc8 52 49 54 45 20 7c 20 53 51 4c 49 54 45 5f 4f 50  RITE | SQLITE_OP
32dc9 45 4e 5f 43 52 45 41 54 45 2c 20 30 29 3b 0a 7d  EN_CREATE, 0);.}
32dca 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
32dcb 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28  sqlite3_open_v2(
32dcc 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66  .  const char *f
32dcd 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 44 61  ilename,   /* Da
32dce 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20  tabase filename 
32dcf 28 55 54 46 2d 38 29 20 2a 2f 0a 20 20 73 71 6c  (UTF-8) */.  sql
32dd0 69 74 65 33 20 2a 2a 70 70 44 62 2c 20 20 20 20  ite3 **ppDb,    
32dd1 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 51 4c       /* OUT: SQL
32dd2 69 74 65 20 64 62 20 68 61 6e 64 6c 65 20 2a 2f  ite db handle */
32dd3 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20  .  int flags,   
32dd4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c             /* Fl
32dd5 61 67 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ags */.  const c
32dd6 68 61 72 20 2a 7a 56 66 73 20 20 20 20 20 20 20  har *zVfs       
32dd7 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 56 46 53 20   /* Name of VFS 
32dd8 6d 6f 64 75 6c 65 20 74 6f 20 75 73 65 20 2a 2f  module to use */
32dd9 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 6f 70 65  .){.  return ope
32dda 6e 44 61 74 61 62 61 73 65 28 66 69 6c 65 6e 61  nDatabase(filena
32ddb 6d 65 2c 20 70 70 44 62 2c 20 66 6c 61 67 73 2c  me, ppDb, flags,
32ddc 20 7a 56 66 73 29 3b 0a 7d 0a 0a 23 69 66 6e 64   zVfs);.}..#ifnd
32ddd 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  ef SQLITE_OMIT_U
32dde 54 46 31 36 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20  TF16./*.** Open 
32ddf 61 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 68  a new database h
32de0 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  andle..*/.SQLITE
32de1 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
32de2 5f 6f 70 65 6e 31 36 28 0a 20 20 63 6f 6e 73 74  _open16(.  const
32de3 20 76 6f 69 64 20 2a 7a 46 69 6c 65 6e 61 6d 65   void *zFilename
32de4 2c 20 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70  , .  sqlite3 **p
32de5 70 44 62 0a 29 7b 0a 20 20 63 68 61 72 20 63 6f  pDb.){.  char co
32de6 6e 73 74 20 2a 7a 46 69 6c 65 6e 61 6d 65 38 3b  nst *zFilename8;
32de7 20 20 20 2f 2a 20 7a 46 69 6c 65 6e 61 6d 65 20     /* zFilename 
32de8 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38  encoded in UTF-8
32de9 20 69 6e 73 74 65 61 64 20 6f 66 20 55 54 46 2d   instead of UTF-
32dea 31 36 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  16 */.  sqlite3_
32deb 76 61 6c 75 65 20 2a 70 56 61 6c 3b 0a 20 20 69  value *pVal;.  i
32dec 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74  nt rc;..  assert
32ded 28 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 20  ( zFilename );. 
32dee 20 61 73 73 65 72 74 28 20 70 70 44 62 20 29 3b   assert( ppDb );
32def 0a 20 20 2a 70 70 44 62 20 3d 20 30 3b 0a 23 69  .  *ppDb = 0;.#i
32df0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
32df1 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 72 63 20  T_AUTOINIT.  rc 
32df2 3d 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  = sqlite3_initia
32df3 6c 69 7a 65 28 29 3b 0a 20 20 69 66 28 20 72 63  lize();.  if( rc
32df4 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65   ) return rc;.#e
32df5 6e 64 69 66 0a 20 20 70 56 61 6c 20 3d 20 73 71  ndif.  pVal = sq
32df6 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 30 29  lite3ValueNew(0)
32df7 3b 0a 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65  ;.  sqlite3Value
32df8 53 65 74 53 74 72 28 70 56 61 6c 2c 20 2d 31 2c  SetStr(pVal, -1,
32df9 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 53 51 4c 49   zFilename, SQLI
32dfa 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20  TE_UTF16NATIVE, 
32dfb 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
32dfc 20 20 7a 46 69 6c 65 6e 61 6d 65 38 20 3d 20 73    zFilename8 = s
32dfd 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28  qlite3ValueText(
32dfe 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46  pVal, SQLITE_UTF
32dff 38 29 3b 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e  8);.  if( zFilen
32e00 61 6d 65 38 20 29 7b 0a 20 20 20 20 72 63 20 3d  ame8 ){.    rc =
32e01 20 6f 70 65 6e 44 61 74 61 62 61 73 65 28 7a 46   openDatabase(zF
32e02 69 6c 65 6e 61 6d 65 38 2c 20 70 70 44 62 2c 0a  ilename8, ppDb,.
32e03 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32e04 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45        SQLITE_OPE
32e05 4e 5f 52 45 41 44 57 52 49 54 45 20 7c 20 53 51  N_READWRITE | SQ
32e06 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45  LITE_OPEN_CREATE
32e07 2c 20 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74  , 0);.    assert
32e08 28 20 2a 70 70 44 62 20 7c 7c 20 72 63 3d 3d 53  ( *ppDb || rc==S
32e09 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20  QLITE_NOMEM );. 
32e0a 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
32e0b 45 5f 4f 4b 20 26 26 20 21 44 62 48 61 73 50 72  E_OK && !DbHasPr
32e0c 6f 70 65 72 74 79 28 2a 70 70 44 62 2c 20 30 2c  operty(*ppDb, 0,
32e0d 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64   DB_SchemaLoaded
32e0e 29 20 29 7b 0a 20 20 20 20 20 20 45 4e 43 28 2a  ) ){.      ENC(*
32e0f 70 70 44 62 29 20 3d 20 53 51 4c 49 54 45 5f 55  ppDb) = SQLITE_U
32e10 54 46 31 36 4e 41 54 49 56 45 3b 0a 20 20 20 20  TF16NATIVE;.    
32e11 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  }.  }else{.    r
32e12 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
32e13 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56  ;.  }.  sqlite3V
32e14 61 6c 75 65 46 72 65 65 28 70 56 61 6c 29 3b 0a  alueFree(pVal);.
32e15 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
32e16 33 41 70 69 45 78 69 74 28 30 2c 20 72 63 29 3b  3ApiExit(0, rc);
32e17 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
32e18 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a  ITE_OMIT_UTF16 *
32e19 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65  /../*.** Registe
32e1a 72 20 61 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f  r a new collatio
32e1b 6e 20 73 65 71 75 65 6e 63 65 20 77 69 74 68 20  n sequence with 
32e1c 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e  the database han
32e1d 64 6c 65 20 64 62 2e 0a 2a 2f 0a 53 51 4c 49 54  dle db..*/.SQLIT
32e1e 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
32e1f 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69  3_create_collati
32e20 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 2a 20 64  on(.  sqlite3* d
32e21 62 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  b, .  const char
32e22 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20 69 6e 74 20   *zName, .  int 
32e23 65 6e 63 2c 20 0a 20 20 76 6f 69 64 2a 20 70 43  enc, .  void* pC
32e24 74 78 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70  tx,.  int(*xComp
32e25 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63  are)(void*,int,c
32e26 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63  onst void*,int,c
32e27 6f 6e 73 74 20 76 6f 69 64 2a 29 0a 29 7b 0a 20  onst void*).){. 
32e28 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74   int rc;.  sqlit
32e29 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
32e2a 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 61 73 73  b->mutex);.  ass
32e2b 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63  ert( !db->malloc
32e2c 46 61 69 6c 65 64 20 29 3b 0a 20 20 72 63 20 3d  Failed );.  rc =
32e2d 20 63 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e   createCollation
32e2e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 28 75 38 29  (db, zName, (u8)
32e2f 65 6e 63 2c 20 53 51 4c 49 54 45 5f 43 4f 4c 4c  enc, SQLITE_COLL
32e30 5f 55 53 45 52 2c 20 70 43 74 78 2c 20 78 43 6f  _USER, pCtx, xCo
32e31 6d 70 61 72 65 2c 20 30 29 3b 0a 20 20 72 63 20  mpare, 0);.  rc 
32e32 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74  = sqlite3ApiExit
32e33 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69  (db, rc);.  sqli
32e34 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
32e35 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  db->mutex);.  re
32e36 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
32e37 2a 20 52 65 67 69 73 74 65 72 20 61 20 6e 65 77  * Register a new
32e38 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
32e39 6e 63 65 20 77 69 74 68 20 74 68 65 20 64 61 74  nce with the dat
32e3a 61 62 61 73 65 20 68 61 6e 64 6c 65 20 64 62 2e  abase handle db.
32e3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
32e3c 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  nt sqlite3_creat
32e3d 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 0a  e_collation_v2(.
32e3e 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 0a    sqlite3* db, .
32e3f 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
32e40 61 6d 65 2c 20 0a 20 20 69 6e 74 20 65 6e 63 2c  ame, .  int enc,
32e41 20 0a 20 20 76 6f 69 64 2a 20 70 43 74 78 2c 0a   .  void* pCtx,.
32e42 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29    int(*xCompare)
32e43 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74  (void*,int,const
32e44 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74   void*,int,const
32e45 20 76 6f 69 64 2a 29 2c 0a 20 20 76 6f 69 64 28   void*),.  void(
32e46 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b  *xDel)(void*).){
32e47 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c  .  int rc;.  sql
32e48 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
32e49 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 61  (db->mutex);.  a
32e4a 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c  ssert( !db->mall
32e4b 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 72 63  ocFailed );.  rc
32e4c 20 3d 20 63 72 65 61 74 65 43 6f 6c 6c 61 74 69   = createCollati
32e4d 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 28 75  on(db, zName, (u
32e4e 38 29 65 6e 63 2c 20 53 51 4c 49 54 45 5f 43 4f  8)enc, SQLITE_CO
32e4f 4c 4c 5f 55 53 45 52 2c 20 70 43 74 78 2c 20 78  LL_USER, pCtx, x
32e50 43 6f 6d 70 61 72 65 2c 20 78 44 65 6c 29 3b 0a  Compare, xDel);.
32e51 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70    rc = sqlite3Ap
32e52 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20  iExit(db, rc);. 
32e53 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
32e54 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
32e55 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
32e56 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
32e57 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a  OMIT_UTF16./*.**
32e58 20 52 65 67 69 73 74 65 72 20 61 20 6e 65 77 20   Register a new 
32e59 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
32e5a 63 65 20 77 69 74 68 20 74 68 65 20 64 61 74 61  ce with the data
32e5b 62 61 73 65 20 68 61 6e 64 6c 65 20 64 62 2e 0a  base handle db..
32e5c 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
32e5d 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  t sqlite3_create
32e5e 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 0a 20 20  _collation16(.  
32e5f 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 0a 20 20  sqlite3* db, .  
32e60 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 4e 61 6d  const void *zNam
32e61 65 2c 0a 20 20 69 6e 74 20 65 6e 63 2c 20 0a 20  e,.  int enc, . 
32e62 20 76 6f 69 64 2a 20 70 43 74 78 2c 0a 20 20 69   void* pCtx,.  i
32e63 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f  nt(*xCompare)(vo
32e64 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
32e65 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
32e66 69 64 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 72 63  id*).){.  int rc
32e67 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
32e68 63 68 61 72 20 2a 7a 4e 61 6d 65 38 3b 0a 20 20  char *zName8;.  
32e69 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
32e6a 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
32e6b 20 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d    assert( !db->m
32e6c 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
32e6d 20 7a 4e 61 6d 65 38 20 3d 20 73 71 6c 69 74 65   zName8 = sqlite
32e6e 33 55 74 66 31 36 74 6f 38 28 64 62 2c 20 7a 4e  3Utf16to8(db, zN
32e6f 61 6d 65 2c 20 2d 31 29 3b 0a 20 20 69 66 28 20  ame, -1);.  if( 
32e70 7a 4e 61 6d 65 38 20 29 7b 0a 20 20 20 20 72 63  zName8 ){.    rc
32e71 20 3d 20 63 72 65 61 74 65 43 6f 6c 6c 61 74 69   = createCollati
32e72 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 38 2c 20 28  on(db, zName8, (
32e73 75 38 29 65 6e 63 2c 20 53 51 4c 49 54 45 5f 43  u8)enc, SQLITE_C
32e74 4f 4c 4c 5f 55 53 45 52 2c 20 70 43 74 78 2c 20  OLL_USER, pCtx, 
32e75 78 43 6f 6d 70 61 72 65 2c 20 30 29 3b 0a 20 20  xCompare, 0);.  
32e76 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
32e77 64 62 2c 20 7a 4e 61 6d 65 38 29 3b 0a 20 20 7d  db, zName8);.  }
32e78 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41  .  rc = sqlite3A
32e79 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a  piExit(db, rc);.
32e7a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
32e7b 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
32e7c 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
32e7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
32e7e 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a  E_OMIT_UTF16 */.
32e7f 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20  ./*.** Register 
32e80 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  a collation sequ
32e81 65 6e 63 65 20 66 61 63 74 6f 72 79 20 63 61 6c  ence factory cal
32e82 6c 62 61 63 6b 20 77 69 74 68 20 74 68 65 20 64  lback with the d
32e83 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a  atabase handle.*
32e84 2a 20 64 62 2e 20 52 65 70 6c 61 63 65 20 61 6e  * db. Replace an
32e85 79 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e 73  y previously ins
32e86 74 61 6c 6c 65 64 20 63 6f 6c 6c 61 74 69 6f 6e  talled collation
32e87 20 73 65 71 75 65 6e 63 65 20 66 61 63 74 6f 72   sequence factor
32e88 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  y..*/.SQLITE_API
32e89 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c   int sqlite3_col
32e8a 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 0a 20  lation_needed(. 
32e8b 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20   sqlite3 *db, . 
32e8c 20 76 6f 69 64 20 2a 70 43 6f 6c 6c 4e 65 65 64   void *pCollNeed
32e8d 65 64 41 72 67 2c 20 0a 20 20 76 6f 69 64 28 2a  edArg, .  void(*
32e8e 78 43 6f 6c 6c 4e 65 65 64 65 64 29 28 76 6f 69  xCollNeeded)(voi
32e8f 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20  d*,sqlite3*,int 
32e90 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 63  eTextRep,const c
32e91 68 61 72 2a 29 0a 29 7b 0a 20 20 73 71 6c 69 74  har*).){.  sqlit
32e92 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
32e93 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64 62 2d  b->mutex);.  db-
32e94 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 20 3d 20 78  >xCollNeeded = x
32e95 43 6f 6c 6c 4e 65 65 64 65 64 3b 0a 20 20 64 62  CollNeeded;.  db
32e96 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 20  ->xCollNeeded16 
32e97 3d 20 30 3b 0a 20 20 64 62 2d 3e 70 43 6f 6c 6c  = 0;.  db->pColl
32e98 4e 65 65 64 65 64 41 72 67 20 3d 20 70 43 6f 6c  NeededArg = pCol
32e99 6c 4e 65 65 64 65 64 41 72 67 3b 0a 20 20 73 71  lNeededArg;.  sq
32e9a 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
32e9b 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
32e9c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
32e9d 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
32e9e 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f  ITE_OMIT_UTF16./
32e9f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20  *.** Register a 
32ea0 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
32ea1 63 65 20 66 61 63 74 6f 72 79 20 63 61 6c 6c 62  ce factory callb
32ea2 61 63 6b 20 77 69 74 68 20 74 68 65 20 64 61 74  ack with the dat
32ea3 61 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a 2a 20  abase handle.** 
32ea4 64 62 2e 20 52 65 70 6c 61 63 65 20 61 6e 79 20  db. Replace any 
32ea5 70 72 65 76 69 6f 75 73 6c 79 20 69 6e 73 74 61  previously insta
32ea6 6c 6c 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  lled collation s
32ea7 65 71 75 65 6e 63 65 20 66 61 63 74 6f 72 79 2e  equence factory.
32ea8 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
32ea9 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61  nt sqlite3_colla
32eaa 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28 0a 20  tion_needed16(. 
32eab 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20   sqlite3 *db, . 
32eac 20 76 6f 69 64 20 2a 70 43 6f 6c 6c 4e 65 65 64   void *pCollNeed
32ead 65 64 41 72 67 2c 20 0a 20 20 76 6f 69 64 28 2a  edArg, .  void(*
32eae 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 29 28 76  xCollNeeded16)(v
32eaf 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e  oid*,sqlite3*,in
32eb0 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74  t eTextRep,const
32eb1 20 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 73 71 6c   void*).){.  sql
32eb2 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
32eb3 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64  (db->mutex);.  d
32eb4 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 20 3d  b->xCollNeeded =
32eb5 20 30 3b 0a 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e   0;.  db->xCollN
32eb6 65 65 64 65 64 31 36 20 3d 20 78 43 6f 6c 6c 4e  eeded16 = xCollN
32eb7 65 65 64 65 64 31 36 3b 0a 20 20 64 62 2d 3e 70  eeded16;.  db->p
32eb8 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 20 3d 20  CollNeededArg = 
32eb9 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 3b 0a  pCollNeededArg;.
32eba 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
32ebb 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
32ebc 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
32ebd 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  E_OK;.}.#endif /
32ebe 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  * SQLITE_OMIT_UT
32ebf 46 31 36 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  F16 */..#ifndef 
32ec0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 47 4c 4f 42  SQLITE_OMIT_GLOB
32ec1 41 4c 52 45 43 4f 56 45 52 0a 23 69 66 6e 64 65  ALRECOVER.#ifnde
32ec2 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45  f SQLITE_OMIT_DE
32ec3 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 54  PRECATED./*.** T
32ec4 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
32ec5 6e 6f 77 20 61 6e 20 61 6e 61 63 68 72 6f 6e 69  now an anachroni
32ec6 73 6d 2e 20 49 74 20 75 73 65 64 20 74 6f 20 62  sm. It used to b
32ec7 65 20 75 73 65 64 20 74 6f 20 72 65 63 6f 76 65  e used to recove
32ec8 72 20 66 72 6f 6d 20 61 0a 2a 2a 20 6d 61 6c 6c  r from a.** mall
32ec9 6f 63 28 29 20 66 61 69 6c 75 72 65 2c 20 62 75  oc() failure, bu
32eca 74 20 53 51 4c 69 74 65 20 6e 6f 77 20 64 6f 65  t SQLite now doe
32ecb 73 20 74 68 69 73 20 61 75 74 6f 6d 61 74 69 63  s this automatic
32ecc 61 6c 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ally..*/.SQLITE_
32ecd 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
32ece 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72 28 76  global_recover(v
32ecf 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 53  oid){.  return S
32ed0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64  QLITE_OK;.}.#end
32ed1 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  if.#endif../*.**
32ed2 20 54 65 73 74 20 74 6f 20 73 65 65 20 77 68 65   Test to see whe
32ed3 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20  ther or not the 
32ed4 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
32ed5 69 6f 6e 20 69 73 20 69 6e 20 61 75 74 6f 63 6f  ion is in autoco
32ed6 6d 6d 69 74 0a 2a 2a 20 6d 6f 64 65 2e 20 20 52  mmit.** mode.  R
32ed7 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 69 74  eturn TRUE if it
32ed8 20 69 73 20 61 6e 64 20 46 41 4c 53 45 20 69 66   is and FALSE if
32ed9 20 6e 6f 74 2e 20 20 41 75 74 6f 63 6f 6d 6d 69   not.  Autocommi
32eda 74 20 6d 6f 64 65 20 69 73 20 6f 6e 0a 2a 2a 20  t mode is on.** 
32edb 62 79 20 64 65 66 61 75 6c 74 2e 20 20 41 75 74  by default.  Aut
32edc 6f 63 6f 6d 6d 69 74 20 69 73 20 64 69 73 61 62  ocommit is disab
32edd 6c 65 64 20 62 79 20 61 20 42 45 47 49 4e 20 73  led by a BEGIN s
32ede 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 72 65 65  tatement and ree
32edf 6e 61 62 6c 65 64 0a 2a 2a 20 62 79 20 74 68 65  nabled.** by the
32ee0 20 6e 65 78 74 20 43 4f 4d 4d 49 54 20 6f 72 20   next COMMIT or 
32ee1 52 4f 4c 4c 42 41 43 4b 2e 0a 2a 2a 0a 2a 2a 2a  ROLLBACK..**.***
32ee2 2a 2a 2a 2a 20 54 48 49 53 20 49 53 20 41 4e 20  **** THIS IS AN 
32ee3 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 41 50 49  EXPERIMENTAL API
32ee4 20 41 4e 44 20 49 53 20 53 55 42 4a 45 43 54 20   AND IS SUBJECT 
32ee5 54 4f 20 43 48 41 4e 47 45 20 2a 2a 2a 2a 2a 2a  TO CHANGE ******
32ee6 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
32ee7 6e 74 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61  nt sqlite3_get_a
32ee8 75 74 6f 63 6f 6d 6d 69 74 28 73 71 6c 69 74 65  utocommit(sqlite
32ee9 33 20 2a 64 62 29 7b 0a 20 20 72 65 74 75 72 6e  3 *db){.  return
32eea 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3b   db->autoCommit;
32eeb 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .}..#ifdef SQLIT
32eec 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68  E_DEBUG./*.** Th
32eed 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74  e following rout
32eee 69 6e 65 20 69 73 20 73 75 62 74 69 74 75 74 65  ine is subtitute
32eef 64 20 66 6f 72 20 63 6f 6e 73 74 61 6e 74 20 53  d for constant S
32ef0 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 69 6e  QLITE_CORRUPT in
32ef1 0a 2a 2a 20 64 65 62 75 67 67 69 6e 67 20 62 75  .** debugging bu
32ef2 69 6c 64 73 2e 20 20 54 68 69 73 20 70 72 6f 76  ilds.  This prov
32ef3 69 64 65 73 20 61 20 77 61 79 20 74 6f 20 73 65  ides a way to se
32ef4 74 20 61 20 62 72 65 61 6b 70 6f 69 6e 74 20 66  t a breakpoint f
32ef5 6f 72 20 77 68 65 6e 0a 2a 2a 20 63 6f 72 72 75  or when.** corru
32ef6 70 74 69 6f 6e 20 69 73 20 66 69 72 73 74 20 64  ption is first d
32ef7 65 74 65 63 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49  etected..*/.SQLI
32ef8 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
32ef9 71 6c 69 74 65 33 43 6f 72 72 75 70 74 28 76 6f  qlite3Corrupt(vo
32efa 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 53 51  id){.  return SQ
32efb 4c 49 54 45 5f 43 4f 52 52 55 50 54 3b 0a 7d 0a  LITE_CORRUPT;.}.
32efc 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20  #endif..#ifndef 
32efd 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52  SQLITE_OMIT_DEPR
32efe 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 54 68 69  ECATED./*.** Thi
32eff 73 20 69 73 20 61 20 63 6f 6e 76 65 6e 69 65 6e  s is a convenien
32f00 63 65 20 72 6f 75 74 69 6e 65 20 74 68 61 74 20  ce routine that 
32f01 6d 61 6b 65 73 20 73 75 72 65 20 74 68 61 74 20  makes sure that 
32f02 61 6c 6c 20 74 68 72 65 61 64 2d 73 70 65 63 69  all thread-speci
32f03 66 69 63 0a 2a 2a 20 64 61 74 61 20 66 6f 72 20  fic.** data for 
32f04 74 68 69 73 20 74 68 72 65 61 64 20 68 61 73 20  this thread has 
32f05 62 65 65 6e 20 64 65 61 6c 6c 6f 63 61 74 65 64  been deallocated
32f06 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 6e  ..**.** SQLite n
32f07 6f 20 6c 6f 6e 67 65 72 20 75 73 65 73 20 74 68  o longer uses th
32f08 72 65 61 64 2d 73 70 65 63 69 66 69 63 20 64 61  read-specific da
32f09 74 61 20 73 6f 20 74 68 69 73 20 72 6f 75 74 69  ta so this routi
32f0a 6e 65 20 69 73 20 6e 6f 77 20 61 0a 2a 2a 20 6e  ne is now a.** n
32f0b 6f 2d 6f 70 2e 20 20 49 74 20 69 73 20 72 65 74  o-op.  It is ret
32f0c 61 69 6e 65 64 20 66 6f 72 20 68 69 73 74 6f 72  ained for histor
32f0d 69 63 61 6c 20 63 6f 6d 70 61 74 69 62 69 6c 69  ical compatibili
32f0e 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ty..*/.SQLITE_AP
32f0f 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 74  I void sqlite3_t
32f10 68 72 65 61 64 5f 63 6c 65 61 6e 75 70 28 76 6f  hread_cleanup(vo
32f11 69 64 29 7b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  id){.}.#endif../
32f12 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 6d 65 74 61  *.** Return meta
32f13 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
32f14 75 74 20 61 20 73 70 65 63 69 66 69 63 20 63 6f  ut a specific co
32f15 6c 75 6d 6e 20 6f 66 20 61 20 64 61 74 61 62 61  lumn of a databa
32f16 73 65 20 74 61 62 6c 65 2e 0a 2a 2a 20 53 65 65  se table..** See
32f17 20 63 6f 6d 6d 65 6e 74 20 69 6e 20 73 71 6c 69   comment in sqli
32f18 74 65 33 2e 68 20 28 73 71 6c 69 74 65 2e 68 2e  te3.h (sqlite.h.
32f19 69 6e 29 20 66 6f 72 20 64 65 74 61 69 6c 73 2e  in) for details.
32f1a 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
32f1b 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f  E_ENABLE_COLUMN_
32f1c 4d 45 54 41 44 41 54 41 0a 53 51 4c 49 54 45 5f  METADATA.SQLITE_
32f1d 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
32f1e 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74  table_column_met
32f1f 61 64 61 74 61 28 0a 20 20 73 71 6c 69 74 65 33  adata(.  sqlite3
32f20 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
32f21 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69       /* Connecti
32f22 6f 6e 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63  on handle */.  c
32f23 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 4e 61  onst char *zDbNa
32f24 6d 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 44 61  me,        /* Da
32f25 74 61 62 61 73 65 20 6e 61 6d 65 20 6f 72 20 4e  tabase name or N
32f26 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ULL */.  const c
32f27 68 61 72 20 2a 7a 54 61 62 6c 65 4e 61 6d 65 2c  har *zTableName,
32f28 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 6e 61       /* Table na
32f29 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  me */.  const ch
32f2a 61 72 20 2a 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 2c  ar *zColumnName,
32f2b 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61      /* Column na
32f2c 6d 65 20 2a 2f 0a 20 20 63 68 61 72 20 63 6f 6e  me */.  char con
32f2d 73 74 20 2a 2a 70 7a 44 61 74 61 54 79 70 65 2c  st **pzDataType,
32f2e 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 44      /* OUTPUT: D
32f2f 65 63 6c 61 72 65 64 20 64 61 74 61 20 74 79 70  eclared data typ
32f30 65 20 2a 2f 0a 20 20 63 68 61 72 20 63 6f 6e 73  e */.  char cons
32f31 74 20 2a 2a 70 7a 43 6f 6c 6c 53 65 71 2c 20 20  t **pzCollSeq,  
32f32 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 43 6f     /* OUTPUT: Co
32f33 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
32f34 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 2a   name */.  int *
32f35 70 4e 6f 74 4e 75 6c 6c 2c 20 20 20 20 20 20 20  pNotNull,       
32f36 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 54         /* OUTPUT
32f37 3a 20 54 72 75 65 20 69 66 20 4e 4f 54 20 4e 55  : True if NOT NU
32f38 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 20 65 78  LL constraint ex
32f39 69 73 74 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  ists */.  int *p
32f3a 50 72 69 6d 61 72 79 4b 65 79 2c 20 20 20 20 20  PrimaryKey,     
32f3b 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a        /* OUTPUT:
32f3c 20 54 72 75 65 20 69 66 20 63 6f 6c 75 6d 6e 20   True if column 
32f3d 70 61 72 74 20 6f 66 20 50 4b 20 2a 2f 0a 20 20  part of PK */.  
32f3e 69 6e 74 20 2a 70 41 75 74 6f 69 6e 63 20 20 20  int *pAutoinc   
32f3f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
32f40 55 54 50 55 54 3a 20 54 72 75 65 20 69 66 20 63  UTPUT: True if c
32f41 6f 6c 75 6d 6e 20 69 73 20 61 75 74 6f 2d 69 6e  olumn is auto-in
32f42 63 72 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20  crement */.){.  
32f43 69 6e 74 20 72 63 3b 0a 20 20 63 68 61 72 20 2a  int rc;.  char *
32f44 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 54  zErrMsg = 0;.  T
32f45 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 30 3b 0a  able *pTab = 0;.
32f46 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d    Column *pCol =
32f47 20 30 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 0a   0;.  int iCol;.
32f48 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a  .  char const *z
32f49 44 61 74 61 54 79 70 65 20 3d 20 30 3b 0a 20 20  DataType = 0;.  
32f4a 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 43 6f 6c  char const *zCol
32f4b 6c 53 65 71 20 3d 20 30 3b 0a 20 20 69 6e 74 20  lSeq = 0;.  int 
32f4c 6e 6f 74 6e 75 6c 6c 20 3d 20 30 3b 0a 20 20 69  notnull = 0;.  i
32f4d 6e 74 20 70 72 69 6d 61 72 79 6b 65 79 20 3d 20  nt primarykey = 
32f4e 30 3b 0a 20 20 69 6e 74 20 61 75 74 6f 69 6e 63  0;.  int autoinc
32f4f 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 45 6e 73 75   = 0;..  /* Ensu
32f50 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  re the database 
32f51 73 63 68 65 6d 61 20 68 61 73 20 62 65 65 6e 20  schema has been 
32f52 6c 6f 61 64 65 64 20 2a 2f 0a 20 20 73 71 6c 69  loaded */.  sqli
32f53 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
32f54 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 28 76  db->mutex);.  (v
32f55 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74  oid)sqlite3Safet
32f56 79 4f 6e 28 64 62 29 3b 0a 20 20 73 71 6c 69 74  yOn(db);.  sqlit
32f57 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28  e3BtreeEnterAll(
32f58 64 62 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  db);.  rc = sqli
32f59 74 65 33 49 6e 69 74 28 64 62 2c 20 26 7a 45 72  te3Init(db, &zEr
32f5a 72 4d 73 67 29 3b 0a 20 20 69 66 28 20 53 51 4c  rMsg);.  if( SQL
32f5b 49 54 45 5f 4f 4b 21 3d 72 63 20 29 7b 0a 20 20  ITE_OK!=rc ){.  
32f5c 20 20 67 6f 74 6f 20 65 72 72 6f 72 5f 6f 75 74    goto error_out
32f5d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 63 61  ;.  }..  /* Loca
32f5e 74 65 20 74 68 65 20 74 61 62 6c 65 20 69 6e 20  te the table in 
32f5f 71 75 65 73 74 69 6f 6e 20 2a 2f 0a 20 20 70 54  question */.  pT
32f60 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  ab = sqlite3Find
32f61 54 61 62 6c 65 28 64 62 2c 20 7a 54 61 62 6c 65  Table(db, zTable
32f62 4e 61 6d 65 2c 20 7a 44 62 4e 61 6d 65 29 3b 0a  Name, zDbName);.
32f63 20 20 69 66 28 20 21 70 54 61 62 20 7c 7c 20 70    if( !pTab || p
32f64 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a  Tab->pSelect ){.
32f65 20 20 20 20 70 54 61 62 20 3d 20 30 3b 0a 20 20      pTab = 0;.  
32f66 20 20 67 6f 74 6f 20 65 72 72 6f 72 5f 6f 75 74    goto error_out
32f67 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 6e 64  ;.  }..  /* Find
32f68 20 74 68 65 20 63 6f 6c 75 6d 6e 20 66 6f 72 20   the column for 
32f69 77 68 69 63 68 20 69 6e 66 6f 20 69 73 20 72 65  which info is re
32f6a 71 75 65 73 74 65 64 20 2a 2f 0a 20 20 69 66 28  quested */.  if(
32f6b 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28   sqlite3IsRowid(
32f6c 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 29 20 29 7b 0a  zColumnName) ){.
32f6d 20 20 20 20 69 43 6f 6c 20 3d 20 70 54 61 62 2d      iCol = pTab-
32f6e 3e 69 50 4b 65 79 3b 0a 20 20 20 20 69 66 28 20  >iPKey;.    if( 
32f6f 69 43 6f 6c 3e 3d 30 20 29 7b 0a 20 20 20 20 20  iCol>=0 ){.     
32f70 20 70 43 6f 6c 20 3d 20 26 70 54 61 62 2d 3e 61   pCol = &pTab->a
32f71 43 6f 6c 5b 69 43 6f 6c 5d 3b 0a 20 20 20 20 7d  Col[iCol];.    }
32f72 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6f  .  }else{.    fo
32f73 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70  r(iCol=0; iCol<p
32f74 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b  Tab->nCol; iCol+
32f75 2b 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c 20 3d  +){.      pCol =
32f76 20 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f   &pTab->aCol[iCo
32f77 6c 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 30 3d  l];.      if( 0=
32f78 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28  =sqlite3StrICmp(
32f79 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f  pCol->zName, zCo
32f7a 6c 75 6d 6e 4e 61 6d 65 29 20 29 7b 0a 20 20 20  lumnName) ){.   
32f7b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32f7c 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
32f7d 28 20 69 43 6f 6c 3d 3d 70 54 61 62 2d 3e 6e 43  ( iCol==pTab->nC
32f7e 6f 6c 20 29 7b 0a 20 20 20 20 20 20 70 54 61 62  ol ){.      pTab
32f7f 20 3d 20 30 3b 0a 20 20 20 20 20 20 67 6f 74 6f   = 0;.      goto
32f80 20 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 20 20   error_out;.    
32f81 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20  }.  }..  /* The 
32f82 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20  following block 
32f83 73 74 6f 72 65 73 20 74 68 65 20 6d 65 74 61 20  stores the meta 
32f84 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74  information that
32f85 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65   will be returne
32f86 64 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 63 61  d.  ** to the ca
32f87 6c 6c 65 72 20 69 6e 20 6c 6f 63 61 6c 20 76 61  ller in local va
32f88 72 69 61 62 6c 65 73 20 7a 44 61 74 61 54 79 70  riables zDataTyp
32f89 65 2c 20 7a 43 6f 6c 6c 53 65 71 2c 20 6e 6f 74  e, zCollSeq, not
32f8a 6e 75 6c 6c 2c 20 70 72 69 6d 61 72 79 6b 65 79  null, primarykey
32f8b 0a 20 20 2a 2a 20 61 6e 64 20 61 75 74 6f 69 6e  .  ** and autoin
32f8c 63 2e 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74  c. At this point
32f8d 20 74 68 65 72 65 20 61 72 65 20 74 77 6f 20 70   there are two p
32f8e 6f 73 73 69 62 69 6c 69 74 69 65 73 3a 0a 20 20  ossibilities:.  
32f8f 2a 2a 20 0a 20 20 2a 2a 20 20 20 20 20 31 2e 20  ** .  **     1. 
32f90 54 68 65 20 73 70 65 63 69 66 69 65 64 20 63 6f  The specified co
32f91 6c 75 6d 6e 20 6e 61 6d 65 20 77 61 73 20 72 6f  lumn name was ro
32f92 77 69 64 22 2c 20 22 6f 69 64 22 20 6f 72 20 22  wid", "oid" or "
32f93 5f 72 6f 77 69 64 5f 22 20 0a 20 20 2a 2a 20 20  _rowid_" .  **  
32f94 20 20 20 20 20 20 61 6e 64 20 74 68 65 72 65 20        and there 
32f95 69 73 20 6e 6f 20 65 78 70 6c 69 63 69 74 6c 79  is no explicitly
32f96 20 64 65 63 6c 61 72 65 64 20 49 50 4b 20 63 6f   declared IPK co
32f97 6c 75 6d 6e 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a  lumn. .  **.  **
32f98 20 20 20 20 20 32 2e 20 54 68 65 20 74 61 62 6c       2. The tabl
32f99 65 20 69 73 20 6e 6f 74 20 61 20 76 69 65 77 20  e is not a view 
32f9a 61 6e 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e  and the column n
32f9b 61 6d 65 20 69 64 65 6e 74 69 66 69 65 64 20 61  ame identified a
32f9c 6e 20 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 65  n .  **        e
32f9d 78 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72  xplicitly declar
32f9e 65 64 20 63 6f 6c 75 6d 6e 2e 20 43 6f 70 79 20  ed column. Copy 
32f9f 6d 65 74 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  meta information
32fa0 20 66 72 6f 6d 20 2a 70 43 6f 6c 2e 0a 20 20 2a   from *pCol..  *
32fa1 2f 20 0a 20 20 69 66 28 20 70 43 6f 6c 20 29 7b  / .  if( pCol ){
32fa2 0a 20 20 20 20 7a 44 61 74 61 54 79 70 65 20 3d  .    zDataType =
32fa3 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 3b 0a 20 20   pCol->zType;.  
32fa4 20 20 7a 43 6f 6c 6c 53 65 71 20 3d 20 70 43 6f    zCollSeq = pCo
32fa5 6c 2d 3e 7a 43 6f 6c 6c 3b 0a 20 20 20 20 6e 6f  l->zColl;.    no
32fa6 74 6e 75 6c 6c 20 3d 20 70 43 6f 6c 2d 3e 6e 6f  tnull = pCol->no
32fa7 74 4e 75 6c 6c 21 3d 30 3b 0a 20 20 20 20 70 72  tNull!=0;.    pr
32fa8 69 6d 61 72 79 6b 65 79 20 20 3d 20 70 43 6f 6c  imarykey  = pCol
32fa9 2d 3e 69 73 50 72 69 6d 4b 65 79 21 3d 30 3b 0a  ->isPrimKey!=0;.
32faa 20 20 20 20 61 75 74 6f 69 6e 63 20 3d 20 70 54      autoinc = pT
32fab 61 62 2d 3e 69 50 4b 65 79 3d 3d 69 43 6f 6c 20  ab->iPKey==iCol 
32fac 26 26 20 28 70 54 61 62 2d 3e 74 61 62 46 6c 61  && (pTab->tabFla
32fad 67 73 20 26 20 54 46 5f 41 75 74 6f 69 6e 63 72  gs & TF_Autoincr
32fae 65 6d 65 6e 74 29 21 3d 30 3b 0a 20 20 7d 65 6c  ement)!=0;.  }el
32faf 73 65 7b 0a 20 20 20 20 7a 44 61 74 61 54 79 70  se{.    zDataTyp
32fb0 65 20 3d 20 22 49 4e 54 45 47 45 52 22 3b 0a 20  e = "INTEGER";. 
32fb1 20 20 20 70 72 69 6d 61 72 79 6b 65 79 20 3d 20     primarykey = 
32fb2 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 7a 43  1;.  }.  if( !zC
32fb3 6f 6c 6c 53 65 71 20 29 7b 0a 20 20 20 20 7a 43  ollSeq ){.    zC
32fb4 6f 6c 6c 53 65 71 20 3d 20 22 42 49 4e 41 52 59  ollSeq = "BINARY
32fb5 22 3b 0a 20 20 7d 0a 0a 65 72 72 6f 72 5f 6f 75  ";.  }..error_ou
32fb6 74 3a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  t:.  sqlite3Btre
32fb7 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20  eLeaveAll(db);. 
32fb8 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61   (void)sqlite3Sa
32fb9 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 0a 20 20  fetyOff(db);..  
32fba 2f 2a 20 57 68 65 74 68 65 72 20 74 68 65 20 66  /* Whether the f
32fbb 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 73 75 63  unction call suc
32fbc 63 65 65 64 65 64 20 6f 72 20 66 61 69 6c 65 64  ceeded or failed
32fbd 2c 20 73 65 74 20 74 68 65 20 6f 75 74 70 75 74  , set the output
32fbe 20 70 61 72 61 6d 65 74 65 72 73 0a 20 20 2a 2a   parameters.  **
32fbf 20 74 6f 20 77 68 61 74 65 76 65 72 20 74 68 65   to whatever the
32fc0 69 72 20 6c 6f 63 61 6c 20 63 6f 75 6e 74 65 72  ir local counter
32fc1 70 61 72 74 73 20 63 6f 6e 74 61 69 6e 2e 20 49  parts contain. I
32fc2 66 20 61 6e 20 65 72 72 6f 72 20 64 69 64 20 6f  f an error did o
32fc3 63 63 75 72 2c 0a 20 20 2a 2a 20 74 68 69 73 20  ccur,.  ** this 
32fc4 68 61 73 20 74 68 65 20 65 66 66 65 63 74 20 6f  has the effect o
32fc5 66 20 7a 65 72 6f 69 6e 67 20 61 6c 6c 20 6f 75  f zeroing all ou
32fc6 74 70 75 74 20 70 61 72 61 6d 65 74 65 72 73 2e  tput parameters.
32fc7 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 7a 44 61  .  */.  if( pzDa
32fc8 74 61 54 79 70 65 20 29 20 2a 70 7a 44 61 74 61  taType ) *pzData
32fc9 54 79 70 65 20 3d 20 7a 44 61 74 61 54 79 70 65  Type = zDataType
32fca 3b 0a 20 20 69 66 28 20 70 7a 43 6f 6c 6c 53 65  ;.  if( pzCollSe
32fcb 71 20 29 20 2a 70 7a 43 6f 6c 6c 53 65 71 20 3d  q ) *pzCollSeq =
32fcc 20 7a 43 6f 6c 6c 53 65 71 3b 0a 20 20 69 66 28   zCollSeq;.  if(
32fcd 20 70 4e 6f 74 4e 75 6c 6c 20 29 20 2a 70 4e 6f   pNotNull ) *pNo
32fce 74 4e 75 6c 6c 20 3d 20 6e 6f 74 6e 75 6c 6c 3b  tNull = notnull;
32fcf 0a 20 20 69 66 28 20 70 50 72 69 6d 61 72 79 4b  .  if( pPrimaryK
32fd0 65 79 20 29 20 2a 70 50 72 69 6d 61 72 79 4b 65  ey ) *pPrimaryKe
32fd1 79 20 3d 20 70 72 69 6d 61 72 79 6b 65 79 3b 0a  y = primarykey;.
32fd2 20 20 69 66 28 20 70 41 75 74 6f 69 6e 63 20 29    if( pAutoinc )
32fd3 20 2a 70 41 75 74 6f 69 6e 63 20 3d 20 61 75 74   *pAutoinc = aut
32fd4 6f 69 6e 63 3b 0a 0a 20 20 69 66 28 20 53 51 4c  oinc;..  if( SQL
32fd5 49 54 45 5f 4f 4b 3d 3d 72 63 20 26 26 20 21 70  ITE_OK==rc && !p
32fd6 54 61 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Tab ){.    sqlit
32fd7 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72  e3DbFree(db, zEr
32fd8 72 4d 73 67 29 3b 0a 20 20 20 20 7a 45 72 72 4d  rMsg);.    zErrM
32fd9 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  sg = sqlite3MPri
32fda 6e 74 66 28 64 62 2c 20 22 6e 6f 20 73 75 63 68  ntf(db, "no such
32fdb 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 3a 20 25   table column: %
32fdc 73 2e 25 73 22 2c 20 7a 54 61 62 6c 65 4e 61 6d  s.%s", zTableNam
32fdd 65 2c 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c 75  e,.        zColu
32fde 6d 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 72 63 20  mnName);.    rc 
32fdf 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
32fe0 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 72 72    }.  sqlite3Err
32fe1 6f 72 28 64 62 2c 20 72 63 2c 20 28 7a 45 72 72  or(db, rc, (zErr
32fe2 4d 73 67 3f 22 25 73 22 3a 30 29 2c 20 7a 45 72  Msg?"%s":0), zEr
32fe3 72 4d 73 67 29 3b 0a 20 20 73 71 6c 69 74 65 33  rMsg);.  sqlite3
32fe4 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 4d  DbFree(db, zErrM
32fe5 73 67 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  sg);.  rc = sqli
32fe6 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72  te3ApiExit(db, r
32fe7 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  c);.  sqlite3_mu
32fe8 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
32fe9 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  tex);.  return r
32fea 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  c;.}.#endif../*.
32feb 2a 2a 20 53 6c 65 65 70 20 66 6f 72 20 61 20 6c  ** Sleep for a l
32fec 69 74 74 6c 65 20 77 68 69 6c 65 2e 20 20 52 65  ittle while.  Re
32fed 74 75 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20  turn the amount 
32fee 6f 66 20 74 69 6d 65 20 73 6c 65 70 74 2e 0a 2a  of time slept..*
32fef 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
32ff0 20 73 71 6c 69 74 65 33 5f 73 6c 65 65 70 28 69   sqlite3_sleep(i
32ff1 6e 74 20 6d 73 29 7b 0a 20 20 73 71 6c 69 74 65  nt ms){.  sqlite
32ff2 33 5f 76 66 73 20 2a 70 56 66 73 3b 0a 20 20 69  3_vfs *pVfs;.  i
32ff3 6e 74 20 72 63 3b 0a 20 20 70 56 66 73 20 3d 20  nt rc;.  pVfs = 
32ff4 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64  sqlite3_vfs_find
32ff5 28 30 29 3b 0a 20 20 69 66 28 20 70 56 66 73 3d  (0);.  if( pVfs=
32ff6 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a  =0 ) return 0;..
32ff7 20 20 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69    /* This functi
32ff8 6f 6e 20 77 6f 72 6b 73 20 69 6e 20 6d 69 6c 6c  on works in mill
32ff9 69 73 65 63 6f 6e 64 73 2c 20 62 75 74 20 74 68  iseconds, but th
32ffa 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 4f 73 53  e underlying OsS
32ffb 6c 65 65 70 28 29 20 0a 20 20 2a 2a 20 41 50 49  leep() .  ** API
32ffc 20 75 73 65 73 20 6d 69 63 72 6f 73 65 63 6f 6e   uses microsecon
32ffd 64 73 2e 20 48 65 6e 63 65 20 74 68 65 20 31 30  ds. Hence the 10
32ffe 30 30 27 73 2e 0a 20 20 2a 2f 0a 20 20 72 63 20  00's..  */.  rc 
32fff 3d 20 28 73 71 6c 69 74 65 33 4f 73 53 6c 65 65  = (sqlite3OsSlee
33000 70 28 70 56 66 73 2c 20 31 30 30 30 2a 6d 73 29  p(pVfs, 1000*ms)
33001 2f 31 30 30 30 29 3b 0a 20 20 72 65 74 75 72 6e  /1000);.  return
33002 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e   rc;.}../*.** En
33003 61 62 6c 65 20 6f 72 20 64 69 73 61 62 6c 65 20  able or disable 
33004 74 68 65 20 65 78 74 65 6e 64 65 64 20 72 65 73  the extended res
33005 75 6c 74 20 63 6f 64 65 73 2e 0a 2a 2f 0a 53 51  ult codes..*/.SQ
33006 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
33007 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65  ite3_extended_re
33008 73 75 6c 74 5f 63 6f 64 65 73 28 73 71 6c 69 74  sult_codes(sqlit
33009 65 33 20 2a 64 62 2c 20 69 6e 74 20 6f 6e 6f 66  e3 *db, int onof
3300a 66 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  f){.  sqlite3_mu
3300b 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
3300c 74 65 78 29 3b 0a 20 20 64 62 2d 3e 65 72 72 4d  tex);.  db->errM
3300d 61 73 6b 20 3d 20 6f 6e 6f 66 66 20 3f 20 30 78  ask = onoff ? 0x
3300e 66 66 66 66 66 66 66 66 20 3a 20 30 78 66 66 3b  ffffffff : 0xff;
3300f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
33010 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
33011 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
33012 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
33013 49 6e 76 6f 6b 65 20 74 68 65 20 78 46 69 6c 65  Invoke the xFile
33014 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20 6f  Control method o
33015 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64  n a particular d
33016 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 53 51 4c 49  atabase..*/.SQLI
33017 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
33018 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28  e3_file_control(
33019 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e  sqlite3 *db, con
3301a 73 74 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65  st char *zDbName
3301b 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a  , int op, void *
3301c 70 41 72 67 29 7b 0a 20 20 69 6e 74 20 72 63 20  pArg){.  int rc 
3301d 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
3301e 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 73 71 6c    int iDb;.  sql
3301f 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
33020 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69  (db->mutex);.  i
33021 66 28 20 7a 44 62 4e 61 6d 65 3d 3d 30 20 29 7b  f( zDbName==0 ){
33022 0a 20 20 20 20 69 44 62 20 3d 20 30 3b 0a 20 20  .    iDb = 0;.  
33023 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6f 72 28 69  }else{.    for(i
33024 44 62 3d 30 3b 20 69 44 62 3c 64 62 2d 3e 6e 44  Db=0; iDb<db->nD
33025 62 3b 20 69 44 62 2b 2b 29 7b 0a 20 20 20 20 20  b; iDb++){.     
33026 20 69 66 28 20 73 74 72 63 6d 70 28 64 62 2d 3e   if( strcmp(db->
33027 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20  aDb[iDb].zName, 
33028 7a 44 62 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72  zDbName)==0 ) br
33029 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  eak;.    }.  }. 
3302a 20 69 66 28 20 69 44 62 3c 64 62 2d 3e 6e 44 62   if( iDb<db->nDb
3302b 20 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70   ){.    Btree *p
3302c 42 74 72 65 65 20 3d 20 64 62 2d 3e 61 44 62 5b  Btree = db->aDb[
3302d 69 44 62 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66  iDb].pBt;.    if
3302e 28 20 70 42 74 72 65 65 20 29 7b 0a 20 20 20 20  ( pBtree ){.    
3302f 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b    Pager *pPager;
33030 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
33031 69 6c 65 20 2a 66 64 3b 0a 20 20 20 20 20 20 73  ile *fd;.      s
33032 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
33033 28 70 42 74 72 65 65 29 3b 0a 20 20 20 20 20 20  (pBtree);.      
33034 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33  pPager = sqlite3
33035 42 74 72 65 65 50 61 67 65 72 28 70 42 74 72 65  BtreePager(pBtre
33036 65 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  e);.      assert
33037 28 20 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20  ( pPager!=0 );. 
33038 20 20 20 20 20 66 64 20 3d 20 73 71 6c 69 74 65       fd = sqlite
33039 33 50 61 67 65 72 46 69 6c 65 28 70 50 61 67 65  3PagerFile(pPage
3303a 72 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  r);.      assert
3303b 28 20 66 64 21 3d 30 20 29 3b 0a 20 20 20 20 20  ( fd!=0 );.     
3303c 20 69 66 28 20 66 64 2d 3e 70 4d 65 74 68 6f 64   if( fd->pMethod
3303d 73 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  s ){.        rc 
3303e 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 43  = sqlite3OsFileC
3303f 6f 6e 74 72 6f 6c 28 66 64 2c 20 6f 70 2c 20 70  ontrol(fd, op, p
33040 41 72 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Arg);.      }.  
33041 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
33042 4c 65 61 76 65 28 70 42 74 72 65 65 29 3b 0a 20  Leave(pBtree);. 
33043 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74     }.  }.  sqlit
33044 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
33045 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
33046 75 72 6e 20 72 63 3b 20 20 20 0a 7d 0a 0a 2f 2a  urn rc;   .}../*
33047 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 20 74 6f  .** Interface to
33048 20 74 68 65 20 74 65 73 74 69 6e 67 20 6c 6f 67   the testing log
33049 69 63 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ic..*/.SQLITE_AP
3304a 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 65  I int sqlite3_te
3304b 73 74 5f 63 6f 6e 74 72 6f 6c 28 69 6e 74 20 6f  st_control(int o
3304c 70 2c 20 2e 2e 2e 29 7b 0a 20 20 69 6e 74 20 72  p, ...){.  int r
3304d 63 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53  c = 0;.#ifndef S
3304e 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54  QLITE_OMIT_BUILT
3304f 49 4e 5f 54 45 53 54 0a 20 20 76 61 5f 6c 69 73  IN_TEST.  va_lis
33050 74 20 61 70 3b 0a 20 20 76 61 5f 73 74 61 72 74  t ap;.  va_start
33051 28 61 70 2c 20 6f 70 29 3b 0a 20 20 73 77 69 74  (ap, op);.  swit
33052 63 68 28 20 6f 70 20 29 7b 0a 0a 20 20 20 20 2f  ch( op ){..    /
33053 2a 0a 20 20 20 20 2a 2a 20 53 61 76 65 20 74 68  *.    ** Save th
33054 65 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 20  e current state 
33055 6f 66 20 74 68 65 20 50 52 4e 47 2e 0a 20 20 20  of the PRNG..   
33056 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c   */.    case SQL
33057 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e  ITE_TESTCTRL_PRN
33058 47 5f 53 41 56 45 3a 20 7b 0a 20 20 20 20 20 20  G_SAVE: {.      
33059 73 71 6c 69 74 65 33 50 72 6e 67 53 61 76 65 53  sqlite3PrngSaveS
3305a 74 61 74 65 28 29 3b 0a 20 20 20 20 20 20 62 72  tate();.      br
3305b 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  eak;.    }..    
3305c 2f 2a 0a 20 20 20 20 2a 2a 20 52 65 73 74 6f 72  /*.    ** Restor
3305d 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74  e the state of t
3305e 68 65 20 50 52 4e 47 20 74 6f 20 74 68 65 20 6c  he PRNG to the l
3305f 61 73 74 20 73 74 61 74 65 20 73 61 76 65 64 20  ast state saved 
33060 75 73 69 6e 67 0a 20 20 20 20 2a 2a 20 50 52 4e  using.    ** PRN
33061 47 5f 53 41 56 45 2e 20 20 49 66 20 50 52 4e 47  G_SAVE.  If PRNG
33062 5f 53 41 56 45 20 68 61 73 20 6e 65 76 65 72 20  _SAVE has never 
33063 62 65 66 6f 72 65 20 62 65 65 6e 20 63 61 6c 6c  before been call
33064 65 64 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20  ed, then.    ** 
33065 74 68 69 73 20 76 65 72 62 20 61 63 74 73 20 6c  this verb acts l
33066 69 6b 65 20 50 52 4e 47 5f 52 45 53 45 54 2e 0a  ike PRNG_RESET..
33067 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20      */.    case 
33068 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f  SQLITE_TESTCTRL_
33069 50 52 4e 47 5f 52 45 53 54 4f 52 45 3a 20 7b 0a  PRNG_RESTORE: {.
3306a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 72 6e        sqlite3Prn
3306b 67 52 65 73 74 6f 72 65 53 74 61 74 65 28 29 3b  gRestoreState();
3306c 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
3306d 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20    }..    /*.    
3306e 2a 2a 20 52 65 73 65 74 20 74 68 65 20 50 52 4e  ** Reset the PRN
3306f 47 20 62 61 63 6b 20 74 6f 20 69 74 73 20 75 6e  G back to its un
33070 69 6e 69 74 69 61 6c 69 7a 65 64 20 73 74 61 74  initialized stat
33071 65 2e 20 20 54 68 65 20 6e 65 78 74 20 63 61 6c  e.  The next cal
33072 6c 0a 20 20 20 20 2a 2a 20 74 6f 20 73 71 6c 69  l.    ** to sqli
33073 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 29  te3_randomness()
33074 20 77 69 6c 6c 20 72 65 73 65 65 64 20 74 68 65   will reseed the
33075 20 50 52 4e 47 20 75 73 69 6e 67 20 61 20 73 69   PRNG using a si
33076 6e 67 6c 65 20 63 61 6c 6c 0a 20 20 20 20 2a 2a  ngle call.    **
33077 20 74 6f 20 74 68 65 20 78 52 61 6e 64 6f 6d 6e   to the xRandomn
33078 65 73 73 20 6d 65 74 68 6f 64 20 6f 66 20 74 68  ess method of th
33079 65 20 64 65 66 61 75 6c 74 20 56 46 53 2e 0a 20  e default VFS.. 
3307a 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53     */.    case S
3307b 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 50  QLITE_TESTCTRL_P
3307c 52 4e 47 5f 52 45 53 45 54 3a 20 7b 0a 20 20 20  RNG_RESET: {.   
3307d 20 20 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65     sqlite3PrngRe
3307e 73 65 74 53 74 61 74 65 28 29 3b 0a 20 20 20 20  setState();.    
3307f 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a    break;.    }..
33080 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 20 73      /*.    **  s
33081 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74  qlite3_test_cont
33082 72 6f 6c 28 42 49 54 56 45 43 5f 54 45 53 54 2c  rol(BITVEC_TEST,
33083 20 73 69 7a 65 2c 20 70 72 6f 67 72 61 6d 29 0a   size, program).
33084 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 52 75      **.    ** Ru
33085 6e 20 61 20 74 65 73 74 20 61 67 61 69 6e 73 74  n a test against
33086 20 61 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74   a Bitvec object
33087 20 6f 66 20 73 69 7a 65 2e 20 20 54 68 65 20 70   of size.  The p
33088 72 6f 67 72 61 6d 20 61 72 67 75 6d 65 6e 74 0a  rogram argument.
33089 20 20 20 20 2a 2a 20 69 73 20 61 6e 20 61 72 72      ** is an arr
3308a 61 79 20 6f 66 20 69 6e 74 65 67 65 72 73 20 74  ay of integers t
3308b 68 61 74 20 64 65 66 69 6e 65 73 20 74 68 65 20  hat defines the 
3308c 74 65 73 74 2e 20 20 52 65 74 75 72 6e 20 2d 31  test.  Return -1
3308d 20 6f 6e 20 61 0a 20 20 20 20 2a 2a 20 6d 65 6d   on a.    ** mem
3308e 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65  ory allocation e
3308f 72 72 6f 72 2c 20 30 20 6f 6e 20 73 75 63 63 65  rror, 0 on succe
33090 73 73 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72 6f 20  ss, or non-zero 
33091 66 6f 72 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20  for an error..  
33092 20 20 2a 2a 20 53 65 65 20 74 68 65 20 73 71 6c    ** See the sql
33093 69 74 65 33 42 69 74 76 65 63 42 75 69 6c 74 69  ite3BitvecBuilti
33094 6e 54 65 73 74 28 29 20 66 6f 72 20 61 64 64 69  nTest() for addi
33095 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69  tional informati
33096 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  on..    */.    c
33097 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43  ase SQLITE_TESTC
33098 54 52 4c 5f 42 49 54 56 45 43 5f 54 45 53 54 3a  TRL_BITVEC_TEST:
33099 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 7a 20   {.      int sz 
3309a 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74  = va_arg(ap, int
3309b 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 2a 61 50  );.      int *aP
3309c 72 6f 67 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  rog = va_arg(ap,
3309d 20 69 6e 74 2a 29 3b 0a 20 20 20 20 20 20 72 63   int*);.      rc
3309e 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63   = sqlite3Bitvec
3309f 42 75 69 6c 74 69 6e 54 65 73 74 28 73 7a 2c 20  BuiltinTest(sz, 
330a0 61 50 72 6f 67 29 3b 0a 20 20 20 20 20 20 62 72  aProg);.      br
330a1 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  eak;.    }..    
330a2 2f 2a 0a 20 20 20 20 2a 2a 20 20 73 71 6c 69 74  /*.    **  sqlit
330a3 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28  e3_test_control(
330a4 42 45 4e 49 47 4e 5f 4d 41 4c 4c 4f 43 5f 48 4f  BENIGN_MALLOC_HO
330a5 4f 4b 53 2c 20 78 42 65 67 69 6e 2c 20 78 45 6e  OKS, xBegin, xEn
330a6 64 29 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  d).    **.    **
330a7 20 52 65 67 69 73 74 65 72 20 68 6f 6f 6b 73 20   Register hooks 
330a8 74 6f 20 63 61 6c 6c 20 74 6f 20 69 6e 64 69 63  to call to indic
330a9 61 74 65 20 77 68 69 63 68 20 6d 61 6c 6c 6f 63  ate which malloc
330aa 28 29 20 66 61 69 6c 75 72 65 73 20 0a 20 20 20  () failures .   
330ab 20 2a 2a 20 61 72 65 20 62 65 6e 69 67 6e 2e 0a   ** are benign..
330ac 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20      */.    case 
330ad 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f  SQLITE_TESTCTRL_
330ae 42 45 4e 49 47 4e 5f 4d 41 4c 4c 4f 43 5f 48 4f  BENIGN_MALLOC_HO
330af 4f 4b 53 3a 20 7b 0a 20 20 20 20 20 20 74 79 70  OKS: {.      typ
330b0 65 64 65 66 20 76 6f 69 64 20 28 2a 76 6f 69 64  edef void (*void
330b1 5f 66 75 6e 63 74 69 6f 6e 29 28 76 6f 69 64 29  _function)(void)
330b2 3b 0a 20 20 20 20 20 20 76 6f 69 64 5f 66 75 6e  ;.      void_fun
330b3 63 74 69 6f 6e 20 78 42 65 6e 69 67 6e 42 65 67  ction xBenignBeg
330b4 69 6e 3b 0a 20 20 20 20 20 20 76 6f 69 64 5f 66  in;.      void_f
330b5 75 6e 63 74 69 6f 6e 20 78 42 65 6e 69 67 6e 45  unction xBenignE
330b6 6e 64 3b 0a 20 20 20 20 20 20 78 42 65 6e 69 67  nd;.      xBenig
330b7 6e 42 65 67 69 6e 20 3d 20 76 61 5f 61 72 67 28  nBegin = va_arg(
330b8 61 70 2c 20 76 6f 69 64 5f 66 75 6e 63 74 69 6f  ap, void_functio
330b9 6e 29 3b 0a 20 20 20 20 20 20 78 42 65 6e 69 67  n);.      xBenig
330ba 6e 45 6e 64 20 3d 20 76 61 5f 61 72 67 28 61 70  nEnd = va_arg(ap
330bb 2c 20 76 6f 69 64 5f 66 75 6e 63 74 69 6f 6e 29  , void_function)
330bc 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  ;.      sqlite3B
330bd 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73  enignMallocHooks
330be 28 78 42 65 6e 69 67 6e 42 65 67 69 6e 2c 20 78  (xBenignBegin, x
330bf 42 65 6e 69 67 6e 45 6e 64 29 3b 0a 20 20 20 20  BenignEnd);.    
330c0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a    break;.    }..
330c1 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 20 73      /*.    **  s
330c2 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74  qlite3_test_cont
330c3 72 6f 6c 28 53 51 4c 49 54 45 5f 54 45 53 54 43  rol(SQLITE_TESTC
330c4 54 52 4c 5f 50 45 4e 44 49 4e 47 5f 42 59 54 45  TRL_PENDING_BYTE
330c5 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 58  , unsigned int X
330c6 29 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ).    **.    ** 
330c7 53 65 74 20 74 68 65 20 50 45 4e 44 49 4e 47 20  Set the PENDING 
330c8 62 79 74 65 20 74 6f 20 74 68 65 20 76 61 6c 75  byte to the valu
330c9 65 20 69 6e 20 74 68 65 20 61 72 67 75 6d 65 6e  e in the argumen
330ca 74 2c 20 69 66 20 58 3e 30 2e 0a 20 20 20 20 2a  t, if X>0..    *
330cb 2a 20 4d 61 6b 65 20 6e 6f 20 63 68 61 6e 67 65  * Make no change
330cc 73 20 69 66 20 58 3d 3d 30 2e 20 20 52 65 74 75  s if X==0.  Retu
330cd 72 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  rn the value of 
330ce 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65  the pending byte
330cf 0a 20 20 20 20 2a 2a 20 61 73 20 69 74 20 65 78  .    ** as it ex
330d0 69 73 74 69 6e 67 20 62 65 66 6f 72 65 20 74 68  isting before th
330d1 69 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20 63  is routine was c
330d2 61 6c 6c 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 20  alled..    **.  
330d3 20 20 2a 2a 20 49 4d 50 4f 52 54 41 4e 54 3a 20    ** IMPORTANT: 
330d4 20 43 68 61 6e 67 69 6e 67 20 74 68 65 20 50 45   Changing the PE
330d5 4e 44 49 4e 47 20 62 79 74 65 20 66 72 6f 6d 20  NDING byte from 
330d6 30 78 34 30 30 30 30 30 30 30 20 72 65 73 75 6c  0x40000000 resul
330d7 74 73 20 69 6e 0a 20 20 20 20 2a 2a 20 61 6e 20  ts in.    ** an 
330d8 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 64 61 74  incompatible dat
330d9 61 62 61 73 65 20 66 69 6c 65 20 66 6f 72 6d 61  abase file forma
330da 74 2e 20 20 43 68 61 6e 67 69 6e 67 20 74 68 65  t.  Changing the
330db 20 50 45 4e 44 49 4e 47 20 62 79 74 65 0a 20 20   PENDING byte.  
330dc 20 20 2a 2a 20 77 68 69 6c 65 20 61 6e 79 20 64    ** while any d
330dd 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
330de 6f 6e 20 69 73 20 6f 70 65 6e 20 72 65 73 75 6c  on is open resul
330df 74 73 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 20  ts in undefined 
330e0 61 6e 64 0a 20 20 20 20 2a 2a 20 64 69 6c 65 74  and.    ** dilet
330e1 65 72 69 6f 75 73 20 62 65 68 61 76 69 6f 72 2e  erious behavior.
330e2 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65  .    */.    case
330e3 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c   SQLITE_TESTCTRL
330e4 5f 50 45 4e 44 49 4e 47 5f 42 59 54 45 3a 20 7b  _PENDING_BYTE: {
330e5 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20  .      unsigned 
330e6 69 6e 74 20 6e 65 77 56 61 6c 20 3d 20 76 61 5f  int newVal = va_
330e7 61 72 67 28 61 70 2c 20 75 6e 73 69 67 6e 65 64  arg(ap, unsigned
330e8 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 72 63 20   int);.      rc 
330e9 3d 20 73 71 6c 69 74 65 33 50 65 6e 64 69 6e 67  = sqlite3Pending
330ea 42 79 74 65 3b 0a 20 20 20 20 20 20 69 66 28 20  Byte;.      if( 
330eb 6e 65 77 56 61 6c 20 29 20 73 71 6c 69 74 65 33  newVal ) sqlite3
330ec 50 65 6e 64 69 6e 67 42 79 74 65 20 3d 20 6e 65  PendingByte = ne
330ed 77 56 61 6c 3b 0a 20 20 20 20 20 20 62 72 65 61  wVal;.      brea
330ee 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  k;.    }..    /*
330ef 0a 20 20 20 20 2a 2a 20 20 73 71 6c 69 74 65 33  .    **  sqlite3
330f0 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 53 51  _test_control(SQ
330f1 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 41 53  LITE_TESTCTRL_AS
330f2 53 45 52 54 2c 20 69 6e 74 20 58 29 0a 20 20 20  SERT, int X).   
330f3 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 69 73 20   **.    ** This 
330f4 61 63 74 69 6f 6e 20 70 72 6f 76 69 64 65 73 20  action provides 
330f5 61 20 72 75 6e 2d 74 69 6d 65 20 74 65 73 74 20  a run-time test 
330f6 74 6f 20 73 65 65 20 77 68 65 74 68 65 72 20 6f  to see whether o
330f7 72 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 61 73 73  r not.    ** ass
330f8 65 72 74 28 29 20 77 61 73 20 65 6e 61 62 6c 65  ert() was enable
330f9 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  d at compile-tim
330fa 65 2e 20 20 49 66 20 58 20 69 73 20 74 72 75 65  e.  If X is true
330fb 20 61 6e 64 20 61 73 73 65 72 74 28 29 0a 20 20   and assert().  
330fc 20 20 2a 2a 20 69 73 20 65 6e 61 62 6c 65 64 2c    ** is enabled,
330fd 20 74 68 65 6e 20 74 68 65 20 72 65 74 75 72 6e   then the return
330fe 20 76 61 6c 75 65 20 69 73 20 74 72 75 65 2e 20   value is true. 
330ff 20 49 66 20 58 20 69 73 20 74 72 75 65 20 61 6e   If X is true an
33100 64 0a 20 20 20 20 2a 2a 20 61 73 73 65 72 74 28  d.    ** assert(
33101 29 20 69 73 20 64 69 73 61 62 6c 65 64 2c 20 74  ) is disabled, t
33102 68 65 6e 20 74 68 65 20 72 65 74 75 72 6e 20 76  hen the return v
33103 61 6c 75 65 20 69 73 20 7a 65 72 6f 2e 20 20 49  alue is zero.  I
33104 66 20 58 20 69 73 0a 20 20 20 20 2a 2a 20 66 61  f X is.    ** fa
33105 6c 73 65 20 61 6e 64 20 61 73 73 65 72 74 28 29  lse and assert()
33106 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65   is enabled, the
33107 6e 20 74 68 65 20 61 73 73 65 72 74 69 6f 6e 20  n the assertion 
33108 66 69 72 65 73 20 61 6e 64 20 74 68 65 0a 20 20  fires and the.  
33109 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 61 62 6f    ** process abo
3310a 72 74 73 2e 20 20 49 66 20 58 20 69 73 20 66 61  rts.  If X is fa
3310b 6c 73 65 20 61 6e 64 20 61 73 73 65 72 74 28 29  lse and assert()
3310c 20 69 73 20 64 69 73 61 62 6c 65 64 2c 20 74 68   is disabled, th
3310d 65 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 65  en the.    ** re
3310e 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 7a 65  turn value is ze
3310f 72 6f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  ro..    */.    c
33110 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43  ase SQLITE_TESTC
33111 54 52 4c 5f 41 53 53 45 52 54 3a 20 7b 0a 20 20  TRL_ASSERT: {.  
33112 20 20 20 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74      volatile int
33113 20 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 61 73   x = 0;.      as
33114 73 65 72 74 28 20 28 78 20 3d 20 76 61 5f 61 72  sert( (x = va_ar
33115 67 28 61 70 2c 69 6e 74 29 29 21 3d 30 20 29 3b  g(ap,int))!=0 );
33116 0a 20 20 20 20 20 20 72 63 20 3d 20 78 3b 0a 20  .      rc = x;. 
33117 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
33118 7d 0a 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a  }...    /*.    *
33119 2a 20 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f  *  sqlite3_test_
3311a 63 6f 6e 74 72 6f 6c 28 53 51 4c 49 54 45 5f 54  control(SQLITE_T
3311b 45 53 54 43 54 52 4c 5f 41 4c 57 41 59 53 2c 20  ESTCTRL_ALWAYS, 
3311c 69 6e 74 20 58 29 0a 20 20 20 20 2a 2a 0a 20 20  int X).    **.  
3311d 20 20 2a 2a 20 54 68 69 73 20 61 63 74 69 6f 6e    ** This action
3311e 20 70 72 6f 76 69 64 65 73 20 61 20 72 75 6e 2d   provides a run-
3311f 74 69 6d 65 20 74 65 73 74 20 74 6f 20 73 65 65  time test to see
33120 20 68 6f 77 20 74 68 65 20 41 4c 57 41 59 53 20   how the ALWAYS 
33121 61 6e 64 0a 20 20 20 20 2a 2a 20 4e 45 56 45 52  and.    ** NEVER
33122 20 6d 61 63 72 6f 73 20 77 65 72 65 20 64 65 66   macros were def
33123 69 6e 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d  ined at compile-
33124 74 69 6d 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  time..    **.   
33125 20 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76   ** The return v
33126 61 6c 75 65 20 69 73 20 41 4c 57 41 59 53 28 58  alue is ALWAYS(X
33127 29 2e 20 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ).  .    **.    
33128 2a 2a 20 54 68 65 20 72 65 63 6f 6d 6d 65 6e 64  ** The recommend
33129 65 64 20 74 65 73 74 20 69 73 20 58 3d 3d 32 2e  ed test is X==2.
3312a 20 20 49 66 20 74 68 65 20 72 65 74 75 72 6e 20    If the return 
3312b 76 61 6c 75 65 20 69 73 20 32 2c 20 74 68 61 74  value is 2, that
3312c 20 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 41 4c   means.    ** AL
3312d 57 41 59 53 28 29 20 61 6e 64 20 4e 45 56 45 52  WAYS() and NEVER
3312e 28 29 20 61 72 65 20 62 6f 74 68 20 6e 6f 2d 6f  () are both no-o
3312f 70 20 70 61 73 73 2d 74 68 72 6f 75 67 68 20 6d  p pass-through m
33130 61 63 72 6f 73 2c 20 77 68 69 63 68 20 69 73 20  acros, which is 
33131 74 68 65 0a 20 20 20 20 2a 2a 20 64 65 66 61 75  the.    ** defau
33132 6c 74 20 73 65 74 74 69 6e 67 2e 20 20 49 66 20  lt setting.  If 
33133 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  the return value
33134 20 69 73 20 31 2c 20 74 68 65 6e 20 41 4c 57 41   is 1, then ALWA
33135 59 53 28 29 20 69 73 20 65 69 74 68 65 72 0a 20  YS() is either. 
33136 20 20 20 2a 2a 20 68 61 72 64 2d 63 6f 64 65 64     ** hard-coded
33137 20 74 6f 20 74 72 75 65 20 6f 72 20 65 6c 73 65   to true or else
33138 20 69 74 20 61 73 73 65 72 74 73 20 69 66 20 69   it asserts if i
33139 74 73 20 61 72 67 75 6d 65 6e 74 20 69 73 20 66  ts argument is f
3313a 61 6c 73 65 2e 0a 20 20 20 20 2a 2a 20 54 68 65  alse..    ** The
3313b 20 66 69 72 73 74 20 62 65 68 61 76 69 6f 72 20   first behavior 
3313c 28 68 61 72 64 2d 63 6f 64 65 64 20 74 6f 20 74  (hard-coded to t
3313d 72 75 65 29 20 69 73 20 74 68 65 20 63 61 73 65  rue) is the case
3313e 20 69 66 0a 20 20 20 20 2a 2a 20 53 51 4c 49 54   if.    ** SQLIT
3313f 45 5f 54 45 53 54 43 54 52 4c 5f 41 53 53 45 52  E_TESTCTRL_ASSER
33140 54 20 73 68 6f 77 73 20 74 68 61 74 20 61 73 73  T shows that ass
33141 65 72 74 28 29 20 69 73 20 64 69 73 61 62 6c 65  ert() is disable
33142 64 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64  d and the second
33143 0a 20 20 20 20 2a 2a 20 62 65 68 61 76 69 6f 72  .    ** behavior
33144 20 28 61 73 73 65 72 74 20 69 66 20 74 68 65 20   (assert if the 
33145 61 72 67 75 6d 65 6e 74 20 74 6f 20 41 4c 57 41  argument to ALWA
33146 59 53 28 29 20 69 73 20 66 61 6c 73 65 29 20 69  YS() is false) i
33147 73 20 74 68 65 20 63 61 73 65 20 69 66 0a 20 20  s the case if.  
33148 20 20 2a 2a 20 53 51 4c 49 54 45 5f 54 45 53 54    ** SQLITE_TEST
33149 43 54 52 4c 5f 41 53 53 45 52 54 20 73 68 6f 77  CTRL_ASSERT show
3314a 73 20 74 68 61 74 20 61 73 73 65 72 74 28 29 20  s that assert() 
3314b 69 73 20 65 6e 61 62 6c 65 64 2e 0a 20 20 20 20  is enabled..    
3314c 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 75  **.    ** The ru
3314d 6e 2d 74 69 6d 65 20 74 65 73 74 20 70 72 6f 63  n-time test proc
3314e 65 64 75 72 65 20 6d 69 67 68 74 20 6c 6f 6f 6b  edure might look
3314f 20 73 6f 6d 65 74 68 69 6e 67 20 6c 69 6b 65 20   something like 
33150 74 68 69 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20  this:.    **.   
33151 20 2a 2a 20 20 20 20 69 66 28 20 73 71 6c 69 74   **    if( sqlit
33152 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28  e3_test_control(
33153 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f  SQLITE_TESTCTRL_
33154 41 4c 57 41 59 53 2c 20 32 29 3d 3d 32 20 29 7b  ALWAYS, 2)==2 ){
33155 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 2f 2f 20  .    **      // 
33156 41 4c 57 41 59 53 28 29 20 61 6e 64 20 4e 45 56  ALWAYS() and NEV
33157 45 52 28 29 20 61 72 65 20 6e 6f 2d 6f 70 20 70  ER() are no-op p
33158 61 73 73 2d 74 68 72 6f 75 67 68 20 6d 61 63 72  ass-through macr
33159 6f 73 0a 20 20 20 20 2a 2a 20 20 20 20 7d 65 6c  os.    **    }el
3315a 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 5f 74  se if( sqlite3_t
3315b 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 53 51 4c 49  est_control(SQLI
3315c 54 45 5f 54 45 53 54 43 54 52 4c 5f 41 53 53 45  TE_TESTCTRL_ASSE
3315d 52 54 2c 20 31 29 20 29 7b 0a 20 20 20 20 2a 2a  RT, 1) ){.    **
3315e 20 20 20 20 20 20 2f 2f 20 41 4c 57 41 59 53 28        // ALWAYS(
3315f 78 29 20 61 73 73 65 72 74 73 20 74 68 61 74 20  x) asserts that 
33160 78 20 69 73 20 74 72 75 65 2e 20 4e 45 56 45 52  x is true. NEVER
33161 28 78 29 20 61 73 73 65 72 74 73 20 78 20 69 73  (x) asserts x is
33162 20 66 61 6c 73 65 2e 0a 20 20 20 20 2a 2a 20 20   false..    **  
33163 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 2a 20    }else{.    ** 
33164 20 20 20 20 20 2f 2f 20 41 4c 57 41 59 53 28 78       // ALWAYS(x
33165 29 20 69 73 20 61 20 63 6f 6e 73 74 61 6e 74 20  ) is a constant 
33166 31 2e 20 20 4e 45 56 45 52 28 78 29 20 69 73 20  1.  NEVER(x) is 
33167 61 20 63 6f 6e 73 74 61 6e 74 20 30 2e 0a 20 20  a constant 0..  
33168 20 20 2a 2a 20 20 20 20 7d 0a 20 20 20 20 2a 2f    **    }.    */
33169 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
3316a 5f 54 45 53 54 43 54 52 4c 5f 41 4c 57 41 59 53  _TESTCTRL_ALWAYS
3316b 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 78 20  : {.      int x 
3316c 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 29  = va_arg(ap,int)
3316d 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 41 4c 57  ;.      rc = ALW
3316e 41 59 53 28 78 29 3b 0a 20 20 20 20 20 20 62 72  AYS(x);.      br
3316f 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  eak;.    }..    
33170 2f 2a 20 20 20 73 71 6c 69 74 65 33 5f 74 65 73  /*   sqlite3_tes
33171 74 5f 63 6f 6e 74 72 6f 6c 28 53 51 4c 49 54 45  t_control(SQLITE
33172 5f 54 45 53 54 43 54 52 4c 5f 52 45 53 45 52 56  _TESTCTRL_RESERV
33173 45 2c 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  E, sqlite3 *db, 
33174 69 6e 74 20 4e 29 0a 20 20 20 20 2a 2a 0a 20 20  int N).    **.  
33175 20 20 2a 2a 20 53 65 74 20 74 68 65 20 6e 52 65    ** Set the nRe
33176 73 65 72 76 65 20 73 69 7a 65 20 74 6f 20 4e 20  serve size to N 
33177 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74  for the main dat
33178 61 62 61 73 65 20 6f 6e 20 74 68 65 20 64 61 74  abase on the dat
33179 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 63 6f 6e  abase.    ** con
3317a 6e 65 63 74 69 6f 6e 20 64 62 2e 0a 20 20 20 20  nection db..    
3317b 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  */.    case SQLI
3317c 54 45 5f 54 45 53 54 43 54 52 4c 5f 52 45 53 45  TE_TESTCTRL_RESE
3317d 52 56 45 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c  RVE: {.      sql
3317e 69 74 65 33 20 2a 64 62 20 3d 20 76 61 5f 61 72  ite3 *db = va_ar
3317f 67 28 61 70 2c 20 73 71 6c 69 74 65 33 2a 29 3b  g(ap, sqlite3*);
33180 0a 20 20 20 20 20 20 69 6e 74 20 78 20 3d 20 76  .      int x = v
33181 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20  a_arg(ap,int);. 
33182 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74       sqlite3_mut
33183 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
33184 65 78 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ex);.      sqlit
33185 65 33 42 74 72 65 65 53 65 74 50 61 67 65 53 69  e3BtreeSetPageSi
33186 7a 65 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42  ze(db->aDb[0].pB
33187 74 2c 20 30 2c 20 78 2c 20 30 29 3b 0a 20 20 20  t, 0, x, 0);.   
33188 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
33189 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
3318a 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
3318b 20 20 20 20 7d 0a 0a 20 20 7d 0a 20 20 76 61 5f      }..  }.  va_
3318c 65 6e 64 28 61 70 29 3b 0a 23 65 6e 64 69 66 20  end(ap);.#endif 
3318d 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42  /* SQLITE_OMIT_B
3318e 55 49 4c 54 49 4e 5f 54 45 53 54 20 2a 2f 0a 20  UILTIN_TEST */. 
3318f 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
33190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
33191 6e 64 20 6f 66 20 6d 61 69 6e 2e 63 20 2a 2a 2a  nd of main.c ***
33192 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33193 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33194 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
33195 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
33196 65 67 69 6e 20 66 69 6c 65 20 6e 6f 74 69 66 79  egin file notify
33197 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
33198 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33199 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
3319a 2a 0a 2a 2a 20 32 30 30 39 20 4d 61 72 63 68 20  *.** 2009 March 
3319b 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  3.**.** The auth
3319c 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
3319d 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
3319e 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
3319f 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
331a0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
331a1 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
331a2 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
331a3 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
331a4 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
331a5 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
331a6 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
331a7 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
331a8 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
331a9 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
331aa 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
331ab 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
331ac 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
331ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
331ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
331af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
331b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
331b1 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
331b2 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 69 6d   contains the im
331b3 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
331b4 74 68 65 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f  the sqlite3_unlo
331b5 63 6b 5f 6e 6f 74 69 66 79 28 29 0a 2a 2a 20 41  ck_notify().** A
331b6 50 49 20 6d 65 74 68 6f 64 20 61 6e 64 20 69 74  PI method and it
331b7 73 20 61 73 73 6f 63 69 61 74 65 64 20 66 75 6e  s associated fun
331b8 63 74 69 6f 6e 61 6c 69 74 79 2e 0a 2a 2a 0a 2a  ctionality..**.*
331b9 2a 20 24 49 64 3a 20 6e 6f 74 69 66 79 2e 63 2c  * $Id: notify.c,
331ba 76 20 31 2e 34 20 32 30 30 39 2f 30 34 2f 30 37  v 1.4 2009/04/07
331bb 20 32 32 3a 30 36 3a 35 37 20 64 72 68 20 45 78   22:06:57 drh Ex
331bc 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 4f 6d 69 74 20  p $.*/../* Omit 
331bd 74 68 69 73 20 65 6e 74 69 72 65 20 66 69 6c 65  this entire file
331be 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   if SQLITE_ENABL
331bf 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 20  E_UNLOCK_NOTIFY 
331c0 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 2e 20  is not defined. 
331c1 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
331c2 5f 45 4e 41 42 4c 45 5f 55 4e 4c 4f 43 4b 5f 4e  _ENABLE_UNLOCK_N
331c3 4f 54 49 46 59 0a 0a 2f 2a 0a 2a 2a 20 50 75 62  OTIFY../*.** Pub
331c4 6c 69 63 20 69 6e 74 65 72 66 61 63 65 73 3a 0a  lic interfaces:.
331c5 2a 2a 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 43  **.**   sqlite3C
331c6 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64  onnectionBlocked
331c7 28 29 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 43  ().**   sqlite3C
331c8 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65  onnectionUnlocke
331c9 64 28 29 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33  d().**   sqlite3
331ca 43 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64  ConnectionClosed
331cb 28 29 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 5f  ().**   sqlite3_
331cc 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 0a  unlock_notify().
331cd 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 61 73 73 65  */..#define asse
331ce 72 74 4d 75 74 65 78 48 65 6c 64 28 29 20 5c 0a  rtMutexHeld() \.
331cf 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
331d0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c  3_mutex_held(sql
331d1 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53  ite3MutexAlloc(S
331d2 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
331d3 49 43 5f 4d 41 53 54 45 52 29 29 20 29 0a 0a 2f  IC_MASTER)) )../
331d4 2a 0a 2a 2a 20 48 65 61 64 20 6f 66 20 61 20 6c  *.** Head of a l
331d5 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 6c  inked list of al
331d6 6c 20 73 71 6c 69 74 65 33 20 6f 62 6a 65 63 74  l sqlite3 object
331d7 73 20 63 72 65 61 74 65 64 20 62 79 20 74 68 69  s created by thi
331d8 73 20 70 72 6f 63 65 73 73 0a 2a 2a 20 66 6f 72  s process.** for
331d9 20 77 68 69 63 68 20 65 69 74 68 65 72 20 73 71   which either sq
331da 6c 69 74 65 33 2e 70 42 6c 6f 63 6b 69 6e 67 43  lite3.pBlockingC
331db 6f 6e 6e 65 63 74 69 6f 6e 20 6f 72 20 73 71 6c  onnection or sql
331dc 69 74 65 33 2e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e  ite3.pUnlockConn
331dd 65 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 6e 6f 74  ection.** is not
331de 20 4e 55 4c 4c 2e 20 54 68 69 73 20 76 61 72 69   NULL. This vari
331df 61 62 6c 65 20 6d 61 79 20 6f 6e 6c 79 20 61 63  able may only ac
331e0 63 65 73 73 65 64 20 77 68 69 6c 65 20 74 68 65  cessed while the
331e1 20 53 54 41 54 49 43 5f 4d 41 53 54 45 52 0a 2a   STATIC_MASTER.*
331e2 2a 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 2e  * mutex is held.
331e3 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74  .*/.static sqlit
331e4 65 33 20 2a 53 51 4c 49 54 45 5f 57 53 44 20 73  e3 *SQLITE_WSD s
331e5 71 6c 69 74 65 33 42 6c 6f 63 6b 65 64 4c 69 73  qlite3BlockedLis
331e6 74 20 3d 20 30 3b 0a 0a 23 69 66 6e 64 65 66 20  t = 0;..#ifndef 
331e7 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 69  NDEBUG./*.** Thi
331e8 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20  s function is a 
331e9 63 6f 6d 70 6c 65 78 20 61 73 73 65 72 74 28 29  complex assert()
331ea 20 74 68 61 74 20 76 65 72 69 66 69 65 73 20 74   that verifies t
331eb 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a  he following .**
331ec 20 70 72 6f 70 65 72 74 69 65 73 20 6f 66 20 74   properties of t
331ed 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65  he blocked conne
331ee 63 74 69 6f 6e 73 20 6c 69 73 74 3a 0a 2a 2a 0a  ctions list:.**.
331ef 2a 2a 20 20 20 31 29 20 45 61 63 68 20 65 6e 74  **   1) Each ent
331f0 72 79 20 69 6e 20 74 68 65 20 6c 69 73 74 20 68  ry in the list h
331f1 61 73 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 76 61  as a non-NULL va
331f2 6c 75 65 20 66 6f 72 20 65 69 74 68 65 72 20 0a  lue for either .
331f3 2a 2a 20 20 20 20 20 20 70 55 6e 6c 6f 63 6b 43  **      pUnlockC
331f4 6f 6e 6e 65 63 74 69 6f 6e 20 6f 72 20 70 42 6c  onnection or pBl
331f5 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e  ockingConnection
331f6 2c 20 6f 72 20 62 6f 74 68 2e 0a 2a 2a 0a 2a 2a  , or both..**.**
331f7 20 20 20 32 29 20 41 6c 6c 20 65 6e 74 72 69 65     2) All entrie
331f8 73 20 69 6e 20 74 68 65 20 6c 69 73 74 20 74 68  s in the list th
331f9 61 74 20 73 68 61 72 65 20 61 20 63 6f 6d 6d 6f  at share a commo
331fa 6e 20 76 61 6c 75 65 20 66 6f 72 20 0a 2a 2a 20  n value for .** 
331fb 20 20 20 20 20 78 55 6e 6c 6f 63 6b 4e 6f 74 69       xUnlockNoti
331fc 66 79 20 61 72 65 20 67 72 6f 75 70 65 64 20 74  fy are grouped t
331fd 6f 67 65 74 68 65 72 2e 0a 2a 2a 0a 2a 2a 20 20  ogether..**.**  
331fe 20 33 29 20 49 66 20 74 68 65 20 61 72 67 75 6d   3) If the argum
331ff 65 6e 74 20 64 62 20 69 73 20 6e 6f 74 20 4e 55  ent db is not NU
33200 4c 4c 2c 20 74 68 65 6e 20 6e 6f 6e 65 20 6f 66  LL, then none of
33201 20 74 68 65 20 65 6e 74 72 69 65 73 20 69 6e 20   the entries in 
33202 74 68 65 0a 2a 2a 20 20 20 20 20 20 62 6c 6f 63  the.**      bloc
33203 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  ked connections 
33204 6c 69 73 74 20 68 61 76 65 20 70 55 6e 6c 6f 63  list have pUnloc
33205 6b 43 6f 6e 6e 65 63 74 69 6f 6e 20 6f 72 20 70  kConnection or p
33206 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69  BlockingConnecti
33207 6f 6e 0a 2a 2a 20 20 20 20 20 20 73 65 74 20 74  on.**      set t
33208 6f 20 64 62 2e 20 54 68 69 73 20 69 73 20 75 73  o db. This is us
33209 65 64 20 77 68 65 6e 20 63 6c 6f 73 69 6e 67 20  ed when closing 
3320a 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 2e 0a 2a  connection db..*
3320b 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 68  /.static void ch
3320c 65 63 6b 4c 69 73 74 50 72 6f 70 65 72 74 69 65  eckListPropertie
3320d 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  s(sqlite3 *db){.
3320e 20 20 73 71 6c 69 74 65 33 20 2a 70 3b 0a 20 20    sqlite3 *p;.  
3320f 66 6f 72 28 70 3d 73 71 6c 69 74 65 33 42 6c 6f  for(p=sqlite3Blo
33210 63 6b 65 64 4c 69 73 74 3b 20 70 3b 20 70 3d 70  ckedList; p; p=p
33211 2d 3e 70 4e 65 78 74 42 6c 6f 63 6b 65 64 29 7b  ->pNextBlocked){
33212 0a 20 20 20 20 69 6e 74 20 73 65 65 6e 20 3d 20  .    int seen = 
33213 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a  0;.    sqlite3 *
33214 70 32 3b 0a 0a 20 20 20 20 2f 2a 20 56 65 72 69  p2;..    /* Veri
33215 66 79 20 70 72 6f 70 65 72 74 79 20 28 31 29 20  fy property (1) 
33216 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  */.    assert( p
33217 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74  ->pUnlockConnect
33218 69 6f 6e 20 7c 7c 20 70 2d 3e 70 42 6c 6f 63 6b  ion || p->pBlock
33219 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 20 29 3b  ingConnection );
3321a 0a 0a 20 20 20 20 2f 2a 20 56 65 72 69 66 79 20  ..    /* Verify 
3321b 70 72 6f 70 65 72 74 79 20 28 32 29 20 2a 2f 0a  property (2) */.
3321c 20 20 20 20 66 6f 72 28 70 32 3d 73 71 6c 69 74      for(p2=sqlit
3321d 65 33 42 6c 6f 63 6b 65 64 4c 69 73 74 3b 20 70  e3BlockedList; p
3321e 32 21 3d 70 3b 20 70 32 3d 70 32 2d 3e 70 4e 65  2!=p; p2=p2->pNe
3321f 78 74 42 6c 6f 63 6b 65 64 29 7b 0a 20 20 20 20  xtBlocked){.    
33220 20 20 69 66 28 20 70 32 2d 3e 78 55 6e 6c 6f 63    if( p2->xUnloc
33221 6b 4e 6f 74 69 66 79 3d 3d 70 2d 3e 78 55 6e 6c  kNotify==p->xUnl
33222 6f 63 6b 4e 6f 74 69 66 79 20 29 20 73 65 65 6e  ockNotify ) seen
33223 20 3d 20 31 3b 0a 20 20 20 20 20 20 61 73 73 65   = 1;.      asse
33224 72 74 28 20 70 32 2d 3e 78 55 6e 6c 6f 63 6b 4e  rt( p2->xUnlockN
33225 6f 74 69 66 79 3d 3d 70 2d 3e 78 55 6e 6c 6f 63  otify==p->xUnloc
33226 6b 4e 6f 74 69 66 79 20 7c 7c 20 21 73 65 65 6e  kNotify || !seen
33227 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
33228 28 20 64 62 3d 3d 30 20 7c 7c 20 70 2d 3e 70 55  ( db==0 || p->pU
33229 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 21  nlockConnection!
3322a 3d 64 62 20 29 3b 0a 20 20 20 20 20 20 61 73 73  =db );.      ass
3322b 65 72 74 28 20 64 62 3d 3d 30 20 7c 7c 20 70 2d  ert( db==0 || p-
3322c 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63  >pBlockingConnec
3322d 74 69 6f 6e 21 3d 64 62 20 29 3b 0a 20 20 20 20  tion!=db );.    
3322e 7d 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 23 20  }.  }.}.#else.# 
3322f 64 65 66 69 6e 65 20 63 68 65 63 6b 4c 69 73 74  define checkList
33230 50 72 6f 70 65 72 74 69 65 73 28 78 29 0a 23 65  Properties(x).#e
33231 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f  ndif../*.** Remo
33232 76 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62  ve connection db
33233 20 66 72 6f 6d 20 74 68 65 20 62 6c 6f 63 6b 65   from the blocke
33234 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6c 69  d connections li
33235 73 74 2e 20 49 66 20 63 6f 6e 6e 65 63 74 69 6f  st. If connectio
33236 6e 0a 2a 2a 20 64 62 20 69 73 20 6e 6f 74 20 63  n.** db is not c
33237 75 72 72 65 6e 74 6c 79 20 61 20 70 61 72 74 20  urrently a part 
33238 6f 66 20 74 68 65 20 6c 69 73 74 2c 20 74 68 69  of the list, thi
33239 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20  s function is a 
3323a 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63  no-op..*/.static
3323b 20 76 6f 69 64 20 72 65 6d 6f 76 65 46 72 6f 6d   void removeFrom
3323c 42 6c 6f 63 6b 65 64 4c 69 73 74 28 73 71 6c 69  BlockedList(sqli
3323d 74 65 33 20 2a 64 62 29 7b 0a 20 20 73 71 6c 69  te3 *db){.  sqli
3323e 74 65 33 20 2a 2a 70 70 3b 0a 20 20 61 73 73 65  te3 **pp;.  asse
3323f 72 74 4d 75 74 65 78 48 65 6c 64 28 29 3b 0a 20  rtMutexHeld();. 
33240 20 66 6f 72 28 70 70 3d 26 73 71 6c 69 74 65 33   for(pp=&sqlite3
33241 42 6c 6f 63 6b 65 64 4c 69 73 74 3b 20 2a 70 70  BlockedList; *pp
33242 3b 20 70 70 20 3d 20 26 28 2a 70 70 29 2d 3e 70  ; pp = &(*pp)->p
33243 4e 65 78 74 42 6c 6f 63 6b 65 64 29 7b 0a 20 20  NextBlocked){.  
33244 20 20 69 66 28 20 2a 70 70 3d 3d 64 62 20 29 7b    if( *pp==db ){
33245 0a 20 20 20 20 20 20 2a 70 70 20 3d 20 28 2a 70  .      *pp = (*p
33246 70 29 2d 3e 70 4e 65 78 74 42 6c 6f 63 6b 65 64  p)->pNextBlocked
33247 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
33248 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
33249 2a 20 41 64 64 20 63 6f 6e 6e 65 63 74 69 6f 6e  * Add connection
3324a 20 64 62 20 74 6f 20 74 68 65 20 62 6c 6f 63 6b   db to the block
3324b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6c  ed connections l
3324c 69 73 74 2e 20 49 74 20 69 73 20 61 73 73 75 6d  ist. It is assum
3324d 65 64 0a 2a 2a 20 74 68 61 74 20 69 74 20 69 73  ed.** that it is
3324e 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 61 20 70   not already a p
3324f 61 72 74 20 6f 66 20 74 68 65 20 6c 69 73 74 2e  art of the list.
33250 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
33251 61 64 64 54 6f 42 6c 6f 63 6b 65 64 4c 69 73 74  addToBlockedList
33252 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
33253 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 3b 0a 20   sqlite3 **pp;. 
33254 20 61 73 73 65 72 74 4d 75 74 65 78 48 65 6c 64   assertMutexHeld
33255 28 29 3b 0a 20 20 66 6f 72 28 0a 20 20 20 20 70  ();.  for(.    p
33256 70 3d 26 73 71 6c 69 74 65 33 42 6c 6f 63 6b 65  p=&sqlite3Blocke
33257 64 4c 69 73 74 3b 20 0a 20 20 20 20 2a 70 70 20  dList; .    *pp 
33258 26 26 20 28 2a 70 70 29 2d 3e 78 55 6e 6c 6f 63  && (*pp)->xUnloc
33259 6b 4e 6f 74 69 66 79 21 3d 64 62 2d 3e 78 55 6e  kNotify!=db->xUn
3325a 6c 6f 63 6b 4e 6f 74 69 66 79 3b 20 0a 20 20 20  lockNotify; .   
3325b 20 70 70 3d 26 28 2a 70 70 29 2d 3e 70 4e 65 78   pp=&(*pp)->pNex
3325c 74 42 6c 6f 63 6b 65 64 0a 20 20 29 3b 0a 20 20  tBlocked.  );.  
3325d 64 62 2d 3e 70 4e 65 78 74 42 6c 6f 63 6b 65 64  db->pNextBlocked
3325e 20 3d 20 2a 70 70 3b 0a 20 20 2a 70 70 20 3d 20   = *pp;.  *pp = 
3325f 64 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74  db;.}../*.** Obt
33260 61 69 6e 20 74 68 65 20 53 54 41 54 49 43 5f 4d  ain the STATIC_M
33261 41 53 54 45 52 20 6d 75 74 65 78 2e 0a 2a 2f 0a  ASTER mutex..*/.
33262 73 74 61 74 69 63 20 76 6f 69 64 20 65 6e 74 65  static void ente
33263 72 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20  rMutex(void){.  
33264 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
33265 74 65 72 28 73 71 6c 69 74 65 33 4d 75 74 65 78  ter(sqlite3Mutex
33266 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
33267 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
33268 29 29 3b 0a 20 20 63 68 65 63 6b 4c 69 73 74 50  ));.  checkListP
33269 72 6f 70 65 72 74 69 65 73 28 30 29 3b 0a 7d 0a  roperties(0);.}.
3326a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 74  ./*.** Release t
3326b 68 65 20 53 54 41 54 49 43 5f 4d 41 53 54 45 52  he STATIC_MASTER
3326c 20 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 61 74 69   mutex..*/.stati
3326d 63 20 76 6f 69 64 20 6c 65 61 76 65 4d 75 74 65  c void leaveMute
3326e 78 28 76 6f 69 64 29 7b 0a 20 20 61 73 73 65 72  x(void){.  asser
3326f 74 4d 75 74 65 78 48 65 6c 64 28 29 3b 0a 20 20  tMutexHeld();.  
33270 63 68 65 63 6b 4c 69 73 74 50 72 6f 70 65 72 74  checkListPropert
33271 69 65 73 28 30 29 3b 0a 20 20 73 71 6c 69 74 65  ies(0);.  sqlite
33272 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71  3_mutex_leave(sq
33273 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
33274 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
33275 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 7d 0a  TIC_MASTER));.}.
33276 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20  ./*.** Register 
33277 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79  an unlock-notify
33278 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a   callback..**.**
33279 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20   This is called 
3327a 61 66 74 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e  after connection
3327b 20 22 64 62 22 20 68 61 73 20 61 74 74 65 6d 70   "db" has attemp
3327c 74 65 64 20 73 6f 6d 65 20 6f 70 65 72 61 74 69  ted some operati
3327d 6f 6e 0a 2a 2a 20 62 75 74 20 68 61 73 20 72 65  on.** but has re
3327e 63 65 69 76 65 64 20 61 6e 20 53 51 4c 49 54 45  ceived an SQLITE
3327f 5f 4c 4f 43 4b 45 44 20 65 72 72 6f 72 20 62 65  _LOCKED error be
33280 63 61 75 73 65 20 61 6e 6f 74 68 65 72 20 63 6f  cause another co
33281 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 28 63 61 6c  nnection.** (cal
33282 6c 20 69 74 20 70 4f 74 68 65 72 29 20 69 6e 20  l it pOther) in 
33283 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73  the same process
33284 20 77 61 73 20 62 75 73 79 20 75 73 69 6e 67 20   was busy using 
33285 74 68 65 20 73 61 6d 65 20 73 68 61 72 65 64 0a  the same shared.
33286 2a 2a 20 63 61 63 68 65 2e 20 20 70 4f 74 68 65  ** cache.  pOthe
33287 72 20 69 73 20 66 6f 75 6e 64 20 62 79 20 6c 6f  r is found by lo
33288 6f 6b 69 6e 67 20 61 74 20 64 62 2d 3e 70 42 6c  oking at db->pBl
33289 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e  ockingConnection
3328a 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65  ..**.** If there
3328b 20 69 73 20 6e 6f 20 62 6c 6f 63 6b 69 6e 67 20   is no blocking 
3328c 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 74 68 65 20  connection, the 
3328d 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f  callback is invo
3328e 6b 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2c  ked immediately,
3328f 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 69 73 20  .** before this 
33290 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 2e  routine returns.
33291 0a 2a 2a 0a 2a 2a 20 49 66 20 70 4f 74 68 65 72  .**.** If pOther
33292 20 69 73 20 61 6c 72 65 61 64 79 20 62 6c 6f 63   is already bloc
33293 6b 65 64 20 6f 6e 20 64 62 2c 20 74 68 65 6e 20  ked on db, then 
33294 72 65 70 6f 72 74 20 53 51 4c 49 54 45 5f 4c 4f  report SQLITE_LO
33295 43 4b 45 44 2c 20 74 6f 20 69 6e 64 69 63 61 74  CKED, to indicat
33296 65 0a 2a 2a 20 61 20 64 65 61 64 6c 6f 63 6b 2e  e.** a deadlock.
33297 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65  .**.** Otherwise
33298 2c 20 6d 61 6b 65 20 61 72 72 61 6e 67 65 6d 65  , make arrangeme
33299 6e 74 73 20 74 6f 20 69 6e 76 6f 6b 65 20 78 4e  nts to invoke xN
3329a 6f 74 69 66 79 20 77 68 65 6e 20 70 4f 74 68 65  otify when pOthe
3329b 72 20 64 72 6f 70 73 0a 2a 2a 20 69 74 73 20 6c  r drops.** its l
3329c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68  ocks..**.** Each
3329d 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 20 72 6f   call to this ro
3329e 75 74 69 6e 65 20 6f 76 65 72 72 69 64 65 73 20  utine overrides 
3329f 61 6e 79 20 70 72 69 6f 72 20 63 61 6c 6c 62 61  any prior callba
332a0 63 6b 73 20 72 65 67 69 73 74 65 72 65 64 0a 2a  cks registered.*
332a1 2a 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 22 64  * on the same "d
332a2 62 22 2e 20 20 49 66 20 78 4e 6f 74 69 66 79 3d  b".  If xNotify=
332a3 3d 30 20 74 68 65 6e 20 61 6e 79 20 70 72 69 6f  =0 then any prio
332a4 72 20 63 61 6c 6c 62 61 63 6b 73 20 61 72 65 20  r callbacks are 
332a5 69 6d 6d 65 64 69 61 74 65 6c 79 0a 2a 2a 20 63  immediately.** c
332a6 61 6e 63 65 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c  ancelled..*/.SQL
332a7 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
332a8 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66  te3_unlock_notif
332a9 79 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  y(.  sqlite3 *db
332aa 2c 0a 20 20 76 6f 69 64 20 28 2a 78 4e 6f 74 69  ,.  void (*xNoti
332ab 66 79 29 28 76 6f 69 64 20 2a 2a 2c 20 69 6e 74  fy)(void **, int
332ac 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 0a  ),.  void *pArg.
332ad 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
332ae 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 73 71 6c 69  LITE_OK;..  sqli
332af 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
332b0 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 65 6e  db->mutex);.  en
332b1 74 65 72 4d 75 74 65 78 28 29 3b 0a 0a 20 20 69  terMutex();..  i
332b2 66 28 20 78 4e 6f 74 69 66 79 3d 3d 30 20 29 7b  f( xNotify==0 ){
332b3 0a 20 20 20 20 72 65 6d 6f 76 65 46 72 6f 6d 42  .    removeFromB
332b4 6c 6f 63 6b 65 64 4c 69 73 74 28 64 62 29 3b 0a  lockedList(db);.
332b5 20 20 20 20 64 62 2d 3e 70 55 6e 6c 6f 63 6b 43      db->pUnlockC
332b6 6f 6e 6e 65 63 74 69 6f 6e 20 3d 20 30 3b 0a 20  onnection = 0;. 
332b7 20 20 20 64 62 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f     db->xUnlockNo
332b8 74 69 66 79 20 3d 20 30 3b 0a 20 20 20 20 64 62  tify = 0;.    db
332b9 2d 3e 70 55 6e 6c 6f 63 6b 41 72 67 20 3d 20 30  ->pUnlockArg = 0
332ba 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 30 3d  ;.  }else if( 0=
332bb 3d 64 62 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f  =db->pBlockingCo
332bc 6e 6e 65 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20  nnection ){.    
332bd 2f 2a 20 54 68 65 20 62 6c 6f 63 6b 69 6e 67 20  /* The blocking 
332be 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 20  transaction has 
332bf 62 65 65 6e 20 63 6f 6e 63 6c 75 64 65 64 2e 20  been concluded. 
332c0 4f 72 20 74 68 65 72 65 20 6e 65 76 65 72 20 77  Or there never w
332c1 61 73 20 61 20 0a 20 20 20 20 2a 2a 20 62 6c 6f  as a .    ** blo
332c2 63 6b 69 6e 67 20 74 72 61 6e 73 61 63 74 69 6f  cking transactio
332c3 6e 2e 20 49 6e 20 65 69 74 68 65 72 20 63 61 73  n. In either cas
332c4 65 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 6e 6f  e, invoke the no
332c5 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 0a 20 20  tify callback.  
332c6 20 20 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79    ** immediately
332c7 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 78 4e  . .    */.    xN
332c8 6f 74 69 66 79 28 26 70 41 72 67 2c 20 31 29 3b  otify(&pArg, 1);
332c9 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
332ca 6c 69 74 65 33 20 2a 70 3b 0a 0a 20 20 20 20 66  lite3 *p;..    f
332cb 6f 72 28 70 3d 64 62 2d 3e 70 42 6c 6f 63 6b 69  or(p=db->pBlocki
332cc 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 3b 20 70 20  ngConnection; p 
332cd 26 26 20 70 21 3d 64 62 3b 20 70 3d 70 2d 3e 70  && p!=db; p=p->p
332ce 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e  UnlockConnection
332cf 29 7b 7d 0a 20 20 20 20 69 66 28 20 70 20 29 7b  ){}.    if( p ){
332d0 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
332d1 54 45 5f 4c 4f 43 4b 45 44 3b 20 20 20 20 20 20  TE_LOCKED;      
332d2 20 20 20 20 20 20 20 20 2f 2a 20 44 65 61 64 6c          /* Deadl
332d3 6f 63 6b 20 64 65 74 65 63 74 65 64 2e 20 2a 2f  ock detected. */
332d4 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
332d5 20 20 64 62 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e    db->pUnlockCon
332d6 6e 65 63 74 69 6f 6e 20 3d 20 64 62 2d 3e 70 42  nection = db->pB
332d7 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f  lockingConnectio
332d8 6e 3b 0a 20 20 20 20 20 20 64 62 2d 3e 78 55 6e  n;.      db->xUn
332d9 6c 6f 63 6b 4e 6f 74 69 66 79 20 3d 20 78 4e 6f  lockNotify = xNo
332da 74 69 66 79 3b 0a 20 20 20 20 20 20 64 62 2d 3e  tify;.      db->
332db 70 55 6e 6c 6f 63 6b 41 72 67 20 3d 20 70 41 72  pUnlockArg = pAr
332dc 67 3b 0a 20 20 20 20 20 20 72 65 6d 6f 76 65 46  g;.      removeF
332dd 72 6f 6d 42 6c 6f 63 6b 65 64 4c 69 73 74 28 64  romBlockedList(d
332de 62 29 3b 0a 20 20 20 20 20 20 61 64 64 54 6f 42  b);.      addToB
332df 6c 6f 63 6b 65 64 4c 69 73 74 28 64 62 29 3b 0a  lockedList(db);.
332e0 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 6c 65 61      }.  }..  lea
332e1 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 61 73 73  veMutex();.  ass
332e2 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63  ert( !db->malloc
332e3 46 61 69 6c 65 64 20 29 3b 0a 20 20 73 71 6c 69  Failed );.  sqli
332e4 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c  te3Error(db, rc,
332e5 20 28 72 63 3f 22 64 61 74 61 62 61 73 65 20 69   (rc?"database i
332e6 73 20 64 65 61 64 6c 6f 63 6b 65 64 22 3a 30 29  s deadlocked":0)
332e7 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
332e8 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
332e9 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ex);.  return rc
332ea 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
332eb 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
332ec 65 64 20 77 68 69 6c 65 20 73 74 65 70 70 69 6e  ed while steppin
332ed 67 20 6f 72 20 70 72 65 70 61 72 69 6e 67 20 61  g or preparing a
332ee 20 73 74 61 74 65 6d 65 6e 74 20 0a 2a 2a 20 61   statement .** a
332ef 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 63  ssociated with c
332f0 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 2e 20 54 68  onnection db. Th
332f1 65 20 6f 70 65 72 61 74 69 6f 6e 20 77 69 6c 6c  e operation will
332f2 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c   return SQLITE_L
332f3 4f 43 4b 45 44 0a 2a 2a 20 74 6f 20 74 68 65 20  OCKED.** to the 
332f4 75 73 65 72 20 62 65 63 61 75 73 65 20 69 74 20  user because it 
332f5 72 65 71 75 69 72 65 73 20 61 20 6c 6f 63 6b 20  requires a lock 
332f6 74 68 61 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65  that will not be
332f7 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 75 6e   available.** un
332f8 74 69 6c 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70  til connection p
332f9 42 6c 6f 63 6b 65 72 20 63 6f 6e 63 6c 75 64 65  Blocker conclude
332fa 73 20 69 74 73 20 63 75 72 72 65 6e 74 20 74 72  s its current tr
332fb 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51  ansaction..*/.SQ
332fc 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
332fd 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74  d sqlite3Connect
332fe 69 6f 6e 42 6c 6f 63 6b 65 64 28 73 71 6c 69 74  ionBlocked(sqlit
332ff 65 33 20 2a 64 62 2c 20 73 71 6c 69 74 65 33 20  e3 *db, sqlite3 
33300 2a 70 42 6c 6f 63 6b 65 72 29 7b 0a 20 20 65 6e  *pBlocker){.  en
33301 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 69 66  terMutex();.  if
33302 28 20 64 62 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43  ( db->pBlockingC
33303 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 30 20 26 26 20  onnection==0 && 
33304 64 62 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65  db->pUnlockConne
33305 63 74 69 6f 6e 3d 3d 30 20 29 7b 0a 20 20 20 20  ction==0 ){.    
33306 61 64 64 54 6f 42 6c 6f 63 6b 65 64 4c 69 73 74  addToBlockedList
33307 28 64 62 29 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e  (db);.  }.  db->
33308 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74  pBlockingConnect
33309 69 6f 6e 20 3d 20 70 42 6c 6f 63 6b 65 72 3b 0a  ion = pBlocker;.
3330a 20 20 6c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a    leaveMutex();.
3330b 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
3330c 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
3330d 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 74 72 61   when.** the tra
3330e 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 65 64 20  nsaction opened 
3330f 62 79 20 64 61 74 61 62 61 73 65 20 64 62 20 68  by database db h
33310 61 73 20 6a 75 73 74 20 66 69 6e 69 73 68 65 64  as just finished
33311 2e 20 4c 6f 63 6b 73 20 68 65 6c 64 20 0a 2a 2a  . Locks held .**
33312 20 62 79 20 64 61 74 61 62 61 73 65 20 63 6f 6e   by database con
33313 6e 65 63 74 69 6f 6e 20 64 62 20 68 61 76 65 20  nection db have 
33314 62 65 65 6e 20 72 65 6c 65 61 73 65 64 2e 0a 2a  been released..*
33315 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
33316 6f 6e 20 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68  on loops through
33317 20 65 61 63 68 20 65 6e 74 72 79 20 69 6e 20 74   each entry in t
33318 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65  he blocked conne
33319 63 74 69 6f 6e 73 0a 2a 2a 20 6c 69 73 74 20 61  ctions.** list a
3331a 6e 64 20 64 6f 65 73 20 74 68 65 20 66 6f 6c 6c  nd does the foll
3331b 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 31  owing:.**.**   1
3331c 29 20 49 66 20 74 68 65 20 73 71 6c 69 74 65 33  ) If the sqlite3
3331d 2e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63  .pBlockingConnec
3331e 74 69 6f 6e 20 6d 65 6d 62 65 72 20 6f 66 20 61  tion member of a
3331f 20 6c 69 73 74 20 65 6e 74 72 79 20 69 73 0a 2a   list entry is.*
33320 2a 20 20 20 20 20 20 73 65 74 20 74 6f 20 64 62  *      set to db
33321 2c 20 74 68 65 6e 20 73 65 74 20 70 42 6c 6f 63  , then set pBloc
33322 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 3d 30  kingConnection=0
33323 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20 49 66 20  ..**.**   2) If 
33324 74 68 65 20 73 71 6c 69 74 65 33 2e 70 55 6e 6c  the sqlite3.pUnl
33325 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 20 6d 65  ockConnection me
33326 6d 62 65 72 20 6f 66 20 61 20 6c 69 73 74 20 65  mber of a list e
33327 6e 74 72 79 20 69 73 0a 2a 2a 20 20 20 20 20 20  ntry is.**      
33328 73 65 74 20 74 6f 20 64 62 2c 20 74 68 65 6e 20  set to db, then 
33329 69 6e 76 6f 6b 65 20 74 68 65 20 63 6f 6e 66 69  invoke the confi
3332a 67 75 72 65 64 20 75 6e 6c 6f 63 6b 2d 6e 6f 74  gured unlock-not
3332b 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 61 6e 64  ify callback and
3332c 0a 2a 2a 20 20 20 20 20 20 73 65 74 20 70 55 6e  .**      set pUn
3332d 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d 30  lockConnection=0
3332e 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 29 20 49 66 20  ..**.**   3) If 
3332f 74 68 65 20 74 77 6f 20 73 74 65 70 73 20 61 62  the two steps ab
33330 6f 76 65 20 6d 65 61 6e 20 74 68 61 74 20 70 42  ove mean that pB
33331 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f  lockingConnectio
33332 6e 3d 3d 30 20 61 6e 64 0a 2a 2a 20 20 20 20 20  n==0 and.**     
33333 20 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69   pUnlockConnecti
33334 6f 6e 3d 3d 30 2c 20 72 65 6d 6f 76 65 20 74 68  on==0, remove th
33335 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65  e entry from the
33336 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74   blocked connect
33337 69 6f 6e 73 0a 2a 2a 20 20 20 20 20 20 6c 69 73  ions.**      lis
33338 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
33339 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
3333a 33 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63  3ConnectionUnloc
3333b 6b 65 64 28 73 71 6c 69 74 65 33 20 2a 64 62 29  ked(sqlite3 *db)
3333c 7b 0a 20 20 76 6f 69 64 20 28 2a 78 55 6e 6c 6f  {.  void (*xUnlo
3333d 63 6b 4e 6f 74 69 66 79 29 28 76 6f 69 64 20 2a  ckNotify)(void *
3333e 2a 2c 20 69 6e 74 29 20 3d 20 30 3b 20 2f 2a 20  *, int) = 0; /* 
3333f 55 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 62  Unlock-notify cb
33340 20 74 6f 20 69 6e 76 6f 6b 65 20 2a 2f 0a 20 20   to invoke */.  
33341 69 6e 74 20 6e 41 72 67 20 3d 20 30 3b 20 20 20  int nArg = 0;   
33342 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33343 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
33344 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e  er of entries in
33345 20 61 41 72 67 5b 5d 20 2a 2f 0a 20 20 73 71 6c   aArg[] */.  sql
33346 69 74 65 33 20 2a 2a 70 70 3b 20 20 20 20 20 20  ite3 **pp;      
33347 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33348 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f        /* Iterato
33349 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20  r variable */.  
3334a 76 6f 69 64 20 2a 2a 61 41 72 67 3b 20 20 20 20  void **aArg;    
3334b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72             /* Ar
3334c 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 75  guments to the u
3334d 6e 6c 6f 63 6b 20 63 61 6c 6c 62 61 63 6b 20 2a  nlock callback *
3334e 2f 0a 20 20 76 6f 69 64 20 2a 2a 61 44 79 6e 20  /.  void **aDyn 
3334f 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f  = 0;           /
33350 2a 20 44 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c  * Dynamically al
33351 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 66 6f  located space fo
33352 72 20 61 41 72 67 5b 5d 20 2a 2f 0a 20 20 76 6f  r aArg[] */.  vo
33353 69 64 20 2a 61 53 74 61 74 69 63 5b 31 36 5d 3b  id *aStatic[16];
33354 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72           /* Star
33355 74 65 72 20 73 70 61 63 65 20 66 6f 72 20 61 41  ter space for aA
33356 72 67 5b 5d 2e 20 20 4e 6f 20 6d 61 6c 6c 6f 63  rg[].  No malloc
33357 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 0a 20 20   required */..  
33358 61 41 72 67 20 3d 20 61 53 74 61 74 69 63 3b 0a  aArg = aStatic;.
33359 20 20 65 6e 74 65 72 4d 75 74 65 78 28 29 3b 20    enterMutex(); 
3335a 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 74 65 72          /* Enter
3335b 20 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20 6d   STATIC_MASTER m
3335c 75 74 65 78 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68  utex */..  /* Th
3335d 69 73 20 6c 6f 6f 70 20 72 75 6e 73 20 6f 6e 63  is loop runs onc
3335e 65 20 66 6f 72 20 65 61 63 68 20 65 6e 74 72 79  e for each entry
3335f 20 69 6e 20 74 68 65 20 62 6c 6f 63 6b 65 64 2d   in the blocked-
33360 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6c 69 73 74  connections list
33361 2e 20 2a 2f 0a 20 20 66 6f 72 28 70 70 3d 26 73  . */.  for(pp=&s
33362 71 6c 69 74 65 33 42 6c 6f 63 6b 65 64 4c 69 73  qlite3BlockedLis
33363 74 3b 20 2a 70 70 3b 20 2f 2a 20 6e 6f 2d 6f 70  t; *pp; /* no-op
33364 20 2a 2f 20 29 7b 0a 20 20 20 20 73 71 6c 69 74   */ ){.    sqlit
33365 65 33 20 2a 70 20 3d 20 2a 70 70 3b 0a 0a 20 20  e3 *p = *pp;..  
33366 20 20 2f 2a 20 53 74 65 70 20 31 2e 20 2a 2f 0a    /* Step 1. */.
33367 20 20 20 20 69 66 28 20 70 2d 3e 70 42 6c 6f 63      if( p->pBloc
33368 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d  kingConnection==
33369 64 62 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70  db ){.      p->p
3336a 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69  BlockingConnecti
3336b 6f 6e 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20  on = 0;.    }.. 
3336c 20 20 20 2f 2a 20 53 74 65 70 20 32 2e 20 2a 2f     /* Step 2. */
3336d 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 55 6e 6c  .    if( p->pUnl
3336e 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 64  ockConnection==d
3336f 62 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  b ){.      asser
33370 74 28 20 70 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74  t( p->xUnlockNot
33371 69 66 79 20 29 3b 0a 20 20 20 20 20 20 69 66 28  ify );.      if(
33372 20 70 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66   p->xUnlockNotif
33373 79 21 3d 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79  y!=xUnlockNotify
33374 20 26 26 20 6e 41 72 67 21 3d 30 20 29 7b 0a 20   && nArg!=0 ){. 
33375 20 20 20 20 20 20 20 78 55 6e 6c 6f 63 6b 4e 6f         xUnlockNo
33376 74 69 66 79 28 61 41 72 67 2c 20 6e 41 72 67 29  tify(aArg, nArg)
33377 3b 0a 20 20 20 20 20 20 20 20 6e 41 72 67 20 3d  ;.        nArg =
33378 20 30 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20   0;.      }..   
33379 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42     sqlite3BeginB
3337a 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20  enignMalloc();. 
3337b 20 20 20 20 20 61 73 73 65 72 74 28 20 61 41 72       assert( aAr
3337c 67 3d 3d 61 44 79 6e 20 7c 7c 20 28 61 44 79 6e  g==aDyn || (aDyn
3337d 3d 3d 30 20 26 26 20 61 41 72 67 3d 3d 61 53 74  ==0 && aArg==aSt
3337e 61 74 69 63 29 20 29 3b 0a 20 20 20 20 20 20 61  atic) );.      a
3337f 73 73 65 72 74 28 20 6e 41 72 67 3c 3d 28 69 6e  ssert( nArg<=(in
33380 74 29 41 72 72 61 79 53 69 7a 65 28 61 53 74 61  t)ArraySize(aSta
33381 74 69 63 29 20 7c 7c 20 61 41 72 67 3d 3d 61 44  tic) || aArg==aD
33382 79 6e 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  yn );.      if( 
33383 28 21 61 44 79 6e 20 26 26 20 6e 41 72 67 3d 3d  (!aDyn && nArg==
33384 28 69 6e 74 29 41 72 72 61 79 53 69 7a 65 28 61  (int)ArraySize(a
33385 53 74 61 74 69 63 29 29 0a 20 20 20 20 20 20 20  Static)).       
33386 7c 7c 20 28 61 44 79 6e 20 26 26 20 6e 41 72 67  || (aDyn && nArg
33387 3d 3d 28 69 6e 74 29 28 73 71 6c 69 74 65 33 44  ==(int)(sqlite3D
33388 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20  bMallocSize(db, 
33389 61 44 79 6e 29 2f 73 69 7a 65 6f 66 28 76 6f 69  aDyn)/sizeof(voi
3338a 64 2a 29 29 29 0a 20 20 20 20 20 20 29 7b 0a 20  d*))).      ){. 
3338b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 41         /* The aA
3338c 72 67 5b 5d 20 61 72 72 61 79 20 6e 65 65 64 73  rg[] array needs
3338d 20 74 6f 20 67 72 6f 77 2e 20 2a 2f 0a 20 20 20   to grow. */.   
3338e 20 20 20 20 20 76 6f 69 64 20 2a 2a 70 4e 65 77       void **pNew
3338f 20 3d 20 28 76 6f 69 64 20 2a 2a 29 73 71 6c 69   = (void **)sqli
33390 74 65 33 4d 61 6c 6c 6f 63 28 6e 41 72 67 2a 73  te3Malloc(nArg*s
33391 69 7a 65 6f 66 28 76 6f 69 64 20 2a 29 2a 32 29  izeof(void *)*2)
33392 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4e  ;.        if( pN
33393 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ew ){.          
33394 6d 65 6d 63 70 79 28 70 4e 65 77 2c 20 61 41 72  memcpy(pNew, aAr
33395 67 2c 20 6e 41 72 67 2a 73 69 7a 65 6f 66 28 76  g, nArg*sizeof(v
33396 6f 69 64 20 2a 29 29 3b 0a 20 20 20 20 20 20 20  oid *));.       
33397 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
33398 61 44 79 6e 29 3b 0a 20 20 20 20 20 20 20 20 20  aDyn);.         
33399 20 61 44 79 6e 20 3d 20 61 41 72 67 20 3d 20 70   aDyn = aArg = p
3339a 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  New;.        }el
3339b 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a  se{.          /*
3339c 20 54 68 69 73 20 6f 63 63 75 72 73 20 77 68 65   This occurs whe
3339d 6e 20 74 68 65 20 61 72 72 61 79 20 6f 66 20 63  n the array of c
3339e 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 73 20  ontext pointers 
3339f 74 68 61 74 20 6e 65 65 64 20 74 6f 0a 20 20 20  that need to.   
333a0 20 20 20 20 20 20 20 2a 2a 20 62 65 20 70 61 73         ** be pas
333a1 73 65 64 20 74 6f 20 74 68 65 20 75 6e 6c 6f 63  sed to the unloc
333a2 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63  k-notify callbac
333a3 6b 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e  k is larger than
333a4 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20 2a   the.          *
333a5 2a 20 61 53 74 61 74 69 63 5b 5d 20 61 72 72 61  * aStatic[] arra
333a6 79 20 61 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 74  y allocated on t
333a7 68 65 20 73 74 61 63 6b 20 61 6e 64 20 74 68 65  he stack and the
333a8 20 61 74 74 65 6d 70 74 20 74 6f 20 0a 20 20 20   attempt to .   
333a9 20 20 20 20 20 20 20 2a 2a 20 61 6c 6c 6f 63 61         ** alloca
333aa 74 65 20 61 20 6c 61 72 67 65 72 20 61 72 72 61  te a larger arra
333ab 79 20 66 72 6f 6d 20 74 68 65 20 68 65 61 70 20  y from the heap 
333ac 68 61 73 20 66 61 69 6c 65 64 2e 0a 20 20 20 20  has failed..    
333ad 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20        **.       
333ae 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 61 20     ** This is a 
333af 64 69 66 66 69 63 75 6c 74 20 73 69 74 75 61 74  difficult situat
333b0 69 6f 6e 20 74 6f 20 68 61 6e 64 6c 65 2e 20 52  ion to handle. R
333b1 65 74 75 72 6e 69 6e 67 20 61 6e 20 65 72 72 6f  eturning an erro
333b2 72 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63  r.          ** c
333b3 6f 64 65 20 74 6f 20 74 68 65 20 63 61 6c 6c 65  ode to the calle
333b4 72 20 69 73 20 69 6e 73 75 66 66 69 63 69 65 6e  r is insufficien
333b5 74 2c 20 61 73 20 65 76 65 6e 20 69 66 20 61 6e  t, as even if an
333b6 20 65 72 72 6f 72 20 63 6f 64 65 0a 20 20 20 20   error code.    
333b7 20 20 20 20 20 20 2a 2a 20 69 73 20 72 65 74 75        ** is retu
333b8 72 6e 65 64 20 74 68 65 20 74 72 61 6e 73 61 63  rned the transac
333b9 74 69 6f 6e 20 6f 6e 20 63 6f 6e 6e 65 63 74 69  tion on connecti
333ba 6f 6e 20 64 62 20 77 69 6c 6c 20 73 74 69 6c 6c  on db will still
333bb 20 62 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a   be.          **
333bc 20 63 6c 6f 73 65 64 20 61 6e 64 20 74 68 65 20   closed and the 
333bd 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61  unlock-notify ca
333be 6c 6c 62 61 63 6b 73 20 6f 6e 20 62 6c 6f 63 6b  llbacks on block
333bf 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 0a 20  ed connections. 
333c0 20 20 20 20 20 20 20 20 20 2a 2a 20 77 69 6c 6c           ** will
333c1 20 67 6f 20 75 6e 69 73 73 75 65 64 2e 20 54 68   go unissued. Th
333c2 69 73 20 6d 69 67 68 74 20 63 61 75 73 65 20 74  is might cause t
333c3 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 74  he application t
333c4 6f 20 77 61 69 74 0a 20 20 20 20 20 20 20 20 20  o wait.         
333c5 20 2a 2a 20 69 6e 64 65 66 69 6e 69 74 65 6c 79   ** indefinitely
333c6 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e   for an unlock-n
333c7 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 74  otify callback t
333c8 68 61 74 20 77 69 6c 6c 20 6e 65 76 65 72 20 0a  hat will never .
333c9 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 72 72            ** arr
333ca 69 76 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a  ive..          *
333cb 2a 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 49  *.          ** I
333cc 6e 73 74 65 61 64 2c 20 69 6e 76 6f 6b 65 20 74  nstead, invoke t
333cd 68 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79  he unlock-notify
333ce 20 63 61 6c 6c 62 61 63 6b 20 77 69 74 68 20 74   callback with t
333cf 68 65 20 63 6f 6e 74 65 78 74 0a 20 20 20 20 20  he context.     
333d0 20 20 20 20 20 2a 2a 20 61 72 72 61 79 20 61 6c       ** array al
333d1 72 65 61 64 79 20 61 63 63 75 6d 75 6c 61 74 65  ready accumulate
333d2 64 2e 20 57 65 20 63 61 6e 20 74 68 65 6e 20 63  d. We can then c
333d3 6c 65 61 72 20 74 68 65 20 61 72 72 61 79 20 61  lear the array a
333d4 6e 64 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  nd.          ** 
333d5 62 65 67 69 6e 20 61 63 63 75 6d 75 6c 61 74 69  begin accumulati
333d6 6e 67 20 61 6e 79 20 66 75 72 74 68 65 72 20 63  ng any further c
333d7 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 73 20  ontext pointers 
333d8 77 69 74 68 6f 75 74 20 0a 20 20 20 20 20 20 20  without .       
333d9 20 20 20 2a 2a 20 72 65 71 75 69 72 69 6e 67 20     ** requiring 
333da 61 6e 79 20 64 79 6e 61 6d 69 63 20 61 6c 6c 6f  any dynamic allo
333db 63 61 74 69 6f 6e 2e 20 54 68 69 73 20 69 73 20  cation. This is 
333dc 73 75 62 2d 6f 70 74 69 6d 61 6c 20 62 65 63 61  sub-optimal beca
333dd 75 73 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  use.          **
333de 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 69   it means that i
333df 6e 73 74 65 61 64 20 6f 66 20 6f 6e 65 20 63 61  nstead of one ca
333e0 6c 6c 62 61 63 6b 20 77 69 74 68 20 61 20 6c 61  llback with a la
333e1 72 67 65 20 61 72 72 61 79 20 6f 66 0a 20 20 20  rge array of.   
333e2 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 65 78         ** contex
333e3 74 20 70 6f 69 6e 74 65 72 73 20 74 68 65 20 61  t pointers the a
333e4 70 70 6c 69 63 61 74 69 6f 6e 20 77 69 6c 6c 20  pplication will 
333e5 72 65 63 65 69 76 65 20 74 77 6f 20 6f 72 20 6d  receive two or m
333e6 6f 72 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  ore.          **
333e7 20 63 61 6c 6c 62 61 63 6b 73 20 77 69 74 68 20   callbacks with 
333e8 73 6d 61 6c 6c 65 72 20 61 72 72 61 79 73 20 6f  smaller arrays o
333e9 66 20 63 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65  f context pointe
333ea 72 73 2c 20 77 68 69 63 68 20 77 69 6c 6c 0a 20  rs, which will. 
333eb 20 20 20 20 20 20 20 20 20 2a 2a 20 72 65 64 75           ** redu
333ec 63 65 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  ce the applicati
333ed 6f 6e 73 20 61 62 69 6c 69 74 79 20 74 6f 20 70  ons ability to p
333ee 72 69 6f 72 69 74 69 7a 65 20 6d 75 6c 74 69 70  rioritize multip
333ef 6c 65 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  le .          **
333f0 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 42 75   connections. Bu
333f1 74 20 69 74 20 69 73 20 74 68 65 20 62 65 73 74  t it is the best
333f2 20 74 68 61 74 20 63 61 6e 20 62 65 20 64 6f 6e   that can be don
333f3 65 20 75 6e 64 65 72 20 74 68 65 0a 20 20 20 20  e under the.    
333f4 20 20 20 20 20 20 2a 2a 20 63 69 72 63 75 6d 73        ** circums
333f5 74 61 6e 63 65 73 2e 0a 20 20 20 20 20 20 20 20  tances..        
333f6 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 78    */.          x
333f7 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 28 61 41 72  UnlockNotify(aAr
333f8 67 2c 20 6e 41 72 67 29 3b 0a 20 20 20 20 20 20  g, nArg);.      
333f9 20 20 20 20 6e 41 72 67 20 3d 20 30 3b 0a 20 20      nArg = 0;.  
333fa 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
333fb 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64        sqlite3End
333fc 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a  BenignMalloc();.
333fd 0a 20 20 20 20 20 20 61 41 72 67 5b 6e 41 72 67  .      aArg[nArg
333fe 2b 2b 5d 20 3d 20 70 2d 3e 70 55 6e 6c 6f 63 6b  ++] = p->pUnlock
333ff 41 72 67 3b 0a 20 20 20 20 20 20 78 55 6e 6c 6f  Arg;.      xUnlo
33400 63 6b 4e 6f 74 69 66 79 20 3d 20 70 2d 3e 78 55  ckNotify = p->xU
33401 6e 6c 6f 63 6b 4e 6f 74 69 66 79 3b 0a 20 20 20  nlockNotify;.   
33402 20 20 20 70 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e     p->pUnlockCon
33403 6e 65 63 74 69 6f 6e 20 3d 20 30 3b 0a 20 20 20  nection = 0;.   
33404 20 20 20 70 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74     p->xUnlockNot
33405 69 66 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  ify = 0;.      p
33406 2d 3e 70 55 6e 6c 6f 63 6b 41 72 67 20 3d 20 30  ->pUnlockArg = 0
33407 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
33408 53 74 65 70 20 33 2e 20 2a 2f 0a 20 20 20 20 69  Step 3. */.    i
33409 66 28 20 70 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43  f( p->pBlockingC
3340a 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 30 20 26 26 20  onnection==0 && 
3340b 70 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63  p->pUnlockConnec
3340c 74 69 6f 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20  tion==0 ){.     
3340d 20 2f 2a 20 52 65 6d 6f 76 65 20 63 6f 6e 6e 65   /* Remove conne
3340e 63 74 69 6f 6e 20 70 20 66 72 6f 6d 20 74 68 65  ction p from the
3340f 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74   blocked connect
33410 69 6f 6e 73 20 6c 69 73 74 2e 20 2a 2f 0a 20 20  ions list. */.  
33411 20 20 20 20 2a 70 70 20 3d 20 70 2d 3e 70 4e 65      *pp = p->pNe
33412 78 74 42 6c 6f 63 6b 65 64 3b 0a 20 20 20 20 20  xtBlocked;.     
33413 20 70 2d 3e 70 4e 65 78 74 42 6c 6f 63 6b 65 64   p->pNextBlocked
33414 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b   = 0;.    }else{
33415 0a 20 20 20 20 20 20 70 70 20 3d 20 26 70 2d 3e  .      pp = &p->
33416 70 4e 65 78 74 42 6c 6f 63 6b 65 64 3b 0a 20 20  pNextBlocked;.  
33417 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 6e    }.  }..  if( n
33418 41 72 67 21 3d 30 20 29 7b 0a 20 20 20 20 78 55  Arg!=0 ){.    xU
33419 6e 6c 6f 63 6b 4e 6f 74 69 66 79 28 61 41 72 67  nlockNotify(aArg
3341a 2c 20 6e 41 72 67 29 3b 0a 20 20 7d 0a 20 20 73  , nArg);.  }.  s
3341b 71 6c 69 74 65 33 5f 66 72 65 65 28 61 44 79 6e  qlite3_free(aDyn
3341c 29 3b 0a 20 20 6c 65 61 76 65 4d 75 74 65 78 28  );.  leaveMutex(
3341d 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65  );         /* Le
3341e 61 76 65 20 53 54 41 54 49 43 5f 4d 41 53 54 45  ave STATIC_MASTE
3341f 52 20 6d 75 74 65 78 20 2a 2f 0a 7d 0a 0a 2f 2a  R mutex */.}../*
33420 0a 2a 2a 20 54 68 69 73 20 69 73 20 63 61 6c 6c  .** This is call
33421 65 64 20 77 68 65 6e 20 74 68 65 20 64 61 74 61  ed when the data
33422 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
33423 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 67  passed as an arg
33424 75 6d 65 6e 74 20 69 73 20 0a 2a 2a 20 62 65 69  ument is .** bei
33425 6e 67 20 63 6c 6f 73 65 64 2e 20 54 68 65 20 63  ng closed. The c
33426 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 72 65 6d  onnection is rem
33427 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 62 6c  oved from the bl
33428 6f 63 6b 65 64 20 6c 69 73 74 2e 0a 2a 2f 0a 53  ocked list..*/.S
33429 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
3342a 69 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63  id sqlite3Connec
3342b 74 69 6f 6e 43 6c 6f 73 65 64 28 73 71 6c 69 74  tionClosed(sqlit
3342c 65 33 20 2a 64 62 29 7b 0a 20 20 73 71 6c 69 74  e3 *db){.  sqlit
3342d 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f  e3ConnectionUnlo
3342e 63 6b 65 64 28 64 62 29 3b 0a 20 20 65 6e 74 65  cked(db);.  ente
3342f 72 4d 75 74 65 78 28 29 3b 0a 20 20 72 65 6d 6f  rMutex();.  remo
33430 76 65 46 72 6f 6d 42 6c 6f 63 6b 65 64 4c 69 73  veFromBlockedLis
33431 74 28 64 62 29 3b 0a 20 20 63 68 65 63 6b 4c 69  t(db);.  checkLi
33432 73 74 50 72 6f 70 65 72 74 69 65 73 28 64 62 29  stProperties(db)
33433 3b 0a 20 20 6c 65 61 76 65 4d 75 74 65 78 28 29  ;.  leaveMutex()
33434 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a  ;.}.#endif../***
33435 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
33436 6f 66 20 6e 6f 74 69 66 79 2e 63 20 2a 2a 2a 2a  of notify.c ****
33437 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33438 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33439 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
3343a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
3343b 6e 20 66 69 6c 65 20 66 74 73 33 2e 63 20 2a 2a  n file fts3.c **
3343c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3343d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3343e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
3343f 2a 20 32 30 30 36 20 4f 63 74 20 31 30 0a 2a 2a  * 2006 Oct 10.**
33440 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
33441 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
33442 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
33443 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
33444 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
33445 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
33446 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
33447 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
33448 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
33449 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
3344a 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
3344b 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
3344c 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
3344d 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
3344e 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
3344f 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
33450 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
33451 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
33452 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33453 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33454 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33455 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33456 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  *.**.** This is 
33457 61 6e 20 53 51 4c 69 74 65 20 6d 6f 64 75 6c 65  an SQLite module
33458 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 66 75   implementing fu
33459 6c 6c 2d 74 65 78 74 20 73 65 61 72 63 68 2e 0a  ll-text search..
3345a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f  */../*.** The co
3345b 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  de in this file 
3345c 69 73 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65 64  is only compiled
3345d 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a   if:.**.**     *
3345e 20 54 68 65 20 46 54 53 33 20 6d 6f 64 75 6c 65   The FTS3 module
3345f 20 69 73 20 62 65 69 6e 67 20 62 75 69 6c 74 20   is being built 
33460 61 73 20 61 6e 20 65 78 74 65 6e 73 69 6f 6e 0a  as an extension.
33461 2a 2a 20 20 20 20 20 20 20 28 69 6e 20 77 68 69  **       (in whi
33462 63 68 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43  ch case SQLITE_C
33463 4f 52 45 20 69 73 20 6e 6f 74 20 64 65 66 69 6e  ORE is not defin
33464 65 64 29 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20  ed), or.**.**   
33465 20 20 2a 20 54 68 65 20 46 54 53 33 20 6d 6f 64    * The FTS3 mod
33466 75 6c 65 20 69 73 20 62 65 69 6e 67 20 62 75 69  ule is being bui
33467 6c 74 20 69 6e 74 6f 20 74 68 65 20 63 6f 72 65  lt into the core
33468 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c   of.**       SQL
33469 69 74 65 20 28 69 6e 20 77 68 69 63 68 20 63 61  ite (in which ca
3346a 73 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  se SQLITE_ENABLE
3346b 5f 46 54 53 33 20 69 73 20 64 65 66 69 6e 65 64  _FTS3 is defined
3346c 29 2e 0a 2a 2f 0a 0a 2f 2a 20 54 4f 44 4f 28 73  )..*/../* TODO(s
3346d 68 65 73 73 29 20 43 6f 6e 73 69 64 65 72 20 65  hess) Consider e
3346e 78 70 6f 72 74 69 6e 67 20 74 68 69 73 20 63 6f  xporting this co
3346f 6d 6d 65 6e 74 20 74 6f 20 61 6e 20 48 54 4d 4c  mment to an HTML
33470 20 66 69 6c 65 20 6f 72 20 74 68 65 0a 2a 2a 20   file or the.** 
33471 77 69 6b 69 2e 0a 2a 2f 0a 2f 2a 20 54 68 65 20  wiki..*/./* The 
33472 66 75 6c 6c 2d 74 65 78 74 20 69 6e 64 65 78 20  full-text index 
33473 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 20 73  is stored in a s
33474 65 72 69 65 73 20 6f 66 20 62 2b 74 72 65 65 20  eries of b+tree 
33475 28 2d 6c 69 6b 65 29 0a 2a 2a 20 73 74 72 75 63  (-like).** struc
33476 74 75 72 65 73 20 63 61 6c 6c 65 64 20 73 65 67  tures called seg
33477 6d 65 6e 74 73 20 77 68 69 63 68 20 6d 61 70 20  ments which map 
33478 74 65 72 6d 73 20 74 6f 20 64 6f 63 6c 69 73 74  terms to doclist
33479 73 2e 20 20 54 68 65 0a 2a 2a 20 73 74 72 75 63  s.  The.** struc
3347a 74 75 72 65 73 20 61 72 65 20 6c 69 6b 65 20 62  tures are like b
3347b 2b 74 72 65 65 73 20 69 6e 20 6c 61 79 6f 75 74  +trees in layout
3347c 2c 20 62 75 74 20 61 72 65 20 63 6f 6e 73 74 72  , but are constr
3347d 75 63 74 65 64 20 66 72 6f 6d 20 74 68 65 0a 2a  ucted from the.*
3347e 2a 20 62 6f 74 74 6f 6d 20 75 70 20 69 6e 20 6f  * bottom up in o
3347f 70 74 69 6d 61 6c 20 66 61 73 68 69 6f 6e 20 61  ptimal fashion a
33480 6e 64 20 61 72 65 20 6e 6f 74 20 75 70 64 61 74  nd are not updat
33481 61 62 6c 65 2e 20 20 53 69 6e 63 65 20 74 72 65  able.  Since tre
33482 65 73 0a 2a 2a 20 61 72 65 20 62 75 69 6c 74 20  es.** are built 
33483 66 72 6f 6d 20 74 68 65 20 62 6f 74 74 6f 6d 20  from the bottom 
33484 75 70 2c 20 74 68 69 6e 67 73 20 77 69 6c 6c 20  up, things will 
33485 62 65 20 64 65 73 63 72 69 62 65 64 20 66 72 6f  be described fro
33486 6d 20 74 68 65 0a 2a 2a 20 62 6f 74 74 6f 6d 20  m the.** bottom 
33487 75 70 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 56  up..**.**.**** V
33488 61 72 69 6e 74 73 20 2a 2a 2a 2a 0a 2a 2a 20 54  arints ****.** T
33489 68 65 20 62 61 73 69 63 20 75 6e 69 74 20 6f 66  he basic unit of
3348a 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 61 20 76   encoding is a v
3348b 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69  ariable-length i
3348c 6e 74 65 67 65 72 20 63 61 6c 6c 65 64 20 61 0a  nteger called a.
3348d 2a 2a 20 76 61 72 69 6e 74 2e 20 20 57 65 20 65  ** varint.  We e
3348e 6e 63 6f 64 65 20 76 61 72 69 61 62 6c 65 2d 6c  ncode variable-l
3348f 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73 20 69  ength integers i
33490 6e 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20  n little-endian 
33491 6f 72 64 65 72 0a 2a 2a 20 75 73 69 6e 67 20 73  order.** using s
33492 65 76 65 6e 20 62 69 74 73 20 2a 20 70 65 72 20  even bits * per 
33493 62 79 74 65 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  byte as follows:
33494 0a 2a 2a 0a 2a 2a 20 4b 45 59 3a 0a 2a 2a 20 20  .**.** KEY:.**  
33495 20 20 20 20 20 20 20 41 20 3d 20 30 78 78 78 78         A = 0xxxx
33496 78 78 78 20 20 20 20 37 20 62 69 74 73 20 6f 66  xxx    7 bits of
33497 20 64 61 74 61 20 61 6e 64 20 6f 6e 65 20 66 6c   data and one fl
33498 61 67 20 62 69 74 0a 2a 2a 20 20 20 20 20 20 20  ag bit.**       
33499 20 20 42 20 3d 20 31 78 78 78 78 78 78 78 20 20    B = 1xxxxxxx  
3349a 20 20 37 20 62 69 74 73 20 6f 66 20 64 61 74 61    7 bits of data
3349b 20 61 6e 64 20 6f 6e 65 20 66 6c 61 67 20 62 69   and one flag bi
3349c 74 0a 2a 2a 0a 2a 2a 20 20 37 20 62 69 74 73 20  t.**.**  7 bits 
3349d 2d 20 41 0a 2a 2a 20 31 34 20 62 69 74 73 20 2d  - A.** 14 bits -
3349e 20 42 41 0a 2a 2a 20 32 31 20 62 69 74 73 20 2d   BA.** 21 bits -
3349f 20 42 42 41 0a 2a 2a 20 61 6e 64 20 73 6f 20 6f   BBA.** and so o
334a0 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73  n..**.** This is
334a1 20 69 64 65 6e 74 69 63 61 6c 20 74 6f 20 68 6f   identical to ho
334a2 77 20 73 71 6c 69 74 65 20 65 6e 63 6f 64 65 73  w sqlite encodes
334a3 20 76 61 72 69 6e 74 73 20 28 73 65 65 20 75 74   varints (see ut
334a4 69 6c 2e 63 29 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a  il.c)..**.**.***
334a5 2a 20 44 6f 63 75 6d 65 6e 74 20 6c 69 73 74 73  * Document lists
334a6 20 2a 2a 2a 2a 0a 2a 2a 20 41 20 64 6f 63 6c 69   ****.** A docli
334a7 73 74 20 28 64 6f 63 75 6d 65 6e 74 20 6c 69 73  st (document lis
334a8 74 29 20 68 6f 6c 64 73 20 61 20 64 6f 63 69 64  t) holds a docid
334a9 2d 73 6f 72 74 65 64 20 6c 69 73 74 20 6f 66 20  -sorted list of 
334aa 68 69 74 73 20 66 6f 72 20 61 0a 2a 2a 20 67 69  hits for a.** gi
334ab 76 65 6e 20 74 65 72 6d 2e 20 20 44 6f 63 6c 69  ven term.  Docli
334ac 73 74 73 20 68 6f 6c 64 20 64 6f 63 69 64 73 2c  sts hold docids,
334ad 20 61 6e 64 20 63 61 6e 20 6f 70 74 69 6f 6e 61   and can optiona
334ae 6c 6c 79 20 61 73 73 6f 63 69 61 74 65 0a 2a 2a  lly associate.**
334af 20 74 6f 6b 65 6e 20 70 6f 73 69 74 69 6f 6e 73   token positions
334b0 20 61 6e 64 20 6f 66 66 73 65 74 73 20 77 69 74   and offsets wit
334b1 68 20 64 6f 63 69 64 73 2e 0a 2a 2a 0a 2a 2a 20  h docids..**.** 
334b2 41 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f  A DL_POSITIONS_O
334b3 46 46 53 45 54 53 20 64 6f 63 6c 69 73 74 20 69  FFSETS doclist i
334b4 73 20 73 74 6f 72 65 64 20 6c 69 6b 65 20 74 68  s stored like th
334b5 69 73 3a 0a 2a 2a 0a 2a 2a 20 61 72 72 61 79 20  is:.**.** array 
334b6 7b 0a 2a 2a 20 20 20 76 61 72 69 6e 74 20 64 6f  {.**   varint do
334b7 63 69 64 3b 0a 2a 2a 20 20 20 61 72 72 61 79 20  cid;.**   array 
334b8 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
334b9 20 28 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 20   (position list 
334ba 66 6f 72 20 63 6f 6c 75 6d 6e 20 30 29 0a 2a 2a  for column 0).**
334bb 20 20 20 20 20 76 61 72 69 6e 74 20 70 6f 73 69       varint posi
334bc 74 69 6f 6e 3b 20 20 20 20 20 28 64 65 6c 74 61  tion;     (delta
334bd 20 66 72 6f 6d 20 70 72 65 76 69 6f 75 73 20 70   from previous p
334be 6f 73 69 74 69 6f 6e 20 70 6c 75 73 20 50 4f 53  osition plus POS
334bf 5f 42 41 53 45 29 0a 2a 2a 20 20 20 20 20 76 61  _BASE).**     va
334c0 72 69 6e 74 20 73 74 61 72 74 4f 66 66 73 65 74  rint startOffset
334c1 3b 20 20 28 64 65 6c 74 61 20 66 72 6f 6d 20 70  ;  (delta from p
334c2 72 65 76 69 6f 75 73 20 73 74 61 72 74 4f 66 66  revious startOff
334c3 73 65 74 29 0a 2a 2a 20 20 20 20 20 76 61 72 69  set).**     vari
334c4 6e 74 20 65 6e 64 4f 66 66 73 65 74 3b 20 20 20  nt endOffset;   
334c5 20 28 64 65 6c 74 61 20 66 72 6f 6d 20 73 74 61   (delta from sta
334c6 72 74 4f 66 66 73 65 74 29 0a 2a 2a 20 20 20 7d  rtOffset).**   }
334c7 0a 2a 2a 20 20 20 61 72 72 61 79 20 7b 0a 2a 2a  .**   array {.**
334c8 20 20 20 20 20 76 61 72 69 6e 74 20 50 4f 53 5f       varint POS_
334c9 43 4f 4c 55 4d 4e 3b 20 20 20 28 6d 61 72 6b 73  COLUMN;   (marks
334ca 20 73 74 61 72 74 20 6f 66 20 70 6f 73 69 74 69   start of positi
334cb 6f 6e 20 6c 69 73 74 20 66 6f 72 20 6e 65 77 20  on list for new 
334cc 63 6f 6c 75 6d 6e 29 0a 2a 2a 20 20 20 20 20 76  column).**     v
334cd 61 72 69 6e 74 20 63 6f 6c 75 6d 6e 3b 20 20 20  arint column;   
334ce 20 20 20 20 28 69 6e 64 65 78 20 6f 66 20 6e 65      (index of ne
334cf 77 20 63 6f 6c 75 6d 6e 29 0a 2a 2a 20 20 20 20  w column).**    
334d0 20 61 72 72 61 79 20 7b 0a 2a 2a 20 20 20 20 20   array {.**     
334d1 20 20 76 61 72 69 6e 74 20 70 6f 73 69 74 69 6f    varint positio
334d2 6e 3b 20 20 20 28 64 65 6c 74 61 20 66 72 6f 6d  n;   (delta from
334d3 20 70 72 65 76 69 6f 75 73 20 70 6f 73 69 74 69   previous positi
334d4 6f 6e 20 70 6c 75 73 20 50 4f 53 5f 42 41 53 45  on plus POS_BASE
334d5 29 0a 2a 2a 20 20 20 20 20 20 20 76 61 72 69 6e  ).**       varin
334d6 74 20 73 74 61 72 74 4f 66 66 73 65 74 3b 28 64  t startOffset;(d
334d7 65 6c 74 61 20 66 72 6f 6d 20 70 72 65 76 69 6f  elta from previo
334d8 75 73 20 73 74 61 72 74 4f 66 66 73 65 74 29 0a  us startOffset).
334d9 2a 2a 20 20 20 20 20 20 20 76 61 72 69 6e 74 20  **       varint 
334da 65 6e 64 4f 66 66 73 65 74 3b 20 20 28 64 65 6c  endOffset;  (del
334db 74 61 20 66 72 6f 6d 20 73 74 61 72 74 4f 66 66  ta from startOff
334dc 73 65 74 29 0a 2a 2a 20 20 20 20 20 7d 0a 2a 2a  set).**     }.**
334dd 20 20 20 7d 0a 2a 2a 20 20 20 76 61 72 69 6e 74     }.**   varint
334de 20 50 4f 53 5f 45 4e 44 3b 20 20 20 20 20 20 20   POS_END;       
334df 20 28 6d 61 72 6b 73 20 65 6e 64 20 6f 66 20 70   (marks end of p
334e0 6f 73 69 74 69 6f 6e 73 20 66 6f 72 20 74 68 69  ositions for thi
334e1 73 20 64 6f 63 75 6d 65 6e 74 2e 0a 2a 2a 20 7d  s document..** }
334e2 0a 2a 2a 0a 2a 2a 20 48 65 72 65 2c 20 61 72 72  .**.** Here, arr
334e3 61 79 20 7b 20 58 20 7d 20 6d 65 61 6e 73 20 7a  ay { X } means z
334e4 65 72 6f 20 6f 72 20 6d 6f 72 65 20 6f 63 63 75  ero or more occu
334e5 72 72 65 6e 63 65 73 20 6f 66 20 58 2c 20 61 64  rrences of X, ad
334e6 6a 61 63 65 6e 74 20 69 6e 0a 2a 2a 20 6d 65 6d  jacent in.** mem
334e7 6f 72 79 2e 20 20 41 20 22 70 6f 73 69 74 69 6f  ory.  A "positio
334e8 6e 22 20 69 73 20 61 6e 20 69 6e 64 65 78 20 6f  n" is an index o
334e9 66 20 61 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65  f a token in the
334ea 20 74 6f 6b 65 6e 20 73 74 72 65 61 6d 0a 2a 2a   token stream.**
334eb 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 74 68   generated by th
334ec 65 20 74 6f 6b 65 6e 69 7a 65 72 2c 20 77 68 69  e tokenizer, whi
334ed 6c 65 20 61 6e 20 22 6f 66 66 73 65 74 22 20 69  le an "offset" i
334ee 73 20 61 20 62 79 74 65 20 6f 66 66 73 65 74 2c  s a byte offset,
334ef 0a 2a 2a 20 62 6f 74 68 20 62 61 73 65 64 20 61  .** both based a
334f0 74 20 30 2e 20 20 4e 6f 74 65 20 74 68 61 74 20  t 0.  Note that 
334f1 50 4f 53 5f 45 4e 44 20 61 6e 64 20 50 4f 53 5f  POS_END and POS_
334f2 43 4f 4c 55 4d 4e 20 6f 63 63 75 72 20 69 6e 20  COLUMN occur in 
334f3 74 68 65 0a 2a 2a 20 73 61 6d 65 20 6c 6f 67 69  the.** same logi
334f4 63 61 6c 20 70 6c 61 63 65 20 61 73 20 74 68 65  cal place as the
334f5 20 70 6f 73 69 74 69 6f 6e 20 65 6c 65 6d 65 6e   position elemen
334f6 74 2c 20 61 6e 64 20 61 63 74 20 61 73 20 73 65  t, and act as se
334f7 6e 74 69 6e 61 6c 73 0a 2a 2a 20 65 6e 64 69 6e  ntinals.** endin
334f8 67 20 61 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73  g a position lis
334f9 74 20 61 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 41  t array..**.** A
334fa 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 20 64 6f   DL_POSITIONS do
334fb 63 6c 69 73 74 20 6f 6d 69 74 73 20 74 68 65 20  clist omits the 
334fc 73 74 61 72 74 4f 66 66 73 65 74 20 61 6e 64 20  startOffset and 
334fd 65 6e 64 4f 66 66 73 65 74 0a 2a 2a 20 69 6e 66  endOffset.** inf
334fe 6f 72 6d 61 74 69 6f 6e 2e 20 20 41 20 44 4c 5f  ormation.  A DL_
334ff 44 4f 43 49 44 53 20 64 6f 63 6c 69 73 74 20 6f  DOCIDS doclist o
33500 6d 69 74 73 20 62 6f 74 68 20 74 68 65 20 70 6f  mits both the po
33501 73 69 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 6f 66  sition and.** of
33502 66 73 65 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  fset information
33503 2c 20 62 65 63 6f 6d 69 6e 67 20 61 6e 20 61 72  , becoming an ar
33504 72 61 79 20 6f 66 20 76 61 72 69 6e 74 2d 65 6e  ray of varint-en
33505 63 6f 64 65 64 20 64 6f 63 69 64 73 2e 0a 2a 2a  coded docids..**
33506 0a 2a 2a 20 4f 6e 2d 64 69 73 6b 20 64 61 74 61  .** On-disk data
33507 20 69 73 20 73 74 6f 72 65 64 20 61 73 20 74 79   is stored as ty
33508 70 65 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20 73  pe DL_DEFAULT, s
33509 6f 20 77 65 20 64 6f 6e 27 74 20 73 65 72 69 61  o we don't seria
3350a 6c 69 7a 65 0a 2a 2a 20 74 68 65 20 74 79 70 65  lize.** the type
3350b 2e 20 20 44 75 65 20 74 6f 20 68 6f 77 20 64 65  .  Due to how de
3350c 6c 65 74 69 6f 6e 20 69 73 20 69 6d 70 6c 65 6d  letion is implem
3350d 65 6e 74 65 64 20 69 6e 20 74 68 65 20 73 65 67  ented in the seg
3350e 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 73 79 73  mentation.** sys
3350f 74 65 6d 2c 20 6f 6e 2d 64 69 73 6b 20 64 6f 63  tem, on-disk doc
33510 6c 69 73 74 73 20 4d 55 53 54 20 73 74 6f 72 65  lists MUST store
33511 20 61 74 20 6c 65 61 73 74 20 70 6f 73 69 74 69   at least positi
33512 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20  ons..**.**.**** 
33513 53 65 67 6d 65 6e 74 20 6c 65 61 66 20 6e 6f 64  Segment leaf nod
33514 65 73 20 2a 2a 2a 2a 0a 2a 2a 20 53 65 67 6d 65  es ****.** Segme
33515 6e 74 20 6c 65 61 66 20 6e 6f 64 65 73 20 73 74  nt leaf nodes st
33516 6f 72 65 20 74 65 72 6d 73 20 61 6e 64 20 64 6f  ore terms and do
33517 63 6c 69 73 74 73 2c 20 6f 72 64 65 72 65 64 20  clists, ordered 
33518 62 79 20 74 65 72 6d 2e 20 20 4c 65 61 66 0a 2a  by term.  Leaf.*
33519 2a 20 6e 6f 64 65 73 20 61 72 65 20 77 72 69 74  * nodes are writ
3351a 74 65 6e 20 75 73 69 6e 67 20 4c 65 61 66 57 72  ten using LeafWr
3351b 69 74 65 72 2c 20 61 6e 64 20 72 65 61 64 20 75  iter, and read u
3351c 73 69 6e 67 20 4c 65 61 66 52 65 61 64 65 72 20  sing LeafReader 
3351d 28 74 6f 0a 2a 2a 20 69 74 65 72 61 74 65 20 74  (to.** iterate t
3351e 68 72 6f 75 67 68 20 61 20 73 69 6e 67 6c 65 20  hrough a single 
3351f 6c 65 61 66 20 6e 6f 64 65 27 73 20 64 61 74 61  leaf node's data
33520 29 20 61 6e 64 20 4c 65 61 76 65 73 52 65 61 64  ) and LeavesRead
33521 65 72 20 28 74 6f 0a 2a 2a 20 69 74 65 72 61 74  er (to.** iterat
33522 65 20 74 68 72 6f 75 67 68 20 61 20 73 65 67 6d  e through a segm
33523 65 6e 74 27 73 20 65 6e 74 69 72 65 20 6c 65 61  ent's entire lea
33524 66 20 6c 61 79 65 72 29 2e 20 20 4c 65 61 66 20  f layer).  Leaf 
33525 6e 6f 64 65 73 20 68 61 76 65 0a 2a 2a 20 74 68  nodes have.** th
33526 65 20 66 6f 72 6d 61 74 3a 0a 2a 2a 0a 2a 2a 20  e format:.**.** 
33527 76 61 72 69 6e 74 20 69 48 65 69 67 68 74 3b 20  varint iHeight; 
33528 20 20 20 20 20 20 20 20 20 20 20 20 28 68 65 69              (hei
33529 67 68 74 20 66 72 6f 6d 20 6c 65 61 66 20 6c 65  ght from leaf le
3352a 76 65 6c 2c 20 61 6c 77 61 79 73 20 30 29 0a 2a  vel, always 0).*
3352b 2a 20 76 61 72 69 6e 74 20 6e 54 65 72 6d 3b 20  * varint nTerm; 
3352c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 6c                (l
3352d 65 6e 67 74 68 20 6f 66 20 66 69 72 73 74 20 74  ength of first t
3352e 65 72 6d 29 0a 2a 2a 20 63 68 61 72 20 70 54 65  erm).** char pTe
3352f 72 6d 5b 6e 54 65 72 6d 5d 3b 20 20 20 20 20 20  rm[nTerm];      
33530 20 20 20 20 28 63 6f 6e 74 65 6e 74 20 6f 66 20      (content of 
33531 66 69 72 73 74 20 74 65 72 6d 29 0a 2a 2a 20 76  first term).** v
33532 61 72 69 6e 74 20 6e 44 6f 63 6c 69 73 74 3b 20  arint nDoclist; 
33533 20 20 20 20 20 20 20 20 20 20 20 28 6c 65 6e 67             (leng
33534 74 68 20 6f 66 20 74 65 72 6d 27 73 20 61 73 73  th of term's ass
33535 6f 63 69 61 74 65 64 20 64 6f 63 6c 69 73 74 29  ociated doclist)
33536 0a 2a 2a 20 63 68 61 72 20 70 44 6f 63 6c 69 73  .** char pDoclis
33537 74 5b 6e 44 6f 63 6c 69 73 74 5d 3b 20 20 20 20  t[nDoclist];    
33538 28 63 6f 6e 74 65 6e 74 20 6f 66 20 64 6f 63 6c  (content of docl
33539 69 73 74 29 0a 2a 2a 20 61 72 72 61 79 20 7b 0a  ist).** array {.
3353a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
3353b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
3353c 66 75 72 74 68 65 72 20 74 65 72 6d 73 20 61 72  further terms ar
3353d 65 20 64 65 6c 74 61 2d 65 6e 63 6f 64 65 64 29  e delta-encoded)
3353e 0a 2a 2a 20 20 20 76 61 72 69 6e 74 20 6e 50 72  .**   varint nPr
3353f 65 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20  efix;           
33540 28 6c 65 6e 67 74 68 20 6f 66 20 70 72 65 66 69  (length of prefi
33541 78 20 73 68 61 72 65 64 20 77 69 74 68 20 70 72  x shared with pr
33542 65 76 69 6f 75 73 20 74 65 72 6d 29 0a 2a 2a 20  evious term).** 
33543 20 20 76 61 72 69 6e 74 20 6e 53 75 66 66 69 78    varint nSuffix
33544 3b 20 20 20 20 20 20 20 20 20 20 20 28 6c 65 6e  ;           (len
33545 67 74 68 20 6f 66 20 75 6e 73 68 61 72 65 64 20  gth of unshared 
33546 73 75 66 66 69 78 29 0a 2a 2a 20 20 20 63 68 61  suffix).**   cha
33547 72 20 70 54 65 72 6d 53 75 66 66 69 78 5b 6e 53  r pTermSuffix[nS
33548 75 66 66 69 78 5d 3b 28 75 6e 73 68 61 72 65 64  uffix];(unshared
33549 20 73 75 66 66 69 78 20 6f 66 20 6e 65 78 74 20   suffix of next 
3354a 74 65 72 6d 29 0a 2a 2a 20 20 20 76 61 72 69 6e  term).**   varin
3354b 74 20 6e 44 6f 63 6c 69 73 74 3b 20 20 20 20 20  t nDoclist;     
3354c 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66 20       (length of 
3354d 74 65 72 6d 27 73 20 61 73 73 6f 63 69 61 74 65  term's associate
3354e 64 20 64 6f 63 6c 69 73 74 29 0a 2a 2a 20 20 20  d doclist).**   
3354f 63 68 61 72 20 70 44 6f 63 6c 69 73 74 5b 6e 44  char pDoclist[nD
33550 6f 63 6c 69 73 74 5d 3b 20 20 28 63 6f 6e 74 65  oclist];  (conte
33551 6e 74 20 6f 66 20 64 6f 63 6c 69 73 74 29 0a 2a  nt of doclist).*
33552 2a 20 7d 0a 2a 2a 0a 2a 2a 20 48 65 72 65 2c 20  * }.**.** Here, 
33553 61 72 72 61 79 20 7b 20 58 20 7d 20 6d 65 61 6e  array { X } mean
33554 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 6f  s zero or more o
33555 63 63 75 72 72 65 6e 63 65 73 20 6f 66 20 58 2c  ccurrences of X,
33556 20 61 64 6a 61 63 65 6e 74 20 69 6e 0a 2a 2a 20   adjacent in.** 
33557 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 4c 65  memory..**.** Le
33558 61 66 20 6e 6f 64 65 73 20 61 72 65 20 62 72 6f  af nodes are bro
33559 6b 65 6e 20 69 6e 74 6f 20 62 6c 6f 63 6b 73 20  ken into blocks 
3355a 77 68 69 63 68 20 61 72 65 20 73 74 6f 72 65 64  which are stored
3355b 20 63 6f 6e 74 69 67 75 6f 75 73 6c 79 20 69 6e   contiguously in
3355c 0a 2a 2a 20 74 68 65 20 25 5f 73 65 67 6d 65 6e  .** the %_segmen
3355d 74 73 20 74 61 62 6c 65 20 69 6e 20 73 6f 72 74  ts table in sort
3355e 65 64 20 6f 72 64 65 72 2e 20 20 54 68 69 73 20  ed order.  This 
3355f 6d 65 61 6e 73 20 74 68 61 74 20 77 68 65 6e 20  means that when 
33560 74 68 65 20 65 6e 64 0a 2a 2a 20 6f 66 20 61 20  the end.** of a 
33561 6e 6f 64 65 20 69 73 20 72 65 61 63 68 65 64 2c  node is reached,
33562 20 74 68 65 20 6e 65 78 74 20 74 65 72 6d 20 69   the next term i
33563 73 20 69 6e 20 74 68 65 20 6e 6f 64 65 20 77 69  s in the node wi
33564 74 68 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 67  th the next.** g
33565 72 65 61 74 65 72 20 6e 6f 64 65 20 69 64 2e 0a  reater node id..
33566 2a 2a 0a 2a 2a 20 4e 65 77 20 64 61 74 61 20 69  **.** New data i
33567 73 20 73 70 69 6c 6c 65 64 20 74 6f 20 61 20 6e  s spilled to a n
33568 65 77 20 6c 65 61 66 20 6e 6f 64 65 20 77 68 65  ew leaf node whe
33569 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 6e 6f  n the current no
3356a 64 65 0a 2a 2a 20 65 78 63 65 65 64 73 20 4c 45  de.** exceeds LE
3356b 41 46 5f 4d 41 58 20 62 79 74 65 73 20 28 64 65  AF_MAX bytes (de
3356c 66 61 75 6c 74 20 32 30 34 38 29 2e 20 20 4e 65  fault 2048).  Ne
3356d 77 20 64 61 74 61 20 77 68 69 63 68 20 69 74 73  w data which its
3356e 65 6c 66 20 69 73 0a 2a 2a 20 6c 61 72 67 65 72  elf is.** larger
3356f 20 74 68 61 6e 20 53 54 41 4e 44 41 4c 4f 4e 45   than STANDALONE
33570 5f 4d 49 4e 20 28 64 65 66 61 75 6c 74 20 31 30  _MIN (default 10
33571 32 34 29 20 69 73 20 70 6c 61 63 65 64 20 69 6e  24) is placed in
33572 20 61 20 73 74 61 6e 64 61 6c 6f 6e 65 0a 2a 2a   a standalone.**
33573 20 6e 6f 64 65 20 28 61 20 6c 65 61 66 20 6e 6f   node (a leaf no
33574 64 65 20 77 69 74 68 20 61 20 73 69 6e 67 6c 65  de with a single
33575 20 74 65 72 6d 20 61 6e 64 20 64 6f 63 6c 69 73   term and doclis
33576 74 29 2e 20 20 54 68 65 20 67 6f 61 6c 20 6f 66  t).  The goal of
33577 0a 2a 2a 20 74 68 65 73 65 20 73 65 74 74 69 6e  .** these settin
33578 67 73 20 69 73 20 74 6f 20 70 61 63 6b 20 74 6f  gs is to pack to
33579 67 65 74 68 65 72 20 67 72 6f 75 70 73 20 6f 66  gether groups of
3357a 20 73 6d 61 6c 6c 20 64 6f 63 6c 69 73 74 73 20   small doclists 
3357b 77 68 69 6c 65 0a 2a 2a 20 6d 61 6b 69 6e 67 20  while.** making 
3357c 69 74 20 65 66 66 69 63 69 65 6e 74 20 74 6f 20  it efficient to 
3357d 64 69 72 65 63 74 6c 79 20 61 63 63 65 73 73 20  directly access 
3357e 6c 61 72 67 65 20 64 6f 63 6c 69 73 74 73 2e 20  large doclists. 
3357f 20 54 68 65 0a 2a 2a 20 61 73 73 75 6d 70 74 69   The.** assumpti
33580 6f 6e 20 69 73 20 74 68 61 74 20 6c 61 72 67 65  on is that large
33581 20 64 6f 63 6c 69 73 74 73 20 72 65 70 72 65 73   doclists repres
33582 65 6e 74 20 74 65 72 6d 73 20 77 68 69 63 68 20  ent terms which 
33583 61 72 65 20 6d 6f 72 65 0a 2a 2a 20 6c 69 6b 65  are more.** like
33584 6c 79 20 74 6f 20 62 65 20 71 75 65 72 79 20 74  ly to be query t
33585 61 72 67 65 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 4f  argets..**.** TO
33586 44 4f 28 73 68 65 73 73 29 20 49 74 20 6d 61 79  DO(shess) It may
33587 20 62 65 20 75 73 65 66 75 6c 20 66 6f 72 20 62   be useful for b
33588 6c 6f 63 6b 69 6e 67 20 64 65 63 69 73 69 6f 6e  locking decision
33589 73 20 74 6f 20 62 65 20 6d 6f 72 65 0a 2a 2a 20  s to be more.** 
3358a 64 79 6e 61 6d 69 63 2e 20 20 46 6f 72 20 69 6e  dynamic.  For in
3358b 73 74 61 6e 63 65 2c 20 69 74 20 6d 61 79 20 6d  stance, it may m
3358c 61 6b 65 20 6d 6f 72 65 20 73 65 6e 73 65 20 74  ake more sense t
3358d 6f 20 68 61 76 65 20 61 20 32 2e 35 6b 20 6c 65  o have a 2.5k le
3358e 61 66 0a 2a 2a 20 6e 6f 64 65 20 72 61 74 68 65  af.** node rathe
3358f 72 20 74 68 61 6e 20 73 70 6c 69 74 74 69 6e 67  r than splitting
33590 20 69 6e 74 6f 20 32 6b 20 61 6e 64 20 2e 35 6b   into 2k and .5k
33591 20 6e 6f 64 65 73 2e 20 20 4d 79 20 69 6e 74 75   nodes.  My intu
33592 69 74 69 6f 6e 20 69 73 0a 2a 2a 20 74 68 61 74  ition is.** that
33593 20 74 68 69 73 20 6d 69 67 68 74 20 65 78 74 65   this might exte
33594 6e 64 20 74 68 72 6f 75 67 68 20 32 78 20 6f 72  nd through 2x or
33595 20 34 78 20 74 68 65 20 70 61 67 65 73 69 7a 65   4x the pagesize
33596 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 53 65 67  ..**.**.**** Seg
33597 6d 65 6e 74 20 69 6e 74 65 72 69 6f 72 20 6e 6f  ment interior no
33598 64 65 73 20 2a 2a 2a 2a 0a 2a 2a 20 53 65 67 6d  des ****.** Segm
33599 65 6e 74 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64  ent interior nod
3359a 65 73 20 73 74 6f 72 65 20 62 6c 6f 63 6b 69 64  es store blockid
3359b 73 20 66 6f 72 20 73 75 62 74 72 65 65 20 6e 6f  s for subtree no
3359c 64 65 73 20 61 6e 64 20 74 65 72 6d 73 0a 2a 2a  des and terms.**
3359d 20 74 6f 20 64 65 73 63 72 69 62 65 20 77 68 61   to describe wha
3359e 74 20 64 61 74 61 20 69 73 20 73 74 6f 72 65 64  t data is stored
3359f 20 62 79 20 74 68 65 20 65 61 63 68 20 73 75 62   by the each sub
335a0 74 72 65 65 2e 20 20 49 6e 74 65 72 69 6f 72 0a  tree.  Interior.
335a1 2a 2a 20 6e 6f 64 65 73 20 61 72 65 20 77 72 69  ** nodes are wri
335a2 74 74 65 6e 20 75 73 69 6e 67 20 49 6e 74 65 72  tten using Inter
335a3 69 6f 72 57 72 69 74 65 72 2c 20 61 6e 64 20 72  iorWriter, and r
335a4 65 61 64 20 75 73 69 6e 67 0a 2a 2a 20 49 6e 74  ead using.** Int
335a5 65 72 69 6f 72 52 65 61 64 65 72 2e 20 20 49 6e  eriorReader.  In
335a6 74 65 72 69 6f 72 57 72 69 74 65 72 73 20 61 72  teriorWriters ar
335a7 65 20 63 72 65 61 74 65 64 20 61 73 20 6e 65 65  e created as nee
335a8 64 65 64 20 77 68 65 6e 0a 2a 2a 20 53 65 67 6d  ded when.** Segm
335a9 65 6e 74 57 72 69 74 65 72 20 63 72 65 61 74 65  entWriter create
335aa 73 20 6e 65 77 20 6c 65 61 66 20 6e 6f 64 65 73  s new leaf nodes
335ab 2c 20 6f 72 20 77 68 65 6e 20 61 6e 20 69 6e 74  , or when an int
335ac 65 72 69 6f 72 20 6e 6f 64 65 0a 2a 2a 20 69 74  erior node.** it
335ad 73 65 6c 66 20 67 72 6f 77 73 20 74 6f 6f 20 62  self grows too b
335ae 69 67 20 61 6e 64 20 6d 75 73 74 20 62 65 20 73  ig and must be s
335af 70 6c 69 74 2e 20 20 54 68 65 20 66 6f 72 6d 61  plit.  The forma
335b0 74 20 6f 66 20 69 6e 74 65 72 69 6f 72 0a 2a 2a  t of interior.**
335b1 20 6e 6f 64 65 73 3a 0a 2a 2a 0a 2a 2a 20 76 61   nodes:.**.** va
335b2 72 69 6e 74 20 69 48 65 69 67 68 74 3b 20 20 20  rint iHeight;   
335b3 20 20 20 20 20 20 20 20 28 68 65 69 67 68 74 20          (height 
335b4 66 72 6f 6d 20 6c 65 61 66 20 6c 65 76 65 6c 2c  from leaf level,
335b5 20 61 6c 77 61 79 73 20 3e 30 29 0a 2a 2a 20 76   always >0).** v
335b6 61 72 69 6e 74 20 69 42 6c 6f 63 6b 69 64 3b 20  arint iBlockid; 
335b7 20 20 20 20 20 20 20 20 20 28 62 6c 6f 63 6b 20           (block 
335b8 69 64 20 6f 66 20 6e 6f 64 65 27 73 20 6c 65 66  id of node's lef
335b9 74 6d 6f 73 74 20 73 75 62 74 72 65 65 29 0a 2a  tmost subtree).*
335ba 2a 20 6f 70 74 69 6f 6e 61 6c 20 7b 0a 2a 2a 20  * optional {.** 
335bb 20 20 76 61 72 69 6e 74 20 6e 54 65 72 6d 3b 20    varint nTerm; 
335bc 20 20 20 20 20 20 20 20 20 20 28 6c 65 6e 67 74            (lengt
335bd 68 20 6f 66 20 66 69 72 73 74 20 74 65 72 6d 29  h of first term)
335be 0a 2a 2a 20 20 20 63 68 61 72 20 70 54 65 72 6d  .**   char pTerm
335bf 5b 6e 54 65 72 6d 5d 3b 20 20 20 20 20 20 28 63  [nTerm];      (c
335c0 6f 6e 74 65 6e 74 20 6f 66 20 66 69 72 73 74 20  ontent of first 
335c1 74 65 72 6d 29 0a 2a 2a 20 20 20 61 72 72 61 79  term).**   array
335c2 20 7b 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20   {.**           
335c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
335c4 20 20 20 20 20 28 66 75 72 74 68 65 72 20 74 65       (further te
335c5 72 6d 73 20 61 72 65 20 64 65 6c 74 61 2d 65 6e  rms are delta-en
335c6 63 6f 64 65 64 29 0a 2a 2a 20 20 20 20 20 76 61  coded).**     va
335c7 72 69 6e 74 20 6e 50 72 65 66 69 78 3b 20 20 20  rint nPrefix;   
335c8 20 20 20 20 20 20 20 20 20 28 6c 65 6e 67 74 68           (length
335c9 20 6f 66 20 73 68 61 72 65 64 20 70 72 65 66 69   of shared prefi
335ca 78 20 77 69 74 68 20 70 72 65 76 69 6f 75 73 20  x with previous 
335cb 74 65 72 6d 29 0a 2a 2a 20 20 20 20 20 76 61 72  term).**     var
335cc 69 6e 74 20 6e 53 75 66 66 69 78 3b 20 20 20 20  int nSuffix;    
335cd 20 20 20 20 20 20 20 20 28 6c 65 6e 67 74 68 20          (length 
335ce 6f 66 20 75 6e 73 68 61 72 65 64 20 73 75 66 66  of unshared suff
335cf 69 78 29 0a 2a 2a 20 20 20 20 20 63 68 61 72 20  ix).**     char 
335d0 70 54 65 72 6d 53 75 66 66 69 78 5b 6e 53 75 66  pTermSuffix[nSuf
335d1 66 69 78 5d 3b 20 28 75 6e 73 68 61 72 65 64 20  fix]; (unshared 
335d2 73 75 66 66 69 78 20 6f 66 20 6e 65 78 74 20 74  suffix of next t
335d3 65 72 6d 29 0a 2a 2a 20 20 20 7d 0a 2a 2a 20 7d  erm).**   }.** }
335d4 0a 2a 2a 0a 2a 2a 20 48 65 72 65 2c 20 6f 70 74  .**.** Here, opt
335d5 69 6f 6e 61 6c 20 7b 20 58 20 7d 20 6d 65 61 6e  ional { X } mean
335d6 73 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 65 6c  s an optional el
335d7 65 6d 65 6e 74 2c 20 77 68 69 6c 65 20 61 72 72  ement, while arr
335d8 61 79 20 7b 20 58 20 7d 0a 2a 2a 20 6d 65 61 6e  ay { X }.** mean
335d9 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 6f  s zero or more o
335da 63 63 75 72 72 65 6e 63 65 73 20 6f 66 20 58 2c  ccurrences of X,
335db 20 61 64 6a 61 63 65 6e 74 20 69 6e 20 6d 65 6d   adjacent in mem
335dc 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e  ory..**.** An in
335dd 74 65 72 69 6f 72 20 6e 6f 64 65 20 65 6e 63 6f  terior node enco
335de 64 65 73 20 6e 20 74 65 72 6d 73 20 73 65 70 61  des n terms sepa
335df 72 61 74 69 6e 67 20 6e 2b 31 20 73 75 62 74 72  rating n+1 subtr
335e0 65 65 73 2e 20 20 54 68 65 0a 2a 2a 20 73 75 62  ees.  The.** sub
335e1 74 72 65 65 20 62 6c 6f 63 6b 73 20 61 72 65 20  tree blocks are 
335e2 63 6f 6e 74 69 67 75 6f 75 73 2c 20 73 6f 20 6f  contiguous, so o
335e3 6e 6c 79 20 74 68 65 20 66 69 72 73 74 20 73 75  nly the first su
335e4 62 74 72 65 65 27 73 20 62 6c 6f 63 6b 69 64 0a  btree's blockid.
335e5 2a 2a 20 69 73 20 65 6e 63 6f 64 65 64 2e 20 20  ** is encoded.  
335e6 54 68 65 20 73 75 62 74 72 65 65 20 61 74 20 69  The subtree at i
335e7 42 6c 6f 63 6b 69 64 20 77 69 6c 6c 20 63 6f 6e  Blockid will con
335e8 74 61 69 6e 20 61 6c 6c 20 74 65 72 6d 73 20 6c  tain all terms l
335e9 65 73 73 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20  ess.** than the 
335ea 66 69 72 73 74 20 74 65 72 6d 20 65 6e 63 6f 64  first term encod
335eb 65 64 20 28 6f 72 20 61 6c 6c 20 74 65 72 6d 73  ed (or all terms
335ec 20 69 66 20 6e 6f 20 74 65 72 6d 20 69 73 20 65   if no term is e
335ed 6e 63 6f 64 65 64 29 2e 0a 2a 2a 20 4f 74 68 65  ncoded)..** Othe
335ee 72 77 69 73 65 2c 20 66 6f 72 20 74 65 72 6d 73  rwise, for terms
335ef 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72   greater than or
335f0 20 65 71 75 61 6c 20 74 6f 20 70 54 65 72 6d 5b   equal to pTerm[
335f1 69 5d 20 62 75 74 20 6c 65 73 73 0a 2a 2a 20 74  i] but less.** t
335f2 68 61 6e 20 70 54 65 72 6d 5b 69 2b 31 5d 2c 20  han pTerm[i+1], 
335f3 74 68 65 20 73 75 62 74 72 65 65 20 66 6f 72 20  the subtree for 
335f4 74 68 61 74 20 74 65 72 6d 20 77 69 6c 6c 20 62  that term will b
335f5 65 20 72 6f 6f 74 65 64 20 61 74 0a 2a 2a 20 69  e rooted at.** i
335f6 42 6c 6f 63 6b 69 64 2b 69 2e 20 20 49 6e 74 65  Blockid+i.  Inte
335f7 72 69 6f 72 20 6e 6f 64 65 73 20 6f 6e 6c 79 20  rior nodes only 
335f8 73 74 6f 72 65 20 65 6e 6f 75 67 68 20 74 65 72  store enough ter
335f9 6d 20 64 61 74 61 20 74 6f 0a 2a 2a 20 64 69 73  m data to.** dis
335fa 74 69 6e 67 75 69 73 68 20 61 64 6a 61 63 65 6e  tinguish adjacen
335fb 74 20 63 68 69 6c 64 72 65 6e 20 28 69 66 20 74  t children (if t
335fc 68 65 20 72 69 67 68 74 6d 6f 73 74 20 74 65 72  he rightmost ter
335fd 6d 20 6f 66 20 74 68 65 20 6c 65 66 74 0a 2a 2a  m of the left.**
335fe 20 63 68 69 6c 64 20 69 73 20 22 73 6f 6d 65 74   child is "somet
335ff 68 69 6e 67 22 2c 20 61 6e 64 20 74 68 65 20 6c  hing", and the l
33600 65 66 74 6d 6f 73 74 20 74 65 72 6d 20 6f 66 20  eftmost term of 
33601 74 68 65 20 72 69 67 68 74 20 63 68 69 6c 64 20  the right child 
33602 69 73 0a 2a 2a 20 22 77 69 63 6b 65 64 22 2c 20  is.** "wicked", 
33603 6f 6e 6c 79 20 22 77 22 20 69 73 20 73 74 6f 72  only "w" is stor
33604 65 64 29 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 64  ed)..**.** New d
33605 61 74 61 20 69 73 20 73 70 69 6c 6c 65 64 20 74  ata is spilled t
33606 6f 20 61 20 6e 65 77 20 69 6e 74 65 72 69 6f 72  o a new interior
33607 20 6e 6f 64 65 20 61 74 20 74 68 65 20 73 61 6d   node at the sam
33608 65 20 68 65 69 67 68 74 20 77 68 65 6e 0a 2a 2a  e height when.**
33609 20 74 68 65 20 63 75 72 72 65 6e 74 20 6e 6f 64   the current nod
3360a 65 20 65 78 63 65 65 64 73 20 49 4e 54 45 52 49  e exceeds INTERI
3360b 4f 52 5f 4d 41 58 20 62 79 74 65 73 20 28 64 65  OR_MAX bytes (de
3360c 66 61 75 6c 74 20 32 30 34 38 29 2e 0a 2a 2a 20  fault 2048)..** 
3360d 49 4e 54 45 52 49 4f 52 5f 4d 49 4e 5f 54 45 52  INTERIOR_MIN_TER
3360e 4d 53 20 28 64 65 66 61 75 6c 74 20 37 29 20 6b  MS (default 7) k
3360f 65 65 70 73 20 6c 61 72 67 65 20 74 65 72 6d 73  eeps large terms
33610 20 66 72 6f 6d 20 6d 6f 6e 6f 70 6f 6c 69 7a 69   from monopolizi
33611 6e 67 0a 2a 2a 20 69 6e 74 65 72 69 6f 72 20 6e  ng.** interior n
33612 6f 64 65 73 20 61 6e 64 20 6d 61 6b 69 6e 67 20  odes and making 
33613 74 68 65 20 74 72 65 65 20 74 6f 6f 20 73 6b 69  the tree too ski
33614 6e 6e 79 2e 20 20 54 68 65 20 69 6e 74 65 72 69  nny.  The interi
33615 6f 72 20 6e 6f 64 65 73 0a 2a 2a 20 61 74 20 61  or nodes.** at a
33616 20 67 69 76 65 6e 20 68 65 69 67 68 74 20 61 72   given height ar
33617 65 20 6e 61 74 75 72 61 6c 6c 79 20 74 72 61 63  e naturally trac
33618 6b 65 64 20 62 79 20 69 6e 74 65 72 69 6f 72 20  ked by interior 
33619 6e 6f 64 65 73 20 61 74 0a 2a 2a 20 68 65 69 67  nodes at.** heig
3361a 68 74 2b 31 2c 20 61 6e 64 20 73 6f 20 6f 6e 2e  ht+1, and so on.
3361b 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 53 65 67 6d  .**.**.**** Segm
3361c 65 6e 74 20 64 69 72 65 63 74 6f 72 79 20 2a 2a  ent directory **
3361d 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 67 6d 65 6e  **.** The segmen
3361e 74 20 64 69 72 65 63 74 6f 72 79 20 69 6e 20 74  t directory in t
3361f 61 62 6c 65 20 25 5f 73 65 67 64 69 72 20 73 74  able %_segdir st
33620 6f 72 65 73 20 6d 65 74 61 2d 69 6e 66 6f 72 6d  ores meta-inform
33621 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 6d 65 72  ation for.** mer
33622 67 69 6e 67 20 61 6e 64 20 64 65 6c 65 74 69 6e  ging and deletin
33623 67 20 73 65 67 6d 65 6e 74 73 2c 20 61 6e 64 20  g segments, and 
33624 61 6c 73 6f 20 74 68 65 20 72 6f 6f 74 20 6e 6f  also the root no
33625 64 65 20 6f 66 20 74 68 65 0a 2a 2a 20 73 65 67  de of the.** seg
33626 6d 65 6e 74 27 73 20 74 72 65 65 2e 0a 2a 2a 0a  ment's tree..**.
33627 2a 2a 20 54 68 65 20 72 6f 6f 74 20 6e 6f 64 65  ** The root node
33628 20 69 73 20 74 68 65 20 74 6f 70 20 6e 6f 64 65   is the top node
33629 20 6f 66 20 74 68 65 20 73 65 67 6d 65 6e 74 27   of the segment'
3362a 73 20 74 72 65 65 20 61 66 74 65 72 20 65 6e 63  s tree after enc
3362b 6f 64 69 6e 67 0a 2a 2a 20 74 68 65 20 65 6e 74  oding.** the ent
3362c 69 72 65 20 73 65 67 6d 65 6e 74 2c 20 72 65 73  ire segment, res
3362d 74 72 69 63 74 65 64 20 74 6f 20 52 4f 4f 54 5f  tricted to ROOT_
3362e 4d 41 58 20 62 79 74 65 73 20 28 64 65 66 61 75  MAX bytes (defau
3362f 6c 74 20 31 30 32 34 29 2e 0a 2a 2a 20 54 68 69  lt 1024)..** Thi
33630 73 20 63 6f 75 6c 64 20 62 65 20 65 69 74 68 65  s could be eithe
33631 72 20 61 20 6c 65 61 66 20 6e 6f 64 65 20 6f 72  r a leaf node or
33632 20 61 6e 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64   an interior nod
33633 65 2e 20 20 49 66 20 74 68 65 20 74 6f 70 0a 2a  e.  If the top.*
33634 2a 20 6e 6f 64 65 20 72 65 71 75 69 72 65 73 20  * node requires 
33635 6d 6f 72 65 20 74 68 61 6e 20 52 4f 4f 54 5f 4d  more than ROOT_M
33636 41 58 20 62 79 74 65 73 2c 20 69 74 20 69 73 20  AX bytes, it is 
33637 66 6c 75 73 68 65 64 20 74 6f 20 25 5f 73 65 67  flushed to %_seg
33638 6d 65 6e 74 73 0a 2a 2a 20 61 6e 64 20 61 20 6e  ments.** and a n
33639 65 77 20 72 6f 6f 74 20 69 6e 74 65 72 69 6f 72  ew root interior
3363a 20 6e 6f 64 65 20 69 73 20 67 65 6e 65 72 61 74   node is generat
3363b 65 64 20 28 77 68 69 63 68 20 73 68 6f 75 6c 64  ed (which should
3363c 20 61 6c 77 61 79 73 20 66 69 74 0a 2a 2a 20 77   always fit.** w
3363d 69 74 68 69 6e 20 52 4f 4f 54 5f 4d 41 58 20 62  ithin ROOT_MAX b
3363e 65 63 61 75 73 65 20 69 74 20 6f 6e 6c 79 20 6e  ecause it only n
3363f 65 65 64 73 20 73 70 61 63 65 20 66 6f 72 20 32  eeds space for 2
33640 20 76 61 72 69 6e 74 73 2c 20 74 68 65 0a 2a 2a   varints, the.**
33641 20 68 65 69 67 68 74 20 61 6e 64 20 74 68 65 20   height and the 
33642 62 6c 6f 63 6b 69 64 20 6f 66 20 74 68 65 20 70  blockid of the p
33643 72 65 76 69 6f 75 73 20 72 6f 6f 74 29 2e 0a 2a  revious root)..*
33644 2a 0a 2a 2a 20 54 68 65 20 6d 65 74 61 2d 69 6e  *.** The meta-in
33645 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 74 68 65  formation in the
33646 20 73 65 67 6d 65 6e 74 20 64 69 72 65 63 74 6f   segment directo
33647 72 79 20 69 73 3a 0a 2a 2a 20 20 20 6c 65 76 65  ry is:.**   leve
33648 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l               
33649 2d 20 73 65 67 6d 65 6e 74 20 6c 65 76 65 6c 20  - segment level 
3364a 28 73 65 65 20 62 65 6c 6f 77 29 0a 2a 2a 20 20  (see below).**  
3364b 20 69 64 78 20 20 20 20 20 20 20 20 20 20 20 20   idx            
3364c 20 20 20 20 20 2d 20 69 6e 64 65 78 20 77 69 74       - index wit
3364d 68 69 6e 20 6c 65 76 65 6c 0a 2a 2a 20 20 20 20  hin level.**    
3364e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3364f 20 20 20 2d 20 28 6c 65 76 65 6c 2c 69 64 78 20     - (level,idx 
33650 75 6e 69 71 75 65 6c 79 20 69 64 65 6e 74 69 66  uniquely identif
33651 79 20 61 20 73 65 67 6d 65 6e 74 29 0a 2a 2a 20  y a segment).** 
33652 20 20 73 74 61 72 74 5f 62 6c 6f 63 6b 20 20 20    start_block   
33653 20 20 20 20 20 20 2d 20 66 69 72 73 74 20 6c 65        - first le
33654 61 66 20 6e 6f 64 65 0a 2a 2a 20 20 20 6c 65 61  af node.**   lea
33655 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 20 20 20  ves_end_block   
33656 20 2d 20 6c 61 73 74 20 6c 65 61 66 20 6e 6f 64   - last leaf nod
33657 65 0a 2a 2a 20 20 20 65 6e 64 5f 62 6c 6f 63 6b  e.**   end_block
33658 20 20 20 20 20 20 20 20 20 20 20 2d 20 6c 61 73             - las
33659 74 20 62 6c 6f 63 6b 20 28 69 6e 63 6c 75 64 69  t block (includi
3365a 6e 67 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  ng interior node
3365b 73 29 0a 2a 2a 20 20 20 72 6f 6f 74 20 20 20 20  s).**   root    
3365c 20 20 20 20 20 20 20 20 20 20 20 20 2d 20 63 6f              - co
3365d 6e 74 65 6e 74 73 20 6f 66 20 72 6f 6f 74 20 6e  ntents of root n
3365e 6f 64 65 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ode.**.** If the
3365f 20 72 6f 6f 74 20 6e 6f 64 65 20 69 73 20 61 20   root node is a 
33660 6c 65 61 66 20 6e 6f 64 65 2c 20 74 68 65 6e 20  leaf node, then 
33661 73 74 61 72 74 5f 62 6c 6f 63 6b 2c 0a 2a 2a 20  start_block,.** 
33662 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b  leaves_end_block
33663 2c 20 61 6e 64 20 65 6e 64 5f 62 6c 6f 63 6b 20  , and end_block 
33664 61 72 65 20 61 6c 6c 20 30 2e 0a 2a 2a 0a 2a 2a  are all 0..**.**
33665 0a 2a 2a 2a 2a 20 53 65 67 6d 65 6e 74 20 6d 65  .**** Segment me
33666 72 67 69 6e 67 20 2a 2a 2a 2a 0a 2a 2a 20 54 6f  rging ****.** To
33667 20 61 6d 6f 72 74 69 7a 65 20 75 70 64 61 74 65   amortize update
33668 20 63 6f 73 74 73 2c 20 73 65 67 6d 65 6e 74 73   costs, segments
33669 20 61 72 65 20 67 72 6f 75 70 65 64 20 69 6e 74   are grouped int
3366a 6f 20 6c 65 76 65 6c 73 20 61 6e 64 0a 2a 2a 20  o levels and.** 
3366b 6d 65 72 67 65 64 20 69 6e 20 62 61 74 63 68 65  merged in batche
3366c 73 2e 20 20 45 61 63 68 20 69 6e 63 72 65 61 73  s.  Each increas
3366d 65 20 69 6e 20 6c 65 76 65 6c 20 72 65 70 72 65  e in level repre
3366e 73 65 6e 74 73 20 65 78 70 6f 6e 65 6e 74 69 61  sents exponentia
3366f 6c 6c 79 0a 2a 2a 20 6d 6f 72 65 20 64 6f 63 75  lly.** more docu
33670 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77  ments..**.** New
33671 20 64 6f 63 75 6d 65 6e 74 73 20 28 61 63 74 75   documents (actu
33672 61 6c 6c 79 2c 20 64 6f 63 75 6d 65 6e 74 20 75  ally, document u
33673 70 64 61 74 65 73 29 20 61 72 65 20 74 6f 6b 65  pdates) are toke
33674 6e 69 7a 65 64 20 61 6e 64 0a 2a 2a 20 77 72 69  nized and.** wri
33675 74 74 65 6e 20 69 6e 64 69 76 69 64 75 61 6c 6c  tten individuall
33676 79 20 28 75 73 69 6e 67 20 4c 65 61 66 57 72 69  y (using LeafWri
33677 74 65 72 29 20 74 6f 20 61 20 6c 65 76 65 6c 20  ter) to a level 
33678 30 20 73 65 67 6d 65 6e 74 2c 20 77 69 74 68 0a  0 segment, with.
33679 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20  ** incrementing 
3367a 69 64 78 2e 20 20 57 68 65 6e 20 69 64 78 20 72  idx.  When idx r
3367b 65 61 63 68 65 73 20 4d 45 52 47 45 5f 43 4f 55  eaches MERGE_COU
3367c 4e 54 20 28 64 65 66 61 75 6c 74 20 31 36 29 2c  NT (default 16),
3367d 20 61 6c 6c 0a 2a 2a 20 6c 65 76 65 6c 20 30 20   all.** level 0 
3367e 73 65 67 6d 65 6e 74 73 20 61 72 65 20 6d 65 72  segments are mer
3367f 67 65 64 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c  ged into a singl
33680 65 20 6c 65 76 65 6c 20 31 20 73 65 67 6d 65 6e  e level 1 segmen
33681 74 2e 20 20 4c 65 76 65 6c 20 31 0a 2a 2a 20 69  t.  Level 1.** i
33682 73 20 70 6f 70 75 6c 61 74 65 64 20 6c 69 6b 65  s populated like
33683 20 6c 65 76 65 6c 20 30 2c 20 61 6e 64 20 65 76   level 0, and ev
33684 65 6e 74 75 61 6c 6c 79 20 4d 45 52 47 45 5f 43  entually MERGE_C
33685 4f 55 4e 54 20 6c 65 76 65 6c 20 31 0a 2a 2a 20  OUNT level 1.** 
33686 73 65 67 6d 65 6e 74 73 20 61 72 65 20 6d 65 72  segments are mer
33687 67 65 64 20 74 6f 20 61 20 73 69 6e 67 6c 65 20  ged to a single 
33688 6c 65 76 65 6c 20 32 20 73 65 67 6d 65 6e 74 20  level 2 segment 
33689 28 72 65 70 72 65 73 65 6e 74 69 6e 67 0a 2a 2a  (representing.**
3368a 20 4d 45 52 47 45 5f 43 4f 55 4e 54 5e 32 20 75   MERGE_COUNT^2 u
3368b 70 64 61 74 65 73 29 2c 20 61 6e 64 20 73 6f 20  pdates), and so 
3368c 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 65 67 6d  on..**.** A segm
3368d 65 6e 74 20 6d 65 72 67 65 20 74 72 61 76 65 72  ent merge traver
3368e 73 65 73 20 61 6c 6c 20 73 65 67 6d 65 6e 74 73  ses all segments
3368f 20 61 74 20 61 20 67 69 76 65 6e 20 6c 65 76 65   at a given leve
33690 6c 20 69 6e 0a 2a 2a 20 70 61 72 61 6c 6c 65 6c  l in.** parallel
33691 2c 20 70 65 72 66 6f 72 6d 69 6e 67 20 61 20 73  , performing a s
33692 74 72 61 69 67 68 74 66 6f 72 77 61 72 64 20 73  traightforward s
33693 6f 72 74 65 64 20 6d 65 72 67 65 2e 20 20 53 69  orted merge.  Si
33694 6e 63 65 20 73 65 67 6d 65 6e 74 0a 2a 2a 20 6c  nce segment.** l
33695 65 61 66 20 6e 6f 64 65 73 20 61 72 65 20 77 72  eaf nodes are wr
33696 69 74 74 65 6e 20 69 6e 20 74 6f 20 74 68 65 20  itten in to the 
33697 25 5f 73 65 67 6d 65 6e 74 73 20 74 61 62 6c 65  %_segments table
33698 20 69 6e 20 6f 72 64 65 72 2c 20 74 68 69 73 0a   in order, this.
33699 2a 2a 20 6d 65 72 67 65 20 74 72 61 76 65 72 73  ** merge travers
3369a 65 73 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e  es the underlyin
3369b 67 20 73 71 6c 69 74 65 20 64 69 73 6b 20 73 74  g sqlite disk st
3369c 72 75 63 74 75 72 65 73 20 65 66 66 69 63 69 65  ructures efficie
3369d 6e 74 6c 79 2e 0a 2a 2a 20 41 66 74 65 72 20 74  ntly..** After t
3369e 68 65 20 6d 65 72 67 65 2c 20 61 6c 6c 20 73 65  he merge, all se
3369f 67 6d 65 6e 74 20 62 6c 6f 63 6b 73 20 66 72 6f  gment blocks fro
336a0 6d 20 74 68 65 20 6d 65 72 67 65 64 20 6c 65 76  m the merged lev
336a1 65 6c 20 61 72 65 0a 2a 2a 20 64 65 6c 65 74 65  el are.** delete
336a2 64 2e 0a 2a 2a 0a 2a 2a 20 4d 45 52 47 45 5f 43  d..**.** MERGE_C
336a3 4f 55 4e 54 20 63 6f 6e 74 72 6f 6c 73 20 68 6f  OUNT controls ho
336a4 77 20 6f 66 74 65 6e 20 77 65 20 6d 65 72 67 65  w often we merge
336a5 20 73 65 67 6d 65 6e 74 73 2e 20 20 31 36 20 73   segments.  16 s
336a6 65 65 6d 73 20 74 6f 20 62 65 0a 2a 2a 20 73 6f  eems to be.** so
336a7 6d 65 77 68 61 74 20 6f 66 20 61 20 73 77 65 65  mewhat of a swee
336a8 74 20 73 70 6f 74 20 66 6f 72 20 69 6e 73 65 72  t spot for inser
336a9 74 69 6f 6e 20 70 65 72 66 6f 72 6d 61 6e 63 65  tion performance
336aa 2e 20 20 33 32 20 61 6e 64 20 36 34 20 73 68 6f  .  32 and 64 sho
336ab 77 0a 2a 2a 20 76 65 72 79 20 73 69 6d 69 6c 61  w.** very simila
336ac 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 6e 75  r performance nu
336ad 6d 62 65 72 73 20 74 6f 20 31 36 20 6f 6e 20 69  mbers to 16 on i
336ae 6e 73 65 72 74 69 6f 6e 2c 20 74 68 6f 75 67 68  nsertion, though
336af 20 74 68 65 79 27 72 65 0a 2a 2a 20 61 20 74 69   they're.** a ti
336b0 6e 79 20 62 69 74 20 73 6c 6f 77 65 72 20 28 70  ny bit slower (p
336b1 65 72 68 61 70 73 20 64 75 65 20 74 6f 20 6d 6f  erhaps due to mo
336b2 72 65 20 6f 76 65 72 68 65 61 64 20 69 6e 20 6d  re overhead in m
336b3 65 72 67 65 2d 74 69 6d 65 0a 2a 2a 20 73 6f 72  erge-time.** sor
336b4 74 69 6e 67 29 2e 20 20 38 20 69 73 20 61 62 6f  ting).  8 is abo
336b5 75 74 20 32 30 25 20 73 6c 6f 77 65 72 20 74 68  ut 20% slower th
336b6 61 6e 20 31 36 2c 20 34 20 61 62 6f 75 74 20 35  an 16, 4 about 5
336b7 30 25 20 73 6c 6f 77 65 72 20 74 68 61 6e 0a 2a  0% slower than.*
336b8 2a 20 31 36 2c 20 32 20 61 62 6f 75 74 20 36 36  * 16, 2 about 66
336b9 25 20 73 6c 6f 77 65 72 20 74 68 61 6e 20 31 36  % slower than 16
336ba 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 71 75 65 72 79  ..**.** At query
336bb 20 74 69 6d 65 2c 20 68 69 67 68 20 4d 45 52 47   time, high MERG
336bc 45 5f 43 4f 55 4e 54 20 69 6e 63 72 65 61 73 65  E_COUNT increase
336bd 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
336be 73 65 67 6d 65 6e 74 73 0a 2a 2a 20 77 68 69 63  segments.** whic
336bf 68 20 6e 65 65 64 20 74 6f 20 62 65 20 73 63 61  h need to be sca
336c0 6e 6e 65 64 20 61 6e 64 20 6d 65 72 67 65 64 2e  nned and merged.
336c1 20 20 46 6f 72 20 69 6e 73 74 61 6e 63 65 2c 20    For instance, 
336c2 77 69 74 68 20 31 30 30 6b 20 64 6f 63 73 0a 2a  with 100k docs.*
336c3 2a 20 69 6e 73 65 72 74 65 64 3a 0a 2a 2a 0a 2a  * inserted:.**.*
336c4 2a 20 20 20 20 4d 45 52 47 45 5f 43 4f 55 4e 54  *    MERGE_COUNT
336c5 20 20 20 73 65 67 6d 65 6e 74 73 0a 2a 2a 20 20     segments.**  
336c6 20 20 20 20 20 31 36 20 20 20 20 20 20 20 20 20       16         
336c7 20 20 32 35 0a 2a 2a 20 20 20 20 20 20 20 20 38    25.**        8
336c8 20 20 20 20 20 20 20 20 20 20 20 31 32 0a 2a 2a             12.**
336c9 20 20 20 20 20 20 20 20 34 20 20 20 20 20 20 20          4       
336ca 20 20 20 20 31 30 0a 2a 2a 20 20 20 20 20 20 20      10.**       
336cb 20 32 20 20 20 20 20 20 20 20 20 20 20 20 36 0a   2            6.
336cc 2a 2a 0a 2a 2a 20 54 68 69 73 20 61 70 70 65 61  **.** This appea
336cd 72 73 20 74 6f 20 68 61 76 65 20 6f 6e 6c 79 20  rs to have only 
336ce 61 20 6d 6f 64 65 72 61 74 65 20 69 6d 70 61 63  a moderate impac
336cf 74 20 6f 6e 20 71 75 65 72 69 65 73 20 66 6f 72  t on queries for
336d0 20 76 65 72 79 0a 2a 2a 20 66 72 65 71 75 65 6e   very.** frequen
336d1 74 20 74 65 72 6d 73 20 28 77 68 69 63 68 20 61  t terms (which a
336d2 72 65 20 73 6f 6d 65 77 68 61 74 20 64 6f 6d 69  re somewhat domi
336d3 6e 61 74 65 64 20 62 79 20 73 65 67 6d 65 6e 74  nated by segment
336d4 20 6d 65 72 67 65 0a 2a 2a 20 63 6f 73 74 73 29   merge.** costs)
336d5 2c 20 61 6e 64 20 69 6e 66 72 65 71 75 65 6e 74  , and infrequent
336d6 20 61 6e 64 20 6e 6f 6e 2d 65 78 69 73 74 65 6e   and non-existen
336d7 74 20 74 65 72 6d 73 20 73 74 69 6c 6c 20 73 65  t terms still se
336d8 65 6d 20 74 6f 20 62 65 20 66 61 73 74 0a 2a 2a  em to be fast.**
336d9 20 65 76 65 6e 20 77 69 74 68 20 6d 61 6e 79 20   even with many 
336da 73 65 67 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20  segments..**.** 
336db 54 4f 44 4f 28 73 68 65 73 73 29 20 54 68 61 74  TODO(shess) That
336dc 20 73 61 69 64 2c 20 69 74 20 77 6f 75 6c 64 20   said, it would 
336dd 62 65 20 6e 69 63 65 20 74 6f 20 68 61 76 65 20  be nice to have 
336de 61 20 62 65 74 74 65 72 20 71 75 65 72 79 2d 73  a better query-s
336df 69 64 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20  ide.** argument 
336e0 66 6f 72 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20  for MERGE_COUNT 
336e1 6f 66 20 31 36 2e 20 20 41 6c 73 6f 2c 20 69 74  of 16.  Also, it
336e2 20 69 73 20 70 6f 73 73 69 62 6c 65 2f 6c 69 6b   is possible/lik
336e3 65 6c 79 20 74 68 61 74 0a 2a 2a 20 6f 70 74 69  ely that.** opti
336e4 6d 69 7a 61 74 69 6f 6e 73 20 74 6f 20 74 68 69  mizations to thi
336e5 6e 67 73 20 6c 69 6b 65 20 64 6f 63 6c 69 73 74  ngs like doclist
336e6 20 6d 65 72 67 69 6e 67 20 77 69 6c 6c 20 73 77   merging will sw
336e7 69 6e 67 20 74 68 65 20 73 77 65 65 74 0a 2a 2a  ing the sweet.**
336e8 20 73 70 6f 74 20 61 72 6f 75 6e 64 2e 0a 2a 2a   spot around..**
336e9 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 48 61 6e 64  .**.**.**** Hand
336ea 6c 69 6e 67 20 6f 66 20 64 65 6c 65 74 69 6f 6e  ling of deletion
336eb 73 20 61 6e 64 20 75 70 64 61 74 65 73 20 2a 2a  s and updates **
336ec 2a 2a 0a 2a 2a 20 53 69 6e 63 65 20 77 65 27 72  **.** Since we'r
336ed 65 20 75 73 69 6e 67 20 61 20 73 65 67 6d 65 6e  e using a segmen
336ee 74 65 64 20 73 74 72 75 63 74 75 72 65 2c 20 77  ted structure, w
336ef 69 74 68 20 6e 6f 20 64 6f 63 69 64 2d 6f 72 69  ith no docid-ori
336f0 65 6e 74 65 64 0a 2a 2a 20 69 6e 64 65 78 20 69  ented.** index i
336f1 6e 74 6f 20 74 68 65 20 74 65 72 6d 20 69 6e 64  nto the term ind
336f2 65 78 2c 20 77 65 20 63 6c 65 61 72 6c 79 20 63  ex, we clearly c
336f3 61 6e 6e 6f 74 20 73 69 6d 70 6c 79 20 75 70 64  annot simply upd
336f4 61 74 65 20 74 68 65 20 74 65 72 6d 0a 2a 2a 20  ate the term.** 
336f5 69 6e 64 65 78 20 77 68 65 6e 20 61 20 64 6f 63  index when a doc
336f6 75 6d 65 6e 74 20 69 73 20 64 65 6c 65 74 65 64  ument is deleted
336f7 20 6f 72 20 75 70 64 61 74 65 64 2e 20 20 46 6f   or updated.  Fo
336f8 72 20 64 65 6c 65 74 69 6f 6e 73 2c 20 77 65 0a  r deletions, we.
336f9 2a 2a 20 77 72 69 74 65 20 61 6e 20 65 6d 70 74  ** write an empt
336fa 79 20 64 6f 63 6c 69 73 74 20 28 76 61 72 69 6e  y doclist (varin
336fb 74 28 64 6f 63 69 64 29 20 76 61 72 69 6e 74 28  t(docid) varint(
336fc 50 4f 53 5f 45 4e 44 29 29 2c 20 66 6f 72 20 75  POS_END)), for u
336fd 70 64 61 74 65 73 0a 2a 2a 20 77 65 20 73 69 6d  pdates.** we sim
336fe 70 6c 79 20 77 72 69 74 65 20 74 68 65 20 6e 65  ply write the ne
336ff 77 20 64 6f 63 6c 69 73 74 2e 20 20 53 65 67 6d  w doclist.  Segm
33700 65 6e 74 20 6d 65 72 67 65 73 20 6f 76 65 72 77  ent merges overw
33701 72 69 74 65 20 6f 6c 64 65 72 0a 2a 2a 20 64 61  rite older.** da
33702 74 61 20 66 6f 72 20 61 20 70 61 72 74 69 63 75  ta for a particu
33703 6c 61 72 20 64 6f 63 69 64 20 77 69 74 68 20 6e  lar docid with n
33704 65 77 65 72 20 64 61 74 61 2c 20 73 6f 20 64 65  ewer data, so de
33705 6c 65 74 65 73 20 6f 72 20 75 70 64 61 74 65 73  letes or updates
33706 0a 2a 2a 20 77 69 6c 6c 20 65 76 65 6e 74 75 61  .** will eventua
33707 6c 6c 79 20 6f 76 65 72 74 61 6b 65 20 74 68 65  lly overtake the
33708 20 65 61 72 6c 69 65 72 20 64 61 74 61 20 61 6e   earlier data an
33709 64 20 6b 6e 6f 63 6b 20 69 74 20 6f 75 74 2e 20  d knock it out. 
3370a 20 54 68 65 0a 2a 2a 20 71 75 65 72 79 20 6c 6f   The.** query lo
3370b 67 69 63 20 6c 69 6b 65 77 69 73 65 20 6d 65 72  gic likewise mer
3370c 67 65 73 20 64 6f 63 6c 69 73 74 73 20 73 6f 20  ges doclists so 
3370d 74 68 61 74 20 6e 65 77 65 72 20 64 61 74 61 20  that newer data 
3370e 6b 6e 6f 63 6b 73 20 6f 75 74 0a 2a 2a 20 6f 6c  knocks out.** ol
3370f 64 65 72 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20  der data..**.** 
33710 54 4f 44 4f 28 73 68 65 73 73 29 20 50 72 6f 76  TODO(shess) Prov
33711 69 64 65 20 61 20 56 41 43 55 55 4d 20 74 79 70  ide a VACUUM typ
33712 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 63  e operation to c
33713 6c 65 61 72 20 6f 75 74 20 61 6c 6c 0a 2a 2a 20  lear out all.** 
33714 64 65 6c 65 74 69 6f 6e 73 20 61 6e 64 20 64 75  deletions and du
33715 70 6c 69 63 61 74 69 6f 6e 73 2e 20 20 54 68 69  plications.  Thi
33716 73 20 77 6f 75 6c 64 20 62 61 73 69 63 61 6c 6c  s would basicall
33717 79 20 62 65 20 61 20 66 6f 72 63 65 64 20 6d 65  y be a forced me
33718 72 67 65 0a 2a 2a 20 69 6e 74 6f 20 61 20 73 69  rge.** into a si
33719 6e 67 6c 65 20 73 65 67 6d 65 6e 74 2e 0a 2a 2f  ngle segment..*/
3371a 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
3371b 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64  QLITE_CORE) || d
3371c 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
3371d 41 42 4c 45 5f 46 54 53 33 29 0a 0a 23 69 66 20  ABLE_FTS3)..#if 
3371e 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
3371f 4e 41 42 4c 45 5f 46 54 53 33 29 20 26 26 20 21  NABLE_FTS3) && !
33720 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43  defined(SQLITE_C
33721 4f 52 45 29 0a 23 20 64 65 66 69 6e 65 20 53 51  ORE).# define SQ
33722 4c 49 54 45 5f 43 4f 52 45 20 31 0a 23 65 6e 64  LITE_CORE 1.#end
33723 69 66 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  if.../**********
33724 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 66 74 73  **** Include fts
33725 33 5f 65 78 70 72 2e 68 20 69 6e 20 74 68 65 20  3_expr.h in the 
33726 6d 69 64 64 6c 65 20 6f 66 20 66 74 73 33 2e 63  middle of fts3.c
33727 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
33728 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
33729 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
3372a 66 74 73 33 5f 65 78 70 72 2e 68 20 2a 2a 2a 2a  fts3_expr.h ****
3372b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3372c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3372d 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20  ***/./*.** 2008 
3372e 4e 6f 76 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65  Nov 28.**.** The
3372f 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
33730 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
33731 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
33732 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
33733 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
33734 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
33735 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
33736 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
33737 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
33738 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
33739 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
3373a 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
3373b 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
3373c 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
3373d 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
3373e 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
3373f 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
33740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33741 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33742 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33743 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33744 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2f  **********.**.*/
33745 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
33746 2a 20 49 6e 63 6c 75 64 65 20 66 74 73 33 5f 74  * Include fts3_t
33747 6f 6b 65 6e 69 7a 65 72 2e 68 20 69 6e 20 74 68  okenizer.h in th
33748 65 20 6d 69 64 64 6c 65 20 6f 66 20 66 74 73 33  e middle of fts3
33749 5f 65 78 70 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  _expr.h ********
3374a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
3374b 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73  * Begin file fts
3374c 33 5f 74 6f 6b 65 6e 69 7a 65 72 2e 68 20 2a 2a  3_tokenizer.h **
3374d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3374e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3374f 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 4a 75 6c  /./*.** 2006 Jul
33750 79 20 31 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  y 10.**.** The a
33751 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
33752 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
33753 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 0a 2a  s source code..*
33754 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
33755 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33756 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33757 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33758 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 44  ***********.** D
33759 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72  efines the inter
3375a 66 61 63 65 20 74 6f 20 74 6f 6b 65 6e 69 7a 65  face to tokenize
3375b 72 73 20 75 73 65 64 20 62 79 20 66 75 6c 6c 74  rs used by fullt
3375c 65 78 74 2d 73 65 61 72 63 68 2e 20 20 54 68 65  ext-search.  The
3375d 72 65 0a 2a 2a 20 61 72 65 20 74 68 72 65 65 20  re.** are three 
3375e 62 61 73 69 63 20 63 6f 6d 70 6f 6e 65 6e 74 73  basic components
3375f 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  :.**.** sqlite3_
33760 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
33761 20 69 73 20 61 20 73 69 6e 67 6c 65 74 6f 6e 20   is a singleton 
33762 64 65 66 69 6e 69 6e 67 20 74 68 65 20 74 6f 6b  defining the tok
33763 65 6e 69 7a 65 72 0a 2a 2a 20 69 6e 74 65 72 66  enizer.** interf
33764 61 63 65 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20  ace functions.  
33765 54 68 69 73 20 69 73 20 65 73 73 65 6e 74 69 61  This is essentia
33766 6c 6c 79 20 74 68 65 20 63 6c 61 73 73 20 73 74  lly the class st
33767 72 75 63 74 75 72 65 20 66 6f 72 0a 2a 2a 20 74  ructure for.** t
33768 6f 6b 65 6e 69 7a 65 72 73 2e 0a 2a 2a 0a 2a 2a  okenizers..**.**
33769 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
3376a 65 72 20 69 73 20 75 73 65 64 20 74 6f 20 64 65  er is used to de
3376b 66 69 6e 65 20 61 20 70 61 72 74 69 63 75 6c 61  fine a particula
3376c 72 20 74 6f 6b 65 6e 69 7a 65 72 2c 20 70 65 72  r tokenizer, per
3376d 68 61 70 73 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e  haps.** includin
3376e 67 20 63 75 73 74 6f 6d 69 7a 61 74 69 6f 6e 20  g customization 
3376f 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 64 65 66 69  information defi
33770 6e 65 64 20 61 74 20 63 72 65 61 74 69 6f 6e 20  ned at creation 
33771 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69  time..**.** sqli
33772 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
33773 72 73 6f 72 20 69 73 20 67 65 6e 65 72 61 74 65  rsor is generate
33774 64 20 62 79 20 61 20 74 6f 6b 65 6e 69 7a 65 72  d by a tokenizer
33775 20 74 6f 20 67 65 6e 65 72 61 74 65 0a 2a 2a 20   to generate.** 
33776 74 6f 6b 65 6e 73 20 66 72 6f 6d 20 61 20 70 61  tokens from a pa
33777 72 74 69 63 75 6c 61 72 20 69 6e 70 75 74 2e 0a  rticular input..
33778 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 46 54 53 33  */.#ifndef _FTS3
33779 5f 54 4f 4b 45 4e 49 5a 45 52 5f 48 5f 0a 23 64  _TOKENIZER_H_.#d
3377a 65 66 69 6e 65 20 5f 46 54 53 33 5f 54 4f 4b 45  efine _FTS3_TOKE
3377b 4e 49 5a 45 52 5f 48 5f 0a 0a 2f 2a 20 54 4f 44  NIZER_H_../* TOD
3377c 4f 28 73 68 65 73 73 29 20 4f 6e 6c 79 20 75 73  O(shess) Only us
3377d 65 64 20 66 6f 72 20 53 51 4c 49 54 45 5f 4f 4b  ed for SQLITE_OK
3377e 20 61 6e 64 20 53 51 4c 49 54 45 5f 44 4f 4e 45   and SQLITE_DONE
3377f 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e 0a 2a   at this time..*
33780 2a 20 49 66 20 74 6f 6b 65 6e 69 7a 65 72 73 20  * If tokenizers 
33781 61 72 65 20 74 6f 20 62 65 20 61 6c 6c 6f 77 65  are to be allowe
33782 64 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65  d to call sqlite
33783 33 5f 2a 28 29 20 66 75 6e 63 74 69 6f 6e 73 2c  3_*() functions,
33784 20 74 68 65 6e 0a 2a 2a 20 77 65 20 77 69 6c 6c   then.** we will
33785 20 6e 65 65 64 20 61 20 77 61 79 20 74 6f 20 72   need a way to r
33786 65 67 69 73 74 65 72 20 74 68 65 20 41 50 49 20  egister the API 
33787 63 6f 6e 73 69 73 74 65 6e 74 6c 79 2e 0a 2a 2f  consistently..*/
33788 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 74 75 72  ../*.** Structur
33789 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20 74  es used by the t
3378a 6f 6b 65 6e 69 7a 65 72 20 69 6e 74 65 72 66 61  okenizer interfa
3378b 63 65 2e 20 57 68 65 6e 20 61 20 6e 65 77 20 74  ce. When a new t
3378c 6f 6b 65 6e 69 7a 65 72 0a 2a 2a 20 69 6d 70 6c  okenizer.** impl
3378d 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 72 65  ementation is re
3378e 67 69 73 74 65 72 65 64 2c 20 74 68 65 20 63 61  gistered, the ca
3378f 6c 6c 65 72 20 70 72 6f 76 69 64 65 73 20 61 20  ller provides a 
33790 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 61 6e  pointer to.** an
33791 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
33792 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 74 61 69  er_module contai
33793 6e 69 6e 67 20 70 6f 69 6e 74 65 72 73 20 74 6f  ning pointers to
33794 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a   the callback.**
33795 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20   functions that 
33796 6d 61 6b 65 20 75 70 20 61 6e 20 69 6d 70 6c 65  make up an imple
33797 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  mentation..**.**
33798 20 57 68 65 6e 20 61 6e 20 66 74 73 33 20 74 61   When an fts3 ta
33799 62 6c 65 20 69 73 20 63 72 65 61 74 65 64 2c 20  ble is created, 
3379a 69 74 20 70 61 73 73 65 73 20 61 6e 79 20 61 72  it passes any ar
3379b 67 75 6d 65 6e 74 73 20 70 61 73 73 65 64 20 74  guments passed t
3379c 6f 0a 2a 2a 20 74 68 65 20 74 6f 6b 65 6e 69 7a  o.** the tokeniz
3379d 65 72 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65  er clause of the
3379e 20 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20   CREATE VIRTUAL 
3379f 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20  TABLE statement 
337a0 74 6f 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65  to the.** sqlite
337a1 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
337a2 6c 65 2e 78 43 72 65 61 74 65 28 29 20 66 75 6e  le.xCreate() fun
337a3 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 72 65 71  ction of the req
337a4 75 65 73 74 65 64 20 74 6f 6b 65 6e 69 7a 65 72  uested tokenizer
337a5 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  .** implementati
337a6 6f 6e 2e 20 54 68 65 20 78 43 72 65 61 74 65 28  on. The xCreate(
337a7 29 20 66 75 6e 63 74 69 6f 6e 20 69 6e 20 74 75  ) function in tu
337a8 72 6e 20 72 65 74 75 72 6e 73 20 61 6e 20 0a 2a  rn returns an .*
337a9 2a 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  * sqlite3_tokeni
337aa 7a 65 72 20 73 74 72 75 63 74 75 72 65 20 72 65  zer structure re
337ab 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 73  presenting the s
337ac 70 65 63 69 66 69 63 20 74 6f 6b 65 6e 69 7a 65  pecific tokenize
337ad 72 20 74 6f 0a 2a 2a 20 62 65 20 75 73 65 64 20  r to.** be used 
337ae 66 6f 72 20 74 68 65 20 66 74 73 33 20 74 61 62  for the fts3 tab
337af 6c 65 20 28 63 75 73 74 6f 6d 69 7a 65 64 20 62  le (customized b
337b0 79 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20  y the tokenizer 
337b1 63 6c 61 75 73 65 20 61 72 67 75 6d 65 6e 74 73  clause arguments
337b2 29 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 74 6f 6b 65  )..**.** To toke
337b3 6e 69 7a 65 20 61 6e 20 69 6e 70 75 74 20 62 75  nize an input bu
337b4 66 66 65 72 2c 20 74 68 65 20 73 71 6c 69 74 65  ffer, the sqlite
337b5 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
337b6 6c 65 2e 78 4f 70 65 6e 28 29 0a 2a 2a 20 6d 65  le.xOpen().** me
337b7 74 68 6f 64 20 69 73 20 63 61 6c 6c 65 64 2e 20  thod is called. 
337b8 49 74 20 72 65 74 75 72 6e 73 20 61 6e 20 73 71  It returns an sq
337b9 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
337ba 63 75 72 73 6f 72 20 6f 62 6a 65 63 74 0a 2a 2a  cursor object.**
337bb 20 74 68 61 74 20 6d 61 79 20 62 65 20 75 73 65   that may be use
337bc 64 20 74 6f 20 74 6f 6b 65 6e 69 7a 65 20 61 20  d to tokenize a 
337bd 73 70 65 63 69 66 69 63 20 69 6e 70 75 74 20 62  specific input b
337be 75 66 66 65 72 20 62 61 73 65 64 20 6f 6e 0a 2a  uffer based on.*
337bf 2a 20 74 68 65 20 74 6f 6b 65 6e 69 7a 61 74 69  * the tokenizati
337c0 6f 6e 20 72 75 6c 65 73 20 73 75 70 70 6c 69 65  on rules supplie
337c1 64 20 62 79 20 61 20 73 70 65 63 69 66 69 63 20  d by a specific 
337c2 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
337c3 72 0a 2a 2a 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a  r.** object..*/.
337c4 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73  typedef struct s
337c5 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
337c6 5f 6d 6f 64 75 6c 65 20 73 71 6c 69 74 65 33 5f  _module sqlite3_
337c7 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
337c8 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
337c9 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
337ca 65 72 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  er sqlite3_token
337cb 69 7a 65 72 3b 0a 74 79 70 65 64 65 66 20 73 74  izer;.typedef st
337cc 72 75 63 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b  ruct sqlite3_tok
337cd 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 73 71  enizer_cursor sq
337ce 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
337cf 63 75 72 73 6f 72 3b 0a 0a 73 74 72 75 63 74 20  cursor;..struct 
337d0 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
337d1 72 5f 6d 6f 64 75 6c 65 20 7b 0a 0a 20 20 2f 2a  r_module {..  /*
337d2 0a 20 20 2a 2a 20 53 74 72 75 63 74 75 72 65 20  .  ** Structure 
337d3 76 65 72 73 69 6f 6e 2e 20 53 68 6f 75 6c 64 20  version. Should 
337d4 61 6c 77 61 79 73 20 62 65 20 73 65 74 20 74 6f  always be set to
337d5 20 30 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 69   0..  */.  int i
337d6 56 65 72 73 69 6f 6e 3b 0a 0a 20 20 2f 2a 0a 20  Version;..  /*. 
337d7 20 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77   ** Create a new
337d8 20 74 6f 6b 65 6e 69 7a 65 72 2e 20 54 68 65 20   tokenizer. The 
337d9 76 61 6c 75 65 73 20 69 6e 20 74 68 65 20 61 72  values in the ar
337da 67 76 5b 5d 20 61 72 72 61 79 20 61 72 65 20 74  gv[] array are t
337db 68 65 0a 20 20 2a 2a 20 61 72 67 75 6d 65 6e 74  he.  ** argument
337dc 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 20  s passed to the 
337dd 22 74 6f 6b 65 6e 69 7a 65 72 22 20 63 6c 61 75  "tokenizer" clau
337de 73 65 20 6f 66 20 74 68 65 20 43 52 45 41 54 45  se of the CREATE
337df 20 56 49 52 54 55 41 4c 0a 20 20 2a 2a 20 54 41   VIRTUAL.  ** TA
337e0 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 74 68  BLE statement th
337e1 61 74 20 63 72 65 61 74 65 64 20 74 68 65 20 66  at created the f
337e2 74 73 33 20 74 61 62 6c 65 2e 20 46 6f 72 20 65  ts3 table. For e
337e3 78 61 6d 70 6c 65 2c 20 69 66 0a 20 20 2a 2a 20  xample, if.  ** 
337e4 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 53 51  the following SQ
337e5 4c 20 69 73 20 65 78 65 63 75 74 65 64 3a 0a 20  L is executed:. 
337e6 20 2a 2a 0a 20 20 2a 2a 20 20 20 43 52 45 41 54   **.  **   CREAT
337e7 45 20 2e 2e 20 55 53 49 4e 47 20 66 74 73 33 28  E .. USING fts3(
337e8 20 2e 2e 2e 20 2c 20 74 6f 6b 65 6e 69 7a 65 72   ... , tokenizer
337e9 20 3c 74 6f 6b 65 6e 69 7a 65 72 2d 6e 61 6d 65   <tokenizer-name
337ea 3e 20 61 72 67 31 20 61 72 67 32 29 0a 20 20 2a  > arg1 arg2).  *
337eb 2a 0a 20 20 2a 2a 20 74 68 65 6e 20 61 72 67 63  *.  ** then argc
337ec 20 69 73 20 73 65 74 20 74 6f 20 32 2c 20 61 6e   is set to 2, an
337ed 64 20 74 68 65 20 61 72 67 76 5b 5d 20 61 72 72  d the argv[] arr
337ee 61 79 20 63 6f 6e 74 61 69 6e 73 20 70 6f 69 6e  ay contains poin
337ef 74 65 72 73 0a 20 20 2a 2a 20 74 6f 20 74 68 65  ters.  ** to the
337f0 20 73 74 72 69 6e 67 73 20 22 61 72 67 31 22 20   strings "arg1" 
337f1 61 6e 64 20 22 61 72 67 32 22 2e 0a 20 20 2a 2a  and "arg2"..  **
337f2 0a 20 20 2a 2a 20 54 68 69 73 20 6d 65 74 68 6f  .  ** This metho
337f3 64 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20  d should return 
337f4 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4f 4b  either SQLITE_OK
337f5 20 28 30 29 2c 20 6f 72 20 61 6e 20 53 51 4c 69   (0), or an SQLi
337f6 74 65 20 65 72 72 6f 72 20 0a 20 20 2a 2a 20 63  te error .  ** c
337f7 6f 64 65 2e 20 49 66 20 53 51 4c 49 54 45 5f 4f  ode. If SQLITE_O
337f8 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 74  K is returned, t
337f9 68 65 6e 20 2a 70 70 54 6f 6b 65 6e 69 7a 65 72  hen *ppTokenizer
337fa 20 73 68 6f 75 6c 64 20 62 65 20 73 65 74 0a 20   should be set. 
337fb 20 2a 2a 20 74 6f 20 70 6f 69 6e 74 20 61 74 20   ** to point at 
337fc 74 68 65 20 6e 65 77 6c 79 20 63 72 65 61 74 65  the newly create
337fd 64 20 74 6f 6b 65 6e 69 7a 65 72 20 73 74 72 75  d tokenizer stru
337fe 63 74 75 72 65 2e 20 54 68 65 20 67 65 6e 65 72  cture. The gener
337ff 69 63 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f  ic.  ** sqlite3_
33800 74 6f 6b 65 6e 69 7a 65 72 2e 70 4d 6f 64 75 6c  tokenizer.pModul
33801 65 20 76 61 72 69 61 62 6c 65 20 73 68 6f 75 6c  e variable shoul
33802 64 20 6e 6f 74 20 62 65 20 69 6e 69 74 69 61 6c  d not be initial
33803 69 73 65 64 20 62 79 0a 20 20 2a 2a 20 74 68 69  ised by.  ** thi
33804 73 20 63 61 6c 6c 62 61 63 6b 2e 20 54 68 65 20  s callback. The 
33805 63 61 6c 6c 65 72 20 77 69 6c 6c 20 64 6f 20 73  caller will do s
33806 6f 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 28 2a  o..  */.  int (*
33807 78 43 72 65 61 74 65 29 28 0a 20 20 20 20 69 6e  xCreate)(.    in
33808 74 20 61 72 67 63 2c 20 20 20 20 20 20 20 20 20  t argc,         
33809 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3380a 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 72 67    /* Size of arg
3380b 76 20 61 72 72 61 79 20 2a 2f 0a 20 20 20 20 63  v array */.    c
3380c 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74  onst char *const
3380d 2a 61 72 67 76 2c 20 20 20 20 20 20 20 20 20 20  *argv,          
3380e 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20     /* Tokenizer 
3380f 61 72 67 75 6d 65 6e 74 20 73 74 72 69 6e 67 73  argument strings
33810 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   */.    sqlite3_
33811 74 6f 6b 65 6e 69 7a 65 72 20 2a 2a 70 70 54 6f  tokenizer **ppTo
33812 6b 65 6e 69 7a 65 72 20 20 20 20 20 2f 2a 20 4f  kenizer     /* O
33813 55 54 3a 20 43 72 65 61 74 65 64 20 74 6f 6b 65  UT: Created toke
33814 6e 69 7a 65 72 20 2a 2f 0a 20 20 29 3b 0a 0a 20  nizer */.  );.. 
33815 20 2f 2a 0a 20 20 2a 2a 20 44 65 73 74 72 6f 79   /*.  ** Destroy
33816 20 61 6e 20 65 78 69 73 74 69 6e 67 20 74 6f 6b   an existing tok
33817 65 6e 69 7a 65 72 2e 20 54 68 65 20 66 74 73 33  enizer. The fts3
33818 20 6d 6f 64 75 6c 65 20 63 61 6c 6c 73 20 74 68   module calls th
33819 69 73 20 6d 65 74 68 6f 64 0a 20 20 2a 2a 20 65  is method.  ** e
3381a 78 61 63 74 6c 79 20 6f 6e 63 65 20 66 6f 72 20  xactly once for 
3381b 65 61 63 68 20 73 75 63 63 65 73 73 66 75 6c 20  each successful 
3381c 63 61 6c 6c 20 74 6f 20 78 43 72 65 61 74 65 28  call to xCreate(
3381d 29 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 28 2a  )..  */.  int (*
3381e 78 44 65 73 74 72 6f 79 29 28 73 71 6c 69 74 65  xDestroy)(sqlite
3381f 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f  3_tokenizer *pTo
33820 6b 65 6e 69 7a 65 72 29 3b 0a 0a 20 20 2f 2a 0a  kenizer);..  /*.
33821 20 20 2a 2a 20 43 72 65 61 74 65 20 61 20 74 6f    ** Create a to
33822 6b 65 6e 69 7a 65 72 20 63 75 72 73 6f 72 20 74  kenizer cursor t
33823 6f 20 74 6f 6b 65 6e 69 7a 65 20 61 6e 20 69 6e  o tokenize an in
33824 70 75 74 20 62 75 66 66 65 72 2e 20 54 68 65 20  put buffer. The 
33825 63 61 6c 6c 65 72 0a 20 20 2a 2a 20 69 73 20 72  caller.  ** is r
33826 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 65  esponsible for e
33827 6e 73 75 72 69 6e 67 20 74 68 61 74 20 74 68 65  nsuring that the
33828 20 69 6e 70 75 74 20 62 75 66 66 65 72 20 72 65   input buffer re
33829 6d 61 69 6e 73 20 76 61 6c 69 64 0a 20 20 2a 2a  mains valid.  **
3382a 20 75 6e 74 69 6c 20 74 68 65 20 63 75 72 73 6f   until the curso
3382b 72 20 69 73 20 63 6c 6f 73 65 64 20 28 75 73 69  r is closed (usi
3382c 6e 67 20 74 68 65 20 78 43 6c 6f 73 65 28 29 20  ng the xClose() 
3382d 6d 65 74 68 6f 64 29 2e 20 0a 20 20 2a 2f 0a 20  method). .  */. 
3382e 20 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28 0a 20   int (*xOpen)(. 
3382f 20 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e     sqlite3_token
33830 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72  izer *pTokenizer
33831 2c 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e  ,       /* Token
33832 69 7a 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20  izer object */. 
33833 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70     const char *p
33834 49 6e 70 75 74 2c 20 69 6e 74 20 6e 42 79 74 65  Input, int nByte
33835 73 2c 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74  s,      /* Input
33836 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 20 20 73   buffer */.    s
33837 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
33838 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73  _cursor **ppCurs
33839 6f 72 20 20 2f 2a 20 4f 55 54 3a 20 43 72 65 61  or  /* OUT: Crea
3383a 74 65 64 20 74 6f 6b 65 6e 69 7a 65 72 20 63 75  ted tokenizer cu
3383b 72 73 6f 72 20 2a 2f 0a 20 20 29 3b 0a 0a 20 20  rsor */.  );..  
3383c 2f 2a 0a 20 20 2a 2a 20 44 65 73 74 72 6f 79 20  /*.  ** Destroy 
3383d 61 6e 20 65 78 69 73 74 69 6e 67 20 74 6f 6b 65  an existing toke
3383e 6e 69 7a 65 72 20 63 75 72 73 6f 72 2e 20 54 68  nizer cursor. Th
3383f 65 20 66 74 73 33 20 6d 6f 64 75 6c 65 20 63 61  e fts3 module ca
33840 6c 6c 73 20 74 68 69 73 20 0a 20 20 2a 2a 20 6d  lls this .  ** m
33841 65 74 68 6f 64 20 65 78 61 63 74 6c 79 20 6f 6e  ethod exactly on
33842 63 65 20 66 6f 72 20 65 61 63 68 20 73 75 63 63  ce for each succ
33843 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 78  essful call to x
33844 4f 70 65 6e 28 29 2e 0a 20 20 2a 2f 0a 20 20 69  Open()..  */.  i
33845 6e 74 20 28 2a 78 43 6c 6f 73 65 29 28 73 71 6c  nt (*xClose)(sql
33846 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
33847 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 3b  ursor *pCursor);
33848 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 74 72  ..  /*.  ** Retr
33849 69 65 76 65 20 74 68 65 20 6e 65 78 74 20 74 6f  ieve the next to
3384a 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20 74 6f 6b  ken from the tok
3384b 65 6e 69 7a 65 72 20 63 75 72 73 6f 72 20 70 43  enizer cursor pC
3384c 75 72 73 6f 72 2e 20 54 68 69 73 0a 20 20 2a 2a  ursor. This.  **
3384d 20 6d 65 74 68 6f 64 20 73 68 6f 75 6c 64 20 65   method should e
3384e 69 74 68 65 72 20 72 65 74 75 72 6e 20 53 51 4c  ither return SQL
3384f 49 54 45 5f 4f 4b 20 61 6e 64 20 73 65 74 20 74  ITE_OK and set t
33850 68 65 20 76 61 6c 75 65 73 20 6f 66 20 74 68 65  he values of the
33851 0a 20 20 2a 2a 20 22 4f 55 54 22 20 76 61 72 69  .  ** "OUT" vari
33852 61 62 6c 65 73 20 69 64 65 6e 74 69 66 69 65 64  ables identified
33853 20 62 65 6c 6f 77 2c 20 6f 72 20 53 51 4c 49 54   below, or SQLIT
33854 45 5f 44 4f 4e 45 20 74 6f 20 69 6e 64 69 63 61  E_DONE to indica
33855 74 65 20 74 68 61 74 0a 20 20 2a 2a 20 74 68 65  te that.  ** the
33856 20 65 6e 64 20 6f 66 20 74 68 65 20 62 75 66 66   end of the buff
33857 65 72 20 68 61 73 20 62 65 65 6e 20 72 65 61 63  er has been reac
33858 68 65 64 2c 20 6f 72 20 61 6e 20 53 51 4c 69 74  hed, or an SQLit
33859 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 20 20  e error code..  
3385a 2a 2a 0a 20 20 2a 2a 20 2a 70 70 54 6f 6b 65 6e  **.  ** *ppToken
3385b 20 73 68 6f 75 6c 64 20 62 65 20 73 65 74 20 74   should be set t
3385c 6f 20 70 6f 69 6e 74 20 61 74 20 61 20 62 75 66  o point at a buf
3385d 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  fer containing t
3385e 68 65 20 0a 20 20 2a 2a 20 6e 6f 72 6d 61 6c 69  he .  ** normali
3385f 7a 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 74  zed version of t
33860 68 65 20 74 6f 6b 65 6e 20 28 69 2e 65 2e 20 61  he token (i.e. a
33861 66 74 65 72 20 61 6e 79 20 63 61 73 65 2d 66 6f  fter any case-fo
33862 6c 64 69 6e 67 20 61 6e 64 2f 6f 72 0a 20 20 2a  lding and/or.  *
33863 2a 20 73 74 65 6d 6d 69 6e 67 20 68 61 73 20 62  * stemming has b
33864 65 65 6e 20 70 65 72 66 6f 72 6d 65 64 29 2e 20  een performed). 
33865 2a 70 6e 42 79 74 65 73 20 73 68 6f 75 6c 64 20  *pnBytes should 
33866 62 65 20 73 65 74 20 74 6f 20 74 68 65 20 6c 65  be set to the le
33867 6e 67 74 68 0a 20 20 2a 2a 20 6f 66 20 74 68 69  ngth.  ** of thi
33868 73 20 62 75 66 66 65 72 20 69 6e 20 62 79 74 65  s buffer in byte
33869 73 2e 20 54 68 65 20 69 6e 70 75 74 20 74 65 78  s. The input tex
3386a 74 20 74 68 61 74 20 67 65 6e 65 72 61 74 65 64  t that generated
3386b 20 74 68 65 20 74 6f 6b 65 6e 20 69 73 0a 20 20   the token is.  
3386c 2a 2a 20 69 64 65 6e 74 69 66 69 65 64 20 62 79  ** identified by
3386d 20 74 68 65 20 62 79 74 65 20 6f 66 66 73 65 74   the byte offset
3386e 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20 2a 70  s returned in *p
3386f 69 53 74 61 72 74 4f 66 66 73 65 74 20 61 6e 64  iStartOffset and
33870 0a 20 20 2a 2a 20 2a 70 69 45 6e 64 4f 66 66 73  .  ** *piEndOffs
33871 65 74 2e 20 2a 70 69 53 74 61 72 74 4f 66 66 73  et. *piStartOffs
33872 65 74 20 73 68 6f 75 6c 64 20 62 65 20 73 65 74  et should be set
33873 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f 66   to the index of
33874 20 74 68 65 20 66 69 72 73 74 0a 20 20 2a 2a 20   the first.  ** 
33875 62 79 74 65 20 6f 66 20 74 68 65 20 74 6f 6b 65  byte of the toke
33876 6e 20 69 6e 20 74 68 65 20 69 6e 70 75 74 20 62  n in the input b
33877 75 66 66 65 72 2e 20 2a 70 69 45 6e 64 4f 66 66  uffer. *piEndOff
33878 73 65 74 20 73 68 6f 75 6c 64 20 62 65 20 73 65  set should be se
33879 74 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 69 6e  t.  ** to the in
3387a 64 65 78 20 6f 66 20 74 68 65 20 66 69 72 73 74  dex of the first
3387b 20 62 79 74 65 20 6a 75 73 74 20 70 61 73 74 20   byte just past 
3387c 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 74  the end of the t
3387d 6f 6b 65 6e 20 69 6e 0a 20 20 2a 2a 20 74 68 65  oken in.  ** the
3387e 20 69 6e 70 75 74 20 62 75 66 66 65 72 2e 0a 20   input buffer.. 
3387f 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 62 75 66   **.  ** The buf
33880 66 65 72 20 2a 70 70 54 6f 6b 65 6e 20 69 73 20  fer *ppToken is 
33881 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 61 74 20  set to point at 
33882 69 73 20 6d 61 6e 61 67 65 64 20 62 79 20 74 68  is managed by th
33883 65 20 74 6f 6b 65 6e 69 7a 65 72 0a 20 20 2a 2a  e tokenizer.  **
33884 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e   implementation.
33885 20 49 74 20 69 73 20 6f 6e 6c 79 20 72 65 71 75   It is only requ
33886 69 72 65 64 20 74 6f 20 62 65 20 76 61 6c 69 64  ired to be valid
33887 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 20   until the next 
33888 63 61 6c 6c 0a 20 20 2a 2a 20 74 6f 20 78 4e 65  call.  ** to xNe
33889 78 74 28 29 20 6f 72 20 78 43 6c 6f 73 65 28 29  xt() or xClose()
3388a 2e 20 0a 20 20 2a 2f 0a 20 20 2f 2a 20 54 4f 44  . .  */.  /* TOD
3388b 4f 28 73 68 65 73 73 29 20 63 75 72 72 65 6e 74  O(shess) current
3388c 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
3388d 72 65 71 75 69 72 65 73 20 70 49 6e 70 75 74 20  requires pInput 
3388e 74 6f 20 62 65 0a 20 20 2a 2a 20 6e 75 6c 2d 74  to be.  ** nul-t
3388f 65 72 6d 69 6e 61 74 65 64 2e 20 20 54 68 69 73  erminated.  This
33890 20 73 68 6f 75 6c 64 20 65 69 74 68 65 72 20 62   should either b
33891 65 20 66 69 78 65 64 2c 20 6f 72 20 70 49 6e 70  e fixed, or pInp
33892 75 74 2f 6e 42 79 74 65 73 0a 20 20 2a 2a 20 73  ut/nBytes.  ** s
33893 68 6f 75 6c 64 20 62 65 20 63 6f 6e 76 65 72 74  hould be convert
33894 65 64 20 74 6f 20 7a 49 6e 70 75 74 2e 0a 20 20  ed to zInput..  
33895 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 4e 65 78 74  */.  int (*xNext
33896 29 28 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 74  )(.    sqlite3_t
33897 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
33898 2a 70 43 75 72 73 6f 72 2c 20 20 20 2f 2a 20 54  *pCursor,   /* T
33899 6f 6b 65 6e 69 7a 65 72 20 63 75 72 73 6f 72 20  okenizer cursor 
3389a 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  */.    const cha
3389b 72 20 2a 2a 70 70 54 6f 6b 65 6e 2c 20 69 6e 74  r **ppToken, int
3389c 20 2a 70 6e 42 79 74 65 73 2c 20 20 2f 2a 20 4f   *pnBytes,  /* O
3389d 55 54 3a 20 4e 6f 72 6d 61 6c 69 7a 65 64 20 74  UT: Normalized t
3389e 65 78 74 20 66 6f 72 20 74 6f 6b 65 6e 20 2a 2f  ext for token */
3389f 0a 20 20 20 20 69 6e 74 20 2a 70 69 53 74 61 72  .    int *piStar
338a0 74 4f 66 66 73 65 74 2c 20 20 2f 2a 20 4f 55 54  tOffset,  /* OUT
338a1 3a 20 42 79 74 65 20 6f 66 66 73 65 74 20 6f 66  : Byte offset of
338a2 20 74 6f 6b 65 6e 20 69 6e 20 69 6e 70 75 74 20   token in input 
338a3 62 75 66 66 65 72 20 2a 2f 0a 20 20 20 20 69 6e  buffer */.    in
338a4 74 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 2c 20  t *piEndOffset, 
338a5 20 20 20 2f 2a 20 4f 55 54 3a 20 42 79 74 65 20     /* OUT: Byte 
338a6 6f 66 66 73 65 74 20 6f 66 20 65 6e 64 20 6f 66  offset of end of
338a7 20 74 6f 6b 65 6e 20 69 6e 20 69 6e 70 75 74 20   token in input 
338a8 62 75 66 66 65 72 20 2a 2f 0a 20 20 20 20 69 6e  buffer */.    in
338a9 74 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20 20 20  t *piPosition   
338aa 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65     /* OUT: Numbe
338ab 72 20 6f 66 20 74 6f 6b 65 6e 73 20 72 65 74 75  r of tokens retu
338ac 72 6e 65 64 20 62 65 66 6f 72 65 20 74 68 69 73  rned before this
338ad 20 6f 6e 65 20 2a 2f 0a 20 20 29 3b 0a 7d 3b 0a   one */.  );.};.
338ae 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  .struct sqlite3_
338af 74 6f 6b 65 6e 69 7a 65 72 20 7b 0a 20 20 63 6f  tokenizer {.  co
338b0 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  nst sqlite3_toke
338b1 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 4d  nizer_module *pM
338b2 6f 64 75 6c 65 3b 20 20 2f 2a 20 54 68 65 20 6d  odule;  /* The m
338b3 6f 64 75 6c 65 20 66 6f 72 20 74 68 69 73 20 74  odule for this t
338b4 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 20 20 2f 2a  okenizer */.  /*
338b5 20 54 6f 6b 65 6e 69 7a 65 72 20 69 6d 70 6c 65   Tokenizer imple
338b6 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c 6c 20  mentations will 
338b7 74 79 70 69 63 61 6c 6c 79 20 61 64 64 20 61 64  typically add ad
338b8 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73 20  ditional fields 
338b9 2a 2f 0a 7d 3b 0a 0a 73 74 72 75 63 74 20 73 71  */.};..struct sq
338ba 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
338bb 63 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c 69 74  cursor {.  sqlit
338bc 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54  e3_tokenizer *pT
338bd 6f 6b 65 6e 69 7a 65 72 3b 20 20 20 20 20 20 20  okenizer;       
338be 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 66 6f 72  /* Tokenizer for
338bf 20 74 68 69 73 20 63 75 72 73 6f 72 2e 20 2a 2f   this cursor. */
338c0 0a 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20  .  /* Tokenizer 
338c1 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
338c2 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79 20 61  will typically a
338c3 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 69  dd additional fi
338c4 65 6c 64 73 20 2a 2f 0a 7d 3b 0a 0a 23 65 6e 64  elds */.};..#end
338c5 69 66 20 2f 2a 20 5f 46 54 53 33 5f 54 4f 4b 45  if /* _FTS3_TOKE
338c6 4e 49 5a 45 52 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a  NIZER_H_ */../**
338c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
338c8 20 6f 66 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a   of fts3_tokeniz
338c9 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  er.h ***********
338ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
338cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
338cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
338cd 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
338ce 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 66 74 73   left off in fts
338cf 33 5f 65 78 70 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a  3_expr.h *******
338d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a  ***********/../*
338d1 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
338d2 67 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20  g describes the 
338d3 73 79 6e 74 61 78 20 73 75 70 70 6f 72 74 65 64  syntax supported
338d4 20 62 79 20 74 68 65 20 66 74 73 33 20 4d 41 54   by the fts3 MAT
338d5 43 48 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20 69  CH.** operator i
338d6 6e 20 61 20 73 69 6d 69 6c 61 72 20 66 6f 72 6d  n a similar form
338d7 61 74 20 74 6f 20 74 68 61 74 20 75 73 65 64 20  at to that used 
338d8 62 79 20 74 68 65 20 6c 65 6d 6f 6e 20 70 61 72  by the lemon par
338d9 73 65 72 0a 2a 2a 20 67 65 6e 65 72 61 74 6f 72  ser.** generator
338da 2e 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 64 6f  . This module do
338db 65 73 20 6e 6f 74 20 75 73 65 20 61 63 74 75 61  es not use actua
338dc 6c 6c 79 20 6c 65 6d 6f 6e 2c 20 69 74 20 75 73  lly lemon, it us
338dd 65 73 20 61 0a 2a 2a 20 63 75 73 74 6f 6d 20 70  es a.** custom p
338de 61 72 73 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 71  arser..**.**   q
338df 75 65 72 79 20 3a 3a 3d 20 61 6e 64 65 78 70 72  uery ::= andexpr
338e0 20 28 4f 52 20 61 6e 64 65 78 70 72 29 2a 2e 0a   (OR andexpr)*..
338e1 2a 2a 0a 2a 2a 20 20 20 61 6e 64 65 78 70 72 20  **.**   andexpr 
338e2 3a 3a 3d 20 6e 6f 74 65 78 70 72 20 28 41 4e 44  ::= notexpr (AND
338e3 3f 20 6e 6f 74 65 78 70 72 29 2a 2e 0a 2a 2a 0a  ? notexpr)*..**.
338e4 2a 2a 20 20 20 6e 6f 74 65 78 70 72 20 3a 3a 3d  **   notexpr ::=
338e5 20 6e 65 61 72 65 78 70 72 20 28 4e 4f 54 20 6e   nearexpr (NOT n
338e6 65 61 72 65 78 70 72 7c 2d 54 4f 4b 45 4e 29 2a  earexpr|-TOKEN)*
338e7 2e 0a 2a 2a 20 20 20 6e 6f 74 65 78 70 72 20 3a  ..**   notexpr :
338e8 3a 3d 20 4c 50 20 71 75 65 72 79 20 52 50 2e 0a  := LP query RP..
338e9 2a 2a 0a 2a 2a 20 20 20 6e 65 61 72 65 78 70 72  **.**   nearexpr
338ea 20 3a 3a 3d 20 70 68 72 61 73 65 20 28 4e 45 41   ::= phrase (NEA
338eb 52 20 64 69 73 74 61 6e 63 65 5f 6f 70 74 20 6e  R distance_opt n
338ec 65 61 72 65 78 70 72 29 2a 2e 0a 2a 2a 0a 2a 2a  earexpr)*..**.**
338ed 20 20 20 64 69 73 74 61 6e 63 65 5f 6f 70 74 20     distance_opt 
338ee 3a 3a 3d 20 2e 0a 2a 2a 20 20 20 64 69 73 74 61  ::= ..**   dista
338ef 6e 63 65 5f 6f 70 74 20 3a 3a 3d 20 2f 20 49 4e  nce_opt ::= / IN
338f0 54 45 47 45 52 2e 0a 2a 2a 0a 2a 2a 20 20 20 70  TEGER..**.**   p
338f1 68 72 61 73 65 20 3a 3a 3d 20 54 4f 4b 45 4e 2e  hrase ::= TOKEN.
338f2 0a 2a 2a 20 20 20 70 68 72 61 73 65 20 3a 3a 3d  .**   phrase ::=
338f3 20 43 4f 4c 55 4d 4e 3a 54 4f 4b 45 4e 2e 0a 2a   COLUMN:TOKEN..*
338f4 2a 20 20 20 70 68 72 61 73 65 20 3a 3a 3d 20 22  *   phrase ::= "
338f5 54 4f 4b 45 4e 20 54 4f 4b 45 4e 20 54 4f 4b 45  TOKEN TOKEN TOKE
338f6 4e 2e 2e 2e 22 2e 0a 2a 2f 0a 0a 74 79 70 65 64  N..."..*/..typed
338f7 65 66 20 73 74 72 75 63 74 20 46 74 73 33 45 78  ef struct Fts3Ex
338f8 70 72 20 46 74 73 33 45 78 70 72 3b 0a 74 79 70  pr Fts3Expr;.typ
338f9 65 64 65 66 20 73 74 72 75 63 74 20 46 74 73 33  edef struct Fts3
338fa 50 68 72 61 73 65 20 46 74 73 33 50 68 72 61 73  Phrase Fts3Phras
338fb 65 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 22 70 68 72  e;../*.** A "phr
338fc 61 73 65 22 20 69 73 20 61 20 73 65 71 75 65 6e  ase" is a sequen
338fd 63 65 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72  ce of one or mor
338fe 65 20 74 6f 6b 65 6e 73 20 74 68 61 74 20 6d 75  e tokens that mu
338ff 73 74 20 6d 61 74 63 68 20 69 6e 0a 2a 2a 20 73  st match in.** s
33900 65 71 75 65 6e 63 65 2e 20 20 41 20 73 69 6e 67  equence.  A sing
33901 6c 65 20 74 6f 6b 65 6e 20 69 73 20 74 68 65 20  le token is the 
33902 62 61 73 65 20 63 61 73 65 20 61 6e 64 20 74 68  base case and th
33903 65 20 6d 6f 73 74 20 63 6f 6d 6d 6f 6e 20 63 61  e most common ca
33904 73 65 2e 0a 2a 2a 20 46 6f 72 20 61 20 73 65 71  se..** For a seq
33905 75 65 6e 63 65 20 6f 66 20 74 6f 6b 65 6e 73 20  uence of tokens 
33906 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 22 2e 2e  contained in "..
33907 2e 22 2c 20 6e 54 6f 6b 65 6e 20 77 69 6c 6c 20  .", nToken will 
33908 62 65 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a  be the number.**
33909 20 6f 66 20 74 6f 6b 65 6e 73 20 69 6e 20 74 68   of tokens in th
3390a 65 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 73 74 72  e string..*/.str
3390b 75 63 74 20 46 74 73 33 50 68 72 61 73 65 20 7b  uct Fts3Phrase {
3390c 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b 20 20  .  int nToken;  
3390d 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
3390e 72 20 6f 66 20 74 6f 6b 65 6e 73 20 69 6e 20 74  r of tokens in t
3390f 68 65 20 70 68 72 61 73 65 20 2a 2f 0a 20 20 69  he phrase */.  i
33910 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20  nt iColumn;     
33911 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
33912 63 6f 6c 75 6d 6e 20 74 68 69 73 20 70 68 72 61  column this phra
33913 73 65 20 6d 75 73 74 20 6d 61 74 63 68 20 2a 2f  se must match */
33914 0a 20 20 69 6e 74 20 69 73 4e 6f 74 3b 20 20 20  .  int isNot;   
33915 20 20 20 20 20 20 20 20 2f 2a 20 50 68 72 61 73          /* Phras
33916 65 20 70 72 65 66 69 78 65 64 20 62 79 20 75 6e  e prefixed by un
33917 61 72 79 20 6e 6f 74 20 28 2d 29 20 6f 70 65 72  ary not (-) oper
33918 61 74 6f 72 20 2a 2f 0a 20 20 73 74 72 75 63 74  ator */.  struct
33919 20 50 68 72 61 73 65 54 6f 6b 65 6e 20 7b 0a 20   PhraseToken {. 
3391a 20 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20     char *z;     
3391b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74           /* Text
3391c 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 2a 2f   of the token */
3391d 0a 20 20 20 20 69 6e 74 20 6e 3b 20 20 20 20 20  .    int n;     
3391e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
3391f 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
33920 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20   buffer pointed 
33921 74 6f 20 62 79 20 7a 20 2a 2f 0a 20 20 20 20 69  to by z */.    i
33922 6e 74 20 69 73 50 72 65 66 69 78 3b 20 20 20 20  nt isPrefix;    
33923 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
33924 74 6f 6b 65 6e 20 65 6e 64 73 20 69 6e 20 77 69  token ends in wi
33925 74 68 20 61 20 22 2a 22 20 63 68 61 72 61 63 74  th a "*" charact
33926 65 72 20 2a 2f 0a 20 20 7d 20 61 54 6f 6b 65 6e  er */.  } aToken
33927 5b 31 5d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  [1];         /* 
33928 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 65 61  One entry for ea
33929 63 68 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20  ch token in the 
3392a 70 68 72 61 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  phrase */.};../*
3392b 0a 2a 2a 20 41 20 74 72 65 65 20 6f 66 20 74 68  .** A tree of th
3392c 65 73 65 20 6f 62 6a 65 63 74 73 20 66 6f 72 6d  ese objects form
3392d 73 20 74 68 65 20 52 48 53 20 6f 66 20 61 20 4d  s the RHS of a M
3392e 41 54 43 48 20 6f 70 65 72 61 74 6f 72 2e 0a 2a  ATCH operator..*
3392f 2f 0a 73 74 72 75 63 74 20 46 74 73 33 45 78 70  /.struct Fts3Exp
33930 72 20 7b 0a 20 20 69 6e 74 20 65 54 79 70 65 3b  r {.  int eType;
33931 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33932 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 46   /* One of the F
33933 54 53 51 55 45 52 59 5f 58 58 58 20 76 61 6c 75  TSQUERY_XXX valu
33934 65 73 20 64 65 66 69 6e 65 64 20 62 65 6c 6f 77  es defined below
33935 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 65 61 72 3b   */.  int nNear;
33936 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33937 20 2f 2a 20 56 61 6c 69 64 20 69 66 20 65 54 79   /* Valid if eTy
33938 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45 41  pe==FTSQUERY_NEA
33939 52 20 2a 2f 0a 20 20 46 74 73 33 45 78 70 72 20  R */.  Fts3Expr 
3393a 2a 70 50 61 72 65 6e 74 3b 20 20 20 20 20 20 20  *pParent;       
3393b 20 20 2f 2a 20 70 50 61 72 65 6e 74 2d 3e 70 4c    /* pParent->pL
3393c 65 66 74 3d 3d 74 68 69 73 20 6f 72 20 70 50 61  eft==this or pPa
3393d 72 65 6e 74 2d 3e 70 52 69 67 68 74 3d 3d 74 68  rent->pRight==th
3393e 69 73 20 2a 2f 0a 20 20 46 74 73 33 45 78 70 72  is */.  Fts3Expr
3393f 20 2a 70 4c 65 66 74 3b 20 20 20 20 20 20 20 20   *pLeft;        
33940 20 20 20 2f 2a 20 4c 65 66 74 20 6f 70 65 72 61     /* Left opera
33941 6e 64 20 2a 2f 0a 20 20 46 74 73 33 45 78 70 72  nd */.  Fts3Expr
33942 20 2a 70 52 69 67 68 74 3b 20 20 20 20 20 20 20   *pRight;       
33943 20 20 20 2f 2a 20 52 69 67 68 74 20 6f 70 65 72     /* Right oper
33944 61 6e 64 20 2a 2f 0a 20 20 46 74 73 33 50 68 72  and */.  Fts3Phr
33945 61 73 65 20 2a 70 50 68 72 61 73 65 3b 20 20 20  ase *pPhrase;   
33946 20 20 20 20 2f 2a 20 56 61 6c 69 64 20 69 66 20      /* Valid if 
33947 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f  eType==FTSQUERY_
33948 50 48 52 41 53 45 20 2a 2f 0a 7d 3b 0a 0a 53 51  PHRASE */.};..SQ
33949 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
3394a 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72   sqlite3Fts3Expr
3394b 50 61 72 73 65 28 73 71 6c 69 74 65 33 5f 74 6f  Parse(sqlite3_to
3394c 6b 65 6e 69 7a 65 72 20 2a 2c 20 63 68 61 72 20  kenizer *, char 
3394d 2a 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 0a 20  **, int, int, . 
3394e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3394f 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
33950 61 72 20 2a 2c 20 69 6e 74 2c 20 46 74 73 33 45  ar *, int, Fts3E
33951 78 70 72 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f  xpr **);.SQLITE_
33952 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
33953 69 74 65 33 46 74 73 33 45 78 70 72 46 72 65 65  ite3Fts3ExprFree
33954 28 46 74 73 33 45 78 70 72 20 2a 29 3b 0a 0a 2f  (Fts3Expr *);../
33955 2a 0a 2a 2a 20 43 61 6e 64 69 64 61 74 65 20 76  *.** Candidate v
33956 61 6c 75 65 73 20 66 6f 72 20 46 74 73 33 51 75  alues for Fts3Qu
33957 65 72 79 2e 65 54 79 70 65 2e 20 4e 6f 74 65 20  ery.eType. Note 
33958 74 68 61 74 20 74 68 65 20 6f 72 64 65 72 20 6f  that the order o
33959 66 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 66  f the first.** f
3395a 6f 75 72 20 76 61 6c 75 65 73 20 69 73 20 69 6e  our values is in
3395b 20 6f 72 64 65 72 20 6f 66 20 70 72 65 63 65 64   order of preced
3395c 65 6e 63 65 20 77 68 65 6e 20 70 61 72 73 69 6e  ence when parsin
3395d 67 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 20 46  g expressions. F
3395e 6f 72 20 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c 20  or .** example, 
3395f 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a  the following:.*
33960 2a 0a 2a 2a 20 20 20 22 61 20 4f 52 20 62 20 41  *.**   "a OR b A
33961 4e 44 20 63 20 4e 4f 54 20 64 20 4e 45 41 52 20  ND c NOT d NEAR 
33962 65 22 0a 2a 2a 0a 2a 2a 20 69 73 20 65 71 75 69  e".**.** is equi
33963 76 61 6c 65 6e 74 20 74 6f 3a 0a 2a 2a 0a 2a 2a  valent to:.**.**
33964 20 20 20 22 61 20 4f 52 20 28 62 20 41 4e 44 20     "a OR (b AND 
33965 28 63 20 4e 4f 54 20 28 64 20 4e 45 41 52 20 65  (c NOT (d NEAR e
33966 29 29 29 22 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  )))".*/.#define 
33967 46 54 53 51 55 45 52 59 5f 4e 45 41 52 20 20 20  FTSQUERY_NEAR   
33968 31 0a 23 64 65 66 69 6e 65 20 46 54 53 51 55 45  1.#define FTSQUE
33969 52 59 5f 4e 4f 54 20 20 20 20 32 0a 23 64 65 66  RY_NOT    2.#def
3396a 69 6e 65 20 46 54 53 51 55 45 52 59 5f 41 4e 44  ine FTSQUERY_AND
3396b 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 46 54      3.#define FT
3396c 53 51 55 45 52 59 5f 4f 52 20 20 20 20 20 34 0a  SQUERY_OR     4.
3396d 23 64 65 66 69 6e 65 20 46 54 53 51 55 45 52 59  #define FTSQUERY
3396e 5f 50 48 52 41 53 45 20 35 0a 0a 23 69 66 64 65  _PHRASE 5..#ifde
3396f 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51  f SQLITE_TEST.SQ
33970 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
33971 64 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70  d sqlite3Fts3Exp
33972 72 49 6e 69 74 54 65 73 74 49 6e 74 65 72 66 61  rInitTestInterfa
33973 63 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b  ce(sqlite3 *db);
33974 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a  .#endif../******
33975 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
33976 66 74 73 33 5f 65 78 70 72 2e 68 20 2a 2a 2a 2a  fts3_expr.h ****
33977 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33978 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33979 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
3397a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
3397b 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
3397c 74 20 6f 66 66 20 69 6e 20 66 74 73 33 2e 63 20  t off in fts3.c 
3397d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3397e 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
3397f 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65  ******** Include
33980 20 66 74 73 33 5f 68 61 73 68 2e 68 20 69 6e 20   fts3_hash.h in 
33981 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 66 74  the middle of ft
33982 73 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s3.c ***********
33983 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
33984 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
33985 69 6c 65 20 66 74 73 33 5f 68 61 73 68 2e 68 20  ile fts3_hash.h 
33986 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33987 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33988 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
33989 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 32 32  001 September 22
3398a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
3398b 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
3398c 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
3398d 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
3398e 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
3398f 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
33990 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
33991 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
33992 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
33993 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
33994 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
33995 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
33996 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
33997 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
33998 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
33999 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
3399a 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
3399b 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
3399c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3399d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3399e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3399f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
339a0 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 68  ** This is the h
339a1 65 61 64 65 72 20 66 69 6c 65 20 66 6f 72 20 74  eader file for t
339a2 68 65 20 67 65 6e 65 72 69 63 20 68 61 73 68 2d  he generic hash-
339a3 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 61 74  table implemenat
339a4 69 6f 6e 0a 2a 2a 20 75 73 65 64 20 69 6e 20 53  ion.** used in S
339a5 51 4c 69 74 65 2e 20 20 57 65 27 76 65 20 6d 6f  QLite.  We've mo
339a6 64 69 66 69 65 64 20 69 74 20 73 6c 69 67 68 74  dified it slight
339a7 6c 79 20 74 6f 20 73 65 72 76 65 20 61 73 20 61  ly to serve as a
339a8 20 73 74 61 6e 64 61 6c 6f 6e 65 0a 2a 2a 20 68   standalone.** h
339a9 61 73 68 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d  ash table implem
339aa 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 74 68 65  entation for the
339ab 20 66 75 6c 6c 2d 74 65 78 74 20 69 6e 64 65 78   full-text index
339ac 69 6e 67 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a  ing module..**.*
339ad 2f 0a 23 69 66 6e 64 65 66 20 5f 46 54 53 33 5f  /.#ifndef _FTS3_
339ae 48 41 53 48 5f 48 5f 0a 23 64 65 66 69 6e 65 20  HASH_H_.#define 
339af 5f 46 54 53 33 5f 48 41 53 48 5f 48 5f 0a 0a 2f  _FTS3_HASH_H_../
339b0 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72  * Forward declar
339b1 61 74 69 6f 6e 73 20 6f 66 20 73 74 72 75 63 74  ations of struct
339b2 75 72 65 73 2e 20 2a 2f 0a 74 79 70 65 64 65 66  ures. */.typedef
339b3 20 73 74 72 75 63 74 20 66 74 73 33 48 61 73 68   struct fts3Hash
339b4 20 66 74 73 33 48 61 73 68 3b 0a 74 79 70 65 64   fts3Hash;.typed
339b5 65 66 20 73 74 72 75 63 74 20 66 74 73 33 48 61  ef struct fts3Ha
339b6 73 68 45 6c 65 6d 20 66 74 73 33 48 61 73 68 45  shElem fts3HashE
339b7 6c 65 6d 3b 0a 0a 2f 2a 20 41 20 63 6f 6d 70 6c  lem;../* A compl
339b8 65 74 65 20 68 61 73 68 20 74 61 62 6c 65 20 69  ete hash table i
339b9 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  s an instance of
339ba 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
339bb 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 65  tructure..** The
339bc 20 69 6e 74 65 72 6e 61 6c 73 20 6f 66 20 74 68   internals of th
339bd 69 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65  is structure are
339be 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20   intended to be 
339bf 6f 70 61 71 75 65 20 2d 2d 20 63 6c 69 65 6e 74  opaque -- client
339c0 0a 2a 2a 20 63 6f 64 65 20 73 68 6f 75 6c 64 20  .** code should 
339c1 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 61  not attempt to a
339c2 63 63 65 73 73 20 6f 72 20 6d 6f 64 69 66 79 20  ccess or modify 
339c3 74 68 65 20 66 69 65 6c 64 73 20 6f 66 20 74 68  the fields of th
339c4 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  is structure.** 
339c5 64 69 72 65 63 74 6c 79 2e 20 20 43 68 61 6e 67  directly.  Chang
339c6 65 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  e this structure
339c7 20 6f 6e 6c 79 20 62 79 20 75 73 69 6e 67 20 74   only by using t
339c8 68 65 20 72 6f 75 74 69 6e 65 73 20 62 65 6c 6f  he routines belo
339c9 77 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 6d  w..** However, m
339ca 61 6e 79 20 6f 66 20 74 68 65 20 22 70 72 6f 63  any of the "proc
339cb 65 64 75 72 65 73 22 20 61 6e 64 20 22 66 75 6e  edures" and "fun
339cc 63 74 69 6f 6e 73 22 20 66 6f 72 20 6d 6f 64 69  ctions" for modi
339cd 66 79 69 6e 67 20 61 6e 64 0a 2a 2a 20 61 63 63  fying and.** acc
339ce 65 73 73 69 6e 67 20 74 68 69 73 20 73 74 72 75  essing this stru
339cf 63 74 75 72 65 20 61 72 65 20 72 65 61 6c 6c 79  cture are really
339d0 20 6d 61 63 72 6f 73 2c 20 73 6f 20 77 65 20 63   macros, so we c
339d1 61 6e 27 74 20 72 65 61 6c 6c 79 20 6d 61 6b 65  an't really make
339d2 0a 2a 2a 20 74 68 69 73 20 73 74 72 75 63 74 75  .** this structu
339d3 72 65 20 6f 70 61 71 75 65 2e 0a 2a 2f 0a 73 74  re opaque..*/.st
339d4 72 75 63 74 20 66 74 73 33 48 61 73 68 20 7b 0a  ruct fts3Hash {.
339d5 20 20 63 68 61 72 20 6b 65 79 43 6c 61 73 73 3b    char keyClass;
339d6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 41 53            /* HAS
339d7 48 5f 49 4e 54 2c 20 5f 50 4f 49 4e 54 45 52 2c  H_INT, _POINTER,
339d8 20 5f 53 54 52 49 4e 47 2c 20 5f 42 49 4e 41 52   _STRING, _BINAR
339d9 59 20 2a 2f 0a 20 20 63 68 61 72 20 63 6f 70 79  Y */.  char copy
339da 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 2f  Key;           /
339db 2a 20 54 72 75 65 20 69 66 20 63 6f 70 79 20 6f  * True if copy o
339dc 66 20 6b 65 79 20 6d 61 64 65 20 6f 6e 20 69 6e  f key made on in
339dd 73 65 72 74 20 2a 2f 0a 20 20 69 6e 74 20 63 6f  sert */.  int co
339de 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  unt;            
339df 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
339e0 6e 74 72 69 65 73 20 69 6e 20 74 68 69 73 20 74  ntries in this t
339e1 61 62 6c 65 20 2a 2f 0a 20 20 66 74 73 33 48 61  able */.  fts3Ha
339e2 73 68 45 6c 65 6d 20 2a 66 69 72 73 74 3b 20 20  shElem *first;  
339e3 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 65    /* The first e
339e4 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 72  lement of the ar
339e5 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 68 74 73  ray */.  int hts
339e6 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ize;            
339e7 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 75   /* Number of bu
339e8 63 6b 65 74 73 20 69 6e 20 74 68 65 20 68 61 73  ckets in the has
339e9 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73 74 72  h table */.  str
339ea 75 63 74 20 5f 66 74 73 33 68 74 20 7b 20 20 20  uct _fts3ht {   
339eb 20 20 20 20 20 2f 2a 20 74 68 65 20 68 61 73 68       /* the hash
339ec 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e   table */.    in
339ed 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20 20  t count;        
339ee 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
339ef 20 6f 66 20 65 6e 74 72 69 65 73 20 77 69 74 68   of entries with
339f0 20 74 68 69 73 20 68 61 73 68 20 2a 2f 0a 20 20   this hash */.  
339f1 20 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a    fts3HashElem *
339f2 63 68 61 69 6e 3b 20 20 20 20 20 2f 2a 20 50 6f  chain;     /* Po
339f3 69 6e 74 65 72 20 74 6f 20 66 69 72 73 74 20 65  inter to first e
339f4 6e 74 72 79 20 77 69 74 68 20 74 68 69 73 20 68  ntry with this h
339f5 61 73 68 20 2a 2f 0a 20 20 7d 20 2a 68 74 3b 0a  ash */.  } *ht;.
339f6 7d 3b 0a 0a 2f 2a 20 45 61 63 68 20 65 6c 65 6d  };../* Each elem
339f7 65 6e 74 20 69 6e 20 74 68 65 20 68 61 73 68 20  ent in the hash 
339f8 74 61 62 6c 65 20 69 73 20 61 6e 20 69 6e 73 74  table is an inst
339f9 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
339fa 6f 77 69 6e 67 20 0a 2a 2a 20 73 74 72 75 63 74  owing .** struct
339fb 75 72 65 2e 20 20 41 6c 6c 20 65 6c 65 6d 65 6e  ure.  All elemen
339fc 74 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e  ts are stored on
339fd 20 61 20 73 69 6e 67 6c 65 20 64 6f 75 62 6c 79   a single doubly
339fe 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a 2a  -linked list..**
339ff 0a 2a 2a 20 41 67 61 69 6e 2c 20 74 68 69 73 20  .** Again, this 
33a00 73 74 72 75 63 74 75 72 65 20 69 73 20 69 6e 74  structure is int
33a01 65 6e 64 65 64 20 74 6f 20 62 65 20 6f 70 61 71  ended to be opaq
33a02 75 65 2c 20 62 75 74 20 69 74 20 63 61 6e 27 74  ue, but it can't
33a03 20 72 65 61 6c 6c 79 0a 2a 2a 20 62 65 20 6f 70   really.** be op
33a04 61 71 75 65 20 62 65 63 61 75 73 65 20 69 74 20  aque because it 
33a05 69 73 20 75 73 65 64 20 62 79 20 6d 61 63 72 6f  is used by macro
33a06 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 66 74 73  s..*/.struct fts
33a07 33 48 61 73 68 45 6c 65 6d 20 7b 0a 20 20 66 74  3HashElem {.  ft
33a08 73 33 48 61 73 68 45 6c 65 6d 20 2a 6e 65 78 74  s3HashElem *next
33a09 2c 20 2a 70 72 65 76 3b 20 2f 2a 20 4e 65 78 74  , *prev; /* Next
33a0a 20 61 6e 64 20 70 72 65 76 69 6f 75 73 20 65 6c   and previous el
33a0b 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 74 61  ements in the ta
33a0c 62 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 64  ble */.  void *d
33a0d 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20  ata;            
33a0e 20 20 20 20 2f 2a 20 44 61 74 61 20 61 73 73 6f      /* Data asso
33a0f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73  ciated with this
33a10 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 76 6f   element */.  vo
33a11 69 64 20 2a 70 4b 65 79 3b 20 69 6e 74 20 6e 4b  id *pKey; int nK
33a12 65 79 3b 20 20 20 20 20 20 2f 2a 20 4b 65 79 20  ey;      /* Key 
33a13 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
33a14 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a  this element */.
33a15 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 72 65 20  };../*.** There 
33a16 61 72 65 20 32 20 64 69 66 66 65 72 65 6e 74 20  are 2 different 
33a17 6d 6f 64 65 73 20 6f 66 20 6f 70 65 72 61 74 69  modes of operati
33a18 6f 6e 20 66 6f 72 20 61 20 68 61 73 68 20 74 61  on for a hash ta
33a19 62 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 46 54 53  ble:.**.**   FTS
33a1a 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 20 20 20  3_HASH_STRING   
33a1b 20 20 20 20 20 70 4b 65 79 20 70 6f 69 6e 74 73       pKey points
33a1c 20 74 6f 20 61 20 73 74 72 69 6e 67 20 74 68 61   to a string tha
33a1d 74 20 69 73 20 6e 4b 65 79 20 62 79 74 65 73 20  t is nKey bytes 
33a1e 6c 6f 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20  long.**         
33a1f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33a20 20 20 28 69 6e 63 6c 75 64 69 6e 67 20 74 68 65    (including the
33a21 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 6f 72   null-terminator
33a22 2c 20 69 66 20 61 6e 79 29 2e 20 20 43 61 73 65  , if any).  Case
33a23 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
33a24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73                is
33a25 20 72 65 73 70 65 63 74 65 64 20 69 6e 20 63 6f   respected in co
33a26 6d 70 61 72 69 73 6f 6e 73 2e 0a 2a 2a 0a 2a 2a  mparisons..**.**
33a27 20 20 20 46 54 53 33 5f 48 41 53 48 5f 42 49 4e     FTS3_HASH_BIN
33a28 41 52 59 20 20 20 20 20 20 20 20 70 4b 65 79 20  ARY        pKey 
33a29 70 6f 69 6e 74 73 20 74 6f 20 62 69 6e 61 72 79  points to binary
33a2a 20 64 61 74 61 20 6e 4b 65 79 20 62 79 74 65 73   data nKey bytes
33a2b 20 6c 6f 6e 67 2e 20 0a 2a 2a 20 20 20 20 20 20   long. .**      
33a2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33a2d 20 20 20 20 20 6d 65 6d 63 6d 70 28 29 20 69 73       memcmp() is
33a2e 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 61 72 65   used to compare
33a2f 20 6b 65 79 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 63   keys..**.** A c
33a30 6f 70 79 20 6f 66 20 74 68 65 20 6b 65 79 20 69  opy of the key i
33a31 73 20 6d 61 64 65 20 69 66 20 74 68 65 20 63 6f  s made if the co
33a32 70 79 4b 65 79 20 70 61 72 61 6d 65 74 65 72 20  pyKey parameter 
33a33 74 6f 20 66 74 73 33 48 61 73 68 49 6e 69 74 20  to fts3HashInit 
33a34 69 73 20 31 2e 20 20 0a 2a 2f 0a 23 64 65 66 69  is 1.  .*/.#defi
33a35 6e 65 20 46 54 53 33 5f 48 41 53 48 5f 53 54 52  ne FTS3_HASH_STR
33a36 49 4e 47 20 20 20 20 31 0a 23 64 65 66 69 6e 65  ING    1.#define
33a37 20 46 54 53 33 5f 48 41 53 48 5f 42 49 4e 41 52   FTS3_HASH_BINAR
33a38 59 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 20 41 63  Y    2../*.** Ac
33a39 63 65 73 73 20 72 6f 75 74 69 6e 65 73 2e 20 20  cess routines.  
33a3a 54 6f 20 64 65 6c 65 74 65 2c 20 69 6e 73 65 72  To delete, inser
33a3b 74 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  t a NULL pointer
33a3c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
33a3d 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
33a3e 46 74 73 33 48 61 73 68 49 6e 69 74 28 66 74 73  Fts3HashInit(fts
33a3f 33 48 61 73 68 2a 2c 20 69 6e 74 20 6b 65 79 74  3Hash*, int keyt
33a40 79 70 65 2c 20 69 6e 74 20 63 6f 70 79 4b 65 79  ype, int copyKey
33a41 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
33a42 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 46  E void *sqlite3F
33a43 74 73 33 48 61 73 68 49 6e 73 65 72 74 28 66 74  ts3HashInsert(ft
33a44 73 33 48 61 73 68 2a 2c 20 63 6f 6e 73 74 20 76  s3Hash*, const v
33a45 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e  oid *pKey, int n
33a46 4b 65 79 2c 20 76 6f 69 64 20 2a 70 44 61 74 61  Key, void *pData
33a47 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
33a48 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 46  E void *sqlite3F
33a49 74 73 33 48 61 73 68 46 69 6e 64 28 63 6f 6e 73  ts3HashFind(cons
33a4a 74 20 66 74 73 33 48 61 73 68 2a 2c 20 63 6f 6e  t fts3Hash*, con
33a4b 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69  st void *pKey, i
33a4c 6e 74 20 6e 4b 65 79 29 3b 0a 53 51 4c 49 54 45  nt nKey);.SQLITE
33a4d 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
33a4e 6c 69 74 65 33 46 74 73 33 48 61 73 68 43 6c 65  lite3Fts3HashCle
33a4f 61 72 28 66 74 73 33 48 61 73 68 2a 29 3b 0a 0a  ar(fts3Hash*);..
33a50 2f 2a 0a 2a 2a 20 53 68 6f 72 74 68 61 6e 64 20  /*.** Shorthand 
33a51 66 6f 72 20 74 68 65 20 66 75 6e 63 74 69 6f 6e  for the function
33a52 73 20 61 62 6f 76 65 0a 2a 2f 0a 23 64 65 66 69  s above.*/.#defi
33a53 6e 65 20 66 74 73 33 48 61 73 68 49 6e 69 74 20  ne fts3HashInit 
33a54 20 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73    sqlite3Fts3Has
33a55 68 49 6e 69 74 0a 23 64 65 66 69 6e 65 20 66 74  hInit.#define ft
33a56 73 33 48 61 73 68 49 6e 73 65 72 74 20 73 71 6c  s3HashInsert sql
33a57 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 73 65  ite3Fts3HashInse
33a58 72 74 0a 23 64 65 66 69 6e 65 20 66 74 73 33 48  rt.#define fts3H
33a59 61 73 68 46 69 6e 64 20 20 20 73 71 6c 69 74 65  ashFind   sqlite
33a5a 33 46 74 73 33 48 61 73 68 46 69 6e 64 0a 23 64  3Fts3HashFind.#d
33a5b 65 66 69 6e 65 20 66 74 73 33 48 61 73 68 43 6c  efine fts3HashCl
33a5c 65 61 72 20 20 73 71 6c 69 74 65 33 46 74 73 33  ear  sqlite3Fts3
33a5d 48 61 73 68 43 6c 65 61 72 0a 0a 2f 2a 0a 2a 2a  HashClear../*.**
33a5e 20 4d 61 63 72 6f 73 20 66 6f 72 20 6c 6f 6f 70   Macros for loop
33a5f 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20 65 6c 65  ing over all ele
33a60 6d 65 6e 74 73 20 6f 66 20 61 20 68 61 73 68 20  ments of a hash 
33a61 74 61 62 6c 65 2e 20 20 54 68 65 20 69 64 69 6f  table.  The idio
33a62 6d 20 69 73 0a 2a 2a 20 6c 69 6b 65 20 74 68 69  m is.** like thi
33a63 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 66 74 73 33 48  s:.**.**   fts3H
33a64 61 73 68 20 68 3b 0a 2a 2a 20 20 20 66 74 73 33  ash h;.**   fts3
33a65 48 61 73 68 45 6c 65 6d 20 2a 70 3b 0a 2a 2a 20  HashElem *p;.** 
33a66 20 20 2e 2e 2e 0a 2a 2a 20 20 20 66 6f 72 28 70    ....**   for(p
33a67 3d 66 74 73 33 48 61 73 68 46 69 72 73 74 28 26  =fts3HashFirst(&
33a68 68 29 3b 20 70 3b 20 70 3d 66 74 73 33 48 61 73  h); p; p=fts3Has
33a69 68 4e 65 78 74 28 70 29 29 7b 0a 2a 2a 20 20 20  hNext(p)){.**   
33a6a 20 20 53 6f 6d 65 53 74 72 75 63 74 75 72 65 20    SomeStructure 
33a6b 2a 70 44 61 74 61 20 3d 20 66 74 73 33 48 61 73  *pData = fts3Has
33a6c 68 44 61 74 61 28 70 29 3b 0a 2a 2a 20 20 20 20  hData(p);.**    
33a6d 20 2f 2f 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67   // do something
33a6e 20 77 69 74 68 20 70 44 61 74 61 0a 2a 2a 20 20   with pData.**  
33a6f 20 7d 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 66 74   }.*/.#define ft
33a70 73 33 48 61 73 68 46 69 72 73 74 28 48 29 20 20  s3HashFirst(H)  
33a71 28 28 48 29 2d 3e 66 69 72 73 74 29 0a 23 64 65  ((H)->first).#de
33a72 66 69 6e 65 20 66 74 73 33 48 61 73 68 4e 65 78  fine fts3HashNex
33a73 74 28 45 29 20 20 20 28 28 45 29 2d 3e 6e 65 78  t(E)   ((E)->nex
33a74 74 29 0a 23 64 65 66 69 6e 65 20 66 74 73 33 48  t).#define fts3H
33a75 61 73 68 44 61 74 61 28 45 29 20 20 20 28 28 45  ashData(E)   ((E
33a76 29 2d 3e 64 61 74 61 29 0a 23 64 65 66 69 6e 65  )->data).#define
33a77 20 66 74 73 33 48 61 73 68 4b 65 79 28 45 29 20   fts3HashKey(E) 
33a78 20 20 20 28 28 45 29 2d 3e 70 4b 65 79 29 0a 23     ((E)->pKey).#
33a79 64 65 66 69 6e 65 20 66 74 73 33 48 61 73 68 4b  define fts3HashK
33a7a 65 79 73 69 7a 65 28 45 29 20 28 28 45 29 2d 3e  eysize(E) ((E)->
33a7b 6e 4b 65 79 29 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d  nKey)../*.** Num
33a7c 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
33a7d 6e 20 61 20 68 61 73 68 20 74 61 62 6c 65 0a 2a  n a hash table.*
33a7e 2f 0a 23 64 65 66 69 6e 65 20 66 74 73 33 48 61  /.#define fts3Ha
33a7f 73 68 43 6f 75 6e 74 28 48 29 20 20 28 28 48 29  shCount(H)  ((H)
33a80 2d 3e 63 6f 75 6e 74 29 0a 0a 23 65 6e 64 69 66  ->count)..#endif
33a81 20 2f 2a 20 5f 46 54 53 33 5f 48 41 53 48 5f 48   /* _FTS3_HASH_H
33a82 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  _ */../*********
33a83 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73  ***** End of fts
33a84 33 5f 68 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a  3_hash.h *******
33a85 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33a86 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33a87 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
33a88 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
33a89 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
33a8a 66 66 20 69 6e 20 66 74 73 33 2e 63 20 2a 2a 2a  ff in fts3.c ***
33a8b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33a8c 2a 2a 2a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  ****/.#ifndef SQ
33a8d 4c 49 54 45 5f 43 4f 52 45 20 0a 20 20 53 51 4c  LITE_CORE .  SQL
33a8e 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e  ITE_EXTENSION_IN
33a8f 49 54 31 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 20  IT1.#endif.../* 
33a90 54 4f 44 4f 28 73 68 65 73 73 29 20 4d 41 4e 2c  TODO(shess) MAN,
33a91 20 74 68 69 73 20 74 68 69 6e 67 20 6e 65 65 64   this thing need
33a92 73 20 73 6f 6d 65 20 72 65 66 61 63 74 6f 72 69  s some refactori
33a93 6e 67 2e 20 20 41 74 20 6d 69 6e 69 6d 75 6d 2c  ng.  At minimum,
33a94 20 69 74 0a 2a 2a 20 77 6f 75 6c 64 20 62 65 20   it.** would be 
33a95 6e 69 63 65 20 74 6f 20 6f 72 64 65 72 20 74 68  nice to order th
33a96 65 20 66 69 6c 65 20 62 65 74 74 65 72 2c 20 70  e file better, p
33a97 65 72 68 61 70 73 20 73 6f 6d 65 74 68 69 6e 67  erhaps something
33a98 20 61 6c 6f 6e 67 20 74 68 65 0a 2a 2a 20 6c 69   along the.** li
33a99 6e 65 73 20 6f 66 3a 0a 2a 2a 0a 2a 2a 20 20 2d  nes of:.**.**  -
33a9a 20 75 74 69 6c 69 74 79 20 66 75 6e 63 74 69 6f   utility functio
33a9b 6e 73 0a 2a 2a 20 20 2d 20 74 61 62 6c 65 20 73  ns.**  - table s
33a9c 65 74 75 70 20 66 75 6e 63 74 69 6f 6e 73 0a 2a  etup functions.*
33a9d 2a 20 20 2d 20 74 61 62 6c 65 20 75 70 64 61 74  *  - table updat
33a9e 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 20  e functions.**  
33a9f 2d 20 74 61 62 6c 65 20 71 75 65 72 79 20 66 75  - table query fu
33aa0 6e 63 74 69 6f 6e 73 0a 2a 2a 0a 2a 2a 20 50 75  nctions.**.** Pu
33aa1 74 20 74 68 65 20 71 75 65 72 79 20 66 75 6e 63  t the query func
33aa2 74 69 6f 6e 73 20 6c 61 73 74 20 62 65 63 61 75  tions last becau
33aa3 73 65 20 74 68 65 79 27 72 65 20 6c 69 6b 65 6c  se they're likel
33aa4 79 20 74 6f 20 72 65 66 65 72 65 6e 63 65 0a 2a  y to reference.*
33aa5 2a 20 74 79 70 65 64 65 66 73 20 6f 72 20 66 75  * typedefs or fu
33aa6 6e 63 74 69 6f 6e 73 20 66 72 6f 6d 20 74 68 65  nctions from the
33aa7 20 74 61 62 6c 65 20 75 70 64 61 74 65 20 73 65   table update se
33aa8 63 74 69 6f 6e 2e 0a 2a 2f 0a 0a 23 69 66 20 30  ction..*/..#if 0
33aa9 0a 23 20 64 65 66 69 6e 65 20 46 54 53 54 52 41  .# define FTSTRA
33aaa 43 45 28 41 29 20 20 70 72 69 6e 74 66 20 41 3b  CE(A)  printf A;
33aab 20 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29 0a   fflush(stdout).
33aac 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 46  #else.# define F
33aad 54 53 54 52 41 43 45 28 41 29 0a 23 65 6e 64 69  TSTRACE(A).#endi
33aae 66 0a 0a 2f 2a 20 49 74 20 69 73 20 6e 6f 74 20  f../* It is not 
33aaf 73 61 66 65 20 74 6f 20 63 61 6c 6c 20 69 73 73  safe to call iss
33ab0 70 61 63 65 28 29 2c 20 74 6f 6c 6f 77 65 72 28  pace(), tolower(
33ab1 29 2c 20 6f 72 20 69 73 61 6c 6e 75 6d 28 29 20  ), or isalnum() 
33ab2 6f 6e 0a 2a 2a 20 68 69 2d 62 69 74 2d 73 65 74  on.** hi-bit-set
33ab3 20 63 68 61 72 61 63 74 65 72 73 2e 20 20 54 68   characters.  Th
33ab4 69 73 20 69 73 20 74 68 65 20 73 61 6d 65 20 73  is is the same s
33ab5 6f 6c 75 74 69 6f 6e 20 75 73 65 64 20 69 6e 20  olution used in 
33ab6 74 68 65 0a 2a 2a 20 74 6f 6b 65 6e 69 7a 65 72  the.** tokenizer
33ab7 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65  ..*/./* TODO(she
33ab8 73 73 29 20 54 68 65 20 73 6e 69 70 70 65 74 2d  ss) The snippet-
33ab9 67 65 6e 65 72 61 74 69 6f 6e 20 63 6f 64 65 20  generation code 
33aba 73 68 6f 75 6c 64 20 62 65 20 75 73 69 6e 67 20  should be using 
33abb 74 68 65 0a 2a 2a 20 74 6f 6b 65 6e 69 7a 65 72  the.** tokenizer
33abc 2d 67 65 6e 65 72 61 74 65 64 20 74 6f 6b 65 6e  -generated token
33abd 73 20 72 61 74 68 65 72 20 74 68 61 6e 20 64 6f  s rather than do
33abe 69 6e 67 20 69 74 73 20 6f 77 6e 20 6c 6f 63 61  ing its own loca
33abf 6c 0a 2a 2a 20 74 6f 6b 65 6e 69 7a 61 74 69 6f  l.** tokenizatio
33ac0 6e 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68  n..*/./* TODO(sh
33ac1 65 73 73 29 20 49 73 20 5f 5f 69 73 61 73 63 69  ess) Is __isasci
33ac2 69 28 29 20 61 20 70 6f 72 74 61 62 6c 65 20 76  i() a portable v
33ac3 65 72 73 69 6f 6e 20 6f 66 20 28 63 26 30 78 38  ersion of (c&0x8
33ac4 30 29 3d 3d 30 3f 20 2a 2f 0a 73 74 61 74 69 63  0)==0? */.static
33ac5 20 69 6e 74 20 73 61 66 65 5f 69 73 73 70 61 63   int safe_isspac
33ac6 65 28 63 68 61 72 20 63 29 7b 0a 20 20 72 65 74  e(char c){.  ret
33ac7 75 72 6e 20 28 63 26 30 78 38 30 29 3d 3d 30 20  urn (c&0x80)==0 
33ac8 3f 20 69 73 73 70 61 63 65 28 63 29 20 3a 20 30  ? isspace(c) : 0
33ac9 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73  ;.}.static int s
33aca 61 66 65 5f 74 6f 6c 6f 77 65 72 28 63 68 61 72  afe_tolower(char
33acb 20 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 63   c){.  return (c
33acc 26 30 78 38 30 29 3d 3d 30 20 3f 20 74 6f 6c 6f  &0x80)==0 ? tolo
33acd 77 65 72 28 63 29 20 3a 20 63 3b 0a 7d 0a 73 74  wer(c) : c;.}.st
33ace 61 74 69 63 20 69 6e 74 20 73 61 66 65 5f 69 73  atic int safe_is
33acf 61 6c 6e 75 6d 28 63 68 61 72 20 63 29 7b 0a 20  alnum(char c){. 
33ad0 20 72 65 74 75 72 6e 20 28 63 26 30 78 38 30 29   return (c&0x80)
33ad1 3d 3d 30 20 3f 20 69 73 61 6c 6e 75 6d 28 63 29  ==0 ? isalnum(c)
33ad2 20 3a 20 30 3b 0a 7d 0a 0a 74 79 70 65 64 65 66   : 0;.}..typedef
33ad3 20 65 6e 75 6d 20 44 6f 63 4c 69 73 74 54 79 70   enum DocListTyp
33ad4 65 20 7b 0a 20 20 44 4c 5f 44 4f 43 49 44 53 2c  e {.  DL_DOCIDS,
33ad5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
33ad6 20 64 6f 63 69 64 73 20 6f 6e 6c 79 20 2a 2f 0a   docids only */.
33ad7 20 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 2c 20    DL_POSITIONS, 
33ad8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 64 6f 63            /* doc
33ad9 69 64 73 20 2b 20 70 6f 73 69 74 69 6f 6e 73 20  ids + positions 
33ada 2a 2f 0a 20 20 44 4c 5f 50 4f 53 49 54 49 4f 4e  */.  DL_POSITION
33adb 53 5f 4f 46 46 53 45 54 53 20 20 20 20 2f 2a 20  S_OFFSETS    /* 
33adc 64 6f 63 69 64 73 20 2b 20 70 6f 73 69 74 69 6f  docids + positio
33add 6e 73 20 2b 20 6f 66 66 73 65 74 73 20 2a 2f 0a  ns + offsets */.
33ade 7d 20 44 6f 63 4c 69 73 74 54 79 70 65 3b 0a 0a  } DocListType;..
33adf 2f 2a 0a 2a 2a 20 42 79 20 64 65 66 61 75 6c 74  /*.** By default
33ae0 2c 20 6f 6e 6c 79 20 70 6f 73 69 74 69 6f 6e 73  , only positions
33ae1 20 61 6e 64 20 6e 6f 74 20 6f 66 66 73 65 74 73   and not offsets
33ae2 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 74   are stored in t
33ae3 68 65 20 64 6f 63 6c 69 73 74 73 2e 0a 2a 2a 20  he doclists..** 
33ae4 54 6f 20 63 68 61 6e 67 65 20 74 68 69 73 20 73  To change this s
33ae5 6f 20 74 68 61 74 20 6f 66 66 73 65 74 73 20 61  o that offsets a
33ae6 72 65 20 73 74 6f 72 65 64 20 74 6f 6f 2c 20 63  re stored too, c
33ae7 6f 6d 70 69 6c 65 20 77 69 74 68 0a 2a 2a 0a 2a  ompile with.**.*
33ae8 2a 20 20 20 20 20 20 20 20 20 20 2d 44 44 4c 5f  *          -DDL_
33ae9 44 45 46 41 55 4c 54 3d 44 4c 5f 50 4f 53 49 54  DEFAULT=DL_POSIT
33aea 49 4f 4e 53 5f 4f 46 46 53 45 54 53 0a 2a 2a 0a  IONS_OFFSETS.**.
33aeb 2a 2a 20 49 66 20 44 4c 5f 44 45 46 41 55 4c 54  ** If DL_DEFAULT
33aec 20 69 73 20 73 65 74 20 74 6f 20 44 4c 5f 44 4f   is set to DL_DO
33aed 43 49 44 53 2c 20 79 6f 75 72 20 74 61 62 6c 65  CIDS, your table
33aee 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 69 6e 73   can only be ins
33aef 65 72 74 65 64 0a 2a 2a 20 69 6e 74 6f 20 28 6e  erted.** into (n
33af0 6f 20 64 65 6c 65 74 65 73 20 6f 72 20 75 70 64  o deletes or upd
33af1 61 74 65 73 29 2e 0a 2a 2f 0a 23 69 66 6e 64 65  ates)..*/.#ifnde
33af2 66 20 44 4c 5f 44 45 46 41 55 4c 54 0a 23 20 64  f DL_DEFAULT.# d
33af3 65 66 69 6e 65 20 44 4c 5f 44 45 46 41 55 4c 54  efine DL_DEFAULT
33af4 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 0a 23 65   DL_POSITIONS.#e
33af5 6e 64 69 66 0a 0a 65 6e 75 6d 20 7b 0a 20 20 50  ndif..enum {.  P
33af6 4f 53 5f 45 4e 44 20 3d 20 30 2c 20 20 20 20 20  OS_END = 0,     
33af7 20 20 20 2f 2a 20 65 6e 64 20 6f 66 20 74 68 69     /* end of thi
33af8 73 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 20  s position list 
33af9 2a 2f 0a 20 20 50 4f 53 5f 43 4f 4c 55 4d 4e 2c  */.  POS_COLUMN,
33afa 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6f 6c 6c           /* foll
33afb 6f 77 65 64 20 62 79 20 6e 65 77 20 63 6f 6c 75  owed by new colu
33afc 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 50  mn number */.  P
33afd 4f 53 5f 42 41 53 45 0a 7d 3b 0a 0a 2f 2a 20 4d  OS_BASE.};../* M
33afe 45 52 47 45 5f 43 4f 55 4e 54 20 63 6f 6e 74 72  ERGE_COUNT contr
33aff 6f 6c 73 20 68 6f 77 20 6f 66 74 65 6e 20 77 65  ols how often we
33b00 20 6d 65 72 67 65 20 73 65 67 6d 65 6e 74 73 20   merge segments 
33b01 28 73 65 65 20 63 6f 6d 6d 65 6e 74 20 61 74 0a  (see comment at.
33b02 2a 2a 20 74 6f 70 20 6f 66 20 66 69 6c 65 29 2e  ** top of file).
33b03 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 52 47  .*/.#define MERG
33b04 45 5f 43 4f 55 4e 54 20 31 36 0a 0a 2f 2a 20 75  E_COUNT 16../* u
33b05 74 69 6c 69 74 79 20 66 75 6e 63 74 69 6f 6e 73  tility functions
33b06 20 2a 2f 0a 0a 2f 2a 20 43 4c 45 41 52 28 29 20   */../* CLEAR() 
33b07 61 6e 64 20 53 43 52 41 4d 42 4c 45 28 29 20 61  and SCRAMBLE() a
33b08 62 73 74 72 61 63 74 20 6d 65 6d 73 65 74 28 29  bstract memset()
33b09 20 6f 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   on a pointer to
33b0a 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 72 65 63   a single.** rec
33b0b 6f 72 64 20 74 6f 20 70 72 65 76 65 6e 74 20 65  ord to prevent e
33b0c 72 72 6f 72 73 20 6f 66 20 74 68 65 20 66 6f 72  rrors of the for
33b0d 6d 3a 0a 2a 2a 0a 2a 2a 20 6d 79 5f 66 75 6e 63  m:.**.** my_func
33b0e 74 69 6f 6e 28 53 6f 6d 65 54 79 70 65 20 2a 62  tion(SomeType *b
33b0f 29 7b 0a 2a 2a 20 20 20 6d 65 6d 73 65 74 28 62  ){.**   memset(b
33b10 2c 20 27 5c 30 27 2c 20 73 69 7a 65 6f 66 28 62  , '\0', sizeof(b
33b11 29 29 3b 20 20 2f 2f 20 73 69 7a 65 6f 66 28 62  ));  // sizeof(b
33b12 29 21 3d 73 69 7a 65 6f 66 28 2a 62 29 0a 2a 2a  )!=sizeof(*b).**
33b13 20 7d 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68   }.*/./* TODO(sh
33b14 65 73 73 29 20 4f 62 76 69 6f 75 73 20 63 61 6e  ess) Obvious can
33b15 64 69 64 61 74 65 73 20 66 6f 72 20 61 20 68 65  didates for a he
33b16 61 64 65 72 20 66 69 6c 65 2e 20 2a 2f 0a 23 64  ader file. */.#d
33b17 65 66 69 6e 65 20 43 4c 45 41 52 28 62 29 20 6d  efine CLEAR(b) m
33b18 65 6d 73 65 74 28 62 2c 20 27 5c 30 27 2c 20 73  emset(b, '\0', s
33b19 69 7a 65 6f 66 28 2a 28 62 29 29 29 0a 0a 23 69  izeof(*(b)))..#i
33b1a 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 23 20 20  fndef NDEBUG.#  
33b1b 64 65 66 69 6e 65 20 53 43 52 41 4d 42 4c 45 28  define SCRAMBLE(
33b1c 62 29 20 6d 65 6d 73 65 74 28 62 2c 20 30 78 35  b) memset(b, 0x5
33b1d 35 2c 20 73 69 7a 65 6f 66 28 2a 28 62 29 29 29  5, sizeof(*(b)))
33b1e 0a 23 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65  .#else.#  define
33b1f 20 53 43 52 41 4d 42 4c 45 28 62 29 0a 23 65 6e   SCRAMBLE(b).#en
33b20 64 69 66 0a 0a 2f 2a 20 57 65 20 6d 61 79 20 6e  dif../* We may n
33b21 65 65 64 20 75 70 20 74 6f 20 56 41 52 49 4e 54  eed up to VARINT
33b22 5f 4d 41 58 20 62 79 74 65 73 20 74 6f 20 73 74  _MAX bytes to st
33b23 6f 72 65 20 61 6e 20 65 6e 63 6f 64 65 64 20 36  ore an encoded 6
33b24 34 2d 62 69 74 20 69 6e 74 65 67 65 72 2e 20 2a  4-bit integer. *
33b25 2f 0a 23 64 65 66 69 6e 65 20 56 41 52 49 4e 54  /.#define VARINT
33b26 5f 4d 41 58 20 31 30 0a 0a 2f 2a 20 57 72 69 74  _MAX 10../* Writ
33b27 65 20 61 20 36 34 2d 62 69 74 20 76 61 72 69 61  e a 64-bit varia
33b28 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67  ble-length integ
33b29 65 72 20 74 6f 20 6d 65 6d 6f 72 79 20 73 74 61  er to memory sta
33b2a 72 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 20  rting at p[0].. 
33b2b 2a 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 20  * The length of 
33b2c 64 61 74 61 20 77 72 69 74 74 65 6e 20 77 69 6c  data written wil
33b2d 6c 20 62 65 20 62 65 74 77 65 65 6e 20 31 20 61  l be between 1 a
33b2e 6e 64 20 56 41 52 49 4e 54 5f 4d 41 58 20 62 79  nd VARINT_MAX by
33b2f 74 65 73 2e 0a 20 2a 20 54 68 65 20 6e 75 6d 62  tes.. * The numb
33b30 65 72 20 6f 66 20 62 79 74 65 73 20 77 72 69 74  er of bytes writ
33b31 74 65 6e 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ten is returned.
33b32 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66   */.static int f
33b33 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 68 61  ts3PutVarint(cha
33b34 72 20 2a 70 2c 20 73 71 6c 69 74 65 5f 69 6e 74  r *p, sqlite_int
33b35 36 34 20 76 29 7b 0a 20 20 75 6e 73 69 67 6e 65  64 v){.  unsigne
33b36 64 20 63 68 61 72 20 2a 71 20 3d 20 28 75 6e 73  d char *q = (uns
33b37 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20 70 3b  igned char *) p;
33b38 0a 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  .  sqlite_uint64
33b39 20 76 75 20 3d 20 76 3b 0a 20 20 64 6f 7b 0a 20   vu = v;.  do{. 
33b3a 20 20 20 2a 71 2b 2b 20 3d 20 28 75 6e 73 69 67     *q++ = (unsig
33b3b 6e 65 64 20 63 68 61 72 29 20 28 28 76 75 20 26  ned char) ((vu &
33b3c 20 30 78 37 66 29 20 7c 20 30 78 38 30 29 3b 0a   0x7f) | 0x80);.
33b3d 20 20 20 20 76 75 20 3e 3e 3d 20 37 3b 0a 20 20      vu >>= 7;.  
33b3e 7d 77 68 69 6c 65 28 20 76 75 21 3d 30 20 29 3b  }while( vu!=0 );
33b3f 0a 20 20 71 5b 2d 31 5d 20 26 3d 20 30 78 37 66  .  q[-1] &= 0x7f
33b40 3b 20 20 2f 2a 20 74 75 72 6e 20 6f 66 66 20 68  ;  /* turn off h
33b41 69 67 68 20 62 69 74 20 69 6e 20 66 69 6e 61 6c  igh bit in final
33b42 20 62 79 74 65 20 2a 2f 0a 20 20 61 73 73 65 72   byte */.  asser
33b43 74 28 20 71 20 2d 20 28 75 6e 73 69 67 6e 65 64  t( q - (unsigned
33b44 20 63 68 61 72 20 2a 29 70 20 3c 3d 20 56 41 52   char *)p <= VAR
33b45 49 4e 54 5f 4d 41 58 20 29 3b 0a 20 20 72 65 74  INT_MAX );.  ret
33b46 75 72 6e 20 28 69 6e 74 29 20 28 71 20 2d 20 28  urn (int) (q - (
33b47 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
33b48 70 29 3b 0a 7d 0a 0a 2f 2a 20 52 65 61 64 20 61  p);.}../* Read a
33b49 20 36 34 2d 62 69 74 20 76 61 72 69 61 62 6c 65   64-bit variable
33b4a 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20  -length integer 
33b4b 66 72 6f 6d 20 6d 65 6d 6f 72 79 20 73 74 61 72  from memory star
33b4c 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 20 2a  ting at p[0].. *
33b4d 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
33b4e 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 61 64  er of bytes read
33b4f 2c 20 6f 72 20 30 20 6f 6e 20 65 72 72 6f 72 2e  , or 0 on error.
33b50 0a 20 2a 20 54 68 65 20 76 61 6c 75 65 20 69 73  . * The value is
33b51 20 73 74 6f 72 65 64 20 69 6e 20 2a 76 2e 20 2a   stored in *v. *
33b52 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
33b53 33 47 65 74 56 61 72 69 6e 74 28 63 6f 6e 73 74  3GetVarint(const
33b54 20 63 68 61 72 20 2a 70 2c 20 73 71 6c 69 74 65   char *p, sqlite
33b55 5f 69 6e 74 36 34 20 2a 76 29 7b 0a 20 20 63 6f  _int64 *v){.  co
33b56 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
33b57 72 20 2a 71 20 3d 20 28 63 6f 6e 73 74 20 75 6e  r *q = (const un
33b58 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20 70  signed char *) p
33b59 3b 0a 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  ;.  sqlite_uint6
33b5a 34 20 78 20 3d 20 30 2c 20 79 20 3d 20 31 3b 0a  4 x = 0, y = 1;.
33b5b 20 20 77 68 69 6c 65 28 20 28 2a 71 20 26 20 30    while( (*q & 0
33b5c 78 38 30 29 20 3d 3d 20 30 78 38 30 20 29 7b 0a  x80) == 0x80 ){.
33b5d 20 20 20 20 78 20 2b 3d 20 79 20 2a 20 28 2a 71      x += y * (*q
33b5e 2b 2b 20 26 20 30 78 37 66 29 3b 0a 20 20 20 20  ++ & 0x7f);.    
33b5f 79 20 3c 3c 3d 20 37 3b 0a 20 20 20 20 69 66 28  y <<= 7;.    if(
33b60 20 71 20 2d 20 28 75 6e 73 69 67 6e 65 64 20 63   q - (unsigned c
33b61 68 61 72 20 2a 29 70 20 3e 3d 20 56 41 52 49 4e  har *)p >= VARIN
33b62 54 5f 4d 41 58 20 29 7b 20 20 2f 2a 20 62 61 64  T_MAX ){  /* bad
33b63 20 64 61 74 61 20 2a 2f 0a 20 20 20 20 20 20 61   data */.      a
33b64 73 73 65 72 74 28 20 30 20 29 3b 0a 20 20 20 20  ssert( 0 );.    
33b65 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
33b66 7d 0a 20 20 7d 0a 20 20 78 20 2b 3d 20 79 20 2a  }.  }.  x += y *
33b67 20 28 2a 71 2b 2b 29 3b 0a 20 20 2a 76 20 3d 20   (*q++);.  *v = 
33b68 28 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 20 78  (sqlite_int64) x
33b69 3b 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29  ;.  return (int)
33b6a 20 28 71 20 2d 20 28 75 6e 73 69 67 6e 65 64 20   (q - (unsigned 
33b6b 63 68 61 72 20 2a 29 70 29 3b 0a 7d 0a 0a 73 74  char *)p);.}..st
33b6c 61 74 69 63 20 69 6e 74 20 66 74 73 33 47 65 74  atic int fts3Get
33b6d 56 61 72 69 6e 74 33 32 28 63 6f 6e 73 74 20 63  Varint32(const c
33b6e 68 61 72 20 2a 70 2c 20 69 6e 74 20 2a 70 69 29  har *p, int *pi)
33b6f 7b 0a 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  {. sqlite_int64 
33b70 69 3b 0a 20 69 6e 74 20 72 65 74 20 3d 20 66 74  i;. int ret = ft
33b71 73 33 47 65 74 56 61 72 69 6e 74 28 70 2c 20 26  s3GetVarint(p, &
33b72 69 29 3b 0a 20 2a 70 69 20 3d 20 28 69 6e 74 29  i);. *pi = (int)
33b73 20 69 3b 0a 20 61 73 73 65 72 74 28 20 2a 70 69   i;. assert( *pi
33b74 3d 3d 69 20 29 3b 0a 20 72 65 74 75 72 6e 20 72  ==i );. return r
33b75 65 74 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  et;.}../********
33b76 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33b77 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33b78 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33b79 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20  ***********/./* 
33b7a 44 61 74 61 42 75 66 66 65 72 20 69 73 20 75 73  DataBuffer is us
33b7b 65 64 20 74 6f 20 63 6f 6c 6c 65 63 74 20 64 61  ed to collect da
33b7c 74 61 20 69 6e 74 6f 20 61 20 62 75 66 66 65 72  ta into a buffer
33b7d 20 69 6e 20 70 69 65 63 65 6d 65 61 6c 0a 2a 2a   in piecemeal.**
33b7e 20 66 61 73 68 69 6f 6e 2e 20 20 49 74 20 69 6d   fashion.  It im
33b7f 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 75 73 75  plements the usu
33b80 61 6c 20 64 69 73 74 69 6e 63 74 69 6f 6e 20 62  al distinction b
33b81 65 74 77 65 65 6e 20 61 6d 6f 75 6e 74 20 6f 66  etween amount of
33b82 0a 2a 2a 20 64 61 74 61 20 63 75 72 72 65 6e 74  .** data current
33b83 6c 79 20 73 74 6f 72 65 64 20 28 6e 44 61 74 61  ly stored (nData
33b84 29 20 61 6e 64 20 62 75 66 66 65 72 20 63 61 70  ) and buffer cap
33b85 61 63 69 74 79 20 28 6e 43 61 70 61 63 69 74 79  acity (nCapacity
33b86 29 2e 0a 2a 2a 0a 2a 2a 20 64 61 74 61 42 75 66  )..**.** dataBuf
33b87 66 65 72 49 6e 69 74 20 2d 20 63 72 65 61 74 65  ferInit - create
33b88 20 61 20 62 75 66 66 65 72 20 77 69 74 68 20 67   a buffer with g
33b89 69 76 65 6e 20 69 6e 69 74 69 61 6c 20 63 61 70  iven initial cap
33b8a 61 63 69 74 79 2e 0a 2a 2a 20 64 61 74 61 42 75  acity..** dataBu
33b8b 66 66 65 72 52 65 73 65 74 20 2d 20 66 6f 72 67  fferReset - forg
33b8c 65 74 20 62 75 66 66 65 72 27 73 20 64 61 74 61  et buffer's data
33b8d 2c 20 72 65 74 61 69 6e 69 6e 67 20 63 61 70 61  , retaining capa
33b8e 63 69 74 79 2e 0a 2a 2a 20 64 61 74 61 42 75 66  city..** dataBuf
33b8f 66 65 72 44 65 73 74 72 6f 79 20 2d 20 66 72 65  ferDestroy - fre
33b90 65 20 62 75 66 66 65 72 27 73 20 64 61 74 61 2e  e buffer's data.
33b91 0a 2a 2a 20 64 61 74 61 42 75 66 66 65 72 53 77  .** dataBufferSw
33b92 61 70 20 2d 20 73 77 61 70 20 63 6f 6e 74 65 6e  ap - swap conten
33b93 74 73 20 6f 66 20 74 77 6f 20 62 75 66 66 65 72  ts of two buffer
33b94 73 2e 0a 2a 2a 20 64 61 74 61 42 75 66 66 65 72  s..** dataBuffer
33b95 45 78 70 61 6e 64 20 2d 20 65 78 70 61 6e 64 20  Expand - expand 
33b96 63 61 70 61 63 69 74 79 20 77 69 74 68 6f 75 74  capacity without
33b97 20 61 64 64 69 6e 67 20 64 61 74 61 2e 0a 2a 2a   adding data..**
33b98 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e   dataBufferAppen
33b99 64 20 2d 20 61 70 70 65 6e 64 20 64 61 74 61 2e  d - append data.
33b9a 0a 2a 2a 20 64 61 74 61 42 75 66 66 65 72 41 70  .** dataBufferAp
33b9b 70 65 6e 64 32 20 2d 20 61 70 70 65 6e 64 20 74  pend2 - append t
33b9c 77 6f 20 70 69 65 63 65 73 20 6f 66 20 64 61 74  wo pieces of dat
33b9d 61 20 61 74 20 6f 6e 63 65 2e 0a 2a 2a 20 64 61  a at once..** da
33b9e 74 61 42 75 66 66 65 72 52 65 70 6c 61 63 65 20  taBufferReplace 
33b9f 2d 20 72 65 70 6c 61 63 65 20 62 75 66 66 65 72  - replace buffer
33ba0 27 73 20 64 61 74 61 2e 0a 2a 2f 0a 74 79 70 65  's data..*/.type
33ba1 64 65 66 20 73 74 72 75 63 74 20 44 61 74 61 42  def struct DataB
33ba2 75 66 66 65 72 20 7b 0a 20 20 63 68 61 72 20 2a  uffer {.  char *
33ba3 70 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20  pData;          
33ba4 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6d 61  /* Pointer to ma
33ba5 6c 6c 6f 63 27 65 64 20 62 75 66 66 65 72 2e 20  lloc'ed buffer. 
33ba6 2a 2f 0a 20 20 69 6e 74 20 6e 43 61 70 61 63 69  */.  int nCapaci
33ba7 74 79 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 69  ty;        /* Si
33ba8 7a 65 20 6f 66 20 70 44 61 74 61 20 62 75 66 66  ze of pData buff
33ba9 65 72 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 61  er. */.  int nDa
33baa 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ta;            /
33bab 2a 20 45 6e 64 20 6f 66 20 64 61 74 61 20 6c 6f  * End of data lo
33bac 61 64 65 64 20 69 6e 74 6f 20 70 44 61 74 61 2e  aded into pData.
33bad 20 2a 2f 0a 7d 20 44 61 74 61 42 75 66 66 65 72   */.} DataBuffer
33bae 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64  ;..static void d
33baf 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 44 61  ataBufferInit(Da
33bb0 74 61 42 75 66 66 65 72 20 2a 70 42 75 66 66 65  taBuffer *pBuffe
33bb1 72 2c 20 69 6e 74 20 6e 43 61 70 61 63 69 74 79  r, int nCapacity
33bb2 29 7b 0a 20 20 61 73 73 65 72 74 28 20 6e 43 61  ){.  assert( nCa
33bb3 70 61 63 69 74 79 3e 3d 30 20 29 3b 0a 20 20 70  pacity>=0 );.  p
33bb4 42 75 66 66 65 72 2d 3e 6e 44 61 74 61 20 3d 20  Buffer->nData = 
33bb5 30 3b 0a 20 20 70 42 75 66 66 65 72 2d 3e 6e 43  0;.  pBuffer->nC
33bb6 61 70 61 63 69 74 79 20 3d 20 6e 43 61 70 61 63  apacity = nCapac
33bb7 69 74 79 3b 0a 20 20 70 42 75 66 66 65 72 2d 3e  ity;.  pBuffer->
33bb8 70 44 61 74 61 20 3d 20 6e 43 61 70 61 63 69 74  pData = nCapacit
33bb9 79 3d 3d 30 20 3f 20 4e 55 4c 4c 20 3a 20 73 71  y==0 ? NULL : sq
33bba 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 43 61  lite3_malloc(nCa
33bbb 70 61 63 69 74 79 29 3b 0a 7d 0a 73 74 61 74 69  pacity);.}.stati
33bbc 63 20 76 6f 69 64 20 64 61 74 61 42 75 66 66 65  c void dataBuffe
33bbd 72 52 65 73 65 74 28 44 61 74 61 42 75 66 66 65  rReset(DataBuffe
33bbe 72 20 2a 70 42 75 66 66 65 72 29 7b 0a 20 20 70  r *pBuffer){.  p
33bbf 42 75 66 66 65 72 2d 3e 6e 44 61 74 61 20 3d 20  Buffer->nData = 
33bc0 30 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  0;.}.static void
33bc1 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72   dataBufferDestr
33bc2 6f 79 28 44 61 74 61 42 75 66 66 65 72 20 2a 70  oy(DataBuffer *p
33bc3 42 75 66 66 65 72 29 7b 0a 20 20 69 66 28 20 70  Buffer){.  if( p
33bc4 42 75 66 66 65 72 2d 3e 70 44 61 74 61 21 3d 4e  Buffer->pData!=N
33bc5 55 4c 4c 20 29 20 73 71 6c 69 74 65 33 5f 66 72  ULL ) sqlite3_fr
33bc6 65 65 28 70 42 75 66 66 65 72 2d 3e 70 44 61 74  ee(pBuffer->pDat
33bc7 61 29 3b 0a 20 20 53 43 52 41 4d 42 4c 45 28 70  a);.  SCRAMBLE(p
33bc8 42 75 66 66 65 72 29 3b 0a 7d 0a 73 74 61 74 69  Buffer);.}.stati
33bc9 63 20 76 6f 69 64 20 64 61 74 61 42 75 66 66 65  c void dataBuffe
33bca 72 53 77 61 70 28 44 61 74 61 42 75 66 66 65 72  rSwap(DataBuffer
33bcb 20 2a 70 42 75 66 66 65 72 31 2c 20 44 61 74 61   *pBuffer1, Data
33bcc 42 75 66 66 65 72 20 2a 70 42 75 66 66 65 72 32  Buffer *pBuffer2
33bcd 29 7b 0a 20 20 44 61 74 61 42 75 66 66 65 72 20  ){.  DataBuffer 
33bce 74 6d 70 20 3d 20 2a 70 42 75 66 66 65 72 31 3b  tmp = *pBuffer1;
33bcf 0a 20 20 2a 70 42 75 66 66 65 72 31 20 3d 20 2a  .  *pBuffer1 = *
33bd0 70 42 75 66 66 65 72 32 3b 0a 20 20 2a 70 42 75  pBuffer2;.  *pBu
33bd1 66 66 65 72 32 20 3d 20 74 6d 70 3b 0a 7d 0a 73  ffer2 = tmp;.}.s
33bd2 74 61 74 69 63 20 76 6f 69 64 20 64 61 74 61 42  tatic void dataB
33bd3 75 66 66 65 72 45 78 70 61 6e 64 28 44 61 74 61  ufferExpand(Data
33bd4 42 75 66 66 65 72 20 2a 70 42 75 66 66 65 72 2c  Buffer *pBuffer,
33bd5 20 69 6e 74 20 6e 41 64 64 43 61 70 61 63 69 74   int nAddCapacit
33bd6 79 29 7b 0a 20 20 61 73 73 65 72 74 28 20 6e 41  y){.  assert( nA
33bd7 64 64 43 61 70 61 63 69 74 79 3e 30 20 29 3b 0a  ddCapacity>0 );.
33bd8 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29    /* TODO(shess)
33bd9 20 43 6f 6e 73 69 64 65 72 20 65 78 70 61 6e 64   Consider expand
33bda 69 6e 67 20 6d 6f 72 65 20 61 67 67 72 65 73 73  ing more aggress
33bdb 69 76 65 6c 79 2e 20 20 4e 6f 74 65 20 74 68 61  ively.  Note tha
33bdc 74 20 74 68 65 0a 20 20 2a 2a 20 75 6e 64 65 72  t the.  ** under
33bdd 6c 79 69 6e 67 20 6d 61 6c 6c 6f 63 20 69 6d 70  lying malloc imp
33bde 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 61 79 20  lementation may 
33bdf 74 61 6b 65 20 63 61 72 65 20 6f 66 20 73 75 63  take care of suc
33be0 68 20 74 68 69 6e 67 73 20 66 6f 72 0a 20 20 2a  h things for.  *
33be1 2a 20 75 73 20 61 6c 72 65 61 64 79 2e 0a 20 20  * us already..  
33be2 2a 2f 0a 20 20 69 66 28 20 70 42 75 66 66 65 72  */.  if( pBuffer
33be3 2d 3e 6e 44 61 74 61 2b 6e 41 64 64 43 61 70 61  ->nData+nAddCapa
33be4 63 69 74 79 3e 70 42 75 66 66 65 72 2d 3e 6e 43  city>pBuffer->nC
33be5 61 70 61 63 69 74 79 20 29 7b 0a 20 20 20 20 70  apacity ){.    p
33be6 42 75 66 66 65 72 2d 3e 6e 43 61 70 61 63 69 74  Buffer->nCapacit
33be7 79 20 3d 20 70 42 75 66 66 65 72 2d 3e 6e 44 61  y = pBuffer->nDa
33be8 74 61 2b 6e 41 64 64 43 61 70 61 63 69 74 79 3b  ta+nAddCapacity;
33be9 0a 20 20 20 20 70 42 75 66 66 65 72 2d 3e 70 44  .    pBuffer->pD
33bea 61 74 61 20 3d 20 73 71 6c 69 74 65 33 5f 72 65  ata = sqlite3_re
33beb 61 6c 6c 6f 63 28 70 42 75 66 66 65 72 2d 3e 70  alloc(pBuffer->p
33bec 44 61 74 61 2c 20 70 42 75 66 66 65 72 2d 3e 6e  Data, pBuffer->n
33bed 43 61 70 61 63 69 74 79 29 3b 0a 20 20 7d 0a 7d  Capacity);.  }.}
33bee 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 61 74  .static void dat
33bef 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 44 61  aBufferAppend(Da
33bf0 74 61 42 75 66 66 65 72 20 2a 70 42 75 66 66 65  taBuffer *pBuffe
33bf1 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r,.             
33bf2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33bf3 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 53 6f 75  const char *pSou
33bf4 72 63 65 2c 20 69 6e 74 20 6e 53 6f 75 72 63 65  rce, int nSource
33bf5 29 7b 0a 20 20 61 73 73 65 72 74 28 20 6e 53 6f  ){.  assert( nSo
33bf6 75 72 63 65 3e 30 20 26 26 20 70 53 6f 75 72 63  urce>0 && pSourc
33bf7 65 21 3d 4e 55 4c 4c 20 29 3b 0a 20 20 64 61 74  e!=NULL );.  dat
33bf8 61 42 75 66 66 65 72 45 78 70 61 6e 64 28 70 42  aBufferExpand(pB
33bf9 75 66 66 65 72 2c 20 6e 53 6f 75 72 63 65 29 3b  uffer, nSource);
33bfa 0a 20 20 6d 65 6d 63 70 79 28 70 42 75 66 66 65  .  memcpy(pBuffe
33bfb 72 2d 3e 70 44 61 74 61 2b 70 42 75 66 66 65 72  r->pData+pBuffer
33bfc 2d 3e 6e 44 61 74 61 2c 20 70 53 6f 75 72 63 65  ->nData, pSource
33bfd 2c 20 6e 53 6f 75 72 63 65 29 3b 0a 20 20 70 42  , nSource);.  pB
33bfe 75 66 66 65 72 2d 3e 6e 44 61 74 61 20 2b 3d 20  uffer->nData += 
33bff 6e 53 6f 75 72 63 65 3b 0a 7d 0a 73 74 61 74 69  nSource;.}.stati
33c00 63 20 76 6f 69 64 20 64 61 74 61 42 75 66 66 65  c void dataBuffe
33c01 72 41 70 70 65 6e 64 32 28 44 61 74 61 42 75 66  rAppend2(DataBuf
33c02 66 65 72 20 2a 70 42 75 66 66 65 72 2c 0a 20 20  fer *pBuffer,.  
33c03 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33c04 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73              cons
33c05 74 20 63 68 61 72 20 2a 70 53 6f 75 72 63 65 31  t char *pSource1
33c06 2c 20 69 6e 74 20 6e 53 6f 75 72 63 65 31 2c 0a  , int nSource1,.
33c07 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33c08 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f                co
33c09 6e 73 74 20 63 68 61 72 20 2a 70 53 6f 75 72 63  nst char *pSourc
33c0a 65 32 2c 20 69 6e 74 20 6e 53 6f 75 72 63 65 32  e2, int nSource2
33c0b 29 7b 0a 20 20 61 73 73 65 72 74 28 20 6e 53 6f  ){.  assert( nSo
33c0c 75 72 63 65 31 3e 30 20 26 26 20 70 53 6f 75 72  urce1>0 && pSour
33c0d 63 65 31 21 3d 4e 55 4c 4c 20 29 3b 0a 20 20 61  ce1!=NULL );.  a
33c0e 73 73 65 72 74 28 20 6e 53 6f 75 72 63 65 32 3e  ssert( nSource2>
33c0f 30 20 26 26 20 70 53 6f 75 72 63 65 32 21 3d 4e  0 && pSource2!=N
33c10 55 4c 4c 20 29 3b 0a 20 20 64 61 74 61 42 75 66  ULL );.  dataBuf
33c11 66 65 72 45 78 70 61 6e 64 28 70 42 75 66 66 65  ferExpand(pBuffe
33c12 72 2c 20 6e 53 6f 75 72 63 65 31 2b 6e 53 6f 75  r, nSource1+nSou
33c13 72 63 65 32 29 3b 0a 20 20 6d 65 6d 63 70 79 28  rce2);.  memcpy(
33c14 70 42 75 66 66 65 72 2d 3e 70 44 61 74 61 2b 70  pBuffer->pData+p
33c15 42 75 66 66 65 72 2d 3e 6e 44 61 74 61 2c 20 70  Buffer->nData, p
33c16 53 6f 75 72 63 65 31 2c 20 6e 53 6f 75 72 63 65  Source1, nSource
33c17 31 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 42 75  1);.  memcpy(pBu
33c18 66 66 65 72 2d 3e 70 44 61 74 61 2b 70 42 75 66  ffer->pData+pBuf
33c19 66 65 72 2d 3e 6e 44 61 74 61 2b 6e 53 6f 75 72  fer->nData+nSour
33c1a 63 65 31 2c 20 70 53 6f 75 72 63 65 32 2c 20 6e  ce1, pSource2, n
33c1b 53 6f 75 72 63 65 32 29 3b 0a 20 20 70 42 75 66  Source2);.  pBuf
33c1c 66 65 72 2d 3e 6e 44 61 74 61 20 2b 3d 20 6e 53  fer->nData += nS
33c1d 6f 75 72 63 65 31 2b 6e 53 6f 75 72 63 65 32 3b  ource1+nSource2;
33c1e 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64  .}.static void d
33c1f 61 74 61 42 75 66 66 65 72 52 65 70 6c 61 63 65  ataBufferReplace
33c20 28 44 61 74 61 42 75 66 66 65 72 20 2a 70 42 75  (DataBuffer *pBu
33c21 66 66 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20  ffer,.          
33c22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33c23 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
33c24 70 53 6f 75 72 63 65 2c 20 69 6e 74 20 6e 53 6f  pSource, int nSo
33c25 75 72 63 65 29 7b 0a 20 20 64 61 74 61 42 75 66  urce){.  dataBuf
33c26 66 65 72 52 65 73 65 74 28 70 42 75 66 66 65 72  ferReset(pBuffer
33c27 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 41  );.  dataBufferA
33c28 70 70 65 6e 64 28 70 42 75 66 66 65 72 2c 20 70  ppend(pBuffer, p
33c29 53 6f 75 72 63 65 2c 20 6e 53 6f 75 72 63 65 29  Source, nSource)
33c2a 3b 0a 7d 0a 0a 2f 2a 20 53 74 72 69 6e 67 42 75  ;.}../* StringBu
33c2b 66 66 65 72 20 69 73 20 61 20 6e 75 6c 6c 2d 74  ffer is a null-t
33c2c 65 72 6d 69 6e 61 74 65 64 20 76 65 72 73 69 6f  erminated versio
33c2d 6e 20 6f 66 20 44 61 74 61 42 75 66 66 65 72 2e  n of DataBuffer.
33c2e 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75   */.typedef stru
33c2f 63 74 20 53 74 72 69 6e 67 42 75 66 66 65 72 20  ct StringBuffer 
33c30 7b 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 62  {.  DataBuffer b
33c31 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
33c32 49 6e 63 6c 75 64 65 73 20 6e 75 6c 6c 20 74 65  Includes null te
33c33 72 6d 69 6e 61 74 6f 72 2e 20 2a 2f 0a 7d 20 53  rminator. */.} S
33c34 74 72 69 6e 67 42 75 66 66 65 72 3b 0a 0a 73 74  tringBuffer;..st
33c35 61 74 69 63 20 76 6f 69 64 20 69 6e 69 74 53 74  atic void initSt
33c36 72 69 6e 67 42 75 66 66 65 72 28 53 74 72 69 6e  ringBuffer(Strin
33c37 67 42 75 66 66 65 72 20 2a 73 62 29 7b 0a 20 20  gBuffer *sb){.  
33c38 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26  dataBufferInit(&
33c39 73 62 2d 3e 62 2c 20 31 30 30 29 3b 0a 20 20 64  sb->b, 100);.  d
33c3a 61 74 61 42 75 66 66 65 72 52 65 70 6c 61 63 65  ataBufferReplace
33c3b 28 26 73 62 2d 3e 62 2c 20 22 22 2c 20 31 29 3b  (&sb->b, "", 1);
33c3c 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 74  .}.static int st
33c3d 72 69 6e 67 42 75 66 66 65 72 4c 65 6e 67 74 68  ringBufferLength
33c3e 28 53 74 72 69 6e 67 42 75 66 66 65 72 20 2a 73  (StringBuffer *s
33c3f 62 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 62 2d  b){.  return sb-
33c40 3e 62 2e 6e 44 61 74 61 2d 31 3b 0a 7d 0a 73 74  >b.nData-1;.}.st
33c41 61 74 69 63 20 63 68 61 72 20 2a 73 74 72 69 6e  atic char *strin
33c42 67 42 75 66 66 65 72 44 61 74 61 28 53 74 72 69  gBufferData(Stri
33c43 6e 67 42 75 66 66 65 72 20 2a 73 62 29 7b 0a 20  ngBuffer *sb){. 
33c44 20 72 65 74 75 72 6e 20 73 62 2d 3e 62 2e 70 44   return sb->b.pD
33c45 61 74 61 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  ata;.}.static vo
33c46 69 64 20 73 74 72 69 6e 67 42 75 66 66 65 72 44  id stringBufferD
33c47 65 73 74 72 6f 79 28 53 74 72 69 6e 67 42 75 66  estroy(StringBuf
33c48 66 65 72 20 2a 73 62 29 7b 0a 20 20 64 61 74 61  fer *sb){.  data
33c49 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 73  BufferDestroy(&s
33c4a 62 2d 3e 62 29 3b 0a 7d 0a 0a 73 74 61 74 69 63  b->b);.}..static
33c4b 20 76 6f 69 64 20 6e 61 70 70 65 6e 64 28 53 74   void nappend(St
33c4c 72 69 6e 67 42 75 66 66 65 72 20 2a 73 62 2c 20  ringBuffer *sb, 
33c4d 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 72 6f  const char *zFro
33c4e 6d 2c 20 69 6e 74 20 6e 46 72 6f 6d 29 7b 0a 20  m, int nFrom){. 
33c4f 20 61 73 73 65 72 74 28 20 73 62 2d 3e 62 2e 6e   assert( sb->b.n
33c50 44 61 74 61 3e 30 20 29 3b 0a 20 20 69 66 28 20  Data>0 );.  if( 
33c51 6e 46 72 6f 6d 3e 30 20 29 7b 0a 20 20 20 20 73  nFrom>0 ){.    s
33c52 62 2d 3e 62 2e 6e 44 61 74 61 2d 2d 3b 0a 20 20  b->b.nData--;.  
33c53 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65    dataBufferAppe
33c54 6e 64 32 28 26 73 62 2d 3e 62 2c 20 7a 46 72 6f  nd2(&sb->b, zFro
33c55 6d 2c 20 6e 46 72 6f 6d 2c 20 22 22 2c 20 31 29  m, nFrom, "", 1)
33c56 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76  ;.  }.}.static v
33c57 6f 69 64 20 61 70 70 65 6e 64 28 53 74 72 69 6e  oid append(Strin
33c58 67 42 75 66 66 65 72 20 2a 73 62 2c 20 63 6f 6e  gBuffer *sb, con
33c59 73 74 20 63 68 61 72 20 2a 7a 46 72 6f 6d 29 7b  st char *zFrom){
33c5a 0a 20 20 6e 61 70 70 65 6e 64 28 73 62 2c 20 7a  .  nappend(sb, z
33c5b 46 72 6f 6d 2c 20 73 74 72 6c 65 6e 28 7a 46 72  From, strlen(zFr
33c5c 6f 6d 29 29 3b 0a 7d 0a 0a 2f 2a 20 41 70 70 65  om));.}../* Appe
33c5d 6e 64 20 61 20 6c 69 73 74 20 6f 66 20 73 74 72  nd a list of str
33c5e 69 6e 67 73 20 73 65 70 61 72 61 74 65 64 20 62  ings separated b
33c5f 79 20 63 6f 6d 6d 61 73 2e 20 2a 2f 0a 73 74 61  y commas. */.sta
33c60 74 69 63 20 76 6f 69 64 20 61 70 70 65 6e 64 4c  tic void appendL
33c61 69 73 74 28 53 74 72 69 6e 67 42 75 66 66 65 72  ist(StringBuffer
33c62 20 2a 73 62 2c 20 69 6e 74 20 6e 53 74 72 69 6e   *sb, int nStrin
33c63 67 2c 20 63 68 61 72 20 2a 2a 61 7a 53 74 72 69  g, char **azStri
33c64 6e 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  ng){.  int i;.  
33c65 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 53 74 72 69  for(i=0; i<nStri
33c66 6e 67 3b 20 2b 2b 69 29 7b 0a 20 20 20 20 69 66  ng; ++i){.    if
33c67 28 20 69 3e 30 20 29 20 61 70 70 65 6e 64 28 73  ( i>0 ) append(s
33c68 62 2c 20 22 2c 20 22 29 3b 0a 20 20 20 20 61 70  b, ", ");.    ap
33c69 70 65 6e 64 28 73 62 2c 20 61 7a 53 74 72 69 6e  pend(sb, azStrin
33c6a 67 5b 69 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 73 74  g[i]);.  }.}..st
33c6b 61 74 69 63 20 69 6e 74 20 65 6e 64 73 49 6e 57  atic int endsInW
33c6c 68 69 74 65 53 70 61 63 65 28 53 74 72 69 6e 67  hiteSpace(String
33c6d 42 75 66 66 65 72 20 2a 70 29 7b 0a 20 20 72 65  Buffer *p){.  re
33c6e 74 75 72 6e 20 73 74 72 69 6e 67 42 75 66 66 65  turn stringBuffe
33c6f 72 4c 65 6e 67 74 68 28 70 29 3e 30 20 26 26 0a  rLength(p)>0 &&.
33c70 20 20 20 20 73 61 66 65 5f 69 73 73 70 61 63 65      safe_isspace
33c71 28 73 74 72 69 6e 67 42 75 66 66 65 72 44 61 74  (stringBufferDat
33c72 61 28 70 29 5b 73 74 72 69 6e 67 42 75 66 66 65  a(p)[stringBuffe
33c73 72 4c 65 6e 67 74 68 28 70 29 2d 31 5d 29 3b 0a  rLength(p)-1]);.
33c74 7d 0a 0a 2f 2a 20 49 66 20 74 68 65 20 53 74 72  }../* If the Str
33c75 69 6e 67 42 75 66 66 65 72 20 65 6e 64 73 20 69  ingBuffer ends i
33c76 6e 20 73 6f 6d 65 74 68 69 6e 67 20 6f 74 68 65  n something othe
33c77 72 20 74 68 61 6e 20 77 68 69 74 65 20 73 70 61  r than white spa
33c78 63 65 2c 20 61 64 64 20 61 0a 2a 2a 20 73 69 6e  ce, add a.** sin
33c79 67 6c 65 20 73 70 61 63 65 20 63 68 61 72 61 63  gle space charac
33c7a 74 65 72 20 74 6f 20 74 68 65 20 65 6e 64 2e 0a  ter to the end..
33c7b 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61  */.static void a
33c7c 70 70 65 6e 64 57 68 69 74 65 53 70 61 63 65 28  ppendWhiteSpace(
33c7d 53 74 72 69 6e 67 42 75 66 66 65 72 20 2a 70 29  StringBuffer *p)
33c7e 7b 0a 20 20 69 66 28 20 73 74 72 69 6e 67 42 75  {.  if( stringBu
33c7f 66 66 65 72 4c 65 6e 67 74 68 28 70 29 3d 3d 30  fferLength(p)==0
33c80 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28   ) return;.  if(
33c81 20 21 65 6e 64 73 49 6e 57 68 69 74 65 53 70 61   !endsInWhiteSpa
33c82 63 65 28 70 29 20 29 20 61 70 70 65 6e 64 28 70  ce(p) ) append(p
33c83 2c 20 22 20 22 29 3b 0a 7d 0a 0a 2f 2a 20 52 65  , " ");.}../* Re
33c84 6d 6f 76 65 20 77 68 69 74 65 20 73 70 61 63 65  move white space
33c85 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 20 6f 66   from the end of
33c86 20 74 68 65 20 53 74 72 69 6e 67 42 75 66 66 65   the StringBuffe
33c87 72 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  r */.static void
33c88 20 74 72 69 6d 57 68 69 74 65 53 70 61 63 65 28   trimWhiteSpace(
33c89 53 74 72 69 6e 67 42 75 66 66 65 72 20 2a 70 29  StringBuffer *p)
33c8a 7b 0a 20 20 77 68 69 6c 65 28 20 65 6e 64 73 49  {.  while( endsI
33c8b 6e 57 68 69 74 65 53 70 61 63 65 28 70 29 20 29  nWhiteSpace(p) )
33c8c 7b 0a 20 20 20 20 70 2d 3e 62 2e 70 44 61 74 61  {.    p->b.pData
33c8d 5b 2d 2d 70 2d 3e 62 2e 6e 44 61 74 61 2d 31 5d  [--p->b.nData-1]
33c8e 20 3d 20 27 5c 30 27 3b 0a 20 20 7d 0a 7d 0a 0a   = '\0';.  }.}..
33c8f 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
33c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33c91 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33c92 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33c93 2a 2a 2a 2a 2f 0a 2f 2a 20 44 4c 52 65 61 64 65  ****/./* DLReade
33c94 72 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 61  r is used to rea
33c95 64 20 64 6f 63 75 6d 65 6e 74 20 65 6c 65 6d 65  d document eleme
33c96 6e 74 73 20 66 72 6f 6d 20 61 20 64 6f 63 6c 69  nts from a docli
33c97 73 74 2e 20 20 54 68 65 0a 2a 2a 20 63 75 72 72  st.  The.** curr
33c98 65 6e 74 20 64 6f 63 69 64 20 69 73 20 63 61 63  ent docid is cac
33c99 68 65 64 2c 20 73 6f 20 64 6c 72 44 6f 63 69 64  hed, so dlrDocid
33c9a 28 29 20 69 73 20 66 61 73 74 2e 20 20 44 4c 52  () is fast.  DLR
33c9b 65 61 64 65 72 20 64 6f 65 73 20 6e 6f 74 0a 2a  eader does not.*
33c9c 2a 20 6f 77 6e 20 74 68 65 20 64 6f 63 6c 69 73  * own the doclis
33c9d 74 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20  t buffer..**.** 
33c9e 64 6c 72 41 74 45 6e 64 20 2d 20 74 72 75 65 20  dlrAtEnd - true 
33c9f 69 66 20 74 68 65 72 65 27 73 20 6e 6f 20 6d 6f  if there's no mo
33ca0 72 65 20 64 61 74 61 20 74 6f 20 72 65 61 64 2e  re data to read.
33ca1 0a 2a 2a 20 64 6c 72 44 6f 63 69 64 20 2d 20 64  .** dlrDocid - d
33ca2 6f 63 69 64 20 6f 66 20 63 75 72 72 65 6e 74 20  ocid of current 
33ca3 64 6f 63 75 6d 65 6e 74 2e 0a 2a 2a 20 64 6c 72  document..** dlr
33ca4 44 6f 63 44 61 74 61 20 2d 20 64 6f 63 6c 69 73  DocData - doclis
33ca5 74 20 64 61 74 61 20 66 6f 72 20 63 75 72 72 65  t data for curre
33ca6 6e 74 20 64 6f 63 75 6d 65 6e 74 20 28 69 6e 63  nt document (inc
33ca7 6c 75 64 69 6e 67 20 64 6f 63 69 64 29 2e 0a 2a  luding docid)..*
33ca8 2a 20 64 6c 72 44 6f 63 44 61 74 61 42 79 74 65  * dlrDocDataByte
33ca9 73 20 2d 20 6c 65 6e 67 74 68 20 6f 66 20 73 61  s - length of sa
33caa 6d 65 2e 0a 2a 2a 20 64 6c 72 41 6c 6c 44 61 74  me..** dlrAllDat
33cab 61 42 79 74 65 73 20 2d 20 6c 65 6e 67 74 68 20  aBytes - length 
33cac 6f 66 20 61 6c 6c 20 72 65 6d 61 69 6e 69 6e 67  of all remaining
33cad 20 64 61 74 61 2e 0a 2a 2a 20 64 6c 72 50 6f 73   data..** dlrPos
33cae 44 61 74 61 20 2d 20 70 6f 73 69 74 69 6f 6e 20  Data - position 
33caf 64 61 74 61 20 66 6f 72 20 63 75 72 72 65 6e 74  data for current
33cb0 20 64 6f 63 75 6d 65 6e 74 2e 0a 2a 2a 20 64 6c   document..** dl
33cb1 72 50 6f 73 44 61 74 61 4c 65 6e 20 2d 20 6c 65  rPosDataLen - le
33cb2 6e 67 74 68 20 6f 66 20 70 6f 73 20 64 61 74 61  ngth of pos data
33cb3 20 66 6f 72 20 63 75 72 72 65 6e 74 20 64 6f 63   for current doc
33cb4 75 6d 65 6e 74 20 28 69 6e 63 6c 20 50 4f 53 5f  ument (incl POS_
33cb5 45 4e 44 29 2e 0a 2a 2a 20 64 6c 72 53 74 65 70  END)..** dlrStep
33cb6 20 2d 20 73 74 65 70 20 74 6f 20 63 75 72 72 65   - step to curre
33cb7 6e 74 20 64 6f 63 75 6d 65 6e 74 2e 0a 2a 2a 20  nt document..** 
33cb8 64 6c 72 49 6e 69 74 20 2d 20 69 6e 69 74 69 61  dlrInit - initia
33cb9 6c 20 66 6f 72 20 64 6f 63 6c 69 73 74 20 6f 66  l for doclist of
33cba 20 67 69 76 65 6e 20 74 79 70 65 20 61 67 61 69   given type agai
33cbb 6e 73 74 20 67 69 76 65 6e 20 64 61 74 61 2e 0a  nst given data..
33cbc 2a 2a 20 64 6c 72 44 65 73 74 72 6f 79 20 2d 20  ** dlrDestroy - 
33cbd 63 6c 65 61 6e 20 75 70 2e 0a 2a 2a 0a 2a 2a 20  clean up..**.** 
33cbe 45 78 70 65 63 74 65 64 20 75 73 61 67 65 20 69  Expected usage i
33cbf 73 20 73 6f 6d 65 74 68 69 6e 67 20 6c 69 6b 65  s something like
33cc0 3a 0a 2a 2a 0a 2a 2a 20 20 20 44 4c 52 65 61 64  :.**.**   DLRead
33cc1 65 72 20 72 65 61 64 65 72 3b 0a 2a 2a 20 20 20  er reader;.**   
33cc2 64 6c 72 49 6e 69 74 28 26 72 65 61 64 65 72 2c  dlrInit(&reader,
33cc3 20 70 44 61 74 61 2c 20 6e 44 61 74 61 29 3b 0a   pData, nData);.
33cc4 2a 2a 20 20 20 77 68 69 6c 65 28 20 21 64 6c 72  **   while( !dlr
33cc5 41 74 45 6e 64 28 26 72 65 61 64 65 72 29 20 29  AtEnd(&reader) )
33cc6 7b 0a 2a 2a 20 20 20 20 20 2f 2f 20 63 61 6c 6c  {.**     // call
33cc7 73 20 74 6f 20 64 6c 72 44 6f 63 69 64 28 29 20  s to dlrDocid() 
33cc8 61 6e 64 20 6b 69 6e 2e 0a 2a 2a 20 20 20 20 20  and kin..**     
33cc9 64 6c 72 53 74 65 70 28 26 72 65 61 64 65 72 29  dlrStep(&reader)
33cca 3b 0a 2a 2a 20 20 20 7d 0a 2a 2a 20 20 20 64 6c  ;.**   }.**   dl
33ccb 72 44 65 73 74 72 6f 79 28 26 72 65 61 64 65 72  rDestroy(&reader
33ccc 29 3b 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  );.*/.typedef st
33ccd 72 75 63 74 20 44 4c 52 65 61 64 65 72 20 7b 0a  ruct DLReader {.
33cce 20 20 44 6f 63 4c 69 73 74 54 79 70 65 20 69 54    DocListType iT
33ccf 79 70 65 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  ype;.  const cha
33cd0 72 20 2a 70 44 61 74 61 3b 0a 20 20 69 6e 74 20  r *pData;.  int 
33cd1 6e 44 61 74 61 3b 0a 0a 20 20 73 71 6c 69 74 65  nData;..  sqlite
33cd2 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 3b 0a 20  _int64 iDocid;. 
33cd3 20 69 6e 74 20 6e 45 6c 65 6d 65 6e 74 3b 0a 7d   int nElement;.}
33cd4 20 44 4c 52 65 61 64 65 72 3b 0a 0a 73 74 61 74   DLReader;..stat
33cd5 69 63 20 69 6e 74 20 64 6c 72 41 74 45 6e 64 28  ic int dlrAtEnd(
33cd6 44 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  DLReader *pReade
33cd7 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 52  r){.  assert( pR
33cd8 65 61 64 65 72 2d 3e 6e 44 61 74 61 3e 3d 30 20  eader->nData>=0 
33cd9 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61  );.  return pRea
33cda 64 65 72 2d 3e 6e 44 61 74 61 3d 3d 30 3b 0a 7d  der->nData==0;.}
33cdb 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f 69  .static sqlite_i
33cdc 6e 74 36 34 20 64 6c 72 44 6f 63 69 64 28 44 4c  nt64 dlrDocid(DL
33cdd 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
33cde 7b 0a 20 20 61 73 73 65 72 74 28 20 21 64 6c 72  {.  assert( !dlr
33cdf 41 74 45 6e 64 28 70 52 65 61 64 65 72 29 20 29  AtEnd(pReader) )
33ce0 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61 64  ;.  return pRead
33ce1 65 72 2d 3e 69 44 6f 63 69 64 3b 0a 7d 0a 73 74  er->iDocid;.}.st
33ce2 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
33ce3 2a 64 6c 72 44 6f 63 44 61 74 61 28 44 4c 52 65  *dlrDocData(DLRe
33ce4 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
33ce5 20 20 61 73 73 65 72 74 28 20 21 64 6c 72 41 74    assert( !dlrAt
33ce6 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a  End(pReader) );.
33ce7 20 20 72 65 74 75 72 6e 20 70 52 65 61 64 65 72    return pReader
33ce8 2d 3e 70 44 61 74 61 3b 0a 7d 0a 73 74 61 74 69  ->pData;.}.stati
33ce9 63 20 69 6e 74 20 64 6c 72 44 6f 63 44 61 74 61  c int dlrDocData
33cea 42 79 74 65 73 28 44 4c 52 65 61 64 65 72 20 2a  Bytes(DLReader *
33ceb 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65  pReader){.  asse
33cec 72 74 28 20 21 64 6c 72 41 74 45 6e 64 28 70 52  rt( !dlrAtEnd(pR
33ced 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75  eader) );.  retu
33cee 72 6e 20 70 52 65 61 64 65 72 2d 3e 6e 45 6c 65  rn pReader->nEle
33cef 6d 65 6e 74 3b 0a 7d 0a 73 74 61 74 69 63 20 69  ment;.}.static i
33cf0 6e 74 20 64 6c 72 41 6c 6c 44 61 74 61 42 79 74  nt dlrAllDataByt
33cf1 65 73 28 44 4c 52 65 61 64 65 72 20 2a 70 52 65  es(DLReader *pRe
33cf2 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28  ader){.  assert(
33cf3 20 21 64 6c 72 41 74 45 6e 64 28 70 52 65 61 64   !dlrAtEnd(pRead
33cf4 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  er) );.  return 
33cf5 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 3b 0a  pReader->nData;.
33cf6 7d 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  }./* TODO(shess)
33cf7 20 43 6f 6e 73 69 64 65 72 20 61 64 64 69 6e 67   Consider adding
33cf8 20 61 20 66 69 65 6c 64 20 74 6f 20 74 72 61 63   a field to trac
33cf9 6b 20 69 44 6f 63 69 64 20 76 61 72 69 6e 74 20  k iDocid varint 
33cfa 6c 65 6e 67 74 68 0a 2a 2a 20 74 6f 20 6d 61 6b  length.** to mak
33cfb 65 20 74 68 65 73 65 20 74 77 6f 20 66 75 6e 63  e these two func
33cfc 74 69 6f 6e 73 20 66 61 73 74 65 72 2e 20 20 54  tions faster.  T
33cfd 68 69 73 20 6d 69 67 68 74 20 6d 61 74 74 65 72  his might matter
33cfe 20 28 61 20 74 69 6e 79 20 62 69 74 29 0a 2a 2a   (a tiny bit).**
33cff 20 66 6f 72 20 71 75 65 72 69 65 73 2e 0a 2a 2f   for queries..*/
33d00 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68  .static const ch
33d01 61 72 20 2a 64 6c 72 50 6f 73 44 61 74 61 28 44  ar *dlrPosData(D
33d02 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  LReader *pReader
33d03 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  ){.  sqlite_int6
33d04 34 20 69 44 75 6d 6d 79 3b 0a 20 20 69 6e 74 20  4 iDummy;.  int 
33d05 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e  n = fts3GetVarin
33d06 74 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61  t(pReader->pData
33d07 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 61 73  , &iDummy);.  as
33d08 73 65 72 74 28 20 21 64 6c 72 41 74 45 6e 64 28  sert( !dlrAtEnd(
33d09 70 52 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65  pReader) );.  re
33d0a 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 70 44  turn pReader->pD
33d0b 61 74 61 2b 6e 3b 0a 7d 0a 73 74 61 74 69 63 20  ata+n;.}.static 
33d0c 69 6e 74 20 64 6c 72 50 6f 73 44 61 74 61 4c 65  int dlrPosDataLe
33d0d 6e 28 44 4c 52 65 61 64 65 72 20 2a 70 52 65 61  n(DLReader *pRea
33d0e 64 65 72 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69  der){.  sqlite_i
33d0f 6e 74 36 34 20 69 44 75 6d 6d 79 3b 0a 20 20 69  nt64 iDummy;.  i
33d10 6e 74 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61  nt n = fts3GetVa
33d11 72 69 6e 74 28 70 52 65 61 64 65 72 2d 3e 70 44  rint(pReader->pD
33d12 61 74 61 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20  ata, &iDummy);. 
33d13 20 61 73 73 65 72 74 28 20 21 64 6c 72 41 74 45   assert( !dlrAtE
33d14 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 20  nd(pReader) );. 
33d15 20 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d   return pReader-
33d16 3e 6e 45 6c 65 6d 65 6e 74 2d 6e 3b 0a 7d 0a 73  >nElement-n;.}.s
33d17 74 61 74 69 63 20 76 6f 69 64 20 64 6c 72 53 74  tatic void dlrSt
33d18 65 70 28 44 4c 52 65 61 64 65 72 20 2a 70 52 65  ep(DLReader *pRe
33d19 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28  ader){.  assert(
33d1a 20 21 64 6c 72 41 74 45 6e 64 28 70 52 65 61 64   !dlrAtEnd(pRead
33d1b 65 72 29 20 29 3b 0a 0a 20 20 2f 2a 20 53 6b 69  er) );..  /* Ski
33d1c 70 20 70 61 73 74 20 63 75 72 72 65 6e 74 20 64  p past current d
33d1d 6f 63 6c 69 73 74 20 65 6c 65 6d 65 6e 74 2e 20  oclist element. 
33d1e 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 52 65  */.  assert( pRe
33d1f 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74 3c 3d  ader->nElement<=
33d20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 29  pReader->nData )
33d21 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61  ;.  pReader->pDa
33d22 74 61 20 2b 3d 20 70 52 65 61 64 65 72 2d 3e 6e  ta += pReader->n
33d23 45 6c 65 6d 65 6e 74 3b 0a 20 20 70 52 65 61 64  Element;.  pRead
33d24 65 72 2d 3e 6e 44 61 74 61 20 2d 3d 20 70 52 65  er->nData -= pRe
33d25 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74 3b 0a  ader->nElement;.
33d26 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69  .  /* If there i
33d27 73 20 6d 6f 72 65 20 64 61 74 61 2c 20 72 65 61  s more data, rea
33d28 64 20 74 68 65 20 6e 65 78 74 20 64 6f 63 6c 69  d the next docli
33d29 73 74 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f 0a 20  st element. */. 
33d2a 20 69 66 28 20 70 52 65 61 64 65 72 2d 3e 6e 44   if( pReader->nD
33d2b 61 74 61 21 3d 30 20 29 7b 0a 20 20 20 20 73 71  ata!=0 ){.    sq
33d2c 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63 69  lite_int64 iDoci
33d2d 64 44 65 6c 74 61 3b 0a 20 20 20 20 69 6e 74 20  dDelta;.    int 
33d2e 69 44 75 6d 6d 79 2c 20 6e 20 3d 20 66 74 73 33  iDummy, n = fts3
33d2f 47 65 74 56 61 72 69 6e 74 28 70 52 65 61 64 65  GetVarint(pReade
33d30 72 2d 3e 70 44 61 74 61 2c 20 26 69 44 6f 63 69  r->pData, &iDoci
33d31 64 44 65 6c 74 61 29 3b 0a 20 20 20 20 70 52 65  dDelta);.    pRe
33d32 61 64 65 72 2d 3e 69 44 6f 63 69 64 20 2b 3d 20  ader->iDocid += 
33d33 69 44 6f 63 69 64 44 65 6c 74 61 3b 0a 20 20 20  iDocidDelta;.   
33d34 20 69 66 28 20 70 52 65 61 64 65 72 2d 3e 69 54   if( pReader->iT
33d35 79 70 65 3e 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e  ype>=DL_POSITION
33d36 53 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  S ){.      asser
33d37 74 28 20 6e 3c 70 52 65 61 64 65 72 2d 3e 6e 44  t( n<pReader->nD
33d38 61 74 61 20 29 3b 0a 20 20 20 20 20 20 77 68 69  ata );.      whi
33d39 6c 65 28 20 31 20 29 7b 0a 20 20 20 20 20 20 20  le( 1 ){.       
33d3a 20 6e 20 2b 3d 20 66 74 73 33 47 65 74 56 61 72   n += fts3GetVar
33d3b 69 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70  int32(pReader->p
33d3c 44 61 74 61 2b 6e 2c 20 26 69 44 75 6d 6d 79 29  Data+n, &iDummy)
33d3d 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
33d3e 28 20 6e 3c 3d 70 52 65 61 64 65 72 2d 3e 6e 44  ( n<=pReader->nD
33d3f 61 74 61 20 29 3b 0a 20 20 20 20 20 20 20 20 69  ata );.        i
33d40 66 28 20 69 44 75 6d 6d 79 3d 3d 50 4f 53 5f 45  f( iDummy==POS_E
33d41 4e 44 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  ND ) break;.    
33d42 20 20 20 20 69 66 28 20 69 44 75 6d 6d 79 3d 3d      if( iDummy==
33d43 50 4f 53 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20  POS_COLUMN ){.  
33d44 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 66 74 73          n += fts
33d45 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 52 65  3GetVarint32(pRe
33d46 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26  ader->pData+n, &
33d47 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20  iDummy);.       
33d48 20 20 20 61 73 73 65 72 74 28 20 6e 3c 70 52 65     assert( n<pRe
33d49 61 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20  ader->nData );. 
33d4a 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28         }else if(
33d4b 20 70 52 65 61 64 65 72 2d 3e 69 54 79 70 65 3d   pReader->iType=
33d4c 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f 46  =DL_POSITIONS_OF
33d4d 46 53 45 54 53 20 29 7b 0a 20 20 20 20 20 20 20  FSETS ){.       
33d4e 20 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74 56     n += fts3GetV
33d4f 61 72 69 6e 74 33 32 28 70 52 65 61 64 65 72 2d  arint32(pReader-
33d50 3e 70 44 61 74 61 2b 6e 2c 20 26 69 44 75 6d 6d  >pData+n, &iDumm
33d51 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 20  y);.          n 
33d52 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74  += fts3GetVarint
33d53 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74  32(pReader->pDat
33d54 61 2b 6e 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20  a+n, &iDummy);. 
33d55 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
33d56 20 6e 3c 70 52 65 61 64 65 72 2d 3e 6e 44 61 74   n<pReader->nDat
33d57 61 20 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  a );.        }. 
33d58 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
33d59 20 70 52 65 61 64 65 72 2d 3e 6e 45 6c 65 6d 65   pReader->nEleme
33d5a 6e 74 20 3d 20 6e 3b 0a 20 20 20 20 61 73 73 65  nt = n;.    asse
33d5b 72 74 28 20 70 52 65 61 64 65 72 2d 3e 6e 45 6c  rt( pReader->nEl
33d5c 65 6d 65 6e 74 3c 3d 70 52 65 61 64 65 72 2d 3e  ement<=pReader->
33d5d 6e 44 61 74 61 20 29 3b 0a 20 20 7d 0a 7d 0a 73  nData );.  }.}.s
33d5e 74 61 74 69 63 20 76 6f 69 64 20 64 6c 72 49 6e  tatic void dlrIn
33d5f 69 74 28 44 4c 52 65 61 64 65 72 20 2a 70 52 65  it(DLReader *pRe
33d60 61 64 65 72 2c 20 44 6f 63 4c 69 73 74 54 79 70  ader, DocListTyp
33d61 65 20 69 54 79 70 65 2c 0a 20 20 20 20 20 20 20  e iType,.       
33d62 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
33d63 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20  st char *pData, 
33d64 69 6e 74 20 6e 44 61 74 61 29 7b 0a 20 20 61 73  int nData){.  as
33d65 73 65 72 74 28 20 70 44 61 74 61 21 3d 4e 55 4c  sert( pData!=NUL
33d66 4c 20 26 26 20 6e 44 61 74 61 21 3d 30 20 29 3b  L && nData!=0 );
33d67 0a 20 20 70 52 65 61 64 65 72 2d 3e 69 54 79 70  .  pReader->iTyp
33d68 65 20 3d 20 69 54 79 70 65 3b 0a 20 20 70 52 65  e = iType;.  pRe
33d69 61 64 65 72 2d 3e 70 44 61 74 61 20 3d 20 70 44  ader->pData = pD
33d6a 61 74 61 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e  ata;.  pReader->
33d6b 6e 44 61 74 61 20 3d 20 6e 44 61 74 61 3b 0a 20  nData = nData;. 
33d6c 20 70 52 65 61 64 65 72 2d 3e 6e 45 6c 65 6d 65   pReader->nEleme
33d6d 6e 74 20 3d 20 30 3b 0a 20 20 70 52 65 61 64 65  nt = 0;.  pReade
33d6e 72 2d 3e 69 44 6f 63 69 64 20 3d 20 30 3b 0a 0a  r->iDocid = 0;..
33d6f 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 66 69    /* Load the fi
33d70 72 73 74 20 65 6c 65 6d 65 6e 74 27 73 20 64 61  rst element's da
33d71 74 61 2e 20 20 54 68 65 72 65 20 6d 75 73 74 20  ta.  There must 
33d72 62 65 20 61 20 66 69 72 73 74 20 65 6c 65 6d 65  be a first eleme
33d73 6e 74 2e 20 2a 2f 0a 20 20 64 6c 72 53 74 65 70  nt. */.  dlrStep
33d74 28 70 52 65 61 64 65 72 29 3b 0a 7d 0a 73 74 61  (pReader);.}.sta
33d75 74 69 63 20 76 6f 69 64 20 64 6c 72 44 65 73 74  tic void dlrDest
33d76 72 6f 79 28 44 4c 52 65 61 64 65 72 20 2a 70 52  roy(DLReader *pR
33d77 65 61 64 65 72 29 7b 0a 20 20 53 43 52 41 4d 42  eader){.  SCRAMB
33d78 4c 45 28 70 52 65 61 64 65 72 29 3b 0a 7d 0a 0a  LE(pReader);.}..
33d79 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f  #ifndef NDEBUG./
33d7a 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68  * Verify that th
33d7b 65 20 64 6f 63 6c 69 73 74 20 63 61 6e 20 62 65  e doclist can be
33d7c 20 76 61 6c 69 64 6c 79 20 64 65 63 6f 64 65 64   validly decoded
33d7d 2e 20 20 41 6c 73 6f 20 72 65 74 75 72 6e 73 20  .  Also returns 
33d7e 74 68 65 0a 2a 2a 20 6c 61 73 74 20 64 6f 63 69  the.** last doci
33d7f 64 20 66 6f 75 6e 64 20 62 65 63 61 75 73 65 20  d found because 
33d80 69 74 20 69 73 20 63 6f 6e 76 65 6e 69 65 6e 74  it is convenient
33d81 20 69 6e 20 6f 74 68 65 72 20 61 73 73 65 72 74   in other assert
33d82 69 6f 6e 73 20 66 6f 72 0a 2a 2a 20 44 4c 57 72  ions for.** DLWr
33d83 69 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  iter..*/.static 
33d84 76 6f 69 64 20 64 6f 63 4c 69 73 74 56 61 6c 69  void docListVali
33d85 64 61 74 65 28 44 6f 63 4c 69 73 74 54 79 70 65  date(DocListType
33d86 20 69 54 79 70 65 2c 20 63 6f 6e 73 74 20 63 68   iType, const ch
33d87 61 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e  ar *pData, int n
33d88 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20  Data,.          
33d89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33d8a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a    sqlite_int64 *
33d8b 70 4c 61 73 74 44 6f 63 69 64 29 7b 0a 20 20 73  pLastDocid){.  s
33d8c 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 50 72 65  qlite_int64 iPre
33d8d 76 44 6f 63 69 64 20 3d 20 30 3b 0a 20 20 61 73  vDocid = 0;.  as
33d8e 73 65 72 74 28 20 6e 44 61 74 61 3e 30 20 29 3b  sert( nData>0 );
33d8f 0a 20 20 61 73 73 65 72 74 28 20 70 44 61 74 61  .  assert( pData
33d90 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
33d91 20 70 44 61 74 61 2b 6e 44 61 74 61 3e 70 44 61   pData+nData>pDa
33d92 74 61 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 6e  ta );.  while( n
33d93 44 61 74 61 21 3d 30 20 29 7b 0a 20 20 20 20 73  Data!=0 ){.    s
33d94 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63  qlite_int64 iDoc
33d95 69 64 44 65 6c 74 61 3b 0a 20 20 20 20 69 6e 74  idDelta;.    int
33d96 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69   n = fts3GetVari
33d97 6e 74 28 70 44 61 74 61 2c 20 26 69 44 6f 63 69  nt(pData, &iDoci
33d98 64 44 65 6c 74 61 29 3b 0a 20 20 20 20 69 50 72  dDelta);.    iPr
33d99 65 76 44 6f 63 69 64 20 2b 3d 20 69 44 6f 63 69  evDocid += iDoci
33d9a 64 44 65 6c 74 61 3b 0a 20 20 20 20 69 66 28 20  dDelta;.    if( 
33d9b 69 54 79 70 65 3e 44 4c 5f 44 4f 43 49 44 53 20  iType>DL_DOCIDS 
33d9c 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 75  ){.      int iDu
33d9d 6d 6d 79 3b 0a 20 20 20 20 20 20 77 68 69 6c 65  mmy;.      while
33d9e 28 20 31 20 29 7b 0a 20 20 20 20 20 20 20 20 6e  ( 1 ){.        n
33d9f 20 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e   += fts3GetVarin
33da0 74 33 32 28 70 44 61 74 61 2b 6e 2c 20 26 69 44  t32(pData+n, &iD
33da1 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 69  ummy);.        i
33da2 66 28 20 69 44 75 6d 6d 79 3d 3d 50 4f 53 5f 45  f( iDummy==POS_E
33da3 4e 44 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  ND ) break;.    
33da4 20 20 20 20 69 66 28 20 69 44 75 6d 6d 79 3d 3d      if( iDummy==
33da5 50 4f 53 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20  POS_COLUMN ){.  
33da6 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 66 74 73          n += fts
33da7 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 44 61  3GetVarint32(pDa
33da8 74 61 2b 6e 2c 20 26 69 44 75 6d 6d 79 29 3b 0a  ta+n, &iDummy);.
33da9 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66          }else if
33daa 28 20 69 54 79 70 65 3e 44 4c 5f 50 4f 53 49 54  ( iType>DL_POSIT
33dab 49 4f 4e 53 20 29 7b 0a 20 20 20 20 20 20 20 20  IONS ){.        
33dac 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74 56 61    n += fts3GetVa
33dad 72 69 6e 74 33 32 28 70 44 61 74 61 2b 6e 2c 20  rint32(pData+n, 
33dae 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20  &iDummy);.      
33daf 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74      n += fts3Get
33db0 56 61 72 69 6e 74 33 32 28 70 44 61 74 61 2b 6e  Varint32(pData+n
33db1 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20  , &iDummy);.    
33db2 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 73      }.        as
33db3 73 65 72 74 28 20 6e 3c 3d 6e 44 61 74 61 20 29  sert( n<=nData )
33db4 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
33db5 20 20 20 20 61 73 73 65 72 74 28 20 6e 3c 3d 6e      assert( n<=n
33db6 44 61 74 61 20 29 3b 0a 20 20 20 20 70 44 61 74  Data );.    pDat
33db7 61 20 2b 3d 20 6e 3b 0a 20 20 20 20 6e 44 61 74  a += n;.    nDat
33db8 61 20 2d 3d 20 6e 3b 0a 20 20 7d 0a 20 20 69 66  a -= n;.  }.  if
33db9 28 20 70 4c 61 73 74 44 6f 63 69 64 20 29 20 2a  ( pLastDocid ) *
33dba 70 4c 61 73 74 44 6f 63 69 64 20 3d 20 69 50 72  pLastDocid = iPr
33dbb 65 76 44 6f 63 69 64 3b 0a 7d 0a 23 64 65 66 69  evDocid;.}.#defi
33dbc 6e 65 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f  ne ASSERT_VALID_
33dbd 44 4f 43 4c 49 53 54 28 69 2c 20 70 2c 20 6e 2c  DOCLIST(i, p, n,
33dbe 20 6f 29 20 64 6f 63 4c 69 73 74 56 61 6c 69 64   o) docListValid
33dbf 61 74 65 28 69 2c 20 70 2c 20 6e 2c 20 6f 29 0a  ate(i, p, n, o).
33dc0 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 41 53  #else.#define AS
33dc1 53 45 52 54 5f 56 41 4c 49 44 5f 44 4f 43 4c 49  SERT_VALID_DOCLI
33dc2 53 54 28 69 2c 20 70 2c 20 6e 2c 20 6f 29 20 61  ST(i, p, n, o) a
33dc3 73 73 65 72 74 28 20 31 20 29 0a 23 65 6e 64 69  ssert( 1 ).#endi
33dc4 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f../************
33dc5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33dc6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33dc7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33dc8 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 44 4c 57 72  *******/./* DLWr
33dc9 69 74 65 72 20 69 73 20 75 73 65 64 20 74 6f 20  iter is used to 
33dca 77 72 69 74 65 20 64 6f 63 6c 69 73 74 20 64 61  write doclist da
33dcb 74 61 20 74 6f 20 61 20 44 61 74 61 42 75 66 66  ta to a DataBuff
33dcc 65 72 2e 20 20 44 4c 57 72 69 74 65 72 0a 2a 2a  er.  DLWriter.**
33dcd 20 61 6c 77 61 79 73 20 61 70 70 65 6e 64 73 20   always appends 
33dce 74 6f 20 74 68 65 20 62 75 66 66 65 72 20 61 6e  to the buffer an
33dcf 64 20 64 6f 65 73 20 6e 6f 74 20 6f 77 6e 20 69  d does not own i
33dd0 74 2e 0a 2a 2a 0a 2a 2a 20 64 6c 77 49 6e 69 74  t..**.** dlwInit
33dd1 20 2d 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 6f   - initialize to
33dd2 20 77 72 69 74 65 20 61 20 67 69 76 65 6e 20 74   write a given t
33dd3 79 70 65 20 64 6f 63 6c 69 73 74 74 6f 20 61 20  ype doclistto a 
33dd4 62 75 66 66 65 72 2e 0a 2a 2a 20 64 6c 77 44 65  buffer..** dlwDe
33dd5 73 74 72 6f 79 20 2d 20 63 6c 65 61 72 20 74 68  stroy - clear th
33dd6 65 20 77 72 69 74 65 72 27 73 20 6d 65 6d 6f 72  e writer's memor
33dd7 79 2e 20 20 44 6f 65 73 20 6e 6f 74 20 66 72 65  y.  Does not fre
33dd8 65 20 62 75 66 66 65 72 2e 0a 2a 2a 20 64 6c 77  e buffer..** dlw
33dd9 41 70 70 65 6e 64 20 2d 20 61 70 70 65 6e 64 20  Append - append 
33dda 72 61 77 20 64 6f 63 6c 69 73 74 20 64 61 74 61  raw doclist data
33ddb 20 74 6f 20 62 75 66 66 65 72 2e 0a 2a 2a 20 64   to buffer..** d
33ddc 6c 77 43 6f 70 79 20 2d 20 63 6f 70 79 20 6e 65  lwCopy - copy ne
33ddd 78 74 20 64 6f 63 6c 69 73 74 20 66 72 6f 6d 20  xt doclist from 
33dde 72 65 61 64 65 72 20 74 6f 20 77 72 69 74 65 72  reader to writer
33ddf 2e 0a 2a 2a 20 64 6c 77 41 64 64 20 2d 20 63 6f  ..** dlwAdd - co
33de0 6e 73 74 72 75 63 74 20 64 6f 63 6c 69 73 74 20  nstruct doclist 
33de1 65 6c 65 6d 65 6e 74 20 61 6e 64 20 61 70 70 65  element and appe
33de2 6e 64 20 74 6f 20 62 75 66 66 65 72 2e 0a 2a 2a  nd to buffer..**
33de3 20 20 20 20 4f 6e 6c 79 20 61 70 70 6c 79 20 64      Only apply d
33de4 6c 77 41 64 64 28 29 20 74 6f 20 44 4c 5f 44 4f  lwAdd() to DL_DO
33de5 43 49 44 53 20 64 6f 63 6c 69 73 74 73 20 28 65  CIDS doclists (e
33de6 6c 73 65 20 75 73 65 20 50 4c 57 72 69 74 65 72  lse use PLWriter
33de7 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  )..*/.typedef st
33de8 72 75 63 74 20 44 4c 57 72 69 74 65 72 20 7b 0a  ruct DLWriter {.
33de9 20 20 44 6f 63 4c 69 73 74 54 79 70 65 20 69 54    DocListType iT
33dea 79 70 65 3b 0a 20 20 44 61 74 61 42 75 66 66 65  ype;.  DataBuffe
33deb 72 20 2a 62 3b 0a 20 20 73 71 6c 69 74 65 5f 69  r *b;.  sqlite_i
33dec 6e 74 36 34 20 69 50 72 65 76 44 6f 63 69 64 3b  nt64 iPrevDocid;
33ded 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
33dee 20 20 69 6e 74 20 68 61 73 5f 69 50 72 65 76 44    int has_iPrevD
33def 6f 63 69 64 3b 0a 23 65 6e 64 69 66 0a 7d 20 44  ocid;.#endif.} D
33df0 4c 57 72 69 74 65 72 3b 0a 0a 73 74 61 74 69 63  LWriter;..static
33df1 20 76 6f 69 64 20 64 6c 77 49 6e 69 74 28 44 4c   void dlwInit(DL
33df2 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 2c  Writer *pWriter,
33df3 20 44 6f 63 4c 69 73 74 54 79 70 65 20 69 54 79   DocListType iTy
33df4 70 65 2c 20 44 61 74 61 42 75 66 66 65 72 20 2a  pe, DataBuffer *
33df5 62 29 7b 0a 20 20 70 57 72 69 74 65 72 2d 3e 62  b){.  pWriter->b
33df6 20 3d 20 62 3b 0a 20 20 70 57 72 69 74 65 72 2d   = b;.  pWriter-
33df7 3e 69 54 79 70 65 20 3d 20 69 54 79 70 65 3b 0a  >iType = iType;.
33df8 20 20 70 57 72 69 74 65 72 2d 3e 69 50 72 65 76    pWriter->iPrev
33df9 44 6f 63 69 64 20 3d 20 30 3b 0a 23 69 66 6e 64  Docid = 0;.#ifnd
33dfa 65 66 20 4e 44 45 42 55 47 0a 20 20 70 57 72 69  ef NDEBUG.  pWri
33dfb 74 65 72 2d 3e 68 61 73 5f 69 50 72 65 76 44 6f  ter->has_iPrevDo
33dfc 63 69 64 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a  cid = 0;.#endif.
33dfd 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 6c  }.static void dl
33dfe 77 44 65 73 74 72 6f 79 28 44 4c 57 72 69 74 65  wDestroy(DLWrite
33dff 72 20 2a 70 57 72 69 74 65 72 29 7b 0a 20 20 53  r *pWriter){.  S
33e00 43 52 41 4d 42 4c 45 28 70 57 72 69 74 65 72 29  CRAMBLE(pWriter)
33e01 3b 0a 7d 0a 2f 2a 20 69 46 69 72 73 74 44 6f 63  ;.}./* iFirstDoc
33e02 69 64 20 69 73 20 74 68 65 20 66 69 72 73 74 20  id is the first 
33e03 64 6f 63 69 64 20 69 6e 20 74 68 65 20 64 6f 63  docid in the doc
33e04 6c 69 73 74 20 69 6e 20 70 44 61 74 61 2e 20 20  list in pData.  
33e05 49 74 20 69 73 0a 2a 2a 20 6e 65 65 64 65 64 20  It is.** needed 
33e06 62 65 63 61 75 73 65 20 70 44 61 74 61 20 6d 61  because pData ma
33e07 79 20 70 6f 69 6e 74 20 77 69 74 68 69 6e 20 61  y point within a
33e08 20 6c 61 72 67 65 72 20 64 6f 63 6c 69 73 74 2c   larger doclist,
33e09 20 69 6e 20 77 68 69 63 68 0a 2a 2a 20 63 61 73   in which.** cas
33e0a 65 20 74 68 65 20 66 69 72 73 74 20 69 74 65 6d  e the first item
33e0b 20 77 6f 75 6c 64 20 62 65 20 64 65 6c 74 61 2d   would be delta-
33e0c 65 6e 63 6f 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 69  encoded..**.** i
33e0d 4c 61 73 74 44 6f 63 69 64 20 69 73 20 74 68 65  LastDocid is the
33e0e 20 66 69 6e 61 6c 20 64 6f 63 69 64 20 69 6e 20   final docid in 
33e0f 74 68 65 20 64 6f 63 6c 69 73 74 20 69 6e 20 70  the doclist in p
33e10 44 61 74 61 2e 20 20 49 74 20 69 73 0a 2a 2a 20  Data.  It is.** 
33e11 6e 65 65 64 65 64 20 74 6f 20 63 72 65 61 74 65  needed to create
33e12 20 74 68 65 20 6e 65 77 20 69 50 72 65 76 44 6f   the new iPrevDo
33e13 63 69 64 20 66 6f 72 20 66 75 74 75 72 65 20 64  cid for future d
33e14 65 6c 74 61 2d 65 6e 63 6f 64 69 6e 67 2e 20 20  elta-encoding.  
33e15 54 68 65 0a 2a 2a 20 63 6f 64 65 20 63 6f 75 6c  The.** code coul
33e16 64 20 64 65 63 6f 64 65 20 74 68 65 20 70 61 73  d decode the pas
33e17 73 65 64 20 64 6f 63 6c 69 73 74 20 74 6f 20 72  sed doclist to r
33e18 65 63 72 65 61 74 65 20 69 4c 61 73 74 44 6f 63  ecreate iLastDoc
33e19 69 64 2c 20 62 75 74 0a 2a 2a 20 74 68 65 20 6f  id, but.** the o
33e1a 6e 6c 79 20 63 75 72 72 65 6e 74 20 75 73 65 72  nly current user
33e1b 20 28 64 6f 63 4c 69 73 74 4d 65 72 67 65 29 20   (docListMerge) 
33e1c 61 6c 72 65 61 64 79 20 68 61 73 20 64 65 63 6f  already has deco
33e1d 64 65 64 20 74 68 69 73 0a 2a 2a 20 69 6e 66 6f  ded this.** info
33e1e 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 54  rmation..*/./* T
33e1f 4f 44 4f 28 73 68 65 73 73 29 20 54 68 69 73 20  ODO(shess) This 
33e20 68 61 73 20 62 65 63 6f 6d 65 20 6a 75 73 74 20  has become just 
33e21 61 20 68 65 6c 70 65 72 20 66 6f 72 20 64 6f 63  a helper for doc
33e22 4c 69 73 74 4d 65 72 67 65 2e 0a 2a 2a 20 43 6f  ListMerge..** Co
33e23 6e 73 69 64 65 72 20 61 20 72 65 66 61 63 74 6f  nsider a refacto
33e24 72 20 74 6f 20 6d 61 6b 65 20 74 68 69 73 20 63  r to make this c
33e25 6c 65 61 6e 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  leaner..*/.stati
33e26 63 20 76 6f 69 64 20 64 6c 77 41 70 70 65 6e 64  c void dlwAppend
33e27 28 44 4c 57 72 69 74 65 72 20 2a 70 57 72 69 74  (DLWriter *pWrit
33e28 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  er,.            
33e29 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
33e2a 63 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e 74  char *pData, int
33e2b 20 6e 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20   nData,.        
33e2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
33e2d 6c 69 74 65 5f 69 6e 74 36 34 20 69 46 69 72 73  lite_int64 iFirs
33e2e 74 44 6f 63 69 64 2c 20 73 71 6c 69 74 65 5f 69  tDocid, sqlite_i
33e2f 6e 74 36 34 20 69 4c 61 73 74 44 6f 63 69 64 29  nt64 iLastDocid)
33e30 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  {.  sqlite_int64
33e31 20 69 44 6f 63 69 64 20 3d 20 30 3b 0a 20 20 63   iDocid = 0;.  c
33e32 68 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41 58  har c[VARINT_MAX
33e33 5d 3b 0a 20 20 69 6e 74 20 6e 46 69 72 73 74 4f  ];.  int nFirstO
33e34 6c 64 2c 20 6e 46 69 72 73 74 4e 65 77 3b 20 20  ld, nFirstNew;  
33e35 20 20 20 2f 2a 20 4f 6c 64 20 61 6e 64 20 6e 65     /* Old and ne
33e36 77 20 76 61 72 69 6e 74 20 6c 65 6e 20 6f 66 20  w varint len of 
33e37 66 69 72 73 74 20 64 6f 63 69 64 2e 20 2a 2f 0a  first docid. */.
33e38 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
33e39 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4c   sqlite_int64 iL
33e3a 61 73 74 44 6f 63 69 64 44 65 6c 74 61 3b 0a 23  astDocidDelta;.#
33e3b 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 63 6f  endif..  /* Reco
33e3c 64 65 20 74 68 65 20 69 6e 69 74 69 61 6c 20 64  de the initial d
33e3d 6f 63 69 64 20 61 73 20 64 65 6c 74 61 20 66 72  ocid as delta fr
33e3e 6f 6d 20 69 50 72 65 76 44 6f 63 69 64 2e 20 2a  om iPrevDocid. *
33e3f 2f 0a 20 20 6e 46 69 72 73 74 4f 6c 64 20 3d 20  /.  nFirstOld = 
33e40 66 74 73 33 47 65 74 56 61 72 69 6e 74 28 70 44  fts3GetVarint(pD
33e41 61 74 61 2c 20 26 69 44 6f 63 69 64 29 3b 0a 20  ata, &iDocid);. 
33e42 20 61 73 73 65 72 74 28 20 6e 46 69 72 73 74 4f   assert( nFirstO
33e43 6c 64 3c 6e 44 61 74 61 20 7c 7c 20 28 6e 46 69  ld<nData || (nFi
33e44 72 73 74 4f 6c 64 3d 3d 6e 44 61 74 61 20 26 26  rstOld==nData &&
33e45 20 70 57 72 69 74 65 72 2d 3e 69 54 79 70 65 3d   pWriter->iType=
33e46 3d 44 4c 5f 44 4f 43 49 44 53 29 20 29 3b 0a 20  =DL_DOCIDS) );. 
33e47 20 6e 46 69 72 73 74 4e 65 77 20 3d 20 66 74 73   nFirstNew = fts
33e48 33 50 75 74 56 61 72 69 6e 74 28 63 2c 20 69 46  3PutVarint(c, iF
33e49 69 72 73 74 44 6f 63 69 64 2d 70 57 72 69 74 65  irstDocid-pWrite
33e4a 72 2d 3e 69 50 72 65 76 44 6f 63 69 64 29 3b 0a  r->iPrevDocid);.
33e4b 0a 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68 61  .  /* Verify tha
33e4c 74 20 74 68 65 20 69 6e 63 6f 6d 69 6e 67 20 64  t the incoming d
33e4d 6f 63 6c 69 73 74 20 69 73 20 76 61 6c 69 64 20  oclist is valid 
33e4e 41 4e 44 20 74 68 61 74 20 69 74 20 65 6e 64 73  AND that it ends
33e4f 20 77 69 74 68 0a 20 20 2a 2a 20 74 68 65 20 65   with.  ** the e
33e50 78 70 65 63 74 65 64 20 64 6f 63 69 64 2e 20 20  xpected docid.  
33e51 54 68 69 73 20 69 73 20 65 73 73 65 6e 74 69 61  This is essentia
33e52 6c 20 62 65 63 61 75 73 65 20 77 65 27 6c 6c 20  l because we'll 
33e53 74 72 75 73 74 20 74 68 69 73 0a 20 20 2a 2a 20  trust this.  ** 
33e54 64 6f 63 69 64 20 69 6e 20 66 75 74 75 72 65 20  docid in future 
33e55 64 65 6c 74 61 2d 65 6e 63 6f 64 69 6e 67 2e 0a  delta-encoding..
33e56 20 20 2a 2f 0a 20 20 41 53 53 45 52 54 5f 56 41    */.  ASSERT_VA
33e57 4c 49 44 5f 44 4f 43 4c 49 53 54 28 70 57 72 69  LID_DOCLIST(pWri
33e58 74 65 72 2d 3e 69 54 79 70 65 2c 20 70 44 61 74  ter->iType, pDat
33e59 61 2c 20 6e 44 61 74 61 2c 20 26 69 4c 61 73 74  a, nData, &iLast
33e5a 44 6f 63 69 64 44 65 6c 74 61 29 3b 0a 20 20 61  DocidDelta);.  a
33e5b 73 73 65 72 74 28 20 69 4c 61 73 74 44 6f 63 69  ssert( iLastDoci
33e5c 64 3d 3d 69 46 69 72 73 74 44 6f 63 69 64 2d 69  d==iFirstDocid-i
33e5d 44 6f 63 69 64 2b 69 4c 61 73 74 44 6f 63 69 64  Docid+iLastDocid
33e5e 44 65 6c 74 61 20 29 3b 0a 0a 20 20 2f 2a 20 41  Delta );..  /* A
33e5f 70 70 65 6e 64 20 72 65 63 6f 64 65 64 20 69 6e  ppend recoded in
33e60 69 74 69 61 6c 20 64 6f 63 69 64 20 61 6e 64 20  itial docid and 
33e61 65 76 65 72 79 74 68 69 6e 67 20 65 6c 73 65 2e  everything else.
33e62 20 20 52 65 73 74 20 6f 66 20 64 6f 63 69 64 73    Rest of docids
33e63 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 68 61 76  .  ** should hav
33e64 65 20 62 65 65 6e 20 64 65 6c 74 61 2d 65 6e 63  e been delta-enc
33e65 6f 64 65 64 20 66 72 6f 6d 20 70 72 65 76 69 6f  oded from previo
33e66 75 73 20 69 6e 69 74 69 61 6c 20 64 6f 63 69 64  us initial docid
33e67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 46 69  ..  */.  if( nFi
33e68 72 73 74 4f 6c 64 3c 6e 44 61 74 61 20 29 7b 0a  rstOld<nData ){.
33e69 20 20 20 20 64 61 74 61 42 75 66 66 65 72 41 70      dataBufferAp
33e6a 70 65 6e 64 32 28 70 57 72 69 74 65 72 2d 3e 62  pend2(pWriter->b
33e6b 2c 20 63 2c 20 6e 46 69 72 73 74 4e 65 77 2c 0a  , c, nFirstNew,.
33e6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33e6d 20 20 20 20 20 20 70 44 61 74 61 2b 6e 46 69 72        pData+nFir
33e6e 73 74 4f 6c 64 2c 20 6e 44 61 74 61 2d 6e 46 69  stOld, nData-nFi
33e6f 72 73 74 4f 6c 64 29 3b 0a 20 20 7d 65 6c 73 65  rstOld);.  }else
33e70 7b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72  {.    dataBuffer
33e71 41 70 70 65 6e 64 28 70 57 72 69 74 65 72 2d 3e  Append(pWriter->
33e72 62 2c 20 63 2c 20 6e 46 69 72 73 74 4e 65 77 29  b, c, nFirstNew)
33e73 3b 0a 20 20 7d 0a 20 20 70 57 72 69 74 65 72 2d  ;.  }.  pWriter-
33e74 3e 69 50 72 65 76 44 6f 63 69 64 20 3d 20 69 4c  >iPrevDocid = iL
33e75 61 73 74 44 6f 63 69 64 3b 0a 7d 0a 73 74 61 74  astDocid;.}.stat
33e76 69 63 20 76 6f 69 64 20 64 6c 77 43 6f 70 79 28  ic void dlwCopy(
33e77 44 4c 57 72 69 74 65 72 20 2a 70 57 72 69 74 65  DLWriter *pWrite
33e78 72 2c 20 44 4c 52 65 61 64 65 72 20 2a 70 52 65  r, DLReader *pRe
33e79 61 64 65 72 29 7b 0a 20 20 64 6c 77 41 70 70 65  ader){.  dlwAppe
33e7a 6e 64 28 70 57 72 69 74 65 72 2c 20 64 6c 72 44  nd(pWriter, dlrD
33e7b 6f 63 44 61 74 61 28 70 52 65 61 64 65 72 29 2c  ocData(pReader),
33e7c 20 64 6c 72 44 6f 63 44 61 74 61 42 79 74 65 73   dlrDocDataBytes
33e7d 28 70 52 65 61 64 65 72 29 2c 0a 20 20 20 20 20  (pReader),.     
33e7e 20 20 20 20 20 20 20 64 6c 72 44 6f 63 69 64 28         dlrDocid(
33e7f 70 52 65 61 64 65 72 29 2c 20 64 6c 72 44 6f 63  pReader), dlrDoc
33e80 69 64 28 70 52 65 61 64 65 72 29 29 3b 0a 7d 0a  id(pReader));.}.
33e81 73 74 61 74 69 63 20 76 6f 69 64 20 64 6c 77 41  static void dlwA
33e82 64 64 28 44 4c 57 72 69 74 65 72 20 2a 70 57 72  dd(DLWriter *pWr
33e83 69 74 65 72 2c 20 73 71 6c 69 74 65 5f 69 6e 74  iter, sqlite_int
33e84 36 34 20 69 44 6f 63 69 64 29 7b 0a 20 20 63 68  64 iDocid){.  ch
33e85 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41 58 5d  ar c[VARINT_MAX]
33e86 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 66 74 73 33  ;.  int n = fts3
33e87 50 75 74 56 61 72 69 6e 74 28 63 2c 20 69 44 6f  PutVarint(c, iDo
33e88 63 69 64 2d 70 57 72 69 74 65 72 2d 3e 69 50 72  cid-pWriter->iPr
33e89 65 76 44 6f 63 69 64 29 3b 0a 0a 20 20 2f 2a 20  evDocid);..  /* 
33e8a 44 6f 63 69 64 73 20 6d 75 73 74 20 61 73 63 65  Docids must asce
33e8b 6e 64 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  nd. */.  assert(
33e8c 20 21 70 57 72 69 74 65 72 2d 3e 68 61 73 5f 69   !pWriter->has_i
33e8d 50 72 65 76 44 6f 63 69 64 20 7c 7c 20 69 44 6f  PrevDocid || iDo
33e8e 63 69 64 3e 70 57 72 69 74 65 72 2d 3e 69 50 72  cid>pWriter->iPr
33e8f 65 76 44 6f 63 69 64 20 29 3b 0a 20 20 61 73 73  evDocid );.  ass
33e90 65 72 74 28 20 70 57 72 69 74 65 72 2d 3e 69 54  ert( pWriter->iT
33e91 79 70 65 3d 3d 44 4c 5f 44 4f 43 49 44 53 20 29  ype==DL_DOCIDS )
33e92 3b 0a 0a 20 20 64 61 74 61 42 75 66 66 65 72 41  ;..  dataBufferA
33e93 70 70 65 6e 64 28 70 57 72 69 74 65 72 2d 3e 62  ppend(pWriter->b
33e94 2c 20 63 2c 20 6e 29 3b 0a 20 20 70 57 72 69 74  , c, n);.  pWrit
33e95 65 72 2d 3e 69 50 72 65 76 44 6f 63 69 64 20 3d  er->iPrevDocid =
33e96 20 69 44 6f 63 69 64 3b 0a 23 69 66 6e 64 65 66   iDocid;.#ifndef
33e97 20 4e 44 45 42 55 47 0a 20 20 70 57 72 69 74 65   NDEBUG.  pWrite
33e98 72 2d 3e 68 61 73 5f 69 50 72 65 76 44 6f 63 69  r->has_iPrevDoci
33e99 64 20 3d 20 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a  d = 1;.#endif.}.
33e9a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
33e9b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33e9c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33e9d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33e9e 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 50 4c 52 65 61 64  *****/./* PLRead
33e9f 65 72 20 69 73 20 75 73 65 64 20 74 6f 20 72 65  er is used to re
33ea0 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61 20 64  ad data from a d
33ea1 6f 63 75 6d 65 6e 74 27 73 20 70 6f 73 69 74 69  ocument's positi
33ea2 6f 6e 20 6c 69 73 74 2e 20 20 41 73 0a 2a 2a 20  on list.  As.** 
33ea3 74 68 65 20 63 61 6c 6c 65 72 20 73 74 65 70 73  the caller steps
33ea4 20 74 68 72 6f 75 67 68 20 74 68 65 20 6c 69 73   through the lis
33ea5 74 2c 20 64 61 74 61 20 69 73 20 63 61 63 68 65  t, data is cache
33ea6 64 20 73 6f 20 74 68 61 74 20 76 61 72 69 6e 74  d so that varint
33ea7 73 0a 2a 2a 20 6f 6e 6c 79 20 6e 65 65 64 20 74  s.** only need t
33ea8 6f 20 62 65 20 64 65 63 6f 64 65 64 20 6f 6e 63  o be decoded onc
33ea9 65 2e 0a 2a 2a 0a 2a 2a 20 70 6c 72 49 6e 69 74  e..**.** plrInit
33eaa 2c 20 70 6c 72 44 65 73 74 72 6f 79 20 2d 20 63  , plrDestroy - c
33eab 72 65 61 74 65 2f 64 65 73 74 72 6f 79 20 61 20  reate/destroy a 
33eac 72 65 61 64 65 72 2e 0a 2a 2a 20 70 6c 72 43 6f  reader..** plrCo
33ead 6c 75 6d 6e 2c 20 70 6c 72 50 6f 73 69 74 69 6f  lumn, plrPositio
33eae 6e 2c 20 70 6c 72 53 74 61 72 74 4f 66 66 73 65  n, plrStartOffse
33eaf 74 2c 20 70 6c 72 45 6e 64 4f 66 66 73 65 74 20  t, plrEndOffset 
33eb0 2d 20 61 63 63 65 73 73 6f 72 73 0a 2a 2a 20 70  - accessors.** p
33eb1 6c 72 41 74 45 6e 64 20 2d 20 61 74 20 65 6e 64  lrAtEnd - at end
33eb2 20 6f 66 20 73 74 72 65 61 6d 2c 20 6f 6e 6c 79   of stream, only
33eb3 20 63 61 6c 6c 20 70 6c 72 44 65 73 74 72 6f 79   call plrDestroy
33eb4 20 6f 6e 63 65 20 74 72 75 65 2e 0a 2a 2a 20 70   once true..** p
33eb5 6c 72 53 74 65 70 20 2d 20 73 74 65 70 20 74 6f  lrStep - step to
33eb6 20 74 68 65 20 6e 65 78 74 20 65 6c 65 6d 65 6e   the next elemen
33eb7 74 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  t..*/.typedef st
33eb8 72 75 63 74 20 50 4c 52 65 61 64 65 72 20 7b 0a  ruct PLReader {.
33eb9 20 20 2f 2a 20 54 68 65 73 65 20 72 65 66 65 72    /* These refer
33eba 20 74 6f 20 74 68 65 20 6e 65 78 74 20 70 6f 73   to the next pos
33ebb 69 74 69 6f 6e 27 73 20 64 61 74 61 2e 20 20 6e  ition's data.  n
33ebc 44 61 74 61 20 77 69 6c 6c 20 72 65 61 63 68 20  Data will reach 
33ebd 30 20 77 68 65 6e 0a 20 20 2a 2a 20 72 65 61 64  0 when.  ** read
33ebe 69 6e 67 20 74 68 65 20 6c 61 73 74 20 70 6f 73  ing the last pos
33ebf 69 74 69 6f 6e 2c 20 73 6f 20 70 6c 72 53 74 65  ition, so plrSte
33ec0 70 28 29 20 73 69 67 6e 61 6c 73 20 45 4f 46 20  p() signals EOF 
33ec1 62 79 20 73 65 74 74 69 6e 67 0a 20 20 2a 2a 20  by setting.  ** 
33ec2 70 44 61 74 61 20 74 6f 20 4e 55 4c 4c 2e 0a 20  pData to NULL.. 
33ec3 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
33ec4 20 2a 70 44 61 74 61 3b 0a 20 20 69 6e 74 20 6e   *pData;.  int n
33ec5 44 61 74 61 3b 0a 0a 20 20 44 6f 63 4c 69 73 74  Data;..  DocList
33ec6 54 79 70 65 20 69 54 79 70 65 3b 0a 20 20 69 6e  Type iType;.  in
33ec7 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20  t iColumn;      
33ec8 20 20 20 2f 2a 20 74 68 65 20 6c 61 73 74 20 63     /* the last c
33ec9 6f 6c 75 6d 6e 20 72 65 61 64 20 2a 2f 0a 20 20  olumn read */.  
33eca 69 6e 74 20 69 50 6f 73 69 74 69 6f 6e 3b 20 20  int iPosition;  
33ecb 20 20 20 20 20 2f 2a 20 74 68 65 20 6c 61 73 74       /* the last
33ecc 20 70 6f 73 69 74 69 6f 6e 20 72 65 61 64 20 2a   position read *
33ecd 2f 0a 20 20 69 6e 74 20 69 53 74 61 72 74 4f 66  /.  int iStartOf
33ece 66 73 65 74 3b 20 20 20 20 2f 2a 20 74 68 65 20  fset;    /* the 
33ecf 6c 61 73 74 20 73 74 61 72 74 20 6f 66 66 73 65  last start offse
33ed0 74 20 72 65 61 64 20 2a 2f 0a 20 20 69 6e 74 20  t read */.  int 
33ed1 69 45 6e 64 4f 66 66 73 65 74 3b 20 20 20 20 20  iEndOffset;     
33ed2 20 2f 2a 20 74 68 65 20 6c 61 73 74 20 65 6e 64   /* the last end
33ed3 20 6f 66 66 73 65 74 20 72 65 61 64 20 2a 2f 0a   offset read */.
33ed4 7d 20 50 4c 52 65 61 64 65 72 3b 0a 0a 73 74 61  } PLReader;..sta
33ed5 74 69 63 20 69 6e 74 20 70 6c 72 41 74 45 6e 64  tic int plrAtEnd
33ed6 28 50 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64  (PLReader *pRead
33ed7 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 52  er){.  return pR
33ed8 65 61 64 65 72 2d 3e 70 44 61 74 61 3d 3d 4e 55  eader->pData==NU
33ed9 4c 4c 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74  LL;.}.static int
33eda 20 70 6c 72 43 6f 6c 75 6d 6e 28 50 4c 52 65 61   plrColumn(PLRea
33edb 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
33edc 20 61 73 73 65 72 74 28 20 21 70 6c 72 41 74 45   assert( !plrAtE
33edd 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 20  nd(pReader) );. 
33ede 20 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d   return pReader-
33edf 3e 69 43 6f 6c 75 6d 6e 3b 0a 7d 0a 73 74 61 74  >iColumn;.}.stat
33ee0 69 63 20 69 6e 74 20 70 6c 72 50 6f 73 69 74 69  ic int plrPositi
33ee1 6f 6e 28 50 4c 52 65 61 64 65 72 20 2a 70 52 65  on(PLReader *pRe
33ee2 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28  ader){.  assert(
33ee3 20 21 70 6c 72 41 74 45 6e 64 28 70 52 65 61 64   !plrAtEnd(pRead
33ee4 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  er) );.  return 
33ee5 70 52 65 61 64 65 72 2d 3e 69 50 6f 73 69 74 69  pReader->iPositi
33ee6 6f 6e 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74  on;.}.static int
33ee7 20 70 6c 72 53 74 61 72 74 4f 66 66 73 65 74 28   plrStartOffset(
33ee8 50 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  PLReader *pReade
33ee9 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 70  r){.  assert( !p
33eea 6c 72 41 74 45 6e 64 28 70 52 65 61 64 65 72 29  lrAtEnd(pReader)
33eeb 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65   );.  return pRe
33eec 61 64 65 72 2d 3e 69 53 74 61 72 74 4f 66 66 73  ader->iStartOffs
33eed 65 74 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74  et;.}.static int
33eee 20 70 6c 72 45 6e 64 4f 66 66 73 65 74 28 50 4c   plrEndOffset(PL
33eef 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
33ef0 7b 0a 20 20 61 73 73 65 72 74 28 20 21 70 6c 72  {.  assert( !plr
33ef1 41 74 45 6e 64 28 70 52 65 61 64 65 72 29 20 29  AtEnd(pReader) )
33ef2 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61 64  ;.  return pRead
33ef3 65 72 2d 3e 69 45 6e 64 4f 66 66 73 65 74 3b 0a  er->iEndOffset;.
33ef4 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 6c  }.static void pl
33ef5 72 53 74 65 70 28 50 4c 52 65 61 64 65 72 20 2a  rStep(PLReader *
33ef6 70 52 65 61 64 65 72 29 7b 0a 20 20 69 6e 74 20  pReader){.  int 
33ef7 69 2c 20 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28  i, n;..  assert(
33ef8 20 21 70 6c 72 41 74 45 6e 64 28 70 52 65 61 64   !plrAtEnd(pRead
33ef9 65 72 29 20 29 3b 0a 0a 20 20 69 66 28 20 70 52  er) );..  if( pR
33efa 65 61 64 65 72 2d 3e 6e 44 61 74 61 3d 3d 30 20  eader->nData==0 
33efb 29 7b 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e  ){.    pReader->
33efc 70 44 61 74 61 20 3d 20 4e 55 4c 4c 3b 0a 20 20  pData = NULL;.  
33efd 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20    return;.  }.. 
33efe 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69   n = fts3GetVari
33eff 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44  nt32(pReader->pD
33f00 61 74 61 2c 20 26 69 29 3b 0a 20 20 69 66 28 20  ata, &i);.  if( 
33f01 69 3d 3d 50 4f 53 5f 43 4f 4c 55 4d 4e 20 29 7b  i==POS_COLUMN ){
33f02 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47 65  .    n += fts3Ge
33f03 74 56 61 72 69 6e 74 33 32 28 70 52 65 61 64 65  tVarint32(pReade
33f04 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 70 52 65  r->pData+n, &pRe
33f05 61 64 65 72 2d 3e 69 43 6f 6c 75 6d 6e 29 3b 0a  ader->iColumn);.
33f06 20 20 20 20 70 52 65 61 64 65 72 2d 3e 69 50 6f      pReader->iPo
33f07 73 69 74 69 6f 6e 20 3d 20 30 3b 0a 20 20 20 20  sition = 0;.    
33f08 70 52 65 61 64 65 72 2d 3e 69 53 74 61 72 74 4f  pReader->iStartO
33f09 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 6e  ffset = 0;.    n
33f0a 20 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e   += fts3GetVarin
33f0b 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61  t32(pReader->pDa
33f0c 74 61 2b 6e 2c 20 26 69 29 3b 0a 20 20 7d 0a 20  ta+n, &i);.  }. 
33f0d 20 2f 2a 20 53 68 6f 75 6c 64 20 6e 65 76 65 72   /* Should never
33f0e 20 73 65 65 20 61 64 6a 61 63 65 6e 74 20 63 6f   see adjacent co
33f0f 6c 75 6d 6e 20 63 68 61 6e 67 65 73 2e 20 2a 2f  lumn changes. */
33f10 0a 20 20 61 73 73 65 72 74 28 20 69 21 3d 50 4f  .  assert( i!=PO
33f11 53 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 0a 20 20 69  S_COLUMN );..  i
33f12 66 28 20 69 3d 3d 50 4f 53 5f 45 4e 44 20 29 7b  f( i==POS_END ){
33f13 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e 6e 44  .    pReader->nD
33f14 61 74 61 20 3d 20 30 3b 0a 20 20 20 20 70 52 65  ata = 0;.    pRe
33f15 61 64 65 72 2d 3e 70 44 61 74 61 20 3d 20 4e 55  ader->pData = NU
33f16 4c 4c 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  LL;.    return;.
33f17 20 20 7d 0a 0a 20 20 70 52 65 61 64 65 72 2d 3e    }..  pReader->
33f18 69 50 6f 73 69 74 69 6f 6e 20 2b 3d 20 69 2d 50  iPosition += i-P
33f19 4f 53 5f 42 41 53 45 3b 0a 20 20 69 66 28 20 70  OS_BASE;.  if( p
33f1a 52 65 61 64 65 72 2d 3e 69 54 79 70 65 3d 3d 44  Reader->iType==D
33f1b 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53  L_POSITIONS_OFFS
33f1c 45 54 53 20 29 7b 0a 20 20 20 20 6e 20 2b 3d 20  ETS ){.    n += 
33f1d 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
33f1e 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e  pReader->pData+n
33f1f 2c 20 26 69 29 3b 0a 20 20 20 20 70 52 65 61 64  , &i);.    pRead
33f20 65 72 2d 3e 69 53 74 61 72 74 4f 66 66 73 65 74  er->iStartOffset
33f21 20 2b 3d 20 69 3b 0a 20 20 20 20 6e 20 2b 3d 20   += i;.    n += 
33f22 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
33f23 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e  pReader->pData+n
33f24 2c 20 26 69 29 3b 0a 20 20 20 20 70 52 65 61 64  , &i);.    pRead
33f25 65 72 2d 3e 69 45 6e 64 4f 66 66 73 65 74 20 3d  er->iEndOffset =
33f26 20 70 52 65 61 64 65 72 2d 3e 69 53 74 61 72 74   pReader->iStart
33f27 4f 66 66 73 65 74 2b 69 3b 0a 20 20 7d 0a 20 20  Offset+i;.  }.  
33f28 61 73 73 65 72 74 28 20 6e 3c 3d 70 52 65 61 64  assert( n<=pRead
33f29 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20 70  er->nData );.  p
33f2a 52 65 61 64 65 72 2d 3e 70 44 61 74 61 20 2b 3d  Reader->pData +=
33f2b 20 6e 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 6e   n;.  pReader->n
33f2c 44 61 74 61 20 2d 3d 20 6e 3b 0a 7d 0a 0a 73 74  Data -= n;.}..st
33f2d 61 74 69 63 20 76 6f 69 64 20 70 6c 72 49 6e 69  atic void plrIni
33f2e 74 28 50 4c 52 65 61 64 65 72 20 2a 70 52 65 61  t(PLReader *pRea
33f2f 64 65 72 2c 20 44 4c 52 65 61 64 65 72 20 2a 70  der, DLReader *p
33f30 44 4c 52 65 61 64 65 72 29 7b 0a 20 20 70 52 65  DLReader){.  pRe
33f31 61 64 65 72 2d 3e 70 44 61 74 61 20 3d 20 64 6c  ader->pData = dl
33f32 72 50 6f 73 44 61 74 61 28 70 44 4c 52 65 61 64  rPosData(pDLRead
33f33 65 72 29 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e  er);.  pReader->
33f34 6e 44 61 74 61 20 3d 20 64 6c 72 50 6f 73 44 61  nData = dlrPosDa
33f35 74 61 4c 65 6e 28 70 44 4c 52 65 61 64 65 72 29  taLen(pDLReader)
33f36 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 69 54 79  ;.  pReader->iTy
33f37 70 65 20 3d 20 70 44 4c 52 65 61 64 65 72 2d 3e  pe = pDLReader->
33f38 69 54 79 70 65 3b 0a 20 20 70 52 65 61 64 65 72  iType;.  pReader
33f39 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 30 3b 0a 20  ->iColumn = 0;. 
33f3a 20 70 52 65 61 64 65 72 2d 3e 69 50 6f 73 69 74   pReader->iPosit
33f3b 69 6f 6e 20 3d 20 30 3b 0a 20 20 70 52 65 61 64  ion = 0;.  pRead
33f3c 65 72 2d 3e 69 53 74 61 72 74 4f 66 66 73 65 74  er->iStartOffset
33f3d 20 3d 20 30 3b 0a 20 20 70 52 65 61 64 65 72 2d   = 0;.  pReader-
33f3e 3e 69 45 6e 64 4f 66 66 73 65 74 20 3d 20 30 3b  >iEndOffset = 0;
33f3f 0a 20 20 70 6c 72 53 74 65 70 28 70 52 65 61 64  .  plrStep(pRead
33f40 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  er);.}.static vo
33f41 69 64 20 70 6c 72 44 65 73 74 72 6f 79 28 50 4c  id plrDestroy(PL
33f42 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
33f43 7b 0a 20 20 53 43 52 41 4d 42 4c 45 28 70 52 65  {.  SCRAMBLE(pRe
33f44 61 64 65 72 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  ader);.}../*****
33f45 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33f46 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33f47 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33f48 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
33f49 2f 2a 20 50 4c 57 72 69 74 65 72 20 69 73 20 75  /* PLWriter is u
33f4a 73 65 64 20 69 6e 20 63 6f 6e 73 74 72 75 63 74  sed in construct
33f4b 69 6e 67 20 61 20 64 6f 63 75 6d 65 6e 74 27 73  ing a document's
33f4c 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 2e 20   position list. 
33f4d 20 41 73 20 61 0a 2a 2a 20 63 6f 6e 76 65 6e 69   As a.** conveni
33f4e 65 6e 63 65 2c 20 69 66 20 69 54 79 70 65 20 69  ence, if iType i
33f4f 73 20 44 4c 5f 44 4f 43 49 44 53 2c 20 50 4c 57  s DL_DOCIDS, PLW
33f50 72 69 74 65 72 20 62 65 63 6f 6d 65 73 20 61 20  riter becomes a 
33f51 6e 6f 2d 6f 70 2e 0a 2a 2a 20 50 4c 57 72 69 74  no-op..** PLWrit
33f52 65 72 20 77 72 69 74 65 73 20 74 6f 20 74 68 65  er writes to the
33f53 20 61 73 73 6f 63 69 61 74 65 64 20 44 4c 57 72   associated DLWr
33f54 69 74 65 72 27 73 20 62 75 66 66 65 72 2e 0a 2a  iter's buffer..*
33f55 2a 0a 2a 2a 20 70 6c 77 49 6e 69 74 20 2d 20 69  *.** plwInit - i
33f56 6e 69 74 20 66 6f 72 20 77 72 69 74 69 6e 67 20  nit for writing 
33f57 61 20 64 6f 63 75 6d 65 6e 74 27 73 20 70 6f 73  a document's pos
33f58 6c 69 73 74 2e 0a 2a 2a 20 70 6c 77 44 65 73 74  list..** plwDest
33f59 72 6f 79 20 2d 20 63 6c 65 61 72 20 61 20 77 72  roy - clear a wr
33f5a 69 74 65 72 2e 0a 2a 2a 20 70 6c 77 41 64 64 20  iter..** plwAdd 
33f5b 2d 20 61 70 70 65 6e 64 20 70 6f 73 69 74 69 6f  - append positio
33f5c 6e 20 61 6e 64 20 6f 66 66 73 65 74 20 69 6e 66  n and offset inf
33f5d 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 20 70 6c 77  ormation..** plw
33f5e 43 6f 70 79 20 2d 20 63 6f 70 79 20 6e 65 78 74  Copy - copy next
33f5f 20 70 6f 73 69 74 69 6f 6e 27 73 20 64 61 74 61   position's data
33f60 20 66 72 6f 6d 20 72 65 61 64 65 72 20 74 6f 20   from reader to 
33f61 77 72 69 74 65 72 2e 0a 2a 2a 20 70 6c 77 54 65  writer..** plwTe
33f62 72 6d 69 6e 61 74 65 20 2d 20 61 64 64 20 61 6e  rminate - add an
33f63 79 20 6e 65 63 65 73 73 61 72 79 20 64 6f 63 6c  y necessary docl
33f64 69 73 74 20 74 65 72 6d 69 6e 61 74 6f 72 2e 0a  ist terminator..
33f65 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 70 6c  **.** Calling pl
33f66 77 41 64 64 28 29 20 61 66 74 65 72 20 70 6c 77  wAdd() after plw
33f67 54 65 72 6d 69 6e 61 74 65 28 29 20 6d 61 79 20  Terminate() may 
33f68 72 65 73 75 6c 74 20 69 6e 20 61 20 63 6f 72 72  result in a corr
33f69 75 70 74 0a 2a 2a 20 64 6f 63 6c 69 73 74 2e 0a  upt.** doclist..
33f6a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73  */./* TODO(shess
33f6b 29 20 55 6e 74 69 6c 20 77 65 27 76 65 20 77 72  ) Until we've wr
33f6c 69 74 74 65 6e 20 74 68 65 20 73 65 63 6f 6e 64  itten the second
33f6d 20 69 74 65 6d 2c 20 77 65 20 63 61 6e 20 63 61   item, we can ca
33f6e 63 68 65 20 74 68 65 0a 2a 2a 20 66 69 72 73 74  che the.** first
33f6f 20 69 74 65 6d 27 73 20 69 6e 66 6f 72 6d 61 74   item's informat
33f70 69 6f 6e 2e 20 20 54 68 65 6e 20 77 65 27 64 20  ion.  Then we'd 
33f71 68 61 76 65 20 74 68 72 65 65 20 73 74 61 74 65  have three state
33f72 73 3a 0a 2a 2a 0a 2a 2a 20 2d 20 69 6e 69 74 69  s:.**.** - initi
33f73 61 6c 69 7a 65 64 20 77 69 74 68 20 64 6f 63 69  alized with doci
33f74 64 2c 20 6e 6f 20 70 6f 73 69 74 69 6f 6e 73 2e  d, no positions.
33f75 0a 2a 2a 20 2d 20 64 6f 63 69 64 20 61 6e 64 20  .** - docid and 
33f76 6f 6e 65 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2a  one position..**
33f77 20 2d 20 64 6f 63 69 64 20 61 6e 64 20 6d 75 6c   - docid and mul
33f78 74 69 70 6c 65 20 70 6f 73 69 74 69 6f 6e 73 2e  tiple positions.
33f79 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 74 68 65 20  .**.** Only the 
33f7a 6c 61 73 74 20 73 74 61 74 65 20 6e 65 65 64 73  last state needs
33f7b 20 74 6f 20 61 63 74 75 61 6c 6c 79 20 77 72 69   to actually wri
33f7c 74 65 20 74 6f 20 64 6c 77 2d 3e 62 2c 20 77 68  te to dlw->b, wh
33f7d 69 63 68 20 77 6f 75 6c 64 0a 2a 2a 20 62 65 20  ich would.** be 
33f7e 61 6e 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20 69  an improvement i
33f7f 6e 20 74 68 65 20 44 4c 43 6f 6c 6c 65 63 74 6f  n the DLCollecto
33f80 72 20 63 61 73 65 2e 0a 2a 2f 0a 74 79 70 65 64  r case..*/.typed
33f81 65 66 20 73 74 72 75 63 74 20 50 4c 57 72 69 74  ef struct PLWrit
33f82 65 72 20 7b 0a 20 20 44 4c 57 72 69 74 65 72 20  er {.  DLWriter 
33f83 2a 64 6c 77 3b 0a 0a 20 20 69 6e 74 20 69 43 6f  *dlw;..  int iCo
33f84 6c 75 6d 6e 3b 20 20 20 20 2f 2a 20 74 68 65 20  lumn;    /* the 
33f85 6c 61 73 74 20 63 6f 6c 75 6d 6e 20 77 72 69 74  last column writ
33f86 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 50 6f  ten */.  int iPo
33f87 73 3b 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20  s;       /* the 
33f88 6c 61 73 74 20 70 6f 73 69 74 69 6f 6e 20 77 72  last position wr
33f89 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 69  itten */.  int i
33f8a 4f 66 66 73 65 74 3b 20 20 20 20 2f 2a 20 74 68  Offset;    /* th
33f8b 65 20 6c 61 73 74 20 73 74 61 72 74 20 6f 66 66  e last start off
33f8c 73 65 74 20 77 72 69 74 74 65 6e 20 2a 2f 0a 7d  set written */.}
33f8d 20 50 4c 57 72 69 74 65 72 3b 0a 0a 2f 2a 20 54   PLWriter;../* T
33f8e 4f 44 4f 28 73 68 65 73 73 29 20 49 6e 20 74 68  ODO(shess) In th
33f8f 65 20 63 61 73 65 20 77 68 65 72 65 20 74 68 65  e case where the
33f90 20 70 61 72 65 6e 74 20 69 73 20 72 65 61 64 69   parent is readi
33f91 6e 67 20 74 68 65 73 65 20 76 61 6c 75 65 73 0a  ng these values.
33f92 2a 2a 20 66 72 6f 6d 20 61 20 50 4c 52 65 61 64  ** from a PLRead
33f93 65 72 2c 20 77 65 20 63 6f 75 6c 64 20 6f 70 74  er, we could opt
33f94 69 6d 69 7a 65 20 74 6f 20 61 20 63 6f 70 79 20  imize to a copy 
33f95 69 66 20 74 68 61 74 20 50 4c 52 65 61 64 65 72  if that PLReader
33f96 20 68 61 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65   has.** the same
33f97 20 74 79 70 65 20 61 73 20 70 57 72 69 74 65 72   type as pWriter
33f98 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
33f99 20 70 6c 77 41 64 64 28 50 4c 57 72 69 74 65 72   plwAdd(PLWriter
33f9a 20 2a 70 57 72 69 74 65 72 2c 20 69 6e 74 20 69   *pWriter, int i
33f9b 43 6f 6c 75 6d 6e 2c 20 69 6e 74 20 69 50 6f 73  Column, int iPos
33f9c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
33f9d 20 20 20 20 20 69 6e 74 20 69 53 74 61 72 74 4f       int iStartO
33f9e 66 66 73 65 74 2c 20 69 6e 74 20 69 45 6e 64 4f  ffset, int iEndO
33f9f 66 66 73 65 74 29 7b 0a 20 20 2f 2a 20 57 6f 72  ffset){.  /* Wor
33fa0 73 74 2d 63 61 73 65 20 73 70 61 63 65 20 66 6f  st-case space fo
33fa1 72 20 50 4f 53 5f 43 4f 4c 55 4d 4e 2c 20 69 43  r POS_COLUMN, iC
33fa2 6f 6c 75 6d 6e 2c 20 69 50 6f 73 44 65 6c 74 61  olumn, iPosDelta
33fa3 2c 0a 20 20 2a 2a 20 69 53 74 61 72 74 4f 66 66  ,.  ** iStartOff
33fa4 73 65 74 44 65 6c 74 61 2c 20 61 6e 64 20 69 45  setDelta, and iE
33fa5 6e 64 4f 66 66 73 65 74 44 65 6c 74 61 2e 0a 20  ndOffsetDelta.. 
33fa6 20 2a 2f 0a 20 20 63 68 61 72 20 63 5b 35 2a 56   */.  char c[5*V
33fa7 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e  ARINT_MAX];.  in
33fa8 74 20 6e 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 42  t n = 0;..  /* B
33fa9 61 6e 20 70 6c 77 41 64 64 28 29 20 61 66 74 65  an plwAdd() afte
33faa 72 20 70 6c 77 54 65 72 6d 69 6e 61 74 65 28 29  r plwTerminate()
33fab 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  . */.  assert( p
33fac 57 72 69 74 65 72 2d 3e 69 50 6f 73 21 3d 2d 31  Writer->iPos!=-1
33fad 20 29 3b 0a 0a 20 20 69 66 28 20 70 57 72 69 74   );..  if( pWrit
33fae 65 72 2d 3e 64 6c 77 2d 3e 69 54 79 70 65 3d 3d  er->dlw->iType==
33faf 44 4c 5f 44 4f 43 49 44 53 20 29 20 72 65 74 75  DL_DOCIDS ) retu
33fb0 72 6e 3b 0a 0a 20 20 69 66 28 20 69 43 6f 6c 75  rn;..  if( iColu
33fb1 6d 6e 21 3d 70 57 72 69 74 65 72 2d 3e 69 43 6f  mn!=pWriter->iCo
33fb2 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 6e 20 2b 3d  lumn ){.    n +=
33fb3 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63   fts3PutVarint(c
33fb4 2b 6e 2c 20 50 4f 53 5f 43 4f 4c 55 4d 4e 29 3b  +n, POS_COLUMN);
33fb5 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 50 75  .    n += fts3Pu
33fb6 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20 69 43 6f  tVarint(c+n, iCo
33fb7 6c 75 6d 6e 29 3b 0a 20 20 20 20 70 57 72 69 74  lumn);.    pWrit
33fb8 65 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 69 43  er->iColumn = iC
33fb9 6f 6c 75 6d 6e 3b 0a 20 20 20 20 70 57 72 69 74  olumn;.    pWrit
33fba 65 72 2d 3e 69 50 6f 73 20 3d 20 30 3b 0a 20 20  er->iPos = 0;.  
33fbb 20 20 70 57 72 69 74 65 72 2d 3e 69 4f 66 66 73    pWriter->iOffs
33fbc 65 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 61 73  et = 0;.  }.  as
33fbd 73 65 72 74 28 20 69 50 6f 73 3e 3d 70 57 72 69  sert( iPos>=pWri
33fbe 74 65 72 2d 3e 69 50 6f 73 20 29 3b 0a 20 20 6e  ter->iPos );.  n
33fbf 20 2b 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e   += fts3PutVarin
33fc0 74 28 63 2b 6e 2c 20 50 4f 53 5f 42 41 53 45 2b  t(c+n, POS_BASE+
33fc1 28 69 50 6f 73 2d 70 57 72 69 74 65 72 2d 3e 69  (iPos-pWriter->i
33fc2 50 6f 73 29 29 3b 0a 20 20 70 57 72 69 74 65 72  Pos));.  pWriter
33fc3 2d 3e 69 50 6f 73 20 3d 20 69 50 6f 73 3b 0a 20  ->iPos = iPos;. 
33fc4 20 69 66 28 20 70 57 72 69 74 65 72 2d 3e 64 6c   if( pWriter->dl
33fc5 77 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f 50 4f 53  w->iType==DL_POS
33fc6 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20 29  ITIONS_OFFSETS )
33fc7 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 53  {.    assert( iS
33fc8 74 61 72 74 4f 66 66 73 65 74 3e 3d 70 57 72 69  tartOffset>=pWri
33fc9 74 65 72 2d 3e 69 4f 66 66 73 65 74 20 29 3b 0a  ter->iOffset );.
33fca 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 50 75 74      n += fts3Put
33fcb 56 61 72 69 6e 74 28 63 2b 6e 2c 20 69 53 74 61  Varint(c+n, iSta
33fcc 72 74 4f 66 66 73 65 74 2d 70 57 72 69 74 65 72  rtOffset-pWriter
33fcd 2d 3e 69 4f 66 66 73 65 74 29 3b 0a 20 20 20 20  ->iOffset);.    
33fce 70 57 72 69 74 65 72 2d 3e 69 4f 66 66 73 65 74  pWriter->iOffset
33fcf 20 3d 20 69 53 74 61 72 74 4f 66 66 73 65 74 3b   = iStartOffset;
33fd0 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 45 6e  .    assert( iEn
33fd1 64 4f 66 66 73 65 74 3e 3d 69 53 74 61 72 74 4f  dOffset>=iStartO
33fd2 66 66 73 65 74 20 29 3b 0a 20 20 20 20 6e 20 2b  ffset );.    n +
33fd3 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28  = fts3PutVarint(
33fd4 63 2b 6e 2c 20 69 45 6e 64 4f 66 66 73 65 74 2d  c+n, iEndOffset-
33fd5 69 53 74 61 72 74 4f 66 66 73 65 74 29 3b 0a 20  iStartOffset);. 
33fd6 20 7d 0a 20 20 64 61 74 61 42 75 66 66 65 72 41   }.  dataBufferA
33fd7 70 70 65 6e 64 28 70 57 72 69 74 65 72 2d 3e 64  ppend(pWriter->d
33fd8 6c 77 2d 3e 62 2c 20 63 2c 20 6e 29 3b 0a 7d 0a  lw->b, c, n);.}.
33fd9 73 74 61 74 69 63 20 76 6f 69 64 20 70 6c 77 43  static void plwC
33fda 6f 70 79 28 50 4c 57 72 69 74 65 72 20 2a 70 57  opy(PLWriter *pW
33fdb 72 69 74 65 72 2c 20 50 4c 52 65 61 64 65 72 20  riter, PLReader 
33fdc 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 70 6c 77  *pReader){.  plw
33fdd 41 64 64 28 70 57 72 69 74 65 72 2c 20 70 6c 72  Add(pWriter, plr
33fde 43 6f 6c 75 6d 6e 28 70 52 65 61 64 65 72 29 2c  Column(pReader),
33fdf 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28 70 52 65   plrPosition(pRe
33fe0 61 64 65 72 29 2c 0a 20 20 20 20 20 20 20 20 20  ader),.         
33fe1 70 6c 72 53 74 61 72 74 4f 66 66 73 65 74 28 70  plrStartOffset(p
33fe2 52 65 61 64 65 72 29 2c 20 70 6c 72 45 6e 64 4f  Reader), plrEndO
33fe3 66 66 73 65 74 28 70 52 65 61 64 65 72 29 29 3b  ffset(pReader));
33fe4 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  .}.static void p
33fe5 6c 77 49 6e 69 74 28 50 4c 57 72 69 74 65 72 20  lwInit(PLWriter 
33fe6 2a 70 57 72 69 74 65 72 2c 20 44 4c 57 72 69 74  *pWriter, DLWrit
33fe7 65 72 20 2a 64 6c 77 2c 20 73 71 6c 69 74 65 5f  er *dlw, sqlite_
33fe8 69 6e 74 36 34 20 69 44 6f 63 69 64 29 7b 0a 20  int64 iDocid){. 
33fe9 20 63 68 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d   char c[VARINT_M
33fea 41 58 5d 3b 0a 20 20 69 6e 74 20 6e 3b 0a 0a 20  AX];.  int n;.. 
33feb 20 70 57 72 69 74 65 72 2d 3e 64 6c 77 20 3d 20   pWriter->dlw = 
33fec 64 6c 77 3b 0a 0a 20 20 2f 2a 20 44 6f 63 69 64  dlw;..  /* Docid
33fed 73 20 6d 75 73 74 20 61 73 63 65 6e 64 2e 20 2a  s must ascend. *
33fee 2f 0a 20 20 61 73 73 65 72 74 28 20 21 70 57 72  /.  assert( !pWr
33fef 69 74 65 72 2d 3e 64 6c 77 2d 3e 68 61 73 5f 69  iter->dlw->has_i
33ff0 50 72 65 76 44 6f 63 69 64 20 7c 7c 20 69 44 6f  PrevDocid || iDo
33ff1 63 69 64 3e 70 57 72 69 74 65 72 2d 3e 64 6c 77  cid>pWriter->dlw
33ff2 2d 3e 69 50 72 65 76 44 6f 63 69 64 20 29 3b 0a  ->iPrevDocid );.
33ff3 20 20 6e 20 3d 20 66 74 73 33 50 75 74 56 61 72    n = fts3PutVar
33ff4 69 6e 74 28 63 2c 20 69 44 6f 63 69 64 2d 70 57  int(c, iDocid-pW
33ff5 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 69 50 72 65  riter->dlw->iPre
33ff6 76 44 6f 63 69 64 29 3b 0a 20 20 64 61 74 61 42  vDocid);.  dataB
33ff7 75 66 66 65 72 41 70 70 65 6e 64 28 70 57 72 69  ufferAppend(pWri
33ff8 74 65 72 2d 3e 64 6c 77 2d 3e 62 2c 20 63 2c 20  ter->dlw->b, c, 
33ff9 6e 29 3b 0a 20 20 70 57 72 69 74 65 72 2d 3e 64  n);.  pWriter->d
33ffa 6c 77 2d 3e 69 50 72 65 76 44 6f 63 69 64 20 3d  lw->iPrevDocid =
33ffb 20 69 44 6f 63 69 64 3b 0a 23 69 66 6e 64 65 66   iDocid;.#ifndef
33ffc 20 4e 44 45 42 55 47 0a 20 20 70 57 72 69 74 65   NDEBUG.  pWrite
33ffd 72 2d 3e 64 6c 77 2d 3e 68 61 73 5f 69 50 72 65  r->dlw->has_iPre
33ffe 76 44 6f 63 69 64 20 3d 20 31 3b 0a 23 65 6e 64  vDocid = 1;.#end
33fff 69 66 0a 0a 20 20 70 57 72 69 74 65 72 2d 3e 69  if..  pWriter->i
34000 43 6f 6c 75 6d 6e 20 3d 20 30 3b 0a 20 20 70 57  Column = 0;.  pW
34001 72 69 74 65 72 2d 3e 69 50 6f 73 20 3d 20 30 3b  riter->iPos = 0;
34002 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 4f 66 66  .  pWriter->iOff
34003 73 65 74 20 3d 20 30 3b 0a 7d 0a 2f 2a 20 54 4f  set = 0;.}./* TO
34004 44 4f 28 73 68 65 73 73 29 20 53 68 6f 75 6c 64  DO(shess) Should
34005 20 70 6c 77 44 65 73 74 72 6f 79 28 29 20 61 6c   plwDestroy() al
34006 73 6f 20 74 65 72 6d 69 6e 61 74 65 20 74 68 65  so terminate the
34007 20 64 6f 63 6c 69 73 74 3f 20 20 42 75 74 0a 2a   doclist?  But.*
34008 2a 20 74 68 65 6e 20 70 6c 77 44 65 73 74 72 6f  * then plwDestro
34009 79 28 29 20 77 6f 75 6c 64 20 6e 6f 20 6c 6f 6e  y() would no lon
3400a 67 65 72 20 62 65 20 6a 75 73 74 20 61 20 64 65  ger be just a de
3400b 73 74 72 75 63 74 6f 72 2c 20 69 74 20 77 6f 75  structor, it wou
3400c 6c 64 0a 2a 2a 20 61 6c 73 6f 20 62 65 20 64 6f  ld.** also be do
3400d 69 6e 67 20 77 6f 72 6b 2c 20 77 68 69 63 68 20  ing work, which 
3400e 69 73 6e 27 74 20 63 6f 6e 73 69 73 74 65 6e 74  isn't consistent
3400f 20 77 69 74 68 20 74 68 65 20 6f 76 65 72 61 6c   with the overal
34010 6c 20 69 64 69 6f 6d 2e 0a 2a 2a 20 41 6e 6f 74  l idiom..** Anot
34011 68 65 72 20 6f 70 74 69 6f 6e 20 77 6f 75 6c 64  her option would
34012 20 62 65 20 66 6f 72 20 70 6c 77 41 64 64 28 29   be for plwAdd()
34013 20 74 6f 20 61 6c 77 61 79 73 20 61 70 70 65 6e   to always appen
34014 64 20 61 6e 79 20 6e 65 63 65 73 73 61 72 79 0a  d any necessary.
34015 2a 2a 20 74 65 72 6d 69 6e 61 74 6f 72 2c 20 73  ** terminator, s
34016 6f 20 74 68 61 74 20 74 68 65 20 6f 75 74 70 75  o that the outpu
34017 74 20 69 73 20 61 6c 77 61 79 73 20 63 6f 72 72  t is always corr
34018 65 63 74 2e 20 20 42 75 74 20 74 68 61 74 20 77  ect.  But that w
34019 6f 75 6c 64 0a 2a 2a 20 61 64 64 20 69 6e 63 72  ould.** add incr
3401a 65 6d 65 6e 74 61 6c 20 77 6f 72 6b 20 74 6f 20  emental work to 
3401b 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20  the common case 
3401c 77 69 74 68 20 74 68 65 20 6f 6e 6c 79 20 62 65  with the only be
3401d 6e 65 66 69 74 20 62 65 69 6e 67 0a 2a 2a 20 41  nefit being.** A
3401e 50 49 20 65 6c 65 67 61 6e 63 65 2e 20 20 50 75  PI elegance.  Pu
3401f 6e 74 20 66 6f 72 20 6e 6f 77 2e 0a 2a 2f 0a 73  nt for now..*/.s
34020 74 61 74 69 63 20 76 6f 69 64 20 70 6c 77 54 65  tatic void plwTe
34021 72 6d 69 6e 61 74 65 28 50 4c 57 72 69 74 65 72  rminate(PLWriter
34022 20 2a 70 57 72 69 74 65 72 29 7b 0a 20 20 69 66   *pWriter){.  if
34023 28 20 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e  ( pWriter->dlw->
34024 69 54 79 70 65 3e 44 4c 5f 44 4f 43 49 44 53 20  iType>DL_DOCIDS 
34025 29 7b 0a 20 20 20 20 63 68 61 72 20 63 5b 56 41  ){.    char c[VA
34026 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 20 20 69  RINT_MAX];.    i
34027 6e 74 20 6e 20 3d 20 66 74 73 33 50 75 74 56 61  nt n = fts3PutVa
34028 72 69 6e 74 28 63 2c 20 50 4f 53 5f 45 4e 44 29  rint(c, POS_END)
34029 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72  ;.    dataBuffer
3402a 41 70 70 65 6e 64 28 70 57 72 69 74 65 72 2d 3e  Append(pWriter->
3402b 64 6c 77 2d 3e 62 2c 20 63 2c 20 6e 29 3b 0a 20  dlw->b, c, n);. 
3402c 20 7d 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55   }.#ifndef NDEBU
3402d 47 0a 20 20 2f 2a 20 4d 61 72 6b 20 61 73 20 74  G.  /* Mark as t
3402e 65 72 6d 69 6e 61 74 65 64 20 66 6f 72 20 61 73  erminated for as
3402f 73 65 72 74 20 69 6e 20 70 6c 77 41 64 64 28 29  sert in plwAdd()
34030 2e 20 2a 2f 0a 20 20 70 57 72 69 74 65 72 2d 3e  . */.  pWriter->
34031 69 50 6f 73 20 3d 20 2d 31 3b 0a 23 65 6e 64 69  iPos = -1;.#endi
34032 66 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  f.}.static void 
34033 70 6c 77 44 65 73 74 72 6f 79 28 50 4c 57 72 69  plwDestroy(PLWri
34034 74 65 72 20 2a 70 57 72 69 74 65 72 29 7b 0a 20  ter *pWriter){. 
34035 20 53 43 52 41 4d 42 4c 45 28 70 57 72 69 74 65   SCRAMBLE(pWrite
34036 72 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  r);.}../********
34037 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34038 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34039 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3403a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20  ***********/./* 
3403b 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 77 72 61 70  DLCollector wrap
3403c 73 20 50 4c 57 72 69 74 65 72 20 61 6e 64 20 44  s PLWriter and D
3403d 4c 57 72 69 74 65 72 20 74 6f 20 70 72 6f 76 69  LWriter to provi
3403e 64 65 20 61 0a 2a 2a 20 64 79 6e 61 6d 69 63 61  de a.** dynamica
3403f 6c 6c 79 2d 61 6c 6c 6f 63 61 74 65 64 20 64 6f  lly-allocated do
34040 63 6c 69 73 74 20 61 72 65 61 20 74 6f 20 75 73  clist area to us
34041 65 20 64 75 72 69 6e 67 20 74 6f 6b 65 6e 69 7a  e during tokeniz
34042 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 64 6c 63  ation..**.** dlc
34043 4e 65 77 20 2d 20 6d 61 6c 6c 6f 63 20 75 70 20  New - malloc up 
34044 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 61  and initialize a
34045 20 63 6f 6c 6c 65 63 74 6f 72 2e 0a 2a 2a 20 64   collector..** d
34046 6c 63 44 65 6c 65 74 65 20 2d 20 64 65 73 74 72  lcDelete - destr
34047 6f 79 20 61 20 63 6f 6c 6c 65 63 74 6f 72 20 61  oy a collector a
34048 6e 64 20 61 6c 6c 20 63 6f 6e 74 61 69 6e 65 64  nd all contained
34049 20 69 74 65 6d 73 2e 0a 2a 2a 20 64 6c 63 41 64   items..** dlcAd
3404a 64 50 6f 73 20 2d 20 61 70 70 65 6e 64 20 70 6f  dPos - append po
3404b 73 69 74 69 6f 6e 20 61 6e 64 20 6f 66 66 73 65  sition and offse
3404c 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a  t information..*
3404d 2a 20 64 6c 63 41 64 64 44 6f 63 6c 69 73 74 20  * dlcAddDoclist 
3404e 2d 20 61 64 64 20 74 68 65 20 63 6f 6c 6c 65 63  - add the collec
3404f 74 65 64 20 64 6f 63 6c 69 73 74 20 74 6f 20 74  ted doclist to t
34050 68 65 20 67 69 76 65 6e 20 62 75 66 66 65 72 2e  he given buffer.
34051 0a 2a 2a 20 64 6c 63 4e 65 78 74 20 2d 20 74 65  .** dlcNext - te
34052 72 6d 69 6e 61 74 65 20 74 68 65 20 63 75 72 72  rminate the curr
34053 65 6e 74 20 64 6f 63 75 6d 65 6e 74 20 61 6e 64  ent document and
34054 20 6f 70 65 6e 20 61 6e 6f 74 68 65 72 2e 0a 2a   open another..*
34055 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
34056 20 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 7b 0a 20   DLCollector {. 
34057 20 44 61 74 61 42 75 66 66 65 72 20 62 3b 0a 20   DataBuffer b;. 
34058 20 44 4c 57 72 69 74 65 72 20 64 6c 77 3b 0a 20   DLWriter dlw;. 
34059 20 50 4c 57 72 69 74 65 72 20 70 6c 77 3b 0a 7d   PLWriter plw;.}
3405a 20 44 4c 43 6f 6c 6c 65 63 74 6f 72 3b 0a 0a 2f   DLCollector;../
3405b 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 54 68  * TODO(shess) Th
3405c 69 73 20 63 6f 75 6c 64 20 61 6c 73 6f 20 62 65  is could also be
3405d 20 64 6f 6e 65 20 62 79 20 63 61 6c 6c 69 6e 67   done by calling
3405e 20 70 6c 77 54 65 72 6d 69 6e 61 74 65 28 29 20   plwTerminate() 
3405f 61 6e 64 0a 2a 2a 20 64 61 74 61 42 75 66 66 65  and.** dataBuffe
34060 72 41 70 70 65 6e 64 28 29 2e 20 20 49 20 74 72  rAppend().  I tr
34061 69 65 64 20 74 68 61 74 2c 20 65 78 70 65 63 74  ied that, expect
34062 69 6e 67 20 6e 6f 6d 69 6e 61 6c 20 70 65 72 66  ing nominal perf
34063 6f 72 6d 61 6e 63 65 0a 2a 2a 20 64 69 66 66 65  ormance.** diffe
34064 72 65 6e 63 65 73 2c 20 62 75 74 20 69 74 20 73  rences, but it s
34065 65 65 6d 65 64 20 74 6f 20 70 72 65 74 74 79 20  eemed to pretty 
34066 72 65 6c 69 61 62 6c 79 20 62 65 20 77 6f 72 74  reliably be wort
34067 68 20 31 25 20 74 6f 20 63 6f 64 65 0a 2a 2a 20  h 1% to code.** 
34068 69 74 20 74 68 69 73 20 77 61 79 2e 20 20 49 20  it this way.  I 
34069 73 75 73 70 65 63 74 20 69 74 20 69 73 20 74 68  suspect it is th
3406a 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 6d 61  e incremental ma
3406b 6c 6c 6f 63 20 6f 76 65 72 68 65 61 64 20 28 73  lloc overhead (s
3406c 6f 6d 65 0a 2a 2a 20 70 65 72 63 65 6e 74 61 67  ome.** percentag
3406d 65 20 6f 66 20 74 68 65 20 70 6c 77 54 65 72 6d  e of the plwTerm
3406e 69 6e 61 74 65 28 29 20 63 61 6c 6c 73 20 77 69  inate() calls wi
3406f 6c 6c 20 63 61 75 73 65 20 61 20 72 65 61 6c 6c  ll cause a reall
34070 6f 63 29 2c 20 73 6f 0a 2a 2a 20 74 68 69 73 20  oc), so.** this 
34071 6d 69 67 68 74 20 62 65 20 77 6f 72 74 68 20 72  might be worth r
34072 65 76 69 73 69 74 69 6e 67 20 69 66 20 74 68 65  evisiting if the
34073 20 44 61 74 61 42 75 66 66 65 72 20 69 6d 70 6c   DataBuffer impl
34074 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 63 68  ementation.** ch
34075 61 6e 67 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  anges..*/.static
34076 20 76 6f 69 64 20 64 6c 63 41 64 64 44 6f 63 6c   void dlcAddDocl
34077 69 73 74 28 44 4c 43 6f 6c 6c 65 63 74 6f 72 20  ist(DLCollector 
34078 2a 70 43 6f 6c 6c 65 63 74 6f 72 2c 20 44 61 74  *pCollector, Dat
34079 61 42 75 66 66 65 72 20 2a 62 29 7b 0a 20 20 69  aBuffer *b){.  i
3407a 66 28 20 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 64  f( pCollector->d
3407b 6c 77 2e 69 54 79 70 65 3e 44 4c 5f 44 4f 43 49  lw.iType>DL_DOCI
3407c 44 53 20 29 7b 0a 20 20 20 20 63 68 61 72 20 63  DS ){.    char c
3407d 5b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20  [VARINT_MAX];.  
3407e 20 20 69 6e 74 20 6e 20 3d 20 66 74 73 33 50 75    int n = fts3Pu
3407f 74 56 61 72 69 6e 74 28 63 2c 20 50 4f 53 5f 45  tVarint(c, POS_E
34080 4e 44 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66  ND);.    dataBuf
34081 66 65 72 41 70 70 65 6e 64 32 28 62 2c 20 70 43  ferAppend2(b, pC
34082 6f 6c 6c 65 63 74 6f 72 2d 3e 62 2e 70 44 61 74  ollector->b.pDat
34083 61 2c 20 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 62  a, pCollector->b
34084 2e 6e 44 61 74 61 2c 20 63 2c 20 6e 29 3b 0a 20  .nData, c, n);. 
34085 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 61 74 61   }else{.    data
34086 42 75 66 66 65 72 41 70 70 65 6e 64 28 62 2c 20  BufferAppend(b, 
34087 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 62 2e 70 44  pCollector->b.pD
34088 61 74 61 2c 20 70 43 6f 6c 6c 65 63 74 6f 72 2d  ata, pCollector-
34089 3e 62 2e 6e 44 61 74 61 29 3b 0a 20 20 7d 0a 7d  >b.nData);.  }.}
3408a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 6c 63  .static void dlc
3408b 4e 65 78 74 28 44 4c 43 6f 6c 6c 65 63 74 6f 72  Next(DLCollector
3408c 20 2a 70 43 6f 6c 6c 65 63 74 6f 72 2c 20 73 71   *pCollector, sq
3408d 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63 69  lite_int64 iDoci
3408e 64 29 7b 0a 20 20 70 6c 77 54 65 72 6d 69 6e 61  d){.  plwTermina
3408f 74 65 28 26 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e  te(&pCollector->
34090 70 6c 77 29 3b 0a 20 20 70 6c 77 44 65 73 74 72  plw);.  plwDestr
34091 6f 79 28 26 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e  oy(&pCollector->
34092 70 6c 77 29 3b 0a 20 20 70 6c 77 49 6e 69 74 28  plw);.  plwInit(
34093 26 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 70 6c 77  &pCollector->plw
34094 2c 20 26 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 64  , &pCollector->d
34095 6c 77 2c 20 69 44 6f 63 69 64 29 3b 0a 7d 0a 73  lw, iDocid);.}.s
34096 74 61 74 69 63 20 76 6f 69 64 20 64 6c 63 41 64  tatic void dlcAd
34097 64 50 6f 73 28 44 4c 43 6f 6c 6c 65 63 74 6f 72  dPos(DLCollector
34098 20 2a 70 43 6f 6c 6c 65 63 74 6f 72 2c 20 69 6e   *pCollector, in
34099 74 20 69 43 6f 6c 75 6d 6e 2c 20 69 6e 74 20 69  t iColumn, int i
3409a 50 6f 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Pos,.           
3409b 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69             int i
3409c 53 74 61 72 74 4f 66 66 73 65 74 2c 20 69 6e 74  StartOffset, int
3409d 20 69 45 6e 64 4f 66 66 73 65 74 29 7b 0a 20 20   iEndOffset){.  
3409e 70 6c 77 41 64 64 28 26 70 43 6f 6c 6c 65 63 74  plwAdd(&pCollect
3409f 6f 72 2d 3e 70 6c 77 2c 20 69 43 6f 6c 75 6d 6e  or->plw, iColumn
340a0 2c 20 69 50 6f 73 2c 20 69 53 74 61 72 74 4f 66  , iPos, iStartOf
340a1 66 73 65 74 2c 20 69 45 6e 64 4f 66 66 73 65 74  fset, iEndOffset
340a2 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 44 4c 43  );.}..static DLC
340a3 6f 6c 6c 65 63 74 6f 72 20 2a 64 6c 63 4e 65 77  ollector *dlcNew
340a4 28 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44  (sqlite_int64 iD
340a5 6f 63 69 64 2c 20 44 6f 63 4c 69 73 74 54 79 70  ocid, DocListTyp
340a6 65 20 69 54 79 70 65 29 7b 0a 20 20 44 4c 43 6f  e iType){.  DLCo
340a7 6c 6c 65 63 74 6f 72 20 2a 70 43 6f 6c 6c 65 63  llector *pCollec
340a8 74 6f 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  tor = sqlite3_ma
340a9 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 44 4c 43 6f  lloc(sizeof(DLCo
340aa 6c 6c 65 63 74 6f 72 29 29 3b 0a 20 20 64 61 74  llector));.  dat
340ab 61 42 75 66 66 65 72 49 6e 69 74 28 26 70 43 6f  aBufferInit(&pCo
340ac 6c 6c 65 63 74 6f 72 2d 3e 62 2c 20 30 29 3b 0a  llector->b, 0);.
340ad 20 20 64 6c 77 49 6e 69 74 28 26 70 43 6f 6c 6c    dlwInit(&pColl
340ae 65 63 74 6f 72 2d 3e 64 6c 77 2c 20 69 54 79 70  ector->dlw, iTyp
340af 65 2c 20 26 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e  e, &pCollector->
340b0 62 29 3b 0a 20 20 70 6c 77 49 6e 69 74 28 26 70  b);.  plwInit(&p
340b1 43 6f 6c 6c 65 63 74 6f 72 2d 3e 70 6c 77 2c 20  Collector->plw, 
340b2 26 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 64 6c 77  &pCollector->dlw
340b3 2c 20 69 44 6f 63 69 64 29 3b 0a 20 20 72 65 74  , iDocid);.  ret
340b4 75 72 6e 20 70 43 6f 6c 6c 65 63 74 6f 72 3b 0a  urn pCollector;.
340b5 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 6c  }.static void dl
340b6 63 44 65 6c 65 74 65 28 44 4c 43 6f 6c 6c 65 63  cDelete(DLCollec
340b7 74 6f 72 20 2a 70 43 6f 6c 6c 65 63 74 6f 72 29  tor *pCollector)
340b8 7b 0a 20 20 70 6c 77 44 65 73 74 72 6f 79 28 26  {.  plwDestroy(&
340b9 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 70 6c 77 29  pCollector->plw)
340ba 3b 0a 20 20 64 6c 77 44 65 73 74 72 6f 79 28 26  ;.  dlwDestroy(&
340bb 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 64 6c 77 29  pCollector->dlw)
340bc 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65  ;.  dataBufferDe
340bd 73 74 72 6f 79 28 26 70 43 6f 6c 6c 65 63 74 6f  stroy(&pCollecto
340be 72 2d 3e 62 29 3b 0a 20 20 53 43 52 41 4d 42 4c  r->b);.  SCRAMBL
340bf 45 28 70 43 6f 6c 6c 65 63 74 6f 72 29 3b 0a 20  E(pCollector);. 
340c0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43   sqlite3_free(pC
340c1 6f 6c 6c 65 63 74 6f 72 29 3b 0a 7d 0a 0a 0a 2f  ollector);.}.../
340c2 2a 20 43 6f 70 79 20 74 68 65 20 64 6f 63 6c 69  * Copy the docli
340c3 73 74 20 64 61 74 61 20 6f 66 20 69 54 79 70 65  st data of iType
340c4 20 69 6e 20 70 44 61 74 61 2f 6e 44 61 74 61 20   in pData/nData 
340c5 69 6e 74 6f 20 2a 6f 75 74 2c 20 74 72 69 6d 6d  into *out, trimm
340c6 69 6e 67 0a 2a 2a 20 75 6e 6e 65 63 65 73 73 61  ing.** unnecessa
340c7 72 79 20 64 61 74 61 20 61 73 20 77 65 20 67 6f  ry data as we go
340c8 2e 20 20 4f 6e 6c 79 20 63 6f 6c 75 6d 6e 73 20  .  Only columns 
340c9 6d 61 74 63 68 69 6e 67 20 69 43 6f 6c 75 6d 6e  matching iColumn
340ca 20 61 72 65 0a 2a 2a 20 63 6f 70 69 65 64 2c 20   are.** copied, 
340cb 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 63 6f 70 69  all columns copi
340cc 65 64 20 69 66 20 69 43 6f 6c 75 6d 6e 20 69 73  ed if iColumn is
340cd 20 2d 31 2e 20 20 45 6c 65 6d 65 6e 74 73 20 77   -1.  Elements w
340ce 69 74 68 20 6e 6f 0a 2a 2a 20 6d 61 74 63 68 69  ith no.** matchi
340cf 6e 67 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 64  ng columns are d
340d0 72 6f 70 70 65 64 2e 20 20 54 68 65 20 6f 75 74  ropped.  The out
340d1 70 75 74 20 69 73 20 61 6e 20 69 4f 75 74 54 79  put is an iOutTy
340d2 70 65 20 64 6f 63 6c 69 73 74 2e 0a 2a 2f 0a 2f  pe doclist..*/./
340d3 2a 20 4e 4f 54 45 28 73 68 65 73 73 29 20 54 68  * NOTE(shess) Th
340d4 69 73 20 63 6f 64 65 20 69 73 20 6f 6e 6c 79 20  is code is only 
340d5 76 61 6c 69 64 20 61 66 74 65 72 20 61 6c 6c 20  valid after all 
340d6 64 6f 63 6c 69 73 74 73 20 61 72 65 20 6d 65 72  doclists are mer
340d7 67 65 64 2e 0a 2a 2a 20 49 66 20 74 68 69 73 20  ged..** If this 
340d8 69 73 20 72 75 6e 20 62 65 66 6f 72 65 20 6d 65  is run before me
340d9 72 67 65 73 2c 20 74 68 65 6e 20 64 6f 63 6c 69  rges, then docli
340da 73 74 20 69 74 65 6d 73 20 77 68 69 63 68 20 72  st items which r
340db 65 70 72 65 73 65 6e 74 0a 2a 2a 20 64 65 6c 65  epresent.** dele
340dc 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 74 72 69  tion will be tri
340dd 6d 6d 65 64 2c 20 61 6e 64 20 77 69 6c 6c 20 74  mmed, and will t
340de 68 75 73 20 6e 6f 74 20 65 66 66 65 63 74 20 61  hus not effect a
340df 20 64 65 6c 65 74 69 6f 6e 0a 2a 2a 20 64 75 72   deletion.** dur
340e0 69 6e 67 20 74 68 65 20 6d 65 72 67 65 2e 0a 2a  ing the merge..*
340e1 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 6f  /.static void do
340e2 63 4c 69 73 74 54 72 69 6d 28 44 6f 63 4c 69 73  cListTrim(DocLis
340e3 74 54 79 70 65 20 69 54 79 70 65 2c 20 63 6f 6e  tType iType, con
340e4 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20  st char *pData, 
340e5 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20 20 20  int nData,.     
340e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
340e7 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20     int iColumn, 
340e8 44 6f 63 4c 69 73 74 54 79 70 65 20 69 4f 75 74  DocListType iOut
340e9 54 79 70 65 2c 20 44 61 74 61 42 75 66 66 65 72  Type, DataBuffer
340ea 20 2a 6f 75 74 29 7b 0a 20 20 44 4c 52 65 61 64   *out){.  DLRead
340eb 65 72 20 64 6c 52 65 61 64 65 72 3b 0a 20 20 44  er dlReader;.  D
340ec 4c 57 72 69 74 65 72 20 64 6c 57 72 69 74 65 72  LWriter dlWriter
340ed 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 4f 75  ;..  assert( iOu
340ee 74 54 79 70 65 3c 3d 69 54 79 70 65 20 29 3b 0a  tType<=iType );.
340ef 0a 20 20 64 6c 72 49 6e 69 74 28 26 64 6c 52 65  .  dlrInit(&dlRe
340f0 61 64 65 72 2c 20 69 54 79 70 65 2c 20 70 44 61  ader, iType, pDa
340f1 74 61 2c 20 6e 44 61 74 61 29 3b 0a 20 20 64 6c  ta, nData);.  dl
340f2 77 49 6e 69 74 28 26 64 6c 57 72 69 74 65 72 2c  wInit(&dlWriter,
340f3 20 69 4f 75 74 54 79 70 65 2c 20 6f 75 74 29 3b   iOutType, out);
340f4 0a 0a 20 20 77 68 69 6c 65 28 20 21 64 6c 72 41  ..  while( !dlrA
340f5 74 45 6e 64 28 26 64 6c 52 65 61 64 65 72 29 20  tEnd(&dlReader) 
340f6 29 7b 0a 20 20 20 20 50 4c 52 65 61 64 65 72 20  ){.    PLReader 
340f7 70 6c 52 65 61 64 65 72 3b 0a 20 20 20 20 50 4c  plReader;.    PL
340f8 57 72 69 74 65 72 20 70 6c 57 72 69 74 65 72 3b  Writer plWriter;
340f9 0a 20 20 20 20 69 6e 74 20 6d 61 74 63 68 20 3d  .    int match =
340fa 20 30 3b 0a 0a 20 20 20 20 70 6c 72 49 6e 69 74   0;..    plrInit
340fb 28 26 70 6c 52 65 61 64 65 72 2c 20 26 64 6c 52  (&plReader, &dlR
340fc 65 61 64 65 72 29 3b 0a 0a 20 20 20 20 77 68 69  eader);..    whi
340fd 6c 65 28 20 21 70 6c 72 41 74 45 6e 64 28 26 70  le( !plrAtEnd(&p
340fe 6c 52 65 61 64 65 72 29 20 29 7b 0a 20 20 20 20  lReader) ){.    
340ff 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3d 3d 2d    if( iColumn==-
34100 31 20 7c 7c 20 70 6c 72 43 6f 6c 75 6d 6e 28 26  1 || plrColumn(&
34101 70 6c 52 65 61 64 65 72 29 3d 3d 69 43 6f 6c 75  plReader)==iColu
34102 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  mn ){.        if
34103 28 20 21 6d 61 74 63 68 20 29 7b 0a 20 20 20 20  ( !match ){.    
34104 20 20 20 20 20 20 70 6c 77 49 6e 69 74 28 26 70        plwInit(&p
34105 6c 57 72 69 74 65 72 2c 20 26 64 6c 57 72 69 74  lWriter, &dlWrit
34106 65 72 2c 20 64 6c 72 44 6f 63 69 64 28 26 64 6c  er, dlrDocid(&dl
34107 52 65 61 64 65 72 29 29 3b 0a 20 20 20 20 20 20  Reader));.      
34108 20 20 20 20 6d 61 74 63 68 20 3d 20 31 3b 0a 20      match = 1;. 
34109 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
3410a 20 70 6c 77 41 64 64 28 26 70 6c 57 72 69 74 65   plwAdd(&plWrite
3410b 72 2c 20 70 6c 72 43 6f 6c 75 6d 6e 28 26 70 6c  r, plrColumn(&pl
3410c 52 65 61 64 65 72 29 2c 20 70 6c 72 50 6f 73 69  Reader), plrPosi
3410d 74 69 6f 6e 28 26 70 6c 52 65 61 64 65 72 29 2c  tion(&plReader),
3410e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3410f 70 6c 72 53 74 61 72 74 4f 66 66 73 65 74 28 26  plrStartOffset(&
34110 70 6c 52 65 61 64 65 72 29 2c 20 70 6c 72 45 6e  plReader), plrEn
34111 64 4f 66 66 73 65 74 28 26 70 6c 52 65 61 64 65  dOffset(&plReade
34112 72 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  r));.      }.   
34113 20 20 20 70 6c 72 53 74 65 70 28 26 70 6c 52 65     plrStep(&plRe
34114 61 64 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20  ader);.    }.   
34115 20 69 66 28 20 6d 61 74 63 68 20 29 7b 0a 20 20   if( match ){.  
34116 20 20 20 20 70 6c 77 54 65 72 6d 69 6e 61 74 65      plwTerminate
34117 28 26 70 6c 57 72 69 74 65 72 29 3b 0a 20 20 20  (&plWriter);.   
34118 20 20 20 70 6c 77 44 65 73 74 72 6f 79 28 26 70     plwDestroy(&p
34119 6c 57 72 69 74 65 72 29 3b 0a 20 20 20 20 7d 0a  lWriter);.    }.
3411a 0a 20 20 20 20 70 6c 72 44 65 73 74 72 6f 79 28  .    plrDestroy(
3411b 26 70 6c 52 65 61 64 65 72 29 3b 0a 20 20 20 20  &plReader);.    
3411c 64 6c 72 53 74 65 70 28 26 64 6c 52 65 61 64 65  dlrStep(&dlReade
3411d 72 29 3b 0a 20 20 7d 0a 20 20 64 6c 77 44 65 73  r);.  }.  dlwDes
3411e 74 72 6f 79 28 26 64 6c 57 72 69 74 65 72 29 3b  troy(&dlWriter);
3411f 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26 64  .  dlrDestroy(&d
34120 6c 52 65 61 64 65 72 29 3b 0a 7d 0a 0a 2f 2a 20  lReader);.}../* 
34121 55 73 65 64 20 62 79 20 64 6f 63 4c 69 73 74 4d  Used by docListM
34122 65 72 67 65 28 29 20 74 6f 20 6b 65 65 70 20 64  erge() to keep d
34123 6f 63 6c 69 73 74 73 20 69 6e 20 74 68 65 20 61  oclists in the a
34124 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62  scending order b
34125 79 0a 2a 2a 20 64 6f 63 69 64 2c 20 74 68 65 6e  y.** docid, then
34126 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72   ascending order
34127 20 62 79 20 61 67 65 20 28 73 6f 20 74 68 65 20   by age (so the 
34128 6e 65 77 65 73 74 20 63 6f 6d 65 73 20 66 69 72  newest comes fir
34129 73 74 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  st)..*/.typedef 
3412a 73 74 72 75 63 74 20 4f 72 64 65 72 65 64 44 4c  struct OrderedDL
3412b 52 65 61 64 65 72 20 7b 0a 20 20 44 4c 52 65 61  Reader {.  DLRea
3412c 64 65 72 20 2a 70 52 65 61 64 65 72 3b 0a 0a 20  der *pReader;.. 
3412d 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20   /* TODO(shess) 
3412e 49 66 20 77 65 20 61 73 73 75 6d 65 20 74 68 61  If we assume tha
3412f 74 20 64 6f 63 4c 69 73 74 4d 65 72 67 65 20 70  t docListMerge p
34130 52 65 61 64 65 72 73 20 69 73 20 6f 72 64 65 72  Readers is order
34131 65 64 20 62 79 0a 20 20 2a 2a 20 61 67 65 20 28  ed by.  ** age (
34132 77 68 69 63 68 20 77 65 20 64 6f 29 2c 20 74 68  which we do), th
34133 65 6e 20 77 65 20 63 6f 75 6c 64 20 75 73 65 20  en we could use 
34134 70 52 65 61 64 65 72 20 63 6f 6d 70 61 72 69 73  pReader comparis
34135 6f 6e 73 20 74 6f 20 62 72 65 61 6b 0a 20 20 2a  ons to break.  *
34136 2a 20 74 69 65 73 2e 0a 20 20 2a 2f 0a 20 20 69  * ties..  */.  i
34137 6e 74 20 69 64 78 3b 0a 7d 20 4f 72 64 65 72 65  nt idx;.} Ordere
34138 64 44 4c 52 65 61 64 65 72 3b 0a 0a 2f 2a 20 4f  dDLReader;../* O
34139 72 64 65 72 20 65 6f 66 20 74 6f 20 65 6e 64 2c  rder eof to end,
3413a 20 74 68 65 6e 20 62 79 20 64 6f 63 69 64 20 61   then by docid a
3413b 73 63 2c 20 69 64 78 20 64 65 73 63 2e 20 2a 2f  sc, idx desc. */
3413c 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 72 64 65  .static int orde
3413d 72 65 64 44 4c 52 65 61 64 65 72 43 6d 70 28 4f  redDLReaderCmp(O
3413e 72 64 65 72 65 64 44 4c 52 65 61 64 65 72 20 2a  rderedDLReader *
3413f 72 31 2c 20 4f 72 64 65 72 65 64 44 4c 52 65 61  r1, OrderedDLRea
34140 64 65 72 20 2a 72 32 29 7b 0a 20 20 69 66 28 20  der *r2){.  if( 
34141 64 6c 72 41 74 45 6e 64 28 72 31 2d 3e 70 52 65  dlrAtEnd(r1->pRe
34142 61 64 65 72 29 20 29 7b 0a 20 20 20 20 69 66 28  ader) ){.    if(
34143 20 64 6c 72 41 74 45 6e 64 28 72 32 2d 3e 70 52   dlrAtEnd(r2->pR
34144 65 61 64 65 72 29 20 29 20 72 65 74 75 72 6e 20  eader) ) return 
34145 30 3b 20 20 2f 2a 20 42 6f 74 68 20 61 74 45 6e  0;  /* Both atEn
34146 64 28 29 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75  d(). */.    retu
34147 72 6e 20 31 3b 20 20 20 20 20 20 20 20 20 20 20  rn 1;           
34148 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34149 20 20 20 2f 2a 20 4f 6e 6c 79 20 72 31 20 61 74     /* Only r1 at
3414a 45 6e 64 28 29 2e 20 2a 2f 0a 20 20 7d 0a 20 20  End(). */.  }.  
3414b 69 66 28 20 64 6c 72 41 74 45 6e 64 28 72 32 2d  if( dlrAtEnd(r2-
3414c 3e 70 52 65 61 64 65 72 29 20 29 20 72 65 74 75  >pReader) ) retu
3414d 72 6e 20 2d 31 3b 20 20 20 2f 2a 20 4f 6e 6c 79  rn -1;   /* Only
3414e 20 72 32 20 61 74 45 6e 64 28 29 2e 20 2a 2f 0a   r2 atEnd(). */.
3414f 0a 20 20 69 66 28 20 64 6c 72 44 6f 63 69 64 28  .  if( dlrDocid(
34150 72 31 2d 3e 70 52 65 61 64 65 72 29 3c 64 6c 72  r1->pReader)<dlr
34151 44 6f 63 69 64 28 72 32 2d 3e 70 52 65 61 64 65  Docid(r2->pReade
34152 72 29 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a  r) ) return -1;.
34153 20 20 69 66 28 20 64 6c 72 44 6f 63 69 64 28 72    if( dlrDocid(r
34154 31 2d 3e 70 52 65 61 64 65 72 29 3e 64 6c 72 44  1->pReader)>dlrD
34155 6f 63 69 64 28 72 32 2d 3e 70 52 65 61 64 65 72  ocid(r2->pReader
34156 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 0a 20  ) ) return 1;.. 
34157 20 2f 2a 20 44 65 73 63 65 6e 64 69 6e 67 20 6f   /* Descending o
34158 6e 20 69 64 78 2e 20 2a 2f 0a 20 20 72 65 74 75  n idx. */.  retu
34159 72 6e 20 72 32 2d 3e 69 64 78 2d 72 31 2d 3e 69  rn r2->idx-r1->i
3415a 64 78 3b 0a 7d 0a 0a 2f 2a 20 42 75 62 62 6c 65  dx;.}../* Bubble
3415b 20 70 5b 30 5d 20 74 6f 20 61 70 70 72 6f 70 72   p[0] to appropr
3415c 69 61 74 65 20 70 6c 61 63 65 20 69 6e 20 70 5b  iate place in p[
3415d 31 2e 2e 6e 2d 31 5d 2e 20 20 41 73 73 75 6d 65  1..n-1].  Assume
3415e 73 20 74 68 61 74 0a 2a 2a 20 70 5b 31 2e 2e 6e  s that.** p[1..n
3415f 2d 31 5d 20 69 73 20 61 6c 72 65 61 64 79 20 73  -1] is already s
34160 6f 72 74 65 64 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44  orted..*/./* TOD
34161 4f 28 73 68 65 73 73 29 20 49 73 20 74 68 69 73  O(shess) Is this
34162 20 66 72 65 71 75 65 6e 74 20 65 6e 6f 75 67 68   frequent enough
34163 20 74 6f 20 77 61 72 72 61 6e 74 20 61 20 62 69   to warrant a bi
34164 6e 61 72 79 20 73 65 61 72 63 68 3f 0a 2a 2a 20  nary search?.** 
34165 42 65 66 6f 72 65 20 69 6d 70 6c 65 6d 65 6e 74  Before implement
34166 69 6e 67 20 74 68 61 74 2c 20 69 6e 73 74 72 75  ing that, instru
34167 6d 65 6e 74 20 74 68 65 20 63 6f 64 65 20 74 6f  ment the code to
34168 20 63 68 65 63 6b 2e 20 20 49 6e 20 6d 6f 73 74   check.  In most
34169 0a 2a 2a 20 63 75 72 72 65 6e 74 20 75 73 61 67  .** current usag
3416a 65 2c 20 49 20 65 78 70 65 63 74 20 74 68 61 74  e, I expect that
3416b 20 70 5b 30 5d 20 77 69 6c 6c 20 62 65 20 6c 65   p[0] will be le
3416c 73 73 20 74 68 61 6e 20 70 5b 31 5d 20 61 20 76  ss than p[1] a v
3416d 65 72 79 0a 2a 2a 20 68 69 67 68 20 70 72 6f 70  ery.** high prop
3416e 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 74 69  ortion of the ti
3416f 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  me..*/.static vo
34170 69 64 20 6f 72 64 65 72 65 64 44 4c 52 65 61 64  id orderedDLRead
34171 65 72 52 65 6f 72 64 65 72 28 4f 72 64 65 72 65  erReorder(Ordere
34172 64 44 4c 52 65 61 64 65 72 20 2a 70 2c 20 69 6e  dDLReader *p, in
34173 74 20 6e 29 7b 0a 20 20 77 68 69 6c 65 28 20 6e  t n){.  while( n
34174 3e 31 20 26 26 20 6f 72 64 65 72 65 64 44 4c 52  >1 && orderedDLR
34175 65 61 64 65 72 43 6d 70 28 70 2c 20 70 2b 31 29  eaderCmp(p, p+1)
34176 3e 30 20 29 7b 0a 20 20 20 20 4f 72 64 65 72 65  >0 ){.    Ordere
34177 64 44 4c 52 65 61 64 65 72 20 74 6d 70 20 3d 20  dDLReader tmp = 
34178 70 5b 30 5d 3b 0a 20 20 20 20 70 5b 30 5d 20 3d  p[0];.    p[0] =
34179 20 70 5b 31 5d 3b 0a 20 20 20 20 70 5b 31 5d 20   p[1];.    p[1] 
3417a 3d 20 74 6d 70 3b 0a 20 20 20 20 6e 2d 2d 3b 0a  = tmp;.    n--;.
3417b 20 20 20 20 70 2b 2b 3b 0a 20 20 7d 0a 7d 0a 0a      p++;.  }.}..
3417c 2f 2a 20 47 69 76 65 6e 20 61 6e 20 61 72 72 61  /* Given an arra
3417d 79 20 6f 66 20 64 6f 63 6c 69 73 74 20 72 65 61  y of doclist rea
3417e 64 65 72 73 2c 20 6d 65 72 67 65 20 74 68 65 69  ders, merge thei
3417f 72 20 64 6f 63 6c 69 73 74 20 65 6c 65 6d 65 6e  r doclist elemen
34180 74 73 0a 2a 2a 20 69 6e 74 6f 20 6f 75 74 20 69  ts.** into out i
34181 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 20 28  n sorted order (
34182 62 79 20 64 6f 63 69 64 29 2c 20 64 72 6f 70 70  by docid), dropp
34183 69 6e 67 20 65 6c 65 6d 65 6e 74 73 20 66 72 6f  ing elements fro
34184 6d 20 6f 6c 64 65 72 0a 2a 2a 20 72 65 61 64 65  m older.** reade
34185 72 73 20 77 68 65 6e 20 74 68 65 72 65 20 69 73  rs when there is
34186 20 61 20 64 75 70 6c 69 63 61 74 65 20 64 6f 63   a duplicate doc
34187 69 64 2e 20 20 70 52 65 61 64 65 72 73 20 69 73  id.  pReaders is
34188 20 61 73 73 75 6d 65 64 20 74 6f 20 62 65 0a 2a   assumed to be.*
34189 2a 20 6f 72 64 65 72 65 64 20 62 79 20 61 67 65  * ordered by age
3418a 2c 20 6f 6c 64 65 73 74 20 66 69 72 73 74 2e 0a  , oldest first..
3418b 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73  */./* TODO(shess
3418c 29 20 6e 52 65 61 64 65 72 73 20 6d 75 73 74 20  ) nReaders must 
3418d 62 65 20 3c 3d 20 4d 45 52 47 45 5f 43 4f 55 4e  be <= MERGE_COUN
3418e 54 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64 20  T.  This should 
3418f 70 72 6f 62 61 62 6c 79 0a 2a 2a 20 62 65 20 66  probably.** be f
34190 69 78 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ixed..*/.static 
34191 76 6f 69 64 20 64 6f 63 4c 69 73 74 4d 65 72 67  void docListMerg
34192 65 28 44 61 74 61 42 75 66 66 65 72 20 2a 6f 75  e(DataBuffer *ou
34193 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t,.             
34194 20 20 20 20 20 20 20 20 20 20 20 20 44 4c 52 65              DLRe
34195 61 64 65 72 20 2a 70 52 65 61 64 65 72 73 2c 20  ader *pReaders, 
34196 69 6e 74 20 6e 52 65 61 64 65 72 73 29 7b 0a 20  int nReaders){. 
34197 20 4f 72 64 65 72 65 64 44 4c 52 65 61 64 65 72   OrderedDLReader
34198 20 72 65 61 64 65 72 73 5b 4d 45 52 47 45 5f 43   readers[MERGE_C
34199 4f 55 4e 54 5d 3b 0a 20 20 44 4c 57 72 69 74 65  OUNT];.  DLWrite
3419a 72 20 77 72 69 74 65 72 3b 0a 20 20 69 6e 74 20  r writer;.  int 
3419b 69 2c 20 6e 3b 0a 20 20 63 6f 6e 73 74 20 63 68  i, n;.  const ch
3419c 61 72 20 2a 70 53 74 61 72 74 20 3d 20 30 3b 0a  ar *pStart = 0;.
3419d 20 20 69 6e 74 20 6e 53 74 61 72 74 20 3d 20 30    int nStart = 0
3419e 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  ;.  sqlite_int64
3419f 20 69 46 69 72 73 74 44 6f 63 69 64 20 3d 20 30   iFirstDocid = 0
341a0 2c 20 69 4c 61 73 74 44 6f 63 69 64 20 3d 20 30  , iLastDocid = 0
341a1 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 52 65  ;..  assert( nRe
341a2 61 64 65 72 73 3e 30 20 29 3b 0a 20 20 69 66 28  aders>0 );.  if(
341a3 20 6e 52 65 61 64 65 72 73 3d 3d 31 20 29 7b 0a   nReaders==1 ){.
341a4 20 20 20 20 64 61 74 61 42 75 66 66 65 72 41 70      dataBufferAp
341a5 70 65 6e 64 28 6f 75 74 2c 20 64 6c 72 44 6f 63  pend(out, dlrDoc
341a6 44 61 74 61 28 70 52 65 61 64 65 72 73 29 2c 20  Data(pReaders), 
341a7 64 6c 72 41 6c 6c 44 61 74 61 42 79 74 65 73 28  dlrAllDataBytes(
341a8 70 52 65 61 64 65 72 73 29 29 3b 0a 20 20 20 20  pReaders));.    
341a9 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 61  return;.  }..  a
341aa 73 73 65 72 74 28 20 6e 52 65 61 64 65 72 73 3c  ssert( nReaders<
341ab 3d 4d 45 52 47 45 5f 43 4f 55 4e 54 20 29 3b 0a  =MERGE_COUNT );.
341ac 20 20 6e 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69    n = 0;.  for(i
341ad 3d 30 3b 20 69 3c 6e 52 65 61 64 65 72 73 3b 20  =0; i<nReaders; 
341ae 69 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74  i++){.    assert
341af 28 20 70 52 65 61 64 65 72 73 5b 69 5d 2e 69 54  ( pReaders[i].iT
341b0 79 70 65 3d 3d 70 52 65 61 64 65 72 73 5b 30 5d  ype==pReaders[0]
341b1 2e 69 54 79 70 65 20 29 3b 0a 20 20 20 20 72 65  .iType );.    re
341b2 61 64 65 72 73 5b 69 5d 2e 70 52 65 61 64 65 72  aders[i].pReader
341b3 20 3d 20 70 52 65 61 64 65 72 73 2b 69 3b 0a 20   = pReaders+i;. 
341b4 20 20 20 72 65 61 64 65 72 73 5b 69 5d 2e 69 64     readers[i].id
341b5 78 20 3d 20 69 3b 0a 20 20 20 20 6e 20 2b 3d 20  x = i;.    n += 
341b6 64 6c 72 41 6c 6c 44 61 74 61 42 79 74 65 73 28  dlrAllDataBytes(
341b7 26 70 52 65 61 64 65 72 73 5b 69 5d 29 3b 0a 20  &pReaders[i]);. 
341b8 20 7d 0a 20 20 2f 2a 20 43 6f 6e 73 65 72 76 61   }.  /* Conserva
341b9 74 69 76 65 6c 79 20 73 69 7a 65 20 6f 75 74 70  tively size outp
341ba 75 74 20 74 6f 20 73 75 6d 20 6f 66 20 69 6e 70  ut to sum of inp
341bb 75 74 73 2e 20 20 4f 75 74 70 75 74 20 73 68 6f  uts.  Output sho
341bc 75 6c 64 20 65 6e 64 0a 20 20 2a 2a 20 75 70 20  uld end.  ** up 
341bd 73 74 72 69 63 74 6c 79 20 73 6d 61 6c 6c 65 72  strictly smaller
341be 20 74 68 61 6e 20 69 6e 70 75 74 2e 0a 20 20 2a   than input..  *
341bf 2f 0a 20 20 64 61 74 61 42 75 66 66 65 72 45 78  /.  dataBufferEx
341c0 70 61 6e 64 28 6f 75 74 2c 20 6e 29 3b 0a 0a 20  pand(out, n);.. 
341c1 20 2f 2a 20 47 65 74 20 74 68 65 20 72 65 61 64   /* Get the read
341c2 65 72 73 20 69 6e 74 6f 20 73 6f 72 74 65 64 20  ers into sorted 
341c3 6f 72 64 65 72 2e 20 2a 2f 0a 20 20 77 68 69 6c  order. */.  whil
341c4 65 28 20 69 2d 2d 3e 30 20 29 7b 0a 20 20 20 20  e( i-->0 ){.    
341c5 6f 72 64 65 72 65 64 44 4c 52 65 61 64 65 72 52  orderedDLReaderR
341c6 65 6f 72 64 65 72 28 72 65 61 64 65 72 73 2b 69  eorder(readers+i
341c7 2c 20 6e 52 65 61 64 65 72 73 2d 69 29 3b 0a 20  , nReaders-i);. 
341c8 20 7d 0a 0a 20 20 64 6c 77 49 6e 69 74 28 26 77   }..  dlwInit(&w
341c9 72 69 74 65 72 2c 20 70 52 65 61 64 65 72 73 5b  riter, pReaders[
341ca 30 5d 2e 69 54 79 70 65 2c 20 6f 75 74 29 3b 0a  0].iType, out);.
341cb 20 20 77 68 69 6c 65 28 20 21 64 6c 72 41 74 45    while( !dlrAtE
341cc 6e 64 28 72 65 61 64 65 72 73 5b 30 5d 2e 70 52  nd(readers[0].pR
341cd 65 61 64 65 72 29 20 29 7b 0a 20 20 20 20 73 71  eader) ){.    sq
341ce 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63 69  lite_int64 iDoci
341cf 64 20 3d 20 64 6c 72 44 6f 63 69 64 28 72 65 61  d = dlrDocid(rea
341d0 64 65 72 73 5b 30 5d 2e 70 52 65 61 64 65 72 29  ders[0].pReader)
341d1 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69  ;..    /* If thi
341d2 73 20 69 73 20 61 20 63 6f 6e 74 69 6e 75 61 74  s is a continuat
341d3 69 6f 6e 20 6f 66 20 74 68 65 20 63 75 72 72 65  ion of the curre
341d4 6e 74 20 62 75 66 66 65 72 20 74 6f 20 63 6f 70  nt buffer to cop
341d5 79 2c 20 65 78 74 65 6e 64 0a 20 20 20 20 2a 2a  y, extend.    **
341d6 20 74 68 61 74 20 62 75 66 66 65 72 2e 20 20 6d   that buffer.  m
341d7 65 6d 63 70 79 28 29 20 73 65 65 6d 73 20 74 6f  emcpy() seems to
341d8 20 62 65 20 6d 6f 72 65 20 65 66 66 69 63 69 65   be more efficie
341d9 6e 74 20 69 66 20 69 74 20 68 61 73 20 61 0a 20  nt if it has a. 
341da 20 20 20 2a 2a 20 6c 6f 74 73 20 6f 66 20 64 61     ** lots of da
341db 74 61 20 74 6f 20 63 6f 70 79 2e 0a 20 20 20 20  ta to copy..    
341dc 2a 2f 0a 20 20 20 20 69 66 28 20 64 6c 72 44 6f  */.    if( dlrDo
341dd 63 44 61 74 61 28 72 65 61 64 65 72 73 5b 30 5d  cData(readers[0]
341de 2e 70 52 65 61 64 65 72 29 3d 3d 70 53 74 61 72  .pReader)==pStar
341df 74 2b 6e 53 74 61 72 74 20 29 7b 0a 20 20 20 20  t+nStart ){.    
341e0 20 20 6e 53 74 61 72 74 20 2b 3d 20 64 6c 72 44    nStart += dlrD
341e1 6f 63 44 61 74 61 42 79 74 65 73 28 72 65 61 64  ocDataBytes(read
341e2 65 72 73 5b 30 5d 2e 70 52 65 61 64 65 72 29 3b  ers[0].pReader);
341e3 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
341e4 20 20 69 66 28 20 70 53 74 61 72 74 21 3d 30 20    if( pStart!=0 
341e5 29 7b 0a 20 20 20 20 20 20 20 20 64 6c 77 41 70  ){.        dlwAp
341e6 70 65 6e 64 28 26 77 72 69 74 65 72 2c 20 70 53  pend(&writer, pS
341e7 74 61 72 74 2c 20 6e 53 74 61 72 74 2c 20 69 46  tart, nStart, iF
341e8 69 72 73 74 44 6f 63 69 64 2c 20 69 4c 61 73 74  irstDocid, iLast
341e9 44 6f 63 69 64 29 3b 0a 20 20 20 20 20 20 7d 0a  Docid);.      }.
341ea 20 20 20 20 20 20 70 53 74 61 72 74 20 3d 20 64        pStart = d
341eb 6c 72 44 6f 63 44 61 74 61 28 72 65 61 64 65 72  lrDocData(reader
341ec 73 5b 30 5d 2e 70 52 65 61 64 65 72 29 3b 0a 20  s[0].pReader);. 
341ed 20 20 20 20 20 6e 53 74 61 72 74 20 3d 20 64 6c       nStart = dl
341ee 72 44 6f 63 44 61 74 61 42 79 74 65 73 28 72 65  rDocDataBytes(re
341ef 61 64 65 72 73 5b 30 5d 2e 70 52 65 61 64 65 72  aders[0].pReader
341f0 29 3b 0a 20 20 20 20 20 20 69 46 69 72 73 74 44  );.      iFirstD
341f1 6f 63 69 64 20 3d 20 69 44 6f 63 69 64 3b 0a 20  ocid = iDocid;. 
341f2 20 20 20 7d 0a 20 20 20 20 69 4c 61 73 74 44 6f     }.    iLastDo
341f3 63 69 64 20 3d 20 69 44 6f 63 69 64 3b 0a 20 20  cid = iDocid;.  
341f4 20 20 64 6c 72 53 74 65 70 28 72 65 61 64 65 72    dlrStep(reader
341f5 73 5b 30 5d 2e 70 52 65 61 64 65 72 29 3b 0a 0a  s[0].pReader);..
341f6 20 20 20 20 2f 2a 20 44 72 6f 70 20 61 6c 6c 20      /* Drop all 
341f7 6f 66 20 74 68 65 20 6f 6c 64 65 72 20 65 6c 65  of the older ele
341f8 6d 65 6e 74 73 20 77 69 74 68 20 74 68 65 20 73  ments with the s
341f9 61 6d 65 20 64 6f 63 69 64 2e 20 2a 2f 0a 20 20  ame docid. */.  
341fa 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 6e 52 65    for(i=1; i<nRe
341fb 61 64 65 72 73 20 26 26 0a 20 20 20 20 20 20 20  aders &&.       
341fc 20 20 20 20 20 20 21 64 6c 72 41 74 45 6e 64 28        !dlrAtEnd(
341fd 72 65 61 64 65 72 73 5b 69 5d 2e 70 52 65 61 64  readers[i].pRead
341fe 65 72 29 20 26 26 0a 20 20 20 20 20 20 20 20 20  er) &&.         
341ff 20 20 20 20 64 6c 72 44 6f 63 69 64 28 72 65 61      dlrDocid(rea
34200 64 65 72 73 5b 69 5d 2e 70 52 65 61 64 65 72 29  ders[i].pReader)
34201 3d 3d 69 44 6f 63 69 64 3b 20 69 2b 2b 29 7b 0a  ==iDocid; i++){.
34202 20 20 20 20 20 20 64 6c 72 53 74 65 70 28 72 65        dlrStep(re
34203 61 64 65 72 73 5b 69 5d 2e 70 52 65 61 64 65 72  aders[i].pReader
34204 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  );.    }..    /*
34205 20 47 65 74 20 74 68 65 20 72 65 61 64 65 72 73   Get the readers
34206 20 62 61 63 6b 20 69 6e 74 6f 20 6f 72 64 65 72   back into order
34207 2e 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20  . */.    while( 
34208 69 2d 2d 3e 30 20 29 7b 0a 20 20 20 20 20 20 6f  i-->0 ){.      o
34209 72 64 65 72 65 64 44 4c 52 65 61 64 65 72 52 65  rderedDLReaderRe
3420a 6f 72 64 65 72 28 72 65 61 64 65 72 73 2b 69 2c  order(readers+i,
3420b 20 6e 52 65 61 64 65 72 73 2d 69 29 3b 0a 20 20   nReaders-i);.  
3420c 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f    }.  }..  /* Co
3420d 70 79 20 6f 76 65 72 20 61 6e 79 20 72 65 6d 61  py over any rema
3420e 69 6e 69 6e 67 20 65 6c 65 6d 65 6e 74 73 2e 20  ining elements. 
3420f 2a 2f 0a 20 20 69 66 28 20 6e 53 74 61 72 74 3e  */.  if( nStart>
34210 30 20 29 20 64 6c 77 41 70 70 65 6e 64 28 26 77  0 ) dlwAppend(&w
34211 72 69 74 65 72 2c 20 70 53 74 61 72 74 2c 20 6e  riter, pStart, n
34212 53 74 61 72 74 2c 20 69 46 69 72 73 74 44 6f 63  Start, iFirstDoc
34213 69 64 2c 20 69 4c 61 73 74 44 6f 63 69 64 29 3b  id, iLastDocid);
34214 0a 20 20 64 6c 77 44 65 73 74 72 6f 79 28 26 77  .  dlwDestroy(&w
34215 72 69 74 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 48 65  riter);.}../* He
34216 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f  lper function fo
34217 72 20 70 6f 73 4c 69 73 74 55 6e 69 6f 6e 28 29  r posListUnion()
34218 2e 20 20 43 6f 6d 70 61 72 65 73 20 74 68 65 20  .  Compares the 
34219 63 75 72 72 65 6e 74 20 70 6f 73 69 74 69 6f 6e  current position
3421a 0a 2a 2a 20 62 65 74 77 65 65 6e 20 6c 65 66 74  .** between left
3421b 20 61 6e 64 20 72 69 67 68 74 2c 20 72 65 74 75   and right, retu
3421c 72 6e 69 6e 67 20 61 73 20 73 74 61 6e 64 61 72  rning as standar
3421d 64 20 43 20 69 64 69 6f 6d 20 6f 66 20 3c 30 20  d C idiom of <0 
3421e 69 66 0a 2a 2a 20 6c 65 66 74 3c 72 69 67 68 74  if.** left<right
3421f 2c 20 3e 30 20 69 66 20 6c 65 66 74 3e 72 69 67  , >0 if left>rig
34220 68 74 2c 20 61 6e 64 20 30 20 69 66 20 6c 65 66  ht, and 0 if lef
34221 74 3d 3d 72 69 67 68 74 2e 20 20 22 45 6e 64 22  t==right.  "End"
34222 20 61 6c 77 61 79 73 0a 2a 2a 20 63 6f 6d 70 61   always.** compa
34223 72 65 73 20 67 72 65 61 74 65 72 2e 0a 2a 2f 0a  res greater..*/.
34224 73 74 61 74 69 63 20 69 6e 74 20 70 6f 73 4c 69  static int posLi
34225 73 74 43 6d 70 28 50 4c 52 65 61 64 65 72 20 2a  stCmp(PLReader *
34226 70 4c 65 66 74 2c 20 50 4c 52 65 61 64 65 72 20  pLeft, PLReader 
34227 2a 70 52 69 67 68 74 29 7b 0a 20 20 61 73 73 65  *pRight){.  asse
34228 72 74 28 20 70 4c 65 66 74 2d 3e 69 54 79 70 65  rt( pLeft->iType
34229 3d 3d 70 52 69 67 68 74 2d 3e 69 54 79 70 65 20  ==pRight->iType 
3422a 29 3b 0a 20 20 69 66 28 20 70 4c 65 66 74 2d 3e  );.  if( pLeft->
3422b 69 54 79 70 65 3d 3d 44 4c 5f 44 4f 43 49 44 53  iType==DL_DOCIDS
3422c 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20   ) return 0;..  
3422d 69 66 28 20 70 6c 72 41 74 45 6e 64 28 70 4c 65  if( plrAtEnd(pLe
3422e 66 74 29 20 29 20 72 65 74 75 72 6e 20 70 6c 72  ft) ) return plr
3422f 41 74 45 6e 64 28 70 52 69 67 68 74 29 20 3f 20  AtEnd(pRight) ? 
34230 30 20 3a 20 31 3b 0a 20 20 69 66 28 20 70 6c 72  0 : 1;.  if( plr
34231 41 74 45 6e 64 28 70 52 69 67 68 74 29 20 29 20  AtEnd(pRight) ) 
34232 72 65 74 75 72 6e 20 2d 31 3b 0a 0a 20 20 69 66  return -1;..  if
34233 28 20 70 6c 72 43 6f 6c 75 6d 6e 28 70 4c 65 66  ( plrColumn(pLef
34234 74 29 3c 70 6c 72 43 6f 6c 75 6d 6e 28 70 52 69  t)<plrColumn(pRi
34235 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 2d 31  ght) ) return -1
34236 3b 0a 20 20 69 66 28 20 70 6c 72 43 6f 6c 75 6d  ;.  if( plrColum
34237 6e 28 70 4c 65 66 74 29 3e 70 6c 72 43 6f 6c 75  n(pLeft)>plrColu
34238 6d 6e 28 70 52 69 67 68 74 29 20 29 20 72 65 74  mn(pRight) ) ret
34239 75 72 6e 20 31 3b 0a 0a 20 20 69 66 28 20 70 6c  urn 1;..  if( pl
3423a 72 50 6f 73 69 74 69 6f 6e 28 70 4c 65 66 74 29  rPosition(pLeft)
3423b 3c 70 6c 72 50 6f 73 69 74 69 6f 6e 28 70 52 69  <plrPosition(pRi
3423c 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 2d 31  ght) ) return -1
3423d 3b 0a 20 20 69 66 28 20 70 6c 72 50 6f 73 69 74  ;.  if( plrPosit
3423e 69 6f 6e 28 70 4c 65 66 74 29 3e 70 6c 72 50 6f  ion(pLeft)>plrPo
3423f 73 69 74 69 6f 6e 28 70 52 69 67 68 74 29 20 29  sition(pRight) )
34240 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 69 66 28   return 1;.  if(
34241 20 70 4c 65 66 74 2d 3e 69 54 79 70 65 3d 3d 44   pLeft->iType==D
34242 4c 5f 50 4f 53 49 54 49 4f 4e 53 20 29 20 72 65  L_POSITIONS ) re
34243 74 75 72 6e 20 30 3b 0a 0a 20 20 69 66 28 20 70  turn 0;..  if( p
34244 6c 72 53 74 61 72 74 4f 66 66 73 65 74 28 70 4c  lrStartOffset(pL
34245 65 66 74 29 3c 70 6c 72 53 74 61 72 74 4f 66 66  eft)<plrStartOff
34246 73 65 74 28 70 52 69 67 68 74 29 20 29 20 72 65  set(pRight) ) re
34247 74 75 72 6e 20 2d 31 3b 0a 20 20 69 66 28 20 70  turn -1;.  if( p
34248 6c 72 53 74 61 72 74 4f 66 66 73 65 74 28 70 4c  lrStartOffset(pL
34249 65 66 74 29 3e 70 6c 72 53 74 61 72 74 4f 66 66  eft)>plrStartOff
3424a 73 65 74 28 70 52 69 67 68 74 29 20 29 20 72 65  set(pRight) ) re
3424b 74 75 72 6e 20 31 3b 0a 0a 20 20 69 66 28 20 70  turn 1;..  if( p
3424c 6c 72 45 6e 64 4f 66 66 73 65 74 28 70 4c 65 66  lrEndOffset(pLef
3424d 74 29 3c 70 6c 72 45 6e 64 4f 66 66 73 65 74 28  t)<plrEndOffset(
3424e 70 52 69 67 68 74 29 20 29 20 72 65 74 75 72 6e  pRight) ) return
3424f 20 2d 31 3b 0a 20 20 69 66 28 20 70 6c 72 45 6e   -1;.  if( plrEn
34250 64 4f 66 66 73 65 74 28 70 4c 65 66 74 29 3e 70  dOffset(pLeft)>p
34251 6c 72 45 6e 64 4f 66 66 73 65 74 28 70 52 69 67  lrEndOffset(pRig
34252 68 74 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  ht) ) return 1;.
34253 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
34254 2f 2a 20 57 72 69 74 65 20 74 68 65 20 75 6e 69  /* Write the uni
34255 6f 6e 20 6f 66 20 70 6f 73 69 74 69 6f 6e 20 6c  on of position l
34256 69 73 74 73 20 69 6e 20 70 4c 65 66 74 20 61 6e  ists in pLeft an
34257 64 20 70 52 69 67 68 74 20 74 6f 20 70 4f 75 74  d pRight to pOut
34258 2e 0a 2a 2a 20 22 55 6e 69 6f 6e 22 20 69 6e 20  ..** "Union" in 
34259 74 68 69 73 20 63 61 73 65 20 6d 65 61 6e 69 6e  this case meanin
3425a 67 20 22 41 6c 6c 20 75 6e 69 71 75 65 20 70 6f  g "All unique po
3425b 73 69 74 69 6f 6e 20 74 75 70 6c 65 73 22 2e 20  sition tuples". 
3425c 20 53 68 6f 75 6c 64 0a 2a 2a 20 77 6f 72 6b 20   Should.** work 
3425d 77 69 74 68 20 61 6e 79 20 64 6f 63 6c 69 73 74  with any doclist
3425e 20 74 79 70 65 2c 20 74 68 6f 75 67 68 20 62 6f   type, though bo
3425f 74 68 20 69 6e 70 75 74 73 20 61 6e 64 20 74 68  th inputs and th
34260 65 20 6f 75 74 70 75 74 0a 2a 2a 20 73 68 6f 75  e output.** shou
34261 6c 64 20 62 65 20 74 68 65 20 73 61 6d 65 20 74  ld be the same t
34262 79 70 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ype..*/.static v
34263 6f 69 64 20 70 6f 73 4c 69 73 74 55 6e 69 6f 6e  oid posListUnion
34264 28 44 4c 52 65 61 64 65 72 20 2a 70 4c 65 66 74  (DLReader *pLeft
34265 2c 20 44 4c 52 65 61 64 65 72 20 2a 70 52 69 67  , DLReader *pRig
34266 68 74 2c 20 44 4c 57 72 69 74 65 72 20 2a 70 4f  ht, DLWriter *pO
34267 75 74 29 7b 0a 20 20 50 4c 52 65 61 64 65 72 20  ut){.  PLReader 
34268 6c 65 66 74 2c 20 72 69 67 68 74 3b 0a 20 20 50  left, right;.  P
34269 4c 57 72 69 74 65 72 20 77 72 69 74 65 72 3b 0a  LWriter writer;.
3426a 0a 20 20 61 73 73 65 72 74 28 20 64 6c 72 44 6f  .  assert( dlrDo
3426b 63 69 64 28 70 4c 65 66 74 29 3d 3d 64 6c 72 44  cid(pLeft)==dlrD
3426c 6f 63 69 64 28 70 52 69 67 68 74 29 20 29 3b 0a  ocid(pRight) );.
3426d 20 20 61 73 73 65 72 74 28 20 70 4c 65 66 74 2d    assert( pLeft-
3426e 3e 69 54 79 70 65 3d 3d 70 52 69 67 68 74 2d 3e  >iType==pRight->
3426f 69 54 79 70 65 20 29 3b 0a 20 20 61 73 73 65 72  iType );.  asser
34270 74 28 20 70 4c 65 66 74 2d 3e 69 54 79 70 65 3d  t( pLeft->iType=
34271 3d 70 4f 75 74 2d 3e 69 54 79 70 65 20 29 3b 0a  =pOut->iType );.
34272 0a 20 20 70 6c 72 49 6e 69 74 28 26 6c 65 66 74  .  plrInit(&left
34273 2c 20 70 4c 65 66 74 29 3b 0a 20 20 70 6c 72 49  , pLeft);.  plrI
34274 6e 69 74 28 26 72 69 67 68 74 2c 20 70 52 69 67  nit(&right, pRig
34275 68 74 29 3b 0a 20 20 70 6c 77 49 6e 69 74 28 26  ht);.  plwInit(&
34276 77 72 69 74 65 72 2c 20 70 4f 75 74 2c 20 64 6c  writer, pOut, dl
34277 72 44 6f 63 69 64 28 70 4c 65 66 74 29 29 3b 0a  rDocid(pLeft));.
34278 0a 20 20 77 68 69 6c 65 28 20 21 70 6c 72 41 74  .  while( !plrAt
34279 45 6e 64 28 26 6c 65 66 74 29 20 7c 7c 20 21 70  End(&left) || !p
3427a 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74 29 20  lrAtEnd(&right) 
3427b 29 7b 0a 20 20 20 20 69 6e 74 20 63 20 3d 20 70  ){.    int c = p
3427c 6f 73 4c 69 73 74 43 6d 70 28 26 6c 65 66 74 2c  osListCmp(&left,
3427d 20 26 72 69 67 68 74 29 3b 0a 20 20 20 20 69 66   &right);.    if
3427e 28 20 63 3c 30 20 29 7b 0a 20 20 20 20 20 20 70  ( c<0 ){.      p
3427f 6c 77 43 6f 70 79 28 26 77 72 69 74 65 72 2c 20  lwCopy(&writer, 
34280 26 6c 65 66 74 29 3b 0a 20 20 20 20 20 20 70 6c  &left);.      pl
34281 72 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20  rStep(&left);.  
34282 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3e 30 20    }else if( c>0 
34283 29 7b 0a 20 20 20 20 20 20 70 6c 77 43 6f 70 79  ){.      plwCopy
34284 28 26 77 72 69 74 65 72 2c 20 26 72 69 67 68 74  (&writer, &right
34285 29 3b 0a 20 20 20 20 20 20 70 6c 72 53 74 65 70  );.      plrStep
34286 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20 7d 65  (&right);.    }e
34287 6c 73 65 7b 0a 20 20 20 20 20 20 70 6c 77 43 6f  lse{.      plwCo
34288 70 79 28 26 77 72 69 74 65 72 2c 20 26 6c 65 66  py(&writer, &lef
34289 74 29 3b 0a 20 20 20 20 20 20 70 6c 72 53 74 65  t);.      plrSte
3428a 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 20 20  p(&left);.      
3428b 70 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b  plrStep(&right);
3428c 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 6c  .    }.  }..  pl
3428d 77 54 65 72 6d 69 6e 61 74 65 28 26 77 72 69 74  wTerminate(&writ
3428e 65 72 29 3b 0a 20 20 70 6c 77 44 65 73 74 72 6f  er);.  plwDestro
3428f 79 28 26 77 72 69 74 65 72 29 3b 0a 20 20 70 6c  y(&writer);.  pl
34290 72 44 65 73 74 72 6f 79 28 26 6c 65 66 74 29 3b  rDestroy(&left);
34291 0a 20 20 70 6c 72 44 65 73 74 72 6f 79 28 26 72  .  plrDestroy(&r
34292 69 67 68 74 29 3b 0a 7d 0a 0a 2f 2a 20 57 72 69  ight);.}../* Wri
34293 74 65 20 74 68 65 20 75 6e 69 6f 6e 20 6f 66 20  te the union of 
34294 64 6f 63 6c 69 73 74 73 20 69 6e 20 70 4c 65 66  doclists in pLef
34295 74 20 61 6e 64 20 70 52 69 67 68 74 20 74 6f 20  t and pRight to 
34296 70 4f 75 74 2e 20 20 46 6f 72 0a 2a 2a 20 64 6f  pOut.  For.** do
34297 63 69 64 73 20 69 6e 20 63 6f 6d 6d 6f 6e 20 62  cids in common b
34298 65 74 77 65 65 6e 20 74 68 65 20 69 6e 70 75 74  etween the input
34299 73 2c 20 74 68 65 20 75 6e 69 6f 6e 20 6f 66 20  s, the union of 
3429a 74 68 65 20 70 6f 73 69 74 69 6f 6e 0a 2a 2a 20  the position.** 
3429b 6c 69 73 74 73 20 69 73 20 77 72 69 74 74 65 6e  lists is written
3429c 2e 20 20 49 6e 70 75 74 73 20 61 6e 64 20 6f 75  .  Inputs and ou
3429d 74 70 75 74 73 20 61 72 65 20 61 6c 77 61 79 73  tputs are always
3429e 20 74 79 70 65 20 44 4c 5f 44 45 46 41 55 4c 54   type DL_DEFAULT
3429f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
342a0 20 64 6f 63 4c 69 73 74 55 6e 69 6f 6e 28 0a 20   docListUnion(. 
342a1 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4c 65   const char *pLe
342a2 66 74 2c 20 69 6e 74 20 6e 4c 65 66 74 2c 0a 20  ft, int nLeft,. 
342a3 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 52 69   const char *pRi
342a4 67 68 74 2c 20 69 6e 74 20 6e 52 69 67 68 74 2c  ght, int nRight,
342a5 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 2a 70  .  DataBuffer *p
342a6 4f 75 74 20 20 20 20 20 20 2f 2a 20 57 72 69 74  Out      /* Writ
342a7 65 20 74 68 65 20 63 6f 6d 62 69 6e 65 64 20 64  e the combined d
342a8 6f 63 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a 29  oclist here */.)
342a9 7b 0a 20 20 44 4c 52 65 61 64 65 72 20 6c 65 66  {.  DLReader lef
342aa 74 2c 20 72 69 67 68 74 3b 0a 20 20 44 4c 57 72  t, right;.  DLWr
342ab 69 74 65 72 20 77 72 69 74 65 72 3b 0a 0a 20 20  iter writer;..  
342ac 69 66 28 20 6e 4c 65 66 74 3d 3d 30 20 29 7b 0a  if( nLeft==0 ){.
342ad 20 20 20 20 69 66 28 20 6e 52 69 67 68 74 21 3d      if( nRight!=
342ae 30 29 20 64 61 74 61 42 75 66 66 65 72 41 70 70  0) dataBufferApp
342af 65 6e 64 28 70 4f 75 74 2c 20 70 52 69 67 68 74  end(pOut, pRight
342b0 2c 20 6e 52 69 67 68 74 29 3b 0a 20 20 20 20 72  , nRight);.    r
342b1 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28  eturn;.  }.  if(
342b2 20 6e 52 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20   nRight==0 ){.  
342b3 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65    dataBufferAppe
342b4 6e 64 28 70 4f 75 74 2c 20 70 4c 65 66 74 2c 20  nd(pOut, pLeft, 
342b5 6e 4c 65 66 74 29 3b 0a 20 20 20 20 72 65 74 75  nLeft);.    retu
342b6 72 6e 3b 0a 20 20 7d 0a 0a 20 20 64 6c 72 49 6e  rn;.  }..  dlrIn
342b7 69 74 28 26 6c 65 66 74 2c 20 44 4c 5f 44 45 46  it(&left, DL_DEF
342b8 41 55 4c 54 2c 20 70 4c 65 66 74 2c 20 6e 4c 65  AULT, pLeft, nLe
342b9 66 74 29 3b 0a 20 20 64 6c 72 49 6e 69 74 28 26  ft);.  dlrInit(&
342ba 72 69 67 68 74 2c 20 44 4c 5f 44 45 46 41 55 4c  right, DL_DEFAUL
342bb 54 2c 20 70 52 69 67 68 74 2c 20 6e 52 69 67 68  T, pRight, nRigh
342bc 74 29 3b 0a 20 20 64 6c 77 49 6e 69 74 28 26 77  t);.  dlwInit(&w
342bd 72 69 74 65 72 2c 20 44 4c 5f 44 45 46 41 55 4c  riter, DL_DEFAUL
342be 54 2c 20 70 4f 75 74 29 3b 0a 0a 20 20 77 68 69  T, pOut);..  whi
342bf 6c 65 28 20 21 64 6c 72 41 74 45 6e 64 28 26 6c  le( !dlrAtEnd(&l
342c0 65 66 74 29 20 7c 7c 20 21 64 6c 72 41 74 45 6e  eft) || !dlrAtEn
342c1 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20  d(&right) ){.   
342c2 20 69 66 28 20 64 6c 72 41 74 45 6e 64 28 26 72   if( dlrAtEnd(&r
342c3 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20 64  ight) ){.      d
342c4 6c 77 43 6f 70 79 28 26 77 72 69 74 65 72 2c 20  lwCopy(&writer, 
342c5 26 6c 65 66 74 29 3b 0a 20 20 20 20 20 20 64 6c  &left);.      dl
342c6 72 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20  rStep(&left);.  
342c7 20 20 7d 65 6c 73 65 20 69 66 28 20 64 6c 72 41    }else if( dlrA
342c8 74 45 6e 64 28 26 6c 65 66 74 29 20 29 7b 0a 20  tEnd(&left) ){. 
342c9 20 20 20 20 20 64 6c 77 43 6f 70 79 28 26 77 72       dlwCopy(&wr
342ca 69 74 65 72 2c 20 26 72 69 67 68 74 29 3b 0a 20  iter, &right);. 
342cb 20 20 20 20 20 64 6c 72 53 74 65 70 28 26 72 69       dlrStep(&ri
342cc 67 68 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20  ght);.    }else 
342cd 69 66 28 20 64 6c 72 44 6f 63 69 64 28 26 6c 65  if( dlrDocid(&le
342ce 66 74 29 3c 64 6c 72 44 6f 63 69 64 28 26 72 69  ft)<dlrDocid(&ri
342cf 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20 64 6c  ght) ){.      dl
342d0 77 43 6f 70 79 28 26 77 72 69 74 65 72 2c 20 26  wCopy(&writer, &
342d1 6c 65 66 74 29 3b 0a 20 20 20 20 20 20 64 6c 72  left);.      dlr
342d2 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20  Step(&left);.   
342d3 20 7d 65 6c 73 65 20 69 66 28 20 64 6c 72 44 6f   }else if( dlrDo
342d4 63 69 64 28 26 6c 65 66 74 29 3e 64 6c 72 44 6f  cid(&left)>dlrDo
342d5 63 69 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20  cid(&right) ){. 
342d6 20 20 20 20 20 64 6c 77 43 6f 70 79 28 26 77 72       dlwCopy(&wr
342d7 69 74 65 72 2c 20 26 72 69 67 68 74 29 3b 0a 20  iter, &right);. 
342d8 20 20 20 20 20 64 6c 72 53 74 65 70 28 26 72 69       dlrStep(&ri
342d9 67 68 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ght);.    }else{
342da 0a 20 20 20 20 20 20 70 6f 73 4c 69 73 74 55 6e  .      posListUn
342db 69 6f 6e 28 26 6c 65 66 74 2c 20 26 72 69 67 68  ion(&left, &righ
342dc 74 2c 20 26 77 72 69 74 65 72 29 3b 0a 20 20 20  t, &writer);.   
342dd 20 20 20 64 6c 72 53 74 65 70 28 26 6c 65 66 74     dlrStep(&left
342de 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70  );.      dlrStep
342df 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a  (&right);.    }.
342e0 20 20 7d 0a 0a 20 20 64 6c 72 44 65 73 74 72 6f    }..  dlrDestro
342e1 79 28 26 6c 65 66 74 29 3b 0a 20 20 64 6c 72 44  y(&left);.  dlrD
342e2 65 73 74 72 6f 79 28 26 72 69 67 68 74 29 3b 0a  estroy(&right);.
342e3 20 20 64 6c 77 44 65 73 74 72 6f 79 28 26 77 72    dlwDestroy(&wr
342e4 69 74 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a  iter);.}../* .**
342e5 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
342e6 73 20 75 73 65 64 20 61 73 20 70 61 72 74 20 6f  s used as part o
342e7 66 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  f the implementa
342e8 74 69 6f 6e 20 6f 66 20 70 68 72 61 73 65 20 61  tion of phrase a
342e9 6e 64 0a 2a 2a 20 4e 45 41 52 20 6d 61 74 63 68  nd.** NEAR match
342ea 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 70 4c 65 66 74  ing..**.** pLeft
342eb 20 61 6e 64 20 70 52 69 67 68 74 20 61 72 65 20   and pRight are 
342ec 44 4c 52 65 61 64 65 72 73 20 70 6f 73 69 74 69  DLReaders positi
342ed 6f 6e 65 64 20 74 6f 20 74 68 65 20 73 61 6d 65  oned to the same
342ee 20 64 6f 63 69 64 20 69 6e 0a 2a 2a 20 6c 69 73   docid in.** lis
342ef 74 73 20 6f 66 20 74 79 70 65 20 44 4c 5f 50 4f  ts of type DL_PO
342f0 53 49 54 49 4f 4e 2e 20 54 68 69 73 20 66 75 6e  SITION. This fun
342f1 63 74 69 6f 6e 20 77 72 69 74 65 73 20 61 6e 20  ction writes an 
342f2 65 6e 74 72 79 20 74 6f 20 74 68 65 0a 2a 2a 20  entry to the.** 
342f3 44 4c 57 72 69 74 65 72 20 70 4f 75 74 20 66 6f  DLWriter pOut fo
342f4 72 20 65 61 63 68 20 70 6f 73 69 74 69 6f 6e 20  r each position 
342f5 69 6e 20 70 52 69 67 68 74 20 74 68 61 74 20 69  in pRight that i
342f6 73 20 6c 65 73 73 20 74 68 61 6e 0a 2a 2a 20 28  s less than.** (
342f7 6e 4e 65 61 72 2b 31 29 20 67 72 65 61 74 65 72  nNear+1) greater
342f8 20 28 62 75 74 20 6e 6f 74 20 65 71 75 61 6c 20   (but not equal 
342f9 74 6f 20 6f 72 20 73 6d 61 6c 6c 65 72 29 20 74  to or smaller) t
342fa 68 61 6e 20 61 20 70 6f 73 69 74 69 6f 6e 20 0a  han a position .
342fb 2a 2a 20 69 6e 20 70 4c 65 66 74 2e 20 46 6f 72  ** in pLeft. For
342fc 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 6e 4e 65   example, if nNe
342fd 61 72 20 69 73 20 30 2c 20 61 6e 64 20 74 68 65  ar is 0, and the
342fe 20 70 6f 73 69 74 69 6f 6e 73 20 63 6f 6e 74 61   positions conta
342ff 69 6e 65 64 0a 2a 2a 20 62 79 20 70 4c 65 66 74  ined.** by pLeft
34300 20 61 6e 64 20 70 52 69 67 68 74 20 61 72 65 3a   and pRight are:
34301 0a 2a 2a 0a 2a 2a 20 20 20 20 70 4c 65 66 74 3a  .**.**    pLeft:
34302 20 20 35 20 31 30 20 31 35 20 32 30 0a 2a 2a 20    5 10 15 20.** 
34303 20 20 20 70 52 69 67 68 74 3a 20 36 20 20 39 20     pRight: 6  9 
34304 31 37 20 32 31 0a 2a 2a 0a 2a 2a 20 74 68 65 6e  17 21.**.** then
34305 20 74 68 65 20 64 6f 63 69 64 20 69 73 20 61 64   the docid is ad
34306 64 65 64 20 74 6f 20 70 4f 75 74 2e 20 49 66 20  ded to pOut. If 
34307 70 4f 75 74 20 69 73 20 6f 66 20 74 79 70 65 20  pOut is of type 
34308 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 2c 0a 2a 2a  DL_POSITIONS,.**
34309 20 74 68 65 6e 20 61 20 70 6f 73 69 74 69 6f 6e   then a position
3430a 69 64 73 20 22 36 22 20 61 6e 64 20 22 32 31 22  ids "6" and "21"
3430b 20 61 72 65 20 61 6c 73 6f 20 61 64 64 65 64 20   are also added 
3430c 74 6f 20 70 4f 75 74 2e 0a 2a 2a 0a 2a 2a 20 49  to pOut..**.** I
3430d 66 20 62 6f 6f 6c 65 61 6e 20 61 72 67 75 6d 65  f boolean argume
3430e 6e 74 20 69 73 53 61 76 65 4c 65 66 74 20 69 73  nt isSaveLeft is
3430f 20 74 72 75 65 2c 20 74 68 65 6e 20 70 6f 73 69   true, then posi
34310 74 69 6f 6e 69 64 73 20 61 72 65 20 63 6f 70 69  tionids are copi
34311 65 64 0a 2a 2a 20 66 72 6f 6d 20 70 4c 65 66 74  ed.** from pLeft
34312 20 69 6e 73 74 65 61 64 20 6f 66 20 70 52 69 67   instead of pRig
34313 68 74 2e 20 49 6e 20 74 68 65 20 65 78 61 6d 70  ht. In the examp
34314 6c 65 20 61 62 6f 76 65 2c 20 74 68 65 20 70 6f  le above, the po
34315 73 69 74 69 6f 6e 73 20 22 35 22 0a 2a 2a 20 61  sitions "5".** a
34316 6e 64 20 22 32 30 22 20 77 6f 75 6c 64 20 62 65  nd "20" would be
34317 20 61 64 64 65 64 20 69 6e 73 74 65 61 64 20 6f   added instead o
34318 66 20 22 36 22 20 61 6e 64 20 22 32 31 22 2e 0a  f "6" and "21"..
34319 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  */.static void p
3431a 6f 73 4c 69 73 74 50 68 72 61 73 65 4d 65 72 67  osListPhraseMerg
3431b 65 28 0a 20 20 44 4c 52 65 61 64 65 72 20 2a 70  e(.  DLReader *p
3431c 4c 65 66 74 2c 20 0a 20 20 44 4c 52 65 61 64 65  Left, .  DLReade
3431d 72 20 2a 70 52 69 67 68 74 2c 0a 20 20 69 6e 74  r *pRight,.  int
3431e 20 6e 4e 65 61 72 2c 0a 20 20 69 6e 74 20 69 73   nNear,.  int is
3431f 53 61 76 65 4c 65 66 74 2c 0a 20 20 44 4c 57 72  SaveLeft,.  DLWr
34320 69 74 65 72 20 2a 70 4f 75 74 0a 29 7b 0a 20 20  iter *pOut.){.  
34321 50 4c 52 65 61 64 65 72 20 6c 65 66 74 2c 20 72  PLReader left, r
34322 69 67 68 74 3b 0a 20 20 50 4c 57 72 69 74 65 72  ight;.  PLWriter
34323 20 77 72 69 74 65 72 3b 0a 20 20 69 6e 74 20 6d   writer;.  int m
34324 61 74 63 68 20 3d 20 30 3b 0a 0a 20 20 61 73 73  atch = 0;..  ass
34325 65 72 74 28 20 64 6c 72 44 6f 63 69 64 28 70 4c  ert( dlrDocid(pL
34326 65 66 74 29 3d 3d 64 6c 72 44 6f 63 69 64 28 70  eft)==dlrDocid(p
34327 52 69 67 68 74 29 20 29 3b 0a 20 20 61 73 73 65  Right) );.  asse
34328 72 74 28 20 70 4f 75 74 2d 3e 69 54 79 70 65 21  rt( pOut->iType!
34329 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f 46  =DL_POSITIONS_OF
3432a 46 53 45 54 53 20 29 3b 0a 0a 20 20 70 6c 72 49  FSETS );..  plrI
3432b 6e 69 74 28 26 6c 65 66 74 2c 20 70 4c 65 66 74  nit(&left, pLeft
3432c 29 3b 0a 20 20 70 6c 72 49 6e 69 74 28 26 72 69  );.  plrInit(&ri
3432d 67 68 74 2c 20 70 52 69 67 68 74 29 3b 0a 0a 20  ght, pRight);.. 
3432e 20 77 68 69 6c 65 28 20 21 70 6c 72 41 74 45 6e   while( !plrAtEn
3432f 64 28 26 6c 65 66 74 29 20 26 26 20 21 70 6c 72  d(&left) && !plr
34330 41 74 45 6e 64 28 26 72 69 67 68 74 29 20 29 7b  AtEnd(&right) ){
34331 0a 20 20 20 20 69 66 28 20 70 6c 72 43 6f 6c 75  .    if( plrColu
34332 6d 6e 28 26 6c 65 66 74 29 3c 70 6c 72 43 6f 6c  mn(&left)<plrCol
34333 75 6d 6e 28 26 72 69 67 68 74 29 20 29 7b 0a 20  umn(&right) ){. 
34334 20 20 20 20 20 70 6c 72 53 74 65 70 28 26 6c 65       plrStep(&le
34335 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  ft);.    }else i
34336 66 28 20 70 6c 72 43 6f 6c 75 6d 6e 28 26 6c 65  f( plrColumn(&le
34337 66 74 29 3e 70 6c 72 43 6f 6c 75 6d 6e 28 26 72  ft)>plrColumn(&r
34338 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20 70  ight) ){.      p
34339 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a  lrStep(&right);.
3433a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 6c      }else if( pl
3433b 72 50 6f 73 69 74 69 6f 6e 28 26 6c 65 66 74 29  rPosition(&left)
3433c 3e 3d 70 6c 72 50 6f 73 69 74 69 6f 6e 28 26 72  >=plrPosition(&r
3433d 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20 70  ight) ){.      p
3433e 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a  lrStep(&right);.
3433f 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
34340 20 69 66 28 20 28 70 6c 72 50 6f 73 69 74 69 6f   if( (plrPositio
34341 6e 28 26 72 69 67 68 74 29 2d 70 6c 72 50 6f 73  n(&right)-plrPos
34342 69 74 69 6f 6e 28 26 6c 65 66 74 29 29 3c 3d 28  ition(&left))<=(
34343 6e 4e 65 61 72 2b 31 29 20 29 7b 0a 20 20 20 20  nNear+1) ){.    
34344 20 20 20 20 69 66 28 20 21 6d 61 74 63 68 20 29      if( !match )
34345 7b 0a 20 20 20 20 20 20 20 20 20 20 70 6c 77 49  {.          plwI
34346 6e 69 74 28 26 77 72 69 74 65 72 2c 20 70 4f 75  nit(&writer, pOu
34347 74 2c 20 64 6c 72 44 6f 63 69 64 28 70 4c 65 66  t, dlrDocid(pLef
34348 74 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6d  t));.          m
34349 61 74 63 68 20 3d 20 31 3b 0a 20 20 20 20 20 20  atch = 1;.      
3434a 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
3434b 21 69 73 53 61 76 65 4c 65 66 74 20 29 7b 0a 20  !isSaveLeft ){. 
3434c 20 20 20 20 20 20 20 20 20 70 6c 77 41 64 64 28           plwAdd(
3434d 26 77 72 69 74 65 72 2c 20 70 6c 72 43 6f 6c 75  &writer, plrColu
3434e 6d 6e 28 26 72 69 67 68 74 29 2c 20 70 6c 72 50  mn(&right), plrP
3434f 6f 73 69 74 69 6f 6e 28 26 72 69 67 68 74 29 2c  osition(&right),
34350 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20   0, 0);.        
34351 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
34352 20 70 6c 77 41 64 64 28 26 77 72 69 74 65 72 2c   plwAdd(&writer,
34353 20 70 6c 72 43 6f 6c 75 6d 6e 28 26 6c 65 66 74   plrColumn(&left
34354 29 2c 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28 26  ), plrPosition(&
34355 6c 65 66 74 29 2c 20 30 2c 20 30 29 3b 0a 20 20  left), 0, 0);.  
34356 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
34357 70 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b  plrStep(&right);
34358 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
34359 20 20 20 20 20 20 70 6c 72 53 74 65 70 28 26 6c        plrStep(&l
3435a 65 66 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  eft);.      }.  
3435b 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 6d    }.  }..  if( m
3435c 61 74 63 68 20 29 7b 0a 20 20 20 20 70 6c 77 54  atch ){.    plwT
3435d 65 72 6d 69 6e 61 74 65 28 26 77 72 69 74 65 72  erminate(&writer
3435e 29 3b 0a 20 20 20 20 70 6c 77 44 65 73 74 72 6f  );.    plwDestro
3435f 79 28 26 77 72 69 74 65 72 29 3b 0a 20 20 7d 0a  y(&writer);.  }.
34360 0a 20 20 70 6c 72 44 65 73 74 72 6f 79 28 26 6c  .  plrDestroy(&l
34361 65 66 74 29 3b 0a 20 20 70 6c 72 44 65 73 74 72  eft);.  plrDestr
34362 6f 79 28 26 72 69 67 68 74 29 3b 0a 7d 0a 0a 2f  oy(&right);.}../
34363 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65  *.** Compare the
34364 20 76 61 6c 75 65 73 20 70 6f 69 6e 74 65 64 20   values pointed 
34365 74 6f 20 62 79 20 74 68 65 20 50 4c 52 65 61 64  to by the PLRead
34366 65 72 73 20 70 61 73 73 65 64 20 61 73 20 61 72  ers passed as ar
34367 67 75 6d 65 6e 74 73 2e 20 0a 2a 2a 20 52 65 74  guments. .** Ret
34368 75 72 6e 20 2d 31 20 69 66 20 74 68 65 20 76 61  urn -1 if the va
34369 6c 75 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  lue pointed to b
3436a 79 20 70 4c 65 66 74 20 69 73 20 63 6f 6e 73 69  y pLeft is consi
3436b 64 65 72 65 64 20 6c 65 73 73 20 74 68 61 6e 0a  dered less than.
3436c 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 70 6f 69  ** the value poi
3436d 6e 74 65 64 20 74 6f 20 62 79 20 70 52 69 67 68  nted to by pRigh
3436e 74 2c 20 2b 31 20 69 66 20 69 74 20 69 73 20 63  t, +1 if it is c
3436f 6f 6e 73 69 64 65 72 65 64 20 67 72 65 61 74 65  onsidered greate
34370 72 0a 2a 2a 20 74 68 61 6e 20 69 74 2c 20 6f 72  r.** than it, or
34371 20 30 20 69 66 20 69 74 20 69 73 20 65 71 75 61   0 if it is equa
34372 6c 2e 20 69 2e 65 2e 0a 2a 2a 0a 2a 2a 20 20 20  l. i.e..**.**   
34373 20 20 28 2a 70 4c 65 66 74 20 2d 20 2a 70 52 69    (*pLeft - *pRi
34374 67 68 74 29 0a 2a 2a 0a 2a 2a 20 41 20 50 4c 52  ght).**.** A PLR
34375 65 61 64 65 72 20 74 68 61 74 20 69 73 20 69 6e  eader that is in
34376 20 74 68 65 20 45 4f 46 20 63 6f 6e 64 69 74 69   the EOF conditi
34377 6f 6e 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64  on is considered
34378 20 67 72 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a   greater than.**
34379 20 61 6e 79 20 6f 74 68 65 72 2e 20 49 66 20 6e   any other. If n
3437a 65 69 74 68 65 72 20 61 72 67 75 6d 65 6e 74 20  either argument 
3437b 69 73 20 69 6e 20 45 4f 46 20 73 74 61 74 65 2c  is in EOF state,
3437c 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
3437d 65 20 6f 66 0a 2a 2a 20 70 6c 72 43 6f 6c 75 6d  e of.** plrColum
3437e 6e 28 29 20 69 73 20 75 73 65 64 2e 20 49 66 20  n() is used. If 
3437f 74 68 65 20 70 6c 72 43 6f 6c 75 6d 6e 28 29 20  the plrColumn() 
34380 76 61 6c 75 65 73 20 61 72 65 20 65 71 75 61 6c  values are equal
34381 2c 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 61 72 69  , the.** compari
34382 73 6f 6e 20 69 73 20 6f 6e 20 74 68 65 20 62 61  son is on the ba
34383 73 69 73 20 6f 66 20 70 6c 72 50 6f 73 69 74 69  sis of plrPositi
34384 6f 6e 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  on()..*/.static 
34385 69 6e 74 20 70 6c 72 43 6f 6d 70 61 72 65 28 50  int plrCompare(P
34386 4c 52 65 61 64 65 72 20 2a 70 4c 65 66 74 2c 20  LReader *pLeft, 
34387 50 4c 52 65 61 64 65 72 20 2a 70 52 69 67 68 74  PLReader *pRight
34388 29 7b 0a 20 20 61 73 73 65 72 74 28 21 70 6c 72  ){.  assert(!plr
34389 41 74 45 6e 64 28 70 4c 65 66 74 29 20 7c 7c 20  AtEnd(pLeft) || 
3438a 21 70 6c 72 41 74 45 6e 64 28 70 52 69 67 68 74  !plrAtEnd(pRight
3438b 29 29 3b 0a 0a 20 20 69 66 28 20 70 6c 72 41 74  ));..  if( plrAt
3438c 45 6e 64 28 70 52 69 67 68 74 29 20 7c 7c 20 70  End(pRight) || p
3438d 6c 72 41 74 45 6e 64 28 70 4c 65 66 74 29 20 29  lrAtEnd(pLeft) )
3438e 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 70 6c  {.    return (pl
3438f 72 41 74 45 6e 64 28 70 52 69 67 68 74 29 20 3f  rAtEnd(pRight) ?
34390 20 2d 31 20 3a 20 31 29 3b 0a 20 20 7d 0a 20 20   -1 : 1);.  }.  
34391 69 66 28 20 70 6c 72 43 6f 6c 75 6d 6e 28 70 4c  if( plrColumn(pL
34392 65 66 74 29 21 3d 70 6c 72 43 6f 6c 75 6d 6e 28  eft)!=plrColumn(
34393 70 52 69 67 68 74 29 20 29 7b 0a 20 20 20 20 72  pRight) ){.    r
34394 65 74 75 72 6e 20 28 28 70 6c 72 43 6f 6c 75 6d  eturn ((plrColum
34395 6e 28 70 4c 65 66 74 29 3c 70 6c 72 43 6f 6c 75  n(pLeft)<plrColu
34396 6d 6e 28 70 52 69 67 68 74 29 29 20 3f 20 2d 31  mn(pRight)) ? -1
34397 20 3a 20 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28   : 1);.  }.  if(
34398 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28 70 4c 65   plrPosition(pLe
34399 66 74 29 21 3d 70 6c 72 50 6f 73 69 74 69 6f 6e  ft)!=plrPosition
3439a 28 70 52 69 67 68 74 29 20 29 7b 0a 20 20 20 20  (pRight) ){.    
3439b 72 65 74 75 72 6e 20 28 28 70 6c 72 50 6f 73 69  return ((plrPosi
3439c 74 69 6f 6e 28 70 4c 65 66 74 29 3c 70 6c 72 50  tion(pLeft)<plrP
3439d 6f 73 69 74 69 6f 6e 28 70 52 69 67 68 74 29 29  osition(pRight))
3439e 20 3f 20 2d 31 20 3a 20 31 29 3b 0a 20 20 7d 0a   ? -1 : 1);.  }.
3439f 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
343a0 2a 20 57 65 20 68 61 76 65 20 74 77 6f 20 64 6f  * We have two do
343a1 63 6c 69 73 74 73 20 77 69 74 68 20 70 6f 73 69  clists with posi
343a2 74 69 6f 6e 73 3a 20 20 70 4c 65 66 74 20 61 6e  tions:  pLeft an
343a3 64 20 70 52 69 67 68 74 2e 20 44 65 70 65 6e 64  d pRight. Depend
343a4 69 6e 67 0a 2a 2a 20 6f 6e 20 74 68 65 20 76 61  ing.** on the va
343a5 6c 75 65 20 6f 66 20 74 68 65 20 6e 4e 65 61 72  lue of the nNear
343a6 20 70 61 72 61 6d 65 74 65 72 2c 20 70 65 72 66   parameter, perf
343a7 6f 72 6d 20 65 69 74 68 65 72 20 61 20 70 68 72  orm either a phr
343a8 61 73 65 0a 2a 2a 20 69 6e 74 65 72 73 65 63 74  ase.** intersect
343a9 69 6f 6e 20 28 69 66 20 6e 4e 65 61 72 3d 3d 30  ion (if nNear==0
343aa 29 20 6f 72 20 61 20 4e 45 41 52 20 69 6e 74 65  ) or a NEAR inte
343ab 72 73 65 63 74 69 6f 6e 20 28 69 66 20 6e 4e 65  rsection (if nNe
343ac 61 72 3e 30 29 0a 2a 2a 20 61 6e 64 20 77 72 69  ar>0).** and wri
343ad 74 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 69  te the results i
343ae 6e 74 6f 20 70 4f 75 74 2e 0a 2a 2a 0a 2a 2a 20  nto pOut..**.** 
343af 41 20 70 68 72 61 73 65 20 69 6e 74 65 72 73 65  A phrase interse
343b0 63 74 69 6f 6e 20 6d 65 61 6e 73 20 74 68 61 74  ction means that
343b1 20 74 77 6f 20 64 6f 63 75 6d 65 6e 74 73 20 6f   two documents o
343b2 6e 6c 79 20 6d 61 74 63 68 0a 2a 2a 20 69 66 20  nly match.** if 
343b3 70 4c 65 66 74 2e 69 50 6f 73 2b 31 3d 3d 70 52  pLeft.iPos+1==pR
343b4 69 67 68 74 2e 69 50 6f 73 2e 0a 2a 2a 0a 2a 2a  ight.iPos..**.**
343b5 20 41 20 4e 45 41 52 20 69 6e 74 65 72 73 65 63   A NEAR intersec
343b6 74 69 6f 6e 20 6d 65 61 6e 73 20 74 68 61 74 20  tion means that 
343b7 74 77 6f 20 64 6f 63 75 6d 65 6e 74 73 20 6f 6e  two documents on
343b8 6c 79 20 6d 61 74 63 68 20 69 66 20 0a 2a 2a 20  ly match if .** 
343b9 28 61 62 73 28 70 4c 65 66 74 2e 69 50 6f 73 2d  (abs(pLeft.iPos-
343ba 70 52 69 67 68 74 2e 69 50 6f 73 29 3c 6e 4e 65  pRight.iPos)<nNe
343bb 61 72 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20  ar)..**.** If a 
343bc 4e 45 41 52 20 69 6e 74 65 72 73 65 63 74 69 6f  NEAR intersectio
343bd 6e 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20  n is requested, 
343be 74 68 65 6e 20 74 68 65 20 6e 50 68 72 61 73 65  then the nPhrase
343bf 20 61 72 67 75 6d 65 6e 74 20 73 68 6f 75 6c 64   argument should
343c0 0a 2a 2a 20 62 65 20 70 61 73 73 65 64 20 74 68  .** be passed th
343c1 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65  e number of toke
343c2 6e 73 20 69 6e 20 74 68 65 20 74 77 6f 20 6f 70  ns in the two op
343c3 65 72 61 6e 64 73 20 74 6f 20 74 68 65 20 4e 45  erands to the NE
343c4 41 52 20 6f 70 65 72 61 74 6f 72 0a 2a 2a 20 63  AR operator.** c
343c5 6f 6d 62 69 6e 65 64 2e 20 46 6f 72 20 65 78 61  ombined. For exa
343c6 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  mple:.**.**     
343c7 20 20 51 75 65 72 79 20 73 79 6e 74 61 78 20 20    Query syntax  
343c8 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 50 68               nPh
343c9 72 61 73 65 0a 2a 2a 20 20 20 20 20 20 2d 2d 2d  rase.**      ---
343ca 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
343cb 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
343cc 2d 0a 2a 2a 20 20 20 20 20 20 20 22 41 20 42 20  -.**       "A B 
343cd 43 22 20 4e 45 41 52 20 22 44 20 45 22 20 20 20  C" NEAR "D E"   
343ce 20 20 20 20 20 20 35 0a 2a 2a 20 20 20 20 20 20        5.**      
343cf 20 41 20 4e 45 41 52 20 42 20 20 20 20 20 20 20   A NEAR B       
343d0 20 20 20 20 20 20 20 20 20 20 20 20 32 0a 2a 2a              2.**
343d1 0a 2a 2a 20 69 54 79 70 65 20 63 6f 6e 74 72 6f  .** iType contro
343d2 6c 73 20 74 68 65 20 74 79 70 65 20 6f 66 20 64  ls the type of d
343d3 61 74 61 20 77 72 69 74 74 65 6e 20 74 6f 20 70  ata written to p
343d4 4f 75 74 2e 20 20 49 66 20 69 54 79 70 65 20 69  Out.  If iType i
343d5 73 0a 2a 2a 20 44 4c 5f 50 4f 53 49 54 49 4f 4e  s.** DL_POSITION
343d6 53 2c 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 73  S, the positions
343d7 20 61 72 65 20 74 68 6f 73 65 20 66 72 6f 6d 20   are those from 
343d8 70 52 69 67 68 74 2e 0a 2a 2f 0a 73 74 61 74 69  pRight..*/.stati
343d9 63 20 76 6f 69 64 20 64 6f 63 4c 69 73 74 50 68  c void docListPh
343da 72 61 73 65 4d 65 72 67 65 28 0a 20 20 63 6f 6e  raseMerge(.  con
343db 73 74 20 63 68 61 72 20 2a 70 4c 65 66 74 2c 20  st char *pLeft, 
343dc 69 6e 74 20 6e 4c 65 66 74 2c 0a 20 20 63 6f 6e  int nLeft,.  con
343dd 73 74 20 63 68 61 72 20 2a 70 52 69 67 68 74 2c  st char *pRight,
343de 20 69 6e 74 20 6e 52 69 67 68 74 2c 0a 20 20 69   int nRight,.  i
343df 6e 74 20 6e 4e 65 61 72 2c 20 20 20 20 20 20 20  nt nNear,       
343e0 20 20 20 20 20 2f 2a 20 30 20 66 6f 72 20 61 20       /* 0 for a 
343e1 70 68 72 61 73 65 20 6d 65 72 67 65 2c 20 6e 6f  phrase merge, no
343e2 6e 2d 7a 65 72 6f 20 66 6f 72 20 61 20 4e 45 41  n-zero for a NEA
343e3 52 20 6d 65 72 67 65 20 2a 2f 0a 20 20 69 6e 74  R merge */.  int
343e4 20 6e 50 68 72 61 73 65 2c 20 20 20 20 20 20 20   nPhrase,       
343e5 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
343e6 74 6f 6b 65 6e 73 20 69 6e 20 6c 65 66 74 2b 72  tokens in left+r
343e7 69 67 68 74 20 6f 70 65 72 61 6e 64 73 20 74 6f  ight operands to
343e8 20 4e 45 41 52 20 2a 2f 0a 20 20 44 6f 63 4c 69   NEAR */.  DocLi
343e9 73 74 54 79 70 65 20 69 54 79 70 65 2c 20 20 20  stType iType,   
343ea 20 2f 2a 20 54 79 70 65 20 6f 66 20 64 6f 63 6c   /* Type of docl
343eb 69 73 74 20 74 6f 20 77 72 69 74 65 20 74 6f 20  ist to write to 
343ec 70 4f 75 74 20 2a 2f 0a 20 20 44 61 74 61 42 75  pOut */.  DataBu
343ed 66 66 65 72 20 2a 70 4f 75 74 20 20 20 20 20 20  ffer *pOut      
343ee 2f 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f 6d  /* Write the com
343ef 62 69 6e 65 64 20 64 6f 63 6c 69 73 74 20 68 65  bined doclist he
343f0 72 65 20 2a 2f 0a 29 7b 0a 20 20 44 4c 52 65 61  re */.){.  DLRea
343f1 64 65 72 20 6c 65 66 74 2c 20 72 69 67 68 74 3b  der left, right;
343f2 0a 20 20 44 4c 57 72 69 74 65 72 20 77 72 69 74  .  DLWriter writ
343f3 65 72 3b 0a 0a 20 20 69 66 28 20 6e 4c 65 66 74  er;..  if( nLeft
343f4 3d 3d 30 20 7c 7c 20 6e 52 69 67 68 74 3d 3d 30  ==0 || nRight==0
343f5 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 61 73   ) return;..  as
343f6 73 65 72 74 28 20 69 54 79 70 65 21 3d 44 4c 5f  sert( iType!=DL_
343f7 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54  POSITIONS_OFFSET
343f8 53 20 29 3b 0a 0a 20 20 64 6c 72 49 6e 69 74 28  S );..  dlrInit(
343f9 26 6c 65 66 74 2c 20 44 4c 5f 50 4f 53 49 54 49  &left, DL_POSITI
343fa 4f 4e 53 2c 20 70 4c 65 66 74 2c 20 6e 4c 65 66  ONS, pLeft, nLef
343fb 74 29 3b 0a 20 20 64 6c 72 49 6e 69 74 28 26 72  t);.  dlrInit(&r
343fc 69 67 68 74 2c 20 44 4c 5f 50 4f 53 49 54 49 4f  ight, DL_POSITIO
343fd 4e 53 2c 20 70 52 69 67 68 74 2c 20 6e 52 69 67  NS, pRight, nRig
343fe 68 74 29 3b 0a 20 20 64 6c 77 49 6e 69 74 28 26  ht);.  dlwInit(&
343ff 77 72 69 74 65 72 2c 20 69 54 79 70 65 2c 20 70  writer, iType, p
34400 4f 75 74 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20  Out);..  while( 
34401 21 64 6c 72 41 74 45 6e 64 28 26 6c 65 66 74 29  !dlrAtEnd(&left)
34402 20 26 26 20 21 64 6c 72 41 74 45 6e 64 28 26 72   && !dlrAtEnd(&r
34403 69 67 68 74 29 20 29 7b 0a 20 20 20 20 69 66 28  ight) ){.    if(
34404 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29   dlrDocid(&left)
34405 3c 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74  <dlrDocid(&right
34406 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 72 53 74  ) ){.      dlrSt
34407 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d  ep(&left);.    }
34408 65 6c 73 65 20 69 66 28 20 64 6c 72 44 6f 63 69  else if( dlrDoci
34409 64 28 26 72 69 67 68 74 29 3c 64 6c 72 44 6f 63  d(&right)<dlrDoc
3440a 69 64 28 26 6c 65 66 74 29 20 29 7b 0a 20 20 20  id(&left) ){.   
3440b 20 20 20 64 6c 72 53 74 65 70 28 26 72 69 67 68     dlrStep(&righ
3440c 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  t);.    }else{. 
3440d 20 20 20 20 20 69 66 28 20 6e 4e 65 61 72 3d 3d       if( nNear==
3440e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 6f 73  0 ){.        pos
3440f 4c 69 73 74 50 68 72 61 73 65 4d 65 72 67 65 28  ListPhraseMerge(
34410 26 6c 65 66 74 2c 20 26 72 69 67 68 74 2c 20 30  &left, &right, 0
34411 2c 20 30 2c 20 26 77 72 69 74 65 72 29 3b 0a 20  , 0, &writer);. 
34412 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
34413 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61 73 65      /* This case
34414 20 6f 63 63 75 72 73 20 77 68 65 6e 20 74 77 6f   occurs when two
34415 20 74 65 72 6d 73 20 28 73 69 6d 70 6c 65 20 74   terms (simple t
34416 65 72 6d 73 20 6f 72 20 70 68 72 61 73 65 73 29  erms or phrases)
34417 20 61 72 65 0a 20 20 20 20 20 20 20 20 20 2a 20   are.         * 
34418 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 61 20 4e  connected by a N
34419 45 41 52 20 6f 70 65 72 61 74 6f 72 2c 20 73 70  EAR operator, sp
3441a 61 6e 20 28 6e 4e 65 61 72 2b 31 29 2e 20 69 2e  an (nNear+1). i.
3441b 65 2e 0a 20 20 20 20 20 20 20 20 20 2a 0a 20 20  e..         *.  
3441c 20 20 20 20 20 20 20 2a 20 20 20 20 20 27 22 74         *     '"t
3441d 65 72 72 69 62 6c 65 20 63 6f 6d 70 61 6e 79 22  errible company"
3441e 20 4e 45 41 52 20 77 69 64 67 65 74 27 0a 20 20   NEAR widget'.  
3441f 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
34420 20 20 44 61 74 61 42 75 66 66 65 72 20 6f 6e 65    DataBuffer one
34421 20 3d 20 7b 30 2c 20 30 2c 20 30 7d 3b 0a 20 20   = {0, 0, 0};.  
34422 20 20 20 20 20 20 44 61 74 61 42 75 66 66 65 72        DataBuffer
34423 20 74 77 6f 20 3d 20 7b 30 2c 20 30 2c 20 30 7d   two = {0, 0, 0}
34424 3b 0a 0a 20 20 20 20 20 20 20 20 44 4c 57 72 69  ;..        DLWri
34425 74 65 72 20 64 6c 77 72 69 74 65 72 32 3b 0a 20  ter dlwriter2;. 
34426 20 20 20 20 20 20 20 44 4c 52 65 61 64 65 72 20         DLReader 
34427 64 72 31 20 3d 20 7b 30 2c 20 30 2c 20 30 2c 20  dr1 = {0, 0, 0, 
34428 30 2c 20 30 7d 3b 20 0a 20 20 20 20 20 20 20 20  0, 0}; .        
34429 44 4c 52 65 61 64 65 72 20 64 72 32 20 3d 20 7b  DLReader dr2 = {
3442a 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 7d 3b 0a  0, 0, 0, 0, 0};.
3442b 0a 20 20 20 20 20 20 20 20 64 6c 77 49 6e 69 74  .        dlwInit
3442c 28 26 64 6c 77 72 69 74 65 72 32 2c 20 69 54 79  (&dlwriter2, iTy
3442d 70 65 2c 20 26 6f 6e 65 29 3b 0a 20 20 20 20 20  pe, &one);.     
3442e 20 20 20 70 6f 73 4c 69 73 74 50 68 72 61 73 65     posListPhrase
3442f 4d 65 72 67 65 28 26 72 69 67 68 74 2c 20 26 6c  Merge(&right, &l
34430 65 66 74 2c 20 6e 4e 65 61 72 2d 33 2b 6e 50 68  eft, nNear-3+nPh
34431 72 61 73 65 2c 20 31 2c 20 26 64 6c 77 72 69 74  rase, 1, &dlwrit
34432 65 72 32 29 3b 0a 20 20 20 20 20 20 20 20 64 6c  er2);.        dl
34433 77 49 6e 69 74 28 26 64 6c 77 72 69 74 65 72 32  wInit(&dlwriter2
34434 2c 20 69 54 79 70 65 2c 20 26 74 77 6f 29 3b 0a  , iType, &two);.
34435 20 20 20 20 20 20 20 20 70 6f 73 4c 69 73 74 50          posListP
34436 68 72 61 73 65 4d 65 72 67 65 28 26 6c 65 66 74  hraseMerge(&left
34437 2c 20 26 72 69 67 68 74 2c 20 6e 4e 65 61 72 2d  , &right, nNear-
34438 31 2c 20 30 2c 20 26 64 6c 77 72 69 74 65 72 32  1, 0, &dlwriter2
34439 29 3b 0a 0a 20 20 20 20 20 20 20 20 69 66 28 20  );..        if( 
3443a 6f 6e 65 2e 6e 44 61 74 61 29 20 64 6c 72 49 6e  one.nData) dlrIn
3443b 69 74 28 26 64 72 31 2c 20 69 54 79 70 65 2c 20  it(&dr1, iType, 
3443c 6f 6e 65 2e 70 44 61 74 61 2c 20 6f 6e 65 2e 6e  one.pData, one.n
3443d 44 61 74 61 29 3b 0a 20 20 20 20 20 20 20 20 69  Data);.        i
3443e 66 28 20 74 77 6f 2e 6e 44 61 74 61 29 20 64 6c  f( two.nData) dl
3443f 72 49 6e 69 74 28 26 64 72 32 2c 20 69 54 79 70  rInit(&dr2, iTyp
34440 65 2c 20 74 77 6f 2e 70 44 61 74 61 2c 20 74 77  e, two.pData, tw
34441 6f 2e 6e 44 61 74 61 29 3b 0a 0a 20 20 20 20 20  o.nData);..     
34442 20 20 20 69 66 28 20 21 64 6c 72 41 74 45 6e 64     if( !dlrAtEnd
34443 28 26 64 72 31 29 20 7c 7c 20 21 64 6c 72 41 74  (&dr1) || !dlrAt
34444 45 6e 64 28 26 64 72 32 29 20 29 7b 0a 20 20 20  End(&dr2) ){.   
34445 20 20 20 20 20 20 20 50 4c 52 65 61 64 65 72 20         PLReader 
34446 70 72 31 20 3d 20 7b 30 7d 3b 0a 20 20 20 20 20  pr1 = {0};.     
34447 20 20 20 20 20 50 4c 52 65 61 64 65 72 20 70 72       PLReader pr
34448 32 20 3d 20 7b 30 7d 3b 0a 0a 20 20 20 20 20 20  2 = {0};..      
34449 20 20 20 20 50 4c 57 72 69 74 65 72 20 70 6c 77      PLWriter plw
3444a 72 69 74 65 72 3b 0a 20 20 20 20 20 20 20 20 20  riter;.         
3444b 20 70 6c 77 49 6e 69 74 28 26 70 6c 77 72 69 74   plwInit(&plwrit
3444c 65 72 2c 20 26 77 72 69 74 65 72 2c 20 64 6c 72  er, &writer, dlr
3444d 44 6f 63 69 64 28 64 6c 72 41 74 45 6e 64 28 26  Docid(dlrAtEnd(&
3444e 64 72 31 29 3f 26 64 72 32 3a 26 64 72 31 29 29  dr1)?&dr2:&dr1))
3444f 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ;..          if(
34450 20 6f 6e 65 2e 6e 44 61 74 61 20 29 20 70 6c 72   one.nData ) plr
34451 49 6e 69 74 28 26 70 72 31 2c 20 26 64 72 31 29  Init(&pr1, &dr1)
34452 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
34453 74 77 6f 2e 6e 44 61 74 61 20 29 20 70 6c 72 49  two.nData ) plrI
34454 6e 69 74 28 26 70 72 32 2c 20 26 64 72 32 29 3b  nit(&pr2, &dr2);
34455 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65  .          while
34456 28 20 21 70 6c 72 41 74 45 6e 64 28 26 70 72 31  ( !plrAtEnd(&pr1
34457 29 20 7c 7c 20 21 70 6c 72 41 74 45 6e 64 28 26  ) || !plrAtEnd(&
34458 70 72 32 29 20 29 7b 0a 20 20 20 20 20 20 20 20  pr2) ){.        
34459 20 20 20 20 69 6e 74 20 69 43 6f 6d 70 61 72 65      int iCompare
3445a 20 3d 20 70 6c 72 43 6f 6d 70 61 72 65 28 26 70   = plrCompare(&p
3445b 72 31 2c 20 26 70 72 32 29 3b 0a 20 20 20 20 20  r1, &pr2);.     
3445c 20 20 20 20 20 20 20 73 77 69 74 63 68 28 20 69         switch( i
3445d 43 6f 6d 70 61 72 65 20 29 7b 0a 20 20 20 20 20  Compare ){.     
3445e 20 20 20 20 20 20 20 20 20 63 61 73 65 20 2d 31           case -1
3445f 3a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  :.              
34460 20 20 70 6c 77 43 6f 70 79 28 26 70 6c 77 72 69    plwCopy(&plwri
34461 74 65 72 2c 20 26 70 72 31 29 3b 0a 20 20 20 20  ter, &pr1);.    
34462 20 20 20 20 20 20 20 20 20 20 20 20 70 6c 72 53              plrS
34463 74 65 70 28 26 70 72 31 29 3b 0a 20 20 20 20 20  tep(&pr1);.     
34464 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
34465 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
34466 63 61 73 65 20 31 3a 0a 20 20 20 20 20 20 20 20  case 1:.        
34467 20 20 20 20 20 20 20 20 70 6c 77 43 6f 70 79 28          plwCopy(
34468 26 70 6c 77 72 69 74 65 72 2c 20 26 70 72 32 29  &plwriter, &pr2)
34469 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
3446a 20 20 70 6c 72 53 74 65 70 28 26 70 72 32 29 3b    plrStep(&pr2);
3446b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3446c 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
3446d 20 20 20 20 20 20 63 61 73 65 20 30 3a 0a 20 20        case 0:.  
3446e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 6c                pl
3446f 77 43 6f 70 79 28 26 70 6c 77 72 69 74 65 72 2c  wCopy(&plwriter,
34470 20 26 70 72 31 29 3b 0a 20 20 20 20 20 20 20 20   &pr1);.        
34471 20 20 20 20 20 20 20 20 70 6c 72 53 74 65 70 28          plrStep(
34472 26 70 72 31 29 3b 0a 20 20 20 20 20 20 20 20 20  &pr1);.         
34473 20 20 20 20 20 20 20 70 6c 72 53 74 65 70 28 26         plrStep(&
34474 70 72 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20  pr2);.          
34475 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
34476 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
34477 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
34478 20 70 6c 77 54 65 72 6d 69 6e 61 74 65 28 26 70   plwTerminate(&p
34479 6c 77 72 69 74 65 72 29 3b 0a 20 20 20 20 20 20  lwriter);.      
3447a 20 20 7d 0a 20 20 20 20 20 20 20 20 64 61 74 61    }.        data
3447b 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 6f  BufferDestroy(&o
3447c 6e 65 29 3b 0a 20 20 20 20 20 20 20 20 64 61 74  ne);.        dat
3447d 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26  aBufferDestroy(&
3447e 74 77 6f 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  two);.      }.  
3447f 20 20 20 20 64 6c 72 53 74 65 70 28 26 6c 65 66      dlrStep(&lef
34480 74 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65  t);.      dlrSte
34481 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20 7d  p(&right);.    }
34482 0a 20 20 7d 0a 0a 20 20 64 6c 72 44 65 73 74 72  .  }..  dlrDestr
34483 6f 79 28 26 6c 65 66 74 29 3b 0a 20 20 64 6c 72  oy(&left);.  dlr
34484 44 65 73 74 72 6f 79 28 26 72 69 67 68 74 29 3b  Destroy(&right);
34485 0a 20 20 64 6c 77 44 65 73 74 72 6f 79 28 26 77  .  dlwDestroy(&w
34486 72 69 74 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 57 65  riter);.}../* We
34487 20 68 61 76 65 20 74 77 6f 20 44 4c 5f 44 4f 43   have two DL_DOC
34488 49 44 53 20 64 6f 63 6c 69 73 74 73 3a 20 20 70  IDS doclists:  p
34489 4c 65 66 74 20 61 6e 64 20 70 52 69 67 68 74 2e  Left and pRight.
3448a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 69 6e  .** Write the in
3448b 74 65 72 73 65 63 74 69 6f 6e 20 6f 66 20 74 68  tersection of th
3448c 65 73 65 20 74 77 6f 20 64 6f 63 6c 69 73 74 73  ese two doclists
3448d 20 69 6e 74 6f 20 70 4f 75 74 20 61 73 20 61 0a   into pOut as a.
3448e 2a 2a 20 44 4c 5f 44 4f 43 49 44 53 20 64 6f 63  ** DL_DOCIDS doc
3448f 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  list..*/.static 
34490 76 6f 69 64 20 64 6f 63 4c 69 73 74 41 6e 64 4d  void docListAndM
34491 65 72 67 65 28 0a 20 20 63 6f 6e 73 74 20 63 68  erge(.  const ch
34492 61 72 20 2a 70 4c 65 66 74 2c 20 69 6e 74 20 6e  ar *pLeft, int n
34493 4c 65 66 74 2c 0a 20 20 63 6f 6e 73 74 20 63 68  Left,.  const ch
34494 61 72 20 2a 70 52 69 67 68 74 2c 20 69 6e 74 20  ar *pRight, int 
34495 6e 52 69 67 68 74 2c 0a 20 20 44 61 74 61 42 75  nRight,.  DataBu
34496 66 66 65 72 20 2a 70 4f 75 74 20 20 20 20 20 20  ffer *pOut      
34497 2f 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f 6d  /* Write the com
34498 62 69 6e 65 64 20 64 6f 63 6c 69 73 74 20 68 65  bined doclist he
34499 72 65 20 2a 2f 0a 29 7b 0a 20 20 44 4c 52 65 61  re */.){.  DLRea
3449a 64 65 72 20 6c 65 66 74 2c 20 72 69 67 68 74 3b  der left, right;
3449b 0a 20 20 44 4c 57 72 69 74 65 72 20 77 72 69 74  .  DLWriter writ
3449c 65 72 3b 0a 0a 20 20 69 66 28 20 6e 4c 65 66 74  er;..  if( nLeft
3449d 3d 3d 30 20 7c 7c 20 6e 52 69 67 68 74 3d 3d 30  ==0 || nRight==0
3449e 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 64 6c   ) return;..  dl
3449f 72 49 6e 69 74 28 26 6c 65 66 74 2c 20 44 4c 5f  rInit(&left, DL_
344a0 44 4f 43 49 44 53 2c 20 70 4c 65 66 74 2c 20 6e  DOCIDS, pLeft, n
344a1 4c 65 66 74 29 3b 0a 20 20 64 6c 72 49 6e 69 74  Left);.  dlrInit
344a2 28 26 72 69 67 68 74 2c 20 44 4c 5f 44 4f 43 49  (&right, DL_DOCI
344a3 44 53 2c 20 70 52 69 67 68 74 2c 20 6e 52 69 67  DS, pRight, nRig
344a4 68 74 29 3b 0a 20 20 64 6c 77 49 6e 69 74 28 26  ht);.  dlwInit(&
344a5 77 72 69 74 65 72 2c 20 44 4c 5f 44 4f 43 49 44  writer, DL_DOCID
344a6 53 2c 20 70 4f 75 74 29 3b 0a 0a 20 20 77 68 69  S, pOut);..  whi
344a7 6c 65 28 20 21 64 6c 72 41 74 45 6e 64 28 26 6c  le( !dlrAtEnd(&l
344a8 65 66 74 29 20 26 26 20 21 64 6c 72 41 74 45 6e  eft) && !dlrAtEn
344a9 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20  d(&right) ){.   
344aa 20 69 66 28 20 64 6c 72 44 6f 63 69 64 28 26 6c   if( dlrDocid(&l
344ab 65 66 74 29 3c 64 6c 72 44 6f 63 69 64 28 26 72  eft)<dlrDocid(&r
344ac 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20 64  ight) ){.      d
344ad 6c 72 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20  lrStep(&left);. 
344ae 20 20 20 7d 65 6c 73 65 20 69 66 28 20 64 6c 72     }else if( dlr
344af 44 6f 63 69 64 28 26 72 69 67 68 74 29 3c 64 6c  Docid(&right)<dl
344b0 72 44 6f 63 69 64 28 26 6c 65 66 74 29 20 29 7b  rDocid(&left) ){
344b1 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70 28 26  .      dlrStep(&
344b2 72 69 67 68 74 29 3b 0a 20 20 20 20 7d 65 6c 73  right);.    }els
344b3 65 7b 0a 20 20 20 20 20 20 64 6c 77 41 64 64 28  e{.      dlwAdd(
344b4 26 77 72 69 74 65 72 2c 20 64 6c 72 44 6f 63 69  &writer, dlrDoci
344b5 64 28 26 6c 65 66 74 29 29 3b 0a 20 20 20 20 20  d(&left));.     
344b6 20 64 6c 72 53 74 65 70 28 26 6c 65 66 74 29 3b   dlrStep(&left);
344b7 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70 28 26  .      dlrStep(&
344b8 72 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20  right);.    }.  
344b9 7d 0a 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28  }..  dlrDestroy(
344ba 26 6c 65 66 74 29 3b 0a 20 20 64 6c 72 44 65 73  &left);.  dlrDes
344bb 74 72 6f 79 28 26 72 69 67 68 74 29 3b 0a 20 20  troy(&right);.  
344bc 64 6c 77 44 65 73 74 72 6f 79 28 26 77 72 69 74  dlwDestroy(&writ
344bd 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 57 65 20 68 61  er);.}../* We ha
344be 76 65 20 74 77 6f 20 44 4c 5f 44 4f 43 49 44 53  ve two DL_DOCIDS
344bf 20 64 6f 63 6c 69 73 74 73 3a 20 20 70 4c 65 66   doclists:  pLef
344c0 74 20 61 6e 64 20 70 52 69 67 68 74 2e 0a 2a 2a  t and pRight..**
344c1 20 57 72 69 74 65 20 74 68 65 20 75 6e 69 6f 6e   Write the union
344c2 20 6f 66 20 74 68 65 73 65 20 74 77 6f 20 64 6f   of these two do
344c3 63 6c 69 73 74 73 20 69 6e 74 6f 20 70 4f 75 74  clists into pOut
344c4 20 61 73 20 61 0a 2a 2a 20 44 4c 5f 44 4f 43 49   as a.** DL_DOCI
344c5 44 53 20 64 6f 63 6c 69 73 74 2e 0a 2a 2f 0a 73  DS doclist..*/.s
344c6 74 61 74 69 63 20 76 6f 69 64 20 64 6f 63 4c 69  tatic void docLi
344c7 73 74 4f 72 4d 65 72 67 65 28 0a 20 20 63 6f 6e  stOrMerge(.  con
344c8 73 74 20 63 68 61 72 20 2a 70 4c 65 66 74 2c 20  st char *pLeft, 
344c9 69 6e 74 20 6e 4c 65 66 74 2c 0a 20 20 63 6f 6e  int nLeft,.  con
344ca 73 74 20 63 68 61 72 20 2a 70 52 69 67 68 74 2c  st char *pRight,
344cb 20 69 6e 74 20 6e 52 69 67 68 74 2c 0a 20 20 44   int nRight,.  D
344cc 61 74 61 42 75 66 66 65 72 20 2a 70 4f 75 74 20  ataBuffer *pOut 
344cd 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68       /* Write th
344ce 65 20 63 6f 6d 62 69 6e 65 64 20 64 6f 63 6c 69  e combined docli
344cf 73 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20  st here */.){.  
344d0 44 4c 52 65 61 64 65 72 20 6c 65 66 74 2c 20 72  DLReader left, r
344d1 69 67 68 74 3b 0a 20 20 44 4c 57 72 69 74 65 72  ight;.  DLWriter
344d2 20 77 72 69 74 65 72 3b 0a 0a 20 20 69 66 28 20   writer;..  if( 
344d3 6e 4c 65 66 74 3d 3d 30 20 29 7b 0a 20 20 20 20  nLeft==0 ){.    
344d4 69 66 28 20 6e 52 69 67 68 74 21 3d 30 20 29 20  if( nRight!=0 ) 
344d5 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64  dataBufferAppend
344d6 28 70 4f 75 74 2c 20 70 52 69 67 68 74 2c 20 6e  (pOut, pRight, n
344d7 52 69 67 68 74 29 3b 0a 20 20 20 20 72 65 74 75  Right);.    retu
344d8 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 52  rn;.  }.  if( nR
344d9 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 64  ight==0 ){.    d
344da 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28  ataBufferAppend(
344db 70 4f 75 74 2c 20 70 4c 65 66 74 2c 20 6e 4c 65  pOut, pLeft, nLe
344dc 66 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  ft);.    return;
344dd 0a 20 20 7d 0a 0a 20 20 64 6c 72 49 6e 69 74 28  .  }..  dlrInit(
344de 26 6c 65 66 74 2c 20 44 4c 5f 44 4f 43 49 44 53  &left, DL_DOCIDS
344df 2c 20 70 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b  , pLeft, nLeft);
344e0 0a 20 20 64 6c 72 49 6e 69 74 28 26 72 69 67 68  .  dlrInit(&righ
344e1 74 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70 52  t, DL_DOCIDS, pR
344e2 69 67 68 74 2c 20 6e 52 69 67 68 74 29 3b 0a 20  ight, nRight);. 
344e3 20 64 6c 77 49 6e 69 74 28 26 77 72 69 74 65 72   dlwInit(&writer
344e4 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70 4f 75  , DL_DOCIDS, pOu
344e5 74 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 21 64  t);..  while( !d
344e6 6c 72 41 74 45 6e 64 28 26 6c 65 66 74 29 20 7c  lrAtEnd(&left) |
344e7 7c 20 21 64 6c 72 41 74 45 6e 64 28 26 72 69 67  | !dlrAtEnd(&rig
344e8 68 74 29 20 29 7b 0a 20 20 20 20 69 66 28 20 64  ht) ){.    if( d
344e9 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74 29 20  lrAtEnd(&right) 
344ea 29 7b 0a 20 20 20 20 20 20 64 6c 77 41 64 64 28  ){.      dlwAdd(
344eb 26 77 72 69 74 65 72 2c 20 64 6c 72 44 6f 63 69  &writer, dlrDoci
344ec 64 28 26 6c 65 66 74 29 29 3b 0a 20 20 20 20 20  d(&left));.     
344ed 20 64 6c 72 53 74 65 70 28 26 6c 65 66 74 29 3b   dlrStep(&left);
344ee 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 64  .    }else if( d
344ef 6c 72 41 74 45 6e 64 28 26 6c 65 66 74 29 20 29  lrAtEnd(&left) )
344f0 7b 0a 20 20 20 20 20 20 64 6c 77 41 64 64 28 26  {.      dlwAdd(&
344f1 77 72 69 74 65 72 2c 20 64 6c 72 44 6f 63 69 64  writer, dlrDocid
344f2 28 26 72 69 67 68 74 29 29 3b 0a 20 20 20 20 20  (&right));.     
344f3 20 64 6c 72 53 74 65 70 28 26 72 69 67 68 74 29   dlrStep(&right)
344f4 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
344f5 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29 3c  dlrDocid(&left)<
344f6 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74 29  dlrDocid(&right)
344f7 20 29 7b 0a 20 20 20 20 20 20 64 6c 77 41 64 64   ){.      dlwAdd
344f8 28 26 77 72 69 74 65 72 2c 20 64 6c 72 44 6f 63  (&writer, dlrDoc
344f9 69 64 28 26 6c 65 66 74 29 29 3b 0a 20 20 20 20  id(&left));.    
344fa 20 20 64 6c 72 53 74 65 70 28 26 6c 65 66 74 29    dlrStep(&left)
344fb 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
344fc 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74 29  dlrDocid(&right)
344fd 3c 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29  <dlrDocid(&left)
344fe 20 29 7b 0a 20 20 20 20 20 20 64 6c 77 41 64 64   ){.      dlwAdd
344ff 28 26 77 72 69 74 65 72 2c 20 64 6c 72 44 6f 63  (&writer, dlrDoc
34500 69 64 28 26 72 69 67 68 74 29 29 3b 0a 20 20 20  id(&right));.   
34501 20 20 20 64 6c 72 53 74 65 70 28 26 72 69 67 68     dlrStep(&righ
34502 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  t);.    }else{. 
34503 20 20 20 20 20 64 6c 77 41 64 64 28 26 77 72 69       dlwAdd(&wri
34504 74 65 72 2c 20 64 6c 72 44 6f 63 69 64 28 26 6c  ter, dlrDocid(&l
34505 65 66 74 29 29 3b 0a 20 20 20 20 20 20 64 6c 72  eft));.      dlr
34506 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20  Step(&left);.   
34507 20 20 20 64 6c 72 53 74 65 70 28 26 72 69 67 68     dlrStep(&righ
34508 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  t);.    }.  }.. 
34509 20 64 6c 72 44 65 73 74 72 6f 79 28 26 6c 65 66   dlrDestroy(&lef
3450a 74 29 3b 0a 20 20 64 6c 72 44 65 73 74 72 6f 79  t);.  dlrDestroy
3450b 28 26 72 69 67 68 74 29 3b 0a 20 20 64 6c 77 44  (&right);.  dlwD
3450c 65 73 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b  estroy(&writer);
3450d 0a 7d 0a 0a 2f 2a 20 57 65 20 68 61 76 65 20 74  .}../* We have t
3450e 77 6f 20 44 4c 5f 44 4f 43 49 44 53 20 64 6f 63  wo DL_DOCIDS doc
3450f 6c 69 73 74 73 3a 20 20 70 4c 65 66 74 20 61 6e  lists:  pLeft an
34510 64 20 70 52 69 67 68 74 2e 0a 2a 2a 20 57 72 69  d pRight..** Wri
34511 74 65 20 69 6e 74 6f 20 70 4f 75 74 20 61 73 20  te into pOut as 
34512 44 4c 5f 44 4f 43 49 44 53 20 64 6f 63 6c 69 73  DL_DOCIDS doclis
34513 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6c 6c  t containing all
34514 20 64 6f 63 75 6d 65 6e 74 73 20 74 68 61 74 0a   documents that.
34515 2a 2a 20 6f 63 63 75 72 20 69 6e 20 70 4c 65 66  ** occur in pLef
34516 74 20 62 75 74 20 6e 6f 74 20 69 6e 20 70 52 69  t but not in pRi
34517 67 68 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ght..*/.static v
34518 6f 69 64 20 64 6f 63 4c 69 73 74 45 78 63 65 70  oid docListExcep
34519 74 4d 65 72 67 65 28 0a 20 20 63 6f 6e 73 74 20  tMerge(.  const 
3451a 63 68 61 72 20 2a 70 4c 65 66 74 2c 20 69 6e 74  char *pLeft, int
3451b 20 6e 4c 65 66 74 2c 0a 20 20 63 6f 6e 73 74 20   nLeft,.  const 
3451c 63 68 61 72 20 2a 70 52 69 67 68 74 2c 20 69 6e  char *pRight, in
3451d 74 20 6e 52 69 67 68 74 2c 0a 20 20 44 61 74 61  t nRight,.  Data
3451e 42 75 66 66 65 72 20 2a 70 4f 75 74 20 20 20 20  Buffer *pOut    
3451f 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 63    /* Write the c
34520 6f 6d 62 69 6e 65 64 20 64 6f 63 6c 69 73 74 20  ombined doclist 
34521 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 44 4c 52  here */.){.  DLR
34522 65 61 64 65 72 20 6c 65 66 74 2c 20 72 69 67 68  eader left, righ
34523 74 3b 0a 20 20 44 4c 57 72 69 74 65 72 20 77 72  t;.  DLWriter wr
34524 69 74 65 72 3b 0a 0a 20 20 69 66 28 20 6e 4c 65  iter;..  if( nLe
34525 66 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  ft==0 ) return;.
34526 20 20 69 66 28 20 6e 52 69 67 68 74 3d 3d 30 20    if( nRight==0 
34527 29 7b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65  ){.    dataBuffe
34528 72 41 70 70 65 6e 64 28 70 4f 75 74 2c 20 70 4c  rAppend(pOut, pL
34529 65 66 74 2c 20 6e 4c 65 66 74 29 3b 0a 20 20 20  eft, nLeft);.   
3452a 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20   return;.  }..  
3452b 64 6c 72 49 6e 69 74 28 26 6c 65 66 74 2c 20 44  dlrInit(&left, D
3452c 4c 5f 44 4f 43 49 44 53 2c 20 70 4c 65 66 74 2c  L_DOCIDS, pLeft,
3452d 20 6e 4c 65 66 74 29 3b 0a 20 20 64 6c 72 49 6e   nLeft);.  dlrIn
3452e 69 74 28 26 72 69 67 68 74 2c 20 44 4c 5f 44 4f  it(&right, DL_DO
3452f 43 49 44 53 2c 20 70 52 69 67 68 74 2c 20 6e 52  CIDS, pRight, nR
34530 69 67 68 74 29 3b 0a 20 20 64 6c 77 49 6e 69 74  ight);.  dlwInit
34531 28 26 77 72 69 74 65 72 2c 20 44 4c 5f 44 4f 43  (&writer, DL_DOC
34532 49 44 53 2c 20 70 4f 75 74 29 3b 0a 0a 20 20 77  IDS, pOut);..  w
34533 68 69 6c 65 28 20 21 64 6c 72 41 74 45 6e 64 28  hile( !dlrAtEnd(
34534 26 6c 65 66 74 29 20 29 7b 0a 20 20 20 20 77 68  &left) ){.    wh
34535 69 6c 65 28 20 21 64 6c 72 41 74 45 6e 64 28 26  ile( !dlrAtEnd(&
34536 72 69 67 68 74 29 20 26 26 20 64 6c 72 44 6f 63  right) && dlrDoc
34537 69 64 28 26 72 69 67 68 74 29 3c 64 6c 72 44 6f  id(&right)<dlrDo
34538 63 69 64 28 26 6c 65 66 74 29 20 29 7b 0a 20 20  cid(&left) ){.  
34539 20 20 20 20 64 6c 72 53 74 65 70 28 26 72 69 67      dlrStep(&rig
3453a 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ht);.    }.    i
3453b 66 28 20 64 6c 72 41 74 45 6e 64 28 26 72 69 67  f( dlrAtEnd(&rig
3453c 68 74 29 20 7c 7c 20 64 6c 72 44 6f 63 69 64 28  ht) || dlrDocid(
3453d 26 6c 65 66 74 29 3c 64 6c 72 44 6f 63 69 64 28  &left)<dlrDocid(
3453e 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20  &right) ){.     
3453f 20 64 6c 77 41 64 64 28 26 77 72 69 74 65 72 2c   dlwAdd(&writer,
34540 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29   dlrDocid(&left)
34541 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 6c 72  );.    }.    dlr
34542 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 7d  Step(&left);.  }
34543 0a 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26  ..  dlrDestroy(&
34544 6c 65 66 74 29 3b 0a 20 20 64 6c 72 44 65 73 74  left);.  dlrDest
34545 72 6f 79 28 26 72 69 67 68 74 29 3b 0a 20 20 64  roy(&right);.  d
34546 6c 77 44 65 73 74 72 6f 79 28 26 77 72 69 74 65  lwDestroy(&write
34547 72 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 63 68  r);.}..static ch
34548 61 72 20 2a 73 74 72 69 6e 67 5f 64 75 70 5f 6e  ar *string_dup_n
34549 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 2c 20  (const char *s, 
3454a 69 6e 74 20 6e 29 7b 0a 20 20 63 68 61 72 20 2a  int n){.  char *
3454b 73 74 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  str = sqlite3_ma
3454c 6c 6c 6f 63 28 6e 20 2b 20 31 29 3b 0a 20 20 6d  lloc(n + 1);.  m
3454d 65 6d 63 70 79 28 73 74 72 2c 20 73 2c 20 6e 29  emcpy(str, s, n)
3454e 3b 0a 20 20 73 74 72 5b 6e 5d 20 3d 20 27 5c 30  ;.  str[n] = '\0
3454f 27 3b 0a 20 20 72 65 74 75 72 6e 20 73 74 72 3b  ';.  return str;
34550 0a 7d 0a 0a 2f 2a 20 44 75 70 6c 69 63 61 74 65  .}../* Duplicate
34551 20 61 20 73 74 72 69 6e 67 3b 20 74 68 65 20 63   a string; the c
34552 61 6c 6c 65 72 20 6d 75 73 74 20 66 72 65 65 28  aller must free(
34553 29 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73  ) the returned s
34554 74 72 69 6e 67 2e 0a 20 2a 20 28 57 65 20 64 6f  tring.. * (We do
34555 6e 27 74 20 75 73 65 20 73 74 72 64 75 70 28 29  n't use strdup()
34556 20 73 69 6e 63 65 20 69 74 20 69 73 20 6e 6f 74   since it is not
34557 20 70 61 72 74 20 6f 66 20 74 68 65 20 73 74 61   part of the sta
34558 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72 79 20  ndard C library 
34559 61 6e 64 0a 20 2a 20 6d 61 79 20 6e 6f 74 20 62  and. * may not b
3455a 65 20 61 76 61 69 6c 61 62 6c 65 20 65 76 65 72  e available ever
3455b 79 77 68 65 72 65 2e 29 20 2a 2f 0a 73 74 61 74  ywhere.) */.stat
3455c 69 63 20 63 68 61 72 20 2a 73 74 72 69 6e 67 5f  ic char *string_
3455d 64 75 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  dup(const char *
3455e 73 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 74 72  s){.  return str
3455f 69 6e 67 5f 64 75 70 5f 6e 28 73 2c 20 73 74 72  ing_dup_n(s, str
34560 6c 65 6e 28 73 29 29 3b 0a 7d 0a 0a 2f 2a 20 46  len(s));.}../* F
34561 6f 72 6d 61 74 20 61 20 73 74 72 69 6e 67 2c 20  ormat a string, 
34562 72 65 70 6c 61 63 69 6e 67 20 65 61 63 68 20 6f  replacing each o
34563 63 63 75 72 72 65 6e 63 65 20 6f 66 20 74 68 65  ccurrence of the
34564 20 25 20 63 68 61 72 61 63 74 65 72 20 77 69 74   % character wit
34565 68 0a 20 2a 20 7a 44 62 2e 7a 4e 61 6d 65 2e 20  h. * zDb.zName. 
34566 20 54 68 69 73 20 6d 61 79 20 62 65 20 6d 6f 72   This may be mor
34567 65 20 63 6f 6e 76 65 6e 69 65 6e 74 20 74 68 61  e convenient tha
34568 6e 20 73 71 6c 69 74 65 5f 6d 70 72 69 6e 74 66  n sqlite_mprintf
34569 28 29 0a 20 2a 20 77 68 65 6e 20 6f 6e 65 20 73  (). * when one s
3456a 74 72 69 6e 67 20 69 73 20 75 73 65 64 20 72 65  tring is used re
3456b 70 65 61 74 65 64 6c 79 20 69 6e 20 61 20 66 6f  peatedly in a fo
3456c 72 6d 61 74 20 73 74 72 69 6e 67 2e 0a 20 2a 20  rmat string.. * 
3456d 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20  The caller must 
3456e 66 72 65 65 28 29 20 74 68 65 20 72 65 74 75 72  free() the retur
3456f 6e 65 64 20 73 74 72 69 6e 67 2e 20 2a 2f 0a 73  ned string. */.s
34570 74 61 74 69 63 20 63 68 61 72 20 2a 73 74 72 69  tatic char *stri
34571 6e 67 5f 66 6f 72 6d 61 74 28 63 6f 6e 73 74 20  ng_format(const 
34572 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 0a 20  char *zFormat,. 
34573 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34574 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
34575 63 68 61 72 20 2a 7a 44 62 2c 20 63 6f 6e 73 74  char *zDb, const
34576 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20   char *zName){. 
34577 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 3b 0a   const char *p;.
34578 20 20 73 69 7a 65 5f 74 20 6c 65 6e 20 3d 20 30    size_t len = 0
34579 3b 0a 20 20 73 69 7a 65 5f 74 20 6e 44 62 20 3d  ;.  size_t nDb =
3457a 20 73 74 72 6c 65 6e 28 7a 44 62 29 3b 0a 20 20   strlen(zDb);.  
3457b 73 69 7a 65 5f 74 20 6e 4e 61 6d 65 20 3d 20 73  size_t nName = s
3457c 74 72 6c 65 6e 28 7a 4e 61 6d 65 29 3b 0a 20 20  trlen(zName);.  
3457d 73 69 7a 65 5f 74 20 6e 46 75 6c 6c 54 61 62 6c  size_t nFullTabl
3457e 65 4e 61 6d 65 20 3d 20 6e 44 62 2b 31 2b 6e 4e  eName = nDb+1+nN
3457f 61 6d 65 3b 0a 20 20 63 68 61 72 20 2a 72 65 73  ame;.  char *res
34580 75 6c 74 3b 0a 20 20 63 68 61 72 20 2a 72 3b 0a  ult;.  char *r;.
34581 0a 20 20 2f 2a 20 66 69 72 73 74 20 63 6f 6d 70  .  /* first comp
34582 75 74 65 20 6c 65 6e 67 74 68 20 6e 65 65 64 65  ute length neede
34583 64 20 2a 2f 0a 20 20 66 6f 72 28 70 20 3d 20 7a  d */.  for(p = z
34584 46 6f 72 6d 61 74 20 3b 20 2a 70 20 3b 20 2b 2b  Format ; *p ; ++
34585 70 29 7b 0a 20 20 20 20 6c 65 6e 20 2b 3d 20 28  p){.    len += (
34586 2a 70 3d 3d 27 25 27 20 3f 20 6e 46 75 6c 6c 54  *p=='%' ? nFullT
34587 61 62 6c 65 4e 61 6d 65 20 3a 20 31 29 3b 0a 20  ableName : 1);. 
34588 20 7d 0a 20 20 6c 65 6e 20 2b 3d 20 31 3b 20 20   }.  len += 1;  
34589 2f 2a 20 66 6f 72 20 6e 75 6c 6c 20 74 65 72 6d  /* for null term
3458a 69 6e 61 74 6f 72 20 2a 2f 0a 0a 20 20 72 20 3d  inator */..  r =
3458b 20 72 65 73 75 6c 74 20 3d 20 73 71 6c 69 74 65   result = sqlite
3458c 33 5f 6d 61 6c 6c 6f 63 28 6c 65 6e 29 3b 0a 20  3_malloc(len);. 
3458d 20 66 6f 72 28 70 20 3d 20 7a 46 6f 72 6d 61 74   for(p = zFormat
3458e 3b 20 2a 70 3b 20 2b 2b 70 29 7b 0a 20 20 20 20  ; *p; ++p){.    
3458f 69 66 28 20 2a 70 3d 3d 27 25 27 20 29 7b 0a 20  if( *p=='%' ){. 
34590 20 20 20 20 20 6d 65 6d 63 70 79 28 72 2c 20 7a       memcpy(r, z
34591 44 62 2c 20 6e 44 62 29 3b 0a 20 20 20 20 20 20  Db, nDb);.      
34592 72 20 2b 3d 20 6e 44 62 3b 0a 20 20 20 20 20 20  r += nDb;.      
34593 2a 72 2b 2b 20 3d 20 27 2e 27 3b 0a 20 20 20 20  *r++ = '.';.    
34594 20 20 6d 65 6d 63 70 79 28 72 2c 20 7a 4e 61 6d    memcpy(r, zNam
34595 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 20  e, nName);.     
34596 20 72 20 2b 3d 20 6e 4e 61 6d 65 3b 0a 20 20 20   r += nName;.   
34597 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20   } else {.      
34598 2a 72 2b 2b 20 3d 20 2a 70 3b 0a 20 20 20 20 7d  *r++ = *p;.    }
34599 0a 20 20 7d 0a 20 20 2a 72 2b 2b 20 3d 20 27 5c  .  }.  *r++ = '\
3459a 30 27 3b 0a 20 20 61 73 73 65 72 74 28 20 72 20  0';.  assert( r 
3459b 3d 3d 20 72 65 73 75 6c 74 20 2b 20 6c 65 6e 20  == result + len 
3459c 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 73 75  );.  return resu
3459d 6c 74 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e  lt;.}..static in
3459e 74 20 73 71 6c 5f 65 78 65 63 28 73 71 6c 69 74  t sql_exec(sqlit
3459f 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68  e3 *db, const ch
345a0 61 72 20 2a 7a 44 62 2c 20 63 6f 6e 73 74 20 63  ar *zDb, const c
345a1 68 61 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 20 20  har *zName,.    
345a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
345a3 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72  const char *zFor
345a4 6d 61 74 29 7b 0a 20 20 63 68 61 72 20 2a 7a 43  mat){.  char *zC
345a5 6f 6d 6d 61 6e 64 20 3d 20 73 74 72 69 6e 67 5f  ommand = string_
345a6 66 6f 72 6d 61 74 28 7a 46 6f 72 6d 61 74 2c 20  format(zFormat, 
345a7 7a 44 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 69  zDb, zName);.  i
345a8 6e 74 20 72 63 3b 0a 20 20 46 54 53 54 52 41 43  nt rc;.  FTSTRAC
345a9 45 28 28 22 46 54 53 33 20 73 71 6c 3a 20 25 73  E(("FTS3 sql: %s
345aa 5c 6e 22 2c 20 7a 43 6f 6d 6d 61 6e 64 29 29 3b  \n", zCommand));
345ab 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
345ac 65 78 65 63 28 64 62 2c 20 7a 43 6f 6d 6d 61 6e  exec(db, zComman
345ad 64 2c 20 4e 55 4c 4c 2c 20 30 2c 20 4e 55 4c 4c  d, NULL, 0, NULL
345ae 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
345af 65 28 7a 43 6f 6d 6d 61 6e 64 29 3b 0a 20 20 72  e(zCommand);.  r
345b0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61  eturn rc;.}..sta
345b1 74 69 63 20 69 6e 74 20 73 71 6c 5f 70 72 65 70  tic int sql_prep
345b2 61 72 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  are(sqlite3 *db,
345b3 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
345b4 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  , const char *zN
345b5 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ame,.           
345b6 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
345b7 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d  te3_stmt **ppStm
345b8 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  t, const char *z
345b9 46 6f 72 6d 61 74 29 7b 0a 20 20 63 68 61 72 20  Format){.  char 
345ba 2a 7a 43 6f 6d 6d 61 6e 64 20 3d 20 73 74 72 69  *zCommand = stri
345bb 6e 67 5f 66 6f 72 6d 61 74 28 7a 46 6f 72 6d 61  ng_format(zForma
345bc 74 2c 20 7a 44 62 2c 20 7a 4e 61 6d 65 29 3b 0a  t, zDb, zName);.
345bd 20 20 69 6e 74 20 72 63 3b 0a 20 20 46 54 53 54    int rc;.  FTST
345be 52 41 43 45 28 28 22 46 54 53 33 20 70 72 65 70  RACE(("FTS3 prep
345bf 61 72 65 3a 20 25 73 5c 6e 22 2c 20 7a 43 6f 6d  are: %s\n", zCom
345c0 6d 61 6e 64 29 29 3b 0a 20 20 72 63 20 3d 20 73  mand));.  rc = s
345c1 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
345c2 32 28 64 62 2c 20 7a 43 6f 6d 6d 61 6e 64 2c 20  2(db, zCommand, 
345c3 2d 31 2c 20 70 70 53 74 6d 74 2c 20 4e 55 4c 4c  -1, ppStmt, NULL
345c4 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
345c5 65 28 7a 43 6f 6d 6d 61 6e 64 29 3b 0a 20 20 72  e(zCommand);.  r
345c6 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20  eturn rc;.}../* 
345c7 65 6e 64 20 75 74 69 6c 69 74 79 20 66 75 6e 63  end utility func
345c8 74 69 6f 6e 73 20 2a 2f 0a 0a 2f 2a 20 46 6f 72  tions */../* For
345c9 77 61 72 64 20 72 65 66 65 72 65 6e 63 65 20 2a  ward reference *
345ca 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
345cb 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 66   fulltext_vtab f
345cc 75 6c 6c 74 65 78 74 5f 76 74 61 62 3b 0a 0a 2f  ulltext_vtab;../
345cd 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
345ce 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
345cf 67 20 73 74 72 75 63 74 75 72 65 20 6b 65 65 70  g structure keep
345d0 73 20 74 72 61 63 6b 20 6f 66 20 67 65 6e 65 72  s track of gener
345d1 61 74 65 64 0a 2a 2a 20 6d 61 74 63 68 69 6e 67  ated.** matching
345d2 2d 77 6f 72 64 20 6f 66 66 73 65 74 20 69 6e 66  -word offset inf
345d3 6f 72 6d 61 74 69 6f 6e 20 61 6e 64 20 73 6e 69  ormation and sni
345d4 70 70 65 74 73 2e 0a 2a 2f 0a 74 79 70 65 64 65  ppets..*/.typede
345d5 66 20 73 74 72 75 63 74 20 53 6e 69 70 70 65 74  f struct Snippet
345d6 20 7b 0a 20 20 69 6e 74 20 6e 4d 61 74 63 68 3b   {.  int nMatch;
345d7 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75       /* Total nu
345d8 6d 62 65 72 20 6f 66 20 6d 61 74 63 68 65 73 20  mber of matches 
345d9 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b  */.  int nAlloc;
345da 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 61 6c       /* Space al
345db 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 4d 61 74  located for aMat
345dc 63 68 5b 5d 20 2a 2f 0a 20 20 73 74 72 75 63 74  ch[] */.  struct
345dd 20 73 6e 69 70 70 65 74 4d 61 74 63 68 20 7b 20   snippetMatch { 
345de 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72  /* One entry for
345df 20 65 61 63 68 20 6d 61 74 63 68 69 6e 67 20 74   each matching t
345e0 65 72 6d 20 2a 2f 0a 20 20 20 20 63 68 61 72 20  erm */.    char 
345e1 73 6e 53 74 61 74 75 73 3b 20 20 20 20 20 20 20  snStatus;       
345e2 2f 2a 20 53 74 61 74 75 73 20 66 6c 61 67 20 66  /* Status flag f
345e3 6f 72 20 75 73 65 20 77 68 69 6c 65 20 63 6f 6e  or use while con
345e4 73 74 72 75 63 74 69 6e 67 20 73 6e 69 70 70 65  structing snippe
345e5 74 73 20 2a 2f 0a 20 20 20 20 73 68 6f 72 74 20  ts */.    short 
345e6 69 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 2f  int iCol;      /
345e7 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 74 68 61  * The column tha
345e8 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6d  t contains the m
345e9 61 74 63 68 20 2a 2f 0a 20 20 20 20 73 68 6f 72  atch */.    shor
345ea 74 20 69 6e 74 20 69 54 65 72 6d 3b 20 20 20 20  t int iTerm;    
345eb 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 69 6e   /* The index in
345ec 20 51 75 65 72 79 2e 70 54 65 72 6d 73 5b 5d 20   Query.pTerms[] 
345ed 6f 66 20 74 68 65 20 6d 61 74 63 68 69 6e 67 20  of the matching 
345ee 74 65 72 6d 20 2a 2f 0a 20 20 20 20 69 6e 74 20  term */.    int 
345ef 69 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20  iToken;         
345f0 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 6f 66   /* The index of
345f1 20 74 68 65 20 6d 61 74 63 68 69 6e 67 20 64 6f   the matching do
345f2 63 75 6d 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a  cument token */.
345f3 20 20 20 20 73 68 6f 72 74 20 69 6e 74 20 6e 42      short int nB
345f4 79 74 65 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62  yte;     /* Numb
345f5 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74  er of bytes in t
345f6 68 65 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20 69  he term */.    i
345f7 6e 74 20 69 53 74 61 72 74 3b 20 20 20 20 20 20  nt iStart;      
345f8 20 20 20 20 2f 2a 20 54 68 65 20 6f 66 66 73 65      /* The offse
345f9 74 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 63  t to the first c
345fa 68 61 72 61 63 74 65 72 20 6f 66 20 74 68 65 20  haracter of the 
345fb 74 65 72 6d 20 2a 2f 0a 20 20 7d 20 2a 61 4d 61  term */.  } *aMa
345fc 74 63 68 3b 20 20 20 20 20 20 2f 2a 20 50 6f 69  tch;      /* Poi
345fd 6e 74 73 20 74 6f 20 73 70 61 63 65 20 6f 62 74  nts to space obt
345fe 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f  ained from mallo
345ff 63 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4f 66  c */.  char *zOf
34600 66 73 65 74 3b 20 20 2f 2a 20 54 65 78 74 20 72  fset;  /* Text r
34601 65 6e 64 65 72 69 6e 67 20 6f 66 20 61 4d 61 74  endering of aMat
34602 63 68 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f  ch[] */.  int nO
34603 66 66 73 65 74 3b 20 20 20 20 2f 2a 20 73 74 72  ffset;    /* str
34604 6c 65 6e 28 7a 4f 66 66 73 65 74 29 20 2a 2f 0a  len(zOffset) */.
34605 20 20 63 68 61 72 20 2a 7a 53 6e 69 70 70 65 74    char *zSnippet
34606 3b 20 2f 2a 20 53 6e 69 70 70 65 74 20 74 65 78  ; /* Snippet tex
34607 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 6e 69 70  t */.  int nSnip
34608 70 65 74 3b 20 20 20 2f 2a 20 73 74 72 6c 65 6e  pet;   /* strlen
34609 28 7a 53 6e 69 70 70 65 74 29 20 2a 2f 0a 7d 20  (zSnippet) */.} 
3460a 53 6e 69 70 70 65 74 3b 0a 0a 0a 74 79 70 65 64  Snippet;...typed
3460b 65 66 20 65 6e 75 6d 20 51 75 65 72 79 54 79 70  ef enum QueryTyp
3460c 65 20 7b 0a 20 20 51 55 45 52 59 5f 47 45 4e 45  e {.  QUERY_GENE
3460d 52 49 43 2c 20 20 20 2f 2a 20 74 61 62 6c 65 20  RIC,   /* table 
3460e 73 63 61 6e 20 2a 2f 0a 20 20 51 55 45 52 59 5f  scan */.  QUERY_
3460f 44 4f 43 49 44 2c 20 20 20 20 20 2f 2a 20 6c 6f  DOCID,     /* lo
34610 6f 6b 75 70 20 62 79 20 64 6f 63 69 64 20 2a 2f  okup by docid */
34611 0a 20 20 51 55 45 52 59 5f 46 55 4c 4c 54 45 58  .  QUERY_FULLTEX
34612 54 20 20 20 2f 2a 20 51 55 45 52 59 5f 46 55 4c  T   /* QUERY_FUL
34613 4c 54 45 58 54 20 2b 20 5b 69 5d 20 69 73 20 61  LTEXT + [i] is a
34614 20 66 75 6c 6c 2d 74 65 78 74 20 73 65 61 72 63   full-text searc
34615 68 20 66 6f 72 20 63 6f 6c 75 6d 6e 20 69 2a 2f  h for column i*/
34616 0a 7d 20 51 75 65 72 79 54 79 70 65 3b 0a 0a 74  .} QueryType;..t
34617 79 70 65 64 65 66 20 65 6e 75 6d 20 66 75 6c 6c  ypedef enum full
34618 74 65 78 74 5f 73 74 61 74 65 6d 65 6e 74 20 7b  text_statement {
34619 0a 20 20 43 4f 4e 54 45 4e 54 5f 49 4e 53 45 52  .  CONTENT_INSER
3461a 54 5f 53 54 4d 54 2c 0a 20 20 43 4f 4e 54 45 4e  T_STMT,.  CONTEN
3461b 54 5f 53 45 4c 45 43 54 5f 53 54 4d 54 2c 0a 20  T_SELECT_STMT,. 
3461c 20 43 4f 4e 54 45 4e 54 5f 55 50 44 41 54 45 5f   CONTENT_UPDATE_
3461d 53 54 4d 54 2c 0a 20 20 43 4f 4e 54 45 4e 54 5f  STMT,.  CONTENT_
3461e 44 45 4c 45 54 45 5f 53 54 4d 54 2c 0a 20 20 43  DELETE_STMT,.  C
3461f 4f 4e 54 45 4e 54 5f 45 58 49 53 54 53 5f 53 54  ONTENT_EXISTS_ST
34620 4d 54 2c 0a 0a 20 20 42 4c 4f 43 4b 5f 49 4e 53  MT,..  BLOCK_INS
34621 45 52 54 5f 53 54 4d 54 2c 0a 20 20 42 4c 4f 43  ERT_STMT,.  BLOC
34622 4b 5f 53 45 4c 45 43 54 5f 53 54 4d 54 2c 0a 20  K_SELECT_STMT,. 
34623 20 42 4c 4f 43 4b 5f 44 45 4c 45 54 45 5f 53 54   BLOCK_DELETE_ST
34624 4d 54 2c 0a 20 20 42 4c 4f 43 4b 5f 44 45 4c 45  MT,.  BLOCK_DELE
34625 54 45 5f 41 4c 4c 5f 53 54 4d 54 2c 0a 0a 20 20  TE_ALL_STMT,..  
34626 53 45 47 44 49 52 5f 4d 41 58 5f 49 4e 44 45 58  SEGDIR_MAX_INDEX
34627 5f 53 54 4d 54 2c 0a 20 20 53 45 47 44 49 52 5f  _STMT,.  SEGDIR_
34628 53 45 54 5f 53 54 4d 54 2c 0a 20 20 53 45 47 44  SET_STMT,.  SEGD
34629 49 52 5f 53 45 4c 45 43 54 5f 4c 45 56 45 4c 5f  IR_SELECT_LEVEL_
3462a 53 54 4d 54 2c 0a 20 20 53 45 47 44 49 52 5f 53  STMT,.  SEGDIR_S
3462b 50 41 4e 5f 53 54 4d 54 2c 0a 20 20 53 45 47 44  PAN_STMT,.  SEGD
3462c 49 52 5f 44 45 4c 45 54 45 5f 53 54 4d 54 2c 0a  IR_DELETE_STMT,.
3462d 20 20 53 45 47 44 49 52 5f 53 45 4c 45 43 54 5f    SEGDIR_SELECT_
3462e 53 45 47 4d 45 4e 54 5f 53 54 4d 54 2c 0a 20 20  SEGMENT_STMT,.  
3462f 53 45 47 44 49 52 5f 53 45 4c 45 43 54 5f 41 4c  SEGDIR_SELECT_AL
34630 4c 5f 53 54 4d 54 2c 0a 20 20 53 45 47 44 49 52  L_STMT,.  SEGDIR
34631 5f 44 45 4c 45 54 45 5f 41 4c 4c 5f 53 54 4d 54  _DELETE_ALL_STMT
34632 2c 0a 20 20 53 45 47 44 49 52 5f 43 4f 55 4e 54  ,.  SEGDIR_COUNT
34633 5f 53 54 4d 54 2c 0a 0a 20 20 4d 41 58 5f 53 54  _STMT,..  MAX_ST
34634 4d 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  MT              
34635 20 20 20 20 20 20 20 2f 2a 20 41 6c 77 61 79 73         /* Always
34636 20 61 74 20 65 6e 64 21 20 2a 2f 0a 7d 20 66 75   at end! */.} fu
34637 6c 6c 74 65 78 74 5f 73 74 61 74 65 6d 65 6e 74  lltext_statement
34638 3b 0a 0a 2f 2a 20 54 68 65 73 65 20 6d 75 73 74  ;../* These must
34639 20 65 78 61 63 74 6c 79 20 6d 61 74 63 68 20 74   exactly match t
3463a 68 65 20 65 6e 75 6d 20 61 62 6f 76 65 2e 20 2a  he enum above. *
3463b 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  /./* TODO(shess)
3463c 3a 20 49 73 20 74 68 65 72 65 20 73 6f 6d 65 20  : Is there some 
3463d 72 69 73 6b 20 74 68 61 74 20 61 20 73 74 61 74  risk that a stat
3463e 65 6d 65 6e 74 20 77 69 6c 6c 20 62 65 20 75 73  ement will be us
3463f 65 64 20 69 6e 20 74 77 6f 0a 2a 2a 20 63 75 72  ed in two.** cur
34640 73 6f 72 73 20 61 74 20 6f 6e 63 65 2c 20 65 2e  sors at once, e.
34641 67 2e 20 20 69 66 20 61 20 71 75 65 72 79 20 6a  g.  if a query j
34642 6f 69 6e 73 20 61 20 76 69 72 74 75 61 6c 20 74  oins a virtual t
34643 61 62 6c 65 20 74 6f 20 69 74 73 65 6c 66 3f 0a  able to itself?.
34644 2a 2a 20 49 66 20 73 6f 20 70 65 72 68 61 70 73  ** If so perhaps
34645 20 77 65 20 73 68 6f 75 6c 64 20 6d 6f 76 65 20   we should move 
34646 73 6f 6d 65 20 6f 66 20 74 68 65 73 65 20 74 6f  some of these to
34647 20 74 68 65 20 63 75 72 73 6f 72 20 6f 62 6a 65   the cursor obje
34648 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f  ct..*/.static co
34649 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 20  nst char *const 
3464a 66 75 6c 6c 74 65 78 74 5f 7a 53 74 61 74 65 6d  fulltext_zStatem
3464b 65 6e 74 5b 4d 41 58 5f 53 54 4d 54 5d 20 3d 20  ent[MAX_STMT] = 
3464c 7b 0a 20 20 2f 2a 20 43 4f 4e 54 45 4e 54 5f 49  {.  /* CONTENT_I
3464d 4e 53 45 52 54 20 2a 2f 20 4e 55 4c 4c 2c 20 20  NSERT */ NULL,  
3464e 2f 2a 20 67 65 6e 65 72 61 74 65 64 20 69 6e 20  /* generated in 
3464f 63 6f 6e 74 65 6e 74 49 6e 73 65 72 74 53 74 61  contentInsertSta
34650 74 65 6d 65 6e 74 28 29 20 2a 2f 0a 20 20 2f 2a  tement() */.  /*
34651 20 43 4f 4e 54 45 4e 54 5f 53 45 4c 45 43 54 20   CONTENT_SELECT 
34652 2a 2f 20 4e 55 4c 4c 2c 20 20 2f 2a 20 67 65 6e  */ NULL,  /* gen
34653 65 72 61 74 65 64 20 69 6e 20 63 6f 6e 74 65 6e  erated in conten
34654 74 53 65 6c 65 63 74 53 74 61 74 65 6d 65 6e 74  tSelectStatement
34655 28 29 20 2a 2f 0a 20 20 2f 2a 20 43 4f 4e 54 45  () */.  /* CONTE
34656 4e 54 5f 55 50 44 41 54 45 20 2a 2f 20 4e 55 4c  NT_UPDATE */ NUL
34657 4c 2c 20 20 2f 2a 20 67 65 6e 65 72 61 74 65 64  L,  /* generated
34658 20 69 6e 20 63 6f 6e 74 65 6e 74 55 70 64 61 74   in contentUpdat
34659 65 53 74 61 74 65 6d 65 6e 74 28 29 20 2a 2f 0a  eStatement() */.
3465a 20 20 2f 2a 20 43 4f 4e 54 45 4e 54 5f 44 45 4c    /* CONTENT_DEL
3465b 45 54 45 20 2a 2f 20 22 64 65 6c 65 74 65 20 66  ETE */ "delete f
3465c 72 6f 6d 20 25 5f 63 6f 6e 74 65 6e 74 20 77 68  rom %_content wh
3465d 65 72 65 20 64 6f 63 69 64 20 3d 20 3f 22 2c 0a  ere docid = ?",.
3465e 20 20 2f 2a 20 43 4f 4e 54 45 4e 54 5f 45 58 49    /* CONTENT_EXI
3465f 53 54 53 20 2a 2f 20 22 73 65 6c 65 63 74 20 64  STS */ "select d
34660 6f 63 69 64 20 66 72 6f 6d 20 25 5f 63 6f 6e 74  ocid from %_cont
34661 65 6e 74 20 6c 69 6d 69 74 20 31 22 2c 0a 0a 20  ent limit 1",.. 
34662 20 2f 2a 20 42 4c 4f 43 4b 5f 49 4e 53 45 52 54   /* BLOCK_INSERT
34663 20 2a 2f 0a 20 20 22 69 6e 73 65 72 74 20 69 6e   */.  "insert in
34664 74 6f 20 25 5f 73 65 67 6d 65 6e 74 73 20 28 62  to %_segments (b
34665 6c 6f 63 6b 69 64 2c 20 62 6c 6f 63 6b 29 20 76  lockid, block) v
34666 61 6c 75 65 73 20 28 6e 75 6c 6c 2c 20 3f 29 22  alues (null, ?)"
34667 2c 0a 20 20 2f 2a 20 42 4c 4f 43 4b 5f 53 45 4c  ,.  /* BLOCK_SEL
34668 45 43 54 20 2a 2f 20 22 73 65 6c 65 63 74 20 62  ECT */ "select b
34669 6c 6f 63 6b 20 66 72 6f 6d 20 25 5f 73 65 67 6d  lock from %_segm
3466a 65 6e 74 73 20 77 68 65 72 65 20 62 6c 6f 63 6b  ents where block
3466b 69 64 20 3d 20 3f 22 2c 0a 20 20 2f 2a 20 42 4c  id = ?",.  /* BL
3466c 4f 43 4b 5f 44 45 4c 45 54 45 20 2a 2f 20 22 64  OCK_DELETE */ "d
3466d 65 6c 65 74 65 20 66 72 6f 6d 20 25 5f 73 65 67  elete from %_seg
3466e 6d 65 6e 74 73 20 77 68 65 72 65 20 62 6c 6f 63  ments where bloc
3466f 6b 69 64 20 62 65 74 77 65 65 6e 20 3f 20 61 6e  kid between ? an
34670 64 20 3f 22 2c 0a 20 20 2f 2a 20 42 4c 4f 43 4b  d ?",.  /* BLOCK
34671 5f 44 45 4c 45 54 45 5f 41 4c 4c 20 2a 2f 20 22  _DELETE_ALL */ "
34672 64 65 6c 65 74 65 20 66 72 6f 6d 20 25 5f 73 65  delete from %_se
34673 67 6d 65 6e 74 73 22 2c 0a 0a 20 20 2f 2a 20 53  gments",..  /* S
34674 45 47 44 49 52 5f 4d 41 58 5f 49 4e 44 45 58 20  EGDIR_MAX_INDEX 
34675 2a 2f 20 22 73 65 6c 65 63 74 20 6d 61 78 28 69  */ "select max(i
34676 64 78 29 20 66 72 6f 6d 20 25 5f 73 65 67 64 69  dx) from %_segdi
34677 72 20 77 68 65 72 65 20 6c 65 76 65 6c 20 3d 20  r where level = 
34678 3f 22 2c 0a 20 20 2f 2a 20 53 45 47 44 49 52 5f  ?",.  /* SEGDIR_
34679 53 45 54 20 2a 2f 20 22 69 6e 73 65 72 74 20 69  SET */ "insert i
3467a 6e 74 6f 20 25 5f 73 65 67 64 69 72 20 76 61 6c  nto %_segdir val
3467b 75 65 73 20 28 3f 2c 20 3f 2c 20 3f 2c 20 3f 2c  ues (?, ?, ?, ?,
3467c 20 3f 2c 20 3f 29 22 2c 0a 20 20 2f 2a 20 53 45   ?, ?)",.  /* SE
3467d 47 44 49 52 5f 53 45 4c 45 43 54 5f 4c 45 56 45  GDIR_SELECT_LEVE
3467e 4c 20 2a 2f 0a 20 20 22 73 65 6c 65 63 74 20 73  L */.  "select s
3467f 74 61 72 74 5f 62 6c 6f 63 6b 2c 20 6c 65 61 76  tart_block, leav
34680 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 2c 20 72 6f  es_end_block, ro
34681 6f 74 20 66 72 6f 6d 20 25 5f 73 65 67 64 69 72  ot from %_segdir
34682 20 22 0a 20 20 22 20 77 68 65 72 65 20 6c 65 76   ".  " where lev
34683 65 6c 20 3d 20 3f 20 6f 72 64 65 72 20 62 79 20  el = ? order by 
34684 69 64 78 22 2c 0a 20 20 2f 2a 20 53 45 47 44 49  idx",.  /* SEGDI
34685 52 5f 53 50 41 4e 20 2a 2f 0a 20 20 22 73 65 6c  R_SPAN */.  "sel
34686 65 63 74 20 6d 69 6e 28 73 74 61 72 74 5f 62 6c  ect min(start_bl
34687 6f 63 6b 29 2c 20 6d 61 78 28 65 6e 64 5f 62 6c  ock), max(end_bl
34688 6f 63 6b 29 20 66 72 6f 6d 20 25 5f 73 65 67 64  ock) from %_segd
34689 69 72 20 22 0a 20 20 22 20 77 68 65 72 65 20 6c  ir ".  " where l
3468a 65 76 65 6c 20 3d 20 3f 20 61 6e 64 20 73 74 61  evel = ? and sta
3468b 72 74 5f 62 6c 6f 63 6b 20 3c 3e 20 30 22 2c 0a  rt_block <> 0",.
3468c 20 20 2f 2a 20 53 45 47 44 49 52 5f 44 45 4c 45    /* SEGDIR_DELE
3468d 54 45 20 2a 2f 20 22 64 65 6c 65 74 65 20 66 72  TE */ "delete fr
3468e 6f 6d 20 25 5f 73 65 67 64 69 72 20 77 68 65 72  om %_segdir wher
3468f 65 20 6c 65 76 65 6c 20 3d 20 3f 22 2c 0a 0a 20  e level = ?",.. 
34690 20 2f 2a 20 4e 4f 54 45 28 73 68 65 73 73 29 3a   /* NOTE(shess):
34691 20 54 68 65 20 66 69 72 73 74 20 74 68 72 65 65   The first three
34692 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20   results of the 
34693 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 0a 20 20  following two.  
34694 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 6d 75  ** statements mu
34695 73 74 20 6d 61 74 63 68 2e 0a 20 20 2a 2f 0a 20  st match..  */. 
34696 20 2f 2a 20 53 45 47 44 49 52 5f 53 45 4c 45 43   /* SEGDIR_SELEC
34697 54 5f 53 45 47 4d 45 4e 54 20 2a 2f 0a 20 20 22  T_SEGMENT */.  "
34698 73 65 6c 65 63 74 20 73 74 61 72 74 5f 62 6c 6f  select start_blo
34699 63 6b 2c 20 6c 65 61 76 65 73 5f 65 6e 64 5f 62  ck, leaves_end_b
3469a 6c 6f 63 6b 2c 20 72 6f 6f 74 20 66 72 6f 6d 20  lock, root from 
3469b 25 5f 73 65 67 64 69 72 20 22 0a 20 20 22 20 77  %_segdir ".  " w
3469c 68 65 72 65 20 6c 65 76 65 6c 20 3d 20 3f 20 61  here level = ? a
3469d 6e 64 20 69 64 78 20 3d 20 3f 22 2c 0a 20 20 2f  nd idx = ?",.  /
3469e 2a 20 53 45 47 44 49 52 5f 53 45 4c 45 43 54 5f  * SEGDIR_SELECT_
3469f 41 4c 4c 20 2a 2f 0a 20 20 22 73 65 6c 65 63 74  ALL */.  "select
346a0 20 73 74 61 72 74 5f 62 6c 6f 63 6b 2c 20 6c 65   start_block, le
346a1 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 2c 20  aves_end_block, 
346a2 72 6f 6f 74 20 66 72 6f 6d 20 25 5f 73 65 67 64  root from %_segd
346a3 69 72 20 22 0a 20 20 22 20 6f 72 64 65 72 20 62  ir ".  " order b
346a4 79 20 6c 65 76 65 6c 20 64 65 73 63 2c 20 69 64  y level desc, id
346a5 78 20 61 73 63 22 2c 0a 20 20 2f 2a 20 53 45 47  x asc",.  /* SEG
346a6 44 49 52 5f 44 45 4c 45 54 45 5f 41 4c 4c 20 2a  DIR_DELETE_ALL *
346a7 2f 20 22 64 65 6c 65 74 65 20 66 72 6f 6d 20 25  / "delete from %
346a8 5f 73 65 67 64 69 72 22 2c 0a 20 20 2f 2a 20 53  _segdir",.  /* S
346a9 45 47 44 49 52 5f 43 4f 55 4e 54 20 2a 2f 20 22  EGDIR_COUNT */ "
346aa 73 65 6c 65 63 74 20 63 6f 75 6e 74 28 2a 29 2c  select count(*),
346ab 20 69 66 6e 75 6c 6c 28 6d 61 78 28 6c 65 76 65   ifnull(max(leve
346ac 6c 29 2c 30 29 20 66 72 6f 6d 20 25 5f 73 65 67  l),0) from %_seg
346ad 64 69 72 22 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  dir",.};../*.** 
346ae 41 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20  A connection to 
346af 61 20 66 75 6c 6c 74 65 78 74 20 69 6e 64 65 78  a fulltext index
346b0 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   is an instance 
346b1 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
346b2 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20  .** structure.  
346b3 54 68 65 20 78 43 72 65 61 74 65 20 61 6e 64 20  The xCreate and 
346b4 78 43 6f 6e 6e 65 63 74 20 6d 65 74 68 6f 64 73  xConnect methods
346b5 20 63 72 65 61 74 65 20 61 6e 20 69 6e 73 74 61   create an insta
346b6 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 69 73 20 73  nce.** of this s
346b7 74 72 75 63 74 75 72 65 20 61 6e 64 20 78 44 65  tructure and xDe
346b8 73 74 72 6f 79 20 61 6e 64 20 78 44 69 73 63 6f  stroy and xDisco
346b9 6e 6e 65 63 74 20 66 72 65 65 20 74 68 61 74 20  nnect free that 
346ba 69 6e 73 74 61 6e 63 65 2e 0a 2a 2a 20 41 6c 6c  instance..** All
346bb 20 6f 74 68 65 72 20 6d 65 74 68 6f 64 73 20 72   other methods r
346bc 65 63 65 69 76 65 20 61 20 70 6f 69 6e 74 65 72  eceive a pointer
346bd 20 74 6f 20 74 68 65 20 73 74 72 75 63 74 75 72   to the structur
346be 65 20 61 73 20 6f 6e 65 20 6f 66 20 74 68 65 69  e as one of thei
346bf 72 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73 2e 0a  r.** arguments..
346c0 2a 2f 0a 73 74 72 75 63 74 20 66 75 6c 6c 74 65  */.struct fullte
346c1 78 74 5f 76 74 61 62 20 7b 0a 20 20 73 71 6c 69  xt_vtab {.  sqli
346c2 74 65 33 5f 76 74 61 62 20 62 61 73 65 3b 20 20  te3_vtab base;  
346c3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
346c4 42 61 73 65 20 63 6c 61 73 73 20 75 73 65 64 20  Base class used 
346c5 62 79 20 53 51 4c 69 74 65 20 63 6f 72 65 20 2a  by SQLite core *
346c6 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  /.  sqlite3 *db;
346c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
346c8 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
346c9 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
346ca 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
346cb 2a 7a 44 62 3b 20 20 20 20 20 20 20 20 20 20 20  *zDb;           
346cc 20 20 20 20 20 20 2f 2a 20 6c 6f 67 69 63 61 6c        /* logical
346cd 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 2a   database name *
346ce 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
346cf 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20  zName;          
346d0 20 20 20 20 20 2f 2a 20 76 69 72 74 75 61 6c 20       /* virtual 
346d1 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20  table name */.  
346d2 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20  int nColumn;    
346d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
346d4 20 2f 2a 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f   /* number of co
346d5 6c 75 6d 6e 73 20 69 6e 20 76 69 72 74 75 61 6c  lumns in virtual
346d6 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72   table */.  char
346d7 20 2a 2a 61 7a 43 6f 6c 75 6d 6e 3b 20 20 20 20   **azColumn;    
346d8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
346d9 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e 20 20 6d  column names.  m
346da 61 6c 6c 6f 63 65 64 20 2a 2f 0a 20 20 63 68 61  alloced */.  cha
346db 72 20 2a 2a 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c  r **azContentCol
346dc 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  umn;          /*
346dd 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 69 6e   column names in
346de 20 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 3b 20   content table; 
346df 6d 61 6c 6c 6f 63 65 64 20 2a 2f 0a 20 20 73 71  malloced */.  sq
346e0 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
346e1 2a 70 54 6f 6b 65 6e 69 7a 65 72 3b 20 20 20 2f  *pTokenizer;   /
346e2 2a 20 74 6f 6b 65 6e 69 7a 65 72 20 66 6f 72 20  * tokenizer for 
346e3 69 6e 73 65 72 74 73 20 61 6e 64 20 71 75 65 72  inserts and quer
346e4 69 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 50 72 65  ies */..  /* Pre
346e5 63 6f 6d 70 69 6c 65 64 20 73 74 61 74 65 6d 65  compiled stateme
346e6 6e 74 73 20 77 68 69 63 68 20 77 65 20 6b 65 65  nts which we kee
346e7 70 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65  p as long as the
346e8 20 74 61 62 6c 65 20 69 73 0a 20 20 2a 2a 20 6f   table is.  ** o
346e9 70 65 6e 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  pen..  */.  sqli
346ea 74 65 33 5f 73 74 6d 74 20 2a 70 46 75 6c 6c 74  te3_stmt *pFullt
346eb 65 78 74 53 74 61 74 65 6d 65 6e 74 73 5b 4d 41  extStatements[MA
346ec 58 5f 53 54 4d 54 5d 3b 0a 0a 20 20 2f 2a 20 50  X_STMT];..  /* P
346ed 72 65 63 6f 6d 70 69 6c 65 64 20 73 74 61 74 65  recompiled state
346ee 6d 65 6e 74 73 20 75 73 65 64 20 66 6f 72 20 73  ments used for s
346ef 65 67 6d 65 6e 74 20 6d 65 72 67 65 73 2e 20 20  egment merges.  
346f0 57 65 20 72 75 6e 20 61 0a 20 20 2a 2a 20 73 65  We run a.  ** se
346f1 70 61 72 61 74 65 20 73 65 6c 65 63 74 20 61 63  parate select ac
346f2 72 6f 73 73 20 74 68 65 20 6c 65 61 66 20 6c 65  ross the leaf le
346f3 76 65 6c 20 6f 66 20 65 61 63 68 20 74 72 65 65  vel of each tree
346f4 20 62 65 69 6e 67 20 6d 65 72 67 65 64 2e 0a 20   being merged.. 
346f5 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74   */.  sqlite3_st
346f6 6d 74 20 2a 70 4c 65 61 66 53 65 6c 65 63 74 53  mt *pLeafSelectS
346f7 74 6d 74 73 5b 4d 45 52 47 45 5f 43 4f 55 4e 54  tmts[MERGE_COUNT
346f8 5d 3b 0a 20 20 2f 2a 20 54 68 65 20 73 74 61 74  ];.  /* The stat
346f9 65 6d 65 6e 74 20 75 73 65 64 20 74 6f 20 70 72  ement used to pr
346fa 65 70 61 72 65 20 70 4c 65 61 66 53 65 6c 65 63  epare pLeafSelec
346fb 74 53 74 6d 74 73 2e 20 2a 2f 0a 23 64 65 66 69  tStmts. */.#defi
346fc 6e 65 20 4c 45 41 46 5f 53 45 4c 45 43 54 20 5c  ne LEAF_SELECT \
346fd 0a 20 20 22 73 65 6c 65 63 74 20 62 6c 6f 63 6b  .  "select block
346fe 20 66 72 6f 6d 20 25 5f 73 65 67 6d 65 6e 74 73   from %_segments
346ff 20 77 68 65 72 65 20 62 6c 6f 63 6b 69 64 20 62   where blockid b
34700 65 74 77 65 65 6e 20 3f 20 61 6e 64 20 3f 20 6f  etween ? and ? o
34701 72 64 65 72 20 62 79 20 62 6c 6f 63 6b 69 64 22  rder by blockid"
34702 0a 0a 20 20 2f 2a 20 54 68 65 73 65 20 62 75 66  ..  /* These buf
34703 66 65 72 20 70 65 6e 64 69 6e 67 20 69 6e 64 65  fer pending inde
34704 78 20 75 70 64 61 74 65 73 20 64 75 72 69 6e 67  x updates during
34705 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 2e 0a 20   transactions.. 
34706 20 2a 2a 20 6e 50 65 6e 64 69 6e 67 44 61 74 61   ** nPendingData
34707 20 65 73 74 69 6d 61 74 65 73 20 74 68 65 20 6d   estimates the m
34708 65 6d 6f 72 79 20 73 69 7a 65 20 6f 66 20 74 68  emory size of th
34709 65 20 70 65 6e 64 69 6e 67 20 64 61 74 61 2e 20  e pending data. 
3470a 20 49 74 0a 20 20 2a 2a 20 64 6f 65 73 6e 27 74   It.  ** doesn't
3470b 20 69 6e 63 6c 75 64 65 20 74 68 65 20 68 61 73   include the has
3470c 68 2d 62 75 63 6b 65 74 20 6f 76 65 72 68 65 61  h-bucket overhea
3470d 64 2c 20 6e 6f 72 20 61 6e 79 20 6d 61 6c 6c 6f  d, nor any mallo
3470e 63 0a 20 20 2a 2a 20 6f 76 65 72 68 65 61 64 2e  c.  ** overhead.
3470f 20 20 57 68 65 6e 20 6e 50 65 6e 64 69 6e 67 44    When nPendingD
34710 61 74 61 20 65 78 63 65 65 64 73 20 6b 50 65 6e  ata exceeds kPen
34711 64 69 6e 67 54 68 72 65 73 68 6f 6c 64 2c 20 74  dingThreshold, t
34712 68 65 0a 20 20 2a 2a 20 62 75 66 66 65 72 20 69  he.  ** buffer i
34713 73 20 66 6c 75 73 68 65 64 20 65 76 65 6e 20 62  s flushed even b
34714 65 66 6f 72 65 20 74 68 65 20 74 72 61 6e 73 61  efore the transa
34715 63 74 69 6f 6e 20 63 6c 6f 73 65 73 2e 0a 20 20  ction closes..  
34716 2a 2a 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 20  ** pendingTerms 
34717 73 74 6f 72 65 73 20 74 68 65 20 64 61 74 61 2c  stores the data,
34718 20 61 6e 64 20 69 73 20 6f 6e 6c 79 20 76 61 6c   and is only val
34719 69 64 20 77 68 65 6e 20 6e 50 65 6e 64 69 6e 67  id when nPending
3471a 44 61 74 61 0a 20 20 2a 2a 20 69 73 20 3e 3d 30  Data.  ** is >=0
3471b 20 28 6e 50 65 6e 64 69 6e 67 44 61 74 61 3c 30   (nPendingData<0
3471c 20 6d 65 61 6e 73 20 70 65 6e 64 69 6e 67 54 65   means pendingTe
3471d 72 6d 73 20 68 61 73 20 6e 6f 74 20 62 65 65 6e  rms has not been
3471e 0a 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65  .  ** initialize
3471f 64 29 2e 20 20 69 50 72 65 76 44 6f 63 69 64 20  d).  iPrevDocid 
34720 69 73 20 74 68 65 20 6c 61 73 74 20 64 6f 63 69  is the last doci
34721 64 20 77 72 69 74 74 65 6e 2c 20 75 73 65 64 20  d written, used 
34722 74 6f 20 6d 61 6b 65 0a 20 20 2a 2a 20 63 65 72  to make.  ** cer
34723 74 61 69 6e 20 77 65 27 72 65 20 69 6e 73 65 72  tain we're inser
34724 74 69 6e 67 20 69 6e 20 73 6f 72 74 65 64 20 6f  ting in sorted o
34725 72 64 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74  rder..  */.  int
34726 20 6e 50 65 6e 64 69 6e 67 44 61 74 61 3b 0a 23   nPendingData;.#
34727 64 65 66 69 6e 65 20 6b 50 65 6e 64 69 6e 67 54  define kPendingT
34728 68 72 65 73 68 6f 6c 64 20 28 31 2a 31 30 32 34  hreshold (1*1024
34729 2a 31 30 32 34 29 0a 20 20 73 71 6c 69 74 65 5f  *1024).  sqlite_
3472a 69 6e 74 36 34 20 69 50 72 65 76 44 6f 63 69 64  int64 iPrevDocid
3472b 3b 0a 20 20 66 74 73 33 48 61 73 68 20 70 65 6e  ;.  fts3Hash pen
3472c 64 69 6e 67 54 65 72 6d 73 3b 0a 7d 3b 0a 0a 2f  dingTerms;.};../
3472d 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63 6f  *.** When the co
3472e 72 65 20 77 61 6e 74 73 20 74 6f 20 64 6f 20 61  re wants to do a
3472f 20 71 75 65 72 79 2c 20 69 74 20 63 72 65 61 74   query, it creat
34730 65 20 61 20 63 75 72 73 6f 72 20 75 73 69 6e 67  e a cursor using
34731 20 61 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 78 4f   a.** call to xO
34732 70 65 6e 2e 20 20 54 68 69 73 20 73 74 72 75 63  pen.  This struc
34733 74 75 72 65 20 69 73 20 61 6e 20 69 6e 73 74 61  ture is an insta
34734 6e 63 65 20 6f 66 20 61 20 63 75 72 73 6f 72 2e  nce of a cursor.
34735 20 20 49 74 0a 2a 2a 20 69 73 20 64 65 73 74 72    It.** is destr
34736 6f 79 65 64 20 62 79 20 78 43 6c 6f 73 65 2e 0a  oyed by xClose..
34737 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
34738 74 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f  t fulltext_curso
34739 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74  r {.  sqlite3_vt
3473a 61 62 5f 63 75 72 73 6f 72 20 62 61 73 65 3b 20  ab_cursor base; 
3473b 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20 63         /* Base c
3473c 6c 61 73 73 20 75 73 65 64 20 62 79 20 53 51 4c  lass used by SQL
3473d 69 74 65 20 63 6f 72 65 20 2a 2f 0a 20 20 51 75  ite core */.  Qu
3473e 65 72 79 54 79 70 65 20 69 43 75 72 73 6f 72 54  eryType iCursorT
3473f 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f  ype;           /
34740 2a 20 43 6f 70 79 20 6f 66 20 73 71 6c 69 74 65  * Copy of sqlite
34741 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 2e 69 64 78  3_index_info.idx
34742 4e 75 6d 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  Num */.  sqlite3
34743 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 20 20 20  _stmt *pStmt;   
34744 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65            /* Pre
34745 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20  pared statement 
34746 69 6e 20 75 73 65 20 62 79 20 74 68 65 20 63 75  in use by the cu
34747 72 73 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 65 6f  rsor */.  int eo
34748 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f;              
34749 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
3474a 75 65 20 69 66 20 61 74 20 45 6e 64 20 4f 66 20  ue if at End Of 
3474b 52 65 73 75 6c 74 73 20 2a 2f 0a 20 20 46 74 73  Results */.  Fts
3474c 33 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20 20  3Expr *pExpr;   
3474d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3474e 20 50 61 72 73 65 64 20 4d 41 54 43 48 20 71 75   Parsed MATCH qu
3474f 65 72 79 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20  ery string */.  
34750 53 6e 69 70 70 65 74 20 73 6e 69 70 70 65 74 3b  Snippet snippet;
34751 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34752 20 2f 2a 20 43 61 63 68 65 64 20 73 6e 69 70 70   /* Cached snipp
34753 65 74 20 66 6f 72 20 74 68 65 20 63 75 72 72 65  et for the curre
34754 6e 74 20 72 6f 77 20 2a 2f 0a 20 20 69 6e 74 20  nt row */.  int 
34755 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20  iColumn;        
34756 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
34757 43 6f 6c 75 6d 6e 20 62 65 69 6e 67 20 73 65 61  Column being sea
34758 72 63 68 65 64 20 2a 2f 0a 20 20 44 61 74 61 42  rched */.  DataB
34759 75 66 66 65 72 20 72 65 73 75 6c 74 3b 20 20 20  uffer result;   
3475a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
3475b 6f 63 6c 69 73 74 20 72 65 73 75 6c 74 73 20 66  oclist results f
3475c 72 6f 6d 20 66 75 6c 6c 74 65 78 74 51 75 65 72  rom fulltextQuer
3475d 79 20 2a 2f 0a 20 20 44 4c 52 65 61 64 65 72 20  y */.  DLReader 
3475e 72 65 61 64 65 72 3b 20 20 20 20 20 20 20 20 20  reader;         
3475f 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c          /* Resul
34760 74 20 72 65 61 64 65 72 20 69 66 20 72 65 73 75  t reader if resu
34761 6c 74 20 6e 6f 74 20 65 6d 70 74 79 20 2a 2f 0a  lt not empty */.
34762 7d 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f  } fulltext_curso
34763 72 3b 0a 0a 73 74 61 74 69 63 20 66 75 6c 6c 74  r;..static fullt
34764 65 78 74 5f 76 74 61 62 20 2a 63 75 72 73 6f 72  ext_vtab *cursor
34765 5f 76 74 61 62 28 66 75 6c 6c 74 65 78 74 5f 63  _vtab(fulltext_c
34766 75 72 73 6f 72 20 2a 63 29 7b 0a 20 20 72 65 74  ursor *c){.  ret
34767 75 72 6e 20 28 66 75 6c 6c 74 65 78 74 5f 76 74  urn (fulltext_vt
34768 61 62 20 2a 29 20 63 2d 3e 62 61 73 65 2e 70 56  ab *) c->base.pV
34769 74 61 62 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 63  tab;.}..static c
3476a 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64  onst sqlite3_mod
3476b 75 6c 65 20 66 74 73 33 4d 6f 64 75 6c 65 3b 20  ule fts3Module; 
3476c 20 20 2f 2a 20 66 6f 72 77 61 72 64 20 64 65 63    /* forward dec
3476d 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a 20  laration */../* 
3476e 52 65 74 75 72 6e 20 61 20 64 79 6e 61 6d 69 63  Return a dynamic
3476f 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 20 73  ally generated s
34770 74 61 74 65 6d 65 6e 74 20 6f 66 20 74 68 65 20  tatement of the 
34771 66 6f 72 6d 0a 20 2a 20 20 20 69 6e 73 65 72 74  form. *   insert
34772 20 69 6e 74 6f 20 25 5f 63 6f 6e 74 65 6e 74 20   into %_content 
34773 28 64 6f 63 69 64 2c 20 2e 2e 2e 29 20 76 61 6c  (docid, ...) val
34774 75 65 73 20 28 3f 2c 20 2e 2e 2e 29 0a 20 2a 2f  ues (?, ...). */
34775 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68  .static const ch
34776 61 72 20 2a 63 6f 6e 74 65 6e 74 49 6e 73 65 72  ar *contentInser
34777 74 53 74 61 74 65 6d 65 6e 74 28 66 75 6c 6c 74  tStatement(fullt
34778 65 78 74 5f 76 74 61 62 20 2a 76 29 7b 0a 20 20  ext_vtab *v){.  
34779 53 74 72 69 6e 67 42 75 66 66 65 72 20 73 62 3b  StringBuffer sb;
3477a 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 69 6e 69  .  int i;..  ini
3477b 74 53 74 72 69 6e 67 42 75 66 66 65 72 28 26 73  tStringBuffer(&s
3477c 62 29 3b 0a 20 20 61 70 70 65 6e 64 28 26 73 62  b);.  append(&sb
3477d 2c 20 22 69 6e 73 65 72 74 20 69 6e 74 6f 20 25  , "insert into %
3477e 5f 63 6f 6e 74 65 6e 74 20 28 64 6f 63 69 64 2c  _content (docid,
3477f 20 22 29 3b 0a 20 20 61 70 70 65 6e 64 4c 69 73   ");.  appendLis
34780 74 28 26 73 62 2c 20 76 2d 3e 6e 43 6f 6c 75 6d  t(&sb, v->nColum
34781 6e 2c 20 76 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43  n, v->azContentC
34782 6f 6c 75 6d 6e 29 3b 0a 20 20 61 70 70 65 6e 64  olumn);.  append
34783 28 26 73 62 2c 20 22 29 20 76 61 6c 75 65 73 20  (&sb, ") values 
34784 28 3f 22 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  (?");.  for(i=0;
34785 20 69 3c 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 2b   i<v->nColumn; +
34786 2b 69 29 0a 20 20 20 20 61 70 70 65 6e 64 28 26  +i).    append(&
34787 73 62 2c 20 22 2c 20 3f 22 29 3b 0a 20 20 61 70  sb, ", ?");.  ap
34788 70 65 6e 64 28 26 73 62 2c 20 22 29 22 29 3b 0a  pend(&sb, ")");.
34789 20 20 72 65 74 75 72 6e 20 73 74 72 69 6e 67 42    return stringB
3478a 75 66 66 65 72 44 61 74 61 28 26 73 62 29 3b 0a  ufferData(&sb);.
3478b 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 61 20 64  }../* Return a d
3478c 79 6e 61 6d 69 63 61 6c 6c 79 20 67 65 6e 65 72  ynamically gener
3478d 61 74 65 64 20 73 74 61 74 65 6d 65 6e 74 20 6f  ated statement o
3478e 66 20 74 68 65 20 66 6f 72 6d 0a 20 2a 20 20 20  f the form. *   
3478f 73 65 6c 65 63 74 20 3c 63 6f 6e 74 65 6e 74 20  select <content 
34790 63 6f 6c 75 6d 6e 73 3e 20 66 72 6f 6d 20 25 5f  columns> from %_
34791 63 6f 6e 74 65 6e 74 20 77 68 65 72 65 20 64 6f  content where do
34792 63 69 64 20 3d 20 3f 0a 20 2a 2f 0a 73 74 61 74  cid = ?. */.stat
34793 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63  ic const char *c
34794 6f 6e 74 65 6e 74 53 65 6c 65 63 74 53 74 61 74  ontentSelectStat
34795 65 6d 65 6e 74 28 66 75 6c 6c 74 65 78 74 5f 76  ement(fulltext_v
34796 74 61 62 20 2a 76 29 7b 0a 20 20 53 74 72 69 6e  tab *v){.  Strin
34797 67 42 75 66 66 65 72 20 73 62 3b 0a 20 20 69 6e  gBuffer sb;.  in
34798 69 74 53 74 72 69 6e 67 42 75 66 66 65 72 28 26  itStringBuffer(&
34799 73 62 29 3b 0a 20 20 61 70 70 65 6e 64 28 26 73  sb);.  append(&s
3479a 62 2c 20 22 53 45 4c 45 43 54 20 22 29 3b 0a 20  b, "SELECT ");. 
3479b 20 61 70 70 65 6e 64 4c 69 73 74 28 26 73 62 2c   appendList(&sb,
3479c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 76 2d 3e   v->nColumn, v->
3479d 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 29  azContentColumn)
3479e 3b 0a 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20  ;.  append(&sb, 
3479f 22 20 46 52 4f 4d 20 25 5f 63 6f 6e 74 65 6e 74  " FROM %_content
347a0 20 57 48 45 52 45 20 64 6f 63 69 64 20 3d 20 3f   WHERE docid = ?
347a1 22 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 74 72  ");.  return str
347a2 69 6e 67 42 75 66 66 65 72 44 61 74 61 28 26 73  ingBufferData(&s
347a3 62 29 3b 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e  b);.}../* Return
347a4 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 67   a dynamically g
347a5 65 6e 65 72 61 74 65 64 20 73 74 61 74 65 6d 65  enerated stateme
347a6 6e 74 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 20  nt of the form. 
347a7 2a 20 20 20 75 70 64 61 74 65 20 25 5f 63 6f 6e  *   update %_con
347a8 74 65 6e 74 20 73 65 74 20 5b 63 6f 6c 5f 30 5d  tent set [col_0]
347a9 20 3d 20 3f 2c 20 5b 63 6f 6c 5f 31 5d 20 3d 20   = ?, [col_1] = 
347aa 3f 2c 20 2e 2e 2e 0a 20 2a 20 20 20 20 20 20 20  ?, .... *       
347ab 20 20 20 20 20 20 20 20 20 20 20 20 20 77 68 65               whe
347ac 72 65 20 64 6f 63 69 64 20 3d 20 3f 0a 20 2a 2f  re docid = ?. */
347ad 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68  .static const ch
347ae 61 72 20 2a 63 6f 6e 74 65 6e 74 55 70 64 61 74  ar *contentUpdat
347af 65 53 74 61 74 65 6d 65 6e 74 28 66 75 6c 6c 74  eStatement(fullt
347b0 65 78 74 5f 76 74 61 62 20 2a 76 29 7b 0a 20 20  ext_vtab *v){.  
347b1 53 74 72 69 6e 67 42 75 66 66 65 72 20 73 62 3b  StringBuffer sb;
347b2 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 69 6e 69  .  int i;..  ini
347b3 74 53 74 72 69 6e 67 42 75 66 66 65 72 28 26 73  tStringBuffer(&s
347b4 62 29 3b 0a 20 20 61 70 70 65 6e 64 28 26 73 62  b);.  append(&sb
347b5 2c 20 22 75 70 64 61 74 65 20 25 5f 63 6f 6e 74  , "update %_cont
347b6 65 6e 74 20 73 65 74 20 22 29 3b 0a 20 20 66 6f  ent set ");.  fo
347b7 72 28 69 3d 30 3b 20 69 3c 76 2d 3e 6e 43 6f 6c  r(i=0; i<v->nCol
347b8 75 6d 6e 3b 20 2b 2b 69 29 20 7b 0a 20 20 20 20  umn; ++i) {.    
347b9 69 66 28 20 69 3e 30 20 29 7b 0a 20 20 20 20 20  if( i>0 ){.     
347ba 20 61 70 70 65 6e 64 28 26 73 62 2c 20 22 2c 20   append(&sb, ", 
347bb 22 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 70  ");.    }.    ap
347bc 70 65 6e 64 28 26 73 62 2c 20 76 2d 3e 61 7a 43  pend(&sb, v->azC
347bd 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 5b 69 5d 29  ontentColumn[i])
347be 3b 0a 20 20 20 20 61 70 70 65 6e 64 28 26 73 62  ;.    append(&sb
347bf 2c 20 22 20 3d 20 3f 22 29 3b 0a 20 20 7d 0a 20  , " = ?");.  }. 
347c0 20 61 70 70 65 6e 64 28 26 73 62 2c 20 22 20 77   append(&sb, " w
347c1 68 65 72 65 20 64 6f 63 69 64 20 3d 20 3f 22 29  here docid = ?")
347c2 3b 0a 20 20 72 65 74 75 72 6e 20 73 74 72 69 6e  ;.  return strin
347c3 67 42 75 66 66 65 72 44 61 74 61 28 26 73 62 29  gBufferData(&sb)
347c4 3b 0a 7d 0a 0a 2f 2a 20 50 75 74 73 20 61 20 66  ;.}../* Puts a f
347c5 72 65 73 68 6c 79 2d 70 72 65 70 61 72 65 64 20  reshly-prepared 
347c6 73 74 61 74 65 6d 65 6e 74 20 64 65 74 65 72 6d  statement determ
347c7 69 6e 65 64 20 62 79 20 69 53 74 6d 74 20 69 6e  ined by iStmt in
347c8 20 2a 70 70 53 74 6d 74 2e 0a 2a 2a 20 49 66 20   *ppStmt..** If 
347c9 74 68 65 20 69 6e 64 69 63 61 74 65 64 20 73 74  the indicated st
347ca 61 74 65 6d 65 6e 74 20 68 61 73 20 6e 65 76 65  atement has neve
347cb 72 20 62 65 65 6e 20 70 72 65 70 61 72 65 64 2c  r been prepared,
347cc 20 69 74 20 69 73 20 70 72 65 70 61 72 65 64 0a   it is prepared.
347cd 2a 2a 20 61 6e 64 20 63 61 63 68 65 64 2c 20 6f  ** and cached, o
347ce 74 68 65 72 77 69 73 65 20 74 68 65 20 63 61 63  therwise the cac
347cf 68 65 64 20 76 65 72 73 69 6f 6e 20 69 73 20 72  hed version is r
347d0 65 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  eset..*/.static 
347d1 69 6e 74 20 73 71 6c 5f 67 65 74 5f 73 74 61 74  int sql_get_stat
347d2 65 6d 65 6e 74 28 66 75 6c 6c 74 65 78 74 5f 76  ement(fulltext_v
347d3 74 61 62 20 2a 76 2c 20 66 75 6c 6c 74 65 78 74  tab *v, fulltext
347d4 5f 73 74 61 74 65 6d 65 6e 74 20 69 53 74 6d 74  _statement iStmt
347d5 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
347d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
347d7 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70  qlite3_stmt **pp
347d8 53 74 6d 74 29 7b 0a 20 20 61 73 73 65 72 74 28  Stmt){.  assert(
347d9 20 69 53 74 6d 74 3c 4d 41 58 5f 53 54 4d 54 20   iStmt<MAX_STMT 
347da 29 3b 0a 20 20 69 66 28 20 76 2d 3e 70 46 75 6c  );.  if( v->pFul
347db 6c 74 65 78 74 53 74 61 74 65 6d 65 6e 74 73 5b  ltextStatements[
347dc 69 53 74 6d 74 5d 3d 3d 4e 55 4c 4c 20 29 7b 0a  iStmt]==NULL ){.
347dd 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
347de 7a 53 74 6d 74 3b 0a 20 20 20 20 69 6e 74 20 72  zStmt;.    int r
347df 63 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20 69  c;.    switch( i
347e0 53 74 6d 74 20 29 7b 0a 20 20 20 20 20 20 63 61  Stmt ){.      ca
347e1 73 65 20 43 4f 4e 54 45 4e 54 5f 49 4e 53 45 52  se CONTENT_INSER
347e2 54 5f 53 54 4d 54 3a 0a 20 20 20 20 20 20 20 20  T_STMT:.        
347e3 7a 53 74 6d 74 20 3d 20 63 6f 6e 74 65 6e 74 49  zStmt = contentI
347e4 6e 73 65 72 74 53 74 61 74 65 6d 65 6e 74 28 76  nsertStatement(v
347e5 29 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20  ); break;.      
347e6 63 61 73 65 20 43 4f 4e 54 45 4e 54 5f 53 45 4c  case CONTENT_SEL
347e7 45 43 54 5f 53 54 4d 54 3a 0a 20 20 20 20 20 20  ECT_STMT:.      
347e8 20 20 7a 53 74 6d 74 20 3d 20 63 6f 6e 74 65 6e    zStmt = conten
347e9 74 53 65 6c 65 63 74 53 74 61 74 65 6d 65 6e 74  tSelectStatement
347ea 28 76 29 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20  (v); break;.    
347eb 20 20 63 61 73 65 20 43 4f 4e 54 45 4e 54 5f 55    case CONTENT_U
347ec 50 44 41 54 45 5f 53 54 4d 54 3a 0a 20 20 20 20  PDATE_STMT:.    
347ed 20 20 20 20 7a 53 74 6d 74 20 3d 20 63 6f 6e 74      zStmt = cont
347ee 65 6e 74 55 70 64 61 74 65 53 74 61 74 65 6d 65  entUpdateStateme
347ef 6e 74 28 76 29 3b 20 62 72 65 61 6b 3b 0a 20 20  nt(v); break;.  
347f0 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20      default:.   
347f1 20 20 20 20 20 7a 53 74 6d 74 20 3d 20 66 75 6c       zStmt = ful
347f2 6c 74 65 78 74 5f 7a 53 74 61 74 65 6d 65 6e 74  ltext_zStatement
347f3 5b 69 53 74 6d 74 5d 3b 0a 20 20 20 20 7d 0a 20  [iStmt];.    }. 
347f4 20 20 20 72 63 20 3d 20 73 71 6c 5f 70 72 65 70     rc = sql_prep
347f5 61 72 65 28 76 2d 3e 64 62 2c 20 76 2d 3e 7a 44  are(v->db, v->zD
347f6 62 2c 20 76 2d 3e 7a 4e 61 6d 65 2c 20 26 76 2d  b, v->zName, &v-
347f7 3e 70 46 75 6c 6c 74 65 78 74 53 74 61 74 65 6d  >pFulltextStatem
347f8 65 6e 74 73 5b 69 53 74 6d 74 5d 2c 0a 20 20 20  ents[iStmt],.   
347f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
347fa 20 20 20 20 20 20 7a 53 74 6d 74 29 3b 0a 20 20        zStmt);.  
347fb 20 20 69 66 28 20 7a 53 74 6d 74 20 21 3d 20 66    if( zStmt != f
347fc 75 6c 6c 74 65 78 74 5f 7a 53 74 61 74 65 6d 65  ulltext_zStateme
347fd 6e 74 5b 69 53 74 6d 74 5d 29 20 73 71 6c 69 74  nt[iStmt]) sqlit
347fe 65 33 5f 66 72 65 65 28 28 76 6f 69 64 20 2a 29  e3_free((void *)
347ff 20 7a 53 74 6d 74 29 3b 0a 20 20 20 20 69 66 28   zStmt);.    if(
34800 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
34801 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 20   return rc;.  } 
34802 65 6c 73 65 20 7b 0a 20 20 20 20 69 6e 74 20 72  else {.    int r
34803 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65  c = sqlite3_rese
34804 74 28 76 2d 3e 70 46 75 6c 6c 74 65 78 74 53 74  t(v->pFulltextSt
34805 61 74 65 6d 65 6e 74 73 5b 69 53 74 6d 74 5d 29  atements[iStmt])
34806 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
34807 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
34808 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70 53   rc;.  }..  *ppS
34809 74 6d 74 20 3d 20 76 2d 3e 70 46 75 6c 6c 74 65  tmt = v->pFullte
3480a 78 74 53 74 61 74 65 6d 65 6e 74 73 5b 69 53 74  xtStatements[iSt
3480b 6d 74 5d 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  mt];.  return SQ
3480c 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 4c  LITE_OK;.}../* L
3480d 69 6b 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70  ike sqlite3_step
3480e 28 29 2c 20 62 75 74 20 63 6f 6e 76 65 72 74 20  (), but convert 
3480f 53 51 4c 49 54 45 5f 44 4f 4e 45 20 74 6f 20 53  SQLITE_DONE to S
34810 51 4c 49 54 45 5f 4f 4b 20 61 6e 64 0a 2a 2a 20  QLITE_OK and.** 
34811 53 51 4c 49 54 45 5f 52 4f 57 20 74 6f 20 53 51  SQLITE_ROW to SQ
34812 4c 49 54 45 5f 45 52 52 4f 52 2e 20 20 55 73 65  LITE_ERROR.  Use
34813 66 75 6c 20 66 6f 72 20 73 74 61 74 65 6d 65 6e  ful for statemen
34814 74 73 20 6c 69 6b 65 20 55 50 44 41 54 45 2c 0a  ts like UPDATE,.
34815 2a 2a 20 77 68 65 72 65 20 77 65 20 65 78 70 65  ** where we expe
34816 63 74 20 6e 6f 20 72 65 73 75 6c 74 73 2e 0a 2a  ct no results..*
34817 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c  /.static int sql
34818 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28 73 71 6c  _single_step(sql
34819 69 74 65 33 5f 73 74 6d 74 20 2a 73 29 7b 0a 20  ite3_stmt *s){. 
3481a 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65   int rc = sqlite
3481b 33 5f 73 74 65 70 28 73 29 3b 0a 20 20 72 65 74  3_step(s);.  ret
3481c 75 72 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f  urn (rc==SQLITE_
3481d 44 4f 4e 45 29 20 3f 20 53 51 4c 49 54 45 5f 4f  DONE) ? SQLITE_O
3481e 4b 20 3a 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4c 69  K : rc;.}../* Li
3481f 6b 65 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65  ke sql_get_state
34820 6d 65 6e 74 28 29 2c 20 62 75 74 20 66 6f 72 20  ment(), but for 
34821 73 70 65 63 69 61 6c 20 72 65 70 6c 69 63 61 74  special replicat
34822 65 64 20 4c 45 41 46 5f 53 45 4c 45 43 54 0a 2a  ed LEAF_SELECT.*
34823 2a 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 69  * statements.  i
34824 64 78 20 2d 31 20 69 73 20 61 20 73 70 65 63 69  dx -1 is a speci
34825 61 6c 20 63 61 73 65 20 66 6f 72 20 61 6e 20 75  al case for an u
34826 6e 63 61 63 68 65 64 20 76 65 72 73 69 6f 6e 20  ncached version 
34827 6f 66 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 6d  of.** the statem
34828 65 6e 74 20 28 75 73 65 64 20 69 6e 20 74 68 65  ent (used in the
34829 20 6f 70 74 69 6d 69 7a 65 20 69 6d 70 6c 65 6d   optimize implem
3482a 65 6e 74 61 74 69 6f 6e 29 2e 0a 2a 2f 0a 2f 2a  entation)..*/./*
3482b 20 54 4f 44 4f 28 73 68 65 73 73 29 20 57 72 69   TODO(shess) Wri
3482c 74 65 20 76 65 72 73 69 6f 6e 20 66 6f 72 20 67  te version for g
3482d 65 6e 65 72 69 63 20 73 74 61 74 65 6d 65 6e 74  eneric statement
3482e 73 20 61 6e 64 20 74 68 65 6e 20 73 68 61 72 65  s and then share
3482f 0a 2a 2a 20 74 68 61 74 20 62 65 74 77 65 65 6e  .** that between
34830 20 74 68 65 20 63 61 63 68 65 64 2d 73 74 61 74   the cached-stat
34831 65 6d 65 6e 74 20 66 75 6e 63 74 69 6f 6e 73 2e  ement functions.
34832 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
34833 71 6c 5f 67 65 74 5f 6c 65 61 66 5f 73 74 61 74  ql_get_leaf_stat
34834 65 6d 65 6e 74 28 66 75 6c 6c 74 65 78 74 5f 76  ement(fulltext_v
34835 74 61 62 20 2a 76 2c 20 69 6e 74 20 69 64 78 2c  tab *v, int idx,
34836 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
34837 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34838 20 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20     sqlite3_stmt 
34839 2a 2a 70 70 53 74 6d 74 29 7b 0a 20 20 61 73 73  **ppStmt){.  ass
3483a 65 72 74 28 20 69 64 78 3e 3d 2d 31 20 26 26 20  ert( idx>=-1 && 
3483b 69 64 78 3c 4d 45 52 47 45 5f 43 4f 55 4e 54 20  idx<MERGE_COUNT 
3483c 29 3b 0a 20 20 69 66 28 20 69 64 78 3d 3d 2d 31  );.  if( idx==-1
3483d 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73   ){.    return s
3483e 71 6c 5f 70 72 65 70 61 72 65 28 76 2d 3e 64 62  ql_prepare(v->db
3483f 2c 20 76 2d 3e 7a 44 62 2c 20 76 2d 3e 7a 4e 61  , v->zDb, v->zNa
34840 6d 65 2c 20 70 70 53 74 6d 74 2c 20 4c 45 41 46  me, ppStmt, LEAF
34841 5f 53 45 4c 45 43 54 29 3b 0a 20 20 7d 65 6c 73  _SELECT);.  }els
34842 65 20 69 66 28 20 76 2d 3e 70 4c 65 61 66 53 65  e if( v->pLeafSe
34843 6c 65 63 74 53 74 6d 74 73 5b 69 64 78 5d 3d 3d  lectStmts[idx]==
34844 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 69 6e 74 20  NULL ){.    int 
34845 72 63 20 3d 20 73 71 6c 5f 70 72 65 70 61 72 65  rc = sql_prepare
34846 28 76 2d 3e 64 62 2c 20 76 2d 3e 7a 44 62 2c 20  (v->db, v->zDb, 
34847 76 2d 3e 7a 4e 61 6d 65 2c 20 26 76 2d 3e 70 4c  v->zName, &v->pL
34848 65 61 66 53 65 6c 65 63 74 53 74 6d 74 73 5b 69  eafSelectStmts[i
34849 64 78 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20  dx],.           
3484a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4c 45                LE
3484b 41 46 5f 53 45 4c 45 43 54 29 3b 0a 20 20 20 20  AF_SELECT);.    
3484c 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
3484d 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  K ) return rc;. 
3484e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20   }else{.    int 
3484f 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73  rc = sqlite3_res
34850 65 74 28 76 2d 3e 70 4c 65 61 66 53 65 6c 65 63  et(v->pLeafSelec
34851 74 53 74 6d 74 73 5b 69 64 78 5d 29 3b 0a 20 20  tStmts[idx]);.  
34852 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
34853 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
34854 0a 20 20 7d 0a 0a 20 20 2a 70 70 53 74 6d 74 20  .  }..  *ppStmt 
34855 3d 20 76 2d 3e 70 4c 65 61 66 53 65 6c 65 63 74  = v->pLeafSelect
34856 53 74 6d 74 73 5b 69 64 78 5d 3b 0a 20 20 72 65  Stmts[idx];.  re
34857 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
34858 7d 0a 0a 2f 2a 20 69 6e 73 65 72 74 20 69 6e 74  }../* insert int
34859 6f 20 25 5f 63 6f 6e 74 65 6e 74 20 28 64 6f 63  o %_content (doc
3485a 69 64 2c 20 2e 2e 2e 29 20 76 61 6c 75 65 73 20  id, ...) values 
3485b 28 5b 64 6f 63 69 64 5d 2c 20 5b 70 56 61 6c 75  ([docid], [pValu
3485c 65 73 5d 29 0a 2a 2a 20 49 66 20 74 68 65 20 64  es]).** If the d
3485d 6f 63 69 64 20 63 6f 6e 74 61 69 6e 73 20 53 51  ocid contains SQ
3485e 4c 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 20 75  L NULL, then a u
3485f 6e 69 71 75 65 20 64 6f 63 69 64 20 77 69 6c 6c  nique docid will
34860 20 62 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65 64   be.** generated
34861 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
34862 63 6f 6e 74 65 6e 74 5f 69 6e 73 65 72 74 28 66  content_insert(f
34863 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c  ulltext_vtab *v,
34864 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
34865 64 6f 63 69 64 2c 0a 20 20 20 20 20 20 20 20 20  docid,.         
34866 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34867 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
34868 2a 70 56 61 6c 75 65 73 29 7b 0a 20 20 73 71 6c  *pValues){.  sql
34869 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20  ite3_stmt *s;.  
3486a 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 72 63 20  int i;.  int rc 
3486b 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d  = sql_get_statem
3486c 65 6e 74 28 76 2c 20 43 4f 4e 54 45 4e 54 5f 49  ent(v, CONTENT_I
3486d 4e 53 45 52 54 5f 53 54 4d 54 2c 20 26 73 29 3b  NSERT_STMT, &s);
3486e 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
3486f 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
34870 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  ;..  rc = sqlite
34871 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73 2c 20  3_bind_value(s, 
34872 31 2c 20 64 6f 63 69 64 29 3b 0a 20 20 69 66 28  1, docid);.  if(
34873 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
34874 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 66   return rc;..  f
34875 6f 72 28 69 3d 30 3b 20 69 3c 76 2d 3e 6e 43 6f  or(i=0; i<v->nCo
34876 6c 75 6d 6e 3b 20 2b 2b 69 29 7b 0a 20 20 20 20  lumn; ++i){.    
34877 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e  rc = sqlite3_bin
34878 64 5f 76 61 6c 75 65 28 73 2c 20 32 2b 69 2c 20  d_value(s, 2+i, 
34879 70 56 61 6c 75 65 73 5b 69 5d 29 3b 0a 20 20 20  pValues[i]);.   
3487a 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
3487b 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
3487c 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 73 71    }..  return sq
3487d 6c 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28 73 29  l_single_step(s)
3487e 3b 0a 7d 0a 0a 2f 2a 20 75 70 64 61 74 65 20 25  ;.}../* update %
3487f 5f 63 6f 6e 74 65 6e 74 20 73 65 74 20 63 6f 6c  _content set col
34880 30 20 3d 20 70 56 61 6c 75 65 73 5b 30 5d 2c 20  0 = pValues[0], 
34881 63 6f 6c 31 20 3d 20 70 56 61 6c 75 65 73 5b 31  col1 = pValues[1
34882 5d 2c 20 2e 2e 2e 0a 20 2a 20 20 20 20 20 20 20  ], .... *       
34883 20 20 20 20 20 20 20 20 20 20 20 77 68 65 72 65             where
34884 20 64 6f 63 69 64 20 3d 20 5b 69 44 6f 63 69 64   docid = [iDocid
34885 5d 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ] */.static int 
34886 63 6f 6e 74 65 6e 74 5f 75 70 64 61 74 65 28 66  content_update(f
34887 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c  ulltext_vtab *v,
34888 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
34889 2a 70 56 61 6c 75 65 73 2c 0a 20 20 20 20 20 20  *pValues,.      
3488a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3488b 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34      sqlite_int64
3488c 20 69 44 6f 63 69 64 29 7b 0a 20 20 73 71 6c 69   iDocid){.  sqli
3488d 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69  te3_stmt *s;.  i
3488e 6e 74 20 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d  nt i;.  int rc =
3488f 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65   sql_get_stateme
34890 6e 74 28 76 2c 20 43 4f 4e 54 45 4e 54 5f 55 50  nt(v, CONTENT_UP
34891 44 41 54 45 5f 53 54 4d 54 2c 20 26 73 29 3b 0a  DATE_STMT, &s);.
34892 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
34893 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
34894 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 76  ..  for(i=0; i<v
34895 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 2b 2b 69 29 7b  ->nColumn; ++i){
34896 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
34897 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73 2c 20  3_bind_value(s, 
34898 31 2b 69 2c 20 70 56 61 6c 75 65 73 5b 69 5d 29  1+i, pValues[i])
34899 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
3489a 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
3489b 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d   rc;.  }..  rc =
3489c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
3489d 74 36 34 28 73 2c 20 31 2b 76 2d 3e 6e 43 6f 6c  t64(s, 1+v->nCol
3489e 75 6d 6e 2c 20 69 44 6f 63 69 64 29 3b 0a 20 20  umn, iDocid);.  
3489f 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
348a0 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
348a1 20 20 72 65 74 75 72 6e 20 73 71 6c 5f 73 69 6e    return sql_sin
348a2 67 6c 65 5f 73 74 65 70 28 73 29 3b 0a 7d 0a 0a  gle_step(s);.}..
348a3 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 65  static void free
348a4 53 74 72 69 6e 67 41 72 72 61 79 28 69 6e 74 20  StringArray(int 
348a5 6e 53 74 72 69 6e 67 2c 20 63 6f 6e 73 74 20 63  nString, const c
348a6 68 61 72 20 2a 2a 70 53 74 72 69 6e 67 29 7b 0a  har **pString){.
348a7 20 20 69 6e 74 20 69 3b 0a 0a 20 20 66 6f 72 20    int i;..  for 
348a8 28 69 3d 30 20 3b 20 69 20 3c 20 6e 53 74 72 69  (i=0 ; i < nStri
348a9 6e 67 20 3b 20 2b 2b 69 29 20 7b 0a 20 20 20 20  ng ; ++i) {.    
348aa 69 66 28 20 70 53 74 72 69 6e 67 5b 69 5d 21 3d  if( pString[i]!=
348ab 4e 55 4c 4c 20 29 20 73 71 6c 69 74 65 33 5f 66  NULL ) sqlite3_f
348ac 72 65 65 28 28 76 6f 69 64 20 2a 29 20 70 53 74  ree((void *) pSt
348ad 72 69 6e 67 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20  ring[i]);.  }.  
348ae 73 71 6c 69 74 65 33 5f 66 72 65 65 28 28 76 6f  sqlite3_free((vo
348af 69 64 20 2a 29 20 70 53 74 72 69 6e 67 29 3b 0a  id *) pString);.
348b0 7d 0a 0a 2f 2a 20 73 65 6c 65 63 74 20 2a 20 66  }../* select * f
348b1 72 6f 6d 20 25 5f 63 6f 6e 74 65 6e 74 20 77 68  rom %_content wh
348b2 65 72 65 20 64 6f 63 69 64 20 3d 20 5b 69 44 6f  ere docid = [iDo
348b3 63 69 64 5d 0a 20 2a 20 54 68 65 20 63 61 6c 6c  cid]. * The call
348b4 65 72 20 6d 75 73 74 20 64 65 6c 65 74 65 20 74  er must delete t
348b5 68 65 20 72 65 74 75 72 6e 65 64 20 61 72 72 61  he returned arra
348b6 79 20 61 6e 64 20 61 6c 6c 20 73 74 72 69 6e 67  y and all string
348b7 73 20 69 6e 20 69 74 2e 0a 20 2a 20 6e 75 6c 6c  s in it.. * null
348b8 20 66 69 65 6c 64 73 20 77 69 6c 6c 20 62 65 20   fields will be 
348b9 4e 55 4c 4c 20 69 6e 20 74 68 65 20 72 65 74 75  NULL in the retu
348ba 72 6e 65 64 20 61 72 72 61 79 2e 0a 20 2a 0a 20  rned array.. *. 
348bb 2a 20 54 4f 44 4f 3a 20 50 65 72 68 61 70 73 20  * TODO: Perhaps 
348bc 77 65 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e  we should return
348bd 20 70 6f 69 6e 74 65 72 2f 6c 65 6e 67 74 68 20   pointer/length 
348be 73 74 72 69 6e 67 73 20 68 65 72 65 20 66 6f 72  strings here for
348bf 20 63 6f 6e 73 69 73 74 65 6e 63 79 0a 20 2a 20   consistency. * 
348c0 77 69 74 68 20 6f 74 68 65 72 20 63 6f 64 65 20  with other code 
348c1 77 68 69 63 68 20 75 73 65 73 20 70 6f 69 6e 74  which uses point
348c2 65 72 2f 6c 65 6e 67 74 68 2e 20 2a 2f 0a 73 74  er/length. */.st
348c3 61 74 69 63 20 69 6e 74 20 63 6f 6e 74 65 6e 74  atic int content
348c4 5f 73 65 6c 65 63 74 28 66 75 6c 6c 74 65 78 74  _select(fulltext
348c5 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65  _vtab *v, sqlite
348c6 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 2c 0a 20  _int64 iDocid,. 
348c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
348c8 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63           const c
348c9 68 61 72 20 2a 2a 2a 70 56 61 6c 75 65 73 29 7b  har ***pValues){
348ca 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
348cb 2a 73 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  *s;.  const char
348cc 20 2a 2a 76 61 6c 75 65 73 3b 0a 20 20 69 6e 74   **values;.  int
348cd 20 69 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20   i;.  int rc;.. 
348ce 20 2a 70 56 61 6c 75 65 73 20 3d 20 4e 55 4c 4c   *pValues = NULL
348cf 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 5f 67 65  ;..  rc = sql_ge
348d0 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 43  t_statement(v, C
348d1 4f 4e 54 45 4e 54 5f 53 45 4c 45 43 54 5f 53 54  ONTENT_SELECT_ST
348d2 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72  MT, &s);.  if( r
348d3 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
348d4 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20  eturn rc;..  rc 
348d5 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69  = sqlite3_bind_i
348d6 6e 74 36 34 28 73 2c 20 31 2c 20 69 44 6f 63 69  nt64(s, 1, iDoci
348d7 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  d);.  if( rc!=SQ
348d8 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
348d9 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c   rc;..  rc = sql
348da 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20  ite3_step(s);.  
348db 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52  if( rc!=SQLITE_R
348dc 4f 57 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OW ) return rc;.
348dd 0a 20 20 76 61 6c 75 65 73 20 3d 20 28 63 6f 6e  .  values = (con
348de 73 74 20 63 68 61 72 20 2a 2a 29 20 73 71 6c 69  st char **) sqli
348df 74 65 33 5f 6d 61 6c 6c 6f 63 28 76 2d 3e 6e 43  te3_malloc(v->nC
348e0 6f 6c 75 6d 6e 20 2a 20 73 69 7a 65 6f 66 28 63  olumn * sizeof(c
348e1 6f 6e 73 74 20 63 68 61 72 20 2a 29 29 3b 0a 20  onst char *));. 
348e2 20 66 6f 72 28 69 3d 30 3b 20 69 3c 76 2d 3e 6e   for(i=0; i<v->n
348e3 43 6f 6c 75 6d 6e 3b 20 2b 2b 69 29 7b 0a 20 20  Column; ++i){.  
348e4 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63 6f    if( sqlite3_co
348e5 6c 75 6d 6e 5f 74 79 70 65 28 73 2c 20 69 29 3d  lumn_type(s, i)=
348e6 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a  =SQLITE_NULL ){.
348e7 20 20 20 20 20 20 76 61 6c 75 65 73 5b 69 5d 20        values[i] 
348e8 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 7d 65 6c 73  = NULL;.    }els
348e9 65 7b 0a 20 20 20 20 20 20 76 61 6c 75 65 73 5b  e{.      values[
348ea 69 5d 20 3d 20 73 74 72 69 6e 67 5f 64 75 70 28  i] = string_dup(
348eb 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 63  (char*)sqlite3_c
348ec 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 2c 20 69 29  olumn_text(s, i)
348ed 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
348ee 2f 2a 20 57 65 20 65 78 70 65 63 74 20 6f 6e 6c  /* We expect onl
348ef 79 20 6f 6e 65 20 72 6f 77 2e 20 20 57 65 20 6d  y one row.  We m
348f0 75 73 74 20 65 78 65 63 75 74 65 20 61 6e 6f 74  ust execute anot
348f1 68 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65 70  her sqlite3_step
348f2 28 29 0a 20 20 20 2a 20 74 6f 20 63 6f 6d 70 6c  ().   * to compl
348f3 65 74 65 20 74 68 65 20 69 74 65 72 61 74 69 6f  ete the iteratio
348f4 6e 3b 20 6f 74 68 65 72 77 69 73 65 20 74 68 65  n; otherwise the
348f5 20 74 61 62 6c 65 20 77 69 6c 6c 20 72 65 6d 61   table will rema
348f6 69 6e 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20  in locked. */.  
348f7 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65  rc = sqlite3_ste
348f8 70 28 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  p(s);.  if( rc==
348f9 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20  SQLITE_DONE ){. 
348fa 20 20 20 2a 70 56 61 6c 75 65 73 20 3d 20 76 61     *pValues = va
348fb 6c 75 65 73 3b 0a 20 20 20 20 72 65 74 75 72 6e  lues;.    return
348fc 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
348fd 0a 20 20 66 72 65 65 53 74 72 69 6e 67 41 72 72  .  freeStringArr
348fe 61 79 28 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 76  ay(v->nColumn, v
348ff 61 6c 75 65 73 29 3b 0a 20 20 72 65 74 75 72 6e  alues);.  return
34900 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 64 65 6c 65 74   rc;.}../* delet
34901 65 20 66 72 6f 6d 20 25 5f 63 6f 6e 74 65 6e 74  e from %_content
34902 20 77 68 65 72 65 20 64 6f 63 69 64 20 3d 20 5b   where docid = [
34903 69 44 6f 63 69 64 20 5d 20 2a 2f 0a 73 74 61 74  iDocid ] */.stat
34904 69 63 20 69 6e 74 20 63 6f 6e 74 65 6e 74 5f 64  ic int content_d
34905 65 6c 65 74 65 28 66 75 6c 6c 74 65 78 74 5f 76  elete(fulltext_v
34906 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f 69  tab *v, sqlite_i
34907 6e 74 36 34 20 69 44 6f 63 69 64 29 7b 0a 20 20  nt64 iDocid){.  
34908 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b  sqlite3_stmt *s;
34909 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f  .  int rc = sql_
3490a 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c  get_statement(v,
3490b 20 43 4f 4e 54 45 4e 54 5f 44 45 4c 45 54 45 5f   CONTENT_DELETE_
3490c 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28  STMT, &s);.  if(
3490d 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
3490e 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72   return rc;..  r
3490f 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  c = sqlite3_bind
34910 5f 69 6e 74 36 34 28 73 2c 20 31 2c 20 69 44 6f  _int64(s, 1, iDo
34911 63 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  cid);.  if( rc!=
34912 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
34913 72 6e 20 72 63 3b 0a 0a 20 20 72 65 74 75 72 6e  rn rc;..  return
34914 20 73 71 6c 5f 73 69 6e 67 6c 65 5f 73 74 65 70   sql_single_step
34915 28 73 29 3b 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72  (s);.}../* Retur
34916 6e 73 20 53 51 4c 49 54 45 5f 52 4f 57 20 69 66  ns SQLITE_ROW if
34917 20 61 6e 79 20 72 6f 77 73 20 65 78 69 73 74 20   any rows exist 
34918 69 6e 20 25 5f 63 6f 6e 74 65 6e 74 2c 20 53 51  in %_content, SQ
34919 4c 49 54 45 5f 44 4f 4e 45 20 69 66 0a 2a 2a 20  LITE_DONE if.** 
3491a 6e 6f 20 72 6f 77 73 20 65 78 69 73 74 2c 20 61  no rows exist, a
3491b 6e 64 20 61 6e 79 20 65 72 72 6f 72 20 69 6e 20  nd any error in 
3491c 63 61 73 65 20 6f 66 20 66 61 69 6c 75 72 65 2e  case of failure.
3491d 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
3491e 6f 6e 74 65 6e 74 5f 65 78 69 73 74 73 28 66 75  ontent_exists(fu
3491f 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 29 7b  lltext_vtab *v){
34920 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
34921 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73  *s;.  int rc = s
34922 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74  ql_get_statement
34923 28 76 2c 20 43 4f 4e 54 45 4e 54 5f 45 58 49 53  (v, CONTENT_EXIS
34924 54 53 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20  TS_STMT, &s);.  
34925 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
34926 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
34927 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73    rc = sqlite3_s
34928 74 65 70 28 73 29 3b 0a 20 20 69 66 28 20 72 63  tep(s);.  if( rc
34929 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20 72  !=SQLITE_ROW ) r
3492a 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20  eturn rc;..  /* 
3492b 57 65 20 65 78 70 65 63 74 20 6f 6e 6c 79 20 6f  We expect only o
3492c 6e 65 20 72 6f 77 2e 20 20 57 65 20 6d 75 73 74  ne row.  We must
3492d 20 65 78 65 63 75 74 65 20 61 6e 6f 74 68 65 72   execute another
3492e 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 0a   sqlite3_step().
3492f 20 20 20 2a 20 74 6f 20 63 6f 6d 70 6c 65 74 65     * to complete
34930 20 74 68 65 20 69 74 65 72 61 74 69 6f 6e 3b 20   the iteration; 
34931 6f 74 68 65 72 77 69 73 65 20 74 68 65 20 74 61  otherwise the ta
34932 62 6c 65 20 77 69 6c 6c 20 72 65 6d 61 69 6e 20  ble will remain 
34933 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 72 63 20  locked. */.  rc 
34934 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73  = sqlite3_step(s
34935 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
34936 49 54 45 5f 44 4f 4e 45 20 29 20 72 65 74 75 72  ITE_DONE ) retur
34937 6e 20 53 51 4c 49 54 45 5f 52 4f 57 3b 0a 20 20  n SQLITE_ROW;.  
34938 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52  if( rc==SQLITE_R
34939 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  OW ) return SQLI
3493a 54 45 5f 45 52 52 4f 52 3b 0a 20 20 72 65 74 75  TE_ERROR;.  retu
3493b 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 69 6e 73  rn rc;.}../* ins
3493c 65 72 74 20 69 6e 74 6f 20 25 5f 73 65 67 6d 65  ert into %_segme
3493d 6e 74 73 20 76 61 6c 75 65 73 20 28 5b 70 44 61  nts values ([pDa
3493e 74 61 5d 29 0a 2a 2a 20 20 20 72 65 74 75 72 6e  ta]).**   return
3493f 73 20 61 73 73 69 67 6e 65 64 20 62 6c 6f 63 6b  s assigned block
34940 69 64 20 69 6e 20 2a 70 69 42 6c 6f 63 6b 69 64  id in *piBlockid
34941 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62  .*/.static int b
34942 6c 6f 63 6b 5f 69 6e 73 65 72 74 28 66 75 6c 6c  lock_insert(full
34943 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 63 6f  text_vtab *v, co
34944 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c  nst char *pData,
34945 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20 20   int nData,.    
34946 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34947 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34      sqlite_int64
34948 20 2a 70 69 42 6c 6f 63 6b 69 64 29 7b 0a 20 20   *piBlockid){.  
34949 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b  sqlite3_stmt *s;
3494a 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f  .  int rc = sql_
3494b 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c  get_statement(v,
3494c 20 42 4c 4f 43 4b 5f 49 4e 53 45 52 54 5f 53 54   BLOCK_INSERT_ST
3494d 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72  MT, &s);.  if( r
3494e 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
3494f 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20  eturn rc;..  rc 
34950 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62  = sqlite3_bind_b
34951 6c 6f 62 28 73 2c 20 31 2c 20 70 44 61 74 61 2c  lob(s, 1, pData,
34952 20 6e 44 61 74 61 2c 20 53 51 4c 49 54 45 5f 53   nData, SQLITE_S
34953 54 41 54 49 43 29 3b 0a 20 20 69 66 28 20 72 63  TATIC);.  if( rc
34954 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
34955 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d  turn rc;..  rc =
34956 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29   sqlite3_step(s)
34957 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
34958 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72 6e 20  TE_ROW ) return 
34959 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
3495a 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44  if( rc!=SQLITE_D
3495b 4f 4e 45 20 29 20 72 65 74 75 72 6e 20 72 63 3b  ONE ) return rc;
3495c 0a 0a 20 20 2f 2a 20 62 6c 6f 63 6b 69 64 20 63  ..  /* blockid c
3495d 6f 6c 75 6d 6e 20 69 73 20 61 6e 20 61 6c 69 61  olumn is an alia
3495e 73 20 66 6f 72 20 72 6f 77 69 64 2e 20 2a 2f 0a  s for rowid. */.
3495f 20 20 2a 70 69 42 6c 6f 63 6b 69 64 20 3d 20 73    *piBlockid = s
34960 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65  qlite3_last_inse
34961 72 74 5f 72 6f 77 69 64 28 76 2d 3e 64 62 29 3b  rt_rowid(v->db);
34962 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
34963 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 64 65 6c 65 74  _OK;.}../* delet
34964 65 20 66 72 6f 6d 20 25 5f 73 65 67 6d 65 6e 74  e from %_segment
34965 73 0a 2a 2a 20 20 20 77 68 65 72 65 20 62 6c 6f  s.**   where blo
34966 63 6b 69 64 20 62 65 74 77 65 65 6e 20 5b 69 53  ckid between [iS
34967 74 61 72 74 42 6c 6f 63 6b 69 64 5d 20 61 6e 64  tartBlockid] and
34968 20 5b 69 45 6e 64 42 6c 6f 63 6b 69 64 5d 0a 2a   [iEndBlockid].*
34969 2a 0a 2a 2a 20 44 65 6c 65 74 65 73 20 74 68 65  *.** Deletes the
3496a 20 72 61 6e 67 65 20 6f 66 20 62 6c 6f 63 6b 73   range of blocks
3496b 2c 20 69 6e 63 6c 75 73 69 76 65 2c 20 75 73 65  , inclusive, use
3496c 64 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 20  d to delete the 
3496d 62 6c 6f 63 6b 73 0a 2a 2a 20 77 68 69 63 68 20  blocks.** which 
3496e 66 6f 72 6d 20 61 20 73 65 67 6d 65 6e 74 2e 0a  form a segment..
3496f 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 6c  */.static int bl
34970 6f 63 6b 5f 64 65 6c 65 74 65 28 66 75 6c 6c 74  ock_delete(fullt
34971 65 78 74 5f 76 74 61 62 20 2a 76 2c 0a 20 20 20  ext_vtab *v,.   
34972 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34973 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
34974 34 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 2c  4 iStartBlockid,
34975 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 45   sqlite_int64 iE
34976 6e 64 42 6c 6f 63 6b 69 64 29 7b 0a 20 20 73 71  ndBlockid){.  sq
34977 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20  lite3_stmt *s;. 
34978 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65   int rc = sql_ge
34979 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 42  t_statement(v, B
3497a 4c 4f 43 4b 5f 44 45 4c 45 54 45 5f 53 54 4d 54  LOCK_DELETE_STMT
3497b 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21  , &s);.  if( rc!
3497c 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
3497d 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20  urn rc;..  rc = 
3497e 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
3497f 36 34 28 73 2c 20 31 2c 20 69 53 74 61 72 74 42  64(s, 1, iStartB
34980 6c 6f 63 6b 69 64 29 3b 0a 20 20 69 66 28 20 72  lockid);.  if( r
34981 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
34982 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20  eturn rc;..  rc 
34983 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69  = sqlite3_bind_i
34984 6e 74 36 34 28 73 2c 20 32 2c 20 69 45 6e 64 42  nt64(s, 2, iEndB
34985 6c 6f 63 6b 69 64 29 3b 0a 20 20 69 66 28 20 72  lockid);.  if( r
34986 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
34987 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 65 74  eturn rc;..  ret
34988 75 72 6e 20 73 71 6c 5f 73 69 6e 67 6c 65 5f 73  urn sql_single_s
34989 74 65 70 28 73 29 3b 0a 7d 0a 0a 2f 2a 20 52 65  tep(s);.}../* Re
3498a 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 52 4f 57  turns SQLITE_ROW
3498b 20 77 69 74 68 20 2a 70 69 64 78 20 73 65 74 20   with *pidx set 
3498c 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 73  to the maximum s
3498d 65 67 6d 65 6e 74 20 69 64 78 20 66 6f 75 6e 64  egment idx found
3498e 0a 2a 2a 20 61 74 20 69 4c 65 76 65 6c 2e 20 20  .** at iLevel.  
3498f 52 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 44  Returns SQLITE_D
34990 4f 4e 45 20 69 66 20 74 68 65 72 65 20 61 72 65  ONE if there are
34991 20 6e 6f 20 73 65 67 6d 65 6e 74 73 20 61 74 0a   no segments at.
34992 2a 2a 20 69 4c 65 76 65 6c 2e 20 20 4f 74 68 65  ** iLevel.  Othe
34993 72 77 69 73 65 20 72 65 74 75 72 6e 73 20 61 6e  rwise returns an
34994 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69   error..*/.stati
34995 63 20 69 6e 74 20 73 65 67 64 69 72 5f 6d 61 78  c int segdir_max
34996 5f 69 6e 64 65 78 28 66 75 6c 6c 74 65 78 74 5f  _index(fulltext_
34997 76 74 61 62 20 2a 76 2c 20 69 6e 74 20 69 4c 65  vtab *v, int iLe
34998 76 65 6c 2c 20 69 6e 74 20 2a 70 69 64 78 29 7b  vel, int *pidx){
34999 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
3499a 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73  *s;.  int rc = s
3499b 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74  ql_get_statement
3499c 28 76 2c 20 53 45 47 44 49 52 5f 4d 41 58 5f 49  (v, SEGDIR_MAX_I
3499d 4e 44 45 58 5f 53 54 4d 54 2c 20 26 73 29 3b 0a  NDEX_STMT, &s);.
3499e 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
3499f 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
349a0 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
349a1 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20 31 2c 20  _bind_int(s, 1, 
349a2 69 4c 65 76 65 6c 29 3b 0a 20 20 69 66 28 20 72  iLevel);.  if( r
349a3 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
349a4 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20  eturn rc;..  rc 
349a5 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73  = sqlite3_step(s
349a6 29 3b 0a 20 20 2f 2a 20 53 68 6f 75 6c 64 20 61  );.  /* Should a
349a7 6c 77 61 79 73 20 67 65 74 20 61 74 20 6c 65 61  lways get at lea
349a8 73 74 20 6f 6e 65 20 72 6f 77 20 64 75 65 20 74  st one row due t
349a9 6f 20 68 6f 77 20 6d 61 78 28 29 20 77 6f 72 6b  o how max() work
349aa 73 2e 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d  s. */.  if( rc==
349ab 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20 72 65  SQLITE_DONE ) re
349ac 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45  turn SQLITE_DONE
349ad 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
349ae 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72 6e 20  TE_ROW ) return 
349af 72 63 3b 0a 0a 20 20 2f 2a 20 4e 55 4c 4c 20 6d  rc;..  /* NULL m
349b0 65 61 6e 73 20 74 68 61 74 20 74 68 65 72 65 20  eans that there 
349b1 77 65 72 65 20 6e 6f 20 69 6e 70 75 74 73 20 74  were no inputs t
349b2 6f 20 6d 61 78 28 29 2e 20 2a 2f 0a 20 20 69 66  o max(). */.  if
349b3 28 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3d 3d 73  ( SQLITE_NULL==s
349b4 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79  qlite3_column_ty
349b5 70 65 28 73 2c 20 30 29 20 29 7b 0a 20 20 20 20  pe(s, 0) ){.    
349b6 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65  rc = sqlite3_ste
349b7 70 28 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63  p(s);.    if( rc
349b8 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20 72  ==SQLITE_ROW ) r
349b9 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
349ba 4f 52 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72  OR;.    return r
349bb 63 3b 0a 20 20 7d 0a 0a 20 20 2a 70 69 64 78 20  c;.  }..  *pidx 
349bc 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
349bd 5f 69 6e 74 28 73 2c 20 30 29 3b 0a 0a 20 20 2f  _int(s, 0);..  /
349be 2a 20 57 65 20 65 78 70 65 63 74 20 6f 6e 6c 79  * We expect only
349bf 20 6f 6e 65 20 72 6f 77 2e 20 20 57 65 20 6d 75   one row.  We mu
349c0 73 74 20 65 78 65 63 75 74 65 20 61 6e 6f 74 68  st execute anoth
349c1 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  er sqlite3_step(
349c2 29 0a 20 20 20 2a 20 74 6f 20 63 6f 6d 70 6c 65  ).   * to comple
349c3 74 65 20 74 68 65 20 69 74 65 72 61 74 69 6f 6e  te the iteration
349c4 3b 20 6f 74 68 65 72 77 69 73 65 20 74 68 65 20  ; otherwise the 
349c5 74 61 62 6c 65 20 77 69 6c 6c 20 72 65 6d 61 69  table will remai
349c6 6e 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 72  n locked. */.  r
349c7 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  c = sqlite3_step
349c8 28 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  (s);.  if( rc==S
349c9 51 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75  QLITE_ROW ) retu
349ca 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
349cb 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
349cc 45 5f 44 4f 4e 45 20 29 20 72 65 74 75 72 6e 20  E_DONE ) return 
349cd 72 63 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  rc;.  return SQL
349ce 49 54 45 5f 52 4f 57 3b 0a 7d 0a 0a 2f 2a 20 69  ITE_ROW;.}../* i
349cf 6e 73 65 72 74 20 69 6e 74 6f 20 25 5f 73 65 67  nsert into %_seg
349d0 64 69 72 20 76 61 6c 75 65 73 20 28 0a 2a 2a 20  dir values (.** 
349d1 20 20 5b 69 4c 65 76 65 6c 5d 2c 20 5b 69 64 78    [iLevel], [idx
349d2 5d 2c 0a 2a 2a 20 20 20 5b 69 53 74 61 72 74 42  ],.**   [iStartB
349d3 6c 6f 63 6b 69 64 5d 2c 20 5b 69 4c 65 61 76 65  lockid], [iLeave
349d4 73 45 6e 64 42 6c 6f 63 6b 69 64 5d 2c 20 5b 69  sEndBlockid], [i
349d5 45 6e 64 42 6c 6f 63 6b 69 64 5d 2c 0a 2a 2a 20  EndBlockid],.** 
349d6 20 20 5b 70 52 6f 6f 74 44 61 74 61 5d 0a 2a 2a    [pRootData].**
349d7 20 29 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74   ).*/.static int
349d8 20 73 65 67 64 69 72 5f 73 65 74 28 66 75 6c 6c   segdir_set(full
349d9 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 69 6e  text_vtab *v, in
349da 74 20 69 4c 65 76 65 6c 2c 20 69 6e 74 20 69 64  t iLevel, int id
349db 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  x,.             
349dc 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f           sqlite_
349dd 69 6e 74 36 34 20 69 53 74 61 72 74 42 6c 6f 63  int64 iStartBloc
349de 6b 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20  kid,.           
349df 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
349e0 65 5f 69 6e 74 36 34 20 69 4c 65 61 76 65 73 45  e_int64 iLeavesE
349e1 6e 64 42 6c 6f 63 6b 69 64 2c 0a 20 20 20 20 20  ndBlockid,.     
349e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
349e3 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 45   sqlite_int64 iE
349e4 6e 64 42 6c 6f 63 6b 69 64 2c 0a 20 20 20 20 20  ndBlockid,.     
349e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
349e6 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 52 6f   const char *pRo
349e7 6f 74 44 61 74 61 2c 20 69 6e 74 20 6e 52 6f 6f  otData, int nRoo
349e8 74 44 61 74 61 29 7b 0a 20 20 73 71 6c 69 74 65  tData){.  sqlite
349e9 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74  3_stmt *s;.  int
349ea 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74   rc = sql_get_st
349eb 61 74 65 6d 65 6e 74 28 76 2c 20 53 45 47 44 49  atement(v, SEGDI
349ec 52 5f 53 45 54 5f 53 54 4d 54 2c 20 26 73 29 3b  R_SET_STMT, &s);
349ed 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
349ee 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
349ef 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  ;..  rc = sqlite
349f0 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20 31 2c  3_bind_int(s, 1,
349f1 20 69 4c 65 76 65 6c 29 3b 0a 20 20 69 66 28 20   iLevel);.  if( 
349f2 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
349f3 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63  return rc;..  rc
349f4 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f   = sqlite3_bind_
349f5 69 6e 74 28 73 2c 20 32 2c 20 69 64 78 29 3b 0a  int(s, 2, idx);.
349f6 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
349f7 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
349f8 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
349f9 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20 33  _bind_int64(s, 3
349fa 2c 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 29  , iStartBlockid)
349fb 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
349fc 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
349fd 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  c;..  rc = sqlit
349fe 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c  e3_bind_int64(s,
349ff 20 34 2c 20 69 4c 65 61 76 65 73 45 6e 64 42 6c   4, iLeavesEndBl
34a00 6f 63 6b 69 64 29 3b 0a 20 20 69 66 28 20 72 63  ockid);.  if( rc
34a01 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
34a02 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d  turn rc;..  rc =
34a03 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
34a04 74 36 34 28 73 2c 20 35 2c 20 69 45 6e 64 42 6c  t64(s, 5, iEndBl
34a05 6f 63 6b 69 64 29 3b 0a 20 20 69 66 28 20 72 63  ockid);.  if( rc
34a06 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
34a07 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d  turn rc;..  rc =
34a08 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c   sqlite3_bind_bl
34a09 6f 62 28 73 2c 20 36 2c 20 70 52 6f 6f 74 44 61  ob(s, 6, pRootDa
34a0a 74 61 2c 20 6e 52 6f 6f 74 44 61 74 61 2c 20 53  ta, nRootData, S
34a0b 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
34a0c 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
34a0d 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
34a0e 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 5f 73 69  .  return sql_si
34a0f 6e 67 6c 65 5f 73 74 65 70 28 73 29 3b 0a 7d 0a  ngle_step(s);.}.
34a10 0a 2f 2a 20 51 75 65 72 69 65 73 20 25 5f 73 65  ./* Queries %_se
34a11 67 64 69 72 20 66 6f 72 20 74 68 65 20 62 6c 6f  gdir for the blo
34a12 63 6b 20 73 70 61 6e 20 6f 66 20 74 68 65 20 73  ck span of the s
34a13 65 67 6d 65 6e 74 73 20 69 6e 20 6c 65 76 65 6c  egments in level
34a14 0a 2a 2a 20 69 4c 65 76 65 6c 2e 20 20 52 65 74  .** iLevel.  Ret
34a15 75 72 6e 73 20 53 51 4c 49 54 45 5f 44 4f 4e 45  urns SQLITE_DONE
34a16 20 69 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f   if there are no
34a17 20 62 6c 6f 63 6b 73 20 66 6f 72 20 69 4c 65 76   blocks for iLev
34a18 65 6c 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 52 4f  el,.** SQLITE_RO
34a19 57 20 69 66 20 74 68 65 72 65 20 61 72 65 20 62  W if there are b
34a1a 6c 6f 63 6b 73 2c 20 65 6c 73 65 20 61 6e 20 65  locks, else an e
34a1b 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rror..*/.static 
34a1c 69 6e 74 20 73 65 67 64 69 72 5f 73 70 61 6e 28  int segdir_span(
34a1d 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
34a1e 2c 20 69 6e 74 20 69 4c 65 76 65 6c 2c 0a 20 20  , int iLevel,.  
34a1f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34a20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
34a21 34 20 2a 70 69 53 74 61 72 74 42 6c 6f 63 6b 69  4 *piStartBlocki
34a22 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  d,.             
34a23 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
34a24 5f 69 6e 74 36 34 20 2a 70 69 45 6e 64 42 6c 6f  _int64 *piEndBlo
34a25 63 6b 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33  ckid){.  sqlite3
34a26 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20  _stmt *s;.  int 
34a27 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61  rc = sql_get_sta
34a28 74 65 6d 65 6e 74 28 76 2c 20 53 45 47 44 49 52  tement(v, SEGDIR
34a29 5f 53 50 41 4e 5f 53 54 4d 54 2c 20 26 73 29 3b  _SPAN_STMT, &s);
34a2a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
34a2b 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
34a2c 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  ;..  rc = sqlite
34a2d 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20 31 2c  3_bind_int(s, 1,
34a2e 20 69 4c 65 76 65 6c 29 3b 0a 20 20 69 66 28 20   iLevel);.  if( 
34a2f 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
34a30 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63  return rc;..  rc
34a31 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
34a32 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  s);.  if( rc==SQ
34a33 4c 49 54 45 5f 44 4f 4e 45 20 29 20 72 65 74 75  LITE_DONE ) retu
34a34 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 20  rn SQLITE_DONE; 
34a35 20 2f 2a 20 53 68 6f 75 6c 64 20 6e 65 76 65 72   /* Should never
34a36 20 68 61 70 70 65 6e 20 2a 2f 0a 20 20 69 66 28   happen */.  if(
34a37 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20   rc!=SQLITE_ROW 
34a38 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
34a39 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20  /* This happens 
34a3a 69 66 20 61 6c 6c 20 73 65 67 6d 65 6e 74 73 20  if all segments 
34a3b 61 74 20 74 68 69 73 20 6c 65 76 65 6c 20 61 72  at this level ar
34a3c 65 20 65 6e 74 69 72 65 6c 79 20 69 6e 6c 69 6e  e entirely inlin
34a3d 65 2e 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49  e. */.  if( SQLI
34a3e 54 45 5f 4e 55 4c 4c 3d 3d 73 71 6c 69 74 65 33  TE_NULL==sqlite3
34a3f 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73 2c 20  _column_type(s, 
34a40 30 29 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20  0) ){.    /* We 
34a41 65 78 70 65 63 74 20 6f 6e 6c 79 20 6f 6e 65 20  expect only one 
34a42 72 6f 77 2e 20 20 57 65 20 6d 75 73 74 20 65 78  row.  We must ex
34a43 65 63 75 74 65 20 61 6e 6f 74 68 65 72 20 73 71  ecute another sq
34a44 6c 69 74 65 33 5f 73 74 65 70 28 29 0a 20 20 20  lite3_step().   
34a45 20 20 2a 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20    * to complete 
34a46 74 68 65 20 69 74 65 72 61 74 69 6f 6e 3b 20 6f  the iteration; o
34a47 74 68 65 72 77 69 73 65 20 74 68 65 20 74 61 62  therwise the tab
34a48 6c 65 20 77 69 6c 6c 20 72 65 6d 61 69 6e 20 6c  le will remain l
34a49 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 20 20 69 6e  ocked. */.    in
34a4a 74 20 72 63 32 20 3d 20 73 71 6c 69 74 65 33 5f  t rc2 = sqlite3_
34a4b 73 74 65 70 28 73 29 3b 0a 20 20 20 20 69 66 28  step(s);.    if(
34a4c 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f 52 4f 57   rc2==SQLITE_ROW
34a4d 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
34a4e 5f 45 52 52 4f 52 3b 0a 20 20 20 20 72 65 74 75  _ERROR;.    retu
34a4f 72 6e 20 72 63 32 3b 0a 20 20 7d 0a 0a 20 20 2a  rn rc2;.  }..  *
34a50 70 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 20 3d  piStartBlockid =
34a51 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
34a52 69 6e 74 36 34 28 73 2c 20 30 29 3b 0a 20 20 2a  int64(s, 0);.  *
34a53 70 69 45 6e 64 42 6c 6f 63 6b 69 64 20 3d 20 73  piEndBlockid = s
34a54 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
34a55 74 36 34 28 73 2c 20 31 29 3b 0a 0a 20 20 2f 2a  t64(s, 1);..  /*
34a56 20 57 65 20 65 78 70 65 63 74 20 6f 6e 6c 79 20   We expect only 
34a57 6f 6e 65 20 72 6f 77 2e 20 20 57 65 20 6d 75 73  one row.  We mus
34a58 74 20 65 78 65 63 75 74 65 20 61 6e 6f 74 68 65  t execute anothe
34a59 72 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  r sqlite3_step()
34a5a 0a 20 20 20 2a 20 74 6f 20 63 6f 6d 70 6c 65 74  .   * to complet
34a5b 65 20 74 68 65 20 69 74 65 72 61 74 69 6f 6e 3b  e the iteration;
34a5c 20 6f 74 68 65 72 77 69 73 65 20 74 68 65 20 74   otherwise the t
34a5d 61 62 6c 65 20 77 69 6c 6c 20 72 65 6d 61 69 6e  able will remain
34a5e 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 72 63   locked. */.  rc
34a5f 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
34a60 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  s);.  if( rc==SQ
34a61 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72  LITE_ROW ) retur
34a62 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
34a63 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
34a64 5f 44 4f 4e 45 20 29 20 72 65 74 75 72 6e 20 72  _DONE ) return r
34a65 63 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  c;.  return SQLI
34a66 54 45 5f 52 4f 57 3b 0a 7d 0a 0a 2f 2a 20 44 65  TE_ROW;.}../* De
34a67 6c 65 74 65 20 74 68 65 20 73 65 67 6d 65 6e 74  lete the segment
34a68 20 62 6c 6f 63 6b 73 20 61 6e 64 20 73 65 67 6d   blocks and segm
34a69 65 6e 74 20 64 69 72 65 63 74 6f 72 79 20 72 65  ent directory re
34a6a 63 6f 72 64 73 20 66 6f 72 20 61 6c 6c 0a 2a 2a  cords for all.**
34a6b 20 73 65 67 6d 65 6e 74 73 20 61 74 20 69 4c 65   segments at iLe
34a6c 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  vel..*/.static i
34a6d 6e 74 20 73 65 67 64 69 72 5f 64 65 6c 65 74 65  nt segdir_delete
34a6e 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
34a6f 76 2c 20 69 6e 74 20 69 4c 65 76 65 6c 29 7b 0a  v, int iLevel){.
34a70 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
34a71 73 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  s;.  sqlite_int6
34a72 34 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 2c  4 iStartBlockid,
34a73 20 69 45 6e 64 42 6c 6f 63 6b 69 64 3b 0a 20 20   iEndBlockid;.  
34a74 69 6e 74 20 72 63 20 3d 20 73 65 67 64 69 72 5f  int rc = segdir_
34a75 73 70 61 6e 28 76 2c 20 69 4c 65 76 65 6c 2c 20  span(v, iLevel, 
34a76 26 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 2c 20  &iStartBlockid, 
34a77 26 69 45 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a 20  &iEndBlockid);. 
34a78 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
34a79 52 4f 57 20 26 26 20 72 63 21 3d 53 51 4c 49 54  ROW && rc!=SQLIT
34a7a 45 5f 44 4f 4e 45 20 29 20 72 65 74 75 72 6e 20  E_DONE ) return 
34a7b 72 63 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53  rc;..  if( rc==S
34a7c 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20  QLITE_ROW ){.   
34a7d 20 72 63 20 3d 20 62 6c 6f 63 6b 5f 64 65 6c 65   rc = block_dele
34a7e 74 65 28 76 2c 20 69 53 74 61 72 74 42 6c 6f 63  te(v, iStartBloc
34a7f 6b 69 64 2c 20 69 45 6e 64 42 6c 6f 63 6b 69 64  kid, iEndBlockid
34a80 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
34a81 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
34a82 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  n rc;.  }..  /* 
34a83 44 65 6c 65 74 65 20 74 68 65 20 73 65 67 6d 65  Delete the segme
34a84 6e 74 20 64 69 72 65 63 74 6f 72 79 20 69 74 73  nt directory its
34a85 65 6c 66 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 73  elf. */.  rc = s
34a86 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74  ql_get_statement
34a87 28 76 2c 20 53 45 47 44 49 52 5f 44 45 4c 45 54  (v, SEGDIR_DELET
34a88 45 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69  E_STMT, &s);.  i
34a89 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
34a8a 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
34a8b 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
34a8c 6e 64 5f 69 6e 74 36 34 28 73 2c 20 31 2c 20 69  nd_int64(s, 1, i
34a8d 4c 65 76 65 6c 29 3b 0a 20 20 69 66 28 20 72 63  Level);.  if( rc
34a8e 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
34a8f 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 65 74 75  turn rc;..  retu
34a90 72 6e 20 73 71 6c 5f 73 69 6e 67 6c 65 5f 73 74  rn sql_single_st
34a91 65 70 28 73 29 3b 0a 7d 0a 0a 2f 2a 20 44 65 6c  ep(s);.}../* Del
34a92 65 74 65 20 65 6e 74 69 72 65 20 66 74 73 20 69  ete entire fts i
34a93 6e 64 65 78 2c 20 53 51 4c 49 54 45 5f 4f 4b 20  ndex, SQLITE_OK 
34a94 6f 6e 20 73 75 63 63 65 73 73 2c 20 72 65 6c 65  on success, rele
34a95 76 61 6e 74 20 65 72 72 6f 72 20 6f 6e 0a 2a 2a  vant error on.**
34a96 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61   failure..*/.sta
34a97 74 69 63 20 69 6e 74 20 73 65 67 64 69 72 5f 64  tic int segdir_d
34a98 65 6c 65 74 65 5f 61 6c 6c 28 66 75 6c 6c 74 65  elete_all(fullte
34a99 78 74 5f 76 74 61 62 20 2a 76 29 7b 0a 20 20 73  xt_vtab *v){.  s
34a9a 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a  qlite3_stmt *s;.
34a9b 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67    int rc = sql_g
34a9c 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20  et_statement(v, 
34a9d 53 45 47 44 49 52 5f 44 45 4c 45 54 45 5f 41 4c  SEGDIR_DELETE_AL
34a9e 4c 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69  L_STMT, &s);.  i
34a9f 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
34aa0 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
34aa1 20 72 63 20 3d 20 73 71 6c 5f 73 69 6e 67 6c 65   rc = sql_single
34aa2 5f 73 74 65 70 28 73 29 3b 0a 20 20 69 66 28 20  _step(s);.  if( 
34aa3 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
34aa4 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63  return rc;..  rc
34aa5 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65   = sql_get_state
34aa6 6d 65 6e 74 28 76 2c 20 42 4c 4f 43 4b 5f 44 45  ment(v, BLOCK_DE
34aa7 4c 45 54 45 5f 41 4c 4c 5f 53 54 4d 54 2c 20 26  LETE_ALL_STMT, &
34aa8 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  s);.  if( rc!=SQ
34aa9 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
34aaa 20 72 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20 73   rc;..  return s
34aab 71 6c 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28 73  ql_single_step(s
34aac 29 3b 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e 73  );.}../* Returns
34aad 20 53 51 4c 49 54 45 5f 4f 4b 20 77 69 74 68 20   SQLITE_OK with 
34aae 2a 70 6e 53 65 67 6d 65 6e 74 73 20 73 65 74 20  *pnSegments set 
34aaf 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  to the number of
34ab0 20 65 6e 74 72 69 65 73 20 69 6e 0a 2a 2a 20 25   entries in.** %
34ab1 5f 73 65 67 64 69 72 20 61 6e 64 20 2a 70 69 4d  _segdir and *piM
34ab2 61 78 4c 65 76 65 6c 20 73 65 74 20 74 6f 20 74  axLevel set to t
34ab3 68 65 20 68 69 67 68 65 73 74 20 6c 65 76 65 6c  he highest level
34ab4 20 77 68 69 63 68 20 68 61 73 20 61 0a 2a 2a 20   which has a.** 
34ab5 73 65 67 6d 65 6e 74 2e 20 20 4f 74 68 65 72 77  segment.  Otherw
34ab6 69 73 65 20 72 65 74 75 72 6e 73 20 74 68 65 20  ise returns the 
34ab7 53 51 4c 69 74 65 20 65 72 72 6f 72 20 77 68 69  SQLite error whi
34ab8 63 68 20 63 61 75 73 65 64 20 66 61 69 6c 75 72  ch caused failur
34ab9 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
34aba 20 73 65 67 64 69 72 5f 63 6f 75 6e 74 28 66 75   segdir_count(fu
34abb 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
34abc 69 6e 74 20 2a 70 6e 53 65 67 6d 65 6e 74 73 2c  int *pnSegments,
34abd 20 69 6e 74 20 2a 70 69 4d 61 78 4c 65 76 65 6c   int *piMaxLevel
34abe 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  ){.  sqlite3_stm
34abf 74 20 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20 3d  t *s;.  int rc =
34ac0 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65   sql_get_stateme
34ac1 6e 74 28 76 2c 20 53 45 47 44 49 52 5f 43 4f 55  nt(v, SEGDIR_COU
34ac2 4e 54 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20  NT_STMT, &s);.  
34ac3 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
34ac4 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
34ac5 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73    rc = sqlite3_s
34ac6 74 65 70 28 73 29 3b 0a 20 20 2f 2a 20 54 4f 44  tep(s);.  /* TOD
34ac7 4f 28 73 68 65 73 73 29 3a 20 54 68 69 73 20 63  O(shess): This c
34ac8 61 73 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62  ase should not b
34ac9 65 20 70 6f 73 73 69 62 6c 65 3f 20 20 53 68 6f  e possible?  Sho
34aca 75 6c 64 20 73 74 72 6f 6e 67 65 72 0a 20 20 2a  uld stronger.  *
34acb 2a 20 6d 65 61 73 75 72 65 73 20 62 65 20 74 61  * measures be ta
34acc 6b 65 6e 20 69 66 20 69 74 20 68 61 70 70 65 6e  ken if it happen
34acd 73 3f 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63  s?.  */.  if( rc
34ace 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b  ==SQLITE_DONE ){
34acf 0a 20 20 20 20 2a 70 6e 53 65 67 6d 65 6e 74 73  .    *pnSegments
34ad0 20 3d 20 30 3b 0a 20 20 20 20 2a 70 69 4d 61 78   = 0;.    *piMax
34ad1 4c 65 76 65 6c 20 3d 20 30 3b 0a 20 20 20 20 72  Level = 0;.    r
34ad2 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
34ad3 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 21 3d 53  .  }.  if( rc!=S
34ad4 51 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75  QLITE_ROW ) retu
34ad5 72 6e 20 72 63 3b 0a 0a 20 20 2a 70 6e 53 65 67  rn rc;..  *pnSeg
34ad6 6d 65 6e 74 73 20 3d 20 73 71 6c 69 74 65 33 5f  ments = sqlite3_
34ad7 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73 2c 20 30 29  column_int(s, 0)
34ad8 3b 0a 20 20 2a 70 69 4d 61 78 4c 65 76 65 6c 20  ;.  *piMaxLevel 
34ad9 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
34ada 5f 69 6e 74 28 73 2c 20 31 29 3b 0a 0a 20 20 2f  _int(s, 1);..  /
34adb 2a 20 57 65 20 65 78 70 65 63 74 20 6f 6e 6c 79  * We expect only
34adc 20 6f 6e 65 20 72 6f 77 2e 20 20 57 65 20 6d 75   one row.  We mu
34add 73 74 20 65 78 65 63 75 74 65 20 61 6e 6f 74 68  st execute anoth
34ade 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  er sqlite3_step(
34adf 29 0a 20 20 20 2a 20 74 6f 20 63 6f 6d 70 6c 65  ).   * to comple
34ae0 74 65 20 74 68 65 20 69 74 65 72 61 74 69 6f 6e  te the iteration
34ae1 3b 20 6f 74 68 65 72 77 69 73 65 20 74 68 65 20  ; otherwise the 
34ae2 74 61 62 6c 65 20 77 69 6c 6c 20 72 65 6d 61 69  table will remai
34ae3 6e 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 72  n locked. */.  r
34ae4 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  c = sqlite3_step
34ae5 28 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  (s);.  if( rc==S
34ae6 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20 72 65 74  QLITE_DONE ) ret
34ae7 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
34ae8 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
34ae9 52 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c  ROW ) return SQL
34aea 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 72 65 74  ITE_ERROR;.  ret
34aeb 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 4f  urn rc;.}../* TO
34aec 44 4f 28 73 68 65 73 73 29 20 63 6c 65 61 72 50  DO(shess) clearP
34aed 65 6e 64 69 6e 67 54 65 72 6d 73 28 29 20 69 73  endingTerms() is
34aee 20 66 61 72 20 64 6f 77 6e 20 74 68 65 20 66 69   far down the fi
34aef 6c 65 20 62 65 63 61 75 73 65 0a 2a 2a 20 77 72  le because.** wr
34af0 69 74 65 5a 65 72 6f 53 65 67 6d 65 6e 74 28 29  iteZeroSegment()
34af1 20 69 73 20 66 61 72 20 64 6f 77 6e 20 74 68 65   is far down the
34af2 20 66 69 6c 65 20 62 65 63 61 75 73 65 20 4c 65   file because Le
34af3 61 66 57 72 69 74 65 72 20 69 73 20 66 61 72 0a  afWriter is far.
34af4 2a 2a 20 64 6f 77 6e 20 74 68 65 20 66 69 6c 65  ** down the file
34af5 2e 20 20 43 6f 6e 73 69 64 65 72 20 72 65 66 61  .  Consider refa
34af6 63 74 6f 72 69 6e 67 20 74 68 65 20 63 6f 64 65  ctoring the code
34af7 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 6e 6f 6e   to move the non
34af8 2d 76 74 61 62 0a 2a 2a 20 63 6f 64 65 20 61 62  -vtab.** code ab
34af9 6f 76 65 20 74 68 65 20 76 74 61 62 20 63 6f 64  ove the vtab cod
34afa 65 20 73 6f 20 74 68 61 74 20 77 65 20 64 6f 6e  e so that we don
34afb 27 74 20 6e 65 65 64 20 74 68 69 73 20 66 6f 72  't need this for
34afc 77 61 72 64 0a 2a 2a 20 72 65 66 65 72 65 6e 63  ward.** referenc
34afd 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
34afe 20 63 6c 65 61 72 50 65 6e 64 69 6e 67 54 65 72   clearPendingTer
34aff 6d 73 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  ms(fulltext_vtab
34b00 20 2a 76 29 3b 0a 0a 2f 2a 0a 2a 2a 20 46 72 65   *v);../*.** Fre
34b01 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 75 73 65  e the memory use
34b02 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20 66  d to contain a f
34b03 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 73 74 72  ulltext_vtab str
34b04 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69  ucture..*/.stati
34b05 63 20 76 6f 69 64 20 66 75 6c 6c 74 65 78 74 5f  c void fulltext_
34b06 76 74 61 62 5f 64 65 73 74 72 6f 79 28 66 75 6c  vtab_destroy(ful
34b07 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 29 7b 0a  ltext_vtab *v){.
34b08 20 20 69 6e 74 20 69 53 74 6d 74 2c 20 69 3b 0a    int iStmt, i;.
34b09 0a 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54  .  FTSTRACE(("FT
34b0a 53 33 20 44 65 73 74 72 6f 79 20 25 70 5c 6e 22  S3 Destroy %p\n"
34b0b 2c 20 76 29 29 3b 0a 20 20 66 6f 72 28 20 69 53  , v));.  for( iS
34b0c 74 6d 74 3d 30 3b 20 69 53 74 6d 74 3c 4d 41 58  tmt=0; iStmt<MAX
34b0d 5f 53 54 4d 54 3b 20 69 53 74 6d 74 2b 2b 20 29  _STMT; iStmt++ )
34b0e 7b 0a 20 20 20 20 69 66 28 20 76 2d 3e 70 46 75  {.    if( v->pFu
34b0f 6c 6c 74 65 78 74 53 74 61 74 65 6d 65 6e 74 73  lltextStatements
34b10 5b 69 53 74 6d 74 5d 21 3d 4e 55 4c 4c 20 29 7b  [iStmt]!=NULL ){
34b11 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
34b12 69 6e 61 6c 69 7a 65 28 76 2d 3e 70 46 75 6c 6c  inalize(v->pFull
34b13 74 65 78 74 53 74 61 74 65 6d 65 6e 74 73 5b 69  textStatements[i
34b14 53 74 6d 74 5d 29 3b 0a 20 20 20 20 20 20 76 2d  Stmt]);.      v-
34b15 3e 70 46 75 6c 6c 74 65 78 74 53 74 61 74 65 6d  >pFulltextStatem
34b16 65 6e 74 73 5b 69 53 74 6d 74 5d 20 3d 20 4e 55  ents[iStmt] = NU
34b17 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  LL;.    }.  }.. 
34b18 20 66 6f 72 28 20 69 3d 30 3b 20 69 3c 4d 45 52   for( i=0; i<MER
34b19 47 45 5f 43 4f 55 4e 54 3b 20 69 2b 2b 20 29 7b  GE_COUNT; i++ ){
34b1a 0a 20 20 20 20 69 66 28 20 76 2d 3e 70 4c 65 61  .    if( v->pLea
34b1b 66 53 65 6c 65 63 74 53 74 6d 74 73 5b 69 5d 21  fSelectStmts[i]!
34b1c 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 73  =NULL ){.      s
34b1d 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
34b1e 76 2d 3e 70 4c 65 61 66 53 65 6c 65 63 74 53 74  v->pLeafSelectSt
34b1f 6d 74 73 5b 69 5d 29 3b 0a 20 20 20 20 20 20 76  mts[i]);.      v
34b20 2d 3e 70 4c 65 61 66 53 65 6c 65 63 74 53 74 6d  ->pLeafSelectStm
34b21 74 73 5b 69 5d 20 3d 20 4e 55 4c 4c 3b 0a 20 20  ts[i] = NULL;.  
34b22 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 76    }.  }..  if( v
34b23 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 21 3d 4e 55  ->pTokenizer!=NU
34b24 4c 4c 20 29 7b 0a 20 20 20 20 76 2d 3e 70 54 6f  LL ){.    v->pTo
34b25 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65  kenizer->pModule
34b26 2d 3e 78 44 65 73 74 72 6f 79 28 76 2d 3e 70 54  ->xDestroy(v->pT
34b27 6f 6b 65 6e 69 7a 65 72 29 3b 0a 20 20 20 20 76  okenizer);.    v
34b28 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 4e  ->pTokenizer = N
34b29 55 4c 4c 3b 0a 20 20 7d 0a 0a 20 20 63 6c 65 61  ULL;.  }..  clea
34b2a 72 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 76 29  rPendingTerms(v)
34b2b 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  ;..  sqlite3_fre
34b2c 65 28 76 2d 3e 61 7a 43 6f 6c 75 6d 6e 29 3b 0a  e(v->azColumn);.
34b2d 20 20 66 6f 72 28 69 20 3d 20 30 3b 20 69 20 3c    for(i = 0; i <
34b2e 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 2b 2b 69   v->nColumn; ++i
34b2f 29 20 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ) {.    sqlite3_
34b30 66 72 65 65 28 76 2d 3e 61 7a 43 6f 6e 74 65 6e  free(v->azConten
34b31 74 43 6f 6c 75 6d 6e 5b 69 5d 29 3b 0a 20 20 7d  tColumn[i]);.  }
34b32 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
34b33 76 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75  v->azContentColu
34b34 6d 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  mn);.  sqlite3_f
34b35 72 65 65 28 76 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ree(v);.}../*.**
34b36 20 54 6f 6b 65 6e 20 74 79 70 65 73 20 66 6f 72   Token types for
34b37 20 70 61 72 73 69 6e 67 20 74 68 65 20 61 72 67   parsing the arg
34b38 75 6d 65 6e 74 73 20 74 6f 20 78 43 6f 6e 6e 65  uments to xConne
34b39 63 74 20 6f 72 20 78 43 72 65 61 74 65 2e 0a 2a  ct or xCreate..*
34b3a 2f 0a 23 64 65 66 69 6e 65 20 54 4f 4b 45 4e 5f  /.#define TOKEN_
34b3b 45 4f 46 20 20 20 20 20 20 20 20 20 30 20 20 20  EOF         0   
34b3c 20 2f 2a 20 45 6e 64 20 6f 66 20 66 69 6c 65 20   /* End of file 
34b3d 2a 2f 0a 23 64 65 66 69 6e 65 20 54 4f 4b 45 4e  */.#define TOKEN
34b3e 5f 53 50 41 43 45 20 20 20 20 20 20 20 31 20 20  _SPACE       1  
34b3f 20 20 2f 2a 20 41 6e 79 20 6b 69 6e 64 20 6f 66    /* Any kind of
34b40 20 77 68 69 74 65 73 70 61 63 65 20 2a 2f 0a 23   whitespace */.#
34b41 64 65 66 69 6e 65 20 54 4f 4b 45 4e 5f 49 44 20  define TOKEN_ID 
34b42 20 20 20 20 20 20 20 20 20 32 20 20 20 20 2f 2a           2    /*
34b43 20 41 6e 20 69 64 65 6e 74 69 66 69 65 72 20 2a   An identifier *
34b44 2f 0a 23 64 65 66 69 6e 65 20 54 4f 4b 45 4e 5f  /.#define TOKEN_
34b45 53 54 52 49 4e 47 20 20 20 20 20 20 33 20 20 20  STRING      3   
34b46 20 2f 2a 20 41 20 73 74 72 69 6e 67 20 6c 69 74   /* A string lit
34b47 65 72 61 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20  eral */.#define 
34b48 54 4f 4b 45 4e 5f 50 55 4e 43 54 20 20 20 20 20  TOKEN_PUNCT     
34b49 20 20 34 20 20 20 20 2f 2a 20 41 20 73 69 6e 67    4    /* A sing
34b4a 6c 65 20 70 75 6e 63 74 75 61 74 69 6f 6e 20 63  le punctuation c
34b4b 68 61 72 61 63 74 65 72 20 2a 2f 0a 0a 2f 2a 0a  haracter */../*.
34b4c 2a 2a 20 49 66 20 58 20 69 73 20 61 20 63 68 61  ** If X is a cha
34b4d 72 61 63 74 65 72 20 74 68 61 74 20 63 61 6e 20  racter that can 
34b4e 62 65 20 75 73 65 64 20 69 6e 20 61 6e 20 69 64  be used in an id
34b4f 65 6e 74 69 66 69 65 72 20 74 68 65 6e 0a 2a 2a  entifier then.**
34b50 20 66 74 73 49 64 43 68 61 72 28 58 29 20 77 69   ftsIdChar(X) wi
34b51 6c 6c 20 62 65 20 74 72 75 65 2e 20 20 4f 74 68  ll be true.  Oth
34b52 65 72 77 69 73 65 20 69 74 20 69 73 20 66 61 6c  erwise it is fal
34b53 73 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 41 53  se..**.** For AS
34b54 43 49 49 2c 20 61 6e 79 20 63 68 61 72 61 63 74  CII, any charact
34b55 65 72 20 77 69 74 68 20 74 68 65 20 68 69 67 68  er with the high
34b56 2d 6f 72 64 65 72 20 62 69 74 20 73 65 74 20 69  -order bit set i
34b57 73 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 69 6e 20  s.** allowed in 
34b58 61 6e 20 69 64 65 6e 74 69 66 69 65 72 2e 20 20  an identifier.  
34b59 46 6f 72 20 37 2d 62 69 74 20 63 68 61 72 61 63  For 7-bit charac
34b5a 74 65 72 73 2c 20 0a 2a 2a 20 69 73 46 74 73 49  ters, .** isFtsI
34b5b 64 43 68 61 72 5b 58 5d 20 6d 75 73 74 20 62 65  dChar[X] must be
34b5c 20 31 2e 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74   1..**.** Ticket
34b5d 20 23 31 30 36 36 2e 20 20 74 68 65 20 53 51 4c   #1066.  the SQL
34b5e 20 73 74 61 6e 64 61 72 64 20 64 6f 65 73 20 6e   standard does n
34b5f 6f 74 20 61 6c 6c 6f 77 20 27 24 27 20 69 6e 20  ot allow '$' in 
34b60 74 68 65 0a 2a 2a 20 6d 69 64 64 6c 65 20 6f 66  the.** middle of
34b61 20 69 64 65 6e 74 66 69 65 72 73 2e 20 20 42 75   identfiers.  Bu
34b62 74 20 6d 61 6e 79 20 53 51 4c 20 69 6d 70 6c 65  t many SQL imple
34b63 6d 65 6e 74 61 74 69 6f 6e 73 20 64 6f 2e 20 0a  mentations do. .
34b64 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61  ** SQLite will a
34b65 6c 6c 6f 77 20 27 24 27 20 69 6e 20 69 64 65 6e  llow '$' in iden
34b66 74 69 66 69 65 72 73 20 66 6f 72 20 63 6f 6d 70  tifiers for comp
34b67 61 74 69 62 69 6c 69 74 79 2e 0a 2a 2a 20 42 75  atibility..** Bu
34b68 74 20 74 68 65 20 66 65 61 74 75 72 65 20 69 73  t the feature is
34b69 20 75 6e 64 6f 63 75 6d 65 6e 74 65 64 2e 0a 2a   undocumented..*
34b6a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  /.static const c
34b6b 68 61 72 20 69 73 46 74 73 49 64 43 68 61 72 5b  har isFtsIdChar[
34b6c 5d 20 3d 20 7b 0a 2f 2a 20 78 30 20 78 31 20 78  ] = {./* x0 x1 x
34b6d 32 20 78 33 20 78 34 20 78 35 20 78 36 20 78 37  2 x3 x4 x5 x6 x7
34b6e 20 78 38 20 78 39 20 78 41 20 78 42 20 78 43 20   x8 x9 xA xB xC 
34b6f 78 44 20 78 45 20 78 46 20 2a 2f 0a 20 20 20 20  xD xE xF */.    
34b70 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20 30  0, 0, 0, 0, 1, 0
34b71 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
34b72 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
34b73 20 2f 2a 20 32 78 20 2a 2f 0a 20 20 20 20 31 2c   /* 2x */.    1,
34b74 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
34b75 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30  1, 1, 1, 1, 0, 0
34b76 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f  , 0, 0, 0, 0,  /
34b77 2a 20 33 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31  * 3x */.    0, 1
34b78 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
34b79 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
34b7a 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20  1, 1, 1, 1,  /* 
34b7b 34 78 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20  4x */.    1, 1, 
34b7c 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
34b7d 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c  , 1, 1, 1, 0, 0,
34b7e 20 30 2c 20 30 2c 20 31 2c 20 20 2f 2a 20 35 78   0, 0, 1,  /* 5x
34b7f 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c   */.    0, 1, 1,
34b80 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
34b81 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
34b82 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 36 78 20 2a  , 1, 1,  /* 6x *
34b83 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 31  /.    1, 1, 1, 1
34b84 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
34b85 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20   1, 1, 0, 0, 0, 
34b86 30 2c 20 30 2c 20 20 2f 2a 20 37 78 20 2a 2f 0a  0, 0,  /* 7x */.
34b87 7d 3b 0a 23 64 65 66 69 6e 65 20 66 74 73 49 64  };.#define ftsId
34b88 43 68 61 72 28 43 29 20 20 28 28 28 63 3d 43 29  Char(C)  (((c=C)
34b89 26 30 78 38 30 29 21 3d 30 20 7c 7c 20 28 63 3e  &0x80)!=0 || (c>
34b8a 30 78 31 66 20 26 26 20 69 73 46 74 73 49 64 43  0x1f && isFtsIdC
34b8b 68 61 72 5b 63 2d 30 78 32 30 5d 29 29 0a 0a 0a  har[c-0x20]))...
34b8c 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
34b8d 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 74   length of the t
34b8e 6f 6b 65 6e 20 74 68 61 74 20 62 65 67 69 6e 73  oken that begins
34b8f 20 61 74 20 7a 5b 30 5d 2e 20 0a 2a 2a 20 53 74   at z[0]. .** St
34b90 6f 72 65 20 74 68 65 20 74 6f 6b 65 6e 20 74 79  ore the token ty
34b91 70 65 20 69 6e 20 2a 74 6f 6b 65 6e 54 79 70 65  pe in *tokenType
34b92 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e   before returnin
34b93 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  g..*/.static int
34b94 20 66 74 73 47 65 74 54 6f 6b 65 6e 28 63 6f 6e   ftsGetToken(con
34b95 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20  st char *z, int 
34b96 2a 74 6f 6b 65 6e 54 79 70 65 29 7b 0a 20 20 69  *tokenType){.  i
34b97 6e 74 20 69 2c 20 63 3b 0a 20 20 73 77 69 74 63  nt i, c;.  switc
34b98 68 28 20 2a 7a 20 29 7b 0a 20 20 20 20 63 61 73  h( *z ){.    cas
34b99 65 20 30 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f  e 0: {.      *to
34b9a 6b 65 6e 54 79 70 65 20 3d 20 54 4f 4b 45 4e 5f  kenType = TOKEN_
34b9b 45 4f 46 3b 0a 20 20 20 20 20 20 72 65 74 75 72  EOF;.      retur
34b9c 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  n 0;.    }.    c
34b9d 61 73 65 20 27 20 27 3a 20 63 61 73 65 20 27 5c  ase ' ': case '\
34b9e 74 27 3a 20 63 61 73 65 20 27 5c 6e 27 3a 20 63  t': case '\n': c
34b9f 61 73 65 20 27 5c 66 27 3a 20 63 61 73 65 20 27  ase '\f': case '
34ba0 5c 72 27 3a 20 7b 0a 20 20 20 20 20 20 66 6f 72  \r': {.      for
34ba1 28 69 3d 31 3b 20 73 61 66 65 5f 69 73 73 70 61  (i=1; safe_isspa
34ba2 63 65 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d  ce(z[i]); i++){}
34ba3 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70  .      *tokenTyp
34ba4 65 20 3d 20 54 4f 4b 45 4e 5f 53 50 41 43 45 3b  e = TOKEN_SPACE;
34ba5 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b  .      return i;
34ba6 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
34ba7 27 60 27 3a 0a 20 20 20 20 63 61 73 65 20 27 5c  '`':.    case '\
34ba8 27 27 3a 0a 20 20 20 20 63 61 73 65 20 27 22 27  '':.    case '"'
34ba9 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 65  : {.      int de
34baa 6c 69 6d 20 3d 20 7a 5b 30 5d 3b 0a 20 20 20 20  lim = z[0];.    
34bab 20 20 66 6f 72 28 69 3d 31 3b 20 28 63 3d 7a 5b    for(i=1; (c=z[
34bac 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20  i])!=0; i++){.  
34bad 20 20 20 20 20 20 69 66 28 20 63 3d 3d 64 65 6c        if( c==del
34bae 69 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  im ){.          
34baf 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 64 65 6c 69  if( z[i+1]==deli
34bb0 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  m ){.           
34bb1 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20   i++;.          
34bb2 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
34bb3 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
34bb4 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
34bb5 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 74        }.      *t
34bb6 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4f 4b 45 4e  okenType = TOKEN
34bb7 5f 53 54 52 49 4e 47 3b 0a 20 20 20 20 20 20 72  _STRING;.      r
34bb8 65 74 75 72 6e 20 69 20 2b 20 28 63 21 3d 30 29  eturn i + (c!=0)
34bb9 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
34bba 20 27 5b 27 3a 20 7b 0a 20 20 20 20 20 20 66 6f   '[': {.      fo
34bbb 72 28 69 3d 31 2c 20 63 3d 7a 5b 30 5d 3b 20 63  r(i=1, c=z[0]; c
34bbc 21 3d 27 5d 27 20 26 26 20 28 63 3d 7a 5b 69 5d  !=']' && (c=z[i]
34bbd 29 21 3d 30 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20  )!=0; i++){}.   
34bbe 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
34bbf 54 4f 4b 45 4e 5f 49 44 3b 0a 20 20 20 20 20 20  TOKEN_ID;.      
34bc0 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a  return i;.    }.
34bc1 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
34bc2 20 20 20 20 20 69 66 28 20 21 66 74 73 49 64 43       if( !ftsIdC
34bc3 68 61 72 28 2a 7a 29 20 29 7b 0a 20 20 20 20 20  har(*z) ){.     
34bc4 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
34bc5 7d 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b  }.      for(i=1;
34bc6 20 66 74 73 49 64 43 68 61 72 28 7a 5b 69 5d 29   ftsIdChar(z[i])
34bc7 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 2a  ; i++){}.      *
34bc8 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4f 4b 45  tokenType = TOKE
34bc9 4e 5f 49 44 3b 0a 20 20 20 20 20 20 72 65 74 75  N_ID;.      retu
34bca 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  rn i;.    }.  }.
34bcb 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
34bcc 4f 4b 45 4e 5f 50 55 4e 43 54 3b 0a 20 20 72 65  OKEN_PUNCT;.  re
34bcd 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 1;.}../*.**
34bce 20 41 20 74 6f 6b 65 6e 20 65 78 74 72 61 63 74   A token extract
34bcf 65 64 20 66 72 6f 6d 20 61 20 73 74 72 69 6e 67  ed from a string
34bd0 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   is an instance 
34bd1 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
34bd2 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  .** structure..*
34bd3 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
34bd4 20 46 74 73 54 6f 6b 65 6e 20 7b 0a 20 20 63 6f   FtsToken {.  co
34bd5 6e 73 74 20 63 68 61 72 20 2a 7a 3b 20 20 20 20  nst char *z;    
34bd6 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
34bd7 20 74 6f 6b 65 6e 20 74 65 78 74 2e 20 20 4e 6f   token text.  No
34bd8 74 20 27 5c 30 30 30 27 20 74 65 72 6d 69 6e 61  t '\000' termina
34bd9 74 65 64 20 2a 2f 0a 20 20 73 68 6f 72 74 20 69  ted */.  short i
34bda 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a  nt n;         /*
34bdb 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 74   Length of the t
34bdc 6f 6b 65 6e 20 74 65 78 74 20 69 6e 20 62 79 74  oken text in byt
34bdd 65 73 2e 20 2a 2f 0a 7d 20 46 74 73 54 6f 6b 65  es. */.} FtsToke
34bde 6e 3b 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20  n;../*.** Given 
34bdf 61 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 28  a input string (
34be0 77 68 69 63 68 20 69 73 20 72 65 61 6c 6c 79 20  which is really 
34be1 6f 6e 65 20 6f 66 20 74 68 65 20 61 72 67 76 5b  one of the argv[
34be2 5d 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20  ] parameters.** 
34be3 70 61 73 73 65 64 20 69 6e 74 6f 20 78 43 6f 6e  passed into xCon
34be4 6e 65 63 74 20 6f 72 20 78 43 72 65 61 74 65 29  nect or xCreate)
34be5 20 73 70 6c 69 74 20 74 68 65 20 73 74 72 69 6e   split the strin
34be6 67 20 75 70 20 69 6e 74 6f 20 74 6f 6b 65 6e 73  g up into tokens
34be7 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 61  ..** Return an a
34be8 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 65 72 73  rray of pointers
34be9 20 74 6f 20 27 5c 30 30 30 27 20 74 65 72 6d 69   to '\000' termi
34bea 6e 61 74 65 64 20 73 74 72 69 6e 67 73 2c 20 6f  nated strings, o
34beb 6e 65 20 73 74 72 69 6e 67 0a 2a 2a 20 66 6f 72  ne string.** for
34bec 20 65 61 63 68 20 6e 6f 6e 2d 77 68 69 74 65 73   each non-whites
34bed 70 61 63 65 20 74 6f 6b 65 6e 2e 0a 2a 2a 0a 2a  pace token..**.*
34bee 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 61  * The returned a
34bef 72 72 61 79 20 69 73 20 74 65 72 6d 69 6e 61 74  rray is terminat
34bf0 65 64 20 62 79 20 61 20 73 69 6e 67 6c 65 20 4e  ed by a single N
34bf1 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a  ULL pointer..**.
34bf2 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64  ** Space to hold
34bf3 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 61 72   the returned ar
34bf4 72 61 79 20 69 73 20 6f 62 74 61 69 6e 65 64 20  ray is obtained 
34bf5 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65 0a 2a 2a  from a single.**
34bf6 20 6d 61 6c 6c 6f 63 20 61 6e 64 20 73 68 6f 75   malloc and shou
34bf7 6c 64 20 62 65 20 66 72 65 65 64 20 62 79 20 70  ld be freed by p
34bf8 61 73 73 69 6e 67 20 74 68 65 20 72 65 74 75 72  assing the retur
34bf9 6e 20 76 61 6c 75 65 20 74 6f 20 66 72 65 65 28  n value to free(
34bfa 29 2e 0a 2a 2a 20 54 68 65 20 69 6e 64 69 76 69  )..** The indivi
34bfb 64 75 61 6c 20 73 74 72 69 6e 67 73 20 77 69 74  dual strings wit
34bfc 68 69 6e 20 74 68 65 20 74 6f 6b 65 6e 20 6c 69  hin the token li
34bfd 73 74 20 61 72 65 20 61 6c 6c 20 61 20 70 61 72  st are all a par
34bfe 74 20 6f 66 0a 2a 2a 20 74 68 65 20 73 69 6e 67  t of.** the sing
34bff 6c 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  le memory alloca
34c00 74 69 6f 6e 20 61 6e 64 20 77 69 6c 6c 20 61 6c  tion and will al
34c01 6c 20 62 65 20 66 72 65 65 64 20 61 74 20 6f 6e  l be freed at on
34c02 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68  ce..*/.static ch
34c03 61 72 20 2a 2a 74 6f 6b 65 6e 69 7a 65 53 74 72  ar **tokenizeStr
34c04 69 6e 67 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ing(const char *
34c05 7a 2c 20 69 6e 74 20 2a 70 6e 54 6f 6b 65 6e 29  z, int *pnToken)
34c06 7b 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 20 3d  {.  int nToken =
34c07 20 30 3b 0a 20 20 46 74 73 54 6f 6b 65 6e 20 2a   0;.  FtsToken *
34c08 61 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65 33  aToken = sqlite3
34c09 5f 6d 61 6c 6c 6f 63 28 20 73 74 72 6c 65 6e 28  _malloc( strlen(
34c0a 7a 29 20 2a 20 73 69 7a 65 6f 66 28 61 54 6f 6b  z) * sizeof(aTok
34c0b 65 6e 5b 30 5d 29 20 29 3b 0a 20 20 69 6e 74 20  en[0]) );.  int 
34c0c 6e 20 3d 20 31 3b 0a 20 20 69 6e 74 20 65 2c 20  n = 1;.  int e, 
34c0d 69 3b 0a 20 20 69 6e 74 20 74 6f 74 61 6c 53 69  i;.  int totalSi
34c0e 7a 65 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a  ze = 0;.  char *
34c0f 2a 61 7a 54 6f 6b 65 6e 3b 0a 20 20 63 68 61 72  *azToken;.  char
34c10 20 2a 7a 43 6f 70 79 3b 0a 20 20 77 68 69 6c 65   *zCopy;.  while
34c11 28 20 6e 3e 30 20 29 7b 0a 20 20 20 20 6e 20 3d  ( n>0 ){.    n =
34c12 20 66 74 73 47 65 74 54 6f 6b 65 6e 28 7a 2c 20   ftsGetToken(z, 
34c13 26 65 29 3b 0a 20 20 20 20 69 66 28 20 65 21 3d  &e);.    if( e!=
34c14 54 4f 4b 45 4e 5f 53 50 41 43 45 20 29 7b 0a 20  TOKEN_SPACE ){. 
34c15 20 20 20 20 20 61 54 6f 6b 65 6e 5b 6e 54 6f 6b       aToken[nTok
34c16 65 6e 5d 2e 7a 20 3d 20 7a 3b 0a 20 20 20 20 20  en].z = z;.     
34c17 20 61 54 6f 6b 65 6e 5b 6e 54 6f 6b 65 6e 5d 2e   aToken[nToken].
34c18 6e 20 3d 20 6e 3b 0a 20 20 20 20 20 20 6e 54 6f  n = n;.      nTo
34c19 6b 65 6e 2b 2b 3b 0a 20 20 20 20 20 20 74 6f 74  ken++;.      tot
34c1a 61 6c 53 69 7a 65 20 2b 3d 20 6e 2b 31 3b 0a 20  alSize += n+1;. 
34c1b 20 20 20 7d 0a 20 20 20 20 7a 20 2b 3d 20 6e 3b     }.    z += n;
34c1c 0a 20 20 7d 0a 20 20 61 7a 54 6f 6b 65 6e 20 3d  .  }.  azToken =
34c1d 20 28 63 68 61 72 2a 2a 29 73 71 6c 69 74 65 33   (char**)sqlite3
34c1e 5f 6d 61 6c 6c 6f 63 28 20 6e 54 6f 6b 65 6e 2a  _malloc( nToken*
34c1f 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 20 2b 20  sizeof(char*) + 
34c20 74 6f 74 61 6c 53 69 7a 65 20 29 3b 0a 20 20 7a  totalSize );.  z
34c21 43 6f 70 79 20 3d 20 28 63 68 61 72 2a 29 26 61  Copy = (char*)&a
34c22 7a 54 6f 6b 65 6e 5b 6e 54 6f 6b 65 6e 5d 3b 0a  zToken[nToken];.
34c23 20 20 6e 54 6f 6b 65 6e 2d 2d 3b 0a 20 20 66 6f    nToken--;.  fo
34c24 72 28 69 3d 30 3b 20 69 3c 6e 54 6f 6b 65 6e 3b  r(i=0; i<nToken;
34c25 20 69 2b 2b 29 7b 0a 20 20 20 20 61 7a 54 6f 6b   i++){.    azTok
34c26 65 6e 5b 69 5d 20 3d 20 7a 43 6f 70 79 3b 0a 20  en[i] = zCopy;. 
34c27 20 20 20 6e 20 3d 20 61 54 6f 6b 65 6e 5b 69 5d     n = aToken[i]
34c28 2e 6e 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a  .n;.    memcpy(z
34c29 43 6f 70 79 2c 20 61 54 6f 6b 65 6e 5b 69 5d 2e  Copy, aToken[i].
34c2a 7a 2c 20 6e 29 3b 0a 20 20 20 20 7a 43 6f 70 79  z, n);.    zCopy
34c2b 5b 6e 5d 20 3d 20 30 3b 0a 20 20 20 20 7a 43 6f  [n] = 0;.    zCo
34c2c 70 79 20 2b 3d 20 6e 2b 31 3b 0a 20 20 7d 0a 20  py += n+1;.  }. 
34c2d 20 61 7a 54 6f 6b 65 6e 5b 6e 54 6f 6b 65 6e 5d   azToken[nToken]
34c2e 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f   = 0;.  sqlite3_
34c2f 66 72 65 65 28 61 54 6f 6b 65 6e 29 3b 0a 20 20  free(aToken);.  
34c30 2a 70 6e 54 6f 6b 65 6e 20 3d 20 6e 54 6f 6b 65  *pnToken = nToke
34c31 6e 3b 0a 20 20 72 65 74 75 72 6e 20 61 7a 54 6f  n;.  return azTo
34c32 6b 65 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  ken;.}../*.** Co
34c33 6e 76 65 72 74 20 61 6e 20 53 51 4c 2d 73 74 79  nvert an SQL-sty
34c34 6c 65 20 71 75 6f 74 65 64 20 73 74 72 69 6e 67  le quoted string
34c35 20 69 6e 74 6f 20 61 20 6e 6f 72 6d 61 6c 20 73   into a normal s
34c36 74 72 69 6e 67 20 62 79 20 72 65 6d 6f 76 69 6e  tring by removin
34c37 67 0a 2a 2a 20 74 68 65 20 71 75 6f 74 65 20 63  g.** the quote c
34c38 68 61 72 61 63 74 65 72 73 2e 20 20 54 68 65 20  haracters.  The 
34c39 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 64 6f  conversion is do
34c3a 6e 65 20 69 6e 2d 70 6c 61 63 65 2e 20 20 49 66  ne in-place.  If
34c3b 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20 64 6f   the.** input do
34c3c 65 73 20 6e 6f 74 20 62 65 67 69 6e 20 77 69 74  es not begin wit
34c3d 68 20 61 20 71 75 6f 74 65 20 63 68 61 72 61 63  h a quote charac
34c3e 74 65 72 2c 20 74 68 65 6e 20 74 68 69 73 20 72  ter, then this r
34c3f 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 61 20 6e  outine.** is a n
34c40 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d  o-op..**.** Exam
34c41 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ples:.**.**     
34c42 22 61 62 63 22 20 20 20 62 65 63 6f 6d 65 73 20  "abc"   becomes 
34c43 20 20 61 62 63 0a 2a 2a 20 20 20 20 20 27 78 79    abc.**     'xy
34c44 7a 27 20 20 20 62 65 63 6f 6d 65 73 20 20 20 78  z'   becomes   x
34c45 79 7a 0a 2a 2a 20 20 20 20 20 5b 70 71 72 5d 20  yz.**     [pqr] 
34c46 20 20 62 65 63 6f 6d 65 73 20 20 20 70 71 72 0a    becomes   pqr.
34c47 2a 2a 20 20 20 20 20 60 6d 6e 6f 60 20 20 20 62  **     `mno`   b
34c48 65 63 6f 6d 65 73 20 20 20 6d 6e 6f 0a 2a 2f 0a  ecomes   mno.*/.
34c49 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 71 75  static void dequ
34c4a 6f 74 65 53 74 72 69 6e 67 28 63 68 61 72 20 2a  oteString(char *
34c4b 7a 29 7b 0a 20 20 69 6e 74 20 71 75 6f 74 65 3b  z){.  int quote;
34c4c 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 69  .  int i, j;.  i
34c4d 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( z==0 ) return
34c4e 3b 0a 20 20 71 75 6f 74 65 20 3d 20 7a 5b 30 5d  ;.  quote = z[0]
34c4f 3b 0a 20 20 73 77 69 74 63 68 28 20 71 75 6f 74  ;.  switch( quot
34c50 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 27 5c  e ){.    case '\
34c51 27 27 3a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20  '':  break;.    
34c52 63 61 73 65 20 27 22 27 3a 20 20 20 62 72 65 61  case '"':   brea
34c53 6b 3b 0a 20 20 20 20 63 61 73 65 20 27 60 27 3a  k;.    case '`':
34c54 20 20 20 62 72 65 61 6b 3b 20 20 20 20 20 20 20     break;       
34c55 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20           /* For 
34c56 4d 79 53 51 4c 20 63 6f 6d 70 61 74 69 62 69 6c  MySQL compatibil
34c57 69 74 79 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  ity */.    case 
34c58 27 5b 27 3a 20 20 20 71 75 6f 74 65 20 3d 20 27  '[':   quote = '
34c59 5d 27 3b 20 20 62 72 65 61 6b 3b 20 20 2f 2a 20  ]';  break;  /* 
34c5a 46 6f 72 20 4d 53 20 53 71 6c 53 65 72 76 65 72  For MS SqlServer
34c5b 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 2a   compatibility *
34c5c 2f 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20  /.    default:  
34c5d 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
34c5e 66 6f 72 28 69 3d 31 2c 20 6a 3d 30 3b 20 7a 5b  for(i=1, j=0; z[
34c5f 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  i]; i++){.    if
34c60 28 20 7a 5b 69 5d 3d 3d 71 75 6f 74 65 20 29 7b  ( z[i]==quote ){
34c61 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b 31  .      if( z[i+1
34c62 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a 20 20 20 20  ]==quote ){.    
34c63 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 71 75 6f      z[j++] = quo
34c64 74 65 3b 0a 20 20 20 20 20 20 20 20 69 2b 2b 3b  te;.        i++;
34c65 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
34c66 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 30        z[j++] = 0
34c67 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
34c68 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
34c69 73 65 7b 0a 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d  se{.      z[j++]
34c6a 20 3d 20 7a 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20   = z[i];.    }. 
34c6b 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20   }.}../*.** The 
34c6c 69 6e 70 75 74 20 61 7a 49 6e 20 69 73 20 61 20  input azIn is a 
34c6d 4e 55 4c 4c 2d 74 65 72 6d 69 6e 61 74 65 64 20  NULL-terminated 
34c6e 6c 69 73 74 20 6f 66 20 74 6f 6b 65 6e 73 2e 20  list of tokens. 
34c6f 20 52 65 6d 6f 76 65 20 74 68 65 20 66 69 72 73   Remove the firs
34c70 74 0a 2a 2a 20 74 6f 6b 65 6e 20 61 6e 64 20 61  t.** token and a
34c71 6c 6c 20 70 75 6e 63 74 75 61 74 69 6f 6e 20 74  ll punctuation t
34c72 6f 6b 65 6e 73 2e 20 20 52 65 6d 6f 76 65 20 74  okens.  Remove t
34c73 68 65 20 71 75 6f 74 65 73 20 66 72 6f 6d 0a 2a  he quotes from.*
34c74 2a 20 61 72 6f 75 6e 64 20 73 74 72 69 6e 67 20  * around string 
34c75 6c 69 74 65 72 61 6c 20 74 6f 6b 65 6e 73 2e 0a  literal tokens..
34c76 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a 2a  **.** Example:.*
34c77 2a 0a 2a 2a 20 20 20 20 20 69 6e 70 75 74 3a 20  *.**     input: 
34c78 20 20 20 20 20 74 6f 6b 65 6e 69 7a 65 20 63 68       tokenize ch
34c79 69 6e 65 73 65 20 28 20 27 73 69 6d 70 6c 69 66  inese ( 'simplif
34c7a 65 64 27 20 2c 20 27 6d 69 78 65 64 27 20 29 0a  ed' , 'mixed' ).
34c7b 2a 2a 20 20 20 20 20 6f 75 74 70 75 74 3a 20 20  **     output:  
34c7c 20 20 20 63 68 69 6e 65 73 65 20 73 69 6d 70 6c     chinese simpl
34c7d 69 66 65 64 20 6d 69 78 65 64 0a 2a 2a 0a 2a 2a  ifed mixed.**.**
34c7e 20 41 6e 6f 74 68 65 72 20 65 78 61 6d 70 6c 65   Another example
34c7f 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 69 6e 70 75  :.**.**     inpu
34c80 74 3a 20 20 20 20 20 20 64 65 6c 69 6d 69 74 65  t:      delimite
34c81 72 73 20 28 20 27 5b 27 20 2c 20 27 5d 27 20 2c  rs ( '[' , ']' ,
34c82 20 27 2e 2e 2e 27 20 29 0a 2a 2a 20 20 20 20 20   '...' ).**     
34c83 6f 75 74 70 75 74 3a 20 20 20 20 20 5b 20 5d 20  output:     [ ] 
34c84 2e 2e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ....*/.static vo
34c85 69 64 20 74 6f 6b 65 6e 4c 69 73 74 54 6f 49 64  id tokenListToId
34c86 4c 69 73 74 28 63 68 61 72 20 2a 2a 61 7a 49 6e  List(char **azIn
34c87 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20  ){.  int i, j;. 
34c88 20 69 66 28 20 61 7a 49 6e 20 29 7b 0a 20 20 20   if( azIn ){.   
34c89 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 2d 31 3b 20   for(i=0, j=-1; 
34c8a 61 7a 49 6e 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20  azIn[i]; i++){. 
34c8b 20 20 20 20 20 69 66 28 20 73 61 66 65 5f 69 73       if( safe_is
34c8c 61 6c 6e 75 6d 28 61 7a 49 6e 5b 69 5d 5b 30 5d  alnum(azIn[i][0]
34c8d 29 20 7c 7c 20 61 7a 49 6e 5b 69 5d 5b 31 5d 20  ) || azIn[i][1] 
34c8e 29 7b 0a 20 20 20 20 20 20 20 20 64 65 71 75 6f  ){.        dequo
34c8f 74 65 53 74 72 69 6e 67 28 61 7a 49 6e 5b 69 5d  teString(azIn[i]
34c90 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6a  );.        if( j
34c91 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  >=0 ){.         
34c92 20 61 7a 49 6e 5b 6a 5d 20 3d 20 61 7a 49 6e 5b   azIn[j] = azIn[
34c93 69 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  i];.        }.  
34c94 20 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 20        j++;.     
34c95 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 7a 49   }.    }.    azI
34c96 6e 5b 6a 5d 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a  n[j] = 0;.  }.}.
34c97 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65  ../*.** Find the
34c98 20 66 69 72 73 74 20 61 6c 70 68 61 6e 75 6d 65   first alphanume
34c99 72 69 63 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65  ric token in the
34c9a 20 73 74 72 69 6e 67 20 7a 49 6e 2e 20 20 4e 75   string zIn.  Nu
34c9b 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 0a 2a 2a 20  ll-terminate.** 
34c9c 74 68 69 73 20 74 6f 6b 65 6e 2e 20 20 52 65 6d  this token.  Rem
34c9d 6f 76 65 20 61 6e 79 20 71 75 6f 74 61 74 69 6f  ove any quotatio
34c9e 6e 20 6d 61 72 6b 73 2e 20 20 41 6e 64 20 72 65  n marks.  And re
34c9f 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
34ca0 6f 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 2e  o.** the result.
34ca1 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20  .*/.static char 
34ca2 2a 66 69 72 73 74 54 6f 6b 65 6e 28 63 68 61 72  *firstToken(char
34ca3 20 2a 7a 49 6e 2c 20 63 68 61 72 20 2a 2a 70 7a   *zIn, char **pz
34ca4 54 61 69 6c 29 7b 0a 20 20 69 6e 74 20 6e 2c 20  Tail){.  int n, 
34ca5 74 74 79 70 65 3b 0a 20 20 77 68 69 6c 65 28 31  ttype;.  while(1
34ca6 29 7b 0a 20 20 20 20 6e 20 3d 20 66 74 73 47 65  ){.    n = ftsGe
34ca7 74 54 6f 6b 65 6e 28 7a 49 6e 2c 20 26 74 74 79  tToken(zIn, &tty
34ca8 70 65 29 3b 0a 20 20 20 20 69 66 28 20 74 74 79  pe);.    if( tty
34ca9 70 65 3d 3d 54 4f 4b 45 4e 5f 53 50 41 43 45 20  pe==TOKEN_SPACE 
34caa 29 7b 0a 20 20 20 20 20 20 7a 49 6e 20 2b 3d 20  ){.      zIn += 
34cab 6e 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  n;.    }else if(
34cac 20 74 74 79 70 65 3d 3d 54 4f 4b 45 4e 5f 45 4f   ttype==TOKEN_EO
34cad 46 20 29 7b 0a 20 20 20 20 20 20 2a 70 7a 54 61  F ){.      *pzTa
34cae 69 6c 20 3d 20 7a 49 6e 3b 0a 20 20 20 20 20 20  il = zIn;.      
34caf 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 65  return 0;.    }e
34cb0 6c 73 65 7b 0a 20 20 20 20 20 20 7a 49 6e 5b 6e  lse{.      zIn[n
34cb1 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 2a 70 7a  ] = 0;.      *pz
34cb2 54 61 69 6c 20 3d 20 26 7a 49 6e 5b 31 5d 3b 0a  Tail = &zIn[1];.
34cb3 20 20 20 20 20 20 64 65 71 75 6f 74 65 53 74 72        dequoteStr
34cb4 69 6e 67 28 7a 49 6e 29 3b 0a 20 20 20 20 20 20  ing(zIn);.      
34cb5 72 65 74 75 72 6e 20 7a 49 6e 3b 0a 20 20 20 20  return zIn;.    
34cb6 7d 0a 20 20 7d 0a 20 20 2f 2a 4e 4f 54 52 45 41  }.  }.  /*NOTREA
34cb7 43 48 45 44 2a 2f 0a 7d 0a 0a 2f 2a 20 52 65 74  CHED*/.}../* Ret
34cb8 75 72 6e 20 74 72 75 65 20 69 66 2e 2e 2e 0a 2a  urn true if....*
34cb9 2a 0a 2a 2a 20 20 20 2a 20 20 73 20 62 65 67 69  *.**   *  s begi
34cba 6e 73 20 77 69 74 68 20 74 68 65 20 73 74 72 69  ns with the stri
34cbb 6e 67 20 74 2c 20 69 67 6e 6f 72 69 6e 67 20 63  ng t, ignoring c
34cbc 61 73 65 0a 2a 2a 20 20 20 2a 20 20 73 20 69 73  ase.**   *  s is
34cbd 20 6c 6f 6e 67 65 72 20 74 68 61 6e 20 74 0a 2a   longer than t.*
34cbe 2a 20 20 20 2a 20 20 54 68 65 20 66 69 72 73 74  *   *  The first
34cbf 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 73 20   character of s 
34cc0 62 65 79 6f 6e 64 20 74 20 69 73 20 6e 6f 74 20  beyond t is not 
34cc1 61 20 61 6c 70 68 61 6e 75 6d 65 72 69 63 0a 2a  a alphanumeric.*
34cc2 2a 20 0a 2a 2a 20 49 67 6e 6f 72 65 20 6c 65 61  * .** Ignore lea
34cc3 64 69 6e 67 20 73 70 61 63 65 20 69 6e 20 2a 73  ding space in *s
34cc4 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 70 75 74 20 69  ..**.** To put i
34cc5 74 20 61 6e 6f 74 68 65 72 20 77 61 79 2c 20 72  t another way, r
34cc6 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68  eturn true if th
34cc7 65 20 66 69 72 73 74 20 74 6f 6b 65 6e 20 6f 66  e first token of
34cc8 0a 2a 2a 20 73 5b 5d 20 69 73 20 74 5b 5d 2e 0a  .** s[] is t[]..
34cc9 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 74  */.static int st
34cca 61 72 74 73 57 69 74 68 28 63 6f 6e 73 74 20 63  artsWith(const c
34ccb 68 61 72 20 2a 73 2c 20 63 6f 6e 73 74 20 63 68  har *s, const ch
34ccc 61 72 20 2a 74 29 7b 0a 20 20 77 68 69 6c 65 28  ar *t){.  while(
34ccd 20 73 61 66 65 5f 69 73 73 70 61 63 65 28 2a 73   safe_isspace(*s
34cce 29 20 29 7b 20 73 2b 2b 3b 20 7d 0a 20 20 77 68  ) ){ s++; }.  wh
34ccf 69 6c 65 28 20 2a 74 20 29 7b 0a 20 20 20 20 69  ile( *t ){.    i
34cd0 66 28 20 73 61 66 65 5f 74 6f 6c 6f 77 65 72 28  f( safe_tolower(
34cd1 2a 73 2b 2b 29 21 3d 73 61 66 65 5f 74 6f 6c 6f  *s++)!=safe_tolo
34cd2 77 65 72 28 2a 74 2b 2b 29 20 29 20 72 65 74 75  wer(*t++) ) retu
34cd3 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rn 0;.  }.  retu
34cd4 72 6e 20 2a 73 21 3d 27 5f 27 20 26 26 20 21 73  rn *s!='_' && !s
34cd5 61 66 65 5f 69 73 61 6c 6e 75 6d 28 2a 73 29 3b  afe_isalnum(*s);
34cd6 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  .}../*.** An ins
34cd7 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74  tance of this st
34cd8 72 75 63 74 75 72 65 20 64 65 66 69 6e 65 73 20  ructure defines 
34cd9 74 68 65 20 22 73 70 65 63 22 20 6f 66 20 61 0a  the "spec" of a.
34cda 2a 2a 20 66 75 6c 6c 20 74 65 78 74 20 69 6e 64  ** full text ind
34cdb 65 78 2e 20 20 54 68 69 73 20 73 74 72 75 63 74  ex.  This struct
34cdc 75 72 65 20 69 73 20 70 6f 70 75 6c 61 74 65 64  ure is populated
34cdd 20 62 79 20 70 61 72 73 65 53 70 65 63 0a 2a 2a   by parseSpec.**
34cde 20 61 6e 64 20 75 73 65 20 62 79 20 66 75 6c 6c   and use by full
34cdf 74 65 78 74 43 6f 6e 6e 65 63 74 20 61 6e 64 20  textConnect and 
34ce0 66 75 6c 6c 74 65 78 74 43 72 65 61 74 65 2e 0a  fulltextCreate..
34ce1 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
34ce2 74 20 54 61 62 6c 65 53 70 65 63 20 7b 0a 20 20  t TableSpec {.  
34ce3 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b  const char *zDb;
34ce4 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 67 69           /* Logi
34ce5 63 61 6c 20 64 61 74 61 62 61 73 65 20 6e 61 6d  cal database nam
34ce6 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  e */.  const cha
34ce7 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20  r *zName;       
34ce8 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66  /* Name of the f
34ce9 75 6c 6c 2d 74 65 78 74 20 69 6e 64 65 78 20 2a  ull-text index *
34cea 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b  /.  int nColumn;
34ceb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
34cec 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  Number of column
34ced 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20  s to be indexed 
34cee 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f  */.  char **azCo
34cef 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a  lumn;         /*
34cf0 20 4f 72 69 67 69 6e 61 6c 20 6e 61 6d 65 73 20   Original names 
34cf1 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 62 65  of columns to be
34cf2 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20 20 63 68   indexed */.  ch
34cf3 61 72 20 2a 2a 61 7a 43 6f 6e 74 65 6e 74 43 6f  ar **azContentCo
34cf4 6c 75 6d 6e 3b 20 20 2f 2a 20 43 6f 6c 75 6d 6e  lumn;  /* Column
34cf5 20 6e 61 6d 65 73 20 66 6f 72 20 25 5f 63 6f 6e   names for %_con
34cf6 74 65 6e 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a  tent */.  char *
34cf7 2a 61 7a 54 6f 6b 65 6e 69 7a 65 72 3b 20 20 20  *azTokenizer;   
34cf8 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 6f     /* Name of to
34cf9 6b 65 6e 69 7a 65 72 20 61 6e 64 20 69 74 73 20  kenizer and its 
34cfa 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 7d 20 54  arguments */.} T
34cfb 61 62 6c 65 53 70 65 63 3b 0a 0a 2f 2a 0a 2a 2a  ableSpec;../*.**
34cfc 20 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6f 66 20   Reclaim all of 
34cfd 74 68 65 20 6d 65 6d 6f 72 79 20 75 73 65 64 20  the memory used 
34cfe 62 79 20 61 20 54 61 62 6c 65 53 70 65 63 0a 2a  by a TableSpec.*
34cff 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c  /.static void cl
34d00 65 61 72 54 61 62 6c 65 53 70 65 63 28 54 61 62  earTableSpec(Tab
34d01 6c 65 53 70 65 63 20 2a 70 29 20 7b 0a 20 20 73  leSpec *p) {.  s
34d02 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 61  qlite3_free(p->a
34d03 7a 43 6f 6c 75 6d 6e 29 3b 0a 20 20 73 71 6c 69  zColumn);.  sqli
34d04 74 65 33 5f 66 72 65 65 28 70 2d 3e 61 7a 43 6f  te3_free(p->azCo
34d05 6e 74 65 6e 74 43 6f 6c 75 6d 6e 29 3b 0a 20 20  ntentColumn);.  
34d06 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e  sqlite3_free(p->
34d07 61 7a 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 7d 0a  azTokenizer);.}.
34d08 0a 2f 2a 20 50 61 72 73 65 20 61 20 43 52 45 41  ./* Parse a CREA
34d09 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45  TE VIRTUAL TABLE
34d0a 20 73 74 61 74 65 6d 65 6e 74 2c 20 77 68 69 63   statement, whic
34d0b 68 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69  h looks like thi
34d0c 73 3a 0a 20 2a 0a 20 2a 20 43 52 45 41 54 45 20  s:. *. * CREATE 
34d0d 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 65 6d  VIRTUAL TABLE em
34d0e 61 69 6c 0a 20 2a 20 20 20 20 20 20 20 20 55 53  ail. *        US
34d0f 49 4e 47 20 66 74 73 33 28 73 75 62 6a 65 63 74  ING fts3(subject
34d10 2c 20 62 6f 64 79 2c 20 74 6f 6b 65 6e 69 7a 65  , body, tokenize
34d11 20 6d 79 74 6f 6b 65 6e 69 7a 65 72 28 6d 79 61   mytokenizer(mya
34d12 72 67 29 29 0a 20 2a 0a 20 2a 20 57 65 20 72 65  rg)). *. * We re
34d13 74 75 72 6e 20 70 61 72 73 65 64 20 69 6e 66 6f  turn parsed info
34d14 72 6d 61 74 69 6f 6e 20 69 6e 20 61 20 54 61 62  rmation in a Tab
34d15 6c 65 53 70 65 63 20 73 74 72 75 63 74 75 72 65  leSpec structure
34d16 2e 0a 20 2a 20 0a 20 2a 2f 0a 73 74 61 74 69 63  .. * . */.static
34d17 20 69 6e 74 20 70 61 72 73 65 53 70 65 63 28 54   int parseSpec(T
34d18 61 62 6c 65 53 70 65 63 20 2a 70 53 70 65 63 2c  ableSpec *pSpec,
34d19 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74   int argc, const
34d1a 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67   char *const*arg
34d1b 76 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  v,.             
34d1c 20 20 20 20 20 20 20 20 63 68 61 72 2a 2a 70 7a          char**pz
34d1d 45 72 72 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6e  Err){.  int i, n
34d1e 3b 0a 20 20 63 68 61 72 20 2a 7a 2c 20 2a 7a 44  ;.  char *z, *zD
34d1f 75 6d 6d 79 3b 0a 20 20 63 68 61 72 20 2a 2a 61  ummy;.  char **a
34d20 7a 41 72 67 3b 0a 20 20 63 6f 6e 73 74 20 63 68  zArg;.  const ch
34d21 61 72 20 2a 7a 54 6f 6b 65 6e 69 7a 65 72 20 3d  ar *zTokenizer =
34d22 20 30 3b 20 20 20 20 2f 2a 20 61 72 67 76 5b 5d   0;    /* argv[]
34d23 20 65 6e 74 72 79 20 64 65 73 63 72 69 62 69 6e   entry describin
34d24 67 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20  g the tokenizer 
34d25 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72  */..  assert( ar
34d26 67 63 3e 3d 33 20 29 3b 0a 20 20 2f 2a 20 43 75  gc>=3 );.  /* Cu
34d27 72 72 65 6e 74 20 69 6e 74 65 72 66 61 63 65 3a  rrent interface:
34d28 0a 20 20 2a 2a 20 61 72 67 76 5b 30 5d 20 2d 20  .  ** argv[0] - 
34d29 6d 6f 64 75 6c 65 20 6e 61 6d 65 0a 20 20 2a 2a  module name.  **
34d2a 20 61 72 67 76 5b 31 5d 20 2d 20 64 61 74 61 62   argv[1] - datab
34d2b 61 73 65 20 6e 61 6d 65 0a 20 20 2a 2a 20 61 72  ase name.  ** ar
34d2c 67 76 5b 32 5d 20 2d 20 74 61 62 6c 65 20 6e 61  gv[2] - table na
34d2d 6d 65 0a 20 20 2a 2a 20 61 72 67 76 5b 33 2e 2e  me.  ** argv[3..
34d2e 5d 20 2d 20 63 6f 6c 75 6d 6e 73 2c 20 6f 70 74  ] - columns, opt
34d2f 69 6f 6e 61 6c 6c 79 20 66 6f 6c 6c 6f 77 65 64  ionally followed
34d30 20 62 79 20 74 6f 6b 65 6e 69 7a 65 72 20 73 70   by tokenizer sp
34d31 65 63 69 66 69 63 61 74 69 6f 6e 0a 20 20 2a 2a  ecification.  **
34d32 20 20 20 20 20 20 20 20 20 20 20 20 20 61 6e 64               and
34d33 20 73 6e 69 70 70 65 74 20 64 65 6c 69 6d 69 74   snippet delimit
34d34 65 72 73 20 73 70 65 63 69 66 69 63 61 74 69 6f  ers specificatio
34d35 6e 2e 0a 20 20 2a 2f 0a 0a 20 20 2f 2a 20 4d 61  n..  */..  /* Ma
34d36 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  ke a copy of the
34d37 20 63 6f 6d 70 6c 65 74 65 20 61 72 67 76 5b 5d   complete argv[]
34d38 5b 5d 20 61 72 72 61 79 20 69 6e 20 61 20 73 69  [] array in a si
34d39 6e 67 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e  ngle allocation.
34d3a 0a 20 20 2a 2a 20 54 68 65 20 61 72 67 76 5b 5d  .  ** The argv[]
34d3b 5b 5d 20 61 72 72 61 79 20 69 73 20 72 65 61 64  [] array is read
34d3c 2d 6f 6e 6c 79 20 61 6e 64 20 74 72 61 6e 73 69  -only and transi
34d3d 65 6e 74 2e 20 20 57 65 20 63 61 6e 20 77 72 69  ent.  We can wri
34d3e 74 65 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20 63  te to the.  ** c
34d3f 6f 70 79 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  opy in order to 
34d40 6d 6f 64 69 66 79 20 74 68 69 6e 67 73 20 61 6e  modify things an
34d41 64 20 74 68 65 20 63 6f 70 79 20 69 73 20 70 65  d the copy is pe
34d42 72 73 69 73 74 65 6e 74 2e 0a 20 20 2a 2f 0a 20  rsistent..  */. 
34d43 20 43 4c 45 41 52 28 70 53 70 65 63 29 3b 0a 20   CLEAR(pSpec);. 
34d44 20 66 6f 72 28 69 3d 6e 3d 30 3b 20 69 3c 61 72   for(i=n=0; i<ar
34d45 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6e 20  gc; i++){.    n 
34d46 2b 3d 20 73 74 72 6c 65 6e 28 61 72 67 76 5b 69  += strlen(argv[i
34d47 5d 29 20 2b 20 31 3b 0a 20 20 7d 0a 20 20 61 7a  ]) + 1;.  }.  az
34d48 41 72 67 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  Arg = sqlite3_ma
34d49 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 63 68 61  lloc( sizeof(cha
34d4a 72 2a 29 2a 61 72 67 63 20 2b 20 6e 20 29 3b 0a  r*)*argc + n );.
34d4b 20 20 69 66 28 20 61 7a 41 72 67 3d 3d 30 20 29    if( azArg==0 )
34d4c 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
34d4d 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20  ITE_NOMEM;.  }. 
34d4e 20 7a 20 3d 20 28 63 68 61 72 2a 29 26 61 7a 41   z = (char*)&azA
34d4f 72 67 5b 61 72 67 63 5d 3b 0a 20 20 66 6f 72 28  rg[argc];.  for(
34d50 69 3d 30 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b  i=0; i<argc; i++
34d51 29 7b 0a 20 20 20 20 61 7a 41 72 67 5b 69 5d 20  ){.    azArg[i] 
34d52 3d 20 7a 3b 0a 20 20 20 20 73 74 72 63 70 79 28  = z;.    strcpy(
34d53 7a 2c 20 61 72 67 76 5b 69 5d 29 3b 0a 20 20 20  z, argv[i]);.   
34d54 20 7a 20 2b 3d 20 73 74 72 6c 65 6e 28 7a 29 2b   z += strlen(z)+
34d55 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 64 65  1;.  }..  /* Ide
34d56 6e 74 69 66 79 20 74 68 65 20 63 6f 6c 75 6d 6e  ntify the column
34d57 20 6e 61 6d 65 73 20 61 6e 64 20 74 68 65 20 74   names and the t
34d58 6f 6b 65 6e 69 7a 65 72 20 61 6e 64 20 64 65 6c  okenizer and del
34d59 69 6d 69 74 65 72 20 61 72 67 75 6d 65 6e 74 73  imiter arguments
34d5a 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 61 72 67  .  ** in the arg
34d5b 76 5b 5d 5b 5d 20 61 72 72 61 79 2e 0a 20 20 2a  v[][] array..  *
34d5c 2f 0a 20 20 70 53 70 65 63 2d 3e 7a 44 62 20 3d  /.  pSpec->zDb =
34d5d 20 61 7a 41 72 67 5b 31 5d 3b 0a 20 20 70 53 70   azArg[1];.  pSp
34d5e 65 63 2d 3e 7a 4e 61 6d 65 20 3d 20 61 7a 41 72  ec->zName = azAr
34d5f 67 5b 32 5d 3b 0a 20 20 70 53 70 65 63 2d 3e 6e  g[2];.  pSpec->n
34d60 43 6f 6c 75 6d 6e 20 3d 20 30 3b 0a 20 20 70 53  Column = 0;.  pS
34d61 70 65 63 2d 3e 61 7a 43 6f 6c 75 6d 6e 20 3d 20  pec->azColumn = 
34d62 61 7a 41 72 67 3b 0a 20 20 7a 54 6f 6b 65 6e 69  azArg;.  zTokeni
34d63 7a 65 72 20 3d 20 22 74 6f 6b 65 6e 69 7a 65 20  zer = "tokenize 
34d64 73 69 6d 70 6c 65 22 3b 0a 20 20 66 6f 72 28 69  simple";.  for(i
34d65 3d 33 3b 20 69 3c 61 72 67 63 3b 20 2b 2b 69 29  =3; i<argc; ++i)
34d66 7b 0a 20 20 20 20 69 66 28 20 73 74 61 72 74 73  {.    if( starts
34d67 57 69 74 68 28 61 7a 41 72 67 5b 69 5d 2c 22 74  With(azArg[i],"t
34d68 6f 6b 65 6e 69 7a 65 22 29 20 29 7b 0a 20 20 20  okenize") ){.   
34d69 20 20 20 7a 54 6f 6b 65 6e 69 7a 65 72 20 3d 20     zTokenizer = 
34d6a 61 7a 41 72 67 5b 69 5d 3b 0a 20 20 20 20 7d 65  azArg[i];.    }e
34d6b 6c 73 65 7b 0a 20 20 20 20 20 20 7a 20 3d 20 61  lse{.      z = a
34d6c 7a 41 72 67 5b 70 53 70 65 63 2d 3e 6e 43 6f 6c  zArg[pSpec->nCol
34d6d 75 6d 6e 5d 20 3d 20 66 69 72 73 74 54 6f 6b 65  umn] = firstToke
34d6e 6e 28 61 7a 41 72 67 5b 69 5d 2c 20 26 7a 44 75  n(azArg[i], &zDu
34d6f 6d 6d 79 29 3b 0a 20 20 20 20 20 20 70 53 70 65  mmy);.      pSpe
34d70 63 2d 3e 6e 43 6f 6c 75 6d 6e 2b 2b 3b 0a 20 20  c->nColumn++;.  
34d71 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 53    }.  }.  if( pS
34d72 70 65 63 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d 30 20  pec->nColumn==0 
34d73 29 7b 0a 20 20 20 20 61 7a 41 72 67 5b 30 5d 20  ){.    azArg[0] 
34d74 3d 20 22 63 6f 6e 74 65 6e 74 22 3b 0a 20 20 20  = "content";.   
34d75 20 70 53 70 65 63 2d 3e 6e 43 6f 6c 75 6d 6e 20   pSpec->nColumn 
34d76 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20  = 1;.  }..  /*. 
34d77 20 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20 74 68   ** Construct th
34d78 65 20 6c 69 73 74 20 6f 66 20 63 6f 6e 74 65 6e  e list of conten
34d79 74 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e 0a  t column names..
34d7a 20 20 2a 2a 0a 20 20 2a 2a 20 45 61 63 68 20 63    **.  ** Each c
34d7b 6f 6e 74 65 6e 74 20 63 6f 6c 75 6d 6e 20 6e 61  ontent column na
34d7c 6d 65 20 77 69 6c 6c 20 62 65 20 6f 66 20 74 68  me will be of th
34d7d 65 20 66 6f 72 6d 20 63 4e 4e 41 41 41 41 0a 20  e form cNNAAAA. 
34d7e 20 2a 2a 20 77 68 65 72 65 20 4e 4e 20 69 73 20   ** where NN is 
34d7f 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65  the column numbe
34d80 72 20 61 6e 64 20 41 41 41 41 20 69 73 20 74 68  r and AAAA is th
34d81 65 20 73 61 6e 69 74 69 7a 65 64 0a 20 20 2a 2a  e sanitized.  **
34d82 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 2e 20 20 22   column name.  "
34d83 73 61 6e 69 74 69 7a 65 64 22 20 6d 65 61 6e 73  sanitized" means
34d84 20 74 68 61 74 20 73 70 65 63 69 61 6c 20 63 68   that special ch
34d85 61 72 61 63 74 65 72 73 20 61 72 65 0a 20 20 2a  aracters are.  *
34d86 2a 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 22  * converted to "
34d87 5f 22 2e 20 20 54 68 65 20 63 4e 4e 20 70 72 65  _".  The cNN pre
34d88 66 69 78 20 67 75 61 72 61 6e 74 65 65 73 20 74  fix guarantees t
34d89 68 61 74 20 61 6c 6c 20 63 6f 6c 75 6d 6e 0a 20  hat all column. 
34d8a 20 2a 2a 20 6e 61 6d 65 73 20 61 72 65 20 75 6e   ** names are un
34d8b 69 71 75 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ique..  **.  ** 
34d8c 54 68 65 20 41 41 41 41 20 73 75 66 66 69 78 20  The AAAA suffix 
34d8d 69 73 20 6e 6f 74 20 73 74 72 69 63 74 6c 79 20  is not strictly 
34d8e 6e 65 63 65 73 73 61 72 79 2e 20 20 49 74 20 69  necessary.  It i
34d8f 73 20 69 6e 63 6c 75 64 65 64 0a 20 20 2a 2a 20  s included.  ** 
34d90 66 6f 72 20 74 68 65 20 63 6f 6e 76 65 6e 69 65  for the convenie
34d91 6e 63 65 20 6f 66 20 70 65 6f 70 6c 65 20 77 68  nce of people wh
34d92 6f 20 6d 69 67 68 74 20 65 78 61 6d 69 6e 65 20  o might examine 
34d93 74 68 65 20 67 65 6e 65 72 61 74 65 64 0a 20 20  the generated.  
34d94 2a 2a 20 25 5f 63 6f 6e 74 65 6e 74 20 74 61 62  ** %_content tab
34d95 6c 65 20 61 6e 64 20 77 6f 6e 64 65 72 20 77 68  le and wonder wh
34d96 61 74 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 61  at the columns a
34d97 72 65 20 75 73 65 64 20 66 6f 72 2e 0a 20 20 2a  re used for..  *
34d98 2f 0a 20 20 70 53 70 65 63 2d 3e 61 7a 43 6f 6e  /.  pSpec->azCon
34d99 74 65 6e 74 43 6f 6c 75 6d 6e 20 3d 20 73 71 6c  tentColumn = sql
34d9a 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 70 53 70  ite3_malloc( pSp
34d9b 65 63 2d 3e 6e 43 6f 6c 75 6d 6e 20 2a 20 73 69  ec->nColumn * si
34d9c 7a 65 6f 66 28 63 68 61 72 20 2a 29 20 29 3b 0a  zeof(char *) );.
34d9d 20 20 69 66 28 20 70 53 70 65 63 2d 3e 61 7a 43    if( pSpec->azC
34d9e 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 3d 3d 30 20  ontentColumn==0 
34d9f 29 7b 0a 20 20 20 20 63 6c 65 61 72 54 61 62 6c  ){.    clearTabl
34da0 65 53 70 65 63 28 70 53 70 65 63 29 3b 0a 20 20  eSpec(pSpec);.  
34da1 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
34da2 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 66 6f 72  NOMEM;.  }.  for
34da3 28 69 3d 30 3b 20 69 3c 70 53 70 65 63 2d 3e 6e  (i=0; i<pSpec->n
34da4 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Column; i++){.  
34da5 20 20 63 68 61 72 20 2a 70 3b 0a 20 20 20 20 70    char *p;.    p
34da6 53 70 65 63 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43  Spec->azContentC
34da7 6f 6c 75 6d 6e 5b 69 5d 20 3d 20 73 71 6c 69 74  olumn[i] = sqlit
34da8 65 33 5f 6d 70 72 69 6e 74 66 28 22 63 25 64 25  e3_mprintf("c%d%
34da9 73 22 2c 20 69 2c 20 61 7a 41 72 67 5b 69 5d 29  s", i, azArg[i])
34daa 3b 0a 20 20 20 20 66 6f 72 20 28 70 20 3d 20 70  ;.    for (p = p
34dab 53 70 65 63 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43  Spec->azContentC
34dac 6f 6c 75 6d 6e 5b 69 5d 3b 20 2a 70 20 3b 20 2b  olumn[i]; *p ; +
34dad 2b 70 29 20 7b 0a 20 20 20 20 20 20 69 66 28 20  +p) {.      if( 
34dae 21 73 61 66 65 5f 69 73 61 6c 6e 75 6d 28 2a 70  !safe_isalnum(*p
34daf 29 20 29 20 2a 70 20 3d 20 27 5f 27 3b 0a 20 20  ) ) *p = '_';.  
34db0 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20    }.  }..  /*.  
34db1 2a 2a 20 50 61 72 73 65 20 74 68 65 20 74 6f 6b  ** Parse the tok
34db2 65 6e 69 7a 65 72 20 73 70 65 63 69 66 69 63 61  enizer specifica
34db3 74 69 6f 6e 20 73 74 72 69 6e 67 2e 0a 20 20 2a  tion string..  *
34db4 2f 0a 20 20 70 53 70 65 63 2d 3e 61 7a 54 6f 6b  /.  pSpec->azTok
34db5 65 6e 69 7a 65 72 20 3d 20 74 6f 6b 65 6e 69 7a  enizer = tokeniz
34db6 65 53 74 72 69 6e 67 28 7a 54 6f 6b 65 6e 69 7a  eString(zTokeniz
34db7 65 72 2c 20 26 6e 29 3b 0a 20 20 74 6f 6b 65 6e  er, &n);.  token
34db8 4c 69 73 74 54 6f 49 64 4c 69 73 74 28 70 53 70  ListToIdList(pSp
34db9 65 63 2d 3e 61 7a 54 6f 6b 65 6e 69 7a 65 72 29  ec->azTokenizer)
34dba 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  ;..  return SQLI
34dbb 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
34dbc 47 65 6e 65 72 61 74 65 20 61 20 43 52 45 41 54  Generate a CREAT
34dbd 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e  E TABLE statemen
34dbe 74 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73  t that describes
34dbf 20 74 68 65 20 73 63 68 65 6d 61 20 6f 66 0a 2a   the schema of.*
34dc0 2a 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  * the virtual ta
34dc1 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 61 20 70  ble.  Return a p
34dc2 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73  ointer to this s
34dc3 63 68 65 6d 61 20 73 74 72 69 6e 67 2e 0a 2a 2a  chema string..**
34dc4 0a 2a 2a 20 53 70 61 63 65 20 69 73 20 6f 62 74  .** Space is obt
34dc5 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74  ained from sqlit
34dc6 65 33 5f 6d 70 72 69 6e 74 66 28 29 20 61 6e 64  e3_mprintf() and
34dc7 20 73 68 6f 75 6c 64 20 62 65 20 66 72 65 65 64   should be freed
34dc8 0a 2a 2a 20 75 73 69 6e 67 20 73 71 6c 69 74 65  .** using sqlite
34dc9 33 5f 66 72 65 65 28 29 2e 0a 2a 2f 0a 73 74 61  3_free()..*/.sta
34dca 74 69 63 20 63 68 61 72 20 2a 66 75 6c 6c 74 65  tic char *fullte
34dcb 78 74 53 63 68 65 6d 61 28 0a 20 20 69 6e 74 20  xtSchema(.  int 
34dcc 6e 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20  nColumn,        
34dcd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
34dce 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 2a  ber of columns *
34dcf 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
34dd0 63 6f 6e 73 74 2a 20 61 7a 43 6f 6c 75 6d 6e 2c  const* azColumn,
34dd1 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 63 6f 6c    /* List of col
34dd2 75 6d 6e 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  umns */.  const 
34dd3 63 68 61 72 20 2a 7a 54 61 62 6c 65 4e 61 6d 65  char *zTableName
34dd4 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
34dd5 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  of the table */.
34dd6 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 68  ){.  int i;.  ch
34dd7 61 72 20 2a 7a 53 63 68 65 6d 61 2c 20 2a 7a 4e  ar *zSchema, *zN
34dd8 65 78 74 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  ext;.  const cha
34dd9 72 20 2a 7a 53 65 70 20 3d 20 22 28 22 3b 0a 20  r *zSep = "(";. 
34dda 20 7a 53 63 68 65 6d 61 20 3d 20 73 71 6c 69 74   zSchema = sqlit
34ddb 65 33 5f 6d 70 72 69 6e 74 66 28 22 43 52 45 41  e3_mprintf("CREA
34ddc 54 45 20 54 41 42 4c 45 20 78 22 29 3b 0a 20 20  TE TABLE x");.  
34ddd 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 75  for(i=0; i<nColu
34dde 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 7a 4e  mn; i++){.    zN
34ddf 65 78 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  ext = sqlite3_mp
34de0 72 69 6e 74 66 28 22 25 73 25 73 25 51 22 2c 20  rintf("%s%s%Q", 
34de1 7a 53 63 68 65 6d 61 2c 20 7a 53 65 70 2c 20 61  zSchema, zSep, a
34de2 7a 43 6f 6c 75 6d 6e 5b 69 5d 29 3b 0a 20 20 20  zColumn[i]);.   
34de3 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53   sqlite3_free(zS
34de4 63 68 65 6d 61 29 3b 0a 20 20 20 20 7a 53 63 68  chema);.    zSch
34de5 65 6d 61 20 3d 20 7a 4e 65 78 74 3b 0a 20 20 20  ema = zNext;.   
34de6 20 7a 53 65 70 20 3d 20 22 2c 22 3b 0a 20 20 7d   zSep = ",";.  }
34de7 0a 20 20 7a 4e 65 78 74 20 3d 20 73 71 6c 69 74  .  zNext = sqlit
34de8 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 2c 25  e3_mprintf("%s,%
34de9 51 20 48 49 44 44 45 4e 22 2c 20 7a 53 63 68 65  Q HIDDEN", zSche
34dea 6d 61 2c 20 7a 54 61 62 6c 65 4e 61 6d 65 29 3b  ma, zTableName);
34deb 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
34dec 7a 53 63 68 65 6d 61 29 3b 0a 20 20 7a 53 63 68  zSchema);.  zSch
34ded 65 6d 61 20 3d 20 7a 4e 65 78 74 3b 0a 20 20 7a  ema = zNext;.  z
34dee 4e 65 78 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d  Next = sqlite3_m
34def 70 72 69 6e 74 66 28 22 25 73 2c 64 6f 63 69 64  printf("%s,docid
34df0 20 48 49 44 44 45 4e 29 22 2c 20 7a 53 63 68 65   HIDDEN)", zSche
34df1 6d 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  ma);.  sqlite3_f
34df2 72 65 65 28 7a 53 63 68 65 6d 61 29 3b 0a 20 20  ree(zSchema);.  
34df3 72 65 74 75 72 6e 20 7a 4e 65 78 74 3b 0a 7d 0a  return zNext;.}.
34df4 0a 2f 2a 0a 2a 2a 20 42 75 69 6c 64 20 61 20 6e  ./*.** Build a n
34df5 65 77 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  ew sqlite3_vtab 
34df6 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 77  structure that w
34df7 69 6c 6c 20 64 65 73 63 72 69 62 65 20 74 68 65  ill describe the
34df8 0a 2a 2a 20 66 75 6c 6c 74 65 78 74 20 69 6e 64  .** fulltext ind
34df9 65 78 20 64 65 66 69 6e 65 64 20 62 79 20 73 70  ex defined by sp
34dfa 65 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ec..*/.static in
34dfb 74 20 63 6f 6e 73 74 72 75 63 74 56 74 61 62 28  t constructVtab(
34dfc 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
34dfd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
34dfe 54 68 65 20 53 51 4c 69 74 65 20 64 61 74 61 62  The SQLite datab
34dff 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
34e00 2f 0a 20 20 66 74 73 33 48 61 73 68 20 2a 70 48  /.  fts3Hash *pH
34e01 61 73 68 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  ash,          /*
34e02 20 48 61 73 68 20 74 61 62 6c 65 20 63 6f 6e 74   Hash table cont
34e03 61 69 6e 69 6e 67 20 74 6f 6b 65 6e 69 7a 65 72  aining tokenizer
34e04 73 20 2a 2f 0a 20 20 54 61 62 6c 65 53 70 65 63  s */.  TableSpec
34e05 20 2a 73 70 65 63 2c 20 20 20 20 20 20 20 20 20   *spec,         
34e06 20 2f 2a 20 50 61 72 73 65 64 20 73 70 65 63 20   /* Parsed spec 
34e07 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d  information from
34e08 20 70 61 72 73 65 53 70 65 63 28 29 20 2a 2f 0a   parseSpec() */.
34e09 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
34e0a 2a 70 70 56 54 61 62 2c 20 20 20 20 2f 2a 20 57  *ppVTab,    /* W
34e0b 72 69 74 65 20 74 68 65 20 72 65 73 75 6c 74 69  rite the resulti
34e0c 6e 67 20 76 74 61 62 20 73 74 72 75 63 74 75 72  ng vtab structur
34e0d 65 20 68 65 72 65 20 2a 2f 0a 20 20 63 68 61 72  e here */.  char
34e0e 20 2a 2a 70 7a 45 72 72 20 20 20 20 20 20 20 20   **pzErr        
34e0f 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 61        /* Write a
34e10 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  ny error message
34e11 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e   here */.){.  in
34e12 74 20 72 63 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20  t rc;.  int n;. 
34e13 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a   fulltext_vtab *
34e14 76 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 73  v = 0;.  const s
34e15 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
34e16 5f 6d 6f 64 75 6c 65 20 2a 6d 20 3d 20 4e 55 4c  _module *m = NUL
34e17 4c 3b 0a 20 20 63 68 61 72 20 2a 73 63 68 65 6d  L;.  char *schem
34e18 61 3b 0a 0a 20 20 63 68 61 72 20 63 6f 6e 73 74  a;..  char const
34e19 20 2a 7a 54 6f 6b 3b 20 20 20 20 20 20 20 20 20   *zTok;         
34e1a 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 6f 6b 65 6e  /* Name of token
34e1b 69 7a 65 72 20 74 6f 20 75 73 65 20 66 6f 72 20  izer to use for 
34e1c 74 68 69 73 20 66 74 73 20 74 61 62 6c 65 20 2a  this fts table *
34e1d 2f 0a 20 20 69 6e 74 20 6e 54 6f 6b 3b 20 20 20  /.  int nTok;   
34e1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
34e1f 20 4c 65 6e 67 74 68 20 6f 66 20 7a 54 6f 6b 2c   Length of zTok,
34e20 20 69 6e 63 6c 75 64 69 6e 67 20 6e 75 6c 20 74   including nul t
34e21 65 72 6d 69 6e 61 74 6f 72 20 2a 2f 0a 0a 20 20  erminator */..  
34e22 76 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f 76 74  v = (fulltext_vt
34e23 61 62 20 2a 29 20 73 71 6c 69 74 65 33 5f 6d 61  ab *) sqlite3_ma
34e24 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 66 75 6c 6c  lloc(sizeof(full
34e25 74 65 78 74 5f 76 74 61 62 29 29 3b 0a 20 20 69  text_vtab));.  i
34e26 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( v==0 ) return
34e27 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
34e28 20 43 4c 45 41 52 28 76 29 3b 0a 20 20 2f 2a 20   CLEAR(v);.  /* 
34e29 73 71 6c 69 74 65 20 77 69 6c 6c 20 69 6e 69 74  sqlite will init
34e2a 69 61 6c 69 7a 65 20 76 2d 3e 62 61 73 65 20 2a  ialize v->base *
34e2b 2f 0a 20 20 76 2d 3e 64 62 20 3d 20 64 62 3b 0a  /.  v->db = db;.
34e2c 20 20 76 2d 3e 7a 44 62 20 3d 20 73 70 65 63 2d    v->zDb = spec-
34e2d 3e 7a 44 62 3b 20 20 20 20 20 20 20 2f 2a 20 46  >zDb;       /* F
34e2e 72 65 65 64 20 77 68 65 6e 20 61 7a 43 6f 6c 75  reed when azColu
34e2f 6d 6e 20 69 73 20 66 72 65 65 64 20 2a 2f 0a 20  mn is freed */. 
34e30 20 76 2d 3e 7a 4e 61 6d 65 20 3d 20 73 70 65 63   v->zName = spec
34e31 2d 3e 7a 4e 61 6d 65 3b 20 20 20 2f 2a 20 46 72  ->zName;   /* Fr
34e32 65 65 64 20 77 68 65 6e 20 61 7a 43 6f 6c 75 6d  eed when azColum
34e33 6e 20 69 73 20 66 72 65 65 64 20 2a 2f 0a 20 20  n is freed */.  
34e34 76 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 73 70 65  v->nColumn = spe
34e35 63 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 76 2d  c->nColumn;.  v-
34e36 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e  >azContentColumn
34e37 20 3d 20 73 70 65 63 2d 3e 61 7a 43 6f 6e 74 65   = spec->azConte
34e38 6e 74 43 6f 6c 75 6d 6e 3b 0a 20 20 73 70 65 63  ntColumn;.  spec
34e39 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d  ->azContentColum
34e3a 6e 20 3d 20 30 3b 0a 20 20 76 2d 3e 61 7a 43 6f  n = 0;.  v->azCo
34e3b 6c 75 6d 6e 20 3d 20 73 70 65 63 2d 3e 61 7a 43  lumn = spec->azC
34e3c 6f 6c 75 6d 6e 3b 0a 20 20 73 70 65 63 2d 3e 61  olumn;.  spec->a
34e3d 7a 43 6f 6c 75 6d 6e 20 3d 20 30 3b 0a 0a 20 20  zColumn = 0;..  
34e3e 69 66 28 20 73 70 65 63 2d 3e 61 7a 54 6f 6b 65  if( spec->azToke
34e3f 6e 69 7a 65 72 3d 3d 30 20 29 7b 0a 20 20 20 20  nizer==0 ){.    
34e40 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
34e41 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 7a 54 6f 6b  MEM;.  }..  zTok
34e42 20 3d 20 73 70 65 63 2d 3e 61 7a 54 6f 6b 65 6e   = spec->azToken
34e43 69 7a 65 72 5b 30 5d 3b 20 0a 20 20 69 66 28 20  izer[0]; .  if( 
34e44 21 7a 54 6f 6b 20 29 7b 0a 20 20 20 20 7a 54 6f  !zTok ){.    zTo
34e45 6b 20 3d 20 22 73 69 6d 70 6c 65 22 3b 0a 20 20  k = "simple";.  
34e46 7d 0a 20 20 6e 54 6f 6b 20 3d 20 73 74 72 6c 65  }.  nTok = strle
34e47 6e 28 7a 54 6f 6b 29 2b 31 3b 0a 0a 20 20 6d 20  n(zTok)+1;..  m 
34e48 3d 20 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  = (sqlite3_token
34e49 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 29 73 71  izer_module *)sq
34e4a 6c 69 74 65 33 46 74 73 33 48 61 73 68 46 69 6e  lite3Fts3HashFin
34e4b 64 28 70 48 61 73 68 2c 20 7a 54 6f 6b 2c 20 6e  d(pHash, zTok, n
34e4c 54 6f 6b 29 3b 0a 20 20 69 66 28 20 21 6d 20 29  Tok);.  if( !m )
34e4d 7b 0a 20 20 20 20 2a 70 7a 45 72 72 20 3d 20 73  {.    *pzErr = s
34e4e 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
34e4f 75 6e 6b 6e 6f 77 6e 20 74 6f 6b 65 6e 69 7a 65  unknown tokenize
34e50 72 3a 20 25 73 22 2c 20 73 70 65 63 2d 3e 61 7a  r: %s", spec->az
34e51 54 6f 6b 65 6e 69 7a 65 72 5b 30 5d 29 3b 0a 20  Tokenizer[0]);. 
34e52 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
34e53 52 52 4f 52 3b 0a 20 20 20 20 67 6f 74 6f 20 65  RROR;.    goto e
34e54 72 72 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 6e  rr;.  }..  for(n
34e55 3d 30 3b 20 73 70 65 63 2d 3e 61 7a 54 6f 6b 65  =0; spec->azToke
34e56 6e 69 7a 65 72 5b 6e 5d 3b 20 6e 2b 2b 29 7b 7d  nizer[n]; n++){}
34e57 0a 20 20 69 66 28 20 6e 20 29 7b 0a 20 20 20 20  .  if( n ){.    
34e58 72 63 20 3d 20 6d 2d 3e 78 43 72 65 61 74 65 28  rc = m->xCreate(
34e59 6e 2d 31 2c 20 28 63 6f 6e 73 74 20 63 68 61 72  n-1, (const char
34e5a 2a 63 6f 6e 73 74 2a 29 26 73 70 65 63 2d 3e 61  *const*)&spec->a
34e5b 7a 54 6f 6b 65 6e 69 7a 65 72 5b 31 5d 2c 0a 20  zTokenizer[1],. 
34e5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34e5d 20 20 20 26 76 2d 3e 70 54 6f 6b 65 6e 69 7a 65     &v->pTokenize
34e5e 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  r);.  }else{.   
34e5f 20 72 63 20 3d 20 6d 2d 3e 78 43 72 65 61 74 65   rc = m->xCreate
34e60 28 30 2c 20 30 2c 20 26 76 2d 3e 70 54 6f 6b 65  (0, 0, &v->pToke
34e61 6e 69 7a 65 72 29 3b 0a 20 20 7d 0a 20 20 69 66  nizer);.  }.  if
34e62 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
34e63 29 20 67 6f 74 6f 20 65 72 72 3b 0a 20 20 76 2d  ) goto err;.  v-
34e64 3e 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f  >pTokenizer->pMo
34e65 64 75 6c 65 20 3d 20 6d 3b 0a 0a 20 20 2f 2a 20  dule = m;..  /* 
34e66 54 4f 44 4f 3a 20 76 65 72 69 66 79 20 74 68 65  TODO: verify the
34e67 20 65 78 69 73 74 65 6e 63 65 20 6f 66 20 62 61   existence of ba
34e68 63 6b 69 6e 67 20 74 61 62 6c 65 73 20 66 6f 6f  cking tables foo
34e69 5f 63 6f 6e 74 65 6e 74 2c 20 66 6f 6f 5f 74 65  _content, foo_te
34e6a 72 6d 20 2a 2f 0a 0a 20 20 73 63 68 65 6d 61 20  rm */..  schema 
34e6b 3d 20 66 75 6c 6c 74 65 78 74 53 63 68 65 6d 61  = fulltextSchema
34e6c 28 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 28 63 6f  (v->nColumn, (co
34e6d 6e 73 74 20 63 68 61 72 2a 63 6f 6e 73 74 2a 29  nst char*const*)
34e6e 76 2d 3e 61 7a 43 6f 6c 75 6d 6e 2c 0a 20 20 20  v->azColumn,.   
34e6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34e70 20 20 20 20 20 20 20 73 70 65 63 2d 3e 7a 4e 61         spec->zNa
34e71 6d 65 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  me);.  rc = sqli
34e72 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62  te3_declare_vtab
34e73 28 64 62 2c 20 73 63 68 65 6d 61 29 3b 0a 20 20  (db, schema);.  
34e74 73 71 6c 69 74 65 33 5f 66 72 65 65 28 73 63 68  sqlite3_free(sch
34e75 65 6d 61 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  ema);.  if( rc!=
34e76 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
34e77 20 65 72 72 3b 0a 0a 20 20 6d 65 6d 73 65 74 28   err;..  memset(
34e78 76 2d 3e 70 46 75 6c 6c 74 65 78 74 53 74 61 74  v->pFulltextStat
34e79 65 6d 65 6e 74 73 2c 20 30 2c 20 73 69 7a 65 6f  ements, 0, sizeo
34e7a 66 28 76 2d 3e 70 46 75 6c 6c 74 65 78 74 53 74  f(v->pFulltextSt
34e7b 61 74 65 6d 65 6e 74 73 29 29 3b 0a 0a 20 20 2f  atements));..  /
34e7c 2a 20 49 6e 64 69 63 61 74 65 20 74 68 61 74 20  * Indicate that 
34e7d 74 68 65 20 62 75 66 66 65 72 20 69 73 20 6e 6f  the buffer is no
34e7e 74 20 6c 69 76 65 2e 20 2a 2f 0a 20 20 76 2d 3e  t live. */.  v->
34e7f 6e 50 65 6e 64 69 6e 67 44 61 74 61 20 3d 20 2d  nPendingData = -
34e80 31 3b 0a 0a 20 20 2a 70 70 56 54 61 62 20 3d 20  1;..  *ppVTab = 
34e81 26 76 2d 3e 62 61 73 65 3b 0a 20 20 46 54 53 54  &v->base;.  FTST
34e82 52 41 43 45 28 28 22 46 54 53 33 20 43 6f 6e 6e  RACE(("FTS3 Conn
34e83 65 63 74 20 25 70 5c 6e 22 2c 20 76 29 29 3b 0a  ect %p\n", v));.
34e84 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 65  .  return rc;..e
34e85 72 72 3a 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76  rr:.  fulltext_v
34e86 74 61 62 5f 64 65 73 74 72 6f 79 28 76 29 3b 0a  tab_destroy(v);.
34e87 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
34e88 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74  static int fullt
34e89 65 78 74 43 6f 6e 6e 65 63 74 28 0a 20 20 73 71  extConnect(.  sq
34e8a 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 76 6f 69  lite3 *db,.  voi
34e8b 64 20 2a 70 41 75 78 2c 0a 20 20 69 6e 74 20 61  d *pAux,.  int a
34e8c 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  rgc, const char 
34e8d 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20 73  *const*argv,.  s
34e8e 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70  qlite3_vtab **pp
34e8f 56 54 61 62 2c 0a 20 20 63 68 61 72 20 2a 2a 70  VTab,.  char **p
34e90 7a 45 72 72 0a 29 7b 0a 20 20 54 61 62 6c 65 53  zErr.){.  TableS
34e91 70 65 63 20 73 70 65 63 3b 0a 20 20 69 6e 74 20  pec spec;.  int 
34e92 72 63 20 3d 20 70 61 72 73 65 53 70 65 63 28 26  rc = parseSpec(&
34e93 73 70 65 63 2c 20 61 72 67 63 2c 20 61 72 67 76  spec, argc, argv
34e94 2c 20 70 7a 45 72 72 29 3b 0a 20 20 69 66 28 20  , pzErr);.  if( 
34e95 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
34e96 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63  return rc;..  rc
34e97 20 3d 20 63 6f 6e 73 74 72 75 63 74 56 74 61 62   = constructVtab
34e98 28 64 62 2c 20 28 66 74 73 33 48 61 73 68 20 2a  (db, (fts3Hash *
34e99 29 70 41 75 78 2c 20 26 73 70 65 63 2c 20 70 70  )pAux, &spec, pp
34e9a 56 54 61 62 2c 20 70 7a 45 72 72 29 3b 0a 20 20  VTab, pzErr);.  
34e9b 63 6c 65 61 72 54 61 62 6c 65 53 70 65 63 28 26  clearTableSpec(&
34e9c 73 70 65 63 29 3b 0a 20 20 72 65 74 75 72 6e 20  spec);.  return 
34e9d 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 25 5f  rc;.}../* The %_
34e9e 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 20 68 6f  content table ho
34e9f 6c 64 73 20 74 68 65 20 74 65 78 74 20 6f 66 20  lds the text of 
34ea0 65 61 63 68 20 64 6f 63 75 6d 65 6e 74 2c 20 77  each document, w
34ea1 69 74 68 0a 2a 2a 20 74 68 65 20 64 6f 63 69 64  ith.** the docid
34ea2 20 63 6f 6c 75 6d 6e 20 65 78 70 6f 73 65 64 20   column exposed 
34ea3 61 73 20 74 68 65 20 53 51 4c 69 74 65 20 72 6f  as the SQLite ro
34ea4 77 69 64 20 66 6f 72 20 74 68 65 20 74 61 62 6c  wid for the tabl
34ea5 65 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68  e..*/./* TODO(sh
34ea6 65 73 73 29 20 54 68 69 73 20 63 6f 6d 6d 65 6e  ess) This commen
34ea7 74 20 6e 65 65 64 73 20 65 6c 61 62 6f 72 61 74  t needs elaborat
34ea8 69 6f 6e 20 74 6f 20 6d 61 74 63 68 20 74 68 65  ion to match the
34ea9 20 75 70 64 61 74 65 64 0a 2a 2a 20 63 6f 64 65   updated.** code
34eaa 2e 20 20 57 6f 72 6b 20 69 74 20 69 6e 74 6f 20  .  Work it into 
34eab 74 68 65 20 74 6f 70 2d 6f 66 2d 66 69 6c 65 20  the top-of-file 
34eac 63 6f 6d 6d 65 6e 74 20 61 74 20 74 68 61 74 20  comment at that 
34ead 74 69 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  time..*/.static 
34eae 69 6e 74 20 66 75 6c 6c 74 65 78 74 43 72 65 61  int fulltextCrea
34eaf 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  te(sqlite3 *db, 
34eb0 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20 20 20  void *pAux,.    
34eb1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34eb2 20 20 20 20 20 20 69 6e 74 20 61 72 67 63 2c 20        int argc, 
34eb3 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 63 6f 6e  const char * con
34eb4 73 74 20 2a 61 72 67 76 2c 0a 20 20 20 20 20 20  st *argv,.      
34eb5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34eb6 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62      sqlite3_vtab
34eb7 20 2a 2a 70 70 56 54 61 62 2c 20 63 68 61 72 20   **ppVTab, char 
34eb8 2a 2a 70 7a 45 72 72 29 7b 0a 20 20 69 6e 74 20  **pzErr){.  int 
34eb9 72 63 3b 0a 20 20 54 61 62 6c 65 53 70 65 63 20  rc;.  TableSpec 
34eba 73 70 65 63 3b 0a 20 20 53 74 72 69 6e 67 42 75  spec;.  StringBu
34ebb 66 66 65 72 20 73 63 68 65 6d 61 3b 0a 20 20 46  ffer schema;.  F
34ebc 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20 43  TSTRACE(("FTS3 C
34ebd 72 65 61 74 65 5c 6e 22 29 29 3b 0a 0a 20 20 72  reate\n"));..  r
34ebe 63 20 3d 20 70 61 72 73 65 53 70 65 63 28 26 73  c = parseSpec(&s
34ebf 70 65 63 2c 20 61 72 67 63 2c 20 61 72 67 76 2c  pec, argc, argv,
34ec0 20 70 7a 45 72 72 29 3b 0a 20 20 69 66 28 20 72   pzErr);.  if( r
34ec1 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
34ec2 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 69 6e 69  eturn rc;..  ini
34ec3 74 53 74 72 69 6e 67 42 75 66 66 65 72 28 26 73  tStringBuffer(&s
34ec4 63 68 65 6d 61 29 3b 0a 20 20 61 70 70 65 6e 64  chema);.  append
34ec5 28 26 73 63 68 65 6d 61 2c 20 22 43 52 45 41 54  (&schema, "CREAT
34ec6 45 20 54 41 42 4c 45 20 25 5f 63 6f 6e 74 65 6e  E TABLE %_conten
34ec7 74 28 22 29 3b 0a 20 20 61 70 70 65 6e 64 28 26  t(");.  append(&
34ec8 73 63 68 65 6d 61 2c 20 22 20 20 64 6f 63 69 64  schema, "  docid
34ec9 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
34eca 20 4b 45 59 2c 22 29 3b 0a 20 20 61 70 70 65 6e   KEY,");.  appen
34ecb 64 4c 69 73 74 28 26 73 63 68 65 6d 61 2c 20 73  dList(&schema, s
34ecc 70 65 63 2e 6e 43 6f 6c 75 6d 6e 2c 20 73 70 65  pec.nColumn, spe
34ecd 63 2e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d  c.azContentColum
34ece 6e 29 3b 0a 20 20 61 70 70 65 6e 64 28 26 73 63  n);.  append(&sc
34ecf 68 65 6d 61 2c 20 22 29 22 29 3b 0a 20 20 72 63  hema, ")");.  rc
34ed0 20 3d 20 73 71 6c 5f 65 78 65 63 28 64 62 2c 20   = sql_exec(db, 
34ed1 73 70 65 63 2e 7a 44 62 2c 20 73 70 65 63 2e 7a  spec.zDb, spec.z
34ed2 4e 61 6d 65 2c 20 73 74 72 69 6e 67 42 75 66 66  Name, stringBuff
34ed3 65 72 44 61 74 61 28 26 73 63 68 65 6d 61 29 29  erData(&schema))
34ed4 3b 0a 20 20 73 74 72 69 6e 67 42 75 66 66 65 72  ;.  stringBuffer
34ed5 44 65 73 74 72 6f 79 28 26 73 63 68 65 6d 61 29  Destroy(&schema)
34ed6 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
34ed7 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 6f 75 74  TE_OK ) goto out
34ed8 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 5f 65 78  ;..  rc = sql_ex
34ed9 65 63 28 64 62 2c 20 73 70 65 63 2e 7a 44 62 2c  ec(db, spec.zDb,
34eda 20 73 70 65 63 2e 7a 4e 61 6d 65 2c 0a 20 20 20   spec.zName,.   
34edb 20 20 20 20 20 20 20 20 20 20 20 20 20 22 63 72               "cr
34edc 65 61 74 65 20 74 61 62 6c 65 20 25 5f 73 65 67  eate table %_seg
34edd 6d 65 6e 74 73 28 22 0a 20 20 20 20 20 20 20 20  ments(".        
34ede 20 20 20 20 20 20 20 20 22 20 20 62 6c 6f 63 6b          "  block
34edf 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  id INTEGER PRIMA
34ee0 52 59 20 4b 45 59 2c 22 0a 20 20 20 20 20 20 20  RY KEY,".       
34ee1 20 20 20 20 20 20 20 20 20 22 20 20 62 6c 6f 63           "  bloc
34ee2 6b 20 62 6c 6f 62 22 0a 20 20 20 20 20 20 20 20  k blob".        
34ee3 20 20 20 20 20 20 20 20 22 29 3b 22 0a 20 20 20          ");".   
34ee4 20 20 20 20 20 20 20 20 20 20 20 20 20 29 3b 0a               );.
34ee5 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
34ee6 5f 4f 4b 20 29 20 67 6f 74 6f 20 6f 75 74 3b 0a  _OK ) goto out;.
34ee7 0a 20 20 72 63 20 3d 20 73 71 6c 5f 65 78 65 63  .  rc = sql_exec
34ee8 28 64 62 2c 20 73 70 65 63 2e 7a 44 62 2c 20 73  (db, spec.zDb, s
34ee9 70 65 63 2e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20  pec.zName,.     
34eea 20 20 20 20 20 20 20 20 20 20 20 22 63 72 65 61             "crea
34eeb 74 65 20 74 61 62 6c 65 20 25 5f 73 65 67 64 69  te table %_segdi
34eec 72 28 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  r(".            
34eed 20 20 20 20 22 20 20 6c 65 76 65 6c 20 69 6e 74      "  level int
34eee 65 67 65 72 2c 22 0a 20 20 20 20 20 20 20 20 20  eger,".         
34eef 20 20 20 20 20 20 20 22 20 20 69 64 78 20 69 6e         "  idx in
34ef0 74 65 67 65 72 2c 22 0a 20 20 20 20 20 20 20 20  teger,".        
34ef1 20 20 20 20 20 20 20 20 22 20 20 73 74 61 72 74          "  start
34ef2 5f 62 6c 6f 63 6b 20 69 6e 74 65 67 65 72 2c 22  _block integer,"
34ef3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
34ef4 20 22 20 20 6c 65 61 76 65 73 5f 65 6e 64 5f 62   "  leaves_end_b
34ef5 6c 6f 63 6b 20 69 6e 74 65 67 65 72 2c 22 0a 20  lock integer,". 
34ef6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
34ef7 20 20 65 6e 64 5f 62 6c 6f 63 6b 20 69 6e 74 65    end_block inte
34ef8 67 65 72 2c 22 0a 20 20 20 20 20 20 20 20 20 20  ger,".          
34ef9 20 20 20 20 20 20 22 20 20 72 6f 6f 74 20 62 6c        "  root bl
34efa 6f 62 2c 22 0a 20 20 20 20 20 20 20 20 20 20 20  ob,".           
34efb 20 20 20 20 20 22 20 20 70 72 69 6d 61 72 79 20       "  primary 
34efc 6b 65 79 28 6c 65 76 65 6c 2c 20 69 64 78 29 22  key(level, idx)"
34efd 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
34efe 20 22 29 3b 22 29 3b 0a 20 20 69 66 28 20 72 63   ");");.  if( rc
34eff 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
34f00 74 6f 20 6f 75 74 3b 0a 0a 20 20 72 63 20 3d 20  to out;..  rc = 
34f01 63 6f 6e 73 74 72 75 63 74 56 74 61 62 28 64 62  constructVtab(db
34f02 2c 20 28 66 74 73 33 48 61 73 68 20 2a 29 70 41  , (fts3Hash *)pA
34f03 75 78 2c 20 26 73 70 65 63 2c 20 70 70 56 54 61  ux, &spec, ppVTa
34f04 62 2c 20 70 7a 45 72 72 29 3b 0a 0a 6f 75 74 3a  b, pzErr);..out:
34f05 0a 20 20 63 6c 65 61 72 54 61 62 6c 65 53 70 65  .  clearTableSpe
34f06 63 28 26 73 70 65 63 29 3b 0a 20 20 72 65 74 75  c(&spec);.  retu
34f07 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 44 65 63  rn rc;.}../* Dec
34f08 69 64 65 20 68 6f 77 20 74 6f 20 68 61 6e 64 6c  ide how to handl
34f09 65 20 61 6e 20 53 51 4c 20 71 75 65 72 79 2e 20  e an SQL query. 
34f0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75  */.static int fu
34f0b 6c 6c 74 65 78 74 42 65 73 74 49 6e 64 65 78 28  lltextBestIndex(
34f0c 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
34f0d 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 69 6e 64  Tab, sqlite3_ind
34f0e 65 78 5f 69 6e 66 6f 20 2a 70 49 6e 66 6f 29 7b  ex_info *pInfo){
34f0f 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  .  fulltext_vtab
34f10 20 2a 76 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f   *v = (fulltext_
34f11 76 74 61 62 20 2a 29 70 56 54 61 62 3b 0a 20 20  vtab *)pVTab;.  
34f12 69 6e 74 20 69 3b 0a 20 20 46 54 53 54 52 41 43  int i;.  FTSTRAC
34f13 45 28 28 22 46 54 53 33 20 42 65 73 74 49 6e 64  E(("FTS3 BestInd
34f14 65 78 5c 6e 22 29 29 3b 0a 0a 20 20 66 6f 72 28  ex\n"));..  for(
34f15 69 3d 30 3b 20 69 3c 70 49 6e 66 6f 2d 3e 6e 43  i=0; i<pInfo->nC
34f16 6f 6e 73 74 72 61 69 6e 74 3b 20 2b 2b 69 29 7b  onstraint; ++i){
34f17 0a 20 20 20 20 63 6f 6e 73 74 20 73 74 72 75 63  .    const struc
34f18 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  t sqlite3_index_
34f19 63 6f 6e 73 74 72 61 69 6e 74 20 2a 70 43 6f 6e  constraint *pCon
34f1a 73 74 72 61 69 6e 74 3b 0a 20 20 20 20 70 43 6f  straint;.    pCo
34f1b 6e 73 74 72 61 69 6e 74 20 3d 20 26 70 49 6e 66  nstraint = &pInf
34f1c 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69  o->aConstraint[i
34f1d 5d 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6e 73  ];.    if( pCons
34f1e 74 72 61 69 6e 74 2d 3e 75 73 61 62 6c 65 20 29  traint->usable )
34f1f 20 7b 0a 20 20 20 20 20 20 69 66 28 20 28 70 43   {.      if( (pC
34f20 6f 6e 73 74 72 61 69 6e 74 2d 3e 69 43 6f 6c 75  onstraint->iColu
34f21 6d 6e 3d 3d 2d 31 20 7c 7c 20 70 43 6f 6e 73 74  mn==-1 || pConst
34f22 72 61 69 6e 74 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d  raint->iColumn==
34f23 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 20 26 26  v->nColumn+1) &&
34f24 0a 20 20 20 20 20 20 20 20 20 20 70 43 6f 6e 73  .          pCons
34f25 74 72 61 69 6e 74 2d 3e 6f 70 3d 3d 53 51 4c 49  traint->op==SQLI
34f26 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41  TE_INDEX_CONSTRA
34f27 49 4e 54 5f 45 51 20 29 7b 0a 20 20 20 20 20 20  INT_EQ ){.      
34f28 20 20 70 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20    pInfo->idxNum 
34f29 3d 20 51 55 45 52 59 5f 44 4f 43 49 44 3b 20 20  = QUERY_DOCID;  
34f2a 20 20 20 20 2f 2a 20 6c 6f 6f 6b 75 70 20 62 79      /* lookup by
34f2b 20 64 6f 63 69 64 20 2a 2f 0a 20 20 20 20 20 20   docid */.      
34f2c 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54 53    FTSTRACE(("FTS
34f2d 33 20 51 55 45 52 59 5f 44 4f 43 49 44 5c 6e 22  3 QUERY_DOCID\n"
34f2e 29 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65  ));.      } else
34f2f 20 69 66 28 20 70 43 6f 6e 73 74 72 61 69 6e 74   if( pConstraint
34f30 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 30 20 26 26 20  ->iColumn>=0 && 
34f31 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 69 43 6f  pConstraint->iCo
34f32 6c 75 6d 6e 3c 3d 76 2d 3e 6e 43 6f 6c 75 6d 6e  lumn<=v->nColumn
34f33 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 20   &&.            
34f34 20 20 20 20 20 70 43 6f 6e 73 74 72 61 69 6e 74       pConstraint
34f35 2d 3e 6f 70 3d 3d 53 51 4c 49 54 45 5f 49 4e 44  ->op==SQLITE_IND
34f36 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4d 41  EX_CONSTRAINT_MA
34f37 54 43 48 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  TCH ){.        /
34f38 2a 20 66 75 6c 6c 2d 74 65 78 74 20 73 65 61 72  * full-text sear
34f39 63 68 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 49  ch */.        pI
34f3a 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20 51 55  nfo->idxNum = QU
34f3b 45 52 59 5f 46 55 4c 4c 54 45 58 54 20 2b 20 70  ERY_FULLTEXT + p
34f3c 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 69 43 6f 6c  Constraint->iCol
34f3d 75 6d 6e 3b 0a 20 20 20 20 20 20 20 20 46 54 53  umn;.        FTS
34f3e 54 52 41 43 45 28 28 22 46 54 53 33 20 51 55 45  TRACE(("FTS3 QUE
34f3f 52 59 5f 46 55 4c 4c 54 45 58 54 20 25 64 5c 6e  RY_FULLTEXT %d\n
34f40 22 2c 20 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e  ", pConstraint->
34f41 69 43 6f 6c 75 6d 6e 29 29 3b 0a 20 20 20 20 20  iColumn));.     
34f42 20 7d 20 65 6c 73 65 20 63 6f 6e 74 69 6e 75 65   } else continue
34f43 3b 0a 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e  ;..      pInfo->
34f44 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
34f45 5b 69 5d 2e 61 72 67 76 49 6e 64 65 78 20 3d 20  [i].argvIndex = 
34f46 31 3b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e  1;.      pInfo->
34f47 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
34f48 5b 69 5d 2e 6f 6d 69 74 20 3d 20 31 3b 0a 0a 20  [i].omit = 1;.. 
34f49 20 20 20 20 20 2f 2a 20 41 6e 20 61 72 62 69 74       /* An arbit
34f4a 72 61 72 79 20 76 61 6c 75 65 20 66 6f 72 20 6e  rary value for n
34f4b 6f 77 2e 0a 20 20 20 20 20 20 20 2a 20 54 4f 44  ow..       * TOD
34f4c 4f 3a 20 50 65 72 68 61 70 73 20 64 6f 63 69 64  O: Perhaps docid
34f4d 20 6d 61 74 63 68 65 73 20 73 68 6f 75 6c 64 20   matches should 
34f4e 62 65 20 63 6f 6e 73 69 64 65 72 65 64 20 63 68  be considered ch
34f4f 65 61 70 65 72 20 74 68 61 6e 0a 20 20 20 20 20  eaper than.     
34f50 20 20 2a 20 66 75 6c 6c 2d 74 65 78 74 20 73 65    * full-text se
34f51 61 72 63 68 65 73 2e 20 2a 2f 0a 20 20 20 20 20  arches. */.     
34f52 20 70 49 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65   pInfo->estimate
34f53 64 43 6f 73 74 20 3d 20 31 2e 30 3b 20 20 20 0a  dCost = 1.0;   .
34f54 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
34f55 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20  LITE_OK;.    }. 
34f56 20 7d 0a 20 20 70 49 6e 66 6f 2d 3e 69 64 78 4e   }.  pInfo->idxN
34f57 75 6d 20 3d 20 51 55 45 52 59 5f 47 45 4e 45 52  um = QUERY_GENER
34f58 49 43 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  IC;.  return SQL
34f59 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69  ITE_OK;.}..stati
34f5a 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 44 69  c int fulltextDi
34f5b 73 63 6f 6e 6e 65 63 74 28 73 71 6c 69 74 65 33  sconnect(sqlite3
34f5c 5f 76 74 61 62 20 2a 70 56 54 61 62 29 7b 0a 20  _vtab *pVTab){. 
34f5d 20 46 54 53 54 52 41 43 45 28 28 22 46 54 53 33   FTSTRACE(("FTS3
34f5e 20 44 69 73 63 6f 6e 6e 65 63 74 20 25 70 5c 6e   Disconnect %p\n
34f5f 22 2c 20 70 56 54 61 62 29 29 3b 0a 20 20 66 75  ", pVTab));.  fu
34f60 6c 6c 74 65 78 74 5f 76 74 61 62 5f 64 65 73 74  lltext_vtab_dest
34f61 72 6f 79 28 28 66 75 6c 6c 74 65 78 74 5f 76 74  roy((fulltext_vt
34f62 61 62 20 2a 29 70 56 54 61 62 29 3b 0a 20 20 72  ab *)pVTab);.  r
34f63 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
34f64 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .}..static int f
34f65 75 6c 6c 74 65 78 74 44 65 73 74 72 6f 79 28 73  ulltextDestroy(s
34f66 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54  qlite3_vtab *pVT
34f67 61 62 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f  ab){.  fulltext_
34f68 76 74 61 62 20 2a 76 20 3d 20 28 66 75 6c 6c 74  vtab *v = (fullt
34f69 65 78 74 5f 76 74 61 62 20 2a 29 70 56 54 61 62  ext_vtab *)pVTab
34f6a 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 46  ;.  int rc;..  F
34f6b 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20 44  TSTRACE(("FTS3 D
34f6c 65 73 74 72 6f 79 20 25 70 5c 6e 22 2c 20 70 56  estroy %p\n", pV
34f6d 54 61 62 29 29 3b 0a 20 20 72 63 20 3d 20 73 71  Tab));.  rc = sq
34f6e 6c 5f 65 78 65 63 28 76 2d 3e 64 62 2c 20 76 2d  l_exec(v->db, v-
34f6f 3e 7a 44 62 2c 20 76 2d 3e 7a 4e 61 6d 65 2c 0a  >zDb, v->zName,.
34f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34f71 22 64 72 6f 70 20 74 61 62 6c 65 20 69 66 20 65  "drop table if e
34f72 78 69 73 74 73 20 25 5f 63 6f 6e 74 65 6e 74 3b  xists %_content;
34f73 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ".              
34f74 20 20 22 64 72 6f 70 20 74 61 62 6c 65 20 69 66    "drop table if
34f75 20 65 78 69 73 74 73 20 25 5f 73 65 67 6d 65 6e   exists %_segmen
34f76 74 73 3b 22 0a 20 20 20 20 20 20 20 20 20 20 20  ts;".           
34f77 20 20 20 20 20 22 64 72 6f 70 20 74 61 62 6c 65       "drop table
34f78 20 69 66 20 65 78 69 73 74 73 20 25 5f 73 65 67   if exists %_seg
34f79 64 69 72 3b 22 0a 20 20 20 20 20 20 20 20 20 20  dir;".          
34f7a 20 20 20 20 20 20 29 3b 0a 20 20 69 66 28 20 72        );.  if( r
34f7b 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
34f7c 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 66 75 6c  eturn rc;..  ful
34f7d 6c 74 65 78 74 5f 76 74 61 62 5f 64 65 73 74 72  ltext_vtab_destr
34f7e 6f 79 28 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  oy((fulltext_vta
34f7f 62 20 2a 29 70 56 54 61 62 29 3b 0a 20 20 72 65  b *)pVTab);.  re
34f80 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
34f81 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75  }..static int fu
34f82 6c 6c 74 65 78 74 4f 70 65 6e 28 73 71 6c 69 74  lltextOpen(sqlit
34f83 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 2c 20  e3_vtab *pVTab, 
34f84 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
34f85 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 29 7b  sor **ppCursor){
34f86 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73  .  fulltext_curs
34f87 6f 72 20 2a 63 3b 0a 0a 20 20 63 20 3d 20 28 66  or *c;..  c = (f
34f88 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a  ulltext_cursor *
34f89 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  ) sqlite3_malloc
34f8a 28 73 69 7a 65 6f 66 28 66 75 6c 6c 74 65 78 74  (sizeof(fulltext
34f8b 5f 63 75 72 73 6f 72 29 29 3b 0a 20 20 69 66 28  _cursor));.  if(
34f8c 20 63 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74   c ){.    memset
34f8d 28 63 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66 75  (c, 0, sizeof(fu
34f8e 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 29 29 3b  lltext_cursor));
34f8f 0a 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 20 77  .    /* sqlite w
34f90 69 6c 6c 20 69 6e 69 74 69 61 6c 69 7a 65 20 63  ill initialize c
34f91 2d 3e 62 61 73 65 20 2a 2f 0a 20 20 20 20 2a 70  ->base */.    *p
34f92 70 43 75 72 73 6f 72 20 3d 20 26 63 2d 3e 62 61  pCursor = &c->ba
34f93 73 65 3b 0a 20 20 20 20 46 54 53 54 52 41 43 45  se;.    FTSTRACE
34f94 28 28 22 46 54 53 33 20 4f 70 65 6e 20 25 70 3a  (("FTS3 Open %p:
34f95 20 25 70 5c 6e 22 2c 20 70 56 54 61 62 2c 20 63   %p\n", pVTab, c
34f96 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  ));.    return S
34f97 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73  QLITE_OK;.  }els
34f98 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  e{.    return SQ
34f99 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
34f9a 7d 0a 0a 2f 2a 20 46 72 65 65 20 61 6c 6c 20 6f  }../* Free all o
34f9b 66 20 74 68 65 20 64 79 6e 61 6d 69 63 61 6c 6c  f the dynamicall
34f9c 79 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f  y allocated memo
34f9d 72 79 20 68 65 6c 64 20 62 79 20 74 68 65 0a 2a  ry held by the.*
34f9e 2a 20 53 6e 69 70 70 65 74 0a 2a 2f 0a 73 74 61  * Snippet.*/.sta
34f9f 74 69 63 20 76 6f 69 64 20 73 6e 69 70 70 65 74  tic void snippet
34fa0 43 6c 65 61 72 28 53 6e 69 70 70 65 74 20 2a 70  Clear(Snippet *p
34fa1 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  ){.  sqlite3_fre
34fa2 65 28 70 2d 3e 61 4d 61 74 63 68 29 3b 0a 20 20  e(p->aMatch);.  
34fa3 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e  sqlite3_free(p->
34fa4 7a 4f 66 66 73 65 74 29 3b 0a 20 20 73 71 6c 69  zOffset);.  sqli
34fa5 74 65 33 5f 66 72 65 65 28 70 2d 3e 7a 53 6e 69  te3_free(p->zSni
34fa6 70 70 65 74 29 3b 0a 20 20 43 4c 45 41 52 28 70  ppet);.  CLEAR(p
34fa7 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65  );.}../*.** Appe
34fa8 6e 64 20 61 20 73 69 6e 67 6c 65 20 65 6e 74 72  nd a single entr
34fa9 79 20 74 6f 20 74 68 65 20 70 2d 3e 61 4d 61 74  y to the p->aMat
34faa 63 68 5b 5d 20 6c 6f 67 2e 0a 2a 2f 0a 73 74 61  ch[] log..*/.sta
34fab 74 69 63 20 76 6f 69 64 20 73 6e 69 70 70 65 74  tic void snippet
34fac 41 70 70 65 6e 64 4d 61 74 63 68 28 0a 20 20 53  AppendMatch(.  S
34fad 6e 69 70 70 65 74 20 2a 70 2c 20 20 20 20 20 20  nippet *p,      
34fae 20 20 20 20 20 20 20 20 20 2f 2a 20 41 70 70 65           /* Appe
34faf 6e 64 20 74 68 65 20 65 6e 74 72 79 20 74 6f 20  nd the entry to 
34fb0 74 68 69 73 20 73 6e 69 70 70 65 74 20 2a 2f 0a  this snippet */.
34fb1 20 20 69 6e 74 20 69 43 6f 6c 2c 20 69 6e 74 20    int iCol, int 
34fb2 69 54 65 72 6d 2c 20 20 20 20 20 20 2f 2a 20 54  iTerm,      /* T
34fb3 68 65 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 71 75  he column and qu
34fb4 65 72 79 20 74 65 72 6d 20 2a 2f 0a 20 20 69 6e  ery term */.  in
34fb5 74 20 69 54 6f 6b 65 6e 2c 20 20 20 20 20 20 20  t iToken,       
34fb6 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 74 63 68          /* Match
34fb7 69 6e 67 20 74 6f 6b 65 6e 20 69 6e 20 64 6f 63  ing token in doc
34fb8 75 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 69  ument */.  int i
34fb9 53 74 61 72 74 2c 20 69 6e 74 20 6e 42 79 74 65  Start, int nByte
34fba 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 61       /* Offset a
34fbb 6e 64 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6d  nd size of the m
34fbc 61 74 63 68 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  atch */.){.  int
34fbd 20 69 3b 0a 20 20 73 74 72 75 63 74 20 73 6e 69   i;.  struct sni
34fbe 70 70 65 74 4d 61 74 63 68 20 2a 70 4d 61 74 63  ppetMatch *pMatc
34fbf 68 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4d 61 74  h;.  if( p->nMat
34fc0 63 68 2b 31 3e 3d 70 2d 3e 6e 41 6c 6c 6f 63 20  ch+1>=p->nAlloc 
34fc1 29 7b 0a 20 20 20 20 70 2d 3e 6e 41 6c 6c 6f 63  ){.    p->nAlloc
34fc2 20 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 2a 32 20 2b   = p->nAlloc*2 +
34fc3 20 31 30 3b 0a 20 20 20 20 70 2d 3e 61 4d 61 74   10;.    p->aMat
34fc4 63 68 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61  ch = sqlite3_rea
34fc5 6c 6c 6f 63 28 70 2d 3e 61 4d 61 74 63 68 2c 20  lloc(p->aMatch, 
34fc6 70 2d 3e 6e 41 6c 6c 6f 63 2a 73 69 7a 65 6f 66  p->nAlloc*sizeof
34fc7 28 70 2d 3e 61 4d 61 74 63 68 5b 30 5d 29 20 29  (p->aMatch[0]) )
34fc8 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 61 4d 61  ;.    if( p->aMa
34fc9 74 63 68 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  tch==0 ){.      
34fca 70 2d 3e 6e 4d 61 74 63 68 20 3d 20 30 3b 0a 20  p->nMatch = 0;. 
34fcb 20 20 20 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d       p->nAlloc =
34fcc 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   0;.      return
34fcd 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 20  ;.    }.  }.  i 
34fce 3d 20 70 2d 3e 6e 4d 61 74 63 68 2b 2b 3b 0a 20  = p->nMatch++;. 
34fcf 20 70 4d 61 74 63 68 20 3d 20 26 70 2d 3e 61 4d   pMatch = &p->aM
34fd0 61 74 63 68 5b 69 5d 3b 0a 20 20 70 4d 61 74 63  atch[i];.  pMatc
34fd1 68 2d 3e 69 43 6f 6c 20 3d 20 69 43 6f 6c 3b 0a  h->iCol = iCol;.
34fd2 20 20 70 4d 61 74 63 68 2d 3e 69 54 65 72 6d 20    pMatch->iTerm 
34fd3 3d 20 69 54 65 72 6d 3b 0a 20 20 70 4d 61 74 63  = iTerm;.  pMatc
34fd4 68 2d 3e 69 54 6f 6b 65 6e 20 3d 20 69 54 6f 6b  h->iToken = iTok
34fd5 65 6e 3b 0a 20 20 70 4d 61 74 63 68 2d 3e 69 53  en;.  pMatch->iS
34fd6 74 61 72 74 20 3d 20 69 53 74 61 72 74 3b 0a 20  tart = iStart;. 
34fd7 20 70 4d 61 74 63 68 2d 3e 6e 42 79 74 65 20 3d   pMatch->nByte =
34fd8 20 6e 42 79 74 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   nByte;.}../*.**
34fd9 20 53 69 7a 69 6e 67 20 69 6e 66 6f 72 6d 61 74   Sizing informat
34fda 69 6f 6e 20 66 6f 72 20 74 68 65 20 63 69 72 63  ion for the circ
34fdb 75 6c 61 72 20 62 75 66 66 65 72 20 75 73 65 64  ular buffer used
34fdc 20 69 6e 20 73 6e 69 70 70 65 74 4f 66 66 73 65   in snippetOffse
34fdd 74 73 4f 66 43 6f 6c 75 6d 6e 28 29 0a 2a 2f 0a  tsOfColumn().*/.
34fde 23 64 65 66 69 6e 65 20 46 54 53 33 5f 52 4f 54  #define FTS3_ROT
34fdf 4f 52 5f 53 5a 20 20 20 28 33 32 29 0a 23 64 65  OR_SZ   (32).#de
34fe0 66 69 6e 65 20 46 54 53 33 5f 52 4f 54 4f 52 5f  fine FTS3_ROTOR_
34fe1 4d 41 53 4b 20 28 46 54 53 33 5f 52 4f 54 4f 52  MASK (FTS3_ROTOR
34fe2 5f 53 5a 2d 31 29 0a 0a 2f 2a 0a 2a 2a 20 46 75  _SZ-1)../*.** Fu
34fe3 6e 63 74 69 6f 6e 20 74 6f 20 69 74 65 72 61 74  nction to iterat
34fe4 65 20 74 68 72 6f 75 67 68 20 74 68 65 20 74 6f  e through the to
34fe5 6b 65 6e 73 20 6f 66 20 61 20 63 6f 6d 70 69 6c  kens of a compil
34fe6 65 64 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a  ed expression..*
34fe7 2a 0a 2a 2a 20 45 78 63 65 70 74 2c 20 73 6b 69  *.** Except, ski
34fe8 70 20 61 6c 6c 20 74 6f 6b 65 6e 73 20 6f 6e 20  p all tokens on 
34fe9 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73  the right-hand s
34fea 69 64 65 20 6f 66 20 61 20 4e 4f 54 20 6f 70 65  ide of a NOT ope
34feb 72 61 74 6f 72 2e 0a 2a 2a 20 54 68 69 73 20 66  rator..** This f
34fec 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  unction is used 
34fed 74 6f 20 66 69 6e 64 20 74 6f 6b 65 6e 73 20 61  to find tokens a
34fee 73 20 70 61 72 74 20 6f 66 20 73 6e 69 70 70 65  s part of snippe
34fef 74 20 61 6e 64 20 6f 66 66 73 65 74 0a 2a 2a 20  t and offset.** 
34ff0 67 65 6e 65 72 61 74 69 6f 6e 20 61 6e 64 20 77  generation and w
34ff1 65 20 64 6f 20 6e 74 20 77 61 6e 74 20 73 6e 69  e do nt want sni
34ff2 70 70 65 74 73 20 61 6e 64 20 6f 66 66 73 65 74  ppets and offset
34ff3 73 20 74 6f 20 72 65 70 6f 72 74 20 6d 61 74 63  s to report matc
34ff4 68 65 73 0a 2a 2a 20 66 6f 72 20 74 6f 6b 65 6e  hes.** for token
34ff5 73 20 6f 6e 20 74 68 65 20 52 48 53 20 6f 66 20  s on the RHS of 
34ff6 61 20 4e 4f 54 2e 0a 2a 2f 0a 73 74 61 74 69 63  a NOT..*/.static
34ff7 20 69 6e 74 20 66 74 73 33 4e 65 78 74 45 78 70   int fts3NextExp
34ff8 72 54 6f 6b 65 6e 28 46 74 73 33 45 78 70 72 20  rToken(Fts3Expr 
34ff9 2a 2a 70 70 45 78 70 72 2c 20 69 6e 74 20 2a 70  **ppExpr, int *p
34ffa 69 54 6f 6b 65 6e 29 7b 0a 20 20 46 74 73 33 45  iToken){.  Fts3E
34ffb 78 70 72 20 2a 70 20 3d 20 2a 70 70 45 78 70 72  xpr *p = *ppExpr
34ffc 3b 0a 20 20 69 6e 74 20 69 54 6f 6b 65 6e 20 3d  ;.  int iToken =
34ffd 20 2a 70 69 54 6f 6b 65 6e 3b 0a 20 20 69 66 28   *piToken;.  if(
34ffe 20 69 54 6f 6b 65 6e 3c 30 20 29 7b 0a 20 20 20   iToken<0 ){.   
34fff 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65   /* In this case
35000 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
35001 70 20 69 73 20 74 68 65 20 72 6f 6f 74 20 6f 66  p is the root of
35002 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74   an expression t
35003 72 65 65 2e 0a 20 20 20 20 2a 2a 20 4d 6f 76 65  ree..    ** Move
35004 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 74 6f   to the first to
35005 6b 65 6e 20 69 6e 20 74 68 65 20 65 78 70 72 65  ken in the expre
35006 73 73 69 6f 6e 20 74 72 65 65 2e 0a 20 20 20 20  ssion tree..    
35007 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 70 2d  */.    while( p-
35008 3e 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20  >pLeft ){.      
35009 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20 20  p = p->pLeft;.  
3500a 20 20 7d 0a 20 20 20 20 69 54 6f 6b 65 6e 20 3d    }.    iToken =
3500b 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
3500c 20 61 73 73 65 72 74 28 70 20 26 26 20 70 2d 3e   assert(p && p->
3500d 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f  eType==FTSQUERY_
3500e 50 48 52 41 53 45 20 29 3b 0a 20 20 20 20 69 66  PHRASE );.    if
3500f 28 20 69 54 6f 6b 65 6e 3c 28 70 2d 3e 70 50 68  ( iToken<(p->pPh
35010 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 2d 31 29 20  rase->nToken-1) 
35011 29 7b 0a 20 20 20 20 20 20 69 54 6f 6b 65 6e 2b  ){.      iToken+
35012 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  +;.    }else{.  
35013 20 20 20 20 69 54 6f 6b 65 6e 20 3d 20 30 3b 0a      iToken = 0;.
35014 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 2d 3e        while( p->
35015 70 50 61 72 65 6e 74 20 26 26 20 70 2d 3e 70 50  pParent && p->pP
35016 61 72 65 6e 74 2d 3e 70 4c 65 66 74 21 3d 70 20  arent->pLeft!=p 
35017 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ){.        asser
35018 74 28 20 70 2d 3e 70 50 61 72 65 6e 74 2d 3e 70  t( p->pParent->p
35019 52 69 67 68 74 3d 3d 70 20 29 3b 0a 20 20 20 20  Right==p );.    
3501a 20 20 20 20 70 20 3d 20 70 2d 3e 70 50 61 72 65      p = p->pPare
3501b 6e 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  nt;.      }.    
3501c 20 20 70 20 3d 20 70 2d 3e 70 50 61 72 65 6e 74    p = p->pParent
3501d 3b 0a 20 20 20 20 20 20 69 66 28 20 70 20 29 7b  ;.      if( p ){
3501e 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
3501f 20 70 2d 3e 70 52 69 67 68 74 21 3d 30 20 29 3b   p->pRight!=0 );
35020 0a 20 20 20 20 20 20 20 20 70 20 3d 20 70 2d 3e  .        p = p->
35021 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 20 20  pRight;.        
35022 77 68 69 6c 65 28 20 70 2d 3e 70 4c 65 66 74 20  while( p->pLeft 
35023 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 20 3d  ){.          p =
35024 20 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20   p->pLeft;.     
35025 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
35026 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 70 45 78 70   }.  }..  *ppExp
35027 72 20 3d 20 70 3b 0a 20 20 2a 70 69 54 6f 6b 65  r = p;.  *piToke
35028 6e 20 3d 20 69 54 6f 6b 65 6e 3b 0a 20 20 72 65  n = iToken;.  re
35029 74 75 72 6e 20 70 3f 31 3a 30 3b 0a 7d 0a 0a 2f  turn p?1:0;.}../
3502a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45  *.** Return TRUE
3502b 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69   if the expressi
3502c 6f 6e 20 6e 6f 64 65 20 70 45 78 70 72 20 69 73  on node pExpr is
3502d 20 6c 6f 63 61 74 65 64 20 62 65 6e 65 61 74 68   located beneath
3502e 20 74 68 65 0a 2a 2a 20 52 48 53 20 6f 66 20 61   the.** RHS of a
3502f 20 4e 4f 54 20 6f 70 65 72 61 74 6f 72 2e 0a 2a   NOT operator..*
35030 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
35031 33 45 78 70 72 42 65 6e 65 61 74 68 4e 6f 74 28  3ExprBeneathNot(
35032 46 74 73 33 45 78 70 72 20 2a 70 29 7b 0a 20 20  Fts3Expr *p){.  
35033 46 74 73 33 45 78 70 72 20 2a 70 50 61 72 65 6e  Fts3Expr *pParen
35034 74 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b  t;.  while( p ){
35035 0a 20 20 20 20 70 50 61 72 65 6e 74 20 3d 20 70  .    pParent = p
35036 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20 20 20 69  ->pParent;.    i
35037 66 28 20 70 50 61 72 65 6e 74 20 26 26 20 70 50  f( pParent && pP
35038 61 72 65 6e 74 2d 3e 65 54 79 70 65 3d 3d 46 54  arent->eType==FT
35039 53 51 55 45 52 59 5f 4e 4f 54 20 26 26 20 70 50  SQUERY_NOT && pP
3503a 61 72 65 6e 74 2d 3e 70 52 69 67 68 74 3d 3d 70  arent->pRight==p
3503b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
3503c 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 20   1;.    }.    p 
3503d 3d 20 70 50 61 72 65 6e 74 3b 0a 20 20 7d 0a 20  = pParent;.  }. 
3503e 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
3503f 0a 2a 2a 20 41 64 64 20 65 6e 74 72 69 65 73 20  .** Add entries 
35040 74 6f 20 70 53 6e 69 70 70 65 74 2d 3e 61 4d 61  to pSnippet->aMa
35041 74 63 68 5b 5d 20 66 6f 72 20 65 76 65 72 79 20  tch[] for every 
35042 6d 61 74 63 68 20 74 68 61 74 20 6f 63 63 75 72  match that occur
35043 73 20 61 67 61 69 6e 73 74 0a 2a 2a 20 64 6f 63  s against.** doc
35044 75 6d 65 6e 74 20 7a 44 6f 63 5b 30 2e 2e 6e 44  ument zDoc[0..nD
35045 6f 63 2d 31 5d 20 77 68 69 63 68 20 69 73 20 73  oc-1] which is s
35046 74 6f 72 65 64 20 69 6e 20 63 6f 6c 75 6d 6e 20  tored in column 
35047 69 43 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 73 74 61 74  iColumn..*/.stat
35048 69 63 20 76 6f 69 64 20 73 6e 69 70 70 65 74 4f  ic void snippetO
35049 66 66 73 65 74 73 4f 66 43 6f 6c 75 6d 6e 28 0a  ffsetsOfColumn(.
3504a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f    fulltext_curso
3504b 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 20 20  r *pCur,        
3504c 20 2f 2a 20 54 68 65 20 66 75 6c 6c 74 65 73 74   /* The fulltest
3504d 20 73 65 61 72 63 68 20 63 75 72 73 6f 72 20 2a   search cursor *
3504e 2f 0a 20 20 53 6e 69 70 70 65 74 20 2a 70 53 6e  /.  Snippet *pSn
3504f 69 70 70 65 74 2c 20 20 20 20 20 20 20 20 20 20  ippet,          
35050 20 20 20 2f 2a 20 54 68 65 20 53 6e 69 70 70 65     /* The Snippe
35051 74 20 6f 62 6a 65 63 74 20 74 6f 20 62 65 20 66  t object to be f
35052 69 6c 6c 65 64 20 69 6e 20 2a 2f 0a 20 20 69 6e  illed in */.  in
35053 74 20 69 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20  t iColumn,      
35054 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
35055 49 6e 64 65 78 20 6f 66 20 66 75 6c 6c 74 65 78  Index of fulltex
35056 74 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 2a  t table column *
35057 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
35058 7a 44 6f 63 2c 20 20 20 20 20 20 20 20 20 20 20  zDoc,           
35059 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68     /* Text of th
3505a 65 20 66 75 6c 6c 74 65 78 74 20 74 61 62 6c 65  e fulltext table
3505b 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74   column */.  int
3505c 20 6e 44 6f 63 20 20 20 20 20 20 20 20 20 20 20   nDoc           
3505d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
3505e 65 6e 67 74 68 20 6f 66 20 7a 44 6f 63 20 69 6e  ength of zDoc in
3505f 20 62 79 74 65 73 20 2a 2f 0a 29 7b 0a 20 20 63   bytes */.){.  c
35060 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b  onst sqlite3_tok
35061 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70  enizer_module *p
35062 54 4d 6f 64 75 6c 65 3b 20 20 2f 2a 20 54 68 65  TModule;  /* The
35063 20 74 6f 6b 65 6e 69 7a 65 72 20 6d 6f 64 75 6c   tokenizer modul
35064 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74  e */.  sqlite3_t
35065 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e  okenizer *pToken
35066 69 7a 65 72 3b 20 20 20 20 20 20 20 20 20 20 20  izer;           
35067 20 20 2f 2a 20 54 68 65 20 73 70 65 63 69 66 69    /* The specifi
35068 63 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 20  c tokenizer */. 
35069 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
3506a 65 72 5f 63 75 72 73 6f 72 20 2a 70 54 43 75 72  er_cursor *pTCur
3506b 73 6f 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  sor;        /* T
3506c 6f 6b 65 6e 69 7a 65 72 20 63 75 72 73 6f 72 20  okenizer cursor 
3506d 2a 2f 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74  */.  fulltext_vt
3506e 61 62 20 2a 70 56 74 61 62 3b 20 20 20 20 20 20  ab *pVtab;      
3506f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
35070 20 66 75 6c 6c 20 74 65 78 74 20 69 6e 64 65 78   full text index
35071 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d   */.  int nColum
35072 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
35073 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
35074 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
35075 69 6e 20 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a  in the index */.
35076 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20    int i, j;     
35077 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35078 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
35079 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74  ounters */.  int
3507a 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
3507b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3507c 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
3507d 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69   */.  unsigned i
3507e 6e 74 20 6d 61 74 63 68 2c 20 70 72 65 76 4d 61  nt match, prevMa
3507f 74 63 68 3b 20 20 20 20 20 20 20 2f 2a 20 50 68  tch;       /* Ph
35080 72 61 73 65 20 73 65 61 72 63 68 20 62 69 74 6d  rase search bitm
35081 61 73 6b 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  asks */.  const 
35082 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 3b 20 20 20  char *zToken;   
35083 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
35084 2a 20 4e 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f  * Next token fro
35085 6d 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20  m the tokenizer 
35086 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b  */.  int nToken;
35087 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35088 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
35089 65 20 6f 66 20 7a 54 6f 6b 65 6e 20 2a 2f 0a 20  e of zToken */. 
3508a 20 69 6e 74 20 69 42 65 67 69 6e 2c 20 69 45 6e   int iBegin, iEn
3508b 64 2c 20 69 50 6f 73 3b 20 20 20 20 20 20 20 20  d, iPos;        
3508c 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 73        /* Offsets
3508d 20 6f 66 20 62 65 67 69 6e 6e 69 6e 67 20 61 6e   of beginning an
3508e 64 20 65 6e 64 20 2a 2f 0a 0a 20 20 2f 2a 20 54  d end */..  /* T
3508f 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72  he following var
35090 69 61 62 6c 65 73 20 6b 65 65 70 20 61 20 63 69  iables keep a ci
35091 72 63 75 6c 61 72 20 62 75 66 66 65 72 20 6f 66  rcular buffer of
35092 20 74 68 65 20 6c 61 73 74 0a 20 20 2a 2a 20 66   the last.  ** f
35093 65 77 20 74 6f 6b 65 6e 73 20 2a 2f 0a 20 20 75  ew tokens */.  u
35094 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 52 6f 74  nsigned int iRot
35095 6f 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  or = 0;         
35096 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
35097 63 75 72 72 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f  current token */
35098 0a 20 20 69 6e 74 20 69 52 6f 74 6f 72 42 65 67  .  int iRotorBeg
35099 69 6e 5b 46 54 53 33 5f 52 4f 54 4f 52 5f 53 5a  in[FTS3_ROTOR_SZ
3509a 5d 3b 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e  ];      /* Begin
3509b 6e 69 6e 67 20 6f 66 66 73 65 74 20 6f 66 20 74  ning offset of t
3509c 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 52  oken */.  int iR
3509d 6f 74 6f 72 4c 65 6e 5b 46 54 53 33 5f 52 4f 54  otorLen[FTS3_ROT
3509e 4f 52 5f 53 5a 5d 3b 20 20 20 20 20 20 20 20 2f  OR_SZ];        /
3509f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 6f 6b 65  * Length of toke
350a0 6e 20 2a 2f 0a 0a 20 20 70 56 74 61 62 20 3d 20  n */..  pVtab = 
350a1 63 75 72 73 6f 72 5f 76 74 61 62 28 70 43 75 72  cursor_vtab(pCur
350a2 29 3b 0a 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20 70  );.  nColumn = p
350a3 56 74 61 62 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20  Vtab->nColumn;. 
350a4 20 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 56   pTokenizer = pV
350a5 74 61 62 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 3b  tab->pTokenizer;
350a6 0a 20 20 70 54 4d 6f 64 75 6c 65 20 3d 20 70 54  .  pTModule = pT
350a7 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c  okenizer->pModul
350a8 65 3b 0a 20 20 72 63 20 3d 20 70 54 4d 6f 64 75  e;.  rc = pTModu
350a9 6c 65 2d 3e 78 4f 70 65 6e 28 70 54 6f 6b 65 6e  le->xOpen(pToken
350aa 69 7a 65 72 2c 20 7a 44 6f 63 2c 20 6e 44 6f 63  izer, zDoc, nDoc
350ab 2c 20 26 70 54 43 75 72 73 6f 72 29 3b 0a 20 20  , &pTCursor);.  
350ac 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 3b  if( rc ) return;
350ad 0a 20 20 70 54 43 75 72 73 6f 72 2d 3e 70 54 6f  .  pTCursor->pTo
350ae 6b 65 6e 69 7a 65 72 20 3d 20 70 54 6f 6b 65 6e  kenizer = pToken
350af 69 7a 65 72 3b 0a 0a 20 20 70 72 65 76 4d 61 74  izer;..  prevMat
350b0 63 68 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28  ch = 0;.  while(
350b1 20 21 70 54 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78   !pTModule->xNex
350b2 74 28 70 54 43 75 72 73 6f 72 2c 20 26 7a 54 6f  t(pTCursor, &zTo
350b3 6b 65 6e 2c 20 26 6e 54 6f 6b 65 6e 2c 20 26 69  ken, &nToken, &i
350b4 42 65 67 69 6e 2c 20 26 69 45 6e 64 2c 20 26 69  Begin, &iEnd, &i
350b5 50 6f 73 29 20 29 7b 0a 20 20 20 20 46 74 73 33  Pos) ){.    Fts3
350b6 45 78 70 72 20 2a 70 49 74 65 72 20 3d 20 70 43  Expr *pIter = pC
350b7 75 72 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 69  ur->pExpr;.    i
350b8 6e 74 20 69 49 74 65 72 20 3d 20 2d 31 3b 0a 20  nt iIter = -1;. 
350b9 20 20 20 69 52 6f 74 6f 72 42 65 67 69 6e 5b 69     iRotorBegin[i
350ba 52 6f 74 6f 72 26 46 54 53 33 5f 52 4f 54 4f 52  Rotor&FTS3_ROTOR
350bb 5f 4d 41 53 4b 5d 20 3d 20 69 42 65 67 69 6e 3b  _MASK] = iBegin;
350bc 0a 20 20 20 20 69 52 6f 74 6f 72 4c 65 6e 5b 69  .    iRotorLen[i
350bd 52 6f 74 6f 72 26 46 54 53 33 5f 52 4f 54 4f 52  Rotor&FTS3_ROTOR
350be 5f 4d 41 53 4b 5d 20 3d 20 69 45 6e 64 2d 69 42  _MASK] = iEnd-iB
350bf 65 67 69 6e 3b 0a 20 20 20 20 6d 61 74 63 68 20  egin;.    match 
350c0 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  = 0;.    for(i=0
350c1 3b 20 69 3c 28 46 54 53 33 5f 52 4f 54 4f 52 5f  ; i<(FTS3_ROTOR_
350c2 53 5a 2d 31 29 20 26 26 20 66 74 73 33 4e 65 78  SZ-1) && fts3Nex
350c3 74 45 78 70 72 54 6f 6b 65 6e 28 26 70 49 74 65  tExprToken(&pIte
350c4 72 2c 20 26 69 49 74 65 72 29 3b 20 69 2b 2b 29  r, &iIter); i++)
350c5 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 50 68 72  {.      int nPhr
350c6 61 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ase;            
350c7 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
350c8 72 20 6f 66 20 74 6f 6b 65 6e 73 20 69 6e 20 63  r of tokens in c
350c9 75 72 72 65 6e 74 20 70 68 72 61 73 65 20 2a 2f  urrent phrase */
350ca 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 50 68  .      struct Ph
350cb 72 61 73 65 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65  raseToken *pToke
350cc 6e 3b 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e  n;     /* Curren
350cd 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20 20  t token */.     
350ce 20 69 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20   int iCol;      
350cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
350d0 20 2f 2a 20 43 6f 6c 75 6d 6e 20 69 6e 64 65 78   /* Column index
350d1 20 2a 2f 0a 0a 20 20 20 20 20 20 69 66 28 20 66   */..      if( f
350d2 74 73 33 45 78 70 72 42 65 6e 65 61 74 68 4e 6f  ts3ExprBeneathNo
350d3 74 28 70 49 74 65 72 29 20 29 20 63 6f 6e 74 69  t(pIter) ) conti
350d4 6e 75 65 3b 0a 20 20 20 20 20 20 6e 50 68 72 61  nue;.      nPhra
350d5 73 65 20 3d 20 70 49 74 65 72 2d 3e 70 50 68 72  se = pIter->pPhr
350d6 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 0a 20 20 20  ase->nToken;.   
350d7 20 20 20 70 54 6f 6b 65 6e 20 3d 20 26 70 49 74     pToken = &pIt
350d8 65 72 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f  er->pPhrase->aTo
350d9 6b 65 6e 5b 69 49 74 65 72 5d 3b 0a 20 20 20 20  ken[iIter];.    
350da 20 20 69 43 6f 6c 20 3d 20 70 49 74 65 72 2d 3e    iCol = pIter->
350db 70 50 68 72 61 73 65 2d 3e 69 43 6f 6c 75 6d 6e  pPhrase->iColumn
350dc 3b 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c  ;.      if( iCol
350dd 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 6e 43 6f 6c  >=0 && iCol<nCol
350de 75 6d 6e 20 26 26 20 69 43 6f 6c 21 3d 69 43 6f  umn && iCol!=iCo
350df 6c 75 6d 6e 20 29 20 63 6f 6e 74 69 6e 75 65 3b  lumn ) continue;
350e0 0a 20 20 20 20 20 20 69 66 28 20 70 54 6f 6b 65  .      if( pToke
350e1 6e 2d 3e 6e 3e 6e 54 6f 6b 65 6e 20 29 20 63 6f  n->n>nToken ) co
350e2 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 69 66  ntinue;.      if
350e3 28 20 21 70 54 6f 6b 65 6e 2d 3e 69 73 50 72 65  ( !pToken->isPre
350e4 66 69 78 20 26 26 20 70 54 6f 6b 65 6e 2d 3e 6e  fix && pToken->n
350e5 3c 6e 54 6f 6b 65 6e 20 29 20 63 6f 6e 74 69 6e  <nToken ) contin
350e6 75 65 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  ue;.      assert
350e7 28 20 70 54 6f 6b 65 6e 2d 3e 6e 3c 3d 6e 54 6f  ( pToken->n<=nTo
350e8 6b 65 6e 20 29 3b 0a 20 20 20 20 20 20 69 66 28  ken );.      if(
350e9 20 6d 65 6d 63 6d 70 28 70 54 6f 6b 65 6e 2d 3e   memcmp(pToken->
350ea 7a 2c 20 7a 54 6f 6b 65 6e 2c 20 70 54 6f 6b 65  z, zToken, pToke
350eb 6e 2d 3e 6e 29 20 29 20 63 6f 6e 74 69 6e 75 65  n->n) ) continue
350ec 3b 0a 20 20 20 20 20 20 69 66 28 20 69 49 74 65  ;.      if( iIte
350ed 72 3e 30 20 26 26 20 28 70 72 65 76 4d 61 74 63  r>0 && (prevMatc
350ee 68 20 26 20 28 31 3c 3c 69 29 29 3d 3d 30 20 29  h & (1<<i))==0 )
350ef 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
350f0 20 6d 61 74 63 68 20 7c 3d 20 31 3c 3c 69 3b 0a   match |= 1<<i;.
350f1 20 20 20 20 20 20 69 66 28 20 69 3d 3d 28 46 54        if( i==(FT
350f2 53 33 5f 52 4f 54 4f 52 5f 53 5a 2d 32 29 20 7c  S3_ROTOR_SZ-2) |
350f3 7c 20 6e 50 68 72 61 73 65 3d 3d 69 49 74 65 72  | nPhrase==iIter
350f4 2b 31 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6f  +1 ){.        fo
350f5 72 28 6a 3d 6e 50 68 72 61 73 65 2d 31 3b 20 6a  r(j=nPhrase-1; j
350f6 3e 3d 30 3b 20 6a 2d 2d 29 7b 0a 20 20 20 20 20  >=0; j--){.     
350f7 20 20 20 20 20 69 6e 74 20 6b 20 3d 20 28 69 52       int k = (iR
350f8 6f 74 6f 72 2d 6a 29 20 26 20 46 54 53 33 5f 52  otor-j) & FTS3_R
350f9 4f 54 4f 52 5f 4d 41 53 4b 3b 0a 20 20 20 20 20  OTOR_MASK;.     
350fa 20 20 20 20 20 73 6e 69 70 70 65 74 41 70 70 65       snippetAppe
350fb 6e 64 4d 61 74 63 68 28 70 53 6e 69 70 70 65 74  ndMatch(pSnippet
350fc 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 2d 6a 2c 20  , iColumn, i-j, 
350fd 69 50 6f 73 2d 6a 2c 0a 20 20 20 20 20 20 20 20  iPos-j,.        
350fe 20 20 20 20 20 20 20 20 69 52 6f 74 6f 72 42 65          iRotorBe
350ff 67 69 6e 5b 6b 5d 2c 20 69 52 6f 74 6f 72 4c 65  gin[k], iRotorLe
35100 6e 5b 6b 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d  n[k]);.        }
35101 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
35102 20 20 20 70 72 65 76 4d 61 74 63 68 20 3d 20 6d     prevMatch = m
35103 61 74 63 68 3c 3c 31 3b 0a 20 20 20 20 69 52 6f  atch<<1;.    iRo
35104 74 6f 72 2b 2b 3b 0a 20 20 7d 0a 20 20 70 54 4d  tor++;.  }.  pTM
35105 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 54  odule->xClose(pT
35106 43 75 72 73 6f 72 29 3b 20 20 0a 7d 0a 0a 2f 2a  Cursor);  .}../*
35107 0a 2a 2a 20 52 65 6d 6f 76 65 20 65 6e 74 72 69  .** Remove entri
35108 65 73 20 66 72 6f 6d 20 74 68 65 20 70 53 6e 69  es from the pSni
35109 70 70 65 74 20 73 74 72 75 63 74 75 72 65 20 74  ppet structure t
3510a 6f 20 61 63 63 6f 75 6e 74 20 66 6f 72 20 74 68  o account for th
3510b 65 20 4e 45 41 52 0a 2a 2a 20 6f 70 65 72 61 74  e NEAR.** operat
3510c 6f 72 2e 20 57 68 65 6e 20 74 68 69 73 20 69 73  or. When this is
3510d 20 63 61 6c 6c 65 64 2c 20 70 53 6e 69 70 70 65   called, pSnippe
3510e 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6c  t contains the l
3510f 69 73 74 20 6f 66 20 74 6f 6b 65 6e 20 0a 2a 2a  ist of token .**
35110 20 6f 66 66 73 65 74 73 20 70 72 6f 64 75 63 65   offsets produce
35111 64 20 62 79 20 74 72 65 61 74 69 6e 67 20 61 6c  d by treating al
35112 6c 20 4e 45 41 52 20 6f 70 65 72 61 74 6f 72 73  l NEAR operators
35113 20 61 73 20 41 4e 44 20 6f 70 65 72 61 74 6f 72   as AND operator
35114 73 2e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  s..** This funct
35115 69 6f 6e 20 72 65 6d 6f 76 65 73 20 61 6e 79 20  ion removes any 
35116 65 6e 74 72 69 65 73 20 74 68 61 74 20 73 68 6f  entries that sho
35117 75 6c 64 20 6e 6f 74 20 62 65 20 70 72 65 73 65  uld not be prese
35118 6e 74 20 61 66 74 65 72 0a 2a 2a 20 61 63 63 6f  nt after.** acco
35119 75 6e 74 69 6e 67 20 66 6f 72 20 74 68 65 20 4e  unting for the N
3511a 45 41 52 20 72 65 73 74 72 69 63 74 69 6f 6e 2e  EAR restriction.
3511b 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66   For example, if
3511c 20 74 68 65 20 71 75 65 72 69 65 64 0a 2a 2a 20   the queried.** 
3511d 64 6f 63 75 6d 65 6e 74 20 69 73 3a 0a 2a 2a 0a  document is:.**.
3511e 2a 2a 20 20 20 20 20 22 41 20 42 20 43 20 44 20  **     "A B C D 
3511f 45 20 41 22 0a 2a 2a 0a 2a 2a 20 61 6e 64 20 74  E A".**.** and t
35120 68 65 20 71 75 65 72 79 20 69 73 3a 0a 2a 2a 20  he query is:.** 
35121 0a 2a 2a 20 20 20 20 20 41 20 4e 45 41 52 2f 30  .**     A NEAR/0
35122 20 45 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 77 68   E.**.** then wh
35123 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
35124 20 69 73 20 63 61 6c 6c 65 64 20 74 68 65 20 53   is called the S
35125 6e 69 70 70 65 74 20 63 6f 6e 74 61 69 6e 73 20  nippet contains 
35126 74 6f 6b 65 6e 20 6f 66 66 73 65 74 73 0a 2a 2a  token offsets.**
35127 20 30 2c 20 34 20 61 6e 64 20 35 2e 20 54 68 69   0, 4 and 5. Thi
35128 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 6d 6f 76  s function remov
35129 65 73 20 74 68 65 20 22 30 22 20 65 6e 74 72 79  es the "0" entry
3512a 20 28 62 65 63 61 75 73 65 20 74 68 65 20 66 69   (because the fi
3512b 72 73 74 20 41 0a 2a 2a 20 69 73 20 6e 6f 74 20  rst A.** is not 
3512c 6e 65 61 72 20 65 6e 6f 75 67 68 20 74 6f 20 61  near enough to a
3512d 6e 20 45 29 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  n E)..**.** When
3512e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
3512f 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 76 61  s called, the va
35130 6c 75 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  lue pointed to b
35131 79 20 70 61 72 61 6d 65 74 65 72 20 70 69 4c 65  y parameter piLe
35132 66 74 20 69 73 0a 2a 2a 20 74 68 65 20 69 6e 74  ft is.** the int
35133 65 67 65 72 20 69 64 20 6f 66 20 74 68 65 20 6c  eger id of the l
35134 65 66 74 2d 6d 6f 73 74 20 74 6f 6b 65 6e 20 69  eft-most token i
35135 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  n the expression
35136 20 74 72 65 65 20 68 65 61 64 65 64 20 62 79 0a   tree headed by.
35137 2a 2a 20 70 45 78 70 72 2e 20 54 68 69 73 20 66  ** pExpr. This f
35138 75 6e 63 74 69 6f 6e 20 69 6e 63 72 65 6d 65 6e  unction incremen
35139 74 73 20 2a 70 69 4c 65 66 74 20 62 79 20 74 68  ts *piLeft by th
3513a 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  e total number o
3513b 66 20 74 6f 6b 65 6e 73 0a 2a 2a 20 69 6e 20 74  f tokens.** in t
3513c 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72  he expression tr
3513d 65 65 20 68 65 61 64 65 64 20 62 79 20 70 45 78  ee headed by pEx
3513e 70 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  pr..**.** Return
3513f 20 31 20 69 66 20 61 6e 79 20 74 72 69 6d 6d 69   1 if any trimmi
35140 6e 67 20 6f 63 63 75 72 73 2e 20 20 52 65 74 75  ng occurs.  Retu
35141 72 6e 20 30 20 69 66 20 6e 6f 20 74 72 69 6d 6d  rn 0 if no trimm
35142 69 6e 67 20 69 73 20 72 65 71 75 69 72 65 64 2e  ing is required.
35143 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 74  .*/.static int t
35144 72 69 6d 53 6e 69 70 70 65 74 4f 66 66 73 65 74  rimSnippetOffset
35145 73 28 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70  s(.  Fts3Expr *p
35146 45 78 70 72 2c 20 20 20 20 20 20 2f 2a 20 54 68  Expr,      /* Th
35147 65 20 73 65 61 72 63 68 20 65 78 70 72 65 73 73  e search express
35148 69 6f 6e 20 2a 2f 0a 20 20 53 6e 69 70 70 65 74  ion */.  Snippet
35149 20 2a 70 53 6e 69 70 70 65 74 2c 20 20 20 20 2f   *pSnippet,    /
3514a 2a 20 54 68 65 20 73 65 74 20 6f 66 20 73 6e 69  * The set of sni
3514b 70 70 65 74 20 6f 66 66 73 65 74 73 20 74 6f 20  ppet offsets to 
3514c 62 65 20 74 72 69 6d 6d 65 64 20 2a 2f 0a 20 20  be trimmed */.  
3514d 69 6e 74 20 2a 70 69 4c 65 66 74 20 20 20 20 20  int *piLeft     
3514e 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
3514f 66 20 6c 65 66 74 2d 6d 6f 73 74 20 74 6f 6b 65  f left-most toke
35150 6e 20 69 6e 20 70 45 78 70 72 20 2a 2f 0a 29 7b  n in pExpr */.){
35151 0a 20 20 69 66 28 20 70 45 78 70 72 20 29 7b 0a  .  if( pExpr ){.
35152 20 20 20 20 69 66 28 20 74 72 69 6d 53 6e 69 70      if( trimSnip
35153 70 65 74 4f 66 66 73 65 74 73 28 70 45 78 70 72  petOffsets(pExpr
35154 2d 3e 70 4c 65 66 74 2c 20 70 53 6e 69 70 70 65  ->pLeft, pSnippe
35155 74 2c 20 70 69 4c 65 66 74 29 20 29 7b 0a 20 20  t, piLeft) ){.  
35156 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
35157 20 20 7d 0a 0a 20 20 20 20 73 77 69 74 63 68 28    }..    switch(
35158 20 70 45 78 70 72 2d 3e 65 54 79 70 65 20 29 7b   pExpr->eType ){
35159 0a 20 20 20 20 20 20 63 61 73 65 20 46 54 53 51  .      case FTSQ
3515a 55 45 52 59 5f 50 48 52 41 53 45 3a 0a 20 20 20  UERY_PHRASE:.   
3515b 20 20 20 20 20 2a 70 69 4c 65 66 74 20 2b 3d 20       *piLeft += 
3515c 70 45 78 70 72 2d 3e 70 50 68 72 61 73 65 2d 3e  pExpr->pPhrase->
3515d 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20  nToken;.        
3515e 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3515f 65 20 46 54 53 51 55 45 52 59 5f 4e 45 41 52 3a  e FTSQUERY_NEAR:
35160 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68   {.        /* Th
35161 65 20 72 69 67 68 74 2d 68 61 6e 64 2d 73 69 64  e right-hand-sid
35162 65 20 6f 66 20 61 20 4e 45 41 52 20 6f 70 65 72  e of a NEAR oper
35163 61 74 6f 72 20 69 73 20 61 6c 77 61 79 73 20 61  ator is always a
35164 20 70 68 72 61 73 65 2e 20 54 68 65 0a 20 20 20   phrase. The.   
35165 20 20 20 20 20 2a 2a 20 6c 65 66 74 2d 68 61 6e       ** left-han
35166 64 2d 73 69 64 65 20 69 73 20 65 69 74 68 65 72  d-side is either
35167 20 61 20 70 68 72 61 73 65 20 6f 72 20 61 6e 20   a phrase or an 
35168 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20  expression tree 
35169 74 68 61 74 20 69 73 20 0a 20 20 20 20 20 20 20  that is .       
3516a 20 2a 2a 20 69 74 73 65 6c 66 20 68 65 61 64 65   ** itself heade
3516b 64 20 62 79 20 61 20 4e 45 41 52 20 6f 70 65 72  d by a NEAR oper
3516c 61 74 6f 72 2e 20 54 68 65 20 66 6f 6c 6c 6f 77  ator. The follow
3516d 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  ing initializati
3516e 6f 6e 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 73  ons.        ** s
3516f 65 74 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  et local variabl
35170 65 20 69 4c 65 66 74 20 74 6f 20 74 68 65 20 74  e iLeft to the t
35171 6f 6b 65 6e 20 6e 75 6d 62 65 72 20 6f 66 20 74  oken number of t
35172 68 65 20 6c 65 66 74 2d 6d 6f 73 74 0a 20 20 20  he left-most.   
35173 20 20 20 20 20 2a 2a 20 74 6f 6b 65 6e 20 69 6e       ** token in
35174 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20   the right-hand 
35175 70 68 72 61 73 65 2c 20 61 6e 64 20 69 52 69 67  phrase, and iRig
35176 68 74 20 74 6f 20 74 68 65 20 72 69 67 68 74 20  ht to the right 
35177 6d 6f 73 74 0a 20 20 20 20 20 20 20 20 2a 2a 20  most.        ** 
35178 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20 73 61 6d  token in the sam
35179 65 20 70 68 72 61 73 65 2e 20 46 6f 72 20 65 78  e phrase. For ex
3517a 61 6d 70 6c 65 2c 20 69 66 20 77 65 20 68 61 64  ample, if we had
3517b 3a 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20  :.        **.   
3517c 20 20 20 20 20 2a 2a 20 20 20 20 20 3c 63 6f 6c       **     <col
3517d 3e 20 4d 41 54 43 48 20 27 22 61 62 63 20 64 65  > MATCH '"abc de
3517e 66 22 20 4e 45 41 52 2f 32 20 22 67 68 69 20 6a  f" NEAR/2 "ghi j
3517f 6b 6c 22 27 0a 20 20 20 20 20 20 20 20 2a 2a 0a  kl"'.        **.
35180 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20          ** then 
35181 69 4c 65 66 74 20 77 69 6c 6c 20 62 65 20 73 65  iLeft will be se
35182 74 20 74 6f 20 32 20 28 74 6f 6b 65 6e 20 6e 75  t to 2 (token nu
35183 6d 62 65 72 20 6f 66 20 67 68 69 29 20 61 6e 64  mber of ghi) and
35184 20 6e 54 6f 6b 65 6e 20 77 69 6c 6c 0a 20 20 20   nToken will.   
35185 20 20 20 20 20 2a 2a 20 62 65 20 73 65 74 20 74       ** be set t
35186 6f 20 34 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  o 4..        */.
35187 20 20 20 20 20 20 20 20 46 74 73 33 45 78 70 72          Fts3Expr
35188 20 2a 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d   *pLeft = pExpr-
35189 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 20 20  >pLeft;.        
3518a 46 74 73 33 45 78 70 72 20 2a 70 52 69 67 68 74  Fts3Expr *pRight
3518b 20 3d 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74   = pExpr->pRight
3518c 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 4c  ;.        int iL
3518d 65 66 74 20 3d 20 2a 70 69 4c 65 66 74 3b 0a 20  eft = *piLeft;. 
3518e 20 20 20 20 20 20 20 69 6e 74 20 6e 4e 65 61 72         int nNear
3518f 20 3d 20 70 45 78 70 72 2d 3e 6e 4e 65 61 72 3b   = pExpr->nNear;
35190 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 54 6f  .        int nTo
35191 6b 65 6e 20 3d 20 70 52 69 67 68 74 2d 3e 70 50  ken = pRight->pP
35192 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 0a 20  hrase->nToken;. 
35193 20 20 20 20 20 20 20 69 6e 74 20 6a 6a 2c 20 69         int jj, i
35194 69 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  i;.        if( p
35195 4c 65 66 74 2d 3e 65 54 79 70 65 3d 3d 46 54 53  Left->eType==FTS
35196 51 55 45 52 59 5f 4e 45 41 52 20 29 7b 0a 20 20  QUERY_NEAR ){.  
35197 20 20 20 20 20 20 20 20 70 4c 65 66 74 20 3d 20          pLeft = 
35198 70 4c 65 66 74 2d 3e 70 52 69 67 68 74 3b 0a 20  pLeft->pRight;. 
35199 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
3519a 20 61 73 73 65 72 74 28 20 70 52 69 67 68 74 2d   assert( pRight-
3519b 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59  >eType==FTSQUERY
3519c 5f 50 48 52 41 53 45 20 29 3b 0a 20 20 20 20 20  _PHRASE );.     
3519d 20 20 20 61 73 73 65 72 74 28 20 70 4c 65 66 74     assert( pLeft
3519e 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52  ->eType==FTSQUER
3519f 59 5f 50 48 52 41 53 45 20 29 3b 0a 20 20 20 20  Y_PHRASE );.    
351a0 20 20 20 20 6e 54 6f 6b 65 6e 20 2b 3d 20 70 4c      nToken += pL
351a1 65 66 74 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54  eft->pPhrase->nT
351a2 6f 6b 65 6e 3b 0a 0a 20 20 20 20 20 20 20 20 66  oken;..        f
351a3 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 53 6e 69  or(ii=0; ii<pSni
351a4 70 70 65 74 2d 3e 6e 4d 61 74 63 68 3b 20 69 69  ppet->nMatch; ii
351a5 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  ++){.          s
351a6 74 72 75 63 74 20 73 6e 69 70 70 65 74 4d 61 74  truct snippetMat
351a7 63 68 20 2a 70 20 3d 20 26 70 53 6e 69 70 70 65  ch *p = &pSnippe
351a8 74 2d 3e 61 4d 61 74 63 68 5b 69 69 5d 3b 0a 20  t->aMatch[ii];. 
351a9 20 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e           if( p->
351aa 69 54 65 72 6d 3d 3d 69 4c 65 66 74 20 29 7b 0a  iTerm==iLeft ){.
351ab 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20              int 
351ac 69 73 4f 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20  isOk = 0;.      
351ad 20 20 20 20 20 20 2f 2a 20 53 6e 69 70 70 65 74        /* Snippet
351ae 20 69 69 20 69 73 20 61 6e 20 6f 63 63 75 72 65   ii is an occure
351af 6e 63 65 20 6f 66 20 71 75 65 72 79 20 74 65 72  nce of query ter
351b0 6d 20 69 4c 65 66 74 20 69 6e 20 74 68 65 20 64  m iLeft in the d
351b1 6f 63 75 6d 65 6e 74 2e 0a 20 20 20 20 20 20 20  ocument..       
351b2 20 20 20 20 20 2a 2a 20 49 74 20 6f 63 63 75 72       ** It occur
351b3 73 20 61 74 20 70 6f 73 69 74 69 6f 6e 20 28 70  s at position (p
351b4 2d 3e 69 54 6f 6b 65 6e 29 20 6f 66 20 74 68 65  ->iToken) of the
351b5 20 64 6f 63 75 6d 65 6e 74 2e 20 57 65 20 6e 6f   document. We no
351b6 77 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a  w.            **
351b7 20 73 65 61 72 63 68 20 66 6f 72 20 61 6e 20 69   search for an i
351b8 6e 73 74 61 6e 63 65 20 6f 66 20 74 6f 6b 65 6e  nstance of token
351b9 20 28 69 4c 65 66 74 2d 31 29 20 73 6f 6d 65 77   (iLeft-1) somew
351ba 68 65 72 65 20 69 6e 20 74 68 65 20 0a 20 20 20  here in the .   
351bb 20 20 20 20 20 20 20 20 20 2a 2a 20 72 61 6e 67           ** rang
351bc 65 20 28 70 2d 3e 69 54 6f 6b 65 6e 20 2d 20 6e  e (p->iToken - n
351bd 4e 65 61 72 29 2e 2e 2e 28 70 2d 3e 69 54 6f 6b  Near)...(p->iTok
351be 65 6e 20 2b 20 6e 4e 65 61 72 20 2b 20 6e 54 6f  en + nNear + nTo
351bf 6b 65 6e 29 20 77 69 74 68 69 6e 20 0a 20 20 20  ken) within .   
351c0 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20           ** the 
351c1 73 65 74 20 6f 66 20 73 6e 69 70 70 65 74 4d 61  set of snippetMa
351c2 74 63 68 20 73 74 72 75 63 74 75 72 65 73 2e 20  tch structures. 
351c3 49 66 20 6f 6e 65 20 69 73 20 66 6f 75 6e 64 2c  If one is found,
351c4 20 70 72 6f 63 65 65 64 2e 20 0a 20 20 20 20 20   proceed. .     
351c5 20 20 20 20 20 20 20 2a 2a 20 49 66 20 6f 6e 65         ** If one
351c6 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64   cannot be found
351c7 2c 20 74 68 65 6e 20 72 65 6d 6f 76 65 20 73 6e  , then remove sn
351c8 69 70 70 65 74 73 20 69 69 2e 2e 28 69 69 2b 4e  ippets ii..(ii+N
351c9 2d 31 29 20 0a 20 20 20 20 20 20 20 20 20 20 20  -1) .           
351ca 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 6d 61 74   ** from the mat
351cb 63 68 69 6e 67 20 73 6e 69 70 70 65 74 73 2c 20  ching snippets, 
351cc 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 6e  where N is the n
351cd 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20  umber of tokens 
351ce 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
351cf 69 6e 20 70 68 72 61 73 65 20 70 52 69 67 68 74  in phrase pRight
351d0 2d 3e 70 50 68 72 61 73 65 2e 0a 20 20 20 20 20  ->pPhrase..     
351d1 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
351d2 20 20 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20        for(jj=0; 
351d3 69 73 4f 6b 3d 3d 30 20 26 26 20 6a 6a 3c 70 53  isOk==0 && jj<pS
351d4 6e 69 70 70 65 74 2d 3e 6e 4d 61 74 63 68 3b 20  nippet->nMatch; 
351d5 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20  jj++){.         
351d6 20 20 20 20 20 73 74 72 75 63 74 20 73 6e 69 70       struct snip
351d7 70 65 74 4d 61 74 63 68 20 2a 70 32 20 3d 20 26  petMatch *p2 = &
351d8 70 53 6e 69 70 70 65 74 2d 3e 61 4d 61 74 63 68  pSnippet->aMatch
351d9 5b 6a 6a 5d 3b 0a 20 20 20 20 20 20 20 20 20 20  [jj];.          
351da 20 20 20 20 69 66 28 20 70 32 2d 3e 69 54 65 72      if( p2->iTer
351db 6d 3d 3d 28 69 4c 65 66 74 2d 31 29 20 29 7b 0a  m==(iLeft-1) ){.
351dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
351dd 69 66 28 20 70 32 2d 3e 69 54 6f 6b 65 6e 3e 3d  if( p2->iToken>=
351de 28 70 2d 3e 69 54 6f 6b 65 6e 2d 6e 4e 65 61 72  (p->iToken-nNear
351df 2d 31 29 20 0a 20 20 20 20 20 20 20 20 20 20 20  -1) .           
351e0 20 20 20 20 20 20 26 26 20 70 32 2d 3e 69 54 6f        && p2->iTo
351e1 6b 65 6e 3c 28 70 2d 3e 69 54 6f 6b 65 6e 2b 6e  ken<(p->iToken+n
351e2 4e 65 61 72 2b 6e 54 6f 6b 65 6e 29 20 0a 20 20  Near+nToken) .  
351e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 29 7b                ){
351e4 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
351e5 20 20 20 69 73 4f 6b 20 3d 20 31 3b 0a 20 20 20     isOk = 1;.   
351e6 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20               }. 
351e7 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20               }. 
351e8 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
351e9 20 20 20 20 20 20 20 20 20 69 66 28 20 21 69 73           if( !is
351ea 4f 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  Ok ){.          
351eb 20 20 20 20 69 6e 74 20 6b 6b 3b 0a 20 20 20 20      int kk;.    
351ec 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6b 6b            for(kk
351ed 3d 30 3b 20 6b 6b 3c 70 52 69 67 68 74 2d 3e 70  =0; kk<pRight->p
351ee 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 20  Phrase->nToken; 
351ef 6b 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20  kk++){.         
351f0 20 20 20 20 20 20 20 70 53 6e 69 70 70 65 74 2d         pSnippet-
351f1 3e 61 4d 61 74 63 68 5b 6b 6b 2b 69 69 5d 2e 69  >aMatch[kk+ii].i
351f2 54 65 72 6d 20 3d 20 2d 32 3b 0a 20 20 20 20 20  Term = -2;.     
351f3 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
351f4 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
351f5 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  1;.            }
351f6 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
351f7 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69 54         if( p->iT
351f8 65 72 6d 3d 3d 28 69 4c 65 66 74 2d 31 29 20 29  erm==(iLeft-1) )
351f9 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e  {.            in
351fa 74 20 69 73 4f 6b 20 3d 20 30 3b 0a 20 20 20 20  t isOk = 0;.    
351fb 20 20 20 20 20 20 20 20 66 6f 72 28 6a 6a 3d 30          for(jj=0
351fc 3b 20 69 73 4f 6b 3d 3d 30 20 26 26 20 6a 6a 3c  ; isOk==0 && jj<
351fd 70 53 6e 69 70 70 65 74 2d 3e 6e 4d 61 74 63 68  pSnippet->nMatch
351fe 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  ; jj++){.       
351ff 20 20 20 20 20 20 20 73 74 72 75 63 74 20 73 6e         struct sn
35200 69 70 70 65 74 4d 61 74 63 68 20 2a 70 32 20 3d  ippetMatch *p2 =
35201 20 26 70 53 6e 69 70 70 65 74 2d 3e 61 4d 61 74   &pSnippet->aMat
35202 63 68 5b 6a 6a 5d 3b 0a 20 20 20 20 20 20 20 20  ch[jj];.        
35203 20 20 20 20 20 20 69 66 28 20 70 32 2d 3e 69 54        if( p2->iT
35204 65 72 6d 3d 3d 69 4c 65 66 74 20 29 7b 0a 20 20  erm==iLeft ){.  
35205 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66                if
35206 28 20 70 32 2d 3e 69 54 6f 6b 65 6e 3c 3d 28 70  ( p2->iToken<=(p
35207 2d 3e 69 54 6f 6b 65 6e 2b 6e 4e 65 61 72 2b 31  ->iToken+nNear+1
35208 29 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ) .             
35209 20 20 20 20 26 26 20 70 32 2d 3e 69 54 6f 6b 65      && p2->iToke
3520a 6e 3e 28 70 2d 3e 69 54 6f 6b 65 6e 2d 6e 4e 65  n>(p->iToken-nNe
3520b 61 72 2d 6e 54 6f 6b 65 6e 29 20 0a 20 20 20 20  ar-nToken) .    
3520c 20 20 20 20 20 20 20 20 20 20 20 20 29 7b 0a 20              ){. 
3520d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3520e 20 69 73 4f 6b 20 3d 20 31 3b 0a 20 20 20 20 20   isOk = 1;.     
3520f 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
35210 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
35211 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
35212 20 20 20 20 20 20 20 69 66 28 20 21 69 73 4f 6b         if( !isOk
35213 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
35214 20 20 69 6e 74 20 6b 6b 3b 0a 20 20 20 20 20 20    int kk;.      
35215 20 20 20 20 20 20 20 20 66 6f 72 28 6b 6b 3d 30          for(kk=0
35216 3b 20 6b 6b 3c 70 4c 65 66 74 2d 3e 70 50 68 72  ; kk<pLeft->pPhr
35217 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 20 6b 6b 2b  ase->nToken; kk+
35218 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  +){.            
35219 20 20 20 20 70 53 6e 69 70 70 65 74 2d 3e 61 4d      pSnippet->aM
3521a 61 74 63 68 5b 69 69 2d 6b 6b 5d 2e 69 54 65 72  atch[ii-kk].iTer
3521b 6d 20 3d 20 2d 32 3b 0a 20 20 20 20 20 20 20 20  m = -2;.        
3521c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
3521d 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
3521e 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
3521f 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
35220 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61    }.        brea
35221 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
35222 0a 0a 20 20 20 20 69 66 28 20 74 72 69 6d 53 6e  ..    if( trimSn
35223 69 70 70 65 74 4f 66 66 73 65 74 73 28 70 45 78  ippetOffsets(pEx
35224 70 72 2d 3e 70 52 69 67 68 74 2c 20 70 53 6e 69  pr->pRight, pSni
35225 70 70 65 74 2c 20 70 69 4c 65 66 74 29 20 29 7b  ppet, piLeft) ){
35226 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
35227 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
35228 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
35229 43 6f 6d 70 75 74 65 20 61 6c 6c 20 6f 66 66 73  Compute all offs
3522a 65 74 73 20 66 6f 72 20 74 68 65 20 63 75 72 72  ets for the curr
3522b 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20 71  ent row of the q
3522c 75 65 72 79 2e 20 20 0a 2a 2a 20 49 66 20 74 68  uery.  .** If th
3522d 65 20 6f 66 66 73 65 74 73 20 68 61 76 65 20 61  e offsets have a
3522e 6c 72 65 61 64 79 20 62 65 65 6e 20 63 6f 6d 70  lready been comp
3522f 75 74 65 64 2c 20 74 68 69 73 20 72 6f 75 74 69  uted, this routi
35230 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  ne is a no-op..*
35231 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 6e  /.static void sn
35232 69 70 70 65 74 41 6c 6c 4f 66 66 73 65 74 73 28  ippetAllOffsets(
35233 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20  fulltext_cursor 
35234 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 43 6f 6c 75  *p){.  int nColu
35235 6d 6e 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d  mn;.  int iColum
35236 6e 2c 20 69 3b 0a 20 20 69 6e 74 20 69 46 69 72  n, i;.  int iFir
35237 73 74 2c 20 69 4c 61 73 74 3b 0a 20 20 69 6e 74  st, iLast;.  int
35238 20 69 54 65 72 6d 20 3d 20 30 3b 0a 20 20 66 75   iTerm = 0;.  fu
35239 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 70 46 74  lltext_vtab *pFt
3523a 73 20 3d 20 63 75 72 73 6f 72 5f 76 74 61 62 28  s = cursor_vtab(
3523b 70 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 73 6e  p);..  if( p->sn
3523c 69 70 70 65 74 2e 6e 4d 61 74 63 68 20 7c 7c 20  ippet.nMatch || 
3523d 70 2d 3e 70 45 78 70 72 3d 3d 30 20 29 7b 0a 20  p->pExpr==0 ){. 
3523e 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
3523f 20 6e 43 6f 6c 75 6d 6e 20 3d 20 70 46 74 73 2d   nColumn = pFts-
35240 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 69 43 6f 6c  >nColumn;.  iCol
35241 75 6d 6e 20 3d 20 28 70 2d 3e 69 43 75 72 73 6f  umn = (p->iCurso
35242 72 54 79 70 65 20 2d 20 51 55 45 52 59 5f 46 55  rType - QUERY_FU
35243 4c 4c 54 45 58 54 29 3b 0a 20 20 69 66 28 20 69  LLTEXT);.  if( i
35244 43 6f 6c 75 6d 6e 3c 30 20 7c 7c 20 69 43 6f 6c  Column<0 || iCol
35245 75 6d 6e 3e 3d 6e 43 6f 6c 75 6d 6e 20 29 7b 0a  umn>=nColumn ){.
35246 20 20 20 20 2f 2a 20 4c 6f 6f 6b 20 66 6f 72 20      /* Look for 
35247 6d 61 74 63 68 65 73 20 6f 76 65 72 20 61 6c 6c  matches over all
35248 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20   columns of the 
35249 66 75 6c 6c 2d 74 65 78 74 20 69 6e 64 65 78 20  full-text index 
3524a 2a 2f 0a 20 20 20 20 69 46 69 72 73 74 20 3d 20  */.    iFirst = 
3524b 30 3b 0a 20 20 20 20 69 4c 61 73 74 20 3d 20 6e  0;.    iLast = n
3524c 43 6f 6c 75 6d 6e 2d 31 3b 0a 20 20 7d 65 6c 73  Column-1;.  }els
3524d 65 7b 0a 20 20 20 20 2f 2a 20 4c 6f 6f 6b 20 66  e{.    /* Look f
3524e 6f 72 20 6d 61 74 63 68 65 73 20 69 6e 20 74 68  or matches in th
3524f 65 20 69 43 6f 6c 75 6d 6e 2d 74 68 20 63 6f 6c  e iColumn-th col
35250 75 6d 6e 20 6f 66 20 74 68 65 20 69 6e 64 65 78  umn of the index
35251 20 6f 6e 6c 79 20 2a 2f 0a 20 20 20 20 69 46 69   only */.    iFi
35252 72 73 74 20 3d 20 69 43 6f 6c 75 6d 6e 3b 0a 20  rst = iColumn;. 
35253 20 20 20 69 4c 61 73 74 20 3d 20 69 43 6f 6c 75     iLast = iColu
35254 6d 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d  mn;.  }.  for(i=
35255 69 46 69 72 73 74 3b 20 69 3c 3d 69 4c 61 73 74  iFirst; i<=iLast
35256 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 6f 6e 73  ; i++){.    cons
35257 74 20 63 68 61 72 20 2a 7a 44 6f 63 3b 0a 20 20  t char *zDoc;.  
35258 20 20 69 6e 74 20 6e 44 6f 63 3b 0a 20 20 20 20    int nDoc;.    
35259 7a 44 6f 63 20 3d 20 28 63 6f 6e 73 74 20 63 68  zDoc = (const ch
3525a 61 72 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ar*)sqlite3_colu
3525b 6d 6e 5f 74 65 78 74 28 70 2d 3e 70 53 74 6d 74  mn_text(p->pStmt
3525c 2c 20 69 2b 31 29 3b 0a 20 20 20 20 6e 44 6f 63  , i+1);.    nDoc
3525d 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d   = sqlite3_colum
3525e 6e 5f 62 79 74 65 73 28 70 2d 3e 70 53 74 6d 74  n_bytes(p->pStmt
3525f 2c 20 69 2b 31 29 3b 0a 20 20 20 20 73 6e 69 70  , i+1);.    snip
35260 70 65 74 4f 66 66 73 65 74 73 4f 66 43 6f 6c 75  petOffsetsOfColu
35261 6d 6e 28 70 2c 20 26 70 2d 3e 73 6e 69 70 70 65  mn(p, &p->snippe
35262 74 2c 20 69 2c 20 7a 44 6f 63 2c 20 6e 44 6f 63  t, i, zDoc, nDoc
35263 29 3b 0a 20 20 7d 0a 0a 20 20 77 68 69 6c 65 28  );.  }..  while(
35264 20 74 72 69 6d 53 6e 69 70 70 65 74 4f 66 66 73   trimSnippetOffs
35265 65 74 73 28 70 2d 3e 70 45 78 70 72 2c 20 26 70  ets(p->pExpr, &p
35266 2d 3e 73 6e 69 70 70 65 74 2c 20 26 69 54 65 72  ->snippet, &iTer
35267 6d 29 20 29 7b 0a 20 20 20 20 69 54 65 72 6d 20  m) ){.    iTerm 
35268 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  = 0;.  }.}../*.*
35269 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 69 6e  * Convert the in
3526a 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 74 68 65  formation in the
3526b 20 61 4d 61 74 63 68 5b 5d 20 61 72 72 61 79 20   aMatch[] array 
3526c 6f 66 20 74 68 65 20 73 6e 69 70 70 65 74 0a 2a  of the snippet.*
3526d 2a 20 69 6e 74 6f 20 74 68 65 20 73 74 72 69 6e  * into the strin
3526e 67 20 7a 4f 66 66 73 65 74 5b 30 2e 2e 6e 4f 66  g zOffset[0..nOf
3526f 66 73 65 74 2d 31 5d 2e 20 54 68 69 73 20 73 74  fset-1]. This st
35270 72 69 6e 67 20 69 73 20 75 73 65 64 20 61 73 0a  ring is used as.
35271 2a 2a 20 74 68 65 20 72 65 74 75 72 6e 20 6f 66  ** the return of
35272 20 74 68 65 20 53 51 4c 20 6f 66 66 73 65 74 73   the SQL offsets
35273 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a  () function..*/.
35274 73 74 61 74 69 63 20 76 6f 69 64 20 73 6e 69 70  static void snip
35275 70 65 74 4f 66 66 73 65 74 54 65 78 74 28 53 6e  petOffsetText(Sn
35276 69 70 70 65 74 20 2a 70 29 7b 0a 20 20 69 6e 74  ippet *p){.  int
35277 20 69 3b 0a 20 20 69 6e 74 20 63 6e 74 20 3d 20   i;.  int cnt = 
35278 30 3b 0a 20 20 53 74 72 69 6e 67 42 75 66 66 65  0;.  StringBuffe
35279 72 20 73 62 3b 0a 20 20 63 68 61 72 20 7a 42 75  r sb;.  char zBu
3527a 66 5b 32 30 30 5d 3b 0a 20 20 69 66 28 20 70 2d  f[200];.  if( p-
3527b 3e 7a 4f 66 66 73 65 74 20 29 20 72 65 74 75 72  >zOffset ) retur
3527c 6e 3b 0a 20 20 69 6e 69 74 53 74 72 69 6e 67 42  n;.  initStringB
3527d 75 66 66 65 72 28 26 73 62 29 3b 0a 20 20 66 6f  uffer(&sb);.  fo
3527e 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4d 61 74  r(i=0; i<p->nMat
3527f 63 68 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74  ch; i++){.    st
35280 72 75 63 74 20 73 6e 69 70 70 65 74 4d 61 74 63  ruct snippetMatc
35281 68 20 2a 70 4d 61 74 63 68 20 3d 20 26 70 2d 3e  h *pMatch = &p->
35282 61 4d 61 74 63 68 5b 69 5d 3b 0a 20 20 20 20 69  aMatch[i];.    i
35283 66 28 20 70 4d 61 74 63 68 2d 3e 69 54 65 72 6d  f( pMatch->iTerm
35284 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  >=0 ){.      /* 
35285 49 66 20 73 6e 69 70 70 65 74 4d 61 74 63 68 2e  If snippetMatch.
35286 69 54 65 72 6d 20 69 73 20 6c 65 73 73 20 74 68  iTerm is less th
35287 61 6e 20 30 2c 20 74 68 65 6e 20 74 68 65 20 6d  an 0, then the m
35288 61 74 63 68 20 77 61 73 20 0a 20 20 20 20 20 20  atch was .      
35289 2a 2a 20 64 69 73 63 61 72 64 65 64 20 61 73 20  ** discarded as 
3528a 70 61 72 74 20 6f 66 20 70 72 6f 63 65 73 73 69  part of processi
3528b 6e 67 20 74 68 65 20 4e 45 41 52 20 6f 70 65 72  ng the NEAR oper
3528c 61 74 6f 72 20 28 73 65 65 20 74 68 65 20 0a 20  ator (see the . 
3528d 20 20 20 20 20 2a 2a 20 74 72 69 6d 53 6e 69 70       ** trimSnip
3528e 70 65 74 4f 66 66 73 65 74 73 46 6f 72 4e 65 61  petOffsetsForNea
3528f 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72  r() function for
35290 20 64 65 74 61 69 6c 73 29 2e 20 49 67 6e 6f 72   details). Ignor
35291 65 20 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 69  e .      ** it i
35292 6e 20 74 68 69 73 20 63 61 73 65 0a 20 20 20 20  n this case.    
35293 20 20 2a 2f 0a 20 20 20 20 20 20 7a 42 75 66 5b    */.      zBuf[
35294 30 5d 20 3d 20 27 20 27 3b 0a 20 20 20 20 20 20  0] = ' ';.      
35295 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
35296 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 2d 31 2c  (sizeof(zBuf)-1,
35297 20 26 7a 42 75 66 5b 63 6e 74 3e 30 5d 2c 20 22   &zBuf[cnt>0], "
35298 25 64 20 25 64 20 25 64 20 25 64 22 2c 0a 20 20  %d %d %d %d",.  
35299 20 20 20 20 20 20 20 20 70 4d 61 74 63 68 2d 3e          pMatch->
3529a 69 43 6f 6c 2c 20 70 4d 61 74 63 68 2d 3e 69 54  iCol, pMatch->iT
3529b 65 72 6d 2c 20 70 4d 61 74 63 68 2d 3e 69 53 74  erm, pMatch->iSt
3529c 61 72 74 2c 20 70 4d 61 74 63 68 2d 3e 6e 42 79  art, pMatch->nBy
3529d 74 65 29 3b 0a 20 20 20 20 20 20 61 70 70 65 6e  te);.      appen
3529e 64 28 26 73 62 2c 20 7a 42 75 66 29 3b 0a 20 20  d(&sb, zBuf);.  
3529f 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 7d      cnt++;.    }
352a0 0a 20 20 7d 0a 20 20 70 2d 3e 7a 4f 66 66 73 65  .  }.  p->zOffse
352a1 74 20 3d 20 73 74 72 69 6e 67 42 75 66 66 65 72  t = stringBuffer
352a2 44 61 74 61 28 26 73 62 29 3b 0a 20 20 70 2d 3e  Data(&sb);.  p->
352a3 6e 4f 66 66 73 65 74 20 3d 20 73 74 72 69 6e 67  nOffset = string
352a4 42 75 66 66 65 72 4c 65 6e 67 74 68 28 26 73 62  BufferLength(&sb
352a5 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 7a 44 6f 63  );.}../*.** zDoc
352a6 5b 30 2e 2e 6e 44 6f 63 2d 31 5d 20 69 73 20 70  [0..nDoc-1] is p
352a7 68 72 61 73 65 20 6f 66 20 74 65 78 74 2e 20 20  hrase of text.  
352a8 61 4d 61 74 63 68 5b 30 2e 2e 6e 4d 61 74 63 68  aMatch[0..nMatch
352a9 2d 31 5d 20 61 72 65 20 61 20 73 65 74 0a 2a 2a  -1] are a set.**
352aa 20 6f 66 20 6d 61 74 63 68 69 6e 67 20 77 6f 72   of matching wor
352ab 64 73 20 73 6f 6d 65 20 6f 66 20 77 68 69 63 68  ds some of which
352ac 20 6d 69 67 68 74 20 62 65 20 69 6e 20 7a 44 6f   might be in zDo
352ad 63 2e 20 20 7a 44 6f 63 20 69 73 20 63 6f 6c 75  c.  zDoc is colu
352ae 6d 6e 0a 2a 2a 20 6e 75 6d 62 65 72 20 69 43 6f  mn.** number iCo
352af 6c 2e 0a 2a 2a 0a 2a 2a 20 69 42 72 65 61 6b 20  l..**.** iBreak 
352b0 69 73 20 73 75 67 67 65 73 74 65 64 20 73 70 6f  is suggested spo
352b1 74 20 69 6e 20 7a 44 6f 63 20 77 68 65 72 65 20  t in zDoc where 
352b2 77 65 20 63 6f 75 6c 64 20 62 65 67 69 6e 20 6f  we could begin o
352b3 72 20 65 6e 64 20 61 6e 0a 2a 2a 20 65 78 63 65  r end an.** exce
352b4 72 70 74 2e 20 20 52 65 74 75 72 6e 20 61 20 76  rpt.  Return a v
352b5 61 6c 75 65 20 73 69 6d 69 6c 61 72 20 74 6f 20  alue similar to 
352b6 69 42 72 65 61 6b 20 62 75 74 20 70 6f 73 73 69  iBreak but possi
352b7 62 6c 79 20 61 64 6a 75 73 74 65 64 0a 2a 2a 20  bly adjusted.** 
352b8 74 6f 20 62 65 20 61 20 6c 69 74 74 6c 65 20 6c  to be a little l
352b9 65 66 74 20 6f 72 20 72 69 67 68 74 20 73 6f 20  eft or right so 
352ba 74 68 61 74 20 74 68 65 20 62 72 65 61 6b 20 70  that the break p
352bb 6f 69 6e 74 20 69 73 20 62 65 74 74 65 72 2e 0a  oint is better..
352bc 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 6f  */.static int wo
352bd 72 64 42 6f 75 6e 64 61 72 79 28 0a 20 20 69 6e  rdBoundary(.  in
352be 74 20 69 42 72 65 61 6b 2c 20 20 20 20 20 20 20  t iBreak,       
352bf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
352c0 68 65 20 73 75 67 67 65 73 74 65 64 20 62 72 65  he suggested bre
352c1 61 6b 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 63 6f  ak point */.  co
352c2 6e 73 74 20 63 68 61 72 20 2a 7a 44 6f 63 2c 20  nst char *zDoc, 
352c3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
352c4 6f 63 75 6d 65 6e 74 20 74 65 78 74 20 2a 2f 0a  ocument text */.
352c5 20 20 69 6e 74 20 6e 44 6f 63 2c 20 20 20 20 20    int nDoc,     
352c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
352c7 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
352c8 65 73 20 69 6e 20 7a 44 6f 63 5b 5d 20 2a 2f 0a  es in zDoc[] */.
352c9 20 20 73 74 72 75 63 74 20 73 6e 69 70 70 65 74    struct snippet
352ca 4d 61 74 63 68 20 2a 61 4d 61 74 63 68 2c 20 20  Match *aMatch,  
352cb 2f 2a 20 4d 61 74 63 68 69 6e 67 20 77 6f 72 64  /* Matching word
352cc 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61 74 63  s */.  int nMatc
352cd 68 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h,              
352ce 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
352cf 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 4d 61  f entries in aMa
352d0 74 63 68 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69  tch[] */.  int i
352d1 43 6f 6c 20 20 20 20 20 20 20 20 20 20 20 20 20  Col             
352d2 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
352d3 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 66 6f  column number fo
352d4 72 20 7a 44 6f 63 5b 5d 20 2a 2f 0a 29 7b 0a 20  r zDoc[] */.){. 
352d5 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 69 42   int i;.  if( iB
352d6 72 65 61 6b 3c 3d 31 30 20 29 7b 0a 20 20 20 20  reak<=10 ){.    
352d7 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
352d8 69 66 28 20 69 42 72 65 61 6b 3e 3d 6e 44 6f 63  if( iBreak>=nDoc
352d9 2d 31 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  -10 ){.    retur
352da 6e 20 6e 44 6f 63 3b 0a 20 20 7d 0a 20 20 66 6f  n nDoc;.  }.  fo
352db 72 28 69 3d 30 3b 20 69 3c 6e 4d 61 74 63 68 20  r(i=0; i<nMatch 
352dc 26 26 20 61 4d 61 74 63 68 5b 69 5d 2e 69 43 6f  && aMatch[i].iCo
352dd 6c 3c 69 43 6f 6c 3b 20 69 2b 2b 29 7b 7d 0a 20  l<iCol; i++){}. 
352de 20 77 68 69 6c 65 28 20 69 3c 6e 4d 61 74 63 68   while( i<nMatch
352df 20 26 26 20 61 4d 61 74 63 68 5b 69 5d 2e 69 53   && aMatch[i].iS
352e0 74 61 72 74 2b 61 4d 61 74 63 68 5b 69 5d 2e 6e  tart+aMatch[i].n
352e1 42 79 74 65 3c 69 42 72 65 61 6b 20 29 7b 20 69  Byte<iBreak ){ i
352e2 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 69 3c 6e 4d  ++; }.  if( i<nM
352e3 61 74 63 68 20 29 7b 0a 20 20 20 20 69 66 28 20  atch ){.    if( 
352e4 61 4d 61 74 63 68 5b 69 5d 2e 69 53 74 61 72 74  aMatch[i].iStart
352e5 3c 69 42 72 65 61 6b 2b 31 30 20 29 7b 0a 20 20  <iBreak+10 ){.  
352e6 20 20 20 20 72 65 74 75 72 6e 20 61 4d 61 74 63      return aMatc
352e7 68 5b 69 5d 2e 69 53 74 61 72 74 3b 0a 20 20 20  h[i].iStart;.   
352e8 20 7d 0a 20 20 20 20 69 66 28 20 69 3e 30 20 26   }.    if( i>0 &
352e9 26 20 61 4d 61 74 63 68 5b 69 2d 31 5d 2e 69 53  & aMatch[i-1].iS
352ea 74 61 72 74 2b 61 4d 61 74 63 68 5b 69 2d 31 5d  tart+aMatch[i-1]
352eb 2e 6e 42 79 74 65 3e 3d 69 42 72 65 61 6b 20 29  .nByte>=iBreak )
352ec 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 61  {.      return a
352ed 4d 61 74 63 68 5b 69 2d 31 5d 2e 69 53 74 61 72  Match[i-1].iStar
352ee 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66  t;.    }.  }.  f
352ef 6f 72 28 69 3d 31 3b 20 69 3c 3d 31 30 3b 20 69  or(i=1; i<=10; i
352f0 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 61 66  ++){.    if( saf
352f1 65 5f 69 73 73 70 61 63 65 28 7a 44 6f 63 5b 69  e_isspace(zDoc[i
352f2 42 72 65 61 6b 2d 69 5d 29 20 29 7b 0a 20 20 20  Break-i]) ){.   
352f3 20 20 20 72 65 74 75 72 6e 20 69 42 72 65 61 6b     return iBreak
352f4 20 2d 20 69 20 2b 20 31 3b 0a 20 20 20 20 7d 0a   - i + 1;.    }.
352f5 20 20 20 20 69 66 28 20 73 61 66 65 5f 69 73 73      if( safe_iss
352f6 70 61 63 65 28 7a 44 6f 63 5b 69 42 72 65 61 6b  pace(zDoc[iBreak
352f7 2b 69 5d 29 20 29 7b 0a 20 20 20 20 20 20 72 65  +i]) ){.      re
352f8 74 75 72 6e 20 69 42 72 65 61 6b 20 2b 20 69 20  turn iBreak + i 
352f9 2b 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  + 1;.    }.  }. 
352fa 20 72 65 74 75 72 6e 20 69 42 72 65 61 6b 3b 0a   return iBreak;.
352fb 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77  }..../*.** Allow
352fc 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 53 6e  ed values for Sn
352fd 69 70 70 65 74 2e 61 4d 61 74 63 68 5b 5d 2e 73  ippet.aMatch[].s
352fe 6e 53 74 61 74 75 73 0a 2a 2f 0a 23 64 65 66 69  nStatus.*/.#defi
352ff 6e 65 20 53 4e 49 50 50 45 54 5f 49 47 4e 4f 52  ne SNIPPET_IGNOR
35300 45 20 20 30 20 20 20 2f 2a 20 49 74 20 69 73 20  E  0   /* It is 
35301 6f 6b 20 74 6f 20 6f 6d 69 74 20 74 68 69 73 20  ok to omit this 
35302 6d 61 74 63 68 20 66 72 6f 6d 20 74 68 65 20 73  match from the s
35303 6e 69 70 70 65 74 20 2a 2f 0a 23 64 65 66 69 6e  nippet */.#defin
35304 65 20 53 4e 49 50 50 45 54 5f 44 45 53 49 52 45  e SNIPPET_DESIRE
35305 44 20 31 20 20 20 2f 2a 20 57 65 20 77 61 6e 74  D 1   /* We want
35306 20 74 6f 20 69 6e 63 6c 75 64 65 20 74 68 69 73   to include this
35307 20 6d 61 74 63 68 20 69 6e 20 74 68 65 20 73 6e   match in the sn
35308 69 70 70 65 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  ippet */../*.** 
35309 47 65 6e 65 72 61 74 65 20 74 68 65 20 74 65 78  Generate the tex
3530a 74 20 6f 66 20 61 20 73 6e 69 70 70 65 74 2e 0a  t of a snippet..
3530b 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
3530c 6e 69 70 70 65 74 54 65 78 74 28 0a 20 20 66 75  nippetText(.  fu
3530d 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 70  lltext_cursor *p
3530e 43 75 72 73 6f 72 2c 20 20 20 2f 2a 20 54 68 65  Cursor,   /* The
3530f 20 63 75 72 73 6f 72 20 77 65 20 6e 65 65 64 20   cursor we need 
35310 74 68 65 20 73 6e 69 70 70 65 74 20 66 6f 72 20  the snippet for 
35311 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
35312 2a 7a 53 74 61 72 74 4d 61 72 6b 2c 20 20 20 20  *zStartMark,    
35313 20 2f 2a 20 4d 61 72 6b 75 70 20 74 6f 20 61 70   /* Markup to ap
35314 70 65 61 72 20 62 65 66 6f 72 65 20 65 61 63 68  pear before each
35315 20 6d 61 74 63 68 20 2a 2f 0a 20 20 63 6f 6e 73   match */.  cons
35316 74 20 63 68 61 72 20 2a 7a 45 6e 64 4d 61 72 6b  t char *zEndMark
35317 2c 20 20 20 20 20 20 20 2f 2a 20 4d 61 72 6b 75  ,       /* Marku
35318 70 20 74 6f 20 61 70 70 65 61 72 20 61 66 74 65  p to appear afte
35319 72 20 65 61 63 68 20 6d 61 74 63 68 20 2a 2f 0a  r each match */.
3531a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45    const char *zE
3531b 6c 6c 69 70 73 69 73 20 20 20 20 20 20 20 2f 2a  llipsis       /*
3531c 20 45 6c 6c 69 70 73 69 73 20 6d 61 72 6b 20 2a   Ellipsis mark *
3531d 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b  /.){.  int i, j;
3531e 0a 20 20 73 74 72 75 63 74 20 73 6e 69 70 70 65  .  struct snippe
3531f 74 4d 61 74 63 68 20 2a 61 4d 61 74 63 68 3b 0a  tMatch *aMatch;.
35320 20 20 69 6e 74 20 6e 4d 61 74 63 68 3b 0a 20 20    int nMatch;.  
35321 69 6e 74 20 6e 44 65 73 69 72 65 64 3b 0a 20 20  int nDesired;.  
35322 53 74 72 69 6e 67 42 75 66 66 65 72 20 73 62 3b  StringBuffer sb;
35323 0a 20 20 69 6e 74 20 74 61 69 6c 43 6f 6c 3b 0a  .  int tailCol;.
35324 20 20 69 6e 74 20 74 61 69 6c 4f 66 66 73 65 74    int tailOffset
35325 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 0a 20 20  ;.  int iCol;.  
35326 69 6e 74 20 6e 44 6f 63 3b 0a 20 20 63 6f 6e 73  int nDoc;.  cons
35327 74 20 63 68 61 72 20 2a 7a 44 6f 63 3b 0a 20 20  t char *zDoc;.  
35328 69 6e 74 20 69 53 74 61 72 74 2c 20 69 45 6e 64  int iStart, iEnd
35329 3b 0a 20 20 69 6e 74 20 74 61 69 6c 45 6c 6c 69  ;.  int tailElli
3532a 70 73 69 73 20 3d 20 30 3b 0a 20 20 69 6e 74 20  psis = 0;.  int 
3532b 69 4d 61 74 63 68 3b 0a 20 20 0a 0a 20 20 73 71  iMatch;.  ..  sq
3532c 6c 69 74 65 33 5f 66 72 65 65 28 70 43 75 72 73  lite3_free(pCurs
3532d 6f 72 2d 3e 73 6e 69 70 70 65 74 2e 7a 53 6e 69  or->snippet.zSni
3532e 70 70 65 74 29 3b 0a 20 20 70 43 75 72 73 6f 72  ppet);.  pCursor
3532f 2d 3e 73 6e 69 70 70 65 74 2e 7a 53 6e 69 70 70  ->snippet.zSnipp
35330 65 74 20 3d 20 30 3b 0a 20 20 61 4d 61 74 63 68  et = 0;.  aMatch
35331 20 3d 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70   = pCursor->snip
35332 70 65 74 2e 61 4d 61 74 63 68 3b 0a 20 20 6e 4d  pet.aMatch;.  nM
35333 61 74 63 68 20 3d 20 70 43 75 72 73 6f 72 2d 3e  atch = pCursor->
35334 73 6e 69 70 70 65 74 2e 6e 4d 61 74 63 68 3b 0a  snippet.nMatch;.
35335 20 20 69 6e 69 74 53 74 72 69 6e 67 42 75 66 66    initStringBuff
35336 65 72 28 26 73 62 29 3b 0a 0a 20 20 66 6f 72 28  er(&sb);..  for(
35337 69 3d 30 3b 20 69 3c 6e 4d 61 74 63 68 3b 20 69  i=0; i<nMatch; i
35338 2b 2b 29 7b 0a 20 20 20 20 61 4d 61 74 63 68 5b  ++){.    aMatch[
35339 69 5d 2e 73 6e 53 74 61 74 75 73 20 3d 20 53 4e  i].snStatus = SN
3533a 49 50 50 45 54 5f 49 47 4e 4f 52 45 3b 0a 20 20  IPPET_IGNORE;.  
3533b 7d 0a 20 20 6e 44 65 73 69 72 65 64 20 3d 20 30  }.  nDesired = 0
3533c 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 46  ;.  for(i=0; i<F
3533d 54 53 33 5f 52 4f 54 4f 52 5f 53 5a 3b 20 69 2b  TS3_ROTOR_SZ; i+
3533e 2b 29 7b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b  +){.    for(j=0;
3533f 20 6a 3c 6e 4d 61 74 63 68 3b 20 6a 2b 2b 29 7b   j<nMatch; j++){
35340 0a 20 20 20 20 20 20 69 66 28 20 61 4d 61 74 63  .      if( aMatc
35341 68 5b 6a 5d 2e 69 54 65 72 6d 3d 3d 69 20 29 7b  h[j].iTerm==i ){
35342 0a 20 20 20 20 20 20 20 20 61 4d 61 74 63 68 5b  .        aMatch[
35343 6a 5d 2e 73 6e 53 74 61 74 75 73 20 3d 20 53 4e  j].snStatus = SN
35344 49 50 50 45 54 5f 44 45 53 49 52 45 44 3b 0a 20  IPPET_DESIRED;. 
35345 20 20 20 20 20 20 20 6e 44 65 73 69 72 65 64 2b         nDesired+
35346 2b 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  +;.        break
35347 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
35348 20 20 7d 0a 0a 20 20 69 4d 61 74 63 68 20 3d 20    }..  iMatch = 
35349 30 3b 0a 20 20 74 61 69 6c 43 6f 6c 20 3d 20 2d  0;.  tailCol = -
3534a 31 3b 0a 20 20 74 61 69 6c 4f 66 66 73 65 74 20  1;.  tailOffset 
3534b 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  = 0;.  for(i=0; 
3534c 69 3c 6e 4d 61 74 63 68 20 26 26 20 6e 44 65 73  i<nMatch && nDes
3534d 69 72 65 64 3e 30 3b 20 69 2b 2b 29 7b 0a 20 20  ired>0; i++){.  
3534e 20 20 69 66 28 20 61 4d 61 74 63 68 5b 69 5d 2e    if( aMatch[i].
3534f 73 6e 53 74 61 74 75 73 21 3d 53 4e 49 50 50 45  snStatus!=SNIPPE
35350 54 5f 44 45 53 49 52 45 44 20 29 20 63 6f 6e 74  T_DESIRED ) cont
35351 69 6e 75 65 3b 0a 20 20 20 20 6e 44 65 73 69 72  inue;.    nDesir
35352 65 64 2d 2d 3b 0a 20 20 20 20 69 43 6f 6c 20 3d  ed--;.    iCol =
35353 20 61 4d 61 74 63 68 5b 69 5d 2e 69 43 6f 6c 3b   aMatch[i].iCol;
35354 0a 20 20 20 20 7a 44 6f 63 20 3d 20 28 63 6f 6e  .    zDoc = (con
35355 73 74 20 63 68 61 72 2a 29 73 71 6c 69 74 65 33  st char*)sqlite3
35356 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 70 43 75  _column_text(pCu
35357 72 73 6f 72 2d 3e 70 53 74 6d 74 2c 20 69 43 6f  rsor->pStmt, iCo
35358 6c 2b 31 29 3b 0a 20 20 20 20 6e 44 6f 63 20 3d  l+1);.    nDoc =
35359 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
3535a 62 79 74 65 73 28 70 43 75 72 73 6f 72 2d 3e 70  bytes(pCursor->p
3535b 53 74 6d 74 2c 20 69 43 6f 6c 2b 31 29 3b 0a 20  Stmt, iCol+1);. 
3535c 20 20 20 69 53 74 61 72 74 20 3d 20 61 4d 61 74     iStart = aMat
3535d 63 68 5b 69 5d 2e 69 53 74 61 72 74 20 2d 20 34  ch[i].iStart - 4
3535e 30 3b 0a 20 20 20 20 69 53 74 61 72 74 20 3d 20  0;.    iStart = 
3535f 77 6f 72 64 42 6f 75 6e 64 61 72 79 28 69 53 74  wordBoundary(iSt
35360 61 72 74 2c 20 7a 44 6f 63 2c 20 6e 44 6f 63 2c  art, zDoc, nDoc,
35361 20 61 4d 61 74 63 68 2c 20 6e 4d 61 74 63 68 2c   aMatch, nMatch,
35362 20 69 43 6f 6c 29 3b 0a 20 20 20 20 69 66 28 20   iCol);.    if( 
35363 69 53 74 61 72 74 3c 3d 31 30 20 29 7b 0a 20 20  iStart<=10 ){.  
35364 20 20 20 20 69 53 74 61 72 74 20 3d 20 30 3b 0a      iStart = 0;.
35365 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 43      }.    if( iC
35366 6f 6c 3d 3d 74 61 69 6c 43 6f 6c 20 26 26 20 69  ol==tailCol && i
35367 53 74 61 72 74 3c 3d 74 61 69 6c 4f 66 66 73 65  Start<=tailOffse
35368 74 2b 32 30 20 29 7b 0a 20 20 20 20 20 20 69 53  t+20 ){.      iS
35369 74 61 72 74 20 3d 20 74 61 69 6c 4f 66 66 73 65  tart = tailOffse
3536a 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  t;.    }.    if(
3536b 20 28 69 43 6f 6c 21 3d 74 61 69 6c 43 6f 6c 20   (iCol!=tailCol 
3536c 26 26 20 74 61 69 6c 43 6f 6c 3e 3d 30 29 20 7c  && tailCol>=0) |
3536d 7c 20 69 53 74 61 72 74 21 3d 74 61 69 6c 4f 66  | iStart!=tailOf
3536e 66 73 65 74 20 29 7b 0a 20 20 20 20 20 20 74 72  fset ){.      tr
3536f 69 6d 57 68 69 74 65 53 70 61 63 65 28 26 73 62  imWhiteSpace(&sb
35370 29 3b 0a 20 20 20 20 20 20 61 70 70 65 6e 64 57  );.      appendW
35371 68 69 74 65 53 70 61 63 65 28 26 73 62 29 3b 0a  hiteSpace(&sb);.
35372 20 20 20 20 20 20 61 70 70 65 6e 64 28 26 73 62        append(&sb
35373 2c 20 7a 45 6c 6c 69 70 73 69 73 29 3b 0a 20 20  , zEllipsis);.  
35374 20 20 20 20 61 70 70 65 6e 64 57 68 69 74 65 53      appendWhiteS
35375 70 61 63 65 28 26 73 62 29 3b 0a 20 20 20 20 7d  pace(&sb);.    }
35376 0a 20 20 20 20 69 45 6e 64 20 3d 20 61 4d 61 74  .    iEnd = aMat
35377 63 68 5b 69 5d 2e 69 53 74 61 72 74 20 2b 20 61  ch[i].iStart + a
35378 4d 61 74 63 68 5b 69 5d 2e 6e 42 79 74 65 20 2b  Match[i].nByte +
35379 20 34 30 3b 0a 20 20 20 20 69 45 6e 64 20 3d 20   40;.    iEnd = 
3537a 77 6f 72 64 42 6f 75 6e 64 61 72 79 28 69 45 6e  wordBoundary(iEn
3537b 64 2c 20 7a 44 6f 63 2c 20 6e 44 6f 63 2c 20 61  d, zDoc, nDoc, a
3537c 4d 61 74 63 68 2c 20 6e 4d 61 74 63 68 2c 20 69  Match, nMatch, i
3537d 43 6f 6c 29 3b 0a 20 20 20 20 69 66 28 20 69 45  Col);.    if( iE
3537e 6e 64 3e 3d 6e 44 6f 63 2d 31 30 20 29 7b 0a 20  nd>=nDoc-10 ){. 
3537f 20 20 20 20 20 69 45 6e 64 20 3d 20 6e 44 6f 63       iEnd = nDoc
35380 3b 0a 20 20 20 20 20 20 74 61 69 6c 45 6c 6c 69  ;.      tailElli
35381 70 73 69 73 20 3d 20 30 3b 0a 20 20 20 20 7d 65  psis = 0;.    }e
35382 6c 73 65 7b 0a 20 20 20 20 20 20 74 61 69 6c 45  lse{.      tailE
35383 6c 6c 69 70 73 69 73 20 3d 20 31 3b 0a 20 20 20  llipsis = 1;.   
35384 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 69 4d   }.    while( iM
35385 61 74 63 68 3c 6e 4d 61 74 63 68 20 26 26 20 61  atch<nMatch && a
35386 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e 69 43  Match[iMatch].iC
35387 6f 6c 3c 69 43 6f 6c 20 29 7b 20 69 4d 61 74 63  ol<iCol ){ iMatc
35388 68 2b 2b 3b 20 7d 0a 20 20 20 20 77 68 69 6c 65  h++; }.    while
35389 28 20 69 53 74 61 72 74 3c 69 45 6e 64 20 29 7b  ( iStart<iEnd ){
3538a 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 69 4d  .      while( iM
3538b 61 74 63 68 3c 6e 4d 61 74 63 68 20 26 26 20 61  atch<nMatch && a
3538c 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e 69 53  Match[iMatch].iS
3538d 74 61 72 74 3c 69 53 74 61 72 74 0a 20 20 20 20  tart<iStart.    
3538e 20 20 20 20 20 20 20 20 20 26 26 20 61 4d 61 74           && aMat
3538f 63 68 5b 69 4d 61 74 63 68 5d 2e 69 43 6f 6c 3c  ch[iMatch].iCol<
35390 3d 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20  =iCol ){.       
35391 20 69 4d 61 74 63 68 2b 2b 3b 0a 20 20 20 20 20   iMatch++;.     
35392 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 4d 61   }.      if( iMa
35393 74 63 68 3c 6e 4d 61 74 63 68 20 26 26 20 61 4d  tch<nMatch && aM
35394 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e 69 53 74  atch[iMatch].iSt
35395 61 72 74 3c 69 45 6e 64 0a 20 20 20 20 20 20 20  art<iEnd.       
35396 20 20 20 20 20 20 26 26 20 61 4d 61 74 63 68 5b        && aMatch[
35397 69 4d 61 74 63 68 5d 2e 69 43 6f 6c 3d 3d 69 43  iMatch].iCol==iC
35398 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 6e 61  ol ){.        na
35399 70 70 65 6e 64 28 26 73 62 2c 20 26 7a 44 6f 63  ppend(&sb, &zDoc
3539a 5b 69 53 74 61 72 74 5d 2c 20 61 4d 61 74 63 68  [iStart], aMatch
3539b 5b 69 4d 61 74 63 68 5d 2e 69 53 74 61 72 74 20  [iMatch].iStart 
3539c 2d 20 69 53 74 61 72 74 29 3b 0a 20 20 20 20 20  - iStart);.     
3539d 20 20 20 69 53 74 61 72 74 20 3d 20 61 4d 61 74     iStart = aMat
3539e 63 68 5b 69 4d 61 74 63 68 5d 2e 69 53 74 61 72  ch[iMatch].iStar
3539f 74 3b 0a 20 20 20 20 20 20 20 20 61 70 70 65 6e  t;.        appen
353a0 64 28 26 73 62 2c 20 7a 53 74 61 72 74 4d 61 72  d(&sb, zStartMar
353a1 6b 29 3b 0a 20 20 20 20 20 20 20 20 6e 61 70 70  k);.        napp
353a2 65 6e 64 28 26 73 62 2c 20 26 7a 44 6f 63 5b 69  end(&sb, &zDoc[i
353a3 53 74 61 72 74 5d 2c 20 61 4d 61 74 63 68 5b 69  Start], aMatch[i
353a4 4d 61 74 63 68 5d 2e 6e 42 79 74 65 29 3b 0a 20  Match].nByte);. 
353a5 20 20 20 20 20 20 20 61 70 70 65 6e 64 28 26 73         append(&s
353a6 62 2c 20 7a 45 6e 64 4d 61 72 6b 29 3b 0a 20 20  b, zEndMark);.  
353a7 20 20 20 20 20 20 69 53 74 61 72 74 20 2b 3d 20        iStart += 
353a8 61 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e 6e  aMatch[iMatch].n
353a9 42 79 74 65 3b 0a 20 20 20 20 20 20 20 20 66 6f  Byte;.        fo
353aa 72 28 6a 3d 69 4d 61 74 63 68 2b 31 3b 20 6a 3c  r(j=iMatch+1; j<
353ab 6e 4d 61 74 63 68 3b 20 6a 2b 2b 29 7b 0a 20 20  nMatch; j++){.  
353ac 20 20 20 20 20 20 20 20 69 66 28 20 61 4d 61 74          if( aMat
353ad 63 68 5b 6a 5d 2e 69 54 65 72 6d 3d 3d 61 4d 61  ch[j].iTerm==aMa
353ae 74 63 68 5b 69 4d 61 74 63 68 5d 2e 69 54 65 72  tch[iMatch].iTer
353af 6d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m.              
353b0 26 26 20 61 4d 61 74 63 68 5b 6a 5d 2e 73 6e 53  && aMatch[j].snS
353b1 74 61 74 75 73 3d 3d 53 4e 49 50 50 45 54 5f 44  tatus==SNIPPET_D
353b2 45 53 49 52 45 44 20 29 7b 0a 20 20 20 20 20 20  ESIRED ){.      
353b3 20 20 20 20 20 20 6e 44 65 73 69 72 65 64 2d 2d        nDesired--
353b4 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 4d  ;.            aM
353b5 61 74 63 68 5b 6a 5d 2e 73 6e 53 74 61 74 75 73  atch[j].snStatus
353b6 20 3d 20 53 4e 49 50 50 45 54 5f 49 47 4e 4f 52   = SNIPPET_IGNOR
353b7 45 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  E;.          }. 
353b8 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
353b9 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6e 61  else{.        na
353ba 70 70 65 6e 64 28 26 73 62 2c 20 26 7a 44 6f 63  ppend(&sb, &zDoc
353bb 5b 69 53 74 61 72 74 5d 2c 20 69 45 6e 64 20 2d  [iStart], iEnd -
353bc 20 69 53 74 61 72 74 29 3b 0a 20 20 20 20 20 20   iStart);.      
353bd 20 20 69 53 74 61 72 74 20 3d 20 69 45 6e 64 3b    iStart = iEnd;
353be 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
353bf 20 20 20 74 61 69 6c 43 6f 6c 20 3d 20 69 43 6f     tailCol = iCo
353c0 6c 3b 0a 20 20 20 20 74 61 69 6c 4f 66 66 73 65  l;.    tailOffse
353c1 74 20 3d 20 69 45 6e 64 3b 0a 20 20 7d 0a 20 20  t = iEnd;.  }.  
353c2 74 72 69 6d 57 68 69 74 65 53 70 61 63 65 28 26  trimWhiteSpace(&
353c3 73 62 29 3b 0a 20 20 69 66 28 20 74 61 69 6c 45  sb);.  if( tailE
353c4 6c 6c 69 70 73 69 73 20 29 7b 0a 20 20 20 20 61  llipsis ){.    a
353c5 70 70 65 6e 64 57 68 69 74 65 53 70 61 63 65 28  ppendWhiteSpace(
353c6 26 73 62 29 3b 0a 20 20 20 20 61 70 70 65 6e 64  &sb);.    append
353c7 28 26 73 62 2c 20 7a 45 6c 6c 69 70 73 69 73 29  (&sb, zEllipsis)
353c8 3b 0a 20 20 7d 0a 20 20 70 43 75 72 73 6f 72 2d  ;.  }.  pCursor-
353c9 3e 73 6e 69 70 70 65 74 2e 7a 53 6e 69 70 70 65  >snippet.zSnippe
353ca 74 20 3d 20 73 74 72 69 6e 67 42 75 66 66 65 72  t = stringBuffer
353cb 44 61 74 61 28 26 73 62 29 3b 0a 20 20 70 43 75  Data(&sb);.  pCu
353cc 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74 2e 6e 53  rsor->snippet.nS
353cd 6e 69 70 70 65 74 20 3d 20 73 74 72 69 6e 67 42  nippet = stringB
353ce 75 66 66 65 72 4c 65 6e 67 74 68 28 26 73 62 29  ufferLength(&sb)
353cf 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73  ;.}.../*.** Clos
353d0 65 20 74 68 65 20 63 75 72 73 6f 72 2e 20 20 46  e the cursor.  F
353d1 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  or additional in
353d2 66 6f 72 6d 61 74 69 6f 6e 20 73 65 65 20 74 68  formation see th
353d3 65 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 0a  e documentation.
353d4 2a 2a 20 6f 6e 20 74 68 65 20 78 43 6c 6f 73 65  ** on the xClose
353d5 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 76   method of the v
353d6 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 74  irtual table int
353d7 65 72 66 61 63 65 2e 0a 2a 2f 0a 73 74 61 74 69  erface..*/.stati
353d8 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 43 6c  c int fulltextCl
353d9 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 74 61 62  ose(sqlite3_vtab
353da 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  _cursor *pCursor
353db 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75  ){.  fulltext_cu
353dc 72 73 6f 72 20 2a 63 20 3d 20 28 66 75 6c 6c 74  rsor *c = (fullt
353dd 65 78 74 5f 63 75 72 73 6f 72 20 2a 29 20 70 43  ext_cursor *) pC
353de 75 72 73 6f 72 3b 0a 20 20 46 54 53 54 52 41 43  ursor;.  FTSTRAC
353df 45 28 28 22 46 54 53 33 20 43 6c 6f 73 65 20 25  E(("FTS3 Close %
353e0 70 5c 6e 22 2c 20 63 29 29 3b 0a 20 20 73 71 6c  p\n", c));.  sql
353e1 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 63 2d  ite3_finalize(c-
353e2 3e 70 53 74 6d 74 29 3b 0a 20 20 73 71 6c 69 74  >pStmt);.  sqlit
353e3 65 33 46 74 73 33 45 78 70 72 46 72 65 65 28 63  e3Fts3ExprFree(c
353e4 2d 3e 70 45 78 70 72 29 3b 0a 20 20 73 6e 69 70  ->pExpr);.  snip
353e5 70 65 74 43 6c 65 61 72 28 26 63 2d 3e 73 6e 69  petClear(&c->sni
353e6 70 70 65 74 29 3b 0a 20 20 69 66 28 20 63 2d 3e  ppet);.  if( c->
353e7 72 65 73 75 6c 74 2e 6e 44 61 74 61 21 3d 30 20  result.nData!=0 
353e8 29 7b 0a 20 20 20 20 64 6c 72 44 65 73 74 72 6f  ){.    dlrDestro
353e9 79 28 26 63 2d 3e 72 65 61 64 65 72 29 3b 0a 20  y(&c->reader);. 
353ea 20 7d 0a 20 20 64 61 74 61 42 75 66 66 65 72 44   }.  dataBufferD
353eb 65 73 74 72 6f 79 28 26 63 2d 3e 72 65 73 75 6c  estroy(&c->resul
353ec 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  t);.  sqlite3_fr
353ed 65 65 28 63 29 3b 0a 20 20 72 65 74 75 72 6e 20  ee(c);.  return 
353ee 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 73 74  SQLITE_OK;.}..st
353ef 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78  atic int fulltex
353f0 74 4e 65 78 74 28 73 71 6c 69 74 65 33 5f 76 74  tNext(sqlite3_vt
353f1 61 62 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73  ab_cursor *pCurs
353f2 6f 72 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f  or){.  fulltext_
353f3 63 75 72 73 6f 72 20 2a 63 20 3d 20 28 66 75 6c  cursor *c = (ful
353f4 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 29 20  ltext_cursor *) 
353f5 70 43 75 72 73 6f 72 3b 0a 20 20 69 6e 74 20 72  pCursor;.  int r
353f6 63 3b 0a 0a 20 20 46 54 53 54 52 41 43 45 28 28  c;..  FTSTRACE((
353f7 22 46 54 53 33 20 4e 65 78 74 20 25 70 5c 6e 22  "FTS3 Next %p\n"
353f8 2c 20 70 43 75 72 73 6f 72 29 29 3b 0a 20 20 73  , pCursor));.  s
353f9 6e 69 70 70 65 74 43 6c 65 61 72 28 26 63 2d 3e  nippetClear(&c->
353fa 73 6e 69 70 70 65 74 29 3b 0a 20 20 69 66 28 20  snippet);.  if( 
353fb 63 2d 3e 69 43 75 72 73 6f 72 54 79 70 65 20 3c  c->iCursorType <
353fc 20 51 55 45 52 59 5f 46 55 4c 4c 54 45 58 54 20   QUERY_FULLTEXT 
353fd 29 7b 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73  ){.    /* TODO(s
353fe 68 65 73 73 29 20 48 61 6e 64 6c 65 20 53 51 4c  hess) Handle SQL
353ff 49 54 45 5f 53 43 48 45 4d 41 20 41 4e 44 20 53  ITE_SCHEMA AND S
35400 51 4c 49 54 45 5f 42 55 53 59 2e 20 2a 2f 0a 20  QLITE_BUSY. */. 
35401 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
35402 73 74 65 70 28 63 2d 3e 70 53 74 6d 74 29 3b 0a  step(c->pStmt);.
35403 20 20 20 20 73 77 69 74 63 68 28 20 72 63 20 29      switch( rc )
35404 7b 0a 20 20 20 20 20 20 63 61 73 65 20 53 51 4c  {.      case SQL
35405 49 54 45 5f 52 4f 57 3a 0a 20 20 20 20 20 20 20  ITE_ROW:.       
35406 20 63 2d 3e 65 6f 66 20 3d 20 30 3b 0a 20 20 20   c->eof = 0;.   
35407 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
35408 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 63 61 73  TE_OK;.      cas
35409 65 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3a 0a 20  e SQLITE_DONE:. 
3540a 20 20 20 20 20 20 20 63 2d 3e 65 6f 66 20 3d 20         c->eof = 
3540b 31 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  1;.        retur
3540c 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  n SQLITE_OK;.   
3540d 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20     default:.    
3540e 20 20 20 20 63 2d 3e 65 6f 66 20 3d 20 31 3b 0a      c->eof = 1;.
3540f 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
35410 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 65 6c 73  c;.    }.  } els
35411 65 20 7b 20 20 2f 2a 20 66 75 6c 6c 2d 74 65 78  e {  /* full-tex
35412 74 20 71 75 65 72 79 20 2a 2f 0a 20 20 20 20 72  t query */.    r
35413 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65  c = sqlite3_rese
35414 74 28 63 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 20  t(c->pStmt);.   
35415 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
35416 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
35417 0a 20 20 20 20 69 66 28 20 63 2d 3e 72 65 73 75  .    if( c->resu
35418 6c 74 2e 6e 44 61 74 61 3d 3d 30 20 7c 7c 20 64  lt.nData==0 || d
35419 6c 72 41 74 45 6e 64 28 26 63 2d 3e 72 65 61 64  lrAtEnd(&c->read
3541a 65 72 29 20 29 7b 0a 20 20 20 20 20 20 63 2d 3e  er) ){.      c->
3541b 65 6f 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 72  eof = 1;.      r
3541c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
3541d 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20  .    }.    rc = 
3541e 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
3541f 36 34 28 63 2d 3e 70 53 74 6d 74 2c 20 31 2c 20  64(c->pStmt, 1, 
35420 64 6c 72 44 6f 63 69 64 28 26 63 2d 3e 72 65 61  dlrDocid(&c->rea
35421 64 65 72 29 29 3b 0a 20 20 20 20 64 6c 72 53 74  der));.    dlrSt
35422 65 70 28 26 63 2d 3e 72 65 61 64 65 72 29 3b 0a  ep(&c->reader);.
35423 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
35424 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
35425 63 3b 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73  c;.    /* TODO(s
35426 68 65 73 73 29 20 48 61 6e 64 6c 65 20 53 51 4c  hess) Handle SQL
35427 49 54 45 5f 53 43 48 45 4d 41 20 41 4e 44 20 53  ITE_SCHEMA AND S
35428 51 4c 49 54 45 5f 42 55 53 59 2e 20 2a 2f 0a 20  QLITE_BUSY. */. 
35429 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
3542a 73 74 65 70 28 63 2d 3e 70 53 74 6d 74 29 3b 0a  step(c->pStmt);.
3542b 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
3542c 54 45 5f 52 4f 57 20 29 7b 20 20 20 2f 2a 20 74  TE_ROW ){   /* t
3542d 68 65 20 63 61 73 65 20 77 65 20 65 78 70 65 63  he case we expec
3542e 74 20 2a 2f 0a 20 20 20 20 20 20 63 2d 3e 65 6f  t */.      c->eo
3542f 66 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74  f = 0;.      ret
35430 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
35431 20 20 20 7d 0a 20 20 20 20 2f 2a 20 61 6e 20 65     }.    /* an e
35432 72 72 6f 72 20 6f 63 63 75 72 72 65 64 3b 20 61  rror occurred; a
35433 62 6f 72 74 20 2a 2f 0a 20 20 20 20 72 65 74 75  bort */.    retu
35434 72 6e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f  rn rc==SQLITE_DO
35435 4e 45 20 3f 20 53 51 4c 49 54 45 5f 45 52 52 4f  NE ? SQLITE_ERRO
35436 52 20 3a 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 0a  R : rc;.  }.}...
35437 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 49  /* TODO(shess) I
35438 66 20 77 65 20 70 75 73 68 65 64 20 4c 65 61 66  f we pushed Leaf
35439 52 65 61 64 65 72 20 74 6f 20 74 68 65 20 74 6f  Reader to the to
3543a 70 20 6f 66 20 74 68 65 20 66 69 6c 65 2c 20 6f  p of the file, o
3543b 72 20 74 6f 0a 2a 2a 20 61 6e 6f 74 68 65 72 20  r to.** another 
3543c 66 69 6c 65 2c 20 74 65 72 6d 5f 73 65 6c 65 63  file, term_selec
3543d 74 28 29 20 63 6f 75 6c 64 20 62 65 20 70 75 73  t() could be pus
3543e 68 65 64 20 61 62 6f 76 65 0a 2a 2a 20 64 6f 63  hed above.** doc
3543f 4c 69 73 74 4f 66 54 65 72 6d 28 29 2e 0a 2a 2f  ListOfTerm()..*/
35440 0a 73 74 61 74 69 63 20 69 6e 74 20 74 65 72 6d  .static int term
35441 53 65 6c 65 63 74 28 66 75 6c 6c 74 65 78 74 5f  Select(fulltext_
35442 76 74 61 62 20 2a 76 2c 20 69 6e 74 20 69 43 6f  vtab *v, int iCo
35443 6c 75 6d 6e 2c 0a 20 20 20 20 20 20 20 20 20 20  lumn,.          
35444 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73              cons
35445 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69  t char *pTerm, i
35446 6e 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69 73  nt nTerm, int is
35447 50 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20 20  Prefix,.        
35448 20 20 20 20 20 20 20 20 20 20 20 20 20 20 44 6f                Do
35449 63 4c 69 73 74 54 79 70 65 20 69 54 79 70 65 2c  cListType iType,
3544a 20 44 61 74 61 42 75 66 66 65 72 20 2a 6f 75 74   DataBuffer *out
3544b 29 3b 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72  );../* .** Retur
3544c 6e 20 61 20 44 6f 63 4c 69 73 74 20 63 6f 72 72  n a DocList corr
3544d 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65  esponding to the
3544e 20 70 68 72 61 73 65 20 2a 70 50 68 72 61 73 65   phrase *pPhrase
3544f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75  ..**.** The resu
35450 6c 74 69 6e 67 20 44 4c 5f 44 4f 43 49 44 53 20  lting DL_DOCIDS 
35451 64 6f 63 6c 69 73 74 20 69 73 20 73 74 6f 72 65  doclist is store
35452 64 20 69 6e 20 70 52 65 73 75 6c 74 2c 20 77 68  d in pResult, wh
35453 69 63 68 20 69 73 0a 2a 2a 20 6f 76 65 72 77 72  ich is.** overwr
35454 69 74 74 65 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  itten..*/.static
35455 20 69 6e 74 20 64 6f 63 4c 69 73 74 4f 66 50 68   int docListOfPh
35456 72 61 73 65 28 0a 20 20 66 75 6c 6c 74 65 78 74  rase(.  fulltext
35457 5f 76 74 61 62 20 2a 70 54 61 62 2c 20 20 20 2f  _vtab *pTab,   /
35458 2a 20 54 68 65 20 66 75 6c 6c 20 74 65 78 74 20  * The full text 
35459 69 6e 64 65 78 20 2a 2f 0a 20 20 46 74 73 33 50  index */.  Fts3P
3545a 68 72 61 73 65 20 2a 70 50 68 72 61 73 65 2c 20  hrase *pPhrase, 
3545b 20 20 2f 2a 20 50 68 72 61 73 65 20 74 6f 20 72    /* Phrase to r
3545c 65 74 75 72 6e 20 61 20 64 6f 63 6c 69 73 74 20  eturn a doclist 
3545d 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f  corresponding to
3545e 20 2a 2f 0a 20 20 44 6f 63 4c 69 73 74 54 79 70   */.  DocListTyp
3545f 65 20 65 4c 69 73 74 54 79 70 65 2c 20 2f 2a 20  e eListType, /* 
35460 45 69 74 68 65 72 20 44 4c 5f 44 4f 43 49 44 53  Either DL_DOCIDS
35461 20 6f 72 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53   or DL_POSITIONS
35462 20 2a 2f 0a 20 20 44 61 74 61 42 75 66 66 65 72   */.  DataBuffer
35463 20 2a 70 52 65 73 75 6c 74 20 20 20 20 2f 2a 20   *pResult    /* 
35464 57 72 69 74 65 20 74 68 65 20 72 65 73 75 6c 74  Write the result
35465 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e   here */.){.  in
35466 74 20 69 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d  t ii;.  int rc =
35467 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e   SQLITE_OK;.  in
35468 74 20 69 43 6f 6c 20 3d 20 70 50 68 72 61 73 65  t iCol = pPhrase
35469 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 44 6f 63  ->iColumn;.  Doc
3546a 4c 69 73 74 54 79 70 65 20 65 54 79 70 65 20 3d  ListType eType =
3546b 20 65 4c 69 73 74 54 79 70 65 3b 0a 20 20 61 73   eListType;.  as
3546c 73 65 72 74 28 20 65 54 79 70 65 3d 3d 44 4c 5f  sert( eType==DL_
3546d 50 4f 53 49 54 49 4f 4e 53 20 7c 7c 20 65 54 79  POSITIONS || eTy
3546e 70 65 3d 3d 44 4c 5f 44 4f 43 49 44 53 20 29 3b  pe==DL_DOCIDS );
3546f 0a 20 20 69 66 28 20 70 50 68 72 61 73 65 2d 3e  .  if( pPhrase->
35470 6e 54 6f 6b 65 6e 3e 31 20 29 7b 0a 20 20 20 20  nToken>1 ){.    
35471 65 54 79 70 65 20 3d 20 44 4c 5f 50 4f 53 49 54  eType = DL_POSIT
35472 49 4f 4e 53 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  IONS;.  }..  /* 
35473 54 68 69 73 20 63 6f 64 65 20 73 68 6f 75 6c 64  This code should
35474 20 6e 65 76 65 72 20 62 65 20 63 61 6c 6c 65 64   never be called
35475 20 77 69 74 68 20 62 75 66 66 65 72 65 64 20 75   with buffered u
35476 70 64 61 74 65 73 2e 20 2a 2f 0a 20 20 61 73 73  pdates. */.  ass
35477 65 72 74 28 20 70 54 61 62 2d 3e 6e 50 65 6e 64  ert( pTab->nPend
35478 69 6e 67 44 61 74 61 3c 30 20 29 3b 0a 0a 20 20  ingData<0 );..  
35479 66 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d 53 51  for(ii=0; rc==SQ
3547a 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c 70 50  LITE_OK && ii<pP
3547b 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 20 69  hrase->nToken; i
3547c 69 2b 2b 29 7b 0a 20 20 20 20 44 61 74 61 42 75  i++){.    DataBu
3547d 66 66 65 72 20 74 6d 70 3b 0a 20 20 20 20 73 74  ffer tmp;.    st
3547e 72 75 63 74 20 50 68 72 61 73 65 54 6f 6b 65 6e  ruct PhraseToken
3547f 20 2a 70 20 3d 20 26 70 50 68 72 61 73 65 2d 3e   *p = &pPhrase->
35480 61 54 6f 6b 65 6e 5b 69 69 5d 3b 0a 20 20 20 20  aToken[ii];.    
35481 72 63 20 3d 20 74 65 72 6d 53 65 6c 65 63 74 28  rc = termSelect(
35482 70 54 61 62 2c 20 69 43 6f 6c 2c 20 70 2d 3e 7a  pTab, iCol, p->z
35483 2c 20 70 2d 3e 6e 2c 20 70 2d 3e 69 73 50 72 65  , p->n, p->isPre
35484 66 69 78 2c 20 65 54 79 70 65 2c 20 26 74 6d 70  fix, eType, &tmp
35485 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
35486 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
35487 20 20 69 66 28 20 69 69 3d 3d 30 20 29 7b 0a 20    if( ii==0 ){. 
35488 20 20 20 20 20 20 20 2a 70 52 65 73 75 6c 74 20         *pResult 
35489 3d 20 74 6d 70 3b 0a 20 20 20 20 20 20 7d 65 6c  = tmp;.      }el
3548a 73 65 7b 0a 20 20 20 20 20 20 20 20 44 61 74 61  se{.        Data
3548b 42 75 66 66 65 72 20 72 65 73 20 3d 20 2a 70 52  Buffer res = *pR
3548c 65 73 75 6c 74 3b 0a 20 20 20 20 20 20 20 20 64  esult;.        d
3548d 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 70 52  ataBufferInit(pR
3548e 65 73 75 6c 74 2c 20 30 29 3b 0a 20 20 20 20 20  esult, 0);.     
3548f 20 20 20 69 66 28 20 69 69 3d 3d 28 70 50 68 72     if( ii==(pPhr
35490 61 73 65 2d 3e 6e 54 6f 6b 65 6e 2d 31 29 20 29  ase->nToken-1) )
35491 7b 0a 20 20 20 20 20 20 20 20 20 20 65 54 79 70  {.          eTyp
35492 65 20 3d 20 65 4c 69 73 74 54 79 70 65 3b 0a 20  e = eListType;. 
35493 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
35494 20 64 6f 63 4c 69 73 74 50 68 72 61 73 65 4d 65   docListPhraseMe
35495 72 67 65 28 0a 20 20 20 20 20 20 20 20 20 20 72  rge(.          r
35496 65 73 2e 70 44 61 74 61 2c 20 72 65 73 2e 6e 44  es.pData, res.nD
35497 61 74 61 2c 20 74 6d 70 2e 70 44 61 74 61 2c 20  ata, tmp.pData, 
35498 74 6d 70 2e 6e 44 61 74 61 2c 20 30 2c 20 30 2c  tmp.nData, 0, 0,
35499 20 65 54 79 70 65 2c 20 70 52 65 73 75 6c 74 0a   eType, pResult.
3549a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20          );.     
3549b 20 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73     dataBufferDes
3549c 74 72 6f 79 28 26 72 65 73 29 3b 0a 20 20 20 20  troy(&res);.    
3549d 20 20 20 20 64 61 74 61 42 75 66 66 65 72 44 65      dataBufferDe
3549e 73 74 72 6f 79 28 26 74 6d 70 29 3b 0a 20 20 20  stroy(&tmp);.   
3549f 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
354a0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
354a1 2f 2a 0a 2a 2a 20 45 76 61 6c 75 61 74 65 20 74  /*.** Evaluate t
354a2 68 65 20 66 75 6c 6c 2d 74 65 78 74 20 65 78 70  he full-text exp
354a3 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 61 67  ression pExpr ag
354a4 61 69 6e 73 74 20 66 74 73 33 20 74 61 62 6c 65  ainst fts3 table
354a5 20 70 54 61 62 2e 20 57 72 69 74 65 0a 2a 2a 20   pTab. Write.** 
354a6 74 68 65 20 72 65 73 75 6c 74 73 20 69 6e 74 6f  the results into
354a7 20 70 52 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63   pRes..*/.static
354a8 20 69 6e 74 20 65 76 61 6c 46 74 73 33 45 78 70   int evalFts3Exp
354a9 72 28 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74  r(.  fulltext_vt
354aa 61 62 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20  ab *pTab,       
354ab 20 20 20 20 2f 2a 20 46 74 73 33 20 56 69 72 74      /* Fts3 Virt
354ac 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 74  ual table object
354ad 20 2a 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a   */.  Fts3Expr *
354ae 70 45 78 70 72 2c 20 20 20 20 20 20 20 20 20 20  pExpr,          
354af 20 20 20 20 20 2f 2a 20 50 61 72 73 65 64 20 66       /* Parsed f
354b0 74 73 33 20 65 78 70 72 65 73 73 69 6f 6e 20 2a  ts3 expression *
354b1 2f 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 2a  /.  DataBuffer *
354b2 70 52 65 73 20 20 20 20 20 20 20 20 20 20 20 20  pRes            
354b3 20 20 20 2f 2a 20 4f 55 54 3a 20 57 72 69 74 65     /* OUT: Write
354b4 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20   results of the 
354b5 65 78 70 72 65 73 73 69 6f 6e 20 68 65 72 65 20  expression here 
354b6 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  */.){.  int rc =
354b7 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f   SQLITE_OK;..  /
354b8 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  * Initialize the
354b9 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 2e 20   output buffer. 
354ba 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 65 6d  If this is an em
354bb 70 74 79 20 71 75 65 72 79 20 28 70 45 78 70 72  pty query (pExpr
354bc 3d 3d 30 29 2c 20 0a 20 20 2a 2a 20 74 68 69 73  ==0), .  ** this
354bd 20 69 73 20 61 6c 6c 20 74 68 61 74 20 6e 65 65   is all that nee
354be 64 73 20 74 6f 20 62 65 20 64 6f 6e 65 2e 20 45  ds to be done. E
354bf 6d 70 74 79 20 71 75 65 72 69 65 73 20 70 72 6f  mpty queries pro
354c0 64 75 63 65 20 65 6d 70 74 79 20 0a 20 20 2a 2a  duce empty .  **
354c1 20 72 65 73 75 6c 74 20 73 65 74 73 2e 0a 20 20   result sets..  
354c2 2a 2f 0a 20 20 64 61 74 61 42 75 66 66 65 72 49  */.  dataBufferI
354c3 6e 69 74 28 70 52 65 73 2c 20 30 29 3b 0a 0a 20  nit(pRes, 0);.. 
354c4 20 69 66 28 20 70 45 78 70 72 20 29 7b 0a 20 20   if( pExpr ){.  
354c5 20 20 69 66 28 20 70 45 78 70 72 2d 3e 65 54 79    if( pExpr->eTy
354c6 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 50 48 52  pe==FTSQUERY_PHR
354c7 41 53 45 20 29 7b 0a 20 20 20 20 20 20 44 6f 63  ASE ){.      Doc
354c8 4c 69 73 74 54 79 70 65 20 65 54 79 70 65 20 3d  ListType eType =
354c9 20 44 4c 5f 44 4f 43 49 44 53 3b 0a 20 20 20 20   DL_DOCIDS;.    
354ca 20 20 69 66 28 20 70 45 78 70 72 2d 3e 70 50 61    if( pExpr->pPa
354cb 72 65 6e 74 20 26 26 20 70 45 78 70 72 2d 3e 70  rent && pExpr->p
354cc 50 61 72 65 6e 74 2d 3e 65 54 79 70 65 3d 3d 46  Parent->eType==F
354cd 54 53 51 55 45 52 59 5f 4e 45 41 52 20 29 7b 0a  TSQUERY_NEAR ){.
354ce 20 20 20 20 20 20 20 20 65 54 79 70 65 20 3d 20          eType = 
354cf 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 3b 0a 20 20  DL_POSITIONS;.  
354d0 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d      }.      rc =
354d1 20 64 6f 63 4c 69 73 74 4f 66 50 68 72 61 73 65   docListOfPhrase
354d2 28 70 54 61 62 2c 20 70 45 78 70 72 2d 3e 70 50  (pTab, pExpr->pP
354d3 68 72 61 73 65 2c 20 65 54 79 70 65 2c 20 70 52  hrase, eType, pR
354d4 65 73 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  es);.    }else{.
354d5 20 20 20 20 20 20 44 61 74 61 42 75 66 66 65 72        DataBuffer
354d6 20 6c 68 73 3b 0a 20 20 20 20 20 20 44 61 74 61   lhs;.      Data
354d7 42 75 66 66 65 72 20 72 68 73 3b 0a 0a 20 20 20  Buffer rhs;..   
354d8 20 20 20 64 61 74 61 42 75 66 66 65 72 49 6e 69     dataBufferIni
354d9 74 28 26 72 68 73 2c 20 30 29 3b 0a 20 20 20 20  t(&rhs, 0);.    
354da 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d    if( SQLITE_OK=
354db 3d 28 72 63 20 3d 20 65 76 61 6c 46 74 73 33 45  =(rc = evalFts3E
354dc 78 70 72 28 70 54 61 62 2c 20 70 45 78 70 72 2d  xpr(pTab, pExpr-
354dd 3e 70 4c 65 66 74 2c 20 26 6c 68 73 29 29 20 0a  >pLeft, &lhs)) .
354de 20 20 20 20 20 20 20 26 26 20 53 51 4c 49 54 45         && SQLITE
354df 5f 4f 4b 3d 3d 28 72 63 20 3d 20 65 76 61 6c 46  _OK==(rc = evalF
354e0 74 73 33 45 78 70 72 28 70 54 61 62 2c 20 70 45  ts3Expr(pTab, pE
354e1 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 68  xpr->pRight, &rh
354e2 73 29 29 20 0a 20 20 20 20 20 20 29 7b 0a 20 20  s)) .      ){.  
354e3 20 20 20 20 20 20 73 77 69 74 63 68 28 20 70 45        switch( pE
354e4 78 70 72 2d 3e 65 54 79 70 65 20 29 7b 0a 20 20  xpr->eType ){.  
354e5 20 20 20 20 20 20 20 20 63 61 73 65 20 46 54 53          case FTS
354e6 51 55 45 52 59 5f 4e 45 41 52 3a 20 7b 0a 20 20  QUERY_NEAR: {.  
354e7 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 54            int nT
354e8 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 20 20  oken;.          
354e9 20 20 46 74 73 33 45 78 70 72 20 2a 70 4c 65 66    Fts3Expr *pLef
354ea 74 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 44  t;.            D
354eb 6f 63 4c 69 73 74 54 79 70 65 20 65 54 79 70 65  ocListType eType
354ec 20 3d 20 44 4c 5f 44 4f 43 49 44 53 3b 0a 20 20   = DL_DOCIDS;.  
354ed 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 45            if( pE
354ee 78 70 72 2d 3e 70 50 61 72 65 6e 74 20 26 26 20  xpr->pParent && 
354ef 70 45 78 70 72 2d 3e 70 50 61 72 65 6e 74 2d 3e  pExpr->pParent->
354f0 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f  eType==FTSQUERY_
354f1 4e 45 41 52 20 29 7b 0a 20 20 20 20 20 20 20 20  NEAR ){.        
354f2 20 20 20 20 20 20 65 54 79 70 65 20 3d 20 44 4c        eType = DL
354f3 5f 50 4f 53 49 54 49 4f 4e 53 3b 0a 20 20 20 20  _POSITIONS;.    
354f4 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
354f5 20 20 20 20 20 20 70 4c 65 66 74 20 3d 20 70 45        pLeft = pE
354f6 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20  xpr->pLeft;.    
354f7 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70          while( p
354f8 4c 65 66 74 2d 3e 65 54 79 70 65 3d 3d 46 54 53  Left->eType==FTS
354f9 51 55 45 52 59 5f 4e 45 41 52 20 29 7b 20 0a 20  QUERY_NEAR ){ . 
354fa 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4c 65               pLe
354fb 66 74 3d 70 4c 65 66 74 2d 3e 70 52 69 67 68 74  ft=pLeft->pRight
354fc 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
354fd 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65              asse
354fe 72 74 28 20 70 45 78 70 72 2d 3e 70 52 69 67 68  rt( pExpr->pRigh
354ff 74 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45  t->eType==FTSQUE
35500 52 59 5f 50 48 52 41 53 45 20 29 3b 0a 20 20 20  RY_PHRASE );.   
35501 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
35502 20 70 4c 65 66 74 2d 3e 65 54 79 70 65 3d 3d 46   pLeft->eType==F
35503 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 29  TSQUERY_PHRASE )
35504 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 54  ;.            nT
35505 6f 6b 65 6e 20 3d 20 70 4c 65 66 74 2d 3e 70 50  oken = pLeft->pP
35506 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 20 2b 20  hrase->nToken + 
35507 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2d 3e 70  pExpr->pRight->p
35508 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 0a  Phrase->nToken;.
35509 20 20 20 20 20 20 20 20 20 20 20 20 64 6f 63 4c              docL
3550a 69 73 74 50 68 72 61 73 65 4d 65 72 67 65 28 6c  istPhraseMerge(l
3550b 68 73 2e 70 44 61 74 61 2c 20 6c 68 73 2e 6e 44  hs.pData, lhs.nD
3550c 61 74 61 2c 20 72 68 73 2e 70 44 61 74 61 2c 20  ata, rhs.pData, 
3550d 72 68 73 2e 6e 44 61 74 61 2c 20 0a 20 20 20 20  rhs.nData, .    
3550e 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70              pExp
3550f 72 2d 3e 6e 4e 65 61 72 2b 31 2c 20 6e 54 6f 6b  r->nNear+1, nTok
35510 65 6e 2c 20 65 54 79 70 65 2c 20 70 52 65 73 0a  en, eType, pRes.
35511 20 20 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20              );. 
35512 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
35513 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
35514 20 20 20 20 20 20 20 20 63 61 73 65 20 46 54 53          case FTS
35515 51 55 45 52 59 5f 4e 4f 54 3a 20 7b 0a 20 20 20  QUERY_NOT: {.   
35516 20 20 20 20 20 20 20 20 20 64 6f 63 4c 69 73 74           docList
35517 45 78 63 65 70 74 4d 65 72 67 65 28 6c 68 73 2e  ExceptMerge(lhs.
35518 70 44 61 74 61 2c 20 6c 68 73 2e 6e 44 61 74 61  pData, lhs.nData
35519 2c 20 72 68 73 2e 70 44 61 74 61 2c 20 72 68 73  , rhs.pData, rhs
3551a 2e 6e 44 61 74 61 2c 70 52 65 73 29 3b 0a 20 20  .nData,pRes);.  
3551b 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
3551c 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
3551d 20 20 20 20 20 20 20 63 61 73 65 20 46 54 53 51         case FTSQ
3551e 55 45 52 59 5f 41 4e 44 3a 20 7b 0a 20 20 20 20  UERY_AND: {.    
3551f 20 20 20 20 20 20 20 20 64 6f 63 4c 69 73 74 41          docListA
35520 6e 64 4d 65 72 67 65 28 6c 68 73 2e 70 44 61 74  ndMerge(lhs.pDat
35521 61 2c 20 6c 68 73 2e 6e 44 61 74 61 2c 20 72 68  a, lhs.nData, rh
35522 73 2e 70 44 61 74 61 2c 20 72 68 73 2e 6e 44 61  s.pData, rhs.nDa
35523 74 61 2c 20 70 52 65 73 29 3b 0a 20 20 20 20 20  ta, pRes);.     
35524 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
35525 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
35526 20 20 20 20 63 61 73 65 20 46 54 53 51 55 45 52      case FTSQUER
35527 59 5f 4f 52 3a 20 7b 0a 20 20 20 20 20 20 20 20  Y_OR: {.        
35528 20 20 20 20 64 6f 63 4c 69 73 74 4f 72 4d 65 72      docListOrMer
35529 67 65 28 6c 68 73 2e 70 44 61 74 61 2c 20 6c 68  ge(lhs.pData, lh
3552a 73 2e 6e 44 61 74 61 2c 20 72 68 73 2e 70 44 61  s.nData, rhs.pDa
3552b 74 61 2c 20 72 68 73 2e 6e 44 61 74 61 2c 20 70  ta, rhs.nData, p
3552c 52 65 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20  Res);.          
3552d 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
3552e 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
3552f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 61 74       }.      dat
35530 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26  aBufferDestroy(&
35531 6c 68 73 29 3b 0a 20 20 20 20 20 20 64 61 74 61  lhs);.      data
35532 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 72  BufferDestroy(&r
35533 68 73 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  hs);.    }.  }..
35534 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
35535 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 52  /* TODO(shess) R
35536 65 66 61 63 74 6f 72 20 74 68 65 20 63 6f 64 65  efactor the code
35537 20 74 6f 20 72 65 6d 6f 76 65 20 74 68 69 73 20   to remove this 
35538 66 6f 72 77 61 72 64 20 64 65 63 6c 2e 20 2a 2f  forward decl. */
35539 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c 75 73  .static int flus
3553a 68 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 66 75  hPendingTerms(fu
3553b 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 29 3b  lltext_vtab *v);
3553c 0a 0a 2f 2a 20 50 65 72 66 6f 72 6d 20 61 20 66  ../* Perform a f
3553d 75 6c 6c 2d 74 65 78 74 20 71 75 65 72 79 20 75  ull-text query u
3553e 73 69 6e 67 20 74 68 65 20 73 65 61 72 63 68 20  sing the search 
3553f 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 0a 2a 2a  expression in.**
35540 20 7a 49 6e 70 75 74 5b 30 2e 2e 6e 49 6e 70 75   zInput[0..nInpu
35541 74 2d 31 5d 2e 20 20 52 65 74 75 72 6e 20 61 20  t-1].  Return a 
35542 6c 69 73 74 20 6f 66 20 6d 61 74 63 68 69 6e 67  list of matching
35543 20 64 6f 63 75 6d 65 6e 74 73 0a 2a 2a 20 69 6e   documents.** in
35544 20 70 52 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20   pResult..**.** 
35545 51 75 65 72 69 65 73 20 6d 75 73 74 20 6d 61 74  Queries must mat
35546 63 68 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 75 6d  ch column iColum
35547 6e 2e 20 20 4f 72 20 69 66 20 69 43 6f 6c 75 6d  n.  Or if iColum
35548 6e 3e 3d 6e 43 6f 6c 75 6d 6e 0a 2a 2a 20 74 68  n>=nColumn.** th
35549 65 79 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 74  ey are allowed t
3554a 6f 20 6d 61 74 63 68 20 61 67 61 69 6e 73 74 20  o match against 
3554b 61 6e 79 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 73  any column..*/.s
3554c 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65  tatic int fullte
3554d 78 74 51 75 65 72 79 28 0a 20 20 66 75 6c 6c 74  xtQuery(.  fullt
3554e 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 20 20 20  ext_vtab *v,    
3554f 20 20 2f 2a 20 54 68 65 20 66 75 6c 6c 20 74 65    /* The full te
35550 78 74 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e  xt index */.  in
35551 74 20 69 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20  t iColumn,      
35552 20 20 20 20 20 2f 2a 20 4d 61 74 63 68 20 61 67       /* Match ag
35553 61 69 6e 73 74 20 74 68 69 73 20 63 6f 6c 75 6d  ainst this colum
35554 6e 20 62 79 20 64 65 66 61 75 6c 74 20 2a 2f 0a  n by default */.
35555 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49    const char *zI
35556 6e 70 75 74 2c 20 20 20 20 2f 2a 20 54 68 65 20  nput,    /* The 
35557 71 75 65 72 79 20 73 74 72 69 6e 67 20 2a 2f 0a  query string */.
35558 20 20 69 6e 74 20 6e 49 6e 70 75 74 2c 20 20 20    int nInput,   
35559 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
3555a 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 7a  er of bytes in z
3555b 49 6e 70 75 74 5b 5d 20 2a 2f 0a 20 20 44 61 74  Input[] */.  Dat
3555c 61 42 75 66 66 65 72 20 2a 70 52 65 73 75 6c 74  aBuffer *pResult
3555d 2c 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65  ,   /* Write the
3555e 20 72 65 73 75 6c 74 20 64 6f 63 6c 69 73 74 20   result doclist 
3555f 68 65 72 65 20 2a 2f 0a 20 20 46 74 73 33 45 78  here */.  Fts3Ex
35560 70 72 20 2a 2a 70 70 45 78 70 72 20 20 20 20 20  pr **ppExpr     
35561 20 20 20 2f 2a 20 50 75 74 20 70 61 72 73 65 64     /* Put parsed
35562 20 71 75 65 72 79 20 73 74 72 69 6e 67 20 68 65   query string he
35563 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  re */.){.  int r
35564 63 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68  c;..  /* TODO(sh
35565 65 73 73 29 20 49 6e 73 74 65 61 64 20 6f 66 20  ess) Instead of 
35566 66 6c 75 73 68 69 6e 67 20 70 65 6e 64 69 6e 67  flushing pending
35567 54 65 72 6d 73 2c 20 77 65 20 63 6f 75 6c 64 20  Terms, we could 
35568 71 75 65 72 79 20 66 6f 72 0a 20 20 2a 2a 20 74  query for.  ** t
35569 68 65 20 72 65 6c 65 76 61 6e 74 20 74 65 72 6d  he relevant term
3556a 20 61 6e 64 20 6d 65 72 67 65 20 74 68 65 20 64   and merge the d
3556b 6f 63 6c 69 73 74 20 69 6e 74 6f 20 77 68 61 74  oclist into what
3556c 20 77 65 20 72 65 63 65 69 76 65 20 66 72 6f 6d   we receive from
3556d 0a 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61  .  ** the databa
3556e 73 65 2e 20 20 57 61 69 74 20 61 6e 64 20 73 65  se.  Wait and se
3556f 65 20 69 66 20 74 68 69 73 20 69 73 20 61 20 63  e if this is a c
35570 6f 6d 6d 6f 6e 20 69 73 73 75 65 2c 20 66 69 72  ommon issue, fir
35571 73 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 20  st..  **.  ** A 
35572 67 6f 6f 64 20 72 65 61 73 6f 6e 20 6e 6f 74 20  good reason not 
35573 74 6f 20 66 6c 75 73 68 20 69 73 20 74 6f 20 6e  to flush is to n
35574 6f 74 20 67 65 6e 65 72 61 74 65 20 75 70 64 61  ot generate upda
35575 74 65 2d 72 65 6c 61 74 65 64 0a 20 20 2a 2a 20  te-related.  ** 
35576 65 72 72 6f 72 20 63 6f 64 65 73 20 66 72 6f 6d  error codes from
35577 20 68 65 72 65 2e 0a 20 20 2a 2f 0a 0a 20 20 2f   here..  */..  /
35578 2a 20 46 6c 75 73 68 20 61 6e 79 20 62 75 66 66  * Flush any buff
35579 65 72 65 64 20 75 70 64 61 74 65 73 20 62 65 66  ered updates bef
3557a 6f 72 65 20 65 78 65 63 75 74 69 6e 67 20 74 68  ore executing th
3557b 65 20 71 75 65 72 79 2e 20 2a 2f 0a 20 20 72 63  e query. */.  rc
3557c 20 3d 20 66 6c 75 73 68 50 65 6e 64 69 6e 67 54   = flushPendingT
3557d 65 72 6d 73 28 76 29 3b 0a 20 20 69 66 28 20 72  erms(v);.  if( r
3557e 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
3557f 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
35580 20 7d 0a 0a 20 20 2f 2a 20 50 61 72 73 65 20 74   }..  /* Parse t
35581 68 65 20 71 75 65 72 79 20 70 61 73 73 65 64 20  he query passed 
35582 74 6f 20 74 68 65 20 4d 41 54 43 48 20 6f 70 65  to the MATCH ope
35583 72 61 74 6f 72 2e 20 2a 2f 0a 20 20 72 63 20 3d  rator. */.  rc =
35584 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72   sqlite3Fts3Expr
35585 50 61 72 73 65 28 76 2d 3e 70 54 6f 6b 65 6e 69  Parse(v->pTokeni
35586 7a 65 72 2c 20 0a 20 20 20 20 20 20 76 2d 3e 61  zer, .      v->a
35587 7a 43 6f 6c 75 6d 6e 2c 20 76 2d 3e 6e 43 6f 6c  zColumn, v->nCol
35588 75 6d 6e 2c 20 69 43 6f 6c 75 6d 6e 2c 20 7a 49  umn, iColumn, zI
35589 6e 70 75 74 2c 20 6e 49 6e 70 75 74 2c 20 70 70  nput, nInput, pp
3558a 45 78 70 72 0a 20 20 29 3b 0a 20 20 69 66 28 20  Expr.  );.  if( 
3558b 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
3558c 0a 20 20 20 20 61 73 73 65 72 74 28 20 30 3d 3d  .    assert( 0==
3558d 28 2a 70 70 45 78 70 72 29 20 29 3b 0a 20 20 20  (*ppExpr) );.   
3558e 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
3558f 0a 20 20 72 65 74 75 72 6e 20 65 76 61 6c 46 74  .  return evalFt
35590 73 33 45 78 70 72 28 76 2c 20 2a 70 70 45 78 70  s3Expr(v, *ppExp
35591 72 2c 20 70 52 65 73 75 6c 74 29 3b 0a 7d 0a 0a  r, pResult);.}..
35592 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68  /*.** This is th
35593 65 20 78 46 69 6c 74 65 72 20 69 6e 74 65 72 66  e xFilter interf
35594 61 63 65 20 66 6f 72 20 74 68 65 20 76 69 72 74  ace for the virt
35595 75 61 6c 20 74 61 62 6c 65 2e 20 20 53 65 65 0a  ual table.  See.
35596 2a 2a 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  ** the virtual t
35597 61 62 6c 65 20 78 46 69 6c 74 65 72 20 6d 65 74  able xFilter met
35598 68 6f 64 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f  hod documentatio
35599 6e 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  n for additional
3559a 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  .** information.
3559b 0a 2a 2a 0a 2a 2a 20 49 66 20 69 64 78 4e 75 6d  .**.** If idxNum
3559c 3d 3d 51 55 45 52 59 5f 47 45 4e 45 52 49 43 20  ==QUERY_GENERIC 
3559d 74 68 65 6e 20 64 6f 20 61 20 66 75 6c 6c 20 74  then do a full t
3559e 61 62 6c 65 20 73 63 61 6e 20 61 67 61 69 6e 73  able scan agains
3559f 74 0a 2a 2a 20 74 68 65 20 25 5f 63 6f 6e 74 65  t.** the %_conte
355a0 6e 74 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  nt table..**.** 
355a1 49 66 20 69 64 78 4e 75 6d 3d 3d 51 55 45 52 59  If idxNum==QUERY
355a2 5f 44 4f 43 49 44 20 74 68 65 6e 20 64 6f 20 61  _DOCID then do a
355a3 20 64 6f 63 69 64 20 6c 6f 6f 6b 75 70 20 66 6f   docid lookup fo
355a4 72 20 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 79  r a single entry
355a5 0a 2a 2a 20 69 6e 20 74 68 65 20 25 5f 63 6f 6e  .** in the %_con
355a6 74 65 6e 74 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a  tent table..**.*
355a7 2a 20 49 66 20 69 64 78 4e 75 6d 3e 3d 51 55 45  * If idxNum>=QUE
355a8 52 59 5f 46 55 4c 4c 54 45 58 54 20 74 68 65 6e  RY_FULLTEXT then
355a9 20 75 73 65 20 74 68 65 20 66 75 6c 6c 20 74 65   use the full te
355aa 78 74 20 69 6e 64 65 78 2e 20 20 54 68 65 0a 2a  xt index.  The.*
355ab 2a 20 63 6f 6c 75 6d 6e 20 6f 6e 20 74 68 65 20  * column on the 
355ac 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6f  left-hand side o
355ad 66 20 74 68 65 20 4d 41 54 43 48 20 6f 70 65 72  f the MATCH oper
355ae 61 74 6f 72 20 69 73 20 63 6f 6c 75 6d 6e 0a 2a  ator is column.*
355af 2a 20 6e 75 6d 62 65 72 20 69 64 78 4e 75 6d 2d  * number idxNum-
355b0 51 55 45 52 59 5f 46 55 4c 4c 54 45 58 54 2c 20  QUERY_FULLTEXT, 
355b1 30 20 69 6e 64 65 78 65 64 2e 20 20 61 72 67 76  0 indexed.  argv
355b2 5b 30 5d 20 69 73 20 74 68 65 20 72 69 67 68 74  [0] is the right
355b3 2d 68 61 6e 64 0a 2a 2a 20 73 69 64 65 20 6f 66  -hand.** side of
355b4 20 74 68 65 20 4d 41 54 43 48 20 6f 70 65 72 61   the MATCH opera
355b5 74 6f 72 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28  tor..*/./* TODO(
355b6 73 68 65 73 73 29 20 55 70 67 72 61 64 65 20 74  shess) Upgrade t
355b7 68 65 20 63 75 72 73 6f 72 20 69 6e 69 74 69 61  he cursor initia
355b8 6c 69 7a 61 74 69 6f 6e 20 61 6e 64 20 64 65 73  lization and des
355b9 74 72 75 63 74 69 6f 6e 20 74 6f 0a 2a 2a 20 61  truction to.** a
355ba 63 63 6f 75 6e 74 20 66 6f 72 20 66 75 6c 6c 74  ccount for fullt
355bb 65 78 74 46 69 6c 74 65 72 28 29 20 62 65 69 6e  extFilter() bein
355bc 67 20 63 61 6c 6c 65 64 20 6d 75 6c 74 69 70 6c  g called multipl
355bd 65 20 74 69 6d 65 73 20 6f 6e 20 74 68 65 0a 2a  e times on the.*
355be 2a 20 73 61 6d 65 20 63 75 72 73 6f 72 2e 20 20  * same cursor.  
355bf 54 68 65 20 63 75 72 72 65 6e 74 20 73 6f 6c 75  The current solu
355c0 74 69 6f 6e 20 69 73 20 76 65 72 79 20 66 72 61  tion is very fra
355c1 67 69 6c 65 2e 20 20 41 70 70 6c 79 20 66 69 78  gile.  Apply fix
355c2 20 74 6f 0a 2a 2a 20 66 74 73 33 20 61 73 20 61   to.** fts3 as a
355c3 70 70 72 6f 70 72 69 61 74 65 2e 0a 2a 2f 0a 73  ppropriate..*/.s
355c4 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65  tatic int fullte
355c5 78 74 46 69 6c 74 65 72 28 0a 20 20 73 71 6c 69  xtFilter(.  sqli
355c6 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
355c7 2a 70 43 75 72 73 6f 72 2c 20 20 20 20 20 2f 2a  *pCursor,     /*
355c8 20 54 68 65 20 63 75 72 73 6f 72 20 75 73 65 64   The cursor used
355c9 20 66 6f 72 20 74 68 69 73 20 71 75 65 72 79 20   for this query 
355ca 2a 2f 0a 20 20 69 6e 74 20 69 64 78 4e 75 6d 2c  */.  int idxNum,
355cb 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 64 78   const char *idx
355cc 53 74 72 2c 20 20 20 2f 2a 20 57 68 69 63 68 20  Str,   /* Which 
355cd 69 6e 64 65 78 69 6e 67 20 73 63 68 65 6d 65 20  indexing scheme 
355ce 74 6f 20 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20  to use */.  int 
355cf 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
355d0 6c 75 65 20 2a 2a 61 72 67 76 20 20 20 20 2f 2a  lue **argv    /*
355d1 20 41 72 67 75 6d 65 6e 74 73 20 66 6f 72 20 74   Arguments for t
355d2 68 65 20 69 6e 64 65 78 69 6e 67 20 73 63 68 65  he indexing sche
355d3 6d 65 20 2a 2f 0a 29 7b 0a 20 20 66 75 6c 6c 74  me */.){.  fullt
355d4 65 78 74 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20  ext_cursor *c = 
355d5 28 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72  (fulltext_cursor
355d6 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a 20 20 66   *) pCursor;.  f
355d7 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 20  ulltext_vtab *v 
355d8 3d 20 63 75 72 73 6f 72 5f 76 74 61 62 28 63 29  = cursor_vtab(c)
355d9 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 46  ;.  int rc;..  F
355da 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20 46  TSTRACE(("FTS3 F
355db 69 6c 74 65 72 20 25 70 5c 6e 22 2c 70 43 75 72  ilter %p\n",pCur
355dc 73 6f 72 29 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  sor));..  /* If 
355dd 74 68 65 20 63 75 72 73 6f 72 20 68 61 73 20 61  the cursor has a
355de 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20   statement that 
355df 77 61 73 20 6e 6f 74 20 70 72 65 70 61 72 65 64  was not prepared
355e0 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 0a 20 20   according to.  
355e1 2a 2a 20 69 64 78 4e 75 6d 2c 20 63 6c 65 61 72  ** idxNum, clear
355e2 20 69 74 2e 20 20 49 20 62 65 6c 69 65 76 65 20   it.  I believe 
355e3 61 6c 6c 20 63 61 6c 6c 73 20 74 6f 20 66 75 6c  all calls to ful
355e4 6c 74 65 78 74 46 69 6c 74 65 72 20 77 69 74 68  ltextFilter with
355e5 20 61 0a 20 20 2a 2a 20 67 69 76 65 6e 20 63 75   a.  ** given cu
355e6 72 73 6f 72 20 77 69 6c 6c 20 68 61 76 65 20 74  rsor will have t
355e7 68 65 20 73 61 6d 65 20 69 64 78 4e 75 6d 20 2c  he same idxNum ,
355e8 20 62 75 74 20 69 6e 20 74 68 69 73 20 63 61 73   but in this cas
355e9 65 20 69 74 27 73 0a 20 20 2a 2a 20 65 61 73 79  e it's.  ** easy
355ea 20 74 6f 20 62 65 20 73 61 66 65 2e 0a 20 20 2a   to be safe..  *
355eb 2f 0a 20 20 69 66 28 20 63 2d 3e 70 53 74 6d 74  /.  if( c->pStmt
355ec 20 26 26 20 63 2d 3e 69 43 75 72 73 6f 72 54 79   && c->iCursorTy
355ed 70 65 21 3d 69 64 78 4e 75 6d 20 29 7b 0a 20 20  pe!=idxNum ){.  
355ee 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69    sqlite3_finali
355ef 7a 65 28 63 2d 3e 70 53 74 6d 74 29 3b 0a 20 20  ze(c->pStmt);.  
355f0 20 20 63 2d 3e 70 53 74 6d 74 20 3d 20 4e 55 4c    c->pStmt = NUL
355f1 4c 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 74  L;.  }..  /* Get
355f2 20 61 20 66 72 65 73 68 20 73 74 61 74 65 6d 65   a fresh stateme
355f3 6e 74 20 61 70 70 72 6f 70 72 69 61 74 65 20 74  nt appropriate t
355f4 6f 20 69 64 78 4e 75 6d 2e 20 2a 2f 0a 20 20 2f  o idxNum. */.  /
355f5 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 3a 20 41  * TODO(shess): A
355f6 64 64 20 61 20 70 72 65 70 61 72 65 64 2d 73 74  dd a prepared-st
355f7 61 74 65 6d 65 6e 74 20 63 61 63 68 65 20 69 6e  atement cache in
355f8 20 74 68 65 20 76 74 20 73 74 72 75 63 74 75 72   the vt structur
355f9 65 2e 0a 20 20 2a 2a 20 54 68 65 20 63 61 63 68  e..  ** The cach
355fa 65 20 6d 75 73 74 20 68 61 6e 64 6c 65 20 6d 75  e must handle mu
355fb 6c 74 69 70 6c 65 20 6f 70 65 6e 20 63 75 72 73  ltiple open curs
355fc 6f 72 73 2e 20 20 45 61 73 69 65 72 20 74 6f 20  ors.  Easier to 
355fd 63 61 63 68 65 20 74 68 65 0a 20 20 2a 2a 20 73  cache the.  ** s
355fe 74 61 74 65 6d 65 6e 74 20 76 61 72 69 61 6e 74  tatement variant
355ff 73 20 61 74 20 74 68 65 20 76 74 20 74 6f 20 72  s at the vt to r
35600 65 64 75 63 65 20 6d 61 6c 6c 6f 63 2f 72 65 61  educe malloc/rea
35601 6c 6c 6f 63 2f 66 72 65 65 20 68 65 72 65 2e 0a  lloc/free here..
35602 20 20 2a 2a 20 4f 72 20 77 65 20 63 6f 75 6c 64    ** Or we could
35603 20 68 61 76 65 20 61 20 53 74 72 69 6e 67 42 75   have a StringBu
35604 66 66 65 72 20 76 61 72 69 61 6e 74 20 77 68 69  ffer variant whi
35605 63 68 20 61 6c 6c 6f 77 65 64 20 73 74 61 63 6b  ch allowed stack
35606 0a 20 20 2a 2a 20 63 6f 6e 73 74 72 75 63 74 69  .  ** constructi
35607 6f 6e 20 66 6f 72 20 73 6d 61 6c 6c 20 76 61 6c  on for small val
35608 75 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ues..  */.  if( 
35609 21 63 2d 3e 70 53 74 6d 74 20 29 7b 0a 20 20 20  !c->pStmt ){.   
3560a 20 53 74 72 69 6e 67 42 75 66 66 65 72 20 73 62   StringBuffer sb
3560b 3b 0a 20 20 20 20 69 6e 69 74 53 74 72 69 6e 67  ;.    initString
3560c 42 75 66 66 65 72 28 26 73 62 29 3b 0a 20 20 20  Buffer(&sb);.   
3560d 20 61 70 70 65 6e 64 28 26 73 62 2c 20 22 53 45   append(&sb, "SE
3560e 4c 45 43 54 20 64 6f 63 69 64 2c 20 22 29 3b 0a  LECT docid, ");.
3560f 20 20 20 20 61 70 70 65 6e 64 4c 69 73 74 28 26      appendList(&
35610 73 62 2c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20  sb, v->nColumn, 
35611 76 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75  v->azContentColu
35612 6d 6e 29 3b 0a 20 20 20 20 61 70 70 65 6e 64 28  mn);.    append(
35613 26 73 62 2c 20 22 20 46 52 4f 4d 20 25 5f 63 6f  &sb, " FROM %_co
35614 6e 74 65 6e 74 22 29 3b 0a 20 20 20 20 69 66 28  ntent");.    if(
35615 20 69 64 78 4e 75 6d 21 3d 51 55 45 52 59 5f 47   idxNum!=QUERY_G
35616 45 4e 45 52 49 43 20 29 20 61 70 70 65 6e 64 28  ENERIC ) append(
35617 26 73 62 2c 20 22 20 57 48 45 52 45 20 64 6f 63  &sb, " WHERE doc
35618 69 64 20 3d 20 3f 22 29 3b 0a 20 20 20 20 72 63  id = ?");.    rc
35619 20 3d 20 73 71 6c 5f 70 72 65 70 61 72 65 28 76   = sql_prepare(v
3561a 2d 3e 64 62 2c 20 76 2d 3e 7a 44 62 2c 20 76 2d  ->db, v->zDb, v-
3561b 3e 7a 4e 61 6d 65 2c 20 26 63 2d 3e 70 53 74 6d  >zName, &c->pStm
3561c 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t,.             
3561d 20 20 20 20 20 20 20 20 73 74 72 69 6e 67 42 75          stringBu
3561e 66 66 65 72 44 61 74 61 28 26 73 62 29 29 3b 0a  fferData(&sb));.
3561f 20 20 20 20 73 74 72 69 6e 67 42 75 66 66 65 72      stringBuffer
35620 44 65 73 74 72 6f 79 28 26 73 62 29 3b 0a 20 20  Destroy(&sb);.  
35621 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
35622 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
35623 0a 20 20 20 20 63 2d 3e 69 43 75 72 73 6f 72 54  .    c->iCursorT
35624 79 70 65 20 3d 20 69 64 78 4e 75 6d 3b 0a 20 20  ype = idxNum;.  
35625 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
35626 65 33 5f 72 65 73 65 74 28 63 2d 3e 70 53 74 6d  e3_reset(c->pStm
35627 74 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  t);.    assert( 
35628 63 2d 3e 69 43 75 72 73 6f 72 54 79 70 65 3d 3d  c->iCursorType==
35629 69 64 78 4e 75 6d 20 29 3b 0a 20 20 7d 0a 0a 20  idxNum );.  }.. 
3562a 20 73 77 69 74 63 68 28 20 69 64 78 4e 75 6d 20   switch( idxNum 
3562b 29 7b 0a 20 20 20 20 63 61 73 65 20 51 55 45 52  ){.    case QUER
3562c 59 5f 47 45 4e 45 52 49 43 3a 0a 20 20 20 20 20  Y_GENERIC:.     
3562d 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 63 61 73   break;..    cas
3562e 65 20 51 55 45 52 59 5f 44 4f 43 49 44 3a 0a 20  e QUERY_DOCID:. 
3562f 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
35630 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 63 2d 3e  3_bind_int64(c->
35631 70 53 74 6d 74 2c 20 31 2c 20 73 71 6c 69 74 65  pStmt, 1, sqlite
35632 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 72  3_value_int64(ar
35633 67 76 5b 30 5d 29 29 3b 0a 20 20 20 20 20 20 69  gv[0]));.      i
35634 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
35635 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
35636 20 20 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20      break;..    
35637 64 65 66 61 75 6c 74 3a 20 20 20 2f 2a 20 66 75  default:   /* fu
35638 6c 6c 2d 74 65 78 74 20 73 65 61 72 63 68 20 2a  ll-text search *
35639 2f 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 69 6e  /.    {.      in
3563a 74 20 69 43 6f 6c 20 3d 20 69 64 78 4e 75 6d 2d  t iCol = idxNum-
3563b 51 55 45 52 59 5f 46 55 4c 4c 54 45 58 54 3b 0a  QUERY_FULLTEXT;.
3563c 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
3563d 20 2a 7a 51 75 65 72 79 20 3d 20 28 63 6f 6e 73   *zQuery = (cons
3563e 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33  t char *)sqlite3
3563f 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
35640 5b 30 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65  [0]);.      asse
35641 72 74 28 20 69 64 78 4e 75 6d 3c 3d 51 55 45 52  rt( idxNum<=QUER
35642 59 5f 46 55 4c 4c 54 45 58 54 2b 76 2d 3e 6e 43  Y_FULLTEXT+v->nC
35643 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 61 73  olumn);.      as
35644 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b  sert( argc==1 );
35645 0a 20 20 20 20 20 20 69 66 28 20 63 2d 3e 72 65  .      if( c->re
35646 73 75 6c 74 2e 6e 44 61 74 61 21 3d 30 20 29 7b  sult.nData!=0 ){
35647 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73  .        /* This
35648 20 63 61 73 65 20 68 61 70 70 65 6e 73 20 69 66   case happens if
35649 20 74 68 65 20 73 61 6d 65 20 63 75 72 73 6f 72   the same cursor
3564a 20 69 73 20 75 73 65 64 20 72 65 70 65 61 74 65   is used repeate
3564b 64 6c 79 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  dly. */.        
3564c 64 6c 72 44 65 73 74 72 6f 79 28 26 63 2d 3e 72  dlrDestroy(&c->r
3564d 65 61 64 65 72 29 3b 0a 20 20 20 20 20 20 20 20  eader);.        
3564e 64 61 74 61 42 75 66 66 65 72 52 65 73 65 74 28  dataBufferReset(
3564f 26 63 2d 3e 72 65 73 75 6c 74 29 3b 0a 20 20 20  &c->result);.   
35650 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
35651 20 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74    dataBufferInit
35652 28 26 63 2d 3e 72 65 73 75 6c 74 2c 20 30 29 3b  (&c->result, 0);
35653 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
35654 63 20 3d 20 66 75 6c 6c 74 65 78 74 51 75 65 72  c = fulltextQuer
35655 79 28 76 2c 20 69 43 6f 6c 2c 20 7a 51 75 65 72  y(v, iCol, zQuer
35656 79 2c 20 2d 31 2c 20 26 63 2d 3e 72 65 73 75 6c  y, -1, &c->resul
35657 74 2c 20 26 63 2d 3e 70 45 78 70 72 29 3b 0a 20  t, &c->pExpr);. 
35658 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
35659 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
3565a 72 63 3b 0a 20 20 20 20 20 20 69 66 28 20 63 2d  rc;.      if( c-
3565b 3e 72 65 73 75 6c 74 2e 6e 44 61 74 61 21 3d 30  >result.nData!=0
3565c 20 29 7b 0a 20 20 20 20 20 20 20 20 64 6c 72 49   ){.        dlrI
3565d 6e 69 74 28 26 63 2d 3e 72 65 61 64 65 72 2c 20  nit(&c->reader, 
3565e 44 4c 5f 44 4f 43 49 44 53 2c 20 63 2d 3e 72 65  DL_DOCIDS, c->re
3565f 73 75 6c 74 2e 70 44 61 74 61 2c 20 63 2d 3e 72  sult.pData, c->r
35660 65 73 75 6c 74 2e 6e 44 61 74 61 29 3b 0a 20 20  esult.nData);.  
35661 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
35662 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  k;.    }.  }..  
35663 72 65 74 75 72 6e 20 66 75 6c 6c 74 65 78 74 4e  return fulltextN
35664 65 78 74 28 70 43 75 72 73 6f 72 29 3b 0a 7d 0a  ext(pCursor);.}.
35665 0a 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65 20  ./* This is the 
35666 78 45 6f 66 20 6d 65 74 68 6f 64 20 6f 66 20 74  xEof method of t
35667 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
35668 2e 20 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f  .  The SQLite co
35669 72 65 0a 2a 2a 20 63 61 6c 6c 73 20 74 68 69 73  re.** calls this
3566a 20 72 6f 75 74 69 6e 65 20 74 6f 20 66 69 6e 64   routine to find
3566b 20 6f 75 74 20 69 66 20 69 74 20 68 61 73 20 72   out if it has r
3566c 65 61 63 68 65 64 20 74 68 65 20 65 6e 64 20 6f  eached the end o
3566d 66 0a 2a 2a 20 61 20 71 75 65 72 79 27 73 20 72  f.** a query's r
3566e 65 73 75 6c 74 73 20 73 65 74 2e 0a 2a 2f 0a 73  esults set..*/.s
3566f 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65  tatic int fullte
35670 78 74 45 6f 66 28 73 71 6c 69 74 65 33 5f 76 74  xtEof(sqlite3_vt
35671 61 62 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73  ab_cursor *pCurs
35672 6f 72 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f  or){.  fulltext_
35673 63 75 72 73 6f 72 20 2a 63 20 3d 20 28 66 75 6c  cursor *c = (ful
35674 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 29 20  ltext_cursor *) 
35675 70 43 75 72 73 6f 72 3b 0a 20 20 72 65 74 75 72  pCursor;.  retur
35676 6e 20 63 2d 3e 65 6f 66 3b 0a 7d 0a 0a 2f 2a 20  n c->eof;.}../* 
35677 54 68 69 73 20 69 73 20 74 68 65 20 78 43 6f 6c  This is the xCol
35678 75 6d 6e 20 6d 65 74 68 6f 64 20 6f 66 20 74 68  umn method of th
35679 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  e virtual table.
3567a 20 20 54 68 65 20 53 51 4c 69 74 65 0a 2a 2a 20    The SQLite.** 
3567b 63 6f 72 65 20 63 61 6c 6c 73 20 74 68 69 73 20  core calls this 
3567c 6d 65 74 68 6f 64 20 64 75 72 69 6e 67 20 61 20  method during a 
3567d 71 75 65 72 79 20 77 68 65 6e 20 69 74 20 6e 65  query when it ne
3567e 65 64 73 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a  eds the value.**
3567f 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 66 72 6f   of a column fro
35680 6d 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  m the virtual ta
35681 62 6c 65 2e 20 20 54 68 69 73 20 6d 65 74 68 6f  ble.  This metho
35682 64 20 6e 65 65 64 73 20 74 6f 20 75 73 65 0a 2a  d needs to use.*
35683 2a 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 71 6c  * one of the sql
35684 69 74 65 33 5f 72 65 73 75 6c 74 5f 2a 28 29 20  ite3_result_*() 
35685 72 6f 75 74 69 6e 65 73 20 74 6f 20 73 74 6f 72  routines to stor
35686 65 20 74 68 65 20 72 65 71 75 65 73 74 65 64 0a  e the requested.
35687 2a 2a 20 76 61 6c 75 65 20 62 61 63 6b 20 69 6e  ** value back in
35688 20 74 68 65 20 70 43 6f 6e 74 65 78 74 2e 0a 2a   the pContext..*
35689 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c  /.static int ful
3568a 6c 74 65 78 74 43 6f 6c 75 6d 6e 28 73 71 6c 69  ltextColumn(sqli
3568b 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
3568c 2a 70 43 75 72 73 6f 72 2c 0a 20 20 20 20 20 20  *pCursor,.      
3568d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3568e 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74      sqlite3_cont
3568f 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c 20 69  ext *pContext, i
35690 6e 74 20 69 64 78 43 6f 6c 29 7b 0a 20 20 66 75  nt idxCol){.  fu
35691 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 63  lltext_cursor *c
35692 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f 63 75 72   = (fulltext_cur
35693 73 6f 72 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a  sor *) pCursor;.
35694 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20    fulltext_vtab 
35695 2a 76 20 3d 20 63 75 72 73 6f 72 5f 76 74 61 62  *v = cursor_vtab
35696 28 63 29 3b 0a 0a 20 20 69 66 28 20 69 64 78 43  (c);..  if( idxC
35697 6f 6c 3c 76 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b  ol<v->nColumn ){
35698 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  .    sqlite3_val
35699 75 65 20 2a 70 56 61 6c 20 3d 20 73 71 6c 69 74  ue *pVal = sqlit
3569a 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28  e3_column_value(
3569b 63 2d 3e 70 53 74 6d 74 2c 20 69 64 78 43 6f 6c  c->pStmt, idxCol
3569c 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  +1);.    sqlite3
3569d 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 70 43  _result_value(pC
3569e 6f 6e 74 65 78 74 2c 20 70 56 61 6c 29 3b 0a 20  ontext, pVal);. 
3569f 20 7d 65 6c 73 65 20 69 66 28 20 69 64 78 43 6f   }else if( idxCo
356a0 6c 3d 3d 76 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b  l==v->nColumn ){
356a1 0a 20 20 20 20 2f 2a 20 54 68 65 20 65 78 74 72  .    /* The extr
356a2 61 20 63 6f 6c 75 6d 6e 20 77 68 6f 73 65 20 6e  a column whose n
356a3 61 6d 65 20 69 73 20 74 68 65 20 73 61 6d 65 20  ame is the same 
356a4 61 73 20 74 68 65 20 74 61 62 6c 65 2e 0a 20 20  as the table..  
356a5 20 20 2a 2a 20 52 65 74 75 72 6e 20 61 20 62 6c    ** Return a bl
356a6 6f 62 20 77 68 69 63 68 20 69 73 20 61 20 70 6f  ob which is a po
356a7 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 75 72  inter to the cur
356a8 73 6f 72 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73  sor.    */.    s
356a9 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c  qlite3_result_bl
356aa 6f 62 28 70 43 6f 6e 74 65 78 74 2c 20 26 63 2c  ob(pContext, &c,
356ab 20 73 69 7a 65 6f 66 28 63 29 2c 20 53 51 4c 49   sizeof(c), SQLI
356ac 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20  TE_TRANSIENT);. 
356ad 20 7d 65 6c 73 65 20 69 66 28 20 69 64 78 43 6f   }else if( idxCo
356ae 6c 3d 3d 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 20  l==v->nColumn+1 
356af 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 64 6f  ){.    /* The do
356b0 63 69 64 20 63 6f 6c 75 6d 6e 2c 20 77 68 69 63  cid column, whic
356b1 68 20 69 73 20 61 6e 20 61 6c 69 61 73 20 66 6f  h is an alias fo
356b2 72 20 72 6f 77 69 64 2e 20 2a 2f 0a 20 20 20 20  r rowid. */.    
356b3 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
356b4 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  Val = sqlite3_co
356b5 6c 75 6d 6e 5f 76 61 6c 75 65 28 63 2d 3e 70 53  lumn_value(c->pS
356b6 74 6d 74 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  tmt, 0);.    sql
356b7 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75  ite3_result_valu
356b8 65 28 70 43 6f 6e 74 65 78 74 2c 20 70 56 61 6c  e(pContext, pVal
356b9 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
356ba 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
356bb 20 54 68 69 73 20 69 73 20 74 68 65 20 78 52 6f   This is the xRo
356bc 77 69 64 20 6d 65 74 68 6f 64 2e 20 20 54 68 65  wid method.  The
356bd 20 53 51 4c 69 74 65 20 63 6f 72 65 20 63 61 6c   SQLite core cal
356be 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ls this routine 
356bf 74 6f 0a 2a 2a 20 72 65 74 72 69 65 76 65 20 74  to.** retrieve t
356c0 68 65 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65  he rowid for the
356c1 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20   current row of 
356c2 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20  the result set. 
356c3 20 66 74 73 33 0a 2a 2a 20 65 78 70 6f 73 65 73   fts3.** exposes
356c4 20 25 5f 63 6f 6e 74 65 6e 74 2e 64 6f 63 69 64   %_content.docid
356c5 20 61 73 20 74 68 65 20 72 6f 77 69 64 20 66 6f   as the rowid fo
356c6 72 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  r the virtual ta
356c7 62 6c 65 2e 20 20 54 68 65 0a 2a 2a 20 72 6f 77  ble.  The.** row
356c8 69 64 20 73 68 6f 75 6c 64 20 62 65 20 77 72 69  id should be wri
356c9 74 74 65 6e 20 74 6f 20 2a 70 52 6f 77 69 64 2e  tten to *pRowid.
356ca 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
356cb 75 6c 6c 74 65 78 74 52 6f 77 69 64 28 73 71 6c  ulltextRowid(sql
356cc 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
356cd 20 2a 70 43 75 72 73 6f 72 2c 20 73 71 6c 69 74   *pCursor, sqlit
356ce 65 5f 69 6e 74 36 34 20 2a 70 52 6f 77 69 64 29  e_int64 *pRowid)
356cf 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72  {.  fulltext_cur
356d0 73 6f 72 20 2a 63 20 3d 20 28 66 75 6c 6c 74 65  sor *c = (fullte
356d1 78 74 5f 63 75 72 73 6f 72 20 2a 29 20 70 43 75  xt_cursor *) pCu
356d2 72 73 6f 72 3b 0a 0a 20 20 2a 70 52 6f 77 69 64  rsor;..  *pRowid
356d3 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d   = sqlite3_colum
356d4 6e 5f 69 6e 74 36 34 28 63 2d 3e 70 53 74 6d 74  n_int64(c->pStmt
356d5 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  , 0);.  return S
356d6 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20  QLITE_OK;.}../* 
356d7 41 64 64 20 61 6c 6c 20 74 65 72 6d 73 20 69 6e  Add all terms in
356d8 20 5b 7a 54 65 78 74 5d 20 74 6f 20 70 65 6e 64   [zText] to pend
356d9 69 6e 67 54 65 72 6d 73 20 74 61 62 6c 65 2e 20  ingTerms table. 
356da 20 49 66 20 5b 69 43 6f 6c 75 6d 6e 5d 20 3e 20   If [iColumn] > 
356db 30 2c 0a 2a 2a 20 77 65 20 61 6c 73 6f 20 73 74  0,.** we also st
356dc 6f 72 65 20 70 6f 73 69 74 69 6f 6e 73 20 61 6e  ore positions an
356dd 64 20 6f 66 66 73 65 74 73 20 69 6e 20 74 68 65  d offsets in the
356de 20 68 61 73 68 20 74 61 62 6c 65 20 75 73 69 6e   hash table usin
356df 67 20 74 68 61 74 0a 2a 2a 20 63 6f 6c 75 6d 6e  g that.** column
356e0 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74   number..*/.stat
356e1 69 63 20 69 6e 74 20 62 75 69 6c 64 54 65 72 6d  ic int buildTerm
356e2 73 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  s(fulltext_vtab 
356e3 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  *v, sqlite_int64
356e4 20 69 44 6f 63 69 64 2c 0a 20 20 20 20 20 20 20   iDocid,.       
356e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
356e6 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 65 78 74  onst char *zText
356e7 2c 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 29 7b 0a  , int iColumn){.
356e8 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
356e9 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 20  zer *pTokenizer 
356ea 3d 20 76 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 3b  = v->pTokenizer;
356eb 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
356ec 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75  izer_cursor *pCu
356ed 72 73 6f 72 3b 0a 20 20 63 6f 6e 73 74 20 63 68  rsor;.  const ch
356ee 61 72 20 2a 70 54 6f 6b 65 6e 3b 0a 20 20 69 6e  ar *pToken;.  in
356ef 74 20 6e 54 6f 6b 65 6e 42 79 74 65 73 3b 0a 20  t nTokenBytes;. 
356f0 20 69 6e 74 20 69 53 74 61 72 74 4f 66 66 73 65   int iStartOffse
356f1 74 2c 20 69 45 6e 64 4f 66 66 73 65 74 2c 20 69  t, iEndOffset, i
356f2 50 6f 73 69 74 69 6f 6e 3b 0a 20 20 69 6e 74 20  Position;.  int 
356f3 72 63 3b 0a 0a 20 20 72 63 20 3d 20 70 54 6f 6b  rc;..  rc = pTok
356f4 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 2d  enizer->pModule-
356f5 3e 78 4f 70 65 6e 28 70 54 6f 6b 65 6e 69 7a 65  >xOpen(pTokenize
356f6 72 2c 20 7a 54 65 78 74 2c 20 2d 31 2c 20 26 70  r, zText, -1, &p
356f7 43 75 72 73 6f 72 29 3b 0a 20 20 69 66 28 20 72  Cursor);.  if( r
356f8 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
356f9 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 70 43 75  eturn rc;..  pCu
356fa 72 73 6f 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72  rsor->pTokenizer
356fb 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20   = pTokenizer;. 
356fc 20 77 68 69 6c 65 28 20 53 51 4c 49 54 45 5f 4f   while( SQLITE_O
356fd 4b 3d 3d 28 72 63 3d 70 54 6f 6b 65 6e 69 7a 65  K==(rc=pTokenize
356fe 72 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78  r->pModule->xNex
356ff 74 28 70 43 75 72 73 6f 72 2c 0a 20 20 20 20 20  t(pCursor,.     
35700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35701 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35702 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 70                &p
35703 54 6f 6b 65 6e 2c 20 26 6e 54 6f 6b 65 6e 42 79  Token, &nTokenBy
35704 74 65 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20  tes,.           
35705 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35706 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35707 20 20 20 20 20 20 20 20 26 69 53 74 61 72 74 4f          &iStartO
35708 66 66 73 65 74 2c 20 26 69 45 6e 64 4f 66 66 73  ffset, &iEndOffs
35709 65 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  et,.            
3570a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3570b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3570c 20 20 20 20 20 20 20 26 69 50 6f 73 69 74 69 6f         &iPositio
3570d 6e 29 29 20 29 7b 0a 20 20 20 20 44 4c 43 6f 6c  n)) ){.    DLCol
3570e 6c 65 63 74 6f 72 20 2a 70 3b 0a 20 20 20 20 69  lector *p;.    i
3570f 6e 74 20 6e 44 61 74 61 3b 20 20 20 20 20 20 20  nt nData;       
35710 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
35711 69 7a 65 20 6f 66 20 64 6f 63 6c 69 73 74 20 62  ize of doclist b
35712 65 66 6f 72 65 20 6f 75 72 20 75 70 64 61 74 65  efore our update
35713 2e 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 50 6f 73  . */..    /* Pos
35714 69 74 69 6f 6e 73 20 63 61 6e 27 74 20 62 65 20  itions can't be 
35715 6e 65 67 61 74 69 76 65 3b 20 77 65 20 75 73 65  negative; we use
35716 20 2d 31 20 61 73 20 61 20 74 65 72 6d 69 6e 61   -1 as a termina
35717 74 6f 72 0a 20 20 20 20 20 2a 20 69 6e 74 65 72  tor.     * inter
35718 6e 61 6c 6c 79 2e 20 20 54 6f 6b 65 6e 20 63 61  nally.  Token ca
35719 6e 27 74 20 62 65 20 4e 55 4c 4c 20 6f 72 20 65  n't be NULL or e
3571a 6d 70 74 79 2e 20 2a 2f 0a 20 20 20 20 69 66 28  mpty. */.    if(
3571b 20 69 50 6f 73 69 74 69 6f 6e 3c 30 20 7c 7c 20   iPosition<0 || 
3571c 70 54 6f 6b 65 6e 20 3d 3d 20 4e 55 4c 4c 20 7c  pToken == NULL |
3571d 7c 20 6e 54 6f 6b 65 6e 42 79 74 65 73 20 3d 3d  | nTokenBytes ==
3571e 20 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d   0 ){.      rc =
3571f 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
35720 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
35721 7d 0a 0a 20 20 20 20 70 20 3d 20 66 74 73 33 48  }..    p = fts3H
35722 61 73 68 46 69 6e 64 28 26 76 2d 3e 70 65 6e 64  ashFind(&v->pend
35723 69 6e 67 54 65 72 6d 73 2c 20 70 54 6f 6b 65 6e  ingTerms, pToken
35724 2c 20 6e 54 6f 6b 65 6e 42 79 74 65 73 29 3b 0a  , nTokenBytes);.
35725 20 20 20 20 69 66 28 20 70 3d 3d 4e 55 4c 4c 20      if( p==NULL 
35726 29 7b 0a 20 20 20 20 20 20 6e 44 61 74 61 20 3d  ){.      nData =
35727 20 30 3b 0a 20 20 20 20 20 20 70 20 3d 20 64 6c   0;.      p = dl
35728 63 4e 65 77 28 69 44 6f 63 69 64 2c 20 44 4c 5f  cNew(iDocid, DL_
35729 44 45 46 41 55 4c 54 29 3b 0a 20 20 20 20 20 20  DEFAULT);.      
3572a 66 74 73 33 48 61 73 68 49 6e 73 65 72 74 28 26  fts3HashInsert(&
3572b 76 2d 3e 70 65 6e 64 69 6e 67 54 65 72 6d 73 2c  v->pendingTerms,
3572c 20 70 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 42   pToken, nTokenB
3572d 79 74 65 73 2c 20 70 29 3b 0a 0a 20 20 20 20 20  ytes, p);..     
3572e 20 2f 2a 20 4f 76 65 72 68 65 61 64 20 66 6f 72   /* Overhead for
3572f 20 6f 75 72 20 68 61 73 68 20 74 61 62 6c 65 20   our hash table 
35730 65 6e 74 72 79 2c 20 74 68 65 20 6b 65 79 2c 20  entry, the key, 
35731 61 6e 64 20 74 68 65 20 76 61 6c 75 65 2e 20 2a  and the value. *
35732 2f 0a 20 20 20 20 20 20 76 2d 3e 6e 50 65 6e 64  /.      v->nPend
35733 69 6e 67 44 61 74 61 20 2b 3d 20 73 69 7a 65 6f  ingData += sizeo
35734 66 28 73 74 72 75 63 74 20 66 74 73 33 48 61 73  f(struct fts3Has
35735 68 45 6c 65 6d 29 2b 73 69 7a 65 6f 66 28 2a 70  hElem)+sizeof(*p
35736 29 2b 6e 54 6f 6b 65 6e 42 79 74 65 73 3b 0a 20  )+nTokenBytes;. 
35737 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
35738 6e 44 61 74 61 20 3d 20 70 2d 3e 62 2e 6e 44 61  nData = p->b.nDa
35739 74 61 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d  ta;.      if( p-
3573a 3e 64 6c 77 2e 69 50 72 65 76 44 6f 63 69 64 21  >dlw.iPrevDocid!
3573b 3d 69 44 6f 63 69 64 20 29 20 64 6c 63 4e 65 78  =iDocid ) dlcNex
3573c 74 28 70 2c 20 69 44 6f 63 69 64 29 3b 0a 20 20  t(p, iDocid);.  
3573d 20 20 7d 0a 20 20 20 20 69 66 28 20 69 43 6f 6c    }.    if( iCol
3573e 75 6d 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20  umn>=0 ){.      
3573f 64 6c 63 41 64 64 50 6f 73 28 70 2c 20 69 43 6f  dlcAddPos(p, iCo
35740 6c 75 6d 6e 2c 20 69 50 6f 73 69 74 69 6f 6e 2c  lumn, iPosition,
35741 20 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20 69   iStartOffset, i
35742 45 6e 64 4f 66 66 73 65 74 29 3b 0a 20 20 20 20  EndOffset);.    
35743 7d 0a 0a 20 20 20 20 2f 2a 20 41 63 63 75 6d 75  }..    /* Accumu
35744 6c 61 74 65 20 64 61 74 61 20 61 64 64 65 64 20  late data added 
35745 62 79 20 64 6c 63 4e 65 77 20 6f 72 20 64 6c 63  by dlcNew or dlc
35746 4e 65 78 74 2c 20 61 6e 64 20 64 6c 63 41 64 64  Next, and dlcAdd
35747 50 6f 73 2e 20 2a 2f 0a 20 20 20 20 76 2d 3e 6e  Pos. */.    v->n
35748 50 65 6e 64 69 6e 67 44 61 74 61 20 2b 3d 20 70  PendingData += p
35749 2d 3e 62 2e 6e 44 61 74 61 2d 6e 44 61 74 61 3b  ->b.nData-nData;
3574a 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 4f 44 4f 28  .  }..  /* TODO(
3574b 73 68 65 73 73 29 20 43 68 65 63 6b 20 72 65 74  shess) Check ret
3574c 75 72 6e 3f 20 20 53 68 6f 75 6c 64 20 74 68 69  urn?  Should thi
3574d 73 20 62 65 20 61 62 6c 65 20 74 6f 20 63 61 75  s be able to cau
3574e 73 65 20 65 72 72 6f 72 73 20 61 74 0a 20 20 2a  se errors at.  *
3574f 2a 20 74 68 69 73 20 70 6f 69 6e 74 3f 20 20 41  * this point?  A
35750 63 74 75 61 6c 6c 79 2c 20 73 61 6d 65 20 71 75  ctually, same qu
35751 65 73 74 69 6f 6e 20 61 62 6f 75 74 20 73 71 6c  estion about sql
35752 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 2c  ite3_finalize(),
35753 0a 20 20 2a 2a 20 74 68 6f 75 67 68 20 6f 6e 65  .  ** though one
35754 20 63 6f 75 6c 64 20 61 72 67 75 65 20 74 68 61   could argue tha
35755 74 20 66 61 69 6c 75 72 65 20 74 68 65 72 65 20  t failure there 
35756 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 64  means that the d
35757 61 74 61 20 69 73 0a 20 20 2a 2a 20 6e 6f 74 20  ata is.  ** not 
35758 64 75 72 61 62 6c 65 2e 20 20 2a 70 6f 6e 64 65  durable.  *ponde
35759 72 2a 0a 20 20 2a 2f 0a 20 20 70 54 6f 6b 65 6e  r*.  */.  pToken
3575a 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78  izer->pModule->x
3575b 43 6c 6f 73 65 28 70 43 75 72 73 6f 72 29 3b 0a  Close(pCursor);.
3575c 20 20 69 66 28 20 53 51 4c 49 54 45 5f 44 4f 4e    if( SQLITE_DON
3575d 45 20 3d 3d 20 72 63 20 29 20 72 65 74 75 72 6e  E == rc ) return
3575e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 72 65   SQLITE_OK;.  re
3575f 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 41  turn rc;.}../* A
35760 64 64 20 64 6f 63 6c 69 73 74 73 20 66 6f 72 20  dd doclists for 
35761 61 6c 6c 20 74 65 72 6d 73 20 69 6e 20 5b 70 56  all terms in [pV
35762 61 6c 75 65 73 5d 20 74 6f 20 70 65 6e 64 69 6e  alues] to pendin
35763 67 54 65 72 6d 73 20 74 61 62 6c 65 2e 20 2a 2f  gTerms table. */
35764 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e 73 65  .static int inse
35765 72 74 54 65 72 6d 73 28 66 75 6c 6c 74 65 78 74  rtTerms(fulltext
35766 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65  _vtab *v, sqlite
35767 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 2c 0a 20  _int64 iDocid,. 
35768 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35769 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61        sqlite3_va
3576a 6c 75 65 20 2a 2a 70 56 61 6c 75 65 73 29 7b 0a  lue **pValues){.
3576b 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69    int i;.  for(i
3576c 20 3d 20 30 3b 20 69 20 3c 20 76 2d 3e 6e 43 6f   = 0; i < v->nCo
3576d 6c 75 6d 6e 20 3b 20 2b 2b 69 29 7b 0a 20 20 20  lumn ; ++i){.   
3576e 20 63 68 61 72 20 2a 7a 54 65 78 74 20 3d 20 28   char *zText = (
3576f 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61  char*)sqlite3_va
35770 6c 75 65 5f 74 65 78 74 28 70 56 61 6c 75 65 73  lue_text(pValues
35771 5b 69 5d 29 3b 0a 20 20 20 20 69 6e 74 20 72 63  [i]);.    int rc
35772 20 3d 20 62 75 69 6c 64 54 65 72 6d 73 28 76 2c   = buildTerms(v,
35773 20 69 44 6f 63 69 64 2c 20 7a 54 65 78 74 2c 20   iDocid, zText, 
35774 69 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  i);.    if( rc!=
35775 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
35776 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 72 65 74  rn rc;.  }.  ret
35777 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
35778 0a 0a 2f 2a 20 41 64 64 20 65 6d 70 74 79 20 64  ../* Add empty d
35779 6f 63 6c 69 73 74 73 20 66 6f 72 20 61 6c 6c 20  oclists for all 
3577a 74 65 72 6d 73 20 69 6e 20 74 68 65 20 67 69 76  terms in the giv
3577b 65 6e 20 72 6f 77 27 73 20 63 6f 6e 74 65 6e 74  en row's content
3577c 20 74 6f 0a 2a 2a 20 70 65 6e 64 69 6e 67 54 65   to.** pendingTe
3577d 72 6d 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  rms..*/.static i
3577e 6e 74 20 64 65 6c 65 74 65 54 65 72 6d 73 28 66  nt deleteTerms(f
3577f 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c  ulltext_vtab *v,
35780 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44   sqlite_int64 iD
35781 6f 63 69 64 29 7b 0a 20 20 63 6f 6e 73 74 20 63  ocid){.  const c
35782 68 61 72 20 2a 2a 70 56 61 6c 75 65 73 3b 0a 20  har **pValues;. 
35783 20 69 6e 74 20 69 2c 20 72 63 3b 0a 0a 20 20 2f   int i, rc;..  /
35784 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 53 68  * TODO(shess) Sh
35785 6f 75 6c 64 20 77 65 20 61 6c 6c 6f 77 20 73 75  ould we allow su
35786 63 68 20 74 61 62 6c 65 73 20 61 74 20 61 6c 6c  ch tables at all
35787 3f 20 2a 2f 0a 20 20 69 66 28 20 44 4c 5f 44 45  ? */.  if( DL_DE
35788 46 41 55 4c 54 3d 3d 44 4c 5f 44 4f 43 49 44 53  FAULT==DL_DOCIDS
35789 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
3578a 5f 45 52 52 4f 52 3b 0a 0a 20 20 72 63 20 3d 20  _ERROR;..  rc = 
3578b 63 6f 6e 74 65 6e 74 5f 73 65 6c 65 63 74 28 76  content_select(v
3578c 2c 20 69 44 6f 63 69 64 2c 20 26 70 56 61 6c 75  , iDocid, &pValu
3578d 65 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  es);.  if( rc!=S
3578e 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
3578f 6e 20 72 63 3b 0a 0a 20 20 66 6f 72 28 69 20 3d  n rc;..  for(i =
35790 20 30 20 3b 20 69 20 3c 20 76 2d 3e 6e 43 6f 6c   0 ; i < v->nCol
35791 75 6d 6e 3b 20 2b 2b 69 29 20 7b 0a 20 20 20 20  umn; ++i) {.    
35792 72 63 20 3d 20 62 75 69 6c 64 54 65 72 6d 73 28  rc = buildTerms(
35793 76 2c 20 69 44 6f 63 69 64 2c 20 70 56 61 6c 75  v, iDocid, pValu
35794 65 73 5b 69 5d 2c 20 2d 31 29 3b 0a 20 20 20 20  es[i], -1);.    
35795 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
35796 4b 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a  K ) break;.  }..
35797 20 20 66 72 65 65 53 74 72 69 6e 67 41 72 72 61    freeStringArra
35798 79 28 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 70 56  y(v->nColumn, pV
35799 61 6c 75 65 73 29 3b 0a 20 20 72 65 74 75 72 6e  alues);.  return
3579a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
3579b 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 52 65  * TODO(shess) Re
3579c 66 61 63 74 6f 72 20 74 68 65 20 63 6f 64 65 20  factor the code 
3579d 74 6f 20 72 65 6d 6f 76 65 20 74 68 69 73 20 66  to remove this f
3579e 6f 72 77 61 72 64 20 64 65 63 6c 2e 20 2a 2f 0a  orward decl. */.
3579f 73 74 61 74 69 63 20 69 6e 74 20 69 6e 69 74 50  static int initP
357a0 65 6e 64 69 6e 67 54 65 72 6d 73 28 66 75 6c 6c  endingTerms(full
357a1 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73 71  text_vtab *v, sq
357a2 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63 69  lite_int64 iDoci
357a3 64 29 3b 0a 0a 2f 2a 20 49 6e 73 65 72 74 20 61  d);../* Insert a
357a4 20 72 6f 77 20 69 6e 74 6f 20 74 68 65 20 25 5f   row into the %_
357a5 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 3b 20 73  content table; s
357a6 65 74 20 2a 70 69 44 6f 63 69 64 20 74 6f 20 62  et *piDocid to b
357a7 65 20 74 68 65 20 49 44 20 6f 66 20 74 68 65 0a  e the ID of the.
357a8 2a 2a 20 6e 65 77 20 72 6f 77 2e 20 20 41 64 64  ** new row.  Add
357a9 20 64 6f 63 6c 69 73 74 73 20 66 6f 72 20 74 65   doclists for te
357aa 72 6d 73 20 74 6f 20 70 65 6e 64 69 6e 67 54 65  rms to pendingTe
357ab 72 6d 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  rms..*/.static i
357ac 6e 74 20 69 6e 64 65 78 5f 69 6e 73 65 72 74 28  nt index_insert(
357ad 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
357ae 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
357af 2a 70 52 65 71 75 65 73 74 44 6f 63 69 64 2c 0a  *pRequestDocid,.
357b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
357b1 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
357b2 76 61 6c 75 65 20 2a 2a 70 56 61 6c 75 65 73 2c  value **pValues,
357b3 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70   sqlite_int64 *p
357b4 69 44 6f 63 69 64 29 7b 0a 20 20 69 6e 74 20 72  iDocid){.  int r
357b5 63 3b 0a 0a 20 20 72 63 20 3d 20 63 6f 6e 74 65  c;..  rc = conte
357b6 6e 74 5f 69 6e 73 65 72 74 28 76 2c 20 70 52 65  nt_insert(v, pRe
357b7 71 75 65 73 74 44 6f 63 69 64 2c 20 70 56 61 6c  questDocid, pVal
357b8 75 65 73 29 3b 20 20 2f 2a 20 65 78 65 63 75 74  ues);  /* execut
357b9 65 20 61 6e 20 53 51 4c 20 49 4e 53 45 52 54 20  e an SQL INSERT 
357ba 2a 2f 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  */.  if( rc!=SQL
357bb 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
357bc 72 63 3b 0a 0a 20 20 2f 2a 20 64 6f 63 69 64 20  rc;..  /* docid 
357bd 63 6f 6c 75 6d 6e 20 69 73 20 61 6e 20 61 6c 69  column is an ali
357be 61 73 20 66 6f 72 20 72 6f 77 69 64 2e 20 2a 2f  as for rowid. */
357bf 0a 20 20 2a 70 69 44 6f 63 69 64 20 3d 20 73 71  .  *piDocid = sq
357c0 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72  lite3_last_inser
357c1 74 5f 72 6f 77 69 64 28 76 2d 3e 64 62 29 3b 0a  t_rowid(v->db);.
357c2 20 20 72 63 20 3d 20 69 6e 69 74 50 65 6e 64 69    rc = initPendi
357c3 6e 67 54 65 72 6d 73 28 76 2c 20 2a 70 69 44 6f  ngTerms(v, *piDo
357c4 63 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  cid);.  if( rc!=
357c5 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
357c6 72 6e 20 72 63 3b 0a 0a 20 20 72 65 74 75 72 6e  rn rc;..  return
357c7 20 69 6e 73 65 72 74 54 65 72 6d 73 28 76 2c 20   insertTerms(v, 
357c8 2a 70 69 44 6f 63 69 64 2c 20 70 56 61 6c 75 65  *piDocid, pValue
357c9 73 29 3b 0a 7d 0a 0a 2f 2a 20 44 65 6c 65 74 65  s);.}../* Delete
357ca 20 61 20 72 6f 77 20 66 72 6f 6d 20 74 68 65 20   a row from the 
357cb 25 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 3b  %_content table;
357cc 20 61 64 64 20 65 6d 70 74 79 20 64 6f 63 6c 69   add empty docli
357cd 73 74 73 20 66 6f 72 20 74 65 72 6d 73 0a 2a 2a  sts for terms.**
357ce 20 74 6f 20 70 65 6e 64 69 6e 67 54 65 72 6d 73   to pendingTerms
357cf 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
357d0 69 6e 64 65 78 5f 64 65 6c 65 74 65 28 66 75 6c  index_delete(ful
357d1 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73  ltext_vtab *v, s
357d2 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 52 6f 77  qlite_int64 iRow
357d3 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 69 6e  ){.  int rc = in
357d4 69 74 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 76  itPendingTerms(v
357d5 2c 20 69 52 6f 77 29 3b 0a 20 20 69 66 28 20 72  , iRow);.  if( r
357d6 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
357d7 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20  eturn rc;..  rc 
357d8 3d 20 64 65 6c 65 74 65 54 65 72 6d 73 28 76 2c  = deleteTerms(v,
357d9 20 69 52 6f 77 29 3b 0a 20 20 69 66 28 20 72 63   iRow);.  if( rc
357da 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
357db 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 65 74 75  turn rc;..  retu
357dc 72 6e 20 63 6f 6e 74 65 6e 74 5f 64 65 6c 65 74  rn content_delet
357dd 65 28 76 2c 20 69 52 6f 77 29 3b 20 20 2f 2a 20  e(v, iRow);  /* 
357de 65 78 65 63 75 74 65 20 61 6e 20 53 51 4c 20 44  execute an SQL D
357df 45 4c 45 54 45 20 2a 2f 0a 7d 0a 0a 2f 2a 20 55  ELETE */.}../* U
357e0 70 64 61 74 65 20 61 20 72 6f 77 20 69 6e 20 74  pdate a row in t
357e1 68 65 20 25 5f 63 6f 6e 74 65 6e 74 20 74 61 62  he %_content tab
357e2 6c 65 3b 20 61 64 64 20 64 65 6c 65 74 65 20 64  le; add delete d
357e3 6f 63 6c 69 73 74 73 20 74 6f 0a 2a 2a 20 70 65  oclists to.** pe
357e4 6e 64 69 6e 67 54 65 72 6d 73 20 66 6f 72 20 6f  ndingTerms for o
357e5 6c 64 20 74 65 72 6d 73 20 6e 6f 74 20 69 6e 20  ld terms not in 
357e6 74 68 65 20 6e 65 77 20 64 61 74 61 2c 20 61 64  the new data, ad
357e7 64 20 69 6e 73 65 72 74 20 64 6f 63 6c 69 73 74  d insert doclist
357e8 73 0a 2a 2a 20 74 6f 20 70 65 6e 64 69 6e 67 54  s.** to pendingT
357e9 65 72 6d 73 20 66 6f 72 20 74 65 72 6d 73 20 69  erms for terms i
357ea 6e 20 74 68 65 20 6e 65 77 20 64 61 74 61 2e 0a  n the new data..
357eb 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e  */.static int in
357ec 64 65 78 5f 75 70 64 61 74 65 28 66 75 6c 6c 74  dex_update(fullt
357ed 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c  ext_vtab *v, sql
357ee 69 74 65 5f 69 6e 74 36 34 20 69 52 6f 77 2c 0a  ite_int64 iRow,.
357ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
357f0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
357f1 76 61 6c 75 65 20 2a 2a 70 56 61 6c 75 65 73 29  value **pValues)
357f2 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 69 6e 69  {.  int rc = ini
357f3 74 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 76 2c  tPendingTerms(v,
357f4 20 69 52 6f 77 29 3b 0a 20 20 69 66 28 20 72 63   iRow);.  if( rc
357f5 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
357f6 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 47  turn rc;..  /* G
357f7 65 6e 65 72 61 74 65 20 61 6e 20 65 6d 70 74 79  enerate an empty
357f8 20 64 6f 63 6c 69 73 74 20 66 6f 72 20 65 61 63   doclist for eac
357f9 68 20 74 65 72 6d 20 74 68 61 74 20 70 72 65 76  h term that prev
357fa 69 6f 75 73 6c 79 20 61 70 70 65 61 72 65 64 20  iously appeared 
357fb 69 6e 20 74 68 69 73 0a 20 20 20 2a 20 72 6f 77  in this.   * row
357fc 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 64 65 6c 65  . */.  rc = dele
357fd 74 65 54 65 72 6d 73 28 76 2c 20 69 52 6f 77 29  teTerms(v, iRow)
357fe 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
357ff 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
35800 63 3b 0a 0a 20 20 72 63 20 3d 20 63 6f 6e 74 65  c;..  rc = conte
35801 6e 74 5f 75 70 64 61 74 65 28 76 2c 20 70 56 61  nt_update(v, pVa
35802 6c 75 65 73 2c 20 69 52 6f 77 29 3b 20 20 2f 2a  lues, iRow);  /*
35803 20 65 78 65 63 75 74 65 20 61 6e 20 53 51 4c 20   execute an SQL 
35804 55 50 44 41 54 45 20 2a 2f 0a 20 20 69 66 28 20  UPDATE */.  if( 
35805 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
35806 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a  return rc;..  /*
35807 20 4e 6f 77 20 61 64 64 20 70 6f 73 69 74 69 6f   Now add positio
35808 6e 73 20 66 6f 72 20 74 65 72 6d 73 20 77 68 69  ns for terms whi
35809 63 68 20 61 70 70 65 61 72 20 69 6e 20 74 68 65  ch appear in the
3580a 20 75 70 64 61 74 65 64 20 72 6f 77 2e 20 2a 2f   updated row. */
3580b 0a 20 20 72 65 74 75 72 6e 20 69 6e 73 65 72 74  .  return insert
3580c 54 65 72 6d 73 28 76 2c 20 69 52 6f 77 2c 20 70  Terms(v, iRow, p
3580d 56 61 6c 75 65 73 29 3b 0a 7d 0a 0a 2f 2a 2a 2a  Values);.}../***
3580e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3580f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35811 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35812 2f 0a 2f 2a 20 49 6e 74 65 72 69 6f 72 57 72 69  /./* InteriorWri
35813 74 65 72 20 69 73 20 75 73 65 64 20 74 6f 20 63  ter is used to c
35814 6f 6c 6c 65 63 74 20 74 65 72 6d 73 20 61 6e 64  ollect terms and
35815 20 62 6c 6f 63 6b 20 72 65 66 65 72 65 6e 63 65   block reference
35816 73 20 69 6e 74 6f 0a 2a 2a 20 69 6e 74 65 72 69  s into.** interi
35817 6f 72 20 6e 6f 64 65 73 20 69 6e 20 25 5f 73 65  or nodes in %_se
35818 67 6d 65 6e 74 73 2e 20 20 53 65 65 20 63 6f 6d  gments.  See com
35819 6d 65 6e 74 61 72 79 20 61 74 20 74 6f 70 20 6f  mentary at top o
3581a 66 20 66 69 6c 65 20 66 6f 72 0a 2a 2a 20 66 6f  f file for.** fo
3581b 72 6d 61 74 2e 0a 2a 2f 0a 0a 2f 2a 20 48 6f 77  rmat..*/../* How
3581c 20 6c 61 72 67 65 20 69 6e 74 65 72 69 6f 72 20   large interior 
3581d 6e 6f 64 65 73 20 63 61 6e 20 67 72 6f 77 2e 20  nodes can grow. 
3581e 2a 2f 0a 23 64 65 66 69 6e 65 20 49 4e 54 45 52  */.#define INTER
3581f 49 4f 52 5f 4d 41 58 20 32 30 34 38 0a 0a 2f 2a  IOR_MAX 2048../*
35820 20 4d 69 6e 69 6d 75 6d 20 6e 75 6d 62 65 72 20   Minimum number 
35821 6f 66 20 74 65 72 6d 73 20 70 65 72 20 69 6e 74  of terms per int
35822 65 72 69 6f 72 20 6e 6f 64 65 20 28 65 78 63 65  erior node (exce
35823 70 74 20 74 68 65 20 72 6f 6f 74 29 2e 20 54 68  pt the root). Th
35824 69 73 0a 2a 2a 20 70 72 65 76 65 6e 74 73 20 6c  is.** prevents l
35825 61 72 67 65 20 74 65 72 6d 73 20 66 72 6f 6d 20  arge terms from 
35826 6d 61 6b 69 6e 67 20 74 68 65 20 74 72 65 65 20  making the tree 
35827 74 6f 6f 20 73 6b 69 6e 6e 79 20 2d 20 6d 75 73  too skinny - mus
35828 74 20 62 65 20 3e 30 0a 2a 2a 20 73 6f 20 74 68  t be >0.** so th
35829 61 74 20 74 68 65 20 74 72 65 65 20 61 6c 77 61  at the tree alwa
3582a 79 73 20 6d 61 6b 65 73 20 70 72 6f 67 72 65 73  ys makes progres
3582b 73 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 74 68  s.  Note that th
3582c 65 20 6d 69 6e 20 74 72 65 65 0a 2a 2a 20 66 61  e min tree.** fa
3582d 6e 6f 75 74 20 77 69 6c 6c 20 62 65 20 49 4e 54  nout will be INT
3582e 45 52 49 4f 52 5f 4d 49 4e 5f 54 45 52 4d 53 2b  ERIOR_MIN_TERMS+
3582f 31 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 49 4e  1..*/.#define IN
35830 54 45 52 49 4f 52 5f 4d 49 4e 5f 54 45 52 4d 53  TERIOR_MIN_TERMS
35831 20 37 0a 23 69 66 20 49 4e 54 45 52 49 4f 52 5f   7.#if INTERIOR_
35832 4d 49 4e 5f 54 45 52 4d 53 3c 31 0a 23 20 65 72  MIN_TERMS<1.# er
35833 72 6f 72 20 49 4e 54 45 52 49 4f 52 5f 4d 49 4e  ror INTERIOR_MIN
35834 5f 54 45 52 4d 53 20 6d 75 73 74 20 62 65 20 67  _TERMS must be g
35835 72 65 61 74 65 72 20 74 68 61 6e 20 30 2e 0a 23  reater than 0..#
35836 65 6e 64 69 66 0a 0a 2f 2a 20 52 4f 4f 54 5f 4d  endif../* ROOT_M
35837 41 58 20 63 6f 6e 74 72 6f 6c 73 20 68 6f 77 20  AX controls how 
35838 6d 75 63 68 20 64 61 74 61 20 69 73 20 73 74 6f  much data is sto
35839 72 65 64 20 69 6e 6c 69 6e 65 20 69 6e 20 74 68  red inline in th
3583a 65 20 73 65 67 6d 65 6e 74 0a 2a 2a 20 64 69 72  e segment.** dir
3583b 65 63 74 6f 72 79 2e 0a 2a 2f 0a 2f 2a 20 54 4f  ectory..*/./* TO
3583c 44 4f 28 73 68 65 73 73 29 20 50 75 73 68 20 52  DO(shess) Push R
3583d 4f 4f 54 5f 4d 41 58 20 64 6f 77 6e 20 74 6f 20  OOT_MAX down to 
3583e 77 68 6f 65 76 65 72 20 69 73 20 77 72 69 74 69  whoever is writi
3583f 6e 67 20 74 68 69 6e 67 73 2e 20 20 49 74 27 73  ng things.  It's
35840 0a 2a 2a 20 6f 6e 6c 79 20 68 65 72 65 20 73 6f  .** only here so
35841 20 74 68 61 74 20 69 6e 74 65 72 69 6f 72 57 72   that interiorWr
35842 69 74 65 72 52 6f 6f 74 49 6e 66 6f 28 29 20 61  iterRootInfo() a
35843 6e 64 20 6c 65 61 66 57 72 69 74 65 72 52 6f 6f  nd leafWriterRoo
35844 74 49 6e 66 6f 28 29 0a 2a 2a 20 63 61 6e 20 62  tInfo().** can b
35845 6f 74 68 20 73 65 65 20 69 74 2c 20 62 75 74 20  oth see it, but 
35846 69 66 20 74 68 65 20 63 61 6c 6c 65 72 20 70 61  if the caller pa
35847 73 73 65 64 20 69 74 20 69 6e 2c 20 77 65 20 77  ssed it in, we w
35848 6f 75 6c 64 6e 27 74 20 65 76 65 6e 0a 2a 2a 20  ouldn't even.** 
35849 6e 65 65 64 20 61 20 64 65 66 69 6e 65 2e 0a 2a  need a define..*
3584a 2f 0a 23 64 65 66 69 6e 65 20 52 4f 4f 54 5f 4d  /.#define ROOT_M
3584b 41 58 20 31 30 32 34 0a 23 69 66 20 52 4f 4f 54  AX 1024.#if ROOT
3584c 5f 4d 41 58 3c 56 41 52 49 4e 54 5f 4d 41 58 2a  _MAX<VARINT_MAX*
3584d 32 0a 23 20 65 72 72 6f 72 20 52 4f 4f 54 5f 4d  2.# error ROOT_M
3584e 41 58 20 6d 75 73 74 20 68 61 76 65 20 65 6e 6f  AX must have eno
3584f 75 67 68 20 73 70 61 63 65 20 66 6f 72 20 61 20  ugh space for a 
35850 68 65 61 64 65 72 2e 0a 23 65 6e 64 69 66 0a 0a  header..#endif..
35851 2f 2a 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b  /* InteriorBlock
35852 20 73 74 6f 72 65 73 20 61 20 6c 69 6e 6b 65 64   stores a linked
35853 2d 6c 69 73 74 20 6f 66 20 69 6e 74 65 72 69 6f  -list of interio
35854 72 20 62 6c 6f 63 6b 73 20 77 68 69 6c 65 20 61  r blocks while a
35855 20 6c 6f 77 65 72 0a 2a 2a 20 6c 61 79 65 72 20   lower.** layer 
35856 69 73 20 62 65 69 6e 67 20 63 6f 6e 73 74 72 75  is being constru
35857 63 74 65 64 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  cted..*/.typedef
35858 20 73 74 72 75 63 74 20 49 6e 74 65 72 69 6f 72   struct Interior
35859 42 6c 6f 63 6b 20 7b 0a 20 20 44 61 74 61 42 75  Block {.  DataBu
3585a 66 66 65 72 20 74 65 72 6d 3b 20 20 20 20 20 20  ffer term;      
3585b 20 20 20 20 20 2f 2a 20 4c 65 66 74 6d 6f 73 74       /* Leftmost
3585c 20 74 65 72 6d 20 69 6e 20 62 6c 6f 63 6b 27 73   term in block's
3585d 20 73 75 62 74 72 65 65 2e 20 2a 2f 0a 20 20 44   subtree. */.  D
3585e 61 74 61 42 75 66 66 65 72 20 64 61 74 61 3b 20  ataBuffer data; 
3585f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 63 63            /* Acc
35860 75 6d 75 6c 61 74 65 64 20 64 61 74 61 20 66 6f  umulated data fo
35861 72 20 74 68 65 20 62 6c 6f 63 6b 2e 20 2a 2f 0a  r the block. */.
35862 20 20 73 74 72 75 63 74 20 49 6e 74 65 72 69 6f    struct Interio
35863 72 42 6c 6f 63 6b 20 2a 6e 65 78 74 3b 0a 7d 20  rBlock *next;.} 
35864 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 3b 0a 0a  InteriorBlock;..
35865 73 74 61 74 69 63 20 49 6e 74 65 72 69 6f 72 42  static InteriorB
35866 6c 6f 63 6b 20 2a 69 6e 74 65 72 69 6f 72 42 6c  lock *interiorBl
35867 6f 63 6b 4e 65 77 28 69 6e 74 20 69 48 65 69 67  ockNew(int iHeig
35868 68 74 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  ht, sqlite_int64
35869 20 69 43 68 69 6c 64 42 6c 6f 63 6b 2c 0a 20 20   iChildBlock,.  
3586a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3586b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3586c 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
3586d 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72  *pTerm, int nTer
3586e 6d 29 7b 0a 20 20 49 6e 74 65 72 69 6f 72 42 6c  m){.  InteriorBl
3586f 6f 63 6b 20 2a 62 6c 6f 63 6b 20 3d 20 73 71 6c  ock *block = sql
35870 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65  ite3_malloc(size
35871 6f 66 28 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b  of(InteriorBlock
35872 29 29 3b 0a 20 20 63 68 61 72 20 63 5b 56 41 52  ));.  char c[VAR
35873 49 4e 54 5f 4d 41 58 2b 56 41 52 49 4e 54 5f 4d  INT_MAX+VARINT_M
35874 41 58 5d 3b 0a 20 20 69 6e 74 20 6e 3b 0a 0a 20  AX];.  int n;.. 
35875 20 69 66 28 20 62 6c 6f 63 6b 20 29 7b 0a 20 20   if( block ){.  
35876 20 20 6d 65 6d 73 65 74 28 62 6c 6f 63 6b 2c 20    memset(block, 
35877 30 2c 20 73 69 7a 65 6f 66 28 2a 62 6c 6f 63 6b  0, sizeof(*block
35878 29 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66  ));.    dataBuff
35879 65 72 49 6e 69 74 28 26 62 6c 6f 63 6b 2d 3e 74  erInit(&block->t
3587a 65 72 6d 2c 20 30 29 3b 0a 20 20 20 20 64 61 74  erm, 0);.    dat
3587b 61 42 75 66 66 65 72 52 65 70 6c 61 63 65 28 26  aBufferReplace(&
3587c 62 6c 6f 63 6b 2d 3e 74 65 72 6d 2c 20 70 54 65  block->term, pTe
3587d 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 0a 20 20 20  rm, nTerm);..   
3587e 20 6e 20 3d 20 66 74 73 33 50 75 74 56 61 72 69   n = fts3PutVari
3587f 6e 74 28 63 2c 20 69 48 65 69 67 68 74 29 3b 0a  nt(c, iHeight);.
35880 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 50 75 74      n += fts3Put
35881 56 61 72 69 6e 74 28 63 2b 6e 2c 20 69 43 68 69  Varint(c+n, iChi
35882 6c 64 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 64 61  ldBlock);.    da
35883 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 62 6c  taBufferInit(&bl
35884 6f 63 6b 2d 3e 64 61 74 61 2c 20 49 4e 54 45 52  ock->data, INTER
35885 49 4f 52 5f 4d 41 58 29 3b 0a 20 20 20 20 64 61  IOR_MAX);.    da
35886 74 61 42 75 66 66 65 72 52 65 70 6c 61 63 65 28  taBufferReplace(
35887 26 62 6c 6f 63 6b 2d 3e 64 61 74 61 2c 20 63 2c  &block->data, c,
35888 20 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72   n);.  }.  retur
35889 6e 20 62 6c 6f 63 6b 3b 0a 7d 0a 0a 23 69 66 6e  n block;.}..#ifn
3588a 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 20 56 65  def NDEBUG./* Ve
3588b 72 69 66 79 20 74 68 61 74 20 74 68 65 20 64 61  rify that the da
3588c 74 61 20 69 73 20 72 65 61 64 61 62 6c 65 20 61  ta is readable a
3588d 73 20 61 6e 20 69 6e 74 65 72 69 6f 72 20 6e 6f  s an interior no
3588e 64 65 2e 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f  de. */.static vo
3588f 69 64 20 69 6e 74 65 72 69 6f 72 42 6c 6f 63 6b  id interiorBlock
35890 56 61 6c 69 64 61 74 65 28 49 6e 74 65 72 69 6f  Validate(Interio
35891 72 42 6c 6f 63 6b 20 2a 70 42 6c 6f 63 6b 29 7b  rBlock *pBlock){
35892 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  .  const char *p
35893 44 61 74 61 20 3d 20 70 42 6c 6f 63 6b 2d 3e 64  Data = pBlock->d
35894 61 74 61 2e 70 44 61 74 61 3b 0a 20 20 69 6e 74  ata.pData;.  int
35895 20 6e 44 61 74 61 20 3d 20 70 42 6c 6f 63 6b 2d   nData = pBlock-
35896 3e 64 61 74 61 2e 6e 44 61 74 61 3b 0a 20 20 69  >data.nData;.  i
35897 6e 74 20 6e 2c 20 69 44 75 6d 6d 79 3b 0a 20 20  nt n, iDummy;.  
35898 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 42 6c  sqlite_int64 iBl
35899 6f 63 6b 69 64 3b 0a 0a 20 20 61 73 73 65 72 74  ockid;..  assert
3589a 28 20 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20 61  ( nData>0 );.  a
3589b 73 73 65 72 74 28 20 70 44 61 74 61 21 3d 30 20  ssert( pData!=0 
3589c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44 61  );.  assert( pDa
3589d 74 61 2b 6e 44 61 74 61 3e 70 44 61 74 61 20 29  ta+nData>pData )
3589e 3b 0a 0a 20 20 2f 2a 20 4d 75 73 74 20 6c 65 61  ;..  /* Must lea
3589f 64 20 77 69 74 68 20 68 65 69 67 68 74 20 6f 66  d with height of
358a0 20 6e 6f 64 65 20 61 73 20 61 20 76 61 72 69 6e   node as a varin
358a1 74 28 6e 29 2c 20 6e 3e 30 20 2a 2f 0a 20 20 6e  t(n), n>0 */.  n
358a2 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74   = fts3GetVarint
358a3 33 32 28 70 44 61 74 61 2c 20 26 69 44 75 6d 6d  32(pData, &iDumm
358a4 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3e  y);.  assert( n>
358a5 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  0 );.  assert( i
358a6 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 61 73 73  Dummy>0 );.  ass
358a7 65 72 74 28 20 6e 3c 6e 44 61 74 61 20 29 3b 0a  ert( n<nData );.
358a8 20 20 70 44 61 74 61 20 2b 3d 20 6e 3b 0a 20 20    pData += n;.  
358a9 6e 44 61 74 61 20 2d 3d 20 6e 3b 0a 0a 20 20 2f  nData -= n;..  /
358aa 2a 20 4d 75 73 74 20 63 6f 6e 74 61 69 6e 20 69  * Must contain i
358ab 42 6c 6f 63 6b 69 64 2e 20 2a 2f 0a 20 20 6e 20  Blockid. */.  n 
358ac 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 28  = fts3GetVarint(
358ad 70 44 61 74 61 2c 20 26 69 42 6c 6f 63 6b 69 64  pData, &iBlockid
358ae 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 30  );.  assert( n>0
358af 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3c   );.  assert( n<
358b0 3d 6e 44 61 74 61 20 29 3b 0a 20 20 70 44 61 74  =nData );.  pDat
358b1 61 20 2b 3d 20 6e 3b 0a 20 20 6e 44 61 74 61 20  a += n;.  nData 
358b2 2d 3d 20 6e 3b 0a 0a 20 20 2f 2a 20 5a 65 72 6f  -= n;..  /* Zero
358b3 20 6f 72 20 6d 6f 72 65 20 74 65 72 6d 73 20 6f   or more terms o
358b4 66 20 70 6f 73 69 74 69 76 65 20 6c 65 6e 67 74  f positive lengt
358b5 68 20 2a 2f 0a 20 20 69 66 28 20 6e 44 61 74 61  h */.  if( nData
358b6 21 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 46 69  !=0 ){.    /* Fi
358b7 72 73 74 20 74 65 72 6d 20 69 73 20 6e 6f 74 20  rst term is not 
358b8 64 65 6c 74 61 2d 65 6e 63 6f 64 65 64 2e 20 2a  delta-encoded. *
358b9 2f 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 47 65  /.    n = fts3Ge
358ba 74 56 61 72 69 6e 74 33 32 28 70 44 61 74 61 2c  tVarint32(pData,
358bb 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 61   &iDummy);.    a
358bc 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20  ssert( n>0 );.  
358bd 20 20 61 73 73 65 72 74 28 20 69 44 75 6d 6d 79    assert( iDummy
358be 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  >0 );.    assert
358bf 28 20 6e 2b 69 44 75 6d 6d 79 3e 30 29 3b 0a 20  ( n+iDummy>0);. 
358c0 20 20 20 61 73 73 65 72 74 28 20 6e 2b 69 44 75     assert( n+iDu
358c1 6d 6d 79 3c 3d 6e 44 61 74 61 20 29 3b 0a 20 20  mmy<=nData );.  
358c2 20 20 70 44 61 74 61 20 2b 3d 20 6e 2b 69 44 75    pData += n+iDu
358c3 6d 6d 79 3b 0a 20 20 20 20 6e 44 61 74 61 20 2d  mmy;.    nData -
358c4 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 0a 20 20 20  = n+iDummy;..   
358c5 20 2f 2a 20 46 6f 6c 6c 6f 77 69 6e 67 20 74 65   /* Following te
358c6 72 6d 73 20 64 65 6c 74 61 2d 65 6e 63 6f 64 65  rms delta-encode
358c7 64 2e 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28  d. */.    while(
358c8 20 6e 44 61 74 61 21 3d 30 20 29 7b 0a 20 20 20   nData!=0 ){.   
358c9 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20     /* Length of 
358ca 73 68 61 72 65 64 20 70 72 65 66 69 78 2e 20 2a  shared prefix. *
358cb 2f 0a 20 20 20 20 20 20 6e 20 3d 20 66 74 73 33  /.      n = fts3
358cc 47 65 74 56 61 72 69 6e 74 33 32 28 70 44 61 74  GetVarint32(pDat
358cd 61 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20  a, &iDummy);.   
358ce 20 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29     assert( n>0 )
358cf 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
358d0 69 44 75 6d 6d 79 3e 3d 30 20 29 3b 0a 20 20 20  iDummy>=0 );.   
358d1 20 20 20 61 73 73 65 72 74 28 20 6e 3c 6e 44 61     assert( n<nDa
358d2 74 61 20 29 3b 0a 20 20 20 20 20 20 70 44 61 74  ta );.      pDat
358d3 61 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 20 6e 44  a += n;.      nD
358d4 61 74 61 20 2d 3d 20 6e 3b 0a 0a 20 20 20 20 20  ata -= n;..     
358d5 20 2f 2a 20 4c 65 6e 67 74 68 20 61 6e 64 20 64   /* Length and d
358d6 61 74 61 20 6f 66 20 64 69 73 74 69 6e 63 74 20  ata of distinct 
358d7 73 75 66 66 69 78 2e 20 2a 2f 0a 20 20 20 20 20  suffix. */.     
358d8 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69   n = fts3GetVari
358d9 6e 74 33 32 28 70 44 61 74 61 2c 20 26 69 44 75  nt32(pData, &iDu
358da 6d 6d 79 29 3b 0a 20 20 20 20 20 20 61 73 73 65  mmy);.      asse
358db 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 20 20 20  rt( n>0 );.     
358dc 20 61 73 73 65 72 74 28 20 69 44 75 6d 6d 79 3e   assert( iDummy>
358dd 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  0 );.      asser
358de 74 28 20 6e 2b 69 44 75 6d 6d 79 3e 30 29 3b 0a  t( n+iDummy>0);.
358df 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b        assert( n+
358e0 69 44 75 6d 6d 79 3c 3d 6e 44 61 74 61 20 29 3b  iDummy<=nData );
358e1 0a 20 20 20 20 20 20 70 44 61 74 61 20 2b 3d 20  .      pData += 
358e2 6e 2b 69 44 75 6d 6d 79 3b 0a 20 20 20 20 20 20  n+iDummy;.      
358e3 6e 44 61 74 61 20 2d 3d 20 6e 2b 69 44 75 6d 6d  nData -= n+iDumm
358e4 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23  y;.    }.  }.}.#
358e5 64 65 66 69 6e 65 20 41 53 53 45 52 54 5f 56 41  define ASSERT_VA
358e6 4c 49 44 5f 49 4e 54 45 52 49 4f 52 5f 42 4c 4f  LID_INTERIOR_BLO
358e7 43 4b 28 78 29 20 69 6e 74 65 72 69 6f 72 42 6c  CK(x) interiorBl
358e8 6f 63 6b 56 61 6c 69 64 61 74 65 28 78 29 0a 23  ockValidate(x).#
358e9 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 41 53 53  else.#define ASS
358ea 45 52 54 5f 56 41 4c 49 44 5f 49 4e 54 45 52 49  ERT_VALID_INTERI
358eb 4f 52 5f 42 4c 4f 43 4b 28 78 29 20 61 73 73 65  OR_BLOCK(x) asse
358ec 72 74 28 20 31 20 29 0a 23 65 6e 64 69 66 0a 0a  rt( 1 ).#endif..
358ed 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 49  typedef struct I
358ee 6e 74 65 72 69 6f 72 57 72 69 74 65 72 20 7b 0a  nteriorWriter {.
358ef 20 20 69 6e 74 20 69 48 65 69 67 68 74 3b 20 20    int iHeight;  
358f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
358f1 20 2f 2a 20 66 72 6f 6d 20 30 20 61 74 20 6c 65   /* from 0 at le
358f2 61 76 65 73 2e 20 2a 2f 0a 20 20 49 6e 74 65 72  aves. */.  Inter
358f3 69 6f 72 42 6c 6f 63 6b 20 2a 66 69 72 73 74 2c  iorBlock *first,
358f4 20 2a 6c 61 73 74 3b 0a 20 20 73 74 72 75 63 74   *last;.  struct
358f5 20 49 6e 74 65 72 69 6f 72 57 72 69 74 65 72 20   InteriorWriter 
358f6 2a 70 61 72 65 6e 74 57 72 69 74 65 72 3b 0a 0a  *parentWriter;..
358f7 20 20 44 61 74 61 42 75 66 66 65 72 20 74 65 72    DataBuffer ter
358f8 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m;              
358f9 20 2f 2a 20 4c 61 73 74 20 74 65 72 6d 20 77 72   /* Last term wr
358fa 69 74 74 65 6e 20 74 6f 20 62 6c 6f 63 6b 20 22  itten to block "
358fb 6c 61 73 74 22 2e 20 2a 2f 0a 20 20 73 71 6c 69  last". */.  sqli
358fc 74 65 5f 69 6e 74 36 34 20 69 4f 70 65 6e 69 6e  te_int64 iOpenin
358fd 67 43 68 69 6c 64 42 6c 6f 63 6b 3b 20 2f 2a 20  gChildBlock; /* 
358fe 46 69 72 73 74 20 63 68 69 6c 64 20 62 6c 6f 63  First child bloc
358ff 6b 20 69 6e 20 62 6c 6f 63 6b 20 22 6c 61 73 74  k in block "last
35900 22 2e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44  ". */.#ifndef ND
35901 45 42 55 47 0a 20 20 73 71 6c 69 74 65 5f 69 6e  EBUG.  sqlite_in
35902 74 36 34 20 69 4c 61 73 74 43 68 69 6c 64 42 6c  t64 iLastChildBl
35903 6f 63 6b 3b 20 20 2f 2a 20 66 6f 72 20 63 6f 6e  ock;  /* for con
35904 73 69 73 74 65 6e 63 79 20 63 68 65 63 6b 73 2e  sistency checks.
35905 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 20 49 6e 74   */.#endif.} Int
35906 65 72 69 6f 72 57 72 69 74 65 72 3b 0a 0a 2f 2a  eriorWriter;../*
35907 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 20 69   Initialize an i
35908 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20 77 68 65  nterior node whe
35909 72 65 20 70 54 65 72 6d 5b 6e 54 65 72 6d 5d 20  re pTerm[nTerm] 
3590a 6d 61 72 6b 73 20 74 68 65 20 6c 65 66 74 6d 6f  marks the leftmo
3590b 73 74 0a 2a 2a 20 74 65 72 6d 20 69 6e 20 74 68  st.** term in th
3590c 65 20 74 72 65 65 2e 20 20 69 43 68 69 6c 64 42  e tree.  iChildB
3590d 6c 6f 63 6b 20 69 73 20 74 68 65 20 6c 65 66 74  lock is the left
3590e 6d 6f 73 74 20 63 68 69 6c 64 20 62 6c 6f 63 6b  most child block
3590f 20 61 74 20 74 68 65 0a 2a 2a 20 6e 65 78 74 20   at the.** next 
35910 6c 65 76 65 6c 20 64 6f 77 6e 20 74 68 65 20 74  level down the t
35911 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ree..*/.static v
35912 6f 69 64 20 69 6e 74 65 72 69 6f 72 57 72 69 74  oid interiorWrit
35913 65 72 49 6e 69 74 28 69 6e 74 20 69 48 65 69 67  erInit(int iHeig
35914 68 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ht, const char *
35915 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d  pTerm, int nTerm
35916 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
35917 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35918 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 43   sqlite_int64 iC
35919 68 69 6c 64 42 6c 6f 63 6b 2c 0a 20 20 20 20 20  hildBlock,.     
3591a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3591b 20 20 20 20 20 20 20 20 20 20 49 6e 74 65 72 69            Interi
3591c 6f 72 57 72 69 74 65 72 20 2a 70 57 72 69 74 65  orWriter *pWrite
3591d 72 29 7b 0a 20 20 49 6e 74 65 72 69 6f 72 42 6c  r){.  InteriorBl
3591e 6f 63 6b 20 2a 62 6c 6f 63 6b 3b 0a 20 20 61 73  ock *block;.  as
3591f 73 65 72 74 28 20 69 48 65 69 67 68 74 3e 30 20  sert( iHeight>0 
35920 29 3b 0a 20 20 43 4c 45 41 52 28 70 57 72 69 74  );.  CLEAR(pWrit
35921 65 72 29 3b 0a 0a 20 20 70 57 72 69 74 65 72 2d  er);..  pWriter-
35922 3e 69 48 65 69 67 68 74 20 3d 20 69 48 65 69 67  >iHeight = iHeig
35923 68 74 3b 0a 20 20 70 57 72 69 74 65 72 2d 3e 69  ht;.  pWriter->i
35924 4f 70 65 6e 69 6e 67 43 68 69 6c 64 42 6c 6f 63  OpeningChildBloc
35925 6b 20 3d 20 69 43 68 69 6c 64 42 6c 6f 63 6b 3b  k = iChildBlock;
35926 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
35927 20 20 70 57 72 69 74 65 72 2d 3e 69 4c 61 73 74    pWriter->iLast
35928 43 68 69 6c 64 42 6c 6f 63 6b 20 3d 20 69 43 68  ChildBlock = iCh
35929 69 6c 64 42 6c 6f 63 6b 3b 0a 23 65 6e 64 69 66  ildBlock;.#endif
3592a 0a 20 20 62 6c 6f 63 6b 20 3d 20 69 6e 74 65 72  .  block = inter
3592b 69 6f 72 42 6c 6f 63 6b 4e 65 77 28 69 48 65 69  iorBlockNew(iHei
3592c 67 68 74 2c 20 69 43 68 69 6c 64 42 6c 6f 63 6b  ght, iChildBlock
3592d 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b  , pTerm, nTerm);
3592e 0a 20 20 70 57 72 69 74 65 72 2d 3e 6c 61 73 74  .  pWriter->last
3592f 20 3d 20 70 57 72 69 74 65 72 2d 3e 66 69 72 73   = pWriter->firs
35930 74 20 3d 20 62 6c 6f 63 6b 3b 0a 20 20 41 53 53  t = block;.  ASS
35931 45 52 54 5f 56 41 4c 49 44 5f 49 4e 54 45 52 49  ERT_VALID_INTERI
35932 4f 52 5f 42 4c 4f 43 4b 28 70 57 72 69 74 65 72  OR_BLOCK(pWriter
35933 2d 3e 6c 61 73 74 29 3b 0a 20 20 64 61 74 61 42  ->last);.  dataB
35934 75 66 66 65 72 49 6e 69 74 28 26 70 57 72 69 74  ufferInit(&pWrit
35935 65 72 2d 3e 74 65 72 6d 2c 20 30 29 3b 0a 7d 0a  er->term, 0);.}.
35936 0a 2f 2a 20 41 70 70 65 6e 64 20 74 68 65 20 63  ./* Append the c
35937 68 69 6c 64 20 6e 6f 64 65 20 72 6f 6f 74 65 64  hild node rooted
35938 20 61 74 20 69 43 68 69 6c 64 42 6c 6f 63 6b 20   at iChildBlock 
35939 74 6f 20 74 68 65 20 69 6e 74 65 72 69 6f 72 20  to the interior 
3593a 6e 6f 64 65 2c 0a 2a 2a 20 77 69 74 68 20 70 54  node,.** with pT
3593b 65 72 6d 5b 6e 54 65 72 6d 5d 20 61 73 20 74 68  erm[nTerm] as th
3593c 65 20 6c 65 66 74 6d 6f 73 74 20 74 65 72 6d 20  e leftmost term 
3593d 69 6e 20 69 43 68 69 6c 64 42 6c 6f 63 6b 27 73  in iChildBlock's
3593e 20 73 75 62 74 72 65 65 2e 0a 2a 2f 0a 73 74 61   subtree..*/.sta
3593f 74 69 63 20 76 6f 69 64 20 69 6e 74 65 72 69 6f  tic void interio
35940 72 57 72 69 74 65 72 41 70 70 65 6e 64 28 49 6e  rWriterAppend(In
35941 74 65 72 69 6f 72 57 72 69 74 65 72 20 2a 70 57  teriorWriter *pW
35942 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20 20 20  riter,.         
35943 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35944 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
35945 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e  ar *pTerm, int n
35946 54 65 72 6d 2c 0a 20 20 20 20 20 20 20 20 20 20  Term,.          
35947 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35948 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e         sqlite_in
35949 74 36 34 20 69 43 68 69 6c 64 42 6c 6f 63 6b 29  t64 iChildBlock)
3594a 7b 0a 20 20 63 68 61 72 20 63 5b 56 41 52 49 4e  {.  char c[VARIN
3594b 54 5f 4d 41 58 2b 56 41 52 49 4e 54 5f 4d 41 58  T_MAX+VARINT_MAX
3594c 5d 3b 0a 20 20 69 6e 74 20 6e 2c 20 6e 50 72 65  ];.  int n, nPre
3594d 66 69 78 20 3d 20 30 3b 0a 0a 20 20 41 53 53 45  fix = 0;..  ASSE
3594e 52 54 5f 56 41 4c 49 44 5f 49 4e 54 45 52 49 4f  RT_VALID_INTERIO
3594f 52 5f 42 4c 4f 43 4b 28 70 57 72 69 74 65 72 2d  R_BLOCK(pWriter-
35950 3e 6c 61 73 74 29 3b 0a 0a 20 20 2f 2a 20 54 68  >last);..  /* Th
35951 65 20 66 69 72 73 74 20 74 65 72 6d 20 77 72 69  e first term wri
35952 74 74 65 6e 20 69 6e 74 6f 20 61 6e 20 69 6e 74  tten into an int
35953 65 72 69 6f 72 20 6e 6f 64 65 20 69 73 20 61 63  erior node is ac
35954 74 75 61 6c 6c 79 0a 20 20 2a 2a 20 61 73 73 6f  tually.  ** asso
35955 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
35956 73 65 63 6f 6e 64 20 63 68 69 6c 64 20 61 64 64  second child add
35957 65 64 20 28 74 68 65 20 66 69 72 73 74 20 63 68  ed (the first ch
35958 69 6c 64 20 77 61 73 20 61 64 64 65 64 0a 20 20  ild was added.  
35959 2a 2a 20 69 6e 20 69 6e 74 65 72 69 6f 72 57 72  ** in interiorWr
3595a 69 74 65 72 49 6e 69 74 2c 20 6f 72 20 69 6e 20  iterInit, or in 
3595b 74 68 65 20 69 66 20 63 6c 61 75 73 65 20 61 74  the if clause at
3595c 20 74 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20 74   the bottom of t
3595d 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f  his.  ** functio
3595e 6e 29 2e 20 20 54 68 61 74 20 74 65 72 6d 20 67  n).  That term g
3595f 65 74 73 20 65 6e 63 6f 64 65 64 20 73 74 72 61  ets encoded stra
35960 69 67 68 74 20 75 70 2c 20 77 69 74 68 20 6e 50  ight up, with nP
35961 72 65 66 69 78 20 6c 65 66 74 0a 20 20 2a 2a 20  refix left.  ** 
35962 61 74 20 30 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  at 0..  */.  if(
35963 20 70 57 72 69 74 65 72 2d 3e 74 65 72 6d 2e 6e   pWriter->term.n
35964 44 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20 6e  Data==0 ){.    n
35965 20 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74   = fts3PutVarint
35966 28 63 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 7d 65  (c, nTerm);.  }e
35967 6c 73 65 7b 0a 20 20 20 20 77 68 69 6c 65 28 20  lse{.    while( 
35968 6e 50 72 65 66 69 78 3c 70 57 72 69 74 65 72 2d  nPrefix<pWriter-
35969 3e 74 65 72 6d 2e 6e 44 61 74 61 20 26 26 0a 20  >term.nData &&. 
3596a 20 20 20 20 20 20 20 20 20 20 70 54 65 72 6d 5b            pTerm[
3596b 6e 50 72 65 66 69 78 5d 3d 3d 70 57 72 69 74 65  nPrefix]==pWrite
3596c 72 2d 3e 74 65 72 6d 2e 70 44 61 74 61 5b 6e 50  r->term.pData[nP
3596d 72 65 66 69 78 5d 20 29 7b 0a 20 20 20 20 20 20  refix] ){.      
3596e 6e 50 72 65 66 69 78 2b 2b 3b 0a 20 20 20 20 7d  nPrefix++;.    }
3596f 0a 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 50 75  ..    n = fts3Pu
35970 74 56 61 72 69 6e 74 28 63 2c 20 6e 50 72 65 66  tVarint(c, nPref
35971 69 78 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74  ix);.    n += ft
35972 73 33 50 75 74 56 61 72 69 6e 74 28 63 2b 6e 2c  s3PutVarint(c+n,
35973 20 6e 54 65 72 6d 2d 6e 50 72 65 66 69 78 29 3b   nTerm-nPrefix);
35974 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44  .  }..#ifndef ND
35975 45 42 55 47 0a 20 20 70 57 72 69 74 65 72 2d 3e  EBUG.  pWriter->
35976 69 4c 61 73 74 43 68 69 6c 64 42 6c 6f 63 6b 2b  iLastChildBlock+
35977 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 61 73 73 65  +;.#endif.  asse
35978 72 74 28 20 70 57 72 69 74 65 72 2d 3e 69 4c 61  rt( pWriter->iLa
35979 73 74 43 68 69 6c 64 42 6c 6f 63 6b 3d 3d 69 43  stChildBlock==iC
3597a 68 69 6c 64 42 6c 6f 63 6b 20 29 3b 0a 0a 20 20  hildBlock );..  
3597b 2f 2a 20 4f 76 65 72 66 6c 6f 77 20 74 6f 20 61  /* Overflow to a
3597c 20 6e 65 77 20 62 6c 6f 63 6b 20 69 66 20 74 68   new block if th
3597d 65 20 6e 65 77 20 74 65 72 6d 20 6d 61 6b 65 73  e new term makes
3597e 20 74 68 65 20 63 75 72 72 65 6e 74 20 62 6c 6f   the current blo
3597f 63 6b 0a 20 20 2a 2a 20 74 6f 6f 20 62 69 67 2c  ck.  ** too big,
35980 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74   and the current
35981 20 62 6c 6f 63 6b 20 61 6c 72 65 61 64 79 20 68   block already h
35982 61 73 20 65 6e 6f 75 67 68 20 74 65 72 6d 73 2e  as enough terms.
35983 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 57 72 69  .  */.  if( pWri
35984 74 65 72 2d 3e 6c 61 73 74 2d 3e 64 61 74 61 2e  ter->last->data.
35985 6e 44 61 74 61 2b 6e 2b 6e 54 65 72 6d 2d 6e 50  nData+n+nTerm-nP
35986 72 65 66 69 78 3e 49 4e 54 45 52 49 4f 52 5f 4d  refix>INTERIOR_M
35987 41 58 20 26 26 0a 20 20 20 20 20 20 69 43 68 69  AX &&.      iChi
35988 6c 64 42 6c 6f 63 6b 2d 70 57 72 69 74 65 72 2d  ldBlock-pWriter-
35989 3e 69 4f 70 65 6e 69 6e 67 43 68 69 6c 64 42 6c  >iOpeningChildBl
3598a 6f 63 6b 3e 49 4e 54 45 52 49 4f 52 5f 4d 49 4e  ock>INTERIOR_MIN
3598b 5f 54 45 52 4d 53 20 29 7b 0a 20 20 20 20 70 57  _TERMS ){.    pW
3598c 72 69 74 65 72 2d 3e 6c 61 73 74 2d 3e 6e 65 78  riter->last->nex
3598d 74 20 3d 20 69 6e 74 65 72 69 6f 72 42 6c 6f 63  t = interiorBloc
3598e 6b 4e 65 77 28 70 57 72 69 74 65 72 2d 3e 69 48  kNew(pWriter->iH
3598f 65 69 67 68 74 2c 20 69 43 68 69 6c 64 42 6c 6f  eight, iChildBlo
35990 63 6b 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ck,.            
35991 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35992 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
35993 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20  Term, nTerm);.  
35994 20 20 70 57 72 69 74 65 72 2d 3e 6c 61 73 74 20    pWriter->last 
35995 3d 20 70 57 72 69 74 65 72 2d 3e 6c 61 73 74 2d  = pWriter->last-
35996 3e 6e 65 78 74 3b 0a 20 20 20 20 70 57 72 69 74  >next;.    pWrit
35997 65 72 2d 3e 69 4f 70 65 6e 69 6e 67 43 68 69 6c  er->iOpeningChil
35998 64 42 6c 6f 63 6b 20 3d 20 69 43 68 69 6c 64 42  dBlock = iChildB
35999 6c 6f 63 6b 3b 0a 20 20 20 20 64 61 74 61 42 75  lock;.    dataBu
3599a 66 66 65 72 52 65 73 65 74 28 26 70 57 72 69 74  fferReset(&pWrit
3599b 65 72 2d 3e 74 65 72 6d 29 3b 0a 20 20 7d 65 6c  er->term);.  }el
3599c 73 65 7b 0a 20 20 20 20 64 61 74 61 42 75 66 66  se{.    dataBuff
3599d 65 72 41 70 70 65 6e 64 32 28 26 70 57 72 69 74  erAppend2(&pWrit
3599e 65 72 2d 3e 6c 61 73 74 2d 3e 64 61 74 61 2c 20  er->last->data, 
3599f 63 2c 20 6e 2c 0a 20 20 20 20 20 20 20 20 20 20  c, n,.          
359a0 20 20 20 20 20 20 20 20 20 20 20 20 70 54 65 72              pTer
359a1 6d 2b 6e 50 72 65 66 69 78 2c 20 6e 54 65 72 6d  m+nPrefix, nTerm
359a2 2d 6e 50 72 65 66 69 78 29 3b 0a 20 20 20 20 64  -nPrefix);.    d
359a3 61 74 61 42 75 66 66 65 72 52 65 70 6c 61 63 65  ataBufferReplace
359a4 28 26 70 57 72 69 74 65 72 2d 3e 74 65 72 6d 2c  (&pWriter->term,
359a5 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a   pTerm, nTerm);.
359a6 20 20 7d 0a 20 20 41 53 53 45 52 54 5f 56 41 4c    }.  ASSERT_VAL
359a7 49 44 5f 49 4e 54 45 52 49 4f 52 5f 42 4c 4f 43  ID_INTERIOR_BLOC
359a8 4b 28 70 57 72 69 74 65 72 2d 3e 6c 61 73 74 29  K(pWriter->last)
359a9 3b 0a 7d 0a 0a 2f 2a 20 46 72 65 65 20 74 68 65  ;.}../* Free the
359aa 20 73 70 61 63 65 20 75 73 65 64 20 62 79 20 70   space used by p
359ab 57 72 69 74 65 72 2c 20 69 6e 63 6c 75 64 69 6e  Writer, includin
359ac 67 20 74 68 65 20 6c 69 6e 6b 65 64 2d 6c 69 73  g the linked-lis
359ad 74 20 6f 66 0a 2a 2a 20 49 6e 74 65 72 69 6f 72  t of.** Interior
359ae 42 6c 6f 63 6b 73 2c 20 61 6e 64 20 70 61 72 65  Blocks, and pare
359af 6e 74 57 72 69 74 65 72 2c 20 69 66 20 70 72 65  ntWriter, if pre
359b0 73 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  sent..*/.static 
359b1 69 6e 74 20 69 6e 74 65 72 69 6f 72 57 72 69 74  int interiorWrit
359b2 65 72 44 65 73 74 72 6f 79 28 49 6e 74 65 72 69  erDestroy(Interi
359b3 6f 72 57 72 69 74 65 72 20 2a 70 57 72 69 74 65  orWriter *pWrite
359b4 72 29 7b 0a 20 20 49 6e 74 65 72 69 6f 72 42 6c  r){.  InteriorBl
359b5 6f 63 6b 20 2a 62 6c 6f 63 6b 20 3d 20 70 57 72  ock *block = pWr
359b6 69 74 65 72 2d 3e 66 69 72 73 74 3b 0a 0a 20 20  iter->first;..  
359b7 77 68 69 6c 65 28 20 62 6c 6f 63 6b 21 3d 4e 55  while( block!=NU
359b8 4c 4c 20 29 7b 0a 20 20 20 20 49 6e 74 65 72 69  LL ){.    Interi
359b9 6f 72 42 6c 6f 63 6b 20 2a 62 20 3d 20 62 6c 6f  orBlock *b = blo
359ba 63 6b 3b 0a 20 20 20 20 62 6c 6f 63 6b 20 3d 20  ck;.    block = 
359bb 62 6c 6f 63 6b 2d 3e 6e 65 78 74 3b 0a 20 20 20  block->next;.   
359bc 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72   dataBufferDestr
359bd 6f 79 28 26 62 2d 3e 74 65 72 6d 29 3b 0a 20 20  oy(&b->term);.  
359be 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74    dataBufferDest
359bf 72 6f 79 28 26 62 2d 3e 64 61 74 61 29 3b 0a 20  roy(&b->data);. 
359c0 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
359c1 62 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 57  b);.  }.  if( pW
359c2 72 69 74 65 72 2d 3e 70 61 72 65 6e 74 57 72 69  riter->parentWri
359c3 74 65 72 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20  ter!=NULL ){.   
359c4 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72 44   interiorWriterD
359c5 65 73 74 72 6f 79 28 70 57 72 69 74 65 72 2d 3e  estroy(pWriter->
359c6 70 61 72 65 6e 74 57 72 69 74 65 72 29 3b 0a 20  parentWriter);. 
359c7 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
359c8 70 57 72 69 74 65 72 2d 3e 70 61 72 65 6e 74 57  pWriter->parentW
359c9 72 69 74 65 72 29 3b 0a 20 20 7d 0a 20 20 64 61  riter);.  }.  da
359ca 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28  taBufferDestroy(
359cb 26 70 57 72 69 74 65 72 2d 3e 74 65 72 6d 29 3b  &pWriter->term);
359cc 0a 20 20 53 43 52 41 4d 42 4c 45 28 70 57 72 69  .  SCRAMBLE(pWri
359cd 74 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  ter);.  return S
359ce 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20  QLITE_OK;.}../* 
359cf 49 66 20 70 57 72 69 74 65 72 20 63 61 6e 20 66  If pWriter can f
359d0 69 74 20 65 6e 74 69 72 65 6c 79 20 69 6e 20 52  it entirely in R
359d1 4f 4f 54 5f 4d 41 58 2c 20 72 65 74 75 72 6e 20  OOT_MAX, return 
359d2 69 74 20 61 73 20 74 68 65 20 72 6f 6f 74 20 69  it as the root i
359d3 6e 66 6f 0a 2a 2a 20 64 69 72 65 63 74 6c 79 2c  nfo.** directly,
359d4 20 6c 65 61 76 69 6e 67 20 2a 70 69 45 6e 64 42   leaving *piEndB
359d5 6c 6f 63 6b 69 64 20 75 6e 63 68 61 6e 67 65 64  lockid unchanged
359d6 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 66 6c  .  Otherwise, fl
359d7 75 73 68 0a 2a 2a 20 70 57 72 69 74 65 72 20 74  ush.** pWriter t
359d8 6f 20 25 5f 73 65 67 6d 65 6e 74 73 2c 20 62 75  o %_segments, bu
359d9 69 6c 64 69 6e 67 20 61 20 6e 65 77 20 6c 61 79  ilding a new lay
359da 65 72 20 6f 66 20 69 6e 74 65 72 69 6f 72 20 6e  er of interior n
359db 6f 64 65 73 2c 20 61 6e 64 0a 2a 2a 20 72 65 63  odes, and.** rec
359dc 75 72 73 69 76 65 6c 79 20 61 73 6b 20 66 6f 72  ursively ask for
359dd 20 74 68 65 69 72 20 72 6f 6f 74 20 69 6e 74 6f   their root into
359de 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
359df 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72 52 6f  interiorWriterRo
359e0 6f 74 49 6e 66 6f 28 66 75 6c 6c 74 65 78 74 5f  otInfo(fulltext_
359e1 76 74 61 62 20 2a 76 2c 20 49 6e 74 65 72 69 6f  vtab *v, Interio
359e2 72 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72  rWriter *pWriter
359e3 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
359e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
359e5 20 20 20 20 63 68 61 72 20 2a 2a 70 70 52 6f 6f      char **ppRoo
359e6 74 49 6e 66 6f 2c 20 69 6e 74 20 2a 70 6e 52 6f  tInfo, int *pnRo
359e7 6f 74 49 6e 66 6f 2c 0a 20 20 20 20 20 20 20 20  otInfo,.        
359e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
359e9 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
359ea 5f 69 6e 74 36 34 20 2a 70 69 45 6e 64 42 6c 6f  _int64 *piEndBlo
359eb 63 6b 69 64 29 7b 0a 20 20 49 6e 74 65 72 69 6f  ckid){.  Interio
359ec 72 42 6c 6f 63 6b 20 2a 62 6c 6f 63 6b 20 3d 20  rBlock *block = 
359ed 70 57 72 69 74 65 72 2d 3e 66 69 72 73 74 3b 0a  pWriter->first;.
359ee 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
359ef 42 6c 6f 63 6b 69 64 20 3d 20 30 3b 0a 20 20 69  Blockid = 0;.  i
359f0 6e 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 49 66 20  nt rc;..  /* If 
359f1 77 65 20 63 61 6e 20 66 69 74 20 74 68 65 20 73  we can fit the s
359f2 65 67 6d 65 6e 74 20 69 6e 6c 69 6e 65 20 2a 2f  egment inline */
359f3 0a 20 20 69 66 28 20 62 6c 6f 63 6b 3d 3d 70 57  .  if( block==pW
359f4 72 69 74 65 72 2d 3e 6c 61 73 74 20 26 26 20 62  riter->last && b
359f5 6c 6f 63 6b 2d 3e 64 61 74 61 2e 6e 44 61 74 61  lock->data.nData
359f6 3c 52 4f 4f 54 5f 4d 41 58 20 29 7b 0a 20 20 20  <ROOT_MAX ){.   
359f7 20 2a 70 70 52 6f 6f 74 49 6e 66 6f 20 3d 20 62   *ppRootInfo = b
359f8 6c 6f 63 6b 2d 3e 64 61 74 61 2e 70 44 61 74 61  lock->data.pData
359f9 3b 0a 20 20 20 20 2a 70 6e 52 6f 6f 74 49 6e 66  ;.    *pnRootInf
359fa 6f 20 3d 20 62 6c 6f 63 6b 2d 3e 64 61 74 61 2e  o = block->data.
359fb 6e 44 61 74 61 3b 0a 20 20 20 20 72 65 74 75 72  nData;.    retur
359fc 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
359fd 0a 0a 20 20 2f 2a 20 46 6c 75 73 68 20 74 68 65  ..  /* Flush the
359fe 20 66 69 72 73 74 20 62 6c 6f 63 6b 20 74 6f 20   first block to 
359ff 25 5f 73 65 67 6d 65 6e 74 73 2c 20 61 6e 64 20  %_segments, and 
35a00 63 72 65 61 74 65 20 61 20 6e 65 77 20 6c 65 76  create a new lev
35a01 65 6c 20 6f 66 0a 20 20 2a 2a 20 69 6e 74 65 72  el of.  ** inter
35a02 69 6f 72 20 6e 6f 64 65 2e 0a 20 20 2a 2f 0a 20  ior node..  */. 
35a03 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 49 4e   ASSERT_VALID_IN
35a04 54 45 52 49 4f 52 5f 42 4c 4f 43 4b 28 62 6c 6f  TERIOR_BLOCK(blo
35a05 63 6b 29 3b 0a 20 20 72 63 20 3d 20 62 6c 6f 63  ck);.  rc = bloc
35a06 6b 5f 69 6e 73 65 72 74 28 76 2c 20 62 6c 6f 63  k_insert(v, bloc
35a07 6b 2d 3e 64 61 74 61 2e 70 44 61 74 61 2c 20 62  k->data.pData, b
35a08 6c 6f 63 6b 2d 3e 64 61 74 61 2e 6e 44 61 74 61  lock->data.nData
35a09 2c 20 26 69 42 6c 6f 63 6b 69 64 29 3b 0a 20 20  , &iBlockid);.  
35a0a 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
35a0b 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  K ) return rc;. 
35a0c 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64 20 3d   *piEndBlockid =
35a0d 20 69 42 6c 6f 63 6b 69 64 3b 0a 0a 20 20 70 57   iBlockid;..  pW
35a0e 72 69 74 65 72 2d 3e 70 61 72 65 6e 74 57 72 69  riter->parentWri
35a0f 74 65 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  ter = sqlite3_ma
35a10 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 70 57 72  lloc(sizeof(*pWr
35a11 69 74 65 72 2d 3e 70 61 72 65 6e 74 57 72 69 74  iter->parentWrit
35a12 65 72 29 29 3b 0a 20 20 69 6e 74 65 72 69 6f 72  er));.  interior
35a13 57 72 69 74 65 72 49 6e 69 74 28 70 57 72 69 74  WriterInit(pWrit
35a14 65 72 2d 3e 69 48 65 69 67 68 74 2b 31 2c 0a 20  er->iHeight+1,. 
35a15 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35a16 20 20 20 20 62 6c 6f 63 6b 2d 3e 74 65 72 6d 2e      block->term.
35a17 70 44 61 74 61 2c 20 62 6c 6f 63 6b 2d 3e 74 65  pData, block->te
35a18 72 6d 2e 6e 44 61 74 61 2c 0a 20 20 20 20 20 20  rm.nData,.      
35a19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
35a1a 42 6c 6f 63 6b 69 64 2c 20 70 57 72 69 74 65 72  Blockid, pWriter
35a1b 2d 3e 70 61 72 65 6e 74 57 72 69 74 65 72 29 3b  ->parentWriter);
35a1c 0a 0a 20 20 2f 2a 20 46 6c 75 73 68 20 61 64 64  ..  /* Flush add
35a1d 69 74 69 6f 6e 61 6c 20 62 6c 6f 63 6b 73 20 61  itional blocks a
35a1e 6e 64 20 61 70 70 65 6e 64 20 74 6f 20 74 68 65  nd append to the
35a1f 20 68 69 67 68 65 72 20 69 6e 74 65 72 69 6f 72   higher interior
35a20 0a 20 20 2a 2a 20 6e 6f 64 65 2e 0a 20 20 2a 2f  .  ** node..  */
35a21 0a 20 20 66 6f 72 28 62 6c 6f 63 6b 3d 62 6c 6f  .  for(block=blo
35a22 63 6b 2d 3e 6e 65 78 74 3b 20 62 6c 6f 63 6b 21  ck->next; block!
35a23 3d 4e 55 4c 4c 3b 20 62 6c 6f 63 6b 3d 62 6c 6f  =NULL; block=blo
35a24 63 6b 2d 3e 6e 65 78 74 29 7b 0a 20 20 20 20 41  ck->next){.    A
35a25 53 53 45 52 54 5f 56 41 4c 49 44 5f 49 4e 54 45  SSERT_VALID_INTE
35a26 52 49 4f 52 5f 42 4c 4f 43 4b 28 62 6c 6f 63 6b  RIOR_BLOCK(block
35a27 29 3b 0a 20 20 20 20 72 63 20 3d 20 62 6c 6f 63  );.    rc = bloc
35a28 6b 5f 69 6e 73 65 72 74 28 76 2c 20 62 6c 6f 63  k_insert(v, bloc
35a29 6b 2d 3e 64 61 74 61 2e 70 44 61 74 61 2c 20 62  k->data.pData, b
35a2a 6c 6f 63 6b 2d 3e 64 61 74 61 2e 6e 44 61 74 61  lock->data.nData
35a2b 2c 20 26 69 42 6c 6f 63 6b 69 64 29 3b 0a 20 20  , &iBlockid);.  
35a2c 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
35a2d 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
35a2e 0a 20 20 20 20 2a 70 69 45 6e 64 42 6c 6f 63 6b  .    *piEndBlock
35a2f 69 64 20 3d 20 69 42 6c 6f 63 6b 69 64 3b 0a 0a  id = iBlockid;..
35a30 20 20 20 20 69 6e 74 65 72 69 6f 72 57 72 69 74      interiorWrit
35a31 65 72 41 70 70 65 6e 64 28 70 57 72 69 74 65 72  erAppend(pWriter
35a32 2d 3e 70 61 72 65 6e 74 57 72 69 74 65 72 2c 0a  ->parentWriter,.
35a33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35a34 20 20 20 20 20 20 20 20 20 62 6c 6f 63 6b 2d 3e           block->
35a35 74 65 72 6d 2e 70 44 61 74 61 2c 20 62 6c 6f 63  term.pData, bloc
35a36 6b 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 2c 20 69  k->term.nData, i
35a37 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 7d 0a 0a 20  Blockid);.  }.. 
35a38 20 2f 2a 20 50 61 72 65 6e 74 20 6e 6f 64 65 20   /* Parent node 
35a39 67 65 74 73 20 74 68 65 20 63 68 61 6e 63 65 20  gets the chance 
35a3a 74 6f 20 62 65 20 74 68 65 20 72 6f 6f 74 2e 20  to be the root. 
35a3b 2a 2f 0a 20 20 72 65 74 75 72 6e 20 69 6e 74 65  */.  return inte
35a3c 72 69 6f 72 57 72 69 74 65 72 52 6f 6f 74 49 6e  riorWriterRootIn
35a3d 66 6f 28 76 2c 20 70 57 72 69 74 65 72 2d 3e 70  fo(v, pWriter->p
35a3e 61 72 65 6e 74 57 72 69 74 65 72 2c 0a 20 20 20  arentWriter,.   
35a3f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35a40 20 20 20 20 20 20 20 20 20 20 20 20 20 70 70 52               ppR
35a41 6f 6f 74 49 6e 66 6f 2c 20 70 6e 52 6f 6f 74 49  ootInfo, pnRootI
35a42 6e 66 6f 2c 20 70 69 45 6e 64 42 6c 6f 63 6b 69  nfo, piEndBlocki
35a43 64 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  d);.}../********
35a44 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35a45 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35a46 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35a47 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 49 6e 74  ********/./* Int
35a48 65 72 69 6f 72 52 65 61 64 65 72 20 69 73 20 75  eriorReader is u
35a49 73 65 64 20 74 6f 20 72 65 61 64 20 6f 66 66 20  sed to read off 
35a4a 74 68 65 20 64 61 74 61 20 66 72 6f 6d 20 61 6e  the data from an
35a4b 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 0a 2a   interior node.*
35a4c 2a 20 28 73 65 65 20 63 6f 6d 6d 65 6e 74 20 61  * (see comment a
35a4d 74 20 74 6f 70 20 6f 66 20 66 69 6c 65 20 66 6f  t top of file fo
35a4e 72 20 74 68 65 20 66 6f 72 6d 61 74 29 2e 0a 2a  r the format)..*
35a4f 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
35a50 20 49 6e 74 65 72 69 6f 72 52 65 61 64 65 72 20   InteriorReader 
35a51 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  {.  const char *
35a52 70 44 61 74 61 3b 0a 20 20 69 6e 74 20 6e 44 61  pData;.  int nDa
35a53 74 61 3b 0a 0a 20 20 44 61 74 61 42 75 66 66 65  ta;..  DataBuffe
35a54 72 20 74 65 72 6d 3b 20 20 20 20 20 20 20 20 20  r term;         
35a55 20 2f 2a 20 70 72 65 76 69 6f 75 73 20 74 65 72   /* previous ter
35a56 6d 2c 20 66 6f 72 20 64 65 63 6f 64 69 6e 67 20  m, for decoding 
35a57 74 65 72 6d 20 64 65 6c 74 61 2e 20 2a 2f 0a 0a  term delta. */..
35a58 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
35a59 42 6c 6f 63 6b 69 64 3b 0a 7d 20 49 6e 74 65 72  Blockid;.} Inter
35a5a 69 6f 72 52 65 61 64 65 72 3b 0a 0a 73 74 61 74  iorReader;..stat
35a5b 69 63 20 76 6f 69 64 20 69 6e 74 65 72 69 6f 72  ic void interior
35a5c 52 65 61 64 65 72 44 65 73 74 72 6f 79 28 49 6e  ReaderDestroy(In
35a5d 74 65 72 69 6f 72 52 65 61 64 65 72 20 2a 70 52  teriorReader *pR
35a5e 65 61 64 65 72 29 7b 0a 20 20 64 61 74 61 42 75  eader){.  dataBu
35a5f 66 66 65 72 44 65 73 74 72 6f 79 28 26 70 52 65  fferDestroy(&pRe
35a60 61 64 65 72 2d 3e 74 65 72 6d 29 3b 0a 20 20 53  ader->term);.  S
35a61 43 52 41 4d 42 4c 45 28 70 52 65 61 64 65 72 29  CRAMBLE(pReader)
35a62 3b 0a 7d 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65  ;.}../* TODO(she
35a63 73 73 29 20 54 68 65 20 61 73 73 65 72 74 69 6f  ss) The assertio
35a64 6e 73 20 61 72 65 20 67 72 65 61 74 2c 20 62 75  ns are great, bu
35a65 74 20 77 68 61 74 20 69 66 20 77 65 27 72 65 20  t what if we're 
35a66 69 6e 20 4e 44 45 42 55 47 0a 2a 2a 20 61 6e 64  in NDEBUG.** and
35a67 20 74 68 65 20 62 6c 6f 62 20 69 73 20 65 6d 70   the blob is emp
35a68 74 79 20 6f 72 20 6f 74 68 65 72 77 69 73 65 20  ty or otherwise 
35a69 63 6f 6e 74 61 69 6e 73 20 73 75 73 70 65 63 74  contains suspect
35a6a 20 64 61 74 61 3f 0a 2a 2f 0a 73 74 61 74 69 63   data?.*/.static
35a6b 20 76 6f 69 64 20 69 6e 74 65 72 69 6f 72 52 65   void interiorRe
35a6c 61 64 65 72 49 6e 69 74 28 63 6f 6e 73 74 20 63  aderInit(const c
35a6d 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20  har *pData, int 
35a6e 6e 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20  nData,.         
35a6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35a70 20 20 20 20 20 20 49 6e 74 65 72 69 6f 72 52 65        InteriorRe
35a71 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
35a72 20 20 69 6e 74 20 6e 2c 20 6e 54 65 72 6d 3b 0a    int n, nTerm;.
35a73 0a 20 20 2f 2a 20 52 65 71 75 69 72 65 20 61 74  .  /* Require at
35a74 20 6c 65 61 73 74 20 74 68 65 20 6c 65 61 64 69   least the leadi
35a75 6e 67 20 66 6c 61 67 20 62 79 74 65 20 2a 2f 0a  ng flag byte */.
35a76 20 20 61 73 73 65 72 74 28 20 6e 44 61 74 61 3e    assert( nData>
35a77 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
35a78 44 61 74 61 5b 30 5d 21 3d 27 5c 30 27 20 29 3b  Data[0]!='\0' );
35a79 0a 0a 20 20 43 4c 45 41 52 28 70 52 65 61 64 65  ..  CLEAR(pReade
35a7a 72 29 3b 0a 0a 20 20 2f 2a 20 44 65 63 6f 64 65  r);..  /* Decode
35a7b 20 74 68 65 20 62 61 73 65 20 62 6c 6f 63 6b 69   the base blocki
35a7c 64 2c 20 61 6e 64 20 73 65 74 20 74 68 65 20 63  d, and set the c
35a7d 75 72 73 6f 72 20 74 6f 20 74 68 65 20 66 69 72  ursor to the fir
35a7e 73 74 20 74 65 72 6d 2e 20 2a 2f 0a 20 20 6e 20  st term. */.  n 
35a7f 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 28  = fts3GetVarint(
35a80 70 44 61 74 61 2b 31 2c 20 26 70 52 65 61 64 65  pData+1, &pReade
35a81 72 2d 3e 69 42 6c 6f 63 6b 69 64 29 3b 0a 20 20  r->iBlockid);.  
35a82 61 73 73 65 72 74 28 20 31 2b 6e 3c 3d 6e 44 61  assert( 1+n<=nDa
35a83 74 61 20 29 3b 0a 20 20 70 52 65 61 64 65 72 2d  ta );.  pReader-
35a84 3e 70 44 61 74 61 20 3d 20 70 44 61 74 61 2b 31  >pData = pData+1
35a85 2b 6e 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 6e  +n;.  pReader->n
35a86 44 61 74 61 20 3d 20 6e 44 61 74 61 2d 28 31 2b  Data = nData-(1+
35a87 6e 29 3b 0a 0a 20 20 2f 2a 20 41 20 73 69 6e 67  n);..  /* A sing
35a88 6c 65 2d 63 68 69 6c 64 20 69 6e 74 65 72 69 6f  le-child interio
35a89 72 20 6e 6f 64 65 20 28 73 75 63 68 20 61 73 20  r node (such as 
35a8a 77 68 65 6e 20 61 20 6c 65 61 66 20 6e 6f 64 65  when a leaf node
35a8b 20 77 61 73 20 74 6f 6f 0a 20 20 2a 2a 20 6c 61   was too.  ** la
35a8c 72 67 65 20 66 6f 72 20 74 68 65 20 73 65 67 6d  rge for the segm
35a8d 65 6e 74 20 64 69 72 65 63 74 6f 72 79 29 20 77  ent directory) w
35a8e 6f 6e 27 74 20 68 61 76 65 20 61 6e 79 20 74 65  on't have any te
35a8f 72 6d 73 2e 0a 20 20 2a 2a 20 4f 74 68 65 72 77  rms..  ** Otherw
35a90 69 73 65 2c 20 64 65 63 6f 64 65 20 74 68 65 20  ise, decode the 
35a91 66 69 72 73 74 20 74 65 72 6d 2e 0a 20 20 2a 2f  first term..  */
35a92 0a 20 20 69 66 28 20 70 52 65 61 64 65 72 2d 3e  .  if( pReader->
35a93 6e 44 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20  nData==0 ){.    
35a94 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26  dataBufferInit(&
35a95 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2c 20 30  pReader->term, 0
35a96 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
35a97 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e  n = fts3GetVarin
35a98 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61  t32(pReader->pDa
35a99 74 61 2c 20 26 6e 54 65 72 6d 29 3b 0a 20 20 20  ta, &nTerm);.   
35a9a 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28   dataBufferInit(
35a9b 26 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2c 20  &pReader->term, 
35a9c 6e 54 65 72 6d 29 3b 0a 20 20 20 20 64 61 74 61  nTerm);.    data
35a9d 42 75 66 66 65 72 52 65 70 6c 61 63 65 28 26 70  BufferReplace(&p
35a9e 52 65 61 64 65 72 2d 3e 74 65 72 6d 2c 20 70 52  Reader->term, pR
35a9f 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20  eader->pData+n, 
35aa0 6e 54 65 72 6d 29 3b 0a 20 20 20 20 61 73 73 65  nTerm);.    asse
35aa1 72 74 28 20 6e 2b 6e 54 65 72 6d 3c 3d 70 52 65  rt( n+nTerm<=pRe
35aa2 61 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20  ader->nData );. 
35aa3 20 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74     pReader->pDat
35aa4 61 20 2b 3d 20 6e 2b 6e 54 65 72 6d 3b 0a 20 20  a += n+nTerm;.  
35aa5 20 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61    pReader->nData
35aa6 20 2d 3d 20 6e 2b 6e 54 65 72 6d 3b 0a 20 20 7d   -= n+nTerm;.  }
35aa7 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .}..static int i
35aa8 6e 74 65 72 69 6f 72 52 65 61 64 65 72 41 74 45  nteriorReaderAtE
35aa9 6e 64 28 49 6e 74 65 72 69 6f 72 52 65 61 64 65  nd(InteriorReade
35aaa 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 72  r *pReader){.  r
35aab 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 74  eturn pReader->t
35aac 65 72 6d 2e 6e 44 61 74 61 3d 3d 30 3b 0a 7d 0a  erm.nData==0;.}.
35aad 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f 69  .static sqlite_i
35aae 6e 74 36 34 20 69 6e 74 65 72 69 6f 72 52 65 61  nt64 interiorRea
35aaf 64 65 72 43 75 72 72 65 6e 74 42 6c 6f 63 6b 69  derCurrentBlocki
35ab0 64 28 49 6e 74 65 72 69 6f 72 52 65 61 64 65 72  d(InteriorReader
35ab1 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 72 65   *pReader){.  re
35ab2 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 69 42  turn pReader->iB
35ab3 6c 6f 63 6b 69 64 3b 0a 7d 0a 0a 73 74 61 74 69  lockid;.}..stati
35ab4 63 20 69 6e 74 20 69 6e 74 65 72 69 6f 72 52 65  c int interiorRe
35ab5 61 64 65 72 54 65 72 6d 42 79 74 65 73 28 49 6e  aderTermBytes(In
35ab6 74 65 72 69 6f 72 52 65 61 64 65 72 20 2a 70 52  teriorReader *pR
35ab7 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74  eader){.  assert
35ab8 28 20 21 69 6e 74 65 72 69 6f 72 52 65 61 64 65  ( !interiorReade
35ab9 72 41 74 45 6e 64 28 70 52 65 61 64 65 72 29 20  rAtEnd(pReader) 
35aba 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61  );.  return pRea
35abb 64 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 3b  der->term.nData;
35abc 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .}.static const 
35abd 63 68 61 72 20 2a 69 6e 74 65 72 69 6f 72 52 65  char *interiorRe
35abe 61 64 65 72 54 65 72 6d 28 49 6e 74 65 72 69 6f  aderTerm(Interio
35abf 72 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  rReader *pReader
35ac0 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 69 6e  ){.  assert( !in
35ac1 74 65 72 69 6f 72 52 65 61 64 65 72 41 74 45 6e  teriorReaderAtEn
35ac2 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 20 20  d(pReader) );.  
35ac3 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e  return pReader->
35ac4 74 65 72 6d 2e 70 44 61 74 61 3b 0a 7d 0a 0a 2f  term.pData;.}../
35ac5 2a 20 53 74 65 70 20 66 6f 72 77 61 72 64 20 74  * Step forward t
35ac6 6f 20 74 68 65 20 6e 65 78 74 20 74 65 72 6d 20  o the next term 
35ac7 69 6e 20 74 68 65 20 6e 6f 64 65 2e 20 2a 2f 0a  in the node. */.
35ac8 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 74 65  static void inte
35ac9 72 69 6f 72 52 65 61 64 65 72 53 74 65 70 28 49  riorReaderStep(I
35aca 6e 74 65 72 69 6f 72 52 65 61 64 65 72 20 2a 70  nteriorReader *p
35acb 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72  Reader){.  asser
35acc 74 28 20 21 69 6e 74 65 72 69 6f 72 52 65 61 64  t( !interiorRead
35acd 65 72 41 74 45 6e 64 28 70 52 65 61 64 65 72 29  erAtEnd(pReader)
35ace 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65   );..  /* If the
35acf 20 6c 61 73 74 20 74 65 72 6d 20 68 61 73 20 62   last term has b
35ad0 65 65 6e 20 72 65 61 64 2c 20 73 69 67 6e 61 6c  een read, signal
35ad1 20 65 6f 66 2c 20 65 6c 73 65 20 63 6f 6e 73 74   eof, else const
35ad2 72 75 63 74 20 74 68 65 0a 20 20 2a 2a 20 6e 65  ruct the.  ** ne
35ad3 78 74 20 74 65 72 6d 2e 0a 20 20 2a 2f 0a 20 20  xt term..  */.  
35ad4 69 66 28 20 70 52 65 61 64 65 72 2d 3e 6e 44 61  if( pReader->nDa
35ad5 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20 64 61 74  ta==0 ){.    dat
35ad6 61 42 75 66 66 65 72 52 65 73 65 74 28 26 70 52  aBufferReset(&pR
35ad7 65 61 64 65 72 2d 3e 74 65 72 6d 29 3b 0a 20 20  eader->term);.  
35ad8 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 6e  }else{.    int n
35ad9 2c 20 6e 50 72 65 66 69 78 2c 20 6e 53 75 66 66  , nPrefix, nSuff
35ada 69 78 3b 0a 0a 20 20 20 20 6e 20 3d 20 66 74 73  ix;..    n = fts
35adb 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 52 65  3GetVarint32(pRe
35adc 61 64 65 72 2d 3e 70 44 61 74 61 2c 20 26 6e 50  ader->pData, &nP
35add 72 65 66 69 78 29 3b 0a 20 20 20 20 6e 20 2b 3d  refix);.    n +=
35ade 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32   fts3GetVarint32
35adf 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b  (pReader->pData+
35ae0 6e 2c 20 26 6e 53 75 66 66 69 78 29 3b 0a 0a 20  n, &nSuffix);.. 
35ae1 20 20 20 2f 2a 20 54 72 75 6e 63 61 74 65 20 74     /* Truncate t
35ae2 68 65 20 63 75 72 72 65 6e 74 20 74 65 72 6d 20  he current term 
35ae3 61 6e 64 20 61 70 70 65 6e 64 20 73 75 66 66 69  and append suffi
35ae4 78 20 64 61 74 61 2e 20 2a 2f 0a 20 20 20 20 70  x data. */.    p
35ae5 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44 61  Reader->term.nDa
35ae6 74 61 20 3d 20 6e 50 72 65 66 69 78 3b 0a 20 20  ta = nPrefix;.  
35ae7 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65    dataBufferAppe
35ae8 6e 64 28 26 70 52 65 61 64 65 72 2d 3e 74 65 72  nd(&pReader->ter
35ae9 6d 2c 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74  m, pReader->pDat
35aea 61 2b 6e 2c 20 6e 53 75 66 66 69 78 29 3b 0a 0a  a+n, nSuffix);..
35aeb 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b 6e 53      assert( n+nS
35aec 75 66 66 69 78 3c 3d 70 52 65 61 64 65 72 2d 3e  uffix<=pReader->
35aed 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 70 52 65  nData );.    pRe
35aee 61 64 65 72 2d 3e 70 44 61 74 61 20 2b 3d 20 6e  ader->pData += n
35aef 2b 6e 53 75 66 66 69 78 3b 0a 20 20 20 20 70 52  +nSuffix;.    pR
35af0 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 2d 3d 20  eader->nData -= 
35af1 6e 2b 6e 53 75 66 66 69 78 3b 0a 20 20 7d 0a 20  n+nSuffix;.  }. 
35af2 20 70 52 65 61 64 65 72 2d 3e 69 42 6c 6f 63 6b   pReader->iBlock
35af3 69 64 2b 2b 3b 0a 7d 0a 0a 2f 2a 20 43 6f 6d 70  id++;.}../* Comp
35af4 61 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  are the current 
35af5 74 65 72 6d 20 74 6f 20 70 54 65 72 6d 5b 6e 54  term to pTerm[nT
35af6 65 72 6d 5d 2c 20 72 65 74 75 72 6e 69 6e 67 20  erm], returning 
35af7 73 74 72 63 6d 70 2d 73 74 79 6c 65 0a 2a 2a 20  strcmp-style.** 
35af8 72 65 73 75 6c 74 73 2e 20 20 49 66 20 69 73 50  results.  If isP
35af9 72 65 66 69 78 2c 20 65 71 75 61 6c 69 74 79 20  refix, equality 
35afa 6d 65 61 6e 73 20 65 71 75 61 6c 20 74 68 72 6f  means equal thro
35afb 75 67 68 20 6e 54 65 72 6d 20 62 79 74 65 73 2e  ugh nTerm bytes.
35afc 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .*/.static int i
35afd 6e 74 65 72 69 6f 72 52 65 61 64 65 72 54 65 72  nteriorReaderTer
35afe 6d 43 6d 70 28 49 6e 74 65 72 69 6f 72 52 65 61  mCmp(InteriorRea
35aff 64 65 72 20 2a 70 52 65 61 64 65 72 2c 0a 20 20  der *pReader,.  
35b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35b01 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
35b02 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d  onst char *pTerm
35b03 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20 69 6e 74  , int nTerm, int
35b04 20 69 73 50 72 65 66 69 78 29 7b 0a 20 20 63 6f   isPrefix){.  co
35b05 6e 73 74 20 63 68 61 72 20 2a 70 52 65 61 64 65  nst char *pReade
35b06 72 54 65 72 6d 20 3d 20 69 6e 74 65 72 69 6f 72  rTerm = interior
35b07 52 65 61 64 65 72 54 65 72 6d 28 70 52 65 61 64  ReaderTerm(pRead
35b08 65 72 29 3b 0a 20 20 69 6e 74 20 6e 52 65 61 64  er);.  int nRead
35b09 65 72 54 65 72 6d 20 3d 20 69 6e 74 65 72 69 6f  erTerm = interio
35b0a 72 52 65 61 64 65 72 54 65 72 6d 42 79 74 65 73  rReaderTermBytes
35b0b 28 70 52 65 61 64 65 72 29 3b 0a 20 20 69 6e 74  (pReader);.  int
35b0c 20 63 2c 20 6e 20 3d 20 6e 52 65 61 64 65 72 54   c, n = nReaderT
35b0d 65 72 6d 3c 6e 54 65 72 6d 20 3f 20 6e 52 65 61  erm<nTerm ? nRea
35b0e 64 65 72 54 65 72 6d 20 3a 20 6e 54 65 72 6d 3b  derTerm : nTerm;
35b0f 0a 0a 20 20 69 66 28 20 6e 3d 3d 30 20 29 7b 0a  ..  if( n==0 ){.
35b10 20 20 20 20 69 66 28 20 6e 52 65 61 64 65 72 54      if( nReaderT
35b11 65 72 6d 3e 30 20 29 20 72 65 74 75 72 6e 20 2d  erm>0 ) return -
35b12 31 3b 0a 20 20 20 20 69 66 28 20 6e 54 65 72 6d  1;.    if( nTerm
35b13 3e 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20  >0 ) return 1;. 
35b14 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
35b15 0a 0a 20 20 63 20 3d 20 6d 65 6d 63 6d 70 28 70  ..  c = memcmp(p
35b16 52 65 61 64 65 72 54 65 72 6d 2c 20 70 54 65 72  ReaderTerm, pTer
35b17 6d 2c 20 6e 29 3b 0a 20 20 69 66 28 20 63 21 3d  m, n);.  if( c!=
35b18 30 20 29 20 72 65 74 75 72 6e 20 63 3b 0a 20 20  0 ) return c;.  
35b19 69 66 28 20 69 73 50 72 65 66 69 78 20 26 26 20  if( isPrefix && 
35b1a 6e 3d 3d 6e 54 65 72 6d 20 29 20 72 65 74 75 72  n==nTerm ) retur
35b1b 6e 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 6e 52  n 0;.  return nR
35b1c 65 61 64 65 72 54 65 72 6d 20 2d 20 6e 54 65 72  eaderTerm - nTer
35b1d 6d 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  m;.}../*********
35b1e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35b1f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35b21 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 4c 65 61 66  *******/./* Leaf
35b22 57 72 69 74 65 72 20 69 73 20 75 73 65 64 20 74  Writer is used t
35b23 6f 20 63 6f 6c 6c 65 63 74 20 74 65 72 6d 73 20  o collect terms 
35b24 61 6e 64 20 61 73 73 6f 63 69 61 74 65 64 20 64  and associated d
35b25 6f 63 6c 69 73 74 20 64 61 74 61 0a 2a 2a 20 69  oclist data.** i
35b26 6e 74 6f 20 6c 65 61 66 20 62 6c 6f 63 6b 73 20  nto leaf blocks 
35b27 69 6e 20 25 5f 73 65 67 6d 65 6e 74 73 20 28 73  in %_segments (s
35b28 65 65 20 74 6f 70 20 6f 66 20 66 69 6c 65 20 66  ee top of file f
35b29 6f 72 20 66 6f 72 6d 61 74 20 69 6e 66 6f 29 2e  or format info).
35b2a 0a 2a 2a 20 45 78 70 65 63 74 65 64 20 75 73 61  .** Expected usa
35b2b 67 65 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 4c 65 61  ge is:.**.** Lea
35b2c 66 57 72 69 74 65 72 20 77 72 69 74 65 72 3b 0a  fWriter writer;.
35b2d 2a 2a 20 6c 65 61 66 57 72 69 74 65 72 49 6e 69  ** leafWriterIni
35b2e 74 28 30 2c 20 30 2c 20 26 77 72 69 74 65 72 29  t(0, 0, &writer)
35b2f 3b 0a 2a 2a 20 77 68 69 6c 65 28 20 73 6f 72 74  ;.** while( sort
35b30 65 64 5f 74 65 72 6d 73 5f 6c 65 66 74 5f 74 6f  ed_terms_left_to
35b31 5f 70 72 6f 63 65 73 73 20 29 7b 0a 2a 2a 20 20  _process ){.**  
35b32 20 2f 2f 20 64 61 74 61 20 69 73 20 64 6f 63 6c   // data is docl
35b33 69 73 74 20 64 61 74 61 20 66 6f 72 20 74 68 61  ist data for tha
35b34 74 20 74 65 72 6d 2e 0a 2a 2a 20 20 20 72 63 20  t term..**   rc 
35b35 3d 20 6c 65 61 66 57 72 69 74 65 72 53 74 65 70  = leafWriterStep
35b36 28 76 2c 20 26 77 72 69 74 65 72 2c 20 70 54 65  (v, &writer, pTe
35b37 72 6d 2c 20 6e 54 65 72 6d 2c 20 70 44 61 74 61  rm, nTerm, pData
35b38 2c 20 6e 44 61 74 61 29 3b 0a 2a 2a 20 20 20 69  , nData);.**   i
35b39 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
35b3a 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 2a 2a 20   ) goto err;.** 
35b3b 7d 0a 2a 2a 20 72 63 20 3d 20 6c 65 61 66 57 72  }.** rc = leafWr
35b3c 69 74 65 72 46 69 6e 61 6c 69 7a 65 28 76 2c 20  iterFinalize(v, 
35b3d 26 77 72 69 74 65 72 29 3b 0a 2a 2a 65 72 72 3a  &writer);.**err:
35b3e 0a 2a 2a 20 6c 65 61 66 57 72 69 74 65 72 44 65  .** leafWriterDe
35b3f 73 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b 0a  stroy(&writer);.
35b40 2a 2a 20 72 65 74 75 72 6e 20 72 63 3b 0a 2a 2a  ** return rc;.**
35b41 0a 2a 2a 20 6c 65 61 66 57 72 69 74 65 72 53 74  .** leafWriterSt
35b42 65 70 28 29 20 6d 61 79 20 77 72 69 74 65 20 61  ep() may write a
35b43 20 63 6f 6c 6c 65 63 74 65 64 20 6c 65 61 66 20   collected leaf 
35b44 6f 75 74 20 74 6f 20 25 5f 73 65 67 6d 65 6e 74  out to %_segment
35b45 73 2e 0a 2a 2a 20 6c 65 61 66 57 72 69 74 65 72  s..** leafWriter
35b46 46 69 6e 61 6c 69 7a 65 28 29 20 66 69 6e 69 73  Finalize() finis
35b47 68 65 73 20 77 72 69 74 69 6e 67 20 61 6e 79 20  hes writing any 
35b48 62 75 66 66 65 72 65 64 20 64 61 74 61 20 61 6e  buffered data an
35b49 64 20 73 74 6f 72 65 73 0a 2a 2a 20 61 20 72 6f  d stores.** a ro
35b4a 6f 74 20 6e 6f 64 65 20 69 6e 20 25 5f 73 65 67  ot node in %_seg
35b4b 64 69 72 2e 20 20 6c 65 61 66 57 72 69 74 65 72  dir.  leafWriter
35b4c 44 65 73 74 72 6f 79 28 29 20 66 72 65 65 73 20  Destroy() frees 
35b4d 61 6c 6c 20 62 75 66 66 65 72 73 20 61 6e 64 0a  all buffers and.
35b4e 2a 2a 20 49 6e 74 65 72 69 6f 72 57 72 69 74 65  ** InteriorWrite
35b4f 72 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 73 20  rs allocated as 
35b50 70 61 72 74 20 6f 66 20 77 72 69 74 69 6e 67 20  part of writing 
35b51 74 68 69 73 20 73 65 67 6d 65 6e 74 2e 0a 2a 2a  this segment..**
35b52 0a 2a 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  .** TODO(shess) 
35b53 44 6f 63 75 6d 65 6e 74 20 6c 65 61 66 57 72 69  Document leafWri
35b54 74 65 72 53 74 65 70 4d 65 72 67 65 28 29 2e 0a  terStepMerge()..
35b55 2a 2f 0a 0a 2f 2a 20 50 75 74 20 74 65 72 6d 73  */../* Put terms
35b56 20 77 69 74 68 20 64 61 74 61 20 74 68 69 73 20   with data this 
35b57 62 69 67 20 69 6e 20 74 68 65 69 72 20 6f 77 6e  big in their own
35b58 20 62 6c 6f 63 6b 2e 20 2a 2f 0a 23 64 65 66 69   block. */.#defi
35b59 6e 65 20 53 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49  ne STANDALONE_MI
35b5a 4e 20 31 30 32 34 0a 0a 2f 2a 20 4b 65 65 70 20  N 1024../* Keep 
35b5b 6c 65 61 66 20 62 6c 6f 63 6b 73 20 62 65 6c 6f  leaf blocks belo
35b5c 77 20 74 68 69 73 20 73 69 7a 65 2e 20 2a 2f 0a  w this size. */.
35b5d 23 64 65 66 69 6e 65 20 4c 45 41 46 5f 4d 41 58  #define LEAF_MAX
35b5e 20 32 30 34 38 0a 0a 74 79 70 65 64 65 66 20 73   2048..typedef s
35b5f 74 72 75 63 74 20 4c 65 61 66 57 72 69 74 65 72  truct LeafWriter
35b60 20 7b 0a 20 20 69 6e 74 20 69 4c 65 76 65 6c 3b   {.  int iLevel;
35b61 0a 20 20 69 6e 74 20 69 64 78 3b 0a 20 20 73 71  .  int idx;.  sq
35b62 6c 69 74 65 5f 69 6e 74 36 34 20 69 53 74 61 72  lite_int64 iStar
35b63 74 42 6c 6f 63 6b 69 64 3b 20 20 20 20 20 2f 2a  tBlockid;     /*
35b64 20 6e 65 65 64 65 64 20 74 6f 20 63 72 65 61 74   needed to creat
35b65 65 20 74 68 65 20 72 6f 6f 74 20 69 6e 66 6f 20  e the root info 
35b66 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  */.  sqlite_int6
35b67 34 20 69 45 6e 64 42 6c 6f 63 6b 69 64 3b 20 20  4 iEndBlockid;  
35b68 20 20 20 20 20 2f 2a 20 77 68 65 6e 20 77 65 27       /* when we'
35b69 72 65 20 64 6f 6e 65 20 77 72 69 74 69 6e 67 2e  re done writing.
35b6a 20 2a 2f 0a 0a 20 20 44 61 74 61 42 75 66 66 65   */..  DataBuffe
35b6b 72 20 74 65 72 6d 3b 20 20 20 20 20 20 20 20 20  r term;         
35b6c 20 20 20 20 20 20 20 2f 2a 20 70 72 65 76 69 6f         /* previo
35b6d 75 73 20 65 6e 63 6f 64 65 64 20 74 65 72 6d 20  us encoded term 
35b6e 2a 2f 0a 20 20 44 61 74 61 42 75 66 66 65 72 20  */.  DataBuffer 
35b6f 64 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20  data;           
35b70 20 20 20 20 20 2f 2a 20 65 6e 63 6f 64 69 6e 67       /* encoding
35b71 20 62 75 66 66 65 72 20 2a 2f 0a 0a 20 20 2f 2a   buffer */..  /*
35b72 20 62 79 74 65 73 20 6f 66 20 66 69 72 73 74 20   bytes of first 
35b73 74 65 72 6d 20 69 6e 20 74 68 65 20 63 75 72 72  term in the curr
35b74 65 6e 74 20 6e 6f 64 65 20 77 68 69 63 68 20 64  ent node which d
35b75 69 73 74 69 6e 67 75 69 73 68 65 73 20 74 68 61  istinguishes tha
35b76 74 0a 20 20 2a 2a 20 74 65 72 6d 20 66 72 6f 6d  t.  ** term from
35b77 20 74 68 65 20 6c 61 73 74 20 74 65 72 6d 20 6f   the last term o
35b78 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 6e  f the previous n
35b79 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20  ode..  */.  int 
35b7a 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 3b 0a 0a  nTermDistinct;..
35b7b 20 20 49 6e 74 65 72 69 6f 72 57 72 69 74 65 72    InteriorWriter
35b7c 20 70 61 72 65 6e 74 57 72 69 74 65 72 3b 20 20   parentWriter;  
35b7d 20 20 2f 2a 20 69 66 20 77 65 20 6f 76 65 72 66    /* if we overf
35b7e 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 68 61 73  low */.  int has
35b7f 5f 70 61 72 65 6e 74 3b 0a 7d 20 4c 65 61 66 57  _parent;.} LeafW
35b80 72 69 74 65 72 3b 0a 0a 73 74 61 74 69 63 20 76  riter;..static v
35b81 6f 69 64 20 6c 65 61 66 57 72 69 74 65 72 49 6e  oid leafWriterIn
35b82 69 74 28 69 6e 74 20 69 4c 65 76 65 6c 2c 20 69  it(int iLevel, i
35b83 6e 74 20 69 64 78 2c 20 4c 65 61 66 57 72 69 74  nt idx, LeafWrit
35b84 65 72 20 2a 70 57 72 69 74 65 72 29 7b 0a 20 20  er *pWriter){.  
35b85 43 4c 45 41 52 28 70 57 72 69 74 65 72 29 3b 0a  CLEAR(pWriter);.
35b86 20 20 70 57 72 69 74 65 72 2d 3e 69 4c 65 76 65    pWriter->iLeve
35b87 6c 20 3d 20 69 4c 65 76 65 6c 3b 0a 20 20 70 57  l = iLevel;.  pW
35b88 72 69 74 65 72 2d 3e 69 64 78 20 3d 20 69 64 78  riter->idx = idx
35b89 3b 0a 0a 20 20 64 61 74 61 42 75 66 66 65 72 49  ;..  dataBufferI
35b8a 6e 69 74 28 26 70 57 72 69 74 65 72 2d 3e 74 65  nit(&pWriter->te
35b8b 72 6d 2c 20 33 32 29 3b 0a 0a 20 20 2f 2a 20 53  rm, 32);..  /* S
35b8c 74 61 72 74 20 6f 75 74 20 77 69 74 68 20 61 20  tart out with a 
35b8d 72 65 61 73 6f 6e 61 62 6c 79 20 73 69 7a 65 64  reasonably sized
35b8e 20 62 6c 6f 63 6b 2c 20 74 68 6f 75 67 68 20 69   block, though i
35b8f 74 20 63 61 6e 20 67 72 6f 77 2e 20 2a 2f 0a 20  t can grow. */. 
35b90 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28   dataBufferInit(
35b91 26 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2c 20  &pWriter->data, 
35b92 4c 45 41 46 5f 4d 41 58 29 3b 0a 7d 0a 0a 23 69  LEAF_MAX);.}..#i
35b93 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 20  fndef NDEBUG./* 
35b94 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20  Verify that the 
35b95 64 61 74 61 20 69 73 20 72 65 61 64 61 62 6c 65  data is readable
35b96 20 61 73 20 61 20 6c 65 61 66 20 6e 6f 64 65 2e   as a leaf node.
35b97 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20   */.static void 
35b98 6c 65 61 66 4e 6f 64 65 56 61 6c 69 64 61 74 65  leafNodeValidate
35b99 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61  (const char *pDa
35b9a 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 29 7b 0a  ta, int nData){.
35b9b 20 20 69 6e 74 20 6e 2c 20 69 44 75 6d 6d 79 3b    int n, iDummy;
35b9c 0a 0a 20 20 69 66 28 20 6e 44 61 74 61 3d 3d 30  ..  if( nData==0
35b9d 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73   ) return;.  ass
35b9e 65 72 74 28 20 6e 44 61 74 61 3e 30 20 29 3b 0a  ert( nData>0 );.
35b9f 20 20 61 73 73 65 72 74 28 20 70 44 61 74 61 21    assert( pData!
35ba0 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
35ba1 70 44 61 74 61 2b 6e 44 61 74 61 3e 70 44 61 74  pData+nData>pDat
35ba2 61 20 29 3b 0a 0a 20 20 2f 2a 20 4d 75 73 74 20  a );..  /* Must 
35ba3 6c 65 61 64 20 77 69 74 68 20 61 20 76 61 72 69  lead with a vari
35ba4 6e 74 28 30 29 20 2a 2f 0a 20 20 6e 20 3d 20 66  nt(0) */.  n = f
35ba5 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70  ts3GetVarint32(p
35ba6 44 61 74 61 2c 20 26 69 44 75 6d 6d 79 29 3b 0a  Data, &iDummy);.
35ba7 20 20 61 73 73 65 72 74 28 20 69 44 75 6d 6d 79    assert( iDummy
35ba8 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
35ba9 20 6e 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74   n>0 );.  assert
35baa 28 20 6e 3c 6e 44 61 74 61 20 29 3b 0a 20 20 70  ( n<nData );.  p
35bab 44 61 74 61 20 2b 3d 20 6e 3b 0a 20 20 6e 44 61  Data += n;.  nDa
35bac 74 61 20 2d 3d 20 6e 3b 0a 0a 20 20 2f 2a 20 4c  ta -= n;..  /* L
35bad 65 61 64 69 6e 67 20 74 65 72 6d 20 6c 65 6e 67  eading term leng
35bae 74 68 20 61 6e 64 20 64 61 74 61 20 6d 75 73 74  th and data must
35baf 20 66 69 74 20 69 6e 20 62 75 66 66 65 72 2e 20   fit in buffer. 
35bb0 2a 2f 0a 20 20 6e 20 3d 20 66 74 73 33 47 65 74  */.  n = fts3Get
35bb1 56 61 72 69 6e 74 33 32 28 70 44 61 74 61 2c 20  Varint32(pData, 
35bb2 26 69 44 75 6d 6d 79 29 3b 0a 20 20 61 73 73 65  &iDummy);.  asse
35bb3 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 61 73 73  rt( n>0 );.  ass
35bb4 65 72 74 28 20 69 44 75 6d 6d 79 3e 30 20 29 3b  ert( iDummy>0 );
35bb5 0a 20 20 61 73 73 65 72 74 28 20 6e 2b 69 44 75  .  assert( n+iDu
35bb6 6d 6d 79 3e 30 20 29 3b 0a 20 20 61 73 73 65 72  mmy>0 );.  asser
35bb7 74 28 20 6e 2b 69 44 75 6d 6d 79 3c 6e 44 61 74  t( n+iDummy<nDat
35bb8 61 20 29 3b 0a 20 20 70 44 61 74 61 20 2b 3d 20  a );.  pData += 
35bb9 6e 2b 69 44 75 6d 6d 79 3b 0a 20 20 6e 44 61 74  n+iDummy;.  nDat
35bba 61 20 2d 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 0a  a -= n+iDummy;..
35bbb 20 20 2f 2a 20 4c 65 61 64 69 6e 67 20 74 65 72    /* Leading ter
35bbc 6d 27 73 20 64 6f 63 6c 69 73 74 20 6c 65 6e 67  m's doclist leng
35bbd 74 68 20 61 6e 64 20 64 61 74 61 20 6d 75 73 74  th and data must
35bbe 20 66 69 74 2e 20 2a 2f 0a 20 20 6e 20 3d 20 66   fit. */.  n = f
35bbf 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70  ts3GetVarint32(p
35bc0 44 61 74 61 2c 20 26 69 44 75 6d 6d 79 29 3b 0a  Data, &iDummy);.
35bc1 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29 3b    assert( n>0 );
35bc2 0a 20 20 61 73 73 65 72 74 28 20 69 44 75 6d 6d  .  assert( iDumm
35bc3 79 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  y>0 );.  assert(
35bc4 20 6e 2b 69 44 75 6d 6d 79 3e 30 20 29 3b 0a 20   n+iDummy>0 );. 
35bc5 20 61 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d   assert( n+iDumm
35bc6 79 3c 3d 6e 44 61 74 61 20 29 3b 0a 20 20 41 53  y<=nData );.  AS
35bc7 53 45 52 54 5f 56 41 4c 49 44 5f 44 4f 43 4c 49  SERT_VALID_DOCLI
35bc8 53 54 28 44 4c 5f 44 45 46 41 55 4c 54 2c 20 70  ST(DL_DEFAULT, p
35bc9 44 61 74 61 2b 6e 2c 20 69 44 75 6d 6d 79 2c 20  Data+n, iDummy, 
35bca 4e 55 4c 4c 29 3b 0a 20 20 70 44 61 74 61 20 2b  NULL);.  pData +
35bcb 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 20 20 6e 44  = n+iDummy;.  nD
35bcc 61 74 61 20 2d 3d 20 6e 2b 69 44 75 6d 6d 79 3b  ata -= n+iDummy;
35bcd 0a 0a 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68  ..  /* Verify th
35bce 61 74 20 74 72 61 69 6c 69 6e 67 20 74 65 72 6d  at trailing term
35bcf 73 20 61 6e 64 20 64 6f 63 6c 69 73 74 73 20 61  s and doclists a
35bd0 6c 73 6f 20 61 72 65 20 72 65 61 64 61 62 6c 65  lso are readable
35bd1 2e 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 6e 44  . */.  while( nD
35bd2 61 74 61 21 3d 30 20 29 7b 0a 20 20 20 20 6e 20  ata!=0 ){.    n 
35bd3 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33  = fts3GetVarint3
35bd4 32 28 70 44 61 74 61 2c 20 26 69 44 75 6d 6d 79  2(pData, &iDummy
35bd5 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e  );.    assert( n
35bd6 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  >0 );.    assert
35bd7 28 20 69 44 75 6d 6d 79 3e 3d 30 20 29 3b 0a 20  ( iDummy>=0 );. 
35bd8 20 20 20 61 73 73 65 72 74 28 20 6e 3c 6e 44 61     assert( n<nDa
35bd9 74 61 20 29 3b 0a 20 20 20 20 70 44 61 74 61 20  ta );.    pData 
35bda 2b 3d 20 6e 3b 0a 20 20 20 20 6e 44 61 74 61 20  += n;.    nData 
35bdb 2d 3d 20 6e 3b 0a 20 20 20 20 6e 20 3d 20 66 74  -= n;.    n = ft
35bdc 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 44  s3GetVarint32(pD
35bdd 61 74 61 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20  ata, &iDummy);. 
35bde 20 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29     assert( n>0 )
35bdf 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 44  ;.    assert( iD
35be0 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 20 20 61 73  ummy>0 );.    as
35be1 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3e 30  sert( n+iDummy>0
35be2 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
35be3 6e 2b 69 44 75 6d 6d 79 3c 6e 44 61 74 61 20 29  n+iDummy<nData )
35be4 3b 0a 20 20 20 20 70 44 61 74 61 20 2b 3d 20 6e  ;.    pData += n
35be5 2b 69 44 75 6d 6d 79 3b 0a 20 20 20 20 6e 44 61  +iDummy;.    nDa
35be6 74 61 20 2d 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a  ta -= n+iDummy;.
35be7 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 47 65 74  .    n = fts3Get
35be8 56 61 72 69 6e 74 33 32 28 70 44 61 74 61 2c 20  Varint32(pData, 
35be9 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 61 73  &iDummy);.    as
35bea 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 20  sert( n>0 );.   
35beb 20 61 73 73 65 72 74 28 20 69 44 75 6d 6d 79 3e   assert( iDummy>
35bec 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
35bed 20 6e 2b 69 44 75 6d 6d 79 3e 30 20 29 3b 0a 20   n+iDummy>0 );. 
35bee 20 20 20 61 73 73 65 72 74 28 20 6e 2b 69 44 75     assert( n+iDu
35bef 6d 6d 79 3c 3d 6e 44 61 74 61 20 29 3b 0a 20 20  mmy<=nData );.  
35bf0 20 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 44    ASSERT_VALID_D
35bf1 4f 43 4c 49 53 54 28 44 4c 5f 44 45 46 41 55 4c  OCLIST(DL_DEFAUL
35bf2 54 2c 20 70 44 61 74 61 2b 6e 2c 20 69 44 75 6d  T, pData+n, iDum
35bf3 6d 79 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 70  my, NULL);.    p
35bf4 44 61 74 61 20 2b 3d 20 6e 2b 69 44 75 6d 6d 79  Data += n+iDummy
35bf5 3b 0a 20 20 20 20 6e 44 61 74 61 20 2d 3d 20 6e  ;.    nData -= n
35bf6 2b 69 44 75 6d 6d 79 3b 0a 20 20 7d 0a 7d 0a 23  +iDummy;.  }.}.#
35bf7 64 65 66 69 6e 65 20 41 53 53 45 52 54 5f 56 41  define ASSERT_VA
35bf8 4c 49 44 5f 4c 45 41 46 5f 4e 4f 44 45 28 70 2c  LID_LEAF_NODE(p,
35bf9 20 6e 29 20 6c 65 61 66 4e 6f 64 65 56 61 6c 69   n) leafNodeVali
35bfa 64 61 74 65 28 70 2c 20 6e 29 0a 23 65 6c 73 65  date(p, n).#else
35bfb 0a 23 64 65 66 69 6e 65 20 41 53 53 45 52 54 5f  .#define ASSERT_
35bfc 56 41 4c 49 44 5f 4c 45 41 46 5f 4e 4f 44 45 28  VALID_LEAF_NODE(
35bfd 70 2c 20 6e 29 20 61 73 73 65 72 74 28 20 31 20  p, n) assert( 1 
35bfe 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 46 6c 75  ).#endif../* Flu
35bff 73 68 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c  sh the current l
35c00 65 61 66 20 6e 6f 64 65 20 74 6f 20 25 5f 73 65  eaf node to %_se
35c01 67 6d 65 6e 74 73 2c 20 61 6e 64 20 61 64 64 69  gments, and addi
35c02 6e 67 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67  ng the resulting
35c03 0a 2a 2a 20 62 6c 6f 63 6b 69 64 20 61 6e 64 20  .** blockid and 
35c04 74 68 65 20 73 74 61 72 74 69 6e 67 20 74 65 72  the starting ter
35c05 6d 20 74 6f 20 74 68 65 20 69 6e 74 65 72 69 6f  m to the interio
35c06 72 20 6e 6f 64 65 20 77 68 69 63 68 20 77 69 6c  r node which wil
35c07 6c 0a 2a 2a 20 63 6f 6e 74 61 69 6e 20 69 74 2e  l.** contain it.
35c08 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c  .*/.static int l
35c09 65 61 66 57 72 69 74 65 72 49 6e 74 65 72 6e 61  eafWriterInterna
35c0a 6c 46 6c 75 73 68 28 66 75 6c 6c 74 65 78 74 5f  lFlush(fulltext_
35c0b 76 74 61 62 20 2a 76 2c 20 4c 65 61 66 57 72 69  vtab *v, LeafWri
35c0c 74 65 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20  ter *pWriter,.  
35c0d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35c0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35c0f 20 69 6e 74 20 69 44 61 74 61 2c 20 69 6e 74 20   int iData, int 
35c10 6e 44 61 74 61 29 7b 0a 20 20 73 71 6c 69 74 65  nData){.  sqlite
35c11 5f 69 6e 74 36 34 20 69 42 6c 6f 63 6b 69 64 20  _int64 iBlockid 
35c12 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  = 0;.  const cha
35c13 72 20 2a 70 53 74 61 72 74 69 6e 67 54 65 72 6d  r *pStartingTerm
35c14 3b 0a 20 20 69 6e 74 20 6e 53 74 61 72 74 69 6e  ;.  int nStartin
35c15 67 54 65 72 6d 2c 20 72 63 2c 20 6e 3b 0a 0a 20  gTerm, rc, n;.. 
35c16 20 2f 2a 20 4d 75 73 74 20 68 61 76 65 20 74 68   /* Must have th
35c17 65 20 6c 65 61 64 69 6e 67 20 76 61 72 69 6e 74  e leading varint
35c18 28 30 29 20 66 6c 61 67 2c 20 70 6c 75 73 20 61  (0) flag, plus a
35c19 74 20 6c 65 61 73 74 20 73 6f 6d 65 0a 20 20 2a  t least some.  *
35c1a 2a 20 76 61 6c 69 64 2d 6c 6f 6f 6b 69 6e 67 20  * valid-looking 
35c1b 64 61 74 61 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  data..  */.  ass
35c1c 65 72 74 28 20 6e 44 61 74 61 3e 32 20 29 3b 0a  ert( nData>2 );.
35c1d 20 20 61 73 73 65 72 74 28 20 69 44 61 74 61 3e    assert( iData>
35c1e 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
35c1f 69 44 61 74 61 2b 6e 44 61 74 61 3c 3d 70 57 72  iData+nData<=pWr
35c20 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61  iter->data.nData
35c21 20 29 3b 0a 20 20 41 53 53 45 52 54 5f 56 41 4c   );.  ASSERT_VAL
35c22 49 44 5f 4c 45 41 46 5f 4e 4f 44 45 28 70 57 72  ID_LEAF_NODE(pWr
35c23 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61  iter->data.pData
35c24 2b 69 44 61 74 61 2c 20 6e 44 61 74 61 29 3b 0a  +iData, nData);.
35c25 0a 20 20 72 63 20 3d 20 62 6c 6f 63 6b 5f 69 6e  .  rc = block_in
35c26 73 65 72 74 28 76 2c 20 70 57 72 69 74 65 72 2d  sert(v, pWriter-
35c27 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44 61 74  >data.pData+iDat
35c28 61 2c 20 6e 44 61 74 61 2c 20 26 69 42 6c 6f 63  a, nData, &iBloc
35c29 6b 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  kid);.  if( rc!=
35c2a 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
35c2b 72 6e 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28  rn rc;.  assert(
35c2c 20 69 42 6c 6f 63 6b 69 64 21 3d 30 20 29 3b 0a   iBlockid!=0 );.
35c2d 0a 20 20 2f 2a 20 52 65 63 6f 6e 73 74 72 75 63  .  /* Reconstruc
35c2e 74 20 74 68 65 20 66 69 72 73 74 20 74 65 72 6d  t the first term
35c2f 20 69 6e 20 74 68 65 20 6c 65 61 66 20 66 6f 72   in the leaf for
35c30 20 70 75 72 70 6f 73 65 73 20 6f 66 20 62 75 69   purposes of bui
35c31 6c 64 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 69  lding.  ** the i
35c32 6e 74 65 72 69 6f 72 20 6e 6f 64 65 2e 0a 20 20  nterior node..  
35c33 2a 2f 0a 20 20 6e 20 3d 20 66 74 73 33 47 65 74  */.  n = fts3Get
35c34 56 61 72 69 6e 74 33 32 28 70 57 72 69 74 65 72  Varint32(pWriter
35c35 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44 61  ->data.pData+iDa
35c36 74 61 2b 31 2c 20 26 6e 53 74 61 72 74 69 6e 67  ta+1, &nStarting
35c37 54 65 72 6d 29 3b 0a 20 20 70 53 74 61 72 74 69  Term);.  pStarti
35c38 6e 67 54 65 72 6d 20 3d 20 70 57 72 69 74 65 72  ngTerm = pWriter
35c39 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44 61  ->data.pData+iDa
35c3a 74 61 2b 31 2b 6e 3b 0a 20 20 61 73 73 65 72 74  ta+1+n;.  assert
35c3b 28 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e  ( pWriter->data.
35c3c 6e 44 61 74 61 3e 69 44 61 74 61 2b 31 2b 6e 2b  nData>iData+1+n+
35c3d 6e 53 74 61 72 74 69 6e 67 54 65 72 6d 20 29 3b  nStartingTerm );
35c3e 0a 20 20 61 73 73 65 72 74 28 20 70 57 72 69 74  .  assert( pWrit
35c3f 65 72 2d 3e 6e 54 65 72 6d 44 69 73 74 69 6e 63  er->nTermDistinc
35c40 74 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  t>0 );.  assert(
35c41 20 70 57 72 69 74 65 72 2d 3e 6e 54 65 72 6d 44   pWriter->nTermD
35c42 69 73 74 69 6e 63 74 3c 3d 6e 53 74 61 72 74 69  istinct<=nStarti
35c43 6e 67 54 65 72 6d 20 29 3b 0a 20 20 6e 53 74 61  ngTerm );.  nSta
35c44 72 74 69 6e 67 54 65 72 6d 20 3d 20 70 57 72 69  rtingTerm = pWri
35c45 74 65 72 2d 3e 6e 54 65 72 6d 44 69 73 74 69 6e  ter->nTermDistin
35c46 63 74 3b 0a 0a 20 20 69 66 28 20 70 57 72 69 74  ct;..  if( pWrit
35c47 65 72 2d 3e 68 61 73 5f 70 61 72 65 6e 74 20 29  er->has_parent )
35c48 7b 0a 20 20 20 20 69 6e 74 65 72 69 6f 72 57 72  {.    interiorWr
35c49 69 74 65 72 41 70 70 65 6e 64 28 26 70 57 72 69  iterAppend(&pWri
35c4a 74 65 72 2d 3e 70 61 72 65 6e 74 57 72 69 74 65  ter->parentWrite
35c4b 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r,.             
35c4c 20 20 20 20 20 20 20 20 20 20 20 20 70 53 74 61              pSta
35c4d 72 74 69 6e 67 54 65 72 6d 2c 20 6e 53 74 61 72  rtingTerm, nStar
35c4e 74 69 6e 67 54 65 72 6d 2c 20 69 42 6c 6f 63 6b  tingTerm, iBlock
35c4f 69 64 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  id);.  }else{.  
35c50 20 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72    interiorWriter
35c51 49 6e 69 74 28 31 2c 20 70 53 74 61 72 74 69 6e  Init(1, pStartin
35c52 67 54 65 72 6d 2c 20 6e 53 74 61 72 74 69 6e 67  gTerm, nStarting
35c53 54 65 72 6d 2c 20 69 42 6c 6f 63 6b 69 64 2c 0a  Term, iBlockid,.
35c54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35c55 20 20 20 20 20 20 20 26 70 57 72 69 74 65 72 2d         &pWriter-
35c56 3e 70 61 72 65 6e 74 57 72 69 74 65 72 29 3b 0a  >parentWriter);.
35c57 20 20 20 20 70 57 72 69 74 65 72 2d 3e 68 61 73      pWriter->has
35c58 5f 70 61 72 65 6e 74 20 3d 20 31 3b 0a 20 20 7d  _parent = 1;.  }
35c59 0a 0a 20 20 2f 2a 20 54 72 61 63 6b 20 74 68 65  ..  /* Track the
35c5a 20 73 70 61 6e 20 6f 66 20 74 68 69 73 20 73 65   span of this se
35c5b 67 6d 65 6e 74 27 73 20 6c 65 61 66 20 6e 6f 64  gment's leaf nod
35c5c 65 73 2e 20 2a 2f 0a 20 20 69 66 28 20 70 57 72  es. */.  if( pWr
35c5d 69 74 65 72 2d 3e 69 45 6e 64 42 6c 6f 63 6b 69  iter->iEndBlocki
35c5e 64 3d 3d 30 20 29 7b 0a 20 20 20 20 70 57 72 69  d==0 ){.    pWri
35c5f 74 65 72 2d 3e 69 45 6e 64 42 6c 6f 63 6b 69 64  ter->iEndBlockid
35c60 20 3d 20 70 57 72 69 74 65 72 2d 3e 69 53 74 61   = pWriter->iSta
35c61 72 74 42 6c 6f 63 6b 69 64 20 3d 20 69 42 6c 6f  rtBlockid = iBlo
35c62 63 6b 69 64 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ckid;.  }else{. 
35c63 20 20 20 70 57 72 69 74 65 72 2d 3e 69 45 6e 64     pWriter->iEnd
35c64 42 6c 6f 63 6b 69 64 2b 2b 3b 0a 20 20 20 20 61  Blockid++;.    a
35c65 73 73 65 72 74 28 20 69 42 6c 6f 63 6b 69 64 3d  ssert( iBlockid=
35c66 3d 70 57 72 69 74 65 72 2d 3e 69 45 6e 64 42 6c  =pWriter->iEndBl
35c67 6f 63 6b 69 64 20 29 3b 0a 20 20 7d 0a 0a 20 20  ockid );.  }..  
35c68 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
35c69 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6c  ;.}.static int l
35c6a 65 61 66 57 72 69 74 65 72 46 6c 75 73 68 28 66  eafWriterFlush(f
35c6b 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c  ulltext_vtab *v,
35c6c 20 4c 65 61 66 57 72 69 74 65 72 20 2a 70 57 72   LeafWriter *pWr
35c6d 69 74 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20  iter){.  int rc 
35c6e 3d 20 6c 65 61 66 57 72 69 74 65 72 49 6e 74 65  = leafWriterInte
35c6f 72 6e 61 6c 46 6c 75 73 68 28 76 2c 20 70 57 72  rnalFlush(v, pWr
35c70 69 74 65 72 2c 20 30 2c 20 70 57 72 69 74 65 72  iter, 0, pWriter
35c71 2d 3e 64 61 74 61 2e 6e 44 61 74 61 29 3b 0a 20  ->data.nData);. 
35c72 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
35c73 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
35c74 0a 20 20 2f 2a 20 52 65 2d 69 6e 69 74 69 61 6c  .  /* Re-initial
35c75 69 7a 65 20 74 68 65 20 6f 75 74 70 75 74 20 62  ize the output b
35c76 75 66 66 65 72 2e 20 2a 2f 0a 20 20 64 61 74 61  uffer. */.  data
35c77 42 75 66 66 65 72 52 65 73 65 74 28 26 70 57 72  BufferReset(&pWr
35c78 69 74 65 72 2d 3e 64 61 74 61 29 3b 0a 0a 20 20  iter->data);..  
35c79 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
35c7a 3b 0a 7d 0a 0a 2f 2a 20 46 65 74 63 68 20 74 68  ;.}../* Fetch th
35c7b 65 20 72 6f 6f 74 20 69 6e 66 6f 20 66 6f 72 20  e root info for 
35c7c 74 68 65 20 73 65 67 6d 65 6e 74 2e 20 20 49 66  the segment.  If
35c7d 20 74 68 65 20 65 6e 74 69 72 65 20 6c 65 61 66   the entire leaf
35c7e 20 66 69 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20   fits.** within 
35c7f 52 4f 4f 54 5f 4d 41 58 2c 20 74 68 65 6e 20 69  ROOT_MAX, then i
35c80 74 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e  t will be return
35c81 65 64 20 64 69 72 65 63 74 6c 79 2c 20 6f 74 68  ed directly, oth
35c82 65 72 77 69 73 65 20 69 74 0a 2a 2a 20 77 69 6c  erwise it.** wil
35c83 6c 20 62 65 20 66 6c 75 73 68 65 64 20 61 6e 64  l be flushed and
35c84 20 74 68 65 20 72 6f 6f 74 20 69 6e 66 6f 20 77   the root info w
35c85 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20  ill be returned 
35c86 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 69 6e 74 65  from the.** inte
35c87 72 69 6f 72 20 6e 6f 64 65 2e 20 20 2a 70 69 45  rior node.  *piE
35c88 6e 64 42 6c 6f 63 6b 69 64 20 69 73 20 73 65 74  ndBlockid is set
35c89 20 74 6f 20 74 68 65 20 62 6c 6f 63 6b 69 64 20   to the blockid 
35c8a 6f 66 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20 69  of the last.** i
35c8b 6e 74 65 72 69 6f 72 20 6f 72 20 6c 65 61 66 20  nterior or leaf 
35c8c 6e 6f 64 65 20 77 72 69 74 74 65 6e 20 74 6f 20  node written to 
35c8d 64 69 73 6b 20 28 30 20 69 66 20 6e 6f 6e 65 20  disk (0 if none 
35c8e 61 72 65 20 77 72 69 74 74 65 6e 20 61 74 0a 2a  are written at.*
35c8f 2a 20 61 6c 6c 29 2e 0a 2a 2f 0a 73 74 61 74 69  * all)..*/.stati
35c90 63 20 69 6e 74 20 6c 65 61 66 57 72 69 74 65 72  c int leafWriter
35c91 52 6f 6f 74 49 6e 66 6f 28 66 75 6c 6c 74 65 78  RootInfo(fulltex
35c92 74 5f 76 74 61 62 20 2a 76 2c 20 4c 65 61 66 57  t_vtab *v, LeafW
35c93 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 2c 0a  riter *pWriter,.
35c94 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35c95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 68                ch
35c96 61 72 20 2a 2a 70 70 52 6f 6f 74 49 6e 66 6f 2c  ar **ppRootInfo,
35c97 20 69 6e 74 20 2a 70 6e 52 6f 6f 74 49 6e 66 6f   int *pnRootInfo
35c98 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
35c99 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35c9a 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 69  sqlite_int64 *pi
35c9b 45 6e 64 42 6c 6f 63 6b 69 64 29 7b 0a 20 20 2f  EndBlockid){.  /
35c9c 2a 20 77 65 20 63 61 6e 20 66 69 74 20 74 68 65  * we can fit the
35c9d 20 73 65 67 6d 65 6e 74 20 65 6e 74 69 72 65 6c   segment entirel
35c9e 79 20 69 6e 6c 69 6e 65 20 2a 2f 0a 20 20 69 66  y inline */.  if
35c9f 28 20 21 70 57 72 69 74 65 72 2d 3e 68 61 73 5f  ( !pWriter->has_
35ca0 70 61 72 65 6e 74 20 26 26 20 70 57 72 69 74 65  parent && pWrite
35ca1 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 3c 52 4f  r->data.nData<RO
35ca2 4f 54 5f 4d 41 58 20 29 7b 0a 20 20 20 20 2a 70  OT_MAX ){.    *p
35ca3 70 52 6f 6f 74 49 6e 66 6f 20 3d 20 70 57 72 69  pRootInfo = pWri
35ca4 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61 3b  ter->data.pData;
35ca5 0a 20 20 20 20 2a 70 6e 52 6f 6f 74 49 6e 66 6f  .    *pnRootInfo
35ca6 20 3d 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61   = pWriter->data
35ca7 2e 6e 44 61 74 61 3b 0a 20 20 20 20 2a 70 69 45  .nData;.    *piE
35ca8 6e 64 42 6c 6f 63 6b 69 64 20 3d 20 30 3b 0a 20  ndBlockid = 0;. 
35ca9 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
35caa 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46  _OK;.  }..  /* F
35cab 6c 75 73 68 20 72 65 6d 61 69 6e 69 6e 67 20 6c  lush remaining l
35cac 65 61 66 20 64 61 74 61 2e 20 2a 2f 0a 20 20 69  eaf data. */.  i
35cad 66 28 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61  f( pWriter->data
35cae 2e 6e 44 61 74 61 3e 30 20 29 7b 0a 20 20 20 20  .nData>0 ){.    
35caf 69 6e 74 20 72 63 20 3d 20 6c 65 61 66 57 72 69  int rc = leafWri
35cb0 74 65 72 46 6c 75 73 68 28 76 2c 20 70 57 72 69  terFlush(v, pWri
35cb1 74 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ter);.    if( rc
35cb2 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
35cb3 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
35cb4 2f 2a 20 57 65 20 6d 75 73 74 20 68 61 76 65 20  /* We must have 
35cb5 66 6c 75 73 68 65 64 20 61 20 6c 65 61 66 20 61  flushed a leaf a
35cb6 74 20 73 6f 6d 65 20 70 6f 69 6e 74 2e 20 2a 2f  t some point. */
35cb7 0a 20 20 61 73 73 65 72 74 28 20 70 57 72 69 74  .  assert( pWrit
35cb8 65 72 2d 3e 68 61 73 5f 70 61 72 65 6e 74 20 29  er->has_parent )
35cb9 3b 0a 0a 20 20 2f 2a 20 54 65 6e 61 74 69 76 65  ;..  /* Tenative
35cba 6c 79 20 73 65 74 20 74 68 65 20 65 6e 64 20 6c  ly set the end l
35cbb 65 61 66 20 62 6c 6f 63 6b 69 64 20 61 73 20 74  eaf blockid as t
35cbc 68 65 20 65 6e 64 20 62 6c 6f 63 6b 69 64 2e 20  he end blockid. 
35cbd 20 49 66 20 74 68 65 0a 20 20 2a 2a 20 69 6e 74   If the.  ** int
35cbe 65 72 69 6f 72 20 6e 6f 64 65 20 63 61 6e 20 62  erior node can b
35cbf 65 20 72 65 74 75 72 6e 65 64 20 69 6e 6c 69 6e  e returned inlin
35cc0 65 2c 20 74 68 69 73 20 77 69 6c 6c 20 62 65 20  e, this will be 
35cc1 74 68 65 20 66 69 6e 61 6c 0a 20 20 2a 2a 20 62  the final.  ** b
35cc2 6c 6f 63 6b 69 64 2c 20 6f 74 68 65 72 77 69 73  lockid, otherwis
35cc3 65 20 69 74 20 77 69 6c 6c 20 62 65 20 6f 76 65  e it will be ove
35cc4 72 77 72 69 74 74 65 6e 20 62 79 0a 20 20 2a 2a  rwritten by.  **
35cc5 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72 52   interiorWriterR
35cc6 6f 6f 74 49 6e 66 6f 28 29 2e 0a 20 20 2a 2f 0a  ootInfo()..  */.
35cc7 20 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64 20    *piEndBlockid 
35cc8 3d 20 70 57 72 69 74 65 72 2d 3e 69 45 6e 64 42  = pWriter->iEndB
35cc9 6c 6f 63 6b 69 64 3b 0a 0a 20 20 72 65 74 75 72  lockid;..  retur
35cca 6e 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72  n interiorWriter
35ccb 52 6f 6f 74 49 6e 66 6f 28 76 2c 20 26 70 57 72  RootInfo(v, &pWr
35ccc 69 74 65 72 2d 3e 70 61 72 65 6e 74 57 72 69 74  iter->parentWrit
35ccd 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  er,.            
35cce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35ccf 20 20 20 20 70 70 52 6f 6f 74 49 6e 66 6f 2c 20      ppRootInfo, 
35cd0 70 6e 52 6f 6f 74 49 6e 66 6f 2c 20 70 69 45 6e  pnRootInfo, piEn
35cd1 64 42 6c 6f 63 6b 69 64 29 3b 0a 7d 0a 0a 2f 2a  dBlockid);.}../*
35cd2 20 43 6f 6c 6c 65 63 74 20 74 68 65 20 72 6f 6f   Collect the roo
35cd3 74 49 6e 66 6f 20 64 61 74 61 20 61 6e 64 20 73  tInfo data and s
35cd4 74 6f 72 65 20 69 74 20 69 6e 74 6f 20 74 68 65  tore it into the
35cd5 20 73 65 67 6d 65 6e 74 20 64 69 72 65 63 74 6f   segment directo
35cd6 72 79 2e 0a 2a 2a 20 54 68 69 73 20 68 61 73 20  ry..** This has 
35cd7 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 66 6c  the effect of fl
35cd8 75 73 68 69 6e 67 20 74 68 65 20 73 65 67 6d 65  ushing the segme
35cd9 6e 74 27 73 20 6c 65 61 66 20 64 61 74 61 20 74  nt's leaf data t
35cda 6f 0a 2a 2a 20 25 5f 73 65 67 6d 65 6e 74 73 2c  o.** %_segments,
35cdb 20 61 6e 64 20 61 6c 73 6f 20 66 6c 75 73 68 69   and also flushi
35cdc 6e 67 20 61 6e 79 20 69 6e 74 65 72 69 6f 72 20  ng any interior 
35cdd 6e 6f 64 65 73 20 74 6f 20 25 5f 73 65 67 6d 65  nodes to %_segme
35cde 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  nts..*/.static i
35cdf 6e 74 20 6c 65 61 66 57 72 69 74 65 72 46 69 6e  nt leafWriterFin
35ce0 61 6c 69 7a 65 28 66 75 6c 6c 74 65 78 74 5f 76  alize(fulltext_v
35ce1 74 61 62 20 2a 76 2c 20 4c 65 61 66 57 72 69 74  tab *v, LeafWrit
35ce2 65 72 20 2a 70 57 72 69 74 65 72 29 7b 0a 20 20  er *pWriter){.  
35ce3 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 45 6e  sqlite_int64 iEn
35ce4 64 42 6c 6f 63 6b 69 64 3b 0a 20 20 63 68 61 72  dBlockid;.  char
35ce5 20 2a 70 52 6f 6f 74 49 6e 66 6f 3b 0a 20 20 69   *pRootInfo;.  i
35ce6 6e 74 20 72 63 2c 20 6e 52 6f 6f 74 49 6e 66 6f  nt rc, nRootInfo
35ce7 3b 0a 0a 20 20 72 63 20 3d 20 6c 65 61 66 57 72  ;..  rc = leafWr
35ce8 69 74 65 72 52 6f 6f 74 49 6e 66 6f 28 76 2c 20  iterRootInfo(v, 
35ce9 70 57 72 69 74 65 72 2c 20 26 70 52 6f 6f 74 49  pWriter, &pRootI
35cea 6e 66 6f 2c 20 26 6e 52 6f 6f 74 49 6e 66 6f 2c  nfo, &nRootInfo,
35ceb 20 26 69 45 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a   &iEndBlockid);.
35cec 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
35ced 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
35cee 0a 0a 20 20 2f 2a 20 44 6f 6e 27 74 20 62 6f 74  ..  /* Don't bot
35cef 68 65 72 20 73 74 6f 72 69 6e 67 20 61 6e 20 65  her storing an e
35cf0 6e 74 69 72 65 6c 79 20 65 6d 70 74 79 20 73 65  ntirely empty se
35cf1 67 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 66 28 20  gment. */.  if( 
35cf2 69 45 6e 64 42 6c 6f 63 6b 69 64 3d 3d 30 20 26  iEndBlockid==0 &
35cf3 26 20 6e 52 6f 6f 74 49 6e 66 6f 3d 3d 30 20 29  & nRootInfo==0 )
35cf4 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
35cf5 4b 3b 0a 0a 20 20 72 65 74 75 72 6e 20 73 65 67  K;..  return seg
35cf6 64 69 72 5f 73 65 74 28 76 2c 20 70 57 72 69 74  dir_set(v, pWrit
35cf7 65 72 2d 3e 69 4c 65 76 65 6c 2c 20 70 57 72 69  er->iLevel, pWri
35cf8 74 65 72 2d 3e 69 64 78 2c 0a 20 20 20 20 20 20  ter->idx,.      
35cf9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 57                pW
35cfa 72 69 74 65 72 2d 3e 69 53 74 61 72 74 42 6c 6f  riter->iStartBlo
35cfb 63 6b 69 64 2c 20 70 57 72 69 74 65 72 2d 3e 69  ckid, pWriter->i
35cfc 45 6e 64 42 6c 6f 63 6b 69 64 2c 0a 20 20 20 20  EndBlockid,.    
35cfd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35cfe 69 45 6e 64 42 6c 6f 63 6b 69 64 2c 20 70 52 6f  iEndBlockid, pRo
35cff 6f 74 49 6e 66 6f 2c 20 6e 52 6f 6f 74 49 6e 66  otInfo, nRootInf
35d00 6f 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f  o);.}..static vo
35d01 69 64 20 6c 65 61 66 57 72 69 74 65 72 44 65 73  id leafWriterDes
35d02 74 72 6f 79 28 4c 65 61 66 57 72 69 74 65 72 20  troy(LeafWriter 
35d03 2a 70 57 72 69 74 65 72 29 7b 0a 20 20 69 66 28  *pWriter){.  if(
35d04 20 70 57 72 69 74 65 72 2d 3e 68 61 73 5f 70 61   pWriter->has_pa
35d05 72 65 6e 74 20 29 20 69 6e 74 65 72 69 6f 72 57  rent ) interiorW
35d06 72 69 74 65 72 44 65 73 74 72 6f 79 28 26 70 57  riterDestroy(&pW
35d07 72 69 74 65 72 2d 3e 70 61 72 65 6e 74 57 72 69  riter->parentWri
35d08 74 65 72 29 3b 0a 20 20 64 61 74 61 42 75 66 66  ter);.  dataBuff
35d09 65 72 44 65 73 74 72 6f 79 28 26 70 57 72 69 74  erDestroy(&pWrit
35d0a 65 72 2d 3e 74 65 72 6d 29 3b 0a 20 20 64 61 74  er->term);.  dat
35d0b 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26  aBufferDestroy(&
35d0c 70 57 72 69 74 65 72 2d 3e 64 61 74 61 29 3b 0a  pWriter->data);.
35d0d 7d 0a 0a 2f 2a 20 45 6e 63 6f 64 65 20 61 20 74  }../* Encode a t
35d0e 65 72 6d 20 69 6e 74 6f 20 74 68 65 20 6c 65 61  erm into the lea
35d0f 66 57 72 69 74 65 72 2c 20 64 65 6c 74 61 2d 65  fWriter, delta-e
35d10 6e 63 6f 64 69 6e 67 20 61 73 20 61 70 70 72 6f  ncoding as appro
35d11 70 72 69 61 74 65 2e 0a 2a 2a 20 52 65 74 75 72  priate..** Retur
35d12 6e 73 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66  ns the length of
35d13 20 74 68 65 20 6e 65 77 20 74 65 72 6d 20 77 68   the new term wh
35d14 69 63 68 20 64 69 73 74 69 6e 67 75 69 73 68 65  ich distinguishe
35d15 73 20 69 74 20 66 72 6f 6d 20 74 68 65 0a 2a 2a  s it from the.**
35d16 20 70 72 65 76 69 6f 75 73 20 74 65 72 6d 2c 20   previous term, 
35d17 77 68 69 63 68 20 63 61 6e 20 62 65 20 75 73 65  which can be use
35d18 64 20 74 6f 20 73 65 74 20 6e 54 65 72 6d 44 69  d to set nTermDi
35d19 73 74 69 6e 63 74 20 77 68 65 6e 20 61 20 6e 6f  stinct when a no
35d1a 64 65 0a 2a 2a 20 62 6f 75 6e 64 61 72 79 20 69  de.** boundary i
35d1b 73 20 63 72 6f 73 73 65 64 2e 0a 2a 2f 0a 73 74  s crossed..*/.st
35d1c 61 74 69 63 20 69 6e 74 20 6c 65 61 66 57 72 69  atic int leafWri
35d1d 74 65 72 45 6e 63 6f 64 65 54 65 72 6d 28 4c 65  terEncodeTerm(Le
35d1e 61 66 57 72 69 74 65 72 20 2a 70 57 72 69 74 65  afWriter *pWrite
35d1f 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r,.             
35d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35d21 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70     const char *p
35d22 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 29  Term, int nTerm)
35d23 7b 0a 20 20 63 68 61 72 20 63 5b 56 41 52 49 4e  {.  char c[VARIN
35d24 54 5f 4d 41 58 2b 56 41 52 49 4e 54 5f 4d 41 58  T_MAX+VARINT_MAX
35d25 5d 3b 0a 20 20 69 6e 74 20 6e 2c 20 6e 50 72 65  ];.  int n, nPre
35d26 66 69 78 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65  fix = 0;..  asse
35d27 72 74 28 20 6e 54 65 72 6d 3e 30 20 29 3b 0a 20  rt( nTerm>0 );. 
35d28 20 77 68 69 6c 65 28 20 6e 50 72 65 66 69 78 3c   while( nPrefix<
35d29 70 57 72 69 74 65 72 2d 3e 74 65 72 6d 2e 6e 44  pWriter->term.nD
35d2a 61 74 61 20 26 26 0a 20 20 20 20 20 20 20 20 20  ata &&.         
35d2b 70 54 65 72 6d 5b 6e 50 72 65 66 69 78 5d 3d 3d  pTerm[nPrefix]==
35d2c 70 57 72 69 74 65 72 2d 3e 74 65 72 6d 2e 70 44  pWriter->term.pD
35d2d 61 74 61 5b 6e 50 72 65 66 69 78 5d 20 29 7b 0a  ata[nPrefix] ){.
35d2e 20 20 20 20 6e 50 72 65 66 69 78 2b 2b 3b 0a 20      nPrefix++;. 
35d2f 20 20 20 2f 2a 20 46 61 69 6c 69 6e 67 20 74 68     /* Failing th
35d30 69 73 20 69 6d 70 6c 69 65 73 20 74 68 61 74 20  is implies that 
35d31 74 68 65 20 74 65 72 6d 73 20 77 65 72 65 6e 27  the terms weren'
35d32 74 20 69 6e 20 6f 72 64 65 72 2e 20 2a 2f 0a 20  t in order. */. 
35d33 20 20 20 61 73 73 65 72 74 28 20 6e 50 72 65 66     assert( nPref
35d34 69 78 3c 6e 54 65 72 6d 20 29 3b 0a 20 20 7d 0a  ix<nTerm );.  }.
35d35 0a 20 20 69 66 28 20 70 57 72 69 74 65 72 2d 3e  .  if( pWriter->
35d36 64 61 74 61 2e 6e 44 61 74 61 3d 3d 30 20 29 7b  data.nData==0 ){
35d37 0a 20 20 20 20 2f 2a 20 45 6e 63 6f 64 65 20 74  .    /* Encode t
35d38 68 65 20 6e 6f 64 65 20 68 65 61 64 65 72 20 61  he node header a
35d39 6e 64 20 6c 65 61 64 69 6e 67 20 74 65 72 6d 20  nd leading term 
35d3a 61 73 3a 0a 20 20 20 20 2a 2a 20 20 76 61 72 69  as:.    **  vari
35d3b 6e 74 28 30 29 0a 20 20 20 20 2a 2a 20 20 76 61  nt(0).    **  va
35d3c 72 69 6e 74 28 6e 54 65 72 6d 29 0a 20 20 20 20  rint(nTerm).    
35d3d 2a 2a 20 20 63 68 61 72 20 70 54 65 72 6d 5b 6e  **  char pTerm[n
35d3e 54 65 72 6d 5d 0a 20 20 20 20 2a 2f 0a 20 20 20  Term].    */.   
35d3f 20 6e 20 3d 20 66 74 73 33 50 75 74 56 61 72 69   n = fts3PutVari
35d40 6e 74 28 63 2c 20 27 5c 30 27 29 3b 0a 20 20 20  nt(c, '\0');.   
35d41 20 6e 20 2b 3d 20 66 74 73 33 50 75 74 56 61 72   n += fts3PutVar
35d42 69 6e 74 28 63 2b 6e 2c 20 6e 54 65 72 6d 29 3b  int(c+n, nTerm);
35d43 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72 41  .    dataBufferA
35d44 70 70 65 6e 64 32 28 26 70 57 72 69 74 65 72 2d  ppend2(&pWriter-
35d45 3e 64 61 74 61 2c 20 63 2c 20 6e 2c 20 70 54 65  >data, c, n, pTe
35d46 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 7d 65  rm, nTerm);.  }e
35d47 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 44 65 6c 74  lse{.    /* Delt
35d48 61 2d 65 6e 63 6f 64 65 20 74 68 65 20 74 65 72  a-encode the ter
35d49 6d 20 61 73 3a 0a 20 20 20 20 2a 2a 20 20 76 61  m as:.    **  va
35d4a 72 69 6e 74 28 6e 50 72 65 66 69 78 29 0a 20 20  rint(nPrefix).  
35d4b 20 20 2a 2a 20 20 76 61 72 69 6e 74 28 6e 53 75    **  varint(nSu
35d4c 66 66 69 78 29 0a 20 20 20 20 2a 2a 20 20 63 68  ffix).    **  ch
35d4d 61 72 20 70 54 65 72 6d 53 75 66 66 69 78 5b 6e  ar pTermSuffix[n
35d4e 53 75 66 66 69 78 5d 0a 20 20 20 20 2a 2f 0a 20  Suffix].    */. 
35d4f 20 20 20 6e 20 3d 20 66 74 73 33 50 75 74 56 61     n = fts3PutVa
35d50 72 69 6e 74 28 63 2c 20 6e 50 72 65 66 69 78 29  rint(c, nPrefix)
35d51 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 50  ;.    n += fts3P
35d52 75 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20 6e 54  utVarint(c+n, nT
35d53 65 72 6d 2d 6e 50 72 65 66 69 78 29 3b 0a 20 20  erm-nPrefix);.  
35d54 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65    dataBufferAppe
35d55 6e 64 32 28 26 70 57 72 69 74 65 72 2d 3e 64 61  nd2(&pWriter->da
35d56 74 61 2c 20 63 2c 20 6e 2c 20 70 54 65 72 6d 2b  ta, c, n, pTerm+
35d57 6e 50 72 65 66 69 78 2c 20 6e 54 65 72 6d 2d 6e  nPrefix, nTerm-n
35d58 50 72 65 66 69 78 29 3b 0a 20 20 7d 0a 20 20 64  Prefix);.  }.  d
35d59 61 74 61 42 75 66 66 65 72 52 65 70 6c 61 63 65  ataBufferReplace
35d5a 28 26 70 57 72 69 74 65 72 2d 3e 74 65 72 6d 2c  (&pWriter->term,
35d5b 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a   pTerm, nTerm);.
35d5c 0a 20 20 72 65 74 75 72 6e 20 6e 50 72 65 66 69  .  return nPrefi
35d5d 78 2b 31 3b 0a 7d 0a 0a 2f 2a 20 55 73 65 64 20  x+1;.}../* Used 
35d5e 74 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d 6d 6f  to avoid a memmo
35d5f 76 65 20 77 68 65 6e 20 61 20 6c 61 72 67 65 20  ve when a large 
35d60 61 6d 6f 75 6e 74 20 6f 66 20 64 6f 63 6c 69 73  amount of doclis
35d61 74 20 64 61 74 61 20 69 73 20 69 6e 0a 2a 2a 20  t data is in.** 
35d62 74 68 65 20 62 75 66 66 65 72 2e 20 20 54 68 69  the buffer.  Thi
35d63 73 20 63 6f 6e 73 74 72 75 63 74 73 20 61 20 6e  s constructs a n
35d64 6f 64 65 20 61 6e 64 20 74 65 72 6d 20 68 65 61  ode and term hea
35d65 64 65 72 20 62 65 66 6f 72 65 0a 2a 2a 20 69 44  der before.** iD
35d66 6f 63 6c 69 73 74 44 61 74 61 20 61 6e 64 20 66  oclistData and f
35d67 6c 75 73 68 65 73 20 74 68 65 20 72 65 73 75 6c  lushes the resul
35d68 74 69 6e 67 20 63 6f 6d 70 6c 65 74 65 20 6e 6f  ting complete no
35d69 64 65 20 75 73 69 6e 67 0a 2a 2a 20 6c 65 61 66  de using.** leaf
35d6a 57 72 69 74 65 72 49 6e 74 65 72 6e 61 6c 46 6c  WriterInternalFl
35d6b 75 73 68 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  ush()..*/.static
35d6c 20 69 6e 74 20 6c 65 61 66 57 72 69 74 65 72 49   int leafWriterI
35d6d 6e 6c 69 6e 65 46 6c 75 73 68 28 66 75 6c 6c 74  nlineFlush(fullt
35d6e 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 4c 65 61  ext_vtab *v, Lea
35d6f 66 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72  fWriter *pWriter
35d70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
35d71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35d72 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70     const char *p
35d73 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c  Term, int nTerm,
35d74 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
35d75 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35d76 20 20 69 6e 74 20 69 44 6f 63 6c 69 73 74 44 61    int iDoclistDa
35d77 74 61 29 7b 0a 20 20 63 68 61 72 20 63 5b 56 41  ta){.  char c[VA
35d78 52 49 4e 54 5f 4d 41 58 2b 56 41 52 49 4e 54 5f  RINT_MAX+VARINT_
35d79 4d 41 58 5d 3b 0a 20 20 69 6e 74 20 69 44 61 74  MAX];.  int iDat
35d7a 61 2c 20 6e 20 3d 20 66 74 73 33 50 75 74 56 61  a, n = fts3PutVa
35d7b 72 69 6e 74 28 63 2c 20 30 29 3b 0a 20 20 6e 20  rint(c, 0);.  n 
35d7c 2b 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74  += fts3PutVarint
35d7d 28 63 2b 6e 2c 20 6e 54 65 72 6d 29 3b 0a 0a 20  (c+n, nTerm);.. 
35d7e 20 2f 2a 20 54 68 65 72 65 20 73 68 6f 75 6c 64   /* There should
35d7f 20 61 6c 77 61 79 73 20 62 65 20 72 6f 6f 6d 20   always be room 
35d80 66 6f 72 20 74 68 65 20 68 65 61 64 65 72 2e 20  for the header. 
35d81 20 45 76 65 6e 20 69 66 20 70 54 65 72 6d 20 73   Even if pTerm s
35d82 68 61 72 65 64 0a 20 20 2a 2a 20 61 20 73 75 62  hared.  ** a sub
35d83 73 74 61 6e 74 69 61 6c 20 70 72 65 66 69 78 20  stantial prefix 
35d84 77 69 74 68 20 74 68 65 20 70 72 65 76 69 6f 75  with the previou
35d85 73 20 74 65 72 6d 2c 20 74 68 65 20 65 6e 74 69  s term, the enti
35d86 72 65 20 70 72 65 66 69 78 0a 20 20 2a 2a 20 63  re prefix.  ** c
35d87 6f 75 6c 64 20 62 65 20 63 6f 6e 73 74 72 75 63  ould be construc
35d88 74 65 64 20 66 72 6f 6d 20 65 61 72 6c 69 65 72  ted from earlier
35d89 20 64 61 74 61 20 69 6e 20 74 68 65 20 64 6f 63   data in the doc
35d8a 6c 69 73 74 2c 20 73 6f 20 74 68 65 72 65 0a 20  list, so there. 
35d8b 20 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 72 6f   ** should be ro
35d8c 6f 6d 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  om..  */.  asser
35d8d 74 28 20 69 44 6f 63 6c 69 73 74 44 61 74 61 3e  t( iDoclistData>
35d8e 3d 6e 2b 6e 54 65 72 6d 20 29 3b 0a 0a 20 20 69  =n+nTerm );..  i
35d8f 44 61 74 61 20 3d 20 69 44 6f 63 6c 69 73 74 44  Data = iDoclistD
35d90 61 74 61 2d 28 6e 2b 6e 54 65 72 6d 29 3b 0a 20  ata-(n+nTerm);. 
35d91 20 6d 65 6d 63 70 79 28 70 57 72 69 74 65 72 2d   memcpy(pWriter-
35d92 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44 61 74  >data.pData+iDat
35d93 61 2c 20 63 2c 20 6e 29 3b 0a 20 20 6d 65 6d 63  a, c, n);.  memc
35d94 70 79 28 70 57 72 69 74 65 72 2d 3e 64 61 74 61  py(pWriter->data
35d95 2e 70 44 61 74 61 2b 69 44 61 74 61 2b 6e 2c 20  .pData+iData+n, 
35d96 70 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 0a  pTerm, nTerm);..
35d97 20 20 72 65 74 75 72 6e 20 6c 65 61 66 57 72 69    return leafWri
35d98 74 65 72 49 6e 74 65 72 6e 61 6c 46 6c 75 73 68  terInternalFlush
35d99 28 76 2c 20 70 57 72 69 74 65 72 2c 20 69 44 61  (v, pWriter, iDa
35d9a 74 61 2c 20 70 57 72 69 74 65 72 2d 3e 64 61 74  ta, pWriter->dat
35d9b 61 2e 6e 44 61 74 61 2d 69 44 61 74 61 29 3b 0a  a.nData-iData);.
35d9c 7d 0a 0a 2f 2a 20 50 75 73 68 20 70 54 65 72 6d  }../* Push pTerm
35d9d 5b 6e 54 65 72 6d 5d 20 61 6c 6f 6e 67 20 77 69  [nTerm] along wi
35d9e 74 68 20 74 68 65 20 64 6f 63 6c 69 73 74 20 64  th the doclist d
35d9f 61 74 61 20 74 6f 20 74 68 65 20 6c 65 61 66 20  ata to the leaf 
35da0 6c 61 79 65 72 20 6f 66 0a 2a 2a 20 25 5f 73 65  layer of.** %_se
35da1 67 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69  gments..*/.stati
35da2 63 20 69 6e 74 20 6c 65 61 66 57 72 69 74 65 72  c int leafWriter
35da3 53 74 65 70 4d 65 72 67 65 28 66 75 6c 6c 74 65  StepMerge(fullte
35da4 78 74 5f 76 74 61 62 20 2a 76 2c 20 4c 65 61 66  xt_vtab *v, Leaf
35da5 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 2c  Writer *pWriter,
35da6 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
35da7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35da8 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72  const char *pTer
35da9 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 0a 20 20  m, int nTerm,.  
35daa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35dab 20 20 20 20 20 20 20 20 20 20 20 20 20 44 4c 52               DLR
35dac 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 73 2c  eader *pReaders,
35dad 20 69 6e 74 20 6e 52 65 61 64 65 72 73 29 7b 0a   int nReaders){.
35dae 20 20 63 68 61 72 20 63 5b 56 41 52 49 4e 54 5f    char c[VARINT_
35daf 4d 41 58 2b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b  MAX+VARINT_MAX];
35db0 0a 20 20 69 6e 74 20 69 54 65 72 6d 44 61 74 61  .  int iTermData
35db1 20 3d 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61   = pWriter->data
35db2 2e 6e 44 61 74 61 2c 20 69 44 6f 63 6c 69 73 74  .nData, iDoclist
35db3 44 61 74 61 3b 0a 20 20 69 6e 74 20 69 2c 20 6e  Data;.  int i, n
35db4 44 61 74 61 2c 20 6e 2c 20 6e 41 63 74 75 61 6c  Data, n, nActual
35db5 44 61 74 61 2c 20 6e 41 63 74 75 61 6c 2c 20 72  Data, nActual, r
35db6 63 2c 20 6e 54 65 72 6d 44 69 73 74 69 6e 63 74  c, nTermDistinct
35db7 3b 0a 0a 20 20 41 53 53 45 52 54 5f 56 41 4c 49  ;..  ASSERT_VALI
35db8 44 5f 4c 45 41 46 5f 4e 4f 44 45 28 70 57 72 69  D_LEAF_NODE(pWri
35db9 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61 2c  ter->data.pData,
35dba 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e   pWriter->data.n
35dbb 44 61 74 61 29 3b 0a 20 20 6e 54 65 72 6d 44 69  Data);.  nTermDi
35dbc 73 74 69 6e 63 74 20 3d 20 6c 65 61 66 57 72 69  stinct = leafWri
35dbd 74 65 72 45 6e 63 6f 64 65 54 65 72 6d 28 70 57  terEncodeTerm(pW
35dbe 72 69 74 65 72 2c 20 70 54 65 72 6d 2c 20 6e 54  riter, pTerm, nT
35dbf 65 72 6d 29 3b 0a 0a 20 20 2f 2a 20 52 65 6d 65  erm);..  /* Reme
35dc0 6d 62 65 72 20 6e 54 65 72 6d 44 69 73 74 69 6e  mber nTermDistin
35dc1 63 74 20 69 66 20 6f 70 65 6e 69 6e 67 20 61 20  ct if opening a 
35dc2 6e 65 77 20 6e 6f 64 65 2e 20 2a 2f 0a 20 20 69  new node. */.  i
35dc3 66 28 20 69 54 65 72 6d 44 61 74 61 3d 3d 30 20  f( iTermData==0 
35dc4 29 20 70 57 72 69 74 65 72 2d 3e 6e 54 65 72 6d  ) pWriter->nTerm
35dc5 44 69 73 74 69 6e 63 74 20 3d 20 6e 54 65 72 6d  Distinct = nTerm
35dc6 44 69 73 74 69 6e 63 74 3b 0a 0a 20 20 69 44 6f  Distinct;..  iDo
35dc7 63 6c 69 73 74 44 61 74 61 20 3d 20 70 57 72 69  clistData = pWri
35dc8 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 3b  ter->data.nData;
35dc9 0a 0a 20 20 2f 2a 20 45 73 74 69 6d 61 74 65 20  ..  /* Estimate 
35dca 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68  the length of th
35dcb 65 20 6d 65 72 67 65 64 20 64 6f 63 6c 69 73 74  e merged doclist
35dcc 20 73 6f 20 77 65 20 63 61 6e 20 6c 65 61 76 65   so we can leave
35dcd 20 73 70 61 63 65 0a 20 20 2a 2a 20 74 6f 20 65   space.  ** to e
35dce 6e 63 6f 64 65 20 69 74 2e 0a 20 20 2a 2f 0a 20  ncode it..  */. 
35dcf 20 66 6f 72 28 69 3d 30 2c 20 6e 44 61 74 61 3d   for(i=0, nData=
35dd0 30 3b 20 69 3c 6e 52 65 61 64 65 72 73 3b 20 69  0; i<nReaders; i
35dd1 2b 2b 29 7b 0a 20 20 20 20 6e 44 61 74 61 20 2b  ++){.    nData +
35dd2 3d 20 64 6c 72 41 6c 6c 44 61 74 61 42 79 74 65  = dlrAllDataByte
35dd3 73 28 26 70 52 65 61 64 65 72 73 5b 69 5d 29 3b  s(&pReaders[i]);
35dd4 0a 20 20 7d 0a 20 20 6e 20 3d 20 66 74 73 33 50  .  }.  n = fts3P
35dd5 75 74 56 61 72 69 6e 74 28 63 2c 20 6e 44 61 74  utVarint(c, nDat
35dd6 61 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72  a);.  dataBuffer
35dd7 41 70 70 65 6e 64 28 26 70 57 72 69 74 65 72 2d  Append(&pWriter-
35dd8 3e 64 61 74 61 2c 20 63 2c 20 6e 29 3b 0a 0a 20  >data, c, n);.. 
35dd9 20 64 6f 63 4c 69 73 74 4d 65 72 67 65 28 26 70   docListMerge(&p
35dda 57 72 69 74 65 72 2d 3e 64 61 74 61 2c 20 70 52  Writer->data, pR
35ddb 65 61 64 65 72 73 2c 20 6e 52 65 61 64 65 72 73  eaders, nReaders
35ddc 29 3b 0a 20 20 41 53 53 45 52 54 5f 56 41 4c 49  );.  ASSERT_VALI
35ddd 44 5f 44 4f 43 4c 49 53 54 28 44 4c 5f 44 45 46  D_DOCLIST(DL_DEF
35dde 41 55 4c 54 2c 0a 20 20 20 20 20 20 20 20 20 20  AULT,.          
35ddf 20 20 20 20 20 20 20 20 20 20 20 20 20 70 57 72               pWr
35de0 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61  iter->data.pData
35de1 2b 69 44 6f 63 6c 69 73 74 44 61 74 61 2b 6e 2c  +iDoclistData+n,
35de2 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
35de3 20 20 20 20 20 20 20 20 70 57 72 69 74 65 72 2d          pWriter-
35de4 3e 64 61 74 61 2e 6e 44 61 74 61 2d 69 44 6f 63  >data.nData-iDoc
35de5 6c 69 73 74 44 61 74 61 2d 6e 2c 20 4e 55 4c 4c  listData-n, NULL
35de6 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 61 63 74  );..  /* The act
35de7 75 61 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 64 6f  ual amount of do
35de8 63 6c 69 73 74 20 64 61 74 61 20 61 74 20 74 68  clist data at th
35de9 69 73 20 70 6f 69 6e 74 20 63 6f 75 6c 64 20 62  is point could b
35dea 65 20 73 6d 61 6c 6c 65 72 0a 20 20 2a 2a 20 74  e smaller.  ** t
35deb 68 61 6e 20 74 68 65 20 6c 65 6e 67 74 68 20 77  han the length w
35dec 65 20 65 6e 63 6f 64 65 64 2e 20 20 41 64 64 69  e encoded.  Addi
35ded 74 69 6f 6e 61 6c 6c 79 2c 20 74 68 65 20 73 70  tionally, the sp
35dee 61 63 65 20 72 65 71 75 69 72 65 64 20 74 6f 0a  ace required to.
35def 20 20 2a 2a 20 65 6e 63 6f 64 65 20 74 68 69 73    ** encode this
35df0 20 6c 65 6e 67 74 68 20 63 6f 75 6c 64 20 62 65   length could be
35df1 20 73 6d 61 6c 6c 65 72 2e 20 20 46 6f 72 20 73   smaller.  For s
35df2 6d 61 6c 6c 20 64 6f 63 6c 69 73 74 73 2c 20 74  mall doclists, t
35df3 68 69 73 20 69 73 0a 20 20 2a 2a 20 6e 6f 74 20  his is.  ** not 
35df4 61 20 62 69 67 20 64 65 61 6c 2c 20 77 65 20 63  a big deal, we c
35df5 61 6e 20 6a 75 73 74 20 75 73 65 20 6d 65 6d 6d  an just use memm
35df6 6f 76 65 28 29 20 74 6f 20 61 64 6a 75 73 74 20  ove() to adjust 
35df7 74 68 69 6e 67 73 2e 0a 20 20 2a 2f 0a 20 20 6e  things..  */.  n
35df8 41 63 74 75 61 6c 44 61 74 61 20 3d 20 70 57 72  ActualData = pWr
35df9 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61  iter->data.nData
35dfa 2d 28 69 44 6f 63 6c 69 73 74 44 61 74 61 2b 6e  -(iDoclistData+n
35dfb 29 3b 0a 20 20 6e 41 63 74 75 61 6c 20 3d 20 66  );.  nActual = f
35dfc 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2c 20  ts3PutVarint(c, 
35dfd 6e 41 63 74 75 61 6c 44 61 74 61 29 3b 0a 20 20  nActualData);.  
35dfe 61 73 73 65 72 74 28 20 6e 41 63 74 75 61 6c 44  assert( nActualD
35dff 61 74 61 3c 3d 6e 44 61 74 61 20 29 3b 0a 20 20  ata<=nData );.  
35e00 61 73 73 65 72 74 28 20 6e 41 63 74 75 61 6c 3c  assert( nActual<
35e01 3d 6e 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  =n );..  /* If t
35e02 68 65 20 6e 65 77 20 64 6f 63 6c 69 73 74 20 69  he new doclist i
35e03 73 20 62 69 67 20 65 6e 6f 75 67 68 20 66 6f 72  s big enough for
35e04 20 66 6f 72 63 65 20 61 20 73 74 61 6e 64 61 6c   force a standal
35e05 6f 6e 65 20 6c 65 61 66 0a 20 20 2a 2a 20 6e 6f  one leaf.  ** no
35e06 64 65 2c 20 77 65 20 63 61 6e 20 69 6d 6d 65 64  de, we can immed
35e07 69 61 74 65 6c 79 20 66 6c 75 73 68 20 69 74 20  iately flush it 
35e08 69 6e 6c 69 6e 65 20 77 69 74 68 6f 75 74 20 64  inline without d
35e09 6f 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 6d 65  oing the.  ** me
35e0a 6d 6d 6f 76 65 28 29 2e 0a 20 20 2a 2f 0a 20 20  mmove()..  */.  
35e0b 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 54  /* TODO(shess) T
35e0c 68 69 73 20 74 65 73 74 20 6d 61 74 63 68 65 73  his test matches
35e0d 20 6c 65 61 66 57 72 69 74 65 72 53 74 65 70 28   leafWriterStep(
35e0e 29 2c 20 77 68 69 63 68 20 64 6f 65 73 20 74 68  ), which does th
35e0f 69 73 0a 20 20 2a 2a 20 74 65 73 74 20 62 65 66  is.  ** test bef
35e10 6f 72 65 20 69 74 20 6b 6e 6f 77 73 20 74 68 65  ore it knows the
35e11 20 63 6f 73 74 20 74 6f 20 76 61 72 69 6e 74 2d   cost to varint-
35e12 65 6e 63 6f 64 65 20 74 68 65 20 74 65 72 6d 20  encode the term 
35e13 61 6e 64 0a 20 20 2a 2a 20 64 6f 63 6c 69 73 74  and.  ** doclist
35e14 20 6c 65 6e 67 74 68 73 2e 20 20 41 74 20 73 6f   lengths.  At so
35e15 6d 65 20 70 6f 69 6e 74 2c 20 63 68 61 6e 67 65  me point, change
35e16 20 74 6f 0a 20 20 2a 2a 20 70 57 72 69 74 65 72   to.  ** pWriter
35e17 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2d 69 54 65  ->data.nData-iTe
35e18 72 6d 44 61 74 61 3e 53 54 41 4e 44 41 4c 4f 4e  rmData>STANDALON
35e19 45 5f 4d 49 4e 2e 0a 20 20 2a 2f 0a 20 20 69 66  E_MIN..  */.  if
35e1a 28 20 6e 54 65 72 6d 2b 6e 41 63 74 75 61 6c 44  ( nTerm+nActualD
35e1b 61 74 61 3e 53 54 41 4e 44 41 4c 4f 4e 45 5f 4d  ata>STANDALONE_M
35e1c 49 4e 20 29 7b 0a 20 20 20 20 2f 2a 20 50 75 73  IN ){.    /* Pus
35e1d 68 20 6c 65 61 66 20 6e 6f 64 65 20 66 72 6f 6d  h leaf node from
35e1e 20 62 65 66 6f 72 65 20 74 68 69 73 20 74 65 72   before this ter
35e1f 6d 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 69 54  m. */.    if( iT
35e20 65 72 6d 44 61 74 61 3e 30 20 29 7b 0a 20 20 20  ermData>0 ){.   
35e21 20 20 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74     rc = leafWrit
35e22 65 72 49 6e 74 65 72 6e 61 6c 46 6c 75 73 68 28  erInternalFlush(
35e23 76 2c 20 70 57 72 69 74 65 72 2c 20 30 2c 20 69  v, pWriter, 0, i
35e24 54 65 72 6d 44 61 74 61 29 3b 0a 20 20 20 20 20  TermData);.     
35e25 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
35e26 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
35e27 0a 20 20 20 20 20 20 70 57 72 69 74 65 72 2d 3e  .      pWriter->
35e28 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 20 3d 20  nTermDistinct = 
35e29 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 3b 0a 20  nTermDistinct;. 
35e2a 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 46 69 78     }..    /* Fix
35e2b 20 74 68 65 20 65 6e 63 6f 64 65 64 20 64 6f 63   the encoded doc
35e2c 6c 69 73 74 20 6c 65 6e 67 74 68 2e 20 2a 2f 0a  list length. */.
35e2d 20 20 20 20 69 44 6f 63 6c 69 73 74 44 61 74 61      iDoclistData
35e2e 20 2b 3d 20 6e 20 2d 20 6e 41 63 74 75 61 6c 3b   += n - nActual;
35e2f 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 57 72 69  .    memcpy(pWri
35e30 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b  ter->data.pData+
35e31 69 44 6f 63 6c 69 73 74 44 61 74 61 2c 20 63 2c  iDoclistData, c,
35e32 20 6e 41 63 74 75 61 6c 29 3b 0a 0a 20 20 20 20   nActual);..    
35e33 2f 2a 20 50 75 73 68 20 74 68 65 20 73 74 61 6e  /* Push the stan
35e34 64 61 6c 6f 6e 65 20 6c 65 61 66 20 6e 6f 64 65  dalone leaf node
35e35 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 6c 65  . */.    rc = le
35e36 61 66 57 72 69 74 65 72 49 6e 6c 69 6e 65 46 6c  afWriterInlineFl
35e37 75 73 68 28 76 2c 20 70 57 72 69 74 65 72 2c 20  ush(v, pWriter, 
35e38 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 44  pTerm, nTerm, iD
35e39 6f 63 6c 69 73 74 44 61 74 61 29 3b 0a 20 20 20  oclistData);.   
35e3a 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
35e3b 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
35e3c 0a 20 20 20 20 2f 2a 20 4c 65 61 76 65 20 74 68  .    /* Leave th
35e3d 65 20 6e 6f 64 65 20 65 6d 70 74 79 2e 20 2a 2f  e node empty. */
35e3e 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72 52  .    dataBufferR
35e3f 65 73 65 74 28 26 70 57 72 69 74 65 72 2d 3e 64  eset(&pWriter->d
35e40 61 74 61 29 3b 0a 0a 20 20 20 20 72 65 74 75 72  ata);..    retur
35e41 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  n rc;.  }..  /* 
35e42 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 77  At this point, w
35e43 65 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 20  e know that the 
35e44 64 6f 63 6c 69 73 74 20 77 61 73 20 73 6d 61 6c  doclist was smal
35e45 6c 2c 20 73 6f 20 64 6f 20 74 68 65 0a 20 20 2a  l, so do the.  *
35e46 2a 20 6d 65 6d 6d 6f 76 65 20 69 66 20 69 6e 64  * memmove if ind
35e47 69 63 61 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 69  icated..  */.  i
35e48 66 28 20 6e 41 63 74 75 61 6c 3c 6e 20 29 7b 0a  f( nActual<n ){.
35e49 20 20 20 20 6d 65 6d 6d 6f 76 65 28 70 57 72 69      memmove(pWri
35e4a 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b  ter->data.pData+
35e4b 69 44 6f 63 6c 69 73 74 44 61 74 61 2b 6e 41 63  iDoclistData+nAc
35e4c 74 75 61 6c 2c 0a 20 20 20 20 20 20 20 20 20 20  tual,.          
35e4d 20 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e    pWriter->data.
35e4e 70 44 61 74 61 2b 69 44 6f 63 6c 69 73 74 44 61  pData+iDoclistDa
35e4f 74 61 2b 6e 2c 0a 20 20 20 20 20 20 20 20 20 20  ta+n,.          
35e50 20 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e    pWriter->data.
35e51 6e 44 61 74 61 2d 28 69 44 6f 63 6c 69 73 74 44  nData-(iDoclistD
35e52 61 74 61 2b 6e 29 29 3b 0a 20 20 20 20 70 57 72  ata+n));.    pWr
35e53 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61  iter->data.nData
35e54 20 2d 3d 20 6e 2d 6e 41 63 74 75 61 6c 3b 0a 20   -= n-nActual;. 
35e55 20 7d 0a 0a 20 20 2f 2a 20 52 65 70 6c 61 63 65   }..  /* Replace
35e56 20 77 72 69 74 74 65 6e 20 6c 65 6e 67 74 68 20   written length 
35e57 77 69 74 68 20 61 63 74 75 61 6c 20 6c 65 6e 67  with actual leng
35e58 74 68 2e 20 2a 2f 0a 20 20 6d 65 6d 63 70 79 28  th. */.  memcpy(
35e59 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44  pWriter->data.pD
35e5a 61 74 61 2b 69 44 6f 63 6c 69 73 74 44 61 74 61  ata+iDoclistData
35e5b 2c 20 63 2c 20 6e 41 63 74 75 61 6c 29 3b 0a 0a  , c, nActual);..
35e5c 20 20 2f 2a 20 49 66 20 74 68 65 20 6e 6f 64 65    /* If the node
35e5d 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 2c 20 62   is too large, b
35e5e 72 65 61 6b 20 74 68 69 6e 67 73 20 75 70 2e 20  reak things up. 
35e5f 2a 2f 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65  */.  /* TODO(she
35e60 73 73 29 20 54 68 69 73 20 74 65 73 74 20 6d 61  ss) This test ma
35e61 74 63 68 65 73 20 6c 65 61 66 57 72 69 74 65 72  tches leafWriter
35e62 53 74 65 70 28 29 2c 20 77 68 69 63 68 20 64 6f  Step(), which do
35e63 65 73 20 74 68 69 73 0a 20 20 2a 2a 20 74 65 73  es this.  ** tes
35e64 74 20 62 65 66 6f 72 65 20 69 74 20 6b 6e 6f 77  t before it know
35e65 73 20 74 68 65 20 63 6f 73 74 20 74 6f 20 76 61  s the cost to va
35e66 72 69 6e 74 2d 65 6e 63 6f 64 65 20 74 68 65 20  rint-encode the 
35e67 74 65 72 6d 20 61 6e 64 0a 20 20 2a 2a 20 64 6f  term and.  ** do
35e68 63 6c 69 73 74 20 6c 65 6e 67 74 68 73 2e 20 20  clist lengths.  
35e69 41 74 20 73 6f 6d 65 20 70 6f 69 6e 74 2c 20 63  At some point, c
35e6a 68 61 6e 67 65 20 74 6f 0a 20 20 2a 2a 20 70 57  hange to.  ** pW
35e6b 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74  riter->data.nDat
35e6c 61 3e 4c 45 41 46 5f 4d 41 58 2e 0a 20 20 2a 2f  a>LEAF_MAX..  */
35e6d 0a 20 20 69 66 28 20 69 54 65 72 6d 44 61 74 61  .  if( iTermData
35e6e 2b 6e 54 65 72 6d 2b 6e 41 63 74 75 61 6c 44 61  +nTerm+nActualDa
35e6f 74 61 3e 4c 45 41 46 5f 4d 41 58 20 29 7b 0a 20  ta>LEAF_MAX ){. 
35e70 20 20 20 2f 2a 20 46 6c 75 73 68 20 6f 75 74 20     /* Flush out 
35e71 74 68 65 20 6c 65 61 64 69 6e 67 20 64 61 74 61  the leading data
35e72 20 61 73 20 61 20 6e 6f 64 65 20 2a 2f 0a 20 20   as a node */.  
35e73 20 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65    rc = leafWrite
35e74 72 49 6e 74 65 72 6e 61 6c 46 6c 75 73 68 28 76  rInternalFlush(v
35e75 2c 20 70 57 72 69 74 65 72 2c 20 30 2c 20 69 54  , pWriter, 0, iT
35e76 65 72 6d 44 61 74 61 29 3b 0a 20 20 20 20 69 66  ermData);.    if
35e77 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
35e78 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
35e79 20 20 70 57 72 69 74 65 72 2d 3e 6e 54 65 72 6d    pWriter->nTerm
35e7a 44 69 73 74 69 6e 63 74 20 3d 20 6e 54 65 72 6d  Distinct = nTerm
35e7b 44 69 73 74 69 6e 63 74 3b 0a 0a 20 20 20 20 2f  Distinct;..    /
35e7c 2a 20 52 65 62 75 69 6c 64 20 68 65 61 64 65 72  * Rebuild header
35e7d 20 75 73 69 6e 67 20 74 68 65 20 63 75 72 72 65   using the curre
35e7e 6e 74 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20 6e  nt term */.    n
35e7f 20 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74   = fts3PutVarint
35e80 28 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70  (pWriter->data.p
35e81 44 61 74 61 2c 20 30 29 3b 0a 20 20 20 20 6e 20  Data, 0);.    n 
35e82 2b 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74  += fts3PutVarint
35e83 28 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70  (pWriter->data.p
35e84 44 61 74 61 2b 6e 2c 20 6e 54 65 72 6d 29 3b 0a  Data+n, nTerm);.
35e85 20 20 20 20 6d 65 6d 63 70 79 28 70 57 72 69 74      memcpy(pWrit
35e86 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b 6e  er->data.pData+n
35e87 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b  , pTerm, nTerm);
35e88 0a 20 20 20 20 6e 20 2b 3d 20 6e 54 65 72 6d 3b  .    n += nTerm;
35e89 0a 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 20 73  ..    /* There s
35e8a 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65 20  hould always be 
35e8b 72 6f 6f 6d 2c 20 62 65 63 61 75 73 65 20 74 68  room, because th
35e8c 65 20 70 72 65 76 69 6f 75 73 20 65 6e 63 6f 64  e previous encod
35e8d 69 6e 67 0a 20 20 20 20 2a 2a 20 69 6e 63 6c 75  ing.    ** inclu
35e8e 64 65 64 20 61 6c 6c 20 64 61 74 61 20 6e 65 63  ded all data nec
35e8f 65 73 73 61 72 79 20 74 6f 20 63 6f 6e 73 74 72  essary to constr
35e90 75 63 74 20 74 68 65 20 74 65 72 6d 2e 0a 20 20  uct the term..  
35e91 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28    */.    assert(
35e92 20 6e 3c 69 44 6f 63 6c 69 73 74 44 61 74 61 20   n<iDoclistData 
35e93 29 3b 0a 20 20 20 20 2f 2a 20 53 6f 20 6c 6f 6e  );.    /* So lon
35e94 67 20 61 73 20 53 54 41 4e 44 41 4c 4f 4e 45 5f  g as STANDALONE_
35e95 4d 49 4e 20 69 73 20 68 61 6c 66 20 6f 72 20 6c  MIN is half or l
35e96 65 73 73 20 6f 66 20 4c 45 41 46 5f 4d 41 58 2c  ess of LEAF_MAX,
35e97 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 6f 6c 6c   the.    ** foll
35e98 6f 77 69 6e 67 20 6d 65 6d 63 70 79 28 29 20 69  owing memcpy() i
35e99 73 20 73 61 66 65 20 28 61 73 20 6f 70 70 6f 73  s safe (as oppos
35e9a 65 64 20 74 6f 20 6e 65 65 64 69 6e 67 20 61 20  ed to needing a 
35e9b 6d 65 6d 6d 6f 76 65 29 2e 0a 20 20 20 20 2a 2f  memmove)..    */
35e9c 0a 20 20 20 20 61 73 73 65 72 74 28 20 32 2a 53  .    assert( 2*S
35e9d 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 3c 3d 4c  TANDALONE_MIN<=L
35e9e 45 41 46 5f 4d 41 58 20 29 3b 0a 20 20 20 20 61  EAF_MAX );.    a
35e9f 73 73 65 72 74 28 20 6e 2b 70 57 72 69 74 65 72  ssert( n+pWriter
35ea0 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2d 69 44 6f  ->data.nData-iDo
35ea1 63 6c 69 73 74 44 61 74 61 3c 69 44 6f 63 6c 69  clistData<iDocli
35ea2 73 74 44 61 74 61 20 29 3b 0a 20 20 20 20 6d 65  stData );.    me
35ea3 6d 63 70 79 28 70 57 72 69 74 65 72 2d 3e 64 61  mcpy(pWriter->da
35ea4 74 61 2e 70 44 61 74 61 2b 6e 2c 0a 20 20 20 20  ta.pData+n,.    
35ea5 20 20 20 20 20 20 20 70 57 72 69 74 65 72 2d 3e         pWriter->
35ea6 64 61 74 61 2e 70 44 61 74 61 2b 69 44 6f 63 6c  data.pData+iDocl
35ea7 69 73 74 44 61 74 61 2c 0a 20 20 20 20 20 20 20  istData,.       
35ea8 20 20 20 20 70 57 72 69 74 65 72 2d 3e 64 61 74      pWriter->dat
35ea9 61 2e 6e 44 61 74 61 2d 69 44 6f 63 6c 69 73 74  a.nData-iDoclist
35eaa 44 61 74 61 29 3b 0a 20 20 20 20 70 57 72 69 74  Data);.    pWrit
35eab 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 20 2d  er->data.nData -
35eac 3d 20 69 44 6f 63 6c 69 73 74 44 61 74 61 2d 6e  = iDoclistData-n
35ead 3b 0a 20 20 7d 0a 20 20 41 53 53 45 52 54 5f 56  ;.  }.  ASSERT_V
35eae 41 4c 49 44 5f 4c 45 41 46 5f 4e 4f 44 45 28 70  ALID_LEAF_NODE(p
35eaf 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61  Writer->data.pDa
35eb0 74 61 2c 20 70 57 72 69 74 65 72 2d 3e 64 61 74  ta, pWriter->dat
35eb1 61 2e 6e 44 61 74 61 29 3b 0a 0a 20 20 72 65 74  a.nData);..  ret
35eb2 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
35eb3 0a 0a 2f 2a 20 50 75 73 68 20 70 54 65 72 6d 5b  ../* Push pTerm[
35eb4 6e 54 65 72 6d 5d 20 61 6c 6f 6e 67 20 77 69 74  nTerm] along wit
35eb5 68 20 74 68 65 20 64 6f 63 6c 69 73 74 20 64 61  h the doclist da
35eb6 74 61 20 74 6f 20 74 68 65 20 6c 65 61 66 20 6c  ta to the leaf l
35eb7 61 79 65 72 20 6f 66 0a 2a 2a 20 25 5f 73 65 67  ayer of.** %_seg
35eb8 6d 65 6e 74 73 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44  ments..*/./* TOD
35eb9 4f 28 73 68 65 73 73 29 20 52 65 76 69 73 65 20  O(shess) Revise 
35eba 77 72 69 74 65 5a 65 72 6f 53 65 67 6d 65 6e 74  writeZeroSegment
35ebb 28 29 20 73 6f 20 74 68 61 74 20 64 6f 63 6c 69  () so that docli
35ebc 73 74 73 20 61 72 65 0a 2a 2a 20 63 6f 6e 73 74  sts are.** const
35ebd 72 75 63 74 65 64 20 64 69 72 65 63 74 6c 79 20  ructed directly 
35ebe 69 6e 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61  in pWriter->data
35ebf 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
35ec0 6c 65 61 66 57 72 69 74 65 72 53 74 65 70 28 66  leafWriterStep(f
35ec1 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c  ulltext_vtab *v,
35ec2 20 4c 65 61 66 57 72 69 74 65 72 20 2a 70 57 72   LeafWriter *pWr
35ec3 69 74 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20  iter,.          
35ec4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35ec5 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72  const char *pTer
35ec6 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 0a 20 20  m, int nTerm,.  
35ec7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35ec8 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
35ec9 61 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e  ar *pData, int n
35eca 44 61 74 61 29 7b 0a 20 20 69 6e 74 20 72 63 3b  Data){.  int rc;
35ecb 0a 20 20 44 4c 52 65 61 64 65 72 20 72 65 61 64  .  DLReader read
35ecc 65 72 3b 0a 0a 20 20 64 6c 72 49 6e 69 74 28 26  er;..  dlrInit(&
35ecd 72 65 61 64 65 72 2c 20 44 4c 5f 44 45 46 41 55  reader, DL_DEFAU
35ece 4c 54 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61  LT, pData, nData
35ecf 29 3b 0a 20 20 72 63 20 3d 20 6c 65 61 66 57 72  );.  rc = leafWr
35ed0 69 74 65 72 53 74 65 70 4d 65 72 67 65 28 76 2c  iterStepMerge(v,
35ed1 20 70 57 72 69 74 65 72 2c 20 70 54 65 72 6d 2c   pWriter, pTerm,
35ed2 20 6e 54 65 72 6d 2c 20 26 72 65 61 64 65 72 2c   nTerm, &reader,
35ed3 20 31 29 3b 0a 20 20 64 6c 72 44 65 73 74 72 6f   1);.  dlrDestro
35ed4 79 28 26 72 65 61 64 65 72 29 3b 0a 0a 20 20 72  y(&reader);..  r
35ed5 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a  eturn rc;.}.../*
35ed6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35ed7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35ed8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35ed9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
35eda 0a 2f 2a 20 4c 65 61 66 52 65 61 64 65 72 20 69  ./* LeafReader i
35edb 73 20 75 73 65 64 20 74 6f 20 69 74 65 72 61 74  s used to iterat
35edc 65 20 6f 76 65 72 20 61 6e 20 69 6e 64 69 76 69  e over an indivi
35edd 64 75 61 6c 20 6c 65 61 66 20 6e 6f 64 65 2e 20  dual leaf node. 
35ede 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
35edf 74 20 4c 65 61 66 52 65 61 64 65 72 20 7b 0a 20  t LeafReader {. 
35ee0 20 44 61 74 61 42 75 66 66 65 72 20 74 65 72 6d   DataBuffer term
35ee1 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f  ;          /* co
35ee2 70 79 20 6f 66 20 63 75 72 72 65 6e 74 20 74 65  py of current te
35ee3 72 6d 2e 20 2a 2f 0a 0a 20 20 63 6f 6e 73 74 20  rm. */..  const 
35ee4 63 68 61 72 20 2a 70 44 61 74 61 3b 20 20 20 20  char *pData;    
35ee5 20 20 20 20 2f 2a 20 64 61 74 61 20 66 6f 72 20      /* data for 
35ee6 63 75 72 72 65 6e 74 20 74 65 72 6d 2e 20 2a 2f  current term. */
35ee7 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 0a 7d 20  .  int nData;.} 
35ee8 4c 65 61 66 52 65 61 64 65 72 3b 0a 0a 73 74 61  LeafReader;..sta
35ee9 74 69 63 20 76 6f 69 64 20 6c 65 61 66 52 65 61  tic void leafRea
35eea 64 65 72 44 65 73 74 72 6f 79 28 4c 65 61 66 52  derDestroy(LeafR
35eeb 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
35eec 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73  .  dataBufferDes
35eed 74 72 6f 79 28 26 70 52 65 61 64 65 72 2d 3e 74  troy(&pReader->t
35eee 65 72 6d 29 3b 0a 20 20 53 43 52 41 4d 42 4c 45  erm);.  SCRAMBLE
35eef 28 70 52 65 61 64 65 72 29 3b 0a 7d 0a 0a 73 74  (pReader);.}..st
35ef0 61 74 69 63 20 69 6e 74 20 6c 65 61 66 52 65 61  atic int leafRea
35ef1 64 65 72 41 74 45 6e 64 28 4c 65 61 66 52 65 61  derAtEnd(LeafRea
35ef2 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
35ef3 20 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d   return pReader-
35ef4 3e 6e 44 61 74 61 3c 3d 30 3b 0a 7d 0a 0a 2f 2a  >nData<=0;.}../*
35ef5 20 41 63 63 65 73 73 20 74 68 65 20 63 75 72 72   Access the curr
35ef6 65 6e 74 20 74 65 72 6d 2e 20 2a 2f 0a 73 74 61  ent term. */.sta
35ef7 74 69 63 20 69 6e 74 20 6c 65 61 66 52 65 61 64  tic int leafRead
35ef8 65 72 54 65 72 6d 42 79 74 65 73 28 4c 65 61 66  erTermBytes(Leaf
35ef9 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
35efa 7b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61 64  {.  return pRead
35efb 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 3b 0a  er->term.nData;.
35efc 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  }.static const c
35efd 68 61 72 20 2a 6c 65 61 66 52 65 61 64 65 72 54  har *leafReaderT
35efe 65 72 6d 28 4c 65 61 66 52 65 61 64 65 72 20 2a  erm(LeafReader *
35eff 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65  pReader){.  asse
35f00 72 74 28 20 70 52 65 61 64 65 72 2d 3e 74 65 72  rt( pReader->ter
35f01 6d 2e 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20 72  m.nData>0 );.  r
35f02 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 74  eturn pReader->t
35f03 65 72 6d 2e 70 44 61 74 61 3b 0a 7d 0a 0a 2f 2a  erm.pData;.}../*
35f04 20 41 63 63 65 73 73 20 74 68 65 20 64 6f 63 6c   Access the docl
35f05 69 73 74 20 64 61 74 61 20 66 6f 72 20 74 68 65  ist data for the
35f06 20 63 75 72 72 65 6e 74 20 74 65 72 6d 2e 20 2a   current term. *
35f07 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61  /.static int lea
35f08 66 52 65 61 64 65 72 44 61 74 61 42 79 74 65 73  fReaderDataBytes
35f09 28 4c 65 61 66 52 65 61 64 65 72 20 2a 70 52 65  (LeafReader *pRe
35f0a 61 64 65 72 29 7b 0a 20 20 69 6e 74 20 6e 44 61  ader){.  int nDa
35f0b 74 61 3b 0a 20 20 61 73 73 65 72 74 28 20 70 52  ta;.  assert( pR
35f0c 65 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74  eader->term.nDat
35f0d 61 3e 30 20 29 3b 0a 20 20 66 74 73 33 47 65 74  a>0 );.  fts3Get
35f0e 56 61 72 69 6e 74 33 32 28 70 52 65 61 64 65 72  Varint32(pReader
35f0f 2d 3e 70 44 61 74 61 2c 20 26 6e 44 61 74 61 29  ->pData, &nData)
35f10 3b 0a 20 20 72 65 74 75 72 6e 20 6e 44 61 74 61  ;.  return nData
35f11 3b 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  ;.}.static const
35f12 20 63 68 61 72 20 2a 6c 65 61 66 52 65 61 64 65   char *leafReade
35f13 72 44 61 74 61 28 4c 65 61 66 52 65 61 64 65 72  rData(LeafReader
35f14 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 69 6e   *pReader){.  in
35f15 74 20 6e 2c 20 6e 44 61 74 61 3b 0a 20 20 61 73  t n, nData;.  as
35f16 73 65 72 74 28 20 70 52 65 61 64 65 72 2d 3e 74  sert( pReader->t
35f17 65 72 6d 2e 6e 44 61 74 61 3e 30 20 29 3b 0a 20  erm.nData>0 );. 
35f18 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69   n = fts3GetVari
35f19 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44  nt32(pReader->pD
35f1a 61 74 61 2c 20 26 6e 44 61 74 61 29 3b 0a 20 20  ata, &nData);.  
35f1b 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e  return pReader->
35f1c 70 44 61 74 61 2b 6e 3b 0a 7d 0a 0a 73 74 61 74  pData+n;.}..stat
35f1d 69 63 20 76 6f 69 64 20 6c 65 61 66 52 65 61 64  ic void leafRead
35f1e 65 72 49 6e 69 74 28 63 6f 6e 73 74 20 63 68 61  erInit(const cha
35f1f 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44  r *pData, int nD
35f20 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ata,.           
35f21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35f22 4c 65 61 66 52 65 61 64 65 72 20 2a 70 52 65 61  LeafReader *pRea
35f23 64 65 72 29 7b 0a 20 20 69 6e 74 20 6e 54 65 72  der){.  int nTer
35f24 6d 2c 20 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28  m, n;..  assert(
35f25 20 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20 61 73   nData>0 );.  as
35f26 73 65 72 74 28 20 70 44 61 74 61 5b 30 5d 3d 3d  sert( pData[0]==
35f27 27 5c 30 27 20 29 3b 0a 0a 20 20 43 4c 45 41 52  '\0' );..  CLEAR
35f28 28 70 52 65 61 64 65 72 29 3b 0a 0a 20 20 2f 2a  (pReader);..  /*
35f29 20 52 65 61 64 20 74 68 65 20 66 69 72 73 74 20   Read the first 
35f2a 74 65 72 6d 2c 20 73 6b 69 70 70 69 6e 67 20 74  term, skipping t
35f2b 68 65 20 68 65 61 64 65 72 20 62 79 74 65 2e 20  he header byte. 
35f2c 2a 2f 0a 20 20 6e 20 3d 20 66 74 73 33 47 65 74  */.  n = fts3Get
35f2d 56 61 72 69 6e 74 33 32 28 70 44 61 74 61 2b 31  Varint32(pData+1
35f2e 2c 20 26 6e 54 65 72 6d 29 3b 0a 20 20 64 61 74  , &nTerm);.  dat
35f2f 61 42 75 66 66 65 72 49 6e 69 74 28 26 70 52 65  aBufferInit(&pRe
35f30 61 64 65 72 2d 3e 74 65 72 6d 2c 20 6e 54 65 72  ader->term, nTer
35f31 6d 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72  m);.  dataBuffer
35f32 52 65 70 6c 61 63 65 28 26 70 52 65 61 64 65 72  Replace(&pReader
35f33 2d 3e 74 65 72 6d 2c 20 70 44 61 74 61 2b 31 2b  ->term, pData+1+
35f34 6e 2c 20 6e 54 65 72 6d 29 3b 0a 0a 20 20 2f 2a  n, nTerm);..  /*
35f35 20 50 6f 73 69 74 69 6f 6e 20 61 66 74 65 72 20   Position after 
35f36 74 68 65 20 66 69 72 73 74 20 74 65 72 6d 2e 20  the first term. 
35f37 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 31 2b 6e  */.  assert( 1+n
35f38 2b 6e 54 65 72 6d 3c 6e 44 61 74 61 20 29 3b 0a  +nTerm<nData );.
35f39 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61    pReader->pData
35f3a 20 3d 20 70 44 61 74 61 2b 31 2b 6e 2b 6e 54 65   = pData+1+n+nTe
35f3b 72 6d 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 6e  rm;.  pReader->n
35f3c 44 61 74 61 20 3d 20 6e 44 61 74 61 2d 31 2d 6e  Data = nData-1-n
35f3d 2d 6e 54 65 72 6d 3b 0a 7d 0a 0a 2f 2a 20 53 74  -nTerm;.}../* St
35f3e 65 70 20 74 68 65 20 72 65 61 64 65 72 20 66 6f  ep the reader fo
35f3f 72 77 61 72 64 20 74 6f 20 74 68 65 20 6e 65 78  rward to the nex
35f40 74 20 74 65 72 6d 2e 20 2a 2f 0a 73 74 61 74 69  t term. */.stati
35f41 63 20 76 6f 69 64 20 6c 65 61 66 52 65 61 64 65  c void leafReade
35f42 72 53 74 65 70 28 4c 65 61 66 52 65 61 64 65 72  rStep(LeafReader
35f43 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 69 6e   *pReader){.  in
35f44 74 20 6e 2c 20 6e 44 61 74 61 2c 20 6e 50 72 65  t n, nData, nPre
35f45 66 69 78 2c 20 6e 53 75 66 66 69 78 3b 0a 20 20  fix, nSuffix;.  
35f46 61 73 73 65 72 74 28 20 21 6c 65 61 66 52 65 61  assert( !leafRea
35f47 64 65 72 41 74 45 6e 64 28 70 52 65 61 64 65 72  derAtEnd(pReader
35f48 29 20 29 3b 0a 0a 20 20 2f 2a 20 53 6b 69 70 20  ) );..  /* Skip 
35f49 70 72 65 76 69 6f 75 73 20 65 6e 74 72 79 27 73  previous entry's
35f4a 20 64 61 74 61 20 62 6c 6f 63 6b 2e 20 2a 2f 0a   data block. */.
35f4b 20 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72    n = fts3GetVar
35f4c 69 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70  int32(pReader->p
35f4d 44 61 74 61 2c 20 26 6e 44 61 74 61 29 3b 0a 20  Data, &nData);. 
35f4e 20 61 73 73 65 72 74 28 20 6e 2b 6e 44 61 74 61   assert( n+nData
35f4f 3c 3d 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61  <=pReader->nData
35f50 20 29 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 70   );.  pReader->p
35f51 44 61 74 61 20 2b 3d 20 6e 2b 6e 44 61 74 61 3b  Data += n+nData;
35f52 0a 20 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74  .  pReader->nDat
35f53 61 20 2d 3d 20 6e 2b 6e 44 61 74 61 3b 0a 0a 20  a -= n+nData;.. 
35f54 20 69 66 28 20 21 6c 65 61 66 52 65 61 64 65 72   if( !leafReader
35f55 41 74 45 6e 64 28 70 52 65 61 64 65 72 29 20 29  AtEnd(pReader) )
35f56 7b 0a 20 20 20 20 2f 2a 20 43 6f 6e 73 74 72 75  {.    /* Constru
35f57 63 74 20 74 68 65 20 6e 65 77 20 74 65 72 6d 20  ct the new term 
35f58 75 73 69 6e 67 20 61 20 70 72 65 66 69 78 20 66  using a prefix f
35f59 72 6f 6d 20 74 68 65 20 6f 6c 64 20 74 65 72 6d  rom the old term
35f5a 20 70 6c 75 73 20 61 0a 20 20 20 20 2a 2a 20 73   plus a.    ** s
35f5b 75 66 66 69 78 20 66 72 6f 6d 20 74 68 65 20 6c  uffix from the l
35f5c 65 61 66 20 64 61 74 61 2e 0a 20 20 20 20 2a 2f  eaf data..    */
35f5d 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 47 65 74  .    n = fts3Get
35f5e 56 61 72 69 6e 74 33 32 28 70 52 65 61 64 65 72  Varint32(pReader
35f5f 2d 3e 70 44 61 74 61 2c 20 26 6e 50 72 65 66 69  ->pData, &nPrefi
35f60 78 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73  x);.    n += fts
35f61 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 52 65  3GetVarint32(pRe
35f62 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26  ader->pData+n, &
35f63 6e 53 75 66 66 69 78 29 3b 0a 20 20 20 20 61 73  nSuffix);.    as
35f64 73 65 72 74 28 20 6e 2b 6e 53 75 66 66 69 78 3c  sert( n+nSuffix<
35f65 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 29  pReader->nData )
35f66 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e 74  ;.    pReader->t
35f67 65 72 6d 2e 6e 44 61 74 61 20 3d 20 6e 50 72 65  erm.nData = nPre
35f68 66 69 78 3b 0a 20 20 20 20 64 61 74 61 42 75 66  fix;.    dataBuf
35f69 66 65 72 41 70 70 65 6e 64 28 26 70 52 65 61 64  ferAppend(&pRead
35f6a 65 72 2d 3e 74 65 72 6d 2c 20 70 52 65 61 64 65  er->term, pReade
35f6b 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 6e 53 75 66  r->pData+n, nSuf
35f6c 66 69 78 29 3b 0a 0a 20 20 20 20 70 52 65 61 64  fix);..    pRead
35f6d 65 72 2d 3e 70 44 61 74 61 20 2b 3d 20 6e 2b 6e  er->pData += n+n
35f6e 53 75 66 66 69 78 3b 0a 20 20 20 20 70 52 65 61  Suffix;.    pRea
35f6f 64 65 72 2d 3e 6e 44 61 74 61 20 2d 3d 20 6e 2b  der->nData -= n+
35f70 6e 53 75 66 66 69 78 3b 0a 20 20 7d 0a 7d 0a 0a  nSuffix;.  }.}..
35f71 2f 2a 20 73 74 72 63 6d 70 2d 73 74 79 6c 65 20  /* strcmp-style 
35f72 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20 70 52  comparison of pR
35f73 65 61 64 65 72 27 73 20 63 75 72 72 65 6e 74 20  eader's current 
35f74 74 65 72 6d 20 61 67 61 69 6e 73 74 20 70 54 65  term against pTe
35f75 72 6d 2e 0a 2a 2a 20 49 66 20 69 73 50 72 65 66  rm..** If isPref
35f76 69 78 2c 20 65 71 75 61 6c 69 74 79 20 6d 65 61  ix, equality mea
35f77 6e 73 20 65 71 75 61 6c 20 74 68 72 6f 75 67 68  ns equal through
35f78 20 6e 54 65 72 6d 20 62 79 74 65 73 2e 0a 2a 2f   nTerm bytes..*/
35f79 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 66  .static int leaf
35f7a 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28 4c 65  ReaderTermCmp(Le
35f7b 61 66 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  afReader *pReade
35f7c 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r,.             
35f7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35f7e 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72  const char *pTer
35f7f 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20 69 6e  m, int nTerm, in
35f80 74 20 69 73 50 72 65 66 69 78 29 7b 0a 20 20 69  t isPrefix){.  i
35f81 6e 74 20 63 2c 20 6e 20 3d 20 70 52 65 61 64 65  nt c, n = pReade
35f82 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 3c 6e 54  r->term.nData<nT
35f83 65 72 6d 20 3f 20 70 52 65 61 64 65 72 2d 3e 74  erm ? pReader->t
35f84 65 72 6d 2e 6e 44 61 74 61 20 3a 20 6e 54 65 72  erm.nData : nTer
35f85 6d 3b 0a 20 20 69 66 28 20 6e 3d 3d 30 20 29 7b  m;.  if( n==0 ){
35f86 0a 20 20 20 20 69 66 28 20 70 52 65 61 64 65 72  .    if( pReader
35f87 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 3e 30 20 29  ->term.nData>0 )
35f88 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20   return -1;.    
35f89 69 66 28 6e 54 65 72 6d 3e 30 20 29 20 72 65 74  if(nTerm>0 ) ret
35f8a 75 72 6e 20 31 3b 0a 20 20 20 20 72 65 74 75 72  urn 1;.    retur
35f8b 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 63 20 3d 20  n 0;.  }..  c = 
35f8c 6d 65 6d 63 6d 70 28 70 52 65 61 64 65 72 2d 3e  memcmp(pReader->
35f8d 74 65 72 6d 2e 70 44 61 74 61 2c 20 70 54 65 72  term.pData, pTer
35f8e 6d 2c 20 6e 29 3b 0a 20 20 69 66 28 20 63 21 3d  m, n);.  if( c!=
35f8f 30 20 29 20 72 65 74 75 72 6e 20 63 3b 0a 20 20  0 ) return c;.  
35f90 69 66 28 20 69 73 50 72 65 66 69 78 20 26 26 20  if( isPrefix && 
35f91 6e 3d 3d 6e 54 65 72 6d 20 29 20 72 65 74 75 72  n==nTerm ) retur
35f92 6e 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 70 52  n 0;.  return pR
35f93 65 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74  eader->term.nDat
35f94 61 20 2d 20 6e 54 65 72 6d 3b 0a 7d 0a 0a 0a 2f  a - nTerm;.}.../
35f95 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35f96 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35f97 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35f98 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35f99 2f 0a 2f 2a 20 4c 65 61 76 65 73 52 65 61 64 65  /./* LeavesReade
35f9a 72 20 77 72 61 70 73 20 4c 65 61 66 52 65 61 64  r wraps LeafRead
35f9b 65 72 20 74 6f 20 61 6c 6c 6f 77 20 69 74 65 72  er to allow iter
35f9c 61 74 69 6e 67 20 6f 76 65 72 20 74 68 65 20 65  ating over the e
35f9d 6e 74 69 72 65 0a 2a 2a 20 6c 65 61 66 20 6c 61  ntire.** leaf la
35f9e 79 65 72 20 6f 66 20 74 68 65 20 74 72 65 65 2e  yer of the tree.
35f9f 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
35fa0 63 74 20 4c 65 61 76 65 73 52 65 61 64 65 72 20  ct LeavesReader 
35fa1 7b 0a 20 20 69 6e 74 20 69 64 78 3b 20 20 20 20  {.  int idx;    
35fa2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35fa3 20 49 6e 64 65 78 20 77 69 74 68 69 6e 20 74 68   Index within th
35fa4 65 20 73 65 67 6d 65 6e 74 2e 20 2a 2f 0a 0a 20  e segment. */.. 
35fa5 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
35fa6 53 74 6d 74 3b 20 20 20 20 20 20 2f 2a 20 53 74  Stmt;      /* St
35fa7 61 74 65 6d 65 6e 74 20 77 65 27 72 65 20 73 74  atement we're st
35fa8 72 65 61 6d 69 6e 67 20 6c 65 61 76 65 73 20 66  reaming leaves f
35fa9 72 6f 6d 2e 20 2a 2f 0a 20 20 69 6e 74 20 65 6f  rom. */.  int eo
35faa 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f;              
35fab 20 20 20 20 2f 2a 20 77 65 27 76 65 20 73 65 65      /* we've see
35fac 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 66 72  n SQLITE_DONE fr
35fad 6f 6d 20 70 53 74 6d 74 2e 20 2a 2f 0a 0a 20 20  om pStmt. */..  
35fae 4c 65 61 66 52 65 61 64 65 72 20 6c 65 61 66 52  LeafReader leafR
35faf 65 61 64 65 72 3b 20 20 20 20 2f 2a 20 72 65 61  eader;    /* rea
35fb0 64 65 72 20 66 6f 72 20 74 68 65 20 63 75 72 72  der for the curr
35fb1 65 6e 74 20 6c 65 61 66 2e 20 2a 2f 0a 20 20 44  ent leaf. */.  D
35fb2 61 74 61 42 75 66 66 65 72 20 72 6f 6f 74 44 61  ataBuffer rootDa
35fb3 74 61 3b 20 20 20 20 20 20 2f 2a 20 72 6f 6f 74  ta;      /* root
35fb4 20 64 61 74 61 20 66 6f 72 20 69 6e 6c 69 6e 65   data for inline
35fb5 2e 20 2a 2f 0a 7d 20 4c 65 61 76 65 73 52 65 61  . */.} LeavesRea
35fb6 64 65 72 3b 0a 0a 2f 2a 20 41 63 63 65 73 73 20  der;../* Access 
35fb7 74 68 65 20 63 75 72 72 65 6e 74 20 74 65 72 6d  the current term
35fb8 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  . */.static int 
35fb9 6c 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d  leavesReaderTerm
35fba 42 79 74 65 73 28 4c 65 61 76 65 73 52 65 61 64  Bytes(LeavesRead
35fbb 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
35fbc 61 73 73 65 72 74 28 20 21 70 52 65 61 64 65 72  assert( !pReader
35fbd 2d 3e 65 6f 66 20 29 3b 0a 20 20 72 65 74 75 72  ->eof );.  retur
35fbe 6e 20 6c 65 61 66 52 65 61 64 65 72 54 65 72 6d  n leafReaderTerm
35fbf 42 79 74 65 73 28 26 70 52 65 61 64 65 72 2d 3e  Bytes(&pReader->
35fc0 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 7d 0a 73  leafReader);.}.s
35fc1 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
35fc2 20 2a 6c 65 61 76 65 73 52 65 61 64 65 72 54 65   *leavesReaderTe
35fc3 72 6d 28 4c 65 61 76 65 73 52 65 61 64 65 72 20  rm(LeavesReader 
35fc4 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73  *pReader){.  ass
35fc5 65 72 74 28 20 21 70 52 65 61 64 65 72 2d 3e 65  ert( !pReader->e
35fc6 6f 66 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 6c  of );.  return l
35fc7 65 61 66 52 65 61 64 65 72 54 65 72 6d 28 26 70  eafReaderTerm(&p
35fc8 52 65 61 64 65 72 2d 3e 6c 65 61 66 52 65 61 64  Reader->leafRead
35fc9 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 41 63 63 65 73  er);.}../* Acces
35fca 73 20 74 68 65 20 64 6f 63 6c 69 73 74 20 64 61  s the doclist da
35fcb 74 61 20 66 6f 72 20 74 68 65 20 63 75 72 72 65  ta for the curre
35fcc 6e 74 20 74 65 72 6d 2e 20 2a 2f 0a 73 74 61 74  nt term. */.stat
35fcd 69 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65 61  ic int leavesRea
35fce 64 65 72 44 61 74 61 42 79 74 65 73 28 4c 65 61  derDataBytes(Lea
35fcf 76 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64  vesReader *pRead
35fd0 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21  er){.  assert( !
35fd1 70 52 65 61 64 65 72 2d 3e 65 6f 66 20 29 3b 0a  pReader->eof );.
35fd2 20 20 72 65 74 75 72 6e 20 6c 65 61 66 52 65 61    return leafRea
35fd3 64 65 72 44 61 74 61 42 79 74 65 73 28 26 70 52  derDataBytes(&pR
35fd4 65 61 64 65 72 2d 3e 6c 65 61 66 52 65 61 64 65  eader->leafReade
35fd5 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e  r);.}.static con
35fd6 73 74 20 63 68 61 72 20 2a 6c 65 61 76 65 73 52  st char *leavesR
35fd7 65 61 64 65 72 44 61 74 61 28 4c 65 61 76 65 73  eaderData(Leaves
35fd8 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
35fd9 7b 0a 20 20 61 73 73 65 72 74 28 20 21 70 52 65  {.  assert( !pRe
35fda 61 64 65 72 2d 3e 65 6f 66 20 29 3b 0a 20 20 72  ader->eof );.  r
35fdb 65 74 75 72 6e 20 6c 65 61 66 52 65 61 64 65 72  eturn leafReader
35fdc 44 61 74 61 28 26 70 52 65 61 64 65 72 2d 3e 6c  Data(&pReader->l
35fdd 65 61 66 52 65 61 64 65 72 29 3b 0a 7d 0a 0a 73  eafReader);.}..s
35fde 74 61 74 69 63 20 69 6e 74 20 6c 65 61 76 65 73  tatic int leaves
35fdf 52 65 61 64 65 72 41 74 45 6e 64 28 4c 65 61 76  ReaderAtEnd(Leav
35fe0 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  esReader *pReade
35fe1 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 52 65  r){.  return pRe
35fe2 61 64 65 72 2d 3e 65 6f 66 3b 0a 7d 0a 0a 2f 2a  ader->eof;.}../*
35fe3 20 6c 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61 76   loadSegmentLeav
35fe4 65 73 28 29 20 6d 61 79 20 6e 6f 74 20 72 65 61  es() may not rea
35fe5 64 20 61 6c 6c 20 74 68 65 20 77 61 79 20 74 6f  d all the way to
35fe6 20 53 51 4c 49 54 45 5f 44 4f 4e 45 2c 20 74 68   SQLITE_DONE, th
35fe7 75 73 0a 2a 2a 20 6c 65 61 76 69 6e 67 20 74 68  us.** leaving th
35fe8 65 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64  e statement hand
35fe9 6c 65 20 6f 70 65 6e 2c 20 77 68 69 63 68 20 6c  le open, which l
35fea 6f 63 6b 73 20 74 68 65 20 74 61 62 6c 65 2e 0a  ocks the table..
35feb 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73  */./* TODO(shess
35fec 29 20 54 68 69 73 20 22 73 6f 6c 75 74 69 6f 6e  ) This "solution
35fed 22 20 69 73 20 6e 6f 74 20 73 61 74 69 73 66 61  " is not satisfa
35fee 63 74 6f 72 79 2e 20 20 52 65 61 6c 6c 79 2c 20  ctory.  Really, 
35fef 74 68 65 72 65 0a 2a 2a 20 73 68 6f 75 6c 64 20  there.** should 
35ff0 62 65 20 63 68 65 63 6b 2d 69 6e 20 66 75 6e 63  be check-in func
35ff1 74 69 6f 6e 20 66 6f 72 20 61 6c 6c 20 73 74 61  tion for all sta
35ff2 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 73 20 77  tement handles w
35ff3 68 69 63 68 0a 2a 2a 20 61 72 72 61 6e 67 65 73  hich.** arranges
35ff4 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 33   to call sqlite3
35ff5 5f 72 65 73 65 74 28 29 2e 20 20 54 68 69 73 20  _reset().  This 
35ff6 6d 6f 73 74 20 6c 69 6b 65 6c 79 20 77 69 6c 6c  most likely will
35ff7 20 72 65 71 75 69 72 65 0a 2a 2a 20 6d 6f 64 69   require.** modi
35ff8 66 69 63 61 74 69 6f 6e 20 74 6f 20 63 6f 6e 74  fication to cont
35ff9 72 6f 6c 20 66 6c 6f 77 20 61 6c 6c 20 6f 76 65  rol flow all ove
35ffa 72 20 74 68 65 20 70 6c 61 63 65 2c 20 74 68 6f  r the place, tho
35ffb 75 67 68 2c 20 73 6f 20 66 6f 72 20 6e 6f 77 0a  ugh, so for now.
35ffc 2a 2a 20 6a 75 73 74 20 70 75 6e 74 2e 0a 2a 2a  ** just punt..**
35ffd 0a 2a 2a 20 4e 6f 74 65 20 74 68 65 20 74 68 65  .** Note the the
35ffe 20 63 75 72 72 65 6e 74 20 73 79 73 74 65 6d 20   current system 
35fff 61 73 73 75 6d 65 73 20 74 68 61 74 20 73 65 67  assumes that seg
36000 6d 65 6e 74 20 6d 65 72 67 65 73 20 77 69 6c 6c  ment merges will
36001 20 72 75 6e 20 74 6f 0a 2a 2a 20 63 6f 6d 70 6c   run to.** compl
36002 65 74 69 6f 6e 2c 20 77 68 69 63 68 20 69 73 20  etion, which is 
36003 77 68 79 20 74 68 69 73 20 70 61 72 74 69 63 75  why this particu
36004 6c 61 72 20 70 72 6f 62 61 62 6c 79 20 68 61 73  lar probably has
36005 6e 27 74 20 61 72 69 73 65 6e 20 69 6e 0a 2a 2a  n't arisen in.**
36006 20 74 68 69 73 20 63 61 73 65 2e 20 20 50 72 6f   this case.  Pro
36007 62 61 62 6c 79 20 61 20 62 72 69 74 74 6c 65 20  bably a brittle 
36008 61 73 73 75 6d 70 74 69 6f 6e 2e 0a 2a 2f 0a 73  assumption..*/.s
36009 74 61 74 69 63 20 69 6e 74 20 6c 65 61 76 65 73  tatic int leaves
3600a 52 65 61 64 65 72 52 65 73 65 74 28 4c 65 61 76  ReaderReset(Leav
3600b 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  esReader *pReade
3600c 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  r){.  return sql
3600d 69 74 65 33 5f 72 65 73 65 74 28 70 52 65 61 64  ite3_reset(pRead
3600e 65 72 2d 3e 70 53 74 6d 74 29 3b 0a 7d 0a 0a 73  er->pStmt);.}..s
3600f 74 61 74 69 63 20 76 6f 69 64 20 6c 65 61 76 65  tatic void leave
36010 73 52 65 61 64 65 72 44 65 73 74 72 6f 79 28 4c  sReaderDestroy(L
36011 65 61 76 65 73 52 65 61 64 65 72 20 2a 70 52 65  eavesReader *pRe
36012 61 64 65 72 29 7b 0a 20 20 2f 2a 20 49 66 20 69  ader){.  /* If i
36013 64 78 20 69 73 20 2d 31 2c 20 74 68 61 74 20 6d  dx is -1, that m
36014 65 61 6e 73 20 77 65 27 72 65 20 75 73 69 6e 67  eans we're using
36015 20 61 20 6e 6f 6e 2d 63 61 63 68 65 64 20 73 74   a non-cached st
36016 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 68 61 6e  atement.  ** han
36017 64 6c 65 20 69 6e 20 74 68 65 20 6f 70 74 69 6d  dle in the optim
36018 69 7a 65 28 29 20 63 61 73 65 2c 20 73 6f 20 77  ize() case, so w
36019 65 20 6e 65 65 64 20 74 6f 20 72 65 6c 65 61 73  e need to releas
3601a 65 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  e it..  */.  if(
3601b 20 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74 21   pReader->pStmt!
3601c 3d 4e 55 4c 4c 20 26 26 20 70 52 65 61 64 65 72  =NULL && pReader
3601d 2d 3e 69 64 78 3d 3d 2d 31 20 29 7b 0a 20 20 20  ->idx==-1 ){.   
3601e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
3601f 65 28 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74  e(pReader->pStmt
36020 29 3b 0a 20 20 7d 0a 20 20 6c 65 61 66 52 65 61  );.  }.  leafRea
36021 64 65 72 44 65 73 74 72 6f 79 28 26 70 52 65 61  derDestroy(&pRea
36022 64 65 72 2d 3e 6c 65 61 66 52 65 61 64 65 72 29  der->leafReader)
36023 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65  ;.  dataBufferDe
36024 73 74 72 6f 79 28 26 70 52 65 61 64 65 72 2d 3e  stroy(&pReader->
36025 72 6f 6f 74 44 61 74 61 29 3b 0a 20 20 53 43 52  rootData);.  SCR
36026 41 4d 42 4c 45 28 70 52 65 61 64 65 72 29 3b 0a  AMBLE(pReader);.
36027 7d 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65  }../* Initialize
36028 20 70 52 65 61 64 65 72 20 77 69 74 68 20 74 68   pReader with th
36029 65 20 67 69 76 65 6e 20 72 6f 6f 74 20 64 61 74  e given root dat
3602a 61 20 28 69 66 20 69 53 74 61 72 74 42 6c 6f 63  a (if iStartBloc
3602b 6b 69 64 3d 3d 30 0a 2a 2a 20 74 68 65 20 6c 65  kid==0.** the le
3602c 61 66 20 64 61 74 61 20 77 61 73 20 65 6e 74 69  af data was enti
3602d 72 65 6c 79 20 63 6f 6e 74 61 69 6e 65 64 20 69  rely contained i
3602e 6e 20 74 68 65 20 72 6f 6f 74 29 2c 20 6f 72 20  n the root), or 
3602f 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 73 74 72 65  from the.** stre
36030 61 6d 20 6f 66 20 62 6c 6f 63 6b 73 20 62 65 74  am of blocks bet
36031 77 65 65 6e 20 69 53 74 61 72 74 42 6c 6f 63 6b  ween iStartBlock
36032 69 64 20 61 6e 64 20 69 45 6e 64 42 6c 6f 63 6b  id and iEndBlock
36033 69 64 2c 20 69 6e 63 6c 75 73 69 76 65 2e 0a 2a  id, inclusive..*
36034 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61  /.static int lea
36035 76 65 73 52 65 61 64 65 72 49 6e 69 74 28 66 75  vesReaderInit(fu
36036 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 0a  lltext_vtab *v,.
36037 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36038 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20              int 
36039 69 64 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20  idx,.           
3603a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3603b 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 53   sqlite_int64 iS
3603c 74 61 72 74 42 6c 6f 63 6b 69 64 2c 0a 20 20 20  tartBlockid,.   
3603d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3603e 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f           sqlite_
3603f 69 6e 74 36 34 20 69 45 6e 64 42 6c 6f 63 6b 69  int64 iEndBlocki
36040 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  d,.             
36041 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
36042 6f 6e 73 74 20 63 68 61 72 20 2a 70 52 6f 6f 74  onst char *pRoot
36043 44 61 74 61 2c 20 69 6e 74 20 6e 52 6f 6f 74 44  Data, int nRootD
36044 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ata,.           
36045 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36046 20 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 70   LeavesReader *p
36047 52 65 61 64 65 72 29 7b 0a 20 20 43 4c 45 41 52  Reader){.  CLEAR
36048 28 70 52 65 61 64 65 72 29 3b 0a 20 20 70 52 65  (pReader);.  pRe
36049 61 64 65 72 2d 3e 69 64 78 20 3d 20 69 64 78 3b  ader->idx = idx;
3604a 0a 0a 20 20 64 61 74 61 42 75 66 66 65 72 49 6e  ..  dataBufferIn
3604b 69 74 28 26 70 52 65 61 64 65 72 2d 3e 72 6f 6f  it(&pReader->roo
3604c 74 44 61 74 61 2c 20 30 29 3b 0a 20 20 69 66 28  tData, 0);.  if(
3604d 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 3d 3d   iStartBlockid==
3604e 30 20 29 7b 0a 20 20 20 20 2f 2a 20 45 6e 74 69  0 ){.    /* Enti
3604f 72 65 20 6c 65 61 66 20 6c 65 76 65 6c 20 66 69  re leaf level fi
36050 74 20 69 6e 20 72 6f 6f 74 20 64 61 74 61 2e 20  t in root data. 
36051 2a 2f 0a 20 20 20 20 64 61 74 61 42 75 66 66 65  */.    dataBuffe
36052 72 52 65 70 6c 61 63 65 28 26 70 52 65 61 64 65  rReplace(&pReade
36053 72 2d 3e 72 6f 6f 74 44 61 74 61 2c 20 70 52 6f  r->rootData, pRo
36054 6f 74 44 61 74 61 2c 20 6e 52 6f 6f 74 44 61 74  otData, nRootDat
36055 61 29 3b 0a 20 20 20 20 6c 65 61 66 52 65 61 64  a);.    leafRead
36056 65 72 49 6e 69 74 28 70 52 65 61 64 65 72 2d 3e  erInit(pReader->
36057 72 6f 6f 74 44 61 74 61 2e 70 44 61 74 61 2c 20  rootData.pData, 
36058 70 52 65 61 64 65 72 2d 3e 72 6f 6f 74 44 61 74  pReader->rootDat
36059 61 2e 6e 44 61 74 61 2c 0a 20 20 20 20 20 20 20  a.nData,.       
3605a 20 20 20 20 20 20 20 20 20 20 20 20 26 70 52 65              &pRe
3605b 61 64 65 72 2d 3e 6c 65 61 66 52 65 61 64 65 72  ader->leafReader
3605c 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
3605d 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b  sqlite3_stmt *s;
3605e 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71  .    int rc = sq
3605f 6c 5f 67 65 74 5f 6c 65 61 66 5f 73 74 61 74 65  l_get_leaf_state
36060 6d 65 6e 74 28 76 2c 20 69 64 78 2c 20 26 73 29  ment(v, idx, &s)
36061 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
36062 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
36063 20 72 63 3b 0a 0a 20 20 20 20 72 63 20 3d 20 73   rc;..    rc = s
36064 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36  qlite3_bind_int6
36065 34 28 73 2c 20 31 2c 20 69 53 74 61 72 74 42 6c  4(s, 1, iStartBl
36066 6f 63 6b 69 64 29 3b 0a 20 20 20 20 69 66 28 20  ockid);.    if( 
36067 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
36068 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20  return rc;..    
36069 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e  rc = sqlite3_bin
3606a 64 5f 69 6e 74 36 34 28 73 2c 20 32 2c 20 69 45  d_int64(s, 2, iE
3606b 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 20 20  ndBlockid);.    
3606c 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
3606d 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
3606e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
3606f 5f 73 74 65 70 28 73 29 3b 0a 20 20 20 20 69 66  _step(s);.    if
36070 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc==SQLITE_DON
36071 45 20 29 7b 0a 20 20 20 20 20 20 70 52 65 61 64  E ){.      pRead
36072 65 72 2d 3e 65 6f 66 20 3d 20 31 3b 0a 20 20 20  er->eof = 1;.   
36073 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
36074 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  _OK;.    }.    i
36075 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f  f( rc!=SQLITE_RO
36076 57 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  W ) return rc;..
36077 20 20 20 20 70 52 65 61 64 65 72 2d 3e 70 53 74      pReader->pSt
36078 6d 74 20 3d 20 73 3b 0a 20 20 20 20 6c 65 61 66  mt = s;.    leaf
36079 52 65 61 64 65 72 49 6e 69 74 28 73 71 6c 69 74  ReaderInit(sqlit
3607a 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 70  e3_column_blob(p
3607b 52 65 61 64 65 72 2d 3e 70 53 74 6d 74 2c 20 30  Reader->pStmt, 0
3607c 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ),.             
3607d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f        sqlite3_co
3607e 6c 75 6d 6e 5f 62 79 74 65 73 28 70 52 65 61 64  lumn_bytes(pRead
3607f 65 72 2d 3e 70 53 74 6d 74 2c 20 30 29 2c 0a 20  er->pStmt, 0),. 
36080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36081 20 20 26 70 52 65 61 64 65 72 2d 3e 6c 65 61 66    &pReader->leaf
36082 52 65 61 64 65 72 29 3b 0a 20 20 7d 0a 20 20 72  Reader);.  }.  r
36083 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
36084 0a 7d 0a 0a 2f 2a 20 53 74 65 70 20 74 68 65 20  .}../* Step the 
36085 63 75 72 72 65 6e 74 20 6c 65 61 66 20 66 6f 72  current leaf for
36086 77 61 72 64 20 74 6f 20 74 68 65 20 6e 65 78 74  ward to the next
36087 20 74 65 72 6d 2e 20 20 49 66 20 77 65 20 72 65   term.  If we re
36088 61 63 68 20 74 68 65 0a 2a 2a 20 65 6e 64 20 6f  ach the.** end o
36089 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 65  f the current le
3608a 61 66 2c 20 73 74 65 70 20 66 6f 72 77 61 72 64  af, step forward
3608b 20 74 6f 20 74 68 65 20 6e 65 78 74 20 6c 65 61   to the next lea
3608c 66 20 62 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74  f block..*/.stat
3608d 69 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65 61  ic int leavesRea
3608e 64 65 72 53 74 65 70 28 66 75 6c 6c 74 65 78 74  derStep(fulltext
3608f 5f 76 74 61 62 20 2a 76 2c 20 4c 65 61 76 65 73  _vtab *v, Leaves
36090 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
36091 7b 0a 20 20 61 73 73 65 72 74 28 20 21 6c 65 61  {.  assert( !lea
36092 76 65 73 52 65 61 64 65 72 41 74 45 6e 64 28 70  vesReaderAtEnd(p
36093 52 65 61 64 65 72 29 20 29 3b 0a 20 20 6c 65 61  Reader) );.  lea
36094 66 52 65 61 64 65 72 53 74 65 70 28 26 70 52 65  fReaderStep(&pRe
36095 61 64 65 72 2d 3e 6c 65 61 66 52 65 61 64 65 72  ader->leafReader
36096 29 3b 0a 0a 20 20 69 66 28 20 6c 65 61 66 52 65  );..  if( leafRe
36097 61 64 65 72 41 74 45 6e 64 28 26 70 52 65 61 64  aderAtEnd(&pRead
36098 65 72 2d 3e 6c 65 61 66 52 65 61 64 65 72 29 20  er->leafReader) 
36099 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20  ){.    int rc;. 
3609a 20 20 20 69 66 28 20 70 52 65 61 64 65 72 2d 3e     if( pReader->
3609b 72 6f 6f 74 44 61 74 61 2e 70 44 61 74 61 20 29  rootData.pData )
3609c 7b 0a 20 20 20 20 20 20 70 52 65 61 64 65 72 2d  {.      pReader-
3609d 3e 65 6f 66 20 3d 20 31 3b 0a 20 20 20 20 20 20  >eof = 1;.      
3609e 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
3609f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d  ;.    }.    rc =
360a0 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52   sqlite3_step(pR
360a1 65 61 64 65 72 2d 3e 70 53 74 6d 74 29 3b 0a 20  eader->pStmt);. 
360a2 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
360a3 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 70  E_ROW ){.      p
360a4 52 65 61 64 65 72 2d 3e 65 6f 66 20 3d 20 31 3b  Reader->eof = 1;
360a5 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
360a6 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 3f 20  ==SQLITE_DONE ? 
360a7 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 72 63 3b 0a  SQLITE_OK : rc;.
360a8 20 20 20 20 7d 0a 20 20 20 20 6c 65 61 66 52 65      }.    leafRe
360a9 61 64 65 72 44 65 73 74 72 6f 79 28 26 70 52 65  aderDestroy(&pRe
360aa 61 64 65 72 2d 3e 6c 65 61 66 52 65 61 64 65 72  ader->leafReader
360ab 29 3b 0a 20 20 20 20 6c 65 61 66 52 65 61 64 65  );.    leafReade
360ac 72 49 6e 69 74 28 73 71 6c 69 74 65 33 5f 63 6f  rInit(sqlite3_co
360ad 6c 75 6d 6e 5f 62 6c 6f 62 28 70 52 65 61 64 65  lumn_blob(pReade
360ae 72 2d 3e 70 53 74 6d 74 2c 20 30 29 2c 0a 20 20  r->pStmt, 0),.  
360af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
360b0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
360b1 62 79 74 65 73 28 70 52 65 61 64 65 72 2d 3e 70  bytes(pReader->p
360b2 53 74 6d 74 2c 20 30 29 2c 0a 20 20 20 20 20 20  Stmt, 0),.      
360b3 20 20 20 20 20 20 20 20 20 20 20 20 20 26 70 52               &pR
360b4 65 61 64 65 72 2d 3e 6c 65 61 66 52 65 61 64 65  eader->leafReade
360b5 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  r);.  }.  return
360b6 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
360b7 2a 20 4f 72 64 65 72 20 4c 65 61 76 65 73 52 65  * Order LeavesRe
360b8 61 64 65 72 73 20 62 79 20 74 68 65 69 72 20 74  aders by their t
360b9 65 72 6d 2c 20 69 67 6e 6f 72 69 6e 67 20 69 64  erm, ignoring id
360ba 78 2e 20 20 52 65 61 64 65 72 73 20 61 74 20 65  x.  Readers at e
360bb 6f 66 0a 2a 2a 20 61 6c 77 61 79 73 20 73 6f 72  of.** always sor
360bc 74 20 74 6f 20 74 68 65 20 65 6e 64 2e 0a 2a 2f  t to the end..*/
360bd 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 76  .static int leav
360be 65 73 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28  esReaderTermCmp(
360bf 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 6c 72  LeavesReader *lr
360c0 31 2c 20 4c 65 61 76 65 73 52 65 61 64 65 72 20  1, LeavesReader 
360c1 2a 6c 72 32 29 7b 0a 20 20 69 66 28 20 6c 65 61  *lr2){.  if( lea
360c2 76 65 73 52 65 61 64 65 72 41 74 45 6e 64 28 6c  vesReaderAtEnd(l
360c3 72 31 29 20 29 7b 0a 20 20 20 20 69 66 28 20 6c  r1) ){.    if( l
360c4 65 61 76 65 73 52 65 61 64 65 72 41 74 45 6e 64  eavesReaderAtEnd
360c5 28 6c 72 32 29 20 29 20 72 65 74 75 72 6e 20 30  (lr2) ) return 0
360c6 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  ;.    return 1;.
360c7 20 20 7d 0a 20 20 69 66 28 20 6c 65 61 76 65 73    }.  if( leaves
360c8 52 65 61 64 65 72 41 74 45 6e 64 28 6c 72 32 29  ReaderAtEnd(lr2)
360c9 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 0a 20   ) return -1;.. 
360ca 20 72 65 74 75 72 6e 20 6c 65 61 66 52 65 61 64   return leafRead
360cb 65 72 54 65 72 6d 43 6d 70 28 26 6c 72 31 2d 3e  erTermCmp(&lr1->
360cc 6c 65 61 66 52 65 61 64 65 72 2c 0a 20 20 20 20  leafReader,.    
360cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
360ce 20 20 20 20 20 20 20 6c 65 61 76 65 73 52 65 61         leavesRea
360cf 64 65 72 54 65 72 6d 28 6c 72 32 29 2c 20 6c 65  derTerm(lr2), le
360d0 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 42 79  avesReaderTermBy
360d1 74 65 73 28 6c 72 32 29 2c 0a 20 20 20 20 20 20  tes(lr2),.      
360d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
360d3 20 20 20 20 20 30 29 3b 0a 7d 0a 0a 2f 2a 20 53       0);.}../* S
360d4 69 6d 69 6c 61 72 20 74 6f 20 6c 65 61 76 65 73  imilar to leaves
360d5 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28 29 2c  ReaderTermCmp(),
360d6 20 77 69 74 68 20 61 64 64 69 74 69 6f 6e 61 6c   with additional
360d7 20 6f 72 64 65 72 69 6e 67 20 62 79 20 69 64 78   ordering by idx
360d8 0a 2a 2a 20 73 6f 20 74 68 61 74 20 6f 6c 64 65  .** so that olde
360d9 72 20 73 65 67 6d 65 6e 74 73 20 73 6f 72 74 20  r segments sort 
360da 62 65 66 6f 72 65 20 6e 65 77 65 72 20 73 65 67  before newer seg
360db 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  ments..*/.static
360dc 20 69 6e 74 20 6c 65 61 76 65 73 52 65 61 64 65   int leavesReade
360dd 72 43 6d 70 28 4c 65 61 76 65 73 52 65 61 64 65  rCmp(LeavesReade
360de 72 20 2a 6c 72 31 2c 20 4c 65 61 76 65 73 52 65  r *lr1, LeavesRe
360df 61 64 65 72 20 2a 6c 72 32 29 7b 0a 20 20 69 6e  ader *lr2){.  in
360e0 74 20 63 20 3d 20 6c 65 61 76 65 73 52 65 61 64  t c = leavesRead
360e1 65 72 54 65 72 6d 43 6d 70 28 6c 72 31 2c 20 6c  erTermCmp(lr1, l
360e2 72 32 29 3b 0a 20 20 69 66 28 20 63 21 3d 30 20  r2);.  if( c!=0 
360e3 29 20 72 65 74 75 72 6e 20 63 3b 0a 20 20 72 65  ) return c;.  re
360e4 74 75 72 6e 20 6c 72 31 2d 3e 69 64 78 2d 6c 72  turn lr1->idx-lr
360e5 32 2d 3e 69 64 78 3b 0a 7d 0a 0a 2f 2a 20 41 73  2->idx;.}../* As
360e6 73 75 6d 65 20 74 68 61 74 20 70 4c 72 5b 31 5d  sume that pLr[1]
360e7 2e 2e 70 4c 72 5b 6e 4c 72 5d 20 61 72 65 20 73  ..pLr[nLr] are s
360e8 6f 72 74 65 64 2e 20 20 42 75 62 62 6c 65 20 70  orted.  Bubble p
360e9 4c 72 5b 30 5d 20 69 6e 74 6f 20 69 74 73 0a 2a  Lr[0] into its.*
360ea 2a 20 73 6f 72 74 65 64 20 70 6f 73 69 74 69 6f  * sorted positio
360eb 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  n..*/.static voi
360ec 64 20 6c 65 61 76 65 73 52 65 61 64 65 72 52 65  d leavesReaderRe
360ed 6f 72 64 65 72 28 4c 65 61 76 65 73 52 65 61 64  order(LeavesRead
360ee 65 72 20 2a 70 4c 72 2c 20 69 6e 74 20 6e 4c 72  er *pLr, int nLr
360ef 29 7b 0a 20 20 77 68 69 6c 65 28 20 6e 4c 72 3e  ){.  while( nLr>
360f0 31 20 26 26 20 6c 65 61 76 65 73 52 65 61 64 65  1 && leavesReade
360f1 72 43 6d 70 28 70 4c 72 2c 20 70 4c 72 2b 31 29  rCmp(pLr, pLr+1)
360f2 3e 30 20 29 7b 0a 20 20 20 20 4c 65 61 76 65 73  >0 ){.    Leaves
360f3 52 65 61 64 65 72 20 74 6d 70 20 3d 20 70 4c 72  Reader tmp = pLr
360f4 5b 30 5d 3b 0a 20 20 20 20 70 4c 72 5b 30 5d 20  [0];.    pLr[0] 
360f5 3d 20 70 4c 72 5b 31 5d 3b 0a 20 20 20 20 70 4c  = pLr[1];.    pL
360f6 72 5b 31 5d 20 3d 20 74 6d 70 3b 0a 20 20 20 20  r[1] = tmp;.    
360f7 6e 4c 72 2d 2d 3b 0a 20 20 20 20 70 4c 72 2b 2b  nLr--;.    pLr++
360f8 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 49 6e 69 74  ;.  }.}../* Init
360f9 69 61 6c 69 7a 65 73 20 70 52 65 61 64 65 72 73  ializes pReaders
360fa 20 77 69 74 68 20 74 68 65 20 73 65 67 6d 65 6e   with the segmen
360fb 74 73 20 66 72 6f 6d 20 6c 65 76 65 6c 20 69 4c  ts from level iL
360fc 65 76 65 6c 2c 20 72 65 74 75 72 6e 69 6e 67 0a  evel, returning.
360fd 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ** the number of
360fe 20 73 65 67 6d 65 6e 74 73 20 69 6e 20 2a 70 69   segments in *pi
360ff 52 65 61 64 65 72 73 2e 20 20 4c 65 61 76 65 73  Readers.  Leaves
36100 20 70 52 65 61 64 65 72 73 20 69 6e 20 73 6f 72   pReaders in sor
36101 74 65 64 0a 2a 2a 20 6f 72 64 65 72 2e 0a 2a 2f  ted.** order..*/
36102 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 76  .static int leav
36103 65 73 52 65 61 64 65 72 73 49 6e 69 74 28 66 75  esReadersInit(fu
36104 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
36105 69 6e 74 20 69 4c 65 76 65 6c 2c 0a 20 20 20 20  int iLevel,.    
36106 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36107 20 20 20 20 20 20 20 20 20 4c 65 61 76 65 73 52           LeavesR
36108 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 73 2c  eader *pReaders,
36109 20 69 6e 74 20 2a 70 69 52 65 61 64 65 72 73 29   int *piReaders)
3610a 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  {.  sqlite3_stmt
3610b 20 2a 73 3b 0a 20 20 69 6e 74 20 69 2c 20 72 63   *s;.  int i, rc
3610c 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65   = sql_get_state
3610d 6d 65 6e 74 28 76 2c 20 53 45 47 44 49 52 5f 53  ment(v, SEGDIR_S
3610e 45 4c 45 43 54 5f 4c 45 56 45 4c 5f 53 54 4d 54  ELECT_LEVEL_STMT
3610f 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21  , &s);.  if( rc!
36110 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
36111 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20  urn rc;..  rc = 
36112 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
36113 28 73 2c 20 31 2c 20 69 4c 65 76 65 6c 29 3b 0a  (s, 1, iLevel);.
36114 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
36115 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
36116 0a 0a 20 20 69 20 3d 20 30 3b 0a 20 20 77 68 69  ..  i = 0;.  whi
36117 6c 65 28 20 28 72 63 20 3d 20 73 71 6c 69 74 65  le( (rc = sqlite
36118 33 5f 73 74 65 70 28 73 29 29 3d 3d 53 51 4c 49  3_step(s))==SQLI
36119 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 73 71  TE_ROW ){.    sq
3611a 6c 69 74 65 5f 69 6e 74 36 34 20 69 53 74 61 72  lite_int64 iStar
3611b 74 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  t = sqlite3_colu
3611c 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 30 29 3b 0a  mn_int64(s, 0);.
3611d 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34      sqlite_int64
3611e 20 69 45 6e 64 20 3d 20 73 71 6c 69 74 65 33 5f   iEnd = sqlite3_
3611f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 2c 20  column_int64(s, 
36120 31 29 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  1);.    const ch
36121 61 72 20 2a 70 52 6f 6f 74 44 61 74 61 20 3d 20  ar *pRootData = 
36122 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
36123 6c 6f 62 28 73 2c 20 32 29 3b 0a 20 20 20 20 69  lob(s, 2);.    i
36124 6e 74 20 6e 52 6f 6f 74 44 61 74 61 20 3d 20 73  nt nRootData = s
36125 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
36126 74 65 73 28 73 2c 20 32 29 3b 0a 0a 20 20 20 20  tes(s, 2);..    
36127 61 73 73 65 72 74 28 20 69 3c 4d 45 52 47 45 5f  assert( i<MERGE_
36128 43 4f 55 4e 54 20 29 3b 0a 20 20 20 20 72 63 20  COUNT );.    rc 
36129 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 49 6e  = leavesReaderIn
3612a 69 74 28 76 2c 20 69 2c 20 69 53 74 61 72 74 2c  it(v, i, iStart,
3612b 20 69 45 6e 64 2c 20 70 52 6f 6f 74 44 61 74 61   iEnd, pRootData
3612c 2c 20 6e 52 6f 6f 74 44 61 74 61 2c 0a 20 20 20  , nRootData,.   
3612d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3612e 20 20 20 20 20 20 20 26 70 52 65 61 64 65 72 73         &pReaders
3612f 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 63  [i]);.    if( rc
36130 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 62 72  !=SQLITE_OK ) br
36131 65 61 6b 3b 0a 0a 20 20 20 20 69 2b 2b 3b 0a 20  eak;..    i++;. 
36132 20 7d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c   }.  if( rc!=SQL
36133 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20  ITE_DONE ){.    
36134 77 68 69 6c 65 28 20 69 2d 2d 3e 30 20 29 7b 0a  while( i-->0 ){.
36135 20 20 20 20 20 20 6c 65 61 76 65 73 52 65 61 64        leavesRead
36136 65 72 44 65 73 74 72 6f 79 28 26 70 52 65 61 64  erDestroy(&pRead
36137 65 72 73 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20  ers[i]);.    }. 
36138 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
36139 7d 0a 0a 20 20 2a 70 69 52 65 61 64 65 72 73 20  }..  *piReaders 
3613a 3d 20 69 3b 0a 0a 20 20 2f 2a 20 4c 65 61 76 65  = i;..  /* Leave
3613b 20 6f 75 72 20 72 65 73 75 6c 74 73 20 73 6f 72   our results sor
3613c 74 65 64 20 62 79 20 74 65 72 6d 2c 20 74 68 65  ted by term, the
3613d 6e 20 61 67 65 2e 20 2a 2f 0a 20 20 77 68 69 6c  n age. */.  whil
3613e 65 28 20 69 2d 2d 20 29 7b 0a 20 20 20 20 6c 65  e( i-- ){.    le
3613f 61 76 65 73 52 65 61 64 65 72 52 65 6f 72 64 65  avesReaderReorde
36140 72 28 70 52 65 61 64 65 72 73 2b 69 2c 20 2a 70  r(pReaders+i, *p
36141 69 52 65 61 64 65 72 73 2d 69 29 3b 0a 20 20 7d  iReaders-i);.  }
36142 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
36143 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 4d 65 72 67 65  _OK;.}../* Merge
36144 20 64 6f 63 6c 69 73 74 73 20 66 72 6f 6d 20 70   doclists from p
36145 52 65 61 64 65 72 73 5b 6e 52 65 61 64 65 72 73  Readers[nReaders
36146 5d 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20  ] into a single 
36147 64 6f 63 6c 69 73 74 2c 20 77 68 69 63 68 0a 2a  doclist, which.*
36148 2a 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20  * is written to 
36149 70 57 72 69 74 65 72 2e 20 20 41 73 73 75 6d 65  pWriter.  Assume
3614a 73 20 70 52 65 61 64 65 72 73 20 69 73 20 6f 72  s pReaders is or
3614b 64 65 72 65 64 20 6f 6c 64 65 73 74 20 74 6f 0a  dered oldest to.
3614c 2a 2a 20 6e 65 77 65 73 74 2e 0a 2a 2f 0a 2f 2a  ** newest..*/./*
3614d 20 54 4f 44 4f 28 73 68 65 73 73 29 20 43 6f 6e   TODO(shess) Con
3614e 73 69 64 65 72 20 70 75 74 74 69 6e 67 20 74 68  sider putting th
3614f 69 73 20 69 6e 6c 69 6e 65 20 69 6e 20 73 65 67  is inline in seg
36150 6d 65 6e 74 4d 65 72 67 65 28 29 2e 20 2a 2f 0a  mentMerge(). */.
36151 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 76 65  static int leave
36152 73 52 65 61 64 65 72 73 4d 65 72 67 65 28 66 75  sReadersMerge(fu
36153 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 0a  lltext_vtab *v,.
36154 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36155 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4c 65                Le
36156 61 76 65 73 52 65 61 64 65 72 20 2a 70 52 65 61  avesReader *pRea
36157 64 65 72 73 2c 20 69 6e 74 20 6e 52 65 61 64 65  ders, int nReade
36158 72 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  rs,.            
36159 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3615a 20 20 4c 65 61 66 57 72 69 74 65 72 20 2a 70 57    LeafWriter *pW
3615b 72 69 74 65 72 29 7b 0a 20 20 44 4c 52 65 61 64  riter){.  DLRead
3615c 65 72 20 64 6c 52 65 61 64 65 72 73 5b 4d 45 52  er dlReaders[MER
3615d 47 45 5f 43 4f 55 4e 54 5d 3b 0a 20 20 63 6f 6e  GE_COUNT];.  con
3615e 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d 20 3d  st char *pTerm =
3615f 20 6c 65 61 76 65 73 52 65 61 64 65 72 54 65 72   leavesReaderTer
36160 6d 28 70 52 65 61 64 65 72 73 29 3b 0a 20 20 69  m(pReaders);.  i
36161 6e 74 20 69 2c 20 6e 54 65 72 6d 20 3d 20 6c 65  nt i, nTerm = le
36162 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 42 79  avesReaderTermBy
36163 74 65 73 28 70 52 65 61 64 65 72 73 29 3b 0a 0a  tes(pReaders);..
36164 20 20 61 73 73 65 72 74 28 20 6e 52 65 61 64 65    assert( nReade
36165 72 73 3c 3d 4d 45 52 47 45 5f 43 4f 55 4e 54 20  rs<=MERGE_COUNT 
36166 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  );..  for(i=0; i
36167 3c 6e 52 65 61 64 65 72 73 3b 20 69 2b 2b 29 7b  <nReaders; i++){
36168 0a 20 20 20 20 64 6c 72 49 6e 69 74 28 26 64 6c  .    dlrInit(&dl
36169 52 65 61 64 65 72 73 5b 69 5d 2c 20 44 4c 5f 44  Readers[i], DL_D
3616a 45 46 41 55 4c 54 2c 0a 20 20 20 20 20 20 20 20  EFAULT,.        
3616b 20 20 20 20 6c 65 61 76 65 73 52 65 61 64 65 72      leavesReader
3616c 44 61 74 61 28 70 52 65 61 64 65 72 73 2b 69 29  Data(pReaders+i)
3616d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 65  ,.            le
3616e 61 76 65 73 52 65 61 64 65 72 44 61 74 61 42 79  avesReaderDataBy
3616f 74 65 73 28 70 52 65 61 64 65 72 73 2b 69 29 29  tes(pReaders+i))
36170 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
36171 6c 65 61 66 57 72 69 74 65 72 53 74 65 70 4d 65  leafWriterStepMe
36172 72 67 65 28 76 2c 20 70 57 72 69 74 65 72 2c 20  rge(v, pWriter, 
36173 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 64 6c  pTerm, nTerm, dl
36174 52 65 61 64 65 72 73 2c 20 6e 52 65 61 64 65 72  Readers, nReader
36175 73 29 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72  s);.}../* Forwar
36176 64 20 72 65 66 20 64 75 65 20 74 6f 20 6d 75 74  d ref due to mut
36177 75 61 6c 20 72 65 63 75 72 73 69 6f 6e 20 77 69  ual recursion wi
36178 74 68 20 73 65 67 64 69 72 4e 65 78 74 49 6e 64  th segdirNextInd
36179 65 78 28 29 2e 20 2a 2f 0a 73 74 61 74 69 63 20  ex(). */.static 
3617a 69 6e 74 20 73 65 67 6d 65 6e 74 4d 65 72 67 65  int segmentMerge
3617b 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
3617c 76 2c 20 69 6e 74 20 69 4c 65 76 65 6c 29 3b 0a  v, int iLevel);.
3617d 0a 2f 2a 20 50 75 74 20 74 68 65 20 6e 65 78 74  ./* Put the next
3617e 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 64 65 78   available index
3617f 20 61 74 20 69 4c 65 76 65 6c 20 69 6e 74 6f 20   at iLevel into 
36180 2a 70 69 64 78 2e 20 20 49 66 20 69 4c 65 76 65  *pidx.  If iLeve
36181 6c 0a 2a 2a 20 61 6c 72 65 61 64 79 20 68 61 73  l.** already has
36182 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 73 65 67   MERGE_COUNT seg
36183 6d 65 6e 74 73 2c 20 74 68 65 79 20 61 72 65 20  ments, they are 
36184 6d 65 72 67 65 64 20 74 6f 20 61 20 68 69 67 68  merged to a high
36185 65 72 0a 2a 2a 20 6c 65 76 65 6c 20 74 6f 20 6d  er.** level to m
36186 61 6b 65 20 72 6f 6f 6d 2e 0a 2a 2f 0a 73 74 61  ake room..*/.sta
36187 74 69 63 20 69 6e 74 20 73 65 67 64 69 72 4e 65  tic int segdirNe
36188 78 74 49 6e 64 65 78 28 66 75 6c 6c 74 65 78 74  xtIndex(fulltext
36189 5f 76 74 61 62 20 2a 76 2c 20 69 6e 74 20 69 4c  _vtab *v, int iL
3618a 65 76 65 6c 2c 20 69 6e 74 20 2a 70 69 64 78 29  evel, int *pidx)
3618b 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 65 67  {.  int rc = seg
3618c 64 69 72 5f 6d 61 78 5f 69 6e 64 65 78 28 76 2c  dir_max_index(v,
3618d 20 69 4c 65 76 65 6c 2c 20 70 69 64 78 29 3b 0a   iLevel, pidx);.
3618e 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
3618f 5f 44 4f 4e 45 20 29 7b 20 20 20 20 20 20 20 20  _DONE ){        
36190 20 20 20 20 20 20 2f 2a 20 4e 6f 20 73 65 67 6d        /* No segm
36191 65 6e 74 73 20 61 74 20 69 4c 65 76 65 6c 2e 20  ents at iLevel. 
36192 2a 2f 0a 20 20 20 20 2a 70 69 64 78 20 3d 20 30  */.    *pidx = 0
36193 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63  ;.  }else if( rc
36194 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a  ==SQLITE_ROW ){.
36195 20 20 20 20 69 66 28 20 2a 70 69 64 78 3d 3d 28      if( *pidx==(
36196 4d 45 52 47 45 5f 43 4f 55 4e 54 2d 31 29 20 29  MERGE_COUNT-1) )
36197 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 65 67  {.      rc = seg
36198 6d 65 6e 74 4d 65 72 67 65 28 76 2c 20 69 4c 65  mentMerge(v, iLe
36199 76 65 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20  vel);.      if( 
3619a 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
3619b 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
3619c 20 2a 70 69 64 78 20 3d 20 30 3b 0a 20 20 20 20   *pidx = 0;.    
3619d 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 2a 70  }else{.      (*p
3619e 69 64 78 29 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20  idx)++;.    }.  
3619f 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72  }else{.    retur
361a0 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 72 65 74 75  n rc;.  }.  retu
361a1 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
361a2 0a 2f 2a 20 4d 65 72 67 65 20 4d 45 52 47 45 5f  ./* Merge MERGE_
361a3 43 4f 55 4e 54 20 73 65 67 6d 65 6e 74 73 20 61  COUNT segments a
361a4 74 20 69 4c 65 76 65 6c 20 69 6e 74 6f 20 61 20  t iLevel into a 
361a5 6e 65 77 20 73 65 67 6d 65 6e 74 20 61 74 0a 2a  new segment at.*
361a6 2a 20 69 4c 65 76 65 6c 2b 31 2e 20 20 49 66 20  * iLevel+1.  If 
361a7 69 4c 65 76 65 6c 2b 31 20 69 73 20 61 6c 72 65  iLevel+1 is alre
361a8 61 64 79 20 66 75 6c 6c 20 6f 66 20 73 65 67 6d  ady full of segm
361a9 65 6e 74 73 2c 20 74 68 6f 73 65 20 77 69 6c 6c  ents, those will
361aa 20 62 65 0a 2a 2a 20 6d 65 72 67 65 64 20 74 6f   be.** merged to
361ab 20 6d 61 6b 65 20 72 6f 6f 6d 2e 0a 2a 2f 0a 73   make room..*/.s
361ac 74 61 74 69 63 20 69 6e 74 20 73 65 67 6d 65 6e  tatic int segmen
361ad 74 4d 65 72 67 65 28 66 75 6c 6c 74 65 78 74 5f  tMerge(fulltext_
361ae 76 74 61 62 20 2a 76 2c 20 69 6e 74 20 69 4c 65  vtab *v, int iLe
361af 76 65 6c 29 7b 0a 20 20 4c 65 61 66 57 72 69 74  vel){.  LeafWrit
361b0 65 72 20 77 72 69 74 65 72 3b 0a 20 20 4c 65 61  er writer;.  Lea
361b1 76 65 73 52 65 61 64 65 72 20 6c 72 73 5b 4d 45  vesReader lrs[ME
361b2 52 47 45 5f 43 4f 55 4e 54 5d 3b 0a 20 20 69 6e  RGE_COUNT];.  in
361b3 74 20 69 2c 20 72 63 2c 20 69 64 78 20 3d 20 30  t i, rc, idx = 0
361b4 3b 0a 0a 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e  ;..  /* Determin
361b5 65 20 74 68 65 20 6e 65 78 74 20 61 76 61 69 6c  e the next avail
361b6 61 62 6c 65 20 73 65 67 6d 65 6e 74 20 69 6e 64  able segment ind
361b7 65 78 20 61 74 20 74 68 65 20 6e 65 78 74 20 6c  ex at the next l
361b8 65 76 65 6c 2c 0a 20 20 2a 2a 20 6d 65 72 67 69  evel,.  ** mergi
361b9 6e 67 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e  ng as necessary.
361ba 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 65 67  .  */.  rc = seg
361bb 64 69 72 4e 65 78 74 49 6e 64 65 78 28 76 2c 20  dirNextIndex(v, 
361bc 69 4c 65 76 65 6c 2b 31 2c 20 26 69 64 78 29 3b  iLevel+1, &idx);
361bd 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
361be 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
361bf 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65  ;..  /* TODO(she
361c0 73 73 29 20 54 68 69 73 20 61 73 73 75 6d 65 73  ss) This assumes
361c1 20 74 68 61 74 20 77 65 27 6c 6c 20 61 6c 77 61   that we'll alwa
361c2 79 73 20 73 65 65 20 65 78 61 63 74 6c 79 0a 20  ys see exactly. 
361c3 20 2a 2a 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20   ** MERGE_COUNT 
361c4 73 65 67 6d 65 6e 74 73 20 74 6f 20 6d 65 72 67  segments to merg
361c5 65 20 61 74 20 61 20 67 69 76 65 6e 20 6c 65 76  e at a given lev
361c6 65 6c 2e 20 20 54 68 61 74 20 77 69 6c 6c 20 62  el.  That will b
361c7 65 0a 20 20 2a 2a 20 62 72 6f 6b 65 6e 20 69 66  e.  ** broken if
361c8 20 77 65 20 61 6c 6c 6f 77 20 74 68 65 20 64 65   we allow the de
361c9 76 65 6c 6f 70 65 72 20 74 6f 20 72 65 71 75 65  veloper to reque
361ca 73 74 20 70 72 65 65 6d 70 74 69 76 65 20 6f 72  st preemptive or
361cb 0a 20 20 2a 2a 20 64 65 66 65 72 72 65 64 20 6d  .  ** deferred m
361cc 65 72 67 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 6d  erging..  */.  m
361cd 65 6d 73 65 74 28 26 6c 72 73 2c 20 27 5c 30 27  emset(&lrs, '\0'
361ce 2c 20 73 69 7a 65 6f 66 28 6c 72 73 29 29 3b 0a  , sizeof(lrs));.
361cf 20 20 72 63 20 3d 20 6c 65 61 76 65 73 52 65 61    rc = leavesRea
361d0 64 65 72 73 49 6e 69 74 28 76 2c 20 69 4c 65 76  dersInit(v, iLev
361d1 65 6c 2c 20 6c 72 73 2c 20 26 69 29 3b 0a 20 20  el, lrs, &i);.  
361d2 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
361d3 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  K ) return rc;. 
361d4 20 61 73 73 65 72 74 28 20 69 3d 3d 4d 45 52 47   assert( i==MERG
361d5 45 5f 43 4f 55 4e 54 20 29 3b 0a 0a 20 20 6c 65  E_COUNT );..  le
361d6 61 66 57 72 69 74 65 72 49 6e 69 74 28 69 4c 65  afWriterInit(iLe
361d7 76 65 6c 2b 31 2c 20 69 64 78 2c 20 26 77 72 69  vel+1, idx, &wri
361d8 74 65 72 29 3b 0a 0a 20 20 2f 2a 20 53 69 6e 63  ter);..  /* Sinc
361d9 65 20 6c 65 61 76 65 73 52 65 61 64 65 72 52 65  e leavesReaderRe
361da 6f 72 64 65 72 28 29 20 70 75 73 68 65 73 20 72  order() pushes r
361db 65 61 64 65 72 73 20 61 74 20 65 6f 66 20 74 6f  eaders at eof to
361dc 20 74 68 65 20 65 6e 64 2c 0a 20 20 2a 2a 20 77   the end,.  ** w
361dd 68 65 6e 20 74 68 65 20 66 69 72 73 74 20 72 65  hen the first re
361de 61 64 65 72 20 69 73 20 65 6d 70 74 79 2c 20 61  ader is empty, a
361df 6c 6c 20 77 69 6c 6c 20 62 65 20 65 6d 70 74 79  ll will be empty
361e0 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20  ..  */.  while( 
361e1 21 6c 65 61 76 65 73 52 65 61 64 65 72 41 74 45  !leavesReaderAtE
361e2 6e 64 28 6c 72 73 29 20 29 7b 0a 20 20 20 20 2f  nd(lrs) ){.    /
361e3 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77  * Figure out how
361e4 20 6d 61 6e 79 20 72 65 61 64 65 72 73 20 73 68   many readers sh
361e5 61 72 65 20 74 68 65 69 72 20 6e 65 78 74 20 74  are their next t
361e6 65 72 6d 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28  erm. */.    for(
361e7 69 3d 31 3b 20 69 3c 4d 45 52 47 45 5f 43 4f 55  i=1; i<MERGE_COU
361e8 4e 54 20 26 26 20 21 6c 65 61 76 65 73 52 65 61  NT && !leavesRea
361e9 64 65 72 41 74 45 6e 64 28 6c 72 73 2b 69 29 3b  derAtEnd(lrs+i);
361ea 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   i++){.      if(
361eb 20 30 21 3d 6c 65 61 76 65 73 52 65 61 64 65 72   0!=leavesReader
361ec 54 65 72 6d 43 6d 70 28 6c 72 73 2c 20 6c 72 73  TermCmp(lrs, lrs
361ed 2b 69 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  +i) ) break;.   
361ee 20 7d 0a 0a 20 20 20 20 72 63 20 3d 20 6c 65 61   }..    rc = lea
361ef 76 65 73 52 65 61 64 65 72 73 4d 65 72 67 65 28  vesReadersMerge(
361f0 76 2c 20 6c 72 73 2c 20 69 2c 20 26 77 72 69 74  v, lrs, i, &writ
361f1 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  er);.    if( rc!
361f2 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
361f3 6f 20 65 72 72 3b 0a 0a 20 20 20 20 2f 2a 20 53  o err;..    /* S
361f4 74 65 70 20 66 6f 72 77 61 72 64 20 74 68 6f 73  tep forward thos
361f5 65 20 74 68 61 74 20 77 65 72 65 20 6d 65 72 67  e that were merg
361f6 65 64 2e 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65  ed. */.    while
361f7 28 20 69 2d 2d 3e 30 20 29 7b 0a 20 20 20 20 20  ( i-->0 ){.     
361f8 20 72 63 20 3d 20 6c 65 61 76 65 73 52 65 61 64   rc = leavesRead
361f9 65 72 53 74 65 70 28 76 2c 20 6c 72 73 2b 69 29  erStep(v, lrs+i)
361fa 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
361fb 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
361fc 20 65 72 72 3b 0a 0a 20 20 20 20 20 20 2f 2a 20   err;..      /* 
361fd 52 65 6f 72 64 65 72 20 62 79 20 74 65 72 6d 2c  Reorder by term,
361fe 20 74 68 65 6e 20 62 79 20 61 67 65 2e 20 2a 2f   then by age. */
361ff 0a 20 20 20 20 20 20 6c 65 61 76 65 73 52 65 61  .      leavesRea
36200 64 65 72 52 65 6f 72 64 65 72 28 6c 72 73 2b 69  derReorder(lrs+i
36201 2c 20 4d 45 52 47 45 5f 43 4f 55 4e 54 2d 69 29  , MERGE_COUNT-i)
36202 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 66  ;.    }.  }..  f
36203 6f 72 28 69 3d 30 3b 20 69 3c 4d 45 52 47 45 5f  or(i=0; i<MERGE_
36204 43 4f 55 4e 54 3b 20 69 2b 2b 29 7b 0a 20 20 20  COUNT; i++){.   
36205 20 6c 65 61 76 65 73 52 65 61 64 65 72 44 65 73   leavesReaderDes
36206 74 72 6f 79 28 26 6c 72 73 5b 69 5d 29 3b 0a 20  troy(&lrs[i]);. 
36207 20 7d 0a 0a 20 20 72 63 20 3d 20 6c 65 61 66 57   }..  rc = leafW
36208 72 69 74 65 72 46 69 6e 61 6c 69 7a 65 28 76 2c  riterFinalize(v,
36209 20 26 77 72 69 74 65 72 29 3b 0a 20 20 6c 65 61   &writer);.  lea
3620a 66 57 72 69 74 65 72 44 65 73 74 72 6f 79 28 26  fWriterDestroy(&
3620b 77 72 69 74 65 72 29 3b 0a 20 20 69 66 28 20 72  writer);.  if( r
3620c 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
3620d 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20  eturn rc;..  /* 
3620e 44 65 6c 65 74 65 20 74 68 65 20 6d 65 72 67 65  Delete the merge
3620f 64 20 73 65 67 6d 65 6e 74 20 64 61 74 61 2e 20  d segment data. 
36210 2a 2f 0a 20 20 72 65 74 75 72 6e 20 73 65 67 64  */.  return segd
36211 69 72 5f 64 65 6c 65 74 65 28 76 2c 20 69 4c 65  ir_delete(v, iLe
36212 76 65 6c 29 3b 0a 0a 20 65 72 72 3a 0a 20 20 66  vel);.. err:.  f
36213 6f 72 28 69 3d 30 3b 20 69 3c 4d 45 52 47 45 5f  or(i=0; i<MERGE_
36214 43 4f 55 4e 54 3b 20 69 2b 2b 29 7b 0a 20 20 20  COUNT; i++){.   
36215 20 6c 65 61 76 65 73 52 65 61 64 65 72 44 65 73   leavesReaderDes
36216 74 72 6f 79 28 26 6c 72 73 5b 69 5d 29 3b 0a 20  troy(&lrs[i]);. 
36217 20 7d 0a 20 20 6c 65 61 66 57 72 69 74 65 72 44   }.  leafWriterD
36218 65 73 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b  estroy(&writer);
36219 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
3621a 0a 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65 20 74  ./* Accumulate t
3621b 68 65 20 75 6e 69 6f 6e 20 6f 66 20 2a 61 63 63  he union of *acc
3621c 20 61 6e 64 20 2a 70 44 61 74 61 20 69 6e 74 6f   and *pData into
3621d 20 2a 61 63 63 2e 20 2a 2f 0a 73 74 61 74 69 63   *acc. */.static
3621e 20 76 6f 69 64 20 64 6f 63 4c 69 73 74 41 63 63   void docListAcc
3621f 75 6d 75 6c 61 74 65 55 6e 69 6f 6e 28 44 61 74  umulateUnion(Dat
36220 61 42 75 66 66 65 72 20 2a 61 63 63 2c 0a 20 20  aBuffer *acc,.  
36221 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36222 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36223 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61   const char *pDa
36224 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 29 20 7b  ta, int nData) {
36225 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 74 6d  .  DataBuffer tm
36226 70 20 3d 20 2a 61 63 63 3b 0a 20 20 64 61 74 61  p = *acc;.  data
36227 42 75 66 66 65 72 49 6e 69 74 28 61 63 63 2c 20  BufferInit(acc, 
36228 74 6d 70 2e 6e 44 61 74 61 2b 6e 44 61 74 61 29  tmp.nData+nData)
36229 3b 0a 20 20 64 6f 63 4c 69 73 74 55 6e 69 6f 6e  ;.  docListUnion
3622a 28 74 6d 70 2e 70 44 61 74 61 2c 20 74 6d 70 2e  (tmp.pData, tmp.
3622b 6e 44 61 74 61 2c 20 70 44 61 74 61 2c 20 6e 44  nData, pData, nD
3622c 61 74 61 2c 20 61 63 63 29 3b 0a 20 20 64 61 74  ata, acc);.  dat
3622d 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26  aBufferDestroy(&
3622e 74 6d 70 29 3b 0a 7d 0a 0a 2f 2a 20 54 4f 44 4f  tmp);.}../* TODO
3622f 28 73 68 65 73 73 29 20 49 74 20 6d 69 67 68 74  (shess) It might
36230 20 62 65 20 69 6e 74 65 72 65 73 74 69 6e 67 20   be interesting 
36231 74 6f 20 65 78 70 6c 6f 72 65 20 64 69 66 66 65  to explore diffe
36232 72 65 6e 74 20 6d 65 72 67 65 0a 2a 2a 20 73 74  rent merge.** st
36233 72 61 74 65 67 69 65 73 2c 20 68 65 72 65 2e 20  rategies, here. 
36234 20 46 6f 72 20 69 6e 73 74 61 6e 63 65 2c 20 73   For instance, s
36235 69 6e 63 65 20 74 68 69 73 20 69 73 20 61 20 73  ince this is a s
36236 6f 72 74 65 64 20 6d 65 72 67 65 2c 20 77 65 0a  orted merge, we.
36237 2a 2a 20 63 6f 75 6c 64 20 65 61 73 69 6c 79 20  ** could easily 
36238 6d 65 72 67 65 20 6d 61 6e 79 20 64 6f 63 6c 69  merge many docli
36239 73 74 73 20 69 6e 20 70 61 72 61 6c 6c 65 6c 2e  sts in parallel.
3623a 20 20 57 69 74 68 20 73 6f 6d 65 0a 2a 2a 20 63    With some.** c
3623b 6f 6d 70 72 65 68 65 6e 73 69 6f 6e 20 6f 66 20  omprehension of 
3623c 74 68 65 20 73 74 6f 72 61 67 65 20 66 6f 72 6d  the storage form
3623d 61 74 2c 20 77 65 20 63 6f 75 6c 64 20 6d 65 72  at, we could mer
3623e 67 65 20 61 6c 6c 20 6f 66 20 74 68 65 0a 2a 2a  ge all of the.**
3623f 20 64 6f 63 6c 69 73 74 73 20 77 69 74 68 69 6e   doclists within
36240 20 61 20 6c 65 61 66 20 6e 6f 64 65 20 64 69 72   a leaf node dir
36241 65 63 74 6c 79 20 66 72 6f 6d 20 74 68 65 20 6c  ectly from the l
36242 65 61 66 20 6e 6f 64 65 27 73 20 73 74 6f 72 61  eaf node's stora
36243 67 65 2e 0a 2a 2a 20 49 74 20 6d 61 79 20 62 65  ge..** It may be
36244 20 77 6f 72 74 68 77 68 69 6c 65 20 74 6f 20 6d   worthwhile to m
36245 65 72 67 65 20 73 6d 61 6c 6c 65 72 20 64 6f 63  erge smaller doc
36246 6c 69 73 74 73 20 62 65 66 6f 72 65 20 6c 61 72  lists before lar
36247 67 65 72 0a 2a 2a 20 64 6f 63 6c 69 73 74 73 2c  ger.** doclists,
36248 20 73 69 6e 63 65 20 74 68 65 79 20 63 61 6e 20   since they can 
36249 62 65 20 74 72 61 76 65 72 73 65 64 20 6d 6f 72  be traversed mor
3624a 65 20 71 75 69 63 6b 6c 79 20 2d 20 62 75 74 20  e quickly - but 
3624b 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 73 20 6d  the.** results m
3624c 61 79 20 68 61 76 65 20 6c 65 73 73 20 6f 76 65  ay have less ove
3624d 72 6c 61 70 2c 20 6d 61 6b 69 6e 67 20 74 68 65  rlap, making the
3624e 6d 20 6d 6f 72 65 20 65 78 70 65 6e 73 69 76 65  m more expensive
3624f 20 69 6e 20 61 0a 2a 2a 20 64 69 66 66 65 72 65   in a.** differe
36250 6e 74 20 77 61 79 2e 0a 2a 2f 0a 0a 2f 2a 20 53  nt way..*/../* S
36251 63 61 6e 20 70 52 65 61 64 65 72 20 66 6f 72 20  can pReader for 
36252 70 54 65 72 6d 2f 6e 54 65 72 6d 2c 20 61 6e 64  pTerm/nTerm, and
36253 20 6d 65 72 67 65 20 74 68 65 20 74 65 72 6d 27   merge the term'
36254 73 20 64 6f 63 6c 69 73 74 20 6f 76 65 72 0a 2a  s doclist over.*
36255 2a 20 2a 6f 75 74 20 28 61 6e 79 20 64 6f 63 6c  * *out (any docl
36256 69 73 74 73 20 77 69 74 68 20 64 75 70 6c 69 63  ists with duplic
36257 61 74 65 20 64 6f 63 69 64 73 20 6f 76 65 72 77  ate docids overw
36258 72 69 74 65 20 74 68 6f 73 65 20 69 6e 20 2a 6f  rite those in *o
36259 75 74 29 2e 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c  ut)..** Internal
3625a 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 6c 6f   function for lo
3625b 61 64 53 65 67 6d 65 6e 74 4c 65 61 66 28 29 2e  adSegmentLeaf().
3625c 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c  .*/.static int l
3625d 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61 76 65 73  oadSegmentLeaves
3625e 49 6e 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  Int(fulltext_vta
3625f 62 20 2a 76 2c 20 4c 65 61 76 65 73 52 65 61 64  b *v, LeavesRead
36260 65 72 20 2a 70 52 65 61 64 65 72 2c 0a 20 20 20  er *pReader,.   
36261 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36262 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
36263 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20  st char *pTerm, 
36264 69 6e 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69  int nTerm, int i
36265 73 50 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20  sPrefix,.       
36266 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36267 20 20 20 20 20 20 20 20 20 44 61 74 61 42 75 66           DataBuf
36268 66 65 72 20 2a 6f 75 74 29 7b 0a 20 20 2f 2a 20  fer *out){.  /* 
36269 64 6f 63 6c 69 73 74 20 64 61 74 61 20 69 73 20  doclist data is 
3626a 61 63 63 75 6d 75 6c 61 74 65 64 20 69 6e 74 6f  accumulated into
3626b 20 70 42 75 66 66 65 72 73 20 73 69 6d 69 6c 61   pBuffers simila
3626c 72 20 74 6f 20 68 6f 77 20 6f 6e 65 20 64 6f 65  r to how one doe
3626d 73 0a 20 20 2a 2a 20 69 6e 63 72 65 6d 65 6e 74  s.  ** increment
3626e 20 69 6e 20 62 69 6e 61 72 79 20 61 72 69 74 68   in binary arith
3626f 6d 65 74 69 63 2e 20 20 49 66 20 69 6e 64 65 78  metic.  If index
36270 20 30 20 69 73 20 65 6d 70 74 79 2c 20 74 68 65   0 is empty, the
36271 20 64 61 74 61 20 69 73 0a 20 20 2a 2a 20 73 74   data is.  ** st
36272 6f 72 65 64 20 74 68 65 72 65 2e 20 20 49 66 20  ored there.  If 
36273 74 68 65 72 65 20 69 73 20 64 61 74 61 20 74 68  there is data th
36274 65 72 65 2c 20 69 74 20 69 73 20 6d 65 72 67 65  ere, it is merge
36275 64 20 61 6e 64 20 74 68 65 0a 20 20 2a 2a 20 72  d and the.  ** r
36276 65 73 75 6c 74 73 20 63 61 72 72 69 65 64 20 69  esults carried i
36277 6e 74 6f 20 70 6f 73 69 74 69 6f 6e 20 31 2c 20  nto position 1, 
36278 77 69 74 68 20 66 75 72 74 68 65 72 20 6d 65 72  with further mer
36279 67 65 2d 61 6e 64 2d 63 61 72 72 79 0a 20 20 2a  ge-and-carry.  *
3627a 2a 20 75 6e 74 69 6c 20 61 6e 20 65 6d 70 74 79  * until an empty
3627b 20 70 6f 73 69 74 69 6f 6e 20 69 73 20 66 6f 75   position is fou
3627c 6e 64 2e 0a 20 20 2a 2f 0a 20 20 44 61 74 61 42  nd..  */.  DataB
3627d 75 66 66 65 72 20 2a 70 42 75 66 66 65 72 73 20  uffer *pBuffers 
3627e 3d 20 4e 55 4c 4c 3b 0a 20 20 69 6e 74 20 6e 42  = NULL;.  int nB
3627f 75 66 66 65 72 73 20 3d 20 30 2c 20 6e 4d 61 78  uffers = 0, nMax
36280 42 75 66 66 65 72 73 20 3d 20 30 2c 20 72 63 3b  Buffers = 0, rc;
36281 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 54 65 72  ..  assert( nTer
36282 6d 3e 30 20 29 3b 0a 0a 20 20 66 6f 72 28 72 63  m>0 );..  for(rc
36283 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 72 63 3d 3d  =SQLITE_OK; rc==
36284 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 6c 65  SQLITE_OK && !le
36285 61 76 65 73 52 65 61 64 65 72 41 74 45 6e 64 28  avesReaderAtEnd(
36286 70 52 65 61 64 65 72 29 3b 0a 20 20 20 20 20 20  pReader);.      
36287 72 63 3d 6c 65 61 76 65 73 52 65 61 64 65 72 53  rc=leavesReaderS
36288 74 65 70 28 76 2c 20 70 52 65 61 64 65 72 29 29  tep(v, pReader))
36289 7b 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68  {.    /* TODO(sh
3628a 65 73 73 29 20 52 65 61 6c 6c 79 20 77 61 6e 74  ess) Really want
3628b 20 6c 65 61 76 65 73 52 65 61 64 65 72 54 65 72   leavesReaderTer
3628c 6d 43 6d 70 28 29 2c 20 62 75 74 20 74 68 61 74  mCmp(), but that
3628d 20 6e 61 6d 65 20 69 73 0a 20 20 20 20 2a 2a 20   name is.    ** 
3628e 61 6c 72 65 61 64 79 20 74 61 6b 65 6e 20 74 6f  already taken to
3628f 20 63 6f 6d 70 61 72 65 20 74 68 65 20 74 65 72   compare the ter
36290 6d 73 20 6f 66 20 74 77 6f 20 4c 65 61 76 65 73  ms of two Leaves
36291 52 65 61 64 65 72 73 2e 20 20 54 68 69 6e 6b 0a  Readers.  Think.
36292 20 20 20 20 2a 2a 20 6f 6e 20 61 20 62 65 74 74      ** on a bett
36293 65 72 20 6e 61 6d 65 2e 20 20 5b 4d 65 61 6e 77  er name.  [Meanw
36294 68 69 6c 65 2c 20 62 72 65 61 6b 20 65 6e 63 61  hile, break enca
36295 70 73 75 6c 61 74 69 6f 6e 20 72 61 74 68 65 72  psulation rather
36296 20 74 68 61 6e 0a 20 20 20 20 2a 2a 20 75 73 65   than.    ** use
36297 20 61 20 63 6f 6e 66 75 73 69 6e 67 20 6e 61 6d   a confusing nam
36298 65 2e 5d 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  e.].    */.    i
36299 6e 74 20 63 20 3d 20 6c 65 61 66 52 65 61 64 65  nt c = leafReade
3629a 72 54 65 72 6d 43 6d 70 28 26 70 52 65 61 64 65  rTermCmp(&pReade
3629b 72 2d 3e 6c 65 61 66 52 65 61 64 65 72 2c 20 70  r->leafReader, p
3629c 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50  Term, nTerm, isP
3629d 72 65 66 69 78 29 3b 0a 20 20 20 20 69 66 28 20  refix);.    if( 
3629e 63 3e 30 20 29 20 62 72 65 61 6b 3b 20 20 20 20  c>0 ) break;    
3629f 20 20 2f 2a 20 50 61 73 74 20 61 6e 79 20 70 6f    /* Past any po
362a0 73 73 69 62 6c 65 20 6d 61 74 63 68 65 73 2e 20  ssible matches. 
362a1 2a 2f 0a 20 20 20 20 69 66 28 20 63 3d 3d 30 20  */.    if( c==0 
362a2 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63  ){.      const c
362a3 68 61 72 20 2a 70 44 61 74 61 20 3d 20 6c 65 61  har *pData = lea
362a4 76 65 73 52 65 61 64 65 72 44 61 74 61 28 70 52  vesReaderData(pR
362a5 65 61 64 65 72 29 3b 0a 20 20 20 20 20 20 69 6e  eader);.      in
362a6 74 20 69 42 75 66 66 65 72 2c 20 6e 44 61 74 61  t iBuffer, nData
362a7 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 44   = leavesReaderD
362a8 61 74 61 42 79 74 65 73 28 70 52 65 61 64 65 72  ataBytes(pReader
362a9 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 46 69 6e  );..      /* Fin
362aa 64 20 74 68 65 20 66 69 72 73 74 20 65 6d 70 74  d the first empt
362ab 79 20 62 75 66 66 65 72 2e 20 2a 2f 0a 20 20 20  y buffer. */.   
362ac 20 20 20 66 6f 72 28 69 42 75 66 66 65 72 3d 30     for(iBuffer=0
362ad 3b 20 69 42 75 66 66 65 72 3c 6e 42 75 66 66 65  ; iBuffer<nBuffe
362ae 72 73 3b 20 2b 2b 69 42 75 66 66 65 72 29 7b 0a  rs; ++iBuffer){.
362af 20 20 20 20 20 20 20 20 69 66 28 20 30 3d 3d 70          if( 0==p
362b0 42 75 66 66 65 72 73 5b 69 42 75 66 66 65 72 5d  Buffers[iBuffer]
362b1 2e 6e 44 61 74 61 20 29 20 62 72 65 61 6b 3b 0a  .nData ) break;.
362b2 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
362b3 2a 20 4f 75 74 20 6f 66 20 62 75 66 66 65 72 73  * Out of buffers
362b4 2c 20 61 64 64 20 61 6e 20 65 6d 70 74 79 20 6f  , add an empty o
362b5 6e 65 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  ne. */.      if(
362b6 20 69 42 75 66 66 65 72 3d 3d 6e 42 75 66 66 65   iBuffer==nBuffe
362b7 72 73 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  rs ){.        if
362b8 28 20 6e 42 75 66 66 65 72 73 3d 3d 6e 4d 61 78  ( nBuffers==nMax
362b9 42 75 66 66 65 72 73 20 29 7b 0a 20 20 20 20 20  Buffers ){.     
362ba 20 20 20 20 20 44 61 74 61 42 75 66 66 65 72 20       DataBuffer 
362bb 2a 70 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 4d  *p;.          nM
362bc 61 78 42 75 66 66 65 72 73 20 2b 3d 20 32 30 3b  axBuffers += 20;
362bd 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  ..          /* M
362be 61 6e 75 61 6c 20 72 65 61 6c 6c 6f 63 20 73 6f  anual realloc so
362bf 20 77 65 20 63 61 6e 20 68 61 6e 64 6c 65 20 4e   we can handle N
362c0 55 4c 4c 20 61 70 70 72 6f 70 72 69 61 74 65 6c  ULL appropriatel
362c1 79 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  y. */.          
362c2 70 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  p = sqlite3_mall
362c3 6f 63 28 6e 4d 61 78 42 75 66 66 65 72 73 2a 73  oc(nMaxBuffers*s
362c4 69 7a 65 6f 66 28 2a 70 42 75 66 66 65 72 73 29  izeof(*pBuffers)
362c5 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
362c6 20 70 3d 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20   p==NULL ){.    
362c7 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
362c8 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
362c9 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
362ca 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20          }..     
362cb 20 20 20 20 20 69 66 28 20 6e 42 75 66 66 65 72       if( nBuffer
362cc 73 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  s>0 ){.         
362cd 20 20 20 61 73 73 65 72 74 28 70 42 75 66 66 65     assert(pBuffe
362ce 72 73 21 3d 4e 55 4c 4c 29 3b 0a 20 20 20 20 20  rs!=NULL);.     
362cf 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2c         memcpy(p,
362d0 20 70 42 75 66 66 65 72 73 2c 20 6e 42 75 66 66   pBuffers, nBuff
362d1 65 72 73 2a 73 69 7a 65 6f 66 28 2a 70 42 75 66  ers*sizeof(*pBuf
362d2 66 65 72 73 29 29 3b 0a 20 20 20 20 20 20 20 20  fers));.        
362d3 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
362d4 28 70 42 75 66 66 65 72 73 29 3b 0a 20 20 20 20  (pBuffers);.    
362d5 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
362d6 20 20 70 42 75 66 66 65 72 73 20 3d 20 70 3b 0a    pBuffers = p;.
362d7 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
362d8 20 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74    dataBufferInit
362d9 28 26 28 70 42 75 66 66 65 72 73 5b 6e 42 75 66  (&(pBuffers[nBuf
362da 66 65 72 73 5d 29 2c 20 30 29 3b 0a 20 20 20 20  fers]), 0);.    
362db 20 20 20 20 6e 42 75 66 66 65 72 73 2b 2b 3b 0a      nBuffers++;.
362dc 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
362dd 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c  * At this point,
362de 20 6d 75 73 74 20 68 61 76 65 20 61 6e 20 65 6d   must have an em
362df 70 74 79 20 61 74 20 69 42 75 66 66 65 72 2e 20  pty at iBuffer. 
362e0 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  */.      assert(
362e1 69 42 75 66 66 65 72 3c 6e 42 75 66 66 65 72 73  iBuffer<nBuffers
362e2 20 26 26 20 70 42 75 66 66 65 72 73 5b 69 42 75   && pBuffers[iBu
362e3 66 66 65 72 5d 2e 6e 44 61 74 61 3d 3d 30 29 3b  ffer].nData==0);
362e4 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 65 6d  ..      /* If em
362e5 70 74 79 20 77 61 73 20 66 69 72 73 74 20 62 75  pty was first bu
362e6 66 66 65 72 2c 20 6e 6f 20 6e 65 65 64 20 66 6f  ffer, no need fo
362e7 72 20 6d 65 72 67 65 20 6c 6f 67 69 63 2e 20 2a  r merge logic. *
362e8 2f 0a 20 20 20 20 20 20 69 66 28 20 69 42 75 66  /.      if( iBuf
362e9 66 65 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  fer==0 ){.      
362ea 20 20 64 61 74 61 42 75 66 66 65 72 52 65 70 6c    dataBufferRepl
362eb 61 63 65 28 26 28 70 42 75 66 66 65 72 73 5b 30  ace(&(pBuffers[0
362ec 5d 29 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61  ]), pData, nData
362ed 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
362ee 20 20 20 20 20 20 20 20 2f 2a 20 70 41 63 63 20          /* pAcc 
362ef 69 73 20 74 68 65 20 65 6d 70 74 79 20 62 75 66  is the empty buf
362f0 66 65 72 20 74 68 65 20 6d 65 72 67 65 64 20 64  fer the merged d
362f1 61 74 61 20 77 69 6c 6c 20 65 6e 64 20 75 70 20  ata will end up 
362f2 69 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 44  in. */.        D
362f3 61 74 61 42 75 66 66 65 72 20 2a 70 41 63 63 20  ataBuffer *pAcc 
362f4 3d 20 26 28 70 42 75 66 66 65 72 73 5b 69 42 75  = &(pBuffers[iBu
362f5 66 66 65 72 5d 29 3b 0a 20 20 20 20 20 20 20 20  ffer]);.        
362f6 44 61 74 61 42 75 66 66 65 72 20 2a 70 20 3d 20  DataBuffer *p = 
362f7 26 28 70 42 75 66 66 65 72 73 5b 30 5d 29 3b 0a  &(pBuffers[0]);.
362f8 0a 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64  .        /* Hand
362f9 6c 65 20 70 6f 73 69 74 69 6f 6e 20 30 20 73 70  le position 0 sp
362fa 65 63 69 61 6c 6c 79 20 74 6f 20 61 76 6f 69 64  ecially to avoid
362fb 20 6e 65 65 64 20 74 6f 20 70 72 69 6d 65 20 70   need to prime p
362fc 41 63 63 0a 20 20 20 20 20 20 20 20 2a 2a 20 77  Acc.        ** w
362fd 69 74 68 20 70 44 61 74 61 2f 6e 44 61 74 61 2e  ith pData/nData.
362fe 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
362ff 20 20 20 20 64 61 74 61 42 75 66 66 65 72 53 77      dataBufferSw
36300 61 70 28 70 2c 20 70 41 63 63 29 3b 0a 20 20 20  ap(p, pAcc);.   
36301 20 20 20 20 20 64 6f 63 4c 69 73 74 41 63 63 75       docListAccu
36302 6d 75 6c 61 74 65 55 6e 69 6f 6e 28 70 41 63 63  mulateUnion(pAcc
36303 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61 29 3b  , pData, nData);
36304 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 63 63  ..        /* Acc
36305 75 6d 75 6c 61 74 65 20 72 65 6d 61 69 6e 69 6e  umulate remainin
36306 67 20 64 6f 63 6c 69 73 74 73 20 69 6e 74 6f 20  g doclists into 
36307 70 41 63 63 2e 20 2a 2f 0a 20 20 20 20 20 20 20  pAcc. */.       
36308 20 66 6f 72 28 2b 2b 70 3b 20 70 3c 70 41 63 63   for(++p; p<pAcc
36309 3b 20 2b 2b 70 29 7b 0a 20 20 20 20 20 20 20 20  ; ++p){.        
3630a 20 20 64 6f 63 4c 69 73 74 41 63 63 75 6d 75 6c    docListAccumul
3630b 61 74 65 55 6e 69 6f 6e 28 70 41 63 63 2c 20 70  ateUnion(pAcc, p
3630c 2d 3e 70 44 61 74 61 2c 20 70 2d 3e 6e 44 61 74  ->pData, p->nDat
3630d 61 29 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 2f  a);..          /
3630e 2a 20 64 61 74 61 42 75 66 66 65 72 52 65 73 65  * dataBufferRese
3630f 74 28 29 20 63 6f 75 6c 64 20 61 6c 6c 6f 77 20  t() could allow 
36310 61 20 6c 61 72 67 65 20 64 6f 63 6c 69 73 74 20  a large doclist 
36311 74 6f 20 62 6c 6f 77 20 75 70 0a 20 20 20 20 20  to blow up.     
36312 20 20 20 20 20 2a 2a 20 6f 75 72 20 6d 65 6d 6f       ** our memo
36313 72 79 20 72 65 71 75 69 72 65 6d 65 6e 74 73 2e  ry requirements.
36314 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  .          */.  
36315 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 6e          if( p->n
36316 43 61 70 61 63 69 74 79 3c 31 30 32 34 20 29 7b  Capacity<1024 ){
36317 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 61 74  .            dat
36318 61 42 75 66 66 65 72 52 65 73 65 74 28 70 29 3b  aBufferReset(p);
36319 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
3631a 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 61  {.            da
3631b 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28  taBufferDestroy(
3631c 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  p);.            
3631d 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 70  dataBufferInit(p
3631e 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 0);.          
3631f 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
36320 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20    }.    }.  }.. 
36321 20 2f 2a 20 55 6e 69 6f 6e 20 61 6c 6c 20 74 68   /* Union all th
36322 65 20 64 6f 63 6c 69 73 74 73 20 74 6f 67 65 74  e doclists toget
36323 68 65 72 20 69 6e 74 6f 20 2a 6f 75 74 2e 20 2a  her into *out. *
36324 2f 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73  /.  /* TODO(shes
36325 73 29 20 57 68 61 74 20 69 66 20 2a 6f 75 74 20  s) What if *out 
36326 69 73 20 62 69 67 3f 20 20 53 69 67 68 2e 20 2a  is big?  Sigh. *
36327 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  /.  if( rc==SQLI
36328 54 45 5f 4f 4b 20 26 26 20 6e 42 75 66 66 65 72  TE_OK && nBuffer
36329 73 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  s>0 ){.    int i
3632a 42 75 66 66 65 72 3b 0a 20 20 20 20 66 6f 72 28  Buffer;.    for(
3632b 69 42 75 66 66 65 72 3d 30 3b 20 69 42 75 66 66  iBuffer=0; iBuff
3632c 65 72 3c 6e 42 75 66 66 65 72 73 3b 20 2b 2b 69  er<nBuffers; ++i
3632d 42 75 66 66 65 72 29 7b 0a 20 20 20 20 20 20 69  Buffer){.      i
3632e 66 28 20 70 42 75 66 66 65 72 73 5b 69 42 75 66  f( pBuffers[iBuf
3632f 66 65 72 5d 2e 6e 44 61 74 61 3e 30 20 29 7b 0a  fer].nData>0 ){.
36330 20 20 20 20 20 20 20 20 69 66 28 20 6f 75 74 2d          if( out-
36331 3e 6e 44 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20  >nData==0 ){.   
36332 20 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65         dataBuffe
36333 72 53 77 61 70 28 6f 75 74 2c 20 26 28 70 42 75  rSwap(out, &(pBu
36334 66 66 65 72 73 5b 69 42 75 66 66 65 72 5d 29 29  ffers[iBuffer]))
36335 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
36336 0a 20 20 20 20 20 20 20 20 20 20 64 6f 63 4c 69  .          docLi
36337 73 74 41 63 63 75 6d 75 6c 61 74 65 55 6e 69 6f  stAccumulateUnio
36338 6e 28 6f 75 74 2c 20 70 42 75 66 66 65 72 73 5b  n(out, pBuffers[
36339 69 42 75 66 66 65 72 5d 2e 70 44 61 74 61 2c 0a  iBuffer].pData,.
3633a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3633b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3633c 20 70 42 75 66 66 65 72 73 5b 69 42 75 66 66 65   pBuffers[iBuffe
3633d 72 5d 2e 6e 44 61 74 61 29 3b 0a 20 20 20 20 20  r].nData);.     
3633e 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
3633f 20 7d 0a 20 20 7d 0a 0a 20 20 77 68 69 6c 65 28   }.  }..  while(
36340 20 6e 42 75 66 66 65 72 73 2d 2d 20 29 7b 0a 20   nBuffers-- ){. 
36341 20 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73     dataBufferDes
36342 74 72 6f 79 28 26 28 70 42 75 66 66 65 72 73 5b  troy(&(pBuffers[
36343 6e 42 75 66 66 65 72 73 5d 29 29 3b 0a 20 20 7d  nBuffers]));.  }
36344 0a 20 20 69 66 28 20 70 42 75 66 66 65 72 73 21  .  if( pBuffers!
36345 3d 4e 55 4c 4c 20 29 20 73 71 6c 69 74 65 33 5f  =NULL ) sqlite3_
36346 66 72 65 65 28 70 42 75 66 66 65 72 73 29 3b 0a  free(pBuffers);.
36347 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
36348 0a 2f 2a 20 43 61 6c 6c 20 6c 6f 61 64 53 65 67  ./* Call loadSeg
36349 6d 65 6e 74 4c 65 61 76 65 73 49 6e 74 28 29 20  mentLeavesInt() 
3634a 77 69 74 68 20 70 44 61 74 61 2f 6e 44 61 74 61  with pData/nData
3634b 20 61 73 20 69 6e 70 75 74 2e 20 2a 2f 0a 73 74   as input. */.st
3634c 61 74 69 63 20 69 6e 74 20 6c 6f 61 64 53 65 67  atic int loadSeg
3634d 6d 65 6e 74 4c 65 61 66 28 66 75 6c 6c 74 65 78  mentLeaf(fulltex
3634e 74 5f 76 74 61 62 20 2a 76 2c 20 63 6f 6e 73 74  t_vtab *v, const
3634f 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e   char *pData, in
36350 74 20 6e 44 61 74 61 2c 0a 20 20 20 20 20 20 20  t nData,.       
36351 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36352 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
36353 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d  pTerm, int nTerm
36354 2c 20 69 6e 74 20 69 73 50 72 65 66 69 78 2c 0a  , int isPrefix,.
36355 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36356 20 20 20 20 20 20 20 20 20 20 20 44 61 74 61 42             DataB
36357 75 66 66 65 72 20 2a 6f 75 74 29 7b 0a 20 20 4c  uffer *out){.  L
36358 65 61 76 65 73 52 65 61 64 65 72 20 72 65 61 64  eavesReader read
36359 65 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20  er;.  int rc;.. 
3635a 20 61 73 73 65 72 74 28 20 6e 44 61 74 61 3e 31   assert( nData>1
3635b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 2a 70   );.  assert( *p
3635c 44 61 74 61 3d 3d 27 5c 30 27 20 29 3b 0a 20 20  Data=='\0' );.  
3635d 72 63 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65  rc = leavesReade
3635e 72 49 6e 69 74 28 76 2c 20 30 2c 20 30 2c 20 30  rInit(v, 0, 0, 0
3635f 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c 20  , pData, nData, 
36360 26 72 65 61 64 65 72 29 3b 0a 20 20 69 66 28 20  &reader);.  if( 
36361 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
36362 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63  return rc;..  rc
36363 20 3d 20 6c 6f 61 64 53 65 67 6d 65 6e 74 4c 65   = loadSegmentLe
36364 61 76 65 73 49 6e 74 28 76 2c 20 26 72 65 61 64  avesInt(v, &read
36365 65 72 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d  er, pTerm, nTerm
36366 2c 20 69 73 50 72 65 66 69 78 2c 20 6f 75 74 29  , isPrefix, out)
36367 3b 0a 20 20 6c 65 61 76 65 73 52 65 61 64 65 72  ;.  leavesReader
36368 52 65 73 65 74 28 26 72 65 61 64 65 72 29 3b 0a  Reset(&reader);.
36369 20 20 6c 65 61 76 65 73 52 65 61 64 65 72 44 65    leavesReaderDe
3636a 73 74 72 6f 79 28 26 72 65 61 64 65 72 29 3b 0a  stroy(&reader);.
3636b 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
3636c 2f 2a 20 43 61 6c 6c 20 6c 6f 61 64 53 65 67 6d  /* Call loadSegm
3636d 65 6e 74 4c 65 61 76 65 73 49 6e 74 28 29 20 77  entLeavesInt() w
3636e 69 74 68 20 74 68 65 20 6c 65 61 66 20 6e 6f 64  ith the leaf nod
3636f 65 73 20 66 72 6f 6d 20 69 53 74 61 72 74 4c 65  es from iStartLe
36370 61 66 20 74 6f 0a 2a 2a 20 69 45 6e 64 4c 65 61  af to.** iEndLea
36371 66 20 28 69 6e 63 6c 75 73 69 76 65 29 20 61 73  f (inclusive) as
36372 20 69 6e 70 75 74 2c 20 61 6e 64 20 6d 65 72 67   input, and merg
36373 65 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20  e the resulting 
36374 64 6f 63 6c 69 73 74 20 69 6e 74 6f 0a 2a 2a 20  doclist into.** 
36375 6f 75 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  out..*/.static i
36376 6e 74 20 6c 6f 61 64 53 65 67 6d 65 6e 74 4c 65  nt loadSegmentLe
36377 61 76 65 73 28 66 75 6c 6c 74 65 78 74 5f 76 74  aves(fulltext_vt
36378 61 62 20 2a 76 2c 0a 20 20 20 20 20 20 20 20 20  ab *v,.         
36379 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3637a 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34      sqlite_int64
3637b 20 69 53 74 61 72 74 4c 65 61 66 2c 20 73 71 6c   iStartLeaf, sql
3637c 69 74 65 5f 69 6e 74 36 34 20 69 45 6e 64 4c 65  ite_int64 iEndLe
3637d 61 66 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  af,.            
3637e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3637f 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65   const char *pTe
36380 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20 69  rm, int nTerm, i
36381 6e 74 20 69 73 50 72 65 66 69 78 2c 0a 20 20 20  nt isPrefix,.   
36382 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36383 20 20 20 20 20 20 20 20 20 20 44 61 74 61 42 75            DataBu
36384 66 66 65 72 20 2a 6f 75 74 29 7b 0a 20 20 69 6e  ffer *out){.  in
36385 74 20 72 63 3b 0a 20 20 4c 65 61 76 65 73 52 65  t rc;.  LeavesRe
36386 61 64 65 72 20 72 65 61 64 65 72 3b 0a 0a 20 20  ader reader;..  
36387 61 73 73 65 72 74 28 20 69 53 74 61 72 74 4c 65  assert( iStartLe
36388 61 66 3c 3d 69 45 6e 64 4c 65 61 66 20 29 3b 0a  af<=iEndLeaf );.
36389 20 20 72 63 20 3d 20 6c 65 61 76 65 73 52 65 61    rc = leavesRea
3638a 64 65 72 49 6e 69 74 28 76 2c 20 30 2c 20 69 53  derInit(v, 0, iS
3638b 74 61 72 74 4c 65 61 66 2c 20 69 45 6e 64 4c 65  tartLeaf, iEndLe
3638c 61 66 2c 20 4e 55 4c 4c 2c 20 30 2c 20 26 72 65  af, NULL, 0, &re
3638d 61 64 65 72 29 3b 0a 20 20 69 66 28 20 72 63 21  ader);.  if( rc!
3638e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
3638f 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20  urn rc;..  rc = 
36390 6c 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61 76 65  loadSegmentLeave
36391 73 49 6e 74 28 76 2c 20 26 72 65 61 64 65 72 2c  sInt(v, &reader,
36392 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69   pTerm, nTerm, i
36393 73 50 72 65 66 69 78 2c 20 6f 75 74 29 3b 0a 20  sPrefix, out);. 
36394 20 6c 65 61 76 65 73 52 65 61 64 65 72 52 65 73   leavesReaderRes
36395 65 74 28 26 72 65 61 64 65 72 29 3b 0a 20 20 6c  et(&reader);.  l
36396 65 61 76 65 73 52 65 61 64 65 72 44 65 73 74 72  eavesReaderDestr
36397 6f 79 28 26 72 65 61 64 65 72 29 3b 0a 20 20 72  oy(&reader);.  r
36398 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20  eturn rc;.}../* 
36399 54 61 6b 69 6e 67 20 70 44 61 74 61 2f 6e 44 61  Taking pData/nDa
3639a 74 61 20 61 73 20 61 6e 20 69 6e 74 65 72 69 6f  ta as an interio
3639b 72 20 6e 6f 64 65 2c 20 66 69 6e 64 20 74 68 65  r node, find the
3639c 20 73 65 71 75 65 6e 63 65 20 6f 66 20 63 68 69   sequence of chi
3639d 6c 64 0a 2a 2a 20 6e 6f 64 65 73 20 77 68 69 63  ld.** nodes whic
3639e 68 20 63 6f 75 6c 64 20 69 6e 63 6c 75 64 65 20  h could include 
3639f 70 54 65 72 6d 2f 6e 54 65 72 6d 2f 69 73 50 72  pTerm/nTerm/isPr
363a0 65 66 69 78 2e 20 20 4e 6f 74 65 20 74 68 61 74  efix.  Note that
363a1 20 74 68 65 0a 2a 2a 20 69 6e 74 65 72 69 6f 72   the.** interior
363a2 20 6e 6f 64 65 20 74 65 72 6d 73 20 6c 6f 67 69   node terms logi
363a3 63 61 6c 6c 79 20 63 6f 6d 65 20 62 65 74 77 65  cally come betwe
363a4 65 6e 20 74 68 65 20 62 6c 6f 63 6b 73 2c 20 73  en the blocks, s
363a5 6f 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 6f 6e  o there is.** on
363a6 65 20 6d 6f 72 65 20 62 6c 6f 63 6b 69 64 20 74  e more blockid t
363a7 68 61 6e 20 74 68 65 72 65 20 61 72 65 20 74 65  han there are te
363a8 72 6d 73 20 28 74 68 61 74 20 62 6c 6f 63 6b 20  rms (that block 
363a9 63 6f 6e 74 61 69 6e 73 20 74 65 72 6d 73 20 3e  contains terms >
363aa 3d 0a 2a 2a 20 74 68 65 20 6c 61 73 74 20 69 6e  =.** the last in
363ab 74 65 72 69 6f 72 2d 6e 6f 64 65 20 74 65 72 6d  terior-node term
363ac 29 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68  )..*/./* TODO(sh
363ad 65 73 73 29 20 54 68 65 20 63 61 6c 6c 69 6e 67  ess) The calling
363ae 20 63 6f 64 65 20 6d 61 79 20 61 6c 72 65 61 64   code may alread
363af 79 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 20  y know that the 
363b0 65 6e 64 20 63 68 69 6c 64 20 69 73 0a 2a 2a 20  end child is.** 
363b1 6e 6f 74 20 77 6f 72 74 68 20 63 61 6c 63 75 6c  not worth calcul
363b2 61 74 69 6e 67 2c 20 62 65 63 61 75 73 65 20 74  ating, because t
363b3 68 65 20 65 6e 64 20 6d 61 79 20 62 65 20 69 6e  he end may be in
363b4 20 61 20 6c 61 74 65 72 20 73 69 62 6c 69 6e 67   a later sibling
363b5 0a 2a 2a 20 6e 6f 64 65 2e 20 20 43 6f 6e 73 69  .** node.  Consi
363b6 64 65 72 20 77 68 65 74 68 65 72 20 62 72 65 61  der whether brea
363b7 6b 69 6e 67 20 73 79 6d 6d 65 74 72 79 20 69 73  king symmetry is
363b8 20 77 6f 72 74 68 77 68 69 6c 65 2e 20 20 49 20   worthwhile.  I 
363b9 73 75 73 70 65 63 74 0a 2a 2a 20 69 74 20 69 73  suspect.** it is
363ba 20 6e 6f 74 20 77 6f 72 74 68 77 68 69 6c 65 2e   not worthwhile.
363bb 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
363bc 67 65 74 43 68 69 6c 64 72 65 6e 43 6f 6e 74 61  getChildrenConta
363bd 69 6e 69 6e 67 28 63 6f 6e 73 74 20 63 68 61 72  ining(const char
363be 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61   *pData, int nDa
363bf 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ta,.            
363c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
363c1 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
363c2 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65   *pTerm, int nTe
363c3 72 6d 2c 20 69 6e 74 20 69 73 50 72 65 66 69 78  rm, int isPrefix
363c4 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
363c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
363c6 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34      sqlite_int64
363c7 20 2a 70 69 53 74 61 72 74 43 68 69 6c 64 2c 0a   *piStartChild,.
363c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
363c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
363ca 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a    sqlite_int64 *
363cb 70 69 45 6e 64 43 68 69 6c 64 29 7b 0a 20 20 49  piEndChild){.  I
363cc 6e 74 65 72 69 6f 72 52 65 61 64 65 72 20 72 65  nteriorReader re
363cd 61 64 65 72 3b 0a 0a 20 20 61 73 73 65 72 74 28  ader;..  assert(
363ce 20 6e 44 61 74 61 3e 31 20 29 3b 0a 20 20 61 73   nData>1 );.  as
363cf 73 65 72 74 28 20 2a 70 44 61 74 61 21 3d 27 5c  sert( *pData!='\
363d0 30 27 20 29 3b 0a 20 20 69 6e 74 65 72 69 6f 72  0' );.  interior
363d1 52 65 61 64 65 72 49 6e 69 74 28 70 44 61 74 61  ReaderInit(pData
363d2 2c 20 6e 44 61 74 61 2c 20 26 72 65 61 64 65 72  , nData, &reader
363d3 29 3b 0a 0a 20 20 2f 2a 20 53 63 61 6e 20 66 6f  );..  /* Scan fo
363d4 72 20 74 68 65 20 66 69 72 73 74 20 63 68 69 6c  r the first chil
363d5 64 20 77 68 69 63 68 20 63 6f 75 6c 64 20 63 6f  d which could co
363d6 6e 74 61 69 6e 20 70 54 65 72 6d 2f 6e 54 65 72  ntain pTerm/nTer
363d7 6d 2e 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 21  m. */.  while( !
363d8 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 41 74  interiorReaderAt
363d9 45 6e 64 28 26 72 65 61 64 65 72 29 20 29 7b 0a  End(&reader) ){.
363da 20 20 20 20 69 66 28 20 69 6e 74 65 72 69 6f 72      if( interior
363db 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28 26 72  ReaderTermCmp(&r
363dc 65 61 64 65 72 2c 20 70 54 65 72 6d 2c 20 6e 54  eader, pTerm, nT
363dd 65 72 6d 2c 20 30 29 3e 30 20 29 20 62 72 65 61  erm, 0)>0 ) brea
363de 6b 3b 0a 20 20 20 20 69 6e 74 65 72 69 6f 72 52  k;.    interiorR
363df 65 61 64 65 72 53 74 65 70 28 26 72 65 61 64 65  eaderStep(&reade
363e0 72 29 3b 0a 20 20 7d 0a 20 20 2a 70 69 53 74 61  r);.  }.  *piSta
363e1 72 74 43 68 69 6c 64 20 3d 20 69 6e 74 65 72 69  rtChild = interi
363e2 6f 72 52 65 61 64 65 72 43 75 72 72 65 6e 74 42  orReaderCurrentB
363e3 6c 6f 63 6b 69 64 28 26 72 65 61 64 65 72 29 3b  lockid(&reader);
363e4 0a 0a 20 20 2f 2a 20 4b 65 65 70 20 73 63 61 6e  ..  /* Keep scan
363e5 6e 69 6e 67 20 74 6f 20 66 69 6e 64 20 61 20 74  ning to find a t
363e6 65 72 6d 20 67 72 65 61 74 65 72 20 74 68 61 6e  erm greater than
363e7 20 6f 75 72 20 74 65 72 6d 2c 20 75 73 69 6e 67   our term, using
363e8 20 70 72 65 66 69 78 0a 20 20 2a 2a 20 63 6f 6d   prefix.  ** com
363e9 70 61 72 69 73 6f 6e 20 69 66 20 69 6e 64 69 63  parison if indic
363ea 61 74 65 64 2e 20 20 49 66 20 69 73 50 72 65 66  ated.  If isPref
363eb 69 78 20 69 73 20 66 61 6c 73 65 2c 20 74 68 69  ix is false, thi
363ec 73 20 77 69 6c 6c 20 62 65 20 74 68 65 0a 20 20  s will be the.  
363ed 2a 2a 20 73 61 6d 65 20 62 6c 6f 63 6b 69 64 20  ** same blockid 
363ee 61 73 20 74 68 65 20 73 74 61 72 74 69 6e 67 20  as the starting 
363ef 62 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 77 68  block..  */.  wh
363f0 69 6c 65 28 20 21 69 6e 74 65 72 69 6f 72 52 65  ile( !interiorRe
363f1 61 64 65 72 41 74 45 6e 64 28 26 72 65 61 64 65  aderAtEnd(&reade
363f2 72 29 20 29 7b 0a 20 20 20 20 69 66 28 20 69 6e  r) ){.    if( in
363f3 74 65 72 69 6f 72 52 65 61 64 65 72 54 65 72 6d  teriorReaderTerm
363f4 43 6d 70 28 26 72 65 61 64 65 72 2c 20 70 54 65  Cmp(&reader, pTe
363f5 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65  rm, nTerm, isPre
363f6 66 69 78 29 3e 30 20 29 20 62 72 65 61 6b 3b 0a  fix)>0 ) break;.
363f7 20 20 20 20 69 6e 74 65 72 69 6f 72 52 65 61 64      interiorRead
363f8 65 72 53 74 65 70 28 26 72 65 61 64 65 72 29 3b  erStep(&reader);
363f9 0a 20 20 7d 0a 20 20 2a 70 69 45 6e 64 43 68 69  .  }.  *piEndChi
363fa 6c 64 20 3d 20 69 6e 74 65 72 69 6f 72 52 65 61  ld = interiorRea
363fb 64 65 72 43 75 72 72 65 6e 74 42 6c 6f 63 6b 69  derCurrentBlocki
363fc 64 28 26 72 65 61 64 65 72 29 3b 0a 0a 20 20 69  d(&reader);..  i
363fd 6e 74 65 72 69 6f 72 52 65 61 64 65 72 44 65 73  nteriorReaderDes
363fe 74 72 6f 79 28 26 72 65 61 64 65 72 29 3b 0a 0a  troy(&reader);..
363ff 20 20 2f 2a 20 43 68 69 6c 64 72 65 6e 20 6d 75    /* Children mu
36400 73 74 20 61 73 63 65 6e 64 2c 20 61 6e 64 20 69  st ascend, and i
36401 66 20 21 70 72 65 66 69 78 2c 20 62 6f 74 68 20  f !prefix, both 
36402 6d 75 73 74 20 62 65 20 74 68 65 20 73 61 6d 65  must be the same
36403 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 2a  . */.  assert( *
36404 70 69 45 6e 64 43 68 69 6c 64 3e 3d 2a 70 69 53  piEndChild>=*piS
36405 74 61 72 74 43 68 69 6c 64 20 29 3b 0a 20 20 61  tartChild );.  a
36406 73 73 65 72 74 28 20 69 73 50 72 65 66 69 78 20  ssert( isPrefix 
36407 7c 7c 20 2a 70 69 53 74 61 72 74 43 68 69 6c 64  || *piStartChild
36408 3d 3d 2a 70 69 45 6e 64 43 68 69 6c 64 20 29 3b  ==*piEndChild );
36409 0a 7d 0a 0a 2f 2a 20 52 65 61 64 20 62 6c 6f 63  .}../* Read bloc
3640a 6b 20 61 74 20 69 42 6c 6f 63 6b 69 64 20 61 6e  k at iBlockid an
3640b 64 20 70 61 73 73 20 69 74 20 77 69 74 68 20 6f  d pass it with o
3640c 74 68 65 72 20 70 61 72 61 6d 73 20 74 6f 0a 2a  ther params to.*
3640d 2a 20 67 65 74 43 68 69 6c 64 72 65 6e 43 6f 6e  * getChildrenCon
3640e 74 61 69 6e 69 6e 67 28 29 2e 0a 2a 2f 0a 73 74  taining()..*/.st
3640f 61 74 69 63 20 69 6e 74 20 6c 6f 61 64 41 6e 64  atic int loadAnd
36410 47 65 74 43 68 69 6c 64 72 65 6e 43 6f 6e 74 61  GetChildrenConta
36411 69 6e 69 6e 67 28 0a 20 20 66 75 6c 6c 74 65 78  ining(.  fulltex
36412 74 5f 76 74 61 62 20 2a 76 2c 0a 20 20 73 71 6c  t_vtab *v,.  sql
36413 69 74 65 5f 69 6e 74 36 34 20 69 42 6c 6f 63 6b  ite_int64 iBlock
36414 69 64 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  id,.  const char
36415 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65   *pTerm, int nTe
36416 72 6d 2c 20 69 6e 74 20 69 73 50 72 65 66 69 78  rm, int isPrefix
36417 2c 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  ,.  sqlite_int64
36418 20 2a 70 69 53 74 61 72 74 43 68 69 6c 64 2c 20   *piStartChild, 
36419 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 69  sqlite_int64 *pi
3641a 45 6e 64 43 68 69 6c 64 0a 29 7b 0a 20 20 73 71  EndChild.){.  sq
3641b 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 20 3d 20  lite3_stmt *s = 
3641c 4e 55 4c 4c 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  NULL;.  int rc;.
3641d 0a 20 20 61 73 73 65 72 74 28 20 69 42 6c 6f 63  .  assert( iBloc
3641e 6b 69 64 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  kid!=0 );.  asse
3641f 72 74 28 20 70 54 65 72 6d 21 3d 4e 55 4c 4c 20  rt( pTerm!=NULL 
36420 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 54 65  );.  assert( nTe
36421 72 6d 21 3d 30 20 29 3b 20 20 20 20 20 20 20 20  rm!=0 );        
36422 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 57  /* TODO(shess) W
36423 68 79 20 6e 6f 74 20 61 6c 6c 6f 77 20 74 68 69  hy not allow thi
36424 73 3f 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  s? */.  assert( 
36425 70 69 53 74 61 72 74 43 68 69 6c 64 21 3d 4e 55  piStartChild!=NU
36426 4c 4c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  LL );.  assert( 
36427 70 69 45 6e 64 43 68 69 6c 64 21 3d 4e 55 4c 4c  piEndChild!=NULL
36428 20 29 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 5f   );..  rc = sql_
36429 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c  get_statement(v,
3642a 20 42 4c 4f 43 4b 5f 53 45 4c 45 43 54 5f 53 54   BLOCK_SELECT_ST
3642b 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72  MT, &s);.  if( r
3642c 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
3642d 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20  eturn rc;..  rc 
3642e 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69  = sqlite3_bind_i
3642f 6e 74 36 34 28 73 2c 20 31 2c 20 69 42 6c 6f 63  nt64(s, 1, iBloc
36430 6b 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  kid);.  if( rc!=
36431 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
36432 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73  rn rc;..  rc = s
36433 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a  qlite3_step(s);.
36434 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
36435 5f 44 4f 4e 45 20 29 20 72 65 74 75 72 6e 20 53  _DONE ) return S
36436 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 69  QLITE_ERROR;.  i
36437 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f  f( rc!=SQLITE_RO
36438 57 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  W ) return rc;..
36439 20 20 67 65 74 43 68 69 6c 64 72 65 6e 43 6f 6e    getChildrenCon
3643a 74 61 69 6e 69 6e 67 28 73 71 6c 69 74 65 33 5f  taining(sqlite3_
3643b 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73 2c 20 30  column_blob(s, 0
3643c 29 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ), sqlite3_colum
3643d 6e 5f 62 79 74 65 73 28 73 2c 20 30 29 2c 0a 20  n_bytes(s, 0),. 
3643e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3643f 20 20 20 20 20 20 20 70 54 65 72 6d 2c 20 6e 54         pTerm, nT
36440 65 72 6d 2c 20 69 73 50 72 65 66 69 78 2c 20 70  erm, isPrefix, p
36441 69 53 74 61 72 74 43 68 69 6c 64 2c 20 70 69 45  iStartChild, piE
36442 6e 64 43 68 69 6c 64 29 3b 0a 0a 20 20 2f 2a 20  ndChild);..  /* 
36443 57 65 20 65 78 70 65 63 74 20 6f 6e 6c 79 20 6f  We expect only o
36444 6e 65 20 72 6f 77 2e 20 20 57 65 20 6d 75 73 74  ne row.  We must
36445 20 65 78 65 63 75 74 65 20 61 6e 6f 74 68 65 72   execute another
36446 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 0a   sqlite3_step().
36447 20 20 20 2a 20 74 6f 20 63 6f 6d 70 6c 65 74 65     * to complete
36448 20 74 68 65 20 69 74 65 72 61 74 69 6f 6e 3b 20   the iteration; 
36449 6f 74 68 65 72 77 69 73 65 20 74 68 65 20 74 61  otherwise the ta
3644a 62 6c 65 20 77 69 6c 6c 20 72 65 6d 61 69 6e 0a  ble will remain.
3644b 20 20 20 2a 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a     * locked. */.
3644c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73    rc = sqlite3_s
3644d 74 65 70 28 73 29 3b 0a 20 20 69 66 28 20 72 63  tep(s);.  if( rc
3644e 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20 72  ==SQLITE_ROW ) r
3644f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
36450 4f 52 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  OR;.  if( rc!=SQ
36451 4c 49 54 45 5f 44 4f 4e 45 20 29 20 72 65 74 75  LITE_DONE ) retu
36452 72 6e 20 72 63 3b 0a 0a 20 20 72 65 74 75 72 6e  rn rc;..  return
36453 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
36454 2a 20 54 72 61 76 65 72 73 65 20 74 68 65 20 74  * Traverse the t
36455 72 65 65 20 72 65 70 72 65 73 65 6e 74 65 64 20  ree represented 
36456 62 79 20 70 44 61 74 61 5b 6e 44 61 74 61 5d 20  by pData[nData] 
36457 6c 6f 6f 6b 69 6e 67 20 66 6f 72 0a 2a 2a 20 70  looking for.** p
36458 54 65 72 6d 5b 6e 54 65 72 6d 5d 2c 20 70 6c 61  Term[nTerm], pla
36459 63 69 6e 67 20 69 74 73 20 64 6f 63 6c 69 73 74  cing its doclist
3645a 20 69 6e 74 6f 20 2a 6f 75 74 2e 20 20 54 68 69   into *out.  Thi
3645b 73 20 69 73 20 69 6e 74 65 72 6e 61 6c 20 74 6f  s is internal to
3645c 0a 2a 2a 20 6c 6f 61 64 53 65 67 6d 65 6e 74 28  .** loadSegment(
3645d 29 20 74 6f 20 6d 61 6b 65 20 65 72 72 6f 72 2d  ) to make error-
3645e 68 61 6e 64 6c 69 6e 67 20 63 6c 65 61 6e 65 72  handling cleaner
3645f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
36460 6c 6f 61 64 53 65 67 6d 65 6e 74 49 6e 74 28 66  loadSegmentInt(f
36461 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c  ulltext_vtab *v,
36462 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61   const char *pDa
36463 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20  ta, int nData,. 
36464 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36465 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f           sqlite_
36466 69 6e 74 36 34 20 69 4c 65 61 76 65 73 45 6e 64  int64 iLeavesEnd
36467 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
36468 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73              cons
36469 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69  t char *pTerm, i
3646a 6e 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69 73  nt nTerm, int is
3646b 50 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20 20  Prefix,.        
3646c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3646d 20 20 44 61 74 61 42 75 66 66 65 72 20 2a 6f 75    DataBuffer *ou
3646e 74 29 7b 0a 20 20 2f 2a 20 53 70 65 63 69 61 6c  t){.  /* Special
3646f 20 63 61 73 65 20 77 68 65 72 65 20 72 6f 6f 74   case where root
36470 20 69 73 20 61 20 6c 65 61 66 2e 20 2a 2f 0a 20   is a leaf. */. 
36471 20 69 66 28 20 2a 70 44 61 74 61 3d 3d 27 5c 30   if( *pData=='\0
36472 27 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ' ){.    return 
36473 6c 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61 66 28  loadSegmentLeaf(
36474 76 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c  v, pData, nData,
36475 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69   pTerm, nTerm, i
36476 73 50 72 65 66 69 78 2c 20 6f 75 74 29 3b 0a 20  sPrefix, out);. 
36477 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20   }else{.    int 
36478 72 63 3b 0a 20 20 20 20 73 71 6c 69 74 65 5f 69  rc;.    sqlite_i
36479 6e 74 36 34 20 69 53 74 61 72 74 43 68 69 6c 64  nt64 iStartChild
3647a 2c 20 69 45 6e 64 43 68 69 6c 64 3b 0a 0a 20 20  , iEndChild;..  
3647b 20 20 2f 2a 20 50 72 6f 63 65 73 73 20 70 44 61    /* Process pDa
3647c 74 61 20 61 73 20 61 6e 20 69 6e 74 65 72 69 6f  ta as an interio
3647d 72 20 6e 6f 64 65 2c 20 74 68 65 6e 20 6c 6f 6f  r node, then loo
3647e 70 20 64 6f 77 6e 20 74 68 65 20 74 72 65 65 0a  p down the tree.
3647f 20 20 20 20 2a 2a 20 75 6e 74 69 6c 20 77 65 20      ** until we 
36480 66 69 6e 64 20 74 68 65 20 73 65 74 20 6f 66 20  find the set of 
36481 6c 65 61 66 20 6e 6f 64 65 73 20 74 6f 20 73 63  leaf nodes to sc
36482 61 6e 20 66 6f 72 20 74 68 65 20 74 65 72 6d 2e  an for the term.
36483 0a 20 20 20 20 2a 2f 0a 20 20 20 20 67 65 74 43  .    */.    getC
36484 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e 69 6e  hildrenContainin
36485 67 28 70 44 61 74 61 2c 20 6e 44 61 74 61 2c 20  g(pData, nData, 
36486 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73  pTerm, nTerm, is
36487 50 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20 20  Prefix,.        
36488 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36489 20 20 26 69 53 74 61 72 74 43 68 69 6c 64 2c 20    &iStartChild, 
3648a 26 69 45 6e 64 43 68 69 6c 64 29 3b 0a 20 20 20  &iEndChild);.   
3648b 20 77 68 69 6c 65 28 20 69 53 74 61 72 74 43 68   while( iStartCh
3648c 69 6c 64 3e 69 4c 65 61 76 65 73 45 6e 64 20 29  ild>iLeavesEnd )
3648d 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69  {.      sqlite_i
3648e 6e 74 36 34 20 69 4e 65 78 74 53 74 61 72 74 2c  nt64 iNextStart,
3648f 20 69 4e 65 78 74 45 6e 64 3b 0a 20 20 20 20 20   iNextEnd;.     
36490 20 72 63 20 3d 20 6c 6f 61 64 41 6e 64 47 65 74   rc = loadAndGet
36491 43 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e 69  ChildrenContaini
36492 6e 67 28 76 2c 20 69 53 74 61 72 74 43 68 69 6c  ng(v, iStartChil
36493 64 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c  d, pTerm, nTerm,
36494 20 69 73 50 72 65 66 69 78 2c 0a 20 20 20 20 20   isPrefix,.     
36495 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36496 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36497 20 20 20 26 69 4e 65 78 74 53 74 61 72 74 2c 20     &iNextStart, 
36498 26 69 4e 65 78 74 45 6e 64 29 3b 0a 20 20 20 20  &iNextEnd);.    
36499 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
3649a 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
3649b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 77 65  ..      /* If we
3649c 27 76 65 20 62 72 61 6e 63 68 65 64 2c 20 66 6f  've branched, fo
3649d 6c 6c 6f 77 20 74 68 65 20 65 6e 64 20 62 72 61  llow the end bra
3649e 6e 63 68 2c 20 74 6f 6f 2e 20 2a 2f 0a 20 20 20  nch, too. */.   
3649f 20 20 20 69 66 28 20 69 53 74 61 72 74 43 68 69     if( iStartChi
364a0 6c 64 21 3d 69 45 6e 64 43 68 69 6c 64 20 29 7b  ld!=iEndChild ){
364a1 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f  .        sqlite_
364a2 69 6e 74 36 34 20 69 44 75 6d 6d 79 3b 0a 20 20  int64 iDummy;.  
364a3 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 61 64 41        rc = loadA
364a4 6e 64 47 65 74 43 68 69 6c 64 72 65 6e 43 6f 6e  ndGetChildrenCon
364a5 74 61 69 6e 69 6e 67 28 76 2c 20 69 45 6e 64 43  taining(v, iEndC
364a6 68 69 6c 64 2c 20 70 54 65 72 6d 2c 20 6e 54 65  hild, pTerm, nTe
364a7 72 6d 2c 20 69 73 50 72 65 66 69 78 2c 0a 20 20  rm, isPrefix,.  
364a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
364a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
364aa 20 20 20 20 20 20 20 20 26 69 44 75 6d 6d 79 2c          &iDummy,
364ab 20 26 69 4e 65 78 74 45 6e 64 29 3b 0a 20 20 20   &iNextEnd);.   
364ac 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
364ad 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
364ae 72 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  rc;.      }..   
364af 20 20 20 61 73 73 65 72 74 28 20 69 4e 65 78 74     assert( iNext
364b0 53 74 61 72 74 3c 3d 69 4e 65 78 74 45 6e 64 20  Start<=iNextEnd 
364b1 29 3b 0a 20 20 20 20 20 20 69 53 74 61 72 74 43  );.      iStartC
364b2 68 69 6c 64 20 3d 20 69 4e 65 78 74 53 74 61 72  hild = iNextStar
364b3 74 3b 0a 20 20 20 20 20 20 69 45 6e 64 43 68 69  t;.      iEndChi
364b4 6c 64 20 3d 20 69 4e 65 78 74 45 6e 64 3b 0a 20  ld = iNextEnd;. 
364b5 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
364b6 20 69 53 74 61 72 74 43 68 69 6c 64 3c 3d 69 4c   iStartChild<=iL
364b7 65 61 76 65 73 45 6e 64 20 29 3b 0a 20 20 20 20  eavesEnd );.    
364b8 61 73 73 65 72 74 28 20 69 45 6e 64 43 68 69 6c  assert( iEndChil
364b9 64 3c 3d 69 4c 65 61 76 65 73 45 6e 64 20 29 3b  d<=iLeavesEnd );
364ba 0a 0a 20 20 20 20 2f 2a 20 53 63 61 6e 20 74 68  ..    /* Scan th
364bb 72 6f 75 67 68 20 74 68 65 20 6c 65 61 66 20 73  rough the leaf s
364bc 65 67 6d 65 6e 74 73 20 66 6f 72 20 64 6f 63 6c  egments for docl
364bd 69 73 74 73 2e 20 2a 2f 0a 20 20 20 20 72 65 74  ists. */.    ret
364be 75 72 6e 20 6c 6f 61 64 53 65 67 6d 65 6e 74 4c  urn loadSegmentL
364bf 65 61 76 65 73 28 76 2c 20 69 53 74 61 72 74 43  eaves(v, iStartC
364c0 68 69 6c 64 2c 20 69 45 6e 64 43 68 69 6c 64 2c  hild, iEndChild,
364c1 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
364c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 54                pT
364c3 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72  erm, nTerm, isPr
364c4 65 66 69 78 2c 20 6f 75 74 29 3b 0a 20 20 7d 0a  efix, out);.  }.
364c5 7d 0a 0a 2f 2a 20 43 61 6c 6c 20 6c 6f 61 64 53  }../* Call loadS
364c6 65 67 6d 65 6e 74 49 6e 74 28 29 20 74 6f 20 63  egmentInt() to c
364c7 6f 6c 6c 65 63 74 20 74 68 65 20 64 6f 63 6c 69  ollect the docli
364c8 73 74 20 66 6f 72 20 70 54 65 72 6d 2f 6e 54 65  st for pTerm/nTe
364c9 72 6d 2c 20 74 68 65 6e 0a 2a 2a 20 6d 65 72 67  rm, then.** merg
364ca 65 20 69 74 73 20 64 6f 63 6c 69 73 74 20 6f 76  e its doclist ov
364cb 65 72 20 2a 6f 75 74 20 28 61 6e 79 20 64 75 70  er *out (any dup
364cc 6c 69 63 61 74 65 20 64 6f 63 6c 69 73 74 73 20  licate doclists 
364cd 72 65 61 64 20 66 72 6f 6d 20 74 68 65 0a 2a 2a  read from the.**
364ce 20 73 65 67 6d 65 6e 74 20 72 6f 6f 74 65 64 20   segment rooted 
364cf 61 74 20 70 44 61 74 61 20 77 69 6c 6c 20 6f 76  at pData will ov
364d0 65 72 77 72 69 74 65 20 74 68 6f 73 65 20 69 6e  erwrite those in
364d1 20 2a 6f 75 74 29 2e 0a 2a 2f 0a 2f 2a 20 54 4f   *out)..*/./* TO
364d2 44 4f 28 73 68 65 73 73 29 20 43 6f 6e 73 69 64  DO(shess) Consid
364d3 65 72 20 63 68 61 6e 67 69 6e 67 20 74 68 69 73  er changing this
364d4 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68   to determine th
364d5 65 20 64 65 70 74 68 20 6f 66 20 74 68 65 0a 2a  e depth of the.*
364d6 2a 20 6c 65 61 76 65 73 20 75 73 69 6e 67 20 65  * leaves using e
364d7 69 74 68 65 72 20 74 68 65 20 66 69 72 73 74 20  ither the first 
364d8 63 68 61 72 61 63 74 65 72 73 20 6f 66 20 69 6e  characters of in
364d9 74 65 72 69 6f 72 20 6e 6f 64 65 73 20 28 77 68  terior nodes (wh
364da 65 6e 0a 2a 2a 20 3d 3d 31 2c 20 77 65 27 72 65  en.** ==1, we're
364db 20 6f 6e 65 20 6c 65 76 65 6c 20 61 62 6f 76 65   one level above
364dc 20 74 68 65 20 6c 65 61 76 65 73 29 2c 20 6f 72   the leaves), or
364dd 20 74 68 65 20 66 69 72 73 74 20 63 68 61 72 61   the first chara
364de 63 74 65 72 20 6f 66 0a 2a 2a 20 74 68 65 20 72  cter of.** the r
364df 6f 6f 74 20 28 77 68 69 63 68 20 77 69 6c 6c 20  oot (which will 
364e0 64 65 73 63 72 69 62 65 20 74 68 65 20 68 65 69  describe the hei
364e1 67 68 74 20 6f 66 20 74 68 65 20 74 72 65 65 20  ght of the tree 
364e2 64 69 72 65 63 74 6c 79 29 2e 0a 2a 2a 20 45 69  directly)..** Ei
364e3 74 68 65 72 20 66 65 65 6c 73 20 73 6f 6d 65 77  ther feels somew
364e4 68 61 74 20 74 72 69 63 6b 79 20 74 6f 20 6d 65  hat tricky to me
364e5 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65  ..*/./* TODO(she
364e6 73 73 29 20 54 68 65 20 63 75 72 72 65 6e 74 20  ss) The current 
364e7 6d 65 72 67 65 20 69 73 20 6c 69 6b 65 6c 79 20  merge is likely 
364e8 74 6f 20 62 65 20 73 6c 6f 77 20 66 6f 72 20 6c  to be slow for l
364e9 61 72 67 65 0a 2a 2a 20 64 6f 63 6c 69 73 74 73  arge.** doclists
364ea 20 28 74 68 6f 75 67 68 20 69 74 20 73 68 6f 75   (though it shou
364eb 6c 64 20 70 72 6f 63 65 73 73 20 66 72 6f 6d 20  ld process from 
364ec 6e 65 77 65 73 74 2f 73 6d 61 6c 6c 65 73 74 20  newest/smallest 
364ed 74 6f 0a 2a 2a 20 6f 6c 64 65 73 74 2f 6c 61 72  to.** oldest/lar
364ee 67 65 73 74 2c 20 73 6f 20 69 74 20 6d 61 79 20  gest, so it may 
364ef 6e 6f 74 20 62 65 20 74 68 61 74 20 62 61 64 29  not be that bad)
364f0 2e 20 20 49 74 20 6d 69 67 68 74 20 62 65 20 75  .  It might be u
364f1 73 65 66 75 6c 20 74 6f 0a 2a 2a 20 6d 6f 64 69  seful to.** modi
364f2 66 79 20 74 68 69 6e 67 73 20 74 6f 20 61 6c 6c  fy things to all
364f3 6f 77 20 66 6f 72 20 4e 2d 77 61 79 20 6d 65 72  ow for N-way mer
364f4 67 69 6e 67 2e 20 20 54 68 69 73 20 63 6f 75 6c  ging.  This coul
364f5 64 20 65 69 74 68 65 72 20 62 65 0a 2a 2a 20 77  d either be.** w
364f6 69 74 68 69 6e 20 61 20 73 65 67 6d 65 6e 74 2c  ithin a segment,
364f7 20 77 69 74 68 20 70 61 69 72 77 69 73 65 20 6d   with pairwise m
364f8 65 72 67 65 73 20 61 63 72 6f 73 73 20 73 65 67  erges across seg
364f9 6d 65 6e 74 73 2c 20 6f 72 20 61 63 72 6f 73 73  ments, or across
364fa 0a 2a 2a 20 61 6c 6c 20 73 65 67 6d 65 6e 74 73  .** all segments
364fb 20 61 74 20 6f 6e 63 65 2e 0a 2a 2f 0a 73 74 61   at once..*/.sta
364fc 74 69 63 20 69 6e 74 20 6c 6f 61 64 53 65 67 6d  tic int loadSegm
364fd 65 6e 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  ent(fulltext_vta
364fe 62 20 2a 76 2c 20 63 6f 6e 73 74 20 63 68 61 72  b *v, const char
364ff 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61   *pData, int nDa
36500 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ta,.            
36501 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
36502 65 5f 69 6e 74 36 34 20 69 4c 65 61 76 65 73 45  e_int64 iLeavesE
36503 6e 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  nd,.            
36504 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74             const
36505 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e   char *pTerm, in
36506 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69 73 50  t nTerm, int isP
36507 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20 20 20  refix,.         
36508 20 20 20 20 20 20 20 20 20 20 20 20 20 20 44 61                Da
36509 74 61 42 75 66 66 65 72 20 2a 6f 75 74 29 7b 0a  taBuffer *out){.
3650a 20 20 44 61 74 61 42 75 66 66 65 72 20 72 65 73    DataBuffer res
3650b 75 6c 74 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  ult;.  int rc;..
3650c 20 20 61 73 73 65 72 74 28 20 6e 44 61 74 61 3e    assert( nData>
3650d 31 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20  1 );..  /* This 
3650e 63 6f 64 65 20 73 68 6f 75 6c 64 20 6e 65 76 65  code should neve
3650f 72 20 62 65 20 63 61 6c 6c 65 64 20 77 69 74 68  r be called with
36510 20 62 75 66 66 65 72 65 64 20 75 70 64 61 74 65   buffered update
36511 73 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  s. */.  assert( 
36512 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 3c  v->nPendingData<
36513 30 20 29 3b 0a 0a 20 20 64 61 74 61 42 75 66 66  0 );..  dataBuff
36514 65 72 49 6e 69 74 28 26 72 65 73 75 6c 74 2c 20  erInit(&result, 
36515 30 29 3b 0a 20 20 72 63 20 3d 20 6c 6f 61 64 53  0);.  rc = loadS
36516 65 67 6d 65 6e 74 49 6e 74 28 76 2c 20 70 44 61  egmentInt(v, pDa
36517 74 61 2c 20 6e 44 61 74 61 2c 20 69 4c 65 61 76  ta, nData, iLeav
36518 65 73 45 6e 64 2c 0a 20 20 20 20 20 20 20 20 20  esEnd,.         
36519 20 20 20 20 20 20 20 20 20 20 20 20 20 70 54 65               pTe
3651a 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65  rm, nTerm, isPre
3651b 66 69 78 2c 20 26 72 65 73 75 6c 74 29 3b 0a 20  fix, &result);. 
3651c 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
3651d 4f 4b 20 26 26 20 72 65 73 75 6c 74 2e 6e 44 61  OK && result.nDa
3651e 74 61 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20  ta>0 ){.    if( 
3651f 6f 75 74 2d 3e 6e 44 61 74 61 3d 3d 30 20 29 7b  out->nData==0 ){
36520 0a 20 20 20 20 20 20 44 61 74 61 42 75 66 66 65  .      DataBuffe
36521 72 20 74 6d 70 20 3d 20 2a 6f 75 74 3b 0a 20 20  r tmp = *out;.  
36522 20 20 20 20 2a 6f 75 74 20 3d 20 72 65 73 75 6c      *out = resul
36523 74 3b 0a 20 20 20 20 20 20 72 65 73 75 6c 74 20  t;.      result 
36524 3d 20 74 6d 70 3b 0a 20 20 20 20 7d 65 6c 73 65  = tmp;.    }else
36525 7b 0a 20 20 20 20 20 20 44 61 74 61 42 75 66 66  {.      DataBuff
36526 65 72 20 6d 65 72 67 65 64 3b 0a 20 20 20 20 20  er merged;.     
36527 20 44 4c 52 65 61 64 65 72 20 72 65 61 64 65 72   DLReader reader
36528 73 5b 32 5d 3b 0a 0a 20 20 20 20 20 20 64 6c 72  s[2];..      dlr
36529 49 6e 69 74 28 26 72 65 61 64 65 72 73 5b 30 5d  Init(&readers[0]
3652a 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20 6f 75  , DL_DEFAULT, ou
3652b 74 2d 3e 70 44 61 74 61 2c 20 6f 75 74 2d 3e 6e  t->pData, out->n
3652c 44 61 74 61 29 3b 0a 20 20 20 20 20 20 64 6c 72  Data);.      dlr
3652d 49 6e 69 74 28 26 72 65 61 64 65 72 73 5b 31 5d  Init(&readers[1]
3652e 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20 72 65  , DL_DEFAULT, re
3652f 73 75 6c 74 2e 70 44 61 74 61 2c 20 72 65 73 75  sult.pData, resu
36530 6c 74 2e 6e 44 61 74 61 29 3b 0a 20 20 20 20 20  lt.nData);.     
36531 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28   dataBufferInit(
36532 26 6d 65 72 67 65 64 2c 20 6f 75 74 2d 3e 6e 44  &merged, out->nD
36533 61 74 61 2b 72 65 73 75 6c 74 2e 6e 44 61 74 61  ata+result.nData
36534 29 3b 0a 20 20 20 20 20 20 64 6f 63 4c 69 73 74  );.      docList
36535 4d 65 72 67 65 28 26 6d 65 72 67 65 64 2c 20 72  Merge(&merged, r
36536 65 61 64 65 72 73 2c 20 32 29 3b 0a 20 20 20 20  eaders, 2);.    
36537 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74    dataBufferDest
36538 72 6f 79 28 6f 75 74 29 3b 0a 20 20 20 20 20 20  roy(out);.      
36539 2a 6f 75 74 20 3d 20 6d 65 72 67 65 64 3b 0a 20  *out = merged;. 
3653a 20 20 20 20 20 64 6c 72 44 65 73 74 72 6f 79 28       dlrDestroy(
3653b 26 72 65 61 64 65 72 73 5b 30 5d 29 3b 0a 20 20  &readers[0]);.  
3653c 20 20 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26      dlrDestroy(&
3653d 72 65 61 64 65 72 73 5b 31 5d 29 3b 0a 20 20 20  readers[1]);.   
3653e 20 7d 0a 20 20 7d 0a 20 20 64 61 74 61 42 75 66   }.  }.  dataBuf
3653f 66 65 72 44 65 73 74 72 6f 79 28 26 72 65 73 75  ferDestroy(&resu
36540 6c 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  lt);.  return rc
36541 3b 0a 7d 0a 0a 2f 2a 20 53 63 61 6e 20 74 68 65  ;.}../* Scan the
36542 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 6d 65   database and me
36543 72 67 65 20 74 6f 67 65 74 68 65 72 20 74 68 65  rge together the
36544 20 70 6f 73 74 69 6e 67 20 6c 69 73 74 73 20 66   posting lists f
36545 6f 72 20 74 68 65 20 74 65 72 6d 0a 2a 2a 20 69  or the term.** i
36546 6e 74 6f 20 2a 6f 75 74 2e 0a 2a 2f 0a 73 74 61  nto *out..*/.sta
36547 74 69 63 20 69 6e 74 20 74 65 72 6d 53 65 6c 65  tic int termSele
36548 63 74 28 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76  ct(.  fulltext_v
36549 74 61 62 20 2a 76 2c 20 0a 20 20 69 6e 74 20 69  tab *v, .  int i
3654a 43 6f 6c 75 6d 6e 2c 0a 20 20 63 6f 6e 73 74 20  Column,.  const 
3654b 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74  char *pTerm, int
3654c 20 6e 54 65 72 6d 2c 20 20 20 20 20 20 20 20 20   nTerm,         
3654d 20 20 20 20 2f 2a 20 54 65 72 6d 20 74 6f 20 71      /* Term to q
3654e 75 65 72 79 20 66 6f 72 20 2a 2f 0a 20 20 69 6e  uery for */.  in
3654f 74 20 69 73 50 72 65 66 69 78 2c 20 20 20 20 20  t isPrefix,     
36550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36551 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
36552 66 6f 72 20 61 20 70 72 65 66 69 78 20 73 65 61  for a prefix sea
36553 72 63 68 20 2a 2f 0a 20 20 44 6f 63 4c 69 73 74  rch */.  DocList
36554 54 79 70 65 20 69 54 79 70 65 2c 20 0a 20 20 44  Type iType, .  D
36555 61 74 61 42 75 66 66 65 72 20 2a 6f 75 74 20 20  ataBuffer *out  
36556 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36557 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
36558 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a  e results here *
36559 2f 0a 29 7b 0a 20 20 44 61 74 61 42 75 66 66 65  /.){.  DataBuffe
3655a 72 20 64 6f 63 6c 69 73 74 3b 0a 20 20 73 71 6c  r doclist;.  sql
3655b 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20  ite3_stmt *s;.  
3655c 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65 74  int rc = sql_get
3655d 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 53 45  _statement(v, SE
3655e 47 44 49 52 5f 53 45 4c 45 43 54 5f 41 4c 4c 5f  GDIR_SELECT_ALL_
3655f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28  STMT, &s);.  if(
36560 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
36561 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f   return rc;..  /
36562 2a 20 54 68 69 73 20 63 6f 64 65 20 73 68 6f 75  * This code shou
36563 6c 64 20 6e 65 76 65 72 20 62 65 20 63 61 6c 6c  ld never be call
36564 65 64 20 77 69 74 68 20 62 75 66 66 65 72 65 64  ed with buffered
36565 20 75 70 64 61 74 65 73 2e 20 2a 2f 0a 20 20 61   updates. */.  a
36566 73 73 65 72 74 28 20 76 2d 3e 6e 50 65 6e 64 69  ssert( v->nPendi
36567 6e 67 44 61 74 61 3c 30 20 29 3b 0a 0a 20 20 64  ngData<0 );..  d
36568 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 64  ataBufferInit(&d
36569 6f 63 6c 69 73 74 2c 20 30 29 3b 0a 20 20 64 61  oclist, 0);.  da
3656a 74 61 42 75 66 66 65 72 49 6e 69 74 28 6f 75 74  taBufferInit(out
3656b 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 54 72 61 76  , 0);..  /* Trav
3656c 65 72 73 65 20 74 68 65 20 73 65 67 6d 65 6e 74  erse the segment
3656d 73 20 66 72 6f 6d 20 6f 6c 64 65 73 74 20 74 6f  s from oldest to
3656e 20 6e 65 77 65 73 74 20 73 6f 20 74 68 61 74 20   newest so that 
3656f 6e 65 77 65 72 20 64 6f 63 6c 69 73 74 0a 20 20  newer doclist.  
36570 2a 2a 20 65 6c 65 6d 65 6e 74 73 20 66 6f 72 20  ** elements for 
36571 67 69 76 65 6e 20 64 6f 63 69 64 73 20 6f 76 65  given docids ove
36572 72 77 72 69 74 65 20 6f 6c 64 65 72 20 65 6c 65  rwrite older ele
36573 6d 65 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 77 68  ments..  */.  wh
36574 69 6c 65 28 20 28 72 63 20 3d 20 73 71 6c 69 74  ile( (rc = sqlit
36575 65 33 5f 73 74 65 70 28 73 29 29 3d 3d 53 51 4c  e3_step(s))==SQL
36576 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 63  ITE_ROW ){.    c
36577 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61  onst char *pData
36578 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d   = sqlite3_colum
36579 6e 5f 62 6c 6f 62 28 73 2c 20 32 29 3b 0a 20 20  n_blob(s, 2);.  
3657a 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 44 61 74    const int nDat
3657b 61 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  a = sqlite3_colu
3657c 6d 6e 5f 62 79 74 65 73 28 73 2c 20 32 29 3b 0a  mn_bytes(s, 2);.
3657d 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65      const sqlite
3657e 5f 69 6e 74 36 34 20 69 4c 65 61 76 65 73 45 6e  _int64 iLeavesEn
3657f 64 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  d = sqlite3_colu
36580 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 31 29 3b 0a  mn_int64(s, 1);.
36581 20 20 20 20 72 63 20 3d 20 6c 6f 61 64 53 65 67      rc = loadSeg
36582 6d 65 6e 74 28 76 2c 20 70 44 61 74 61 2c 20 6e  ment(v, pData, n
36583 44 61 74 61 2c 20 69 4c 65 61 76 65 73 45 6e 64  Data, iLeavesEnd
36584 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20  , pTerm, nTerm, 
36585 69 73 50 72 65 66 69 78 2c 0a 20 20 20 20 20 20  isPrefix,.      
36586 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
36587 64 6f 63 6c 69 73 74 29 3b 0a 20 20 20 20 69 66  doclist);.    if
36588 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
36589 29 20 67 6f 74 6f 20 65 72 72 3b 0a 20 20 7d 0a  ) goto err;.  }.
3658a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
3658b 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 69 66 28  _DONE ){.    if(
3658c 20 64 6f 63 6c 69 73 74 2e 6e 44 61 74 61 21 3d   doclist.nData!=
3658d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 4f  0 ){.      /* TO
3658e 44 4f 28 73 68 65 73 73 29 20 54 68 65 20 6f 6c  DO(shess) The ol
3658f 64 20 74 65 72 6d 5f 73 65 6c 65 63 74 5f 61 6c  d term_select_al
36590 6c 28 29 20 63 6f 64 65 20 61 70 70 6c 69 65 64  l() code applied
36591 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 20 20 20 20   the column.    
36592 20 20 2a 2a 20 72 65 73 74 72 69 63 74 20 61 73    ** restrict as
36593 20 77 65 20 6d 65 72 67 65 64 20 73 65 67 6d 65   we merged segme
36594 6e 74 73 2c 20 6c 65 61 64 69 6e 67 20 74 6f 20  nts, leading to 
36595 73 6d 61 6c 6c 65 72 20 62 75 66 66 65 72 73 2e  smaller buffers.
36596 0a 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 69  .      ** This i
36597 73 20 70 72 6f 62 61 62 6c 79 20 77 6f 72 74 68  s probably worth
36598 77 68 69 6c 65 20 74 6f 20 62 72 69 6e 67 20 62  while to bring b
36599 61 63 6b 2c 20 6f 6e 63 65 20 74 68 65 20 6e 65  ack, once the ne
3659a 77 20 73 74 6f 72 61 67 65 0a 20 20 20 20 20 20  w storage.      
3659b 2a 2a 20 73 79 73 74 65 6d 20 69 73 20 63 68 65  ** system is che
3659c 63 6b 65 64 20 69 6e 2e 0a 20 20 20 20 20 20 2a  cked in..      *
3659d 2f 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c  /.      if( iCol
3659e 75 6d 6e 3d 3d 76 2d 3e 6e 43 6f 6c 75 6d 6e 29  umn==v->nColumn)
3659f 20 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20   iColumn = -1;. 
365a0 20 20 20 20 20 64 6f 63 4c 69 73 74 54 72 69 6d       docListTrim
365a1 28 44 4c 5f 44 45 46 41 55 4c 54 2c 20 64 6f 63  (DL_DEFAULT, doc
365a2 6c 69 73 74 2e 70 44 61 74 61 2c 20 64 6f 63 6c  list.pData, docl
365a3 69 73 74 2e 6e 44 61 74 61 2c 0a 20 20 20 20 20  ist.nData,.     
365a4 20 20 20 20 20 20 20 20 20 20 20 20 20 69 43 6f               iCo
365a5 6c 75 6d 6e 2c 20 69 54 79 70 65 2c 20 6f 75 74  lumn, iType, out
365a6 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20  );.    }.    rc 
365a7 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  = SQLITE_OK;.  }
365a8 0a 0a 20 65 72 72 3a 0a 20 20 64 61 74 61 42 75  .. err:.  dataBu
365a9 66 66 65 72 44 65 73 74 72 6f 79 28 26 64 6f 63  fferDestroy(&doc
365aa 6c 69 73 74 29 3b 0a 20 20 72 65 74 75 72 6e 20  list);.  return 
365ab 72 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  rc;.}../********
365ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
365ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
365ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
365af 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 55 73 65  ********/./* Use
365b0 64 20 74 6f 20 68 6f 6c 64 20 68 61 73 68 74 61  d to hold hashta
365b1 62 6c 65 20 64 61 74 61 20 66 6f 72 20 73 6f 72  ble data for sor
365b2 74 69 6e 67 2e 20 2a 2f 0a 74 79 70 65 64 65 66  ting. */.typedef
365b3 20 73 74 72 75 63 74 20 54 65 72 6d 44 61 74 61   struct TermData
365b4 20 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20   {.  const char 
365b5 2a 70 54 65 72 6d 3b 0a 20 20 69 6e 74 20 6e 54  *pTerm;.  int nT
365b6 65 72 6d 3b 0a 20 20 44 4c 43 6f 6c 6c 65 63 74  erm;.  DLCollect
365b7 6f 72 20 2a 70 43 6f 6c 6c 65 63 74 6f 72 3b 0a  or *pCollector;.
365b8 7d 20 54 65 72 6d 44 61 74 61 3b 0a 0a 2f 2a 20  } TermData;../* 
365b9 4f 72 64 65 72 73 20 54 65 72 6d 44 61 74 61 20  Orders TermData 
365ba 65 6c 65 6d 65 6e 74 73 20 69 6e 20 73 74 72 63  elements in strc
365bb 6d 70 20 66 61 73 68 69 6f 6e 20 28 20 3c 30 20  mp fashion ( <0 
365bc 66 6f 72 20 6c 65 73 73 2d 74 68 61 6e 2c 20 30  for less-than, 0
365bd 0a 2a 2a 20 66 6f 72 20 65 71 75 61 6c 2c 20 3e  .** for equal, >
365be 30 20 66 6f 72 20 67 72 65 61 74 65 72 2d 74 68  0 for greater-th
365bf 61 6e 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  an)..*/.static i
365c0 6e 74 20 74 65 72 6d 44 61 74 61 43 6d 70 28 63  nt termDataCmp(c
365c1 6f 6e 73 74 20 76 6f 69 64 20 2a 61 76 2c 20 63  onst void *av, c
365c2 6f 6e 73 74 20 76 6f 69 64 20 2a 62 76 29 7b 0a  onst void *bv){.
365c3 20 20 63 6f 6e 73 74 20 54 65 72 6d 44 61 74 61    const TermData
365c4 20 2a 61 20 3d 20 28 63 6f 6e 73 74 20 54 65 72   *a = (const Ter
365c5 6d 44 61 74 61 20 2a 29 61 76 3b 0a 20 20 63 6f  mData *)av;.  co
365c6 6e 73 74 20 54 65 72 6d 44 61 74 61 20 2a 62 20  nst TermData *b 
365c7 3d 20 28 63 6f 6e 73 74 20 54 65 72 6d 44 61 74  = (const TermDat
365c8 61 20 2a 29 62 76 3b 0a 20 20 69 6e 74 20 6e 20  a *)bv;.  int n 
365c9 3d 20 61 2d 3e 6e 54 65 72 6d 3c 62 2d 3e 6e 54  = a->nTerm<b->nT
365ca 65 72 6d 20 3f 20 61 2d 3e 6e 54 65 72 6d 20 3a  erm ? a->nTerm :
365cb 20 62 2d 3e 6e 54 65 72 6d 3b 0a 20 20 69 6e 74   b->nTerm;.  int
365cc 20 63 20 3d 20 6d 65 6d 63 6d 70 28 61 2d 3e 70   c = memcmp(a->p
365cd 54 65 72 6d 2c 20 62 2d 3e 70 54 65 72 6d 2c 20  Term, b->pTerm, 
365ce 6e 29 3b 0a 20 20 69 66 28 20 63 21 3d 30 20 29  n);.  if( c!=0 )
365cf 20 72 65 74 75 72 6e 20 63 3b 0a 20 20 72 65 74   return c;.  ret
365d0 75 72 6e 20 61 2d 3e 6e 54 65 72 6d 2d 62 2d 3e  urn a->nTerm-b->
365d1 6e 54 65 72 6d 3b 0a 7d 0a 0a 2f 2a 20 4f 72 64  nTerm;.}../* Ord
365d2 65 72 20 70 54 65 72 6d 73 20 64 61 74 61 20 62  er pTerms data b
365d3 79 20 74 65 72 6d 2c 20 74 68 65 6e 20 77 72 69  y term, then wri
365d4 74 65 20 61 20 6e 65 77 20 6c 65 76 65 6c 20 30  te a new level 0
365d5 20 73 65 67 6d 65 6e 74 20 75 73 69 6e 67 0a 2a   segment using.*
365d6 2a 20 4c 65 61 66 57 72 69 74 65 72 2e 0a 2a 2f  * LeafWriter..*/
365d7 0a 73 74 61 74 69 63 20 69 6e 74 20 77 72 69 74  .static int writ
365d8 65 5a 65 72 6f 53 65 67 6d 65 6e 74 28 66 75 6c  eZeroSegment(ful
365d9 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 66  ltext_vtab *v, f
365da 74 73 33 48 61 73 68 20 2a 70 54 65 72 6d 73 29  ts3Hash *pTerms)
365db 7b 0a 20 20 66 74 73 33 48 61 73 68 45 6c 65 6d  {.  fts3HashElem
365dc 20 2a 65 3b 0a 20 20 69 6e 74 20 69 64 78 2c 20   *e;.  int idx, 
365dd 72 63 2c 20 69 2c 20 6e 3b 0a 20 20 54 65 72 6d  rc, i, n;.  Term
365de 44 61 74 61 20 2a 70 44 61 74 61 3b 0a 20 20 4c  Data *pData;.  L
365df 65 61 66 57 72 69 74 65 72 20 77 72 69 74 65 72  eafWriter writer
365e0 3b 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 64  ;.  DataBuffer d
365e1 6c 3b 0a 0a 20 20 2f 2a 20 44 65 74 65 72 6d 69  l;..  /* Determi
365e2 6e 65 20 74 68 65 20 6e 65 78 74 20 69 6e 64 65  ne the next inde
365e3 78 20 61 74 20 6c 65 76 65 6c 20 30 2c 20 6d 65  x at level 0, me
365e4 72 67 69 6e 67 20 61 73 20 6e 65 63 65 73 73 61  rging as necessa
365e5 72 79 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 73 65  ry. */.  rc = se
365e6 67 64 69 72 4e 65 78 74 49 6e 64 65 78 28 76 2c  gdirNextIndex(v,
365e7 20 30 2c 20 26 69 64 78 29 3b 0a 20 20 69 66 28   0, &idx);.  if(
365e8 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
365e9 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 6e   return rc;..  n
365ea 20 3d 20 66 74 73 33 48 61 73 68 43 6f 75 6e 74   = fts3HashCount
365eb 28 70 54 65 72 6d 73 29 3b 0a 20 20 70 44 61 74  (pTerms);.  pDat
365ec 61 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  a = sqlite3_mall
365ed 6f 63 28 6e 2a 73 69 7a 65 6f 66 28 54 65 72 6d  oc(n*sizeof(Term
365ee 44 61 74 61 29 29 3b 0a 0a 20 20 66 6f 72 28 69  Data));..  for(i
365ef 20 3d 20 30 2c 20 65 20 3d 20 66 74 73 33 48 61   = 0, e = fts3Ha
365f0 73 68 46 69 72 73 74 28 70 54 65 72 6d 73 29 3b  shFirst(pTerms);
365f1 20 65 3b 20 69 2b 2b 2c 20 65 20 3d 20 66 74 73   e; i++, e = fts
365f2 33 48 61 73 68 4e 65 78 74 28 65 29 29 7b 0a 20  3HashNext(e)){. 
365f3 20 20 20 61 73 73 65 72 74 28 20 69 3c 6e 20 29     assert( i<n )
365f4 3b 0a 20 20 20 20 70 44 61 74 61 5b 69 5d 2e 70  ;.    pData[i].p
365f5 54 65 72 6d 20 3d 20 66 74 73 33 48 61 73 68 4b  Term = fts3HashK
365f6 65 79 28 65 29 3b 0a 20 20 20 20 70 44 61 74 61  ey(e);.    pData
365f7 5b 69 5d 2e 6e 54 65 72 6d 20 3d 20 66 74 73 33  [i].nTerm = fts3
365f8 48 61 73 68 4b 65 79 73 69 7a 65 28 65 29 3b 0a  HashKeysize(e);.
365f9 20 20 20 20 70 44 61 74 61 5b 69 5d 2e 70 43 6f      pData[i].pCo
365fa 6c 6c 65 63 74 6f 72 20 3d 20 66 74 73 33 48 61  llector = fts3Ha
365fb 73 68 44 61 74 61 28 65 29 3b 0a 20 20 7d 0a 20  shData(e);.  }. 
365fc 20 61 73 73 65 72 74 28 20 69 3d 3d 6e 20 29 3b   assert( i==n );
365fd 0a 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73  ..  /* TODO(shes
365fe 73 29 20 53 68 6f 75 6c 64 20 77 65 20 61 6c 6c  s) Should we all
365ff 6f 77 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20  ow user-defined 
36600 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
36601 63 65 73 2c 0a 20 20 2a 2a 20 68 65 72 65 3f 20  ces,.  ** here? 
36602 20 49 20 74 68 69 6e 6b 20 77 65 20 6f 6e 6c 79   I think we only
36603 20 6e 65 65 64 20 74 68 61 74 20 6f 6e 63 65 20   need that once 
36604 77 65 20 73 75 70 70 6f 72 74 20 70 72 65 66 69  we support prefi
36605 78 20 73 65 61 72 63 68 65 73 2e 0a 20 20 2a 2f  x searches..  */
36606 0a 20 20 69 66 28 20 6e 3e 31 20 29 20 71 73 6f  .  if( n>1 ) qso
36607 72 74 28 70 44 61 74 61 2c 20 6e 2c 20 73 69 7a  rt(pData, n, siz
36608 65 6f 66 28 2a 70 44 61 74 61 29 2c 20 74 65 72  eof(*pData), ter
36609 6d 44 61 74 61 43 6d 70 29 3b 0a 0a 20 20 2f 2a  mDataCmp);..  /*
3660a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 52 65 66   TODO(shess) Ref
3660b 61 63 74 6f 72 20 73 6f 20 74 68 61 74 20 77 65  actor so that we
3660c 20 63 61 6e 20 77 72 69 74 65 20 64 69 72 65 63   can write direc
3660d 74 6c 79 20 74 6f 20 74 68 65 20 73 65 67 6d 65  tly to the segme
3660e 6e 74 0a 20 20 2a 2a 20 44 61 74 61 42 75 66 66  nt.  ** DataBuff
3660f 65 72 2c 20 61 73 20 68 61 70 70 65 6e 73 20 66  er, as happens f
36610 6f 72 20 73 65 67 6d 65 6e 74 20 6d 65 72 67 65  or segment merge
36611 73 2e 0a 20 20 2a 2f 0a 20 20 6c 65 61 66 57 72  s..  */.  leafWr
36612 69 74 65 72 49 6e 69 74 28 30 2c 20 69 64 78 2c  iterInit(0, idx,
36613 20 26 77 72 69 74 65 72 29 3b 0a 20 20 64 61 74   &writer);.  dat
36614 61 42 75 66 66 65 72 49 6e 69 74 28 26 64 6c 2c  aBufferInit(&dl,
36615 20 30 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20   0);.  for(i=0; 
36616 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 64  i<n; i++){.    d
36617 61 74 61 42 75 66 66 65 72 52 65 73 65 74 28 26  ataBufferReset(&
36618 64 6c 29 3b 0a 20 20 20 20 64 6c 63 41 64 64 44  dl);.    dlcAddD
36619 6f 63 6c 69 73 74 28 70 44 61 74 61 5b 69 5d 2e  oclist(pData[i].
3661a 70 43 6f 6c 6c 65 63 74 6f 72 2c 20 26 64 6c 29  pCollector, &dl)
3661b 3b 0a 20 20 20 20 72 63 20 3d 20 6c 65 61 66 57  ;.    rc = leafW
3661c 72 69 74 65 72 53 74 65 70 28 76 2c 20 26 77 72  riterStep(v, &wr
3661d 69 74 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20  iter,.          
3661e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 44                pD
3661f 61 74 61 5b 69 5d 2e 70 54 65 72 6d 2c 20 70 44  ata[i].pTerm, pD
36620 61 74 61 5b 69 5d 2e 6e 54 65 72 6d 2c 20 64 6c  ata[i].nTerm, dl
36621 2e 70 44 61 74 61 2c 20 64 6c 2e 6e 44 61 74 61  .pData, dl.nData
36622 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
36623 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
36624 65 72 72 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20  err;.  }.  rc = 
36625 6c 65 61 66 57 72 69 74 65 72 46 69 6e 61 6c 69  leafWriterFinali
36626 7a 65 28 76 2c 20 26 77 72 69 74 65 72 29 3b 0a  ze(v, &writer);.
36627 0a 20 65 72 72 3a 0a 20 20 64 61 74 61 42 75 66  . err:.  dataBuf
36628 66 65 72 44 65 73 74 72 6f 79 28 26 64 6c 29 3b  ferDestroy(&dl);
36629 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
3662a 70 44 61 74 61 29 3b 0a 20 20 6c 65 61 66 57 72  pData);.  leafWr
3662b 69 74 65 72 44 65 73 74 72 6f 79 28 26 77 72 69  iterDestroy(&wri
3662c 74 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  ter);.  return r
3662d 63 3b 0a 7d 0a 0a 2f 2a 20 49 66 20 70 65 6e 64  c;.}../* If pend
3662e 69 6e 67 54 65 72 6d 73 20 68 61 73 20 64 61 74  ingTerms has dat
3662f 61 2c 20 66 72 65 65 20 69 74 2e 20 2a 2f 0a 73  a, free it. */.s
36630 74 61 74 69 63 20 69 6e 74 20 63 6c 65 61 72 50  tatic int clearP
36631 65 6e 64 69 6e 67 54 65 72 6d 73 28 66 75 6c 6c  endingTerms(full
36632 74 65 78 74 5f 76 74 61 62 20 2a 76 29 7b 0a 20  text_vtab *v){. 
36633 20 69 66 28 20 76 2d 3e 6e 50 65 6e 64 69 6e 67   if( v->nPending
36634 44 61 74 61 3e 3d 30 20 29 7b 0a 20 20 20 20 66  Data>=0 ){.    f
36635 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 65 3b 0a  ts3HashElem *e;.
36636 20 20 20 20 66 6f 72 28 65 3d 66 74 73 33 48 61      for(e=fts3Ha
36637 73 68 46 69 72 73 74 28 26 76 2d 3e 70 65 6e 64  shFirst(&v->pend
36638 69 6e 67 54 65 72 6d 73 29 3b 20 65 3b 20 65 3d  ingTerms); e; e=
36639 66 74 73 33 48 61 73 68 4e 65 78 74 28 65 29 29  fts3HashNext(e))
3663a 7b 0a 20 20 20 20 20 20 64 6c 63 44 65 6c 65 74  {.      dlcDelet
3663b 65 28 66 74 73 33 48 61 73 68 44 61 74 61 28 65  e(fts3HashData(e
3663c 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 74  ));.    }.    ft
3663d 73 33 48 61 73 68 43 6c 65 61 72 28 26 76 2d 3e  s3HashClear(&v->
3663e 70 65 6e 64 69 6e 67 54 65 72 6d 73 29 3b 0a 20  pendingTerms);. 
3663f 20 20 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61     v->nPendingDa
36640 74 61 20 3d 20 2d 31 3b 0a 20 20 7d 0a 20 20 72  ta = -1;.  }.  r
36641 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
36642 0a 7d 0a 0a 2f 2a 20 49 66 20 70 65 6e 64 69 6e  .}../* If pendin
36643 67 54 65 72 6d 73 20 68 61 73 20 64 61 74 61 2c  gTerms has data,
36644 20 66 6c 75 73 68 20 69 74 20 74 6f 20 61 20 6c   flush it to a l
36645 65 76 65 6c 2d 7a 65 72 6f 20 73 65 67 6d 65 6e  evel-zero segmen
36646 74 2c 20 61 6e 64 0a 2a 2a 20 66 72 65 65 20 69  t, and.** free i
36647 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t..*/.static int
36648 20 66 6c 75 73 68 50 65 6e 64 69 6e 67 54 65 72   flushPendingTer
36649 6d 73 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  ms(fulltext_vtab
3664a 20 2a 76 29 7b 0a 20 20 69 66 28 20 76 2d 3e 6e   *v){.  if( v->n
3664b 50 65 6e 64 69 6e 67 44 61 74 61 3e 3d 30 20 29  PendingData>=0 )
3664c 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 77  {.    int rc = w
3664d 72 69 74 65 5a 65 72 6f 53 65 67 6d 65 6e 74 28  riteZeroSegment(
3664e 76 2c 20 26 76 2d 3e 70 65 6e 64 69 6e 67 54 65  v, &v->pendingTe
3664f 72 6d 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63  rms);.    if( rc
36650 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 63 6c  ==SQLITE_OK ) cl
36651 65 61 72 50 65 6e 64 69 6e 67 54 65 72 6d 73 28  earPendingTerms(
36652 76 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72  v);.    return r
36653 63 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  c;.  }.  return 
36654 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
36655 20 49 66 20 70 65 6e 64 69 6e 67 54 65 72 6d 73   If pendingTerms
36656 20 69 73 20 22 74 6f 6f 20 62 69 67 22 2c 20 6f   is "too big", o
36657 72 20 64 6f 63 69 64 20 69 73 20 6f 75 74 20 6f  r docid is out o
36658 66 20 6f 72 64 65 72 2c 20 66 6c 75 73 68 20 69  f order, flush i
36659 74 2e 0a 2a 2a 20 52 65 67 61 72 64 6c 65 73 73  t..** Regardless
3665a 2c 20 62 65 20 63 65 72 74 61 69 6e 20 74 68 61  , be certain tha
3665b 74 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 20 69  t pendingTerms i
3665c 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 66 6f  s initialized fo
3665d 72 20 75 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  r use..*/.static
3665e 20 69 6e 74 20 69 6e 69 74 50 65 6e 64 69 6e 67   int initPending
3665f 54 65 72 6d 73 28 66 75 6c 6c 74 65 78 74 5f 76  Terms(fulltext_v
36660 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f 69  tab *v, sqlite_i
36661 6e 74 36 34 20 69 44 6f 63 69 64 29 7b 0a 20 20  nt64 iDocid){.  
36662 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 45  /* TODO(shess) E
36663 78 70 6c 6f 72 65 20 77 68 65 74 68 65 72 20 70  xplore whether p
36664 61 72 74 69 61 6c 6c 79 20 66 6c 75 73 68 69 6e  artially flushin
36665 67 20 74 68 65 20 62 75 66 66 65 72 20 6f 6e 0a  g the buffer on.
36666 20 20 2a 2a 20 66 6f 72 63 65 64 2d 66 6c 75 73    ** forced-flus
36667 68 20 77 6f 75 6c 64 20 70 72 6f 76 69 64 65 20  h would provide 
36668 62 65 74 74 65 72 20 70 65 72 66 6f 72 6d 61 6e  better performan
36669 63 65 2e 20 20 49 20 73 75 73 70 65 63 74 20 74  ce.  I suspect t
3666a 68 61 74 20 69 66 0a 20 20 2a 2a 20 77 65 20 6f  hat if.  ** we o
3666b 72 64 65 72 65 64 20 74 68 65 20 64 6f 63 6c 69  rdered the docli
3666c 73 74 73 20 62 79 20 73 69 7a 65 20 61 6e 64 20  sts by size and 
3666d 66 6c 75 73 68 65 64 20 74 68 65 20 6c 61 72 67  flushed the larg
3666e 65 73 74 20 75 6e 74 69 6c 20 74 68 65 0a 20 20  est until the.  
3666f 2a 2a 20 62 75 66 66 65 72 20 77 61 73 20 68 61  ** buffer was ha
36670 6c 66 20 65 6d 70 74 79 2c 20 74 68 61 74 20 77  lf empty, that w
36671 6f 75 6c 64 20 6c 65 74 20 74 68 65 20 6c 65 73  ould let the les
36672 73 20 66 72 65 71 75 65 6e 74 20 74 65 72 6d 73  s frequent terms
36673 0a 20 20 2a 2a 20 67 65 6e 65 72 61 74 65 20 6c  .  ** generate l
36674 6f 6e 67 65 72 20 64 6f 63 6c 69 73 74 73 2e 0a  onger doclists..
36675 20 20 2a 2f 0a 20 20 69 66 28 20 69 44 6f 63 69    */.  if( iDoci
36676 64 3c 3d 76 2d 3e 69 50 72 65 76 44 6f 63 69 64  d<=v->iPrevDocid
36677 20 7c 7c 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44   || v->nPendingD
36678 61 74 61 3e 6b 50 65 6e 64 69 6e 67 54 68 72 65  ata>kPendingThre
36679 73 68 6f 6c 64 20 29 7b 0a 20 20 20 20 69 6e 74  shold ){.    int
3667a 20 72 63 20 3d 20 66 6c 75 73 68 50 65 6e 64 69   rc = flushPendi
3667b 6e 67 54 65 72 6d 73 28 76 29 3b 0a 20 20 20 20  ngTerms(v);.    
3667c 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
3667d 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  K ) return rc;. 
3667e 20 7d 0a 20 20 69 66 28 20 76 2d 3e 6e 50 65 6e   }.  if( v->nPen
3667f 64 69 6e 67 44 61 74 61 3c 30 20 29 7b 0a 20 20  dingData<0 ){.  
36680 20 20 66 74 73 33 48 61 73 68 49 6e 69 74 28 26    fts3HashInit(&
36681 76 2d 3e 70 65 6e 64 69 6e 67 54 65 72 6d 73 2c  v->pendingTerms,
36682 20 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49 4e   FTS3_HASH_STRIN
36683 47 2c 20 31 29 3b 0a 20 20 20 20 76 2d 3e 6e 50  G, 1);.    v->nP
36684 65 6e 64 69 6e 67 44 61 74 61 20 3d 20 30 3b 0a  endingData = 0;.
36685 20 20 7d 0a 20 20 76 2d 3e 69 50 72 65 76 44 6f    }.  v->iPrevDo
36686 63 69 64 20 3d 20 69 44 6f 63 69 64 3b 0a 20 20  cid = iDocid;.  
36687 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
36688 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 66 75 6e  ;.}../* This fun
36689 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73  ction implements
3668a 20 74 68 65 20 78 55 70 64 61 74 65 20 63 61 6c   the xUpdate cal
3668b 6c 62 61 63 6b 3b 20 69 74 20 69 73 20 74 68 65  lback; it is the
3668c 20 74 6f 70 2d 6c 65 76 65 6c 20 65 6e 74 72 79   top-level entry
3668d 0a 20 2a 20 70 6f 69 6e 74 20 66 6f 72 20 69 6e  . * point for in
3668e 73 65 72 74 69 6e 67 2c 20 64 65 6c 65 74 69 6e  serting, deletin
3668f 67 20 6f 72 20 75 70 64 61 74 69 6e 67 20 61 20  g or updating a 
36690 72 6f 77 20 69 6e 20 61 20 66 75 6c 6c 2d 74 65  row in a full-te
36691 78 74 20 74 61 62 6c 65 2e 20 2a 2f 0a 73 74 61  xt table. */.sta
36692 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74  tic int fulltext
36693 55 70 64 61 74 65 28 73 71 6c 69 74 65 33 5f 76  Update(sqlite3_v
36694 74 61 62 20 2a 70 56 74 61 62 2c 20 69 6e 74 20  tab *pVtab, int 
36695 6e 41 72 67 2c 20 73 71 6c 69 74 65 33 5f 76 61  nArg, sqlite3_va
36696 6c 75 65 20 2a 2a 70 70 41 72 67 2c 0a 20 20 20  lue **ppArg,.   
36697 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36698 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e         sqlite_in
36699 74 36 34 20 2a 70 52 6f 77 69 64 29 7b 0a 20 20  t64 *pRowid){.  
3669a 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
3669b 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f 76 74 61   = (fulltext_vta
3669c 62 20 2a 29 20 70 56 74 61 62 3b 0a 20 20 69 6e  b *) pVtab;.  in
3669d 74 20 72 63 3b 0a 0a 20 20 46 54 53 54 52 41 43  t rc;..  FTSTRAC
3669e 45 28 28 22 46 54 53 33 20 55 70 64 61 74 65 20  E(("FTS3 Update 
3669f 25 70 5c 6e 22 2c 20 70 56 74 61 62 29 29 3b 0a  %p\n", pVtab));.
366a0 0a 20 20 69 66 28 20 6e 41 72 67 3c 32 20 29 7b  .  if( nArg<2 ){
366a1 0a 20 20 20 20 72 63 20 3d 20 69 6e 64 65 78 5f  .    rc = index_
366a2 64 65 6c 65 74 65 28 76 2c 20 73 71 6c 69 74 65  delete(v, sqlite
366a3 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 70 70  3_value_int64(pp
366a4 41 72 67 5b 30 5d 29 29 3b 0a 20 20 20 20 69 66  Arg[0]));.    if
366a5 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
366a6 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 77  ){.      /* If w
366a7 65 20 6a 75 73 74 20 64 65 6c 65 74 65 64 20 74  e just deleted t
366a8 68 65 20 6c 61 73 74 20 72 6f 77 20 69 6e 20 74  he last row in t
366a9 68 65 20 74 61 62 6c 65 2c 20 63 6c 65 61 72 20  he table, clear 
366aa 6f 75 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a  out the.      **
366ab 20 69 6e 64 65 78 20 64 61 74 61 2e 0a 20 20 20   index data..   
366ac 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d     */.      rc =
366ad 20 63 6f 6e 74 65 6e 74 5f 65 78 69 73 74 73 28   content_exists(
366ae 76 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  v);.      if( rc
366af 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a  ==SQLITE_ROW ){.
366b0 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
366b1 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 65  ITE_OK;.      }e
366b2 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  lse if( rc==SQLI
366b3 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20  TE_DONE ){.     
366b4 20 20 20 2f 2a 20 43 6c 65 61 72 20 74 68 65 20     /* Clear the 
366b5 70 65 6e 64 69 6e 67 20 74 65 72 6d 73 20 73 6f  pending terms so
366b6 20 77 65 20 64 6f 6e 27 74 20 66 6c 75 73 68 20   we don't flush 
366b7 61 20 75 73 65 6c 65 73 73 20 6c 65 76 65 6c 2d  a useless level-
366b8 30 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 65 67  0.        ** seg
366b9 6d 65 6e 74 20 77 68 65 6e 20 74 68 65 20 74 72  ment when the tr
366ba 61 6e 73 61 63 74 69 6f 6e 20 63 6c 6f 73 65 73  ansaction closes
366bb 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
366bc 20 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 50       rc = clearP
366bd 65 6e 64 69 6e 67 54 65 72 6d 73 28 76 29 3b 0a  endingTerms(v);.
366be 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
366bf 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
366c0 20 20 20 20 20 20 20 72 63 20 3d 20 73 65 67 64         rc = segd
366c1 69 72 5f 64 65 6c 65 74 65 5f 61 6c 6c 28 76 29  ir_delete_all(v)
366c2 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
366c3 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 20 65 6c    }.    }.  } el
366c4 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76  se if( sqlite3_v
366c5 61 6c 75 65 5f 74 79 70 65 28 70 70 41 72 67 5b  alue_type(ppArg[
366c6 30 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 4e 55  0]) != SQLITE_NU
366c7 4c 4c 20 29 7b 0a 20 20 20 20 2f 2a 20 41 6e 20  LL ){.    /* An 
366c8 75 70 64 61 74 65 3a 0a 20 20 20 20 20 2a 20 70  update:.     * p
366c9 70 41 72 67 5b 30 5d 20 3d 20 6f 6c 64 20 72 6f  pArg[0] = old ro
366ca 77 69 64 0a 20 20 20 20 20 2a 20 70 70 41 72 67  wid.     * ppArg
366cb 5b 31 5d 20 3d 20 6e 65 77 20 72 6f 77 69 64 0a  [1] = new rowid.
366cc 20 20 20 20 20 2a 20 70 70 41 72 67 5b 32 2e 2e       * ppArg[2..
366cd 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 2d 31 5d 20  2+v->nColumn-1] 
366ce 3d 20 76 61 6c 75 65 73 0a 20 20 20 20 20 2a 20  = values.     * 
366cf 70 70 41 72 67 5b 32 2b 76 2d 3e 6e 43 6f 6c 75  ppArg[2+v->nColu
366d0 6d 6e 5d 20 3d 20 76 61 6c 75 65 20 66 6f 72 20  mn] = value for 
366d1 6d 61 67 69 63 20 63 6f 6c 75 6d 6e 20 28 77 65  magic column (we
366d2 20 69 67 6e 6f 72 65 20 74 68 69 73 29 0a 20 20   ignore this).  
366d3 20 20 20 2a 20 70 70 41 72 67 5b 32 2b 76 2d 3e     * ppArg[2+v->
366d4 6e 43 6f 6c 75 6d 6e 2b 31 5d 20 3d 20 76 61 6c  nColumn+1] = val
366d5 75 65 20 66 6f 72 20 64 6f 63 69 64 0a 20 20 20  ue for docid.   
366d6 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 5f    */.    sqlite_
366d7 69 6e 74 36 34 20 72 6f 77 69 64 20 3d 20 73 71  int64 rowid = sq
366d8 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36  lite3_value_int6
366d9 34 28 70 70 41 72 67 5b 30 5d 29 3b 0a 20 20 20  4(ppArg[0]);.   
366da 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c   if( sqlite3_val
366db 75 65 5f 74 79 70 65 28 70 70 41 72 67 5b 31 5d  ue_type(ppArg[1]
366dc 29 20 21 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45  ) != SQLITE_INTE
366dd 47 45 52 20 7c 7c 0a 20 20 20 20 20 20 20 20 73  GER ||.        s
366de 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
366df 36 34 28 70 70 41 72 67 5b 31 5d 29 20 21 3d 20  64(ppArg[1]) != 
366e0 72 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 72  rowid ){.      r
366e1 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
366e2 3b 20 20 2f 2a 20 77 65 20 64 6f 6e 27 74 20 61  ;  /* we don't a
366e3 6c 6c 6f 77 20 63 68 61 6e 67 69 6e 67 20 74 68  llow changing th
366e4 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20 20 20 7d  e rowid */.    }
366e5 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33  else if( sqlite3
366e6 5f 76 61 6c 75 65 5f 74 79 70 65 28 70 70 41 72  _value_type(ppAr
366e7 67 5b 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31  g[2+v->nColumn+1
366e8 5d 29 20 21 3d 20 53 51 4c 49 54 45 5f 49 4e 54  ]) != SQLITE_INT
366e9 45 47 45 52 20 7c 7c 0a 20 20 20 20 20 20 20 20  EGER ||.        
366ea 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61        sqlite3_va
366eb 6c 75 65 5f 69 6e 74 36 34 28 70 70 41 72 67 5b  lue_int64(ppArg[
366ec 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 5d 29  2+v->nColumn+1])
366ed 20 21 3d 20 72 6f 77 69 64 20 29 7b 0a 20 20 20   != rowid ){.   
366ee 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
366ef 52 52 4f 52 3b 20 20 2f 2a 20 77 65 20 64 6f 6e  RROR;  /* we don
366f0 27 74 20 61 6c 6c 6f 77 20 63 68 61 6e 67 69 6e  't allow changin
366f1 67 20 74 68 65 20 64 6f 63 69 64 20 2a 2f 0a 20  g the docid */. 
366f2 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
366f3 61 73 73 65 72 74 28 20 6e 41 72 67 3d 3d 32 2b  assert( nArg==2+
366f4 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b 32 29 3b 0a 20  v->nColumn+2);. 
366f5 20 20 20 20 20 72 63 20 3d 20 69 6e 64 65 78 5f       rc = index_
366f6 75 70 64 61 74 65 28 76 2c 20 72 6f 77 69 64 2c  update(v, rowid,
366f7 20 26 70 70 41 72 67 5b 32 5d 29 3b 0a 20 20 20   &ppArg[2]);.   
366f8 20 7d 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20   }.  } else {.  
366f9 20 20 2f 2a 20 41 6e 20 69 6e 73 65 72 74 3a 0a    /* An insert:.
366fa 20 20 20 20 20 2a 20 70 70 41 72 67 5b 31 5d 20       * ppArg[1] 
366fb 3d 20 72 65 71 75 65 73 74 65 64 20 72 6f 77 69  = requested rowi
366fc 64 0a 20 20 20 20 20 2a 20 70 70 41 72 67 5b 32  d.     * ppArg[2
366fd 2e 2e 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 2d 31  ..2+v->nColumn-1
366fe 5d 20 3d 20 76 61 6c 75 65 73 0a 20 20 20 20 20  ] = values.     
366ff 2a 20 70 70 41 72 67 5b 32 2b 76 2d 3e 6e 43 6f  * ppArg[2+v->nCo
36700 6c 75 6d 6e 5d 20 3d 20 76 61 6c 75 65 20 66 6f  lumn] = value fo
36701 72 20 6d 61 67 69 63 20 63 6f 6c 75 6d 6e 20 28  r magic column (
36702 77 65 20 69 67 6e 6f 72 65 20 74 68 69 73 29 0a  we ignore this).
36703 20 20 20 20 20 2a 20 70 70 41 72 67 5b 32 2b 76       * ppArg[2+v
36704 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 5d 20 3d 20 76  ->nColumn+1] = v
36705 61 6c 75 65 20 66 6f 72 20 64 6f 63 69 64 0a 20  alue for docid. 
36706 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74      */.    sqlit
36707 65 33 5f 76 61 6c 75 65 20 2a 70 52 65 71 75 65  e3_value *pReque
36708 73 74 44 6f 63 69 64 20 3d 20 70 70 41 72 67 5b  stDocid = ppArg[
36709 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 5d 3b  2+v->nColumn+1];
3670a 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 41 72  .    assert( nAr
3670b 67 3d 3d 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b  g==2+v->nColumn+
3670c 32 29 3b 0a 20 20 20 20 69 66 28 20 53 51 4c 49  2);.    if( SQLI
3670d 54 45 5f 4e 55 4c 4c 20 21 3d 20 73 71 6c 69 74  TE_NULL != sqlit
3670e 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 70 52  e3_value_type(pR
3670f 65 71 75 65 73 74 44 6f 63 69 64 29 20 26 26 0a  equestDocid) &&.
36710 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4e          SQLITE_N
36711 55 4c 4c 20 21 3d 20 73 71 6c 69 74 65 33 5f 76  ULL != sqlite3_v
36712 61 6c 75 65 5f 74 79 70 65 28 70 70 41 72 67 5b  alue_type(ppArg[
36713 31 5d 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  1]) ){.      /* 
36714 54 4f 44 4f 28 73 68 65 73 73 29 20 43 6f 6e 73  TODO(shess) Cons
36715 69 64 65 72 20 61 6c 6c 6f 77 69 6e 67 20 74 68  ider allowing th
36716 69 73 20 74 6f 20 77 6f 72 6b 20 69 66 20 74 68  is to work if th
36717 65 20 76 61 6c 75 65 73 20 61 72 65 0a 20 20 20  e values are.   
36718 20 20 20 2a 2a 20 69 64 65 6e 74 69 63 61 6c 2e     ** identical.
36719 20 20 49 27 6d 20 69 6e 63 6c 69 6e 65 64 20 74    I'm inclined t
3671a 6f 20 64 69 73 63 6f 75 72 61 67 65 20 74 68 61  o discourage tha
3671b 74 20 75 73 61 67 65 2c 20 74 68 6f 75 67 68 2c  t usage, though,
3671c 0a 20 20 20 20 20 20 2a 2a 20 67 69 76 65 6e 20  .      ** given 
3671d 74 68 61 74 20 62 6f 74 68 20 72 6f 77 69 64 20  that both rowid 
3671e 61 6e 64 20 64 6f 63 69 64 20 61 72 65 20 73 70  and docid are sp
3671f 65 63 69 61 6c 20 63 6f 6c 75 6d 6e 73 2e 20 20  ecial columns.  
36720 42 65 74 74 65 72 0a 20 20 20 20 20 20 2a 2a 20  Better.      ** 
36721 77 6f 75 6c 64 20 62 65 20 74 6f 20 64 65 66 69  would be to defi
36722 6e 65 20 6f 6e 65 20 6f 72 20 74 68 65 20 6f 74  ne one or the ot
36723 68 65 72 20 61 73 20 74 68 65 20 64 65 66 61 75  her as the defau
36724 6c 74 20 77 69 6e 6e 65 72 2c 0a 20 20 20 20 20  lt winner,.     
36725 20 2a 2a 20 62 75 74 20 73 68 6f 75 6c 64 20 69   ** but should i
36726 74 20 62 65 20 66 74 73 33 2d 63 65 6e 74 72 69  t be fts3-centri
36727 63 20 28 64 6f 63 69 64 29 20 6f 72 20 53 51 4c  c (docid) or SQL
36728 69 74 65 2d 63 65 6e 74 72 69 63 0a 20 20 20 20  ite-centric.    
36729 20 20 2a 2a 20 28 72 6f 77 69 64 29 3f 0a 20 20    ** (rowid)?.  
3672a 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20      */.      rc 
3672b 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
3672c 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
3672d 20 69 66 28 20 53 51 4c 49 54 45 5f 4e 55 4c 4c   if( SQLITE_NULL
3672e 20 3d 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   == sqlite3_valu
3672f 65 5f 74 79 70 65 28 70 52 65 71 75 65 73 74 44  e_type(pRequestD
36730 6f 63 69 64 29 20 29 7b 0a 20 20 20 20 20 20 20  ocid) ){.       
36731 20 70 52 65 71 75 65 73 74 44 6f 63 69 64 20 3d   pRequestDocid =
36732 20 70 70 41 72 67 5b 31 5d 3b 0a 20 20 20 20 20   ppArg[1];.     
36733 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 69 6e   }.      rc = in
36734 64 65 78 5f 69 6e 73 65 72 74 28 76 2c 20 70 52  dex_insert(v, pR
36735 65 71 75 65 73 74 44 6f 63 69 64 2c 20 26 70 70  equestDocid, &pp
36736 41 72 67 5b 32 5d 2c 20 70 52 6f 77 69 64 29 3b  Arg[2], pRowid);
36737 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65  .    }.  }..  re
36738 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74  turn rc;.}..stat
36739 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 53  ic int fulltextS
3673a 79 6e 63 28 73 71 6c 69 74 65 33 5f 76 74 61 62  ync(sqlite3_vtab
3673b 20 2a 70 56 74 61 62 29 7b 0a 20 20 46 54 53 54   *pVtab){.  FTST
3673c 52 41 43 45 28 28 22 46 54 53 33 20 78 53 79 6e  RACE(("FTS3 xSyn
3673d 63 28 29 5c 6e 22 29 29 3b 0a 20 20 72 65 74 75  c()\n"));.  retu
3673e 72 6e 20 66 6c 75 73 68 50 65 6e 64 69 6e 67 54  rn flushPendingT
3673f 65 72 6d 73 28 28 66 75 6c 6c 74 65 78 74 5f 76  erms((fulltext_v
36740 74 61 62 20 2a 29 70 56 74 61 62 29 3b 0a 7d 0a  tab *)pVtab);.}.
36741 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c  .static int full
36742 74 65 78 74 42 65 67 69 6e 28 73 71 6c 69 74 65  textBegin(sqlite
36743 33 5f 76 74 61 62 20 2a 70 56 74 61 62 29 7b 0a  3_vtab *pVtab){.
36744 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20    fulltext_vtab 
36745 2a 76 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f 76  *v = (fulltext_v
36746 74 61 62 20 2a 29 20 70 56 74 61 62 3b 0a 20 20  tab *) pVtab;.  
36747 46 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20  FTSTRACE(("FTS3 
36748 78 42 65 67 69 6e 28 29 5c 6e 22 29 29 3b 0a 0a  xBegin()\n"));..
36749 20 20 2f 2a 20 41 6e 79 20 62 75 66 66 65 72 65    /* Any buffere
3674a 64 20 75 70 64 61 74 65 73 20 73 68 6f 75 6c 64  d updates should
3674b 20 68 61 76 65 20 62 65 65 6e 20 63 6c 65 61 72   have been clear
3674c 65 64 20 62 79 20 74 68 65 20 70 72 65 76 69 6f  ed by the previo
3674d 75 73 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74  us.  ** transact
3674e 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ion..  */.  asse
3674f 72 74 28 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44  rt( v->nPendingD
36750 61 74 61 3c 30 20 29 3b 0a 20 20 72 65 74 75 72  ata<0 );.  retur
36751 6e 20 63 6c 65 61 72 50 65 6e 64 69 6e 67 54 65  n clearPendingTe
36752 72 6d 73 28 76 29 3b 0a 7d 0a 0a 73 74 61 74 69  rms(v);.}..stati
36753 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 43 6f  c int fulltextCo
36754 6d 6d 69 74 28 73 71 6c 69 74 65 33 5f 76 74 61  mmit(sqlite3_vta
36755 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 66 75 6c  b *pVtab){.  ful
36756 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 20 3d 20  ltext_vtab *v = 
36757 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
36758 29 20 70 56 74 61 62 3b 0a 20 20 46 54 53 54 52  ) pVtab;.  FTSTR
36759 41 43 45 28 28 22 46 54 53 33 20 78 43 6f 6d 6d  ACE(("FTS3 xComm
3675a 69 74 28 29 5c 6e 22 29 29 3b 0a 0a 20 20 2f 2a  it()\n"));..  /*
3675b 20 42 75 66 66 65 72 65 64 20 75 70 64 61 74 65   Buffered update
3675c 73 20 73 68 6f 75 6c 64 20 68 61 76 65 20 62 65  s should have be
3675d 65 6e 20 63 6c 65 61 72 65 64 20 62 79 20 66 75  en cleared by fu
3675e 6c 6c 74 65 78 74 53 79 6e 63 28 29 2e 20 2a 2f  lltextSync(). */
3675f 0a 20 20 61 73 73 65 72 74 28 20 76 2d 3e 6e 50  .  assert( v->nP
36760 65 6e 64 69 6e 67 44 61 74 61 3c 30 20 29 3b 0a  endingData<0 );.
36761 20 20 72 65 74 75 72 6e 20 63 6c 65 61 72 50 65    return clearPe
36762 6e 64 69 6e 67 54 65 72 6d 73 28 76 29 3b 0a 7d  ndingTerms(v);.}
36763 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c  ..static int ful
36764 6c 74 65 78 74 52 6f 6c 6c 62 61 63 6b 28 73 71  ltextRollback(sq
36765 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
36766 62 29 7b 0a 20 20 46 54 53 54 52 41 43 45 28 28  b){.  FTSTRACE((
36767 22 46 54 53 33 20 78 52 6f 6c 6c 62 61 63 6b 28  "FTS3 xRollback(
36768 29 5c 6e 22 29 29 3b 0a 20 20 72 65 74 75 72 6e  )\n"));.  return
36769 20 63 6c 65 61 72 50 65 6e 64 69 6e 67 54 65 72   clearPendingTer
3676a 6d 73 28 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  ms((fulltext_vta
3676b 62 20 2a 29 70 56 74 61 62 29 3b 0a 7d 0a 0a 2f  b *)pVtab);.}../
3676c 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
3676d 69 6f 6e 20 6f 66 20 74 68 65 20 73 6e 69 70 70  ion of the snipp
3676e 65 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f  et() function fo
3676f 72 20 46 54 53 33 0a 2a 2f 0a 73 74 61 74 69 63  r FTS3.*/.static
36770 20 76 6f 69 64 20 73 6e 69 70 70 65 74 46 75 6e   void snippetFun
36771 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
36772 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c 0a  text *pContext,.
36773 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
36774 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
36775 67 76 0a 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74  gv.){.  fulltext
36776 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  _cursor *pCursor
36777 3b 0a 20 20 69 66 28 20 61 72 67 63 3c 31 20 29  ;.  if( argc<1 )
36778 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 73   return;.  if( s
36779 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
3677a 65 28 61 72 67 76 5b 30 5d 29 21 3d 53 51 4c 49  e(argv[0])!=SQLI
3677b 54 45 5f 42 4c 4f 42 20 7c 7c 0a 20 20 20 20 20  TE_BLOB ||.     
3677c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
3677d 79 74 65 73 28 61 72 67 76 5b 30 5d 29 21 3d 73  ytes(argv[0])!=s
3677e 69 7a 65 6f 66 28 70 43 75 72 73 6f 72 29 20 29  izeof(pCursor) )
3677f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
36780 73 75 6c 74 5f 65 72 72 6f 72 28 70 43 6f 6e 74  sult_error(pCont
36781 65 78 74 2c 20 22 69 6c 6c 65 67 61 6c 20 66 69  ext, "illegal fi
36782 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
36783 68 74 6d 6c 5f 73 6e 69 70 70 65 74 22 2c 2d 31  html_snippet",-1
36784 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
36785 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 61  const char *zSta
36786 72 74 20 3d 20 22 3c 62 3e 22 3b 0a 20 20 20 20  rt = "<b>";.    
36787 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 6e 64  const char *zEnd
36788 20 3d 20 22 3c 2f 62 3e 22 3b 0a 20 20 20 20 63   = "</b>";.    c
36789 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 6c 6c 69  onst char *zElli
3678a 70 73 69 73 20 3d 20 22 3c 62 3e 2e 2e 2e 3c 2f  psis = "<b>...</
3678b 62 3e 22 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  b>";.    memcpy(
3678c 26 70 43 75 72 73 6f 72 2c 20 73 71 6c 69 74 65  &pCursor, sqlite
3678d 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67  3_value_blob(arg
3678e 76 5b 30 5d 29 2c 20 73 69 7a 65 6f 66 28 70 43  v[0]), sizeof(pC
3678f 75 72 73 6f 72 29 29 3b 0a 20 20 20 20 69 66 28  ursor));.    if(
36790 20 61 72 67 63 3e 3d 32 20 29 7b 0a 20 20 20 20   argc>=2 ){.    
36791 20 20 7a 53 74 61 72 74 20 3d 20 28 63 6f 6e 73    zStart = (cons
36792 74 20 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f  t char*)sqlite3_
36793 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
36794 31 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 61  1]);.      if( a
36795 72 67 63 3e 3d 33 20 29 7b 0a 20 20 20 20 20 20  rgc>=3 ){.      
36796 20 20 7a 45 6e 64 20 3d 20 28 63 6f 6e 73 74 20    zEnd = (const 
36797 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61  char*)sqlite3_va
36798 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 32 5d  lue_text(argv[2]
36799 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 61  );.        if( a
3679a 72 67 63 3e 3d 34 20 29 7b 0a 20 20 20 20 20 20  rgc>=4 ){.      
3679b 20 20 20 20 7a 45 6c 6c 69 70 73 69 73 20 3d 20      zEllipsis = 
3679c 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 6c  (const char*)sql
3679d 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
3679e 61 72 67 76 5b 33 5d 29 3b 0a 20 20 20 20 20 20  argv[3]);.      
3679f 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
367a0 7d 0a 20 20 20 20 73 6e 69 70 70 65 74 41 6c 6c  }.    snippetAll
367a1 4f 66 66 73 65 74 73 28 70 43 75 72 73 6f 72 29  Offsets(pCursor)
367a2 3b 0a 20 20 20 20 73 6e 69 70 70 65 74 54 65 78  ;.    snippetTex
367a3 74 28 70 43 75 72 73 6f 72 2c 20 7a 53 74 61 72  t(pCursor, zStar
367a4 74 2c 20 7a 45 6e 64 2c 20 7a 45 6c 6c 69 70 73  t, zEnd, zEllips
367a5 69 73 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  is);.    sqlite3
367a6 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43 6f  _result_text(pCo
367a7 6e 74 65 78 74 2c 20 70 43 75 72 73 6f 72 2d 3e  ntext, pCursor->
367a8 73 6e 69 70 70 65 74 2e 7a 53 6e 69 70 70 65 74  snippet.zSnippet
367a9 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
367aa 20 20 20 20 20 20 20 20 20 20 70 43 75 72 73 6f            pCurso
367ab 72 2d 3e 73 6e 69 70 70 65 74 2e 6e 53 6e 69 70  r->snippet.nSnip
367ac 70 65 74 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  pet, SQLITE_STAT
367ad 49 43 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  IC);.  }.}../*.*
367ae 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
367af 20 6f 66 20 74 68 65 20 6f 66 66 73 65 74 73 28   of the offsets(
367b0 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 46  ) function for F
367b1 54 53 33 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  TS3.*/.static vo
367b2 69 64 20 73 6e 69 70 70 65 74 4f 66 66 73 65 74  id snippetOffset
367b3 73 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  sFunc(.  sqlite3
367b4 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65  _context *pConte
367b5 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
367b6 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
367b7 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 66 75 6c 6c  **argv.){.  full
367b8 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 70 43 75  text_cursor *pCu
367b9 72 73 6f 72 3b 0a 20 20 69 66 28 20 61 72 67 63  rsor;.  if( argc
367ba 3c 31 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69  <1 ) return;.  i
367bb 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f( sqlite3_value
367bc 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 21 3d  _type(argv[0])!=
367bd 53 51 4c 49 54 45 5f 42 4c 4f 42 20 7c 7c 0a 20  SQLITE_BLOB ||. 
367be 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c       sqlite3_val
367bf 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d  ue_bytes(argv[0]
367c0 29 21 3d 73 69 7a 65 6f 66 28 70 43 75 72 73 6f  )!=sizeof(pCurso
367c1 72 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  r) ){.    sqlite
367c2 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 70  3_result_error(p
367c3 43 6f 6e 74 65 78 74 2c 20 22 69 6c 6c 65 67 61  Context, "illega
367c4 6c 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  l first argument
367c5 20 74 6f 20 6f 66 66 73 65 74 73 22 2c 2d 31 29   to offsets",-1)
367c6 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d  ;.  }else{.    m
367c7 65 6d 63 70 79 28 26 70 43 75 72 73 6f 72 2c 20  emcpy(&pCursor, 
367c8 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
367c9 6f 62 28 61 72 67 76 5b 30 5d 29 2c 20 73 69 7a  ob(argv[0]), siz
367ca 65 6f 66 28 70 43 75 72 73 6f 72 29 29 3b 0a 20  eof(pCursor));. 
367cb 20 20 20 73 6e 69 70 70 65 74 41 6c 6c 4f 66 66     snippetAllOff
367cc 73 65 74 73 28 70 43 75 72 73 6f 72 29 3b 0a 20  sets(pCursor);. 
367cd 20 20 20 73 6e 69 70 70 65 74 4f 66 66 73 65 74     snippetOffset
367ce 54 65 78 74 28 26 70 43 75 72 73 6f 72 2d 3e 73  Text(&pCursor->s
367cf 6e 69 70 70 65 74 29 3b 0a 20 20 20 20 73 71 6c  nippet);.    sql
367d0 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
367d1 28 70 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20  (pContext,.     
367d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
367d3 20 20 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70     pCursor->snip
367d4 70 65 74 2e 7a 4f 66 66 73 65 74 2c 20 70 43 75  pet.zOffset, pCu
367d5 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74 2e 6e 4f  rsor->snippet.nO
367d6 66 66 73 65 74 2c 0a 20 20 20 20 20 20 20 20 20  ffset,.         
367d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
367d8 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
367d9 20 7d 0a 7d 0a 0a 2f 2a 20 4f 70 74 4c 65 61 76   }.}../* OptLeav
367da 65 73 52 65 61 64 65 72 20 69 73 20 6e 65 61 72  esReader is near
367db 6c 79 20 69 64 65 6e 74 69 63 61 6c 20 74 6f 20  ly identical to 
367dc 4c 65 61 76 65 73 52 65 61 64 65 72 2c 20 65 78  LeavesReader, ex
367dd 63 65 70 74 20 74 68 61 74 0a 2a 2a 20 77 68 65  cept that.** whe
367de 72 65 20 4c 65 61 76 65 73 52 65 61 64 65 72 20  re LeavesReader 
367df 69 73 20 67 65 61 72 65 64 20 74 6f 77 61 72 64  is geared toward
367e0 73 20 74 68 65 20 6d 65 72 67 69 6e 67 20 6f 66  s the merging of
367e1 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 73 65 67   complete.** seg
367e2 6d 65 6e 74 20 6c 65 76 65 6c 73 20 28 77 69 74  ment levels (wit
367e3 68 20 65 78 61 63 74 6c 79 20 4d 45 52 47 45 5f  h exactly MERGE_
367e4 43 4f 55 4e 54 20 73 65 67 6d 65 6e 74 73 29 2c  COUNT segments),
367e5 20 4f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72   OptLeavesReader
367e6 0a 2a 2a 20 69 73 20 67 65 61 72 65 64 20 74 6f  .** is geared to
367e7 77 61 72 64 73 20 69 6d 70 6c 65 6d 65 6e 74 61  wards implementa
367e8 74 69 6f 6e 20 6f 66 20 74 68 65 20 6f 70 74 69  tion of the opti
367e9 6d 69 7a 65 28 29 20 66 75 6e 63 74 69 6f 6e 2c  mize() function,
367ea 20 61 6e 64 0a 2a 2a 20 63 61 6e 20 6d 65 72 67   and.** can merg
367eb 65 20 61 6c 6c 20 73 65 67 6d 65 6e 74 73 20 73  e all segments s
367ec 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 2e 20 20  imultaneously.  
367ed 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6d 61 79  This version may
367ee 20 62 65 0a 2a 2a 20 73 6f 6d 65 77 68 61 74 20   be.** somewhat 
367ef 6c 65 73 73 20 65 66 66 69 63 69 65 6e 74 20 74  less efficient t
367f0 68 61 6e 20 4c 65 61 76 65 73 52 65 61 64 65 72  han LeavesReader
367f1 20 62 65 63 61 75 73 65 20 69 74 20 6d 65 72 67   because it merg
367f2 65 73 20 69 6e 74 6f 20 61 6e 0a 2a 2a 20 61 63  es into an.** ac
367f3 63 75 6d 75 6c 61 74 6f 72 20 72 61 74 68 65 72  cumulator rather
367f4 20 74 68 61 6e 20 64 6f 69 6e 67 20 61 6e 20 4e   than doing an N
367f5 2d 77 61 79 20 6d 65 72 67 65 2c 20 62 75 74 20  -way merge, but 
367f6 73 69 6e 63 65 20 73 65 67 6d 65 6e 74 0a 2a 2a  since segment.**
367f7 20 73 69 7a 65 20 67 72 6f 77 73 20 65 78 70 6f   size grows expo
367f8 6e 65 6e 74 69 61 6c 6c 79 20 28 73 6f 20 73 65  nentially (so se
367f9 67 6d 65 6e 74 20 63 6f 75 6e 74 20 6c 6f 67 72  gment count logr
367fa 69 74 68 6d 69 63 61 6c 6c 79 29 20 74 68 69 73  ithmically) this
367fb 20 69 73 0a 2a 2a 20 70 72 6f 62 61 62 6c 79 20   is.** probably 
367fc 6e 6f 74 20 61 6e 20 69 6d 6d 65 64 69 61 74 65  not an immediate
367fd 20 70 72 6f 62 6c 65 6d 2e 0a 2a 2f 0a 2f 2a 20   problem..*/./* 
367fe 54 4f 44 4f 28 73 68 65 73 73 29 3a 20 50 72 6f  TODO(shess): Pro
367ff 76 65 20 74 68 61 74 20 61 73 73 65 72 74 69 6f  ve that assertio
36800 6e 2c 20 6f 72 20 65 78 74 65 6e 64 20 74 68 65  n, or extend the
36801 20 6d 65 72 67 65 20 63 6f 64 65 20 74 6f 0a 2a   merge code to.*
36802 2a 20 6d 65 72 67 65 20 74 72 65 65 20 66 61 73  * merge tree fas
36803 68 69 6f 6e 20 28 6c 69 6b 65 20 74 68 65 20 70  hion (like the p
36804 72 65 66 69 78 2d 73 65 61 72 63 68 69 6e 67 20  refix-searching 
36805 63 6f 64 65 20 64 6f 65 73 29 2e 0a 2a 2f 0a 2f  code does)..*/./
36806 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 3a 20 4f  * TODO(shess): O
36807 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 20 61  ptLeavesReader a
36808 6e 64 20 4c 65 61 76 65 73 52 65 61 64 65 72 20  nd LeavesReader 
36809 63 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79 20 62  could probably b
3680a 65 0a 2a 2a 20 6d 65 72 67 65 64 20 77 69 74 68  e.** merged with
3680b 20 6c 69 74 74 6c 65 20 6f 72 20 6e 6f 20 6c 6f   little or no lo
3680c 73 73 20 6f 66 20 70 65 72 66 6f 72 6d 61 6e 63  ss of performanc
3680d 65 20 66 6f 72 20 4c 65 61 76 65 73 52 65 61 64  e for LeavesRead
3680e 65 72 2e 20 20 54 68 65 0a 2a 2a 20 6d 65 72 67  er.  The.** merg
3680f 65 64 20 63 6f 64 65 20 77 6f 75 6c 64 20 6e 65  ed code would ne
36810 65 64 20 74 6f 20 68 61 6e 64 6c 65 20 3e 4d 45  ed to handle >ME
36811 52 47 45 5f 43 4f 55 4e 54 20 73 65 67 6d 65 6e  RGE_COUNT segmen
36812 74 73 2c 20 61 6e 64 20 77 6f 75 6c 64 0a 2a 2a  ts, and would.**
36813 20 61 6c 73 6f 20 6e 65 65 64 20 74 6f 20 62 65   also need to be
36814 20 61 62 6c 65 20 74 6f 20 6f 70 74 69 6f 6e 61   able to optiona
36815 6c 6c 79 20 6f 70 74 69 6d 69 7a 65 20 61 77 61  lly optimize awa
36816 79 20 64 65 6c 65 74 65 73 2e 0a 2a 2f 0a 74 79  y deletes..*/.ty
36817 70 65 64 65 66 20 73 74 72 75 63 74 20 4f 70 74  pedef struct Opt
36818 4c 65 61 76 65 73 52 65 61 64 65 72 20 7b 0a 20  LeavesReader {. 
36819 20 2f 2a 20 53 65 67 6d 65 6e 74 20 6e 75 6d 62   /* Segment numb
3681a 65 72 2c 20 74 6f 20 6f 72 64 65 72 20 72 65 61  er, to order rea
3681b 64 65 72 73 20 62 79 20 61 67 65 2e 20 2a 2f 0a  ders by age. */.
3681c 20 20 69 6e 74 20 73 65 67 6d 65 6e 74 3b 0a 20    int segment;. 
3681d 20 4c 65 61 76 65 73 52 65 61 64 65 72 20 72 65   LeavesReader re
3681e 61 64 65 72 3b 0a 7d 20 4f 70 74 4c 65 61 76 65  ader;.} OptLeave
3681f 73 52 65 61 64 65 72 3b 0a 0a 73 74 61 74 69 63  sReader;..static
36820 20 69 6e 74 20 6f 70 74 4c 65 61 76 65 73 52 65   int optLeavesRe
36821 61 64 65 72 41 74 45 6e 64 28 4f 70 74 4c 65 61  aderAtEnd(OptLea
36822 76 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64  vesReader *pRead
36823 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 6c 65  er){.  return le
36824 61 76 65 73 52 65 61 64 65 72 41 74 45 6e 64 28  avesReaderAtEnd(
36825 26 70 52 65 61 64 65 72 2d 3e 72 65 61 64 65 72  &pReader->reader
36826 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  );.}.static int 
36827 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 54  optLeavesReaderT
36828 65 72 6d 42 79 74 65 73 28 4f 70 74 4c 65 61 76  ermBytes(OptLeav
36829 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  esReader *pReade
3682a 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 6c 65 61  r){.  return lea
3682b 76 65 73 52 65 61 64 65 72 54 65 72 6d 42 79 74  vesReaderTermByt
3682c 65 73 28 26 70 52 65 61 64 65 72 2d 3e 72 65 61  es(&pReader->rea
3682d 64 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20 63  der);.}.static c
3682e 6f 6e 73 74 20 63 68 61 72 20 2a 6f 70 74 4c 65  onst char *optLe
3682f 61 76 65 73 52 65 61 64 65 72 44 61 74 61 28 4f  avesReaderData(O
36830 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  ptLeavesReader *
36831 70 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74 75  pReader){.  retu
36832 72 6e 20 6c 65 61 76 65 73 52 65 61 64 65 72 44  rn leavesReaderD
36833 61 74 61 28 26 70 52 65 61 64 65 72 2d 3e 72 65  ata(&pReader->re
36834 61 64 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20  ader);.}.static 
36835 69 6e 74 20 6f 70 74 4c 65 61 76 65 73 52 65 61  int optLeavesRea
36836 64 65 72 44 61 74 61 42 79 74 65 73 28 4f 70 74  derDataBytes(Opt
36837 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 70 52  LeavesReader *pR
36838 65 61 64 65 72 29 7b 0a 20 20 72 65 74 75 72 6e  eader){.  return
36839 20 6c 65 61 76 65 73 52 65 61 64 65 72 44 61 74   leavesReaderDat
3683a 61 42 79 74 65 73 28 26 70 52 65 61 64 65 72 2d  aBytes(&pReader-
3683b 3e 72 65 61 64 65 72 29 3b 0a 7d 0a 73 74 61 74  >reader);.}.stat
3683c 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 6f  ic const char *o
3683d 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 54 65  ptLeavesReaderTe
3683e 72 6d 28 4f 70 74 4c 65 61 76 65 73 52 65 61 64  rm(OptLeavesRead
3683f 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
36840 72 65 74 75 72 6e 20 6c 65 61 76 65 73 52 65 61  return leavesRea
36841 64 65 72 54 65 72 6d 28 26 70 52 65 61 64 65 72  derTerm(&pReader
36842 2d 3e 72 65 61 64 65 72 29 3b 0a 7d 0a 73 74 61  ->reader);.}.sta
36843 74 69 63 20 69 6e 74 20 6f 70 74 4c 65 61 76 65  tic int optLeave
36844 73 52 65 61 64 65 72 53 74 65 70 28 66 75 6c 6c  sReaderStep(full
36845 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 4f 70  text_vtab *v, Op
36846 74 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 70  tLeavesReader *p
36847 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74 75 72  Reader){.  retur
36848 6e 20 6c 65 61 76 65 73 52 65 61 64 65 72 53 74  n leavesReaderSt
36849 65 70 28 76 2c 20 26 70 52 65 61 64 65 72 2d 3e  ep(v, &pReader->
3684a 72 65 61 64 65 72 29 3b 0a 7d 0a 73 74 61 74 69  reader);.}.stati
3684b 63 20 69 6e 74 20 6f 70 74 4c 65 61 76 65 73 52  c int optLeavesR
3684c 65 61 64 65 72 54 65 72 6d 43 6d 70 28 4f 70 74  eaderTermCmp(Opt
3684d 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 6c 72  LeavesReader *lr
3684e 31 2c 20 4f 70 74 4c 65 61 76 65 73 52 65 61 64  1, OptLeavesRead
3684f 65 72 20 2a 6c 72 32 29 7b 0a 20 20 72 65 74 75  er *lr2){.  retu
36850 72 6e 20 6c 65 61 76 65 73 52 65 61 64 65 72 54  rn leavesReaderT
36851 65 72 6d 43 6d 70 28 26 6c 72 31 2d 3e 72 65 61  ermCmp(&lr1->rea
36852 64 65 72 2c 20 26 6c 72 32 2d 3e 72 65 61 64 65  der, &lr2->reade
36853 72 29 3b 0a 7d 0a 2f 2a 20 4f 72 64 65 72 20 62  r);.}./* Order b
36854 79 20 74 65 72 6d 20 61 73 63 65 6e 64 69 6e 67  y term ascending
36855 2c 20 73 65 67 6d 65 6e 74 20 61 73 63 65 6e 64  , segment ascend
36856 69 6e 67 20 28 6f 6c 64 65 73 74 20 74 6f 20 6e  ing (oldest to n
36857 65 77 65 73 74 29 2c 20 77 69 74 68 0a 2a 2a 20  ewest), with.** 
36858 65 78 68 61 75 73 74 65 64 20 72 65 61 64 65 72  exhausted reader
36859 73 20 74 6f 20 74 68 65 20 65 6e 64 2e 0a 2a 2f  s to the end..*/
3685a 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70 74 4c  .static int optL
3685b 65 61 76 65 73 52 65 61 64 65 72 43 6d 70 28 4f  eavesReaderCmp(O
3685c 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  ptLeavesReader *
3685d 6c 72 31 2c 20 4f 70 74 4c 65 61 76 65 73 52 65  lr1, OptLeavesRe
3685e 61 64 65 72 20 2a 6c 72 32 29 7b 0a 20 20 69 6e  ader *lr2){.  in
3685f 74 20 63 20 3d 20 6f 70 74 4c 65 61 76 65 73 52  t c = optLeavesR
36860 65 61 64 65 72 54 65 72 6d 43 6d 70 28 6c 72 31  eaderTermCmp(lr1
36861 2c 20 6c 72 32 29 3b 0a 20 20 69 66 28 20 63 21  , lr2);.  if( c!
36862 3d 30 20 29 20 72 65 74 75 72 6e 20 63 3b 0a 20  =0 ) return c;. 
36863 20 72 65 74 75 72 6e 20 6c 72 31 2d 3e 73 65 67   return lr1->seg
36864 6d 65 6e 74 2d 6c 72 32 2d 3e 73 65 67 6d 65 6e  ment-lr2->segmen
36865 74 3b 0a 7d 0a 2f 2a 20 42 75 62 62 6c 65 20 70  t;.}./* Bubble p
36866 4c 72 5b 30 5d 20 74 6f 20 61 70 70 72 6f 70 72  Lr[0] to appropr
36867 69 61 74 65 20 70 6c 61 63 65 20 69 6e 20 70 4c  iate place in pL
36868 72 5b 31 2e 2e 6e 4c 72 2d 31 5d 2e 20 20 41 73  r[1..nLr-1].  As
36869 73 75 6d 65 73 20 74 68 61 74 0a 2a 2a 20 70 4c  sumes that.** pL
3686a 72 5b 31 2e 2e 6e 4c 72 2d 31 5d 20 69 73 20 61  r[1..nLr-1] is a
3686b 6c 72 65 61 64 79 20 73 6f 72 74 65 64 2e 0a 2a  lready sorted..*
3686c 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 70  /.static void op
3686d 74 4c 65 61 76 65 73 52 65 61 64 65 72 52 65 6f  tLeavesReaderReo
3686e 72 64 65 72 28 4f 70 74 4c 65 61 76 65 73 52 65  rder(OptLeavesRe
3686f 61 64 65 72 20 2a 70 4c 72 2c 20 69 6e 74 20 6e  ader *pLr, int n
36870 4c 72 29 7b 0a 20 20 77 68 69 6c 65 28 20 6e 4c  Lr){.  while( nL
36871 72 3e 31 20 26 26 20 6f 70 74 4c 65 61 76 65 73  r>1 && optLeaves
36872 52 65 61 64 65 72 43 6d 70 28 70 4c 72 2c 20 70  ReaderCmp(pLr, p
36873 4c 72 2b 31 29 3e 30 20 29 7b 0a 20 20 20 20 4f  Lr+1)>0 ){.    O
36874 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 20 74  ptLeavesReader t
36875 6d 70 20 3d 20 70 4c 72 5b 30 5d 3b 0a 20 20 20  mp = pLr[0];.   
36876 20 70 4c 72 5b 30 5d 20 3d 20 70 4c 72 5b 31 5d   pLr[0] = pLr[1]
36877 3b 0a 20 20 20 20 70 4c 72 5b 31 5d 20 3d 20 74  ;.    pLr[1] = t
36878 6d 70 3b 0a 20 20 20 20 6e 4c 72 2d 2d 3b 0a 20  mp;.    nLr--;. 
36879 20 20 20 70 4c 72 2b 2b 3b 0a 20 20 7d 0a 7d 0a     pLr++;.  }.}.
3687a 0a 2f 2a 20 6f 70 74 69 6d 69 7a 65 28 29 20 68  ./* optimize() h
3687b 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 2e 20  elper function. 
3687c 20 50 75 74 20 74 68 65 20 72 65 61 64 65 72 73   Put the readers
3687d 20 69 6e 20 6f 72 64 65 72 20 61 6e 64 20 69 74   in order and it
3687e 65 72 61 74 65 0a 2a 2a 20 74 68 72 6f 75 67 68  erate.** through
3687f 20 74 68 65 6d 2c 20 6d 65 72 67 69 6e 67 20 64   them, merging d
36880 6f 63 6c 69 73 74 73 20 66 6f 72 20 6d 61 74 63  oclists for matc
36881 68 69 6e 67 20 74 65 72 6d 73 20 69 6e 74 6f 20  hing terms into 
36882 70 57 72 69 74 65 72 2e 0a 2a 2a 20 52 65 74 75  pWriter..** Retu
36883 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e  rns SQLITE_OK on
36884 20 73 75 63 63 65 73 73 2c 20 6f 72 20 74 68 65   success, or the
36885 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f   SQLite error co
36886 64 65 20 77 68 69 63 68 0a 2a 2a 20 70 72 65 76  de which.** prev
36887 65 6e 74 65 64 20 73 75 63 63 65 73 73 2e 0a 2a  ented success..*
36888 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70 74  /.static int opt
36889 69 6d 69 7a 65 49 6e 74 65 72 6e 61 6c 28 66 75  imizeInternal(fu
3688a 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 0a  lltext_vtab *v,.
3688b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3688c 20 20 20 20 20 20 20 20 20 20 20 20 4f 70 74 4c              OptL
3688d 65 61 76 65 73 52 65 61 64 65 72 20 2a 72 65 61  eavesReader *rea
3688e 64 65 72 73 2c 20 69 6e 74 20 6e 52 65 61 64 65  ders, int nReade
3688f 72 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  rs,.            
36890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36891 4c 65 61 66 57 72 69 74 65 72 20 2a 70 57 72 69  LeafWriter *pWri
36892 74 65 72 29 7b 0a 20 20 69 6e 74 20 69 2c 20 72  ter){.  int i, r
36893 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
36894 20 44 61 74 61 42 75 66 66 65 72 20 64 6f 63 6c   DataBuffer docl
36895 69 73 74 2c 20 6d 65 72 67 65 64 2c 20 74 6d 70  ist, merged, tmp
36896 3b 0a 0a 20 20 2f 2a 20 4f 72 64 65 72 20 74 68  ;..  /* Order th
36897 65 20 72 65 61 64 65 72 73 2e 20 2a 2f 0a 20 20  e readers. */.  
36898 69 20 3d 20 6e 52 65 61 64 65 72 73 3b 0a 20 20  i = nReaders;.  
36899 77 68 69 6c 65 28 20 69 2d 2d 20 3e 20 30 20 29  while( i-- > 0 )
3689a 7b 0a 20 20 20 20 6f 70 74 4c 65 61 76 65 73 52  {.    optLeavesR
3689b 65 61 64 65 72 52 65 6f 72 64 65 72 28 26 72 65  eaderReorder(&re
3689c 61 64 65 72 73 5b 69 5d 2c 20 6e 52 65 61 64 65  aders[i], nReade
3689d 72 73 2d 69 29 3b 0a 20 20 7d 0a 0a 20 20 64 61  rs-i);.  }..  da
3689e 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 64 6f  taBufferInit(&do
3689f 63 6c 69 73 74 2c 20 4c 45 41 46 5f 4d 41 58 29  clist, LEAF_MAX)
368a0 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 49 6e  ;.  dataBufferIn
368a1 69 74 28 26 6d 65 72 67 65 64 2c 20 4c 45 41 46  it(&merged, LEAF
368a2 5f 4d 41 58 29 3b 0a 0a 20 20 2f 2a 20 45 78 68  _MAX);..  /* Exh
368a3 61 75 73 74 65 64 20 72 65 61 64 65 72 73 20 62  austed readers b
368a4 75 62 62 6c 65 20 74 6f 20 74 68 65 20 65 6e 64  ubble to the end
368a5 2c 20 73 6f 20 77 68 65 6e 20 74 68 65 20 66 69  , so when the fi
368a6 72 73 74 20 72 65 61 64 65 72 20 69 73 0a 20 20  rst reader is.  
368a7 2a 2a 20 61 74 20 65 6f 66 2c 20 61 6c 6c 20 61  ** at eof, all a
368a8 72 65 20 61 74 20 65 6f 66 2e 0a 20 20 2a 2f 0a  re at eof..  */.
368a9 20 20 77 68 69 6c 65 28 20 21 6f 70 74 4c 65 61    while( !optLea
368aa 76 65 73 52 65 61 64 65 72 41 74 45 6e 64 28 26  vesReaderAtEnd(&
368ab 72 65 61 64 65 72 73 5b 30 5d 29 20 29 7b 0a 0a  readers[0]) ){..
368ac 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75      /* Figure ou
368ad 74 20 68 6f 77 20 6d 61 6e 79 20 72 65 61 64 65  t how many reade
368ae 72 73 20 73 68 61 72 65 20 74 68 65 20 6e 65 78  rs share the nex
368af 74 20 74 65 72 6d 2e 20 2a 2f 0a 20 20 20 20 66  t term. */.    f
368b0 6f 72 28 69 3d 31 3b 20 69 3c 6e 52 65 61 64 65  or(i=1; i<nReade
368b1 72 73 20 26 26 20 21 6f 70 74 4c 65 61 76 65 73  rs && !optLeaves
368b2 52 65 61 64 65 72 41 74 45 6e 64 28 26 72 65 61  ReaderAtEnd(&rea
368b3 64 65 72 73 5b 69 5d 29 3b 20 69 2b 2b 29 7b 0a  ders[i]); i++){.
368b4 20 20 20 20 20 20 69 66 28 20 30 21 3d 6f 70 74        if( 0!=opt
368b5 4c 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d  LeavesReaderTerm
368b6 43 6d 70 28 26 72 65 61 64 65 72 73 5b 30 5d 2c  Cmp(&readers[0],
368b7 20 26 72 65 61 64 65 72 73 5b 69 5d 29 20 29 20   &readers[i]) ) 
368b8 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20  break;.    }..  
368b9 20 20 2f 2a 20 53 70 65 63 69 61 6c 2d 63 61 73    /* Special-cas
368ba 65 20 66 6f 72 20 6e 6f 20 6d 65 72 67 65 2e 20  e for no merge. 
368bb 2a 2f 0a 20 20 20 20 69 66 28 20 69 3d 3d 31 20  */.    if( i==1 
368bc 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 72 69 6d  ){.      /* Trim
368bd 20 64 65 6c 65 74 69 6f 6e 73 20 66 72 6f 6d 20   deletions from 
368be 74 68 65 20 64 6f 63 6c 69 73 74 2e 20 2a 2f 0a  the doclist. */.
368bf 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65 72        dataBuffer
368c0 52 65 73 65 74 28 26 6d 65 72 67 65 64 29 3b 0a  Reset(&merged);.
368c1 20 20 20 20 20 20 64 6f 63 4c 69 73 74 54 72 69        docListTri
368c2 6d 28 44 4c 5f 44 45 46 41 55 4c 54 2c 0a 20 20  m(DL_DEFAULT,.  
368c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
368c4 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 44  optLeavesReaderD
368c5 61 74 61 28 26 72 65 61 64 65 72 73 5b 30 5d 29  ata(&readers[0])
368c6 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
368c7 20 20 20 20 6f 70 74 4c 65 61 76 65 73 52 65 61      optLeavesRea
368c8 64 65 72 44 61 74 61 42 79 74 65 73 28 26 72 65  derDataBytes(&re
368c9 61 64 65 72 73 5b 30 5d 29 2c 0a 20 20 20 20 20  aders[0]),.     
368ca 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 31 2c               -1,
368cb 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20 26 6d 65   DL_DEFAULT, &me
368cc 72 67 65 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65  rged);.    }else
368cd 7b 0a 20 20 20 20 20 20 44 4c 52 65 61 64 65 72  {.      DLReader
368ce 20 64 6c 52 65 61 64 65 72 73 5b 4d 45 52 47 45   dlReaders[MERGE
368cf 5f 43 4f 55 4e 54 5d 3b 0a 20 20 20 20 20 20 69  _COUNT];.      i
368d0 6e 74 20 69 52 65 61 64 65 72 2c 20 6e 52 65 61  nt iReader, nRea
368d1 64 65 72 73 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  ders;..      /* 
368d2 50 72 69 6d 65 20 74 68 65 20 70 69 70 65 6c 69  Prime the pipeli
368d3 6e 65 20 77 69 74 68 20 74 68 65 20 66 69 72 73  ne with the firs
368d4 74 20 72 65 61 64 65 72 27 73 20 64 6f 63 6c 69  t reader's docli
368d5 73 74 2e 20 20 41 66 74 65 72 0a 20 20 20 20 20  st.  After.     
368d6 20 2a 2a 20 6f 6e 65 20 70 61 73 73 20 69 6e 64   ** one pass ind
368d7 65 78 20 30 20 77 69 6c 6c 20 72 65 66 65 72 65  ex 0 will refere
368d8 6e 63 65 20 74 68 65 20 61 63 63 75 6d 75 6c 61  nce the accumula
368d9 74 65 64 20 64 6f 63 6c 69 73 74 2e 0a 20 20 20  ted doclist..   
368da 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 6c 72 49     */.      dlrI
368db 6e 69 74 28 26 64 6c 52 65 61 64 65 72 73 5b 30  nit(&dlReaders[0
368dc 5d 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 0a 20  ], DL_DEFAULT,. 
368dd 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 70 74               opt
368de 4c 65 61 76 65 73 52 65 61 64 65 72 44 61 74 61  LeavesReaderData
368df 28 26 72 65 61 64 65 72 73 5b 30 5d 29 2c 0a 20  (&readers[0]),. 
368e0 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 70 74               opt
368e1 4c 65 61 76 65 73 52 65 61 64 65 72 44 61 74 61  LeavesReaderData
368e2 42 79 74 65 73 28 26 72 65 61 64 65 72 73 5b 30  Bytes(&readers[0
368e3 5d 29 29 3b 0a 20 20 20 20 20 20 69 52 65 61 64  ]));.      iRead
368e4 65 72 20 3d 20 31 3b 0a 0a 20 20 20 20 20 20 61  er = 1;..      a
368e5 73 73 65 72 74 28 20 69 52 65 61 64 65 72 3c 69  ssert( iReader<i
368e6 20 29 3b 20 20 2f 2a 20 4d 75 73 74 20 65 78 65   );  /* Must exe
368e7 63 75 74 65 20 74 68 65 20 6c 6f 6f 70 20 61 74  cute the loop at
368e8 20 6c 65 61 73 74 20 6f 6e 63 65 2e 20 2a 2f 0a   least once. */.
368e9 20 20 20 20 20 20 77 68 69 6c 65 28 20 69 52 65        while( iRe
368ea 61 64 65 72 3c 69 20 29 7b 0a 20 20 20 20 20 20  ader<i ){.      
368eb 20 20 2f 2a 20 4d 65 72 67 65 20 31 36 20 69 6e    /* Merge 16 in
368ec 70 75 74 73 20 70 65 72 20 70 61 73 73 2e 20 2a  puts per pass. *
368ed 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28 20 6e  /.        for( n
368ee 52 65 61 64 65 72 73 3d 31 3b 20 69 52 65 61 64  Readers=1; iRead
368ef 65 72 3c 69 20 26 26 20 6e 52 65 61 64 65 72 73  er<i && nReaders
368f0 3c 4d 45 52 47 45 5f 43 4f 55 4e 54 3b 0a 20 20  <MERGE_COUNT;.  
368f1 20 20 20 20 20 20 20 20 20 20 20 69 52 65 61 64             iRead
368f2 65 72 2b 2b 2c 20 6e 52 65 61 64 65 72 73 2b 2b  er++, nReaders++
368f3 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64 6c   ){.          dl
368f4 72 49 6e 69 74 28 26 64 6c 52 65 61 64 65 72 73  rInit(&dlReaders
368f5 5b 6e 52 65 61 64 65 72 73 5d 2c 20 44 4c 5f 44  [nReaders], DL_D
368f6 45 46 41 55 4c 54 2c 0a 20 20 20 20 20 20 20 20  EFAULT,.        
368f7 20 20 20 20 20 20 20 20 20 20 6f 70 74 4c 65 61            optLea
368f8 76 65 73 52 65 61 64 65 72 44 61 74 61 28 26 72  vesReaderData(&r
368f9 65 61 64 65 72 73 5b 69 52 65 61 64 65 72 5d 29  eaders[iReader])
368fa 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
368fb 20 20 20 20 6f 70 74 4c 65 61 76 65 73 52 65 61      optLeavesRea
368fc 64 65 72 44 61 74 61 42 79 74 65 73 28 26 72 65  derDataBytes(&re
368fd 61 64 65 72 73 5b 69 52 65 61 64 65 72 5d 29 29  aders[iReader]))
368fe 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20  ;.        }..   
368ff 20 20 20 20 20 2f 2a 20 4d 65 72 67 65 20 64 6f       /* Merge do
36900 63 6c 69 73 74 73 20 61 6e 64 20 73 77 61 70 20  clists and swap 
36901 72 65 73 75 6c 74 20 69 6e 74 6f 20 61 63 63 75  result into accu
36902 6d 75 6c 61 74 6f 72 2e 20 2a 2f 0a 20 20 20 20  mulator. */.    
36903 20 20 20 20 64 61 74 61 42 75 66 66 65 72 52 65      dataBufferRe
36904 73 65 74 28 26 6d 65 72 67 65 64 29 3b 0a 20 20  set(&merged);.  
36905 20 20 20 20 20 20 64 6f 63 4c 69 73 74 4d 65 72        docListMer
36906 67 65 28 26 6d 65 72 67 65 64 2c 20 64 6c 52 65  ge(&merged, dlRe
36907 61 64 65 72 73 2c 20 6e 52 65 61 64 65 72 73 29  aders, nReaders)
36908 3b 0a 20 20 20 20 20 20 20 20 74 6d 70 20 3d 20  ;.        tmp = 
36909 6d 65 72 67 65 64 3b 0a 20 20 20 20 20 20 20 20  merged;.        
3690a 6d 65 72 67 65 64 20 3d 20 64 6f 63 6c 69 73 74  merged = doclist
3690b 3b 0a 20 20 20 20 20 20 20 20 64 6f 63 6c 69 73  ;.        doclis
3690c 74 20 3d 20 74 6d 70 3b 0a 0a 20 20 20 20 20 20  t = tmp;..      
3690d 20 20 77 68 69 6c 65 28 20 6e 52 65 61 64 65 72    while( nReader
3690e 73 2d 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 20  s-- > 0 ){.     
3690f 20 20 20 20 20 64 6c 72 44 65 73 74 72 6f 79 28       dlrDestroy(
36910 26 64 6c 52 65 61 64 65 72 73 5b 6e 52 65 61 64  &dlReaders[nRead
36911 65 72 73 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d  ers]);.        }
36912 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 63 63  ..        /* Acc
36913 75 6d 75 6c 61 74 65 64 20 64 6f 63 6c 69 73 74  umulated doclist
36914 20 74 6f 20 72 65 61 64 65 72 20 30 20 66 6f 72   to reader 0 for
36915 20 6e 65 78 74 20 70 61 73 73 2e 20 2a 2f 0a 20   next pass. */. 
36916 20 20 20 20 20 20 20 64 6c 72 49 6e 69 74 28 26         dlrInit(&
36917 64 6c 52 65 61 64 65 72 73 5b 30 5d 2c 20 44 4c  dlReaders[0], DL
36918 5f 44 45 46 41 55 4c 54 2c 20 64 6f 63 6c 69 73  _DEFAULT, doclis
36919 74 2e 70 44 61 74 61 2c 20 64 6f 63 6c 69 73 74  t.pData, doclist
3691a 2e 6e 44 61 74 61 29 3b 0a 20 20 20 20 20 20 7d  .nData);.      }
3691b 0a 0a 20 20 20 20 20 20 2f 2a 20 44 65 73 74 72  ..      /* Destr
3691c 6f 79 20 72 65 61 64 65 72 20 74 68 61 74 20 77  oy reader that w
3691d 61 73 20 6c 65 66 74 20 69 6e 20 74 68 65 20 70  as left in the p
3691e 69 70 65 6c 69 6e 65 2e 20 2a 2f 0a 20 20 20 20  ipeline. */.    
3691f 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26 64 6c    dlrDestroy(&dl
36920 52 65 61 64 65 72 73 5b 30 5d 29 3b 0a 0a 20 20  Readers[0]);..  
36921 20 20 20 20 2f 2a 20 54 72 69 6d 20 64 65 6c 65      /* Trim dele
36922 74 69 6f 6e 73 20 66 72 6f 6d 20 74 68 65 20 64  tions from the d
36923 6f 63 6c 69 73 74 2e 20 2a 2f 0a 20 20 20 20 20  oclist. */.     
36924 20 64 61 74 61 42 75 66 66 65 72 52 65 73 65 74   dataBufferReset
36925 28 26 6d 65 72 67 65 64 29 3b 0a 20 20 20 20 20  (&merged);.     
36926 20 64 6f 63 4c 69 73 74 54 72 69 6d 28 44 4c 5f   docListTrim(DL_
36927 44 45 46 41 55 4c 54 2c 20 64 6f 63 6c 69 73 74  DEFAULT, doclist
36928 2e 70 44 61 74 61 2c 20 64 6f 63 6c 69 73 74 2e  .pData, doclist.
36929 6e 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20  nData,.         
3692a 20 20 20 20 20 20 20 20 20 2d 31 2c 20 44 4c 5f           -1, DL_
3692b 44 45 46 41 55 4c 54 2c 20 26 6d 65 72 67 65 64  DEFAULT, &merged
3692c 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  );.    }..    /*
3692d 20 4f 6e 6c 79 20 70 61 73 73 20 64 6f 63 6c 69   Only pass docli
3692e 73 74 73 20 77 69 74 68 20 68 69 74 73 20 28 73  sts with hits (s
3692f 6b 69 70 20 69 66 20 61 6c 6c 20 68 69 74 73 20  kip if all hits 
36930 64 65 6c 65 74 65 64 29 2e 20 2a 2f 0a 20 20 20  deleted). */.   
36931 20 69 66 28 20 6d 65 72 67 65 64 2e 6e 44 61 74   if( merged.nDat
36932 61 3e 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20  a>0 ){.      rc 
36933 3d 20 6c 65 61 66 57 72 69 74 65 72 53 74 65 70  = leafWriterStep
36934 28 76 2c 20 70 57 72 69 74 65 72 2c 0a 20 20 20  (v, pWriter,.   
36935 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36936 20 20 20 20 20 20 20 6f 70 74 4c 65 61 76 65 73         optLeaves
36937 52 65 61 64 65 72 54 65 72 6d 28 26 72 65 61 64  ReaderTerm(&read
36938 65 72 73 5b 30 5d 29 2c 0a 20 20 20 20 20 20 20  ers[0]),.       
36939 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3693a 20 20 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64     optLeavesRead
3693b 65 72 54 65 72 6d 42 79 74 65 73 28 26 72 65 61  erTermBytes(&rea
3693c 64 65 72 73 5b 30 5d 29 2c 0a 20 20 20 20 20 20  ders[0]),.      
3693d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3693e 20 20 20 20 6d 65 72 67 65 64 2e 70 44 61 74 61      merged.pData
3693f 2c 20 6d 65 72 67 65 64 2e 6e 44 61 74 61 29 3b  , merged.nData);
36940 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
36941 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
36942 65 72 72 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  err;.    }..    
36943 2f 2a 20 53 74 65 70 20 6d 65 72 67 65 64 20 72  /* Step merged r
36944 65 61 64 65 72 73 20 74 6f 20 6e 65 78 74 20 74  eaders to next t
36945 65 72 6d 20 61 6e 64 20 72 65 6f 72 64 65 72 2e  erm and reorder.
36946 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 69   */.    while( i
36947 2d 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 20 20  -- > 0 ){.      
36948 72 63 20 3d 20 6f 70 74 4c 65 61 76 65 73 52 65  rc = optLeavesRe
36949 61 64 65 72 53 74 65 70 28 76 2c 20 26 72 65 61  aderStep(v, &rea
3694a 64 65 72 73 5b 69 5d 29 3b 0a 20 20 20 20 20 20  ders[i]);.      
3694b 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
3694c 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 0a 20  K ) goto err;.. 
3694d 20 20 20 20 20 6f 70 74 4c 65 61 76 65 73 52 65       optLeavesRe
3694e 61 64 65 72 52 65 6f 72 64 65 72 28 26 72 65 61  aderReorder(&rea
3694f 64 65 72 73 5b 69 5d 2c 20 6e 52 65 61 64 65 72  ders[i], nReader
36950 73 2d 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  s-i);.    }.  }.
36951 0a 20 65 72 72 3a 0a 20 20 64 61 74 61 42 75 66  . err:.  dataBuf
36952 66 65 72 44 65 73 74 72 6f 79 28 26 64 6f 63 6c  ferDestroy(&docl
36953 69 73 74 29 3b 0a 20 20 64 61 74 61 42 75 66 66  ist);.  dataBuff
36954 65 72 44 65 73 74 72 6f 79 28 26 6d 65 72 67 65  erDestroy(&merge
36955 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  d);.  return rc;
36956 0a 7d 0a 0a 2f 2a 20 49 6d 70 6c 65 6d 65 6e 74  .}../* Implement
36957 20 6f 70 74 69 6d 69 7a 65 28 29 20 66 75 6e 63   optimize() func
36958 74 69 6f 6e 20 66 6f 72 20 46 54 53 33 2e 20 20  tion for FTS3.  
36959 6f 70 74 69 6d 69 7a 65 28 74 29 20 6d 65 72 67  optimize(t) merg
3695a 65 73 20 61 6c 6c 0a 2a 2a 20 73 65 67 6d 65 6e  es all.** segmen
3695b 74 73 20 69 6e 20 74 68 65 20 66 74 73 20 69 6e  ts in the fts in
3695c 64 65 78 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c  dex into a singl
3695d 65 20 73 65 67 6d 65 6e 74 2e 20 20 27 74 27 20  e segment.  't' 
3695e 69 73 20 74 68 65 20 6d 61 67 69 63 0a 2a 2a 20  is the magic.** 
3695f 74 61 62 6c 65 2d 6e 61 6d 65 64 20 63 6f 6c 75  table-named colu
36960 6d 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  mn..*/.static vo
36961 69 64 20 6f 70 74 69 6d 69 7a 65 46 75 6e 63 28  id optimizeFunc(
36962 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
36963 2a 70 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20  *pContext,.     
36964 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36965 20 20 20 20 69 6e 74 20 61 72 67 63 2c 20 73 71      int argc, sq
36966 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
36967 67 76 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f  gv){.  fulltext_
36968 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b  cursor *pCursor;
36969 0a 20 20 69 66 28 20 61 72 67 63 3e 31 20 29 7b  .  if( argc>1 ){
3696a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
3696b 75 6c 74 5f 65 72 72 6f 72 28 70 43 6f 6e 74 65  ult_error(pConte
3696c 78 74 2c 20 22 65 78 63 65 73 73 20 61 72 67 75  xt, "excess argu
3696d 6d 65 6e 74 73 20 74 6f 20 6f 70 74 69 6d 69 7a  ments to optimiz
3696e 65 28 29 22 2c 2d 31 29 3b 0a 20 20 7d 65 6c 73  e()",-1);.  }els
3696f 65 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61  e if( sqlite3_va
36970 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d  lue_type(argv[0]
36971 29 21 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 7c  )!=SQLITE_BLOB |
36972 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71  |.            sq
36973 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
36974 73 28 61 72 67 76 5b 30 5d 29 21 3d 73 69 7a 65  s(argv[0])!=size
36975 6f 66 28 70 43 75 72 73 6f 72 29 20 29 7b 0a 20  of(pCursor) ){. 
36976 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
36977 74 5f 65 72 72 6f 72 28 70 43 6f 6e 74 65 78 74  t_error(pContext
36978 2c 20 22 69 6c 6c 65 67 61 6c 20 66 69 72 73 74  , "illegal first
36979 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 6f 70 74   argument to opt
3697a 69 6d 69 7a 65 22 2c 2d 31 29 3b 0a 20 20 7d 65  imize",-1);.  }e
3697b 6c 73 65 7b 0a 20 20 20 20 66 75 6c 6c 74 65 78  lse{.    fulltex
3697c 74 5f 76 74 61 62 20 2a 76 3b 0a 20 20 20 20 69  t_vtab *v;.    i
3697d 6e 74 20 69 2c 20 72 63 2c 20 69 4d 61 78 4c 65  nt i, rc, iMaxLe
3697e 76 65 6c 3b 0a 20 20 20 20 4f 70 74 4c 65 61 76  vel;.    OptLeav
3697f 65 73 52 65 61 64 65 72 20 2a 72 65 61 64 65 72  esReader *reader
36980 73 3b 0a 20 20 20 20 69 6e 74 20 6e 52 65 61 64  s;.    int nRead
36981 65 72 73 3b 0a 20 20 20 20 4c 65 61 66 57 72 69  ers;.    LeafWri
36982 74 65 72 20 77 72 69 74 65 72 3b 0a 20 20 20 20  ter writer;.    
36983 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b  sqlite3_stmt *s;
36984 0a 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 43  ..    memcpy(&pC
36985 75 72 73 6f 72 2c 20 73 71 6c 69 74 65 33 5f 76  ursor, sqlite3_v
36986 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30  alue_blob(argv[0
36987 5d 29 2c 20 73 69 7a 65 6f 66 28 70 43 75 72 73  ]), sizeof(pCurs
36988 6f 72 29 29 3b 0a 20 20 20 20 76 20 3d 20 63 75  or));.    v = cu
36989 72 73 6f 72 5f 76 74 61 62 28 70 43 75 72 73 6f  rsor_vtab(pCurso
3698a 72 29 3b 0a 0a 20 20 20 20 2f 2a 20 46 6c 75 73  r);..    /* Flus
3698b 68 20 61 6e 79 20 62 75 66 66 65 72 65 64 20 75  h any buffered u
3698c 70 64 61 74 65 73 20 62 65 66 6f 72 65 20 6f 70  pdates before op
3698d 74 69 6d 69 7a 69 6e 67 2e 20 2a 2f 0a 20 20 20  timizing. */.   
3698e 20 72 63 20 3d 20 66 6c 75 73 68 50 65 6e 64 69   rc = flushPendi
3698f 6e 67 54 65 72 6d 73 28 76 29 3b 0a 20 20 20 20  ngTerms(v);.    
36990 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
36991 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 0a 20  K ) goto err;.. 
36992 20 20 20 72 63 20 3d 20 73 65 67 64 69 72 5f 63     rc = segdir_c
36993 6f 75 6e 74 28 76 2c 20 26 6e 52 65 61 64 65 72  ount(v, &nReader
36994 73 2c 20 26 69 4d 61 78 4c 65 76 65 6c 29 3b 0a  s, &iMaxLevel);.
36995 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
36996 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72  TE_OK ) goto err
36997 3b 0a 20 20 20 20 69 66 28 20 6e 52 65 61 64 65  ;.    if( nReade
36998 72 73 3d 3d 30 20 7c 7c 20 6e 52 65 61 64 65 72  rs==0 || nReader
36999 73 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 73 71  s==1 ){.      sq
3699a 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
3699b 74 28 70 43 6f 6e 74 65 78 74 2c 20 22 49 6e 64  t(pContext, "Ind
3699c 65 78 20 61 6c 72 65 61 64 79 20 6f 70 74 69 6d  ex already optim
3699d 61 6c 22 2c 20 2d 31 2c 0a 20 20 20 20 20 20 20  al", -1,.       
3699e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3699f 20 20 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43     SQLITE_STATIC
369a0 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b  );.      return;
369a1 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72 63 20 3d  .    }..    rc =
369a2 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65   sql_get_stateme
369a3 6e 74 28 76 2c 20 53 45 47 44 49 52 5f 53 45 4c  nt(v, SEGDIR_SEL
369a4 45 43 54 5f 41 4c 4c 5f 53 54 4d 54 2c 20 26 73  ECT_ALL_STMT, &s
369a5 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
369a6 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
369a7 65 72 72 3b 0a 0a 20 20 20 20 72 65 61 64 65 72  err;..    reader
369a8 73 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  s = sqlite3_mall
369a9 6f 63 28 6e 52 65 61 64 65 72 73 2a 73 69 7a 65  oc(nReaders*size
369aa 6f 66 28 72 65 61 64 65 72 73 5b 30 5d 29 29 3b  of(readers[0]));
369ab 0a 20 20 20 20 69 66 28 20 72 65 61 64 65 72 73  .    if( readers
369ac 3d 3d 4e 55 4c 4c 20 29 20 67 6f 74 6f 20 65 72  ==NULL ) goto er
369ad 72 3b 0a 0a 20 20 20 20 2f 2a 20 4e 6f 74 65 20  r;..    /* Note 
369ae 74 68 61 74 20 74 68 65 72 65 20 77 69 6c 6c 20  that there will 
369af 61 6c 72 65 61 64 79 20 62 65 20 61 20 73 65 67  already be a seg
369b0 6d 65 6e 74 20 61 74 20 74 68 69 73 20 70 6f 73  ment at this pos
369b1 69 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 75 6e 74  ition.    ** unt
369b2 69 6c 20 77 65 20 63 61 6c 6c 20 73 65 67 64 69  il we call segdi
369b3 72 5f 64 65 6c 65 74 65 28 29 20 6f 6e 20 69 4d  r_delete() on iM
369b4 61 78 4c 65 76 65 6c 2e 0a 20 20 20 20 2a 2f 0a  axLevel..    */.
369b5 20 20 20 20 6c 65 61 66 57 72 69 74 65 72 49 6e      leafWriterIn
369b6 69 74 28 69 4d 61 78 4c 65 76 65 6c 2c 20 30 2c  it(iMaxLevel, 0,
369b7 20 26 77 72 69 74 65 72 29 3b 0a 0a 20 20 20 20   &writer);..    
369b8 69 20 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c 65  i = 0;.    while
369b9 28 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  ( (rc = sqlite3_
369ba 73 74 65 70 28 73 29 29 3d 3d 53 51 4c 49 54 45  step(s))==SQLITE
369bb 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 73 71  _ROW ){.      sq
369bc 6c 69 74 65 5f 69 6e 74 36 34 20 69 53 74 61 72  lite_int64 iStar
369bd 74 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  t = sqlite3_colu
369be 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 30 29 3b 0a  mn_int64(s, 0);.
369bf 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74        sqlite_int
369c0 36 34 20 69 45 6e 64 20 3d 20 73 71 6c 69 74 65  64 iEnd = sqlite
369c1 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73  3_column_int64(s
369c2 2c 20 31 29 3b 0a 20 20 20 20 20 20 63 6f 6e 73  , 1);.      cons
369c3 74 20 63 68 61 72 20 2a 70 52 6f 6f 74 44 61 74  t char *pRootDat
369c4 61 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  a = sqlite3_colu
369c5 6d 6e 5f 62 6c 6f 62 28 73 2c 20 32 29 3b 0a 20  mn_blob(s, 2);. 
369c6 20 20 20 20 20 69 6e 74 20 6e 52 6f 6f 74 44 61       int nRootDa
369c7 74 61 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  ta = sqlite3_col
369c8 75 6d 6e 5f 62 79 74 65 73 28 73 2c 20 32 29 3b  umn_bytes(s, 2);
369c9 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
369ca 69 3c 6e 52 65 61 64 65 72 73 20 29 3b 0a 20 20  i<nReaders );.  
369cb 20 20 20 20 72 63 20 3d 20 6c 65 61 76 65 73 52      rc = leavesR
369cc 65 61 64 65 72 49 6e 69 74 28 76 2c 20 2d 31 2c  eaderInit(v, -1,
369cd 20 69 53 74 61 72 74 2c 20 69 45 6e 64 2c 20 70   iStart, iEnd, p
369ce 52 6f 6f 74 44 61 74 61 2c 20 6e 52 6f 6f 74 44  RootData, nRootD
369cf 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ata,.           
369d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
369d1 20 26 72 65 61 64 65 72 73 5b 69 5d 2e 72 65 61   &readers[i].rea
369d2 64 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20  der);.      if( 
369d3 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
369d4 62 72 65 61 6b 3b 0a 0a 20 20 20 20 20 20 72 65  break;..      re
369d5 61 64 65 72 73 5b 69 5d 2e 73 65 67 6d 65 6e 74  aders[i].segment
369d6 20 3d 20 69 3b 0a 20 20 20 20 20 20 69 2b 2b 3b   = i;.      i++;
369d7 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49  .    }..    /* I
369d8 66 20 77 65 20 6d 61 6e 61 67 65 64 20 74 6f 20  f we managed to 
369d9 73 75 63 63 65 73 73 66 75 6c 6c 79 20 72 65 61  successfully rea
369da 64 20 74 68 65 6d 20 61 6c 6c 2c 20 6f 70 74 69  d them all, opti
369db 6d 69 7a 65 20 74 68 65 6d 2e 20 2a 2f 0a 20 20  mize them. */.  
369dc 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
369dd 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 61  _DONE ){.      a
369de 73 73 65 72 74 28 20 69 3d 3d 6e 52 65 61 64 65  ssert( i==nReade
369df 72 73 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  rs );.      rc =
369e0 20 6f 70 74 69 6d 69 7a 65 49 6e 74 65 72 6e 61   optimizeInterna
369e1 6c 28 76 2c 20 72 65 61 64 65 72 73 2c 20 6e 52  l(v, readers, nR
369e2 65 61 64 65 72 73 2c 20 26 77 72 69 74 65 72 29  eaders, &writer)
369e3 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 77 68 69  ;.    }..    whi
369e4 6c 65 28 20 69 2d 2d 20 3e 20 30 20 29 7b 0a 20  le( i-- > 0 ){. 
369e5 20 20 20 20 20 6c 65 61 76 65 73 52 65 61 64 65       leavesReade
369e6 72 44 65 73 74 72 6f 79 28 26 72 65 61 64 65 72  rDestroy(&reader
369e7 73 5b 69 5d 2e 72 65 61 64 65 72 29 3b 0a 20 20  s[i].reader);.  
369e8 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f    }.    sqlite3_
369e9 66 72 65 65 28 72 65 61 64 65 72 73 29 3b 0a 0a  free(readers);..
369ea 20 20 20 20 2f 2a 20 49 66 20 77 65 27 76 65 20      /* If we've 
369eb 73 75 63 63 65 73 73 66 75 6c 6c 79 20 67 6f 74  successfully got
369ec 74 65 6e 20 74 6f 20 68 65 72 65 2c 20 64 65 6c  ten to here, del
369ed 65 74 65 20 74 68 65 20 6f 6c 64 20 73 65 67 6d  ete the old segm
369ee 65 6e 74 73 0a 20 20 20 20 2a 2a 20 61 6e 64 20  ents.    ** and 
369ef 66 6c 75 73 68 20 74 68 65 20 69 6e 74 65 72 69  flush the interi
369f0 6f 72 20 73 74 72 75 63 74 75 72 65 20 6f 66 20  or structure of 
369f1 74 68 65 20 6e 65 77 20 73 65 67 6d 65 6e 74 2e  the new segment.
369f2 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
369f3 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
369f4 0a 20 20 20 20 20 20 66 6f 72 28 20 69 3d 30 3b  .      for( i=0;
369f5 20 69 3c 3d 69 4d 61 78 4c 65 76 65 6c 3b 20 69   i<=iMaxLevel; i
369f6 2b 2b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  ++ ){.        rc
369f7 20 3d 20 73 65 67 64 69 72 5f 64 65 6c 65 74 65   = segdir_delete
369f8 28 76 2c 20 69 29 3b 0a 20 20 20 20 20 20 20 20  (v, i);.        
369f9 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
369fa 4b 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  K ) break;.     
369fb 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 72 63   }..      if( rc
369fc 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 63  ==SQLITE_OK ) rc
369fd 20 3d 20 6c 65 61 66 57 72 69 74 65 72 46 69 6e   = leafWriterFin
369fe 61 6c 69 7a 65 28 76 2c 20 26 77 72 69 74 65 72  alize(v, &writer
369ff 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6c 65  );.    }..    le
36a00 61 66 57 72 69 74 65 72 44 65 73 74 72 6f 79 28  afWriterDestroy(
36a01 26 77 72 69 74 65 72 29 3b 0a 0a 20 20 20 20 69  &writer);..    i
36a02 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
36a03 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 0a 20 20   ) goto err;..  
36a04 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
36a05 5f 74 65 78 74 28 70 43 6f 6e 74 65 78 74 2c 20  _text(pContext, 
36a06 22 49 6e 64 65 78 20 6f 70 74 69 6d 69 7a 65 64  "Index optimized
36a07 22 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54  ", -1, SQLITE_ST
36a08 41 54 49 43 29 3b 0a 20 20 20 20 72 65 74 75 72  ATIC);.    retur
36a09 6e 3b 0a 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 28  n;..    /* TODO(
36a0a 73 68 65 73 73 29 3a 20 45 72 72 6f 72 2d 68 61  shess): Error-ha
36a0b 6e 64 6c 69 6e 67 20 6e 65 65 64 73 20 74 6f 20  ndling needs to 
36a0c 62 65 20 69 6d 70 72 6f 76 65 64 20 61 6c 6f 6e  be improved alon
36a0d 67 20 74 68 65 0a 20 20 20 20 2a 2a 20 6c 69 6e  g the.    ** lin
36a0e 65 73 20 6f 66 20 74 68 65 20 64 75 6d 70 5f 20  es of the dump_ 
36a0f 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 20 20 2a  functions..    *
36a10 2f 0a 20 65 72 72 3a 0a 20 20 20 20 7b 0a 20 20  /. err:.    {.  
36a11 20 20 20 20 63 68 61 72 20 62 75 66 5b 35 31 32      char buf[512
36a12 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ];.      sqlite3
36a13 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66  _snprintf(sizeof
36a14 28 62 75 66 29 2c 20 62 75 66 2c 20 22 45 72 72  (buf), buf, "Err
36a15 6f 72 20 69 6e 20 6f 70 74 69 6d 69 7a 65 3a 20  or in optimize: 
36a16 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  %s",.           
36a17 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
36a18 74 65 33 5f 65 72 72 6d 73 67 28 73 71 6c 69 74  te3_errmsg(sqlit
36a19 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  e3_context_db_ha
36a1a 6e 64 6c 65 28 70 43 6f 6e 74 65 78 74 29 29 29  ndle(pContext)))
36a1b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
36a1c 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 70 43 6f  result_error(pCo
36a1d 6e 74 65 78 74 2c 20 62 75 66 2c 20 2d 31 29 3b  ntext, buf, -1);
36a1e 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 69  .    }.  }.}..#i
36a1f 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
36a20 0a 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 6e 20  ./* Generate an 
36a21 65 72 72 6f 72 20 6f 66 20 74 68 65 20 66 6f 72  error of the for
36a22 6d 20 22 3c 70 72 65 66 69 78 3e 3a 20 3c 6d 73  m "<prefix>: <ms
36a23 67 3e 22 2e 20 20 49 66 20 6d 73 67 20 69 73 20  g>".  If msg is 
36a24 4e 55 4c 4c 2c 0a 2a 2a 20 70 75 6c 6c 20 74 68  NULL,.** pull th
36a25 65 20 65 72 72 6f 72 20 66 72 6f 6d 20 74 68 65  e error from the
36a26 20 63 6f 6e 74 65 78 74 27 73 20 64 62 20 68 61   context's db ha
36a27 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ndle..*/.static 
36a28 76 6f 69 64 20 67 65 6e 65 72 61 74 65 45 72 72  void generateErr
36a29 6f 72 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  or(sqlite3_conte
36a2a 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c 0a 20 20  xt *pContext,.  
36a2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36a2c 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
36a2d 61 72 20 2a 70 72 65 66 69 78 2c 20 63 6f 6e 73  ar *prefix, cons
36a2e 74 20 63 68 61 72 20 2a 6d 73 67 29 7b 0a 20 20  t char *msg){.  
36a2f 63 68 61 72 20 62 75 66 5b 35 31 32 5d 3b 0a 20  char buf[512];. 
36a30 20 69 66 28 20 6d 73 67 3d 3d 4e 55 4c 4c 20 29   if( msg==NULL )
36a31 20 6d 73 67 20 3d 20 73 71 6c 69 74 65 33 5f 65   msg = sqlite3_e
36a32 72 72 6d 73 67 28 73 71 6c 69 74 65 33 5f 63 6f  rrmsg(sqlite3_co
36a33 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28  ntext_db_handle(
36a34 70 43 6f 6e 74 65 78 74 29 29 3b 0a 20 20 73 71  pContext));.  sq
36a35 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
36a36 69 7a 65 6f 66 28 62 75 66 29 2c 20 62 75 66 2c  izeof(buf), buf,
36a37 20 22 25 73 3a 20 25 73 22 2c 20 70 72 65 66 69   "%s: %s", prefi
36a38 78 2c 20 6d 73 67 29 3b 0a 20 20 73 71 6c 69 74  x, msg);.  sqlit
36a39 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
36a3a 70 43 6f 6e 74 65 78 74 2c 20 62 75 66 2c 20 2d  pContext, buf, -
36a3b 31 29 3b 0a 7d 0a 0a 2f 2a 20 48 65 6c 70 65 72  1);.}../* Helper
36a3c 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 6f 6c   function to col
36a3d 6c 65 63 74 20 74 68 65 20 73 65 74 20 6f 66 20  lect the set of 
36a3e 74 65 72 6d 73 20 69 6e 20 74 68 65 20 73 65 67  terms in the seg
36a3f 6d 65 6e 74 20 69 6e 74 6f 0a 2a 2a 20 70 54 65  ment into.** pTe
36a40 72 6d 73 2e 20 20 54 68 65 20 73 65 67 6d 65 6e  rms.  The segmen
36a41 74 20 69 73 20 64 65 66 69 6e 65 64 20 62 79 20  t is defined by 
36a42 74 68 65 20 6c 65 61 66 20 6e 6f 64 65 73 20 62  the leaf nodes b
36a43 65 74 77 65 65 6e 0a 2a 2a 20 69 53 74 61 72 74  etween.** iStart
36a44 42 6c 6f 63 6b 69 64 20 61 6e 64 20 69 45 6e 64  Blockid and iEnd
36a45 42 6c 6f 63 6b 69 64 2c 20 69 6e 63 6c 75 73 69  Blockid, inclusi
36a46 76 65 2c 20 6f 72 20 62 79 20 74 68 65 20 63 6f  ve, or by the co
36a47 6e 74 65 6e 74 73 20 6f 66 0a 2a 2a 20 70 52 6f  ntents of.** pRo
36a48 6f 74 44 61 74 61 20 69 66 20 69 53 74 61 72 74  otData if iStart
36a49 42 6c 6f 63 6b 69 64 20 69 73 20 30 20 28 69 6e  Blockid is 0 (in
36a4a 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65 20   which case the 
36a4b 65 6e 74 69 72 65 20 73 65 67 6d 65 6e 74 0a 2a  entire segment.*
36a4c 2a 20 66 69 74 20 69 6e 20 61 20 6c 65 61 66 29  * fit in a leaf)
36a4d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
36a4e 63 6f 6c 6c 65 63 74 53 65 67 6d 65 6e 74 54 65  collectSegmentTe
36a4f 72 6d 73 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  rms(fulltext_vta
36a50 62 20 2a 76 2c 20 73 71 6c 69 74 65 33 5f 73 74  b *v, sqlite3_st
36a51 6d 74 20 2a 73 2c 0a 20 20 20 20 20 20 20 20 20  mt *s,.         
36a52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36a53 20 20 20 20 20 20 66 74 73 33 48 61 73 68 20 2a        fts3Hash *
36a54 70 54 65 72 6d 73 29 7b 0a 20 20 63 6f 6e 73 74  pTerms){.  const
36a55 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 53   sqlite_int64 iS
36a56 74 61 72 74 42 6c 6f 63 6b 69 64 20 3d 20 73 71  tartBlockid = sq
36a57 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
36a58 36 34 28 73 2c 20 30 29 3b 0a 20 20 63 6f 6e 73  64(s, 0);.  cons
36a59 74 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69  t sqlite_int64 i
36a5a 45 6e 64 42 6c 6f 63 6b 69 64 20 3d 20 73 71 6c  EndBlockid = sql
36a5b 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36  ite3_column_int6
36a5c 34 28 73 2c 20 31 29 3b 0a 20 20 63 6f 6e 73 74  4(s, 1);.  const
36a5d 20 63 68 61 72 20 2a 70 52 6f 6f 74 44 61 74 61   char *pRootData
36a5e 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d   = sqlite3_colum
36a5f 6e 5f 62 6c 6f 62 28 73 2c 20 32 29 3b 0a 20 20  n_blob(s, 2);.  
36a60 63 6f 6e 73 74 20 69 6e 74 20 6e 52 6f 6f 74 44  const int nRootD
36a61 61 74 61 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  ata = sqlite3_co
36a62 6c 75 6d 6e 5f 62 79 74 65 73 28 73 2c 20 32 29  lumn_bytes(s, 2)
36a63 3b 0a 20 20 4c 65 61 76 65 73 52 65 61 64 65 72  ;.  LeavesReader
36a64 20 72 65 61 64 65 72 3b 0a 20 20 69 6e 74 20 72   reader;.  int r
36a65 63 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72  c = leavesReader
36a66 49 6e 69 74 28 76 2c 20 30 2c 20 69 53 74 61 72  Init(v, 0, iStar
36a67 74 42 6c 6f 63 6b 69 64 2c 20 69 45 6e 64 42 6c  tBlockid, iEndBl
36a68 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20 20 20 20  ockid,.         
36a69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36a6a 20 20 20 70 52 6f 6f 74 44 61 74 61 2c 20 6e 52     pRootData, nR
36a6b 6f 6f 74 44 61 74 61 2c 20 26 72 65 61 64 65 72  ootData, &reader
36a6c 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
36a6d 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
36a6e 72 63 3b 0a 0a 20 20 77 68 69 6c 65 28 20 72 63  rc;..  while( rc
36a6f 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21  ==SQLITE_OK && !
36a70 6c 65 61 76 65 73 52 65 61 64 65 72 41 74 45 6e  leavesReaderAtEn
36a71 64 28 26 72 65 61 64 65 72 29 20 29 7b 0a 20 20  d(&reader) ){.  
36a72 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54    const char *pT
36a73 65 72 6d 20 3d 20 6c 65 61 76 65 73 52 65 61 64  erm = leavesRead
36a74 65 72 54 65 72 6d 28 26 72 65 61 64 65 72 29 3b  erTerm(&reader);
36a75 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e  .    const int n
36a76 54 65 72 6d 20 3d 20 6c 65 61 76 65 73 52 65 61  Term = leavesRea
36a77 64 65 72 54 65 72 6d 42 79 74 65 73 28 26 72 65  derTermBytes(&re
36a78 61 64 65 72 29 3b 0a 20 20 20 20 76 6f 69 64 20  ader);.    void 
36a79 2a 6f 6c 64 56 61 6c 75 65 20 3d 20 73 71 6c 69  *oldValue = sqli
36a7a 74 65 33 46 74 73 33 48 61 73 68 46 69 6e 64 28  te3Fts3HashFind(
36a7b 70 54 65 72 6d 73 2c 20 70 54 65 72 6d 2c 20 6e  pTerms, pTerm, n
36a7c 54 65 72 6d 29 3b 0a 20 20 20 20 76 6f 69 64 20  Term);.    void 
36a7d 2a 6e 65 77 56 61 6c 75 65 20 3d 20 28 76 6f 69  *newValue = (voi
36a7e 64 20 2a 29 28 28 63 68 61 72 20 2a 29 6f 6c 64  d *)((char *)old
36a7f 56 61 6c 75 65 2b 31 29 3b 0a 0a 20 20 20 20 2f  Value+1);..    /
36a80 2a 20 46 72 6f 6d 20 74 68 65 20 63 6f 6d 6d 65  * From the comme
36a81 6e 74 20 62 65 66 6f 72 65 20 73 71 6c 69 74 65  nt before sqlite
36a82 33 46 74 73 33 48 61 73 68 49 6e 73 65 72 74 20  3Fts3HashInsert 
36a83 69 6e 20 66 74 73 33 5f 68 61 73 68 2e 63 2c 0a  in fts3_hash.c,.
36a84 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 20      ** the data 
36a85 76 61 6c 75 65 20 70 61 73 73 65 64 20 69 73 20  value passed is 
36a86 72 65 74 75 72 6e 65 64 20 69 6e 20 63 61 73 65  returned in case
36a87 20 6f 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75   of malloc failu
36a88 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  re..    */.    i
36a89 66 28 20 6e 65 77 56 61 6c 75 65 3d 3d 73 71 6c  f( newValue==sql
36a8a 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 73 65  ite3Fts3HashInse
36a8b 72 74 28 70 54 65 72 6d 73 2c 20 70 54 65 72 6d  rt(pTerms, pTerm
36a8c 2c 20 6e 54 65 72 6d 2c 20 6e 65 77 56 61 6c 75  , nTerm, newValu
36a8d 65 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  e) ){.      rc =
36a8e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
36a8f 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
36a90 72 63 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65  rc = leavesReade
36a91 72 53 74 65 70 28 76 2c 20 26 72 65 61 64 65 72  rStep(v, &reader
36a92 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
36a93 6c 65 61 76 65 73 52 65 61 64 65 72 44 65 73 74  leavesReaderDest
36a94 72 6f 79 28 26 72 65 61 64 65 72 29 3b 0a 20 20  roy(&reader);.  
36a95 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
36a96 20 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e   Helper function
36a97 20 74 6f 20 62 75 69 6c 64 20 74 68 65 20 72 65   to build the re
36a98 73 75 6c 74 20 73 74 72 69 6e 67 20 66 6f 72 20  sult string for 
36a99 64 75 6d 70 5f 74 65 72 6d 73 28 29 2e 20 2a 2f  dump_terms(). */
36a9a 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 6e 65  .static int gene
36a9b 72 61 74 65 54 65 72 6d 73 52 65 73 75 6c 74 28  rateTermsResult(
36a9c 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
36a9d 2a 70 43 6f 6e 74 65 78 74 2c 20 66 74 73 33 48  *pContext, fts3H
36a9e 61 73 68 20 2a 70 54 65 72 6d 73 29 7b 0a 20 20  ash *pTerms){.  
36a9f 69 6e 74 20 69 54 65 72 6d 2c 20 6e 54 65 72 6d  int iTerm, nTerm
36aa0 73 2c 20 6e 52 65 73 75 6c 74 42 79 74 65 73 2c  s, nResultBytes,
36aa1 20 69 42 79 74 65 3b 0a 20 20 63 68 61 72 20 2a   iByte;.  char *
36aa2 72 65 73 75 6c 74 3b 0a 20 20 54 65 72 6d 44 61  result;.  TermDa
36aa3 74 61 20 2a 70 44 61 74 61 3b 0a 20 20 66 74 73  ta *pData;.  fts
36aa4 33 48 61 73 68 45 6c 65 6d 20 2a 65 3b 0a 0a 20  3HashElem *e;.. 
36aa5 20 2f 2a 20 49 74 65 72 61 74 65 20 70 54 65 72   /* Iterate pTer
36aa6 6d 73 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61  ms to generate a
36aa7 6e 20 61 72 72 61 79 20 6f 66 20 74 65 72 6d 73  n array of terms
36aa8 20 69 6e 20 70 44 61 74 61 20 66 6f 72 0a 20 20   in pData for.  
36aa9 2a 2a 20 73 6f 72 74 69 6e 67 2e 0a 20 20 2a 2f  ** sorting..  */
36aaa 0a 20 20 6e 54 65 72 6d 73 20 3d 20 66 74 73 33  .  nTerms = fts3
36aab 48 61 73 68 43 6f 75 6e 74 28 70 54 65 72 6d 73  HashCount(pTerms
36aac 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 54 65  );.  assert( nTe
36aad 72 6d 73 3e 30 20 29 3b 0a 20 20 70 44 61 74 61  rms>0 );.  pData
36aae 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
36aaf 63 28 6e 54 65 72 6d 73 2a 73 69 7a 65 6f 66 28  c(nTerms*sizeof(
36ab0 54 65 72 6d 44 61 74 61 29 29 3b 0a 20 20 69 66  TermData));.  if
36ab1 28 20 70 44 61 74 61 3d 3d 4e 55 4c 4c 20 29 20  ( pData==NULL ) 
36ab2 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
36ab3 4d 45 4d 3b 0a 0a 20 20 6e 52 65 73 75 6c 74 42  MEM;..  nResultB
36ab4 79 74 65 73 20 3d 20 30 3b 0a 20 20 66 6f 72 28  ytes = 0;.  for(
36ab5 69 54 65 72 6d 20 3d 20 30 2c 20 65 20 3d 20 66  iTerm = 0, e = f
36ab6 74 73 33 48 61 73 68 46 69 72 73 74 28 70 54 65  ts3HashFirst(pTe
36ab7 72 6d 73 29 3b 20 65 3b 20 69 54 65 72 6d 2b 2b  rms); e; iTerm++
36ab8 2c 20 65 20 3d 20 66 74 73 33 48 61 73 68 4e 65  , e = fts3HashNe
36ab9 78 74 28 65 29 29 7b 0a 20 20 20 20 6e 52 65 73  xt(e)){.    nRes
36aba 75 6c 74 42 79 74 65 73 20 2b 3d 20 66 74 73 33  ultBytes += fts3
36abb 48 61 73 68 4b 65 79 73 69 7a 65 28 65 29 2b 31  HashKeysize(e)+1
36abc 3b 20 20 20 2f 2a 20 54 65 72 6d 20 70 6c 75 73  ;   /* Term plus
36abd 20 74 72 61 69 6c 69 6e 67 20 73 70 61 63 65 20   trailing space 
36abe 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  */.    assert( i
36abf 54 65 72 6d 3c 6e 54 65 72 6d 73 20 29 3b 0a 20  Term<nTerms );. 
36ac0 20 20 20 70 44 61 74 61 5b 69 54 65 72 6d 5d 2e     pData[iTerm].
36ac1 70 54 65 72 6d 20 3d 20 66 74 73 33 48 61 73 68  pTerm = fts3Hash
36ac2 4b 65 79 28 65 29 3b 0a 20 20 20 20 70 44 61 74  Key(e);.    pDat
36ac3 61 5b 69 54 65 72 6d 5d 2e 6e 54 65 72 6d 20 3d  a[iTerm].nTerm =
36ac4 20 66 74 73 33 48 61 73 68 4b 65 79 73 69 7a 65   fts3HashKeysize
36ac5 28 65 29 3b 0a 20 20 20 20 70 44 61 74 61 5b 69  (e);.    pData[i
36ac6 54 65 72 6d 5d 2e 70 43 6f 6c 6c 65 63 74 6f 72  Term].pCollector
36ac7 20 3d 20 66 74 73 33 48 61 73 68 44 61 74 61 28   = fts3HashData(
36ac8 65 29 3b 20 20 2f 2a 20 75 6e 75 73 65 64 20 2a  e);  /* unused *
36ac9 2f 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  /.  }.  assert( 
36aca 69 54 65 72 6d 3d 3d 6e 54 65 72 6d 73 20 29 3b  iTerm==nTerms );
36acb 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 52 65 73  ..  assert( nRes
36acc 75 6c 74 42 79 74 65 73 3e 30 20 29 3b 20 20 20  ultBytes>0 );   
36acd 2f 2a 20 6e 54 65 72 6d 73 3e 30 2c 20 6e 52 65  /* nTerms>0, nRe
36ace 73 75 6c 74 73 42 79 74 65 73 20 6d 75 73 74 20  sultsBytes must 
36acf 62 65 2c 20 74 6f 6f 2e 20 2a 2f 0a 20 20 72 65  be, too. */.  re
36ad0 73 75 6c 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d  sult = sqlite3_m
36ad1 61 6c 6c 6f 63 28 6e 52 65 73 75 6c 74 42 79 74  alloc(nResultByt
36ad2 65 73 29 3b 0a 20 20 69 66 28 20 72 65 73 75 6c  es);.  if( resul
36ad3 74 3d 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 73  t==NULL ){.    s
36ad4 71 6c 69 74 65 33 5f 66 72 65 65 28 70 44 61 74  qlite3_free(pDat
36ad5 61 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  a);.    return S
36ad6 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
36ad7 0a 0a 20 20 69 66 28 20 6e 54 65 72 6d 73 3e 31  ..  if( nTerms>1
36ad8 20 29 20 71 73 6f 72 74 28 70 44 61 74 61 2c 20   ) qsort(pData, 
36ad9 6e 54 65 72 6d 73 2c 20 73 69 7a 65 6f 66 28 2a  nTerms, sizeof(*
36ada 70 44 61 74 61 29 2c 20 74 65 72 6d 44 61 74 61  pData), termData
36adb 43 6d 70 29 3b 0a 0a 20 20 2f 2a 20 52 65 61 64  Cmp);..  /* Read
36adc 20 74 68 65 20 74 65 72 6d 73 20 69 6e 20 6f 72   the terms in or
36add 64 65 72 20 74 6f 20 62 75 69 6c 64 20 74 68 65  der to build the
36ade 20 72 65 73 75 6c 74 2e 20 2a 2f 0a 20 20 69 42   result. */.  iB
36adf 79 74 65 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69  yte = 0;.  for(i
36ae0 54 65 72 6d 3d 30 3b 20 69 54 65 72 6d 3c 6e 54  Term=0; iTerm<nT
36ae1 65 72 6d 73 3b 20 2b 2b 69 54 65 72 6d 29 7b 0a  erms; ++iTerm){.
36ae2 20 20 20 20 6d 65 6d 63 70 79 28 72 65 73 75 6c      memcpy(resul
36ae3 74 2b 69 42 79 74 65 2c 20 70 44 61 74 61 5b 69  t+iByte, pData[i
36ae4 54 65 72 6d 5d 2e 70 54 65 72 6d 2c 20 70 44 61  Term].pTerm, pDa
36ae5 74 61 5b 69 54 65 72 6d 5d 2e 6e 54 65 72 6d 29  ta[iTerm].nTerm)
36ae6 3b 0a 20 20 20 20 69 42 79 74 65 20 2b 3d 20 70  ;.    iByte += p
36ae7 44 61 74 61 5b 69 54 65 72 6d 5d 2e 6e 54 65 72  Data[iTerm].nTer
36ae8 6d 3b 0a 20 20 20 20 72 65 73 75 6c 74 5b 69 42  m;.    result[iB
36ae9 79 74 65 2b 2b 5d 20 3d 20 27 20 27 3b 0a 20 20  yte++] = ' ';.  
36aea 7d 0a 20 20 61 73 73 65 72 74 28 20 69 42 79 74  }.  assert( iByt
36aeb 65 3d 3d 6e 52 65 73 75 6c 74 42 79 74 65 73 20  e==nResultBytes 
36aec 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 65 73  );.  assert( res
36aed 75 6c 74 5b 6e 52 65 73 75 6c 74 42 79 74 65 73  ult[nResultBytes
36aee 2d 31 5d 3d 3d 27 20 27 20 29 3b 0a 20 20 72 65  -1]==' ' );.  re
36aef 73 75 6c 74 5b 6e 52 65 73 75 6c 74 42 79 74 65  sult[nResultByte
36af0 73 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 0a 20 20  s-1] = '\0';..  
36af1 2f 2a 20 50 61 73 73 65 73 20 61 77 61 79 20 6f  /* Passes away o
36af2 77 6e 65 72 73 68 69 70 20 6f 66 20 72 65 73 75  wnership of resu
36af3 6c 74 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  lt. */.  sqlite3
36af4 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43 6f  _result_text(pCo
36af5 6e 74 65 78 74 2c 20 72 65 73 75 6c 74 2c 20 6e  ntext, result, n
36af6 52 65 73 75 6c 74 42 79 74 65 73 2d 31 2c 20 73  ResultBytes-1, s
36af7 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20  qlite3_free);.  
36af8 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 44 61  sqlite3_free(pDa
36af9 74 61 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  ta);.  return SQ
36afa 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 49  LITE_OK;.}../* I
36afb 6d 70 6c 65 6d 65 6e 74 73 20 64 75 6d 70 5f 74  mplements dump_t
36afc 65 72 6d 73 28 29 20 66 6f 72 20 75 73 65 20 69  erms() for use i
36afd 6e 20 69 6e 73 70 65 63 74 69 6e 67 20 74 68 65  n inspecting the
36afe 20 66 74 73 33 20 69 6e 64 65 78 20 66 72 6f 6d   fts3 index from
36aff 0a 2a 2a 20 74 65 73 74 73 2e 20 20 54 45 58 54  .** tests.  TEXT
36b00 20 72 65 73 75 6c 74 20 63 6f 6e 74 61 69 6e 69   result containi
36b01 6e 67 20 74 68 65 20 6f 72 64 65 72 65 64 20 6c  ng the ordered l
36b02 69 73 74 20 6f 66 20 74 65 72 6d 73 20 6a 6f 69  ist of terms joi
36b03 6e 65 64 20 62 79 0a 2a 2a 20 73 70 61 63 65 73  ned by.** spaces
36b04 2e 20 20 64 75 6d 70 5f 74 65 72 6d 73 28 74 2c  .  dump_terms(t,
36b05 20 6c 65 76 65 6c 2c 20 69 64 78 29 20 64 75 6d   level, idx) dum
36b06 70 73 20 74 68 65 20 74 65 72 6d 73 20 66 6f 72  ps the terms for
36b07 20 74 68 65 20 73 65 67 6d 65 6e 74 0a 2a 2a 20   the segment.** 
36b08 73 70 65 63 69 66 69 65 64 20 62 79 20 6c 65 76  specified by lev
36b09 65 6c 2c 20 69 64 78 20 28 69 6e 20 25 5f 73 65  el, idx (in %_se
36b0a 67 64 69 72 29 2c 20 77 68 69 6c 65 20 64 75 6d  gdir), while dum
36b0b 70 5f 74 65 72 6d 73 28 74 29 20 64 75 6d 70 73  p_terms(t) dumps
36b0c 0a 2a 2a 20 61 6c 6c 20 74 65 72 6d 73 20 69 6e  .** all terms in
36b0d 20 74 68 65 20 69 6e 64 65 78 2e 20 20 49 6e 20   the index.  In 
36b0e 62 6f 74 68 20 63 61 73 65 73 20 74 20 69 73 20  both cases t is 
36b0f 74 68 65 20 66 74 73 20 74 61 62 6c 65 27 73 20  the fts table's 
36b10 6d 61 67 69 63 0a 2a 2a 20 74 61 62 6c 65 2d 6e  magic.** table-n
36b11 61 6d 65 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a  amed column..*/.
36b12 73 74 61 74 69 63 20 76 6f 69 64 20 64 75 6d 70  static void dump
36b13 54 65 72 6d 73 46 75 6e 63 28 0a 20 20 73 71 6c  TermsFunc(.  sql
36b14 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43  ite3_context *pC
36b15 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
36b16 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
36b17 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 66 75  e **argv.){.  fu
36b18 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 70  lltext_cursor *p
36b19 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 61 72  Cursor;.  if( ar
36b1a 67 63 21 3d 33 20 26 26 20 61 72 67 63 21 3d 31  gc!=3 && argc!=1
36b1b 20 29 7b 0a 20 20 20 20 67 65 6e 65 72 61 74 65   ){.    generate
36b1c 45 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20  Error(pContext, 
36b1d 22 64 75 6d 70 5f 74 65 72 6d 73 22 2c 20 22 69  "dump_terms", "i
36b1e 6e 63 6f 72 72 65 63 74 20 61 72 67 75 6d 65 6e  ncorrect argumen
36b1f 74 73 22 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  ts");.  }else if
36b20 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ( sqlite3_value_
36b21 74 79 70 65 28 61 72 67 76 5b 30 5d 29 21 3d 53  type(argv[0])!=S
36b22 51 4c 49 54 45 5f 42 4c 4f 42 20 7c 7c 0a 20 20  QLITE_BLOB ||.  
36b23 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
36b24 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72  3_value_bytes(ar
36b25 67 76 5b 30 5d 29 21 3d 73 69 7a 65 6f 66 28 70  gv[0])!=sizeof(p
36b26 43 75 72 73 6f 72 29 20 29 7b 0a 20 20 20 20 67  Cursor) ){.    g
36b27 65 6e 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f  enerateError(pCo
36b28 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f 74 65 72  ntext, "dump_ter
36b29 6d 73 22 2c 20 22 69 6c 6c 65 67 61 6c 20 66 69  ms", "illegal fi
36b2a 72 73 74 20 61 72 67 75 6d 65 6e 74 22 29 3b 0a  rst argument");.
36b2b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 75 6c    }else{.    ful
36b2c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 3b 0a 20  ltext_vtab *v;. 
36b2d 20 20 20 66 74 73 33 48 61 73 68 20 74 65 72 6d     fts3Hash term
36b2e 73 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  s;.    sqlite3_s
36b2f 74 6d 74 20 2a 73 20 3d 20 4e 55 4c 4c 3b 0a 20  tmt *s = NULL;. 
36b30 20 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 20 20     int rc;..    
36b31 6d 65 6d 63 70 79 28 26 70 43 75 72 73 6f 72 2c  memcpy(&pCursor,
36b32 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
36b33 6c 6f 62 28 61 72 67 76 5b 30 5d 29 2c 20 73 69  lob(argv[0]), si
36b34 7a 65 6f 66 28 70 43 75 72 73 6f 72 29 29 3b 0a  zeof(pCursor));.
36b35 20 20 20 20 76 20 3d 20 63 75 72 73 6f 72 5f 76      v = cursor_v
36b36 74 61 62 28 70 43 75 72 73 6f 72 29 3b 0a 0a 20  tab(pCursor);.. 
36b37 20 20 20 2f 2a 20 49 66 20 70 61 73 73 65 64 20     /* If passed 
36b38 6f 6e 6c 79 20 74 68 65 20 63 75 72 73 6f 72 20  only the cursor 
36b39 63 6f 6c 75 6d 6e 2c 20 67 65 74 20 61 6c 6c 20  column, get all 
36b3a 73 65 67 6d 65 6e 74 73 2e 20 20 4f 74 68 65 72  segments.  Other
36b3b 77 69 73 65 0a 20 20 20 20 2a 2a 20 67 65 74 20  wise.    ** get 
36b3c 74 68 65 20 73 65 67 6d 65 6e 74 20 64 65 73 63  the segment desc
36b3d 72 69 62 65 64 20 62 79 20 74 68 65 20 66 6f 6c  ribed by the fol
36b3e 6c 6f 77 69 6e 67 20 74 77 6f 20 61 72 67 75 6d  lowing two argum
36b3f 65 6e 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ents..    */.   
36b40 20 69 66 28 20 61 72 67 63 3d 3d 31 20 29 7b 0a   if( argc==1 ){.
36b41 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 5f 67        rc = sql_g
36b42 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20  et_statement(v, 
36b43 53 45 47 44 49 52 5f 53 45 4c 45 43 54 5f 41 4c  SEGDIR_SELECT_AL
36b44 4c 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 20  L_STMT, &s);.   
36b45 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63   }else{.      rc
36b46 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65   = sql_get_state
36b47 6d 65 6e 74 28 76 2c 20 53 45 47 44 49 52 5f 53  ment(v, SEGDIR_S
36b48 45 4c 45 43 54 5f 53 45 47 4d 45 4e 54 5f 53 54  ELECT_SEGMENT_ST
36b49 4d 54 2c 20 26 73 29 3b 0a 20 20 20 20 20 20 69  MT, &s);.      i
36b4a 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
36b4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
36b4c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
36b4d 74 28 73 2c 20 31 2c 20 73 71 6c 69 74 65 33 5f  t(s, 1, sqlite3_
36b4e 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b 31  value_int(argv[1
36b4f 5d 29 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ]));.        if(
36b50 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
36b51 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  {.          rc =
36b52 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
36b53 74 28 73 2c 20 32 2c 20 73 71 6c 69 74 65 33 5f  t(s, 2, sqlite3_
36b54 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b 32  value_int(argv[2
36b55 5d 29 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ]));.        }. 
36b56 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
36b57 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
36b58 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 65 6e  _OK ){.      gen
36b59 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f 6e 74  erateError(pCont
36b5a 65 78 74 2c 20 22 64 75 6d 70 5f 74 65 72 6d 73  ext, "dump_terms
36b5b 22 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20  ", NULL);.      
36b5c 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 0a 20  return;.    }.. 
36b5d 20 20 20 2f 2a 20 43 6f 6c 6c 65 63 74 20 74 68     /* Collect th
36b5e 65 20 74 65 72 6d 73 20 66 6f 72 20 65 61 63 68  e terms for each
36b5f 20 73 65 67 6d 65 6e 74 2e 20 2a 2f 0a 20 20 20   segment. */.   
36b60 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68   sqlite3Fts3Hash
36b61 49 6e 69 74 28 26 74 65 72 6d 73 2c 20 46 54 53  Init(&terms, FTS
36b62 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 2c 20 31  3_HASH_STRING, 1
36b63 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 72  );.    while( (r
36b64 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  c = sqlite3_step
36b65 28 73 29 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57  (s))==SQLITE_ROW
36b66 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 63   ){.      rc = c
36b67 6f 6c 6c 65 63 74 53 65 67 6d 65 6e 74 54 65 72  ollectSegmentTer
36b68 6d 73 28 76 2c 20 73 2c 20 26 74 65 72 6d 73 29  ms(v, s, &terms)
36b69 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
36b6a 53 51 4c 49 54 45 5f 4f 4b 20 29 20 62 72 65 61  SQLITE_OK ) brea
36b6b 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  k;.    }..    if
36b6c 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc!=SQLITE_DON
36b6d 45 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  E ){.      sqlit
36b6e 65 33 5f 72 65 73 65 74 28 73 29 3b 0a 20 20 20  e3_reset(s);.   
36b6f 20 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f 72     generateError
36b70 28 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d 70  (pContext, "dump
36b71 5f 74 65 72 6d 73 22 2c 20 4e 55 4c 4c 29 3b 0a  _terms", NULL);.
36b72 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
36b73 20 63 6f 6e 73 74 20 69 6e 74 20 6e 54 65 72 6d   const int nTerm
36b74 73 20 3d 20 66 74 73 33 48 61 73 68 43 6f 75 6e  s = fts3HashCoun
36b75 74 28 26 74 65 72 6d 73 29 3b 0a 20 20 20 20 20  t(&terms);.     
36b76 20 69 66 28 20 6e 54 65 72 6d 73 3e 30 20 29 7b   if( nTerms>0 ){
36b77 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 67 65  .        rc = ge
36b78 6e 65 72 61 74 65 54 65 72 6d 73 52 65 73 75 6c  nerateTermsResul
36b79 74 28 70 43 6f 6e 74 65 78 74 2c 20 26 74 65 72  t(pContext, &ter
36b7a 6d 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ms);.        if(
36b7b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45   rc==SQLITE_NOME
36b7c 4d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67  M ){.          g
36b7d 65 6e 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f  enerateError(pCo
36b7e 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f 74 65 72  ntext, "dump_ter
36b7f 6d 73 22 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d  ms", "out of mem
36b80 6f 72 79 22 29 3b 0a 20 20 20 20 20 20 20 20 7d  ory");.        }
36b81 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
36b82 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
36b83 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 20 20  TE_OK );.       
36b84 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69   }.      }else i
36b85 66 28 20 61 72 67 63 3d 3d 33 20 29 7b 0a 20 20  f( argc==3 ){.  
36b86 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 70 65        /* The spe
36b87 63 69 66 69 63 20 73 65 67 6d 65 6e 74 20 61 73  cific segment as
36b88 6b 65 64 20 66 6f 72 20 63 6f 75 6c 64 20 6e 6f  ked for could no
36b89 74 20 62 65 20 66 6f 75 6e 64 2e 20 2a 2f 0a 20  t be found. */. 
36b8a 20 20 20 20 20 20 20 67 65 6e 65 72 61 74 65 45         generateE
36b8b 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 22  rror(pContext, "
36b8c 64 75 6d 70 5f 74 65 72 6d 73 22 2c 20 22 73 65  dump_terms", "se
36b8d 67 6d 65 6e 74 20 6e 6f 74 20 66 6f 75 6e 64 22  gment not found"
36b8e 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
36b8f 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 20 73 65          /* No se
36b90 67 6d 65 6e 74 73 20 66 6f 75 6e 64 2e 20 2a 2f  gments found. */
36b91 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 4f 44 4f  .        /* TODO
36b92 28 73 68 65 73 73 29 3a 20 49 74 20 73 68 6f 75  (shess): It shou
36b93 6c 64 20 62 65 20 69 6d 70 6f 73 73 69 62 6c 65  ld be impossible
36b94 20 74 6f 20 72 65 61 63 68 20 74 68 69 73 2e 20   to reach this. 
36b95 20 54 68 69 73 0a 20 20 20 20 20 20 20 20 2a 2a   This.        **
36b96 20 63 61 73 65 20 63 61 6e 20 6f 6e 6c 79 20 68   case can only h
36b97 61 70 70 65 6e 20 66 6f 72 20 61 6e 20 65 6d 70  appen for an emp
36b98 74 79 20 74 61 62 6c 65 2c 20 69 6e 20 77 68 69  ty table, in whi
36b99 63 68 20 63 61 73 65 0a 20 20 20 20 20 20 20 20  ch case.        
36b9a 2a 2a 20 53 51 4c 69 74 65 20 68 61 73 20 6e 6f  ** SQLite has no
36b9b 20 72 6f 77 73 20 74 6f 20 63 61 6c 6c 20 74 68   rows to call th
36b9c 69 73 20 66 75 6e 63 74 69 6f 6e 20 6f 6e 2e 0a  is function on..
36b9d 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
36b9e 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
36b9f 74 5f 6e 75 6c 6c 28 70 43 6f 6e 74 65 78 74 29  t_null(pContext)
36ba0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
36ba1 20 20 20 20 73 71 6c 69 74 65 33 46 74 73 33 48      sqlite3Fts3H
36ba2 61 73 68 43 6c 65 61 72 28 26 74 65 72 6d 73 29  ashClear(&terms)
36ba3 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 45 78 70 61  ;.  }.}../* Expa
36ba4 6e 64 20 74 68 65 20 44 4c 5f 44 45 46 41 55 4c  nd the DL_DEFAUL
36ba5 54 20 64 6f 63 6c 69 73 74 20 69 6e 20 70 44 61  T doclist in pDa
36ba6 74 61 20 69 6e 74 6f 20 61 20 74 65 78 74 20 72  ta into a text r
36ba7 65 73 75 6c 74 20 69 6e 0a 2a 2a 20 70 43 6f 6e  esult in.** pCon
36ba8 74 65 78 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  text..*/.static 
36ba9 76 6f 69 64 20 63 72 65 61 74 65 44 6f 63 6c 69  void createDocli
36baa 73 74 52 65 73 75 6c 74 28 73 71 6c 69 74 65 33  stResult(sqlite3
36bab 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65  _context *pConte
36bac 78 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  xt,.            
36bad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36bae 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
36baf 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61  pData, int nData
36bb0 29 7b 0a 20 20 44 61 74 61 42 75 66 66 65 72 20  ){.  DataBuffer 
36bb1 64 75 6d 70 3b 0a 20 20 44 4c 52 65 61 64 65 72  dump;.  DLReader
36bb2 20 64 6c 52 65 61 64 65 72 3b 0a 0a 20 20 61 73   dlReader;..  as
36bb3 73 65 72 74 28 20 70 44 61 74 61 21 3d 4e 55 4c  sert( pData!=NUL
36bb4 4c 20 26 26 20 6e 44 61 74 61 3e 30 20 29 3b 0a  L && nData>0 );.
36bb5 0a 20 20 64 61 74 61 42 75 66 66 65 72 49 6e 69  .  dataBufferIni
36bb6 74 28 26 64 75 6d 70 2c 20 30 29 3b 0a 20 20 64  t(&dump, 0);.  d
36bb7 6c 72 49 6e 69 74 28 26 64 6c 52 65 61 64 65 72  lrInit(&dlReader
36bb8 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20 70 44  , DL_DEFAULT, pD
36bb9 61 74 61 2c 20 6e 44 61 74 61 29 3b 0a 20 20 66  ata, nData);.  f
36bba 6f 72 28 20 3b 20 21 64 6c 72 41 74 45 6e 64 28  or( ; !dlrAtEnd(
36bbb 26 64 6c 52 65 61 64 65 72 29 3b 20 64 6c 72 53  &dlReader); dlrS
36bbc 74 65 70 28 26 64 6c 52 65 61 64 65 72 29 20 29  tep(&dlReader) )
36bbd 7b 0a 20 20 20 20 63 68 61 72 20 62 75 66 5b 32  {.    char buf[2
36bbe 35 36 5d 3b 0a 20 20 20 20 50 4c 52 65 61 64 65  56];.    PLReade
36bbf 72 20 70 6c 52 65 61 64 65 72 3b 0a 0a 20 20 20  r plReader;..   
36bc0 20 70 6c 72 49 6e 69 74 28 26 70 6c 52 65 61 64   plrInit(&plRead
36bc1 65 72 2c 20 26 64 6c 52 65 61 64 65 72 29 3b 0a  er, &dlReader);.
36bc2 20 20 20 20 69 66 28 20 44 4c 5f 44 45 46 41 55      if( DL_DEFAU
36bc3 4c 54 3d 3d 44 4c 5f 44 4f 43 49 44 53 20 7c 7c  LT==DL_DOCIDS ||
36bc4 20 70 6c 72 41 74 45 6e 64 28 26 70 6c 52 65 61   plrAtEnd(&plRea
36bc5 64 65 72 29 20 29 7b 0a 20 20 20 20 20 20 73 71  der) ){.      sq
36bc6 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
36bc7 69 7a 65 6f 66 28 62 75 66 29 2c 20 62 75 66 2c  izeof(buf), buf,
36bc8 20 22 5b 25 6c 6c 64 5d 20 22 2c 20 64 6c 72 44   "[%lld] ", dlrD
36bc9 6f 63 69 64 28 26 64 6c 52 65 61 64 65 72 29 29  ocid(&dlReader))
36bca 3b 0a 20 20 20 20 20 20 64 61 74 61 42 75 66 66  ;.      dataBuff
36bcb 65 72 41 70 70 65 6e 64 28 26 64 75 6d 70 2c 20  erAppend(&dump, 
36bcc 62 75 66 2c 20 73 74 72 6c 65 6e 28 62 75 66 29  buf, strlen(buf)
36bcd 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
36bce 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 20      int iColumn 
36bcf 3d 20 70 6c 72 43 6f 6c 75 6d 6e 28 26 70 6c 52  = plrColumn(&plR
36bd0 65 61 64 65 72 29 3b 0a 0a 20 20 20 20 20 20 73  eader);..      s
36bd1 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
36bd2 73 69 7a 65 6f 66 28 62 75 66 29 2c 20 62 75 66  sizeof(buf), buf
36bd3 2c 20 22 5b 25 6c 6c 64 20 25 64 5b 22 2c 0a 20  , "[%lld %d[",. 
36bd4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36bd5 20 20 20 20 20 20 64 6c 72 44 6f 63 69 64 28 26        dlrDocid(&
36bd6 64 6c 52 65 61 64 65 72 29 2c 20 69 43 6f 6c 75  dlReader), iColu
36bd7 6d 6e 29 3b 0a 20 20 20 20 20 20 64 61 74 61 42  mn);.      dataB
36bd8 75 66 66 65 72 41 70 70 65 6e 64 28 26 64 75 6d  ufferAppend(&dum
36bd9 70 2c 20 62 75 66 2c 20 73 74 72 6c 65 6e 28 62  p, buf, strlen(b
36bda 75 66 29 29 3b 0a 0a 20 20 20 20 20 20 66 6f 72  uf));..      for
36bdb 28 20 3b 20 21 70 6c 72 41 74 45 6e 64 28 26 70  ( ; !plrAtEnd(&p
36bdc 6c 52 65 61 64 65 72 29 3b 20 70 6c 72 53 74 65  lReader); plrSte
36bdd 70 28 26 70 6c 52 65 61 64 65 72 29 20 29 7b 0a  p(&plReader) ){.
36bde 20 20 20 20 20 20 20 20 69 66 28 20 70 6c 72 43          if( plrC
36bdf 6f 6c 75 6d 6e 28 26 70 6c 52 65 61 64 65 72 29  olumn(&plReader)
36be0 21 3d 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  !=iColumn ){.   
36be1 20 20 20 20 20 20 20 69 43 6f 6c 75 6d 6e 20 3d         iColumn =
36be2 20 70 6c 72 43 6f 6c 75 6d 6e 28 26 70 6c 52 65   plrColumn(&plRe
36be3 61 64 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20  ader);.         
36be4 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
36be5 66 28 73 69 7a 65 6f 66 28 62 75 66 29 2c 20 62  f(sizeof(buf), b
36be6 75 66 2c 20 22 5d 20 25 64 5b 22 2c 20 69 43 6f  uf, "] %d[", iCo
36be7 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 20 20 20  lumn);.         
36be8 20 61 73 73 65 72 74 28 20 64 75 6d 70 2e 6e 44   assert( dump.nD
36be9 61 74 61 3e 30 20 29 3b 0a 20 20 20 20 20 20 20  ata>0 );.       
36bea 20 20 20 64 75 6d 70 2e 6e 44 61 74 61 2d 2d 3b     dump.nData--;
36beb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36bec 20 20 20 20 20 2f 2a 20 4f 76 65 72 77 72 69 74       /* Overwrit
36bed 65 20 74 72 61 69 6c 69 6e 67 20 73 70 61 63 65  e trailing space
36bee 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 61  . */.          a
36bef 73 73 65 72 74 28 20 64 75 6d 70 2e 70 44 61 74  ssert( dump.pDat
36bf0 61 5b 64 75 6d 70 2e 6e 44 61 74 61 5d 3d 3d 27  a[dump.nData]=='
36bf1 20 27 29 3b 0a 20 20 20 20 20 20 20 20 20 20 64   ');.          d
36bf2 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28  ataBufferAppend(
36bf3 26 64 75 6d 70 2c 20 62 75 66 2c 20 73 74 72 6c  &dump, buf, strl
36bf4 65 6e 28 62 75 66 29 29 3b 0a 20 20 20 20 20 20  en(buf));.      
36bf5 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
36bf6 44 4c 5f 44 45 46 41 55 4c 54 3d 3d 44 4c 5f 50  DL_DEFAULT==DL_P
36bf7 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53  OSITIONS_OFFSETS
36bf8 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71   ){.          sq
36bf9 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
36bfa 69 7a 65 6f 66 28 62 75 66 29 2c 20 62 75 66 2c  izeof(buf), buf,
36bfb 20 22 25 64 2c 25 64 2c 25 64 20 22 2c 0a 20 20   "%d,%d,%d ",.  
36bfc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36bfd 20 20 20 20 20 20 20 20 20 70 6c 72 50 6f 73 69           plrPosi
36bfe 74 69 6f 6e 28 26 70 6c 52 65 61 64 65 72 29 2c  tion(&plReader),
36bff 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
36c00 20 20 20 20 20 20 20 20 20 20 20 20 70 6c 72 53              plrS
36c01 74 61 72 74 4f 66 66 73 65 74 28 26 70 6c 52 65  tartOffset(&plRe
36c02 61 64 65 72 29 2c 20 70 6c 72 45 6e 64 4f 66 66  ader), plrEndOff
36c03 73 65 74 28 26 70 6c 52 65 61 64 65 72 29 29 3b  set(&plReader));
36c04 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69  .        }else i
36c05 66 28 20 44 4c 5f 44 45 46 41 55 4c 54 3d 3d 44  f( DL_DEFAULT==D
36c06 4c 5f 50 4f 53 49 54 49 4f 4e 53 20 29 7b 0a 20  L_POSITIONS ){. 
36c07 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
36c08 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66  _snprintf(sizeof
36c09 28 62 75 66 29 2c 20 62 75 66 2c 20 22 25 64 20  (buf), buf, "%d 
36c0a 22 2c 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28 26  ", plrPosition(&
36c0b 70 6c 52 65 61 64 65 72 29 29 3b 0a 20 20 20 20  plReader));.    
36c0c 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
36c0d 20 20 20 20 20 61 73 73 65 72 74 28 20 4e 55 4c       assert( NUL
36c0e 4c 3d 3d 22 55 6e 68 61 6e 64 6c 65 64 20 44 4c  L=="Unhandled DL
36c0f 5f 44 45 46 41 55 4c 54 20 76 61 6c 75 65 22 29  _DEFAULT value")
36c10 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
36c11 20 20 20 20 64 61 74 61 42 75 66 66 65 72 41 70      dataBufferAp
36c12 70 65 6e 64 28 26 64 75 6d 70 2c 20 62 75 66 2c  pend(&dump, buf,
36c13 20 73 74 72 6c 65 6e 28 62 75 66 29 29 3b 0a 20   strlen(buf));. 
36c14 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 6c 72       }.      plr
36c15 44 65 73 74 72 6f 79 28 26 70 6c 52 65 61 64 65  Destroy(&plReade
36c16 72 29 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72  r);..      asser
36c17 74 28 20 64 75 6d 70 2e 6e 44 61 74 61 3e 30 20  t( dump.nData>0 
36c18 29 3b 0a 20 20 20 20 20 20 64 75 6d 70 2e 6e 44  );.      dump.nD
36c19 61 74 61 2d 2d 3b 20 20 20 20 20 20 20 20 20 20  ata--;          
36c1a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 76             /* Ov
36c1b 65 72 77 72 69 74 65 20 74 72 61 69 6c 69 6e 67  erwrite trailing
36c1c 20 73 70 61 63 65 2e 20 2a 2f 0a 20 20 20 20 20   space. */.     
36c1d 20 61 73 73 65 72 74 28 20 64 75 6d 70 2e 70 44   assert( dump.pD
36c1e 61 74 61 5b 64 75 6d 70 2e 6e 44 61 74 61 5d 3d  ata[dump.nData]=
36c1f 3d 27 20 27 29 3b 0a 20 20 20 20 20 20 64 61 74  =' ');.      dat
36c20 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 26 64  aBufferAppend(&d
36c21 75 6d 70 2c 20 22 5d 5d 20 22 2c 20 33 29 3b 0a  ump, "]] ", 3);.
36c22 20 20 20 20 7d 0a 20 20 7d 0a 20 20 64 6c 72 44      }.  }.  dlrD
36c23 65 73 74 72 6f 79 28 26 64 6c 52 65 61 64 65 72  estroy(&dlReader
36c24 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 64 75  );..  assert( du
36c25 6d 70 2e 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20  mp.nData>0 );.  
36c26 64 75 6d 70 2e 6e 44 61 74 61 2d 2d 3b 20 20 20  dump.nData--;   
36c27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36c28 20 20 2f 2a 20 4f 76 65 72 77 72 69 74 65 20 74    /* Overwrite t
36c29 72 61 69 6c 69 6e 67 20 73 70 61 63 65 2e 20 2a  railing space. *
36c2a 2f 0a 20 20 61 73 73 65 72 74 28 20 64 75 6d 70  /.  assert( dump
36c2b 2e 70 44 61 74 61 5b 64 75 6d 70 2e 6e 44 61 74  .pData[dump.nDat
36c2c 61 5d 3d 3d 27 20 27 29 3b 0a 20 20 64 75 6d 70  a]==' ');.  dump
36c2d 2e 70 44 61 74 61 5b 64 75 6d 70 2e 6e 44 61 74  .pData[dump.nDat
36c2e 61 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 61 73 73  a] = '\0';.  ass
36c2f 65 72 74 28 20 64 75 6d 70 2e 6e 44 61 74 61 3e  ert( dump.nData>
36c30 30 20 29 3b 0a 0a 20 20 2f 2a 20 50 61 73 73 65  0 );..  /* Passe
36c31 73 20 6f 77 6e 65 72 73 68 69 70 20 6f 66 20 64  s ownership of d
36c32 75 6d 70 27 73 20 62 75 66 66 65 72 20 74 6f 20  ump's buffer to 
36c33 70 43 6f 6e 74 65 78 74 2e 20 2a 2f 0a 20 20 73  pContext. */.  s
36c34 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
36c35 78 74 28 70 43 6f 6e 74 65 78 74 2c 20 64 75 6d  xt(pContext, dum
36c36 70 2e 70 44 61 74 61 2c 20 64 75 6d 70 2e 6e 44  p.pData, dump.nD
36c37 61 74 61 2c 20 73 71 6c 69 74 65 33 5f 66 72 65  ata, sqlite3_fre
36c38 65 29 3b 0a 20 20 64 75 6d 70 2e 70 44 61 74 61  e);.  dump.pData
36c39 20 3d 20 4e 55 4c 4c 3b 0a 20 20 64 75 6d 70 2e   = NULL;.  dump.
36c3a 6e 44 61 74 61 20 3d 20 64 75 6d 70 2e 6e 43 61  nData = dump.nCa
36c3b 70 61 63 69 74 79 20 3d 20 30 3b 0a 7d 0a 0a 2f  pacity = 0;.}../
36c3c 2a 20 49 6d 70 6c 65 6d 65 6e 74 73 20 64 75 6d  * Implements dum
36c3d 70 5f 64 6f 63 6c 69 73 74 28 29 20 66 6f 72 20  p_doclist() for 
36c3e 75 73 65 20 69 6e 20 69 6e 73 70 65 63 74 69 6e  use in inspectin
36c3f 67 20 74 68 65 20 66 74 73 33 20 69 6e 64 65 78  g the fts3 index
36c40 20 66 72 6f 6d 0a 2a 2a 20 74 65 73 74 73 2e 20   from.** tests. 
36c41 20 54 45 58 54 20 72 65 73 75 6c 74 20 63 6f 6e   TEXT result con
36c42 74 61 69 6e 69 6e 67 20 61 20 73 74 72 69 6e 67  taining a string
36c43 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
36c44 6f 66 20 74 68 65 0a 2a 2a 20 64 6f 63 6c 69 73  of the.** doclis
36c45 74 20 66 6f 72 20 74 68 65 20 69 6e 64 69 63 61  t for the indica
36c46 74 65 64 20 74 65 72 6d 2e 20 20 64 75 6d 70 5f  ted term.  dump_
36c47 64 6f 63 6c 69 73 74 28 74 2c 20 74 65 72 6d 2c  doclist(t, term,
36c48 20 6c 65 76 65 6c 2c 20 69 64 78 29 0a 2a 2a 20   level, idx).** 
36c49 64 75 6d 70 73 20 74 68 65 20 64 6f 63 6c 69 73  dumps the doclis
36c4a 74 20 66 6f 72 20 74 65 72 6d 20 66 72 6f 6d 20  t for term from 
36c4b 74 68 65 20 73 65 67 6d 65 6e 74 20 73 70 65 63  the segment spec
36c4c 69 66 69 65 64 20 62 79 20 6c 65 76 65 6c 2c 20  ified by level, 
36c4d 69 64 78 0a 2a 2a 20 28 69 6e 20 25 5f 73 65 67  idx.** (in %_seg
36c4e 64 69 72 29 2c 20 77 68 69 6c 65 20 64 75 6d 70  dir), while dump
36c4f 5f 64 6f 63 6c 69 73 74 28 74 2c 20 74 65 72 6d  _doclist(t, term
36c50 29 20 64 75 6d 70 73 20 74 68 65 20 6c 6f 67 69  ) dumps the logi
36c51 63 61 6c 0a 2a 2a 20 64 6f 63 6c 69 73 74 20 66  cal.** doclist f
36c52 6f 72 20 74 68 65 20 74 65 72 6d 20 61 63 72 6f  or the term acro
36c53 73 73 20 61 6c 6c 20 73 65 67 6d 65 6e 74 73 2e  ss all segments.
36c54 20 20 54 68 65 20 70 65 72 2d 73 65 67 6d 65 6e    The per-segmen
36c55 74 20 64 6f 63 6c 69 73 74 0a 2a 2a 20 63 61 6e  t doclist.** can
36c56 20 63 6f 6e 74 61 69 6e 20 64 65 6c 65 74 69 6f   contain deletio
36c57 6e 73 2c 20 77 68 69 6c 65 20 74 68 65 20 66 75  ns, while the fu
36c58 6c 6c 2d 69 6e 64 65 78 20 64 6f 63 6c 69 73 74  ll-index doclist
36c59 20 77 69 6c 6c 20 6e 6f 74 0a 2a 2a 20 28 64 65   will not.** (de
36c5a 6c 65 74 69 6f 6e 73 20 61 72 65 20 6f 6d 69 74  letions are omit
36c5b 74 65 64 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 73 75  ted)..**.** Resu
36c5c 6c 74 20 66 6f 72 6d 61 74 73 20 64 69 66 66 65  lt formats diffe
36c5d 72 20 77 69 74 68 20 74 68 65 20 73 65 74 74 69  r with the setti
36c5e 6e 67 20 6f 66 20 44 4c 5f 44 45 46 41 55 4c 54  ng of DL_DEFAULT
36c5f 53 2e 20 20 45 78 61 6d 70 6c 65 73 3a 0a 2a 2a  S.  Examples:.**
36c60 0a 2a 2a 20 44 4c 5f 44 4f 43 49 44 53 3a 20 5b  .** DL_DOCIDS: [
36c61 31 5d 20 5b 33 5d 20 5b 37 5d 0a 2a 2a 20 44 4c  1] [3] [7].** DL
36c62 5f 50 4f 53 49 54 49 4f 4e 53 3a 20 5b 31 20 30  _POSITIONS: [1 0
36c63 5b 30 20 34 5d 20 31 5b 31 37 5d 5d 20 5b 33 20  [0 4] 1[17]] [3 
36c64 31 5b 35 5d 5d 0a 2a 2a 20 44 4c 5f 50 4f 53 49  1[5]].** DL_POSI
36c65 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53 3a 20 5b  TIONS_OFFSETS: [
36c66 31 20 30 5b 30 2c 30 2c 33 20 34 2c 32 33 2c 32  1 0[0,0,3 4,23,2
36c67 36 5d 20 31 5b 31 37 2c 31 30 32 2c 31 30 35 5d  6] 1[17,102,105]
36c68 5d 20 5b 33 20 31 5b 35 2c 32 30 2c 32 33 5d 5d  ] [3 1[5,20,23]]
36c69 0a 2a 2a 0a 2a 2a 20 49 6e 20 65 61 63 68 20 63  .**.** In each c
36c6a 61 73 65 20 74 68 65 20 6e 75 6d 62 65 72 20 61  ase the number a
36c6b 66 74 65 72 20 74 68 65 20 6f 75 74 65 72 20 27  fter the outer '
36c6c 5b 27 20 69 73 20 74 68 65 20 64 6f 63 69 64 2e  [' is the docid.
36c6d 20 20 49 6e 20 74 68 65 0a 2a 2a 20 6c 61 74 74    In the.** latt
36c6e 65 72 20 74 77 6f 20 63 61 73 65 73 2c 20 74 68  er two cases, th
36c6f 65 20 6e 75 6d 62 65 72 20 62 65 66 6f 72 65 20  e number before 
36c70 74 68 65 20 69 6e 6e 65 72 20 27 5b 27 20 69 73  the inner '[' is
36c71 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 61   the column.** a
36c72 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
36c73 68 65 20 76 61 6c 75 65 73 20 77 69 74 68 69 6e  he values within
36c74 2e 20 20 46 6f 72 20 44 4c 5f 50 4f 53 49 54 49  .  For DL_POSITI
36c75 4f 4e 53 20 74 68 65 20 6e 75 6d 62 65 72 73 0a  ONS the numbers.
36c76 2a 2a 20 77 69 74 68 69 6e 20 61 72 65 20 74 68  ** within are th
36c77 65 20 70 6f 73 69 74 69 6f 6e 73 2c 20 66 6f 72  e positions, for
36c78 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f 46   DL_POSITIONS_OF
36c79 46 53 45 54 53 20 74 68 65 79 20 61 72 65 20 74  FSETS they are t
36c7a 68 65 0a 2a 2a 20 70 6f 73 69 74 69 6f 6e 2c 20  he.** position, 
36c7b 74 68 65 20 73 74 61 72 74 20 6f 66 66 73 65 74  the start offset
36c7c 2c 20 61 6e 64 20 74 68 65 20 65 6e 64 20 6f 66  , and the end of
36c7d 66 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  fset..*/.static 
36c7e 76 6f 69 64 20 64 75 6d 70 44 6f 63 6c 69 73 74  void dumpDoclist
36c7f 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
36c80 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78  context *pContex
36c81 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 73  t,.  int argc, s
36c82 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
36c83 72 67 76 0a 29 7b 0a 20 20 66 75 6c 6c 74 65 78  rgv.){.  fulltex
36c84 74 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f  t_cursor *pCurso
36c85 72 3b 0a 20 20 69 66 28 20 61 72 67 63 21 3d 32  r;.  if( argc!=2
36c86 20 26 26 20 61 72 67 63 21 3d 34 20 29 7b 0a 20   && argc!=4 ){. 
36c87 20 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f 72     generateError
36c88 28 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d 70  (pContext, "dump
36c89 5f 64 6f 63 6c 69 73 74 22 2c 20 22 69 6e 63 6f  _doclist", "inco
36c8a 72 72 65 63 74 20 61 72 67 75 6d 65 6e 74 73 22  rrect arguments"
36c8b 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73  );.  }else if( s
36c8c 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
36c8d 65 28 61 72 67 76 5b 30 5d 29 21 3d 53 51 4c 49  e(argv[0])!=SQLI
36c8e 54 45 5f 42 4c 4f 42 20 7c 7c 0a 20 20 20 20 20  TE_BLOB ||.     
36c8f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76         sqlite3_v
36c90 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b  alue_bytes(argv[
36c91 30 5d 29 21 3d 73 69 7a 65 6f 66 28 70 43 75 72  0])!=sizeof(pCur
36c92 73 6f 72 29 20 29 7b 0a 20 20 20 20 67 65 6e 65  sor) ){.    gene
36c93 72 61 74 65 45 72 72 6f 72 28 70 43 6f 6e 74 65  rateError(pConte
36c94 78 74 2c 20 22 64 75 6d 70 5f 64 6f 63 6c 69 73  xt, "dump_doclis
36c95 74 22 2c 20 22 69 6c 6c 65 67 61 6c 20 66 69 72  t", "illegal fir
36c96 73 74 20 61 72 67 75 6d 65 6e 74 22 29 3b 0a 20  st argument");. 
36c97 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74   }else if( sqlit
36c98 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
36c99 67 76 5b 31 5d 29 3d 3d 4e 55 4c 4c 20 7c 7c 0a  gv[1])==NULL ||.
36c9a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
36c9b 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
36c9c 72 67 76 5b 31 5d 29 5b 30 5d 3d 3d 27 5c 30 27  rgv[1])[0]=='\0'
36c9d 20 29 7b 0a 20 20 20 20 67 65 6e 65 72 61 74 65   ){.    generate
36c9e 45 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20  Error(pContext, 
36c9f 22 64 75 6d 70 5f 64 6f 63 6c 69 73 74 22 2c 20  "dump_doclist", 
36ca0 22 65 6d 70 74 79 20 73 65 63 6f 6e 64 20 61 72  "empty second ar
36ca1 67 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 65 6c 73  gument");.  }els
36ca2 65 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  e{.    const cha
36ca3 72 20 2a 70 54 65 72 6d 20 3d 20 28 63 6f 6e 73  r *pTerm = (cons
36ca4 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33  t char *)sqlite3
36ca5 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
36ca6 5b 31 5d 29 3b 0a 20 20 20 20 63 6f 6e 73 74 20  [1]);.    const 
36ca7 69 6e 74 20 6e 54 65 72 6d 20 3d 20 73 74 72 6c  int nTerm = strl
36ca8 65 6e 28 70 54 65 72 6d 29 3b 0a 20 20 20 20 66  en(pTerm);.    f
36ca9 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 3b  ulltext_vtab *v;
36caa 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20  .    int rc;.   
36cab 20 44 61 74 61 42 75 66 66 65 72 20 64 6f 63 6c   DataBuffer docl
36cac 69 73 74 3b 0a 0a 20 20 20 20 6d 65 6d 63 70 79  ist;..    memcpy
36cad 28 26 70 43 75 72 73 6f 72 2c 20 73 71 6c 69 74  (&pCursor, sqlit
36cae 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72  e3_value_blob(ar
36caf 67 76 5b 30 5d 29 2c 20 73 69 7a 65 6f 66 28 70  gv[0]), sizeof(p
36cb0 43 75 72 73 6f 72 29 29 3b 0a 20 20 20 20 76 20  Cursor));.    v 
36cb1 3d 20 63 75 72 73 6f 72 5f 76 74 61 62 28 70 43  = cursor_vtab(pC
36cb2 75 72 73 6f 72 29 3b 0a 0a 20 20 20 20 64 61 74  ursor);..    dat
36cb3 61 42 75 66 66 65 72 49 6e 69 74 28 26 64 6f 63  aBufferInit(&doc
36cb4 6c 69 73 74 2c 20 30 29 3b 0a 0a 20 20 20 20 2f  list, 0);..    /
36cb5 2a 20 74 65 72 6d 53 65 6c 65 63 74 28 29 20 79  * termSelect() y
36cb6 69 65 6c 64 73 20 74 68 65 20 73 61 6d 65 20 6c  ields the same l
36cb7 6f 67 69 63 61 6c 20 64 6f 63 6c 69 73 74 20 74  ogical doclist t
36cb8 68 61 74 20 71 75 65 72 69 65 73 20 61 72 65 0a  hat queries are.
36cb9 20 20 20 20 2a 2a 20 72 75 6e 20 61 67 61 69 6e      ** run again
36cba 73 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  st..    */.    i
36cbb 66 28 20 61 72 67 63 3d 3d 32 20 29 7b 0a 20 20  f( argc==2 ){.  
36cbc 20 20 20 20 72 63 20 3d 20 74 65 72 6d 53 65 6c      rc = termSel
36cbd 65 63 74 28 76 2c 20 76 2d 3e 6e 43 6f 6c 75 6d  ect(v, v->nColum
36cbe 6e 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c  n, pTerm, nTerm,
36cbf 20 30 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20   0, DL_DEFAULT, 
36cc0 26 64 6f 63 6c 69 73 74 29 3b 0a 20 20 20 20 7d  &doclist);.    }
36cc1 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
36cc2 74 65 33 5f 73 74 6d 74 20 2a 73 20 3d 20 4e 55  te3_stmt *s = NU
36cc3 4c 4c 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 47 65  LL;..      /* Ge
36cc4 74 20 6f 75 72 20 73 70 65 63 69 66 69 63 20 73  t our specific s
36cc5 65 67 6d 65 6e 74 27 73 20 69 6e 66 6f 72 6d 61  egment's informa
36cc6 74 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 72  tion. */.      r
36cc7 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74  c = sql_get_stat
36cc8 65 6d 65 6e 74 28 76 2c 20 53 45 47 44 49 52 5f  ement(v, SEGDIR_
36cc9 53 45 4c 45 43 54 5f 53 45 47 4d 45 4e 54 5f 53  SELECT_SEGMENT_S
36cca 54 4d 54 2c 20 26 73 29 3b 0a 20 20 20 20 20 20  TMT, &s);.      
36ccb 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
36ccc 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  K ){.        rc 
36ccd 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69  = sqlite3_bind_i
36cce 6e 74 28 73 2c 20 31 2c 20 73 71 6c 69 74 65 33  nt(s, 1, sqlite3
36ccf 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b  _value_int(argv[
36cd0 32 5d 29 29 3b 0a 20 20 20 20 20 20 20 20 69 66  2]));.        if
36cd1 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
36cd2 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  ){.          rc 
36cd3 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69  = sqlite3_bind_i
36cd4 6e 74 28 73 2c 20 32 2c 20 73 71 6c 69 74 65 33  nt(s, 2, sqlite3
36cd5 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b  _value_int(argv[
36cd6 33 5d 29 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  3]));.        }.
36cd7 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69        }..      i
36cd8 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
36cd9 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
36cda 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29   sqlite3_step(s)
36cdb 3b 0a 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  ;..        if( r
36cdc 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29  c==SQLITE_DONE )
36cdd 7b 0a 20 20 20 20 20 20 20 20 20 20 64 61 74 61  {.          data
36cde 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 64  BufferDestroy(&d
36cdf 6f 63 6c 69 73 74 29 3b 0a 20 20 20 20 20 20 20  oclist);.       
36ce0 20 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f 72     generateError
36ce1 28 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d 70  (pContext, "dump
36ce2 5f 64 6f 63 6c 69 73 74 22 2c 20 22 73 65 67 6d  _doclist", "segm
36ce3 65 6e 74 20 6e 6f 74 20 66 6f 75 6e 64 22 29 3b  ent not found");
36ce4 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
36ce5 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20  n;.        }..  
36ce6 20 20 20 20 20 20 2f 2a 20 46 6f 75 6e 64 20 61        /* Found a
36ce7 20 73 65 67 6d 65 6e 74 2c 20 6c 6f 61 64 20 69   segment, load i
36ce8 74 20 69 6e 74 6f 20 64 6f 63 6c 69 73 74 2e 20  t into doclist. 
36ce9 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  */.        if( r
36cea 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b  c==SQLITE_ROW ){
36ceb 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74  .          const
36cec 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4c   sqlite_int64 iL
36ced 65 61 76 65 73 45 6e 64 20 3d 20 73 71 6c 69 74  eavesEnd = sqlit
36cee 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28  e3_column_int64(
36cef 73 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 20  s, 1);.         
36cf0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61   const char *pDa
36cf1 74 61 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  ta = sqlite3_col
36cf2 75 6d 6e 5f 62 6c 6f 62 28 73 2c 20 32 29 3b 0a  umn_blob(s, 2);.
36cf3 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
36cf4 69 6e 74 20 6e 44 61 74 61 20 3d 20 73 71 6c 69  int nData = sqli
36cf5 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  te3_column_bytes
36cf6 28 73 2c 20 32 29 3b 0a 0a 20 20 20 20 20 20 20  (s, 2);..       
36cf7 20 20 20 2f 2a 20 6c 6f 61 64 53 65 67 6d 65 6e     /* loadSegmen
36cf8 74 28 29 20 69 73 20 75 73 65 64 20 62 79 20 74  t() is used by t
36cf9 65 72 6d 53 65 6c 65 63 74 28 29 20 74 6f 20 6c  ermSelect() to l
36cfa 6f 61 64 20 65 61 63 68 0a 20 20 20 20 20 20 20  oad each.       
36cfb 20 20 20 2a 2a 20 73 65 67 6d 65 6e 74 27 73 20     ** segment's 
36cfc 64 61 74 61 2e 0a 20 20 20 20 20 20 20 20 20 20  data..          
36cfd 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  */.          rc 
36cfe 3d 20 6c 6f 61 64 53 65 67 6d 65 6e 74 28 76 2c  = loadSegment(v,
36cff 20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 69   pData, nData, i
36d00 4c 65 61 76 65 73 45 6e 64 2c 20 70 54 65 72 6d  LeavesEnd, pTerm
36d01 2c 20 6e 54 65 72 6d 2c 20 30 2c 0a 20 20 20 20  , nTerm, 0,.    
36d02 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36d03 20 20 20 20 20 20 20 26 64 6f 63 6c 69 73 74 29         &doclist)
36d04 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
36d05 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
36d06 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20  .            rc 
36d07 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73  = sqlite3_step(s
36d08 29 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 20 20  );..            
36d09 2f 2a 20 53 68 6f 75 6c 64 20 6e 6f 74 20 68 61  /* Should not ha
36d0a 76 65 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65  ve more than one
36d0b 20 6d 61 74 63 68 69 6e 67 20 73 65 67 6d 65 6e   matching segmen
36d0c 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  t. */.          
36d0d 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
36d0e 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 20  _DONE ){.       
36d0f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72         sqlite3_r
36d10 65 73 65 74 28 73 29 3b 0a 20 20 20 20 20 20 20  eset(s);.       
36d11 20 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65         dataBuffe
36d12 72 44 65 73 74 72 6f 79 28 26 64 6f 63 6c 69 73  rDestroy(&doclis
36d13 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  t);.            
36d14 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f 72 28    generateError(
36d15 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f  pContext, "dump_
36d16 64 6f 63 6c 69 73 74 22 2c 20 22 69 6e 76 61 6c  doclist", "inval
36d17 69 64 20 73 65 67 64 69 72 22 29 3b 0a 20 20 20  id segdir");.   
36d18 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72             retur
36d19 6e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  n;.            }
36d1a 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20  .            rc 
36d1b 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
36d1c 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
36d1d 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20   }.      }..    
36d1e 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28    sqlite3_reset(
36d1f 73 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69  s);.    }..    i
36d20 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
36d21 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 64 6f   ){.      if( do
36d22 63 6c 69 73 74 2e 6e 44 61 74 61 3e 30 20 29 7b  clist.nData>0 ){
36d23 0a 20 20 20 20 20 20 20 20 63 72 65 61 74 65 44  .        createD
36d24 6f 63 6c 69 73 74 52 65 73 75 6c 74 28 70 43 6f  oclistResult(pCo
36d25 6e 74 65 78 74 2c 20 64 6f 63 6c 69 73 74 2e 70  ntext, doclist.p
36d26 44 61 74 61 2c 20 64 6f 63 6c 69 73 74 2e 6e 44  Data, doclist.nD
36d27 61 74 61 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  ata);.      }els
36d28 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 4f  e{.        /* TO
36d29 44 4f 28 73 68 65 73 73 29 3a 20 54 68 69 73 20  DO(shess): This 
36d2a 63 61 6e 20 68 61 70 70 65 6e 20 69 66 20 74 68  can happen if th
36d2b 65 20 74 65 72 6d 20 69 73 20 6e 6f 74 20 70 72  e term is not pr
36d2c 65 73 65 6e 74 2c 20 6f 72 0a 20 20 20 20 20 20  esent, or.      
36d2d 20 20 2a 2a 20 69 66 20 61 6c 6c 20 69 6e 73 74    ** if all inst
36d2e 61 6e 63 65 73 20 6f 66 20 74 68 65 20 74 65 72  ances of the ter
36d2f 6d 20 68 61 76 65 20 62 65 65 6e 20 64 65 6c 65  m have been dele
36d30 74 65 64 20 61 6e 64 20 74 68 69 73 20 69 73 0a  ted and this is.
36d31 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 20 61 6c          ** an al
36d32 6c 2d 69 6e 64 65 78 20 64 75 6d 70 2e 20 20 49  l-index dump.  I
36d33 74 20 6d 61 79 20 62 65 20 69 6e 74 65 72 65 73  t may be interes
36d34 74 69 6e 67 20 74 6f 20 64 69 73 74 69 6e 67 75  ting to distingu
36d35 69 73 68 0a 20 20 20 20 20 20 20 20 2a 2a 20 74  ish.        ** t
36d36 68 65 73 65 20 63 61 73 65 73 2e 0a 20 20 20 20  hese cases..    
36d37 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 73      */.        s
36d38 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
36d39 78 74 28 70 43 6f 6e 74 65 78 74 2c 20 22 22 2c  xt(pContext, "",
36d3a 20 30 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49   0, SQLITE_STATI
36d3b 43 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  C);.      }.    
36d3c 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51  }else if( rc==SQ
36d3d 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20  LITE_NOMEM ){.  
36d3e 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 6f 75      /* Handle ou
36d3f 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 61 73 65  t-of-memory case
36d40 73 20 73 70 65 63 69 61 6c 6c 79 20 62 65 63 61  s specially beca
36d41 75 73 65 20 69 66 20 74 68 65 79 20 61 72 65 0a  use if they are.
36d42 20 20 20 20 20 20 2a 2a 20 67 65 6e 65 72 61 74        ** generat
36d43 65 64 20 69 6e 20 66 74 73 33 20 63 6f 64 65 20  ed in fts3 code 
36d44 74 68 65 79 20 6d 61 79 20 6e 6f 74 20 62 65 20  they may not be 
36d45 72 65 66 6c 65 63 74 65 64 20 69 6e 20 74 68 65  reflected in the
36d46 20 64 62 0a 20 20 20 20 20 20 2a 2a 20 68 61 6e   db.      ** han
36d47 64 6c 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  dle..      */.  
36d48 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73      /* TODO(shes
36d49 73 29 3a 20 48 61 6e 64 6c 65 20 74 68 69 73 20  s): Handle this 
36d4a 6d 6f 72 65 20 63 6f 6d 70 72 65 68 65 6e 73 69  more comprehensi
36d4b 76 65 6c 79 2e 0a 20 20 20 20 20 20 2a 2a 20 73  vely..      ** s
36d4c 71 6c 69 74 65 33 45 72 72 53 74 72 28 29 20 68  qlite3ErrStr() h
36d4d 61 73 20 77 68 61 74 20 49 20 6e 65 65 64 2c 20  as what I need, 
36d4e 62 75 74 20 69 73 20 69 6e 74 65 72 6e 61 6c 2e  but is internal.
36d4f 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
36d50 67 65 6e 65 72 61 74 65 45 72 72 6f 72 28 70 43  generateError(pC
36d51 6f 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f 64 6f  ontext, "dump_do
36d52 63 6c 69 73 74 22 2c 20 22 6f 75 74 20 6f 66 20  clist", "out of 
36d53 6d 65 6d 6f 72 79 22 29 3b 0a 20 20 20 20 7d 65  memory");.    }e
36d54 6c 73 65 7b 0a 20 20 20 20 20 20 67 65 6e 65 72  lse{.      gener
36d55 61 74 65 45 72 72 6f 72 28 70 43 6f 6e 74 65 78  ateError(pContex
36d56 74 2c 20 22 64 75 6d 70 5f 64 6f 63 6c 69 73 74  t, "dump_doclist
36d57 22 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 7d 0a  ", NULL);.    }.
36d58 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72 44  .    dataBufferD
36d59 65 73 74 72 6f 79 28 26 64 6f 63 6c 69 73 74 29  estroy(&doclist)
36d5a 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a  ;.  }.}.#endif..
36d5b 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
36d5c 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68  ne implements th
36d5d 65 20 78 46 69 6e 64 46 75 6e 63 74 69 6f 6e 20  e xFindFunction 
36d5e 6d 65 74 68 6f 64 20 66 6f 72 20 74 68 65 20 46  method for the F
36d5f 54 53 33 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74  TS3.** virtual t
36d60 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  able..*/.static 
36d61 69 6e 74 20 66 75 6c 6c 74 65 78 74 46 69 6e 64  int fulltextFind
36d62 46 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69  Function(.  sqli
36d63 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 2c  te3_vtab *pVtab,
36d64 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20 63  .  int nArg,.  c
36d65 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
36d66 2c 0a 20 20 76 6f 69 64 20 28 2a 2a 70 78 46 75  ,.  void (**pxFu
36d67 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  nc)(sqlite3_cont
36d68 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
36d69 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69  _value**),.  voi
36d6a 64 20 2a 2a 70 70 41 72 67 0a 29 7b 0a 20 20 69  d **ppArg.){.  i
36d6b 66 28 20 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c  f( strcmp(zName,
36d6c 22 73 6e 69 70 70 65 74 22 29 3d 3d 30 20 29 7b  "snippet")==0 ){
36d6d 0a 20 20 20 20 2a 70 78 46 75 6e 63 20 3d 20 73  .    *pxFunc = s
36d6e 6e 69 70 70 65 74 46 75 6e 63 3b 0a 20 20 20 20  nippetFunc;.    
36d6f 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73  return 1;.  }els
36d70 65 20 69 66 28 20 73 74 72 63 6d 70 28 7a 4e 61  e if( strcmp(zNa
36d71 6d 65 2c 22 6f 66 66 73 65 74 73 22 29 3d 3d 30  me,"offsets")==0
36d72 20 29 7b 0a 20 20 20 20 2a 70 78 46 75 6e 63 20   ){.    *pxFunc 
36d73 3d 20 73 6e 69 70 70 65 74 4f 66 66 73 65 74 73  = snippetOffsets
36d74 46 75 6e 63 3b 0a 20 20 20 20 72 65 74 75 72 6e  Func;.    return
36d75 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   1;.  }else if( 
36d76 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 22 6f 70  strcmp(zName,"op
36d77 74 69 6d 69 7a 65 22 29 3d 3d 30 20 29 7b 0a 20  timize")==0 ){. 
36d78 20 20 20 2a 70 78 46 75 6e 63 20 3d 20 6f 70 74     *pxFunc = opt
36d79 69 6d 69 7a 65 46 75 6e 63 3b 0a 20 20 20 20 72  imizeFunc;.    r
36d7a 65 74 75 72 6e 20 31 3b 0a 23 69 66 64 65 66 20  eturn 1;.#ifdef 
36d7b 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20  SQLITE_TEST.    
36d7c 2f 2a 20 4e 4f 54 45 28 73 68 65 73 73 29 3a 20  /* NOTE(shess): 
36d7d 54 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20  These functions 
36d7e 61 72 65 20 70 72 65 73 65 6e 74 20 6f 6e 6c 79  are present only
36d7f 20 66 6f 72 20 74 65 73 74 69 6e 67 0a 20 20 20   for testing.   
36d80 20 2a 2a 20 70 75 72 70 6f 73 65 73 2e 20 20 4e   ** purposes.  N
36d81 6f 20 70 61 72 74 69 63 75 6c 61 72 20 65 66 66  o particular eff
36d82 6f 72 74 20 69 73 20 6d 61 64 65 20 74 6f 20 6f  ort is made to o
36d83 70 74 69 6d 69 7a 65 20 74 68 65 69 72 0a 20 20  ptimize their.  
36d84 20 20 2a 2a 20 65 78 65 63 75 74 69 6f 6e 20 6f    ** execution o
36d85 72 20 68 6f 77 20 74 68 65 79 20 62 75 69 6c 64  r how they build
36d86 20 74 68 65 69 72 20 72 65 73 75 6c 74 73 2e 0a   their results..
36d87 20 20 20 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69      */.  }else i
36d88 66 28 20 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c  f( strcmp(zName,
36d89 22 64 75 6d 70 5f 74 65 72 6d 73 22 29 3d 3d 30  "dump_terms")==0
36d8a 20 29 7b 0a 20 20 20 20 2f 2a 20 66 70 72 69 6e   ){.    /* fprin
36d8b 74 66 28 73 74 64 65 72 72 2c 20 22 46 6f 75 6e  tf(stderr, "Foun
36d8c 64 20 64 75 6d 70 5f 74 65 72 6d 73 5c 6e 22 29  d dump_terms\n")
36d8d 3b 20 2a 2f 0a 20 20 20 20 2a 70 78 46 75 6e 63  ; */.    *pxFunc
36d8e 20 3d 20 64 75 6d 70 54 65 72 6d 73 46 75 6e 63   = dumpTermsFunc
36d8f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  ;.    return 1;.
36d90 20 20 7d 65 6c 73 65 20 69 66 28 20 73 74 72 63    }else if( strc
36d91 6d 70 28 7a 4e 61 6d 65 2c 22 64 75 6d 70 5f 64  mp(zName,"dump_d
36d92 6f 63 6c 69 73 74 22 29 3d 3d 30 20 29 7b 0a 20  oclist")==0 ){. 
36d93 20 20 20 2f 2a 20 66 70 72 69 6e 74 66 28 73 74     /* fprintf(st
36d94 64 65 72 72 2c 20 22 46 6f 75 6e 64 20 64 75 6d  derr, "Found dum
36d95 70 5f 64 6f 63 6c 69 73 74 5c 6e 22 29 3b 20 2a  p_doclist\n"); *
36d96 2f 0a 20 20 20 20 2a 70 78 46 75 6e 63 20 3d 20  /.    *pxFunc = 
36d97 64 75 6d 70 44 6f 63 6c 69 73 74 46 75 6e 63 3b  dumpDoclistFunc;
36d98 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 23  .    return 1;.#
36d99 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72 65 74 75  endif.  }.  retu
36d9a 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  rn 0;.}../*.** R
36d9b 65 6e 61 6d 65 20 61 6e 20 66 74 73 33 20 74 61  ename an fts3 ta
36d9c 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ble..*/.static i
36d9d 6e 74 20 66 75 6c 6c 74 65 78 74 52 65 6e 61 6d  nt fulltextRenam
36d9e 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61  e(.  sqlite3_vta
36d9f 62 20 2a 70 56 74 61 62 2c 0a 20 20 63 6f 6e 73  b *pVtab,.  cons
36da0 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 0a 29 7b  t char *zName.){
36da1 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  .  fulltext_vtab
36da2 20 2a 70 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f   *p = (fulltext_
36da3 76 74 61 62 20 2a 29 70 56 74 61 62 3b 0a 20 20  vtab *)pVtab;.  
36da4 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
36da5 4e 4f 4d 45 4d 3b 0a 20 20 63 68 61 72 20 2a 7a  NOMEM;.  char *z
36da6 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  Sql = sqlite3_mp
36da7 72 69 6e 74 66 28 0a 20 20 20 20 22 41 4c 54 45  rintf(.    "ALTE
36da8 52 20 54 41 42 4c 45 20 25 51 2e 27 25 71 5f 63  R TABLE %Q.'%q_c
36da9 6f 6e 74 65 6e 74 27 20 20 52 45 4e 41 4d 45 20  ontent'  RENAME 
36daa 54 4f 20 27 25 71 5f 63 6f 6e 74 65 6e 74 27 3b  TO '%q_content';
36dab 22 0a 20 20 20 20 22 41 4c 54 45 52 20 54 41 42  ".    "ALTER TAB
36dac 4c 45 20 25 51 2e 27 25 71 5f 73 65 67 6d 65 6e  LE %Q.'%q_segmen
36dad 74 73 27 20 52 45 4e 41 4d 45 20 54 4f 20 27 25  ts' RENAME TO '%
36dae 71 5f 73 65 67 6d 65 6e 74 73 27 3b 22 0a 20 20  q_segments';".  
36daf 20 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 25    "ALTER TABLE %
36db0 51 2e 27 25 71 5f 73 65 67 64 69 72 27 20 20 20  Q.'%q_segdir'   
36db1 52 45 4e 41 4d 45 20 54 4f 20 27 25 71 5f 73 65  RENAME TO '%q_se
36db2 67 64 69 72 27 3b 22 0a 20 20 20 20 2c 20 70 2d  gdir';".    , p-
36db3 3e 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 20  >zDb, p->zName, 
36db4 7a 4e 61 6d 65 20 0a 20 20 20 20 2c 20 70 2d 3e  zName .    , p->
36db5 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 20 7a  zDb, p->zName, z
36db6 4e 61 6d 65 20 0a 20 20 20 20 2c 20 70 2d 3e 7a  Name .    , p->z
36db7 44 62 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e  Db, p->zName, zN
36db8 61 6d 65 0a 20 20 29 3b 0a 20 20 69 66 28 20 7a  ame.  );.  if( z
36db9 53 71 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  Sql ){.    rc = 
36dba 73 71 6c 69 74 65 33 5f 65 78 65 63 28 70 2d 3e  sqlite3_exec(p->
36dbb 64 62 2c 20 7a 53 71 6c 2c 20 30 2c 20 30 2c 20  db, zSql, 0, 0, 
36dbc 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
36dbd 66 72 65 65 28 7a 53 71 6c 29 3b 0a 20 20 7d 0a  free(zSql);.  }.
36dbe 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
36dbf 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c  static const sql
36dc0 69 74 65 33 5f 6d 6f 64 75 6c 65 20 66 74 73 33  ite3_module fts3
36dc1 4d 6f 64 75 6c 65 20 3d 20 7b 0a 20 20 2f 2a 20  Module = {.  /* 
36dc2 69 56 65 72 73 69 6f 6e 20 20 20 20 20 20 2a 2f  iVersion      */
36dc3 20 30 2c 0a 20 20 2f 2a 20 78 43 72 65 61 74 65   0,.  /* xCreate
36dc4 20 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65         */ fullte
36dc5 78 74 43 72 65 61 74 65 2c 0a 20 20 2f 2a 20 78  xtCreate,.  /* x
36dc6 43 6f 6e 6e 65 63 74 20 20 20 20 20 20 2a 2f 20  Connect      */ 
36dc7 66 75 6c 6c 74 65 78 74 43 6f 6e 6e 65 63 74 2c  fulltextConnect,
36dc8 0a 20 20 2f 2a 20 78 42 65 73 74 49 6e 64 65 78  .  /* xBestIndex
36dc9 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 42      */ fulltextB
36dca 65 73 74 49 6e 64 65 78 2c 0a 20 20 2f 2a 20 78  estIndex,.  /* x
36dcb 44 69 73 63 6f 6e 6e 65 63 74 20 20 20 2a 2f 20  Disconnect   */ 
36dcc 66 75 6c 6c 74 65 78 74 44 69 73 63 6f 6e 6e 65  fulltextDisconne
36dcd 63 74 2c 0a 20 20 2f 2a 20 78 44 65 73 74 72 6f  ct,.  /* xDestro
36dce 79 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65  y      */ fullte
36dcf 78 74 44 65 73 74 72 6f 79 2c 0a 20 20 2f 2a 20  xtDestroy,.  /* 
36dd0 78 4f 70 65 6e 20 20 20 20 20 20 20 20 20 2a 2f  xOpen         */
36dd1 20 66 75 6c 6c 74 65 78 74 4f 70 65 6e 2c 0a 20   fulltextOpen,. 
36dd2 20 2f 2a 20 78 43 6c 6f 73 65 20 20 20 20 20 20   /* xClose      
36dd3 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 43 6c 6f    */ fulltextClo
36dd4 73 65 2c 0a 20 20 2f 2a 20 78 46 69 6c 74 65 72  se,.  /* xFilter
36dd5 20 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65         */ fullte
36dd6 78 74 46 69 6c 74 65 72 2c 0a 20 20 2f 2a 20 78  xtFilter,.  /* x
36dd7 4e 65 78 74 20 20 20 20 20 20 20 20 20 2a 2f 20  Next         */ 
36dd8 66 75 6c 6c 74 65 78 74 4e 65 78 74 2c 0a 20 20  fulltextNext,.  
36dd9 2f 2a 20 78 45 6f 66 20 20 20 20 20 20 20 20 20  /* xEof         
36dda 20 2a 2f 20 66 75 6c 6c 74 65 78 74 45 6f 66 2c   */ fulltextEof,
36ddb 0a 20 20 2f 2a 20 78 43 6f 6c 75 6d 6e 20 20 20  .  /* xColumn   
36ddc 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 43      */ fulltextC
36ddd 6f 6c 75 6d 6e 2c 0a 20 20 2f 2a 20 78 52 6f 77  olumn,.  /* xRow
36dde 69 64 20 20 20 20 20 20 20 20 2a 2f 20 66 75 6c  id        */ ful
36ddf 6c 74 65 78 74 52 6f 77 69 64 2c 0a 20 20 2f 2a  ltextRowid,.  /*
36de0 20 78 55 70 64 61 74 65 20 20 20 20 20 20 20 2a   xUpdate       *
36de1 2f 20 66 75 6c 6c 74 65 78 74 55 70 64 61 74 65  / fulltextUpdate
36de2 2c 0a 20 20 2f 2a 20 78 42 65 67 69 6e 20 20 20  ,.  /* xBegin   
36de3 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74       */ fulltext
36de4 42 65 67 69 6e 2c 0a 20 20 2f 2a 20 78 53 79 6e  Begin,.  /* xSyn
36de5 63 20 20 20 20 20 20 20 20 20 2a 2f 20 66 75 6c  c         */ ful
36de6 6c 74 65 78 74 53 79 6e 63 2c 0a 20 20 2f 2a 20  ltextSync,.  /* 
36de7 78 43 6f 6d 6d 69 74 20 20 20 20 20 20 20 2a 2f  xCommit       */
36de8 20 66 75 6c 6c 74 65 78 74 43 6f 6d 6d 69 74 2c   fulltextCommit,
36de9 0a 20 20 2f 2a 20 78 52 6f 6c 6c 62 61 63 6b 20  .  /* xRollback 
36dea 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 52      */ fulltextR
36deb 6f 6c 6c 62 61 63 6b 2c 0a 20 20 2f 2a 20 78 46  ollback,.  /* xF
36dec 69 6e 64 46 75 6e 63 74 69 6f 6e 20 2a 2f 20 66  indFunction */ f
36ded 75 6c 6c 74 65 78 74 46 69 6e 64 46 75 6e 63 74  ulltextFindFunct
36dee 69 6f 6e 2c 0a 20 20 2f 2a 20 78 52 65 6e 61 6d  ion,.  /* xRenam
36def 65 20 2a 2f 20 20 20 20 20 20 20 66 75 6c 6c 74  e */       fullt
36df0 65 78 74 52 65 6e 61 6d 65 2c 0a 7d 3b 0a 0a 73  extRename,.};..s
36df1 74 61 74 69 63 20 76 6f 69 64 20 68 61 73 68 44  tatic void hashD
36df2 65 73 74 72 6f 79 28 76 6f 69 64 20 2a 70 29 7b  estroy(void *p){
36df3 0a 20 20 66 74 73 33 48 61 73 68 20 2a 70 48 61  .  fts3Hash *pHa
36df4 73 68 20 3d 20 28 66 74 73 33 48 61 73 68 20 2a  sh = (fts3Hash *
36df5 29 70 3b 0a 20 20 73 71 6c 69 74 65 33 46 74 73  )p;.  sqlite3Fts
36df6 33 48 61 73 68 43 6c 65 61 72 28 70 48 61 73 68  3HashClear(pHash
36df7 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
36df8 65 28 70 48 61 73 68 29 3b 0a 7d 0a 0a 2f 2a 0a  e(pHash);.}../*.
36df9 2a 2a 20 54 68 65 20 66 74 73 33 20 62 75 69 6c  ** The fts3 buil
36dfa 74 2d 69 6e 20 74 6f 6b 65 6e 69 7a 65 72 73 20  t-in tokenizers 
36dfb 2d 20 22 73 69 6d 70 6c 65 22 20 61 6e 64 20 22  - "simple" and "
36dfc 70 6f 72 74 65 72 22 20 2d 20 61 72 65 20 69 6d  porter" - are im
36dfd 70 6c 65 6d 65 6e 74 65 64 0a 2a 2a 20 69 6e 20  plemented.** in 
36dfe 66 69 6c 65 73 20 66 74 73 33 5f 74 6f 6b 65 6e  files fts3_token
36dff 69 7a 65 72 31 2e 63 20 61 6e 64 20 66 74 73 33  izer1.c and fts3
36e00 5f 70 6f 72 74 65 72 2e 63 20 72 65 73 70 65 63  _porter.c respec
36e01 74 69 76 65 6c 79 2e 20 54 68 65 20 66 6f 6c 6c  tively. The foll
36e02 6f 77 69 6e 67 0a 2a 2a 20 74 77 6f 20 66 6f 72  owing.** two for
36e03 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e  ward declaration
36e04 73 20 61 72 65 20 66 6f 72 20 66 75 6e 63 74 69  s are for functi
36e05 6f 6e 73 20 64 65 63 6c 61 72 65 64 20 69 6e 20  ons declared in 
36e06 74 68 65 73 65 20 66 69 6c 65 73 0a 2a 2a 20 75  these files.** u
36e07 73 65 64 20 74 6f 20 72 65 74 72 69 65 76 65 20  sed to retrieve 
36e08 74 68 65 20 72 65 73 70 65 63 74 69 76 65 20 69  the respective i
36e09 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a  mplementations..
36e0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 73 71  **.** Calling sq
36e0b 6c 69 74 65 33 46 74 73 33 53 69 6d 70 6c 65 54  lite3Fts3SimpleT
36e0c 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 29  okenizerModule()
36e0d 20 73 65 74 73 20 74 68 65 20 76 61 6c 75 65 20   sets the value 
36e0e 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62 79  pointed.** to by
36e0f 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74 6f   the argument to
36e10 20 70 6f 69 6e 74 20 61 20 74 68 65 20 22 73 69   point a the "si
36e11 6d 70 6c 65 22 20 74 6f 6b 65 6e 69 7a 65 72 20  mple" tokenizer 
36e12 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 0a  implementation..
36e13 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 2e 2e 2e 50  ** Function ...P
36e14 6f 72 74 65 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f  orterTokenizerMo
36e15 64 75 6c 65 28 29 20 73 65 74 73 20 2a 70 4d 6f  dule() sets *pMo
36e16 64 75 6c 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f  dule to point to
36e17 20 74 68 65 0a 2a 2a 20 70 6f 72 74 65 72 20 74   the.** porter t
36e18 6f 6b 65 6e 69 7a 65 72 2f 73 74 65 6d 6d 65 72  okenizer/stemmer
36e19 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e   implementation.
36e1a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
36e1b 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46  TE void sqlite3F
36e1c 74 73 33 53 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a  ts3SimpleTokeniz
36e1d 65 72 4d 6f 64 75 6c 65 28 73 71 6c 69 74 65 33  erModule(sqlite3
36e1e 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c  _tokenizer_modul
36e1f 65 20 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c  e const**ppModul
36e20 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  e);.SQLITE_PRIVA
36e21 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46  TE void sqlite3F
36e22 74 73 33 50 6f 72 74 65 72 54 6f 6b 65 6e 69 7a  ts3PorterTokeniz
36e23 65 72 4d 6f 64 75 6c 65 28 73 71 6c 69 74 65 33  erModule(sqlite3
36e24 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c  _tokenizer_modul
36e25 65 20 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c  e const**ppModul
36e26 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  e);.SQLITE_PRIVA
36e27 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46  TE void sqlite3F
36e28 74 73 33 49 63 75 54 6f 6b 65 6e 69 7a 65 72 4d  ts3IcuTokenizerM
36e29 6f 64 75 6c 65 28 73 71 6c 69 74 65 33 5f 74 6f  odule(sqlite3_to
36e2a 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63  kenizer_module c
36e2b 6f 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 29 3b  onst**ppModule);
36e2c 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
36e2d 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33   int sqlite3Fts3
36e2e 49 6e 69 74 48 61 73 68 54 61 62 6c 65 28 73 71  InitHashTable(sq
36e2f 6c 69 74 65 33 20 2a 2c 20 66 74 73 33 48 61 73  lite3 *, fts3Has
36e30 68 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  h *, const char 
36e31 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69  *);../*.** Initi
36e32 61 6c 69 73 65 20 74 68 65 20 66 74 73 33 20 65  alise the fts3 e
36e33 78 74 65 6e 73 69 6f 6e 2e 20 49 66 20 74 68 69  xtension. If thi
36e34 73 20 65 78 74 65 6e 73 69 6f 6e 20 69 73 20 62  s extension is b
36e35 75 69 6c 74 20 61 73 20 70 61 72 74 0a 2a 2a 20  uilt as part.** 
36e36 6f 66 20 74 68 65 20 73 71 6c 69 74 65 20 6c 69  of the sqlite li
36e37 62 72 61 72 79 2c 20 74 68 65 6e 20 74 68 69 73  brary, then this
36e38 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
36e39 6c 65 64 20 64 69 72 65 63 74 6c 79 20 62 79 0a  led directly by.
36e3a 2a 2a 20 53 51 4c 69 74 65 2e 20 49 66 20 66 74  ** SQLite. If ft
36e3b 73 33 20 69 73 20 62 75 69 6c 74 20 61 73 20 61  s3 is built as a
36e3c 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 6c 6f 61   dynamically loa
36e3d 64 61 62 6c 65 20 65 78 74 65 6e 73 69 6f 6e 2c  dable extension,
36e3e 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f   this.** functio
36e3f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74  n is called by t
36e40 68 65 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e  he sqlite3_exten
36e41 73 69 6f 6e 5f 69 6e 69 74 28 29 20 65 6e 74 72  sion_init() entr
36e42 79 20 70 6f 69 6e 74 2e 0a 2a 2f 0a 53 51 4c 49  y point..*/.SQLI
36e43 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
36e44 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74 28 73  qlite3Fts3Init(s
36e45 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69  qlite3 *db){.  i
36e46 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
36e47 4b 3b 0a 20 20 66 74 73 33 48 61 73 68 20 2a 70  K;.  fts3Hash *p
36e48 48 61 73 68 20 3d 20 30 3b 0a 20 20 63 6f 6e 73  Hash = 0;.  cons
36e49 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  t sqlite3_tokeni
36e4a 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 53 69 6d  zer_module *pSim
36e4b 70 6c 65 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74  ple = 0;.  const
36e4c 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
36e4d 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 50 6f 72 74  er_module *pPort
36e4e 65 72 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20  er = 0;.  const 
36e4f 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
36e50 72 5f 6d 6f 64 75 6c 65 20 2a 70 49 63 75 20 3d  r_module *pIcu =
36e51 20 30 3b 0a 0a 20 20 73 71 6c 69 74 65 33 46 74   0;..  sqlite3Ft
36e52 73 33 53 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65  s3SimpleTokenize
36e53 72 4d 6f 64 75 6c 65 28 26 70 53 69 6d 70 6c 65  rModule(&pSimple
36e54 29 3b 0a 20 20 73 71 6c 69 74 65 33 46 74 73 33  );.  sqlite3Fts3
36e55 50 6f 72 74 65 72 54 6f 6b 65 6e 69 7a 65 72 4d  PorterTokenizerM
36e56 6f 64 75 6c 65 28 26 70 50 6f 72 74 65 72 29 3b  odule(&pPorter);
36e57 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
36e58 4e 41 42 4c 45 5f 49 43 55 0a 20 20 73 71 6c 69  NABLE_ICU.  sqli
36e59 74 65 33 46 74 73 33 49 63 75 54 6f 6b 65 6e 69  te3Fts3IcuTokeni
36e5a 7a 65 72 4d 6f 64 75 6c 65 28 26 70 49 63 75 29  zerModule(&pIcu)
36e5b 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41  ;.#endif..  /* A
36e5c 6c 6c 6f 63 61 74 65 20 61 6e 64 20 69 6e 69 74  llocate and init
36e5d 69 61 6c 69 73 65 20 74 68 65 20 68 61 73 68 2d  ialise the hash-
36e5e 74 61 62 6c 65 20 75 73 65 64 20 74 6f 20 73 74  table used to st
36e5f 6f 72 65 20 74 6f 6b 65 6e 69 7a 65 72 73 2e 20  ore tokenizers. 
36e60 2a 2f 0a 20 20 70 48 61 73 68 20 3d 20 73 71 6c  */.  pHash = sql
36e61 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65  ite3_malloc(size
36e62 6f 66 28 66 74 73 33 48 61 73 68 29 29 3b 0a 20  of(fts3Hash));. 
36e63 20 69 66 28 20 21 70 48 61 73 68 20 29 7b 0a 20   if( !pHash ){. 
36e64 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
36e65 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  OMEM;.  }else{. 
36e66 20 20 20 73 71 6c 69 74 65 33 46 74 73 33 48 61     sqlite3Fts3Ha
36e67 73 68 49 6e 69 74 28 70 48 61 73 68 2c 20 46 54  shInit(pHash, FT
36e68 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 2c 20  S3_HASH_STRING, 
36e69 31 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f  1);.  }..  /* Lo
36e6a 61 64 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20  ad the built-in 
36e6b 74 6f 6b 65 6e 69 7a 65 72 73 20 69 6e 74 6f 20  tokenizers into 
36e6c 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 2a  the hash table *
36e6d 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  /.  if( rc==SQLI
36e6e 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28  TE_OK ){.    if(
36e6f 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68   sqlite3Fts3Hash
36e70 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 22 73  Insert(pHash, "s
36e71 69 6d 70 6c 65 22 2c 20 37 2c 20 28 76 6f 69 64  imple", 7, (void
36e72 20 2a 29 70 53 69 6d 70 6c 65 29 0a 20 20 20 20   *)pSimple).    
36e73 20 7c 7c 20 73 71 6c 69 74 65 33 46 74 73 33 48   || sqlite3Fts3H
36e74 61 73 68 49 6e 73 65 72 74 28 70 48 61 73 68 2c  ashInsert(pHash,
36e75 20 22 70 6f 72 74 65 72 22 2c 20 37 2c 20 28 76   "porter", 7, (v
36e76 6f 69 64 20 2a 29 70 50 6f 72 74 65 72 29 20 0a  oid *)pPorter) .
36e77 20 20 20 20 20 7c 7c 20 28 70 49 63 75 20 26 26       || (pIcu &&
36e78 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68   sqlite3Fts3Hash
36e79 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 22 69  Insert(pHash, "i
36e7a 63 75 22 2c 20 34 2c 20 28 76 6f 69 64 20 2a 29  cu", 4, (void *)
36e7b 70 49 63 75 29 29 0a 20 20 20 20 29 7b 0a 20 20  pIcu)).    ){.  
36e7c 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
36e7d 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d  NOMEM;.    }.  }
36e7e 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
36e7f 54 45 53 54 0a 20 20 73 71 6c 69 74 65 33 46 74  TEST.  sqlite3Ft
36e80 73 33 45 78 70 72 49 6e 69 74 54 65 73 74 49 6e  s3ExprInitTestIn
36e81 74 65 72 66 61 63 65 28 64 62 29 3b 0a 23 65 6e  terface(db);.#en
36e82 64 69 66 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65  dif..  /* Create
36e83 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
36e84 6c 65 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e  le wrapper aroun
36e85 64 20 74 68 65 20 68 61 73 68 2d 74 61 62 6c 65  d the hash-table
36e86 20 61 6e 64 20 6f 76 65 72 6c 6f 61 64 20 0a 20   and overload . 
36e87 20 2a 2a 20 74 68 65 20 74 77 6f 20 73 63 61 6c   ** the two scal
36e88 61 72 20 66 75 6e 63 74 69 6f 6e 73 2e 20 49 66  ar functions. If
36e89 20 74 68 69 73 20 69 73 20 73 75 63 63 65 73 73   this is success
36e8a 66 75 6c 2c 20 72 65 67 69 73 74 65 72 20 74 68  ful, register th
36e8b 65 0a 20 20 2a 2a 20 6d 6f 64 75 6c 65 20 77 69  e.  ** module wi
36e8c 74 68 20 73 71 6c 69 74 65 2e 0a 20 20 2a 2f 0a  th sqlite..  */.
36e8d 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d    if( SQLITE_OK=
36e8e 3d 72 63 20 0a 20 20 20 26 26 20 53 51 4c 49 54  =rc .   && SQLIT
36e8f 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69  E_OK==(rc = sqli
36e90 74 65 33 46 74 73 33 49 6e 69 74 48 61 73 68 54  te3Fts3InitHashT
36e91 61 62 6c 65 28 64 62 2c 20 70 48 61 73 68 2c 20  able(db, pHash, 
36e92 22 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 22  "fts3_tokenizer"
36e93 29 29 0a 20 20 20 26 26 20 53 51 4c 49 54 45 5f  )).   && SQLITE_
36e94 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65  OK==(rc = sqlite
36e95 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74  3_overload_funct
36e96 69 6f 6e 28 64 62 2c 20 22 73 6e 69 70 70 65 74  ion(db, "snippet
36e97 22 2c 20 2d 31 29 29 0a 20 20 20 26 26 20 53 51  ", -1)).   && SQ
36e98 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73  LITE_OK==(rc = s
36e99 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f  qlite3_overload_
36e9a 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22 6f 66  function(db, "of
36e9b 66 73 65 74 73 22 2c 20 2d 31 29 29 0a 20 20 20  fsets", -1)).   
36e9c 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72  && SQLITE_OK==(r
36e9d 63 20 3d 20 73 71 6c 69 74 65 33 5f 6f 76 65 72  c = sqlite3_over
36e9e 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 64 62  load_function(db
36e9f 2c 20 22 6f 70 74 69 6d 69 7a 65 22 2c 20 2d 31  , "optimize", -1
36ea0 29 29 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  )).#ifdef SQLITE
36ea1 5f 54 45 53 54 0a 20 20 20 26 26 20 53 51 4c 49  _TEST.   && SQLI
36ea2 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c  TE_OK==(rc = sql
36ea3 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75  ite3_overload_fu
36ea4 6e 63 74 69 6f 6e 28 64 62 2c 20 22 64 75 6d 70  nction(db, "dump
36ea5 5f 74 65 72 6d 73 22 2c 20 2d 31 29 29 0a 20 20  _terms", -1)).  
36ea6 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28   && SQLITE_OK==(
36ea7 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 6f 76 65  rc = sqlite3_ove
36ea8 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 64  rload_function(d
36ea9 62 2c 20 22 64 75 6d 70 5f 64 6f 63 6c 69 73 74  b, "dump_doclist
36eaa 22 2c 20 2d 31 29 29 0a 23 65 6e 64 69 66 0a 20  ", -1)).#endif. 
36eab 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73   ){.    return s
36eac 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f  qlite3_create_mo
36ead 64 75 6c 65 5f 76 32 28 0a 20 20 20 20 20 20 20  dule_v2(.       
36eae 20 64 62 2c 20 22 66 74 73 33 22 2c 20 26 66 74   db, "fts3", &ft
36eaf 73 33 4d 6f 64 75 6c 65 2c 20 28 76 6f 69 64 20  s3Module, (void 
36eb0 2a 29 70 48 61 73 68 2c 20 68 61 73 68 44 65 73  *)pHash, hashDes
36eb1 74 72 6f 79 0a 20 20 20 20 29 3b 0a 20 20 7d 0a  troy.    );.  }.
36eb2 0a 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20 68  .  /* An error h
36eb3 61 73 20 6f 63 63 75 72 72 65 64 2e 20 44 65 6c  as occurred. Del
36eb4 65 74 65 20 74 68 65 20 68 61 73 68 20 74 61 62  ete the hash tab
36eb5 6c 65 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68  le and return th
36eb6 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 2a 2f  e error code. */
36eb7 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53  .  assert( rc!=S
36eb8 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69 66  QLITE_OK );.  if
36eb9 28 20 70 48 61 73 68 20 29 7b 0a 20 20 20 20 73  ( pHash ){.    s
36eba 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 43 6c  qlite3Fts3HashCl
36ebb 65 61 72 28 70 48 61 73 68 29 3b 0a 20 20 20 20  ear(pHash);.    
36ebc 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 48 61  sqlite3_free(pHa
36ebd 73 68 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  sh);.  }.  retur
36ebe 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 21 53 51  n rc;.}..#if !SQ
36ebf 4c 49 54 45 5f 43 4f 52 45 0a 53 51 4c 49 54 45  LITE_CORE.SQLITE
36ec0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
36ec1 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 28  _extension_init(
36ec2 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
36ec3 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d  .  char **pzErrM
36ec4 73 67 2c 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69  sg,.  const sqli
36ec5 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73  te3_api_routines
36ec6 20 2a 70 41 70 69 0a 29 7b 0a 20 20 53 51 4c 49   *pApi.){.  SQLI
36ec7 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49  TE_EXTENSION_INI
36ec8 54 32 28 70 41 70 69 29 0a 20 20 72 65 74 75 72  T2(pApi).  retur
36ec9 6e 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69  n sqlite3Fts3Ini
36eca 74 28 64 62 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  t(db);.}.#endif.
36ecb 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69  .#endif /* !defi
36ecc 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29  ned(SQLITE_CORE)
36ecd 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
36ece 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 20  TE_ENABLE_FTS3) 
36ecf 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
36ed0 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73 33 2e  *** End of fts3.
36ed1 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
36ed2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36ed3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36ed4 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
36ed5 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66  *** Begin file f
36ed6 74 73 33 5f 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a  ts3_expr.c *****
36ed7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36ed8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36ed9 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4e  **/./*.** 2008 N
36eda 6f 76 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ov 28.**.** The 
36edb 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
36edc 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
36edd 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
36ede 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
36edf 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
36ee0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
36ee1 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
36ee2 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
36ee3 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
36ee4 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
36ee5 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
36ee6 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
36ee7 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
36ee8 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
36ee9 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
36eea 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
36eeb 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
36eec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36eed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36eee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36eef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
36ef1 54 68 69 73 20 6d 6f 64 75 6c 65 20 63 6f 6e 74  This module cont
36ef2 61 69 6e 73 20 63 6f 64 65 20 74 68 61 74 20 69  ains code that i
36ef3 6d 70 6c 65 6d 65 6e 74 73 20 61 20 70 61 72 73  mplements a pars
36ef4 65 72 20 66 6f 72 20 66 74 73 33 20 71 75 65 72  er for fts3 quer
36ef5 79 20 73 74 72 69 6e 67 73 0a 2a 2a 20 28 74 68  y strings.** (th
36ef6 65 20 72 69 67 68 74 2d 68 61 6e 64 20 61 72 67  e right-hand arg
36ef7 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 4d 41 54  ument to the MAT
36ef8 43 48 20 6f 70 65 72 61 74 6f 72 29 2e 20 42 65  CH operator). Be
36ef9 63 61 75 73 65 20 74 68 65 20 73 75 70 70 6f 72  cause the suppor
36efa 74 65 64 20 0a 2a 2a 20 73 79 6e 74 61 78 20 69  ted .** syntax i
36efb 73 20 72 65 6c 61 74 69 76 65 6c 79 20 73 69 6d  s relatively sim
36efc 70 6c 65 2c 20 74 68 65 20 77 68 6f 6c 65 20 74  ple, the whole t
36efd 6f 6b 65 6e 69 7a 65 72 2f 70 61 72 73 65 72 20  okenizer/parser 
36efe 73 79 73 74 65 6d 20 69 73 0a 2a 2a 20 68 61 6e  system is.** han
36eff 64 2d 63 6f 64 65 64 2e 20 54 68 65 20 70 75 62  d-coded. The pub
36f00 6c 69 63 20 69 6e 74 65 72 66 61 63 65 20 74 6f  lic interface to
36f01 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 73 20   this module is 
36f02 64 65 63 6c 61 72 65 64 20 69 6e 20 73 6f 75 72  declared in sour
36f03 63 65 0a 2a 2a 20 63 6f 64 65 20 66 69 6c 65 20  ce.** code file 
36f04 22 66 74 73 33 5f 65 78 70 72 2e 68 22 2e 0a 2a  "fts3_expr.h"..*
36f05 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  /.#if !defined(S
36f06 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64  QLITE_CORE) || d
36f07 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
36f08 41 42 4c 45 5f 46 54 53 33 29 0a 0a 2f 2a 0a 2a  ABLE_FTS3)../*.*
36f09 2a 20 42 79 20 64 65 66 61 75 6c 74 2c 20 74 68  * By default, th
36f0a 69 73 20 6d 6f 64 75 6c 65 20 70 61 72 73 65 73  is module parses
36f0b 20 74 68 65 20 6c 65 67 61 63 79 20 73 79 6e 74   the legacy synt
36f0c 61 78 20 74 68 61 74 20 68 61 73 20 62 65 65 6e  ax that has been
36f0d 20 0a 2a 2a 20 74 72 61 64 69 74 69 6f 6e 61 6c   .** traditional
36f0e 6c 79 20 75 73 65 64 20 62 79 20 66 74 73 33 2e  ly used by fts3.
36f0f 20 4f 72 2c 20 69 66 20 53 51 4c 49 54 45 5f 45   Or, if SQLITE_E
36f10 4e 41 42 4c 45 5f 46 54 53 33 5f 50 41 52 45 4e  NABLE_FTS3_PAREN
36f11 54 48 45 53 49 53 0a 2a 2a 20 69 73 20 64 65 66  THESIS.** is def
36f12 69 6e 65 64 2c 20 74 68 65 6e 20 69 74 20 75 73  ined, then it us
36f13 65 73 20 74 68 65 20 6e 65 77 20 73 79 6e 74 61  es the new synta
36f14 78 2e 20 54 68 65 20 64 69 66 66 65 72 65 6e 63  x. The differenc
36f15 65 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 74 68  es between.** th
36f16 65 20 6e 65 77 20 61 6e 64 20 74 68 65 20 6f 6c  e new and the ol
36f17 64 20 73 79 6e 74 61 78 65 73 20 61 72 65 3a 0a  d syntaxes are:.
36f18 2a 2a 0a 2a 2a 20 20 61 29 20 54 68 65 20 6e 65  **.**  a) The ne
36f19 77 20 73 79 6e 74 61 78 20 73 75 70 70 6f 72 74  w syntax support
36f1a 73 20 70 61 72 65 6e 74 68 65 73 69 73 2e 20 54  s parenthesis. T
36f1b 68 65 20 6f 6c 64 20 64 6f 65 73 20 6e 6f 74 2e  he old does not.
36f1c 0a 2a 2a 0a 2a 2a 20 20 62 29 20 54 68 65 20 6e  .**.**  b) The n
36f1d 65 77 20 73 79 6e 74 61 78 20 73 75 70 70 6f 72  ew syntax suppor
36f1e 74 73 20 74 68 65 20 41 4e 44 20 61 6e 64 20 4e  ts the AND and N
36f1f 4f 54 20 6f 70 65 72 61 74 6f 72 73 2e 20 54 68  OT operators. Th
36f20 65 20 6f 6c 64 20 64 6f 65 73 20 6e 6f 74 2e 0a  e old does not..
36f21 2a 2a 0a 2a 2a 20 20 63 29 20 54 68 65 20 6f 6c  **.**  c) The ol
36f22 64 20 73 79 6e 74 61 78 20 73 75 70 70 6f 72 74  d syntax support
36f23 73 20 74 68 65 20 22 2d 22 20 74 6f 6b 65 6e 20  s the "-" token 
36f24 71 75 61 6c 69 66 69 65 72 2e 20 54 68 69 73 20  qualifier. This 
36f25 69 73 20 6e 6f 74 20 0a 2a 2a 20 20 20 20 20 73  is not .**     s
36f26 75 70 70 6f 72 74 65 64 20 62 79 20 74 68 65 20  upported by the 
36f27 6e 65 77 20 73 79 6e 74 61 78 20 28 69 74 20 69  new syntax (it i
36f28 73 20 72 65 70 6c 61 63 65 64 20 62 79 20 74 68  s replaced by th
36f29 65 20 4e 4f 54 20 6f 70 65 72 61 74 6f 72 29 2e  e NOT operator).
36f2a 0a 2a 2a 0a 2a 2a 20 20 64 29 20 57 68 65 6e 20  .**.**  d) When 
36f2b 75 73 69 6e 67 20 74 68 65 20 6f 6c 64 20 73 79  using the old sy
36f2c 6e 74 61 78 2c 20 74 68 65 20 4f 52 20 6f 70 65  ntax, the OR ope
36f2d 72 61 74 6f 72 20 68 61 73 20 61 20 67 72 65 61  rator has a grea
36f2e 74 65 72 20 70 72 65 63 65 64 65 6e 63 65 0a 2a  ter precedence.*
36f2f 2a 20 20 20 20 20 74 68 61 6e 20 61 6e 20 69 6d  *     than an im
36f30 70 6c 69 63 69 74 20 41 4e 44 2e 20 57 68 65 6e  plicit AND. When
36f31 20 75 73 69 6e 67 20 74 68 65 20 6e 65 77 2c 20   using the new, 
36f32 62 6f 74 68 20 69 6d 70 6c 69 63 69 74 79 20 61  both implicity a
36f33 6e 64 20 65 78 70 6c 69 63 69 74 0a 2a 2a 20 20  nd explicit.**  
36f34 20 20 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 73     AND operators
36f35 20 68 61 76 65 20 61 20 68 69 67 68 65 72 20 70   have a higher p
36f36 72 65 63 65 64 65 6e 63 65 20 74 68 61 6e 20 4f  recedence than O
36f37 52 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 63 6f 6d 70  R..**.** If comp
36f38 69 6c 65 64 20 77 69 74 68 20 53 51 4c 49 54 45  iled with SQLITE
36f39 5f 54 45 53 54 20 64 65 66 69 6e 65 64 2c 20 74  _TEST defined, t
36f3a 68 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20  hen this module 
36f3b 65 78 70 6f 72 74 73 20 74 68 65 0a 2a 2a 20 73  exports the.** s
36f3c 79 6d 62 6f 6c 20 22 69 6e 74 20 73 71 6c 69 74  ymbol "int sqlit
36f3d 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70  e3_fts3_enable_p
36f3e 61 72 65 6e 74 68 65 73 65 73 22 2e 20 53 65 74  arentheses". Set
36f3f 74 69 6e 67 20 74 68 69 73 20 76 61 72 69 61 62  ting this variab
36f40 6c 65 0a 2a 2a 20 74 6f 20 7a 65 72 6f 20 63 61  le.** to zero ca
36f41 75 73 65 73 20 74 68 65 20 6d 6f 64 75 6c 65 20  uses the module 
36f42 74 6f 20 75 73 65 20 74 68 65 20 6f 6c 64 20 73  to use the old s
36f43 79 6e 74 61 78 2e 20 49 66 20 69 74 20 69 73 20  yntax. If it is 
36f44 73 65 74 20 74 6f 20 0a 2a 2a 20 6e 6f 6e 2d 7a  set to .** non-z
36f45 65 72 6f 20 74 68 65 20 6e 65 77 20 73 79 6e 74  ero the new synt
36f46 61 78 20 69 73 20 61 63 74 69 76 61 74 65 64 2e  ax is activated.
36f47 20 54 68 69 73 20 69 73 20 73 6f 20 62 6f 74 68   This is so both
36f48 20 73 79 6e 74 61 78 65 73 20 63 61 6e 0a 2a 2a   syntaxes can.**
36f49 20 62 65 20 74 65 73 74 65 64 20 75 73 69 6e 67   be tested using
36f4a 20 61 20 73 69 6e 67 6c 65 20 62 75 69 6c 64 20   a single build 
36f4b 6f 66 20 74 65 73 74 66 69 78 74 75 72 65 2e 0a  of testfixture..
36f4c 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
36f4d 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49  _TEST.SQLITE_API
36f4e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 74 73   int sqlite3_fts
36f4f 33 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68  3_enable_parenth
36f50 65 73 65 73 20 3d 20 30 3b 0a 23 65 6c 73 65 0a  eses = 0;.#else.
36f51 23 20 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  # ifdef SQLITE_E
36f52 4e 41 42 4c 45 5f 46 54 53 33 5f 50 41 52 45 4e  NABLE_FTS3_PAREN
36f53 54 48 45 53 49 53 20 0a 23 20 20 64 65 66 69 6e  THESIS .#  defin
36f54 65 20 73 71 6c 69 74 65 33 5f 66 74 73 33 5f 65  e sqlite3_fts3_e
36f55 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68 65 73 65  nable_parenthese
36f56 73 20 31 0a 23 20 65 6c 73 65 0a 23 20 20 64 65  s 1.# else.#  de
36f57 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 66 74 73  fine sqlite3_fts
36f58 33 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68  3_enable_parenth
36f59 65 73 65 73 20 30 0a 23 20 65 6e 64 69 66 0a 23  eses 0.# endif.#
36f5a 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66  endif../*.** Def
36f5b 61 75 6c 74 20 73 70 61 6e 20 66 6f 72 20 4e 45  ault span for NE
36f5c 41 52 20 6f 70 65 72 61 74 6f 72 73 2e 0a 2a 2f  AR operators..*/
36f5d 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
36f5e 46 54 53 33 5f 44 45 46 41 55 4c 54 5f 4e 45 41  FTS3_DEFAULT_NEA
36f5f 52 5f 50 41 52 41 4d 20 31 30 0a 0a 0a 74 79 70  R_PARAM 10...typ
36f60 65 64 65 66 20 73 74 72 75 63 74 20 50 61 72 73  edef struct Pars
36f61 65 43 6f 6e 74 65 78 74 20 50 61 72 73 65 43 6f  eContext ParseCo
36f62 6e 74 65 78 74 3b 0a 73 74 72 75 63 74 20 50 61  ntext;.struct Pa
36f63 72 73 65 43 6f 6e 74 65 78 74 20 7b 0a 20 20 73  rseContext {.  s
36f64 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
36f65 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 3b 20 20 20   *pTokenizer;   
36f66 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20     /* Tokenizer 
36f67 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73  module */.  cons
36f68 74 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 3b 20  t char **azCol; 
36f69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36f6a 2f 2a 20 41 72 72 61 79 20 6f 66 20 63 6f 6c 75  /* Array of colu
36f6b 6d 6e 20 6e 61 6d 65 73 20 66 6f 72 20 66 74 73  mn names for fts
36f6c 33 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  3 table */.  int
36f6d 20 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20   nCol;          
36f6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36f6f 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
36f70 74 72 69 65 73 20 69 6e 20 61 7a 43 6f 6c 5b 5d  tries in azCol[]
36f71 20 2a 2f 0a 20 20 69 6e 74 20 69 44 65 66 61 75   */.  int iDefau
36f72 6c 74 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20  ltCol;          
36f73 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66            /* Def
36f74 61 75 6c 74 20 63 6f 6c 75 6d 6e 20 74 6f 20 71  ault column to q
36f75 75 65 72 79 20 2a 2f 0a 20 20 73 71 6c 69 74 65  uery */.  sqlite
36f76 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 3b  3_context *pCtx;
36f77 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
36f78 20 57 72 69 74 65 20 65 72 72 6f 72 20 6d 65 73   Write error mes
36f79 73 61 67 65 20 68 65 72 65 20 2a 2f 0a 20 20 69  sage here */.  i
36f7a 6e 74 20 6e 4e 65 73 74 3b 20 20 20 20 20 20 20  nt nNest;       
36f7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36f7c 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
36f7d 6e 65 73 74 65 64 20 62 72 61 63 6b 65 74 73 20  nested brackets 
36f7e 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  */.};../*.** Thi
36f7f 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 65 71  s function is eq
36f80 75 69 76 61 6c 65 6e 74 20 74 6f 20 74 68 65 20  uivalent to the 
36f81 73 74 61 6e 64 61 72 64 20 69 73 73 70 61 63 65  standard isspace
36f82 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 0a 2a 2a  () function. .**
36f83 0a 2a 2a 20 54 68 65 20 73 74 61 6e 64 61 72 64  .** The standard
36f84 20 69 73 73 70 61 63 65 28 29 20 63 61 6e 20 62   isspace() can b
36f85 65 20 61 77 6b 77 61 72 64 20 74 6f 20 75 73 65  e awkward to use
36f86 20 73 61 66 65 6c 79 2c 20 62 65 63 61 75 73 65   safely, because
36f87 20 61 6c 74 68 6f 75 67 68 20 69 74 0a 2a 2a 20   although it.** 
36f88 69 73 20 64 65 66 69 6e 65 64 20 74 6f 20 61 63  is defined to ac
36f89 63 65 70 74 20 61 6e 20 61 72 67 75 6d 65 6e 74  cept an argument
36f8a 20 6f 66 20 74 79 70 65 20 69 6e 74 2c 20 69 74   of type int, it
36f8b 73 20 62 65 68 61 76 69 6f 75 72 20 77 68 65 6e  s behaviour when
36f8c 20 70 61 73 73 65 64 0a 2a 2a 20 61 6e 20 69 6e   passed.** an in
36f8d 74 65 67 65 72 20 74 68 61 74 20 66 61 6c 6c 73  teger that falls
36f8e 20 6f 75 74 73 69 64 65 20 6f 66 20 74 68 65 20   outside of the 
36f8f 72 61 6e 67 65 20 6f 66 20 74 68 65 20 75 6e 73  range of the uns
36f90 69 67 6e 65 64 20 63 68 61 72 20 74 79 70 65 0a  igned char type.
36f91 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20  ** is undefined 
36f92 28 61 6e 64 20 73 6f 6d 65 74 69 6d 65 73 2c 20  (and sometimes, 
36f93 22 75 6e 64 65 66 69 6e 65 64 22 20 6d 65 61 6e  "undefined" mean
36f94 73 20 73 65 67 66 61 75 6c 74 29 2e 20 54 68 69  s segfault). Thi
36f95 73 20 77 72 61 70 70 65 72 0a 2a 2a 20 69 73 20  s wrapper.** is 
36f96 64 65 66 69 6e 65 64 20 74 6f 20 61 63 63 65 70  defined to accep
36f97 74 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 6f 66  t an argument of
36f98 20 74 79 70 65 20 63 68 61 72 2c 20 61 6e 64 20   type char, and 
36f99 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 30  always returns 0
36f9a 20 66 6f 72 0a 2a 2a 20 61 6e 79 20 76 61 6c 75   for.** any valu
36f9b 65 73 20 74 68 61 74 20 66 61 6c 6c 20 6f 75 74  es that fall out
36f9c 73 69 64 65 20 6f 66 20 74 68 65 20 72 61 6e 67  side of the rang
36f9d 65 20 6f 66 20 74 68 65 20 75 6e 73 69 67 6e 65  e of the unsigne
36f9e 64 20 63 68 61 72 20 74 79 70 65 20 28 69 2e 65  d char type (i.e
36f9f 2e 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20 76 61  ..** negative va
36fa0 6c 75 65 73 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  lues)..*/.static
36fa1 20 69 6e 74 20 66 74 73 33 69 73 73 70 61 63 65   int fts3isspace
36fa2 28 63 68 61 72 20 63 29 7b 0a 20 20 72 65 74 75  (char c){.  retu
36fa3 72 6e 20 28 63 26 30 78 38 30 29 3d 3d 30 20 3f  rn (c&0x80)==0 ?
36fa4 20 69 73 73 70 61 63 65 28 63 29 20 3a 20 30 3b   isspace(c) : 0;
36fa5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63  .}../*.** Extrac
36fa6 74 20 74 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e  t the next token
36fa7 20 66 72 6f 6d 20 62 75 66 66 65 72 20 7a 20 28   from buffer z (
36fa8 6c 65 6e 67 74 68 20 6e 29 20 75 73 69 6e 67 20  length n) using 
36fa9 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2a  the tokenizer.**
36faa 20 61 6e 64 20 6f 74 68 65 72 20 69 6e 66 6f 72   and other infor
36fab 6d 61 74 69 6f 6e 20 28 63 6f 6c 75 6d 6e 20 6e  mation (column n
36fac 61 6d 65 73 20 65 74 63 2e 29 20 69 6e 20 70 50  ames etc.) in pP
36fad 61 72 73 65 2e 20 43 72 65 61 74 65 20 61 6e 20  arse. Create an 
36fae 46 74 73 33 45 78 70 72 0a 2a 2a 20 73 74 72 75  Fts3Expr.** stru
36faf 63 74 75 72 65 20 6f 66 20 74 79 70 65 20 46 54  cture of type FT
36fb0 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 63 6f  SQUERY_PHRASE co
36fb1 6e 74 61 69 6e 69 6e 67 20 61 20 70 68 72 61 73  ntaining a phras
36fb2 65 20 63 6f 6e 73 69 73 74 69 6e 67 20 6f 66 20  e consisting of 
36fb3 74 68 69 73 0a 2a 2a 20 73 69 6e 67 6c 65 20 74  this.** single t
36fb4 6f 6b 65 6e 20 61 6e 64 20 73 65 74 20 2a 70 70  oken and set *pp
36fb5 45 78 70 72 20 74 6f 20 70 6f 69 6e 74 20 74 6f  Expr to point to
36fb6 20 69 74 2e 20 49 66 20 74 68 65 20 65 6e 64 20   it. If the end 
36fb7 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 69 73  of the buffer is
36fb8 0a 2a 2a 20 72 65 61 63 68 65 64 20 62 65 66 6f  .** reached befo
36fb9 72 65 20 61 20 74 6f 6b 65 6e 20 69 73 20 66 6f  re a token is fo
36fba 75 6e 64 2c 20 73 65 74 20 2a 70 70 45 78 70 72  und, set *ppExpr
36fbb 20 74 6f 20 7a 65 72 6f 2e 20 49 74 20 69 73 20   to zero. It is 
36fbc 74 68 65 0a 2a 2a 20 72 65 73 70 6f 6e 73 69 62  the.** responsib
36fbd 69 6c 69 74 79 20 6f 66 20 74 68 65 20 63 61 6c  ility of the cal
36fbe 6c 65 72 20 74 6f 20 65 76 65 6e 74 75 61 6c 6c  ler to eventuall
36fbf 79 20 64 65 61 6c 6c 6f 63 61 74 65 20 74 68 65  y deallocate the
36fc0 20 61 6c 6c 6f 63 61 74 65 64 20 0a 2a 2a 20 46   allocated .** F
36fc1 74 73 33 45 78 70 72 20 73 74 72 75 63 74 75 72  ts3Expr structur
36fc2 65 20 28 69 66 20 61 6e 79 29 20 62 79 20 70 61  e (if any) by pa
36fc3 73 73 69 6e 67 20 69 74 20 74 6f 20 73 71 6c 69  ssing it to sqli
36fc4 74 65 33 5f 66 72 65 65 28 29 2e 0a 2a 2a 0a 2a  te3_free()..**.*
36fc5 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  * Return SQLITE_
36fc6 4f 4b 20 69 66 20 73 75 63 63 65 73 73 66 75 6c  OK if successful
36fc7 2c 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  , or SQLITE_NOME
36fc8 4d 20 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c  M if a memory al
36fc9 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 66 61 69 6c  location.** fail
36fca 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
36fcb 20 67 65 74 4e 65 78 74 54 6f 6b 65 6e 28 0a 20   getNextToken(. 
36fcc 20 50 61 72 73 65 43 6f 6e 74 65 78 74 20 2a 70   ParseContext *p
36fcd 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
36fce 20 20 20 20 20 20 20 20 20 2f 2a 20 66 74 73 33           /* fts3
36fcf 20 71 75 65 72 79 20 70 61 72 73 65 20 63 6f 6e   query parse con
36fd0 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 43  text */.  int iC
36fd1 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ol,             
36fd2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36fd3 20 20 2f 2a 20 56 61 6c 75 65 20 66 6f 72 20 46    /* Value for F
36fd4 74 73 33 50 68 72 61 73 65 2e 69 43 6f 6c 75 6d  ts3Phrase.iColum
36fd5 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  n */.  const cha
36fd6 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 20 20 20  r *z, int n,    
36fd7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
36fd8 2a 20 49 6e 70 75 74 20 73 74 72 69 6e 67 20 2a  * Input string *
36fd9 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a 2a 70  /.  Fts3Expr **p
36fda 70 45 78 70 72 2c 20 20 20 20 20 20 20 20 20 20  pExpr,          
36fdb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
36fdc 55 54 3a 20 65 78 70 72 65 73 73 69 6f 6e 20 2a  UT: expression *
36fdd 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6e 73 75  /.  int *pnConsu
36fde 6d 65 64 20 20 20 20 20 20 20 20 20 20 20 20 20  med             
36fdf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
36fe0 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79  UT: Number of by
36fe1 74 65 73 20 63 6f 6e 73 75 6d 65 64 20 2a 2f 0a  tes consumed */.
36fe2 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  ){.  sqlite3_tok
36fe3 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a  enizer *pTokeniz
36fe4 65 72 20 3d 20 70 50 61 72 73 65 2d 3e 70 54 6f  er = pParse->pTo
36fe5 6b 65 6e 69 7a 65 72 3b 0a 20 20 73 71 6c 69 74  kenizer;.  sqlit
36fe6 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
36fe7 75 6c 65 20 63 6f 6e 73 74 20 2a 70 4d 6f 64 75  ule const *pModu
36fe8 6c 65 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 2d  le = pTokenizer-
36fe9 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e 74 20  >pModule;.  int 
36fea 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f  rc;.  sqlite3_to
36feb 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
36fec 70 43 75 72 73 6f 72 3b 0a 20 20 46 74 73 33 45  pCursor;.  Fts3E
36fed 78 70 72 20 2a 70 52 65 74 20 3d 20 30 3b 0a 20  xpr *pRet = 0;. 
36fee 20 69 6e 74 20 6e 43 6f 6e 73 75 6d 65 64 20 3d   int nConsumed =
36fef 20 30 3b 0a 0a 20 20 72 63 20 3d 20 70 4d 6f 64   0;..  rc = pMod
36ff0 75 6c 65 2d 3e 78 4f 70 65 6e 28 70 54 6f 6b 65  ule->xOpen(pToke
36ff1 6e 69 7a 65 72 2c 20 7a 2c 20 6e 2c 20 26 70 43  nizer, z, n, &pC
36ff2 75 72 73 6f 72 29 3b 0a 20 20 69 66 28 20 72 63  ursor);.  if( rc
36ff3 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
36ff4 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
36ff5 54 6f 6b 65 6e 3b 0a 20 20 20 20 69 6e 74 20 6e  Token;.    int n
36ff6 54 6f 6b 65 6e 2c 20 69 53 74 61 72 74 2c 20 69  Token, iStart, i
36ff7 45 6e 64 2c 20 69 50 6f 73 69 74 69 6f 6e 3b 0a  End, iPosition;.
36ff8 20 20 20 20 69 6e 74 20 6e 42 79 74 65 3b 20 20      int nByte;  
36ff9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36ffa 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
36ffb 74 6f 74 61 6c 20 73 70 61 63 65 20 74 6f 20 61  total space to a
36ffc 6c 6c 6f 63 61 74 65 20 2a 2f 0a 0a 20 20 20 20  llocate */..    
36ffd 70 43 75 72 73 6f 72 2d 3e 70 54 6f 6b 65 6e 69  pCursor->pTokeni
36ffe 7a 65 72 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72  zer = pTokenizer
36fff 3b 0a 20 20 20 20 72 63 20 3d 20 70 4d 6f 64 75  ;.    rc = pModu
37000 6c 65 2d 3e 78 4e 65 78 74 28 70 43 75 72 73 6f  le->xNext(pCurso
37001 72 2c 20 26 7a 54 6f 6b 65 6e 2c 20 26 6e 54 6f  r, &zToken, &nTo
37002 6b 65 6e 2c 20 26 69 53 74 61 72 74 2c 20 26 69  ken, &iStart, &i
37003 45 6e 64 2c 20 26 69 50 6f 73 69 74 69 6f 6e 29  End, &iPosition)
37004 3b 0a 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  ;..    if( rc==S
37005 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
37006 20 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66    nByte = sizeof
37007 28 46 74 73 33 45 78 70 72 29 20 2b 20 73 69 7a  (Fts3Expr) + siz
37008 65 6f 66 28 46 74 73 33 50 68 72 61 73 65 29 20  eof(Fts3Phrase) 
37009 2b 20 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20  + nToken;.      
3700a 70 52 65 74 20 3d 20 28 46 74 73 33 45 78 70 72  pRet = (Fts3Expr
3700b 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   *)sqlite3_mallo
3700c 63 28 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20  c(nByte);.      
3700d 69 66 28 20 21 70 52 65 74 20 29 7b 0a 20 20 20  if( !pRet ){.   
3700e 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
3700f 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 65  _NOMEM;.      }e
37010 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d  lse{.        mem
37011 73 65 74 28 70 52 65 74 2c 20 30 2c 20 6e 42 79  set(pRet, 0, nBy
37012 74 65 29 3b 0a 20 20 20 20 20 20 20 20 70 52 65  te);.        pRe
37013 74 2d 3e 65 54 79 70 65 20 3d 20 46 54 53 51 55  t->eType = FTSQU
37014 45 52 59 5f 50 48 52 41 53 45 3b 0a 20 20 20 20  ERY_PHRASE;.    
37015 20 20 20 20 70 52 65 74 2d 3e 70 50 68 72 61 73      pRet->pPhras
37016 65 20 3d 20 28 46 74 73 33 50 68 72 61 73 65 20  e = (Fts3Phrase 
37017 2a 29 26 70 52 65 74 5b 31 5d 3b 0a 20 20 20 20  *)&pRet[1];.    
37018 20 20 20 20 70 52 65 74 2d 3e 70 50 68 72 61 73      pRet->pPhras
37019 65 2d 3e 6e 54 6f 6b 65 6e 20 3d 20 31 3b 0a 20  e->nToken = 1;. 
3701a 20 20 20 20 20 20 20 70 52 65 74 2d 3e 70 50 68         pRet->pPh
3701b 72 61 73 65 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20  rase->iColumn = 
3701c 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 52  iCol;.        pR
3701d 65 74 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f  et->pPhrase->aTo
3701e 6b 65 6e 5b 30 5d 2e 6e 20 3d 20 6e 54 6f 6b 65  ken[0].n = nToke
3701f 6e 3b 0a 20 20 20 20 20 20 20 20 70 52 65 74 2d  n;.        pRet-
37020 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e  >pPhrase->aToken
37021 5b 30 5d 2e 7a 20 3d 20 28 63 68 61 72 20 2a 29  [0].z = (char *)
37022 26 70 52 65 74 2d 3e 70 50 68 72 61 73 65 5b 31  &pRet->pPhrase[1
37023 5d 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70  ];.        memcp
37024 79 28 70 52 65 74 2d 3e 70 50 68 72 61 73 65 2d  y(pRet->pPhrase-
37025 3e 61 54 6f 6b 65 6e 5b 30 5d 2e 7a 2c 20 7a 54  >aToken[0].z, zT
37026 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29 3b 0a 0a  oken, nToken);..
37027 20 20 20 20 20 20 20 20 69 66 28 20 69 45 6e 64          if( iEnd
37028 3c 6e 20 26 26 20 7a 5b 69 45 6e 64 5d 3d 3d 27  <n && z[iEnd]=='
37029 2a 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  *' ){.          
3702a 70 52 65 74 2d 3e 70 50 68 72 61 73 65 2d 3e 61  pRet->pPhrase->a
3702b 54 6f 6b 65 6e 5b 30 5d 2e 69 73 50 72 65 66 69  Token[0].isPrefi
3702c 78 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  x = 1;.         
3702d 20 69 45 6e 64 2b 2b 3b 0a 20 20 20 20 20 20 20   iEnd++;.       
3702e 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 21   }.        if( !
3702f 73 71 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e 61  sqlite3_fts3_ena
37030 62 6c 65 5f 70 61 72 65 6e 74 68 65 73 65 73 20  ble_parentheses 
37031 26 26 20 69 53 74 61 72 74 3e 30 20 26 26 20 7a  && iStart>0 && z
37032 5b 69 53 74 61 72 74 2d 31 5d 3d 3d 27 2d 27 20  [iStart-1]=='-' 
37033 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 52 65  ){.          pRe
37034 74 2d 3e 70 50 68 72 61 73 65 2d 3e 69 73 4e 6f  t->pPhrase->isNo
37035 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  t = 1;.        }
37036 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6e  .      }.      n
37037 43 6f 6e 73 75 6d 65 64 20 3d 20 69 45 6e 64 3b  Consumed = iEnd;
37038 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 4d 6f 64  .    }..    pMod
37039 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 43 75 72  ule->xClose(pCur
3703a 73 6f 72 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 2a  sor);.  }.  .  *
3703b 70 6e 43 6f 6e 73 75 6d 65 64 20 3d 20 6e 43 6f  pnConsumed = nCo
3703c 6e 73 75 6d 65 64 3b 0a 20 20 2a 70 70 45 78 70  nsumed;.  *ppExp
3703d 72 20 3d 20 70 52 65 74 3b 0a 20 20 72 65 74 75  r = pRet;.  retu
3703e 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  rn rc;.}.../*.**
3703f 20 45 6e 6c 61 72 67 65 20 61 20 6d 65 6d 6f 72   Enlarge a memor
37040 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 49  y allocation.  I
37041 66 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f  f an out-of-memo
37042 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 63  ry allocation oc
37043 63 75 72 73 2c 0a 2a 2a 20 74 68 65 6e 20 66 72  curs,.** then fr
37044 65 65 20 74 68 65 20 6f 6c 64 20 61 6c 6c 6f 63  ee the old alloc
37045 61 74 69 6f 6e 2e 0a 2a 2f 0a 76 6f 69 64 20 2a  ation..*/.void *
37046 66 74 73 33 52 65 61 6c 6c 6f 63 4f 72 46 72 65  fts3ReallocOrFre
37047 65 28 76 6f 69 64 20 2a 70 4f 72 69 67 2c 20 69  e(void *pOrig, i
37048 6e 74 20 6e 4e 65 77 29 7b 0a 20 20 76 6f 69 64  nt nNew){.  void
37049 20 2a 70 52 65 74 20 3d 20 73 71 6c 69 74 65 33   *pRet = sqlite3
3704a 5f 72 65 61 6c 6c 6f 63 28 70 4f 72 69 67 2c 20  _realloc(pOrig, 
3704b 6e 4e 65 77 29 3b 0a 20 20 69 66 28 20 21 70 52  nNew);.  if( !pR
3704c 65 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  et ){.    sqlite
3704d 33 5f 66 72 65 65 28 70 4f 72 69 67 29 3b 0a 20  3_free(pOrig);. 
3704e 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 52 65 74   }.  return pRet
3704f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 75 66 66 65  ;.}../*.** Buffe
37050 72 20 7a 49 6e 70 75 74 2c 20 6c 65 6e 67 74 68  r zInput, length
37051 20 6e 49 6e 70 75 74 2c 20 63 6f 6e 74 61 69 6e   nInput, contain
37052 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  s the contents o
37053 66 20 61 20 71 75 6f 74 65 64 20 73 74 72 69 6e  f a quoted strin
37054 67 0a 2a 2a 20 74 68 61 74 20 61 70 70 65 61 72  g.** that appear
37055 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61 6e  ed as part of an
37056 20 66 74 73 33 20 71 75 65 72 79 20 65 78 70 72   fts3 query expr
37057 65 73 73 69 6f 6e 2e 20 4e 65 69 74 68 65 72 20  ession. Neither 
37058 71 75 6f 74 65 20 63 68 61 72 61 63 74 65 72 0a  quote character.
37059 2a 2a 20 69 73 20 69 6e 63 6c 75 64 65 64 20 69  ** is included i
3705a 6e 20 74 68 65 20 62 75 66 66 65 72 2e 20 54 68  n the buffer. Th
3705b 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 65  is function atte
3705c 6d 70 74 73 20 74 6f 20 74 6f 6b 65 6e 69 7a 65  mpts to tokenize
3705d 20 74 68 65 20 65 6e 74 69 72 65 0a 2a 2a 20 69   the entire.** i
3705e 6e 70 75 74 20 62 75 66 66 65 72 20 61 6e 64 20  nput buffer and 
3705f 63 72 65 61 74 65 20 61 6e 20 46 74 73 33 45 78  create an Fts3Ex
37060 70 72 20 73 74 72 75 63 74 75 72 65 20 6f 66 20  pr structure of 
37061 74 79 70 65 20 46 54 53 51 55 45 52 59 5f 50 48  type FTSQUERY_PH
37062 52 41 53 45 20 0a 2a 2a 20 63 6f 6e 74 61 69 6e  RASE .** contain
37063 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 73 2e  ing the results.
37064 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73  .**.** If succes
37065 73 66 75 6c 2c 20 53 51 4c 49 54 45 5f 4f 4b 20  sful, SQLITE_OK 
37066 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
37067 2a 70 70 45 78 70 72 20 73 65 74 20 74 6f 20 70  *ppExpr set to p
37068 6f 69 6e 74 20 61 74 20 74 68 65 0a 2a 2a 20 61  oint at the.** a
37069 6c 6c 6f 63 61 74 65 64 20 46 74 73 33 45 78 70  llocated Fts3Exp
3706a 72 20 73 74 72 75 63 74 75 72 65 2e 20 4f 74 68  r structure. Oth
3706b 65 72 77 69 73 65 2c 20 65 69 74 68 65 72 20 53  erwise, either S
3706c 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 28 6f 75 74  QLITE_NOMEM (out
3706d 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 65 72   of memory.** er
3706e 72 6f 72 29 20 6f 72 20 53 51 4c 49 54 45 5f 45  ror) or SQLITE_E
3706f 52 52 4f 52 20 28 74 6f 6b 65 6e 69 7a 61 74 69  RROR (tokenizati
37070 6f 6e 20 65 72 72 6f 72 29 20 69 73 20 72 65 74  on error) is ret
37071 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 45 78 70  urned and *ppExp
37072 72 20 73 65 74 0a 2a 2a 20 74 6f 20 30 2e 0a 2a  r set.** to 0..*
37073 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74  /.static int get
37074 4e 65 78 74 53 74 72 69 6e 67 28 0a 20 20 50 61  NextString(.  Pa
37075 72 73 65 43 6f 6e 74 65 78 74 20 2a 70 50 61 72  rseContext *pPar
37076 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  se,             
37077 20 20 20 20 20 20 2f 2a 20 66 74 73 33 20 71 75        /* fts3 qu
37078 65 72 79 20 70 61 72 73 65 20 63 6f 6e 74 65 78  ery parse contex
37079 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  t */.  const cha
3707a 72 20 2a 7a 49 6e 70 75 74 2c 20 69 6e 74 20 6e  r *zInput, int n
3707b 49 6e 70 75 74 2c 20 20 20 20 20 20 20 20 20 2f  Input,         /
3707c 2a 20 49 6e 70 75 74 20 73 74 72 69 6e 67 20 2a  * Input string *
3707d 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a 2a 70  /.  Fts3Expr **p
3707e 70 45 78 70 72 20 20 20 20 20 20 20 20 20 20 20  pExpr           
3707f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
37080 55 54 3a 20 65 78 70 72 65 73 73 69 6f 6e 20 2a  UT: expression *
37081 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74  /.){.  sqlite3_t
37082 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e  okenizer *pToken
37083 69 7a 65 72 20 3d 20 70 50 61 72 73 65 2d 3e 70  izer = pParse->p
37084 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 73 71 6c  Tokenizer;.  sql
37085 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
37086 6f 64 75 6c 65 20 63 6f 6e 73 74 20 2a 70 4d 6f  odule const *pMo
37087 64 75 6c 65 20 3d 20 70 54 6f 6b 65 6e 69 7a 65  dule = pTokenize
37088 72 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e  r->pModule;.  in
37089 74 20 72 63 3b 0a 20 20 46 74 73 33 45 78 70 72  t rc;.  Fts3Expr
3708a 20 2a 70 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74   *p = 0;.  sqlit
3708b 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  e3_tokenizer_cur
3708c 73 6f 72 20 2a 70 43 75 72 73 6f 72 20 3d 20 30  sor *pCursor = 0
3708d 3b 0a 20 20 63 68 61 72 20 2a 7a 54 65 6d 70 20  ;.  char *zTemp 
3708e 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 54 65 6d 70  = 0;.  int nTemp
3708f 20 3d 20 30 3b 0a 0a 20 20 72 63 20 3d 20 70 4d   = 0;..  rc = pM
37090 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e 28 70 54 6f  odule->xOpen(pTo
37091 6b 65 6e 69 7a 65 72 2c 20 7a 49 6e 70 75 74 2c  kenizer, zInput,
37092 20 6e 49 6e 70 75 74 2c 20 26 70 43 75 72 73 6f   nInput, &pCurso
37093 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  r);.  if( rc==SQ
37094 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69  LITE_OK ){.    i
37095 6e 74 20 69 69 3b 0a 20 20 20 20 70 43 75 72 73  nt ii;.    pCurs
37096 6f 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 20 3d  or->pTokenizer =
37097 20 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 20   pTokenizer;.   
37098 20 66 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d 53   for(ii=0; rc==S
37099 51 4c 49 54 45 5f 4f 4b 3b 20 69 69 2b 2b 29 7b  QLITE_OK; ii++){
3709a 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61  .      const cha
3709b 72 20 2a 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20  r *zToken;.     
3709c 20 69 6e 74 20 6e 54 6f 6b 65 6e 2c 20 69 42 65   int nToken, iBe
3709d 67 69 6e 2c 20 69 45 6e 64 2c 20 69 50 6f 73 3b  gin, iEnd, iPos;
3709e 0a 20 20 20 20 20 20 72 63 20 3d 20 70 4d 6f 64  .      rc = pMod
3709f 75 6c 65 2d 3e 78 4e 65 78 74 28 70 43 75 72 73  ule->xNext(pCurs
370a0 6f 72 2c 20 26 7a 54 6f 6b 65 6e 2c 20 26 6e 54  or, &zToken, &nT
370a1 6f 6b 65 6e 2c 20 26 69 42 65 67 69 6e 2c 20 26  oken, &iBegin, &
370a2 69 45 6e 64 2c 20 26 69 50 6f 73 29 3b 0a 20 20  iEnd, &iPos);.  
370a3 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
370a4 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
370a5 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 73 69 7a   int nByte = siz
370a6 65 6f 66 28 46 74 73 33 45 78 70 72 29 20 2b 20  eof(Fts3Expr) + 
370a7 73 69 7a 65 6f 66 28 46 74 73 33 50 68 72 61 73  sizeof(Fts3Phras
370a8 65 29 3b 0a 20 20 20 20 20 20 20 20 70 20 3d 20  e);.        p = 
370a9 66 74 73 33 52 65 61 6c 6c 6f 63 4f 72 46 72 65  fts3ReallocOrFre
370aa 65 28 70 2c 20 6e 42 79 74 65 2b 69 69 2a 73 69  e(p, nByte+ii*si
370ab 7a 65 6f 66 28 73 74 72 75 63 74 20 50 68 72 61  zeof(struct Phra
370ac 73 65 54 6f 6b 65 6e 29 29 3b 0a 20 20 20 20 20  seToken));.     
370ad 20 20 20 7a 54 65 6d 70 20 3d 20 66 74 73 33 52     zTemp = fts3R
370ae 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 7a 54 65  eallocOrFree(zTe
370af 6d 70 2c 20 6e 54 65 6d 70 20 2b 20 6e 54 6f 6b  mp, nTemp + nTok
370b0 65 6e 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  en);.        if(
370b1 20 21 70 20 7c 7c 20 21 7a 54 65 6d 70 20 29 7b   !p || !zTemp ){
370b2 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
370b3 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 20 20 20 20  no_mem;.        
370b4 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 69  }.        if( ii
370b5 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
370b6 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 6e 42   memset(p, 0, nB
370b7 79 74 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  yte);.          
370b8 70 2d 3e 70 50 68 72 61 73 65 20 3d 20 28 46 74  p->pPhrase = (Ft
370b9 73 33 50 68 72 61 73 65 20 2a 29 26 70 5b 31 5d  s3Phrase *)&p[1]
370ba 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
370bb 20 20 20 20 70 2d 3e 70 50 68 72 61 73 65 20 3d      p->pPhrase =
370bc 20 28 46 74 73 33 50 68 72 61 73 65 20 2a 29 26   (Fts3Phrase *)&
370bd 70 5b 31 5d 3b 0a 20 20 20 20 20 20 20 20 70 2d  p[1];.        p-
370be 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e  >pPhrase->nToken
370bf 20 3d 20 69 69 2b 31 3b 0a 20 20 20 20 20 20 20   = ii+1;.       
370c0 20 70 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f   p->pPhrase->aTo
370c1 6b 65 6e 5b 69 69 5d 2e 6e 20 3d 20 6e 54 6f 6b  ken[ii].n = nTok
370c2 65 6e 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63  en;.        memc
370c3 70 79 28 26 7a 54 65 6d 70 5b 6e 54 65 6d 70 5d  py(&zTemp[nTemp]
370c4 2c 20 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e  , zToken, nToken
370c5 29 3b 0a 20 20 20 20 20 20 20 20 6e 54 65 6d 70  );.        nTemp
370c6 20 2b 3d 20 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20   += nToken;.    
370c7 20 20 20 20 69 66 28 20 69 45 6e 64 3c 6e 49 6e      if( iEnd<nIn
370c8 70 75 74 20 26 26 20 7a 49 6e 70 75 74 5b 69 45  put && zInput[iE
370c9 6e 64 5d 3d 3d 27 2a 27 20 29 7b 0a 20 20 20 20  nd]=='*' ){.    
370ca 20 20 20 20 20 20 70 2d 3e 70 50 68 72 61 73 65        p->pPhrase
370cb 2d 3e 61 54 6f 6b 65 6e 5b 69 69 5d 2e 69 73 50  ->aToken[ii].isP
370cc 72 65 66 69 78 20 3d 20 31 3b 0a 20 20 20 20 20  refix = 1;.     
370cd 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
370ce 20 20 20 20 70 2d 3e 70 50 68 72 61 73 65 2d 3e      p->pPhrase->
370cf 61 54 6f 6b 65 6e 5b 69 69 5d 2e 69 73 50 72 65  aToken[ii].isPre
370d0 66 69 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  fix = 0;.       
370d1 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
370d2 0a 0a 20 20 20 20 70 4d 6f 64 75 6c 65 2d 3e 78  ..    pModule->x
370d3 43 6c 6f 73 65 28 70 43 75 72 73 6f 72 29 3b 0a  Close(pCursor);.
370d4 20 20 20 20 70 43 75 72 73 6f 72 20 3d 20 30 3b      pCursor = 0;
370d5 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d  .  }..  if( rc==
370d6 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20  SQLITE_DONE ){. 
370d7 20 20 20 69 6e 74 20 6a 6a 3b 0a 20 20 20 20 63     int jj;.    c
370d8 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 20 20 69  har *zNew;.    i
370d9 6e 74 20 6e 4e 65 77 20 3d 20 30 3b 0a 20 20 20  nt nNew = 0;.   
370da 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 73 69 7a   int nByte = siz
370db 65 6f 66 28 46 74 73 33 45 78 70 72 29 20 2b 20  eof(Fts3Expr) + 
370dc 73 69 7a 65 6f 66 28 46 74 73 33 50 68 72 61 73  sizeof(Fts3Phras
370dd 65 29 3b 0a 20 20 20 20 6e 42 79 74 65 20 2b 3d  e);.    nByte +=
370de 20 28 70 3f 28 70 2d 3e 70 50 68 72 61 73 65 2d   (p?(p->pPhrase-
370df 3e 6e 54 6f 6b 65 6e 2d 31 29 3a 30 29 20 2a 20  >nToken-1):0) * 
370e0 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 50 68  sizeof(struct Ph
370e1 72 61 73 65 54 6f 6b 65 6e 29 3b 0a 20 20 20 20  raseToken);.    
370e2 70 20 3d 20 66 74 73 33 52 65 61 6c 6c 6f 63 4f  p = fts3ReallocO
370e3 72 46 72 65 65 28 70 2c 20 6e 42 79 74 65 20 2b  rFree(p, nByte +
370e4 20 6e 54 65 6d 70 29 3b 0a 20 20 20 20 69 66 28   nTemp);.    if(
370e5 20 21 70 20 29 7b 0a 20 20 20 20 20 20 67 6f 74   !p ){.      got
370e6 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a  o no_mem;.    }.
370e7 20 20 20 20 69 66 28 20 7a 54 65 6d 70 20 29 7b      if( zTemp ){
370e8 0a 20 20 20 20 20 20 7a 4e 65 77 20 3d 20 26 28  .      zNew = &(
370e9 28 28 63 68 61 72 20 2a 29 70 29 5b 6e 42 79 74  ((char *)p)[nByt
370ea 65 5d 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70  e]);.      memcp
370eb 79 28 7a 4e 65 77 2c 20 7a 54 65 6d 70 2c 20 6e  y(zNew, zTemp, n
370ec 54 65 6d 70 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Temp);.    }else
370ed 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70  {.      memset(p
370ee 2c 20 30 2c 20 6e 42 79 74 65 2b 6e 54 65 6d 70  , 0, nByte+nTemp
370ef 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e  );.    }.    p->
370f0 70 50 68 72 61 73 65 20 3d 20 28 46 74 73 33 50  pPhrase = (Fts3P
370f1 68 72 61 73 65 20 2a 29 26 70 5b 31 5d 3b 0a 20  hrase *)&p[1];. 
370f2 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c     for(jj=0; jj<
370f3 70 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b  p->pPhrase->nTok
370f4 65 6e 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20  en; jj++){.     
370f5 20 70 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f   p->pPhrase->aTo
370f6 6b 65 6e 5b 6a 6a 5d 2e 7a 20 3d 20 26 7a 4e 65  ken[jj].z = &zNe
370f7 77 5b 6e 4e 65 77 5d 3b 0a 20 20 20 20 20 20 6e  w[nNew];.      n
370f8 4e 65 77 20 2b 3d 20 70 2d 3e 70 50 68 72 61 73  New += p->pPhras
370f9 65 2d 3e 61 54 6f 6b 65 6e 5b 6a 6a 5d 2e 6e 3b  e->aToken[jj].n;
370fa 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
370fb 65 33 5f 66 72 65 65 28 7a 54 65 6d 70 29 3b 0a  e3_free(zTemp);.
370fc 20 20 20 20 70 2d 3e 65 54 79 70 65 20 3d 20 46      p->eType = F
370fd 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 3b 0a  TSQUERY_PHRASE;.
370fe 20 20 20 20 70 2d 3e 70 50 68 72 61 73 65 2d 3e      p->pPhrase->
370ff 69 43 6f 6c 75 6d 6e 20 3d 20 70 50 61 72 73 65  iColumn = pParse
37100 2d 3e 69 44 65 66 61 75 6c 74 43 6f 6c 3b 0a 20  ->iDefaultCol;. 
37101 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
37102 4b 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70 45 78 70  K;.  }..  *ppExp
37103 72 20 3d 20 70 3b 0a 20 20 72 65 74 75 72 6e 20  r = p;.  return 
37104 72 63 3b 0a 6e 6f 5f 6d 65 6d 3a 0a 0a 20 20 69  rc;.no_mem:..  i
37105 66 28 20 70 43 75 72 73 6f 72 20 29 7b 0a 20 20  f( pCursor ){.  
37106 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73    pModule->xClos
37107 65 28 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d 0a  e(pCursor);.  }.
37108 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
37109 54 65 6d 70 29 3b 0a 20 20 73 71 6c 69 74 65 33  Temp);.  sqlite3
3710a 5f 66 72 65 65 28 70 29 3b 0a 20 20 2a 70 70 45  _free(p);.  *ppE
3710b 78 70 72 20 3d 20 30 3b 0a 20 20 72 65 74 75 72  xpr = 0;.  retur
3710c 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
3710d 7d 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f  }../*.** Functio
3710e 6e 20 67 65 74 4e 65 78 74 4e 6f 64 65 28 29 2c  n getNextNode(),
3710f 20 77 68 69 63 68 20 69 73 20 63 61 6c 6c 65 64   which is called
37110 20 62 79 20 66 74 73 33 45 78 70 72 50 61 72 73   by fts3ExprPars
37111 65 28 29 2c 20 6d 61 79 20 69 74 73 65 6c 66 0a  e(), may itself.
37112 2a 2a 20 63 61 6c 6c 20 66 74 73 33 45 78 70 72  ** call fts3Expr
37113 50 61 72 73 65 28 29 2e 20 53 6f 20 74 68 69 73  Parse(). So this
37114 20 66 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61   forward declara
37115 74 69 6f 6e 20 69 73 20 72 65 71 75 69 72 65 64  tion is required
37116 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
37117 66 74 73 33 45 78 70 72 50 61 72 73 65 28 50 61  fts3ExprParse(Pa
37118 72 73 65 43 6f 6e 74 65 78 74 20 2a 2c 20 63 6f  rseContext *, co
37119 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c  nst char *, int,
3711a 20 46 74 73 33 45 78 70 72 20 2a 2a 2c 20 69 6e   Fts3Expr **, in
3711b 74 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  t *);../*.** The
3711c 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c 65   output variable
3711d 20 2a 70 70 45 78 70 72 20 69 73 20 70 6f 70 75   *ppExpr is popu
3711e 6c 61 74 65 64 20 77 69 74 68 20 61 6e 20 61 6c  lated with an al
3711f 6c 6f 63 61 74 65 64 20 46 74 73 33 45 78 70 72  located Fts3Expr
37120 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2c 20   .** structure, 
37121 6f 72 20 73 65 74 20 74 6f 20 30 20 69 66 20 74  or set to 0 if t
37122 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69 6e  he end of the in
37123 70 75 74 20 62 75 66 66 65 72 20 69 73 20 72 65  put buffer is re
37124 61 63 68 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74  ached..**.** Ret
37125 75 72 6e 73 20 61 6e 20 53 51 4c 69 74 65 20 65  urns an SQLite e
37126 72 72 6f 72 20 63 6f 64 65 2e 20 53 51 4c 49 54  rror code. SQLIT
37127 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68 69  E_OK if everythi
37128 6e 67 20 77 6f 72 6b 73 2c 20 53 51 4c 49 54 45  ng works, SQLITE
37129 5f 4e 4f 4d 45 4d 0a 2a 2a 20 69 66 20 61 20 6d  _NOMEM.** if a m
3712a 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f 63  alloc failure oc
3712b 63 75 72 73 2c 20 6f 72 20 53 51 4c 49 54 45 5f  curs, or SQLITE_
3712c 45 52 52 4f 52 20 69 66 20 61 20 70 61 72 73 65  ERROR if a parse
3712d 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e   error is encoun
3712e 74 65 72 65 64 2e 0a 2a 2a 20 49 66 20 53 51 4c  tered..** If SQL
3712f 49 54 45 5f 45 52 52 4f 52 20 69 73 20 72 65 74  ITE_ERROR is ret
37130 75 72 6e 65 64 2c 20 70 43 6f 6e 74 65 78 74 20  urned, pContext 
37131 69 73 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74  is populated wit
37132 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  h an error messa
37133 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ge..*/.static in
37134 74 20 67 65 74 4e 65 78 74 4e 6f 64 65 28 0a 20  t getNextNode(. 
37135 20 50 61 72 73 65 43 6f 6e 74 65 78 74 20 2a 70   ParseContext *p
37136 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
37137 20 20 20 20 20 20 20 20 20 2f 2a 20 66 74 73 33           /* fts3
37138 20 71 75 65 72 79 20 70 61 72 73 65 20 63 6f 6e   query parse con
37139 74 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  text */.  const 
3713a 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20  char *z, int n, 
3713b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3713c 20 20 2f 2a 20 49 6e 70 75 74 20 73 74 72 69 6e    /* Input strin
3713d 67 20 2a 2f 0a 20 20 46 74 73 33 45 78 70 72 20  g */.  Fts3Expr 
3713e 2a 2a 70 70 45 78 70 72 2c 20 20 20 20 20 20 20  **ppExpr,       
3713f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
37140 2a 20 4f 55 54 3a 20 65 78 70 72 65 73 73 69 6f  * OUT: expressio
37141 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 6f  n */.  int *pnCo
37142 6e 73 75 6d 65 64 20 20 20 20 20 20 20 20 20 20  nsumed          
37143 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
37144 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66  * OUT: Number of
37145 20 62 79 74 65 73 20 63 6f 6e 73 75 6d 65 64 20   bytes consumed 
37146 2a 2f 0a 29 7b 0a 20 20 73 74 61 74 69 63 20 63  */.){.  static c
37147 6f 6e 73 74 20 73 74 72 75 63 74 20 46 74 73 33  onst struct Fts3
37148 4b 65 79 77 6f 72 64 20 7b 0a 20 20 20 20 63 68  Keyword {.    ch
37149 61 72 20 7a 5b 34 5d 3b 20 20 20 20 20 20 20 20  ar z[4];        
3714a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3714b 20 20 20 20 2f 2a 20 4b 65 79 77 6f 72 64 20 74      /* Keyword t
3714c 65 78 74 20 2a 2f 0a 20 20 20 20 75 6e 73 69 67  ext */.    unsig
3714d 6e 65 64 20 63 68 61 72 20 6e 3b 20 20 20 20 20  ned char n;     
3714e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3714f 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68   /* Length of th
37150 65 20 6b 65 79 77 6f 72 64 20 2a 2f 0a 20 20 20  e keyword */.   
37151 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 70   unsigned char p
37152 61 72 65 6e 4f 6e 6c 79 3b 20 20 20 20 20 20 20  arenOnly;       
37153 20 20 20 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 76         /* Only v
37154 61 6c 69 64 20 69 6e 20 70 61 72 65 6e 20 6d 6f  alid in paren mo
37155 64 65 20 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e  de */.    unsign
37156 65 64 20 63 68 61 72 20 65 54 79 70 65 3b 20 20  ed char eType;  
37157 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37158 2f 2a 20 4b 65 79 77 6f 72 64 20 63 6f 64 65 20  /* Keyword code 
37159 2a 2f 0a 20 20 7d 20 61 4b 65 79 77 6f 72 64 5b  */.  } aKeyword[
3715a 5d 20 3d 20 7b 0a 20 20 20 20 7b 20 22 4f 52 22  ] = {.    { "OR"
3715b 20 2c 20 20 32 2c 20 30 2c 20 46 54 53 51 55 45   ,  2, 0, FTSQUE
3715c 52 59 5f 4f 52 20 20 20 7d 2c 0a 20 20 20 20 7b  RY_OR   },.    {
3715d 20 22 41 4e 44 22 2c 20 20 33 2c 20 31 2c 20 46   "AND",  3, 1, F
3715e 54 53 51 55 45 52 59 5f 41 4e 44 20 20 7d 2c 0a  TSQUERY_AND  },.
3715f 20 20 20 20 7b 20 22 4e 4f 54 22 2c 20 20 33 2c      { "NOT",  3,
37160 20 31 2c 20 46 54 53 51 55 45 52 59 5f 4e 4f 54   1, FTSQUERY_NOT
37161 20 20 7d 2c 0a 20 20 20 20 7b 20 22 4e 45 41 52    },.    { "NEAR
37162 22 2c 20 34 2c 20 30 2c 20 46 54 53 51 55 45 52  ", 4, 0, FTSQUER
37163 59 5f 4e 45 41 52 20 7d 0a 20 20 7d 3b 0a 20 20  Y_NEAR }.  };.  
37164 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 69 43  int ii;.  int iC
37165 6f 6c 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 4c 65  ol;.  int iColLe
37166 6e 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 46  n;.  int rc;.  F
37167 74 73 33 45 78 70 72 20 2a 70 52 65 74 20 3d 20  ts3Expr *pRet = 
37168 30 3b 0a 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  0;..  const char
37169 20 2a 7a 49 6e 70 75 74 20 3d 20 7a 3b 0a 20 20   *zInput = z;.  
3716a 69 6e 74 20 6e 49 6e 70 75 74 20 3d 20 6e 3b 0a  int nInput = n;.
3716b 0a 20 20 2f 2a 20 53 6b 69 70 20 6f 76 65 72 20  .  /* Skip over 
3716c 61 6e 79 20 77 68 69 74 65 73 70 61 63 65 20 62  any whitespace b
3716d 65 66 6f 72 65 20 63 68 65 63 6b 69 6e 67 20 66  efore checking f
3716e 6f 72 20 61 20 6b 65 79 77 6f 72 64 2c 20 61 6e  or a keyword, an
3716f 20 6f 70 65 6e 20 6f 72 0a 20 20 2a 2a 20 63 6c   open or.  ** cl
37170 6f 73 65 20 62 72 61 63 6b 65 74 2c 20 6f 72 20  ose bracket, or 
37171 61 20 71 75 6f 74 65 64 20 73 74 72 69 6e 67 2e  a quoted string.
37172 20 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20   .  */.  while( 
37173 6e 49 6e 70 75 74 3e 30 20 26 26 20 66 74 73 33  nInput>0 && fts3
37174 69 73 73 70 61 63 65 28 2a 7a 49 6e 70 75 74 29  isspace(*zInput)
37175 20 29 7b 0a 20 20 20 20 6e 49 6e 70 75 74 2d 2d   ){.    nInput--
37176 3b 0a 20 20 20 20 7a 49 6e 70 75 74 2b 2b 3b 0a  ;.    zInput++;.
37177 20 20 7d 0a 20 20 69 66 28 20 6e 49 6e 70 75 74    }.  if( nInput
37178 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
37179 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20  n SQLITE_DONE;. 
3717a 20 7d 0a 0a 20 20 2f 2a 20 53 65 65 20 69 66 20   }..  /* See if 
3717b 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77  we are dealing w
3717c 69 74 68 20 61 20 6b 65 79 77 6f 72 64 2e 20 2a  ith a keyword. *
3717d 2f 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  /.  for(ii=0; ii
3717e 3c 28 69 6e 74 29 28 73 69 7a 65 6f 66 28 61 4b  <(int)(sizeof(aK
3717f 65 79 77 6f 72 64 29 2f 73 69 7a 65 6f 66 28 73  eyword)/sizeof(s
37180 74 72 75 63 74 20 46 74 73 33 4b 65 79 77 6f 72  truct Fts3Keywor
37181 64 29 29 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  d)); ii++){.    
37182 63 6f 6e 73 74 20 73 74 72 75 63 74 20 46 74 73  const struct Fts
37183 33 4b 65 79 77 6f 72 64 20 2a 70 4b 65 79 20 3d  3Keyword *pKey =
37184 20 26 61 4b 65 79 77 6f 72 64 5b 69 69 5d 3b 0a   &aKeyword[ii];.
37185 0a 20 20 20 20 69 66 28 20 28 70 4b 65 79 2d 3e  .    if( (pKey->
37186 70 61 72 65 6e 4f 6e 6c 79 20 26 20 7e 73 71 6c  parenOnly & ~sql
37187 69 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65  ite3_fts3_enable
37188 5f 70 61 72 65 6e 74 68 65 73 65 73 29 21 3d 30  _parentheses)!=0
37189 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e   ){.      contin
3718a 75 65 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69  ue;.    }..    i
3718b 66 28 20 6e 49 6e 70 75 74 3e 3d 70 4b 65 79 2d  f( nInput>=pKey-
3718c 3e 6e 20 26 26 20 30 3d 3d 6d 65 6d 63 6d 70 28  >n && 0==memcmp(
3718d 7a 49 6e 70 75 74 2c 20 70 4b 65 79 2d 3e 7a 2c  zInput, pKey->z,
3718e 20 70 4b 65 79 2d 3e 6e 29 20 29 7b 0a 20 20 20   pKey->n) ){.   
3718f 20 20 20 69 6e 74 20 6e 4e 65 61 72 20 3d 20 53     int nNear = S
37190 51 4c 49 54 45 5f 46 54 53 33 5f 44 45 46 41 55  QLITE_FTS3_DEFAU
37191 4c 54 5f 4e 45 41 52 5f 50 41 52 41 4d 3b 0a 20  LT_NEAR_PARAM;. 
37192 20 20 20 20 20 69 6e 74 20 6e 4b 65 79 20 3d 20       int nKey = 
37193 70 4b 65 79 2d 3e 6e 3b 0a 20 20 20 20 20 20 63  pKey->n;.      c
37194 68 61 72 20 63 4e 65 78 74 3b 0a 0a 20 20 20 20  har cNext;..    
37195 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
37196 61 20 22 4e 45 41 52 22 20 6b 65 79 77 6f 72 64  a "NEAR" keyword
37197 2c 20 63 68 65 63 6b 20 66 6f 72 20 61 6e 20 65  , check for an e
37198 78 70 6c 69 63 69 74 20 6e 65 61 72 6e 65 73 73  xplicit nearness
37199 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70  . */.      if( p
3719a 4b 65 79 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51  Key->eType==FTSQ
3719b 55 45 52 59 5f 4e 45 41 52 20 29 7b 0a 20 20 20  UERY_NEAR ){.   
3719c 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 4b 65       assert( nKe
3719d 79 3d 3d 34 20 29 3b 0a 20 20 20 20 20 20 20 20  y==4 );.        
3719e 69 66 28 20 7a 49 6e 70 75 74 5b 34 5d 3d 3d 27  if( zInput[4]=='
3719f 2f 27 20 26 26 20 7a 49 6e 70 75 74 5b 35 5d 3e  /' && zInput[5]>
371a0 3d 27 30 27 20 26 26 20 7a 49 6e 70 75 74 5b 35  ='0' && zInput[5
371a1 5d 3c 3d 27 39 27 20 29 7b 0a 20 20 20 20 20 20  ]<='9' ){.      
371a2 20 20 20 20 6e 4e 65 61 72 20 3d 20 30 3b 0a 20      nNear = 0;. 
371a3 20 20 20 20 20 20 20 20 20 66 6f 72 28 6e 4b 65           for(nKe
371a4 79 3d 35 3b 20 7a 49 6e 70 75 74 5b 6e 4b 65 79  y=5; zInput[nKey
371a5 5d 3e 3d 27 30 27 20 26 26 20 7a 49 6e 70 75 74  ]>='0' && zInput
371a6 5b 6e 4b 65 79 5d 3c 3d 27 39 27 3b 20 6e 4b 65  [nKey]<='9'; nKe
371a7 79 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  y++){.          
371a8 20 20 6e 4e 65 61 72 20 3d 20 6e 4e 65 61 72 20    nNear = nNear 
371a9 2a 20 31 30 20 2b 20 28 7a 49 6e 70 75 74 5b 6e  * 10 + (zInput[n
371aa 4b 65 79 5d 20 2d 20 27 30 27 29 3b 0a 20 20 20  Key] - '0');.   
371ab 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
371ac 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20   }.      }..    
371ad 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69    /* At this poi
371ae 6e 74 20 74 68 69 73 20 69 73 20 70 72 6f 62 61  nt this is proba
371af 62 6c 79 20 61 20 6b 65 79 77 6f 72 64 2e 20 42  bly a keyword. B
371b0 75 74 20 66 6f 72 20 74 68 61 74 20 74 6f 20 62  ut for that to b
371b1 65 20 74 72 75 65 2c 0a 20 20 20 20 20 20 2a 2a  e true,.      **
371b2 20 74 68 65 20 6e 65 78 74 20 62 79 74 65 20 6d   the next byte m
371b3 75 73 74 20 63 6f 6e 74 61 69 6e 20 65 69 74 68  ust contain eith
371b4 65 72 20 77 68 69 74 65 73 70 61 63 65 2c 20 61  er whitespace, a
371b5 6e 20 6f 70 65 6e 20 6f 72 20 63 6c 6f 73 65 0a  n open or close.
371b6 20 20 20 20 20 20 2a 2a 20 70 61 72 65 6e 74 68        ** parenth
371b7 65 73 69 73 2c 20 61 20 71 75 6f 74 65 20 63 68  esis, a quote ch
371b8 61 72 61 63 74 65 72 2c 20 6f 72 20 45 4f 46 2e  aracter, or EOF.
371b9 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20   .      */.     
371ba 20 63 4e 65 78 74 20 3d 20 7a 49 6e 70 75 74 5b   cNext = zInput[
371bb 6e 4b 65 79 5d 3b 0a 20 20 20 20 20 20 69 66 28  nKey];.      if(
371bc 20 66 74 73 33 69 73 73 70 61 63 65 28 63 4e 65   fts3isspace(cNe
371bd 78 74 29 20 0a 20 20 20 20 20 20 20 7c 7c 20 63  xt) .       || c
371be 4e 65 78 74 3d 3d 27 22 27 20 7c 7c 20 63 4e 65  Next=='"' || cNe
371bf 78 74 3d 3d 27 28 27 20 7c 7c 20 63 4e 65 78 74  xt=='(' || cNext
371c0 3d 3d 27 29 27 20 7c 7c 20 63 4e 65 78 74 3d 3d  ==')' || cNext==
371c1 30 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20  0.      ){.     
371c2 20 20 20 70 52 65 74 20 3d 20 28 46 74 73 33 45     pRet = (Fts3E
371c3 78 70 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61  xpr *)sqlite3_ma
371c4 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 46 74 73 33  lloc(sizeof(Fts3
371c5 45 78 70 72 29 29 3b 0a 20 20 20 20 20 20 20 20  Expr));.        
371c6 6d 65 6d 73 65 74 28 70 52 65 74 2c 20 30 2c 20  memset(pRet, 0, 
371c7 73 69 7a 65 6f 66 28 46 74 73 33 45 78 70 72 29  sizeof(Fts3Expr)
371c8 29 3b 0a 20 20 20 20 20 20 20 20 70 52 65 74 2d  );.        pRet-
371c9 3e 65 54 79 70 65 20 3d 20 70 4b 65 79 2d 3e 65  >eType = pKey->e
371ca 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 70 52  Type;.        pR
371cb 65 74 2d 3e 6e 4e 65 61 72 20 3d 20 6e 4e 65 61  et->nNear = nNea
371cc 72 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 45 78  r;.        *ppEx
371cd 70 72 20 3d 20 70 52 65 74 3b 0a 20 20 20 20 20  pr = pRet;.     
371ce 20 20 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20 3d     *pnConsumed =
371cf 20 28 7a 49 6e 70 75 74 20 2d 20 7a 29 20 2b 20   (zInput - z) + 
371d0 6e 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 72 65  nKey;.        re
371d1 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
371d2 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
371d3 2a 20 54 75 72 6e 73 20 6f 75 74 20 74 68 61 74  * Turns out that
371d4 20 77 61 73 6e 27 74 20 61 20 6b 65 79 77 6f 72   wasn't a keywor
371d5 64 20 61 66 74 65 72 20 61 6c 6c 2e 20 54 68 69  d after all. Thi
371d6 73 20 68 61 70 70 65 6e 73 20 69 66 20 74 68 65  s happens if the
371d7 0a 20 20 20 20 20 20 2a 2a 20 75 73 65 72 20 68  .      ** user h
371d8 61 73 20 73 75 70 70 6c 69 65 64 20 61 20 74 6f  as supplied a to
371d9 6b 65 6e 20 73 75 63 68 20 61 73 20 22 4f 52 61  ken such as "ORa
371da 63 6c 65 22 2e 20 43 6f 6e 74 69 6e 75 65 2e 0a  cle". Continue..
371db 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 7d 0a 20        */.    }. 
371dc 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66   }..  /* Check f
371dd 6f 72 20 61 6e 20 6f 70 65 6e 20 62 72 61 63 6b  or an open brack
371de 65 74 2e 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  et. */.  if( sql
371df 69 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65  ite3_fts3_enable
371e0 5f 70 61 72 65 6e 74 68 65 73 65 73 20 29 7b 0a  _parentheses ){.
371e1 20 20 20 20 69 66 28 20 2a 7a 49 6e 70 75 74 3d      if( *zInput=
371e2 3d 27 28 27 20 29 7b 0a 20 20 20 20 20 20 69 6e  ='(' ){.      in
371e3 74 20 6e 43 6f 6e 73 75 6d 65 64 3b 0a 20 20 20  t nConsumed;.   
371e4 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 20     int rc;.     
371e5 20 70 50 61 72 73 65 2d 3e 6e 4e 65 73 74 2b 2b   pParse->nNest++
371e6 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 74 73  ;.      rc = fts
371e7 33 45 78 70 72 50 61 72 73 65 28 70 50 61 72 73  3ExprParse(pPars
371e8 65 2c 20 26 7a 49 6e 70 75 74 5b 31 5d 2c 20 6e  e, &zInput[1], n
371e9 49 6e 70 75 74 2d 31 2c 20 70 70 45 78 70 72 2c  Input-1, ppExpr,
371ea 20 26 6e 43 6f 6e 73 75 6d 65 64 29 3b 0a 20 20   &nConsumed);.  
371eb 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
371ec 54 45 5f 4f 4b 20 26 26 20 21 2a 70 70 45 78 70  TE_OK && !*ppExp
371ed 72 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  r ){.        rc 
371ee 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20  = SQLITE_DONE;. 
371ef 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 70 6e       }.      *pn
371f0 43 6f 6e 73 75 6d 65 64 20 3d 20 28 7a 49 6e 70  Consumed = (zInp
371f1 75 74 20 2d 20 7a 29 20 2b 20 31 20 2b 20 6e 43  ut - z) + 1 + nC
371f2 6f 6e 73 75 6d 65 64 3b 0a 20 20 20 20 20 20 72  onsumed;.      r
371f3 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
371f4 20 20 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20    .    /* Check 
371f5 66 6f 72 20 61 20 63 6c 6f 73 65 20 62 72 61 63  for a close brac
371f6 6b 65 74 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  ket. */.    if( 
371f7 2a 7a 49 6e 70 75 74 3d 3d 27 29 27 20 29 7b 0a  *zInput==')' ){.
371f8 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4e        pParse->nN
371f9 65 73 74 2d 2d 3b 0a 20 20 20 20 20 20 2a 70 6e  est--;.      *pn
371fa 43 6f 6e 73 75 6d 65 64 20 3d 20 28 7a 49 6e 70  Consumed = (zInp
371fb 75 74 20 2d 20 7a 29 20 2b 20 31 3b 0a 20 20 20  ut - z) + 1;.   
371fc 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
371fd 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d  _DONE;.    }.  }
371fe 0a 0a 20 20 2f 2a 20 53 65 65 20 69 66 20 77 65  ..  /* See if we
371ff 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74   are dealing wit
37200 68 20 61 20 71 75 6f 74 65 64 20 70 68 72 61 73  h a quoted phras
37201 65 2e 20 49 66 20 74 68 69 73 20 69 73 20 74 68  e. If this is th
37202 65 20 63 61 73 65 2c 20 74 68 65 6e 0a 20 20 2a  e case, then.  *
37203 2a 20 73 65 61 72 63 68 20 66 6f 72 20 74 68 65  * search for the
37204 20 63 6c 6f 73 69 6e 67 20 71 75 6f 74 65 20 61   closing quote a
37205 6e 64 20 70 61 73 73 20 74 68 65 20 77 68 6f 6c  nd pass the whol
37206 65 20 73 74 72 69 6e 67 20 74 6f 20 67 65 74 4e  e string to getN
37207 65 78 74 53 74 72 69 6e 67 28 29 0a 20 20 2a 2a  extString().  **
37208 20 66 6f 72 20 70 72 6f 63 65 73 73 69 6e 67 2e   for processing.
37209 20 54 68 69 73 20 69 73 20 65 61 73 79 20 74 6f   This is easy to
3720a 20 64 6f 2c 20 61 73 20 66 74 73 33 20 68 61 73   do, as fts3 has
3720b 20 6e 6f 20 73 79 6e 74 61 78 20 66 6f 72 20 65   no syntax for e
3720c 73 63 61 70 69 6e 67 0a 20 20 2a 2a 20 61 20 71  scaping.  ** a q
3720d 75 6f 74 65 20 63 68 61 72 61 63 74 65 72 20 65  uote character e
3720e 6d 62 65 64 64 65 64 20 69 6e 20 61 20 73 74 72  mbedded in a str
3720f 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ing..  */.  if( 
37210 2a 7a 49 6e 70 75 74 3d 3d 27 22 27 20 29 7b 0a  *zInput=='"' ){.
37211 20 20 20 20 66 6f 72 28 69 69 3d 31 3b 20 69 69      for(ii=1; ii
37212 3c 6e 49 6e 70 75 74 20 26 26 20 7a 49 6e 70 75  <nInput && zInpu
37213 74 5b 69 69 5d 21 3d 27 22 27 3b 20 69 69 2b 2b  t[ii]!='"'; ii++
37214 29 3b 0a 20 20 20 20 2a 70 6e 43 6f 6e 73 75 6d  );.    *pnConsum
37215 65 64 20 3d 20 28 7a 49 6e 70 75 74 20 2d 20 7a  ed = (zInput - z
37216 29 20 2b 20 69 69 20 2b 20 31 3b 0a 20 20 20 20  ) + ii + 1;.    
37217 69 66 28 20 69 69 3d 3d 6e 49 6e 70 75 74 20 29  if( ii==nInput )
37218 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
37219 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
3721a 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 67 65   }.    return ge
3721b 74 4e 65 78 74 53 74 72 69 6e 67 28 70 50 61 72  tNextString(pPar
3721c 73 65 2c 20 26 7a 49 6e 70 75 74 5b 31 5d 2c 20  se, &zInput[1], 
3721d 69 69 2d 31 2c 20 70 70 45 78 70 72 29 3b 0a 20  ii-1, ppExpr);. 
3721e 20 7d 0a 0a 0a 20 20 2f 2a 20 49 66 20 63 6f 6e   }...  /* If con
3721f 74 72 6f 6c 20 66 6c 6f 77 73 20 74 6f 20 74 68  trol flows to th
37220 69 73 20 70 6f 69 6e 74 2c 20 74 68 69 73 20 6d  is point, this m
37221 75 73 74 20 62 65 20 61 20 72 65 67 75 6c 61 72  ust be a regular
37222 20 74 6f 6b 65 6e 2c 20 6f 72 20 0a 20 20 2a 2a   token, or .  **
37223 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
37224 69 6e 70 75 74 2e 20 52 65 61 64 20 61 20 72 65  input. Read a re
37225 67 75 6c 61 72 20 74 6f 6b 65 6e 20 75 73 69 6e  gular token usin
37226 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 74 6f  g the sqlite3_to
37227 6b 65 6e 69 7a 65 72 0a 20 20 2a 2a 20 69 6e 74  kenizer.  ** int
37228 65 72 66 61 63 65 2e 20 42 65 66 6f 72 65 20 64  erface. Before d
37229 6f 69 6e 67 20 73 6f 2c 20 66 69 67 75 72 65 20  oing so, figure 
3722a 6f 75 74 20 69 66 20 74 68 65 72 65 20 69 73 20  out if there is 
3722b 61 6e 20 65 78 70 6c 69 63 69 74 0a 20 20 2a 2a  an explicit.  **
3722c 20 63 6f 6c 75 6d 6e 20 73 70 65 63 69 66 69 65   column specifie
3722d 72 20 66 6f 72 20 74 68 65 20 74 6f 6b 65 6e 2e  r for the token.
3722e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 4f 44 4f   .  **.  ** TODO
3722f 3a 20 53 74 72 61 6e 67 65 6c 79 2c 20 69 74 20  : Strangely, it 
37230 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  is not possible 
37231 74 6f 20 61 73 73 6f 63 69 61 74 65 20 61 20 63  to associate a c
37232 6f 6c 75 6d 6e 20 73 70 65 63 69 66 69 65 72 0a  olumn specifier.
37233 20 20 2a 2a 20 77 69 74 68 20 61 20 71 75 6f 74    ** with a quot
37234 65 64 20 70 68 72 61 73 65 2c 20 6f 6e 6c 79 20  ed phrase, only 
37235 77 69 74 68 20 61 20 73 69 6e 67 6c 65 20 74 6f  with a single to
37236 6b 65 6e 2e 20 4e 6f 74 20 73 75 72 65 20 69 66  ken. Not sure if
37237 20 74 68 69 73 20 77 61 73 0a 20 20 2a 2a 20 61   this was.  ** a
37238 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  n implementation
37239 20 61 72 74 69 66 61 63 74 20 6f 72 20 61 6e 20   artifact or an 
3723a 69 6e 74 65 6e 74 69 6f 6e 61 6c 20 64 65 63 69  intentional deci
3723b 73 69 6f 6e 20 77 68 65 6e 20 66 74 73 33 20 77  sion when fts3 w
3723c 61 73 0a 20 20 2a 2a 20 66 69 72 73 74 20 69 6d  as.  ** first im
3723d 70 6c 65 6d 65 6e 74 65 64 2e 20 57 68 69 63 68  plemented. Which
3723e 65 76 65 72 20 69 74 20 77 61 73 2c 20 74 68 69  ever it was, thi
3723f 73 20 6d 6f 64 75 6c 65 20 64 75 70 6c 69 63 61  s module duplica
37240 74 65 73 20 74 68 65 20 0a 20 20 2a 2a 20 6c 69  tes the .  ** li
37241 6d 69 74 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20  mitation..  */. 
37242 20 69 43 6f 6c 20 3d 20 70 50 61 72 73 65 2d 3e   iCol = pParse->
37243 69 44 65 66 61 75 6c 74 43 6f 6c 3b 0a 20 20 69  iDefaultCol;.  i
37244 43 6f 6c 4c 65 6e 20 3d 20 30 3b 0a 20 20 66 6f  ColLen = 0;.  fo
37245 72 28 69 69 3d 30 3b 20 69 69 3c 70 50 61 72 73  r(ii=0; ii<pPars
37246 65 2d 3e 6e 43 6f 6c 3b 20 69 69 2b 2b 29 7b 0a  e->nCol; ii++){.
37247 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
37248 7a 53 74 72 20 3d 20 70 50 61 72 73 65 2d 3e 61  zStr = pParse->a
37249 7a 43 6f 6c 5b 69 69 5d 3b 0a 20 20 20 20 69 6e  zCol[ii];.    in
3724a 74 20 6e 53 74 72 20 3d 20 73 74 72 6c 65 6e 28  t nStr = strlen(
3724b 7a 53 74 72 29 3b 0a 20 20 20 20 69 66 28 20 6e  zStr);.    if( n
3724c 49 6e 70 75 74 3e 6e 53 74 72 20 26 26 20 7a 49  Input>nStr && zI
3724d 6e 70 75 74 5b 6e 53 74 72 5d 3d 3d 27 3a 27 20  nput[nStr]==':' 
3724e 0a 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33  .     && sqlite3
3724f 5f 73 74 72 6e 69 63 6d 70 28 7a 53 74 72 2c 20  _strnicmp(zStr, 
37250 7a 49 6e 70 75 74 2c 20 6e 53 74 72 29 3d 3d 30  zInput, nStr)==0
37251 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69   .    ){.      i
37252 43 6f 6c 20 3d 20 69 69 3b 0a 20 20 20 20 20 20  Col = ii;.      
37253 69 43 6f 6c 4c 65 6e 20 3d 20 28 28 7a 49 6e 70  iColLen = ((zInp
37254 75 74 20 2d 20 7a 29 20 2b 20 6e 53 74 72 20 2b  ut - z) + nStr +
37255 20 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b   1);.      break
37256 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 63  ;.    }.  }.  rc
37257 20 3d 20 67 65 74 4e 65 78 74 54 6f 6b 65 6e 28   = getNextToken(
37258 70 50 61 72 73 65 2c 20 69 43 6f 6c 2c 20 26 7a  pParse, iCol, &z
37259 5b 69 43 6f 6c 4c 65 6e 5d 2c 20 6e 2d 69 43 6f  [iColLen], n-iCo
3725a 6c 4c 65 6e 2c 20 70 70 45 78 70 72 2c 20 70 6e  lLen, ppExpr, pn
3725b 43 6f 6e 73 75 6d 65 64 29 3b 0a 20 20 2a 70 6e  Consumed);.  *pn
3725c 43 6f 6e 73 75 6d 65 64 20 2b 3d 20 69 43 6f 6c  Consumed += iCol
3725d 4c 65 6e 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  Len;.  return rc
3725e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61  ;.}../*.** The a
3725f 72 67 75 6d 65 6e 74 20 69 73 20 61 6e 20 46 74  rgument is an Ft
37260 73 33 45 78 70 72 20 73 74 72 75 63 74 75 72 65  s3Expr structure
37261 20 66 6f 72 20 61 20 62 69 6e 61 72 79 20 6f 70   for a binary op
37262 65 72 61 74 6f 72 20 28 61 6e 79 20 74 79 70 65  erator (any type
37263 0a 2a 2a 20 65 78 63 65 70 74 20 61 6e 20 46 54  .** except an FT
37264 53 51 55 45 52 59 5f 50 48 52 41 53 45 29 2e 20  SQUERY_PHRASE). 
37265 52 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67 65  Return an intege
37266 72 20 76 61 6c 75 65 20 72 65 70 72 65 73 65 6e  r value represen
37267 74 69 6e 67 20 74 68 65 0a 2a 2a 20 70 72 65 63  ting the.** prec
37268 65 64 65 6e 63 65 20 6f 66 20 74 68 65 20 6f 70  edence of the op
37269 65 72 61 74 6f 72 2e 20 4c 6f 77 65 72 20 76 61  erator. Lower va
3726a 6c 75 65 73 20 68 61 76 65 20 61 20 68 69 67 68  lues have a high
3726b 65 72 20 70 72 65 63 65 64 65 6e 63 65 20 28 69  er precedence (i
3726c 2e 65 2e 0a 2a 2a 20 67 72 6f 75 70 20 6d 6f 72  .e..** group mor
3726d 65 20 74 69 67 68 74 6c 79 29 2e 20 46 6f 72 20  e tightly). For 
3726e 65 78 61 6d 70 6c 65 2c 20 69 6e 20 74 68 65 20  example, in the 
3726f 43 20 6c 61 6e 67 75 61 67 65 2c 20 74 68 65 20  C language, the 
37270 3d 3d 20 6f 70 65 72 61 74 6f 72 0a 2a 2a 20 67  == operator.** g
37271 72 6f 75 70 73 20 6d 6f 72 65 20 74 69 67 68 74  roups more tight
37272 6c 79 20 74 68 61 6e 20 7c 7c 2c 20 61 6e 64 20  ly than ||, and 
37273 77 6f 75 6c 64 20 74 68 65 72 65 66 6f 72 65 20  would therefore 
37274 68 61 76 65 20 61 20 68 69 67 68 65 72 20 70 72  have a higher pr
37275 65 63 65 64 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20  ecedence..**.** 
37276 57 68 65 6e 20 75 73 69 6e 67 20 74 68 65 20 6e  When using the n
37277 65 77 20 66 74 73 33 20 71 75 65 72 79 20 73 79  ew fts3 query sy
37278 6e 74 61 78 20 28 77 68 65 6e 20 53 51 4c 49 54  ntax (when SQLIT
37279 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 5f 50 41  E_ENABLE_FTS3_PA
3727a 52 45 4e 54 48 45 53 49 53 0a 2a 2a 20 69 73 20  RENTHESIS.** is 
3727b 64 65 66 69 6e 65 64 29 2c 20 74 68 65 20 6f 72  defined), the or
3727c 64 65 72 20 6f 66 20 74 68 65 20 6f 70 65 72 61  der of the opera
3727d 74 6f 72 73 20 69 6e 20 70 72 65 63 65 64 65 6e  tors in preceden
3727e 63 65 20 66 72 6f 6d 20 68 69 67 68 65 73 74 20  ce from highest 
3727f 74 6f 0a 2a 2a 20 6c 6f 77 65 73 74 20 69 73 3a  to.** lowest is:
37280 0a 2a 2a 0a 2a 2a 20 20 20 4e 45 41 52 0a 2a 2a  .**.**   NEAR.**
37281 20 20 20 4e 4f 54 0a 2a 2a 20 20 20 41 4e 44 20     NOT.**   AND 
37282 28 69 6e 63 6c 75 64 69 6e 67 20 69 6d 70 6c 69  (including impli
37283 63 69 74 20 41 4e 44 73 29 0a 2a 2a 20 20 20 4f  cit ANDs).**   O
37284 52 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  R.**.** Note tha
37285 74 20 77 68 65 6e 20 75 73 69 6e 67 20 74 68 65  t when using the
37286 20 6f 6c 64 20 71 75 65 72 79 20 73 79 6e 74 61   old query synta
37287 78 2c 20 74 68 65 20 4f 52 20 6f 70 65 72 61 74  x, the OR operat
37288 6f 72 20 68 61 73 20 61 20 68 69 67 68 65 72 0a  or has a higher.
37289 2a 2a 20 70 72 65 63 65 64 65 6e 63 65 20 74 68  ** precedence th
3728a 61 6e 20 74 68 65 20 41 4e 44 20 6f 70 65 72 61  an the AND opera
3728b 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  tor..*/.static i
3728c 6e 74 20 6f 70 50 72 65 63 65 64 65 6e 63 65 28  nt opPrecedence(
3728d 46 74 73 33 45 78 70 72 20 2a 70 29 7b 0a 20 20  Fts3Expr *p){.  
3728e 61 73 73 65 72 74 28 20 70 2d 3e 65 54 79 70 65  assert( p->eType
3728f 21 3d 46 54 53 51 55 45 52 59 5f 50 48 52 41 53  !=FTSQUERY_PHRAS
37290 45 20 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  E );.  if( sqlit
37291 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70  e3_fts3_enable_p
37292 61 72 65 6e 74 68 65 73 65 73 20 29 7b 0a 20 20  arentheses ){.  
37293 20 20 72 65 74 75 72 6e 20 70 2d 3e 65 54 79 70    return p->eTyp
37294 65 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  e;.  }else if( p
37295 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52  ->eType==FTSQUER
37296 59 5f 4e 45 41 52 20 29 7b 0a 20 20 20 20 72 65  Y_NEAR ){.    re
37297 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 20  turn 1;.  }else 
37298 69 66 28 20 70 2d 3e 65 54 79 70 65 3d 3d 46 54  if( p->eType==FT
37299 53 51 55 45 52 59 5f 4f 52 20 29 7b 0a 20 20 20  SQUERY_OR ){.   
3729a 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 7d 0a 20   return 2;.  }. 
3729b 20 61 73 73 65 72 74 28 20 70 2d 3e 65 54 79 70   assert( p->eTyp
3729c 65 3d 3d 46 54 53 51 55 45 52 59 5f 41 4e 44 20  e==FTSQUERY_AND 
3729d 29 3b 0a 20 20 72 65 74 75 72 6e 20 33 3b 0a 7d  );.  return 3;.}
3729e 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74  ../*.** Argument
3729f 20 70 70 48 65 61 64 20 63 6f 6e 74 61 69 6e 73   ppHead contains
372a0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
372a1 65 20 63 75 72 72 65 6e 74 20 68 65 61 64 20 6f  e current head o
372a2 66 20 61 20 71 75 65 72 79 20 0a 2a 2a 20 65 78  f a query .** ex
372a3 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 62 65  pression tree be
372a4 69 6e 67 20 70 61 72 73 65 64 2e 20 70 50 72 65  ing parsed. pPre
372a5 76 20 69 73 20 74 68 65 20 65 78 70 72 65 73 73  v is the express
372a6 69 6f 6e 20 6e 6f 64 65 20 6d 6f 73 74 20 72 65  ion node most re
372a7 63 65 6e 74 6c 79 0a 2a 2a 20 69 6e 73 65 72 74  cently.** insert
372a8 65 64 20 69 6e 74 6f 20 74 68 65 20 74 72 65 65  ed into the tree
372a9 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
372aa 61 64 64 73 20 70 4e 65 77 2c 20 77 68 69 63 68  adds pNew, which
372ab 20 69 73 20 61 6c 77 61 79 73 20 61 20 62 69 6e   is always a bin
372ac 61 72 79 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20  ary.** operator 
372ad 6e 6f 64 65 2c 20 69 6e 74 6f 20 74 68 65 20 65  node, into the e
372ae 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 62  xpression tree b
372af 61 73 65 64 20 6f 6e 20 74 68 65 20 72 65 6c 61  ased on the rela
372b0 74 69 76 65 20 70 72 65 63 65 64 65 6e 63 65 0a  tive precedence.
372b1 2a 2a 20 6f 66 20 70 4e 65 77 20 61 6e 64 20 74  ** of pNew and t
372b2 68 65 20 65 78 69 73 74 69 6e 67 20 6e 6f 64 65  he existing node
372b3 73 20 6f 66 20 74 68 65 20 74 72 65 65 2e 20 54  s of the tree. T
372b4 68 69 73 20 6d 61 79 20 72 65 73 75 6c 74 20 69  his may result i
372b5 6e 20 74 68 65 20 68 65 61 64 0a 2a 2a 20 6f 66  n the head.** of
372b6 20 74 68 65 20 74 72 65 65 20 63 68 61 6e 67 69   the tree changi
372b7 6e 67 2c 20 69 6e 20 77 68 69 63 68 20 63 61 73  ng, in which cas
372b8 65 20 2a 70 70 48 65 61 64 20 69 73 20 73 65 74  e *ppHead is set
372b9 20 74 6f 20 74 68 65 20 6e 65 77 20 72 6f 6f 74   to the new root
372ba 20 6e 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   node..*/.static
372bb 20 76 6f 69 64 20 69 6e 73 65 72 74 42 69 6e 61   void insertBina
372bc 72 79 4f 70 65 72 61 74 6f 72 28 0a 20 20 46 74  ryOperator(.  Ft
372bd 73 33 45 78 70 72 20 2a 2a 70 70 48 65 61 64 2c  s3Expr **ppHead,
372be 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
372bf 72 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 6e 6f  r to the root no
372c0 64 65 20 6f 66 20 61 20 74 72 65 65 20 2a 2f 0a  de of a tree */.
372c1 20 20 46 74 73 33 45 78 70 72 20 2a 70 50 72 65    Fts3Expr *pPre
372c2 76 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f  v,         /* No
372c3 64 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79  de most recently
372c4 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74   inserted into t
372c5 68 65 20 74 72 65 65 20 2a 2f 0a 20 20 46 74 73  he tree */.  Fts
372c6 33 45 78 70 72 20 2a 70 4e 65 77 20 20 20 20 20  3Expr *pNew     
372c7 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 62 69 6e        /* New bin
372c8 61 72 79 20 6e 6f 64 65 20 74 6f 20 69 6e 73 65  ary node to inse
372c9 72 74 20 69 6e 74 6f 20 65 78 70 72 65 73 73 69  rt into expressi
372ca 6f 6e 20 74 72 65 65 20 2a 2f 0a 29 7b 0a 20 20  on tree */.){.  
372cb 46 74 73 33 45 78 70 72 20 2a 70 53 70 6c 69 74  Fts3Expr *pSplit
372cc 20 3d 20 70 50 72 65 76 3b 0a 20 20 77 68 69 6c   = pPrev;.  whil
372cd 65 28 20 70 53 70 6c 69 74 2d 3e 70 50 61 72 65  e( pSplit->pPare
372ce 6e 74 20 26 26 20 6f 70 50 72 65 63 65 64 65 6e  nt && opPreceden
372cf 63 65 28 70 53 70 6c 69 74 2d 3e 70 50 61 72 65  ce(pSplit->pPare
372d0 6e 74 29 3c 3d 6f 70 50 72 65 63 65 64 65 6e 63  nt)<=opPrecedenc
372d1 65 28 70 4e 65 77 29 20 29 7b 0a 20 20 20 20 70  e(pNew) ){.    p
372d2 53 70 6c 69 74 20 3d 20 70 53 70 6c 69 74 2d 3e  Split = pSplit->
372d3 70 50 61 72 65 6e 74 3b 0a 20 20 7d 0a 0a 20 20  pParent;.  }..  
372d4 69 66 28 20 70 53 70 6c 69 74 2d 3e 70 50 61 72  if( pSplit->pPar
372d5 65 6e 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ent ){.    asser
372d6 74 28 20 70 53 70 6c 69 74 2d 3e 70 50 61 72 65  t( pSplit->pPare
372d7 6e 74 2d 3e 70 52 69 67 68 74 3d 3d 70 53 70 6c  nt->pRight==pSpl
372d8 69 74 20 29 3b 0a 20 20 20 20 70 53 70 6c 69 74  it );.    pSplit
372d9 2d 3e 70 50 61 72 65 6e 74 2d 3e 70 52 69 67 68  ->pParent->pRigh
372da 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 70 4e  t = pNew;.    pN
372db 65 77 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 53  ew->pParent = pS
372dc 70 6c 69 74 2d 3e 70 50 61 72 65 6e 74 3b 0a 20  plit->pParent;. 
372dd 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 48   }else{.    *ppH
372de 65 61 64 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a  ead = pNew;.  }.
372df 20 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d 20    pNew->pLeft = 
372e0 70 53 70 6c 69 74 3b 0a 20 20 70 53 70 6c 69 74  pSplit;.  pSplit
372e1 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 4e 65 77  ->pParent = pNew
372e2 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65  ;.}../*.** Parse
372e3 20 74 68 65 20 66 74 73 33 20 71 75 65 72 79 20   the fts3 query 
372e4 65 78 70 72 65 73 73 69 6f 6e 20 66 6f 75 6e 64  expression found
372e5 20 69 6e 20 62 75 66 66 65 72 20 7a 2c 20 6c 65   in buffer z, le
372e6 6e 67 74 68 20 6e 2e 20 54 68 69 73 20 66 75 6e  ngth n. This fun
372e7 63 74 69 6f 6e 0a 2a 2a 20 72 65 74 75 72 6e 73  ction.** returns
372e8 20 65 69 74 68 65 72 20 77 68 65 6e 20 74 68 65   either when the
372e9 20 65 6e 64 20 6f 66 20 74 68 65 20 62 75 66 66   end of the buff
372ea 65 72 20 69 73 20 72 65 61 63 68 65 64 20 6f 72  er is reached or
372eb 20 61 6e 20 75 6e 6d 61 74 63 68 65 64 20 0a 2a   an unmatched .*
372ec 2a 20 63 6c 6f 73 69 6e 67 20 62 72 61 63 6b 65  * closing bracke
372ed 74 20 2d 20 27 29 27 20 2d 20 69 73 20 65 6e 63  t - ')' - is enc
372ee 6f 75 6e 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20  ountered..**.** 
372ef 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 53  If successful, S
372f0 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
372f1 72 6e 65 64 2c 20 2a 70 70 45 78 70 72 20 69 73  rned, *ppExpr is
372f2 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f   set to point to
372f3 20 74 68 65 0a 2a 2a 20 70 61 72 73 65 64 20 66   the.** parsed f
372f4 6f 72 6d 20 6f 66 20 74 68 65 20 65 78 70 72 65  orm of the expre
372f5 73 73 69 6f 6e 20 61 6e 64 20 2a 70 6e 43 6f 6e  ssion and *pnCon
372f6 73 75 6d 65 64 20 69 73 20 73 65 74 20 74 6f 20  sumed is set to 
372f7 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a  the number of.**
372f8 20 62 79 74 65 73 20 72 65 61 64 20 66 72 6f 6d   bytes read from
372f9 20 62 75 66 66 65 72 20 7a 2e 20 4f 74 68 65 72   buffer z. Other
372fa 77 69 73 65 2c 20 2a 70 70 45 78 70 72 20 69 73  wise, *ppExpr is
372fb 20 73 65 74 20 74 6f 20 30 20 61 6e 64 20 53 51   set to 0 and SQ
372fc 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 28 6f  LITE_NOMEM.** (o
372fd 75 74 20 6f 66 20 6d 65 6d 6f 72 79 20 65 72 72  ut of memory err
372fe 6f 72 29 20 6f 72 20 53 51 4c 49 54 45 5f 45 52  or) or SQLITE_ER
372ff 52 4f 52 20 28 70 61 72 73 65 20 65 72 72 6f 72  ROR (parse error
37300 29 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  ) is returned..*
37301 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
37302 33 45 78 70 72 50 61 72 73 65 28 0a 20 20 50 61  3ExprParse(.  Pa
37303 72 73 65 43 6f 6e 74 65 78 74 20 2a 70 50 61 72  rseContext *pPar
37304 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  se,             
37305 20 20 20 20 20 20 2f 2a 20 66 74 73 33 20 71 75        /* fts3 qu
37306 65 72 79 20 70 61 72 73 65 20 63 6f 6e 74 65 78  ery parse contex
37307 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  t */.  const cha
37308 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 20 20 20  r *z, int n,    
37309 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3730a 2a 20 54 65 78 74 20 6f 66 20 4d 41 54 43 48 20  * Text of MATCH 
3730b 71 75 65 72 79 20 2a 2f 0a 20 20 46 74 73 33 45  query */.  Fts3E
3730c 78 70 72 20 2a 2a 70 70 45 78 70 72 2c 20 20 20  xpr **ppExpr,   
3730d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3730e 20 20 20 2f 2a 20 4f 55 54 3a 20 50 61 72 73 65     /* OUT: Parse
3730f 64 20 71 75 65 72 79 20 73 74 72 75 63 74 75 72  d query structur
37310 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 6f  e */.  int *pnCo
37311 6e 73 75 6d 65 64 20 20 20 20 20 20 20 20 20 20  nsumed          
37312 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
37313 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66  * OUT: Number of
37314 20 62 79 74 65 73 20 63 6f 6e 73 75 6d 65 64 20   bytes consumed 
37315 2a 2f 0a 29 7b 0a 20 20 46 74 73 33 45 78 70 72  */.){.  Fts3Expr
37316 20 2a 70 52 65 74 20 3d 20 30 3b 0a 20 20 46 74   *pRet = 0;.  Ft
37317 73 33 45 78 70 72 20 2a 70 50 72 65 76 20 3d 20  s3Expr *pPrev = 
37318 30 3b 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70  0;.  Fts3Expr *p
37319 4e 6f 74 42 72 61 6e 63 68 20 3d 20 30 3b 20 20  NotBranch = 0;  
3731a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3731b 4f 6e 6c 79 20 75 73 65 64 20 69 6e 20 6c 65 67  Only used in leg
3731c 61 63 79 20 70 61 72 73 65 20 6d 6f 64 65 20 2a  acy parse mode *
3731d 2f 0a 20 20 69 6e 74 20 6e 49 6e 20 3d 20 6e 3b  /.  int nIn = n;
3731e 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
3731f 49 6e 20 3d 20 7a 3b 0a 20 20 69 6e 74 20 72 63  In = z;.  int rc
37320 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
37321 69 6e 74 20 69 73 52 65 71 75 69 72 65 50 68 72  int isRequirePhr
37322 61 73 65 20 3d 20 31 3b 0a 0a 20 20 77 68 69 6c  ase = 1;..  whil
37323 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  e( rc==SQLITE_OK
37324 20 29 7b 0a 20 20 20 20 46 74 73 33 45 78 70 72   ){.    Fts3Expr
37325 20 2a 70 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74   *p = 0;.    int
37326 20 6e 42 79 74 65 20 3d 20 30 3b 0a 20 20 20 20   nByte = 0;.    
37327 72 63 20 3d 20 67 65 74 4e 65 78 74 4e 6f 64 65  rc = getNextNode
37328 28 70 50 61 72 73 65 2c 20 7a 49 6e 2c 20 6e 49  (pParse, zIn, nI
37329 6e 2c 20 26 70 2c 20 26 6e 42 79 74 65 29 3b 0a  n, &p, &nByte);.
3732a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
3732b 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69  TE_OK ){.      i
3732c 6e 74 20 69 73 50 68 72 61 73 65 3b 0a 0a 20 20  nt isPhrase;..  
3732d 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33      if( !sqlite3
3732e 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72  _fts3_enable_par
3732f 65 6e 74 68 65 73 65 73 20 0a 20 20 20 20 20 20  entheses .      
37330 20 26 26 20 70 2d 3e 65 54 79 70 65 3d 3d 46 54   && p->eType==FT
37331 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 26 26  SQUERY_PHRASE &&
37332 20 70 2d 3e 70 50 68 72 61 73 65 2d 3e 69 73 4e   p->pPhrase->isN
37333 6f 74 20 0a 20 20 20 20 20 20 29 7b 0a 20 20 20  ot .      ){.   
37334 20 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 61       /* Create a
37335 6e 20 69 6d 70 6c 69 63 69 74 20 4e 4f 54 20 6f  n implicit NOT o
37336 70 65 72 61 74 6f 72 2e 20 2a 2f 0a 20 20 20 20  perator. */.    
37337 20 20 20 20 46 74 73 33 45 78 70 72 20 2a 70 4e      Fts3Expr *pN
37338 6f 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  ot = sqlite3_mal
37339 6c 6f 63 28 73 69 7a 65 6f 66 28 46 74 73 33 45  loc(sizeof(Fts3E
3733a 78 70 72 29 29 3b 0a 20 20 20 20 20 20 20 20 69  xpr));.        i
3733b 66 28 20 21 70 4e 6f 74 20 29 7b 0a 20 20 20 20  f( !pNot ){.    
3733c 20 20 20 20 20 20 73 71 6c 69 74 65 33 46 74 73        sqlite3Fts
3733d 33 45 78 70 72 46 72 65 65 28 70 29 3b 0a 20 20  3ExprFree(p);.  
3733e 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
3733f 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
37340 20 20 20 20 20 67 6f 74 6f 20 65 78 70 72 70 61       goto exprpa
37341 72 73 65 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20  rse_out;.       
37342 20 7d 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65   }.        memse
37343 74 28 70 4e 6f 74 2c 20 30 2c 20 73 69 7a 65 6f  t(pNot, 0, sizeo
37344 66 28 46 74 73 33 45 78 70 72 29 29 3b 0a 20 20  f(Fts3Expr));.  
37345 20 20 20 20 20 20 70 4e 6f 74 2d 3e 65 54 79 70        pNot->eTyp
37346 65 20 3d 20 46 54 53 51 55 45 52 59 5f 4e 4f 54  e = FTSQUERY_NOT
37347 3b 0a 20 20 20 20 20 20 20 20 70 4e 6f 74 2d 3e  ;.        pNot->
37348 70 52 69 67 68 74 20 3d 20 70 3b 0a 20 20 20 20  pRight = p;.    
37349 20 20 20 20 69 66 28 20 70 4e 6f 74 42 72 61 6e      if( pNotBran
3734a 63 68 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ch ){.          
3734b 70 4e 6f 74 2d 3e 70 4c 65 66 74 20 3d 20 70 4e  pNot->pLeft = pN
3734c 6f 74 42 72 61 6e 63 68 3b 0a 20 20 20 20 20 20  otBranch;.      
3734d 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4e 6f 74    }.        pNot
3734e 42 72 61 6e 63 68 20 3d 20 70 4e 6f 74 3b 0a 20  Branch = pNot;. 
3734f 20 20 20 20 20 20 20 70 20 3d 20 70 50 72 65 76         p = pPrev
37350 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
37351 20 20 20 20 20 20 20 69 6e 74 20 65 54 79 70 65         int eType
37352 20 3d 20 70 2d 3e 65 54 79 70 65 3b 0a 20 20 20   = p->eType;.   
37353 20 20 20 20 20 61 73 73 65 72 74 28 20 65 54 79       assert( eTy
37354 70 65 21 3d 46 54 53 51 55 45 52 59 5f 50 48 52  pe!=FTSQUERY_PHR
37355 41 53 45 20 7c 7c 20 21 70 2d 3e 70 50 68 72 61  ASE || !p->pPhra
37356 73 65 2d 3e 69 73 4e 6f 74 20 29 3b 0a 20 20 20  se->isNot );.   
37357 20 20 20 20 20 69 73 50 68 72 61 73 65 20 3d 20       isPhrase = 
37358 28 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59  (eType==FTSQUERY
37359 5f 50 48 52 41 53 45 20 7c 7c 20 70 2d 3e 70 4c  _PHRASE || p->pL
3735a 65 66 74 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f  eft);..        /
3735b 2a 20 54 68 65 20 69 73 52 65 71 75 69 72 65 50  * The isRequireP
3735c 68 72 61 73 65 20 76 61 72 69 61 62 6c 65 20 69  hrase variable i
3735d 73 20 73 65 74 20 74 6f 20 74 72 75 65 20 69 66  s set to true if
3735e 20 61 20 70 68 72 61 73 65 20 6f 72 0a 20 20 20   a phrase or.   
3735f 20 20 20 20 20 2a 2a 20 61 6e 20 65 78 70 72 65       ** an expre
37360 73 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 65 64 20  ssion contained 
37361 69 6e 20 70 61 72 65 6e 74 68 65 73 69 73 20 69  in parenthesis i
37362 73 20 72 65 71 75 69 72 65 64 2e 20 49 66 20 61  s required. If a
37363 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 69 6e 61  .        ** bina
37364 72 79 20 6f 70 65 72 61 74 6f 72 20 28 41 4e 44  ry operator (AND
37365 2c 20 4f 52 2c 20 4e 4f 54 20 6f 72 20 4e 45 41  , OR, NOT or NEA
37366 52 29 20 69 73 20 65 6e 63 6f 75 6e 74 65 64 20  R) is encounted 
37367 77 68 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20  when.        ** 
37368 69 73 52 65 71 75 69 72 65 50 68 72 61 73 65 20  isRequirePhrase 
37369 69 73 20 73 65 74 2c 20 74 68 69 73 20 69 73 20  is set, this is 
3736a 61 20 73 79 6e 74 61 78 20 65 72 72 6f 72 2e 0a  a syntax error..
3736b 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
3736c 20 20 20 69 66 28 20 21 69 73 50 68 72 61 73 65     if( !isPhrase
3736d 20 26 26 20 69 73 52 65 71 75 69 72 65 50 68 72   && isRequirePhr
3736e 61 73 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ase ){.         
3736f 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72   sqlite3Fts3Expr
37370 46 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 20  Free(p);.       
37371 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
37372 52 52 4f 52 3b 0a 20 20 20 20 20 20 20 20 20 20  RROR;.          
37373 67 6f 74 6f 20 65 78 70 72 70 61 72 73 65 5f 6f  goto exprparse_o
37374 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ut;.        }.  
37375 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 73 50  .        if( isP
37376 68 72 61 73 65 20 26 26 20 21 69 73 52 65 71 75  hrase && !isRequ
37377 69 72 65 50 68 72 61 73 65 20 29 7b 0a 20 20 20  irePhrase ){.   
37378 20 20 20 20 20 20 20 2f 2a 20 49 6e 73 65 72 74         /* Insert
37379 20 61 6e 20 69 6d 70 6c 69 63 69 74 20 41 4e 44   an implicit AND
3737a 20 6f 70 65 72 61 74 6f 72 2e 20 2a 2f 0a 20 20   operator. */.  
3737b 20 20 20 20 20 20 20 20 46 74 73 33 45 78 70 72          Fts3Expr
3737c 20 2a 70 41 6e 64 3b 0a 20 20 20 20 20 20 20 20   *pAnd;.        
3737d 20 20 61 73 73 65 72 74 28 20 70 52 65 74 20 26    assert( pRet &
3737e 26 20 70 50 72 65 76 20 29 3b 0a 20 20 20 20 20  & pPrev );.     
3737f 20 20 20 20 20 70 41 6e 64 20 3d 20 73 71 6c 69       pAnd = sqli
37380 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f  te3_malloc(sizeo
37381 66 28 46 74 73 33 45 78 70 72 29 29 3b 0a 20 20  f(Fts3Expr));.  
37382 20 20 20 20 20 20 20 20 69 66 28 20 21 70 41 6e          if( !pAn
37383 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  d ){.           
37384 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72   sqlite3Fts3Expr
37385 46 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 20  Free(p);.       
37386 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
37387 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20  _NOMEM;.        
37388 20 20 20 20 67 6f 74 6f 20 65 78 70 72 70 61 72      goto exprpar
37389 73 65 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20  se_out;.        
3738a 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 6d 65    }.          me
3738b 6d 73 65 74 28 70 41 6e 64 2c 20 30 2c 20 73 69  mset(pAnd, 0, si
3738c 7a 65 6f 66 28 46 74 73 33 45 78 70 72 29 29 3b  zeof(Fts3Expr));
3738d 0a 20 20 20 20 20 20 20 20 20 20 70 41 6e 64 2d  .          pAnd-
3738e 3e 65 54 79 70 65 20 3d 20 46 54 53 51 55 45 52  >eType = FTSQUER
3738f 59 5f 41 4e 44 3b 0a 20 20 20 20 20 20 20 20 20  Y_AND;.         
37390 20 69 6e 73 65 72 74 42 69 6e 61 72 79 4f 70 65   insertBinaryOpe
37391 72 61 74 6f 72 28 26 70 52 65 74 2c 20 70 50 72  rator(&pRet, pPr
37392 65 76 2c 20 70 41 6e 64 29 3b 0a 20 20 20 20 20  ev, pAnd);.     
37393 20 20 20 20 20 70 50 72 65 76 20 3d 20 70 41 6e       pPrev = pAn
37394 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20  d;.        }..  
37395 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 74 65        /* This te
37396 73 74 20 63 61 74 63 68 65 73 20 61 74 74 65 6d  st catches attem
37397 70 74 73 20 74 6f 20 6d 61 6b 65 20 65 69 74 68  pts to make eith
37398 65 72 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 20  er operand of a 
37399 4e 45 41 52 0a 20 20 20 20 20 20 20 20 2a 2a 20  NEAR.        ** 
3739a 6f 70 65 72 61 74 6f 72 20 73 6f 6d 65 74 68 69  operator somethi
3739b 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 61 20  ng other than a 
3739c 70 68 72 61 73 65 2e 20 46 6f 72 20 65 78 61 6d  phrase. For exam
3739d 70 6c 65 2c 20 65 69 74 68 65 72 20 6f 66 0a 20  ple, either of. 
3739e 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 66 6f         ** the fo
3739f 6c 6c 6f 77 69 6e 67 3a 0a 20 20 20 20 20 20 20  llowing:.       
373a0 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 20   **.        **  
373a1 20 20 28 62 72 61 63 6b 65 74 65 64 20 65 78 70    (bracketed exp
373a2 72 65 73 73 69 6f 6e 29 20 4e 45 41 52 20 70 68  ression) NEAR ph
373a3 72 61 73 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  rase.        ** 
373a4 20 20 20 70 68 72 61 73 65 20 4e 45 41 52 20 28     phrase NEAR (
373a5 62 72 61 63 6b 65 74 65 64 20 65 78 70 72 65 73  bracketed expres
373a6 73 69 6f 6e 29 0a 20 20 20 20 20 20 20 20 2a 2a  sion).        **
373a7 0a 20 20 20 20 20 20 20 20 2a 2a 20 52 65 74 75  .        ** Retu
373a8 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 6e 20 65  rn an error in e
373a9 69 74 68 65 72 20 63 61 73 65 2e 0a 20 20 20 20  ither case..    
373aa 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69      */.        i
373ab 66 28 20 70 50 72 65 76 20 26 26 20 28 0a 20 20  f( pPrev && (.  
373ac 20 20 20 20 20 20 20 20 20 20 28 65 54 79 70 65            (eType
373ad 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45 41 52 20  ==FTSQUERY_NEAR 
373ae 26 26 20 21 69 73 50 68 72 61 73 65 20 26 26 20  && !isPhrase && 
373af 70 50 72 65 76 2d 3e 65 54 79 70 65 21 3d 46 54  pPrev->eType!=FT
373b0 53 51 55 45 52 59 5f 50 48 52 41 53 45 29 0a 20  SQUERY_PHRASE). 
373b1 20 20 20 20 20 20 20 20 7c 7c 20 28 65 54 79 70          || (eTyp
373b2 65 21 3d 46 54 53 51 55 45 52 59 5f 50 48 52 41  e!=FTSQUERY_PHRA
373b3 53 45 20 26 26 20 69 73 50 68 72 61 73 65 20 26  SE && isPhrase &
373b4 26 20 70 50 72 65 76 2d 3e 65 54 79 70 65 3d 3d  & pPrev->eType==
373b5 46 54 53 51 55 45 52 59 5f 4e 45 41 52 29 0a 20  FTSQUERY_NEAR). 
373b6 20 20 20 20 20 20 20 29 29 7b 0a 20 20 20 20 20         )){.     
373b7 20 20 20 20 20 73 71 6c 69 74 65 33 46 74 73 33       sqlite3Fts3
373b8 45 78 70 72 46 72 65 65 28 70 29 3b 0a 20 20 20  ExprFree(p);.   
373b9 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
373ba 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20  TE_ERROR;.      
373bb 20 20 20 20 67 6f 74 6f 20 65 78 70 72 70 61 72      goto exprpar
373bc 73 65 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20  se_out;.        
373bd 7d 0a 20 20 0a 20 20 20 20 20 20 20 20 69 66 28  }.  .        if(
373be 20 69 73 50 68 72 61 73 65 20 29 7b 0a 20 20 20   isPhrase ){.   
373bf 20 20 20 20 20 20 20 69 66 28 20 70 52 65 74 20         if( pRet 
373c0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61  ){.            a
373c1 73 73 65 72 74 28 20 70 50 72 65 76 20 26 26 20  ssert( pPrev && 
373c2 70 50 72 65 76 2d 3e 70 4c 65 66 74 20 26 26 20  pPrev->pLeft && 
373c3 70 50 72 65 76 2d 3e 70 52 69 67 68 74 3d 3d 30  pPrev->pRight==0
373c4 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   );.            
373c5 70 50 72 65 76 2d 3e 70 52 69 67 68 74 20 3d 20  pPrev->pRight = 
373c6 70 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  p;.            p
373c7 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 50 72 65  ->pParent = pPre
373c8 76 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  v;.          }el
373c9 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
373ca 70 52 65 74 20 3d 20 70 3b 0a 20 20 20 20 20 20  pRet = p;.      
373cb 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65      }.        }e
373cc 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 69  lse{.          i
373cd 6e 73 65 72 74 42 69 6e 61 72 79 4f 70 65 72 61  nsertBinaryOpera
373ce 74 6f 72 28 26 70 52 65 74 2c 20 70 50 72 65 76  tor(&pRet, pPrev
373cf 2c 20 70 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  , p);.        }.
373d0 20 20 20 20 20 20 20 20 69 73 52 65 71 75 69 72          isRequir
373d1 65 50 68 72 61 73 65 20 3d 20 21 69 73 50 68 72  ePhrase = !isPhr
373d2 61 73 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ase;.      }.   
373d3 20 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 65     assert( nByte
373d4 3e 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  >0 );.    }.    
373d5 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49  assert( rc!=SQLI
373d6 54 45 5f 4f 4b 20 7c 7c 20 28 6e 42 79 74 65 3e  TE_OK || (nByte>
373d7 30 20 26 26 20 6e 42 79 74 65 3c 3d 6e 49 6e 29  0 && nByte<=nIn)
373d8 20 29 3b 0a 20 20 20 20 6e 49 6e 20 2d 3d 20 6e   );.    nIn -= n
373d9 42 79 74 65 3b 0a 20 20 20 20 7a 49 6e 20 2b 3d  Byte;.    zIn +=
373da 20 6e 42 79 74 65 3b 0a 20 20 20 20 70 50 72 65   nByte;.    pPre
373db 76 20 3d 20 70 3b 0a 20 20 7d 0a 0a 20 20 69 66  v = p;.  }..  if
373dc 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc==SQLITE_DON
373dd 45 20 26 26 20 70 52 65 74 20 26 26 20 69 73 52  E && pRet && isR
373de 65 71 75 69 72 65 50 68 72 61 73 65 20 29 7b 0a  equirePhrase ){.
373df 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
373e0 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 69 66  ERROR;.  }..  if
373e1 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc==SQLITE_DON
373e2 45 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  E ){.    rc = SQ
373e3 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 69 66 28  LITE_OK;.    if(
373e4 20 21 73 71 6c 69 74 65 33 5f 66 74 73 33 5f 65   !sqlite3_fts3_e
373e5 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68 65 73 65  nable_parenthese
373e6 73 20 26 26 20 70 4e 6f 74 42 72 61 6e 63 68 20  s && pNotBranch 
373e7 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21 70 52  ){.      if( !pR
373e8 65 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  et ){.        rc
373e9 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
373ea 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
373eb 20 20 20 20 20 20 46 74 73 33 45 78 70 72 20 2a        Fts3Expr *
373ec 70 49 74 65 72 20 3d 20 70 4e 6f 74 42 72 61 6e  pIter = pNotBran
373ed 63 68 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c  ch;.        whil
373ee 65 28 20 70 49 74 65 72 2d 3e 70 4c 65 66 74 20  e( pIter->pLeft 
373ef 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 49 74  ){.          pIt
373f0 65 72 20 3d 20 70 49 74 65 72 2d 3e 70 4c 65 66  er = pIter->pLef
373f1 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  t;.        }.   
373f2 20 20 20 20 20 70 49 74 65 72 2d 3e 70 4c 65 66       pIter->pLef
373f3 74 20 3d 20 70 52 65 74 3b 0a 20 20 20 20 20 20  t = pRet;.      
373f4 20 20 70 52 65 74 20 3d 20 70 4e 6f 74 42 72 61    pRet = pNotBra
373f5 6e 63 68 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  nch;.      }.   
373f6 20 7d 0a 20 20 7d 0a 20 20 2a 70 6e 43 6f 6e 73   }.  }.  *pnCons
373f7 75 6d 65 64 20 3d 20 6e 20 2d 20 6e 49 6e 3b 0a  umed = n - nIn;.
373f8 0a 65 78 70 72 70 61 72 73 65 5f 6f 75 74 3a 0a  .exprparse_out:.
373f9 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
373fa 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  _OK ){.    sqlit
373fb 65 33 46 74 73 33 45 78 70 72 46 72 65 65 28 70  e3Fts3ExprFree(p
373fc 52 65 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Ret);.    sqlite
373fd 33 46 74 73 33 45 78 70 72 46 72 65 65 28 70 4e  3Fts3ExprFree(pN
373fe 6f 74 42 72 61 6e 63 68 29 3b 0a 20 20 20 20 70  otBranch);.    p
373ff 52 65 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 2a  Ret = 0;.  }.  *
37400 70 70 45 78 70 72 20 3d 20 70 52 65 74 3b 0a 20  ppExpr = pRet;. 
37401 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
37402 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 73 20  *.** Parameters 
37403 7a 20 61 6e 64 20 6e 20 63 6f 6e 74 61 69 6e 20  z and n contain 
37404 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 64  a pointer to and
37405 20 6c 65 6e 67 74 68 20 6f 66 20 61 20 62 75 66   length of a buf
37406 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 0a 2a  fer containing.*
37407 2a 20 61 6e 20 66 74 73 33 20 71 75 65 72 79 20  * an fts3 query 
37408 65 78 70 72 65 73 73 69 6f 6e 2c 20 72 65 73 70  expression, resp
37409 65 63 74 69 76 65 6c 79 2e 20 54 68 69 73 20 66  ectively. This f
3740a 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73  unction attempts
3740b 20 74 6f 20 70 61 72 73 65 20 74 68 65 0a 2a 2a   to parse the.**
3740c 20 71 75 65 72 79 20 65 78 70 72 65 73 73 69 6f   query expressio
3740d 6e 20 61 6e 64 20 63 72 65 61 74 65 20 61 20 74  n and create a t
3740e 72 65 65 20 6f 66 20 46 74 73 33 45 78 70 72 20  ree of Fts3Expr 
3740f 73 74 72 75 63 74 75 72 65 73 20 72 65 70 72 65  structures repre
37410 73 65 6e 74 69 6e 67 20 74 68 65 0a 2a 2a 20 70  senting the.** p
37411 61 72 73 65 64 20 65 78 70 72 65 73 73 69 6f 6e  arsed expression
37412 2e 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c  . If successful,
37413 20 2a 70 70 45 78 70 72 20 69 73 20 73 65 74 20   *ppExpr is set 
37414 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20  to point to the 
37415 68 65 61 64 0a 2a 2a 20 6f 66 20 74 68 65 20 70  head.** of the p
37416 61 72 73 65 64 20 65 78 70 72 65 73 73 69 6f 6e  arsed expression
37417 20 74 72 65 65 20 61 6e 64 20 53 51 4c 49 54 45   tree and SQLITE
37418 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e  _OK is returned.
37419 20 49 66 20 61 6e 20 65 72 72 6f 72 0a 2a 2a 20   If an error.** 
3741a 6f 63 63 75 72 73 2c 20 65 69 74 68 65 72 20 53  occurs, either S
3741b 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 28 6f 75 74  QLITE_NOMEM (out
3741c 2d 6f 66 2d 6d 65 6d 6f 72 79 20 65 72 72 6f 72  -of-memory error
3741d 29 20 6f 72 20 53 51 4c 49 54 45 5f 45 52 52 4f  ) or SQLITE_ERRO
3741e 52 20 28 70 61 72 73 65 0a 2a 2a 20 65 72 72 6f  R (parse.** erro
3741f 72 29 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  r) is returned a
37420 6e 64 20 2a 70 70 45 78 70 72 20 69 73 20 73 65  nd *ppExpr is se
37421 74 20 74 6f 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66  t to 0..**.** If
37422 20 70 61 72 61 6d 65 74 65 72 20 6e 20 69 73 20   parameter n is 
37423 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65  a negative numbe
37424 72 2c 20 74 68 65 6e 20 7a 20 69 73 20 61 73 73  r, then z is ass
37425 75 6d 65 64 20 74 6f 20 70 6f 69 6e 74 20 74 6f  umed to point to
37426 20 61 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e   a.** nul-termin
37427 61 74 65 64 20 73 74 72 69 6e 67 20 61 6e 64 20  ated string and 
37428 74 68 65 20 6c 65 6e 67 74 68 20 69 73 20 64 65  the length is de
37429 74 65 72 6d 69 6e 65 64 20 75 73 69 6e 67 20 73  termined using s
3742a 74 72 6c 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54  trlen()..**.** T
3742b 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74  he first paramet
3742c 65 72 2c 20 70 54 6f 6b 65 6e 69 7a 65 72 2c 20  er, pTokenizer, 
3742d 69 73 20 70 61 73 73 65 64 20 74 68 65 20 66 74  is passed the ft
3742e 73 33 20 74 6f 6b 65 6e 69 7a 65 72 20 6d 6f 64  s3 tokenizer mod
3742f 75 6c 65 20 74 6f 0a 2a 2a 20 75 73 65 20 74 6f  ule to.** use to
37430 20 6e 6f 72 6d 61 6c 69 7a 65 20 71 75 65 72 79   normalize query
37431 20 74 6f 6b 65 6e 73 20 77 68 69 6c 65 20 70 61   tokens while pa
37432 72 73 69 6e 67 20 74 68 65 20 65 78 70 72 65 73  rsing the expres
37433 73 69 6f 6e 2e 20 54 68 65 20 61 7a 43 6f 6c 5b  sion. The azCol[
37434 5d 0a 2a 2a 20 61 72 72 61 79 2c 20 77 68 69 63  ].** array, whic
37435 68 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20  h is assumed to 
37436 63 6f 6e 74 61 69 6e 20 6e 43 6f 6c 20 65 6e 74  contain nCol ent
37437 72 69 65 73 2c 20 73 68 6f 75 6c 64 20 63 6f 6e  ries, should con
37438 74 61 69 6e 20 74 68 65 20 6e 61 6d 65 73 0a 2a  tain the names.*
37439 2a 20 6f 66 20 65 61 63 68 20 63 6f 6c 75 6d 6e  * of each column
3743a 20 69 6e 20 74 68 65 20 74 61 72 67 65 74 20 66   in the target f
3743b 74 73 33 20 74 61 62 6c 65 2c 20 69 6e 20 6f 72  ts3 table, in or
3743c 64 65 72 20 66 72 6f 6d 20 6c 65 66 74 20 74 6f  der from left to
3743d 20 72 69 67 68 74 2e 20 0a 2a 2a 20 43 6f 6c 75   right. .** Colu
3743e 6d 6e 20 6e 61 6d 65 73 20 6d 75 73 74 20 62 65  mn names must be
3743f 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20   nul-terminated 
37440 73 74 72 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 54  strings..**.** T
37441 68 65 20 69 44 65 66 61 75 6c 74 43 6f 6c 20 70  he iDefaultCol p
37442 61 72 61 6d 65 74 65 72 20 73 68 6f 75 6c 64 20  arameter should 
37443 62 65 20 70 61 73 73 65 64 20 74 68 65 20 69 6e  be passed the in
37444 64 65 78 20 6f 66 20 74 68 65 20 74 61 62 6c 65  dex of the table
37445 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 74 68 61 74 20   column.** that 
37446 61 70 70 65 61 72 73 20 6f 6e 20 74 68 65 20 6c  appears on the l
37447 65 66 74 2d 68 61 6e 64 2d 73 69 64 65 20 6f 66  eft-hand-side of
37448 20 74 68 65 20 4d 41 54 43 48 20 6f 70 65 72 61   the MATCH opera
37449 74 6f 72 20 28 74 68 65 20 64 65 66 61 75 6c 74  tor (the default
3744a 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 74 6f 20 6d 61  .** column to ma
3744b 74 63 68 20 61 67 61 69 6e 73 74 20 66 6f 72 20  tch against for 
3744c 74 6f 6b 65 6e 73 20 66 6f 72 20 77 68 69 63 68  tokens for which
3744d 20 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 69   a column name i
3744e 73 20 6e 6f 74 20 65 78 70 6c 69 63 69 74 6c 79  s not explicitly
3744f 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 61 73  .** specified as
37450 20 70 61 72 74 20 6f 66 20 74 68 65 20 71 75 65   part of the que
37451 72 79 20 73 74 72 69 6e 67 29 2c 20 6f 72 20 2d  ry string), or -
37452 31 20 69 66 20 74 6f 6b 65 6e 73 20 6d 61 79 20  1 if tokens may 
37453 62 79 20 64 65 66 61 75 6c 74 0a 2a 2a 20 6d 61  by default.** ma
37454 74 63 68 20 61 6e 79 20 74 61 62 6c 65 20 63 6f  tch any table co
37455 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  lumn..*/.SQLITE_
37456 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
37457 74 65 33 46 74 73 33 45 78 70 72 50 61 72 73 65  te3Fts3ExprParse
37458 28 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  (.  sqlite3_toke
37459 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65  nizer *pTokenize
3745a 72 2c 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e  r,      /* Token
3745b 69 7a 65 72 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20  izer module */. 
3745c 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 2c 20 20   char **azCol,  
3745d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3745e 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66       /* Array of
3745f 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 6f   column names fo
37460 72 20 66 74 73 33 20 74 61 62 6c 65 20 2a 2f 0a  r fts3 table */.
37461 20 20 69 6e 74 20 6e 43 6f 6c 2c 20 20 20 20 20    int nCol,     
37462 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37463 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
37464 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 7a  of entries in az
37465 43 6f 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69  Col[] */.  int i
37466 44 65 66 61 75 6c 74 43 6f 6c 2c 20 20 20 20 20  DefaultCol,     
37467 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
37468 2a 20 44 65 66 61 75 6c 74 20 63 6f 6c 75 6d 6e  * Default column
37469 20 74 6f 20 71 75 65 72 79 20 2a 2f 0a 20 20 63   to query */.  c
3746a 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e  onst char *z, in
3746b 74 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20  t n,            
3746c 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 4d 41     /* Text of MA
3746d 54 43 48 20 71 75 65 72 79 20 2a 2f 0a 20 20 46  TCH query */.  F
3746e 74 73 33 45 78 70 72 20 2a 2a 70 70 45 78 70 72  ts3Expr **ppExpr
3746f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37470 20 20 20 2f 2a 20 4f 55 54 3a 20 50 61 72 73 65     /* OUT: Parse
37471 64 20 71 75 65 72 79 20 73 74 72 75 63 74 75 72  d query structur
37472 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 50  e */.){.  int nP
37473 61 72 73 65 64 3b 0a 20 20 69 6e 74 20 72 63 3b  arsed;.  int rc;
37474 0a 20 20 50 61 72 73 65 43 6f 6e 74 65 78 74 20  .  ParseContext 
37475 73 50 61 72 73 65 3b 0a 20 20 73 50 61 72 73 65  sParse;.  sParse
37476 2e 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 54  .pTokenizer = pT
37477 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 73 50 61 72  okenizer;.  sPar
37478 73 65 2e 61 7a 43 6f 6c 20 3d 20 28 63 6f 6e 73  se.azCol = (cons
37479 74 20 63 68 61 72 20 2a 2a 29 61 7a 43 6f 6c 3b  t char **)azCol;
3747a 0a 20 20 73 50 61 72 73 65 2e 6e 43 6f 6c 20 3d  .  sParse.nCol =
3747b 20 6e 43 6f 6c 3b 0a 20 20 73 50 61 72 73 65 2e   nCol;.  sParse.
3747c 69 44 65 66 61 75 6c 74 43 6f 6c 20 3d 20 69 44  iDefaultCol = iD
3747d 65 66 61 75 6c 74 43 6f 6c 3b 0a 20 20 73 50 61  efaultCol;.  sPa
3747e 72 73 65 2e 6e 4e 65 73 74 20 3d 20 30 3b 0a 20  rse.nNest = 0;. 
3747f 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20   if( z==0 ){.   
37480 20 2a 70 70 45 78 70 72 20 3d 20 30 3b 0a 20 20   *ppExpr = 0;.  
37481 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
37482 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 3c  OK;.  }.  if( n<
37483 30 20 29 7b 0a 20 20 20 20 6e 20 3d 20 73 74 72  0 ){.    n = str
37484 6c 65 6e 28 7a 29 3b 0a 20 20 7d 0a 20 20 72 63  len(z);.  }.  rc
37485 20 3d 20 66 74 73 33 45 78 70 72 50 61 72 73 65   = fts3ExprParse
37486 28 26 73 50 61 72 73 65 2c 20 7a 2c 20 6e 2c 20  (&sParse, z, n, 
37487 70 70 45 78 70 72 2c 20 26 6e 50 61 72 73 65 64  ppExpr, &nParsed
37488 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66  );..  /* Check f
37489 6f 72 20 6d 69 73 6d 61 74 63 68 65 64 20 70 61  or mismatched pa
3748a 72 65 6e 74 68 65 73 69 73 20 2a 2f 0a 20 20 69  renthesis */.  i
3748b 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
3748c 20 26 26 20 73 50 61 72 73 65 2e 6e 4e 65 73 74   && sParse.nNest
3748d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
3748e 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73  ITE_ERROR;.    s
3748f 71 6c 69 74 65 33 46 74 73 33 45 78 70 72 46 72  qlite3Fts3ExprFr
37490 65 65 28 2a 70 70 45 78 70 72 29 3b 0a 20 20 20  ee(*ppExpr);.   
37491 20 2a 70 70 45 78 70 72 20 3d 20 30 3b 0a 20 20   *ppExpr = 0;.  
37492 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  }..  return rc;.
37493 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 20  }../*.** Free a 
37494 70 61 72 73 65 64 20 66 74 73 33 20 71 75 65 72  parsed fts3 quer
37495 79 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6c 6c  y expression all
37496 6f 63 61 74 65 64 20 62 79 20 73 71 6c 69 74 65  ocated by sqlite
37497 33 46 74 73 33 45 78 70 72 50 61 72 73 65 28 29  3Fts3ExprParse()
37498 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
37499 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
3749a 46 74 73 33 45 78 70 72 46 72 65 65 28 46 74 73  Fts3ExprFree(Fts
3749b 33 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 66 28  3Expr *p){.  if(
3749c 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65   p ){.    sqlite
3749d 33 46 74 73 33 45 78 70 72 46 72 65 65 28 70 2d  3Fts3ExprFree(p-
3749e 3e 70 4c 65 66 74 29 3b 0a 20 20 20 20 73 71 6c  >pLeft);.    sql
3749f 69 74 65 33 46 74 73 33 45 78 70 72 46 72 65 65  ite3Fts3ExprFree
374a0 28 70 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 20  (p->pRight);.   
374a1 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29   sqlite3_free(p)
374a2 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  ;.  }.}../******
374a3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
374a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
374a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
374a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
374a7 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ******.*********
374a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
374a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
374aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
374ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
374ac 2a 2a 2a 2a 0a 2a 2a 20 45 76 65 72 79 74 68 69  ****.** Everythi
374ad 6e 67 20 61 66 74 65 72 20 74 68 69 73 20 70 6f  ng after this po
374ae 69 6e 74 20 69 73 20 6a 75 73 74 20 74 65 73 74  int is just test
374af 20 63 6f 64 65 2e 0a 2a 2f 0a 0a 23 69 66 64 65   code..*/..#ifde
374b0 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 0a 0a  f SQLITE_TEST...
374b1 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 74  /*.** Function t
374b2 6f 20 71 75 65 72 79 20 74 68 65 20 68 61 73 68  o query the hash
374b3 2d 74 61 62 6c 65 20 6f 66 20 74 6f 6b 65 6e 69  -table of tokeni
374b4 7a 65 72 73 20 28 73 65 65 20 52 45 41 44 4d 45  zers (see README
374b5 2e 74 6f 6b 65 6e 69 7a 65 72 73 29 2e 0a 2a 2f  .tokenizers)..*/
374b6 0a 73 74 61 74 69 63 20 69 6e 74 20 71 75 65 72  .static int quer
374b7 79 54 65 73 74 54 6f 6b 65 6e 69 7a 65 72 28 0a  yTestTokenizer(.
374b8 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a    sqlite3 *db, .
374b9 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
374ba 61 6d 65 2c 20 20 0a 20 20 63 6f 6e 73 74 20 73  ame,  .  const s
374bb 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
374bc 5f 6d 6f 64 75 6c 65 20 2a 2a 70 70 0a 29 7b 0a  _module **pp.){.
374bd 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69    int rc;.  sqli
374be 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b  te3_stmt *pStmt;
374bf 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 53  .  const char zS
374c0 71 6c 5b 5d 20 3d 20 22 53 45 4c 45 43 54 20 66  ql[] = "SELECT f
374c1 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 28 3f 29  ts3_tokenizer(?)
374c2 22 3b 0a 0a 20 20 2a 70 70 20 3d 20 30 3b 0a 20  ";..  *pp = 0;. 
374c3 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72   rc = sqlite3_pr
374c4 65 70 61 72 65 5f 76 32 28 64 62 2c 20 7a 53 71  epare_v2(db, zSq
374c5 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20 30  l, -1, &pStmt, 0
374c6 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
374c7 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65  ITE_OK ){.    re
374c8 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
374c9 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78  sqlite3_bind_tex
374ca 74 28 70 53 74 6d 74 2c 20 31 2c 20 7a 4e 61 6d  t(pStmt, 1, zNam
374cb 65 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54  e, -1, SQLITE_ST
374cc 41 54 49 43 29 3b 0a 20 20 69 66 28 20 53 51 4c  ATIC);.  if( SQL
374cd 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33  ITE_ROW==sqlite3
374ce 5f 73 74 65 70 28 70 53 74 6d 74 29 20 29 7b 0a  _step(pStmt) ){.
374cf 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f      if( sqlite3_
374d0 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 70 53 74 6d  column_type(pStm
374d1 74 2c 20 30 29 3d 3d 53 51 4c 49 54 45 5f 42 4c  t, 0)==SQLITE_BL
374d2 4f 42 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63  OB ){.      memc
374d3 70 79 28 70 70 2c 20 73 71 6c 69 74 65 33 5f 63  py(pp, sqlite3_c
374d4 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 70 53 74 6d 74  olumn_blob(pStmt
374d5 2c 20 30 29 2c 20 73 69 7a 65 6f 66 28 2a 70 70  , 0), sizeof(*pp
374d6 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  ));.    }.  }.. 
374d7 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f   return sqlite3_
374d8 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b  finalize(pStmt);
374d9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
374da 75 6e 63 74 69 6f 6e 20 69 73 20 70 61 72 74 20  unction is part 
374db 6f 66 20 74 68 65 20 74 65 73 74 20 69 6e 74 65  of the test inte
374dc 72 66 61 63 65 20 66 6f 72 20 74 68 65 20 71 75  rface for the qu
374dd 65 72 79 20 70 61 72 73 65 72 2e 20 49 74 0a 2a  ery parser. It.*
374de 2a 20 77 72 69 74 65 73 20 61 20 74 65 78 74 20  * writes a text 
374df 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f  representation o
374e0 66 20 74 68 65 20 71 75 65 72 79 20 65 78 70 72  f the query expr
374e1 65 73 73 69 6f 6e 20 70 45 78 70 72 20 69 6e 74  ession pExpr int
374e2 6f 20 74 68 65 0a 2a 2a 20 62 75 66 66 65 72 20  o the.** buffer 
374e3 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 61 72  pointed to by ar
374e4 67 75 6d 65 6e 74 20 7a 42 75 66 2e 20 49 74 20  gument zBuf. It 
374e5 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 20  is assumed that 
374e6 7a 42 75 66 20 69 73 20 6c 61 72 67 65 20 0a 2a  zBuf is large .*
374e7 2a 20 65 6e 6f 75 67 68 20 74 6f 20 73 74 6f 72  * enough to stor
374e8 65 20 74 68 65 20 72 65 71 75 69 72 65 64 20 74  e the required t
374e9 65 78 74 20 72 65 70 72 65 73 65 6e 74 61 74 69  ext representati
374ea 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  on..*/.static vo
374eb 69 64 20 65 78 70 72 54 6f 53 74 72 69 6e 67 28  id exprToString(
374ec 46 74 73 33 45 78 70 72 20 2a 70 45 78 70 72 2c  Fts3Expr *pExpr,
374ed 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20   char *zBuf){.  
374ee 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 65  switch( pExpr->e
374ef 54 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 65  Type ){.    case
374f0 20 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45   FTSQUERY_PHRASE
374f1 3a 20 7b 0a 20 20 20 20 20 20 46 74 73 33 50 68  : {.      Fts3Ph
374f2 72 61 73 65 20 2a 70 50 68 72 61 73 65 20 3d 20  rase *pPhrase = 
374f3 70 45 78 70 72 2d 3e 70 50 68 72 61 73 65 3b 0a  pExpr->pPhrase;.
374f4 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20        int i;.   
374f5 20 20 20 7a 42 75 66 20 2b 3d 20 73 70 72 69 6e     zBuf += sprin
374f6 74 66 28 7a 42 75 66 2c 20 22 50 48 52 41 53 45  tf(zBuf, "PHRASE
374f7 20 25 64 20 25 64 22 2c 20 70 50 68 72 61 73 65   %d %d", pPhrase
374f8 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 70 50 68 72 61  ->iColumn, pPhra
374f9 73 65 2d 3e 69 73 4e 6f 74 29 3b 0a 20 20 20 20  se->isNot);.    
374fa 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 68    for(i=0; i<pPh
374fb 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 20 69 2b  rase->nToken; i+
374fc 2b 29 7b 0a 20 20 20 20 20 20 20 20 7a 42 75 66  +){.        zBuf
374fd 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42 75 66   += sprintf(zBuf
374fe 2c 22 20 25 2e 2a 73 22 2c 70 50 68 72 61 73 65  ," %.*s",pPhrase
374ff 2d 3e 61 54 6f 6b 65 6e 5b 69 5d 2e 6e 2c 70 50  ->aToken[i].n,pP
37500 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 69 5d  hrase->aToken[i]
37501 2e 7a 29 3b 0a 20 20 20 20 20 20 20 20 7a 42 75  .z);.        zBu
37502 66 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42 75  f += sprintf(zBu
37503 66 2c 22 25 73 22 2c 20 28 70 50 68 72 61 73 65  f,"%s", (pPhrase
37504 2d 3e 61 54 6f 6b 65 6e 5b 69 5d 2e 69 73 50 72  ->aToken[i].isPr
37505 65 66 69 78 3f 22 2b 22 3a 22 22 29 29 3b 0a 20  efix?"+":""));. 
37506 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74       }.      ret
37507 75 72 6e 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  urn;.    }..    
37508 63 61 73 65 20 46 54 53 51 55 45 52 59 5f 4e 45  case FTSQUERY_NE
37509 41 52 3a 0a 20 20 20 20 20 20 7a 42 75 66 20 2b  AR:.      zBuf +
3750a 3d 20 73 70 72 69 6e 74 66 28 7a 42 75 66 2c 20  = sprintf(zBuf, 
3750b 22 4e 45 41 52 2f 25 64 20 22 2c 20 70 45 78 70  "NEAR/%d ", pExp
3750c 72 2d 3e 6e 4e 65 61 72 29 3b 0a 20 20 20 20 20  r->nNear);.     
3750d 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
3750e 20 46 54 53 51 55 45 52 59 5f 4e 4f 54 3a 0a 20   FTSQUERY_NOT:. 
3750f 20 20 20 20 20 7a 42 75 66 20 2b 3d 20 73 70 72       zBuf += spr
37510 69 6e 74 66 28 7a 42 75 66 2c 20 22 4e 4f 54 20  intf(zBuf, "NOT 
37511 22 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ");.      break;
37512 0a 20 20 20 20 63 61 73 65 20 46 54 53 51 55 45  .    case FTSQUE
37513 52 59 5f 41 4e 44 3a 0a 20 20 20 20 20 20 7a 42  RY_AND:.      zB
37514 75 66 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42  uf += sprintf(zB
37515 75 66 2c 20 22 41 4e 44 20 22 29 3b 0a 20 20 20  uf, "AND ");.   
37516 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
37517 73 65 20 46 54 53 51 55 45 52 59 5f 4f 52 3a 0a  se FTSQUERY_OR:.
37518 20 20 20 20 20 20 7a 42 75 66 20 2b 3d 20 73 70        zBuf += sp
37519 72 69 6e 74 66 28 7a 42 75 66 2c 20 22 4f 52 20  rintf(zBuf, "OR 
3751a 22 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ");.      break;
3751b 0a 20 20 7d 0a 0a 20 20 7a 42 75 66 20 2b 3d 20  .  }..  zBuf += 
3751c 73 70 72 69 6e 74 66 28 7a 42 75 66 2c 20 22 7b  sprintf(zBuf, "{
3751d 22 29 3b 0a 20 20 65 78 70 72 54 6f 53 74 72 69  ");.  exprToStri
3751e 6e 67 28 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c  ng(pExpr->pLeft,
3751f 20 7a 42 75 66 29 3b 0a 20 20 7a 42 75 66 20 2b   zBuf);.  zBuf +
37520 3d 20 73 74 72 6c 65 6e 28 7a 42 75 66 29 3b 0a  = strlen(zBuf);.
37521 20 20 7a 42 75 66 20 2b 3d 20 73 70 72 69 6e 74    zBuf += sprint
37522 66 28 7a 42 75 66 2c 20 22 7d 20 22 29 3b 0a 0a  f(zBuf, "} ");..
37523 20 20 7a 42 75 66 20 2b 3d 20 73 70 72 69 6e 74    zBuf += sprint
37524 66 28 7a 42 75 66 2c 20 22 7b 22 29 3b 0a 20 20  f(zBuf, "{");.  
37525 65 78 70 72 54 6f 53 74 72 69 6e 67 28 70 45 78  exprToString(pEx
37526 70 72 2d 3e 70 52 69 67 68 74 2c 20 7a 42 75 66  pr->pRight, zBuf
37527 29 3b 0a 20 20 7a 42 75 66 20 2b 3d 20 73 74 72  );.  zBuf += str
37528 6c 65 6e 28 7a 42 75 66 29 3b 0a 20 20 7a 42 75  len(zBuf);.  zBu
37529 66 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42 75  f += sprintf(zBu
3752a 66 2c 20 22 7d 22 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  f, "}");.}../*.*
3752b 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 69 6d  * This is the im
3752c 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
3752d 61 20 73 63 61 6c 61 72 20 53 51 4c 20 66 75 6e  a scalar SQL fun
3752e 63 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 74 65  ction used to te
3752f 73 74 20 74 68 65 20 0a 2a 2a 20 65 78 70 72 65  st the .** expre
37530 73 73 69 6f 6e 20 70 61 72 73 65 72 2e 20 49 74  ssion parser. It
37531 20 73 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65   should be calle
37532 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  d as follows:.**
37533 0a 2a 2a 20 20 20 66 74 73 33 5f 65 78 70 72 74  .**   fts3_exprt
37534 65 73 74 28 3c 74 6f 6b 65 6e 69 7a 65 72 3e 2c  est(<tokenizer>,
37535 20 3c 65 78 70 72 3e 2c 20 3c 63 6f 6c 75 6d 6e   <expr>, <column
37536 20 31 3e 2c 20 2e 2e 2e 29 3b 0a 2a 2a 0a 2a 2a   1>, ...);.**.**
37537 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   The first argum
37538 65 6e 74 2c 20 3c 74 6f 6b 65 6e 69 7a 65 72 3e  ent, <tokenizer>
37539 2c 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  , is the name of
3753a 20 74 68 65 20 66 74 73 33 20 74 6f 6b 65 6e 69   the fts3 tokeni
3753b 7a 65 72 20 75 73 65 64 0a 2a 2a 20 74 6f 20 70  zer used.** to p
3753c 61 72 73 65 20 74 68 65 20 71 75 65 72 79 20 65  arse the query e
3753d 78 70 72 65 73 73 69 6f 6e 20 28 73 65 65 20 52  xpression (see R
3753e 45 41 44 4d 45 2e 74 6f 6b 65 6e 69 7a 65 72 73  EADME.tokenizers
3753f 29 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72  ). The second ar
37540 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 74 68 65  gument.** is the
37541 20 71 75 65 72 79 20 65 78 70 72 65 73 73 69 6f   query expressio
37542 6e 20 74 6f 20 70 61 72 73 65 2e 20 45 61 63 68  n to parse. Each
37543 20 73 75 62 73 65 71 75 65 6e 74 20 61 72 67 75   subsequent argu
37544 6d 65 6e 74 20 69 73 20 74 68 65 20 6e 61 6d 65  ment is the name
37545 0a 2a 2a 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20  .** of a column 
37546 6f 66 20 74 68 65 20 66 74 73 33 20 74 61 62 6c  of the fts3 tabl
37547 65 20 74 68 61 74 20 74 68 65 20 71 75 65 72 79  e that the query
37548 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 61 79 20   expression may 
37549 72 65 66 65 72 20 74 6f 2e 0a 2a 2a 20 46 6f 72  refer to..** For
3754a 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20   example:.**.** 
3754b 20 20 53 45 4c 45 43 54 20 66 74 73 33 5f 65 78    SELECT fts3_ex
3754c 70 72 74 65 73 74 28 27 73 69 6d 70 6c 65 27 2c  prtest('simple',
3754d 20 27 42 69 6c 6c 20 63 6f 6c 32 3a 42 6c 6f 67   'Bill col2:Blog
3754e 67 73 27 2c 20 27 63 6f 6c 31 27 2c 20 27 63 6f  gs', 'col1', 'co
3754f 6c 32 27 29 3b 0a 2a 2f 0a 73 74 61 74 69 63 20  l2');.*/.static 
37550 76 6f 69 64 20 66 74 73 33 45 78 70 72 54 65 73  void fts3ExprTes
37551 74 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  t(.  sqlite3_con
37552 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
37553 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c   int argc,.  sql
37554 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
37555 76 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74  v.){.  sqlite3_t
37556 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
37557 63 6f 6e 73 74 20 2a 70 4d 6f 64 75 6c 65 20 3d  const *pModule =
37558 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f   0;.  sqlite3_to
37559 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69  kenizer *pTokeni
3755a 7a 65 72 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72  zer = 0;.  int r
3755b 63 3b 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f  c;.  char **azCo
3755c 6c 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 63  l = 0;.  const c
3755d 68 61 72 20 2a 7a 45 78 70 72 3b 0a 20 20 69 6e  har *zExpr;.  in
3755e 74 20 6e 45 78 70 72 3b 0a 20 20 69 6e 74 20 6e  t nExpr;.  int n
3755f 43 6f 6c 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 20  Col;.  int ii;. 
37560 20 46 74 73 33 45 78 70 72 20 2a 70 45 78 70 72   Fts3Expr *pExpr
37561 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
37562 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  = sqlite3_contex
37563 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74  t_db_handle(cont
37564 65 78 74 29 3b 0a 0a 20 20 69 66 28 20 61 72 67  ext);..  if( arg
37565 63 3c 33 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  c<3 ){.    sqlit
37566 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
37567 63 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 20  context, .      
37568 20 20 22 55 73 61 67 65 3a 20 66 74 73 33 5f 65    "Usage: fts3_e
37569 78 70 72 74 65 73 74 28 74 6f 6b 65 6e 69 7a 65  xprtest(tokenize
3756a 72 2c 20 65 78 70 72 2c 20 63 6f 6c 31 2c 20 2e  r, expr, col1, .
3756b 2e 2e 22 2c 20 2d 31 0a 20 20 20 20 29 3b 0a 20  ..", -1.    );. 
3756c 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a     return;.  }..
3756d 20 20 72 63 20 3d 20 71 75 65 72 79 54 65 73 74    rc = queryTest
3756e 54 6f 6b 65 6e 69 7a 65 72 28 64 62 2c 0a 20 20  Tokenizer(db,.  
3756f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37570 20 20 20 20 20 20 20 20 28 63 6f 6e 73 74 20 63          (const c
37571 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61  har *)sqlite3_va
37572 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
37573 29 2c 20 26 70 4d 6f 64 75 6c 65 29 3b 0a 20 20  ), &pModule);.  
37574 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e  if( rc==SQLITE_N
37575 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 73 71 6c 69  OMEM ){.    sqli
37576 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
37577 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b  _nomem(context);
37578 0a 20 20 20 20 67 6f 74 6f 20 65 78 70 72 74 65  .    goto exprte
37579 73 74 5f 6f 75 74 3b 0a 20 20 7d 65 6c 73 65 20  st_out;.  }else 
3757a 69 66 28 20 21 70 4d 6f 64 75 6c 65 20 29 7b 0a  if( !pModule ){.
3757b 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
3757c 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74  lt_error(context
3757d 2c 20 22 4e 6f 20 73 75 63 68 20 74 6f 6b 65 6e  , "No such token
3757e 69 7a 65 72 20 6d 6f 64 75 6c 65 22 2c 20 2d 31  izer module", -1
3757f 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 70 72  );.    goto expr
37580 74 65 73 74 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20  test_out;.  }.. 
37581 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78   rc = pModule->x
37582 43 72 65 61 74 65 28 30 2c 20 30 2c 20 26 70 54  Create(0, 0, &pT
37583 6f 6b 65 6e 69 7a 65 72 29 3b 0a 20 20 61 73 73  okenizer);.  ass
37584 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
37585 4e 4f 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c  NOMEM || rc==SQL
37586 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69 66 28 20  ITE_OK );.  if( 
37587 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
37588 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
37589 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d  result_error_nom
3758a 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  em(context);.   
3758b 20 67 6f 74 6f 20 65 78 70 72 74 65 73 74 5f 6f   goto exprtest_o
3758c 75 74 3b 0a 20 20 7d 0a 20 20 70 54 6f 6b 65 6e  ut;.  }.  pToken
3758d 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20  izer->pModule = 
3758e 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20 7a 45 78 70  pModule;..  zExp
3758f 72 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  r = (const char 
37590 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
37591 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20  text(argv[1]);. 
37592 20 6e 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33   nExpr = sqlite3
37593 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67  _value_bytes(arg
37594 76 5b 31 5d 29 3b 0a 20 20 6e 43 6f 6c 20 3d 20  v[1]);.  nCol = 
37595 61 72 67 63 2d 32 3b 0a 20 20 61 7a 43 6f 6c 20  argc-2;.  azCol 
37596 3d 20 28 63 68 61 72 20 2a 2a 29 73 71 6c 69 74  = (char **)sqlit
37597 65 33 5f 6d 61 6c 6c 6f 63 28 6e 43 6f 6c 2a 73  e3_malloc(nCol*s
37598 69 7a 65 6f 66 28 63 68 61 72 20 2a 29 29 3b 0a  izeof(char *));.
37599 20 20 69 66 28 20 21 61 7a 43 6f 6c 20 29 7b 0a    if( !azCol ){.
3759a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
3759b 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63  lt_error_nomem(c
3759c 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 67 6f 74  ontext);.    got
3759d 6f 20 65 78 70 72 74 65 73 74 5f 6f 75 74 3b 0a  o exprtest_out;.
3759e 20 20 7d 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20    }.  for(ii=0; 
3759f 69 69 3c 6e 43 6f 6c 3b 20 69 69 2b 2b 29 7b 0a  ii<nCol; ii++){.
375a0 20 20 20 20 61 7a 43 6f 6c 5b 69 69 5d 20 3d 20      azCol[ii] = 
375a1 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f  (char *)sqlite3_
375a2 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
375a3 69 69 2b 32 5d 29 3b 0a 20 20 7d 0a 0a 20 20 72  ii+2]);.  }..  r
375a4 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 45  c = sqlite3Fts3E
375a5 78 70 72 50 61 72 73 65 28 0a 20 20 20 20 20 20  xprParse(.      
375a6 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 61 7a 43 6f  pTokenizer, azCo
375a7 6c 2c 20 6e 43 6f 6c 2c 20 6e 43 6f 6c 2c 20 7a  l, nCol, nCol, z
375a8 45 78 70 72 2c 20 6e 45 78 70 72 2c 20 26 70 45  Expr, nExpr, &pE
375a9 78 70 72 0a 20 20 29 3b 0a 20 20 69 66 28 20 72  xpr.  );.  if( r
375aa 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
375ab 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  ){.    sqlite3_r
375ac 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65  esult_error_nome
375ad 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20  m(context);.    
375ae 67 6f 74 6f 20 65 78 70 72 74 65 73 74 5f 6f 75  goto exprtest_ou
375af 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72  t;.  }else if( r
375b0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
375b1 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b 34 30      char zBuf[40
375b2 39 36 5d 3b 0a 20 20 20 20 65 78 70 72 54 6f 53  96];.    exprToS
375b3 74 72 69 6e 67 28 70 45 78 70 72 2c 20 7a 42 75  tring(pExpr, zBu
375b4 66 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  f);.    sqlite3_
375b5 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
375b6 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53  ext, zBuf, -1, S
375b7 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
375b8 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74 73  ;.    sqlite3Fts
375b9 33 45 78 70 72 46 72 65 65 28 70 45 78 70 72 29  3ExprFree(pExpr)
375ba 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
375bb 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
375bc 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22 45 72  ror(context, "Er
375bd 72 6f 72 20 70 61 72 73 69 6e 67 20 65 78 70 72  ror parsing expr
375be 65 73 73 69 6f 6e 22 2c 20 2d 31 29 3b 0a 20 20  ession", -1);.  
375bf 7d 0a 0a 65 78 70 72 74 65 73 74 5f 6f 75 74 3a  }..exprtest_out:
375c0 0a 20 20 69 66 28 20 70 4d 6f 64 75 6c 65 20 26  .  if( pModule &
375c1 26 20 70 54 6f 6b 65 6e 69 7a 65 72 20 29 7b 0a  & pTokenizer ){.
375c2 20 20 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65      rc = pModule
375c3 2d 3e 78 44 65 73 74 72 6f 79 28 70 54 6f 6b 65  ->xDestroy(pToke
375c4 6e 69 7a 65 72 29 3b 0a 20 20 7d 0a 20 20 73 71  nizer);.  }.  sq
375c5 6c 69 74 65 33 5f 66 72 65 65 28 61 7a 43 6f 6c  lite3_free(azCol
375c6 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69  );.}../*.** Regi
375c7 73 74 65 72 20 74 68 65 20 71 75 65 72 79 20 65  ster the query e
375c8 78 70 72 65 73 73 69 6f 6e 20 70 61 72 73 65 72  xpression parser
375c9 20 74 65 73 74 20 66 75 6e 63 74 69 6f 6e 20 66   test function f
375ca 74 73 33 5f 65 78 70 72 74 65 73 74 28 29 20 0a  ts3_exprtest() .
375cb 2a 2a 20 77 69 74 68 20 64 61 74 61 62 61 73 65  ** with database
375cc 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 2e 20   connection db. 
375cd 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
375ce 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46  TE void sqlite3F
375cf 74 73 33 45 78 70 72 49 6e 69 74 54 65 73 74 49  ts3ExprInitTestI
375d0 6e 74 65 72 66 61 63 65 28 73 71 6c 69 74 65 33  nterface(sqlite3
375d1 2a 20 64 62 29 7b 0a 20 20 73 71 6c 69 74 65 33  * db){.  sqlite3
375d2 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
375d3 28 0a 20 20 20 20 20 20 64 62 2c 20 22 66 74 73  (.      db, "fts
375d4 33 5f 65 78 70 72 74 65 73 74 22 2c 20 2d 31 2c  3_exprtest", -1,
375d5 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c   SQLITE_UTF8, 0,
375d6 20 66 74 73 33 45 78 70 72 54 65 73 74 2c 20 30   fts3ExprTest, 0
375d7 2c 20 30 0a 20 20 29 3b 0a 7d 0a 0a 23 65 6e 64  , 0.  );.}..#end
375d8 69 66 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  if.#endif /* !de
375d9 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52  fined(SQLITE_COR
375da 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  E) || defined(SQ
375db 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
375dc 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ) */../*********
375dd 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73  ***** End of fts
375de 33 5f 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a  3_expr.c *******
375df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
375e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
375e1 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
375e2 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
375e3 20 66 74 73 33 5f 68 61 73 68 2e 63 20 2a 2a 2a   fts3_hash.c ***
375e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
375e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
375e6 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31  ****/./*.** 2001
375e7 20 53 65 70 74 65 6d 62 65 72 20 32 32 0a 2a 2a   September 22.**
375e8 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
375e9 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
375ea 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
375eb 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
375ec 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
375ed 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
375ee 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
375ef 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
375f0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
375f1 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
375f2 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
375f3 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
375f4 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
375f5 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
375f6 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
375f7 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
375f8 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
375f9 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
375fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
375fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
375fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
375fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
375fe 54 68 69 73 20 69 73 20 74 68 65 20 69 6d 70 6c  This is the impl
375ff 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 67 65  ementation of ge
37600 6e 65 72 69 63 20 68 61 73 68 2d 74 61 62 6c 65  neric hash-table
37601 73 20 75 73 65 64 20 69 6e 20 53 51 4c 69 74 65  s used in SQLite
37602 2e 0a 2a 2a 20 57 65 27 76 65 20 6d 6f 64 69 66  ..** We've modif
37603 69 65 64 20 69 74 20 73 6c 69 67 68 74 6c 79 20  ied it slightly 
37604 74 6f 20 73 65 72 76 65 20 61 73 20 61 20 73 74  to serve as a st
37605 61 6e 64 61 6c 6f 6e 65 20 68 61 73 68 20 74 61  andalone hash ta
37606 62 6c 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  ble.** implement
37607 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 66 75  ation for the fu
37608 6c 6c 2d 74 65 78 74 20 69 6e 64 65 78 69 6e 67  ll-text indexing
37609 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 0a 2f 2a 0a   module..*/../*.
3760a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74  ** The code in t
3760b 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79  his file is only
3760c 20 63 6f 6d 70 69 6c 65 64 20 69 66 3a 0a 2a 2a   compiled if:.**
3760d 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54  .**     * The FT
3760e 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69  S3 module is bei
3760f 6e 67 20 62 75 69 6c 74 20 61 73 20 61 6e 20 65  ng built as an e
37610 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20 20  xtension.**     
37611 20 20 28 69 6e 20 77 68 69 63 68 20 63 61 73 65    (in which case
37612 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 69 73 20   SQLITE_CORE is 
37613 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c 20 6f 72  not defined), or
37614 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65  .**.**     * The
37615 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20   FTS3 module is 
37616 62 65 69 6e 67 20 62 75 69 6c 74 20 69 6e 74 6f  being built into
37617 20 74 68 65 20 63 6f 72 65 20 6f 66 0a 2a 2a 20   the core of.** 
37618 20 20 20 20 20 20 53 51 4c 69 74 65 20 28 69 6e        SQLite (in
37619 20 77 68 69 63 68 20 63 61 73 65 20 53 51 4c 49   which case SQLI
3761a 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 20 69  TE_ENABLE_FTS3 i
3761b 73 20 64 65 66 69 6e 65 64 29 2e 0a 2a 2f 0a 23  s defined)..*/.#
3761c 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
3761d 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69  TE_CORE) || defi
3761e 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
3761f 45 5f 46 54 53 33 29 0a 0a 0a 0a 2f 2a 0a 2a 2a  E_FTS3)..../*.**
37620 20 4d 61 6c 6c 6f 63 20 61 6e 64 20 46 72 65 65   Malloc and Free
37621 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2f 0a 73 74   functions.*/.st
37622 61 74 69 63 20 76 6f 69 64 20 2a 66 74 73 33 48  atic void *fts3H
37623 61 73 68 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 29  ashMalloc(int n)
37624 7b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 73 71  {.  void *p = sq
37625 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 29 3b  lite3_malloc(n);
37626 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20  .  if( p ){.    
37627 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 6e 29 3b  memset(p, 0, n);
37628 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b  .  }.  return p;
37629 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  .}.static void f
3762a 74 73 33 48 61 73 68 46 72 65 65 28 76 6f 69 64  ts3HashFree(void
3762b 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f   *p){.  sqlite3_
3762c 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 20 54  free(p);.}../* T
3762d 75 72 6e 20 62 75 6c 6b 20 6d 65 6d 6f 72 79 20  urn bulk memory 
3762e 69 6e 74 6f 20 61 20 68 61 73 68 20 74 61 62 6c  into a hash tabl
3762f 65 20 6f 62 6a 65 63 74 20 62 79 20 69 6e 69 74  e object by init
37630 69 61 6c 69 7a 69 6e 67 20 74 68 65 0a 2a 2a 20  ializing the.** 
37631 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 48 61  fields of the Ha
37632 73 68 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  sh structure..**
37633 0a 2a 2a 20 22 70 4e 65 77 22 20 69 73 20 61 20  .** "pNew" is a 
37634 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 68  pointer to the h
37635 61 73 68 20 74 61 62 6c 65 20 74 68 61 74 20 69  ash table that i
37636 73 20 74 6f 20 62 65 20 69 6e 69 74 69 61 6c 69  s to be initiali
37637 7a 65 64 2e 0a 2a 2a 20 6b 65 79 43 6c 61 73 73  zed..** keyClass
37638 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 20 63   is one of the c
37639 6f 6e 73 74 61 6e 74 73 20 0a 2a 2a 20 46 54 53  onstants .** FTS
3763a 33 5f 48 41 53 48 5f 42 49 4e 41 52 59 20 6f 72  3_HASH_BINARY or
3763b 20 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49 4e   FTS3_HASH_STRIN
3763c 47 2e 20 20 54 68 65 20 76 61 6c 75 65 20 6f 66  G.  The value of
3763d 20 6b 65 79 43 6c 61 73 73 20 0a 2a 2a 20 64 65   keyClass .** de
3763e 74 65 72 6d 69 6e 65 73 20 77 68 61 74 20 6b 69  termines what ki
3763f 6e 64 20 6f 66 20 6b 65 79 20 74 68 65 20 68 61  nd of key the ha
37640 73 68 20 74 61 62 6c 65 20 77 69 6c 6c 20 75 73  sh table will us
37641 65 2e 20 20 22 63 6f 70 79 4b 65 79 22 20 69 73  e.  "copyKey" is
37642 0a 2a 2a 20 74 72 75 65 20 69 66 20 74 68 65 20  .** true if the 
37643 68 61 73 68 20 74 61 62 6c 65 20 73 68 6f 75 6c  hash table shoul
37644 64 20 6d 61 6b 65 20 69 74 73 20 6f 77 6e 20 70  d make its own p
37645 72 69 76 61 74 65 20 63 6f 70 79 20 6f 66 20 6b  rivate copy of k
37646 65 79 73 20 61 6e 64 0a 2a 2a 20 66 61 6c 73 65  eys and.** false
37647 20 69 66 20 69 74 20 73 68 6f 75 6c 64 20 6a 75   if it should ju
37648 73 74 20 75 73 65 20 74 68 65 20 73 75 70 70 6c  st use the suppl
37649 69 65 64 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a  ied pointer..*/.
3764a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
3764b 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 48  oid sqlite3Fts3H
3764c 61 73 68 49 6e 69 74 28 66 74 73 33 48 61 73 68  ashInit(fts3Hash
3764d 20 2a 70 4e 65 77 2c 20 69 6e 74 20 6b 65 79 43   *pNew, int keyC
3764e 6c 61 73 73 2c 20 69 6e 74 20 63 6f 70 79 4b 65  lass, int copyKe
3764f 79 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4e  y){.  assert( pN
37650 65 77 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ew!=0 );.  asser
37651 74 28 20 6b 65 79 43 6c 61 73 73 3e 3d 46 54 53  t( keyClass>=FTS
37652 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 20 26 26  3_HASH_STRING &&
37653 20 6b 65 79 43 6c 61 73 73 3c 3d 46 54 53 33 5f   keyClass<=FTS3_
37654 48 41 53 48 5f 42 49 4e 41 52 59 20 29 3b 0a 20  HASH_BINARY );. 
37655 20 70 4e 65 77 2d 3e 6b 65 79 43 6c 61 73 73 20   pNew->keyClass 
37656 3d 20 6b 65 79 43 6c 61 73 73 3b 0a 20 20 70 4e  = keyClass;.  pN
37657 65 77 2d 3e 63 6f 70 79 4b 65 79 20 3d 20 63 6f  ew->copyKey = co
37658 70 79 4b 65 79 3b 0a 20 20 70 4e 65 77 2d 3e 66  pyKey;.  pNew->f
37659 69 72 73 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77  irst = 0;.  pNew
3765a 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 20 20 70  ->count = 0;.  p
3765b 4e 65 77 2d 3e 68 74 73 69 7a 65 20 3d 20 30 3b  New->htsize = 0;
3765c 0a 20 20 70 4e 65 77 2d 3e 68 74 20 3d 20 30 3b  .  pNew->ht = 0;
3765d 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 6c  .}../* Remove al
3765e 6c 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 61  l entries from a
3765f 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 52 65   hash table.  Re
37660 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79  claim all memory
37661 2e 0a 2a 2a 20 43 61 6c 6c 20 74 68 69 73 20 72  ..** Call this r
37662 6f 75 74 69 6e 65 20 74 6f 20 64 65 6c 65 74 65  outine to delete
37663 20 61 20 68 61 73 68 20 74 61 62 6c 65 20 6f 72   a hash table or
37664 20 74 6f 20 72 65 73 65 74 20 61 20 68 61 73 68   to reset a hash
37665 20 74 61 62 6c 65 0a 2a 2a 20 74 6f 20 74 68 65   table.** to the
37666 20 65 6d 70 74 79 20 73 74 61 74 65 2e 0a 2a 2f   empty state..*/
37667 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
37668 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33  void sqlite3Fts3
37669 48 61 73 68 43 6c 65 61 72 28 66 74 73 33 48 61  HashClear(fts3Ha
3766a 73 68 20 2a 70 48 29 7b 0a 20 20 66 74 73 33 48  sh *pH){.  fts3H
3766b 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20  ashElem *elem;  
3766c 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f         /* For lo
3766d 6f 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20 65  oping over all e
3766e 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 74  lements of the t
3766f 61 62 6c 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72  able */..  asser
37670 74 28 20 70 48 21 3d 30 20 29 3b 0a 20 20 65 6c  t( pH!=0 );.  el
37671 65 6d 20 3d 20 70 48 2d 3e 66 69 72 73 74 3b 0a  em = pH->first;.
37672 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 30 3b    pH->first = 0;
37673 0a 20 20 66 74 73 33 48 61 73 68 46 72 65 65 28  .  fts3HashFree(
37674 70 48 2d 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68  pH->ht);.  pH->h
37675 74 20 3d 20 30 3b 0a 20 20 70 48 2d 3e 68 74 73  t = 0;.  pH->hts
37676 69 7a 65 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65  ize = 0;.  while
37677 28 20 65 6c 65 6d 20 29 7b 0a 20 20 20 20 66 74  ( elem ){.    ft
37678 73 33 48 61 73 68 45 6c 65 6d 20 2a 6e 65 78 74  s3HashElem *next
37679 5f 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65  _elem = elem->ne
3767a 78 74 3b 0a 20 20 20 20 69 66 28 20 70 48 2d 3e  xt;.    if( pH->
3767b 63 6f 70 79 4b 65 79 20 26 26 20 65 6c 65 6d 2d  copyKey && elem-
3767c 3e 70 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 66  >pKey ){.      f
3767d 74 73 33 48 61 73 68 46 72 65 65 28 65 6c 65 6d  ts3HashFree(elem
3767e 2d 3e 70 4b 65 79 29 3b 0a 20 20 20 20 7d 0a 20  ->pKey);.    }. 
3767f 20 20 20 66 74 73 33 48 61 73 68 46 72 65 65 28     fts3HashFree(
37680 65 6c 65 6d 29 3b 0a 20 20 20 20 65 6c 65 6d 20  elem);.    elem 
37681 3d 20 6e 65 78 74 5f 65 6c 65 6d 3b 0a 20 20 7d  = next_elem;.  }
37682 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 20 3d 20 30  .  pH->count = 0
37683 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 61 73 68 20  ;.}../*.** Hash 
37684 61 6e 64 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66  and comparison f
37685 75 6e 63 74 69 6f 6e 73 20 77 68 65 6e 20 74 68  unctions when th
37686 65 20 6d 6f 64 65 20 69 73 20 46 54 53 33 5f 48  e mode is FTS3_H
37687 41 53 48 5f 53 54 52 49 4e 47 0a 2a 2f 0a 73 74  ASH_STRING.*/.st
37688 61 74 69 63 20 69 6e 74 20 66 74 73 33 53 74 72  atic int fts3Str
37689 48 61 73 68 28 63 6f 6e 73 74 20 76 6f 69 64 20  Hash(const void 
3768a 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29  *pKey, int nKey)
3768b 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  {.  const char *
3768c 7a 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  z = (const char 
3768d 2a 29 70 4b 65 79 3b 0a 20 20 69 6e 74 20 68 20  *)pKey;.  int h 
3768e 3d 20 30 3b 0a 20 20 69 66 28 20 6e 4b 65 79 3c  = 0;.  if( nKey<
3768f 3d 30 20 29 20 6e 4b 65 79 20 3d 20 28 69 6e 74  =0 ) nKey = (int
37690 29 20 73 74 72 6c 65 6e 28 7a 29 3b 0a 20 20 77  ) strlen(z);.  w
37691 68 69 6c 65 28 20 6e 4b 65 79 20 3e 20 30 20 20  hile( nKey > 0  
37692 29 7b 0a 20 20 20 20 68 20 3d 20 28 68 3c 3c 33  ){.    h = (h<<3
37693 29 20 5e 20 68 20 5e 20 2a 7a 2b 2b 3b 0a 20 20  ) ^ h ^ *z++;.  
37694 20 20 6e 4b 65 79 2d 2d 3b 0a 20 20 7d 0a 20 20    nKey--;.  }.  
37695 72 65 74 75 72 6e 20 68 20 26 20 30 78 37 66 66  return h & 0x7ff
37696 66 66 66 66 66 3b 0a 7d 0a 73 74 61 74 69 63 20  fffff;.}.static 
37697 69 6e 74 20 66 74 73 33 53 74 72 43 6f 6d 70 61  int fts3StrCompa
37698 72 65 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  re(const void *p
37699 4b 65 79 31 2c 20 69 6e 74 20 6e 31 2c 20 63 6f  Key1, int n1, co
3769a 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 32 2c  nst void *pKey2,
3769b 20 69 6e 74 20 6e 32 29 7b 0a 20 20 69 66 28 20   int n2){.  if( 
3769c 6e 31 21 3d 6e 32 20 29 20 72 65 74 75 72 6e 20  n1!=n2 ) return 
3769d 31 3b 0a 20 20 72 65 74 75 72 6e 20 73 74 72 6e  1;.  return strn
3769e 63 6d 70 28 28 63 6f 6e 73 74 20 63 68 61 72 2a  cmp((const char*
3769f 29 70 4b 65 79 31 2c 28 63 6f 6e 73 74 20 63 68  )pKey1,(const ch
376a0 61 72 2a 29 70 4b 65 79 32 2c 6e 31 29 3b 0a 7d  ar*)pKey2,n1);.}
376a1 0a 0a 2f 2a 0a 2a 2a 20 48 61 73 68 20 61 6e 64  ../*.** Hash and
376a2 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63   comparison func
376a3 74 69 6f 6e 73 20 77 68 65 6e 20 74 68 65 20 6d  tions when the m
376a4 6f 64 65 20 69 73 20 46 54 53 33 5f 48 41 53 48  ode is FTS3_HASH
376a5 5f 42 49 4e 41 52 59 0a 2a 2f 0a 73 74 61 74 69  _BINARY.*/.stati
376a6 63 20 69 6e 74 20 66 74 73 33 42 69 6e 48 61 73  c int fts3BinHas
376a7 68 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b  h(const void *pK
376a8 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20  ey, int nKey){. 
376a9 20 69 6e 74 20 68 20 3d 20 30 3b 0a 20 20 63 6f   int h = 0;.  co
376aa 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 28 63  nst char *z = (c
376ab 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 4b 65 79  onst char *)pKey
376ac 3b 0a 20 20 77 68 69 6c 65 28 20 6e 4b 65 79 2d  ;.  while( nKey-
376ad 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 68 20 3d  - > 0 ){.    h =
376ae 20 28 68 3c 3c 33 29 20 5e 20 68 20 5e 20 2a 28   (h<<3) ^ h ^ *(
376af 7a 2b 2b 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  z++);.  }.  retu
376b0 72 6e 20 68 20 26 20 30 78 37 66 66 66 66 66 66  rn h & 0x7ffffff
376b1 66 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  f;.}.static int 
376b2 66 74 73 33 42 69 6e 43 6f 6d 70 61 72 65 28 63  fts3BinCompare(c
376b3 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 31  onst void *pKey1
376b4 2c 20 69 6e 74 20 6e 31 2c 20 63 6f 6e 73 74 20  , int n1, const 
376b5 76 6f 69 64 20 2a 70 4b 65 79 32 2c 20 69 6e 74  void *pKey2, int
376b6 20 6e 32 29 7b 0a 20 20 69 66 28 20 6e 31 21 3d   n2){.  if( n1!=
376b7 6e 32 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20  n2 ) return 1;. 
376b8 20 72 65 74 75 72 6e 20 6d 65 6d 63 6d 70 28 70   return memcmp(p
376b9 4b 65 79 31 2c 70 4b 65 79 32 2c 6e 31 29 3b 0a  Key1,pKey2,n1);.
376ba 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
376bb 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
376bc 20 61 70 70 72 6f 70 72 69 61 74 65 20 68 61 73   appropriate has
376bd 68 20 66 75 6e 63 74 69 6f 6e 20 67 69 76 65 6e  h function given
376be 20 74 68 65 20 6b 65 79 20 63 6c 61 73 73 2e 0a   the key class..
376bf 2a 2a 0a 2a 2a 20 54 68 65 20 43 20 73 79 6e 74  **.** The C synt
376c0 61 78 20 69 6e 20 74 68 69 73 20 66 75 6e 63 74  ax in this funct
376c1 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 6d  ion definition m
376c2 61 79 20 62 65 20 75 6e 66 61 6d 69 6c 61 72 20  ay be unfamilar 
376c3 74 6f 20 73 6f 6d 65 20 0a 2a 2a 20 70 72 6f 67  to some .** prog
376c4 72 61 6d 6d 65 72 73 2c 20 73 6f 20 77 65 20 70  rammers, so we p
376c5 72 6f 76 69 64 65 20 74 68 65 20 66 6f 6c 6c 6f  rovide the follo
376c6 77 69 6e 67 20 61 64 64 69 74 69 6f 6e 61 6c 20  wing additional 
376c7 65 78 70 6c 61 6e 61 74 69 6f 6e 3a 0a 2a 2a 0a  explanation:.**.
376c8 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74  ** The name of t
376c9 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 22  he function is "
376ca 66 74 73 48 61 73 68 46 75 6e 63 74 69 6f 6e 22  ftsHashFunction"
376cb 2e 20 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20  .  The function 
376cc 74 61 6b 65 73 20 61 0a 2a 2a 20 73 69 6e 67 6c  takes a.** singl
376cd 65 20 70 61 72 61 6d 65 74 65 72 20 22 6b 65 79  e parameter "key
376ce 43 6c 61 73 73 22 2e 20 20 54 68 65 20 72 65 74  Class".  The ret
376cf 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 66 74 73  urn value of fts
376d0 48 61 73 68 46 75 6e 63 74 69 6f 6e 28 29 0a 2a  HashFunction().*
376d1 2a 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  * is a pointer t
376d2 6f 20 61 6e 6f 74 68 65 72 20 66 75 6e 63 74 69  o another functi
376d3 6f 6e 2e 20 20 53 70 65 63 69 66 69 63 61 6c 6c  on.  Specificall
376d4 79 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  y, the return va
376d5 6c 75 65 0a 2a 2a 20 6f 66 20 66 74 73 48 61 73  lue.** of ftsHas
376d6 68 46 75 6e 63 74 69 6f 6e 28 29 20 69 73 20 61  hFunction() is a
376d7 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75   pointer to a fu
376d8 6e 63 74 69 6f 6e 20 74 68 61 74 20 74 61 6b 65  nction that take
376d9 73 20 74 77 6f 20 70 61 72 61 6d 65 74 65 72 73  s two parameters
376da 0a 2a 2a 20 77 69 74 68 20 74 79 70 65 73 20 22  .** with types "
376db 63 6f 6e 73 74 20 76 6f 69 64 2a 22 20 61 6e 64  const void*" and
376dc 20 22 69 6e 74 22 20 61 6e 64 20 72 65 74 75 72   "int" and retur
376dd 6e 73 20 61 6e 20 22 69 6e 74 22 2e 0a 2a 2f 0a  ns an "int"..*/.
376de 73 74 61 74 69 63 20 69 6e 74 20 28 2a 66 74 73  static int (*fts
376df 48 61 73 68 46 75 6e 63 74 69 6f 6e 28 69 6e 74  HashFunction(int
376e0 20 6b 65 79 43 6c 61 73 73 29 29 28 63 6f 6e 73   keyClass))(cons
376e1 74 20 76 6f 69 64 2a 2c 69 6e 74 29 7b 0a 20 20  t void*,int){.  
376e2 69 66 28 20 6b 65 79 43 6c 61 73 73 3d 3d 46 54  if( keyClass==FT
376e3 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 20 29  S3_HASH_STRING )
376e4 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 66 74  {.    return &ft
376e5 73 33 53 74 72 48 61 73 68 3b 0a 20 20 7d 65 6c  s3StrHash;.  }el
376e6 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  se{.    assert( 
376e7 6b 65 79 43 6c 61 73 73 3d 3d 46 54 53 33 5f 48  keyClass==FTS3_H
376e8 41 53 48 5f 42 49 4e 41 52 59 20 29 3b 0a 20 20  ASH_BINARY );.  
376e9 20 20 72 65 74 75 72 6e 20 26 66 74 73 33 42 69    return &fts3Bi
376ea 6e 48 61 73 68 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  nHash;.  }.}../*
376eb 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69  .** Return a poi
376ec 6e 74 65 72 20 74 6f 20 74 68 65 20 61 70 70 72  nter to the appr
376ed 6f 70 72 69 61 74 65 20 68 61 73 68 20 66 75 6e  opriate hash fun
376ee 63 74 69 6f 6e 20 67 69 76 65 6e 20 74 68 65 20  ction given the 
376ef 6b 65 79 20 63 6c 61 73 73 2e 0a 2a 2a 0a 2a 2a  key class..**.**
376f0 20 46 6f 72 20 68 65 6c 70 20 69 6e 20 69 6e 74   For help in int
376f1 65 72 70 72 65 74 65 64 20 74 68 65 20 6f 62 73  erpreted the obs
376f2 63 75 72 65 20 43 20 63 6f 64 65 20 69 6e 20 74  cure C code in t
376f3 68 65 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69  he function defi
376f4 6e 69 74 69 6f 6e 2c 0a 2a 2a 20 73 65 65 20 74  nition,.** see t
376f5 68 65 20 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e  he header commen
376f6 74 20 6f 6e 20 74 68 65 20 70 72 65 76 69 6f 75  t on the previou
376f7 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73  s function..*/.s
376f8 74 61 74 69 63 20 69 6e 74 20 28 2a 66 74 73 43  tatic int (*ftsC
376f9 6f 6d 70 61 72 65 46 75 6e 63 74 69 6f 6e 28 69  ompareFunction(i
376fa 6e 74 20 6b 65 79 43 6c 61 73 73 29 29 28 63 6f  nt keyClass))(co
376fb 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  nst void*,int,co
376fc 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 7b 0a  nst void*,int){.
376fd 20 20 69 66 28 20 6b 65 79 43 6c 61 73 73 3d 3d    if( keyClass==
376fe 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47  FTS3_HASH_STRING
376ff 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26   ){.    return &
37700 66 74 73 33 53 74 72 43 6f 6d 70 61 72 65 3b 0a  fts3StrCompare;.
37701 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
37702 65 72 74 28 20 6b 65 79 43 6c 61 73 73 3d 3d 46  ert( keyClass==F
37703 54 53 33 5f 48 41 53 48 5f 42 49 4e 41 52 59 20  TS3_HASH_BINARY 
37704 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 66  );.    return &f
37705 74 73 33 42 69 6e 43 6f 6d 70 61 72 65 3b 0a 20  ts3BinCompare;. 
37706 20 7d 0a 7d 0a 0a 2f 2a 20 4c 69 6e 6b 20 61 6e   }.}../* Link an
37707 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74 68   element into th
37708 65 20 68 61 73 68 20 74 61 62 6c 65 0a 2a 2f 0a  e hash table.*/.
37709 73 74 61 74 69 63 20 76 6f 69 64 20 66 74 73 33  static void fts3
3770a 48 61 73 68 49 6e 73 65 72 74 45 6c 65 6d 65 6e  HashInsertElemen
3770b 74 28 0a 20 20 66 74 73 33 48 61 73 68 20 2a 70  t(.  fts3Hash *p
3770c 48 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  H,            /*
3770d 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 68 61   The complete ha
3770e 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73 74  sh table */.  st
3770f 72 75 63 74 20 5f 66 74 73 33 68 74 20 2a 70 45  ruct _fts3ht *pE
37710 6e 74 72 79 2c 20 20 2f 2a 20 54 68 65 20 65 6e  ntry,  /* The en
37711 74 72 79 20 69 6e 74 6f 20 77 68 69 63 68 20 70  try into which p
37712 4e 65 77 20 69 73 20 69 6e 73 65 72 74 65 64 20  New is inserted 
37713 2a 2f 0a 20 20 66 74 73 33 48 61 73 68 45 6c 65  */.  fts3HashEle
37714 6d 20 2a 70 4e 65 77 20 20 20 20 20 20 20 2f 2a  m *pNew       /*
37715 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 74 6f 20   The element to 
37716 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 29  be inserted */.)
37717 7b 0a 20 20 66 74 73 33 48 61 73 68 45 6c 65 6d  {.  fts3HashElem
37718 20 2a 70 48 65 61 64 3b 20 20 20 20 20 2f 2a 20   *pHead;     /* 
37719 46 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 61 6c  First element al
3771a 72 65 61 64 79 20 69 6e 20 70 45 6e 74 72 79 20  ready in pEntry 
3771b 2a 2f 0a 20 20 70 48 65 61 64 20 3d 20 70 45 6e  */.  pHead = pEn
3771c 74 72 79 2d 3e 63 68 61 69 6e 3b 0a 20 20 69 66  try->chain;.  if
3771d 28 20 70 48 65 61 64 20 29 7b 0a 20 20 20 20 70  ( pHead ){.    p
3771e 4e 65 77 2d 3e 6e 65 78 74 20 3d 20 70 48 65 61  New->next = pHea
3771f 64 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 70 72 65  d;.    pNew->pre
37720 76 20 3d 20 70 48 65 61 64 2d 3e 70 72 65 76 3b  v = pHead->prev;
37721 0a 20 20 20 20 69 66 28 20 70 48 65 61 64 2d 3e  .    if( pHead->
37722 70 72 65 76 20 29 7b 20 70 48 65 61 64 2d 3e 70  prev ){ pHead->p
37723 72 65 76 2d 3e 6e 65 78 74 20 3d 20 70 4e 65 77  rev->next = pNew
37724 3b 20 7d 0a 20 20 20 20 65 6c 73 65 20 20 20 20  ; }.    else    
37725 20 20 20 20 20 20 20 20 20 7b 20 70 48 2d 3e 66           { pH->f
37726 69 72 73 74 20 3d 20 70 4e 65 77 3b 20 7d 0a 20  irst = pNew; }. 
37727 20 20 20 70 48 65 61 64 2d 3e 70 72 65 76 20 3d     pHead->prev =
37728 20 70 4e 65 77 3b 0a 20 20 7d 65 6c 73 65 7b 0a   pNew;.  }else{.
37729 20 20 20 20 70 4e 65 77 2d 3e 6e 65 78 74 20 3d      pNew->next =
3772a 20 70 48 2d 3e 66 69 72 73 74 3b 0a 20 20 20 20   pH->first;.    
3772b 69 66 28 20 70 48 2d 3e 66 69 72 73 74 20 29 7b  if( pH->first ){
3772c 20 70 48 2d 3e 66 69 72 73 74 2d 3e 70 72 65 76   pH->first->prev
3772d 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20 70   = pNew; }.    p
3772e 4e 65 77 2d 3e 70 72 65 76 20 3d 20 30 3b 0a 20  New->prev = 0;. 
3772f 20 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 70     pH->first = p
37730 4e 65 77 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 72  New;.  }.  pEntr
37731 79 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 70 45  y->count++;.  pE
37732 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 70 4e  ntry->chain = pN
37733 65 77 3b 0a 7d 0a 0a 0a 2f 2a 20 52 65 73 69 7a  ew;.}.../* Resiz
37734 65 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  e the hash table
37735 20 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e 74   so that it cant
37736 61 69 6e 73 20 22 6e 65 77 5f 73 69 7a 65 22 20  ains "new_size" 
37737 62 75 63 6b 65 74 73 2e 0a 2a 2a 20 22 6e 65 77  buckets..** "new
37738 5f 73 69 7a 65 22 20 6d 75 73 74 20 62 65 20 61  _size" must be a
37739 20 70 6f 77 65 72 20 6f 66 20 32 2e 20 20 54 68   power of 2.  Th
3773a 65 20 68 61 73 68 20 74 61 62 6c 65 20 6d 69 67  e hash table mig
3773b 68 74 20 66 61 69 6c 20 0a 2a 2a 20 74 6f 20 72  ht fail .** to r
3773c 65 73 69 7a 65 20 69 66 20 73 71 6c 69 74 65 4d  esize if sqliteM
3773d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e 0a 2a  alloc() fails..*
3773e 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 74  /.static void ft
3773f 73 33 52 65 68 61 73 68 28 66 74 73 33 48 61 73  s3Rehash(fts3Has
37740 68 20 2a 70 48 2c 20 69 6e 74 20 6e 65 77 5f 73  h *pH, int new_s
37741 69 7a 65 29 7b 0a 20 20 73 74 72 75 63 74 20 5f  ize){.  struct _
37742 66 74 73 33 68 74 20 2a 6e 65 77 5f 68 74 3b 20  fts3ht *new_ht; 
37743 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
37744 6e 65 77 20 68 61 73 68 20 74 61 62 6c 65 20 2a  new hash table *
37745 2f 0a 20 20 66 74 73 33 48 61 73 68 45 6c 65 6d  /.  fts3HashElem
37746 20 2a 65 6c 65 6d 2c 20 2a 6e 65 78 74 5f 65 6c   *elem, *next_el
37747 65 6d 3b 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70  em;  /* For loop
37748 69 6e 67 20 6f 76 65 72 20 65 78 69 73 74 69 6e  ing over existin
37749 67 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20  g elements */.  
3774a 69 6e 74 20 28 2a 78 48 61 73 68 29 28 63 6f 6e  int (*xHash)(con
3774b 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 20 20  st void*,int);  
3774c 20 2f 2a 20 54 68 65 20 68 61 73 68 20 66 75 6e   /* The hash fun
3774d 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65  ction */..  asse
3774e 72 74 28 20 28 6e 65 77 5f 73 69 7a 65 20 26 20  rt( (new_size & 
3774f 28 6e 65 77 5f 73 69 7a 65 2d 31 29 29 3d 3d 30  (new_size-1))==0
37750 20 29 3b 0a 20 20 6e 65 77 5f 68 74 20 3d 20 28   );.  new_ht = (
37751 73 74 72 75 63 74 20 5f 66 74 73 33 68 74 20 2a  struct _fts3ht *
37752 29 66 74 73 33 48 61 73 68 4d 61 6c 6c 6f 63 28  )fts3HashMalloc(
37753 20 6e 65 77 5f 73 69 7a 65 2a 73 69 7a 65 6f 66   new_size*sizeof
37754 28 73 74 72 75 63 74 20 5f 66 74 73 33 68 74 29  (struct _fts3ht)
37755 20 29 3b 0a 20 20 69 66 28 20 6e 65 77 5f 68 74   );.  if( new_ht
37756 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
37757 66 74 73 33 48 61 73 68 46 72 65 65 28 70 48 2d  fts3HashFree(pH-
37758 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68 74 20 3d  >ht);.  pH->ht =
37759 20 6e 65 77 5f 68 74 3b 0a 20 20 70 48 2d 3e 68   new_ht;.  pH->h
3775a 74 73 69 7a 65 20 3d 20 6e 65 77 5f 73 69 7a 65  tsize = new_size
3775b 3b 0a 20 20 78 48 61 73 68 20 3d 20 66 74 73 48  ;.  xHash = ftsH
3775c 61 73 68 46 75 6e 63 74 69 6f 6e 28 70 48 2d 3e  ashFunction(pH->
3775d 6b 65 79 43 6c 61 73 73 29 3b 0a 20 20 66 6f 72  keyClass);.  for
3775e 28 65 6c 65 6d 3d 70 48 2d 3e 66 69 72 73 74 2c  (elem=pH->first,
3775f 20 70 48 2d 3e 66 69 72 73 74 3d 30 3b 20 65 6c   pH->first=0; el
37760 65 6d 3b 20 65 6c 65 6d 20 3d 20 6e 65 78 74 5f  em; elem = next_
37761 65 6c 65 6d 29 7b 0a 20 20 20 20 69 6e 74 20 68  elem){.    int h
37762 20 3d 20 28 2a 78 48 61 73 68 29 28 65 6c 65 6d   = (*xHash)(elem
37763 2d 3e 70 4b 65 79 2c 20 65 6c 65 6d 2d 3e 6e 4b  ->pKey, elem->nK
37764 65 79 29 20 26 20 28 6e 65 77 5f 73 69 7a 65 2d  ey) & (new_size-
37765 31 29 3b 0a 20 20 20 20 6e 65 78 74 5f 65 6c 65  1);.    next_ele
37766 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a  m = elem->next;.
37767 20 20 20 20 66 74 73 33 48 61 73 68 49 6e 73 65      fts3HashInse
37768 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20 26 6e  rtElement(pH, &n
37769 65 77 5f 68 74 5b 68 5d 2c 20 65 6c 65 6d 29 3b  ew_ht[h], elem);
3776a 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20  .  }.}../* This 
3776b 66 75 6e 63 74 69 6f 6e 20 28 66 6f 72 20 69 6e  function (for in
3776c 74 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79 29  ternal use only)
3776d 20 6c 6f 63 61 74 65 73 20 61 6e 20 65 6c 65 6d   locates an elem
3776e 65 6e 74 20 69 6e 20 61 6e 0a 2a 2a 20 68 61 73  ent in an.** has
3776f 68 20 74 61 62 6c 65 20 74 68 61 74 20 6d 61 74  h table that mat
37770 63 68 65 73 20 74 68 65 20 67 69 76 65 6e 20 6b  ches the given k
37771 65 79 2e 20 20 54 68 65 20 68 61 73 68 20 66 6f  ey.  The hash fo
37772 72 20 74 68 69 73 20 6b 65 79 20 68 61 73 0a 2a  r this key has.*
37773 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63  * already been c
37774 6f 6d 70 75 74 65 64 20 61 6e 64 20 69 73 20 70  omputed and is p
37775 61 73 73 65 64 20 61 73 20 74 68 65 20 34 74 68  assed as the 4th
37776 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 73   parameter..*/.s
37777 74 61 74 69 63 20 66 74 73 33 48 61 73 68 45 6c  tatic fts3HashEl
37778 65 6d 20 2a 66 74 73 33 46 69 6e 64 45 6c 65 6d  em *fts3FindElem
37779 65 6e 74 42 79 48 61 73 68 28 0a 20 20 63 6f 6e  entByHash(.  con
3777a 73 74 20 66 74 73 33 48 61 73 68 20 2a 70 48 2c  st fts3Hash *pH,
3777b 20 2f 2a 20 54 68 65 20 70 48 20 74 6f 20 62 65   /* The pH to be
3777c 20 73 65 61 72 63 68 65 64 20 2a 2f 0a 20 20 63   searched */.  c
3777d 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c  onst void *pKey,
3777e 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 77 65     /* The key we
3777f 20 61 72 65 20 73 65 61 72 63 68 69 6e 67 20 66   are searching f
37780 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 79  or */.  int nKey
37781 2c 0a 20 20 69 6e 74 20 68 20 20 20 20 20 20 20  ,.  int h       
37782 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 68          /* The h
37783 61 73 68 20 66 6f 72 20 74 68 69 73 20 6b 65 79  ash for this key
37784 2e 20 2a 2f 0a 29 7b 0a 20 20 66 74 73 33 48 61  . */.){.  fts3Ha
37785 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20  shElem *elem;   
37786 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64           /* Used
37787 20 74 6f 20 6c 6f 6f 70 20 74 68 72 75 20 74 68   to loop thru th
37788 65 20 65 6c 65 6d 65 6e 74 20 6c 69 73 74 20 2a  e element list *
37789 2f 0a 20 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20  /.  int count;  
3778a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3778b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
3778c 65 6c 65 6d 65 6e 74 73 20 6c 65 66 74 20 74 6f  elements left to
3778d 20 74 65 73 74 20 2a 2f 0a 20 20 69 6e 74 20 28   test */.  int (
3778e 2a 78 43 6f 6d 70 61 72 65 29 28 63 6f 6e 73 74  *xCompare)(const
3778f 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74   void*,int,const
37790 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 20 20 2f 2a   void*,int);  /*
37791 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63   comparison func
37792 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 70  tion */..  if( p
37793 48 2d 3e 68 74 20 29 7b 0a 20 20 20 20 73 74 72  H->ht ){.    str
37794 75 63 74 20 5f 66 74 73 33 68 74 20 2a 70 45 6e  uct _fts3ht *pEn
37795 74 72 79 20 3d 20 26 70 48 2d 3e 68 74 5b 68 5d  try = &pH->ht[h]
37796 3b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 70 45 6e  ;.    elem = pEn
37797 74 72 79 2d 3e 63 68 61 69 6e 3b 0a 20 20 20 20  try->chain;.    
37798 63 6f 75 6e 74 20 3d 20 70 45 6e 74 72 79 2d 3e  count = pEntry->
37799 63 6f 75 6e 74 3b 0a 20 20 20 20 78 43 6f 6d 70  count;.    xComp
3779a 61 72 65 20 3d 20 66 74 73 43 6f 6d 70 61 72 65  are = ftsCompare
3779b 46 75 6e 63 74 69 6f 6e 28 70 48 2d 3e 6b 65 79  Function(pH->key
3779c 43 6c 61 73 73 29 3b 0a 20 20 20 20 77 68 69 6c  Class);.    whil
3779d 65 28 20 63 6f 75 6e 74 2d 2d 20 26 26 20 65 6c  e( count-- && el
3779e 65 6d 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  em ){.      if( 
3779f 28 2a 78 43 6f 6d 70 61 72 65 29 28 65 6c 65 6d  (*xCompare)(elem
377a0 2d 3e 70 4b 65 79 2c 65 6c 65 6d 2d 3e 6e 4b 65  ->pKey,elem->nKe
377a1 79 2c 70 4b 65 79 2c 6e 4b 65 79 29 3d 3d 30 20  y,pKey,nKey)==0 
377a2 29 7b 20 0a 20 20 20 20 20 20 20 20 72 65 74 75  ){ .        retu
377a3 72 6e 20 65 6c 65 6d 3b 0a 20 20 20 20 20 20 7d  rn elem;.      }
377a4 0a 20 20 20 20 20 20 65 6c 65 6d 20 3d 20 65 6c  .      elem = el
377a5 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 7d 0a  em->next;.    }.
377a6 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
377a7 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 20 73  }../* Remove a s
377a8 69 6e 67 6c 65 20 65 6e 74 72 79 20 66 72 6f 6d  ingle entry from
377a9 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
377aa 67 69 76 65 6e 20 61 20 70 6f 69 6e 74 65 72 20  given a pointer 
377ab 74 6f 20 74 68 61 74 0a 2a 2a 20 65 6c 65 6d 65  to that.** eleme
377ac 6e 74 20 61 6e 64 20 61 20 68 61 73 68 20 6f 6e  nt and a hash on
377ad 20 74 68 65 20 65 6c 65 6d 65 6e 74 27 73 20 6b   the element's k
377ae 65 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ey..*/.static vo
377af 69 64 20 66 74 73 33 52 65 6d 6f 76 65 45 6c 65  id fts3RemoveEle
377b0 6d 65 6e 74 42 79 48 61 73 68 28 0a 20 20 66 74  mentByHash(.  ft
377b1 73 33 48 61 73 68 20 2a 70 48 2c 20 20 20 20 20  s3Hash *pH,     
377b2 20 20 20 20 2f 2a 20 54 68 65 20 70 48 20 63 6f      /* The pH co
377b3 6e 74 61 69 6e 69 6e 67 20 22 65 6c 65 6d 22 20  ntaining "elem" 
377b4 2a 2f 0a 20 20 66 74 73 33 48 61 73 68 45 6c 65  */.  fts3HashEle
377b5 6d 2a 20 65 6c 65 6d 2c 20 20 20 2f 2a 20 54 68  m* elem,   /* Th
377b6 65 20 65 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20  e element to be 
377b7 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65  removed from the
377b8 20 70 48 20 2a 2f 0a 20 20 69 6e 74 20 68 20 20   pH */.  int h  
377b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
377ba 2a 20 48 61 73 68 20 76 61 6c 75 65 20 66 6f 72  * Hash value for
377bb 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a   the element */.
377bc 29 7b 0a 20 20 73 74 72 75 63 74 20 5f 66 74 73  ){.  struct _fts
377bd 33 68 74 20 2a 70 45 6e 74 72 79 3b 0a 20 20 69  3ht *pEntry;.  i
377be 66 28 20 65 6c 65 6d 2d 3e 70 72 65 76 20 29 7b  f( elem->prev ){
377bf 0a 20 20 20 20 65 6c 65 6d 2d 3e 70 72 65 76 2d  .    elem->prev-
377c0 3e 6e 65 78 74 20 3d 20 65 6c 65 6d 2d 3e 6e 65  >next = elem->ne
377c1 78 74 3b 20 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  xt; .  }else{.  
377c2 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 65 6c    pH->first = el
377c3 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20  em->next;.  }.  
377c4 69 66 28 20 65 6c 65 6d 2d 3e 6e 65 78 74 20 29  if( elem->next )
377c5 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 6e 65 78 74  {.    elem->next
377c6 2d 3e 70 72 65 76 20 3d 20 65 6c 65 6d 2d 3e 70  ->prev = elem->p
377c7 72 65 76 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 72  rev;.  }.  pEntr
377c8 79 20 3d 20 26 70 48 2d 3e 68 74 5b 68 5d 3b 0a  y = &pH->ht[h];.
377c9 20 20 69 66 28 20 70 45 6e 74 72 79 2d 3e 63 68    if( pEntry->ch
377ca 61 69 6e 3d 3d 65 6c 65 6d 20 29 7b 0a 20 20 20  ain==elem ){.   
377cb 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d   pEntry->chain =
377cc 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d   elem->next;.  }
377cd 0a 20 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74  .  pEntry->count
377ce 2d 2d 3b 0a 20 20 69 66 28 20 70 45 6e 74 72 79  --;.  if( pEntry
377cf 2d 3e 63 6f 75 6e 74 3c 3d 30 20 29 7b 0a 20 20  ->count<=0 ){.  
377d0 20 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20    pEntry->chain 
377d1 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  = 0;.  }.  if( p
377d2 48 2d 3e 63 6f 70 79 4b 65 79 20 26 26 20 65 6c  H->copyKey && el
377d3 65 6d 2d 3e 70 4b 65 79 20 29 7b 0a 20 20 20 20  em->pKey ){.    
377d4 66 74 73 33 48 61 73 68 46 72 65 65 28 65 6c 65  fts3HashFree(ele
377d5 6d 2d 3e 70 4b 65 79 29 3b 0a 20 20 7d 0a 20 20  m->pKey);.  }.  
377d6 66 74 73 33 48 61 73 68 46 72 65 65 28 20 65 6c  fts3HashFree( el
377d7 65 6d 20 29 3b 0a 20 20 70 48 2d 3e 63 6f 75 6e  em );.  pH->coun
377d8 74 2d 2d 3b 0a 20 20 69 66 28 20 70 48 2d 3e 63  t--;.  if( pH->c
377d9 6f 75 6e 74 3c 3d 30 20 29 7b 0a 20 20 20 20 61  ount<=0 ){.    a
377da 73 73 65 72 74 28 20 70 48 2d 3e 66 69 72 73 74  ssert( pH->first
377db 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ==0 );.    asser
377dc 74 28 20 70 48 2d 3e 63 6f 75 6e 74 3d 3d 30 20  t( pH->count==0 
377dd 29 3b 0a 20 20 20 20 66 74 73 33 48 61 73 68 43  );.    fts3HashC
377de 6c 65 61 72 28 70 48 29 3b 0a 20 20 7d 0a 7d 0a  lear(pH);.  }.}.
377df 0a 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6c  ./* Attempt to l
377e0 6f 63 61 74 65 20 61 6e 20 65 6c 65 6d 65 6e 74  ocate an element
377e1 20 6f 66 20 74 68 65 20 68 61 73 68 20 74 61 62   of the hash tab
377e2 6c 65 20 70 48 20 77 69 74 68 20 61 20 6b 65 79  le pH with a key
377e3 0a 2a 2a 20 74 68 61 74 20 6d 61 74 63 68 65 73  .** that matches
377e4 20 70 4b 65 79 2c 6e 4b 65 79 2e 20 20 52 65 74   pKey,nKey.  Ret
377e5 75 72 6e 20 74 68 65 20 64 61 74 61 20 66 6f 72  urn the data for
377e6 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 69 66   this element if
377e7 20 69 74 20 69 73 0a 2a 2a 20 66 6f 75 6e 64 2c   it is.** found,
377e8 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 72   or NULL if ther
377e9 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 2e 0a 2a  e is no match..*
377ea 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
377eb 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 46 74   void *sqlite3Ft
377ec 73 33 48 61 73 68 46 69 6e 64 28 63 6f 6e 73 74  s3HashFind(const
377ed 20 66 74 73 33 48 61 73 68 20 2a 70 48 2c 20 63   fts3Hash *pH, c
377ee 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c  onst void *pKey,
377ef 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 69 6e   int nKey){.  in
377f0 74 20 68 3b 20 20 20 20 20 20 20 20 20 20 20 20  t h;            
377f1 20 20 20 20 20 2f 2a 20 41 20 68 61 73 68 20 6f       /* A hash o
377f2 6e 20 6b 65 79 20 2a 2f 0a 20 20 66 74 73 33 48  n key */.  fts3H
377f3 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20  ashElem *elem;  
377f4 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74    /* The element
377f5 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 6b 65   that matches ke
377f6 79 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 48 61  y */.  int (*xHa
377f7 73 68 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  sh)(const void*,
377f8 69 6e 74 29 3b 20 20 2f 2a 20 54 68 65 20 68 61  int);  /* The ha
377f9 73 68 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a  sh function */..
377fa 20 20 69 66 28 20 70 48 3d 3d 30 20 7c 7c 20 70    if( pH==0 || p
377fb 48 2d 3e 68 74 3d 3d 30 20 29 20 72 65 74 75 72  H->ht==0 ) retur
377fc 6e 20 30 3b 0a 20 20 78 48 61 73 68 20 3d 20 66  n 0;.  xHash = f
377fd 74 73 48 61 73 68 46 75 6e 63 74 69 6f 6e 28 70  tsHashFunction(p
377fe 48 2d 3e 6b 65 79 43 6c 61 73 73 29 3b 0a 20 20  H->keyClass);.  
377ff 61 73 73 65 72 74 28 20 78 48 61 73 68 21 3d 30  assert( xHash!=0
37800 20 29 3b 0a 20 20 68 20 3d 20 28 2a 78 48 61 73   );.  h = (*xHas
37801 68 29 28 70 4b 65 79 2c 6e 4b 65 79 29 3b 0a 20  h)(pKey,nKey);. 
37802 20 61 73 73 65 72 74 28 20 28 70 48 2d 3e 68 74   assert( (pH->ht
37803 73 69 7a 65 20 26 20 28 70 48 2d 3e 68 74 73 69  size & (pH->htsi
37804 7a 65 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20 65  ze-1))==0 );.  e
37805 6c 65 6d 20 3d 20 66 74 73 33 46 69 6e 64 45 6c  lem = fts3FindEl
37806 65 6d 65 6e 74 42 79 48 61 73 68 28 70 48 2c 70  ementByHash(pH,p
37807 4b 65 79 2c 6e 4b 65 79 2c 20 68 20 26 20 28 70  Key,nKey, h & (p
37808 48 2d 3e 68 74 73 69 7a 65 2d 31 29 29 3b 0a 20  H->htsize-1));. 
37809 20 72 65 74 75 72 6e 20 65 6c 65 6d 20 3f 20 65   return elem ? e
3780a 6c 65 6d 2d 3e 64 61 74 61 20 3a 20 30 3b 0a 7d  lem->data : 0;.}
3780b 0a 0a 2f 2a 20 49 6e 73 65 72 74 20 61 6e 20 65  ../* Insert an e
3780c 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65 20  lement into the 
3780d 68 61 73 68 20 74 61 62 6c 65 20 70 48 2e 20 20  hash table pH.  
3780e 54 68 65 20 6b 65 79 20 69 73 20 70 4b 65 79 2c  The key is pKey,
3780f 6e 4b 65 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20  nKey.** and the 
37810 64 61 74 61 20 69 73 20 22 64 61 74 61 22 2e 0a  data is "data"..
37811 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 65 6c 65 6d  **.** If no elem
37812 65 6e 74 20 65 78 69 73 74 73 20 77 69 74 68 20  ent exists with 
37813 61 20 6d 61 74 63 68 69 6e 67 20 6b 65 79 2c 20  a matching key, 
37814 74 68 65 6e 20 61 20 6e 65 77 0a 2a 2a 20 65 6c  then a new.** el
37815 65 6d 65 6e 74 20 69 73 20 63 72 65 61 74 65 64  ement is created
37816 2e 20 20 41 20 63 6f 70 79 20 6f 66 20 74 68 65  .  A copy of the
37817 20 6b 65 79 20 69 73 20 6d 61 64 65 20 69 66 20   key is made if 
37818 74 68 65 20 63 6f 70 79 4b 65 79 0a 2a 2a 20 66  the copyKey.** f
37819 6c 61 67 20 69 73 20 73 65 74 2e 20 20 4e 55 4c  lag is set.  NUL
3781a 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  L is returned..*
3781b 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20  *.** If another 
3781c 65 6c 65 6d 65 6e 74 20 61 6c 72 65 61 64 79 20  element already 
3781d 65 78 69 73 74 73 20 77 69 74 68 20 74 68 65 20  exists with the 
3781e 73 61 6d 65 20 6b 65 79 2c 20 74 68 65 6e 20 74  same key, then t
3781f 68 65 0a 2a 2a 20 6e 65 77 20 64 61 74 61 20 72  he.** new data r
37820 65 70 6c 61 63 65 73 20 74 68 65 20 6f 6c 64 20  eplaces the old 
37821 64 61 74 61 20 61 6e 64 20 74 68 65 20 6f 6c 64  data and the old
37822 20 64 61 74 61 20 69 73 20 72 65 74 75 72 6e 65   data is returne
37823 64 2e 0a 2a 2a 20 54 68 65 20 6b 65 79 20 69 73  d..** The key is
37824 20 6e 6f 74 20 63 6f 70 69 65 64 20 69 6e 20 74   not copied in t
37825 68 69 73 20 69 6e 73 74 61 6e 63 65 2e 20 20 49  his instance.  I
37826 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73  f a malloc fails
37827 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6e 65  , then.** the ne
37828 77 20 64 61 74 61 20 69 73 20 72 65 74 75 72 6e  w data is return
37829 65 64 20 61 6e 64 20 74 68 65 20 68 61 73 68 20  ed and the hash 
3782a 74 61 62 6c 65 20 69 73 20 75 6e 63 68 61 6e 67  table is unchang
3782b 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ed..**.** If the
3782c 20 22 64 61 74 61 22 20 70 61 72 61 6d 65 74 65   "data" paramete
3782d 72 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69  r to this functi
3782e 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e  on is NULL, then
3782f 20 74 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20   the.** element 
37830 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f  corresponding to
37831 20 22 6b 65 79 22 20 69 73 20 72 65 6d 6f 76 65   "key" is remove
37832 64 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20  d from the hash 
37833 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  table..*/.SQLITE
37834 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
37835 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49 6e  qlite3Fts3HashIn
37836 73 65 72 74 28 0a 20 20 66 74 73 33 48 61 73 68  sert(.  fts3Hash
37837 20 2a 70 48 2c 20 20 20 20 20 20 20 20 2f 2a 20   *pH,        /* 
37838 54 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 74  The hash table t
37839 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 2a 2f  o insert into */
3783a 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  .  const void *p
3783b 4b 65 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 6b  Key,    /* The k
3783c 65 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 79  ey */.  int nKey
3783d 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
3783e 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
3783f 69 6e 20 74 68 65 20 6b 65 79 20 2a 2f 0a 20 20  in the key */.  
37840 76 6f 69 64 20 2a 64 61 74 61 20 20 20 20 20 20  void *data      
37841 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
37842 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 68 72 61   */.){.  int hra
37843 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  w;              
37844 20 20 20 2f 2a 20 52 61 77 20 68 61 73 68 20 76     /* Raw hash v
37845 61 6c 75 65 20 6f 66 20 74 68 65 20 6b 65 79 20  alue of the key 
37846 2a 2f 0a 20 20 69 6e 74 20 68 3b 20 20 20 20 20  */.  int h;     
37847 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
37848 2a 20 74 68 65 20 68 61 73 68 20 6f 66 20 74 68  * the hash of th
37849 65 20 6b 65 79 20 6d 6f 64 75 6c 6f 20 68 61 73  e key modulo has
3784a 68 20 74 61 62 6c 65 20 73 69 7a 65 20 2a 2f 0a  h table size */.
3784b 20 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a    fts3HashElem *
3784c 65 6c 65 6d 3b 20 20 20 20 20 20 20 2f 2a 20 55  elem;       /* U
3784d 73 65 64 20 74 6f 20 6c 6f 6f 70 20 74 68 72 75  sed to loop thru
3784e 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 6c 69 73   the element lis
3784f 74 20 2a 2f 0a 20 20 66 74 73 33 48 61 73 68 45  t */.  fts3HashE
37850 6c 65 6d 20 2a 6e 65 77 5f 65 6c 65 6d 3b 20 20  lem *new_elem;  
37851 20 2f 2a 20 4e 65 77 20 65 6c 65 6d 65 6e 74 20   /* New element 
37852 61 64 64 65 64 20 74 6f 20 74 68 65 20 70 48 20  added to the pH 
37853 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 48 61 73 68  */.  int (*xHash
37854 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  )(const void*,in
37855 74 29 3b 20 20 2f 2a 20 54 68 65 20 68 61 73 68  t);  /* The hash
37856 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20   function */..  
37857 61 73 73 65 72 74 28 20 70 48 21 3d 30 20 29 3b  assert( pH!=0 );
37858 0a 20 20 78 48 61 73 68 20 3d 20 66 74 73 48 61  .  xHash = ftsHa
37859 73 68 46 75 6e 63 74 69 6f 6e 28 70 48 2d 3e 6b  shFunction(pH->k
3785a 65 79 43 6c 61 73 73 29 3b 0a 20 20 61 73 73 65  eyClass);.  asse
3785b 72 74 28 20 78 48 61 73 68 21 3d 30 20 29 3b 0a  rt( xHash!=0 );.
3785c 20 20 68 72 61 77 20 3d 20 28 2a 78 48 61 73 68    hraw = (*xHash
3785d 29 28 70 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a 20  )(pKey, nKey);. 
3785e 20 61 73 73 65 72 74 28 20 28 70 48 2d 3e 68 74   assert( (pH->ht
3785f 73 69 7a 65 20 26 20 28 70 48 2d 3e 68 74 73 69  size & (pH->htsi
37860 7a 65 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20 68  ze-1))==0 );.  h
37861 20 3d 20 68 72 61 77 20 26 20 28 70 48 2d 3e 68   = hraw & (pH->h
37862 74 73 69 7a 65 2d 31 29 3b 0a 20 20 65 6c 65 6d  tsize-1);.  elem
37863 20 3d 20 66 74 73 33 46 69 6e 64 45 6c 65 6d 65   = fts3FindEleme
37864 6e 74 42 79 48 61 73 68 28 70 48 2c 70 4b 65 79  ntByHash(pH,pKey
37865 2c 6e 4b 65 79 2c 68 29 3b 0a 20 20 69 66 28 20  ,nKey,h);.  if( 
37866 65 6c 65 6d 20 29 7b 0a 20 20 20 20 76 6f 69 64  elem ){.    void
37867 20 2a 6f 6c 64 5f 64 61 74 61 20 3d 20 65 6c 65   *old_data = ele
37868 6d 2d 3e 64 61 74 61 3b 0a 20 20 20 20 69 66 28  m->data;.    if(
37869 20 64 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20   data==0 ){.    
3786a 20 20 66 74 73 33 52 65 6d 6f 76 65 45 6c 65 6d    fts3RemoveElem
3786b 65 6e 74 42 79 48 61 73 68 28 70 48 2c 65 6c 65  entByHash(pH,ele
3786c 6d 2c 68 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  m,h);.    }else{
3786d 0a 20 20 20 20 20 20 65 6c 65 6d 2d 3e 64 61 74  .      elem->dat
3786e 61 20 3d 20 64 61 74 61 3b 0a 20 20 20 20 7d 0a  a = data;.    }.
3786f 20 20 20 20 72 65 74 75 72 6e 20 6f 6c 64 5f 64      return old_d
37870 61 74 61 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64  ata;.  }.  if( d
37871 61 74 61 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ata==0 ) return 
37872 30 3b 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 73  0;.  if( pH->hts
37873 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 66 74  ize==0 ){.    ft
37874 73 33 52 65 68 61 73 68 28 70 48 2c 38 29 3b 0a  s3Rehash(pH,8);.
37875 20 20 20 20 69 66 28 20 70 48 2d 3e 68 74 73 69      if( pH->htsi
37876 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70  ze==0 ){.      p
37877 48 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 20 20  H->count = 0;.  
37878 20 20 20 20 72 65 74 75 72 6e 20 64 61 74 61 3b      return data;
37879 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6e 65 77  .    }.  }.  new
3787a 5f 65 6c 65 6d 20 3d 20 28 66 74 73 33 48 61 73  _elem = (fts3Has
3787b 68 45 6c 65 6d 2a 29 66 74 73 33 48 61 73 68 4d  hElem*)fts3HashM
3787c 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 66 74  alloc( sizeof(ft
3787d 73 33 48 61 73 68 45 6c 65 6d 29 20 29 3b 0a 20  s3HashElem) );. 
3787e 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d 3d 3d 30   if( new_elem==0
3787f 20 29 20 72 65 74 75 72 6e 20 64 61 74 61 3b 0a   ) return data;.
37880 20 20 69 66 28 20 70 48 2d 3e 63 6f 70 79 4b 65    if( pH->copyKe
37881 79 20 26 26 20 70 4b 65 79 21 3d 30 20 29 7b 0a  y && pKey!=0 ){.
37882 20 20 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b      new_elem->pK
37883 65 79 20 3d 20 66 74 73 33 48 61 73 68 4d 61 6c  ey = fts3HashMal
37884 6c 6f 63 28 20 6e 4b 65 79 20 29 3b 0a 20 20 20  loc( nKey );.   
37885 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d 2d 3e 70   if( new_elem->p
37886 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Key==0 ){.      
37887 66 74 73 33 48 61 73 68 46 72 65 65 28 6e 65 77  fts3HashFree(new
37888 5f 65 6c 65 6d 29 3b 0a 20 20 20 20 20 20 72 65  _elem);.      re
37889 74 75 72 6e 20 64 61 74 61 3b 0a 20 20 20 20 7d  turn data;.    }
3788a 0a 20 20 20 20 6d 65 6d 63 70 79 28 28 76 6f 69  .    memcpy((voi
3788b 64 2a 29 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65  d*)new_elem->pKe
3788c 79 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a  y, pKey, nKey);.
3788d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 65 77    }else{.    new
3788e 5f 65 6c 65 6d 2d 3e 70 4b 65 79 20 3d 20 28 76  _elem->pKey = (v
3788f 6f 69 64 2a 29 70 4b 65 79 3b 0a 20 20 7d 0a 20  oid*)pKey;.  }. 
37890 20 6e 65 77 5f 65 6c 65 6d 2d 3e 6e 4b 65 79 20   new_elem->nKey 
37891 3d 20 6e 4b 65 79 3b 0a 20 20 70 48 2d 3e 63 6f  = nKey;.  pH->co
37892 75 6e 74 2b 2b 3b 0a 20 20 69 66 28 20 70 48 2d  unt++;.  if( pH-
37893 3e 63 6f 75 6e 74 20 3e 20 70 48 2d 3e 68 74 73  >count > pH->hts
37894 69 7a 65 20 29 7b 0a 20 20 20 20 66 74 73 33 52  ize ){.    fts3R
37895 65 68 61 73 68 28 70 48 2c 70 48 2d 3e 68 74 73  ehash(pH,pH->hts
37896 69 7a 65 2a 32 29 3b 0a 20 20 7d 0a 20 20 61 73  ize*2);.  }.  as
37897 73 65 72 74 28 20 70 48 2d 3e 68 74 73 69 7a 65  sert( pH->htsize
37898 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  >0 );.  assert( 
37899 28 70 48 2d 3e 68 74 73 69 7a 65 20 26 20 28 70  (pH->htsize & (p
3789a 48 2d 3e 68 74 73 69 7a 65 2d 31 29 29 3d 3d 30  H->htsize-1))==0
3789b 20 29 3b 0a 20 20 68 20 3d 20 68 72 61 77 20 26   );.  h = hraw &
3789c 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d 31 29 3b   (pH->htsize-1);
3789d 0a 20 20 66 74 73 33 48 61 73 68 49 6e 73 65 72  .  fts3HashInser
3789e 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20 26 70 48  tElement(pH, &pH
3789f 2d 3e 68 74 5b 68 5d 2c 20 6e 65 77 5f 65 6c 65  ->ht[h], new_ele
378a0 6d 29 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e  m);.  new_elem->
378a1 64 61 74 61 20 3d 20 64 61 74 61 3b 0a 20 20 72  data = data;.  r
378a2 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 23 65 6e 64  eturn 0;.}..#end
378a3 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53  if /* !defined(S
378a4 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64  QLITE_CORE) || d
378a5 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
378a6 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f  ABLE_FTS3) */../
378a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
378a8 6e 64 20 6f 66 20 66 74 73 33 5f 68 61 73 68 2e  nd of fts3_hash.
378a9 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
378aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
378ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
378ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
378ad 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f 70  egin file fts3_p
378ae 6f 72 74 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  orter.c ********
378af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
378b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
378b1 2a 0a 2a 2a 20 32 30 30 36 20 53 65 70 74 65 6d  *.** 2006 Septem
378b2 62 65 72 20 33 30 0a 2a 2a 0a 2a 2a 20 54 68 65  ber 30.**.** The
378b3 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
378b4 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
378b5 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
378b6 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
378b7 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
378b8 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
378b9 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
378ba 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
378bb 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
378bc 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
378bd 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
378be 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
378bf 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
378c0 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
378c1 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
378c2 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
378c3 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
378c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
378c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
378c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
378c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
378c8 2a 2a 2a 2a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  *****.** Impleme
378c9 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 66  ntation of the f
378ca 75 6c 6c 2d 74 65 78 74 2d 73 65 61 72 63 68 20  ull-text-search 
378cb 74 6f 6b 65 6e 69 7a 65 72 20 74 68 61 74 20 69  tokenizer that i
378cc 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 61 20 50  mplements.** a P
378cd 6f 72 74 65 72 20 73 74 65 6d 6d 65 72 2e 0a 2a  orter stemmer..*
378ce 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64  /../*.** The cod
378cf 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69  e in this file i
378d0 73 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 20  s only compiled 
378d1 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20  if:.**.**     * 
378d2 54 68 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20  The FTS3 module 
378d3 69 73 20 62 65 69 6e 67 20 62 75 69 6c 74 20 61  is being built a
378d4 73 20 61 6e 20 65 78 74 65 6e 73 69 6f 6e 0a 2a  s an extension.*
378d5 2a 20 20 20 20 20 20 20 28 69 6e 20 77 68 69 63  *       (in whic
378d6 68 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f  h case SQLITE_CO
378d7 52 45 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65  RE is not define
378d8 64 29 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20  d), or.**.**    
378d9 20 2a 20 54 68 65 20 46 54 53 33 20 6d 6f 64 75   * The FTS3 modu
378da 6c 65 20 69 73 20 62 65 69 6e 67 20 62 75 69 6c  le is being buil
378db 74 20 69 6e 74 6f 20 74 68 65 20 63 6f 72 65 20  t into the core 
378dc 6f 66 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 69  of.**       SQLi
378dd 74 65 20 28 69 6e 20 77 68 69 63 68 20 63 61 73  te (in which cas
378de 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  e SQLITE_ENABLE_
378df 46 54 53 33 20 69 73 20 64 65 66 69 6e 65 64 29  FTS3 is defined)
378e0 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65  ..*/.#if !define
378e1 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c  d(SQLITE_CORE) |
378e2 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
378e3 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 0a 0a 0a  _ENABLE_FTS3)...
378e4 0a 0a 2f 2a 0a 2a 2a 20 43 6c 61 73 73 20 64 65  ../*.** Class de
378e5 72 69 76 65 64 20 66 72 6f 6d 20 73 71 6c 69 74  rived from sqlit
378e6 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a  e3_tokenizer.*/.
378e7 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 70  typedef struct p
378e8 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 20  orter_tokenizer 
378e9 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  {.  sqlite3_toke
378ea 6e 69 7a 65 72 20 62 61 73 65 3b 20 20 20 20 20  nizer base;     
378eb 20 2f 2a 20 42 61 73 65 20 63 6c 61 73 73 20 2a   /* Base class *
378ec 2f 0a 7d 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e  /.} porter_token
378ed 69 7a 65 72 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6c 61  izer;../*.** Cla
378ee 73 73 20 64 65 72 69 76 65 64 20 66 72 6f 6d 20  ss derived from 
378ef 73 71 6c 69 74 33 5f 74 6f 6b 65 6e 69 7a 65 72  sqlit3_tokenizer
378f0 5f 63 75 72 73 6f 72 0a 2a 2f 0a 74 79 70 65 64  _cursor.*/.typed
378f1 65 66 20 73 74 72 75 63 74 20 70 6f 72 74 65 72  ef struct porter
378f2 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
378f3 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f  r {.  sqlite3_to
378f4 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 62  kenizer_cursor b
378f5 61 73 65 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  ase;.  const cha
378f6 72 20 2a 7a 49 6e 70 75 74 3b 20 20 20 20 20 20  r *zInput;      
378f7 20 20 20 20 2f 2a 20 69 6e 70 75 74 20 77 65 20      /* input we 
378f8 61 72 65 20 74 6f 6b 65 6e 69 7a 69 6e 67 20 2a  are tokenizing *
378f9 2f 0a 20 20 69 6e 74 20 6e 49 6e 70 75 74 3b 20  /.  int nInput; 
378fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
378fb 20 2f 2a 20 73 69 7a 65 20 6f 66 20 74 68 65 20   /* size of the 
378fc 69 6e 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20 69  input */.  int i
378fd 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20  Offset;         
378fe 20 20 20 20 20 20 20 20 2f 2a 20 63 75 72 72 65          /* curre
378ff 6e 74 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 7a  nt position in z
37900 49 6e 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20 69  Input */.  int i
37901 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20  Token;          
37902 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 64 65 78          /* index
37903 20 6f 66 20 6e 65 78 74 20 74 6f 6b 65 6e 20 74   of next token t
37904 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20 2a 2f  o be returned */
37905 0a 20 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 3b  .  char *zToken;
37906 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37907 2f 2a 20 73 74 6f 72 61 67 65 20 66 6f 72 20 63  /* storage for c
37908 75 72 72 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a  urrent token */.
37909 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 61 74 65 64    int nAllocated
3790a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
3790b 2a 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65  * space allocate
3790c 64 20 74 6f 20 7a 54 6f 6b 65 6e 20 62 75 66 66  d to zToken buff
3790d 65 72 20 2a 2f 0a 7d 20 70 6f 72 74 65 72 5f 74  er */.} porter_t
3790e 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 3b  okenizer_cursor;
3790f 0a 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65  .../* Forward de
37910 63 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a 73 74 61  claration */.sta
37911 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  tic const sqlite
37912 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
37913 6c 65 20 70 6f 72 74 65 72 54 6f 6b 65 6e 69 7a  le porterTokeniz
37914 65 72 4d 6f 64 75 6c 65 3b 0a 0a 0a 2f 2a 0a 2a  erModule;.../*.*
37915 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 74  * Create a new t
37916 6f 6b 65 6e 69 7a 65 72 20 69 6e 73 74 61 6e 63  okenizer instanc
37917 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
37918 20 70 6f 72 74 65 72 43 72 65 61 74 65 28 0a 20   porterCreate(. 
37919 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74   int argc, const
3791a 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 2a 61   char * const *a
3791b 72 67 76 2c 0a 20 20 73 71 6c 69 74 65 33 5f 74  rgv,.  sqlite3_t
3791c 6f 6b 65 6e 69 7a 65 72 20 2a 2a 70 70 54 6f 6b  okenizer **ppTok
3791d 65 6e 69 7a 65 72 0a 29 7b 0a 20 20 70 6f 72 74  enizer.){.  port
3791e 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 74 3b  er_tokenizer *t;
3791f 0a 20 20 74 20 3d 20 28 70 6f 72 74 65 72 5f 74  .  t = (porter_t
37920 6f 6b 65 6e 69 7a 65 72 20 2a 29 20 73 71 6c 69  okenizer *) sqli
37921 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f  te3_malloc(sizeo
37922 66 28 2a 74 29 29 3b 0a 20 20 69 66 28 20 74 3d  f(*t));.  if( t=
37923 3d 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 20 53  =NULL ) return S
37924 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 6d  QLITE_NOMEM;.  m
37925 65 6d 73 65 74 28 74 2c 20 30 2c 20 73 69 7a 65  emset(t, 0, size
37926 6f 66 28 2a 74 29 29 3b 0a 20 20 2a 70 70 54 6f  of(*t));.  *ppTo
37927 6b 65 6e 69 7a 65 72 20 3d 20 26 74 2d 3e 62 61  kenizer = &t->ba
37928 73 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  se;.  return SQL
37929 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
3792a 20 44 65 73 74 72 6f 79 20 61 20 74 6f 6b 65 6e   Destroy a token
3792b 69 7a 65 72 0a 2a 2f 0a 73 74 61 74 69 63 20 69  izer.*/.static i
3792c 6e 74 20 70 6f 72 74 65 72 44 65 73 74 72 6f 79  nt porterDestroy
3792d 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a  (sqlite3_tokeniz
3792e 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 29 7b  er *pTokenizer){
3792f 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
37930 70 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 20 20 72  pTokenizer);.  r
37931 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
37932 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61 72  .}../*.** Prepar
37933 65 20 74 6f 20 62 65 67 69 6e 20 74 6f 6b 65 6e  e to begin token
37934 69 7a 69 6e 67 20 61 20 70 61 72 74 69 63 75 6c  izing a particul
37935 61 72 20 73 74 72 69 6e 67 2e 20 20 54 68 65 20  ar string.  The 
37936 69 6e 70 75 74 0a 2a 2a 20 73 74 72 69 6e 67 20  input.** string 
37937 74 6f 20 62 65 20 74 6f 6b 65 6e 69 7a 65 64 20  to be tokenized 
37938 69 73 20 7a 49 6e 70 75 74 5b 30 2e 2e 6e 49 6e  is zInput[0..nIn
37939 70 75 74 2d 31 5d 2e 20 20 41 20 63 75 72 73 6f  put-1].  A curso
3793a 72 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69 6e 63  r.** used to inc
3793b 72 65 6d 65 6e 74 61 6c 6c 79 20 74 6f 6b 65 6e  rementally token
3793c 69 7a 65 20 74 68 69 73 20 73 74 72 69 6e 67 20  ize this string 
3793d 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20 0a  is returned in .
3793e 2a 2a 20 2a 70 70 43 75 72 73 6f 72 2e 0a 2a 2f  ** *ppCursor..*/
3793f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6f 72 74  .static int port
37940 65 72 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65  erOpen(.  sqlite
37941 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f  3_tokenizer *pTo
37942 6b 65 6e 69 7a 65 72 2c 20 20 20 20 20 20 20 20  kenizer,        
37943 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 69 7a 65   /* The tokenize
37944 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  r */.  const cha
37945 72 20 2a 7a 49 6e 70 75 74 2c 20 69 6e 74 20 6e  r *zInput, int n
37946 49 6e 70 75 74 2c 20 20 20 20 20 20 20 20 2f 2a  Input,        /*
37947 20 53 74 72 69 6e 67 20 74 6f 20 62 65 20 74 6f   String to be to
37948 6b 65 6e 69 7a 65 64 20 2a 2f 0a 20 20 73 71 6c  kenized */.  sql
37949 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
3794a 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72  ursor **ppCursor
3794b 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f 6b 65      /* OUT: Toke
3794c 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72 20  nization cursor 
3794d 2a 2f 0a 29 7b 0a 20 20 70 6f 72 74 65 72 5f 74  */.){.  porter_t
3794e 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
3794f 2a 63 3b 0a 0a 20 20 63 20 3d 20 28 70 6f 72 74  *c;..  c = (port
37950 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  er_tokenizer_cur
37951 73 6f 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 6d  sor *) sqlite3_m
37952 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 63 29  alloc(sizeof(*c)
37953 29 3b 0a 20 20 69 66 28 20 63 3d 3d 4e 55 4c 4c  );.  if( c==NULL
37954 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
37955 5f 4e 4f 4d 45 4d 3b 0a 0a 20 20 63 2d 3e 7a 49  _NOMEM;..  c->zI
37956 6e 70 75 74 20 3d 20 7a 49 6e 70 75 74 3b 0a 20  nput = zInput;. 
37957 20 69 66 28 20 7a 49 6e 70 75 74 3d 3d 30 20 29   if( zInput==0 )
37958 7b 0a 20 20 20 20 63 2d 3e 6e 49 6e 70 75 74 20  {.    c->nInput 
37959 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  = 0;.  }else if(
3795a 20 6e 49 6e 70 75 74 3c 30 20 29 7b 0a 20 20 20   nInput<0 ){.   
3795b 20 63 2d 3e 6e 49 6e 70 75 74 20 3d 20 28 69 6e   c->nInput = (in
3795c 74 29 73 74 72 6c 65 6e 28 7a 49 6e 70 75 74 29  t)strlen(zInput)
3795d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63  ;.  }else{.    c
3795e 2d 3e 6e 49 6e 70 75 74 20 3d 20 6e 49 6e 70 75  ->nInput = nInpu
3795f 74 3b 0a 20 20 7d 0a 20 20 63 2d 3e 69 4f 66 66  t;.  }.  c->iOff
37960 73 65 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20  set = 0;        
37961 20 20 20 20 20 20 20 20 20 2f 2a 20 73 74 61 72           /* star
37962 74 20 74 6f 6b 65 6e 69 7a 69 6e 67 20 61 74 20  t tokenizing at 
37963 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 2a 2f  the beginning */
37964 0a 20 20 63 2d 3e 69 54 6f 6b 65 6e 20 3d 20 30  .  c->iToken = 0
37965 3b 0a 20 20 63 2d 3e 7a 54 6f 6b 65 6e 20 3d 20  ;.  c->zToken = 
37966 4e 55 4c 4c 3b 20 20 20 20 20 20 20 20 20 20 20  NULL;           
37967 20 20 20 20 2f 2a 20 6e 6f 20 73 70 61 63 65 20      /* no space 
37968 61 6c 6c 6f 63 61 74 65 64 2c 20 79 65 74 2e 20  allocated, yet. 
37969 2a 2f 0a 20 20 63 2d 3e 6e 41 6c 6c 6f 63 61 74  */.  c->nAllocat
3796a 65 64 20 3d 20 30 3b 0a 0a 20 20 2a 70 70 43 75  ed = 0;..  *ppCu
3796b 72 73 6f 72 20 3d 20 26 63 2d 3e 62 61 73 65 3b  rsor = &c->base;
3796c 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
3796d 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c  _OK;.}../*.** Cl
3796e 6f 73 65 20 61 20 74 6f 6b 65 6e 69 7a 61 74 69  ose a tokenizati
3796f 6f 6e 20 63 75 72 73 6f 72 20 70 72 65 76 69 6f  on cursor previo
37970 75 73 6c 79 20 6f 70 65 6e 65 64 20 62 79 20 61  usly opened by a
37971 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 70 6f 72 74   call to.** port
37972 65 72 4f 70 65 6e 28 29 20 61 62 6f 76 65 2e 0a  erOpen() above..
37973 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6f  */.static int po
37974 72 74 65 72 43 6c 6f 73 65 28 73 71 6c 69 74 65  rterClose(sqlite
37975 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
37976 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20  or *pCursor){.  
37977 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72  porter_tokenizer
37978 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20 28 70 6f  _cursor *c = (po
37979 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  rter_tokenizer_c
3797a 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73 6f 72  ursor *) pCursor
3797b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
3797c 28 63 2d 3e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 73  (c->zToken);.  s
3797d 71 6c 69 74 65 33 5f 66 72 65 65 28 63 29 3b 0a  qlite3_free(c);.
3797e 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3797f 4f 4b 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 56 6f 77 65  OK;.}./*.** Vowe
37980 6c 20 6f 72 20 63 6f 6e 73 6f 6e 61 6e 74 0a 2a  l or consonant.*
37981 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  /.static const c
37982 68 61 72 20 63 54 79 70 65 5b 5d 20 3d 20 7b 0a  har cType[] = {.
37983 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 30     0, 1, 1, 1, 0
37984 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c  , 1, 1, 1, 0, 1,
37985 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20   1, 1, 1, 1, 0, 
37986 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30  1, 1, 1, 1, 1, 0
37987 2c 0a 20 20 20 31 2c 20 31 2c 20 31 2c 20 32 2c  ,.   1, 1, 1, 2,
37988 20 31 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 69 73 43   1.};../*.** isC
37989 6f 6e 73 6f 6e 61 6e 74 28 29 20 61 6e 64 20 69  onsonant() and i
3798a 73 56 6f 77 65 6c 28 29 20 64 65 74 65 72 6d 69  sVowel() determi
3798b 6e 65 20 69 66 20 74 68 65 69 72 20 66 69 72 73  ne if their firs
3798c 74 20 63 68 61 72 61 63 74 65 72 20 69 6e 0a 2a  t character in.*
3798d 2a 20 74 68 65 20 73 74 72 69 6e 67 20 74 68 65  * the string the
3798e 79 20 70 6f 69 6e 74 20 74 6f 20 69 73 20 61 20  y point to is a 
3798f 63 6f 6e 73 6f 6e 61 6e 74 20 6f 72 20 61 20 76  consonant or a v
37990 6f 77 65 6c 2c 20 61 63 63 6f 72 64 69 6e 67 0a  owel, according.
37991 2a 2a 20 74 6f 20 50 6f 72 74 65 72 20 72 75 6c  ** to Porter rul
37992 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 41 20 63 6f 6e  s.  .**.** A con
37993 73 6f 6e 61 74 65 20 69 73 20 61 6e 79 20 6c 65  sonate is any le
37994 74 74 65 72 20 6f 74 68 65 72 20 74 68 61 6e 20  tter other than 
37995 27 61 27 2c 20 27 65 27 2c 20 27 69 27 2c 20 27  'a', 'e', 'i', '
37996 6f 27 2c 20 6f 72 20 27 75 27 2e 0a 2a 2a 20 27  o', or 'u'..** '
37997 59 27 20 69 73 20 61 20 63 6f 6e 73 6f 6e 61 6e  Y' is a consonan
37998 74 20 75 6e 6c 65 73 73 20 69 74 20 66 6f 6c 6c  t unless it foll
37999 6f 77 73 20 61 6e 6f 74 68 65 72 20 63 6f 6e 73  ows another cons
3799a 6f 6e 61 6e 74 2c 0a 2a 2a 20 69 6e 20 77 68 69  onant,.** in whi
3799b 63 68 20 63 61 73 65 20 69 74 20 69 73 20 61 20  ch case it is a 
3799c 76 6f 77 65 6c 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20  vowel..**.** In 
3799d 74 68 65 73 65 20 72 6f 75 74 69 6e 65 2c 20 74  these routine, t
3799e 68 65 20 6c 65 74 74 65 72 73 20 61 72 65 20 69  he letters are i
3799f 6e 20 72 65 76 65 72 73 65 20 6f 72 64 65 72 2e  n reverse order.
379a0 20 20 53 6f 20 74 68 65 20 27 79 27 20 72 75 6c    So the 'y' rul
379a1 65 0a 2a 2a 20 69 73 20 74 68 61 74 20 27 79 27  e.** is that 'y'
379a2 20 69 73 20 61 20 63 6f 6e 73 6f 6e 61 6e 74 20   is a consonant 
379a3 75 6e 6c 65 73 73 20 69 74 20 69 73 20 66 6f 6c  unless it is fol
379a4 6c 6f 77 65 64 20 62 79 20 61 6e 6f 74 68 65 72  lowed by another
379a5 0a 2a 2a 20 63 6f 6e 73 6f 6e 65 6e 74 2e 0a 2a  .** consonent..*
379a6 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 56  /.static int isV
379a7 6f 77 65 6c 28 63 6f 6e 73 74 20 63 68 61 72 2a  owel(const char*
379a8 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73  );.static int is
379a9 43 6f 6e 73 6f 6e 61 6e 74 28 63 6f 6e 73 74 20  Consonant(const 
379aa 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 6e 74 20  char *z){.  int 
379ab 6a 3b 0a 20 20 63 68 61 72 20 78 20 3d 20 2a 7a  j;.  char x = *z
379ac 3b 0a 20 20 69 66 28 20 78 3d 3d 30 20 29 20 72  ;.  if( x==0 ) r
379ad 65 74 75 72 6e 20 30 3b 0a 20 20 61 73 73 65 72  eturn 0;.  asser
379ae 74 28 20 78 3e 3d 27 61 27 20 26 26 20 78 3c 3d  t( x>='a' && x<=
379af 27 7a 27 20 29 3b 0a 20 20 6a 20 3d 20 63 54 79  'z' );.  j = cTy
379b0 70 65 5b 78 2d 27 61 27 5d 3b 0a 20 20 69 66 28  pe[x-'a'];.  if(
379b1 20 6a 3c 32 20 29 20 72 65 74 75 72 6e 20 6a 3b   j<2 ) return j;
379b2 0a 20 20 72 65 74 75 72 6e 20 7a 5b 31 5d 3d 3d  .  return z[1]==
379b3 30 20 7c 7c 20 69 73 56 6f 77 65 6c 28 7a 20 2b  0 || isVowel(z +
379b4 20 31 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e   1);.}.static in
379b5 74 20 69 73 56 6f 77 65 6c 28 63 6f 6e 73 74 20  t isVowel(const 
379b6 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 6e 74 20  char *z){.  int 
379b7 6a 3b 0a 20 20 63 68 61 72 20 78 20 3d 20 2a 7a  j;.  char x = *z
379b8 3b 0a 20 20 69 66 28 20 78 3d 3d 30 20 29 20 72  ;.  if( x==0 ) r
379b9 65 74 75 72 6e 20 30 3b 0a 20 20 61 73 73 65 72  eturn 0;.  asser
379ba 74 28 20 78 3e 3d 27 61 27 20 26 26 20 78 3c 3d  t( x>='a' && x<=
379bb 27 7a 27 20 29 3b 0a 20 20 6a 20 3d 20 63 54 79  'z' );.  j = cTy
379bc 70 65 5b 78 2d 27 61 27 5d 3b 0a 20 20 69 66 28  pe[x-'a'];.  if(
379bd 20 6a 3c 32 20 29 20 72 65 74 75 72 6e 20 31 2d   j<2 ) return 1-
379be 6a 3b 0a 20 20 72 65 74 75 72 6e 20 69 73 43 6f  j;.  return isCo
379bf 6e 73 6f 6e 61 6e 74 28 7a 20 2b 20 31 29 3b 0a  nsonant(z + 1);.
379c0 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 65 74 20 61 6e 79  }../*.** Let any
379c1 20 73 65 71 75 65 6e 63 65 20 6f 66 20 6f 6e 65   sequence of one
379c2 20 6f 72 20 6d 6f 72 65 20 76 6f 77 65 6c 73 20   or more vowels 
379c3 62 65 20 72 65 70 72 65 73 65 6e 74 65 64 20 62  be represented b
379c4 79 20 56 20 61 6e 64 20 6c 65 74 0a 2a 2a 20 43  y V and let.** C
379c5 20 62 65 20 73 65 71 75 65 6e 63 65 20 6f 66 20   be sequence of 
379c6 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 63 6f 6e 73  one or more cons
379c7 6f 6e 61 6e 74 73 2e 20 20 54 68 65 6e 20 65 76  onants.  Then ev
379c8 65 72 79 20 77 6f 72 64 20 63 61 6e 20 62 65 0a  ery word can be.
379c9 2a 2a 20 72 65 70 72 65 73 65 6e 74 65 64 20 61  ** represented a
379ca 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  s:.**.**        
379cb 20 20 20 5b 43 5d 20 28 56 43 29 7b 6d 7d 20 5b     [C] (VC){m} [
379cc 56 5d 0a 2a 2a 0a 2a 2a 20 49 6e 20 70 72 6f 73  V].**.** In pros
379cd 65 3a 20 20 41 20 77 6f 72 64 20 69 73 20 61 6e  e:  A word is an
379ce 20 6f 70 74 69 6f 6e 61 6c 20 63 6f 6e 73 6f 6e   optional conson
379cf 61 6e 74 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20  ant followed by 
379d0 7a 65 72 6f 20 6f 72 0a 2a 2a 20 76 6f 77 65 6c  zero or.** vowel
379d1 2d 63 6f 6e 73 6f 6e 61 6e 74 20 70 61 69 72 73  -consonant pairs
379d2 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 6e 20   followed by an 
379d3 6f 70 74 69 6f 6e 61 6c 20 76 6f 77 65 6c 2e 20  optional vowel. 
379d4 20 22 6d 22 20 69 73 20 74 68 65 0a 2a 2a 20 6e   "m" is the.** n
379d5 75 6d 62 65 72 20 6f 66 20 76 6f 77 65 6c 20 63  umber of vowel c
379d6 6f 6e 73 6f 6e 61 6e 74 20 70 61 69 72 73 2e 20  onsonant pairs. 
379d7 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6f   This routine co
379d8 6d 70 75 74 65 73 20 74 68 65 20 76 61 6c 75 65  mputes the value
379d9 0a 2a 2a 20 6f 66 20 6d 20 66 6f 72 20 74 68 65  .** of m for the
379da 20 66 69 72 73 74 20 69 20 62 79 74 65 73 20 6f   first i bytes o
379db 66 20 61 20 77 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20  f a word..**.** 
379dc 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74  Return true if t
379dd 68 65 20 6d 2d 76 61 6c 75 65 20 66 6f 72 20 7a  he m-value for z
379de 20 69 73 20 31 20 6f 72 20 6d 6f 72 65 2e 20 20   is 1 or more.  
379df 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 0a  In other words,.
379e0 2a 2a 20 72 65 74 75 72 6e 20 74 72 75 65 20 69  ** return true i
379e1 66 20 7a 20 63 6f 6e 74 61 69 6e 73 20 61 74 20  f z contains at 
379e2 6c 65 61 73 74 20 6f 6e 65 20 76 6f 77 65 6c 20  least one vowel 
379e3 74 68 61 74 20 69 73 20 66 6f 6c 6c 6f 77 65 64  that is followed
379e4 0a 2a 2a 20 62 79 20 61 20 63 6f 6e 73 6f 6e 61  .** by a consona
379e5 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 69  nt..**.** In thi
379e6 73 20 72 6f 75 74 69 6e 65 20 7a 5b 5d 20 69 73  s routine z[] is
379e7 20 69 6e 20 72 65 76 65 72 73 65 20 6f 72 64 65   in reverse orde
379e8 72 2e 20 20 53 6f 20 77 65 20 61 72 65 20 72 65  r.  So we are re
379e9 61 6c 6c 79 20 6c 6f 6f 6b 69 6e 67 0a 2a 2a 20  ally looking.** 
379ea 66 6f 72 20 61 6e 20 69 6e 73 74 61 6e 63 65 20  for an instance 
379eb 6f 66 20 6f 66 20 61 20 63 6f 6e 73 6f 6e 61 6e  of of a consonan
379ec 74 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 20  t followed by a 
379ed 76 6f 77 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63  vowel..*/.static
379ee 20 69 6e 74 20 6d 5f 67 74 5f 30 28 63 6f 6e 73   int m_gt_0(cons
379ef 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 77 68  t char *z){.  wh
379f0 69 6c 65 28 20 69 73 56 6f 77 65 6c 28 7a 29 20  ile( isVowel(z) 
379f1 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28 20  ){ z++; }.  if( 
379f2 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  *z==0 ) return 0
379f3 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 43 6f 6e  ;.  while( isCon
379f4 73 6f 6e 61 6e 74 28 7a 29 20 29 7b 20 7a 2b 2b  sonant(z) ){ z++
379f5 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a 21  ; }.  return *z!
379f6 3d 30 3b 0a 7d 0a 0a 2f 2a 20 4c 69 6b 65 20 6d  =0;.}../* Like m
379f7 67 74 30 20 61 62 6f 76 65 20 65 78 63 65 70 74  gt0 above except
379f8 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69 6e 67 20   we are looking 
379f9 66 6f 72 20 61 20 76 61 6c 75 65 20 6f 66 20 6d  for a value of m
379fa 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 65 78 61   which is.** exa
379fb 63 74 6c 79 20 31 0a 2a 2f 0a 73 74 61 74 69 63  ctly 1.*/.static
379fc 20 69 6e 74 20 6d 5f 65 71 5f 31 28 63 6f 6e 73   int m_eq_1(cons
379fd 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 77 68  t char *z){.  wh
379fe 69 6c 65 28 20 69 73 56 6f 77 65 6c 28 7a 29 20  ile( isVowel(z) 
379ff 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28 20  ){ z++; }.  if( 
37a00 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  *z==0 ) return 0
37a01 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 43 6f 6e  ;.  while( isCon
37a02 73 6f 6e 61 6e 74 28 7a 29 20 29 7b 20 7a 2b 2b  sonant(z) ){ z++
37a03 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20  ; }.  if( *z==0 
37a04 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 77 68  ) return 0;.  wh
37a05 69 6c 65 28 20 69 73 56 6f 77 65 6c 28 7a 29 20  ile( isVowel(z) 
37a06 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28 20  ){ z++; }.  if( 
37a07 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31  *z==0 ) return 1
37a08 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 43 6f 6e  ;.  while( isCon
37a09 73 6f 6e 61 6e 74 28 7a 29 20 29 7b 20 7a 2b 2b  sonant(z) ){ z++
37a0a 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a 3d  ; }.  return *z=
37a0b 3d 30 3b 0a 7d 0a 0a 2f 2a 20 4c 69 6b 65 20 6d  =0;.}../* Like m
37a0c 67 74 30 20 61 62 6f 76 65 20 65 78 63 65 70 74  gt0 above except
37a0d 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69 6e 67 20   we are looking 
37a0e 66 6f 72 20 61 20 76 61 6c 75 65 20 6f 66 20 6d  for a value of m
37a0f 3e 31 20 69 6e 73 74 65 61 64 0a 2a 2a 20 6f 72  >1 instead.** or
37a10 20 6d 3e 30 0a 2a 2f 0a 73 74 61 74 69 63 20 69   m>0.*/.static i
37a11 6e 74 20 6d 5f 67 74 5f 31 28 63 6f 6e 73 74 20  nt m_gt_1(const 
37a12 63 68 61 72 20 2a 7a 29 7b 0a 20 20 77 68 69 6c  char *z){.  whil
37a13 65 28 20 69 73 56 6f 77 65 6c 28 7a 29 20 29 7b  e( isVowel(z) ){
37a14 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 2a 7a   z++; }.  if( *z
37a15 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
37a16 20 20 77 68 69 6c 65 28 20 69 73 43 6f 6e 73 6f    while( isConso
37a17 6e 61 6e 74 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20  nant(z) ){ z++; 
37a18 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29 20  }.  if( *z==0 ) 
37a19 72 65 74 75 72 6e 20 30 3b 0a 20 20 77 68 69 6c  return 0;.  whil
37a1a 65 28 20 69 73 56 6f 77 65 6c 28 7a 29 20 29 7b  e( isVowel(z) ){
37a1b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 2a 7a   z++; }.  if( *z
37a1c 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
37a1d 20 20 77 68 69 6c 65 28 20 69 73 43 6f 6e 73 6f    while( isConso
37a1e 6e 61 6e 74 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20  nant(z) ){ z++; 
37a1f 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a 21 3d 30  }.  return *z!=0
37a20 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
37a21 6e 20 54 52 55 45 20 69 66 20 74 68 65 72 65 20  n TRUE if there 
37a22 69 73 20 61 20 76 6f 77 65 6c 20 61 6e 79 77 68  is a vowel anywh
37a23 65 72 65 20 77 69 74 68 69 6e 20 7a 5b 30 2e 2e  ere within z[0..
37a24 6e 2d 31 5d 0a 2a 2f 0a 73 74 61 74 69 63 20 69  n-1].*/.static i
37a25 6e 74 20 68 61 73 56 6f 77 65 6c 28 63 6f 6e 73  nt hasVowel(cons
37a26 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 77 68  t char *z){.  wh
37a27 69 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74  ile( isConsonant
37a28 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20  (z) ){ z++; }.  
37a29 72 65 74 75 72 6e 20 2a 7a 21 3d 30 3b 0a 7d 0a  return *z!=0;.}.
37a2a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52  ./*.** Return TR
37a2b 55 45 20 69 66 20 74 68 65 20 77 6f 72 64 20 65  UE if the word e
37a2c 6e 64 73 20 69 6e 20 61 20 64 6f 75 62 6c 65 20  nds in a double 
37a2d 63 6f 6e 73 6f 6e 61 6e 74 2e 0a 2a 2a 0a 2a 2a  consonant..**.**
37a2e 20 54 68 65 20 74 65 78 74 20 69 73 20 72 65 76   The text is rev
37a2f 65 72 73 65 64 20 68 65 72 65 2e 20 53 6f 20 77  ersed here. So w
37a30 65 20 61 72 65 20 72 65 61 6c 6c 79 20 6c 6f 6f  e are really loo
37a31 6b 69 6e 67 20 61 74 0a 2a 2a 20 74 68 65 20 66  king at.** the f
37a32 69 72 73 74 20 74 77 6f 20 63 68 61 72 61 63 74  irst two charact
37a33 65 72 73 20 6f 66 20 7a 5b 5d 2e 0a 2a 2f 0a 73  ers of z[]..*/.s
37a34 74 61 74 69 63 20 69 6e 74 20 64 6f 75 62 6c 65  tatic int double
37a35 43 6f 6e 73 6f 6e 61 6e 74 28 63 6f 6e 73 74 20  Consonant(const 
37a36 63 68 61 72 20 2a 7a 29 7b 0a 20 20 72 65 74 75  char *z){.  retu
37a37 72 6e 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a  rn isConsonant(z
37a38 29 20 26 26 20 7a 5b 30 5d 3d 3d 7a 5b 31 5d 20  ) && z[0]==z[1] 
37a39 26 26 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a  && isConsonant(z
37a3a 2b 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  +1);.}../*.** Re
37a3b 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65  turn TRUE if the
37a3c 20 77 6f 72 64 20 65 6e 64 73 20 77 69 74 68 20   word ends with 
37a3d 74 68 72 65 65 20 6c 65 74 74 65 72 73 20 77 68  three letters wh
37a3e 69 63 68 0a 2a 2a 20 61 72 65 20 63 6f 6e 73 6f  ich.** are conso
37a3f 6e 61 6e 74 2d 76 6f 77 65 6c 2d 63 6f 6e 73 6f  nant-vowel-conso
37a40 6e 65 6e 74 20 61 6e 64 20 77 68 65 72 65 20 74  nent and where t
37a41 68 65 20 66 69 6e 61 6c 20 63 6f 6e 73 6f 6e 61  he final consona
37a42 6e 74 0a 2a 2a 20 69 73 20 6e 6f 74 20 27 77 27  nt.** is not 'w'
37a43 2c 20 27 78 27 2c 20 6f 72 20 27 79 27 2e 0a 2a  , 'x', or 'y'..*
37a44 2a 0a 2a 2a 20 54 68 65 20 77 6f 72 64 20 69 73  *.** The word is
37a45 20 72 65 76 65 72 73 65 64 20 68 65 72 65 2e 20   reversed here. 
37a46 20 53 6f 20 77 65 20 61 72 65 20 72 65 61 6c 6c   So we are reall
37a47 79 20 63 68 65 63 6b 69 6e 67 20 74 68 65 0a 2a  y checking the.*
37a48 2a 20 66 69 72 73 74 20 74 68 72 65 65 20 6c 65  * first three le
37a49 74 74 65 72 73 20 61 6e 64 20 74 68 65 20 66 69  tters and the fi
37a4a 72 73 74 20 6f 6e 65 20 63 61 6e 6e 6f 74 20 62  rst one cannot b
37a4b 65 20 69 6e 20 5b 77 78 79 5d 2e 0a 2a 2f 0a 73  e in [wxy]..*/.s
37a4c 74 61 74 69 63 20 69 6e 74 20 73 74 61 72 5f 6f  tatic int star_o
37a4d 68 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29  h(const char *z)
37a4e 7b 0a 20 20 72 65 74 75 72 6e 0a 20 20 20 20 7a  {.  return.    z
37a4f 5b 30 5d 21 3d 30 20 26 26 20 69 73 43 6f 6e 73  [0]!=0 && isCons
37a50 6f 6e 61 6e 74 28 7a 29 20 26 26 0a 20 20 20 20  onant(z) &&.    
37a51 7a 5b 30 5d 21 3d 27 77 27 20 26 26 20 7a 5b 30  z[0]!='w' && z[0
37a52 5d 21 3d 27 78 27 20 26 26 20 7a 5b 30 5d 21 3d  ]!='x' && z[0]!=
37a53 27 79 27 20 26 26 0a 20 20 20 20 7a 5b 31 5d 21  'y' &&.    z[1]!
37a54 3d 30 20 26 26 20 69 73 56 6f 77 65 6c 28 7a 2b  =0 && isVowel(z+
37a55 31 29 20 26 26 0a 20 20 20 20 7a 5b 32 5d 21 3d  1) &&.    z[2]!=
37a56 30 20 26 26 20 69 73 43 6f 6e 73 6f 6e 61 6e 74  0 && isConsonant
37a57 28 7a 2b 32 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  (z+2);.}../*.** 
37a58 49 66 20 74 68 65 20 77 6f 72 64 20 65 6e 64 73  If the word ends
37a59 20 77 69 74 68 20 7a 46 72 6f 6d 20 61 6e 64 20   with zFrom and 
37a5a 78 43 6f 6e 64 28 29 20 69 73 20 74 72 75 65 20  xCond() is true 
37a5b 66 6f 72 20 74 68 65 20 73 74 65 6d 0a 2a 2a 20  for the stem.** 
37a5c 6f 66 20 74 68 65 20 77 6f 72 64 20 74 68 61 74  of the word that
37a5d 20 70 72 65 63 65 65 64 73 20 74 68 65 20 7a 46   preceeds the zF
37a5e 72 6f 6d 20 65 6e 64 69 6e 67 2c 20 74 68 65 6e  rom ending, then
37a5f 20 63 68 61 6e 67 65 20 74 68 65 20 0a 2a 2a 20   change the .** 
37a60 65 6e 64 69 6e 67 20 74 6f 20 7a 54 6f 2e 0a 2a  ending to zTo..*
37a61 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 77  *.** The input w
37a62 6f 72 64 20 2a 70 7a 20 61 6e 64 20 7a 46 72 6f  ord *pz and zFro
37a63 6d 20 61 72 65 20 62 6f 74 68 20 69 6e 20 72 65  m are both in re
37a64 76 65 72 73 65 20 6f 72 64 65 72 2e 20 20 7a 54  verse order.  zT
37a65 6f 0a 2a 2a 20 69 73 20 69 6e 20 6e 6f 72 6d 61  o.** is in norma
37a66 6c 20 6f 72 64 65 72 2e 20 0a 2a 2a 0a 2a 2a 20  l order. .**.** 
37a67 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 7a  Return TRUE if z
37a68 46 72 6f 6d 20 6d 61 74 63 68 65 73 2e 20 20 52  From matches.  R
37a69 65 74 75 72 6e 20 46 41 4c 53 45 20 69 66 20 7a  eturn FALSE if z
37a6a 46 72 6f 6d 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a  From does not.**
37a6b 20 6d 61 74 63 68 2e 20 20 4e 6f 74 20 74 68 61   match.  Not tha
37a6c 74 20 54 52 55 45 20 69 73 20 72 65 74 75 72 6e  t TRUE is return
37a6d 65 64 20 65 76 65 6e 20 69 66 20 78 43 6f 6e 64  ed even if xCond
37a6e 28 29 20 66 61 69 6c 73 20 61 6e 64 0a 2a 2a 20  () fails and.** 
37a6f 6e 6f 20 73 75 62 73 74 69 74 75 74 69 6f 6e 20  no substitution 
37a70 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69  occurs..*/.stati
37a71 63 20 69 6e 74 20 73 74 65 6d 28 0a 20 20 63 68  c int stem(.  ch
37a72 61 72 20 2a 2a 70 7a 2c 20 20 20 20 20 20 20 20  ar **pz,        
37a73 20 20 20 20 20 2f 2a 20 54 68 65 20 77 6f 72 64       /* The word
37a74 20 62 65 69 6e 67 20 73 74 65 6d 6d 65 64 20 28   being stemmed (
37a75 52 65 76 65 72 73 65 64 29 20 2a 2f 0a 20 20 63  Reversed) */.  c
37a76 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 72 6f 6d  onst char *zFrom
37a77 2c 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20  ,     /* If the 
37a78 65 6e 64 69 6e 67 20 6d 61 74 63 68 65 73 20 74  ending matches t
37a79 68 69 73 2e 2e 2e 20 28 52 65 76 65 72 73 65 64  his... (Reversed
37a7a 29 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  ) */.  const cha
37a7b 72 20 2a 7a 54 6f 2c 20 20 20 20 20 20 20 2f 2a  r *zTo,       /*
37a7c 20 2e 2e 2e 20 63 68 61 6e 67 65 20 74 68 65 20   ... change the 
37a7d 65 6e 64 69 6e 67 20 74 6f 20 74 68 69 73 20 28  ending to this (
37a7e 6e 6f 74 20 72 65 76 65 72 73 65 64 29 20 2a 2f  not reversed) */
37a7f 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6e 64 29 28  .  int (*xCond)(
37a80 63 6f 6e 73 74 20 63 68 61 72 2a 29 20 20 20 2f  const char*)   /
37a81 2a 20 43 6f 6e 64 69 74 69 6f 6e 20 74 68 61 74  * Condition that
37a82 20 6d 75 73 74 20 62 65 20 74 72 75 65 20 2a 2f   must be true */
37a83 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 20 3d 20  .){.  char *z = 
37a84 2a 70 7a 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a  *pz;.  while( *z
37a85 46 72 6f 6d 20 26 26 20 2a 7a 46 72 6f 6d 3d 3d  From && *zFrom==
37a86 2a 7a 20 29 7b 20 7a 2b 2b 3b 20 7a 46 72 6f 6d  *z ){ z++; zFrom
37a87 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 46 72  ++; }.  if( *zFr
37a88 6f 6d 21 3d 30 20 29 20 72 65 74 75 72 6e 20 30  om!=0 ) return 0
37a89 3b 0a 20 20 69 66 28 20 78 43 6f 6e 64 20 26 26  ;.  if( xCond &&
37a8a 20 21 78 43 6f 6e 64 28 7a 29 20 29 20 72 65 74   !xCond(z) ) ret
37a8b 75 72 6e 20 31 3b 0a 20 20 77 68 69 6c 65 28 20  urn 1;.  while( 
37a8c 2a 7a 54 6f 20 29 7b 0a 20 20 20 20 2a 28 2d 2d  *zTo ){.    *(--
37a8d 7a 29 20 3d 20 2a 28 7a 54 6f 2b 2b 29 3b 0a 20  z) = *(zTo++);. 
37a8e 20 7d 0a 20 20 2a 70 7a 20 3d 20 7a 3b 0a 20 20   }.  *pz = z;.  
37a8f 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a  return 1;.}../*.
37a90 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 66  ** This is the f
37a91 61 6c 6c 62 61 63 6b 20 73 74 65 6d 6d 65 72 20  allback stemmer 
37a92 75 73 65 64 20 77 68 65 6e 20 74 68 65 20 70 6f  used when the po
37a93 72 74 65 72 20 73 74 65 6d 6d 65 72 20 69 73 0a  rter stemmer is.
37a94 2a 2a 20 69 6e 61 70 70 72 6f 70 72 69 61 74 65  ** inappropriate
37a95 2e 20 20 54 68 65 20 69 6e 70 75 74 20 77 6f 72  .  The input wor
37a96 64 20 69 73 20 63 6f 70 69 65 64 20 69 6e 74 6f  d is copied into
37a97 20 74 68 65 20 6f 75 74 70 75 74 20 77 69 74 68   the output with
37a98 0a 2a 2a 20 55 53 2d 41 53 43 49 49 20 63 61 73  .** US-ASCII cas
37a99 65 20 66 6f 6c 64 69 6e 67 2e 20 20 49 66 20 74  e folding.  If t
37a9a 68 65 20 69 6e 70 75 74 20 77 6f 72 64 20 69 73  he input word is
37a9b 20 74 6f 6f 20 6c 6f 6e 67 20 28 6d 6f 72 65 0a   too long (more.
37a9c 2a 2a 20 74 68 61 6e 20 32 30 20 62 79 74 65 73  ** than 20 bytes
37a9d 20 69 66 20 69 74 20 63 6f 6e 74 61 69 6e 73 20   if it contains 
37a9e 6e 6f 20 64 69 67 69 74 73 20 6f 72 20 6d 6f 72  no digits or mor
37a9f 65 20 74 68 61 6e 20 36 20 62 79 74 65 73 20 69  e than 6 bytes i
37aa0 66 0a 2a 2a 20 69 74 20 63 6f 6e 74 61 69 6e 73  f.** it contains
37aa1 20 64 69 67 69 74 73 29 20 74 68 65 6e 20 77 6f   digits) then wo
37aa2 72 64 20 69 73 20 74 72 75 6e 63 61 74 65 64 20  rd is truncated 
37aa3 74 6f 20 32 30 20 6f 72 20 36 20 62 79 74 65 73  to 20 or 6 bytes
37aa4 0a 2a 2a 20 62 79 20 74 61 6b 69 6e 67 20 31 30  .** by taking 10
37aa5 20 6f 72 20 33 20 62 79 74 65 73 20 66 72 6f 6d   or 3 bytes from
37aa6 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 61   the beginning a
37aa7 6e 64 20 65 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69  nd end..*/.stati
37aa8 63 20 76 6f 69 64 20 63 6f 70 79 5f 73 74 65 6d  c void copy_stem
37aa9 6d 65 72 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  mer(const char *
37aaa 7a 49 6e 2c 20 69 6e 74 20 6e 49 6e 2c 20 63 68  zIn, int nIn, ch
37aab 61 72 20 2a 7a 4f 75 74 2c 20 69 6e 74 20 2a 70  ar *zOut, int *p
37aac 6e 4f 75 74 29 7b 0a 20 20 69 6e 74 20 69 2c 20  nOut){.  int i, 
37aad 6d 78 2c 20 6a 3b 0a 20 20 69 6e 74 20 68 61 73  mx, j;.  int has
37aae 44 69 67 69 74 20 3d 20 30 3b 0a 20 20 66 6f 72  Digit = 0;.  for
37aaf 28 69 3d 30 3b 20 69 3c 6e 49 6e 3b 20 69 2b 2b  (i=0; i<nIn; i++
37ab0 29 7b 0a 20 20 20 20 69 6e 74 20 63 20 3d 20 7a  ){.    int c = z
37ab1 49 6e 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 63  In[i];.    if( c
37ab2 3e 3d 27 41 27 20 26 26 20 63 3c 3d 27 5a 27 20  >='A' && c<='Z' 
37ab3 29 7b 0a 20 20 20 20 20 20 7a 4f 75 74 5b 69 5d  ){.      zOut[i]
37ab4 20 3d 20 63 20 2d 20 27 41 27 20 2b 20 27 61 27   = c - 'A' + 'a'
37ab5 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
37ab6 20 20 20 69 66 28 20 63 3e 3d 27 30 27 20 26 26     if( c>='0' &&
37ab7 20 63 3c 3d 27 39 27 20 29 20 68 61 73 44 69 67   c<='9' ) hasDig
37ab8 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 7a 4f  it = 1;.      zO
37ab9 75 74 5b 69 5d 20 3d 20 63 3b 0a 20 20 20 20 7d  ut[i] = c;.    }
37aba 0a 20 20 7d 0a 20 20 6d 78 20 3d 20 68 61 73 44  .  }.  mx = hasD
37abb 69 67 69 74 20 3f 20 33 20 3a 20 31 30 3b 0a 20  igit ? 3 : 10;. 
37abc 20 69 66 28 20 6e 49 6e 3e 6d 78 2a 32 20 29 7b   if( nIn>mx*2 ){
37abd 0a 20 20 20 20 66 6f 72 28 6a 3d 6d 78 2c 20 69  .    for(j=mx, i
37abe 3d 6e 49 6e 2d 6d 78 3b 20 69 3c 6e 49 6e 3b 20  =nIn-mx; i<nIn; 
37abf 69 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  i++, j++){.     
37ac0 20 7a 4f 75 74 5b 6a 5d 20 3d 20 7a 4f 75 74 5b   zOut[j] = zOut[
37ac1 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 20  i];.    }.    i 
37ac2 3d 20 6a 3b 0a 20 20 7d 0a 20 20 7a 4f 75 74 5b  = j;.  }.  zOut[
37ac3 69 5d 20 3d 20 30 3b 0a 20 20 2a 70 6e 4f 75 74  i] = 0;.  *pnOut
37ac4 20 3d 20 69 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20   = i;.}.../*.** 
37ac5 53 74 65 6d 20 74 68 65 20 69 6e 70 75 74 20 77  Stem the input w
37ac6 6f 72 64 20 7a 49 6e 5b 30 2e 2e 6e 49 6e 2d 31  ord zIn[0..nIn-1
37ac7 5d 2e 20 20 53 74 6f 72 65 20 74 68 65 20 6f 75  ].  Store the ou
37ac8 74 70 75 74 20 69 6e 20 7a 4f 75 74 2e 0a 2a 2a  tput in zOut..**
37ac9 20 7a 4f 75 74 20 69 73 20 61 74 20 6c 65 61 73   zOut is at leas
37aca 74 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20  t big enough to 
37acb 68 6f 6c 64 20 6e 49 6e 20 62 79 74 65 73 2e 20  hold nIn bytes. 
37acc 20 57 72 69 74 65 20 74 68 65 20 61 63 74 75 61   Write the actua
37acd 6c 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 74 68 65  l.** size of the
37ace 20 6f 75 74 70 75 74 20 77 6f 72 64 20 28 65 78   output word (ex
37acf 63 6c 75 73 69 76 65 20 6f 66 20 74 68 65 20 27  clusive of the '
37ad0 5c 30 27 20 74 65 72 6d 69 6e 61 74 6f 72 29 20  \0' terminator) 
37ad1 69 6e 74 6f 20 2a 70 6e 4f 75 74 2e 0a 2a 2a 0a  into *pnOut..**.
37ad2 2a 2a 20 41 6e 79 20 75 70 70 65 72 2d 63 61 73  ** Any upper-cas
37ad3 65 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20  e characters in 
37ad4 74 68 65 20 55 53 2d 41 53 43 49 49 20 63 68 61  the US-ASCII cha
37ad5 72 61 63 74 65 72 20 73 65 74 20 28 5b 41 2d 5a  racter set ([A-Z
37ad6 5d 29 0a 2a 2a 20 61 72 65 20 63 6f 6e 76 65 72  ]).** are conver
37ad7 74 65 64 20 74 6f 20 6c 6f 77 65 72 20 63 61 73  ted to lower cas
37ad8 65 2e 20 20 55 70 70 65 72 2d 63 61 73 65 20 55  e.  Upper-case U
37ad9 54 46 20 63 68 61 72 61 63 74 65 72 73 20 61 72  TF characters ar
37ada 65 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64 2e 0a  e.** unchanged..
37adb 2a 2a 0a 2a 2a 20 57 6f 72 64 73 20 74 68 61 74  **.** Words that
37adc 20 61 72 65 20 6c 6f 6e 67 65 72 20 74 68 61 6e   are longer than
37add 20 61 62 6f 75 74 20 32 30 20 62 79 74 65 73 20   about 20 bytes 
37ade 61 72 65 20 73 74 65 6d 6d 65 64 20 62 79 20 72  are stemmed by r
37adf 65 74 61 69 6e 69 6e 67 0a 2a 2a 20 61 20 66 65  etaining.** a fe
37ae0 77 20 62 79 74 65 73 20 66 72 6f 6d 20 74 68 65  w bytes from the
37ae1 20 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64 20 74   beginning and t
37ae2 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 77 6f  he end of the wo
37ae3 72 64 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 77  rd.  If the.** w
37ae4 6f 72 64 20 63 6f 6e 74 61 69 6e 73 20 64 69 67  ord contains dig
37ae5 69 74 73 2c 20 33 20 62 79 74 65 73 20 61 72 65  its, 3 bytes are
37ae6 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20   taken from the 
37ae7 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64 0a 2a 2a  beginning and.**
37ae8 20 33 20 62 79 74 65 73 20 66 72 6f 6d 20 74 68   3 bytes from th
37ae9 65 20 65 6e 64 2e 20 20 46 6f 72 20 6c 6f 6e 67  e end.  For long
37aea 20 77 6f 72 64 73 20 77 69 74 68 6f 75 74 20 64   words without d
37aeb 69 67 69 74 73 2c 20 31 30 20 62 79 74 65 73 0a  igits, 10 bytes.
37aec 2a 2a 20 61 72 65 20 74 61 6b 65 6e 20 66 72 6f  ** are taken fro
37aed 6d 20 65 61 63 68 20 65 6e 64 2e 20 20 55 53 2d  m each end.  US-
37aee 41 53 43 49 49 20 63 61 73 65 20 66 6f 6c 64 69  ASCII case foldi
37aef 6e 67 20 73 74 69 6c 6c 20 61 70 70 6c 69 65 73  ng still applies
37af0 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74 68 65 20  ..** .** If the 
37af1 69 6e 70 75 74 20 77 6f 72 64 20 63 6f 6e 74 61  input word conta
37af2 69 6e 73 20 6e 6f 74 20 64 69 67 69 74 73 20 62  ins not digits b
37af3 75 74 20 64 6f 65 73 20 63 68 61 72 61 63 74 65  ut does characte
37af4 72 73 20 6e 6f 74 20 0a 2a 2a 20 69 6e 20 5b 61  rs not .** in [a
37af5 2d 7a 41 2d 5a 5d 20 74 68 65 6e 20 6e 6f 20 73  -zA-Z] then no s
37af6 74 65 6d 6d 69 6e 67 20 69 73 20 61 74 74 65 6d  temming is attem
37af7 70 74 65 64 20 61 6e 64 20 74 68 69 73 20 72 6f  pted and this ro
37af8 75 74 69 6e 65 20 6a 75 73 74 20 0a 2a 2a 20 63  utine just .** c
37af9 6f 70 69 65 73 20 74 68 65 20 69 6e 70 75 74 20  opies the input 
37afa 69 6e 74 6f 20 74 68 65 20 69 6e 70 75 74 20 69  into the input i
37afb 6e 74 6f 20 74 68 65 20 6f 75 74 70 75 74 20 77  nto the output w
37afc 69 74 68 20 55 53 2d 41 53 43 49 49 0a 2a 2a 20  ith US-ASCII.** 
37afd 63 61 73 65 20 66 6f 6c 64 69 6e 67 2e 0a 2a 2a  case folding..**
37afe 0a 2a 2a 20 53 74 65 6d 6d 69 6e 67 20 6e 65 76  .** Stemming nev
37aff 65 72 20 69 6e 63 72 65 61 73 65 73 20 74 68 65  er increases the
37b00 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 77   length of the w
37b01 6f 72 64 2e 20 20 53 6f 20 74 68 65 72 65 20 69  ord.  So there i
37b02 73 0a 2a 2a 20 6e 6f 20 63 68 61 6e 63 65 20 6f  s.** no chance o
37b03 66 20 6f 76 65 72 66 6c 6f 77 69 6e 67 20 74 68  f overflowing th
37b04 65 20 7a 4f 75 74 20 62 75 66 66 65 72 2e 0a 2a  e zOut buffer..*
37b05 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 6f  /.static void po
37b06 72 74 65 72 5f 73 74 65 6d 6d 65 72 28 63 6f 6e  rter_stemmer(con
37b07 73 74 20 63 68 61 72 20 2a 7a 49 6e 2c 20 69 6e  st char *zIn, in
37b08 74 20 6e 49 6e 2c 20 63 68 61 72 20 2a 7a 4f 75  t nIn, char *zOu
37b09 74 2c 20 69 6e 74 20 2a 70 6e 4f 75 74 29 7b 0a  t, int *pnOut){.
37b0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 63 3b 0a 20    int i, j, c;. 
37b0b 20 63 68 61 72 20 7a 52 65 76 65 72 73 65 5b 32   char zReverse[2
37b0c 38 5d 3b 0a 20 20 63 68 61 72 20 2a 7a 2c 20 2a  8];.  char *z, *
37b0d 7a 32 3b 0a 20 20 69 66 28 20 6e 49 6e 3c 33 20  z2;.  if( nIn<3 
37b0e 7c 7c 20 6e 49 6e 3e 3d 73 69 7a 65 6f 66 28 7a  || nIn>=sizeof(z
37b0f 52 65 76 65 72 73 65 29 2d 37 20 29 7b 0a 20 20  Reverse)-7 ){.  
37b10 20 20 2f 2a 20 54 68 65 20 77 6f 72 64 20 69 73    /* The word is
37b11 20 74 6f 6f 20 62 69 67 20 6f 72 20 74 6f 6f 20   too big or too 
37b12 73 6d 61 6c 6c 20 66 6f 72 20 74 68 65 20 70 6f  small for the po
37b13 72 74 65 72 20 73 74 65 6d 6d 65 72 2e 0a 20 20  rter stemmer..  
37b14 20 20 2a 2a 20 46 61 6c 6c 62 61 63 6b 20 74 6f    ** Fallback to
37b15 20 74 68 65 20 63 6f 70 79 20 73 74 65 6d 6d 65   the copy stemme
37b16 72 20 2a 2f 0a 20 20 20 20 63 6f 70 79 5f 73 74  r */.    copy_st
37b17 65 6d 6d 65 72 28 7a 49 6e 2c 20 6e 49 6e 2c 20  emmer(zIn, nIn, 
37b18 7a 4f 75 74 2c 20 70 6e 4f 75 74 29 3b 0a 20 20  zOut, pnOut);.  
37b19 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
37b1a 66 6f 72 28 69 3d 30 2c 20 6a 3d 73 69 7a 65 6f  for(i=0, j=sizeo
37b1b 66 28 7a 52 65 76 65 72 73 65 29 2d 36 3b 20 69  f(zReverse)-6; i
37b1c 3c 6e 49 6e 3b 20 69 2b 2b 2c 20 6a 2d 2d 29 7b  <nIn; i++, j--){
37b1d 0a 20 20 20 20 63 20 3d 20 7a 49 6e 5b 69 5d 3b  .    c = zIn[i];
37b1e 0a 20 20 20 20 69 66 28 20 63 3e 3d 27 41 27 20  .    if( c>='A' 
37b1f 26 26 20 63 3c 3d 27 5a 27 20 29 7b 0a 20 20 20  && c<='Z' ){.   
37b20 20 20 20 7a 52 65 76 65 72 73 65 5b 6a 5d 20 3d     zReverse[j] =
37b21 20 63 20 2b 20 27 61 27 20 2d 20 27 41 27 3b 0a   c + 'a' - 'A';.
37b22 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3e      }else if( c>
37b23 3d 27 61 27 20 26 26 20 63 3c 3d 27 7a 27 20 29  ='a' && c<='z' )
37b24 7b 0a 20 20 20 20 20 20 7a 52 65 76 65 72 73 65  {.      zReverse
37b25 5b 6a 5d 20 3d 20 63 3b 0a 20 20 20 20 7d 65 6c  [j] = c;.    }el
37b26 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65  se{.      /* The
37b27 20 75 73 65 20 6f 66 20 61 20 63 68 61 72 61 63   use of a charac
37b28 74 65 72 20 6e 6f 74 20 69 6e 20 5b 61 2d 7a 41  ter not in [a-zA
37b29 2d 5a 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 77  -Z] means that w
37b2a 65 20 66 61 6c 6c 62 61 63 6b 0a 20 20 20 20 20  e fallback.     
37b2b 20 2a 2a 20 74 6f 20 74 68 65 20 63 6f 70 79 20   ** to the copy 
37b2c 73 74 65 6d 6d 65 72 20 2a 2f 0a 20 20 20 20 20  stemmer */.     
37b2d 20 63 6f 70 79 5f 73 74 65 6d 6d 65 72 28 7a 49   copy_stemmer(zI
37b2e 6e 2c 20 6e 49 6e 2c 20 7a 4f 75 74 2c 20 70 6e  n, nIn, zOut, pn
37b2f 4f 75 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75  Out);.      retu
37b30 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  rn;.    }.  }.  
37b31 6d 65 6d 73 65 74 28 26 7a 52 65 76 65 72 73 65  memset(&zReverse
37b32 5b 73 69 7a 65 6f 66 28 7a 52 65 76 65 72 73 65  [sizeof(zReverse
37b33 29 2d 35 5d 2c 20 30 2c 20 35 29 3b 0a 20 20 7a  )-5], 0, 5);.  z
37b34 20 3d 20 26 7a 52 65 76 65 72 73 65 5b 6a 2b 31   = &zReverse[j+1
37b35 5d 3b 0a 0a 0a 20 20 2f 2a 20 53 74 65 70 20 31  ];...  /* Step 1
37b36 61 20 2a 2f 0a 20 20 69 66 28 20 7a 5b 30 5d 3d  a */.  if( z[0]=
37b37 3d 27 73 27 20 29 7b 0a 20 20 20 20 69 66 28 0a  ='s' ){.    if(.
37b38 20 20 20 20 20 21 73 74 65 6d 28 26 7a 2c 20 22       !stem(&z, "
37b39 73 65 73 73 22 2c 20 22 73 73 22 2c 20 30 29 20  sess", "ss", 0) 
37b3a 26 26 0a 20 20 20 20 20 21 73 74 65 6d 28 26 7a  &&.     !stem(&z
37b3b 2c 20 22 73 65 69 22 2c 20 22 69 22 2c 20 30 29  , "sei", "i", 0)
37b3c 20 20 26 26 0a 20 20 20 20 20 21 73 74 65 6d 28    &&.     !stem(
37b3d 26 7a 2c 20 22 73 73 22 2c 20 22 73 73 22 2c 20  &z, "ss", "ss", 
37b3e 30 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  0).    ){.      
37b3f 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  z++;.    }.  }..
37b40 20 20 2f 2a 20 53 74 65 70 20 31 62 20 2a 2f 20    /* Step 1b */ 
37b41 20 0a 20 20 7a 32 20 3d 20 7a 3b 0a 20 20 69 66   .  z2 = z;.  if
37b42 28 20 73 74 65 6d 28 26 7a 2c 20 22 64 65 65 22  ( stem(&z, "dee"
37b43 2c 20 22 65 65 22 2c 20 6d 5f 67 74 5f 30 29 20  , "ee", m_gt_0) 
37b44 29 7b 0a 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74  ){.    /* Do not
37b45 68 69 6e 67 2e 20 20 54 68 65 20 77 6f 72 6b 20  hing.  The work 
37b46 77 61 73 20 61 6c 6c 20 69 6e 20 74 68 65 20 74  was all in the t
37b47 65 73 74 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69  est */.  }else i
37b48 66 28 20 0a 20 20 20 20 20 28 73 74 65 6d 28 26  f( .     (stem(&
37b49 7a 2c 20 22 67 6e 69 22 2c 20 22 22 2c 20 68 61  z, "gni", "", ha
37b4a 73 56 6f 77 65 6c 29 20 7c 7c 20 73 74 65 6d 28  sVowel) || stem(
37b4b 26 7a 2c 20 22 64 65 22 2c 20 22 22 2c 20 68 61  &z, "de", "", ha
37b4c 73 56 6f 77 65 6c 29 29 0a 20 20 20 20 20 20 26  sVowel)).      &
37b4d 26 20 7a 21 3d 7a 32 0a 20 20 29 7b 0a 20 20 20  & z!=z2.  ){.   
37b4e 20 20 69 66 28 20 73 74 65 6d 28 26 7a 2c 20 22    if( stem(&z, "
37b4f 74 61 22 2c 20 22 61 74 65 22 2c 20 30 29 20 7c  ta", "ate", 0) |
37b50 7c 0a 20 20 20 20 20 20 20 20 20 73 74 65 6d 28  |.         stem(
37b51 26 7a 2c 20 22 6c 62 22 2c 20 22 62 6c 65 22 2c  &z, "lb", "ble",
37b52 20 30 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 20   0) ||.         
37b53 73 74 65 6d 28 26 7a 2c 20 22 7a 69 22 2c 20 22  stem(&z, "zi", "
37b54 69 7a 65 22 2c 20 30 29 20 29 7b 0a 20 20 20 20  ize", 0) ){.    
37b55 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e 67     /* Do nothing
37b56 2e 20 20 54 68 65 20 77 6f 72 6b 20 77 61 73 20  .  The work was 
37b57 61 6c 6c 20 69 6e 20 74 68 65 20 74 65 73 74 20  all in the test 
37b58 2a 2f 0a 20 20 20 20 20 7d 65 6c 73 65 20 69 66  */.     }else if
37b59 28 20 64 6f 75 62 6c 65 43 6f 6e 73 6f 6e 61 6e  ( doubleConsonan
37b5a 74 28 7a 29 20 26 26 20 28 2a 7a 21 3d 27 6c 27  t(z) && (*z!='l'
37b5b 20 26 26 20 2a 7a 21 3d 27 73 27 20 26 26 20 2a   && *z!='s' && *
37b5c 7a 21 3d 27 7a 27 29 20 29 7b 0a 20 20 20 20 20  z!='z') ){.     
37b5d 20 20 7a 2b 2b 3b 0a 20 20 20 20 20 7d 65 6c 73    z++;.     }els
37b5e 65 20 69 66 28 20 6d 5f 65 71 5f 31 28 7a 29 20  e if( m_eq_1(z) 
37b5f 26 26 20 73 74 61 72 5f 6f 68 28 7a 29 20 29 7b  && star_oh(z) ){
37b60 0a 20 20 20 20 20 20 20 2a 28 2d 2d 7a 29 20 3d  .       *(--z) =
37b61 20 27 65 27 3b 0a 20 20 20 20 20 7d 0a 20 20 7d   'e';.     }.  }
37b62 0a 0a 20 20 2f 2a 20 53 74 65 70 20 31 63 20 2a  ..  /* Step 1c *
37b63 2f 0a 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 79  /.  if( z[0]=='y
37b64 27 20 26 26 20 68 61 73 56 6f 77 65 6c 28 7a 2b  ' && hasVowel(z+
37b65 31 29 20 29 7b 0a 20 20 20 20 7a 5b 30 5d 20 3d  1) ){.    z[0] =
37b66 20 27 69 27 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20   'i';.  }..  /* 
37b67 53 74 65 70 20 32 20 2a 2f 0a 20 20 73 77 69 74  Step 2 */.  swit
37b68 63 68 28 20 7a 5b 31 5d 20 29 7b 0a 20 20 20 63  ch( z[1] ){.   c
37b69 61 73 65 20 27 61 27 3a 0a 20 20 20 20 20 73 74  ase 'a':.     st
37b6a 65 6d 28 26 7a 2c 20 22 6c 61 6e 6f 69 74 61 22  em(&z, "lanoita"
37b6b 2c 20 22 61 74 65 22 2c 20 6d 5f 67 74 5f 30 29  , "ate", m_gt_0)
37b6c 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a   ||.     stem(&z
37b6d 2c 20 22 6c 61 6e 6f 69 74 22 2c 20 22 74 69 6f  , "lanoit", "tio
37b6e 6e 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20  n", m_gt_0);.   
37b6f 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65    break;.   case
37b70 20 27 63 27 3a 0a 20 20 20 20 20 73 74 65 6d 28   'c':.     stem(
37b71 26 7a 2c 20 22 69 63 6e 65 22 2c 20 22 65 6e 63  &z, "icne", "enc
37b72 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20  e", m_gt_0) ||. 
37b73 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 63      stem(&z, "ic
37b74 6e 61 22 2c 20 22 61 6e 63 65 22 2c 20 6d 5f 67  na", "ance", m_g
37b75 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b  t_0);.     break
37b76 3b 0a 20 20 20 63 61 73 65 20 27 65 27 3a 0a 20  ;.   case 'e':. 
37b77 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 72 65      stem(&z, "re
37b78 7a 69 22 2c 20 22 69 7a 65 22 2c 20 6d 5f 67 74  zi", "ize", m_gt
37b79 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b  _0);.     break;
37b7a 0a 20 20 20 63 61 73 65 20 27 67 27 3a 0a 20 20  .   case 'g':.  
37b7b 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 67 6f     stem(&z, "igo
37b7c 6c 22 2c 20 22 6c 6f 67 22 2c 20 6d 5f 67 74 5f  l", "log", m_gt_
37b7d 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a  0);.     break;.
37b7e 20 20 20 63 61 73 65 20 27 6c 27 3a 0a 20 20 20     case 'l':.   
37b7f 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 6c 62 22    stem(&z, "ilb"
37b80 2c 20 22 62 6c 65 22 2c 20 6d 5f 67 74 5f 30 29  , "ble", m_gt_0)
37b81 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a   ||.     stem(&z
37b82 2c 20 22 69 6c 6c 61 22 2c 20 22 61 6c 22 2c 20  , "illa", "al", 
37b83 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20  m_gt_0) ||.     
37b84 73 74 65 6d 28 26 7a 2c 20 22 69 6c 74 6e 65 22  stem(&z, "iltne"
37b85 2c 20 22 65 6e 74 22 2c 20 6d 5f 67 74 5f 30 29  , "ent", m_gt_0)
37b86 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a   ||.     stem(&z
37b87 2c 20 22 69 6c 65 22 2c 20 22 65 22 2c 20 6d 5f  , "ile", "e", m_
37b88 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74  gt_0) ||.     st
37b89 65 6d 28 26 7a 2c 20 22 69 6c 73 75 6f 22 2c 20  em(&z, "ilsuo", 
37b8a 22 6f 75 73 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a  "ous", m_gt_0);.
37b8b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63       break;.   c
37b8c 61 73 65 20 27 6f 27 3a 0a 20 20 20 20 20 73 74  ase 'o':.     st
37b8d 65 6d 28 26 7a 2c 20 22 6e 6f 69 74 61 7a 69 22  em(&z, "noitazi"
37b8e 2c 20 22 69 7a 65 22 2c 20 6d 5f 67 74 5f 30 29  , "ize", m_gt_0)
37b8f 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a   ||.     stem(&z
37b90 2c 20 22 6e 6f 69 74 61 22 2c 20 22 61 74 65 22  , "noita", "ate"
37b91 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20  , m_gt_0) ||.   
37b92 20 20 73 74 65 6d 28 26 7a 2c 20 22 72 6f 74 61    stem(&z, "rota
37b93 22 2c 20 22 61 74 65 22 2c 20 6d 5f 67 74 5f 30  ", "ate", m_gt_0
37b94 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  );.     break;. 
37b95 20 20 63 61 73 65 20 27 73 27 3a 0a 20 20 20 20    case 's':.    
37b96 20 73 74 65 6d 28 26 7a 2c 20 22 6d 73 69 6c 61   stem(&z, "msila
37b97 22 2c 20 22 61 6c 22 2c 20 6d 5f 67 74 5f 30 29  ", "al", m_gt_0)
37b98 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a   ||.     stem(&z
37b99 2c 20 22 73 73 65 6e 65 76 69 22 2c 20 22 69 76  , "ssenevi", "iv
37b9a 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20  e", m_gt_0) ||. 
37b9b 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 73 73      stem(&z, "ss
37b9c 65 6e 6c 75 66 22 2c 20 22 66 75 6c 22 2c 20 6d  enluf", "ful", m
37b9d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73  _gt_0) ||.     s
37b9e 74 65 6d 28 26 7a 2c 20 22 73 73 65 6e 73 75 6f  tem(&z, "ssensuo
37b9f 22 2c 20 22 6f 75 73 22 2c 20 6d 5f 67 74 5f 30  ", "ous", m_gt_0
37ba0 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  );.     break;. 
37ba1 20 20 63 61 73 65 20 27 74 27 3a 0a 20 20 20 20    case 't':.    
37ba2 20 73 74 65 6d 28 26 7a 2c 20 22 69 74 69 6c 61   stem(&z, "itila
37ba3 22 2c 20 22 61 6c 22 2c 20 6d 5f 67 74 5f 30 29  ", "al", m_gt_0)
37ba4 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a   ||.     stem(&z
37ba5 2c 20 22 69 74 69 76 69 22 2c 20 22 69 76 65 22  , "itivi", "ive"
37ba6 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20  , m_gt_0) ||.   
37ba7 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 74 69 6c    stem(&z, "itil
37ba8 69 62 22 2c 20 22 62 6c 65 22 2c 20 6d 5f 67 74  ib", "ble", m_gt
37ba9 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b  _0);.     break;
37baa 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20  .  }..  /* Step 
37bab 33 20 2a 2f 0a 20 20 73 77 69 74 63 68 28 20 7a  3 */.  switch( z
37bac 5b 30 5d 20 29 7b 0a 20 20 20 63 61 73 65 20 27  [0] ){.   case '
37bad 65 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a  e':.     stem(&z
37bae 2c 20 22 65 74 61 63 69 22 2c 20 22 69 63 22 2c  , "etaci", "ic",
37baf 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20   m_gt_0) ||.    
37bb0 20 73 74 65 6d 28 26 7a 2c 20 22 65 76 69 74 61   stem(&z, "evita
37bb1 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 30 29 20 20  ", "", m_gt_0)  
37bb2 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a   ||.     stem(&z
37bb3 2c 20 22 65 7a 69 6c 61 22 2c 20 22 61 6c 22 2c  , "ezila", "al",
37bb4 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62   m_gt_0);.     b
37bb5 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 69  reak;.   case 'i
37bb6 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c  ':.     stem(&z,
37bb7 20 22 69 74 69 63 69 22 2c 20 22 69 63 22 2c 20   "itici", "ic", 
37bb8 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62 72  m_gt_0);.     br
37bb9 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 6c 27  eak;.   case 'l'
37bba 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20  :.     stem(&z, 
37bbb 22 6c 61 63 69 22 2c 20 22 69 63 22 2c 20 6d 5f  "laci", "ic", m_
37bbc 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74  gt_0) ||.     st
37bbd 65 6d 28 26 7a 2c 20 22 6c 75 66 22 2c 20 22 22  em(&z, "luf", ""
37bbe 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20  , m_gt_0);.     
37bbf 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27  break;.   case '
37bc0 73 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a  s':.     stem(&z
37bc1 2c 20 22 73 73 65 6e 22 2c 20 22 22 2c 20 6d 5f  , "ssen", "", m_
37bc2 67 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61  gt_0);.     brea
37bc3 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65  k;.  }..  /* Ste
37bc4 70 20 34 20 2a 2f 0a 20 20 73 77 69 74 63 68 28  p 4 */.  switch(
37bc5 20 7a 5b 31 5d 20 29 7b 0a 20 20 20 63 61 73 65   z[1] ){.   case
37bc6 20 27 61 27 3a 0a 20 20 20 20 20 69 66 28 20 7a   'a':.     if( z
37bc7 5b 30 5d 3d 3d 27 6c 27 20 26 26 20 6d 5f 67 74  [0]=='l' && m_gt
37bc8 5f 31 28 7a 2b 32 29 20 29 7b 0a 20 20 20 20 20  _1(z+2) ){.     
37bc9 20 20 7a 20 2b 3d 20 32 3b 0a 20 20 20 20 20 7d    z += 2;.     }
37bca 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  .     break;.   
37bcb 63 61 73 65 20 27 63 27 3a 0a 20 20 20 20 20 69  case 'c':.     i
37bcc 66 28 20 7a 5b 30 5d 3d 3d 27 65 27 20 26 26 20  f( z[0]=='e' && 
37bcd 7a 5b 32 5d 3d 3d 27 6e 27 20 26 26 20 28 7a 5b  z[2]=='n' && (z[
37bce 33 5d 3d 3d 27 61 27 20 7c 7c 20 7a 5b 33 5d 3d  3]=='a' || z[3]=
37bcf 3d 27 65 27 29 20 20 26 26 20 6d 5f 67 74 5f 31  ='e')  && m_gt_1
37bd0 28 7a 2b 34 29 20 20 29 7b 0a 20 20 20 20 20 20  (z+4)  ){.      
37bd1 20 7a 20 2b 3d 20 34 3b 0a 20 20 20 20 20 7d 0a   z += 4;.     }.
37bd2 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63       break;.   c
37bd3 61 73 65 20 27 65 27 3a 0a 20 20 20 20 20 69 66  ase 'e':.     if
37bd4 28 20 7a 5b 30 5d 3d 3d 27 72 27 20 26 26 20 6d  ( z[0]=='r' && m
37bd5 5f 67 74 5f 31 28 7a 2b 32 29 20 29 7b 0a 20 20  _gt_1(z+2) ){.  
37bd6 20 20 20 20 20 7a 20 2b 3d 20 32 3b 0a 20 20 20       z += 2;.   
37bd7 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a    }.     break;.
37bd8 20 20 20 63 61 73 65 20 27 69 27 3a 0a 20 20 20     case 'i':.   
37bd9 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 63 27 20    if( z[0]=='c' 
37bda 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 32 29 20 29  && m_gt_1(z+2) )
37bdb 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20 32 3b  {.       z += 2;
37bdc 0a 20 20 20 20 20 7d 0a 20 20 20 20 20 62 72 65  .     }.     bre
37bdd 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 6c 27 3a  ak;.   case 'l':
37bde 0a 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d  .     if( z[0]==
37bdf 27 65 27 20 26 26 20 7a 5b 32 5d 3d 3d 27 62 27  'e' && z[2]=='b'
37be0 20 26 26 20 28 7a 5b 33 5d 3d 3d 27 61 27 20 7c   && (z[3]=='a' |
37be1 7c 20 7a 5b 33 5d 3d 3d 27 69 27 29 20 26 26 20  | z[3]=='i') && 
37be2 6d 5f 67 74 5f 31 28 7a 2b 34 29 20 29 7b 0a 20  m_gt_1(z+4) ){. 
37be3 20 20 20 20 20 20 7a 20 2b 3d 20 34 3b 0a 20 20        z += 4;.  
37be4 20 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b 3b     }.     break;
37be5 0a 20 20 20 63 61 73 65 20 27 6e 27 3a 0a 20 20  .   case 'n':.  
37be6 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 74 27     if( z[0]=='t'
37be7 20 29 7b 0a 20 20 20 20 20 20 20 69 66 28 20 7a   ){.       if( z
37be8 5b 32 5d 3d 3d 27 61 27 20 29 7b 0a 20 20 20 20  [2]=='a' ){.    
37be9 20 20 20 20 20 69 66 28 20 6d 5f 67 74 5f 31 28       if( m_gt_1(
37bea 7a 2b 33 29 20 29 7b 0a 20 20 20 20 20 20 20 20  z+3) ){.        
37beb 20 20 20 7a 20 2b 3d 20 33 3b 0a 20 20 20 20 20     z += 3;.     
37bec 20 20 20 20 7d 0a 20 20 20 20 20 20 20 7d 65 6c      }.       }el
37bed 73 65 20 69 66 28 20 7a 5b 32 5d 3d 3d 27 65 27  se if( z[2]=='e'
37bee 20 29 7b 0a 20 20 20 20 20 20 20 20 20 73 74 65   ){.         ste
37bef 6d 28 26 7a 2c 20 22 74 6e 65 6d 65 22 2c 20 22  m(&z, "tneme", "
37bf0 22 2c 20 6d 5f 67 74 5f 31 29 20 7c 7c 0a 20 20  ", m_gt_1) ||.  
37bf1 20 20 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20         stem(&z, 
37bf2 22 74 6e 65 6d 22 2c 20 22 22 2c 20 6d 5f 67 74  "tnem", "", m_gt
37bf3 5f 31 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 20  _1) ||.         
37bf4 73 74 65 6d 28 26 7a 2c 20 22 74 6e 65 22 2c 20  stem(&z, "tne", 
37bf5 22 22 2c 20 6d 5f 67 74 5f 31 29 3b 0a 20 20 20  "", m_gt_1);.   
37bf6 20 20 20 20 7d 0a 20 20 20 20 20 7d 0a 20 20 20      }.     }.   
37bf7 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65    break;.   case
37bf8 20 27 6f 27 3a 0a 20 20 20 20 20 69 66 28 20 7a   'o':.     if( z
37bf9 5b 30 5d 3d 3d 27 75 27 20 29 7b 0a 20 20 20 20  [0]=='u' ){.    
37bfa 20 20 20 69 66 28 20 6d 5f 67 74 5f 31 28 7a 2b     if( m_gt_1(z+
37bfb 32 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 7a  2) ){.         z
37bfc 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 20 7d 0a   += 2;.       }.
37bfd 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 7a       }else if( z
37bfe 5b 33 5d 3d 3d 27 73 27 20 7c 7c 20 7a 5b 33 5d  [3]=='s' || z[3]
37bff 3d 3d 27 74 27 20 29 7b 0a 20 20 20 20 20 20 20  =='t' ){.       
37c00 73 74 65 6d 28 26 7a 2c 20 22 6e 6f 69 22 2c 20  stem(&z, "noi", 
37c01 22 22 2c 20 6d 5f 67 74 5f 31 29 3b 0a 20 20 20  "", m_gt_1);.   
37c02 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a    }.     break;.
37c03 20 20 20 63 61 73 65 20 27 73 27 3a 0a 20 20 20     case 's':.   
37c04 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 6d 27 20    if( z[0]=='m' 
37c05 26 26 20 7a 5b 32 5d 3d 3d 27 69 27 20 26 26 20  && z[2]=='i' && 
37c06 6d 5f 67 74 5f 31 28 7a 2b 33 29 20 29 7b 0a 20  m_gt_1(z+3) ){. 
37c07 20 20 20 20 20 20 7a 20 2b 3d 20 33 3b 0a 20 20        z += 3;.  
37c08 20 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b 3b     }.     break;
37c09 0a 20 20 20 63 61 73 65 20 27 74 27 3a 0a 20 20  .   case 't':.  
37c0a 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 65 74 61     stem(&z, "eta
37c0b 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 31 29 20 7c  ", "", m_gt_1) |
37c0c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20  |.     stem(&z, 
37c0d 22 69 74 69 22 2c 20 22 22 2c 20 6d 5f 67 74 5f  "iti", "", m_gt_
37c0e 31 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a  1);.     break;.
37c0f 20 20 20 63 61 73 65 20 27 75 27 3a 0a 20 20 20     case 'u':.   
37c10 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 73 27 20    if( z[0]=='s' 
37c11 26 26 20 7a 5b 32 5d 3d 3d 27 6f 27 20 26 26 20  && z[2]=='o' && 
37c12 6d 5f 67 74 5f 31 28 7a 2b 33 29 20 29 7b 0a 20  m_gt_1(z+3) ){. 
37c13 20 20 20 20 20 20 7a 20 2b 3d 20 33 3b 0a 20 20        z += 3;.  
37c14 20 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b 3b     }.     break;
37c15 0a 20 20 20 63 61 73 65 20 27 76 27 3a 0a 20 20  .   case 'v':.  
37c16 20 63 61 73 65 20 27 7a 27 3a 0a 20 20 20 20 20   case 'z':.     
37c17 69 66 28 20 7a 5b 30 5d 3d 3d 27 65 27 20 26 26  if( z[0]=='e' &&
37c18 20 7a 5b 32 5d 3d 3d 27 69 27 20 26 26 20 6d 5f   z[2]=='i' && m_
37c19 67 74 5f 31 28 7a 2b 33 29 20 29 7b 0a 20 20 20  gt_1(z+3) ){.   
37c1a 20 20 20 20 7a 20 2b 3d 20 33 3b 0a 20 20 20 20      z += 3;.    
37c1b 20 7d 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20   }.     break;. 
37c1c 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 35 61   }..  /* Step 5a
37c1d 20 2a 2f 0a 20 20 69 66 28 20 7a 5b 30 5d 3d 3d   */.  if( z[0]==
37c1e 27 65 27 20 29 7b 0a 20 20 20 20 69 66 28 20 6d  'e' ){.    if( m
37c1f 5f 67 74 5f 31 28 7a 2b 31 29 20 29 7b 0a 20 20  _gt_1(z+1) ){.  
37c20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 65 6c      z++;.    }el
37c21 73 65 20 69 66 28 20 6d 5f 65 71 5f 31 28 7a 2b  se if( m_eq_1(z+
37c22 31 29 20 26 26 20 21 73 74 61 72 5f 6f 68 28 7a  1) && !star_oh(z
37c23 2b 31 29 20 29 7b 0a 20 20 20 20 20 20 7a 2b 2b  +1) ){.      z++
37c24 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
37c25 2a 20 53 74 65 70 20 35 62 20 2a 2f 0a 20 20 69  * Step 5b */.  i
37c26 66 28 20 6d 5f 67 74 5f 31 28 7a 29 20 26 26 20  f( m_gt_1(z) && 
37c27 7a 5b 30 5d 3d 3d 27 6c 27 20 26 26 20 7a 5b 31  z[0]=='l' && z[1
37c28 5d 3d 3d 27 6c 27 20 29 7b 0a 20 20 20 20 7a 2b  ]=='l' ){.    z+
37c29 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 7a 5b 5d  +;.  }..  /* z[]
37c2a 20 69 73 20 6e 6f 77 20 74 68 65 20 73 74 65 6d   is now the stem
37c2b 6d 65 64 20 77 6f 72 64 20 69 6e 20 72 65 76 65  med word in reve
37c2c 72 73 65 20 6f 72 64 65 72 2e 20 20 46 6c 69 70  rse order.  Flip
37c2d 20 69 74 20 62 61 63 6b 0a 20 20 2a 2a 20 61 72   it back.  ** ar
37c2e 6f 75 6e 64 20 69 6e 74 6f 20 66 6f 72 77 61 72  ound into forwar
37c2f 64 20 6f 72 64 65 72 20 61 6e 64 20 72 65 74 75  d order and retu
37c30 72 6e 2e 0a 20 20 2a 2f 0a 20 20 2a 70 6e 4f 75  rn..  */.  *pnOu
37c31 74 20 3d 20 69 20 3d 20 73 74 72 6c 65 6e 28 7a  t = i = strlen(z
37c32 29 3b 0a 20 20 7a 4f 75 74 5b 69 5d 20 3d 20 30  );.  zOut[i] = 0
37c33 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a 20 29 7b  ;.  while( *z ){
37c34 0a 20 20 20 20 7a 4f 75 74 5b 2d 2d 69 5d 20 3d  .    zOut[--i] =
37c35 20 2a 28 7a 2b 2b 29 3b 0a 20 20 7d 0a 7d 0a 0a   *(z++);.  }.}..
37c36 2f 2a 0a 2a 2a 20 43 68 61 72 61 63 74 65 72 73  /*.** Characters
37c37 20 74 68 61 74 20 63 61 6e 20 62 65 20 70 61 72   that can be par
37c38 74 20 6f 66 20 61 20 74 6f 6b 65 6e 2e 20 20 57  t of a token.  W
37c39 65 20 61 73 73 75 6d 65 20 61 6e 79 20 63 68 61  e assume any cha
37c3a 72 61 63 74 65 72 0a 2a 2a 20 77 68 6f 73 65 20  racter.** whose 
37c3b 76 61 6c 75 65 20 69 73 20 67 72 65 61 74 65 72  value is greater
37c3c 20 74 68 61 6e 20 30 78 38 30 20 28 61 6e 79 20   than 0x80 (any 
37c3d 55 54 46 20 63 68 61 72 61 63 74 65 72 29 20 63  UTF character) c
37c3e 61 6e 20 62 65 0a 2a 2a 20 70 61 72 74 20 6f 66  an be.** part of
37c3f 20 61 20 74 6f 6b 65 6e 2e 20 20 49 6e 20 6f 74   a token.  In ot
37c40 68 65 72 20 77 6f 72 64 73 2c 20 64 65 6c 69 6d  her words, delim
37c41 69 74 65 72 73 20 61 6c 6c 20 6d 75 73 74 20 68  iters all must h
37c42 61 76 65 0a 2a 2a 20 76 61 6c 75 65 73 20 6f 66  ave.** values of
37c43 20 30 78 37 66 20 6f 72 20 6c 6f 77 65 72 2e 0a   0x7f or lower..
37c44 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
37c45 63 68 61 72 20 70 6f 72 74 65 72 49 64 43 68 61  char porterIdCha
37c46 72 5b 5d 20 3d 20 7b 0a 2f 2a 20 78 30 20 78 31  r[] = {./* x0 x1
37c47 20 78 32 20 78 33 20 78 34 20 78 35 20 78 36 20   x2 x3 x4 x5 x6 
37c48 78 37 20 78 38 20 78 39 20 78 41 20 78 42 20 78  x7 x8 x9 xA xB x
37c49 43 20 78 44 20 78 45 20 78 46 20 2a 2f 0a 20 20  C xD xE xF */.  
37c4a 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    1, 1, 1, 1, 1,
37c4b 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
37c4c 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
37c4d 2c 20 20 2f 2a 20 33 78 20 2a 2f 0a 20 20 20 20  ,  /* 3x */.    
37c4e 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 1, 1, 1, 1, 1
37c4f 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
37c50 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
37c51 20 2f 2a 20 34 78 20 2a 2f 0a 20 20 20 20 31 2c   /* 4x */.    1,
37c52 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
37c53 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30  1, 1, 1, 1, 1, 0
37c54 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20 20 2f  , 0, 0, 0, 1,  /
37c55 2a 20 35 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31  * 5x */.    0, 1
37c56 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
37c57 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
37c58 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20  1, 1, 1, 1,  /* 
37c59 36 78 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20  6x */.    1, 1, 
37c5a 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
37c5b 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c  , 1, 1, 1, 0, 0,
37c5c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 37 78   0, 0, 0,  /* 7x
37c5d 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 69   */.};.#define i
37c5e 73 44 65 6c 69 6d 28 43 29 20 28 28 28 63 68 3d  sDelim(C) (((ch=
37c5f 43 29 26 30 78 38 30 29 3d 3d 30 20 26 26 20 28  C)&0x80)==0 && (
37c60 63 68 3c 30 78 33 30 20 7c 7c 20 21 70 6f 72 74  ch<0x30 || !port
37c61 65 72 49 64 43 68 61 72 5b 63 68 2d 30 78 33 30  erIdChar[ch-0x30
37c62 5d 29 29 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61  ]))../*.** Extra
37c63 63 74 20 74 68 65 20 6e 65 78 74 20 74 6f 6b 65  ct the next toke
37c64 6e 20 66 72 6f 6d 20 61 20 74 6f 6b 65 6e 69 7a  n from a tokeniz
37c65 61 74 69 6f 6e 20 63 75 72 73 6f 72 2e 20 20 54  ation cursor.  T
37c66 68 65 20 63 75 72 73 6f 72 20 6d 75 73 74 0a 2a  he cursor must.*
37c67 2a 20 68 61 76 65 20 62 65 65 6e 20 6f 70 65 6e  * have been open
37c68 65 64 20 62 79 20 61 20 70 72 69 6f 72 20 63 61  ed by a prior ca
37c69 6c 6c 20 74 6f 20 70 6f 72 74 65 72 4f 70 65 6e  ll to porterOpen
37c6a 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ()..*/.static in
37c6b 74 20 70 6f 72 74 65 72 4e 65 78 74 28 0a 20 20  t porterNext(.  
37c6c 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
37c6d 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f  r_cursor *pCurso
37c6e 72 2c 20 20 2f 2a 20 43 75 72 73 6f 72 20 72 65  r,  /* Cursor re
37c6f 74 75 72 6e 65 64 20 62 79 20 70 6f 72 74 65 72  turned by porter
37c70 4f 70 65 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  Open */.  const 
37c71 63 68 61 72 20 2a 2a 70 7a 54 6f 6b 65 6e 2c 20  char **pzToken, 
37c72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
37c73 20 4f 55 54 3a 20 2a 70 7a 54 6f 6b 65 6e 20 69   OUT: *pzToken i
37c74 73 20 74 68 65 20 74 6f 6b 65 6e 20 74 65 78 74  s the token text
37c75 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 42 79 74   */.  int *pnByt
37c76 65 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  es,             
37c77 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
37c78 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  : Number of byte
37c79 73 20 69 6e 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20  s in token */.  
37c7a 69 6e 74 20 2a 70 69 53 74 61 72 74 4f 66 66 73  int *piStartOffs
37c7b 65 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  et,             
37c7c 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61 72      /* OUT: Star
37c7d 74 69 6e 67 20 6f 66 66 73 65 74 20 6f 66 20 74  ting offset of t
37c7e 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  oken */.  int *p
37c7f 69 45 6e 64 4f 66 66 73 65 74 2c 20 20 20 20 20  iEndOffset,     
37c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
37c81 20 4f 55 54 3a 20 45 6e 64 69 6e 67 20 6f 66 66   OUT: Ending off
37c82 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a  set of token */.
37c83 20 20 69 6e 74 20 2a 70 69 50 6f 73 69 74 69 6f    int *piPositio
37c84 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n               
37c85 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f        /* OUT: Po
37c86 73 69 74 69 6f 6e 20 69 6e 74 65 67 65 72 20 6f  sition integer o
37c87 66 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20  f token */.){.  
37c88 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72  porter_tokenizer
37c89 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20 28 70 6f  _cursor *c = (po
37c8a 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  rter_tokenizer_c
37c8b 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73 6f 72  ursor *) pCursor
37c8c 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
37c8d 7a 20 3d 20 63 2d 3e 7a 49 6e 70 75 74 3b 0a 0a  z = c->zInput;..
37c8e 20 20 77 68 69 6c 65 28 20 63 2d 3e 69 4f 66 66    while( c->iOff
37c8f 73 65 74 3c 63 2d 3e 6e 49 6e 70 75 74 20 29 7b  set<c->nInput ){
37c90 0a 20 20 20 20 69 6e 74 20 69 53 74 61 72 74 4f  .    int iStartO
37c91 66 66 73 65 74 2c 20 63 68 3b 0a 0a 20 20 20 20  ffset, ch;..    
37c92 2f 2a 20 53 63 61 6e 20 70 61 73 74 20 64 65 6c  /* Scan past del
37c93 69 6d 69 74 65 72 20 63 68 61 72 61 63 74 65 72  imiter character
37c94 73 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20  s */.    while( 
37c95 63 2d 3e 69 4f 66 66 73 65 74 3c 63 2d 3e 6e 49  c->iOffset<c->nI
37c96 6e 70 75 74 20 26 26 20 69 73 44 65 6c 69 6d 28  nput && isDelim(
37c97 7a 5b 63 2d 3e 69 4f 66 66 73 65 74 5d 29 20 29  z[c->iOffset]) )
37c98 7b 0a 20 20 20 20 20 20 63 2d 3e 69 4f 66 66 73  {.      c->iOffs
37c99 65 74 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  et++;.    }..   
37c9a 20 2f 2a 20 43 6f 75 6e 74 20 6e 6f 6e 2d 64 65   /* Count non-de
37c9b 6c 69 6d 69 74 65 72 20 63 68 61 72 61 63 74 65  limiter characte
37c9c 72 73 2e 20 2a 2f 0a 20 20 20 20 69 53 74 61 72  rs. */.    iStar
37c9d 74 4f 66 66 73 65 74 20 3d 20 63 2d 3e 69 4f 66  tOffset = c->iOf
37c9e 66 73 65 74 3b 0a 20 20 20 20 77 68 69 6c 65 28  fset;.    while(
37c9f 20 63 2d 3e 69 4f 66 66 73 65 74 3c 63 2d 3e 6e   c->iOffset<c->n
37ca0 49 6e 70 75 74 20 26 26 20 21 69 73 44 65 6c 69  Input && !isDeli
37ca1 6d 28 7a 5b 63 2d 3e 69 4f 66 66 73 65 74 5d 29  m(z[c->iOffset])
37ca2 20 29 7b 0a 20 20 20 20 20 20 63 2d 3e 69 4f 66   ){.      c->iOf
37ca3 66 73 65 74 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20  fset++;.    }.. 
37ca4 20 20 20 69 66 28 20 63 2d 3e 69 4f 66 66 73 65     if( c->iOffse
37ca5 74 3e 69 53 74 61 72 74 4f 66 66 73 65 74 20 29  t>iStartOffset )
37ca6 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20  {.      int n = 
37ca7 63 2d 3e 69 4f 66 66 73 65 74 2d 69 53 74 61 72  c->iOffset-iStar
37ca8 74 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 69  tOffset;.      i
37ca9 66 28 20 6e 3e 63 2d 3e 6e 41 6c 6c 6f 63 61 74  f( n>c->nAllocat
37caa 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 63 2d  ed ){.        c-
37cab 3e 6e 41 6c 6c 6f 63 61 74 65 64 20 3d 20 6e 2b  >nAllocated = n+
37cac 32 30 3b 0a 20 20 20 20 20 20 20 20 63 2d 3e 7a  20;.        c->z
37cad 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65 33 5f  Token = sqlite3_
37cae 72 65 61 6c 6c 6f 63 28 63 2d 3e 7a 54 6f 6b 65  realloc(c->zToke
37caf 6e 2c 20 63 2d 3e 6e 41 6c 6c 6f 63 61 74 65 64  n, c->nAllocated
37cb0 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63  );.        if( c
37cb1 2d 3e 7a 54 6f 6b 65 6e 3d 3d 4e 55 4c 4c 20 29  ->zToken==NULL )
37cb2 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
37cb3 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20  OMEM;.      }.  
37cb4 20 20 20 20 70 6f 72 74 65 72 5f 73 74 65 6d 6d      porter_stemm
37cb5 65 72 28 26 7a 5b 69 53 74 61 72 74 4f 66 66 73  er(&z[iStartOffs
37cb6 65 74 5d 2c 20 6e 2c 20 63 2d 3e 7a 54 6f 6b 65  et], n, c->zToke
37cb7 6e 2c 20 70 6e 42 79 74 65 73 29 3b 0a 20 20 20  n, pnBytes);.   
37cb8 20 20 20 2a 70 7a 54 6f 6b 65 6e 20 3d 20 63 2d     *pzToken = c-
37cb9 3e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 2a  >zToken;.      *
37cba 70 69 53 74 61 72 74 4f 66 66 73 65 74 20 3d 20  piStartOffset = 
37cbb 69 53 74 61 72 74 4f 66 66 73 65 74 3b 0a 20 20  iStartOffset;.  
37cbc 20 20 20 20 2a 70 69 45 6e 64 4f 66 66 73 65 74      *piEndOffset
37cbd 20 3d 20 63 2d 3e 69 4f 66 66 73 65 74 3b 0a 20   = c->iOffset;. 
37cbe 20 20 20 20 20 2a 70 69 50 6f 73 69 74 69 6f 6e       *piPosition
37cbf 20 3d 20 63 2d 3e 69 54 6f 6b 65 6e 2b 2b 3b 0a   = c->iToken++;.
37cc0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
37cc1 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20  ITE_OK;.    }.  
37cc2 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
37cc3 45 5f 44 4f 4e 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  E_DONE;.}../*.**
37cc4 20 54 68 65 20 73 65 74 20 6f 66 20 72 6f 75 74   The set of rout
37cc5 69 6e 65 73 20 74 68 61 74 20 69 6d 70 6c 65 6d  ines that implem
37cc6 65 6e 74 20 74 68 65 20 70 6f 72 74 65 72 2d 73  ent the porter-s
37cc7 74 65 6d 6d 65 72 20 74 6f 6b 65 6e 69 7a 65 72  temmer tokenizer
37cc8 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .*/.static const
37cc9 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
37cca 65 72 5f 6d 6f 64 75 6c 65 20 70 6f 72 74 65 72  er_module porter
37ccb 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 20  TokenizerModule 
37ccc 3d 20 7b 0a 20 20 30 2c 0a 20 20 70 6f 72 74 65  = {.  0,.  porte
37ccd 72 43 72 65 61 74 65 2c 0a 20 20 70 6f 72 74 65  rCreate,.  porte
37cce 72 44 65 73 74 72 6f 79 2c 0a 20 20 70 6f 72 74  rDestroy,.  port
37ccf 65 72 4f 70 65 6e 2c 0a 20 20 70 6f 72 74 65 72  erOpen,.  porter
37cd0 43 6c 6f 73 65 2c 0a 20 20 70 6f 72 74 65 72 4e  Close,.  porterN
37cd1 65 78 74 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  ext,.};../*.** A
37cd2 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 6f  llocate a new po
37cd3 72 74 65 72 20 74 6f 6b 65 6e 69 7a 65 72 2e 20  rter tokenizer. 
37cd4 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
37cd5 72 20 74 6f 20 74 68 65 20 6e 65 77 0a 2a 2a 20  r to the new.** 
37cd6 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 20 2a 70 70  tokenizer in *pp
37cd7 4d 6f 64 75 6c 65 0a 2a 2f 0a 53 51 4c 49 54 45  Module.*/.SQLITE
37cd8 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
37cd9 6c 69 74 65 33 46 74 73 33 50 6f 72 74 65 72 54  lite3Fts3PorterT
37cda 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 0a  okenizerModule(.
37cdb 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
37cdc 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74  zer_module const
37cdd 2a 2a 70 70 4d 6f 64 75 6c 65 0a 29 7b 0a 20 20  **ppModule.){.  
37cde 2a 70 70 4d 6f 64 75 6c 65 20 3d 20 26 70 6f 72  *ppModule = &por
37cdf 74 65 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75  terTokenizerModu
37ce0 6c 65 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a  le;.}..#endif /*
37ce1 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
37ce2 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65  _CORE) || define
37ce3 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
37ce4 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  FTS3) */../*****
37ce5 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
37ce6 20 66 74 73 33 5f 70 6f 72 74 65 72 2e 63 20 2a   fts3_porter.c *
37ce7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37ce8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37ce9 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
37cea 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
37ceb 66 69 6c 65 20 66 74 73 33 5f 74 6f 6b 65 6e 69  file fts3_tokeni
37cec 7a 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  zer.c **********
37ced 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37cee 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
37cef 32 30 30 37 20 4a 75 6e 65 20 32 32 0a 2a 2a 0a  2007 June 22.**.
37cf0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
37cf1 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
37cf2 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
37cf3 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
37cf4 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
37cf5 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
37cf6 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
37cf7 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
37cf8 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
37cf9 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
37cfa 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
37cfb 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
37cfc 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
37cfd 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
37cfe 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
37cff 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
37d00 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
37d01 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
37d02 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37d03 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37d04 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37d05 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37d06 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 70  .**.** This is p
37d07 61 72 74 20 6f 66 20 61 6e 20 53 51 4c 69 74 65  art of an SQLite
37d08 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e   module implemen
37d09 74 69 6e 67 20 66 75 6c 6c 2d 74 65 78 74 20 73  ting full-text s
37d0a 65 61 72 63 68 2e 0a 2a 2a 20 54 68 69 73 20 70  earch..** This p
37d0b 61 72 74 69 63 75 6c 61 72 20 66 69 6c 65 20 69  articular file i
37d0c 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 67 65  mplements the ge
37d0d 6e 65 72 69 63 20 74 6f 6b 65 6e 69 7a 65 72 20  neric tokenizer 
37d0e 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 0a 2f  interface..*/../
37d0f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e  *.** The code in
37d10 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e   this file is on
37d11 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 3a 0a  ly compiled if:.
37d12 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20  **.**     * The 
37d13 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62  FTS3 module is b
37d14 65 69 6e 67 20 62 75 69 6c 74 20 61 73 20 61 6e  eing built as an
37d15 20 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20   extension.**   
37d16 20 20 20 20 28 69 6e 20 77 68 69 63 68 20 63 61      (in which ca
37d17 73 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 69  se SQLITE_CORE i
37d18 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c 20  s not defined), 
37d19 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54  or.**.**     * T
37d1a 68 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69  he FTS3 module i
37d1b 73 20 62 65 69 6e 67 20 62 75 69 6c 74 20 69 6e  s being built in
37d1c 74 6f 20 74 68 65 20 63 6f 72 65 20 6f 66 0a 2a  to the core of.*
37d1d 2a 20 20 20 20 20 20 20 53 51 4c 69 74 65 20 28  *       SQLite (
37d1e 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 53 51  in which case SQ
37d1f 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
37d20 20 69 73 20 64 65 66 69 6e 65 64 29 2e 0a 2a 2f   is defined)..*/
37d21 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
37d22 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65  LITE_CORE) || de
37d23 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
37d24 42 4c 45 5f 46 54 53 33 29 0a 0a 23 69 66 6e 64  BLE_FTS3)..#ifnd
37d25 65 66 20 53 51 4c 49 54 45 5f 43 4f 52 45 0a 20  ef SQLITE_CORE. 
37d26 20 53 51 4c 49 54 45 5f 45 58 54 45 4e 53 49 4f   SQLITE_EXTENSIO
37d27 4e 5f 49 4e 49 54 31 0a 23 65 6e 64 69 66 0a 0a  N_INIT1.#endif..
37d28 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
37d29 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 53 51 4c  ation of the SQL
37d2a 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e   scalar function
37d2b 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20 74   for accessing t
37d2c 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 0a 2a  he underlying .*
37d2d 2a 20 68 61 73 68 20 74 61 62 6c 65 2e 20 54 68  * hash table. Th
37d2e 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20  is function may 
37d2f 62 65 20 63 61 6c 6c 65 64 20 61 73 20 66 6f 6c  be called as fol
37d30 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 45  lows:.**.**   SE
37d31 4c 45 43 54 20 3c 66 75 6e 63 74 69 6f 6e 2d 6e  LECT <function-n
37d32 61 6d 65 3e 28 3c 6b 65 79 2d 6e 61 6d 65 3e 29  ame>(<key-name>)
37d33 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 3c 66  ;.**   SELECT <f
37d34 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e 28 3c 6b  unction-name>(<k
37d35 65 79 2d 6e 61 6d 65 3e 2c 20 3c 70 6f 69 6e 74  ey-name>, <point
37d36 65 72 3e 29 3b 0a 2a 2a 0a 2a 2a 20 77 68 65 72  er>);.**.** wher
37d37 65 20 3c 66 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65  e <function-name
37d38 3e 20 69 73 20 74 68 65 20 6e 61 6d 65 20 70 61  > is the name pa
37d39 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f  ssed as the seco
37d3a 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74  nd argument.** t
37d3b 6f 20 74 68 65 20 73 71 6c 69 74 65 33 46 74 73  o the sqlite3Fts
37d3c 33 49 6e 69 74 48 61 73 68 54 61 62 6c 65 28 29  3InitHashTable()
37d3d 20 66 75 6e 63 74 69 6f 6e 20 28 65 2e 67 2e 20   function (e.g. 
37d3e 27 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 27  'fts3_tokenizer'
37d3f 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  )..**.** If the 
37d40 3c 70 6f 69 6e 74 65 72 3e 20 61 72 67 75 6d 65  <pointer> argume
37d41 6e 74 20 69 73 20 73 70 65 63 69 66 69 65 64 2c  nt is specified,
37d42 20 69 74 20 6d 75 73 74 20 62 65 20 61 20 62 6c   it must be a bl
37d43 6f 62 20 76 61 6c 75 65 0a 2a 2a 20 63 6f 6e 74  ob value.** cont
37d44 61 69 6e 69 6e 67 20 61 20 70 6f 69 6e 74 65 72  aining a pointer
37d45 20 74 6f 20 62 65 20 73 74 6f 72 65 64 20 61 73   to be stored as
37d46 20 74 68 65 20 68 61 73 68 20 64 61 74 61 20 63   the hash data c
37d47 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20  orresponding.** 
37d48 74 6f 20 74 68 65 20 73 74 72 69 6e 67 20 3c 6b  to the string <k
37d49 65 79 2d 6e 61 6d 65 3e 2e 20 49 66 20 3c 70 6f  ey-name>. If <po
37d4a 69 6e 74 65 72 3e 20 69 73 20 6e 6f 74 20 73 70  inter> is not sp
37d4b 65 63 69 66 69 65 64 2c 20 74 68 65 6e 0a 2a 2a  ecified, then.**
37d4c 20 74 68 65 20 73 74 72 69 6e 67 20 3c 6b 65 79   the string <key
37d4d 2d 6e 61 6d 65 3e 20 6d 75 73 74 20 61 6c 72 65  -name> must alre
37d4e 61 64 79 20 65 78 69 73 74 20 69 6e 20 74 68 65  ady exist in the
37d4f 20 68 61 73 20 74 61 62 6c 65 2e 20 4f 74 68 65   has table. Othe
37d50 72 77 69 73 65 2c 0a 2a 2a 20 61 6e 20 65 72 72  rwise,.** an err
37d51 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  or is returned..
37d52 2a 2a 0a 2a 2a 20 57 68 65 74 68 65 72 20 6f 72  **.** Whether or
37d53 20 6e 6f 74 20 74 68 65 20 3c 70 6f 69 6e 74 65   not the <pointe
37d54 72 3e 20 61 72 67 75 6d 65 6e 74 20 69 73 20 73  r> argument is s
37d55 70 65 63 69 66 69 65 64 2c 20 74 68 65 20 76 61  pecified, the va
37d56 6c 75 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20  lue returned.** 
37d57 69 73 20 61 20 62 6c 6f 62 20 63 6f 6e 74 61 69  is a blob contai
37d58 6e 69 6e 67 20 74 68 65 20 70 6f 69 6e 74 65 72  ning the pointer
37d59 20 73 74 6f 72 65 64 20 61 73 20 74 68 65 20 68   stored as the h
37d5a 61 73 68 20 64 61 74 61 20 63 6f 72 72 65 73 70  ash data corresp
37d5b 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20 73 74 72  onding.** to str
37d5c 69 6e 67 20 3c 6b 65 79 2d 6e 61 6d 65 3e 20 28  ing <key-name> (
37d5d 61 66 74 65 72 20 74 68 65 20 68 61 73 68 2d 74  after the hash-t
37d5e 61 62 6c 65 20 69 73 20 75 70 64 61 74 65 64 2c  able is updated,
37d5f 20 69 66 20 61 70 70 6c 69 63 61 62 6c 65 29 2e   if applicable).
37d60 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
37d61 73 63 61 6c 61 72 46 75 6e 63 28 0a 20 20 73 71  scalarFunc(.  sq
37d62 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
37d63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
37d64 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  gc,.  sqlite3_va
37d65 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
37d66 66 74 73 33 48 61 73 68 20 2a 70 48 61 73 68 3b  fts3Hash *pHash;
37d67 0a 20 20 76 6f 69 64 20 2a 70 50 74 72 20 3d 20  .  void *pPtr = 
37d68 30 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  0;.  const unsig
37d69 6e 65 64 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b  ned char *zName;
37d6a 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 0a 20  .  int nName;.. 
37d6b 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31   assert( argc==1
37d6c 20 7c 7c 20 61 72 67 63 3d 3d 32 20 29 3b 0a 0a   || argc==2 );..
37d6d 20 20 70 48 61 73 68 20 3d 20 28 66 74 73 33 48    pHash = (fts3H
37d6e 61 73 68 20 2a 29 73 71 6c 69 74 65 33 5f 75 73  ash *)sqlite3_us
37d6f 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29  er_data(context)
37d70 3b 0a 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c  ;..  zName = sql
37d71 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
37d72 61 72 67 76 5b 30 5d 29 3b 0a 20 20 6e 4e 61 6d  argv[0]);.  nNam
37d73 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  e = sqlite3_valu
37d74 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29  e_bytes(argv[0])
37d75 2b 31 3b 0a 0a 20 20 69 66 28 20 61 72 67 63 3d  +1;..  if( argc=
37d76 3d 32 20 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a  =2 ){.    void *
37d77 70 4f 6c 64 3b 0a 20 20 20 20 69 6e 74 20 6e 20  pOld;.    int n 
37d78 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
37d79 62 79 74 65 73 28 61 72 67 76 5b 31 5d 29 3b 0a  bytes(argv[1]);.
37d7a 20 20 20 20 69 66 28 20 6e 21 3d 73 69 7a 65 6f      if( n!=sizeo
37d7b 66 28 70 50 74 72 29 20 29 7b 0a 20 20 20 20 20  f(pPtr) ){.     
37d7c 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
37d7d 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22  error(context, "
37d7e 61 72 67 75 6d 65 6e 74 20 74 79 70 65 20 6d 69  argument type mi
37d7f 73 6d 61 74 63 68 22 2c 20 2d 31 29 3b 0a 20 20  smatch", -1);.  
37d80 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
37d81 7d 0a 20 20 20 20 70 50 74 72 20 3d 20 2a 28 76  }.    pPtr = *(v
37d82 6f 69 64 20 2a 2a 29 73 71 6c 69 74 65 33 5f 76  oid **)sqlite3_v
37d83 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 31  alue_blob(argv[1
37d84 5d 29 3b 0a 20 20 20 20 70 4f 6c 64 20 3d 20 73  ]);.    pOld = s
37d85 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49 6e  qlite3Fts3HashIn
37d86 73 65 72 74 28 70 48 61 73 68 2c 20 28 76 6f 69  sert(pHash, (voi
37d87 64 20 2a 29 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65  d *)zName, nName
37d88 2c 20 70 50 74 72 29 3b 0a 20 20 20 20 69 66 28  , pPtr);.    if(
37d89 20 70 4f 6c 64 3d 3d 70 50 74 72 20 29 7b 0a 20   pOld==pPtr ){. 
37d8a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
37d8b 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78  ult_error(contex
37d8c 74 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72  t, "out of memor
37d8d 79 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 72  y", -1);.      r
37d8e 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d  eturn;.    }.  }
37d8f 65 6c 73 65 7b 0a 20 20 20 20 70 50 74 72 20 3d  else{.    pPtr =
37d90 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68   sqlite3Fts3Hash
37d91 46 69 6e 64 28 70 48 61 73 68 2c 20 7a 4e 61 6d  Find(pHash, zNam
37d92 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 69  e, nName);.    i
37d93 66 28 20 21 70 50 74 72 20 29 7b 0a 20 20 20 20  f( !pPtr ){.    
37d94 20 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 73    char *zErr = s
37d95 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
37d96 75 6e 6b 6e 6f 77 6e 20 74 6f 6b 65 6e 69 7a 65  unknown tokenize
37d97 72 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a  r: %s", zName);.
37d98 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
37d99 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65  sult_error(conte
37d9a 78 74 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0a 20  xt, zErr, -1);. 
37d9b 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
37d9c 65 28 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 72  e(zErr);.      r
37d9d 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d  eturn;.    }.  }
37d9e 0a 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ..  sqlite3_resu
37d9f 6c 74 5f 62 6c 6f 62 28 63 6f 6e 74 65 78 74 2c  lt_blob(context,
37da0 20 28 76 6f 69 64 20 2a 29 26 70 50 74 72 2c 20   (void *)&pPtr, 
37da1 73 69 7a 65 6f 66 28 70 50 74 72 29 2c 20 53 51  sizeof(pPtr), SQ
37da2 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b  LITE_TRANSIENT);
37da3 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .}..#ifdef SQLIT
37da4 45 5f 54 45 53 54 0a 0a 0a 2f 2a 0a 2a 2a 20 49  E_TEST.../*.** I
37da5 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
37da6 20 61 20 73 70 65 63 69 61 6c 20 53 51 4c 20 73   a special SQL s
37da7 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 66  calar function f
37da8 6f 72 20 74 65 73 74 69 6e 67 20 74 6f 6b 65 6e  or testing token
37da9 69 7a 65 72 73 20 0a 2a 2a 20 64 65 73 69 67 6e  izers .** design
37daa 65 64 20 74 6f 20 62 65 20 75 73 65 64 20 69 6e  ed to be used in
37dab 20 63 6f 6e 63 65 72 74 20 77 69 74 68 20 74 68   concert with th
37dac 65 20 54 63 6c 20 74 65 73 74 69 6e 67 20 66 72  e Tcl testing fr
37dad 61 6d 65 77 6f 72 6b 2e 20 54 68 69 73 0a 2a 2a  amework. This.**
37dae 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62   function must b
37daf 65 20 63 61 6c 6c 65 64 20 77 69 74 68 20 74 77  e called with tw
37db0 6f 20 61 72 67 75 6d 65 6e 74 73 3a 0a 2a 2a 0a  o arguments:.**.
37db1 2a 2a 20 20 20 53 45 4c 45 43 54 20 3c 66 75 6e  **   SELECT <fun
37db2 63 74 69 6f 6e 2d 6e 61 6d 65 3e 28 3c 6b 65 79  ction-name>(<key
37db3 2d 6e 61 6d 65 3e 2c 20 3c 69 6e 70 75 74 2d 73  -name>, <input-s
37db4 74 72 69 6e 67 3e 29 3b 0a 2a 2a 20 20 20 53 45  tring>);.**   SE
37db5 4c 45 43 54 20 3c 66 75 6e 63 74 69 6f 6e 2d 6e  LECT <function-n
37db6 61 6d 65 3e 28 3c 6b 65 79 2d 6e 61 6d 65 3e 2c  ame>(<key-name>,
37db7 20 3c 70 6f 69 6e 74 65 72 3e 29 3b 0a 2a 2a 0a   <pointer>);.**.
37db8 2a 2a 20 77 68 65 72 65 20 3c 66 75 6e 63 74 69  ** where <functi
37db9 6f 6e 2d 6e 61 6d 65 3e 20 69 73 20 74 68 65 20  on-name> is the 
37dba 6e 61 6d 65 20 70 61 73 73 65 64 20 61 73 20 74  name passed as t
37dbb 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
37dbc 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20 73 71 6c  nt.** to the sql
37dbd 69 74 65 33 46 74 73 33 49 6e 69 74 48 61 73 68  ite3Fts3InitHash
37dbe 54 61 62 6c 65 28 29 20 66 75 6e 63 74 69 6f 6e  Table() function
37dbf 20 28 65 2e 67 2e 20 27 66 74 73 33 5f 74 6f 6b   (e.g. 'fts3_tok
37dc0 65 6e 69 7a 65 72 27 29 0a 2a 2a 20 63 6f 6e 63  enizer').** conc
37dc1 61 74 65 6e 61 74 65 64 20 77 69 74 68 20 74 68  atenated with th
37dc2 65 20 73 74 72 69 6e 67 20 27 5f 74 65 73 74 27  e string '_test'
37dc3 20 28 65 2e 67 2e 20 27 66 74 73 33 5f 74 6f 6b   (e.g. 'fts3_tok
37dc4 65 6e 69 7a 65 72 5f 74 65 73 74 27 29 2e 0a 2a  enizer_test')..*
37dc5 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20  *.** The return 
37dc6 76 61 6c 75 65 20 69 73 20 61 20 73 74 72 69 6e  value is a strin
37dc7 67 20 74 68 61 74 20 6d 61 79 20 62 65 20 69 6e  g that may be in
37dc8 74 65 72 70 72 65 74 65 64 20 61 73 20 61 20 54  terpreted as a T
37dc9 63 6c 0a 2a 2a 20 6c 69 73 74 2e 20 46 6f 72 20  cl.** list. For 
37dca 65 61 63 68 20 74 6f 6b 65 6e 20 69 6e 20 74 68  each token in th
37dcb 65 20 3c 69 6e 70 75 74 2d 73 74 72 69 6e 67 3e  e <input-string>
37dcc 2c 20 74 68 72 65 65 20 65 6c 65 6d 65 6e 74 73  , three elements
37dcd 20 61 72 65 0a 2a 2a 20 61 64 64 65 64 20 74 6f   are.** added to
37dce 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 6c 69   the returned li
37dcf 73 74 2e 20 54 68 65 20 66 69 72 73 74 20 69 73  st. The first is
37dd0 20 74 68 65 20 74 6f 6b 65 6e 20 70 6f 73 69 74   the token posit
37dd1 69 6f 6e 2c 20 74 68 65 20 0a 2a 2a 20 73 65 63  ion, the .** sec
37dd2 6f 6e 64 20 69 73 20 74 68 65 20 74 6f 6b 65 6e  ond is the token
37dd3 20 74 65 78 74 20 28 66 6f 6c 64 65 64 2c 20 73   text (folded, s
37dd4 74 65 6d 6d 65 64 2c 20 65 74 63 2e 29 20 61 6e  temmed, etc.) an
37dd5 64 20 74 68 65 20 74 68 69 72 64 20 69 73 20 74  d the third is t
37dd6 68 65 0a 2a 2a 20 73 75 62 73 74 72 69 6e 67 20  he.** substring 
37dd7 6f 66 20 3c 69 6e 70 75 74 2d 73 74 72 69 6e 67  of <input-string
37dd8 3e 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  > associated wit
37dd9 68 20 74 68 65 20 74 6f 6b 65 6e 2e 20 46 6f 72  h the token. For
37dda 20 65 78 61 6d 70 6c 65 2c 20 0a 2a 2a 20 75 73   example, .** us
37ddb 69 6e 67 20 74 68 65 20 62 75 69 6c 74 2d 69 6e  ing the built-in
37ddc 20 22 73 69 6d 70 6c 65 22 20 74 6f 6b 65 6e 69   "simple" tokeni
37ddd 7a 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c  zer:.**.**   SEL
37dde 45 43 54 20 66 74 73 5f 74 6f 6b 65 6e 69 7a 65  ECT fts_tokenize
37ddf 72 5f 74 65 73 74 28 27 73 69 6d 70 6c 65 27 2c  r_test('simple',
37de0 20 27 49 20 64 6f 6e 27 74 20 73 65 65 20 68 6f   'I don't see ho
37de1 77 27 29 3b 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c 20  w');.**.** will 
37de2 72 65 74 75 72 6e 20 74 68 65 20 73 74 72 69 6e  return the strin
37de3 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 22 7b 30 20 69  g:.**.**   "{0 i
37de4 20 49 20 31 20 64 6f 6e 74 20 64 6f 6e 27 74 20   I 1 dont don't 
37de5 32 20 73 65 65 20 73 65 65 20 33 20 68 6f 77 20  2 see see 3 how 
37de6 68 6f 77 7d 22 0a 2a 2a 20 20 20 0a 2a 2f 0a 73  how}".**   .*/.s
37de7 74 61 74 69 63 20 76 6f 69 64 20 74 65 73 74 46  tatic void testF
37de8 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
37de9 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
37dea 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73  .  int argc,.  s
37deb 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
37dec 72 67 76 0a 29 7b 0a 20 20 66 74 73 33 48 61 73  rgv.){.  fts3Has
37ded 68 20 2a 70 48 61 73 68 3b 0a 20 20 73 71 6c 69  h *pHash;.  sqli
37dee 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
37def 64 75 6c 65 20 2a 70 3b 0a 20 20 73 71 6c 69 74  dule *p;.  sqlit
37df0 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54  e3_tokenizer *pT
37df1 6f 6b 65 6e 69 7a 65 72 20 3d 20 30 3b 0a 20 20  okenizer = 0;.  
37df2 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
37df3 72 5f 63 75 72 73 6f 72 20 2a 70 43 73 72 20 3d  r_cursor *pCsr =
37df4 20 30 3b 0a 0a 20 20 63 6f 6e 73 74 20 63 68 61   0;..  const cha
37df5 72 20 2a 7a 45 72 72 20 3d 20 30 3b 0a 0a 20 20  r *zErr = 0;..  
37df6 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
37df7 65 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a  e;.  int nName;.
37df8 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49    const char *zI
37df9 6e 70 75 74 3b 0a 20 20 69 6e 74 20 6e 49 6e 70  nput;.  int nInp
37dfa 75 74 3b 0a 0a 20 20 63 6f 6e 73 74 20 63 68 61  ut;..  const cha
37dfb 72 20 2a 7a 41 72 67 20 3d 20 30 3b 0a 0a 20 20  r *zArg = 0;..  
37dfc 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 6f 6b  const char *zTok
37dfd 65 6e 3b 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e  en;.  int nToken
37dfe 3b 0a 20 20 69 6e 74 20 69 53 74 61 72 74 3b 0a  ;.  int iStart;.
37dff 20 20 69 6e 74 20 69 45 6e 64 3b 0a 20 20 69 6e    int iEnd;.  in
37e00 74 20 69 50 6f 73 3b 0a 0a 20 20 54 63 6c 5f 4f  t iPos;..  Tcl_O
37e01 62 6a 20 2a 70 52 65 74 3b 0a 0a 20 20 61 73 73  bj *pRet;..  ass
37e02 65 72 74 28 20 61 72 67 63 3d 3d 32 20 7c 7c 20  ert( argc==2 || 
37e03 61 72 67 63 3d 3d 33 20 29 3b 0a 0a 20 20 6e 4e  argc==3 );..  nN
37e04 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  ame = sqlite3_va
37e05 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30  lue_bytes(argv[0
37e06 5d 29 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 28 63  ]);.  zName = (c
37e07 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69  onst char *)sqli
37e08 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
37e09 72 67 76 5b 30 5d 29 3b 0a 20 20 6e 49 6e 70 75  rgv[0]);.  nInpu
37e0a 74 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  t = sqlite3_valu
37e0b 65 5f 62 79 74 65 73 28 61 72 67 76 5b 61 72 67  e_bytes(argv[arg
37e0c 63 2d 31 5d 29 3b 0a 20 20 7a 49 6e 70 75 74 20  c-1]);.  zInput 
37e0d 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
37e0e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
37e0f 78 74 28 61 72 67 76 5b 61 72 67 63 2d 31 5d 29  xt(argv[argc-1])
37e10 3b 0a 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 33  ;..  if( argc==3
37e11 20 29 7b 0a 20 20 20 20 7a 41 72 67 20 3d 20 28   ){.    zArg = (
37e12 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c  const char *)sql
37e13 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
37e14 61 72 67 76 5b 31 5d 29 3b 0a 20 20 7d 0a 0a 20  argv[1]);.  }.. 
37e15 20 70 48 61 73 68 20 3d 20 28 66 74 73 33 48 61   pHash = (fts3Ha
37e16 73 68 20 2a 29 73 71 6c 69 74 65 33 5f 75 73 65  sh *)sqlite3_use
37e17 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 3b  r_data(context);
37e18 0a 20 20 70 20 3d 20 28 73 71 6c 69 74 65 33 5f  .  p = (sqlite3_
37e19 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
37e1a 20 2a 29 73 71 6c 69 74 65 33 46 74 73 33 48 61   *)sqlite3Fts3Ha
37e1b 73 68 46 69 6e 64 28 70 48 61 73 68 2c 20 7a 4e  shFind(pHash, zN
37e1c 61 6d 65 2c 20 6e 4e 61 6d 65 2b 31 29 3b 0a 0a  ame, nName+1);..
37e1d 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20    if( !p ){.    
37e1e 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 73 71 6c  char *zErr = sql
37e1f 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 75 6e  ite3_mprintf("un
37e20 6b 6e 6f 77 6e 20 74 6f 6b 65 6e 69 7a 65 72 3a  known tokenizer:
37e21 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20   %s", zName);.  
37e22 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
37e23 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20  _error(context, 
37e24 7a 45 72 72 2c 20 2d 31 29 3b 0a 20 20 20 20 73  zErr, -1);.    s
37e25 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 72 72  qlite3_free(zErr
37e26 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
37e27 20 7d 0a 0a 20 20 70 52 65 74 20 3d 20 54 63 6c   }..  pRet = Tcl
37e28 5f 4e 65 77 4f 62 6a 28 29 3b 0a 20 20 54 63 6c  _NewObj();.  Tcl
37e29 5f 49 6e 63 72 52 65 66 43 6f 75 6e 74 28 70 52  _IncrRefCount(pR
37e2a 65 74 29 3b 0a 0a 20 20 69 66 28 20 53 51 4c 49  et);..  if( SQLI
37e2b 54 45 5f 4f 4b 21 3d 70 2d 3e 78 43 72 65 61 74  TE_OK!=p->xCreat
37e2c 65 28 7a 41 72 67 20 3f 20 31 20 3a 20 30 2c 20  e(zArg ? 1 : 0, 
37e2d 26 7a 41 72 67 2c 20 26 70 54 6f 6b 65 6e 69 7a  &zArg, &pTokeniz
37e2e 65 72 29 20 29 7b 0a 20 20 20 20 7a 45 72 72 20  er) ){.    zErr 
37e2f 3d 20 22 65 72 72 6f 72 20 69 6e 20 78 43 72 65  = "error in xCre
37e30 61 74 65 28 29 22 3b 0a 20 20 20 20 67 6f 74 6f  ate()";.    goto
37e31 20 66 69 6e 69 73 68 3b 0a 20 20 7d 0a 20 20 70   finish;.  }.  p
37e32 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75  Tokenizer->pModu
37e33 6c 65 20 3d 20 70 3b 0a 20 20 69 66 28 20 53 51  le = p;.  if( SQ
37e34 4c 49 54 45 5f 4f 4b 21 3d 70 2d 3e 78 4f 70 65  LITE_OK!=p->xOpe
37e35 6e 28 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 7a 49  n(pTokenizer, zI
37e36 6e 70 75 74 2c 20 6e 49 6e 70 75 74 2c 20 26 70  nput, nInput, &p
37e37 43 73 72 29 20 29 7b 0a 20 20 20 20 7a 45 72 72  Csr) ){.    zErr
37e38 20 3d 20 22 65 72 72 6f 72 20 69 6e 20 78 4f 70   = "error in xOp
37e39 65 6e 28 29 22 3b 0a 20 20 20 20 67 6f 74 6f 20  en()";.    goto 
37e3a 66 69 6e 69 73 68 3b 0a 20 20 7d 0a 20 20 70 43  finish;.  }.  pC
37e3b 73 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 20 3d  sr->pTokenizer =
37e3c 20 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 0a 20 20   pTokenizer;..  
37e3d 77 68 69 6c 65 28 20 53 51 4c 49 54 45 5f 4f 4b  while( SQLITE_OK
37e3e 3d 3d 70 2d 3e 78 4e 65 78 74 28 70 43 73 72 2c  ==p->xNext(pCsr,
37e3f 20 26 7a 54 6f 6b 65 6e 2c 20 26 6e 54 6f 6b 65   &zToken, &nToke
37e40 6e 2c 20 26 69 53 74 61 72 74 2c 20 26 69 45 6e  n, &iStart, &iEn
37e41 64 2c 20 26 69 50 6f 73 29 20 29 7b 0a 20 20 20  d, &iPos) ){.   
37e42 20 54 63 6c 5f 4c 69 73 74 4f 62 6a 41 70 70 65   Tcl_ListObjAppe
37e43 6e 64 45 6c 65 6d 65 6e 74 28 30 2c 20 70 52 65  ndElement(0, pRe
37e44 74 2c 20 54 63 6c 5f 4e 65 77 49 6e 74 4f 62 6a  t, Tcl_NewIntObj
37e45 28 69 50 6f 73 29 29 3b 0a 20 20 20 20 54 63 6c  (iPos));.    Tcl
37e46 5f 4c 69 73 74 4f 62 6a 41 70 70 65 6e 64 45 6c  _ListObjAppendEl
37e47 65 6d 65 6e 74 28 30 2c 20 70 52 65 74 2c 20 54  ement(0, pRet, T
37e48 63 6c 5f 4e 65 77 53 74 72 69 6e 67 4f 62 6a 28  cl_NewStringObj(
37e49 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29 29  zToken, nToken))
37e4a 3b 0a 20 20 20 20 7a 54 6f 6b 65 6e 20 3d 20 26  ;.    zToken = &
37e4b 7a 49 6e 70 75 74 5b 69 53 74 61 72 74 5d 3b 0a  zInput[iStart];.
37e4c 20 20 20 20 6e 54 6f 6b 65 6e 20 3d 20 69 45 6e      nToken = iEn
37e4d 64 2d 69 53 74 61 72 74 3b 0a 20 20 20 20 54 63  d-iStart;.    Tc
37e4e 6c 5f 4c 69 73 74 4f 62 6a 41 70 70 65 6e 64 45  l_ListObjAppendE
37e4f 6c 65 6d 65 6e 74 28 30 2c 20 70 52 65 74 2c 20  lement(0, pRet, 
37e50 54 63 6c 5f 4e 65 77 53 74 72 69 6e 67 4f 62 6a  Tcl_NewStringObj
37e51 28 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29  (zToken, nToken)
37e52 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 53 51  );.  }..  if( SQ
37e53 4c 49 54 45 5f 4f 4b 21 3d 70 2d 3e 78 43 6c 6f  LITE_OK!=p->xClo
37e54 73 65 28 70 43 73 72 29 20 29 7b 0a 20 20 20 20  se(pCsr) ){.    
37e55 7a 45 72 72 20 3d 20 22 65 72 72 6f 72 20 69 6e  zErr = "error in
37e56 20 78 43 6c 6f 73 65 28 29 22 3b 0a 20 20 20 20   xClose()";.    
37e57 67 6f 74 6f 20 66 69 6e 69 73 68 3b 0a 20 20 7d  goto finish;.  }
37e58 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  .  if( SQLITE_OK
37e59 21 3d 70 2d 3e 78 44 65 73 74 72 6f 79 28 70 54  !=p->xDestroy(pT
37e5a 6f 6b 65 6e 69 7a 65 72 29 20 29 7b 0a 20 20 20  okenizer) ){.   
37e5b 20 7a 45 72 72 20 3d 20 22 65 72 72 6f 72 20 69   zErr = "error i
37e5c 6e 20 78 44 65 73 74 72 6f 79 28 29 22 3b 0a 20  n xDestroy()";. 
37e5d 20 20 20 67 6f 74 6f 20 66 69 6e 69 73 68 3b 0a     goto finish;.
37e5e 20 20 7d 0a 0a 66 69 6e 69 73 68 3a 0a 20 20 69    }..finish:.  i
37e5f 66 28 20 7a 45 72 72 20 29 7b 0a 20 20 20 20 73  f( zErr ){.    s
37e60 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
37e61 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72  ror(context, zEr
37e62 72 2c 20 2d 31 29 3b 0a 20 20 7d 65 6c 73 65 7b  r, -1);.  }else{
37e63 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
37e64 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
37e65 2c 20 54 63 6c 5f 47 65 74 53 74 72 69 6e 67 28  , Tcl_GetString(
37e66 70 52 65 74 29 2c 20 2d 31 2c 20 53 51 4c 49 54  pRet), -1, SQLIT
37e67 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20  E_TRANSIENT);.  
37e68 7d 0a 20 20 54 63 6c 5f 44 65 63 72 52 65 66 43  }.  Tcl_DecrRefC
37e69 6f 75 6e 74 28 70 52 65 74 29 3b 0a 7d 0a 0a 73  ount(pRet);.}..s
37e6a 74 61 74 69 63 0a 69 6e 74 20 72 65 67 69 73 74  tatic.int regist
37e6b 65 72 54 6f 6b 65 6e 69 7a 65 72 28 0a 20 20 73  erTokenizer(.  s
37e6c 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 63  qlite3 *db, .  c
37e6d 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20 63  har *zName, .  c
37e6e 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b  onst sqlite3_tok
37e6f 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70  enizer_module *p
37e70 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
37e71 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
37e72 74 6d 74 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  tmt;.  const cha
37e73 72 20 7a 53 71 6c 5b 5d 20 3d 20 22 53 45 4c 45  r zSql[] = "SELE
37e74 43 54 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65  CT fts3_tokenize
37e75 72 28 3f 2c 20 3f 29 22 3b 0a 0a 20 20 72 63 20  r(?, ?)";..  rc 
37e76 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  = sqlite3_prepar
37e77 65 5f 76 32 28 64 62 2c 20 7a 53 71 6c 2c 20 2d  e_v2(db, zSql, -
37e78 31 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20  1, &pStmt, 0);. 
37e79 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
37e7a 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OK ){.    return
37e7b 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69   rc;.  }..  sqli
37e7c 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 70 53  te3_bind_text(pS
37e7d 74 6d 74 2c 20 31 2c 20 7a 4e 61 6d 65 2c 20 2d  tmt, 1, zName, -
37e7e 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  1, SQLITE_STATIC
37e7f 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  );.  sqlite3_bin
37e80 64 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 32 2c  d_blob(pStmt, 2,
37e81 20 26 70 2c 20 73 69 7a 65 6f 66 28 70 29 2c 20   &p, sizeof(p), 
37e82 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
37e83 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70    sqlite3_step(p
37e84 53 74 6d 74 29 3b 0a 0a 20 20 72 65 74 75 72 6e  Stmt);..  return
37e85 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
37e86 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a 73 74 61  e(pStmt);.}..sta
37e87 74 69 63 0a 69 6e 74 20 71 75 65 72 79 54 6f 6b  tic.int queryTok
37e88 65 6e 69 7a 65 72 28 0a 20 20 73 71 6c 69 74 65  enizer(.  sqlite
37e89 33 20 2a 64 62 2c 20 0a 20 20 63 68 61 72 20 2a  3 *db, .  char *
37e8a 7a 4e 61 6d 65 2c 20 20 0a 20 20 63 6f 6e 73 74  zName,  .  const
37e8b 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
37e8c 65 72 5f 6d 6f 64 75 6c 65 20 2a 2a 70 70 0a 29  er_module **pp.)
37e8d 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71  {.  int rc;.  sq
37e8e 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
37e8f 74 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  t;.  const char 
37e90 7a 53 71 6c 5b 5d 20 3d 20 22 53 45 4c 45 43 54  zSql[] = "SELECT
37e91 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 28   fts3_tokenizer(
37e92 3f 29 22 3b 0a 0a 20 20 2a 70 70 20 3d 20 30 3b  ?)";..  *pp = 0;
37e93 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
37e94 70 72 65 70 61 72 65 5f 76 32 28 64 62 2c 20 7a  prepare_v2(db, z
37e95 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c  Sql, -1, &pStmt,
37e96 20 30 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53   0);.  if( rc!=S
37e97 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
37e98 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a  return rc;.  }..
37e99 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74    sqlite3_bind_t
37e9a 65 78 74 28 70 53 74 6d 74 2c 20 31 2c 20 7a 4e  ext(pStmt, 1, zN
37e9b 61 6d 65 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f  ame, -1, SQLITE_
37e9c 53 54 41 54 49 43 29 3b 0a 20 20 69 66 28 20 53  STATIC);.  if( S
37e9d 51 4c 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74  QLITE_ROW==sqlit
37e9e 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29 20 29  e3_step(pStmt) )
37e9f 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
37ea0 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 70 53  3_column_type(pS
37ea1 74 6d 74 2c 20 30 29 3d 3d 53 51 4c 49 54 45 5f  tmt, 0)==SQLITE_
37ea2 42 4c 4f 42 20 29 7b 0a 20 20 20 20 20 20 6d 65  BLOB ){.      me
37ea3 6d 63 70 79 28 70 70 2c 20 73 71 6c 69 74 65 33  mcpy(pp, sqlite3
37ea4 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 70 53 74  _column_blob(pSt
37ea5 6d 74 2c 20 30 29 2c 20 73 69 7a 65 6f 66 28 2a  mt, 0), sizeof(*
37ea6 70 70 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  pp));.    }.  }.
37ea7 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
37ea8 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74  3_finalize(pStmt
37ea9 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49  );.}..SQLITE_PRI
37eaa 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
37eab 33 46 74 73 33 53 69 6d 70 6c 65 54 6f 6b 65 6e  3Fts3SimpleToken
37eac 69 7a 65 72 4d 6f 64 75 6c 65 28 73 71 6c 69 74  izerModule(sqlit
37ead 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
37eae 75 6c 65 20 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64  ule const**ppMod
37eaf 75 6c 65 29 3b 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70  ule);../*.** Imp
37eb0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
37eb1 68 65 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69  he scalar functi
37eb2 6f 6e 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65  on fts3_tokenize
37eb3 72 5f 69 6e 74 65 72 6e 61 6c 5f 74 65 73 74 28  r_internal_test(
37eb4 29 2e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  )..** This funct
37eb5 69 6f 6e 20 69 73 20 75 73 65 64 20 66 6f 72 20  ion is used for 
37eb6 74 65 73 74 69 6e 67 20 6f 6e 6c 79 2c 20 69 74  testing only, it
37eb7 20 69 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 64   is not included
37eb8 20 69 6e 20 74 68 65 0a 2a 2a 20 62 75 69 6c 64   in the.** build
37eb9 20 75 6e 6c 65 73 73 20 53 51 4c 49 54 45 5f 54   unless SQLITE_T
37eba 45 53 54 20 69 73 20 64 65 66 69 6e 65 64 2e 0a  EST is defined..
37ebb 2a 2a 0a 2a 2a 20 54 68 65 20 70 75 72 70 6f 73  **.** The purpos
37ebc 65 20 6f 66 20 74 68 69 73 20 69 73 20 74 6f 20  e of this is to 
37ebd 74 65 73 74 20 74 68 61 74 20 74 68 65 20 66 74  test that the ft
37ebe 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 28 29 20 66  s3_tokenizer() f
37ebf 75 6e 63 74 69 6f 6e 0a 2a 2a 20 63 61 6e 20 62  unction.** can b
37ec0 65 20 75 73 65 64 20 61 73 20 64 65 73 69 67 6e  e used as design
37ec1 65 64 20 62 79 20 74 68 65 20 43 2d 63 6f 64 65  ed by the C-code
37ec2 20 69 6e 20 74 68 65 20 71 75 65 72 79 54 6f 6b   in the queryTok
37ec3 65 6e 69 7a 65 72 20 61 6e 64 0a 2a 2a 20 72 65  enizer and.** re
37ec4 67 69 73 74 65 72 54 6f 6b 65 6e 69 7a 65 72 28  gisterTokenizer(
37ec5 29 20 66 75 6e 63 74 69 6f 6e 73 20 61 62 6f 76  ) functions abov
37ec6 65 2e 20 54 68 65 73 65 20 74 77 6f 20 66 75 6e  e. These two fun
37ec7 63 74 69 6f 6e 73 20 61 72 65 20 72 65 70 65 61  ctions are repea
37ec8 74 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 52 45  ted.** in the RE
37ec9 41 44 4d 45 2e 74 6f 6b 65 6e 69 7a 65 72 20 66  ADME.tokenizer f
37eca 69 6c 65 20 61 73 20 61 6e 20 65 78 61 6d 70 6c  ile as an exampl
37ecb 65 2c 20 73 6f 20 69 74 20 69 73 20 69 6d 70 6f  e, so it is impo
37ecc 72 74 61 6e 74 20 74 6f 0a 2a 2a 20 74 65 73 74  rtant to.** test
37ecd 20 74 68 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20   them..**.** To 
37ece 72 75 6e 20 74 68 65 20 74 65 73 74 73 2c 20 65  run the tests, e
37ecf 76 61 6c 75 61 74 65 20 74 68 65 20 66 74 73 33  valuate the fts3
37ed0 5f 74 6f 6b 65 6e 69 7a 65 72 5f 69 6e 74 65 72  _tokenizer_inter
37ed1 6e 61 6c 5f 74 65 73 74 28 29 20 73 63 61 6c 61  nal_test() scala
37ed2 72 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 77 69  r.** function wi
37ed3 74 68 20 6e 6f 20 61 72 67 75 6d 65 6e 74 73 2e  th no arguments.
37ed4 20 41 6e 20 61 73 73 65 72 74 28 29 20 77 69 6c   An assert() wil
37ed5 6c 20 66 61 69 6c 20 69 66 20 61 20 70 72 6f 62  l fail if a prob
37ed6 6c 65 6d 20 69 73 0a 2a 2a 20 64 65 74 65 63 74  lem is.** detect
37ed7 65 64 2e 20 69 2e 65 2e 3a 0a 2a 2a 0a 2a 2a 20  ed. i.e.:.**.** 
37ed8 20 20 20 20 53 45 4c 45 43 54 20 66 74 73 33 5f      SELECT fts3_
37ed9 74 6f 6b 65 6e 69 7a 65 72 5f 69 6e 74 65 72 6e  tokenizer_intern
37eda 61 6c 5f 74 65 73 74 28 29 3b 0a 2a 2a 0a 2a 2f  al_test();.**.*/
37edb 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 74  .static void int
37edc 54 65 73 74 46 75 6e 63 28 0a 20 20 73 71 6c 69  TestFunc(.  sqli
37edd 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
37ede 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63  text,.  int argc
37edf 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
37ee0 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e  e **argv.){.  in
37ee1 74 20 72 63 3b 0a 20 20 63 6f 6e 73 74 20 73 71  t rc;.  const sq
37ee2 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
37ee3 6d 6f 64 75 6c 65 20 2a 70 31 3b 0a 20 20 63 6f  module *p1;.  co
37ee4 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  nst sqlite3_toke
37ee5 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 32  nizer_module *p2
37ee6 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
37ee7 3d 20 28 73 71 6c 69 74 65 33 20 2a 29 73 71 6c  = (sqlite3 *)sql
37ee8 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 63  ite3_user_data(c
37ee9 6f 6e 74 65 78 74 29 3b 0a 0a 20 20 2f 2a 20 54  ontext);..  /* T
37eea 65 73 74 20 74 68 65 20 71 75 65 72 79 20 66 75  est the query fu
37eeb 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69  nction */.  sqli
37eec 74 65 33 46 74 73 33 53 69 6d 70 6c 65 54 6f 6b  te3Fts3SimpleTok
37eed 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 26 70 31  enizerModule(&p1
37eee 29 3b 0a 20 20 72 63 20 3d 20 71 75 65 72 79 54  );.  rc = queryT
37eef 6f 6b 65 6e 69 7a 65 72 28 64 62 2c 20 22 73 69  okenizer(db, "si
37ef0 6d 70 6c 65 22 2c 20 26 70 32 29 3b 0a 20 20 61  mple", &p2);.  a
37ef1 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
37ef2 45 5f 4f 4b 20 29 3b 0a 20 20 61 73 73 65 72 74  E_OK );.  assert
37ef3 28 20 70 31 3d 3d 70 32 20 29 3b 0a 20 20 72 63  ( p1==p2 );.  rc
37ef4 20 3d 20 71 75 65 72 79 54 6f 6b 65 6e 69 7a 65   = queryTokenize
37ef5 72 28 64 62 2c 20 22 6e 6f 73 75 63 68 74 6f 6b  r(db, "nosuchtok
37ef6 65 6e 69 7a 65 72 22 2c 20 26 70 32 29 3b 0a 20  enizer", &p2);. 
37ef7 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
37ef8 49 54 45 5f 45 52 52 4f 52 20 29 3b 0a 20 20 61  ITE_ERROR );.  a
37ef9 73 73 65 72 74 28 20 70 32 3d 3d 30 20 29 3b 0a  ssert( p2==0 );.
37efa 20 20 61 73 73 65 72 74 28 20 30 3d 3d 73 74 72    assert( 0==str
37efb 63 6d 70 28 73 71 6c 69 74 65 33 5f 65 72 72 6d  cmp(sqlite3_errm
37efc 73 67 28 64 62 29 2c 20 22 75 6e 6b 6e 6f 77 6e  sg(db), "unknown
37efd 20 74 6f 6b 65 6e 69 7a 65 72 3a 20 6e 6f 73 75   tokenizer: nosu
37efe 63 68 74 6f 6b 65 6e 69 7a 65 72 22 29 20 29 3b  chtokenizer") );
37eff 0a 0a 20 20 2f 2a 20 54 65 73 74 20 74 68 65 20  ..  /* Test the 
37f00 73 74 6f 72 61 67 65 20 66 75 6e 63 74 69 6f 6e  storage function
37f01 20 2a 2f 0a 20 20 72 63 20 3d 20 72 65 67 69 73   */.  rc = regis
37f02 74 65 72 54 6f 6b 65 6e 69 7a 65 72 28 64 62 2c  terTokenizer(db,
37f03 20 22 6e 6f 73 75 63 68 74 6f 6b 65 6e 69 7a 65   "nosuchtokenize
37f04 72 22 2c 20 70 31 29 3b 0a 20 20 61 73 73 65 72  r", p1);.  asser
37f05 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
37f06 20 29 3b 0a 20 20 72 63 20 3d 20 71 75 65 72 79   );.  rc = query
37f07 54 6f 6b 65 6e 69 7a 65 72 28 64 62 2c 20 22 6e  Tokenizer(db, "n
37f08 6f 73 75 63 68 74 6f 6b 65 6e 69 7a 65 72 22 2c  osuchtokenizer",
37f09 20 26 70 32 29 3b 0a 20 20 61 73 73 65 72 74 28   &p2);.  assert(
37f0a 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
37f0b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 32 3d 3d  ;.  assert( p2==
37f0c 70 31 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33  p1 );..  sqlite3
37f0d 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e  _result_text(con
37f0e 74 65 78 74 2c 20 22 6f 6b 22 2c 20 2d 31 2c 20  text, "ok", -1, 
37f0f 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
37f10 7d 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  }..#endif../*.**
37f11 20 53 65 74 20 75 70 20 53 51 4c 20 6f 62 6a 65   Set up SQL obje
37f12 63 74 73 20 69 6e 20 64 61 74 61 62 61 73 65 20  cts in database 
37f13 64 62 20 75 73 65 64 20 74 6f 20 61 63 63 65 73  db used to acces
37f14 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  s the contents o
37f15 66 0a 2a 2a 20 74 68 65 20 68 61 73 68 20 74 61  f.** the hash ta
37f16 62 6c 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  ble pointed to b
37f17 79 20 61 72 67 75 6d 65 6e 74 20 70 48 61 73 68  y argument pHash
37f18 2e 20 54 68 65 20 68 61 73 68 20 74 61 62 6c 65  . The hash table
37f19 20 6d 75 73 74 0a 2a 2a 20 62 65 65 6e 20 69 6e   must.** been in
37f1a 69 74 69 61 6c 69 73 65 64 20 74 6f 20 75 73 65  itialised to use
37f1b 20 73 74 72 69 6e 67 20 6b 65 79 73 2c 20 61 6e   string keys, an
37f1c 64 20 74 6f 20 74 61 6b 65 20 61 20 70 72 69 76  d to take a priv
37f1d 61 74 65 20 63 6f 70 79 20 0a 2a 2a 20 6f 66 20  ate copy .** of 
37f1e 74 68 65 20 6b 65 79 20 77 68 65 6e 20 61 20 76  the key when a v
37f1f 61 6c 75 65 20 69 73 20 69 6e 73 65 72 74 65 64  alue is inserted
37f20 2e 20 69 2e 65 2e 20 62 79 20 61 20 63 61 6c 6c  . i.e. by a call
37f21 20 73 69 6d 69 6c 61 72 20 74 6f 3a 0a 2a 2a 0a   similar to:.**.
37f22 2a 2a 20 20 20 20 73 71 6c 69 74 65 33 46 74 73  **    sqlite3Fts
37f23 33 48 61 73 68 49 6e 69 74 28 70 48 61 73 68 2c  3HashInit(pHash,
37f24 20 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49 4e   FTS3_HASH_STRIN
37f25 47 2c 20 31 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 69  G, 1);.**.** Thi
37f26 73 20 66 75 6e 63 74 69 6f 6e 20 61 64 64 73 20  s function adds 
37f27 61 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f  a scalar functio
37f28 6e 20 28 73 65 65 20 68 65 61 64 65 72 20 63 6f  n (see header co
37f29 6d 6d 65 6e 74 20 61 62 6f 76 65 0a 2a 2a 20 73  mment above.** s
37f2a 63 61 6c 61 72 46 75 6e 63 28 29 20 69 6e 20 74  calarFunc() in t
37f2b 68 69 73 20 66 69 6c 65 20 66 6f 72 20 64 65 74  his file for det
37f2c 61 69 6c 73 29 20 61 6e 64 2c 20 69 66 20 45 4e  ails) and, if EN
37f2d 41 42 4c 45 5f 54 41 42 4c 45 20 69 73 0a 2a 2a  ABLE_TABLE is.**
37f2e 20 64 65 66 69 6e 65 64 20 61 74 20 63 6f 6d 70   defined at comp
37f2f 69 6c 61 74 69 6f 6e 20 74 69 6d 65 2c 20 61 20  ilation time, a 
37f30 74 65 6d 70 6f 72 61 72 79 20 76 69 72 74 75 61  temporary virtua
37f31 6c 20 74 61 62 6c 65 20 28 73 65 65 20 68 65 61  l table (see hea
37f32 64 65 72 20 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 20  der .** comment 
37f33 61 62 6f 76 65 20 73 74 72 75 63 74 20 48 61 73  above struct Has
37f34 68 54 61 62 6c 65 56 74 61 62 29 20 74 6f 20 74  hTableVtab) to t
37f35 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65  he database sche
37f36 6d 61 2e 20 42 6f 74 68 20 0a 2a 2a 20 70 72 6f  ma. Both .** pro
37f37 76 69 64 65 20 72 65 61 64 2f 77 72 69 74 65 20  vide read/write 
37f38 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 63 6f  access to the co
37f39 6e 74 65 6e 74 73 20 6f 66 20 2a 70 48 61 73 68  ntents of *pHash
37f3a 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72  ..**.** The thir
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 2c 20 7a 4e 61  is function, zNa
37f3d 6d 65 2c 20 69 73 20 75 73 65 64 20 61 73 20 74  me, is used as t
37f3e 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20 62 6f  he name.** of bo
37f3f 74 68 20 74 68 65 20 73 63 61 6c 61 72 20 61 6e  th the scalar an
37f40 64 2c 20 69 66 20 63 72 65 61 74 65 64 2c 20 74  d, if created, t
37f41 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
37f42 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
37f43 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
37f44 74 73 33 49 6e 69 74 48 61 73 68 54 61 62 6c 65  ts3InitHashTable
37f45 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
37f46 20 0a 20 20 66 74 73 33 48 61 73 68 20 2a 70 48   .  fts3Hash *pH
37f47 61 73 68 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68  ash, .  const ch
37f48 61 72 20 2a 7a 4e 61 6d 65 0a 29 7b 0a 20 20 69  ar *zName.){.  i
37f49 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
37f4a 4b 3b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 28  K;.  void *p = (
37f4b 76 6f 69 64 20 2a 29 70 48 61 73 68 3b 0a 20 20  void *)pHash;.  
37f4c 63 6f 6e 73 74 20 69 6e 74 20 61 6e 79 20 3d 20  const int any = 
37f4d 53 51 4c 49 54 45 5f 41 4e 59 3b 0a 20 20 63 68  SQLITE_ANY;.  ch
37f4e 61 72 20 2a 7a 54 65 73 74 20 3d 20 30 3b 0a 20  ar *zTest = 0;. 
37f4f 20 63 68 61 72 20 2a 7a 54 65 73 74 32 20 3d 20   char *zTest2 = 
37f50 30 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  0;..#ifdef SQLIT
37f51 45 5f 54 45 53 54 0a 20 20 76 6f 69 64 20 2a 70  E_TEST.  void *p
37f52 64 62 20 3d 20 28 76 6f 69 64 20 2a 29 64 62 3b  db = (void *)db;
37f53 0a 20 20 7a 54 65 73 74 20 3d 20 73 71 6c 69 74  .  zTest = sqlit
37f54 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 5f 74  e3_mprintf("%s_t
37f55 65 73 74 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20  est", zName);.  
37f56 7a 54 65 73 74 32 20 3d 20 73 71 6c 69 74 65 33  zTest2 = sqlite3
37f57 5f 6d 70 72 69 6e 74 66 28 22 25 73 5f 69 6e 74  _mprintf("%s_int
37f58 65 72 6e 61 6c 5f 74 65 73 74 22 2c 20 7a 4e 61  ernal_test", zNa
37f59 6d 65 29 3b 0a 20 20 69 66 28 20 21 7a 54 65 73  me);.  if( !zTes
37f5a 74 20 7c 7c 20 21 7a 54 65 73 74 32 20 29 7b 0a  t || !zTest2 ){.
37f5b 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
37f5c 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 23 65 6e 64 69  NOMEM;.  }.#endi
37f5d 66 0a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  f..  if( rc!=SQL
37f5e 49 54 45 5f 4f 4b 0a 20 20 20 7c 7c 20 28 72 63  ITE_OK.   || (rc
37f5f 20 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74   = sqlite3_creat
37f60 65 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a  e_function(db, z
37f61 4e 61 6d 65 2c 20 31 2c 20 61 6e 79 2c 20 70 2c  Name, 1, any, p,
37f62 20 73 63 61 6c 61 72 46 75 6e 63 2c 20 30 2c 20   scalarFunc, 0, 
37f63 30 29 29 0a 20 20 20 7c 7c 20 28 72 63 20 3d 20  0)).   || (rc = 
37f64 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
37f65 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d  unction(db, zNam
37f66 65 2c 20 32 2c 20 61 6e 79 2c 20 70 2c 20 73 63  e, 2, any, p, sc
37f67 61 6c 61 72 46 75 6e 63 2c 20 30 2c 20 30 29 29  alarFunc, 0, 0))
37f68 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
37f69 45 53 54 0a 20 20 20 7c 7c 20 28 72 63 20 3d 20  EST.   || (rc = 
37f6a 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
37f6b 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 54 65 73  unction(db, zTes
37f6c 74 2c 20 32 2c 20 61 6e 79 2c 20 70 2c 20 74 65  t, 2, any, p, te
37f6d 73 74 46 75 6e 63 2c 20 30 2c 20 30 29 29 0a 20  stFunc, 0, 0)). 
37f6e 20 20 7c 7c 20 28 72 63 20 3d 20 73 71 6c 69 74    || (rc = sqlit
37f6f 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
37f70 6f 6e 28 64 62 2c 20 7a 54 65 73 74 2c 20 33 2c  on(db, zTest, 3,
37f71 20 61 6e 79 2c 20 70 2c 20 74 65 73 74 46 75 6e   any, p, testFun
37f72 63 2c 20 30 2c 20 30 29 29 0a 20 20 20 7c 7c 20  c, 0, 0)).   || 
37f73 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72  (rc = sqlite3_cr
37f74 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 64 62  eate_function(db
37f75 2c 20 7a 54 65 73 74 32 2c 20 30 2c 20 61 6e 79  , zTest2, 0, any
37f76 2c 20 70 64 62 2c 20 69 6e 74 54 65 73 74 46 75  , pdb, intTestFu
37f77 6e 63 2c 20 30 2c 20 30 29 29 0a 23 65 6e 64 69  nc, 0, 0)).#endi
37f78 66 0a 20 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65  f.  );..  sqlite
37f79 33 5f 66 72 65 65 28 7a 54 65 73 74 29 3b 0a 20  3_free(zTest);. 
37f7a 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 54   sqlite3_free(zT
37f7b 65 73 74 32 29 3b 0a 20 20 72 65 74 75 72 6e 20  est2);.  return 
37f7c 72 63 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a  rc;.}..#endif /*
37f7d 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
37f7e 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65  _CORE) || define
37f7f 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
37f80 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  FTS3) */../*****
37f81 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
37f82 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 2e   fts3_tokenizer.
37f83 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
37f84 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37f85 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
37f86 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
37f87 66 69 6c 65 20 66 74 73 33 5f 74 6f 6b 65 6e 69  file fts3_tokeni
37f88 7a 65 72 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  zer1.c *********
37f89 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37f8a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
37f8b 32 30 30 36 20 4f 63 74 20 31 30 0a 2a 2a 0a 2a  2006 Oct 10.**.*
37f8c 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
37f8d 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
37f8e 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
37f8f 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
37f90 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
37f91 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
37f92 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
37f93 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
37f94 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
37f95 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
37f96 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
37f97 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
37f98 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
37f99 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
37f9a 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
37f9b 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
37f9c 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
37f9d 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
37f9e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37f9f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37fa1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
37fa2 2a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  **.** Implementa
37fa3 74 69 6f 6e 20 6f 66 20 74 68 65 20 22 73 69 6d  tion of the "sim
37fa4 70 6c 65 22 20 66 75 6c 6c 2d 74 65 78 74 2d 73  ple" full-text-s
37fa5 65 61 72 63 68 20 74 6f 6b 65 6e 69 7a 65 72 2e  earch tokenizer.
37fa6 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63  .*/../*.** The c
37fa7 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65  ode in this file
37fa8 20 69 73 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65   is only compile
37fa9 64 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  d if:.**.**     
37faa 2a 20 54 68 65 20 46 54 53 33 20 6d 6f 64 75 6c  * The FTS3 modul
37fab 65 20 69 73 20 62 65 69 6e 67 20 62 75 69 6c 74  e is being built
37fac 20 61 73 20 61 6e 20 65 78 74 65 6e 73 69 6f 6e   as an extension
37fad 0a 2a 2a 20 20 20 20 20 20 20 28 69 6e 20 77 68  .**       (in wh
37fae 69 63 68 20 63 61 73 65 20 53 51 4c 49 54 45 5f  ich case SQLITE_
37faf 43 4f 52 45 20 69 73 20 6e 6f 74 20 64 65 66 69  CORE is not defi
37fb0 6e 65 64 29 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20  ned), or.**.**  
37fb1 20 20 20 2a 20 54 68 65 20 46 54 53 33 20 6d 6f     * The FTS3 mo
37fb2 64 75 6c 65 20 69 73 20 62 65 69 6e 67 20 62 75  dule is being bu
37fb3 69 6c 74 20 69 6e 74 6f 20 74 68 65 20 63 6f 72  ilt into the cor
37fb4 65 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 53 51  e of.**       SQ
37fb5 4c 69 74 65 20 28 69 6e 20 77 68 69 63 68 20 63  Lite (in which c
37fb6 61 73 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ase SQLITE_ENABL
37fb7 45 5f 46 54 53 33 20 69 73 20 64 65 66 69 6e 65  E_FTS3 is define
37fb8 64 29 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69  d)..*/.#if !defi
37fb9 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29  ned(SQLITE_CORE)
37fba 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
37fbb 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 0a  TE_ENABLE_FTS3).
37fbc 0a 0a 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75  ....typedef stru
37fbd 63 74 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69  ct simple_tokeni
37fbe 7a 65 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f  zer {.  sqlite3_
37fbf 74 6f 6b 65 6e 69 7a 65 72 20 62 61 73 65 3b 0a  tokenizer base;.
37fc0 20 20 63 68 61 72 20 64 65 6c 69 6d 5b 31 32 38    char delim[128
37fc1 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ];             /
37fc2 2a 20 66 6c 61 67 20 41 53 43 49 49 20 64 65 6c  * flag ASCII del
37fc3 69 6d 69 74 65 72 73 20 2a 2f 0a 7d 20 73 69 6d  imiters */.} sim
37fc4 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 3b 0a 0a  ple_tokenizer;..
37fc5 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73  typedef struct s
37fc6 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f  imple_tokenizer_
37fc7 63 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c 69 74  cursor {.  sqlit
37fc8 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  e3_tokenizer_cur
37fc9 73 6f 72 20 62 61 73 65 3b 0a 20 20 63 6f 6e 73  sor base;.  cons
37fca 74 20 63 68 61 72 20 2a 70 49 6e 70 75 74 3b 20  t char *pInput; 
37fcb 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 70 75           /* inpu
37fcc 74 20 77 65 20 61 72 65 20 74 6f 6b 65 6e 69 7a  t we are tokeniz
37fcd 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79  ing */.  int nBy
37fce 74 65 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  tes;            
37fcf 20 20 20 20 20 20 2f 2a 20 73 69 7a 65 20 6f 66        /* size of
37fd0 20 74 68 65 20 69 6e 70 75 74 20 2a 2f 0a 20 20   the input */.  
37fd1 69 6e 74 20 69 4f 66 66 73 65 74 3b 20 20 20 20  int iOffset;    
37fd2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
37fd3 63 75 72 72 65 6e 74 20 70 6f 73 69 74 69 6f 6e  current position
37fd4 20 69 6e 20 70 49 6e 70 75 74 20 2a 2f 0a 20 20   in pInput */.  
37fd5 69 6e 74 20 69 54 6f 6b 65 6e 3b 20 20 20 20 20  int iToken;     
37fd6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
37fd7 69 6e 64 65 78 20 6f 66 20 6e 65 78 74 20 74 6f  index of next to
37fd8 6b 65 6e 20 74 6f 20 62 65 20 72 65 74 75 72 6e  ken to be return
37fd9 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 70 54  ed */.  char *pT
37fda 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20  oken;           
37fdb 20 20 20 20 20 2f 2a 20 73 74 6f 72 61 67 65 20       /* storage 
37fdc 66 6f 72 20 63 75 72 72 65 6e 74 20 74 6f 6b 65  for current toke
37fdd 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 6b 65  n */.  int nToke
37fde 6e 41 6c 6c 6f 63 61 74 65 64 3b 20 20 20 20 20  nAllocated;     
37fdf 20 20 20 20 2f 2a 20 73 70 61 63 65 20 61 6c 6c      /* space all
37fe0 6f 63 61 74 65 64 20 74 6f 20 7a 54 6f 6b 65 6e  ocated to zToken
37fe1 20 62 75 66 66 65 72 20 2a 2f 0a 7d 20 73 69 6d   buffer */.} sim
37fe2 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  ple_tokenizer_cu
37fe3 72 73 6f 72 3b 0a 0a 0a 2f 2a 20 46 6f 72 77 61  rsor;.../* Forwa
37fe4 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 2a  rd declaration *
37fe5 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73  /.static const s
37fe6 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
37fe7 5f 6d 6f 64 75 6c 65 20 73 69 6d 70 6c 65 54 6f  _module simpleTo
37fe8 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 3b 0a 0a  kenizerModule;..
37fe9 73 74 61 74 69 63 20 69 6e 74 20 73 69 6d 70 6c  static int simpl
37fea 65 44 65 6c 69 6d 28 73 69 6d 70 6c 65 5f 74 6f  eDelim(simple_to
37feb 6b 65 6e 69 7a 65 72 20 2a 74 2c 20 75 6e 73 69  kenizer *t, unsi
37fec 67 6e 65 64 20 63 68 61 72 20 63 29 7b 0a 20 20  gned char c){.  
37fed 72 65 74 75 72 6e 20 63 3c 30 78 38 30 20 26 26  return c<0x80 &&
37fee 20 74 2d 3e 64 65 6c 69 6d 5b 63 5d 3b 0a 7d 0a   t->delim[c];.}.
37fef 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20  ./*.** Create a 
37ff0 6e 65 77 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6e  new tokenizer in
37ff1 73 74 61 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74 69  stance..*/.stati
37ff2 63 20 69 6e 74 20 73 69 6d 70 6c 65 43 72 65 61  c int simpleCrea
37ff3 74 65 28 0a 20 20 69 6e 74 20 61 72 67 63 2c 20  te(.  int argc, 
37ff4 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 63 6f 6e  const char * con
37ff5 73 74 20 2a 61 72 67 76 2c 0a 20 20 73 71 6c 69  st *argv,.  sqli
37ff6 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 2a  te3_tokenizer **
37ff7 70 70 54 6f 6b 65 6e 69 7a 65 72 0a 29 7b 0a 20  ppTokenizer.){. 
37ff8 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65   simple_tokenize
37ff9 72 20 2a 74 3b 0a 0a 20 20 74 20 3d 20 28 73 69  r *t;..  t = (si
37ffa 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a  mple_tokenizer *
37ffb 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  ) sqlite3_malloc
37ffc 28 73 69 7a 65 6f 66 28 2a 74 29 29 3b 0a 20 20  (sizeof(*t));.  
37ffd 69 66 28 20 74 3d 3d 4e 55 4c 4c 20 29 20 72 65  if( t==NULL ) re
37ffe 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
37fff 4d 3b 0a 20 20 6d 65 6d 73 65 74 28 74 2c 20 30  M;.  memset(t, 0
38000 2c 20 73 69 7a 65 6f 66 28 2a 74 29 29 3b 0a 0a  , sizeof(*t));..
38001 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29    /* TODO(shess)
38002 20 44 65 6c 69 6d 69 74 65 72 73 20 6e 65 65 64   Delimiters need
38003 20 74 6f 20 72 65 6d 61 69 6e 20 74 68 65 20 73   to remain the s
38004 61 6d 65 20 66 72 6f 6d 20 72 75 6e 20 74 6f 20  ame from run to 
38005 72 75 6e 2c 0a 20 20 2a 2a 20 65 6c 73 65 20 77  run,.  ** else w
38006 65 20 6e 65 65 64 20 74 6f 20 72 65 69 6e 64 65  e need to reinde
38007 78 2e 20 20 4f 6e 65 20 73 6f 6c 75 74 69 6f 6e  x.  One solution
38008 20 77 6f 75 6c 64 20 62 65 20 61 20 6d 65 74 61   would be a meta
38009 2d 74 61 62 6c 65 20 74 6f 0a 20 20 2a 2a 20 74  -table to.  ** t
3800a 72 61 63 6b 20 73 75 63 68 20 69 6e 66 6f 72 6d  rack such inform
3800b 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 64 61 74  ation in the dat
3800c 61 62 61 73 65 2c 20 74 68 65 6e 20 77 65 27 64  abase, then we'd
3800d 20 6f 6e 6c 79 20 77 61 6e 74 20 74 68 69 73 0a   only want this.
3800e 20 20 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e    ** information
3800f 20 6f 6e 20 74 68 65 20 69 6e 69 74 69 61 6c 20   on the initial 
38010 63 72 65 61 74 65 2e 0a 20 20 2a 2f 0a 20 20 69  create..  */.  i
38011 66 28 20 61 72 67 63 3e 31 20 29 7b 0a 20 20 20  f( argc>1 ){.   
38012 20 69 6e 74 20 69 2c 20 6e 20 3d 20 73 74 72 6c   int i, n = strl
38013 65 6e 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20  en(argv[1]);.   
38014 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69   for(i=0; i<n; i
38015 2b 2b 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67  ++){.      unsig
38016 6e 65 64 20 63 68 61 72 20 63 68 20 3d 20 61 72  ned char ch = ar
38017 67 76 5b 31 5d 5b 69 5d 3b 0a 20 20 20 20 20 20  gv[1][i];.      
38018 2f 2a 20 57 65 20 65 78 70 6c 69 63 69 74 6c 79  /* We explicitly
38019 20 64 6f 6e 27 74 20 73 75 70 70 6f 72 74 20 55   don't support U
3801a 54 46 2d 38 20 64 65 6c 69 6d 69 74 65 72 73 20  TF-8 delimiters 
3801b 66 6f 72 20 6e 6f 77 2e 20 2a 2f 0a 20 20 20 20  for now. */.    
3801c 20 20 69 66 28 20 63 68 3e 3d 30 78 38 30 20 29    if( ch>=0x80 )
3801d 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
3801e 33 5f 66 72 65 65 28 74 29 3b 0a 20 20 20 20 20  3_free(t);.     
3801f 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
38020 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 7d 0a  _ERROR;.      }.
38021 20 20 20 20 20 20 74 2d 3e 64 65 6c 69 6d 5b 63        t->delim[c
38022 68 5d 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  h] = 1;.    }.  
38023 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 2f 2a 20  } else {.    /* 
38024 4d 61 72 6b 20 6e 6f 6e 2d 61 6c 70 68 61 6e 75  Mark non-alphanu
38025 6d 65 72 69 63 20 41 53 43 49 49 20 63 68 61 72  meric ASCII char
38026 61 63 74 65 72 73 20 61 73 20 64 65 6c 69 6d 69  acters as delimi
38027 74 65 72 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ters */.    int 
38028 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b 20  i;.    for(i=1; 
38029 69 3c 30 78 38 30 3b 20 69 2b 2b 29 7b 0a 20 20  i<0x80; i++){.  
3802a 20 20 20 20 74 2d 3e 64 65 6c 69 6d 5b 69 5d 20      t->delim[i] 
3802b 3d 20 21 69 73 61 6c 6e 75 6d 28 69 29 3b 0a 20  = !isalnum(i);. 
3802c 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 70 54     }.  }..  *ppT
3802d 6f 6b 65 6e 69 7a 65 72 20 3d 20 26 74 2d 3e 62  okenizer = &t->b
3802e 61 73 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  ase;.  return SQ
3802f 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
38030 2a 20 44 65 73 74 72 6f 79 20 61 20 74 6f 6b 65  * Destroy a toke
38031 6e 69 7a 65 72 0a 2a 2f 0a 73 74 61 74 69 63 20  nizer.*/.static 
38032 69 6e 74 20 73 69 6d 70 6c 65 44 65 73 74 72 6f  int simpleDestro
38033 79 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  y(sqlite3_tokeni
38034 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 29  zer *pTokenizer)
38035 7b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  {.  sqlite3_free
38036 28 70 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 20 20  (pTokenizer);.  
38037 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
38038 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61  ;.}../*.** Prepa
38039 72 65 20 74 6f 20 62 65 67 69 6e 20 74 6f 6b 65  re to begin toke
3803a 6e 69 7a 69 6e 67 20 61 20 70 61 72 74 69 63 75  nizing a particu
3803b 6c 61 72 20 73 74 72 69 6e 67 2e 20 20 54 68 65  lar string.  The
3803c 20 69 6e 70 75 74 0a 2a 2a 20 73 74 72 69 6e 67   input.** string
3803d 20 74 6f 20 62 65 20 74 6f 6b 65 6e 69 7a 65 64   to be tokenized
3803e 20 69 73 20 70 49 6e 70 75 74 5b 30 2e 2e 6e 42   is pInput[0..nB
3803f 79 74 65 73 2d 31 5d 2e 20 20 41 20 63 75 72 73  ytes-1].  A curs
38040 6f 72 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69 6e  or.** used to in
38041 63 72 65 6d 65 6e 74 61 6c 6c 79 20 74 6f 6b 65  crementally toke
38042 6e 69 7a 65 20 74 68 69 73 20 73 74 72 69 6e 67  nize this string
38043 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20   is returned in 
38044 0a 2a 2a 20 2a 70 70 43 75 72 73 6f 72 2e 0a 2a  .** *ppCursor..*
38045 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 69 6d  /.static int sim
38046 70 6c 65 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74  pleOpen(.  sqlit
38047 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54  e3_tokenizer *pT
38048 6f 6b 65 6e 69 7a 65 72 2c 20 20 20 20 20 20 20  okenizer,       
38049 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 69 7a    /* The tokeniz
3804a 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  er */.  const ch
3804b 61 72 20 2a 70 49 6e 70 75 74 2c 20 69 6e 74 20  ar *pInput, int 
3804c 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20 20 2f  nBytes,        /
3804d 2a 20 53 74 72 69 6e 67 20 74 6f 20 62 65 20 74  * String to be t
3804e 6f 6b 65 6e 69 7a 65 64 20 2a 2f 0a 20 20 73 71  okenized */.  sq
3804f 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
38050 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f  cursor **ppCurso
38051 72 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f 6b  r    /* OUT: Tok
38052 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72  enization cursor
38053 20 2a 2f 0a 29 7b 0a 20 20 73 69 6d 70 6c 65 5f   */.){.  simple_
38054 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
38055 20 2a 63 3b 0a 0a 20 20 63 20 3d 20 28 73 69 6d   *c;..  c = (sim
38056 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  ple_tokenizer_cu
38057 72 73 6f 72 20 2a 29 20 73 71 6c 69 74 65 33 5f  rsor *) sqlite3_
38058 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 63  malloc(sizeof(*c
38059 29 29 3b 0a 20 20 69 66 28 20 63 3d 3d 4e 55 4c  ));.  if( c==NUL
3805a 4c 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  L ) return SQLIT
3805b 45 5f 4e 4f 4d 45 4d 3b 0a 0a 20 20 63 2d 3e 70  E_NOMEM;..  c->p
3805c 49 6e 70 75 74 20 3d 20 70 49 6e 70 75 74 3b 0a  Input = pInput;.
3805d 20 20 69 66 28 20 70 49 6e 70 75 74 3d 3d 30 20    if( pInput==0 
3805e 29 7b 0a 20 20 20 20 63 2d 3e 6e 42 79 74 65 73  ){.    c->nBytes
3805f 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66   = 0;.  }else if
38060 28 20 6e 42 79 74 65 73 3c 30 20 29 7b 0a 20 20  ( nBytes<0 ){.  
38061 20 20 63 2d 3e 6e 42 79 74 65 73 20 3d 20 28 69    c->nBytes = (i
38062 6e 74 29 73 74 72 6c 65 6e 28 70 49 6e 70 75 74  nt)strlen(pInput
38063 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
38064 63 2d 3e 6e 42 79 74 65 73 20 3d 20 6e 42 79 74  c->nBytes = nByt
38065 65 73 3b 0a 20 20 7d 0a 20 20 63 2d 3e 69 4f 66  es;.  }.  c->iOf
38066 66 73 65 74 20 3d 20 30 3b 20 20 20 20 20 20 20  fset = 0;       
38067 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 74 61            /* sta
38068 72 74 20 74 6f 6b 65 6e 69 7a 69 6e 67 20 61 74  rt tokenizing at
38069 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 2a   the beginning *
3806a 2f 0a 20 20 63 2d 3e 69 54 6f 6b 65 6e 20 3d 20  /.  c->iToken = 
3806b 30 3b 0a 20 20 63 2d 3e 70 54 6f 6b 65 6e 20 3d  0;.  c->pToken =
3806c 20 4e 55 4c 4c 3b 20 20 20 20 20 20 20 20 20 20   NULL;          
3806d 20 20 20 20 20 2f 2a 20 6e 6f 20 73 70 61 63 65       /* no space
3806e 20 61 6c 6c 6f 63 61 74 65 64 2c 20 79 65 74 2e   allocated, yet.
3806f 20 2a 2f 0a 20 20 63 2d 3e 6e 54 6f 6b 65 6e 41   */.  c->nTokenA
38070 6c 6c 6f 63 61 74 65 64 20 3d 20 30 3b 0a 0a 20  llocated = 0;.. 
38071 20 2a 70 70 43 75 72 73 6f 72 20 3d 20 26 63 2d   *ppCursor = &c-
38072 3e 62 61 73 65 3b 0a 20 20 72 65 74 75 72 6e 20  >base;.  return 
38073 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
38074 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 74 6f 6b 65  .** Close a toke
38075 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72 20  nization cursor 
38076 70 72 65 76 69 6f 75 73 6c 79 20 6f 70 65 6e 65  previously opene
38077 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 0a 2a  d by a call to.*
38078 2a 20 73 69 6d 70 6c 65 4f 70 65 6e 28 29 20 61  * simpleOpen() a
38079 62 6f 76 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  bove..*/.static 
3807a 69 6e 74 20 73 69 6d 70 6c 65 43 6c 6f 73 65 28  int simpleClose(
3807b 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
3807c 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f  r_cursor *pCurso
3807d 72 29 7b 0a 20 20 73 69 6d 70 6c 65 5f 74 6f 6b  r){.  simple_tok
3807e 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 63  enizer_cursor *c
3807f 20 3d 20 28 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e   = (simple_token
38080 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 29 20 70  izer_cursor *) p
38081 43 75 72 73 6f 72 3b 0a 20 20 73 71 6c 69 74 65  Cursor;.  sqlite
38082 33 5f 66 72 65 65 28 63 2d 3e 70 54 6f 6b 65 6e  3_free(c->pToken
38083 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
38084 65 28 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  e(c);.  return S
38085 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
38086 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 6e  ** Extract the n
38087 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f 6d 20 61  ext token from a
38088 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75   tokenization cu
38089 72 73 6f 72 2e 20 20 54 68 65 20 63 75 72 73 6f  rsor.  The curso
3808a 72 20 6d 75 73 74 0a 2a 2a 20 68 61 76 65 20 62  r must.** have b
3808b 65 65 6e 20 6f 70 65 6e 65 64 20 62 79 20 61 20  een opened by a 
3808c 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73 69  prior call to si
3808d 6d 70 6c 65 4f 70 65 6e 28 29 2e 0a 2a 2f 0a 73  mpleOpen()..*/.s
3808e 74 61 74 69 63 20 69 6e 74 20 73 69 6d 70 6c 65  tatic int simple
3808f 4e 65 78 74 28 0a 20 20 73 71 6c 69 74 65 33 5f  Next(.  sqlite3_
38090 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
38091 20 2a 70 43 75 72 73 6f 72 2c 20 20 2f 2a 20 43   *pCursor,  /* C
38092 75 72 73 6f 72 20 72 65 74 75 72 6e 65 64 20 62  ursor returned b
38093 79 20 73 69 6d 70 6c 65 4f 70 65 6e 20 2a 2f 0a  y simpleOpen */.
38094 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70    const char **p
38095 70 54 6f 6b 65 6e 2c 20 20 20 20 20 20 20 20 20  pToken,         
38096 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 2a 70        /* OUT: *p
38097 70 54 6f 6b 65 6e 20 69 73 20 74 68 65 20 74 6f  pToken is the to
38098 6b 65 6e 20 74 65 78 74 20 2a 2f 0a 20 20 69 6e  ken text */.  in
38099 74 20 2a 70 6e 42 79 74 65 73 2c 20 20 20 20 20  t *pnBytes,     
3809a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3809b 20 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72    /* OUT: Number
3809c 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 6f 6b   of bytes in tok
3809d 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 53  en */.  int *piS
3809e 74 61 72 74 4f 66 66 73 65 74 2c 20 20 20 20 20  tartOffset,     
3809f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
380a0 55 54 3a 20 53 74 61 72 74 69 6e 67 20 6f 66 66  UT: Starting off
380a1 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a  set of token */.
380a2 20 20 69 6e 74 20 2a 70 69 45 6e 64 4f 66 66 73    int *piEndOffs
380a3 65 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  et,             
380a4 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e        /* OUT: En
380a5 64 69 6e 67 20 6f 66 66 73 65 74 20 6f 66 20 74  ding offset of t
380a6 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  oken */.  int *p
380a7 69 50 6f 73 69 74 69 6f 6e 20 20 20 20 20 20 20  iPosition       
380a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
380a9 20 4f 55 54 3a 20 50 6f 73 69 74 69 6f 6e 20 69   OUT: Position i
380aa 6e 74 65 67 65 72 20 6f 66 20 74 6f 6b 65 6e 20  nteger of token 
380ab 2a 2f 0a 29 7b 0a 20 20 73 69 6d 70 6c 65 5f 74  */.){.  simple_t
380ac 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
380ad 2a 63 20 3d 20 28 73 69 6d 70 6c 65 5f 74 6f 6b  *c = (simple_tok
380ae 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 29  enizer_cursor *)
380af 20 70 43 75 72 73 6f 72 3b 0a 20 20 73 69 6d 70   pCursor;.  simp
380b0 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 74 20  le_tokenizer *t 
380b1 3d 20 28 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69  = (simple_tokeni
380b2 7a 65 72 20 2a 29 20 70 43 75 72 73 6f 72 2d 3e  zer *) pCursor->
380b3 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 75 6e  pTokenizer;.  un
380b4 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 20 3d  signed char *p =
380b5 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
380b6 2a 29 63 2d 3e 70 49 6e 70 75 74 3b 0a 0a 20 20  *)c->pInput;..  
380b7 77 68 69 6c 65 28 20 63 2d 3e 69 4f 66 66 73 65  while( c->iOffse
380b8 74 3c 63 2d 3e 6e 42 79 74 65 73 20 29 7b 0a 20  t<c->nBytes ){. 
380b9 20 20 20 69 6e 74 20 69 53 74 61 72 74 4f 66 66     int iStartOff
380ba 73 65 74 3b 0a 0a 20 20 20 20 2f 2a 20 53 63 61  set;..    /* Sca
380bb 6e 20 70 61 73 74 20 64 65 6c 69 6d 69 74 65 72  n past delimiter
380bc 20 63 68 61 72 61 63 74 65 72 73 20 2a 2f 0a 20   characters */. 
380bd 20 20 20 77 68 69 6c 65 28 20 63 2d 3e 69 4f 66     while( c->iOf
380be 66 73 65 74 3c 63 2d 3e 6e 42 79 74 65 73 20 26  fset<c->nBytes &
380bf 26 20 73 69 6d 70 6c 65 44 65 6c 69 6d 28 74 2c  & simpleDelim(t,
380c0 20 70 5b 63 2d 3e 69 4f 66 66 73 65 74 5d 29 20   p[c->iOffset]) 
380c1 29 7b 0a 20 20 20 20 20 20 63 2d 3e 69 4f 66 66  ){.      c->iOff
380c2 73 65 74 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20  set++;.    }..  
380c3 20 20 2f 2a 20 43 6f 75 6e 74 20 6e 6f 6e 2d 64    /* Count non-d
380c4 65 6c 69 6d 69 74 65 72 20 63 68 61 72 61 63 74  elimiter charact
380c5 65 72 73 2e 20 2a 2f 0a 20 20 20 20 69 53 74 61  ers. */.    iSta
380c6 72 74 4f 66 66 73 65 74 20 3d 20 63 2d 3e 69 4f  rtOffset = c->iO
380c7 66 66 73 65 74 3b 0a 20 20 20 20 77 68 69 6c 65  ffset;.    while
380c8 28 20 63 2d 3e 69 4f 66 66 73 65 74 3c 63 2d 3e  ( c->iOffset<c->
380c9 6e 42 79 74 65 73 20 26 26 20 21 73 69 6d 70 6c  nBytes && !simpl
380ca 65 44 65 6c 69 6d 28 74 2c 20 70 5b 63 2d 3e 69  eDelim(t, p[c->i
380cb 4f 66 66 73 65 74 5d 29 20 29 7b 0a 20 20 20 20  Offset]) ){.    
380cc 20 20 63 2d 3e 69 4f 66 66 73 65 74 2b 2b 3b 0a    c->iOffset++;.
380cd 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 63      }..    if( c
380ce 2d 3e 69 4f 66 66 73 65 74 3e 69 53 74 61 72 74  ->iOffset>iStart
380cf 4f 66 66 73 65 74 20 29 7b 0a 20 20 20 20 20 20  Offset ){.      
380d0 69 6e 74 20 69 2c 20 6e 20 3d 20 63 2d 3e 69 4f  int i, n = c->iO
380d1 66 66 73 65 74 2d 69 53 74 61 72 74 4f 66 66 73  ffset-iStartOffs
380d2 65 74 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 3e  et;.      if( n>
380d3 63 2d 3e 6e 54 6f 6b 65 6e 41 6c 6c 6f 63 61 74  c->nTokenAllocat
380d4 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 63 2d  ed ){.        c-
380d5 3e 6e 54 6f 6b 65 6e 41 6c 6c 6f 63 61 74 65 64  >nTokenAllocated
380d6 20 3d 20 6e 2b 32 30 3b 0a 20 20 20 20 20 20 20   = n+20;.       
380d7 20 63 2d 3e 70 54 6f 6b 65 6e 20 3d 20 73 71 6c   c->pToken = sql
380d8 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 63 2d 3e  ite3_realloc(c->
380d9 70 54 6f 6b 65 6e 2c 20 63 2d 3e 6e 54 6f 6b 65  pToken, c->nToke
380da 6e 41 6c 6c 6f 63 61 74 65 64 29 3b 0a 20 20 20  nAllocated);.   
380db 20 20 20 20 20 69 66 28 20 63 2d 3e 70 54 6f 6b       if( c->pTok
380dc 65 6e 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75 72  en==NULL ) retur
380dd 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
380de 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f        }.      fo
380df 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29  r(i=0; i<n; i++)
380e0 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 4f 44  {.        /* TOD
380e1 4f 28 73 68 65 73 73 29 20 54 68 69 73 20 6e 65  O(shess) This ne
380e2 65 64 73 20 65 78 70 61 6e 73 69 6f 6e 20 74 6f  eds expansion to
380e3 20 68 61 6e 64 6c 65 20 55 54 46 2d 38 0a 20 20   handle UTF-8.  
380e4 20 20 20 20 20 20 2a 2a 20 63 61 73 65 2d 69 6e        ** case-in
380e5 73 65 6e 73 69 74 69 76 69 74 79 2e 0a 20 20 20  sensitivity..   
380e6 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
380e7 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 68  unsigned char ch
380e8 20 3d 20 70 5b 69 53 74 61 72 74 4f 66 66 73 65   = p[iStartOffse
380e9 74 2b 69 5d 3b 0a 20 20 20 20 20 20 20 20 63 2d  t+i];.        c-
380ea 3e 70 54 6f 6b 65 6e 5b 69 5d 20 3d 20 63 68 3c  >pToken[i] = ch<
380eb 30 78 38 30 20 3f 20 74 6f 6c 6f 77 65 72 28 63  0x80 ? tolower(c
380ec 68 29 20 3a 20 63 68 3b 0a 20 20 20 20 20 20 7d  h) : ch;.      }
380ed 0a 20 20 20 20 20 20 2a 70 70 54 6f 6b 65 6e 20  .      *ppToken 
380ee 3d 20 63 2d 3e 70 54 6f 6b 65 6e 3b 0a 20 20 20  = c->pToken;.   
380ef 20 20 20 2a 70 6e 42 79 74 65 73 20 3d 20 6e 3b     *pnBytes = n;
380f0 0a 20 20 20 20 20 20 2a 70 69 53 74 61 72 74 4f  .      *piStartO
380f1 66 66 73 65 74 20 3d 20 69 53 74 61 72 74 4f 66  ffset = iStartOf
380f2 66 73 65 74 3b 0a 20 20 20 20 20 20 2a 70 69 45  fset;.      *piE
380f3 6e 64 4f 66 66 73 65 74 20 3d 20 63 2d 3e 69 4f  ndOffset = c->iO
380f4 66 66 73 65 74 3b 0a 20 20 20 20 20 20 2a 70 69  ffset;.      *pi
380f5 50 6f 73 69 74 69 6f 6e 20 3d 20 63 2d 3e 69 54  Position = c->iT
380f6 6f 6b 65 6e 2b 2b 3b 0a 0a 20 20 20 20 20 20 72  oken++;..      r
380f7 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
380f8 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
380f9 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b  urn SQLITE_DONE;
380fa 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65  .}../*.** The se
380fb 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 74 68  t of routines th
380fc 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  at implement the
380fd 20 73 69 6d 70 6c 65 20 74 6f 6b 65 6e 69 7a 65   simple tokenize
380fe 72 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  r.*/.static cons
380ff 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  t sqlite3_tokeni
38100 7a 65 72 5f 6d 6f 64 75 6c 65 20 73 69 6d 70 6c  zer_module simpl
38101 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65  eTokenizerModule
38102 20 3d 20 7b 0a 20 20 30 2c 0a 20 20 73 69 6d 70   = {.  0,.  simp
38103 6c 65 43 72 65 61 74 65 2c 0a 20 20 73 69 6d 70  leCreate,.  simp
38104 6c 65 44 65 73 74 72 6f 79 2c 0a 20 20 73 69 6d  leDestroy,.  sim
38105 70 6c 65 4f 70 65 6e 2c 0a 20 20 73 69 6d 70 6c  pleOpen,.  simpl
38106 65 43 6c 6f 73 65 2c 0a 20 20 73 69 6d 70 6c 65  eClose,.  simple
38107 4e 65 78 74 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  Next,.};../*.** 
38108 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 73  Allocate a new s
38109 69 6d 70 6c 65 20 74 6f 6b 65 6e 69 7a 65 72 2e  imple tokenizer.
3810a 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74    Return a point
3810b 65 72 20 74 6f 20 74 68 65 20 6e 65 77 0a 2a 2a  er to the new.**
3810c 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 20 2a 70   tokenizer in *p
3810d 70 4d 6f 64 75 6c 65 0a 2a 2f 0a 53 51 4c 49 54  pModule.*/.SQLIT
3810e 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
3810f 71 6c 69 74 65 33 46 74 73 33 53 69 6d 70 6c 65  qlite3Fts3Simple
38110 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28  TokenizerModule(
38111 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
38112 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73  izer_module cons
38113 74 2a 2a 70 70 4d 6f 64 75 6c 65 0a 29 7b 0a 20  t**ppModule.){. 
38114 20 2a 70 70 4d 6f 64 75 6c 65 20 3d 20 26 73 69   *ppModule = &si
38115 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64  mpleTokenizerMod
38116 75 6c 65 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f  ule;.}..#endif /
38117 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
38118 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e  E_CORE) || defin
38119 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
3811a 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  _FTS3) */../****
3811b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
3811c 66 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72  f fts3_tokenizer
3811d 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  1.c ************
3811e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3811f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
38120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
38121 20 66 69 6c 65 20 72 74 72 65 65 2e 63 20 2a 2a   file rtree.c **
38122 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38123 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38124 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
38125 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
38126 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
38127 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
38128 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
38129 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
3812a 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
3812b 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
3812c 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
3812d 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
3812e 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
3812f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
38130 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
38131 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
38132 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
38133 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
38134 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
38135 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
38136 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
38137 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
38138 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38139 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3813a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3813b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3813c 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
3813d 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 66 6f 72  ontains code for
3813e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
3813f 20 6f 66 20 74 68 65 20 72 2d 74 72 65 65 20 61   of the r-tree a
38140 6e 64 20 72 2a 2d 74 72 65 65 0a 2a 2a 20 61 6c  nd r*-tree.** al
38141 67 6f 72 69 74 68 6d 73 20 70 61 63 6b 61 67 65  gorithms package
38142 64 20 61 73 20 61 6e 20 53 51 4c 69 74 65 20 76  d as an SQLite v
38143 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64  irtual table mod
38144 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  ule..**.** $Id: 
38145 72 74 72 65 65 2e 63 2c 76 20 31 2e 31 34 20 32  rtree.c,v 1.14 2
38146 30 30 39 2f 30 38 2f 30 36 20 31 38 3a 33 36 3a  009/08/06 18:36:
38147 34 37 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45  47 danielk1977 E
38148 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 20 21 64 65  xp $.*/..#if !de
38149 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52  fined(SQLITE_COR
3814a 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  E) || defined(SQ
3814b 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 52 54 52 45  LITE_ENABLE_RTRE
3814c 45 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  E)../*.** This f
3814d 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20  ile contains an 
3814e 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
3814f 66 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 64 69  f a couple of di
38150 66 66 65 72 65 6e 74 20 76 61 72 69 61 6e 74 73  fferent variants
38151 0a 2a 2a 20 6f 66 20 74 68 65 20 72 2d 74 72 65  .** of the r-tre
38152 65 20 61 6c 67 6f 72 69 74 68 6d 2e 20 53 65 65  e algorithm. See
38153 20 74 68 65 20 52 45 41 44 4d 45 20 66 69 6c 65   the README file
38154 20 66 6f 72 20 66 75 72 74 68 65 72 20 64 65 74   for further det
38155 61 69 6c 73 2e 20 54 68 65 20 0a 2a 2a 20 73 61  ails. The .** sa
38156 6d 65 20 64 61 74 61 2d 73 74 72 75 63 74 75 72  me data-structur
38157 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 61 6c  e is used for al
38158 6c 2c 20 62 75 74 20 74 68 65 20 61 6c 67 6f 72  l, but the algor
38159 69 74 68 6d 73 20 66 6f 72 20 69 6e 73 65 72 74  ithms for insert
3815a 20 61 6e 64 0a 2a 2a 20 64 65 6c 65 74 65 20 6f   and.** delete o
3815b 70 65 72 61 74 69 6f 6e 73 20 76 61 72 79 2e 20  perations vary. 
3815c 54 68 65 20 76 61 72 69 61 6e 74 73 20 75 73 65  The variants use
3815d 64 20 61 72 65 20 73 65 6c 65 63 74 65 64 20 61  d are selected a
3815e 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 20 0a  t compile time .
3815f 2a 2a 20 62 79 20 64 65 66 69 6e 69 6e 67 20 74  ** by defining t
38160 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 79 6d  he following sym
38161 62 6f 6c 73 3a 0a 2a 2f 0a 0a 2f 2a 20 45 69 74  bols:.*/../* Eit
38162 68 65 72 2c 20 62 6f 74 68 20 6f 72 20 6e 6f 6e  her, both or non
38163 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
38164 6e 67 20 6d 61 79 20 62 65 20 73 65 74 20 74 6f  ng may be set to
38165 20 61 63 74 69 76 61 74 65 20 0a 2a 2a 20 72 2a   activate .** r*
38166 74 72 65 65 20 76 61 72 69 61 6e 74 20 61 6c 67  tree variant alg
38167 6f 72 69 74 68 6d 73 2e 0a 2a 2f 0a 23 64 65 66  orithms..*/.#def
38168 69 6e 65 20 56 41 52 49 41 4e 54 5f 52 53 54 41  ine VARIANT_RSTA
38169 52 54 52 45 45 5f 43 48 4f 4f 53 45 53 55 42 54  RTREE_CHOOSESUBT
3816a 52 45 45 20 30 0a 23 64 65 66 69 6e 65 20 56 41  REE 0.#define VA
3816b 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45 45 5f  RIANT_RSTARTREE_
3816c 52 45 49 4e 53 45 52 54 20 20 20 20 20 20 31 0a  REINSERT      1.
3816d 0a 2f 2a 20 0a 2a 2a 20 45 78 61 63 74 6c 79 20  ./* .** Exactly 
3816e 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  one of the follo
3816f 77 69 6e 67 20 6d 75 73 74 20 62 65 20 73 65 74  wing must be set
38170 20 74 6f 20 31 2e 0a 2a 2f 0a 23 64 65 66 69 6e   to 1..*/.#defin
38171 65 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41  e VARIANT_GUTTMA
38172 4e 5f 51 55 41 44 52 41 54 49 43 5f 53 50 4c 49  N_QUADRATIC_SPLI
38173 54 20 30 0a 23 64 65 66 69 6e 65 20 56 41 52 49  T 0.#define VARI
38174 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 4c 49 4e 45  ANT_GUTTMAN_LINE
38175 41 52 5f 53 50 4c 49 54 20 20 20 20 30 0a 23 64  AR_SPLIT    0.#d
38176 65 66 69 6e 65 20 56 41 52 49 41 4e 54 5f 52 53  efine VARIANT_RS
38177 54 41 52 54 52 45 45 5f 53 50 4c 49 54 20 20 20  TARTREE_SPLIT   
38178 20 20 20 20 20 20 31 0a 0a 23 64 65 66 69 6e 65        1..#define
38179 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e   VARIANT_GUTTMAN
3817a 5f 53 50 4c 49 54 20 5c 0a 20 20 20 20 20 20 20  _SPLIT \.       
3817b 20 28 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41   (VARIANT_GUTTMA
3817c 4e 5f 4c 49 4e 45 41 52 5f 53 50 4c 49 54 7c 7c  N_LINEAR_SPLIT||
3817d 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f  VARIANT_GUTTMAN_
3817e 51 55 41 44 52 41 54 49 43 5f 53 50 4c 49 54 29  QUADRATIC_SPLIT)
3817f 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 47 55  ..#if VARIANT_GU
38180 54 54 4d 41 4e 5f 51 55 41 44 52 41 54 49 43 5f  TTMAN_QUADRATIC_
38181 53 50 4c 49 54 0a 20 20 23 64 65 66 69 6e 65 20  SPLIT.  #define 
38182 50 69 63 6b 4e 65 78 74 20 51 75 61 64 72 61 74  PickNext Quadrat
38183 69 63 50 69 63 6b 4e 65 78 74 0a 20 20 23 64 65  icPickNext.  #de
38184 66 69 6e 65 20 50 69 63 6b 53 65 65 64 73 20 51  fine PickSeeds Q
38185 75 61 64 72 61 74 69 63 50 69 63 6b 53 65 65 64  uadraticPickSeed
38186 73 0a 20 20 23 64 65 66 69 6e 65 20 41 73 73 69  s.  #define Assi
38187 67 6e 43 65 6c 6c 73 20 73 70 6c 69 74 4e 6f 64  gnCells splitNod
38188 65 47 75 74 74 6d 61 6e 0a 23 65 6e 64 69 66 0a  eGuttman.#endif.
38189 23 69 66 20 56 41 52 49 41 4e 54 5f 47 55 54 54  #if VARIANT_GUTT
3818a 4d 41 4e 5f 4c 49 4e 45 41 52 5f 53 50 4c 49 54  MAN_LINEAR_SPLIT
3818b 0a 20 20 23 64 65 66 69 6e 65 20 50 69 63 6b 4e  .  #define PickN
3818c 65 78 74 20 4c 69 6e 65 61 72 50 69 63 6b 4e 65  ext LinearPickNe
3818d 78 74 0a 20 20 23 64 65 66 69 6e 65 20 50 69 63  xt.  #define Pic
3818e 6b 53 65 65 64 73 20 4c 69 6e 65 61 72 50 69 63  kSeeds LinearPic
3818f 6b 53 65 65 64 73 0a 20 20 23 64 65 66 69 6e 65  kSeeds.  #define
38190 20 41 73 73 69 67 6e 43 65 6c 6c 73 20 73 70 6c   AssignCells spl
38191 69 74 4e 6f 64 65 47 75 74 74 6d 61 6e 0a 23 65  itNodeGuttman.#e
38192 6e 64 69 66 0a 23 69 66 20 56 41 52 49 41 4e 54  ndif.#if VARIANT
38193 5f 52 53 54 41 52 54 52 45 45 5f 53 50 4c 49 54  _RSTARTREE_SPLIT
38194 0a 20 20 23 64 65 66 69 6e 65 20 41 73 73 69 67  .  #define Assig
38195 6e 43 65 6c 6c 73 20 73 70 6c 69 74 4e 6f 64 65  nCells splitNode
38196 53 74 61 72 74 72 65 65 0a 23 65 6e 64 69 66 0a  Startree.#endif.
38197 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
38198 5f 43 4f 52 45 0a 20 20 53 51 4c 49 54 45 5f 45  _CORE.  SQLITE_E
38199 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 31 0a 23  XTENSION_INIT1.#
3819a 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 0a 23 69  else.#endif...#i
3819b 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 4d 41  fndef SQLITE_AMA
3819c 4c 47 41 4d 41 54 49 4f 4e 0a 74 79 70 65 64 65  LGAMATION.typede
3819d 66 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  f sqlite3_int64 
3819e 69 36 34 3b 0a 74 79 70 65 64 65 66 20 75 6e 73  i64;.typedef uns
3819f 69 67 6e 65 64 20 63 68 61 72 20 75 38 3b 0a 74  igned char u8;.t
381a0 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64 20  ypedef unsigned 
381a1 69 6e 74 20 75 33 32 3b 0a 23 65 6e 64 69 66 0a  int u32;.#endif.
381a2 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
381a3 52 74 72 65 65 20 52 74 72 65 65 3b 0a 74 79 70  Rtree Rtree;.typ
381a4 65 64 65 66 20 73 74 72 75 63 74 20 52 74 72 65  edef struct Rtre
381a5 65 43 75 72 73 6f 72 20 52 74 72 65 65 43 75 72  eCursor RtreeCur
381a6 73 6f 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72  sor;.typedef str
381a7 75 63 74 20 52 74 72 65 65 4e 6f 64 65 20 52 74  uct RtreeNode Rt
381a8 72 65 65 4e 6f 64 65 3b 0a 74 79 70 65 64 65 66  reeNode;.typedef
381a9 20 73 74 72 75 63 74 20 52 74 72 65 65 43 65 6c   struct RtreeCel
381aa 6c 20 52 74 72 65 65 43 65 6c 6c 3b 0a 74 79 70  l RtreeCell;.typ
381ab 65 64 65 66 20 73 74 72 75 63 74 20 52 74 72 65  edef struct Rtre
381ac 65 43 6f 6e 73 74 72 61 69 6e 74 20 52 74 72 65  eConstraint Rtre
381ad 65 43 6f 6e 73 74 72 61 69 6e 74 3b 0a 74 79 70  eConstraint;.typ
381ae 65 64 65 66 20 75 6e 69 6f 6e 20 52 74 72 65 65  edef union Rtree
381af 43 6f 6f 72 64 20 52 74 72 65 65 43 6f 6f 72 64  Coord RtreeCoord
381b0 3b 0a 0a 2f 2a 20 54 68 65 20 72 74 72 65 65 20  ;../* The rtree 
381b1 6d 61 79 20 68 61 76 65 20 62 65 74 77 65 65 6e  may have between
381b2 20 31 20 61 6e 64 20 52 54 52 45 45 5f 4d 41 58   1 and RTREE_MAX
381b3 5f 44 49 4d 45 4e 53 49 4f 4e 53 20 64 69 6d 65  _DIMENSIONS dime
381b4 6e 73 69 6f 6e 73 2e 20 2a 2f 0a 23 64 65 66 69  nsions. */.#defi
381b5 6e 65 20 52 54 52 45 45 5f 4d 41 58 5f 44 49 4d  ne RTREE_MAX_DIM
381b6 45 4e 53 49 4f 4e 53 20 35 0a 0a 2f 2a 20 53 69  ENSIONS 5../* Si
381b7 7a 65 20 6f 66 20 68 61 73 68 20 74 61 62 6c 65  ze of hash table
381b8 20 52 74 72 65 65 2e 61 48 61 73 68 2e 20 54 68   Rtree.aHash. Th
381b9 69 73 20 68 61 73 68 20 74 61 62 6c 65 20 69 73  is hash table is
381ba 20 6e 6f 74 20 65 78 70 65 63 74 65 64 20 74 6f   not expected to
381bb 0a 2a 2a 20 65 76 65 72 20 63 6f 6e 74 61 69 6e  .** ever contain
381bc 20 76 65 72 79 20 6d 61 6e 79 20 65 6e 74 72 69   very many entri
381bd 65 73 2c 20 73 6f 20 61 20 66 69 78 65 64 20 6e  es, so a fixed n
381be 75 6d 62 65 72 20 6f 66 20 62 75 63 6b 65 74 73  umber of buckets
381bf 20 69 73 20 0a 2a 2a 20 75 73 65 64 2e 0a 2a 2f   is .** used..*/
381c0 0a 23 64 65 66 69 6e 65 20 48 41 53 48 53 49 5a  .#define HASHSIZ
381c1 45 20 31 32 38 0a 0a 2f 2a 20 0a 2a 2a 20 41 6e  E 128../* .** An
381c2 20 72 74 72 65 65 20 76 69 72 74 75 61 6c 2d 74   rtree virtual-t
381c3 61 62 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a  able object..*/.
381c4 73 74 72 75 63 74 20 52 74 72 65 65 20 7b 0a 20  struct Rtree {. 
381c5 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 62 61   sqlite3_vtab ba
381c6 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  se;.  sqlite3 *d
381c7 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
381c8 20 20 2f 2a 20 48 6f 73 74 20 64 61 74 61 62 61    /* Host databa
381c9 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
381ca 0a 20 20 69 6e 74 20 69 4e 6f 64 65 53 69 7a 65  .  int iNodeSize
381cb 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
381cc 2a 20 53 69 7a 65 20 69 6e 20 62 79 74 65 73 20  * Size in bytes 
381cd 6f 66 20 65 61 63 68 20 6e 6f 64 65 20 69 6e 20  of each node in 
381ce 74 68 65 20 6e 6f 64 65 20 74 61 62 6c 65 20 2a  the node table *
381cf 2f 0a 20 20 69 6e 74 20 6e 44 69 6d 3b 20 20 20  /.  int nDim;   
381d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
381d1 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 69 6d  /* Number of dim
381d2 65 6e 73 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74  ensions */.  int
381d3 20 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 3b 20   nBytesPerCell; 
381d4 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65           /* Byte
381d5 73 20 63 6f 6e 73 75 6d 65 64 20 70 65 72 20 63  s consumed per c
381d6 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 69 44 65  ell */.  int iDe
381d7 70 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 20  pth;            
381d8 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
381d9 64 65 70 74 68 20 6f 66 20 74 68 65 20 72 2d 74  depth of the r-t
381da 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f  ree structure */
381db 0a 20 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20  .  char *zDb;   
381dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
381dd 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61  * Name of databa
381de 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 72 2d  se containing r-
381df 74 72 65 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20  tree table */.  
381e0 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20  char *zName;    
381e1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
381e2 61 6d 65 20 6f 66 20 72 2d 74 72 65 65 20 74 61  ame of r-tree ta
381e3 62 6c 65 20 2a 2f 20 0a 20 20 52 74 72 65 65 4e  ble */ .  RtreeN
381e4 6f 64 65 20 2a 61 48 61 73 68 5b 48 41 53 48 53  ode *aHash[HASHS
381e5 49 5a 45 5d 3b 20 2f 2a 20 48 61 73 68 20 74 61  IZE]; /* Hash ta
381e6 62 6c 65 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79  ble of in-memory
381e7 20 6e 6f 64 65 73 2e 20 2a 2f 20 0a 20 20 69 6e   nodes. */ .  in
381e8 74 20 6e 42 75 73 79 3b 20 20 20 20 20 20 20 20  t nBusy;        
381e9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
381ea 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 75  rent number of u
381eb 73 65 72 73 20 6f 66 20 74 68 69 73 20 73 74 72  sers of this str
381ec 75 63 74 75 72 65 20 2a 2f 0a 0a 20 20 2f 2a 20  ucture */..  /* 
381ed 4c 69 73 74 20 6f 66 20 6e 6f 64 65 73 20 72 65  List of nodes re
381ee 6d 6f 76 65 64 20 64 75 72 69 6e 67 20 61 20 43  moved during a C
381ef 6f 6e 64 65 6e 73 65 54 72 65 65 20 6f 70 65 72  ondenseTree oper
381f0 61 74 69 6f 6e 2e 20 4c 69 73 74 20 69 73 0a 20  ation. List is. 
381f1 20 2a 2a 20 6c 69 6e 6b 65 64 20 74 6f 67 65 74   ** linked toget
381f2 68 65 72 20 76 69 61 20 74 68 65 20 70 6f 69 6e  her via the poin
381f3 74 65 72 20 6e 6f 72 6d 61 6c 6c 79 20 75 73 65  ter normally use
381f4 64 20 66 6f 72 20 68 61 73 68 20 63 68 61 69 6e  d for hash chain
381f5 73 20 2d 0a 20 20 2a 2a 20 52 74 72 65 65 4e 6f  s -.  ** RtreeNo
381f6 64 65 2e 70 4e 65 78 74 2e 20 52 74 72 65 65 4e  de.pNext. RtreeN
381f7 6f 64 65 2e 69 4e 6f 64 65 20 73 74 6f 72 65 73  ode.iNode stores
381f8 20 74 68 65 20 64 65 70 74 68 20 6f 66 20 74 68   the depth of th
381f9 65 20 73 75 62 2d 74 72 65 65 20 0a 20 20 2a 2a  e sub-tree .  **
381fa 20 68 65 61 64 65 64 20 62 79 20 74 68 65 20 6e   headed by the n
381fb 6f 64 65 20 28 6c 65 61 66 20 6e 6f 64 65 73 20  ode (leaf nodes 
381fc 68 61 76 65 20 52 74 72 65 65 4e 6f 64 65 2e 69  have RtreeNode.i
381fd 4e 6f 64 65 3d 3d 30 29 2e 0a 20 20 2a 2f 0a 20  Node==0)..  */. 
381fe 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 44 65 6c   RtreeNode *pDel
381ff 65 74 65 64 3b 0a 20 20 69 6e 74 20 69 52 65 69  eted;.  int iRei
38200 6e 73 65 72 74 48 65 69 67 68 74 3b 20 20 20 20  nsertHeight;    
38201 20 20 20 20 2f 2a 20 48 65 69 67 68 74 20 6f 66      /* Height of
38202 20 73 75 62 2d 74 72 65 65 73 20 52 65 69 6e 73   sub-trees Reins
38203 65 72 74 28 29 20 68 61 73 20 72 75 6e 20 6f 6e  ert() has run on
38204 20 2a 2f 0a 0a 20 20 2f 2a 20 53 74 61 74 65 6d   */..  /* Statem
38205 65 6e 74 73 20 74 6f 20 72 65 61 64 2f 77 72 69  ents to read/wri
38206 74 65 2f 64 65 6c 65 74 65 20 61 20 72 65 63 6f  te/delete a reco
38207 72 64 20 66 72 6f 6d 20 78 78 78 5f 6e 6f 64 65  rd from xxx_node
38208 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74   */.  sqlite3_st
38209 6d 74 20 2a 70 52 65 61 64 4e 6f 64 65 3b 0a 20  mt *pReadNode;. 
3820a 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
3820b 57 72 69 74 65 4e 6f 64 65 3b 0a 20 20 73 71 6c  WriteNode;.  sql
3820c 69 74 65 33 5f 73 74 6d 74 20 2a 70 44 65 6c 65  ite3_stmt *pDele
3820d 74 65 4e 6f 64 65 3b 0a 0a 20 20 2f 2a 20 53 74  teNode;..  /* St
3820e 61 74 65 6d 65 6e 74 73 20 74 6f 20 72 65 61 64  atements to read
3820f 2f 77 72 69 74 65 2f 64 65 6c 65 74 65 20 61 20  /write/delete a 
38210 72 65 63 6f 72 64 20 66 72 6f 6d 20 78 78 78 5f  record from xxx_
38211 72 6f 77 69 64 20 2a 2f 0a 20 20 73 71 6c 69 74  rowid */.  sqlit
38212 65 33 5f 73 74 6d 74 20 2a 70 52 65 61 64 52 6f  e3_stmt *pReadRo
38213 77 69 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73  wid;.  sqlite3_s
38214 74 6d 74 20 2a 70 57 72 69 74 65 52 6f 77 69 64  tmt *pWriteRowid
38215 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  ;.  sqlite3_stmt
38216 20 2a 70 44 65 6c 65 74 65 52 6f 77 69 64 3b 0a   *pDeleteRowid;.
38217 0a 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 73  .  /* Statements
38218 20 74 6f 20 72 65 61 64 2f 77 72 69 74 65 2f 64   to read/write/d
38219 65 6c 65 74 65 20 61 20 72 65 63 6f 72 64 20 66  elete a record f
3821a 72 6f 6d 20 78 78 78 5f 70 61 72 65 6e 74 20 2a  rom xxx_parent *
3821b 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  /.  sqlite3_stmt
3821c 20 2a 70 52 65 61 64 50 61 72 65 6e 74 3b 0a 20   *pReadParent;. 
3821d 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
3821e 57 72 69 74 65 50 61 72 65 6e 74 3b 0a 20 20 73  WriteParent;.  s
3821f 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 44 65  qlite3_stmt *pDe
38220 6c 65 74 65 50 61 72 65 6e 74 3b 0a 0a 20 20 69  leteParent;..  i
38221 6e 74 20 65 43 6f 6f 72 64 54 79 70 65 3b 0a 7d  nt eCoordType;.}
38222 3b 0a 0a 2f 2a 20 50 6f 73 73 69 62 6c 65 20 76  ;../* Possible v
38223 61 6c 75 65 73 20 66 6f 72 20 65 43 6f 6f 72 64  alues for eCoord
38224 54 79 70 65 3a 20 2a 2f 0a 23 64 65 66 69 6e 65  Type: */.#define
38225 20 52 54 52 45 45 5f 43 4f 4f 52 44 5f 52 45 41   RTREE_COORD_REA
38226 4c 33 32 20 30 0a 23 64 65 66 69 6e 65 20 52 54  L32 0.#define RT
38227 52 45 45 5f 43 4f 4f 52 44 5f 49 4e 54 33 32 20  REE_COORD_INT32 
38228 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 69   1../*.** The mi
38229 6e 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20  nimum number of 
3822a 63 65 6c 6c 73 20 61 6c 6c 6f 77 65 64 20 66 6f  cells allowed fo
3822b 72 20 61 20 6e 6f 64 65 20 69 73 20 61 20 74 68  r a node is a th
3822c 69 72 64 20 6f 66 20 74 68 65 20 0a 2a 2a 20 6d  ird of the .** m
3822d 61 78 69 6d 75 6d 2e 20 49 6e 20 47 75 74 6d 61  aximum. In Gutma
3822e 6e 27 73 20 6e 6f 74 61 74 69 6f 6e 3a 0a 2a 2a  n's notation:.**
3822f 0a 2a 2a 20 20 20 20 20 6d 20 3d 20 4d 2f 33 0a  .**     m = M/3.
38230 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 52 2a 2d 74  **.** If an R*-t
38231 72 65 65 20 22 52 65 69 6e 73 65 72 74 22 20 6f  ree "Reinsert" o
38232 70 65 72 61 74 69 6f 6e 20 69 73 20 72 65 71 75  peration is requ
38233 69 72 65 64 2c 20 74 68 65 20 73 61 6d 65 20 6e  ired, the same n
38234 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 65 6c 6c  umber of.** cell
38235 73 20 61 72 65 20 72 65 6d 6f 76 65 64 20 66 72  s are removed fr
38236 6f 6d 20 74 68 65 20 6f 76 65 72 66 75 6c 6c 20  om the overfull 
38237 6e 6f 64 65 20 61 6e 64 20 72 65 69 6e 73 65 72  node and reinser
38238 74 65 64 20 69 6e 74 6f 20 74 68 65 20 74 72 65  ted into the tre
38239 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 54  e..*/.#define RT
3823a 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70 29 20  REE_MINCELLS(p) 
3823b 28 28 28 28 70 29 2d 3e 69 4e 6f 64 65 53 69 7a  ((((p)->iNodeSiz
3823c 65 2d 34 29 2f 28 70 29 2d 3e 6e 42 79 74 65 73  e-4)/(p)->nBytes
3823d 50 65 72 43 65 6c 6c 29 2f 33 29 0a 23 64 65 66  PerCell)/3).#def
3823e 69 6e 65 20 52 54 52 45 45 5f 52 45 49 4e 53 45  ine RTREE_REINSE
3823f 52 54 28 70 29 20 52 54 52 45 45 5f 4d 49 4e 43  RT(p) RTREE_MINC
38240 45 4c 4c 53 28 70 29 0a 23 64 65 66 69 6e 65 20  ELLS(p).#define 
38241 52 54 52 45 45 5f 4d 41 58 43 45 4c 4c 53 20 35  RTREE_MAXCELLS 5
38242 31 0a 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 72 74 72  1../* .** An rtr
38243 65 65 20 63 75 72 73 6f 72 20 6f 62 6a 65 63 74  ee cursor object
38244 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 74 72 65  ..*/.struct Rtre
38245 65 43 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c 69  eCursor {.  sqli
38246 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
38247 62 61 73 65 3b 0a 20 20 52 74 72 65 65 4e 6f 64  base;.  RtreeNod
38248 65 20 2a 70 4e 6f 64 65 3b 20 20 20 20 20 20 20  e *pNode;       
38249 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 64            /* Nod
3824a 65 20 63 75 72 73 6f 72 20 69 73 20 63 75 72 72  e cursor is curr
3824b 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 61  ently pointing a
3824c 74 20 2a 2f 0a 20 20 69 6e 74 20 69 43 65 6c 6c  t */.  int iCell
3824d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
3824e 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
3824f 78 20 6f 66 20 63 75 72 72 65 6e 74 20 63 65 6c  x of current cel
38250 6c 20 69 6e 20 70 4e 6f 64 65 20 2a 2f 0a 20 20  l in pNode */.  
38251 69 6e 74 20 69 53 74 72 61 74 65 67 79 3b 20 20  int iStrategy;  
38252 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38253 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 69 64 78    /* Copy of idx
38254 4e 75 6d 20 73 65 61 72 63 68 20 70 61 72 61 6d  Num search param
38255 65 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  eter */.  int nC
38256 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20 20  onstraint;      
38257 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
38258 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
38259 20 69 6e 20 61 43 6f 6e 73 74 72 61 69 6e 74 20   in aConstraint 
3825a 2a 2f 0a 20 20 52 74 72 65 65 43 6f 6e 73 74 72  */.  RtreeConstr
3825b 61 69 6e 74 20 2a 61 43 6f 6e 73 74 72 61 69 6e  aint *aConstrain
3825c 74 3b 20 20 20 20 20 2f 2a 20 53 65 61 72 63 68  t;     /* Search
3825d 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 2a 2f   constraints. */
3825e 0a 7d 3b 0a 0a 75 6e 69 6f 6e 20 52 74 72 65 65  .};..union Rtree
3825f 43 6f 6f 72 64 20 7b 0a 20 20 66 6c 6f 61 74 20  Coord {.  float 
38260 66 3b 0a 20 20 69 6e 74 20 69 3b 0a 7d 3b 0a 0a  f;.  int i;.};..
38261 2f 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65  /*.** The argume
38262 6e 74 20 69 73 20 61 6e 20 52 74 72 65 65 43 6f  nt is an RtreeCo
38263 6f 72 64 2e 20 52 65 74 75 72 6e 20 74 68 65 20  ord. Return the 
38264 76 61 6c 75 65 20 73 74 6f 72 65 64 20 77 69 74  value stored wit
38265 68 69 6e 20 74 68 65 20 52 74 72 65 65 43 6f 6f  hin the RtreeCoo
38266 72 64 0a 2a 2a 20 66 6f 72 6d 61 74 74 65 64 20  rd.** formatted 
38267 61 73 20 61 20 64 6f 75 62 6c 65 2e 20 54 68 69  as a double. Thi
38268 73 20 6d 61 63 72 6f 20 61 73 73 75 6d 65 73 20  s macro assumes 
38269 74 68 61 74 20 6c 6f 63 61 6c 20 76 61 72 69 61  that local varia
3826a 62 6c 65 20 70 52 74 72 65 65 20 70 6f 69 6e 74  ble pRtree point
3826b 73 0a 2a 2a 20 74 6f 20 74 68 65 20 52 74 72 65  s.** to the Rtre
3826c 65 20 73 74 72 75 63 74 75 72 65 20 61 73 73 6f  e structure asso
3826d 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
3826e 52 74 72 65 65 43 6f 6f 72 64 2e 0a 2a 2f 0a 23  RtreeCoord..*/.#
3826f 64 65 66 69 6e 65 20 44 43 4f 4f 52 44 28 63 6f  define DCOORD(co
38270 6f 72 64 29 20 28 20 20 20 20 20 20 20 20 20 20  ord) (          
38271 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38272 20 5c 0a 20 20 28 70 52 74 72 65 65 2d 3e 65 43   \.  (pRtree->eC
38273 6f 6f 72 64 54 79 70 65 3d 3d 52 54 52 45 45 5f  oordType==RTREE_
38274 43 4f 4f 52 44 5f 52 45 41 4c 33 32 29 20 3f 20  COORD_REAL32) ? 
38275 20 20 20 20 20 5c 0a 20 20 20 20 28 28 64 6f 75       \.    ((dou
38276 62 6c 65 29 63 6f 6f 72 64 2e 66 29 20 3a 20 20  ble)coord.f) :  
38277 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38278 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 28           \.    (
38279 28 64 6f 75 62 6c 65 29 63 6f 6f 72 64 2e 69 29  (double)coord.i)
3827a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3827b 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 29               \.)
3827c 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 65 61 72 63 68  ../*.** A search
3827d 20 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 2a 2f 0a   constraint..*/.
3827e 73 74 72 75 63 74 20 52 74 72 65 65 43 6f 6e 73  struct RtreeCons
3827f 74 72 61 69 6e 74 20 7b 0a 20 20 69 6e 74 20 69  traint {.  int i
38280 43 6f 6f 72 64 3b 20 20 20 20 20 20 20 20 20 20  Coord;          
38281 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
38282 49 6e 64 65 78 20 6f 66 20 63 6f 6e 73 74 72 61  Index of constra
38283 69 6e 65 64 20 63 6f 6f 72 64 69 6e 61 74 65 20  ined coordinate 
38284 2a 2f 0a 20 20 69 6e 74 20 6f 70 3b 20 20 20 20  */.  int op;    
38285 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38286 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 73 74 72         /* Constr
38287 61 69 6e 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e  aining operation
38288 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 56 61   */.  double rVa
38289 6c 75 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  lue;            
3828a 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 73 74          /* Const
3828b 72 61 69 6e 74 20 76 61 6c 75 65 2e 20 2a 2f 0a  raint value. */.
3828c 7d 3b 0a 0a 2f 2a 20 50 6f 73 73 69 62 6c 65 20  };../* Possible 
3828d 76 61 6c 75 65 73 20 66 6f 72 20 52 74 72 65 65  values for Rtree
3828e 43 6f 6e 73 74 72 61 69 6e 74 2e 6f 70 20 2a 2f  Constraint.op */
3828f 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45 5f 45  .#define RTREE_E
38290 51 20 30 78 34 31 0a 23 64 65 66 69 6e 65 20 52  Q 0x41.#define R
38291 54 52 45 45 5f 4c 45 20 30 78 34 32 0a 23 64 65  TREE_LE 0x42.#de
38292 66 69 6e 65 20 52 54 52 45 45 5f 4c 54 20 30 78  fine RTREE_LT 0x
38293 34 33 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45  43.#define RTREE
38294 5f 47 45 20 30 78 34 34 0a 23 64 65 66 69 6e 65  _GE 0x44.#define
38295 20 52 54 52 45 45 5f 47 54 20 30 78 34 35 0a 0a   RTREE_GT 0x45..
38296 2f 2a 20 0a 2a 2a 20 41 6e 20 72 74 72 65 65 20  /* .** An rtree 
38297 73 74 72 75 63 74 75 72 65 20 6e 6f 64 65 2e 0a  structure node..
38298 2a 2a 0a 2a 2a 20 44 61 74 61 20 66 6f 72 6d 61  **.** Data forma
38299 74 20 28 52 74 72 65 65 4e 6f 64 65 2e 7a 44 61  t (RtreeNode.zDa
3829a 74 61 29 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20  ta):.**.**   1. 
3829b 49 66 20 74 68 65 20 6e 6f 64 65 20 69 73 20 74  If the node is t
3829c 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 28 6e 6f  he root node (no
3829d 64 65 20 31 29 2c 20 74 68 65 6e 20 74 68 65 20  de 1), then the 
3829e 66 69 72 73 74 20 32 20 62 79 74 65 73 0a 2a 2a  first 2 bytes.**
3829f 20 20 20 20 20 20 6f 66 20 74 68 65 20 6e 6f 64        of the nod
382a0 65 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 74 72  e contain the tr
382a1 65 65 20 64 65 70 74 68 20 61 73 20 61 20 62 69  ee depth as a bi
382a2 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72  g-endian integer
382a3 2e 0a 2a 2a 20 20 20 20 20 20 46 6f 72 20 6e 6f  ..**      For no
382a4 6e 2d 72 6f 6f 74 20 6e 6f 64 65 73 2c 20 74 68  n-root nodes, th
382a5 65 20 66 69 72 73 74 20 32 20 62 79 74 65 73 20  e first 2 bytes 
382a6 61 72 65 20 6c 65 66 74 20 75 6e 75 73 65 64 2e  are left unused.
382a7 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20 54 68 65 20  .**.**   2. The 
382a8 6e 65 78 74 20 32 20 62 79 74 65 73 20 63 6f 6e  next 2 bytes con
382a9 74 61 69 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  tain the number 
382aa 6f 66 20 65 6e 74 72 69 65 73 20 63 75 72 72 65  of entries curre
382ab 6e 74 6c 79 20 0a 2a 2a 20 20 20 20 20 20 73 74  ntly .**      st
382ac 6f 72 65 64 20 69 6e 20 74 68 65 20 6e 6f 64 65  ored in the node
382ad 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 2e 20 54 68 65  ..**.**   3. The
382ae 20 72 65 6d 61 69 6e 64 65 72 20 6f 66 20 74 68   remainder of th
382af 65 20 6e 6f 64 65 20 63 6f 6e 74 61 69 6e 73 20  e node contains 
382b0 74 68 65 20 6e 6f 64 65 20 65 6e 74 72 69 65 73  the node entries
382b1 2e 20 45 61 63 68 20 65 6e 74 72 79 0a 2a 2a 20  . Each entry.** 
382b2 20 20 20 20 20 63 6f 6e 73 69 73 74 73 20 6f 66       consists of
382b3 20 61 20 73 69 6e 67 6c 65 20 38 2d 62 79 74 65   a single 8-byte
382b4 20 69 6e 74 65 67 65 72 20 66 6f 6c 6c 6f 77 65   integer followe
382b5 64 20 62 79 20 61 6e 20 65 76 65 6e 20 6e 75 6d  d by an even num
382b6 62 65 72 0a 2a 2a 20 20 20 20 20 20 6f 66 20 34  ber.**      of 4
382b7 2d 62 79 74 65 20 63 6f 6f 72 64 69 6e 61 74 65  -byte coordinate
382b8 73 2e 20 46 6f 72 20 6c 65 61 66 20 6e 6f 64 65  s. For leaf node
382b9 73 20 74 68 65 20 69 6e 74 65 67 65 72 20 69 73  s the integer is
382ba 20 74 68 65 20 72 6f 77 69 64 0a 2a 2a 20 20 20   the rowid.**   
382bb 20 20 20 6f 66 20 61 20 72 65 63 6f 72 64 2e 20     of a record. 
382bc 46 6f 72 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64  For internal nod
382bd 65 73 20 69 74 20 69 73 20 74 68 65 20 6e 6f 64  es it is the nod
382be 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 0a 2a 2a  e number of a.**
382bf 20 20 20 20 20 20 63 68 69 6c 64 20 70 61 67 65        child page
382c0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 74 72 65  ..*/.struct Rtre
382c1 65 4e 6f 64 65 20 7b 0a 20 20 52 74 72 65 65 4e  eNode {.  RtreeN
382c2 6f 64 65 20 2a 70 50 61 72 65 6e 74 3b 20 20 20  ode *pParent;   
382c3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
382c4 61 72 65 6e 74 20 6e 6f 64 65 20 2a 2f 0a 20 20  arent node */.  
382c5 69 36 34 20 69 4e 6f 64 65 3b 0a 20 20 69 6e 74  i64 iNode;.  int
382c6 20 6e 52 65 66 3b 0a 20 20 69 6e 74 20 69 73 44   nRef;.  int isD
382c7 69 72 74 79 3b 0a 20 20 75 38 20 2a 7a 44 61 74  irty;.  u8 *zDat
382c8 61 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  a;.  RtreeNode *
382c9 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20  pNext;          
382ca 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 6e         /* Next n
382cb 6f 64 65 20 69 6e 20 74 68 69 73 20 68 61 73 68  ode in this hash
382cc 20 63 68 61 69 6e 20 2a 2f 0a 7d 3b 0a 23 64 65   chain */.};.#de
382cd 66 69 6e 65 20 4e 43 45 4c 4c 28 70 4e 6f 64 65  fine NCELL(pNode
382ce 29 20 72 65 61 64 49 6e 74 31 36 28 26 28 70 4e  ) readInt16(&(pN
382cf 6f 64 65 29 2d 3e 7a 44 61 74 61 5b 32 5d 29 0a  ode)->zData[2]).
382d0 0a 2f 2a 20 0a 2a 2a 20 53 74 72 75 63 74 75 72  ./* .** Structur
382d1 65 20 74 6f 20 73 74 6f 72 65 20 61 20 64 65 73  e to store a des
382d2 65 72 69 61 6c 69 7a 65 64 20 72 74 72 65 65 20  erialized rtree 
382d3 72 65 63 6f 72 64 2e 0a 2a 2f 0a 73 74 72 75 63  record..*/.struc
382d4 74 20 52 74 72 65 65 43 65 6c 6c 20 7b 0a 20 20  t RtreeCell {.  
382d5 69 36 34 20 69 52 6f 77 69 64 3b 0a 20 20 52 74  i64 iRowid;.  Rt
382d6 72 65 65 43 6f 6f 72 64 20 61 43 6f 6f 72 64 5b  reeCoord aCoord[
382d7 52 54 52 45 45 5f 4d 41 58 5f 44 49 4d 45 4e 53  RTREE_MAX_DIMENS
382d8 49 4f 4e 53 2a 32 5d 3b 0a 7d 3b 0a 0a 23 69 66  IONS*2];.};..#if
382d9 6e 64 65 66 20 4d 41 58 0a 23 20 64 65 66 69 6e  ndef MAX.# defin
382da 65 20 4d 41 58 28 78 2c 79 29 20 28 28 78 29 20  e MAX(x,y) ((x) 
382db 3c 20 28 79 29 20 3f 20 28 79 29 20 3a 20 28 78  < (y) ? (y) : (x
382dc 29 29 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65  )).#endif.#ifnde
382dd 66 20 4d 49 4e 0a 23 20 64 65 66 69 6e 65 20 4d  f MIN.# define M
382de 49 4e 28 78 2c 79 29 20 28 28 78 29 20 3e 20 28  IN(x,y) ((x) > (
382df 79 29 20 3f 20 28 79 29 20 3a 20 28 78 29 29 0a  y) ? (y) : (x)).
382e0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 75  #endif../*.** Fu
382e1 6e 63 74 69 6f 6e 73 20 74 6f 20 64 65 73 65 72  nctions to deser
382e2 69 61 6c 69 7a 65 20 61 20 31 36 20 62 69 74 20  ialize a 16 bit 
382e3 69 6e 74 65 67 65 72 2c 20 33 32 20 62 69 74 20  integer, 32 bit 
382e4 72 65 61 6c 20 6e 75 6d 62 65 72 20 61 6e 64 0a  real number and.
382e5 2a 2a 20 36 34 20 62 69 74 20 69 6e 74 65 67 65  ** 64 bit intege
382e6 72 2e 20 54 68 65 20 64 65 73 65 72 69 61 6c 69  r. The deseriali
382e7 7a 65 64 20 76 61 6c 75 65 20 69 73 20 72 65 74  zed value is ret
382e8 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  urned..*/.static
382e9 20 69 6e 74 20 72 65 61 64 49 6e 74 31 36 28 75   int readInt16(u
382ea 38 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20  8 *p){.  return 
382eb 28 70 5b 30 5d 3c 3c 38 29 20 2b 20 70 5b 31 5d  (p[0]<<8) + p[1]
382ec 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  ;.}.static void 
382ed 72 65 61 64 43 6f 6f 72 64 28 75 38 20 2a 70 2c  readCoord(u8 *p,
382ee 20 52 74 72 65 65 43 6f 6f 72 64 20 2a 70 43 6f   RtreeCoord *pCo
382ef 6f 72 64 29 7b 0a 20 20 75 33 32 20 69 20 3d 20  ord){.  u32 i = 
382f0 28 0a 20 20 20 20 28 28 28 75 33 32 29 70 5b 30  (.    (((u32)p[0
382f1 5d 29 20 3c 3c 20 32 34 29 20 2b 20 0a 20 20 20  ]) << 24) + .   
382f2 20 28 28 28 75 33 32 29 70 5b 31 5d 29 20 3c 3c   (((u32)p[1]) <<
382f3 20 31 36 29 20 2b 20 0a 20 20 20 20 28 28 28 75   16) + .    (((u
382f4 33 32 29 70 5b 32 5d 29 20 3c 3c 20 20 38 29 20  32)p[2]) <<  8) 
382f5 2b 20 0a 20 20 20 20 28 28 28 75 33 32 29 70 5b  + .    (((u32)p[
382f6 33 5d 29 20 3c 3c 20 20 30 29 0a 20 20 29 3b 0a  3]) <<  0).  );.
382f7 20 20 2a 28 75 33 32 20 2a 29 70 43 6f 6f 72 64    *(u32 *)pCoord
382f8 20 3d 20 69 3b 0a 7d 0a 73 74 61 74 69 63 20 69   = i;.}.static i
382f9 36 34 20 72 65 61 64 49 6e 74 36 34 28 75 38 20  64 readInt64(u8 
382fa 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 0a  *p){.  return (.
382fb 20 20 20 20 28 28 28 69 36 34 29 70 5b 30 5d 29      (((i64)p[0])
382fc 20 3c 3c 20 35 36 29 20 2b 20 0a 20 20 20 20 28   << 56) + .    (
382fd 28 28 69 36 34 29 70 5b 31 5d 29 20 3c 3c 20 34  ((i64)p[1]) << 4
382fe 38 29 20 2b 20 0a 20 20 20 20 28 28 28 69 36 34  8) + .    (((i64
382ff 29 70 5b 32 5d 29 20 3c 3c 20 34 30 29 20 2b 20  )p[2]) << 40) + 
38300 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b 33 5d  .    (((i64)p[3]
38301 29 20 3c 3c 20 33 32 29 20 2b 20 0a 20 20 20 20  ) << 32) + .    
38302 28 28 28 69 36 34 29 70 5b 34 5d 29 20 3c 3c 20  (((i64)p[4]) << 
38303 32 34 29 20 2b 20 0a 20 20 20 20 28 28 28 69 36  24) + .    (((i6
38304 34 29 70 5b 35 5d 29 20 3c 3c 20 31 36 29 20 2b  4)p[5]) << 16) +
38305 20 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b 36   .    (((i64)p[6
38306 5d 29 20 3c 3c 20 20 38 29 20 2b 20 0a 20 20 20  ]) <<  8) + .   
38307 20 28 28 28 69 36 34 29 70 5b 37 5d 29 20 3c 3c   (((i64)p[7]) <<
38308 20 20 30 29 0a 20 20 29 3b 0a 7d 0a 0a 2f 2a 0a    0).  );.}../*.
38309 2a 2a 20 46 75 6e 63 74 69 6f 6e 73 20 74 6f 20  ** Functions to 
3830a 73 65 72 69 61 6c 69 7a 65 20 61 20 31 36 20 62  serialize a 16 b
3830b 69 74 20 69 6e 74 65 67 65 72 2c 20 33 32 20 62  it integer, 32 b
3830c 69 74 20 72 65 61 6c 20 6e 75 6d 62 65 72 20 61  it real number a
3830d 6e 64 0a 2a 2a 20 36 34 20 62 69 74 20 69 6e 74  nd.** 64 bit int
3830e 65 67 65 72 2e 20 54 68 65 20 76 61 6c 75 65 20  eger. The value 
3830f 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 65 20  returned is the 
38310 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
38311 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 74 68  written.** to th
38312 65 20 61 72 67 75 6d 65 6e 74 20 62 75 66 66 65  e argument buffe
38313 72 20 28 61 6c 77 61 79 73 20 32 2c 20 34 20 61  r (always 2, 4 a
38314 6e 64 20 38 20 72 65 73 70 65 63 74 69 76 65 6c  nd 8 respectivel
38315 79 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  y)..*/.static in
38316 74 20 77 72 69 74 65 49 6e 74 31 36 28 75 38 20  t writeInt16(u8 
38317 2a 70 2c 20 69 6e 74 20 69 29 7b 0a 20 20 70 5b  *p, int i){.  p[
38318 30 5d 20 3d 20 28 69 3e 3e 20 38 29 26 30 78 46  0] = (i>> 8)&0xF
38319 46 3b 0a 20 20 70 5b 31 5d 20 3d 20 28 69 3e 3e  F;.  p[1] = (i>>
3831a 20 30 29 26 30 78 46 46 3b 0a 20 20 72 65 74 75   0)&0xFF;.  retu
3831b 72 6e 20 32 3b 0a 7d 0a 73 74 61 74 69 63 20 69  rn 2;.}.static i
3831c 6e 74 20 77 72 69 74 65 43 6f 6f 72 64 28 75 38  nt writeCoord(u8
3831d 20 2a 70 2c 20 52 74 72 65 65 43 6f 6f 72 64 20   *p, RtreeCoord 
3831e 2a 70 43 6f 6f 72 64 29 7b 0a 20 20 75 33 32 20  *pCoord){.  u32 
3831f 69 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a  i;.  assert( siz
38320 65 6f 66 28 52 74 72 65 65 43 6f 6f 72 64 29 3d  eof(RtreeCoord)=
38321 3d 34 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =4 );.  assert( 
38322 73 69 7a 65 6f 66 28 75 33 32 29 3d 3d 34 20 29  sizeof(u32)==4 )
38323 3b 0a 20 20 69 20 3d 20 2a 28 75 33 32 20 2a 29  ;.  i = *(u32 *)
38324 70 43 6f 6f 72 64 3b 0a 20 20 70 5b 30 5d 20 3d  pCoord;.  p[0] =
38325 20 28 69 3e 3e 32 34 29 26 30 78 46 46 3b 0a 20   (i>>24)&0xFF;. 
38326 20 70 5b 31 5d 20 3d 20 28 69 3e 3e 31 36 29 26   p[1] = (i>>16)&
38327 30 78 46 46 3b 0a 20 20 70 5b 32 5d 20 3d 20 28  0xFF;.  p[2] = (
38328 69 3e 3e 20 38 29 26 30 78 46 46 3b 0a 20 20 70  i>> 8)&0xFF;.  p
38329 5b 33 5d 20 3d 20 28 69 3e 3e 20 30 29 26 30 78  [3] = (i>> 0)&0x
3832a 46 46 3b 0a 20 20 72 65 74 75 72 6e 20 34 3b 0a  FF;.  return 4;.
3832b 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 77 72 69  }.static int wri
3832c 74 65 49 6e 74 36 34 28 75 38 20 2a 70 2c 20 69  teInt64(u8 *p, i
3832d 36 34 20 69 29 7b 0a 20 20 70 5b 30 5d 20 3d 20  64 i){.  p[0] = 
3832e 28 69 3e 3e 35 36 29 26 30 78 46 46 3b 0a 20 20  (i>>56)&0xFF;.  
3832f 70 5b 31 5d 20 3d 20 28 69 3e 3e 34 38 29 26 30  p[1] = (i>>48)&0
38330 78 46 46 3b 0a 20 20 70 5b 32 5d 20 3d 20 28 69  xFF;.  p[2] = (i
38331 3e 3e 34 30 29 26 30 78 46 46 3b 0a 20 20 70 5b  >>40)&0xFF;.  p[
38332 33 5d 20 3d 20 28 69 3e 3e 33 32 29 26 30 78 46  3] = (i>>32)&0xF
38333 46 3b 0a 20 20 70 5b 34 5d 20 3d 20 28 69 3e 3e  F;.  p[4] = (i>>
38334 32 34 29 26 30 78 46 46 3b 0a 20 20 70 5b 35 5d  24)&0xFF;.  p[5]
38335 20 3d 20 28 69 3e 3e 31 36 29 26 30 78 46 46 3b   = (i>>16)&0xFF;
38336 0a 20 20 70 5b 36 5d 20 3d 20 28 69 3e 3e 20 38  .  p[6] = (i>> 8
38337 29 26 30 78 46 46 3b 0a 20 20 70 5b 37 5d 20 3d  )&0xFF;.  p[7] =
38338 20 28 69 3e 3e 20 30 29 26 30 78 46 46 3b 0a 20   (i>> 0)&0xFF;. 
38339 20 72 65 74 75 72 6e 20 38 3b 0a 7d 0a 0a 2f 2a   return 8;.}../*
3833a 0a 2a 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68  .** Increment th
3833b 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  e reference coun
3833c 74 20 6f 66 20 6e 6f 64 65 20 70 2e 0a 2a 2f 0a  t of node p..*/.
3833d 73 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 64 65  static void node
3833e 52 65 66 65 72 65 6e 63 65 28 52 74 72 65 65 4e  Reference(RtreeN
3833f 6f 64 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 70  ode *p){.  if( p
38340 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 52 65 66 2b   ){.    p->nRef+
38341 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  +;.  }.}../*.** 
38342 43 6c 65 61 72 20 74 68 65 20 63 6f 6e 74 65 6e  Clear the conten
38343 74 20 6f 66 20 6e 6f 64 65 20 70 20 28 73 65 74  t of node p (set
38344 20 61 6c 6c 20 62 79 74 65 73 20 74 6f 20 30 78   all bytes to 0x
38345 30 30 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  00)..*/.static v
38346 6f 69 64 20 6e 6f 64 65 5a 65 72 6f 28 52 74 72  oid nodeZero(Rtr
38347 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65  ee *pRtree, Rtre
38348 65 4e 6f 64 65 20 2a 70 29 7b 0a 20 20 69 66 28  eNode *p){.  if(
38349 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74   p ){.    memset
3834a 28 26 70 2d 3e 7a 44 61 74 61 5b 32 5d 2c 20 30  (&p->zData[2], 0
3834b 2c 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53  , pRtree->iNodeS
3834c 69 7a 65 2d 32 29 3b 0a 20 20 20 20 70 2d 3e 69  ize-2);.    p->i
3834d 73 44 69 72 74 79 20 3d 20 31 3b 0a 20 20 7d 0a  sDirty = 1;.  }.
3834e 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61  }../*.** Given a
3834f 20 6e 6f 64 65 20 6e 75 6d 62 65 72 20 69 4e 6f   node number iNo
38350 64 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20 63  de, return the c
38351 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 6b 65 79  orresponding key
38352 20 74 6f 20 75 73 65 0a 2a 2a 20 69 6e 20 74 68   to use.** in th
38353 65 20 52 74 72 65 65 2e 61 48 61 73 68 20 74 61  e Rtree.aHash ta
38354 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ble..*/.static i
38355 6e 74 20 6e 6f 64 65 48 61 73 68 28 69 36 34 20  nt nodeHash(i64 
38356 69 4e 6f 64 65 29 7b 0a 20 20 72 65 74 75 72 6e  iNode){.  return
38357 20 28 0a 20 20 20 20 28 69 4e 6f 64 65 3e 3e 35   (.    (iNode>>5
38358 36 29 20 5e 20 28 69 4e 6f 64 65 3e 3e 34 38 29  6) ^ (iNode>>48)
38359 20 5e 20 28 69 4e 6f 64 65 3e 3e 34 30 29 20 5e   ^ (iNode>>40) ^
3835a 20 28 69 4e 6f 64 65 3e 3e 33 32 29 20 5e 20 0a   (iNode>>32) ^ .
3835b 20 20 20 20 28 69 4e 6f 64 65 3e 3e 32 34 29 20      (iNode>>24) 
3835c 5e 20 28 69 4e 6f 64 65 3e 3e 31 36 29 20 5e 20  ^ (iNode>>16) ^ 
3835d 28 69 4e 6f 64 65 3e 3e 20 38 29 20 5e 20 28 69  (iNode>> 8) ^ (i
3835e 4e 6f 64 65 3e 3e 20 30 29 0a 20 20 29 20 25 20  Node>> 0).  ) % 
3835f 48 41 53 48 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a  HASHSIZE;.}../*.
38360 2a 2a 20 53 65 61 72 63 68 20 74 68 65 20 6e 6f  ** Search the no
38361 64 65 20 68 61 73 68 20 74 61 62 6c 65 20 66 6f  de hash table fo
38362 72 20 6e 6f 64 65 20 69 4e 6f 64 65 2e 20 49 66  r node iNode. If
38363 20 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e 20 61   found, return a
38364 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 69   pointer.** to i
38365 74 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65  t. Otherwise, re
38366 74 75 72 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74 69  turn 0..*/.stati
38367 63 20 52 74 72 65 65 4e 6f 64 65 20 2a 6e 6f 64  c RtreeNode *nod
38368 65 48 61 73 68 4c 6f 6f 6b 75 70 28 52 74 72 65  eHashLookup(Rtre
38369 65 20 2a 70 52 74 72 65 65 2c 20 69 36 34 20 69  e *pRtree, i64 i
3836a 4e 6f 64 65 29 7b 0a 20 20 52 74 72 65 65 4e 6f  Node){.  RtreeNo
3836b 64 65 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28  de *p;.  assert(
3836c 20 69 4e 6f 64 65 21 3d 30 20 29 3b 0a 20 20 66   iNode!=0 );.  f
3836d 6f 72 28 70 3d 70 52 74 72 65 65 2d 3e 61 48 61  or(p=pRtree->aHa
3836e 73 68 5b 6e 6f 64 65 48 61 73 68 28 69 4e 6f 64  sh[nodeHash(iNod
3836f 65 29 5d 3b 20 70 20 26 26 20 70 2d 3e 69 4e 6f  e)]; p && p->iNo
38370 64 65 21 3d 69 4e 6f 64 65 3b 20 70 3d 70 2d 3e  de!=iNode; p=p->
38371 70 4e 65 78 74 29 3b 0a 20 20 72 65 74 75 72 6e  pNext);.  return
38372 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64   p;.}../*.** Add
38373 20 6e 6f 64 65 20 70 4e 6f 64 65 20 74 6f 20 74   node pNode to t
38374 68 65 20 6e 6f 64 65 20 68 61 73 68 20 74 61 62  he node hash tab
38375 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  le..*/.static vo
38376 69 64 20 6e 6f 64 65 48 61 73 68 49 6e 73 65 72  id nodeHashInser
38377 74 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c  t(Rtree *pRtree,
38378 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64   RtreeNode *pNod
38379 65 29 7b 0a 20 20 69 66 28 20 70 4e 6f 64 65 20  e){.  if( pNode 
3837a 29 7b 0a 20 20 20 20 69 6e 74 20 69 48 61 73 68  ){.    int iHash
3837b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e  ;.    assert( pN
3837c 6f 64 65 2d 3e 70 4e 65 78 74 3d 3d 30 20 29 3b  ode->pNext==0 );
3837d 0a 20 20 20 20 69 48 61 73 68 20 3d 20 6e 6f 64  .    iHash = nod
3837e 65 48 61 73 68 28 70 4e 6f 64 65 2d 3e 69 4e 6f  eHash(pNode->iNo
3837f 64 65 29 3b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e  de);.    pNode->
38380 70 4e 65 78 74 20 3d 20 70 52 74 72 65 65 2d 3e  pNext = pRtree->
38381 61 48 61 73 68 5b 69 48 61 73 68 5d 3b 0a 20 20  aHash[iHash];.  
38382 20 20 70 52 74 72 65 65 2d 3e 61 48 61 73 68 5b    pRtree->aHash[
38383 69 48 61 73 68 5d 20 3d 20 70 4e 6f 64 65 3b 0a  iHash] = pNode;.
38384 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d    }.}../*.** Rem
38385 6f 76 65 20 6e 6f 64 65 20 70 4e 6f 64 65 20 66  ove node pNode f
38386 72 6f 6d 20 74 68 65 20 6e 6f 64 65 20 68 61 73  rom the node has
38387 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74  h table..*/.stat
38388 69 63 20 76 6f 69 64 20 6e 6f 64 65 48 61 73 68  ic void nodeHash
38389 44 65 6c 65 74 65 28 52 74 72 65 65 20 2a 70 52  Delete(Rtree *pR
3838a 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20  tree, RtreeNode 
3838b 2a 70 4e 6f 64 65 29 7b 0a 20 20 52 74 72 65 65  *pNode){.  Rtree
3838c 4e 6f 64 65 20 2a 2a 70 70 3b 0a 20 20 69 66 28  Node **pp;.  if(
3838d 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 21 3d 30   pNode->iNode!=0
3838e 20 29 7b 0a 20 20 20 20 70 70 20 3d 20 26 70 52   ){.    pp = &pR
3838f 74 72 65 65 2d 3e 61 48 61 73 68 5b 6e 6f 64 65  tree->aHash[node
38390 48 61 73 68 28 70 4e 6f 64 65 2d 3e 69 4e 6f 64  Hash(pNode->iNod
38391 65 29 5d 3b 0a 20 20 20 20 66 6f 72 28 20 3b 20  e)];.    for( ; 
38392 28 2a 70 70 29 21 3d 70 4e 6f 64 65 3b 20 70 70  (*pp)!=pNode; pp
38393 20 3d 20 26 28 2a 70 70 29 2d 3e 70 4e 65 78 74   = &(*pp)->pNext
38394 29 7b 20 61 73 73 65 72 74 28 2a 70 70 29 3b 20  ){ assert(*pp); 
38395 7d 0a 20 20 20 20 2a 70 70 20 3d 20 70 4e 6f 64  }.    *pp = pNod
38396 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 70 4e  e->pNext;.    pN
38397 6f 64 65 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a  ode->pNext = 0;.
38398 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c    }.}../*.** All
38399 6f 63 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e  ocate and return
3839a 20 6e 65 77 20 72 2d 74 72 65 65 20 6e 6f 64 65   new r-tree node
3839b 2e 20 49 6e 69 74 69 61 6c 6c 79 2c 20 28 52 74  . Initially, (Rt
3839c 72 65 65 4e 6f 64 65 2e 69 4e 6f 64 65 3d 3d 30  reeNode.iNode==0
3839d 29 2c 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67  ),.** indicating
3839e 20 74 68 61 74 20 6e 6f 64 65 20 68 61 73 20 6e   that node has n
3839f 6f 74 20 79 65 74 20 62 65 65 6e 20 61 73 73 69  ot yet been assi
383a0 67 6e 65 64 20 61 20 6e 6f 64 65 20 6e 75 6d 62  gned a node numb
383a1 65 72 2e 20 49 74 20 69 73 0a 2a 2a 20 61 73 73  er. It is.** ass
383a2 69 67 6e 65 64 20 61 20 6e 6f 64 65 20 6e 75 6d  igned a node num
383a3 62 65 72 20 77 68 65 6e 20 6e 6f 64 65 57 72 69  ber when nodeWri
383a4 74 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20 74  te() is called t
383a5 6f 20 77 72 69 74 65 20 74 68 65 0a 2a 2a 20 6e  o write the.** n
383a6 6f 64 65 20 63 6f 6e 74 65 6e 74 73 20 6f 75 74  ode contents out
383a7 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
383a8 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 52 74 72 65  ..*/.static Rtre
383a9 65 4e 6f 64 65 20 2a 6e 6f 64 65 4e 65 77 28 52  eNode *nodeNew(R
383aa 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74  tree *pRtree, Rt
383ab 72 65 65 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74  reeNode *pParent
383ac 2c 20 69 6e 74 20 7a 65 72 6f 29 7b 0a 20 20 52  , int zero){.  R
383ad 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 3b  treeNode *pNode;
383ae 0a 20 20 70 4e 6f 64 65 20 3d 20 28 52 74 72 65  .  pNode = (Rtre
383af 65 4e 6f 64 65 20 2a 29 73 71 6c 69 74 65 33 5f  eNode *)sqlite3_
383b0 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 52 74  malloc(sizeof(Rt
383b1 72 65 65 4e 6f 64 65 29 20 2b 20 70 52 74 72 65  reeNode) + pRtre
383b2 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 29 3b 0a 20  e->iNodeSize);. 
383b3 20 69 66 28 20 70 4e 6f 64 65 20 29 7b 0a 20 20   if( pNode ){.  
383b4 20 20 6d 65 6d 73 65 74 28 70 4e 6f 64 65 2c 20    memset(pNode, 
383b5 30 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65 4e  0, sizeof(RtreeN
383b6 6f 64 65 29 20 2b 20 28 7a 65 72 6f 3f 70 52 74  ode) + (zero?pRt
383b7 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 3a 30  ree->iNodeSize:0
383b8 29 29 3b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 7a  ));.    pNode->z
383b9 44 61 74 61 20 3d 20 28 75 38 20 2a 29 26 70 4e  Data = (u8 *)&pN
383ba 6f 64 65 5b 31 5d 3b 0a 20 20 20 20 70 4e 6f 64  ode[1];.    pNod
383bb 65 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20  e->nRef = 1;.   
383bc 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 20   pNode->pParent 
383bd 3d 20 70 50 61 72 65 6e 74 3b 0a 20 20 20 20 70  = pParent;.    p
383be 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79 20 3d 20  Node->isDirty = 
383bf 31 3b 0a 20 20 20 20 6e 6f 64 65 52 65 66 65 72  1;.    nodeRefer
383c0 65 6e 63 65 28 70 50 61 72 65 6e 74 29 3b 0a 20  ence(pParent);. 
383c1 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 6f 64   }.  return pNod
383c2 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61  e;.}../*.** Obta
383c3 69 6e 20 61 20 72 65 66 65 72 65 6e 63 65 20 74  in a reference t
383c4 6f 20 61 6e 20 72 2d 74 72 65 65 20 6e 6f 64 65  o an r-tree node
383c5 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 0a  ..*/.static int.
383c6 6e 6f 64 65 41 63 71 75 69 72 65 28 0a 20 20 52  nodeAcquire(.  R
383c7 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 20 20  tree *pRtree,   
383c8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 2d 74            /* R-t
383c9 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f  ree structure */
383ca 0a 20 20 69 36 34 20 69 4e 6f 64 65 2c 20 20 20  .  i64 iNode,   
383cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
383cc 20 4e 6f 64 65 20 6e 75 6d 62 65 72 20 74 6f 20   Node number to 
383cd 6c 6f 61 64 20 2a 2f 0a 20 20 52 74 72 65 65 4e  load */.  RtreeN
383ce 6f 64 65 20 2a 70 50 61 72 65 6e 74 2c 20 20 20  ode *pParent,   
383cf 20 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20 74       /* Either t
383d0 68 65 20 70 61 72 65 6e 74 20 6e 6f 64 65 20 6f  he parent node o
383d1 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 52 74 72 65  r NULL */.  Rtre
383d2 65 4e 6f 64 65 20 2a 2a 70 70 4e 6f 64 65 20 20  eNode **ppNode  
383d3 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41         /* OUT: A
383d4 63 71 75 69 72 65 64 20 6e 6f 64 65 20 2a 2f 0a  cquired node */.
383d5 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 52  ){.  int rc;.  R
383d6 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 3b  treeNode *pNode;
383d7 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20  ..  /* Check if 
383d8 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6e 6f  the requested no
383d9 64 65 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e  de is already in
383da 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e   the hash table.
383db 20 49 66 20 73 6f 2c 0a 20 20 2a 2a 20 69 6e 63   If so,.  ** inc
383dc 72 65 61 73 65 20 69 74 73 20 72 65 66 65 72 65  rease its refere
383dd 6e 63 65 20 63 6f 75 6e 74 20 61 6e 64 20 72 65  nce count and re
383de 74 75 72 6e 20 69 74 2e 0a 20 20 2a 2f 0a 20 20  turn it..  */.  
383df 69 66 28 20 28 70 4e 6f 64 65 20 3d 20 6e 6f 64  if( (pNode = nod
383e0 65 48 61 73 68 4c 6f 6f 6b 75 70 28 70 52 74 72  eHashLookup(pRtr
383e1 65 65 2c 20 69 4e 6f 64 65 29 29 20 29 7b 0a 20  ee, iNode)) ){. 
383e2 20 20 20 61 73 73 65 72 74 28 20 21 70 50 61 72     assert( !pPar
383e3 65 6e 74 20 7c 7c 20 21 70 4e 6f 64 65 2d 3e 70  ent || !pNode->p
383e4 50 61 72 65 6e 74 20 7c 7c 20 70 4e 6f 64 65 2d  Parent || pNode-
383e5 3e 70 50 61 72 65 6e 74 3d 3d 70 50 61 72 65 6e  >pParent==pParen
383e6 74 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61  t );.    if( pPa
383e7 72 65 6e 74 20 26 26 20 21 70 4e 6f 64 65 2d 3e  rent && !pNode->
383e8 70 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 20  pParent ){.     
383e9 20 6e 6f 64 65 52 65 66 65 72 65 6e 63 65 28 70   nodeReference(p
383ea 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 20 20 70  Parent);.      p
383eb 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 20 3d 20  Node->pParent = 
383ec 70 50 61 72 65 6e 74 3b 0a 20 20 20 20 7d 0a 20  pParent;.    }. 
383ed 20 20 20 70 4e 6f 64 65 2d 3e 6e 52 65 66 2b 2b     pNode->nRef++
383ee 3b 0a 20 20 20 20 2a 70 70 4e 6f 64 65 20 3d 20  ;.    *ppNode = 
383ef 70 4e 6f 64 65 3b 0a 20 20 20 20 72 65 74 75 72  pNode;.    retur
383f0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
383f1 0a 0a 20 20 70 4e 6f 64 65 20 3d 20 28 52 74 72  ..  pNode = (Rtr
383f2 65 65 4e 6f 64 65 20 2a 29 73 71 6c 69 74 65 33  eeNode *)sqlite3
383f3 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 52  _malloc(sizeof(R
383f4 74 72 65 65 4e 6f 64 65 29 20 2b 20 70 52 74 72  treeNode) + pRtr
383f5 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 29 3b 0a  ee->iNodeSize);.
383f6 20 20 69 66 28 20 21 70 4e 6f 64 65 20 29 7b 0a    if( !pNode ){.
383f7 20 20 20 20 2a 70 70 4e 6f 64 65 20 3d 20 30 3b      *ppNode = 0;
383f8 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
383f9 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
383fa 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 20 3d  pNode->pParent =
383fb 20 70 50 61 72 65 6e 74 3b 0a 20 20 70 4e 6f 64   pParent;.  pNod
383fc 65 2d 3e 7a 44 61 74 61 20 3d 20 28 75 38 20 2a  e->zData = (u8 *
383fd 29 26 70 4e 6f 64 65 5b 31 5d 3b 0a 20 20 70 4e  )&pNode[1];.  pN
383fe 6f 64 65 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20  ode->nRef = 1;. 
383ff 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 20 3d 20   pNode->iNode = 
38400 69 4e 6f 64 65 3b 0a 20 20 70 4e 6f 64 65 2d 3e  iNode;.  pNode->
38401 69 73 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 70  isDirty = 0;.  p
38402 4e 6f 64 65 2d 3e 70 4e 65 78 74 20 3d 20 30 3b  Node->pNext = 0;
38403 0a 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ..  sqlite3_bind
38404 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70  _int64(pRtree->p
38405 52 65 61 64 4e 6f 64 65 2c 20 31 2c 20 69 4e 6f  ReadNode, 1, iNo
38406 64 65 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  de);.  rc = sqli
38407 74 65 33 5f 73 74 65 70 28 70 52 74 72 65 65 2d  te3_step(pRtree-
38408 3e 70 52 65 61 64 4e 6f 64 65 29 3b 0a 20 20 69  >pReadNode);.  i
38409 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f  f( rc==SQLITE_RO
3840a 57 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 75  W ){.    const u
3840b 38 20 2a 7a 42 6c 6f 62 20 3d 20 73 71 6c 69 74  8 *zBlob = sqlit
3840c 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 70  e3_column_blob(p
3840d 52 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f 64 65  Rtree->pReadNode
3840e 2c 20 30 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79  , 0);.    memcpy
3840f 28 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 2c 20 7a  (pNode->zData, z
38410 42 6c 6f 62 2c 20 70 52 74 72 65 65 2d 3e 69 4e  Blob, pRtree->iN
38411 6f 64 65 53 69 7a 65 29 3b 0a 20 20 20 20 6e 6f  odeSize);.    no
38412 64 65 52 65 66 65 72 65 6e 63 65 28 70 50 61 72  deReference(pPar
38413 65 6e 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ent);.  }else{. 
38414 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
38415 70 4e 6f 64 65 29 3b 0a 20 20 20 20 70 4e 6f 64  pNode);.    pNod
38416 65 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2a 70  e = 0;.  }..  *p
38417 70 4e 6f 64 65 20 3d 20 70 4e 6f 64 65 3b 0a 20  pNode = pNode;. 
38418 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65   rc = sqlite3_re
38419 73 65 74 28 70 52 74 72 65 65 2d 3e 70 52 65 61  set(pRtree->pRea
3841a 64 4e 6f 64 65 29 3b 0a 0a 20 20 69 66 28 20 72  dNode);..  if( r
3841b 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
3841c 69 4e 6f 64 65 3d 3d 31 20 29 7b 0a 20 20 20 20  iNode==1 ){.    
3841d 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 20 3d  pRtree->iDepth =
3841e 20 72 65 61 64 49 6e 74 31 36 28 70 4e 6f 64 65   readInt16(pNode
3841f 2d 3e 7a 44 61 74 61 29 3b 0a 20 20 7d 0a 0a 20  ->zData);.  }.. 
38420 20 61 73 73 65 72 74 28 20 28 72 63 3d 3d 53 51   assert( (rc==SQ
38421 4c 49 54 45 5f 4f 4b 20 26 26 20 70 4e 6f 64 65  LITE_OK && pNode
38422 29 20 7c 7c 20 28 70 4e 6f 64 65 3d 3d 30 20 26  ) || (pNode==0 &
38423 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 29  & rc!=SQLITE_OK)
38424 20 29 3b 0a 20 20 6e 6f 64 65 48 61 73 68 49 6e   );.  nodeHashIn
38425 73 65 72 74 28 70 52 74 72 65 65 2c 20 70 4e 6f  sert(pRtree, pNo
38426 64 65 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72  de);..  return r
38427 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 76 65 72  c;.}../*.** Over
38428 77 72 69 74 65 20 63 65 6c 6c 20 69 43 65 6c 6c  write cell iCell
38429 20 6f 66 20 6e 6f 64 65 20 70 4e 6f 64 65 20 77   of node pNode w
3842a 69 74 68 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  ith the contents
3842b 20 6f 66 20 70 43 65 6c 6c 2e 0a 2a 2f 0a 73 74   of pCell..*/.st
3842c 61 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 4f 76  atic void nodeOv
3842d 65 72 77 72 69 74 65 43 65 6c 6c 28 0a 20 20 52  erwriteCell(.  R
3842e 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20  tree *pRtree, . 
3842f 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64   RtreeNode *pNod
38430 65 2c 20 20 0a 20 20 52 74 72 65 65 43 65 6c 6c  e,  .  RtreeCell
38431 20 2a 70 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20   *pCell, .  int 
38432 69 43 65 6c 6c 0a 29 7b 0a 20 20 69 6e 74 20 69  iCell.){.  int i
38433 69 3b 0a 20 20 75 38 20 2a 70 20 3d 20 26 70 4e  i;.  u8 *p = &pN
38434 6f 64 65 2d 3e 7a 44 61 74 61 5b 34 20 2b 20 70  ode->zData[4 + p
38435 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50 65 72  Rtree->nBytesPer
38436 43 65 6c 6c 2a 69 43 65 6c 6c 5d 3b 0a 20 20 70  Cell*iCell];.  p
38437 20 2b 3d 20 77 72 69 74 65 49 6e 74 36 34 28 70   += writeInt64(p
38438 2c 20 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64 29  , pCell->iRowid)
38439 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ;.  for(ii=0; ii
3843a 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32  <(pRtree->nDim*2
3843b 29 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 70 20  ); ii++){.    p 
3843c 2b 3d 20 77 72 69 74 65 43 6f 6f 72 64 28 70 2c  += writeCoord(p,
3843d 20 26 70 43 65 6c 6c 2d 3e 61 43 6f 6f 72 64 5b   &pCell->aCoord[
3843e 69 69 5d 29 3b 0a 20 20 7d 0a 20 20 70 4e 6f 64  ii]);.  }.  pNod
3843f 65 2d 3e 69 73 44 69 72 74 79 20 3d 20 31 3b 0a  e->isDirty = 1;.
38440 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20  }../*.** Remove 
38441 63 65 6c 6c 20 74 68 65 20 63 65 6c 6c 20 77 69  cell the cell wi
38442 74 68 20 69 6e 64 65 78 20 69 43 65 6c 6c 20 66  th index iCell f
38443 72 6f 6d 20 6e 6f 64 65 20 70 4e 6f 64 65 2e 0a  rom node pNode..
38444 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e  */.static void n
38445 6f 64 65 44 65 6c 65 74 65 43 65 6c 6c 28 52 74  odeDeleteCell(Rt
38446 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72  ree *pRtree, Rtr
38447 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 69  eeNode *pNode, i
38448 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20 75 38 20  nt iCell){.  u8 
38449 2a 70 44 73 74 20 3d 20 26 70 4e 6f 64 65 2d 3e  *pDst = &pNode->
3844a 7a 44 61 74 61 5b 34 20 2b 20 70 52 74 72 65 65  zData[4 + pRtree
3844b 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 2a  ->nBytesPerCell*
3844c 69 43 65 6c 6c 5d 3b 0a 20 20 75 38 20 2a 70 53  iCell];.  u8 *pS
3844d 72 63 20 3d 20 26 70 44 73 74 5b 70 52 74 72 65  rc = &pDst[pRtre
3844e 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c  e->nBytesPerCell
3844f 5d 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 20 3d  ];.  int nByte =
38450 20 28 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 20 2d   (NCELL(pNode) -
38451 20 69 43 65 6c 6c 20 2d 20 31 29 20 2a 20 70 52   iCell - 1) * pR
38452 74 72 65 65 2d 3e 6e 42 79 74 65 73 50 65 72 43  tree->nBytesPerC
38453 65 6c 6c 3b 0a 20 20 6d 65 6d 6d 6f 76 65 28 70  ell;.  memmove(p
38454 44 73 74 2c 20 70 53 72 63 2c 20 6e 42 79 74 65  Dst, pSrc, nByte
38455 29 3b 0a 20 20 77 72 69 74 65 49 6e 74 31 36 28  );.  writeInt16(
38456 26 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 5b 32 5d  &pNode->zData[2]
38457 2c 20 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 2d 31  , NCELL(pNode)-1
38458 29 3b 0a 20 20 70 4e 6f 64 65 2d 3e 69 73 44 69  );.  pNode->isDi
38459 72 74 79 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a  rty = 1;.}../*.*
3845a 2a 20 49 6e 73 65 72 74 20 74 68 65 20 63 6f 6e  * Insert the con
3845b 74 65 6e 74 73 20 6f 66 20 63 65 6c 6c 20 70 43  tents of cell pC
3845c 65 6c 6c 20 69 6e 74 6f 20 6e 6f 64 65 20 70 4e  ell into node pN
3845d 6f 64 65 2e 20 49 66 20 74 68 65 20 69 6e 73 65  ode. If the inse
3845e 72 74 0a 2a 2a 20 69 73 20 73 75 63 63 65 73 73  rt.** is success
3845f 66 75 6c 2c 20 72 65 74 75 72 6e 20 53 51 4c 49  ful, return SQLI
38460 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  TE_OK..**.** If 
38461 74 68 65 72 65 20 69 73 20 6e 6f 74 20 65 6e 6f  there is not eno
38462 75 67 68 20 66 72 65 65 20 73 70 61 63 65 20 69  ugh free space i
38463 6e 20 70 4e 6f 64 65 2c 20 72 65 74 75 72 6e 20  n pNode, return 
38464 53 51 4c 49 54 45 5f 46 55 4c 4c 2e 0a 2a 2f 0a  SQLITE_FULL..*/.
38465 73 74 61 74 69 63 20 69 6e 74 0a 6e 6f 64 65 49  static int.nodeI
38466 6e 73 65 72 74 43 65 6c 6c 28 0a 20 20 52 74 72  nsertCell(.  Rtr
38467 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20 20 52  ee *pRtree, .  R
38468 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c  treeNode *pNode,
38469 20 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70   .  RtreeCell *p
3846a 43 65 6c 6c 20 0a 29 7b 0a 20 20 69 6e 74 20 6e  Cell .){.  int n
3846b 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20  Cell;           
3846c 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72           /* Curr
3846d 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 63 65  ent number of ce
3846e 6c 6c 73 20 69 6e 20 70 4e 6f 64 65 20 2a 2f 0a  lls in pNode */.
3846f 20 20 69 6e 74 20 6e 4d 61 78 43 65 6c 6c 3b 20    int nMaxCell; 
38470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38471 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  /* Maximum numbe
38472 72 20 6f 66 20 63 65 6c 6c 73 20 66 6f 72 20 70  r of cells for p
38473 4e 6f 64 65 20 2a 2f 0a 0a 20 20 6e 4d 61 78 43  Node */..  nMaxC
38474 65 6c 6c 20 3d 20 28 70 52 74 72 65 65 2d 3e 69  ell = (pRtree->i
38475 4e 6f 64 65 53 69 7a 65 2d 34 29 2f 70 52 74 72  NodeSize-4)/pRtr
38476 65 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c  ee->nBytesPerCel
38477 6c 3b 0a 20 20 6e 43 65 6c 6c 20 3d 20 4e 43 45  l;.  nCell = NCE
38478 4c 4c 28 70 4e 6f 64 65 29 3b 0a 0a 20 20 61 73  LL(pNode);..  as
38479 73 65 72 74 28 6e 43 65 6c 6c 3c 3d 6e 4d 61 78  sert(nCell<=nMax
3847a 43 65 6c 6c 29 3b 0a 0a 20 20 69 66 28 20 6e 43  Cell);..  if( nC
3847b 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c 20 29 7b 0a  ell<nMaxCell ){.
3847c 20 20 20 20 6e 6f 64 65 4f 76 65 72 77 72 69 74      nodeOverwrit
3847d 65 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e  eCell(pRtree, pN
3847e 6f 64 65 2c 20 70 43 65 6c 6c 2c 20 6e 43 65 6c  ode, pCell, nCel
3847f 6c 29 3b 0a 20 20 20 20 77 72 69 74 65 49 6e 74  l);.    writeInt
38480 31 36 28 26 70 4e 6f 64 65 2d 3e 7a 44 61 74 61  16(&pNode->zData
38481 5b 32 5d 2c 20 6e 43 65 6c 6c 2b 31 29 3b 0a 20  [2], nCell+1);. 
38482 20 20 20 70 4e 6f 64 65 2d 3e 69 73 44 69 72 74     pNode->isDirt
38483 79 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 72 65  y = 1;.  }..  re
38484 74 75 72 6e 20 28 6e 43 65 6c 6c 3d 3d 6e 4d 61  turn (nCell==nMa
38485 78 43 65 6c 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  xCell);.}../*.**
38486 20 49 66 20 74 68 65 20 6e 6f 64 65 20 69 73 20   If the node is 
38487 64 69 72 74 79 2c 20 77 72 69 74 65 20 69 74 20  dirty, write it 
38488 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 62  out to the datab
38489 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ase..*/.static i
3848a 6e 74 0a 6e 6f 64 65 57 72 69 74 65 28 52 74 72  nt.nodeWrite(Rtr
3848b 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65  ee *pRtree, Rtre
3848c 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0a 20  eNode *pNode){. 
3848d 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
3848e 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 4e 6f 64 65  _OK;.  if( pNode
3848f 2d 3e 69 73 44 69 72 74 79 20 29 7b 0a 20 20 20  ->isDirty ){.   
38490 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
38491 20 3d 20 70 52 74 72 65 65 2d 3e 70 57 72 69 74   = pRtree->pWrit
38492 65 4e 6f 64 65 3b 0a 20 20 20 20 69 66 28 20 70  eNode;.    if( p
38493 4e 6f 64 65 2d 3e 69 4e 6f 64 65 20 29 7b 0a 20  Node->iNode ){. 
38494 20 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e       sqlite3_bin
38495 64 5f 69 6e 74 36 34 28 70 2c 20 31 2c 20 70 4e  d_int64(p, 1, pN
38496 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 20  ode->iNode);.   
38497 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71   }else{.      sq
38498 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28  lite3_bind_null(
38499 70 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20  p, 1);.    }.   
3849a 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c   sqlite3_bind_bl
3849b 6f 62 28 70 2c 20 32 2c 20 70 4e 6f 64 65 2d 3e  ob(p, 2, pNode->
3849c 7a 44 61 74 61 2c 20 70 52 74 72 65 65 2d 3e 69  zData, pRtree->i
3849d 4e 6f 64 65 53 69 7a 65 2c 20 53 51 4c 49 54 45  NodeSize, SQLITE
3849e 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73 71  _STATIC);.    sq
3849f 6c 69 74 65 33 5f 73 74 65 70 28 70 29 3b 0a 20  lite3_step(p);. 
384a0 20 20 20 70 4e 6f 64 65 2d 3e 69 73 44 69 72 74     pNode->isDirt
384a1 79 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20  y = 0;.    rc = 
384a2 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 29  sqlite3_reset(p)
384a3 3b 0a 20 20 20 20 69 66 28 20 70 4e 6f 64 65 2d  ;.    if( pNode-
384a4 3e 69 4e 6f 64 65 3d 3d 30 20 26 26 20 72 63 3d  >iNode==0 && rc=
384a5 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
384a6 20 20 20 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65      pNode->iNode
384a7 20 3d 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f   = sqlite3_last_
384a8 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 70 52 74  insert_rowid(pRt
384a9 72 65 65 2d 3e 64 62 29 3b 0a 20 20 20 20 20 20  ree->db);.      
384aa 6e 6f 64 65 48 61 73 68 49 6e 73 65 72 74 28 70  nodeHashInsert(p
384ab 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20  Rtree, pNode);. 
384ac 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
384ad 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  n rc;.}../*.** R
384ae 65 6c 65 61 73 65 20 61 20 72 65 66 65 72 65 6e  elease a referen
384af 63 65 20 74 6f 20 61 20 6e 6f 64 65 2e 20 49 66  ce to a node. If
384b0 20 74 68 65 20 6e 6f 64 65 20 69 73 20 64 69 72   the node is dir
384b1 74 79 20 61 6e 64 20 74 68 65 20 72 65 66 65 72  ty and the refer
384b2 65 6e 63 65 0a 2a 2a 20 63 6f 75 6e 74 20 64 72  ence.** count dr
384b3 6f 70 73 20 74 6f 20 7a 65 72 6f 2c 20 74 68 65  ops to zero, the
384b4 20 6e 6f 64 65 20 64 61 74 61 20 69 73 20 77 72   node data is wr
384b5 69 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74  itten to the dat
384b6 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  abase..*/.static
384b7 20 69 6e 74 0a 6e 6f 64 65 52 65 6c 65 61 73 65   int.nodeRelease
384b8 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20  (Rtree *pRtree, 
384b9 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65  RtreeNode *pNode
384ba 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
384bb 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70  LITE_OK;.  if( p
384bc 4e 6f 64 65 20 29 7b 0a 20 20 20 20 61 73 73 65  Node ){.    asse
384bd 72 74 28 20 70 4e 6f 64 65 2d 3e 6e 52 65 66 3e  rt( pNode->nRef>
384be 30 20 29 3b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e  0 );.    pNode->
384bf 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28 20  nRef--;.    if( 
384c0 70 4e 6f 64 65 2d 3e 6e 52 65 66 3d 3d 30 20 29  pNode->nRef==0 )
384c1 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4e 6f 64  {.      if( pNod
384c2 65 2d 3e 69 4e 6f 64 65 3d 3d 31 20 29 7b 0a 20  e->iNode==1 ){. 
384c3 20 20 20 20 20 20 20 70 52 74 72 65 65 2d 3e 69         pRtree->i
384c4 44 65 70 74 68 20 3d 20 2d 31 3b 0a 20 20 20 20  Depth = -1;.    
384c5 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4e    }.      if( pN
384c6 6f 64 65 2d 3e 70 50 61 72 65 6e 74 20 29 7b 0a  ode->pParent ){.
384c7 20 20 20 20 20 20 20 20 72 63 20 3d 20 6e 6f 64          rc = nod
384c8 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
384c9 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 29   pNode->pParent)
384ca 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
384cb 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
384cc 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  K ){.        rc 
384cd 3d 20 6e 6f 64 65 57 72 69 74 65 28 70 52 74 72  = nodeWrite(pRtr
384ce 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20  ee, pNode);.    
384cf 20 20 7d 0a 20 20 20 20 20 20 6e 6f 64 65 48 61    }.      nodeHa
384d0 73 68 44 65 6c 65 74 65 28 70 52 74 72 65 65 2c  shDelete(pRtree,
384d1 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 73   pNode);.      s
384d2 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4e 6f 64  qlite3_free(pNod
384d3 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  e);.    }.  }.  
384d4 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
384d5 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 36  .** Return the 6
384d6 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 76 61  4-bit integer va
384d7 6c 75 65 20 61 73 73 6f 63 69 61 74 65 64 20 77  lue associated w
384d8 69 74 68 20 63 65 6c 6c 20 69 43 65 6c 6c 20 6f  ith cell iCell o
384d9 66 0a 2a 2a 20 6e 6f 64 65 20 70 4e 6f 64 65 2e  f.** node pNode.
384da 20 49 66 20 70 4e 6f 64 65 20 69 73 20 61 20 6c   If pNode is a l
384db 65 61 66 20 6e 6f 64 65 2c 20 74 68 69 73 20 69  eaf node, this i
384dc 73 20 61 20 72 6f 77 69 64 2e 20 49 66 20 69 74  s a rowid. If it
384dd 20 69 73 0a 2a 2a 20 61 6e 20 69 6e 74 65 72 6e   is.** an intern
384de 61 6c 20 6e 6f 64 65 2c 20 74 68 65 6e 20 74 68  al node, then th
384df 65 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72  e 64-bit integer
384e0 20 69 73 20 61 20 63 68 69 6c 64 20 70 61 67 65   is a child page
384e1 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74   number..*/.stat
384e2 69 63 20 69 36 34 20 6e 6f 64 65 47 65 74 52 6f  ic i64 nodeGetRo
384e3 77 69 64 28 0a 20 20 52 74 72 65 65 20 2a 70 52  wid(.  Rtree *pR
384e4 74 72 65 65 2c 20 0a 20 20 52 74 72 65 65 4e 6f  tree, .  RtreeNo
384e5 64 65 20 2a 70 4e 6f 64 65 2c 20 0a 20 20 69 6e  de *pNode, .  in
384e6 74 20 69 43 65 6c 6c 0a 29 7b 0a 20 20 61 73 73  t iCell.){.  ass
384e7 65 72 74 28 20 69 43 65 6c 6c 3c 4e 43 45 4c 4c  ert( iCell<NCELL
384e8 28 70 4e 6f 64 65 29 20 29 3b 0a 20 20 72 65 74  (pNode) );.  ret
384e9 75 72 6e 20 72 65 61 64 49 6e 74 36 34 28 26 70  urn readInt64(&p
384ea 4e 6f 64 65 2d 3e 7a 44 61 74 61 5b 34 20 2b 20  Node->zData[4 + 
384eb 70 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50 65  pRtree->nBytesPe
384ec 72 43 65 6c 6c 2a 69 43 65 6c 6c 5d 29 3b 0a 7d  rCell*iCell]);.}
384ed 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 63  ../*.** Return c
384ee 6f 6f 72 64 69 6e 61 74 65 20 69 43 6f 6f 72 64  oordinate iCoord
384ef 20 66 72 6f 6d 20 63 65 6c 6c 20 69 43 65 6c 6c   from cell iCell
384f0 20 69 6e 20 6e 6f 64 65 20 70 4e 6f 64 65 2e 0a   in node pNode..
384f1 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e  */.static void n
384f2 6f 64 65 47 65 74 43 6f 6f 72 64 28 0a 20 20 52  odeGetCoord(.  R
384f3 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20  tree *pRtree, . 
384f4 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64   RtreeNode *pNod
384f5 65 2c 20 0a 20 20 69 6e 74 20 69 43 65 6c 6c 2c  e, .  int iCell,
384f6 0a 20 20 69 6e 74 20 69 43 6f 6f 72 64 2c 0a 20  .  int iCoord,. 
384f7 20 52 74 72 65 65 43 6f 6f 72 64 20 2a 70 43 6f   RtreeCoord *pCo
384f8 6f 72 64 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ord           /*
384f9 20 53 70 61 63 65 20 74 6f 20 77 72 69 74 65 20   Space to write 
384fa 72 65 73 75 6c 74 20 74 6f 20 2a 2f 0a 29 7b 0a  result to */.){.
384fb 20 20 72 65 61 64 43 6f 6f 72 64 28 26 70 4e 6f    readCoord(&pNo
384fc 64 65 2d 3e 7a 44 61 74 61 5b 31 32 20 2b 20 70  de->zData[12 + p
384fd 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50 65 72  Rtree->nBytesPer
384fe 43 65 6c 6c 2a 69 43 65 6c 6c 20 2b 20 34 2a 69  Cell*iCell + 4*i
384ff 43 6f 6f 72 64 5d 2c 20 70 43 6f 6f 72 64 29 3b  Coord], pCoord);
38500 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 65 72 69  .}../*.** Deseri
38501 61 6c 69 7a 65 20 63 65 6c 6c 20 69 43 65 6c 6c  alize cell iCell
38502 20 6f 66 20 6e 6f 64 65 20 70 4e 6f 64 65 2e 20   of node pNode. 
38503 50 6f 70 75 6c 61 74 65 20 74 68 65 20 73 74 72  Populate the str
38504 75 63 74 75 72 65 20 70 6f 69 6e 74 65 64 0a 2a  ucture pointed.*
38505 2a 20 74 6f 20 62 79 20 70 43 65 6c 6c 20 77 69  * to by pCell wi
38506 74 68 20 74 68 65 20 72 65 73 75 6c 74 73 2e 0a  th the results..
38507 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e  */.static void n
38508 6f 64 65 47 65 74 43 65 6c 6c 28 0a 20 20 52 74  odeGetCell(.  Rt
38509 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20 20  ree *pRtree, .  
3850a 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65  RtreeNode *pNode
3850b 2c 20 0a 20 20 69 6e 74 20 69 43 65 6c 6c 2c 0a  , .  int iCell,.
3850c 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65    RtreeCell *pCe
3850d 6c 6c 0a 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a  ll.){.  int ii;.
3850e 20 20 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64 20    pCell->iRowid 
3850f 3d 20 6e 6f 64 65 47 65 74 52 6f 77 69 64 28 70  = nodeGetRowid(p
38510 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 43  Rtree, pNode, iC
38511 65 6c 6c 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30  ell);.  for(ii=0
38512 3b 20 69 69 3c 70 52 74 72 65 65 2d 3e 6e 44 69  ; ii<pRtree->nDi
38513 6d 2a 32 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  m*2; ii++){.    
38514 6e 6f 64 65 47 65 74 43 6f 6f 72 64 28 70 52 74  nodeGetCoord(pRt
38515 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 43 65 6c  ree, pNode, iCel
38516 6c 2c 20 69 69 2c 20 26 70 43 65 6c 6c 2d 3e 61  l, ii, &pCell->a
38517 43 6f 6f 72 64 5b 69 69 5d 29 3b 0a 20 20 7d 0a  Coord[ii]);.  }.
38518 7d 0a 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64  }.../* Forward d
38519 65 63 6c 61 72 61 74 69 6f 6e 20 66 6f 72 20 74  eclaration for t
3851a 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74  he function that
3851b 20 64 6f 65 73 20 74 68 65 20 77 6f 72 6b 20 6f   does the work o
3851c 66 0a 2a 2a 20 74 68 65 20 76 69 72 74 75 61 6c  f.** the virtual
3851d 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 43   table module xC
3851e 72 65 61 74 65 28 29 20 61 6e 64 20 78 43 6f 6e  reate() and xCon
3851f 6e 65 63 74 28 29 20 6d 65 74 68 6f 64 73 2e 0a  nect() methods..
38520 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74  */.static int rt
38521 72 65 65 49 6e 69 74 28 0a 20 20 73 71 6c 69 74  reeInit(.  sqlit
38522 65 33 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 69 6e  e3 *, void *, in
38523 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63  t, const char *c
38524 6f 6e 73 74 2a 2c 20 73 71 6c 69 74 65 33 5f 76  onst*, sqlite3_v
38525 74 61 62 20 2a 2a 2c 20 63 68 61 72 20 2a 2a 2c  tab **, char **,
38526 20 69 6e 74 0a 29 3b 0a 0a 2f 2a 20 0a 2a 2a 20   int.);../* .** 
38527 52 74 72 65 65 20 76 69 72 74 75 61 6c 20 74 61  Rtree virtual ta
38528 62 6c 65 20 6d 6f 64 75 6c 65 20 78 43 72 65 61  ble module xCrea
38529 74 65 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74  te method..*/.st
3852a 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 43 72  atic int rtreeCr
3852b 65 61 74 65 28 0a 20 20 73 71 6c 69 74 65 33 20  eate(.  sqlite3 
3852c 2a 64 62 2c 0a 20 20 76 6f 69 64 20 2a 70 41 75  *db,.  void *pAu
3852d 78 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 63  x,.  int argc, c
3852e 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74  onst char *const
3852f 2a 61 72 67 76 2c 0a 20 20 73 71 6c 69 74 65 33  *argv,.  sqlite3
38530 5f 76 74 61 62 20 2a 2a 70 70 56 74 61 62 2c 0a  _vtab **ppVtab,.
38531 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 0a 29    char **pzErr.)
38532 7b 0a 20 20 72 65 74 75 72 6e 20 72 74 72 65 65  {.  return rtree
38533 49 6e 69 74 28 64 62 2c 20 70 41 75 78 2c 20 61  Init(db, pAux, a
38534 72 67 63 2c 20 61 72 67 76 2c 20 70 70 56 74 61  rgc, argv, ppVta
38535 62 2c 20 70 7a 45 72 72 2c 20 31 29 3b 0a 7d 0a  b, pzErr, 1);.}.
38536 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76 69  ./* .** Rtree vi
38537 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75  rtual table modu
38538 6c 65 20 78 43 6f 6e 6e 65 63 74 20 6d 65 74 68  le xConnect meth
38539 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  od..*/.static in
3853a 74 20 72 74 72 65 65 43 6f 6e 6e 65 63 74 28 0a  t rtreeConnect(.
3853b 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20    sqlite3 *db,. 
3853c 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20 69   void *pAux,.  i
3853d 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63  nt argc, const c
3853e 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c  har *const*argv,
3853f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  .  sqlite3_vtab 
38540 2a 2a 70 70 56 74 61 62 2c 0a 20 20 63 68 61 72  **ppVtab,.  char
38541 20 2a 2a 70 7a 45 72 72 0a 29 7b 0a 20 20 72 65   **pzErr.){.  re
38542 74 75 72 6e 20 72 74 72 65 65 49 6e 69 74 28 64  turn rtreeInit(d
38543 62 2c 20 70 41 75 78 2c 20 61 72 67 63 2c 20 61  b, pAux, argc, a
38544 72 67 76 2c 20 70 70 56 74 61 62 2c 20 70 7a 45  rgv, ppVtab, pzE
38545 72 72 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  rr, 0);.}../*.**
38546 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 72   Increment the r
38547 2d 74 72 65 65 20 72 65 66 65 72 65 6e 63 65 20  -tree reference 
38548 63 6f 75 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  count..*/.static
38549 20 76 6f 69 64 20 72 74 72 65 65 52 65 66 65 72   void rtreeRefer
3854a 65 6e 63 65 28 52 74 72 65 65 20 2a 70 52 74 72  ence(Rtree *pRtr
3854b 65 65 29 7b 0a 20 20 70 52 74 72 65 65 2d 3e 6e  ee){.  pRtree->n
3854c 42 75 73 79 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Busy++;.}../*.**
3854d 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 72   Decrement the r
3854e 2d 74 72 65 65 20 72 65 66 65 72 65 6e 63 65 20  -tree reference 
3854f 63 6f 75 6e 74 2e 20 57 68 65 6e 20 74 68 65 20  count. When the 
38550 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20  reference count 
38551 72 65 61 63 68 65 73 0a 2a 2a 20 7a 65 72 6f 20  reaches.** zero 
38552 74 68 65 20 73 74 72 75 63 74 75 72 65 20 69 73  the structure is
38553 20 64 65 6c 65 74 65 64 2e 0a 2a 2f 0a 73 74 61   deleted..*/.sta
38554 74 69 63 20 76 6f 69 64 20 72 74 72 65 65 52 65  tic void rtreeRe
38555 6c 65 61 73 65 28 52 74 72 65 65 20 2a 70 52 74  lease(Rtree *pRt
38556 72 65 65 29 7b 0a 20 20 70 52 74 72 65 65 2d 3e  ree){.  pRtree->
38557 6e 42 75 73 79 2d 2d 3b 0a 20 20 69 66 28 20 70  nBusy--;.  if( p
38558 52 74 72 65 65 2d 3e 6e 42 75 73 79 3d 3d 30 20  Rtree->nBusy==0 
38559 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  ){.    sqlite3_f
3855a 69 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e  inalize(pRtree->
3855b 70 52 65 61 64 4e 6f 64 65 29 3b 0a 20 20 20 20  pReadNode);.    
3855c 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
3855d 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 4e  (pRtree->pWriteN
3855e 6f 64 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ode);.    sqlite
3855f 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74 72 65  3_finalize(pRtre
38560 65 2d 3e 70 44 65 6c 65 74 65 4e 6f 64 65 29 3b  e->pDeleteNode);
38561 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e  .    sqlite3_fin
38562 61 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e 70 52  alize(pRtree->pR
38563 65 61 64 52 6f 77 69 64 29 3b 0a 20 20 20 20 73  eadRowid);.    s
38564 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
38565 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f  pRtree->pWriteRo
38566 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  wid);.    sqlite
38567 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74 72 65  3_finalize(pRtre
38568 65 2d 3e 70 44 65 6c 65 74 65 52 6f 77 69 64 29  e->pDeleteRowid)
38569 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69  ;.    sqlite3_fi
3856a 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e 70  nalize(pRtree->p
3856b 52 65 61 64 50 61 72 65 6e 74 29 3b 0a 20 20 20  ReadParent);.   
3856c 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
3856d 65 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65  e(pRtree->pWrite
3856e 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 73 71 6c  Parent);.    sql
3856f 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52  ite3_finalize(pR
38570 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 50 61 72  tree->pDeletePar
38571 65 6e 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ent);.    sqlite
38572 33 5f 66 72 65 65 28 70 52 74 72 65 65 29 3b 0a  3_free(pRtree);.
38573 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74    }.}../* .** Rt
38574 72 65 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  ree virtual tabl
38575 65 20 6d 6f 64 75 6c 65 20 78 44 69 73 63 6f 6e  e module xDiscon
38576 6e 65 63 74 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a  nect method..*/.
38577 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65  static int rtree
38578 44 69 73 63 6f 6e 6e 65 63 74 28 73 71 6c 69 74  Disconnect(sqlit
38579 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 29 7b  e3_vtab *pVtab){
3857a 0a 20 20 72 74 72 65 65 52 65 6c 65 61 73 65 28  .  rtreeRelease(
3857b 28 52 74 72 65 65 20 2a 29 70 56 74 61 62 29 3b  (Rtree *)pVtab);
3857c 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
3857d 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52  _OK;.}../* .** R
3857e 74 72 65 65 20 76 69 72 74 75 61 6c 20 74 61 62  tree virtual tab
3857f 6c 65 20 6d 6f 64 75 6c 65 20 78 44 65 73 74 72  le module xDestr
38580 6f 79 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74  oy method..*/.st
38581 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 44 65  atic int rtreeDe
38582 73 74 72 6f 79 28 73 71 6c 69 74 65 33 5f 76 74  stroy(sqlite3_vt
38583 61 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 52 74  ab *pVtab){.  Rt
38584 72 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28 52  ree *pRtree = (R
38585 74 72 65 65 20 2a 29 70 56 74 61 62 3b 0a 20 20  tree *)pVtab;.  
38586 69 6e 74 20 72 63 3b 0a 20 20 63 68 61 72 20 2a  int rc;.  char *
38587 7a 43 72 65 61 74 65 20 3d 20 73 71 6c 69 74 65  zCreate = sqlite
38588 33 5f 6d 70 72 69 6e 74 66 28 0a 20 20 20 20 22  3_mprintf(.    "
38589 44 52 4f 50 20 54 41 42 4c 45 20 27 25 71 27 2e  DROP TABLE '%q'.
3858a 27 25 71 5f 6e 6f 64 65 27 3b 22 0a 20 20 20 20  '%q_node';".    
3858b 22 44 52 4f 50 20 54 41 42 4c 45 20 27 25 71 27  "DROP TABLE '%q'
3858c 2e 27 25 71 5f 72 6f 77 69 64 27 3b 22 0a 20 20  .'%q_rowid';".  
3858d 20 20 22 44 52 4f 50 20 54 41 42 4c 45 20 27 25    "DROP TABLE '%
3858e 71 27 2e 27 25 71 5f 70 61 72 65 6e 74 27 3b 22  q'.'%q_parent';"
3858f 2c 0a 20 20 20 20 70 52 74 72 65 65 2d 3e 7a 44  ,.    pRtree->zD
38590 62 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65  b, pRtree->zName
38591 2c 20 0a 20 20 20 20 70 52 74 72 65 65 2d 3e 7a  , .    pRtree->z
38592 44 62 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61 6d  Db, pRtree->zNam
38593 65 2c 0a 20 20 20 20 70 52 74 72 65 65 2d 3e 7a  e,.    pRtree->z
38594 44 62 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61 6d  Db, pRtree->zNam
38595 65 0a 20 20 29 3b 0a 20 20 69 66 28 20 21 7a 43  e.  );.  if( !zC
38596 72 65 61 74 65 20 29 7b 0a 20 20 20 20 72 63 20  reate ){.    rc 
38597 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
38598 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
38599 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 70  = sqlite3_exec(p
3859a 52 74 72 65 65 2d 3e 64 62 2c 20 7a 43 72 65 61  Rtree->db, zCrea
3859b 74 65 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20  te, 0, 0, 0);.  
3859c 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
3859d 43 72 65 61 74 65 29 3b 0a 20 20 7d 0a 20 20 69  Create);.  }.  i
3859e 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
3859f 20 29 7b 0a 20 20 20 20 72 74 72 65 65 52 65 6c   ){.    rtreeRel
385a0 65 61 73 65 28 70 52 74 72 65 65 29 3b 0a 20 20  ease(pRtree);.  
385a1 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  }..  return rc;.
385a2 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20  }../* .** Rtree 
385a3 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
385a4 64 75 6c 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f  dule xOpen metho
385a5 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
385a6 20 72 74 72 65 65 4f 70 65 6e 28 73 71 6c 69 74   rtreeOpen(sqlit
385a7 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 2c 20  e3_vtab *pVTab, 
385a8 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
385a9 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 29 7b  sor **ppCursor){
385aa 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
385ab 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 52 74 72 65  TE_NOMEM;.  Rtre
385ac 65 43 75 72 73 6f 72 20 2a 70 43 73 72 3b 0a 0a  eCursor *pCsr;..
385ad 20 20 70 43 73 72 20 3d 20 28 52 74 72 65 65 43    pCsr = (RtreeC
385ae 75 72 73 6f 72 20 2a 29 73 71 6c 69 74 65 33 5f  ursor *)sqlite3_
385af 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 52 74  malloc(sizeof(Rt
385b0 72 65 65 43 75 72 73 6f 72 29 29 3b 0a 20 20 69  reeCursor));.  i
385b1 66 28 20 70 43 73 72 20 29 7b 0a 20 20 20 20 6d  f( pCsr ){.    m
385b2 65 6d 73 65 74 28 70 43 73 72 2c 20 30 2c 20 73  emset(pCsr, 0, s
385b3 69 7a 65 6f 66 28 52 74 72 65 65 43 75 72 73 6f  izeof(RtreeCurso
385b4 72 29 29 3b 0a 20 20 20 20 70 43 73 72 2d 3e 62  r));.    pCsr->b
385b5 61 73 65 2e 70 56 74 61 62 20 3d 20 70 56 54 61  ase.pVtab = pVTa
385b6 62 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  b;.    rc = SQLI
385b7 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 2a 70 70  TE_OK;.  }.  *pp
385b8 43 75 72 73 6f 72 20 3d 20 28 73 71 6c 69 74 65  Cursor = (sqlite
385b9 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 29  3_vtab_cursor *)
385ba 70 43 73 72 3b 0a 0a 20 20 72 65 74 75 72 6e 20  pCsr;..  return 
385bb 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74  rc;.}../* .** Rt
385bc 72 65 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  ree virtual tabl
385bd 65 20 6d 6f 64 75 6c 65 20 78 43 6c 6f 73 65 20  e module xClose 
385be 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69  method..*/.stati
385bf 63 20 69 6e 74 20 72 74 72 65 65 43 6c 6f 73 65  c int rtreeClose
385c0 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75  (sqlite3_vtab_cu
385c1 72 73 6f 72 20 2a 63 75 72 29 7b 0a 20 20 52 74  rsor *cur){.  Rt
385c2 72 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28 52  ree *pRtree = (R
385c3 74 72 65 65 20 2a 29 28 63 75 72 2d 3e 70 56 74  tree *)(cur->pVt
385c4 61 62 29 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  ab);.  int rc;. 
385c5 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43   RtreeCursor *pC
385c6 73 72 20 3d 20 28 52 74 72 65 65 43 75 72 73 6f  sr = (RtreeCurso
385c7 72 20 2a 29 63 75 72 3b 0a 20 20 73 71 6c 69 74  r *)cur;.  sqlit
385c8 65 33 5f 66 72 65 65 28 70 43 73 72 2d 3e 61 43  e3_free(pCsr->aC
385c9 6f 6e 73 74 72 61 69 6e 74 29 3b 0a 20 20 72 63  onstraint);.  rc
385ca 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70   = nodeRelease(p
385cb 52 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 4e 6f  Rtree, pCsr->pNo
385cc 64 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  de);.  sqlite3_f
385cd 72 65 65 28 70 43 73 72 29 3b 0a 20 20 72 65 74  ree(pCsr);.  ret
385ce 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
385cf 20 52 74 72 65 65 20 76 69 72 74 75 61 6c 20 74   Rtree virtual t
385d0 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 45 6f 66  able module xEof
385d1 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 52   method..**.** R
385d2 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69  eturn non-zero i
385d3 66 20 74 68 65 20 63 75 72 73 6f 72 20 64 6f 65  f the cursor doe
385d4 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20  s not currently 
385d5 70 6f 69 6e 74 20 74 6f 20 61 20 76 61 6c 69 64  point to a valid
385d6 20 0a 2a 2a 20 72 65 63 6f 72 64 20 28 69 2e 65   .** record (i.e
385d7 20 69 66 20 74 68 65 20 73 63 61 6e 20 68 61 73   if the scan has
385d8 20 66 69 6e 69 73 68 65 64 29 2c 20 6f 72 20 7a   finished), or z
385d9 65 72 6f 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a  ero otherwise..*
385da 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72  /.static int rtr
385db 65 65 45 6f 66 28 73 71 6c 69 74 65 33 5f 76 74  eeEof(sqlite3_vt
385dc 61 62 5f 63 75 72 73 6f 72 20 2a 63 75 72 29 7b  ab_cursor *cur){
385dd 0a 20 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a  .  RtreeCursor *
385de 70 43 73 72 20 3d 20 28 52 74 72 65 65 43 75 72  pCsr = (RtreeCur
385df 73 6f 72 20 2a 29 63 75 72 3b 0a 20 20 72 65 74  sor *)cur;.  ret
385e0 75 72 6e 20 28 70 43 73 72 2d 3e 70 4e 6f 64 65  urn (pCsr->pNode
385e1 3d 3d 30 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20  ==0);.}../* .** 
385e2 43 75 72 73 6f 72 20 70 43 75 72 73 6f 72 20 63  Cursor pCursor c
385e3 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 73 20  urrently points 
385e4 74 6f 20 61 20 63 65 6c 6c 20 69 6e 20 61 20 6e  to a cell in a n
385e5 6f 6e 2d 6c 65 61 66 20 70 61 67 65 2e 0a 2a 2a  on-leaf page..**
385e6 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   Return true if 
385e7 74 68 65 20 73 75 62 2d 74 72 65 65 20 68 65 61  the sub-tree hea
385e8 64 65 64 20 62 79 20 74 68 65 20 63 65 6c 6c 20  ded by the cell 
385e9 69 73 20 66 69 6c 74 65 72 65 64 0a 2a 2a 20 28  is filtered.** (
385ea 65 78 63 6c 75 64 65 64 29 20 62 79 20 74 68 65  excluded) by the
385eb 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 69 6e 20   constraints in 
385ec 74 68 65 20 70 43 75 72 73 6f 72 2d 3e 61 43 6f  the pCursor->aCo
385ed 6e 73 74 72 61 69 6e 74 5b 5d 20 0a 2a 2a 20 61  nstraint[] .** a
385ee 72 72 61 79 2c 20 6f 72 20 66 61 6c 73 65 20 6f  rray, or false o
385ef 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 74 61  therwise..*/.sta
385f0 74 69 63 20 69 6e 74 20 74 65 73 74 52 74 72 65  tic int testRtre
385f1 65 43 65 6c 6c 28 52 74 72 65 65 20 2a 70 52 74  eCell(Rtree *pRt
385f2 72 65 65 2c 20 52 74 72 65 65 43 75 72 73 6f 72  ree, RtreeCursor
385f3 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20 52 74   *pCursor){.  Rt
385f4 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20  reeCell cell;.  
385f5 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 62 52  int ii;.  int bR
385f6 65 73 20 3d 20 30 3b 0a 0a 20 20 6e 6f 64 65 47  es = 0;..  nodeG
385f7 65 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  etCell(pRtree, p
385f8 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 2c 20 70  Cursor->pNode, p
385f9 43 75 72 73 6f 72 2d 3e 69 43 65 6c 6c 2c 20 26  Cursor->iCell, &
385fa 63 65 6c 6c 29 3b 0a 20 20 66 6f 72 28 69 69 3d  cell);.  for(ii=
385fb 30 3b 20 62 52 65 73 3d 3d 30 20 26 26 20 69 69  0; bRes==0 && ii
385fc 3c 70 43 75 72 73 6f 72 2d 3e 6e 43 6f 6e 73 74  <pCursor->nConst
385fd 72 61 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20  raint; ii++){.  
385fe 20 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69 6e    RtreeConstrain
385ff 74 20 2a 70 20 3d 20 26 70 43 75 72 73 6f 72 2d  t *p = &pCursor-
38600 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 69 5d  >aConstraint[ii]
38601 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 63 65 6c  ;.    double cel
38602 6c 5f 6d 69 6e 20 3d 20 44 43 4f 4f 52 44 28 63  l_min = DCOORD(c
38603 65 6c 6c 2e 61 43 6f 6f 72 64 5b 28 70 2d 3e 69  ell.aCoord[(p->i
38604 43 6f 6f 72 64 3e 3e 31 29 2a 32 5d 29 3b 0a 20  Coord>>1)*2]);. 
38605 20 20 20 64 6f 75 62 6c 65 20 63 65 6c 6c 5f 6d     double cell_m
38606 61 78 20 3d 20 44 43 4f 4f 52 44 28 63 65 6c 6c  ax = DCOORD(cell
38607 2e 61 43 6f 6f 72 64 5b 28 70 2d 3e 69 43 6f 6f  .aCoord[(p->iCoo
38608 72 64 3e 3e 31 29 2a 32 2b 31 5d 29 3b 0a 0a 20  rd>>1)*2+1]);.. 
38609 20 20 20 61 73 73 65 72 74 28 70 2d 3e 6f 70 3d     assert(p->op=
3860a 3d 52 54 52 45 45 5f 4c 45 20 7c 7c 20 70 2d 3e  =RTREE_LE || p->
3860b 6f 70 3d 3d 52 54 52 45 45 5f 4c 54 20 7c 7c 20  op==RTREE_LT || 
3860c 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f 47 45 20  p->op==RTREE_GE 
3860d 0a 20 20 20 20 20 20 20 20 7c 7c 20 70 2d 3e 6f  .        || p->o
3860e 70 3d 3d 52 54 52 45 45 5f 47 54 20 7c 7c 20 70  p==RTREE_GT || p
3860f 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f 45 51 0a 20  ->op==RTREE_EQ. 
38610 20 20 20 29 3b 0a 0a 20 20 20 20 73 77 69 74 63     );..    switc
38611 68 28 20 70 2d 3e 6f 70 20 29 7b 0a 20 20 20 20  h( p->op ){.    
38612 20 20 63 61 73 65 20 52 54 52 45 45 5f 4c 45 3a    case RTREE_LE:
38613 20 63 61 73 65 20 52 54 52 45 45 5f 4c 54 3a 20   case RTREE_LT: 
38614 62 52 65 73 20 3d 20 70 2d 3e 72 56 61 6c 75 65  bRes = p->rValue
38615 3c 63 65 6c 6c 5f 6d 69 6e 3b 20 62 72 65 61 6b  <cell_min; break
38616 3b 0a 20 20 20 20 20 20 63 61 73 65 20 52 54 52  ;.      case RTR
38617 45 45 5f 47 45 3a 20 63 61 73 65 20 52 54 52 45  EE_GE: case RTRE
38618 45 5f 47 54 3a 20 62 52 65 73 20 3d 20 70 2d 3e  E_GT: bRes = p->
38619 72 56 61 6c 75 65 3e 63 65 6c 6c 5f 6d 61 78 3b  rValue>cell_max;
3861a 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3861b 73 65 20 52 54 52 45 45 5f 45 51 3a 20 0a 20 20  se RTREE_EQ: .  
3861c 20 20 20 20 20 20 62 52 65 73 20 3d 20 28 70 2d        bRes = (p-
3861d 3e 72 56 61 6c 75 65 3e 63 65 6c 6c 5f 6d 61 78  >rValue>cell_max
3861e 20 7c 7c 20 70 2d 3e 72 56 61 6c 75 65 3c 63 65   || p->rValue<ce
3861f 6c 6c 5f 6d 69 6e 29 3b 0a 20 20 20 20 20 20 20  ll_min);.       
38620 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
38621 7d 0a 0a 20 20 72 65 74 75 72 6e 20 62 52 65 73  }..  return bRes
38622 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75  ;.}../* .** Retu
38623 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 63  rn true if the c
38624 65 6c 6c 20 74 68 61 74 20 63 75 72 73 6f 72 20  ell that cursor 
38625 70 43 75 72 73 6f 72 20 63 75 72 72 65 6e 74 6c  pCursor currentl
38626 79 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 77  y points to.** w
38627 6f 75 6c 64 20 62 65 20 66 69 6c 74 65 72 65 64  ould be filtered
38628 20 28 65 78 63 6c 75 64 65 64 29 20 62 79 20 74   (excluded) by t
38629 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 69  he constraints i
3862a 6e 20 74 68 65 20 0a 2a 2a 20 70 43 75 72 73 6f  n the .** pCurso
3862b 72 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d  r->aConstraint[]
3862c 20 61 72 72 61 79 2c 20 6f 72 20 66 61 6c 73 65   array, or false
3862d 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a   otherwise..**.*
3862e 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
3862f 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65  assumes that the
38630 20 63 65 6c 6c 20 69 73 20 70 61 72 74 20 6f 66   cell is part of
38631 20 61 20 6c 65 61 66 20 6e 6f 64 65 2e 0a 2a 2f   a leaf node..*/
38632 0a 73 74 61 74 69 63 20 69 6e 74 20 74 65 73 74  .static int test
38633 52 74 72 65 65 45 6e 74 72 79 28 52 74 72 65 65  RtreeEntry(Rtree
38634 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65 43   *pRtree, RtreeC
38635 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b  ursor *pCursor){
38636 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 63 65 6c  .  RtreeCell cel
38637 6c 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 0a 20 20  l;.  int ii;..  
38638 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52 74 72  nodeGetCell(pRtr
38639 65 65 2c 20 70 43 75 72 73 6f 72 2d 3e 70 4e 6f  ee, pCursor->pNo
3863a 64 65 2c 20 70 43 75 72 73 6f 72 2d 3e 69 43 65  de, pCursor->iCe
3863b 6c 6c 2c 20 26 63 65 6c 6c 29 3b 0a 20 20 66 6f  ll, &cell);.  fo
3863c 72 28 69 69 3d 30 3b 20 69 69 3c 70 43 75 72 73  r(ii=0; ii<pCurs
3863d 6f 72 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b  or->nConstraint;
3863e 20 69 69 2b 2b 29 7b 0a 20 20 20 20 52 74 72 65   ii++){.    Rtre
3863f 65 43 6f 6e 73 74 72 61 69 6e 74 20 2a 70 20 3d  eConstraint *p =
38640 20 26 70 43 75 72 73 6f 72 2d 3e 61 43 6f 6e 73   &pCursor->aCons
38641 74 72 61 69 6e 74 5b 69 69 5d 3b 0a 20 20 20 20  traint[ii];.    
38642 64 6f 75 62 6c 65 20 63 6f 6f 72 64 20 3d 20 44  double coord = D
38643 43 4f 4f 52 44 28 63 65 6c 6c 2e 61 43 6f 6f 72  COORD(cell.aCoor
38644 64 5b 70 2d 3e 69 43 6f 6f 72 64 5d 29 3b 0a 20  d[p->iCoord]);. 
38645 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20     int res;.    
38646 61 73 73 65 72 74 28 70 2d 3e 6f 70 3d 3d 52 54  assert(p->op==RT
38647 52 45 45 5f 4c 45 20 7c 7c 20 70 2d 3e 6f 70 3d  REE_LE || p->op=
38648 3d 52 54 52 45 45 5f 4c 54 20 7c 7c 20 70 2d 3e  =RTREE_LT || p->
38649 6f 70 3d 3d 52 54 52 45 45 5f 47 45 20 0a 20 20  op==RTREE_GE .  
3864a 20 20 20 20 20 20 7c 7c 20 70 2d 3e 6f 70 3d 3d        || p->op==
3864b 52 54 52 45 45 5f 47 54 20 7c 7c 20 70 2d 3e 6f  RTREE_GT || p->o
3864c 70 3d 3d 52 54 52 45 45 5f 45 51 0a 20 20 20 20  p==RTREE_EQ.    
3864d 29 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20 70  );.    switch( p
3864e 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 20 20 63 61  ->op ){.      ca
3864f 73 65 20 52 54 52 45 45 5f 4c 45 3a 20 72 65 73  se RTREE_LE: res
38650 20 3d 20 28 63 6f 6f 72 64 3c 3d 70 2d 3e 72 56   = (coord<=p->rV
38651 61 6c 75 65 29 3b 20 62 72 65 61 6b 3b 0a 20 20  alue); break;.  
38652 20 20 20 20 63 61 73 65 20 52 54 52 45 45 5f 4c      case RTREE_L
38653 54 3a 20 72 65 73 20 3d 20 28 63 6f 6f 72 64 3c  T: res = (coord<
38654 70 2d 3e 72 56 61 6c 75 65 29 3b 20 20 62 72 65  p->rValue);  bre
38655 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 52  ak;.      case R
38656 54 52 45 45 5f 47 45 3a 20 72 65 73 20 3d 20 28  TREE_GE: res = (
38657 63 6f 6f 72 64 3e 3d 70 2d 3e 72 56 61 6c 75 65  coord>=p->rValue
38658 29 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20  ); break;.      
38659 63 61 73 65 20 52 54 52 45 45 5f 47 54 3a 20 72  case RTREE_GT: r
3865a 65 73 20 3d 20 28 63 6f 6f 72 64 3e 70 2d 3e 72  es = (coord>p->r
3865b 56 61 6c 75 65 29 3b 20 20 62 72 65 61 6b 3b 0a  Value);  break;.
3865c 20 20 20 20 20 20 63 61 73 65 20 52 54 52 45 45        case RTREE
3865d 5f 45 51 3a 20 72 65 73 20 3d 20 28 63 6f 6f 72  _EQ: res = (coor
3865e 64 3d 3d 70 2d 3e 72 56 61 6c 75 65 29 3b 20 62  d==p->rValue); b
3865f 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  reak;.    }..   
38660 20 69 66 28 20 21 72 65 73 20 29 20 72 65 74 75   if( !res ) retu
38661 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  rn 1;.  }..  ret
38662 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
38663 43 75 72 73 6f 72 20 70 43 75 72 73 6f 72 20 63  Cursor pCursor c
38664 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 73 20  urrently points 
38665 61 74 20 61 20 6e 6f 64 65 20 74 68 61 74 20 68  at a node that h
38666 65 61 64 73 20 61 20 73 75 62 2d 74 72 65 65 20  eads a sub-tree 
38667 6f 66 0a 2a 2a 20 68 65 69 67 68 74 20 69 48 65  of.** height iHe
38668 69 67 68 74 20 28 69 66 20 69 48 65 69 67 68 74  ight (if iHeight
38669 3d 3d 30 2c 20 74 68 65 6e 20 74 68 65 20 6e 6f  ==0, then the no
3866a 64 65 20 69 73 20 61 20 6c 65 61 66 29 2e 20 44  de is a leaf). D
3866b 65 73 63 65 6e 64 0a 2a 2a 20 74 6f 20 70 6f 69  escend.** to poi
3866c 6e 74 20 74 6f 20 74 68 65 20 6c 65 66 74 2d 6d  nt to the left-m
3866d 6f 73 74 20 63 65 6c 6c 20 6f 66 20 74 68 65 20  ost cell of the 
3866e 73 75 62 2d 74 72 65 65 20 74 68 61 74 20 6d 61  sub-tree that ma
3866f 74 63 68 65 73 20 74 68 65 20 0a 2a 2a 20 63 6f  tches the .** co
38670 6e 66 69 67 75 72 65 64 20 63 6f 6e 73 74 72 61  nfigured constra
38671 69 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ints..*/.static 
38672 69 6e 74 20 64 65 73 63 65 6e 64 54 6f 43 65 6c  int descendToCel
38673 6c 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  l(.  Rtree *pRtr
38674 65 65 2c 20 0a 20 20 52 74 72 65 65 43 75 72 73  ee, .  RtreeCurs
38675 6f 72 20 2a 70 43 75 72 73 6f 72 2c 20 0a 20 20  or *pCursor, .  
38676 69 6e 74 20 69 48 65 69 67 68 74 2c 0a 20 20 69  int iHeight,.  i
38677 6e 74 20 2a 70 45 6f 66 20 20 20 20 20 20 20 20  nt *pEof        
38678 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
38679 20 53 65 74 20 74 6f 20 74 72 75 65 20 69 66 20   Set to true if 
3867a 63 61 6e 6e 6f 74 20 64 65 73 63 65 6e 64 20 2a  cannot descend *
3867b 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 73 45 6f 66  /.){.  int isEof
3867c 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e  ;.  int rc;.  in
3867d 74 20 69 69 3b 0a 20 20 52 74 72 65 65 4e 6f 64  t ii;.  RtreeNod
3867e 65 20 2a 70 43 68 69 6c 64 3b 0a 20 20 73 71 6c  e *pChild;.  sql
3867f 69 74 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 69  ite3_int64 iRowi
38680 64 3b 0a 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  d;..  RtreeNode 
38681 2a 70 53 61 76 65 64 4e 6f 64 65 20 3d 20 70 43  *pSavedNode = pC
38682 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 3b 0a 20 20  ursor->pNode;.  
38683 69 6e 74 20 69 53 61 76 65 64 43 65 6c 6c 20 3d  int iSavedCell =
38684 20 70 43 75 72 73 6f 72 2d 3e 69 43 65 6c 6c 3b   pCursor->iCell;
38685 0a 0a 20 20 61 73 73 65 72 74 28 20 69 48 65 69  ..  assert( iHei
38686 67 68 74 3e 3d 30 20 29 3b 0a 0a 20 20 69 66 28  ght>=0 );..  if(
38687 20 69 48 65 69 67 68 74 3d 3d 30 20 29 7b 0a 20   iHeight==0 ){. 
38688 20 20 20 69 73 45 6f 66 20 3d 20 74 65 73 74 52     isEof = testR
38689 74 72 65 65 45 6e 74 72 79 28 70 52 74 72 65 65  treeEntry(pRtree
3868a 2c 20 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d 65  , pCursor);.  }e
3868b 6c 73 65 7b 0a 20 20 20 20 69 73 45 6f 66 20 3d  lse{.    isEof =
3868c 20 74 65 73 74 52 74 72 65 65 43 65 6c 6c 28 70   testRtreeCell(p
3868d 52 74 72 65 65 2c 20 70 43 75 72 73 6f 72 29 3b  Rtree, pCursor);
3868e 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 45 6f 66  .  }.  if( isEof
3868f 20 7c 7c 20 69 48 65 69 67 68 74 3d 3d 30 20 29   || iHeight==0 )
38690 7b 0a 20 20 20 20 2a 70 45 6f 66 20 3d 20 69 73  {.    *pEof = is
38691 45 6f 66 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  Eof;.    return 
38692 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a  SQLITE_OK;.  }..
38693 20 20 69 52 6f 77 69 64 20 3d 20 6e 6f 64 65 47    iRowid = nodeG
38694 65 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c 20  etRowid(pRtree, 
38695 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 2c 20  pCursor->pNode, 
38696 70 43 75 72 73 6f 72 2d 3e 69 43 65 6c 6c 29 3b  pCursor->iCell);
38697 0a 20 20 72 63 20 3d 20 6e 6f 64 65 41 63 71 75  .  rc = nodeAcqu
38698 69 72 65 28 70 52 74 72 65 65 2c 20 69 52 6f 77  ire(pRtree, iRow
38699 69 64 2c 20 70 43 75 72 73 6f 72 2d 3e 70 4e 6f  id, pCursor->pNo
3869a 64 65 2c 20 26 70 43 68 69 6c 64 29 3b 0a 20 20  de, &pChild);.  
3869b 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
3869c 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  K ){.    return 
3869d 72 63 3b 0a 20 20 7d 0a 0a 20 20 6e 6f 64 65 52  rc;.  }..  nodeR
3869e 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70  elease(pRtree, p
3869f 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 29 3b 0a  Cursor->pNode);.
386a0 20 20 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65    pCursor->pNode
386a1 20 3d 20 70 43 68 69 6c 64 3b 0a 20 20 69 73 45   = pChild;.  isE
386a2 6f 66 20 3d 20 31 3b 0a 20 20 66 6f 72 28 69 69  of = 1;.  for(ii
386a3 3d 30 3b 20 69 73 45 6f 66 20 26 26 20 69 69 3c  =0; isEof && ii<
386a4 4e 43 45 4c 4c 28 70 43 68 69 6c 64 29 3b 20 69  NCELL(pChild); i
386a5 69 2b 2b 29 7b 0a 20 20 20 20 70 43 75 72 73 6f  i++){.    pCurso
386a6 72 2d 3e 69 43 65 6c 6c 20 3d 20 69 69 3b 0a 20  r->iCell = ii;. 
386a7 20 20 20 72 63 20 3d 20 64 65 73 63 65 6e 64 54     rc = descendT
386a8 6f 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 43  oCell(pRtree, pC
386a9 75 72 73 6f 72 2c 20 69 48 65 69 67 68 74 2d 31  ursor, iHeight-1
386aa 2c 20 26 69 73 45 6f 66 29 3b 0a 20 20 20 20 69  , &isEof);.    i
386ab 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
386ac 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
386ad 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a   rc;.    }.  }..
386ae 20 20 69 66 28 20 69 73 45 6f 66 20 29 7b 0a 20    if( isEof ){. 
386af 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 73     assert( pCurs
386b0 6f 72 2d 3e 70 4e 6f 64 65 3d 3d 70 43 68 69 6c  or->pNode==pChil
386b1 64 20 29 3b 0a 20 20 20 20 6e 6f 64 65 52 65 66  d );.    nodeRef
386b2 65 72 65 6e 63 65 28 70 53 61 76 65 64 4e 6f 64  erence(pSavedNod
386b3 65 29 3b 0a 20 20 20 20 6e 6f 64 65 52 65 6c 65  e);.    nodeRele
386b4 61 73 65 28 70 52 74 72 65 65 2c 20 70 43 68 69  ase(pRtree, pChi
386b5 6c 64 29 3b 0a 20 20 20 20 70 43 75 72 73 6f 72  ld);.    pCursor
386b6 2d 3e 70 4e 6f 64 65 20 3d 20 70 53 61 76 65 64  ->pNode = pSaved
386b7 4e 6f 64 65 3b 0a 20 20 20 20 70 43 75 72 73 6f  Node;.    pCurso
386b8 72 2d 3e 69 43 65 6c 6c 20 3d 20 69 53 61 76 65  r->iCell = iSave
386b9 64 43 65 6c 6c 3b 0a 20 20 7d 0a 0a 20 20 2a 70  dCell;.  }..  *p
386ba 45 6f 66 20 3d 20 69 73 45 6f 66 3b 0a 20 20 72  Eof = isEof;.  r
386bb 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
386bc 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 65 20 6f 66  .}../*.** One of
386bd 20 74 68 65 20 63 65 6c 6c 73 20 69 6e 20 6e 6f   the cells in no
386be 64 65 20 70 4e 6f 64 65 20 69 73 20 67 75 61 72  de pNode is guar
386bf 61 6e 74 65 65 64 20 74 6f 20 68 61 76 65 20 61  anteed to have a
386c0 20 36 34 2d 62 69 74 20 0a 2a 2a 20 69 6e 74 65   64-bit .** inte
386c1 67 65 72 20 76 61 6c 75 65 20 65 71 75 61 6c 20  ger value equal 
386c2 74 6f 20 69 52 6f 77 69 64 2e 20 52 65 74 75 72  to iRowid. Retur
386c3 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74  n the index of t
386c4 68 69 73 20 63 65 6c 6c 2e 0a 2a 2f 0a 73 74 61  his cell..*/.sta
386c5 74 69 63 20 69 6e 74 20 6e 6f 64 65 52 6f 77 69  tic int nodeRowi
386c6 64 49 6e 64 65 78 28 52 74 72 65 65 20 2a 70 52  dIndex(Rtree *pR
386c7 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20  tree, RtreeNode 
386c8 2a 70 4e 6f 64 65 2c 20 69 36 34 20 69 52 6f 77  *pNode, i64 iRow
386c9 69 64 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20  id){.  int ii;. 
386ca 20 66 6f 72 28 69 69 3d 30 3b 20 6e 6f 64 65 47   for(ii=0; nodeG
386cb 65 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c 20  etRowid(pRtree, 
386cc 70 4e 6f 64 65 2c 20 69 69 29 21 3d 69 52 6f 77  pNode, ii)!=iRow
386cd 69 64 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 61  id; ii++){.    a
386ce 73 73 65 72 74 28 20 69 69 3c 28 4e 43 45 4c 4c  ssert( ii<(NCELL
386cf 28 70 4e 6f 64 65 29 2d 31 29 20 29 3b 0a 20 20  (pNode)-1) );.  
386d0 7d 0a 20 20 72 65 74 75 72 6e 20 69 69 3b 0a 7d  }.  return ii;.}
386d1 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
386d2 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
386d3 63 65 6c 6c 20 63 6f 6e 74 61 69 6e 69 6e 67 20  cell containing 
386d4 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 6e 6f 64  a pointer to nod
386d5 65 20 70 4e 6f 64 65 0a 2a 2a 20 69 6e 20 69 74  e pNode.** in it
386d6 73 20 70 61 72 65 6e 74 2e 20 49 66 20 70 4e 6f  s parent. If pNo
386d7 64 65 20 69 73 20 74 68 65 20 72 6f 6f 74 20 6e  de is the root n
386d8 6f 64 65 2c 20 72 65 74 75 72 6e 20 2d 31 2e 0a  ode, return -1..
386d9 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f  */.static int no
386da 64 65 50 61 72 65 6e 74 49 6e 64 65 78 28 52 74  deParentIndex(Rt
386db 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72  ree *pRtree, Rtr
386dc 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0a  eeNode *pNode){.
386dd 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 50 61    RtreeNode *pPa
386de 72 65 6e 74 20 3d 20 70 4e 6f 64 65 2d 3e 70 50  rent = pNode->pP
386df 61 72 65 6e 74 3b 0a 20 20 69 66 28 20 70 50 61  arent;.  if( pPa
386e0 72 65 6e 74 20 29 7b 0a 20 20 20 20 72 65 74 75  rent ){.    retu
386e1 72 6e 20 6e 6f 64 65 52 6f 77 69 64 49 6e 64 65  rn nodeRowidInde
386e2 78 28 70 52 74 72 65 65 2c 20 70 50 61 72 65 6e  x(pRtree, pParen
386e3 74 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29  t, pNode->iNode)
386e4 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2d  ;.  }.  return -
386e5 31 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72  1;.}../* .** Rtr
386e6 65 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ee virtual table
386e7 20 6d 6f 64 75 6c 65 20 78 4e 65 78 74 20 6d 65   module xNext me
386e8 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  thod..*/.static 
386e9 69 6e 74 20 72 74 72 65 65 4e 65 78 74 28 73 71  int rtreeNext(sq
386ea 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
386eb 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 29 7b  r *pVtabCursor){
386ec 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
386ed 20 3d 20 28 52 74 72 65 65 20 2a 29 28 70 56 74   = (Rtree *)(pVt
386ee 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 29  abCursor->pVtab)
386ef 3b 0a 20 20 52 74 72 65 65 43 75 72 73 6f 72 20  ;.  RtreeCursor 
386f0 2a 70 43 73 72 20 3d 20 28 52 74 72 65 65 43 75  *pCsr = (RtreeCu
386f1 72 73 6f 72 20 2a 29 70 56 74 61 62 43 75 72 73  rsor *)pVtabCurs
386f2 6f 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  or;.  int rc = S
386f3 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66 28  QLITE_OK;..  if(
386f4 20 70 43 73 72 2d 3e 69 53 74 72 61 74 65 67 79   pCsr->iStrategy
386f5 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  ==1 ){.    /* Th
386f6 69 73 20 22 73 63 61 6e 22 20 69 73 20 61 20 64  is "scan" is a d
386f7 69 72 65 63 74 20 6c 6f 6f 6b 75 70 20 62 79 20  irect lookup by 
386f8 72 6f 77 69 64 2e 20 54 68 65 72 65 20 69 73 20  rowid. There is 
386f9 6e 6f 20 6e 65 78 74 20 65 6e 74 72 79 2e 20 2a  no next entry. *
386fa 2f 0a 20 20 20 20 6e 6f 64 65 52 65 6c 65 61 73  /.    nodeReleas
386fb 65 28 70 52 74 72 65 65 2c 20 70 43 73 72 2d 3e  e(pRtree, pCsr->
386fc 70 4e 6f 64 65 29 3b 0a 20 20 20 20 70 43 73 72  pNode);.    pCsr
386fd 2d 3e 70 4e 6f 64 65 20 3d 20 30 3b 0a 20 20 7d  ->pNode = 0;.  }
386fe 0a 0a 20 20 65 6c 73 65 20 69 66 28 20 70 43 73  ..  else if( pCs
386ff 72 2d 3e 70 4e 6f 64 65 20 29 7b 0a 20 20 20 20  r->pNode ){.    
38700 2f 2a 20 4d 6f 76 65 20 74 6f 20 74 68 65 20 6e  /* Move to the n
38701 65 78 74 20 65 6e 74 72 79 20 74 68 61 74 20 6d  ext entry that m
38702 61 74 63 68 65 73 20 74 68 65 20 63 6f 6e 66 69  atches the confi
38703 67 75 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74  gured constraint
38704 73 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 48  s. */.    int iH
38705 65 69 67 68 74 20 3d 20 30 3b 0a 20 20 20 20 77  eight = 0;.    w
38706 68 69 6c 65 28 20 70 43 73 72 2d 3e 70 4e 6f 64  hile( pCsr->pNod
38707 65 20 29 7b 0a 20 20 20 20 20 20 52 74 72 65 65  e ){.      Rtree
38708 4e 6f 64 65 20 2a 70 4e 6f 64 65 20 3d 20 70 43  Node *pNode = pC
38709 73 72 2d 3e 70 4e 6f 64 65 3b 0a 20 20 20 20 20  sr->pNode;.     
3870a 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 4e 43 45   int nCell = NCE
3870b 4c 4c 28 70 4e 6f 64 65 29 3b 0a 20 20 20 20 20  LL(pNode);.     
3870c 20 66 6f 72 28 70 43 73 72 2d 3e 69 43 65 6c 6c   for(pCsr->iCell
3870d 2b 2b 3b 20 70 43 73 72 2d 3e 69 43 65 6c 6c 3c  ++; pCsr->iCell<
3870e 6e 43 65 6c 6c 3b 20 70 43 73 72 2d 3e 69 43 65  nCell; pCsr->iCe
3870f 6c 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69  ll++){.        i
38710 6e 74 20 69 73 45 6f 66 3b 0a 20 20 20 20 20 20  nt isEof;.      
38711 20 20 72 63 20 3d 20 64 65 73 63 65 6e 64 54 6f    rc = descendTo
38712 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 43 73  Cell(pRtree, pCs
38713 72 2c 20 69 48 65 69 67 68 74 2c 20 26 69 73 45  r, iHeight, &isE
38714 6f 66 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  of);.        if(
38715 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc!=SQLITE_OK |
38716 7c 20 21 69 73 45 6f 66 20 29 7b 0a 20 20 20 20  | !isEof ){.    
38717 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
38718 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
38719 20 7d 0a 20 20 20 20 20 20 70 43 73 72 2d 3e 70   }.      pCsr->p
3871a 4e 6f 64 65 20 3d 20 70 4e 6f 64 65 2d 3e 70 50  Node = pNode->pP
3871b 61 72 65 6e 74 3b 0a 20 20 20 20 20 20 70 43 73  arent;.      pCs
3871c 72 2d 3e 69 43 65 6c 6c 20 3d 20 6e 6f 64 65 50  r->iCell = nodeP
3871d 61 72 65 6e 74 49 6e 64 65 78 28 70 52 74 72 65  arentIndex(pRtre
3871e 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20 20  e, pNode);.     
3871f 20 6e 6f 64 65 52 65 66 65 72 65 6e 63 65 28 70   nodeReference(p
38720 43 73 72 2d 3e 70 4e 6f 64 65 29 3b 0a 20 20 20  Csr->pNode);.   
38721 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70     nodeRelease(p
38722 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20  Rtree, pNode);. 
38723 20 20 20 20 20 69 48 65 69 67 68 74 2b 2b 3b 0a       iHeight++;.
38724 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74      }.  }..  ret
38725 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a 2a  urn rc;.}../* .*
38726 2a 20 52 74 72 65 65 20 76 69 72 74 75 61 6c 20  * Rtree virtual 
38727 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 52 6f  table module xRo
38728 77 69 64 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73  wid method..*/.s
38729 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 52  tatic int rtreeR
3872a 6f 77 69 64 28 73 71 6c 69 74 65 33 5f 76 74 61  owid(sqlite3_vta
3872b 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43  b_cursor *pVtabC
3872c 75 72 73 6f 72 2c 20 73 71 6c 69 74 65 5f 69 6e  ursor, sqlite_in
3872d 74 36 34 20 2a 70 52 6f 77 69 64 29 7b 0a 20 20  t64 *pRowid){.  
3872e 52 74 72 65 65 20 2a 70 52 74 72 65 65 20 3d 20  Rtree *pRtree = 
3872f 28 52 74 72 65 65 20 2a 29 70 56 74 61 62 43 75  (Rtree *)pVtabCu
38730 72 73 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 20 52  rsor->pVtab;.  R
38731 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43 73 72  treeCursor *pCsr
38732 20 3d 20 28 52 74 72 65 65 43 75 72 73 6f 72 20   = (RtreeCursor 
38733 2a 29 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 0a  *)pVtabCursor;..
38734 20 20 61 73 73 65 72 74 28 70 43 73 72 2d 3e 70    assert(pCsr->p
38735 4e 6f 64 65 29 3b 0a 20 20 2a 70 52 6f 77 69 64  Node);.  *pRowid
38736 20 3d 20 6e 6f 64 65 47 65 74 52 6f 77 69 64 28   = nodeGetRowid(
38737 70 52 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 4e  pRtree, pCsr->pN
38738 6f 64 65 2c 20 70 43 73 72 2d 3e 69 43 65 6c 6c  ode, pCsr->iCell
38739 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  );..  return SQL
3873a 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 0a 2a  ITE_OK;.}../* .*
3873b 2a 20 52 74 72 65 65 20 76 69 72 74 75 61 6c 20  * Rtree virtual 
3873c 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 43 6f  table module xCo
3873d 6c 75 6d 6e 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a  lumn method..*/.
3873e 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65  static int rtree
3873f 43 6f 6c 75 6d 6e 28 73 71 6c 69 74 65 33 5f 76  Column(sqlite3_v
38740 74 61 62 5f 63 75 72 73 6f 72 20 2a 63 75 72 2c  tab_cursor *cur,
38741 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
38742 20 2a 63 74 78 2c 20 69 6e 74 20 69 29 7b 0a 20   *ctx, int i){. 
38743 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 20 3d   Rtree *pRtree =
38744 20 28 52 74 72 65 65 20 2a 29 63 75 72 2d 3e 70   (Rtree *)cur->p
38745 56 74 61 62 3b 0a 20 20 52 74 72 65 65 43 75 72  Vtab;.  RtreeCur
38746 73 6f 72 20 2a 70 43 73 72 20 3d 20 28 52 74 72  sor *pCsr = (Rtr
38747 65 65 43 75 72 73 6f 72 20 2a 29 63 75 72 3b 0a  eeCursor *)cur;.
38748 0a 20 20 69 66 28 20 69 3d 3d 30 20 29 7b 0a 20  .  if( i==0 ){. 
38749 20 20 20 69 36 34 20 69 52 6f 77 69 64 20 3d 20     i64 iRowid = 
3874a 6e 6f 64 65 47 65 74 52 6f 77 69 64 28 70 52 74  nodeGetRowid(pRt
3874b 72 65 65 2c 20 70 43 73 72 2d 3e 70 4e 6f 64 65  ree, pCsr->pNode
3874c 2c 20 70 43 73 72 2d 3e 69 43 65 6c 6c 29 3b 0a  , pCsr->iCell);.
3874d 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
3874e 6c 74 5f 69 6e 74 36 34 28 63 74 78 2c 20 69 52  lt_int64(ctx, iR
3874f 6f 77 69 64 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  owid);.  }else{.
38750 20 20 20 20 52 74 72 65 65 43 6f 6f 72 64 20 63      RtreeCoord c
38751 3b 0a 20 20 20 20 6e 6f 64 65 47 65 74 43 6f 6f  ;.    nodeGetCoo
38752 72 64 28 70 52 74 72 65 65 2c 20 70 43 73 72 2d  rd(pRtree, pCsr-
38753 3e 70 4e 6f 64 65 2c 20 70 43 73 72 2d 3e 69 43  >pNode, pCsr->iC
38754 65 6c 6c 2c 20 69 2d 31 2c 20 26 63 29 3b 0a 20  ell, i-1, &c);. 
38755 20 20 20 69 66 28 20 70 52 74 72 65 65 2d 3e 65     if( pRtree->e
38756 43 6f 6f 72 64 54 79 70 65 3d 3d 52 54 52 45 45  CoordType==RTREE
38757 5f 43 4f 4f 52 44 5f 52 45 41 4c 33 32 20 29 7b  _COORD_REAL32 ){
38758 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
38759 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 74 78  esult_double(ctx
3875a 2c 20 63 2e 66 29 3b 0a 20 20 20 20 7d 65 6c 73  , c.f);.    }els
3875b 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  e{.      assert(
3875c 20 70 52 74 72 65 65 2d 3e 65 43 6f 6f 72 64 54   pRtree->eCoordT
3875d 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f 4f 52 44  ype==RTREE_COORD
3875e 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 20 20 20  _INT32 );.      
3875f 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
38760 6e 74 28 63 74 78 2c 20 63 2e 69 29 3b 0a 20 20  nt(ctx, c.i);.  
38761 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72    }.  }..  retur
38762 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
38763 2f 2a 20 0a 2a 2a 20 55 73 65 20 6e 6f 64 65 41  /* .** Use nodeA
38764 63 71 75 69 72 65 28 29 20 74 6f 20 6f 62 74 61  cquire() to obta
38765 69 6e 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65  in the leaf node
38766 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
38767 72 65 63 6f 72 64 20 77 69 74 68 20 0a 2a 2a 20  record with .** 
38768 72 6f 77 69 64 20 69 52 6f 77 69 64 2e 20 49 66  rowid iRowid. If
38769 20 73 75 63 63 65 73 73 66 75 6c 2c 20 73 65 74   successful, set
3876a 20 2a 70 70 4c 65 61 66 20 74 6f 20 70 6f 69 6e   *ppLeaf to poin
3876b 74 20 74 6f 20 74 68 65 20 6e 6f 64 65 20 61 6e  t to the node an
3876c 64 0a 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c 49  d.** return SQLI
3876d 54 45 5f 4f 4b 2e 20 49 66 20 74 68 65 72 65 20  TE_OK. If there 
3876e 69 73 20 6e 6f 20 73 75 63 68 20 72 65 63 6f 72  is no such recor
3876f 64 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2c 20  d in the table, 
38770 73 65 74 0a 2a 2a 20 2a 70 70 4c 65 61 66 20 74  set.** *ppLeaf t
38771 6f 20 30 20 61 6e 64 20 72 65 74 75 72 6e 20 53  o 0 and return S
38772 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 61 6e 20  QLITE_OK. If an 
38773 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 73 65  error occurs, se
38774 74 20 2a 70 70 4c 65 61 66 0a 2a 2a 20 74 6f 20  t *ppLeaf.** to 
38775 7a 65 72 6f 20 61 6e 64 20 72 65 74 75 72 6e 20  zero and return 
38776 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20  an SQLite error 
38777 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  code..*/.static 
38778 69 6e 74 20 66 69 6e 64 4c 65 61 66 4e 6f 64 65  int findLeafNode
38779 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20  (Rtree *pRtree, 
3877a 69 36 34 20 69 52 6f 77 69 64 2c 20 52 74 72 65  i64 iRowid, Rtre
3877b 65 4e 6f 64 65 20 2a 2a 70 70 4c 65 61 66 29 7b  eNode **ppLeaf){
3877c 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 2a 70 70  .  int rc;.  *pp
3877d 4c 65 61 66 20 3d 20 30 3b 0a 20 20 73 71 6c 69  Leaf = 0;.  sqli
3877e 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70  te3_bind_int64(p
3877f 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77 69  Rtree->pReadRowi
38780 64 2c 20 31 2c 20 69 52 6f 77 69 64 29 3b 0a 20  d, 1, iRowid);. 
38781 20 69 66 28 20 73 71 6c 69 74 65 33 5f 73 74 65   if( sqlite3_ste
38782 70 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52  p(pRtree->pReadR
38783 6f 77 69 64 29 3d 3d 53 51 4c 49 54 45 5f 52 4f  owid)==SQLITE_RO
38784 57 20 29 7b 0a 20 20 20 20 69 36 34 20 69 4e 6f  W ){.    i64 iNo
38785 64 65 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  de = sqlite3_col
38786 75 6d 6e 5f 69 6e 74 36 34 28 70 52 74 72 65 65  umn_int64(pRtree
38787 2d 3e 70 52 65 61 64 52 6f 77 69 64 2c 20 30 29  ->pReadRowid, 0)
38788 3b 0a 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 41  ;.    rc = nodeA
38789 63 71 75 69 72 65 28 70 52 74 72 65 65 2c 20 69  cquire(pRtree, i
3878a 4e 6f 64 65 2c 20 30 2c 20 70 70 4c 65 61 66 29  Node, 0, ppLeaf)
3878b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ;.    sqlite3_re
3878c 73 65 74 28 70 52 74 72 65 65 2d 3e 70 52 65 61  set(pRtree->pRea
3878d 64 52 6f 77 69 64 29 3b 0a 20 20 7d 65 6c 73 65  dRowid);.  }else
3878e 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
3878f 65 33 5f 72 65 73 65 74 28 70 52 74 72 65 65 2d  e3_reset(pRtree-
38790 3e 70 52 65 61 64 52 6f 77 69 64 29 3b 0a 20 20  >pReadRowid);.  
38791 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
38792 0a 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20  .../* .** Rtree 
38793 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
38794 64 75 6c 65 20 78 46 69 6c 74 65 72 20 6d 65 74  dule xFilter met
38795 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  hod..*/.static i
38796 6e 74 20 72 74 72 65 65 46 69 6c 74 65 72 28 0a  nt rtreeFilter(.
38797 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63    sqlite3_vtab_c
38798 75 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72 73  ursor *pVtabCurs
38799 6f 72 2c 20 0a 20 20 69 6e 74 20 69 64 78 4e 75  or, .  int idxNu
3879a 6d 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69  m, const char *i
3879b 64 78 53 74 72 2c 0a 20 20 69 6e 74 20 61 72 67  dxStr,.  int arg
3879c 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  c, sqlite3_value
3879d 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 52 74 72   **argv.){.  Rtr
3879e 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28 52 74  ee *pRtree = (Rt
3879f 72 65 65 20 2a 29 70 56 74 61 62 43 75 72 73 6f  ree *)pVtabCurso
387a0 72 2d 3e 70 56 74 61 62 3b 0a 20 20 52 74 72 65  r->pVtab;.  Rtre
387a1 65 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d 20  eCursor *pCsr = 
387a2 28 52 74 72 65 65 43 75 72 73 6f 72 20 2a 29 70  (RtreeCursor *)p
387a3 56 74 61 62 43 75 72 73 6f 72 3b 0a 0a 20 20 52  VtabCursor;..  R
387a4 74 72 65 65 4e 6f 64 65 20 2a 70 52 6f 6f 74 20  treeNode *pRoot 
387a5 3d 20 30 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 20  = 0;.  int ii;. 
387a6 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
387a7 5f 4f 4b 3b 0a 0a 20 20 72 74 72 65 65 52 65 66  _OK;..  rtreeRef
387a8 65 72 65 6e 63 65 28 70 52 74 72 65 65 29 3b 0a  erence(pRtree);.
387a9 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
387aa 70 43 73 72 2d 3e 61 43 6f 6e 73 74 72 61 69 6e  pCsr->aConstrain
387ab 74 29 3b 0a 20 20 70 43 73 72 2d 3e 61 43 6f 6e  t);.  pCsr->aCon
387ac 73 74 72 61 69 6e 74 20 3d 20 30 3b 0a 20 20 70  straint = 0;.  p
387ad 43 73 72 2d 3e 69 53 74 72 61 74 65 67 79 20 3d  Csr->iStrategy =
387ae 20 69 64 78 4e 75 6d 3b 0a 0a 20 20 69 66 28 20   idxNum;..  if( 
387af 69 64 78 4e 75 6d 3d 3d 31 20 29 7b 0a 20 20 20  idxNum==1 ){.   
387b0 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73 65   /* Special case
387b1 20 2d 20 6c 6f 6f 6b 75 70 20 62 79 20 72 6f 77   - lookup by row
387b2 69 64 2e 20 2a 2f 0a 20 20 20 20 52 74 72 65 65  id. */.    Rtree
387b3 4e 6f 64 65 20 2a 70 4c 65 61 66 3b 20 20 20 20  Node *pLeaf;    
387b4 20 20 20 20 2f 2a 20 4c 65 61 66 20 6f 6e 20 77      /* Leaf on w
387b5 68 69 63 68 20 74 68 65 20 72 65 71 75 69 72 65  hich the require
387b6 64 20 63 65 6c 6c 20 72 65 73 69 64 65 73 20 2a  d cell resides *
387b7 2f 0a 20 20 20 20 69 36 34 20 69 52 6f 77 69 64  /.    i64 iRowid
387b8 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
387b9 5f 69 6e 74 36 34 28 61 72 67 76 5b 30 5d 29 3b  _int64(argv[0]);
387ba 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c 65  .    rc = findLe
387bb 61 66 4e 6f 64 65 28 70 52 74 72 65 65 2c 20 69  afNode(pRtree, i
387bc 52 6f 77 69 64 2c 20 26 70 4c 65 61 66 29 3b 0a  Rowid, &pLeaf);.
387bd 20 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64 65 20      pCsr->pNode 
387be 3d 20 70 4c 65 61 66 3b 20 0a 20 20 20 20 69 66  = pLeaf; .    if
387bf 28 20 70 4c 65 61 66 20 26 26 20 72 63 3d 3d 53  ( pLeaf && rc==S
387c0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
387c1 20 20 70 43 73 72 2d 3e 69 43 65 6c 6c 20 3d 20    pCsr->iCell = 
387c2 6e 6f 64 65 52 6f 77 69 64 49 6e 64 65 78 28 70  nodeRowidIndex(p
387c3 52 74 72 65 65 2c 20 70 4c 65 61 66 2c 20 69 52  Rtree, pLeaf, iR
387c4 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  owid);.    }.  }
387c5 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 4e 6f 72  else{.    /* Nor
387c6 6d 61 6c 20 63 61 73 65 20 2d 20 72 2d 74 72 65  mal case - r-tre
387c7 65 20 73 63 61 6e 2e 20 53 65 74 20 75 70 20 74  e scan. Set up t
387c8 68 65 20 52 74 72 65 65 43 75 72 73 6f 72 2e 61  he RtreeCursor.a
387c9 43 6f 6e 73 74 72 61 69 6e 74 20 61 72 72 61 79  Constraint array
387ca 20 0a 20 20 20 20 2a 2a 20 77 69 74 68 20 74 68   .    ** with th
387cb 65 20 63 6f 6e 66 69 67 75 72 65 64 20 63 6f 6e  e configured con
387cc 73 74 72 61 69 6e 74 73 2e 20 0a 20 20 20 20 2a  straints. .    *
387cd 2f 0a 20 20 20 20 69 66 28 20 61 72 67 63 3e 30  /.    if( argc>0
387ce 20 29 7b 0a 20 20 20 20 20 20 70 43 73 72 2d 3e   ){.      pCsr->
387cf 61 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20 73 71  aConstraint = sq
387d0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a  lite3_malloc(siz
387d1 65 6f 66 28 52 74 72 65 65 43 6f 6e 73 74 72 61  eof(RtreeConstra
387d2 69 6e 74 29 2a 61 72 67 63 29 3b 0a 20 20 20 20  int)*argc);.    
387d3 20 20 70 43 73 72 2d 3e 6e 43 6f 6e 73 74 72 61    pCsr->nConstra
387d4 69 6e 74 20 3d 20 61 72 67 63 3b 0a 20 20 20 20  int = argc;.    
387d5 20 20 69 66 28 20 21 70 43 73 72 2d 3e 61 43 6f    if( !pCsr->aCo
387d6 6e 73 74 72 61 69 6e 74 20 29 7b 0a 20 20 20 20  nstraint ){.    
387d7 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
387d8 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 65 6c  NOMEM;.      }el
387d9 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65  se{.        asse
387da 72 74 28 20 28 69 64 78 53 74 72 3d 3d 30 20 26  rt( (idxStr==0 &
387db 26 20 61 72 67 63 3d 3d 30 29 20 7c 7c 20 73 74  & argc==0) || st
387dc 72 6c 65 6e 28 69 64 78 53 74 72 29 3d 3d 61 72  rlen(idxStr)==ar
387dd 67 63 2a 32 20 29 3b 0a 20 20 20 20 20 20 20 20  gc*2 );.        
387de 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 61 72 67  for(ii=0; ii<arg
387df 63 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20  c; ii++){.      
387e0 20 20 20 20 52 74 72 65 65 43 6f 6e 73 74 72 61      RtreeConstra
387e1 69 6e 74 20 2a 70 20 3d 20 26 70 43 73 72 2d 3e  int *p = &pCsr->
387e2 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 69 5d 3b  aConstraint[ii];
387e3 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 6f 70  .          p->op
387e4 20 3d 20 69 64 78 53 74 72 5b 69 69 2a 32 5d 3b   = idxStr[ii*2];
387e5 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69 43  .          p->iC
387e6 6f 6f 72 64 20 3d 20 69 64 78 53 74 72 5b 69 69  oord = idxStr[ii
387e7 2a 32 2b 31 5d 2d 27 61 27 3b 0a 20 20 20 20 20  *2+1]-'a';.     
387e8 20 20 20 20 20 70 2d 3e 72 56 61 6c 75 65 20 3d       p->rValue =
387e9 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64   sqlite3_value_d
387ea 6f 75 62 6c 65 28 61 72 67 76 5b 69 69 5d 29 3b  ouble(argv[ii]);
387eb 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
387ec 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20   }.    }.  .    
387ed 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
387ee 4b 20 29 7b 0a 20 20 20 20 20 20 70 43 73 72 2d  K ){.      pCsr-
387ef 3e 70 4e 6f 64 65 20 3d 20 30 3b 0a 20 20 20 20  >pNode = 0;.    
387f0 20 20 72 63 20 3d 20 6e 6f 64 65 41 63 71 75 69    rc = nodeAcqui
387f1 72 65 28 70 52 74 72 65 65 2c 20 31 2c 20 30 2c  re(pRtree, 1, 0,
387f2 20 26 70 52 6f 6f 74 29 3b 0a 20 20 20 20 7d 0a   &pRoot);.    }.
387f3 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
387f4 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69  TE_OK ){.      i
387f5 6e 74 20 69 73 45 6f 66 20 3d 20 31 3b 0a 20 20  nt isEof = 1;.  
387f6 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20      int nCell = 
387f7 4e 43 45 4c 4c 28 70 52 6f 6f 74 29 3b 0a 20 20  NCELL(pRoot);.  
387f8 20 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64 65 20      pCsr->pNode 
387f9 3d 20 70 52 6f 6f 74 3b 0a 20 20 20 20 20 20 66  = pRoot;.      f
387fa 6f 72 28 70 43 73 72 2d 3e 69 43 65 6c 6c 3d 30  or(pCsr->iCell=0
387fb 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ; rc==SQLITE_OK 
387fc 26 26 20 70 43 73 72 2d 3e 69 43 65 6c 6c 3c 6e  && pCsr->iCell<n
387fd 43 65 6c 6c 3b 20 70 43 73 72 2d 3e 69 43 65 6c  Cell; pCsr->iCel
387fe 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 61 73  l++){.        as
387ff 73 65 72 74 28 20 70 43 73 72 2d 3e 70 4e 6f 64  sert( pCsr->pNod
38800 65 3d 3d 70 52 6f 6f 74 20 29 3b 0a 20 20 20 20  e==pRoot );.    
38801 20 20 20 20 72 63 20 3d 20 64 65 73 63 65 6e 64      rc = descend
38802 54 6f 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  ToCell(pRtree, p
38803 43 73 72 2c 20 70 52 74 72 65 65 2d 3e 69 44 65  Csr, pRtree->iDe
38804 70 74 68 2c 20 26 69 73 45 6f 66 29 3b 0a 20 20  pth, &isEof);.  
38805 20 20 20 20 20 20 69 66 28 20 21 69 73 45 6f 66        if( !isEof
38806 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62 72   ){.          br
38807 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
38808 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
38809 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
3880a 26 20 69 73 45 6f 66 20 29 7b 0a 20 20 20 20 20  & isEof ){.     
3880b 20 20 20 61 73 73 65 72 74 28 20 70 43 73 72 2d     assert( pCsr-
3880c 3e 70 4e 6f 64 65 3d 3d 70 52 6f 6f 74 20 29 3b  >pNode==pRoot );
3880d 0a 20 20 20 20 20 20 20 20 6e 6f 64 65 52 65 6c  .        nodeRel
3880e 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 52 6f  ease(pRtree, pRo
3880f 6f 74 29 3b 0a 20 20 20 20 20 20 20 20 70 43 73  ot);.        pCs
38810 72 2d 3e 70 4e 6f 64 65 20 3d 20 30 3b 0a 20 20  r->pNode = 0;.  
38811 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65      }.      asse
38812 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  rt( rc!=SQLITE_O
38813 4b 20 7c 7c 20 21 70 43 73 72 2d 3e 70 4e 6f 64  K || !pCsr->pNod
38814 65 20 7c 7c 20 70 43 73 72 2d 3e 69 43 65 6c 6c  e || pCsr->iCell
38815 3c 4e 43 45 4c 4c 28 70 43 73 72 2d 3e 70 4e 6f  <NCELL(pCsr->pNo
38816 64 65 29 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  de) );.    }.  }
38817 0a 0a 20 20 72 74 72 65 65 52 65 6c 65 61 73 65  ..  rtreeRelease
38818 28 70 52 74 72 65 65 29 3b 0a 20 20 72 65 74 75  (pRtree);.  retu
38819 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
3881a 52 74 72 65 65 20 76 69 72 74 75 61 6c 20 74 61  Rtree virtual ta
3881b 62 6c 65 20 6d 6f 64 75 6c 65 20 78 42 65 73 74  ble module xBest
3881c 49 6e 64 65 78 20 6d 65 74 68 6f 64 2e 20 54 68  Index method. Th
3881d 65 72 65 20 61 72 65 20 74 68 72 65 65 0a 2a 2a  ere are three.**
3881e 20 74 61 62 6c 65 20 73 63 61 6e 20 73 74 72 61   table scan stra
3881f 74 65 67 69 65 73 20 74 6f 20 63 68 6f 6f 73 65  tegies to choose
38820 20 66 72 6f 6d 20 28 69 6e 20 6f 72 64 65 72 20   from (in order 
38821 66 72 6f 6d 20 6d 6f 73 74 20 74 6f 20 0a 2a 2a  from most to .**
38822 20 6c 65 61 73 74 20 64 65 73 69 72 61 62 6c 65   least desirable
38823 29 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 64 78 4e 75  ):.**.**   idxNu
38824 6d 20 20 20 20 20 69 64 78 53 74 72 20 20 20 20  m     idxStr    
38825 20 20 20 20 53 74 72 61 74 65 67 79 0a 2a 2a 20      Strategy.** 
38826 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    --------------
38827 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
38828 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
38829 2d 2d 0a 2a 2a 20 20 20 20 20 31 20 20 20 20 20  --.**     1     
3882a 20 20 20 55 6e 75 73 65 64 20 20 20 20 20 20 20     Unused       
3882b 20 44 69 72 65 63 74 20 6c 6f 6f 6b 75 70 20 62   Direct lookup b
3882c 79 20 72 6f 77 69 64 2e 0a 2a 2a 20 20 20 20 20  y rowid..**     
3882d 32 20 20 20 20 20 20 20 20 53 65 65 20 62 65 6c  2        See bel
3882e 6f 77 20 20 20 20 20 52 2d 74 72 65 65 20 71 75  ow     R-tree qu
3882f 65 72 79 2e 0a 2a 2a 20 20 20 20 20 33 20 20 20  ery..**     3   
38830 20 20 20 20 20 55 6e 75 73 65 64 20 20 20 20 20       Unused     
38831 20 20 20 46 75 6c 6c 20 74 61 62 6c 65 20 73 63     Full table sc
38832 61 6e 2e 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d  an..**   -------
38833 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
38834 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
38835 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20  ---------.**.** 
38836 49 66 20 73 74 72 61 74 65 67 79 20 31 20 6f 72  If strategy 1 or
38837 20 33 20 69 73 20 75 73 65 64 2c 20 74 68 65 6e   3 is used, then
38838 20 69 64 78 53 74 72 20 69 73 20 6e 6f 74 20 6d   idxStr is not m
38839 65 61 6e 69 6e 67 66 75 6c 2e 20 49 66 20 73 74  eaningful. If st
3883a 72 61 74 65 67 79 0a 2a 2a 20 32 20 69 73 20 75  rategy.** 2 is u
3883b 73 65 64 2c 20 69 64 78 53 74 72 20 69 73 20 66  sed, idxStr is f
3883c 6f 72 6d 61 74 74 65 64 20 74 6f 20 63 6f 6e 74  ormatted to cont
3883d 61 69 6e 20 32 20 62 79 74 65 73 20 66 6f 72 20  ain 2 bytes for 
3883e 65 61 63 68 20 0a 2a 2a 20 63 6f 6e 73 74 72 61  each .** constra
3883f 69 6e 74 20 75 73 65 64 2e 20 54 68 65 20 66 69  int used. The fi
38840 72 73 74 20 74 77 6f 20 62 79 74 65 73 20 6f 66  rst two bytes of
38841 20 69 64 78 53 74 72 20 63 6f 72 72 65 73 70 6f   idxStr correspo
38842 6e 64 20 74 6f 20 0a 2a 2a 20 74 68 65 20 63 6f  nd to .** the co
38843 6e 73 74 72 61 69 6e 74 20 69 6e 20 73 71 6c 69  nstraint in sqli
38844 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 2e 61  te3_index_info.a
38845 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b  ConstraintUsage[
38846 5d 20 77 69 74 68 0a 2a 2a 20 28 61 72 67 76 49  ] with.** (argvI
38847 6e 64 65 78 3d 3d 31 29 20 65 74 63 2e 0a 2a 2a  ndex==1) etc..**
38848 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 6f 66  .** The first of
38849 20 65 61 63 68 20 70 61 69 72 20 6f 66 20 62 79   each pair of by
3884a 74 65 73 20 69 6e 20 69 64 78 53 74 72 20 69 64  tes in idxStr id
3884b 65 6e 74 69 66 69 65 73 20 74 68 65 20 63 6f 6e  entifies the con
3884c 73 74 72 61 69 6e 74 0a 2a 2a 20 6f 70 65 72 61  straint.** opera
3884d 74 6f 72 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  tor as follows:.
3884e 2a 2a 0a 2a 2a 20 20 20 4f 70 65 72 61 74 6f 72  **.**   Operator
3884f 20 20 20 20 42 79 74 65 20 56 61 6c 75 65 0a 2a      Byte Value.*
38850 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  *   ------------
38851 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20  ----------.**   
38852 20 20 20 3d 20 20 20 20 20 20 20 20 30 78 34 31     =        0x41
38853 20 28 27 41 27 29 0a 2a 2a 20 20 20 20 20 3c 3d   ('A').**     <=
38854 20 20 20 20 20 20 20 20 30 78 34 32 20 28 27 42          0x42 ('B
38855 27 29 0a 2a 2a 20 20 20 20 20 20 3c 20 20 20 20  ').**      <    
38856 20 20 20 20 30 78 34 33 20 28 27 43 27 29 0a 2a      0x43 ('C').*
38857 2a 20 20 20 20 20 3e 3d 20 20 20 20 20 20 20 20  *     >=        
38858 30 78 34 34 20 28 27 44 27 29 0a 2a 2a 20 20 20  0x44 ('D').**   
38859 20 20 20 3e 20 20 20 20 20 20 20 20 30 78 34 35     >        0x45
3885a 20 28 27 45 27 29 0a 2a 2a 20 20 20 2d 2d 2d 2d   ('E').**   ----
3885b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3885c 2d 2d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63  --.**.** The sec
3885d 6f 6e 64 20 6f 66 20 65 61 63 68 20 70 61 69 72  ond of each pair
3885e 20 6f 66 20 62 79 74 65 73 20 69 64 65 6e 74 69   of bytes identi
3885f 66 69 65 73 20 74 68 65 20 63 6f 6f 72 64 69 6e  fies the coordin
38860 61 74 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 74 6f  ate column.** to
38861 20 77 68 69 63 68 20 74 68 65 20 63 6f 6e 73 74   which the const
38862 72 61 69 6e 74 20 61 70 70 6c 69 65 73 2e 20 54  raint applies. T
38863 68 65 20 6c 65 66 74 6d 6f 73 74 20 63 6f 6f 72  he leftmost coor
38864 64 69 6e 61 74 65 20 63 6f 6c 75 6d 6e 0a 2a 2a  dinate column.**
38865 20 69 73 20 27 61 27 2c 20 74 68 65 20 73 65 63   is 'a', the sec
38866 6f 6e 64 20 66 72 6f 6d 20 74 68 65 20 6c 65 66  ond from the lef
38867 74 20 27 62 27 20 65 74 63 2e 0a 2a 2f 0a 73 74  t 'b' etc..*/.st
38868 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 42 65  atic int rtreeBe
38869 73 74 49 6e 64 65 78 28 73 71 6c 69 74 65 33 5f  stIndex(sqlite3_
3886a 76 74 61 62 20 2a 74 61 62 2c 20 73 71 6c 69 74  vtab *tab, sqlit
3886b 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70  e3_index_info *p
3886c 49 64 78 49 6e 66 6f 29 7b 0a 20 20 69 6e 74 20  IdxInfo){.  int 
3886d 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
3886e 20 20 69 6e 74 20 69 69 2c 20 63 43 6f 6c 3b 0a    int ii, cCol;.
3886f 0a 20 20 69 6e 74 20 69 49 64 78 20 3d 20 30 3b  .  int iIdx = 0;
38870 0a 20 20 63 68 61 72 20 7a 49 64 78 53 74 72 5b  .  char zIdxStr[
38871 52 54 52 45 45 5f 4d 41 58 5f 44 49 4d 45 4e 53  RTREE_MAX_DIMENS
38872 49 4f 4e 53 2a 38 2b 31 5d 3b 0a 20 20 6d 65 6d  IONS*8+1];.  mem
38873 73 65 74 28 7a 49 64 78 53 74 72 2c 20 30 2c 20  set(zIdxStr, 0, 
38874 73 69 7a 65 6f 66 28 7a 49 64 78 53 74 72 29 29  sizeof(zIdxStr))
38875 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 49 64  ;..  assert( pId
38876 78 49 6e 66 6f 2d 3e 69 64 78 53 74 72 3d 3d 30  xInfo->idxStr==0
38877 20 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20   );.  for(ii=0; 
38878 69 69 3c 70 49 64 78 49 6e 66 6f 2d 3e 6e 43 6f  ii<pIdxInfo->nCo
38879 6e 73 74 72 61 69 6e 74 3b 20 69 69 2b 2b 29 7b  nstraint; ii++){
3887a 0a 20 20 20 20 73 74 72 75 63 74 20 73 71 6c 69  .    struct sqli
3887b 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72  te3_index_constr
3887c 61 69 6e 74 20 2a 70 20 3d 20 26 70 49 64 78 49  aint *p = &pIdxI
3887d 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74  nfo->aConstraint
3887e 5b 69 69 5d 3b 0a 0a 20 20 20 20 69 66 28 20 70  [ii];..    if( p
3887f 2d 3e 75 73 61 62 6c 65 20 26 26 20 70 2d 3e 69  ->usable && p->i
38880 43 6f 6c 75 6d 6e 3d 3d 30 20 26 26 20 70 2d 3e  Column==0 && p->
38881 6f 70 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58  op==SQLITE_INDEX
38882 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 45 51 20 29  _CONSTRAINT_EQ )
38883 7b 0a 20 20 20 20 20 20 2f 2a 20 57 65 20 68 61  {.      /* We ha
38884 76 65 20 61 6e 20 65 71 75 61 6c 69 74 79 20 63  ve an equality c
38885 6f 6e 73 74 72 61 69 6e 74 20 6f 6e 20 74 68 65  onstraint on the
38886 20 72 6f 77 69 64 2e 20 55 73 65 20 73 74 72 61   rowid. Use stra
38887 74 65 67 79 20 31 2e 20 2a 2f 0a 20 20 20 20 20  tegy 1. */.     
38888 20 69 6e 74 20 6a 6a 3b 0a 20 20 20 20 20 20 66   int jj;.      f
38889 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c 69 69 3b 20  or(jj=0; jj<ii; 
3888a 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70  jj++){.        p
3888b 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72  IdxInfo->aConstr
3888c 61 69 6e 74 55 73 61 67 65 5b 6a 6a 5d 2e 61 72  aintUsage[jj].ar
3888d 67 76 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20 20  gvIndex = 0;.   
3888e 20 20 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e 61       pIdxInfo->a
3888f 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b  ConstraintUsage[
38890 6a 6a 5d 2e 6f 6d 69 74 20 3d 20 30 3b 0a 20 20  jj].omit = 0;.  
38891 20 20 20 20 7d 0a 20 20 20 20 20 20 70 49 64 78      }.      pIdx
38892 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20 31  Info->idxNum = 1
38893 3b 0a 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f  ;.      pIdxInfo
38894 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61  ->aConstraintUsa
38895 67 65 5b 69 69 5d 2e 61 72 67 76 49 6e 64 65 78  ge[ii].argvIndex
38896 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 49 64 78   = 1;.      pIdx
38897 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e  Info->aConstrain
38898 74 55 73 61 67 65 5b 6a 6a 5d 2e 6f 6d 69 74 20  tUsage[jj].omit 
38899 3d 20 31 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 54  = 1;..      /* T
3889a 68 69 73 20 73 74 72 61 74 65 67 79 20 69 6e 76  his strategy inv
3889b 6f 6c 76 65 73 20 61 20 74 77 6f 20 72 6f 77 69  olves a two rowi
3889c 64 20 6c 6f 6f 6b 75 70 73 20 6f 6e 20 61 6e 20  d lookups on an 
3889d 42 2d 54 72 65 65 20 73 74 72 75 63 74 75 72 65  B-Tree structure
3889e 73 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 74  s.      ** and t
3889f 68 65 6e 20 61 20 6c 69 6e 65 61 72 20 73 65 61  hen a linear sea
388a0 72 63 68 20 6f 66 20 61 6e 20 52 2d 54 72 65 65  rch of an R-Tree
388a1 20 6e 6f 64 65 2e 20 54 68 69 73 20 73 68 6f 75   node. This shou
388a2 6c 64 20 62 65 20 0a 20 20 20 20 20 20 2a 2a 20  ld be .      ** 
388a3 63 6f 6e 73 69 64 65 72 65 64 20 61 6c 6d 6f 73  considered almos
388a4 74 20 61 73 20 71 75 69 63 6b 20 61 73 20 61 20  t as quick as a 
388a5 64 69 72 65 63 74 20 72 6f 77 69 64 20 6c 6f 6f  direct rowid loo
388a6 6b 75 70 20 28 66 6f 72 20 77 68 69 63 68 20 0a  kup (for which .
388a7 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 20        ** sqlite 
388a8 75 73 65 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c  uses an internal
388a9 20 63 6f 73 74 20 6f 66 20 30 2e 30 29 2e 0a 20   cost of 0.0).. 
388aa 20 20 20 20 20 2a 2f 20 0a 20 20 20 20 20 20 70       */ .      p
388ab 49 64 78 49 6e 66 6f 2d 3e 65 73 74 69 6d 61 74  IdxInfo->estimat
388ac 65 64 43 6f 73 74 20 3d 20 31 30 2e 30 3b 0a 20  edCost = 10.0;. 
388ad 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
388ae 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 0a 20 20  TE_OK;.    }..  
388af 20 20 69 66 28 20 70 2d 3e 75 73 61 62 6c 65 20    if( p->usable 
388b0 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3e 30 20  && p->iColumn>0 
388b1 29 7b 0a 20 20 20 20 20 20 75 38 20 6f 70 20 3d  ){.      u8 op =
388b2 20 30 3b 0a 20 20 20 20 20 20 73 77 69 74 63 68   0;.      switch
388b3 28 20 70 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 20  ( p->op ){.     
388b4 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49     case SQLITE_I
388b5 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f  NDEX_CONSTRAINT_
388b6 45 51 3a 20 6f 70 20 3d 20 52 54 52 45 45 5f 45  EQ: op = RTREE_E
388b7 51 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20  Q; break;.      
388b8 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e    case SQLITE_IN
388b9 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47  DEX_CONSTRAINT_G
388ba 54 3a 20 6f 70 20 3d 20 52 54 52 45 45 5f 47 54  T: op = RTREE_GT
388bb 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20  ; break;.       
388bc 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 44   case SQLITE_IND
388bd 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 45  EX_CONSTRAINT_LE
388be 3a 20 6f 70 20 3d 20 52 54 52 45 45 5f 4c 45 3b  : op = RTREE_LE;
388bf 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
388c0 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 44 45  case SQLITE_INDE
388c1 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 54 3a  X_CONSTRAINT_LT:
388c2 20 6f 70 20 3d 20 52 54 52 45 45 5f 4c 54 3b 20   op = RTREE_LT; 
388c3 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63  break;.        c
388c4 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58  ase SQLITE_INDEX
388c5 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 45 3a 20  _CONSTRAINT_GE: 
388c6 6f 70 20 3d 20 52 54 52 45 45 5f 47 45 3b 20 62  op = RTREE_GE; b
388c7 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
388c8 20 20 20 20 69 66 28 20 6f 70 20 29 7b 0a 20 20      if( op ){.  
388c9 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75        /* Make su
388ca 72 65 20 74 68 69 73 20 70 61 72 74 69 63 75 6c  re this particul
388cb 61 72 20 63 6f 6e 73 74 72 61 69 6e 74 20 68 61  ar constraint ha
388cc 73 20 6e 6f 74 20 62 65 65 6e 20 75 73 65 64 20  s not been used 
388cd 62 65 66 6f 72 65 2e 0a 20 20 20 20 20 20 20 20  before..        
388ce 2a 2a 20 49 66 20 69 74 20 68 61 73 20 62 65 65  ** If it has bee
388cf 6e 20 75 73 65 64 20 62 65 66 6f 72 65 2c 20 69  n used before, i
388d0 67 6e 6f 72 65 20 69 74 2e 0a 20 20 20 20 20 20  gnore it..      
388d1 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20    **.        ** 
388d2 41 20 3c 3d 20 6f 72 20 3c 20 63 61 6e 20 62 65  A <= or < can be
388d3 20 75 73 65 64 20 69 66 20 74 68 65 72 65 20 69   used if there i
388d4 73 20 61 20 70 72 69 6f 72 20 3e 3d 20 6f 72 20  s a prior >= or 
388d5 3e 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 41 20  >..        ** A 
388d6 3e 3d 20 6f 72 20 3e 20 63 61 6e 20 62 65 20 75  >= or > can be u
388d7 73 65 64 20 69 66 20 74 68 65 72 65 20 69 73 20  sed if there is 
388d8 61 20 70 72 69 6f 72 20 3c 20 6f 72 20 3c 3d 2e  a prior < or <=.
388d9 0a 20 20 20 20 20 20 20 20 2a 2a 20 41 20 3c 3d  .        ** A <=
388da 20 6f 72 20 3c 20 69 73 20 64 69 73 71 75 61 6c   or < is disqual
388db 69 66 69 65 64 20 69 66 20 74 68 65 72 65 20 69  ified if there i
388dc 73 20 61 20 70 72 69 6f 72 20 3c 3d 2c 20 3c 2c  s a prior <=, <,
388dd 20 6f 72 20 3d 3d 2e 0a 20 20 20 20 20 20 20 20   or ==..        
388de 2a 2a 20 41 20 3e 3d 20 6f 72 20 3e 20 69 73 20  ** A >= or > is 
388df 64 69 73 71 75 61 6c 69 66 69 65 64 20 69 66 20  disqualified if 
388e0 74 68 65 72 65 20 69 73 20 61 20 70 72 69 6f 72  there is a prior
388e1 20 3e 3d 2c 20 3e 2c 20 6f 72 20 3d 3d 2e 0a 20   >=, >, or ==.. 
388e2 20 20 20 20 20 20 20 2a 2a 20 41 20 3d 3d 20 69         ** A == i
388e3 73 20 64 69 73 71 75 61 6c 69 66 65 64 20 69 66  s disqualifed if
388e4 20 74 68 65 72 65 20 69 73 20 61 6e 79 20 70 72   there is any pr
388e5 69 6f 72 20 63 6f 6e 73 74 72 61 69 6e 74 2e 0a  ior constraint..
388e6 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
388e7 20 20 20 69 6e 74 20 6a 2c 20 6f 70 6d 73 6b 3b     int j, opmsk;
388e8 0a 20 20 20 20 20 20 20 20 73 74 61 74 69 63 20  .        static 
388e9 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
388ea 68 61 72 20 63 6f 6d 70 61 74 69 62 6c 65 5b 5d  har compatible[]
388eb 20 3d 20 7b 20 30 2c 20 30 2c 20 31 2c 20 31 2c   = { 0, 0, 1, 1,
388ec 20 32 2c 20 32 20 7d 3b 0a 20 20 20 20 20 20 20   2, 2 };.       
388ed 20 61 73 73 65 72 74 28 20 63 6f 6d 70 61 74 69   assert( compati
388ee 62 6c 65 5b 52 54 52 45 45 5f 45 51 20 26 20 37  ble[RTREE_EQ & 7
388ef 5d 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  ]==0 );.        
388f0 61 73 73 65 72 74 28 20 63 6f 6d 70 61 74 69 62  assert( compatib
388f1 6c 65 5b 52 54 52 45 45 5f 4c 54 20 26 20 37 5d  le[RTREE_LT & 7]
388f2 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 61  ==1 );.        a
388f3 73 73 65 72 74 28 20 63 6f 6d 70 61 74 69 62 6c  ssert( compatibl
388f4 65 5b 52 54 52 45 45 5f 4c 45 20 26 20 37 5d 3d  e[RTREE_LE & 7]=
388f5 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73  =1 );.        as
388f6 73 65 72 74 28 20 63 6f 6d 70 61 74 69 62 6c 65  sert( compatible
388f7 5b 52 54 52 45 45 5f 47 54 20 26 20 37 5d 3d 3d  [RTREE_GT & 7]==
388f8 32 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73  2 );.        ass
388f9 65 72 74 28 20 63 6f 6d 70 61 74 69 62 6c 65 5b  ert( compatible[
388fa 52 54 52 45 45 5f 47 45 20 26 20 37 5d 3d 3d 32  RTREE_GE & 7]==2
388fb 20 29 3b 0a 20 20 20 20 20 20 20 20 63 43 6f 6c   );.        cCol
388fc 20 3d 20 70 2d 3e 69 43 6f 6c 75 6d 6e 20 2d 20   = p->iColumn - 
388fd 31 20 2b 20 27 61 27 3b 0a 20 20 20 20 20 20 20  1 + 'a';.       
388fe 20 6f 70 6d 73 6b 20 3d 20 63 6f 6d 70 61 74 69   opmsk = compati
388ff 62 6c 65 5b 6f 70 20 26 20 37 5d 3b 0a 20 20 20  ble[op & 7];.   
38900 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
38901 69 49 64 78 3b 20 6a 2b 3d 32 29 7b 0a 20 20 20  iIdx; j+=2){.   
38902 20 20 20 20 20 20 20 69 66 28 20 7a 49 64 78 53         if( zIdxS
38903 74 72 5b 6a 2b 31 5d 3d 3d 63 43 6f 6c 20 26 26  tr[j+1]==cCol &&
38904 20 28 63 6f 6d 70 61 74 69 62 6c 65 5b 7a 49 64   (compatible[zId
38905 78 53 74 72 5b 6a 5d 20 26 20 37 5d 20 26 20 6f  xStr[j] & 7] & o
38906 70 6d 73 6b 29 21 3d 30 20 29 7b 0a 20 20 20 20  pmsk)!=0 ){.    
38907 20 20 20 20 20 20 20 20 6f 70 20 3d 20 30 3b 0a          op = 0;.
38908 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61              brea
38909 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  k;.          }. 
3890a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
3890b 0a 20 20 20 20 20 20 69 66 28 20 6f 70 20 29 7b  .      if( op ){
3890c 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
3890d 20 69 49 64 78 3c 73 69 7a 65 6f 66 28 7a 49 64   iIdx<sizeof(zId
3890e 78 53 74 72 29 2d 31 20 29 3b 0a 20 20 20 20 20  xStr)-1 );.     
3890f 20 20 20 7a 49 64 78 53 74 72 5b 69 49 64 78 2b     zIdxStr[iIdx+
38910 2b 5d 20 3d 20 6f 70 3b 0a 20 20 20 20 20 20 20  +] = op;.       
38911 20 7a 49 64 78 53 74 72 5b 69 49 64 78 2b 2b 5d   zIdxStr[iIdx++]
38912 20 3d 20 63 43 6f 6c 3b 0a 20 20 20 20 20 20 20   = cCol;.       
38913 20 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73   pIdxInfo->aCons
38914 74 72 61 69 6e 74 55 73 61 67 65 5b 69 69 5d 2e  traintUsage[ii].
38915 61 72 67 76 49 6e 64 65 78 20 3d 20 28 69 49 64  argvIndex = (iId
38916 78 2f 32 29 3b 0a 20 20 20 20 20 20 20 20 70 49  x/2);.        pI
38917 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61  dxInfo->aConstra
38918 69 6e 74 55 73 61 67 65 5b 69 69 5d 2e 6f 6d 69  intUsage[ii].omi
38919 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  t = 1;.      }. 
3891a 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 49 64 78     }.  }..  pIdx
3891b 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20 32  Info->idxNum = 2
3891c 3b 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 6e 65  ;.  pIdxInfo->ne
3891d 65 64 54 6f 46 72 65 65 49 64 78 53 74 72 20 3d  edToFreeIdxStr =
3891e 20 31 3b 0a 20 20 69 66 28 20 69 49 64 78 3e 30   1;.  if( iIdx>0
3891f 20 26 26 20 30 3d 3d 28 70 49 64 78 49 6e 66 6f   && 0==(pIdxInfo
38920 2d 3e 69 64 78 53 74 72 20 3d 20 73 71 6c 69 74  ->idxStr = sqlit
38921 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 22 2c  e3_mprintf("%s",
38922 20 7a 49 64 78 53 74 72 29 29 20 29 7b 0a 20 20   zIdxStr)) ){.  
38923 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
38924 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 61 73 73  NOMEM;.  }.  ass
38925 65 72 74 28 20 69 49 64 78 3e 3d 30 20 29 3b 0a  ert( iIdx>=0 );.
38926 20 20 70 49 64 78 49 6e 66 6f 2d 3e 65 73 74 69    pIdxInfo->esti
38927 6d 61 74 65 64 43 6f 73 74 20 3d 20 28 32 30 30  matedCost = (200
38928 30 30 30 30 2e 30 20 2f 20 28 64 6f 75 62 6c 65  0000.0 / (double
38929 29 28 69 49 64 78 20 2b 20 31 29 29 3b 0a 20 20  )(iIdx + 1));.  
3892a 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
3892b 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 4e  .** Return the N
3892c 2d 64 69 6d 65 6e 73 69 6f 6e 61 6c 20 76 6f 6c  -dimensional vol
3892d 75 6d 6e 20 6f 66 20 74 68 65 20 63 65 6c 6c 20  umn of the cell 
3892e 73 74 6f 72 65 64 20 69 6e 20 2a 70 2e 0a 2a 2f  stored in *p..*/
3892f 0a 73 74 61 74 69 63 20 66 6c 6f 61 74 20 63 65  .static float ce
38930 6c 6c 41 72 65 61 28 52 74 72 65 65 20 2a 70 52  llArea(Rtree *pR
38931 74 72 65 65 2c 20 52 74 72 65 65 43 65 6c 6c 20  tree, RtreeCell 
38932 2a 70 29 7b 0a 20 20 66 6c 6f 61 74 20 61 72 65  *p){.  float are
38933 61 20 3d 20 31 2e 30 3b 0a 20 20 69 6e 74 20 69  a = 1.0;.  int i
38934 69 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  i;.  for(ii=0; i
38935 69 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a  i<(pRtree->nDim*
38936 32 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20 20  2); ii+=2){.    
38937 61 72 65 61 20 3d 20 61 72 65 61 20 2a 20 28 44  area = area * (D
38938 43 4f 4f 52 44 28 70 2d 3e 61 43 6f 6f 72 64 5b  COORD(p->aCoord[
38939 69 69 2b 31 5d 29 20 2d 20 44 43 4f 4f 52 44 28  ii+1]) - DCOORD(
3893a 70 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 29 29 3b  p->aCoord[ii]));
3893b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 61 72  .  }.  return ar
3893c 65 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  ea;.}../*.** Ret
3893d 75 72 6e 20 74 68 65 20 6d 61 72 67 69 6e 20 6c  urn the margin l
3893e 65 6e 67 74 68 20 6f 66 20 63 65 6c 6c 20 70 2e  ength of cell p.
3893f 20 54 68 65 20 6d 61 72 67 69 6e 20 6c 65 6e 67   The margin leng
38940 74 68 20 69 73 20 74 68 65 20 73 75 6d 0a 2a 2a  th is the sum.**
38941 20 6f 66 20 74 68 65 20 6f 62 6a 65 63 74 73 20   of the objects 
38942 73 69 7a 65 20 69 6e 20 65 61 63 68 20 64 69 6d  size in each dim
38943 65 6e 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  ension..*/.stati
38944 63 20 66 6c 6f 61 74 20 63 65 6c 6c 4d 61 72 67  c float cellMarg
38945 69 6e 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  in(Rtree *pRtree
38946 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 29 7b  , RtreeCell *p){
38947 0a 20 20 66 6c 6f 61 74 20 6d 61 72 67 69 6e 20  .  float margin 
38948 3d 20 30 2e 30 3b 0a 20 20 69 6e 74 20 69 69 3b  = 0.0;.  int ii;
38949 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c  .  for(ii=0; ii<
3894a 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 29  (pRtree->nDim*2)
3894b 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20 20 6d 61  ; ii+=2){.    ma
3894c 72 67 69 6e 20 2b 3d 20 28 44 43 4f 4f 52 44 28  rgin += (DCOORD(
3894d 70 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 29  p->aCoord[ii+1])
3894e 20 2d 20 44 43 4f 4f 52 44 28 70 2d 3e 61 43 6f   - DCOORD(p->aCo
3894f 6f 72 64 5b 69 69 5d 29 29 3b 0a 20 20 7d 0a 20  ord[ii]));.  }. 
38950 20 72 65 74 75 72 6e 20 6d 61 72 67 69 6e 3b 0a   return margin;.
38951 7d 0a 0a 2f 2a 0a 2a 2a 20 53 74 6f 72 65 20 74  }../*.** Store t
38952 68 65 20 75 6e 69 6f 6e 20 6f 66 20 63 65 6c 6c  he union of cell
38953 73 20 70 31 20 61 6e 64 20 70 32 20 69 6e 20 70  s p1 and p2 in p
38954 31 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  1..*/.static voi
38955 64 20 63 65 6c 6c 55 6e 69 6f 6e 28 52 74 72 65  d cellUnion(Rtre
38956 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65  e *pRtree, Rtree
38957 43 65 6c 6c 20 2a 70 31 2c 20 52 74 72 65 65 43  Cell *p1, RtreeC
38958 65 6c 6c 20 2a 70 32 29 7b 0a 20 20 69 6e 74 20  ell *p2){.  int 
38959 69 69 3b 0a 20 20 69 66 28 20 70 52 74 72 65 65  ii;.  if( pRtree
3895a 2d 3e 65 43 6f 6f 72 64 54 79 70 65 3d 3d 52 54  ->eCoordType==RT
3895b 52 45 45 5f 43 4f 4f 52 44 5f 52 45 41 4c 33 32  REE_COORD_REAL32
3895c 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 69 3d 30   ){.    for(ii=0
3895d 3b 20 69 69 3c 28 70 52 74 72 65 65 2d 3e 6e 44  ; ii<(pRtree->nD
3895e 69 6d 2a 32 29 3b 20 69 69 2b 3d 32 29 7b 0a 20  im*2); ii+=2){. 
3895f 20 20 20 20 20 70 31 2d 3e 61 43 6f 6f 72 64 5b       p1->aCoord[
38960 69 69 5d 2e 66 20 3d 20 4d 49 4e 28 70 31 2d 3e  ii].f = MIN(p1->
38961 61 43 6f 6f 72 64 5b 69 69 5d 2e 66 2c 20 70 32  aCoord[ii].f, p2
38962 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 2e 66 29 3b  ->aCoord[ii].f);
38963 0a 20 20 20 20 20 20 70 31 2d 3e 61 43 6f 6f 72  .      p1->aCoor
38964 64 5b 69 69 2b 31 5d 2e 66 20 3d 20 4d 41 58 28  d[ii+1].f = MAX(
38965 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d  p1->aCoord[ii+1]
38966 2e 66 2c 20 70 32 2d 3e 61 43 6f 6f 72 64 5b 69  .f, p2->aCoord[i
38967 69 2b 31 5d 2e 66 29 3b 0a 20 20 20 20 7d 0a 20  i+1].f);.    }. 
38968 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6f 72 28   }else{.    for(
38969 69 69 3d 30 3b 20 69 69 3c 28 70 52 74 72 65 65  ii=0; ii<(pRtree
3896a 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69 2b 3d 32  ->nDim*2); ii+=2
3896b 29 7b 0a 20 20 20 20 20 20 70 31 2d 3e 61 43 6f  ){.      p1->aCo
3896c 6f 72 64 5b 69 69 5d 2e 69 20 3d 20 4d 49 4e 28  ord[ii].i = MIN(
3896d 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 2e 69  p1->aCoord[ii].i
3896e 2c 20 70 32 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d  , p2->aCoord[ii]
3896f 2e 69 29 3b 0a 20 20 20 20 20 20 70 31 2d 3e 61  .i);.      p1->a
38970 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 69 20 3d 20  Coord[ii+1].i = 
38971 4d 41 58 28 70 31 2d 3e 61 43 6f 6f 72 64 5b 69  MAX(p1->aCoord[i
38972 69 2b 31 5d 2e 69 2c 20 70 32 2d 3e 61 43 6f 6f  i+1].i, p2->aCoo
38973 72 64 5b 69 69 2b 31 5d 2e 69 29 3b 0a 20 20 20  rd[ii+1].i);.   
38974 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
38975 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74  Return true if t
38976 68 65 20 61 72 65 61 20 63 6f 76 65 72 65 64 20  he area covered 
38977 62 79 20 70 32 20 69 73 20 61 20 73 75 62 73 65  by p2 is a subse
38978 74 20 6f 66 20 74 68 65 20 61 72 65 61 20 63 6f  t of the area co
38979 76 65 72 65 64 0a 2a 2a 20 62 79 20 70 31 2e 20  vered.** by p1. 
3897a 46 61 6c 73 65 20 6f 74 68 65 72 77 69 73 65 2e  False otherwise.
3897b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
3897c 65 6c 6c 43 6f 6e 74 61 69 6e 73 28 52 74 72 65  ellContains(Rtre
3897d 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65  e *pRtree, Rtree
3897e 43 65 6c 6c 20 2a 70 31 2c 20 52 74 72 65 65 43  Cell *p1, RtreeC
3897f 65 6c 6c 20 2a 70 32 29 7b 0a 20 20 69 6e 74 20  ell *p2){.  int 
38980 69 69 3b 0a 20 20 69 6e 74 20 69 73 49 6e 74 20  ii;.  int isInt 
38981 3d 20 28 70 52 74 72 65 65 2d 3e 65 43 6f 6f 72  = (pRtree->eCoor
38982 64 54 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f 4f  dType==RTREE_COO
38983 52 44 5f 49 4e 54 33 32 29 3b 0a 20 20 66 6f 72  RD_INT32);.  for
38984 28 69 69 3d 30 3b 20 69 69 3c 28 70 52 74 72 65  (ii=0; ii<(pRtre
38985 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69 2b 3d  e->nDim*2); ii+=
38986 32 29 7b 0a 20 20 20 20 52 74 72 65 65 43 6f 6f  2){.    RtreeCoo
38987 72 64 20 2a 61 31 20 3d 20 26 70 31 2d 3e 61 43  rd *a1 = &p1->aC
38988 6f 6f 72 64 5b 69 69 5d 3b 0a 20 20 20 20 52 74  oord[ii];.    Rt
38989 72 65 65 43 6f 6f 72 64 20 2a 61 32 20 3d 20 26  reeCoord *a2 = &
3898a 70 32 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 3b 0a  p2->aCoord[ii];.
3898b 20 20 20 20 69 66 28 20 28 21 69 73 49 6e 74 20      if( (!isInt 
3898c 26 26 20 28 61 32 5b 30 5d 2e 66 3c 61 31 5b 30  && (a2[0].f<a1[0
3898d 5d 2e 66 20 7c 7c 20 61 32 5b 31 5d 2e 66 3e 61  ].f || a2[1].f>a
3898e 31 5b 31 5d 2e 66 29 29 20 0a 20 20 20 20 20 7c  1[1].f)) .     |
3898f 7c 20 28 20 69 73 49 6e 74 20 26 26 20 28 61 32  | ( isInt && (a2
38990 5b 30 5d 2e 69 3c 61 31 5b 30 5d 2e 69 20 7c 7c  [0].i<a1[0].i ||
38991 20 61 32 5b 31 5d 2e 69 3e 61 31 5b 31 5d 2e 69   a2[1].i>a1[1].i
38992 29 29 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  )) .    ){.     
38993 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d   return 0;.    }
38994 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b  .  }.  return 1;
38995 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
38996 20 74 68 65 20 61 6d 6f 75 6e 74 20 63 65 6c 6c   the amount cell
38997 20 70 20 77 6f 75 6c 64 20 67 72 6f 77 20 62 79   p would grow by
38998 20 69 66 20 69 74 20 77 65 72 65 20 75 6e 69 6f   if it were unio
38999 6e 65 64 20 77 69 74 68 20 70 43 65 6c 6c 2e 0a  ned with pCell..
3899a 2a 2f 0a 73 74 61 74 69 63 20 66 6c 6f 61 74 20  */.static float 
3899b 63 65 6c 6c 47 72 6f 77 74 68 28 52 74 72 65 65  cellGrowth(Rtree
3899c 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65 43   *pRtree, RtreeC
3899d 65 6c 6c 20 2a 70 2c 20 52 74 72 65 65 43 65 6c  ell *p, RtreeCel
3899e 6c 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 66 6c 6f  l *pCell){.  flo
3899f 61 74 20 61 72 65 61 3b 0a 20 20 52 74 72 65 65  at area;.  Rtree
389a0 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 6d 65 6d  Cell cell;.  mem
389a1 63 70 79 28 26 63 65 6c 6c 2c 20 70 2c 20 73 69  cpy(&cell, p, si
389a2 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 29  zeof(RtreeCell))
389a3 3b 0a 20 20 61 72 65 61 20 3d 20 63 65 6c 6c 41  ;.  area = cellA
389a4 72 65 61 28 70 52 74 72 65 65 2c 20 26 63 65 6c  rea(pRtree, &cel
389a5 6c 29 3b 0a 20 20 63 65 6c 6c 55 6e 69 6f 6e 28  l);.  cellUnion(
389a6 70 52 74 72 65 65 2c 20 26 63 65 6c 6c 2c 20 70  pRtree, &cell, p
389a7 43 65 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20  Cell);.  return 
389a8 28 63 65 6c 6c 41 72 65 61 28 70 52 74 72 65 65  (cellArea(pRtree
389a9 2c 20 26 63 65 6c 6c 29 2d 61 72 65 61 29 3b 0a  , &cell)-area);.
389aa 7d 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 52  }..#if VARIANT_R
389ab 53 54 41 52 54 52 45 45 5f 43 48 4f 4f 53 45 53  STARTREE_CHOOSES
389ac 55 42 54 52 45 45 20 7c 7c 20 56 41 52 49 41 4e  UBTREE || VARIAN
389ad 54 5f 52 53 54 41 52 54 52 45 45 5f 53 50 4c 49  T_RSTARTREE_SPLI
389ae 54 0a 73 74 61 74 69 63 20 66 6c 6f 61 74 20 63  T.static float c
389af 65 6c 6c 4f 76 65 72 6c 61 70 28 0a 20 20 52 74  ellOverlap(.  Rt
389b0 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20 20  ree *pRtree, .  
389b1 52 74 72 65 65 43 65 6c 6c 20 2a 70 2c 20 0a 20  RtreeCell *p, . 
389b2 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c   RtreeCell *aCel
389b3 6c 2c 20 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c  l, .  int nCell,
389b4 20 0a 20 20 69 6e 74 20 69 45 78 63 6c 75 64 65   .  int iExclude
389b5 0a 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20  .){.  int ii;.  
389b6 66 6c 6f 61 74 20 6f 76 65 72 6c 61 70 20 3d 20  float overlap = 
389b7 30 2e 30 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b  0.0;.  for(ii=0;
389b8 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29   ii<nCell; ii++)
389b9 7b 0a 20 20 20 20 69 66 28 20 69 69 21 3d 69 45  {.    if( ii!=iE
389ba 78 63 6c 75 64 65 20 29 7b 0a 20 20 20 20 20 20  xclude ){.      
389bb 69 6e 74 20 6a 6a 3b 0a 20 20 20 20 20 20 66 6c  int jj;.      fl
389bc 6f 61 74 20 6f 20 3d 20 31 2e 30 3b 0a 20 20 20  oat o = 1.0;.   
389bd 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c     for(jj=0; jj<
389be 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 29  (pRtree->nDim*2)
389bf 3b 20 6a 6a 2b 3d 32 29 7b 0a 20 20 20 20 20 20  ; jj+=2){.      
389c0 20 20 64 6f 75 62 6c 65 20 78 31 3b 0a 20 20 20    double x1;.   
389c1 20 20 20 20 20 64 6f 75 62 6c 65 20 78 32 3b 0a       double x2;.
389c2 0a 20 20 20 20 20 20 20 20 78 31 20 3d 20 4d 41  .        x1 = MA
389c3 58 28 44 43 4f 4f 52 44 28 70 2d 3e 61 43 6f 6f  X(DCOORD(p->aCoo
389c4 72 64 5b 6a 6a 5d 29 2c 20 44 43 4f 4f 52 44 28  rd[jj]), DCOORD(
389c5 61 43 65 6c 6c 5b 69 69 5d 2e 61 43 6f 6f 72 64  aCell[ii].aCoord
389c6 5b 6a 6a 5d 29 29 3b 0a 20 20 20 20 20 20 20 20  [jj]));.        
389c7 78 32 20 3d 20 4d 49 4e 28 44 43 4f 4f 52 44 28  x2 = MIN(DCOORD(
389c8 70 2d 3e 61 43 6f 6f 72 64 5b 6a 6a 2b 31 5d 29  p->aCoord[jj+1])
389c9 2c 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 69  , DCOORD(aCell[i
389ca 69 5d 2e 61 43 6f 6f 72 64 5b 6a 6a 2b 31 5d 29  i].aCoord[jj+1])
389cb 29 3b 0a 0a 20 20 20 20 20 20 20 20 69 66 28 20  );..        if( 
389cc 78 32 3c 78 31 20 29 7b 0a 20 20 20 20 20 20 20  x2<x1 ){.       
389cd 20 20 20 6f 20 3d 20 30 2e 30 3b 0a 20 20 20 20     o = 0.0;.    
389ce 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
389cf 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
389d0 20 20 20 20 20 20 6f 20 3d 20 6f 20 2a 20 28 78        o = o * (x
389d1 32 2d 78 31 29 3b 0a 20 20 20 20 20 20 20 20 7d  2-x1);.        }
389d2 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6f  .      }.      o
389d3 76 65 72 6c 61 70 20 2b 3d 20 6f 3b 0a 20 20 20  verlap += o;.   
389d4 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
389d5 6f 76 65 72 6c 61 70 3b 0a 7d 0a 23 65 6e 64 69  overlap;.}.#endi
389d6 66 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 52  f..#if VARIANT_R
389d7 53 54 41 52 54 52 45 45 5f 43 48 4f 4f 53 45 53  STARTREE_CHOOSES
389d8 55 42 54 52 45 45 0a 73 74 61 74 69 63 20 66 6c  UBTREE.static fl
389d9 6f 61 74 20 63 65 6c 6c 4f 76 65 72 6c 61 70 45  oat cellOverlapE
389da 6e 6c 61 72 67 65 6d 65 6e 74 28 0a 20 20 52 74  nlargement(.  Rt
389db 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20 20  ree *pRtree, .  
389dc 52 74 72 65 65 43 65 6c 6c 20 2a 70 2c 20 0a 20  RtreeCell *p, . 
389dd 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 49 6e 73   RtreeCell *pIns
389de 65 72 74 2c 20 0a 20 20 52 74 72 65 65 43 65 6c  ert, .  RtreeCel
389df 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e 74  l *aCell, .  int
389e0 20 6e 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 69   nCell, .  int i
389e1 45 78 63 6c 75 64 65 0a 29 7b 0a 20 20 66 6c 6f  Exclude.){.  flo
389e2 61 74 20 62 65 66 6f 72 65 3b 0a 20 20 66 6c 6f  at before;.  flo
389e3 61 74 20 61 66 74 65 72 3b 0a 20 20 62 65 66 6f  at after;.  befo
389e4 72 65 20 3d 20 63 65 6c 6c 4f 76 65 72 6c 61 70  re = cellOverlap
389e5 28 70 52 74 72 65 65 2c 20 70 2c 20 61 43 65 6c  (pRtree, p, aCel
389e6 6c 2c 20 6e 43 65 6c 6c 2c 20 69 45 78 63 6c 75  l, nCell, iExclu
389e7 64 65 29 3b 0a 20 20 63 65 6c 6c 55 6e 69 6f 6e  de);.  cellUnion
389e8 28 70 52 74 72 65 65 2c 20 70 2c 20 70 49 6e 73  (pRtree, p, pIns
389e9 65 72 74 29 3b 0a 20 20 61 66 74 65 72 20 3d 20  ert);.  after = 
389ea 63 65 6c 6c 4f 76 65 72 6c 61 70 28 70 52 74 72  cellOverlap(pRtr
389eb 65 65 2c 20 70 2c 20 61 43 65 6c 6c 2c 20 6e 43  ee, p, aCell, nC
389ec 65 6c 6c 2c 20 69 45 78 63 6c 75 64 65 29 3b 0a  ell, iExclude);.
389ed 20 20 72 65 74 75 72 6e 20 61 66 74 65 72 2d 62    return after-b
389ee 65 66 6f 72 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a  efore;.}.#endif.
389ef 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
389f0 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73  ction implements
389f1 20 74 68 65 20 43 68 6f 6f 73 65 4c 65 61 66 20   the ChooseLeaf 
389f2 61 6c 67 6f 72 69 74 68 6d 20 66 72 6f 6d 20 47  algorithm from G
389f3 75 74 6d 61 6e 5b 38 34 5d 2e 0a 2a 2a 20 43 68  utman[84]..** Ch
389f4 6f 6f 73 65 53 75 62 54 72 65 65 20 69 6e 20 72  ooseSubTree in r
389f5 2a 74 72 65 65 20 74 65 72 6d 69 6e 6f 6c 6f 67  *tree terminolog
389f6 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  y..*/.static int
389f7 20 43 68 6f 6f 73 65 4c 65 61 66 28 0a 20 20 52   ChooseLeaf(.  R
389f8 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 20 20  tree *pRtree,   
389f9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
389fa 74 72 65 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20  tree table */.  
389fb 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c  RtreeCell *pCell
389fc 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
389fd 43 65 6c 6c 20 74 6f 20 69 6e 73 65 72 74 20 69  Cell to insert i
389fe 6e 74 6f 20 72 74 72 65 65 20 2a 2f 0a 20 20 69  nto rtree */.  i
389ff 6e 74 20 69 48 65 69 67 68 74 2c 20 20 20 20 20  nt iHeight,     
38a00 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48              /* H
38a01 65 69 67 68 74 20 6f 66 20 73 75 62 2d 74 72 65  eight of sub-tre
38a02 65 20 72 6f 6f 74 65 64 20 61 74 20 70 43 65 6c  e rooted at pCel
38a03 6c 20 2a 2f 0a 20 20 52 74 72 65 65 4e 6f 64 65  l */.  RtreeNode
38a04 20 2a 2a 70 70 4c 65 61 66 20 20 20 20 20 20 20   **ppLeaf       
38a05 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 65 6c 65      /* OUT: Sele
38a06 63 74 65 64 20 6c 65 61 66 20 70 61 67 65 20 2a  cted leaf page *
38a07 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  /.){.  int rc;. 
38a08 20 69 6e 74 20 69 69 3b 0a 20 20 52 74 72 65 65   int ii;.  Rtree
38a09 4e 6f 64 65 20 2a 70 4e 6f 64 65 3b 0a 20 20 72  Node *pNode;.  r
38a0a 63 20 3d 20 6e 6f 64 65 41 63 71 75 69 72 65 28  c = nodeAcquire(
38a0b 70 52 74 72 65 65 2c 20 31 2c 20 30 2c 20 26 70  pRtree, 1, 0, &p
38a0c 4e 6f 64 65 29 3b 0a 0a 20 20 66 6f 72 28 69 69  Node);..  for(ii
38a0d 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  =0; rc==SQLITE_O
38a0e 4b 20 26 26 20 69 69 3c 28 70 52 74 72 65 65 2d  K && ii<(pRtree-
38a0f 3e 69 44 65 70 74 68 2d 69 48 65 69 67 68 74 29  >iDepth-iHeight)
38a10 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74  ; ii++){.    int
38a11 20 69 43 65 6c 6c 3b 0a 20 20 20 20 73 71 6c 69   iCell;.    sqli
38a12 74 65 33 5f 69 6e 74 36 34 20 69 42 65 73 74 3b  te3_int64 iBest;
38a13 0a 0a 20 20 20 20 66 6c 6f 61 74 20 66 4d 69 6e  ..    float fMin
38a14 47 72 6f 77 74 68 3b 0a 20 20 20 20 66 6c 6f 61  Growth;.    floa
38a15 74 20 66 4d 69 6e 41 72 65 61 3b 0a 20 20 20 20  t fMinArea;.    
38a16 66 6c 6f 61 74 20 66 4d 69 6e 4f 76 65 72 6c 61  float fMinOverla
38a17 70 3b 0a 0a 20 20 20 20 69 6e 74 20 6e 43 65 6c  p;..    int nCel
38a18 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f 64 65 29  l = NCELL(pNode)
38a19 3b 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20  ;.    RtreeCell 
38a1a 63 65 6c 6c 3b 0a 20 20 20 20 52 74 72 65 65 4e  cell;.    RtreeN
38a1b 6f 64 65 20 2a 70 43 68 69 6c 64 3b 0a 0a 20 20  ode *pChild;..  
38a1c 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65    RtreeCell *aCe
38a1d 6c 6c 20 3d 20 30 3b 0a 0a 23 69 66 20 56 41 52  ll = 0;..#if VAR
38a1e 49 41 4e 54 5f 52 53 54 41 52 54 52 45 45 5f 43  IANT_RSTARTREE_C
38a1f 48 4f 4f 53 45 53 55 42 54 52 45 45 0a 20 20 20  HOOSESUBTREE.   
38a20 20 69 66 28 20 69 69 3d 3d 28 70 52 74 72 65 65   if( ii==(pRtree
38a21 2d 3e 69 44 65 70 74 68 2d 31 29 20 29 7b 0a 20  ->iDepth-1) ){. 
38a22 20 20 20 20 20 69 6e 74 20 6a 6a 3b 0a 20 20 20       int jj;.   
38a23 20 20 20 61 43 65 6c 6c 20 3d 20 73 71 6c 69 74     aCell = sqlit
38a24 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66  e3_malloc(sizeof
38a25 28 52 74 72 65 65 43 65 6c 6c 29 2a 6e 43 65 6c  (RtreeCell)*nCel
38a26 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 61  l);.      if( !a
38a27 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  Cell ){.        
38a28 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
38a29 4d 3b 0a 20 20 20 20 20 20 20 20 6e 6f 64 65 52  M;.        nodeR
38a2a 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70  elease(pRtree, p
38a2b 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 20 20 70  Node);.        p
38a2c 4e 6f 64 65 20 3d 20 30 3b 0a 20 20 20 20 20 20  Node = 0;.      
38a2d 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20    continue;.    
38a2e 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 6a 6a    }.      for(jj
38a2f 3d 30 3b 20 6a 6a 3c 6e 43 65 6c 6c 3b 20 6a 6a  =0; jj<nCell; jj
38a30 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 6e 6f 64  ++){.        nod
38a31 65 47 65 74 43 65 6c 6c 28 70 52 74 72 65 65 2c  eGetCell(pRtree,
38a32 20 70 4e 6f 64 65 2c 20 6a 6a 2c 20 26 61 43 65   pNode, jj, &aCe
38a33 6c 6c 5b 6a 6a 5d 29 3b 0a 20 20 20 20 20 20 7d  ll[jj]);.      }
38a34 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  .    }.#endif.. 
38a35 20 20 20 2f 2a 20 53 65 6c 65 63 74 20 74 68 65     /* Select the
38a36 20 63 68 69 6c 64 20 6e 6f 64 65 20 77 68 69 63   child node whic
38a37 68 20 77 69 6c 6c 20 62 65 20 65 6e 6c 61 72 67  h will be enlarg
38a38 65 64 20 74 68 65 20 6c 65 61 73 74 20 69 66 20  ed the least if 
38a39 70 43 65 6c 6c 0a 20 20 20 20 2a 2a 20 69 73 20  pCell.    ** is 
38a3a 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 69 74  inserted into it
38a3b 2e 20 52 65 73 6f 6c 76 65 20 74 69 65 73 20 62  . Resolve ties b
38a3c 79 20 63 68 6f 6f 73 69 6e 67 20 74 68 65 20 65  y choosing the e
38a3d 6e 74 72 79 20 77 69 74 68 0a 20 20 20 20 2a 2a  ntry with.    **
38a3e 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 61 72   the smallest ar
38a3f 65 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66  ea..    */.    f
38a40 6f 72 28 69 43 65 6c 6c 3d 30 3b 20 69 43 65 6c  or(iCell=0; iCel
38a41 6c 3c 6e 43 65 6c 6c 3b 20 69 43 65 6c 6c 2b 2b  l<nCell; iCell++
38a42 29 7b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 67  ){.      float g
38a43 72 6f 77 74 68 3b 0a 20 20 20 20 20 20 66 6c 6f  rowth;.      flo
38a44 61 74 20 61 72 65 61 3b 0a 20 20 20 20 20 20 66  at area;.      f
38a45 6c 6f 61 74 20 6f 76 65 72 6c 61 70 20 3d 20 30  loat overlap = 0
38a46 2e 30 3b 0a 20 20 20 20 20 20 6e 6f 64 65 47 65  .0;.      nodeGe
38a47 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e  tCell(pRtree, pN
38a48 6f 64 65 2c 20 69 43 65 6c 6c 2c 20 26 63 65 6c  ode, iCell, &cel
38a49 6c 29 3b 0a 20 20 20 20 20 20 67 72 6f 77 74 68  l);.      growth
38a4a 20 3d 20 63 65 6c 6c 47 72 6f 77 74 68 28 70 52   = cellGrowth(pR
38a4b 74 72 65 65 2c 20 26 63 65 6c 6c 2c 20 70 43 65  tree, &cell, pCe
38a4c 6c 6c 29 3b 0a 20 20 20 20 20 20 61 72 65 61 20  ll);.      area 
38a4d 3d 20 63 65 6c 6c 41 72 65 61 28 70 52 74 72 65  = cellArea(pRtre
38a4e 65 2c 20 26 63 65 6c 6c 29 3b 0a 23 69 66 20 56  e, &cell);.#if V
38a4f 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45 45  ARIANT_RSTARTREE
38a50 5f 43 48 4f 4f 53 45 53 55 42 54 52 45 45 0a 20  _CHOOSESUBTREE. 
38a51 20 20 20 20 20 69 66 28 20 69 69 3d 3d 28 70 52       if( ii==(pR
38a52 74 72 65 65 2d 3e 69 44 65 70 74 68 2d 31 29 20  tree->iDepth-1) 
38a53 29 7b 0a 20 20 20 20 20 20 20 20 6f 76 65 72 6c  ){.        overl
38a54 61 70 20 3d 20 63 65 6c 6c 4f 76 65 72 6c 61 70  ap = cellOverlap
38a55 45 6e 6c 61 72 67 65 6d 65 6e 74 28 70 52 74 72  Enlargement(pRtr
38a56 65 65 2c 26 63 65 6c 6c 2c 70 43 65 6c 6c 2c 61  ee,&cell,pCell,a
38a57 43 65 6c 6c 2c 6e 43 65 6c 6c 2c 69 43 65 6c 6c  Cell,nCell,iCell
38a58 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  );.      }.#endi
38a59 66 0a 20 20 20 20 20 20 69 66 28 20 28 69 43 65  f.      if( (iCe
38a5a 6c 6c 3d 3d 30 29 20 0a 20 20 20 20 20 20 20 7c  ll==0) .       |
38a5b 7c 20 28 6f 76 65 72 6c 61 70 3c 66 4d 69 6e 4f  | (overlap<fMinO
38a5c 76 65 72 6c 61 70 29 20 0a 20 20 20 20 20 20 20  verlap) .       
38a5d 7c 7c 20 28 6f 76 65 72 6c 61 70 3d 3d 66 4d 69  || (overlap==fMi
38a5e 6e 4f 76 65 72 6c 61 70 20 26 26 20 67 72 6f 77  nOverlap && grow
38a5f 74 68 3c 66 4d 69 6e 47 72 6f 77 74 68 29 0a 20  th<fMinGrowth). 
38a60 20 20 20 20 20 20 7c 7c 20 28 6f 76 65 72 6c 61        || (overla
38a61 70 3d 3d 66 4d 69 6e 4f 76 65 72 6c 61 70 20 26  p==fMinOverlap &
38a62 26 20 67 72 6f 77 74 68 3d 3d 66 4d 69 6e 47 72  & growth==fMinGr
38a63 6f 77 74 68 20 26 26 20 61 72 65 61 3c 66 4d 69  owth && area<fMi
38a64 6e 41 72 65 61 29 0a 20 20 20 20 20 20 29 7b 0a  nArea).      ){.
38a65 20 20 20 20 20 20 20 20 66 4d 69 6e 4f 76 65 72          fMinOver
38a66 6c 61 70 20 3d 20 6f 76 65 72 6c 61 70 3b 0a 20  lap = overlap;. 
38a67 20 20 20 20 20 20 20 66 4d 69 6e 47 72 6f 77 74         fMinGrowt
38a68 68 20 3d 20 67 72 6f 77 74 68 3b 0a 20 20 20 20  h = growth;.    
38a69 20 20 20 20 66 4d 69 6e 41 72 65 61 20 3d 20 61      fMinArea = a
38a6a 72 65 61 3b 0a 20 20 20 20 20 20 20 20 69 42 65  rea;.        iBe
38a6b 73 74 20 3d 20 63 65 6c 6c 2e 69 52 6f 77 69 64  st = cell.iRowid
38a6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
38a6d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
38a6e 65 28 61 43 65 6c 6c 29 3b 0a 20 20 20 20 72 63  e(aCell);.    rc
38a6f 20 3d 20 6e 6f 64 65 41 63 71 75 69 72 65 28 70   = nodeAcquire(p
38a70 52 74 72 65 65 2c 20 69 42 65 73 74 2c 20 70 4e  Rtree, iBest, pN
38a71 6f 64 65 2c 20 26 70 43 68 69 6c 64 29 3b 0a 20  ode, &pChild);. 
38a72 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70     nodeRelease(p
38a73 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20  Rtree, pNode);. 
38a74 20 20 20 70 4e 6f 64 65 20 3d 20 70 43 68 69 6c     pNode = pChil
38a75 64 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70 4c 65 61  d;.  }..  *ppLea
38a76 66 20 3d 20 70 4e 6f 64 65 3b 0a 20 20 72 65 74  f = pNode;.  ret
38a77 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
38a78 20 41 20 63 65 6c 6c 20 77 69 74 68 20 74 68 65   A cell with the
38a79 20 73 61 6d 65 20 63 6f 6e 74 65 6e 74 20 61 73   same content as
38a7a 20 70 43 65 6c 6c 20 68 61 73 20 6a 75 73 74 20   pCell has just 
38a7b 62 65 65 6e 20 69 6e 73 65 72 74 65 64 20 69 6e  been inserted in
38a7c 74 6f 0a 2a 2a 20 74 68 65 20 6e 6f 64 65 20 70  to.** the node p
38a7d 4e 6f 64 65 2e 20 54 68 69 73 20 66 75 6e 63 74  Node. This funct
38a7e 69 6f 6e 20 75 70 64 61 74 65 73 20 74 68 65 20  ion updates the 
38a7f 62 6f 75 6e 64 69 6e 67 20 62 6f 78 20 63 65 6c  bounding box cel
38a80 6c 73 20 69 6e 0a 2a 2a 20 61 6c 6c 20 61 6e 63  ls in.** all anc
38a81 65 73 74 6f 72 20 65 6c 65 6d 65 6e 74 73 2e 0a  estor elements..
38a82 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 41  */.static void A
38a83 64 6a 75 73 74 54 72 65 65 28 0a 20 20 52 74 72  djustTree(.  Rtr
38a84 65 65 20 2a 70 52 74 72 65 65 2c 20 20 20 20 20  ee *pRtree,     
38a85 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
38a86 2a 20 52 74 72 65 65 20 74 61 62 6c 65 20 2a 2f  * Rtree table */
38a87 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e  .  RtreeNode *pN
38a88 6f 64 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ode,            
38a89 20 20 20 20 20 2f 2a 20 41 64 6a 75 73 74 20 61       /* Adjust a
38a8a 6e 63 65 73 74 72 79 20 6f 66 20 74 68 69 73 20  ncestry of this 
38a8b 6e 6f 64 65 2e 20 2a 2f 0a 20 20 52 74 72 65 65  node. */.  Rtree
38a8c 43 65 6c 6c 20 2a 70 43 65 6c 6c 20 20 20 20 20  Cell *pCell     
38a8d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
38a8e 54 68 69 73 20 63 65 6c 6c 20 77 61 73 20 6a 75  This cell was ju
38a8f 73 74 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 29  st inserted */.)
38a90 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  {.  RtreeNode *p
38a91 20 3d 20 70 4e 6f 64 65 3b 0a 20 20 77 68 69 6c   = pNode;.  whil
38a92 65 28 20 70 2d 3e 70 50 61 72 65 6e 74 20 29 7b  e( p->pParent ){
38a93 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20 63  .    RtreeCell c
38a94 65 6c 6c 3b 0a 20 20 20 20 52 74 72 65 65 4e 6f  ell;.    RtreeNo
38a95 64 65 20 2a 70 50 61 72 65 6e 74 20 3d 20 70 2d  de *pParent = p-
38a96 3e 70 50 61 72 65 6e 74 3b 0a 20 20 20 20 69 6e  >pParent;.    in
38a97 74 20 69 43 65 6c 6c 20 3d 20 6e 6f 64 65 50 61  t iCell = nodePa
38a98 72 65 6e 74 49 6e 64 65 78 28 70 52 74 72 65 65  rentIndex(pRtree
38a99 2c 20 70 29 3b 0a 0a 20 20 20 20 6e 6f 64 65 47  , p);..    nodeG
38a9a 65 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  etCell(pRtree, p
38a9b 50 61 72 65 6e 74 2c 20 69 43 65 6c 6c 2c 20 26  Parent, iCell, &
38a9c 63 65 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 21  cell);.    if( !
38a9d 63 65 6c 6c 43 6f 6e 74 61 69 6e 73 28 70 52 74  cellContains(pRt
38a9e 72 65 65 2c 20 26 63 65 6c 6c 2c 20 70 43 65 6c  ree, &cell, pCel
38a9f 6c 29 20 29 7b 0a 20 20 20 20 20 20 63 65 6c 6c  l) ){.      cell
38aa0 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20 26 63  Union(pRtree, &c
38aa1 65 6c 6c 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20  ell, pCell);.   
38aa2 20 20 20 6e 6f 64 65 4f 76 65 72 77 72 69 74 65     nodeOverwrite
38aa3 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 50 61  Cell(pRtree, pPa
38aa4 72 65 6e 74 2c 20 26 63 65 6c 6c 2c 20 69 43 65  rent, &cell, iCe
38aa5 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 0a 20 20 20  ll);.    }. .   
38aa6 20 70 20 3d 20 70 50 61 72 65 6e 74 3b 0a 20 20   p = pParent;.  
38aa7 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  }.}../*.** Write
38aa8 20 6d 61 70 70 69 6e 67 20 28 69 52 6f 77 69 64   mapping (iRowid
38aa9 2d 3e 69 4e 6f 64 65 29 20 74 6f 20 74 68 65 20  ->iNode) to the 
38aaa 3c 72 74 72 65 65 3e 5f 72 6f 77 69 64 20 74 61  <rtree>_rowid ta
38aab 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ble..*/.static i
38aac 6e 74 20 72 6f 77 69 64 57 72 69 74 65 28 52 74  nt rowidWrite(Rt
38aad 72 65 65 20 2a 70 52 74 72 65 65 2c 20 73 71 6c  ree *pRtree, sql
38aae 69 74 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 69  ite3_int64 iRowi
38aaf 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  d, sqlite3_int64
38ab0 20 69 4e 6f 64 65 29 7b 0a 20 20 73 71 6c 69 74   iNode){.  sqlit
38ab1 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52  e3_bind_int64(pR
38ab2 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77 69  tree->pWriteRowi
38ab3 64 2c 20 31 2c 20 69 52 6f 77 69 64 29 3b 0a 20  d, 1, iRowid);. 
38ab4 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
38ab5 74 36 34 28 70 52 74 72 65 65 2d 3e 70 57 72 69  t64(pRtree->pWri
38ab6 74 65 52 6f 77 69 64 2c 20 32 2c 20 69 4e 6f 64  teRowid, 2, iNod
38ab7 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  e);.  sqlite3_st
38ab8 65 70 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74  ep(pRtree->pWrit
38ab9 65 52 6f 77 69 64 29 3b 0a 20 20 72 65 74 75 72  eRowid);.  retur
38aba 6e 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28  n sqlite3_reset(
38abb 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f  pRtree->pWriteRo
38abc 77 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57  wid);.}../*.** W
38abd 72 69 74 65 20 6d 61 70 70 69 6e 67 20 28 69 4e  rite mapping (iN
38abe 6f 64 65 2d 3e 69 50 61 72 29 20 74 6f 20 74 68  ode->iPar) to th
38abf 65 20 3c 72 74 72 65 65 3e 5f 70 61 72 65 6e 74  e <rtree>_parent
38ac0 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69   table..*/.stati
38ac1 63 20 69 6e 74 20 70 61 72 65 6e 74 57 72 69 74  c int parentWrit
38ac2 65 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c  e(Rtree *pRtree,
38ac3 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69   sqlite3_int64 i
38ac4 4e 6f 64 65 2c 20 73 71 6c 69 74 65 33 5f 69 6e  Node, sqlite3_in
38ac5 74 36 34 20 69 50 61 72 29 7b 0a 20 20 73 71 6c  t64 iPar){.  sql
38ac6 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
38ac7 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 50 61  pRtree->pWritePa
38ac8 72 65 6e 74 2c 20 31 2c 20 69 4e 6f 64 65 29 3b  rent, 1, iNode);
38ac9 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
38aca 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 57  int64(pRtree->pW
38acb 72 69 74 65 50 61 72 65 6e 74 2c 20 32 2c 20 69  riteParent, 2, i
38acc 50 61 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  Par);.  sqlite3_
38acd 73 74 65 70 28 70 52 74 72 65 65 2d 3e 70 57 72  step(pRtree->pWr
38ace 69 74 65 50 61 72 65 6e 74 29 3b 0a 20 20 72 65  iteParent);.  re
38acf 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 72 65 73  turn sqlite3_res
38ad0 65 74 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74  et(pRtree->pWrit
38ad1 65 50 61 72 65 6e 74 29 3b 0a 7d 0a 0a 73 74 61  eParent);.}..sta
38ad2 74 69 63 20 69 6e 74 20 72 74 72 65 65 49 6e 73  tic int rtreeIns
38ad3 65 72 74 43 65 6c 6c 28 52 74 72 65 65 20 2a 2c  ertCell(Rtree *,
38ad4 20 52 74 72 65 65 4e 6f 64 65 20 2a 2c 20 52 74   RtreeNode *, Rt
38ad5 72 65 65 43 65 6c 6c 20 2a 2c 20 69 6e 74 29 3b  reeCell *, int);
38ad6 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 47 55  ..#if VARIANT_GU
38ad7 54 54 4d 41 4e 5f 4c 49 4e 45 41 52 5f 53 50 4c  TTMAN_LINEAR_SPL
38ad8 49 54 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  IT./*.** Impleme
38ad9 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c  ntation of the l
38ada 69 6e 65 61 72 20 76 61 72 69 61 6e 74 20 6f 66  inear variant of
38adb 20 74 68 65 20 50 69 63 6b 4e 65 78 74 28 29 20   the PickNext() 
38adc 66 75 6e 63 74 69 6f 6e 20 66 72 6f 6d 0a 2a 2a  function from.**
38add 20 47 75 74 74 6d 61 6e 5b 38 34 5d 2e 0a 2a 2f   Guttman[84]..*/
38ade 0a 73 74 61 74 69 63 20 52 74 72 65 65 43 65 6c  .static RtreeCel
38adf 6c 20 2a 4c 69 6e 65 61 72 50 69 63 6b 4e 65 78  l *LinearPickNex
38ae0 74 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  t(.  Rtree *pRtr
38ae1 65 65 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20  ee,.  RtreeCell 
38ae2 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 6e  *aCell, .  int n
38ae3 43 65 6c 6c 2c 20 0a 20 20 52 74 72 65 65 43 65  Cell, .  RtreeCe
38ae4 6c 6c 20 2a 70 4c 65 66 74 42 6f 78 2c 20 0a 20  ll *pLeftBox, . 
38ae5 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 52 69 67   RtreeCell *pRig
38ae6 68 74 42 6f 78 2c 0a 20 20 69 6e 74 20 2a 61 69  htBox,.  int *ai
38ae7 55 73 65 64 0a 29 7b 0a 20 20 69 6e 74 20 69 69  Used.){.  int ii
38ae8 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 61 69  ;.  for(ii=0; ai
38ae9 55 73 65 64 5b 69 69 5d 3b 20 69 69 2b 2b 29 3b  Used[ii]; ii++);
38aea 0a 20 20 61 69 55 73 65 64 5b 69 69 5d 20 3d 20  .  aiUsed[ii] = 
38aeb 31 3b 0a 20 20 72 65 74 75 72 6e 20 26 61 43 65  1;.  return &aCe
38aec 6c 6c 5b 69 69 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ll[ii];.}../*.**
38aed 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
38aee 6f 66 20 74 68 65 20 6c 69 6e 65 61 72 20 76 61  of the linear va
38aef 72 69 61 6e 74 20 6f 66 20 74 68 65 20 50 69 63  riant of the Pic
38af0 6b 53 65 65 64 73 28 29 20 66 75 6e 63 74 69 6f  kSeeds() functio
38af1 6e 20 66 72 6f 6d 0a 2a 2a 20 47 75 74 74 6d 61  n from.** Guttma
38af2 6e 5b 38 34 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63  n[84]..*/.static
38af3 20 76 6f 69 64 20 4c 69 6e 65 61 72 50 69 63 6b   void LinearPick
38af4 53 65 65 64 73 28 0a 20 20 52 74 72 65 65 20 2a  Seeds(.  Rtree *
38af5 70 52 74 72 65 65 2c 0a 20 20 52 74 72 65 65 43  pRtree,.  RtreeC
38af6 65 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20 69  ell *aCell, .  i
38af7 6e 74 20 6e 43 65 6c 6c 2c 20 0a 20 20 69 6e 74  nt nCell, .  int
38af8 20 2a 70 69 4c 65 66 74 53 65 65 64 2c 20 0a 20   *piLeftSeed, . 
38af9 20 69 6e 74 20 2a 70 69 52 69 67 68 74 53 65 65   int *piRightSee
38afa 64 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  d.){.  int i;.  
38afb 69 6e 74 20 69 4c 65 66 74 53 65 65 64 20 3d 20  int iLeftSeed = 
38afc 30 3b 0a 20 20 69 6e 74 20 69 52 69 67 68 74 53  0;.  int iRightS
38afd 65 65 64 20 3d 20 31 3b 0a 20 20 66 6c 6f 61 74  eed = 1;.  float
38afe 20 6d 61 78 4e 6f 72 6d 61 6c 49 6e 6e 65 72 57   maxNormalInnerW
38aff 69 64 74 68 20 3d 20 30 2e 30 3b 0a 0a 20 20 2f  idth = 0.0;..  /
38b00 2a 20 50 69 63 6b 20 74 77 6f 20 22 73 65 65 64  * Pick two "seed
38b01 22 20 63 65 6c 6c 73 20 66 72 6f 6d 20 74 68 65  " cells from the
38b02 20 61 72 72 61 79 20 6f 66 20 63 65 6c 6c 73 2e   array of cells.
38b03 20 54 68 65 20 61 6c 67 6f 72 69 74 68 6d 20 75   The algorithm u
38b04 73 65 64 0a 20 20 2a 2a 20 68 65 72 65 20 69 73  sed.  ** here is
38b05 20 74 68 65 20 4c 69 6e 65 61 72 50 69 63 6b 53   the LinearPickS
38b06 65 65 64 73 20 61 6c 67 6f 72 69 74 68 6d 20 66  eeds algorithm f
38b07 72 6f 6d 20 47 75 74 6d 61 6e 5b 31 39 38 34 5d  rom Gutman[1984]
38b08 2e 20 54 68 65 20 0a 20 20 2a 2a 20 69 6e 64 69  . The .  ** indi
38b09 63 65 73 20 6f 66 20 74 68 65 20 74 77 6f 20 73  ces of the two s
38b0a 65 65 64 20 63 65 6c 6c 73 20 69 6e 20 74 68 65  eed cells in the
38b0b 20 61 72 72 61 79 20 61 72 65 20 73 74 6f 72 65   array are store
38b0c 64 20 69 6e 20 6c 6f 63 61 6c 0a 20 20 2a 2a 20  d in local.  ** 
38b0d 76 61 72 69 61 62 6c 65 73 20 69 4c 65 66 74 53  variables iLeftS
38b0e 65 65 6b 20 61 6e 64 20 69 52 69 67 68 74 53 65  eek and iRightSe
38b0f 65 64 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69  ed..  */.  for(i
38b10 3d 30 3b 20 69 3c 70 52 74 72 65 65 2d 3e 6e 44  =0; i<pRtree->nD
38b11 69 6d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 66 6c  im; i++){.    fl
38b12 6f 61 74 20 78 31 20 3d 20 61 43 65 6c 6c 5b 30  oat x1 = aCell[0
38b13 5d 2e 61 43 6f 6f 72 64 5b 69 2a 32 5d 3b 0a 20  ].aCoord[i*2];. 
38b14 20 20 20 66 6c 6f 61 74 20 78 32 20 3d 20 61 43     float x2 = aC
38b15 65 6c 6c 5b 30 5d 2e 61 43 6f 6f 72 64 5b 69 2a  ell[0].aCoord[i*
38b16 32 2b 31 5d 3b 0a 20 20 20 20 66 6c 6f 61 74 20  2+1];.    float 
38b17 78 33 20 3d 20 78 31 3b 0a 20 20 20 20 66 6c 6f  x3 = x1;.    flo
38b18 61 74 20 78 34 20 3d 20 78 32 3b 0a 20 20 20 20  at x4 = x2;.    
38b19 69 6e 74 20 6a 6a 3b 0a 0a 20 20 20 20 69 6e 74  int jj;..    int
38b1a 20 69 43 65 6c 6c 4c 65 66 74 20 3d 20 30 3b 0a   iCellLeft = 0;.
38b1b 20 20 20 20 69 6e 74 20 69 43 65 6c 6c 52 69 67      int iCellRig
38b1c 68 74 20 3d 20 30 3b 0a 0a 20 20 20 20 66 6f 72  ht = 0;..    for
38b1d 28 6a 6a 3d 31 3b 20 6a 6a 3c 6e 43 65 6c 6c 3b  (jj=1; jj<nCell;
38b1e 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 66 6c   jj++){.      fl
38b1f 6f 61 74 20 6c 65 66 74 20 3d 20 61 43 65 6c 6c  oat left = aCell
38b20 5b 6a 6a 5d 2e 61 43 6f 6f 72 64 5b 69 2a 32 5d  [jj].aCoord[i*2]
38b21 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 72 69  ;.      float ri
38b22 67 68 74 20 3d 20 61 43 65 6c 6c 5b 6a 6a 5d 2e  ght = aCell[jj].
38b23 61 43 6f 6f 72 64 5b 69 2a 32 2b 31 5d 3b 0a 0a  aCoord[i*2+1];..
38b24 20 20 20 20 20 20 69 66 28 20 6c 65 66 74 3c 78        if( left<x
38b25 31 20 29 20 78 31 20 3d 20 6c 65 66 74 3b 0a 20  1 ) x1 = left;. 
38b26 20 20 20 20 20 69 66 28 20 72 69 67 68 74 3e 78       if( right>x
38b27 34 20 29 20 78 34 20 3d 20 72 69 67 68 74 3b 0a  4 ) x4 = right;.
38b28 20 20 20 20 20 20 69 66 28 20 6c 65 66 74 3e 78        if( left>x
38b29 33 20 29 7b 0a 20 20 20 20 20 20 20 20 78 33 20  3 ){.        x3 
38b2a 3d 20 6c 65 66 74 3b 0a 20 20 20 20 20 20 20 20  = left;.        
38b2b 69 43 65 6c 6c 52 69 67 68 74 20 3d 20 6a 6a 3b  iCellRight = jj;
38b2c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
38b2d 66 28 20 72 69 67 68 74 3c 78 32 20 29 7b 0a 20  f( right<x2 ){. 
38b2e 20 20 20 20 20 20 20 78 32 20 3d 20 72 69 67 68         x2 = righ
38b2f 74 3b 0a 20 20 20 20 20 20 20 20 69 43 65 6c 6c  t;.        iCell
38b30 4c 65 66 74 20 3d 20 6a 6a 3b 0a 20 20 20 20 20  Left = jj;.     
38b31 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66   }.    }..    if
38b32 28 20 78 34 21 3d 78 31 20 29 7b 0a 20 20 20 20  ( x4!=x1 ){.    
38b33 20 20 66 6c 6f 61 74 20 6e 6f 72 6d 61 6c 77 69    float normalwi
38b34 64 74 68 20 3d 20 28 78 33 20 2d 20 78 32 29 20  dth = (x3 - x2) 
38b35 2f 20 28 78 34 20 2d 20 78 31 29 3b 0a 20 20 20  / (x4 - x1);.   
38b36 20 20 20 69 66 28 20 6e 6f 72 6d 61 6c 77 69 64     if( normalwid
38b37 74 68 3e 6d 61 78 4e 6f 72 6d 61 6c 49 6e 6e 65  th>maxNormalInne
38b38 72 57 69 64 74 68 20 29 7b 0a 20 20 20 20 20 20  rWidth ){.      
38b39 20 20 69 4c 65 66 74 53 65 65 64 20 3d 20 69 43    iLeftSeed = iC
38b3a 65 6c 6c 4c 65 66 74 3b 0a 20 20 20 20 20 20 20  ellLeft;.       
38b3b 20 69 52 69 67 68 74 53 65 65 64 20 3d 20 69 43   iRightSeed = iC
38b3c 65 6c 6c 52 69 67 68 74 3b 0a 20 20 20 20 20 20  ellRight;.      
38b3d 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a  }.    }.  }..  *
38b3e 70 69 4c 65 66 74 53 65 65 64 20 3d 20 69 4c 65  piLeftSeed = iLe
38b3f 66 74 53 65 65 64 3b 0a 20 20 2a 70 69 52 69 67  ftSeed;.  *piRig
38b40 68 74 53 65 65 64 20 3d 20 69 52 69 67 68 74 53  htSeed = iRightS
38b41 65 65 64 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  eed;.}.#endif /*
38b42 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e   VARIANT_GUTTMAN
38b43 5f 4c 49 4e 45 41 52 5f 53 50 4c 49 54 20 2a 2f  _LINEAR_SPLIT */
38b44 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 47 55  ..#if VARIANT_GU
38b45 54 54 4d 41 4e 5f 51 55 41 44 52 41 54 49 43 5f  TTMAN_QUADRATIC_
38b46 53 50 4c 49 54 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c  SPLIT./*.** Impl
38b47 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
38b48 65 20 71 75 61 64 72 61 74 69 63 20 76 61 72 69  e quadratic vari
38b49 61 6e 74 20 6f 66 20 74 68 65 20 50 69 63 6b 4e  ant of the PickN
38b4a 65 78 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 66  ext() function f
38b4b 72 6f 6d 0a 2a 2a 20 47 75 74 74 6d 61 6e 5b 38  rom.** Guttman[8
38b4c 34 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 52 74  4]..*/.static Rt
38b4d 72 65 65 43 65 6c 6c 20 2a 51 75 61 64 72 61 74  reeCell *Quadrat
38b4e 69 63 50 69 63 6b 4e 65 78 74 28 0a 20 20 52 74  icPickNext(.  Rt
38b4f 72 65 65 20 2a 70 52 74 72 65 65 2c 0a 20 20 52  ree *pRtree,.  R
38b50 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c  treeCell *aCell,
38b51 20 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 20 0a   .  int nCell, .
38b52 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 4c 65    RtreeCell *pLe
38b53 66 74 42 6f 78 2c 20 0a 20 20 52 74 72 65 65 43  ftBox, .  RtreeC
38b54 65 6c 6c 20 2a 70 52 69 67 68 74 42 6f 78 2c 0a  ell *pRightBox,.
38b55 20 20 69 6e 74 20 2a 61 69 55 73 65 64 0a 29 7b    int *aiUsed.){
38b56 0a 20 20 23 64 65 66 69 6e 65 20 46 41 42 53 28  .  #define FABS(
38b57 61 29 20 28 28 61 29 3c 30 2e 30 3f 2d 31 2e 30  a) ((a)<0.0?-1.0
38b58 2a 28 61 29 3a 28 61 29 29 0a 0a 20 20 69 6e 74  *(a):(a))..  int
38b59 20 69 53 65 6c 65 63 74 20 3d 20 2d 31 3b 0a 20   iSelect = -1;. 
38b5a 20 66 6c 6f 61 74 20 66 44 69 66 66 3b 0a 20 20   float fDiff;.  
38b5b 69 6e 74 20 69 69 3b 0a 20 20 66 6f 72 28 69 69  int ii;.  for(ii
38b5c 3d 30 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69  =0; ii<nCell; ii
38b5d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 69 55  ++){.    if( aiU
38b5e 73 65 64 5b 69 69 5d 3d 3d 30 20 29 7b 0a 20 20  sed[ii]==0 ){.  
38b5f 20 20 20 20 66 6c 6f 61 74 20 6c 65 66 74 20 3d      float left =
38b60 20 63 65 6c 6c 47 72 6f 77 74 68 28 70 52 74 72   cellGrowth(pRtr
38b61 65 65 2c 20 70 4c 65 66 74 42 6f 78 2c 20 26 61  ee, pLeftBox, &a
38b62 43 65 6c 6c 5b 69 69 5d 29 3b 0a 20 20 20 20 20  Cell[ii]);.     
38b63 20 66 6c 6f 61 74 20 72 69 67 68 74 20 3d 20 63   float right = c
38b64 65 6c 6c 47 72 6f 77 74 68 28 70 52 74 72 65 65  ellGrowth(pRtree
38b65 2c 20 70 4c 65 66 74 42 6f 78 2c 20 26 61 43 65  , pLeftBox, &aCe
38b66 6c 6c 5b 69 69 5d 29 3b 0a 20 20 20 20 20 20 66  ll[ii]);.      f
38b67 6c 6f 61 74 20 64 69 66 66 20 3d 20 46 41 42 53  loat diff = FABS
38b68 28 72 69 67 68 74 2d 6c 65 66 74 29 3b 0a 20 20  (right-left);.  
38b69 20 20 20 20 69 66 28 20 69 53 65 6c 65 63 74 3c      if( iSelect<
38b6a 30 20 7c 7c 20 64 69 66 66 3e 66 44 69 66 66 20  0 || diff>fDiff 
38b6b 29 7b 0a 20 20 20 20 20 20 20 20 66 44 69 66 66  ){.        fDiff
38b6c 20 3d 20 64 69 66 66 3b 0a 20 20 20 20 20 20 20   = diff;.       
38b6d 20 69 53 65 6c 65 63 74 20 3d 20 69 69 3b 0a 20   iSelect = ii;. 
38b6e 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
38b6f 0a 20 20 61 69 55 73 65 64 5b 69 53 65 6c 65 63  .  aiUsed[iSelec
38b70 74 5d 20 3d 20 31 3b 0a 20 20 72 65 74 75 72 6e  t] = 1;.  return
38b71 20 26 61 43 65 6c 6c 5b 69 53 65 6c 65 63 74 5d   &aCell[iSelect]
38b72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  ;.}../*.** Imple
38b73 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
38b74 20 71 75 61 64 72 61 74 69 63 20 76 61 72 69 61   quadratic varia
38b75 6e 74 20 6f 66 20 74 68 65 20 50 69 63 6b 53 65  nt of the PickSe
38b76 65 64 73 28 29 20 66 75 6e 63 74 69 6f 6e 20 66  eds() function f
38b77 72 6f 6d 0a 2a 2a 20 47 75 74 74 6d 61 6e 5b 38  rom.** Guttman[8
38b78 34 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  4]..*/.static vo
38b79 69 64 20 51 75 61 64 72 61 74 69 63 50 69 63 6b  id QuadraticPick
38b7a 53 65 65 64 73 28 0a 20 20 52 74 72 65 65 20 2a  Seeds(.  Rtree *
38b7b 70 52 74 72 65 65 2c 0a 20 20 52 74 72 65 65 43  pRtree,.  RtreeC
38b7c 65 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20 69  ell *aCell, .  i
38b7d 6e 74 20 6e 43 65 6c 6c 2c 20 0a 20 20 69 6e 74  nt nCell, .  int
38b7e 20 2a 70 69 4c 65 66 74 53 65 65 64 2c 20 0a 20   *piLeftSeed, . 
38b7f 20 69 6e 74 20 2a 70 69 52 69 67 68 74 53 65 65   int *piRightSee
38b80 64 0a 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20  d.){.  int ii;. 
38b81 20 69 6e 74 20 6a 6a 3b 0a 0a 20 20 69 6e 74 20   int jj;..  int 
38b82 69 4c 65 66 74 53 65 65 64 20 3d 20 30 3b 0a 20  iLeftSeed = 0;. 
38b83 20 69 6e 74 20 69 52 69 67 68 74 53 65 65 64 20   int iRightSeed 
38b84 3d 20 31 3b 0a 20 20 66 6c 6f 61 74 20 66 57 61  = 1;.  float fWa
38b85 73 74 65 20 3d 20 30 2e 30 3b 0a 0a 20 20 66 6f  ste = 0.0;..  fo
38b86 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43 65 6c 6c  r(ii=0; ii<nCell
38b87 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 66 6f 72  ; ii++){.    for
38b88 28 6a 6a 3d 69 69 2b 31 3b 20 6a 6a 3c 6e 43 65  (jj=ii+1; jj<nCe
38b89 6c 6c 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20  ll; jj++){.     
38b8a 20 66 6c 6f 61 74 20 72 69 67 68 74 20 3d 20 63   float right = c
38b8b 65 6c 6c 41 72 65 61 28 70 52 74 72 65 65 2c 20  ellArea(pRtree, 
38b8c 26 61 43 65 6c 6c 5b 6a 6a 5d 29 3b 0a 20 20 20  &aCell[jj]);.   
38b8d 20 20 20 66 6c 6f 61 74 20 67 72 6f 77 74 68 20     float growth 
38b8e 3d 20 63 65 6c 6c 47 72 6f 77 74 68 28 70 52 74  = cellGrowth(pRt
38b8f 72 65 65 2c 20 26 61 43 65 6c 6c 5b 69 69 5d 2c  ree, &aCell[ii],
38b90 20 26 61 43 65 6c 6c 5b 6a 6a 5d 29 3b 0a 20 20   &aCell[jj]);.  
38b91 20 20 20 20 66 6c 6f 61 74 20 77 61 73 74 65 20      float waste 
38b92 3d 20 67 72 6f 77 74 68 20 2d 20 72 69 67 68 74  = growth - right
38b93 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 77 61 73  ;..      if( was
38b94 74 65 3e 66 57 61 73 74 65 20 29 7b 0a 20 20 20  te>fWaste ){.   
38b95 20 20 20 20 20 69 4c 65 66 74 53 65 65 64 20 3d       iLeftSeed =
38b96 20 69 69 3b 0a 20 20 20 20 20 20 20 20 69 52 69   ii;.        iRi
38b97 67 68 74 53 65 65 64 20 3d 20 6a 6a 3b 0a 20 20  ghtSeed = jj;.  
38b98 20 20 20 20 20 20 66 57 61 73 74 65 20 3d 20 77        fWaste = w
38b99 61 73 74 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20  aste;.      }.  
38b9a 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 69 4c 65    }.  }..  *piLe
38b9b 66 74 53 65 65 64 20 3d 20 69 4c 65 66 74 53 65  ftSeed = iLeftSe
38b9c 65 64 3b 0a 20 20 2a 70 69 52 69 67 68 74 53 65  ed;.  *piRightSe
38b9d 65 64 20 3d 20 69 52 69 67 68 74 53 65 65 64 3b  ed = iRightSeed;
38b9e 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 56 41 52  .}.#endif /* VAR
38b9f 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 51 55 41  IANT_GUTTMAN_QUA
38ba0 44 52 41 54 49 43 5f 53 50 4c 49 54 20 2a 2f 0a  DRATIC_SPLIT */.
38ba1 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 73  ./*.** Arguments
38ba2 20 61 49 64 78 2c 20 61 44 69 73 74 61 6e 63 65   aIdx, aDistance
38ba3 20 61 6e 64 20 61 53 70 61 72 65 20 61 6c 6c 20   and aSpare all 
38ba4 70 6f 69 6e 74 20 74 6f 20 61 72 72 61 79 73 20  point to arrays 
38ba5 6f 66 20 73 69 7a 65 0a 2a 2a 20 6e 49 64 78 2e  of size.** nIdx.
38ba6 20 54 68 65 20 61 49 64 78 20 61 72 72 61 79 20   The aIdx array 
38ba7 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 73 65 74  contains the set
38ba8 20 6f 66 20 69 6e 74 65 67 65 72 73 20 66 72 6f   of integers fro
38ba9 6d 20 30 20 74 6f 20 0a 2a 2a 20 28 6e 49 64 78  m 0 to .** (nIdx
38baa 2d 31 29 20 69 6e 20 6e 6f 20 70 61 72 74 69 63  -1) in no partic
38bab 75 6c 61 72 20 6f 72 64 65 72 2e 20 54 68 69 73  ular order. This
38bac 20 66 75 6e 63 74 69 6f 6e 20 73 6f 72 74 73 20   function sorts 
38bad 74 68 65 20 76 61 6c 75 65 73 0a 2a 2a 20 69 6e  the values.** in
38bae 20 61 49 64 78 20 61 63 63 6f 72 64 69 6e 67 20   aIdx according 
38baf 74 6f 20 74 68 65 20 69 6e 64 65 78 65 64 20 76  to the indexed v
38bb0 61 6c 75 65 73 20 69 6e 20 61 44 69 73 74 61 6e  alues in aDistan
38bb1 63 65 2e 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70  ce. For.** examp
38bb2 6c 65 2c 20 61 73 73 75 6d 69 6e 67 20 74 68 65  le, assuming the
38bb3 20 69 6e 70 75 74 73 3a 0a 2a 2a 0a 2a 2a 20 20   inputs:.**.**  
38bb4 20 61 49 64 78 20 20 20 20 20 20 3d 20 7b 20 30   aIdx      = { 0
38bb5 2c 20 20 20 31 2c 20 20 20 32 2c 20 20 20 33 20  ,   1,   2,   3 
38bb6 7d 0a 2a 2a 20 20 20 61 44 69 73 74 61 6e 63 65  }.**   aDistance
38bb7 20 3d 20 7b 20 35 2e 30 2c 20 32 2e 30 2c 20 37   = { 5.0, 2.0, 7
38bb8 2e 30 2c 20 36 2e 30 20 7d 0a 2a 2a 0a 2a 2a 20  .0, 6.0 }.**.** 
38bb9 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 65  this function se
38bba 74 73 20 74 68 65 20 61 49 64 78 20 61 72 72 61  ts the aIdx arra
38bbb 79 20 74 6f 20 63 6f 6e 74 61 69 6e 3a 0a 2a 2a  y to contain:.**
38bbc 0a 2a 2a 20 20 20 61 49 64 78 20 20 20 20 20 20  .**   aIdx      
38bbd 3d 20 7b 20 30 2c 20 20 20 31 2c 20 20 20 32 2c  = { 0,   1,   2,
38bbe 20 20 20 33 20 7d 0a 2a 2a 0a 2a 2a 20 54 68 65     3 }.**.** The
38bbf 20 61 53 70 61 72 65 20 61 72 72 61 79 20 69 73   aSpare array is
38bc0 20 75 73 65 64 20 61 73 20 74 65 6d 70 6f 72 61   used as tempora
38bc1 72 79 20 77 6f 72 6b 69 6e 67 20 73 70 61 63 65  ry working space
38bc2 20 62 79 20 74 68 65 0a 2a 2a 20 73 6f 72 74 69   by the.** sorti
38bc3 6e 67 20 61 6c 67 6f 72 69 74 68 6d 2e 0a 2a 2f  ng algorithm..*/
38bc4 0a 73 74 61 74 69 63 20 76 6f 69 64 20 53 6f 72  .static void Sor
38bc5 74 42 79 44 69 73 74 61 6e 63 65 28 0a 20 20 69  tByDistance(.  i
38bc6 6e 74 20 2a 61 49 64 78 2c 20 0a 20 20 69 6e 74  nt *aIdx, .  int
38bc7 20 6e 49 64 78 2c 20 0a 20 20 66 6c 6f 61 74 20   nIdx, .  float 
38bc8 2a 61 44 69 73 74 61 6e 63 65 2c 20 0a 20 20 69  *aDistance, .  i
38bc9 6e 74 20 2a 61 53 70 61 72 65 0a 29 7b 0a 20 20  nt *aSpare.){.  
38bca 69 66 28 20 6e 49 64 78 3e 31 20 29 7b 0a 20 20  if( nIdx>1 ){.  
38bcb 20 20 69 6e 74 20 69 4c 65 66 74 20 3d 20 30 3b    int iLeft = 0;
38bcc 0a 20 20 20 20 69 6e 74 20 69 52 69 67 68 74 20  .    int iRight 
38bcd 3d 20 30 3b 0a 0a 20 20 20 20 69 6e 74 20 6e 4c  = 0;..    int nL
38bce 65 66 74 20 3d 20 6e 49 64 78 2f 32 3b 0a 20 20  eft = nIdx/2;.  
38bcf 20 20 69 6e 74 20 6e 52 69 67 68 74 20 3d 20 6e    int nRight = n
38bd0 49 64 78 2d 6e 4c 65 66 74 3b 0a 20 20 20 20 69  Idx-nLeft;.    i
38bd1 6e 74 20 2a 61 4c 65 66 74 20 3d 20 61 49 64 78  nt *aLeft = aIdx
38bd2 3b 0a 20 20 20 20 69 6e 74 20 2a 61 52 69 67 68  ;.    int *aRigh
38bd3 74 20 3d 20 26 61 49 64 78 5b 6e 4c 65 66 74 5d  t = &aIdx[nLeft]
38bd4 3b 0a 0a 20 20 20 20 53 6f 72 74 42 79 44 69 73  ;..    SortByDis
38bd5 74 61 6e 63 65 28 61 4c 65 66 74 2c 20 6e 4c 65  tance(aLeft, nLe
38bd6 66 74 2c 20 61 44 69 73 74 61 6e 63 65 2c 20 61  ft, aDistance, a
38bd7 53 70 61 72 65 29 3b 0a 20 20 20 20 53 6f 72 74  Spare);.    Sort
38bd8 42 79 44 69 73 74 61 6e 63 65 28 61 52 69 67 68  ByDistance(aRigh
38bd9 74 2c 20 6e 52 69 67 68 74 2c 20 61 44 69 73 74  t, nRight, aDist
38bda 61 6e 63 65 2c 20 61 53 70 61 72 65 29 3b 0a 0a  ance, aSpare);..
38bdb 20 20 20 20 6d 65 6d 63 70 79 28 61 53 70 61 72      memcpy(aSpar
38bdc 65 2c 20 61 4c 65 66 74 2c 20 73 69 7a 65 6f 66  e, aLeft, sizeof
38bdd 28 69 6e 74 29 2a 6e 4c 65 66 74 29 3b 0a 20 20  (int)*nLeft);.  
38bde 20 20 61 4c 65 66 74 20 3d 20 61 53 70 61 72 65    aLeft = aSpare
38bdf 3b 0a 0a 20 20 20 20 77 68 69 6c 65 28 20 69 4c  ;..    while( iL
38be0 65 66 74 3c 6e 4c 65 66 74 20 7c 7c 20 69 52 69  eft<nLeft || iRi
38be1 67 68 74 3c 6e 52 69 67 68 74 20 29 7b 0a 20 20  ght<nRight ){.  
38be2 20 20 20 20 69 66 28 20 69 4c 65 66 74 3d 3d 6e      if( iLeft==n
38be3 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Left ){.        
38be4 61 49 64 78 5b 69 4c 65 66 74 2b 69 52 69 67 68  aIdx[iLeft+iRigh
38be5 74 5d 20 3d 20 61 52 69 67 68 74 5b 69 52 69 67  t] = aRight[iRig
38be6 68 74 5d 3b 0a 20 20 20 20 20 20 20 20 69 52 69  ht];.        iRi
38be7 67 68 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c  ght++;.      }el
38be8 73 65 20 69 66 28 20 69 52 69 67 68 74 3d 3d 6e  se if( iRight==n
38be9 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 20  Right ){.       
38bea 20 61 49 64 78 5b 69 4c 65 66 74 2b 69 52 69 67   aIdx[iLeft+iRig
38beb 68 74 5d 20 3d 20 61 4c 65 66 74 5b 69 4c 65 66  ht] = aLeft[iLef
38bec 74 5d 3b 0a 20 20 20 20 20 20 20 20 69 4c 65 66  t];.        iLef
38bed 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  t++;.      }else
38bee 7b 0a 20 20 20 20 20 20 20 20 66 6c 6f 61 74 20  {.        float 
38bef 66 4c 65 66 74 20 3d 20 61 44 69 73 74 61 6e 63  fLeft = aDistanc
38bf0 65 5b 61 4c 65 66 74 5b 69 4c 65 66 74 5d 5d 3b  e[aLeft[iLeft]];
38bf1 0a 20 20 20 20 20 20 20 20 66 6c 6f 61 74 20 66  .        float f
38bf2 52 69 67 68 74 20 3d 20 61 44 69 73 74 61 6e 63  Right = aDistanc
38bf3 65 5b 61 52 69 67 68 74 5b 69 52 69 67 68 74 5d  e[aRight[iRight]
38bf4 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 66  ];.        if( f
38bf5 4c 65 66 74 3c 66 52 69 67 68 74 20 29 7b 0a 20  Left<fRight ){. 
38bf6 20 20 20 20 20 20 20 20 20 61 49 64 78 5b 69 4c           aIdx[iL
38bf7 65 66 74 2b 69 52 69 67 68 74 5d 20 3d 20 61 4c  eft+iRight] = aL
38bf8 65 66 74 5b 69 4c 65 66 74 5d 3b 0a 20 20 20 20  eft[iLeft];.    
38bf9 20 20 20 20 20 20 69 4c 65 66 74 2b 2b 3b 0a 20        iLeft++;. 
38bfa 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
38bfb 20 20 20 20 20 20 20 20 61 49 64 78 5b 69 4c 65          aIdx[iLe
38bfc 66 74 2b 69 52 69 67 68 74 5d 20 3d 20 61 52 69  ft+iRight] = aRi
38bfd 67 68 74 5b 69 52 69 67 68 74 5d 3b 0a 20 20 20  ght[iRight];.   
38bfe 20 20 20 20 20 20 20 69 52 69 67 68 74 2b 2b 3b         iRight++;
38bff 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
38c00 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 66 20 30 0a   }.    }..#if 0.
38c01 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61      /* Check tha
38c02 74 20 74 68 65 20 73 6f 72 74 20 77 6f 72 6b 65  t the sort worke
38c03 64 20 2a 2f 0a 20 20 20 20 7b 0a 20 20 20 20 20  d */.    {.     
38c04 20 69 6e 74 20 6a 6a 3b 0a 20 20 20 20 20 20 66   int jj;.      f
38c05 6f 72 28 6a 6a 3d 31 3b 20 6a 6a 3c 6e 49 64 78  or(jj=1; jj<nIdx
38c06 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  ; jj++){.       
38c07 20 66 6c 6f 61 74 20 6c 65 66 74 20 3d 20 61 44   float left = aD
38c08 69 73 74 61 6e 63 65 5b 61 49 64 78 5b 6a 6a 2d  istance[aIdx[jj-
38c09 31 5d 5d 3b 0a 20 20 20 20 20 20 20 20 66 6c 6f  1]];.        flo
38c0a 61 74 20 72 69 67 68 74 20 3d 20 61 44 69 73 74  at right = aDist
38c0b 61 6e 63 65 5b 61 49 64 78 5b 6a 6a 5d 5d 3b 0a  ance[aIdx[jj]];.
38c0c 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
38c0d 6c 65 66 74 3c 3d 72 69 67 68 74 20 29 3b 0a 20  left<=right );. 
38c0e 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e       }.    }.#en
38c0f 64 69 66 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  dif.  }.}../*.**
38c10 20 41 72 67 75 6d 65 6e 74 73 20 61 49 64 78 2c   Arguments aIdx,
38c11 20 61 43 65 6c 6c 20 61 6e 64 20 61 53 70 61 72   aCell and aSpar
38c12 65 20 61 6c 6c 20 70 6f 69 6e 74 20 74 6f 20 61  e all point to a
38c13 72 72 61 79 73 20 6f 66 20 73 69 7a 65 0a 2a 2a  rrays of size.**
38c14 20 6e 49 64 78 2e 20 54 68 65 20 61 49 64 78 20   nIdx. The aIdx 
38c15 61 72 72 61 79 20 63 6f 6e 74 61 69 6e 73 20 74  array contains t
38c16 68 65 20 73 65 74 20 6f 66 20 69 6e 74 65 67 65  he set of intege
38c17 72 73 20 66 72 6f 6d 20 30 20 74 6f 20 0a 2a 2a  rs from 0 to .**
38c18 20 28 6e 49 64 78 2d 31 29 20 69 6e 20 6e 6f 20   (nIdx-1) in no 
38c19 70 61 72 74 69 63 75 6c 61 72 20 6f 72 64 65 72  particular order
38c1a 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
38c1b 73 6f 72 74 73 20 74 68 65 20 76 61 6c 75 65 73  sorts the values
38c1c 0a 2a 2a 20 69 6e 20 61 49 64 78 20 61 63 63 6f  .** in aIdx acco
38c1d 72 64 69 6e 67 20 74 6f 20 64 69 6d 65 6e 73 69  rding to dimensi
38c1e 6f 6e 20 69 44 69 6d 20 6f 66 20 74 68 65 20 63  on iDim of the c
38c1f 65 6c 6c 73 20 69 6e 20 61 43 65 6c 6c 2e 20 54  ells in aCell. T
38c20 68 65 0a 2a 2a 20 6d 69 6e 69 6d 75 6d 20 76 61  he.** minimum va
38c21 6c 75 65 20 6f 66 20 64 69 6d 65 6e 73 69 6f 6e  lue of dimension
38c22 20 69 44 69 6d 20 69 73 20 63 6f 6e 73 69 64 65   iDim is conside
38c23 72 65 64 20 66 69 72 73 74 2c 20 74 68 65 0a 2a  red first, the.*
38c24 2a 20 6d 61 78 69 6d 75 6d 20 75 73 65 64 20 74  * maximum used t
38c25 6f 20 62 72 65 61 6b 20 74 69 65 73 2e 0a 2a 2a  o break ties..**
38c26 0a 2a 2a 20 54 68 65 20 61 53 70 61 72 65 20 61  .** The aSpare a
38c27 72 72 61 79 20 69 73 20 75 73 65 64 20 61 73 20  rray is used as 
38c28 74 65 6d 70 6f 72 61 72 79 20 77 6f 72 6b 69 6e  temporary workin
38c29 67 20 73 70 61 63 65 20 62 79 20 74 68 65 0a 2a  g space by the.*
38c2a 2a 20 73 6f 72 74 69 6e 67 20 61 6c 67 6f 72 69  * sorting algori
38c2b 74 68 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  thm..*/.static v
38c2c 6f 69 64 20 53 6f 72 74 42 79 44 69 6d 65 6e 73  oid SortByDimens
38c2d 69 6f 6e 28 0a 20 20 52 74 72 65 65 20 2a 70 52  ion(.  Rtree *pR
38c2e 74 72 65 65 2c 0a 20 20 69 6e 74 20 2a 61 49 64  tree,.  int *aId
38c2f 78 2c 20 0a 20 20 69 6e 74 20 6e 49 64 78 2c 20  x, .  int nIdx, 
38c30 0a 20 20 69 6e 74 20 69 44 69 6d 2c 20 0a 20 20  .  int iDim, .  
38c31 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c  RtreeCell *aCell
38c32 2c 20 0a 20 20 69 6e 74 20 2a 61 53 70 61 72 65  , .  int *aSpare
38c33 0a 29 7b 0a 20 20 69 66 28 20 6e 49 64 78 3e 31  .){.  if( nIdx>1
38c34 20 29 7b 0a 0a 20 20 20 20 69 6e 74 20 69 4c 65   ){..    int iLe
38c35 66 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20  ft = 0;.    int 
38c36 69 52 69 67 68 74 20 3d 20 30 3b 0a 0a 20 20 20  iRight = 0;..   
38c37 20 69 6e 74 20 6e 4c 65 66 74 20 3d 20 6e 49 64   int nLeft = nId
38c38 78 2f 32 3b 0a 20 20 20 20 69 6e 74 20 6e 52 69  x/2;.    int nRi
38c39 67 68 74 20 3d 20 6e 49 64 78 2d 6e 4c 65 66 74  ght = nIdx-nLeft
38c3a 3b 0a 20 20 20 20 69 6e 74 20 2a 61 4c 65 66 74  ;.    int *aLeft
38c3b 20 3d 20 61 49 64 78 3b 0a 20 20 20 20 69 6e 74   = aIdx;.    int
38c3c 20 2a 61 52 69 67 68 74 20 3d 20 26 61 49 64 78   *aRight = &aIdx
38c3d 5b 6e 4c 65 66 74 5d 3b 0a 0a 20 20 20 20 53 6f  [nLeft];..    So
38c3e 72 74 42 79 44 69 6d 65 6e 73 69 6f 6e 28 70 52  rtByDimension(pR
38c3f 74 72 65 65 2c 20 61 4c 65 66 74 2c 20 6e 4c 65  tree, aLeft, nLe
38c40 66 74 2c 20 69 44 69 6d 2c 20 61 43 65 6c 6c 2c  ft, iDim, aCell,
38c41 20 61 53 70 61 72 65 29 3b 0a 20 20 20 20 53 6f   aSpare);.    So
38c42 72 74 42 79 44 69 6d 65 6e 73 69 6f 6e 28 70 52  rtByDimension(pR
38c43 74 72 65 65 2c 20 61 52 69 67 68 74 2c 20 6e 52  tree, aRight, nR
38c44 69 67 68 74 2c 20 69 44 69 6d 2c 20 61 43 65 6c  ight, iDim, aCel
38c45 6c 2c 20 61 53 70 61 72 65 29 3b 0a 0a 20 20 20  l, aSpare);..   
38c46 20 6d 65 6d 63 70 79 28 61 53 70 61 72 65 2c 20   memcpy(aSpare, 
38c47 61 4c 65 66 74 2c 20 73 69 7a 65 6f 66 28 69 6e  aLeft, sizeof(in
38c48 74 29 2a 6e 4c 65 66 74 29 3b 0a 20 20 20 20 61  t)*nLeft);.    a
38c49 4c 65 66 74 20 3d 20 61 53 70 61 72 65 3b 0a 20  Left = aSpare;. 
38c4a 20 20 20 77 68 69 6c 65 28 20 69 4c 65 66 74 3c     while( iLeft<
38c4b 6e 4c 65 66 74 20 7c 7c 20 69 52 69 67 68 74 3c  nLeft || iRight<
38c4c 6e 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20  nRight ){.      
38c4d 64 6f 75 62 6c 65 20 78 6c 65 66 74 31 20 3d 20  double xleft1 = 
38c4e 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 61 4c 65  DCOORD(aCell[aLe
38c4f 66 74 5b 69 4c 65 66 74 5d 5d 2e 61 43 6f 6f 72  ft[iLeft]].aCoor
38c50 64 5b 69 44 69 6d 2a 32 5d 29 3b 0a 20 20 20 20  d[iDim*2]);.    
38c51 20 20 64 6f 75 62 6c 65 20 78 6c 65 66 74 32 20    double xleft2 
38c52 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 61  = DCOORD(aCell[a
38c53 4c 65 66 74 5b 69 4c 65 66 74 5d 5d 2e 61 43 6f  Left[iLeft]].aCo
38c54 6f 72 64 5b 69 44 69 6d 2a 32 2b 31 5d 29 3b 0a  ord[iDim*2+1]);.
38c55 20 20 20 20 20 20 64 6f 75 62 6c 65 20 78 72 69        double xri
38c56 67 68 74 31 20 3d 20 44 43 4f 4f 52 44 28 61 43  ght1 = DCOORD(aC
38c57 65 6c 6c 5b 61 52 69 67 68 74 5b 69 52 69 67 68  ell[aRight[iRigh
38c58 74 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a  t]].aCoord[iDim*
38c59 32 5d 29 3b 0a 20 20 20 20 20 20 64 6f 75 62 6c  2]);.      doubl
38c5a 65 20 78 72 69 67 68 74 32 20 3d 20 44 43 4f 4f  e xright2 = DCOO
38c5b 52 44 28 61 43 65 6c 6c 5b 61 52 69 67 68 74 5b  RD(aCell[aRight[
38c5c 69 52 69 67 68 74 5d 5d 2e 61 43 6f 6f 72 64 5b  iRight]].aCoord[
38c5d 69 44 69 6d 2a 32 2b 31 5d 29 3b 0a 20 20 20 20  iDim*2+1]);.    
38c5e 20 20 69 66 28 20 28 69 4c 65 66 74 21 3d 6e 4c    if( (iLeft!=nL
38c5f 65 66 74 29 20 26 26 20 28 28 69 52 69 67 68 74  eft) && ((iRight
38c60 3d 3d 6e 52 69 67 68 74 29 0a 20 20 20 20 20 20  ==nRight).      
38c61 20 7c 7c 20 28 78 6c 65 66 74 31 3c 78 72 69 67   || (xleft1<xrig
38c62 68 74 31 29 0a 20 20 20 20 20 20 20 7c 7c 20 28  ht1).       || (
38c63 78 6c 65 66 74 31 3d 3d 78 72 69 67 68 74 31 20  xleft1==xright1 
38c64 26 26 20 78 6c 65 66 74 32 3c 78 72 69 67 68 74  && xleft2<xright
38c65 32 29 0a 20 20 20 20 20 20 29 29 7b 0a 20 20 20  2).      )){.   
38c66 20 20 20 20 20 61 49 64 78 5b 69 4c 65 66 74 2b       aIdx[iLeft+
38c67 69 52 69 67 68 74 5d 20 3d 20 61 4c 65 66 74 5b  iRight] = aLeft[
38c68 69 4c 65 66 74 5d 3b 0a 20 20 20 20 20 20 20 20  iLeft];.        
38c69 69 4c 65 66 74 2b 2b 3b 0a 20 20 20 20 20 20 7d  iLeft++;.      }
38c6a 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 49  else{.        aI
38c6b 64 78 5b 69 4c 65 66 74 2b 69 52 69 67 68 74 5d  dx[iLeft+iRight]
38c6c 20 3d 20 61 52 69 67 68 74 5b 69 52 69 67 68 74   = aRight[iRight
38c6d 5d 3b 0a 20 20 20 20 20 20 20 20 69 52 69 67 68  ];.        iRigh
38c6e 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  t++;.      }.   
38c6f 20 7d 0a 0a 23 69 66 20 30 0a 20 20 20 20 2f 2a   }..#if 0.    /*
38c70 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20   Check that the 
38c71 73 6f 72 74 20 77 6f 72 6b 65 64 20 2a 2f 0a 20  sort worked */. 
38c72 20 20 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a     {.      int j
38c73 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 6a 3d  j;.      for(jj=
38c74 31 3b 20 6a 6a 3c 6e 49 64 78 3b 20 6a 6a 2b 2b  1; jj<nIdx; jj++
38c75 29 7b 0a 20 20 20 20 20 20 20 20 66 6c 6f 61 74  ){.        float
38c76 20 78 6c 65 66 74 31 20 3d 20 61 43 65 6c 6c 5b   xleft1 = aCell[
38c77 61 49 64 78 5b 6a 6a 2d 31 5d 5d 2e 61 43 6f 6f  aIdx[jj-1]].aCoo
38c78 72 64 5b 69 44 69 6d 2a 32 5d 3b 0a 20 20 20 20  rd[iDim*2];.    
38c79 20 20 20 20 66 6c 6f 61 74 20 78 6c 65 66 74 32      float xleft2
38c7a 20 3d 20 61 43 65 6c 6c 5b 61 49 64 78 5b 6a 6a   = aCell[aIdx[jj
38c7b 2d 31 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d  -1]].aCoord[iDim
38c7c 2a 32 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20 66  *2+1];.        f
38c7d 6c 6f 61 74 20 78 72 69 67 68 74 31 20 3d 20 61  loat xright1 = a
38c7e 43 65 6c 6c 5b 61 49 64 78 5b 6a 6a 5d 5d 2e 61  Cell[aIdx[jj]].a
38c7f 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 5d 3b 0a 20  Coord[iDim*2];. 
38c80 20 20 20 20 20 20 20 66 6c 6f 61 74 20 78 72 69         float xri
38c81 67 68 74 32 20 3d 20 61 43 65 6c 6c 5b 61 49 64  ght2 = aCell[aId
38c82 78 5b 6a 6a 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44  x[jj]].aCoord[iD
38c83 69 6d 2a 32 2b 31 5d 3b 0a 20 20 20 20 20 20 20  im*2+1];.       
38c84 20 61 73 73 65 72 74 28 20 78 6c 65 66 74 31 3c   assert( xleft1<
38c85 3d 78 72 69 67 68 74 31 20 26 26 20 28 78 6c 65  =xright1 && (xle
38c86 66 74 31 3c 78 72 69 67 68 74 31 20 7c 7c 20 78  ft1<xright1 || x
38c87 6c 65 66 74 32 3c 3d 78 72 69 67 68 74 32 29 20  left2<=xright2) 
38c88 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
38c89 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a 23  .#endif.  }.}..#
38c8a 69 66 20 56 41 52 49 41 4e 54 5f 52 53 54 41 52  if VARIANT_RSTAR
38c8b 54 52 45 45 5f 53 50 4c 49 54 0a 2f 2a 0a 2a 2a  TREE_SPLIT./*.**
38c8c 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
38c8d 6f 66 20 74 68 65 20 52 2a 2d 74 72 65 65 20 76  of the R*-tree v
38c8e 61 72 69 61 6e 74 20 6f 66 20 53 70 6c 69 74 4e  ariant of SplitN
38c8f 6f 64 65 20 66 72 6f 6d 20 42 65 63 6b 6d 61 6e  ode from Beckman
38c90 5b 31 39 39 30 5d 2e 0a 2a 2f 0a 73 74 61 74 69  [1990]..*/.stati
38c91 63 20 69 6e 74 20 73 70 6c 69 74 4e 6f 64 65 53  c int splitNodeS
38c92 74 61 72 74 72 65 65 28 0a 20 20 52 74 72 65 65  tartree(.  Rtree
38c93 20 2a 70 52 74 72 65 65 2c 0a 20 20 52 74 72 65   *pRtree,.  Rtre
38c94 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 0a 20 20  eCell *aCell,.  
38c95 69 6e 74 20 6e 43 65 6c 6c 2c 0a 20 20 52 74 72  int nCell,.  Rtr
38c96 65 65 4e 6f 64 65 20 2a 70 4c 65 66 74 2c 0a 20  eeNode *pLeft,. 
38c97 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 52 69 67   RtreeNode *pRig
38c98 68 74 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20  ht,.  RtreeCell 
38c99 2a 70 42 62 6f 78 4c 65 66 74 2c 0a 20 20 52 74  *pBboxLeft,.  Rt
38c9a 72 65 65 43 65 6c 6c 20 2a 70 42 62 6f 78 52 69  reeCell *pBboxRi
38c9b 67 68 74 0a 29 7b 0a 20 20 69 6e 74 20 2a 2a 61  ght.){.  int **a
38c9c 61 53 6f 72 74 65 64 3b 0a 20 20 69 6e 74 20 2a  aSorted;.  int *
38c9d 61 53 70 61 72 65 3b 0a 20 20 69 6e 74 20 69 69  aSpare;.  int ii
38c9e 3b 0a 0a 20 20 69 6e 74 20 69 42 65 73 74 44 69  ;..  int iBestDi
38c9f 6d 3b 0a 20 20 69 6e 74 20 69 42 65 73 74 53 70  m;.  int iBestSp
38ca0 6c 69 74 3b 0a 20 20 66 6c 6f 61 74 20 66 42 65  lit;.  float fBe
38ca1 73 74 4d 61 72 67 69 6e 3b 0a 0a 20 20 69 6e 74  stMargin;..  int
38ca2 20 6e 42 79 74 65 20 3d 20 28 70 52 74 72 65 65   nByte = (pRtree
38ca3 2d 3e 6e 44 69 6d 2b 31 29 2a 28 73 69 7a 65 6f  ->nDim+1)*(sizeo
38ca4 66 28 69 6e 74 2a 29 2b 6e 43 65 6c 6c 2a 73 69  f(int*)+nCell*si
38ca5 7a 65 6f 66 28 69 6e 74 29 29 3b 0a 0a 20 20 61  zeof(int));..  a
38ca6 61 53 6f 72 74 65 64 20 3d 20 28 69 6e 74 20 2a  aSorted = (int *
38ca7 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  *)sqlite3_malloc
38ca8 28 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20 21  (nByte);.  if( !
38ca9 61 61 53 6f 72 74 65 64 20 29 7b 0a 20 20 20 20  aaSorted ){.    
38caa 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
38cab 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 61 53 70 61  MEM;.  }..  aSpa
38cac 72 65 20 3d 20 26 28 28 69 6e 74 20 2a 29 26 61  re = &((int *)&a
38cad 61 53 6f 72 74 65 64 5b 70 52 74 72 65 65 2d 3e  aSorted[pRtree->
38cae 6e 44 69 6d 5d 29 5b 70 52 74 72 65 65 2d 3e 6e  nDim])[pRtree->n
38caf 44 69 6d 2a 6e 43 65 6c 6c 5d 3b 0a 20 20 6d 65  Dim*nCell];.  me
38cb0 6d 73 65 74 28 61 61 53 6f 72 74 65 64 2c 20 30  mset(aaSorted, 0
38cb1 2c 20 6e 42 79 74 65 29 3b 0a 20 20 66 6f 72 28  , nByte);.  for(
38cb2 69 69 3d 30 3b 20 69 69 3c 70 52 74 72 65 65 2d  ii=0; ii<pRtree-
38cb3 3e 6e 44 69 6d 3b 20 69 69 2b 2b 29 7b 0a 20 20  >nDim; ii++){.  
38cb4 20 20 69 6e 74 20 6a 6a 3b 0a 20 20 20 20 61 61    int jj;.    aa
38cb5 53 6f 72 74 65 64 5b 69 69 5d 20 3d 20 26 28 28  Sorted[ii] = &((
38cb6 69 6e 74 20 2a 29 26 61 61 53 6f 72 74 65 64 5b  int *)&aaSorted[
38cb7 70 52 74 72 65 65 2d 3e 6e 44 69 6d 5d 29 5b 69  pRtree->nDim])[i
38cb8 69 2a 6e 43 65 6c 6c 5d 3b 0a 20 20 20 20 66 6f  i*nCell];.    fo
38cb9 72 28 6a 6a 3d 30 3b 20 6a 6a 3c 6e 43 65 6c 6c  r(jj=0; jj<nCell
38cba 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 61  ; jj++){.      a
38cbb 61 53 6f 72 74 65 64 5b 69 69 5d 5b 6a 6a 5d 20  aSorted[ii][jj] 
38cbc 3d 20 6a 6a 3b 0a 20 20 20 20 7d 0a 20 20 20 20  = jj;.    }.    
38cbd 53 6f 72 74 42 79 44 69 6d 65 6e 73 69 6f 6e 28  SortByDimension(
38cbe 70 52 74 72 65 65 2c 20 61 61 53 6f 72 74 65 64  pRtree, aaSorted
38cbf 5b 69 69 5d 2c 20 6e 43 65 6c 6c 2c 20 69 69 2c  [ii], nCell, ii,
38cc0 20 61 43 65 6c 6c 2c 20 61 53 70 61 72 65 29 3b   aCell, aSpare);
38cc1 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 69 69 3d 30  .  }..  for(ii=0
38cc2 3b 20 69 69 3c 70 52 74 72 65 65 2d 3e 6e 44 69  ; ii<pRtree->nDi
38cc3 6d 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 66 6c  m; ii++){.    fl
38cc4 6f 61 74 20 6d 61 72 67 69 6e 20 3d 20 30 2e 30  oat margin = 0.0
38cc5 3b 0a 20 20 20 20 66 6c 6f 61 74 20 66 42 65 73  ;.    float fBes
38cc6 74 4f 76 65 72 6c 61 70 3b 0a 20 20 20 20 66 6c  tOverlap;.    fl
38cc7 6f 61 74 20 66 42 65 73 74 41 72 65 61 3b 0a 20  oat fBestArea;. 
38cc8 20 20 20 69 6e 74 20 69 42 65 73 74 4c 65 66 74     int iBestLeft
38cc9 3b 0a 20 20 20 20 69 6e 74 20 6e 4c 65 66 74 3b  ;.    int nLeft;
38cca 0a 0a 20 20 20 20 66 6f 72 28 0a 20 20 20 20 20  ..    for(.     
38ccb 20 6e 4c 65 66 74 3d 52 54 52 45 45 5f 4d 49 4e   nLeft=RTREE_MIN
38ccc 43 45 4c 4c 53 28 70 52 74 72 65 65 29 3b 20 0a  CELLS(pRtree); .
38ccd 20 20 20 20 20 20 6e 4c 65 66 74 3c 3d 28 6e 43        nLeft<=(nC
38cce 65 6c 6c 2d 52 54 52 45 45 5f 4d 49 4e 43 45 4c  ell-RTREE_MINCEL
38ccf 4c 53 28 70 52 74 72 65 65 29 29 3b 20 0a 20 20  LS(pRtree)); .  
38cd0 20 20 20 20 6e 4c 65 66 74 2b 2b 0a 20 20 20 20      nLeft++.    
38cd1 29 7b 0a 20 20 20 20 20 20 52 74 72 65 65 43 65  ){.      RtreeCe
38cd2 6c 6c 20 6c 65 66 74 3b 0a 20 20 20 20 20 20 52  ll left;.      R
38cd3 74 72 65 65 43 65 6c 6c 20 72 69 67 68 74 3b 0a  treeCell right;.
38cd4 20 20 20 20 20 20 69 6e 74 20 6b 6b 3b 0a 20 20        int kk;.  
38cd5 20 20 20 20 66 6c 6f 61 74 20 6f 76 65 72 6c 61      float overla
38cd6 70 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 61  p;.      float a
38cd7 72 65 61 3b 0a 0a 20 20 20 20 20 20 6d 65 6d 63  rea;..      memc
38cd8 70 79 28 26 6c 65 66 74 2c 20 26 61 43 65 6c 6c  py(&left, &aCell
38cd9 5b 61 61 53 6f 72 74 65 64 5b 69 69 5d 5b 30 5d  [aaSorted[ii][0]
38cda 5d 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65 43  ], sizeof(RtreeC
38cdb 65 6c 6c 29 29 3b 0a 20 20 20 20 20 20 6d 65 6d  ell));.      mem
38cdc 63 70 79 28 26 72 69 67 68 74 2c 20 26 61 43 65  cpy(&right, &aCe
38cdd 6c 6c 5b 61 61 53 6f 72 74 65 64 5b 69 69 5d 5b  ll[aaSorted[ii][
38cde 6e 43 65 6c 6c 2d 31 5d 5d 2c 20 73 69 7a 65 6f  nCell-1]], sizeo
38cdf 66 28 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20  f(RtreeCell));. 
38ce0 20 20 20 20 20 66 6f 72 28 6b 6b 3d 31 3b 20 6b       for(kk=1; k
38ce1 6b 3c 28 6e 43 65 6c 6c 2d 31 29 3b 20 6b 6b 2b  k<(nCell-1); kk+
38ce2 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  +){.        if( 
38ce3 6b 6b 3c 6e 4c 65 66 74 20 29 7b 0a 20 20 20 20  kk<nLeft ){.    
38ce4 20 20 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28        cellUnion(
38ce5 70 52 74 72 65 65 2c 20 26 6c 65 66 74 2c 20 26  pRtree, &left, &
38ce6 61 43 65 6c 6c 5b 61 61 53 6f 72 74 65 64 5b 69  aCell[aaSorted[i
38ce7 69 5d 5b 6b 6b 5d 5d 29 3b 0a 20 20 20 20 20 20  i][kk]]);.      
38ce8 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
38ce9 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74     cellUnion(pRt
38cea 72 65 65 2c 20 26 72 69 67 68 74 2c 20 26 61 43  ree, &right, &aC
38ceb 65 6c 6c 5b 61 61 53 6f 72 74 65 64 5b 69 69 5d  ell[aaSorted[ii]
38cec 5b 6b 6b 5d 5d 29 3b 0a 20 20 20 20 20 20 20 20  [kk]]);.        
38ced 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
38cee 6d 61 72 67 69 6e 20 2b 3d 20 63 65 6c 6c 4d 61  margin += cellMa
38cef 72 67 69 6e 28 70 52 74 72 65 65 2c 20 26 6c 65  rgin(pRtree, &le
38cf0 66 74 29 3b 0a 20 20 20 20 20 20 6d 61 72 67 69  ft);.      margi
38cf1 6e 20 2b 3d 20 63 65 6c 6c 4d 61 72 67 69 6e 28  n += cellMargin(
38cf2 70 52 74 72 65 65 2c 20 26 72 69 67 68 74 29 3b  pRtree, &right);
38cf3 0a 20 20 20 20 20 20 6f 76 65 72 6c 61 70 20 3d  .      overlap =
38cf4 20 63 65 6c 6c 4f 76 65 72 6c 61 70 28 70 52 74   cellOverlap(pRt
38cf5 72 65 65 2c 20 26 6c 65 66 74 2c 20 26 72 69 67  ree, &left, &rig
38cf6 68 74 2c 20 31 2c 20 2d 31 29 3b 0a 20 20 20 20  ht, 1, -1);.    
38cf7 20 20 61 72 65 61 20 3d 20 63 65 6c 6c 41 72 65    area = cellAre
38cf8 61 28 70 52 74 72 65 65 2c 20 26 6c 65 66 74 29  a(pRtree, &left)
38cf9 20 2b 20 63 65 6c 6c 41 72 65 61 28 70 52 74 72   + cellArea(pRtr
38cfa 65 65 2c 20 26 72 69 67 68 74 29 3b 0a 20 20 20  ee, &right);.   
38cfb 20 20 20 69 66 28 20 28 6e 4c 65 66 74 3d 3d 52     if( (nLeft==R
38cfc 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70 52  TREE_MINCELLS(pR
38cfd 74 72 65 65 29 29 0a 20 20 20 20 20 20 20 7c 7c  tree)).       ||
38cfe 20 28 6f 76 65 72 6c 61 70 3c 66 42 65 73 74 4f   (overlap<fBestO
38cff 76 65 72 6c 61 70 29 0a 20 20 20 20 20 20 20 7c  verlap).       |
38d00 7c 20 28 6f 76 65 72 6c 61 70 3d 3d 66 42 65 73  | (overlap==fBes
38d01 74 4f 76 65 72 6c 61 70 20 26 26 20 61 72 65 61  tOverlap && area
38d02 3c 66 42 65 73 74 41 72 65 61 29 0a 20 20 20 20  <fBestArea).    
38d03 20 20 29 7b 0a 20 20 20 20 20 20 20 20 69 42 65    ){.        iBe
38d04 73 74 4c 65 66 74 20 3d 20 6e 4c 65 66 74 3b 0a  stLeft = nLeft;.
38d05 20 20 20 20 20 20 20 20 66 42 65 73 74 4f 76 65          fBestOve
38d06 72 6c 61 70 20 3d 20 6f 76 65 72 6c 61 70 3b 0a  rlap = overlap;.
38d07 20 20 20 20 20 20 20 20 66 42 65 73 74 41 72 65          fBestAre
38d08 61 20 3d 20 61 72 65 61 3b 0a 20 20 20 20 20 20  a = area;.      
38d09 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  }.    }..    if(
38d0a 20 69 69 3d 3d 30 20 7c 7c 20 6d 61 72 67 69 6e   ii==0 || margin
38d0b 3c 66 42 65 73 74 4d 61 72 67 69 6e 20 29 7b 0a  <fBestMargin ){.
38d0c 20 20 20 20 20 20 69 42 65 73 74 44 69 6d 20 3d        iBestDim =
38d0d 20 69 69 3b 0a 20 20 20 20 20 20 66 42 65 73 74   ii;.      fBest
38d0e 4d 61 72 67 69 6e 20 3d 20 6d 61 72 67 69 6e 3b  Margin = margin;
38d0f 0a 20 20 20 20 20 20 69 42 65 73 74 53 70 6c 69  .      iBestSpli
38d10 74 20 3d 20 69 42 65 73 74 4c 65 66 74 3b 0a 20  t = iBestLeft;. 
38d11 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 6d 65 6d 63     }.  }..  memc
38d12 70 79 28 70 42 62 6f 78 4c 65 66 74 2c 20 26 61  py(pBboxLeft, &a
38d13 43 65 6c 6c 5b 61 61 53 6f 72 74 65 64 5b 69 42  Cell[aaSorted[iB
38d14 65 73 74 44 69 6d 5d 5b 30 5d 5d 2c 20 73 69 7a  estDim][0]], siz
38d15 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 29 3b  eof(RtreeCell));
38d16 0a 20 20 6d 65 6d 63 70 79 28 70 42 62 6f 78 52  .  memcpy(pBboxR
38d17 69 67 68 74 2c 20 26 61 43 65 6c 6c 5b 61 61 53  ight, &aCell[aaS
38d18 6f 72 74 65 64 5b 69 42 65 73 74 44 69 6d 5d 5b  orted[iBestDim][
38d19 69 42 65 73 74 53 70 6c 69 74 5d 5d 2c 20 73 69  iBestSplit]], si
38d1a 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 29  zeof(RtreeCell))
38d1b 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ;.  for(ii=0; ii
38d1c 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20  <nCell; ii++){. 
38d1d 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 54     RtreeNode *pT
38d1e 61 72 67 65 74 20 3d 20 28 69 69 3c 69 42 65 73  arget = (ii<iBes
38d1f 74 53 70 6c 69 74 29 3f 70 4c 65 66 74 3a 70 52  tSplit)?pLeft:pR
38d20 69 67 68 74 3b 0a 20 20 20 20 52 74 72 65 65 43  ight;.    RtreeC
38d21 65 6c 6c 20 2a 70 42 62 6f 78 20 3d 20 28 69 69  ell *pBbox = (ii
38d22 3c 69 42 65 73 74 53 70 6c 69 74 29 3f 70 42 62  <iBestSplit)?pBb
38d23 6f 78 4c 65 66 74 3a 70 42 62 6f 78 52 69 67 68  oxLeft:pBboxRigh
38d24 74 3b 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c  t;.    RtreeCell
38d25 20 2a 70 43 65 6c 6c 20 3d 20 26 61 43 65 6c 6c   *pCell = &aCell
38d26 5b 61 61 53 6f 72 74 65 64 5b 69 42 65 73 74 44  [aaSorted[iBestD
38d27 69 6d 5d 5b 69 69 5d 5d 3b 0a 20 20 20 20 6e 6f  im][ii]];.    no
38d28 64 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74  deInsertCell(pRt
38d29 72 65 65 2c 20 70 54 61 72 67 65 74 2c 20 70 43  ree, pTarget, pC
38d2a 65 6c 6c 29 3b 0a 20 20 20 20 63 65 6c 6c 55 6e  ell);.    cellUn
38d2b 69 6f 6e 28 70 52 74 72 65 65 2c 20 70 42 62 6f  ion(pRtree, pBbo
38d2c 78 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 7d 0a 0a  x, pCell);.  }..
38d2d 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61    sqlite3_free(a
38d2e 61 53 6f 72 74 65 64 29 3b 0a 20 20 72 65 74 75  aSorted);.  retu
38d2f 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
38d30 23 65 6e 64 69 66 0a 0a 23 69 66 20 56 41 52 49  #endif..#if VARI
38d31 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 53 50 4c 49  ANT_GUTTMAN_SPLI
38d32 54 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  T./*.** Implemen
38d33 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 72 65  tation of the re
38d34 67 75 6c 61 72 20 52 2d 74 72 65 65 20 53 70 6c  gular R-tree Spl
38d35 69 74 4e 6f 64 65 20 66 72 6f 6d 20 47 75 74 74  itNode from Gutt
38d36 6d 61 6e 5b 31 39 38 34 5d 2e 0a 2a 2f 0a 73 74  man[1984]..*/.st
38d37 61 74 69 63 20 69 6e 74 20 73 70 6c 69 74 4e 6f  atic int splitNo
38d38 64 65 47 75 74 74 6d 61 6e 28 0a 20 20 52 74 72  deGuttman(.  Rtr
38d39 65 65 20 2a 70 52 74 72 65 65 2c 0a 20 20 52 74  ee *pRtree,.  Rt
38d3a 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 0a  reeCell *aCell,.
38d3b 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 0a 20 20 52    int nCell,.  R
38d3c 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65 66 74 2c  treeNode *pLeft,
38d3d 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 52  .  RtreeNode *pR
38d3e 69 67 68 74 2c 0a 20 20 52 74 72 65 65 43 65 6c  ight,.  RtreeCel
38d3f 6c 20 2a 70 42 62 6f 78 4c 65 66 74 2c 0a 20 20  l *pBboxLeft,.  
38d40 52 74 72 65 65 43 65 6c 6c 20 2a 70 42 62 6f 78  RtreeCell *pBbox
38d41 52 69 67 68 74 0a 29 7b 0a 20 20 69 6e 74 20 69  Right.){.  int i
38d42 4c 65 66 74 53 65 65 64 20 3d 20 30 3b 0a 20 20  LeftSeed = 0;.  
38d43 69 6e 74 20 69 52 69 67 68 74 53 65 65 64 20 3d  int iRightSeed =
38d44 20 31 3b 0a 20 20 69 6e 74 20 2a 61 69 55 73 65   1;.  int *aiUse
38d45 64 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 61  d;.  int i;..  a
38d46 69 55 73 65 64 20 3d 20 73 71 6c 69 74 65 33 5f  iUsed = sqlite3_
38d47 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 69 6e  malloc(sizeof(in
38d48 74 29 2a 6e 43 65 6c 6c 29 3b 0a 20 20 6d 65 6d  t)*nCell);.  mem
38d49 73 65 74 28 61 69 55 73 65 64 2c 20 30 2c 20 73  set(aiUsed, 0, s
38d4a 69 7a 65 6f 66 28 69 6e 74 29 2a 6e 43 65 6c 6c  izeof(int)*nCell
38d4b 29 3b 0a 0a 20 20 50 69 63 6b 53 65 65 64 73 28  );..  PickSeeds(
38d4c 70 52 74 72 65 65 2c 20 61 43 65 6c 6c 2c 20 6e  pRtree, aCell, n
38d4d 43 65 6c 6c 2c 20 26 69 4c 65 66 74 53 65 65 64  Cell, &iLeftSeed
38d4e 2c 20 26 69 52 69 67 68 74 53 65 65 64 29 3b 0a  , &iRightSeed);.
38d4f 0a 20 20 6d 65 6d 63 70 79 28 70 42 62 6f 78 4c  .  memcpy(pBboxL
38d50 65 66 74 2c 20 26 61 43 65 6c 6c 5b 69 4c 65 66  eft, &aCell[iLef
38d51 74 53 65 65 64 5d 2c 20 73 69 7a 65 6f 66 28 52  tSeed], sizeof(R
38d52 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20 6d 65  treeCell));.  me
38d53 6d 63 70 79 28 70 42 62 6f 78 52 69 67 68 74 2c  mcpy(pBboxRight,
38d54 20 26 61 43 65 6c 6c 5b 69 52 69 67 68 74 53 65   &aCell[iRightSe
38d55 65 64 5d 2c 20 73 69 7a 65 6f 66 28 52 74 72 65  ed], sizeof(Rtre
38d56 65 43 65 6c 6c 29 29 3b 0a 20 20 6e 6f 64 65 49  eCell));.  nodeI
38d57 6e 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65  nsertCell(pRtree
38d58 2c 20 70 4c 65 66 74 2c 20 26 61 43 65 6c 6c 5b  , pLeft, &aCell[
38d59 69 4c 65 66 74 53 65 65 64 5d 29 3b 0a 20 20 6e  iLeftSeed]);.  n
38d5a 6f 64 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52  odeInsertCell(pR
38d5b 74 72 65 65 2c 20 70 52 69 67 68 74 2c 20 26 61  tree, pRight, &a
38d5c 43 65 6c 6c 5b 69 52 69 67 68 74 53 65 65 64 5d  Cell[iRightSeed]
38d5d 29 3b 0a 20 20 61 69 55 73 65 64 5b 69 4c 65 66  );.  aiUsed[iLef
38d5e 74 53 65 65 64 5d 20 3d 20 31 3b 0a 20 20 61 69  tSeed] = 1;.  ai
38d5f 55 73 65 64 5b 69 52 69 67 68 74 53 65 65 64 5d  Used[iRightSeed]
38d60 20 3d 20 31 3b 0a 0a 20 20 66 6f 72 28 69 3d 6e   = 1;..  for(i=n
38d61 43 65 6c 6c 2d 32 3b 20 69 3e 30 3b 20 69 2d 2d  Cell-2; i>0; i--
38d62 29 7b 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c  ){.    RtreeCell
38d63 20 2a 70 4e 65 78 74 3b 0a 20 20 20 20 70 4e 65   *pNext;.    pNe
38d64 78 74 20 3d 20 50 69 63 6b 4e 65 78 74 28 70 52  xt = PickNext(pR
38d65 74 72 65 65 2c 20 61 43 65 6c 6c 2c 20 6e 43 65  tree, aCell, nCe
38d66 6c 6c 2c 20 70 42 62 6f 78 4c 65 66 74 2c 20 70  ll, pBboxLeft, p
38d67 42 62 6f 78 52 69 67 68 74 2c 20 61 69 55 73 65  BboxRight, aiUse
38d68 64 29 3b 0a 20 20 20 20 66 6c 6f 61 74 20 64 69  d);.    float di
38d69 66 66 20 3d 20 20 0a 20 20 20 20 20 20 63 65 6c  ff =  .      cel
38d6a 6c 47 72 6f 77 74 68 28 70 52 74 72 65 65 2c 20  lGrowth(pRtree, 
38d6b 70 42 62 6f 78 4c 65 66 74 2c 20 70 4e 65 78 74  pBboxLeft, pNext
38d6c 29 20 2d 20 0a 20 20 20 20 20 20 63 65 6c 6c 47  ) - .      cellG
38d6d 72 6f 77 74 68 28 70 52 74 72 65 65 2c 20 70 42  rowth(pRtree, pB
38d6e 62 6f 78 52 69 67 68 74 2c 20 70 4e 65 78 74 29  boxRight, pNext)
38d6f 0a 20 20 20 20 3b 0a 20 20 20 20 69 66 28 20 28  .    ;.    if( (
38d70 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70  RTREE_MINCELLS(p
38d71 52 74 72 65 65 29 2d 4e 43 45 4c 4c 28 70 52 69  Rtree)-NCELL(pRi
38d72 67 68 74 29 3d 3d 69 29 0a 20 20 20 20 20 7c 7c  ght)==i).     ||
38d73 20 28 64 69 66 66 3e 30 2e 30 20 26 26 20 28 52   (diff>0.0 && (R
38d74 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70 52  TREE_MINCELLS(pR
38d75 74 72 65 65 29 2d 4e 43 45 4c 4c 28 70 4c 65 66  tree)-NCELL(pLef
38d76 74 29 21 3d 69 29 29 0a 20 20 20 20 29 7b 0a 20  t)!=i)).    ){. 
38d77 20 20 20 20 20 6e 6f 64 65 49 6e 73 65 72 74 43       nodeInsertC
38d78 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 52 69 67  ell(pRtree, pRig
38d79 68 74 2c 20 70 4e 65 78 74 29 3b 0a 20 20 20 20  ht, pNext);.    
38d7a 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72    cellUnion(pRtr
38d7b 65 65 2c 20 70 42 62 6f 78 52 69 67 68 74 2c 20  ee, pBboxRight, 
38d7c 70 4e 65 78 74 29 3b 0a 20 20 20 20 7d 65 6c 73  pNext);.    }els
38d7d 65 7b 0a 20 20 20 20 20 20 6e 6f 64 65 49 6e 73  e{.      nodeIns
38d7e 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20  ertCell(pRtree, 
38d7f 70 4c 65 66 74 2c 20 70 4e 65 78 74 29 3b 0a 20  pLeft, pNext);. 
38d80 20 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70       cellUnion(p
38d81 52 74 72 65 65 2c 20 70 42 62 6f 78 4c 65 66 74  Rtree, pBboxLeft
38d82 2c 20 70 4e 65 78 74 29 3b 0a 20 20 20 20 7d 0a  , pNext);.    }.
38d83 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66    }..  sqlite3_f
38d84 72 65 65 28 61 69 55 73 65 64 29 3b 0a 20 20 72  ree(aiUsed);.  r
38d85 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
38d86 0a 7d 0a 23 65 6e 64 69 66 0a 0a 73 74 61 74 69  .}.#endif..stati
38d87 63 20 69 6e 74 20 75 70 64 61 74 65 4d 61 70 70  c int updateMapp
38d88 69 6e 67 28 0a 20 20 52 74 72 65 65 20 2a 70 52  ing(.  Rtree *pR
38d89 74 72 65 65 2c 20 0a 20 20 69 36 34 20 69 52 6f  tree, .  i64 iRo
38d8a 77 69 64 2c 20 0a 20 20 52 74 72 65 65 4e 6f 64  wid, .  RtreeNod
38d8b 65 20 2a 70 4e 6f 64 65 2c 20 0a 20 20 69 6e 74  e *pNode, .  int
38d8c 20 69 48 65 69 67 68 74 0a 29 7b 0a 20 20 69 6e   iHeight.){.  in
38d8d 74 20 28 2a 78 53 65 74 4d 61 70 70 69 6e 67 29  t (*xSetMapping)
38d8e 28 52 74 72 65 65 20 2a 2c 20 73 71 6c 69 74 65  (Rtree *, sqlite
38d8f 33 5f 69 6e 74 36 34 2c 20 73 71 6c 69 74 65 33  3_int64, sqlite3
38d90 5f 69 6e 74 36 34 29 3b 0a 20 20 78 53 65 74 4d  _int64);.  xSetM
38d91 61 70 70 69 6e 67 20 3d 20 28 28 69 48 65 69 67  apping = ((iHeig
38d92 68 74 3d 3d 30 29 3f 72 6f 77 69 64 57 72 69 74  ht==0)?rowidWrit
38d93 65 3a 70 61 72 65 6e 74 57 72 69 74 65 29 3b 0a  e:parentWrite);.
38d94 20 20 69 66 28 20 69 48 65 69 67 68 74 3e 30 20    if( iHeight>0 
38d95 29 7b 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65  ){.    RtreeNode
38d96 20 2a 70 43 68 69 6c 64 20 3d 20 6e 6f 64 65 48   *pChild = nodeH
38d97 61 73 68 4c 6f 6f 6b 75 70 28 70 52 74 72 65 65  ashLookup(pRtree
38d98 2c 20 69 52 6f 77 69 64 29 3b 0a 20 20 20 20 69  , iRowid);.    i
38d99 66 28 20 70 43 68 69 6c 64 20 29 7b 0a 20 20 20  f( pChild ){.   
38d9a 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70     nodeRelease(p
38d9b 52 74 72 65 65 2c 20 70 43 68 69 6c 64 2d 3e 70  Rtree, pChild->p
38d9c 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 20 20 6e  Parent);.      n
38d9d 6f 64 65 52 65 66 65 72 65 6e 63 65 28 70 4e 6f  odeReference(pNo
38d9e 64 65 29 3b 0a 20 20 20 20 20 20 70 43 68 69 6c  de);.      pChil
38d9f 64 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 4e 6f  d->pParent = pNo
38da0 64 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  de;.    }.  }.  
38da1 72 65 74 75 72 6e 20 78 53 65 74 4d 61 70 70 69  return xSetMappi
38da2 6e 67 28 70 52 74 72 65 65 2c 20 69 52 6f 77 69  ng(pRtree, iRowi
38da3 64 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29  d, pNode->iNode)
38da4 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  ;.}..static int 
38da5 53 70 6c 69 74 4e 6f 64 65 28 0a 20 20 52 74 72  SplitNode(.  Rtr
38da6 65 65 20 2a 70 52 74 72 65 65 2c 0a 20 20 52 74  ee *pRtree,.  Rt
38da7 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 0a  reeNode *pNode,.
38da8 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65    RtreeCell *pCe
38da9 6c 6c 2c 0a 20 20 69 6e 74 20 69 48 65 69 67 68  ll,.  int iHeigh
38daa 74 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  t.){.  int i;.  
38dab 69 6e 74 20 6e 65 77 43 65 6c 6c 49 73 52 69 67  int newCellIsRig
38dac 68 74 20 3d 20 30 3b 0a 0a 20 20 69 6e 74 20 72  ht = 0;..  int r
38dad 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
38dae 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 4e 43 45   int nCell = NCE
38daf 4c 4c 28 70 4e 6f 64 65 29 3b 0a 20 20 52 74 72  LL(pNode);.  Rtr
38db0 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 3b 0a 20  eeCell *aCell;. 
38db1 20 69 6e 74 20 2a 61 69 55 73 65 64 3b 0a 0a 20   int *aiUsed;.. 
38db2 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65 66   RtreeNode *pLef
38db3 74 20 3d 20 30 3b 0a 20 20 52 74 72 65 65 4e 6f  t = 0;.  RtreeNo
38db4 64 65 20 2a 70 52 69 67 68 74 20 3d 20 30 3b 0a  de *pRight = 0;.
38db5 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 6c 65 66  .  RtreeCell lef
38db6 74 62 62 6f 78 3b 0a 20 20 52 74 72 65 65 43 65  tbbox;.  RtreeCe
38db7 6c 6c 20 72 69 67 68 74 62 62 6f 78 3b 0a 0a 20  ll rightbbox;.. 
38db8 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 20   /* Allocate an 
38db9 61 72 72 61 79 20 61 6e 64 20 70 6f 70 75 6c 61  array and popula
38dba 74 65 20 69 74 20 77 69 74 68 20 61 20 63 6f 70  te it with a cop
38dbb 79 20 6f 66 20 70 43 65 6c 6c 20 61 6e 64 20 0a  y of pCell and .
38dbc 20 20 2a 2a 20 61 6c 6c 20 63 65 6c 6c 73 20 66    ** all cells f
38dbd 72 6f 6d 20 6e 6f 64 65 20 70 4c 65 66 74 2e 20  rom node pLeft. 
38dbe 54 68 65 6e 20 7a 65 72 6f 20 74 68 65 20 6f 72  Then zero the or
38dbf 69 67 69 6e 61 6c 20 6e 6f 64 65 2e 0a 20 20 2a  iginal node..  *
38dc0 2f 0a 20 20 61 43 65 6c 6c 20 3d 20 73 71 6c 69  /.  aCell = sqli
38dc1 74 65 33 5f 6d 61 6c 6c 6f 63 28 28 73 69 7a 65  te3_malloc((size
38dc2 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 2b 73 69  of(RtreeCell)+si
38dc3 7a 65 6f 66 28 69 6e 74 29 29 2a 28 6e 43 65 6c  zeof(int))*(nCel
38dc4 6c 2b 31 29 29 3b 0a 20 20 69 66 28 20 21 61 43  l+1));.  if( !aC
38dc5 65 6c 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ell ){.    rc = 
38dc6 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
38dc7 20 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65    goto splitnode
38dc8 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 61 69 55 73  _out;.  }.  aiUs
38dc9 65 64 20 3d 20 28 69 6e 74 20 2a 29 26 61 43 65  ed = (int *)&aCe
38dca 6c 6c 5b 6e 43 65 6c 6c 2b 31 5d 3b 0a 20 20 6d  ll[nCell+1];.  m
38dcb 65 6d 73 65 74 28 61 69 55 73 65 64 2c 20 30 2c  emset(aiUsed, 0,
38dcc 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 28 6e 43   sizeof(int)*(nC
38dcd 65 6c 6c 2b 31 29 29 3b 0a 20 20 66 6f 72 28 69  ell+1));.  for(i
38dce 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b  =0; i<nCell; i++
38dcf 29 7b 0a 20 20 20 20 6e 6f 64 65 47 65 74 43 65  ){.    nodeGetCe
38dd0 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ll(pRtree, pNode
38dd1 2c 20 69 2c 20 26 61 43 65 6c 6c 5b 69 5d 29 3b  , i, &aCell[i]);
38dd2 0a 20 20 7d 0a 20 20 6e 6f 64 65 5a 65 72 6f 28  .  }.  nodeZero(
38dd3 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a  pRtree, pNode);.
38dd4 20 20 6d 65 6d 63 70 79 28 26 61 43 65 6c 6c 5b    memcpy(&aCell[
38dd5 6e 43 65 6c 6c 5d 2c 20 70 43 65 6c 6c 2c 20 73  nCell], pCell, s
38dd6 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29  izeof(RtreeCell)
38dd7 29 3b 0a 20 20 6e 43 65 6c 6c 2b 2b 3b 0a 0a 20  );.  nCell++;.. 
38dd8 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64   if( pNode->iNod
38dd9 65 3d 3d 31 20 29 7b 0a 20 20 20 20 70 52 69 67  e==1 ){.    pRig
38dda 68 74 20 3d 20 6e 6f 64 65 4e 65 77 28 70 52 74  ht = nodeNew(pRt
38ddb 72 65 65 2c 20 70 4e 6f 64 65 2c 20 31 29 3b 0a  ree, pNode, 1);.
38ddc 20 20 20 20 70 4c 65 66 74 20 3d 20 6e 6f 64 65      pLeft = node
38ddd 4e 65 77 28 70 52 74 72 65 65 2c 20 70 4e 6f 64  New(pRtree, pNod
38dde 65 2c 20 31 29 3b 0a 20 20 20 20 70 52 74 72 65  e, 1);.    pRtre
38ddf 65 2d 3e 69 44 65 70 74 68 2b 2b 3b 0a 20 20 20  e->iDepth++;.   
38de0 20 70 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79 20   pNode->isDirty 
38de1 3d 20 31 3b 0a 20 20 20 20 77 72 69 74 65 49 6e  = 1;.    writeIn
38de2 74 31 36 28 70 4e 6f 64 65 2d 3e 7a 44 61 74 61  t16(pNode->zData
38de3 2c 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68  , pRtree->iDepth
38de4 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
38de5 70 4c 65 66 74 20 3d 20 70 4e 6f 64 65 3b 0a 20  pLeft = pNode;. 
38de6 20 20 20 70 52 69 67 68 74 20 3d 20 6e 6f 64 65     pRight = node
38de7 4e 65 77 28 70 52 74 72 65 65 2c 20 70 4c 65 66  New(pRtree, pLef
38de8 74 2d 3e 70 50 61 72 65 6e 74 2c 20 31 29 3b 0a  t->pParent, 1);.
38de9 20 20 20 20 6e 6f 64 65 52 65 66 65 72 65 6e 63      nodeReferenc
38dea 65 28 70 4c 65 66 74 29 3b 0a 20 20 7d 0a 0a 20  e(pLeft);.  }.. 
38deb 20 69 66 28 20 21 70 4c 65 66 74 20 7c 7c 20 21   if( !pLeft || !
38dec 70 52 69 67 68 74 20 29 7b 0a 20 20 20 20 72 63  pRight ){.    rc
38ded 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
38dee 0a 20 20 20 20 67 6f 74 6f 20 73 70 6c 69 74 6e  .    goto splitn
38def 6f 64 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20  ode_out;.  }..  
38df0 6d 65 6d 73 65 74 28 70 4c 65 66 74 2d 3e 7a 44  memset(pLeft->zD
38df1 61 74 61 2c 20 30 2c 20 70 52 74 72 65 65 2d 3e  ata, 0, pRtree->
38df2 69 4e 6f 64 65 53 69 7a 65 29 3b 0a 20 20 6d 65  iNodeSize);.  me
38df3 6d 73 65 74 28 70 52 69 67 68 74 2d 3e 7a 44 61  mset(pRight->zDa
38df4 74 61 2c 20 30 2c 20 70 52 74 72 65 65 2d 3e 69  ta, 0, pRtree->i
38df5 4e 6f 64 65 53 69 7a 65 29 3b 0a 0a 20 20 72 63  NodeSize);..  rc
38df6 20 3d 20 41 73 73 69 67 6e 43 65 6c 6c 73 28 70   = AssignCells(p
38df7 52 74 72 65 65 2c 20 61 43 65 6c 6c 2c 20 6e 43  Rtree, aCell, nC
38df8 65 6c 6c 2c 20 70 4c 65 66 74 2c 20 70 52 69 67  ell, pLeft, pRig
38df9 68 74 2c 20 26 6c 65 66 74 62 62 6f 78 2c 20 26  ht, &leftbbox, &
38dfa 72 69 67 68 74 62 62 6f 78 29 3b 0a 20 20 69 66  rightbbox);.  if
38dfb 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
38dfc 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 70 6c 69  ){.    goto spli
38dfd 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a  tnode_out;.  }..
38dfe 20 20 2f 2a 20 45 6e 73 75 72 65 20 62 6f 74 68    /* Ensure both
38dff 20 63 68 69 6c 64 20 6e 6f 64 65 73 20 68 61 76   child nodes hav
38e00 65 20 6e 6f 64 65 20 6e 75 6d 62 65 72 73 20 61  e node numbers a
38e01 73 73 69 67 6e 65 64 20 74 6f 20 74 68 65 6d 2e  ssigned to them.
38e02 20 2a 2f 0a 20 20 69 66 28 20 28 30 3d 3d 70 52   */.  if( (0==pR
38e03 69 67 68 74 2d 3e 69 4e 6f 64 65 20 26 26 20 53  ight->iNode && S
38e04 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20  QLITE_OK!=(rc = 
38e05 6e 6f 64 65 57 72 69 74 65 28 70 52 74 72 65 65  nodeWrite(pRtree
38e06 2c 20 70 52 69 67 68 74 29 29 29 0a 20 20 20 7c  , pRight))).   |
38e07 7c 20 28 30 3d 3d 70 4c 65 66 74 2d 3e 69 4e 6f  | (0==pLeft->iNo
38e08 64 65 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 21  de && SQLITE_OK!
38e09 3d 28 72 63 20 3d 20 6e 6f 64 65 57 72 69 74 65  =(rc = nodeWrite
38e0a 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74 29 29  (pRtree, pLeft))
38e0b 29 0a 20 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  ).  ){.    goto 
38e0c 73 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20  splitnode_out;. 
38e0d 20 7d 0a 0a 20 20 72 69 67 68 74 62 62 6f 78 2e   }..  rightbbox.
38e0e 69 52 6f 77 69 64 20 3d 20 70 52 69 67 68 74 2d  iRowid = pRight-
38e0f 3e 69 4e 6f 64 65 3b 0a 20 20 6c 65 66 74 62 62  >iNode;.  leftbb
38e10 6f 78 2e 69 52 6f 77 69 64 20 3d 20 70 4c 65 66  ox.iRowid = pLef
38e11 74 2d 3e 69 4e 6f 64 65 3b 0a 0a 20 20 69 66 28  t->iNode;..  if(
38e12 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31   pNode->iNode==1
38e13 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 72 74 72   ){.    rc = rtr
38e14 65 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74  eeInsertCell(pRt
38e15 72 65 65 2c 20 70 4c 65 66 74 2d 3e 70 50 61 72  ree, pLeft->pPar
38e16 65 6e 74 2c 20 26 6c 65 66 74 62 62 6f 78 2c 20  ent, &leftbbox, 
38e17 69 48 65 69 67 68 74 2b 31 29 3b 0a 20 20 20 20  iHeight+1);.    
38e18 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
38e19 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  K ){.      goto 
38e1a 73 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20  splitnode_out;. 
38e1b 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
38e1c 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 50 61    RtreeNode *pPa
38e1d 72 65 6e 74 20 3d 20 70 4c 65 66 74 2d 3e 70 50  rent = pLeft->pP
38e1e 61 72 65 6e 74 3b 0a 20 20 20 20 69 6e 74 20 69  arent;.    int i
38e1f 43 65 6c 6c 20 3d 20 6e 6f 64 65 50 61 72 65 6e  Cell = nodeParen
38e20 74 49 6e 64 65 78 28 70 52 74 72 65 65 2c 20 70  tIndex(pRtree, p
38e21 4c 65 66 74 29 3b 0a 20 20 20 20 6e 6f 64 65 4f  Left);.    nodeO
38e22 76 65 72 77 72 69 74 65 43 65 6c 6c 28 70 52 74  verwriteCell(pRt
38e23 72 65 65 2c 20 70 50 61 72 65 6e 74 2c 20 26 6c  ree, pParent, &l
38e24 65 66 74 62 62 6f 78 2c 20 69 43 65 6c 6c 29 3b  eftbbox, iCell);
38e25 0a 20 20 20 20 41 64 6a 75 73 74 54 72 65 65 28  .    AdjustTree(
38e26 70 52 74 72 65 65 2c 20 70 50 61 72 65 6e 74 2c  pRtree, pParent,
38e27 20 26 6c 65 66 74 62 62 6f 78 29 3b 0a 20 20 7d   &leftbbox);.  }
38e28 0a 20 20 69 66 28 20 28 72 63 20 3d 20 72 74 72  .  if( (rc = rtr
38e29 65 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74  eeInsertCell(pRt
38e2a 72 65 65 2c 20 70 52 69 67 68 74 2d 3e 70 50 61  ree, pRight->pPa
38e2b 72 65 6e 74 2c 20 26 72 69 67 68 74 62 62 6f 78  rent, &rightbbox
38e2c 2c 20 69 48 65 69 67 68 74 2b 31 29 29 20 29 7b  , iHeight+1)) ){
38e2d 0a 20 20 20 20 67 6f 74 6f 20 73 70 6c 69 74 6e  .    goto splitn
38e2e 6f 64 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20  ode_out;.  }..  
38e2f 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 45 4c 4c  for(i=0; i<NCELL
38e30 28 70 52 69 67 68 74 29 3b 20 69 2b 2b 29 7b 0a  (pRight); i++){.
38e31 20 20 20 20 69 36 34 20 69 52 6f 77 69 64 20 3d      i64 iRowid =
38e32 20 6e 6f 64 65 47 65 74 52 6f 77 69 64 28 70 52   nodeGetRowid(pR
38e33 74 72 65 65 2c 20 70 52 69 67 68 74 2c 20 69 29  tree, pRight, i)
38e34 3b 0a 20 20 20 20 72 63 20 3d 20 75 70 64 61 74  ;.    rc = updat
38e35 65 4d 61 70 70 69 6e 67 28 70 52 74 72 65 65 2c  eMapping(pRtree,
38e36 20 69 52 6f 77 69 64 2c 20 70 52 69 67 68 74 2c   iRowid, pRight,
38e37 20 69 48 65 69 67 68 74 29 3b 0a 20 20 20 20 69   iHeight);.    i
38e38 66 28 20 69 52 6f 77 69 64 3d 3d 70 43 65 6c 6c  f( iRowid==pCell
38e39 2d 3e 69 52 6f 77 69 64 20 29 7b 0a 20 20 20 20  ->iRowid ){.    
38e3a 20 20 6e 65 77 43 65 6c 6c 49 73 52 69 67 68 74    newCellIsRight
38e3b 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 1;.    }.    
38e3c 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
38e3d 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  K ){.      goto 
38e3e 73 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20  splitnode_out;. 
38e3f 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70     }.  }.  if( p
38e40 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31 20 29  Node->iNode==1 )
38e41 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  {.    for(i=0; i
38e42 3c 4e 43 45 4c 4c 28 70 4c 65 66 74 29 3b 20 69  <NCELL(pLeft); i
38e43 2b 2b 29 7b 0a 20 20 20 20 20 20 69 36 34 20 69  ++){.      i64 i
38e44 52 6f 77 69 64 20 3d 20 6e 6f 64 65 47 65 74 52  Rowid = nodeGetR
38e45 6f 77 69 64 28 70 52 74 72 65 65 2c 20 70 4c 65  owid(pRtree, pLe
38e46 66 74 2c 20 69 29 3b 0a 20 20 20 20 20 20 72 63  ft, i);.      rc
38e47 20 3d 20 75 70 64 61 74 65 4d 61 70 70 69 6e 67   = updateMapping
38e48 28 70 52 74 72 65 65 2c 20 69 52 6f 77 69 64 2c  (pRtree, iRowid,
38e49 20 70 4c 65 66 74 2c 20 69 48 65 69 67 68 74 29   pLeft, iHeight)
38e4a 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
38e4b 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
38e4c 20 20 20 20 20 67 6f 74 6f 20 73 70 6c 69 74 6e       goto splitn
38e4d 6f 64 65 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d  ode_out;.      }
38e4e 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69  .    }.  }else i
38e4f 66 28 20 6e 65 77 43 65 6c 6c 49 73 52 69 67 68  f( newCellIsRigh
38e50 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d  t==0 ){.    rc =
38e51 20 75 70 64 61 74 65 4d 61 70 70 69 6e 67 28 70   updateMapping(p
38e52 52 74 72 65 65 2c 20 70 43 65 6c 6c 2d 3e 69 52  Rtree, pCell->iR
38e53 6f 77 69 64 2c 20 70 4c 65 66 74 2c 20 69 48 65  owid, pLeft, iHe
38e54 69 67 68 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 66  ight);.  }..  if
38e55 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
38e56 29 7b 0a 20 20 20 20 72 63 20 3d 20 6e 6f 64 65  ){.    rc = node
38e57 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20  Release(pRtree, 
38e58 70 52 69 67 68 74 29 3b 0a 20 20 20 20 70 52 69  pRight);.    pRi
38e59 67 68 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69  ght = 0;.  }.  i
38e5a 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
38e5b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 6e 6f 64   ){.    rc = nod
38e5c 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
38e5d 20 70 4c 65 66 74 29 3b 0a 20 20 20 20 70 4c 65   pLeft);.    pLe
38e5e 66 74 20 3d 20 30 3b 0a 20 20 7d 0a 0a 73 70 6c  ft = 0;.  }..spl
38e5f 69 74 6e 6f 64 65 5f 6f 75 74 3a 0a 20 20 6e 6f  itnode_out:.  no
38e60 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65  deRelease(pRtree
38e61 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 6e 6f 64  , pRight);.  nod
38e62 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
38e63 20 70 4c 65 66 74 29 3b 0a 20 20 73 71 6c 69 74   pLeft);.  sqlit
38e64 65 33 5f 66 72 65 65 28 61 43 65 6c 6c 29 3b 0a  e3_free(aCell);.
38e65 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
38e66 73 74 61 74 69 63 20 69 6e 74 20 66 69 78 4c 65  static int fixLe
38e67 61 66 50 61 72 65 6e 74 28 52 74 72 65 65 20 2a  afParent(Rtree *
38e68 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64  pRtree, RtreeNod
38e69 65 20 2a 70 4c 65 61 66 29 7b 0a 20 20 69 6e 74  e *pLeaf){.  int
38e6a 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
38e6b 0a 20 20 69 66 28 20 70 4c 65 61 66 2d 3e 69 4e  .  if( pLeaf->iN
38e6c 6f 64 65 21 3d 31 20 26 26 20 70 4c 65 61 66 2d  ode!=1 && pLeaf-
38e6d 3e 70 50 61 72 65 6e 74 3d 3d 30 20 29 7b 0a 20  >pParent==0 ){. 
38e6e 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f     sqlite3_bind_
38e6f 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 52  int64(pRtree->pR
38e70 65 61 64 50 61 72 65 6e 74 2c 20 31 2c 20 70 4c  eadParent, 1, pL
38e71 65 61 66 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 20  eaf->iNode);.   
38e72 20 69 66 28 20 73 71 6c 69 74 65 33 5f 73 74 65   if( sqlite3_ste
38e73 70 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 50  p(pRtree->pReadP
38e74 61 72 65 6e 74 29 3d 3d 53 51 4c 49 54 45 5f 52  arent)==SQLITE_R
38e75 4f 57 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20  OW ){.      i64 
38e76 69 4e 6f 64 65 20 3d 20 73 71 6c 69 74 65 33 5f  iNode = sqlite3_
38e77 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 70 52 74  column_int64(pRt
38e78 72 65 65 2d 3e 70 52 65 61 64 50 61 72 65 6e 74  ree->pReadParent
38e79 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  , 0);.      rc =
38e7a 20 6e 6f 64 65 41 63 71 75 69 72 65 28 70 52 74   nodeAcquire(pRt
38e7b 72 65 65 2c 20 69 4e 6f 64 65 2c 20 30 2c 20 26  ree, iNode, 0, &
38e7c 70 4c 65 61 66 2d 3e 70 50 61 72 65 6e 74 29 3b  pLeaf->pParent);
38e7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
38e7e 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
38e7f 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  ROR;.    }.    s
38e80 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52 74  qlite3_reset(pRt
38e81 72 65 65 2d 3e 70 52 65 61 64 50 61 72 65 6e 74  ree->pReadParent
38e82 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
38e83 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
38e84 20 20 72 63 20 3d 20 66 69 78 4c 65 61 66 50 61    rc = fixLeafPa
38e85 72 65 6e 74 28 70 52 74 72 65 65 2c 20 70 4c 65  rent(pRtree, pLe
38e86 61 66 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20 20  af->pParent);.  
38e87 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
38e88 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69   rc;.}..static i
38e89 6e 74 20 64 65 6c 65 74 65 43 65 6c 6c 28 52 74  nt deleteCell(Rt
38e8a 72 65 65 20 2a 2c 20 52 74 72 65 65 4e 6f 64 65  ree *, RtreeNode
38e8b 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a   *, int, int);..
38e8c 73 74 61 74 69 63 20 69 6e 74 20 72 65 6d 6f 76  static int remov
38e8d 65 4e 6f 64 65 28 52 74 72 65 65 20 2a 70 52 74  eNode(Rtree *pRt
38e8e 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a  ree, RtreeNode *
38e8f 70 4e 6f 64 65 2c 20 69 6e 74 20 69 48 65 69 67  pNode, int iHeig
38e90 68 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  ht){.  int rc;. 
38e91 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 50 61 72   RtreeNode *pPar
38e92 65 6e 74 3b 0a 20 20 69 6e 74 20 69 43 65 6c 6c  ent;.  int iCell
38e93 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4e 6f  ;..  assert( pNo
38e94 64 65 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a 0a  de->nRef==1 );..
38e95 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20    /* Remove the 
38e96 65 6e 74 72 79 20 69 6e 20 74 68 65 20 70 61 72  entry in the par
38e97 65 6e 74 20 63 65 6c 6c 2e 20 2a 2f 0a 20 20 69  ent cell. */.  i
38e98 43 65 6c 6c 20 3d 20 6e 6f 64 65 50 61 72 65 6e  Cell = nodeParen
38e99 74 49 6e 64 65 78 28 70 52 74 72 65 65 2c 20 70  tIndex(pRtree, p
38e9a 4e 6f 64 65 29 3b 0a 20 20 70 50 61 72 65 6e 74  Node);.  pParent
38e9b 20 3d 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e   = pNode->pParen
38e9c 74 3b 0a 20 20 70 4e 6f 64 65 2d 3e 70 50 61 72  t;.  pNode->pPar
38e9d 65 6e 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 53  ent = 0;.  if( S
38e9e 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20  QLITE_OK!=(rc = 
38e9f 64 65 6c 65 74 65 43 65 6c 6c 28 70 52 74 72 65  deleteCell(pRtre
38ea0 65 2c 20 70 50 61 72 65 6e 74 2c 20 69 43 65 6c  e, pParent, iCel
38ea1 6c 2c 20 69 48 65 69 67 68 74 2b 31 29 29 20 0a  l, iHeight+1)) .
38ea2 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21     || SQLITE_OK!
38ea3 3d 28 72 63 20 3d 20 6e 6f 64 65 52 65 6c 65 61  =(rc = nodeRelea
38ea4 73 65 28 70 52 74 72 65 65 2c 20 70 50 61 72 65  se(pRtree, pPare
38ea5 6e 74 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65  nt)).  ){.    re
38ea6 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
38ea7 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 78 78  /* Remove the xx
38ea8 78 5f 6e 6f 64 65 20 65 6e 74 72 79 2e 20 2a 2f  x_node entry. */
38ea9 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
38eaa 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 44  int64(pRtree->pD
38eab 65 6c 65 74 65 4e 6f 64 65 2c 20 31 2c 20 70 4e  eleteNode, 1, pN
38eac 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 73  ode->iNode);.  s
38ead 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52 74 72  qlite3_step(pRtr
38eae 65 65 2d 3e 70 44 65 6c 65 74 65 4e 6f 64 65 29  ee->pDeleteNode)
38eaf 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  ;.  if( SQLITE_O
38eb0 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33  K!=(rc = sqlite3
38eb1 5f 72 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70  _reset(pRtree->p
38eb2 44 65 6c 65 74 65 4e 6f 64 65 29 29 20 29 7b 0a  DeleteNode)) ){.
38eb3 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
38eb4 20 7d 0a 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20   }..  /* Remove 
38eb5 74 68 65 20 78 78 78 5f 70 61 72 65 6e 74 20 65  the xxx_parent e
38eb6 6e 74 72 79 2e 20 2a 2f 0a 20 20 73 71 6c 69 74  ntry. */.  sqlit
38eb7 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52  e3_bind_int64(pR
38eb8 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 50 61 72  tree->pDeletePar
38eb9 65 6e 74 2c 20 31 2c 20 70 4e 6f 64 65 2d 3e 69  ent, 1, pNode->i
38eba 4e 6f 64 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  Node);.  sqlite3
38ebb 5f 73 74 65 70 28 70 52 74 72 65 65 2d 3e 70 44  _step(pRtree->pD
38ebc 65 6c 65 74 65 50 61 72 65 6e 74 29 3b 0a 20 20  eleteParent);.  
38ebd 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28  if( SQLITE_OK!=(
38ebe 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73  rc = sqlite3_res
38ebf 65 74 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65  et(pRtree->pDele
38ec0 74 65 50 61 72 65 6e 74 29 29 20 29 7b 0a 20 20  teParent)) ){.  
38ec1 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
38ec2 0a 20 20 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20  .  .  /* Remove 
38ec3 74 68 65 20 6e 6f 64 65 20 66 72 6f 6d 20 74 68  the node from th
38ec4 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 68 61 73 68  e in-memory hash
38ec5 20 74 61 62 6c 65 20 61 6e 64 20 6c 69 6e 6b 20   table and link 
38ec6 69 74 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68 65  it into.  ** the
38ec7 20 52 74 72 65 65 2e 70 44 65 6c 65 74 65 64 20   Rtree.pDeleted 
38ec8 6c 69 73 74 2e 20 49 74 73 20 63 6f 6e 74 65 6e  list. Its conten
38ec9 74 73 20 77 69 6c 6c 20 62 65 20 72 65 2d 69 6e  ts will be re-in
38eca 73 65 72 74 65 64 20 6c 61 74 65 72 20 6f 6e 2e  serted later on.
38ecb 0a 20 20 2a 2f 0a 20 20 6e 6f 64 65 48 61 73 68  .  */.  nodeHash
38ecc 44 65 6c 65 74 65 28 70 52 74 72 65 65 2c 20 70  Delete(pRtree, p
38ecd 4e 6f 64 65 29 3b 0a 20 20 70 4e 6f 64 65 2d 3e  Node);.  pNode->
38ece 69 4e 6f 64 65 20 3d 20 69 48 65 69 67 68 74 3b  iNode = iHeight;
38ecf 0a 20 20 70 4e 6f 64 65 2d 3e 70 4e 65 78 74 20  .  pNode->pNext 
38ed0 3d 20 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74  = pRtree->pDelet
38ed1 65 64 3b 0a 20 20 70 4e 6f 64 65 2d 3e 6e 52 65  ed;.  pNode->nRe
38ed2 66 2b 2b 3b 0a 20 20 70 52 74 72 65 65 2d 3e 70  f++;.  pRtree->p
38ed3 44 65 6c 65 74 65 64 20 3d 20 70 4e 6f 64 65 3b  Deleted = pNode;
38ed4 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
38ed5 45 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  E_OK;.}..static 
38ed6 76 6f 69 64 20 66 69 78 42 6f 75 6e 64 69 6e 67  void fixBounding
38ed7 42 6f 78 28 52 74 72 65 65 20 2a 70 52 74 72 65  Box(Rtree *pRtre
38ed8 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e  e, RtreeNode *pN
38ed9 6f 64 65 29 7b 0a 20 20 52 74 72 65 65 4e 6f 64  ode){.  RtreeNod
38eda 65 20 2a 70 50 61 72 65 6e 74 20 3d 20 70 4e 6f  e *pParent = pNo
38edb 64 65 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20 69  de->pParent;.  i
38edc 66 28 20 70 50 61 72 65 6e 74 20 29 7b 0a 20 20  f( pParent ){.  
38edd 20 20 69 6e 74 20 69 69 3b 20 0a 20 20 20 20 69    int ii; .    i
38ede 6e 74 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c  nt nCell = NCELL
38edf 28 70 4e 6f 64 65 29 3b 0a 20 20 20 20 52 74 72  (pNode);.    Rtr
38ee0 65 65 43 65 6c 6c 20 62 6f 78 3b 20 20 20 20 20  eeCell box;     
38ee1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38ee2 20 20 20 20 20 20 20 2f 2a 20 42 6f 75 6e 64 69         /* Boundi
38ee3 6e 67 20 62 6f 78 20 66 6f 72 20 70 4e 6f 64 65  ng box for pNode
38ee4 20 2a 2f 0a 20 20 20 20 6e 6f 64 65 47 65 74 43   */.    nodeGetC
38ee5 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64  ell(pRtree, pNod
38ee6 65 2c 20 30 2c 20 26 62 6f 78 29 3b 0a 20 20 20  e, 0, &box);.   
38ee7 20 66 6f 72 28 69 69 3d 31 3b 20 69 69 3c 6e 43   for(ii=1; ii<nC
38ee8 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  ell; ii++){.    
38ee9 20 20 52 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c    RtreeCell cell
38eea 3b 0a 20 20 20 20 20 20 6e 6f 64 65 47 65 74 43  ;.      nodeGetC
38eeb 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64  ell(pRtree, pNod
38eec 65 2c 20 69 69 2c 20 26 63 65 6c 6c 29 3b 0a 20  e, ii, &cell);. 
38eed 20 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70       cellUnion(p
38eee 52 74 72 65 65 2c 20 26 62 6f 78 2c 20 26 63 65  Rtree, &box, &ce
38eef 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 62  ll);.    }.    b
38ef0 6f 78 2e 69 52 6f 77 69 64 20 3d 20 70 4e 6f 64  ox.iRowid = pNod
38ef1 65 2d 3e 69 4e 6f 64 65 3b 0a 20 20 20 20 69 69  e->iNode;.    ii
38ef2 20 3d 20 6e 6f 64 65 50 61 72 65 6e 74 49 6e 64   = nodeParentInd
38ef3 65 78 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ex(pRtree, pNode
38ef4 29 3b 0a 20 20 20 20 6e 6f 64 65 4f 76 65 72 77  );.    nodeOverw
38ef5 72 69 74 65 43 65 6c 6c 28 70 52 74 72 65 65 2c  riteCell(pRtree,
38ef6 20 70 50 61 72 65 6e 74 2c 20 26 62 6f 78 2c 20   pParent, &box, 
38ef7 69 69 29 3b 0a 20 20 20 20 66 69 78 42 6f 75 6e  ii);.    fixBoun
38ef8 64 69 6e 67 42 6f 78 28 70 52 74 72 65 65 2c 20  dingBox(pRtree, 
38ef9 70 50 61 72 65 6e 74 29 3b 0a 20 20 7d 0a 7d 0a  pParent);.  }.}.
38efa 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68  ./*.** Delete th
38efb 65 20 63 65 6c 6c 20 61 74 20 69 6e 64 65 78 20  e cell at index 
38efc 69 43 65 6c 6c 20 6f 66 20 6e 6f 64 65 20 70 4e  iCell of node pN
38efd 6f 64 65 2e 20 41 66 74 65 72 20 72 65 6d 6f 76  ode. After remov
38efe 69 6e 67 20 74 68 65 0a 2a 2a 20 63 65 6c 6c 2c  ing the.** cell,
38eff 20 61 64 6a 75 73 74 20 74 68 65 20 72 2d 74 72   adjust the r-tr
38f00 65 65 20 64 61 74 61 20 73 74 72 75 63 74 75 72  ee data structur
38f01 65 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a 2a  e if required..*
38f02 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 6c  /.static int del
38f03 65 74 65 43 65 6c 6c 28 52 74 72 65 65 20 2a 70  eteCell(Rtree *p
38f04 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65  Rtree, RtreeNode
38f05 20 2a 70 4e 6f 64 65 2c 20 69 6e 74 20 69 43 65   *pNode, int iCe
38f06 6c 6c 2c 20 69 6e 74 20 69 48 65 69 67 68 74 29  ll, int iHeight)
38f07 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 69  {.  int rc;..  i
38f08 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72  f( SQLITE_OK!=(r
38f09 63 20 3d 20 66 69 78 4c 65 61 66 50 61 72 65 6e  c = fixLeafParen
38f0a 74 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29  t(pRtree, pNode)
38f0b 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
38f0c 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65  rc;.  }..  /* Re
38f0d 6d 6f 76 65 20 74 68 65 20 63 65 6c 6c 20 66 72  move the cell fr
38f0e 6f 6d 20 74 68 65 20 6e 6f 64 65 2e 20 54 68 69  om the node. Thi
38f0f 73 20 63 61 6c 6c 20 6a 75 73 74 20 6d 6f 76 65  s call just move
38f10 73 20 62 79 74 65 73 20 61 72 6f 75 6e 64 0a 20  s bytes around. 
38f11 20 2a 2a 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72   ** the in-memor
38f12 79 20 6e 6f 64 65 20 69 6d 61 67 65 2c 20 73 6f  y node image, so
38f13 20 69 74 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e   it cannot fail.
38f14 0a 20 20 2a 2f 0a 20 20 6e 6f 64 65 44 65 6c 65  .  */.  nodeDele
38f15 74 65 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  teCell(pRtree, p
38f16 4e 6f 64 65 2c 20 69 43 65 6c 6c 29 3b 0a 0a 20  Node, iCell);.. 
38f17 20 2f 2a 20 49 66 20 74 68 65 20 6e 6f 64 65 20   /* If the node 
38f18 69 73 20 6e 6f 74 20 74 68 65 20 74 72 65 65 20  is not the tree 
38f19 72 6f 6f 74 20 61 6e 64 20 6e 6f 77 20 68 61 73  root and now has
38f1a 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 6d   less than the m
38f1b 69 6e 69 6d 75 6d 0a 20 20 2a 2a 20 6e 75 6d 62  inimum.  ** numb
38f1c 65 72 20 6f 66 20 63 65 6c 6c 73 2c 20 72 65 6d  er of cells, rem
38f1d 6f 76 65 20 69 74 20 66 72 6f 6d 20 74 68 65 20  ove it from the 
38f1e 74 72 65 65 2e 20 4f 74 68 65 72 77 69 73 65 2c  tree. Otherwise,
38f1f 20 75 70 64 61 74 65 20 74 68 65 0a 20 20 2a 2a   update the.  **
38f20 20 63 65 6c 6c 20 69 6e 20 74 68 65 20 70 61 72   cell in the par
38f21 65 6e 74 20 6e 6f 64 65 20 73 6f 20 74 68 61 74  ent node so that
38f22 20 69 74 20 74 69 67 68 74 6c 79 20 63 6f 6e 74   it tightly cont
38f23 61 69 6e 73 20 74 68 65 20 75 70 64 61 74 65 64  ains the updated
38f24 0a 20 20 2a 2a 20 6e 6f 64 65 2e 0a 20 20 2a 2f  .  ** node..  */
38f25 0a 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69 4e  .  if( pNode->iN
38f26 6f 64 65 21 3d 31 20 29 7b 0a 20 20 20 20 52 74  ode!=1 ){.    Rt
38f27 72 65 65 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74  reeNode *pParent
38f28 20 3d 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e   = pNode->pParen
38f29 74 3b 0a 20 20 20 20 69 66 28 20 28 70 50 61 72  t;.    if( (pPar
38f2a 65 6e 74 2d 3e 69 4e 6f 64 65 21 3d 31 20 7c 7c  ent->iNode!=1 ||
38f2b 20 4e 43 45 4c 4c 28 70 50 61 72 65 6e 74 29 21   NCELL(pParent)!
38f2c 3d 31 29 20 0a 20 20 20 20 20 26 26 20 28 4e 43  =1) .     && (NC
38f2d 45 4c 4c 28 70 4e 6f 64 65 29 3c 52 54 52 45 45  ELL(pNode)<RTREE
38f2e 5f 4d 49 4e 43 45 4c 4c 53 28 70 52 74 72 65 65  _MINCELLS(pRtree
38f2f 29 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  )).    ){.      
38f30 72 63 20 3d 20 72 65 6d 6f 76 65 4e 6f 64 65 28  rc = removeNode(
38f31 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69  pRtree, pNode, i
38f32 48 65 69 67 68 74 29 3b 0a 20 20 20 20 7d 65 6c  Height);.    }el
38f33 73 65 7b 0a 20 20 20 20 20 20 66 69 78 42 6f 75  se{.      fixBou
38f34 6e 64 69 6e 67 42 6f 78 28 70 52 74 72 65 65 2c  ndingBox(pRtree,
38f35 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 20   pNode);.    }. 
38f36 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
38f37 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 52  .}..static int R
38f38 65 69 6e 73 65 72 74 28 0a 20 20 52 74 72 65 65  einsert(.  Rtree
38f39 20 2a 70 52 74 72 65 65 2c 20 0a 20 20 52 74 72   *pRtree, .  Rtr
38f3a 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 0a  eeNode *pNode, .
38f3b 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65    RtreeCell *pCe
38f3c 6c 6c 2c 20 0a 20 20 69 6e 74 20 69 48 65 69 67  ll, .  int iHeig
38f3d 68 74 0a 29 7b 0a 20 20 69 6e 74 20 2a 61 4f 72  ht.){.  int *aOr
38f3e 64 65 72 3b 0a 20 20 69 6e 74 20 2a 61 53 70 61  der;.  int *aSpa
38f3f 72 65 3b 0a 20 20 52 74 72 65 65 43 65 6c 6c 20  re;.  RtreeCell 
38f40 2a 61 43 65 6c 6c 3b 0a 20 20 66 6c 6f 61 74 20  *aCell;.  float 
38f41 2a 61 44 69 73 74 61 6e 63 65 3b 0a 20 20 69 6e  *aDistance;.  in
38f42 74 20 6e 43 65 6c 6c 3b 0a 20 20 66 6c 6f 61 74  t nCell;.  float
38f43 20 61 43 65 6e 74 65 72 43 6f 6f 72 64 5b 52 54   aCenterCoord[RT
38f44 52 45 45 5f 4d 41 58 5f 44 49 4d 45 4e 53 49 4f  REE_MAX_DIMENSIO
38f45 4e 53 5d 3b 0a 20 20 69 6e 74 20 69 44 69 6d 3b  NS];.  int iDim;
38f46 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74  .  int ii;.  int
38f47 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
38f48 0a 0a 20 20 6d 65 6d 73 65 74 28 61 43 65 6e 74  ..  memset(aCent
38f49 65 72 43 6f 6f 72 64 2c 20 30 2c 20 73 69 7a 65  erCoord, 0, size
38f4a 6f 66 28 66 6c 6f 61 74 29 2a 52 54 52 45 45 5f  of(float)*RTREE_
38f4b 4d 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 29 3b  MAX_DIMENSIONS);
38f4c 0a 0a 20 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c  ..  nCell = NCEL
38f4d 4c 28 70 4e 6f 64 65 29 2b 31 3b 0a 0a 20 20 2f  L(pNode)+1;..  /
38f4e 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 62  * Allocate the b
38f4f 75 66 66 65 72 73 20 75 73 65 64 20 62 79 20 74  uffers used by t
38f50 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 20 54  his operation. T
38f51 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73  he allocation is
38f52 0a 20 20 2a 2a 20 72 65 6c 69 6e 71 75 69 73 68  .  ** relinquish
38f53 65 64 20 62 65 66 6f 72 65 20 74 68 69 73 20 66  ed before this f
38f54 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e  unction returns.
38f55 0a 20 20 2a 2f 0a 20 20 61 43 65 6c 6c 20 3d 20  .  */.  aCell = 
38f56 28 52 74 72 65 65 43 65 6c 6c 20 2a 29 73 71 6c  (RtreeCell *)sql
38f57 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 43 65 6c  ite3_malloc(nCel
38f58 6c 20 2a 20 28 0a 20 20 20 20 73 69 7a 65 6f 66  l * (.    sizeof
38f59 28 52 74 72 65 65 43 65 6c 6c 29 20 2b 20 20 20  (RtreeCell) +   
38f5a 20 20 20 20 20 20 2f 2a 20 61 43 65 6c 6c 20 61        /* aCell a
38f5b 72 72 61 79 20 2a 2f 0a 20 20 20 20 73 69 7a 65  rray */.    size
38f5c 6f 66 28 69 6e 74 29 20 20 20 20 20 20 20 2b 20  of(int)       + 
38f5d 20 20 20 20 20 20 20 20 2f 2a 20 61 4f 72 64 65          /* aOrde
38f5e 72 20 61 72 72 61 79 20 2a 2f 0a 20 20 20 20 73  r array */.    s
38f5f 69 7a 65 6f 66 28 69 6e 74 29 20 20 20 20 20 20  izeof(int)      
38f60 20 2b 20 20 20 20 20 20 20 20 20 2f 2a 20 61 53   +         /* aS
38f61 70 61 72 65 20 61 72 72 61 79 20 2a 2f 0a 20 20  pare array */.  
38f62 20 20 73 69 7a 65 6f 66 28 66 6c 6f 61 74 29 20    sizeof(float) 
38f63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
38f64 20 61 44 69 73 74 61 6e 63 65 20 61 72 72 61 79   aDistance array
38f65 20 2a 2f 0a 20 20 29 29 3b 0a 20 20 69 66 28 20   */.  ));.  if( 
38f66 21 61 43 65 6c 6c 20 29 7b 0a 20 20 20 20 72 65  !aCell ){.    re
38f67 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
38f68 4d 3b 0a 20 20 7d 0a 20 20 61 4f 72 64 65 72 20  M;.  }.  aOrder 
38f69 20 20 20 3d 20 28 69 6e 74 20 2a 29 26 61 43 65     = (int *)&aCe
38f6a 6c 6c 5b 6e 43 65 6c 6c 5d 3b 0a 20 20 61 53 70  ll[nCell];.  aSp
38f6b 61 72 65 20 20 20 20 3d 20 28 69 6e 74 20 2a 29  are    = (int *)
38f6c 26 61 4f 72 64 65 72 5b 6e 43 65 6c 6c 5d 3b 0a  &aOrder[nCell];.
38f6d 20 20 61 44 69 73 74 61 6e 63 65 20 3d 20 28 66    aDistance = (f
38f6e 6c 6f 61 74 20 2a 29 26 61 53 70 61 72 65 5b 6e  loat *)&aSpare[n
38f6f 43 65 6c 6c 5d 3b 0a 0a 20 20 66 6f 72 28 69 69  Cell];..  for(ii
38f70 3d 30 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69  =0; ii<nCell; ii
38f71 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 69 69 3d  ++){.    if( ii=
38f72 3d 28 6e 43 65 6c 6c 2d 31 29 20 29 7b 0a 20 20  =(nCell-1) ){.  
38f73 20 20 20 20 6d 65 6d 63 70 79 28 26 61 43 65 6c      memcpy(&aCel
38f74 6c 5b 69 69 5d 2c 20 70 43 65 6c 6c 2c 20 73 69  l[ii], pCell, si
38f75 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 29  zeof(RtreeCell))
38f76 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
38f77 20 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70     nodeGetCell(p
38f78 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 69  Rtree, pNode, ii
38f79 2c 20 26 61 43 65 6c 6c 5b 69 69 5d 29 3b 0a 20  , &aCell[ii]);. 
38f7a 20 20 20 7d 0a 20 20 20 20 61 4f 72 64 65 72 5b     }.    aOrder[
38f7b 69 69 5d 20 3d 20 69 69 3b 0a 20 20 20 20 66 6f  ii] = ii;.    fo
38f7c 72 28 69 44 69 6d 3d 30 3b 20 69 44 69 6d 3c 70  r(iDim=0; iDim<p
38f7d 52 74 72 65 65 2d 3e 6e 44 69 6d 3b 20 69 44 69  Rtree->nDim; iDi
38f7e 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 61 43 65 6e  m++){.      aCen
38f7f 74 65 72 43 6f 6f 72 64 5b 69 44 69 6d 5d 20 2b  terCoord[iDim] +
38f80 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 69  = DCOORD(aCell[i
38f81 69 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32  i].aCoord[iDim*2
38f82 5d 29 3b 0a 20 20 20 20 20 20 61 43 65 6e 74 65  ]);.      aCente
38f83 72 43 6f 6f 72 64 5b 69 44 69 6d 5d 20 2b 3d 20  rCoord[iDim] += 
38f84 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 69 69 5d  DCOORD(aCell[ii]
38f85 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31  .aCoord[iDim*2+1
38f86 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ]);.    }.  }.  
38f87 66 6f 72 28 69 44 69 6d 3d 30 3b 20 69 44 69 6d  for(iDim=0; iDim
38f88 3c 70 52 74 72 65 65 2d 3e 6e 44 69 6d 3b 20 69  <pRtree->nDim; i
38f89 44 69 6d 2b 2b 29 7b 0a 20 20 20 20 61 43 65 6e  Dim++){.    aCen
38f8a 74 65 72 43 6f 6f 72 64 5b 69 44 69 6d 5d 20 3d  terCoord[iDim] =
38f8b 20 61 43 65 6e 74 65 72 43 6f 6f 72 64 5b 69 44   aCenterCoord[iD
38f8c 69 6d 5d 2f 28 28 66 6c 6f 61 74 29 6e 43 65 6c  im]/((float)nCel
38f8d 6c 2a 32 2e 30 29 3b 0a 20 20 7d 0a 0a 20 20 66  l*2.0);.  }..  f
38f8e 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43 65 6c  or(ii=0; ii<nCel
38f8f 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 61 44  l; ii++){.    aD
38f90 69 73 74 61 6e 63 65 5b 69 69 5d 20 3d 20 30 2e  istance[ii] = 0.
38f91 30 3b 0a 20 20 20 20 66 6f 72 28 69 44 69 6d 3d  0;.    for(iDim=
38f92 30 3b 20 69 44 69 6d 3c 70 52 74 72 65 65 2d 3e  0; iDim<pRtree->
38f93 6e 44 69 6d 3b 20 69 44 69 6d 2b 2b 29 7b 0a 20  nDim; iDim++){. 
38f94 20 20 20 20 20 66 6c 6f 61 74 20 63 6f 6f 72 64       float coord
38f95 20 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b   = DCOORD(aCell[
38f96 69 69 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a  ii].aCoord[iDim*
38f97 32 2b 31 5d 29 20 2d 20 0a 20 20 20 20 20 20 20  2+1]) - .       
38f98 20 20 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b     DCOORD(aCell[
38f99 69 69 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a  ii].aCoord[iDim*
38f9a 32 5d 29 3b 0a 20 20 20 20 20 20 61 44 69 73 74  2]);.      aDist
38f9b 61 6e 63 65 5b 69 69 5d 20 2b 3d 20 28 63 6f 6f  ance[ii] += (coo
38f9c 72 64 2d 61 43 65 6e 74 65 72 43 6f 6f 72 64 5b  rd-aCenterCoord[
38f9d 69 44 69 6d 5d 29 2a 28 63 6f 6f 72 64 2d 61 43  iDim])*(coord-aC
38f9e 65 6e 74 65 72 43 6f 6f 72 64 5b 69 44 69 6d 5d  enterCoord[iDim]
38f9f 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
38fa0 53 6f 72 74 42 79 44 69 73 74 61 6e 63 65 28 61  SortByDistance(a
38fa1 4f 72 64 65 72 2c 20 6e 43 65 6c 6c 2c 20 61 44  Order, nCell, aD
38fa2 69 73 74 61 6e 63 65 2c 20 61 53 70 61 72 65 29  istance, aSpare)
38fa3 3b 0a 20 20 6e 6f 64 65 5a 65 72 6f 28 70 52 74  ;.  nodeZero(pRt
38fa4 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 0a 20 20  ree, pNode);..  
38fa5 66 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d 53 51  for(ii=0; rc==SQ
38fa6 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c 28 6e  LITE_OK && ii<(n
38fa7 43 65 6c 6c 2d 28 52 54 52 45 45 5f 4d 49 4e 43  Cell-(RTREE_MINC
38fa8 45 4c 4c 53 28 70 52 74 72 65 65 29 2b 31 29 29  ELLS(pRtree)+1))
38fa9 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 52 74 72  ; ii++){.    Rtr
38faa 65 65 43 65 6c 6c 20 2a 70 20 3d 20 26 61 43 65  eeCell *p = &aCe
38fab 6c 6c 5b 61 4f 72 64 65 72 5b 69 69 5d 5d 3b 0a  ll[aOrder[ii]];.
38fac 20 20 20 20 6e 6f 64 65 49 6e 73 65 72 74 43 65      nodeInsertCe
38fad 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ll(pRtree, pNode
38fae 2c 20 70 29 3b 0a 20 20 20 20 69 66 28 20 70 2d  , p);.    if( p-
38faf 3e 69 52 6f 77 69 64 3d 3d 70 43 65 6c 6c 2d 3e  >iRowid==pCell->
38fb0 69 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20  iRowid ){.      
38fb1 69 66 28 20 69 48 65 69 67 68 74 3d 3d 30 20 29  if( iHeight==0 )
38fb2 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72  {.        rc = r
38fb3 6f 77 69 64 57 72 69 74 65 28 70 52 74 72 65 65  owidWrite(pRtree
38fb4 2c 20 70 2d 3e 69 52 6f 77 69 64 2c 20 70 4e 6f  , p->iRowid, pNo
38fb5 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20  de->iNode);.    
38fb6 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
38fb7 20 72 63 20 3d 20 70 61 72 65 6e 74 57 72 69 74   rc = parentWrit
38fb8 65 28 70 52 74 72 65 65 2c 20 70 2d 3e 69 52 6f  e(pRtree, p->iRo
38fb9 77 69 64 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64  wid, pNode->iNod
38fba 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  e);.      }.    
38fbb 7d 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d  }.  }.  if( rc==
38fbc 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
38fbd 20 66 69 78 42 6f 75 6e 64 69 6e 67 42 6f 78 28   fixBoundingBox(
38fbe 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a  pRtree, pNode);.
38fbf 20 20 7d 0a 20 20 66 6f 72 28 3b 20 72 63 3d 3d    }.  for(; rc==
38fc0 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c  SQLITE_OK && ii<
38fc1 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20  nCell; ii++){.  
38fc2 20 20 2f 2a 20 46 69 6e 64 20 61 20 6e 6f 64 65    /* Find a node
38fc3 20 74 6f 20 73 74 6f 72 65 20 74 68 69 73 20 63   to store this c
38fc4 65 6c 6c 20 69 6e 2e 20 70 4e 6f 64 65 2d 3e 69  ell in. pNode->i
38fc5 4e 6f 64 65 20 63 75 72 72 65 6e 74 6c 79 20 63  Node currently c
38fc6 6f 6e 74 61 69 6e 73 0a 20 20 20 20 2a 2a 20 74  ontains.    ** t
38fc7 68 65 20 68 65 69 67 68 74 20 6f 66 20 74 68 65  he height of the
38fc8 20 73 75 62 2d 74 72 65 65 20 68 65 61 64 65 64   sub-tree headed
38fc9 20 62 79 20 74 68 65 20 63 65 6c 6c 2e 0a 20 20   by the cell..  
38fca 20 20 2a 2f 0a 20 20 20 20 52 74 72 65 65 4e 6f    */.    RtreeNo
38fcb 64 65 20 2a 70 49 6e 73 65 72 74 3b 0a 20 20 20  de *pInsert;.   
38fcc 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 20 3d 20   RtreeCell *p = 
38fcd 26 61 43 65 6c 6c 5b 61 4f 72 64 65 72 5b 69 69  &aCell[aOrder[ii
38fce 5d 5d 3b 0a 20 20 20 20 72 63 20 3d 20 43 68 6f  ]];.    rc = Cho
38fcf 6f 73 65 4c 65 61 66 28 70 52 74 72 65 65 2c 20  oseLeaf(pRtree, 
38fd0 70 2c 20 69 48 65 69 67 68 74 2c 20 26 70 49 6e  p, iHeight, &pIn
38fd1 73 65 72 74 29 3b 0a 20 20 20 20 69 66 28 20 72  sert);.    if( r
38fd2 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
38fd3 20 20 20 20 20 20 69 6e 74 20 72 63 32 3b 0a 20        int rc2;. 
38fd4 20 20 20 20 20 72 63 20 3d 20 72 74 72 65 65 49       rc = rtreeI
38fd5 6e 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65  nsertCell(pRtree
38fd6 2c 20 70 49 6e 73 65 72 74 2c 20 70 2c 20 69 48  , pInsert, p, iH
38fd7 65 69 67 68 74 29 3b 0a 20 20 20 20 20 20 72 63  eight);.      rc
38fd8 32 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28  2 = nodeRelease(
38fd9 70 52 74 72 65 65 2c 20 70 49 6e 73 65 72 74 29  pRtree, pInsert)
38fda 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  ;.      if( rc==
38fdb 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
38fdc 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20       rc = rc2;. 
38fdd 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
38fde 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ..  sqlite3_free
38fdf 28 61 43 65 6c 6c 29 3b 0a 20 20 72 65 74 75 72  (aCell);.  retur
38fe0 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  n rc;.}../*.** I
38fe1 6e 73 65 72 74 20 63 65 6c 6c 20 70 43 65 6c 6c  nsert cell pCell
38fe2 20 69 6e 74 6f 20 6e 6f 64 65 20 70 4e 6f 64 65   into node pNode
38fe3 2e 20 4e 6f 64 65 20 70 4e 6f 64 65 20 69 73 20  . Node pNode is 
38fe4 74 68 65 20 68 65 61 64 20 6f 66 20 61 20 0a 2a  the head of a .*
38fe5 2a 20 73 75 62 74 72 65 65 20 69 48 65 69 67 68  * subtree iHeigh
38fe6 74 20 68 69 67 68 20 28 6c 65 61 66 20 6e 6f 64  t high (leaf nod
38fe7 65 73 20 68 61 76 65 20 69 48 65 69 67 68 74 3d  es have iHeight=
38fe8 3d 30 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  =0)..*/.static i
38fe9 6e 74 20 72 74 72 65 65 49 6e 73 65 72 74 43 65  nt rtreeInsertCe
38fea 6c 6c 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74  ll(.  Rtree *pRt
38feb 72 65 65 2c 0a 20 20 52 74 72 65 65 4e 6f 64 65  ree,.  RtreeNode
38fec 20 2a 70 4e 6f 64 65 2c 0a 20 20 52 74 72 65 65   *pNode,.  Rtree
38fed 43 65 6c 6c 20 2a 70 43 65 6c 6c 2c 0a 20 20 69  Cell *pCell,.  i
38fee 6e 74 20 69 48 65 69 67 68 74 0a 29 7b 0a 20 20  nt iHeight.){.  
38fef 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
38ff0 4f 4b 3b 0a 20 20 69 66 28 20 69 48 65 69 67 68  OK;.  if( iHeigh
38ff1 74 3e 30 20 29 7b 0a 20 20 20 20 52 74 72 65 65  t>0 ){.    Rtree
38ff2 4e 6f 64 65 20 2a 70 43 68 69 6c 64 20 3d 20 6e  Node *pChild = n
38ff3 6f 64 65 48 61 73 68 4c 6f 6f 6b 75 70 28 70 52  odeHashLookup(pR
38ff4 74 72 65 65 2c 20 70 43 65 6c 6c 2d 3e 69 52 6f  tree, pCell->iRo
38ff5 77 69 64 29 3b 0a 20 20 20 20 69 66 28 20 70 43  wid);.    if( pC
38ff6 68 69 6c 64 20 29 7b 0a 20 20 20 20 20 20 6e 6f  hild ){.      no
38ff7 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65  deRelease(pRtree
38ff8 2c 20 70 43 68 69 6c 64 2d 3e 70 50 61 72 65 6e  , pChild->pParen
38ff9 74 29 3b 0a 20 20 20 20 20 20 6e 6f 64 65 52 65  t);.      nodeRe
38ffa 66 65 72 65 6e 63 65 28 70 4e 6f 64 65 29 3b 0a  ference(pNode);.
38ffb 20 20 20 20 20 20 70 43 68 69 6c 64 2d 3e 70 50        pChild->pP
38ffc 61 72 65 6e 74 20 3d 20 70 4e 6f 64 65 3b 0a 20  arent = pNode;. 
38ffd 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6e     }.  }.  if( n
38ffe 6f 64 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52  odeInsertCell(pR
38fff 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70 43 65  tree, pNode, pCe
39000 6c 6c 29 20 29 7b 0a 23 69 66 20 56 41 52 49 41  ll) ){.#if VARIA
39001 4e 54 5f 52 53 54 41 52 54 52 45 45 5f 52 45 49  NT_RSTARTREE_REI
39002 4e 53 45 52 54 0a 20 20 20 20 69 66 28 20 69 48  NSERT.    if( iH
39003 65 69 67 68 74 3c 3d 70 52 74 72 65 65 2d 3e 69  eight<=pRtree->i
39004 52 65 69 6e 73 65 72 74 48 65 69 67 68 74 20 7c  ReinsertHeight |
39005 7c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3d 3d  | pNode->iNode==
39006 31 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53  1){.      rc = S
39007 70 6c 69 74 4e 6f 64 65 28 70 52 74 72 65 65 2c  plitNode(pRtree,
39008 20 70 4e 6f 64 65 2c 20 70 43 65 6c 6c 2c 20 69   pNode, pCell, i
39009 48 65 69 67 68 74 29 3b 0a 20 20 20 20 7d 65 6c  Height);.    }el
3900a 73 65 7b 0a 20 20 20 20 20 20 70 52 74 72 65 65  se{.      pRtree
3900b 2d 3e 69 52 65 69 6e 73 65 72 74 48 65 69 67 68  ->iReinsertHeigh
3900c 74 20 3d 20 69 48 65 69 67 68 74 3b 0a 20 20 20  t = iHeight;.   
3900d 20 20 20 72 63 20 3d 20 52 65 69 6e 73 65 72 74     rc = Reinsert
3900e 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20  (pRtree, pNode, 
3900f 70 43 65 6c 6c 2c 20 69 48 65 69 67 68 74 29 3b  pCell, iHeight);
39010 0a 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20  .    }.#else.   
39011 20 72 63 20 3d 20 53 70 6c 69 74 4e 6f 64 65 28   rc = SplitNode(
39012 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70  pRtree, pNode, p
39013 43 65 6c 6c 2c 20 69 48 65 69 67 68 74 29 3b 0a  Cell, iHeight);.
39014 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b 0a  #endif.  }else{.
39015 20 20 20 20 41 64 6a 75 73 74 54 72 65 65 28 70      AdjustTree(p
39016 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70 43  Rtree, pNode, pC
39017 65 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 69 48  ell);.    if( iH
39018 65 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20  eight==0 ){.    
39019 20 20 72 63 20 3d 20 72 6f 77 69 64 57 72 69 74    rc = rowidWrit
3901a 65 28 70 52 74 72 65 65 2c 20 70 43 65 6c 6c 2d  e(pRtree, pCell-
3901b 3e 69 52 6f 77 69 64 2c 20 70 4e 6f 64 65 2d 3e  >iRowid, pNode->
3901c 69 4e 6f 64 65 29 3b 0a 20 20 20 20 7d 65 6c 73  iNode);.    }els
3901d 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61  e{.      rc = pa
3901e 72 65 6e 74 57 72 69 74 65 28 70 52 74 72 65 65  rentWrite(pRtree
3901f 2c 20 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64 2c  , pCell->iRowid,
39020 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a   pNode->iNode);.
39021 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
39022 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63  rn rc;.}..static
39023 20 69 6e 74 20 72 65 69 6e 73 65 72 74 4e 6f 64   int reinsertNod
39024 65 43 6f 6e 74 65 6e 74 28 52 74 72 65 65 20 2a  eContent(Rtree *
39025 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64  pRtree, RtreeNod
39026 65 20 2a 70 4e 6f 64 65 29 7b 0a 20 20 69 6e 74  e *pNode){.  int
39027 20 69 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20   ii;.  int rc = 
39028 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74  SQLITE_OK;.  int
39029 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70   nCell = NCELL(p
3902a 4e 6f 64 65 29 3b 0a 0a 20 20 66 6f 72 28 69 69  Node);..  for(ii
3902b 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  =0; rc==SQLITE_O
3902c 4b 20 26 26 20 69 69 3c 6e 43 65 6c 6c 3b 20 69  K && ii<nCell; i
3902d 69 2b 2b 29 7b 0a 20 20 20 20 52 74 72 65 65 4e  i++){.    RtreeN
3902e 6f 64 65 20 2a 70 49 6e 73 65 72 74 3b 0a 20 20  ode *pInsert;.  
3902f 20 20 52 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c    RtreeCell cell
39030 3b 0a 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c  ;.    nodeGetCel
39031 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c  l(pRtree, pNode,
39032 20 69 69 2c 20 26 63 65 6c 6c 29 3b 0a 0a 20 20   ii, &cell);..  
39033 20 20 2f 2a 20 46 69 6e 64 20 61 20 6e 6f 64 65    /* Find a node
39034 20 74 6f 20 73 74 6f 72 65 20 74 68 69 73 20 63   to store this c
39035 65 6c 6c 20 69 6e 2e 20 70 4e 6f 64 65 2d 3e 69  ell in. pNode->i
39036 4e 6f 64 65 20 63 75 72 72 65 6e 74 6c 79 20 63  Node currently c
39037 6f 6e 74 61 69 6e 73 0a 20 20 20 20 2a 2a 20 74  ontains.    ** t
39038 68 65 20 68 65 69 67 68 74 20 6f 66 20 74 68 65  he height of the
39039 20 73 75 62 2d 74 72 65 65 20 68 65 61 64 65 64   sub-tree headed
3903a 20 62 79 20 74 68 65 20 63 65 6c 6c 2e 0a 20 20   by the cell..  
3903b 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 43 68    */.    rc = Ch
3903c 6f 6f 73 65 4c 65 61 66 28 70 52 74 72 65 65 2c  ooseLeaf(pRtree,
3903d 20 26 63 65 6c 6c 2c 20 70 4e 6f 64 65 2d 3e 69   &cell, pNode->i
3903e 4e 6f 64 65 2c 20 26 70 49 6e 73 65 72 74 29 3b  Node, &pInsert);
3903f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
39040 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
39041 69 6e 74 20 72 63 32 3b 0a 20 20 20 20 20 20 72  int rc2;.      r
39042 63 20 3d 20 72 74 72 65 65 49 6e 73 65 72 74 43  c = rtreeInsertC
39043 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 49 6e 73  ell(pRtree, pIns
39044 65 72 74 2c 20 26 63 65 6c 6c 2c 20 70 4e 6f 64  ert, &cell, pNod
39045 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20 20  e->iNode);.     
39046 20 72 63 32 20 3d 20 6e 6f 64 65 52 65 6c 65 61   rc2 = nodeRelea
39047 73 65 28 70 52 74 72 65 65 2c 20 70 49 6e 73 65  se(pRtree, pInse
39048 72 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  rt);.      if( r
39049 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
3904a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 63 32          rc = rc2
3904b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
3904c 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
3904d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 6c 65 63 74  .}../*.** Select
3904e 20 61 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 75   a currently unu
3904f 73 65 64 20 72 6f 77 69 64 20 66 6f 72 20 61 20  sed rowid for a 
39050 6e 65 77 20 72 2d 74 72 65 65 20 72 65 63 6f 72  new r-tree recor
39051 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
39052 20 6e 65 77 52 6f 77 69 64 28 52 74 72 65 65 20   newRowid(Rtree 
39053 2a 70 52 74 72 65 65 2c 20 69 36 34 20 2a 70 69  *pRtree, i64 *pi
39054 52 6f 77 69 64 29 7b 0a 20 20 69 6e 74 20 72 63  Rowid){.  int rc
39055 3b 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ;.  sqlite3_bind
39056 5f 6e 75 6c 6c 28 70 52 74 72 65 65 2d 3e 70 57  _null(pRtree->pW
39057 72 69 74 65 52 6f 77 69 64 2c 20 31 29 3b 0a 20  riteRowid, 1);. 
39058 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75   sqlite3_bind_nu
39059 6c 6c 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74  ll(pRtree->pWrit
3905a 65 52 6f 77 69 64 2c 20 32 29 3b 0a 20 20 73 71  eRowid, 2);.  sq
3905b 6c 69 74 65 33 5f 73 74 65 70 28 70 52 74 72 65  lite3_step(pRtre
3905c 65 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 29 3b  e->pWriteRowid);
3905d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
3905e 72 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70 57  reset(pRtree->pW
3905f 72 69 74 65 52 6f 77 69 64 29 3b 0a 20 20 2a 70  riteRowid);.  *p
39060 69 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33  iRowid = sqlite3
39061 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77  _last_insert_row
39062 69 64 28 70 52 74 72 65 65 2d 3e 64 62 29 3b 0a  id(pRtree->db);.
39063 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
39064 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 73  #ifndef NDEBUG.s
39065 74 61 74 69 63 20 69 6e 74 20 68 61 73 68 49 73  tatic int hashIs
39066 45 6d 70 74 79 28 52 74 72 65 65 20 2a 70 52 74  Empty(Rtree *pRt
39067 72 65 65 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a  ree){.  int ii;.
39068 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 48    for(ii=0; ii<H
39069 41 53 48 53 49 5a 45 3b 20 69 69 2b 2b 29 7b 0a  ASHSIZE; ii++){.
3906a 20 20 20 20 61 73 73 65 72 74 28 20 21 70 52 74      assert( !pRt
3906b 72 65 65 2d 3e 61 48 61 73 68 5b 69 69 5d 20 29  ree->aHash[ii] )
3906c 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31  ;.  }.  return 1
3906d 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
3906e 2a 20 54 68 65 20 78 55 70 64 61 74 65 20 6d 65  * The xUpdate me
3906f 74 68 6f 64 20 66 6f 72 20 72 74 72 65 65 20 6d  thod for rtree m
39070 6f 64 75 6c 65 20 76 69 72 74 75 61 6c 20 74 61  odule virtual ta
39071 62 6c 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  bles..*/.static 
39072 69 6e 74 20 72 74 72 65 65 55 70 64 61 74 65 28  int rtreeUpdate(
39073 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  .  sqlite3_vtab 
39074 2a 70 56 74 61 62 2c 20 0a 20 20 69 6e 74 20 6e  *pVtab, .  int n
39075 44 61 74 61 2c 20 0a 20 20 73 71 6c 69 74 65 33  Data, .  sqlite3
39076 5f 76 61 6c 75 65 20 2a 2a 61 7a 44 61 74 61 2c  _value **azData,
39077 20 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34   .  sqlite_int64
39078 20 2a 70 52 6f 77 69 64 0a 29 7b 0a 20 20 52 74   *pRowid.){.  Rt
39079 72 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28 52  ree *pRtree = (R
3907a 74 72 65 65 20 2a 29 70 56 74 61 62 3b 0a 20 20  tree *)pVtab;.  
3907b 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
3907c 4f 4b 3b 0a 0a 20 20 72 74 72 65 65 52 65 66 65  OK;..  rtreeRefe
3907d 72 65 6e 63 65 28 70 52 74 72 65 65 29 3b 0a 0a  rence(pRtree);..
3907e 20 20 61 73 73 65 72 74 28 6e 44 61 74 61 3e 3d    assert(nData>=
3907f 31 29 3b 0a 20 20 61 73 73 65 72 74 28 68 61 73  1);.  assert(has
39080 68 49 73 45 6d 70 74 79 28 70 52 74 72 65 65 29  hIsEmpty(pRtree)
39081 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 7a 44 61  );..  /* If azDa
39082 74 61 5b 30 5d 20 69 73 20 6e 6f 74 20 61 6e 20  ta[0] is not an 
39083 53 51 4c 20 4e 55 4c 4c 20 76 61 6c 75 65 2c 20  SQL NULL value, 
39084 69 74 20 69 73 20 74 68 65 20 72 6f 77 69 64 20  it is the rowid 
39085 6f 66 20 61 0a 20 20 2a 2a 20 72 65 63 6f 72 64  of a.  ** record
39086 20 74 6f 20 64 65 6c 65 74 65 20 66 72 6f 6d 20   to delete from 
39087 74 68 65 20 72 2d 74 72 65 65 20 74 61 62 6c 65  the r-tree table
39088 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  . The following 
39089 62 6c 6f 63 6b 20 64 6f 65 73 0a 20 20 2a 2a 20  block does.  ** 
3908a 6a 75 73 74 20 74 68 61 74 2e 0a 20 20 2a 2f 0a  just that..  */.
3908b 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61    if( sqlite3_va
3908c 6c 75 65 5f 74 79 70 65 28 61 7a 44 61 74 61 5b  lue_type(azData[
3908d 30 5d 29 21 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c  0])!=SQLITE_NULL
3908e 20 29 7b 0a 20 20 20 20 69 36 34 20 69 44 65 6c   ){.    i64 iDel
3908f 65 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ete;            
39090 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 69 64      /* The rowid
39091 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20   to delete */.  
39092 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65    RtreeNode *pLe
39093 61 66 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  af;           /*
39094 20 4c 65 61 66 20 6e 6f 64 65 20 63 6f 6e 74 61   Leaf node conta
39095 69 6e 69 6e 67 20 72 65 63 6f 72 64 20 69 44 65  ining record iDe
39096 6c 65 74 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20  lete */.    int 
39097 69 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20  iCell;          
39098 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
39099 20 6f 66 20 69 44 65 6c 65 74 65 20 63 65 6c 6c   of iDelete cell
3909a 20 69 6e 20 70 4c 65 61 66 20 2a 2f 0a 20 20 20   in pLeaf */.   
3909b 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 52 6f 6f   RtreeNode *pRoo
3909c 74 3b 0a 0a 20 20 20 20 2f 2a 20 4f 62 74 61 69  t;..    /* Obtai
3909d 6e 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f  n a reference to
3909e 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 74   the root node t
3909f 6f 20 69 6e 69 74 69 61 6c 69 73 65 20 52 74 72  o initialise Rtr
390a0 65 65 2e 69 44 65 70 74 68 20 2a 2f 0a 20 20 20  ee.iDepth */.   
390a1 20 72 63 20 3d 20 6e 6f 64 65 41 63 71 75 69 72   rc = nodeAcquir
390a2 65 28 70 52 74 72 65 65 2c 20 31 2c 20 30 2c 20  e(pRtree, 1, 0, 
390a3 26 70 52 6f 6f 74 29 3b 0a 0a 20 20 20 20 2f 2a  &pRoot);..    /*
390a4 20 4f 62 74 61 69 6e 20 61 20 72 65 66 65 72 65   Obtain a refere
390a5 6e 63 65 20 74 6f 20 74 68 65 20 6c 65 61 66 20  nce to the leaf 
390a6 6e 6f 64 65 20 74 68 61 74 20 63 6f 6e 74 61 69  node that contai
390a7 6e 73 20 74 68 65 20 65 6e 74 72 79 20 0a 20 20  ns the entry .  
390a8 20 20 2a 2a 20 61 62 6f 75 74 20 74 6f 20 62 65    ** about to be
390a9 20 64 65 6c 65 74 65 64 2e 20 0a 20 20 20 20 2a   deleted. .    *
390aa 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  /.    if( rc==SQ
390ab 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
390ac 20 69 44 65 6c 65 74 65 20 3d 20 73 71 6c 69 74   iDelete = sqlit
390ad 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61  e3_value_int64(a
390ae 7a 44 61 74 61 5b 30 5d 29 3b 0a 20 20 20 20 20  zData[0]);.     
390af 20 72 63 20 3d 20 66 69 6e 64 4c 65 61 66 4e 6f   rc = findLeafNo
390b0 64 65 28 70 52 74 72 65 65 2c 20 69 44 65 6c 65  de(pRtree, iDele
390b1 74 65 2c 20 26 70 4c 65 61 66 29 3b 0a 20 20 20  te, &pLeaf);.   
390b2 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65 74   }..    /* Delet
390b3 65 20 74 68 65 20 63 65 6c 6c 20 69 6e 20 71 75  e the cell in qu
390b4 65 73 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20  estion from the 
390b5 6c 65 61 66 20 6e 6f 64 65 2e 20 2a 2f 0a 20 20  leaf node. */.  
390b6 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
390b7 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  _OK ){.      int
390b8 20 72 63 32 3b 0a 20 20 20 20 20 20 69 43 65 6c   rc2;.      iCel
390b9 6c 20 3d 20 6e 6f 64 65 52 6f 77 69 64 49 6e 64  l = nodeRowidInd
390ba 65 78 28 70 52 74 72 65 65 2c 20 70 4c 65 61 66  ex(pRtree, pLeaf
390bb 2c 20 69 44 65 6c 65 74 65 29 3b 0a 20 20 20 20  , iDelete);.    
390bc 20 20 72 63 20 3d 20 64 65 6c 65 74 65 43 65 6c    rc = deleteCel
390bd 6c 28 70 52 74 72 65 65 2c 20 70 4c 65 61 66 2c  l(pRtree, pLeaf,
390be 20 69 43 65 6c 6c 2c 20 30 29 3b 0a 20 20 20 20   iCell, 0);.    
390bf 20 20 72 63 32 20 3d 20 6e 6f 64 65 52 65 6c 65    rc2 = nodeRele
390c0 61 73 65 28 70 52 74 72 65 65 2c 20 70 4c 65 61  ase(pRtree, pLea
390c1 66 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  f);.      if( rc
390c2 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
390c3 20 20 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b         rc = rc2;
390c4 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
390c5 20 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68      /* Delete th
390c6 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
390c7 65 6e 74 72 79 20 69 6e 20 74 68 65 20 3c 72 74  entry in the <rt
390c8 72 65 65 3e 5f 72 6f 77 69 64 20 74 61 62 6c 65  ree>_rowid table
390c9 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d  . */.    if( rc=
390ca 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
390cb 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64      sqlite3_bind
390cc 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70  _int64(pRtree->p
390cd 44 65 6c 65 74 65 52 6f 77 69 64 2c 20 31 2c 20  DeleteRowid, 1, 
390ce 69 44 65 6c 65 74 65 29 3b 0a 20 20 20 20 20 20  iDelete);.      
390cf 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52 74  sqlite3_step(pRt
390d0 72 65 65 2d 3e 70 44 65 6c 65 74 65 52 6f 77 69  ree->pDeleteRowi
390d1 64 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  d);.      rc = s
390d2 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52 74  qlite3_reset(pRt
390d3 72 65 65 2d 3e 70 44 65 6c 65 74 65 52 6f 77 69  ree->pDeleteRowi
390d4 64 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  d);.    }..    /
390d5 2a 20 43 68 65 63 6b 20 69 66 20 74 68 65 20 72  * Check if the r
390d6 6f 6f 74 20 6e 6f 64 65 20 6e 6f 77 20 68 61 73  oot node now has
390d7 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 63 68 69   exactly one chi
390d8 6c 64 2e 20 49 66 20 73 6f 2c 20 72 65 6d 6f 76  ld. If so, remov
390d9 65 0a 20 20 20 20 2a 2a 20 69 74 2c 20 73 63 68  e.    ** it, sch
390da 65 64 75 6c 65 20 74 68 65 20 63 6f 6e 74 65 6e  edule the conten
390db 74 73 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20  ts of the child 
390dc 66 6f 72 20 72 65 69 6e 73 65 72 74 69 6f 6e 20  for reinsertion 
390dd 61 6e 64 20 0a 20 20 20 20 2a 2a 20 72 65 64 75  and .    ** redu
390de 63 65 20 74 68 65 20 74 72 65 65 20 68 65 69 67  ce the tree heig
390df 68 74 20 62 79 20 6f 6e 65 2e 0a 20 20 20 20 2a  ht by one..    *
390e0 2a 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73  *.    ** This is
390e1 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 63   equivalent to c
390e2 6f 70 79 69 6e 67 20 74 68 65 20 63 6f 6e 74 65  opying the conte
390e3 6e 74 73 20 6f 66 20 74 68 65 20 63 68 69 6c 64  nts of the child
390e4 20 69 6e 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65   into.    ** the
390e5 20 72 6f 6f 74 20 6e 6f 64 65 20 28 74 68 65 20   root node (the 
390e6 6f 70 65 72 61 74 69 6f 6e 20 74 68 61 74 20 47  operation that G
390e7 75 74 6d 61 6e 27 73 20 70 61 70 65 72 20 73 61  utman's paper sa
390e8 79 73 20 74 6f 20 70 65 72 66 6f 72 6d 20 0a 20  ys to perform . 
390e9 20 20 20 2a 2a 20 69 6e 20 74 68 69 73 20 73 63     ** in this sc
390ea 65 6e 61 72 69 6f 29 2e 0a 20 20 20 20 2a 2f 0a  enario)..    */.
390eb 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
390ec 54 45 5f 4f 4b 20 26 26 20 70 52 74 72 65 65 2d  TE_OK && pRtree-
390ed 3e 69 44 65 70 74 68 3e 30 20 29 7b 0a 20 20 20  >iDepth>0 ){.   
390ee 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
390ef 45 5f 4f 4b 20 26 26 20 4e 43 45 4c 4c 28 70 52  E_OK && NCELL(pR
390f0 6f 6f 74 29 3d 3d 31 20 29 7b 0a 20 20 20 20 20  oot)==1 ){.     
390f1 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 43     RtreeNode *pC
390f2 68 69 6c 64 3b 0a 20 20 20 20 20 20 20 20 69 36  hild;.        i6
390f3 34 20 69 43 68 69 6c 64 20 3d 20 6e 6f 64 65 47  4 iChild = nodeG
390f4 65 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c 20  etRowid(pRtree, 
390f5 70 52 6f 6f 74 2c 20 30 29 3b 0a 20 20 20 20 20  pRoot, 0);.     
390f6 20 20 20 72 63 20 3d 20 6e 6f 64 65 41 63 71 75     rc = nodeAcqu
390f7 69 72 65 28 70 52 74 72 65 65 2c 20 69 43 68 69  ire(pRtree, iChi
390f8 6c 64 2c 20 70 52 6f 6f 74 2c 20 26 70 43 68 69  ld, pRoot, &pChi
390f9 6c 64 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ld);.        if(
390fa 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
390fb 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  {.          rc =
390fc 20 72 65 6d 6f 76 65 4e 6f 64 65 28 70 52 74 72   removeNode(pRtr
390fd 65 65 2c 20 70 43 68 69 6c 64 2c 20 70 52 74 72  ee, pChild, pRtr
390fe 65 65 2d 3e 69 44 65 70 74 68 2d 31 29 3b 0a 20  ee->iDepth-1);. 
390ff 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
39100 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
39101 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
39102 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2d 2d  pRtree->iDepth--
39103 3b 0a 20 20 20 20 20 20 20 20 20 20 77 72 69 74  ;.          writ
39104 65 49 6e 74 31 36 28 70 52 6f 6f 74 2d 3e 7a 44  eInt16(pRoot->zD
39105 61 74 61 2c 20 70 52 74 72 65 65 2d 3e 69 44 65  ata, pRtree->iDe
39106 70 74 68 29 3b 0a 20 20 20 20 20 20 20 20 20 20  pth);.          
39107 70 52 6f 6f 74 2d 3e 69 73 44 69 72 74 79 20 3d  pRoot->isDirty =
39108 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   1;.        }.  
39109 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
3910a 20 2f 2a 20 52 65 2d 69 6e 73 65 72 74 20 74 68   /* Re-insert th
3910b 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 6e  e contents of an
3910c 79 20 75 6e 64 65 72 66 75 6c 6c 20 6e 6f 64 65  y underfull node
3910d 73 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74  s removed from t
3910e 68 65 20 74 72 65 65 2e 20 2a 2f 0a 20 20 20 20  he tree. */.    
3910f 66 6f 72 28 70 4c 65 61 66 3d 70 52 74 72 65 65  for(pLeaf=pRtree
39110 2d 3e 70 44 65 6c 65 74 65 64 3b 20 70 4c 65 61  ->pDeleted; pLea
39111 66 3b 20 70 4c 65 61 66 3d 70 52 74 72 65 65 2d  f; pLeaf=pRtree-
39112 3e 70 44 65 6c 65 74 65 64 29 7b 0a 20 20 20 20  >pDeleted){.    
39113 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
39114 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  _OK ){.        r
39115 63 20 3d 20 72 65 69 6e 73 65 72 74 4e 6f 64 65  c = reinsertNode
39116 43 6f 6e 74 65 6e 74 28 70 52 74 72 65 65 2c 20  Content(pRtree, 
39117 70 4c 65 61 66 29 3b 0a 20 20 20 20 20 20 7d 0a  pLeaf);.      }.
39118 20 20 20 20 20 20 70 52 74 72 65 65 2d 3e 70 44        pRtree->pD
39119 65 6c 65 74 65 64 20 3d 20 70 4c 65 61 66 2d 3e  eleted = pLeaf->
3911a 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 73 71 6c  pNext;.      sql
3911b 69 74 65 33 5f 66 72 65 65 28 70 4c 65 61 66 29  ite3_free(pLeaf)
3911c 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
3911d 52 65 6c 65 61 73 65 20 74 68 65 20 72 65 66 65  Release the refe
3911e 72 65 6e 63 65 20 74 6f 20 74 68 65 20 72 6f 6f  rence to the roo
3911f 74 20 6e 6f 64 65 2e 20 2a 2f 0a 20 20 20 20 69  t node. */.    i
39120 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
39121 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6e   ){.      rc = n
39122 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  odeRelease(pRtre
39123 65 2c 20 70 52 6f 6f 74 29 3b 0a 20 20 20 20 7d  e, pRoot);.    }
39124 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 6f 64 65  else{.      node
39125 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20  Release(pRtree, 
39126 70 52 6f 6f 74 29 3b 0a 20 20 20 20 7d 0a 20 20  pRoot);.    }.  
39127 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 61  }..  /* If the a
39128 7a 44 61 74 61 5b 5d 20 61 72 72 61 79 20 63 6f  zData[] array co
39129 6e 74 61 69 6e 73 20 6d 6f 72 65 20 74 68 61 6e  ntains more than
3912a 20 6f 6e 65 20 65 6c 65 6d 65 6e 74 2c 20 65 6c   one element, el
3912b 65 6d 65 6e 74 73 0a 20 20 2a 2a 20 28 61 7a 44  ements.  ** (azD
3912c 61 74 61 5b 32 5d 2e 2e 61 7a 44 61 74 61 5b 61  ata[2]..azData[a
3912d 72 67 63 2d 31 5d 29 20 63 6f 6e 74 61 69 6e 20  rgc-1]) contain 
3912e 61 20 6e 65 77 20 72 65 63 6f 72 64 20 74 6f 20  a new record to 
3912f 69 6e 73 65 72 74 20 69 6e 74 6f 0a 20 20 2a 2a  insert into.  **
39130 20 74 68 65 20 72 2d 74 72 65 65 20 73 74 72 75   the r-tree stru
39131 63 74 75 72 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  cture..  */.  if
39132 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
39133 26 26 20 6e 44 61 74 61 3e 31 20 29 7b 0a 20 20  && nData>1 ){.  
39134 20 20 2f 2a 20 49 6e 73 65 72 74 20 61 20 6e 65    /* Insert a ne
39135 77 20 72 65 63 6f 72 64 20 69 6e 74 6f 20 74 68  w record into th
39136 65 20 72 2d 74 72 65 65 20 2a 2f 0a 20 20 20 20  e r-tree */.    
39137 52 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a  RtreeCell cell;.
39138 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20      int ii;.    
39139 52 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65 61 66  RtreeNode *pLeaf
3913a 3b 0a 0a 20 20 20 20 2f 2a 20 50 6f 70 75 6c 61  ;..    /* Popula
3913b 74 65 20 74 68 65 20 63 65 6c 6c 2e 61 43 6f 6f  te the cell.aCoo
3913c 72 64 5b 5d 20 61 72 72 61 79 2e 20 54 68 65 20  rd[] array. The 
3913d 66 69 72 73 74 20 63 6f 6f 72 64 69 6e 61 74 65  first coordinate
3913e 20 69 73 20 61 7a 44 61 74 61 5b 33 5d 2e 20 2a   is azData[3]. *
3913f 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 44  /.    assert( nD
39140 61 74 61 3d 3d 28 70 52 74 72 65 65 2d 3e 6e 44  ata==(pRtree->nD
39141 69 6d 2a 32 20 2b 20 33 29 20 29 3b 0a 20 20 20  im*2 + 3) );.   
39142 20 69 66 28 20 70 52 74 72 65 65 2d 3e 65 43 6f   if( pRtree->eCo
39143 6f 72 64 54 79 70 65 3d 3d 52 54 52 45 45 5f 43  ordType==RTREE_C
39144 4f 4f 52 44 5f 52 45 41 4c 33 32 20 29 7b 0a 20  OORD_REAL32 ){. 
39145 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69       for(ii=0; i
39146 69 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a  i<(pRtree->nDim*
39147 32 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20 20  2); ii+=2){.    
39148 20 20 20 20 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b      cell.aCoord[
39149 69 69 5d 2e 66 20 3d 20 28 66 6c 6f 61 74 29 73  ii].f = (float)s
3914a 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75  qlite3_value_dou
3914b 62 6c 65 28 61 7a 44 61 74 61 5b 69 69 2b 33 5d  ble(azData[ii+3]
3914c 29 3b 0a 20 20 20 20 20 20 20 20 63 65 6c 6c 2e  );.        cell.
3914d 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 66 20 3d  aCoord[ii+1].f =
3914e 20 28 66 6c 6f 61 74 29 73 71 6c 69 74 65 33 5f   (float)sqlite3_
3914f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 7a 44  value_double(azD
39150 61 74 61 5b 69 69 2b 34 5d 29 3b 0a 20 20 20 20  ata[ii+4]);.    
39151 20 20 20 20 69 66 28 20 63 65 6c 6c 2e 61 43 6f      if( cell.aCo
39152 6f 72 64 5b 69 69 5d 2e 66 3e 63 65 6c 6c 2e 61  ord[ii].f>cell.a
39153 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 66 20 29 7b  Coord[ii+1].f ){
39154 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
39155 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e  SQLITE_CONSTRAIN
39156 54 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  T;.          got
39157 6f 20 63 6f 6e 73 74 72 61 69 6e 74 3b 0a 20 20  o constraint;.  
39158 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
39159 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
3915a 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28 70   for(ii=0; ii<(p
3915b 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20  Rtree->nDim*2); 
3915c 69 69 2b 3d 32 29 7b 0a 20 20 20 20 20 20 20 20  ii+=2){.        
3915d 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 5d 2e  cell.aCoord[ii].
3915e 69 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  i = sqlite3_valu
3915f 65 5f 69 6e 74 28 61 7a 44 61 74 61 5b 69 69 2b  e_int(azData[ii+
39160 33 5d 29 3b 0a 20 20 20 20 20 20 20 20 63 65 6c  3]);.        cel
39161 6c 2e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 69  l.aCoord[ii+1].i
39162 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
39163 5f 69 6e 74 28 61 7a 44 61 74 61 5b 69 69 2b 34  _int(azData[ii+4
39164 5d 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ]);.        if( 
39165 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 5d 2e  cell.aCoord[ii].
39166 69 3e 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69  i>cell.aCoord[ii
39167 2b 31 5d 2e 69 20 29 7b 0a 20 20 20 20 20 20 20  +1].i ){.       
39168 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43     rc = SQLITE_C
39169 4f 4e 53 54 52 41 49 4e 54 3b 0a 20 20 20 20 20  ONSTRAINT;.     
3916a 20 20 20 20 20 67 6f 74 6f 20 63 6f 6e 73 74 72       goto constr
3916b 61 69 6e 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  aint;.        }.
3916c 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
3916d 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74     /* Figure out
3916e 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68   the rowid of th
3916f 65 20 6e 65 77 20 72 6f 77 2e 20 2a 2f 0a 20 20  e new row. */.  
39170 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61    if( sqlite3_va
39171 6c 75 65 5f 74 79 70 65 28 61 7a 44 61 74 61 5b  lue_type(azData[
39172 32 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c  2])==SQLITE_NULL
39173 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6e   ){.      rc = n
39174 65 77 52 6f 77 69 64 28 70 52 74 72 65 65 2c 20  ewRowid(pRtree, 
39175 26 63 65 6c 6c 2e 69 52 6f 77 69 64 29 3b 0a 20  &cell.iRowid);. 
39176 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
39177 63 65 6c 6c 2e 69 52 6f 77 69 64 20 3d 20 73 71  cell.iRowid = sq
39178 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36  lite3_value_int6
39179 34 28 61 7a 44 61 74 61 5b 32 5d 29 3b 0a 20 20  4(azData[2]);.  
3917a 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64      sqlite3_bind
3917b 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70  _int64(pRtree->p
3917c 52 65 61 64 52 6f 77 69 64 2c 20 31 2c 20 63 65  ReadRowid, 1, ce
3917d 6c 6c 2e 69 52 6f 77 69 64 29 3b 0a 20 20 20 20  ll.iRowid);.    
3917e 20 20 69 66 28 20 53 51 4c 49 54 45 5f 52 4f 57    if( SQLITE_ROW
3917f 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70  ==sqlite3_step(p
39180 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77 69  Rtree->pReadRowi
39181 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  d) ){.        sq
39182 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52 74 72  lite3_reset(pRtr
39183 65 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 29 3b  ee->pReadRowid);
39184 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
39185 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 3b  LITE_CONSTRAINT;
39186 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 63 6f  .        goto co
39187 6e 73 74 72 61 69 6e 74 3b 0a 20 20 20 20 20 20  nstraint;.      
39188 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  }.      rc = sql
39189 69 74 65 33 5f 72 65 73 65 74 28 70 52 74 72 65  ite3_reset(pRtre
3918a 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 29 3b 0a  e->pReadRowid);.
3918b 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72      }..    if( r
3918c 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
3918d 20 20 20 20 20 20 72 63 20 3d 20 43 68 6f 6f 73        rc = Choos
3918e 65 4c 65 61 66 28 70 52 74 72 65 65 2c 20 26 63  eLeaf(pRtree, &c
3918f 65 6c 6c 2c 20 30 2c 20 26 70 4c 65 61 66 29 3b  ell, 0, &pLeaf);
39190 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72  .    }.    if( r
39191 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
39192 20 20 20 20 20 20 69 6e 74 20 72 63 32 3b 0a 20        int rc2;. 
39193 20 20 20 20 20 70 52 74 72 65 65 2d 3e 69 52 65       pRtree->iRe
39194 69 6e 73 65 72 74 48 65 69 67 68 74 20 3d 20 2d  insertHeight = -
39195 31 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 72 74  1;.      rc = rt
39196 72 65 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52  reeInsertCell(pR
39197 74 72 65 65 2c 20 70 4c 65 61 66 2c 20 26 63 65  tree, pLeaf, &ce
39198 6c 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 63  ll, 0);.      rc
39199 32 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28  2 = nodeRelease(
3919a 70 52 74 72 65 65 2c 20 70 4c 65 61 66 29 3b 0a  pRtree, pLeaf);.
3919b 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
3919c 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
3919d 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 20     rc = rc2;.   
3919e 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
3919f 63 6f 6e 73 74 72 61 69 6e 74 3a 0a 20 20 72 74  constraint:.  rt
391a0 72 65 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  reeRelease(pRtre
391a1 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  e);.  return rc;
391a2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 78 52  .}../*.** The xR
391a3 65 6e 61 6d 65 20 6d 65 74 68 6f 64 20 66 6f 72  ename method for
391a4 20 72 74 72 65 65 20 6d 6f 64 75 6c 65 20 76 69   rtree module vi
391a5 72 74 75 61 6c 20 74 61 62 6c 65 73 2e 0a 2a 2f  rtual tables..*/
391a6 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65  .static int rtre
391a7 65 52 65 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f  eRename(sqlite3_
391a8 76 74 61 62 20 2a 70 56 74 61 62 2c 20 63 6f 6e  vtab *pVtab, con
391a9 73 74 20 63 68 61 72 20 2a 7a 4e 65 77 4e 61 6d  st char *zNewNam
391aa 65 29 7b 0a 20 20 52 74 72 65 65 20 2a 70 52 74  e){.  Rtree *pRt
391ab 72 65 65 20 3d 20 28 52 74 72 65 65 20 2a 29 70  ree = (Rtree *)p
391ac 56 74 61 62 3b 0a 20 20 69 6e 74 20 72 63 20 3d  Vtab;.  int rc =
391ad 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
391ae 20 63 68 61 72 20 2a 7a 53 71 6c 20 3d 20 73 71   char *zSql = sq
391af 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 0a 20  lite3_mprintf(. 
391b0 20 20 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20     "ALTER TABLE 
391b1 25 51 2e 27 25 71 5f 6e 6f 64 65 27 20 20 20 52  %Q.'%q_node'   R
391b2 45 4e 41 4d 45 20 54 4f 20 5c 22 25 77 5f 6e 6f  ENAME TO \"%w_no
391b3 64 65 5c 22 3b 22 0a 20 20 20 20 22 41 4c 54 45  de\";".    "ALTE
391b4 52 20 54 41 42 4c 45 20 25 51 2e 27 25 71 5f 70  R TABLE %Q.'%q_p
391b5 61 72 65 6e 74 27 20 52 45 4e 41 4d 45 20 54 4f  arent' RENAME TO
391b6 20 5c 22 25 77 5f 70 61 72 65 6e 74 5c 22 3b 22   \"%w_parent\";"
391b7 0a 20 20 20 20 22 41 4c 54 45 52 20 54 41 42 4c  .    "ALTER TABL
391b8 45 20 25 51 2e 27 25 71 5f 72 6f 77 69 64 27 20  E %Q.'%q_rowid' 
391b9 20 52 45 4e 41 4d 45 20 54 4f 20 5c 22 25 77 5f   RENAME TO \"%w_
391ba 72 6f 77 69 64 5c 22 3b 22 0a 20 20 20 20 2c 20  rowid\";".    , 
391bb 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20 70 52 74  pRtree->zDb, pRt
391bc 72 65 65 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 65 77  ree->zName, zNew
391bd 4e 61 6d 65 20 0a 20 20 20 20 2c 20 70 52 74 72  Name .    , pRtr
391be 65 65 2d 3e 7a 44 62 2c 20 70 52 74 72 65 65 2d  ee->zDb, pRtree-
391bf 3e 7a 4e 61 6d 65 2c 20 7a 4e 65 77 4e 61 6d 65  >zName, zNewName
391c0 20 0a 20 20 20 20 2c 20 70 52 74 72 65 65 2d 3e   .    , pRtree->
391c1 7a 44 62 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61  zDb, pRtree->zNa
391c2 6d 65 2c 20 7a 4e 65 77 4e 61 6d 65 0a 20 20 29  me, zNewName.  )
391c3 3b 0a 20 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a  ;.  if( zSql ){.
391c4 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
391c5 5f 65 78 65 63 28 70 52 74 72 65 65 2d 3e 64 62  _exec(pRtree->db
391c6 2c 20 7a 53 71 6c 2c 20 30 2c 20 30 2c 20 30 29  , zSql, 0, 0, 0)
391c7 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
391c8 65 65 28 7a 53 71 6c 29 3b 0a 20 20 7d 0a 20 20  ee(zSql);.  }.  
391c9 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74  return rc;.}..st
391ca 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 6f 64  atic sqlite3_mod
391cb 75 6c 65 20 72 74 72 65 65 4d 6f 64 75 6c 65 20  ule rtreeModule 
391cc 3d 20 7b 0a 20 20 30 2c 20 20 20 20 20 20 20 20  = {.  0,        
391cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
391ce 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a   /* iVersion */.
391cf 20 20 72 74 72 65 65 43 72 65 61 74 65 2c 20 20    rtreeCreate,  
391d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
391d1 20 78 43 72 65 61 74 65 20 2d 20 63 72 65 61 74   xCreate - creat
391d2 65 20 61 20 74 61 62 6c 65 20 2a 2f 0a 20 20 72  e a table */.  r
391d3 74 72 65 65 43 6f 6e 6e 65 63 74 2c 20 20 20 20  treeConnect,    
391d4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43             /* xC
391d5 6f 6e 6e 65 63 74 20 2d 20 63 6f 6e 6e 65 63 74  onnect - connect
391d6 20 74 6f 20 61 6e 20 65 78 69 73 74 69 6e 67 20   to an existing 
391d7 74 61 62 6c 65 20 2a 2f 0a 20 20 72 74 72 65 65  table */.  rtree
391d8 42 65 73 74 49 6e 64 65 78 2c 20 20 20 20 20 20  BestIndex,      
391d9 20 20 20 20 20 20 20 2f 2a 20 78 42 65 73 74 49         /* xBestI
391da 6e 64 65 78 20 2d 20 44 65 74 65 72 6d 69 6e 65  ndex - Determine
391db 20 73 65 61 72 63 68 20 73 74 72 61 74 65 67 79   search strategy
391dc 20 2a 2f 0a 20 20 72 74 72 65 65 44 69 73 63 6f   */.  rtreeDisco
391dd 6e 6e 65 63 74 2c 20 20 20 20 20 20 20 20 20 20  nnect,          
391de 20 20 2f 2a 20 78 44 69 73 63 6f 6e 6e 65 63 74    /* xDisconnect
391df 20 2d 20 44 69 73 63 6f 6e 6e 65 63 74 20 66 72   - Disconnect fr
391e0 6f 6d 20 61 20 74 61 62 6c 65 20 2a 2f 0a 20 20  om a table */.  
391e1 72 74 72 65 65 44 65 73 74 72 6f 79 2c 20 20 20  rtreeDestroy,   
391e2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
391e3 44 65 73 74 72 6f 79 20 2d 20 44 72 6f 70 20 61  Destroy - Drop a
391e4 20 74 61 62 6c 65 20 2a 2f 0a 20 20 72 74 72 65   table */.  rtre
391e5 65 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20  eOpen,          
391e6 20 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e          /* xOpen
391e7 20 2d 20 6f 70 65 6e 20 61 20 63 75 72 73 6f 72   - open a cursor
391e8 20 2a 2f 0a 20 20 72 74 72 65 65 43 6c 6f 73 65   */.  rtreeClose
391e9 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
391ea 20 20 2f 2a 20 78 43 6c 6f 73 65 20 2d 20 63 6c    /* xClose - cl
391eb 6f 73 65 20 61 20 63 75 72 73 6f 72 20 2a 2f 0a  ose a cursor */.
391ec 20 20 72 74 72 65 65 46 69 6c 74 65 72 2c 20 20    rtreeFilter,  
391ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
391ee 20 78 46 69 6c 74 65 72 20 2d 20 63 6f 6e 66 69   xFilter - confi
391ef 67 75 72 65 20 73 63 61 6e 20 63 6f 6e 73 74 72  gure scan constr
391f0 61 69 6e 74 73 20 2a 2f 0a 20 20 72 74 72 65 65  aints */.  rtree
391f1 4e 65 78 74 2c 20 20 20 20 20 20 20 20 20 20 20  Next,           
391f2 20 20 20 20 20 20 20 2f 2a 20 78 4e 65 78 74 20         /* xNext 
391f3 2d 20 61 64 76 61 6e 63 65 20 61 20 63 75 72 73  - advance a curs
391f4 6f 72 20 2a 2f 0a 20 20 72 74 72 65 65 45 6f 66  or */.  rtreeEof
391f5 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
391f6 20 20 20 20 2f 2a 20 78 45 6f 66 20 2a 2f 0a 20      /* xEof */. 
391f7 20 72 74 72 65 65 43 6f 6c 75 6d 6e 2c 20 20 20   rtreeColumn,   
391f8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
391f9 78 43 6f 6c 75 6d 6e 20 2d 20 72 65 61 64 20 64  xColumn - read d
391fa 61 74 61 20 2a 2f 0a 20 20 72 74 72 65 65 52 6f  ata */.  rtreeRo
391fb 77 69 64 2c 20 20 20 20 20 20 20 20 20 20 20 20  wid,            
391fc 20 20 20 20 20 2f 2a 20 78 52 6f 77 69 64 20 2d       /* xRowid -
391fd 20 72 65 61 64 20 64 61 74 61 20 2a 2f 0a 20 20   read data */.  
391fe 72 74 72 65 65 55 70 64 61 74 65 2c 20 20 20 20  rtreeUpdate,    
391ff 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
39200 55 70 64 61 74 65 20 2d 20 77 72 69 74 65 20 64  Update - write d
39201 61 74 61 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20  ata */.  0,     
39202 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39203 20 20 20 20 20 2f 2a 20 78 42 65 67 69 6e 20 2d       /* xBegin -
39204 20 62 65 67 69 6e 20 74 72 61 6e 73 61 63 74 69   begin transacti
39205 6f 6e 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20  on */.  0,      
39206 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39207 20 20 20 20 2f 2a 20 78 53 79 6e 63 20 2d 20 73      /* xSync - s
39208 79 6e 63 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ync transaction 
39209 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20  */.  0,         
3920a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3920b 20 2f 2a 20 78 43 6f 6d 6d 69 74 20 2d 20 63 6f   /* xCommit - co
3920c 6d 6d 69 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  mmit transaction
3920d 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20   */.  0,        
3920e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3920f 20 20 2f 2a 20 78 52 6f 6c 6c 62 61 63 6b 20 2d    /* xRollback -
39210 20 72 6f 6c 6c 62 61 63 6b 20 74 72 61 6e 73 61   rollback transa
39211 63 74 69 6f 6e 20 2a 2f 0a 20 20 30 2c 20 20 20  ction */.  0,   
39212 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39213 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6e 64 46         /* xFindF
39214 75 6e 63 74 69 6f 6e 20 2d 20 66 75 6e 63 74 69  unction - functi
39215 6f 6e 20 6f 76 65 72 6c 6f 61 64 69 6e 67 20 2a  on overloading *
39216 2f 0a 20 20 72 74 72 65 65 52 65 6e 61 6d 65 20  /.  rtreeRename 
39217 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39218 2f 2a 20 78 52 65 6e 61 6d 65 20 2d 20 72 65 6e  /* xRename - ren
39219 61 6d 65 20 74 68 65 20 74 61 62 6c 65 20 2a 2f  ame the table */
3921a 0a 7d 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  .};..static int 
3921b 72 74 72 65 65 53 71 6c 49 6e 69 74 28 0a 20 20  rtreeSqlInit(.  
3921c 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a  Rtree *pRtree, .
3921d 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a    sqlite3 *db, .
3921e 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
3921f 62 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  b, .  const char
39220 20 2a 7a 50 72 65 66 69 78 2c 20 0a 20 20 69 6e   *zPrefix, .  in
39221 74 20 69 73 43 72 65 61 74 65 0a 29 7b 0a 20 20  t isCreate.){.  
39222 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
39223 4f 4b 3b 0a 0a 20 20 23 64 65 66 69 6e 65 20 4e  OK;..  #define N
39224 5f 53 54 41 54 45 4d 45 4e 54 20 39 0a 20 20 73  _STATEMENT 9.  s
39225 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
39226 20 2a 61 7a 53 71 6c 5b 4e 5f 53 54 41 54 45 4d   *azSql[N_STATEM
39227 45 4e 54 5d 20 3d 20 7b 0a 20 20 20 20 2f 2a 20  ENT] = {.    /* 
39228 52 65 61 64 20 61 6e 64 20 77 72 69 74 65 20 74  Read and write t
39229 68 65 20 78 78 78 5f 6e 6f 64 65 20 74 61 62 6c  he xxx_node tabl
3922a 65 20 2a 2f 0a 20 20 20 20 22 53 45 4c 45 43 54  e */.    "SELECT
3922b 20 64 61 74 61 20 46 52 4f 4d 20 27 25 71 27 2e   data FROM '%q'.
3922c 27 25 71 5f 6e 6f 64 65 27 20 57 48 45 52 45 20  '%q_node' WHERE 
3922d 6e 6f 64 65 6e 6f 20 3d 20 3a 31 22 2c 0a 20 20  nodeno = :1",.  
3922e 20 20 22 49 4e 53 45 52 54 20 4f 52 20 52 45 50    "INSERT OR REP
3922f 4c 41 43 45 20 49 4e 54 4f 20 27 25 71 27 2e 27  LACE INTO '%q'.'
39230 25 71 5f 6e 6f 64 65 27 20 56 41 4c 55 45 53 28  %q_node' VALUES(
39231 3a 31 2c 20 3a 32 29 22 2c 0a 20 20 20 20 22 44  :1, :2)",.    "D
39232 45 4c 45 54 45 20 46 52 4f 4d 20 27 25 71 27 2e  ELETE FROM '%q'.
39233 27 25 71 5f 6e 6f 64 65 27 20 57 48 45 52 45 20  '%q_node' WHERE 
39234 6e 6f 64 65 6e 6f 20 3d 20 3a 31 22 2c 0a 0a 20  nodeno = :1",.. 
39235 20 20 20 2f 2a 20 52 65 61 64 20 61 6e 64 20 77     /* Read and w
39236 72 69 74 65 20 74 68 65 20 78 78 78 5f 72 6f 77  rite the xxx_row
39237 69 64 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  id table */.    
39238 22 53 45 4c 45 43 54 20 6e 6f 64 65 6e 6f 20 46  "SELECT nodeno F
39239 52 4f 4d 20 27 25 71 27 2e 27 25 71 5f 72 6f 77  ROM '%q'.'%q_row
3923a 69 64 27 20 57 48 45 52 45 20 72 6f 77 69 64 20  id' WHERE rowid 
3923b 3d 20 3a 31 22 2c 0a 20 20 20 20 22 49 4e 53 45  = :1",.    "INSE
3923c 52 54 20 4f 52 20 52 45 50 4c 41 43 45 20 49 4e  RT OR REPLACE IN
3923d 54 4f 20 27 25 71 27 2e 27 25 71 5f 72 6f 77 69  TO '%q'.'%q_rowi
3923e 64 27 20 56 41 4c 55 45 53 28 3a 31 2c 20 3a 32  d' VALUES(:1, :2
3923f 29 22 2c 0a 20 20 20 20 22 44 45 4c 45 54 45 20  )",.    "DELETE 
39240 46 52 4f 4d 20 27 25 71 27 2e 27 25 71 5f 72 6f  FROM '%q'.'%q_ro
39241 77 69 64 27 20 57 48 45 52 45 20 72 6f 77 69 64  wid' WHERE rowid
39242 20 3d 20 3a 31 22 2c 0a 0a 20 20 20 20 2f 2a 20   = :1",..    /* 
39243 52 65 61 64 20 61 6e 64 20 77 72 69 74 65 20 74  Read and write t
39244 68 65 20 78 78 78 5f 70 61 72 65 6e 74 20 74 61  he xxx_parent ta
39245 62 6c 65 20 2a 2f 0a 20 20 20 20 22 53 45 4c 45  ble */.    "SELE
39246 43 54 20 70 61 72 65 6e 74 6e 6f 64 65 20 46 52  CT parentnode FR
39247 4f 4d 20 27 25 71 27 2e 27 25 71 5f 70 61 72 65  OM '%q'.'%q_pare
39248 6e 74 27 20 57 48 45 52 45 20 6e 6f 64 65 6e 6f  nt' WHERE nodeno
39249 20 3d 20 3a 31 22 2c 0a 20 20 20 20 22 49 4e 53   = :1",.    "INS
3924a 45 52 54 20 4f 52 20 52 45 50 4c 41 43 45 20 49  ERT OR REPLACE I
3924b 4e 54 4f 20 27 25 71 27 2e 27 25 71 5f 70 61 72  NTO '%q'.'%q_par
3924c 65 6e 74 27 20 56 41 4c 55 45 53 28 3a 31 2c 20  ent' VALUES(:1, 
3924d 3a 32 29 22 2c 0a 20 20 20 20 22 44 45 4c 45 54  :2)",.    "DELET
3924e 45 20 46 52 4f 4d 20 27 25 71 27 2e 27 25 71 5f  E FROM '%q'.'%q_
3924f 70 61 72 65 6e 74 27 20 57 48 45 52 45 20 6e 6f  parent' WHERE no
39250 64 65 6e 6f 20 3d 20 3a 31 22 0a 20 20 7d 3b 0a  deno = :1".  };.
39251 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
39252 2a 61 70 70 53 74 6d 74 5b 4e 5f 53 54 41 54 45  *appStmt[N_STATE
39253 4d 45 4e 54 5d 3b 0a 20 20 69 6e 74 20 69 3b 0a  MENT];.  int i;.
39254 0a 20 20 70 52 74 72 65 65 2d 3e 64 62 20 3d 20  .  pRtree->db = 
39255 64 62 3b 0a 0a 20 20 69 66 28 20 69 73 43 72 65  db;..  if( isCre
39256 61 74 65 20 29 7b 0a 20 20 20 20 63 68 61 72 20  ate ){.    char 
39257 2a 7a 43 72 65 61 74 65 20 3d 20 73 71 6c 69 74  *zCreate = sqlit
39258 65 33 5f 6d 70 72 69 6e 74 66 28 0a 22 43 52 45  e3_mprintf(."CRE
39259 41 54 45 20 54 41 42 4c 45 20 5c 22 25 77 5c 22  ATE TABLE \"%w\"
3925a 2e 5c 22 25 77 5f 6e 6f 64 65 5c 22 28 6e 6f 64  .\"%w_node\"(nod
3925b 65 6e 6f 20 49 4e 54 45 47 45 52 20 50 52 49 4d  eno INTEGER PRIM
3925c 41 52 59 20 4b 45 59 2c 20 64 61 74 61 20 42 4c  ARY KEY, data BL
3925d 4f 42 29 3b 22 0a 22 43 52 45 41 54 45 20 54 41  OB);"."CREATE TA
3925e 42 4c 45 20 5c 22 25 77 5c 22 2e 5c 22 25 77 5f  BLE \"%w\".\"%w_
3925f 72 6f 77 69 64 5c 22 28 72 6f 77 69 64 20 49 4e  rowid\"(rowid IN
39260 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
39261 59 2c 20 6e 6f 64 65 6e 6f 20 49 4e 54 45 47 45  Y, nodeno INTEGE
39262 52 29 3b 22 0a 22 43 52 45 41 54 45 20 54 41 42  R);"."CREATE TAB
39263 4c 45 20 5c 22 25 77 5c 22 2e 5c 22 25 77 5f 70  LE \"%w\".\"%w_p
39264 61 72 65 6e 74 5c 22 28 6e 6f 64 65 6e 6f 20 49  arent\"(nodeno I
39265 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
39266 45 59 2c 20 70 61 72 65 6e 74 6e 6f 64 65 20 49  EY, parentnode I
39267 4e 54 45 47 45 52 29 3b 22 0a 22 49 4e 53 45 52  NTEGER);"."INSER
39268 54 20 49 4e 54 4f 20 27 25 71 27 2e 27 25 71 5f  T INTO '%q'.'%q_
39269 6e 6f 64 65 27 20 56 41 4c 55 45 53 28 31 2c 20  node' VALUES(1, 
3926a 7a 65 72 6f 62 6c 6f 62 28 25 64 29 29 22 2c 0a  zeroblob(%d))",.
3926b 20 20 20 20 20 20 7a 44 62 2c 20 7a 50 72 65 66        zDb, zPref
3926c 69 78 2c 20 7a 44 62 2c 20 7a 50 72 65 66 69 78  ix, zDb, zPrefix
3926d 2c 20 7a 44 62 2c 20 7a 50 72 65 66 69 78 2c 20  , zDb, zPrefix, 
3926e 7a 44 62 2c 20 7a 50 72 65 66 69 78 2c 20 70 52  zDb, zPrefix, pR
3926f 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 0a  tree->iNodeSize.
39270 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20 21      );.    if( !
39271 7a 43 72 65 61 74 65 20 29 7b 0a 20 20 20 20 20  zCreate ){.     
39272 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
39273 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  OMEM;.    }.    
39274 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65  rc = sqlite3_exe
39275 63 28 64 62 2c 20 7a 43 72 65 61 74 65 2c 20 30  c(db, zCreate, 0
39276 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  , 0, 0);.    sql
39277 69 74 65 33 5f 66 72 65 65 28 7a 43 72 65 61 74  ite3_free(zCreat
39278 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  e);.    if( rc!=
39279 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
3927a 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
3927b 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 70 70 53 74    }.  }..  appSt
3927c 6d 74 5b 30 5d 20 3d 20 26 70 52 74 72 65 65 2d  mt[0] = &pRtree-
3927d 3e 70 52 65 61 64 4e 6f 64 65 3b 0a 20 20 61 70  >pReadNode;.  ap
3927e 70 53 74 6d 74 5b 31 5d 20 3d 20 26 70 52 74 72  pStmt[1] = &pRtr
3927f 65 65 2d 3e 70 57 72 69 74 65 4e 6f 64 65 3b 0a  ee->pWriteNode;.
39280 20 20 61 70 70 53 74 6d 74 5b 32 5d 20 3d 20 26    appStmt[2] = &
39281 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 4e  pRtree->pDeleteN
39282 6f 64 65 3b 0a 20 20 61 70 70 53 74 6d 74 5b 33  ode;.  appStmt[3
39283 5d 20 3d 20 26 70 52 74 72 65 65 2d 3e 70 52 65  ] = &pRtree->pRe
39284 61 64 52 6f 77 69 64 3b 0a 20 20 61 70 70 53 74  adRowid;.  appSt
39285 6d 74 5b 34 5d 20 3d 20 26 70 52 74 72 65 65 2d  mt[4] = &pRtree-
39286 3e 70 57 72 69 74 65 52 6f 77 69 64 3b 0a 20 20  >pWriteRowid;.  
39287 61 70 70 53 74 6d 74 5b 35 5d 20 3d 20 26 70 52  appStmt[5] = &pR
39288 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 52 6f 77  tree->pDeleteRow
39289 69 64 3b 0a 20 20 61 70 70 53 74 6d 74 5b 36 5d  id;.  appStmt[6]
3928a 20 3d 20 26 70 52 74 72 65 65 2d 3e 70 52 65 61   = &pRtree->pRea
3928b 64 50 61 72 65 6e 74 3b 0a 20 20 61 70 70 53 74  dParent;.  appSt
3928c 6d 74 5b 37 5d 20 3d 20 26 70 52 74 72 65 65 2d  mt[7] = &pRtree-
3928d 3e 70 57 72 69 74 65 50 61 72 65 6e 74 3b 0a 20  >pWriteParent;. 
3928e 20 61 70 70 53 74 6d 74 5b 38 5d 20 3d 20 26 70   appStmt[8] = &p
3928f 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 50 61  Rtree->pDeletePa
39290 72 65 6e 74 3b 0a 0a 20 20 66 6f 72 28 69 3d 30  rent;..  for(i=0
39291 3b 20 69 3c 4e 5f 53 54 41 54 45 4d 45 4e 54 20  ; i<N_STATEMENT 
39292 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  && rc==SQLITE_OK
39293 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72  ; i++){.    char
39294 20 2a 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33   *zSql = sqlite3
39295 5f 6d 70 72 69 6e 74 66 28 61 7a 53 71 6c 5b 69  _mprintf(azSql[i
39296 5d 2c 20 7a 44 62 2c 20 7a 50 72 65 66 69 78 29  ], zDb, zPrefix)
39297 3b 0a 20 20 20 20 69 66 28 20 7a 53 71 6c 20 29  ;.    if( zSql )
39298 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
39299 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
3929a 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 61 70  db, zSql, -1, ap
3929b 70 53 74 6d 74 5b 69 5d 2c 20 30 29 3b 20 0a 20  pStmt[i], 0); . 
3929c 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
3929d 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
3929e 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  M;.    }.    sql
3929f 69 74 65 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b  ite3_free(zSql);
392a0 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
392a1 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  c;.}../*.** This
392a2 20 72 6f 75 74 69 6e 65 20 71 75 65 72 69 65 73   routine queries
392a3 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
392a4 20 64 62 20 66 6f 72 20 74 68 65 20 70 61 67 65   db for the page
392a5 2d 73 69 7a 65 20 75 73 65 64 20 62 79 0a 2a 2a  -size used by.**
392a6 20 64 61 74 61 62 61 73 65 20 7a 44 62 2e 20 49   database zDb. I
392a7 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74 68  f successful, th
392a8 65 20 70 61 67 65 2d 73 69 7a 65 20 69 6e 20 62  e page-size in b
392a9 79 74 65 73 20 69 73 20 77 72 69 74 74 65 6e 20  ytes is written 
392aa 74 6f 0a 2a 2a 20 2a 70 69 50 61 67 65 53 69 7a  to.** *piPageSiz
392ab 65 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20  e and SQLITE_OK 
392ac 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77  returned. Otherw
392ad 69 73 65 2c 20 61 6e 64 20 61 6e 20 53 51 4c 69  ise, and an SQLi
392ae 74 65 20 65 72 72 6f 72 20 0a 2a 2a 20 63 6f 64  te error .** cod
392af 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  e is returned..*
392b0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74  /.static int get
392b1 50 61 67 65 53 69 7a 65 28 73 71 6c 69 74 65 33  PageSize(sqlite3
392b2 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72   *db, const char
392b3 20 2a 7a 44 62 2c 20 69 6e 74 20 2a 70 69 50 61   *zDb, int *piPa
392b4 67 65 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72  geSize){.  int r
392b5 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
392b6 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a  ;.  char *zSql;.
392b7 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
392b8 70 53 74 6d 74 20 3d 20 30 3b 0a 0a 20 20 7a 53  pStmt = 0;..  zS
392b9 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  ql = sqlite3_mpr
392ba 69 6e 74 66 28 22 50 52 41 47 4d 41 20 25 51 2e  intf("PRAGMA %Q.
392bb 70 61 67 65 5f 73 69 7a 65 22 2c 20 7a 44 62 29  page_size", zDb)
392bc 3b 0a 20 20 69 66 28 20 21 7a 53 71 6c 20 29 7b  ;.  if( !zSql ){
392bd 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
392be 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20  TE_NOMEM;.  }.. 
392bf 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72   rc = sqlite3_pr
392c0 65 70 61 72 65 5f 76 32 28 64 62 2c 20 7a 53 71  epare_v2(db, zSq
392c1 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20 30  l, -1, &pStmt, 0
392c2 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
392c3 65 28 7a 53 71 6c 29 3b 0a 20 20 69 66 28 20 72  e(zSql);.  if( r
392c4 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
392c5 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
392c6 20 7d 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45   }..  if( SQLITE
392c7 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74  _ROW==sqlite3_st
392c8 65 70 28 70 53 74 6d 74 29 20 29 7b 0a 20 20 20  ep(pStmt) ){.   
392c9 20 2a 70 69 50 61 67 65 53 69 7a 65 20 3d 20 73   *piPageSize = s
392ca 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
392cb 74 28 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 7d  t(pStmt, 0);.  }
392cc 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
392cd 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74  3_finalize(pStmt
392ce 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69  );.}../* .** Thi
392cf 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 68  s function is th
392d0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
392d1 20 6f 66 20 62 6f 74 68 20 74 68 65 20 78 43 6f   of both the xCo
392d2 6e 6e 65 63 74 20 61 6e 64 20 78 43 72 65 61 74  nnect and xCreat
392d3 65 0a 2a 2a 20 6d 65 74 68 6f 64 73 20 6f 66 20  e.** methods of 
392d4 74 68 65 20 72 2d 74 72 65 65 20 76 69 72 74 75  the r-tree virtu
392d5 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  al table..**.** 
392d6 20 20 61 72 67 76 5b 30 5d 20 20 20 2d 3e 20 6d    argv[0]   -> m
392d7 6f 64 75 6c 65 20 6e 61 6d 65 0a 2a 2a 20 20 20  odule name.**   
392d8 61 72 67 76 5b 31 5d 20 20 20 2d 3e 20 64 61 74  argv[1]   -> dat
392d9 61 62 61 73 65 20 6e 61 6d 65 0a 2a 2a 20 20 20  abase name.**   
392da 61 72 67 76 5b 32 5d 20 20 20 2d 3e 20 74 61 62  argv[2]   -> tab
392db 6c 65 20 6e 61 6d 65 0a 2a 2a 20 20 20 61 72 67  le name.**   arg
392dc 76 5b 2e 2e 2e 5d 20 2d 3e 20 63 6f 6c 75 6d 6e  v[...] -> column
392dd 20 6e 61 6d 65 73 2e 2e 2e 0a 2a 2f 0a 73 74 61   names....*/.sta
392de 74 69 63 20 69 6e 74 20 72 74 72 65 65 49 6e 69  tic int rtreeIni
392df 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  t(.  sqlite3 *db
392e0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
392e1 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
392e2 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
392e3 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 2c  */.  void *pAux,
392e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
392e5 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
392e6 6f 66 20 74 68 65 20 52 54 52 45 45 5f 43 4f 4f  of the RTREE_COO
392e7 52 44 5f 2a 20 63 6f 6e 73 74 61 6e 74 73 20 2a  RD_* constants *
392e8 2f 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f  /.  int argc, co
392e9 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a  nst char *const*
392ea 61 72 67 76 2c 20 20 20 2f 2a 20 50 61 72 61 6d  argv,   /* Param
392eb 65 74 65 72 73 20 74 6f 20 43 52 45 41 54 45 20  eters to CREATE 
392ec 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20  TABLE statement 
392ed 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61  */.  sqlite3_vta
392ee 62 20 2a 2a 70 70 56 74 61 62 2c 20 20 20 20 20  b **ppVtab,     
392ef 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
392f0 20 4e 65 77 20 76 69 72 74 75 61 6c 20 74 61 62   New virtual tab
392f1 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70  le */.  char **p
392f2 7a 45 72 72 2c 20 20 20 20 20 20 20 20 20 20 20  zErr,           
392f3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
392f4 55 54 3a 20 45 72 72 6f 72 20 6d 65 73 73 61 67  UT: Error messag
392f5 65 2c 20 69 66 20 61 6e 79 20 2a 2f 0a 20 20 69  e, if any */.  i
392f6 6e 74 20 69 73 43 72 65 61 74 65 20 20 20 20 20  nt isCreate     
392f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
392f8 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 78     /* True for x
392f9 43 72 65 61 74 65 2c 20 66 61 6c 73 65 20 66 6f  Create, false fo
392fa 72 20 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 29 7b  r xConnect */.){
392fb 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
392fc 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 50 61  TE_OK;.  int iPa
392fd 67 65 53 69 7a 65 20 3d 20 30 3b 0a 20 20 52 74  geSize = 0;.  Rt
392fe 72 65 65 20 2a 70 52 74 72 65 65 3b 0a 20 20 69  ree *pRtree;.  i
392ff 6e 74 20 6e 44 62 3b 20 20 20 20 20 20 20 20 20  nt nDb;         
39300 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f       /* Length o
39301 66 20 73 74 72 69 6e 67 20 61 72 67 76 5b 31 5d  f string argv[1]
39302 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b   */.  int nName;
39303 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
39304 65 6e 67 74 68 20 6f 66 20 73 74 72 69 6e 67 20  ength of string 
39305 61 72 67 76 5b 32 5d 20 2a 2f 0a 20 20 69 6e 74  argv[2] */.  int
39306 20 65 43 6f 6f 72 64 54 79 70 65 20 3d 20 28 69   eCoordType = (i
39307 6e 74 29 70 41 75 78 3b 0a 0a 20 20 63 6f 6e 73  nt)pAux;..  cons
39308 74 20 63 68 61 72 20 2a 61 45 72 72 4d 73 67 5b  t char *aErrMsg[
39309 5d 20 3d 20 7b 0a 20 20 20 20 30 2c 20 20 20 20  ] = {.    0,    
3930a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3930b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3930c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3930d 2f 2a 20 30 20 2a 2f 0a 20 20 20 20 22 57 72 6f  /* 0 */.    "Wro
3930e 6e 67 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  ng number of col
3930f 75 6d 6e 73 20 66 6f 72 20 61 6e 20 72 74 72 65  umns for an rtre
39310 65 20 74 61 62 6c 65 22 2c 20 20 20 20 20 20 20  e table",       
39311 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20 22 54    /* 1 */.    "T
39312 6f 6f 20 66 65 77 20 63 6f 6c 75 6d 6e 73 20 66  oo few columns f
39313 6f 72 20 61 6e 20 72 74 72 65 65 20 74 61 62 6c  or an rtree tabl
39314 65 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  e",             
39315 20 20 20 20 2f 2a 20 32 20 2a 2f 0a 20 20 20 20      /* 2 */.    
39316 22 54 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e  "Too many column
39317 73 20 66 6f 72 20 61 6e 20 72 74 72 65 65 20 74  s for an rtree t
39318 61 62 6c 65 22 20 20 20 20 20 20 20 20 20 20 20  able"           
39319 20 20 20 20 20 20 2f 2a 20 33 20 2a 2f 0a 20 20        /* 3 */.  
3931a 7d 3b 0a 0a 20 20 69 6e 74 20 69 45 72 72 20 3d  };..  int iErr =
3931b 20 28 61 72 67 63 3c 36 29 20 3f 20 32 20 3a 20   (argc<6) ? 2 : 
3931c 61 72 67 63 3e 28 52 54 52 45 45 5f 4d 41 58 5f  argc>(RTREE_MAX_
3931d 44 49 4d 45 4e 53 49 4f 4e 53 2a 32 2b 34 29 20  DIMENSIONS*2+4) 
3931e 3f 20 33 20 3a 20 61 72 67 63 25 32 3b 0a 20 20  ? 3 : argc%2;.  
3931f 69 66 28 20 61 45 72 72 4d 73 67 5b 69 45 72 72  if( aErrMsg[iErr
39320 5d 20 29 7b 0a 20 20 20 20 2a 70 7a 45 72 72 20  ] ){.    *pzErr 
39321 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
39322 66 28 22 25 73 22 2c 20 61 45 72 72 4d 73 67 5b  f("%s", aErrMsg[
39323 69 45 72 72 5d 29 3b 0a 20 20 20 20 72 65 74 75  iErr]);.    retu
39324 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
39325 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 67 65 74  .  }..  rc = get
39326 50 61 67 65 53 69 7a 65 28 64 62 2c 20 61 72 67  PageSize(db, arg
39327 76 5b 31 5d 2c 20 26 69 50 61 67 65 53 69 7a 65  v[1], &iPageSize
39328 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
39329 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65  ITE_OK ){.    re
3932a 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
3932b 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20  /* Allocate the 
3932c 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72  sqlite3_vtab str
3932d 75 63 74 75 72 65 20 2a 2f 0a 20 20 6e 44 62 20  ucture */.  nDb 
3932e 3d 20 73 74 72 6c 65 6e 28 61 72 67 76 5b 31 5d  = strlen(argv[1]
3932f 29 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 74 72  );.  nName = str
39330 6c 65 6e 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20  len(argv[2]);.  
39331 70 52 74 72 65 65 20 3d 20 28 52 74 72 65 65 20  pRtree = (Rtree 
39332 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  *)sqlite3_malloc
39333 28 73 69 7a 65 6f 66 28 52 74 72 65 65 29 2b 6e  (sizeof(Rtree)+n
39334 44 62 2b 6e 4e 61 6d 65 2b 32 29 3b 0a 20 20 69  Db+nName+2);.  i
39335 66 28 20 21 70 52 74 72 65 65 20 29 7b 0a 20 20  f( !pRtree ){.  
39336 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
39337 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d  NOMEM;.  }.  mem
39338 73 65 74 28 70 52 74 72 65 65 2c 20 30 2c 20 73  set(pRtree, 0, s
39339 69 7a 65 6f 66 28 52 74 72 65 65 29 2b 6e 44 62  izeof(Rtree)+nDb
3933a 2b 6e 4e 61 6d 65 2b 32 29 3b 0a 20 20 70 52 74  +nName+2);.  pRt
3933b 72 65 65 2d 3e 6e 42 75 73 79 20 3d 20 31 3b 0a  ree->nBusy = 1;.
3933c 20 20 70 52 74 72 65 65 2d 3e 62 61 73 65 2e 70    pRtree->base.p
3933d 4d 6f 64 75 6c 65 20 3d 20 26 72 74 72 65 65 4d  Module = &rtreeM
3933e 6f 64 75 6c 65 3b 0a 20 20 70 52 74 72 65 65 2d  odule;.  pRtree-
3933f 3e 7a 44 62 20 3d 20 28 63 68 61 72 20 2a 29 26  >zDb = (char *)&
39340 70 52 74 72 65 65 5b 31 5d 3b 0a 20 20 70 52 74  pRtree[1];.  pRt
39341 72 65 65 2d 3e 7a 4e 61 6d 65 20 3d 20 26 70 52  ree->zName = &pR
39342 74 72 65 65 2d 3e 7a 44 62 5b 6e 44 62 2b 31 5d  tree->zDb[nDb+1]
39343 3b 0a 20 20 70 52 74 72 65 65 2d 3e 6e 44 69 6d  ;.  pRtree->nDim
39344 20 3d 20 28 61 72 67 63 2d 34 29 2f 32 3b 0a 20   = (argc-4)/2;. 
39345 20 70 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50   pRtree->nBytesP
39346 65 72 43 65 6c 6c 20 3d 20 38 20 2b 20 70 52 74  erCell = 8 + pRt
39347 72 65 65 2d 3e 6e 44 69 6d 2a 34 2a 32 3b 0a 20  ree->nDim*4*2;. 
39348 20 70 52 74 72 65 65 2d 3e 65 43 6f 6f 72 64 54   pRtree->eCoordT
39349 79 70 65 20 3d 20 65 43 6f 6f 72 64 54 79 70 65  ype = eCoordType
3934a 3b 0a 20 20 6d 65 6d 63 70 79 28 70 52 74 72 65  ;.  memcpy(pRtre
3934b 65 2d 3e 7a 44 62 2c 20 61 72 67 76 5b 31 5d 2c  e->zDb, argv[1],
3934c 20 6e 44 62 29 3b 0a 20 20 6d 65 6d 63 70 79 28   nDb);.  memcpy(
3934d 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 2c 20 61  pRtree->zName, a
3934e 72 67 76 5b 32 5d 2c 20 6e 4e 61 6d 65 29 3b 0a  rgv[2], nName);.
3934f 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74  .  /* Figure out
39350 20 74 68 65 20 6e 6f 64 65 20 73 69 7a 65 20 74   the node size t
39351 6f 20 75 73 65 2e 20 42 79 20 64 65 66 61 75 6c  o use. By defaul
39352 74 2c 20 75 73 65 20 36 34 20 62 79 74 65 73 20  t, use 64 bytes 
39353 6c 65 73 73 20 74 68 61 6e 0a 20 20 2a 2a 20 74  less than.  ** t
39354 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65  he database page
39355 2d 73 69 7a 65 2e 20 54 68 69 73 20 65 6e 73 75  -size. This ensu
39356 72 65 73 20 74 68 61 74 20 65 61 63 68 20 6e 6f  res that each no
39357 64 65 20 69 73 20 73 74 6f 72 65 64 20 6f 6e 0a  de is stored on.
39358 20 20 2a 2a 20 61 20 73 69 6e 67 6c 65 20 64 61    ** a single da
39359 74 61 62 61 73 65 20 70 61 67 65 2e 0a 20 20 2a  tabase page..  *
3935a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 64 61  *.  ** If the da
3935b 74 61 62 61 73 64 20 70 61 67 65 2d 73 69 7a 65  tabasd page-size
3935c 20 69 73 20 73 6f 20 6c 61 72 67 65 20 74 68 61   is so large tha
3935d 74 20 6d 6f 72 65 20 74 68 61 6e 20 52 54 52 45  t more than RTRE
3935e 45 5f 4d 41 58 43 45 4c 4c 53 0a 20 20 2a 2a 20  E_MAXCELLS.  ** 
3935f 65 6e 74 72 69 65 73 20 77 6f 75 6c 64 20 66 69  entries would fi
39360 74 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 6e 6f  t in a single no
39361 64 65 2c 20 75 73 65 20 61 20 73 6d 61 6c 6c 65  de, use a smalle
39362 72 20 6e 6f 64 65 2d 73 69 7a 65 2e 0a 20 20 2a  r node-size..  *
39363 2f 0a 20 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64  /.  pRtree->iNod
39364 65 53 69 7a 65 20 3d 20 69 50 61 67 65 53 69 7a  eSize = iPageSiz
39365 65 2d 36 34 3b 0a 20 20 69 66 28 20 28 34 2b 70  e-64;.  if( (4+p
39366 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50 65 72  Rtree->nBytesPer
39367 43 65 6c 6c 2a 52 54 52 45 45 5f 4d 41 58 43 45  Cell*RTREE_MAXCE
39368 4c 4c 53 29 3c 70 52 74 72 65 65 2d 3e 69 4e 6f  LLS)<pRtree->iNo
39369 64 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 70 52  deSize ){.    pR
3936a 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 20  tree->iNodeSize 
3936b 3d 20 34 2b 70 52 74 72 65 65 2d 3e 6e 42 79 74  = 4+pRtree->nByt
3936c 65 73 50 65 72 43 65 6c 6c 2a 52 54 52 45 45 5f  esPerCell*RTREE_
3936d 4d 41 58 43 45 4c 4c 53 3b 0a 20 20 7d 0a 0a 20  MAXCELLS;.  }.. 
3936e 20 2f 2a 20 43 72 65 61 74 65 2f 43 6f 6e 6e 65   /* Create/Conne
3936f 63 74 20 74 6f 20 74 68 65 20 75 6e 64 65 72 6c  ct to the underl
39370 79 69 6e 67 20 72 65 6c 61 74 69 6f 6e 61 6c 20  ying relational 
39371 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e  database schema.
39372 20 49 66 0a 20 20 2a 2a 20 74 68 61 74 20 69 73   If.  ** that is
39373 20 73 75 63 63 65 73 73 66 75 6c 2c 20 63 61 6c   successful, cal
39374 6c 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72  l sqlite3_declar
39375 65 5f 76 74 61 62 28 29 20 74 6f 20 63 6f 6e 66  e_vtab() to conf
39376 69 67 75 72 65 0a 20 20 2a 2a 20 74 68 65 20 72  igure.  ** the r
39377 2d 74 72 65 65 20 74 61 62 6c 65 20 73 63 68 65  -tree table sche
39378 6d 61 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  ma..  */.  if( (
39379 72 63 20 3d 20 72 74 72 65 65 53 71 6c 49 6e 69  rc = rtreeSqlIni
3937a 74 28 70 52 74 72 65 65 2c 20 64 62 2c 20 61 72  t(pRtree, db, ar
3937b 67 76 5b 31 5d 2c 20 61 72 67 76 5b 32 5d 2c 20  gv[1], argv[2], 
3937c 69 73 43 72 65 61 74 65 29 29 20 29 7b 0a 20 20  isCreate)) ){.  
3937d 20 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74    *pzErr = sqlit
3937e 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 22 2c  e3_mprintf("%s",
3937f 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28   sqlite3_errmsg(
39380 64 62 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  db));.  }else{. 
39381 20 20 20 63 68 61 72 20 2a 7a 53 71 6c 20 3d 20     char *zSql = 
39382 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
39383 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 78 28  "CREATE TABLE x(
39384 25 73 22 2c 20 61 72 67 76 5b 33 5d 29 3b 0a 20  %s", argv[3]);. 
39385 20 20 20 63 68 61 72 20 2a 7a 54 6d 70 3b 0a 20     char *zTmp;. 
39386 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 66     int ii;.    f
39387 6f 72 28 69 69 3d 34 3b 20 7a 53 71 6c 20 26 26  or(ii=4; zSql &&
39388 20 69 69 3c 61 72 67 63 3b 20 69 69 2b 2b 29 7b   ii<argc; ii++){
39389 0a 20 20 20 20 20 20 7a 54 6d 70 20 3d 20 7a 53  .      zTmp = zS
3938a 71 6c 3b 0a 20 20 20 20 20 20 7a 53 71 6c 20 3d  ql;.      zSql =
3938b 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
3938c 28 22 25 73 2c 20 25 73 22 2c 20 7a 54 6d 70 2c  ("%s, %s", zTmp,
3938d 20 61 72 67 76 5b 69 69 5d 29 3b 0a 20 20 20 20   argv[ii]);.    
3938e 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
3938f 54 6d 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Tmp);.    }.    
39390 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20 20 20 20  if( zSql ){.    
39391 20 20 7a 54 6d 70 20 3d 20 7a 53 71 6c 3b 0a 20    zTmp = zSql;. 
39392 20 20 20 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69       zSql = sqli
39393 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 29  te3_mprintf("%s)
39394 3b 22 2c 20 7a 54 6d 70 29 3b 0a 20 20 20 20 20  ;", zTmp);.     
39395 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 54   sqlite3_free(zT
39396 6d 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  mp);.    }.    i
39397 66 28 20 21 7a 53 71 6c 20 29 7b 0a 20 20 20 20  f( !zSql ){.    
39398 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
39399 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  MEM;.    }else i
3939a 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72  f( SQLITE_OK!=(r
3939b 63 20 3d 20 73 71 6c 69 74 65 33 5f 64 65 63 6c  c = sqlite3_decl
3939c 61 72 65 5f 76 74 61 62 28 64 62 2c 20 7a 53 71  are_vtab(db, zSq
3939d 6c 29 29 20 29 7b 0a 20 20 20 20 20 20 2a 70 7a  l)) ){.      *pz
3939e 45 72 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  Err = sqlite3_mp
3939f 72 69 6e 74 66 28 22 25 73 22 2c 20 73 71 6c 69  rintf("%s", sqli
393a0 74 65 33 5f 65 72 72 6d 73 67 28 64 62 29 29 3b  te3_errmsg(db));
393a1 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
393a2 65 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a 20  e3_free(zSql);. 
393a3 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   }..  if( rc==SQ
393a4 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a  LITE_OK ){.    *
393a5 70 70 56 74 61 62 20 3d 20 28 73 71 6c 69 74 65  ppVtab = (sqlite
393a6 33 5f 76 74 61 62 20 2a 29 70 52 74 72 65 65 3b  3_vtab *)pRtree;
393a7 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 74  .  }else{.    rt
393a8 72 65 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  reeRelease(pRtre
393a9 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  e);.  }.  return
393aa 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49   rc;.}.../*.** I
393ab 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
393ac 20 61 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69   a scalar functi
393ad 6f 6e 20 74 68 61 74 20 64 65 63 6f 64 65 73 20  on that decodes 
393ae 72 2d 74 72 65 65 20 6e 6f 64 65 73 20 74 6f 0a  r-tree nodes to.
393af 2a 2a 20 68 75 6d 61 6e 20 72 65 61 64 61 62 6c  ** human readabl
393b0 65 20 73 74 72 69 6e 67 73 2e 20 54 68 69 73 20  e strings. This 
393b1 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20  can be used for 
393b2 64 65 62 75 67 67 69 6e 67 20 61 6e 64 20 61 6e  debugging and an
393b3 61 6c 79 73 69 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  alysis..**.** Th
393b4 65 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f  e scalar functio
393b5 6e 20 74 61 6b 65 73 20 74 77 6f 20 61 72 67 75  n takes two argu
393b6 6d 65 6e 74 73 2c 20 61 20 62 6c 6f 62 20 6f 66  ments, a blob of
393b7 20 64 61 74 61 20 63 6f 6e 74 61 69 6e 69 6e 67   data containing
393b8 0a 2a 2a 20 61 6e 20 72 2d 74 72 65 65 20 6e 6f  .** an r-tree no
393b9 64 65 2c 20 61 6e 64 20 74 68 65 20 6e 75 6d 62  de, and the numb
393ba 65 72 20 6f 66 20 64 69 6d 65 6e 73 69 6f 6e 73  er of dimensions
393bb 20 74 68 65 20 72 2d 74 72 65 65 20 69 6e 64 65   the r-tree inde
393bc 78 65 73 2e 0a 2a 2a 20 46 6f 72 20 61 20 74 77  xes..** For a tw
393bd 6f 2d 64 69 6d 65 6e 73 69 6f 6e 61 6c 20 72 2d  o-dimensional r-
393be 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 63  tree structure c
393bf 61 6c 6c 65 64 20 22 72 74 22 2c 20 74 6f 20 64  alled "rt", to d
393c0 65 73 65 72 69 61 6c 69 7a 65 0a 2a 2a 20 61 6c  eserialize.** al
393c1 6c 20 6e 6f 64 65 73 2c 20 61 20 73 74 61 74 65  l nodes, a state
393c2 6d 65 6e 74 20 6c 69 6b 65 3a 0a 2a 2a 0a 2a 2a  ment like:.**.**
393c3 20 20 20 53 45 4c 45 43 54 20 72 74 72 65 65 6e     SELECT rtreen
393c4 6f 64 65 28 32 2c 20 64 61 74 61 29 20 46 52 4f  ode(2, data) FRO
393c5 4d 20 72 74 5f 6e 6f 64 65 3b 0a 2a 2a 0a 2a 2a  M rt_node;.**.**
393c6 20 54 68 65 20 68 75 6d 61 6e 20 72 65 61 64 61   The human reada
393c7 62 6c 65 20 73 74 72 69 6e 67 20 74 61 6b 65 73  ble string takes
393c8 20 74 68 65 20 66 6f 72 6d 20 6f 66 20 61 20 54   the form of a T
393c9 63 6c 20 6c 69 73 74 20 77 69 74 68 20 6f 6e 65  cl list with one
393ca 0a 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20 65 61  .** entry for ea
393cb 63 68 20 63 65 6c 6c 20 69 6e 20 74 68 65 20 72  ch cell in the r
393cc 2d 74 72 65 65 20 6e 6f 64 65 2e 20 45 61 63 68  -tree node. Each
393cd 20 65 6e 74 72 79 20 69 73 20 69 74 73 65 6c 66   entry is itself
393ce 20 61 0a 2a 2a 20 6c 69 73 74 2c 20 63 6f 6e 74   a.** list, cont
393cf 61 69 6e 69 6e 67 20 74 68 65 20 38 2d 62 79 74  aining the 8-byt
393d0 65 20 72 6f 77 69 64 2f 70 61 67 65 6e 6f 20 66  e rowid/pageno f
393d1 6f 6c 6c 6f 77 65 64 20 62 79 20 74 68 65 20 0a  ollowed by the .
393d2 2a 2a 20 3c 6e 75 6d 2d 64 69 6d 65 6e 73 69 6f  ** <num-dimensio
393d3 6e 3e 2a 32 20 63 6f 6f 72 64 69 6e 61 74 65 73  n>*2 coordinates
393d4 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
393d5 20 72 74 72 65 65 6e 6f 64 65 28 73 71 6c 69 74   rtreenode(sqlit
393d6 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 74 78 2c  e3_context *ctx,
393d7 20 69 6e 74 20 6e 41 72 67 2c 20 73 71 6c 69 74   int nArg, sqlit
393d8 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 41 72 67  e3_value **apArg
393d9 29 7b 0a 20 20 63 68 61 72 20 2a 7a 54 65 78 74  ){.  char *zText
393da 20 3d 20 30 3b 0a 20 20 52 74 72 65 65 4e 6f 64   = 0;.  RtreeNod
393db 65 20 6e 6f 64 65 3b 0a 20 20 52 74 72 65 65 20  e node;.  Rtree 
393dc 74 72 65 65 3b 0a 20 20 69 6e 74 20 69 69 3b 0a  tree;.  int ii;.
393dd 0a 20 20 6d 65 6d 73 65 74 28 26 6e 6f 64 65 2c  .  memset(&node,
393de 20 30 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65   0, sizeof(Rtree
393df 4e 6f 64 65 29 29 3b 0a 20 20 6d 65 6d 73 65 74  Node));.  memset
393e0 28 26 74 72 65 65 2c 20 30 2c 20 73 69 7a 65 6f  (&tree, 0, sizeo
393e1 66 28 52 74 72 65 65 29 29 3b 0a 20 20 74 72 65  f(Rtree));.  tre
393e2 65 2e 6e 44 69 6d 20 3d 20 73 71 6c 69 74 65 33  e.nDim = sqlite3
393e3 5f 76 61 6c 75 65 5f 69 6e 74 28 61 70 41 72 67  _value_int(apArg
393e4 5b 30 5d 29 3b 0a 20 20 74 72 65 65 2e 6e 42 79  [0]);.  tree.nBy
393e5 74 65 73 50 65 72 43 65 6c 6c 20 3d 20 38 20 2b  tesPerCell = 8 +
393e6 20 38 20 2a 20 74 72 65 65 2e 6e 44 69 6d 3b 0a   8 * tree.nDim;.
393e7 20 20 6e 6f 64 65 2e 7a 44 61 74 61 20 3d 20 28    node.zData = (
393e8 75 38 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  u8 *)sqlite3_val
393e9 75 65 5f 62 6c 6f 62 28 61 70 41 72 67 5b 31 5d  ue_blob(apArg[1]
393ea 29 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  );..  for(ii=0; 
393eb 69 69 3c 4e 43 45 4c 4c 28 26 6e 6f 64 65 29 3b  ii<NCELL(&node);
393ec 20 69 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72   ii++){.    char
393ed 20 7a 43 65 6c 6c 5b 35 31 32 5d 3b 0a 20 20 20   zCell[512];.   
393ee 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 30 3b 0a   int nCell = 0;.
393ef 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20 63 65      RtreeCell ce
393f0 6c 6c 3b 0a 20 20 20 20 69 6e 74 20 6a 6a 3b 0a  ll;.    int jj;.
393f1 0a 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c  .    nodeGetCell
393f2 28 26 74 72 65 65 2c 20 26 6e 6f 64 65 2c 20 69  (&tree, &node, i
393f3 69 2c 20 26 63 65 6c 6c 29 3b 0a 20 20 20 20 73  i, &cell);.    s
393f4 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
393f5 35 31 32 2d 6e 43 65 6c 6c 2c 26 7a 43 65 6c 6c  512-nCell,&zCell
393f6 5b 6e 43 65 6c 6c 5d 2c 22 25 64 22 2c 20 63 65  [nCell],"%d", ce
393f7 6c 6c 2e 69 52 6f 77 69 64 29 3b 0a 20 20 20 20  ll.iRowid);.    
393f8 6e 43 65 6c 6c 20 3d 20 73 74 72 6c 65 6e 28 7a  nCell = strlen(z
393f9 43 65 6c 6c 29 3b 0a 20 20 20 20 66 6f 72 28 6a  Cell);.    for(j
393fa 6a 3d 30 3b 20 6a 6a 3c 74 72 65 65 2e 6e 44 69  j=0; jj<tree.nDi
393fb 6d 2a 32 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20  m*2; jj++){.    
393fc 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
393fd 74 66 28 35 31 32 2d 6e 43 65 6c 6c 2c 26 7a 43  tf(512-nCell,&zC
393fe 65 6c 6c 5b 6e 43 65 6c 6c 5d 2c 22 20 25 66 22  ell[nCell]," %f"
393ff 2c 28 64 6f 75 62 6c 65 29 63 65 6c 6c 2e 61 43  ,(double)cell.aC
39400 6f 6f 72 64 5b 6a 6a 5d 2e 66 29 3b 0a 20 20 20  oord[jj].f);.   
39401 20 20 20 6e 43 65 6c 6c 20 3d 20 73 74 72 6c 65     nCell = strle
39402 6e 28 7a 43 65 6c 6c 29 3b 0a 20 20 20 20 7d 0a  n(zCell);.    }.
39403 0a 20 20 20 20 69 66 28 20 7a 54 65 78 74 20 29  .    if( zText )
39404 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54  {.      char *zT
39405 65 78 74 4e 65 77 20 3d 20 73 71 6c 69 74 65 33  extNew = sqlite3
39406 5f 6d 70 72 69 6e 74 66 28 22 25 73 20 7b 25 73  _mprintf("%s {%s
39407 7d 22 2c 20 7a 54 65 78 74 2c 20 7a 43 65 6c 6c  }", zText, zCell
39408 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
39409 5f 66 72 65 65 28 7a 54 65 78 74 29 3b 0a 20 20  _free(zText);.  
3940a 20 20 20 20 7a 54 65 78 74 20 3d 20 7a 54 65 78      zText = zTex
3940b 74 4e 65 77 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  tNew;.    }else{
3940c 0a 20 20 20 20 20 20 7a 54 65 78 74 20 3d 20 73  .      zText = s
3940d 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
3940e 7b 25 73 7d 22 2c 20 7a 43 65 6c 6c 29 3b 0a 20  {%s}", zCell);. 
3940f 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 73 71     }.  }.  .  sq
39410 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
39411 74 28 63 74 78 2c 20 7a 54 65 78 74 2c 20 2d 31  t(ctx, zText, -1
39412 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b  , sqlite3_free);
39413 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .}..static void 
39414 72 74 72 65 65 64 65 70 74 68 28 73 71 6c 69 74  rtreedepth(sqlit
39415 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 74 78 2c  e3_context *ctx,
39416 20 69 6e 74 20 6e 41 72 67 2c 20 73 71 6c 69 74   int nArg, sqlit
39417 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 41 72 67  e3_value **apArg
39418 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  ){.  if( sqlite3
39419 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 70 41 72  _value_type(apAr
3941a 67 5b 30 5d 29 21 3d 53 51 4c 49 54 45 5f 42 4c  g[0])!=SQLITE_BL
3941b 4f 42 20 0a 20 20 20 7c 7c 20 73 71 6c 69 74 65  OB .   || sqlite
3941c 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 70  3_value_bytes(ap
3941d 41 72 67 5b 30 5d 29 3c 32 0a 20 20 29 7b 0a 20  Arg[0])<2.  ){. 
3941e 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
3941f 74 5f 65 72 72 6f 72 28 63 74 78 2c 20 22 49 6e  t_error(ctx, "In
39420 76 61 6c 69 64 20 61 72 67 75 6d 65 6e 74 20 74  valid argument t
39421 6f 20 72 74 72 65 65 64 65 70 74 68 28 29 22 2c  o rtreedepth()",
39422 20 2d 31 29 3b 20 0a 20 20 7d 65 6c 73 65 7b 0a   -1); .  }else{.
39423 20 20 20 20 75 38 20 2a 7a 42 6c 6f 62 20 3d 20      u8 *zBlob = 
39424 28 75 38 20 2a 29 73 71 6c 69 74 65 33 5f 76 61  (u8 *)sqlite3_va
39425 6c 75 65 5f 62 6c 6f 62 28 61 70 41 72 67 5b 30  lue_blob(apArg[0
39426 5d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ]);.    sqlite3_
39427 72 65 73 75 6c 74 5f 69 6e 74 28 63 74 78 2c 20  result_int(ctx, 
39428 72 65 61 64 49 6e 74 31 36 28 7a 42 6c 6f 62 29  readInt16(zBlob)
39429 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
3942a 52 65 67 69 73 74 65 72 20 74 68 65 20 72 2d 74  Register the r-t
3942b 72 65 65 20 6d 6f 64 75 6c 65 20 77 69 74 68 20  ree module with 
3942c 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  database handle 
3942d 64 62 2e 20 54 68 69 73 20 63 72 65 61 74 65 73  db. This creates
3942e 20 74 68 65 0a 2a 2a 20 76 69 72 74 75 61 6c 20   the.** virtual 
3942f 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 22 72 74  table module "rt
39430 72 65 65 22 20 61 6e 64 20 74 68 65 20 64 65 62  ree" and the deb
39431 75 67 67 69 6e 67 2f 61 6e 61 6c 79 73 69 73 20  ugging/analysis 
39432 73 63 61 6c 61 72 20 0a 2a 2a 20 66 75 6e 63 74  scalar .** funct
39433 69 6f 6e 20 22 72 74 72 65 65 6e 6f 64 65 22 2e  ion "rtreenode".
39434 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
39435 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 74  TE int sqlite3Rt
39436 72 65 65 49 6e 69 74 28 73 71 6c 69 74 65 33 20  reeInit(sqlite3 
39437 2a 64 62 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  *db){.  int rc =
39438 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69   SQLITE_OK;..  i
39439 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
3943a 20 29 7b 0a 20 20 20 20 69 6e 74 20 75 74 66 38   ){.    int utf8
3943b 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a   = SQLITE_UTF8;.
3943c 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
3943d 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
3943e 28 64 62 2c 20 22 72 74 72 65 65 6e 6f 64 65 22  (db, "rtreenode"
3943f 2c 20 32 2c 20 75 74 66 38 2c 20 30 2c 20 72 74  , 2, utf8, 0, rt
39440 72 65 65 6e 6f 64 65 2c 20 30 2c 20 30 29 3b 0a  reenode, 0, 0);.
39441 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51    }.  if( rc==SQ
39442 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69  LITE_OK ){.    i
39443 6e 74 20 75 74 66 38 20 3d 20 53 51 4c 49 54 45  nt utf8 = SQLITE
39444 5f 55 54 46 38 3b 0a 20 20 20 20 72 63 20 3d 20  _UTF8;.    rc = 
39445 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
39446 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22 72 74 72  unction(db, "rtr
39447 65 65 64 65 70 74 68 22 2c 20 31 2c 20 75 74 66  eedepth", 1, utf
39448 38 2c 20 30 2c 72 74 72 65 65 64 65 70 74 68 2c  8, 0,rtreedepth,
39449 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66   0, 0);.  }.  if
3944a 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
3944b 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 63 20 3d  ){.    void *c =
3944c 20 28 76 6f 69 64 20 2a 29 52 54 52 45 45 5f 43   (void *)RTREE_C
3944d 4f 4f 52 44 5f 52 45 41 4c 33 32 3b 0a 20 20 20  OORD_REAL32;.   
3944e 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72   rc = sqlite3_cr
3944f 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 64  eate_module_v2(d
39450 62 2c 20 22 72 74 72 65 65 22 2c 20 26 72 74 72  b, "rtree", &rtr
39451 65 65 4d 6f 64 75 6c 65 2c 20 63 2c 20 30 29 3b  eeModule, c, 0);
39452 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53  .  }.  if( rc==S
39453 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
39454 76 6f 69 64 20 2a 63 20 3d 20 28 76 6f 69 64 20  void *c = (void 
39455 2a 29 52 54 52 45 45 5f 43 4f 4f 52 44 5f 49 4e  *)RTREE_COORD_IN
39456 54 33 32 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  T32;.    rc = sq
39457 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64  lite3_create_mod
39458 75 6c 65 5f 76 32 28 64 62 2c 20 22 72 74 72 65  ule_v2(db, "rtre
39459 65 5f 69 33 32 22 2c 20 26 72 74 72 65 65 4d 6f  e_i32", &rtreeMo
3945a 64 75 6c 65 2c 20 63 2c 20 30 29 3b 0a 20 20 7d  dule, c, 0);.  }
3945b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
3945c 0a 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 43 4f  ..#if !SQLITE_CO
3945d 52 45 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  RE.SQLITE_API in
3945e 74 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73  t sqlite3_extens
3945f 69 6f 6e 5f 69 6e 69 74 28 0a 20 20 73 71 6c 69  ion_init(.  sqli
39460 74 65 33 20 2a 64 62 2c 0a 20 20 63 68 61 72 20  te3 *db,.  char 
39461 2a 2a 70 7a 45 72 72 4d 73 67 2c 0a 20 20 63 6f  **pzErrMsg,.  co
39462 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f  nst sqlite3_api_
39463 72 6f 75 74 69 6e 65 73 20 2a 70 41 70 69 0a 29  routines *pApi.)
39464 7b 0a 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e  {.  SQLITE_EXTEN
39465 53 49 4f 4e 5f 49 4e 49 54 32 28 70 41 70 69 29  SION_INIT2(pApi)
39466 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
39467 33 52 74 72 65 65 49 6e 69 74 28 64 62 29 3b 0a  3RtreeInit(db);.
39468 7d 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66  }.#endif..#endif
39469 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
3946a 2a 20 45 6e 64 20 6f 66 20 72 74 72 65 65 2e 63  * End of rtree.c
3946b 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
3946c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3946d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3946e 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
3946f 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 69 63 75  * Begin file icu
39470 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
39471 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39472 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39473 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4d 61 79  /./*.** 2007 May
39474 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74   6.**.** The aut
39475 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
39476 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
39477 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
39478 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
39479 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
3947a 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
3947b 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
3947c 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
3947d 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
3947e 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
3947f 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
39480 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
39481 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
39482 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
39483 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
39484 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
39485 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
39486 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39487 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39488 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39489 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3948a 2a 0a 2a 2a 20 24 49 64 3a 20 69 63 75 2e 63 2c  *.** $Id: icu.c,
3948b 76 20 31 2e 37 20 32 30 30 37 2f 31 32 2f 31 33  v 1.7 2007/12/13
3948c 20 32 31 3a 35 34 3a 31 31 20 64 72 68 20 45 78   21:54:11 drh Ex
3948d 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  p $.**.** This f
3948e 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61  ile implements a
3948f 6e 20 69 6e 74 65 67 72 61 74 69 6f 6e 20 62 65  n integration be
39490 74 77 65 65 6e 20 74 68 65 20 49 43 55 20 6c 69  tween the ICU li
39491 62 72 61 72 79 20 0a 2a 2a 20 28 22 49 6e 74 65  brary .** ("Inte
39492 72 6e 61 74 69 6f 6e 61 6c 20 43 6f 6d 70 6f 6e  rnational Compon
39493 65 6e 74 73 20 66 6f 72 20 55 6e 69 63 6f 64 65  ents for Unicode
39494 22 2c 20 61 6e 20 6f 70 65 6e 2d 73 6f 75 72 63  ", an open-sourc
39495 65 20 6c 69 62 72 61 72 79 20 0a 2a 2a 20 66 6f  e library .** fo
39496 72 20 68 61 6e 64 6c 69 6e 67 20 75 6e 69 63 6f  r handling unico
39497 64 65 20 64 61 74 61 29 20 61 6e 64 20 53 51 4c  de data) and SQL
39498 69 74 65 2e 20 54 68 65 20 69 6e 74 65 67 72 61  ite. The integra
39499 74 69 6f 6e 20 75 73 65 73 20 0a 2a 2a 20 49 43  tion uses .** IC
3949a 55 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68 65  U to provide the
3949b 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 6f 20 53 51   following to SQ
3949c 4c 69 74 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20  Lite:.**.**   * 
3949d 41 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  An implementatio
3949e 6e 20 6f 66 20 74 68 65 20 53 51 4c 20 72 65 67  n of the SQL reg
3949f 65 78 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 28  exp() function (
394a0 61 6e 64 20 68 65 6e 63 65 20 52 45 47 45 58 50  and hence REGEXP
394a1 0a 2a 2a 20 20 20 20 20 6f 70 65 72 61 74 6f 72  .**     operator
394a2 29 20 75 73 69 6e 67 20 74 68 65 20 49 43 55 20  ) using the ICU 
394a3 75 72 65 67 65 78 5f 58 58 28 29 20 41 50 49 73  uregex_XX() APIs
394a4 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 6d 70 6c  ..**.**   * Impl
394a5 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74  ementations of t
394a6 68 65 20 53 51 4c 20 73 63 61 6c 61 72 20 75 70  he SQL scalar up
394a7 70 65 72 28 29 20 61 6e 64 20 6c 6f 77 65 72 28  per() and lower(
394a8 29 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 20  ) functions.**  
394a9 20 20 20 66 6f 72 20 63 61 73 65 20 6d 61 70 70     for case mapp
394aa 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49  ing..**.**   * I
394ab 6e 74 65 67 72 61 74 69 6f 6e 20 6f 66 20 49 43  ntegration of IC
394ac 55 20 61 6e 64 20 53 51 4c 69 74 65 20 63 6f 6c  U and SQLite col
394ad 6c 61 74 69 6f 6e 20 73 65 71 65 6e 63 65 73 2e  lation seqences.
394ae 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 41 6e 20 69 6d  .**.**   * An im
394af 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
394b0 74 68 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f  the LIKE operato
394b1 72 20 74 68 61 74 20 75 73 65 73 20 49 43 55 20  r that uses ICU 
394b2 74 6f 20 0a 2a 2a 20 20 20 20 20 70 72 6f 76 69  to .**     provi
394b3 64 65 20 63 61 73 65 2d 69 6e 64 65 70 65 6e 64  de case-independ
394b4 65 6e 74 20 6d 61 74 63 68 69 6e 67 2e 0a 2a 2f  ent matching..*/
394b5 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
394b6 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64  QLITE_CORE) || d
394b7 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
394b8 41 42 4c 45 5f 49 43 55 29 0a 0a 2f 2a 20 49 6e  ABLE_ICU)../* In
394b9 63 6c 75 64 65 20 49 43 55 20 68 65 61 64 65 72  clude ICU header
394ba 73 20 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 75  s */.#include <u
394bb 6e 69 63 6f 64 65 2f 75 74 79 70 65 73 2e 68 3e  nicode/utypes.h>
394bc 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69 63 6f  .#include <unico
394bd 64 65 2f 75 72 65 67 65 78 2e 68 3e 0a 23 69 6e  de/uregex.h>.#in
394be 63 6c 75 64 65 20 3c 75 6e 69 63 6f 64 65 2f 75  clude <unicode/u
394bf 73 74 72 69 6e 67 2e 68 3e 0a 23 69 6e 63 6c 75  string.h>.#inclu
394c0 64 65 20 3c 75 6e 69 63 6f 64 65 2f 75 63 6f 6c  de <unicode/ucol
394c1 2e 68 3e 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51  .h>...#ifndef SQ
394c2 4c 49 54 45 5f 43 4f 52 45 0a 20 20 53 51 4c 49  LITE_CORE.  SQLI
394c3 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49  TE_EXTENSION_INI
394c4 54 31 0a 23 65 6c 73 65 0a 23 65 6e 64 69 66 0a  T1.#else.#endif.
394c5 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 6c  ./*.** Maximum l
394c6 65 6e 67 74 68 20 28 69 6e 20 62 79 74 65 73 29  ength (in bytes)
394c7 20 6f 66 20 74 68 65 20 70 61 74 74 65 72 6e 20   of the pattern 
394c8 69 6e 20 61 20 4c 49 4b 45 20 6f 72 20 47 4c 4f  in a LIKE or GLO
394c9 42 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 2e 0a 2a  B.** operator..*
394ca 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
394cb 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52  _MAX_LIKE_PATTER
394cc 4e 5f 4c 45 4e 47 54 48 0a 23 20 64 65 66 69 6e  N_LENGTH.# defin
394cd 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b  e SQLITE_MAX_LIK
394ce 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48  E_PATTERN_LENGTH
394cf 20 35 30 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f   50000.#endif../
394d0 2a 0a 2a 2a 20 56 65 72 73 69 6f 6e 20 6f 66 20  *.** Version of 
394d1 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 74  sqlite3_free() t
394d2 68 61 74 20 69 73 20 61 6c 77 61 79 73 20 61 20  hat is always a 
394d3 66 75 6e 63 74 69 6f 6e 2c 20 6e 65 76 65 72 20  function, never 
394d4 61 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 73 74 61 74  a macro..*/.stat
394d5 69 63 20 76 6f 69 64 20 78 46 72 65 65 28 76 6f  ic void xFree(vo
394d6 69 64 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65  id *p){.  sqlite
394d7 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a  3_free(p);.}../*
394d8 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 77 6f 20  .** Compare two 
394d9 55 54 46 2d 38 20 73 74 72 69 6e 67 73 20 66 6f  UTF-8 strings fo
394da 72 20 65 71 75 61 6c 69 74 79 20 77 68 65 72 65  r equality where
394db 20 74 68 65 20 66 69 72 73 74 20 73 74 72 69 6e   the first strin
394dc 67 20 69 73 0a 2a 2a 20 61 20 22 4c 49 4b 45 22  g is.** a "LIKE"
394dd 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 52 65 74   expression. Ret
394de 75 72 6e 20 74 72 75 65 20 28 31 29 20 69 66 20  urn true (1) if 
394df 74 68 65 79 20 61 72 65 20 74 68 65 20 73 61 6d  they are the sam
394e0 65 20 61 6e 64 20 0a 2a 2a 20 66 61 6c 73 65 20  e and .** false 
394e1 28 30 29 20 69 66 20 74 68 65 79 20 61 72 65 20  (0) if they are 
394e2 64 69 66 66 65 72 65 6e 74 2e 0a 2a 2f 0a 73 74  different..*/.st
394e3 61 74 69 63 20 69 6e 74 20 69 63 75 4c 69 6b 65  atic int icuLike
394e4 43 6f 6d 70 61 72 65 28 0a 20 20 63 6f 6e 73 74  Compare(.  const
394e5 20 75 69 6e 74 38 5f 74 20 2a 7a 50 61 74 74 65   uint8_t *zPatte
394e6 72 6e 2c 20 20 20 2f 2a 20 4c 49 4b 45 20 70 61  rn,   /* LIKE pa
394e7 74 74 65 72 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74  ttern */.  const
394e8 20 75 69 6e 74 38 5f 74 20 2a 7a 53 74 72 69 6e   uint8_t *zStrin
394e9 67 2c 20 20 20 20 2f 2a 20 54 68 65 20 55 54 46  g,    /* The UTF
394ea 2d 38 20 73 74 72 69 6e 67 20 74 6f 20 63 6f 6d  -8 string to com
394eb 70 61 72 65 20 61 67 61 69 6e 73 74 20 2a 2f 0a  pare against */.
394ec 20 20 63 6f 6e 73 74 20 55 43 68 61 72 33 32 20    const UChar32 
394ed 75 45 73 63 20 20 20 20 20 20 20 20 20 2f 2a 20  uEsc         /* 
394ee 54 68 65 20 65 73 63 61 70 65 20 63 68 61 72 61  The escape chara
394ef 63 74 65 72 20 2a 2f 0a 29 7b 0a 20 20 73 74 61  cter */.){.  sta
394f0 74 69 63 20 63 6f 6e 73 74 20 69 6e 74 20 4d 41  tic const int MA
394f1 54 43 48 5f 4f 4e 45 20 3d 20 28 55 43 68 61 72  TCH_ONE = (UChar
394f2 33 32 29 27 5f 27 3b 0a 20 20 73 74 61 74 69 63  32)'_';.  static
394f3 20 63 6f 6e 73 74 20 69 6e 74 20 4d 41 54 43 48   const int MATCH
394f4 5f 41 4c 4c 20 3d 20 28 55 43 68 61 72 33 32 29  _ALL = (UChar32)
394f5 27 25 27 3b 0a 0a 20 20 69 6e 74 20 69 50 61 74  '%';..  int iPat
394f6 74 65 72 6e 20 3d 20 30 3b 20 20 20 20 20 20 20  tern = 0;       
394f7 2f 2a 20 43 75 72 72 65 6e 74 20 62 79 74 65 20  /* Current byte 
394f8 69 6e 64 65 78 20 69 6e 20 7a 50 61 74 74 65 72  index in zPatter
394f9 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74 72 69  n */.  int iStri
394fa 6e 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f  ng = 0;        /
394fb 2a 20 43 75 72 72 65 6e 74 20 62 79 74 65 20 69  * Current byte i
394fc 6e 64 65 78 20 69 6e 20 7a 53 74 72 69 6e 67 20  ndex in zString 
394fd 2a 2f 0a 0a 20 20 69 6e 74 20 70 72 65 76 45 73  */..  int prevEs
394fe 63 61 70 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a  cape = 0;     /*
394ff 20 54 72 75 65 20 69 66 20 74 68 65 20 70 72 65   True if the pre
39500 76 69 6f 75 73 20 63 68 61 72 61 63 74 65 72 20  vious character 
39501 77 61 73 20 75 45 73 63 20 2a 2f 0a 0a 20 20 77  was uEsc */..  w
39502 68 69 6c 65 28 20 7a 50 61 74 74 65 72 6e 5b 69  hile( zPattern[i
39503 50 61 74 74 65 72 6e 5d 21 3d 30 20 29 7b 0a 0a  Pattern]!=0 ){..
39504 20 20 20 20 2f 2a 20 52 65 61 64 20 28 61 6e 64      /* Read (and
39505 20 63 6f 6e 73 75 6d 65 29 20 74 68 65 20 6e 65   consume) the ne
39506 78 74 20 63 68 61 72 61 63 74 65 72 20 66 72 6f  xt character fro
39507 6d 20 74 68 65 20 69 6e 70 75 74 20 70 61 74 74  m the input patt
39508 65 72 6e 2e 20 2a 2f 0a 20 20 20 20 55 43 68 61  ern. */.    UCha
39509 72 33 32 20 75 50 61 74 74 65 72 6e 3b 0a 20 20  r32 uPattern;.  
3950a 20 20 55 38 5f 4e 45 58 54 5f 55 4e 53 41 46 45    U8_NEXT_UNSAFE
3950b 28 7a 50 61 74 74 65 72 6e 2c 20 69 50 61 74 74  (zPattern, iPatt
3950c 65 72 6e 2c 20 75 50 61 74 74 65 72 6e 29 3b 0a  ern, uPattern);.
3950d 20 20 20 20 61 73 73 65 72 74 28 75 50 61 74 74      assert(uPatt
3950e 65 72 6e 21 3d 30 29 3b 0a 0a 20 20 20 20 2f 2a  ern!=0);..    /*
3950f 20 54 68 65 72 65 20 61 72 65 20 6e 6f 77 20 34   There are now 4
39510 20 70 6f 73 73 69 62 69 6c 69 74 69 65 73 3a 0a   possibilities:.
39511 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
39512 20 20 31 2e 20 75 50 61 74 74 65 72 6e 20 69 73    1. uPattern is
39513 20 61 6e 20 75 6e 65 73 63 61 70 65 64 20 6d 61   an unescaped ma
39514 74 63 68 2d 61 6c 6c 20 63 68 61 72 61 63 74 65  tch-all characte
39515 72 20 22 25 22 2c 0a 20 20 20 20 2a 2a 20 20 20  r "%",.    **   
39516 20 20 32 2e 20 75 50 61 74 74 65 72 6e 20 69 73    2. uPattern is
39517 20 61 6e 20 75 6e 65 73 63 61 70 65 64 20 6d 61   an unescaped ma
39518 74 63 68 2d 6f 6e 65 20 63 68 61 72 61 63 74 65  tch-one characte
39519 72 20 22 5f 22 2c 0a 20 20 20 20 2a 2a 20 20 20  r "_",.    **   
3951a 20 20 33 2e 20 75 50 61 74 74 65 72 6e 20 69 73    3. uPattern is
3951b 20 61 6e 20 75 6e 65 73 63 61 70 65 64 20 65 73   an unescaped es
3951c 63 61 70 65 20 63 68 61 72 61 63 74 65 72 2c 20  cape character, 
3951d 6f 72 0a 20 20 20 20 2a 2a 20 20 20 20 20 34 2e  or.    **     4.
3951e 20 75 50 61 74 74 65 72 6e 20 69 73 20 74 6f 20   uPattern is to 
3951f 62 65 20 68 61 6e 64 6c 65 64 20 61 73 20 61 6e  be handled as an
39520 20 6f 72 64 69 6e 61 72 79 20 63 68 61 72 61 63   ordinary charac
39521 74 65 72 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ter.    */.    i
39522 66 28 20 21 70 72 65 76 45 73 63 61 70 65 20 26  f( !prevEscape &
39523 26 20 75 50 61 74 74 65 72 6e 3d 3d 4d 41 54 43  & uPattern==MATC
39524 48 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 20 20 2f  H_ALL ){.      /
39525 2a 20 43 61 73 65 20 31 2e 20 2a 2f 0a 20 20 20  * Case 1. */.   
39526 20 20 20 75 69 6e 74 38 5f 74 20 63 3b 0a 0a 20     uint8_t c;.. 
39527 20 20 20 20 20 2f 2a 20 53 6b 69 70 20 61 6e 79       /* Skip any
39528 20 4d 41 54 43 48 5f 41 4c 4c 20 6f 72 20 4d 41   MATCH_ALL or MA
39529 54 43 48 5f 4f 4e 45 20 63 68 61 72 61 63 74 65  TCH_ONE characte
3952a 72 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 61  rs that follow a
3952b 0a 20 20 20 20 20 20 2a 2a 20 4d 41 54 43 48 5f  .      ** MATCH_
3952c 41 4c 4c 2e 20 46 6f 72 20 65 61 63 68 20 4d 41  ALL. For each MA
3952d 54 43 48 5f 4f 4e 45 2c 20 73 6b 69 70 20 6f 6e  TCH_ONE, skip on
3952e 65 20 63 68 61 72 61 63 74 65 72 20 69 6e 20 74  e character in t
3952f 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 74 65 73  he .      ** tes
39530 74 20 73 74 72 69 6e 67 2e 0a 20 20 20 20 20 20  t string..      
39531 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  */.      while( 
39532 28 63 3d 7a 50 61 74 74 65 72 6e 5b 69 50 61 74  (c=zPattern[iPat
39533 74 65 72 6e 5d 29 20 3d 3d 20 4d 41 54 43 48 5f  tern]) == MATCH_
39534 41 4c 4c 20 7c 7c 20 63 20 3d 3d 20 4d 41 54 43  ALL || c == MATC
39535 48 5f 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 20  H_ONE ){.       
39536 20 69 66 28 20 63 3d 3d 4d 41 54 43 48 5f 4f 4e   if( c==MATCH_ON
39537 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  E ){.          i
39538 66 28 20 7a 53 74 72 69 6e 67 5b 69 53 74 72 69  f( zString[iStri
39539 6e 67 5d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ng]==0 ) return 
3953a 30 3b 0a 20 20 20 20 20 20 20 20 20 20 55 38 5f  0;.          U8_
3953b 46 57 44 5f 31 5f 55 4e 53 41 46 45 28 7a 53 74  FWD_1_UNSAFE(zSt
3953c 72 69 6e 67 2c 20 69 53 74 72 69 6e 67 29 3b 0a  ring, iString);.
3953d 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
3953e 20 20 69 50 61 74 74 65 72 6e 2b 2b 3b 0a 20 20    iPattern++;.  
3953f 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28      }..      if(
39540 20 7a 50 61 74 74 65 72 6e 5b 69 50 61 74 74 65   zPattern[iPatte
39541 72 6e 5d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  rn]==0 ) return 
39542 31 3b 0a 0a 20 20 20 20 20 20 77 68 69 6c 65 28  1;..      while(
39543 20 7a 53 74 72 69 6e 67 5b 69 53 74 72 69 6e 67   zString[iString
39544 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ] ){.        if(
39545 20 69 63 75 4c 69 6b 65 43 6f 6d 70 61 72 65 28   icuLikeCompare(
39546 26 7a 50 61 74 74 65 72 6e 5b 69 50 61 74 74 65  &zPattern[iPatte
39547 72 6e 5d 2c 20 26 7a 53 74 72 69 6e 67 5b 69 53  rn], &zString[iS
39548 74 72 69 6e 67 5d 2c 20 75 45 73 63 29 20 29 7b  tring], uEsc) ){
39549 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
3954a 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  n 1;.        }. 
3954b 20 20 20 20 20 20 20 55 38 5f 46 57 44 5f 31 5f         U8_FWD_1_
3954c 55 4e 53 41 46 45 28 7a 53 74 72 69 6e 67 2c 20  UNSAFE(zString, 
3954d 69 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20  iString);.      
3954e 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  }.      return 0
3954f 3b 0a 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  ;..    }else if(
39550 20 21 70 72 65 76 45 73 63 61 70 65 20 26 26 20   !prevEscape && 
39551 75 50 61 74 74 65 72 6e 3d 3d 4d 41 54 43 48 5f  uPattern==MATCH_
39552 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ONE ){.      /* 
39553 43 61 73 65 20 32 2e 20 2a 2f 0a 20 20 20 20 20  Case 2. */.     
39554 20 69 66 28 20 7a 53 74 72 69 6e 67 5b 69 53 74   if( zString[iSt
39555 72 69 6e 67 5d 3d 3d 30 20 29 20 72 65 74 75 72  ring]==0 ) retur
39556 6e 20 30 3b 0a 20 20 20 20 20 20 55 38 5f 46 57  n 0;.      U8_FW
39557 44 5f 31 5f 55 4e 53 41 46 45 28 7a 53 74 72 69  D_1_UNSAFE(zStri
39558 6e 67 2c 20 69 53 74 72 69 6e 67 29 3b 0a 0a 20  ng, iString);.. 
39559 20 20 20 7d 65 6c 73 65 20 69 66 28 20 21 70 72     }else if( !pr
3955a 65 76 45 73 63 61 70 65 20 26 26 20 75 50 61 74  evEscape && uPat
3955b 74 65 72 6e 3d 3d 75 45 73 63 29 7b 0a 20 20 20  tern==uEsc){.   
3955c 20 20 20 2f 2a 20 43 61 73 65 20 33 2e 20 2a 2f     /* Case 3. */
3955d 0a 20 20 20 20 20 20 70 72 65 76 45 73 63 61 70  .      prevEscap
3955e 65 20 3d 20 31 3b 0a 0a 20 20 20 20 7d 65 6c 73  e = 1;..    }els
3955f 65 7b 0a 20 20 20 20 20 20 2f 2a 20 43 61 73 65  e{.      /* Case
39560 20 34 2e 20 2a 2f 0a 20 20 20 20 20 20 55 43 68   4. */.      UCh
39561 61 72 33 32 20 75 53 74 72 69 6e 67 3b 0a 20 20  ar32 uString;.  
39562 20 20 20 20 55 38 5f 4e 45 58 54 5f 55 4e 53 41      U8_NEXT_UNSA
39563 46 45 28 7a 53 74 72 69 6e 67 2c 20 69 53 74 72  FE(zString, iStr
39564 69 6e 67 2c 20 75 53 74 72 69 6e 67 29 3b 0a 20  ing, uString);. 
39565 20 20 20 20 20 75 53 74 72 69 6e 67 20 3d 20 75       uString = u
39566 5f 66 6f 6c 64 43 61 73 65 28 75 53 74 72 69 6e  _foldCase(uStrin
39567 67 2c 20 55 5f 46 4f 4c 44 5f 43 41 53 45 5f 44  g, U_FOLD_CASE_D
39568 45 46 41 55 4c 54 29 3b 0a 20 20 20 20 20 20 75  EFAULT);.      u
39569 50 61 74 74 65 72 6e 20 3d 20 75 5f 66 6f 6c 64  Pattern = u_fold
3956a 43 61 73 65 28 75 50 61 74 74 65 72 6e 2c 20 55  Case(uPattern, U
3956b 5f 46 4f 4c 44 5f 43 41 53 45 5f 44 45 46 41 55  _FOLD_CASE_DEFAU
3956c 4c 54 29 3b 0a 20 20 20 20 20 20 69 66 28 20 75  LT);.      if( u
3956d 53 74 72 69 6e 67 21 3d 75 50 61 74 74 65 72 6e  String!=uPattern
3956e 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
3956f 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 0;.      }.  
39570 20 20 20 20 70 72 65 76 45 73 63 61 70 65 20 3d      prevEscape =
39571 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20   0;.    }.  }.. 
39572 20 72 65 74 75 72 6e 20 7a 53 74 72 69 6e 67 5b   return zString[
39573 69 53 74 72 69 6e 67 5d 3d 3d 30 3b 0a 7d 0a 0a  iString]==0;.}..
39574 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
39575 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 6b 65  tion of the like
39576 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e  () SQL function.
39577 20 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20    This function 
39578 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 74 68  implements.** th
39579 65 20 62 75 69 6c 64 2d 69 6e 20 4c 49 4b 45 20  e build-in LIKE 
3957a 6f 70 65 72 61 74 6f 72 2e 20 20 54 68 65 20 66  operator.  The f
3957b 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
3957c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73   the function is
3957d 20 74 68 65 0a 2a 2a 20 70 61 74 74 65 72 6e 20   the.** pattern 
3957e 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 61  and the second a
3957f 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 73  rgument is the s
39580 74 72 69 6e 67 2e 20 20 53 6f 2c 20 74 68 65 20  tring.  So, the 
39581 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 3a 0a  SQL statements:.
39582 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 41 20 4c 49  **.**       A LI
39583 4b 45 20 42 0a 2a 2a 0a 2a 2a 20 69 73 20 69 6d  KE B.**.** is im
39584 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 6c 69 6b  plemented as lik
39585 65 28 42 2c 20 41 29 2e 20 49 66 20 74 68 65 72  e(B, A). If ther
39586 65 20 69 73 20 61 6e 20 65 73 63 61 70 65 20 63  e is an escape c
39587 68 61 72 61 63 74 65 72 20 45 2c 20 0a 2a 2a 0a  haracter E, .**.
39588 2a 2a 20 20 20 20 20 20 20 41 20 4c 49 4b 45 20  **       A LIKE 
39589 42 20 45 53 43 41 50 45 20 45 0a 2a 2a 0a 2a 2a  B ESCAPE E.**.**
3958a 20 69 73 20 6d 61 70 70 65 64 20 74 6f 20 6c 69   is mapped to li
3958b 6b 65 28 42 2c 20 41 2c 20 45 29 2e 0a 2a 2f 0a  ke(B, A, E)..*/.
3958c 73 74 61 74 69 63 20 76 6f 69 64 20 69 63 75 4c  static void icuL
3958d 69 6b 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  ikeFunc(.  sqlit
3958e 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
3958f 65 78 74 2c 20 0a 20 20 69 6e 74 20 61 72 67 63  ext, .  int argc
39590 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  , .  sqlite3_val
39591 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63  ue **argv.){.  c
39592 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
39593 61 72 20 2a 7a 41 20 3d 20 73 71 6c 69 74 65 33  ar *zA = sqlite3
39594 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
39595 5b 30 5d 29 3b 0a 20 20 63 6f 6e 73 74 20 75 6e  [0]);.  const un
39596 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 42 20  signed char *zB 
39597 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
39598 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20  text(argv[1]);. 
39599 20 55 43 68 61 72 33 32 20 75 45 73 63 20 3d 20   UChar32 uEsc = 
3959a 30 3b 0a 0a 20 20 2f 2a 20 4c 69 6d 69 74 20 74  0;..  /* Limit t
3959b 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65  he length of the
3959c 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 20 70 61   LIKE or GLOB pa
3959d 74 74 65 72 6e 20 74 6f 20 61 76 6f 69 64 20 70  ttern to avoid p
3959e 72 6f 62 6c 65 6d 73 0a 20 20 2a 2a 20 6f 66 20  roblems.  ** of 
3959f 64 65 65 70 20 72 65 63 75 72 73 69 6f 6e 20 61  deep recursion a
395a0 6e 64 20 4e 2a 4e 20 62 65 68 61 76 69 6f 72 20  nd N*N behavior 
395a1 69 6e 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 72  in patternCompar
395a2 65 28 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  e()..  */.  if( 
395a3 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
395a4 74 65 73 28 61 72 67 76 5b 30 5d 29 3e 53 51 4c  tes(argv[0])>SQL
395a5 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54  ITE_MAX_LIKE_PAT
395a6 54 45 52 4e 5f 4c 45 4e 47 54 48 20 29 7b 0a 20  TERN_LENGTH ){. 
395a7 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
395a8 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c  t_error(context,
395a9 20 22 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 20 70   "LIKE or GLOB p
395aa 61 74 74 65 72 6e 20 74 6f 6f 20 63 6f 6d 70 6c  attern too compl
395ab 65 78 22 2c 20 2d 31 29 3b 0a 20 20 20 20 72 65  ex", -1);.    re
395ac 74 75 72 6e 3b 0a 20 20 7d 0a 0a 0a 20 20 69 66  turn;.  }...  if
395ad 28 20 61 72 67 63 3d 3d 33 20 29 7b 0a 20 20 20  ( argc==3 ){.   
395ae 20 2f 2a 20 54 68 65 20 65 73 63 61 70 65 20 63   /* The escape c
395af 68 61 72 61 63 74 65 72 20 73 74 72 69 6e 67 20  haracter string 
395b0 6d 75 73 74 20 63 6f 6e 73 69 73 74 20 6f 66 20  must consist of 
395b1 61 20 73 69 6e 67 6c 65 20 55 54 46 2d 38 20 63  a single UTF-8 c
395b2 68 61 72 61 63 74 65 72 2e 0a 20 20 20 20 2a 2a  haracter..    **
395b3 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 75   Otherwise, retu
395b4 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 20  rn an error..   
395b5 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 45 3d 20   */.    int nE= 
395b6 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
395b7 74 65 73 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20  tes(argv[2]);.  
395b8 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
395b9 20 63 68 61 72 20 2a 7a 45 20 3d 20 73 71 6c 69   char *zE = sqli
395ba 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
395bb 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 69 6e 74  rgv[2]);.    int
395bc 20 69 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20   i = 0;.    if( 
395bd 7a 45 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  zE==0 ) return;.
395be 20 20 20 20 55 38 5f 4e 45 58 54 28 7a 45 2c 20      U8_NEXT(zE, 
395bf 69 2c 20 6e 45 2c 20 75 45 73 63 29 3b 0a 20 20  i, nE, uEsc);.  
395c0 20 20 69 66 28 20 69 21 3d 6e 45 29 7b 0a 20 20    if( i!=nE){.  
395c1 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
395c2 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74  lt_error(context
395c3 2c 20 0a 20 20 20 20 20 20 20 20 20 20 22 45 53  , .          "ES
395c4 43 41 50 45 20 65 78 70 72 65 73 73 69 6f 6e 20  CAPE expression 
395c5 6d 75 73 74 20 62 65 20 61 20 73 69 6e 67 6c 65  must be a single
395c6 20 63 68 61 72 61 63 74 65 72 22 2c 20 2d 31 29   character", -1)
395c7 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  ;.      return;.
395c8 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28      }.  }..  if(
395c9 20 7a 41 20 26 26 20 7a 42 20 29 7b 0a 20 20 20   zA && zB ){.   
395ca 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
395cb 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 69 63 75  int(context, icu
395cc 4c 69 6b 65 43 6f 6d 70 61 72 65 28 7a 41 2c 20  LikeCompare(zA, 
395cd 7a 42 2c 20 75 45 73 63 29 29 3b 0a 20 20 7d 0a  zB, uEsc));.  }.
395ce 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
395cf 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
395d0 20 77 68 65 6e 20 61 6e 20 49 43 55 20 66 75 6e   when an ICU fun
395d1 63 74 69 6f 6e 20 63 61 6c 6c 65 64 20 66 72 6f  ction called fro
395d2 6d 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20  m within.** the 
395d3 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
395d4 66 20 61 6e 20 53 51 4c 20 73 63 61 6c 61 72 20  f an SQL scalar 
395d5 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
395d6 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a   an error..**.**
395d7 20 54 68 65 20 73 63 61 6c 61 72 20 66 75 6e 63   The scalar func
395d8 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 70 61 73  tion context pas
395d9 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74  sed as the first
395da 20 61 72 67 75 6d 65 6e 74 20 69 73 20 0a 2a 2a   argument is .**
395db 20 6c 6f 61 64 65 64 20 77 69 74 68 20 61 6e 20   loaded with an 
395dc 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 62 61  error message ba
395dd 73 65 64 20 6f 6e 20 74 68 65 20 66 6f 6c 6c 6f  sed on the follo
395de 77 69 6e 67 20 74 77 6f 20 61 72 67 73 2e 0a 2a  wing two args..*
395df 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 63  /.static void ic
395e0 75 46 75 6e 63 74 69 6f 6e 45 72 72 6f 72 28 0a  uFunctionError(.
395e1 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
395e2 74 20 2a 70 43 74 78 2c 20 20 20 20 20 20 20 2f  t *pCtx,       /
395e3 2a 20 53 51 4c 69 74 65 20 73 63 61 6c 61 72 20  * SQLite scalar 
395e4 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 78 74  function context
395e5 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
395e6 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20   *zName,        
395e7 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 49 43     /* Name of IC
395e8 55 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20  U function that 
395e9 66 61 69 6c 65 64 20 2a 2f 0a 20 20 55 45 72 72  failed */.  UErr
395ea 6f 72 43 6f 64 65 20 65 20 20 20 20 20 20 20 20  orCode e        
395eb 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f           /* Erro
395ec 72 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 20  r code returned 
395ed 62 79 20 49 43 55 20 66 75 6e 63 74 69 6f 6e 20  by ICU function 
395ee 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 7a 42 75  */.){.  char zBu
395ef 66 5b 31 32 38 5d 3b 0a 20 20 73 71 6c 69 74 65  f[128];.  sqlite
395f0 33 5f 73 6e 70 72 69 6e 74 66 28 31 32 38 2c 20  3_snprintf(128, 
395f1 7a 42 75 66 2c 20 22 49 43 55 20 65 72 72 6f 72  zBuf, "ICU error
395f2 3a 20 25 73 28 29 3a 20 25 73 22 2c 20 7a 4e 61  : %s(): %s", zNa
395f3 6d 65 2c 20 75 5f 65 72 72 6f 72 4e 61 6d 65 28  me, u_errorName(
395f4 65 29 29 3b 0a 20 20 7a 42 75 66 5b 31 32 37 5d  e));.  zBuf[127]
395f5 20 3d 20 27 5c 30 27 3b 0a 20 20 73 71 6c 69 74   = '\0';.  sqlit
395f6 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
395f7 70 43 74 78 2c 20 7a 42 75 66 2c 20 2d 31 29 3b  pCtx, zBuf, -1);
395f8 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69  .}../*.** Functi
395f9 6f 6e 20 74 6f 20 64 65 6c 65 74 65 20 63 6f 6d  on to delete com
395fa 70 69 6c 65 64 20 72 65 67 65 78 70 20 6f 62 6a  piled regexp obj
395fb 65 63 74 73 2e 20 52 65 67 69 73 74 65 72 65 64  ects. Registered
395fc 20 61 73 0a 2a 2a 20 61 20 64 65 73 74 72 75 63   as.** a destruc
395fd 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 77 69 74  tor function wit
395fe 68 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75  h sqlite3_set_au
395ff 78 64 61 74 61 28 29 2e 0a 2a 2f 0a 73 74 61 74  xdata()..*/.stat
39600 69 63 20 76 6f 69 64 20 69 63 75 52 65 67 65 78  ic void icuRegex
39601 70 44 65 6c 65 74 65 28 76 6f 69 64 20 2a 70 29  pDelete(void *p)
39602 7b 0a 20 20 55 52 65 67 75 6c 61 72 45 78 70 72  {.  URegularExpr
39603 65 73 73 69 6f 6e 20 2a 70 45 78 70 72 20 3d 20  ession *pExpr = 
39604 28 55 52 65 67 75 6c 61 72 45 78 70 72 65 73 73  (URegularExpress
39605 69 6f 6e 20 2a 29 70 3b 0a 20 20 75 72 65 67 65  ion *)p;.  urege
39606 78 5f 63 6c 6f 73 65 28 70 45 78 70 72 29 3b 0a  x_close(pExpr);.
39607 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  }../*.** Impleme
39608 6e 74 61 74 69 6f 6e 20 6f 66 20 53 51 4c 69 74  ntation of SQLit
39609 65 20 52 45 47 45 58 50 20 6f 70 65 72 61 74 6f  e REGEXP operato
3960a 72 2e 20 54 68 69 73 20 73 63 61 6c 61 72 20 66  r. This scalar f
3960b 75 6e 63 74 69 6f 6e 20 74 61 6b 65 73 0a 2a 2a  unction takes.**
3960c 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 2e 20   two arguments. 
3960d 54 68 65 20 66 69 72 73 74 20 69 73 20 61 20 72  The first is a r
3960e 65 67 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f  egular expressio
3960f 6e 20 70 61 74 74 65 72 6e 20 74 6f 20 63 6f 6d  n pattern to com
39610 70 69 6c 65 0a 2a 2a 20 74 68 65 20 73 65 63 6f  pile.** the seco
39611 6e 64 20 69 73 20 61 20 73 74 72 69 6e 67 20 74  nd is a string t
39612 6f 20 6d 61 74 63 68 20 61 67 61 69 6e 73 74 20  o match against 
39613 74 68 61 74 20 70 61 74 74 65 72 6e 2e 20 49 66  that pattern. If
39614 20 65 69 74 68 65 72 20 0a 2a 2a 20 61 72 67 75   either .** argu
39615 6d 65 6e 74 20 69 73 20 61 6e 20 53 51 4c 20 4e  ment is an SQL N
39616 55 4c 4c 2c 20 74 68 65 6e 20 4e 55 4c 4c 20 49  ULL, then NULL I
39617 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65  s returned. Othe
39618 72 77 69 73 65 2c 20 74 68 65 20 72 65 73 75 6c  rwise, the resul
39619 74 0a 2a 2a 20 69 73 20 31 20 69 66 20 74 68 65  t.** is 1 if the
3961a 20 73 74 72 69 6e 67 20 6d 61 74 63 68 65 73 20   string matches 
3961b 74 68 65 20 70 61 74 74 65 72 6e 2c 20 6f 72 20  the pattern, or 
3961c 30 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a  0 otherwise..**.
3961d 2a 2a 20 53 51 4c 69 74 65 20 6d 61 70 73 20 74  ** SQLite maps t
3961e 68 65 20 72 65 67 65 78 70 28 29 20 66 75 6e 63  he regexp() func
3961f 74 69 6f 6e 20 74 6f 20 74 68 65 20 72 65 67 65  tion to the rege
39620 78 70 28 29 20 6f 70 65 72 61 74 6f 72 20 73 75  xp() operator su
39621 63 68 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 66  ch.** that the f
39622 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 61 72 65  ollowing two are
39623 20 65 71 75 69 76 61 6c 65 6e 74 3a 0a 2a 2a 0a   equivalent:.**.
39624 2a 2a 20 20 20 20 20 7a 53 74 72 69 6e 67 20 52  **     zString R
39625 45 47 45 58 50 20 7a 50 61 74 74 65 72 6e 0a 2a  EGEXP zPattern.*
39626 2a 20 20 20 20 20 72 65 67 65 78 70 28 7a 50 61  *     regexp(zPa
39627 74 74 65 72 6e 2c 20 7a 53 74 72 69 6e 67 29 0a  ttern, zString).
39628 2a 2a 0a 2a 2a 20 55 73 65 73 20 74 68 65 20 66  **.** Uses the f
39629 6f 6c 6c 6f 77 69 6e 67 20 49 43 55 20 72 65 67  ollowing ICU reg
3962a 65 78 70 20 41 50 49 73 3a 0a 2a 2a 0a 2a 2a 20  exp APIs:.**.** 
3962b 20 20 20 20 75 72 65 67 65 78 5f 6f 70 65 6e 28      uregex_open(
3962c 29 0a 2a 2a 20 20 20 20 20 75 72 65 67 65 78 5f  ).**     uregex_
3962d 6d 61 74 63 68 65 73 28 29 0a 2a 2a 20 20 20 20  matches().**    
3962e 20 75 72 65 67 65 78 5f 63 6c 6f 73 65 28 29 0a   uregex_close().
3962f 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69  */.static void i
39630 63 75 52 65 67 65 78 70 46 75 6e 63 28 73 71 6c  cuRegexpFunc(sql
39631 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 2c  ite3_context *p,
39632 20 69 6e 74 20 6e 41 72 67 2c 20 73 71 6c 69 74   int nArg, sqlit
39633 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 41 72 67  e3_value **apArg
39634 29 7b 0a 20 20 55 45 72 72 6f 72 43 6f 64 65 20  ){.  UErrorCode 
39635 73 74 61 74 75 73 20 3d 20 55 5f 5a 45 52 4f 5f  status = U_ZERO_
39636 45 52 52 4f 52 3b 0a 20 20 55 52 65 67 75 6c 61  ERROR;.  URegula
39637 72 45 78 70 72 65 73 73 69 6f 6e 20 2a 70 45 78  rExpression *pEx
39638 70 72 3b 0a 20 20 55 42 6f 6f 6c 20 72 65 73 3b  pr;.  UBool res;
39639 0a 20 20 63 6f 6e 73 74 20 55 43 68 61 72 20 2a  .  const UChar *
3963a 7a 53 74 72 69 6e 67 20 3d 20 73 71 6c 69 74 65  zString = sqlite
3963b 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 61  3_value_text16(a
3963c 70 41 72 67 5b 31 5d 29 3b 0a 0a 20 20 2f 2a 20  pArg[1]);..  /* 
3963d 49 66 20 74 68 65 20 6c 65 66 74 20 68 61 6e 64  If the left hand
3963e 20 73 69 64 65 20 6f 66 20 74 68 65 20 72 65 67   side of the reg
3963f 65 78 70 20 6f 70 65 72 61 74 6f 72 20 69 73 20  exp operator is 
39640 4e 55 4c 4c 2c 20 0a 20 20 2a 2a 20 74 68 65 6e  NULL, .  ** then
39641 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 61   the result is a
39642 6c 73 6f 20 4e 55 4c 4c 2e 20 0a 20 20 2a 2f 0a  lso NULL. .  */.
39643 20 20 69 66 28 20 21 7a 53 74 72 69 6e 67 20 29    if( !zString )
39644 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
39645 7d 0a 0a 20 20 70 45 78 70 72 20 3d 20 73 71 6c  }..  pExpr = sql
39646 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61  ite3_get_auxdata
39647 28 70 2c 20 30 29 3b 0a 20 20 69 66 28 20 21 70  (p, 0);.  if( !p
39648 45 78 70 72 20 29 7b 0a 20 20 20 20 63 6f 6e 73  Expr ){.    cons
39649 74 20 55 43 68 61 72 20 2a 7a 50 61 74 74 65 72  t UChar *zPatter
3964a 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  n = sqlite3_valu
3964b 65 5f 74 65 78 74 31 36 28 61 70 41 72 67 5b 30  e_text16(apArg[0
3964c 5d 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 50 61  ]);.    if( !zPa
3964d 74 74 65 72 6e 20 29 7b 0a 20 20 20 20 20 20 72  ttern ){.      r
3964e 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20  eturn;.    }.   
3964f 20 70 45 78 70 72 20 3d 20 75 72 65 67 65 78 5f   pExpr = uregex_
39650 6f 70 65 6e 28 7a 50 61 74 74 65 72 6e 2c 20 2d  open(zPattern, -
39651 31 2c 20 30 2c 20 30 2c 20 26 73 74 61 74 75 73  1, 0, 0, &status
39652 29 3b 0a 0a 20 20 20 20 69 66 28 20 55 5f 53 55  );..    if( U_SU
39653 43 43 45 53 53 28 73 74 61 74 75 73 29 20 29 7b  CCESS(status) ){
39654 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
39655 65 74 5f 61 75 78 64 61 74 61 28 70 2c 20 30 2c  et_auxdata(p, 0,
39656 20 70 45 78 70 72 2c 20 69 63 75 52 65 67 65 78   pExpr, icuRegex
39657 70 44 65 6c 65 74 65 29 3b 0a 20 20 20 20 7d 65  pDelete);.    }e
39658 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72  lse{.      asser
39659 74 28 21 70 45 78 70 72 29 3b 0a 20 20 20 20 20  t(!pExpr);.     
3965a 20 69 63 75 46 75 6e 63 74 69 6f 6e 45 72 72 6f   icuFunctionErro
3965b 72 28 70 2c 20 22 75 72 65 67 65 78 5f 6f 70 65  r(p, "uregex_ope
3965c 6e 22 2c 20 73 74 61 74 75 73 29 3b 0a 20 20 20  n", status);.   
3965d 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
3965e 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 66 69  .  }..  /* Confi
3965f 67 75 72 65 20 74 68 65 20 74 65 78 74 20 74 68  gure the text th
39660 61 74 20 74 68 65 20 72 65 67 75 6c 61 72 20 65  at the regular e
39661 78 70 72 65 73 73 69 6f 6e 20 6f 70 65 72 61 74  xpression operat
39662 65 73 20 6f 6e 2e 20 2a 2f 0a 20 20 75 72 65 67  es on. */.  ureg
39663 65 78 5f 73 65 74 54 65 78 74 28 70 45 78 70 72  ex_setText(pExpr
39664 2c 20 7a 53 74 72 69 6e 67 2c 20 2d 31 2c 20 26  , zString, -1, &
39665 73 74 61 74 75 73 29 3b 0a 20 20 69 66 28 20 21  status);.  if( !
39666 55 5f 53 55 43 43 45 53 53 28 73 74 61 74 75 73  U_SUCCESS(status
39667 29 20 29 7b 0a 20 20 20 20 69 63 75 46 75 6e 63  ) ){.    icuFunc
39668 74 69 6f 6e 45 72 72 6f 72 28 70 2c 20 22 75 72  tionError(p, "ur
39669 65 67 65 78 5f 73 65 74 54 65 78 74 22 2c 20 73  egex_setText", s
3966a 74 61 74 75 73 29 3b 0a 20 20 20 20 72 65 74 75  tatus);.    retu
3966b 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 74  rn;.  }..  /* At
3966c 74 65 6d 70 74 20 74 68 65 20 6d 61 74 63 68 20  tempt the match 
3966d 2a 2f 0a 20 20 72 65 73 20 3d 20 75 72 65 67 65  */.  res = urege
3966e 78 5f 6d 61 74 63 68 65 73 28 70 45 78 70 72 2c  x_matches(pExpr,
3966f 20 30 2c 20 26 73 74 61 74 75 73 29 3b 0a 20 20   0, &status);.  
39670 69 66 28 20 21 55 5f 53 55 43 43 45 53 53 28 73  if( !U_SUCCESS(s
39671 74 61 74 75 73 29 20 29 7b 0a 20 20 20 20 69 63  tatus) ){.    ic
39672 75 46 75 6e 63 74 69 6f 6e 45 72 72 6f 72 28 70  uFunctionError(p
39673 2c 20 22 75 72 65 67 65 78 5f 6d 61 74 63 68 65  , "uregex_matche
39674 73 22 2c 20 73 74 61 74 75 73 29 3b 0a 20 20 20  s", status);.   
39675 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20   return;.  }..  
39676 2f 2a 20 53 65 74 20 74 68 65 20 74 65 78 74 20  /* Set the text 
39677 74 68 61 74 20 74 68 65 20 72 65 67 75 6c 61 72  that the regular
39678 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 70 65 72   expression oper
39679 61 74 65 73 20 6f 6e 20 74 6f 20 61 20 4e 55 4c  ates on to a NUL
3967a 4c 0a 20 20 2a 2a 20 70 6f 69 6e 74 65 72 2e 20  L.  ** pointer. 
3967b 54 68 69 73 20 69 73 20 6e 6f 74 20 72 65 61 6c  This is not real
3967c 6c 79 20 6e 65 63 65 73 73 61 72 79 2c 20 62 75  ly necessary, bu
3967d 74 20 69 74 20 69 73 20 74 69 64 69 65 72 20 74  t it is tidier t
3967e 68 61 6e 20 0a 20 20 2a 2a 20 6c 65 61 76 69 6e  han .  ** leavin
3967f 67 20 74 68 65 20 72 65 67 75 6c 61 72 20 65 78  g the regular ex
39680 70 72 65 73 73 69 6f 6e 20 6f 62 6a 65 63 74 20  pression object 
39681 63 6f 6e 66 69 67 75 72 65 64 20 77 69 74 68 20  configured with 
39682 61 6e 20 69 6e 76 61 6c 69 64 0a 20 20 2a 2a 20  an invalid.  ** 
39683 70 6f 69 6e 74 65 72 20 61 66 74 65 72 20 74 68  pointer after th
39684 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  is function retu
39685 72 6e 73 2e 0a 20 20 2a 2f 0a 20 20 75 72 65 67  rns..  */.  ureg
39686 65 78 5f 73 65 74 54 65 78 74 28 70 45 78 70 72  ex_setText(pExpr
39687 2c 20 30 2c 20 30 2c 20 26 73 74 61 74 75 73 29  , 0, 0, &status)
39688 3b 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 31  ;..  /* Return 1
39689 20 6f 72 20 30 2e 20 2a 2f 0a 20 20 73 71 6c 69   or 0. */.  sqli
3968a 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 70  te3_result_int(p
3968b 2c 20 72 65 73 20 3f 20 31 20 3a 20 30 29 3b 0a  , res ? 1 : 0);.
3968c 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  }../*.** Impleme
3968d 6e 74 61 74 69 6f 6e 73 20 6f 66 20 73 63 61 6c  ntations of scal
3968e 61 72 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72  ar functions for
3968f 20 63 61 73 65 20 6d 61 70 70 69 6e 67 20 2d 20   case mapping - 
39690 75 70 70 65 72 28 29 20 61 6e 64 20 0a 2a 2a 20  upper() and .** 
39691 6c 6f 77 65 72 28 29 2e 20 46 75 6e 63 74 69 6f  lower(). Functio
39692 6e 20 75 70 70 65 72 28 29 20 63 6f 6e 76 65 72  n upper() conver
39693 74 73 20 69 74 73 20 69 6e 70 75 74 20 74 6f 20  ts its input to 
39694 75 70 70 65 72 2d 63 61 73 65 20 28 41 42 43 29  upper-case (ABC)
39695 2e 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 6c 6f  ..** Function lo
39696 77 65 72 28 29 20 63 6f 6e 76 65 72 74 73 20 74  wer() converts t
39697 6f 20 6c 6f 77 65 72 2d 63 61 73 65 20 28 61 62  o lower-case (ab
39698 63 29 2e 0a 2a 2a 0a 2a 2a 20 49 43 55 20 70 72  c)..**.** ICU pr
39699 6f 76 69 64 65 73 20 74 77 6f 20 74 79 70 65 73  ovides two types
3969a 20 6f 66 20 63 61 73 65 20 6d 61 70 70 69 6e 67   of case mapping
3969b 2c 20 22 67 65 6e 65 72 61 6c 22 20 63 61 73 65  , "general" case
3969c 20 6d 61 70 70 69 6e 67 20 61 6e 64 0a 2a 2a 20   mapping and.** 
3969d 22 6c 61 6e 67 75 61 67 65 20 73 70 65 63 69 66  "language specif
3969e 69 63 22 2e 20 52 65 66 65 72 20 74 6f 20 49 43  ic". Refer to IC
3969f 55 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20  U documentation 
396a0 66 6f 72 20 74 68 65 20 64 69 66 66 65 72 65 6e  for the differen
396a1 63 65 73 0a 2a 2a 20 62 65 74 77 65 65 6e 20 74  ces.** between t
396a2 68 65 20 74 77 6f 2e 0a 2a 2a 0a 2a 2a 20 54 6f  he two..**.** To
396a3 20 75 74 69 6c 69 73 65 20 22 67 65 6e 65 72 61   utilise "genera
396a4 6c 22 20 63 61 73 65 20 6d 61 70 70 69 6e 67 2c  l" case mapping,
396a5 20 74 68 65 20 75 70 70 65 72 28 29 20 6f 72 20   the upper() or 
396a6 6c 6f 77 65 72 28 29 20 73 63 61 6c 61 72 20 0a  lower() scalar .
396a7 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65  ** functions are
396a8 20 69 6e 76 6f 6b 65 64 20 77 69 74 68 20 6f 6e   invoked with on
396a9 65 20 61 72 67 75 6d 65 6e 74 3a 0a 2a 2a 0a 2a  e argument:.**.*
396aa 2a 20 20 20 20 20 75 70 70 65 72 28 27 41 42 43  *     upper('ABC
396ab 27 29 20 2d 3e 20 27 61 62 63 27 0a 2a 2a 20 20  ') -> 'abc'.**  
396ac 20 20 20 6c 6f 77 65 72 28 27 61 62 63 27 29 20     lower('abc') 
396ad 2d 3e 20 27 41 42 43 27 0a 2a 2a 0a 2a 2a 20 54  -> 'ABC'.**.** T
396ae 6f 20 61 63 63 65 73 73 20 49 43 55 20 22 6c 61  o access ICU "la
396af 6e 67 75 61 67 65 20 73 70 65 63 69 66 69 63 22  nguage specific"
396b0 20 63 61 73 65 20 6d 61 70 70 69 6e 67 2c 20 75   case mapping, u
396b1 70 70 65 72 28 29 20 6f 72 20 6c 6f 77 65 72 28  pper() or lower(
396b2 29 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 69  ).** should be i
396b3 6e 76 6f 6b 65 64 20 77 69 74 68 20 74 77 6f 20  nvoked with two 
396b4 61 72 67 75 6d 65 6e 74 73 2e 20 54 68 65 20 73  arguments. The s
396b5 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69  econd argument i
396b6 73 20 74 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66  s the name.** of
396b7 20 74 68 65 20 6c 6f 63 61 6c 65 20 74 6f 20 75   the locale to u
396b8 73 65 2e 20 50 61 73 73 69 6e 67 20 61 6e 20 65  se. Passing an e
396b9 6d 70 74 79 20 73 74 72 69 6e 67 20 28 22 22 29  mpty string ("")
396ba 20 6f 72 20 53 51 4c 20 4e 55 4c 4c 20 76 61 6c   or SQL NULL val
396bb 75 65 0a 2a 2a 20 61 73 20 74 68 65 20 73 65 63  ue.** as the sec
396bc 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ond argument is 
396bd 74 68 65 20 73 61 6d 65 20 61 73 20 69 6e 76 6f  the same as invo
396be 6b 69 6e 67 20 74 68 65 20 31 20 61 72 67 75 6d  king the 1 argum
396bf 65 6e 74 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 6f  ent version.** o
396c0 66 20 75 70 70 65 72 28 29 20 6f 72 20 6c 6f 77  f upper() or low
396c1 65 72 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  er()..**.**     
396c2 6c 6f 77 65 72 28 27 49 27 2c 20 27 65 6e 5f 75  lower('I', 'en_u
396c3 73 27 29 20 2d 3e 20 27 69 27 0a 2a 2a 20 20 20  s') -> 'i'.**   
396c4 20 20 6c 6f 77 65 72 28 27 49 27 2c 20 27 74 72    lower('I', 'tr
396c5 5f 74 72 27 29 20 2d 3e 20 27 c4 b1 27 20 28 73  _tr') -> '..' (s
396c6 6d 61 6c 6c 20 64 6f 74 6c 65 73 73 20 69 29 0a  mall dotless i).
396c7 2a 2a 0a 2a 2a 20 68 74 74 70 3a 2f 2f 77 77 77  **.** http://www
396c8 2e 69 63 75 2d 70 72 6f 6a 65 63 74 2e 6f 72 67  .icu-project.org
396c9 2f 75 73 65 72 67 75 69 64 65 2f 70 6f 73 69 78  /userguide/posix
396ca 2e 68 74 6d 6c 23 63 61 73 65 5f 6d 61 70 70 69  .html#case_mappi
396cb 6e 67 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ngs.*/.static vo
396cc 69 64 20 69 63 75 43 61 73 65 46 75 6e 63 31 36  id icuCaseFunc16
396cd 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
396ce 20 2a 70 2c 20 69 6e 74 20 6e 41 72 67 2c 20 73   *p, int nArg, s
396cf 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
396d0 70 41 72 67 29 7b 0a 20 20 63 6f 6e 73 74 20 55  pArg){.  const U
396d1 43 68 61 72 20 2a 7a 49 6e 70 75 74 3b 0a 20 20  Char *zInput;.  
396d2 55 43 68 61 72 20 2a 7a 4f 75 74 70 75 74 3b 0a  UChar *zOutput;.
396d3 20 20 69 6e 74 20 6e 49 6e 70 75 74 3b 0a 20 20    int nInput;.  
396d4 69 6e 74 20 6e 4f 75 74 70 75 74 3b 0a 0a 20 20  int nOutput;..  
396d5 55 45 72 72 6f 72 43 6f 64 65 20 73 74 61 74 75  UErrorCode statu
396d6 73 20 3d 20 55 5f 5a 45 52 4f 5f 45 52 52 4f 52  s = U_ZERO_ERROR
396d7 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
396d8 7a 4c 6f 63 61 6c 65 20 3d 20 30 3b 0a 0a 20 20  zLocale = 0;..  
396d9 61 73 73 65 72 74 28 6e 41 72 67 3d 3d 31 20 7c  assert(nArg==1 |
396da 7c 20 6e 41 72 67 3d 3d 32 29 3b 0a 20 20 69 66  | nArg==2);.  if
396db 28 20 6e 41 72 67 3d 3d 32 20 29 7b 0a 20 20 20  ( nArg==2 ){.   
396dc 20 7a 4c 6f 63 61 6c 65 20 3d 20 28 63 6f 6e 73   zLocale = (cons
396dd 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33  t char *)sqlite3
396de 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 70 41 72  _value_text(apAr
396df 67 5b 31 5d 29 3b 0a 20 20 7d 0a 0a 20 20 7a 49  g[1]);.  }..  zI
396e0 6e 70 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 76  nput = sqlite3_v
396e1 61 6c 75 65 5f 74 65 78 74 31 36 28 61 70 41 72  alue_text16(apAr
396e2 67 5b 30 5d 29 3b 0a 20 20 69 66 28 20 21 7a 49  g[0]);.  if( !zI
396e3 6e 70 75 74 20 29 7b 0a 20 20 20 20 72 65 74 75  nput ){.    retu
396e4 72 6e 3b 0a 20 20 7d 0a 20 20 6e 49 6e 70 75 74  rn;.  }.  nInput
396e5 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
396e6 5f 62 79 74 65 73 31 36 28 61 70 41 72 67 5b 30  _bytes16(apArg[0
396e7 5d 29 3b 0a 0a 20 20 6e 4f 75 74 70 75 74 20 3d  ]);..  nOutput =
396e8 20 6e 49 6e 70 75 74 20 2a 20 32 20 2b 20 32 3b   nInput * 2 + 2;
396e9 0a 20 20 7a 4f 75 74 70 75 74 20 3d 20 73 71 6c  .  zOutput = sql
396ea 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 4f 75 74  ite3_malloc(nOut
396eb 70 75 74 29 3b 0a 20 20 69 66 28 20 21 7a 4f 75  put);.  if( !zOu
396ec 74 70 75 74 20 29 7b 0a 20 20 20 20 72 65 74 75  tput ){.    retu
396ed 72 6e 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73  rn;.  }..  if( s
396ee 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61  qlite3_user_data
396ef 28 70 29 20 29 7b 0a 20 20 20 20 75 5f 73 74 72  (p) ){.    u_str
396f0 54 6f 55 70 70 65 72 28 7a 4f 75 74 70 75 74 2c  ToUpper(zOutput,
396f1 20 6e 4f 75 74 70 75 74 2f 32 2c 20 7a 49 6e 70   nOutput/2, zInp
396f2 75 74 2c 20 6e 49 6e 70 75 74 2f 32 2c 20 7a 4c  ut, nInput/2, zL
396f3 6f 63 61 6c 65 2c 20 26 73 74 61 74 75 73 29 3b  ocale, &status);
396f4 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 5f  .  }else{.    u_
396f5 73 74 72 54 6f 4c 6f 77 65 72 28 7a 4f 75 74 70  strToLower(zOutp
396f6 75 74 2c 20 6e 4f 75 74 70 75 74 2f 32 2c 20 7a  ut, nOutput/2, z
396f7 49 6e 70 75 74 2c 20 6e 49 6e 70 75 74 2f 32 2c  Input, nInput/2,
396f8 20 7a 4c 6f 63 61 6c 65 2c 20 26 73 74 61 74 75   zLocale, &statu
396f9 73 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21  s);.  }..  if( !
396fa 55 5f 53 55 43 43 45 53 53 28 73 74 61 74 75 73  U_SUCCESS(status
396fb 29 20 29 7b 0a 20 20 20 20 69 63 75 46 75 6e 63  ) ){.    icuFunc
396fc 74 69 6f 6e 45 72 72 6f 72 28 70 2c 20 22 75 5f  tionError(p, "u_
396fd 73 74 72 54 6f 4c 6f 77 65 72 28 29 2f 75 5f 73  strToLower()/u_s
396fe 74 72 54 6f 55 70 70 65 72 22 2c 20 73 74 61 74  trToUpper", stat
396ff 75 73 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  us);.    return;
39700 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f  .  }..  sqlite3_
39701 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28 70 2c  result_text16(p,
39702 20 7a 4f 75 74 70 75 74 2c 20 2d 31 2c 20 78 46   zOutput, -1, xF
39703 72 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  ree);.}../*.** C
39704 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
39705 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e  e destructor fun
39706 63 74 69 6f 6e 2e 20 54 68 65 20 70 43 74 78 20  ction. The pCtx 
39707 61 72 67 75 6d 65 6e 74 20 70 6f 69 6e 74 73 20  argument points 
39708 74 6f 0a 2a 2a 20 61 20 55 43 6f 6c 6c 61 74 6f  to.** a UCollato
39709 72 20 73 74 72 75 63 74 75 72 65 20 70 72 65 76  r structure prev
3970a 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64  iously allocated
3970b 20 75 73 69 6e 67 20 75 63 6f 6c 5f 6f 70 65 6e   using ucol_open
3970c 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ()..*/.static vo
3970d 69 64 20 69 63 75 43 6f 6c 6c 61 74 69 6f 6e 44  id icuCollationD
3970e 65 6c 28 76 6f 69 64 20 2a 70 43 74 78 29 7b 0a  el(void *pCtx){.
3970f 20 20 55 43 6f 6c 6c 61 74 6f 72 20 2a 70 20 3d    UCollator *p =
39710 20 28 55 43 6f 6c 6c 61 74 6f 72 20 2a 29 70 43   (UCollator *)pC
39711 74 78 3b 0a 20 20 75 63 6f 6c 5f 63 6c 6f 73 65  tx;.  ucol_close
39712 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  (p);.}../*.** Co
39713 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
39714 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63   comparison func
39715 74 69 6f 6e 2e 20 54 68 65 20 70 43 74 78 20 61  tion. The pCtx a
39716 72 67 75 6d 65 6e 74 20 70 6f 69 6e 74 73 20 74  rgument points t
39717 6f 0a 2a 2a 20 61 20 55 43 6f 6c 6c 61 74 6f 72  o.** a UCollator
39718 20 73 74 72 75 63 74 75 72 65 20 70 72 65 76 69   structure previ
39719 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20  ously allocated 
3971a 75 73 69 6e 67 20 75 63 6f 6c 5f 6f 70 65 6e 28  using ucol_open(
3971b 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
3971c 20 69 63 75 43 6f 6c 6c 61 74 69 6f 6e 43 6f 6c   icuCollationCol
3971d 6c 28 0a 20 20 76 6f 69 64 20 2a 70 43 74 78 2c  l(.  void *pCtx,
3971e 0a 20 20 69 6e 74 20 6e 4c 65 66 74 2c 0a 20 20  .  int nLeft,.  
3971f 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 4c 65 66  const void *zLef
39720 74 2c 0a 20 20 69 6e 74 20 6e 52 69 67 68 74 2c  t,.  int nRight,
39721 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  .  const void *z
39722 52 69 67 68 74 0a 29 7b 0a 20 20 55 43 6f 6c 6c  Right.){.  UColl
39723 61 74 69 6f 6e 52 65 73 75 6c 74 20 72 65 73 3b  ationResult res;
39724 0a 20 20 55 43 6f 6c 6c 61 74 6f 72 20 2a 70 20  .  UCollator *p 
39725 3d 20 28 55 43 6f 6c 6c 61 74 6f 72 20 2a 29 70  = (UCollator *)p
39726 43 74 78 3b 0a 20 20 72 65 73 20 3d 20 75 63 6f  Ctx;.  res = uco
39727 6c 5f 73 74 72 63 6f 6c 6c 28 70 2c 20 28 55 43  l_strcoll(p, (UC
39728 68 61 72 20 2a 29 7a 4c 65 66 74 2c 20 6e 4c 65  har *)zLeft, nLe
39729 66 74 2f 32 2c 20 28 55 43 68 61 72 20 2a 29 7a  ft/2, (UChar *)z
3972a 52 69 67 68 74 2c 20 6e 52 69 67 68 74 2f 32 29  Right, nRight/2)
3972b 3b 0a 20 20 73 77 69 74 63 68 28 20 72 65 73 20  ;.  switch( res 
3972c 29 7b 0a 20 20 20 20 63 61 73 65 20 55 43 4f 4c  ){.    case UCOL
3972d 5f 4c 45 53 53 3a 20 20 20 20 72 65 74 75 72 6e  _LESS:    return
3972e 20 2d 31 3b 0a 20 20 20 20 63 61 73 65 20 55 43   -1;.    case UC
3972f 4f 4c 5f 47 52 45 41 54 45 52 3a 20 72 65 74 75  OL_GREATER: retu
39730 72 6e 20 2b 31 3b 0a 20 20 20 20 63 61 73 65 20  rn +1;.    case 
39731 55 43 4f 4c 5f 45 51 55 41 4c 3a 20 20 20 72 65  UCOL_EQUAL:   re
39732 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73  turn 0;.  }.  as
39733 73 65 72 74 28 21 22 55 6e 65 78 70 65 63 74 65  sert(!"Unexpecte
39734 64 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66  d return value f
39735 72 6f 6d 20 75 63 6f 6c 5f 73 74 72 63 6f 6c 6c  rom ucol_strcoll
39736 28 29 22 29 3b 0a 20 20 72 65 74 75 72 6e 20 30  ()");.  return 0
39737 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  ;.}../*.** Imple
39738 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
39739 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e   scalar function
3973a 20 69 63 75 5f 6c 6f 61 64 5f 63 6f 6c 6c 61 74   icu_load_collat
3973b 69 6f 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ion()..**.** Thi
3973c 73 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f  s scalar functio
3973d 6e 20 69 73 20 75 73 65 64 20 74 6f 20 61 64 64  n is used to add
3973e 20 49 43 55 20 63 6f 6c 6c 61 74 69 6f 6e 20 62   ICU collation b
3973f 61 73 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 0a  ased collation .
39740 2a 2a 20 74 79 70 65 73 20 74 6f 20 61 6e 20 53  ** types to an S
39741 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 20 63  QLite database c
39742 6f 6e 6e 65 63 74 69 6f 6e 2e 20 49 74 20 69 73  onnection. It is
39743 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20   intended to be 
39744 63 61 6c 6c 65 64 0a 2a 2a 20 61 73 20 66 6f 6c  called.** as fol
39745 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  lows:.**.**     
39746 53 45 4c 45 43 54 20 69 63 75 5f 6c 6f 61 64 5f  SELECT icu_load_
39747 63 6f 6c 6c 61 74 69 6f 6e 28 3c 6c 6f 63 61 6c  collation(<local
39748 65 3e 2c 20 3c 63 6f 6c 6c 61 74 69 6f 6e 2d 6e  e>, <collation-n
39749 61 6d 65 3e 29 3b 0a 2a 2a 0a 2a 2a 20 57 68 65  ame>);.**.** Whe
3974a 72 65 20 3c 6c 6f 63 61 6c 65 3e 20 69 73 20 61  re <locale> is a
3974b 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69   string containi
3974c 6e 67 20 61 6e 20 49 43 55 20 6c 6f 63 61 6c 65  ng an ICU locale
3974d 20 69 64 65 6e 74 69 66 69 65 72 20 28 69 2e 65   identifier (i.e
3974e 2e 0a 2a 2a 20 22 65 6e 5f 41 55 22 2c 20 22 74  ..** "en_AU", "t
3974f 72 5f 54 52 22 20 65 74 63 2e 29 20 61 6e 64 20  r_TR" etc.) and 
39750 3c 63 6f 6c 6c 61 74 69 6f 6e 2d 6e 61 6d 65 3e  <collation-name>
39751 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   is the name of 
39752 74 68 65 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e  the.** collation
39753 20 73 65 71 75 65 6e 63 65 20 74 6f 20 63 72 65   sequence to cre
39754 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ate..*/.static v
39755 6f 69 64 20 69 63 75 4c 6f 61 64 43 6f 6c 6c 61  oid icuLoadColla
39756 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f  tion(.  sqlite3_
39757 63 6f 6e 74 65 78 74 20 2a 70 2c 20 0a 20 20 69  context *p, .  i
39758 6e 74 20 6e 41 72 67 2c 20 0a 20 20 73 71 6c 69  nt nArg, .  sqli
39759 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 41 72  te3_value **apAr
3975a 67 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  g.){.  sqlite3 *
3975b 64 62 20 3d 20 28 73 71 6c 69 74 65 33 20 2a 29  db = (sqlite3 *)
3975c 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74  sqlite3_user_dat
3975d 61 28 70 29 3b 0a 20 20 55 45 72 72 6f 72 43 6f  a(p);.  UErrorCo
3975e 64 65 20 73 74 61 74 75 73 20 3d 20 55 5f 5a 45  de status = U_ZE
3975f 52 4f 5f 45 52 52 4f 52 3b 0a 20 20 63 6f 6e 73  RO_ERROR;.  cons
39760 74 20 63 68 61 72 20 2a 7a 4c 6f 63 61 6c 65 3b  t char *zLocale;
39761 20 20 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 65 20        /* Locale 
39762 69 64 65 6e 74 69 66 69 65 72 20 2d 20 28 65 67  identifier - (eg
39763 2e 20 22 6a 70 5f 4a 50 22 29 20 2a 2f 0a 20 20  . "jp_JP") */.  
39764 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
39765 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c  e;        /* SQL
39766 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   Collation seque
39767 6e 63 65 20 6e 61 6d 65 20 28 65 67 2e 20 22 6a  nce name (eg. "j
39768 61 70 61 6e 65 73 65 22 29 20 2a 2f 0a 20 20 55  apanese") */.  U
39769 43 6f 6c 6c 61 74 6f 72 20 2a 70 55 43 6f 6c 6c  Collator *pUColl
3976a 61 74 6f 72 3b 20 20 20 20 2f 2a 20 49 43 55 20  ator;    /* ICU 
3976b 6c 69 62 72 61 72 79 20 63 6f 6c 6c 61 74 69 6f  library collatio
3976c 6e 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e  n object */.  in
3976d 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
3976e 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
3976f 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 71 6c 69  n code from sqli
39770 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
39771 74 69 6f 6e 5f 78 28 29 20 2a 2f 0a 0a 20 20 61  tion_x() */..  a
39772 73 73 65 72 74 28 6e 41 72 67 3d 3d 32 29 3b 0a  ssert(nArg==2);.
39773 20 20 7a 4c 6f 63 61 6c 65 20 3d 20 28 63 6f 6e    zLocale = (con
39774 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65  st char *)sqlite
39775 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 70 41  3_value_text(apA
39776 72 67 5b 30 5d 29 3b 0a 20 20 7a 4e 61 6d 65 20  rg[0]);.  zName 
39777 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
39778 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
39779 78 74 28 61 70 41 72 67 5b 31 5d 29 3b 0a 0a 20  xt(apArg[1]);.. 
3977a 20 69 66 28 20 21 7a 4c 6f 63 61 6c 65 20 7c 7c   if( !zLocale ||
3977b 20 21 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 72   !zName ){.    r
3977c 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 70 55  eturn;.  }..  pU
3977d 43 6f 6c 6c 61 74 6f 72 20 3d 20 75 63 6f 6c 5f  Collator = ucol_
3977e 6f 70 65 6e 28 7a 4c 6f 63 61 6c 65 2c 20 26 73  open(zLocale, &s
3977f 74 61 74 75 73 29 3b 0a 20 20 69 66 28 20 21 55  tatus);.  if( !U
39780 5f 53 55 43 43 45 53 53 28 73 74 61 74 75 73 29  _SUCCESS(status)
39781 20 29 7b 0a 20 20 20 20 69 63 75 46 75 6e 63 74   ){.    icuFunct
39782 69 6f 6e 45 72 72 6f 72 28 70 2c 20 22 75 63 6f  ionError(p, "uco
39783 6c 5f 6f 70 65 6e 22 2c 20 73 74 61 74 75 73 29  l_open", status)
39784 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
39785 7d 0a 20 20 61 73 73 65 72 74 28 70 29 3b 0a 0a  }.  assert(p);..
39786 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63    rc = sqlite3_c
39787 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f  reate_collation_
39788 76 32 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 53 51  v2(db, zName, SQ
39789 4c 49 54 45 5f 55 54 46 31 36 2c 20 28 76 6f 69  LITE_UTF16, (voi
3978a 64 20 2a 29 70 55 43 6f 6c 6c 61 74 6f 72 2c 20  d *)pUCollator, 
3978b 0a 20 20 20 20 20 20 69 63 75 43 6f 6c 6c 61 74  .      icuCollat
3978c 69 6f 6e 43 6f 6c 6c 2c 20 69 63 75 43 6f 6c 6c  ionColl, icuColl
3978d 61 74 69 6f 6e 44 65 6c 0a 20 20 29 3b 0a 20 20  ationDel.  );.  
3978e 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
3978f 4b 20 29 7b 0a 20 20 20 20 75 63 6f 6c 5f 63 6c  K ){.    ucol_cl
39790 6f 73 65 28 70 55 43 6f 6c 6c 61 74 6f 72 29 3b  ose(pUCollator);
39791 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
39792 75 6c 74 5f 65 72 72 6f 72 28 70 2c 20 22 45 72  ult_error(p, "Er
39793 72 6f 72 20 72 65 67 69 73 74 65 72 69 6e 67 20  ror registering 
39794 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69  collation functi
39795 6f 6e 22 2c 20 2d 31 29 3b 0a 20 20 7d 0a 7d 0a  on", -1);.  }.}.
39796 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20  ./*.** Register 
39797 74 68 65 20 49 43 55 20 65 78 74 65 6e 73 69 6f  the ICU extensio
39798 6e 20 66 75 6e 63 74 69 6f 6e 73 20 77 69 74 68  n functions with
39799 20 64 61 74 61 62 61 73 65 20 64 62 2e 0a 2a 2f   database db..*/
3979a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
3979b 69 6e 74 20 73 71 6c 69 74 65 33 49 63 75 49 6e  int sqlite3IcuIn
3979c 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  it(sqlite3 *db){
3979d 0a 20 20 73 74 72 75 63 74 20 49 63 75 53 63 61  .  struct IcuSca
3979e 6c 61 72 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20  lar {.    const 
3979f 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20  char *zName;    
397a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
397a1 20 20 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20      /* Function 
397a2 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20  name */.    int 
397a3 6e 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20  nArg;           
397a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
397a5 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
397a6 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a  of arguments */.
397a7 20 20 20 20 69 6e 74 20 65 6e 63 3b 20 20 20 20      int enc;    
397a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
397a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
397aa 20 4f 70 74 69 6d 61 6c 20 74 65 78 74 20 65 6e   Optimal text en
397ab 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 20 20 76 6f  coding */.    vo
397ac 69 64 20 2a 70 43 6f 6e 74 65 78 74 3b 20 20 20  id *pContext;   
397ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
397ae 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74          /* sqlit
397af 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 20 63  e3_user_data() c
397b0 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 20 20 76 6f  ontext */.    vo
397b1 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69  id (*xFunc)(sqli
397b2 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
397b3 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a  ,sqlite3_value**
397b4 29 3b 0a 20 20 7d 20 73 63 61 6c 61 72 73 5b 5d  );.  } scalars[]
397b5 20 3d 20 7b 0a 20 20 20 20 7b 22 72 65 67 65 78   = {.    {"regex
397b6 70 22 2c 2d 31 2c 20 53 51 4c 49 54 45 5f 41 4e  p",-1, SQLITE_AN
397b7 59 2c 20 20 20 20 20 20 20 20 20 20 30 2c 20 69  Y,          0, i
397b8 63 75 52 65 67 65 78 70 46 75 6e 63 7d 2c 0a 0a  cuRegexpFunc},..
397b9 20 20 20 20 7b 22 6c 6f 77 65 72 22 2c 20 20 31      {"lower",  1
397ba 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 2c 20  , SQLITE_UTF16, 
397bb 20 20 20 20 20 20 20 30 2c 20 69 63 75 43 61 73         0, icuCas
397bc 65 46 75 6e 63 31 36 7d 2c 0a 20 20 20 20 7b 22  eFunc16},.    {"
397bd 6c 6f 77 65 72 22 2c 20 20 32 2c 20 53 51 4c 49  lower",  2, SQLI
397be 54 45 5f 55 54 46 31 36 2c 20 20 20 20 20 20 20  TE_UTF16,       
397bf 20 30 2c 20 69 63 75 43 61 73 65 46 75 6e 63 31   0, icuCaseFunc1
397c0 36 7d 2c 0a 20 20 20 20 7b 22 75 70 70 65 72 22  6},.    {"upper"
397c1 2c 20 20 31 2c 20 53 51 4c 49 54 45 5f 55 54 46  ,  1, SQLITE_UTF
397c2 31 36 2c 20 28 76 6f 69 64 2a 29 31 2c 20 69 63  16, (void*)1, ic
397c3 75 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a 20 20  uCaseFunc16},.  
397c4 20 20 7b 22 75 70 70 65 72 22 2c 20 20 32 2c 20    {"upper",  2, 
397c5 53 51 4c 49 54 45 5f 55 54 46 31 36 2c 20 28 76  SQLITE_UTF16, (v
397c6 6f 69 64 2a 29 31 2c 20 69 63 75 43 61 73 65 46  oid*)1, icuCaseF
397c7 75 6e 63 31 36 7d 2c 0a 0a 20 20 20 20 7b 22 6c  unc16},..    {"l
397c8 6f 77 65 72 22 2c 20 20 31 2c 20 53 51 4c 49 54  ower",  1, SQLIT
397c9 45 5f 55 54 46 38 2c 20 20 20 20 20 20 20 20 20  E_UTF8,         
397ca 30 2c 20 69 63 75 43 61 73 65 46 75 6e 63 31 36  0, icuCaseFunc16
397cb 7d 2c 0a 20 20 20 20 7b 22 6c 6f 77 65 72 22 2c  },.    {"lower",
397cc 20 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38    2, SQLITE_UTF8
397cd 2c 20 20 20 20 20 20 20 20 20 30 2c 20 69 63 75  ,         0, icu
397ce 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a 20 20 20  CaseFunc16},.   
397cf 20 7b 22 75 70 70 65 72 22 2c 20 20 31 2c 20 53   {"upper",  1, S
397d0 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 28 76 6f  QLITE_UTF8,  (vo
397d1 69 64 2a 29 31 2c 20 69 63 75 43 61 73 65 46 75  id*)1, icuCaseFu
397d2 6e 63 31 36 7d 2c 0a 20 20 20 20 7b 22 75 70 70  nc16},.    {"upp
397d3 65 72 22 2c 20 20 32 2c 20 53 51 4c 49 54 45 5f  er",  2, SQLITE_
397d4 55 54 46 38 2c 20 20 28 76 6f 69 64 2a 29 31 2c  UTF8,  (void*)1,
397d5 20 69 63 75 43 61 73 65 46 75 6e 63 31 36 7d 2c   icuCaseFunc16},
397d6 0a 0a 20 20 20 20 7b 22 6c 69 6b 65 22 2c 20 20  ..    {"like",  
397d7 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   2, SQLITE_UTF8,
397d8 20 20 20 20 20 20 20 20 20 30 2c 20 69 63 75 4c           0, icuL
397d9 69 6b 65 46 75 6e 63 7d 2c 0a 20 20 20 20 7b 22  ikeFunc},.    {"
397da 6c 69 6b 65 22 2c 20 20 20 33 2c 20 53 51 4c 49  like",   3, SQLI
397db 54 45 5f 55 54 46 38 2c 20 20 20 20 20 20 20 20  TE_UTF8,        
397dc 20 30 2c 20 69 63 75 4c 69 6b 65 46 75 6e 63 7d   0, icuLikeFunc}
397dd 2c 0a 0a 20 20 20 20 7b 22 69 63 75 5f 6c 6f 61  ,..    {"icu_loa
397de 64 5f 63 6f 6c 6c 61 74 69 6f 6e 22 2c 20 20 32  d_collation",  2
397df 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 28  , SQLITE_UTF8, (
397e0 76 6f 69 64 2a 29 64 62 2c 20 69 63 75 4c 6f 61  void*)db, icuLoa
397e1 64 43 6f 6c 6c 61 74 69 6f 6e 7d 2c 0a 20 20 7d  dCollation},.  }
397e2 3b 0a 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ;..  int rc = SQ
397e3 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69  LITE_OK;.  int i
397e4 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 72 63  ;..  for(i=0; rc
397e5 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69  ==SQLITE_OK && i
397e6 3c 28 73 69 7a 65 6f 66 28 73 63 61 6c 61 72 73  <(sizeof(scalars
397e7 29 2f 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20  )/sizeof(struct 
397e8 49 63 75 53 63 61 6c 61 72 29 29 3b 20 69 2b 2b  IcuScalar)); i++
397e9 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 49 63  ){.    struct Ic
397ea 75 53 63 61 6c 61 72 20 2a 70 20 3d 20 26 73 63  uScalar *p = &sc
397eb 61 6c 61 72 73 5b 69 5d 3b 0a 20 20 20 20 72 63  alars[i];.    rc
397ec 20 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74   = sqlite3_creat
397ed 65 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20 20 20  e_function(.    
397ee 20 20 20 20 64 62 2c 20 70 2d 3e 7a 4e 61 6d 65      db, p->zName
397ef 2c 20 70 2d 3e 6e 41 72 67 2c 20 70 2d 3e 65 6e  , p->nArg, p->en
397f0 63 2c 20 70 2d 3e 70 43 6f 6e 74 65 78 74 2c 20  c, p->pContext, 
397f1 70 2d 3e 78 46 75 6e 63 2c 20 30 2c 20 30 0a 20  p->xFunc, 0, 0. 
397f2 20 20 20 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74     );.  }..  ret
397f3 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 21  urn rc;.}..#if !
397f4 53 51 4c 49 54 45 5f 43 4f 52 45 0a 53 51 4c 49  SQLITE_CORE.SQLI
397f5 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
397f6 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69  e3_extension_ini
397f7 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  t(.  sqlite3 *db
397f8 2c 20 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72  , .  char **pzEr
397f9 72 4d 73 67 2c 0a 20 20 63 6f 6e 73 74 20 73 71  rMsg,.  const sq
397fa 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e  lite3_api_routin
397fb 65 73 20 2a 70 41 70 69 0a 29 7b 0a 20 20 53 51  es *pApi.){.  SQ
397fc 4c 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49  LITE_EXTENSION_I
397fd 4e 49 54 32 28 70 41 70 69 29 0a 20 20 72 65 74  NIT2(pApi).  ret
397fe 75 72 6e 20 73 71 6c 69 74 65 33 49 63 75 49 6e  urn sqlite3IcuIn
397ff 69 74 28 64 62 29 3b 0a 7d 0a 23 65 6e 64 69 66  it(db);.}.#endif
39800 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a  ..#endif../*****
39801 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
39802 20 69 63 75 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a   icu.c *********
39803 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39804 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39805 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
39806 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
39807 66 69 6c 65 20 66 74 73 33 5f 69 63 75 2e 63 20  file fts3_icu.c 
39808 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39809 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3980a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
3980b 32 30 30 37 20 4a 75 6e 65 20 32 32 0a 2a 2a 0a  2007 June 22.**.
3980c 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
3980d 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
3980e 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
3980f 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
39810 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
39811 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
39812 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
39813 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
39814 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
39815 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
39816 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
39817 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
39818 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
39819 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
3981a 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
3981b 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
3981c 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
3981d 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
3981e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3981f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39821 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
39822 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65  his file impleme
39823 6e 74 73 20 61 20 74 6f 6b 65 6e 69 7a 65 72 20  nts a tokenizer 
39824 66 6f 72 20 66 74 73 33 20 62 61 73 65 64 20 6f  for fts3 based o
39825 6e 20 74 68 65 20 49 43 55 20 6c 69 62 72 61 72  n the ICU librar
39826 79 2e 0a 2a 2a 20 0a 2a 2a 20 24 49 64 3a 20 66  y..** .** $Id: f
39827 74 73 33 5f 69 63 75 2e 63 2c 76 20 31 2e 33 20  ts3_icu.c,v 1.3 
39828 32 30 30 38 2f 30 39 2f 30 31 20 31 38 3a 33 34  2008/09/01 18:34
39829 3a 32 30 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :20 danielk1977 
3982a 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 20 21 64  Exp $.*/..#if !d
3982b 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f  efined(SQLITE_CO
3982c 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  RE) || defined(S
3982d 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
3982e 33 29 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  3).#ifdef SQLITE
3982f 5f 45 4e 41 42 4c 45 5f 49 43 55 0a 0a 0a 23 69  _ENABLE_ICU...#i
39830 6e 63 6c 75 64 65 20 3c 75 6e 69 63 6f 64 65 2f  nclude <unicode/
39831 75 62 72 6b 2e 68 3e 0a 23 69 6e 63 6c 75 64 65  ubrk.h>.#include
39832 20 3c 75 6e 69 63 6f 64 65 2f 75 74 66 31 36 2e   <unicode/utf16.
39833 68 3e 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75  h>..typedef stru
39834 63 74 20 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20  ct IcuTokenizer 
39835 49 63 75 54 6f 6b 65 6e 69 7a 65 72 3b 0a 74 79  IcuTokenizer;.ty
39836 70 65 64 65 66 20 73 74 72 75 63 74 20 49 63 75  pedef struct Icu
39837 43 75 72 73 6f 72 20 49 63 75 43 75 72 73 6f 72  Cursor IcuCursor
39838 3b 0a 0a 73 74 72 75 63 74 20 49 63 75 54 6f 6b  ;..struct IcuTok
39839 65 6e 69 7a 65 72 20 7b 0a 20 20 73 71 6c 69 74  enizer {.  sqlit
3983a 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 62 61 73  e3_tokenizer bas
3983b 65 3b 0a 20 20 63 68 61 72 20 2a 7a 4c 6f 63 61  e;.  char *zLoca
3983c 6c 65 3b 0a 7d 3b 0a 0a 73 74 72 75 63 74 20 49  le;.};..struct I
3983d 63 75 43 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c  cuCursor {.  sql
3983e 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
3983f 75 72 73 6f 72 20 62 61 73 65 3b 0a 0a 20 20 55  ursor base;..  U
39840 42 72 65 61 6b 49 74 65 72 61 74 6f 72 20 2a 70  BreakIterator *p
39841 49 74 65 72 3b 20 20 20 20 20 20 2f 2a 20 49 43  Iter;      /* IC
39842 55 20 62 72 65 61 6b 2d 69 74 65 72 61 74 6f 72  U break-iterator
39843 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74   object */.  int
39844 20 6e 43 68 61 72 3b 20 20 20 20 20 20 20 20 20   nChar;         
39845 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
39846 65 72 20 6f 66 20 55 43 68 61 72 20 65 6c 65 6d  er of UChar elem
39847 65 6e 74 73 20 69 6e 20 70 49 6e 70 75 74 20 2a  ents in pInput *
39848 2f 0a 20 20 55 43 68 61 72 20 2a 61 43 68 61 72  /.  UChar *aChar
39849 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
3984a 2f 2a 20 43 6f 70 79 20 6f 66 20 69 6e 70 75 74  /* Copy of input
3984b 20 75 73 69 6e 67 20 75 74 66 2d 31 36 20 65 6e   using utf-16 en
3984c 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20  coding */.  int 
3984d 2a 61 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20  *aOffset;       
3984e 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65          /* Offse
3984f 74 73 20 6f 66 20 65 61 63 68 20 63 68 61 72 61  ts of each chara
39850 63 74 65 72 20 69 6e 20 75 74 66 2d 38 20 69 6e  cter in utf-8 in
39851 70 75 74 20 2a 2f 0a 0a 20 20 69 6e 74 20 6e 42  put */..  int nB
39852 75 66 66 65 72 3b 0a 20 20 63 68 61 72 20 2a 7a  uffer;.  char *z
39853 42 75 66 66 65 72 3b 0a 0a 20 20 69 6e 74 20 69  Buffer;..  int i
39854 54 6f 6b 65 6e 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  Token;.};../*.**
39855 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 74 6f   Create a new to
39856 6b 65 6e 69 7a 65 72 20 69 6e 73 74 61 6e 63 65  kenizer instance
39857 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
39858 69 63 75 43 72 65 61 74 65 28 0a 20 20 69 6e 74  icuCreate(.  int
39859 20 61 72 67 63 2c 20 20 20 20 20 20 20 20 20 20   argc,          
3985a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3985b 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
3985c 6e 74 72 69 65 73 20 69 6e 20 61 72 67 76 5b 5d  ntries in argv[]
3985d 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
3985e 20 2a 20 63 6f 6e 73 74 20 2a 61 72 67 76 2c 20   * const *argv, 
3985f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f             /* To
39860 6b 65 6e 69 7a 65 72 20 63 72 65 61 74 69 6f 6e  kenizer creation
39861 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20   arguments */.  
39862 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
39863 72 20 2a 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 20  r **ppTokenizer 
39864 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 43 72 65       /* OUT: Cre
39865 61 74 65 64 20 74 6f 6b 65 6e 69 7a 65 72 20 2a  ated tokenizer *
39866 2f 0a 29 7b 0a 20 20 49 63 75 54 6f 6b 65 6e 69  /.){.  IcuTokeni
39867 7a 65 72 20 2a 70 3b 0a 20 20 69 6e 74 20 6e 20  zer *p;.  int n 
39868 3d 20 30 3b 0a 0a 20 20 69 66 28 20 61 72 67 63  = 0;..  if( argc
39869 3e 30 20 29 7b 0a 20 20 20 20 6e 20 3d 20 73 74  >0 ){.    n = st
3986a 72 6c 65 6e 28 61 72 67 76 5b 30 5d 29 2b 31 3b  rlen(argv[0])+1;
3986b 0a 20 20 7d 0a 20 20 70 20 3d 20 28 49 63 75 54  .  }.  p = (IcuT
3986c 6f 6b 65 6e 69 7a 65 72 20 2a 29 73 71 6c 69 74  okenizer *)sqlit
3986d 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66  e3_malloc(sizeof
3986e 28 49 63 75 54 6f 6b 65 6e 69 7a 65 72 29 2b 6e  (IcuTokenizer)+n
3986f 29 3b 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20  );.  if( !p ){. 
39870 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
39871 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65  _NOMEM;.  }.  me
39872 6d 73 65 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f  mset(p, 0, sizeo
39873 66 28 49 63 75 54 6f 6b 65 6e 69 7a 65 72 29 29  f(IcuTokenizer))
39874 3b 0a 0a 20 20 69 66 28 20 6e 20 29 7b 0a 20 20  ;..  if( n ){.  
39875 20 20 70 2d 3e 7a 4c 6f 63 61 6c 65 20 3d 20 28    p->zLocale = (
39876 63 68 61 72 20 2a 29 26 70 5b 31 5d 3b 0a 20 20  char *)&p[1];.  
39877 20 20 6d 65 6d 63 70 79 28 70 2d 3e 7a 4c 6f 63    memcpy(p->zLoc
39878 61 6c 65 2c 20 61 72 67 76 5b 30 5d 2c 20 6e 29  ale, argv[0], n)
39879 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70 54 6f 6b 65  ;.  }..  *ppToke
3987a 6e 69 7a 65 72 20 3d 20 28 73 71 6c 69 74 65 33  nizer = (sqlite3
3987b 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 29 70 3b 0a  _tokenizer *)p;.
3987c 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
3987d 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  _OK;.}../*.** De
3987e 73 74 72 6f 79 20 61 20 74 6f 6b 65 6e 69 7a 65  stroy a tokenize
3987f 72 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  r.*/.static int 
39880 69 63 75 44 65 73 74 72 6f 79 28 73 71 6c 69 74  icuDestroy(sqlit
39881 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54  e3_tokenizer *pT
39882 6f 6b 65 6e 69 7a 65 72 29 7b 0a 20 20 49 63 75  okenizer){.  Icu
39883 54 6f 6b 65 6e 69 7a 65 72 20 2a 70 20 3d 20 28  Tokenizer *p = (
39884 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20 2a 29 70  IcuTokenizer *)p
39885 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 73 71 6c  Tokenizer;.  sql
39886 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20  ite3_free(p);.  
39887 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
39888 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61  ;.}../*.** Prepa
39889 72 65 20 74 6f 20 62 65 67 69 6e 20 74 6f 6b 65  re to begin toke
3988a 6e 69 7a 69 6e 67 20 61 20 70 61 72 74 69 63 75  nizing a particu
3988b 6c 61 72 20 73 74 72 69 6e 67 2e 20 20 54 68 65  lar string.  The
3988c 20 69 6e 70 75 74 0a 2a 2a 20 73 74 72 69 6e 67   input.** string
3988d 20 74 6f 20 62 65 20 74 6f 6b 65 6e 69 7a 65 64   to be tokenized
3988e 20 69 73 20 70 49 6e 70 75 74 5b 30 2e 2e 6e 42   is pInput[0..nB
3988f 79 74 65 73 2d 31 5d 2e 20 20 41 20 63 75 72 73  ytes-1].  A curs
39890 6f 72 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69 6e  or.** used to in
39891 63 72 65 6d 65 6e 74 61 6c 6c 79 20 74 6f 6b 65  crementally toke
39892 6e 69 7a 65 20 74 68 69 73 20 73 74 72 69 6e 67  nize this string
39893 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20   is returned in 
39894 0a 2a 2a 20 2a 70 70 43 75 72 73 6f 72 2e 0a 2a  .** *ppCursor..*
39895 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 63 75  /.static int icu
39896 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f  Open(.  sqlite3_
39897 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65  tokenizer *pToke
39898 6e 69 7a 65 72 2c 20 20 20 20 20 20 20 20 20 2f  nizer,         /
39899 2a 20 54 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20  * The tokenizer 
3989a 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
3989b 2a 7a 49 6e 70 75 74 2c 20 20 20 20 20 20 20 20  *zInput,        
3989c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
3989d 6e 70 75 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20  nput string */. 
3989e 20 69 6e 74 20 6e 49 6e 70 75 74 2c 20 20 20 20   int nInput,    
3989f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
398a0 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
398a1 68 20 6f 66 20 7a 49 6e 70 75 74 20 69 6e 20 62  h of zInput in b
398a2 79 74 65 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ytes */.  sqlite
398a3 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
398a4 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 20 20 20  or **ppCursor   
398a5 20 2f 2a 20 4f 55 54 3a 20 54 6f 6b 65 6e 69 7a   /* OUT: Tokeniz
398a6 61 74 69 6f 6e 20 63 75 72 73 6f 72 20 2a 2f 0a  ation cursor */.
398a7 29 7b 0a 20 20 49 63 75 54 6f 6b 65 6e 69 7a 65  ){.  IcuTokenize
398a8 72 20 2a 70 20 3d 20 28 49 63 75 54 6f 6b 65 6e  r *p = (IcuToken
398a9 69 7a 65 72 20 2a 29 70 54 6f 6b 65 6e 69 7a 65  izer *)pTokenize
398aa 72 3b 0a 20 20 49 63 75 43 75 72 73 6f 72 20 2a  r;.  IcuCursor *
398ab 70 43 73 72 3b 0a 0a 20 20 63 6f 6e 73 74 20 69  pCsr;..  const i
398ac 6e 74 33 32 5f 74 20 6f 70 74 20 3d 20 55 5f 46  nt32_t opt = U_F
398ad 4f 4c 44 5f 43 41 53 45 5f 44 45 46 41 55 4c 54  OLD_CASE_DEFAULT
398ae 3b 0a 20 20 55 45 72 72 6f 72 43 6f 64 65 20 73  ;.  UErrorCode s
398af 74 61 74 75 73 20 3d 20 55 5f 5a 45 52 4f 5f 45  tatus = U_ZERO_E
398b0 52 52 4f 52 3b 0a 20 20 69 6e 74 20 6e 43 68 61  RROR;.  int nCha
398b1 72 3b 0a 0a 20 20 55 43 68 61 72 33 32 20 63 3b  r;..  UChar32 c;
398b2 0a 20 20 69 6e 74 20 69 49 6e 70 75 74 20 3d 20  .  int iInput = 
398b3 30 3b 0a 20 20 69 6e 74 20 69 4f 75 74 20 3d 20  0;.  int iOut = 
398b4 30 3b 0a 0a 20 20 2a 70 70 43 75 72 73 6f 72 20  0;..  *ppCursor 
398b5 3d 20 30 3b 0a 0a 20 20 69 66 28 20 6e 49 6e 70  = 0;..  if( nInp
398b6 75 74 3c 30 20 29 7b 0a 20 20 20 20 6e 49 6e 70  ut<0 ){.    nInp
398b7 75 74 20 3d 20 73 74 72 6c 65 6e 28 7a 49 6e 70  ut = strlen(zInp
398b8 75 74 29 3b 0a 20 20 7d 0a 20 20 6e 43 68 61 72  ut);.  }.  nChar
398b9 20 3d 20 6e 49 6e 70 75 74 2b 31 3b 0a 20 20 70   = nInput+1;.  p
398ba 43 73 72 20 3d 20 28 49 63 75 43 75 72 73 6f 72  Csr = (IcuCursor
398bb 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   *)sqlite3_mallo
398bc 63 28 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28  c(.      sizeof(
398bd 49 63 75 43 75 72 73 6f 72 29 20 2b 20 20 20 20  IcuCursor) +    
398be 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
398bf 63 75 43 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20  cuCursor */.    
398c0 20 20 6e 43 68 61 72 20 2a 20 73 69 7a 65 6f 66    nChar * sizeof
398c1 28 55 43 68 61 72 29 20 2b 20 20 20 20 20 20 20  (UChar) +       
398c2 20 20 20 20 20 2f 2a 20 49 63 75 43 75 72 73 6f       /* IcuCurso
398c3 72 2e 61 43 68 61 72 5b 5d 20 2a 2f 0a 20 20 20  r.aChar[] */.   
398c4 20 20 20 28 6e 43 68 61 72 2b 31 29 20 2a 20 73     (nChar+1) * s
398c5 69 7a 65 6f 66 28 69 6e 74 29 20 20 20 20 20 20  izeof(int)      
398c6 20 20 20 20 20 20 2f 2a 20 49 63 75 43 75 72 73        /* IcuCurs
398c7 6f 72 2e 61 4f 66 66 73 65 74 5b 5d 20 2a 2f 0a  or.aOffset[] */.
398c8 20 20 29 3b 0a 20 20 69 66 28 20 21 70 43 73 72    );.  if( !pCsr
398c9 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
398ca 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
398cb 0a 20 20 6d 65 6d 73 65 74 28 70 43 73 72 2c 20  .  memset(pCsr, 
398cc 30 2c 20 73 69 7a 65 6f 66 28 49 63 75 43 75 72  0, sizeof(IcuCur
398cd 73 6f 72 29 29 3b 0a 20 20 70 43 73 72 2d 3e 61  sor));.  pCsr->a
398ce 43 68 61 72 20 3d 20 28 55 43 68 61 72 20 2a 29  Char = (UChar *)
398cf 26 70 43 73 72 5b 31 5d 3b 0a 20 20 70 43 73 72  &pCsr[1];.  pCsr
398d0 2d 3e 61 4f 66 66 73 65 74 20 3d 20 28 69 6e 74  ->aOffset = (int
398d1 20 2a 29 26 70 43 73 72 2d 3e 61 43 68 61 72 5b   *)&pCsr->aChar[
398d2 6e 43 68 61 72 5d 3b 0a 0a 20 20 70 43 73 72 2d  nChar];..  pCsr-
398d3 3e 61 4f 66 66 73 65 74 5b 69 4f 75 74 5d 20 3d  >aOffset[iOut] =
398d4 20 69 49 6e 70 75 74 3b 0a 20 20 55 38 5f 4e 45   iInput;.  U8_NE
398d5 58 54 28 7a 49 6e 70 75 74 2c 20 69 49 6e 70 75  XT(zInput, iInpu
398d6 74 2c 20 6e 49 6e 70 75 74 2c 20 63 29 3b 20 0a  t, nInput, c); .
398d7 20 20 77 68 69 6c 65 28 20 63 3e 30 20 29 7b 0a    while( c>0 ){.
398d8 20 20 20 20 69 6e 74 20 69 73 45 72 72 6f 72 20      int isError 
398d9 3d 20 30 3b 0a 20 20 20 20 63 20 3d 20 75 5f 66  = 0;.    c = u_f
398da 6f 6c 64 43 61 73 65 28 63 2c 20 6f 70 74 29 3b  oldCase(c, opt);
398db 0a 20 20 20 20 55 31 36 5f 41 50 50 45 4e 44 28  .    U16_APPEND(
398dc 70 43 73 72 2d 3e 61 43 68 61 72 2c 20 69 4f 75  pCsr->aChar, iOu
398dd 74 2c 20 6e 43 68 61 72 2c 20 63 2c 20 69 73 45  t, nChar, c, isE
398de 72 72 6f 72 29 3b 0a 20 20 20 20 69 66 28 20 69  rror);.    if( i
398df 73 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 20 20  sError ){.      
398e0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 73  sqlite3_free(pCs
398e1 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  r);.      return
398e2 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
398e3 20 20 20 7d 0a 20 20 20 20 70 43 73 72 2d 3e 61     }.    pCsr->a
398e4 4f 66 66 73 65 74 5b 69 4f 75 74 5d 20 3d 20 69  Offset[iOut] = i
398e5 49 6e 70 75 74 3b 0a 0a 20 20 20 20 69 66 28 20  Input;..    if( 
398e6 69 49 6e 70 75 74 3c 6e 49 6e 70 75 74 20 29 7b  iInput<nInput ){
398e7 0a 20 20 20 20 20 20 55 38 5f 4e 45 58 54 28 7a  .      U8_NEXT(z
398e8 49 6e 70 75 74 2c 20 69 49 6e 70 75 74 2c 20 6e  Input, iInput, n
398e9 49 6e 70 75 74 2c 20 63 29 3b 0a 20 20 20 20 7d  Input, c);.    }
398ea 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 20 3d 20  else{.      c = 
398eb 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  0;.    }.  }..  
398ec 70 43 73 72 2d 3e 70 49 74 65 72 20 3d 20 75 62  pCsr->pIter = ub
398ed 72 6b 5f 6f 70 65 6e 28 55 42 52 4b 5f 57 4f 52  rk_open(UBRK_WOR
398ee 44 2c 20 70 2d 3e 7a 4c 6f 63 61 6c 65 2c 20 70  D, p->zLocale, p
398ef 43 73 72 2d 3e 61 43 68 61 72 2c 20 69 4f 75 74  Csr->aChar, iOut
398f0 2c 20 26 73 74 61 74 75 73 29 3b 0a 20 20 69 66  , &status);.  if
398f1 28 20 21 55 5f 53 55 43 43 45 53 53 28 73 74 61  ( !U_SUCCESS(sta
398f2 74 75 73 29 20 29 7b 0a 20 20 20 20 73 71 6c 69  tus) ){.    sqli
398f3 74 65 33 5f 66 72 65 65 28 70 43 73 72 29 3b 0a  te3_free(pCsr);.
398f4 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
398f5 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 70  E_ERROR;.  }.  p
398f6 43 73 72 2d 3e 6e 43 68 61 72 20 3d 20 69 4f 75  Csr->nChar = iOu
398f7 74 3b 0a 0a 20 20 75 62 72 6b 5f 66 69 72 73 74  t;..  ubrk_first
398f8 28 70 43 73 72 2d 3e 70 49 74 65 72 29 3b 0a 20  (pCsr->pIter);. 
398f9 20 2a 70 70 43 75 72 73 6f 72 20 3d 20 28 73 71   *ppCursor = (sq
398fa 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
398fb 63 75 72 73 6f 72 20 2a 29 70 43 73 72 3b 0a 20  cursor *)pCsr;. 
398fc 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
398fd 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73  K;.}../*.** Clos
398fe 65 20 61 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e  e a tokenization
398ff 20 63 75 72 73 6f 72 20 70 72 65 76 69 6f 75 73   cursor previous
39900 6c 79 20 6f 70 65 6e 65 64 20 62 79 20 61 20 63  ly opened by a c
39901 61 6c 6c 20 74 6f 20 69 63 75 4f 70 65 6e 28 29  all to icuOpen()
39902 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
39903 69 63 75 43 6c 6f 73 65 28 73 71 6c 69 74 65 33  icuClose(sqlite3
39904 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
39905 72 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20 49  r *pCursor){.  I
39906 63 75 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d  cuCursor *pCsr =
39907 20 28 49 63 75 43 75 72 73 6f 72 20 2a 29 70 43   (IcuCursor *)pC
39908 75 72 73 6f 72 3b 0a 20 20 75 62 72 6b 5f 63 6c  ursor;.  ubrk_cl
39909 6f 73 65 28 70 43 73 72 2d 3e 70 49 74 65 72 29  ose(pCsr->pIter)
3990a 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
3990b 28 70 43 73 72 2d 3e 7a 42 75 66 66 65 72 29 3b  (pCsr->zBuffer);
3990c 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
3990d 70 43 73 72 29 3b 0a 20 20 72 65 74 75 72 6e 20  pCsr);.  return 
3990e 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
3990f 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20  .** Extract the 
39910 6e 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f 6d 20  next token from 
39911 61 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63  a tokenization c
39912 75 72 73 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  ursor..*/.static
39913 20 69 6e 74 20 69 63 75 4e 65 78 74 28 0a 20 20   int icuNext(.  
39914 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
39915 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f  r_cursor *pCurso
39916 72 2c 20 20 2f 2a 20 43 75 72 73 6f 72 20 72 65  r,  /* Cursor re
39917 74 75 72 6e 65 64 20 62 79 20 73 69 6d 70 6c 65  turned by simple
39918 4f 70 65 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  Open */.  const 
39919 63 68 61 72 20 2a 2a 70 70 54 6f 6b 65 6e 2c 20  char **ppToken, 
3991a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3991b 20 4f 55 54 3a 20 2a 70 70 54 6f 6b 65 6e 20 69   OUT: *ppToken i
3991c 73 20 74 68 65 20 74 6f 6b 65 6e 20 74 65 78 74  s the token text
3991d 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 42 79 74   */.  int *pnByt
3991e 65 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  es,             
3991f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
39920 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  : Number of byte
39921 73 20 69 6e 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20  s in token */.  
39922 69 6e 74 20 2a 70 69 53 74 61 72 74 4f 66 66 73  int *piStartOffs
39923 65 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  et,             
39924 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61 72      /* OUT: Star
39925 74 69 6e 67 20 6f 66 66 73 65 74 20 6f 66 20 74  ting offset of t
39926 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  oken */.  int *p
39927 69 45 6e 64 4f 66 66 73 65 74 2c 20 20 20 20 20  iEndOffset,     
39928 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
39929 20 4f 55 54 3a 20 45 6e 64 69 6e 67 20 6f 66 66   OUT: Ending off
3992a 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a  set of token */.
3992b 20 20 69 6e 74 20 2a 70 69 50 6f 73 69 74 69 6f    int *piPositio
3992c 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n               
3992d 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f        /* OUT: Po
3992e 73 69 74 69 6f 6e 20 69 6e 74 65 67 65 72 20 6f  sition integer o
3992f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20  f token */.){.  
39930 49 63 75 43 75 72 73 6f 72 20 2a 70 43 73 72 20  IcuCursor *pCsr 
39931 3d 20 28 49 63 75 43 75 72 73 6f 72 20 2a 29 70  = (IcuCursor *)p
39932 43 75 72 73 6f 72 3b 0a 0a 20 20 69 6e 74 20 69  Cursor;..  int i
39933 53 74 61 72 74 20 3d 20 30 3b 0a 20 20 69 6e 74  Start = 0;.  int
39934 20 69 45 6e 64 20 3d 20 30 3b 0a 20 20 69 6e 74   iEnd = 0;.  int
39935 20 6e 42 79 74 65 20 3d 20 30 3b 0a 0a 20 20 77   nByte = 0;..  w
39936 68 69 6c 65 28 20 69 53 74 61 72 74 3d 3d 69 45  hile( iStart==iE
39937 6e 64 20 29 7b 0a 20 20 20 20 55 43 68 61 72 33  nd ){.    UChar3
39938 32 20 63 3b 0a 0a 20 20 20 20 69 53 74 61 72 74  2 c;..    iStart
39939 20 3d 20 75 62 72 6b 5f 63 75 72 72 65 6e 74 28   = ubrk_current(
3993a 70 43 73 72 2d 3e 70 49 74 65 72 29 3b 0a 20 20  pCsr->pIter);.  
3993b 20 20 69 45 6e 64 20 3d 20 75 62 72 6b 5f 6e 65    iEnd = ubrk_ne
3993c 78 74 28 70 43 73 72 2d 3e 70 49 74 65 72 29 3b  xt(pCsr->pIter);
3993d 0a 20 20 20 20 69 66 28 20 69 45 6e 64 3d 3d 55  .    if( iEnd==U
3993e 42 52 4b 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20  BRK_DONE ){.    
3993f 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
39940 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  DONE;.    }..   
39941 20 77 68 69 6c 65 28 20 69 53 74 61 72 74 3c 69   while( iStart<i
39942 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  End ){.      int
39943 20 69 57 68 69 74 65 20 3d 20 69 53 74 61 72 74   iWhite = iStart
39944 3b 0a 20 20 20 20 20 20 55 38 5f 4e 45 58 54 28  ;.      U8_NEXT(
39945 70 43 73 72 2d 3e 61 43 68 61 72 2c 20 69 57 68  pCsr->aChar, iWh
39946 69 74 65 2c 20 70 43 73 72 2d 3e 6e 43 68 61 72  ite, pCsr->nChar
39947 2c 20 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , c);.      if( 
39948 75 5f 69 73 73 70 61 63 65 28 63 29 20 29 7b 0a  u_isspace(c) ){.
39949 20 20 20 20 20 20 20 20 69 53 74 61 72 74 20 3d          iStart =
3994a 20 69 57 68 69 74 65 3b 0a 20 20 20 20 20 20 7d   iWhite;.      }
3994b 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 62 72  else{.        br
3994c 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
3994d 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 69 53   }.    assert(iS
3994e 74 61 72 74 3c 3d 69 45 6e 64 29 3b 0a 20 20 7d  tart<=iEnd);.  }
3994f 0a 0a 20 20 64 6f 20 7b 0a 20 20 20 20 55 45 72  ..  do {.    UEr
39950 72 6f 72 43 6f 64 65 20 73 74 61 74 75 73 20 3d  rorCode status =
39951 20 55 5f 5a 45 52 4f 5f 45 52 52 4f 52 3b 0a 20   U_ZERO_ERROR;. 
39952 20 20 20 69 66 28 20 6e 42 79 74 65 20 29 7b 0a     if( nByte ){.
39953 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 65 77        char *zNew
39954 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c   = sqlite3_reall
39955 6f 63 28 70 43 73 72 2d 3e 7a 42 75 66 66 65 72  oc(pCsr->zBuffer
39956 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20  , nByte);.      
39957 69 66 28 20 21 7a 4e 65 77 20 29 7b 0a 20 20 20  if( !zNew ){.   
39958 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
39959 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
3995a 7d 0a 20 20 20 20 20 20 70 43 73 72 2d 3e 7a 42  }.      pCsr->zB
3995b 75 66 66 65 72 20 3d 20 7a 4e 65 77 3b 0a 20 20  uffer = zNew;.  
3995c 20 20 20 20 70 43 73 72 2d 3e 6e 42 75 66 66 65      pCsr->nBuffe
3995d 72 20 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 7d  r = nByte;.    }
3995e 0a 0a 20 20 20 20 75 5f 73 74 72 54 6f 55 54 46  ..    u_strToUTF
3995f 38 28 0a 20 20 20 20 20 20 20 20 70 43 73 72 2d  8(.        pCsr-
39960 3e 7a 42 75 66 66 65 72 2c 20 70 43 73 72 2d 3e  >zBuffer, pCsr->
39961 6e 42 75 66 66 65 72 2c 20 26 6e 42 79 74 65 2c  nBuffer, &nByte,
39962 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 76 61      /* Output va
39963 72 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 26 70  rs */.        &p
39964 43 73 72 2d 3e 61 43 68 61 72 5b 69 53 74 61 72  Csr->aChar[iStar
39965 74 5d 2c 20 69 45 6e 64 2d 69 53 74 61 72 74 2c  t], iEnd-iStart,
39966 20 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20         /* Input 
39967 76 61 72 73 20 2a 2f 0a 20 20 20 20 20 20 20 20  vars */.        
39968 26 73 74 61 74 75 73 20 20 20 20 20 20 20 20 20  &status         
39969 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3996a 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70           /* Outp
3996b 75 74 20 73 75 63 63 65 73 73 2f 66 61 69 6c 75  ut success/failu
3996c 72 65 20 2a 2f 0a 20 20 20 20 29 3b 0a 20 20 7d  re */.    );.  }
3996d 20 77 68 69 6c 65 28 20 6e 42 79 74 65 3e 70 43   while( nByte>pC
3996e 73 72 2d 3e 6e 42 75 66 66 65 72 20 29 3b 0a 0a  sr->nBuffer );..
3996f 20 20 2a 70 70 54 6f 6b 65 6e 20 3d 20 70 43 73    *ppToken = pCs
39970 72 2d 3e 7a 42 75 66 66 65 72 3b 0a 20 20 2a 70  r->zBuffer;.  *p
39971 6e 42 79 74 65 73 20 3d 20 6e 42 79 74 65 3b 0a  nBytes = nByte;.
39972 20 20 2a 70 69 53 74 61 72 74 4f 66 66 73 65 74    *piStartOffset
39973 20 3d 20 70 43 73 72 2d 3e 61 4f 66 66 73 65 74   = pCsr->aOffset
39974 5b 69 53 74 61 72 74 5d 3b 0a 20 20 2a 70 69 45  [iStart];.  *piE
39975 6e 64 4f 66 66 73 65 74 20 3d 20 70 43 73 72 2d  ndOffset = pCsr-
39976 3e 61 4f 66 66 73 65 74 5b 69 45 6e 64 5d 3b 0a  >aOffset[iEnd];.
39977 20 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20 3d 20    *piPosition = 
39978 70 43 73 72 2d 3e 69 54 6f 6b 65 6e 2b 2b 3b 0a  pCsr->iToken++;.
39979 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
3997a 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  _OK;.}../*.** Th
3997b 65 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65  e set of routine
3997c 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  s that implement
3997d 20 74 68 65 20 73 69 6d 70 6c 65 20 74 6f 6b 65   the simple toke
3997e 6e 69 7a 65 72 0a 2a 2f 0a 73 74 61 74 69 63 20  nizer.*/.static 
3997f 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f  const sqlite3_to
39980 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 69  kenizer_module i
39981 63 75 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c  cuTokenizerModul
39982 65 20 3d 20 7b 0a 20 20 30 2c 20 20 20 20 20 20  e = {.  0,      
39983 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39984 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e       /* iVersion
39985 20 2a 2f 0a 20 20 69 63 75 43 72 65 61 74 65 2c   */.  icuCreate,
39986 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39987 20 20 20 2f 2a 20 78 43 72 65 61 74 65 20 20 2a     /* xCreate  *
39988 2f 0a 20 20 69 63 75 44 65 73 74 72 6f 79 2c 20  /.  icuDestroy, 
39989 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3998a 20 2f 2a 20 78 43 72 65 61 74 65 20 20 2a 2f 0a   /* xCreate  */.
3998b 20 20 69 63 75 4f 70 65 6e 2c 20 20 20 20 20 20    icuOpen,      
3998c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3998d 2a 20 78 4f 70 65 6e 20 20 20 20 2a 2f 0a 20 20  * xOpen    */.  
3998e 69 63 75 43 6c 6f 73 65 2c 20 20 20 20 20 20 20  icuClose,       
3998f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
39990 78 43 6c 6f 73 65 20 20 20 2a 2f 0a 20 20 69 63  xClose   */.  ic
39991 75 4e 65 78 74 2c 20 20 20 20 20 20 20 20 20 20  uNext,          
39992 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4e             /* xN
39993 65 78 74 20 20 20 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  ext    */.};../*
39994 0a 2a 2a 20 53 65 74 20 2a 70 70 4d 6f 64 75 6c  .** Set *ppModul
39995 65 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68  e to point at th
39996 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
39997 20 6f 66 20 74 68 65 20 49 43 55 20 74 6f 6b 65   of the ICU toke
39998 6e 69 7a 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  nizer..*/.SQLITE
39999 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
3999a 6c 69 74 65 33 46 74 73 33 49 63 75 54 6f 6b 65  lite3Fts3IcuToke
3999b 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 0a 20 20 73  nizerModule(.  s
3999c 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
3999d 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70  _module const**p
3999e 70 4d 6f 64 75 6c 65 0a 29 7b 0a 20 20 2a 70 70  pModule.){.  *pp
3999f 4d 6f 64 75 6c 65 20 3d 20 26 69 63 75 54 6f 6b  Module = &icuTok
399a0 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 3b 0a 7d 0a  enizerModule;.}.
399a1 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e  .#endif /* defin
399a2 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
399a3 5f 49 43 55 29 20 2a 2f 0a 23 65 6e 64 69 66 20  _ICU) */.#endif 
399a4 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  /* !defined(SQLI
399a5 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69  TE_CORE) || defi
399a6 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
399a7 45 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a  E_FTS3) */../***
399a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
399a9 6f 66 20 66 74 73 33 5f 69 63 75 2e 63 20 2a 2a  of fts3_icu.c **
399aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
399ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
399ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a              **********/.